1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * (c) Copyright 2006, 2007 Hewlett-Packard Development Company, L.P.
4  *	Bjorn Helgaas <bjorn.helgaas@hp.com>
5  */
6 
7 #include <linux/compiler.h>
8 #include <linux/module.h>
9 #include <linux/efi.h>
10 #include <linux/io.h>
11 #include <linux/mm.h>
12 #include <linux/vmalloc.h>
13 #include <asm/io.h>
14 #include <asm/meminit.h>
15 
16 static inline void __iomem *
__ioremap_uc(unsigned long phys_addr)17 __ioremap_uc(unsigned long phys_addr)
18 {
19 	return (void __iomem *) (__IA64_UNCACHED_OFFSET | phys_addr);
20 }
21 
22 void __iomem *
early_ioremap(unsigned long phys_addr,unsigned long size)23 early_ioremap (unsigned long phys_addr, unsigned long size)
24 {
25 	u64 attr;
26 	attr = kern_mem_attribute(phys_addr, size);
27 	if (attr & EFI_MEMORY_WB)
28 		return (void __iomem *) phys_to_virt(phys_addr);
29 	return __ioremap_uc(phys_addr);
30 }
31 
ioremap_prot(phys_addr_t phys_addr,size_t size,unsigned long flags)32 void __iomem *ioremap_prot(phys_addr_t phys_addr, size_t size,
33 			   unsigned long flags)
34 {
35 	u64 attr;
36 	unsigned long gran_base, gran_size;
37 	unsigned long page_base;
38 
39 	/*
40 	 * For things in kern_memmap, we must use the same attribute
41 	 * as the rest of the kernel.  For more details, see
42 	 * Documentation/arch/ia64/aliasing.rst.
43 	 */
44 	attr = kern_mem_attribute(phys_addr, size);
45 	if (attr & EFI_MEMORY_WB)
46 		return (void __iomem *) phys_to_virt(phys_addr);
47 	else if (attr & EFI_MEMORY_UC)
48 		return __ioremap_uc(phys_addr);
49 
50 	/*
51 	 * Some chipsets don't support UC access to memory.  If
52 	 * WB is supported for the whole granule, we prefer that.
53 	 */
54 	gran_base = GRANULEROUNDDOWN(phys_addr);
55 	gran_size = GRANULEROUNDUP(phys_addr + size) - gran_base;
56 	if (efi_mem_attribute(gran_base, gran_size) & EFI_MEMORY_WB)
57 		return (void __iomem *) phys_to_virt(phys_addr);
58 
59 	/*
60 	 * WB is not supported for the whole granule, so we can't use
61 	 * the region 7 identity mapping.  If we can safely cover the
62 	 * area with kernel page table mappings, we can use those
63 	 * instead.
64 	 */
65 	page_base = phys_addr & PAGE_MASK;
66 	size = PAGE_ALIGN(phys_addr + size) - page_base;
67 	if (efi_mem_attribute(page_base, size) & EFI_MEMORY_WB)
68 		return generic_ioremap_prot(phys_addr, size, __pgprot(flags));
69 
70 	return __ioremap_uc(phys_addr);
71 }
72 EXPORT_SYMBOL(ioremap_prot);
73 
74 void __iomem *
ioremap_uc(unsigned long phys_addr,unsigned long size)75 ioremap_uc(unsigned long phys_addr, unsigned long size)
76 {
77 	if (kern_mem_attribute(phys_addr, size) & EFI_MEMORY_WB)
78 		return NULL;
79 
80 	return __ioremap_uc(phys_addr);
81 }
82 EXPORT_SYMBOL(ioremap_uc);
83 
84 void
early_iounmap(volatile void __iomem * addr,unsigned long size)85 early_iounmap (volatile void __iomem *addr, unsigned long size)
86 {
87 }
88 
iounmap(volatile void __iomem * addr)89 void iounmap(volatile void __iomem *addr)
90 {
91 	if (REGION_NUMBER(addr) == RGN_GATE)
92 		vunmap((void *) ((unsigned long) addr & PAGE_MASK));
93 }
94 EXPORT_SYMBOL(iounmap);
95