Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 1 | /* |
| 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 Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 10 | * |
| 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 Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 24 | #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 Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 36 | |
| 37 | #include <asm/pgalloc.h> |
| 38 | #include <asm/page.h> |
| 39 | #include <asm/prom.h> |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 40 | #include <asm/io.h> |
| 41 | #include <asm/mmu_context.h> |
| 42 | #include <asm/pgtable.h> |
| 43 | #include <asm/mmu.h> |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 44 | #include <asm/smp.h> |
| 45 | #include <asm/machdep.h> |
| 46 | #include <asm/tlb.h> |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 47 | #include <asm/processor.h> |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 48 | #include <asm/cputable.h> |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 49 | #include <asm/sections.h> |
| 50 | #include <asm/system.h> |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 51 | #include <asm/abs_addr.h> |
Stephen Rothwell | 5e203d6 | 2006-09-25 13:36:31 +1000 | [diff] [blame] | 52 | #include <asm/firmware.h> |
David Gibson | 800fc3e | 2005-11-16 15:43:48 +1100 | [diff] [blame] | 53 | |
| 54 | #include "mmu_decl.h" |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 55 | |
Benjamin Herrenschmidt | 3d5134e | 2007-06-04 15:15:36 +1000 | [diff] [blame] | 56 | unsigned long ioremap_bot = IOREMAP_BASE; |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 57 | |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 58 | /* |
| 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 | */ |
| 63 | static 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 Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 69 | |
| 70 | if (mem_init_done) { |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 71 | 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 Mackerras | 23fd077 | 2005-10-31 13:37:12 +1100 | [diff] [blame] | 78 | ptep = pte_alloc_kernel(pmdp, ea); |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 79 | if (!ptep) |
| 80 | return -ENOMEM; |
| 81 | set_pte_at(&init_mm, ea, ptep, pfn_pte(pa >> PAGE_SHIFT, |
| 82 | __pgprot(flags))); |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 83 | } else { |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 84 | /* |
| 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 Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 88 | * |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 89 | */ |
Benjamin Herrenschmidt | 3d5134e | 2007-06-04 15:15:36 +1000 | [diff] [blame] | 90 | if (htab_bolt_mapping(ea, (unsigned long)ea + PAGE_SIZE, |
| 91 | pa, flags, mmu_io_psize)) { |
Benjamin Herrenschmidt | 77ac166 | 2005-11-10 11:12:11 +1100 | [diff] [blame] | 92 | printk(KERN_ERR "Failed to do bolted mapping IO " |
| 93 | "memory at %016lx !\n", pa); |
| 94 | return -ENOMEM; |
| 95 | } |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 96 | } |
| 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | |
Benjamin Herrenschmidt | 3d5134e | 2007-06-04 15:15:36 +1000 | [diff] [blame] | 101 | /** |
| 102 | * __ioremap_at - Low level function to establish the page tables |
| 103 | * for an IO mapping |
| 104 | */ |
| 105 | void __iomem * __ioremap_at(phys_addr_t pa, void *ea, unsigned long size, |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 106 | unsigned long flags) |
| 107 | { |
| 108 | unsigned long i; |
| 109 | |
| 110 | if ((flags & _PAGE_PRESENT) == 0) |
| 111 | flags |= pgprot_val(PAGE_KERNEL); |
| 112 | |
Benjamin Herrenschmidt | 3d5134e | 2007-06-04 15:15:36 +1000 | [diff] [blame] | 113 | WARN_ON(pa & ~PAGE_MASK); |
| 114 | WARN_ON(((unsigned long)ea) & ~PAGE_MASK); |
| 115 | WARN_ON(size & ~PAGE_MASK); |
| 116 | |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 117 | for (i = 0; i < size; i += PAGE_SIZE) |
Benjamin Herrenschmidt | 3d5134e | 2007-06-04 15:15:36 +1000 | [diff] [blame] | 118 | if (map_io_page((unsigned long)ea+i, pa+i, flags)) |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 119 | return NULL; |
| 120 | |
Benjamin Herrenschmidt | 3d5134e | 2007-06-04 15:15:36 +1000 | [diff] [blame] | 121 | return (void __iomem *)ea; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * __iounmap_from - Low level function to tear down the page tables |
| 126 | * for an IO mapping. This is used for mappings that |
| 127 | * are manipulated manually, like partial unmapping of |
| 128 | * PCI IOs or ISA space. |
| 129 | */ |
| 130 | void __iounmap_at(void *ea, unsigned long size) |
| 131 | { |
| 132 | WARN_ON(((unsigned long)ea) & ~PAGE_MASK); |
| 133 | WARN_ON(size & ~PAGE_MASK); |
| 134 | |
| 135 | unmap_kernel_range((unsigned long)ea, size); |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 136 | } |
| 137 | |
Benjamin Herrenschmidt | 68a6435 | 2006-11-13 09:27:39 +1100 | [diff] [blame] | 138 | void __iomem * __ioremap(phys_addr_t addr, unsigned long size, |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 139 | unsigned long flags) |
| 140 | { |
Benjamin Herrenschmidt | 3d5134e | 2007-06-04 15:15:36 +1000 | [diff] [blame] | 141 | phys_addr_t paligned; |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 142 | void __iomem *ret; |
| 143 | |
| 144 | /* |
| 145 | * Choose an address to map it to. |
| 146 | * Once the imalloc system is running, we use it. |
| 147 | * Before that, we map using addresses going |
| 148 | * up from ioremap_bot. imalloc will use |
| 149 | * the addresses from ioremap_bot through |
| 150 | * IMALLOC_END |
| 151 | * |
| 152 | */ |
Benjamin Herrenschmidt | 3d5134e | 2007-06-04 15:15:36 +1000 | [diff] [blame] | 153 | paligned = addr & PAGE_MASK; |
| 154 | size = PAGE_ALIGN(addr + size) - paligned; |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 155 | |
Benjamin Herrenschmidt | 3d5134e | 2007-06-04 15:15:36 +1000 | [diff] [blame] | 156 | if ((size == 0) || (paligned == 0)) |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 157 | return NULL; |
| 158 | |
| 159 | if (mem_init_done) { |
| 160 | struct vm_struct *area; |
Benjamin Herrenschmidt | 3d5134e | 2007-06-04 15:15:36 +1000 | [diff] [blame] | 161 | |
| 162 | area = __get_vm_area(size, VM_IOREMAP, |
| 163 | ioremap_bot, IOREMAP_END); |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 164 | if (area == NULL) |
| 165 | return NULL; |
Benjamin Herrenschmidt | 3d5134e | 2007-06-04 15:15:36 +1000 | [diff] [blame] | 166 | ret = __ioremap_at(paligned, area->addr, size, flags); |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 167 | if (!ret) |
Benjamin Herrenschmidt | 3d5134e | 2007-06-04 15:15:36 +1000 | [diff] [blame] | 168 | vunmap(area->addr); |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 169 | } else { |
Benjamin Herrenschmidt | 3d5134e | 2007-06-04 15:15:36 +1000 | [diff] [blame] | 170 | ret = __ioremap_at(paligned, (void *)ioremap_bot, size, flags); |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 171 | if (ret) |
| 172 | ioremap_bot += size; |
| 173 | } |
Benjamin Herrenschmidt | 3d5134e | 2007-06-04 15:15:36 +1000 | [diff] [blame] | 174 | |
| 175 | if (ret) |
| 176 | ret += addr & ~PAGE_MASK; |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 177 | return ret; |
| 178 | } |
| 179 | |
Benjamin Herrenschmidt | 4cb3cee | 2006-11-11 17:25:10 +1100 | [diff] [blame] | 180 | |
Benjamin Herrenschmidt | 68a6435 | 2006-11-13 09:27:39 +1100 | [diff] [blame] | 181 | void __iomem * ioremap(phys_addr_t addr, unsigned long size) |
Benjamin Herrenschmidt | 4cb3cee | 2006-11-11 17:25:10 +1100 | [diff] [blame] | 182 | { |
| 183 | unsigned long flags = _PAGE_NO_CACHE | _PAGE_GUARDED; |
| 184 | |
| 185 | if (ppc_md.ioremap) |
| 186 | return ppc_md.ioremap(addr, size, flags); |
| 187 | return __ioremap(addr, size, flags); |
| 188 | } |
| 189 | |
Benjamin Herrenschmidt | 68a6435 | 2006-11-13 09:27:39 +1100 | [diff] [blame] | 190 | void __iomem * ioremap_flags(phys_addr_t addr, unsigned long size, |
Benjamin Herrenschmidt | 4cb3cee | 2006-11-11 17:25:10 +1100 | [diff] [blame] | 191 | unsigned long flags) |
| 192 | { |
| 193 | if (ppc_md.ioremap) |
| 194 | return ppc_md.ioremap(addr, size, flags); |
| 195 | return __ioremap(addr, size, flags); |
| 196 | } |
| 197 | |
| 198 | |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 199 | /* |
| 200 | * Unmap an IO region and remove it from imalloc'd list. |
| 201 | * Access to IO memory should be serialized by driver. |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 202 | */ |
Benjamin Herrenschmidt | 68a6435 | 2006-11-13 09:27:39 +1100 | [diff] [blame] | 203 | void __iounmap(volatile void __iomem *token) |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 204 | { |
| 205 | void *addr; |
| 206 | |
| 207 | if (!mem_init_done) |
| 208 | return; |
| 209 | |
Benjamin Herrenschmidt | 3d5134e | 2007-06-04 15:15:36 +1000 | [diff] [blame] | 210 | addr = (void *) ((unsigned long __force) |
| 211 | PCI_FIX_ADDR(token) & PAGE_MASK); |
| 212 | if ((unsigned long)addr < ioremap_bot) { |
| 213 | printk(KERN_WARNING "Attempt to iounmap early bolted mapping" |
| 214 | " at 0x%p\n", addr); |
| 215 | return; |
| 216 | } |
| 217 | vunmap(addr); |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 218 | } |
| 219 | |
Benjamin Herrenschmidt | 68a6435 | 2006-11-13 09:27:39 +1100 | [diff] [blame] | 220 | void iounmap(volatile void __iomem *token) |
Benjamin Herrenschmidt | 4cb3cee | 2006-11-11 17:25:10 +1100 | [diff] [blame] | 221 | { |
| 222 | if (ppc_md.iounmap) |
| 223 | ppc_md.iounmap(token); |
| 224 | else |
| 225 | __iounmap(token); |
| 226 | } |
| 227 | |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 228 | EXPORT_SYMBOL(ioremap); |
Benjamin Herrenschmidt | 4cb3cee | 2006-11-11 17:25:10 +1100 | [diff] [blame] | 229 | EXPORT_SYMBOL(ioremap_flags); |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 230 | EXPORT_SYMBOL(__ioremap); |
| 231 | EXPORT_SYMBOL(iounmap); |
Benjamin Herrenschmidt | 4cb3cee | 2006-11-11 17:25:10 +1100 | [diff] [blame] | 232 | EXPORT_SYMBOL(__iounmap); |