blob: caf551ad9d8c647c216d48dd161a2de40c3970fd [file] [log] [blame]
Adrian Bunk88278ca2008-05-19 16:53:02 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#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>
Jens Axboe0912a5d2007-05-14 15:44:38 +020014#include <linux/scatterlist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <asm/pgalloc.h>
17#include <asm/pgtable.h>
18#include <asm/sbus.h>
19#include <asm/io.h>
20#include <asm/io-unit.h>
21#include <asm/mxcc.h>
22#include <asm/cacheflush.h>
23#include <asm/tlbflush.h>
24#include <asm/dma.h>
David S. Millerd4accd62006-11-30 17:11:26 -080025#include <asm/oplib.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27/* #define IOUNIT_DEBUG */
28#ifdef IOUNIT_DEBUG
29#define IOD(x) printk(x)
30#else
31#define IOD(x) do { } while (0)
32#endif
33
34#define IOPERM (IOUPTE_CACHE | IOUPTE_WRITE | IOUPTE_VALID)
35#define MKIOPTE(phys) __iopte((((phys)>>4) & IOUPTE_PAGE) | IOPERM)
36
David S. Miller046e26a2008-08-27 04:54:04 -070037static void __init iounit_iommu_init(struct of_device *op)
Linus Torvalds1da177e2005-04-16 15:20:36 -070038{
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 struct iounit_struct *iounit;
David S. Millere0039342008-08-25 22:47:20 -070040 iopte_t *xpt, *xptend;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Yan Burmanc80892d2006-11-30 17:07:04 -080042 iounit = kzalloc(sizeof(struct iounit_struct), GFP_ATOMIC);
David S. Millerd4accd62006-11-30 17:11:26 -080043 if (!iounit) {
44 prom_printf("SUN4D: Cannot alloc iounit, halting.\n");
45 prom_halt();
46 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 iounit->limit[0] = IOUNIT_BMAP1_START;
49 iounit->limit[1] = IOUNIT_BMAP2_START;
50 iounit->limit[2] = IOUNIT_BMAPM_START;
51 iounit->limit[3] = IOUNIT_BMAPM_END;
52 iounit->rotor[1] = IOUNIT_BMAP2_START;
53 iounit->rotor[2] = IOUNIT_BMAPM_START;
54
David S. Millere0039342008-08-25 22:47:20 -070055 xpt = of_ioremap(&op->resource[2], 0, PAGE_SIZE * 16, "XPT");
56 if (!xpt) {
57 prom_printf("SUN4D: Cannot map External Page Table.");
58 prom_halt();
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
David S. Millere0039342008-08-25 22:47:20 -070061 op->dev.archdata.iommu = iounit;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 iounit->page_table = xpt;
Raymond Burns2f72ba42006-07-17 21:40:27 -070063 spin_lock_init(&iounit->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65 for (xptend = iounit->page_table + (16 * PAGE_SIZE) / sizeof(iopte_t);
66 xpt < xptend;)
67 iopte_val(*xpt++) = 0;
68}
69
David S. Miller046e26a2008-08-27 04:54:04 -070070static int __init iounit_init(void)
71{
72 extern void sun4d_init_sbi_irq(void);
73 struct device_node *dp;
74
75 for_each_node_by_name(dp, "sbi") {
76 struct of_device *op = of_find_device_by_node(dp);
77
78 iounit_iommu_init(op);
79 of_propagate_archdata(op);
80 }
81
82 sun4d_init_sbi_irq();
83
84 return 0;
85}
86
87subsys_initcall(iounit_init);
88
Linus Torvalds1da177e2005-04-16 15:20:36 -070089/* One has to hold iounit->lock to call this */
90static unsigned long iounit_get_area(struct iounit_struct *iounit, unsigned long vaddr, int size)
91{
92 int i, j, k, npages;
93 unsigned long rotor, scan, limit;
94 iopte_t iopte;
95
96 npages = ((vaddr & ~PAGE_MASK) + size + (PAGE_SIZE-1)) >> PAGE_SHIFT;
97
98 /* A tiny bit of magic ingredience :) */
99 switch (npages) {
100 case 1: i = 0x0231; break;
101 case 2: i = 0x0132; break;
102 default: i = 0x0213; break;
103 }
104
105 IOD(("iounit_get_area(%08lx,%d[%d])=", vaddr, size, npages));
106
107next: j = (i & 15);
108 rotor = iounit->rotor[j - 1];
109 limit = iounit->limit[j];
110 scan = rotor;
111nexti: scan = find_next_zero_bit(iounit->bmap, limit, scan);
112 if (scan + npages > limit) {
113 if (limit != rotor) {
114 limit = rotor;
115 scan = iounit->limit[j - 1];
116 goto nexti;
117 }
118 i >>= 4;
119 if (!(i & 15))
120 panic("iounit_get_area: Couldn't find free iopte slots for (%08lx,%d)\n", vaddr, size);
121 goto next;
122 }
123 for (k = 1, scan++; k < npages; k++)
124 if (test_bit(scan++, iounit->bmap))
125 goto nexti;
126 iounit->rotor[j - 1] = (scan < limit) ? scan : iounit->limit[j - 1];
127 scan -= npages;
128 iopte = MKIOPTE(__pa(vaddr & PAGE_MASK));
129 vaddr = IOUNIT_DMA_BASE + (scan << PAGE_SHIFT) + (vaddr & ~PAGE_MASK);
130 for (k = 0; k < npages; k++, iopte = __iopte(iopte_val(iopte) + 0x100), scan++) {
131 set_bit(scan, iounit->bmap);
132 iounit->page_table[scan] = iopte;
133 }
134 IOD(("%08lx\n", vaddr));
135 return vaddr;
136}
137
David S. Miller260489f2008-08-26 23:00:58 -0700138static __u32 iounit_get_scsi_one(struct device *dev, char *vaddr, unsigned long len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
David S. Miller260489f2008-08-26 23:00:58 -0700140 struct iounit_struct *iounit = dev->archdata.iommu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 unsigned long ret, flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
143 spin_lock_irqsave(&iounit->lock, flags);
144 ret = iounit_get_area(iounit, (unsigned long)vaddr, len);
145 spin_unlock_irqrestore(&iounit->lock, flags);
146 return ret;
147}
148
David S. Miller260489f2008-08-26 23:00:58 -0700149static void iounit_get_scsi_sgl(struct device *dev, struct scatterlist *sg, int sz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150{
David S. Miller260489f2008-08-26 23:00:58 -0700151 struct iounit_struct *iounit = dev->archdata.iommu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
154 /* FIXME: Cache some resolved pages - often several sg entries are to the same page */
155 spin_lock_irqsave(&iounit->lock, flags);
156 while (sz != 0) {
157 --sz;
David S. Miller6ee4e282007-10-27 00:54:14 -0700158 sg->dvma_address = iounit_get_area(iounit, (unsigned long) sg_virt(sg), sg->length);
Jens Axboe0912a5d2007-05-14 15:44:38 +0200159 sg->dvma_length = sg->length;
160 sg = sg_next(sg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 }
162 spin_unlock_irqrestore(&iounit->lock, flags);
163}
164
David S. Miller260489f2008-08-26 23:00:58 -0700165static void iounit_release_scsi_one(struct device *dev, __u32 vaddr, unsigned long len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
David S. Miller260489f2008-08-26 23:00:58 -0700167 struct iounit_struct *iounit = dev->archdata.iommu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
170 spin_lock_irqsave(&iounit->lock, flags);
171 len = ((vaddr & ~PAGE_MASK) + len + (PAGE_SIZE-1)) >> PAGE_SHIFT;
172 vaddr = (vaddr - IOUNIT_DMA_BASE) >> PAGE_SHIFT;
173 IOD(("iounit_release %08lx-%08lx\n", (long)vaddr, (long)len+vaddr));
174 for (len += vaddr; vaddr < len; vaddr++)
175 clear_bit(vaddr, iounit->bmap);
176 spin_unlock_irqrestore(&iounit->lock, flags);
177}
178
David S. Miller260489f2008-08-26 23:00:58 -0700179static void iounit_release_scsi_sgl(struct device *dev, struct scatterlist *sg, int sz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
David S. Miller260489f2008-08-26 23:00:58 -0700181 struct iounit_struct *iounit = dev->archdata.iommu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 unsigned long flags;
183 unsigned long vaddr, len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
185 spin_lock_irqsave(&iounit->lock, flags);
186 while (sz != 0) {
187 --sz;
Jens Axboe0912a5d2007-05-14 15:44:38 +0200188 len = ((sg->dvma_address & ~PAGE_MASK) + sg->length + (PAGE_SIZE-1)) >> PAGE_SHIFT;
189 vaddr = (sg->dvma_address - IOUNIT_DMA_BASE) >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 IOD(("iounit_release %08lx-%08lx\n", (long)vaddr, (long)len+vaddr));
191 for (len += vaddr; vaddr < len; vaddr++)
192 clear_bit(vaddr, iounit->bmap);
Jens Axboe0912a5d2007-05-14 15:44:38 +0200193 sg = sg_next(sg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 }
195 spin_unlock_irqrestore(&iounit->lock, flags);
196}
197
198#ifdef CONFIG_SBUS
David S. Miller4b1c5df2008-08-27 18:40:38 -0700199static int iounit_map_dma_area(struct device *dev, dma_addr_t *pba, unsigned long va, __u32 addr, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
David S. Miller4b1c5df2008-08-27 18:40:38 -0700201 struct iounit_struct *iounit = dev->archdata.iommu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 unsigned long page, end;
203 pgprot_t dvma_prot;
204 iopte_t *iopte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
206 *pba = addr;
207
208 dvma_prot = __pgprot(SRMMU_CACHE | SRMMU_ET_PTE | SRMMU_PRIV);
209 end = PAGE_ALIGN((addr + len));
210 while(addr < end) {
211 page = va;
212 {
213 pgd_t *pgdp;
214 pmd_t *pmdp;
215 pte_t *ptep;
216 long i;
217
218 pgdp = pgd_offset(&init_mm, addr);
219 pmdp = pmd_offset(pgdp, addr);
220 ptep = pte_offset_map(pmdp, addr);
221
222 set_pte(ptep, mk_pte(virt_to_page(page), dvma_prot));
223
224 i = ((addr - IOUNIT_DMA_BASE) >> PAGE_SHIFT);
225
David S. Miller4b1c5df2008-08-27 18:40:38 -0700226 iopte = (iopte_t *)(iounit->page_table + i);
227 *iopte = MKIOPTE(__pa(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 }
229 addr += PAGE_SIZE;
230 va += PAGE_SIZE;
231 }
232 flush_cache_all();
233 flush_tlb_all();
234
235 return 0;
236}
237
David S. Miller4b1c5df2008-08-27 18:40:38 -0700238static void iounit_unmap_dma_area(struct device *dev, unsigned long addr, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239{
240 /* XXX Somebody please fill this in */
241}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242#endif
243
244static char *iounit_lockarea(char *vaddr, unsigned long len)
245{
246/* FIXME: Write this */
247 return vaddr;
248}
249
250static void iounit_unlockarea(char *vaddr, unsigned long len)
251{
252/* FIXME: Write this */
253}
254
255void __init ld_mmu_iounit(void)
256{
257 BTFIXUPSET_CALL(mmu_lockarea, iounit_lockarea, BTFIXUPCALL_RETO0);
258 BTFIXUPSET_CALL(mmu_unlockarea, iounit_unlockarea, BTFIXUPCALL_NOP);
259
260 BTFIXUPSET_CALL(mmu_get_scsi_one, iounit_get_scsi_one, BTFIXUPCALL_NORM);
261 BTFIXUPSET_CALL(mmu_get_scsi_sgl, iounit_get_scsi_sgl, BTFIXUPCALL_NORM);
262 BTFIXUPSET_CALL(mmu_release_scsi_one, iounit_release_scsi_one, BTFIXUPCALL_NORM);
263 BTFIXUPSET_CALL(mmu_release_scsi_sgl, iounit_release_scsi_sgl, BTFIXUPCALL_NORM);
264
265#ifdef CONFIG_SBUS
266 BTFIXUPSET_CALL(mmu_map_dma_area, iounit_map_dma_area, BTFIXUPCALL_NORM);
267 BTFIXUPSET_CALL(mmu_unmap_dma_area, iounit_unmap_dma_area, BTFIXUPCALL_NORM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268#endif
269}
270
271__u32 iounit_map_dma_init(struct sbus_bus *sbus, int size)
272{
273 int i, j, k, npages;
274 unsigned long rotor, scan, limit;
275 unsigned long flags;
276 __u32 ret;
Robert Reif3ac4c942007-08-10 15:52:06 -0700277 struct iounit_struct *iounit = sbus->ofdev.dev.archdata.iommu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279 npages = (size + (PAGE_SIZE-1)) >> PAGE_SHIFT;
280 i = 0x0213;
281 spin_lock_irqsave(&iounit->lock, flags);
282next: j = (i & 15);
283 rotor = iounit->rotor[j - 1];
284 limit = iounit->limit[j];
285 scan = rotor;
286nexti: scan = find_next_zero_bit(iounit->bmap, limit, scan);
287 if (scan + npages > limit) {
288 if (limit != rotor) {
289 limit = rotor;
290 scan = iounit->limit[j - 1];
291 goto nexti;
292 }
293 i >>= 4;
294 if (!(i & 15))
295 panic("iounit_map_dma_init: Couldn't find free iopte slots for %d bytes\n", size);
296 goto next;
297 }
298 for (k = 1, scan++; k < npages; k++)
299 if (test_bit(scan++, iounit->bmap))
300 goto nexti;
301 iounit->rotor[j - 1] = (scan < limit) ? scan : iounit->limit[j - 1];
302 scan -= npages;
303 ret = IOUNIT_DMA_BASE + (scan << PAGE_SHIFT);
304 for (k = 0; k < npages; k++, scan++)
305 set_bit(scan, iounit->bmap);
306 spin_unlock_irqrestore(&iounit->lock, flags);
307 return ret;
308}
309
310__u32 iounit_map_dma_page(__u32 vaddr, void *addr, struct sbus_bus *sbus)
311{
312 int scan = (vaddr - IOUNIT_DMA_BASE) >> PAGE_SHIFT;
Robert Reif3ac4c942007-08-10 15:52:06 -0700313 struct iounit_struct *iounit = sbus->ofdev.dev.archdata.iommu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
315 iounit->page_table[scan] = MKIOPTE(__pa(((unsigned long)addr) & PAGE_MASK));
316 return vaddr + (((unsigned long)addr) & ~PAGE_MASK);
317}