dma-octeon.c 9.9 KB

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