io-unit.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /* $Id: io-unit.c,v 1.24 2001/12/17 07:05:09 davem Exp $
  2. * io-unit.c: IO-UNIT specific routines for memory management.
  3. *
  4. * Copyright (C) 1997,1998 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
  5. */
  6. #include <linux/config.h>
  7. #include <linux/kernel.h>
  8. #include <linux/init.h>
  9. #include <linux/slab.h>
  10. #include <linux/spinlock.h>
  11. #include <linux/mm.h>
  12. #include <linux/highmem.h> /* pte_offset_map => kmap_atomic */
  13. #include <linux/bitops.h>
  14. #include <asm/scatterlist.h>
  15. #include <asm/pgalloc.h>
  16. #include <asm/pgtable.h>
  17. #include <asm/sbus.h>
  18. #include <asm/io.h>
  19. #include <asm/io-unit.h>
  20. #include <asm/mxcc.h>
  21. #include <asm/cacheflush.h>
  22. #include <asm/tlbflush.h>
  23. #include <asm/dma.h>
  24. /* #define IOUNIT_DEBUG */
  25. #ifdef IOUNIT_DEBUG
  26. #define IOD(x) printk(x)
  27. #else
  28. #define IOD(x) do { } while (0)
  29. #endif
  30. #define IOPERM (IOUPTE_CACHE | IOUPTE_WRITE | IOUPTE_VALID)
  31. #define MKIOPTE(phys) __iopte((((phys)>>4) & IOUPTE_PAGE) | IOPERM)
  32. void __init
  33. iounit_init(int sbi_node, int io_node, struct sbus_bus *sbus)
  34. {
  35. iopte_t *xpt, *xptend;
  36. struct iounit_struct *iounit;
  37. struct linux_prom_registers iommu_promregs[PROMREG_MAX];
  38. struct resource r;
  39. iounit = kmalloc(sizeof(struct iounit_struct), GFP_ATOMIC);
  40. memset(iounit, 0, sizeof(*iounit));
  41. iounit->limit[0] = IOUNIT_BMAP1_START;
  42. iounit->limit[1] = IOUNIT_BMAP2_START;
  43. iounit->limit[2] = IOUNIT_BMAPM_START;
  44. iounit->limit[3] = IOUNIT_BMAPM_END;
  45. iounit->rotor[1] = IOUNIT_BMAP2_START;
  46. iounit->rotor[2] = IOUNIT_BMAPM_START;
  47. xpt = NULL;
  48. if(prom_getproperty(sbi_node, "reg", (void *) iommu_promregs,
  49. sizeof(iommu_promregs)) != -1) {
  50. prom_apply_generic_ranges(io_node, 0, iommu_promregs, 3);
  51. memset(&r, 0, sizeof(r));
  52. r.flags = iommu_promregs[2].which_io;
  53. r.start = iommu_promregs[2].phys_addr;
  54. xpt = (iopte_t *) sbus_ioremap(&r, 0, PAGE_SIZE * 16, "XPT");
  55. }
  56. if(!xpt) panic("Cannot map External Page Table.");
  57. sbus->iommu = (struct iommu_struct *)iounit;
  58. iounit->page_table = xpt;
  59. for (xptend = iounit->page_table + (16 * PAGE_SIZE) / sizeof(iopte_t);
  60. xpt < xptend;)
  61. iopte_val(*xpt++) = 0;
  62. }
  63. /* One has to hold iounit->lock to call this */
  64. static unsigned long iounit_get_area(struct iounit_struct *iounit, unsigned long vaddr, int size)
  65. {
  66. int i, j, k, npages;
  67. unsigned long rotor, scan, limit;
  68. iopte_t iopte;
  69. npages = ((vaddr & ~PAGE_MASK) + size + (PAGE_SIZE-1)) >> PAGE_SHIFT;
  70. /* A tiny bit of magic ingredience :) */
  71. switch (npages) {
  72. case 1: i = 0x0231; break;
  73. case 2: i = 0x0132; break;
  74. default: i = 0x0213; break;
  75. }
  76. IOD(("iounit_get_area(%08lx,%d[%d])=", vaddr, size, npages));
  77. next: j = (i & 15);
  78. rotor = iounit->rotor[j - 1];
  79. limit = iounit->limit[j];
  80. scan = rotor;
  81. nexti: scan = find_next_zero_bit(iounit->bmap, limit, scan);
  82. if (scan + npages > limit) {
  83. if (limit != rotor) {
  84. limit = rotor;
  85. scan = iounit->limit[j - 1];
  86. goto nexti;
  87. }
  88. i >>= 4;
  89. if (!(i & 15))
  90. panic("iounit_get_area: Couldn't find free iopte slots for (%08lx,%d)\n", vaddr, size);
  91. goto next;
  92. }
  93. for (k = 1, scan++; k < npages; k++)
  94. if (test_bit(scan++, iounit->bmap))
  95. goto nexti;
  96. iounit->rotor[j - 1] = (scan < limit) ? scan : iounit->limit[j - 1];
  97. scan -= npages;
  98. iopte = MKIOPTE(__pa(vaddr & PAGE_MASK));
  99. vaddr = IOUNIT_DMA_BASE + (scan << PAGE_SHIFT) + (vaddr & ~PAGE_MASK);
  100. for (k = 0; k < npages; k++, iopte = __iopte(iopte_val(iopte) + 0x100), scan++) {
  101. set_bit(scan, iounit->bmap);
  102. iounit->page_table[scan] = iopte;
  103. }
  104. IOD(("%08lx\n", vaddr));
  105. return vaddr;
  106. }
  107. static __u32 iounit_get_scsi_one(char *vaddr, unsigned long len, struct sbus_bus *sbus)
  108. {
  109. unsigned long ret, flags;
  110. struct iounit_struct *iounit = (struct iounit_struct *)sbus->iommu;
  111. spin_lock_irqsave(&iounit->lock, flags);
  112. ret = iounit_get_area(iounit, (unsigned long)vaddr, len);
  113. spin_unlock_irqrestore(&iounit->lock, flags);
  114. return ret;
  115. }
  116. static void iounit_get_scsi_sgl(struct scatterlist *sg, int sz, struct sbus_bus *sbus)
  117. {
  118. unsigned long flags;
  119. struct iounit_struct *iounit = (struct iounit_struct *)sbus->iommu;
  120. /* FIXME: Cache some resolved pages - often several sg entries are to the same page */
  121. spin_lock_irqsave(&iounit->lock, flags);
  122. while (sz != 0) {
  123. --sz;
  124. sg[sz].dvma_address = iounit_get_area(iounit, (unsigned long)page_address(sg[sz].page) + sg[sz].offset, sg[sz].length);
  125. sg[sz].dvma_length = sg[sz].length;
  126. }
  127. spin_unlock_irqrestore(&iounit->lock, flags);
  128. }
  129. static void iounit_release_scsi_one(__u32 vaddr, unsigned long len, struct sbus_bus *sbus)
  130. {
  131. unsigned long flags;
  132. struct iounit_struct *iounit = (struct iounit_struct *)sbus->iommu;
  133. spin_lock_irqsave(&iounit->lock, flags);
  134. len = ((vaddr & ~PAGE_MASK) + len + (PAGE_SIZE-1)) >> PAGE_SHIFT;
  135. vaddr = (vaddr - IOUNIT_DMA_BASE) >> PAGE_SHIFT;
  136. IOD(("iounit_release %08lx-%08lx\n", (long)vaddr, (long)len+vaddr));
  137. for (len += vaddr; vaddr < len; vaddr++)
  138. clear_bit(vaddr, iounit->bmap);
  139. spin_unlock_irqrestore(&iounit->lock, flags);
  140. }
  141. static void iounit_release_scsi_sgl(struct scatterlist *sg, int sz, struct sbus_bus *sbus)
  142. {
  143. unsigned long flags;
  144. unsigned long vaddr, len;
  145. struct iounit_struct *iounit = (struct iounit_struct *)sbus->iommu;
  146. spin_lock_irqsave(&iounit->lock, flags);
  147. while (sz != 0) {
  148. --sz;
  149. len = ((sg[sz].dvma_address & ~PAGE_MASK) + sg[sz].length + (PAGE_SIZE-1)) >> PAGE_SHIFT;
  150. vaddr = (sg[sz].dvma_address - IOUNIT_DMA_BASE) >> PAGE_SHIFT;
  151. IOD(("iounit_release %08lx-%08lx\n", (long)vaddr, (long)len+vaddr));
  152. for (len += vaddr; vaddr < len; vaddr++)
  153. clear_bit(vaddr, iounit->bmap);
  154. }
  155. spin_unlock_irqrestore(&iounit->lock, flags);
  156. }
  157. #ifdef CONFIG_SBUS
  158. static int iounit_map_dma_area(dma_addr_t *pba, unsigned long va, __u32 addr, int len)
  159. {
  160. unsigned long page, end;
  161. pgprot_t dvma_prot;
  162. iopte_t *iopte;
  163. struct sbus_bus *sbus;
  164. *pba = addr;
  165. dvma_prot = __pgprot(SRMMU_CACHE | SRMMU_ET_PTE | SRMMU_PRIV);
  166. end = PAGE_ALIGN((addr + len));
  167. while(addr < end) {
  168. page = va;
  169. {
  170. pgd_t *pgdp;
  171. pmd_t *pmdp;
  172. pte_t *ptep;
  173. long i;
  174. pgdp = pgd_offset(&init_mm, addr);
  175. pmdp = pmd_offset(pgdp, addr);
  176. ptep = pte_offset_map(pmdp, addr);
  177. set_pte(ptep, mk_pte(virt_to_page(page), dvma_prot));
  178. i = ((addr - IOUNIT_DMA_BASE) >> PAGE_SHIFT);
  179. for_each_sbus(sbus) {
  180. struct iounit_struct *iounit = (struct iounit_struct *)sbus->iommu;
  181. iopte = (iopte_t *)(iounit->page_table + i);
  182. *iopte = MKIOPTE(__pa(page));
  183. }
  184. }
  185. addr += PAGE_SIZE;
  186. va += PAGE_SIZE;
  187. }
  188. flush_cache_all();
  189. flush_tlb_all();
  190. return 0;
  191. }
  192. static void iounit_unmap_dma_area(unsigned long addr, int len)
  193. {
  194. /* XXX Somebody please fill this in */
  195. }
  196. /* XXX We do not pass sbus device here, bad. */
  197. static struct page *iounit_translate_dvma(unsigned long addr)
  198. {
  199. struct sbus_bus *sbus = sbus_root; /* They are all the same */
  200. struct iounit_struct *iounit = (struct iounit_struct *)sbus->iommu;
  201. int i;
  202. iopte_t *iopte;
  203. i = ((addr - IOUNIT_DMA_BASE) >> PAGE_SHIFT);
  204. iopte = (iopte_t *)(iounit->page_table + i);
  205. return pfn_to_page(iopte_val(*iopte) >> (PAGE_SHIFT-4)); /* XXX sun4d guru, help */
  206. }
  207. #endif
  208. static char *iounit_lockarea(char *vaddr, unsigned long len)
  209. {
  210. /* FIXME: Write this */
  211. return vaddr;
  212. }
  213. static void iounit_unlockarea(char *vaddr, unsigned long len)
  214. {
  215. /* FIXME: Write this */
  216. }
  217. void __init ld_mmu_iounit(void)
  218. {
  219. BTFIXUPSET_CALL(mmu_lockarea, iounit_lockarea, BTFIXUPCALL_RETO0);
  220. BTFIXUPSET_CALL(mmu_unlockarea, iounit_unlockarea, BTFIXUPCALL_NOP);
  221. BTFIXUPSET_CALL(mmu_get_scsi_one, iounit_get_scsi_one, BTFIXUPCALL_NORM);
  222. BTFIXUPSET_CALL(mmu_get_scsi_sgl, iounit_get_scsi_sgl, BTFIXUPCALL_NORM);
  223. BTFIXUPSET_CALL(mmu_release_scsi_one, iounit_release_scsi_one, BTFIXUPCALL_NORM);
  224. BTFIXUPSET_CALL(mmu_release_scsi_sgl, iounit_release_scsi_sgl, BTFIXUPCALL_NORM);
  225. #ifdef CONFIG_SBUS
  226. BTFIXUPSET_CALL(mmu_map_dma_area, iounit_map_dma_area, BTFIXUPCALL_NORM);
  227. BTFIXUPSET_CALL(mmu_unmap_dma_area, iounit_unmap_dma_area, BTFIXUPCALL_NORM);
  228. BTFIXUPSET_CALL(mmu_translate_dvma, iounit_translate_dvma, BTFIXUPCALL_NORM);
  229. #endif
  230. }
  231. __u32 iounit_map_dma_init(struct sbus_bus *sbus, int size)
  232. {
  233. int i, j, k, npages;
  234. unsigned long rotor, scan, limit;
  235. unsigned long flags;
  236. __u32 ret;
  237. struct iounit_struct *iounit = (struct iounit_struct *)sbus->iommu;
  238. npages = (size + (PAGE_SIZE-1)) >> PAGE_SHIFT;
  239. i = 0x0213;
  240. spin_lock_irqsave(&iounit->lock, flags);
  241. next: j = (i & 15);
  242. rotor = iounit->rotor[j - 1];
  243. limit = iounit->limit[j];
  244. scan = rotor;
  245. nexti: scan = find_next_zero_bit(iounit->bmap, limit, scan);
  246. if (scan + npages > limit) {
  247. if (limit != rotor) {
  248. limit = rotor;
  249. scan = iounit->limit[j - 1];
  250. goto nexti;
  251. }
  252. i >>= 4;
  253. if (!(i & 15))
  254. panic("iounit_map_dma_init: Couldn't find free iopte slots for %d bytes\n", size);
  255. goto next;
  256. }
  257. for (k = 1, scan++; k < npages; k++)
  258. if (test_bit(scan++, iounit->bmap))
  259. goto nexti;
  260. iounit->rotor[j - 1] = (scan < limit) ? scan : iounit->limit[j - 1];
  261. scan -= npages;
  262. ret = IOUNIT_DMA_BASE + (scan << PAGE_SHIFT);
  263. for (k = 0; k < npages; k++, scan++)
  264. set_bit(scan, iounit->bmap);
  265. spin_unlock_irqrestore(&iounit->lock, flags);
  266. return ret;
  267. }
  268. __u32 iounit_map_dma_page(__u32 vaddr, void *addr, struct sbus_bus *sbus)
  269. {
  270. int scan = (vaddr - IOUNIT_DMA_BASE) >> PAGE_SHIFT;
  271. struct iounit_struct *iounit = (struct iounit_struct *)sbus->iommu;
  272. iounit->page_table[scan] = MKIOPTE(__pa(((unsigned long)addr) & PAGE_MASK));
  273. return vaddr + (((unsigned long)addr) & ~PAGE_MASK);
  274. }