xref: /netbsd-src/sys/arch/arm/cortex/pl310.c (revision 6a493d6bc668897c91594964a732d38505b70cbb)
1 /*	$NetBSD: pl310.c,v 1.12 2013/06/17 05:13:07 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.12 2013/06/17 05:13:07 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 inline void arml2cc_disable(struct arml2cc_softc *);
71 static inline 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 	KASSERTMSG(arm_pcache.dcache_line_size == arm_scache.dcache_line_size,
195 	    "pcache %u scache %u",
196 	    arm_pcache.dcache_line_size, arm_scache.dcache_line_size);
197 }
198 
199 static inline void
200 arml2cc_cache_op(struct arml2cc_softc *sc, bus_size_t off, uint32_t val,
201     bool wait)
202 {
203 	arml2cc_write_4(sc, off, val);
204 	if (wait) {
205 		while (arml2cc_read_4(sc, off) & 1) {
206 			/* spin */
207 		}
208 	}
209 }
210 
211 static inline void
212 arml2cc_cache_way_op(struct arml2cc_softc *sc, bus_size_t off, uint32_t way_mask)
213 {
214 	arml2cc_write_4(sc, off, way_mask);
215 	while (arml2cc_read_4(sc, off) & way_mask) {
216 		/* spin */
217 	}
218 }
219 
220 static inline void
221 arml2cc_cache_sync(struct arml2cc_softc *sc)
222 {
223 	arml2cc_cache_op(sc, L2C_CACHE_SYNC, 0, true);
224 }
225 
226 static inline void
227 arml2cc_disable(struct arml2cc_softc *sc)
228 {
229 	mutex_spin_enter(&sc->sc_lock);
230 
231 	arml2cc_cache_way_op(sc, L2C_CLEAN_INV_WAY, sc->sc_waymask);
232 	arml2cc_cache_sync(sc);
233 
234 	arml2cc_write_4(sc, L2C_CTL, 0);	// turn it off
235 	mutex_spin_exit(&sc->sc_lock);
236 }
237 
238 static inline void
239 arml2cc_enable(struct arml2cc_softc *sc)
240 {
241 	mutex_spin_enter(&sc->sc_lock);
242 
243 	arml2cc_write_4(sc, L2C_CTL, 1);	// turn it on
244 
245 	arml2cc_cache_way_op(sc, L2C_INV_WAY, sc->sc_waymask);
246 	arml2cc_cache_sync(sc);
247 
248 	mutex_spin_exit(&sc->sc_lock);
249 }
250 
251 void
252 arml2cc_init(bus_space_tag_t bst, bus_space_handle_t bsh, bus_size_t o)
253 {
254 	struct arm_cache_info * const info = &arm_scache;
255 
256 	uint32_t cfg = bus_space_read_4(bst, bsh, o + L2C_CACHE_TYPE);
257 
258 	info->cache_type = __SHIFTOUT(cfg, CACHE_TYPE_CTYPE);
259 	info->cache_unified = __SHIFTOUT(cfg, CACHE_TYPE_HARVARD) == 0;
260 	u_int cfg_dsize = __SHIFTOUT(cfg, CACHE_TYPE_DSIZE);
261 
262 	u_int d_waysize = 8192 << __SHIFTOUT(cfg_dsize, CACHE_TYPE_xWAYSIZE);
263 	info->dcache_ways = 8 << __SHIFTOUT(cfg_dsize, CACHE_TYPE_xASSOC);
264 	info->dcache_line_size = 32 << __SHIFTOUT(cfg_dsize, CACHE_TYPE_xLINESIZE);
265 	info->dcache_size = info->dcache_ways * d_waysize;
266 
267 	if (info->cache_unified) {
268 		info->icache_ways = info->dcache_ways;
269 		info->icache_line_size = info->dcache_line_size;
270 		info->icache_size = info->dcache_size;
271 	} else {
272 		u_int cfg_isize = __SHIFTOUT(cfg, CACHE_TYPE_ISIZE);
273 		u_int i_waysize = 8192 << __SHIFTOUT(cfg_isize, CACHE_TYPE_xWAYSIZE);
274 		info->icache_ways = 8 << __SHIFTOUT(cfg_isize, CACHE_TYPE_xASSOC);
275 		info->icache_line_size = 32 << __SHIFTOUT(cfg_isize, CACHE_TYPE_xLINESIZE);
276 		info->icache_size = i_waysize * info->icache_ways;
277 	}
278 }
279 
280 static void
281 arml2cc_cache_range_op(paddr_t pa, psize_t len, bus_size_t cache_op)
282 {
283 	struct arml2cc_softc * const sc = arml2cc_sc;
284 	const size_t line_size = arm_scache.dcache_line_size;
285 	const size_t line_mask = line_size - 1;
286 	size_t off = pa & line_mask;
287 	if (off) {
288 		len += off;
289 		pa -= off;
290 	}
291 	len = roundup2(len, line_size);
292 	mutex_spin_enter(&sc->sc_lock);
293 	if (__predict_false(!sc->sc_enabled)) {
294 		mutex_spin_exit(&sc->sc_lock);
295 		return;
296 	}
297 	for (const paddr_t endpa = pa + len; pa < endpa; pa += line_size) {
298 		arml2cc_cache_op(sc, cache_op, pa, false);
299 	}
300 	arml2cc_cache_sync(sc);
301 	mutex_spin_exit(&sc->sc_lock);
302 }
303 
304 static void
305 arml2cc_sdcache_inv_range(vaddr_t va, paddr_t pa, psize_t len)
306 {
307 	atomic_inc_64(&arml2cc_sc->sc_ev_inv.ev_count);
308 	arml2cc_cache_range_op(pa, len, L2C_INV_PA);
309 }
310 
311 static void
312 arml2cc_sdcache_wb_range(vaddr_t va, paddr_t pa, psize_t len)
313 {
314 	atomic_inc_64(&arml2cc_sc->sc_ev_wb.ev_count);
315 	arml2cc_cache_range_op(pa, len, L2C_CLEAN_PA);
316 }
317 
318 static void
319 arml2cc_sdcache_wbinv_range(vaddr_t va, paddr_t pa, psize_t len)
320 {
321 	atomic_inc_64(&arml2cc_sc->sc_ev_wbinv.ev_count);
322 	arml2cc_cache_range_op(pa, len, L2C_CLEAN_INV_PA);
323 }
324