blob: 02e2a7daddd6af31764c106cd4dcad688fe4468c [file] [log] [blame]
Duncan Laurie36e6c6f2020-05-09 19:20:10 -07001/* SPDX-License-Identifier: GPL-2.0-only */
2
Tim Wawrzynczak299f3f82020-08-25 16:49:45 -06003#include <assert.h>
Duncan Laurie36e6c6f2020-05-09 19:20:10 -07004#include <bootstate.h>
5#include <cbfs.h>
6#include <console/console.h>
7#include <device/device.h>
8#include <ec/google/chromeec/ec.h>
9#include <fw_config.h>
Tim Wawrzynczak24b4af62020-10-01 15:41:31 -060010#include <inttypes.h>
Tim Wawrzynczak299f3f82020-08-25 16:49:45 -060011#include <lib.h>
Wonkyu Kim43e26922021-11-01 20:55:25 -070012#include <drivers/vpd/vpd.h>
Duncan Laurie36e6c6f2020-05-09 19:20:10 -070013
Tim Wawrzynczakc70505a2020-10-09 17:06:28 -060014uint64_t fw_config_get(void)
Duncan Laurie36e6c6f2020-05-09 19:20:10 -070015{
Tim Wawrzynczak24b4af62020-10-01 15:41:31 -060016 static uint64_t fw_config_value;
Duncan Laurie36e6c6f2020-05-09 19:20:10 -070017 static bool fw_config_value_initialized;
18
19 /* Nothing to prepare if setup is already done. */
20 if (fw_config_value_initialized)
21 return fw_config_value;
22 fw_config_value_initialized = true;
Wonkyu Kim38649732021-11-01 20:15:30 -070023 fw_config_value = UNDEFINED_FW_CONFIG;
Duncan Laurie36e6c6f2020-05-09 19:20:10 -070024
25 /* Read the value from EC CBI. */
26 if (CONFIG(FW_CONFIG_SOURCE_CHROMEEC_CBI)) {
Wonkyu Kim38649732021-11-01 20:15:30 -070027 if (google_chromeec_cbi_get_fw_config(&fw_config_value))
28 printk(BIOS_WARNING, "%s: Could not get fw_config from CBI\n",
29 __func__);
30 else
31 printk(BIOS_INFO, "FW_CONFIG value from CBI is 0x%" PRIx64 "\n",
32 fw_config_value);
Duncan Laurie36e6c6f2020-05-09 19:20:10 -070033 }
34
Wonkyu Kim38649732021-11-01 20:15:30 -070035 /* Look in CBFS to allow override of value. */
36 if (CONFIG(FW_CONFIG_SOURCE_CBFS) && fw_config_value == UNDEFINED_FW_CONFIG) {
37 if (cbfs_load(CONFIG_CBFS_PREFIX "/fw_config", &fw_config_value,
38 sizeof(fw_config_value)) != sizeof(fw_config_value))
39 printk(BIOS_WARNING, "%s: Could not get fw_config from CBFS\n",
40 __func__);
41 else
42 printk(BIOS_INFO, "FW_CONFIG value from CBFS is 0x%" PRIx64 "\n",
43 fw_config_value);
44 }
45
Wonkyu Kim43e26922021-11-01 20:55:25 -070046 if (CONFIG(FW_CONFIG_SOURCE_VPD) && fw_config_value == UNDEFINED_FW_CONFIG) {
47 int vpd_value;
48 if (vpd_get_int("fw_config", VPD_RW_THEN_RO, &vpd_value)) {
49 fw_config_value = vpd_value;
50 printk(BIOS_INFO, "FW_CONFIG value from VPD is 0x%" PRIx64 "\n",
51 fw_config_value);
52 } else
53 printk(BIOS_WARNING, "%s: Could not get fw_config from vpd\n",
54 __func__);
55 }
56
Duncan Laurie36e6c6f2020-05-09 19:20:10 -070057 return fw_config_value;
58}
59
Eric Lai5b89bf42022-12-14 15:48:35 +080060uint64_t fw_config_get_field(const struct fw_config_field *field)
61{
62 /* If fw_config is not provisioned, then there is nothing to get. */
63 if (!fw_config_is_provisioned())
64 return UNDEFINED_FW_CONFIG;
65
66 int shift = __ffs64(field->mask);
67 const uint64_t value = (fw_config_get() & field->mask) >> shift;
68
69 printk(BIOS_INFO, "fw_config get field name=%s, mask=0x%" PRIx64 ", shift=%d, value=0x%"
70 PRIx64 "\n", field->field_name, field->mask, shift, value);
71
72 return value;
73}
74
Duncan Laurie36e6c6f2020-05-09 19:20:10 -070075bool fw_config_probe(const struct fw_config *match)
76{
Furquan Shaikh17298c02021-05-20 22:08:57 -070077 /* If fw_config is not provisioned, then there is nothing to match. */
78 if (!fw_config_is_provisioned())
79 return false;
80
Duncan Laurie36e6c6f2020-05-09 19:20:10 -070081 /* Compare to system value. */
82 if ((fw_config_get() & match->mask) == match->value) {
83 if (match->field_name && match->option_name)
84 printk(BIOS_INFO, "fw_config match found: %s=%s\n", match->field_name,
85 match->option_name);
86 else
Tim Wawrzynczak24b4af62020-10-01 15:41:31 -060087 printk(BIOS_INFO, "fw_config match found: mask=0x%" PRIx64 " value=0x%"
88 PRIx64 "\n",
Duncan Laurie36e6c6f2020-05-09 19:20:10 -070089 match->mask, match->value);
90 return true;
91 }
92
93 return false;
94}
95
Furquan Shaikh665891e2021-05-20 22:30:02 -070096bool fw_config_probe_dev(const struct device *dev, const struct fw_config **matching_probe)
97{
98 const struct fw_config *probe;
99
100 if (matching_probe)
101 *matching_probe = NULL;
102
103 /* If the device does not have a probe list, then probing is not required. */
104 if (!dev->probe_list)
105 return true;
106
107 for (probe = dev->probe_list; probe && probe->mask != 0; probe++) {
108 if (!fw_config_probe(probe))
109 continue;
110
111 if (matching_probe)
112 *matching_probe = probe;
113 return true;
114 }
115
116 return false;
117}
118
Duncan Laurie36e6c6f2020-05-09 19:20:10 -0700119#if ENV_RAMSTAGE
Tim Wawrzynczak299f3f82020-08-25 16:49:45 -0600120
121/*
Tim Wawrzynczak24b4af62020-10-01 15:41:31 -0600122 * The maximum number of fw_config fields is limited by the 64-bit mask that is used to
Tim Wawrzynczak299f3f82020-08-25 16:49:45 -0600123 * represent them.
124 */
Tim Wawrzynczak24b4af62020-10-01 15:41:31 -0600125#define MAX_CACHE_ELEMENTS (8 * sizeof(uint64_t))
Tim Wawrzynczak299f3f82020-08-25 16:49:45 -0600126
127static const struct fw_config *cached_configs[MAX_CACHE_ELEMENTS];
128
Tim Wawrzynczak24b4af62020-10-01 15:41:31 -0600129static size_t probe_index(uint64_t mask)
Tim Wawrzynczak299f3f82020-08-25 16:49:45 -0600130{
131 assert(mask);
Tim Wawrzynczak24b4af62020-10-01 15:41:31 -0600132 return __ffs64(mask);
Tim Wawrzynczak299f3f82020-08-25 16:49:45 -0600133}
134
Tim Wawrzynczak24b4af62020-10-01 15:41:31 -0600135const struct fw_config *fw_config_get_found(uint64_t field_mask)
Tim Wawrzynczak299f3f82020-08-25 16:49:45 -0600136{
137 const struct fw_config *config;
138 config = cached_configs[probe_index(field_mask)];
139 if (config && config->mask == field_mask)
140 return config;
141
142 return NULL;
143}
144
145void fw_config_for_each_found(void (*cb)(const struct fw_config *config, void *arg), void *arg)
146{
147 size_t i;
148
149 for (i = 0; i < MAX_CACHE_ELEMENTS; ++i)
150 if (cached_configs[i])
151 cb(cached_configs[i], arg);
152}
153
Duncan Laurie36e6c6f2020-05-09 19:20:10 -0700154static void fw_config_init(void *unused)
155{
156 struct device *dev;
157
158 for (dev = all_devices; dev; dev = dev->next) {
159 const struct fw_config *probe;
Duncan Laurie36e6c6f2020-05-09 19:20:10 -0700160
Furquan Shaikh665891e2021-05-20 22:30:02 -0700161 if (!fw_config_probe_dev(dev, &probe)) {
Duncan Laurie36e6c6f2020-05-09 19:20:10 -0700162 printk(BIOS_INFO, "%s disabled by fw_config\n", dev_path(dev));
163 dev->enabled = 0;
Furquan Shaikh665891e2021-05-20 22:30:02 -0700164 continue;
Duncan Laurie36e6c6f2020-05-09 19:20:10 -0700165 }
Furquan Shaikh665891e2021-05-20 22:30:02 -0700166
167 if (probe)
168 cached_configs[probe_index(probe->mask)] = probe;
Duncan Laurie36e6c6f2020-05-09 19:20:10 -0700169 }
170}
Nick Vaccaro79943752020-10-02 11:48:17 -0700171BOOT_STATE_INIT_ENTRY(BS_DEV_INIT_CHIPS, BS_ON_ENTRY, fw_config_init, NULL);
Duncan Laurie36e6c6f2020-05-09 19:20:10 -0700172#endif