blob: f311bf2190165589977b63581a35dde81b93fe9c [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>
David S. Miller9dc69232008-08-27 19:54:01 -070015#include <linux/of.h>
16#include <linux/of_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <asm/pgalloc.h>
19#include <asm/pgtable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <asm/io.h>
21#include <asm/io-unit.h>
22#include <asm/mxcc.h>
23#include <asm/cacheflush.h>
24#include <asm/tlbflush.h>
25#include <asm/dma.h>
David S. Millerd4accd62006-11-30 17:11:26 -080026#include <asm/oplib.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Sam Ravnborg19186602014-05-16 23:25:41 +020028#include "mm_32.h"
29
Linus Torvalds1da177e2005-04-16 15:20:36 -070030/* #define IOUNIT_DEBUG */
31#ifdef IOUNIT_DEBUG
32#define IOD(x) printk(x)
33#else
34#define IOD(x) do { } while (0)
35#endif
36
37#define IOPERM (IOUPTE_CACHE | IOUPTE_WRITE | IOUPTE_VALID)
38#define MKIOPTE(phys) __iopte((((phys)>>4) & IOUPTE_PAGE) | IOPERM)
39
Grant Likelycd4cd732010-07-22 16:04:30 -060040static void __init iounit_iommu_init(struct platform_device *op)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041{
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 struct iounit_struct *iounit;
Sam Ravnborg19186602014-05-16 23:25:41 +020043 iopte_t __iomem *xpt;
44 iopte_t __iomem *xptend;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Yan Burmanc80892d2006-11-30 17:07:04 -080046 iounit = kzalloc(sizeof(struct iounit_struct), GFP_ATOMIC);
David S. Millerd4accd62006-11-30 17:11:26 -080047 if (!iounit) {
48 prom_printf("SUN4D: Cannot alloc iounit, halting.\n");
49 prom_halt();
50 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 iounit->limit[0] = IOUNIT_BMAP1_START;
53 iounit->limit[1] = IOUNIT_BMAP2_START;
54 iounit->limit[2] = IOUNIT_BMAPM_START;
55 iounit->limit[3] = IOUNIT_BMAPM_END;
56 iounit->rotor[1] = IOUNIT_BMAP2_START;
57 iounit->rotor[2] = IOUNIT_BMAPM_START;
58
David S. Millere0039342008-08-25 22:47:20 -070059 xpt = of_ioremap(&op->resource[2], 0, PAGE_SIZE * 16, "XPT");
60 if (!xpt) {
61 prom_printf("SUN4D: Cannot map External Page Table.");
62 prom_halt();
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
David S. Millere0039342008-08-25 22:47:20 -070065 op->dev.archdata.iommu = iounit;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 iounit->page_table = xpt;
Raymond Burns2f72ba42006-07-17 21:40:27 -070067 spin_lock_init(&iounit->lock);
Sam Ravnborg19186602014-05-16 23:25:41 +020068
69 xptend = iounit->page_table + (16 * PAGE_SIZE) / sizeof(iopte_t);
70 for (; xpt < xptend; xpt++)
71 sbus_writel(0, xpt);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072}
73
David S. Miller046e26a2008-08-27 04:54:04 -070074static int __init iounit_init(void)
75{
76 extern void sun4d_init_sbi_irq(void);
77 struct device_node *dp;
78
79 for_each_node_by_name(dp, "sbi") {
Grant Likelycd4cd732010-07-22 16:04:30 -060080 struct platform_device *op = of_find_device_by_node(dp);
David S. Miller046e26a2008-08-27 04:54:04 -070081
82 iounit_iommu_init(op);
83 of_propagate_archdata(op);
84 }
85
86 sun4d_init_sbi_irq();
87
88 return 0;
89}
90
91subsys_initcall(iounit_init);
92
Linus Torvalds1da177e2005-04-16 15:20:36 -070093/* One has to hold iounit->lock to call this */
94static unsigned long iounit_get_area(struct iounit_struct *iounit, unsigned long vaddr, int size)
95{
96 int i, j, k, npages;
97 unsigned long rotor, scan, limit;
98 iopte_t iopte;
99
100 npages = ((vaddr & ~PAGE_MASK) + size + (PAGE_SIZE-1)) >> PAGE_SHIFT;
101
102 /* A tiny bit of magic ingredience :) */
103 switch (npages) {
104 case 1: i = 0x0231; break;
105 case 2: i = 0x0132; break;
106 default: i = 0x0213; break;
107 }
108
109 IOD(("iounit_get_area(%08lx,%d[%d])=", vaddr, size, npages));
110
111next: j = (i & 15);
112 rotor = iounit->rotor[j - 1];
113 limit = iounit->limit[j];
114 scan = rotor;
115nexti: scan = find_next_zero_bit(iounit->bmap, limit, scan);
116 if (scan + npages > limit) {
117 if (limit != rotor) {
118 limit = rotor;
119 scan = iounit->limit[j - 1];
120 goto nexti;
121 }
122 i >>= 4;
123 if (!(i & 15))
124 panic("iounit_get_area: Couldn't find free iopte slots for (%08lx,%d)\n", vaddr, size);
125 goto next;
126 }
127 for (k = 1, scan++; k < npages; k++)
128 if (test_bit(scan++, iounit->bmap))
129 goto nexti;
130 iounit->rotor[j - 1] = (scan < limit) ? scan : iounit->limit[j - 1];
131 scan -= npages;
132 iopte = MKIOPTE(__pa(vaddr & PAGE_MASK));
133 vaddr = IOUNIT_DMA_BASE + (scan << PAGE_SHIFT) + (vaddr & ~PAGE_MASK);
134 for (k = 0; k < npages; k++, iopte = __iopte(iopte_val(iopte) + 0x100), scan++) {
135 set_bit(scan, iounit->bmap);
Sam Ravnborg19186602014-05-16 23:25:41 +0200136 sbus_writel(iopte, &iounit->page_table[scan]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 }
138 IOD(("%08lx\n", vaddr));
139 return vaddr;
140}
141
David S. Miller260489f2008-08-26 23:00:58 -0700142static __u32 iounit_get_scsi_one(struct device *dev, char *vaddr, unsigned long len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
David S. Miller260489f2008-08-26 23:00:58 -0700144 struct iounit_struct *iounit = dev->archdata.iommu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 unsigned long ret, flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
147 spin_lock_irqsave(&iounit->lock, flags);
148 ret = iounit_get_area(iounit, (unsigned long)vaddr, len);
149 spin_unlock_irqrestore(&iounit->lock, flags);
150 return ret;
151}
152
David S. Miller260489f2008-08-26 23:00:58 -0700153static void iounit_get_scsi_sgl(struct device *dev, struct scatterlist *sg, int sz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
David S. Miller260489f2008-08-26 23:00:58 -0700155 struct iounit_struct *iounit = dev->archdata.iommu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
158 /* FIXME: Cache some resolved pages - often several sg entries are to the same page */
159 spin_lock_irqsave(&iounit->lock, flags);
160 while (sz != 0) {
161 --sz;
Robert Reifaa83a262008-12-11 20:24:58 -0800162 sg->dma_address = iounit_get_area(iounit, (unsigned long) sg_virt(sg), sg->length);
163 sg->dma_length = sg->length;
Jens Axboe0912a5d2007-05-14 15:44:38 +0200164 sg = sg_next(sg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 }
166 spin_unlock_irqrestore(&iounit->lock, flags);
167}
168
David S. Miller260489f2008-08-26 23:00:58 -0700169static void iounit_release_scsi_one(struct device *dev, __u32 vaddr, unsigned long len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170{
David S. Miller260489f2008-08-26 23:00:58 -0700171 struct iounit_struct *iounit = dev->archdata.iommu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174 spin_lock_irqsave(&iounit->lock, flags);
175 len = ((vaddr & ~PAGE_MASK) + len + (PAGE_SIZE-1)) >> PAGE_SHIFT;
176 vaddr = (vaddr - IOUNIT_DMA_BASE) >> PAGE_SHIFT;
177 IOD(("iounit_release %08lx-%08lx\n", (long)vaddr, (long)len+vaddr));
178 for (len += vaddr; vaddr < len; vaddr++)
179 clear_bit(vaddr, iounit->bmap);
180 spin_unlock_irqrestore(&iounit->lock, flags);
181}
182
David S. Miller260489f2008-08-26 23:00:58 -0700183static void iounit_release_scsi_sgl(struct device *dev, struct scatterlist *sg, int sz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
David S. Miller260489f2008-08-26 23:00:58 -0700185 struct iounit_struct *iounit = dev->archdata.iommu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 unsigned long flags;
187 unsigned long vaddr, len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
189 spin_lock_irqsave(&iounit->lock, flags);
190 while (sz != 0) {
191 --sz;
Robert Reifaa83a262008-12-11 20:24:58 -0800192 len = ((sg->dma_address & ~PAGE_MASK) + sg->length + (PAGE_SIZE-1)) >> PAGE_SHIFT;
193 vaddr = (sg->dma_address - IOUNIT_DMA_BASE) >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 IOD(("iounit_release %08lx-%08lx\n", (long)vaddr, (long)len+vaddr));
195 for (len += vaddr; vaddr < len; vaddr++)
196 clear_bit(vaddr, iounit->bmap);
Jens Axboe0912a5d2007-05-14 15:44:38 +0200197 sg = sg_next(sg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 }
199 spin_unlock_irqrestore(&iounit->lock, flags);
200}
201
202#ifdef CONFIG_SBUS
David S. Millerd894d962012-05-13 13:57:05 -0700203static int iounit_map_dma_area(struct device *dev, dma_addr_t *pba, unsigned long va, unsigned long addr, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
David S. Miller4b1c5df2008-08-27 18:40:38 -0700205 struct iounit_struct *iounit = dev->archdata.iommu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 unsigned long page, end;
207 pgprot_t dvma_prot;
Sam Ravnborg19186602014-05-16 23:25:41 +0200208 iopte_t __iomem *iopte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
210 *pba = addr;
211
212 dvma_prot = __pgprot(SRMMU_CACHE | SRMMU_ET_PTE | SRMMU_PRIV);
213 end = PAGE_ALIGN((addr + len));
214 while(addr < end) {
215 page = va;
216 {
217 pgd_t *pgdp;
218 pmd_t *pmdp;
219 pte_t *ptep;
220 long i;
221
222 pgdp = pgd_offset(&init_mm, addr);
223 pmdp = pmd_offset(pgdp, addr);
224 ptep = pte_offset_map(pmdp, addr);
225
226 set_pte(ptep, mk_pte(virt_to_page(page), dvma_prot));
227
228 i = ((addr - IOUNIT_DMA_BASE) >> PAGE_SHIFT);
229
Sam Ravnborg19186602014-05-16 23:25:41 +0200230 iopte = iounit->page_table + i;
231 sbus_writel(MKIOPTE(__pa(page)), iopte);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 }
233 addr += PAGE_SIZE;
234 va += PAGE_SIZE;
235 }
236 flush_cache_all();
237 flush_tlb_all();
238
239 return 0;
240}
241
David S. Miller4b1c5df2008-08-27 18:40:38 -0700242static void iounit_unmap_dma_area(struct device *dev, unsigned long addr, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243{
244 /* XXX Somebody please fill this in */
245}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246#endif
247
David S. Millerd894d962012-05-13 13:57:05 -0700248static const struct sparc32_dma_ops iounit_dma_ops = {
249 .get_scsi_one = iounit_get_scsi_one,
250 .get_scsi_sgl = iounit_get_scsi_sgl,
251 .release_scsi_one = iounit_release_scsi_one,
252 .release_scsi_sgl = iounit_release_scsi_sgl,
253#ifdef CONFIG_SBUS
254 .map_dma_area = iounit_map_dma_area,
255 .unmap_dma_area = iounit_unmap_dma_area,
256#endif
257};
258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259void __init ld_mmu_iounit(void)
260{
David S. Millerd894d962012-05-13 13:57:05 -0700261 sparc32_dma_ops = &iounit_dma_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262}