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