xref: /freebsd/sys/arm64/coresight/coresight_fdt.c (revision e17f5b1d)
1 /*-
2  * Copyright (c) 2018-2020 Ruslan Bukin <br@bsdpad.com>
3  * All rights reserved.
4  *
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.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
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.
17  *
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
28  * SUCH DAMAGE.
29  */
30 
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/bus.h>
37 #include <sys/rman.h>
38 #include <sys/kernel.h>
39 #include <sys/lock.h>
40 #include <sys/module.h>
41 #include <sys/mutex.h>
42 #include <machine/bus.h>
43 
44 #include <dev/ofw/ofw_bus.h>
45 #include <dev/ofw/ofw_bus_subr.h>
46 
47 #include <arm64/coresight/coresight.h>
48 
49 static int
50 coresight_fdt_get_ports(phandle_t dev_node,
51     struct coresight_platform_data *pdata)
52 {
53 	phandle_t node, child;
54 	pcell_t port_reg;
55 	phandle_t xref;
56 	char *name;
57 	int ret;
58 	phandle_t endpoint_child;
59 	struct endpoint *endp;
60 
61 	child = ofw_bus_find_child(dev_node, "ports");
62 	if (child)
63 		node = child;
64 	else
65 		node = dev_node;
66 
67 	for (child = OF_child(node); child != 0; child = OF_peer(child)) {
68 		ret = OF_getprop_alloc(child, "name", (void **)&name);
69 		if (ret == -1)
70 			continue;
71 
72 		if (strcasecmp(name, "port") ||
73 		    strncasecmp(name, "port@", 6)) {
74 
75 			port_reg = -1;
76 			OF_getencprop(child, "reg", (void *)&port_reg,
77 			    sizeof(port_reg));
78 
79 			endpoint_child = ofw_bus_find_child(child, "endpoint");
80 			if (endpoint_child) {
81 				if (OF_getencprop(endpoint_child,
82 				    "remote-endpoint", &xref,
83 				    sizeof(xref)) == -1) {
84 					printf("failed\n");
85 					continue;
86 				}
87 				endp = malloc(sizeof(struct endpoint),
88 				    M_CORESIGHT, M_WAITOK | M_ZERO);
89 				endp->my_node = endpoint_child;
90 				endp->their_node = OF_node_from_xref(xref);
91 				endp->dev_node = dev_node;
92 				endp->reg = port_reg;
93 				if (OF_getproplen(endpoint_child,
94 				    "slave-mode") >= 0) {
95 					pdata->in_ports++;
96 					endp->input = 1;
97 				} else
98 					pdata->out_ports++;
99 
100 				mtx_lock(&pdata->mtx_lock);
101 				TAILQ_INSERT_TAIL(&pdata->endpoints,
102 				    endp, link);
103 				mtx_unlock(&pdata->mtx_lock);
104 			}
105 		}
106 	}
107 
108 	return (0);
109 }
110 
111 static int
112 coresight_fdt_get_cpu(phandle_t node,
113     struct coresight_platform_data *pdata)
114 {
115 	phandle_t cpu_node;
116 	pcell_t xref;
117 	pcell_t cpu_reg;
118 
119 	if (OF_getencprop(node, "cpu", &xref, sizeof(xref)) != -1) {
120 		cpu_node = OF_node_from_xref(xref);
121 		if (OF_getencprop(cpu_node, "reg", (void *)&cpu_reg,
122 			sizeof(cpu_reg)) > 0) {
123 			pdata->cpu = cpu_reg;
124 			return (0);
125 		}
126 	}
127 
128 	return (-1);
129 }
130 
131 struct coresight_platform_data *
132 coresight_fdt_get_platform_data(device_t dev)
133 {
134 	struct coresight_platform_data *pdata;
135 	phandle_t node;
136 
137 	node = ofw_bus_get_node(dev);
138 
139 	pdata = malloc(sizeof(struct coresight_platform_data),
140 	    M_CORESIGHT, M_WAITOK | M_ZERO);
141 	pdata->bus_type = CORESIGHT_BUS_FDT;
142 
143 	mtx_init(&pdata->mtx_lock, "Coresight Platform Data", NULL, MTX_DEF);
144 	TAILQ_INIT(&pdata->endpoints);
145 
146 	coresight_fdt_get_cpu(node, pdata);
147 	coresight_fdt_get_ports(node, pdata);
148 
149 	if (bootverbose)
150 		printf("Total ports: in %d out %d\n",
151 		    pdata->in_ports, pdata->out_ports);
152 
153 	return (pdata);
154 }
155