pci_dma.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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/module.h>
  12. #include <linux/dma-attrs.h>
  13. #include <asm/dma.h>
  14. #include <asm/sn/intr.h>
  15. #include <asm/sn/pcibus_provider_defs.h>
  16. #include <asm/sn/pcidev.h>
  17. #include <asm/sn/sn_sal.h>
  18. #define SG_ENT_VIRT_ADDRESS(sg) (sg_virt((sg)))
  19. #define SG_ENT_PHYS_ADDRESS(SG) virt_to_phys(SG_ENT_VIRT_ADDRESS(SG))
  20. /**
  21. * sn_dma_supported - test a DMA mask
  22. * @dev: device to test
  23. * @mask: DMA mask to test
  24. *
  25. * Return whether the given PCI device DMA address mask can be supported
  26. * properly. For example, if your device can only drive the low 24-bits
  27. * during PCI bus mastering, then you would pass 0x00ffffff as the mask to
  28. * this function. Of course, SN only supports devices that have 32 or more
  29. * address bits when using the PMU.
  30. */
  31. int sn_dma_supported(struct device *dev, u64 mask)
  32. {
  33. BUG_ON(dev->bus != &pci_bus_type);
  34. if (mask < 0x7fffffff)
  35. return 0;
  36. return 1;
  37. }
  38. EXPORT_SYMBOL(sn_dma_supported);
  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. void *sn_dma_alloc_coherent(struct device *dev, size_t size,
  71. dma_addr_t * dma_handle, gfp_t flags)
  72. {
  73. void *cpuaddr;
  74. unsigned long phys_addr;
  75. int node;
  76. struct pci_dev *pdev = to_pci_dev(dev);
  77. struct sn_pcibus_provider *provider = SN_PCIDEV_BUSPROVIDER(pdev);
  78. BUG_ON(dev->bus != &pci_bus_type);
  79. /*
  80. * Allocate the memory.
  81. */
  82. node = pcibus_to_node(pdev->bus);
  83. if (likely(node >=0)) {
  84. struct page *p = alloc_pages_node(node, flags, get_order(size));
  85. if (likely(p))
  86. cpuaddr = page_address(p);
  87. else
  88. return NULL;
  89. } else
  90. cpuaddr = (void *)__get_free_pages(flags, get_order(size));
  91. if (unlikely(!cpuaddr))
  92. return NULL;
  93. memset(cpuaddr, 0x0, size);
  94. /* physical addr. of the memory we just got */
  95. phys_addr = __pa(cpuaddr);
  96. /*
  97. * 64 bit address translations should never fail.
  98. * 32 bit translations can fail if there are insufficient mapping
  99. * resources.
  100. */
  101. *dma_handle = provider->dma_map_consistent(pdev, phys_addr, size,
  102. SN_DMA_ADDR_PHYS);
  103. if (!*dma_handle) {
  104. printk(KERN_ERR "%s: out of ATEs\n", __func__);
  105. free_pages((unsigned long)cpuaddr, get_order(size));
  106. return NULL;
  107. }
  108. return cpuaddr;
  109. }
  110. EXPORT_SYMBOL(sn_dma_alloc_coherent);
  111. /**
  112. * sn_pci_free_coherent - free memory associated with coherent DMAable region
  113. * @dev: device to free for
  114. * @size: size to free
  115. * @cpu_addr: kernel virtual address to free
  116. * @dma_handle: DMA address associated with this region
  117. *
  118. * Frees the memory allocated by dma_alloc_coherent(), potentially unmapping
  119. * any associated IOMMU mappings.
  120. */
  121. void sn_dma_free_coherent(struct device *dev, size_t size, void *cpu_addr,
  122. dma_addr_t dma_handle)
  123. {
  124. struct pci_dev *pdev = to_pci_dev(dev);
  125. struct sn_pcibus_provider *provider = SN_PCIDEV_BUSPROVIDER(pdev);
  126. BUG_ON(dev->bus != &pci_bus_type);
  127. provider->dma_unmap(pdev, dma_handle, 0);
  128. free_pages((unsigned long)cpu_addr, get_order(size));
  129. }
  130. EXPORT_SYMBOL(sn_dma_free_coherent);
  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. dma_addr_t sn_dma_map_single_attrs(struct device *dev, void *cpu_addr,
  156. size_t size, int direction,
  157. struct dma_attrs *attrs)
  158. {
  159. dma_addr_t dma_addr;
  160. unsigned long phys_addr;
  161. struct pci_dev *pdev = to_pci_dev(dev);
  162. struct sn_pcibus_provider *provider = SN_PCIDEV_BUSPROVIDER(pdev);
  163. int dmabarr;
  164. dmabarr = dma_get_attr(DMA_ATTR_WRITE_BARRIER, attrs);
  165. BUG_ON(dev->bus != &pci_bus_type);
  166. phys_addr = __pa(cpu_addr);
  167. if (dmabarr)
  168. dma_addr = provider->dma_map_consistent(pdev, phys_addr,
  169. size, SN_DMA_ADDR_PHYS);
  170. else
  171. dma_addr = provider->dma_map(pdev, phys_addr, size,
  172. SN_DMA_ADDR_PHYS);
  173. if (!dma_addr) {
  174. printk(KERN_ERR "%s: out of ATEs\n", __func__);
  175. return 0;
  176. }
  177. return dma_addr;
  178. }
  179. EXPORT_SYMBOL(sn_dma_map_single_attrs);
  180. /**
  181. * sn_dma_unmap_single_attrs - unamp a DMA mapped page
  182. * @dev: device to sync
  183. * @dma_addr: DMA address to sync
  184. * @size: size of region
  185. * @direction: DMA direction
  186. * @attrs: optional dma attributes
  187. *
  188. * This routine is supposed to sync the DMA region specified
  189. * by @dma_handle into the coherence domain. On SN, we're always cache
  190. * coherent, so we just need to free any ATEs associated with this mapping.
  191. */
  192. void sn_dma_unmap_single_attrs(struct device *dev, dma_addr_t dma_addr,
  193. size_t size, int direction,
  194. struct dma_attrs *attrs)
  195. {
  196. struct pci_dev *pdev = to_pci_dev(dev);
  197. struct sn_pcibus_provider *provider = SN_PCIDEV_BUSPROVIDER(pdev);
  198. BUG_ON(dev->bus != &pci_bus_type);
  199. provider->dma_unmap(pdev, dma_addr, direction);
  200. }
  201. EXPORT_SYMBOL(sn_dma_unmap_single_attrs);
  202. /**
  203. * sn_dma_unmap_sg_attrs - 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. void sn_dma_unmap_sg_attrs(struct device *dev, struct scatterlist *sgl,
  213. int nhwentries, int direction,
  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, direction);
  223. sg->dma_address = (dma_addr_t) NULL;
  224. sg->dma_length = 0;
  225. }
  226. }
  227. EXPORT_SYMBOL(sn_dma_unmap_sg_attrs);
  228. /**
  229. * sn_dma_map_sg_attrs - map a scatterlist for DMA
  230. * @dev: device to map for
  231. * @sg: scatterlist to map
  232. * @nhwentries: number of entries
  233. * @direction: direction of the DMA transaction
  234. * @attrs: optional dma attributes
  235. *
  236. * mappings with the DMA_ATTR_WRITE_BARRIER get mapped with
  237. * dma_map_consistent() so that writes force a flush of pending DMA.
  238. * (See "SGI Altix Architecture Considerations for Linux Device Drivers",
  239. * Document Number: 007-4763-001)
  240. *
  241. * Maps each entry of @sg for DMA.
  242. */
  243. int sn_dma_map_sg_attrs(struct device *dev, struct scatterlist *sgl,
  244. int nhwentries, int direction, 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_attrs(dev, saved_sg, i,
  277. direction, attrs);
  278. return 0;
  279. }
  280. sg->dma_length = sg->length;
  281. }
  282. return nhwentries;
  283. }
  284. EXPORT_SYMBOL(sn_dma_map_sg_attrs);
  285. void sn_dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle,
  286. size_t size, int direction)
  287. {
  288. BUG_ON(dev->bus != &pci_bus_type);
  289. }
  290. EXPORT_SYMBOL(sn_dma_sync_single_for_cpu);
  291. void sn_dma_sync_single_for_device(struct device *dev, dma_addr_t dma_handle,
  292. size_t size, int direction)
  293. {
  294. BUG_ON(dev->bus != &pci_bus_type);
  295. }
  296. EXPORT_SYMBOL(sn_dma_sync_single_for_device);
  297. void sn_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
  298. int nelems, int direction)
  299. {
  300. BUG_ON(dev->bus != &pci_bus_type);
  301. }
  302. EXPORT_SYMBOL(sn_dma_sync_sg_for_cpu);
  303. void sn_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
  304. int nelems, int direction)
  305. {
  306. BUG_ON(dev->bus != &pci_bus_type);
  307. }
  308. EXPORT_SYMBOL(sn_dma_sync_sg_for_device);
  309. int sn_dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
  310. {
  311. return 0;
  312. }
  313. EXPORT_SYMBOL(sn_dma_mapping_error);
  314. char *sn_pci_get_legacy_mem(struct pci_bus *bus)
  315. {
  316. if (!SN_PCIBUS_BUSSOFT(bus))
  317. return ERR_PTR(-ENODEV);
  318. return (char *)(SN_PCIBUS_BUSSOFT(bus)->bs_legacy_mem | __IA64_UNCACHED_OFFSET);
  319. }
  320. int sn_pci_legacy_read(struct pci_bus *bus, u16 port, u32 *val, u8 size)
  321. {
  322. unsigned long addr;
  323. int ret;
  324. struct ia64_sal_retval isrv;
  325. /*
  326. * First, try the SN_SAL_IOIF_PCI_SAFE SAL call which can work
  327. * around hw issues at the pci bus level. SGI proms older than
  328. * 4.10 don't implement this.
  329. */
  330. SAL_CALL(isrv, SN_SAL_IOIF_PCI_SAFE,
  331. pci_domain_nr(bus), bus->number,
  332. 0, /* io */
  333. 0, /* read */
  334. port, size, __pa(val));
  335. if (isrv.status == 0)
  336. return size;
  337. /*
  338. * If the above failed, retry using the SAL_PROBE call which should
  339. * be present in all proms (but which cannot work round PCI chipset
  340. * bugs). This code is retained for compatibility with old
  341. * pre-4.10 proms, and should be removed at some point in the future.
  342. */
  343. if (!SN_PCIBUS_BUSSOFT(bus))
  344. return -ENODEV;
  345. addr = SN_PCIBUS_BUSSOFT(bus)->bs_legacy_io | __IA64_UNCACHED_OFFSET;
  346. addr += port;
  347. ret = ia64_sn_probe_mem(addr, (long)size, (void *)val);
  348. if (ret == 2)
  349. return -EINVAL;
  350. if (ret == 1)
  351. *val = -1;
  352. return size;
  353. }
  354. int sn_pci_legacy_write(struct pci_bus *bus, u16 port, u32 val, u8 size)
  355. {
  356. int ret = size;
  357. unsigned long paddr;
  358. unsigned long *addr;
  359. struct ia64_sal_retval isrv;
  360. /*
  361. * First, try the SN_SAL_IOIF_PCI_SAFE SAL call which can work
  362. * around hw issues at the pci bus level. SGI proms older than
  363. * 4.10 don't implement this.
  364. */
  365. SAL_CALL(isrv, SN_SAL_IOIF_PCI_SAFE,
  366. pci_domain_nr(bus), bus->number,
  367. 0, /* io */
  368. 1, /* write */
  369. port, size, __pa(&val));
  370. if (isrv.status == 0)
  371. return size;
  372. /*
  373. * If the above failed, retry using the SAL_PROBE call which should
  374. * be present in all proms (but which cannot work round PCI chipset
  375. * bugs). This code is retained for compatibility with old
  376. * pre-4.10 proms, and should be removed at some point in the future.
  377. */
  378. if (!SN_PCIBUS_BUSSOFT(bus)) {
  379. ret = -ENODEV;
  380. goto out;
  381. }
  382. /* Put the phys addr in uncached space */
  383. paddr = SN_PCIBUS_BUSSOFT(bus)->bs_legacy_io | __IA64_UNCACHED_OFFSET;
  384. paddr += port;
  385. addr = (unsigned long *)paddr;
  386. switch (size) {
  387. case 1:
  388. *(volatile u8 *)(addr) = (u8)(val);
  389. break;
  390. case 2:
  391. *(volatile u16 *)(addr) = (u16)(val);
  392. break;
  393. case 4:
  394. *(volatile u32 *)(addr) = (u32)(val);
  395. break;
  396. default:
  397. ret = -EINVAL;
  398. break;
  399. }
  400. out:
  401. return ret;
  402. }