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