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/driver-api/firmware/ for more information.
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 #include <linux/capability.h>
14 #include <linux/device.h>
15 #include <linux/kernel_read_file.h>
16 #include <linux/module.h>
17 #include <linux/init.h>
18 #include <linux/timer.h>
19 #include <linux/vmalloc.h>
20 #include <linux/interrupt.h>
21 #include <linux/bitops.h>
22 #include <linux/mutex.h>
23 #include <linux/workqueue.h>
24 #include <linux/highmem.h>
25 #include <linux/firmware.h>
26 #include <linux/slab.h>
27 #include <linux/sched.h>
28 #include <linux/file.h>
29 #include <linux/list.h>
31 #include <linux/async.h>
33 #include <linux/suspend.h>
34 #include <linux/syscore_ops.h>
35 #include <linux/reboot.h>
36 #include <linux/security.h>
39 #include <generated/utsrelease.h>
45 MODULE_AUTHOR("Manuel Estrada Sainz");
46 MODULE_DESCRIPTION("Multi purpose firmware loading support");
47 MODULE_LICENSE("GPL");
49 struct firmware_cache {
50 /* firmware_buf instance will be added into the below list */
52 struct list_head head;
55 #ifdef CONFIG_FW_CACHE
57 * Names of firmware images which have been cached successfully
58 * will be added into the below list so that device uncache
59 * helper can trace which firmware images have been cached
63 struct list_head fw_names;
65 struct delayed_work work;
67 struct notifier_block pm_notify;
71 struct fw_cache_entry {
72 struct list_head list;
81 static inline struct fw_priv *to_fw_priv(struct kref *ref)
83 return container_of(ref, struct fw_priv, ref);
86 #define FW_LOADER_NO_CACHE 0
87 #define FW_LOADER_START_CACHE 1
89 /* fw_lock could be moved to 'struct fw_sysfs' but since it is just
90 * guarding for corner cases a global lock should be OK */
91 DEFINE_MUTEX(fw_lock);
93 static struct firmware_cache fw_cache;
95 /* Builtin firmware support */
97 #ifdef CONFIG_FW_LOADER
99 extern struct builtin_fw __start_builtin_fw[];
100 extern struct builtin_fw __end_builtin_fw[];
102 static void fw_copy_to_prealloc_buf(struct firmware *fw,
103 void *buf, size_t size)
105 if (!buf || size < fw->size)
107 memcpy(buf, fw->data, fw->size);
110 static bool fw_get_builtin_firmware(struct firmware *fw, const char *name,
111 void *buf, size_t size)
113 struct builtin_fw *b_fw;
115 for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++) {
116 if (strcmp(name, b_fw->name) == 0) {
117 fw->size = b_fw->size;
118 fw->data = b_fw->data;
119 fw_copy_to_prealloc_buf(fw, buf, size);
128 static bool fw_is_builtin_firmware(const struct firmware *fw)
130 struct builtin_fw *b_fw;
132 for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++)
133 if (fw->data == b_fw->data)
139 #else /* Module case - no builtin firmware support */
141 static inline bool fw_get_builtin_firmware(struct firmware *fw,
142 const char *name, void *buf,
148 static inline bool fw_is_builtin_firmware(const struct firmware *fw)
154 static void fw_state_init(struct fw_priv *fw_priv)
156 struct fw_state *fw_st = &fw_priv->fw_st;
158 init_completion(&fw_st->completion);
159 fw_st->status = FW_STATUS_UNKNOWN;
162 static inline int fw_state_wait(struct fw_priv *fw_priv)
164 return __fw_state_wait_common(fw_priv, MAX_SCHEDULE_TIMEOUT);
167 static int fw_cache_piggyback_on_request(const char *name);
169 static struct fw_priv *__allocate_fw_priv(const char *fw_name,
170 struct firmware_cache *fwc,
171 void *dbuf, size_t size)
173 struct fw_priv *fw_priv;
175 fw_priv = kzalloc(sizeof(*fw_priv), GFP_ATOMIC);
179 fw_priv->fw_name = kstrdup_const(fw_name, GFP_ATOMIC);
180 if (!fw_priv->fw_name) {
185 kref_init(&fw_priv->ref);
187 fw_priv->data = dbuf;
188 fw_priv->allocated_size = size;
189 fw_state_init(fw_priv);
190 #ifdef CONFIG_FW_LOADER_USER_HELPER
191 INIT_LIST_HEAD(&fw_priv->pending_list);
194 pr_debug("%s: fw-%s fw_priv=%p\n", __func__, fw_name, fw_priv);
199 static struct fw_priv *__lookup_fw_priv(const char *fw_name)
202 struct firmware_cache *fwc = &fw_cache;
204 list_for_each_entry(tmp, &fwc->head, list)
205 if (!strcmp(tmp->fw_name, fw_name))
210 /* Returns 1 for batching firmware requests with the same name */
211 static int alloc_lookup_fw_priv(const char *fw_name,
212 struct firmware_cache *fwc,
213 struct fw_priv **fw_priv, void *dbuf,
214 size_t size, u32 opt_flags)
218 spin_lock(&fwc->lock);
219 if (!(opt_flags & FW_OPT_NOCACHE)) {
220 tmp = __lookup_fw_priv(fw_name);
223 spin_unlock(&fwc->lock);
225 pr_debug("batched request - sharing the same struct fw_priv and lookup for multiple requests\n");
230 tmp = __allocate_fw_priv(fw_name, fwc, dbuf, size);
232 INIT_LIST_HEAD(&tmp->list);
233 if (!(opt_flags & FW_OPT_NOCACHE))
234 list_add(&tmp->list, &fwc->head);
236 spin_unlock(&fwc->lock);
240 return tmp ? 0 : -ENOMEM;
243 static void __free_fw_priv(struct kref *ref)
244 __releases(&fwc->lock)
246 struct fw_priv *fw_priv = to_fw_priv(ref);
247 struct firmware_cache *fwc = fw_priv->fwc;
249 pr_debug("%s: fw-%s fw_priv=%p data=%p size=%u\n",
250 __func__, fw_priv->fw_name, fw_priv, fw_priv->data,
251 (unsigned int)fw_priv->size);
253 list_del(&fw_priv->list);
254 spin_unlock(&fwc->lock);
256 if (fw_is_paged_buf(fw_priv))
257 fw_free_paged_buf(fw_priv);
258 else if (!fw_priv->allocated_size)
259 vfree(fw_priv->data);
261 kfree_const(fw_priv->fw_name);
265 static void free_fw_priv(struct fw_priv *fw_priv)
267 struct firmware_cache *fwc = fw_priv->fwc;
268 spin_lock(&fwc->lock);
269 if (!kref_put(&fw_priv->ref, __free_fw_priv))
270 spin_unlock(&fwc->lock);
273 #ifdef CONFIG_FW_LOADER_PAGED_BUF
274 bool fw_is_paged_buf(struct fw_priv *fw_priv)
276 return fw_priv->is_paged_buf;
279 void fw_free_paged_buf(struct fw_priv *fw_priv)
286 vunmap(fw_priv->data);
288 for (i = 0; i < fw_priv->nr_pages; i++)
289 __free_page(fw_priv->pages[i]);
290 kvfree(fw_priv->pages);
291 fw_priv->pages = NULL;
292 fw_priv->page_array_size = 0;
293 fw_priv->nr_pages = 0;
296 int fw_grow_paged_buf(struct fw_priv *fw_priv, int pages_needed)
298 /* If the array of pages is too small, grow it */
299 if (fw_priv->page_array_size < pages_needed) {
300 int new_array_size = max(pages_needed,
301 fw_priv->page_array_size * 2);
302 struct page **new_pages;
304 new_pages = kvmalloc_array(new_array_size, sizeof(void *),
308 memcpy(new_pages, fw_priv->pages,
309 fw_priv->page_array_size * sizeof(void *));
310 memset(&new_pages[fw_priv->page_array_size], 0, sizeof(void *) *
311 (new_array_size - fw_priv->page_array_size));
312 kvfree(fw_priv->pages);
313 fw_priv->pages = new_pages;
314 fw_priv->page_array_size = new_array_size;
317 while (fw_priv->nr_pages < pages_needed) {
318 fw_priv->pages[fw_priv->nr_pages] =
319 alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
321 if (!fw_priv->pages[fw_priv->nr_pages])
329 int fw_map_paged_buf(struct fw_priv *fw_priv)
331 /* one pages buffer should be mapped/unmapped only once */
335 vunmap(fw_priv->data);
336 fw_priv->data = vmap(fw_priv->pages, fw_priv->nr_pages, 0,
346 * XZ-compressed firmware support
348 #ifdef CONFIG_FW_LOADER_COMPRESS
349 /* show an error and return the standard error code */
350 static int fw_decompress_xz_error(struct device *dev, enum xz_ret xz_ret)
352 if (xz_ret != XZ_STREAM_END) {
353 dev_warn(dev, "xz decompression failed (xz_ret=%d)\n", xz_ret);
354 return xz_ret == XZ_MEM_ERROR ? -ENOMEM : -EINVAL;
359 /* single-shot decompression onto the pre-allocated buffer */
360 static int fw_decompress_xz_single(struct device *dev, struct fw_priv *fw_priv,
361 size_t in_size, const void *in_buffer)
363 struct xz_dec *xz_dec;
364 struct xz_buf xz_buf;
367 xz_dec = xz_dec_init(XZ_SINGLE, (u32)-1);
371 xz_buf.in_size = in_size;
372 xz_buf.in = in_buffer;
374 xz_buf.out_size = fw_priv->allocated_size;
375 xz_buf.out = fw_priv->data;
378 xz_ret = xz_dec_run(xz_dec, &xz_buf);
381 fw_priv->size = xz_buf.out_pos;
382 return fw_decompress_xz_error(dev, xz_ret);
385 /* decompression on paged buffer and map it */
386 static int fw_decompress_xz_pages(struct device *dev, struct fw_priv *fw_priv,
387 size_t in_size, const void *in_buffer)
389 struct xz_dec *xz_dec;
390 struct xz_buf xz_buf;
395 xz_dec = xz_dec_init(XZ_DYNALLOC, (u32)-1);
399 xz_buf.in_size = in_size;
400 xz_buf.in = in_buffer;
403 fw_priv->is_paged_buf = true;
406 if (fw_grow_paged_buf(fw_priv, fw_priv->nr_pages + 1)) {
411 /* decompress onto the new allocated page */
412 page = fw_priv->pages[fw_priv->nr_pages - 1];
413 xz_buf.out = kmap(page);
415 xz_buf.out_size = PAGE_SIZE;
416 xz_ret = xz_dec_run(xz_dec, &xz_buf);
418 fw_priv->size += xz_buf.out_pos;
419 /* partial decompression means either end or error */
420 if (xz_buf.out_pos != PAGE_SIZE)
422 } while (xz_ret == XZ_OK);
424 err = fw_decompress_xz_error(dev, xz_ret);
426 err = fw_map_paged_buf(fw_priv);
433 static int fw_decompress_xz(struct device *dev, struct fw_priv *fw_priv,
434 size_t in_size, const void *in_buffer)
436 /* if the buffer is pre-allocated, we can perform in single-shot mode */
438 return fw_decompress_xz_single(dev, fw_priv, in_size, in_buffer);
440 return fw_decompress_xz_pages(dev, fw_priv, in_size, in_buffer);
442 #endif /* CONFIG_FW_LOADER_COMPRESS */
444 /* direct firmware loading support */
445 static char fw_path_para[256];
446 static const char * const fw_path[] = {
448 "/lib/firmware/updates/" UTS_RELEASE,
449 "/lib/firmware/updates",
450 "/lib/firmware/" UTS_RELEASE,
455 * Typical usage is that passing 'firmware_class.path=$CUSTOMIZED_PATH'
456 * from kernel command line because firmware_class is generally built in
457 * kernel instead of module.
459 module_param_string(path, fw_path_para, sizeof(fw_path_para), 0644);
460 MODULE_PARM_DESC(path, "customized firmware image search path with a higher priority than default path");
463 fw_get_filesystem_firmware(struct device *device, struct fw_priv *fw_priv,
465 int (*decompress)(struct device *dev,
466 struct fw_priv *fw_priv,
468 const void *in_buffer))
474 size_t msize = INT_MAX;
477 /* Already populated data member means we're loading into a buffer */
478 if (!decompress && fw_priv->data) {
479 buffer = fw_priv->data;
480 msize = fw_priv->allocated_size;
487 for (i = 0; i < ARRAY_SIZE(fw_path); i++) {
488 /* skip the unset customized path */
492 len = snprintf(path, PATH_MAX, "%s/%s%s",
493 fw_path[i], fw_priv->fw_name, suffix);
494 if (len >= PATH_MAX) {
501 /* load firmware files from the mount namespace of init */
502 rc = kernel_read_file_from_path_initns(path, &buffer, msize,
507 dev_warn(device, "loading %s failed with error %d\n",
510 dev_dbg(device, "loading %s failed for no such file or directory.\n",
517 dev_dbg(device, "Loading firmware from %s\n", path);
519 dev_dbg(device, "f/w decompressing %s\n",
521 rc = decompress(device, fw_priv, size, buffer);
522 /* discard the superfluous original content */
526 fw_free_paged_buf(fw_priv);
530 dev_dbg(device, "direct-loading %s\n",
533 fw_priv->data = buffer;
534 fw_priv->size = size;
536 fw_state_done(fw_priv);
544 /* firmware holds the ownership of pages */
545 static void firmware_free_data(const struct firmware *fw)
547 /* Loaded directly? */
552 free_fw_priv(fw->priv);
555 /* store the pages buffer info firmware from buf */
556 static void fw_set_page_data(struct fw_priv *fw_priv, struct firmware *fw)
559 fw->size = fw_priv->size;
560 fw->data = fw_priv->data;
562 pr_debug("%s: fw-%s fw_priv=%p data=%p size=%u\n",
563 __func__, fw_priv->fw_name, fw_priv, fw_priv->data,
564 (unsigned int)fw_priv->size);
567 #ifdef CONFIG_FW_CACHE
568 static void fw_name_devm_release(struct device *dev, void *res)
570 struct fw_name_devm *fwn = res;
572 if (fwn->magic == (unsigned long)&fw_cache)
573 pr_debug("%s: fw_name-%s devm-%p released\n",
574 __func__, fwn->name, res);
575 kfree_const(fwn->name);
578 static int fw_devm_match(struct device *dev, void *res,
581 struct fw_name_devm *fwn = res;
583 return (fwn->magic == (unsigned long)&fw_cache) &&
584 !strcmp(fwn->name, match_data);
587 static struct fw_name_devm *fw_find_devm_name(struct device *dev,
590 struct fw_name_devm *fwn;
592 fwn = devres_find(dev, fw_name_devm_release,
593 fw_devm_match, (void *)name);
597 static bool fw_cache_is_setup(struct device *dev, const char *name)
599 struct fw_name_devm *fwn;
601 fwn = fw_find_devm_name(dev, name);
608 /* add firmware name into devres list */
609 static int fw_add_devm_name(struct device *dev, const char *name)
611 struct fw_name_devm *fwn;
613 if (fw_cache_is_setup(dev, name))
616 fwn = devres_alloc(fw_name_devm_release, sizeof(struct fw_name_devm),
620 fwn->name = kstrdup_const(name, GFP_KERNEL);
626 fwn->magic = (unsigned long)&fw_cache;
627 devres_add(dev, fwn);
632 static bool fw_cache_is_setup(struct device *dev, const char *name)
637 static int fw_add_devm_name(struct device *dev, const char *name)
643 int assign_fw(struct firmware *fw, struct device *device, u32 opt_flags)
645 struct fw_priv *fw_priv = fw->priv;
648 mutex_lock(&fw_lock);
649 if (!fw_priv->size || fw_state_is_aborted(fw_priv)) {
650 mutex_unlock(&fw_lock);
655 * add firmware name into devres list so that we can auto cache
656 * and uncache firmware for device.
658 * device may has been deleted already, but the problem
659 * should be fixed in devres or driver core.
661 /* don't cache firmware handled without uevent */
662 if (device && (opt_flags & FW_OPT_UEVENT) &&
663 !(opt_flags & FW_OPT_NOCACHE)) {
664 ret = fw_add_devm_name(device, fw_priv->fw_name);
666 mutex_unlock(&fw_lock);
672 * After caching firmware image is started, let it piggyback
673 * on request firmware.
675 if (!(opt_flags & FW_OPT_NOCACHE) &&
676 fw_priv->fwc->state == FW_LOADER_START_CACHE) {
677 if (fw_cache_piggyback_on_request(fw_priv->fw_name))
678 kref_get(&fw_priv->ref);
681 /* pass the pages buffer to driver at the last minute */
682 fw_set_page_data(fw_priv, fw);
683 mutex_unlock(&fw_lock);
687 /* prepare firmware and firmware_buf structs;
688 * return 0 if a firmware is already assigned, 1 if need to load one,
689 * or a negative error code
692 _request_firmware_prepare(struct firmware **firmware_p, const char *name,
693 struct device *device, void *dbuf, size_t size,
696 struct firmware *firmware;
697 struct fw_priv *fw_priv;
700 *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
702 dev_err(device, "%s: kmalloc(struct firmware) failed\n",
707 if (fw_get_builtin_firmware(firmware, name, dbuf, size)) {
708 dev_dbg(device, "using built-in %s\n", name);
709 return 0; /* assigned */
712 ret = alloc_lookup_fw_priv(name, &fw_cache, &fw_priv, dbuf, size,
716 * bind with 'priv' now to avoid warning in failure path
717 * of requesting firmware.
719 firmware->priv = fw_priv;
722 ret = fw_state_wait(fw_priv);
724 fw_set_page_data(fw_priv, firmware);
725 return 0; /* assigned */
731 return 1; /* need to load */
735 * Batched requests need only one wake, we need to do this step last due to the
736 * fallback mechanism. The buf is protected with kref_get(), and it won't be
737 * released until the last user calls release_firmware().
739 * Failed batched requests are possible as well, in such cases we just share
740 * the struct fw_priv and won't release it until all requests are woken
741 * and have gone through this same path.
743 static void fw_abort_batch_reqs(struct firmware *fw)
745 struct fw_priv *fw_priv;
747 /* Loaded directly? */
748 if (!fw || !fw->priv)
752 if (!fw_state_is_aborted(fw_priv))
753 fw_state_aborted(fw_priv);
756 /* called from request_firmware() and request_firmware_work_func() */
758 _request_firmware(const struct firmware **firmware_p, const char *name,
759 struct device *device, void *buf, size_t size,
762 struct firmware *fw = NULL;
768 if (!name || name[0] == '\0') {
773 ret = _request_firmware_prepare(&fw, name, device, buf, size,
775 if (ret <= 0) /* error or already assigned */
778 ret = fw_get_filesystem_firmware(device, fw->priv, "", NULL);
779 #ifdef CONFIG_FW_LOADER_COMPRESS
781 ret = fw_get_filesystem_firmware(device, fw->priv, ".xz",
786 ret = firmware_fallback_platform(fw->priv, opt_flags);
789 if (!(opt_flags & FW_OPT_NO_WARN))
791 "Direct firmware load for %s failed with error %d\n",
793 ret = firmware_fallback_sysfs(fw, name, device, opt_flags, ret);
795 ret = assign_fw(fw, device, opt_flags);
799 fw_abort_batch_reqs(fw);
800 release_firmware(fw);
809 * request_firmware() - send firmware request and wait for it
810 * @firmware_p: pointer to firmware image
811 * @name: name of firmware file
812 * @device: device for which firmware is being loaded
814 * @firmware_p will be used to return a firmware image by the name
815 * of @name for device @device.
817 * Should be called from user context where sleeping is allowed.
819 * @name will be used as $FIRMWARE in the uevent environment and
820 * should be distinctive enough not to be confused with any other
821 * firmware image for this or any other device.
823 * Caller must hold the reference count of @device.
825 * The function can be called safely inside device's suspend and
829 request_firmware(const struct firmware **firmware_p, const char *name,
830 struct device *device)
834 /* Need to pin this module until return */
835 __module_get(THIS_MODULE);
836 ret = _request_firmware(firmware_p, name, device, NULL, 0,
838 module_put(THIS_MODULE);
841 EXPORT_SYMBOL(request_firmware);
844 * firmware_request_nowarn() - request for an optional fw module
845 * @firmware: pointer to firmware image
846 * @name: name of firmware file
847 * @device: device for which firmware is being loaded
849 * This function is similar in behaviour to request_firmware(), except it
850 * doesn't produce warning messages when the file is not found. The sysfs
851 * fallback mechanism is enabled if direct filesystem lookup fails. However,
852 * failures to find the firmware file with it are still suppressed. It is
853 * therefore up to the driver to check for the return value of this call and to
854 * decide when to inform the users of errors.
856 int firmware_request_nowarn(const struct firmware **firmware, const char *name,
857 struct device *device)
861 /* Need to pin this module until return */
862 __module_get(THIS_MODULE);
863 ret = _request_firmware(firmware, name, device, NULL, 0,
864 FW_OPT_UEVENT | FW_OPT_NO_WARN);
865 module_put(THIS_MODULE);
868 EXPORT_SYMBOL_GPL(firmware_request_nowarn);
871 * request_firmware_direct() - load firmware directly without usermode helper
872 * @firmware_p: pointer to firmware image
873 * @name: name of firmware file
874 * @device: device for which firmware is being loaded
876 * This function works pretty much like request_firmware(), but this doesn't
877 * fall back to usermode helper even if the firmware couldn't be loaded
878 * directly from fs. Hence it's useful for loading optional firmwares, which
879 * aren't always present, without extra long timeouts of udev.
881 int request_firmware_direct(const struct firmware **firmware_p,
882 const char *name, struct device *device)
886 __module_get(THIS_MODULE);
887 ret = _request_firmware(firmware_p, name, device, NULL, 0,
888 FW_OPT_UEVENT | FW_OPT_NO_WARN |
889 FW_OPT_NOFALLBACK_SYSFS);
890 module_put(THIS_MODULE);
893 EXPORT_SYMBOL_GPL(request_firmware_direct);
896 * firmware_request_platform() - request firmware with platform-fw fallback
897 * @firmware: pointer to firmware image
898 * @name: name of firmware file
899 * @device: device for which firmware is being loaded
901 * This function is similar in behaviour to request_firmware, except that if
902 * direct filesystem lookup fails, it will fallback to looking for a copy of the
903 * requested firmware embedded in the platform's main (e.g. UEFI) firmware.
905 int firmware_request_platform(const struct firmware **firmware,
906 const char *name, struct device *device)
910 /* Need to pin this module until return */
911 __module_get(THIS_MODULE);
912 ret = _request_firmware(firmware, name, device, NULL, 0,
913 FW_OPT_UEVENT | FW_OPT_FALLBACK_PLATFORM);
914 module_put(THIS_MODULE);
917 EXPORT_SYMBOL_GPL(firmware_request_platform);
920 * firmware_request_cache() - cache firmware for suspend so resume can use it
921 * @name: name of firmware file
922 * @device: device for which firmware should be cached for
924 * There are some devices with an optimization that enables the device to not
925 * require loading firmware on system reboot. This optimization may still
926 * require the firmware present on resume from suspend. This routine can be
927 * used to ensure the firmware is present on resume from suspend in these
928 * situations. This helper is not compatible with drivers which use
929 * request_firmware_into_buf() or request_firmware_nowait() with no uevent set.
931 int firmware_request_cache(struct device *device, const char *name)
935 mutex_lock(&fw_lock);
936 ret = fw_add_devm_name(device, name);
937 mutex_unlock(&fw_lock);
941 EXPORT_SYMBOL_GPL(firmware_request_cache);
944 * request_firmware_into_buf() - load firmware into a previously allocated buffer
945 * @firmware_p: pointer to firmware image
946 * @name: name of firmware file
947 * @device: device for which firmware is being loaded and DMA region allocated
948 * @buf: address of buffer to load firmware into
949 * @size: size of buffer
951 * This function works pretty much like request_firmware(), but it doesn't
952 * allocate a buffer to hold the firmware data. Instead, the firmware
953 * is loaded directly into the buffer pointed to by @buf and the @firmware_p
954 * data member is pointed at @buf.
956 * This function doesn't cache firmware either.
959 request_firmware_into_buf(const struct firmware **firmware_p, const char *name,
960 struct device *device, void *buf, size_t size)
964 if (fw_cache_is_setup(device, name))
967 __module_get(THIS_MODULE);
968 ret = _request_firmware(firmware_p, name, device, buf, size,
969 FW_OPT_UEVENT | FW_OPT_NOCACHE);
970 module_put(THIS_MODULE);
973 EXPORT_SYMBOL(request_firmware_into_buf);
976 * release_firmware() - release the resource associated with a firmware image
977 * @fw: firmware resource to release
979 void release_firmware(const struct firmware *fw)
982 if (!fw_is_builtin_firmware(fw))
983 firmware_free_data(fw);
987 EXPORT_SYMBOL(release_firmware);
990 struct firmware_work {
991 struct work_struct work;
992 struct module *module;
994 struct device *device;
996 void (*cont)(const struct firmware *fw, void *context);
1000 static void request_firmware_work_func(struct work_struct *work)
1002 struct firmware_work *fw_work;
1003 const struct firmware *fw;
1005 fw_work = container_of(work, struct firmware_work, work);
1007 _request_firmware(&fw, fw_work->name, fw_work->device, NULL, 0,
1008 fw_work->opt_flags);
1009 fw_work->cont(fw, fw_work->context);
1010 put_device(fw_work->device); /* taken in request_firmware_nowait() */
1012 module_put(fw_work->module);
1013 kfree_const(fw_work->name);
1018 * request_firmware_nowait() - asynchronous version of request_firmware
1019 * @module: module requesting the firmware
1020 * @uevent: sends uevent to copy the firmware image if this flag
1021 * is non-zero else the firmware copy must be done manually.
1022 * @name: name of firmware file
1023 * @device: device for which firmware is being loaded
1024 * @gfp: allocation flags
1025 * @context: will be passed over to @cont, and
1026 * @fw may be %NULL if firmware request fails.
1027 * @cont: function will be called asynchronously when the firmware
1030 * Caller must hold the reference count of @device.
1032 * Asynchronous variant of request_firmware() for user contexts:
1033 * - sleep for as small periods as possible since it may
1034 * increase kernel boot time of built-in device drivers
1035 * requesting firmware in their ->probe() methods, if
1036 * @gfp is GFP_KERNEL.
1038 * - can't sleep at all if @gfp is GFP_ATOMIC.
1041 request_firmware_nowait(
1042 struct module *module, bool uevent,
1043 const char *name, struct device *device, gfp_t gfp, void *context,
1044 void (*cont)(const struct firmware *fw, void *context))
1046 struct firmware_work *fw_work;
1048 fw_work = kzalloc(sizeof(struct firmware_work), gfp);
1052 fw_work->module = module;
1053 fw_work->name = kstrdup_const(name, gfp);
1054 if (!fw_work->name) {
1058 fw_work->device = device;
1059 fw_work->context = context;
1060 fw_work->cont = cont;
1061 fw_work->opt_flags = FW_OPT_NOWAIT |
1062 (uevent ? FW_OPT_UEVENT : FW_OPT_USERHELPER);
1064 if (!uevent && fw_cache_is_setup(device, name)) {
1065 kfree_const(fw_work->name);
1070 if (!try_module_get(module)) {
1071 kfree_const(fw_work->name);
1076 get_device(fw_work->device);
1077 INIT_WORK(&fw_work->work, request_firmware_work_func);
1078 schedule_work(&fw_work->work);
1081 EXPORT_SYMBOL(request_firmware_nowait);
1083 #ifdef CONFIG_FW_CACHE
1084 static ASYNC_DOMAIN_EXCLUSIVE(fw_cache_domain);
1087 * cache_firmware() - cache one firmware image in kernel memory space
1088 * @fw_name: the firmware image name
1090 * Cache firmware in kernel memory so that drivers can use it when
1091 * system isn't ready for them to request firmware image from userspace.
1092 * Once it returns successfully, driver can use request_firmware or its
1093 * nowait version to get the cached firmware without any interacting
1096 * Return 0 if the firmware image has been cached successfully
1097 * Return !0 otherwise
1100 static int cache_firmware(const char *fw_name)
1103 const struct firmware *fw;
1105 pr_debug("%s: %s\n", __func__, fw_name);
1107 ret = request_firmware(&fw, fw_name, NULL);
1111 pr_debug("%s: %s ret=%d\n", __func__, fw_name, ret);
1116 static struct fw_priv *lookup_fw_priv(const char *fw_name)
1118 struct fw_priv *tmp;
1119 struct firmware_cache *fwc = &fw_cache;
1121 spin_lock(&fwc->lock);
1122 tmp = __lookup_fw_priv(fw_name);
1123 spin_unlock(&fwc->lock);
1129 * uncache_firmware() - remove one cached firmware image
1130 * @fw_name: the firmware image name
1132 * Uncache one firmware image which has been cached successfully
1135 * Return 0 if the firmware cache has been removed successfully
1136 * Return !0 otherwise
1139 static int uncache_firmware(const char *fw_name)
1141 struct fw_priv *fw_priv;
1144 pr_debug("%s: %s\n", __func__, fw_name);
1146 if (fw_get_builtin_firmware(&fw, fw_name, NULL, 0))
1149 fw_priv = lookup_fw_priv(fw_name);
1151 free_fw_priv(fw_priv);
1158 static struct fw_cache_entry *alloc_fw_cache_entry(const char *name)
1160 struct fw_cache_entry *fce;
1162 fce = kzalloc(sizeof(*fce), GFP_ATOMIC);
1166 fce->name = kstrdup_const(name, GFP_ATOMIC);
1176 static int __fw_entry_found(const char *name)
1178 struct firmware_cache *fwc = &fw_cache;
1179 struct fw_cache_entry *fce;
1181 list_for_each_entry(fce, &fwc->fw_names, list) {
1182 if (!strcmp(fce->name, name))
1188 static int fw_cache_piggyback_on_request(const char *name)
1190 struct firmware_cache *fwc = &fw_cache;
1191 struct fw_cache_entry *fce;
1194 spin_lock(&fwc->name_lock);
1195 if (__fw_entry_found(name))
1198 fce = alloc_fw_cache_entry(name);
1201 list_add(&fce->list, &fwc->fw_names);
1202 pr_debug("%s: fw: %s\n", __func__, name);
1205 spin_unlock(&fwc->name_lock);
1209 static void free_fw_cache_entry(struct fw_cache_entry *fce)
1211 kfree_const(fce->name);
1215 static void __async_dev_cache_fw_image(void *fw_entry,
1216 async_cookie_t cookie)
1218 struct fw_cache_entry *fce = fw_entry;
1219 struct firmware_cache *fwc = &fw_cache;
1222 ret = cache_firmware(fce->name);
1224 spin_lock(&fwc->name_lock);
1225 list_del(&fce->list);
1226 spin_unlock(&fwc->name_lock);
1228 free_fw_cache_entry(fce);
1232 /* called with dev->devres_lock held */
1233 static void dev_create_fw_entry(struct device *dev, void *res,
1236 struct fw_name_devm *fwn = res;
1237 const char *fw_name = fwn->name;
1238 struct list_head *head = data;
1239 struct fw_cache_entry *fce;
1241 fce = alloc_fw_cache_entry(fw_name);
1243 list_add(&fce->list, head);
1246 static int devm_name_match(struct device *dev, void *res,
1249 struct fw_name_devm *fwn = res;
1250 return (fwn->magic == (unsigned long)match_data);
1253 static void dev_cache_fw_image(struct device *dev, void *data)
1256 struct fw_cache_entry *fce;
1257 struct fw_cache_entry *fce_next;
1258 struct firmware_cache *fwc = &fw_cache;
1260 devres_for_each_res(dev, fw_name_devm_release,
1261 devm_name_match, &fw_cache,
1262 dev_create_fw_entry, &todo);
1264 list_for_each_entry_safe(fce, fce_next, &todo, list) {
1265 list_del(&fce->list);
1267 spin_lock(&fwc->name_lock);
1268 /* only one cache entry for one firmware */
1269 if (!__fw_entry_found(fce->name)) {
1270 list_add(&fce->list, &fwc->fw_names);
1272 free_fw_cache_entry(fce);
1275 spin_unlock(&fwc->name_lock);
1278 async_schedule_domain(__async_dev_cache_fw_image,
1284 static void __device_uncache_fw_images(void)
1286 struct firmware_cache *fwc = &fw_cache;
1287 struct fw_cache_entry *fce;
1289 spin_lock(&fwc->name_lock);
1290 while (!list_empty(&fwc->fw_names)) {
1291 fce = list_entry(fwc->fw_names.next,
1292 struct fw_cache_entry, list);
1293 list_del(&fce->list);
1294 spin_unlock(&fwc->name_lock);
1296 uncache_firmware(fce->name);
1297 free_fw_cache_entry(fce);
1299 spin_lock(&fwc->name_lock);
1301 spin_unlock(&fwc->name_lock);
1305 * device_cache_fw_images() - cache devices' firmware
1307 * If one device called request_firmware or its nowait version
1308 * successfully before, the firmware names are recored into the
1309 * device's devres link list, so device_cache_fw_images can call
1310 * cache_firmware() to cache these firmwares for the device,
1311 * then the device driver can load its firmwares easily at
1312 * time when system is not ready to complete loading firmware.
1314 static void device_cache_fw_images(void)
1316 struct firmware_cache *fwc = &fw_cache;
1319 pr_debug("%s\n", __func__);
1321 /* cancel uncache work */
1322 cancel_delayed_work_sync(&fwc->work);
1324 fw_fallback_set_cache_timeout();
1326 mutex_lock(&fw_lock);
1327 fwc->state = FW_LOADER_START_CACHE;
1328 dpm_for_each_dev(NULL, dev_cache_fw_image);
1329 mutex_unlock(&fw_lock);
1331 /* wait for completion of caching firmware for all devices */
1332 async_synchronize_full_domain(&fw_cache_domain);
1334 fw_fallback_set_default_timeout();
1338 * device_uncache_fw_images() - uncache devices' firmware
1340 * uncache all firmwares which have been cached successfully
1341 * by device_uncache_fw_images earlier
1343 static void device_uncache_fw_images(void)
1345 pr_debug("%s\n", __func__);
1346 __device_uncache_fw_images();
1349 static void device_uncache_fw_images_work(struct work_struct *work)
1351 device_uncache_fw_images();
1355 * device_uncache_fw_images_delay() - uncache devices firmwares
1356 * @delay: number of milliseconds to delay uncache device firmwares
1358 * uncache all devices's firmwares which has been cached successfully
1359 * by device_cache_fw_images after @delay milliseconds.
1361 static void device_uncache_fw_images_delay(unsigned long delay)
1363 queue_delayed_work(system_power_efficient_wq, &fw_cache.work,
1364 msecs_to_jiffies(delay));
1367 static int fw_pm_notify(struct notifier_block *notify_block,
1368 unsigned long mode, void *unused)
1371 case PM_HIBERNATION_PREPARE:
1372 case PM_SUSPEND_PREPARE:
1373 case PM_RESTORE_PREPARE:
1375 * kill pending fallback requests with a custom fallback
1376 * to avoid stalling suspend.
1378 kill_pending_fw_fallback_reqs(true);
1379 device_cache_fw_images();
1382 case PM_POST_SUSPEND:
1383 case PM_POST_HIBERNATION:
1384 case PM_POST_RESTORE:
1386 * In case that system sleep failed and syscore_suspend is
1389 mutex_lock(&fw_lock);
1390 fw_cache.state = FW_LOADER_NO_CACHE;
1391 mutex_unlock(&fw_lock);
1393 device_uncache_fw_images_delay(10 * MSEC_PER_SEC);
1400 /* stop caching firmware once syscore_suspend is reached */
1401 static int fw_suspend(void)
1403 fw_cache.state = FW_LOADER_NO_CACHE;
1407 static struct syscore_ops fw_syscore_ops = {
1408 .suspend = fw_suspend,
1411 static int __init register_fw_pm_ops(void)
1415 spin_lock_init(&fw_cache.name_lock);
1416 INIT_LIST_HEAD(&fw_cache.fw_names);
1418 INIT_DELAYED_WORK(&fw_cache.work,
1419 device_uncache_fw_images_work);
1421 fw_cache.pm_notify.notifier_call = fw_pm_notify;
1422 ret = register_pm_notifier(&fw_cache.pm_notify);
1426 register_syscore_ops(&fw_syscore_ops);
1431 static inline void unregister_fw_pm_ops(void)
1433 unregister_syscore_ops(&fw_syscore_ops);
1434 unregister_pm_notifier(&fw_cache.pm_notify);
1437 static int fw_cache_piggyback_on_request(const char *name)
1441 static inline int register_fw_pm_ops(void)
1445 static inline void unregister_fw_pm_ops(void)
1450 static void __init fw_cache_init(void)
1452 spin_lock_init(&fw_cache.lock);
1453 INIT_LIST_HEAD(&fw_cache.head);
1454 fw_cache.state = FW_LOADER_NO_CACHE;
1457 static int fw_shutdown_notify(struct notifier_block *unused1,
1458 unsigned long unused2, void *unused3)
1461 * Kill all pending fallback requests to avoid both stalling shutdown,
1462 * and avoid a deadlock with the usermode_lock.
1464 kill_pending_fw_fallback_reqs(false);
1469 static struct notifier_block fw_shutdown_nb = {
1470 .notifier_call = fw_shutdown_notify,
1473 static int __init firmware_class_init(void)
1477 /* No need to unfold these on exit */
1480 ret = register_fw_pm_ops();
1484 ret = register_reboot_notifier(&fw_shutdown_nb);
1488 return register_sysfs_loader();
1491 unregister_fw_pm_ops();
1495 static void __exit firmware_class_exit(void)
1497 unregister_fw_pm_ops();
1498 unregister_reboot_notifier(&fw_shutdown_nb);
1499 unregister_sysfs_loader();
1502 fs_initcall(firmware_class_init);
1503 module_exit(firmware_class_exit);