io-unit.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  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 <linux/scatterlist.h>
  14. #include <linux/of.h>
  15. #include <linux/of_device.h>
  16. #include <asm/pgalloc.h>
  17. #include <asm/pgtable.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. #include <asm/oplib.h>
  25. /* #define IOUNIT_DEBUG */
  26. #ifdef IOUNIT_DEBUG
  27. #define IOD(x) printk(x)
  28. #else
  29. #define IOD(x) do { } while (0)
  30. #endif
  31. #define IOPERM (IOUPTE_CACHE | IOUPTE_WRITE | IOUPTE_VALID)
  32. #define MKIOPTE(phys) __iopte((((phys)>>4) & IOUPTE_PAGE) | IOPERM)
  33. static void __init iounit_iommu_init(struct platform_device *op)
  34. {
  35. struct iounit_struct *iounit;
  36. iopte_t *xpt, *xptend;
  37. iounit = kzalloc(sizeof(struct iounit_struct), GFP_ATOMIC);
  38. if (!iounit) {
  39. prom_printf("SUN4D: Cannot alloc iounit, halting.\n");
  40. prom_halt();
  41. }
  42. iounit->limit[0] = IOUNIT_BMAP1_START;
  43. iounit->limit[1] = IOUNIT_BMAP2_START;
  44. iounit->limit[2] = IOUNIT_BMAPM_START;
  45. iounit->limit[3] = IOUNIT_BMAPM_END;
  46. iounit->rotor[1] = IOUNIT_BMAP2_START;
  47. iounit->rotor[2] = IOUNIT_BMAPM_START;
  48. xpt = of_ioremap(&op->resource[2], 0, PAGE_SIZE * 16, "XPT");
  49. if (!xpt) {
  50. prom_printf("SUN4D: Cannot map External Page Table.");
  51. prom_halt();
  52. }
  53. op->dev.archdata.iommu = iounit;
  54. iounit->page_table = xpt;
  55. spin_lock_init(&iounit->lock);
  56. for (xptend = iounit->page_table + (16 * PAGE_SIZE) / sizeof(iopte_t);
  57. xpt < xptend;)
  58. iopte_val(*xpt++) = 0;
  59. }
  60. static int __init iounit_init(void)
  61. {
  62. extern void sun4d_init_sbi_irq(void);
  63. struct device_node *dp;
  64. for_each_node_by_name(dp, "sbi") {
  65. struct platform_device *op = of_find_device_by_node(dp);
  66. iounit_iommu_init(op);
  67. of_propagate_archdata(op);
  68. }
  69. sun4d_init_sbi_irq();
  70. return 0;
  71. }
  72. subsys_initcall(iounit_init);
  73. /* One has to hold iounit->lock to call this */
  74. static unsigned long iounit_get_area(struct iounit_struct *iounit, unsigned long vaddr, int size)
  75. {
  76. int i, j, k, npages;
  77. unsigned long rotor, scan, limit;
  78. iopte_t iopte;
  79. npages = ((vaddr & ~PAGE_MASK) + size + (PAGE_SIZE-1)) >> PAGE_SHIFT;
  80. /* A tiny bit of magic ingredience :) */
  81. switch (npages) {
  82. case 1: i = 0x0231; break;
  83. case 2: i = 0x0132; break;
  84. default: i = 0x0213; break;
  85. }
  86. IOD(("iounit_get_area(%08lx,%d[%d])=", vaddr, size, npages));
  87. next: j = (i & 15);
  88. rotor = iounit->rotor[j - 1];
  89. limit = iounit->limit[j];
  90. scan = rotor;
  91. nexti: scan = find_next_zero_bit(iounit->bmap, limit, scan);
  92. if (scan + npages > limit) {
  93. if (limit != rotor) {
  94. limit = rotor;
  95. scan = iounit->limit[j - 1];
  96. goto nexti;
  97. }
  98. i >>= 4;
  99. if (!(i & 15))
  100. panic("iounit_get_area: Couldn't find free iopte slots for (%08lx,%d)\n", vaddr, size);
  101. goto next;
  102. }
  103. for (k = 1, scan++; k < npages; k++)
  104. if (test_bit(scan++, iounit->bmap))
  105. goto nexti;
  106. iounit->rotor[j - 1] = (scan < limit) ? scan : iounit->limit[j - 1];
  107. scan -= npages;
  108. iopte = MKIOPTE(__pa(vaddr & PAGE_MASK));
  109. vaddr = IOUNIT_DMA_BASE + (scan << PAGE_SHIFT) + (vaddr & ~PAGE_MASK);
  110. for (k = 0; k < npages; k++, iopte = __iopte(iopte_val(iopte) + 0x100), scan++) {
  111. set_bit(scan, iounit->bmap);
  112. iounit->page_table[scan] = iopte;
  113. }
  114. IOD(("%08lx\n", vaddr));
  115. return vaddr;
  116. }
  117. static __u32 iounit_get_scsi_one(struct device *dev, char *vaddr, unsigned long len)
  118. {
  119. struct iounit_struct *iounit = dev->archdata.iommu;
  120. unsigned long ret, flags;
  121. spin_lock_irqsave(&iounit->lock, flags);
  122. ret = iounit_get_area(iounit, (unsigned long)vaddr, len);
  123. spin_unlock_irqrestore(&iounit->lock, flags);
  124. return ret;
  125. }
  126. static void iounit_get_scsi_sgl(struct device *dev, struct scatterlist *sg, int sz)
  127. {
  128. struct iounit_struct *iounit = dev->archdata.iommu;
  129. unsigned long flags;
  130. /* FIXME: Cache some resolved pages - often several sg entries are to the same page */
  131. spin_lock_irqsave(&iounit->lock, flags);
  132. while (sz != 0) {
  133. --sz;
  134. sg->dma_address = iounit_get_area(iounit, (unsigned long) sg_virt(sg), sg->length);
  135. sg->dma_length = sg->length;
  136. sg = sg_next(sg);
  137. }
  138. spin_unlock_irqrestore(&iounit->lock, flags);
  139. }
  140. static void iounit_release_scsi_one(struct device *dev, __u32 vaddr, unsigned long len)
  141. {
  142. struct iounit_struct *iounit = dev->archdata.iommu;
  143. unsigned long flags;
  144. spin_lock_irqsave(&iounit->lock, flags);
  145. len = ((vaddr & ~PAGE_MASK) + len + (PAGE_SIZE-1)) >> PAGE_SHIFT;
  146. vaddr = (vaddr - IOUNIT_DMA_BASE) >> PAGE_SHIFT;
  147. IOD(("iounit_release %08lx-%08lx\n", (long)vaddr, (long)len+vaddr));
  148. for (len += vaddr; vaddr < len; vaddr++)
  149. clear_bit(vaddr, iounit->bmap);
  150. spin_unlock_irqrestore(&iounit->lock, flags);
  151. }
  152. static void iounit_release_scsi_sgl(struct device *dev, struct scatterlist *sg, int sz)
  153. {
  154. struct iounit_struct *iounit = dev->archdata.iommu;
  155. unsigned long flags;
  156. unsigned long vaddr, len;
  157. spin_lock_irqsave(&iounit->lock, flags);
  158. while (sz != 0) {
  159. --sz;
  160. len = ((sg->dma_address & ~PAGE_MASK) + sg->length + (PAGE_SIZE-1)) >> PAGE_SHIFT;
  161. vaddr = (sg->dma_address - IOUNIT_DMA_BASE) >> PAGE_SHIFT;
  162. IOD(("iounit_release %08lx-%08lx\n", (long)vaddr, (long)len+vaddr));
  163. for (len += vaddr; vaddr < len; vaddr++)
  164. clear_bit(vaddr, iounit->bmap);
  165. sg = sg_next(sg);
  166. }
  167. spin_unlock_irqrestore(&iounit->lock, flags);
  168. }
  169. #ifdef CONFIG_SBUS
  170. static int iounit_map_dma_area(struct device *dev, dma_addr_t *pba, unsigned long va, __u32 addr, int len)
  171. {
  172. struct iounit_struct *iounit = dev->archdata.iommu;
  173. unsigned long page, end;
  174. pgprot_t dvma_prot;
  175. iopte_t *iopte;
  176. *pba = addr;
  177. dvma_prot = __pgprot(SRMMU_CACHE | SRMMU_ET_PTE | SRMMU_PRIV);
  178. end = PAGE_ALIGN((addr + len));
  179. while(addr < end) {
  180. page = va;
  181. {
  182. pgd_t *pgdp;
  183. pmd_t *pmdp;
  184. pte_t *ptep;
  185. long i;
  186. pgdp = pgd_offset(&init_mm, addr);
  187. pmdp = pmd_offset(pgdp, addr);
  188. ptep = pte_offset_map(pmdp, addr);
  189. set_pte(ptep, mk_pte(virt_to_page(page), dvma_prot));
  190. i = ((addr - IOUNIT_DMA_BASE) >> PAGE_SHIFT);
  191. iopte = (iopte_t *)(iounit->page_table + i);
  192. *iopte = MKIOPTE(__pa(page));
  193. }
  194. addr += PAGE_SIZE;
  195. va += PAGE_SIZE;
  196. }
  197. flush_cache_all();
  198. flush_tlb_all();
  199. return 0;
  200. }
  201. static void iounit_unmap_dma_area(struct device *dev, unsigned long addr, int len)
  202. {
  203. /* XXX Somebody please fill this in */
  204. }
  205. #endif
  206. static char *iounit_lockarea(char *vaddr, unsigned long len)
  207. {
  208. /* FIXME: Write this */
  209. return vaddr;
  210. }
  211. static void iounit_unlockarea(char *vaddr, unsigned long len)
  212. {
  213. /* FIXME: Write this */
  214. }
  215. void __init ld_mmu_iounit(void)
  216. {
  217. BTFIXUPSET_CALL(mmu_lockarea, iounit_lockarea, BTFIXUPCALL_RETO0);
  218. BTFIXUPSET_CALL(mmu_unlockarea, iounit_unlockarea, BTFIXUPCALL_NOP);
  219. BTFIXUPSET_CALL(mmu_get_scsi_one, iounit_get_scsi_one, BTFIXUPCALL_NORM);
  220. BTFIXUPSET_CALL(mmu_get_scsi_sgl, iounit_get_scsi_sgl, BTFIXUPCALL_NORM);
  221. BTFIXUPSET_CALL(mmu_release_scsi_one, iounit_release_scsi_one, BTFIXUPCALL_NORM);
  222. BTFIXUPSET_CALL(mmu_release_scsi_sgl, iounit_release_scsi_sgl, BTFIXUPCALL_NORM);
  223. #ifdef CONFIG_SBUS
  224. BTFIXUPSET_CALL(mmu_map_dma_area, iounit_map_dma_area, BTFIXUPCALL_NORM);
  225. BTFIXUPSET_CALL(mmu_unmap_dma_area, iounit_unmap_dma_area, BTFIXUPCALL_NORM);
  226. #endif
  227. }