From owner-svn-src-stable@freebsd.org Tue Jun 16 20:22:57 2020 Return-Path: Delivered-To: svn-src-stable@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id D0F7A3329F9; Tue, 16 Jun 2020 20:22:57 +0000 (UTC) (envelope-from manu@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 49mflP58Wfz4dSw; Tue, 16 Jun 2020 20:22:57 +0000 (UTC) (envelope-from manu@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 9364B14E22; Tue, 16 Jun 2020 20:22:57 +0000 (UTC) (envelope-from manu@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id 05GKMvRB051332; Tue, 16 Jun 2020 20:22:57 GMT (envelope-from manu@FreeBSD.org) Received: (from manu@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id 05GKMuwK051329; Tue, 16 Jun 2020 20:22:56 GMT (envelope-from manu@FreeBSD.org) Message-Id: <202006162022.05GKMuwK051329@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: manu set sender to manu@FreeBSD.org using -f From: Emmanuel Vadot Date: Tue, 16 Jun 2020 20:22:56 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-12@freebsd.org Subject: svn commit: r362237 - in stable/12: share/man/man9 sys/conf sys/dev/ofw X-SVN-Group: stable-12 X-SVN-Commit-Author: manu X-SVN-Commit-Paths: in stable/12: share/man/man9 sys/conf sys/dev/ofw X-SVN-Commit-Revision: 362237 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-stable@freebsd.org X-Mailman-Version: 2.1.33 Precedence: list List-Id: SVN commit messages for all the -stable branches of the src tree List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 16 Jun 2020 20:22:57 -0000 Author: manu Date: Tue Jun 16 20:22:56 2020 New Revision: 362237 URL: https://svnweb.freebsd.org/changeset/base/362237 Log: MFC r346332: ofw_graph: Add functions for graph bindings Those functions are helpers to work on graph bindings. graphs are mostly use with video related devices. See https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/devicetree/bindings/graph.txt?id=4436a3711e3249840e0679e92d3c951bcaf25515 Differential Revision: https://reviews.freebsd.org/D19877 Added: stable/12/share/man/man9/ofw_graph.9 - copied unchanged from r346332, head/share/man/man9/ofw_graph.9 stable/12/sys/dev/ofw/ofw_graph.c - copied unchanged from r346332, head/sys/dev/ofw/ofw_graph.c stable/12/sys/dev/ofw/ofw_graph.h - copied unchanged from r346332, head/sys/dev/ofw/ofw_graph.h Modified: stable/12/sys/conf/files Directory Properties: stable/12/ (props changed) Copied: stable/12/share/man/man9/ofw_graph.9 (from r346332, head/share/man/man9/ofw_graph.9) ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ stable/12/share/man/man9/ofw_graph.9 Tue Jun 16 20:22:56 2020 (r362237, copy of r346332, head/share/man/man9/ofw_graph.9) @@ -0,0 +1,106 @@ +.\" Copyright (c) 2019 Emmanuel Vadot +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY EXPRESS OR +.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES +.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +.\" IN NO EVENT SHALL THE DEVELOPERS BE LIABLE FOR ANY DIRECT, INDIRECT, +.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +.\" +.\" $FreeBSD$ +.\" +.Dd April 10, 2019 +.Dt ofw_graph 9 +.Os +.Sh NAME +.Nm ofw_graph , +.Nm ofw_graph_get_port_by_idx , +.Nm ofw_graph_port_get_num_endpoints , +.Nm ofw_graph_get_endpoint_by_idx , +.Nm ofw_graph_get_remote_endpoint , +.Nm ofw_graph_get_remote_parent , +.Nm ofw_graph_get_device_by_port_ep , +.Nd Helpers for the graph bindings +.Sh SYNOPSIS +.In dev/ofw/openfirm.h +.In dev/ofw/ofw_graph.h +.Ft phandle_t +.Fn ofw_graph_get_port_by_idx "phandle_t node" "uint32_t idx" +.Ft size_t +.Fn ofw_graph_port_get_num_endpoints "phandle_t port" +.Ft phandle_t +.Fn ofw_graph_get_endpoint_by_idx "phandle_t port" "uint32_t idx" +.Ft phandle_t +.Fn ofw_graph_get_remote_endpoint "phandle_t endpoint" +.Ft phandle_t +.Fn ofw_graph_get_remote_parent "phandle_t remote" +.Ft device_t +.Fn ofw_graph_get_device_by_port_ep "phandle_t node" "uint32_t port_id" "uin32_t ep_id" +.Sh DESCRIPTION +.Pp +The ofw_graph functions are helpers to parse the DTS graph bindings +.Pp +.Fn ofw_graph_get_port_by_idx +return the port with id +.Fa idx . +It will first check node named +.Fa port@idx +and then fallback on checking the +.Fa ports +child for a child node matching the id. If no ports matching +.Fa idx +is found the function return 0. +.Pp +.Fn ofw_graph_port_get_num_endpoints +returns the number of endpoints a port node have. +.Pp +.Fn ofw_graph_get_endpoint_by_idx +return the endpoint with id +.Fa idx . +It will first check if there is a single child named +.Fa endpoint +and returns it if there is. If there is multiple endpoints it will check +the +.Fa reg +property and returns the correct +.Fa phandle_t +or 0 if none match. +.Pp +.Fn ofw_graph_get_remote_endpoint +returns the +.Fa remote-endpoint +property if it exists or 0. +.Pp +.Fn ofw_graph_get_remote_parent +returns the device node corresponding to the +.Fa remote-endpoint +phandle or 0 if none. +.Fn ofw_graph_get_device_by_port_ep +returns the device associated with the port and endpoint or +.Fa NULL +if none. The device driver should have called +.Fn OF_device_register_xref +before. +.Fn +.Sh HISTORY +The +.Nm ofw_graph +functions first appeared in +.Fx 13.0 . +The +.Nm ofw_graph +functions and manual page were written by +.An Emmanuel Vadot Aq Mt manu@FreeBSD.org . Modified: stable/12/sys/conf/files ============================================================================== --- stable/12/sys/conf/files Tue Jun 16 19:21:28 2020 (r362236) +++ stable/12/sys/conf/files Tue Jun 16 20:22:56 2020 (r362237) @@ -2628,6 +2628,7 @@ dev/ofw/ofw_bus_subr.c optional fdt dev/ofw/ofw_cpu.c optional fdt dev/ofw/ofw_fdt.c optional fdt dev/ofw/ofw_if.m optional fdt +dev/ofw/ofw_graph.c optional fdt dev/ofw/ofw_subr.c optional fdt dev/ofw/ofwbus.c optional fdt dev/ofw/openfirm.c optional fdt Copied: stable/12/sys/dev/ofw/ofw_graph.c (from r346332, head/sys/dev/ofw/ofw_graph.c) ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ stable/12/sys/dev/ofw/ofw_graph.c Tue Jun 16 20:22:56 2020 (r362237, copy of r346332, head/sys/dev/ofw/ofw_graph.c) @@ -0,0 +1,186 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause-FreeBSD + * + * Copyright (c) 2019 Emmanuel Vadot + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions, and the following disclaimer, + * without modification, immediately at the beginning of the file. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#include +__FBSDID("$FreeBSD$"); + +#include "opt_platform.h" +#include +#include +#include +#include + +#include +#include +#include +#include + +#include "ofw_bus_if.h" + +#define PORT_MAX_NAME 8 + +phandle_t +ofw_graph_get_port_by_idx(phandle_t node, uint32_t idx) +{ + phandle_t ports, child; + uint32_t reg; + char portnode[PORT_MAX_NAME]; + + /* First try to find a port@ node */ + snprintf(portnode, sizeof(portnode), "port@%d", idx); + child = ofw_bus_find_child(node, portnode); + if (child != 0) + return (child); + + /* Next try to look under ports */ + ports = ofw_bus_find_child(node, "ports"); + if (ports == 0) + return (0); + + for (child = OF_child(ports); child != 0; child = OF_peer(child)) { + if (OF_getencprop(child, "reg", ®, sizeof(uint32_t)) <= 0 || + reg != idx) + continue; + + return (child); + } + + return (0); +} + +size_t +ofw_graph_port_get_num_endpoints(phandle_t port) +{ + phandle_t child; + char *name; + size_t num = 0; + int ret; + + for (num = 0, child = OF_child(port); child != 0; + child = OF_peer(child)) { + ret = OF_getprop_alloc(child, "name", (void **)&name); + if (ret == -1) + continue; + if (strcmp(name, "endpoint") == 0) + num++; + else if (strncmp(name, "endpoint@", 9) == 0) + num++; + free(name, M_OFWPROP); + } + + return (num); +} + +phandle_t +ofw_graph_get_endpoint_by_idx(phandle_t port, uint32_t idx) +{ + phandle_t endpoint, child; + uint32_t reg; + + /* First test if we have only one endpoint */ + endpoint = ofw_bus_find_child(port, "endpoint"); + if (endpoint != 0) + return (endpoint); + + /* Then test all childs based on the reg property */ + for (child = OF_child(port); child != 0; child = OF_peer(child)) { + if (OF_getencprop(child, "reg", ®, sizeof(uint32_t)) <= 0 || + reg != idx) + continue; + + return (child); + } + + return (0); +} + +phandle_t +ofw_graph_get_remote_endpoint(phandle_t endpoint) +{ + phandle_t remote; + + if (OF_getencprop(endpoint, "remote-endpoint", &remote, + sizeof(phandle_t)) <= 0) + return (0); + + return (remote); +} + +phandle_t +ofw_graph_get_remote_parent(phandle_t remote) +{ + phandle_t node; + char *name; + int ret; + + /* get the endpoint node */ + node = OF_node_from_xref(remote); + + /* go to the port@X node */ + node = OF_parent(node); + /* go to the ports node or parent */ + node = OF_parent(node); + + /* if the node name is 'ports' we need to go up one last time */ + ret = OF_getprop_alloc(node, "name", (void **)&name); + if (ret == -1) { + printf("%s: Node %x don't have a name, abort\n", __func__, node); + node = 0; + goto end; + } + if (strcmp("ports", name) == 0) + node = OF_parent(node); + +end: + free(name, M_OFWPROP); + return (node); +} + +device_t +ofw_graph_get_device_by_port_ep(phandle_t node, uint32_t port_id, uint32_t ep_id) +{ + phandle_t outport, port, endpoint, remote; + + port = ofw_graph_get_port_by_idx(node, port_id); + if (port == 0) + return (NULL); + endpoint = ofw_graph_get_endpoint_by_idx(port, ep_id); + if (endpoint == 0) + return NULL; + remote = ofw_graph_get_remote_endpoint(endpoint); + if (remote == 0) + return (NULL); + outport = ofw_graph_get_remote_parent(remote); + if (outport == 0) + return (NULL); + + return (OF_device_from_xref(OF_xref_from_node(outport))); +} Copied: stable/12/sys/dev/ofw/ofw_graph.h (from r346332, head/sys/dev/ofw/ofw_graph.h) ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ stable/12/sys/dev/ofw/ofw_graph.h Tue Jun 16 20:22:56 2020 (r362237, copy of r346332, head/sys/dev/ofw/ofw_graph.h) @@ -0,0 +1,44 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause-FreeBSD + * + * Copyright (c) 2019 Emmanuel Vadot + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions, and the following disclaimer, + * without modification, immediately at the beginning of the file. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#ifndef _OFW_GRAPH_H_ +#define _OFW_GRAPH_H_ + +phandle_t ofw_graph_get_port_by_idx(phandle_t node, uint32_t idx); +phandle_t ofw_graph_get_remote_endpoint(phandle_t endpoint); +size_t ofw_graph_port_get_num_endpoints(phandle_t port); +phandle_t ofw_graph_get_endpoint_by_idx(phandle_t port, uint32_t idx); +phandle_t ofw_graph_get_remote_parent(phandle_t remote); + +device_t ofw_graph_get_device_by_port_ep(phandle_t node, uint32_t port_id, uint32_t ep_id); + +#endif /* _OFW_GRAPH_H_ */ +