1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __FIRMWARE_LOADER_H
3 #define __FIRMWARE_LOADER_H
4 
5 #include <linux/bitops.h>
6 #include <linux/firmware.h>
7 #include <linux/types.h>
8 #include <linux/kref.h>
9 #include <linux/list.h>
10 #include <linux/completion.h>
11 
12 #include <generated/utsrelease.h>
13 
14 /**
15  * enum fw_opt - options to control firmware loading behaviour
16  *
17  * @FW_OPT_UEVENT: Enables the fallback mechanism to send a kobject uevent
18  *	when the firmware is not found. Userspace is in charge to load the
19  *	firmware using the sysfs loading facility.
20  * @FW_OPT_NOWAIT: Used to describe the firmware request is asynchronous.
21  * @FW_OPT_USERHELPER: Enable the fallback mechanism, in case the direct
22  *	filesystem lookup fails at finding the firmware.  For details refer to
23  *	firmware_fallback_sysfs().
24  * @FW_OPT_NO_WARN: Quiet, avoid printing warning messages.
25  * @FW_OPT_NOCACHE: Disables firmware caching. Firmware caching is used to
26  *	cache the firmware upon suspend, so that upon resume races against the
27  *	firmware file lookup on storage is avoided. Used for calls where the
28  *	file may be too big, or where the driver takes charge of its own
29  *	firmware caching mechanism.
30  * @FW_OPT_NOFALLBACK_SYSFS: Disable the sysfs fallback mechanism. Takes
31  *	precedence over &FW_OPT_UEVENT and &FW_OPT_USERHELPER.
32  * @FW_OPT_FALLBACK_PLATFORM: Enable fallback to device fw copy embedded in
33  *	the platform's main firmware. If both this fallback and the sysfs
34  *      fallback are enabled, then this fallback will be tried first.
35  * @FW_OPT_PARTIAL: Allow partial read of firmware instead of needing to read
36  *	entire file.
37  */
38 enum fw_opt {
39 	FW_OPT_UEVENT			= BIT(0),
40 	FW_OPT_NOWAIT			= BIT(1),
41 	FW_OPT_USERHELPER		= BIT(2),
42 	FW_OPT_NO_WARN			= BIT(3),
43 	FW_OPT_NOCACHE			= BIT(4),
44 	FW_OPT_NOFALLBACK_SYSFS		= BIT(5),
45 	FW_OPT_FALLBACK_PLATFORM	= BIT(6),
46 	FW_OPT_PARTIAL			= BIT(7),
47 };
48 
49 enum fw_status {
50 	FW_STATUS_UNKNOWN,
51 	FW_STATUS_LOADING,
52 	FW_STATUS_DONE,
53 	FW_STATUS_ABORTED,
54 };
55 
56 /*
57  * Concurrent request_firmware() for the same firmware need to be
58  * serialized.  struct fw_state is simple state machine which hold the
59  * state of the firmware loading.
60  */
61 struct fw_state {
62 	struct completion completion;
63 	enum fw_status status;
64 };
65 
66 struct fw_priv {
67 	struct kref ref;
68 	struct list_head list;
69 	struct firmware_cache *fwc;
70 	struct fw_state fw_st;
71 	void *data;
72 	size_t size;
73 	size_t allocated_size;
74 	size_t offset;
75 	u32 opt_flags;
76 #ifdef CONFIG_FW_LOADER_PAGED_BUF
77 	bool is_paged_buf;
78 	struct page **pages;
79 	int nr_pages;
80 	int page_array_size;
81 #endif
82 #ifdef CONFIG_FW_LOADER_USER_HELPER
83 	bool need_uevent;
84 	struct list_head pending_list;
85 #endif
86 	const char *fw_name;
87 };
88 
89 extern struct mutex fw_lock;
90 extern struct firmware_cache fw_cache;
91 
92 static inline bool __fw_state_check(struct fw_priv *fw_priv,
93 				    enum fw_status status)
94 {
95 	struct fw_state *fw_st = &fw_priv->fw_st;
96 
97 	return fw_st->status == status;
98 }
99 
100 static inline int __fw_state_wait_common(struct fw_priv *fw_priv, long timeout)
101 {
102 	struct fw_state *fw_st = &fw_priv->fw_st;
103 	long ret;
104 
105 	ret = wait_for_completion_killable_timeout(&fw_st->completion, timeout);
106 	if (ret != 0 && fw_st->status == FW_STATUS_ABORTED)
107 		return -ENOENT;
108 	if (!ret)
109 		return -ETIMEDOUT;
110 
111 	return ret < 0 ? ret : 0;
112 }
113 
114 static inline void __fw_state_set(struct fw_priv *fw_priv,
115 				  enum fw_status status)
116 {
117 	struct fw_state *fw_st = &fw_priv->fw_st;
118 
119 	WRITE_ONCE(fw_st->status, status);
120 
121 	if (status == FW_STATUS_DONE || status == FW_STATUS_ABORTED) {
122 #ifdef CONFIG_FW_LOADER_USER_HELPER
123 		/*
124 		 * Doing this here ensures that the fw_priv is deleted from
125 		 * the pending list in all abort/done paths.
126 		 */
127 		list_del_init(&fw_priv->pending_list);
128 #endif
129 		complete_all(&fw_st->completion);
130 	}
131 }
132 
133 static inline void fw_state_aborted(struct fw_priv *fw_priv)
134 {
135 	__fw_state_set(fw_priv, FW_STATUS_ABORTED);
136 }
137 
138 static inline bool fw_state_is_aborted(struct fw_priv *fw_priv)
139 {
140 	return __fw_state_check(fw_priv, FW_STATUS_ABORTED);
141 }
142 
143 static inline void fw_state_start(struct fw_priv *fw_priv)
144 {
145 	__fw_state_set(fw_priv, FW_STATUS_LOADING);
146 }
147 
148 static inline void fw_state_done(struct fw_priv *fw_priv)
149 {
150 	__fw_state_set(fw_priv, FW_STATUS_DONE);
151 }
152 
153 static inline bool fw_state_is_done(struct fw_priv *fw_priv)
154 {
155 	return __fw_state_check(fw_priv, FW_STATUS_DONE);
156 }
157 
158 static inline bool fw_state_is_loading(struct fw_priv *fw_priv)
159 {
160 	return __fw_state_check(fw_priv, FW_STATUS_LOADING);
161 }
162 
163 int alloc_lookup_fw_priv(const char *fw_name, struct firmware_cache *fwc,
164 			 struct fw_priv **fw_priv, void *dbuf, size_t size,
165 			 size_t offset, u32 opt_flags);
166 int assign_fw(struct firmware *fw, struct device *device);
167 void free_fw_priv(struct fw_priv *fw_priv);
168 void fw_state_init(struct fw_priv *fw_priv);
169 
170 #ifdef CONFIG_FW_LOADER
171 bool firmware_is_builtin(const struct firmware *fw);
172 bool firmware_request_builtin_buf(struct firmware *fw, const char *name,
173 				  void *buf, size_t size);
174 #else /* module case */
175 static inline bool firmware_is_builtin(const struct firmware *fw)
176 {
177 	return false;
178 }
179 static inline bool firmware_request_builtin_buf(struct firmware *fw,
180 						const char *name,
181 						void *buf, size_t size)
182 {
183 	return false;
184 }
185 #endif
186 
187 #ifdef CONFIG_FW_LOADER_PAGED_BUF
188 void fw_free_paged_buf(struct fw_priv *fw_priv);
189 int fw_grow_paged_buf(struct fw_priv *fw_priv, int pages_needed);
190 int fw_map_paged_buf(struct fw_priv *fw_priv);
191 bool fw_is_paged_buf(struct fw_priv *fw_priv);
192 #else
193 static inline void fw_free_paged_buf(struct fw_priv *fw_priv) {}
194 static inline int fw_grow_paged_buf(struct fw_priv *fw_priv, int pages_needed) { return -ENXIO; }
195 static inline int fw_map_paged_buf(struct fw_priv *fw_priv) { return -ENXIO; }
196 static inline bool fw_is_paged_buf(struct fw_priv *fw_priv) { return false; }
197 #endif
198 
199 #endif /* __FIRMWARE_LOADER_H */
200