xref: /freebsd-13-stable/sys/dev/ofw/ofw_graph.c (revision 3bc80996974a61a4223eae4c1ccd47b6ee32a48a)
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
ofw_graph_get_port_by_idx(phandle_t node,uint32_t idx)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 	/* Next try to look under ports */
60 	ports = ofw_bus_find_child(node, "ports");
61 	if (ports == 0)
62 		return (0);
63 
64 	for (child = OF_child(ports); child != 0; child = OF_peer(child)) {
65 		if (OF_getencprop(child, "reg", &reg, sizeof(uint32_t)) <= 0 ||
66 		    reg != idx)
67 			continue;
68 
69 		return (child);
70 	}
71 
72 	return (0);
73 }
74 
75 size_t
ofw_graph_port_get_num_endpoints(phandle_t port)76 ofw_graph_port_get_num_endpoints(phandle_t port)
77 {
78 	phandle_t child;
79 	char *name;
80 	size_t num = 0;
81 	int ret;
82 
83 	for (num = 0, child = OF_child(port); child != 0;
84 	     child = OF_peer(child)) {
85 		ret = OF_getprop_alloc(child, "name", (void **)&name);
86 		if (ret == -1)
87 			continue;
88 		if (strcmp(name, "endpoint") == 0)
89 			num++;
90 		else if (strncmp(name, "endpoint@", 9) == 0)
91 			num++;
92 		free(name, M_OFWPROP);
93 	}
94 
95 	return (num);
96 }
97 
98 phandle_t
ofw_graph_get_endpoint_by_idx(phandle_t port,uint32_t idx)99 ofw_graph_get_endpoint_by_idx(phandle_t port, uint32_t idx)
100 {
101 	phandle_t endpoint, child;
102 	uint32_t reg;
103 
104 	/* First test if we have only one endpoint */
105 	endpoint = ofw_bus_find_child(port, "endpoint");
106 	if (endpoint != 0)
107 		return (endpoint);
108 
109 	/* Then test all childs based on the reg property */
110 	for (child = OF_child(port); child != 0; child = OF_peer(child)) {
111 		if (OF_getencprop(child, "reg", &reg, sizeof(uint32_t)) <= 0 ||
112 		    reg != idx)
113 			continue;
114 
115 		return (child);
116 	}
117 
118 	return (0);
119 }
120 
121 phandle_t
ofw_graph_get_remote_endpoint(phandle_t endpoint)122 ofw_graph_get_remote_endpoint(phandle_t endpoint)
123 {
124 	phandle_t remote;
125 
126 	if (OF_getencprop(endpoint, "remote-endpoint", &remote,
127 	      sizeof(phandle_t)) <= 0)
128 		return (0);
129 
130 	return (remote);
131 }
132 
133 phandle_t
ofw_graph_get_remote_parent(phandle_t remote)134 ofw_graph_get_remote_parent(phandle_t remote)
135 {
136 	phandle_t node;
137 	char *name;
138 	int ret;
139 
140 	/* get the endpoint node */
141 	node = OF_node_from_xref(remote);
142 
143 	/* go to the port@X node */
144 	node = OF_parent(node);
145 	/* go to the ports node or parent */
146 	node = OF_parent(node);
147 
148 	/* if the node name is 'ports' we need to go up one last time */
149 	ret = OF_getprop_alloc(node, "name", (void **)&name);
150 	if (ret == -1) {
151 		printf("%s: Node %x don't have a name, abort\n", __func__, node);
152 		node = 0;
153 		goto end;
154 	}
155 	if (strcmp("ports", name) == 0)
156 		node = OF_parent(node);
157 
158 end:
159 	free(name, M_OFWPROP);
160 	return (node);
161 }
162 
163 device_t
ofw_graph_get_device_by_port_ep(phandle_t node,uint32_t port_id,uint32_t ep_id)164 ofw_graph_get_device_by_port_ep(phandle_t node, uint32_t port_id, uint32_t ep_id)
165 {
166 	phandle_t outport, port, endpoint, remote;
167 
168 	port = ofw_graph_get_port_by_idx(node, port_id);
169 	if (port == 0)
170 		return (NULL);
171 	endpoint = ofw_graph_get_endpoint_by_idx(port, ep_id);
172 	if (endpoint == 0)
173 		return NULL;
174 	remote = ofw_graph_get_remote_endpoint(endpoint);
175 	if (remote == 0)
176 		return (NULL);
177 	outport = ofw_graph_get_remote_parent(remote);
178 	if (outport == 0)
179 		return (NULL);
180 
181 	return (OF_device_from_xref(OF_xref_from_node(outport)));
182 }
183