blob: 43964cde6214ae6b0fc3cb6cfec911b6a4df2fd2 [file] [log] [blame]
Bjorn Helgaase9b0a072006-03-26 01:37:06 -08001/*
Bjorn Helgaas9b50ffb2007-03-30 10:34:05 -06002 * (c) Copyright 2006, 2007 Hewlett-Packard Development Company, L.P.
Bjorn Helgaase9b0a072006-03-26 01:37:06 -08003 * 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 Helgaas9b50ffb2007-03-30 10:34:05 -060013#include <linux/io.h>
14#include <linux/vmalloc.h>
Bjorn Helgaase9b0a072006-03-26 01:37:06 -080015#include <asm/io.h>
Bjorn Helgaas32e62c62006-05-05 17:19:50 -060016#include <asm/meminit.h>
Bjorn Helgaase9b0a072006-03-26 01:37:06 -080017
18static inline void __iomem *
Li, Zhen-Huaa4279e62013-03-18 10:45:43 +080019__ioremap_uc(unsigned long phys_addr)
Bjorn Helgaase9b0a072006-03-26 01:37:06 -080020{
Bjorn Helgaasc4add2e2007-03-30 10:33:11 -060021 return (void __iomem *) (__IA64_UNCACHED_OFFSET | phys_addr);
Bjorn Helgaase9b0a072006-03-26 01:37:06 -080022}
23
24void __iomem *
Luck, Tonycd7bcf32009-12-14 20:00:36 +000025early_ioremap (unsigned long phys_addr, unsigned long size)
26{
Li, Zhen-Huaa4279e62013-03-18 10:45:43 +080027 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, Tonycd7bcf32009-12-14 20:00:36 +000032}
33
34void __iomem *
Bjorn Helgaasc4add2e2007-03-30 10:33:11 -060035ioremap (unsigned long phys_addr, unsigned long size)
Bjorn Helgaase9b0a072006-03-26 01:37:06 -080036{
Bjorn Helgaas9b50ffb2007-03-30 10:34:05 -060037 void __iomem *addr;
38 struct vm_struct *area;
39 unsigned long offset;
40 pgprot_t prot;
Bjorn Helgaas32e62c62006-05-05 17:19:50 -060041 u64 attr;
42 unsigned long gran_base, gran_size;
Bjorn Helgaas9b50ffb2007-03-30 10:34:05 -060043 unsigned long page_base;
Bjorn Helgaase9b0a072006-03-26 01:37:06 -080044
Bjorn Helgaas32e62c62006-05-05 17:19:50 -060045 /*
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 Helgaasc4add2e2007-03-30 10:33:11 -060050 attr = kern_mem_attribute(phys_addr, size);
Bjorn Helgaas32e62c62006-05-05 17:19:50 -060051 if (attr & EFI_MEMORY_WB)
Bjorn Helgaasc4add2e2007-03-30 10:33:11 -060052 return (void __iomem *) phys_to_virt(phys_addr);
Bjorn Helgaas32e62c62006-05-05 17:19:50 -060053 else if (attr & EFI_MEMORY_UC)
Li, Zhen-Huaa4279e62013-03-18 10:45:43 +080054 return __ioremap_uc(phys_addr);
Bjorn Helgaasc1c57d72006-03-30 09:53:39 -070055
Bjorn Helgaase9b0a072006-03-26 01:37:06 -080056 /*
Bjorn Helgaas32e62c62006-05-05 17:19:50 -060057 * Some chipsets don't support UC access to memory. If
58 * WB is supported for the whole granule, we prefer that.
Bjorn Helgaase9b0a072006-03-26 01:37:06 -080059 */
Bjorn Helgaasc4add2e2007-03-30 10:33:11 -060060 gran_base = GRANULEROUNDDOWN(phys_addr);
61 gran_size = GRANULEROUNDUP(phys_addr + size) - gran_base;
Bjorn Helgaas32e62c62006-05-05 17:19:50 -060062 if (efi_mem_attribute(gran_base, gran_size) & EFI_MEMORY_WB)
Bjorn Helgaasc4add2e2007-03-30 10:33:11 -060063 return (void __iomem *) phys_to_virt(phys_addr);
Bjorn Helgaas32e62c62006-05-05 17:19:50 -060064
Bjorn Helgaas9b50ffb2007-03-30 10:34:05 -060065 /*
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-Huaa4279e62013-03-18 10:45:43 +0800100 return __ioremap_uc(phys_addr);
Bjorn Helgaase9b0a072006-03-26 01:37:06 -0800101}
102EXPORT_SYMBOL(ioremap);
103
104void __iomem *
Bjorn Helgaasc4add2e2007-03-30 10:33:11 -0600105ioremap_nocache (unsigned long phys_addr, unsigned long size)
Bjorn Helgaase9b0a072006-03-26 01:37:06 -0800106{
Bjorn Helgaasc4add2e2007-03-30 10:33:11 -0600107 if (kern_mem_attribute(phys_addr, size) & EFI_MEMORY_WB)
Keith Owense037cda2006-07-17 15:41:59 +1000108 return NULL;
Bjorn Helgaas32e62c62006-05-05 17:19:50 -0600109
Li, Zhen-Huaa4279e62013-03-18 10:45:43 +0800110 return __ioremap_uc(phys_addr);
Bjorn Helgaase9b0a072006-03-26 01:37:06 -0800111}
112EXPORT_SYMBOL(ioremap_nocache);
Bjorn Helgaas9b50ffb2007-03-30 10:34:05 -0600113
114void
Luck, Tonycd7bcf32009-12-14 20:00:36 +0000115early_iounmap (volatile void __iomem *addr, unsigned long size)
116{
117}
118
119void
Bjorn Helgaas9b50ffb2007-03-30 10:34:05 -0600120iounmap (volatile void __iomem *addr)
121{
122 if (REGION_NUMBER(addr) == RGN_GATE)
123 vunmap((void *) ((unsigned long) addr & PAGE_MASK));
124}
125EXPORT_SYMBOL(iounmap);