1 /* SPDX-License-Identifier: GPL-2.0-only */
6 #include <console/console.h>
7 #include <device/device.h>
8 #include <ec/google/chromeec/ec.h>
14 #include <drivers/vpd/vpd.h>
16 uint64_t fw_config_get(void)
18 static uint64_t fw_config_value
;
19 static bool fw_config_value_initialized
;
21 /* Nothing to prepare if setup is already done. */
22 if (fw_config_value_initialized
)
23 return fw_config_value
;
24 fw_config_value_initialized
= true;
25 fw_config_value
= UNDEFINED_FW_CONFIG
;
27 /* Read the value from EC CBI. */
28 if (CONFIG(FW_CONFIG_SOURCE_CHROMEEC_CBI
)) {
29 if (google_chromeec_cbi_get_fw_config(&fw_config_value
))
30 printk(BIOS_WARNING
, "%s: Could not get fw_config from CBI\n",
33 printk(BIOS_INFO
, "FW_CONFIG value from CBI is 0x%" PRIx64
"\n",
37 /* Look in CBFS to allow override of value. */
38 if (CONFIG(FW_CONFIG_SOURCE_CBFS
) && fw_config_value
== UNDEFINED_FW_CONFIG
) {
39 if (cbfs_load(CONFIG_CBFS_PREFIX
"/fw_config", &fw_config_value
,
40 sizeof(fw_config_value
)) != sizeof(fw_config_value
))
41 printk(BIOS_WARNING
, "%s: Could not get fw_config from CBFS\n",
44 printk(BIOS_INFO
, "FW_CONFIG value from CBFS is 0x%" PRIx64
"\n",
48 if (CONFIG(FW_CONFIG_SOURCE_VPD
) && fw_config_value
== UNDEFINED_FW_CONFIG
) {
50 if (vpd_get_int("fw_config", VPD_RW_THEN_RO
, &vpd_value
)) {
51 fw_config_value
= vpd_value
;
52 printk(BIOS_INFO
, "FW_CONFIG value from VPD is 0x%" PRIx64
"\n",
55 printk(BIOS_WARNING
, "%s: Could not get fw_config from vpd\n",
59 return fw_config_value
;
62 bool fw_config_probe(const struct fw_config
*match
)
64 /* If fw_config is not provisioned, then there is nothing to match. */
65 if (!fw_config_is_provisioned())
68 /* Compare to system value. */
69 if ((fw_config_get() & match
->mask
) == match
->value
) {
70 if (match
->field_name
&& match
->option_name
)
71 printk(BIOS_INFO
, "fw_config match found: %s=%s\n", match
->field_name
,
74 printk(BIOS_INFO
, "fw_config match found: mask=0x%" PRIx64
" value=0x%"
76 match
->mask
, match
->value
);
83 bool fw_config_is_provisioned(void)
85 return fw_config_get() != UNDEFINED_FW_CONFIG
;
88 bool fw_config_probe_dev(const struct device
*dev
, const struct fw_config
**matching_probe
)
90 const struct fw_config
*probe
;
93 *matching_probe
= NULL
;
95 /* If the device does not have a probe list, then probing is not required. */
99 for (probe
= dev
->probe_list
; probe
&& probe
->mask
!= 0; probe
++) {
100 if (!fw_config_probe(probe
))
104 *matching_probe
= probe
;
114 * The maximum number of fw_config fields is limited by the 64-bit mask that is used to
117 #define MAX_CACHE_ELEMENTS (8 * sizeof(uint64_t))
119 static const struct fw_config
*cached_configs
[MAX_CACHE_ELEMENTS
];
121 static size_t probe_index(uint64_t mask
)
124 return __ffs64(mask
);
127 const struct fw_config
*fw_config_get_found(uint64_t field_mask
)
129 const struct fw_config
*config
;
130 config
= cached_configs
[probe_index(field_mask
)];
131 if (config
&& config
->mask
== field_mask
)
137 void fw_config_for_each_found(void (*cb
)(const struct fw_config
*config
, void *arg
), void *arg
)
141 for (i
= 0; i
< MAX_CACHE_ELEMENTS
; ++i
)
142 if (cached_configs
[i
])
143 cb(cached_configs
[i
], arg
);
146 static void fw_config_init(void *unused
)
150 for (dev
= all_devices
; dev
; dev
= dev
->next
) {
151 const struct fw_config
*probe
;
153 if (!fw_config_probe_dev(dev
, &probe
)) {
154 printk(BIOS_INFO
, "%s disabled by fw_config\n", dev_path(dev
));
160 cached_configs
[probe_index(probe
->mask
)] = probe
;
163 BOOT_STATE_INIT_ENTRY(BS_DEV_INIT_CHIPS
, BS_ON_ENTRY
, fw_config_init
, NULL
);