Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Bjorn Helgaas | e9b0a07 | 2006-03-26 01:37:06 -0800 | [diff] [blame] | 2 | /* |
Bjorn Helgaas | 9b50ffb | 2007-03-30 10:34:05 -0600 | [diff] [blame] | 3 | * (c) Copyright 2006, 2007 Hewlett-Packard Development Company, L.P. |
Bjorn Helgaas | e9b0a07 | 2006-03-26 01:37:06 -0800 | [diff] [blame] | 4 | * Bjorn Helgaas <bjorn.helgaas@hp.com> |
Bjorn Helgaas | e9b0a07 | 2006-03-26 01:37:06 -0800 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <linux/compiler.h> |
| 8 | #include <linux/module.h> |
| 9 | #include <linux/efi.h> |
Bjorn Helgaas | 9b50ffb | 2007-03-30 10:34:05 -0600 | [diff] [blame] | 10 | #include <linux/io.h> |
Sebastian Andrzej Siewior | 6f28b46 | 2020-03-21 12:25:53 +0100 | [diff] [blame] | 11 | #include <linux/mm.h> |
Bjorn Helgaas | 9b50ffb | 2007-03-30 10:34:05 -0600 | [diff] [blame] | 12 | #include <linux/vmalloc.h> |
Bjorn Helgaas | e9b0a07 | 2006-03-26 01:37:06 -0800 | [diff] [blame] | 13 | #include <asm/io.h> |
Bjorn Helgaas | 32e62c6 | 2006-05-05 17:19:50 -0600 | [diff] [blame] | 14 | #include <asm/meminit.h> |
Bjorn Helgaas | e9b0a07 | 2006-03-26 01:37:06 -0800 | [diff] [blame] | 15 | |
| 16 | static inline void __iomem * |
Li, Zhen-Hua | a4279e6 | 2013-03-18 10:45:43 +0800 | [diff] [blame] | 17 | __ioremap_uc(unsigned long phys_addr) |
Bjorn Helgaas | e9b0a07 | 2006-03-26 01:37:06 -0800 | [diff] [blame] | 18 | { |
Bjorn Helgaas | c4add2e | 2007-03-30 10:33:11 -0600 | [diff] [blame] | 19 | return (void __iomem *) (__IA64_UNCACHED_OFFSET | phys_addr); |
Bjorn Helgaas | e9b0a07 | 2006-03-26 01:37:06 -0800 | [diff] [blame] | 20 | } |
| 21 | |
| 22 | void __iomem * |
Luck, Tony | cd7bcf3 | 2009-12-14 20:00:36 +0000 | [diff] [blame] | 23 | early_ioremap (unsigned long phys_addr, unsigned long size) |
| 24 | { |
Li, Zhen-Hua | a4279e6 | 2013-03-18 10:45:43 +0800 | [diff] [blame] | 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); |
Luck, Tony | cd7bcf3 | 2009-12-14 20:00:36 +0000 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | void __iomem * |
Bjorn Helgaas | c4add2e | 2007-03-30 10:33:11 -0600 | [diff] [blame] | 33 | ioremap (unsigned long phys_addr, unsigned long size) |
Bjorn Helgaas | e9b0a07 | 2006-03-26 01:37:06 -0800 | [diff] [blame] | 34 | { |
Bjorn Helgaas | 9b50ffb | 2007-03-30 10:34:05 -0600 | [diff] [blame] | 35 | void __iomem *addr; |
| 36 | struct vm_struct *area; |
| 37 | unsigned long offset; |
| 38 | pgprot_t prot; |
Bjorn Helgaas | 32e62c6 | 2006-05-05 17:19:50 -0600 | [diff] [blame] | 39 | u64 attr; |
| 40 | unsigned long gran_base, gran_size; |
Bjorn Helgaas | 9b50ffb | 2007-03-30 10:34:05 -0600 | [diff] [blame] | 41 | unsigned long page_base; |
Bjorn Helgaas | e9b0a07 | 2006-03-26 01:37:06 -0800 | [diff] [blame] | 42 | |
Bjorn Helgaas | 32e62c6 | 2006-05-05 17:19:50 -0600 | [diff] [blame] | 43 | /* |
| 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 Chehab | db9a097 | 2019-04-18 10:10:33 -0300 | [diff] [blame] | 46 | * Documentation/ia64/aliasing.rst. |
Bjorn Helgaas | 32e62c6 | 2006-05-05 17:19:50 -0600 | [diff] [blame] | 47 | */ |
Bjorn Helgaas | c4add2e | 2007-03-30 10:33:11 -0600 | [diff] [blame] | 48 | attr = kern_mem_attribute(phys_addr, size); |
Bjorn Helgaas | 32e62c6 | 2006-05-05 17:19:50 -0600 | [diff] [blame] | 49 | if (attr & EFI_MEMORY_WB) |
Bjorn Helgaas | c4add2e | 2007-03-30 10:33:11 -0600 | [diff] [blame] | 50 | return (void __iomem *) phys_to_virt(phys_addr); |
Bjorn Helgaas | 32e62c6 | 2006-05-05 17:19:50 -0600 | [diff] [blame] | 51 | else if (attr & EFI_MEMORY_UC) |
Li, Zhen-Hua | a4279e6 | 2013-03-18 10:45:43 +0800 | [diff] [blame] | 52 | return __ioremap_uc(phys_addr); |
Bjorn Helgaas | c1c57d7 | 2006-03-30 09:53:39 -0700 | [diff] [blame] | 53 | |
Bjorn Helgaas | e9b0a07 | 2006-03-26 01:37:06 -0800 | [diff] [blame] | 54 | /* |
Bjorn Helgaas | 32e62c6 | 2006-05-05 17:19:50 -0600 | [diff] [blame] | 55 | * Some chipsets don't support UC access to memory. If |
| 56 | * WB is supported for the whole granule, we prefer that. |
Bjorn Helgaas | e9b0a07 | 2006-03-26 01:37:06 -0800 | [diff] [blame] | 57 | */ |
Bjorn Helgaas | c4add2e | 2007-03-30 10:33:11 -0600 | [diff] [blame] | 58 | gran_base = GRANULEROUNDDOWN(phys_addr); |
| 59 | gran_size = GRANULEROUNDUP(phys_addr + size) - gran_base; |
Bjorn Helgaas | 32e62c6 | 2006-05-05 17:19:50 -0600 | [diff] [blame] | 60 | if (efi_mem_attribute(gran_base, gran_size) & EFI_MEMORY_WB) |
Bjorn Helgaas | c4add2e | 2007-03-30 10:33:11 -0600 | [diff] [blame] | 61 | return (void __iomem *) phys_to_virt(phys_addr); |
Bjorn Helgaas | 32e62c6 | 2006-05-05 17:19:50 -0600 | [diff] [blame] | 62 | |
Bjorn Helgaas | 9b50ffb | 2007-03-30 10:34:05 -0600 | [diff] [blame] | 63 | /* |
| 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-Hua | a4279e6 | 2013-03-18 10:45:43 +0800 | [diff] [blame] | 98 | return __ioremap_uc(phys_addr); |
Bjorn Helgaas | e9b0a07 | 2006-03-26 01:37:06 -0800 | [diff] [blame] | 99 | } |
| 100 | EXPORT_SYMBOL(ioremap); |
| 101 | |
| 102 | void __iomem * |
Christoph Hellwig | fded182 | 2019-08-11 14:30:14 +0200 | [diff] [blame] | 103 | ioremap_uc(unsigned long phys_addr, unsigned long size) |
Bjorn Helgaas | e9b0a07 | 2006-03-26 01:37:06 -0800 | [diff] [blame] | 104 | { |
Bjorn Helgaas | c4add2e | 2007-03-30 10:33:11 -0600 | [diff] [blame] | 105 | if (kern_mem_attribute(phys_addr, size) & EFI_MEMORY_WB) |
Keith Owens | e037cda | 2006-07-17 15:41:59 +1000 | [diff] [blame] | 106 | return NULL; |
Bjorn Helgaas | 32e62c6 | 2006-05-05 17:19:50 -0600 | [diff] [blame] | 107 | |
Li, Zhen-Hua | a4279e6 | 2013-03-18 10:45:43 +0800 | [diff] [blame] | 108 | return __ioremap_uc(phys_addr); |
Bjorn Helgaas | e9b0a07 | 2006-03-26 01:37:06 -0800 | [diff] [blame] | 109 | } |
Christoph Hellwig | fded182 | 2019-08-11 14:30:14 +0200 | [diff] [blame] | 110 | EXPORT_SYMBOL(ioremap_uc); |
Bjorn Helgaas | 9b50ffb | 2007-03-30 10:34:05 -0600 | [diff] [blame] | 111 | |
| 112 | void |
Luck, Tony | cd7bcf3 | 2009-12-14 20:00:36 +0000 | [diff] [blame] | 113 | early_iounmap (volatile void __iomem *addr, unsigned long size) |
| 114 | { |
| 115 | } |
| 116 | |
| 117 | void |
Bjorn Helgaas | 9b50ffb | 2007-03-30 10:34:05 -0600 | [diff] [blame] | 118 | iounmap (volatile void __iomem *addr) |
| 119 | { |
| 120 | if (REGION_NUMBER(addr) == RGN_GATE) |
| 121 | vunmap((void *) ((unsigned long) addr & PAGE_MASK)); |
| 122 | } |
| 123 | EXPORT_SYMBOL(iounmap); |