blob: 1093514a57732edc9c4ce5fa2c3fe1f36b55cd74 [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. Millere0039342008-08-25 22:47:20 -070037void __init iounit_init(struct sbus_bus *sbus)
Linus Torvalds1da177e2005-04-16 15:20:36 -070038{
David S. Millere0039342008-08-25 22:47:20 -070039 struct device_node *dp = sbus->ofdev.node;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 struct iounit_struct *iounit;
David S. Millere0039342008-08-25 22:47:20 -070041 iopte_t *xpt, *xptend;
42 struct of_device *op;
43
44 op = of_find_device_by_node(dp);
45 if (!op) {
46 prom_printf("SUN4D: Cannot find SBI of_device.\n");
47 prom_halt();
48 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Yan Burmanc80892d2006-11-30 17:07:04 -080050 iounit = kzalloc(sizeof(struct iounit_struct), GFP_ATOMIC);
David S. Millerd4accd62006-11-30 17:11:26 -080051 if (!iounit) {
52 prom_printf("SUN4D: Cannot alloc iounit, halting.\n");
53 prom_halt();
54 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 iounit->limit[0] = IOUNIT_BMAP1_START;
57 iounit->limit[1] = IOUNIT_BMAP2_START;
58 iounit->limit[2] = IOUNIT_BMAPM_START;
59 iounit->limit[3] = IOUNIT_BMAPM_END;
60 iounit->rotor[1] = IOUNIT_BMAP2_START;
61 iounit->rotor[2] = IOUNIT_BMAPM_START;
62
David S. Millere0039342008-08-25 22:47:20 -070063 xpt = of_ioremap(&op->resource[2], 0, PAGE_SIZE * 16, "XPT");
64 if (!xpt) {
65 prom_printf("SUN4D: Cannot map External Page Table.");
66 prom_halt();
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Robert Reif3ac4c942007-08-10 15:52:06 -070069 sbus->ofdev.dev.archdata.iommu = iounit;
David S. Millere0039342008-08-25 22:47:20 -070070 op->dev.archdata.iommu = iounit;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 iounit->page_table = xpt;
Raymond Burns2f72ba42006-07-17 21:40:27 -070072 spin_lock_init(&iounit->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74 for (xptend = iounit->page_table + (16 * PAGE_SIZE) / sizeof(iopte_t);
75 xpt < xptend;)
76 iopte_val(*xpt++) = 0;
77}
78
79/* One has to hold iounit->lock to call this */
80static unsigned long iounit_get_area(struct iounit_struct *iounit, unsigned long vaddr, int size)
81{
82 int i, j, k, npages;
83 unsigned long rotor, scan, limit;
84 iopte_t iopte;
85
86 npages = ((vaddr & ~PAGE_MASK) + size + (PAGE_SIZE-1)) >> PAGE_SHIFT;
87
88 /* A tiny bit of magic ingredience :) */
89 switch (npages) {
90 case 1: i = 0x0231; break;
91 case 2: i = 0x0132; break;
92 default: i = 0x0213; break;
93 }
94
95 IOD(("iounit_get_area(%08lx,%d[%d])=", vaddr, size, npages));
96
97next: j = (i & 15);
98 rotor = iounit->rotor[j - 1];
99 limit = iounit->limit[j];
100 scan = rotor;
101nexti: scan = find_next_zero_bit(iounit->bmap, limit, scan);
102 if (scan + npages > limit) {
103 if (limit != rotor) {
104 limit = rotor;
105 scan = iounit->limit[j - 1];
106 goto nexti;
107 }
108 i >>= 4;
109 if (!(i & 15))
110 panic("iounit_get_area: Couldn't find free iopte slots for (%08lx,%d)\n", vaddr, size);
111 goto next;
112 }
113 for (k = 1, scan++; k < npages; k++)
114 if (test_bit(scan++, iounit->bmap))
115 goto nexti;
116 iounit->rotor[j - 1] = (scan < limit) ? scan : iounit->limit[j - 1];
117 scan -= npages;
118 iopte = MKIOPTE(__pa(vaddr & PAGE_MASK));
119 vaddr = IOUNIT_DMA_BASE + (scan << PAGE_SHIFT) + (vaddr & ~PAGE_MASK);
120 for (k = 0; k < npages; k++, iopte = __iopte(iopte_val(iopte) + 0x100), scan++) {
121 set_bit(scan, iounit->bmap);
122 iounit->page_table[scan] = iopte;
123 }
124 IOD(("%08lx\n", vaddr));
125 return vaddr;
126}
127
128static __u32 iounit_get_scsi_one(char *vaddr, unsigned long len, struct sbus_bus *sbus)
129{
130 unsigned long ret, flags;
Robert Reif3ac4c942007-08-10 15:52:06 -0700131 struct iounit_struct *iounit = sbus->ofdev.dev.archdata.iommu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133 spin_lock_irqsave(&iounit->lock, flags);
134 ret = iounit_get_area(iounit, (unsigned long)vaddr, len);
135 spin_unlock_irqrestore(&iounit->lock, flags);
136 return ret;
137}
138
139static void iounit_get_scsi_sgl(struct scatterlist *sg, int sz, struct sbus_bus *sbus)
140{
141 unsigned long flags;
Robert Reif3ac4c942007-08-10 15:52:06 -0700142 struct iounit_struct *iounit = sbus->ofdev.dev.archdata.iommu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144 /* FIXME: Cache some resolved pages - often several sg entries are to the same page */
145 spin_lock_irqsave(&iounit->lock, flags);
146 while (sz != 0) {
147 --sz;
David S. Miller6ee4e282007-10-27 00:54:14 -0700148 sg->dvma_address = iounit_get_area(iounit, (unsigned long) sg_virt(sg), sg->length);
Jens Axboe0912a5d2007-05-14 15:44:38 +0200149 sg->dvma_length = sg->length;
150 sg = sg_next(sg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 }
152 spin_unlock_irqrestore(&iounit->lock, flags);
153}
154
155static void iounit_release_scsi_one(__u32 vaddr, unsigned long len, struct sbus_bus *sbus)
156{
157 unsigned long flags;
Robert Reif3ac4c942007-08-10 15:52:06 -0700158 struct iounit_struct *iounit = sbus->ofdev.dev.archdata.iommu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
160 spin_lock_irqsave(&iounit->lock, flags);
161 len = ((vaddr & ~PAGE_MASK) + len + (PAGE_SIZE-1)) >> PAGE_SHIFT;
162 vaddr = (vaddr - IOUNIT_DMA_BASE) >> PAGE_SHIFT;
163 IOD(("iounit_release %08lx-%08lx\n", (long)vaddr, (long)len+vaddr));
164 for (len += vaddr; vaddr < len; vaddr++)
165 clear_bit(vaddr, iounit->bmap);
166 spin_unlock_irqrestore(&iounit->lock, flags);
167}
168
169static void iounit_release_scsi_sgl(struct scatterlist *sg, int sz, struct sbus_bus *sbus)
170{
171 unsigned long flags;
172 unsigned long vaddr, len;
Robert Reif3ac4c942007-08-10 15:52:06 -0700173 struct iounit_struct *iounit = sbus->ofdev.dev.archdata.iommu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
175 spin_lock_irqsave(&iounit->lock, flags);
176 while (sz != 0) {
177 --sz;
Jens Axboe0912a5d2007-05-14 15:44:38 +0200178 len = ((sg->dvma_address & ~PAGE_MASK) + sg->length + (PAGE_SIZE-1)) >> PAGE_SHIFT;
179 vaddr = (sg->dvma_address - IOUNIT_DMA_BASE) >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 IOD(("iounit_release %08lx-%08lx\n", (long)vaddr, (long)len+vaddr));
181 for (len += vaddr; vaddr < len; vaddr++)
182 clear_bit(vaddr, iounit->bmap);
Jens Axboe0912a5d2007-05-14 15:44:38 +0200183 sg = sg_next(sg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 }
185 spin_unlock_irqrestore(&iounit->lock, flags);
186}
187
188#ifdef CONFIG_SBUS
189static int iounit_map_dma_area(dma_addr_t *pba, unsigned long va, __u32 addr, int len)
190{
191 unsigned long page, end;
192 pgprot_t dvma_prot;
193 iopte_t *iopte;
194 struct sbus_bus *sbus;
195
196 *pba = addr;
197
198 dvma_prot = __pgprot(SRMMU_CACHE | SRMMU_ET_PTE | SRMMU_PRIV);
199 end = PAGE_ALIGN((addr + len));
200 while(addr < end) {
201 page = va;
202 {
203 pgd_t *pgdp;
204 pmd_t *pmdp;
205 pte_t *ptep;
206 long i;
207
208 pgdp = pgd_offset(&init_mm, addr);
209 pmdp = pmd_offset(pgdp, addr);
210 ptep = pte_offset_map(pmdp, addr);
211
212 set_pte(ptep, mk_pte(virt_to_page(page), dvma_prot));
213
214 i = ((addr - IOUNIT_DMA_BASE) >> PAGE_SHIFT);
215
216 for_each_sbus(sbus) {
Robert Reif3ac4c942007-08-10 15:52:06 -0700217 struct iounit_struct *iounit = sbus->ofdev.dev.archdata.iommu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
219 iopte = (iopte_t *)(iounit->page_table + i);
220 *iopte = MKIOPTE(__pa(page));
221 }
222 }
223 addr += PAGE_SIZE;
224 va += PAGE_SIZE;
225 }
226 flush_cache_all();
227 flush_tlb_all();
228
229 return 0;
230}
231
232static void iounit_unmap_dma_area(unsigned long addr, int len)
233{
234 /* XXX Somebody please fill this in */
235}
236
237/* XXX We do not pass sbus device here, bad. */
238static struct page *iounit_translate_dvma(unsigned long addr)
239{
240 struct sbus_bus *sbus = sbus_root; /* They are all the same */
Robert Reif3ac4c942007-08-10 15:52:06 -0700241 struct iounit_struct *iounit = sbus->ofdev.dev.archdata.iommu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 int i;
243 iopte_t *iopte;
244
245 i = ((addr - IOUNIT_DMA_BASE) >> PAGE_SHIFT);
246 iopte = (iopte_t *)(iounit->page_table + i);
247 return pfn_to_page(iopte_val(*iopte) >> (PAGE_SHIFT-4)); /* XXX sun4d guru, help */
248}
249#endif
250
251static char *iounit_lockarea(char *vaddr, unsigned long len)
252{
253/* FIXME: Write this */
254 return vaddr;
255}
256
257static void iounit_unlockarea(char *vaddr, unsigned long len)
258{
259/* FIXME: Write this */
260}
261
262void __init ld_mmu_iounit(void)
263{
264 BTFIXUPSET_CALL(mmu_lockarea, iounit_lockarea, BTFIXUPCALL_RETO0);
265 BTFIXUPSET_CALL(mmu_unlockarea, iounit_unlockarea, BTFIXUPCALL_NOP);
266
267 BTFIXUPSET_CALL(mmu_get_scsi_one, iounit_get_scsi_one, BTFIXUPCALL_NORM);
268 BTFIXUPSET_CALL(mmu_get_scsi_sgl, iounit_get_scsi_sgl, BTFIXUPCALL_NORM);
269 BTFIXUPSET_CALL(mmu_release_scsi_one, iounit_release_scsi_one, BTFIXUPCALL_NORM);
270 BTFIXUPSET_CALL(mmu_release_scsi_sgl, iounit_release_scsi_sgl, BTFIXUPCALL_NORM);
271
272#ifdef CONFIG_SBUS
273 BTFIXUPSET_CALL(mmu_map_dma_area, iounit_map_dma_area, BTFIXUPCALL_NORM);
274 BTFIXUPSET_CALL(mmu_unmap_dma_area, iounit_unmap_dma_area, BTFIXUPCALL_NORM);
275 BTFIXUPSET_CALL(mmu_translate_dvma, iounit_translate_dvma, BTFIXUPCALL_NORM);
276#endif
277}
278
279__u32 iounit_map_dma_init(struct sbus_bus *sbus, int size)
280{
281 int i, j, k, npages;
282 unsigned long rotor, scan, limit;
283 unsigned long flags;
284 __u32 ret;
Robert Reif3ac4c942007-08-10 15:52:06 -0700285 struct iounit_struct *iounit = sbus->ofdev.dev.archdata.iommu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
287 npages = (size + (PAGE_SIZE-1)) >> PAGE_SHIFT;
288 i = 0x0213;
289 spin_lock_irqsave(&iounit->lock, flags);
290next: j = (i & 15);
291 rotor = iounit->rotor[j - 1];
292 limit = iounit->limit[j];
293 scan = rotor;
294nexti: scan = find_next_zero_bit(iounit->bmap, limit, scan);
295 if (scan + npages > limit) {
296 if (limit != rotor) {
297 limit = rotor;
298 scan = iounit->limit[j - 1];
299 goto nexti;
300 }
301 i >>= 4;
302 if (!(i & 15))
303 panic("iounit_map_dma_init: Couldn't find free iopte slots for %d bytes\n", size);
304 goto next;
305 }
306 for (k = 1, scan++; k < npages; k++)
307 if (test_bit(scan++, iounit->bmap))
308 goto nexti;
309 iounit->rotor[j - 1] = (scan < limit) ? scan : iounit->limit[j - 1];
310 scan -= npages;
311 ret = IOUNIT_DMA_BASE + (scan << PAGE_SHIFT);
312 for (k = 0; k < npages; k++, scan++)
313 set_bit(scan, iounit->bmap);
314 spin_unlock_irqrestore(&iounit->lock, flags);
315 return ret;
316}
317
318__u32 iounit_map_dma_page(__u32 vaddr, void *addr, struct sbus_bus *sbus)
319{
320 int scan = (vaddr - IOUNIT_DMA_BASE) >> PAGE_SHIFT;
Robert Reif3ac4c942007-08-10 15:52:06 -0700321 struct iounit_struct *iounit = sbus->ofdev.dev.archdata.iommu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
323 iounit->page_table[scan] = MKIOPTE(__pa(((unsigned long)addr) & PAGE_MASK));
324 return vaddr + (((unsigned long)addr) & ~PAGE_MASK);
325}