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