Describe a provider that exposes the contents of a UEFI variable as an NVMEM device, so data provisioned in a UEFI variable can be consumed through the standard nvmem-cells bindings. The vendor GUID is optional; when omitted the variable is looked up by name across the whole variable store. If the variable has not been provisioned, the cells read as zeroes. Assisted-by: LLM Signed-off-by: Abel Vesa --- .../devicetree/bindings/nvmem/nvmem-efivar.yaml | 73 ++++++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/Documentation/devicetree/bindings/nvmem/nvmem-efivar.yaml b/Documentation/devicetree/bindings/nvmem/nvmem-efivar.yaml new file mode 100644 index 000000000000..cdc10bee3c6f --- /dev/null +++ b/Documentation/devicetree/bindings/nvmem/nvmem-efivar.yaml @@ -0,0 +1,73 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/nvmem/nvmem-efivar.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: UEFI Variable Based nvmem Device + +maintainers: + - Abel Vesa + +description: | + Exposes the contents of a UEFI variable as an NVMEM device, so that data + provisioned in a UEFI variable can be consumed through the standard + "nvmem-cells" / "nvmem-cell-names" bindings. + + The variable is read via the EFI runtime GetVariable service, which keeps + working even on platforms where that service is emulated by firmware (for + example the Qualcomm uefisecapp backend) and where direct flash access is not + available to the non-secure world. If the variable has not been provisioned, + the cells read as zeroes. + +allOf: + - $ref: nvmem.yaml# + - $ref: nvmem-deprecated-cells.yaml# + +properties: + compatible: + const: nvmem-efivar + + efivar-name: + $ref: /schemas/types.yaml#/definitions/string + description: + Name of the UEFI variable to read. + + efivar-guid: + $ref: /schemas/types.yaml#/definitions/string + description: + Vendor GUID of the UEFI variable, in the canonical + "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" form. Optional; when omitted + the variable is looked up by name across the whole variable store. + +required: + - compatible + - efivar-name + +unevaluatedProperties: false + +examples: + - | + nvmem-efivar { + compatible = "nvmem-efivar"; + efivar-name = "ConfigData"; + #address-cells = <1>; + #size-cells = <1>; + + cell_a: cell@0 { + reg = <0x0 0x6>; + }; + + cell_b: cell@6 { + reg = <0x6 0x6>; + }; + }; + + /* A consumer references a cell through the standard nvmem bindings: + * + * &some_device { + * nvmem-cells = <&cell_a>; + * nvmem-cell-names = "some-value"; + * }; + */ +... -- 2.54.0 Add a provider that reads a UEFI variable via the EFI runtime GetVariable service and publishes its bytes as a read-only NVMEM device. NVMEM cells are described as device-tree child nodes and can be referenced by consumers through nvmem-cells / nvmem-cell-names. The vendor GUID is optional: when it is not provided, the variable is located by name by enumerating the variable store. This keeps working on platforms where GetVariable is emulated by firmware (e.g. the Qualcomm uefisecapp backend) and where direct flash access is unavailable to the non-secure world, in particular when Linux boots at EL2. If the variable has not been provisioned yet, the device is still registered, sized to cover the described cells and filled with zeroes, the same way an erased EEPROM would read. This keeps consumers probing and falling back to their default addresses. Assisted-by: LLM Signed-off-by: Abel Vesa --- drivers/nvmem/Kconfig | 12 +++ drivers/nvmem/Makefile | 2 + drivers/nvmem/efivar.c | 273 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 287 insertions(+) diff --git a/drivers/nvmem/Kconfig b/drivers/nvmem/Kconfig index 083a5e528960..3f619dfa1cf7 100644 --- a/drivers/nvmem/Kconfig +++ b/drivers/nvmem/Kconfig @@ -185,6 +185,18 @@ config NVMEM_BRCM_NVRAM This driver provides support for Broadcom's NVRAM that can be accessed using I/O mapping. +config NVMEM_EFIVAR + tristate "UEFI variable NVMEM provider" + depends on EFI || COMPILE_TEST + help + Say y here to expose the contents of a UEFI variable as an NVMEM + device. This allows the kernel to consume data provisioned in a UEFI + variable, referenced from consumers via the standard nvmem-cells + bindings. + + This driver can also be built as a module. If so, the module will + be called nvmem-efivar. + config NVMEM_IMX_IIM tristate "i.MX IC Identification Module support" depends on ARCH_MXC || COMPILE_TEST diff --git a/drivers/nvmem/Makefile b/drivers/nvmem/Makefile index 5e70bad9f0f9..038d3d9ee95c 100644 --- a/drivers/nvmem/Makefile +++ b/drivers/nvmem/Makefile @@ -27,6 +27,8 @@ obj-$(CONFIG_NVMEM_BCM_OCOTP) += nvmem-bcm-ocotp.o nvmem-bcm-ocotp-y := bcm-ocotp.o obj-$(CONFIG_NVMEM_BRCM_NVRAM) += nvmem_brcm_nvram.o nvmem_brcm_nvram-y := brcm_nvram.o +obj-$(CONFIG_NVMEM_EFIVAR) += nvmem-efivar.o +nvmem-efivar-y := efivar.o obj-$(CONFIG_NVMEM_IMX_IIM) += nvmem-imx-iim.o nvmem-imx-iim-y := imx-iim.o obj-$(CONFIG_NVMEM_IMX_OCOTP) += nvmem-imx-ocotp.o diff --git a/drivers/nvmem/efivar.c b/drivers/nvmem/efivar.c new file mode 100644 index 000000000000..699b1d67aea4 --- /dev/null +++ b/drivers/nvmem/efivar.c @@ -0,0 +1,273 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * nvmem-efivar - expose the contents of a UEFI variable as an NVMEM device + * + * Lets the kernel consume data provisioned in a UEFI variable through the + * standard NVMEM cell bindings. The variable is read at probe time through + * the EFI runtime GetVariable service, which also works on platforms where + * the variable store is emulated by firmware (for example the Qualcomm + * uefisecapp backend) and where direct flash access is not available to the + * non-secure world. The variable contents are published as a read-only + * NVMEM device, with cells described as child nodes in the device tree and + * referenced by consumers through the standard "nvmem-cells" / + * "nvmem-cell-names" properties. + * + * If the variable has not been provisioned yet, the device is still + * registered, sized to cover the described cells and filled with zeroes, + * the same way an erased EEPROM would read. Consumers are expected to + * validate the data they read so such systems keep working with their + * defaults. + * + * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +struct efivar_nvmem { + void *data; + size_t size; +}; + +static int efivar_nvmem_reg_read(void *priv, unsigned int offset, + void *val, size_t bytes) +{ + struct efivar_nvmem *p = priv; + + if (offset >= p->size || offset + bytes > p->size) + return -EINVAL; + + memcpy(val, p->data + offset, bytes); + return 0; +} + +/* Size needed to cover all cells described as child nodes. */ +static size_t efivar_nvmem_cells_extent(struct device *dev) +{ + struct device_node *child; + size_t extent = 0; + u32 reg[2]; + + for_each_child_of_node(dev->of_node, child) { + if (of_property_read_u32_array(child, "reg", reg, 2)) + continue; + extent = max_t(size_t, extent, (size_t)reg[0] + reg[1]); + } + + return extent; +} + +/* + * Find the vendor GUID of a variable given only its (UCS-2) name, by + * enumerating the variable store. Used when no 'efivar-guid' is provided. + */ +static int efivar_nvmem_find_guid(const efi_char16_t *want, efi_guid_t *out) +{ + unsigned long max = 512; /* in UCS-2 chars; variable names are short */ + efi_char16_t *name; + efi_guid_t vendor = {}; + efi_status_t status; + int ret = -ENODEV; + + name = kcalloc(max, sizeof(*name), GFP_KERNEL); + if (!name) + return -ENOMEM; + name[0] = 0; + + for (;;) { + unsigned long name_size = max * sizeof(*name); + + status = efivar_get_next_variable(&name_size, name, &vendor); + if (status == EFI_BUFFER_TOO_SMALL) { + efi_char16_t *tmp; + + max = name_size / sizeof(*name) + 1; + tmp = krealloc_array(name, max, sizeof(*name), GFP_KERNEL); + if (!tmp) { + ret = -ENOMEM; + break; + } + name = tmp; + continue; + } + if (status != EFI_SUCCESS) /* EFI_NOT_FOUND ends the walk */ + break; + + if (!ucs2_strncmp(name, want, ucs2_strlen(want) + 1)) { + *out = vendor; + ret = 0; + break; + } + } + + kfree(name); + return ret; +} + +static int efivar_nvmem_probe(struct platform_device *pdev) +{ + struct device *dev = &pdev->dev; + const char *name, *guid_str; + struct nvmem_config cfg = {}; + struct efivar_nvmem *priv; + struct nvmem_device *nvmem; + efi_char16_t *name16; + efi_guid_t guid; + guid_t g; + unsigned long size; + efi_status_t status; + u32 attr; + int i, len, ret; + void *buf; + + /* + * The EFI variable service may not be a native EFI runtime service; + * backends such as the Qualcomm uefisecapp driver register the + * efivars operations asynchronously. Wait for them. + */ + if (!efivar_is_available()) + return -EPROBE_DEFER; + + ret = device_property_read_string(dev, "efivar-name", &name); + if (ret) + return dev_err_probe(dev, ret, "missing 'efivar-name'\n"); + + /* ASCII variable name -> UCS-2 as expected by the EFI service */ + len = strlen(name); + name16 = kcalloc(len + 1, sizeof(*name16), GFP_KERNEL); + if (!name16) + return -ENOMEM; + for (i = 0; i < len; i++) + name16[i] = name[i]; + + priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); + if (!priv) { + kfree(name16); + return -ENOMEM; + } + + /* + * The vendor GUID is optional: if it is not provided, look the + * variable up by name across the whole store. A variable that has + * not been provisioned yet is reported as not found and handled + * below like an erased EEPROM. + */ + if (!device_property_read_string(dev, "efivar-guid", &guid_str)) { + ret = guid_parse(guid_str, &g); + if (ret) { + kfree(name16); + return dev_err_probe(dev, ret, "invalid 'efivar-guid'\n"); + } + memcpy(&guid, &g, sizeof(guid)); + status = EFI_SUCCESS; + } else { + ret = efivar_nvmem_find_guid(name16, &guid); + if (ret && ret != -ENODEV) { + kfree(name16); + return dev_err_probe(dev, ret, + "failed to look up variable '%s'\n", + name); + } + status = ret ? EFI_NOT_FOUND : EFI_SUCCESS; + } + + /* First attempt with a modest buffer; grow if the firmware asks. */ + size = 512; + buf = kzalloc(size, GFP_KERNEL); + if (!buf) { + kfree(name16); + return -ENOMEM; + } + + if (status == EFI_SUCCESS) { + status = efivar_get_variable(name16, &guid, &attr, &size, buf); + if (status == EFI_BUFFER_TOO_SMALL) { + kfree(buf); + buf = kzalloc(size, GFP_KERNEL); + if (!buf) { + kfree(name16); + return -ENOMEM; + } + status = efivar_get_variable(name16, &guid, &attr, + &size, buf); + } + } + kfree(name16); + + if (status == EFI_NOT_FOUND) { + /* + * Variable not provisioned yet: behave like an erased + * EEPROM so that consumers still probe and fall back to + * their defaults. + */ + kfree(buf); + size = efivar_nvmem_cells_extent(dev); + if (!size) + return dev_err_probe(dev, -ENODEV, + "variable '%s' not found and no cells described\n", + name); + buf = kzalloc(size, GFP_KERNEL); + if (!buf) + return -ENOMEM; + dev_info(dev, "variable '%s' not provisioned, reading as zeroes\n", + name); + } else if (status != EFI_SUCCESS) { + kfree(buf); + return dev_err_probe(dev, -ENODEV, + "GetVariable('%s') failed: 0x%lx\n", + name, status); + } else { + dev_dbg(dev, "variable '%s' attr=0x%x size=%lu\n", + name, attr, size); + } + + priv->data = devm_kmemdup(dev, buf, size, GFP_KERNEL); + kfree(buf); + if (!priv->data) + return -ENOMEM; + priv->size = size; + + cfg.dev = dev; + cfg.name = "efivar"; + cfg.id = NVMEM_DEVID_AUTO; + cfg.owner = THIS_MODULE; + cfg.type = NVMEM_TYPE_OTP; + cfg.read_only = true; + cfg.word_size = 1; + cfg.stride = 1; + cfg.size = priv->size; + cfg.reg_read = efivar_nvmem_reg_read; + cfg.priv = priv; + cfg.of_node = dev->of_node; + cfg.add_legacy_fixed_of_cells = true; + + nvmem = devm_nvmem_register(dev, &cfg); + return PTR_ERR_OR_ZERO(nvmem); +} + +static const struct of_device_id efivar_nvmem_of_match[] = { + { .compatible = "nvmem-efivar" }, + {} +}; +MODULE_DEVICE_TABLE(of, efivar_nvmem_of_match); + +static struct platform_driver efivar_nvmem_driver = { + .probe = efivar_nvmem_probe, + .driver = { + .name = "nvmem-efivar", + .of_match_table = efivar_nvmem_of_match, + }, +}; +module_platform_driver(efivar_nvmem_driver); + +MODULE_DESCRIPTION("NVMEM provider backed by a UEFI variable"); +MODULE_IMPORT_NS("EFIVAR"); +MODULE_LICENSE("GPL"); -- 2.54.0 Read an optional Bluetooth device address from an nvmem cell named "bd-address" (as already supported by hci_ll), and program it into the controller during setup. This lets QCA controllers source their BD_ADDR from firmware-provisioned storage such as a UEFI variable exposed via nvmem. An all-zero value is ignored, so unprovisioned storage leaves the controller default address untouched. Assisted-by: LLM Signed-off-by: Abel Vesa --- drivers/bluetooth/hci_qca.c | 48 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/drivers/bluetooth/hci_qca.c b/drivers/bluetooth/hci_qca.c index 7089e9b639b2..b704f4c3e1a4 100644 --- a/drivers/bluetooth/hci_qca.c +++ b/drivers/bluetooth/hci_qca.c @@ -25,6 +25,7 @@ #include #include #include +#include #include #include #include @@ -230,6 +231,8 @@ struct qca_serdev { bool bdaddr_property_broken; bool support_hfp_hw_offload; const char *firmware_name[2]; + bdaddr_t bdaddr; + bool bdaddr_valid; }; static int qca_regulator_enable(struct qca_serdev *qcadev); @@ -2079,6 +2082,16 @@ static int qca_setup(struct hci_uart *hu) else hu->hdev->set_bdaddr = qca_set_bdaddr; + /* Program a BD address sourced from an nvmem cell, if provided. */ + if (qcadev && qcadev->bdaddr_valid) { + int berr = hu->hdev->set_bdaddr(hdev, &qcadev->bdaddr); + + if (berr) + bt_dev_warn(hdev, "Failed to set nvmem bd-address: %d", berr); + else + bt_dev_info(hdev, "Set bd-address %pMR from nvmem", &qcadev->bdaddr); + } + if (qcadev && qcadev->support_hfp_hw_offload) qca_configure_hfp_offload(hdev); @@ -2420,6 +2433,41 @@ static int qca_serdev_probe(struct serdev_device *serdev) qcadev->bdaddr_property_broken = device_property_read_bool(&serdev->dev, "qcom,local-bd-address-broken"); + /* Optional BD address from an nvmem cell (e.g. a UEFI variable). */ + { + struct nvmem_cell *cell; + + cell = nvmem_cell_get(&serdev->dev, "bd-address"); + if (IS_ERR(cell)) { + err = PTR_ERR(cell); + if (err == -EPROBE_DEFER) + return err; + /* -ENOENT/-ENOSYS: no cell configured, ignore. */ + } else { + size_t clen; + void *cdata = nvmem_cell_read(cell, &clen); + + nvmem_cell_put(cell); + if (!IS_ERR(cdata)) { + /* + * Ignore an all-zero address: the backing + * storage has not been provisioned. + */ + if (clen == sizeof(bdaddr_t) && + bacmp(cdata, BDADDR_ANY)) { + /* + * Per the device tree bindings the nvmem + * value is MSB first, while bdaddr_t is + * LSB first. + */ + baswap(&qcadev->bdaddr, cdata); + qcadev->bdaddr_valid = true; + } + kfree(cdata); + } + } + } + if (data) qcadev->btsoc_type = data->soc_type; else -- 2.54.0 Read a MAC address from the device's firmware node (a DT "mac-address"/ "local-mac-address" property, or an nvmem cell named "mac-address" via device_get_mac_address()), and use it in preference to the address from the firmware board data. This mirrors what ath10k and ath11k already do and lets the WLAN MAC be sourced from firmware-provisioned storage such as a UEFI variable exposed through nvmem. A device tree / nvmem address takes precedence over the board-data MAC, so an explicitly provisioned address wins even when the firmware reports a valid per-pdev address. An all-zero (absent) address is ignored. Note: the provisioned address must be a valid unicast MAC; a multicast address (odd first octet) is rejected by nvmem_get_mac_address(). Assisted-by: LLM Signed-off-by: Abel Vesa --- drivers/net/wireless/ath/ath12k/mac.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/drivers/net/wireless/ath/ath12k/mac.c b/drivers/net/wireless/ath/ath12k/mac.c index 01f8c6572f65..f4d2d0377c8e 100644 --- a/drivers/net/wireless/ath/ath12k/mac.c +++ b/drivers/net/wireless/ath/ath12k/mac.c @@ -14878,15 +14878,27 @@ static int ath12k_mac_hw_register(struct ath12k_hw *ah) u32 ht_cap = U32_MAX, antennas_rx = 0, antennas_tx = 0; bool is_6ghz = false, is_raw_mode = false, is_monitor_disable = false; u8 *mac_addr = NULL; + u8 mac_from_dt[ETH_ALEN] = {}; u8 mbssid_max_interfaces = 0; wiphy->max_ap_assoc_sta = 0; + /* + * Allow a MAC address provided through the device tree (directly or + * via an nvmem cell named "mac-address") to override the one from the + * firmware board data. + */ + device_get_mac_address(ab->dev, mac_from_dt); + for_each_ar(ah, ar, i) { u32 ht_cap_info = 0; pdev = ar->pdev; - if (ar->ab->pdevs_macaddr_valid) { + if (!is_zero_ether_addr(mac_from_dt)) { + /* A device tree / nvmem address takes precedence. */ + ether_addr_copy(ar->mac_addr, mac_from_dt); + ar->mac_addr[4] += ar->pdev_idx; + } else if (ar->ab->pdevs_macaddr_valid) { ether_addr_copy(ar->mac_addr, pdev->mac_addr); } else { ether_addr_copy(ar->mac_addr, ar->ab->mac_addr); -- 2.54.0 Add an nvmem-efivar provider exposing the "MacAddresses" UEFI variable, carrying the Bluetooth BD_ADDR (bytes 0-5) and the WLAN MAC address (bytes 6-11), both MSB first. The variable is looked up by name, so no vendor GUID needs to be hardcoded. Wire the bd-address cell to the Bluetooth controller and the mac-address cell to the WLAN endpoint (pci17cb,1107) via the standard nvmem-cells binding. Until the variable is provisioned the cells read as zeroes and the controllers keep using their default addresses. Assisted-by: LLM Signed-off-by: Abel Vesa --- arch/arm64/boot/dts/qcom/glymur-crd.dtsi | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/arch/arm64/boot/dts/qcom/glymur-crd.dtsi b/arch/arm64/boot/dts/qcom/glymur-crd.dtsi index 58158b51fe29..2dd09ab1d550 100644 --- a/arch/arm64/boot/dts/qcom/glymur-crd.dtsi +++ b/arch/arm64/boot/dts/qcom/glymur-crd.dtsi @@ -31,6 +31,22 @@ chosen { stdout-path = "serial0:115200n8"; }; + /* BD_ADDR and WLAN MAC from the "MacAddresses" UEFI variable. */ + efivar_macs: nvmem-efivar { + compatible = "nvmem-efivar"; + efivar-name = "MacAddresses"; + #address-cells = <1>; + #size-cells = <1>; + + bt_bdaddr: bd-address@0 { + reg = <0x0 0x6>; + }; + + net_mac: mac-address@6 { + reg = <0x6 0x6>; + }; + }; + clocks { xo_board: xo-board { compatible = "fixed-clock"; @@ -776,6 +792,9 @@ wifi@0 { compatible = "pci17cb,1107"; reg = <0x10000 0x0 0x0 0x0 0x0>; + nvmem-cells = <&net_mac>; + nvmem-cell-names = "mac-address"; + vddaon-supply = <&vreg_pmu_aon_0p59>; vddwlcx-supply = <&vreg_pmu_wlcx_0p8>; vddwlmx-supply = <&vreg_pmu_wlmx_0p85>; @@ -1163,6 +1182,9 @@ bluetooth { compatible = "qcom,wcn7850-bt"; max-speed = <3200000>; + nvmem-cells = <&bt_bdaddr>; + nvmem-cell-names = "bd-address"; + vddaon-supply = <&vreg_pmu_aon_0p59>; vddwlcx-supply = <&vreg_pmu_wlcx_0p8>; vddwlmx-supply = <&vreg_pmu_wlmx_0p85>; -- 2.54.0