xref: /openbmc/linux/arch/x86/mm/ioremap.c (revision 43f6c078)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Re-map IO memory to kernel address space so that we can access it.
4  * This is needed for high PCI addresses that aren't mapped in the
5  * 640k-1MB IO memory area on PC's
6  *
7  * (C) Copyright 1995 1996 Linus Torvalds
8  */
9 
10 #include <linux/memblock.h>
11 #include <linux/init.h>
12 #include <linux/io.h>
13 #include <linux/ioport.h>
14 #include <linux/slab.h>
15 #include <linux/vmalloc.h>
16 #include <linux/mmiotrace.h>
17 #include <linux/mem_encrypt.h>
18 #include <linux/efi.h>
19 
20 #include <asm/set_memory.h>
21 #include <asm/e820/api.h>
22 #include <asm/fixmap.h>
23 #include <asm/pgtable.h>
24 #include <asm/tlbflush.h>
25 #include <asm/pgalloc.h>
26 #include <asm/pat.h>
27 #include <asm/setup.h>
28 
29 #include "physaddr.h"
30 
31 /*
32  * Descriptor controlling ioremap() behavior.
33  */
34 struct ioremap_desc {
35 	unsigned int flags;
36 };
37 
38 /*
39  * Fix up the linear direct mapping of the kernel to avoid cache attribute
40  * conflicts.
41  */
42 int ioremap_change_attr(unsigned long vaddr, unsigned long size,
43 			enum page_cache_mode pcm)
44 {
45 	unsigned long nrpages = size >> PAGE_SHIFT;
46 	int err;
47 
48 	switch (pcm) {
49 	case _PAGE_CACHE_MODE_UC:
50 	default:
51 		err = _set_memory_uc(vaddr, nrpages);
52 		break;
53 	case _PAGE_CACHE_MODE_WC:
54 		err = _set_memory_wc(vaddr, nrpages);
55 		break;
56 	case _PAGE_CACHE_MODE_WT:
57 		err = _set_memory_wt(vaddr, nrpages);
58 		break;
59 	case _PAGE_CACHE_MODE_WB:
60 		err = _set_memory_wb(vaddr, nrpages);
61 		break;
62 	}
63 
64 	return err;
65 }
66 
67 /* Does the range (or a subset of) contain normal RAM? */
68 static unsigned int __ioremap_check_ram(struct resource *res)
69 {
70 	unsigned long start_pfn, stop_pfn;
71 	unsigned long i;
72 
73 	if ((res->flags & IORESOURCE_SYSTEM_RAM) != IORESOURCE_SYSTEM_RAM)
74 		return 0;
75 
76 	start_pfn = (res->start + PAGE_SIZE - 1) >> PAGE_SHIFT;
77 	stop_pfn = (res->end + 1) >> PAGE_SHIFT;
78 	if (stop_pfn > start_pfn) {
79 		for (i = 0; i < (stop_pfn - start_pfn); ++i)
80 			if (pfn_valid(start_pfn + i) &&
81 			    !PageReserved(pfn_to_page(start_pfn + i)))
82 				return IORES_MAP_SYSTEM_RAM;
83 	}
84 
85 	return 0;
86 }
87 
88 /*
89  * In a SEV guest, NONE and RESERVED should not be mapped encrypted because
90  * there the whole memory is already encrypted.
91  */
92 static unsigned int __ioremap_check_encrypted(struct resource *res)
93 {
94 	if (!sev_active())
95 		return 0;
96 
97 	switch (res->desc) {
98 	case IORES_DESC_NONE:
99 	case IORES_DESC_RESERVED:
100 		break;
101 	default:
102 		return IORES_MAP_ENCRYPTED;
103 	}
104 
105 	return 0;
106 }
107 
108 static int __ioremap_collect_map_flags(struct resource *res, void *arg)
109 {
110 	struct ioremap_desc *desc = arg;
111 
112 	if (!(desc->flags & IORES_MAP_SYSTEM_RAM))
113 		desc->flags |= __ioremap_check_ram(res);
114 
115 	if (!(desc->flags & IORES_MAP_ENCRYPTED))
116 		desc->flags |= __ioremap_check_encrypted(res);
117 
118 	return ((desc->flags & (IORES_MAP_SYSTEM_RAM | IORES_MAP_ENCRYPTED)) ==
119 			       (IORES_MAP_SYSTEM_RAM | IORES_MAP_ENCRYPTED));
120 }
121 
122 /*
123  * To avoid multiple resource walks, this function walks resources marked as
124  * IORESOURCE_MEM and IORESOURCE_BUSY and looking for system RAM and/or a
125  * resource described not as IORES_DESC_NONE (e.g. IORES_DESC_ACPI_TABLES).
126  */
127 static void __ioremap_check_mem(resource_size_t addr, unsigned long size,
128 				struct ioremap_desc *desc)
129 {
130 	u64 start, end;
131 
132 	start = (u64)addr;
133 	end = start + size - 1;
134 	memset(desc, 0, sizeof(struct ioremap_desc));
135 
136 	walk_mem_res(start, end, desc, __ioremap_collect_map_flags);
137 }
138 
139 /*
140  * Remap an arbitrary physical address space into the kernel virtual
141  * address space. It transparently creates kernel huge I/O mapping when
142  * the physical address is aligned by a huge page size (1GB or 2MB) and
143  * the requested size is at least the huge page size.
144  *
145  * NOTE: MTRRs can override PAT memory types with a 4KB granularity.
146  * Therefore, the mapping code falls back to use a smaller page toward 4KB
147  * when a mapping range is covered by non-WB type of MTRRs.
148  *
149  * NOTE! We need to allow non-page-aligned mappings too: we will obviously
150  * have to convert them into an offset in a page-aligned mapping, but the
151  * caller shouldn't need to know that small detail.
152  */
153 static void __iomem *
154 __ioremap_caller(resource_size_t phys_addr, unsigned long size,
155 		 enum page_cache_mode pcm, void *caller, bool encrypted)
156 {
157 	unsigned long offset, vaddr;
158 	resource_size_t last_addr;
159 	const resource_size_t unaligned_phys_addr = phys_addr;
160 	const unsigned long unaligned_size = size;
161 	struct ioremap_desc io_desc;
162 	struct vm_struct *area;
163 	enum page_cache_mode new_pcm;
164 	pgprot_t prot;
165 	int retval;
166 	void __iomem *ret_addr;
167 
168 	/* Don't allow wraparound or zero size */
169 	last_addr = phys_addr + size - 1;
170 	if (!size || last_addr < phys_addr)
171 		return NULL;
172 
173 	if (!phys_addr_valid(phys_addr)) {
174 		printk(KERN_WARNING "ioremap: invalid physical address %llx\n",
175 		       (unsigned long long)phys_addr);
176 		WARN_ON_ONCE(1);
177 		return NULL;
178 	}
179 
180 	__ioremap_check_mem(phys_addr, size, &io_desc);
181 
182 	/*
183 	 * Don't allow anybody to remap normal RAM that we're using..
184 	 */
185 	if (io_desc.flags & IORES_MAP_SYSTEM_RAM) {
186 		WARN_ONCE(1, "ioremap on RAM at %pa - %pa\n",
187 			  &phys_addr, &last_addr);
188 		return NULL;
189 	}
190 
191 	/*
192 	 * Mappings have to be page-aligned
193 	 */
194 	offset = phys_addr & ~PAGE_MASK;
195 	phys_addr &= PHYSICAL_PAGE_MASK;
196 	size = PAGE_ALIGN(last_addr+1) - phys_addr;
197 
198 	retval = reserve_memtype(phys_addr, (u64)phys_addr + size,
199 						pcm, &new_pcm);
200 	if (retval) {
201 		printk(KERN_ERR "ioremap reserve_memtype failed %d\n", retval);
202 		return NULL;
203 	}
204 
205 	if (pcm != new_pcm) {
206 		if (!is_new_memtype_allowed(phys_addr, size, pcm, new_pcm)) {
207 			printk(KERN_ERR
208 		"ioremap error for 0x%llx-0x%llx, requested 0x%x, got 0x%x\n",
209 				(unsigned long long)phys_addr,
210 				(unsigned long long)(phys_addr + size),
211 				pcm, new_pcm);
212 			goto err_free_memtype;
213 		}
214 		pcm = new_pcm;
215 	}
216 
217 	/*
218 	 * If the page being mapped is in memory and SEV is active then
219 	 * make sure the memory encryption attribute is enabled in the
220 	 * resulting mapping.
221 	 */
222 	prot = PAGE_KERNEL_IO;
223 	if ((io_desc.flags & IORES_MAP_ENCRYPTED) || encrypted)
224 		prot = pgprot_encrypted(prot);
225 
226 	switch (pcm) {
227 	case _PAGE_CACHE_MODE_UC:
228 	default:
229 		prot = __pgprot(pgprot_val(prot) |
230 				cachemode2protval(_PAGE_CACHE_MODE_UC));
231 		break;
232 	case _PAGE_CACHE_MODE_UC_MINUS:
233 		prot = __pgprot(pgprot_val(prot) |
234 				cachemode2protval(_PAGE_CACHE_MODE_UC_MINUS));
235 		break;
236 	case _PAGE_CACHE_MODE_WC:
237 		prot = __pgprot(pgprot_val(prot) |
238 				cachemode2protval(_PAGE_CACHE_MODE_WC));
239 		break;
240 	case _PAGE_CACHE_MODE_WT:
241 		prot = __pgprot(pgprot_val(prot) |
242 				cachemode2protval(_PAGE_CACHE_MODE_WT));
243 		break;
244 	case _PAGE_CACHE_MODE_WB:
245 		break;
246 	}
247 
248 	/*
249 	 * Ok, go for it..
250 	 */
251 	area = get_vm_area_caller(size, VM_IOREMAP, caller);
252 	if (!area)
253 		goto err_free_memtype;
254 	area->phys_addr = phys_addr;
255 	vaddr = (unsigned long) area->addr;
256 
257 	if (kernel_map_sync_memtype(phys_addr, size, pcm))
258 		goto err_free_area;
259 
260 	if (ioremap_page_range(vaddr, vaddr + size, phys_addr, prot))
261 		goto err_free_area;
262 
263 	ret_addr = (void __iomem *) (vaddr + offset);
264 	mmiotrace_ioremap(unaligned_phys_addr, unaligned_size, ret_addr);
265 
266 	/*
267 	 * Check if the request spans more than any BAR in the iomem resource
268 	 * tree.
269 	 */
270 	if (iomem_map_sanity_check(unaligned_phys_addr, unaligned_size))
271 		pr_warn("caller %pS mapping multiple BARs\n", caller);
272 
273 	return ret_addr;
274 err_free_area:
275 	free_vm_area(area);
276 err_free_memtype:
277 	free_memtype(phys_addr, phys_addr + size);
278 	return NULL;
279 }
280 
281 /**
282  * ioremap_nocache     -   map bus memory into CPU space
283  * @phys_addr:    bus address of the memory
284  * @size:      size of the resource to map
285  *
286  * ioremap_nocache performs a platform specific sequence of operations to
287  * make bus memory CPU accessible via the readb/readw/readl/writeb/
288  * writew/writel functions and the other mmio helpers. The returned
289  * address is not guaranteed to be usable directly as a virtual
290  * address.
291  *
292  * This version of ioremap ensures that the memory is marked uncachable
293  * on the CPU as well as honouring existing caching rules from things like
294  * the PCI bus. Note that there are other caches and buffers on many
295  * busses. In particular driver authors should read up on PCI writes
296  *
297  * It's useful if some control registers are in such an area and
298  * write combining or read caching is not desirable:
299  *
300  * Must be freed with iounmap.
301  */
302 void __iomem *ioremap_nocache(resource_size_t phys_addr, unsigned long size)
303 {
304 	/*
305 	 * Ideally, this should be:
306 	 *	pat_enabled() ? _PAGE_CACHE_MODE_UC : _PAGE_CACHE_MODE_UC_MINUS;
307 	 *
308 	 * Till we fix all X drivers to use ioremap_wc(), we will use
309 	 * UC MINUS. Drivers that are certain they need or can already
310 	 * be converted over to strong UC can use ioremap_uc().
311 	 */
312 	enum page_cache_mode pcm = _PAGE_CACHE_MODE_UC_MINUS;
313 
314 	return __ioremap_caller(phys_addr, size, pcm,
315 				__builtin_return_address(0), false);
316 }
317 EXPORT_SYMBOL(ioremap_nocache);
318 
319 /**
320  * ioremap_uc     -   map bus memory into CPU space as strongly uncachable
321  * @phys_addr:    bus address of the memory
322  * @size:      size of the resource to map
323  *
324  * ioremap_uc performs a platform specific sequence of operations to
325  * make bus memory CPU accessible via the readb/readw/readl/writeb/
326  * writew/writel functions and the other mmio helpers. The returned
327  * address is not guaranteed to be usable directly as a virtual
328  * address.
329  *
330  * This version of ioremap ensures that the memory is marked with a strong
331  * preference as completely uncachable on the CPU when possible. For non-PAT
332  * systems this ends up setting page-attribute flags PCD=1, PWT=1. For PAT
333  * systems this will set the PAT entry for the pages as strong UC.  This call
334  * will honor existing caching rules from things like the PCI bus. Note that
335  * there are other caches and buffers on many busses. In particular driver
336  * authors should read up on PCI writes.
337  *
338  * It's useful if some control registers are in such an area and
339  * write combining or read caching is not desirable:
340  *
341  * Must be freed with iounmap.
342  */
343 void __iomem *ioremap_uc(resource_size_t phys_addr, unsigned long size)
344 {
345 	enum page_cache_mode pcm = _PAGE_CACHE_MODE_UC;
346 
347 	return __ioremap_caller(phys_addr, size, pcm,
348 				__builtin_return_address(0), false);
349 }
350 EXPORT_SYMBOL_GPL(ioremap_uc);
351 
352 /**
353  * ioremap_wc	-	map memory into CPU space write combined
354  * @phys_addr:	bus address of the memory
355  * @size:	size of the resource to map
356  *
357  * This version of ioremap ensures that the memory is marked write combining.
358  * Write combining allows faster writes to some hardware devices.
359  *
360  * Must be freed with iounmap.
361  */
362 void __iomem *ioremap_wc(resource_size_t phys_addr, unsigned long size)
363 {
364 	return __ioremap_caller(phys_addr, size, _PAGE_CACHE_MODE_WC,
365 					__builtin_return_address(0), false);
366 }
367 EXPORT_SYMBOL(ioremap_wc);
368 
369 /**
370  * ioremap_wt	-	map memory into CPU space write through
371  * @phys_addr:	bus address of the memory
372  * @size:	size of the resource to map
373  *
374  * This version of ioremap ensures that the memory is marked write through.
375  * Write through stores data into memory while keeping the cache up-to-date.
376  *
377  * Must be freed with iounmap.
378  */
379 void __iomem *ioremap_wt(resource_size_t phys_addr, unsigned long size)
380 {
381 	return __ioremap_caller(phys_addr, size, _PAGE_CACHE_MODE_WT,
382 					__builtin_return_address(0), false);
383 }
384 EXPORT_SYMBOL(ioremap_wt);
385 
386 void __iomem *ioremap_encrypted(resource_size_t phys_addr, unsigned long size)
387 {
388 	return __ioremap_caller(phys_addr, size, _PAGE_CACHE_MODE_WB,
389 				__builtin_return_address(0), true);
390 }
391 EXPORT_SYMBOL(ioremap_encrypted);
392 
393 void __iomem *ioremap_cache(resource_size_t phys_addr, unsigned long size)
394 {
395 	return __ioremap_caller(phys_addr, size, _PAGE_CACHE_MODE_WB,
396 				__builtin_return_address(0), false);
397 }
398 EXPORT_SYMBOL(ioremap_cache);
399 
400 void __iomem *ioremap_prot(resource_size_t phys_addr, unsigned long size,
401 				unsigned long prot_val)
402 {
403 	return __ioremap_caller(phys_addr, size,
404 				pgprot2cachemode(__pgprot(prot_val)),
405 				__builtin_return_address(0), false);
406 }
407 EXPORT_SYMBOL(ioremap_prot);
408 
409 /**
410  * iounmap - Free a IO remapping
411  * @addr: virtual address from ioremap_*
412  *
413  * Caller must ensure there is only one unmapping for the same pointer.
414  */
415 void iounmap(volatile void __iomem *addr)
416 {
417 	struct vm_struct *p, *o;
418 
419 	if ((void __force *)addr <= high_memory)
420 		return;
421 
422 	/*
423 	 * The PCI/ISA range special-casing was removed from __ioremap()
424 	 * so this check, in theory, can be removed. However, there are
425 	 * cases where iounmap() is called for addresses not obtained via
426 	 * ioremap() (vga16fb for example). Add a warning so that these
427 	 * cases can be caught and fixed.
428 	 */
429 	if ((void __force *)addr >= phys_to_virt(ISA_START_ADDRESS) &&
430 	    (void __force *)addr < phys_to_virt(ISA_END_ADDRESS)) {
431 		WARN(1, "iounmap() called for ISA range not obtained using ioremap()\n");
432 		return;
433 	}
434 
435 	mmiotrace_iounmap(addr);
436 
437 	addr = (volatile void __iomem *)
438 		(PAGE_MASK & (unsigned long __force)addr);
439 
440 	/* Use the vm area unlocked, assuming the caller
441 	   ensures there isn't another iounmap for the same address
442 	   in parallel. Reuse of the virtual address is prevented by
443 	   leaving it in the global lists until we're done with it.
444 	   cpa takes care of the direct mappings. */
445 	p = find_vm_area((void __force *)addr);
446 
447 	if (!p) {
448 		printk(KERN_ERR "iounmap: bad address %p\n", addr);
449 		dump_stack();
450 		return;
451 	}
452 
453 	free_memtype(p->phys_addr, p->phys_addr + get_vm_area_size(p));
454 
455 	/* Finally remove it */
456 	o = remove_vm_area((void __force *)addr);
457 	BUG_ON(p != o || o == NULL);
458 	kfree(p);
459 }
460 EXPORT_SYMBOL(iounmap);
461 
462 int __init arch_ioremap_pud_supported(void)
463 {
464 #ifdef CONFIG_X86_64
465 	return boot_cpu_has(X86_FEATURE_GBPAGES);
466 #else
467 	return 0;
468 #endif
469 }
470 
471 int __init arch_ioremap_pmd_supported(void)
472 {
473 	return boot_cpu_has(X86_FEATURE_PSE);
474 }
475 
476 /*
477  * Convert a physical pointer to a virtual kernel pointer for /dev/mem
478  * access
479  */
480 void *xlate_dev_mem_ptr(phys_addr_t phys)
481 {
482 	unsigned long start  = phys &  PAGE_MASK;
483 	unsigned long offset = phys & ~PAGE_MASK;
484 	void *vaddr;
485 
486 	/* memremap() maps if RAM, otherwise falls back to ioremap() */
487 	vaddr = memremap(start, PAGE_SIZE, MEMREMAP_WB);
488 
489 	/* Only add the offset on success and return NULL if memremap() failed */
490 	if (vaddr)
491 		vaddr += offset;
492 
493 	return vaddr;
494 }
495 
496 void unxlate_dev_mem_ptr(phys_addr_t phys, void *addr)
497 {
498 	memunmap((void *)((unsigned long)addr & PAGE_MASK));
499 }
500 
501 /*
502  * Examine the physical address to determine if it is an area of memory
503  * that should be mapped decrypted.  If the memory is not part of the
504  * kernel usable area it was accessed and created decrypted, so these
505  * areas should be mapped decrypted. And since the encryption key can
506  * change across reboots, persistent memory should also be mapped
507  * decrypted.
508  *
509  * If SEV is active, that implies that BIOS/UEFI also ran encrypted so
510  * only persistent memory should be mapped decrypted.
511  */
512 static bool memremap_should_map_decrypted(resource_size_t phys_addr,
513 					  unsigned long size)
514 {
515 	int is_pmem;
516 
517 	/*
518 	 * Check if the address is part of a persistent memory region.
519 	 * This check covers areas added by E820, EFI and ACPI.
520 	 */
521 	is_pmem = region_intersects(phys_addr, size, IORESOURCE_MEM,
522 				    IORES_DESC_PERSISTENT_MEMORY);
523 	if (is_pmem != REGION_DISJOINT)
524 		return true;
525 
526 	/*
527 	 * Check if the non-volatile attribute is set for an EFI
528 	 * reserved area.
529 	 */
530 	if (efi_enabled(EFI_BOOT)) {
531 		switch (efi_mem_type(phys_addr)) {
532 		case EFI_RESERVED_TYPE:
533 			if (efi_mem_attributes(phys_addr) & EFI_MEMORY_NV)
534 				return true;
535 			break;
536 		default:
537 			break;
538 		}
539 	}
540 
541 	/* Check if the address is outside kernel usable area */
542 	switch (e820__get_entry_type(phys_addr, phys_addr + size - 1)) {
543 	case E820_TYPE_RESERVED:
544 	case E820_TYPE_ACPI:
545 	case E820_TYPE_NVS:
546 	case E820_TYPE_UNUSABLE:
547 		/* For SEV, these areas are encrypted */
548 		if (sev_active())
549 			break;
550 		/* Fallthrough */
551 
552 	case E820_TYPE_PRAM:
553 		return true;
554 	default:
555 		break;
556 	}
557 
558 	return false;
559 }
560 
561 /*
562  * Examine the physical address to determine if it is EFI data. Check
563  * it against the boot params structure and EFI tables and memory types.
564  */
565 static bool memremap_is_efi_data(resource_size_t phys_addr,
566 				 unsigned long size)
567 {
568 	u64 paddr;
569 
570 	/* Check if the address is part of EFI boot/runtime data */
571 	if (!efi_enabled(EFI_BOOT))
572 		return false;
573 
574 	paddr = boot_params.efi_info.efi_memmap_hi;
575 	paddr <<= 32;
576 	paddr |= boot_params.efi_info.efi_memmap;
577 	if (phys_addr == paddr)
578 		return true;
579 
580 	paddr = boot_params.efi_info.efi_systab_hi;
581 	paddr <<= 32;
582 	paddr |= boot_params.efi_info.efi_systab;
583 	if (phys_addr == paddr)
584 		return true;
585 
586 	if (efi_is_table_address(phys_addr))
587 		return true;
588 
589 	switch (efi_mem_type(phys_addr)) {
590 	case EFI_BOOT_SERVICES_DATA:
591 	case EFI_RUNTIME_SERVICES_DATA:
592 		return true;
593 	default:
594 		break;
595 	}
596 
597 	return false;
598 }
599 
600 /*
601  * Examine the physical address to determine if it is boot data by checking
602  * it against the boot params setup_data chain.
603  */
604 static bool memremap_is_setup_data(resource_size_t phys_addr,
605 				   unsigned long size)
606 {
607 	struct setup_data *data;
608 	u64 paddr, paddr_next;
609 
610 	paddr = boot_params.hdr.setup_data;
611 	while (paddr) {
612 		unsigned int len;
613 
614 		if (phys_addr == paddr)
615 			return true;
616 
617 		data = memremap(paddr, sizeof(*data),
618 				MEMREMAP_WB | MEMREMAP_DEC);
619 
620 		paddr_next = data->next;
621 		len = data->len;
622 
623 		memunmap(data);
624 
625 		if ((phys_addr > paddr) && (phys_addr < (paddr + len)))
626 			return true;
627 
628 		paddr = paddr_next;
629 	}
630 
631 	return false;
632 }
633 
634 /*
635  * Examine the physical address to determine if it is boot data by checking
636  * it against the boot params setup_data chain (early boot version).
637  */
638 static bool __init early_memremap_is_setup_data(resource_size_t phys_addr,
639 						unsigned long size)
640 {
641 	struct setup_data *data;
642 	u64 paddr, paddr_next;
643 
644 	paddr = boot_params.hdr.setup_data;
645 	while (paddr) {
646 		unsigned int len;
647 
648 		if (phys_addr == paddr)
649 			return true;
650 
651 		data = early_memremap_decrypted(paddr, sizeof(*data));
652 
653 		paddr_next = data->next;
654 		len = data->len;
655 
656 		early_memunmap(data, sizeof(*data));
657 
658 		if ((phys_addr > paddr) && (phys_addr < (paddr + len)))
659 			return true;
660 
661 		paddr = paddr_next;
662 	}
663 
664 	return false;
665 }
666 
667 /*
668  * Architecture function to determine if RAM remap is allowed. By default, a
669  * RAM remap will map the data as encrypted. Determine if a RAM remap should
670  * not be done so that the data will be mapped decrypted.
671  */
672 bool arch_memremap_can_ram_remap(resource_size_t phys_addr, unsigned long size,
673 				 unsigned long flags)
674 {
675 	if (!mem_encrypt_active())
676 		return true;
677 
678 	if (flags & MEMREMAP_ENC)
679 		return true;
680 
681 	if (flags & MEMREMAP_DEC)
682 		return false;
683 
684 	if (sme_active()) {
685 		if (memremap_is_setup_data(phys_addr, size) ||
686 		    memremap_is_efi_data(phys_addr, size))
687 			return false;
688 	}
689 
690 	return !memremap_should_map_decrypted(phys_addr, size);
691 }
692 
693 /*
694  * Architecture override of __weak function to adjust the protection attributes
695  * used when remapping memory. By default, early_memremap() will map the data
696  * as encrypted. Determine if an encrypted mapping should not be done and set
697  * the appropriate protection attributes.
698  */
699 pgprot_t __init early_memremap_pgprot_adjust(resource_size_t phys_addr,
700 					     unsigned long size,
701 					     pgprot_t prot)
702 {
703 	bool encrypted_prot;
704 
705 	if (!mem_encrypt_active())
706 		return prot;
707 
708 	encrypted_prot = true;
709 
710 	if (sme_active()) {
711 		if (early_memremap_is_setup_data(phys_addr, size) ||
712 		    memremap_is_efi_data(phys_addr, size))
713 			encrypted_prot = false;
714 	}
715 
716 	if (encrypted_prot && memremap_should_map_decrypted(phys_addr, size))
717 		encrypted_prot = false;
718 
719 	return encrypted_prot ? pgprot_encrypted(prot)
720 			      : pgprot_decrypted(prot);
721 }
722 
723 bool phys_mem_access_encrypted(unsigned long phys_addr, unsigned long size)
724 {
725 	return arch_memremap_can_ram_remap(phys_addr, size, 0);
726 }
727 
728 #ifdef CONFIG_AMD_MEM_ENCRYPT
729 /* Remap memory with encryption */
730 void __init *early_memremap_encrypted(resource_size_t phys_addr,
731 				      unsigned long size)
732 {
733 	return early_memremap_prot(phys_addr, size, __PAGE_KERNEL_ENC);
734 }
735 
736 /*
737  * Remap memory with encryption and write-protected - cannot be called
738  * before pat_init() is called
739  */
740 void __init *early_memremap_encrypted_wp(resource_size_t phys_addr,
741 					 unsigned long size)
742 {
743 	/* Be sure the write-protect PAT entry is set for write-protect */
744 	if (__pte2cachemode_tbl[_PAGE_CACHE_MODE_WP] != _PAGE_CACHE_MODE_WP)
745 		return NULL;
746 
747 	return early_memremap_prot(phys_addr, size, __PAGE_KERNEL_ENC_WP);
748 }
749 
750 /* Remap memory without encryption */
751 void __init *early_memremap_decrypted(resource_size_t phys_addr,
752 				      unsigned long size)
753 {
754 	return early_memremap_prot(phys_addr, size, __PAGE_KERNEL_NOENC);
755 }
756 
757 /*
758  * Remap memory without encryption and write-protected - cannot be called
759  * before pat_init() is called
760  */
761 void __init *early_memremap_decrypted_wp(resource_size_t phys_addr,
762 					 unsigned long size)
763 {
764 	/* Be sure the write-protect PAT entry is set for write-protect */
765 	if (__pte2cachemode_tbl[_PAGE_CACHE_MODE_WP] != _PAGE_CACHE_MODE_WP)
766 		return NULL;
767 
768 	return early_memremap_prot(phys_addr, size, __PAGE_KERNEL_NOENC_WP);
769 }
770 #endif	/* CONFIG_AMD_MEM_ENCRYPT */
771 
772 static pte_t bm_pte[PAGE_SIZE/sizeof(pte_t)] __page_aligned_bss;
773 
774 static inline pmd_t * __init early_ioremap_pmd(unsigned long addr)
775 {
776 	/* Don't assume we're using swapper_pg_dir at this point */
777 	pgd_t *base = __va(read_cr3_pa());
778 	pgd_t *pgd = &base[pgd_index(addr)];
779 	p4d_t *p4d = p4d_offset(pgd, addr);
780 	pud_t *pud = pud_offset(p4d, addr);
781 	pmd_t *pmd = pmd_offset(pud, addr);
782 
783 	return pmd;
784 }
785 
786 static inline pte_t * __init early_ioremap_pte(unsigned long addr)
787 {
788 	return &bm_pte[pte_index(addr)];
789 }
790 
791 bool __init is_early_ioremap_ptep(pte_t *ptep)
792 {
793 	return ptep >= &bm_pte[0] && ptep < &bm_pte[PAGE_SIZE/sizeof(pte_t)];
794 }
795 
796 void __init early_ioremap_init(void)
797 {
798 	pmd_t *pmd;
799 
800 #ifdef CONFIG_X86_64
801 	BUILD_BUG_ON((fix_to_virt(0) + PAGE_SIZE) & ((1 << PMD_SHIFT) - 1));
802 #else
803 	WARN_ON((fix_to_virt(0) + PAGE_SIZE) & ((1 << PMD_SHIFT) - 1));
804 #endif
805 
806 	early_ioremap_setup();
807 
808 	pmd = early_ioremap_pmd(fix_to_virt(FIX_BTMAP_BEGIN));
809 	memset(bm_pte, 0, sizeof(bm_pte));
810 	pmd_populate_kernel(&init_mm, pmd, bm_pte);
811 
812 	/*
813 	 * The boot-ioremap range spans multiple pmds, for which
814 	 * we are not prepared:
815 	 */
816 #define __FIXADDR_TOP (-PAGE_SIZE)
817 	BUILD_BUG_ON((__fix_to_virt(FIX_BTMAP_BEGIN) >> PMD_SHIFT)
818 		     != (__fix_to_virt(FIX_BTMAP_END) >> PMD_SHIFT));
819 #undef __FIXADDR_TOP
820 	if (pmd != early_ioremap_pmd(fix_to_virt(FIX_BTMAP_END))) {
821 		WARN_ON(1);
822 		printk(KERN_WARNING "pmd %p != %p\n",
823 		       pmd, early_ioremap_pmd(fix_to_virt(FIX_BTMAP_END)));
824 		printk(KERN_WARNING "fix_to_virt(FIX_BTMAP_BEGIN): %08lx\n",
825 			fix_to_virt(FIX_BTMAP_BEGIN));
826 		printk(KERN_WARNING "fix_to_virt(FIX_BTMAP_END):   %08lx\n",
827 			fix_to_virt(FIX_BTMAP_END));
828 
829 		printk(KERN_WARNING "FIX_BTMAP_END:       %d\n", FIX_BTMAP_END);
830 		printk(KERN_WARNING "FIX_BTMAP_BEGIN:     %d\n",
831 		       FIX_BTMAP_BEGIN);
832 	}
833 }
834 
835 void __init __early_set_fixmap(enum fixed_addresses idx,
836 			       phys_addr_t phys, pgprot_t flags)
837 {
838 	unsigned long addr = __fix_to_virt(idx);
839 	pte_t *pte;
840 
841 	if (idx >= __end_of_fixed_addresses) {
842 		BUG();
843 		return;
844 	}
845 	pte = early_ioremap_pte(addr);
846 
847 	/* Sanitize 'prot' against any unsupported bits: */
848 	pgprot_val(flags) &= __supported_pte_mask;
849 
850 	if (pgprot_val(flags))
851 		set_pte(pte, pfn_pte(phys >> PAGE_SHIFT, flags));
852 	else
853 		pte_clear(&init_mm, addr, pte);
854 	__flush_tlb_one_kernel(addr);
855 }
856