1 /*-
2 * Copyright (c) 2018-2020 Ruslan Bukin <br@bsdpad.com>
3 * All rights reserved.
4 *
5 * This software was developed by BAE Systems, the University of Cambridge
6 * Computer Laboratory, and Memorial University under DARPA/AFRL contract
7 * FA8650-15-C-7558 ("CADETS"), as part of the DARPA Transparent Computing
8 * (TC) research program.
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 AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/bus.h>
36 #include <sys/rman.h>
37 #include <sys/kernel.h>
38 #include <sys/module.h>
39 #include <machine/bus.h>
40
41 #include <arm64/coresight/coresight.h>
42 #include <arm64/coresight/coresight_funnel.h>
43
44 #include "coresight_if.h"
45
46 #define FUNNEL_DEBUG
47 #undef FUNNEL_DEBUG
48
49 #ifdef FUNNEL_DEBUG
50 #define dprintf(fmt, ...) printf(fmt, ##__VA_ARGS__)
51 #else
52 #define dprintf(fmt, ...)
53 #endif
54
55 static struct resource_spec funnel_spec[] = {
56 { SYS_RES_MEMORY, 0, RF_ACTIVE },
57 { -1, 0 }
58 };
59
60 static int
funnel_init(device_t dev)61 funnel_init(device_t dev)
62 {
63 struct funnel_softc *sc;
64
65 sc = device_get_softc(dev);
66 if (sc->hwtype == HWTYPE_STATIC_FUNNEL)
67 return (0);
68
69 /* Unlock Coresight */
70 bus_write_4(sc->res, CORESIGHT_LAR, CORESIGHT_UNLOCK);
71 dprintf("Device ID: %x\n", bus_read_4(sc->res, FUNNEL_DEVICEID));
72
73 return (0);
74 }
75
76 static int
funnel_enable(device_t dev,struct endpoint * endp,struct coresight_event * event)77 funnel_enable(device_t dev, struct endpoint *endp,
78 struct coresight_event *event)
79 {
80 struct funnel_softc *sc;
81 uint32_t reg;
82
83 sc = device_get_softc(dev);
84 if (sc->hwtype == HWTYPE_STATIC_FUNNEL)
85 return (0);
86
87 reg = bus_read_4(sc->res, FUNNEL_FUNCTL);
88 reg &= ~(FUNCTL_HOLDTIME_MASK);
89 reg |= (7 << FUNCTL_HOLDTIME_SHIFT);
90 reg |= (1 << endp->reg);
91 bus_write_4(sc->res, FUNNEL_FUNCTL, reg);
92
93 return (0);
94 }
95
96 static void
funnel_disable(device_t dev,struct endpoint * endp,struct coresight_event * event)97 funnel_disable(device_t dev, struct endpoint *endp,
98 struct coresight_event *event)
99 {
100 struct funnel_softc *sc;
101 uint32_t reg;
102
103 sc = device_get_softc(dev);
104 if (sc->hwtype == HWTYPE_STATIC_FUNNEL)
105 return;
106
107 reg = bus_read_4(sc->res, FUNNEL_FUNCTL);
108 reg &= ~(1 << endp->reg);
109 bus_write_4(sc->res, FUNNEL_FUNCTL, reg);
110 }
111
112 int
funnel_attach(device_t dev)113 funnel_attach(device_t dev)
114 {
115 struct coresight_desc desc;
116 struct funnel_softc *sc;
117
118 sc = device_get_softc(dev);
119 if (sc->hwtype == HWTYPE_FUNNEL &&
120 bus_alloc_resources(dev, funnel_spec, &sc->res) != 0) {
121 device_printf(dev, "cannot allocate resources for device\n");
122 return (ENXIO);
123 }
124
125 desc.pdata = sc->pdata;
126 desc.dev = dev;
127 desc.dev_type = CORESIGHT_FUNNEL;
128 coresight_register(&desc);
129
130 return (0);
131 }
132
133 static device_method_t funnel_methods[] = {
134 /* Coresight interface */
135 DEVMETHOD(coresight_init, funnel_init),
136 DEVMETHOD(coresight_enable, funnel_enable),
137 DEVMETHOD(coresight_disable, funnel_disable),
138 DEVMETHOD_END
139 };
140
141 DEFINE_CLASS_0(funnel, funnel_driver, funnel_methods,
142 sizeof(struct funnel_softc));
143