pci_dma.c 13 KB

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