Duncan Laurie | 36e6c6f | 2020-05-09 19:20:10 -0700 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
| 2 | |
Tim Wawrzynczak | 299f3f8 | 2020-08-25 16:49:45 -0600 | [diff] [blame] | 3 | #include <assert.h> |
Duncan Laurie | 36e6c6f | 2020-05-09 19:20:10 -0700 | [diff] [blame] | 4 | #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 Wawrzynczak | 24b4af6 | 2020-10-01 15:41:31 -0600 | [diff] [blame] | 10 | #include <inttypes.h> |
Tim Wawrzynczak | 299f3f8 | 2020-08-25 16:49:45 -0600 | [diff] [blame] | 11 | #include <lib.h> |
Duncan Laurie | 36e6c6f | 2020-05-09 19:20:10 -0700 | [diff] [blame] | 12 | #include <stdbool.h> |
| 13 | #include <stdint.h> |
Wonkyu Kim | 43e2692 | 2021-11-01 20:55:25 -0700 | [diff] [blame] | 14 | #include <drivers/vpd/vpd.h> |
Duncan Laurie | 36e6c6f | 2020-05-09 19:20:10 -0700 | [diff] [blame] | 15 | |
Tim Wawrzynczak | c70505a | 2020-10-09 17:06:28 -0600 | [diff] [blame] | 16 | uint64_t fw_config_get(void) |
Duncan Laurie | 36e6c6f | 2020-05-09 19:20:10 -0700 | [diff] [blame] | 17 | { |
Tim Wawrzynczak | 24b4af6 | 2020-10-01 15:41:31 -0600 | [diff] [blame] | 18 | static uint64_t fw_config_value; |
Duncan Laurie | 36e6c6f | 2020-05-09 19:20:10 -0700 | [diff] [blame] | 19 | static bool fw_config_value_initialized; |
| 20 | |
| 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; |
Wonkyu Kim | 3864973 | 2021-11-01 20:15:30 -0700 | [diff] [blame] | 25 | fw_config_value = UNDEFINED_FW_CONFIG; |
Duncan Laurie | 36e6c6f | 2020-05-09 19:20:10 -0700 | [diff] [blame] | 26 | |
| 27 | /* Read the value from EC CBI. */ |
| 28 | if (CONFIG(FW_CONFIG_SOURCE_CHROMEEC_CBI)) { |
Wonkyu Kim | 3864973 | 2021-11-01 20:15:30 -0700 | [diff] [blame] | 29 | if (google_chromeec_cbi_get_fw_config(&fw_config_value)) |
| 30 | printk(BIOS_WARNING, "%s: Could not get fw_config from CBI\n", |
| 31 | __func__); |
| 32 | else |
| 33 | printk(BIOS_INFO, "FW_CONFIG value from CBI is 0x%" PRIx64 "\n", |
| 34 | fw_config_value); |
Duncan Laurie | 36e6c6f | 2020-05-09 19:20:10 -0700 | [diff] [blame] | 35 | } |
| 36 | |
Wonkyu Kim | 3864973 | 2021-11-01 20:15:30 -0700 | [diff] [blame] | 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", |
| 42 | __func__); |
| 43 | else |
| 44 | printk(BIOS_INFO, "FW_CONFIG value from CBFS is 0x%" PRIx64 "\n", |
| 45 | fw_config_value); |
| 46 | } |
| 47 | |
Wonkyu Kim | 43e2692 | 2021-11-01 20:55:25 -0700 | [diff] [blame] | 48 | if (CONFIG(FW_CONFIG_SOURCE_VPD) && fw_config_value == UNDEFINED_FW_CONFIG) { |
| 49 | int vpd_value; |
| 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", |
| 53 | fw_config_value); |
| 54 | } else |
| 55 | printk(BIOS_WARNING, "%s: Could not get fw_config from vpd\n", |
| 56 | __func__); |
| 57 | } |
| 58 | |
Duncan Laurie | 36e6c6f | 2020-05-09 19:20:10 -0700 | [diff] [blame] | 59 | return fw_config_value; |
| 60 | } |
| 61 | |
Eric Lai | 5b89bf4 | 2022-12-14 15:48:35 +0800 | [diff] [blame] | 62 | uint64_t fw_config_get_field(const struct fw_config_field *field) |
| 63 | { |
| 64 | /* If fw_config is not provisioned, then there is nothing to get. */ |
| 65 | if (!fw_config_is_provisioned()) |
| 66 | return UNDEFINED_FW_CONFIG; |
| 67 | |
| 68 | int shift = __ffs64(field->mask); |
| 69 | const uint64_t value = (fw_config_get() & field->mask) >> shift; |
| 70 | |
| 71 | printk(BIOS_INFO, "fw_config get field name=%s, mask=0x%" PRIx64 ", shift=%d, value=0x%" |
| 72 | PRIx64 "\n", field->field_name, field->mask, shift, value); |
| 73 | |
| 74 | return value; |
| 75 | } |
| 76 | |
Duncan Laurie | 36e6c6f | 2020-05-09 19:20:10 -0700 | [diff] [blame] | 77 | bool fw_config_probe(const struct fw_config *match) |
| 78 | { |
Furquan Shaikh | 17298c0 | 2021-05-20 22:08:57 -0700 | [diff] [blame] | 79 | /* If fw_config is not provisioned, then there is nothing to match. */ |
| 80 | if (!fw_config_is_provisioned()) |
| 81 | return false; |
| 82 | |
Duncan Laurie | 36e6c6f | 2020-05-09 19:20:10 -0700 | [diff] [blame] | 83 | /* Compare to system value. */ |
| 84 | if ((fw_config_get() & match->mask) == match->value) { |
| 85 | if (match->field_name && match->option_name) |
| 86 | printk(BIOS_INFO, "fw_config match found: %s=%s\n", match->field_name, |
| 87 | match->option_name); |
| 88 | else |
Tim Wawrzynczak | 24b4af6 | 2020-10-01 15:41:31 -0600 | [diff] [blame] | 89 | printk(BIOS_INFO, "fw_config match found: mask=0x%" PRIx64 " value=0x%" |
| 90 | PRIx64 "\n", |
Duncan Laurie | 36e6c6f | 2020-05-09 19:20:10 -0700 | [diff] [blame] | 91 | match->mask, match->value); |
| 92 | return true; |
| 93 | } |
| 94 | |
| 95 | return false; |
| 96 | } |
| 97 | |
Furquan Shaikh | 665891e | 2021-05-20 22:30:02 -0700 | [diff] [blame] | 98 | bool fw_config_probe_dev(const struct device *dev, const struct fw_config **matching_probe) |
| 99 | { |
| 100 | const struct fw_config *probe; |
| 101 | |
| 102 | if (matching_probe) |
| 103 | *matching_probe = NULL; |
| 104 | |
| 105 | /* If the device does not have a probe list, then probing is not required. */ |
| 106 | if (!dev->probe_list) |
| 107 | return true; |
| 108 | |
| 109 | for (probe = dev->probe_list; probe && probe->mask != 0; probe++) { |
| 110 | if (!fw_config_probe(probe)) |
| 111 | continue; |
| 112 | |
| 113 | if (matching_probe) |
| 114 | *matching_probe = probe; |
| 115 | return true; |
| 116 | } |
| 117 | |
| 118 | return false; |
| 119 | } |
| 120 | |
Duncan Laurie | 36e6c6f | 2020-05-09 19:20:10 -0700 | [diff] [blame] | 121 | #if ENV_RAMSTAGE |
Tim Wawrzynczak | 299f3f8 | 2020-08-25 16:49:45 -0600 | [diff] [blame] | 122 | |
| 123 | /* |
Tim Wawrzynczak | 24b4af6 | 2020-10-01 15:41:31 -0600 | [diff] [blame] | 124 | * The maximum number of fw_config fields is limited by the 64-bit mask that is used to |
Tim Wawrzynczak | 299f3f8 | 2020-08-25 16:49:45 -0600 | [diff] [blame] | 125 | * represent them. |
| 126 | */ |
Tim Wawrzynczak | 24b4af6 | 2020-10-01 15:41:31 -0600 | [diff] [blame] | 127 | #define MAX_CACHE_ELEMENTS (8 * sizeof(uint64_t)) |
Tim Wawrzynczak | 299f3f8 | 2020-08-25 16:49:45 -0600 | [diff] [blame] | 128 | |
| 129 | static const struct fw_config *cached_configs[MAX_CACHE_ELEMENTS]; |
| 130 | |
Tim Wawrzynczak | 24b4af6 | 2020-10-01 15:41:31 -0600 | [diff] [blame] | 131 | static size_t probe_index(uint64_t mask) |
Tim Wawrzynczak | 299f3f8 | 2020-08-25 16:49:45 -0600 | [diff] [blame] | 132 | { |
| 133 | assert(mask); |
Tim Wawrzynczak | 24b4af6 | 2020-10-01 15:41:31 -0600 | [diff] [blame] | 134 | return __ffs64(mask); |
Tim Wawrzynczak | 299f3f8 | 2020-08-25 16:49:45 -0600 | [diff] [blame] | 135 | } |
| 136 | |
Tim Wawrzynczak | 24b4af6 | 2020-10-01 15:41:31 -0600 | [diff] [blame] | 137 | const struct fw_config *fw_config_get_found(uint64_t field_mask) |
Tim Wawrzynczak | 299f3f8 | 2020-08-25 16:49:45 -0600 | [diff] [blame] | 138 | { |
| 139 | const struct fw_config *config; |
| 140 | config = cached_configs[probe_index(field_mask)]; |
| 141 | if (config && config->mask == field_mask) |
| 142 | return config; |
| 143 | |
| 144 | return NULL; |
| 145 | } |
| 146 | |
| 147 | void fw_config_for_each_found(void (*cb)(const struct fw_config *config, void *arg), void *arg) |
| 148 | { |
| 149 | size_t i; |
| 150 | |
| 151 | for (i = 0; i < MAX_CACHE_ELEMENTS; ++i) |
| 152 | if (cached_configs[i]) |
| 153 | cb(cached_configs[i], arg); |
| 154 | } |
| 155 | |
Duncan Laurie | 36e6c6f | 2020-05-09 19:20:10 -0700 | [diff] [blame] | 156 | static void fw_config_init(void *unused) |
| 157 | { |
| 158 | struct device *dev; |
| 159 | |
| 160 | for (dev = all_devices; dev; dev = dev->next) { |
| 161 | const struct fw_config *probe; |
Duncan Laurie | 36e6c6f | 2020-05-09 19:20:10 -0700 | [diff] [blame] | 162 | |
Furquan Shaikh | 665891e | 2021-05-20 22:30:02 -0700 | [diff] [blame] | 163 | if (!fw_config_probe_dev(dev, &probe)) { |
Duncan Laurie | 36e6c6f | 2020-05-09 19:20:10 -0700 | [diff] [blame] | 164 | printk(BIOS_INFO, "%s disabled by fw_config\n", dev_path(dev)); |
| 165 | dev->enabled = 0; |
Furquan Shaikh | 665891e | 2021-05-20 22:30:02 -0700 | [diff] [blame] | 166 | continue; |
Duncan Laurie | 36e6c6f | 2020-05-09 19:20:10 -0700 | [diff] [blame] | 167 | } |
Furquan Shaikh | 665891e | 2021-05-20 22:30:02 -0700 | [diff] [blame] | 168 | |
| 169 | if (probe) |
| 170 | cached_configs[probe_index(probe->mask)] = probe; |
Duncan Laurie | 36e6c6f | 2020-05-09 19:20:10 -0700 | [diff] [blame] | 171 | } |
| 172 | } |
Nick Vaccaro | 7994375 | 2020-10-02 11:48:17 -0700 | [diff] [blame] | 173 | BOOT_STATE_INIT_ENTRY(BS_DEV_INIT_CHIPS, BS_ON_ENTRY, fw_config_init, NULL); |
Duncan Laurie | 36e6c6f | 2020-05-09 19:20:10 -0700 | [diff] [blame] | 174 | #endif |