xref: /openbmc/linux/arch/x86/mm/ioremap.c (revision 23966841)
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_p4d_supported(void)
463 {
464 	return 0;
465 }
466 
467 int __init arch_ioremap_pud_supported(void)
468 {
469 #ifdef CONFIG_X86_64
470 	return boot_cpu_has(X86_FEATURE_GBPAGES);
471 #else
472 	return 0;
473 #endif
474 }
475 
476 int __init arch_ioremap_pmd_supported(void)
477 {
478 	return boot_cpu_has(X86_FEATURE_PSE);
479 }
480 
481 /*
482  * Convert a physical pointer to a virtual kernel pointer for /dev/mem
483  * access
484  */
485 void *xlate_dev_mem_ptr(phys_addr_t phys)
486 {
487 	unsigned long start  = phys &  PAGE_MASK;
488 	unsigned long offset = phys & ~PAGE_MASK;
489 	void *vaddr;
490 
491 	/* memremap() maps if RAM, otherwise falls back to ioremap() */
492 	vaddr = memremap(start, PAGE_SIZE, MEMREMAP_WB);
493 
494 	/* Only add the offset on success and return NULL if memremap() failed */
495 	if (vaddr)
496 		vaddr += offset;
497 
498 	return vaddr;
499 }
500 
501 void unxlate_dev_mem_ptr(phys_addr_t phys, void *addr)
502 {
503 	memunmap((void *)((unsigned long)addr & PAGE_MASK));
504 }
505 
506 /*
507  * Examine the physical address to determine if it is an area of memory
508  * that should be mapped decrypted.  If the memory is not part of the
509  * kernel usable area it was accessed and created decrypted, so these
510  * areas should be mapped decrypted. And since the encryption key can
511  * change across reboots, persistent memory should also be mapped
512  * decrypted.
513  *
514  * If SEV is active, that implies that BIOS/UEFI also ran encrypted so
515  * only persistent memory should be mapped decrypted.
516  */
517 static bool memremap_should_map_decrypted(resource_size_t phys_addr,
518 					  unsigned long size)
519 {
520 	int is_pmem;
521 
522 	/*
523 	 * Check if the address is part of a persistent memory region.
524 	 * This check covers areas added by E820, EFI and ACPI.
525 	 */
526 	is_pmem = region_intersects(phys_addr, size, IORESOURCE_MEM,
527 				    IORES_DESC_PERSISTENT_MEMORY);
528 	if (is_pmem != REGION_DISJOINT)
529 		return true;
530 
531 	/*
532 	 * Check if the non-volatile attribute is set for an EFI
533 	 * reserved area.
534 	 */
535 	if (efi_enabled(EFI_BOOT)) {
536 		switch (efi_mem_type(phys_addr)) {
537 		case EFI_RESERVED_TYPE:
538 			if (efi_mem_attributes(phys_addr) & EFI_MEMORY_NV)
539 				return true;
540 			break;
541 		default:
542 			break;
543 		}
544 	}
545 
546 	/* Check if the address is outside kernel usable area */
547 	switch (e820__get_entry_type(phys_addr, phys_addr + size - 1)) {
548 	case E820_TYPE_RESERVED:
549 	case E820_TYPE_ACPI:
550 	case E820_TYPE_NVS:
551 	case E820_TYPE_UNUSABLE:
552 		/* For SEV, these areas are encrypted */
553 		if (sev_active())
554 			break;
555 		/* Fallthrough */
556 
557 	case E820_TYPE_PRAM:
558 		return true;
559 	default:
560 		break;
561 	}
562 
563 	return false;
564 }
565 
566 /*
567  * Examine the physical address to determine if it is EFI data. Check
568  * it against the boot params structure and EFI tables and memory types.
569  */
570 static bool memremap_is_efi_data(resource_size_t phys_addr,
571 				 unsigned long size)
572 {
573 	u64 paddr;
574 
575 	/* Check if the address is part of EFI boot/runtime data */
576 	if (!efi_enabled(EFI_BOOT))
577 		return false;
578 
579 	paddr = boot_params.efi_info.efi_memmap_hi;
580 	paddr <<= 32;
581 	paddr |= boot_params.efi_info.efi_memmap;
582 	if (phys_addr == paddr)
583 		return true;
584 
585 	paddr = boot_params.efi_info.efi_systab_hi;
586 	paddr <<= 32;
587 	paddr |= boot_params.efi_info.efi_systab;
588 	if (phys_addr == paddr)
589 		return true;
590 
591 	if (efi_is_table_address(phys_addr))
592 		return true;
593 
594 	switch (efi_mem_type(phys_addr)) {
595 	case EFI_BOOT_SERVICES_DATA:
596 	case EFI_RUNTIME_SERVICES_DATA:
597 		return true;
598 	default:
599 		break;
600 	}
601 
602 	return false;
603 }
604 
605 /*
606  * Examine the physical address to determine if it is boot data by checking
607  * it against the boot params setup_data chain.
608  */
609 static bool memremap_is_setup_data(resource_size_t phys_addr,
610 				   unsigned long size)
611 {
612 	struct setup_data *data;
613 	u64 paddr, paddr_next;
614 
615 	paddr = boot_params.hdr.setup_data;
616 	while (paddr) {
617 		unsigned int len;
618 
619 		if (phys_addr == paddr)
620 			return true;
621 
622 		data = memremap(paddr, sizeof(*data),
623 				MEMREMAP_WB | MEMREMAP_DEC);
624 
625 		paddr_next = data->next;
626 		len = data->len;
627 
628 		memunmap(data);
629 
630 		if ((phys_addr > paddr) && (phys_addr < (paddr + len)))
631 			return true;
632 
633 		paddr = paddr_next;
634 	}
635 
636 	return false;
637 }
638 
639 /*
640  * Examine the physical address to determine if it is boot data by checking
641  * it against the boot params setup_data chain (early boot version).
642  */
643 static bool __init early_memremap_is_setup_data(resource_size_t phys_addr,
644 						unsigned long size)
645 {
646 	struct setup_data *data;
647 	u64 paddr, paddr_next;
648 
649 	paddr = boot_params.hdr.setup_data;
650 	while (paddr) {
651 		unsigned int len;
652 
653 		if (phys_addr == paddr)
654 			return true;
655 
656 		data = early_memremap_decrypted(paddr, sizeof(*data));
657 
658 		paddr_next = data->next;
659 		len = data->len;
660 
661 		early_memunmap(data, sizeof(*data));
662 
663 		if ((phys_addr > paddr) && (phys_addr < (paddr + len)))
664 			return true;
665 
666 		paddr = paddr_next;
667 	}
668 
669 	return false;
670 }
671 
672 /*
673  * Architecture function to determine if RAM remap is allowed. By default, a
674  * RAM remap will map the data as encrypted. Determine if a RAM remap should
675  * not be done so that the data will be mapped decrypted.
676  */
677 bool arch_memremap_can_ram_remap(resource_size_t phys_addr, unsigned long size,
678 				 unsigned long flags)
679 {
680 	if (!mem_encrypt_active())
681 		return true;
682 
683 	if (flags & MEMREMAP_ENC)
684 		return true;
685 
686 	if (flags & MEMREMAP_DEC)
687 		return false;
688 
689 	if (sme_active()) {
690 		if (memremap_is_setup_data(phys_addr, size) ||
691 		    memremap_is_efi_data(phys_addr, size))
692 			return false;
693 	}
694 
695 	return !memremap_should_map_decrypted(phys_addr, size);
696 }
697 
698 /*
699  * Architecture override of __weak function to adjust the protection attributes
700  * used when remapping memory. By default, early_memremap() will map the data
701  * as encrypted. Determine if an encrypted mapping should not be done and set
702  * the appropriate protection attributes.
703  */
704 pgprot_t __init early_memremap_pgprot_adjust(resource_size_t phys_addr,
705 					     unsigned long size,
706 					     pgprot_t prot)
707 {
708 	bool encrypted_prot;
709 
710 	if (!mem_encrypt_active())
711 		return prot;
712 
713 	encrypted_prot = true;
714 
715 	if (sme_active()) {
716 		if (early_memremap_is_setup_data(phys_addr, size) ||
717 		    memremap_is_efi_data(phys_addr, size))
718 			encrypted_prot = false;
719 	}
720 
721 	if (encrypted_prot && memremap_should_map_decrypted(phys_addr, size))
722 		encrypted_prot = false;
723 
724 	return encrypted_prot ? pgprot_encrypted(prot)
725 			      : pgprot_decrypted(prot);
726 }
727 
728 bool phys_mem_access_encrypted(unsigned long phys_addr, unsigned long size)
729 {
730 	return arch_memremap_can_ram_remap(phys_addr, size, 0);
731 }
732 
733 #ifdef CONFIG_AMD_MEM_ENCRYPT
734 /* Remap memory with encryption */
735 void __init *early_memremap_encrypted(resource_size_t phys_addr,
736 				      unsigned long size)
737 {
738 	return early_memremap_prot(phys_addr, size, __PAGE_KERNEL_ENC);
739 }
740 
741 /*
742  * Remap memory with encryption and write-protected - cannot be called
743  * before pat_init() is called
744  */
745 void __init *early_memremap_encrypted_wp(resource_size_t phys_addr,
746 					 unsigned long size)
747 {
748 	/* Be sure the write-protect PAT entry is set for write-protect */
749 	if (__pte2cachemode_tbl[_PAGE_CACHE_MODE_WP] != _PAGE_CACHE_MODE_WP)
750 		return NULL;
751 
752 	return early_memremap_prot(phys_addr, size, __PAGE_KERNEL_ENC_WP);
753 }
754 
755 /* Remap memory without encryption */
756 void __init *early_memremap_decrypted(resource_size_t phys_addr,
757 				      unsigned long size)
758 {
759 	return early_memremap_prot(phys_addr, size, __PAGE_KERNEL_NOENC);
760 }
761 
762 /*
763  * Remap memory without encryption and write-protected - cannot be called
764  * before pat_init() is called
765  */
766 void __init *early_memremap_decrypted_wp(resource_size_t phys_addr,
767 					 unsigned long size)
768 {
769 	/* Be sure the write-protect PAT entry is set for write-protect */
770 	if (__pte2cachemode_tbl[_PAGE_CACHE_MODE_WP] != _PAGE_CACHE_MODE_WP)
771 		return NULL;
772 
773 	return early_memremap_prot(phys_addr, size, __PAGE_KERNEL_NOENC_WP);
774 }
775 #endif	/* CONFIG_AMD_MEM_ENCRYPT */
776 
777 static pte_t bm_pte[PAGE_SIZE/sizeof(pte_t)] __page_aligned_bss;
778 
779 static inline pmd_t * __init early_ioremap_pmd(unsigned long addr)
780 {
781 	/* Don't assume we're using swapper_pg_dir at this point */
782 	pgd_t *base = __va(read_cr3_pa());
783 	pgd_t *pgd = &base[pgd_index(addr)];
784 	p4d_t *p4d = p4d_offset(pgd, addr);
785 	pud_t *pud = pud_offset(p4d, addr);
786 	pmd_t *pmd = pmd_offset(pud, addr);
787 
788 	return pmd;
789 }
790 
791 static inline pte_t * __init early_ioremap_pte(unsigned long addr)
792 {
793 	return &bm_pte[pte_index(addr)];
794 }
795 
796 bool __init is_early_ioremap_ptep(pte_t *ptep)
797 {
798 	return ptep >= &bm_pte[0] && ptep < &bm_pte[PAGE_SIZE/sizeof(pte_t)];
799 }
800 
801 void __init early_ioremap_init(void)
802 {
803 	pmd_t *pmd;
804 
805 #ifdef CONFIG_X86_64
806 	BUILD_BUG_ON((fix_to_virt(0) + PAGE_SIZE) & ((1 << PMD_SHIFT) - 1));
807 #else
808 	WARN_ON((fix_to_virt(0) + PAGE_SIZE) & ((1 << PMD_SHIFT) - 1));
809 #endif
810 
811 	early_ioremap_setup();
812 
813 	pmd = early_ioremap_pmd(fix_to_virt(FIX_BTMAP_BEGIN));
814 	memset(bm_pte, 0, sizeof(bm_pte));
815 	pmd_populate_kernel(&init_mm, pmd, bm_pte);
816 
817 	/*
818 	 * The boot-ioremap range spans multiple pmds, for which
819 	 * we are not prepared:
820 	 */
821 #define __FIXADDR_TOP (-PAGE_SIZE)
822 	BUILD_BUG_ON((__fix_to_virt(FIX_BTMAP_BEGIN) >> PMD_SHIFT)
823 		     != (__fix_to_virt(FIX_BTMAP_END) >> PMD_SHIFT));
824 #undef __FIXADDR_TOP
825 	if (pmd != early_ioremap_pmd(fix_to_virt(FIX_BTMAP_END))) {
826 		WARN_ON(1);
827 		printk(KERN_WARNING "pmd %p != %p\n",
828 		       pmd, early_ioremap_pmd(fix_to_virt(FIX_BTMAP_END)));
829 		printk(KERN_WARNING "fix_to_virt(FIX_BTMAP_BEGIN): %08lx\n",
830 			fix_to_virt(FIX_BTMAP_BEGIN));
831 		printk(KERN_WARNING "fix_to_virt(FIX_BTMAP_END):   %08lx\n",
832 			fix_to_virt(FIX_BTMAP_END));
833 
834 		printk(KERN_WARNING "FIX_BTMAP_END:       %d\n", FIX_BTMAP_END);
835 		printk(KERN_WARNING "FIX_BTMAP_BEGIN:     %d\n",
836 		       FIX_BTMAP_BEGIN);
837 	}
838 }
839 
840 void __init __early_set_fixmap(enum fixed_addresses idx,
841 			       phys_addr_t phys, pgprot_t flags)
842 {
843 	unsigned long addr = __fix_to_virt(idx);
844 	pte_t *pte;
845 
846 	if (idx >= __end_of_fixed_addresses) {
847 		BUG();
848 		return;
849 	}
850 	pte = early_ioremap_pte(addr);
851 
852 	/* Sanitize 'prot' against any unsupported bits: */
853 	pgprot_val(flags) &= __supported_pte_mask;
854 
855 	if (pgprot_val(flags))
856 		set_pte(pte, pfn_pte(phys >> PAGE_SHIFT, flags));
857 	else
858 		pte_clear(&init_mm, addr, pte);
859 	__flush_tlb_one_kernel(addr);
860 }
861