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/module.h>
  12. #include <linux/dma-attrs.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. 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. EXPORT_SYMBOL(sn_dma_supported);
  40. /**
  41. * sn_dma_set_mask - set the DMA mask
  42. * @dev: device to set
  43. * @dma_mask: new mask
  44. *
  45. * Set @dev's DMA mask if the hw supports it.
  46. */
  47. int sn_dma_set_mask(struct device *dev, u64 dma_mask)
  48. {
  49. BUG_ON(dev->bus != &pci_bus_type);
  50. if (!sn_dma_supported(dev, dma_mask))
  51. return 0;
  52. *dev->dma_mask = dma_mask;
  53. return 1;
  54. }
  55. EXPORT_SYMBOL(sn_dma_set_mask);
  56. /**
  57. * sn_dma_alloc_coherent - allocate memory for coherent DMA
  58. * @dev: device to allocate for
  59. * @size: size of the region
  60. * @dma_handle: DMA (bus) address
  61. * @flags: memory allocation flags
  62. *
  63. * dma_alloc_coherent() returns a pointer to a memory region suitable for
  64. * coherent DMA traffic to/from a PCI device. On SN platforms, this means
  65. * that @dma_handle will have the %PCIIO_DMA_CMD flag set.
  66. *
  67. * This interface is usually used for "command" streams (e.g. the command
  68. * queue for a SCSI controller). See Documentation/DMA-API.txt for
  69. * more information.
  70. */
  71. void *sn_dma_alloc_coherent(struct device *dev, size_t size,
  72. dma_addr_t * dma_handle, gfp_t flags)
  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_node(node, flags, get_order(size));
  86. if (likely(p))
  87. cpuaddr = page_address(p);
  88. else
  89. return NULL;
  90. } else
  91. cpuaddr = (void *)__get_free_pages(flags, get_order(size));
  92. if (unlikely(!cpuaddr))
  93. return NULL;
  94. memset(cpuaddr, 0x0, size);
  95. /* physical addr. of the memory we just got */
  96. phys_addr = __pa(cpuaddr);
  97. /*
  98. * 64 bit address translations should never fail.
  99. * 32 bit translations can fail if there are insufficient mapping
  100. * resources.
  101. */
  102. *dma_handle = provider->dma_map_consistent(pdev, phys_addr, size,
  103. SN_DMA_ADDR_PHYS);
  104. if (!*dma_handle) {
  105. printk(KERN_ERR "%s: out of ATEs\n", __func__);
  106. free_pages((unsigned long)cpuaddr, get_order(size));
  107. return NULL;
  108. }
  109. return cpuaddr;
  110. }
  111. EXPORT_SYMBOL(sn_dma_alloc_coherent);
  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. void sn_dma_free_coherent(struct device *dev, size_t size, void *cpu_addr,
  123. dma_addr_t dma_handle)
  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. EXPORT_SYMBOL(sn_dma_free_coherent);
  132. /**
  133. * sn_dma_map_single_attrs - map a single page for DMA
  134. * @dev: device to map for
  135. * @cpu_addr: kernel virtual address of the region to map
  136. * @size: size of the region
  137. * @direction: DMA direction
  138. * @attrs: optional dma attributes
  139. *
  140. * Map the region pointed to by @cpu_addr for DMA and return the
  141. * DMA address.
  142. *
  143. * We map this to the one step pcibr_dmamap_trans interface rather than
  144. * the two step pcibr_dmamap_alloc/pcibr_dmamap_addr because we have
  145. * no way of saving the dmamap handle from the alloc to later free
  146. * (which is pretty much unacceptable).
  147. *
  148. * mappings with the DMA_ATTR_WRITE_BARRIER get mapped with
  149. * dma_map_consistent() so that writes force a flush of pending DMA.
  150. * (See "SGI Altix Architecture Considerations for Linux Device Drivers",
  151. * Document Number: 007-4763-001)
  152. *
  153. * TODO: simplify our interface;
  154. * figure out how to save dmamap handle so can use two step.
  155. */
  156. dma_addr_t sn_dma_map_single_attrs(struct device *dev, void *cpu_addr,
  157. size_t size, int direction,
  158. struct dma_attrs *attrs)
  159. {
  160. dma_addr_t dma_addr;
  161. unsigned long phys_addr;
  162. struct pci_dev *pdev = to_pci_dev(dev);
  163. struct sn_pcibus_provider *provider = SN_PCIDEV_BUSPROVIDER(pdev);
  164. int dmabarr;
  165. dmabarr = dma_get_attr(DMA_ATTR_WRITE_BARRIER, attrs);
  166. BUG_ON(dev->bus != &pci_bus_type);
  167. phys_addr = __pa(cpu_addr);
  168. if (dmabarr)
  169. dma_addr = provider->dma_map_consistent(pdev, phys_addr,
  170. size, SN_DMA_ADDR_PHYS);
  171. else
  172. dma_addr = provider->dma_map(pdev, phys_addr, size,
  173. SN_DMA_ADDR_PHYS);
  174. if (!dma_addr) {
  175. printk(KERN_ERR "%s: out of ATEs\n", __func__);
  176. return 0;
  177. }
  178. return dma_addr;
  179. }
  180. EXPORT_SYMBOL(sn_dma_map_single_attrs);
  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. void sn_dma_unmap_single_attrs(struct device *dev, dma_addr_t dma_addr,
  194. size_t size, int direction,
  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, direction);
  201. }
  202. EXPORT_SYMBOL(sn_dma_unmap_single_attrs);
  203. /**
  204. * sn_dma_unmap_sg_attrs - unmap a DMA scatterlist
  205. * @dev: device to unmap
  206. * @sg: scatterlist to unmap
  207. * @nhwentries: number of scatterlist entries
  208. * @direction: DMA direction
  209. * @attrs: optional dma attributes
  210. *
  211. * Unmap a set of streaming mode DMA translations.
  212. */
  213. void sn_dma_unmap_sg_attrs(struct device *dev, struct scatterlist *sgl,
  214. int nhwentries, int direction,
  215. struct dma_attrs *attrs)
  216. {
  217. int i;
  218. struct pci_dev *pdev = to_pci_dev(dev);
  219. struct sn_pcibus_provider *provider = SN_PCIDEV_BUSPROVIDER(pdev);
  220. struct scatterlist *sg;
  221. BUG_ON(dev->bus != &pci_bus_type);
  222. for_each_sg(sgl, sg, nhwentries, i) {
  223. provider->dma_unmap(pdev, sg->dma_address, direction);
  224. sg->dma_address = (dma_addr_t) NULL;
  225. sg->dma_length = 0;
  226. }
  227. }
  228. EXPORT_SYMBOL(sn_dma_unmap_sg_attrs);
  229. /**
  230. * sn_dma_map_sg_attrs - map a scatterlist for DMA
  231. * @dev: device to map for
  232. * @sg: scatterlist to map
  233. * @nhwentries: number of entries
  234. * @direction: direction of the DMA transaction
  235. * @attrs: optional dma attributes
  236. *
  237. * mappings with the DMA_ATTR_WRITE_BARRIER get mapped with
  238. * dma_map_consistent() so that writes force a flush of pending DMA.
  239. * (See "SGI Altix Architecture Considerations for Linux Device Drivers",
  240. * Document Number: 007-4763-001)
  241. *
  242. * Maps each entry of @sg for DMA.
  243. */
  244. int sn_dma_map_sg_attrs(struct device *dev, struct scatterlist *sgl,
  245. int nhwentries, int direction, struct dma_attrs *attrs)
  246. {
  247. unsigned long phys_addr;
  248. struct scatterlist *saved_sg = sgl, *sg;
  249. struct pci_dev *pdev = to_pci_dev(dev);
  250. struct sn_pcibus_provider *provider = SN_PCIDEV_BUSPROVIDER(pdev);
  251. int i;
  252. int dmabarr;
  253. dmabarr = dma_get_attr(DMA_ATTR_WRITE_BARRIER, attrs);
  254. BUG_ON(dev->bus != &pci_bus_type);
  255. /*
  256. * Setup a DMA address for each entry in the scatterlist.
  257. */
  258. for_each_sg(sgl, sg, nhwentries, i) {
  259. dma_addr_t dma_addr;
  260. phys_addr = SG_ENT_PHYS_ADDRESS(sg);
  261. if (dmabarr)
  262. dma_addr = provider->dma_map_consistent(pdev,
  263. phys_addr,
  264. sg->length,
  265. SN_DMA_ADDR_PHYS);
  266. else
  267. dma_addr = provider->dma_map(pdev, phys_addr,
  268. sg->length,
  269. SN_DMA_ADDR_PHYS);
  270. sg->dma_address = dma_addr;
  271. if (!sg->dma_address) {
  272. printk(KERN_ERR "%s: out of ATEs\n", __func__);
  273. /*
  274. * Free any successfully allocated entries.
  275. */
  276. if (i > 0)
  277. sn_dma_unmap_sg_attrs(dev, saved_sg, i,
  278. direction, attrs);
  279. return 0;
  280. }
  281. sg->dma_length = sg->length;
  282. }
  283. return nhwentries;
  284. }
  285. EXPORT_SYMBOL(sn_dma_map_sg_attrs);
  286. void sn_dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle,
  287. size_t size, int direction)
  288. {
  289. BUG_ON(dev->bus != &pci_bus_type);
  290. }
  291. EXPORT_SYMBOL(sn_dma_sync_single_for_cpu);
  292. void sn_dma_sync_single_for_device(struct device *dev, dma_addr_t dma_handle,
  293. size_t size, int direction)
  294. {
  295. BUG_ON(dev->bus != &pci_bus_type);
  296. }
  297. EXPORT_SYMBOL(sn_dma_sync_single_for_device);
  298. void sn_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
  299. int nelems, int direction)
  300. {
  301. BUG_ON(dev->bus != &pci_bus_type);
  302. }
  303. EXPORT_SYMBOL(sn_dma_sync_sg_for_cpu);
  304. void sn_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
  305. int nelems, int direction)
  306. {
  307. BUG_ON(dev->bus != &pci_bus_type);
  308. }
  309. EXPORT_SYMBOL(sn_dma_sync_sg_for_device);
  310. int sn_dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
  311. {
  312. return 0;
  313. }
  314. EXPORT_SYMBOL(sn_dma_mapping_error);
  315. char *sn_pci_get_legacy_mem(struct pci_bus *bus)
  316. {
  317. if (!SN_PCIBUS_BUSSOFT(bus))
  318. return ERR_PTR(-ENODEV);
  319. return (char *)(SN_PCIBUS_BUSSOFT(bus)->bs_legacy_mem | __IA64_UNCACHED_OFFSET);
  320. }
  321. int sn_pci_legacy_read(struct pci_bus *bus, u16 port, u32 *val, u8 size)
  322. {
  323. unsigned long addr;
  324. int ret;
  325. struct ia64_sal_retval isrv;
  326. /*
  327. * First, try the SN_SAL_IOIF_PCI_SAFE SAL call which can work
  328. * around hw issues at the pci bus level. SGI proms older than
  329. * 4.10 don't implement this.
  330. */
  331. SAL_CALL(isrv, SN_SAL_IOIF_PCI_SAFE,
  332. pci_domain_nr(bus), bus->number,
  333. 0, /* io */
  334. 0, /* read */
  335. port, size, __pa(val));
  336. if (isrv.status == 0)
  337. return size;
  338. /*
  339. * If the above failed, retry using the SAL_PROBE call which should
  340. * be present in all proms (but which cannot work round PCI chipset
  341. * bugs). This code is retained for compatibility with old
  342. * pre-4.10 proms, and should be removed at some point in the future.
  343. */
  344. if (!SN_PCIBUS_BUSSOFT(bus))
  345. return -ENODEV;
  346. addr = SN_PCIBUS_BUSSOFT(bus)->bs_legacy_io | __IA64_UNCACHED_OFFSET;
  347. addr += port;
  348. ret = ia64_sn_probe_mem(addr, (long)size, (void *)val);
  349. if (ret == 2)
  350. return -EINVAL;
  351. if (ret == 1)
  352. *val = -1;
  353. return size;
  354. }
  355. int sn_pci_legacy_write(struct pci_bus *bus, u16 port, u32 val, u8 size)
  356. {
  357. int ret = size;
  358. unsigned long paddr;
  359. unsigned long *addr;
  360. struct ia64_sal_retval isrv;
  361. /*
  362. * First, try the SN_SAL_IOIF_PCI_SAFE SAL call which can work
  363. * around hw issues at the pci bus level. SGI proms older than
  364. * 4.10 don't implement this.
  365. */
  366. SAL_CALL(isrv, SN_SAL_IOIF_PCI_SAFE,
  367. pci_domain_nr(bus), bus->number,
  368. 0, /* io */
  369. 1, /* write */
  370. port, size, __pa(&val));
  371. if (isrv.status == 0)
  372. return size;
  373. /*
  374. * If the above failed, retry using the SAL_PROBE call which should
  375. * be present in all proms (but which cannot work round PCI chipset
  376. * bugs). This code is retained for compatibility with old
  377. * pre-4.10 proms, and should be removed at some point in the future.
  378. */
  379. if (!SN_PCIBUS_BUSSOFT(bus)) {
  380. ret = -ENODEV;
  381. goto out;
  382. }
  383. /* Put the phys addr in uncached space */
  384. paddr = SN_PCIBUS_BUSSOFT(bus)->bs_legacy_io | __IA64_UNCACHED_OFFSET;
  385. paddr += port;
  386. addr = (unsigned long *)paddr;
  387. switch (size) {
  388. case 1:
  389. *(volatile u8 *)(addr) = (u8)(val);
  390. break;
  391. case 2:
  392. *(volatile u16 *)(addr) = (u16)(val);
  393. break;
  394. case 4:
  395. *(volatile u32 *)(addr) = (u32)(val);
  396. break;
  397. default:
  398. ret = -EINVAL;
  399. break;
  400. }
  401. out:
  402. return ret;
  403. }
  404. struct dma_mapping_ops sn_dma_ops = {
  405. .alloc_coherent = sn_dma_alloc_coherent,
  406. .free_coherent = sn_dma_free_coherent,
  407. .map_single_attrs = sn_dma_map_single_attrs,
  408. .unmap_single_attrs = sn_dma_unmap_single_attrs,
  409. .map_sg_attrs = sn_dma_map_sg_attrs,
  410. .unmap_sg_attrs = sn_dma_unmap_sg_attrs,
  411. .sync_single_for_cpu = sn_dma_sync_single_for_cpu,
  412. .sync_sg_for_cpu = sn_dma_sync_sg_for_cpu,
  413. .sync_single_for_device = sn_dma_sync_single_for_device,
  414. .sync_sg_for_device = sn_dma_sync_sg_for_device,
  415. .mapping_error = sn_dma_mapping_error,
  416. .dma_supported_op = sn_dma_supported,
  417. };
  418. void sn_dma_init(void)
  419. {
  420. dma_ops = &sn_dma_ops;
  421. }