iommu.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * iommu.c: IOMMU specific routines for memory management.
  4. *
  5. * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
  6. * Copyright (C) 1995,2002 Pete Zaitcev (zaitcev@yahoo.com)
  7. * Copyright (C) 1996 Eddie C. Dost (ecd@skynet.be)
  8. * Copyright (C) 1997,1998 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/init.h>
  12. #include <linux/mm.h>
  13. #include <linux/slab.h>
  14. #include <linux/dma-map-ops.h>
  15. #include <linux/of.h>
  16. #include <linux/of_platform.h>
  17. #include <linux/platform_device.h>
  18. #include <asm/io.h>
  19. #include <asm/mxcc.h>
  20. #include <asm/mbus.h>
  21. #include <asm/cacheflush.h>
  22. #include <asm/tlbflush.h>
  23. #include <asm/bitext.h>
  24. #include <asm/iommu.h>
  25. #include <asm/dma.h>
  26. #include "mm_32.h"
  27. /*
  28. * This can be sized dynamically, but we will do this
  29. * only when we have a guidance about actual I/O pressures.
  30. */
  31. #define IOMMU_RNGE IOMMU_RNGE_256MB
  32. #define IOMMU_START 0xF0000000
  33. #define IOMMU_WINSIZE (256*1024*1024U)
  34. #define IOMMU_NPTES (IOMMU_WINSIZE/PAGE_SIZE) /* 64K PTEs, 256KB */
  35. #define IOMMU_ORDER 6 /* 4096 * (1<<6) */
  36. static int viking_flush;
  37. /* viking.S */
  38. extern void viking_flush_page(unsigned long page);
  39. extern void viking_mxcc_flush_page(unsigned long page);
  40. /*
  41. * Values precomputed according to CPU type.
  42. */
  43. static unsigned int ioperm_noc; /* Consistent mapping iopte flags */
  44. static pgprot_t dvma_prot; /* Consistent mapping pte flags */
  45. #define IOPERM (IOPTE_CACHE | IOPTE_WRITE | IOPTE_VALID)
  46. #define MKIOPTE(pfn, perm) (((((pfn)<<8) & IOPTE_PAGE) | (perm)) & ~IOPTE_WAZ)
  47. static const struct dma_map_ops sbus_iommu_dma_gflush_ops;
  48. static const struct dma_map_ops sbus_iommu_dma_pflush_ops;
  49. static void __init sbus_iommu_init(struct platform_device *op)
  50. {
  51. struct iommu_struct *iommu;
  52. unsigned int impl, vers;
  53. unsigned long *bitmap;
  54. unsigned long control;
  55. unsigned long base;
  56. unsigned long tmp;
  57. iommu = kmalloc(sizeof(struct iommu_struct), GFP_KERNEL);
  58. if (!iommu) {
  59. prom_printf("Unable to allocate iommu structure\n");
  60. prom_halt();
  61. }
  62. iommu->regs = of_ioremap(&op->resource[0], 0, PAGE_SIZE * 3,
  63. "iommu_regs");
  64. if (!iommu->regs) {
  65. prom_printf("Cannot map IOMMU registers\n");
  66. prom_halt();
  67. }
  68. control = sbus_readl(&iommu->regs->control);
  69. impl = (control & IOMMU_CTRL_IMPL) >> 28;
  70. vers = (control & IOMMU_CTRL_VERS) >> 24;
  71. control &= ~(IOMMU_CTRL_RNGE);
  72. control |= (IOMMU_RNGE_256MB | IOMMU_CTRL_ENAB);
  73. sbus_writel(control, &iommu->regs->control);
  74. iommu_invalidate(iommu->regs);
  75. iommu->start = IOMMU_START;
  76. iommu->end = 0xffffffff;
  77. /* Allocate IOMMU page table */
  78. /* Stupid alignment constraints give me a headache.
  79. We need 256K or 512K or 1M or 2M area aligned to
  80. its size and current gfp will fortunately give
  81. it to us. */
  82. tmp = __get_free_pages(GFP_KERNEL, IOMMU_ORDER);
  83. if (!tmp) {
  84. prom_printf("Unable to allocate iommu table [0x%lx]\n",
  85. IOMMU_NPTES * sizeof(iopte_t));
  86. prom_halt();
  87. }
  88. iommu->page_table = (iopte_t *)tmp;
  89. /* Initialize new table. */
  90. memset(iommu->page_table, 0, IOMMU_NPTES*sizeof(iopte_t));
  91. flush_cache_all();
  92. flush_tlb_all();
  93. base = __pa((unsigned long)iommu->page_table) >> 4;
  94. sbus_writel(base, &iommu->regs->base);
  95. iommu_invalidate(iommu->regs);
  96. bitmap = kmalloc(IOMMU_NPTES>>3, GFP_KERNEL);
  97. if (!bitmap) {
  98. prom_printf("Unable to allocate iommu bitmap [%d]\n",
  99. (int)(IOMMU_NPTES>>3));
  100. prom_halt();
  101. }
  102. bit_map_init(&iommu->usemap, bitmap, IOMMU_NPTES);
  103. /* To be coherent on HyperSparc, the page color of DVMA
  104. * and physical addresses must match.
  105. */
  106. if (srmmu_modtype == HyperSparc)
  107. iommu->usemap.num_colors = vac_cache_size >> PAGE_SHIFT;
  108. else
  109. iommu->usemap.num_colors = 1;
  110. printk(KERN_INFO "IOMMU: impl %d vers %d table 0x%p[%d B] map [%d b]\n",
  111. impl, vers, iommu->page_table,
  112. (int)(IOMMU_NPTES*sizeof(iopte_t)), (int)IOMMU_NPTES);
  113. op->dev.archdata.iommu = iommu;
  114. if (flush_page_for_dma_global)
  115. op->dev.dma_ops = &sbus_iommu_dma_gflush_ops;
  116. else
  117. op->dev.dma_ops = &sbus_iommu_dma_pflush_ops;
  118. }
  119. static int __init iommu_init(void)
  120. {
  121. struct device_node *dp;
  122. for_each_node_by_name(dp, "iommu") {
  123. struct platform_device *op = of_find_device_by_node(dp);
  124. sbus_iommu_init(op);
  125. of_propagate_archdata(op);
  126. }
  127. return 0;
  128. }
  129. subsys_initcall(iommu_init);
  130. /* Flush the iotlb entries to ram. */
  131. /* This could be better if we didn't have to flush whole pages. */
  132. static void iommu_flush_iotlb(iopte_t *iopte, unsigned int niopte)
  133. {
  134. unsigned long start;
  135. unsigned long end;
  136. start = (unsigned long)iopte;
  137. end = PAGE_ALIGN(start + niopte*sizeof(iopte_t));
  138. start &= PAGE_MASK;
  139. if (viking_mxcc_present) {
  140. while(start < end) {
  141. viking_mxcc_flush_page(start);
  142. start += PAGE_SIZE;
  143. }
  144. } else if (viking_flush) {
  145. while(start < end) {
  146. viking_flush_page(start);
  147. start += PAGE_SIZE;
  148. }
  149. } else {
  150. while(start < end) {
  151. __flush_page_to_ram(start);
  152. start += PAGE_SIZE;
  153. }
  154. }
  155. }
  156. static dma_addr_t __sbus_iommu_map_page(struct device *dev, struct page *page,
  157. unsigned long offset, size_t len, bool per_page_flush)
  158. {
  159. struct iommu_struct *iommu = dev->archdata.iommu;
  160. phys_addr_t paddr = page_to_phys(page) + offset;
  161. unsigned long off = paddr & ~PAGE_MASK;
  162. unsigned long npages = (off + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  163. unsigned long pfn = __phys_to_pfn(paddr);
  164. unsigned int busa, busa0;
  165. iopte_t *iopte, *iopte0;
  166. int ioptex, i;
  167. /* XXX So what is maxphys for us and how do drivers know it? */
  168. if (!len || len > 256 * 1024)
  169. return DMA_MAPPING_ERROR;
  170. /*
  171. * We expect unmapped highmem pages to be not in the cache.
  172. * XXX Is this a good assumption?
  173. * XXX What if someone else unmaps it here and races us?
  174. */
  175. if (per_page_flush && !PageHighMem(page)) {
  176. unsigned long vaddr, p;
  177. vaddr = (unsigned long)page_address(page) + offset;
  178. for (p = vaddr & PAGE_MASK; p < vaddr + len; p += PAGE_SIZE)
  179. flush_page_for_dma(p);
  180. }
  181. /* page color = pfn of page */
  182. ioptex = bit_map_string_get(&iommu->usemap, npages, pfn);
  183. if (ioptex < 0)
  184. panic("iommu out");
  185. busa0 = iommu->start + (ioptex << PAGE_SHIFT);
  186. iopte0 = &iommu->page_table[ioptex];
  187. busa = busa0;
  188. iopte = iopte0;
  189. for (i = 0; i < npages; i++) {
  190. iopte_val(*iopte) = MKIOPTE(pfn, IOPERM);
  191. iommu_invalidate_page(iommu->regs, busa);
  192. busa += PAGE_SIZE;
  193. iopte++;
  194. pfn++;
  195. }
  196. iommu_flush_iotlb(iopte0, npages);
  197. return busa0 + off;
  198. }
  199. static dma_addr_t sbus_iommu_map_page_gflush(struct device *dev,
  200. struct page *page, unsigned long offset, size_t len,
  201. enum dma_data_direction dir, unsigned long attrs)
  202. {
  203. flush_page_for_dma(0);
  204. return __sbus_iommu_map_page(dev, page, offset, len, false);
  205. }
  206. static dma_addr_t sbus_iommu_map_page_pflush(struct device *dev,
  207. struct page *page, unsigned long offset, size_t len,
  208. enum dma_data_direction dir, unsigned long attrs)
  209. {
  210. return __sbus_iommu_map_page(dev, page, offset, len, true);
  211. }
  212. static int __sbus_iommu_map_sg(struct device *dev, struct scatterlist *sgl,
  213. int nents, enum dma_data_direction dir, unsigned long attrs,
  214. bool per_page_flush)
  215. {
  216. struct scatterlist *sg;
  217. int j;
  218. for_each_sg(sgl, sg, nents, j) {
  219. sg->dma_address =__sbus_iommu_map_page(dev, sg_page(sg),
  220. sg->offset, sg->length, per_page_flush);
  221. if (sg->dma_address == DMA_MAPPING_ERROR)
  222. return -EIO;
  223. sg->dma_length = sg->length;
  224. }
  225. return nents;
  226. }
  227. static int sbus_iommu_map_sg_gflush(struct device *dev, struct scatterlist *sgl,
  228. int nents, enum dma_data_direction dir, unsigned long attrs)
  229. {
  230. flush_page_for_dma(0);
  231. return __sbus_iommu_map_sg(dev, sgl, nents, dir, attrs, false);
  232. }
  233. static int sbus_iommu_map_sg_pflush(struct device *dev, struct scatterlist *sgl,
  234. int nents, enum dma_data_direction dir, unsigned long attrs)
  235. {
  236. return __sbus_iommu_map_sg(dev, sgl, nents, dir, attrs, true);
  237. }
  238. static void sbus_iommu_unmap_page(struct device *dev, dma_addr_t dma_addr,
  239. size_t len, enum dma_data_direction dir, unsigned long attrs)
  240. {
  241. struct iommu_struct *iommu = dev->archdata.iommu;
  242. unsigned int busa = dma_addr & PAGE_MASK;
  243. unsigned long off = dma_addr & ~PAGE_MASK;
  244. unsigned int npages = (off + len + PAGE_SIZE-1) >> PAGE_SHIFT;
  245. unsigned int ioptex = (busa - iommu->start) >> PAGE_SHIFT;
  246. unsigned int i;
  247. BUG_ON(busa < iommu->start);
  248. for (i = 0; i < npages; i++) {
  249. iopte_val(iommu->page_table[ioptex + i]) = 0;
  250. iommu_invalidate_page(iommu->regs, busa);
  251. busa += PAGE_SIZE;
  252. }
  253. bit_map_clear(&iommu->usemap, ioptex, npages);
  254. }
  255. static void sbus_iommu_unmap_sg(struct device *dev, struct scatterlist *sgl,
  256. int nents, enum dma_data_direction dir, unsigned long attrs)
  257. {
  258. struct scatterlist *sg;
  259. int i;
  260. for_each_sg(sgl, sg, nents, i) {
  261. sbus_iommu_unmap_page(dev, sg->dma_address, sg->length, dir,
  262. attrs);
  263. sg->dma_address = 0x21212121;
  264. }
  265. }
  266. #ifdef CONFIG_SBUS
  267. static void *sbus_iommu_alloc(struct device *dev, size_t len,
  268. dma_addr_t *dma_handle, gfp_t gfp, unsigned long attrs)
  269. {
  270. struct iommu_struct *iommu = dev->archdata.iommu;
  271. unsigned long va, addr, page, end, ret;
  272. iopte_t *iopte = iommu->page_table;
  273. iopte_t *first;
  274. int ioptex;
  275. /* XXX So what is maxphys for us and how do drivers know it? */
  276. if (!len || len > 256 * 1024)
  277. return NULL;
  278. len = PAGE_ALIGN(len);
  279. va = __get_free_pages(gfp | __GFP_ZERO, get_order(len));
  280. if (va == 0)
  281. return NULL;
  282. addr = ret = sparc_dma_alloc_resource(dev, len);
  283. if (!addr)
  284. goto out_free_pages;
  285. BUG_ON((va & ~PAGE_MASK) != 0);
  286. BUG_ON((addr & ~PAGE_MASK) != 0);
  287. BUG_ON((len & ~PAGE_MASK) != 0);
  288. /* page color = physical address */
  289. ioptex = bit_map_string_get(&iommu->usemap, len >> PAGE_SHIFT,
  290. addr >> PAGE_SHIFT);
  291. if (ioptex < 0)
  292. panic("iommu out");
  293. iopte += ioptex;
  294. first = iopte;
  295. end = addr + len;
  296. while(addr < end) {
  297. page = va;
  298. {
  299. pmd_t *pmdp;
  300. pte_t *ptep;
  301. if (viking_mxcc_present)
  302. viking_mxcc_flush_page(page);
  303. else if (viking_flush)
  304. viking_flush_page(page);
  305. else
  306. __flush_page_to_ram(page);
  307. pmdp = pmd_off_k(addr);
  308. ptep = pte_offset_kernel(pmdp, addr);
  309. set_pte(ptep, mk_pte(virt_to_page(page), dvma_prot));
  310. }
  311. iopte_val(*iopte++) =
  312. MKIOPTE(page_to_pfn(virt_to_page(page)), ioperm_noc);
  313. addr += PAGE_SIZE;
  314. va += PAGE_SIZE;
  315. }
  316. /* P3: why do we need this?
  317. *
  318. * DAVEM: Because there are several aspects, none of which
  319. * are handled by a single interface. Some cpus are
  320. * completely not I/O DMA coherent, and some have
  321. * virtually indexed caches. The driver DMA flushing
  322. * methods handle the former case, but here during
  323. * IOMMU page table modifications, and usage of non-cacheable
  324. * cpu mappings of pages potentially in the cpu caches, we have
  325. * to handle the latter case as well.
  326. */
  327. flush_cache_all();
  328. iommu_flush_iotlb(first, len >> PAGE_SHIFT);
  329. flush_tlb_all();
  330. iommu_invalidate(iommu->regs);
  331. *dma_handle = iommu->start + (ioptex << PAGE_SHIFT);
  332. return (void *)ret;
  333. out_free_pages:
  334. free_pages(va, get_order(len));
  335. return NULL;
  336. }
  337. static void sbus_iommu_free(struct device *dev, size_t len, void *cpu_addr,
  338. dma_addr_t busa, unsigned long attrs)
  339. {
  340. struct iommu_struct *iommu = dev->archdata.iommu;
  341. iopte_t *iopte = iommu->page_table;
  342. struct page *page = virt_to_page(cpu_addr);
  343. int ioptex = (busa - iommu->start) >> PAGE_SHIFT;
  344. unsigned long end;
  345. if (!sparc_dma_free_resource(cpu_addr, len))
  346. return;
  347. BUG_ON((busa & ~PAGE_MASK) != 0);
  348. BUG_ON((len & ~PAGE_MASK) != 0);
  349. iopte += ioptex;
  350. end = busa + len;
  351. while (busa < end) {
  352. iopte_val(*iopte++) = 0;
  353. busa += PAGE_SIZE;
  354. }
  355. flush_tlb_all();
  356. iommu_invalidate(iommu->regs);
  357. bit_map_clear(&iommu->usemap, ioptex, len >> PAGE_SHIFT);
  358. __free_pages(page, get_order(len));
  359. }
  360. #endif
  361. static const struct dma_map_ops sbus_iommu_dma_gflush_ops = {
  362. #ifdef CONFIG_SBUS
  363. .alloc = sbus_iommu_alloc,
  364. .free = sbus_iommu_free,
  365. #endif
  366. .map_page = sbus_iommu_map_page_gflush,
  367. .unmap_page = sbus_iommu_unmap_page,
  368. .map_sg = sbus_iommu_map_sg_gflush,
  369. .unmap_sg = sbus_iommu_unmap_sg,
  370. };
  371. static const struct dma_map_ops sbus_iommu_dma_pflush_ops = {
  372. #ifdef CONFIG_SBUS
  373. .alloc = sbus_iommu_alloc,
  374. .free = sbus_iommu_free,
  375. #endif
  376. .map_page = sbus_iommu_map_page_pflush,
  377. .unmap_page = sbus_iommu_unmap_page,
  378. .map_sg = sbus_iommu_map_sg_pflush,
  379. .unmap_sg = sbus_iommu_unmap_sg,
  380. };
  381. void __init ld_mmu_iommu(void)
  382. {
  383. if (viking_mxcc_present || srmmu_modtype == HyperSparc) {
  384. dvma_prot = __pgprot(SRMMU_CACHE | SRMMU_ET_PTE | SRMMU_PRIV);
  385. ioperm_noc = IOPTE_CACHE | IOPTE_WRITE | IOPTE_VALID;
  386. } else {
  387. dvma_prot = __pgprot(SRMMU_ET_PTE | SRMMU_PRIV);
  388. ioperm_noc = IOPTE_WRITE | IOPTE_VALID;
  389. }
  390. }