pci_dma.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. struct pci_dev *pdev = to_pci_dev(dev);
  75. struct sn_pcibus_provider *provider = SN_PCIDEV_BUSPROVIDER(pdev);
  76. BUG_ON(dev->bus != &pci_bus_type);
  77. /*
  78. * Allocate the memory.
  79. * FIXME: We should be doing alloc_pages_node for the node closest
  80. * to the PCI device.
  81. */
  82. if (!(cpuaddr = (void *)__get_free_pages(GFP_ATOMIC, get_order(size))))
  83. return NULL;
  84. memset(cpuaddr, 0x0, size);
  85. /* physical addr. of the memory we just got */
  86. phys_addr = __pa(cpuaddr);
  87. /*
  88. * 64 bit address translations should never fail.
  89. * 32 bit translations can fail if there are insufficient mapping
  90. * resources.
  91. */
  92. *dma_handle = provider->dma_map_consistent(pdev, phys_addr, size);
  93. if (!*dma_handle) {
  94. printk(KERN_ERR "%s: out of ATEs\n", __FUNCTION__);
  95. free_pages((unsigned long)cpuaddr, get_order(size));
  96. return NULL;
  97. }
  98. return cpuaddr;
  99. }
  100. EXPORT_SYMBOL(sn_dma_alloc_coherent);
  101. /**
  102. * sn_pci_free_coherent - free memory associated with coherent DMAable region
  103. * @dev: device to free for
  104. * @size: size to free
  105. * @cpu_addr: kernel virtual address to free
  106. * @dma_handle: DMA address associated with this region
  107. *
  108. * Frees the memory allocated by dma_alloc_coherent(), potentially unmapping
  109. * any associated IOMMU mappings.
  110. */
  111. void sn_dma_free_coherent(struct device *dev, size_t size, void *cpu_addr,
  112. dma_addr_t dma_handle)
  113. {
  114. struct pci_dev *pdev = to_pci_dev(dev);
  115. struct sn_pcibus_provider *provider = SN_PCIDEV_BUSPROVIDER(pdev);
  116. BUG_ON(dev->bus != &pci_bus_type);
  117. provider->dma_unmap(pdev, dma_handle, 0);
  118. free_pages((unsigned long)cpu_addr, get_order(size));
  119. }
  120. EXPORT_SYMBOL(sn_dma_free_coherent);
  121. /**
  122. * sn_dma_map_single - map a single page for DMA
  123. * @dev: device to map for
  124. * @cpu_addr: kernel virtual address of the region to map
  125. * @size: size of the region
  126. * @direction: DMA direction
  127. *
  128. * Map the region pointed to by @cpu_addr for DMA and return the
  129. * DMA address.
  130. *
  131. * We map this to the one step pcibr_dmamap_trans interface rather than
  132. * the two step pcibr_dmamap_alloc/pcibr_dmamap_addr because we have
  133. * no way of saving the dmamap handle from the alloc to later free
  134. * (which is pretty much unacceptable).
  135. *
  136. * TODO: simplify our interface;
  137. * figure out how to save dmamap handle so can use two step.
  138. */
  139. dma_addr_t sn_dma_map_single(struct device *dev, void *cpu_addr, size_t size,
  140. int direction)
  141. {
  142. dma_addr_t dma_addr;
  143. unsigned long phys_addr;
  144. struct pci_dev *pdev = to_pci_dev(dev);
  145. struct sn_pcibus_provider *provider = SN_PCIDEV_BUSPROVIDER(pdev);
  146. BUG_ON(dev->bus != &pci_bus_type);
  147. phys_addr = __pa(cpu_addr);
  148. dma_addr = provider->dma_map(pdev, phys_addr, size);
  149. if (!dma_addr) {
  150. printk(KERN_ERR "%s: out of ATEs\n", __FUNCTION__);
  151. return 0;
  152. }
  153. return dma_addr;
  154. }
  155. EXPORT_SYMBOL(sn_dma_map_single);
  156. /**
  157. * sn_dma_unmap_single - unamp a DMA mapped page
  158. * @dev: device to sync
  159. * @dma_addr: DMA address to sync
  160. * @size: size of region
  161. * @direction: DMA direction
  162. *
  163. * This routine is supposed to sync the DMA region specified
  164. * by @dma_handle into the coherence domain. On SN, we're always cache
  165. * coherent, so we just need to free any ATEs associated with this mapping.
  166. */
  167. void sn_dma_unmap_single(struct device *dev, dma_addr_t dma_addr, size_t size,
  168. int direction)
  169. {
  170. struct pci_dev *pdev = to_pci_dev(dev);
  171. struct sn_pcibus_provider *provider = SN_PCIDEV_BUSPROVIDER(pdev);
  172. BUG_ON(dev->bus != &pci_bus_type);
  173. provider->dma_unmap(pdev, dma_addr, direction);
  174. }
  175. EXPORT_SYMBOL(sn_dma_unmap_single);
  176. /**
  177. * sn_dma_unmap_sg - unmap a DMA scatterlist
  178. * @dev: device to unmap
  179. * @sg: scatterlist to unmap
  180. * @nhwentries: number of scatterlist entries
  181. * @direction: DMA direction
  182. *
  183. * Unmap a set of streaming mode DMA translations.
  184. */
  185. void sn_dma_unmap_sg(struct device *dev, struct scatterlist *sg,
  186. int nhwentries, int direction)
  187. {
  188. int i;
  189. struct pci_dev *pdev = to_pci_dev(dev);
  190. struct sn_pcibus_provider *provider = SN_PCIDEV_BUSPROVIDER(pdev);
  191. BUG_ON(dev->bus != &pci_bus_type);
  192. for (i = 0; i < nhwentries; i++, sg++) {
  193. provider->dma_unmap(pdev, sg->dma_address, direction);
  194. sg->dma_address = (dma_addr_t) NULL;
  195. sg->dma_length = 0;
  196. }
  197. }
  198. EXPORT_SYMBOL(sn_dma_unmap_sg);
  199. /**
  200. * sn_dma_map_sg - map a scatterlist for DMA
  201. * @dev: device to map for
  202. * @sg: scatterlist to map
  203. * @nhwentries: number of entries
  204. * @direction: direction of the DMA transaction
  205. *
  206. * Maps each entry of @sg for DMA.
  207. */
  208. int sn_dma_map_sg(struct device *dev, struct scatterlist *sg, int nhwentries,
  209. int direction)
  210. {
  211. unsigned long phys_addr;
  212. struct scatterlist *saved_sg = sg;
  213. struct pci_dev *pdev = to_pci_dev(dev);
  214. struct sn_pcibus_provider *provider = SN_PCIDEV_BUSPROVIDER(pdev);
  215. int i;
  216. BUG_ON(dev->bus != &pci_bus_type);
  217. /*
  218. * Setup a DMA address for each entry in the scatterlist.
  219. */
  220. for (i = 0; i < nhwentries; i++, sg++) {
  221. phys_addr = SG_ENT_PHYS_ADDRESS(sg);
  222. sg->dma_address = provider->dma_map(pdev,
  223. phys_addr, sg->length);
  224. if (!sg->dma_address) {
  225. printk(KERN_ERR "%s: out of ATEs\n", __FUNCTION__);
  226. /*
  227. * Free any successfully allocated entries.
  228. */
  229. if (i > 0)
  230. sn_dma_unmap_sg(dev, saved_sg, i, direction);
  231. return 0;
  232. }
  233. sg->dma_length = sg->length;
  234. }
  235. return nhwentries;
  236. }
  237. EXPORT_SYMBOL(sn_dma_map_sg);
  238. void sn_dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle,
  239. size_t size, int direction)
  240. {
  241. BUG_ON(dev->bus != &pci_bus_type);
  242. }
  243. EXPORT_SYMBOL(sn_dma_sync_single_for_cpu);
  244. void sn_dma_sync_single_for_device(struct device *dev, dma_addr_t dma_handle,
  245. size_t size, int direction)
  246. {
  247. BUG_ON(dev->bus != &pci_bus_type);
  248. }
  249. EXPORT_SYMBOL(sn_dma_sync_single_for_device);
  250. void sn_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
  251. int nelems, int direction)
  252. {
  253. BUG_ON(dev->bus != &pci_bus_type);
  254. }
  255. EXPORT_SYMBOL(sn_dma_sync_sg_for_cpu);
  256. void sn_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
  257. int nelems, int direction)
  258. {
  259. BUG_ON(dev->bus != &pci_bus_type);
  260. }
  261. EXPORT_SYMBOL(sn_dma_sync_sg_for_device);
  262. int sn_dma_mapping_error(dma_addr_t dma_addr)
  263. {
  264. return 0;
  265. }
  266. EXPORT_SYMBOL(sn_dma_mapping_error);
  267. char *sn_pci_get_legacy_mem(struct pci_bus *bus)
  268. {
  269. if (!SN_PCIBUS_BUSSOFT(bus))
  270. return ERR_PTR(-ENODEV);
  271. return (char *)(SN_PCIBUS_BUSSOFT(bus)->bs_legacy_mem | __IA64_UNCACHED_OFFSET);
  272. }
  273. int sn_pci_legacy_read(struct pci_bus *bus, u16 port, u32 *val, u8 size)
  274. {
  275. unsigned long addr;
  276. int ret;
  277. if (!SN_PCIBUS_BUSSOFT(bus))
  278. return -ENODEV;
  279. addr = SN_PCIBUS_BUSSOFT(bus)->bs_legacy_io | __IA64_UNCACHED_OFFSET;
  280. addr += port;
  281. ret = ia64_sn_probe_mem(addr, (long)size, (void *)val);
  282. if (ret == 2)
  283. return -EINVAL;
  284. if (ret == 1)
  285. *val = -1;
  286. return size;
  287. }
  288. int sn_pci_legacy_write(struct pci_bus *bus, u16 port, u32 val, u8 size)
  289. {
  290. int ret = size;
  291. unsigned long paddr;
  292. unsigned long *addr;
  293. if (!SN_PCIBUS_BUSSOFT(bus)) {
  294. ret = -ENODEV;
  295. goto out;
  296. }
  297. /* Put the phys addr in uncached space */
  298. paddr = SN_PCIBUS_BUSSOFT(bus)->bs_legacy_io | __IA64_UNCACHED_OFFSET;
  299. paddr += port;
  300. addr = (unsigned long *)paddr;
  301. switch (size) {
  302. case 1:
  303. *(volatile u8 *)(addr) = (u8)(val);
  304. break;
  305. case 2:
  306. *(volatile u16 *)(addr) = (u16)(val);
  307. break;
  308. case 4:
  309. *(volatile u32 *)(addr) = (u32)(val);
  310. break;
  311. default:
  312. ret = -EINVAL;
  313. break;
  314. }
  315. out:
  316. return ret;
  317. }