Re: [PATCH RFC 2/2] soc: Add a basic ACPI generic driver

From: John Garry
Date: Tue Jan 28 2020 - 08:33:53 EST



Hi Greg,

+
+#define pr_fmt(fmt) "SOC ACPI GENERIC: " fmt

You have a device, why do you need pr_fmt()?


The only print in the code can be removed, below, so I need not worry about this, i.e. remove it.

+
+#include <linux/acpi.h>
+#include <linux/sys_soc.h>
+
+/*
+ * Known platforms that fill in PPTT package ID structures according to
+ * ACPI spec examples, that being:
+ * - Custom driver attribute is in ID Type Structure VENDOR_ID member
+ * - SoC id is in ID Type Structure LEVEL_2_ID member
+ * See ACPI SPEC 6.2 Table 5-154 for PPTT ID Type Structure
+ */
+static struct acpi_platform_list plat_list[] = {
+ {"HISI ", "HIP08 ", 0, ACPI_SIG_PPTT, all_versions},
+ { } /* End */
+};
+
+struct acpi_generic_soc_struct {
+ struct soc_device_attribute dev_attr;
+ u32 vendor;
+};
+
+static ssize_t vendor_show(struct device *dev,
+ struct device_attribute *attr,
+ char *buf)
+{
+ struct acpi_generic_soc_struct *soc = dev_get_drvdata(dev);
+ u8 vendor_id[5] = {};
+
+ *(u32 *)vendor_id = soc->vendor;
+
+ return sprintf(buf, "%s\n", vendor_id);
+}
+
+static DEVICE_ATTR_RO(vendor);
+
+static __init int soc_acpi_generic_init(void)
+{
+ int index;
+
+ index = acpi_match_platform_list(plat_list);
+ if (index < 0)
+ return -ENOENT;
+
+ index = 0;
+ while (true) {
+ struct acpi_pptt_package_info info;
+
+ if (!acpi_pptt_get_package_info(index, &info)) {
+ struct soc_device_attribute *soc_dev_attr;
+ struct acpi_generic_soc_struct *soc;
+ struct soc_device *soc_dev;
+ u8 soc_id[9] = {};
+
+ *(u64 *)soc_id = info.LEVEL_2_ID;
+
+ soc = kzalloc(sizeof(*soc), GFP_KERNEL);
+ if (!soc)
+ return -ENOMEM;
+
+ soc_dev_attr = &soc->dev_attr;
+ soc_dev_attr->soc_id = kasprintf(GFP_KERNEL, "%s",
+ soc_id);
+ if (!soc_dev_attr->soc_id) {
+ kfree(soc);
+ return -ENOMEM;
+ }
+ soc->vendor = info.vendor_id;
+
+ soc_dev = soc_device_register(soc_dev_attr);
+ if (IS_ERR(soc_dev)) {
+ int ret = PTR_ERR(soc_dev);
+
+ pr_info("could not register soc (%d) index=%d\n",
+ ret, index);

pr_err()?

Yes, more appropriate.


And shouldn't the core print out the error, not the person who calls it?

Sure, that would sounds reasonable, but I just wanted to get the index at which we fail. I could live without it.



+ kfree(soc_dev_attr->soc_id);
+ kfree(soc);
+ return ret;
+ }
+ dev_set_drvdata(soc_device_to_device(soc_dev), soc);
+ device_create_file(soc_device_to_device(soc_dev),
+ &dev_attr_vendor);

You just raced with userspace and lost. Use the built-in api that I
made _just_ because of SOC drivers to do this correctly.


Fine, there is the soc device custom attr group which I can use. But, as Arnd said, maybe we can drop this custom file.

Cheers,
John