pci_dma.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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/pcibr_provider.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, int 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, GFP_ATOMIC, 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(GFP_ATOMIC, 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. if (!*dma_handle) {
  102. printk(KERN_ERR "%s: out of ATEs\n", __FUNCTION__);
  103. free_pages((unsigned long)cpuaddr, get_order(size));
  104. return NULL;
  105. }
  106. return cpuaddr;
  107. }
  108. EXPORT_SYMBOL(sn_dma_alloc_coherent);
  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. 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. EXPORT_SYMBOL(sn_dma_free_coherent);
  129. /**
  130. * sn_dma_map_single - 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. *
  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. * TODO: simplify our interface;
  145. * figure out how to save dmamap handle so can use two step.
  146. */
  147. dma_addr_t sn_dma_map_single(struct device *dev, void *cpu_addr, size_t size,
  148. int direction)
  149. {
  150. dma_addr_t dma_addr;
  151. unsigned long phys_addr;
  152. struct pci_dev *pdev = to_pci_dev(dev);
  153. struct sn_pcibus_provider *provider = SN_PCIDEV_BUSPROVIDER(pdev);
  154. BUG_ON(dev->bus != &pci_bus_type);
  155. phys_addr = __pa(cpu_addr);
  156. dma_addr = provider->dma_map(pdev, phys_addr, size);
  157. if (!dma_addr) {
  158. printk(KERN_ERR "%s: out of ATEs\n", __FUNCTION__);
  159. return 0;
  160. }
  161. return dma_addr;
  162. }
  163. EXPORT_SYMBOL(sn_dma_map_single);
  164. /**
  165. * sn_dma_unmap_single - unamp a DMA mapped page
  166. * @dev: device to sync
  167. * @dma_addr: DMA address to sync
  168. * @size: size of region
  169. * @direction: DMA direction
  170. *
  171. * This routine is supposed to sync the DMA region specified
  172. * by @dma_handle into the coherence domain. On SN, we're always cache
  173. * coherent, so we just need to free any ATEs associated with this mapping.
  174. */
  175. void sn_dma_unmap_single(struct device *dev, dma_addr_t dma_addr, size_t size,
  176. int direction)
  177. {
  178. struct pci_dev *pdev = to_pci_dev(dev);
  179. struct sn_pcibus_provider *provider = SN_PCIDEV_BUSPROVIDER(pdev);
  180. BUG_ON(dev->bus != &pci_bus_type);
  181. provider->dma_unmap(pdev, dma_addr, direction);
  182. }
  183. EXPORT_SYMBOL(sn_dma_unmap_single);
  184. /**
  185. * sn_dma_unmap_sg - unmap a DMA scatterlist
  186. * @dev: device to unmap
  187. * @sg: scatterlist to unmap
  188. * @nhwentries: number of scatterlist entries
  189. * @direction: DMA direction
  190. *
  191. * Unmap a set of streaming mode DMA translations.
  192. */
  193. void sn_dma_unmap_sg(struct device *dev, struct scatterlist *sg,
  194. int nhwentries, int direction)
  195. {
  196. int i;
  197. struct pci_dev *pdev = to_pci_dev(dev);
  198. struct sn_pcibus_provider *provider = SN_PCIDEV_BUSPROVIDER(pdev);
  199. BUG_ON(dev->bus != &pci_bus_type);
  200. for (i = 0; i < nhwentries; i++, sg++) {
  201. provider->dma_unmap(pdev, sg->dma_address, direction);
  202. sg->dma_address = (dma_addr_t) NULL;
  203. sg->dma_length = 0;
  204. }
  205. }
  206. EXPORT_SYMBOL(sn_dma_unmap_sg);
  207. /**
  208. * sn_dma_map_sg - map a scatterlist for DMA
  209. * @dev: device to map for
  210. * @sg: scatterlist to map
  211. * @nhwentries: number of entries
  212. * @direction: direction of the DMA transaction
  213. *
  214. * Maps each entry of @sg for DMA.
  215. */
  216. int sn_dma_map_sg(struct device *dev, struct scatterlist *sg, int nhwentries,
  217. int direction)
  218. {
  219. unsigned long phys_addr;
  220. struct scatterlist *saved_sg = sg;
  221. struct pci_dev *pdev = to_pci_dev(dev);
  222. struct sn_pcibus_provider *provider = SN_PCIDEV_BUSPROVIDER(pdev);
  223. int i;
  224. BUG_ON(dev->bus != &pci_bus_type);
  225. /*
  226. * Setup a DMA address for each entry in the scatterlist.
  227. */
  228. for (i = 0; i < nhwentries; i++, sg++) {
  229. phys_addr = SG_ENT_PHYS_ADDRESS(sg);
  230. sg->dma_address = provider->dma_map(pdev,
  231. phys_addr, sg->length);
  232. if (!sg->dma_address) {
  233. printk(KERN_ERR "%s: out of ATEs\n", __FUNCTION__);
  234. /*
  235. * Free any successfully allocated entries.
  236. */
  237. if (i > 0)
  238. sn_dma_unmap_sg(dev, saved_sg, i, direction);
  239. return 0;
  240. }
  241. sg->dma_length = sg->length;
  242. }
  243. return nhwentries;
  244. }
  245. EXPORT_SYMBOL(sn_dma_map_sg);
  246. void sn_dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle,
  247. size_t size, int direction)
  248. {
  249. BUG_ON(dev->bus != &pci_bus_type);
  250. }
  251. EXPORT_SYMBOL(sn_dma_sync_single_for_cpu);
  252. void sn_dma_sync_single_for_device(struct device *dev, dma_addr_t dma_handle,
  253. size_t size, int direction)
  254. {
  255. BUG_ON(dev->bus != &pci_bus_type);
  256. }
  257. EXPORT_SYMBOL(sn_dma_sync_single_for_device);
  258. void sn_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
  259. int nelems, int direction)
  260. {
  261. BUG_ON(dev->bus != &pci_bus_type);
  262. }
  263. EXPORT_SYMBOL(sn_dma_sync_sg_for_cpu);
  264. void sn_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
  265. int nelems, int direction)
  266. {
  267. BUG_ON(dev->bus != &pci_bus_type);
  268. }
  269. EXPORT_SYMBOL(sn_dma_sync_sg_for_device);
  270. int sn_dma_mapping_error(dma_addr_t dma_addr)
  271. {
  272. return 0;
  273. }
  274. EXPORT_SYMBOL(sn_dma_mapping_error);
  275. char *sn_pci_get_legacy_mem(struct pci_bus *bus)
  276. {
  277. if (!SN_PCIBUS_BUSSOFT(bus))
  278. return ERR_PTR(-ENODEV);
  279. return (char *)(SN_PCIBUS_BUSSOFT(bus)->bs_legacy_mem | __IA64_UNCACHED_OFFSET);
  280. }
  281. int sn_pci_legacy_read(struct pci_bus *bus, u16 port, u32 *val, u8 size)
  282. {
  283. unsigned long addr;
  284. int ret;
  285. if (!SN_PCIBUS_BUSSOFT(bus))
  286. return -ENODEV;
  287. addr = SN_PCIBUS_BUSSOFT(bus)->bs_legacy_io | __IA64_UNCACHED_OFFSET;
  288. addr += port;
  289. ret = ia64_sn_probe_mem(addr, (long)size, (void *)val);
  290. if (ret == 2)
  291. return -EINVAL;
  292. if (ret == 1)
  293. *val = -1;
  294. return size;
  295. }
  296. int sn_pci_legacy_write(struct pci_bus *bus, u16 port, u32 val, u8 size)
  297. {
  298. int ret = size;
  299. unsigned long paddr;
  300. unsigned long *addr;
  301. if (!SN_PCIBUS_BUSSOFT(bus)) {
  302. ret = -ENODEV;
  303. goto out;
  304. }
  305. /* Put the phys addr in uncached space */
  306. paddr = SN_PCIBUS_BUSSOFT(bus)->bs_legacy_io | __IA64_UNCACHED_OFFSET;
  307. paddr += port;
  308. addr = (unsigned long *)paddr;
  309. switch (size) {
  310. case 1:
  311. *(volatile u8 *)(addr) = (u8)(val);
  312. break;
  313. case 2:
  314. *(volatile u16 *)(addr) = (u16)(val);
  315. break;
  316. case 4:
  317. *(volatile u32 *)(addr) = (u32)(val);
  318. break;
  319. default:
  320. ret = -EINVAL;
  321. break;
  322. }
  323. out:
  324. return ret;
  325. }