Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #ifndef __FIRMWARE_LOADER_H
0003 #define __FIRMWARE_LOADER_H
0004 
0005 #include <linux/bitops.h>
0006 #include <linux/firmware.h>
0007 #include <linux/types.h>
0008 #include <linux/kref.h>
0009 #include <linux/list.h>
0010 #include <linux/completion.h>
0011 
0012 #include <generated/utsrelease.h>
0013 
0014 /**
0015  * enum fw_opt - options to control firmware loading behaviour
0016  *
0017  * @FW_OPT_UEVENT: Enables the fallback mechanism to send a kobject uevent
0018  *  when the firmware is not found. Userspace is in charge to load the
0019  *  firmware using the sysfs loading facility.
0020  * @FW_OPT_NOWAIT: Used to describe the firmware request is asynchronous.
0021  * @FW_OPT_USERHELPER: Enable the fallback mechanism, in case the direct
0022  *  filesystem lookup fails at finding the firmware.  For details refer to
0023  *  firmware_fallback_sysfs().
0024  * @FW_OPT_NO_WARN: Quiet, avoid printing warning messages.
0025  * @FW_OPT_NOCACHE: Disables firmware caching. Firmware caching is used to
0026  *  cache the firmware upon suspend, so that upon resume races against the
0027  *  firmware file lookup on storage is avoided. Used for calls where the
0028  *  file may be too big, or where the driver takes charge of its own
0029  *  firmware caching mechanism.
0030  * @FW_OPT_NOFALLBACK_SYSFS: Disable the sysfs fallback mechanism. Takes
0031  *  precedence over &FW_OPT_UEVENT and &FW_OPT_USERHELPER.
0032  * @FW_OPT_FALLBACK_PLATFORM: Enable fallback to device fw copy embedded in
0033  *  the platform's main firmware. If both this fallback and the sysfs
0034  *      fallback are enabled, then this fallback will be tried first.
0035  * @FW_OPT_PARTIAL: Allow partial read of firmware instead of needing to read
0036  *  entire file.
0037  */
0038 enum fw_opt {
0039     FW_OPT_UEVENT           = BIT(0),
0040     FW_OPT_NOWAIT           = BIT(1),
0041     FW_OPT_USERHELPER       = BIT(2),
0042     FW_OPT_NO_WARN          = BIT(3),
0043     FW_OPT_NOCACHE          = BIT(4),
0044     FW_OPT_NOFALLBACK_SYSFS     = BIT(5),
0045     FW_OPT_FALLBACK_PLATFORM    = BIT(6),
0046     FW_OPT_PARTIAL          = BIT(7),
0047 };
0048 
0049 enum fw_status {
0050     FW_STATUS_UNKNOWN,
0051     FW_STATUS_LOADING,
0052     FW_STATUS_DONE,
0053     FW_STATUS_ABORTED,
0054 };
0055 
0056 /*
0057  * Concurrent request_firmware() for the same firmware need to be
0058  * serialized.  struct fw_state is simple state machine which hold the
0059  * state of the firmware loading.
0060  */
0061 struct fw_state {
0062     struct completion completion;
0063     enum fw_status status;
0064 };
0065 
0066 struct fw_priv {
0067     struct kref ref;
0068     struct list_head list;
0069     struct firmware_cache *fwc;
0070     struct fw_state fw_st;
0071     void *data;
0072     size_t size;
0073     size_t allocated_size;
0074     size_t offset;
0075     u32 opt_flags;
0076 #ifdef CONFIG_FW_LOADER_PAGED_BUF
0077     bool is_paged_buf;
0078     struct page **pages;
0079     int nr_pages;
0080     int page_array_size;
0081 #endif
0082 #ifdef CONFIG_FW_LOADER_USER_HELPER
0083     bool need_uevent;
0084     struct list_head pending_list;
0085 #endif
0086     const char *fw_name;
0087 };
0088 
0089 extern struct mutex fw_lock;
0090 extern struct firmware_cache fw_cache;
0091 
0092 static inline bool __fw_state_check(struct fw_priv *fw_priv,
0093                     enum fw_status status)
0094 {
0095     struct fw_state *fw_st = &fw_priv->fw_st;
0096 
0097     return fw_st->status == status;
0098 }
0099 
0100 static inline int __fw_state_wait_common(struct fw_priv *fw_priv, long timeout)
0101 {
0102     struct fw_state *fw_st = &fw_priv->fw_st;
0103     long ret;
0104 
0105     ret = wait_for_completion_killable_timeout(&fw_st->completion, timeout);
0106     if (ret != 0 && fw_st->status == FW_STATUS_ABORTED)
0107         return -ENOENT;
0108     if (!ret)
0109         return -ETIMEDOUT;
0110 
0111     return ret < 0 ? ret : 0;
0112 }
0113 
0114 static inline void __fw_state_set(struct fw_priv *fw_priv,
0115                   enum fw_status status)
0116 {
0117     struct fw_state *fw_st = &fw_priv->fw_st;
0118 
0119     WRITE_ONCE(fw_st->status, status);
0120 
0121     if (status == FW_STATUS_DONE || status == FW_STATUS_ABORTED) {
0122 #ifdef CONFIG_FW_LOADER_USER_HELPER
0123         /*
0124          * Doing this here ensures that the fw_priv is deleted from
0125          * the pending list in all abort/done paths.
0126          */
0127         list_del_init(&fw_priv->pending_list);
0128 #endif
0129         complete_all(&fw_st->completion);
0130     }
0131 }
0132 
0133 static inline void fw_state_aborted(struct fw_priv *fw_priv)
0134 {
0135     __fw_state_set(fw_priv, FW_STATUS_ABORTED);
0136 }
0137 
0138 static inline bool fw_state_is_aborted(struct fw_priv *fw_priv)
0139 {
0140     return __fw_state_check(fw_priv, FW_STATUS_ABORTED);
0141 }
0142 
0143 static inline void fw_state_start(struct fw_priv *fw_priv)
0144 {
0145     __fw_state_set(fw_priv, FW_STATUS_LOADING);
0146 }
0147 
0148 static inline void fw_state_done(struct fw_priv *fw_priv)
0149 {
0150     __fw_state_set(fw_priv, FW_STATUS_DONE);
0151 }
0152 
0153 static inline bool fw_state_is_done(struct fw_priv *fw_priv)
0154 {
0155     return __fw_state_check(fw_priv, FW_STATUS_DONE);
0156 }
0157 
0158 static inline bool fw_state_is_loading(struct fw_priv *fw_priv)
0159 {
0160     return __fw_state_check(fw_priv, FW_STATUS_LOADING);
0161 }
0162 
0163 int alloc_lookup_fw_priv(const char *fw_name, struct firmware_cache *fwc,
0164              struct fw_priv **fw_priv, void *dbuf, size_t size,
0165              size_t offset, u32 opt_flags);
0166 int assign_fw(struct firmware *fw, struct device *device);
0167 void free_fw_priv(struct fw_priv *fw_priv);
0168 void fw_state_init(struct fw_priv *fw_priv);
0169 
0170 #ifdef CONFIG_FW_LOADER
0171 bool firmware_is_builtin(const struct firmware *fw);
0172 bool firmware_request_builtin_buf(struct firmware *fw, const char *name,
0173                   void *buf, size_t size);
0174 #else /* module case */
0175 static inline bool firmware_is_builtin(const struct firmware *fw)
0176 {
0177     return false;
0178 }
0179 static inline bool firmware_request_builtin_buf(struct firmware *fw,
0180                         const char *name,
0181                         void *buf, size_t size)
0182 {
0183     return false;
0184 }
0185 #endif
0186 
0187 #ifdef CONFIG_FW_LOADER_PAGED_BUF
0188 void fw_free_paged_buf(struct fw_priv *fw_priv);
0189 int fw_grow_paged_buf(struct fw_priv *fw_priv, int pages_needed);
0190 int fw_map_paged_buf(struct fw_priv *fw_priv);
0191 bool fw_is_paged_buf(struct fw_priv *fw_priv);
0192 #else
0193 static inline void fw_free_paged_buf(struct fw_priv *fw_priv) {}
0194 static inline int fw_grow_paged_buf(struct fw_priv *fw_priv, int pages_needed) { return -ENXIO; }
0195 static inline int fw_map_paged_buf(struct fw_priv *fw_priv) { return -ENXIO; }
0196 static inline bool fw_is_paged_buf(struct fw_priv *fw_priv) { return false; }
0197 #endif
0198 
0199 #endif /* __FIRMWARE_LOADER_H */