Bjorn Helgaas | e9b0a07 | 2006-03-26 01:37:06 -0800 | [diff] [blame] | 1 | /* |
Bjorn Helgaas | 9b50ffb | 2007-03-30 10:34:05 -0600 | [diff] [blame] | 2 | * (c) Copyright 2006, 2007 Hewlett-Packard Development Company, L.P. |
Bjorn Helgaas | e9b0a07 | 2006-03-26 01:37:06 -0800 | [diff] [blame] | 3 | * Bjorn Helgaas <bjorn.helgaas@hp.com> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation. |
| 8 | */ |
| 9 | |
| 10 | #include <linux/compiler.h> |
| 11 | #include <linux/module.h> |
| 12 | #include <linux/efi.h> |
Bjorn Helgaas | 9b50ffb | 2007-03-30 10:34:05 -0600 | [diff] [blame] | 13 | #include <linux/io.h> |
| 14 | #include <linux/vmalloc.h> |
Bjorn Helgaas | e9b0a07 | 2006-03-26 01:37:06 -0800 | [diff] [blame] | 15 | #include <asm/io.h> |
Bjorn Helgaas | 32e62c6 | 2006-05-05 17:19:50 -0600 | [diff] [blame] | 16 | #include <asm/meminit.h> |
Bjorn Helgaas | e9b0a07 | 2006-03-26 01:37:06 -0800 | [diff] [blame] | 17 | |
| 18 | static inline void __iomem * |
Li, Zhen-Hua | a4279e6 | 2013-03-18 10:45:43 +0800 | [diff] [blame] | 19 | __ioremap_uc(unsigned long phys_addr) |
Bjorn Helgaas | e9b0a07 | 2006-03-26 01:37:06 -0800 | [diff] [blame] | 20 | { |
Bjorn Helgaas | c4add2e | 2007-03-30 10:33:11 -0600 | [diff] [blame] | 21 | return (void __iomem *) (__IA64_UNCACHED_OFFSET | phys_addr); |
Bjorn Helgaas | e9b0a07 | 2006-03-26 01:37:06 -0800 | [diff] [blame] | 22 | } |
| 23 | |
| 24 | void __iomem * |
Luck, Tony | cd7bcf3 | 2009-12-14 20:00:36 +0000 | [diff] [blame] | 25 | early_ioremap (unsigned long phys_addr, unsigned long size) |
| 26 | { |
Li, Zhen-Hua | a4279e6 | 2013-03-18 10:45:43 +0800 | [diff] [blame] | 27 | u64 attr; |
| 28 | attr = kern_mem_attribute(phys_addr, size); |
| 29 | if (attr & EFI_MEMORY_WB) |
| 30 | return (void __iomem *) phys_to_virt(phys_addr); |
| 31 | return __ioremap_uc(phys_addr); |
Luck, Tony | cd7bcf3 | 2009-12-14 20:00:36 +0000 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | void __iomem * |
Bjorn Helgaas | c4add2e | 2007-03-30 10:33:11 -0600 | [diff] [blame] | 35 | ioremap (unsigned long phys_addr, unsigned long size) |
Bjorn Helgaas | e9b0a07 | 2006-03-26 01:37:06 -0800 | [diff] [blame] | 36 | { |
Bjorn Helgaas | 9b50ffb | 2007-03-30 10:34:05 -0600 | [diff] [blame] | 37 | void __iomem *addr; |
| 38 | struct vm_struct *area; |
| 39 | unsigned long offset; |
| 40 | pgprot_t prot; |
Bjorn Helgaas | 32e62c6 | 2006-05-05 17:19:50 -0600 | [diff] [blame] | 41 | u64 attr; |
| 42 | unsigned long gran_base, gran_size; |
Bjorn Helgaas | 9b50ffb | 2007-03-30 10:34:05 -0600 | [diff] [blame] | 43 | unsigned long page_base; |
Bjorn Helgaas | e9b0a07 | 2006-03-26 01:37:06 -0800 | [diff] [blame] | 44 | |
Bjorn Helgaas | 32e62c6 | 2006-05-05 17:19:50 -0600 | [diff] [blame] | 45 | /* |
| 46 | * For things in kern_memmap, we must use the same attribute |
| 47 | * as the rest of the kernel. For more details, see |
| 48 | * Documentation/ia64/aliasing.txt. |
| 49 | */ |
Bjorn Helgaas | c4add2e | 2007-03-30 10:33:11 -0600 | [diff] [blame] | 50 | attr = kern_mem_attribute(phys_addr, size); |
Bjorn Helgaas | 32e62c6 | 2006-05-05 17:19:50 -0600 | [diff] [blame] | 51 | if (attr & EFI_MEMORY_WB) |
Bjorn Helgaas | c4add2e | 2007-03-30 10:33:11 -0600 | [diff] [blame] | 52 | return (void __iomem *) phys_to_virt(phys_addr); |
Bjorn Helgaas | 32e62c6 | 2006-05-05 17:19:50 -0600 | [diff] [blame] | 53 | else if (attr & EFI_MEMORY_UC) |
Li, Zhen-Hua | a4279e6 | 2013-03-18 10:45:43 +0800 | [diff] [blame] | 54 | return __ioremap_uc(phys_addr); |
Bjorn Helgaas | c1c57d7 | 2006-03-30 09:53:39 -0700 | [diff] [blame] | 55 | |
Bjorn Helgaas | e9b0a07 | 2006-03-26 01:37:06 -0800 | [diff] [blame] | 56 | /* |
Bjorn Helgaas | 32e62c6 | 2006-05-05 17:19:50 -0600 | [diff] [blame] | 57 | * Some chipsets don't support UC access to memory. If |
| 58 | * WB is supported for the whole granule, we prefer that. |
Bjorn Helgaas | e9b0a07 | 2006-03-26 01:37:06 -0800 | [diff] [blame] | 59 | */ |
Bjorn Helgaas | c4add2e | 2007-03-30 10:33:11 -0600 | [diff] [blame] | 60 | gran_base = GRANULEROUNDDOWN(phys_addr); |
| 61 | gran_size = GRANULEROUNDUP(phys_addr + size) - gran_base; |
Bjorn Helgaas | 32e62c6 | 2006-05-05 17:19:50 -0600 | [diff] [blame] | 62 | if (efi_mem_attribute(gran_base, gran_size) & EFI_MEMORY_WB) |
Bjorn Helgaas | c4add2e | 2007-03-30 10:33:11 -0600 | [diff] [blame] | 63 | return (void __iomem *) phys_to_virt(phys_addr); |
Bjorn Helgaas | 32e62c6 | 2006-05-05 17:19:50 -0600 | [diff] [blame] | 64 | |
Bjorn Helgaas | 9b50ffb | 2007-03-30 10:34:05 -0600 | [diff] [blame] | 65 | /* |
| 66 | * WB is not supported for the whole granule, so we can't use |
| 67 | * the region 7 identity mapping. If we can safely cover the |
| 68 | * area with kernel page table mappings, we can use those |
| 69 | * instead. |
| 70 | */ |
| 71 | page_base = phys_addr & PAGE_MASK; |
| 72 | size = PAGE_ALIGN(phys_addr + size) - page_base; |
| 73 | if (efi_mem_attribute(page_base, size) & EFI_MEMORY_WB) { |
| 74 | prot = PAGE_KERNEL; |
| 75 | |
| 76 | /* |
| 77 | * Mappings have to be page-aligned |
| 78 | */ |
| 79 | offset = phys_addr & ~PAGE_MASK; |
| 80 | phys_addr &= PAGE_MASK; |
| 81 | |
| 82 | /* |
| 83 | * Ok, go for it.. |
| 84 | */ |
| 85 | area = get_vm_area(size, VM_IOREMAP); |
| 86 | if (!area) |
| 87 | return NULL; |
| 88 | |
| 89 | area->phys_addr = phys_addr; |
| 90 | addr = (void __iomem *) area->addr; |
| 91 | if (ioremap_page_range((unsigned long) addr, |
| 92 | (unsigned long) addr + size, phys_addr, prot)) { |
| 93 | vunmap((void __force *) addr); |
| 94 | return NULL; |
| 95 | } |
| 96 | |
| 97 | return (void __iomem *) (offset + (char __iomem *)addr); |
| 98 | } |
| 99 | |
Li, Zhen-Hua | a4279e6 | 2013-03-18 10:45:43 +0800 | [diff] [blame] | 100 | return __ioremap_uc(phys_addr); |
Bjorn Helgaas | e9b0a07 | 2006-03-26 01:37:06 -0800 | [diff] [blame] | 101 | } |
| 102 | EXPORT_SYMBOL(ioremap); |
| 103 | |
| 104 | void __iomem * |
Bjorn Helgaas | c4add2e | 2007-03-30 10:33:11 -0600 | [diff] [blame] | 105 | ioremap_nocache (unsigned long phys_addr, unsigned long size) |
Bjorn Helgaas | e9b0a07 | 2006-03-26 01:37:06 -0800 | [diff] [blame] | 106 | { |
Bjorn Helgaas | c4add2e | 2007-03-30 10:33:11 -0600 | [diff] [blame] | 107 | if (kern_mem_attribute(phys_addr, size) & EFI_MEMORY_WB) |
Keith Owens | e037cda | 2006-07-17 15:41:59 +1000 | [diff] [blame] | 108 | return NULL; |
Bjorn Helgaas | 32e62c6 | 2006-05-05 17:19:50 -0600 | [diff] [blame] | 109 | |
Li, Zhen-Hua | a4279e6 | 2013-03-18 10:45:43 +0800 | [diff] [blame] | 110 | return __ioremap_uc(phys_addr); |
Bjorn Helgaas | e9b0a07 | 2006-03-26 01:37:06 -0800 | [diff] [blame] | 111 | } |
| 112 | EXPORT_SYMBOL(ioremap_nocache); |
Bjorn Helgaas | 9b50ffb | 2007-03-30 10:34:05 -0600 | [diff] [blame] | 113 | |
| 114 | void |
Luck, Tony | cd7bcf3 | 2009-12-14 20:00:36 +0000 | [diff] [blame] | 115 | early_iounmap (volatile void __iomem *addr, unsigned long size) |
| 116 | { |
| 117 | } |
| 118 | |
| 119 | void |
Bjorn Helgaas | 9b50ffb | 2007-03-30 10:34:05 -0600 | [diff] [blame] | 120 | iounmap (volatile void __iomem *addr) |
| 121 | { |
| 122 | if (REGION_NUMBER(addr) == RGN_GATE) |
| 123 | vunmap((void *) ((unsigned long) addr & PAGE_MASK)); |
| 124 | } |
| 125 | EXPORT_SYMBOL(iounmap); |