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