io-unit.c 9.2 KB

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