xref: /freebsd/sys/dev/ofw/ofw_graph.c (revision 148a8da8)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2019 Emmanuel Vadot <manu@FreeBSD.org>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions, and the following disclaimer,
11  *    without modification, immediately at the beginning of the file.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in
14  *    the documentation and/or other materials provided with the
15  *    distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
21  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * $FreeBSD$
30  */
31 
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 #include "opt_platform.h"
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/bus.h>
39 #include <sys/errno.h>
40 
41 #include <dev/ofw/ofw_bus.h>
42 #include <dev/ofw/ofw_bus_subr.h>
43 #include <dev/ofw/ofw_graph.h>
44 #include <dev/ofw/openfirm.h>
45 
46 #include "ofw_bus_if.h"
47 
48 #define	PORT_MAX_NAME	8
49 
50 phandle_t
51 ofw_graph_get_port_by_idx(phandle_t node, uint32_t idx)
52 {
53 	phandle_t ports, child;
54 	uint32_t reg;
55 	char portnode[PORT_MAX_NAME];
56 
57 	/* First try to find a port@<idx> node */
58 	snprintf(portnode, sizeof(portnode), "port@%d", idx);
59 	child = ofw_bus_find_child(node, portnode);
60 	if (child != 0)
61 		return (child);
62 
63 	/* Next try to look under ports */
64 	ports = ofw_bus_find_child(node, "ports");
65 	if (ports == 0)
66 		return (0);
67 
68 	for (child = OF_child(ports); child != 0; child = OF_peer(child)) {
69 		if (OF_getencprop(child, "reg", &reg, sizeof(uint32_t)) <= 0 ||
70 		    reg != idx)
71 			continue;
72 
73 		return (child);
74 	}
75 
76 	return (0);
77 }
78 
79 size_t
80 ofw_graph_port_get_num_endpoints(phandle_t port)
81 {
82 	phandle_t child;
83 	char *name;
84 	size_t num = 0;
85 	int ret;
86 
87 	for (num = 0, child = OF_child(port); child != 0;
88 	     child = OF_peer(child)) {
89 		ret = OF_getprop_alloc(child, "name", (void **)&name);
90 		if (ret == -1)
91 			continue;
92 		if (strcmp(name, "endpoint") == 0)
93 			num++;
94 		else if (strncmp(name, "endpoint@", 9) == 0)
95 			num++;
96 		free(name, M_OFWPROP);
97 	}
98 
99 	return (num);
100 }
101 
102 phandle_t
103 ofw_graph_get_endpoint_by_idx(phandle_t port, uint32_t idx)
104 {
105 	phandle_t endpoint, child;
106 	uint32_t reg;
107 
108 	/* First test if we have only one endpoint */
109 	endpoint = ofw_bus_find_child(port, "endpoint");
110 	if (endpoint != 0)
111 		return (endpoint);
112 
113 	/* Then test all childs based on the reg property */
114 	for (child = OF_child(port); child != 0; child = OF_peer(child)) {
115 		if (OF_getencprop(child, "reg", &reg, sizeof(uint32_t)) <= 0 ||
116 		    reg != idx)
117 			continue;
118 
119 		return (child);
120 	}
121 
122 	return (0);
123 }
124 
125 phandle_t
126 ofw_graph_get_remote_endpoint(phandle_t endpoint)
127 {
128 	phandle_t remote;
129 
130 	if (OF_getencprop(endpoint, "remote-endpoint", &remote,
131 	      sizeof(phandle_t)) <= 0)
132 		return (0);
133 
134 	return (remote);
135 }
136 
137 phandle_t
138 ofw_graph_get_remote_parent(phandle_t remote)
139 {
140 	phandle_t node;
141 	char *name;
142 	int ret;
143 
144 	/* get the endpoint node */
145 	node = OF_node_from_xref(remote);
146 
147 	/* go to the port@X node */
148 	node = OF_parent(node);
149 	/* go to the ports node or parent */
150 	node = OF_parent(node);
151 
152 	/* if the node name is 'ports' we need to go up one last time */
153 	ret = OF_getprop_alloc(node, "name", (void **)&name);
154 	if (ret == -1) {
155 		printf("%s: Node %x don't have a name, abort\n", __func__, node);
156 		node = 0;
157 		goto end;
158 	}
159 	if (strcmp("ports", name) == 0)
160 		node = OF_parent(node);
161 
162 end:
163 	free(name, M_OFWPROP);
164 	return (node);
165 }
166 
167 device_t
168 ofw_graph_get_device_by_port_ep(phandle_t node, uint32_t port_id, uint32_t ep_id)
169 {
170 	phandle_t outport, port, endpoint, remote;
171 
172 	port = ofw_graph_get_port_by_idx(node, port_id);
173 	if (port == 0)
174 		return (NULL);
175 	endpoint = ofw_graph_get_endpoint_by_idx(port, ep_id);
176 	if (endpoint == 0)
177 		return NULL;
178 	remote = ofw_graph_get_remote_endpoint(endpoint);
179 	if (remote == 0)
180 		return (NULL);
181 	outport = ofw_graph_get_remote_parent(remote);
182 	if (outport == 0)
183 		return (NULL);
184 
185 	return (OF_device_from_xref(OF_xref_from_node(outport)));
186 }
187