blob: 1e1652efb6bc8bc4449dc2059eedc0fa0f160050 [file] [log] [blame]
Duncan Laurie36e6c6f2020-05-09 19:20:10 -07001/* 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 Wawrzynczake1a7a262020-10-09 17:07:45 -060011#define UNDEFINED_FW_CONFIG ~((uint64_t)0)
12
Duncan Laurie36e6c6f2020-05-09 19:20:10 -070013/**
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 */
20struct fw_config {
21 const char *field_name;
22 const char *option_name;
Tim Wawrzynczak24b4af62020-10-01 15:41:31 -060023 uint64_t mask;
24 uint64_t value;
Duncan Laurie36e6c6f2020-05-09 19:20:10 -070025};
26
Eric Lai5b89bf42022-12-14 15:48:35 +080027struct fw_config_field {
28 const char *field_name;
29 uint64_t mask;
30};
31
Duncan Laurie36e6c6f2020-05-09 19:20:10 -070032/* 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 Lai5b89bf42022-12-14 15:48:35 +080040#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 Laurie36e6c6f2020-05-09 19:20:10 -070045/**
Tim Wawrzynczakc70505a2020-10-09 17:06:28 -060046 * fw_config_get() - Provide firmware configuration value.
47 *
48 * Return 64bit firmware configuration value determined for the system.
49 */
50uint64_t fw_config_get(void);
51
Jakub Czapigaf6ae1a92023-06-15 09:16:30 +020052/**
53 * fw_config_is_provisioned() - Determine if FW_CONFIG has been provisioned.
54 * Return %true if FW_CONFIG has been provisioned, %false otherwise.
55 */
56static inline bool fw_config_is_provisioned(void)
57{
58 return fw_config_get() != UNDEFINED_FW_CONFIG;
59}
60
61
Tim Wawrzynczake1a7a262020-10-09 17:07:45 -060062#if CONFIG(FW_CONFIG)
63
Tim Wawrzynczakc70505a2020-10-09 17:06:28 -060064/**
Eric Lai5b89bf42022-12-14 15:48:35 +080065 * fw_config_get_field() - Provide firmware configuration field value.
66 * @field: Structure containing field name and mask
67 *
68 * Return 64bit firmware configuration value determined for the system.
69 * Will return UNDEFINED_FW_CONFIG if unprovisioned, caller should treat
70 * as error value for the case.
71 */
72uint64_t fw_config_get_field(const struct fw_config_field *field);
73
74/**
Duncan Laurie36e6c6f2020-05-09 19:20:10 -070075 * fw_config_probe() - Check if field and option matches.
76 * @match: Structure containing field and option to probe.
77 *
78 * Return %true if match is found, %false if match is not found.
79 */
80bool fw_config_probe(const struct fw_config *match);
81
Tim Wawrzynczak299f3f82020-08-25 16:49:45 -060082/**
83 * fw_config_for_each_found() - Call a callback for each fw_config field found
84 * @cb: The callback function
85 * @arg: A context argument that is passed to the callback
86 */
87void fw_config_for_each_found(void (*cb)(const struct fw_config *config, void *arg), void *arg);
88
89/**
90 * fw_config_get_found() - Return a pointer to the fw_config struct for a given field.
91 * @field_mask: A field mask from static.h, e.g., FW_CONFIG_FIELD_FEATURE_MASK
92 *
93 * Return pointer to cached `struct fw_config` if successfully probed, otherwise NULL.
94*/
Tim Wawrzynczak24b4af62020-10-01 15:41:31 -060095const struct fw_config *fw_config_get_found(uint64_t field_mask);
Tim Wawrzynczak299f3f82020-08-25 16:49:45 -060096
Furquan Shaikh665891e2021-05-20 22:30:02 -070097/**
98 * fw_config_probe_dev() - Check if any of the probe conditions are true for given device.
99 * @dev: Device for which probe conditions are checked
100 * @matching_probe: If any probe condition match, then the matching probe condition is returned
101 * to the caller.
102 * Return %true if device has no probing conditions or if a matching probe condition is
103 * encountered, %false otherwise.
104 */
105bool fw_config_probe_dev(const struct device *dev, const struct fw_config **matching_probe);
106
Duncan Laurie36e6c6f2020-05-09 19:20:10 -0700107#else
108
109static inline bool fw_config_probe(const struct fw_config *match)
110{
111 /* Always return true when probing with disabled fw_config. */
112 return true;
113}
114
Furquan Shaikh665891e2021-05-20 22:30:02 -0700115static inline bool fw_config_probe_dev(const struct device *dev,
116 const struct fw_config **matching_probe)
117{
118 /* Always return true when probing with disabled fw_config. */
119 if (matching_probe)
120 *matching_probe = NULL;
121 return true;
122}
123
Eric Lai9f2faca2022-12-22 08:12:51 +0800124static inline uint64_t fw_config_get_field(const struct fw_config_field *field)
125{
126 /* Always return UNDEFINED_FW_CONFIG when get with disabled fw_config. */
127 return UNDEFINED_FW_CONFIG;
128}
129
Duncan Laurie36e6c6f2020-05-09 19:20:10 -0700130#endif /* CONFIG(FW_CONFIG) */
131
132#endif /* __FW_CONFIG__ */