firmware: vpd: Add an interface to read VPD value
PATCH 1/1 firmware: vpd: Add an interface to read VPD value
Add an interface for other driver to query VPD value. This will be used for ASoC machine driver to query calibration data stored in VPD for smart amplifier speaker resistor calibration.
--- drivers/firmware/google/vpd.c | 16 ++++++++++++++++ include/linux/firmware/google/google_vpd.h | 18 ++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 include/linux/firmware/google/google_vpd.h
drivers/firmware/google/vpd.c +16 -0
@@ -65,6 +65,22 @@ static ssize_t vpd_attrib_read(struct file *filp, struct kobject *kobp, info->bin_attr.size); } +int vpd_attribute_read_value (bool ro,const char *key, +char **value,u32 value_len) +{ +struct vpd_attrib_info *info; +struct vpd_section *sec = ro ? &ro_vpd : &rw_vpd; + +list_for_each_entry (info, &sec->attribs, list) { +if (strcmp (info->key, key) ==0 ) { + *value =kstrndup (info->value, value_len,GFP_KERNEL ); +return 0 ; + } + } +return -EINVAL ; +} +EXPORT_SYMBOL (vpd_attribute_read_value); +/* * vpd_section_check_key_name() *
include/linux/firmware/google/google_vpd.h new file +18 -0
@@ -0,0 +1,18 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Google VPD interface. +* +* Copyright 2019 Google Inc. +*/ + +/* Interface for reading VPD value on Chrome platform. */ + +#ifndef __GOOGLE_VPD_H +#define __GOOGLE_VPD_H + +#include <linux/types.h> + +int vpd_attribute_read_value (bool ro,const char *key, +char **value,u32 value_len); + +#endif /* __GOOGLE_VPD_H */
Signature · 1 line
2.23.0.581.g78d2f28ef7-goog
Quoted 20 lines · Cheng-Yi Chiang
Add an interface for other driver to query VPD value. This will be used for ASoC machine driver to query calibration data stored in VPD for smart amplifier speaker resistor calibration.
Signed-off-by Cheng-Yi Chiang <[email protected]>--- drivers/firmware/google/vpd.c | 16 ++++++++++++++++ include/linux/firmware/google/google_vpd.h | 18 ++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 include/linux/firmware/google/google_vpd.hdrivers/firmware/google/vpd.c +0
-0@@ -65,6 +65,22 @@ static ssize_t vpd_attrib_read(struct file *filp, struct kobject *kobp, info->bin_attr.size); }
drivers/firmware/google/vpd.c static ssize_t vpd_attrib_read(struct file *filp, struct kobject *kobp, · line 76
Quoted 10 lines
drivers/firmware/google/vpd.c +9
-0+int vpd_attribute_read_value (bool ro,const char *key, +char **value,u32 value_len) +{ +struct vpd_attrib_info *info; +struct vpd_section *sec = ro ? &ro_vpd : &rw_vpd; + +list_for_each_entry (info, &sec->attribs, list) { +if (strcmp (info->key, key) ==0 ) { + *value =kstrndup (info->value, value_len,GFP_KERNEL );
Value is not necessary a NULL-terminated string. kmalloc(info->bin_attr.size) and memcpy(...) would make the most sense.
The value_len parameter makes less sense. It seems the caller knows the length of the value in advance.
Suggest to change the value_len to report the length of value. I.e. *value_len = info->bin_attr.size;
Also please check the return value for memory allocation-like functions (e.g. kstrndup, kmalloc) so that *value won't be NULL but the function returned 0.
Quoted 37 lines
drivers/firmware/google/vpd.c +7
-0+return 0 ; + } + } +return -EINVAL ; +} +EXPORT_SYMBOL (vpd_attribute_read_value); +/* * vpd_section_check_key_name() * include/linux/firmware/google/google_vpd.h new file +18
-1@@ -0,0 +1,18 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Google VPD interface. +* +* Copyright 2019 Google Inc. +*/ + +/* Interface for reading VPD value on Chrome platform. */ + +#ifndef __GOOGLE_VPD_H +#define __GOOGLE_VPD_H + +#include <linux/types.h> + +int vpd_attribute_read_value (bool ro,const char *key, +char **value,u32 value_len); + +#endif /* __GOOGLE_VPD_H */ --2.23.0.581.g78d2f28ef7-goog
Quoted 37 lines · Tzung-Bi Shih
Quoted 20 lines · Cheng-Yi Chiang
Add an interface for other driver to query VPD value. This will be used for ASoC machine driver to query calibration data stored in VPD for smart amplifier speaker resistor calibration.
Signed-off-by Cheng-Yi Chiang <[email protected]>--- drivers/firmware/google/vpd.c | 16 ++++++++++++++++ include/linux/firmware/google/google_vpd.h | 18 ++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 include/linux/firmware/google/google_vpd.hdrivers/firmware/google/vpd.c +0
-0@@ -65,6 +65,22 @@ static ssize_t vpd_attrib_read(struct file *filp, struct kobject *kobp, info->bin_attr.size); }drivers/firmware/google/vpd.c static ssize_t vpd_attrib_read(struct file *filp, struct kobject *kobp, · line 76
Quoted 10 lines
drivers/firmware/google/vpd.c +9
-0+int vpd_attribute_read_value (bool ro,const char *key, +char **value,u32 value_len) +{ +struct vpd_attrib_info *info; +struct vpd_section *sec = ro ? &ro_vpd : &rw_vpd; + +list_for_each_entry (info, &sec->attribs, list) { +if (strcmp (info->key, key) ==0 ) { + *value =kstrndup (info->value, value_len,GFP_KERNEL );Value is not necessary a NULL-terminated string.
kmalloc(info->bin_attr.size)and memcpy(...) would make the most sense.
Quoted 8 lines
The
value_lenparameter makes less sense. It seems the caller knows the length of the value in advance.
Suggest to change thevalue_lento report the length of value. I.e.*value_len=info->bin_attr.size;Also please check the return value for memory allocation-like functions (e.g. kstrndup, kmalloc) so that *value won't be NULL but the function returned 0.
Thanks for the review.
I will them in v2.
Quoted 38 lines
Quoted 38 lines
drivers/firmware/google/vpd.c +7
-0+return 0 ; + } + } +return -EINVAL ; +} +EXPORT_SYMBOL (vpd_attribute_read_value); +/* * vpd_section_check_key_name() * include/linux/firmware/google/google_vpd.h new file +18
-1@@ -0,0 +1,18 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Google VPD interface. +* +* Copyright 2019 Google Inc. +*/ + +/* Interface for reading VPD value on Chrome platform. */ + +#ifndef __GOOGLE_VPD_H +#define __GOOGLE_VPD_H + +#include <linux/types.h> + +int vpd_attribute_read_value (bool ro,const char *key, +char **value,u32 value_len); + +#endif /* __GOOGLE_VPD_H */ --2.23.0.581.g78d2f28ef7-goog
Quoted 22 lines · Tzung-Bi Shih
Quoted 21 lines · Cheng-Yi Chiang
Add an interface for other driver to query VPD value. This will be used for ASoC machine driver to query calibration data stored in VPD for smart amplifier speaker resistor calibration.
Signed-off-by Cheng-Yi Chiang <[email protected]>--- drivers/firmware/google/vpd.c | 16 ++++++++++++++++ include/linux/firmware/google/google_vpd.h | 18 ++++++++++++++++++ 2 files changed, 34 insertions(+)create mode 100644
include/linux/firmware/google/google_vpd.hdrivers/firmware/google/vpd.c +0
-0@@ -65,6 +65,22 @@ static ssize_t vpd_attrib_read(struct file *filp, struct kobject *kobp, info->bin_attr.size); }
Quoted 2 lines
drivers/firmware/google/vpd.c static ssize_t vpd_attrib_read(struct file *filp, struct kobject *kobp, · line 69
Quoted 2 lines
drivers/firmware/google/vpd.c +2
-0+int vpd_attribute_read_value (bool ro,const char *key, +char **value,u32 value_len)
FWIW, I don't think the "_value" in this function name adds any value, unless there is going to be some other read function.
The API should be documented, and state clearly that the caller must release the returned value.
Quoted 12 lines
drivers/firmware/google/vpd.c static ssize_t vpd_attrib_read(struct file *filp, struct kobject *kobp, · line 76
Quoted 7 lines
drivers/firmware/google/vpd.c +7
-0+{ +struct vpd_attrib_info *info; +struct vpd_section *sec = ro ? &ro_vpd : &rw_vpd; + +list_for_each_entry (info, &sec->attribs, list) { +if (strcmp (info->key, key) ==0 ) { + *value =kstrndup (info->value, value_len,GFP_KERNEL );Value is not necessary a NULL-terminated string.
kmalloc(info->bin_attr.size)and memcpy(...) would make the most sense.
kmemdup() ?
Quoted 4 lines
The
value_lenparameter makes less sense. It seems the caller knows the length of the value in advance.
Suggest to change thevalue_lento report the length of value. I.e.*value_len=info->bin_attr.size;
Quoted 8 lines
Quoted 1 line
Also please check the return value for memory allocation-like
functions (e.g. kstrndup, kmalloc) so that *value won't be NULL but the function returned 0.
Quoted 4 lines
drivers/firmware/google/vpd.c +4
-0+return 0 ; + } + } +return -EINVAL ;
Maybe something like -ENOENT would be more appropriate here.
Quoted 3 lines
drivers/firmware/google/vpd.c static ssize_t vpd_attrib_read(struct file *filp, struct kobject *kobp, · line 83
Quoted 3 lines
drivers/firmware/google/vpd.c +3
-0+} +EXPORT_SYMBOL (vpd_attribute_read_value); +
I would suggest to use EXPORT_SYMBOL_GPL().
Quoted 31 lines
Quoted 30 lines
/*
*vpd_section_check_key_name()
*include/linux/firmware/google/google_vpd.h new file +18
-1@@ -0,0 +1,18 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Google VPD interface. +* +* Copyright 2019 Google Inc. +*/ + +/* Interface for reading VPD value on Chrome platform. */ + +#ifndef __GOOGLE_VPD_H +#define __GOOGLE_VPD_H + +#include <linux/types.h> + +int vpd_attribute_read_value (bool ro,const char *key, +char **value,u32 value_len); + +#endif /* __GOOGLE_VPD_H */ --2.23.0.581.g78d2f28ef7-goog
Quoted 24 lines · Guenter Roeck
Quoted 22 lines · Tzung-Bi Shih
Quoted 21 lines · Cheng-Yi Chiang
Add an interface for other driver to query VPD value. This will be used for ASoC machine driver to query calibration data stored in VPD for smart amplifier speaker resistor calibration.
Signed-off-by Cheng-Yi Chiang <[email protected]>--- drivers/firmware/google/vpd.c | 16 ++++++++++++++++ include/linux/firmware/google/google_vpd.h | 18 ++++++++++++++++++ 2 files changed, 34 insertions(+)create mode 100644
include/linux/firmware/google/google_vpd.hdrivers/firmware/google/vpd.c +0
-0@@ -65,6 +65,22 @@ static ssize_t vpd_attrib_read(struct file *filp, struct kobject *kobp, info->bin_attr.size); }
Quoted 5 lines
Quoted 2 lines
drivers/firmware/google/vpd.c static ssize_t vpd_attrib_read(struct file *filp, struct kobject *kobp, · line 69
Quoted 2 lines
drivers/firmware/google/vpd.c +2
-0+int vpd_attribute_read_value (bool ro,const char *key, +char **value,u32 value_len)FWIW, I don't think the "
_value" in this function name adds any value, unless there is going to be some other read function.
ACK
Quoted 3 lines
The API should be documented, and state clearly that the caller must release the returned value.
ACK
Quoted 13 lines
Quoted 13 lines
drivers/firmware/google/vpd.c static ssize_t vpd_attrib_read(struct file *filp, struct kobject *kobp, · line 76
Quoted 8 lines
drivers/firmware/google/vpd.c +7
-0+{ +struct vpd_attrib_info *info; +struct vpd_section *sec = ro ? &ro_vpd : &rw_vpd; + +list_for_each_entry (info, &sec->attribs, list) { +if (strcmp (info->key, key) ==0 ) { + *value =kstrndup (info->value, value_len,GFP_KERNEL );Value is not necessary a NULL-terminated string.
kmalloc(info->bin_attr.size)and memcpy(...) would make the most sense.
Quoted 1 line
kmemdup()?
ACK
Quoted 9 lines
Quoted 9 lines
The
value_lenparameter makes less sense. It seems the caller knows the length of the value in advance.
Suggest to change thevalue_lento report the length of value. I.e.*value_len=info->bin_attr.size;Quoted 1 line
Also please check the return value for memory allocation-like
functions (e.g. kstrndup, kmalloc) so that *value won't be NULL but the function returned 0.
Quoted 7 lines
Quoted 4 lines
drivers/firmware/google/vpd.c static ssize_t vpd_attrib_read(struct file *filp, struct kobject *kobp, · line 80
Quoted 4 lines
drivers/firmware/google/vpd.c +4
-0+return 0 ; + } + } +return -EINVAL ;Maybe something like
-ENOENTwould be more appropriate here.
ACK
Quoted 6 lines
Quoted 3 lines
drivers/firmware/google/vpd.c static ssize_t vpd_attrib_read(struct file *filp, struct kobject *kobp, · line 83
Quoted 3 lines
drivers/firmware/google/vpd.c +3
-0+} +EXPORT_SYMBOL (vpd_attribute_read_value); +I would suggest to use
EXPORT_SYMBOL_GPL().
ACK
Hi Guenter,
Thanks for the quick review.
I'll update accordingly in v2.
Quoted 32 lines
Quoted 31 lines
Quoted 30 lines
/*
*vpd_section_check_key_name()
*include/linux/firmware/google/google_vpd.h new file +18
-1@@ -0,0 +1,18 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Google VPD interface. +* +* Copyright 2019 Google Inc. +*/ + +/* Interface for reading VPD value on Chrome platform. */ + +#ifndef __GOOGLE_VPD_H +#define __GOOGLE_VPD_H + +#include <linux/types.h> + +int vpd_attribute_read_value (bool ro,const char *key, +char **value,u32 value_len); + +#endif /* __GOOGLE_VPD_H */ --2.23.0.581.g78d2f28ef7-goog
Quoting Cheng-yi Chiang (2019-10-07 06:58:41)
Quoted 4 lines
Hi Guenter,
Thanks for the quick review.
I'll update accordingly in v2.
I'd prefer this use the nvmem framework which already handles many of the requirements discussed here. Implement an nvmem provider and figure out how to wire that up to the kernel users. Also, please include a user of the added support, otherwise it is impossible to understand how this code is used.
Quoted 7 lines · Stephen Boyd
Quoting Cheng-yi Chiang (2019-10-07 06:58:41)
Quoted 4 lines
Hi Guenter,
Thanks for the quick review.
I'll update accordingly in v2.
Quoted 6 lines
I'd prefer this use the nvmem framework which already handles many of the requirements discussed here. Implement an nvmem provider and figure out how to wire that up to the kernel users. Also, please include a user of the added support, otherwise it is impossible to understand how this code is used.
Hi Stephen,
Thanks for the suggestion.
My usage is for Intel machine driver to read a string for speaker calibration.
Based on the comments in this thread, its usage would look like
#define DSM_CALIB_KEY "dsm_calib"
static int load_calibration_data(struct cml_card_private *ctx) {
char *data = NULL;
int ret;
u32 value_len;
/* Read calibration data from VPD. */
ret = vpd_attribute_read(1, DSM_CALIB_KEY,
(u8 **)&data, &value_len);
/* Parsing of this string...*/
}It is currently pending on unmerged machine driver cml_rt1011_rt5682.c in ASoC so I can not post it for review for now.
As for nvmem approach, I looked into examples of nvmem usage, and have a rough idea how to do this.
1) In vpd.c, as it parses key and value in the VPD section, add nvmem cell with {
.name=key,
.offset=consumed, // need some change in vpd_decodec.c to get the
offset of value in the section.
.bytes=value
}
Implement read function with vpd_section as context.
2) In vpd.c, register an nvm_device using devm_nvmem_register in coreboot_driver's probe function vpd_probe.
3) As my use case does not use device tree, it is hard for ASoC machine to access nvmem device. I am wondering if I can use nvm_cell_lookup so machine driver can find the nvmem device using a con_id. But currently the cell lookup API requires a matched device, which does not fit my usage because there will be different machine drivers requesting the value.
I think I can still workaround this by adding the lookup table in machine driver. This would seem to be a bit weird because I found that most lookup table is added in provider side, not consumer side. Not sure if this is logically correct.
IMO the nvmem approach would create more complexity to support this simple usage. Plus, the underlying assumption of accessing data with offset in a buffer does not fit well with the already parsed VPD values in a list of vpd_attrib_info. But if you strongly feel that this is a better approach I can work toward this.
Thanks!
Quoting Cheng-yi Chiang (2019-10-07 11:50:31)
Quoted 53 lines · Stephen Boyd
Quoted 7 lines
Quoting Cheng-yi Chiang (2019-10-07 06:58:41)
Quoted 4 lines
Hi Guenter,
Thanks for the quick review.
I'll update accordingly in v2.Quoted 6 lines
I'd prefer this use the nvmem framework which already handles many of the requirements discussed here. Implement an nvmem provider and figure out how to wire that up to the kernel users. Also, please include a user of the added support, otherwise it is impossible to understand how this code is used.
Hi Stephen,
Thanks for the suggestion.
My usage is for Intel machine driver to read a string for speaker calibration.Based on the comments in this thread, its usage would look like
#define
DSM_CALIB_KEY"dsm_calib"static int load_calibration_data(struct cml_card_private *ctx) { char *data = NULL; int ret; u32 value_len; /* Read calibration data from VPD. */ ret = vpd_attribute_read(1, DSM_CALIB_KEY, (u8 **)&data, &value_len); /* Parsing of this string...*/ }It is currently pending on unmerged machine driver
cml_rt1011_rt5682.cin ASoC so I can not post it for review for now.As for nvmem approach, I looked into examples of nvmem usage, and have a rough idea how to do this.
1) In
vpd.c, as it parses key and value in the VPD section, add nvmem cell with {
.name=key,
.offset=consumed, // need some change invpd_decodec.cto get the
offset of value in the section.
.bytes=value
}
Implement read function withvpd_sectionas context.2) In
vpd.c, register annvm_deviceusingdevm_nvmem_registerin coreboot_driver's probe functionvpd_probe.
Quoted 10 lines
3) As my use case does not use device tree, it is hard for ASoC machine to access nvmem device. I am wondering if I can use
nvm_cell_lookupso machine driver can find the nvmem device using acon_id. But currently the cell lookup API requires a matched device, which does not fit my usage because there will be different machine drivers requesting the value.
I think I can still workaround this by adding the lookup table in machine driver. This would seem to be a bit weird because I found that most lookup table is added in provider side, not consumer side. Not sure if this is logically correct.
Maybe Srini has some input here. It looks like your main concern is consumer to provider mapping?
Quoted 7 lines
IMO the nvmem approach would create more complexity to support this simple usage. Plus, the underlying assumption of accessing data with offset in a buffer does not fit well with the already parsed VPD values in a list of
vpd_attrib_info. But if you strongly feel that this is a better approach I can work toward this.
I'm not sure how an ACPI system like this would work because my exposure to ACPI is extremely limited. I would expect there to be some sort of firmware property indicating that an nvmem should be used and it's provided by VPD or for firmware to parse VPD itself and put the information into the ACPI table for this device.
Has either of those things been done? If it is a reference/property in firmware then it should be possible to connect consumer devices like the audio device you mention to VPD via the nvmem APIs with some changes to the nvmem framework assuming there's an approach for nvmem in ACPI in some "standard" way.
I'd like to use nvmem for two reasons. First, it is a kernel framework for reading non-volatile memories, which is fairly close to what VPD is for. Second, it makes a standard, i.e. non-vpd/coreboot specific, API that drivers can use to read memories. Of course in ASoC the machine driver is already very platform specific, but the idea is to avoid platform specific APIs so that drivers are loosely coupled with the rest of the kernel. We shouldn't push against that goal by introducing more platform specific APIs.
Quoted 8 lines · Stephen Boyd
Quoting Cheng-yi Chiang (2019-10-07 11:50:31)
Quoted 5 lines
IMO the nvmem approach would create more complexity to support this simple usage. Plus, the underlying assumption of accessing data with offset in a buffer does not fit well with the already parsed VPD values in a list of
vpd_attrib_info. But if you strongly feel that this is a better approach I can work toward this.
Quoted 5 lines
I'm not sure how an ACPI system like this would work because my exposure to ACPI is extremely limited. I would expect there to be some sort of firmware property indicating that an nvmem should be used and it's provided by VPD or for firmware to parse VPD itself and put the information into the ACPI table for this device.
I fear this is optimistic. It's fairly idiomatic for ACPI for stuff like this to be keyed off DMI information rather than integrated with anything, basically once you get out of the bits that are explictly standardized you're into board file territory. There is the _DSD stuff for using DT properties in ACPI but it's had limited use AFAICT.
Quoted 10 lines · Stephen Boyd
Quoted 10 lines
3) As my use case does not use device tree, it is hard for ASoC machine to access nvmem device. I am wondering if I can use
nvm_cell_lookupso machine driver can find the nvmem device using acon_id. But currently the cell lookup API requires a matched device, which does not fit my usage because there will be different machine drivers requesting the value.
I think I can still workaround this by adding the lookup table in machine driver. This would seem to be a bit weird because I found that most lookup table is added in provider side, not consumer side. Not sure if this is logically correct.
Quoted 3 lines
Maybe Srini has some input here. It looks like your main concern is consumer to provider mapping?
In non-DT setup, there are various ways to lookup nvmem provider.
1> nvmem_device_get()/put() using provider devid/name. I think you should be able to use this in your case. 2> nvmem_register_notifier() which notifies when nvmem provider is added to system.
3> nvmem_device_find() with own match function this will be merged in next window (https://lkml.org/lkml/2019/10/3/215)
If none of these are of any help, could explain what exactly are you looking for w.r.t nvmem to be able to move to what Stephen Boyd suggested?
--srini
Quoted 28 lines · Srinivas Kandagatla
Quoted 10 lines · Stephen Boyd
Quoted 10 lines
3) As my use case does not use device tree, it is hard for ASoC machine to access nvmem device. I am wondering if I can use
nvm_cell_lookupso machine driver can find the nvmem device using acon_id. But currently the cell lookup API requires a matched device, which does not fit my usage because there will be different machine drivers requesting the value.
I think I can still workaround this by adding the lookup table in machine driver. This would seem to be a bit weird because I found that most lookup table is added in provider side, not consumer side. Not sure if this is logically correct.Quoted 3 lines
Maybe Srini has some input here. It looks like your main concern is consumer to provider mapping?
In non-DT setup, there are various ways to lookup nvmem provider.
1> nvmem_device_get()/put() using provider devid/name. I think you should be able to use this in your case.
2>nvmem_register_notifier()which notifies when nvmem provider is added to system.
3>nvmem_device_find()with own match function this will be merged in next window (https://lkml.org/lkml/2019/10/3/215)
Quoted 5 lines
If none of these are of any help, could explain what exactly are you looking for w.r.t nvmem to be able to move to what Stephen Boyd suggested?
--srini
Hi Stephen, Mark and Srinivas,
Thank you all for the suggestions.
In my non-DT setup, I have been working on coreboot changes to prepare data in _DSD following suggestion in
https://patchwork.kernel.org/patch/11179237
The basic idea is that codec driver should just get data it needs from device property.
The coreboot approach works in my local setup, but I have not sent the change to coreboot upstream yet.
If that path work, then the change needed in kernel will be much simpler.
In the future, there might be DT setup use case, and I think it should be doable for VPD to register a nvmem device, and let codec driver gets the property.
But I would drop this path for now.
Thanks!