2 * Copyright (c) 2018 Ruslan Bukin <br@bsdpad.com>
5 * This software was developed by SRI International and the University of
6 * Cambridge Computer Laboratory under DARPA/AFRL contract FA8750-10-C-0237
7 * ("CTSRD"), as part of the DARPA CRASH research programme.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/systm.h>
38 #include <sys/kernel.h>
40 #include <sys/module.h>
41 #include <sys/mutex.h>
42 #include <machine/bus.h>
44 #include <dev/ofw/ofw_bus.h>
45 #include <dev/ofw/ofw_bus_subr.h>
47 #include <arm64/coresight/coresight.h>
49 MALLOC_DEFINE(M_CORESIGHT, "coresight", "ARM Coresight");
50 static struct mtx cs_mtx;
52 struct coresight_device_list cs_devs;
55 coresight_get_ports(phandle_t dev_node,
56 struct coresight_platform_data *pdata)
58 phandle_t node, child;
63 phandle_t endpoint_child;
64 struct endpoint *endp;
66 child = ofw_bus_find_child(dev_node, "ports");
72 for (child = OF_child(node); child != 0; child = OF_peer(child)) {
73 ret = OF_getprop_alloc(child, "name", (void **)&name);
77 if (strcasecmp(name, "port") ||
78 strncasecmp(name, "port@", 6)) {
81 OF_getencprop(child, "reg", (void *)&port_reg, sizeof(port_reg));
83 endpoint_child = ofw_bus_find_child(child, "endpoint");
85 if (OF_getencprop(endpoint_child, "remote-endpoint", &xref,
86 sizeof(xref)) == -1) {
90 endp = malloc(sizeof(struct endpoint), M_CORESIGHT,
92 endp->my_node = endpoint_child;
93 endp->their_node = OF_node_from_xref(xref);
94 endp->dev_node = dev_node;
96 if (OF_getproplen(endpoint_child, "slave-mode") >= 0) {
103 mtx_lock(&pdata->mtx_lock);
104 TAILQ_INSERT_TAIL(&pdata->endpoints, endp, link);
105 mtx_unlock(&pdata->mtx_lock);
114 coresight_register(struct coresight_desc *desc)
116 struct coresight_device *cs_dev;
118 cs_dev = malloc(sizeof(struct coresight_device),
119 M_CORESIGHT, M_WAITOK | M_ZERO);
120 cs_dev->dev = desc->dev;
121 cs_dev->node = ofw_bus_get_node(desc->dev);
122 cs_dev->pdata = desc->pdata;
123 cs_dev->dev_type = desc->dev_type;
126 TAILQ_INSERT_TAIL(&cs_devs, cs_dev, link);
133 coresight_get_output_endpoint(struct coresight_platform_data *pdata)
135 struct endpoint *endp;
137 if (pdata->out_ports != 1)
140 TAILQ_FOREACH(endp, &pdata->endpoints, link) {
141 if (endp->slave == 0)
148 struct coresight_device *
149 coresight_get_output_device(struct endpoint *endp, struct endpoint **out_endp)
151 struct coresight_device *cs_dev;
152 struct endpoint *endp2;
154 TAILQ_FOREACH(cs_dev, &cs_devs, link) {
155 TAILQ_FOREACH(endp2, &cs_dev->pdata->endpoints, link) {
156 if (endp->their_node == endp2->my_node) {
167 coresight_get_cpu(phandle_t node,
168 struct coresight_platform_data *pdata)
174 if (OF_getencprop(node, "cpu", &xref, sizeof(xref)) != -1) {
175 cpu_node = OF_node_from_xref(xref);
176 if (OF_getencprop(cpu_node, "reg", (void *)&cpu_reg,
177 sizeof(cpu_reg)) > 0) {
178 pdata->cpu = cpu_reg;
186 struct coresight_platform_data *
187 coresight_get_platform_data(device_t dev)
189 struct coresight_platform_data *pdata;
192 node = ofw_bus_get_node(dev);
194 pdata = malloc(sizeof(struct coresight_platform_data),
195 M_CORESIGHT, M_WAITOK | M_ZERO);
196 mtx_init(&pdata->mtx_lock, "Coresight Platform Data", NULL, MTX_DEF);
197 TAILQ_INIT(&pdata->endpoints);
199 coresight_get_cpu(node, pdata);
200 coresight_get_ports(node, pdata);
203 printf("Total ports: in %d out %d\n",
204 pdata->in_ports, pdata->out_ports);
213 mtx_init(&cs_mtx, "ARM Coresight", NULL, MTX_DEF);
214 TAILQ_INIT(&cs_devs);
217 SYSINIT(coresight, SI_SUB_DRIVERS, SI_ORDER_FIRST, coresight_init, NULL);