From nobody Tue Mar 29 23:00:02 2022 X-Original-To: dev-commits-src-branches@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id DA6791A34CE9; Tue, 29 Mar 2022 23:00:03 +0000 (UTC) (envelope-from git@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 "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4KSlQB4wmmz4Tjx; Tue, 29 Mar 2022 23:00:02 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1648594803; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=XrekNHy2WvM/DRm1+ruK35Q0MmkP+3WIsJHeiAMXXik=; b=cUsTg1wj8fsDy6RVq1tHGmw8ZLz7LEjK+fj/a0e7+caf9qXyQF/E9FeYuKkLhc0EmybpnQ raXwedEhh8e1AnW5rOfoi9AUxd8ENIKikZgvzUWlFV3mlCJ2rvvtt3GX1C+r4B2nwYXytS uynhG7jrPbqTvhhdXOketjO2HeMJj9YMyUivhQCJAULSiW+rxK9xxAbliJcRfvX/dCOOUI gt8v597lErWqghcXJx4M10l/eRXf6CjN6PnR7tm9/m1ohApKAQqnbhicUELKZOsxKTUUDQ gkj675CUvQcLqsAJomexYQYQR2aM59pHkdxgYZ7KtDLCoNbT7hwC/MXsmwXt5g== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (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 did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 78659276C0; Tue, 29 Mar 2022 23:00:02 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 22TN02Ki052783; Tue, 29 Mar 2022 23:00:02 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 22TN02Co052779; Tue, 29 Mar 2022 23:00:02 GMT (envelope-from git) Date: Tue, 29 Mar 2022 23:00:02 GMT Message-Id: <202203292300.22TN02Co052779@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Marcin Wojtas Subject: git: 817556add336 - stable/13 - Extend device_get_property API List-Id: Commits to the stable branches of the FreeBSD src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-branches List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-branches@freebsd.org X-BeenThere: dev-commits-src-branches@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: mw X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: 817556add33643b10c06827eb17ea1c1421b7969 Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1648594803; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=XrekNHy2WvM/DRm1+ruK35Q0MmkP+3WIsJHeiAMXXik=; b=NdlLxAoHPlXNbuIUOr6LEisgsUFa1zvs3IKBGOJ9KkxVB5l8hIAqHlHwQJiCCFCDQXoAna aS9NZVXy7tsDNV2V0KhFYz+MUh5YKl4SbnPSDj0hiVDeadm1Pqc3dIGfZ1x5wkqje+Yg2Y sE76Gnlm/TV5215zXHHjSYSu2W8ooW4xML7KpbnvEv1AgVhlmONsmMPi2Pl+67SZUAi0mk 0l+aYYMR8SU3IVwUm72V94tBhXqPbhOfn/no1c3Mz4h6w6Q98e7b2fDdEQvqDq3QeShWPx SsmnLBDrjgJuV7vgeDPEt/ydjVWoRUXnfNNlfbr+cWWnsM3jpEr+4rV1a2n4PQ== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1648594803; a=rsa-sha256; cv=none; b=H70BBbgWIJ/THPrJAjYc9+NcfFFyJqZ5ND/aX6w/FrWtIL3Qslp05f+2Is5aqcRLUf/InI pF4k5J3Sh5aeKY4pwyCsbFDMjF51J15MxZuUBnNfALOKUxL88pkfsFcpvRYxnmD8ulvERu yC1woIrgUOO5j+/hdAkq2oskKVJ5c4XCA+z63LtDyGvg+oFKkmluyDO7u5y0NoOv2rteOG KJk8aNiaLM/mVwkZD/WdYEgQpYSOwD7EPRuqmczY7Q+5pqVGX+dTP/7wZUF4SP6BPt/fWP uStN8qJxyn/9MNGNsTWdSbG4fR69xfIGwmmSdruyj8onTXIpR9/ag2GIXqkrhA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch stable/13 has been updated by mw: URL: https://cgit.FreeBSD.org/src/commit/?id=817556add33643b10c06827eb17ea1c1421b7969 commit 817556add33643b10c06827eb17ea1c1421b7969 Author: Kornel Duleba AuthorDate: 2022-01-28 09:28:37 +0000 Commit: Marcin Wojtas CommitDate: 2022-03-29 22:24:29 +0000 Extend device_get_property API In order to support various types of data stored in device tree properties or ACPI _DSD packages, create a new enum so the caller can specify the expected type of a property they want to read, according to the binding. The bus logic will use that information to process the underlying data. For example in DT all integer properties are stored in BE format. In order to get constant results across different platforms we need to convert its endianness to match the host. Another example are ACPI_TYPE_INTEGER properties stored as uint64_t. Before this patch the ACPI logic would refuse to read them if the provided buffer was smaller than 8 bytes. Now this can be handled by using DEVICE_PROP_UINT32 type. Modify the existing consumers of this API to reflect the changes and update the man pages accordingly. Reviewed by: mw Obtained from: Semihalf MFC after: 1 week Differential Revision: https://reviews.freebsd.org/D33457 (cherry picked from commit b344de4d0d163cbd8bf88cb5d226c18fe96f488f) --- share/man/man9/BUS_GET_PROPERTY.9 | 23 ++++++++++++----- share/man/man9/device_get_property.9 | 26 +++++++++++++++++-- sys/dev/acpica/acpi.c | 23 +++++++++++++++-- sys/dev/fdt/simplebus.c | 49 +++++++++++++++++++++++++++++++++--- sys/dev/mmc/mmc_helpers.c | 7 +++--- sys/dev/sdhci/sdhci_xenon.c | 9 ++++--- sys/kern/bus_if.m | 1 + sys/kern/subr_bus.c | 27 ++++++++++++++++---- sys/sys/bus.h | 21 +++++++++++++--- 9 files changed, 158 insertions(+), 28 deletions(-) diff --git a/share/man/man9/BUS_GET_PROPERTY.9 b/share/man/man9/BUS_GET_PROPERTY.9 index 89edbd95b68c..6d6bc14b4d8f 100644 --- a/share/man/man9/BUS_GET_PROPERTY.9 +++ b/share/man/man9/BUS_GET_PROPERTY.9 @@ -25,7 +25,7 @@ .\" .\" $FreeBSD$ .\" -.Dd August 19, 2021 +.Dd February 18, 2022 .Dt BUS_GET_PROPERTY 9 .Os .Sh NAME @@ -36,27 +36,38 @@ .In sys/bus.h .Ft ssize_t .Fn BUS_GET_PROPERTY "device_t dev" "device_t child" "const char *propname" \ - "void *propvalue" "size_t size" + "void *propvalue" "size_t size" "device_property_type_t type" .Sh DESCRIPTION The .Fn BUS_GET_PROPERTY method -is called from driver code which wants to access child's specific data stored +is called from driver code which wants to access a child's specific data stored on the bus. -Property consits of its name and value. +A property has a name and an associated value. Implementation shall copy to .Fa propvalue at most .Fa size bytes. +.Pp +.Fn BUS_GET_PROPERTY +supports different property types specified via the +.Fa type +argument. +The +.Fa size +is guaranteed to be a multiple of the underlying property type. +If a type is not supported, +.Fn BUS_GET_PROPERTY +shall return -1. .Sh NOTES If .Fa propvalue is NULL or .Fa size -is zero, then implementation shall only return size of the property. +is zero, the implementation shall return only the size of the property. .Sh RETURN VALUES -Property's size if successful, otherwise -1. +The property size if successful, otherwise -1. .Sh SEE ALSO .Xr device 9 , .Xr device_get_property 9 diff --git a/share/man/man9/device_get_property.9 b/share/man/man9/device_get_property.9 index 1d9bd5058b1a..d925f5f224db 100644 --- a/share/man/man9/device_get_property.9 +++ b/share/man/man9/device_get_property.9 @@ -25,7 +25,7 @@ .\" .\" $FreeBSD$ .\" -.Dd August 19, 2021 +.Dd February 18, 2022 .Dt DEVICE_GET_PROPERTY 9 .Os .Sh NAME @@ -36,13 +36,35 @@ .In sys/param.h .In sys/bus.h .Ft ssize_t -.Fn device_get_property "device_t dev" "const char *prop" "void *val" "size_t sz" +.Fn device_get_property "device_t dev" "const char *prop" "void *val" "size_t sz" \ + "device_property_type_t type" .Ft bool .Fn device_has_property "device_t dev" "const char *prop" .Sh DESCRIPTION Access device specific data provided by the parent bus. Drivers can use these properties to obtain device capabilities and set necessary quirks. +.Pp +The underlying property type is specified with the +.Fa type +argument. +Currently the following types are supported: +.Bl -tag -width ".Dv DEVICE_PROP_BUFFER" +.It Dv DEVICE_PROP_BUFFER +The underlying property is a string of bytes. +.It Dv DEVICE_PROP_ANY +Wildcard property type. +.It Dv DEVICE_PROP_UINT32 +The underlying property is an array of unsigned 32 bit integers. +The +.Fa sz +argument shall be a multiple of 4. +.It Dv DEVICE_PROP_UINT64 +The underlying property is an array of unsigned 64 bit integers. +The +.Fa sz +argument shall be a multiple of 8. +.El .Sh NOTES You can pass NULL as pointer to property's value when calling .Fn device_get_property diff --git a/sys/dev/acpica/acpi.c b/sys/dev/acpica/acpi.c index f25559d6b7ea..1242b7ce9ce1 100644 --- a/sys/dev/acpica/acpi.c +++ b/sys/dev/acpica/acpi.c @@ -145,7 +145,7 @@ static void acpi_delete_resource(device_t bus, device_t child, int type, static uint32_t acpi_isa_get_logicalid(device_t dev); static int acpi_isa_get_compatid(device_t dev, uint32_t *cids, int count); static ssize_t acpi_bus_get_prop(device_t bus, device_t child, const char *propname, - void *propvalue, size_t size); + void *propvalue, size_t size, device_property_type_t type); static int acpi_device_id_probe(device_t bus, device_t dev, char **ids, char **match); static ACPI_STATUS acpi_device_eval_obj(device_t bus, device_t dev, ACPI_STRING pathname, ACPI_OBJECT_LIST *parameters, @@ -1828,7 +1828,7 @@ acpi_find_dsd(device_t bus, device_t dev) static ssize_t acpi_bus_get_prop(device_t bus, device_t child, const char *propname, - void *propvalue, size_t size) + void *propvalue, size_t size, device_property_type_t type) { ACPI_STATUS status; const ACPI_OBJECT *obj; @@ -1838,13 +1838,32 @@ acpi_bus_get_prop(device_t bus, device_t child, const char *propname, if (ACPI_FAILURE(status)) return (-1); + switch (type) { + case DEVICE_PROP_ANY: + case DEVICE_PROP_BUFFER: + case DEVICE_PROP_UINT32: + case DEVICE_PROP_UINT64: + break; + default: + return (-1); + } + switch (obj->Type) { case ACPI_TYPE_INTEGER: + if (type == DEVICE_PROP_UINT32) { + if (propvalue != NULL && size >= sizeof(uint32_t)) + *((uint32_t *)propvalue) = obj->Integer.Value; + return (sizeof(uint32_t)); + } if (propvalue != NULL && size >= sizeof(uint64_t)) *((uint64_t *) propvalue) = obj->Integer.Value; return (sizeof(uint64_t)); case ACPI_TYPE_STRING: + if (type != DEVICE_PROP_ANY && + type != DEVICE_PROP_BUFFER) + return (-1); + if (propvalue != NULL && size > 0) memcpy(propvalue, obj->String.Pointer, MIN(size, obj->String.Length)); diff --git a/sys/dev/fdt/simplebus.c b/sys/dev/fdt/simplebus.c index ba4e58955bd2..ab7868f245bf 100644 --- a/sys/dev/fdt/simplebus.c +++ b/sys/dev/fdt/simplebus.c @@ -56,7 +56,8 @@ static struct resource_list *simplebus_get_resource_list(device_t bus, device_t child); static ssize_t simplebus_get_property(device_t bus, device_t child, - const char *propname, void *propvalue, size_t size); + const char *propname, void *propvalue, size_t size, + device_property_type_t type); /* * ofw_bus interface */ @@ -356,14 +357,56 @@ simplebus_get_resource_list(device_t bus __unused, device_t child) static ssize_t simplebus_get_property(device_t bus, device_t child, const char *propname, - void *propvalue, size_t size) + void *propvalue, size_t size, device_property_type_t type) { phandle_t node = ofw_bus_get_node(child); + ssize_t ret, i; + uint32_t *buffer; + uint64_t val; + + switch (type) { + case DEVICE_PROP_ANY: + case DEVICE_PROP_BUFFER: + case DEVICE_PROP_UINT32: + case DEVICE_PROP_UINT64: + break; + default: + return (-1); + } if (propvalue == NULL || size == 0) return (OF_getproplen(node, propname)); - return (OF_getencprop(node, propname, propvalue, size)); + /* + * Integer values are stored in BE format. + * If caller declared that the underlying property type is uint32_t + * we need to do the conversion to match host endianness. + */ + if (type == DEVICE_PROP_UINT32) + return (OF_getencprop(node, propname, propvalue, size)); + + /* + * uint64_t also requires endianness handling. + * In FDT every 8 byte value is stored using two uint32_t variables + * in BE format. Now, since the upper bits are stored as the first + * of the pair, both halves require swapping. + */ + if (type == DEVICE_PROP_UINT64) { + ret = OF_getencprop(node, propname, propvalue, size); + if (ret <= 0) { + return (ret); + } + + buffer = (uint32_t *)propvalue; + + for (i = 0; i < size / 4; i += 2) { + val = (uint64_t)buffer[i] << 32 | buffer[i + 1]; + ((uint64_t *)buffer)[i / 2] = val; + } + return (ret); + } + + return (OF_getprop(node, propname, propvalue, size)); } static struct resource * diff --git a/sys/dev/mmc/mmc_helpers.c b/sys/dev/mmc/mmc_helpers.c index 1a90f291fc47..db3337b24534 100644 --- a/sys/dev/mmc/mmc_helpers.c +++ b/sys/dev/mmc/mmc_helpers.c @@ -89,10 +89,11 @@ mmc_parse_mmc_speed(device_t dev, struct mmc_host *host) int mmc_parse(device_t dev, struct mmc_helper *helper, struct mmc_host *host) { - uint64_t bus_width, max_freq; + uint32_t bus_width, max_freq; bus_width = 0; - if (device_get_property(dev, "bus-width", &bus_width, sizeof(uint64_t)) <= 0) + if (device_get_property(dev, "bus-width", &bus_width, + sizeof(bus_width), DEVICE_PROP_UINT32) <= 0) bus_width = 1; if (bus_width >= 4) @@ -106,7 +107,7 @@ mmc_parse(device_t dev, struct mmc_helper *helper, struct mmc_host *host) * operates on */ if (device_get_property(dev, "max-frequency", &max_freq, - sizeof(uint64_t)) > 0) + sizeof(max_freq), DEVICE_PROP_UINT32) > 0) host->f_max = max_freq; if (device_has_property(dev, "broken-cd")) diff --git a/sys/dev/sdhci/sdhci_xenon.c b/sys/dev/sdhci/sdhci_xenon.c index 6dc0974c4e4e..59669affe4c5 100644 --- a/sys/dev/sdhci/sdhci_xenon.c +++ b/sys/dev/sdhci/sdhci_xenon.c @@ -471,20 +471,21 @@ static void sdhci_xenon_parse_prop(device_t dev) { struct sdhci_xenon_softc *sc; - uint64_t val; + uint32_t val; sc = device_get_softc(dev); val = 0; - if (device_get_property(dev, "quirks", &val, sizeof(val)) > 0) + if (device_get_property(dev, "quirks", + &val, sizeof(val), DEVICE_PROP_UINT32) > 0) sc->slot->quirks = val; sc->znr = XENON_ZNR_DEF_VALUE; if (device_get_property(dev, "marvell,xenon-phy-znr", - &val, sizeof(val)) > 0) + &val, sizeof(val), DEVICE_PROP_UINT32) > 0) sc->znr = val & XENON_ZNR_MASK; sc->zpr = XENON_ZPR_DEF_VALUE; if (device_get_property(dev, "marvell,xenon-phy-zpr", - &val, sizeof(val)) > 0) + &val, sizeof(val), DEVICE_PROP_UINT32) > 0) sc->zpr = val & XENON_ZPR_MASK; if (device_has_property(dev, "marvell,xenon-phy-slow-mode")) sc->slow_mode = true; diff --git a/sys/kern/bus_if.m b/sys/kern/bus_if.m index 5b6577b963ec..fa6d1d85c412 100644 --- a/sys/kern/bus_if.m +++ b/sys/kern/bus_if.m @@ -955,4 +955,5 @@ METHOD ssize_t get_property { const char *_propname; void *_propvalue; size_t _size; + device_property_type_t type; } DEFAULT bus_generic_get_property; diff --git a/sys/kern/subr_bus.c b/sys/kern/subr_bus.c index 0139eb31f976..42657034550f 100644 --- a/sys/kern/subr_bus.c +++ b/sys/kern/subr_bus.c @@ -2651,17 +2651,34 @@ device_verbose(device_t dev) } ssize_t -device_get_property(device_t dev, const char *prop, void *val, size_t sz) +device_get_property(device_t dev, const char *prop, void *val, size_t sz, + device_property_type_t type) { device_t bus = device_get_parent(dev); - return (BUS_GET_PROPERTY(bus, dev, prop, val, sz)); + switch (type) { + case DEVICE_PROP_ANY: + case DEVICE_PROP_BUFFER: + break; + case DEVICE_PROP_UINT32: + if (sz % 4 != 0) + return (-1); + break; + case DEVICE_PROP_UINT64: + if (sz % 8 != 0) + return (-1); + break; + default: + return (-1); + } + + return (BUS_GET_PROPERTY(bus, dev, prop, val, sz, type)); } bool device_has_property(device_t dev, const char *prop) { - return (device_get_property(dev, prop, NULL, 0) >= 0); + return (device_get_property(dev, prop, NULL, 0, DEVICE_PROP_ANY) >= 0); } /** @@ -4070,11 +4087,11 @@ bus_generic_write_ivar(device_t dev, device_t child, int index, */ ssize_t bus_generic_get_property(device_t dev, device_t child, const char *propname, - void *propvalue, size_t size) + void *propvalue, size_t size, device_property_type_t type) { if (device_get_parent(dev) != NULL) return (BUS_GET_PROPERTY(device_get_parent(dev), child, - propname, propvalue, size)); + propname, propvalue, size, type)); return (-1); } diff --git a/sys/sys/bus.h b/sys/sys/bus.h index 6500a283a7f5..1fc7fdf25aca 100644 --- a/sys/sys/bus.h +++ b/sys/sys/bus.h @@ -61,6 +61,20 @@ typedef enum device_state { DS_BUSY = 40 /**< @brief device is open */ } device_state_t; +/** + * @brief Device proprty types. + * + * Those are used by bus logic to encode requested properties, + * e.g. in DT all properties are stored as BE and need to be converted + * to host endianness. + */ +typedef enum device_property_type { + DEVICE_PROP_ANY = 0, + DEVICE_PROP_BUFFER = 1, + DEVICE_PROP_UINT32 = 2, + DEVICE_PROP_UINT64 = 3, +} device_property_type_t; + /** * @brief Device information exported to userspace. * The strings are placed one after the other, separated by NUL characters. @@ -441,9 +455,9 @@ bus_dma_tag_t bus_space_tag_t bus_generic_get_bus_tag(device_t dev, device_t child); int bus_generic_get_domain(device_t dev, device_t child, int *domain); -ssize_t bus_generic_get_property(device_t dev, device_t child, +ssize_t bus_generic_get_property(device_t dev, device_t child, const char *propname, void *propvalue, - size_t size); + size_t size, device_property_type_t type); struct resource_list * bus_generic_get_resource_list (device_t, device_t); int bus_generic_map_resource(device_t dev, device_t child, int type, @@ -629,7 +643,8 @@ int device_set_unit(device_t dev, int unit); /* XXX DONT USE XXX */ int device_shutdown(device_t dev); void device_unbusy(device_t dev); void device_verbose(device_t dev); -ssize_t device_get_property(device_t dev, const char *prop, void *val, size_t sz); +ssize_t device_get_property(device_t dev, const char *prop, void *val, + size_t sz, device_property_type_t type); bool device_has_property(device_t dev, const char *prop); /*