dma-octeon.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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_gen2_phys_to_dma(struct device *dev, phys_addr_t paddr)
  55. {
  56. return octeon_hole_phys_to_dma(paddr);
  57. }
  58. static phys_addr_t octeon_gen2_dma_to_phys(struct device *dev, dma_addr_t daddr)
  59. {
  60. return octeon_hole_dma_to_phys(daddr);
  61. }
  62. static dma_addr_t octeon_big_phys_to_dma(struct device *dev, phys_addr_t paddr)
  63. {
  64. if (paddr >= 0x410000000ull && paddr < 0x420000000ull)
  65. paddr -= 0x400000000ull;
  66. /* Anything in the BAR1 hole or above goes via BAR2 */
  67. if (paddr >= 0xf0000000ull)
  68. paddr = OCTEON_BAR2_PCI_ADDRESS + paddr;
  69. return paddr;
  70. }
  71. static phys_addr_t octeon_big_dma_to_phys(struct device *dev, dma_addr_t daddr)
  72. {
  73. if (daddr >= OCTEON_BAR2_PCI_ADDRESS)
  74. daddr -= OCTEON_BAR2_PCI_ADDRESS;
  75. if (daddr >= 0x10000000ull && daddr < 0x20000000ull)
  76. daddr += 0x400000000ull;
  77. return daddr;
  78. }
  79. static dma_addr_t octeon_small_phys_to_dma(struct device *dev,
  80. phys_addr_t paddr)
  81. {
  82. if (paddr >= 0x410000000ull && paddr < 0x420000000ull)
  83. paddr -= 0x400000000ull;
  84. /* Anything not in the BAR1 range goes via BAR2 */
  85. if (paddr >= octeon_bar1_pci_phys && paddr < octeon_bar1_pci_phys + 0x8000000ull)
  86. paddr = paddr - octeon_bar1_pci_phys;
  87. else
  88. paddr = OCTEON_BAR2_PCI_ADDRESS + paddr;
  89. return paddr;
  90. }
  91. static phys_addr_t octeon_small_dma_to_phys(struct device *dev,
  92. dma_addr_t daddr)
  93. {
  94. if (daddr >= OCTEON_BAR2_PCI_ADDRESS)
  95. daddr -= OCTEON_BAR2_PCI_ADDRESS;
  96. else
  97. daddr += octeon_bar1_pci_phys;
  98. if (daddr >= 0x10000000ull && daddr < 0x20000000ull)
  99. daddr += 0x400000000ull;
  100. return daddr;
  101. }
  102. #endif /* CONFIG_PCI */
  103. static dma_addr_t octeon_dma_map_page(struct device *dev, struct page *page,
  104. unsigned long offset, size_t size, enum dma_data_direction direction,
  105. unsigned long attrs)
  106. {
  107. dma_addr_t daddr = swiotlb_map_page(dev, page, offset, size,
  108. direction, attrs);
  109. mb();
  110. return daddr;
  111. }
  112. static int octeon_dma_map_sg(struct device *dev, struct scatterlist *sg,
  113. int nents, enum dma_data_direction direction, unsigned long attrs)
  114. {
  115. int r = swiotlb_map_sg_attrs(dev, sg, nents, direction, attrs);
  116. mb();
  117. return r;
  118. }
  119. static void octeon_dma_sync_single_for_device(struct device *dev,
  120. dma_addr_t dma_handle, size_t size, enum dma_data_direction direction)
  121. {
  122. swiotlb_sync_single_for_device(dev, dma_handle, size, direction);
  123. mb();
  124. }
  125. static void octeon_dma_sync_sg_for_device(struct device *dev,
  126. struct scatterlist *sg, int nelems, enum dma_data_direction direction)
  127. {
  128. swiotlb_sync_sg_for_device(dev, sg, nelems, direction);
  129. mb();
  130. }
  131. static void *octeon_dma_alloc_coherent(struct device *dev, size_t size,
  132. dma_addr_t *dma_handle, gfp_t gfp, unsigned long attrs)
  133. {
  134. void *ret;
  135. /* ignore region specifiers */
  136. gfp &= ~(__GFP_DMA | __GFP_DMA32 | __GFP_HIGHMEM);
  137. if (IS_ENABLED(CONFIG_ZONE_DMA) && dev == NULL)
  138. gfp |= __GFP_DMA;
  139. else if (IS_ENABLED(CONFIG_ZONE_DMA) &&
  140. dev->coherent_dma_mask <= DMA_BIT_MASK(24))
  141. gfp |= __GFP_DMA;
  142. else if (IS_ENABLED(CONFIG_ZONE_DMA32) &&
  143. dev->coherent_dma_mask <= DMA_BIT_MASK(32))
  144. gfp |= __GFP_DMA32;
  145. /* Don't invoke OOM killer */
  146. gfp |= __GFP_NORETRY;
  147. ret = swiotlb_alloc_coherent(dev, size, dma_handle, gfp);
  148. mb();
  149. return ret;
  150. }
  151. static void octeon_dma_free_coherent(struct device *dev, size_t size,
  152. void *vaddr, dma_addr_t dma_handle, unsigned long attrs)
  153. {
  154. swiotlb_free_coherent(dev, size, vaddr, dma_handle);
  155. }
  156. static dma_addr_t octeon_unity_phys_to_dma(struct device *dev, phys_addr_t paddr)
  157. {
  158. return paddr;
  159. }
  160. static phys_addr_t octeon_unity_dma_to_phys(struct device *dev, dma_addr_t daddr)
  161. {
  162. return daddr;
  163. }
  164. struct octeon_dma_map_ops {
  165. const struct dma_map_ops dma_map_ops;
  166. dma_addr_t (*phys_to_dma)(struct device *dev, phys_addr_t paddr);
  167. phys_addr_t (*dma_to_phys)(struct device *dev, dma_addr_t daddr);
  168. };
  169. dma_addr_t phys_to_dma(struct device *dev, phys_addr_t paddr)
  170. {
  171. struct octeon_dma_map_ops *ops = container_of(get_dma_ops(dev),
  172. struct octeon_dma_map_ops,
  173. dma_map_ops);
  174. return ops->phys_to_dma(dev, paddr);
  175. }
  176. EXPORT_SYMBOL(phys_to_dma);
  177. phys_addr_t dma_to_phys(struct device *dev, dma_addr_t daddr)
  178. {
  179. struct octeon_dma_map_ops *ops = container_of(get_dma_ops(dev),
  180. struct octeon_dma_map_ops,
  181. dma_map_ops);
  182. return ops->dma_to_phys(dev, daddr);
  183. }
  184. EXPORT_SYMBOL(dma_to_phys);
  185. static struct octeon_dma_map_ops octeon_linear_dma_map_ops = {
  186. .dma_map_ops = {
  187. .alloc = octeon_dma_alloc_coherent,
  188. .free = octeon_dma_free_coherent,
  189. .map_page = octeon_dma_map_page,
  190. .unmap_page = swiotlb_unmap_page,
  191. .map_sg = octeon_dma_map_sg,
  192. .unmap_sg = swiotlb_unmap_sg_attrs,
  193. .sync_single_for_cpu = swiotlb_sync_single_for_cpu,
  194. .sync_single_for_device = octeon_dma_sync_single_for_device,
  195. .sync_sg_for_cpu = swiotlb_sync_sg_for_cpu,
  196. .sync_sg_for_device = octeon_dma_sync_sg_for_device,
  197. .mapping_error = swiotlb_dma_mapping_error,
  198. .dma_supported = swiotlb_dma_supported
  199. },
  200. .phys_to_dma = octeon_unity_phys_to_dma,
  201. .dma_to_phys = octeon_unity_dma_to_phys
  202. };
  203. char *octeon_swiotlb;
  204. void __init plat_swiotlb_setup(void)
  205. {
  206. int i;
  207. phys_addr_t max_addr;
  208. phys_addr_t addr_size;
  209. size_t swiotlbsize;
  210. unsigned long swiotlb_nslabs;
  211. max_addr = 0;
  212. addr_size = 0;
  213. for (i = 0 ; i < boot_mem_map.nr_map; i++) {
  214. struct boot_mem_map_entry *e = &boot_mem_map.map[i];
  215. if (e->type != BOOT_MEM_RAM && e->type != BOOT_MEM_INIT_RAM)
  216. continue;
  217. /* These addresses map low for PCI. */
  218. if (e->addr > 0x410000000ull && !OCTEON_IS_OCTEON2())
  219. continue;
  220. addr_size += e->size;
  221. if (max_addr < e->addr + e->size)
  222. max_addr = e->addr + e->size;
  223. }
  224. swiotlbsize = PAGE_SIZE;
  225. #ifdef CONFIG_PCI
  226. /*
  227. * For OCTEON_DMA_BAR_TYPE_SMALL, size the iotlb at 1/4 memory
  228. * size to a maximum of 64MB
  229. */
  230. if (OCTEON_IS_MODEL(OCTEON_CN31XX)
  231. || OCTEON_IS_MODEL(OCTEON_CN38XX_PASS2)) {
  232. swiotlbsize = addr_size / 4;
  233. if (swiotlbsize > 64 * (1<<20))
  234. swiotlbsize = 64 * (1<<20);
  235. } else if (max_addr > 0xf0000000ul) {
  236. /*
  237. * Otherwise only allocate a big iotlb if there is
  238. * memory past the BAR1 hole.
  239. */
  240. swiotlbsize = 64 * (1<<20);
  241. }
  242. #endif
  243. #ifdef CONFIG_USB_OHCI_HCD_PLATFORM
  244. /* OCTEON II ohci is only 32-bit. */
  245. if (OCTEON_IS_OCTEON2() && max_addr >= 0x100000000ul)
  246. swiotlbsize = 64 * (1<<20);
  247. #endif
  248. swiotlb_nslabs = swiotlbsize >> IO_TLB_SHIFT;
  249. swiotlb_nslabs = ALIGN(swiotlb_nslabs, IO_TLB_SEGSIZE);
  250. swiotlbsize = swiotlb_nslabs << IO_TLB_SHIFT;
  251. octeon_swiotlb = alloc_bootmem_low_pages(swiotlbsize);
  252. if (swiotlb_init_with_tbl(octeon_swiotlb, swiotlb_nslabs, 1) == -ENOMEM)
  253. panic("Cannot allocate SWIOTLB buffer");
  254. mips_dma_map_ops = &octeon_linear_dma_map_ops.dma_map_ops;
  255. }
  256. #ifdef CONFIG_PCI
  257. static struct octeon_dma_map_ops _octeon_pci_dma_map_ops = {
  258. .dma_map_ops = {
  259. .alloc = octeon_dma_alloc_coherent,
  260. .free = octeon_dma_free_coherent,
  261. .map_page = octeon_dma_map_page,
  262. .unmap_page = swiotlb_unmap_page,
  263. .map_sg = octeon_dma_map_sg,
  264. .unmap_sg = swiotlb_unmap_sg_attrs,
  265. .sync_single_for_cpu = swiotlb_sync_single_for_cpu,
  266. .sync_single_for_device = octeon_dma_sync_single_for_device,
  267. .sync_sg_for_cpu = swiotlb_sync_sg_for_cpu,
  268. .sync_sg_for_device = octeon_dma_sync_sg_for_device,
  269. .mapping_error = swiotlb_dma_mapping_error,
  270. .dma_supported = swiotlb_dma_supported
  271. },
  272. };
  273. const struct dma_map_ops *octeon_pci_dma_map_ops;
  274. void __init octeon_pci_dma_init(void)
  275. {
  276. switch (octeon_dma_bar_type) {
  277. case OCTEON_DMA_BAR_TYPE_PCIE2:
  278. _octeon_pci_dma_map_ops.phys_to_dma = octeon_gen2_phys_to_dma;
  279. _octeon_pci_dma_map_ops.dma_to_phys = octeon_gen2_dma_to_phys;
  280. break;
  281. case OCTEON_DMA_BAR_TYPE_PCIE:
  282. _octeon_pci_dma_map_ops.phys_to_dma = octeon_gen1_phys_to_dma;
  283. _octeon_pci_dma_map_ops.dma_to_phys = octeon_gen1_dma_to_phys;
  284. break;
  285. case OCTEON_DMA_BAR_TYPE_BIG:
  286. _octeon_pci_dma_map_ops.phys_to_dma = octeon_big_phys_to_dma;
  287. _octeon_pci_dma_map_ops.dma_to_phys = octeon_big_dma_to_phys;
  288. break;
  289. case OCTEON_DMA_BAR_TYPE_SMALL:
  290. _octeon_pci_dma_map_ops.phys_to_dma = octeon_small_phys_to_dma;
  291. _octeon_pci_dma_map_ops.dma_to_phys = octeon_small_dma_to_phys;
  292. break;
  293. default:
  294. BUG();
  295. }
  296. octeon_pci_dma_map_ops = &_octeon_pci_dma_map_ops.dma_map_ops;
  297. }
  298. #endif /* CONFIG_PCI */