Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * main.c - Multi purpose firmware loading support
4 *
5 * Copyright (c) 2003 Manuel Estrada Sainz
6 *
7 * Please see Documentation/firmware_class/ for more information.
8 *
9 */
10
11#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
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>
29#include <linux/fs.h>
30#include <linux/async.h>
31#include <linux/pm.h>
32#include <linux/suspend.h>
33#include <linux/syscore_ops.h>
34#include <linux/reboot.h>
35#include <linux/security.h>
36
37#include <generated/utsrelease.h>
38
39#include "../base.h"
40#include "firmware.h"
41#include "fallback.h"
42
43MODULE_AUTHOR("Manuel Estrada Sainz");
44MODULE_DESCRIPTION("Multi purpose firmware loading support");
45MODULE_LICENSE("GPL");
46
47struct firmware_cache {
48 /* firmware_buf instance will be added into the below list */
49 spinlock_t lock;
50 struct list_head head;
51 int state;
52
53#ifdef CONFIG_PM_SLEEP
54 /*
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
58 * before.
59 */
60 spinlock_t name_lock;
61 struct list_head fw_names;
62
63 struct delayed_work work;
64
65 struct notifier_block pm_notify;
66#endif
67};
68
69struct fw_cache_entry {
70 struct list_head list;
71 const char *name;
72};
73
74struct fw_name_devm {
75 unsigned long magic;
76 const char *name;
77};
78
79static inline struct fw_priv *to_fw_priv(struct kref *ref)
80{
81 return container_of(ref, struct fw_priv, ref);
82}
83
84#define FW_LOADER_NO_CACHE 0
85#define FW_LOADER_START_CACHE 1
86
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 */
89DEFINE_MUTEX(fw_lock);
90
91static struct firmware_cache fw_cache;
92
93/* Builtin firmware support */
94
95#ifdef CONFIG_FW_LOADER
96
97extern struct builtin_fw __start_builtin_fw[];
98extern struct builtin_fw __end_builtin_fw[];
99
100static void fw_copy_to_prealloc_buf(struct firmware *fw,
101 void *buf, size_t size)
102{
103 if (!buf || size < fw->size)
104 return;
105 memcpy(buf, fw->data, fw->size);
106}
107
108static bool fw_get_builtin_firmware(struct firmware *fw, const char *name,
109 void *buf, size_t size)
110{
111 struct builtin_fw *b_fw;
112
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);
118
119 return true;
120 }
121 }
122
123 return false;
124}
125
126static bool fw_is_builtin_firmware(const struct firmware *fw)
127{
128 struct builtin_fw *b_fw;
129
130 for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++)
131 if (fw->data == b_fw->data)
132 return true;
133
134 return false;
135}
136
137#else /* Module case - no builtin firmware support */
138
139static inline bool fw_get_builtin_firmware(struct firmware *fw,
140 const char *name, void *buf,
141 size_t size)
142{
143 return false;
144}
145
146static inline bool fw_is_builtin_firmware(const struct firmware *fw)
147{
148 return false;
149}
150#endif
151
152static void fw_state_init(struct fw_priv *fw_priv)
153{
154 struct fw_state *fw_st = &fw_priv->fw_st;
155
156 init_completion(&fw_st->completion);
157 fw_st->status = FW_STATUS_UNKNOWN;
158}
159
160static inline int fw_state_wait(struct fw_priv *fw_priv)
161{
162 return __fw_state_wait_common(fw_priv, MAX_SCHEDULE_TIMEOUT);
163}
164
165static int fw_cache_piggyback_on_request(const char *name);
166
167static struct fw_priv *__allocate_fw_priv(const char *fw_name,
168 struct firmware_cache *fwc,
169 void *dbuf, size_t size)
170{
171 struct fw_priv *fw_priv;
172
173 fw_priv = kzalloc(sizeof(*fw_priv), GFP_ATOMIC);
174 if (!fw_priv)
175 return NULL;
176
177 fw_priv->fw_name = kstrdup_const(fw_name, GFP_ATOMIC);
178 if (!fw_priv->fw_name) {
179 kfree(fw_priv);
180 return NULL;
181 }
182
183 kref_init(&fw_priv->ref);
184 fw_priv->fwc = fwc;
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);
190#endif
191
192 pr_debug("%s: fw-%s fw_priv=%p\n", __func__, fw_name, fw_priv);
193
194 return fw_priv;
195}
196
197static struct fw_priv *__lookup_fw_priv(const char *fw_name)
198{
199 struct fw_priv *tmp;
200 struct firmware_cache *fwc = &fw_cache;
201
202 list_for_each_entry(tmp, &fwc->head, list)
203 if (!strcmp(tmp->fw_name, fw_name))
204 return tmp;
205 return NULL;
206}
207
208/* Returns 1 for batching firmware requests with the same name */
209static int alloc_lookup_fw_priv(const char *fw_name,
210 struct firmware_cache *fwc,
211 struct fw_priv **fw_priv, void *dbuf,
212 size_t size)
213{
214 struct fw_priv *tmp;
215
216 spin_lock(&fwc->lock);
217 tmp = __lookup_fw_priv(fw_name);
218 if (tmp) {
219 kref_get(&tmp->ref);
220 spin_unlock(&fwc->lock);
221 *fw_priv = tmp;
222 pr_debug("batched request - sharing the same struct fw_priv and lookup for multiple requests\n");
223 return 1;
224 }
225 tmp = __allocate_fw_priv(fw_name, fwc, dbuf, size);
226 if (tmp)
227 list_add(&tmp->list, &fwc->head);
228 spin_unlock(&fwc->lock);
229
230 *fw_priv = tmp;
231
232 return tmp ? 0 : -ENOMEM;
233}
234
235static void __free_fw_priv(struct kref *ref)
236 __releases(&fwc->lock)
237{
238 struct fw_priv *fw_priv = to_fw_priv(ref);
239 struct firmware_cache *fwc = fw_priv->fwc;
240
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);
244
245 list_del(&fw_priv->list);
246 spin_unlock(&fwc->lock);
247
248#ifdef CONFIG_FW_LOADER_USER_HELPER
249 if (fw_priv->is_paged_buf) {
250 int i;
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);
255 } else
256#endif
257 if (!fw_priv->allocated_size)
258 vfree(fw_priv->data);
259 kfree_const(fw_priv->fw_name);
260 kfree(fw_priv);
261}
262
263static void free_fw_priv(struct fw_priv *fw_priv)
264{
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);
269}
270
271/* direct firmware loading support */
272static char fw_path_para[256];
273static const char * const fw_path[] = {
274 fw_path_para,
275 "/lib/firmware/updates/" UTS_RELEASE,
276 "/lib/firmware/updates",
277 "/lib/firmware/" UTS_RELEASE,
278 "/lib/firmware"
279};
280
281/*
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.
285 */
286module_param_string(path, fw_path_para, sizeof(fw_path_para), 0644);
287MODULE_PARM_DESC(path, "customized firmware image search path with a higher priority than default path");
288
289static int
290fw_get_filesystem_firmware(struct device *device, struct fw_priv *fw_priv)
291{
292 loff_t size;
293 int i, len;
294 int rc = -ENOENT;
295 char *path;
296 enum kernel_read_file_id id = READING_FIRMWARE;
297 size_t msize = INT_MAX;
298
299 /* Already populated data member means we're loading into a buffer */
300 if (fw_priv->data) {
301 id = READING_FIRMWARE_PREALLOC_BUFFER;
302 msize = fw_priv->allocated_size;
303 }
304
305 path = __getname();
306 if (!path)
307 return -ENOMEM;
308
309 for (i = 0; i < ARRAY_SIZE(fw_path); i++) {
310 /* skip the unset customized path */
311 if (!fw_path[i][0])
312 continue;
313
314 len = snprintf(path, PATH_MAX, "%s/%s",
315 fw_path[i], fw_priv->fw_name);
316 if (len >= PATH_MAX) {
317 rc = -ENAMETOOLONG;
318 break;
319 }
320
321 fw_priv->size = 0;
322 rc = kernel_read_file_from_path(path, &fw_priv->data, &size,
323 msize, id);
324 if (rc) {
325 if (rc == -ENOENT)
326 dev_dbg(device, "loading %s failed with error %d\n",
327 path, rc);
328 else
329 dev_warn(device, "loading %s failed with error %d\n",
330 path, rc);
331 continue;
332 }
333 dev_dbg(device, "direct-loading %s\n", fw_priv->fw_name);
334 fw_priv->size = size;
335 fw_state_done(fw_priv);
336 break;
337 }
338 __putname(path);
339
340 return rc;
341}
342
343/* firmware holds the ownership of pages */
344static void firmware_free_data(const struct firmware *fw)
345{
346 /* Loaded directly? */
347 if (!fw->priv) {
348 vfree(fw->data);
349 return;
350 }
351 free_fw_priv(fw->priv);
352}
353
354/* store the pages buffer info firmware from buf */
355static void fw_set_page_data(struct fw_priv *fw_priv, struct firmware *fw)
356{
357 fw->priv = fw_priv;
358#ifdef CONFIG_FW_LOADER_USER_HELPER
359 fw->pages = fw_priv->pages;
360#endif
361 fw->size = fw_priv->size;
362 fw->data = fw_priv->data;
363
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);
367}
368
369#ifdef CONFIG_PM_SLEEP
370static void fw_name_devm_release(struct device *dev, void *res)
371{
372 struct fw_name_devm *fwn = res;
373
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);
378}
379
380static int fw_devm_match(struct device *dev, void *res,
381 void *match_data)
382{
383 struct fw_name_devm *fwn = res;
384
385 return (fwn->magic == (unsigned long)&fw_cache) &&
386 !strcmp(fwn->name, match_data);
387}
388
389static struct fw_name_devm *fw_find_devm_name(struct device *dev,
390 const char *name)
391{
392 struct fw_name_devm *fwn;
393
394 fwn = devres_find(dev, fw_name_devm_release,
395 fw_devm_match, (void *)name);
396 return fwn;
397}
398
399static bool fw_cache_is_setup(struct device *dev, const char *name)
400{
401 struct fw_name_devm *fwn;
402
403 fwn = fw_find_devm_name(dev, name);
404 if (fwn)
405 return true;
406
407 return false;
408}
409
410/* add firmware name into devres list */
411static int fw_add_devm_name(struct device *dev, const char *name)
412{
413 struct fw_name_devm *fwn;
414
415 if (fw_cache_is_setup(dev, name))
416 return 0;
417
418 fwn = devres_alloc(fw_name_devm_release, sizeof(struct fw_name_devm),
419 GFP_KERNEL);
420 if (!fwn)
421 return -ENOMEM;
422 fwn->name = kstrdup_const(name, GFP_KERNEL);
423 if (!fwn->name) {
424 devres_free(fwn);
425 return -ENOMEM;
426 }
427
428 fwn->magic = (unsigned long)&fw_cache;
429 devres_add(dev, fwn);
430
431 return 0;
432}
433#else
434static bool fw_cache_is_setup(struct device *dev, const char *name)
435{
436 return false;
437}
438
439static int fw_add_devm_name(struct device *dev, const char *name)
440{
441 return 0;
442}
443#endif
444
445int assign_fw(struct firmware *fw, struct device *device,
446 enum fw_opt opt_flags)
447{
448 struct fw_priv *fw_priv = fw->priv;
449 int ret;
450
451 mutex_lock(&fw_lock);
452 if (!fw_priv->size || fw_state_is_aborted(fw_priv)) {
453 mutex_unlock(&fw_lock);
454 return -ENOENT;
455 }
456
457 /*
458 * add firmware name into devres list so that we can auto cache
459 * and uncache firmware for device.
460 *
461 * device may has been deleted already, but the problem
462 * should be fixed in devres or driver core.
463 */
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);
468 if (ret) {
469 mutex_unlock(&fw_lock);
470 return ret;
471 }
472 }
473
474 /*
475 * After caching firmware image is started, let it piggyback
476 * on request firmware.
477 */
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);
482 }
483
484 /* pass the pages buffer to driver at the last minute */
485 fw_set_page_data(fw_priv, fw);
486 mutex_unlock(&fw_lock);
487 return 0;
488}
489
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
493 */
494static int
495_request_firmware_prepare(struct firmware **firmware_p, const char *name,
496 struct device *device, void *dbuf, size_t size)
497{
498 struct firmware *firmware;
499 struct fw_priv *fw_priv;
500 int ret;
501
502 *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
503 if (!firmware) {
504 dev_err(device, "%s: kmalloc(struct firmware) failed\n",
505 __func__);
506 return -ENOMEM;
507 }
508
509 if (fw_get_builtin_firmware(firmware, name, dbuf, size)) {
510 dev_dbg(device, "using built-in %s\n", name);
511 return 0; /* assigned */
512 }
513
514 ret = alloc_lookup_fw_priv(name, &fw_cache, &fw_priv, dbuf, size);
515
516 /*
517 * bind with 'priv' now to avoid warning in failure path
518 * of requesting firmware.
519 */
520 firmware->priv = fw_priv;
521
522 if (ret > 0) {
523 ret = fw_state_wait(fw_priv);
524 if (!ret) {
525 fw_set_page_data(fw_priv, firmware);
526 return 0; /* assigned */
527 }
528 }
529
530 if (ret < 0)
531 return ret;
532 return 1; /* need to load */
533}
534
535/*
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().
539 *
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.
543 */
544static void fw_abort_batch_reqs(struct firmware *fw)
545{
546 struct fw_priv *fw_priv;
547
548 /* Loaded directly? */
549 if (!fw || !fw->priv)
550 return;
551
552 fw_priv = fw->priv;
553 if (!fw_state_is_aborted(fw_priv))
554 fw_state_aborted(fw_priv);
555}
556
557/* called from request_firmware() and request_firmware_work_func() */
558static int
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)
562{
563 struct firmware *fw = NULL;
564 int ret;
565
566 if (!firmware_p)
567 return -EINVAL;
568
569 if (!name || name[0] == '\0') {
570 ret = -EINVAL;
571 goto out;
572 }
573
574 ret = _request_firmware_prepare(&fw, name, device, buf, size);
575 if (ret <= 0) /* error or already assigned */
576 goto out;
577
578 ret = fw_get_filesystem_firmware(device, fw->priv);
579 if (ret) {
580 if (!(opt_flags & FW_OPT_NO_WARN))
581 dev_warn(device,
582 "Direct firmware load for %s failed with error %d\n",
583 name, ret);
584 ret = firmware_fallback_sysfs(fw, name, device, opt_flags, ret);
585 } else
586 ret = assign_fw(fw, device, opt_flags);
587
588 out:
589 if (ret < 0) {
590 fw_abort_batch_reqs(fw);
591 release_firmware(fw);
592 fw = NULL;
593 }
594
595 *firmware_p = fw;
596 return ret;
597}
598
599/**
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
604 *
605 * @firmware_p will be used to return a firmware image by the name
606 * of @name for device @device.
607 *
608 * Should be called from user context where sleeping is allowed.
609 *
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.
613 *
614 * Caller must hold the reference count of @device.
615 *
616 * The function can be called safely inside device's suspend and
617 * resume callback.
618 **/
619int
620request_firmware(const struct firmware **firmware_p, const char *name,
621 struct device *device)
622{
623 int ret;
624
625 /* Need to pin this module until return */
626 __module_get(THIS_MODULE);
627 ret = _request_firmware(firmware_p, name, device, NULL, 0,
628 FW_OPT_UEVENT);
629 module_put(THIS_MODULE);
630 return ret;
631}
632EXPORT_SYMBOL(request_firmware);
633
634/**
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
639 *
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.
646 **/
647int firmware_request_nowarn(const struct firmware **firmware, const char *name,
648 struct device *device)
649{
650 int ret;
651
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);
657 return ret;
658}
659EXPORT_SYMBOL_GPL(firmware_request_nowarn);
660
661/**
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
666 *
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.
671 **/
672int request_firmware_direct(const struct firmware **firmware_p,
673 const char *name, struct device *device)
674{
675 int ret;
676
677 __module_get(THIS_MODULE);
678 ret = _request_firmware(firmware_p, name, device, NULL, 0,
679 FW_OPT_UEVENT | FW_OPT_NO_WARN |
680 FW_OPT_NOFALLBACK);
681 module_put(THIS_MODULE);
682 return ret;
683}
684EXPORT_SYMBOL_GPL(request_firmware_direct);
685
686/**
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
690 *
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.
697 **/
698int firmware_request_cache(struct device *device, const char *name)
699{
700 int ret;
701
702 mutex_lock(&fw_lock);
703 ret = fw_add_devm_name(device, name);
704 mutex_unlock(&fw_lock);
705
706 return ret;
707}
708EXPORT_SYMBOL_GPL(firmware_request_cache);
709
710/**
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
717 *
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.
722 *
723 * This function doesn't cache firmware either.
724 */
725int
726request_firmware_into_buf(const struct firmware **firmware_p, const char *name,
727 struct device *device, void *buf, size_t size)
728{
729 int ret;
730
731 if (fw_cache_is_setup(device, name))
732 return -EOPNOTSUPP;
733
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);
738 return ret;
739}
740EXPORT_SYMBOL(request_firmware_into_buf);
741
742/**
743 * release_firmware() - release the resource associated with a firmware image
744 * @fw: firmware resource to release
745 **/
746void release_firmware(const struct firmware *fw)
747{
748 if (fw) {
749 if (!fw_is_builtin_firmware(fw))
750 firmware_free_data(fw);
751 kfree(fw);
752 }
753}
754EXPORT_SYMBOL(release_firmware);
755
756/* Async support */
757struct firmware_work {
758 struct work_struct work;
759 struct module *module;
760 const char *name;
761 struct device *device;
762 void *context;
763 void (*cont)(const struct firmware *fw, void *context);
764 enum fw_opt opt_flags;
765};
766
767static void request_firmware_work_func(struct work_struct *work)
768{
769 struct firmware_work *fw_work;
770 const struct firmware *fw;
771
772 fw_work = container_of(work, struct firmware_work, work);
773
774 _request_firmware(&fw, fw_work->name, fw_work->device, NULL, 0,
775 fw_work->opt_flags);
776 fw_work->cont(fw, fw_work->context);
777 put_device(fw_work->device); /* taken in request_firmware_nowait() */
778
779 module_put(fw_work->module);
780 kfree_const(fw_work->name);
781 kfree(fw_work);
782}
783
784/**
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
795 * request is over.
796 *
797 * Caller must hold the reference count of @device.
798 *
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.
804 *
805 * - can't sleep at all if @gfp is GFP_ATOMIC.
806 **/
807int
808request_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))
812{
813 struct firmware_work *fw_work;
814
815 fw_work = kzalloc(sizeof(struct firmware_work), gfp);
816 if (!fw_work)
817 return -ENOMEM;
818
819 fw_work->module = module;
820 fw_work->name = kstrdup_const(name, gfp);
821 if (!fw_work->name) {
822 kfree(fw_work);
823 return -ENOMEM;
824 }
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);
830
831 if (!uevent && fw_cache_is_setup(device, name)) {
832 kfree_const(fw_work->name);
833 kfree(fw_work);
834 return -EOPNOTSUPP;
835 }
836
837 if (!try_module_get(module)) {
838 kfree_const(fw_work->name);
839 kfree(fw_work);
840 return -EFAULT;
841 }
842
843 get_device(fw_work->device);
844 INIT_WORK(&fw_work->work, request_firmware_work_func);
845 schedule_work(&fw_work->work);
846 return 0;
847}
848EXPORT_SYMBOL(request_firmware_nowait);
849
850#ifdef CONFIG_PM_SLEEP
851static ASYNC_DOMAIN_EXCLUSIVE(fw_cache_domain);
852
853/**
854 * cache_firmware() - cache one firmware image in kernel memory space
855 * @fw_name: the firmware image name
856 *
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
861 * with userspace
862 *
863 * Return 0 if the firmware image has been cached successfully
864 * Return !0 otherwise
865 *
866 */
867static int cache_firmware(const char *fw_name)
868{
869 int ret;
870 const struct firmware *fw;
871
872 pr_debug("%s: %s\n", __func__, fw_name);
873
874 ret = request_firmware(&fw, fw_name, NULL);
875 if (!ret)
876 kfree(fw);
877
878 pr_debug("%s: %s ret=%d\n", __func__, fw_name, ret);
879
880 return ret;
881}
882
883static struct fw_priv *lookup_fw_priv(const char *fw_name)
884{
885 struct fw_priv *tmp;
886 struct firmware_cache *fwc = &fw_cache;
887
888 spin_lock(&fwc->lock);
889 tmp = __lookup_fw_priv(fw_name);
890 spin_unlock(&fwc->lock);
891
892 return tmp;
893}
894
895/**
896 * uncache_firmware() - remove one cached firmware image
897 * @fw_name: the firmware image name
898 *
899 * Uncache one firmware image which has been cached successfully
900 * before.
901 *
902 * Return 0 if the firmware cache has been removed successfully
903 * Return !0 otherwise
904 *
905 */
906static int uncache_firmware(const char *fw_name)
907{
908 struct fw_priv *fw_priv;
909 struct firmware fw;
910
911 pr_debug("%s: %s\n", __func__, fw_name);
912
913 if (fw_get_builtin_firmware(&fw, fw_name, NULL, 0))
914 return 0;
915
916 fw_priv = lookup_fw_priv(fw_name);
917 if (fw_priv) {
918 free_fw_priv(fw_priv);
919 return 0;
920 }
921
922 return -EINVAL;
923}
924
925static struct fw_cache_entry *alloc_fw_cache_entry(const char *name)
926{
927 struct fw_cache_entry *fce;
928
929 fce = kzalloc(sizeof(*fce), GFP_ATOMIC);
930 if (!fce)
931 goto exit;
932
933 fce->name = kstrdup_const(name, GFP_ATOMIC);
934 if (!fce->name) {
935 kfree(fce);
936 fce = NULL;
937 goto exit;
938 }
939exit:
940 return fce;
941}
942
943static int __fw_entry_found(const char *name)
944{
945 struct firmware_cache *fwc = &fw_cache;
946 struct fw_cache_entry *fce;
947
948 list_for_each_entry(fce, &fwc->fw_names, list) {
949 if (!strcmp(fce->name, name))
950 return 1;
951 }
952 return 0;
953}
954
955static int fw_cache_piggyback_on_request(const char *name)
956{
957 struct firmware_cache *fwc = &fw_cache;
958 struct fw_cache_entry *fce;
959 int ret = 0;
960
961 spin_lock(&fwc->name_lock);
962 if (__fw_entry_found(name))
963 goto found;
964
965 fce = alloc_fw_cache_entry(name);
966 if (fce) {
967 ret = 1;
968 list_add(&fce->list, &fwc->fw_names);
969 pr_debug("%s: fw: %s\n", __func__, name);
970 }
971found:
972 spin_unlock(&fwc->name_lock);
973 return ret;
974}
975
976static void free_fw_cache_entry(struct fw_cache_entry *fce)
977{
978 kfree_const(fce->name);
979 kfree(fce);
980}
981
982static void __async_dev_cache_fw_image(void *fw_entry,
983 async_cookie_t cookie)
984{
985 struct fw_cache_entry *fce = fw_entry;
986 struct firmware_cache *fwc = &fw_cache;
987 int ret;
988
989 ret = cache_firmware(fce->name);
990 if (ret) {
991 spin_lock(&fwc->name_lock);
992 list_del(&fce->list);
993 spin_unlock(&fwc->name_lock);
994
995 free_fw_cache_entry(fce);
996 }
997}
998
999/* called with dev->devres_lock held */
1000static void dev_create_fw_entry(struct device *dev, void *res,
1001 void *data)
1002{
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;
1007
1008 fce = alloc_fw_cache_entry(fw_name);
1009 if (fce)
1010 list_add(&fce->list, head);
1011}
1012
1013static int devm_name_match(struct device *dev, void *res,
1014 void *match_data)
1015{
1016 struct fw_name_devm *fwn = res;
1017 return (fwn->magic == (unsigned long)match_data);
1018}
1019
1020static void dev_cache_fw_image(struct device *dev, void *data)
1021{
1022 LIST_HEAD(todo);
1023 struct fw_cache_entry *fce;
1024 struct fw_cache_entry *fce_next;
1025 struct firmware_cache *fwc = &fw_cache;
1026
1027 devres_for_each_res(dev, fw_name_devm_release,
1028 devm_name_match, &fw_cache,
1029 dev_create_fw_entry, &todo);
1030
1031 list_for_each_entry_safe(fce, fce_next, &todo, list) {
1032 list_del(&fce->list);
1033
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);
1038 } else {
1039 free_fw_cache_entry(fce);
1040 fce = NULL;
1041 }
1042 spin_unlock(&fwc->name_lock);
1043
1044 if (fce)
1045 async_schedule_domain(__async_dev_cache_fw_image,
1046 (void *)fce,
1047 &fw_cache_domain);
1048 }
1049}
1050
1051static void __device_uncache_fw_images(void)
1052{
1053 struct firmware_cache *fwc = &fw_cache;
1054 struct fw_cache_entry *fce;
1055
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);
1062
1063 uncache_firmware(fce->name);
1064 free_fw_cache_entry(fce);
1065
1066 spin_lock(&fwc->name_lock);
1067 }
1068 spin_unlock(&fwc->name_lock);
1069}
1070
1071/**
1072 * device_cache_fw_images() - cache devices' firmware
1073 *
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.
1080 */
1081static void device_cache_fw_images(void)
1082{
1083 struct firmware_cache *fwc = &fw_cache;
1084 DEFINE_WAIT(wait);
1085
1086 pr_debug("%s\n", __func__);
1087
1088 /* cancel uncache work */
1089 cancel_delayed_work_sync(&fwc->work);
1090
1091 fw_fallback_set_cache_timeout();
1092
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);
1097
1098 /* wait for completion of caching firmware for all devices */
1099 async_synchronize_full_domain(&fw_cache_domain);
1100
1101 fw_fallback_set_default_timeout();
1102}
1103
1104/**
1105 * device_uncache_fw_images() - uncache devices' firmware
1106 *
1107 * uncache all firmwares which have been cached successfully
1108 * by device_uncache_fw_images earlier
1109 */
1110static void device_uncache_fw_images(void)
1111{
1112 pr_debug("%s\n", __func__);
1113 __device_uncache_fw_images();
1114}
1115
1116static void device_uncache_fw_images_work(struct work_struct *work)
1117{
1118 device_uncache_fw_images();
1119}
1120
1121/**
1122 * device_uncache_fw_images_delay() - uncache devices firmwares
1123 * @delay: number of milliseconds to delay uncache device firmwares
1124 *
1125 * uncache all devices's firmwares which has been cached successfully
1126 * by device_cache_fw_images after @delay milliseconds.
1127 */
1128static void device_uncache_fw_images_delay(unsigned long delay)
1129{
1130 queue_delayed_work(system_power_efficient_wq, &fw_cache.work,
1131 msecs_to_jiffies(delay));
1132}
1133
1134static int fw_pm_notify(struct notifier_block *notify_block,
1135 unsigned long mode, void *unused)
1136{
1137 switch (mode) {
1138 case PM_HIBERNATION_PREPARE:
1139 case PM_SUSPEND_PREPARE:
1140 case PM_RESTORE_PREPARE:
1141 /*
1142 * kill pending fallback requests with a custom fallback
1143 * to avoid stalling suspend.
1144 */
1145 kill_pending_fw_fallback_reqs(true);
1146 device_cache_fw_images();
1147 break;
1148
1149 case PM_POST_SUSPEND:
1150 case PM_POST_HIBERNATION:
1151 case PM_POST_RESTORE:
1152 /*
1153 * In case that system sleep failed and syscore_suspend is
1154 * not called.
1155 */
1156 mutex_lock(&fw_lock);
1157 fw_cache.state = FW_LOADER_NO_CACHE;
1158 mutex_unlock(&fw_lock);
1159
1160 device_uncache_fw_images_delay(10 * MSEC_PER_SEC);
1161 break;
1162 }
1163
1164 return 0;
1165}
1166
1167/* stop caching firmware once syscore_suspend is reached */
1168static int fw_suspend(void)
1169{
1170 fw_cache.state = FW_LOADER_NO_CACHE;
1171 return 0;
1172}
1173
1174static struct syscore_ops fw_syscore_ops = {
1175 .suspend = fw_suspend,
1176};
1177
1178static int __init register_fw_pm_ops(void)
1179{
1180 int ret;
1181
1182 spin_lock_init(&fw_cache.name_lock);
1183 INIT_LIST_HEAD(&fw_cache.fw_names);
1184
1185 INIT_DELAYED_WORK(&fw_cache.work,
1186 device_uncache_fw_images_work);
1187
1188 fw_cache.pm_notify.notifier_call = fw_pm_notify;
1189 ret = register_pm_notifier(&fw_cache.pm_notify);
1190 if (ret)
1191 return ret;
1192
1193 register_syscore_ops(&fw_syscore_ops);
1194
1195 return ret;
1196}
1197
1198static inline void unregister_fw_pm_ops(void)
1199{
1200 unregister_syscore_ops(&fw_syscore_ops);
1201 unregister_pm_notifier(&fw_cache.pm_notify);
1202}
1203#else
1204static int fw_cache_piggyback_on_request(const char *name)
1205{
1206 return 0;
1207}
1208static inline int register_fw_pm_ops(void)
1209{
1210 return 0;
1211}
1212static inline void unregister_fw_pm_ops(void)
1213{
1214}
1215#endif
1216
1217static void __init fw_cache_init(void)
1218{
1219 spin_lock_init(&fw_cache.lock);
1220 INIT_LIST_HEAD(&fw_cache.head);
1221 fw_cache.state = FW_LOADER_NO_CACHE;
1222}
1223
1224static int fw_shutdown_notify(struct notifier_block *unused1,
1225 unsigned long unused2, void *unused3)
1226{
1227 /*
1228 * Kill all pending fallback requests to avoid both stalling shutdown,
1229 * and avoid a deadlock with the usermode_lock.
1230 */
1231 kill_pending_fw_fallback_reqs(false);
1232
1233 return NOTIFY_DONE;
1234}
1235
1236static struct notifier_block fw_shutdown_nb = {
1237 .notifier_call = fw_shutdown_notify,
1238};
1239
1240static int __init firmware_class_init(void)
1241{
1242 int ret;
1243
1244 /* No need to unfold these on exit */
1245 fw_cache_init();
1246
1247 ret = register_fw_pm_ops();
1248 if (ret)
1249 return ret;
1250
1251 ret = register_reboot_notifier(&fw_shutdown_nb);
1252 if (ret)
1253 goto out;
1254
1255 return register_sysfs_loader();
1256
1257out:
1258 unregister_fw_pm_ops();
1259 return ret;
1260}
1261
1262static void __exit firmware_class_exit(void)
1263{
1264 unregister_fw_pm_ops();
1265 unregister_reboot_notifier(&fw_shutdown_nb);
1266 unregister_sysfs_loader();
1267}
1268
1269fs_initcall(firmware_class_init);
1270module_exit(firmware_class_exit);