blob: bfa7db6b2fd5446e193669a1afd02d09f7f9aab9 [file] [log] [blame]
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001/*
2 * This file contains ioremap and related functions for 64-bit machines.
3 *
4 * Derived from arch/ppc64/mm/init.c
5 * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
6 *
7 * Modifications by Paul Mackerras (PowerMac) (paulus@samba.org)
8 * and Cort Dougan (PReP) (cort@cs.nmt.edu)
9 * Copyright (C) 1996 Paul Mackerras
Paul Mackerras14cf11a2005-09-26 16:04:21 +100010 *
11 * Derived from "arch/i386/mm/init.c"
12 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
13 *
14 * Dave Engebretsen <engebret@us.ibm.com>
15 * Rework for PPC64 port.
16 *
17 * This program is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU General Public License
19 * as published by the Free Software Foundation; either version
20 * 2 of the License, or (at your option) any later version.
21 *
22 */
23
Paul Mackerras14cf11a2005-09-26 16:04:21 +100024#include <linux/signal.h>
25#include <linux/sched.h>
26#include <linux/kernel.h>
27#include <linux/errno.h>
28#include <linux/string.h>
29#include <linux/types.h>
30#include <linux/mman.h>
31#include <linux/mm.h>
32#include <linux/swap.h>
33#include <linux/stddef.h>
34#include <linux/vmalloc.h>
35#include <linux/init.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100036
37#include <asm/pgalloc.h>
38#include <asm/page.h>
39#include <asm/prom.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100040#include <asm/io.h>
41#include <asm/mmu_context.h>
42#include <asm/pgtable.h>
43#include <asm/mmu.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100044#include <asm/smp.h>
45#include <asm/machdep.h>
46#include <asm/tlb.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100047#include <asm/processor.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100048#include <asm/cputable.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100049#include <asm/sections.h>
50#include <asm/system.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100051#include <asm/abs_addr.h>
Stephen Rothwell5e203d62006-09-25 13:36:31 +100052#include <asm/firmware.h>
David Gibson800fc3e2005-11-16 15:43:48 +110053
54#include "mmu_decl.h"
Paul Mackerras14cf11a2005-09-26 16:04:21 +100055
Benjamin Herrenschmidt3d5134e2007-06-04 15:15:36 +100056unsigned long ioremap_bot = IOREMAP_BASE;
Paul Mackerras14cf11a2005-09-26 16:04:21 +100057
Paul Mackerras14cf11a2005-09-26 16:04:21 +100058/*
59 * map_io_page currently only called by __ioremap
60 * map_io_page adds an entry to the ioremap page table
61 * and adds an entry to the HPT, possibly bolting it
62 */
63static int map_io_page(unsigned long ea, unsigned long pa, int flags)
64{
65 pgd_t *pgdp;
66 pud_t *pudp;
67 pmd_t *pmdp;
68 pte_t *ptep;
Paul Mackerras14cf11a2005-09-26 16:04:21 +100069
70 if (mem_init_done) {
Paul Mackerras14cf11a2005-09-26 16:04:21 +100071 pgdp = pgd_offset_k(ea);
72 pudp = pud_alloc(&init_mm, pgdp, ea);
73 if (!pudp)
74 return -ENOMEM;
75 pmdp = pmd_alloc(&init_mm, pudp, ea);
76 if (!pmdp)
77 return -ENOMEM;
Paul Mackerras23fd0772005-10-31 13:37:12 +110078 ptep = pte_alloc_kernel(pmdp, ea);
Paul Mackerras14cf11a2005-09-26 16:04:21 +100079 if (!ptep)
80 return -ENOMEM;
81 set_pte_at(&init_mm, ea, ptep, pfn_pte(pa >> PAGE_SHIFT,
82 __pgprot(flags)));
Paul Mackerras14cf11a2005-09-26 16:04:21 +100083 } else {
Paul Mackerras14cf11a2005-09-26 16:04:21 +100084 /*
85 * If the mm subsystem is not fully up, we cannot create a
86 * linux page table entry for this mapping. Simply bolt an
87 * entry in the hardware page table.
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +110088 *
Paul Mackerras14cf11a2005-09-26 16:04:21 +100089 */
Paul Mackerras1189be62007-10-11 20:37:10 +100090 if (htab_bolt_mapping(ea, ea + PAGE_SIZE, pa, flags,
91 mmu_io_psize, mmu_kernel_ssize)) {
Benjamin Herrenschmidt77ac1662005-11-10 11:12:11 +110092 printk(KERN_ERR "Failed to do bolted mapping IO "
93 "memory at %016lx !\n", pa);
94 return -ENOMEM;
95 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +100096 }
97 return 0;
98}
99
100
Benjamin Herrenschmidt3d5134e2007-06-04 15:15:36 +1000101/**
102 * __ioremap_at - Low level function to establish the page tables
103 * for an IO mapping
104 */
105void __iomem * __ioremap_at(phys_addr_t pa, void *ea, unsigned long size,
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000106 unsigned long flags)
107{
108 unsigned long i;
109
Benjamin Herrenschmidta1f242f2008-07-23 21:27:08 -0700110 /* Make sure we have the base flags */
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000111 if ((flags & _PAGE_PRESENT) == 0)
112 flags |= pgprot_val(PAGE_KERNEL);
113
Benjamin Herrenschmidta1f242f2008-07-23 21:27:08 -0700114 /* Non-cacheable page cannot be coherent */
115 if (flags & _PAGE_NO_CACHE)
116 flags &= ~_PAGE_COHERENT;
117
118 /* We don't support the 4K PFN hack with ioremap */
119 if (flags & _PAGE_4K_PFN)
120 return NULL;
121
Benjamin Herrenschmidt3d5134e2007-06-04 15:15:36 +1000122 WARN_ON(pa & ~PAGE_MASK);
123 WARN_ON(((unsigned long)ea) & ~PAGE_MASK);
124 WARN_ON(size & ~PAGE_MASK);
125
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000126 for (i = 0; i < size; i += PAGE_SIZE)
Benjamin Herrenschmidt3d5134e2007-06-04 15:15:36 +1000127 if (map_io_page((unsigned long)ea+i, pa+i, flags))
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000128 return NULL;
129
Benjamin Herrenschmidt3d5134e2007-06-04 15:15:36 +1000130 return (void __iomem *)ea;
131}
132
133/**
134 * __iounmap_from - Low level function to tear down the page tables
135 * for an IO mapping. This is used for mappings that
136 * are manipulated manually, like partial unmapping of
137 * PCI IOs or ISA space.
138 */
139void __iounmap_at(void *ea, unsigned long size)
140{
141 WARN_ON(((unsigned long)ea) & ~PAGE_MASK);
142 WARN_ON(size & ~PAGE_MASK);
143
144 unmap_kernel_range((unsigned long)ea, size);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000145}
146
Benjamin Herrenschmidt1cdab552009-02-22 16:19:14 +0000147void __iomem * __ioremap_caller(phys_addr_t addr, unsigned long size,
148 unsigned long flags, void *caller)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000149{
Benjamin Herrenschmidt3d5134e2007-06-04 15:15:36 +1000150 phys_addr_t paligned;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000151 void __iomem *ret;
152
153 /*
154 * Choose an address to map it to.
155 * Once the imalloc system is running, we use it.
156 * Before that, we map using addresses going
157 * up from ioremap_bot. imalloc will use
158 * the addresses from ioremap_bot through
159 * IMALLOC_END
160 *
161 */
Benjamin Herrenschmidt3d5134e2007-06-04 15:15:36 +1000162 paligned = addr & PAGE_MASK;
163 size = PAGE_ALIGN(addr + size) - paligned;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000164
Benjamin Herrenschmidt3d5134e2007-06-04 15:15:36 +1000165 if ((size == 0) || (paligned == 0))
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000166 return NULL;
167
168 if (mem_init_done) {
169 struct vm_struct *area;
Benjamin Herrenschmidt3d5134e2007-06-04 15:15:36 +1000170
Benjamin Herrenschmidt1cdab552009-02-22 16:19:14 +0000171 area = __get_vm_area_caller(size, VM_IOREMAP,
172 ioremap_bot, IOREMAP_END,
173 caller);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000174 if (area == NULL)
175 return NULL;
Benjamin Herrenschmidt3d5134e2007-06-04 15:15:36 +1000176 ret = __ioremap_at(paligned, area->addr, size, flags);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000177 if (!ret)
Benjamin Herrenschmidt3d5134e2007-06-04 15:15:36 +1000178 vunmap(area->addr);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000179 } else {
Benjamin Herrenschmidt3d5134e2007-06-04 15:15:36 +1000180 ret = __ioremap_at(paligned, (void *)ioremap_bot, size, flags);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000181 if (ret)
182 ioremap_bot += size;
183 }
Benjamin Herrenschmidt3d5134e2007-06-04 15:15:36 +1000184
185 if (ret)
186 ret += addr & ~PAGE_MASK;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000187 return ret;
188}
189
Benjamin Herrenschmidt1cdab552009-02-22 16:19:14 +0000190void __iomem * __ioremap(phys_addr_t addr, unsigned long size,
191 unsigned long flags)
192{
193 return __ioremap_caller(addr, size, flags, __builtin_return_address(0));
194}
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100195
Benjamin Herrenschmidt68a64352006-11-13 09:27:39 +1100196void __iomem * ioremap(phys_addr_t addr, unsigned long size)
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100197{
198 unsigned long flags = _PAGE_NO_CACHE | _PAGE_GUARDED;
Benjamin Herrenschmidt1cdab552009-02-22 16:19:14 +0000199 void *caller = __builtin_return_address(0);
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100200
201 if (ppc_md.ioremap)
Benjamin Herrenschmidt1cdab552009-02-22 16:19:14 +0000202 return ppc_md.ioremap(addr, size, flags, caller);
203 return __ioremap_caller(addr, size, flags, caller);
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100204}
205
Benjamin Herrenschmidt68a64352006-11-13 09:27:39 +1100206void __iomem * ioremap_flags(phys_addr_t addr, unsigned long size,
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100207 unsigned long flags)
208{
Benjamin Herrenschmidt1cdab552009-02-22 16:19:14 +0000209 void *caller = __builtin_return_address(0);
210
Benjamin Herrenschmidta1f242f2008-07-23 21:27:08 -0700211 /* writeable implies dirty for kernel addresses */
212 if (flags & _PAGE_RW)
213 flags |= _PAGE_DIRTY;
214
215 /* we don't want to let _PAGE_USER and _PAGE_EXEC leak out */
216 flags &= ~(_PAGE_USER | _PAGE_EXEC);
217
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100218 if (ppc_md.ioremap)
Benjamin Herrenschmidt1cdab552009-02-22 16:19:14 +0000219 return ppc_md.ioremap(addr, size, flags, caller);
220 return __ioremap_caller(addr, size, flags, caller);
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100221}
222
223
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000224/*
225 * Unmap an IO region and remove it from imalloc'd list.
226 * Access to IO memory should be serialized by driver.
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000227 */
Benjamin Herrenschmidt68a64352006-11-13 09:27:39 +1100228void __iounmap(volatile void __iomem *token)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000229{
230 void *addr;
231
232 if (!mem_init_done)
233 return;
234
Benjamin Herrenschmidt3d5134e2007-06-04 15:15:36 +1000235 addr = (void *) ((unsigned long __force)
236 PCI_FIX_ADDR(token) & PAGE_MASK);
237 if ((unsigned long)addr < ioremap_bot) {
238 printk(KERN_WARNING "Attempt to iounmap early bolted mapping"
239 " at 0x%p\n", addr);
240 return;
241 }
242 vunmap(addr);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000243}
244
Benjamin Herrenschmidt68a64352006-11-13 09:27:39 +1100245void iounmap(volatile void __iomem *token)
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100246{
247 if (ppc_md.iounmap)
248 ppc_md.iounmap(token);
249 else
250 __iounmap(token);
251}
252
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000253EXPORT_SYMBOL(ioremap);
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100254EXPORT_SYMBOL(ioremap_flags);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000255EXPORT_SYMBOL(__ioremap);
Olof Johanssona302cb92007-08-31 13:58:51 +1000256EXPORT_SYMBOL(__ioremap_at);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000257EXPORT_SYMBOL(iounmap);
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100258EXPORT_SYMBOL(__iounmap);
Olof Johanssona302cb92007-08-31 13:58:51 +1000259EXPORT_SYMBOL(__iounmap_at);