xref: /openbsd/sys/arch/arm/armv7/armv7_space.c (revision 31fae585)
1 /*	$OpenBSD: armv7_space.c,v 1.11 2022/03/06 12:16:27 kettenis Exp $ */
2 
3 /*
4  * Copyright (c) 2001, 2002 Wasabi Systems, Inc.
5  * All rights reserved.
6  *
7  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *	This product includes software developed for the NetBSD Project by
20  *	Wasabi Systems, Inc.
21  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22  *    or promote products derived from this software without specific prior
23  *    written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  */
37 /*
38  * Copyright (c) 1997 Mark Brinicombe.
39  * Copyright (c) 1997 Causality Limited.
40  * All rights reserved.
41  *
42  * This code is derived from software contributed to The NetBSD Foundation
43  * by Ichiro FUKUHARA.
44  *
45  * Redistribution and use in source and binary forms, with or without
46  * modification, are permitted provided that the following conditions
47  * are met:
48  * 1. Redistributions of source code must retain the above copyright
49  *    notice, this list of conditions and the following disclaimer.
50  * 2. Redistributions in binary form must reproduce the above copyright
51  *    notice, this list of conditions and the following disclaimer in the
52  *    documentation and/or other materials provided with the distribution.
53  * 3. All advertising materials mentioning features or use of this software
54  *    must display the following acknowledgement:
55  *	This product includes software developed by Mark Brinicombe.
56  * 4. The name of the company nor the name of the author may be used to
57  *    endorse or promote products derived from this software without specific
58  *    prior written permission.
59  *
60  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
61  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
62  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
63  * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
64  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
65  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
66  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
67  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
68  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
69  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
70  * SUCH DAMAGE.
71  */
72 
73 /*
74  * bus_space functions for Intel PXA2[51]0 application processor.
75  * Derived from i80321_space.c.
76  */
77 
78 #include <sys/param.h>
79 #include <sys/systm.h>
80 
81 #include <uvm/uvm_extern.h>
82 
83 #include <machine/bus.h>
84 
85 /* Prototypes for all the bus_space structure functions */
86 bs_protos(armv7);
87 bs_protos(generic);
88 bs_protos(bs_notimpl);
89 
90 struct bus_space armv7_bs_tag = {
91 	/* cookie */
92 	(void *) 0,
93 
94 	/* mapping/unmapping */
95 	armv7_bs_map,
96 	armv7_bs_unmap,
97 	armv7_bs_subregion,
98 
99 	/* allocation/deallocation */
100 	armv7_bs_alloc,		/* not implemented */
101 	armv7_bs_free,		/* not implemented */
102 
103 	/* get kernel virtual address */
104 	armv7_bs_vaddr,
105 
106 	/* mmap */
107 	bs_notimpl_bs_mmap,
108 
109 	/* barrier */
110 	armv7_bs_barrier,
111 
112 	/* read (single) */
113 	armv7_bs_r_1,
114 	armv7_bs_r_2,
115 	armv7_bs_r_4,
116 	bs_notimpl_bs_r_8,
117 
118 	/* read multiple */
119 	armv7_bs_rm_1,
120 	armv7_bs_rm_2,
121 	armv7_bs_rm_4,
122 	bs_notimpl_bs_rm_8,
123 
124 	/* read region */
125 	armv7_bs_rr_1,
126 	armv7_bs_rr_2,
127 	armv7_bs_rr_4,
128 	bs_notimpl_bs_rr_8,
129 
130 	/* write (single) */
131 	armv7_bs_w_1,
132 	armv7_bs_w_2,
133 	armv7_bs_w_4,
134 	bs_notimpl_bs_w_8,
135 
136 	/* write multiple */
137 	armv7_bs_wm_1,
138 	armv7_bs_wm_2,
139 	armv7_bs_wm_4,
140 	bs_notimpl_bs_wm_8,
141 
142 	/* write region */
143 	armv7_bs_wr_1,
144 	armv7_bs_wr_2,
145 	armv7_bs_wr_4,
146 	bs_notimpl_bs_wr_8,
147 
148 	/* set multiple */
149 	bs_notimpl_bs_sm_1,
150 	bs_notimpl_bs_sm_2,
151 	bs_notimpl_bs_sm_4,
152 	bs_notimpl_bs_sm_8,
153 
154 	/* set region */
155 	armv7_bs_sr_1,
156 	armv7_bs_sr_2,
157 	armv7_bs_sr_4,
158 	bs_notimpl_bs_sr_8,
159 
160 	/* copy */
161 	bs_notimpl_bs_c_1,
162 	armv7_bs_c_2,
163 	bs_notimpl_bs_c_4,
164 	bs_notimpl_bs_c_8,
165 };
166 struct bus_space *fdt_cons_bs_tag = &armv7_bs_tag;
167 
168 int
armv7_bs_map(void * t,uint64_t bpa,bus_size_t size,int flags,bus_space_handle_t * bshp)169 armv7_bs_map(void *t, uint64_t bpa, bus_size_t size,
170 	      int flags, bus_space_handle_t *bshp)
171 {
172 	u_long startpa, endpa, pa;
173 	vaddr_t va;
174 	int pmap_flags = PMAP_DEVICE;
175 
176 	startpa = trunc_page(bpa);
177 	endpa = round_page(bpa + size);
178 
179 	/* XXX use extent manager to check duplicate mapping */
180 
181 	va = (vaddr_t)km_alloc(endpa - startpa, &kv_any, &kp_none, &kd_nowait);
182 	if (va == 0)
183 		return(ENOMEM);
184 
185 	*bshp = (bus_space_handle_t)(va + (bpa - startpa));
186 
187 	if (flags & BUS_SPACE_MAP_CACHEABLE)
188 		pmap_flags = 0;
189 
190 	for (pa = startpa; pa < endpa; pa += PAGE_SIZE, va += PAGE_SIZE)
191 		pmap_kenter_pa(va, pa | pmap_flags, PROT_READ | PROT_WRITE);
192 	pmap_update(pmap_kernel());
193 
194 	return(0);
195 }
196 
197 void
armv7_bs_unmap(void * t,bus_space_handle_t bsh,bus_size_t size)198 armv7_bs_unmap(void *t, bus_space_handle_t bsh, bus_size_t size)
199 {
200 	vaddr_t	va, endva;
201 
202 	va = trunc_page((vaddr_t)bsh);
203 	endva = round_page((vaddr_t)bsh + size);
204 
205 	pmap_kremove(va, endva - va);
206 	pmap_update(pmap_kernel());
207 
208 	km_free((void *)va, endva - va, &kv_any, &kp_none);
209 }
210 
211 int
armv7_bs_subregion(void * t,bus_space_handle_t bsh,bus_size_t offset,bus_size_t size,bus_space_handle_t * nbshp)212 armv7_bs_subregion(void *t, bus_space_handle_t bsh, bus_size_t offset,
213     bus_size_t size, bus_space_handle_t *nbshp)
214 {
215 
216 	*nbshp = bsh + offset;
217 	return (0);
218 }
219 
220 void
armv7_bs_barrier(void * t,bus_space_handle_t bsh,bus_size_t offset,bus_size_t len,int flags)221 armv7_bs_barrier(void *t, bus_space_handle_t bsh, bus_size_t offset,
222     bus_size_t len, int flags)
223 {
224 	cpu_drain_writebuf();
225 }
226 
227 void *
armv7_bs_vaddr(void * t,bus_space_handle_t bsh)228 armv7_bs_vaddr(void *t, bus_space_handle_t bsh)
229 {
230 
231 	return ((void *)bsh);
232 }
233 
234 
235 int
armv7_bs_alloc(void * t,bus_addr_t rstart,bus_addr_t rend,bus_size_t size,bus_size_t alignment,bus_size_t boundary,int flags,bus_addr_t * bpap,bus_space_handle_t * bshp)236 armv7_bs_alloc(void *t, bus_addr_t rstart, bus_addr_t rend,
237     bus_size_t size, bus_size_t alignment, bus_size_t boundary, int flags,
238     bus_addr_t *bpap, bus_space_handle_t *bshp)
239 {
240 
241 	panic("armv7_io_bs_alloc(): not implemented");
242 }
243 
244 void
armv7_bs_free(void * t,bus_space_handle_t bsh,bus_size_t size)245 armv7_bs_free(void *t, bus_space_handle_t bsh, bus_size_t size)
246 {
247 
248 	panic("armv7_io_bs_free(): not implemented");
249 }
250 
251