blob: 55fd3eb753ff94fe8491e095a42ca6a1c3a2b5ba [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Bjorn Helgaase9b0a072006-03-26 01:37:06 -08002/*
Bjorn Helgaas9b50ffb2007-03-30 10:34:05 -06003 * (c) Copyright 2006, 2007 Hewlett-Packard Development Company, L.P.
Bjorn Helgaase9b0a072006-03-26 01:37:06 -08004 * Bjorn Helgaas <bjorn.helgaas@hp.com>
Bjorn Helgaase9b0a072006-03-26 01:37:06 -08005 */
6
7#include <linux/compiler.h>
8#include <linux/module.h>
9#include <linux/efi.h>
Bjorn Helgaas9b50ffb2007-03-30 10:34:05 -060010#include <linux/io.h>
Sebastian Andrzej Siewior6f28b462020-03-21 12:25:53 +010011#include <linux/mm.h>
Bjorn Helgaas9b50ffb2007-03-30 10:34:05 -060012#include <linux/vmalloc.h>
Bjorn Helgaase9b0a072006-03-26 01:37:06 -080013#include <asm/io.h>
Bjorn Helgaas32e62c62006-05-05 17:19:50 -060014#include <asm/meminit.h>
Bjorn Helgaase9b0a072006-03-26 01:37:06 -080015
16static inline void __iomem *
Li, Zhen-Huaa4279e62013-03-18 10:45:43 +080017__ioremap_uc(unsigned long phys_addr)
Bjorn Helgaase9b0a072006-03-26 01:37:06 -080018{
Bjorn Helgaasc4add2e2007-03-30 10:33:11 -060019 return (void __iomem *) (__IA64_UNCACHED_OFFSET | phys_addr);
Bjorn Helgaase9b0a072006-03-26 01:37:06 -080020}
21
22void __iomem *
Luck, Tonycd7bcf32009-12-14 20:00:36 +000023early_ioremap (unsigned long phys_addr, unsigned long size)
24{
Li, Zhen-Huaa4279e62013-03-18 10:45:43 +080025 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);
Luck, Tonycd7bcf32009-12-14 20:00:36 +000030}
31
32void __iomem *
Bjorn Helgaasc4add2e2007-03-30 10:33:11 -060033ioremap (unsigned long phys_addr, unsigned long size)
Bjorn Helgaase9b0a072006-03-26 01:37:06 -080034{
Bjorn Helgaas9b50ffb2007-03-30 10:34:05 -060035 void __iomem *addr;
36 struct vm_struct *area;
37 unsigned long offset;
38 pgprot_t prot;
Bjorn Helgaas32e62c62006-05-05 17:19:50 -060039 u64 attr;
40 unsigned long gran_base, gran_size;
Bjorn Helgaas9b50ffb2007-03-30 10:34:05 -060041 unsigned long page_base;
Bjorn Helgaase9b0a072006-03-26 01:37:06 -080042
Bjorn Helgaas32e62c62006-05-05 17:19:50 -060043 /*
44 * For things in kern_memmap, we must use the same attribute
45 * as the rest of the kernel. For more details, see
Mauro Carvalho Chehabdb9a0972019-04-18 10:10:33 -030046 * Documentation/ia64/aliasing.rst.
Bjorn Helgaas32e62c62006-05-05 17:19:50 -060047 */
Bjorn Helgaasc4add2e2007-03-30 10:33:11 -060048 attr = kern_mem_attribute(phys_addr, size);
Bjorn Helgaas32e62c62006-05-05 17:19:50 -060049 if (attr & EFI_MEMORY_WB)
Bjorn Helgaasc4add2e2007-03-30 10:33:11 -060050 return (void __iomem *) phys_to_virt(phys_addr);
Bjorn Helgaas32e62c62006-05-05 17:19:50 -060051 else if (attr & EFI_MEMORY_UC)
Li, Zhen-Huaa4279e62013-03-18 10:45:43 +080052 return __ioremap_uc(phys_addr);
Bjorn Helgaasc1c57d72006-03-30 09:53:39 -070053
Bjorn Helgaase9b0a072006-03-26 01:37:06 -080054 /*
Bjorn Helgaas32e62c62006-05-05 17:19:50 -060055 * Some chipsets don't support UC access to memory. If
56 * WB is supported for the whole granule, we prefer that.
Bjorn Helgaase9b0a072006-03-26 01:37:06 -080057 */
Bjorn Helgaasc4add2e2007-03-30 10:33:11 -060058 gran_base = GRANULEROUNDDOWN(phys_addr);
59 gran_size = GRANULEROUNDUP(phys_addr + size) - gran_base;
Bjorn Helgaas32e62c62006-05-05 17:19:50 -060060 if (efi_mem_attribute(gran_base, gran_size) & EFI_MEMORY_WB)
Bjorn Helgaasc4add2e2007-03-30 10:33:11 -060061 return (void __iomem *) phys_to_virt(phys_addr);
Bjorn Helgaas32e62c62006-05-05 17:19:50 -060062
Bjorn Helgaas9b50ffb2007-03-30 10:34:05 -060063 /*
64 * WB is not supported for the whole granule, so we can't use
65 * the region 7 identity mapping. If we can safely cover the
66 * area with kernel page table mappings, we can use those
67 * instead.
68 */
69 page_base = phys_addr & PAGE_MASK;
70 size = PAGE_ALIGN(phys_addr + size) - page_base;
71 if (efi_mem_attribute(page_base, size) & EFI_MEMORY_WB) {
72 prot = PAGE_KERNEL;
73
74 /*
75 * Mappings have to be page-aligned
76 */
77 offset = phys_addr & ~PAGE_MASK;
78 phys_addr &= PAGE_MASK;
79
80 /*
81 * Ok, go for it..
82 */
83 area = get_vm_area(size, VM_IOREMAP);
84 if (!area)
85 return NULL;
86
87 area->phys_addr = phys_addr;
88 addr = (void __iomem *) area->addr;
89 if (ioremap_page_range((unsigned long) addr,
90 (unsigned long) addr + size, phys_addr, prot)) {
91 vunmap((void __force *) addr);
92 return NULL;
93 }
94
95 return (void __iomem *) (offset + (char __iomem *)addr);
96 }
97
Li, Zhen-Huaa4279e62013-03-18 10:45:43 +080098 return __ioremap_uc(phys_addr);
Bjorn Helgaase9b0a072006-03-26 01:37:06 -080099}
100EXPORT_SYMBOL(ioremap);
101
102void __iomem *
Christoph Hellwigfded1822019-08-11 14:30:14 +0200103ioremap_uc(unsigned long phys_addr, unsigned long size)
Bjorn Helgaase9b0a072006-03-26 01:37:06 -0800104{
Bjorn Helgaasc4add2e2007-03-30 10:33:11 -0600105 if (kern_mem_attribute(phys_addr, size) & EFI_MEMORY_WB)
Keith Owense037cda2006-07-17 15:41:59 +1000106 return NULL;
Bjorn Helgaas32e62c62006-05-05 17:19:50 -0600107
Li, Zhen-Huaa4279e62013-03-18 10:45:43 +0800108 return __ioremap_uc(phys_addr);
Bjorn Helgaase9b0a072006-03-26 01:37:06 -0800109}
Christoph Hellwigfded1822019-08-11 14:30:14 +0200110EXPORT_SYMBOL(ioremap_uc);
Bjorn Helgaas9b50ffb2007-03-30 10:34:05 -0600111
112void
Luck, Tonycd7bcf32009-12-14 20:00:36 +0000113early_iounmap (volatile void __iomem *addr, unsigned long size)
114{
115}
116
117void
Bjorn Helgaas9b50ffb2007-03-30 10:34:05 -0600118iounmap (volatile void __iomem *addr)
119{
120 if (REGION_NUMBER(addr) == RGN_GATE)
121 vunmap((void *) ((unsigned long) addr & PAGE_MASK));
122}
123EXPORT_SYMBOL(iounmap);