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 void fw_copy_to_prealloc_buf(struct firmware *fw,
101 void *buf, size_t size)
103 if (!buf || size < fw->size)
105 memcpy(buf, fw->data, fw->size);
108 static bool fw_get_builtin_firmware(struct firmware *fw, const char *name,
109 void *buf, size_t size)
111 struct builtin_fw *b_fw;
113 for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++) {
114 if (strcmp(name, b_fw->name) == 0) {
115 fw->size = b_fw->size;
116 fw->data = b_fw->data;
117 fw_copy_to_prealloc_buf(fw, buf, size);
126 static bool fw_is_builtin_firmware(const struct firmware *fw)
128 struct builtin_fw *b_fw;
130 for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++)
131 if (fw->data == b_fw->data)
137 #else /* Module case - no builtin firmware support */
139 static inline bool fw_get_builtin_firmware(struct firmware *fw,
140 const char *name, void *buf,
146 static inline bool fw_is_builtin_firmware(const struct firmware *fw)
152 static void fw_state_init(struct fw_priv *fw_priv)
154 struct fw_state *fw_st = &fw_priv->fw_st;
156 init_completion(&fw_st->completion);
157 fw_st->status = FW_STATUS_UNKNOWN;
160 static inline int fw_state_wait(struct fw_priv *fw_priv)
162 return __fw_state_wait_common(fw_priv, MAX_SCHEDULE_TIMEOUT);
165 static int fw_cache_piggyback_on_request(const char *name);
167 static struct fw_priv *__allocate_fw_priv(const char *fw_name,
168 struct firmware_cache *fwc,
169 void *dbuf, size_t size)
171 struct fw_priv *fw_priv;
173 fw_priv = kzalloc(sizeof(*fw_priv), GFP_ATOMIC);
177 fw_priv->fw_name = kstrdup_const(fw_name, GFP_ATOMIC);
178 if (!fw_priv->fw_name) {
183 kref_init(&fw_priv->ref);
185 fw_priv->data = dbuf;
186 fw_priv->allocated_size = size;
187 fw_state_init(fw_priv);
188 #ifdef CONFIG_FW_LOADER_USER_HELPER
189 INIT_LIST_HEAD(&fw_priv->pending_list);
192 pr_debug("%s: fw-%s fw_priv=%p\n", __func__, fw_name, fw_priv);
197 static struct fw_priv *__lookup_fw_priv(const char *fw_name)
200 struct firmware_cache *fwc = &fw_cache;
202 list_for_each_entry(tmp, &fwc->head, list)
203 if (!strcmp(tmp->fw_name, fw_name))
208 /* Returns 1 for batching firmware requests with the same name */
209 static int alloc_lookup_fw_priv(const char *fw_name,
210 struct firmware_cache *fwc,
211 struct fw_priv **fw_priv, void *dbuf,
216 spin_lock(&fwc->lock);
217 tmp = __lookup_fw_priv(fw_name);
220 spin_unlock(&fwc->lock);
222 pr_debug("batched request - sharing the same struct fw_priv and lookup for multiple requests\n");
225 tmp = __allocate_fw_priv(fw_name, fwc, dbuf, size);
227 list_add(&tmp->list, &fwc->head);
228 spin_unlock(&fwc->lock);
232 return tmp ? 0 : -ENOMEM;
235 static void __free_fw_priv(struct kref *ref)
236 __releases(&fwc->lock)
238 struct fw_priv *fw_priv = to_fw_priv(ref);
239 struct firmware_cache *fwc = fw_priv->fwc;
241 pr_debug("%s: fw-%s fw_priv=%p data=%p size=%u\n",
242 __func__, fw_priv->fw_name, fw_priv, fw_priv->data,
243 (unsigned int)fw_priv->size);
245 list_del(&fw_priv->list);
246 spin_unlock(&fwc->lock);
248 #ifdef CONFIG_FW_LOADER_USER_HELPER
249 if (fw_priv->is_paged_buf) {
251 vunmap(fw_priv->data);
252 for (i = 0; i < fw_priv->nr_pages; i++)
253 __free_page(fw_priv->pages[i]);
254 vfree(fw_priv->pages);
257 if (!fw_priv->allocated_size)
258 vfree(fw_priv->data);
259 kfree_const(fw_priv->fw_name);
263 static void free_fw_priv(struct fw_priv *fw_priv)
265 struct firmware_cache *fwc = fw_priv->fwc;
266 spin_lock(&fwc->lock);
267 if (!kref_put(&fw_priv->ref, __free_fw_priv))
268 spin_unlock(&fwc->lock);
271 /* direct firmware loading support */
272 static char fw_path_para[256];
273 static const char * const fw_path[] = {
275 "/lib/firmware/updates/" UTS_RELEASE,
276 "/lib/firmware/updates",
277 "/lib/firmware/" UTS_RELEASE,
282 * Typical usage is that passing 'firmware_class.path=$CUSTOMIZED_PATH'
283 * from kernel command line because firmware_class is generally built in
284 * kernel instead of module.
286 module_param_string(path, fw_path_para, sizeof(fw_path_para), 0644);
287 MODULE_PARM_DESC(path, "customized firmware image search path with a higher priority than default path");
290 fw_get_filesystem_firmware(struct device *device, struct fw_priv *fw_priv)
296 enum kernel_read_file_id id = READING_FIRMWARE;
297 size_t msize = INT_MAX;
299 /* Already populated data member means we're loading into a buffer */
301 id = READING_FIRMWARE_PREALLOC_BUFFER;
302 msize = fw_priv->allocated_size;
309 for (i = 0; i < ARRAY_SIZE(fw_path); i++) {
310 /* skip the unset customized path */
314 len = snprintf(path, PATH_MAX, "%s/%s",
315 fw_path[i], fw_priv->fw_name);
316 if (len >= PATH_MAX) {
322 rc = kernel_read_file_from_path(path, &fw_priv->data, &size,
326 dev_dbg(device, "loading %s failed with error %d\n",
329 dev_warn(device, "loading %s failed with error %d\n",
333 dev_dbg(device, "direct-loading %s\n", fw_priv->fw_name);
334 fw_priv->size = size;
335 fw_state_done(fw_priv);
343 /* firmware holds the ownership of pages */
344 static void firmware_free_data(const struct firmware *fw)
346 /* Loaded directly? */
351 free_fw_priv(fw->priv);
354 /* store the pages buffer info firmware from buf */
355 static void fw_set_page_data(struct fw_priv *fw_priv, struct firmware *fw)
358 #ifdef CONFIG_FW_LOADER_USER_HELPER
359 fw->pages = fw_priv->pages;
361 fw->size = fw_priv->size;
362 fw->data = fw_priv->data;
364 pr_debug("%s: fw-%s fw_priv=%p data=%p size=%u\n",
365 __func__, fw_priv->fw_name, fw_priv, fw_priv->data,
366 (unsigned int)fw_priv->size);
369 #ifdef CONFIG_PM_SLEEP
370 static void fw_name_devm_release(struct device *dev, void *res)
372 struct fw_name_devm *fwn = res;
374 if (fwn->magic == (unsigned long)&fw_cache)
375 pr_debug("%s: fw_name-%s devm-%p released\n",
376 __func__, fwn->name, res);
377 kfree_const(fwn->name);
380 static int fw_devm_match(struct device *dev, void *res,
383 struct fw_name_devm *fwn = res;
385 return (fwn->magic == (unsigned long)&fw_cache) &&
386 !strcmp(fwn->name, match_data);
389 static struct fw_name_devm *fw_find_devm_name(struct device *dev,
392 struct fw_name_devm *fwn;
394 fwn = devres_find(dev, fw_name_devm_release,
395 fw_devm_match, (void *)name);
399 static bool fw_cache_is_setup(struct device *dev, const char *name)
401 struct fw_name_devm *fwn;
403 fwn = fw_find_devm_name(dev, name);
410 /* add firmware name into devres list */
411 static int fw_add_devm_name(struct device *dev, const char *name)
413 struct fw_name_devm *fwn;
415 if (fw_cache_is_setup(dev, name))
418 fwn = devres_alloc(fw_name_devm_release, sizeof(struct fw_name_devm),
422 fwn->name = kstrdup_const(name, GFP_KERNEL);
428 fwn->magic = (unsigned long)&fw_cache;
429 devres_add(dev, fwn);
434 static bool fw_cache_is_setup(struct device *dev, const char *name)
439 static int fw_add_devm_name(struct device *dev, const char *name)
445 int assign_fw(struct firmware *fw, struct device *device,
446 enum fw_opt opt_flags)
448 struct fw_priv *fw_priv = fw->priv;
451 mutex_lock(&fw_lock);
452 if (!fw_priv->size || fw_state_is_aborted(fw_priv)) {
453 mutex_unlock(&fw_lock);
458 * add firmware name into devres list so that we can auto cache
459 * and uncache firmware for device.
461 * device may has been deleted already, but the problem
462 * should be fixed in devres or driver core.
464 /* don't cache firmware handled without uevent */
465 if (device && (opt_flags & FW_OPT_UEVENT) &&
466 !(opt_flags & FW_OPT_NOCACHE)) {
467 ret = fw_add_devm_name(device, fw_priv->fw_name);
469 mutex_unlock(&fw_lock);
475 * After caching firmware image is started, let it piggyback
476 * on request firmware.
478 if (!(opt_flags & FW_OPT_NOCACHE) &&
479 fw_priv->fwc->state == FW_LOADER_START_CACHE) {
480 if (fw_cache_piggyback_on_request(fw_priv->fw_name))
481 kref_get(&fw_priv->ref);
484 /* pass the pages buffer to driver at the last minute */
485 fw_set_page_data(fw_priv, fw);
486 mutex_unlock(&fw_lock);
490 /* prepare firmware and firmware_buf structs;
491 * return 0 if a firmware is already assigned, 1 if need to load one,
492 * or a negative error code
495 _request_firmware_prepare(struct firmware **firmware_p, const char *name,
496 struct device *device, void *dbuf, size_t size)
498 struct firmware *firmware;
499 struct fw_priv *fw_priv;
502 *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
504 dev_err(device, "%s: kmalloc(struct firmware) failed\n",
509 if (fw_get_builtin_firmware(firmware, name, dbuf, size)) {
510 dev_dbg(device, "using built-in %s\n", name);
511 return 0; /* assigned */
514 ret = alloc_lookup_fw_priv(name, &fw_cache, &fw_priv, dbuf, size);
517 * bind with 'priv' now to avoid warning in failure path
518 * of requesting firmware.
520 firmware->priv = fw_priv;
523 ret = fw_state_wait(fw_priv);
525 fw_set_page_data(fw_priv, firmware);
526 return 0; /* assigned */
532 return 1; /* need to load */
536 * Batched requests need only one wake, we need to do this step last due to the
537 * fallback mechanism. The buf is protected with kref_get(), and it won't be
538 * released until the last user calls release_firmware().
540 * Failed batched requests are possible as well, in such cases we just share
541 * the struct fw_priv and won't release it until all requests are woken
542 * and have gone through this same path.
544 static void fw_abort_batch_reqs(struct firmware *fw)
546 struct fw_priv *fw_priv;
548 /* Loaded directly? */
549 if (!fw || !fw->priv)
553 if (!fw_state_is_aborted(fw_priv))
554 fw_state_aborted(fw_priv);
557 /* called from request_firmware() and request_firmware_work_func() */
559 _request_firmware(const struct firmware **firmware_p, const char *name,
560 struct device *device, void *buf, size_t size,
561 enum fw_opt opt_flags)
563 struct firmware *fw = NULL;
569 if (!name || name[0] == '\0') {
574 ret = _request_firmware_prepare(&fw, name, device, buf, size);
575 if (ret <= 0) /* error or already assigned */
578 ret = fw_get_filesystem_firmware(device, fw->priv);
580 if (!(opt_flags & FW_OPT_NO_WARN))
582 "Direct firmware load for %s failed with error %d\n",
584 ret = firmware_fallback_sysfs(fw, name, device, opt_flags, ret);
586 ret = assign_fw(fw, device, opt_flags);
590 fw_abort_batch_reqs(fw);
591 release_firmware(fw);
600 * request_firmware() - send firmware request and wait for it
601 * @firmware_p: pointer to firmware image
602 * @name: name of firmware file
603 * @device: device for which firmware is being loaded
605 * @firmware_p will be used to return a firmware image by the name
606 * of @name for device @device.
608 * Should be called from user context where sleeping is allowed.
610 * @name will be used as $FIRMWARE in the uevent environment and
611 * should be distinctive enough not to be confused with any other
612 * firmware image for this or any other device.
614 * Caller must hold the reference count of @device.
616 * The function can be called safely inside device's suspend and
620 request_firmware(const struct firmware **firmware_p, const char *name,
621 struct device *device)
625 /* Need to pin this module until return */
626 __module_get(THIS_MODULE);
627 ret = _request_firmware(firmware_p, name, device, NULL, 0,
629 module_put(THIS_MODULE);
632 EXPORT_SYMBOL(request_firmware);
635 * firmware_request_nowarn() - request for an optional fw module
636 * @firmware: pointer to firmware image
637 * @name: name of firmware file
638 * @device: device for which firmware is being loaded
640 * This function is similar in behaviour to request_firmware(), except
641 * it doesn't produce warning messages when the file is not found.
642 * The sysfs fallback mechanism is enabled if direct filesystem lookup fails,
643 * however, however failures to find the firmware file with it are still
644 * suppressed. It is therefore up to the driver to check for the return value
645 * of this call and to decide when to inform the users of errors.
647 int firmware_request_nowarn(const struct firmware **firmware, const char *name,
648 struct device *device)
652 /* Need to pin this module until return */
653 __module_get(THIS_MODULE);
654 ret = _request_firmware(firmware, name, device, NULL, 0,
655 FW_OPT_UEVENT | FW_OPT_NO_WARN);
656 module_put(THIS_MODULE);
659 EXPORT_SYMBOL_GPL(firmware_request_nowarn);
662 * request_firmware_direct() - load firmware directly without usermode helper
663 * @firmware_p: pointer to firmware image
664 * @name: name of firmware file
665 * @device: device for which firmware is being loaded
667 * This function works pretty much like request_firmware(), but this doesn't
668 * fall back to usermode helper even if the firmware couldn't be loaded
669 * directly from fs. Hence it's useful for loading optional firmwares, which
670 * aren't always present, without extra long timeouts of udev.
672 int request_firmware_direct(const struct firmware **firmware_p,
673 const char *name, struct device *device)
677 __module_get(THIS_MODULE);
678 ret = _request_firmware(firmware_p, name, device, NULL, 0,
679 FW_OPT_UEVENT | FW_OPT_NO_WARN |
681 module_put(THIS_MODULE);
684 EXPORT_SYMBOL_GPL(request_firmware_direct);
687 * firmware_request_cache() - cache firmware for suspend so resume can use it
688 * @name: name of firmware file
689 * @device: device for which firmware should be cached for
691 * There are some devices with an optimization that enables the device to not
692 * require loading firmware on system reboot. This optimization may still
693 * require the firmware present on resume from suspend. This routine can be
694 * used to ensure the firmware is present on resume from suspend in these
695 * situations. This helper is not compatible with drivers which use
696 * request_firmware_into_buf() or request_firmware_nowait() with no uevent set.
698 int firmware_request_cache(struct device *device, const char *name)
702 mutex_lock(&fw_lock);
703 ret = fw_add_devm_name(device, name);
704 mutex_unlock(&fw_lock);
708 EXPORT_SYMBOL_GPL(firmware_request_cache);
711 * request_firmware_into_buf() - load firmware into a previously allocated buffer
712 * @firmware_p: pointer to firmware image
713 * @name: name of firmware file
714 * @device: device for which firmware is being loaded and DMA region allocated
715 * @buf: address of buffer to load firmware into
716 * @size: size of buffer
718 * This function works pretty much like request_firmware(), but it doesn't
719 * allocate a buffer to hold the firmware data. Instead, the firmware
720 * is loaded directly into the buffer pointed to by @buf and the @firmware_p
721 * data member is pointed at @buf.
723 * This function doesn't cache firmware either.
726 request_firmware_into_buf(const struct firmware **firmware_p, const char *name,
727 struct device *device, void *buf, size_t size)
731 if (fw_cache_is_setup(device, name))
734 __module_get(THIS_MODULE);
735 ret = _request_firmware(firmware_p, name, device, buf, size,
736 FW_OPT_UEVENT | FW_OPT_NOCACHE);
737 module_put(THIS_MODULE);
740 EXPORT_SYMBOL(request_firmware_into_buf);
743 * release_firmware() - release the resource associated with a firmware image
744 * @fw: firmware resource to release
746 void release_firmware(const struct firmware *fw)
749 if (!fw_is_builtin_firmware(fw))
750 firmware_free_data(fw);
754 EXPORT_SYMBOL(release_firmware);
757 struct firmware_work {
758 struct work_struct work;
759 struct module *module;
761 struct device *device;
763 void (*cont)(const struct firmware *fw, void *context);
764 enum fw_opt opt_flags;
767 static void request_firmware_work_func(struct work_struct *work)
769 struct firmware_work *fw_work;
770 const struct firmware *fw;
772 fw_work = container_of(work, struct firmware_work, work);
774 _request_firmware(&fw, fw_work->name, fw_work->device, NULL, 0,
776 fw_work->cont(fw, fw_work->context);
777 put_device(fw_work->device); /* taken in request_firmware_nowait() */
779 module_put(fw_work->module);
780 kfree_const(fw_work->name);
785 * request_firmware_nowait() - asynchronous version of request_firmware
786 * @module: module requesting the firmware
787 * @uevent: sends uevent to copy the firmware image if this flag
788 * is non-zero else the firmware copy must be done manually.
789 * @name: name of firmware file
790 * @device: device for which firmware is being loaded
791 * @gfp: allocation flags
792 * @context: will be passed over to @cont, and
793 * @fw may be %NULL if firmware request fails.
794 * @cont: function will be called asynchronously when the firmware
797 * Caller must hold the reference count of @device.
799 * Asynchronous variant of request_firmware() for user contexts:
800 * - sleep for as small periods as possible since it may
801 * increase kernel boot time of built-in device drivers
802 * requesting firmware in their ->probe() methods, if
803 * @gfp is GFP_KERNEL.
805 * - can't sleep at all if @gfp is GFP_ATOMIC.
808 request_firmware_nowait(
809 struct module *module, bool uevent,
810 const char *name, struct device *device, gfp_t gfp, void *context,
811 void (*cont)(const struct firmware *fw, void *context))
813 struct firmware_work *fw_work;
815 fw_work = kzalloc(sizeof(struct firmware_work), gfp);
819 fw_work->module = module;
820 fw_work->name = kstrdup_const(name, gfp);
821 if (!fw_work->name) {
825 fw_work->device = device;
826 fw_work->context = context;
827 fw_work->cont = cont;
828 fw_work->opt_flags = FW_OPT_NOWAIT |
829 (uevent ? FW_OPT_UEVENT : FW_OPT_USERHELPER);
831 if (!uevent && fw_cache_is_setup(device, name)) {
832 kfree_const(fw_work->name);
837 if (!try_module_get(module)) {
838 kfree_const(fw_work->name);
843 get_device(fw_work->device);
844 INIT_WORK(&fw_work->work, request_firmware_work_func);
845 schedule_work(&fw_work->work);
848 EXPORT_SYMBOL(request_firmware_nowait);
850 #ifdef CONFIG_PM_SLEEP
851 static ASYNC_DOMAIN_EXCLUSIVE(fw_cache_domain);
854 * cache_firmware() - cache one firmware image in kernel memory space
855 * @fw_name: the firmware image name
857 * Cache firmware in kernel memory so that drivers can use it when
858 * system isn't ready for them to request firmware image from userspace.
859 * Once it returns successfully, driver can use request_firmware or its
860 * nowait version to get the cached firmware without any interacting
863 * Return 0 if the firmware image has been cached successfully
864 * Return !0 otherwise
867 static int cache_firmware(const char *fw_name)
870 const struct firmware *fw;
872 pr_debug("%s: %s\n", __func__, fw_name);
874 ret = request_firmware(&fw, fw_name, NULL);
878 pr_debug("%s: %s ret=%d\n", __func__, fw_name, ret);
883 static struct fw_priv *lookup_fw_priv(const char *fw_name)
886 struct firmware_cache *fwc = &fw_cache;
888 spin_lock(&fwc->lock);
889 tmp = __lookup_fw_priv(fw_name);
890 spin_unlock(&fwc->lock);
896 * uncache_firmware() - remove one cached firmware image
897 * @fw_name: the firmware image name
899 * Uncache one firmware image which has been cached successfully
902 * Return 0 if the firmware cache has been removed successfully
903 * Return !0 otherwise
906 static int uncache_firmware(const char *fw_name)
908 struct fw_priv *fw_priv;
911 pr_debug("%s: %s\n", __func__, fw_name);
913 if (fw_get_builtin_firmware(&fw, fw_name, NULL, 0))
916 fw_priv = lookup_fw_priv(fw_name);
918 free_fw_priv(fw_priv);
925 static struct fw_cache_entry *alloc_fw_cache_entry(const char *name)
927 struct fw_cache_entry *fce;
929 fce = kzalloc(sizeof(*fce), GFP_ATOMIC);
933 fce->name = kstrdup_const(name, GFP_ATOMIC);
943 static int __fw_entry_found(const char *name)
945 struct firmware_cache *fwc = &fw_cache;
946 struct fw_cache_entry *fce;
948 list_for_each_entry(fce, &fwc->fw_names, list) {
949 if (!strcmp(fce->name, name))
955 static int fw_cache_piggyback_on_request(const char *name)
957 struct firmware_cache *fwc = &fw_cache;
958 struct fw_cache_entry *fce;
961 spin_lock(&fwc->name_lock);
962 if (__fw_entry_found(name))
965 fce = alloc_fw_cache_entry(name);
968 list_add(&fce->list, &fwc->fw_names);
969 pr_debug("%s: fw: %s\n", __func__, name);
972 spin_unlock(&fwc->name_lock);
976 static void free_fw_cache_entry(struct fw_cache_entry *fce)
978 kfree_const(fce->name);
982 static void __async_dev_cache_fw_image(void *fw_entry,
983 async_cookie_t cookie)
985 struct fw_cache_entry *fce = fw_entry;
986 struct firmware_cache *fwc = &fw_cache;
989 ret = cache_firmware(fce->name);
991 spin_lock(&fwc->name_lock);
992 list_del(&fce->list);
993 spin_unlock(&fwc->name_lock);
995 free_fw_cache_entry(fce);
999 /* called with dev->devres_lock held */
1000 static void dev_create_fw_entry(struct device *dev, void *res,
1003 struct fw_name_devm *fwn = res;
1004 const char *fw_name = fwn->name;
1005 struct list_head *head = data;
1006 struct fw_cache_entry *fce;
1008 fce = alloc_fw_cache_entry(fw_name);
1010 list_add(&fce->list, head);
1013 static int devm_name_match(struct device *dev, void *res,
1016 struct fw_name_devm *fwn = res;
1017 return (fwn->magic == (unsigned long)match_data);
1020 static void dev_cache_fw_image(struct device *dev, void *data)
1023 struct fw_cache_entry *fce;
1024 struct fw_cache_entry *fce_next;
1025 struct firmware_cache *fwc = &fw_cache;
1027 devres_for_each_res(dev, fw_name_devm_release,
1028 devm_name_match, &fw_cache,
1029 dev_create_fw_entry, &todo);
1031 list_for_each_entry_safe(fce, fce_next, &todo, list) {
1032 list_del(&fce->list);
1034 spin_lock(&fwc->name_lock);
1035 /* only one cache entry for one firmware */
1036 if (!__fw_entry_found(fce->name)) {
1037 list_add(&fce->list, &fwc->fw_names);
1039 free_fw_cache_entry(fce);
1042 spin_unlock(&fwc->name_lock);
1045 async_schedule_domain(__async_dev_cache_fw_image,
1051 static void __device_uncache_fw_images(void)
1053 struct firmware_cache *fwc = &fw_cache;
1054 struct fw_cache_entry *fce;
1056 spin_lock(&fwc->name_lock);
1057 while (!list_empty(&fwc->fw_names)) {
1058 fce = list_entry(fwc->fw_names.next,
1059 struct fw_cache_entry, list);
1060 list_del(&fce->list);
1061 spin_unlock(&fwc->name_lock);
1063 uncache_firmware(fce->name);
1064 free_fw_cache_entry(fce);
1066 spin_lock(&fwc->name_lock);
1068 spin_unlock(&fwc->name_lock);
1072 * device_cache_fw_images() - cache devices' firmware
1074 * If one device called request_firmware or its nowait version
1075 * successfully before, the firmware names are recored into the
1076 * device's devres link list, so device_cache_fw_images can call
1077 * cache_firmware() to cache these firmwares for the device,
1078 * then the device driver can load its firmwares easily at
1079 * time when system is not ready to complete loading firmware.
1081 static void device_cache_fw_images(void)
1083 struct firmware_cache *fwc = &fw_cache;
1086 pr_debug("%s\n", __func__);
1088 /* cancel uncache work */
1089 cancel_delayed_work_sync(&fwc->work);
1091 fw_fallback_set_cache_timeout();
1093 mutex_lock(&fw_lock);
1094 fwc->state = FW_LOADER_START_CACHE;
1095 dpm_for_each_dev(NULL, dev_cache_fw_image);
1096 mutex_unlock(&fw_lock);
1098 /* wait for completion of caching firmware for all devices */
1099 async_synchronize_full_domain(&fw_cache_domain);
1101 fw_fallback_set_default_timeout();
1105 * device_uncache_fw_images() - uncache devices' firmware
1107 * uncache all firmwares which have been cached successfully
1108 * by device_uncache_fw_images earlier
1110 static void device_uncache_fw_images(void)
1112 pr_debug("%s\n", __func__);
1113 __device_uncache_fw_images();
1116 static void device_uncache_fw_images_work(struct work_struct *work)
1118 device_uncache_fw_images();
1122 * device_uncache_fw_images_delay() - uncache devices firmwares
1123 * @delay: number of milliseconds to delay uncache device firmwares
1125 * uncache all devices's firmwares which has been cached successfully
1126 * by device_cache_fw_images after @delay milliseconds.
1128 static void device_uncache_fw_images_delay(unsigned long delay)
1130 queue_delayed_work(system_power_efficient_wq, &fw_cache.work,
1131 msecs_to_jiffies(delay));
1134 static int fw_pm_notify(struct notifier_block *notify_block,
1135 unsigned long mode, void *unused)
1138 case PM_HIBERNATION_PREPARE:
1139 case PM_SUSPEND_PREPARE:
1140 case PM_RESTORE_PREPARE:
1142 * kill pending fallback requests with a custom fallback
1143 * to avoid stalling suspend.
1145 kill_pending_fw_fallback_reqs(true);
1146 device_cache_fw_images();
1149 case PM_POST_SUSPEND:
1150 case PM_POST_HIBERNATION:
1151 case PM_POST_RESTORE:
1153 * In case that system sleep failed and syscore_suspend is
1156 mutex_lock(&fw_lock);
1157 fw_cache.state = FW_LOADER_NO_CACHE;
1158 mutex_unlock(&fw_lock);
1160 device_uncache_fw_images_delay(10 * MSEC_PER_SEC);
1167 /* stop caching firmware once syscore_suspend is reached */
1168 static int fw_suspend(void)
1170 fw_cache.state = FW_LOADER_NO_CACHE;
1174 static struct syscore_ops fw_syscore_ops = {
1175 .suspend = fw_suspend,
1178 static int __init register_fw_pm_ops(void)
1182 spin_lock_init(&fw_cache.name_lock);
1183 INIT_LIST_HEAD(&fw_cache.fw_names);
1185 INIT_DELAYED_WORK(&fw_cache.work,
1186 device_uncache_fw_images_work);
1188 fw_cache.pm_notify.notifier_call = fw_pm_notify;
1189 ret = register_pm_notifier(&fw_cache.pm_notify);
1193 register_syscore_ops(&fw_syscore_ops);
1198 static inline void unregister_fw_pm_ops(void)
1200 unregister_syscore_ops(&fw_syscore_ops);
1201 unregister_pm_notifier(&fw_cache.pm_notify);
1204 static int fw_cache_piggyback_on_request(const char *name)
1208 static inline int register_fw_pm_ops(void)
1212 static inline void unregister_fw_pm_ops(void)
1217 static void __init fw_cache_init(void)
1219 spin_lock_init(&fw_cache.lock);
1220 INIT_LIST_HEAD(&fw_cache.head);
1221 fw_cache.state = FW_LOADER_NO_CACHE;
1224 static int fw_shutdown_notify(struct notifier_block *unused1,
1225 unsigned long unused2, void *unused3)
1228 * Kill all pending fallback requests to avoid both stalling shutdown,
1229 * and avoid a deadlock with the usermode_lock.
1231 kill_pending_fw_fallback_reqs(false);
1236 static struct notifier_block fw_shutdown_nb = {
1237 .notifier_call = fw_shutdown_notify,
1240 static int __init firmware_class_init(void)
1244 /* No need to unfold these on exit */
1247 ret = register_fw_pm_ops();
1251 ret = register_reboot_notifier(&fw_shutdown_nb);
1255 return register_sysfs_loader();
1258 unregister_fw_pm_ops();
1262 static void __exit firmware_class_exit(void)
1264 unregister_fw_pm_ops();
1265 unregister_reboot_notifier(&fw_shutdown_nb);
1266 unregister_sysfs_loader();
1269 fs_initcall(firmware_class_init);
1270 module_exit(firmware_class_exit);