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  */
36 enum fw_opt {
37 	FW_OPT_UEVENT			= BIT(0),
38 	FW_OPT_NOWAIT			= BIT(1),
39 	FW_OPT_USERHELPER		= BIT(2),
40 	FW_OPT_NO_WARN			= BIT(3),
41 	FW_OPT_NOCACHE			= BIT(4),
42 	FW_OPT_NOFALLBACK_SYSFS		= BIT(5),
43 	FW_OPT_FALLBACK_PLATFORM	= BIT(6),
44 };
45 
46 enum fw_status {
47 	FW_STATUS_UNKNOWN,
48 	FW_STATUS_LOADING,
49 	FW_STATUS_DONE,
50 	FW_STATUS_ABORTED,
51 };
52 
53 /*
54  * Concurrent request_firmware() for the same firmware need to be
55  * serialized.  struct fw_state is simple state machine which hold the
56  * state of the firmware loading.
57  */
58 struct fw_state {
59 	struct completion completion;
60 	enum fw_status status;
61 };
62 
63 struct fw_priv {
64 	struct kref ref;
65 	struct list_head list;
66 	struct firmware_cache *fwc;
67 	struct fw_state fw_st;
68 	void *data;
69 	size_t size;
70 	size_t allocated_size;
71 #ifdef CONFIG_FW_LOADER_PAGED_BUF
72 	bool is_paged_buf;
73 	struct page **pages;
74 	int nr_pages;
75 	int page_array_size;
76 #endif
77 #ifdef CONFIG_FW_LOADER_USER_HELPER
78 	bool need_uevent;
79 	struct list_head pending_list;
80 #endif
81 	const char *fw_name;
82 };
83 
84 extern struct mutex fw_lock;
85 
86 static inline bool __fw_state_check(struct fw_priv *fw_priv,
87 				    enum fw_status status)
88 {
89 	struct fw_state *fw_st = &fw_priv->fw_st;
90 
91 	return fw_st->status == status;
92 }
93 
94 static inline int __fw_state_wait_common(struct fw_priv *fw_priv, long timeout)
95 {
96 	struct fw_state *fw_st = &fw_priv->fw_st;
97 	long ret;
98 
99 	ret = wait_for_completion_killable_timeout(&fw_st->completion, timeout);
100 	if (ret != 0 && fw_st->status == FW_STATUS_ABORTED)
101 		return -ENOENT;
102 	if (!ret)
103 		return -ETIMEDOUT;
104 
105 	return ret < 0 ? ret : 0;
106 }
107 
108 static inline void __fw_state_set(struct fw_priv *fw_priv,
109 				  enum fw_status status)
110 {
111 	struct fw_state *fw_st = &fw_priv->fw_st;
112 
113 	WRITE_ONCE(fw_st->status, status);
114 
115 	if (status == FW_STATUS_DONE || status == FW_STATUS_ABORTED)
116 		complete_all(&fw_st->completion);
117 }
118 
119 static inline void fw_state_aborted(struct fw_priv *fw_priv)
120 {
121 	__fw_state_set(fw_priv, FW_STATUS_ABORTED);
122 }
123 
124 static inline bool fw_state_is_aborted(struct fw_priv *fw_priv)
125 {
126 	return __fw_state_check(fw_priv, FW_STATUS_ABORTED);
127 }
128 
129 static inline void fw_state_start(struct fw_priv *fw_priv)
130 {
131 	__fw_state_set(fw_priv, FW_STATUS_LOADING);
132 }
133 
134 static inline void fw_state_done(struct fw_priv *fw_priv)
135 {
136 	__fw_state_set(fw_priv, FW_STATUS_DONE);
137 }
138 
139 int assign_fw(struct firmware *fw, struct device *device, u32 opt_flags);
140 
141 #ifdef CONFIG_FW_LOADER_PAGED_BUF
142 void fw_free_paged_buf(struct fw_priv *fw_priv);
143 int fw_grow_paged_buf(struct fw_priv *fw_priv, int pages_needed);
144 int fw_map_paged_buf(struct fw_priv *fw_priv);
145 #else
146 static inline void fw_free_paged_buf(struct fw_priv *fw_priv) {}
147 static inline int fw_grow_paged_buf(struct fw_priv *fw_priv, int pages_needed) { return -ENXIO; }
148 static inline int fw_map_paged_buf(struct fw_priv *fw_priv) { return -ENXIO; }
149 #endif
150 
151 #endif /* __FIRMWARE_LOADER_H */
152