^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * main.c - Multi purpose firmware loading support
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (c) 2003 Manuel Estrada Sainz
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Please see Documentation/driver-api/firmware/ for more information.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/capability.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/kernel_read_file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/timer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/vmalloc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/bitops.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/workqueue.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <linux/highmem.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <linux/firmware.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include <linux/file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include <linux/list.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #include <linux/async.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #include <linux/pm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #include <linux/suspend.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #include <linux/syscore_ops.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #include <linux/reboot.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) #include <linux/security.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) #include <linux/xz.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) #include <generated/utsrelease.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) #include "../base.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) #include "firmware.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) #include "fallback.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) MODULE_AUTHOR("Manuel Estrada Sainz");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) MODULE_DESCRIPTION("Multi purpose firmware loading support");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) struct firmware_cache {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) /* firmware_buf instance will be added into the below list */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) spinlock_t lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) struct list_head head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) int state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) #ifdef CONFIG_FW_CACHE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) * Names of firmware images which have been cached successfully
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) * will be added into the below list so that device uncache
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) * helper can trace which firmware images have been cached
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) * before.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) spinlock_t name_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) struct list_head fw_names;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) struct delayed_work work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) struct notifier_block pm_notify;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) struct fw_cache_entry {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) struct list_head list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) const char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) struct fw_name_devm {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) unsigned long magic;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) const char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) static inline struct fw_priv *to_fw_priv(struct kref *ref)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) return container_of(ref, struct fw_priv, ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) #define FW_LOADER_NO_CACHE 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) #define FW_LOADER_START_CACHE 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) /* fw_lock could be moved to 'struct fw_sysfs' but since it is just
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) * guarding for corner cases a global lock should be OK */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) DEFINE_MUTEX(fw_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) static struct firmware_cache fw_cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) /* Builtin firmware support */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) #ifdef CONFIG_FW_LOADER
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) extern struct builtin_fw __start_builtin_fw[];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) extern struct builtin_fw __end_builtin_fw[];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) static bool fw_copy_to_prealloc_buf(struct firmware *fw,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) void *buf, size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) if (!buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) if (size < fw->size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) memcpy(buf, fw->data, fw->size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) static bool fw_get_builtin_firmware(struct firmware *fw, const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) void *buf, size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) struct builtin_fw *b_fw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) if (strcmp(name, b_fw->name) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) fw->size = b_fw->size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) fw->data = b_fw->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) return fw_copy_to_prealloc_buf(fw, buf, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) static bool fw_is_builtin_firmware(const struct firmware *fw)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) struct builtin_fw *b_fw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) if (fw->data == b_fw->data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) #else /* Module case - no builtin firmware support */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) static inline bool fw_get_builtin_firmware(struct firmware *fw,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) const char *name, void *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) static inline bool fw_is_builtin_firmware(const struct firmware *fw)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) static void fw_state_init(struct fw_priv *fw_priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) struct fw_state *fw_st = &fw_priv->fw_st;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) init_completion(&fw_st->completion);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) fw_st->status = FW_STATUS_UNKNOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) static inline int fw_state_wait(struct fw_priv *fw_priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) return __fw_state_wait_common(fw_priv, MAX_SCHEDULE_TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) static void fw_cache_piggyback_on_request(struct fw_priv *fw_priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) static struct fw_priv *__allocate_fw_priv(const char *fw_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) struct firmware_cache *fwc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) void *dbuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) size_t size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) size_t offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) u32 opt_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) struct fw_priv *fw_priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) /* For a partial read, the buffer must be preallocated. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) if ((opt_flags & FW_OPT_PARTIAL) && !dbuf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) /* Only partial reads are allowed to use an offset. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) if (offset != 0 && !(opt_flags & FW_OPT_PARTIAL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) fw_priv = kzalloc(sizeof(*fw_priv), GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) if (!fw_priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) fw_priv->fw_name = kstrdup_const(fw_name, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) if (!fw_priv->fw_name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) kfree(fw_priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) kref_init(&fw_priv->ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) fw_priv->fwc = fwc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) fw_priv->data = dbuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) fw_priv->allocated_size = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) fw_priv->offset = offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) fw_priv->opt_flags = opt_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) fw_state_init(fw_priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) #ifdef CONFIG_FW_LOADER_USER_HELPER
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) INIT_LIST_HEAD(&fw_priv->pending_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) pr_debug("%s: fw-%s fw_priv=%p\n", __func__, fw_name, fw_priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) return fw_priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) static struct fw_priv *__lookup_fw_priv(const char *fw_name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) struct fw_priv *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) struct firmware_cache *fwc = &fw_cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) list_for_each_entry(tmp, &fwc->head, list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) if (!strcmp(tmp->fw_name, fw_name))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) return tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) /* Returns 1 for batching firmware requests with the same name */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) static int alloc_lookup_fw_priv(const char *fw_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) struct firmware_cache *fwc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) struct fw_priv **fw_priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) void *dbuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) size_t size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) size_t offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) u32 opt_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) struct fw_priv *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) spin_lock(&fwc->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) * Do not merge requests that are marked to be non-cached or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) * are performing partial reads.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) if (!(opt_flags & (FW_OPT_NOCACHE | FW_OPT_PARTIAL))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) tmp = __lookup_fw_priv(fw_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) if (tmp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) kref_get(&tmp->ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) spin_unlock(&fwc->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) *fw_priv = tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) pr_debug("batched request - sharing the same struct fw_priv and lookup for multiple requests\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) tmp = __allocate_fw_priv(fw_name, fwc, dbuf, size, offset, opt_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) if (tmp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) INIT_LIST_HEAD(&tmp->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) if (!(opt_flags & FW_OPT_NOCACHE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) list_add(&tmp->list, &fwc->head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) spin_unlock(&fwc->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) *fw_priv = tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) return tmp ? 0 : -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) static void __free_fw_priv(struct kref *ref)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) __releases(&fwc->lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) struct fw_priv *fw_priv = to_fw_priv(ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) struct firmware_cache *fwc = fw_priv->fwc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) pr_debug("%s: fw-%s fw_priv=%p data=%p size=%u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) __func__, fw_priv->fw_name, fw_priv, fw_priv->data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) (unsigned int)fw_priv->size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) list_del(&fw_priv->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) spin_unlock(&fwc->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) if (fw_is_paged_buf(fw_priv))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) fw_free_paged_buf(fw_priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) else if (!fw_priv->allocated_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) vfree(fw_priv->data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) kfree_const(fw_priv->fw_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) kfree(fw_priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) static void free_fw_priv(struct fw_priv *fw_priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) struct firmware_cache *fwc = fw_priv->fwc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) spin_lock(&fwc->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) if (!kref_put(&fw_priv->ref, __free_fw_priv))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) spin_unlock(&fwc->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) #ifdef CONFIG_FW_LOADER_PAGED_BUF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) bool fw_is_paged_buf(struct fw_priv *fw_priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) return fw_priv->is_paged_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) void fw_free_paged_buf(struct fw_priv *fw_priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) if (!fw_priv->pages)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) vunmap(fw_priv->data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) for (i = 0; i < fw_priv->nr_pages; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) __free_page(fw_priv->pages[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) kvfree(fw_priv->pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) fw_priv->pages = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) fw_priv->page_array_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) fw_priv->nr_pages = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) int fw_grow_paged_buf(struct fw_priv *fw_priv, int pages_needed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) /* If the array of pages is too small, grow it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) if (fw_priv->page_array_size < pages_needed) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) int new_array_size = max(pages_needed,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) fw_priv->page_array_size * 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) struct page **new_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) new_pages = kvmalloc_array(new_array_size, sizeof(void *),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) if (!new_pages)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) memcpy(new_pages, fw_priv->pages,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) fw_priv->page_array_size * sizeof(void *));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) memset(&new_pages[fw_priv->page_array_size], 0, sizeof(void *) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) (new_array_size - fw_priv->page_array_size));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) kvfree(fw_priv->pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) fw_priv->pages = new_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) fw_priv->page_array_size = new_array_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) while (fw_priv->nr_pages < pages_needed) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) fw_priv->pages[fw_priv->nr_pages] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) if (!fw_priv->pages[fw_priv->nr_pages])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) fw_priv->nr_pages++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) int fw_map_paged_buf(struct fw_priv *fw_priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) /* one pages buffer should be mapped/unmapped only once */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) if (!fw_priv->pages)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) vunmap(fw_priv->data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) fw_priv->data = vmap(fw_priv->pages, fw_priv->nr_pages, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) PAGE_KERNEL_RO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) if (!fw_priv->data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) * XZ-compressed firmware support
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) #ifdef CONFIG_FW_LOADER_COMPRESS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) /* show an error and return the standard error code */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) static int fw_decompress_xz_error(struct device *dev, enum xz_ret xz_ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) if (xz_ret != XZ_STREAM_END) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) dev_warn(dev, "xz decompression failed (xz_ret=%d)\n", xz_ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) return xz_ret == XZ_MEM_ERROR ? -ENOMEM : -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) /* single-shot decompression onto the pre-allocated buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) static int fw_decompress_xz_single(struct device *dev, struct fw_priv *fw_priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) size_t in_size, const void *in_buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) struct xz_dec *xz_dec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) struct xz_buf xz_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) enum xz_ret xz_ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) xz_dec = xz_dec_init(XZ_SINGLE, (u32)-1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) if (!xz_dec)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) xz_buf.in_size = in_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) xz_buf.in = in_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) xz_buf.in_pos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) xz_buf.out_size = fw_priv->allocated_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) xz_buf.out = fw_priv->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) xz_buf.out_pos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) xz_ret = xz_dec_run(xz_dec, &xz_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) xz_dec_end(xz_dec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) fw_priv->size = xz_buf.out_pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) return fw_decompress_xz_error(dev, xz_ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) /* decompression on paged buffer and map it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) static int fw_decompress_xz_pages(struct device *dev, struct fw_priv *fw_priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) size_t in_size, const void *in_buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) struct xz_dec *xz_dec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) struct xz_buf xz_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) enum xz_ret xz_ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) xz_dec = xz_dec_init(XZ_DYNALLOC, (u32)-1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) if (!xz_dec)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) xz_buf.in_size = in_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) xz_buf.in = in_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) xz_buf.in_pos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) fw_priv->is_paged_buf = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) fw_priv->size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) if (fw_grow_paged_buf(fw_priv, fw_priv->nr_pages + 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) /* decompress onto the new allocated page */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) page = fw_priv->pages[fw_priv->nr_pages - 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) xz_buf.out = kmap(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) xz_buf.out_pos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) xz_buf.out_size = PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) xz_ret = xz_dec_run(xz_dec, &xz_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) kunmap(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) fw_priv->size += xz_buf.out_pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) /* partial decompression means either end or error */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) if (xz_buf.out_pos != PAGE_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) } while (xz_ret == XZ_OK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) err = fw_decompress_xz_error(dev, xz_ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) err = fw_map_paged_buf(fw_priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) xz_dec_end(xz_dec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) static int fw_decompress_xz(struct device *dev, struct fw_priv *fw_priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) size_t in_size, const void *in_buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) /* if the buffer is pre-allocated, we can perform in single-shot mode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) if (fw_priv->data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) return fw_decompress_xz_single(dev, fw_priv, in_size, in_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) return fw_decompress_xz_pages(dev, fw_priv, in_size, in_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) #endif /* CONFIG_FW_LOADER_COMPRESS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) /* direct firmware loading support */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) static char fw_path_para[256];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) static const char * const fw_path[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) fw_path_para,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) "/lib/firmware/updates/" UTS_RELEASE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) "/lib/firmware/updates",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) "/lib/firmware/" UTS_RELEASE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) "/lib/firmware"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) * Typical usage is that passing 'firmware_class.path=$CUSTOMIZED_PATH'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) * from kernel command line because firmware_class is generally built in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) * kernel instead of module.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) module_param_string(path, fw_path_para, sizeof(fw_path_para), 0644);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) MODULE_PARM_DESC(path, "customized firmware image search path with a higher priority than default path");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) fw_get_filesystem_firmware(struct device *device, struct fw_priv *fw_priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) const char *suffix,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) int (*decompress)(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) struct fw_priv *fw_priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) size_t in_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) const void *in_buffer))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) size_t size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) int i, len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) int rc = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) char *path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) size_t msize = INT_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) void *buffer = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) /* Already populated data member means we're loading into a buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) if (!decompress && fw_priv->data) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) buffer = fw_priv->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) msize = fw_priv->allocated_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) path = __getname();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) if (!path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) for (i = 0; i < ARRAY_SIZE(fw_path); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) size_t file_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) size_t *file_size_ptr = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) /* skip the unset customized path */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) if (!fw_path[i][0])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) len = snprintf(path, PATH_MAX, "%s/%s%s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) fw_path[i], fw_priv->fw_name, suffix);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) if (len >= PATH_MAX) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) rc = -ENAMETOOLONG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) fw_priv->size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) * The total file size is only examined when doing a partial
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) * read; the "full read" case needs to fail if the whole
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) * firmware was not completely loaded.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) if ((fw_priv->opt_flags & FW_OPT_PARTIAL) && buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) file_size_ptr = &file_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) /* load firmware files from the mount namespace of init */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) rc = kernel_read_file_from_path_initns(path, fw_priv->offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) &buffer, msize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) file_size_ptr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) READING_FIRMWARE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) if (rc < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) if (rc != -ENOENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) dev_warn(device, "loading %s failed with error %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) path, rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) dev_dbg(device, "loading %s failed for no such file or directory.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) size = rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) dev_dbg(device, "Loading firmware from %s\n", path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) if (decompress) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) dev_dbg(device, "f/w decompressing %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) fw_priv->fw_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) rc = decompress(device, fw_priv, size, buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) /* discard the superfluous original content */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) vfree(buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) buffer = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) fw_free_paged_buf(fw_priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) dev_dbg(device, "direct-loading %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) fw_priv->fw_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) if (!fw_priv->data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) fw_priv->data = buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) fw_priv->size = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) fw_state_done(fw_priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) __putname(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) /* firmware holds the ownership of pages */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) static void firmware_free_data(const struct firmware *fw)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) /* Loaded directly? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) if (!fw->priv) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) vfree(fw->data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) free_fw_priv(fw->priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) /* store the pages buffer info firmware from buf */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) static void fw_set_page_data(struct fw_priv *fw_priv, struct firmware *fw)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) fw->priv = fw_priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) fw->size = fw_priv->size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) fw->data = fw_priv->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) pr_debug("%s: fw-%s fw_priv=%p data=%p size=%u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) __func__, fw_priv->fw_name, fw_priv, fw_priv->data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) (unsigned int)fw_priv->size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) #ifdef CONFIG_FW_CACHE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) static void fw_name_devm_release(struct device *dev, void *res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) struct fw_name_devm *fwn = res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) if (fwn->magic == (unsigned long)&fw_cache)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) pr_debug("%s: fw_name-%s devm-%p released\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) __func__, fwn->name, res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) kfree_const(fwn->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) static int fw_devm_match(struct device *dev, void *res,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) void *match_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) struct fw_name_devm *fwn = res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) return (fwn->magic == (unsigned long)&fw_cache) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) !strcmp(fwn->name, match_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) static struct fw_name_devm *fw_find_devm_name(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) struct fw_name_devm *fwn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) fwn = devres_find(dev, fw_name_devm_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) fw_devm_match, (void *)name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) return fwn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) static bool fw_cache_is_setup(struct device *dev, const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) struct fw_name_devm *fwn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) fwn = fw_find_devm_name(dev, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) if (fwn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) /* add firmware name into devres list */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) static int fw_add_devm_name(struct device *dev, const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) struct fw_name_devm *fwn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) if (fw_cache_is_setup(dev, name))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) fwn = devres_alloc(fw_name_devm_release, sizeof(struct fw_name_devm),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) if (!fwn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) fwn->name = kstrdup_const(name, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) if (!fwn->name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) devres_free(fwn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) fwn->magic = (unsigned long)&fw_cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) devres_add(dev, fwn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) static bool fw_cache_is_setup(struct device *dev, const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) static int fw_add_devm_name(struct device *dev, const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) int assign_fw(struct firmware *fw, struct device *device)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) struct fw_priv *fw_priv = fw->priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) mutex_lock(&fw_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) if (!fw_priv->size || fw_state_is_aborted(fw_priv)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) mutex_unlock(&fw_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) * add firmware name into devres list so that we can auto cache
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) * and uncache firmware for device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) * device may has been deleted already, but the problem
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) * should be fixed in devres or driver core.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) /* don't cache firmware handled without uevent */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) if (device && (fw_priv->opt_flags & FW_OPT_UEVENT) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) !(fw_priv->opt_flags & FW_OPT_NOCACHE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) ret = fw_add_devm_name(device, fw_priv->fw_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) mutex_unlock(&fw_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) * After caching firmware image is started, let it piggyback
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) * on request firmware.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) if (!(fw_priv->opt_flags & FW_OPT_NOCACHE) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) fw_priv->fwc->state == FW_LOADER_START_CACHE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) fw_cache_piggyback_on_request(fw_priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) /* pass the pages buffer to driver at the last minute */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) fw_set_page_data(fw_priv, fw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) mutex_unlock(&fw_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) /* prepare firmware and firmware_buf structs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) * return 0 if a firmware is already assigned, 1 if need to load one,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) * or a negative error code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) _request_firmware_prepare(struct firmware **firmware_p, const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) struct device *device, void *dbuf, size_t size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) size_t offset, u32 opt_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) struct firmware *firmware;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) struct fw_priv *fw_priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) if (!firmware) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) dev_err(device, "%s: kmalloc(struct firmware) failed\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) if (fw_get_builtin_firmware(firmware, name, dbuf, size)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) dev_dbg(device, "using built-in %s\n", name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) return 0; /* assigned */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) ret = alloc_lookup_fw_priv(name, &fw_cache, &fw_priv, dbuf, size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) offset, opt_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) * bind with 'priv' now to avoid warning in failure path
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) * of requesting firmware.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) firmware->priv = fw_priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) if (ret > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) ret = fw_state_wait(fw_priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) if (!ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) fw_set_page_data(fw_priv, firmware);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) return 0; /* assigned */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) return 1; /* need to load */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) * Batched requests need only one wake, we need to do this step last due to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) * fallback mechanism. The buf is protected with kref_get(), and it won't be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) * released until the last user calls release_firmware().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) * Failed batched requests are possible as well, in such cases we just share
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) * the struct fw_priv and won't release it until all requests are woken
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) * and have gone through this same path.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) static void fw_abort_batch_reqs(struct firmware *fw)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) struct fw_priv *fw_priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) /* Loaded directly? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) if (!fw || !fw->priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) fw_priv = fw->priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) mutex_lock(&fw_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) if (!fw_state_is_aborted(fw_priv))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) fw_state_aborted(fw_priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) mutex_unlock(&fw_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) /* called from request_firmware() and request_firmware_work_func() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) _request_firmware(const struct firmware **firmware_p, const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) struct device *device, void *buf, size_t size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) size_t offset, u32 opt_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) struct firmware *fw = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) bool nondirect = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) if (!firmware_p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) if (!name || name[0] == '\0') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) ret = _request_firmware_prepare(&fw, name, device, buf, size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) offset, opt_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) if (ret <= 0) /* error or already assigned */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) ret = fw_get_filesystem_firmware(device, fw->priv, "", NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) /* Only full reads can support decompression, platform, and sysfs. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) if (!(opt_flags & FW_OPT_PARTIAL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) nondirect = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) #ifdef CONFIG_FW_LOADER_COMPRESS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) if (ret == -ENOENT && nondirect)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) ret = fw_get_filesystem_firmware(device, fw->priv, ".xz",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) fw_decompress_xz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) if (ret == -ENOENT && nondirect)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) ret = firmware_fallback_platform(fw->priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) if (!(opt_flags & FW_OPT_NO_WARN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) dev_warn(device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) "Direct firmware load for %s failed with error %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) name, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) if (nondirect)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) ret = firmware_fallback_sysfs(fw, name, device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) opt_flags, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) ret = assign_fw(fw, device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) fw_abort_batch_reqs(fw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) release_firmware(fw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) fw = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) *firmware_p = fw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) * request_firmware() - send firmware request and wait for it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) * @firmware_p: pointer to firmware image
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) * @name: name of firmware file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) * @device: device for which firmware is being loaded
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) * @firmware_p will be used to return a firmware image by the name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) * of @name for device @device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) * Should be called from user context where sleeping is allowed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) * @name will be used as $FIRMWARE in the uevent environment and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) * should be distinctive enough not to be confused with any other
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) * firmware image for this or any other device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) * Caller must hold the reference count of @device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) * The function can be called safely inside device's suspend and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) * resume callback.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) **/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) request_firmware(const struct firmware **firmware_p, const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) struct device *device)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) /* Need to pin this module until return */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) __module_get(THIS_MODULE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) ret = _request_firmware(firmware_p, name, device, NULL, 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) FW_OPT_UEVENT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) module_put(THIS_MODULE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) EXPORT_SYMBOL(request_firmware);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) * firmware_request_nowarn() - request for an optional fw module
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) * @firmware: pointer to firmware image
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) * @name: name of firmware file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) * @device: device for which firmware is being loaded
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) * This function is similar in behaviour to request_firmware(), except it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) * doesn't produce warning messages when the file is not found. The sysfs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) * fallback mechanism is enabled if direct filesystem lookup fails. However,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) * failures to find the firmware file with it are still suppressed. It is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) * therefore up to the driver to check for the return value of this call and to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) * decide when to inform the users of errors.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) **/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) int firmware_request_nowarn(const struct firmware **firmware, const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) struct device *device)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) /* Need to pin this module until return */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) __module_get(THIS_MODULE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) ret = _request_firmware(firmware, name, device, NULL, 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) FW_OPT_UEVENT | FW_OPT_NO_WARN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) module_put(THIS_MODULE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) EXPORT_SYMBOL_GPL(firmware_request_nowarn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) * request_firmware_direct() - load firmware directly without usermode helper
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) * @firmware_p: pointer to firmware image
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) * @name: name of firmware file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) * @device: device for which firmware is being loaded
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) * This function works pretty much like request_firmware(), but this doesn't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) * fall back to usermode helper even if the firmware couldn't be loaded
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) * directly from fs. Hence it's useful for loading optional firmwares, which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) * aren't always present, without extra long timeouts of udev.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) **/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) int request_firmware_direct(const struct firmware **firmware_p,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) const char *name, struct device *device)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) __module_get(THIS_MODULE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) ret = _request_firmware(firmware_p, name, device, NULL, 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) FW_OPT_UEVENT | FW_OPT_NO_WARN |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) FW_OPT_NOFALLBACK_SYSFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) module_put(THIS_MODULE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) EXPORT_SYMBOL_GPL(request_firmware_direct);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) * firmware_request_platform() - request firmware with platform-fw fallback
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) * @firmware: pointer to firmware image
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) * @name: name of firmware file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) * @device: device for which firmware is being loaded
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) * This function is similar in behaviour to request_firmware, except that if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) * direct filesystem lookup fails, it will fallback to looking for a copy of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) * requested firmware embedded in the platform's main (e.g. UEFI) firmware.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) **/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) int firmware_request_platform(const struct firmware **firmware,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) const char *name, struct device *device)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) /* Need to pin this module until return */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) __module_get(THIS_MODULE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) ret = _request_firmware(firmware, name, device, NULL, 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) FW_OPT_UEVENT | FW_OPT_FALLBACK_PLATFORM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) module_put(THIS_MODULE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) EXPORT_SYMBOL_GPL(firmware_request_platform);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960) * firmware_request_cache() - cache firmware for suspend so resume can use it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) * @name: name of firmware file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962) * @device: device for which firmware should be cached for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) * There are some devices with an optimization that enables the device to not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965) * require loading firmware on system reboot. This optimization may still
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966) * require the firmware present on resume from suspend. This routine can be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967) * used to ensure the firmware is present on resume from suspend in these
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968) * situations. This helper is not compatible with drivers which use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969) * request_firmware_into_buf() or request_firmware_nowait() with no uevent set.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970) **/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971) int firmware_request_cache(struct device *device, const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975) mutex_lock(&fw_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976) ret = fw_add_devm_name(device, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977) mutex_unlock(&fw_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981) EXPORT_SYMBOL_GPL(firmware_request_cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984) * request_firmware_into_buf() - load firmware into a previously allocated buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985) * @firmware_p: pointer to firmware image
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986) * @name: name of firmware file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987) * @device: device for which firmware is being loaded and DMA region allocated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988) * @buf: address of buffer to load firmware into
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989) * @size: size of buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991) * This function works pretty much like request_firmware(), but it doesn't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992) * allocate a buffer to hold the firmware data. Instead, the firmware
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993) * is loaded directly into the buffer pointed to by @buf and the @firmware_p
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994) * data member is pointed at @buf.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996) * This function doesn't cache firmware either.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999) request_firmware_into_buf(const struct firmware **firmware_p, const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) struct device *device, void *buf, size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) if (fw_cache_is_setup(device, name))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) __module_get(THIS_MODULE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) ret = _request_firmware(firmware_p, name, device, buf, size, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) FW_OPT_UEVENT | FW_OPT_NOCACHE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) module_put(THIS_MODULE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) EXPORT_SYMBOL(request_firmware_into_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) * request_partial_firmware_into_buf() - load partial firmware into a previously allocated buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) * @firmware_p: pointer to firmware image
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) * @name: name of firmware file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) * @device: device for which firmware is being loaded and DMA region allocated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) * @buf: address of buffer to load firmware into
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) * @size: size of buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) * @offset: offset into file to read
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) * This function works pretty much like request_firmware_into_buf except
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) * it allows a partial read of the file.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) request_partial_firmware_into_buf(const struct firmware **firmware_p,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) const char *name, struct device *device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) void *buf, size_t size, size_t offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) if (fw_cache_is_setup(device, name))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) __module_get(THIS_MODULE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) ret = _request_firmware(firmware_p, name, device, buf, size, offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) FW_OPT_UEVENT | FW_OPT_NOCACHE |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) FW_OPT_PARTIAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) module_put(THIS_MODULE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) EXPORT_SYMBOL(request_partial_firmware_into_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) * release_firmware() - release the resource associated with a firmware image
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) * @fw: firmware resource to release
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) **/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) void release_firmware(const struct firmware *fw)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) if (fw) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) if (!fw_is_builtin_firmware(fw))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) firmware_free_data(fw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) kfree(fw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) EXPORT_SYMBOL(release_firmware);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) /* Async support */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) struct firmware_work {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) struct work_struct work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) struct module *module;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) const char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) struct device *device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) void *context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) void (*cont)(const struct firmware *fw, void *context);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) u32 opt_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) static void request_firmware_work_func(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) struct firmware_work *fw_work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) const struct firmware *fw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) fw_work = container_of(work, struct firmware_work, work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) _request_firmware(&fw, fw_work->name, fw_work->device, NULL, 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) fw_work->opt_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) fw_work->cont(fw, fw_work->context);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) put_device(fw_work->device); /* taken in request_firmware_nowait() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) module_put(fw_work->module);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) kfree_const(fw_work->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) kfree(fw_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) * request_firmware_nowait() - asynchronous version of request_firmware
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) * @module: module requesting the firmware
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) * @uevent: sends uevent to copy the firmware image if this flag
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) * is non-zero else the firmware copy must be done manually.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) * @name: name of firmware file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) * @device: device for which firmware is being loaded
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) * @gfp: allocation flags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) * @context: will be passed over to @cont, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) * @fw may be %NULL if firmware request fails.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) * @cont: function will be called asynchronously when the firmware
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) * request is over.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) * Caller must hold the reference count of @device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) * Asynchronous variant of request_firmware() for user contexts:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) * - sleep for as small periods as possible since it may
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) * increase kernel boot time of built-in device drivers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) * requesting firmware in their ->probe() methods, if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) * @gfp is GFP_KERNEL.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) * - can't sleep at all if @gfp is GFP_ATOMIC.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) **/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) request_firmware_nowait(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) struct module *module, bool uevent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) const char *name, struct device *device, gfp_t gfp, void *context,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) void (*cont)(const struct firmware *fw, void *context))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) struct firmware_work *fw_work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) fw_work = kzalloc(sizeof(struct firmware_work), gfp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) if (!fw_work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) fw_work->module = module;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) fw_work->name = kstrdup_const(name, gfp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) if (!fw_work->name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) kfree(fw_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) fw_work->device = device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) fw_work->context = context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) fw_work->cont = cont;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) fw_work->opt_flags = FW_OPT_NOWAIT |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) (uevent ? FW_OPT_UEVENT : FW_OPT_USERHELPER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) if (!uevent && fw_cache_is_setup(device, name)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) kfree_const(fw_work->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) kfree(fw_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) if (!try_module_get(module)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) kfree_const(fw_work->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) kfree(fw_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) get_device(fw_work->device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) INIT_WORK(&fw_work->work, request_firmware_work_func);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) schedule_work(&fw_work->work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) EXPORT_SYMBOL(request_firmware_nowait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) #ifdef CONFIG_FW_CACHE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) static ASYNC_DOMAIN_EXCLUSIVE(fw_cache_domain);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) * cache_firmware() - cache one firmware image in kernel memory space
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) * @fw_name: the firmware image name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) * Cache firmware in kernel memory so that drivers can use it when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) * system isn't ready for them to request firmware image from userspace.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) * Once it returns successfully, driver can use request_firmware or its
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) * nowait version to get the cached firmware without any interacting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) * with userspace
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) * Return 0 if the firmware image has been cached successfully
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) * Return !0 otherwise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) static int cache_firmware(const char *fw_name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) const struct firmware *fw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) pr_debug("%s: %s\n", __func__, fw_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) ret = request_firmware(&fw, fw_name, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) kfree(fw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) pr_debug("%s: %s ret=%d\n", __func__, fw_name, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) static struct fw_priv *lookup_fw_priv(const char *fw_name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) struct fw_priv *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) struct firmware_cache *fwc = &fw_cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) spin_lock(&fwc->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) tmp = __lookup_fw_priv(fw_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) spin_unlock(&fwc->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) return tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) * uncache_firmware() - remove one cached firmware image
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) * @fw_name: the firmware image name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) * Uncache one firmware image which has been cached successfully
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) * before.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) * Return 0 if the firmware cache has been removed successfully
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) * Return !0 otherwise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) static int uncache_firmware(const char *fw_name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) struct fw_priv *fw_priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) struct firmware fw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) pr_debug("%s: %s\n", __func__, fw_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) if (fw_get_builtin_firmware(&fw, fw_name, NULL, 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) fw_priv = lookup_fw_priv(fw_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) if (fw_priv) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) free_fw_priv(fw_priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) static struct fw_cache_entry *alloc_fw_cache_entry(const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) struct fw_cache_entry *fce;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) fce = kzalloc(sizeof(*fce), GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) if (!fce)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) fce->name = kstrdup_const(name, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) if (!fce->name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) kfree(fce);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) fce = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) exit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) return fce;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) static int __fw_entry_found(const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) struct firmware_cache *fwc = &fw_cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) struct fw_cache_entry *fce;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) list_for_each_entry(fce, &fwc->fw_names, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) if (!strcmp(fce->name, name))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) static void fw_cache_piggyback_on_request(struct fw_priv *fw_priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) const char *name = fw_priv->fw_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) struct firmware_cache *fwc = fw_priv->fwc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) struct fw_cache_entry *fce;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) spin_lock(&fwc->name_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) if (__fw_entry_found(name))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) goto found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) fce = alloc_fw_cache_entry(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) if (fce) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) list_add(&fce->list, &fwc->fw_names);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) kref_get(&fw_priv->ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) pr_debug("%s: fw: %s\n", __func__, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) found:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) spin_unlock(&fwc->name_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) static void free_fw_cache_entry(struct fw_cache_entry *fce)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) kfree_const(fce->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) kfree(fce);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) static void __async_dev_cache_fw_image(void *fw_entry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) async_cookie_t cookie)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) struct fw_cache_entry *fce = fw_entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) struct firmware_cache *fwc = &fw_cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) ret = cache_firmware(fce->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) spin_lock(&fwc->name_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) list_del(&fce->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) spin_unlock(&fwc->name_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) free_fw_cache_entry(fce);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) /* called with dev->devres_lock held */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) static void dev_create_fw_entry(struct device *dev, void *res,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) struct fw_name_devm *fwn = res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) const char *fw_name = fwn->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) struct list_head *head = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) struct fw_cache_entry *fce;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) fce = alloc_fw_cache_entry(fw_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) if (fce)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) list_add(&fce->list, head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) static int devm_name_match(struct device *dev, void *res,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) void *match_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) struct fw_name_devm *fwn = res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) return (fwn->magic == (unsigned long)match_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) static void dev_cache_fw_image(struct device *dev, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) LIST_HEAD(todo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) struct fw_cache_entry *fce;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) struct fw_cache_entry *fce_next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) struct firmware_cache *fwc = &fw_cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) devres_for_each_res(dev, fw_name_devm_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) devm_name_match, &fw_cache,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) dev_create_fw_entry, &todo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) list_for_each_entry_safe(fce, fce_next, &todo, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) list_del(&fce->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) spin_lock(&fwc->name_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) /* only one cache entry for one firmware */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) if (!__fw_entry_found(fce->name)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) list_add(&fce->list, &fwc->fw_names);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) free_fw_cache_entry(fce);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) fce = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) spin_unlock(&fwc->name_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) if (fce)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) async_schedule_domain(__async_dev_cache_fw_image,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) (void *)fce,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) &fw_cache_domain);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) static void __device_uncache_fw_images(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) struct firmware_cache *fwc = &fw_cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) struct fw_cache_entry *fce;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) spin_lock(&fwc->name_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) while (!list_empty(&fwc->fw_names)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) fce = list_entry(fwc->fw_names.next,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) struct fw_cache_entry, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) list_del(&fce->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) spin_unlock(&fwc->name_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) uncache_firmware(fce->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) free_fw_cache_entry(fce);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) spin_lock(&fwc->name_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) spin_unlock(&fwc->name_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) * device_cache_fw_images() - cache devices' firmware
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) * If one device called request_firmware or its nowait version
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) * successfully before, the firmware names are recored into the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) * device's devres link list, so device_cache_fw_images can call
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) * cache_firmware() to cache these firmwares for the device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) * then the device driver can load its firmwares easily at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) * time when system is not ready to complete loading firmware.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) static void device_cache_fw_images(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) struct firmware_cache *fwc = &fw_cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) DEFINE_WAIT(wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) pr_debug("%s\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) /* cancel uncache work */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) cancel_delayed_work_sync(&fwc->work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) fw_fallback_set_cache_timeout();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) mutex_lock(&fw_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) fwc->state = FW_LOADER_START_CACHE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) dpm_for_each_dev(NULL, dev_cache_fw_image);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) mutex_unlock(&fw_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) /* wait for completion of caching firmware for all devices */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) async_synchronize_full_domain(&fw_cache_domain);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) fw_fallback_set_default_timeout();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) * device_uncache_fw_images() - uncache devices' firmware
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) * uncache all firmwares which have been cached successfully
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) * by device_uncache_fw_images earlier
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) static void device_uncache_fw_images(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) pr_debug("%s\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) __device_uncache_fw_images();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) static void device_uncache_fw_images_work(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) device_uncache_fw_images();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) * device_uncache_fw_images_delay() - uncache devices firmwares
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) * @delay: number of milliseconds to delay uncache device firmwares
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) * uncache all devices's firmwares which has been cached successfully
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) * by device_cache_fw_images after @delay milliseconds.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) static void device_uncache_fw_images_delay(unsigned long delay)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) queue_delayed_work(system_power_efficient_wq, &fw_cache.work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) msecs_to_jiffies(delay));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) static int fw_pm_notify(struct notifier_block *notify_block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) unsigned long mode, void *unused)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) switch (mode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) case PM_HIBERNATION_PREPARE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) case PM_SUSPEND_PREPARE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) case PM_RESTORE_PREPARE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) * kill pending fallback requests with a custom fallback
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) * to avoid stalling suspend.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) kill_pending_fw_fallback_reqs(true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) device_cache_fw_images();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) case PM_POST_SUSPEND:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) case PM_POST_HIBERNATION:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) case PM_POST_RESTORE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) * In case that system sleep failed and syscore_suspend is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) * not called.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) mutex_lock(&fw_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) fw_cache.state = FW_LOADER_NO_CACHE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) mutex_unlock(&fw_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) device_uncache_fw_images_delay(10 * MSEC_PER_SEC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) /* stop caching firmware once syscore_suspend is reached */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) static int fw_suspend(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) fw_cache.state = FW_LOADER_NO_CACHE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) static struct syscore_ops fw_syscore_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) .suspend = fw_suspend,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) static int __init register_fw_pm_ops(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) spin_lock_init(&fw_cache.name_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) INIT_LIST_HEAD(&fw_cache.fw_names);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) INIT_DELAYED_WORK(&fw_cache.work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) device_uncache_fw_images_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) fw_cache.pm_notify.notifier_call = fw_pm_notify;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) ret = register_pm_notifier(&fw_cache.pm_notify);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) register_syscore_ops(&fw_syscore_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) static inline void unregister_fw_pm_ops(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) unregister_syscore_ops(&fw_syscore_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) unregister_pm_notifier(&fw_cache.pm_notify);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) static void fw_cache_piggyback_on_request(struct fw_priv *fw_priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) static inline int register_fw_pm_ops(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) static inline void unregister_fw_pm_ops(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) static void __init fw_cache_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) spin_lock_init(&fw_cache.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) INIT_LIST_HEAD(&fw_cache.head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) fw_cache.state = FW_LOADER_NO_CACHE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) static int fw_shutdown_notify(struct notifier_block *unused1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) unsigned long unused2, void *unused3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) * Kill all pending fallback requests to avoid both stalling shutdown,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) * and avoid a deadlock with the usermode_lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) kill_pending_fw_fallback_reqs(false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) return NOTIFY_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) static struct notifier_block fw_shutdown_nb = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) .notifier_call = fw_shutdown_notify,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) static int __init firmware_class_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) /* No need to unfold these on exit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) fw_cache_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) ret = register_fw_pm_ops();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) ret = register_reboot_notifier(&fw_shutdown_nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) return register_sysfs_loader();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) unregister_fw_pm_ops();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) static void __exit firmware_class_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) unregister_fw_pm_ops();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) unregister_reboot_notifier(&fw_shutdown_nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) unregister_sysfs_loader();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) fs_initcall(firmware_class_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) module_exit(firmware_class_exit);