xref: /freebsd/sys/dev/hwpmc/pmu_dmc620.c (revision 131c1718c6331e87f139b42b4ad0e57b6a71ea44)
11459a227SAleksandr Rybalko /*-
25ec1d020SAndrew Turner  * SPDX-License-Identifier: BSD-2-Clause
31459a227SAleksandr Rybalko  *
41459a227SAleksandr Rybalko  * Copyright (c) 2021 Ampere Computing LLC
51459a227SAleksandr Rybalko  *
61459a227SAleksandr Rybalko  * Redistribution and use in source and binary forms, with or without
71459a227SAleksandr Rybalko  * modification, are permitted provided that the following conditions
81459a227SAleksandr Rybalko  * are met:
91459a227SAleksandr Rybalko  * 1. Redistributions of source code must retain the above copyright
101459a227SAleksandr Rybalko  *    notice, this list of conditions and the following disclaimer.
111459a227SAleksandr Rybalko  * 2. Redistributions in binary form must reproduce the above copyright
121459a227SAleksandr Rybalko  *    notice, this list of conditions and the following disclaimer in the
131459a227SAleksandr Rybalko  *    documentation and/or other materials provided with the distribution.
141459a227SAleksandr Rybalko  *
151459a227SAleksandr Rybalko  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
161459a227SAleksandr Rybalko  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
171459a227SAleksandr Rybalko  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
181459a227SAleksandr Rybalko  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
191459a227SAleksandr Rybalko  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
201459a227SAleksandr Rybalko  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
211459a227SAleksandr Rybalko  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
221459a227SAleksandr Rybalko  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
231459a227SAleksandr Rybalko  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
241459a227SAleksandr Rybalko  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
251459a227SAleksandr Rybalko  * SUCH DAMAGE.
261459a227SAleksandr Rybalko  */
271459a227SAleksandr Rybalko 
281459a227SAleksandr Rybalko #include <sys/cdefs.h>
291459a227SAleksandr Rybalko #include "opt_hwpmc_hooks.h"
301459a227SAleksandr Rybalko #include "opt_acpi.h"
311459a227SAleksandr Rybalko 
321459a227SAleksandr Rybalko #include <sys/param.h>
331459a227SAleksandr Rybalko #include <sys/bus.h>
341459a227SAleksandr Rybalko #include <sys/module.h>
351459a227SAleksandr Rybalko #include <sys/rman.h>
361459a227SAleksandr Rybalko #include <sys/pmc.h>
371459a227SAleksandr Rybalko #include <sys/pmckern.h>
381459a227SAleksandr Rybalko 
391459a227SAleksandr Rybalko #include <machine/bus.h>
401459a227SAleksandr Rybalko #include <machine/cpu.h>
411459a227SAleksandr Rybalko 
421459a227SAleksandr Rybalko #include <contrib/dev/acpica/include/acpi.h>
431459a227SAleksandr Rybalko #include <dev/acpica/acpivar.h>
441459a227SAleksandr Rybalko 
451459a227SAleksandr Rybalko #include <dev/hwpmc/pmu_dmc620_reg.h>
461459a227SAleksandr Rybalko 
471459a227SAleksandr Rybalko static char *pmu_dmc620_ids[] = {
481459a227SAleksandr Rybalko 	"ARMHD620",
491459a227SAleksandr Rybalko 	NULL
501459a227SAleksandr Rybalko };
511459a227SAleksandr Rybalko 
521459a227SAleksandr Rybalko static struct resource_spec pmu_dmc620_res_spec[] = {
531459a227SAleksandr Rybalko 	{ SYS_RES_MEMORY,	0,	RF_ACTIVE },
541459a227SAleksandr Rybalko 	{ SYS_RES_IRQ,		0,	RF_ACTIVE | RF_SHAREABLE },
551459a227SAleksandr Rybalko 	{ -1, 0 }
561459a227SAleksandr Rybalko };
571459a227SAleksandr Rybalko 
581459a227SAleksandr Rybalko struct pmu_dmc620_softc {
591459a227SAleksandr Rybalko 	device_t	sc_dev;
601459a227SAleksandr Rybalko 	int		sc_unit;
611459a227SAleksandr Rybalko 	int		sc_domain;
621459a227SAleksandr Rybalko 	struct resource *sc_res[2];
631459a227SAleksandr Rybalko 	void		*sc_ih;
641459a227SAleksandr Rybalko 	uint32_t	sc_clkdiv2_conters_hi[DMC620_CLKDIV2_COUNTERS_N];
651459a227SAleksandr Rybalko 	uint32_t	sc_clk_conters_hi[DMC620_CLK_COUNTERS_N];
661459a227SAleksandr Rybalko 	uint32_t	sc_saved_control[DMC620_COUNTERS_N];
671459a227SAleksandr Rybalko };
681459a227SAleksandr Rybalko 
691459a227SAleksandr Rybalko #define	RD4(sc, r)		bus_read_4((sc)->sc_res[0], (r))
701459a227SAleksandr Rybalko #define	WR4(sc, r, v)		bus_write_4((sc)->sc_res[0], (r), (v))
71*131c1718SAndrew Turner #define	MD4(sc, r, c, s)	WR4((sc), (r), (RD4((sc), (r)) & ~(c)) | (s))
721459a227SAleksandr Rybalko 
731459a227SAleksandr Rybalko #define	CD2MD4(sc, u, r, c, s)	MD4((sc), DMC620_CLKDIV2_REG((u), (r)), (c), (s))
741459a227SAleksandr Rybalko #define	CMD4(sc, u, r, c, s)	MD4((sc), DMC620_CLK_REG((u), (r)), (c), (s))
751459a227SAleksandr Rybalko 
761459a227SAleksandr Rybalko static int pmu_dmc620_counter_overflow_intr(void *arg);
771459a227SAleksandr Rybalko 
781459a227SAleksandr Rybalko uint32_t
pmu_dmc620_rd4(void * arg,u_int cntr,off_t reg)791459a227SAleksandr Rybalko pmu_dmc620_rd4(void *arg, u_int cntr, off_t reg)
801459a227SAleksandr Rybalko {
811459a227SAleksandr Rybalko 	struct pmu_dmc620_softc *sc;
821459a227SAleksandr Rybalko 	uint32_t val;
831459a227SAleksandr Rybalko 
841459a227SAleksandr Rybalko 	sc = (struct pmu_dmc620_softc *)arg;
851459a227SAleksandr Rybalko 	KASSERT(cntr < DMC620_COUNTERS_N, ("Wrong counter unit %d", cntr));
861459a227SAleksandr Rybalko 
871459a227SAleksandr Rybalko 	val = RD4(sc, DMC620_REG(cntr, reg));
881459a227SAleksandr Rybalko 	return (val);
891459a227SAleksandr Rybalko }
901459a227SAleksandr Rybalko 
911459a227SAleksandr Rybalko void
pmu_dmc620_wr4(void * arg,u_int cntr,off_t reg,uint32_t val)921459a227SAleksandr Rybalko pmu_dmc620_wr4(void *arg, u_int cntr, off_t reg, uint32_t val)
931459a227SAleksandr Rybalko {
941459a227SAleksandr Rybalko 	struct pmu_dmc620_softc *sc;
951459a227SAleksandr Rybalko 
961459a227SAleksandr Rybalko 	sc = (struct pmu_dmc620_softc *)arg;
971459a227SAleksandr Rybalko 	KASSERT(cntr < DMC620_COUNTERS_N, ("Wrong counter unit %d", cntr));
981459a227SAleksandr Rybalko 
991459a227SAleksandr Rybalko 	WR4(sc, DMC620_REG(cntr, reg), val);
1001459a227SAleksandr Rybalko }
1011459a227SAleksandr Rybalko 
1021459a227SAleksandr Rybalko static int
pmu_dmc620_acpi_probe(device_t dev)1031459a227SAleksandr Rybalko pmu_dmc620_acpi_probe(device_t dev)
1041459a227SAleksandr Rybalko {
1051459a227SAleksandr Rybalko 	int err;
1061459a227SAleksandr Rybalko 
1071459a227SAleksandr Rybalko 	err = ACPI_ID_PROBE(device_get_parent(dev), dev, pmu_dmc620_ids, NULL);
1081459a227SAleksandr Rybalko 	if (err <= 0)
1091459a227SAleksandr Rybalko 		device_set_desc(dev, "ARM DMC-620 Memory Controller PMU");
1101459a227SAleksandr Rybalko 
1111459a227SAleksandr Rybalko 	return (err);
1121459a227SAleksandr Rybalko }
1131459a227SAleksandr Rybalko 
1141459a227SAleksandr Rybalko static int
pmu_dmc620_acpi_attach(device_t dev)1151459a227SAleksandr Rybalko pmu_dmc620_acpi_attach(device_t dev)
1161459a227SAleksandr Rybalko {
1171459a227SAleksandr Rybalko 	struct pmu_dmc620_softc *sc;
1181459a227SAleksandr Rybalko 	int domain, i, u;
1191459a227SAleksandr Rybalko 	const char *dname;
1201459a227SAleksandr Rybalko 
1211459a227SAleksandr Rybalko 	dname = device_get_name(dev);
1221459a227SAleksandr Rybalko 	sc = device_get_softc(dev);
1231459a227SAleksandr Rybalko 	sc->sc_dev = dev;
1241459a227SAleksandr Rybalko 	u = device_get_unit(dev);
1251459a227SAleksandr Rybalko 	sc->sc_unit = u;
1261459a227SAleksandr Rybalko 
1271459a227SAleksandr Rybalko 	/*
1281459a227SAleksandr Rybalko 	 * Ampere Altra support NUMA emulation, but DMC-620 PMU units have no
1291459a227SAleksandr Rybalko 	 * mapping. Emulate this with kenv/hints.
1301459a227SAleksandr Rybalko 	 * Format "hint.pmu_dmc620.3.domain=1".
1311459a227SAleksandr Rybalko 	 */
1321459a227SAleksandr Rybalko 	if ((resource_int_value(dname, u, "domain", &domain) == 0 ||
1331459a227SAleksandr Rybalko 	    bus_get_domain(dev, &domain) == 0) && domain < MAXMEMDOM) {
1341459a227SAleksandr Rybalko 		sc->sc_domain = domain;
1351459a227SAleksandr Rybalko 	}
1361459a227SAleksandr Rybalko 	device_printf(dev, "domain=%d\n", domain);
1371459a227SAleksandr Rybalko 
1381459a227SAleksandr Rybalko 	i = bus_alloc_resources(dev, pmu_dmc620_res_spec, sc->sc_res);
1391459a227SAleksandr Rybalko 	if (i != 0) {
1401459a227SAleksandr Rybalko 		device_printf(dev, "cannot allocate resources for device (%d)\n",
1411459a227SAleksandr Rybalko 		    i);
1421459a227SAleksandr Rybalko 		return (i);
1431459a227SAleksandr Rybalko 	}
1441459a227SAleksandr Rybalko 	/* Disable counter before enable interrupt. */
1451459a227SAleksandr Rybalko 	for (i = 0; i < DMC620_CLKDIV2_COUNTERS_N; i++) {
1461459a227SAleksandr Rybalko 		CD2MD4(sc, i, DMC620_COUNTER_CONTROL,
1471459a227SAleksandr Rybalko 		    DMC620_COUNTER_CONTROL_ENABLE, 0);
1481459a227SAleksandr Rybalko 	}
1491459a227SAleksandr Rybalko 	for (i = 0; i < DMC620_CLK_COUNTERS_N; i++) {
1501459a227SAleksandr Rybalko 		CMD4(sc, i, DMC620_COUNTER_CONTROL,
1511459a227SAleksandr Rybalko 		    DMC620_COUNTER_CONTROL_ENABLE, 0);
1521459a227SAleksandr Rybalko 	}
1531459a227SAleksandr Rybalko 
1541459a227SAleksandr Rybalko 	/* Clear intr status. */
1551459a227SAleksandr Rybalko 	WR4(sc, DMC620_OVERFLOW_STATUS_CLKDIV2, 0);
1561459a227SAleksandr Rybalko 	WR4(sc, DMC620_OVERFLOW_STATUS_CLK, 0);
1571459a227SAleksandr Rybalko 
1581459a227SAleksandr Rybalko 	if (sc->sc_res[1] != NULL && bus_setup_intr(dev, sc->sc_res[1],
1591459a227SAleksandr Rybalko 	    INTR_TYPE_MISC | INTR_MPSAFE, pmu_dmc620_counter_overflow_intr,
1601459a227SAleksandr Rybalko 	    NULL, sc, &sc->sc_ih)) {
1611459a227SAleksandr Rybalko 		bus_release_resources(dev, pmu_dmc620_res_spec, sc->sc_res);
1621459a227SAleksandr Rybalko 		device_printf(dev, "cannot setup interrupt handler\n");
1631459a227SAleksandr Rybalko 		return (ENXIO);
1641459a227SAleksandr Rybalko 	}
1651459a227SAleksandr Rybalko 	dmc620_pmc_register(u, sc, domain);
1661459a227SAleksandr Rybalko 	return (0);
1671459a227SAleksandr Rybalko }
1681459a227SAleksandr Rybalko 
1691459a227SAleksandr Rybalko static int
pmu_dmc620_acpi_detach(device_t dev)1701459a227SAleksandr Rybalko pmu_dmc620_acpi_detach(device_t dev)
1711459a227SAleksandr Rybalko {
1721459a227SAleksandr Rybalko 	struct pmu_dmc620_softc *sc;
1731459a227SAleksandr Rybalko 
1741459a227SAleksandr Rybalko 	sc = device_get_softc(dev);
1751459a227SAleksandr Rybalko 	dmc620_pmc_unregister(device_get_unit(dev));
1761459a227SAleksandr Rybalko 	if (sc->sc_res[1] != NULL) {
1771459a227SAleksandr Rybalko 		bus_teardown_intr(dev, sc->sc_res[1], sc->sc_ih);
1781459a227SAleksandr Rybalko 	}
1791459a227SAleksandr Rybalko 	bus_release_resources(dev, pmu_dmc620_res_spec, sc->sc_res);
1801459a227SAleksandr Rybalko 
1811459a227SAleksandr Rybalko 	return (0);
1821459a227SAleksandr Rybalko }
1831459a227SAleksandr Rybalko 
1841459a227SAleksandr Rybalko static void
pmu_dmc620_clkdiv2_overflow(struct trapframe * tf,struct pmu_dmc620_softc * sc,u_int i)1851459a227SAleksandr Rybalko pmu_dmc620_clkdiv2_overflow(struct trapframe *tf, struct pmu_dmc620_softc *sc,
1861459a227SAleksandr Rybalko     u_int i)
1871459a227SAleksandr Rybalko {
1881459a227SAleksandr Rybalko 
1891459a227SAleksandr Rybalko 	atomic_add_32(&sc->sc_clkdiv2_conters_hi[i], 1);
1901459a227SAleksandr Rybalko 	/* Call dmc620 handler directly, because hook busy by arm64_intr. */
1911459a227SAleksandr Rybalko 	dmc620_intr(tf, PMC_CLASS_DMC620_PMU_CD2, sc->sc_unit, i);
1921459a227SAleksandr Rybalko }
1931459a227SAleksandr Rybalko 
1941459a227SAleksandr Rybalko static void
pmu_dmc620_clk_overflow(struct trapframe * tf,struct pmu_dmc620_softc * sc,u_int i)1951459a227SAleksandr Rybalko pmu_dmc620_clk_overflow(struct trapframe *tf, struct pmu_dmc620_softc *sc,
1961459a227SAleksandr Rybalko     u_int i)
1971459a227SAleksandr Rybalko {
1981459a227SAleksandr Rybalko 
1991459a227SAleksandr Rybalko 	atomic_add_32(&sc->sc_clk_conters_hi[i], 1);
2001459a227SAleksandr Rybalko 	/* Call dmc620 handler directly, because hook busy by arm64_intr. */
2011459a227SAleksandr Rybalko 	dmc620_intr(tf, PMC_CLASS_DMC620_PMU_C, sc->sc_unit, i);
2021459a227SAleksandr Rybalko 
2031459a227SAleksandr Rybalko }
2041459a227SAleksandr Rybalko 
2051459a227SAleksandr Rybalko static int
pmu_dmc620_counter_overflow_intr(void * arg)2061459a227SAleksandr Rybalko pmu_dmc620_counter_overflow_intr(void *arg)
2071459a227SAleksandr Rybalko {
2081459a227SAleksandr Rybalko 	uint32_t clkdiv2_stat, clk_stat;
2091459a227SAleksandr Rybalko 	struct pmu_dmc620_softc *sc;
2101459a227SAleksandr Rybalko 	struct trapframe *tf;
2111459a227SAleksandr Rybalko 	u_int i;
2121459a227SAleksandr Rybalko 
2131459a227SAleksandr Rybalko 	tf = PCPU_GET(curthread)->td_intr_frame;
2141459a227SAleksandr Rybalko 	sc = (struct pmu_dmc620_softc *) arg;
2151459a227SAleksandr Rybalko 	clkdiv2_stat = RD4(sc, DMC620_OVERFLOW_STATUS_CLKDIV2);
2161459a227SAleksandr Rybalko 	clk_stat = RD4(sc, DMC620_OVERFLOW_STATUS_CLK);
2171459a227SAleksandr Rybalko 
2181459a227SAleksandr Rybalko 	if ((clkdiv2_stat == 0) && (clk_stat == 0))
2191459a227SAleksandr Rybalko 		return (FILTER_STRAY);
2201459a227SAleksandr Rybalko 	/* Stop and save states of all counters. */
2211459a227SAleksandr Rybalko 	for (i = 0; i < DMC620_COUNTERS_N; i++) {
2221459a227SAleksandr Rybalko 		sc->sc_saved_control[i] = RD4(sc, DMC620_REG(i,
2231459a227SAleksandr Rybalko 		    DMC620_COUNTER_CONTROL));
2241459a227SAleksandr Rybalko 		WR4(sc, DMC620_REG(i, DMC620_COUNTER_CONTROL),
2251459a227SAleksandr Rybalko 		    sc->sc_saved_control[i] & ~DMC620_COUNTER_CONTROL_ENABLE);
2261459a227SAleksandr Rybalko 	}
2271459a227SAleksandr Rybalko 
2281459a227SAleksandr Rybalko 	if (clkdiv2_stat != 0) {
2291459a227SAleksandr Rybalko 		for (i = 0; i < DMC620_CLKDIV2_COUNTERS_N; i++) {
2301459a227SAleksandr Rybalko 			if ((clkdiv2_stat & (1 << i)) == 0)
2311459a227SAleksandr Rybalko 				continue;
2321459a227SAleksandr Rybalko 			pmu_dmc620_clkdiv2_overflow(tf, sc, i);
2331459a227SAleksandr Rybalko 		}
2341459a227SAleksandr Rybalko 		WR4(sc, DMC620_OVERFLOW_STATUS_CLKDIV2, 0);
2351459a227SAleksandr Rybalko 	}
2361459a227SAleksandr Rybalko 	if (clk_stat != 0) {
2371459a227SAleksandr Rybalko 		for (i = 0; i < DMC620_CLK_COUNTERS_N; i++) {
2381459a227SAleksandr Rybalko 			if ((clk_stat & (1 << i)) == 0)
2391459a227SAleksandr Rybalko 				continue;
2401459a227SAleksandr Rybalko 			pmu_dmc620_clk_overflow(tf, sc, i);
2411459a227SAleksandr Rybalko 		}
2421459a227SAleksandr Rybalko 		WR4(sc, DMC620_OVERFLOW_STATUS_CLK, 0);
2431459a227SAleksandr Rybalko 	}
2441459a227SAleksandr Rybalko 
2451459a227SAleksandr Rybalko 	/* Restore states of all counters. */
2461459a227SAleksandr Rybalko 	for (i = 0; i < DMC620_COUNTERS_N; i++) {
2471459a227SAleksandr Rybalko 		WR4(sc, DMC620_REG(i, DMC620_COUNTER_CONTROL),
2481459a227SAleksandr Rybalko 		    sc->sc_saved_control[i]);
2491459a227SAleksandr Rybalko 	}
2501459a227SAleksandr Rybalko 
2511459a227SAleksandr Rybalko 	return (FILTER_HANDLED);
2521459a227SAleksandr Rybalko }
2531459a227SAleksandr Rybalko 
2541459a227SAleksandr Rybalko static device_method_t pmu_dmc620_acpi_methods[] = {
2551459a227SAleksandr Rybalko 	/* Device interface */
2561459a227SAleksandr Rybalko 	DEVMETHOD(device_probe,			pmu_dmc620_acpi_probe),
2571459a227SAleksandr Rybalko 	DEVMETHOD(device_attach,		pmu_dmc620_acpi_attach),
2581459a227SAleksandr Rybalko 	DEVMETHOD(device_detach,		pmu_dmc620_acpi_detach),
2591459a227SAleksandr Rybalko 
2601459a227SAleksandr Rybalko 	/* End */
2611459a227SAleksandr Rybalko 	DEVMETHOD_END
2621459a227SAleksandr Rybalko };
2631459a227SAleksandr Rybalko 
2641459a227SAleksandr Rybalko static driver_t pmu_dmc620_acpi_driver = {
2651459a227SAleksandr Rybalko 	"pmu_dmc620",
2661459a227SAleksandr Rybalko 	pmu_dmc620_acpi_methods,
2671459a227SAleksandr Rybalko 	sizeof(struct pmu_dmc620_softc),
2681459a227SAleksandr Rybalko };
2691459a227SAleksandr Rybalko 
2706ea773d4SJohn Baldwin DRIVER_MODULE(pmu_dmc620, acpi, pmu_dmc620_acpi_driver, 0, 0);
2711459a227SAleksandr Rybalko /* Reverse dependency. hwpmc needs DMC-620 on ARM64. */
2721459a227SAleksandr Rybalko MODULE_DEPEND(pmc, pmu_dmc620, 1, 1, 1);
2731459a227SAleksandr Rybalko MODULE_VERSION(pmu_dmc620, 1);
274