#include <sys/param.h>
#include <sys/systm.h>
#include <sys/bus.h>
#include <sys/rman.h>
#include <sys/kernel.h>
#include <sys/module.h>
#include <machine/bus.h>
#include <arm64/coresight/coresight.h>
#include <arm64/coresight/coresight_etm4x.h>
#include "coresight_if.h"
#define ETM_DEBUG
#undef ETM_DEBUG
#ifdef ETM_DEBUG
#define dprintf(fmt, ...) printf(fmt, ##__VA_ARGS__)
#else
#define dprintf(fmt, ...)
#endif
static struct resource_spec etm_spec[] = {
{ SYS_RES_MEMORY, 0, RF_ACTIVE },
{ -1, 0 }
};
static int
etm_prepare(device_t dev, struct coresight_event *event)
{
struct etm_softc *sc;
uint32_t reg;
int i;
sc = device_get_softc(dev);
reg = TRCCONFIGR_RS | TRCCONFIGR_TS;
reg |= TRCCONFIGR_CID | TRCCONFIGR_VMID;
reg |= TRCCONFIGR_INSTP0_LDRSTR;
reg |= TRCCONFIGR_COND_ALL;
bus_write_4(sc->res, TRCCONFIGR, reg);
bus_write_4(sc->res, TRCEVENTCTL0R, 0);
bus_write_4(sc->res, TRCEVENTCTL1R, 0);
bus_write_4(sc->res, TRCSTALLCTLR, 0);
bus_write_4(sc->res, TRCSYNCPR, TRCSYNCPR_4K);
bus_write_4(sc->res, TRCTRACEIDR, event->etm.trace_id);
bus_write_4(sc->res, TRCTSCTLR, 0);
reg = TRCVICTLR_SSSTATUS;
reg |= (1 << EVENT_SEL_S);
if (event->excp_level > 2)
return (-1);
reg |= TRCVICTLR_EXLEVEL_NS_M;
reg &= ~TRCVICTLR_EXLEVEL_NS(event->excp_level);
reg |= TRCVICTLR_EXLEVEL_S_M;
reg &= ~TRCVICTLR_EXLEVEL_S(event->excp_level);
bus_write_4(sc->res, TRCVICTLR, reg);
for (i = 0; i < event->naddr * 2; i++) {
dprintf("configure range %d, address %lx\n",
i, event->addr[i]);
bus_write_8(sc->res, TRCACVR(i), event->addr[i]);
reg = 0;
reg |= TRCACATR_EXLEVEL_S_M;
reg &= ~TRCACATR_EXLEVEL_S(event->excp_level);
reg |= TRCACATR_EXLEVEL_NS_M;
reg &= ~TRCACATR_EXLEVEL_NS(event->excp_level);
bus_write_4(sc->res, TRCACATR(i), reg);
reg = bus_read_4(sc->res, TRCVIIECTLR);
reg |= (1 << (TRCVIIECTLR_INCLUDE_S + i / 2));
bus_write_4(sc->res, TRCVIIECTLR, reg);
}
bus_write_4(sc->res, TRCVDARCCTLR, 0);
bus_write_4(sc->res, TRCSSCSR(0), 0);
if (event->naddr == 0) {
bus_write_4(sc->res, TRCVIIECTLR, 0);
}
bus_write_4(sc->res, TRCVISSCTLR, 0);
bus_write_4(sc->res, TRCVDCTLR, 0);
bus_write_4(sc->res, TRCVDSACCTLR, 0);
return (0);
}
static int
etm_init(device_t dev)
{
struct etm_softc *sc;
uint32_t reg __unused;
sc = device_get_softc(dev);
bus_write_4(sc->res, CORESIGHT_LAR, CORESIGHT_UNLOCK);
bus_write_4(sc->res, TRCOSLAR, 0);
reg = bus_read_4(sc->res, TRCIDR(1));
dprintf("ETM Version: %d.%d\n",
(reg & TRCIDR1_TRCARCHMAJ_M) >> TRCIDR1_TRCARCHMAJ_S,
(reg & TRCIDR1_TRCARCHMIN_M) >> TRCIDR1_TRCARCHMIN_S);
return (0);
}
static int
etm_enable(device_t dev, struct endpoint *endp,
struct coresight_event *event)
{
struct etm_softc *sc;
uint32_t reg;
sc = device_get_softc(dev);
etm_prepare(dev, event);
bus_write_4(sc->res, TRCPRGCTLR, TRCPRGCTLR_EN);
do {
reg = bus_read_4(sc->res, TRCSTATR);
} while ((reg & TRCSTATR_IDLE) == 1);
if ((bus_read_4(sc->res, TRCPRGCTLR) & TRCPRGCTLR_EN) == 0)
panic("etm is not enabled\n");
return (0);
}
static void
etm_disable(device_t dev, struct endpoint *endp,
struct coresight_event *event)
{
struct etm_softc *sc;
uint32_t reg;
sc = device_get_softc(dev);
bus_write_4(sc->res, TRCPRGCTLR, 0);
do {
reg = bus_read_4(sc->res, TRCSTATR);
} while ((reg & TRCSTATR_IDLE) == 0);
}
int
etm_attach(device_t dev)
{
struct coresight_desc desc;
struct etm_softc *sc;
sc = device_get_softc(dev);
if (bus_alloc_resources(dev, etm_spec, &sc->res) != 0) {
device_printf(dev, "cannot allocate resources for device\n");
return (ENXIO);
}
desc.pdata = sc->pdata;
desc.dev = dev;
desc.dev_type = CORESIGHT_ETMV4;
coresight_register(&desc);
return (0);
}
static device_method_t etm_methods[] = {
DEVMETHOD(coresight_init, etm_init),
DEVMETHOD(coresight_enable, etm_enable),
DEVMETHOD(coresight_disable, etm_disable),
DEVMETHOD_END
};
DEFINE_CLASS_0(etm, etm_driver, etm_methods, sizeof(struct etm_softc));