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