Duncan Laurie | 36e6c6f | 2020-05-09 19:20:10 -0700 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
| 2 | |
| 3 | #ifndef __FW_CONFIG__ |
| 4 | #define __FW_CONFIG__ |
| 5 | |
| 6 | #include <device/device.h> |
| 7 | #include <static.h> /* Provides fw_config definitions from devicetree.cb */ |
| 8 | #include <stdbool.h> |
| 9 | #include <stdint.h> |
| 10 | |
Tim Wawrzynczak | e1a7a26 | 2020-10-09 17:07:45 -0600 | [diff] [blame] | 11 | #define UNDEFINED_FW_CONFIG ~((uint64_t)0) |
| 12 | |
Duncan Laurie | 36e6c6f | 2020-05-09 19:20:10 -0700 | [diff] [blame] | 13 | /** |
| 14 | * struct fw_config - Firmware configuration field and option. |
| 15 | * @field_name: Name of the field that this option belongs to. |
| 16 | * @option_name: Name of the option within this field. |
| 17 | * @mask: Bitmask of the field. |
| 18 | * @value: Value of the option within the mask. |
| 19 | */ |
| 20 | struct fw_config { |
| 21 | const char *field_name; |
| 22 | const char *option_name; |
Tim Wawrzynczak | 24b4af6 | 2020-10-01 15:41:31 -0600 | [diff] [blame] | 23 | uint64_t mask; |
| 24 | uint64_t value; |
Duncan Laurie | 36e6c6f | 2020-05-09 19:20:10 -0700 | [diff] [blame] | 25 | }; |
| 26 | |
Eric Lai | 5b89bf4 | 2022-12-14 15:48:35 +0800 | [diff] [blame] | 27 | struct fw_config_field { |
| 28 | const char *field_name; |
| 29 | uint64_t mask; |
| 30 | }; |
| 31 | |
Duncan Laurie | 36e6c6f | 2020-05-09 19:20:10 -0700 | [diff] [blame] | 32 | /* Generate a pointer to a compound literal of the fw_config structure. */ |
| 33 | #define FW_CONFIG(__field, __option) (&(const struct fw_config) { \ |
| 34 | .field_name = FW_CONFIG_FIELD_##__field##_NAME, \ |
| 35 | .option_name = FW_CONFIG_FIELD_##__field##_OPTION_##__option##_NAME, \ |
| 36 | .mask = FW_CONFIG_FIELD_##__field##_MASK, \ |
| 37 | .value = FW_CONFIG_FIELD_##__field##_OPTION_##__option##_VALUE \ |
| 38 | }) |
| 39 | |
Eric Lai | 5b89bf4 | 2022-12-14 15:48:35 +0800 | [diff] [blame] | 40 | #define FW_CONFIG_FIELD(__field) (&(const struct fw_config_field) { \ |
| 41 | .field_name = FW_CONFIG_FIELD_##__field##_NAME, \ |
| 42 | .mask = FW_CONFIG_FIELD_##__field##_MASK, \ |
| 43 | }) |
| 44 | |
Duncan Laurie | 36e6c6f | 2020-05-09 19:20:10 -0700 | [diff] [blame] | 45 | /** |
Tim Wawrzynczak | c70505a | 2020-10-09 17:06:28 -0600 | [diff] [blame] | 46 | * fw_config_get() - Provide firmware configuration value. |
| 47 | * |
| 48 | * Return 64bit firmware configuration value determined for the system. |
| 49 | */ |
| 50 | uint64_t fw_config_get(void); |
| 51 | |
Tim Wawrzynczak | e1a7a26 | 2020-10-09 17:07:45 -0600 | [diff] [blame] | 52 | #if CONFIG(FW_CONFIG) |
| 53 | |
Tim Wawrzynczak | c70505a | 2020-10-09 17:06:28 -0600 | [diff] [blame] | 54 | /** |
Eric Lai | 5b89bf4 | 2022-12-14 15:48:35 +0800 | [diff] [blame] | 55 | * fw_config_get_field() - Provide firmware configuration field value. |
| 56 | * @field: Structure containing field name and mask |
| 57 | * |
| 58 | * Return 64bit firmware configuration value determined for the system. |
| 59 | * Will return UNDEFINED_FW_CONFIG if unprovisioned, caller should treat |
| 60 | * as error value for the case. |
| 61 | */ |
| 62 | uint64_t fw_config_get_field(const struct fw_config_field *field); |
| 63 | |
| 64 | /** |
Duncan Laurie | 36e6c6f | 2020-05-09 19:20:10 -0700 | [diff] [blame] | 65 | * fw_config_probe() - Check if field and option matches. |
| 66 | * @match: Structure containing field and option to probe. |
| 67 | * |
| 68 | * Return %true if match is found, %false if match is not found. |
| 69 | */ |
| 70 | bool fw_config_probe(const struct fw_config *match); |
| 71 | |
Tim Wawrzynczak | 299f3f8 | 2020-08-25 16:49:45 -0600 | [diff] [blame] | 72 | /** |
| 73 | * fw_config_for_each_found() - Call a callback for each fw_config field found |
| 74 | * @cb: The callback function |
| 75 | * @arg: A context argument that is passed to the callback |
| 76 | */ |
| 77 | void fw_config_for_each_found(void (*cb)(const struct fw_config *config, void *arg), void *arg); |
| 78 | |
| 79 | /** |
Tim Wawrzynczak | 473bc8c | 2020-11-24 10:33:00 -0700 | [diff] [blame] | 80 | * fw_config_is_provisioned() - Determine if FW_CONFIG has been provisioned. |
| 81 | * Return %true if FW_CONFIG has been provisioned, %false otherwise. |
| 82 | */ |
| 83 | bool fw_config_is_provisioned(void); |
| 84 | |
| 85 | /** |
Tim Wawrzynczak | 299f3f8 | 2020-08-25 16:49:45 -0600 | [diff] [blame] | 86 | * fw_config_get_found() - Return a pointer to the fw_config struct for a given field. |
| 87 | * @field_mask: A field mask from static.h, e.g., FW_CONFIG_FIELD_FEATURE_MASK |
| 88 | * |
| 89 | * Return pointer to cached `struct fw_config` if successfully probed, otherwise NULL. |
| 90 | */ |
Tim Wawrzynczak | 24b4af6 | 2020-10-01 15:41:31 -0600 | [diff] [blame] | 91 | const struct fw_config *fw_config_get_found(uint64_t field_mask); |
Tim Wawrzynczak | 299f3f8 | 2020-08-25 16:49:45 -0600 | [diff] [blame] | 92 | |
Furquan Shaikh | 665891e | 2021-05-20 22:30:02 -0700 | [diff] [blame] | 93 | /** |
| 94 | * fw_config_probe_dev() - Check if any of the probe conditions are true for given device. |
| 95 | * @dev: Device for which probe conditions are checked |
| 96 | * @matching_probe: If any probe condition match, then the matching probe condition is returned |
| 97 | * to the caller. |
| 98 | * Return %true if device has no probing conditions or if a matching probe condition is |
| 99 | * encountered, %false otherwise. |
| 100 | */ |
| 101 | bool fw_config_probe_dev(const struct device *dev, const struct fw_config **matching_probe); |
| 102 | |
Duncan Laurie | 36e6c6f | 2020-05-09 19:20:10 -0700 | [diff] [blame] | 103 | #else |
| 104 | |
| 105 | static inline bool fw_config_probe(const struct fw_config *match) |
| 106 | { |
| 107 | /* Always return true when probing with disabled fw_config. */ |
| 108 | return true; |
| 109 | } |
| 110 | |
Furquan Shaikh | 665891e | 2021-05-20 22:30:02 -0700 | [diff] [blame] | 111 | static inline bool fw_config_probe_dev(const struct device *dev, |
| 112 | const struct fw_config **matching_probe) |
| 113 | { |
| 114 | /* Always return true when probing with disabled fw_config. */ |
| 115 | if (matching_probe) |
| 116 | *matching_probe = NULL; |
| 117 | return true; |
| 118 | } |
| 119 | |
Eric Lai | 9f2faca | 2022-12-22 08:12:51 +0800 | [diff] [blame] | 120 | static inline uint64_t fw_config_get_field(const struct fw_config_field *field) |
| 121 | { |
| 122 | /* Always return UNDEFINED_FW_CONFIG when get with disabled fw_config. */ |
| 123 | return UNDEFINED_FW_CONFIG; |
| 124 | } |
| 125 | |
Duncan Laurie | 36e6c6f | 2020-05-09 19:20:10 -0700 | [diff] [blame] | 126 | #endif /* CONFIG(FW_CONFIG) */ |
| 127 | |
| 128 | #endif /* __FW_CONFIG__ */ |