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> |
| 14 | |
Tim Wawrzynczak | c70505a | 2020-10-09 17:06:28 -0600 | [diff] [blame] | 15 | uint64_t fw_config_get(void) |
Duncan Laurie | 36e6c6f | 2020-05-09 19:20:10 -0700 | [diff] [blame] | 16 | { |
Tim Wawrzynczak | 24b4af6 | 2020-10-01 15:41:31 -0600 | [diff] [blame] | 17 | static uint64_t fw_config_value; |
Duncan Laurie | 36e6c6f | 2020-05-09 19:20:10 -0700 | [diff] [blame] | 18 | static bool fw_config_value_initialized; |
| 19 | |
| 20 | /* Nothing to prepare if setup is already done. */ |
| 21 | if (fw_config_value_initialized) |
| 22 | return fw_config_value; |
| 23 | fw_config_value_initialized = true; |
| 24 | |
| 25 | /* Look in CBFS to allow override of value. */ |
| 26 | if (CONFIG(FW_CONFIG_SOURCE_CBFS)) { |
Julius Werner | 834b3ec | 2020-03-04 16:52:08 -0800 | [diff] [blame^] | 27 | if (cbfs_load(CONFIG_CBFS_PREFIX "/fw_config", &fw_config_value, |
| 28 | sizeof(fw_config_value)) != sizeof(fw_config_value)) { |
Duncan Laurie | 36e6c6f | 2020-05-09 19:20:10 -0700 | [diff] [blame] | 29 | printk(BIOS_WARNING, "%s: Could not get fw_config from CBFS\n", |
| 30 | __func__); |
| 31 | fw_config_value = 0; |
| 32 | } else { |
Tim Wawrzynczak | 24b4af6 | 2020-10-01 15:41:31 -0600 | [diff] [blame] | 33 | printk(BIOS_INFO, "FW_CONFIG value from CBFS is 0x%" PRIx64 "\n", |
Duncan Laurie | 36e6c6f | 2020-05-09 19:20:10 -0700 | [diff] [blame] | 34 | fw_config_value); |
| 35 | return fw_config_value; |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | /* Read the value from EC CBI. */ |
| 40 | if (CONFIG(FW_CONFIG_SOURCE_CHROMEEC_CBI)) { |
| 41 | if (google_chromeec_cbi_get_fw_config(&fw_config_value)) |
| 42 | printk(BIOS_WARNING, "%s: Could not get fw_config from EC\n", __func__); |
| 43 | } |
| 44 | |
Tim Wawrzynczak | 24b4af6 | 2020-10-01 15:41:31 -0600 | [diff] [blame] | 45 | printk(BIOS_INFO, "FW_CONFIG value is 0x%" PRIx64 "\n", fw_config_value); |
Duncan Laurie | 36e6c6f | 2020-05-09 19:20:10 -0700 | [diff] [blame] | 46 | return fw_config_value; |
| 47 | } |
| 48 | |
| 49 | bool fw_config_probe(const struct fw_config *match) |
| 50 | { |
| 51 | /* Compare to system value. */ |
| 52 | if ((fw_config_get() & match->mask) == match->value) { |
| 53 | if (match->field_name && match->option_name) |
| 54 | printk(BIOS_INFO, "fw_config match found: %s=%s\n", match->field_name, |
| 55 | match->option_name); |
| 56 | else |
Tim Wawrzynczak | 24b4af6 | 2020-10-01 15:41:31 -0600 | [diff] [blame] | 57 | printk(BIOS_INFO, "fw_config match found: mask=0x%" PRIx64 " value=0x%" |
| 58 | PRIx64 "\n", |
Duncan Laurie | 36e6c6f | 2020-05-09 19:20:10 -0700 | [diff] [blame] | 59 | match->mask, match->value); |
| 60 | return true; |
| 61 | } |
| 62 | |
| 63 | return false; |
| 64 | } |
| 65 | |
| 66 | #if ENV_RAMSTAGE |
Tim Wawrzynczak | 299f3f8 | 2020-08-25 16:49:45 -0600 | [diff] [blame] | 67 | |
| 68 | /* |
Tim Wawrzynczak | 24b4af6 | 2020-10-01 15:41:31 -0600 | [diff] [blame] | 69 | * 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] | 70 | * represent them. |
| 71 | */ |
Tim Wawrzynczak | 24b4af6 | 2020-10-01 15:41:31 -0600 | [diff] [blame] | 72 | #define MAX_CACHE_ELEMENTS (8 * sizeof(uint64_t)) |
Tim Wawrzynczak | 299f3f8 | 2020-08-25 16:49:45 -0600 | [diff] [blame] | 73 | |
| 74 | static const struct fw_config *cached_configs[MAX_CACHE_ELEMENTS]; |
| 75 | |
Tim Wawrzynczak | 24b4af6 | 2020-10-01 15:41:31 -0600 | [diff] [blame] | 76 | static size_t probe_index(uint64_t mask) |
Tim Wawrzynczak | 299f3f8 | 2020-08-25 16:49:45 -0600 | [diff] [blame] | 77 | { |
| 78 | assert(mask); |
Tim Wawrzynczak | 24b4af6 | 2020-10-01 15:41:31 -0600 | [diff] [blame] | 79 | return __ffs64(mask); |
Tim Wawrzynczak | 299f3f8 | 2020-08-25 16:49:45 -0600 | [diff] [blame] | 80 | } |
| 81 | |
Tim Wawrzynczak | 24b4af6 | 2020-10-01 15:41:31 -0600 | [diff] [blame] | 82 | const struct fw_config *fw_config_get_found(uint64_t field_mask) |
Tim Wawrzynczak | 299f3f8 | 2020-08-25 16:49:45 -0600 | [diff] [blame] | 83 | { |
| 84 | const struct fw_config *config; |
| 85 | config = cached_configs[probe_index(field_mask)]; |
| 86 | if (config && config->mask == field_mask) |
| 87 | return config; |
| 88 | |
| 89 | return NULL; |
| 90 | } |
| 91 | |
| 92 | void fw_config_for_each_found(void (*cb)(const struct fw_config *config, void *arg), void *arg) |
| 93 | { |
| 94 | size_t i; |
| 95 | |
| 96 | for (i = 0; i < MAX_CACHE_ELEMENTS; ++i) |
| 97 | if (cached_configs[i]) |
| 98 | cb(cached_configs[i], arg); |
| 99 | } |
| 100 | |
Duncan Laurie | 36e6c6f | 2020-05-09 19:20:10 -0700 | [diff] [blame] | 101 | static void fw_config_init(void *unused) |
| 102 | { |
| 103 | struct device *dev; |
| 104 | |
| 105 | for (dev = all_devices; dev; dev = dev->next) { |
| 106 | const struct fw_config *probe; |
| 107 | bool match = false; |
| 108 | |
| 109 | if (!dev->probe_list) |
| 110 | continue; |
| 111 | |
| 112 | for (probe = dev->probe_list; probe && probe->mask != 0; probe++) { |
| 113 | if (fw_config_probe(probe)) { |
| 114 | match = true; |
Tim Wawrzynczak | 299f3f8 | 2020-08-25 16:49:45 -0600 | [diff] [blame] | 115 | cached_configs[probe_index(probe->mask)] = probe; |
Duncan Laurie | 36e6c6f | 2020-05-09 19:20:10 -0700 | [diff] [blame] | 116 | break; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | if (!match) { |
| 121 | printk(BIOS_INFO, "%s disabled by fw_config\n", dev_path(dev)); |
| 122 | dev->enabled = 0; |
| 123 | } |
| 124 | } |
| 125 | } |
Nick Vaccaro | 7994375 | 2020-10-02 11:48:17 -0700 | [diff] [blame] | 126 | 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] | 127 | #endif |