pci_dma.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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_is_pci(dev));
  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_is_pci(dev));
  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. unsigned long attrs)
  73. {
  74. void *cpuaddr;
  75. unsigned long phys_addr;
  76. int node;
  77. struct pci_dev *pdev = to_pci_dev(dev);
  78. struct sn_pcibus_provider *provider = SN_PCIDEV_BUSPROVIDER(pdev);
  79. BUG_ON(!dev_is_pci(dev));
  80. /*
  81. * Allocate the memory.
  82. */
  83. node = pcibus_to_node(pdev->bus);
  84. if (likely(node >=0)) {
  85. struct page *p = __alloc_pages_node(node,
  86. flags, get_order(size));
  87. if (likely(p))
  88. cpuaddr = page_address(p);
  89. else
  90. return NULL;
  91. } else
  92. cpuaddr = (void *)__get_free_pages(flags, get_order(size));
  93. if (unlikely(!cpuaddr))
  94. return NULL;
  95. memset(cpuaddr, 0x0, size);
  96. /* physical addr. of the memory we just got */
  97. phys_addr = __pa(cpuaddr);
  98. /*
  99. * 64 bit address translations should never fail.
  100. * 32 bit translations can fail if there are insufficient mapping
  101. * resources.
  102. */
  103. *dma_handle = provider->dma_map_consistent(pdev, phys_addr, size,
  104. SN_DMA_ADDR_PHYS);
  105. if (!*dma_handle) {
  106. printk(KERN_ERR "%s: out of ATEs\n", __func__);
  107. free_pages((unsigned long)cpuaddr, get_order(size));
  108. return NULL;
  109. }
  110. return cpuaddr;
  111. }
  112. /**
  113. * sn_pci_free_coherent - free memory associated with coherent DMAable region
  114. * @dev: device to free for
  115. * @size: size to free
  116. * @cpu_addr: kernel virtual address to free
  117. * @dma_handle: DMA address associated with this region
  118. *
  119. * Frees the memory allocated by dma_alloc_coherent(), potentially unmapping
  120. * any associated IOMMU mappings.
  121. */
  122. static void sn_dma_free_coherent(struct device *dev, size_t size, void *cpu_addr,
  123. dma_addr_t dma_handle, unsigned long attrs)
  124. {
  125. struct pci_dev *pdev = to_pci_dev(dev);
  126. struct sn_pcibus_provider *provider = SN_PCIDEV_BUSPROVIDER(pdev);
  127. BUG_ON(!dev_is_pci(dev));
  128. provider->dma_unmap(pdev, dma_handle, 0);
  129. free_pages((unsigned long)cpu_addr, get_order(size));
  130. }
  131. /**
  132. * sn_dma_map_single_attrs - map a single page for DMA
  133. * @dev: device to map for
  134. * @cpu_addr: kernel virtual address of the region to map
  135. * @size: size of the region
  136. * @direction: DMA direction
  137. * @attrs: optional dma attributes
  138. *
  139. * Map the region pointed to by @cpu_addr for DMA and return the
  140. * DMA address.
  141. *
  142. * We map this to the one step pcibr_dmamap_trans interface rather than
  143. * the two step pcibr_dmamap_alloc/pcibr_dmamap_addr because we have
  144. * no way of saving the dmamap handle from the alloc to later free
  145. * (which is pretty much unacceptable).
  146. *
  147. * mappings with the DMA_ATTR_WRITE_BARRIER get mapped with
  148. * dma_map_consistent() so that writes force a flush of pending DMA.
  149. * (See "SGI Altix Architecture Considerations for Linux Device Drivers",
  150. * Document Number: 007-4763-001)
  151. *
  152. * TODO: simplify our interface;
  153. * figure out how to save dmamap handle so can use two step.
  154. */
  155. static dma_addr_t sn_dma_map_page(struct device *dev, struct page *page,
  156. unsigned long offset, size_t size,
  157. enum dma_data_direction dir,
  158. unsigned long attrs)
  159. {
  160. void *cpu_addr = page_address(page) + offset;
  161. dma_addr_t dma_addr;
  162. unsigned long phys_addr;
  163. struct pci_dev *pdev = to_pci_dev(dev);
  164. struct sn_pcibus_provider *provider = SN_PCIDEV_BUSPROVIDER(pdev);
  165. BUG_ON(!dev_is_pci(dev));
  166. phys_addr = __pa(cpu_addr);
  167. if (attrs & DMA_ATTR_WRITE_BARRIER)
  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. unsigned long 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_is_pci(dev));
  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. unsigned long 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_is_pci(dev));
  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. unsigned long 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. BUG_ON(!dev_is_pci(dev));
  250. /*
  251. * Setup a DMA address for each entry in the scatterlist.
  252. */
  253. for_each_sg(sgl, sg, nhwentries, i) {
  254. dma_addr_t dma_addr;
  255. phys_addr = SG_ENT_PHYS_ADDRESS(sg);
  256. if (attrs & DMA_ATTR_WRITE_BARRIER)
  257. dma_addr = provider->dma_map_consistent(pdev,
  258. phys_addr,
  259. sg->length,
  260. SN_DMA_ADDR_PHYS);
  261. else
  262. dma_addr = provider->dma_map(pdev, phys_addr,
  263. sg->length,
  264. SN_DMA_ADDR_PHYS);
  265. sg->dma_address = dma_addr;
  266. if (!sg->dma_address) {
  267. printk(KERN_ERR "%s: out of ATEs\n", __func__);
  268. /*
  269. * Free any successfully allocated entries.
  270. */
  271. if (i > 0)
  272. sn_dma_unmap_sg(dev, saved_sg, i, dir, 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, enum dma_data_direction dir)
  281. {
  282. BUG_ON(!dev_is_pci(dev));
  283. }
  284. static void sn_dma_sync_single_for_device(struct device *dev, dma_addr_t dma_handle,
  285. size_t size,
  286. enum dma_data_direction dir)
  287. {
  288. BUG_ON(!dev_is_pci(dev));
  289. }
  290. static void sn_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
  291. int nelems, enum dma_data_direction dir)
  292. {
  293. BUG_ON(!dev_is_pci(dev));
  294. }
  295. static void sn_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
  296. int nelems, enum dma_data_direction dir)
  297. {
  298. BUG_ON(!dev_is_pci(dev));
  299. }
  300. static int sn_dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
  301. {
  302. return 0;
  303. }
  304. u64 sn_dma_get_required_mask(struct device *dev)
  305. {
  306. return DMA_BIT_MASK(64);
  307. }
  308. EXPORT_SYMBOL_GPL(sn_dma_get_required_mask);
  309. char *sn_pci_get_legacy_mem(struct pci_bus *bus)
  310. {
  311. if (!SN_PCIBUS_BUSSOFT(bus))
  312. return ERR_PTR(-ENODEV);
  313. return (char *)(SN_PCIBUS_BUSSOFT(bus)->bs_legacy_mem | __IA64_UNCACHED_OFFSET);
  314. }
  315. int sn_pci_legacy_read(struct pci_bus *bus, u16 port, u32 *val, u8 size)
  316. {
  317. unsigned long addr;
  318. int ret;
  319. struct ia64_sal_retval isrv;
  320. /*
  321. * First, try the SN_SAL_IOIF_PCI_SAFE SAL call which can work
  322. * around hw issues at the pci bus level. SGI proms older than
  323. * 4.10 don't implement this.
  324. */
  325. SAL_CALL(isrv, SN_SAL_IOIF_PCI_SAFE,
  326. pci_domain_nr(bus), bus->number,
  327. 0, /* io */
  328. 0, /* read */
  329. port, size, __pa(val));
  330. if (isrv.status == 0)
  331. return size;
  332. /*
  333. * If the above failed, retry using the SAL_PROBE call which should
  334. * be present in all proms (but which cannot work round PCI chipset
  335. * bugs). This code is retained for compatibility with old
  336. * pre-4.10 proms, and should be removed at some point in the future.
  337. */
  338. if (!SN_PCIBUS_BUSSOFT(bus))
  339. return -ENODEV;
  340. addr = SN_PCIBUS_BUSSOFT(bus)->bs_legacy_io | __IA64_UNCACHED_OFFSET;
  341. addr += port;
  342. ret = ia64_sn_probe_mem(addr, (long)size, (void *)val);
  343. if (ret == 2)
  344. return -EINVAL;
  345. if (ret == 1)
  346. *val = -1;
  347. return size;
  348. }
  349. int sn_pci_legacy_write(struct pci_bus *bus, u16 port, u32 val, u8 size)
  350. {
  351. int ret = size;
  352. unsigned long paddr;
  353. unsigned long *addr;
  354. struct ia64_sal_retval isrv;
  355. /*
  356. * First, try the SN_SAL_IOIF_PCI_SAFE SAL call which can work
  357. * around hw issues at the pci bus level. SGI proms older than
  358. * 4.10 don't implement this.
  359. */
  360. SAL_CALL(isrv, SN_SAL_IOIF_PCI_SAFE,
  361. pci_domain_nr(bus), bus->number,
  362. 0, /* io */
  363. 1, /* write */
  364. port, size, __pa(&val));
  365. if (isrv.status == 0)
  366. return size;
  367. /*
  368. * If the above failed, retry using the SAL_PROBE call which should
  369. * be present in all proms (but which cannot work round PCI chipset
  370. * bugs). This code is retained for compatibility with old
  371. * pre-4.10 proms, and should be removed at some point in the future.
  372. */
  373. if (!SN_PCIBUS_BUSSOFT(bus)) {
  374. ret = -ENODEV;
  375. goto out;
  376. }
  377. /* Put the phys addr in uncached space */
  378. paddr = SN_PCIBUS_BUSSOFT(bus)->bs_legacy_io | __IA64_UNCACHED_OFFSET;
  379. paddr += port;
  380. addr = (unsigned long *)paddr;
  381. switch (size) {
  382. case 1:
  383. *(volatile u8 *)(addr) = (u8)(val);
  384. break;
  385. case 2:
  386. *(volatile u16 *)(addr) = (u16)(val);
  387. break;
  388. case 4:
  389. *(volatile u32 *)(addr) = (u32)(val);
  390. break;
  391. default:
  392. ret = -EINVAL;
  393. break;
  394. }
  395. out:
  396. return ret;
  397. }
  398. static struct dma_map_ops sn_dma_ops = {
  399. .alloc = sn_dma_alloc_coherent,
  400. .free = sn_dma_free_coherent,
  401. .map_page = sn_dma_map_page,
  402. .unmap_page = sn_dma_unmap_page,
  403. .map_sg = sn_dma_map_sg,
  404. .unmap_sg = sn_dma_unmap_sg,
  405. .sync_single_for_cpu = sn_dma_sync_single_for_cpu,
  406. .sync_sg_for_cpu = sn_dma_sync_sg_for_cpu,
  407. .sync_single_for_device = sn_dma_sync_single_for_device,
  408. .sync_sg_for_device = sn_dma_sync_sg_for_device,
  409. .mapping_error = sn_dma_mapping_error,
  410. .dma_supported = sn_dma_supported,
  411. };
  412. void sn_dma_init(void)
  413. {
  414. dma_ops = &sn_dma_ops;
  415. }