pl310.c revision 1.8 1 /* $NetBSD: pl310.c,v 1.8 2012/11/01 20:17:44 matt Exp $ */
2
3 /*-
4 * Copyright (c) 2012 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Matt Thomas
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: pl310.c,v 1.8 2012/11/01 20:17:44 matt Exp $");
34
35 #include <sys/param.h>
36 #include <sys/bus.h>
37 #include <sys/cpu.h>
38 #include <sys/device.h>
39 #include <sys/atomic.h>
40
41 #include <arm/cortex/mpcore_var.h>
42 #include <arm/cortex/pl310_reg.h>
43 #include <arm/cortex/pl310_var.h>
44
45 static int arml2cc_match(device_t, cfdata_t, void *);
46 static void arml2cc_attach(device_t, device_t, void *);
47
48 #define L2CC_BASE 0x2000
49 #define L2CC_SIZE 0x1000
50
51 struct arml2cc_softc {
52 device_t sc_dev;
53 bus_space_tag_t sc_memt;
54 bus_space_handle_t sc_memh;
55 kmutex_t sc_lock;
56 uint32_t sc_waymask;
57 struct evcnt sc_ev_inv __aligned(8);
58 struct evcnt sc_ev_wb;
59 struct evcnt sc_ev_wbinv;
60 bool sc_enabled;
61 };
62
63 __CTASSERT(offsetof(struct arml2cc_softc, sc_ev_inv.ev_count) % 8 == 0);
64 __CTASSERT(offsetof(struct arml2cc_softc, sc_ev_wb.ev_count) % 8 == 0);
65 __CTASSERT(offsetof(struct arml2cc_softc, sc_ev_wbinv.ev_count) % 8 == 0);
66
67 CFATTACH_DECL_NEW(arml2cc, sizeof(struct arml2cc_softc),
68 arml2cc_match, arml2cc_attach, NULL, NULL);
69
70 static void arml2cc_disable(struct arml2cc_softc *);
71 static void arml2cc_enable(struct arml2cc_softc *);
72 static void arml2cc_sdcache_wb_range(vaddr_t, paddr_t, psize_t);
73 static void arml2cc_sdcache_inv_range(vaddr_t, paddr_t, psize_t);
74 static void arml2cc_sdcache_wbinv_range(vaddr_t, paddr_t, psize_t);
75
76 static struct arml2cc_softc *arml2cc_sc;
77
78 static inline uint32_t
79 arml2cc_read_4(struct arml2cc_softc *sc, bus_size_t o)
80 {
81 return bus_space_read_4(sc->sc_memt, sc->sc_memh, o);
82 }
83
84 static inline void
85 arml2cc_write_4(struct arml2cc_softc *sc, bus_size_t o, uint32_t v)
86 {
87 bus_space_write_4(sc->sc_memt, sc->sc_memh, o, v);
88 }
89
90
91 /* ARGSUSED */
92 static int
93 arml2cc_match(device_t parent, cfdata_t cf, void *aux)
94 {
95 struct mpcore_attach_args * const mpcaa = aux;
96
97 if (arml2cc_sc)
98 return 0;
99
100 if (!CPU_ID_CORTEX_A9_P(curcpu()->ci_arm_cpuid))
101 return 0;
102
103 if (strcmp(mpcaa->mpcaa_name, cf->cf_name) != 0)
104 return 0;
105
106 /*
107 * This isn't present on UP A9s (since CBAR isn't present).
108 */
109 uint32_t mpidr = armreg_mpidr_read();
110 if (mpidr == 0 || (mpidr & MPIDR_U))
111 return 0;
112
113 return 1;
114 }
115
116 static const struct {
117 uint8_t rev;
118 uint8_t str[7];
119 } pl310_revs[] = {
120 { 0, " r0p0" },
121 { 2, " r1p0" },
122 { 4, " r2p0" },
123 { 5, " r3p0" },
124 { 6, " r3p1" },
125 { 8, " r3p2" },
126 { 9, " r3p3" },
127 };
128
129 static void
130 arml2cc_attach(device_t parent, device_t self, void *aux)
131 {
132 struct arml2cc_softc * const sc = device_private(self);
133 struct mpcore_attach_args * const mpcaa = aux;
134 const char * const xname = device_xname(self);
135
136 arml2cc_sc = sc;
137 sc->sc_dev = self;
138 sc->sc_memt = mpcaa->mpcaa_memt;
139 sc->sc_waymask = __BIT(arm_scache.dcache_ways) - 1;
140
141 evcnt_attach_dynamic(&sc->sc_ev_inv, EVCNT_TYPE_MISC, NULL,
142 xname, "L2 inv requests");
143 evcnt_attach_dynamic(&sc->sc_ev_wb, EVCNT_TYPE_MISC, NULL,
144 xname, "L2 wb requests");
145 evcnt_attach_dynamic(&sc->sc_ev_wbinv, EVCNT_TYPE_MISC, NULL,
146 xname, "L2 wbinv requests");
147
148 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_HIGH);
149
150 bus_space_subregion(sc->sc_memt, mpcaa->mpcaa_memh,
151 L2CC_BASE, L2CC_SIZE, &sc->sc_memh);
152
153 uint32_t id = arml2cc_read_4(sc, L2C_CACHE_ID);
154 u_int rev = __SHIFTOUT(id, CACHE_ID_REV);
155
156 const char *revstr = "";
157 for (size_t i = 0; i < __arraycount(pl310_revs); i++) {
158 if (rev == pl310_revs[i].rev) {
159 revstr = pl310_revs[i].str;
160 break;
161 }
162 }
163
164 const bool enabled_p = arml2cc_read_4(sc, L2C_CTL) != 0;
165
166 aprint_naive("\n");
167 aprint_normal(": ARM PL310%s L2 Cache Controller%s\n",
168 revstr, enabled_p ? "" : " (disabled)");
169
170 if (enabled_p) {
171 if (device_cfdata(self)->cf_flags & 1) {
172 arml2cc_disable(sc);
173 aprint_normal_dev(self, "cache %s\n",
174 arml2cc_read_4(sc, L2C_CTL) ? "enabled" : "disabled");
175 sc->sc_enabled = false;
176 } else {
177 cpufuncs.cf_sdcache_wb_range = arml2cc_sdcache_wb_range;
178 cpufuncs.cf_sdcache_inv_range = arml2cc_sdcache_inv_range;
179 cpufuncs.cf_sdcache_wbinv_range = arml2cc_sdcache_wbinv_range;
180 sc->sc_enabled = true;
181 }
182 } else if ((device_cfdata(self)->cf_flags & 1) == 0) {
183 if (!enabled_p) {
184 arml2cc_enable(sc);
185 aprint_normal_dev(self, "cache %s\n",
186 arml2cc_read_4(sc, L2C_CTL) ? "enabled" : "disabled");
187 }
188 cpufuncs.cf_sdcache_wb_range = arml2cc_sdcache_wb_range;
189 cpufuncs.cf_sdcache_inv_range = arml2cc_sdcache_inv_range;
190 cpufuncs.cf_sdcache_wbinv_range = arml2cc_sdcache_wbinv_range;
191 sc->sc_enabled = true;
192 }
193
194 KASSERT(arm_pcache.dcache_line_size == arm_scache.dcache_line_size);
195 }
196
197 static inline void
198 arml2cc_cache_op(struct arml2cc_softc *sc, bus_size_t off, uint32_t val)
199 {
200 arml2cc_write_4(sc, off, val);
201 while (arml2cc_read_4(sc, off) & 1) {
202 /* spin */
203 }
204 }
205
206 static inline void
207 arml2cc_cache_way_op(struct arml2cc_softc *sc, bus_size_t off, uint32_t way_mask)
208 {
209 arml2cc_write_4(sc, off, way_mask);
210 while (arml2cc_read_4(sc, off) & way_mask) {
211 /* spin */
212 }
213 }
214
215 static inline void
216 arml2cc_cache_sync(struct arml2cc_softc *sc)
217 {
218 arml2cc_cache_op(sc, L2C_CACHE_SYNC, 0);
219 }
220
221 static inline void
222 arml2cc_disable(struct arml2cc_softc *sc)
223 {
224 mutex_spin_enter(&sc->sc_lock);
225
226 arml2cc_cache_way_op(sc, L2C_CLEAN_INV_WAY, sc->sc_waymask);
227 arml2cc_cache_sync(sc);
228
229 arml2cc_write_4(sc, L2C_CTL, 0); // turn it off
230 mutex_spin_exit(&sc->sc_lock);
231 }
232
233 static inline void
234 arml2cc_enable(struct arml2cc_softc *sc)
235 {
236 mutex_spin_enter(&sc->sc_lock);
237
238 arml2cc_write_4(sc, L2C_CTL, 1); // turn it on
239
240 arml2cc_cache_way_op(sc, L2C_INV_WAY, sc->sc_waymask);
241 arml2cc_cache_sync(sc);
242
243 mutex_spin_exit(&sc->sc_lock);
244 }
245
246 void
247 arml2cc_init(bus_space_tag_t bst, bus_space_handle_t bsh, bus_size_t o)
248 {
249 struct arm_cache_info * const info = &arm_scache;
250
251 uint32_t cfg = bus_space_read_4(bst, bsh, o + L2C_CACHE_TYPE);
252
253 info->cache_type = __SHIFTOUT(cfg, CACHE_TYPE_CTYPE);
254 info->cache_unified = __SHIFTOUT(cfg, CACHE_TYPE_HARVARD) == 0;
255 u_int cfg_dsize = __SHIFTOUT(cfg, CACHE_TYPE_DSIZE);
256
257 u_int d_waysize = 8192 << __SHIFTOUT(cfg_dsize, CACHE_TYPE_xWAYSIZE);
258 info->dcache_ways = 8 << __SHIFTOUT(cfg_dsize, CACHE_TYPE_xASSOC);
259 info->dcache_line_size = 32 << __SHIFTOUT(cfg_dsize, CACHE_TYPE_xLINESIZE);
260 info->dcache_size = info->dcache_ways * d_waysize;
261
262 if (info->cache_unified) {
263 info->icache_ways = info->dcache_ways;
264 info->icache_line_size = info->dcache_line_size;
265 info->icache_size = info->dcache_size;
266 } else {
267 u_int cfg_isize = __SHIFTOUT(cfg, CACHE_TYPE_ISIZE);
268 u_int i_waysize = 8192 << __SHIFTOUT(cfg_isize, CACHE_TYPE_xWAYSIZE);
269 info->icache_ways = 8 << __SHIFTOUT(cfg_isize, CACHE_TYPE_xASSOC);
270 info->icache_line_size = 32 << __SHIFTOUT(cfg_isize, CACHE_TYPE_xLINESIZE);
271 info->icache_size = i_waysize * info->icache_ways;
272 }
273 }
274
275 static void
276 arml2cc_cache_range_op(paddr_t pa, psize_t len, bus_size_t cache_op)
277 {
278 struct arml2cc_softc * const sc = arml2cc_sc;
279 const size_t line_size = arm_scache.dcache_line_size;
280 const size_t line_mask = line_size - 1;
281 size_t off = pa & line_mask;
282 if (off) {
283 len += off;
284 pa -= off;
285 }
286 len = roundup2(len, line_size);
287 off = pa & PAGE_MASK;
288 for (const paddr_t endpa = pa + len; pa < endpa; off = 0) {
289 psize_t seglen = min(len, PAGE_SIZE - off);
290
291 mutex_spin_enter(&sc->sc_lock);
292 if (!sc->sc_enabled) {
293 mutex_spin_exit(&sc->sc_lock);
294 return;
295 }
296 for (paddr_t segend = pa + seglen; pa < segend; pa += line_size) {
297 arml2cc_cache_op(sc, cache_op, pa);
298 }
299 mutex_spin_exit(&sc->sc_lock);
300 }
301 }
302
303 static void
304 arml2cc_sdcache_inv_range(vaddr_t va, paddr_t pa, psize_t len)
305 {
306 atomic_inc_64(&arml2cc_sc->sc_ev_inv.ev_count);
307 arml2cc_cache_range_op(pa, len, L2C_INV_PA);
308 }
309
310 static void
311 arml2cc_sdcache_wb_range(vaddr_t va, paddr_t pa, psize_t len)
312 {
313 atomic_inc_64(&arml2cc_sc->sc_ev_wb.ev_count);
314 arml2cc_cache_range_op(pa, len, L2C_CLEAN_PA);
315 }
316
317 static void
318 arml2cc_sdcache_wbinv_range(vaddr_t va, paddr_t pa, psize_t len)
319 {
320 atomic_inc_64(&arml2cc_sc->sc_ev_wbinv.ev_count);
321 arml2cc_cache_range_op(pa, len, L2C_CLEAN_INV_PA);
322 }
323