pci_dma.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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. 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. {
  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. /**
  111. * sn_pci_free_coherent - free memory associated with coherent DMAable region
  112. * @dev: device to free for
  113. * @size: size to free
  114. * @cpu_addr: kernel virtual address to free
  115. * @dma_handle: DMA address associated with this region
  116. *
  117. * Frees the memory allocated by dma_alloc_coherent(), potentially unmapping
  118. * any associated IOMMU mappings.
  119. */
  120. static void sn_dma_free_coherent(struct device *dev, size_t size, void *cpu_addr,
  121. dma_addr_t dma_handle)
  122. {
  123. struct pci_dev *pdev = to_pci_dev(dev);
  124. struct sn_pcibus_provider *provider = SN_PCIDEV_BUSPROVIDER(pdev);
  125. BUG_ON(dev->bus != &pci_bus_type);
  126. provider->dma_unmap(pdev, dma_handle, 0);
  127. free_pages((unsigned long)cpu_addr, get_order(size));
  128. }
  129. /**
  130. * sn_dma_map_single_attrs - map a single page for DMA
  131. * @dev: device to map for
  132. * @cpu_addr: kernel virtual address of the region to map
  133. * @size: size of the region
  134. * @direction: DMA direction
  135. * @attrs: optional dma attributes
  136. *
  137. * Map the region pointed to by @cpu_addr for DMA and return the
  138. * DMA address.
  139. *
  140. * We map this to the one step pcibr_dmamap_trans interface rather than
  141. * the two step pcibr_dmamap_alloc/pcibr_dmamap_addr because we have
  142. * no way of saving the dmamap handle from the alloc to later free
  143. * (which is pretty much unacceptable).
  144. *
  145. * mappings with the DMA_ATTR_WRITE_BARRIER get mapped with
  146. * dma_map_consistent() so that writes force a flush of pending DMA.
  147. * (See "SGI Altix Architecture Considerations for Linux Device Drivers",
  148. * Document Number: 007-4763-001)
  149. *
  150. * TODO: simplify our interface;
  151. * figure out how to save dmamap handle so can use two step.
  152. */
  153. static dma_addr_t sn_dma_map_single_attrs(struct device *dev, void *cpu_addr,
  154. size_t size, int direction,
  155. struct dma_attrs *attrs)
  156. {
  157. dma_addr_t dma_addr;
  158. unsigned long phys_addr;
  159. struct pci_dev *pdev = to_pci_dev(dev);
  160. struct sn_pcibus_provider *provider = SN_PCIDEV_BUSPROVIDER(pdev);
  161. int dmabarr;
  162. dmabarr = dma_get_attr(DMA_ATTR_WRITE_BARRIER, attrs);
  163. BUG_ON(dev->bus != &pci_bus_type);
  164. phys_addr = __pa(cpu_addr);
  165. if (dmabarr)
  166. dma_addr = provider->dma_map_consistent(pdev, phys_addr,
  167. size, SN_DMA_ADDR_PHYS);
  168. else
  169. dma_addr = provider->dma_map(pdev, phys_addr, size,
  170. SN_DMA_ADDR_PHYS);
  171. if (!dma_addr) {
  172. printk(KERN_ERR "%s: out of ATEs\n", __func__);
  173. return 0;
  174. }
  175. return dma_addr;
  176. }
  177. /**
  178. * sn_dma_unmap_single_attrs - unamp a DMA mapped page
  179. * @dev: device to sync
  180. * @dma_addr: DMA address to sync
  181. * @size: size of region
  182. * @direction: DMA direction
  183. * @attrs: optional dma attributes
  184. *
  185. * This routine is supposed to sync the DMA region specified
  186. * by @dma_handle into the coherence domain. On SN, we're always cache
  187. * coherent, so we just need to free any ATEs associated with this mapping.
  188. */
  189. static void sn_dma_unmap_single_attrs(struct device *dev, dma_addr_t dma_addr,
  190. size_t size, int direction,
  191. struct dma_attrs *attrs)
  192. {
  193. struct pci_dev *pdev = to_pci_dev(dev);
  194. struct sn_pcibus_provider *provider = SN_PCIDEV_BUSPROVIDER(pdev);
  195. BUG_ON(dev->bus != &pci_bus_type);
  196. provider->dma_unmap(pdev, dma_addr, direction);
  197. }
  198. /**
  199. * sn_dma_unmap_sg_attrs - unmap a DMA scatterlist
  200. * @dev: device to unmap
  201. * @sg: scatterlist to unmap
  202. * @nhwentries: number of scatterlist entries
  203. * @direction: DMA direction
  204. * @attrs: optional dma attributes
  205. *
  206. * Unmap a set of streaming mode DMA translations.
  207. */
  208. static void sn_dma_unmap_sg_attrs(struct device *dev, struct scatterlist *sgl,
  209. int nhwentries, int direction,
  210. struct dma_attrs *attrs)
  211. {
  212. int i;
  213. struct pci_dev *pdev = to_pci_dev(dev);
  214. struct sn_pcibus_provider *provider = SN_PCIDEV_BUSPROVIDER(pdev);
  215. struct scatterlist *sg;
  216. BUG_ON(dev->bus != &pci_bus_type);
  217. for_each_sg(sgl, sg, nhwentries, i) {
  218. provider->dma_unmap(pdev, sg->dma_address, direction);
  219. sg->dma_address = (dma_addr_t) NULL;
  220. sg->dma_length = 0;
  221. }
  222. }
  223. /**
  224. * sn_dma_map_sg_attrs - map a scatterlist for DMA
  225. * @dev: device to map for
  226. * @sg: scatterlist to map
  227. * @nhwentries: number of entries
  228. * @direction: direction of the DMA transaction
  229. * @attrs: optional dma attributes
  230. *
  231. * mappings with the DMA_ATTR_WRITE_BARRIER get mapped with
  232. * dma_map_consistent() so that writes force a flush of pending DMA.
  233. * (See "SGI Altix Architecture Considerations for Linux Device Drivers",
  234. * Document Number: 007-4763-001)
  235. *
  236. * Maps each entry of @sg for DMA.
  237. */
  238. static int sn_dma_map_sg_attrs(struct device *dev, struct scatterlist *sgl,
  239. int nhwentries, int direction, struct dma_attrs *attrs)
  240. {
  241. unsigned long phys_addr;
  242. struct scatterlist *saved_sg = sgl, *sg;
  243. struct pci_dev *pdev = to_pci_dev(dev);
  244. struct sn_pcibus_provider *provider = SN_PCIDEV_BUSPROVIDER(pdev);
  245. int i;
  246. int dmabarr;
  247. dmabarr = dma_get_attr(DMA_ATTR_WRITE_BARRIER, attrs);
  248. BUG_ON(dev->bus != &pci_bus_type);
  249. /*
  250. * Setup a DMA address for each entry in the scatterlist.
  251. */
  252. for_each_sg(sgl, sg, nhwentries, i) {
  253. dma_addr_t dma_addr;
  254. phys_addr = SG_ENT_PHYS_ADDRESS(sg);
  255. if (dmabarr)
  256. dma_addr = provider->dma_map_consistent(pdev,
  257. phys_addr,
  258. sg->length,
  259. SN_DMA_ADDR_PHYS);
  260. else
  261. dma_addr = provider->dma_map(pdev, phys_addr,
  262. sg->length,
  263. SN_DMA_ADDR_PHYS);
  264. sg->dma_address = dma_addr;
  265. if (!sg->dma_address) {
  266. printk(KERN_ERR "%s: out of ATEs\n", __func__);
  267. /*
  268. * Free any successfully allocated entries.
  269. */
  270. if (i > 0)
  271. sn_dma_unmap_sg_attrs(dev, saved_sg, i,
  272. direction, attrs);
  273. return 0;
  274. }
  275. sg->dma_length = sg->length;
  276. }
  277. return nhwentries;
  278. }
  279. static void sn_dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle,
  280. size_t size, int direction)
  281. {
  282. BUG_ON(dev->bus != &pci_bus_type);
  283. }
  284. static void sn_dma_sync_single_for_device(struct device *dev, dma_addr_t dma_handle,
  285. size_t size, int direction)
  286. {
  287. BUG_ON(dev->bus != &pci_bus_type);
  288. }
  289. static void sn_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
  290. int nelems, int direction)
  291. {
  292. BUG_ON(dev->bus != &pci_bus_type);
  293. }
  294. static void sn_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
  295. int nelems, int direction)
  296. {
  297. BUG_ON(dev->bus != &pci_bus_type);
  298. }
  299. static int sn_dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
  300. {
  301. return 0;
  302. }
  303. char *sn_pci_get_legacy_mem(struct pci_bus *bus)
  304. {
  305. if (!SN_PCIBUS_BUSSOFT(bus))
  306. return ERR_PTR(-ENODEV);
  307. return (char *)(SN_PCIBUS_BUSSOFT(bus)->bs_legacy_mem | __IA64_UNCACHED_OFFSET);
  308. }
  309. int sn_pci_legacy_read(struct pci_bus *bus, u16 port, u32 *val, u8 size)
  310. {
  311. unsigned long addr;
  312. int ret;
  313. struct ia64_sal_retval isrv;
  314. /*
  315. * First, try the SN_SAL_IOIF_PCI_SAFE SAL call which can work
  316. * around hw issues at the pci bus level. SGI proms older than
  317. * 4.10 don't implement this.
  318. */
  319. SAL_CALL(isrv, SN_SAL_IOIF_PCI_SAFE,
  320. pci_domain_nr(bus), bus->number,
  321. 0, /* io */
  322. 0, /* read */
  323. port, size, __pa(val));
  324. if (isrv.status == 0)
  325. return size;
  326. /*
  327. * If the above failed, retry using the SAL_PROBE call which should
  328. * be present in all proms (but which cannot work round PCI chipset
  329. * bugs). This code is retained for compatibility with old
  330. * pre-4.10 proms, and should be removed at some point in the future.
  331. */
  332. if (!SN_PCIBUS_BUSSOFT(bus))
  333. return -ENODEV;
  334. addr = SN_PCIBUS_BUSSOFT(bus)->bs_legacy_io | __IA64_UNCACHED_OFFSET;
  335. addr += port;
  336. ret = ia64_sn_probe_mem(addr, (long)size, (void *)val);
  337. if (ret == 2)
  338. return -EINVAL;
  339. if (ret == 1)
  340. *val = -1;
  341. return size;
  342. }
  343. int sn_pci_legacy_write(struct pci_bus *bus, u16 port, u32 val, u8 size)
  344. {
  345. int ret = size;
  346. unsigned long paddr;
  347. unsigned long *addr;
  348. struct ia64_sal_retval isrv;
  349. /*
  350. * First, try the SN_SAL_IOIF_PCI_SAFE SAL call which can work
  351. * around hw issues at the pci bus level. SGI proms older than
  352. * 4.10 don't implement this.
  353. */
  354. SAL_CALL(isrv, SN_SAL_IOIF_PCI_SAFE,
  355. pci_domain_nr(bus), bus->number,
  356. 0, /* io */
  357. 1, /* write */
  358. port, size, __pa(&val));
  359. if (isrv.status == 0)
  360. return size;
  361. /*
  362. * If the above failed, retry using the SAL_PROBE call which should
  363. * be present in all proms (but which cannot work round PCI chipset
  364. * bugs). This code is retained for compatibility with old
  365. * pre-4.10 proms, and should be removed at some point in the future.
  366. */
  367. if (!SN_PCIBUS_BUSSOFT(bus)) {
  368. ret = -ENODEV;
  369. goto out;
  370. }
  371. /* Put the phys addr in uncached space */
  372. paddr = SN_PCIBUS_BUSSOFT(bus)->bs_legacy_io | __IA64_UNCACHED_OFFSET;
  373. paddr += port;
  374. addr = (unsigned long *)paddr;
  375. switch (size) {
  376. case 1:
  377. *(volatile u8 *)(addr) = (u8)(val);
  378. break;
  379. case 2:
  380. *(volatile u16 *)(addr) = (u16)(val);
  381. break;
  382. case 4:
  383. *(volatile u32 *)(addr) = (u32)(val);
  384. break;
  385. default:
  386. ret = -EINVAL;
  387. break;
  388. }
  389. out:
  390. return ret;
  391. }
  392. static struct dma_mapping_ops sn_dma_ops = {
  393. .alloc_coherent = sn_dma_alloc_coherent,
  394. .free_coherent = sn_dma_free_coherent,
  395. .map_single_attrs = sn_dma_map_single_attrs,
  396. .unmap_single_attrs = sn_dma_unmap_single_attrs,
  397. .map_sg_attrs = sn_dma_map_sg_attrs,
  398. .unmap_sg_attrs = sn_dma_unmap_sg_attrs,
  399. .sync_single_for_cpu = sn_dma_sync_single_for_cpu,
  400. .sync_sg_for_cpu = sn_dma_sync_sg_for_cpu,
  401. .sync_single_for_device = sn_dma_sync_single_for_device,
  402. .sync_sg_for_device = sn_dma_sync_sg_for_device,
  403. .mapping_error = sn_dma_mapping_error,
  404. .dma_supported_op = sn_dma_supported,
  405. };
  406. void sn_dma_init(void)
  407. {
  408. dma_ops = &sn_dma_ops;
  409. }