io-unit.c 6.9 KB

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