iommu.c 12 KB

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