firmware.h 5.7 KB

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