pci_dma.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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/sn_sal.h>
  14. #include <asm/sn/pcibus_provider_defs.h>
  15. #include <asm/sn/pcidev.h>
  16. #define SG_ENT_VIRT_ADDRESS(sg) (page_address((sg)->page) + (sg)->offset)
  17. #define SG_ENT_PHYS_ADDRESS(SG) virt_to_phys(SG_ENT_VIRT_ADDRESS(SG))
  18. /**
  19. * sn_dma_supported - test a DMA mask
  20. * @dev: device to test
  21. * @mask: DMA mask to test
  22. *
  23. * Return whether the given PCI device DMA address mask can be supported
  24. * properly. For example, if your device can only drive the low 24-bits
  25. * during PCI bus mastering, then you would pass 0x00ffffff as the mask to
  26. * this function. Of course, SN only supports devices that have 32 or more
  27. * address bits when using the PMU.
  28. */
  29. int sn_dma_supported(struct device *dev, u64 mask)
  30. {
  31. BUG_ON(dev->bus != &pci_bus_type);
  32. if (mask < 0x7fffffff)
  33. return 0;
  34. return 1;
  35. }
  36. EXPORT_SYMBOL(sn_dma_supported);
  37. /**
  38. * sn_dma_set_mask - set the DMA mask
  39. * @dev: device to set
  40. * @dma_mask: new mask
  41. *
  42. * Set @dev's DMA mask if the hw supports it.
  43. */
  44. int sn_dma_set_mask(struct device *dev, u64 dma_mask)
  45. {
  46. BUG_ON(dev->bus != &pci_bus_type);
  47. if (!sn_dma_supported(dev, dma_mask))
  48. return 0;
  49. *dev->dma_mask = dma_mask;
  50. return 1;
  51. }
  52. EXPORT_SYMBOL(sn_dma_set_mask);
  53. /**
  54. * sn_dma_alloc_coherent - allocate memory for coherent DMA
  55. * @dev: device to allocate for
  56. * @size: size of the region
  57. * @dma_handle: DMA (bus) address
  58. * @flags: memory allocation flags
  59. *
  60. * dma_alloc_coherent() returns a pointer to a memory region suitable for
  61. * coherent DMA traffic to/from a PCI device. On SN platforms, this means
  62. * that @dma_handle will have the %PCIIO_DMA_CMD flag set.
  63. *
  64. * This interface is usually used for "command" streams (e.g. the command
  65. * queue for a SCSI controller). See Documentation/DMA-API.txt for
  66. * more information.
  67. */
  68. void *sn_dma_alloc_coherent(struct device *dev, size_t size,
  69. dma_addr_t * dma_handle, int flags)
  70. {
  71. void *cpuaddr;
  72. unsigned long phys_addr;
  73. struct pci_dev *pdev = to_pci_dev(dev);
  74. struct sn_pcibus_provider *provider = SN_PCIDEV_BUSPROVIDER(pdev);
  75. BUG_ON(dev->bus != &pci_bus_type);
  76. /*
  77. * Allocate the memory.
  78. * FIXME: We should be doing alloc_pages_node for the node closest
  79. * to the PCI device.
  80. */
  81. if (!(cpuaddr = (void *)__get_free_pages(GFP_ATOMIC, get_order(size))))
  82. return NULL;
  83. memset(cpuaddr, 0x0, size);
  84. /* physical addr. of the memory we just got */
  85. phys_addr = __pa(cpuaddr);
  86. /*
  87. * 64 bit address translations should never fail.
  88. * 32 bit translations can fail if there are insufficient mapping
  89. * resources.
  90. */
  91. *dma_handle = provider->dma_map_consistent(pdev, phys_addr, size);
  92. if (!*dma_handle) {
  93. printk(KERN_ERR "%s: out of ATEs\n", __FUNCTION__);
  94. free_pages((unsigned long)cpuaddr, get_order(size));
  95. return NULL;
  96. }
  97. return cpuaddr;
  98. }
  99. EXPORT_SYMBOL(sn_dma_alloc_coherent);
  100. /**
  101. * sn_pci_free_coherent - free memory associated with coherent DMAable region
  102. * @dev: device to free for
  103. * @size: size to free
  104. * @cpu_addr: kernel virtual address to free
  105. * @dma_handle: DMA address associated with this region
  106. *
  107. * Frees the memory allocated by dma_alloc_coherent(), potentially unmapping
  108. * any associated IOMMU mappings.
  109. */
  110. void sn_dma_free_coherent(struct device *dev, size_t size, void *cpu_addr,
  111. dma_addr_t dma_handle)
  112. {
  113. struct pci_dev *pdev = to_pci_dev(dev);
  114. struct sn_pcibus_provider *provider = SN_PCIDEV_BUSPROVIDER(pdev);
  115. BUG_ON(dev->bus != &pci_bus_type);
  116. provider->dma_unmap(pdev, dma_handle, 0);
  117. free_pages((unsigned long)cpu_addr, get_order(size));
  118. }
  119. EXPORT_SYMBOL(sn_dma_free_coherent);
  120. /**
  121. * sn_dma_map_single - map a single page for DMA
  122. * @dev: device to map for
  123. * @cpu_addr: kernel virtual address of the region to map
  124. * @size: size of the region
  125. * @direction: DMA direction
  126. *
  127. * Map the region pointed to by @cpu_addr for DMA and return the
  128. * DMA address.
  129. *
  130. * We map this to the one step pcibr_dmamap_trans interface rather than
  131. * the two step pcibr_dmamap_alloc/pcibr_dmamap_addr because we have
  132. * no way of saving the dmamap handle from the alloc to later free
  133. * (which is pretty much unacceptable).
  134. *
  135. * TODO: simplify our interface;
  136. * figure out how to save dmamap handle so can use two step.
  137. */
  138. dma_addr_t sn_dma_map_single(struct device *dev, void *cpu_addr, size_t size,
  139. int direction)
  140. {
  141. dma_addr_t dma_addr;
  142. unsigned long phys_addr;
  143. struct pci_dev *pdev = to_pci_dev(dev);
  144. struct sn_pcibus_provider *provider = SN_PCIDEV_BUSPROVIDER(pdev);
  145. BUG_ON(dev->bus != &pci_bus_type);
  146. phys_addr = __pa(cpu_addr);
  147. dma_addr = provider->dma_map(pdev, phys_addr, size);
  148. if (!dma_addr) {
  149. printk(KERN_ERR "%s: out of ATEs\n", __FUNCTION__);
  150. return 0;
  151. }
  152. return dma_addr;
  153. }
  154. EXPORT_SYMBOL(sn_dma_map_single);
  155. /**
  156. * sn_dma_unmap_single - unamp a DMA mapped page
  157. * @dev: device to sync
  158. * @dma_addr: DMA address to sync
  159. * @size: size of region
  160. * @direction: DMA direction
  161. *
  162. * This routine is supposed to sync the DMA region specified
  163. * by @dma_handle into the coherence domain. On SN, we're always cache
  164. * coherent, so we just need to free any ATEs associated with this mapping.
  165. */
  166. void sn_dma_unmap_single(struct device *dev, dma_addr_t dma_addr, size_t size,
  167. int direction)
  168. {
  169. struct pci_dev *pdev = to_pci_dev(dev);
  170. struct sn_pcibus_provider *provider = SN_PCIDEV_BUSPROVIDER(pdev);
  171. BUG_ON(dev->bus != &pci_bus_type);
  172. provider->dma_unmap(pdev, dma_addr, direction);
  173. }
  174. EXPORT_SYMBOL(sn_dma_unmap_single);
  175. /**
  176. * sn_dma_unmap_sg - unmap a DMA scatterlist
  177. * @dev: device to unmap
  178. * @sg: scatterlist to unmap
  179. * @nhwentries: number of scatterlist entries
  180. * @direction: DMA direction
  181. *
  182. * Unmap a set of streaming mode DMA translations.
  183. */
  184. void sn_dma_unmap_sg(struct device *dev, struct scatterlist *sg,
  185. int nhwentries, int direction)
  186. {
  187. int i;
  188. struct pci_dev *pdev = to_pci_dev(dev);
  189. struct sn_pcibus_provider *provider = SN_PCIDEV_BUSPROVIDER(pdev);
  190. BUG_ON(dev->bus != &pci_bus_type);
  191. for (i = 0; i < nhwentries; i++, sg++) {
  192. provider->dma_unmap(pdev, sg->dma_address, direction);
  193. sg->dma_address = (dma_addr_t) NULL;
  194. sg->dma_length = 0;
  195. }
  196. }
  197. EXPORT_SYMBOL(sn_dma_unmap_sg);
  198. /**
  199. * sn_dma_map_sg - map a scatterlist for DMA
  200. * @dev: device to map for
  201. * @sg: scatterlist to map
  202. * @nhwentries: number of entries
  203. * @direction: direction of the DMA transaction
  204. *
  205. * Maps each entry of @sg for DMA.
  206. */
  207. int sn_dma_map_sg(struct device *dev, struct scatterlist *sg, int nhwentries,
  208. int direction)
  209. {
  210. unsigned long phys_addr;
  211. struct scatterlist *saved_sg = sg;
  212. struct pci_dev *pdev = to_pci_dev(dev);
  213. struct sn_pcibus_provider *provider = SN_PCIDEV_BUSPROVIDER(pdev);
  214. int i;
  215. BUG_ON(dev->bus != &pci_bus_type);
  216. /*
  217. * Setup a DMA address for each entry in the scatterlist.
  218. */
  219. for (i = 0; i < nhwentries; i++, sg++) {
  220. phys_addr = SG_ENT_PHYS_ADDRESS(sg);
  221. sg->dma_address = provider->dma_map(pdev,
  222. phys_addr, sg->length);
  223. if (!sg->dma_address) {
  224. printk(KERN_ERR "%s: out of ATEs\n", __FUNCTION__);
  225. /*
  226. * Free any successfully allocated entries.
  227. */
  228. if (i > 0)
  229. sn_dma_unmap_sg(dev, saved_sg, i, direction);
  230. return 0;
  231. }
  232. sg->dma_length = sg->length;
  233. }
  234. return nhwentries;
  235. }
  236. EXPORT_SYMBOL(sn_dma_map_sg);
  237. void sn_dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle,
  238. size_t size, int direction)
  239. {
  240. BUG_ON(dev->bus != &pci_bus_type);
  241. }
  242. EXPORT_SYMBOL(sn_dma_sync_single_for_cpu);
  243. void sn_dma_sync_single_for_device(struct device *dev, dma_addr_t dma_handle,
  244. size_t size, int direction)
  245. {
  246. BUG_ON(dev->bus != &pci_bus_type);
  247. }
  248. EXPORT_SYMBOL(sn_dma_sync_single_for_device);
  249. void sn_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
  250. int nelems, int direction)
  251. {
  252. BUG_ON(dev->bus != &pci_bus_type);
  253. }
  254. EXPORT_SYMBOL(sn_dma_sync_sg_for_cpu);
  255. void sn_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
  256. int nelems, int direction)
  257. {
  258. BUG_ON(dev->bus != &pci_bus_type);
  259. }
  260. EXPORT_SYMBOL(sn_dma_sync_sg_for_device);
  261. int sn_dma_mapping_error(dma_addr_t dma_addr)
  262. {
  263. return 0;
  264. }
  265. EXPORT_SYMBOL(sn_dma_mapping_error);
  266. char *sn_pci_get_legacy_mem(struct pci_bus *bus)
  267. {
  268. if (!SN_PCIBUS_BUSSOFT(bus))
  269. return ERR_PTR(-ENODEV);
  270. return (char *)(SN_PCIBUS_BUSSOFT(bus)->bs_legacy_mem | __IA64_UNCACHED_OFFSET);
  271. }
  272. int sn_pci_legacy_read(struct pci_bus *bus, u16 port, u32 *val, u8 size)
  273. {
  274. unsigned long addr;
  275. int ret;
  276. if (!SN_PCIBUS_BUSSOFT(bus))
  277. return -ENODEV;
  278. addr = SN_PCIBUS_BUSSOFT(bus)->bs_legacy_io | __IA64_UNCACHED_OFFSET;
  279. addr += port;
  280. ret = ia64_sn_probe_mem(addr, (long)size, (void *)val);
  281. if (ret == 2)
  282. return -EINVAL;
  283. if (ret == 1)
  284. *val = -1;
  285. return size;
  286. }
  287. int sn_pci_legacy_write(struct pci_bus *bus, u16 port, u32 val, u8 size)
  288. {
  289. int ret = size;
  290. unsigned long paddr;
  291. unsigned long *addr;
  292. if (!SN_PCIBUS_BUSSOFT(bus)) {
  293. ret = -ENODEV;
  294. goto out;
  295. }
  296. /* Put the phys addr in uncached space */
  297. paddr = SN_PCIBUS_BUSSOFT(bus)->bs_legacy_io | __IA64_UNCACHED_OFFSET;
  298. paddr += port;
  299. addr = (unsigned long *)paddr;
  300. switch (size) {
  301. case 1:
  302. *(volatile u8 *)(addr) = (u8)(val);
  303. break;
  304. case 2:
  305. *(volatile u16 *)(addr) = (u16)(val);
  306. break;
  307. case 4:
  308. *(volatile u32 *)(addr) = (u32)(val);
  309. break;
  310. default:
  311. ret = -EINVAL;
  312. break;
  313. }
  314. out:
  315. return ret;
  316. }