firmware.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. #include <generated/utsrelease.h>
  11. /**
  12. * enum fw_opt - options to control firmware loading behaviour
  13. *
  14. * @FW_OPT_UEVENT: Enables the fallback mechanism to send a kobject uevent
  15. * when the firmware is not found. Userspace is in charge to load the
  16. * firmware using the sysfs loading facility.
  17. * @FW_OPT_NOWAIT: Used to describe the firmware request is asynchronous.
  18. * @FW_OPT_USERHELPER: Enable the fallback mechanism, in case the direct
  19. * filesystem lookup fails at finding the firmware. For details refer to
  20. * firmware_fallback_sysfs().
  21. * @FW_OPT_NO_WARN: Quiet, avoid printing warning messages.
  22. * @FW_OPT_NOCACHE: Disables firmware caching. Firmware caching is used to
  23. * cache the firmware upon suspend, so that upon resume races against the
  24. * firmware file lookup on storage is avoided. Used for calls where the
  25. * file may be too big, or where the driver takes charge of its own
  26. * firmware caching mechanism.
  27. * @FW_OPT_NOFALLBACK_SYSFS: Disable the sysfs fallback mechanism. Takes
  28. * precedence over &FW_OPT_UEVENT and &FW_OPT_USERHELPER.
  29. * @FW_OPT_FALLBACK_PLATFORM: Enable fallback to device fw copy embedded in
  30. * the platform's main firmware. If both this fallback and the sysfs
  31. * fallback are enabled, then this fallback will be tried first.
  32. * @FW_OPT_PARTIAL: Allow partial read of firmware instead of needing to read
  33. * entire file.
  34. */
  35. enum fw_opt {
  36. FW_OPT_UEVENT = BIT(0),
  37. FW_OPT_NOWAIT = BIT(1),
  38. FW_OPT_USERHELPER = BIT(2),
  39. FW_OPT_NO_WARN = BIT(3),
  40. FW_OPT_NOCACHE = BIT(4),
  41. FW_OPT_NOFALLBACK_SYSFS = BIT(5),
  42. FW_OPT_FALLBACK_PLATFORM = BIT(6),
  43. FW_OPT_PARTIAL = BIT(7),
  44. };
  45. enum fw_status {
  46. FW_STATUS_UNKNOWN,
  47. FW_STATUS_LOADING,
  48. FW_STATUS_DONE,
  49. FW_STATUS_ABORTED,
  50. };
  51. /*
  52. * Concurrent request_firmware() for the same firmware need to be
  53. * serialized. struct fw_state is simple state machine which hold the
  54. * state of the firmware loading.
  55. */
  56. struct fw_state {
  57. struct completion completion;
  58. enum fw_status status;
  59. };
  60. struct fw_priv {
  61. struct kref ref;
  62. struct list_head list;
  63. struct firmware_cache *fwc;
  64. struct fw_state fw_st;
  65. void *data;
  66. size_t size;
  67. size_t allocated_size;
  68. size_t offset;
  69. u32 opt_flags;
  70. #ifdef CONFIG_FW_LOADER_PAGED_BUF
  71. bool is_paged_buf;
  72. struct page **pages;
  73. int nr_pages;
  74. int page_array_size;
  75. #endif
  76. #ifdef CONFIG_FW_LOADER_USER_HELPER
  77. bool need_uevent;
  78. struct list_head pending_list;
  79. #endif
  80. const char *fw_name;
  81. };
  82. extern struct mutex fw_lock;
  83. extern struct firmware_cache fw_cache;
  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 */