Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp4147462ybb; Tue, 7 Apr 2020 01:31:36 -0700 (PDT) X-Google-Smtp-Source: APiQypLkDUlM18G1uQVnNoqRdMkDDNQ95JqqCyU8Iq9NEtzkX/6X+k8wZqr0zqHVp5DbgD8GN6IG X-Received: by 2002:aca:ecd0:: with SMTP id k199mr860753oih.60.1586248296857; Tue, 07 Apr 2020 01:31:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1586248296; cv=none; d=google.com; s=arc-20160816; b=W3U8NJN6YYbAacTwTaY6mz7WtMx20N6dpBY9qxMp6aFXxMfv8Z/PyNhFhBwR0hWSWG CDJYMud6YVBtK0yaeOgM3lnCe+XNtAi+FeIyZH2bIuht2r0rcR2Mdj3b5CKGDe5JEHx8 eyvKzrPkRSAcRUBjXj1njzhg5Dx4rMds5bgVX36v5RpBruzgARqqk202Pw/X5ydAgs51 wT9X3p89IwHiI1RCA36lUIJchCwk6u1HRQgzSFN0Hl9BZ2Vbba2EzxM3REtU+MUy6hrq DwMM5FIPNn5tIE+6BleO13y7VTgJVj9cPXhO1zAOZJuJZYTYx4MggHFOFIc32exTkEar PPGg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=1rLkkWBeHYVkUgXRLxwh4vhm1yAC8NAtadLpWozmeG4=; b=f/Gk8ozs27PlL328dctoMyXaZyaUt1QJFeCOp1ifywZ5xHBMbopMpH3L9e6AcNWUmz /5Nnu98rqgX6+wwFwMpfKaiR3prm+I3GB9M11lU4ffVkQNLcUO35LGIr+/sOTiYZt8QS cjF8P3ofR9cIzyLk0FIZkV9wT0sn6OYNCCoJQ3QUXKzcWVcb5G8Rl241Y50EArJVkHYh 261o3RQHE0CyhZb/hCsICE56ttDKLoq+RaqpDFblgyc2Vz8SWOnsAK6AtHYww5i1dQrR R/VTKYf5MX2L160u9khJ0cLUi652PGUPPrXKjBDFmhPz6Ue+Mb/BuN3lhmm+1Ql0TaEb uLNw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@9elements.com header.s=google header.b=c8zxkUo6; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=9elements.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id d2si1116855oti.128.2020.04.07.01.31.25; Tue, 07 Apr 2020 01:31:36 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@9elements.com header.s=google header.b=c8zxkUo6; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=9elements.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727962AbgDGIam (ORCPT + 99 others); Tue, 7 Apr 2020 04:30:42 -0400 Received: from mail-wr1-f65.google.com ([209.85.221.65]:45159 "EHLO mail-wr1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726637AbgDGIam (ORCPT ); Tue, 7 Apr 2020 04:30:42 -0400 Received: by mail-wr1-f65.google.com with SMTP id v5so2770658wrp.12 for ; Tue, 07 Apr 2020 01:30:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=9elements.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=1rLkkWBeHYVkUgXRLxwh4vhm1yAC8NAtadLpWozmeG4=; b=c8zxkUo6Df+hygxHgOjZtp2Dln/vY2cPgAyyx1mGzhn672X7z0ugiGi2UQ7caMPvge BatPAJbWke4rsggK/rulkMAIvCUa98Zx3+HGBb6s8rDtL841+/qjNeyGgwLnERqvZYey NiyjZF/HacUthhLuJg5zj4JKk5iwlFJ3k9oS0ErMB7omt5IP3OglLugcftto7c1+Qpho 3lCsyMe1NJmUvOWxTdSqSTSuzcmZOE7/1RV1y4VSsXFCc7UhyJsAIyZBHieT/k6N2o02 7N5BhWs1UOScjsAnOK60OlPmfyQXz9x/4JUEbOk0feXfN5WCLX3R7XA7ZLn86CdlwtZU cyFA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=1rLkkWBeHYVkUgXRLxwh4vhm1yAC8NAtadLpWozmeG4=; b=dR0/nD0ppd0XmzPaSSrXC0/RHIHJ39nyqYTcrfXjxOKotoK5VmZ22gKR8k0ZjJuxUw sV/DRe+VgI/7fKu4EN6qyDSvzVtBnQ4BRCOdHHVtzrJPxlD6etq/2miBku9Wub+Vwq80 V36Ug4jiSUIwqt5FO6vo15LxWcRQ+kyUn4q2MCSSUuTLfNNPzJXO3eTd1vqvrQB4T71G 5FfBlow1YVYGfFchcZebcrx6PJLys8tLBn9C3xd8YhDTjrrX5rWYADz5sE32SWPDJvTl mxiwIAAC2w2qP1jP/wAGTTUK2r87biTU1BrwmN7svZzNGyRfUA+/wxxItpRcNgft1U35 INgA== X-Gm-Message-State: AGi0Pua7f0k5QWIdZOWj0SXw+VzY76CtLNx8ieXECbgMDqYyuqPXXVyn C1wzE86kvb+SbCDX7Af08L3n5CvLXcTCxGsF X-Received: by 2002:adf:9d8a:: with SMTP id p10mr1406666wre.190.1586248238087; Tue, 07 Apr 2020 01:30:38 -0700 (PDT) Received: from localhost.localdomain (ip-94-114-101-228.unity-media.net. [94.114.101.228]) by smtp.gmail.com with ESMTPSA id t16sm31190743wra.17.2020.04.07.01.30.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Apr 2020 01:30:37 -0700 (PDT) From: patrick.rudolph@9elements.com To: linux-kernel@vger.kernel.org Cc: coreboot@coreboot.org, Patrick Rudolph , Thomas Gleixner , Greg Kroah-Hartman , Alexios Zavras , Allison Randal , Julius Werner , Samuel Holland , Stephen Boyd Subject: [PATCH v4 1/2] firmware: google: Expose CBMEM over sysfs Date: Tue, 7 Apr 2020 10:29:06 +0200 Message-Id: <20200407082923.2001556-2-patrick.rudolph@9elements.com> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200407082923.2001556-1-patrick.rudolph@9elements.com> References: <20200407082923.2001556-1-patrick.rudolph@9elements.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Patrick Rudolph Make all CBMEM buffers available to userland. This is useful for tools that are currently using /dev/mem. Make the id, size and address available, as well as the raw table data. Tools can easily scan the right CBMEM buffer by reading /sys/bus/coreboot/drivers/cbmem/coreboot*/cbmem_attributes/id or /sys/bus/coreboot/devices/coreboot*/cbmem_attributes/id The binary table data can then be read from /sys/bus/coreboot/drivers/cbmem/coreboot*/cbmem_attributes/data or /sys/bus/coreboot/devices/coreboot*/cbmem_attributes/data Signed-off-by: Patrick Rudolph --- -v2: - Add ABI documentation - Add 0x prefix on hex values -v3: - Use BIN_ATTR_RO -v4: - Use temporary memremap instead of persistent ioremap - Constify a struct - Get rid of unused headers - Use dev_{get|set}_drvdata - Use dev_groups to automatically handle attributes - Updated file description - Updated ABI documentation --- Documentation/ABI/stable/sysfs-bus-coreboot | 44 +++++++ drivers/firmware/google/Kconfig | 9 ++ drivers/firmware/google/Makefile | 1 + drivers/firmware/google/cbmem-coreboot.c | 128 ++++++++++++++++++++ drivers/firmware/google/coreboot_table.h | 14 +++ 5 files changed, 196 insertions(+) create mode 100644 Documentation/ABI/stable/sysfs-bus-coreboot create mode 100644 drivers/firmware/google/cbmem-coreboot.c diff --git a/Documentation/ABI/stable/sysfs-bus-coreboot b/Documentation/ABI/stable/sysfs-bus-coreboot new file mode 100644 index 000000000000..6055906f41f2 --- /dev/null +++ b/Documentation/ABI/stable/sysfs-bus-coreboot @@ -0,0 +1,44 @@ +What: /sys/bus/coreboot/devices/.../cbmem_attributes/id +Date: Apr 2020 +KernelVersion: 5.6 +Contact: Patrick Rudolph +Description: + coreboot device directory can contain a file named + cbmem_attributes/id if the device corresponds to a CBMEM + buffer. + The file holds an ASCII representation of the CBMEM ID in hex + (e.g. 0xdeadbeef). + +What: /sys/bus/coreboot/devices/.../cbmem_attributes/size +Date: Apr 2020 +KernelVersion: 5.6 +Contact: Patrick Rudolph +Description: + coreboot device directory can contain a file named + cbmem_attributes/size if the device corresponds to a CBMEM + buffer. + The file holds an representation as decimal number of the + CBMEM buffer size (e.g. 64). + +What: /sys/bus/coreboot/devices/.../cbmem_attributes/address +Date: Apr 2020 +KernelVersion: 5.6 +Contact: Patrick Rudolph +Description: + coreboot device directory can contain a file named + cbmem_attributes/address if the device corresponds to a CBMEM + buffer. + The file holds an ASCII representation of the physical address + of the CBMEM buffer in hex (e.g. 0x000000008000d000) and should + be used for debugging only. + +What: /sys/bus/coreboot/devices/.../cbmem_attributes/data +Date: Apr 2020 +KernelVersion: 5.6 +Contact: Patrick Rudolph +Description: + coreboot device directory can contain a file named + cbmem_attributes/data if the device corresponds to a CBMEM + buffer. + The file holds a read-only binary representation of the CBMEM + buffer. diff --git a/drivers/firmware/google/Kconfig b/drivers/firmware/google/Kconfig index a3a6ca659ffa..11a67c397ab3 100644 --- a/drivers/firmware/google/Kconfig +++ b/drivers/firmware/google/Kconfig @@ -58,6 +58,15 @@ config GOOGLE_FRAMEBUFFER_COREBOOT This option enables the kernel to search for a framebuffer in the coreboot table. If found, it is registered with simplefb. +config GOOGLE_CBMEM_COREBOOT + tristate "Coreboot CBMEM access" + depends on GOOGLE_COREBOOT_TABLE + help + This option exposes all available CBMEM buffers to userland. + The CBMEM id, size and address as well as the raw table data + are exported as sysfs attributes of the corresponding coreboot + table. + config GOOGLE_MEMCONSOLE_COREBOOT tristate "Firmware Memory Console" depends on GOOGLE_COREBOOT_TABLE diff --git a/drivers/firmware/google/Makefile b/drivers/firmware/google/Makefile index d17caded5d88..62053cd6d058 100644 --- a/drivers/firmware/google/Makefile +++ b/drivers/firmware/google/Makefile @@ -2,6 +2,7 @@ obj-$(CONFIG_GOOGLE_SMI) += gsmi.o obj-$(CONFIG_GOOGLE_COREBOOT_TABLE) += coreboot_table.o +obj-$(CONFIG_GOOGLE_CBMEM_COREBOOT) += cbmem-coreboot.o obj-$(CONFIG_GOOGLE_FRAMEBUFFER_COREBOOT) += framebuffer-coreboot.o obj-$(CONFIG_GOOGLE_MEMCONSOLE) += memconsole.o obj-$(CONFIG_GOOGLE_MEMCONSOLE_COREBOOT) += memconsole-coreboot.o diff --git a/drivers/firmware/google/cbmem-coreboot.c b/drivers/firmware/google/cbmem-coreboot.c new file mode 100644 index 000000000000..f76704a6eec7 --- /dev/null +++ b/drivers/firmware/google/cbmem-coreboot.c @@ -0,0 +1,128 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * cbmem-coreboot.c + * + * Exports CBMEM as attributes in sysfs. + * + * Copyright 2012-2013 David Herrmann + * Copyright 2017 Google Inc. + * Copyright 2019 9elements Agency GmbH + */ + +#include +#include +#include +#include +#include + +#include "coreboot_table.h" + +#define CB_TAG_CBMEM_ENTRY 0x31 + +struct cb_priv { + struct lb_cbmem_entry entry; +}; + +static ssize_t id_show(struct device *dev, + struct device_attribute *attr, char *buffer) +{ + const struct cb_priv *priv = dev_get_drvdata(dev); + + return sprintf(buffer, "%#08x\n", priv->entry.id); +} + +static ssize_t size_show(struct device *dev, + struct device_attribute *attr, char *buffer) +{ + const struct cb_priv *priv = dev_get_drvdata(dev); + + return sprintf(buffer, "%u\n", priv->entry.entry_size); +} + +static ssize_t address_show(struct device *dev, + struct device_attribute *attr, char *buffer) +{ + const struct cb_priv *priv = dev_get_drvdata(dev); + + return sprintf(buffer, "%#016llx\n", priv->entry.address); +} + +static DEVICE_ATTR_RO(id); +static DEVICE_ATTR_RO(size); +static DEVICE_ATTR_RO(address); + +static struct attribute *cb_mem_attrs[] = { + &dev_attr_address.attr, + &dev_attr_id.attr, + &dev_attr_size.attr, + NULL +}; + +static ssize_t data_read(struct file *filp, struct kobject *kobj, + struct bin_attribute *bin_attr, + char *buffer, loff_t offset, size_t count) +{ + const struct device *dev = kobj_to_dev(kobj); + const struct cb_priv *priv = dev_get_drvdata(dev); + void *ptr; + + /* CBMEM is always RAM with unknown caching attributes. */ + ptr = memremap(priv->entry.address, priv->entry.entry_size, + MEMREMAP_WB | MEMREMAP_WT); + if (!ptr) + return -ENOMEM; + + count = memory_read_from_buffer(buffer, count, &offset, ptr, + priv->entry.entry_size); + memunmap(ptr); + + return count; +} + +static BIN_ATTR_RO(data, 0); + +static struct bin_attribute *cb_mem_bin_attrs[] = { + &bin_attr_data, + NULL +}; + +static const struct attribute_group cb_mem_attr_group = { + .name = "cbmem_attributes", + .attrs = cb_mem_attrs, + .bin_attrs = cb_mem_bin_attrs, +}; + +static const struct attribute_group *attribute_groups[] = { + &cb_mem_attr_group, + NULL, +}; + +static int cbmem_probe(struct coreboot_device *cdev) +{ + struct device *dev = &cdev->dev; + struct cb_priv *priv; + + priv = devm_kmalloc(dev, sizeof(*priv), GFP_KERNEL); + if (!priv) + return -ENOMEM; + + memcpy(&priv->entry, &cdev->cbmem_entry, sizeof(priv->entry)); + + dev_set_drvdata(dev, priv); + + return 0; +} + +static struct coreboot_driver cbmem_driver = { + .probe = cbmem_probe, + .drv = { + .name = "cbmem", + .dev_groups = attribute_groups, + }, + .tag = CB_TAG_CBMEM_ENTRY, +}; + +module_coreboot_driver(cbmem_driver); + +MODULE_AUTHOR("9elements Agency GmbH"); +MODULE_LICENSE("GPL"); diff --git a/drivers/firmware/google/coreboot_table.h b/drivers/firmware/google/coreboot_table.h index 7b7b4a6eedda..fc20b8649882 100644 --- a/drivers/firmware/google/coreboot_table.h +++ b/drivers/firmware/google/coreboot_table.h @@ -59,6 +59,19 @@ struct lb_framebuffer { u8 reserved_mask_size; }; +/* + * There can be more than one of these records as there is one per cbmem entry. + * Describes a buffer in memory containing runtime data. + */ +struct lb_cbmem_entry { + u32 tag; + u32 size; + + u64 address; + u32 entry_size; + u32 id; +}; + /* A device, additionally with information from coreboot. */ struct coreboot_device { struct device dev; @@ -66,6 +79,7 @@ struct coreboot_device { struct coreboot_table_entry entry; struct lb_cbmem_ref cbmem_ref; struct lb_framebuffer framebuffer; + struct lb_cbmem_entry cbmem_entry; }; }; -- 2.24.1