pci_dma.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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 <asm/dma.h>
  13. #include <asm/sn/intr.h>
  14. #include <asm/sn/pcibus_provider_defs.h>
  15. #include <asm/sn/pcidev.h>
  16. #include <asm/sn/sn_sal.h>
  17. #define SG_ENT_VIRT_ADDRESS(sg) (page_address((sg)->page) + (sg)->offset)
  18. #define SG_ENT_PHYS_ADDRESS(SG) virt_to_phys(SG_ENT_VIRT_ADDRESS(SG))
  19. /**
  20. * sn_dma_supported - test a DMA mask
  21. * @dev: device to test
  22. * @mask: DMA mask to test
  23. *
  24. * Return whether the given PCI device DMA address mask can be supported
  25. * properly. For example, if your device can only drive the low 24-bits
  26. * during PCI bus mastering, then you would pass 0x00ffffff as the mask to
  27. * this function. Of course, SN only supports devices that have 32 or more
  28. * address bits when using the PMU.
  29. */
  30. int sn_dma_supported(struct device *dev, u64 mask)
  31. {
  32. BUG_ON(dev->bus != &pci_bus_type);
  33. if (mask < 0x7fffffff)
  34. return 0;
  35. return 1;
  36. }
  37. EXPORT_SYMBOL(sn_dma_supported);
  38. /**
  39. * sn_dma_set_mask - set the DMA mask
  40. * @dev: device to set
  41. * @dma_mask: new mask
  42. *
  43. * Set @dev's DMA mask if the hw supports it.
  44. */
  45. int sn_dma_set_mask(struct device *dev, u64 dma_mask)
  46. {
  47. BUG_ON(dev->bus != &pci_bus_type);
  48. if (!sn_dma_supported(dev, dma_mask))
  49. return 0;
  50. *dev->dma_mask = dma_mask;
  51. return 1;
  52. }
  53. EXPORT_SYMBOL(sn_dma_set_mask);
  54. /**
  55. * sn_dma_alloc_coherent - allocate memory for coherent DMA
  56. * @dev: device to allocate for
  57. * @size: size of the region
  58. * @dma_handle: DMA (bus) address
  59. * @flags: memory allocation flags
  60. *
  61. * dma_alloc_coherent() returns a pointer to a memory region suitable for
  62. * coherent DMA traffic to/from a PCI device. On SN platforms, this means
  63. * that @dma_handle will have the %PCIIO_DMA_CMD flag set.
  64. *
  65. * This interface is usually used for "command" streams (e.g. the command
  66. * queue for a SCSI controller). See Documentation/DMA-API.txt for
  67. * more information.
  68. */
  69. void *sn_dma_alloc_coherent(struct device *dev, size_t size,
  70. dma_addr_t * dma_handle, gfp_t flags)
  71. {
  72. void *cpuaddr;
  73. unsigned long phys_addr;
  74. int node;
  75. struct pci_dev *pdev = to_pci_dev(dev);
  76. struct sn_pcibus_provider *provider = SN_PCIDEV_BUSPROVIDER(pdev);
  77. BUG_ON(dev->bus != &pci_bus_type);
  78. /*
  79. * Allocate the memory.
  80. */
  81. node = pcibus_to_node(pdev->bus);
  82. if (likely(node >=0)) {
  83. struct page *p = alloc_pages_node(node, flags, get_order(size));
  84. if (likely(p))
  85. cpuaddr = page_address(p);
  86. else
  87. return NULL;
  88. } else
  89. cpuaddr = (void *)__get_free_pages(flags, get_order(size));
  90. if (unlikely(!cpuaddr))
  91. return NULL;
  92. memset(cpuaddr, 0x0, size);
  93. /* physical addr. of the memory we just got */
  94. phys_addr = __pa(cpuaddr);
  95. /*
  96. * 64 bit address translations should never fail.
  97. * 32 bit translations can fail if there are insufficient mapping
  98. * resources.
  99. */
  100. *dma_handle = provider->dma_map_consistent(pdev, phys_addr, size,
  101. SN_DMA_ADDR_PHYS);
  102. if (!*dma_handle) {
  103. printk(KERN_ERR "%s: out of ATEs\n", __FUNCTION__);
  104. free_pages((unsigned long)cpuaddr, get_order(size));
  105. return NULL;
  106. }
  107. return cpuaddr;
  108. }
  109. EXPORT_SYMBOL(sn_dma_alloc_coherent);
  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. 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. EXPORT_SYMBOL(sn_dma_free_coherent);
  130. /**
  131. * sn_dma_map_single - map a single page for DMA
  132. * @dev: device to map for
  133. * @cpu_addr: kernel virtual address of the region to map
  134. * @size: size of the region
  135. * @direction: DMA direction
  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. * TODO: simplify our interface;
  146. * figure out how to save dmamap handle so can use two step.
  147. */
  148. dma_addr_t sn_dma_map_single(struct device *dev, void *cpu_addr, size_t size,
  149. int direction)
  150. {
  151. dma_addr_t dma_addr;
  152. unsigned long phys_addr;
  153. struct pci_dev *pdev = to_pci_dev(dev);
  154. struct sn_pcibus_provider *provider = SN_PCIDEV_BUSPROVIDER(pdev);
  155. BUG_ON(dev->bus != &pci_bus_type);
  156. phys_addr = __pa(cpu_addr);
  157. dma_addr = provider->dma_map(pdev, phys_addr, size, SN_DMA_ADDR_PHYS);
  158. if (!dma_addr) {
  159. printk(KERN_ERR "%s: out of ATEs\n", __FUNCTION__);
  160. return 0;
  161. }
  162. return dma_addr;
  163. }
  164. EXPORT_SYMBOL(sn_dma_map_single);
  165. /**
  166. * sn_dma_unmap_single - unamp a DMA mapped page
  167. * @dev: device to sync
  168. * @dma_addr: DMA address to sync
  169. * @size: size of region
  170. * @direction: DMA direction
  171. *
  172. * This routine is supposed to sync the DMA region specified
  173. * by @dma_handle into the coherence domain. On SN, we're always cache
  174. * coherent, so we just need to free any ATEs associated with this mapping.
  175. */
  176. void sn_dma_unmap_single(struct device *dev, dma_addr_t dma_addr, size_t size,
  177. int direction)
  178. {
  179. struct pci_dev *pdev = to_pci_dev(dev);
  180. struct sn_pcibus_provider *provider = SN_PCIDEV_BUSPROVIDER(pdev);
  181. BUG_ON(dev->bus != &pci_bus_type);
  182. provider->dma_unmap(pdev, dma_addr, direction);
  183. }
  184. EXPORT_SYMBOL(sn_dma_unmap_single);
  185. /**
  186. * sn_dma_unmap_sg - unmap a DMA scatterlist
  187. * @dev: device to unmap
  188. * @sg: scatterlist to unmap
  189. * @nhwentries: number of scatterlist entries
  190. * @direction: DMA direction
  191. *
  192. * Unmap a set of streaming mode DMA translations.
  193. */
  194. void sn_dma_unmap_sg(struct device *dev, struct scatterlist *sg,
  195. int nhwentries, int direction)
  196. {
  197. int i;
  198. struct pci_dev *pdev = to_pci_dev(dev);
  199. struct sn_pcibus_provider *provider = SN_PCIDEV_BUSPROVIDER(pdev);
  200. BUG_ON(dev->bus != &pci_bus_type);
  201. for (i = 0; i < nhwentries; i++, sg++) {
  202. provider->dma_unmap(pdev, sg->dma_address, direction);
  203. sg->dma_address = (dma_addr_t) NULL;
  204. sg->dma_length = 0;
  205. }
  206. }
  207. EXPORT_SYMBOL(sn_dma_unmap_sg);
  208. /**
  209. * sn_dma_map_sg - map a scatterlist for DMA
  210. * @dev: device to map for
  211. * @sg: scatterlist to map
  212. * @nhwentries: number of entries
  213. * @direction: direction of the DMA transaction
  214. *
  215. * Maps each entry of @sg for DMA.
  216. */
  217. int sn_dma_map_sg(struct device *dev, struct scatterlist *sg, int nhwentries,
  218. int direction)
  219. {
  220. unsigned long phys_addr;
  221. struct scatterlist *saved_sg = sg;
  222. struct pci_dev *pdev = to_pci_dev(dev);
  223. struct sn_pcibus_provider *provider = SN_PCIDEV_BUSPROVIDER(pdev);
  224. int i;
  225. BUG_ON(dev->bus != &pci_bus_type);
  226. /*
  227. * Setup a DMA address for each entry in the scatterlist.
  228. */
  229. for (i = 0; i < nhwentries; i++, sg++) {
  230. phys_addr = SG_ENT_PHYS_ADDRESS(sg);
  231. sg->dma_address = provider->dma_map(pdev,
  232. phys_addr, sg->length,
  233. SN_DMA_ADDR_PHYS);
  234. if (!sg->dma_address) {
  235. printk(KERN_ERR "%s: out of ATEs\n", __FUNCTION__);
  236. /*
  237. * Free any successfully allocated entries.
  238. */
  239. if (i > 0)
  240. sn_dma_unmap_sg(dev, saved_sg, i, direction);
  241. return 0;
  242. }
  243. sg->dma_length = sg->length;
  244. }
  245. return nhwentries;
  246. }
  247. EXPORT_SYMBOL(sn_dma_map_sg);
  248. void sn_dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle,
  249. size_t size, int direction)
  250. {
  251. BUG_ON(dev->bus != &pci_bus_type);
  252. }
  253. EXPORT_SYMBOL(sn_dma_sync_single_for_cpu);
  254. void sn_dma_sync_single_for_device(struct device *dev, dma_addr_t dma_handle,
  255. size_t size, int direction)
  256. {
  257. BUG_ON(dev->bus != &pci_bus_type);
  258. }
  259. EXPORT_SYMBOL(sn_dma_sync_single_for_device);
  260. void sn_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
  261. int nelems, int direction)
  262. {
  263. BUG_ON(dev->bus != &pci_bus_type);
  264. }
  265. EXPORT_SYMBOL(sn_dma_sync_sg_for_cpu);
  266. void sn_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
  267. int nelems, int direction)
  268. {
  269. BUG_ON(dev->bus != &pci_bus_type);
  270. }
  271. EXPORT_SYMBOL(sn_dma_sync_sg_for_device);
  272. int sn_dma_mapping_error(dma_addr_t dma_addr)
  273. {
  274. return 0;
  275. }
  276. EXPORT_SYMBOL(sn_dma_mapping_error);
  277. char *sn_pci_get_legacy_mem(struct pci_bus *bus)
  278. {
  279. if (!SN_PCIBUS_BUSSOFT(bus))
  280. return ERR_PTR(-ENODEV);
  281. return (char *)(SN_PCIBUS_BUSSOFT(bus)->bs_legacy_mem | __IA64_UNCACHED_OFFSET);
  282. }
  283. int sn_pci_legacy_read(struct pci_bus *bus, u16 port, u32 *val, u8 size)
  284. {
  285. unsigned long addr;
  286. int ret;
  287. struct ia64_sal_retval isrv;
  288. /*
  289. * First, try the SN_SAL_IOIF_PCI_SAFE SAL call which can work
  290. * around hw issues at the pci bus level. SGI proms older than
  291. * 4.10 don't implment this.
  292. */
  293. SAL_CALL(isrv, SN_SAL_IOIF_PCI_SAFE,
  294. pci_domain_nr(bus), bus->number,
  295. 0, /* io */
  296. 0, /* read */
  297. port, size, __pa(val));
  298. if (isrv.status == 0)
  299. return size;
  300. /*
  301. * If the above failed, retry using the SAL_PROBE call which should
  302. * be present in all proms (but which cannot work round PCI chipset
  303. * bugs). This code is retained for compatability with old
  304. * pre-4.10 proms, and should be removed at some point in the future.
  305. */
  306. if (!SN_PCIBUS_BUSSOFT(bus))
  307. return -ENODEV;
  308. addr = SN_PCIBUS_BUSSOFT(bus)->bs_legacy_io | __IA64_UNCACHED_OFFSET;
  309. addr += port;
  310. ret = ia64_sn_probe_mem(addr, (long)size, (void *)val);
  311. if (ret == 2)
  312. return -EINVAL;
  313. if (ret == 1)
  314. *val = -1;
  315. return size;
  316. }
  317. int sn_pci_legacy_write(struct pci_bus *bus, u16 port, u32 val, u8 size)
  318. {
  319. int ret = size;
  320. unsigned long paddr;
  321. unsigned long *addr;
  322. struct ia64_sal_retval isrv;
  323. /*
  324. * First, try the SN_SAL_IOIF_PCI_SAFE SAL call which can work
  325. * around hw issues at the pci bus level. SGI proms older than
  326. * 4.10 don't implment this.
  327. */
  328. SAL_CALL(isrv, SN_SAL_IOIF_PCI_SAFE,
  329. pci_domain_nr(bus), bus->number,
  330. 0, /* io */
  331. 1, /* write */
  332. port, size, __pa(&val));
  333. if (isrv.status == 0)
  334. return size;
  335. /*
  336. * If the above failed, retry using the SAL_PROBE call which should
  337. * be present in all proms (but which cannot work round PCI chipset
  338. * bugs). This code is retained for compatability with old
  339. * pre-4.10 proms, and should be removed at some point in the future.
  340. */
  341. if (!SN_PCIBUS_BUSSOFT(bus)) {
  342. ret = -ENODEV;
  343. goto out;
  344. }
  345. /* Put the phys addr in uncached space */
  346. paddr = SN_PCIBUS_BUSSOFT(bus)->bs_legacy_io | __IA64_UNCACHED_OFFSET;
  347. paddr += port;
  348. addr = (unsigned long *)paddr;
  349. switch (size) {
  350. case 1:
  351. *(volatile u8 *)(addr) = (u8)(val);
  352. break;
  353. case 2:
  354. *(volatile u16 *)(addr) = (u16)(val);
  355. break;
  356. case 4:
  357. *(volatile u32 *)(addr) = (u32)(val);
  358. break;
  359. default:
  360. ret = -EINVAL;
  361. break;
  362. }
  363. out:
  364. return ret;
  365. }