xref: /freebsd/sys/dev/ofw/ofw_graph.c (revision 2008043f386721d58158e37e0d7e50df8095942d)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
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 
30 #include <sys/cdefs.h>
31 #include "opt_platform.h"
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/bus.h>
35 #include <sys/errno.h>
36 
37 #include <dev/ofw/ofw_bus.h>
38 #include <dev/ofw/ofw_bus_subr.h>
39 #include <dev/ofw/ofw_graph.h>
40 #include <dev/ofw/openfirm.h>
41 
42 #include "ofw_bus_if.h"
43 
44 #define	PORT_MAX_NAME	8
45 
46 phandle_t
47 ofw_graph_get_port_by_idx(phandle_t node, uint32_t idx)
48 {
49 	phandle_t ports, child;
50 	uint32_t reg;
51 	char portnode[PORT_MAX_NAME];
52 
53 	/* First try to find a port@<idx> node */
54 	snprintf(portnode, sizeof(portnode), "port@%d", idx);
55 	child = ofw_bus_find_child(node, portnode);
56 	if (child != 0)
57 		return (child);
58 
59 	/* Now check for 'port' without explicit index. */
60 	if (idx == 0) {
61 		snprintf(portnode, sizeof(portnode), "port");
62 		child = ofw_bus_find_child(node, portnode);
63 		if (child != 0)
64 			return (child);
65 	}
66 
67 	/* Next try to look under ports */
68 	ports = ofw_bus_find_child(node, "ports");
69 	if (ports == 0)
70 		return (0);
71 
72 	for (child = OF_child(ports); child != 0; child = OF_peer(child)) {
73 		if (OF_getencprop(child, "reg", &reg, sizeof(uint32_t)) <= 0 ||
74 		    reg != idx)
75 			continue;
76 
77 		return (child);
78 	}
79 
80 	return (0);
81 }
82 
83 size_t
84 ofw_graph_port_get_num_endpoints(phandle_t port)
85 {
86 	phandle_t child;
87 	char *name;
88 	size_t num = 0;
89 	int ret;
90 
91 	for (num = 0, child = OF_child(port); child != 0;
92 	     child = OF_peer(child)) {
93 		ret = OF_getprop_alloc(child, "name", (void **)&name);
94 		if (ret == -1)
95 			continue;
96 		if (strcmp(name, "endpoint") == 0)
97 			num++;
98 		else if (strncmp(name, "endpoint@", 9) == 0)
99 			num++;
100 		free(name, M_OFWPROP);
101 	}
102 
103 	return (num);
104 }
105 
106 phandle_t
107 ofw_graph_get_endpoint_by_idx(phandle_t port, uint32_t idx)
108 {
109 	phandle_t endpoint, child;
110 	uint32_t reg;
111 
112 	/* First test if we have only one endpoint */
113 	endpoint = ofw_bus_find_child(port, "endpoint");
114 	if (endpoint != 0)
115 		return (endpoint);
116 
117 	/* Then test all childs based on the reg property */
118 	for (child = OF_child(port); child != 0; child = OF_peer(child)) {
119 		if (OF_getencprop(child, "reg", &reg, sizeof(uint32_t)) <= 0 ||
120 		    reg != idx)
121 			continue;
122 
123 		return (child);
124 	}
125 
126 	return (0);
127 }
128 
129 phandle_t
130 ofw_graph_get_remote_endpoint(phandle_t endpoint)
131 {
132 	phandle_t remote;
133 
134 	if (OF_getencprop(endpoint, "remote-endpoint", &remote,
135 	      sizeof(phandle_t)) <= 0)
136 		return (0);
137 
138 	return (remote);
139 }
140 
141 phandle_t
142 ofw_graph_get_remote_parent(phandle_t remote)
143 {
144 	phandle_t node;
145 	char *name;
146 	int ret;
147 
148 	/* get the endpoint node */
149 	node = OF_node_from_xref(remote);
150 
151 	/* go to the port@X node */
152 	node = OF_parent(node);
153 	/* go to the ports node or parent */
154 	node = OF_parent(node);
155 
156 	/* if the node name is 'ports' we need to go up one last time */
157 	ret = OF_getprop_alloc(node, "name", (void **)&name);
158 	if (ret == -1) {
159 		printf("%s: Node %x don't have a name, abort\n", __func__, node);
160 		node = 0;
161 		goto end;
162 	}
163 	if (strcmp("ports", name) == 0)
164 		node = OF_parent(node);
165 
166 end:
167 	free(name, M_OFWPROP);
168 	return (node);
169 }
170 
171 device_t
172 ofw_graph_get_device_by_port_ep(phandle_t node, uint32_t port_id, uint32_t ep_id)
173 {
174 	phandle_t outport, port, endpoint, remote;
175 
176 	port = ofw_graph_get_port_by_idx(node, port_id);
177 	if (port == 0)
178 		return (NULL);
179 	endpoint = ofw_graph_get_endpoint_by_idx(port, ep_id);
180 	if (endpoint == 0)
181 		return NULL;
182 	remote = ofw_graph_get_remote_endpoint(endpoint);
183 	if (remote == 0)
184 		return (NULL);
185 	outport = ofw_graph_get_remote_parent(remote);
186 	if (outport == 0)
187 		return (NULL);
188 
189 	return (OF_device_from_xref(OF_xref_from_node(outport)));
190 }
191