1 // SPDX-License-Identifier: GPL-2.0
3 * io-unit.c: IO-UNIT specific routines for memory management.
5 * Copyright (C) 1997,1998 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
8 #include <linux/kernel.h>
9 #include <linux/init.h>
10 #include <linux/slab.h>
11 #include <linux/spinlock.h>
13 #include <linux/bitops.h>
14 #include <linux/dma-map-ops.h>
16 #include <linux/of_device.h>
19 #include <asm/io-unit.h>
21 #include <asm/cacheflush.h>
22 #include <asm/tlbflush.h>
24 #include <asm/oplib.h>
28 /* #define IOUNIT_DEBUG */
30 #define IOD(x) printk(x)
32 #define IOD(x) do { } while (0)
35 #define IOPERM (IOUPTE_CACHE | IOUPTE_WRITE | IOUPTE_VALID)
36 #define MKIOPTE(phys) __iopte((((phys)>>4) & IOUPTE_PAGE) | IOPERM)
38 static const struct dma_map_ops iounit_dma_ops
;
40 static void __init
iounit_iommu_init(struct platform_device
*op
)
42 struct iounit_struct
*iounit
;
44 iopte_t __iomem
*xptend
;
46 iounit
= kzalloc(sizeof(struct iounit_struct
), GFP_ATOMIC
);
48 prom_printf("SUN4D: Cannot alloc iounit, halting.\n");
52 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
;
59 xpt
= of_ioremap(&op
->resource
[2], 0, PAGE_SIZE
* 16, "XPT");
61 prom_printf("SUN4D: Cannot map External Page Table.");
65 op
->dev
.archdata
.iommu
= iounit
;
66 iounit
->page_table
= xpt
;
67 spin_lock_init(&iounit
->lock
);
69 xptend
= iounit
->page_table
+ (16 * PAGE_SIZE
) / sizeof(iopte_t
);
70 for (; xpt
< xptend
; xpt
++)
73 op
->dev
.dma_ops
= &iounit_dma_ops
;
76 static int __init
iounit_init(void)
78 extern void sun4d_init_sbi_irq(void);
79 struct device_node
*dp
;
81 for_each_node_by_name(dp
, "sbi") {
82 struct platform_device
*op
= of_find_device_by_node(dp
);
84 iounit_iommu_init(op
);
85 of_propagate_archdata(op
);
93 subsys_initcall(iounit_init
);
95 /* One has to hold iounit->lock to call this */
96 static unsigned long iounit_get_area(struct iounit_struct
*iounit
, unsigned long vaddr
, int size
)
99 unsigned long rotor
, scan
, limit
;
102 npages
= ((vaddr
& ~PAGE_MASK
) + size
+ (PAGE_SIZE
-1)) >> PAGE_SHIFT
;
104 /* A tiny bit of magic ingredience :) */
106 case 1: i
= 0x0231; break;
107 case 2: i
= 0x0132; break;
108 default: i
= 0x0213; break;
111 IOD(("iounit_get_area(%08lx,%d[%d])=", vaddr
, size
, npages
));
114 rotor
= iounit
->rotor
[j
- 1];
115 limit
= iounit
->limit
[j
];
117 nexti
: scan
= find_next_zero_bit(iounit
->bmap
, limit
, scan
);
118 if (scan
+ npages
> limit
) {
119 if (limit
!= rotor
) {
121 scan
= iounit
->limit
[j
- 1];
126 panic("iounit_get_area: Couldn't find free iopte slots for (%08lx,%d)\n", vaddr
, size
);
129 for (k
= 1, scan
++; k
< npages
; k
++)
130 if (test_bit(scan
++, iounit
->bmap
))
132 iounit
->rotor
[j
- 1] = (scan
< limit
) ? scan
: iounit
->limit
[j
- 1];
134 iopte
= MKIOPTE(__pa(vaddr
& PAGE_MASK
));
135 vaddr
= IOUNIT_DMA_BASE
+ (scan
<< PAGE_SHIFT
) + (vaddr
& ~PAGE_MASK
);
136 for (k
= 0; k
< npages
; k
++, iopte
= __iopte(iopte_val(iopte
) + 0x100), scan
++) {
137 set_bit(scan
, iounit
->bmap
);
138 sbus_writel(iopte_val(iopte
), &iounit
->page_table
[scan
]);
140 IOD(("%08lx\n", vaddr
));
144 static dma_addr_t
iounit_map_page(struct device
*dev
, struct page
*page
,
145 unsigned long offset
, size_t len
, enum dma_data_direction dir
,
148 void *vaddr
= page_address(page
) + offset
;
149 struct iounit_struct
*iounit
= dev
->archdata
.iommu
;
150 unsigned long ret
, flags
;
152 /* XXX So what is maxphys for us and how do drivers know it? */
153 if (!len
|| len
> 256 * 1024)
154 return DMA_MAPPING_ERROR
;
156 spin_lock_irqsave(&iounit
->lock
, flags
);
157 ret
= iounit_get_area(iounit
, (unsigned long)vaddr
, len
);
158 spin_unlock_irqrestore(&iounit
->lock
, flags
);
162 static int iounit_map_sg(struct device
*dev
, struct scatterlist
*sgl
, int nents
,
163 enum dma_data_direction dir
, unsigned long attrs
)
165 struct iounit_struct
*iounit
= dev
->archdata
.iommu
;
166 struct scatterlist
*sg
;
170 /* FIXME: Cache some resolved pages - often several sg entries are to the same page */
171 spin_lock_irqsave(&iounit
->lock
, flags
);
172 for_each_sg(sgl
, sg
, nents
, i
) {
173 sg
->dma_address
= iounit_get_area(iounit
, (unsigned long) sg_virt(sg
), sg
->length
);
174 sg
->dma_length
= sg
->length
;
176 spin_unlock_irqrestore(&iounit
->lock
, flags
);
180 static void iounit_unmap_page(struct device
*dev
, dma_addr_t vaddr
, size_t len
,
181 enum dma_data_direction dir
, unsigned long attrs
)
183 struct iounit_struct
*iounit
= dev
->archdata
.iommu
;
186 spin_lock_irqsave(&iounit
->lock
, flags
);
187 len
= ((vaddr
& ~PAGE_MASK
) + len
+ (PAGE_SIZE
-1)) >> PAGE_SHIFT
;
188 vaddr
= (vaddr
- IOUNIT_DMA_BASE
) >> PAGE_SHIFT
;
189 IOD(("iounit_release %08lx-%08lx\n", (long)vaddr
, (long)len
+vaddr
));
190 for (len
+= vaddr
; vaddr
< len
; vaddr
++)
191 clear_bit(vaddr
, iounit
->bmap
);
192 spin_unlock_irqrestore(&iounit
->lock
, flags
);
195 static void iounit_unmap_sg(struct device
*dev
, struct scatterlist
*sgl
,
196 int nents
, enum dma_data_direction dir
, unsigned long attrs
)
198 struct iounit_struct
*iounit
= dev
->archdata
.iommu
;
199 unsigned long flags
, vaddr
, len
;
200 struct scatterlist
*sg
;
203 spin_lock_irqsave(&iounit
->lock
, flags
);
204 for_each_sg(sgl
, sg
, nents
, i
) {
205 len
= ((sg
->dma_address
& ~PAGE_MASK
) + sg
->length
+ (PAGE_SIZE
-1)) >> PAGE_SHIFT
;
206 vaddr
= (sg
->dma_address
- IOUNIT_DMA_BASE
) >> PAGE_SHIFT
;
207 IOD(("iounit_release %08lx-%08lx\n", (long)vaddr
, (long)len
+vaddr
));
208 for (len
+= vaddr
; vaddr
< len
; vaddr
++)
209 clear_bit(vaddr
, iounit
->bmap
);
211 spin_unlock_irqrestore(&iounit
->lock
, flags
);
215 static void *iounit_alloc(struct device
*dev
, size_t len
,
216 dma_addr_t
*dma_handle
, gfp_t gfp
, unsigned long attrs
)
218 struct iounit_struct
*iounit
= dev
->archdata
.iommu
;
219 unsigned long va
, addr
, page
, end
, ret
;
221 iopte_t __iomem
*iopte
;
223 /* XXX So what is maxphys for us and how do drivers know it? */
224 if (!len
|| len
> 256 * 1024)
227 len
= PAGE_ALIGN(len
);
228 va
= __get_free_pages(gfp
| __GFP_ZERO
, get_order(len
));
232 addr
= ret
= sparc_dma_alloc_resource(dev
, len
);
237 dvma_prot
= __pgprot(SRMMU_CACHE
| SRMMU_ET_PTE
| SRMMU_PRIV
);
238 end
= PAGE_ALIGN((addr
+ len
));
246 pmdp
= pmd_off_k(addr
);
247 ptep
= pte_offset_map(pmdp
, addr
);
249 set_pte(ptep
, mk_pte(virt_to_page(page
), dvma_prot
));
251 i
= ((addr
- IOUNIT_DMA_BASE
) >> PAGE_SHIFT
);
253 iopte
= iounit
->page_table
+ i
;
254 sbus_writel(iopte_val(MKIOPTE(__pa(page
))), iopte
);
265 free_pages(va
, get_order(len
));
269 static void iounit_free(struct device
*dev
, size_t size
, void *cpu_addr
,
270 dma_addr_t dma_addr
, unsigned long attrs
)
272 /* XXX Somebody please fill this in */
276 static const struct dma_map_ops iounit_dma_ops
= {
278 .alloc
= iounit_alloc
,
281 .map_page
= iounit_map_page
,
282 .unmap_page
= iounit_unmap_page
,
283 .map_sg
= iounit_map_sg
,
284 .unmap_sg
= iounit_unmap_sg
,