dma-octeon.c 9.1 KB

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