xref: /netbsd-src/sys/external/bsd/drm2/linux/linux_io_mapping.c (revision 7977c01b6021d6f21018b08d9cd13399aef1103f)
1*7977c01bSriastradh /*	$NetBSD: linux_io_mapping.c,v 1.1 2021/12/19 12:28:04 riastradh Exp $	*/
2*7977c01bSriastradh 
3*7977c01bSriastradh /*-
4*7977c01bSriastradh  * Copyright (c) 2013-2021 The NetBSD Foundation, Inc.
5*7977c01bSriastradh  * All rights reserved.
6*7977c01bSriastradh  *
7*7977c01bSriastradh  * Redistribution and use in source and binary forms, with or without
8*7977c01bSriastradh  * modification, are permitted provided that the following conditions
9*7977c01bSriastradh  * are met:
10*7977c01bSriastradh  * 1. Redistributions of source code must retain the above copyright
11*7977c01bSriastradh  *    notice, this list of conditions and the following disclaimer.
12*7977c01bSriastradh  * 2. Redistributions in binary form must reproduce the above copyright
13*7977c01bSriastradh  *    notice, this list of conditions and the following disclaimer in the
14*7977c01bSriastradh  *    documentation and/or other materials provided with the distribution.
15*7977c01bSriastradh  *
16*7977c01bSriastradh  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17*7977c01bSriastradh  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18*7977c01bSriastradh  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19*7977c01bSriastradh  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20*7977c01bSriastradh  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21*7977c01bSriastradh  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22*7977c01bSriastradh  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23*7977c01bSriastradh  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24*7977c01bSriastradh  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25*7977c01bSriastradh  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26*7977c01bSriastradh  * POSSIBILITY OF SUCH DAMAGE.
27*7977c01bSriastradh  */
28*7977c01bSriastradh 
29*7977c01bSriastradh #include <sys/cdefs.h>
30*7977c01bSriastradh __KERNEL_RCSID(0, "$NetBSD: linux_io_mapping.c,v 1.1 2021/12/19 12:28:04 riastradh Exp $");
31*7977c01bSriastradh 
32*7977c01bSriastradh #include <sys/param.h>
33*7977c01bSriastradh #include <sys/bus.h>
34*7977c01bSriastradh #include <sys/kmem.h>
35*7977c01bSriastradh #include <sys/systm.h>
36*7977c01bSriastradh #include <sys/mman.h>
37*7977c01bSriastradh 
38*7977c01bSriastradh #include <uvm/uvm_extern.h>
39*7977c01bSriastradh 
40*7977c01bSriastradh #include <linux/io-mapping.h>
41*7977c01bSriastradh 
42*7977c01bSriastradh bool
bus_space_io_mapping_init_wc(bus_space_tag_t bst,struct io_mapping * mapping,bus_addr_t addr,bus_size_t size)43*7977c01bSriastradh bus_space_io_mapping_init_wc(bus_space_tag_t bst, struct io_mapping *mapping,
44*7977c01bSriastradh     bus_addr_t addr, bus_size_t size)
45*7977c01bSriastradh {
46*7977c01bSriastradh 	bus_size_t offset;
47*7977c01bSriastradh 
48*7977c01bSriastradh 	KASSERT(PAGE_SIZE <= size);
49*7977c01bSriastradh 	KASSERT(0 == (size & (PAGE_SIZE - 1)));
50*7977c01bSriastradh 	KASSERT(__type_fit(off_t, size));
51*7977c01bSriastradh 
52*7977c01bSriastradh 	/*
53*7977c01bSriastradh 	 * XXX For x86: Reserve the region (bus_space_reserve) and set
54*7977c01bSriastradh 	 * an MTRR to make it write-combining.  Doesn't matter if we
55*7977c01bSriastradh 	 * have PAT and we use pmap_kenter_pa, but matters if we don't
56*7977c01bSriastradh 	 * have PAT or if we later make this use direct map.
57*7977c01bSriastradh 	 */
58*7977c01bSriastradh 
59*7977c01bSriastradh 	/* Make sure the region is mappable.  */
60*7977c01bSriastradh 	for (offset = 0; offset < size; offset += PAGE_SIZE) {
61*7977c01bSriastradh 		if (bus_space_mmap(bst, addr, offset, PROT_READ|PROT_WRITE,
62*7977c01bSriastradh 			BUS_SPACE_MAP_LINEAR|BUS_SPACE_MAP_PREFETCHABLE)
63*7977c01bSriastradh 		    == (paddr_t)-1)
64*7977c01bSriastradh 			return false;
65*7977c01bSriastradh 	}
66*7977c01bSriastradh 
67*7977c01bSriastradh 	/* Initialize the mapping record.  */
68*7977c01bSriastradh 	mapping->diom_bst = bst;
69*7977c01bSriastradh 	mapping->base = addr;
70*7977c01bSriastradh 	mapping->size = size;
71*7977c01bSriastradh 	mapping->diom_atomic = false;
72*7977c01bSriastradh 
73*7977c01bSriastradh 	/* Allocate kva for one page.  */
74*7977c01bSriastradh 	mapping->diom_va = uvm_km_alloc(kernel_map, PAGE_SIZE, PAGE_SIZE,
75*7977c01bSriastradh 	    UVM_KMF_VAONLY | UVM_KMF_WAITVA);
76*7977c01bSriastradh 	KASSERT(mapping->diom_va != 0);
77*7977c01bSriastradh 
78*7977c01bSriastradh 	return true;
79*7977c01bSriastradh }
80*7977c01bSriastradh 
81*7977c01bSriastradh void
io_mapping_fini(struct io_mapping * mapping)82*7977c01bSriastradh io_mapping_fini(struct io_mapping *mapping)
83*7977c01bSriastradh {
84*7977c01bSriastradh 
85*7977c01bSriastradh 	KASSERT(!mapping->diom_atomic);
86*7977c01bSriastradh 
87*7977c01bSriastradh 	uvm_km_free(kernel_map, mapping->diom_va, PAGE_SIZE, UVM_KMF_VAONLY);
88*7977c01bSriastradh 	mapping->diom_va = 0;	/* paranoia */
89*7977c01bSriastradh }
90*7977c01bSriastradh 
91*7977c01bSriastradh struct io_mapping *
bus_space_io_mapping_create_wc(bus_space_tag_t bst,bus_addr_t addr,bus_size_t size)92*7977c01bSriastradh bus_space_io_mapping_create_wc(bus_space_tag_t bst, bus_addr_t addr,
93*7977c01bSriastradh     bus_size_t size)
94*7977c01bSriastradh {
95*7977c01bSriastradh 	struct io_mapping *mapping;
96*7977c01bSriastradh 
97*7977c01bSriastradh 	mapping = kmem_alloc(sizeof(*mapping), KM_SLEEP);
98*7977c01bSriastradh 	if (!bus_space_io_mapping_init_wc(bst, mapping, addr, size)) {
99*7977c01bSriastradh 		kmem_free(mapping, sizeof(*mapping));
100*7977c01bSriastradh 		return NULL;
101*7977c01bSriastradh 	}
102*7977c01bSriastradh 
103*7977c01bSriastradh 	return mapping;
104*7977c01bSriastradh }
105*7977c01bSriastradh 
106*7977c01bSriastradh void
io_mapping_free(struct io_mapping * mapping)107*7977c01bSriastradh io_mapping_free(struct io_mapping *mapping)
108*7977c01bSriastradh {
109*7977c01bSriastradh 
110*7977c01bSriastradh 	io_mapping_fini(mapping);
111*7977c01bSriastradh 	kmem_free(mapping, sizeof(*mapping));
112*7977c01bSriastradh }
113*7977c01bSriastradh 
114*7977c01bSriastradh void *
io_mapping_map_wc(struct io_mapping * mapping,bus_addr_t offset,bus_size_t size)115*7977c01bSriastradh io_mapping_map_wc(struct io_mapping *mapping, bus_addr_t offset,
116*7977c01bSriastradh     bus_size_t size)
117*7977c01bSriastradh {
118*7977c01bSriastradh 	bus_size_t pg, npgs = size >> PAGE_SHIFT;
119*7977c01bSriastradh 	vaddr_t va;
120*7977c01bSriastradh 	paddr_t cookie;
121*7977c01bSriastradh 
122*7977c01bSriastradh 	KASSERT(0 == (offset & (PAGE_SIZE - 1)));
123*7977c01bSriastradh 	KASSERT(PAGE_SIZE <= mapping->size);
124*7977c01bSriastradh 	KASSERT(offset <= (mapping->size - PAGE_SIZE));
125*7977c01bSriastradh 	KASSERT(__type_fit(off_t, offset));
126*7977c01bSriastradh 
127*7977c01bSriastradh 	va = uvm_km_alloc(kernel_map, size, PAGE_SIZE,
128*7977c01bSriastradh 	    UVM_KMF_VAONLY|UVM_KMF_WAITVA);
129*7977c01bSriastradh 	KASSERT(va != mapping->diom_va);
130*7977c01bSriastradh 	for (pg = 0; pg < npgs; pg++) {
131*7977c01bSriastradh 		cookie = bus_space_mmap(mapping->diom_bst, mapping->base,
132*7977c01bSriastradh 		    offset + pg*PAGE_SIZE,
133*7977c01bSriastradh 		    PROT_READ|PROT_WRITE,
134*7977c01bSriastradh 		    BUS_SPACE_MAP_LINEAR|BUS_SPACE_MAP_PREFETCHABLE);
135*7977c01bSriastradh 		KASSERT(cookie != (paddr_t)-1);
136*7977c01bSriastradh 
137*7977c01bSriastradh 		pmap_kenter_pa(va + pg*PAGE_SIZE, pmap_phys_address(cookie),
138*7977c01bSriastradh 		    PROT_READ|PROT_WRITE, pmap_mmap_flags(cookie));
139*7977c01bSriastradh 	}
140*7977c01bSriastradh 	pmap_update(pmap_kernel());
141*7977c01bSriastradh 
142*7977c01bSriastradh 	return (void *)va;
143*7977c01bSriastradh }
144*7977c01bSriastradh 
145*7977c01bSriastradh void
io_mapping_unmap(struct io_mapping * mapping,void * ptr,bus_size_t size)146*7977c01bSriastradh io_mapping_unmap(struct io_mapping *mapping, void *ptr, bus_size_t size)
147*7977c01bSriastradh {
148*7977c01bSriastradh 	vaddr_t va = (vaddr_t)ptr;
149*7977c01bSriastradh 
150*7977c01bSriastradh 	KASSERT(mapping->diom_va != va);
151*7977c01bSriastradh 
152*7977c01bSriastradh 	pmap_kremove(va, size);
153*7977c01bSriastradh 	pmap_update(pmap_kernel());
154*7977c01bSriastradh 
155*7977c01bSriastradh 	uvm_km_free(kernel_map, va, size, UVM_KMF_VAONLY);
156*7977c01bSriastradh }
157*7977c01bSriastradh 
158*7977c01bSriastradh void *
io_mapping_map_atomic_wc(struct io_mapping * mapping,bus_addr_t offset)159*7977c01bSriastradh io_mapping_map_atomic_wc(struct io_mapping *mapping, bus_addr_t offset)
160*7977c01bSriastradh {
161*7977c01bSriastradh 	paddr_t cookie;
162*7977c01bSriastradh 
163*7977c01bSriastradh 	KASSERT(0 == (offset & (PAGE_SIZE - 1)));
164*7977c01bSriastradh 	KASSERT(PAGE_SIZE <= mapping->size);
165*7977c01bSriastradh 	KASSERT(offset <= (mapping->size - PAGE_SIZE));
166*7977c01bSriastradh 	KASSERT(__type_fit(off_t, offset));
167*7977c01bSriastradh 	KASSERT(!mapping->diom_atomic);
168*7977c01bSriastradh 
169*7977c01bSriastradh 	cookie = bus_space_mmap(mapping->diom_bst, mapping->base, offset,
170*7977c01bSriastradh 	    PROT_READ|PROT_WRITE,
171*7977c01bSriastradh 	    BUS_SPACE_MAP_LINEAR|BUS_SPACE_MAP_PREFETCHABLE);
172*7977c01bSriastradh 	KASSERT(cookie != (paddr_t)-1);
173*7977c01bSriastradh 
174*7977c01bSriastradh 	pmap_kenter_pa(mapping->diom_va, pmap_phys_address(cookie),
175*7977c01bSriastradh 	    PROT_READ|PROT_WRITE, pmap_mmap_flags(cookie));
176*7977c01bSriastradh 	pmap_update(pmap_kernel());
177*7977c01bSriastradh 
178*7977c01bSriastradh 	mapping->diom_atomic = true;
179*7977c01bSriastradh 	return (void *)mapping->diom_va;
180*7977c01bSriastradh }
181*7977c01bSriastradh 
182*7977c01bSriastradh void
io_mapping_unmap_atomic(struct io_mapping * mapping,void * ptr __diagused)183*7977c01bSriastradh io_mapping_unmap_atomic(struct io_mapping *mapping, void *ptr __diagused)
184*7977c01bSriastradh {
185*7977c01bSriastradh 
186*7977c01bSriastradh 	KASSERT(mapping->diom_atomic);
187*7977c01bSriastradh 	KASSERT(mapping->diom_va == (vaddr_t)ptr);
188*7977c01bSriastradh 
189*7977c01bSriastradh 	pmap_kremove(mapping->diom_va, PAGE_SIZE);
190*7977c01bSriastradh 	pmap_update(pmap_kernel());
191*7977c01bSriastradh 
192*7977c01bSriastradh 	mapping->diom_atomic = false;
193*7977c01bSriastradh }
194