1 /* $NetBSD: pl310.c,v 1.18 2018/06/20 08:03:55 hkenken 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.18 2018/06/20 08:03:55 hkenken 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/locore.h> 42 43 #include <arm/cortex/mpcore_var.h> 44 #include <arm/cortex/pl310_reg.h> 45 #include <arm/cortex/pl310_var.h> 46 47 static int arml2cc_match(device_t, cfdata_t, void *); 48 static void arml2cc_attach(device_t, device_t, void *); 49 50 #define L2CC_SIZE 0x1000 51 52 struct arml2cc_softc { 53 device_t sc_dev; 54 bus_space_tag_t sc_memt; 55 bus_space_handle_t sc_memh; 56 kmutex_t sc_lock; 57 uint32_t sc_waymask; 58 struct evcnt sc_ev_inv __aligned(8); 59 struct evcnt sc_ev_wb; 60 struct evcnt sc_ev_wbinv; 61 bool sc_enabled; 62 }; 63 64 __CTASSERT(offsetof(struct arml2cc_softc, sc_ev_inv.ev_count) % 8 == 0); 65 __CTASSERT(offsetof(struct arml2cc_softc, sc_ev_wb.ev_count) % 8 == 0); 66 __CTASSERT(offsetof(struct arml2cc_softc, sc_ev_wbinv.ev_count) % 8 == 0); 67 68 CFATTACH_DECL_NEW(arml2cc, sizeof(struct arml2cc_softc), 69 arml2cc_match, arml2cc_attach, NULL, NULL); 70 71 static inline void arml2cc_disable(struct arml2cc_softc *); 72 static inline void arml2cc_enable(struct arml2cc_softc *); 73 static void arml2cc_sdcache_wb_range(vaddr_t, paddr_t, psize_t); 74 static void arml2cc_sdcache_inv_range(vaddr_t, paddr_t, psize_t); 75 static void arml2cc_sdcache_wbinv_range(vaddr_t, paddr_t, psize_t); 76 77 static struct arml2cc_softc *arml2cc_sc; 78 79 static inline uint32_t 80 arml2cc_read_4(struct arml2cc_softc *sc, bus_size_t o) 81 { 82 return bus_space_read_4(sc->sc_memt, sc->sc_memh, o); 83 } 84 85 static inline void 86 arml2cc_write_4(struct arml2cc_softc *sc, bus_size_t o, uint32_t v) 87 { 88 bus_space_write_4(sc->sc_memt, sc->sc_memh, o, v); 89 } 90 91 92 /* ARGSUSED */ 93 static int 94 arml2cc_match(device_t parent, cfdata_t cf, void *aux) 95 { 96 struct mpcore_attach_args * const mpcaa = aux; 97 98 if (arml2cc_sc) 99 return 0; 100 101 if (!CPU_ID_CORTEX_A9_P(curcpu()->ci_arm_cpuid) && 102 !CPU_ID_CORTEX_A5_P(curcpu()->ci_arm_cpuid)) 103 return 0; 104 105 if (strcmp(mpcaa->mpcaa_name, cf->cf_name) != 0) 106 return 0; 107 108 /* 109 * This isn't present on UP A9s (since CBAR isn't present). 110 */ 111 uint32_t mpidr = armreg_mpidr_read(); 112 if (mpidr == 0 || (mpidr & MPIDR_U)) 113 return 0; 114 115 return 1; 116 } 117 118 static const struct { 119 uint8_t rev; 120 uint8_t str[7]; 121 } pl310_revs[] = { 122 { 0, " r0p0" }, 123 { 2, " r1p0" }, 124 { 4, " r2p0" }, 125 { 5, " r3p0" }, 126 { 6, " r3p1" }, 127 { 7, " r3p1a" }, 128 { 8, " r3p2" }, 129 { 9, " r3p3" }, 130 }; 131 132 static void 133 arml2cc_attach(device_t parent, device_t self, void *aux) 134 { 135 struct arml2cc_softc * const sc = device_private(self); 136 struct mpcore_attach_args * const mpcaa = aux; 137 const char * const xname = device_xname(self); 138 prop_dictionary_t dict = device_properties(self); 139 uint32_t off; 140 141 aprint_naive("\n"); 142 143 if (!prop_dictionary_get_uint32(dict, "offset", &off)) { 144 if (CPU_ID_CORTEX_A5_P(curcpu()->ci_arm_cpuid)) { 145 /* 146 * PL310 on Cortex-A5 is external to PERIPHBASE, so 147 * "offset" property is required. 148 */ 149 aprint_normal(": not configured\n"); 150 return; 151 } 152 off = mpcaa->mpcaa_off1; 153 } 154 155 arml2cc_sc = sc; 156 sc->sc_dev = self; 157 sc->sc_memt = mpcaa->mpcaa_memt; 158 sc->sc_waymask = __BIT(arm_scache.dcache_ways) - 1; 159 160 evcnt_attach_dynamic(&sc->sc_ev_inv, EVCNT_TYPE_MISC, NULL, 161 xname, "L2 inv requests"); 162 evcnt_attach_dynamic(&sc->sc_ev_wb, EVCNT_TYPE_MISC, NULL, 163 xname, "L2 wb requests"); 164 evcnt_attach_dynamic(&sc->sc_ev_wbinv, EVCNT_TYPE_MISC, NULL, 165 xname, "L2 wbinv requests"); 166 167 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_HIGH); 168 169 bus_space_subregion(sc->sc_memt, mpcaa->mpcaa_memh, 170 off, L2CC_SIZE, &sc->sc_memh); 171 172 uint32_t id = arml2cc_read_4(sc, L2C_CACHE_ID); 173 u_int rev = __SHIFTOUT(id, CACHE_ID_REV); 174 175 const char *revstr = ""; 176 for (size_t i = 0; i < __arraycount(pl310_revs); i++) { 177 if (rev == pl310_revs[i].rev) { 178 revstr = pl310_revs[i].str; 179 break; 180 } 181 } 182 183 const bool enabled_p = arml2cc_read_4(sc, L2C_CTL) != 0; 184 185 aprint_normal(": ARM PL310%s L2 Cache Controller%s\n", 186 revstr, enabled_p ? "" : " (disabled)"); 187 188 if (enabled_p) { 189 if (device_cfdata(self)->cf_flags & 1) { 190 arml2cc_disable(sc); 191 aprint_normal_dev(self, "cache %s\n", 192 arml2cc_read_4(sc, L2C_CTL) ? "enabled" : "disabled"); 193 sc->sc_enabled = false; 194 } else { 195 cpufuncs.cf_sdcache_wb_range = arml2cc_sdcache_wb_range; 196 cpufuncs.cf_sdcache_inv_range = arml2cc_sdcache_inv_range; 197 cpufuncs.cf_sdcache_wbinv_range = arml2cc_sdcache_wbinv_range; 198 sc->sc_enabled = true; 199 } 200 } else if ((device_cfdata(self)->cf_flags & 1) == 0) { 201 if (!enabled_p) { 202 arml2cc_enable(sc); 203 aprint_normal_dev(self, "cache %s\n", 204 arml2cc_read_4(sc, L2C_CTL) ? "enabled" : "disabled"); 205 } 206 cpufuncs.cf_sdcache_wb_range = arml2cc_sdcache_wb_range; 207 cpufuncs.cf_sdcache_inv_range = arml2cc_sdcache_inv_range; 208 cpufuncs.cf_sdcache_wbinv_range = arml2cc_sdcache_wbinv_range; 209 sc->sc_enabled = true; 210 } 211 212 KASSERTMSG(arm_pcache.dcache_line_size == arm_scache.dcache_line_size, 213 "pcache %u scache %u", 214 arm_pcache.dcache_line_size, arm_scache.dcache_line_size); 215 } 216 217 static inline void 218 arml2cc_cache_op(struct arml2cc_softc *sc, bus_size_t off, uint32_t val, 219 bool wait) 220 { 221 arml2cc_write_4(sc, off, val); 222 if (wait) { 223 while (arml2cc_read_4(sc, off) & 1) { 224 /* spin */ 225 } 226 } 227 } 228 229 static inline void 230 arml2cc_cache_way_op(struct arml2cc_softc *sc, bus_size_t off, uint32_t way_mask) 231 { 232 arml2cc_write_4(sc, off, way_mask); 233 while (arml2cc_read_4(sc, off) & way_mask) { 234 /* spin */ 235 } 236 } 237 238 static inline void 239 arml2cc_cache_sync(struct arml2cc_softc *sc) 240 { 241 arml2cc_cache_op(sc, L2C_CACHE_SYNC, 0, true); 242 } 243 244 static inline void 245 arml2cc_disable(struct arml2cc_softc *sc) 246 { 247 mutex_spin_enter(&sc->sc_lock); 248 249 arml2cc_cache_way_op(sc, L2C_CLEAN_INV_WAY, sc->sc_waymask); 250 arml2cc_cache_sync(sc); 251 252 arml2cc_write_4(sc, L2C_CTL, 0); // turn it off 253 mutex_spin_exit(&sc->sc_lock); 254 } 255 256 static inline void 257 arml2cc_enable(struct arml2cc_softc *sc) 258 { 259 mutex_spin_enter(&sc->sc_lock); 260 261 arml2cc_cache_way_op(sc, L2C_INV_WAY, sc->sc_waymask); 262 arml2cc_cache_sync(sc); 263 264 arml2cc_write_4(sc, L2C_CTL, 1); // turn it on 265 266 mutex_spin_exit(&sc->sc_lock); 267 } 268 269 void 270 arml2cc_init(bus_space_tag_t bst, bus_space_handle_t bsh, bus_size_t o) 271 { 272 struct arm_cache_info * const info = &arm_scache; 273 274 uint32_t cfg = bus_space_read_4(bst, bsh, o + L2C_CACHE_TYPE); 275 276 info->cache_type = __SHIFTOUT(cfg, CACHE_TYPE_CTYPE); 277 info->cache_unified = __SHIFTOUT(cfg, CACHE_TYPE_HARVARD) == 0; 278 u_int cfg_dsize = __SHIFTOUT(cfg, CACHE_TYPE_DSIZE); 279 280 u_int d_waysize = 8192 << __SHIFTOUT(cfg_dsize, CACHE_TYPE_xWAYSIZE); 281 info->dcache_ways = 8 << __SHIFTOUT(cfg_dsize, CACHE_TYPE_xASSOC); 282 info->dcache_line_size = 32 << __SHIFTOUT(cfg_dsize, CACHE_TYPE_xLINESIZE); 283 info->dcache_size = info->dcache_ways * d_waysize; 284 info->dcache_type = CACHE_TYPE_PIPT; 285 info->icache_type = CACHE_TYPE_PIPT; 286 287 if (info->cache_unified) { 288 info->icache_ways = info->dcache_ways; 289 info->icache_line_size = info->dcache_line_size; 290 info->icache_size = info->dcache_size; 291 } else { 292 u_int cfg_isize = __SHIFTOUT(cfg, CACHE_TYPE_ISIZE); 293 u_int i_waysize = 8192 << __SHIFTOUT(cfg_isize, CACHE_TYPE_xWAYSIZE); 294 info->icache_ways = 8 << __SHIFTOUT(cfg_isize, CACHE_TYPE_xASSOC); 295 info->icache_line_size = 32 << __SHIFTOUT(cfg_isize, CACHE_TYPE_xLINESIZE); 296 info->icache_size = i_waysize * info->icache_ways; 297 } 298 } 299 300 static void 301 arml2cc_cache_range_op(paddr_t pa, psize_t len, bus_size_t cache_op) 302 { 303 struct arml2cc_softc * const sc = arml2cc_sc; 304 const size_t line_size = arm_scache.dcache_line_size; 305 const size_t line_mask = line_size - 1; 306 size_t off = pa & line_mask; 307 if (off) { 308 len += off; 309 pa -= off; 310 } 311 len = roundup2(len, line_size); 312 mutex_spin_enter(&sc->sc_lock); 313 if (__predict_false(!sc->sc_enabled)) { 314 mutex_spin_exit(&sc->sc_lock); 315 return; 316 } 317 for (const paddr_t endpa = pa + len; pa < endpa; pa += line_size) { 318 arml2cc_cache_op(sc, cache_op, pa, false); 319 } 320 arml2cc_cache_sync(sc); 321 mutex_spin_exit(&sc->sc_lock); 322 } 323 324 static void 325 arml2cc_sdcache_inv_range(vaddr_t va, paddr_t pa, psize_t len) 326 { 327 atomic_inc_64(&arml2cc_sc->sc_ev_inv.ev_count); 328 arml2cc_cache_range_op(pa, len, L2C_INV_PA); 329 } 330 331 static void 332 arml2cc_sdcache_wb_range(vaddr_t va, paddr_t pa, psize_t len) 333 { 334 atomic_inc_64(&arml2cc_sc->sc_ev_wb.ev_count); 335 arml2cc_cache_range_op(pa, len, L2C_CLEAN_PA); 336 } 337 338 static void 339 arml2cc_sdcache_wbinv_range(vaddr_t va, paddr_t pa, psize_t len) 340 { 341 atomic_inc_64(&arml2cc_sc->sc_ev_wbinv.ev_count); 342 arml2cc_cache_range_op(pa, len, L2C_CLEAN_INV_PA); 343 } 344