dma-octeon.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. #ifdef CONFIG_ZONE_DMA
  138. if (dev == NULL)
  139. gfp |= __GFP_DMA;
  140. else if (dev->coherent_dma_mask <= DMA_BIT_MASK(24))
  141. gfp |= __GFP_DMA;
  142. else
  143. #endif
  144. #ifdef CONFIG_ZONE_DMA32
  145. if (dev->coherent_dma_mask <= DMA_BIT_MASK(32))
  146. gfp |= __GFP_DMA32;
  147. else
  148. #endif
  149. ;
  150. /* Don't invoke OOM killer */
  151. gfp |= __GFP_NORETRY;
  152. ret = swiotlb_alloc_coherent(dev, size, dma_handle, gfp);
  153. mb();
  154. return ret;
  155. }
  156. static void octeon_dma_free_coherent(struct device *dev, size_t size,
  157. void *vaddr, dma_addr_t dma_handle, unsigned long attrs)
  158. {
  159. swiotlb_free_coherent(dev, size, vaddr, dma_handle);
  160. }
  161. static dma_addr_t octeon_unity_phys_to_dma(struct device *dev, phys_addr_t paddr)
  162. {
  163. return paddr;
  164. }
  165. static phys_addr_t octeon_unity_dma_to_phys(struct device *dev, dma_addr_t daddr)
  166. {
  167. return daddr;
  168. }
  169. struct octeon_dma_map_ops {
  170. struct dma_map_ops dma_map_ops;
  171. dma_addr_t (*phys_to_dma)(struct device *dev, phys_addr_t paddr);
  172. phys_addr_t (*dma_to_phys)(struct device *dev, dma_addr_t daddr);
  173. };
  174. dma_addr_t phys_to_dma(struct device *dev, phys_addr_t paddr)
  175. {
  176. struct octeon_dma_map_ops *ops = container_of(get_dma_ops(dev),
  177. struct octeon_dma_map_ops,
  178. dma_map_ops);
  179. return ops->phys_to_dma(dev, paddr);
  180. }
  181. EXPORT_SYMBOL(phys_to_dma);
  182. phys_addr_t dma_to_phys(struct device *dev, dma_addr_t daddr)
  183. {
  184. struct octeon_dma_map_ops *ops = container_of(get_dma_ops(dev),
  185. struct octeon_dma_map_ops,
  186. dma_map_ops);
  187. return ops->dma_to_phys(dev, daddr);
  188. }
  189. EXPORT_SYMBOL(dma_to_phys);
  190. static struct octeon_dma_map_ops octeon_linear_dma_map_ops = {
  191. .dma_map_ops = {
  192. .alloc = octeon_dma_alloc_coherent,
  193. .free = octeon_dma_free_coherent,
  194. .map_page = octeon_dma_map_page,
  195. .unmap_page = swiotlb_unmap_page,
  196. .map_sg = octeon_dma_map_sg,
  197. .unmap_sg = swiotlb_unmap_sg_attrs,
  198. .sync_single_for_cpu = swiotlb_sync_single_for_cpu,
  199. .sync_single_for_device = octeon_dma_sync_single_for_device,
  200. .sync_sg_for_cpu = swiotlb_sync_sg_for_cpu,
  201. .sync_sg_for_device = octeon_dma_sync_sg_for_device,
  202. .mapping_error = swiotlb_dma_mapping_error,
  203. .dma_supported = swiotlb_dma_supported
  204. },
  205. .phys_to_dma = octeon_unity_phys_to_dma,
  206. .dma_to_phys = octeon_unity_dma_to_phys
  207. };
  208. char *octeon_swiotlb;
  209. void __init plat_swiotlb_setup(void)
  210. {
  211. int i;
  212. phys_addr_t max_addr;
  213. phys_addr_t addr_size;
  214. size_t swiotlbsize;
  215. unsigned long swiotlb_nslabs;
  216. max_addr = 0;
  217. addr_size = 0;
  218. for (i = 0 ; i < boot_mem_map.nr_map; i++) {
  219. struct boot_mem_map_entry *e = &boot_mem_map.map[i];
  220. if (e->type != BOOT_MEM_RAM && e->type != BOOT_MEM_INIT_RAM)
  221. continue;
  222. /* These addresses map low for PCI. */
  223. if (e->addr > 0x410000000ull && !OCTEON_IS_OCTEON2())
  224. continue;
  225. addr_size += e->size;
  226. if (max_addr < e->addr + e->size)
  227. max_addr = e->addr + e->size;
  228. }
  229. swiotlbsize = PAGE_SIZE;
  230. #ifdef CONFIG_PCI
  231. /*
  232. * For OCTEON_DMA_BAR_TYPE_SMALL, size the iotlb at 1/4 memory
  233. * size to a maximum of 64MB
  234. */
  235. if (OCTEON_IS_MODEL(OCTEON_CN31XX)
  236. || OCTEON_IS_MODEL(OCTEON_CN38XX_PASS2)) {
  237. swiotlbsize = addr_size / 4;
  238. if (swiotlbsize > 64 * (1<<20))
  239. swiotlbsize = 64 * (1<<20);
  240. } else if (max_addr > 0xf0000000ul) {
  241. /*
  242. * Otherwise only allocate a big iotlb if there is
  243. * memory past the BAR1 hole.
  244. */
  245. swiotlbsize = 64 * (1<<20);
  246. }
  247. #endif
  248. #ifdef CONFIG_USB_OHCI_HCD_PLATFORM
  249. /* OCTEON II ohci is only 32-bit. */
  250. if (OCTEON_IS_OCTEON2() && max_addr >= 0x100000000ul)
  251. swiotlbsize = 64 * (1<<20);
  252. #endif
  253. swiotlb_nslabs = swiotlbsize >> IO_TLB_SHIFT;
  254. swiotlb_nslabs = ALIGN(swiotlb_nslabs, IO_TLB_SEGSIZE);
  255. swiotlbsize = swiotlb_nslabs << IO_TLB_SHIFT;
  256. octeon_swiotlb = alloc_bootmem_low_pages(swiotlbsize);
  257. if (swiotlb_init_with_tbl(octeon_swiotlb, swiotlb_nslabs, 1) == -ENOMEM)
  258. panic("Cannot allocate SWIOTLB buffer");
  259. mips_dma_map_ops = &octeon_linear_dma_map_ops.dma_map_ops;
  260. }
  261. #ifdef CONFIG_PCI
  262. static struct octeon_dma_map_ops _octeon_pci_dma_map_ops = {
  263. .dma_map_ops = {
  264. .alloc = octeon_dma_alloc_coherent,
  265. .free = octeon_dma_free_coherent,
  266. .map_page = octeon_dma_map_page,
  267. .unmap_page = swiotlb_unmap_page,
  268. .map_sg = octeon_dma_map_sg,
  269. .unmap_sg = swiotlb_unmap_sg_attrs,
  270. .sync_single_for_cpu = swiotlb_sync_single_for_cpu,
  271. .sync_single_for_device = octeon_dma_sync_single_for_device,
  272. .sync_sg_for_cpu = swiotlb_sync_sg_for_cpu,
  273. .sync_sg_for_device = octeon_dma_sync_sg_for_device,
  274. .mapping_error = swiotlb_dma_mapping_error,
  275. .dma_supported = swiotlb_dma_supported
  276. },
  277. };
  278. struct dma_map_ops *octeon_pci_dma_map_ops;
  279. void __init octeon_pci_dma_init(void)
  280. {
  281. switch (octeon_dma_bar_type) {
  282. case OCTEON_DMA_BAR_TYPE_PCIE2:
  283. _octeon_pci_dma_map_ops.phys_to_dma = octeon_gen2_phys_to_dma;
  284. _octeon_pci_dma_map_ops.dma_to_phys = octeon_gen2_dma_to_phys;
  285. break;
  286. case OCTEON_DMA_BAR_TYPE_PCIE:
  287. _octeon_pci_dma_map_ops.phys_to_dma = octeon_gen1_phys_to_dma;
  288. _octeon_pci_dma_map_ops.dma_to_phys = octeon_gen1_dma_to_phys;
  289. break;
  290. case OCTEON_DMA_BAR_TYPE_BIG:
  291. _octeon_pci_dma_map_ops.phys_to_dma = octeon_big_phys_to_dma;
  292. _octeon_pci_dma_map_ops.dma_to_phys = octeon_big_dma_to_phys;
  293. break;
  294. case OCTEON_DMA_BAR_TYPE_SMALL:
  295. _octeon_pci_dma_map_ops.phys_to_dma = octeon_small_phys_to_dma;
  296. _octeon_pci_dma_map_ops.dma_to_phys = octeon_small_dma_to_phys;
  297. break;
  298. default:
  299. BUG();
  300. }
  301. octeon_pci_dma_map_ops = &_octeon_pci_dma_map_ops.dma_map_ops;
  302. }
  303. #endif /* CONFIG_PCI */