iommu.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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. /*
  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, 265KB */
  35. #define IOMMU_ORDER 6 /* 4096 * (1<<6) */
  36. /* srmmu.c */
  37. extern int viking_mxcc_present;
  38. extern int flush_page_for_dma_global;
  39. static int viking_flush;
  40. /* viking.S */
  41. extern void viking_flush_page(unsigned long page);
  42. extern void viking_mxcc_flush_page(unsigned long page);
  43. /*
  44. * Values precomputed according to CPU type.
  45. */
  46. static unsigned int ioperm_noc; /* Consistent mapping iopte flags */
  47. static pgprot_t dvma_prot; /* Consistent mapping pte flags */
  48. #define IOPERM (IOPTE_CACHE | IOPTE_WRITE | IOPTE_VALID)
  49. #define MKIOPTE(pfn, perm) (((((pfn)<<8) & IOPTE_PAGE) | (perm)) & ~IOPTE_WAZ)
  50. static void __init sbus_iommu_init(struct platform_device *op)
  51. {
  52. struct iommu_struct *iommu;
  53. unsigned int impl, vers;
  54. unsigned long *bitmap;
  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. impl = (iommu->regs->control & IOMMU_CTRL_IMPL) >> 28;
  68. vers = (iommu->regs->control & IOMMU_CTRL_VERS) >> 24;
  69. tmp = iommu->regs->control;
  70. tmp &= ~(IOMMU_CTRL_RNGE);
  71. tmp |= (IOMMU_RNGE_256MB | IOMMU_CTRL_ENAB);
  72. iommu->regs->control = tmp;
  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. iommu->regs->base = __pa((unsigned long) iommu->page_table) >> 4;
  93. iommu_invalidate(iommu->regs);
  94. bitmap = kmalloc(IOMMU_NPTES>>3, GFP_KERNEL);
  95. if (!bitmap) {
  96. prom_printf("Unable to allocate iommu bitmap [%d]\n",
  97. (int)(IOMMU_NPTES>>3));
  98. prom_halt();
  99. }
  100. bit_map_init(&iommu->usemap, bitmap, IOMMU_NPTES);
  101. /* To be coherent on HyperSparc, the page color of DVMA
  102. * and physical addresses must match.
  103. */
  104. if (srmmu_modtype == HyperSparc)
  105. iommu->usemap.num_colors = vac_cache_size >> PAGE_SHIFT;
  106. else
  107. iommu->usemap.num_colors = 1;
  108. printk(KERN_INFO "IOMMU: impl %d vers %d table 0x%p[%d B] map [%d b]\n",
  109. impl, vers, iommu->page_table,
  110. (int)(IOMMU_NPTES*sizeof(iopte_t)), (int)IOMMU_NPTES);
  111. op->dev.archdata.iommu = iommu;
  112. }
  113. static int __init iommu_init(void)
  114. {
  115. struct device_node *dp;
  116. for_each_node_by_name(dp, "iommu") {
  117. struct platform_device *op = of_find_device_by_node(dp);
  118. sbus_iommu_init(op);
  119. of_propagate_archdata(op);
  120. }
  121. return 0;
  122. }
  123. subsys_initcall(iommu_init);
  124. /* Flush the iotlb entries to ram. */
  125. /* This could be better if we didn't have to flush whole pages. */
  126. static void iommu_flush_iotlb(iopte_t *iopte, unsigned int niopte)
  127. {
  128. unsigned long start;
  129. unsigned long end;
  130. start = (unsigned long)iopte;
  131. end = PAGE_ALIGN(start + niopte*sizeof(iopte_t));
  132. start &= PAGE_MASK;
  133. if (viking_mxcc_present) {
  134. while(start < end) {
  135. viking_mxcc_flush_page(start);
  136. start += PAGE_SIZE;
  137. }
  138. } else if (viking_flush) {
  139. while(start < end) {
  140. viking_flush_page(start);
  141. start += PAGE_SIZE;
  142. }
  143. } else {
  144. while(start < end) {
  145. __flush_page_to_ram(start);
  146. start += PAGE_SIZE;
  147. }
  148. }
  149. }
  150. static u32 iommu_get_one(struct device *dev, struct page *page, int npages)
  151. {
  152. struct iommu_struct *iommu = dev->archdata.iommu;
  153. int ioptex;
  154. iopte_t *iopte, *iopte0;
  155. unsigned int busa, busa0;
  156. int i;
  157. /* page color = pfn of page */
  158. ioptex = bit_map_string_get(&iommu->usemap, npages, page_to_pfn(page));
  159. if (ioptex < 0)
  160. panic("iommu out");
  161. busa0 = iommu->start + (ioptex << PAGE_SHIFT);
  162. iopte0 = &iommu->page_table[ioptex];
  163. busa = busa0;
  164. iopte = iopte0;
  165. for (i = 0; i < npages; i++) {
  166. iopte_val(*iopte) = MKIOPTE(page_to_pfn(page), IOPERM);
  167. iommu_invalidate_page(iommu->regs, busa);
  168. busa += PAGE_SIZE;
  169. iopte++;
  170. page++;
  171. }
  172. iommu_flush_iotlb(iopte0, npages);
  173. return busa0;
  174. }
  175. static u32 iommu_get_scsi_one(struct device *dev, char *vaddr, unsigned int len)
  176. {
  177. unsigned long off;
  178. int npages;
  179. struct page *page;
  180. u32 busa;
  181. off = (unsigned long)vaddr & ~PAGE_MASK;
  182. npages = (off + len + PAGE_SIZE-1) >> PAGE_SHIFT;
  183. page = virt_to_page((unsigned long)vaddr & PAGE_MASK);
  184. busa = iommu_get_one(dev, page, npages);
  185. return busa + off;
  186. }
  187. static __u32 iommu_get_scsi_one_gflush(struct device *dev, char *vaddr, unsigned long len)
  188. {
  189. flush_page_for_dma(0);
  190. return iommu_get_scsi_one(dev, vaddr, len);
  191. }
  192. static __u32 iommu_get_scsi_one_pflush(struct device *dev, char *vaddr, unsigned long len)
  193. {
  194. unsigned long page = ((unsigned long) vaddr) & PAGE_MASK;
  195. while(page < ((unsigned long)(vaddr + len))) {
  196. flush_page_for_dma(page);
  197. page += PAGE_SIZE;
  198. }
  199. return iommu_get_scsi_one(dev, vaddr, len);
  200. }
  201. static void iommu_get_scsi_sgl_gflush(struct device *dev, struct scatterlist *sg, int sz)
  202. {
  203. int n;
  204. flush_page_for_dma(0);
  205. while (sz != 0) {
  206. --sz;
  207. n = (sg->length + sg->offset + PAGE_SIZE-1) >> PAGE_SHIFT;
  208. sg->dma_address = iommu_get_one(dev, sg_page(sg), n) + sg->offset;
  209. sg->dma_length = sg->length;
  210. sg = sg_next(sg);
  211. }
  212. }
  213. static void iommu_get_scsi_sgl_pflush(struct device *dev, struct scatterlist *sg, int sz)
  214. {
  215. unsigned long page, oldpage = 0;
  216. int n, i;
  217. while(sz != 0) {
  218. --sz;
  219. n = (sg->length + sg->offset + PAGE_SIZE-1) >> PAGE_SHIFT;
  220. /*
  221. * We expect unmapped highmem pages to be not in the cache.
  222. * XXX Is this a good assumption?
  223. * XXX What if someone else unmaps it here and races us?
  224. */
  225. if ((page = (unsigned long) page_address(sg_page(sg))) != 0) {
  226. for (i = 0; i < n; i++) {
  227. if (page != oldpage) { /* Already flushed? */
  228. flush_page_for_dma(page);
  229. oldpage = page;
  230. }
  231. page += PAGE_SIZE;
  232. }
  233. }
  234. sg->dma_address = iommu_get_one(dev, sg_page(sg), n) + sg->offset;
  235. sg->dma_length = sg->length;
  236. sg = sg_next(sg);
  237. }
  238. }
  239. static void iommu_release_one(struct device *dev, u32 busa, int npages)
  240. {
  241. struct iommu_struct *iommu = dev->archdata.iommu;
  242. int ioptex;
  243. int i;
  244. BUG_ON(busa < iommu->start);
  245. ioptex = (busa - iommu->start) >> PAGE_SHIFT;
  246. for (i = 0; i < npages; i++) {
  247. iopte_val(iommu->page_table[ioptex + i]) = 0;
  248. iommu_invalidate_page(iommu->regs, busa);
  249. busa += PAGE_SIZE;
  250. }
  251. bit_map_clear(&iommu->usemap, ioptex, npages);
  252. }
  253. static void iommu_release_scsi_one(struct device *dev, __u32 vaddr, unsigned long len)
  254. {
  255. unsigned long off;
  256. int npages;
  257. off = vaddr & ~PAGE_MASK;
  258. npages = (off + len + PAGE_SIZE-1) >> PAGE_SHIFT;
  259. iommu_release_one(dev, vaddr & PAGE_MASK, npages);
  260. }
  261. static void iommu_release_scsi_sgl(struct device *dev, struct scatterlist *sg, int sz)
  262. {
  263. int n;
  264. while(sz != 0) {
  265. --sz;
  266. n = (sg->length + sg->offset + PAGE_SIZE-1) >> PAGE_SHIFT;
  267. iommu_release_one(dev, sg->dma_address & PAGE_MASK, n);
  268. sg->dma_address = 0x21212121;
  269. sg = sg_next(sg);
  270. }
  271. }
  272. #ifdef CONFIG_SBUS
  273. static int iommu_map_dma_area(struct device *dev, dma_addr_t *pba, unsigned long va,
  274. unsigned long addr, int len)
  275. {
  276. struct iommu_struct *iommu = dev->archdata.iommu;
  277. unsigned long page, end;
  278. iopte_t *iopte = iommu->page_table;
  279. iopte_t *first;
  280. int ioptex;
  281. BUG_ON((va & ~PAGE_MASK) != 0);
  282. BUG_ON((addr & ~PAGE_MASK) != 0);
  283. BUG_ON((len & ~PAGE_MASK) != 0);
  284. /* page color = physical address */
  285. ioptex = bit_map_string_get(&iommu->usemap, len >> PAGE_SHIFT,
  286. addr >> PAGE_SHIFT);
  287. if (ioptex < 0)
  288. panic("iommu out");
  289. iopte += ioptex;
  290. first = iopte;
  291. end = addr + len;
  292. while(addr < end) {
  293. page = va;
  294. {
  295. pgd_t *pgdp;
  296. pmd_t *pmdp;
  297. pte_t *ptep;
  298. if (viking_mxcc_present)
  299. viking_mxcc_flush_page(page);
  300. else if (viking_flush)
  301. viking_flush_page(page);
  302. else
  303. __flush_page_to_ram(page);
  304. pgdp = pgd_offset(&init_mm, addr);
  305. pmdp = pmd_offset(pgdp, addr);
  306. ptep = pte_offset_map(pmdp, addr);
  307. set_pte(ptep, mk_pte(virt_to_page(page), dvma_prot));
  308. }
  309. iopte_val(*iopte++) =
  310. MKIOPTE(page_to_pfn(virt_to_page(page)), ioperm_noc);
  311. addr += PAGE_SIZE;
  312. va += PAGE_SIZE;
  313. }
  314. /* P3: why do we need this?
  315. *
  316. * DAVEM: Because there are several aspects, none of which
  317. * are handled by a single interface. Some cpus are
  318. * completely not I/O DMA coherent, and some have
  319. * virtually indexed caches. The driver DMA flushing
  320. * methods handle the former case, but here during
  321. * IOMMU page table modifications, and usage of non-cacheable
  322. * cpu mappings of pages potentially in the cpu caches, we have
  323. * to handle the latter case as well.
  324. */
  325. flush_cache_all();
  326. iommu_flush_iotlb(first, len >> PAGE_SHIFT);
  327. flush_tlb_all();
  328. iommu_invalidate(iommu->regs);
  329. *pba = iommu->start + (ioptex << PAGE_SHIFT);
  330. return 0;
  331. }
  332. static void iommu_unmap_dma_area(struct device *dev, unsigned long busa, int len)
  333. {
  334. struct iommu_struct *iommu = dev->archdata.iommu;
  335. iopte_t *iopte = iommu->page_table;
  336. unsigned long end;
  337. int ioptex = (busa - iommu->start) >> PAGE_SHIFT;
  338. BUG_ON((busa & ~PAGE_MASK) != 0);
  339. BUG_ON((len & ~PAGE_MASK) != 0);
  340. iopte += ioptex;
  341. end = busa + len;
  342. while (busa < end) {
  343. iopte_val(*iopte++) = 0;
  344. busa += PAGE_SIZE;
  345. }
  346. flush_tlb_all();
  347. iommu_invalidate(iommu->regs);
  348. bit_map_clear(&iommu->usemap, ioptex, len >> PAGE_SHIFT);
  349. }
  350. #endif
  351. static const struct sparc32_dma_ops iommu_dma_gflush_ops = {
  352. .get_scsi_one = iommu_get_scsi_one_gflush,
  353. .get_scsi_sgl = iommu_get_scsi_sgl_gflush,
  354. .release_scsi_one = iommu_release_scsi_one,
  355. .release_scsi_sgl = iommu_release_scsi_sgl,
  356. #ifdef CONFIG_SBUS
  357. .map_dma_area = iommu_map_dma_area,
  358. .unmap_dma_area = iommu_unmap_dma_area,
  359. #endif
  360. };
  361. static const struct sparc32_dma_ops iommu_dma_pflush_ops = {
  362. .get_scsi_one = iommu_get_scsi_one_pflush,
  363. .get_scsi_sgl = iommu_get_scsi_sgl_pflush,
  364. .release_scsi_one = iommu_release_scsi_one,
  365. .release_scsi_sgl = iommu_release_scsi_sgl,
  366. #ifdef CONFIG_SBUS
  367. .map_dma_area = iommu_map_dma_area,
  368. .unmap_dma_area = iommu_unmap_dma_area,
  369. #endif
  370. };
  371. void __init ld_mmu_iommu(void)
  372. {
  373. if (flush_page_for_dma_global) {
  374. /* flush_page_for_dma flushes everything, no matter of what page is it */
  375. sparc32_dma_ops = &iommu_dma_gflush_ops;
  376. } else {
  377. sparc32_dma_ops = &iommu_dma_pflush_ops;
  378. }
  379. if (viking_mxcc_present || srmmu_modtype == HyperSparc) {
  380. dvma_prot = __pgprot(SRMMU_CACHE | SRMMU_ET_PTE | SRMMU_PRIV);
  381. ioperm_noc = IOPTE_CACHE | IOPTE_WRITE | IOPTE_VALID;
  382. } else {
  383. dvma_prot = __pgprot(SRMMU_ET_PTE | SRMMU_PRIV);
  384. ioperm_noc = IOPTE_WRITE | IOPTE_VALID;
  385. }
  386. }