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 * |
Bjorn Helgaas | 9b50ffb | 2007-03-30 10:34:05 -0600 | [diff] [blame] | 19 | __ioremap (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 * |
Bjorn Helgaas | c4add2e | 2007-03-30 10:33:11 -0600 | [diff] [blame] | 25 | ioremap (unsigned long phys_addr, unsigned long size) |
Bjorn Helgaas | e9b0a07 | 2006-03-26 01:37:06 -0800 | [diff] [blame] | 26 | { |
Bjorn Helgaas | 9b50ffb | 2007-03-30 10:34:05 -0600 | [diff] [blame] | 27 | void __iomem *addr; |
| 28 | struct vm_struct *area; |
| 29 | unsigned long offset; |
| 30 | pgprot_t prot; |
Bjorn Helgaas | 32e62c6 | 2006-05-05 17:19:50 -0600 | [diff] [blame] | 31 | u64 attr; |
| 32 | unsigned long gran_base, gran_size; |
Bjorn Helgaas | 9b50ffb | 2007-03-30 10:34:05 -0600 | [diff] [blame] | 33 | unsigned long page_base; |
Bjorn Helgaas | e9b0a07 | 2006-03-26 01:37:06 -0800 | [diff] [blame] | 34 | |
Bjorn Helgaas | 32e62c6 | 2006-05-05 17:19:50 -0600 | [diff] [blame] | 35 | /* |
| 36 | * For things in kern_memmap, we must use the same attribute |
| 37 | * as the rest of the kernel. For more details, see |
| 38 | * Documentation/ia64/aliasing.txt. |
| 39 | */ |
Bjorn Helgaas | c4add2e | 2007-03-30 10:33:11 -0600 | [diff] [blame] | 40 | attr = kern_mem_attribute(phys_addr, size); |
Bjorn Helgaas | 32e62c6 | 2006-05-05 17:19:50 -0600 | [diff] [blame] | 41 | if (attr & EFI_MEMORY_WB) |
Bjorn Helgaas | c4add2e | 2007-03-30 10:33:11 -0600 | [diff] [blame] | 42 | return (void __iomem *) phys_to_virt(phys_addr); |
Bjorn Helgaas | 32e62c6 | 2006-05-05 17:19:50 -0600 | [diff] [blame] | 43 | else if (attr & EFI_MEMORY_UC) |
Bjorn Helgaas | 9b50ffb | 2007-03-30 10:34:05 -0600 | [diff] [blame] | 44 | return __ioremap(phys_addr); |
Bjorn Helgaas | c1c57d7 | 2006-03-30 09:53:39 -0700 | [diff] [blame] | 45 | |
Bjorn Helgaas | e9b0a07 | 2006-03-26 01:37:06 -0800 | [diff] [blame] | 46 | /* |
Bjorn Helgaas | 32e62c6 | 2006-05-05 17:19:50 -0600 | [diff] [blame] | 47 | * Some chipsets don't support UC access to memory. If |
| 48 | * WB is supported for the whole granule, we prefer that. |
Bjorn Helgaas | e9b0a07 | 2006-03-26 01:37:06 -0800 | [diff] [blame] | 49 | */ |
Bjorn Helgaas | c4add2e | 2007-03-30 10:33:11 -0600 | [diff] [blame] | 50 | gran_base = GRANULEROUNDDOWN(phys_addr); |
| 51 | gran_size = GRANULEROUNDUP(phys_addr + size) - gran_base; |
Bjorn Helgaas | 32e62c6 | 2006-05-05 17:19:50 -0600 | [diff] [blame] | 52 | if (efi_mem_attribute(gran_base, gran_size) & EFI_MEMORY_WB) |
Bjorn Helgaas | c4add2e | 2007-03-30 10:33:11 -0600 | [diff] [blame] | 53 | return (void __iomem *) phys_to_virt(phys_addr); |
Bjorn Helgaas | 32e62c6 | 2006-05-05 17:19:50 -0600 | [diff] [blame] | 54 | |
Bjorn Helgaas | 9b50ffb | 2007-03-30 10:34:05 -0600 | [diff] [blame] | 55 | /* |
| 56 | * WB is not supported for the whole granule, so we can't use |
| 57 | * the region 7 identity mapping. If we can safely cover the |
| 58 | * area with kernel page table mappings, we can use those |
| 59 | * instead. |
| 60 | */ |
| 61 | page_base = phys_addr & PAGE_MASK; |
| 62 | size = PAGE_ALIGN(phys_addr + size) - page_base; |
| 63 | if (efi_mem_attribute(page_base, size) & EFI_MEMORY_WB) { |
| 64 | prot = PAGE_KERNEL; |
| 65 | |
| 66 | /* |
| 67 | * Mappings have to be page-aligned |
| 68 | */ |
| 69 | offset = phys_addr & ~PAGE_MASK; |
| 70 | phys_addr &= PAGE_MASK; |
| 71 | |
| 72 | /* |
| 73 | * Ok, go for it.. |
| 74 | */ |
| 75 | area = get_vm_area(size, VM_IOREMAP); |
| 76 | if (!area) |
| 77 | return NULL; |
| 78 | |
| 79 | area->phys_addr = phys_addr; |
| 80 | addr = (void __iomem *) area->addr; |
| 81 | if (ioremap_page_range((unsigned long) addr, |
| 82 | (unsigned long) addr + size, phys_addr, prot)) { |
| 83 | vunmap((void __force *) addr); |
| 84 | return NULL; |
| 85 | } |
| 86 | |
| 87 | return (void __iomem *) (offset + (char __iomem *)addr); |
| 88 | } |
| 89 | |
| 90 | return __ioremap(phys_addr); |
Bjorn Helgaas | e9b0a07 | 2006-03-26 01:37:06 -0800 | [diff] [blame] | 91 | } |
| 92 | EXPORT_SYMBOL(ioremap); |
| 93 | |
| 94 | void __iomem * |
Bjorn Helgaas | c4add2e | 2007-03-30 10:33:11 -0600 | [diff] [blame] | 95 | ioremap_nocache (unsigned long phys_addr, unsigned long size) |
Bjorn Helgaas | e9b0a07 | 2006-03-26 01:37:06 -0800 | [diff] [blame] | 96 | { |
Bjorn Helgaas | c4add2e | 2007-03-30 10:33:11 -0600 | [diff] [blame] | 97 | if (kern_mem_attribute(phys_addr, size) & EFI_MEMORY_WB) |
Keith Owens | e037cda | 2006-07-17 15:41:59 +1000 | [diff] [blame] | 98 | return NULL; |
Bjorn Helgaas | 32e62c6 | 2006-05-05 17:19:50 -0600 | [diff] [blame] | 99 | |
Bjorn Helgaas | 9b50ffb | 2007-03-30 10:34:05 -0600 | [diff] [blame] | 100 | return __ioremap(phys_addr); |
Bjorn Helgaas | e9b0a07 | 2006-03-26 01:37:06 -0800 | [diff] [blame] | 101 | } |
| 102 | EXPORT_SYMBOL(ioremap_nocache); |
Bjorn Helgaas | 9b50ffb | 2007-03-30 10:34:05 -0600 | [diff] [blame] | 103 | |
| 104 | void |
| 105 | iounmap (volatile void __iomem *addr) |
| 106 | { |
| 107 | if (REGION_NUMBER(addr) == RGN_GATE) |
| 108 | vunmap((void *) ((unsigned long) addr & PAGE_MASK)); |
| 109 | } |
| 110 | EXPORT_SYMBOL(iounmap); |