iommu.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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/highmem.h> /* pte_offset_map => kmap_atomic */
  15. #include <linux/scatterlist.h>
  16. #include <linux/of.h>
  17. #include <linux/of_device.h>
  18. #include <asm/pgalloc.h>
  19. #include <asm/pgtable.h>
  20. #include <asm/io.h>
  21. #include <asm/mxcc.h>
  22. #include <asm/mbus.h>
  23. #include <asm/cacheflush.h>
  24. #include <asm/tlbflush.h>
  25. #include <asm/bitext.h>
  26. #include <asm/iommu.h>
  27. #include <asm/dma.h>
  28. #include "mm_32.h"
  29. /*
  30. * This can be sized dynamically, but we will do this
  31. * only when we have a guidance about actual I/O pressures.
  32. */
  33. #define IOMMU_RNGE IOMMU_RNGE_256MB
  34. #define IOMMU_START 0xF0000000
  35. #define IOMMU_WINSIZE (256*1024*1024U)
  36. #define IOMMU_NPTES (IOMMU_WINSIZE/PAGE_SIZE) /* 64K PTEs, 256KB */
  37. #define IOMMU_ORDER 6 /* 4096 * (1<<6) */
  38. static int viking_flush;
  39. /* viking.S */
  40. extern void viking_flush_page(unsigned long page);
  41. extern void viking_mxcc_flush_page(unsigned long page);
  42. /*
  43. * Values precomputed according to CPU type.
  44. */
  45. static unsigned int ioperm_noc; /* Consistent mapping iopte flags */
  46. static pgprot_t dvma_prot; /* Consistent mapping pte flags */
  47. #define IOPERM (IOPTE_CACHE | IOPTE_WRITE | IOPTE_VALID)
  48. #define MKIOPTE(pfn, perm) (((((pfn)<<8) & IOPTE_PAGE) | (perm)) & ~IOPTE_WAZ)
  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. }
  115. static int __init iommu_init(void)
  116. {
  117. struct device_node *dp;
  118. for_each_node_by_name(dp, "iommu") {
  119. struct platform_device *op = of_find_device_by_node(dp);
  120. sbus_iommu_init(op);
  121. of_propagate_archdata(op);
  122. }
  123. return 0;
  124. }
  125. subsys_initcall(iommu_init);
  126. /* Flush the iotlb entries to ram. */
  127. /* This could be better if we didn't have to flush whole pages. */
  128. static void iommu_flush_iotlb(iopte_t *iopte, unsigned int niopte)
  129. {
  130. unsigned long start;
  131. unsigned long end;
  132. start = (unsigned long)iopte;
  133. end = PAGE_ALIGN(start + niopte*sizeof(iopte_t));
  134. start &= PAGE_MASK;
  135. if (viking_mxcc_present) {
  136. while(start < end) {
  137. viking_mxcc_flush_page(start);
  138. start += PAGE_SIZE;
  139. }
  140. } else if (viking_flush) {
  141. while(start < end) {
  142. viking_flush_page(start);
  143. start += PAGE_SIZE;
  144. }
  145. } else {
  146. while(start < end) {
  147. __flush_page_to_ram(start);
  148. start += PAGE_SIZE;
  149. }
  150. }
  151. }
  152. static u32 iommu_get_one(struct device *dev, struct page *page, int npages)
  153. {
  154. struct iommu_struct *iommu = dev->archdata.iommu;
  155. int ioptex;
  156. iopte_t *iopte, *iopte0;
  157. unsigned int busa, busa0;
  158. int i;
  159. /* page color = pfn of page */
  160. ioptex = bit_map_string_get(&iommu->usemap, npages, page_to_pfn(page));
  161. if (ioptex < 0)
  162. panic("iommu out");
  163. busa0 = iommu->start + (ioptex << PAGE_SHIFT);
  164. iopte0 = &iommu->page_table[ioptex];
  165. busa = busa0;
  166. iopte = iopte0;
  167. for (i = 0; i < npages; i++) {
  168. iopte_val(*iopte) = MKIOPTE(page_to_pfn(page), IOPERM);
  169. iommu_invalidate_page(iommu->regs, busa);
  170. busa += PAGE_SIZE;
  171. iopte++;
  172. page++;
  173. }
  174. iommu_flush_iotlb(iopte0, npages);
  175. return busa0;
  176. }
  177. static u32 iommu_get_scsi_one(struct device *dev, char *vaddr, unsigned int len)
  178. {
  179. unsigned long off;
  180. int npages;
  181. struct page *page;
  182. u32 busa;
  183. off = (unsigned long)vaddr & ~PAGE_MASK;
  184. npages = (off + len + PAGE_SIZE-1) >> PAGE_SHIFT;
  185. page = virt_to_page((unsigned long)vaddr & PAGE_MASK);
  186. busa = iommu_get_one(dev, page, npages);
  187. return busa + off;
  188. }
  189. static __u32 iommu_get_scsi_one_gflush(struct device *dev, char *vaddr, unsigned long len)
  190. {
  191. flush_page_for_dma(0);
  192. return iommu_get_scsi_one(dev, vaddr, len);
  193. }
  194. static __u32 iommu_get_scsi_one_pflush(struct device *dev, char *vaddr, unsigned long len)
  195. {
  196. unsigned long page = ((unsigned long) vaddr) & PAGE_MASK;
  197. while(page < ((unsigned long)(vaddr + len))) {
  198. flush_page_for_dma(page);
  199. page += PAGE_SIZE;
  200. }
  201. return iommu_get_scsi_one(dev, vaddr, len);
  202. }
  203. static void iommu_get_scsi_sgl_gflush(struct device *dev, struct scatterlist *sg, int sz)
  204. {
  205. int n;
  206. flush_page_for_dma(0);
  207. while (sz != 0) {
  208. --sz;
  209. n = (sg->length + sg->offset + PAGE_SIZE-1) >> PAGE_SHIFT;
  210. sg->dma_address = iommu_get_one(dev, sg_page(sg), n) + sg->offset;
  211. sg->dma_length = sg->length;
  212. sg = sg_next(sg);
  213. }
  214. }
  215. static void iommu_get_scsi_sgl_pflush(struct device *dev, struct scatterlist *sg, int sz)
  216. {
  217. unsigned long page, oldpage = 0;
  218. int n, i;
  219. while(sz != 0) {
  220. --sz;
  221. n = (sg->length + sg->offset + PAGE_SIZE-1) >> PAGE_SHIFT;
  222. /*
  223. * We expect unmapped highmem pages to be not in the cache.
  224. * XXX Is this a good assumption?
  225. * XXX What if someone else unmaps it here and races us?
  226. */
  227. if ((page = (unsigned long) page_address(sg_page(sg))) != 0) {
  228. for (i = 0; i < n; i++) {
  229. if (page != oldpage) { /* Already flushed? */
  230. flush_page_for_dma(page);
  231. oldpage = page;
  232. }
  233. page += PAGE_SIZE;
  234. }
  235. }
  236. sg->dma_address = iommu_get_one(dev, sg_page(sg), n) + sg->offset;
  237. sg->dma_length = sg->length;
  238. sg = sg_next(sg);
  239. }
  240. }
  241. static void iommu_release_one(struct device *dev, u32 busa, int npages)
  242. {
  243. struct iommu_struct *iommu = dev->archdata.iommu;
  244. int ioptex;
  245. int i;
  246. BUG_ON(busa < iommu->start);
  247. ioptex = (busa - iommu->start) >> PAGE_SHIFT;
  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 iommu_release_scsi_one(struct device *dev, __u32 vaddr, unsigned long len)
  256. {
  257. unsigned long off;
  258. int npages;
  259. off = vaddr & ~PAGE_MASK;
  260. npages = (off + len + PAGE_SIZE-1) >> PAGE_SHIFT;
  261. iommu_release_one(dev, vaddr & PAGE_MASK, npages);
  262. }
  263. static void iommu_release_scsi_sgl(struct device *dev, struct scatterlist *sg, int sz)
  264. {
  265. int n;
  266. while(sz != 0) {
  267. --sz;
  268. n = (sg->length + sg->offset + PAGE_SIZE-1) >> PAGE_SHIFT;
  269. iommu_release_one(dev, sg->dma_address & PAGE_MASK, n);
  270. sg->dma_address = 0x21212121;
  271. sg = sg_next(sg);
  272. }
  273. }
  274. #ifdef CONFIG_SBUS
  275. static int iommu_map_dma_area(struct device *dev, dma_addr_t *pba, unsigned long va,
  276. unsigned long addr, int len)
  277. {
  278. struct iommu_struct *iommu = dev->archdata.iommu;
  279. unsigned long page, end;
  280. iopte_t *iopte = iommu->page_table;
  281. iopte_t *first;
  282. int ioptex;
  283. BUG_ON((va & ~PAGE_MASK) != 0);
  284. BUG_ON((addr & ~PAGE_MASK) != 0);
  285. BUG_ON((len & ~PAGE_MASK) != 0);
  286. /* page color = physical address */
  287. ioptex = bit_map_string_get(&iommu->usemap, len >> PAGE_SHIFT,
  288. addr >> PAGE_SHIFT);
  289. if (ioptex < 0)
  290. panic("iommu out");
  291. iopte += ioptex;
  292. first = iopte;
  293. end = addr + len;
  294. while(addr < end) {
  295. page = va;
  296. {
  297. pgd_t *pgdp;
  298. pmd_t *pmdp;
  299. pte_t *ptep;
  300. if (viking_mxcc_present)
  301. viking_mxcc_flush_page(page);
  302. else if (viking_flush)
  303. viking_flush_page(page);
  304. else
  305. __flush_page_to_ram(page);
  306. pgdp = pgd_offset(&init_mm, addr);
  307. pmdp = pmd_offset(pgdp, addr);
  308. ptep = pte_offset_map(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. *pba = iommu->start + (ioptex << PAGE_SHIFT);
  332. return 0;
  333. }
  334. static void iommu_unmap_dma_area(struct device *dev, unsigned long busa, int len)
  335. {
  336. struct iommu_struct *iommu = dev->archdata.iommu;
  337. iopte_t *iopte = iommu->page_table;
  338. unsigned long end;
  339. int ioptex = (busa - iommu->start) >> PAGE_SHIFT;
  340. BUG_ON((busa & ~PAGE_MASK) != 0);
  341. BUG_ON((len & ~PAGE_MASK) != 0);
  342. iopte += ioptex;
  343. end = busa + len;
  344. while (busa < end) {
  345. iopte_val(*iopte++) = 0;
  346. busa += PAGE_SIZE;
  347. }
  348. flush_tlb_all();
  349. iommu_invalidate(iommu->regs);
  350. bit_map_clear(&iommu->usemap, ioptex, len >> PAGE_SHIFT);
  351. }
  352. #endif
  353. static const struct sparc32_dma_ops iommu_dma_gflush_ops = {
  354. .get_scsi_one = iommu_get_scsi_one_gflush,
  355. .get_scsi_sgl = iommu_get_scsi_sgl_gflush,
  356. .release_scsi_one = iommu_release_scsi_one,
  357. .release_scsi_sgl = iommu_release_scsi_sgl,
  358. #ifdef CONFIG_SBUS
  359. .map_dma_area = iommu_map_dma_area,
  360. .unmap_dma_area = iommu_unmap_dma_area,
  361. #endif
  362. };
  363. static const struct sparc32_dma_ops iommu_dma_pflush_ops = {
  364. .get_scsi_one = iommu_get_scsi_one_pflush,
  365. .get_scsi_sgl = iommu_get_scsi_sgl_pflush,
  366. .release_scsi_one = iommu_release_scsi_one,
  367. .release_scsi_sgl = iommu_release_scsi_sgl,
  368. #ifdef CONFIG_SBUS
  369. .map_dma_area = iommu_map_dma_area,
  370. .unmap_dma_area = iommu_unmap_dma_area,
  371. #endif
  372. };
  373. void __init ld_mmu_iommu(void)
  374. {
  375. if (flush_page_for_dma_global) {
  376. /* flush_page_for_dma flushes everything, no matter of what page is it */
  377. sparc32_dma_ops = &iommu_dma_gflush_ops;
  378. } else {
  379. sparc32_dma_ops = &iommu_dma_pflush_ops;
  380. }
  381. if (viking_mxcc_present || srmmu_modtype == HyperSparc) {
  382. dvma_prot = __pgprot(SRMMU_CACHE | SRMMU_ET_PTE | SRMMU_PRIV);
  383. ioperm_noc = IOPTE_CACHE | IOPTE_WRITE | IOPTE_VALID;
  384. } else {
  385. dvma_prot = __pgprot(SRMMU_ET_PTE | SRMMU_PRIV);
  386. ioperm_noc = IOPTE_WRITE | IOPTE_VALID;
  387. }
  388. }