1 // SPDX-License-Identifier: GPL-2.0
3 * main.c - Multi purpose firmware loading support
5 * Copyright (c) 2003 Manuel Estrada Sainz
7 * Please see Documentation/firmware_class/ for more information.
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 #include <linux/capability.h>
14 #include <linux/device.h>
15 #include <linux/module.h>
16 #include <linux/init.h>
17 #include <linux/timer.h>
18 #include <linux/vmalloc.h>
19 #include <linux/interrupt.h>
20 #include <linux/bitops.h>
21 #include <linux/mutex.h>
22 #include <linux/workqueue.h>
23 #include <linux/highmem.h>
24 #include <linux/firmware.h>
25 #include <linux/slab.h>
26 #include <linux/sched.h>
27 #include <linux/file.h>
28 #include <linux/list.h>
30 #include <linux/async.h>
32 #include <linux/suspend.h>
33 #include <linux/syscore_ops.h>
34 #include <linux/reboot.h>
35 #include <linux/security.h>
37 #include <generated/utsrelease.h>
43 MODULE_AUTHOR("Manuel Estrada Sainz");
44 MODULE_DESCRIPTION("Multi purpose firmware loading support");
45 MODULE_LICENSE("GPL");
47 struct firmware_cache {
48 /* firmware_buf instance will be added into the below list */
50 struct list_head head;
53 #ifdef CONFIG_PM_SLEEP
55 * Names of firmware images which have been cached successfully
56 * will be added into the below list so that device uncache
57 * helper can trace which firmware images have been cached
61 struct list_head fw_names;
63 struct delayed_work work;
65 struct notifier_block pm_notify;
69 struct fw_cache_entry {
70 struct list_head list;
79 static inline struct fw_priv *to_fw_priv(struct kref *ref)
81 return container_of(ref, struct fw_priv, ref);
84 #define FW_LOADER_NO_CACHE 0
85 #define FW_LOADER_START_CACHE 1
87 /* fw_lock could be moved to 'struct fw_sysfs' but since it is just
88 * guarding for corner cases a global lock should be OK */
89 DEFINE_MUTEX(fw_lock);
91 static struct firmware_cache fw_cache;
93 /* Builtin firmware support */
95 #ifdef CONFIG_FW_LOADER
97 extern struct builtin_fw __start_builtin_fw[];
98 extern struct builtin_fw __end_builtin_fw[];
100 static bool fw_copy_to_prealloc_buf(struct firmware *fw,
101 void *buf, size_t size)
107 memcpy(buf, fw->data, fw->size);
111 static bool fw_get_builtin_firmware(struct firmware *fw, const char *name,
112 void *buf, size_t size)
114 struct builtin_fw *b_fw;
116 for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++) {
117 if (strcmp(name, b_fw->name) == 0) {
118 fw->size = b_fw->size;
119 fw->data = b_fw->data;
120 return fw_copy_to_prealloc_buf(fw, buf, size);
127 static bool fw_is_builtin_firmware(const struct firmware *fw)
129 struct builtin_fw *b_fw;
131 for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++)
132 if (fw->data == b_fw->data)
138 #else /* Module case - no builtin firmware support */
140 static inline bool fw_get_builtin_firmware(struct firmware *fw,
141 const char *name, void *buf,
147 static inline bool fw_is_builtin_firmware(const struct firmware *fw)
153 static void fw_state_init(struct fw_priv *fw_priv)
155 struct fw_state *fw_st = &fw_priv->fw_st;
157 init_completion(&fw_st->completion);
158 fw_st->status = FW_STATUS_UNKNOWN;
161 static inline int fw_state_wait(struct fw_priv *fw_priv)
163 return __fw_state_wait_common(fw_priv, MAX_SCHEDULE_TIMEOUT);
166 static int fw_cache_piggyback_on_request(const char *name);
168 static struct fw_priv *__allocate_fw_priv(const char *fw_name,
169 struct firmware_cache *fwc,
170 void *dbuf, size_t size)
172 struct fw_priv *fw_priv;
174 fw_priv = kzalloc(sizeof(*fw_priv), GFP_ATOMIC);
178 fw_priv->fw_name = kstrdup_const(fw_name, GFP_ATOMIC);
179 if (!fw_priv->fw_name) {
184 kref_init(&fw_priv->ref);
186 fw_priv->data = dbuf;
187 fw_priv->allocated_size = size;
188 fw_state_init(fw_priv);
189 #ifdef CONFIG_FW_LOADER_USER_HELPER
190 INIT_LIST_HEAD(&fw_priv->pending_list);
193 pr_debug("%s: fw-%s fw_priv=%p\n", __func__, fw_name, fw_priv);
198 static struct fw_priv *__lookup_fw_priv(const char *fw_name)
201 struct firmware_cache *fwc = &fw_cache;
203 list_for_each_entry(tmp, &fwc->head, list)
204 if (!strcmp(tmp->fw_name, fw_name))
209 /* Returns 1 for batching firmware requests with the same name */
210 static int alloc_lookup_fw_priv(const char *fw_name,
211 struct firmware_cache *fwc,
212 struct fw_priv **fw_priv, void *dbuf,
213 size_t size, enum fw_opt opt_flags)
217 spin_lock(&fwc->lock);
218 if (!(opt_flags & FW_OPT_NOCACHE)) {
219 tmp = __lookup_fw_priv(fw_name);
222 spin_unlock(&fwc->lock);
224 pr_debug("batched request - sharing the same struct fw_priv and lookup for multiple requests\n");
229 tmp = __allocate_fw_priv(fw_name, fwc, dbuf, size);
231 INIT_LIST_HEAD(&tmp->list);
232 if (!(opt_flags & FW_OPT_NOCACHE))
233 list_add(&tmp->list, &fwc->head);
235 spin_unlock(&fwc->lock);
239 return tmp ? 0 : -ENOMEM;
242 static void __free_fw_priv(struct kref *ref)
243 __releases(&fwc->lock)
245 struct fw_priv *fw_priv = to_fw_priv(ref);
246 struct firmware_cache *fwc = fw_priv->fwc;
248 pr_debug("%s: fw-%s fw_priv=%p data=%p size=%u\n",
249 __func__, fw_priv->fw_name, fw_priv, fw_priv->data,
250 (unsigned int)fw_priv->size);
252 list_del(&fw_priv->list);
253 spin_unlock(&fwc->lock);
255 #ifdef CONFIG_FW_LOADER_USER_HELPER
256 if (fw_priv->is_paged_buf) {
258 vunmap(fw_priv->data);
259 for (i = 0; i < fw_priv->nr_pages; i++)
260 __free_page(fw_priv->pages[i]);
261 vfree(fw_priv->pages);
264 if (!fw_priv->allocated_size)
265 vfree(fw_priv->data);
266 kfree_const(fw_priv->fw_name);
270 static void free_fw_priv(struct fw_priv *fw_priv)
272 struct firmware_cache *fwc = fw_priv->fwc;
273 spin_lock(&fwc->lock);
274 if (!kref_put(&fw_priv->ref, __free_fw_priv))
275 spin_unlock(&fwc->lock);
278 /* direct firmware loading support */
279 static char fw_path_para[256];
280 static const char * const fw_path[] = {
282 "/lib/firmware/updates/" UTS_RELEASE,
283 "/lib/firmware/updates",
284 "/lib/firmware/" UTS_RELEASE,
289 * Typical usage is that passing 'firmware_class.path=$CUSTOMIZED_PATH'
290 * from kernel command line because firmware_class is generally built in
291 * kernel instead of module.
293 module_param_string(path, fw_path_para, sizeof(fw_path_para), 0644);
294 MODULE_PARM_DESC(path, "customized firmware image search path with a higher priority than default path");
297 fw_get_filesystem_firmware(struct device *device, struct fw_priv *fw_priv)
303 enum kernel_read_file_id id = READING_FIRMWARE;
304 size_t msize = INT_MAX;
306 /* Already populated data member means we're loading into a buffer */
308 id = READING_FIRMWARE_PREALLOC_BUFFER;
309 msize = fw_priv->allocated_size;
316 for (i = 0; i < ARRAY_SIZE(fw_path); i++) {
317 /* skip the unset customized path */
321 len = snprintf(path, PATH_MAX, "%s/%s",
322 fw_path[i], fw_priv->fw_name);
323 if (len >= PATH_MAX) {
329 rc = kernel_read_file_from_path(path, &fw_priv->data, &size,
333 dev_dbg(device, "loading %s failed with error %d\n",
336 dev_warn(device, "loading %s failed with error %d\n",
340 dev_dbg(device, "direct-loading %s\n", fw_priv->fw_name);
341 fw_priv->size = size;
342 fw_state_done(fw_priv);
350 /* firmware holds the ownership of pages */
351 static void firmware_free_data(const struct firmware *fw)
353 /* Loaded directly? */
358 free_fw_priv(fw->priv);
361 /* store the pages buffer info firmware from buf */
362 static void fw_set_page_data(struct fw_priv *fw_priv, struct firmware *fw)
365 #ifdef CONFIG_FW_LOADER_USER_HELPER
366 fw->pages = fw_priv->pages;
368 fw->size = fw_priv->size;
369 fw->data = fw_priv->data;
371 pr_debug("%s: fw-%s fw_priv=%p data=%p size=%u\n",
372 __func__, fw_priv->fw_name, fw_priv, fw_priv->data,
373 (unsigned int)fw_priv->size);
376 #ifdef CONFIG_PM_SLEEP
377 static void fw_name_devm_release(struct device *dev, void *res)
379 struct fw_name_devm *fwn = res;
381 if (fwn->magic == (unsigned long)&fw_cache)
382 pr_debug("%s: fw_name-%s devm-%p released\n",
383 __func__, fwn->name, res);
384 kfree_const(fwn->name);
387 static int fw_devm_match(struct device *dev, void *res,
390 struct fw_name_devm *fwn = res;
392 return (fwn->magic == (unsigned long)&fw_cache) &&
393 !strcmp(fwn->name, match_data);
396 static struct fw_name_devm *fw_find_devm_name(struct device *dev,
399 struct fw_name_devm *fwn;
401 fwn = devres_find(dev, fw_name_devm_release,
402 fw_devm_match, (void *)name);
406 static bool fw_cache_is_setup(struct device *dev, const char *name)
408 struct fw_name_devm *fwn;
410 fwn = fw_find_devm_name(dev, name);
417 /* add firmware name into devres list */
418 static int fw_add_devm_name(struct device *dev, const char *name)
420 struct fw_name_devm *fwn;
422 if (fw_cache_is_setup(dev, name))
425 fwn = devres_alloc(fw_name_devm_release, sizeof(struct fw_name_devm),
429 fwn->name = kstrdup_const(name, GFP_KERNEL);
435 fwn->magic = (unsigned long)&fw_cache;
436 devres_add(dev, fwn);
441 static bool fw_cache_is_setup(struct device *dev, const char *name)
446 static int fw_add_devm_name(struct device *dev, const char *name)
452 int assign_fw(struct firmware *fw, struct device *device,
453 enum fw_opt opt_flags)
455 struct fw_priv *fw_priv = fw->priv;
458 mutex_lock(&fw_lock);
459 if (!fw_priv->size || fw_state_is_aborted(fw_priv)) {
460 mutex_unlock(&fw_lock);
465 * add firmware name into devres list so that we can auto cache
466 * and uncache firmware for device.
468 * device may has been deleted already, but the problem
469 * should be fixed in devres or driver core.
471 /* don't cache firmware handled without uevent */
472 if (device && (opt_flags & FW_OPT_UEVENT) &&
473 !(opt_flags & FW_OPT_NOCACHE)) {
474 ret = fw_add_devm_name(device, fw_priv->fw_name);
476 mutex_unlock(&fw_lock);
482 * After caching firmware image is started, let it piggyback
483 * on request firmware.
485 if (!(opt_flags & FW_OPT_NOCACHE) &&
486 fw_priv->fwc->state == FW_LOADER_START_CACHE) {
487 if (fw_cache_piggyback_on_request(fw_priv->fw_name))
488 kref_get(&fw_priv->ref);
491 /* pass the pages buffer to driver at the last minute */
492 fw_set_page_data(fw_priv, fw);
493 mutex_unlock(&fw_lock);
497 /* prepare firmware and firmware_buf structs;
498 * return 0 if a firmware is already assigned, 1 if need to load one,
499 * or a negative error code
502 _request_firmware_prepare(struct firmware **firmware_p, const char *name,
503 struct device *device, void *dbuf, size_t size,
504 enum fw_opt opt_flags)
506 struct firmware *firmware;
507 struct fw_priv *fw_priv;
510 *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
512 dev_err(device, "%s: kmalloc(struct firmware) failed\n",
517 if (fw_get_builtin_firmware(firmware, name, dbuf, size)) {
518 dev_dbg(device, "using built-in %s\n", name);
519 return 0; /* assigned */
522 ret = alloc_lookup_fw_priv(name, &fw_cache, &fw_priv, dbuf, size,
526 * bind with 'priv' now to avoid warning in failure path
527 * of requesting firmware.
529 firmware->priv = fw_priv;
532 ret = fw_state_wait(fw_priv);
534 fw_set_page_data(fw_priv, firmware);
535 return 0; /* assigned */
541 return 1; /* need to load */
545 * Batched requests need only one wake, we need to do this step last due to the
546 * fallback mechanism. The buf is protected with kref_get(), and it won't be
547 * released until the last user calls release_firmware().
549 * Failed batched requests are possible as well, in such cases we just share
550 * the struct fw_priv and won't release it until all requests are woken
551 * and have gone through this same path.
553 static void fw_abort_batch_reqs(struct firmware *fw)
555 struct fw_priv *fw_priv;
557 /* Loaded directly? */
558 if (!fw || !fw->priv)
562 mutex_lock(&fw_lock);
563 if (!fw_state_is_aborted(fw_priv))
564 fw_state_aborted(fw_priv);
565 mutex_unlock(&fw_lock);
568 /* called from request_firmware() and request_firmware_work_func() */
570 _request_firmware(const struct firmware **firmware_p, const char *name,
571 struct device *device, void *buf, size_t size,
572 enum fw_opt opt_flags)
574 struct firmware *fw = NULL;
580 if (!name || name[0] == '\0') {
585 ret = _request_firmware_prepare(&fw, name, device, buf, size,
587 if (ret <= 0) /* error or already assigned */
590 ret = fw_get_filesystem_firmware(device, fw->priv);
592 if (!(opt_flags & FW_OPT_NO_WARN))
594 "Direct firmware load for %s failed with error %d\n",
596 ret = firmware_fallback_sysfs(fw, name, device, opt_flags, ret);
598 ret = assign_fw(fw, device, opt_flags);
602 fw_abort_batch_reqs(fw);
603 release_firmware(fw);
612 * request_firmware() - send firmware request and wait for it
613 * @firmware_p: pointer to firmware image
614 * @name: name of firmware file
615 * @device: device for which firmware is being loaded
617 * @firmware_p will be used to return a firmware image by the name
618 * of @name for device @device.
620 * Should be called from user context where sleeping is allowed.
622 * @name will be used as $FIRMWARE in the uevent environment and
623 * should be distinctive enough not to be confused with any other
624 * firmware image for this or any other device.
626 * Caller must hold the reference count of @device.
628 * The function can be called safely inside device's suspend and
632 request_firmware(const struct firmware **firmware_p, const char *name,
633 struct device *device)
637 /* Need to pin this module until return */
638 __module_get(THIS_MODULE);
639 ret = _request_firmware(firmware_p, name, device, NULL, 0,
641 module_put(THIS_MODULE);
644 EXPORT_SYMBOL(request_firmware);
647 * firmware_request_nowarn() - request for an optional fw module
648 * @firmware: pointer to firmware image
649 * @name: name of firmware file
650 * @device: device for which firmware is being loaded
652 * This function is similar in behaviour to request_firmware(), except
653 * it doesn't produce warning messages when the file is not found.
654 * The sysfs fallback mechanism is enabled if direct filesystem lookup fails,
655 * however, however failures to find the firmware file with it are still
656 * suppressed. It is therefore up to the driver to check for the return value
657 * of this call and to decide when to inform the users of errors.
659 int firmware_request_nowarn(const struct firmware **firmware, const char *name,
660 struct device *device)
664 /* Need to pin this module until return */
665 __module_get(THIS_MODULE);
666 ret = _request_firmware(firmware, name, device, NULL, 0,
667 FW_OPT_UEVENT | FW_OPT_NO_WARN);
668 module_put(THIS_MODULE);
671 EXPORT_SYMBOL_GPL(firmware_request_nowarn);
674 * request_firmware_direct() - load firmware directly without usermode helper
675 * @firmware_p: pointer to firmware image
676 * @name: name of firmware file
677 * @device: device for which firmware is being loaded
679 * This function works pretty much like request_firmware(), but this doesn't
680 * fall back to usermode helper even if the firmware couldn't be loaded
681 * directly from fs. Hence it's useful for loading optional firmwares, which
682 * aren't always present, without extra long timeouts of udev.
684 int request_firmware_direct(const struct firmware **firmware_p,
685 const char *name, struct device *device)
689 __module_get(THIS_MODULE);
690 ret = _request_firmware(firmware_p, name, device, NULL, 0,
691 FW_OPT_UEVENT | FW_OPT_NO_WARN |
693 module_put(THIS_MODULE);
696 EXPORT_SYMBOL_GPL(request_firmware_direct);
699 * firmware_request_cache() - cache firmware for suspend so resume can use it
700 * @name: name of firmware file
701 * @device: device for which firmware should be cached for
703 * There are some devices with an optimization that enables the device to not
704 * require loading firmware on system reboot. This optimization may still
705 * require the firmware present on resume from suspend. This routine can be
706 * used to ensure the firmware is present on resume from suspend in these
707 * situations. This helper is not compatible with drivers which use
708 * request_firmware_into_buf() or request_firmware_nowait() with no uevent set.
710 int firmware_request_cache(struct device *device, const char *name)
714 mutex_lock(&fw_lock);
715 ret = fw_add_devm_name(device, name);
716 mutex_unlock(&fw_lock);
720 EXPORT_SYMBOL_GPL(firmware_request_cache);
723 * request_firmware_into_buf() - load firmware into a previously allocated buffer
724 * @firmware_p: pointer to firmware image
725 * @name: name of firmware file
726 * @device: device for which firmware is being loaded and DMA region allocated
727 * @buf: address of buffer to load firmware into
728 * @size: size of buffer
730 * This function works pretty much like request_firmware(), but it doesn't
731 * allocate a buffer to hold the firmware data. Instead, the firmware
732 * is loaded directly into the buffer pointed to by @buf and the @firmware_p
733 * data member is pointed at @buf.
735 * This function doesn't cache firmware either.
738 request_firmware_into_buf(const struct firmware **firmware_p, const char *name,
739 struct device *device, void *buf, size_t size)
743 if (fw_cache_is_setup(device, name))
746 __module_get(THIS_MODULE);
747 ret = _request_firmware(firmware_p, name, device, buf, size,
748 FW_OPT_UEVENT | FW_OPT_NOCACHE);
749 module_put(THIS_MODULE);
752 EXPORT_SYMBOL(request_firmware_into_buf);
755 * release_firmware() - release the resource associated with a firmware image
756 * @fw: firmware resource to release
758 void release_firmware(const struct firmware *fw)
761 if (!fw_is_builtin_firmware(fw))
762 firmware_free_data(fw);
766 EXPORT_SYMBOL(release_firmware);
769 struct firmware_work {
770 struct work_struct work;
771 struct module *module;
773 struct device *device;
775 void (*cont)(const struct firmware *fw, void *context);
776 enum fw_opt opt_flags;
779 static void request_firmware_work_func(struct work_struct *work)
781 struct firmware_work *fw_work;
782 const struct firmware *fw;
784 fw_work = container_of(work, struct firmware_work, work);
786 _request_firmware(&fw, fw_work->name, fw_work->device, NULL, 0,
788 fw_work->cont(fw, fw_work->context);
789 put_device(fw_work->device); /* taken in request_firmware_nowait() */
791 module_put(fw_work->module);
792 kfree_const(fw_work->name);
797 * request_firmware_nowait() - asynchronous version of request_firmware
798 * @module: module requesting the firmware
799 * @uevent: sends uevent to copy the firmware image if this flag
800 * is non-zero else the firmware copy must be done manually.
801 * @name: name of firmware file
802 * @device: device for which firmware is being loaded
803 * @gfp: allocation flags
804 * @context: will be passed over to @cont, and
805 * @fw may be %NULL if firmware request fails.
806 * @cont: function will be called asynchronously when the firmware
809 * Caller must hold the reference count of @device.
811 * Asynchronous variant of request_firmware() for user contexts:
812 * - sleep for as small periods as possible since it may
813 * increase kernel boot time of built-in device drivers
814 * requesting firmware in their ->probe() methods, if
815 * @gfp is GFP_KERNEL.
817 * - can't sleep at all if @gfp is GFP_ATOMIC.
820 request_firmware_nowait(
821 struct module *module, bool uevent,
822 const char *name, struct device *device, gfp_t gfp, void *context,
823 void (*cont)(const struct firmware *fw, void *context))
825 struct firmware_work *fw_work;
827 fw_work = kzalloc(sizeof(struct firmware_work), gfp);
831 fw_work->module = module;
832 fw_work->name = kstrdup_const(name, gfp);
833 if (!fw_work->name) {
837 fw_work->device = device;
838 fw_work->context = context;
839 fw_work->cont = cont;
840 fw_work->opt_flags = FW_OPT_NOWAIT |
841 (uevent ? FW_OPT_UEVENT : FW_OPT_USERHELPER);
843 if (!uevent && fw_cache_is_setup(device, name)) {
844 kfree_const(fw_work->name);
849 if (!try_module_get(module)) {
850 kfree_const(fw_work->name);
855 get_device(fw_work->device);
856 INIT_WORK(&fw_work->work, request_firmware_work_func);
857 schedule_work(&fw_work->work);
860 EXPORT_SYMBOL(request_firmware_nowait);
862 #ifdef CONFIG_PM_SLEEP
863 static ASYNC_DOMAIN_EXCLUSIVE(fw_cache_domain);
866 * cache_firmware() - cache one firmware image in kernel memory space
867 * @fw_name: the firmware image name
869 * Cache firmware in kernel memory so that drivers can use it when
870 * system isn't ready for them to request firmware image from userspace.
871 * Once it returns successfully, driver can use request_firmware or its
872 * nowait version to get the cached firmware without any interacting
875 * Return 0 if the firmware image has been cached successfully
876 * Return !0 otherwise
879 static int cache_firmware(const char *fw_name)
882 const struct firmware *fw;
884 pr_debug("%s: %s\n", __func__, fw_name);
886 ret = request_firmware(&fw, fw_name, NULL);
890 pr_debug("%s: %s ret=%d\n", __func__, fw_name, ret);
895 static struct fw_priv *lookup_fw_priv(const char *fw_name)
898 struct firmware_cache *fwc = &fw_cache;
900 spin_lock(&fwc->lock);
901 tmp = __lookup_fw_priv(fw_name);
902 spin_unlock(&fwc->lock);
908 * uncache_firmware() - remove one cached firmware image
909 * @fw_name: the firmware image name
911 * Uncache one firmware image which has been cached successfully
914 * Return 0 if the firmware cache has been removed successfully
915 * Return !0 otherwise
918 static int uncache_firmware(const char *fw_name)
920 struct fw_priv *fw_priv;
923 pr_debug("%s: %s\n", __func__, fw_name);
925 if (fw_get_builtin_firmware(&fw, fw_name, NULL, 0))
928 fw_priv = lookup_fw_priv(fw_name);
930 free_fw_priv(fw_priv);
937 static struct fw_cache_entry *alloc_fw_cache_entry(const char *name)
939 struct fw_cache_entry *fce;
941 fce = kzalloc(sizeof(*fce), GFP_ATOMIC);
945 fce->name = kstrdup_const(name, GFP_ATOMIC);
955 static int __fw_entry_found(const char *name)
957 struct firmware_cache *fwc = &fw_cache;
958 struct fw_cache_entry *fce;
960 list_for_each_entry(fce, &fwc->fw_names, list) {
961 if (!strcmp(fce->name, name))
967 static int fw_cache_piggyback_on_request(const char *name)
969 struct firmware_cache *fwc = &fw_cache;
970 struct fw_cache_entry *fce;
973 spin_lock(&fwc->name_lock);
974 if (__fw_entry_found(name))
977 fce = alloc_fw_cache_entry(name);
980 list_add(&fce->list, &fwc->fw_names);
981 pr_debug("%s: fw: %s\n", __func__, name);
984 spin_unlock(&fwc->name_lock);
988 static void free_fw_cache_entry(struct fw_cache_entry *fce)
990 kfree_const(fce->name);
994 static void __async_dev_cache_fw_image(void *fw_entry,
995 async_cookie_t cookie)
997 struct fw_cache_entry *fce = fw_entry;
998 struct firmware_cache *fwc = &fw_cache;
1001 ret = cache_firmware(fce->name);
1003 spin_lock(&fwc->name_lock);
1004 list_del(&fce->list);
1005 spin_unlock(&fwc->name_lock);
1007 free_fw_cache_entry(fce);
1011 /* called with dev->devres_lock held */
1012 static void dev_create_fw_entry(struct device *dev, void *res,
1015 struct fw_name_devm *fwn = res;
1016 const char *fw_name = fwn->name;
1017 struct list_head *head = data;
1018 struct fw_cache_entry *fce;
1020 fce = alloc_fw_cache_entry(fw_name);
1022 list_add(&fce->list, head);
1025 static int devm_name_match(struct device *dev, void *res,
1028 struct fw_name_devm *fwn = res;
1029 return (fwn->magic == (unsigned long)match_data);
1032 static void dev_cache_fw_image(struct device *dev, void *data)
1035 struct fw_cache_entry *fce;
1036 struct fw_cache_entry *fce_next;
1037 struct firmware_cache *fwc = &fw_cache;
1039 devres_for_each_res(dev, fw_name_devm_release,
1040 devm_name_match, &fw_cache,
1041 dev_create_fw_entry, &todo);
1043 list_for_each_entry_safe(fce, fce_next, &todo, list) {
1044 list_del(&fce->list);
1046 spin_lock(&fwc->name_lock);
1047 /* only one cache entry for one firmware */
1048 if (!__fw_entry_found(fce->name)) {
1049 list_add(&fce->list, &fwc->fw_names);
1051 free_fw_cache_entry(fce);
1054 spin_unlock(&fwc->name_lock);
1057 async_schedule_domain(__async_dev_cache_fw_image,
1063 static void __device_uncache_fw_images(void)
1065 struct firmware_cache *fwc = &fw_cache;
1066 struct fw_cache_entry *fce;
1068 spin_lock(&fwc->name_lock);
1069 while (!list_empty(&fwc->fw_names)) {
1070 fce = list_entry(fwc->fw_names.next,
1071 struct fw_cache_entry, list);
1072 list_del(&fce->list);
1073 spin_unlock(&fwc->name_lock);
1075 uncache_firmware(fce->name);
1076 free_fw_cache_entry(fce);
1078 spin_lock(&fwc->name_lock);
1080 spin_unlock(&fwc->name_lock);
1084 * device_cache_fw_images() - cache devices' firmware
1086 * If one device called request_firmware or its nowait version
1087 * successfully before, the firmware names are recored into the
1088 * device's devres link list, so device_cache_fw_images can call
1089 * cache_firmware() to cache these firmwares for the device,
1090 * then the device driver can load its firmwares easily at
1091 * time when system is not ready to complete loading firmware.
1093 static void device_cache_fw_images(void)
1095 struct firmware_cache *fwc = &fw_cache;
1098 pr_debug("%s\n", __func__);
1100 /* cancel uncache work */
1101 cancel_delayed_work_sync(&fwc->work);
1103 fw_fallback_set_cache_timeout();
1105 mutex_lock(&fw_lock);
1106 fwc->state = FW_LOADER_START_CACHE;
1107 dpm_for_each_dev(NULL, dev_cache_fw_image);
1108 mutex_unlock(&fw_lock);
1110 /* wait for completion of caching firmware for all devices */
1111 async_synchronize_full_domain(&fw_cache_domain);
1113 fw_fallback_set_default_timeout();
1117 * device_uncache_fw_images() - uncache devices' firmware
1119 * uncache all firmwares which have been cached successfully
1120 * by device_uncache_fw_images earlier
1122 static void device_uncache_fw_images(void)
1124 pr_debug("%s\n", __func__);
1125 __device_uncache_fw_images();
1128 static void device_uncache_fw_images_work(struct work_struct *work)
1130 device_uncache_fw_images();
1134 * device_uncache_fw_images_delay() - uncache devices firmwares
1135 * @delay: number of milliseconds to delay uncache device firmwares
1137 * uncache all devices's firmwares which has been cached successfully
1138 * by device_cache_fw_images after @delay milliseconds.
1140 static void device_uncache_fw_images_delay(unsigned long delay)
1142 queue_delayed_work(system_power_efficient_wq, &fw_cache.work,
1143 msecs_to_jiffies(delay));
1146 static int fw_pm_notify(struct notifier_block *notify_block,
1147 unsigned long mode, void *unused)
1150 case PM_HIBERNATION_PREPARE:
1151 case PM_SUSPEND_PREPARE:
1152 case PM_RESTORE_PREPARE:
1154 * kill pending fallback requests with a custom fallback
1155 * to avoid stalling suspend.
1157 kill_pending_fw_fallback_reqs(true);
1158 device_cache_fw_images();
1161 case PM_POST_SUSPEND:
1162 case PM_POST_HIBERNATION:
1163 case PM_POST_RESTORE:
1165 * In case that system sleep failed and syscore_suspend is
1168 mutex_lock(&fw_lock);
1169 fw_cache.state = FW_LOADER_NO_CACHE;
1170 mutex_unlock(&fw_lock);
1172 device_uncache_fw_images_delay(10 * MSEC_PER_SEC);
1179 /* stop caching firmware once syscore_suspend is reached */
1180 static int fw_suspend(void)
1182 fw_cache.state = FW_LOADER_NO_CACHE;
1186 static struct syscore_ops fw_syscore_ops = {
1187 .suspend = fw_suspend,
1190 static int __init register_fw_pm_ops(void)
1194 spin_lock_init(&fw_cache.name_lock);
1195 INIT_LIST_HEAD(&fw_cache.fw_names);
1197 INIT_DELAYED_WORK(&fw_cache.work,
1198 device_uncache_fw_images_work);
1200 fw_cache.pm_notify.notifier_call = fw_pm_notify;
1201 ret = register_pm_notifier(&fw_cache.pm_notify);
1205 register_syscore_ops(&fw_syscore_ops);
1210 static inline void unregister_fw_pm_ops(void)
1212 unregister_syscore_ops(&fw_syscore_ops);
1213 unregister_pm_notifier(&fw_cache.pm_notify);
1216 static int fw_cache_piggyback_on_request(const char *name)
1220 static inline int register_fw_pm_ops(void)
1224 static inline void unregister_fw_pm_ops(void)
1229 static void __init fw_cache_init(void)
1231 spin_lock_init(&fw_cache.lock);
1232 INIT_LIST_HEAD(&fw_cache.head);
1233 fw_cache.state = FW_LOADER_NO_CACHE;
1236 static int fw_shutdown_notify(struct notifier_block *unused1,
1237 unsigned long unused2, void *unused3)
1240 * Kill all pending fallback requests to avoid both stalling shutdown,
1241 * and avoid a deadlock with the usermode_lock.
1243 kill_pending_fw_fallback_reqs(false);
1248 static struct notifier_block fw_shutdown_nb = {
1249 .notifier_call = fw_shutdown_notify,
1252 static int __init firmware_class_init(void)
1256 /* No need to unfold these on exit */
1259 ret = register_fw_pm_ops();
1263 ret = register_reboot_notifier(&fw_shutdown_nb);
1267 return register_sysfs_loader();
1270 unregister_fw_pm_ops();
1274 static void __exit firmware_class_exit(void)
1276 unregister_fw_pm_ops();
1277 unregister_reboot_notifier(&fw_shutdown_nb);
1278 unregister_sysfs_loader();
1281 fs_initcall(firmware_class_init);
1282 module_exit(firmware_class_exit);