pci_dma.c 13 KB

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