pci_dma.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Copyright (C) 2000,2002-2005 Silicon Graphics, Inc. All rights reserved.
  7. *
  8. * Routines for PCI DMA mapping. See Documentation/DMA-API.txt for
  9. * a description of how these routines should be used.
  10. */
  11. #include <linux/gfp.h>
  12. #include <linux/module.h>
  13. #include <linux/dma-mapping.h>
  14. #include <asm/dma.h>
  15. #include <asm/sn/intr.h>
  16. #include <asm/sn/pcibus_provider_defs.h>
  17. #include <asm/sn/pcidev.h>
  18. #include <asm/sn/sn_sal.h>
  19. #define SG_ENT_VIRT_ADDRESS(sg) (sg_virt((sg)))
  20. #define SG_ENT_PHYS_ADDRESS(SG) virt_to_phys(SG_ENT_VIRT_ADDRESS(SG))
  21. /**
  22. * sn_dma_supported - test a DMA mask
  23. * @dev: device to test
  24. * @mask: DMA mask to test
  25. *
  26. * Return whether the given PCI device DMA address mask can be supported
  27. * properly. For example, if your device can only drive the low 24-bits
  28. * during PCI bus mastering, then you would pass 0x00ffffff as the mask to
  29. * this function. Of course, SN only supports devices that have 32 or more
  30. * address bits when using the PMU.
  31. */
  32. static int sn_dma_supported(struct device *dev, u64 mask)
  33. {
  34. BUG_ON(dev->bus != &pci_bus_type);
  35. if (mask < 0x7fffffff)
  36. return 0;
  37. return 1;
  38. }
  39. /**
  40. * sn_dma_set_mask - set the DMA mask
  41. * @dev: device to set
  42. * @dma_mask: new mask
  43. *
  44. * Set @dev's DMA mask if the hw supports it.
  45. */
  46. int sn_dma_set_mask(struct device *dev, u64 dma_mask)
  47. {
  48. BUG_ON(dev->bus != &pci_bus_type);
  49. if (!sn_dma_supported(dev, dma_mask))
  50. return 0;
  51. *dev->dma_mask = dma_mask;
  52. return 1;
  53. }
  54. EXPORT_SYMBOL(sn_dma_set_mask);
  55. /**
  56. * sn_dma_alloc_coherent - allocate memory for coherent DMA
  57. * @dev: device to allocate for
  58. * @size: size of the region
  59. * @dma_handle: DMA (bus) address
  60. * @flags: memory allocation flags
  61. *
  62. * dma_alloc_coherent() returns a pointer to a memory region suitable for
  63. * coherent DMA traffic to/from a PCI device. On SN platforms, this means
  64. * that @dma_handle will have the %PCIIO_DMA_CMD flag set.
  65. *
  66. * This interface is usually used for "command" streams (e.g. the command
  67. * queue for a SCSI controller). See Documentation/DMA-API.txt for
  68. * more information.
  69. */
  70. static void *sn_dma_alloc_coherent(struct device *dev, size_t size,
  71. dma_addr_t * dma_handle, gfp_t flags,
  72. struct dma_attrs *attrs)
  73. {
  74. void *cpuaddr;
  75. unsigned long phys_addr;
  76. int node;
  77. struct pci_dev *pdev = to_pci_dev(dev);
  78. struct sn_pcibus_provider *provider = SN_PCIDEV_BUSPROVIDER(pdev);
  79. BUG_ON(dev->bus != &pci_bus_type);
  80. /*
  81. * Allocate the memory.
  82. */
  83. node = pcibus_to_node(pdev->bus);
  84. if (likely(node >=0)) {
  85. struct page *p = alloc_pages_exact_node(node,
  86. flags, get_order(size));
  87. if (likely(p))
  88. cpuaddr = page_address(p);
  89. else
  90. return NULL;
  91. } else
  92. cpuaddr = (void *)__get_free_pages(flags, get_order(size));
  93. if (unlikely(!cpuaddr))
  94. return NULL;
  95. memset(cpuaddr, 0x0, size);
  96. /* physical addr. of the memory we just got */
  97. phys_addr = __pa(cpuaddr);
  98. /*
  99. * 64 bit address translations should never fail.
  100. * 32 bit translations can fail if there are insufficient mapping
  101. * resources.
  102. */
  103. *dma_handle = provider->dma_map_consistent(pdev, phys_addr, size,
  104. SN_DMA_ADDR_PHYS);
  105. if (!*dma_handle) {
  106. printk(KERN_ERR "%s: out of ATEs\n", __func__);
  107. free_pages((unsigned long)cpuaddr, get_order(size));
  108. return NULL;
  109. }
  110. return cpuaddr;
  111. }
  112. /**
  113. * sn_pci_free_coherent - free memory associated with coherent DMAable region
  114. * @dev: device to free for
  115. * @size: size to free
  116. * @cpu_addr: kernel virtual address to free
  117. * @dma_handle: DMA address associated with this region
  118. *
  119. * Frees the memory allocated by dma_alloc_coherent(), potentially unmapping
  120. * any associated IOMMU mappings.
  121. */
  122. static void sn_dma_free_coherent(struct device *dev, size_t size, void *cpu_addr,
  123. dma_addr_t dma_handle, struct dma_attrs *attrs)
  124. {
  125. struct pci_dev *pdev = to_pci_dev(dev);
  126. struct sn_pcibus_provider *provider = SN_PCIDEV_BUSPROVIDER(pdev);
  127. BUG_ON(dev->bus != &pci_bus_type);
  128. provider->dma_unmap(pdev, dma_handle, 0);
  129. free_pages((unsigned long)cpu_addr, get_order(size));
  130. }
  131. /**
  132. * sn_dma_map_single_attrs - map a single page for DMA
  133. * @dev: device to map for
  134. * @cpu_addr: kernel virtual address of the region to map
  135. * @size: size of the region
  136. * @direction: DMA direction
  137. * @attrs: optional dma attributes
  138. *
  139. * Map the region pointed to by @cpu_addr for DMA and return the
  140. * DMA address.
  141. *
  142. * We map this to the one step pcibr_dmamap_trans interface rather than
  143. * the two step pcibr_dmamap_alloc/pcibr_dmamap_addr because we have
  144. * no way of saving the dmamap handle from the alloc to later free
  145. * (which is pretty much unacceptable).
  146. *
  147. * mappings with the DMA_ATTR_WRITE_BARRIER get mapped with
  148. * dma_map_consistent() so that writes force a flush of pending DMA.
  149. * (See "SGI Altix Architecture Considerations for Linux Device Drivers",
  150. * Document Number: 007-4763-001)
  151. *
  152. * TODO: simplify our interface;
  153. * figure out how to save dmamap handle so can use two step.
  154. */
  155. static dma_addr_t sn_dma_map_page(struct device *dev, struct page *page,
  156. unsigned long offset, size_t size,
  157. enum dma_data_direction dir,
  158. struct dma_attrs *attrs)
  159. {
  160. void *cpu_addr = page_address(page) + offset;
  161. dma_addr_t dma_addr;
  162. unsigned long phys_addr;
  163. struct pci_dev *pdev = to_pci_dev(dev);
  164. struct sn_pcibus_provider *provider = SN_PCIDEV_BUSPROVIDER(pdev);
  165. int dmabarr;
  166. dmabarr = dma_get_attr(DMA_ATTR_WRITE_BARRIER, attrs);
  167. BUG_ON(dev->bus != &pci_bus_type);
  168. phys_addr = __pa(cpu_addr);
  169. if (dmabarr)
  170. dma_addr = provider->dma_map_consistent(pdev, phys_addr,
  171. size, SN_DMA_ADDR_PHYS);
  172. else
  173. dma_addr = provider->dma_map(pdev, phys_addr, size,
  174. SN_DMA_ADDR_PHYS);
  175. if (!dma_addr) {
  176. printk(KERN_ERR "%s: out of ATEs\n", __func__);
  177. return 0;
  178. }
  179. return dma_addr;
  180. }
  181. /**
  182. * sn_dma_unmap_single_attrs - unamp a DMA mapped page
  183. * @dev: device to sync
  184. * @dma_addr: DMA address to sync
  185. * @size: size of region
  186. * @direction: DMA direction
  187. * @attrs: optional dma attributes
  188. *
  189. * This routine is supposed to sync the DMA region specified
  190. * by @dma_handle into the coherence domain. On SN, we're always cache
  191. * coherent, so we just need to free any ATEs associated with this mapping.
  192. */
  193. static void sn_dma_unmap_page(struct device *dev, dma_addr_t dma_addr,
  194. size_t size, enum dma_data_direction dir,
  195. struct dma_attrs *attrs)
  196. {
  197. struct pci_dev *pdev = to_pci_dev(dev);
  198. struct sn_pcibus_provider *provider = SN_PCIDEV_BUSPROVIDER(pdev);
  199. BUG_ON(dev->bus != &pci_bus_type);
  200. provider->dma_unmap(pdev, dma_addr, dir);
  201. }
  202. /**
  203. * sn_dma_unmap_sg - unmap a DMA scatterlist
  204. * @dev: device to unmap
  205. * @sg: scatterlist to unmap
  206. * @nhwentries: number of scatterlist entries
  207. * @direction: DMA direction
  208. * @attrs: optional dma attributes
  209. *
  210. * Unmap a set of streaming mode DMA translations.
  211. */
  212. static void sn_dma_unmap_sg(struct device *dev, struct scatterlist *sgl,
  213. int nhwentries, enum dma_data_direction dir,
  214. struct dma_attrs *attrs)
  215. {
  216. int i;
  217. struct pci_dev *pdev = to_pci_dev(dev);
  218. struct sn_pcibus_provider *provider = SN_PCIDEV_BUSPROVIDER(pdev);
  219. struct scatterlist *sg;
  220. BUG_ON(dev->bus != &pci_bus_type);
  221. for_each_sg(sgl, sg, nhwentries, i) {
  222. provider->dma_unmap(pdev, sg->dma_address, dir);
  223. sg->dma_address = (dma_addr_t) NULL;
  224. sg->dma_length = 0;
  225. }
  226. }
  227. /**
  228. * sn_dma_map_sg - map a scatterlist for DMA
  229. * @dev: device to map for
  230. * @sg: scatterlist to map
  231. * @nhwentries: number of entries
  232. * @direction: direction of the DMA transaction
  233. * @attrs: optional dma attributes
  234. *
  235. * mappings with the DMA_ATTR_WRITE_BARRIER get mapped with
  236. * dma_map_consistent() so that writes force a flush of pending DMA.
  237. * (See "SGI Altix Architecture Considerations for Linux Device Drivers",
  238. * Document Number: 007-4763-001)
  239. *
  240. * Maps each entry of @sg for DMA.
  241. */
  242. static int sn_dma_map_sg(struct device *dev, struct scatterlist *sgl,
  243. int nhwentries, enum dma_data_direction dir,
  244. struct dma_attrs *attrs)
  245. {
  246. unsigned long phys_addr;
  247. struct scatterlist *saved_sg = sgl, *sg;
  248. struct pci_dev *pdev = to_pci_dev(dev);
  249. struct sn_pcibus_provider *provider = SN_PCIDEV_BUSPROVIDER(pdev);
  250. int i;
  251. int dmabarr;
  252. dmabarr = dma_get_attr(DMA_ATTR_WRITE_BARRIER, attrs);
  253. BUG_ON(dev->bus != &pci_bus_type);
  254. /*
  255. * Setup a DMA address for each entry in the scatterlist.
  256. */
  257. for_each_sg(sgl, sg, nhwentries, i) {
  258. dma_addr_t dma_addr;
  259. phys_addr = SG_ENT_PHYS_ADDRESS(sg);
  260. if (dmabarr)
  261. dma_addr = provider->dma_map_consistent(pdev,
  262. phys_addr,
  263. sg->length,
  264. SN_DMA_ADDR_PHYS);
  265. else
  266. dma_addr = provider->dma_map(pdev, phys_addr,
  267. sg->length,
  268. SN_DMA_ADDR_PHYS);
  269. sg->dma_address = dma_addr;
  270. if (!sg->dma_address) {
  271. printk(KERN_ERR "%s: out of ATEs\n", __func__);
  272. /*
  273. * Free any successfully allocated entries.
  274. */
  275. if (i > 0)
  276. sn_dma_unmap_sg(dev, saved_sg, i, dir, attrs);
  277. return 0;
  278. }
  279. sg->dma_length = sg->length;
  280. }
  281. return nhwentries;
  282. }
  283. static void sn_dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle,
  284. size_t size, enum dma_data_direction dir)
  285. {
  286. BUG_ON(dev->bus != &pci_bus_type);
  287. }
  288. static void sn_dma_sync_single_for_device(struct device *dev, dma_addr_t dma_handle,
  289. size_t size,
  290. enum dma_data_direction dir)
  291. {
  292. BUG_ON(dev->bus != &pci_bus_type);
  293. }
  294. static void sn_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
  295. int nelems, enum dma_data_direction dir)
  296. {
  297. BUG_ON(dev->bus != &pci_bus_type);
  298. }
  299. static void sn_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
  300. int nelems, enum dma_data_direction dir)
  301. {
  302. BUG_ON(dev->bus != &pci_bus_type);
  303. }
  304. static int sn_dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
  305. {
  306. return 0;
  307. }
  308. u64 sn_dma_get_required_mask(struct device *dev)
  309. {
  310. return DMA_BIT_MASK(64);
  311. }
  312. EXPORT_SYMBOL_GPL(sn_dma_get_required_mask);
  313. char *sn_pci_get_legacy_mem(struct pci_bus *bus)
  314. {
  315. if (!SN_PCIBUS_BUSSOFT(bus))
  316. return ERR_PTR(-ENODEV);
  317. return (char *)(SN_PCIBUS_BUSSOFT(bus)->bs_legacy_mem | __IA64_UNCACHED_OFFSET);
  318. }
  319. int sn_pci_legacy_read(struct pci_bus *bus, u16 port, u32 *val, u8 size)
  320. {
  321. unsigned long addr;
  322. int ret;
  323. struct ia64_sal_retval isrv;
  324. /*
  325. * First, try the SN_SAL_IOIF_PCI_SAFE SAL call which can work
  326. * around hw issues at the pci bus level. SGI proms older than
  327. * 4.10 don't implement this.
  328. */
  329. SAL_CALL(isrv, SN_SAL_IOIF_PCI_SAFE,
  330. pci_domain_nr(bus), bus->number,
  331. 0, /* io */
  332. 0, /* read */
  333. port, size, __pa(val));
  334. if (isrv.status == 0)
  335. return size;
  336. /*
  337. * If the above failed, retry using the SAL_PROBE call which should
  338. * be present in all proms (but which cannot work round PCI chipset
  339. * bugs). This code is retained for compatibility with old
  340. * pre-4.10 proms, and should be removed at some point in the future.
  341. */
  342. if (!SN_PCIBUS_BUSSOFT(bus))
  343. return -ENODEV;
  344. addr = SN_PCIBUS_BUSSOFT(bus)->bs_legacy_io | __IA64_UNCACHED_OFFSET;
  345. addr += port;
  346. ret = ia64_sn_probe_mem(addr, (long)size, (void *)val);
  347. if (ret == 2)
  348. return -EINVAL;
  349. if (ret == 1)
  350. *val = -1;
  351. return size;
  352. }
  353. int sn_pci_legacy_write(struct pci_bus *bus, u16 port, u32 val, u8 size)
  354. {
  355. int ret = size;
  356. unsigned long paddr;
  357. unsigned long *addr;
  358. struct ia64_sal_retval isrv;
  359. /*
  360. * First, try the SN_SAL_IOIF_PCI_SAFE SAL call which can work
  361. * around hw issues at the pci bus level. SGI proms older than
  362. * 4.10 don't implement this.
  363. */
  364. SAL_CALL(isrv, SN_SAL_IOIF_PCI_SAFE,
  365. pci_domain_nr(bus), bus->number,
  366. 0, /* io */
  367. 1, /* write */
  368. port, size, __pa(&val));
  369. if (isrv.status == 0)
  370. return size;
  371. /*
  372. * If the above failed, retry using the SAL_PROBE call which should
  373. * be present in all proms (but which cannot work round PCI chipset
  374. * bugs). This code is retained for compatibility with old
  375. * pre-4.10 proms, and should be removed at some point in the future.
  376. */
  377. if (!SN_PCIBUS_BUSSOFT(bus)) {
  378. ret = -ENODEV;
  379. goto out;
  380. }
  381. /* Put the phys addr in uncached space */
  382. paddr = SN_PCIBUS_BUSSOFT(bus)->bs_legacy_io | __IA64_UNCACHED_OFFSET;
  383. paddr += port;
  384. addr = (unsigned long *)paddr;
  385. switch (size) {
  386. case 1:
  387. *(volatile u8 *)(addr) = (u8)(val);
  388. break;
  389. case 2:
  390. *(volatile u16 *)(addr) = (u16)(val);
  391. break;
  392. case 4:
  393. *(volatile u32 *)(addr) = (u32)(val);
  394. break;
  395. default:
  396. ret = -EINVAL;
  397. break;
  398. }
  399. out:
  400. return ret;
  401. }
  402. static struct dma_map_ops sn_dma_ops = {
  403. .alloc = sn_dma_alloc_coherent,
  404. .free = sn_dma_free_coherent,
  405. .map_page = sn_dma_map_page,
  406. .unmap_page = sn_dma_unmap_page,
  407. .map_sg = sn_dma_map_sg,
  408. .unmap_sg = sn_dma_unmap_sg,
  409. .sync_single_for_cpu = sn_dma_sync_single_for_cpu,
  410. .sync_sg_for_cpu = sn_dma_sync_sg_for_cpu,
  411. .sync_single_for_device = sn_dma_sync_single_for_device,
  412. .sync_sg_for_device = sn_dma_sync_sg_for_device,
  413. .mapping_error = sn_dma_mapping_error,
  414. .dma_supported = sn_dma_supported,
  415. };
  416. void sn_dma_init(void)
  417. {
  418. dma_ops = &sn_dma_ops;
  419. }