dma-octeon.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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 Ani Joshi <ajoshi@unixbox.com>
  7. * Copyright (C) 2000, 2001 Ralf Baechle <ralf@gnu.org>
  8. * Copyright (C) 2005 Ilya A. Volynets-Evenbakh <ilya@total-knowledge.com>
  9. * swiped from i386, and cloned for MIPS by Geert, polished by Ralf.
  10. * IP32 changes by Ilya.
  11. * Copyright (C) 2010 Cavium Networks, Inc.
  12. */
  13. #include <linux/dma-mapping.h>
  14. #include <linux/scatterlist.h>
  15. #include <linux/bootmem.h>
  16. #include <linux/export.h>
  17. #include <linux/swiotlb.h>
  18. #include <linux/types.h>
  19. #include <linux/init.h>
  20. #include <linux/mm.h>
  21. #include <asm/bootinfo.h>
  22. #include <asm/octeon/octeon.h>
  23. #ifdef CONFIG_PCI
  24. #include <asm/octeon/pci-octeon.h>
  25. #include <asm/octeon/cvmx-npi-defs.h>
  26. #include <asm/octeon/cvmx-pci-defs.h>
  27. static dma_addr_t octeon_hole_phys_to_dma(phys_addr_t paddr)
  28. {
  29. if (paddr >= CVMX_PCIE_BAR1_PHYS_BASE && paddr < (CVMX_PCIE_BAR1_PHYS_BASE + CVMX_PCIE_BAR1_PHYS_SIZE))
  30. return paddr - CVMX_PCIE_BAR1_PHYS_BASE + CVMX_PCIE_BAR1_RC_BASE;
  31. else
  32. return paddr;
  33. }
  34. static phys_addr_t octeon_hole_dma_to_phys(dma_addr_t daddr)
  35. {
  36. if (daddr >= CVMX_PCIE_BAR1_RC_BASE)
  37. return daddr + CVMX_PCIE_BAR1_PHYS_BASE - CVMX_PCIE_BAR1_RC_BASE;
  38. else
  39. return daddr;
  40. }
  41. static dma_addr_t octeon_gen1_phys_to_dma(struct device *dev, phys_addr_t paddr)
  42. {
  43. if (paddr >= 0x410000000ull && paddr < 0x420000000ull)
  44. paddr -= 0x400000000ull;
  45. return octeon_hole_phys_to_dma(paddr);
  46. }
  47. static phys_addr_t octeon_gen1_dma_to_phys(struct device *dev, dma_addr_t daddr)
  48. {
  49. daddr = octeon_hole_dma_to_phys(daddr);
  50. if (daddr >= 0x10000000ull && daddr < 0x20000000ull)
  51. daddr += 0x400000000ull;
  52. return daddr;
  53. }
  54. static dma_addr_t octeon_big_phys_to_dma(struct device *dev, phys_addr_t paddr)
  55. {
  56. if (paddr >= 0x410000000ull && paddr < 0x420000000ull)
  57. paddr -= 0x400000000ull;
  58. /* Anything in the BAR1 hole or above goes via BAR2 */
  59. if (paddr >= 0xf0000000ull)
  60. paddr = OCTEON_BAR2_PCI_ADDRESS + paddr;
  61. return paddr;
  62. }
  63. static phys_addr_t octeon_big_dma_to_phys(struct device *dev, dma_addr_t daddr)
  64. {
  65. if (daddr >= OCTEON_BAR2_PCI_ADDRESS)
  66. daddr -= OCTEON_BAR2_PCI_ADDRESS;
  67. if (daddr >= 0x10000000ull && daddr < 0x20000000ull)
  68. daddr += 0x400000000ull;
  69. return daddr;
  70. }
  71. static dma_addr_t octeon_small_phys_to_dma(struct device *dev,
  72. phys_addr_t paddr)
  73. {
  74. if (paddr >= 0x410000000ull && paddr < 0x420000000ull)
  75. paddr -= 0x400000000ull;
  76. /* Anything not in the BAR1 range goes via BAR2 */
  77. if (paddr >= octeon_bar1_pci_phys && paddr < octeon_bar1_pci_phys + 0x8000000ull)
  78. paddr = paddr - octeon_bar1_pci_phys;
  79. else
  80. paddr = OCTEON_BAR2_PCI_ADDRESS + paddr;
  81. return paddr;
  82. }
  83. static phys_addr_t octeon_small_dma_to_phys(struct device *dev,
  84. dma_addr_t daddr)
  85. {
  86. if (daddr >= OCTEON_BAR2_PCI_ADDRESS)
  87. daddr -= OCTEON_BAR2_PCI_ADDRESS;
  88. else
  89. daddr += octeon_bar1_pci_phys;
  90. if (daddr >= 0x10000000ull && daddr < 0x20000000ull)
  91. daddr += 0x400000000ull;
  92. return daddr;
  93. }
  94. #endif /* CONFIG_PCI */
  95. static dma_addr_t octeon_dma_map_page(struct device *dev, struct page *page,
  96. unsigned long offset, size_t size, enum dma_data_direction direction,
  97. struct dma_attrs *attrs)
  98. {
  99. dma_addr_t daddr = swiotlb_map_page(dev, page, offset, size,
  100. direction, attrs);
  101. mb();
  102. return daddr;
  103. }
  104. static int octeon_dma_map_sg(struct device *dev, struct scatterlist *sg,
  105. int nents, enum dma_data_direction direction, struct dma_attrs *attrs)
  106. {
  107. int r = swiotlb_map_sg_attrs(dev, sg, nents, direction, attrs);
  108. mb();
  109. return r;
  110. }
  111. static void octeon_dma_sync_single_for_device(struct device *dev,
  112. dma_addr_t dma_handle, size_t size, enum dma_data_direction direction)
  113. {
  114. swiotlb_sync_single_for_device(dev, dma_handle, size, direction);
  115. mb();
  116. }
  117. static void octeon_dma_sync_sg_for_device(struct device *dev,
  118. struct scatterlist *sg, int nelems, enum dma_data_direction direction)
  119. {
  120. swiotlb_sync_sg_for_device(dev, sg, nelems, direction);
  121. mb();
  122. }
  123. static void *octeon_dma_alloc_coherent(struct device *dev, size_t size,
  124. dma_addr_t *dma_handle, gfp_t gfp)
  125. {
  126. void *ret;
  127. if (dma_alloc_from_coherent(dev, size, dma_handle, &ret))
  128. return ret;
  129. /* ignore region specifiers */
  130. gfp &= ~(__GFP_DMA | __GFP_DMA32 | __GFP_HIGHMEM);
  131. #ifdef CONFIG_ZONE_DMA
  132. if (dev == NULL)
  133. gfp |= __GFP_DMA;
  134. else if (dev->coherent_dma_mask <= DMA_BIT_MASK(24))
  135. gfp |= __GFP_DMA;
  136. else
  137. #endif
  138. #ifdef CONFIG_ZONE_DMA32
  139. if (dev->coherent_dma_mask <= DMA_BIT_MASK(32))
  140. gfp |= __GFP_DMA32;
  141. else
  142. #endif
  143. ;
  144. /* Don't invoke OOM killer */
  145. gfp |= __GFP_NORETRY;
  146. ret = swiotlb_alloc_coherent(dev, size, dma_handle, gfp);
  147. mb();
  148. return ret;
  149. }
  150. static void octeon_dma_free_coherent(struct device *dev, size_t size,
  151. void *vaddr, dma_addr_t dma_handle)
  152. {
  153. int order = get_order(size);
  154. if (dma_release_from_coherent(dev, order, vaddr))
  155. return;
  156. swiotlb_free_coherent(dev, size, vaddr, dma_handle);
  157. }
  158. static dma_addr_t octeon_unity_phys_to_dma(struct device *dev, phys_addr_t paddr)
  159. {
  160. return paddr;
  161. }
  162. static phys_addr_t octeon_unity_dma_to_phys(struct device *dev, dma_addr_t daddr)
  163. {
  164. return daddr;
  165. }
  166. struct octeon_dma_map_ops {
  167. struct dma_map_ops dma_map_ops;
  168. dma_addr_t (*phys_to_dma)(struct device *dev, phys_addr_t paddr);
  169. phys_addr_t (*dma_to_phys)(struct device *dev, dma_addr_t daddr);
  170. };
  171. dma_addr_t phys_to_dma(struct device *dev, phys_addr_t paddr)
  172. {
  173. struct octeon_dma_map_ops *ops = container_of(get_dma_ops(dev),
  174. struct octeon_dma_map_ops,
  175. dma_map_ops);
  176. return ops->phys_to_dma(dev, paddr);
  177. }
  178. EXPORT_SYMBOL(phys_to_dma);
  179. phys_addr_t dma_to_phys(struct device *dev, dma_addr_t daddr)
  180. {
  181. struct octeon_dma_map_ops *ops = container_of(get_dma_ops(dev),
  182. struct octeon_dma_map_ops,
  183. dma_map_ops);
  184. return ops->dma_to_phys(dev, daddr);
  185. }
  186. EXPORT_SYMBOL(dma_to_phys);
  187. static struct octeon_dma_map_ops octeon_linear_dma_map_ops = {
  188. .dma_map_ops = {
  189. .alloc_coherent = octeon_dma_alloc_coherent,
  190. .free_coherent = octeon_dma_free_coherent,
  191. .map_page = octeon_dma_map_page,
  192. .unmap_page = swiotlb_unmap_page,
  193. .map_sg = octeon_dma_map_sg,
  194. .unmap_sg = swiotlb_unmap_sg_attrs,
  195. .sync_single_for_cpu = swiotlb_sync_single_for_cpu,
  196. .sync_single_for_device = octeon_dma_sync_single_for_device,
  197. .sync_sg_for_cpu = swiotlb_sync_sg_for_cpu,
  198. .sync_sg_for_device = octeon_dma_sync_sg_for_device,
  199. .mapping_error = swiotlb_dma_mapping_error,
  200. .dma_supported = swiotlb_dma_supported
  201. },
  202. .phys_to_dma = octeon_unity_phys_to_dma,
  203. .dma_to_phys = octeon_unity_dma_to_phys
  204. };
  205. char *octeon_swiotlb;
  206. void __init plat_swiotlb_setup(void)
  207. {
  208. int i;
  209. phys_t max_addr;
  210. phys_t addr_size;
  211. size_t swiotlbsize;
  212. unsigned long swiotlb_nslabs;
  213. max_addr = 0;
  214. addr_size = 0;
  215. for (i = 0 ; i < boot_mem_map.nr_map; i++) {
  216. struct boot_mem_map_entry *e = &boot_mem_map.map[i];
  217. if (e->type != BOOT_MEM_RAM)
  218. continue;
  219. /* These addresses map low for PCI. */
  220. if (e->addr > 0x410000000ull)
  221. continue;
  222. addr_size += e->size;
  223. if (max_addr < e->addr + e->size)
  224. max_addr = e->addr + e->size;
  225. }
  226. swiotlbsize = PAGE_SIZE;
  227. #ifdef CONFIG_PCI
  228. /*
  229. * For OCTEON_DMA_BAR_TYPE_SMALL, size the iotlb at 1/4 memory
  230. * size to a maximum of 64MB
  231. */
  232. if (OCTEON_IS_MODEL(OCTEON_CN31XX)
  233. || OCTEON_IS_MODEL(OCTEON_CN38XX_PASS2)) {
  234. swiotlbsize = addr_size / 4;
  235. if (swiotlbsize > 64 * (1<<20))
  236. swiotlbsize = 64 * (1<<20);
  237. } else if (max_addr > 0xf0000000ul) {
  238. /*
  239. * Otherwise only allocate a big iotlb if there is
  240. * memory past the BAR1 hole.
  241. */
  242. swiotlbsize = 64 * (1<<20);
  243. }
  244. #endif
  245. swiotlb_nslabs = swiotlbsize >> IO_TLB_SHIFT;
  246. swiotlb_nslabs = ALIGN(swiotlb_nslabs, IO_TLB_SEGSIZE);
  247. swiotlbsize = swiotlb_nslabs << IO_TLB_SHIFT;
  248. octeon_swiotlb = alloc_bootmem_low_pages(swiotlbsize);
  249. swiotlb_init_with_tbl(octeon_swiotlb, swiotlb_nslabs, 1);
  250. mips_dma_map_ops = &octeon_linear_dma_map_ops.dma_map_ops;
  251. }
  252. #ifdef CONFIG_PCI
  253. static struct octeon_dma_map_ops _octeon_pci_dma_map_ops = {
  254. .dma_map_ops = {
  255. .alloc_coherent = octeon_dma_alloc_coherent,
  256. .free_coherent = octeon_dma_free_coherent,
  257. .map_page = octeon_dma_map_page,
  258. .unmap_page = swiotlb_unmap_page,
  259. .map_sg = octeon_dma_map_sg,
  260. .unmap_sg = swiotlb_unmap_sg_attrs,
  261. .sync_single_for_cpu = swiotlb_sync_single_for_cpu,
  262. .sync_single_for_device = octeon_dma_sync_single_for_device,
  263. .sync_sg_for_cpu = swiotlb_sync_sg_for_cpu,
  264. .sync_sg_for_device = octeon_dma_sync_sg_for_device,
  265. .mapping_error = swiotlb_dma_mapping_error,
  266. .dma_supported = swiotlb_dma_supported
  267. },
  268. };
  269. struct dma_map_ops *octeon_pci_dma_map_ops;
  270. void __init octeon_pci_dma_init(void)
  271. {
  272. switch (octeon_dma_bar_type) {
  273. case OCTEON_DMA_BAR_TYPE_PCIE:
  274. _octeon_pci_dma_map_ops.phys_to_dma = octeon_gen1_phys_to_dma;
  275. _octeon_pci_dma_map_ops.dma_to_phys = octeon_gen1_dma_to_phys;
  276. break;
  277. case OCTEON_DMA_BAR_TYPE_BIG:
  278. _octeon_pci_dma_map_ops.phys_to_dma = octeon_big_phys_to_dma;
  279. _octeon_pci_dma_map_ops.dma_to_phys = octeon_big_dma_to_phys;
  280. break;
  281. case OCTEON_DMA_BAR_TYPE_SMALL:
  282. _octeon_pci_dma_map_ops.phys_to_dma = octeon_small_phys_to_dma;
  283. _octeon_pci_dma_map_ops.dma_to_phys = octeon_small_dma_to_phys;
  284. break;
  285. default:
  286. BUG();
  287. }
  288. octeon_pci_dma_map_ops = &_octeon_pci_dma_map_ops.dma_map_ops;
  289. }
  290. #endif /* CONFIG_PCI */