pci_dma.c 13 KB

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