Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Extensible Firmware Interface |
| 3 | * |
Aron Griffis | 7d9aed2 | 2008-02-04 15:31:49 -0800 | [diff] [blame] | 4 | * Based on Extensible Firmware Interface Specification version 0.9 |
| 5 | * April 30, 1999 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * |
| 7 | * Copyright (C) 1999 VA Linux Systems |
| 8 | * Copyright (C) 1999 Walt Drummond <drummond@valinux.com> |
| 9 | * Copyright (C) 1999-2003 Hewlett-Packard Co. |
| 10 | * David Mosberger-Tang <davidm@hpl.hp.com> |
| 11 | * Stephane Eranian <eranian@hpl.hp.com> |
Bjorn Helgaas | 32e62c6 | 2006-05-05 17:19:50 -0600 | [diff] [blame] | 12 | * (c) Copyright 2006 Hewlett-Packard Development Company, L.P. |
| 13 | * Bjorn Helgaas <bjorn.helgaas@hp.com> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | * |
| 15 | * All EFI Runtime Services are not implemented yet as EFI only |
| 16 | * supports physical mode addressing on SoftSDV. This is to be fixed |
| 17 | * in a future version. --drummond 1999-07-20 |
| 18 | * |
| 19 | * Implemented EFI runtime services and virtual mode calls. --davidm |
| 20 | * |
| 21 | * Goutham Rao: <goutham.rao@intel.com> |
| 22 | * Skip non-WB memory and ignore empty memory ranges. |
| 23 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <linux/module.h> |
Horms | f4a5709 | 2007-03-06 02:34:21 -0800 | [diff] [blame] | 25 | #include <linux/bootmem.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <linux/kernel.h> |
| 27 | #include <linux/init.h> |
| 28 | #include <linux/types.h> |
| 29 | #include <linux/time.h> |
| 30 | #include <linux/efi.h> |
Zou Nan hai | a7956113 | 2006-12-07 09:51:35 -0800 | [diff] [blame] | 31 | #include <linux/kexec.h> |
Mel Gorman | ed7ed36 | 2007-07-17 04:03:14 -0700 | [diff] [blame] | 32 | #include <linux/mm.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | |
| 34 | #include <asm/io.h> |
| 35 | #include <asm/kregs.h> |
| 36 | #include <asm/meminit.h> |
| 37 | #include <asm/pgtable.h> |
| 38 | #include <asm/processor.h> |
| 39 | #include <asm/mca.h> |
| 40 | |
| 41 | #define EFI_DEBUG 0 |
| 42 | |
| 43 | extern efi_status_t efi_call_phys (void *, ...); |
| 44 | |
| 45 | struct efi efi; |
| 46 | EXPORT_SYMBOL(efi); |
| 47 | static efi_runtime_services_t *runtime; |
Zou Nan hai | a7956113 | 2006-12-07 09:51:35 -0800 | [diff] [blame] | 48 | static unsigned long mem_limit = ~0UL, max_addr = ~0UL, min_addr = 0UL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | |
| 50 | #define efi_call_virt(f, args...) (*(f))(args) |
| 51 | |
Aron Griffis | 7d9aed2 | 2008-02-04 15:31:49 -0800 | [diff] [blame] | 52 | #define STUB_GET_TIME(prefix, adjust_arg) \ |
| 53 | static efi_status_t \ |
| 54 | prefix##_get_time (efi_time_t *tm, efi_time_cap_t *tc) \ |
| 55 | { \ |
| 56 | struct ia64_fpreg fr[6]; \ |
| 57 | efi_time_cap_t *atc = NULL; \ |
| 58 | efi_status_t ret; \ |
| 59 | \ |
| 60 | if (tc) \ |
| 61 | atc = adjust_arg(tc); \ |
| 62 | ia64_save_scratch_fpregs(fr); \ |
| 63 | ret = efi_call_##prefix((efi_get_time_t *) __va(runtime->get_time), \ |
| 64 | adjust_arg(tm), atc); \ |
| 65 | ia64_load_scratch_fpregs(fr); \ |
| 66 | return ret; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Aron Griffis | 7d9aed2 | 2008-02-04 15:31:49 -0800 | [diff] [blame] | 69 | #define STUB_SET_TIME(prefix, adjust_arg) \ |
| 70 | static efi_status_t \ |
| 71 | prefix##_set_time (efi_time_t *tm) \ |
| 72 | { \ |
| 73 | struct ia64_fpreg fr[6]; \ |
| 74 | efi_status_t ret; \ |
| 75 | \ |
| 76 | ia64_save_scratch_fpregs(fr); \ |
| 77 | ret = efi_call_##prefix((efi_set_time_t *) __va(runtime->set_time), \ |
| 78 | adjust_arg(tm)); \ |
| 79 | ia64_load_scratch_fpregs(fr); \ |
| 80 | return ret; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Aron Griffis | 7d9aed2 | 2008-02-04 15:31:49 -0800 | [diff] [blame] | 83 | #define STUB_GET_WAKEUP_TIME(prefix, adjust_arg) \ |
| 84 | static efi_status_t \ |
| 85 | prefix##_get_wakeup_time (efi_bool_t *enabled, efi_bool_t *pending, \ |
| 86 | efi_time_t *tm) \ |
| 87 | { \ |
| 88 | struct ia64_fpreg fr[6]; \ |
| 89 | efi_status_t ret; \ |
| 90 | \ |
| 91 | ia64_save_scratch_fpregs(fr); \ |
| 92 | ret = efi_call_##prefix( \ |
| 93 | (efi_get_wakeup_time_t *) __va(runtime->get_wakeup_time), \ |
| 94 | adjust_arg(enabled), adjust_arg(pending), adjust_arg(tm)); \ |
| 95 | ia64_load_scratch_fpregs(fr); \ |
| 96 | return ret; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Aron Griffis | 7d9aed2 | 2008-02-04 15:31:49 -0800 | [diff] [blame] | 99 | #define STUB_SET_WAKEUP_TIME(prefix, adjust_arg) \ |
| 100 | static efi_status_t \ |
| 101 | prefix##_set_wakeup_time (efi_bool_t enabled, efi_time_t *tm) \ |
| 102 | { \ |
| 103 | struct ia64_fpreg fr[6]; \ |
| 104 | efi_time_t *atm = NULL; \ |
| 105 | efi_status_t ret; \ |
| 106 | \ |
| 107 | if (tm) \ |
| 108 | atm = adjust_arg(tm); \ |
| 109 | ia64_save_scratch_fpregs(fr); \ |
| 110 | ret = efi_call_##prefix( \ |
| 111 | (efi_set_wakeup_time_t *) __va(runtime->set_wakeup_time), \ |
| 112 | enabled, atm); \ |
| 113 | ia64_load_scratch_fpregs(fr); \ |
| 114 | return ret; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Aron Griffis | 7d9aed2 | 2008-02-04 15:31:49 -0800 | [diff] [blame] | 117 | #define STUB_GET_VARIABLE(prefix, adjust_arg) \ |
| 118 | static efi_status_t \ |
| 119 | prefix##_get_variable (efi_char16_t *name, efi_guid_t *vendor, u32 *attr, \ |
| 120 | unsigned long *data_size, void *data) \ |
| 121 | { \ |
| 122 | struct ia64_fpreg fr[6]; \ |
| 123 | u32 *aattr = NULL; \ |
| 124 | efi_status_t ret; \ |
| 125 | \ |
| 126 | if (attr) \ |
| 127 | aattr = adjust_arg(attr); \ |
| 128 | ia64_save_scratch_fpregs(fr); \ |
| 129 | ret = efi_call_##prefix( \ |
| 130 | (efi_get_variable_t *) __va(runtime->get_variable), \ |
| 131 | adjust_arg(name), adjust_arg(vendor), aattr, \ |
| 132 | adjust_arg(data_size), adjust_arg(data)); \ |
| 133 | ia64_load_scratch_fpregs(fr); \ |
| 134 | return ret; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Aron Griffis | 7d9aed2 | 2008-02-04 15:31:49 -0800 | [diff] [blame] | 137 | #define STUB_GET_NEXT_VARIABLE(prefix, adjust_arg) \ |
| 138 | static efi_status_t \ |
| 139 | prefix##_get_next_variable (unsigned long *name_size, efi_char16_t *name, \ |
| 140 | efi_guid_t *vendor) \ |
| 141 | { \ |
| 142 | struct ia64_fpreg fr[6]; \ |
| 143 | efi_status_t ret; \ |
| 144 | \ |
| 145 | ia64_save_scratch_fpregs(fr); \ |
| 146 | ret = efi_call_##prefix( \ |
| 147 | (efi_get_next_variable_t *) __va(runtime->get_next_variable), \ |
| 148 | adjust_arg(name_size), adjust_arg(name), adjust_arg(vendor)); \ |
| 149 | ia64_load_scratch_fpregs(fr); \ |
| 150 | return ret; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | } |
| 152 | |
Aron Griffis | 7d9aed2 | 2008-02-04 15:31:49 -0800 | [diff] [blame] | 153 | #define STUB_SET_VARIABLE(prefix, adjust_arg) \ |
| 154 | static efi_status_t \ |
| 155 | prefix##_set_variable (efi_char16_t *name, efi_guid_t *vendor, \ |
| 156 | unsigned long attr, unsigned long data_size, \ |
| 157 | void *data) \ |
| 158 | { \ |
| 159 | struct ia64_fpreg fr[6]; \ |
| 160 | efi_status_t ret; \ |
| 161 | \ |
| 162 | ia64_save_scratch_fpregs(fr); \ |
| 163 | ret = efi_call_##prefix( \ |
| 164 | (efi_set_variable_t *) __va(runtime->set_variable), \ |
| 165 | adjust_arg(name), adjust_arg(vendor), attr, data_size, \ |
| 166 | adjust_arg(data)); \ |
| 167 | ia64_load_scratch_fpregs(fr); \ |
| 168 | return ret; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Aron Griffis | 7d9aed2 | 2008-02-04 15:31:49 -0800 | [diff] [blame] | 171 | #define STUB_GET_NEXT_HIGH_MONO_COUNT(prefix, adjust_arg) \ |
| 172 | static efi_status_t \ |
| 173 | prefix##_get_next_high_mono_count (u32 *count) \ |
| 174 | { \ |
| 175 | struct ia64_fpreg fr[6]; \ |
| 176 | efi_status_t ret; \ |
| 177 | \ |
| 178 | ia64_save_scratch_fpregs(fr); \ |
| 179 | ret = efi_call_##prefix((efi_get_next_high_mono_count_t *) \ |
| 180 | __va(runtime->get_next_high_mono_count), \ |
| 181 | adjust_arg(count)); \ |
| 182 | ia64_load_scratch_fpregs(fr); \ |
| 183 | return ret; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | } |
| 185 | |
Aron Griffis | 7d9aed2 | 2008-02-04 15:31:49 -0800 | [diff] [blame] | 186 | #define STUB_RESET_SYSTEM(prefix, adjust_arg) \ |
| 187 | static void \ |
| 188 | prefix##_reset_system (int reset_type, efi_status_t status, \ |
| 189 | unsigned long data_size, efi_char16_t *data) \ |
| 190 | { \ |
| 191 | struct ia64_fpreg fr[6]; \ |
| 192 | efi_char16_t *adata = NULL; \ |
| 193 | \ |
| 194 | if (data) \ |
| 195 | adata = adjust_arg(data); \ |
| 196 | \ |
| 197 | ia64_save_scratch_fpregs(fr); \ |
| 198 | efi_call_##prefix( \ |
| 199 | (efi_reset_system_t *) __va(runtime->reset_system), \ |
| 200 | reset_type, status, data_size, adata); \ |
| 201 | /* should not return, but just in case... */ \ |
| 202 | ia64_load_scratch_fpregs(fr); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | #define phys_ptr(arg) ((__typeof__(arg)) ia64_tpa(arg)) |
| 206 | |
| 207 | STUB_GET_TIME(phys, phys_ptr) |
| 208 | STUB_SET_TIME(phys, phys_ptr) |
| 209 | STUB_GET_WAKEUP_TIME(phys, phys_ptr) |
| 210 | STUB_SET_WAKEUP_TIME(phys, phys_ptr) |
| 211 | STUB_GET_VARIABLE(phys, phys_ptr) |
| 212 | STUB_GET_NEXT_VARIABLE(phys, phys_ptr) |
| 213 | STUB_SET_VARIABLE(phys, phys_ptr) |
| 214 | STUB_GET_NEXT_HIGH_MONO_COUNT(phys, phys_ptr) |
| 215 | STUB_RESET_SYSTEM(phys, phys_ptr) |
| 216 | |
| 217 | #define id(arg) arg |
| 218 | |
| 219 | STUB_GET_TIME(virt, id) |
| 220 | STUB_SET_TIME(virt, id) |
| 221 | STUB_GET_WAKEUP_TIME(virt, id) |
| 222 | STUB_SET_WAKEUP_TIME(virt, id) |
| 223 | STUB_GET_VARIABLE(virt, id) |
| 224 | STUB_GET_NEXT_VARIABLE(virt, id) |
| 225 | STUB_SET_VARIABLE(virt, id) |
| 226 | STUB_GET_NEXT_HIGH_MONO_COUNT(virt, id) |
| 227 | STUB_RESET_SYSTEM(virt, id) |
| 228 | |
| 229 | void |
| 230 | efi_gettimeofday (struct timespec *ts) |
| 231 | { |
| 232 | efi_time_t tm; |
| 233 | |
Li Zefan | 4b07ae9 | 2007-11-06 13:40:39 -0800 | [diff] [blame] | 234 | if ((*efi.get_time)(&tm, NULL) != EFI_SUCCESS) { |
| 235 | memset(ts, 0, sizeof(*ts)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | return; |
Li Zefan | 4b07ae9 | 2007-11-06 13:40:39 -0800 | [diff] [blame] | 237 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | |
Aron Griffis | 7d9aed2 | 2008-02-04 15:31:49 -0800 | [diff] [blame] | 239 | ts->tv_sec = mktime(tm.year, tm.month, tm.day, |
| 240 | tm.hour, tm.minute, tm.second); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | ts->tv_nsec = tm.nanosecond; |
| 242 | } |
| 243 | |
| 244 | static int |
Christoph Lameter | 66888a6 | 2006-12-04 14:58:35 -0800 | [diff] [blame] | 245 | is_memory_available (efi_memory_desc_t *md) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | { |
| 247 | if (!(md->attribute & EFI_MEMORY_WB)) |
| 248 | return 0; |
| 249 | |
| 250 | switch (md->type) { |
| 251 | case EFI_LOADER_CODE: |
| 252 | case EFI_LOADER_DATA: |
| 253 | case EFI_BOOT_SERVICES_CODE: |
| 254 | case EFI_BOOT_SERVICES_DATA: |
| 255 | case EFI_CONVENTIONAL_MEMORY: |
| 256 | return 1; |
| 257 | } |
| 258 | return 0; |
| 259 | } |
| 260 | |
Tony Luck | d8c97d5 | 2005-09-08 12:39:59 -0700 | [diff] [blame] | 261 | typedef struct kern_memdesc { |
| 262 | u64 attribute; |
| 263 | u64 start; |
| 264 | u64 num_pages; |
| 265 | } kern_memdesc_t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | |
Tony Luck | d8c97d5 | 2005-09-08 12:39:59 -0700 | [diff] [blame] | 267 | static kern_memdesc_t *kern_memmap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | |
Bjorn Helgaas | 80851ef | 2006-01-08 01:04:13 -0800 | [diff] [blame] | 269 | #define efi_md_size(md) (md->num_pages << EFI_PAGE_SHIFT) |
| 270 | |
| 271 | static inline u64 |
| 272 | kmd_end(kern_memdesc_t *kmd) |
| 273 | { |
| 274 | return (kmd->start + (kmd->num_pages << EFI_PAGE_SHIFT)); |
| 275 | } |
| 276 | |
| 277 | static inline u64 |
| 278 | efi_md_end(efi_memory_desc_t *md) |
| 279 | { |
| 280 | return (md->phys_addr + efi_md_size(md)); |
| 281 | } |
| 282 | |
| 283 | static inline int |
| 284 | efi_wb(efi_memory_desc_t *md) |
| 285 | { |
| 286 | return (md->attribute & EFI_MEMORY_WB); |
| 287 | } |
| 288 | |
| 289 | static inline int |
| 290 | efi_uc(efi_memory_desc_t *md) |
| 291 | { |
| 292 | return (md->attribute & EFI_MEMORY_UC); |
| 293 | } |
| 294 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | static void |
Tony Luck | d8c97d5 | 2005-09-08 12:39:59 -0700 | [diff] [blame] | 296 | walk (efi_freemem_callback_t callback, void *arg, u64 attr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | { |
Tony Luck | d8c97d5 | 2005-09-08 12:39:59 -0700 | [diff] [blame] | 298 | kern_memdesc_t *k; |
| 299 | u64 start, end, voff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | |
Tony Luck | d8c97d5 | 2005-09-08 12:39:59 -0700 | [diff] [blame] | 301 | voff = (attr == EFI_MEMORY_WB) ? PAGE_OFFSET : __IA64_UNCACHED_OFFSET; |
| 302 | for (k = kern_memmap; k->start != ~0UL; k++) { |
| 303 | if (k->attribute != attr) |
| 304 | continue; |
| 305 | start = PAGE_ALIGN(k->start); |
| 306 | end = (k->start + (k->num_pages << EFI_PAGE_SHIFT)) & PAGE_MASK; |
| 307 | if (start < end) |
| 308 | if ((*callback)(start + voff, end + voff, arg) < 0) |
| 309 | return; |
| 310 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | /* |
Aron Griffis | 965e7c8 | 2008-01-08 22:29:37 -0500 | [diff] [blame] | 314 | * Walk the EFI memory map and call CALLBACK once for each EFI memory |
Aron Griffis | 7d9aed2 | 2008-02-04 15:31:49 -0800 | [diff] [blame] | 315 | * descriptor that has memory that is available for OS use. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | */ |
| 317 | void |
| 318 | efi_memmap_walk (efi_freemem_callback_t callback, void *arg) |
| 319 | { |
Tony Luck | d8c97d5 | 2005-09-08 12:39:59 -0700 | [diff] [blame] | 320 | walk(callback, arg, EFI_MEMORY_WB); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | /* |
Aron Griffis | 965e7c8 | 2008-01-08 22:29:37 -0500 | [diff] [blame] | 324 | * Walk the EFI memory map and call CALLBACK once for each EFI memory |
Aron Griffis | 7d9aed2 | 2008-02-04 15:31:49 -0800 | [diff] [blame] | 325 | * descriptor that has memory that is available for uncached allocator. |
Jes Sorensen | f14f75b | 2005-06-21 17:15:02 -0700 | [diff] [blame] | 326 | */ |
Tony Luck | d8c97d5 | 2005-09-08 12:39:59 -0700 | [diff] [blame] | 327 | void |
| 328 | efi_memmap_walk_uc (efi_freemem_callback_t callback, void *arg) |
Jes Sorensen | f14f75b | 2005-06-21 17:15:02 -0700 | [diff] [blame] | 329 | { |
Tony Luck | d8c97d5 | 2005-09-08 12:39:59 -0700 | [diff] [blame] | 330 | walk(callback, arg, EFI_MEMORY_UC); |
Jes Sorensen | f14f75b | 2005-06-21 17:15:02 -0700 | [diff] [blame] | 331 | } |
| 332 | |
Jes Sorensen | f14f75b | 2005-06-21 17:15:02 -0700 | [diff] [blame] | 333 | /* |
Aron Griffis | 965e7c8 | 2008-01-08 22:29:37 -0500 | [diff] [blame] | 334 | * Look for the PAL_CODE region reported by EFI and map it using an |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | * ITR to enable safe PAL calls in virtual mode. See IA-64 Processor |
| 336 | * Abstraction Layer chapter 11 in ADAG |
| 337 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | void * |
| 339 | efi_get_pal_addr (void) |
| 340 | { |
| 341 | void *efi_map_start, *efi_map_end, *p; |
| 342 | efi_memory_desc_t *md; |
| 343 | u64 efi_desc_size; |
| 344 | int pal_code_count = 0; |
| 345 | u64 vaddr, mask; |
| 346 | |
| 347 | efi_map_start = __va(ia64_boot_param->efi_memmap); |
| 348 | efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size; |
| 349 | efi_desc_size = ia64_boot_param->efi_memdesc_size; |
| 350 | |
| 351 | for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) { |
| 352 | md = p; |
| 353 | if (md->type != EFI_PAL_CODE) |
| 354 | continue; |
| 355 | |
| 356 | if (++pal_code_count > 1) { |
Aron Griffis | 7d9aed2 | 2008-02-04 15:31:49 -0800 | [diff] [blame] | 357 | printk(KERN_ERR "Too many EFI Pal Code memory ranges, " |
| 358 | "dropped @ %lx\n", md->phys_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | continue; |
| 360 | } |
| 361 | /* |
Aron Griffis | 7d9aed2 | 2008-02-04 15:31:49 -0800 | [diff] [blame] | 362 | * The only ITLB entry in region 7 that is used is the one |
| 363 | * installed by __start(). That entry covers a 64MB range. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | */ |
| 365 | mask = ~((1 << KERNEL_TR_PAGE_SHIFT) - 1); |
| 366 | vaddr = PAGE_OFFSET + md->phys_addr; |
| 367 | |
| 368 | /* |
Aron Griffis | 7d9aed2 | 2008-02-04 15:31:49 -0800 | [diff] [blame] | 369 | * We must check that the PAL mapping won't overlap with the |
| 370 | * kernel mapping. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | * |
Aron Griffis | 7d9aed2 | 2008-02-04 15:31:49 -0800 | [diff] [blame] | 372 | * PAL code is guaranteed to be aligned on a power of 2 between |
| 373 | * 4k and 256KB and that only one ITR is needed to map it. This |
| 374 | * implies that the PAL code is always aligned on its size, |
| 375 | * i.e., the closest matching page size supported by the TLB. |
| 376 | * Therefore PAL code is guaranteed never to cross a 64MB unless |
| 377 | * it is bigger than 64MB (very unlikely!). So for now the |
| 378 | * following test is enough to determine whether or not we need |
| 379 | * a dedicated ITR for the PAL code. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | */ |
| 381 | if ((vaddr & mask) == (KERNEL_START & mask)) { |
Harvey Harrison | d4ed808 | 2008-03-04 15:15:00 -0800 | [diff] [blame^] | 382 | printk(KERN_INFO "%s: no need to install ITR for PAL code\n", |
| 383 | __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | continue; |
| 385 | } |
| 386 | |
Li Zefan | 685c7f5 | 2007-11-21 14:58:26 -0800 | [diff] [blame] | 387 | if (efi_md_size(md) > IA64_GRANULE_SIZE) |
Aron Griffis | 965e7c8 | 2008-01-08 22:29:37 -0500 | [diff] [blame] | 388 | panic("Whoa! PAL code size bigger than a granule!"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | |
| 390 | #if EFI_DEBUG |
| 391 | mask = ~((1 << IA64_GRANULE_SHIFT) - 1); |
| 392 | |
Aron Griffis | 7d9aed2 | 2008-02-04 15:31:49 -0800 | [diff] [blame] | 393 | printk(KERN_INFO "CPU %d: mapping PAL code " |
| 394 | "[0x%lx-0x%lx) into [0x%lx-0x%lx)\n", |
| 395 | smp_processor_id(), md->phys_addr, |
| 396 | md->phys_addr + efi_md_size(md), |
| 397 | vaddr & mask, (vaddr & mask) + IA64_GRANULE_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | #endif |
| 399 | return __va(md->phys_addr); |
| 400 | } |
Horms | 9473252 | 2007-02-05 10:17:38 +0900 | [diff] [blame] | 401 | printk(KERN_WARNING "%s: no PAL-code memory-descriptor found\n", |
Harvey Harrison | d4ed808 | 2008-03-04 15:15:00 -0800 | [diff] [blame^] | 402 | __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | return NULL; |
| 404 | } |
| 405 | |
| 406 | void |
| 407 | efi_map_pal_code (void) |
| 408 | { |
| 409 | void *pal_vaddr = efi_get_pal_addr (); |
| 410 | u64 psr; |
| 411 | |
| 412 | if (!pal_vaddr) |
| 413 | return; |
| 414 | |
| 415 | /* |
| 416 | * Cannot write to CRx with PSR.ic=1 |
| 417 | */ |
| 418 | psr = ia64_clear_ic(); |
Aron Griffis | 7d9aed2 | 2008-02-04 15:31:49 -0800 | [diff] [blame] | 419 | ia64_itr(0x1, IA64_TR_PALCODE, |
| 420 | GRANULEROUNDDOWN((unsigned long) pal_vaddr), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | pte_val(pfn_pte(__pa(pal_vaddr) >> PAGE_SHIFT, PAGE_KERNEL)), |
| 422 | IA64_GRANULE_SHIFT); |
| 423 | ia64_set_psr(psr); /* restore psr */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | void __init |
| 427 | efi_init (void) |
| 428 | { |
| 429 | void *efi_map_start, *efi_map_end; |
| 430 | efi_config_table_t *config_tables; |
| 431 | efi_char16_t *c16; |
| 432 | u64 efi_desc_size; |
Zou Nan hai | 9d78f43 | 2006-02-07 11:35:46 +0800 | [diff] [blame] | 433 | char *cp, vendor[100] = "unknown"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | int i; |
| 435 | |
Aron Griffis | 7d9aed2 | 2008-02-04 15:31:49 -0800 | [diff] [blame] | 436 | /* |
Aron Griffis | 965e7c8 | 2008-01-08 22:29:37 -0500 | [diff] [blame] | 437 | * It's too early to be able to use the standard kernel command line |
Aron Griffis | 7d9aed2 | 2008-02-04 15:31:49 -0800 | [diff] [blame] | 438 | * support... |
| 439 | */ |
Alon Bar-Lev | a8d91b8 | 2007-02-12 00:54:12 -0800 | [diff] [blame] | 440 | for (cp = boot_command_line; *cp; ) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | if (memcmp(cp, "mem=", 4) == 0) { |
Zou Nan hai | 9d78f43 | 2006-02-07 11:35:46 +0800 | [diff] [blame] | 442 | mem_limit = memparse(cp + 4, &cp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | } else if (memcmp(cp, "max_addr=", 9) == 0) { |
Zou Nan hai | 9d78f43 | 2006-02-07 11:35:46 +0800 | [diff] [blame] | 444 | max_addr = GRANULEROUNDDOWN(memparse(cp + 9, &cp)); |
Zou Nan hai | a7956113 | 2006-12-07 09:51:35 -0800 | [diff] [blame] | 445 | } else if (memcmp(cp, "min_addr=", 9) == 0) { |
| 446 | min_addr = GRANULEROUNDDOWN(memparse(cp + 9, &cp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | } else { |
| 448 | while (*cp != ' ' && *cp) |
| 449 | ++cp; |
| 450 | while (*cp == ' ') |
| 451 | ++cp; |
| 452 | } |
| 453 | } |
Zou Nan hai | a7956113 | 2006-12-07 09:51:35 -0800 | [diff] [blame] | 454 | if (min_addr != 0UL) |
Aron Griffis | 7d9aed2 | 2008-02-04 15:31:49 -0800 | [diff] [blame] | 455 | printk(KERN_INFO "Ignoring memory below %luMB\n", |
| 456 | min_addr >> 20); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | if (max_addr != ~0UL) |
Aron Griffis | 7d9aed2 | 2008-02-04 15:31:49 -0800 | [diff] [blame] | 458 | printk(KERN_INFO "Ignoring memory above %luMB\n", |
| 459 | max_addr >> 20); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | |
| 461 | efi.systab = __va(ia64_boot_param->efi_systab); |
| 462 | |
| 463 | /* |
| 464 | * Verify the EFI Table |
| 465 | */ |
| 466 | if (efi.systab == NULL) |
Aron Griffis | 965e7c8 | 2008-01-08 22:29:37 -0500 | [diff] [blame] | 467 | panic("Whoa! Can't find EFI system table.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) |
Aron Griffis | 965e7c8 | 2008-01-08 22:29:37 -0500 | [diff] [blame] | 469 | panic("Whoa! EFI system table signature incorrect\n"); |
Bjorn Helgaas | 873ec74 | 2007-05-08 00:29:57 -0700 | [diff] [blame] | 470 | if ((efi.systab->hdr.revision >> 16) == 0) |
| 471 | printk(KERN_WARNING "Warning: EFI system table version " |
| 472 | "%d.%02d, expected 1.00 or greater\n", |
| 473 | efi.systab->hdr.revision >> 16, |
| 474 | efi.systab->hdr.revision & 0xffff); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | |
| 476 | config_tables = __va(efi.systab->tables); |
| 477 | |
| 478 | /* Show what we know for posterity */ |
| 479 | c16 = __va(efi.systab->fw_vendor); |
| 480 | if (c16) { |
Zou Nan hai | ecdd5da | 2006-02-07 11:25:55 +0800 | [diff] [blame] | 481 | for (i = 0;i < (int) sizeof(vendor) - 1 && *c16; ++i) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | vendor[i] = *c16++; |
| 483 | vendor[i] = '\0'; |
| 484 | } |
| 485 | |
| 486 | printk(KERN_INFO "EFI v%u.%.02u by %s:", |
Aron Griffis | 7d9aed2 | 2008-02-04 15:31:49 -0800 | [diff] [blame] | 487 | efi.systab->hdr.revision >> 16, |
| 488 | efi.systab->hdr.revision & 0xffff, vendor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | |
Bjorn Helgaas | b2c99e3 | 2006-03-26 01:37:08 -0800 | [diff] [blame] | 490 | efi.mps = EFI_INVALID_TABLE_ADDR; |
| 491 | efi.acpi = EFI_INVALID_TABLE_ADDR; |
| 492 | efi.acpi20 = EFI_INVALID_TABLE_ADDR; |
| 493 | efi.smbios = EFI_INVALID_TABLE_ADDR; |
| 494 | efi.sal_systab = EFI_INVALID_TABLE_ADDR; |
| 495 | efi.boot_info = EFI_INVALID_TABLE_ADDR; |
| 496 | efi.hcdp = EFI_INVALID_TABLE_ADDR; |
| 497 | efi.uga = EFI_INVALID_TABLE_ADDR; |
| 498 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | for (i = 0; i < (int) efi.systab->nr_tables; i++) { |
| 500 | if (efi_guidcmp(config_tables[i].guid, MPS_TABLE_GUID) == 0) { |
Bjorn Helgaas | b2c99e3 | 2006-03-26 01:37:08 -0800 | [diff] [blame] | 501 | efi.mps = config_tables[i].table; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | printk(" MPS=0x%lx", config_tables[i].table); |
| 503 | } else if (efi_guidcmp(config_tables[i].guid, ACPI_20_TABLE_GUID) == 0) { |
Bjorn Helgaas | b2c99e3 | 2006-03-26 01:37:08 -0800 | [diff] [blame] | 504 | efi.acpi20 = config_tables[i].table; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | printk(" ACPI 2.0=0x%lx", config_tables[i].table); |
| 506 | } else if (efi_guidcmp(config_tables[i].guid, ACPI_TABLE_GUID) == 0) { |
Bjorn Helgaas | b2c99e3 | 2006-03-26 01:37:08 -0800 | [diff] [blame] | 507 | efi.acpi = config_tables[i].table; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | printk(" ACPI=0x%lx", config_tables[i].table); |
| 509 | } else if (efi_guidcmp(config_tables[i].guid, SMBIOS_TABLE_GUID) == 0) { |
Bjorn Helgaas | b2c99e3 | 2006-03-26 01:37:08 -0800 | [diff] [blame] | 510 | efi.smbios = config_tables[i].table; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | printk(" SMBIOS=0x%lx", config_tables[i].table); |
| 512 | } else if (efi_guidcmp(config_tables[i].guid, SAL_SYSTEM_TABLE_GUID) == 0) { |
Bjorn Helgaas | b2c99e3 | 2006-03-26 01:37:08 -0800 | [diff] [blame] | 513 | efi.sal_systab = config_tables[i].table; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | printk(" SALsystab=0x%lx", config_tables[i].table); |
| 515 | } else if (efi_guidcmp(config_tables[i].guid, HCDP_TABLE_GUID) == 0) { |
Bjorn Helgaas | b2c99e3 | 2006-03-26 01:37:08 -0800 | [diff] [blame] | 516 | efi.hcdp = config_tables[i].table; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | printk(" HCDP=0x%lx", config_tables[i].table); |
| 518 | } |
| 519 | } |
| 520 | printk("\n"); |
| 521 | |
| 522 | runtime = __va(efi.systab->runtime); |
| 523 | efi.get_time = phys_get_time; |
| 524 | efi.set_time = phys_set_time; |
| 525 | efi.get_wakeup_time = phys_get_wakeup_time; |
| 526 | efi.set_wakeup_time = phys_set_wakeup_time; |
| 527 | efi.get_variable = phys_get_variable; |
| 528 | efi.get_next_variable = phys_get_next_variable; |
| 529 | efi.set_variable = phys_set_variable; |
| 530 | efi.get_next_high_mono_count = phys_get_next_high_mono_count; |
| 531 | efi.reset_system = phys_reset_system; |
| 532 | |
| 533 | efi_map_start = __va(ia64_boot_param->efi_memmap); |
| 534 | efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size; |
| 535 | efi_desc_size = ia64_boot_param->efi_memdesc_size; |
| 536 | |
| 537 | #if EFI_DEBUG |
| 538 | /* print EFI memory map: */ |
| 539 | { |
| 540 | efi_memory_desc_t *md; |
| 541 | void *p; |
| 542 | |
Aron Griffis | 7d9aed2 | 2008-02-04 15:31:49 -0800 | [diff] [blame] | 543 | for (i = 0, p = efi_map_start; p < efi_map_end; |
| 544 | ++i, p += efi_desc_size) |
| 545 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | md = p; |
Aron Griffis | 7d9aed2 | 2008-02-04 15:31:49 -0800 | [diff] [blame] | 547 | printk("mem%02u: type=%u, attr=0x%lx, " |
| 548 | "range=[0x%016lx-0x%016lx) (%luMB)\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | i, md->type, md->attribute, md->phys_addr, |
Li Zefan | 685c7f5 | 2007-11-21 14:58:26 -0800 | [diff] [blame] | 550 | md->phys_addr + efi_md_size(md), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | md->num_pages >> (20 - EFI_PAGE_SHIFT)); |
| 552 | } |
| 553 | } |
| 554 | #endif |
| 555 | |
| 556 | efi_map_pal_code(); |
| 557 | efi_enter_virtual_mode(); |
| 558 | } |
| 559 | |
| 560 | void |
| 561 | efi_enter_virtual_mode (void) |
| 562 | { |
| 563 | void *efi_map_start, *efi_map_end, *p; |
| 564 | efi_memory_desc_t *md; |
| 565 | efi_status_t status; |
| 566 | u64 efi_desc_size; |
| 567 | |
| 568 | efi_map_start = __va(ia64_boot_param->efi_memmap); |
| 569 | efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size; |
| 570 | efi_desc_size = ia64_boot_param->efi_memdesc_size; |
| 571 | |
| 572 | for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) { |
| 573 | md = p; |
| 574 | if (md->attribute & EFI_MEMORY_RUNTIME) { |
| 575 | /* |
Aron Griffis | 7d9aed2 | 2008-02-04 15:31:49 -0800 | [diff] [blame] | 576 | * Some descriptors have multiple bits set, so the |
| 577 | * order of the tests is relevant. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | */ |
| 579 | if (md->attribute & EFI_MEMORY_WB) { |
| 580 | md->virt_addr = (u64) __va(md->phys_addr); |
| 581 | } else if (md->attribute & EFI_MEMORY_UC) { |
| 582 | md->virt_addr = (u64) ioremap(md->phys_addr, 0); |
| 583 | } else if (md->attribute & EFI_MEMORY_WC) { |
| 584 | #if 0 |
Aron Griffis | 7d9aed2 | 2008-02-04 15:31:49 -0800 | [diff] [blame] | 585 | md->virt_addr = ia64_remap(md->phys_addr, |
| 586 | (_PAGE_A | |
| 587 | _PAGE_P | |
| 588 | _PAGE_D | |
| 589 | _PAGE_MA_WC | |
| 590 | _PAGE_PL_0 | |
| 591 | _PAGE_AR_RW)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | #else |
| 593 | printk(KERN_INFO "EFI_MEMORY_WC mapping\n"); |
| 594 | md->virt_addr = (u64) ioremap(md->phys_addr, 0); |
| 595 | #endif |
| 596 | } else if (md->attribute & EFI_MEMORY_WT) { |
| 597 | #if 0 |
Aron Griffis | 7d9aed2 | 2008-02-04 15:31:49 -0800 | [diff] [blame] | 598 | md->virt_addr = ia64_remap(md->phys_addr, |
| 599 | (_PAGE_A | |
| 600 | _PAGE_P | |
| 601 | _PAGE_D | |
| 602 | _PAGE_MA_WT | |
| 603 | _PAGE_PL_0 | |
| 604 | _PAGE_AR_RW)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | #else |
| 606 | printk(KERN_INFO "EFI_MEMORY_WT mapping\n"); |
| 607 | md->virt_addr = (u64) ioremap(md->phys_addr, 0); |
| 608 | #endif |
| 609 | } |
| 610 | } |
| 611 | } |
| 612 | |
| 613 | status = efi_call_phys(__va(runtime->set_virtual_address_map), |
| 614 | ia64_boot_param->efi_memmap_size, |
Aron Griffis | 7d9aed2 | 2008-02-04 15:31:49 -0800 | [diff] [blame] | 615 | efi_desc_size, |
| 616 | ia64_boot_param->efi_memdesc_version, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | ia64_boot_param->efi_memmap); |
| 618 | if (status != EFI_SUCCESS) { |
Aron Griffis | 7d9aed2 | 2008-02-04 15:31:49 -0800 | [diff] [blame] | 619 | printk(KERN_WARNING "warning: unable to switch EFI into " |
| 620 | "virtual mode (status=%lu)\n", status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | return; |
| 622 | } |
| 623 | |
| 624 | /* |
Aron Griffis | 7d9aed2 | 2008-02-04 15:31:49 -0800 | [diff] [blame] | 625 | * Now that EFI is in virtual mode, we call the EFI functions more |
| 626 | * efficiently: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | */ |
| 628 | efi.get_time = virt_get_time; |
| 629 | efi.set_time = virt_set_time; |
| 630 | efi.get_wakeup_time = virt_get_wakeup_time; |
| 631 | efi.set_wakeup_time = virt_set_wakeup_time; |
| 632 | efi.get_variable = virt_get_variable; |
| 633 | efi.get_next_variable = virt_get_next_variable; |
| 634 | efi.set_variable = virt_set_variable; |
| 635 | efi.get_next_high_mono_count = virt_get_next_high_mono_count; |
| 636 | efi.reset_system = virt_reset_system; |
| 637 | } |
| 638 | |
| 639 | /* |
Aron Griffis | 7d9aed2 | 2008-02-04 15:31:49 -0800 | [diff] [blame] | 640 | * Walk the EFI memory map looking for the I/O port range. There can only be |
| 641 | * one entry of this type, other I/O port ranges should be described via ACPI. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | */ |
| 643 | u64 |
| 644 | efi_get_iobase (void) |
| 645 | { |
| 646 | void *efi_map_start, *efi_map_end, *p; |
| 647 | efi_memory_desc_t *md; |
| 648 | u64 efi_desc_size; |
| 649 | |
| 650 | efi_map_start = __va(ia64_boot_param->efi_memmap); |
| 651 | efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size; |
| 652 | efi_desc_size = ia64_boot_param->efi_memdesc_size; |
| 653 | |
| 654 | for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) { |
| 655 | md = p; |
| 656 | if (md->type == EFI_MEMORY_MAPPED_IO_PORT_SPACE) { |
| 657 | if (md->attribute & EFI_MEMORY_UC) |
| 658 | return md->phys_addr; |
| 659 | } |
| 660 | } |
| 661 | return 0; |
| 662 | } |
| 663 | |
Bjorn Helgaas | 32e62c6 | 2006-05-05 17:19:50 -0600 | [diff] [blame] | 664 | static struct kern_memdesc * |
| 665 | kern_memory_descriptor (unsigned long phys_addr) |
| 666 | { |
| 667 | struct kern_memdesc *md; |
| 668 | |
| 669 | for (md = kern_memmap; md->start != ~0UL; md++) { |
| 670 | if (phys_addr - md->start < (md->num_pages << EFI_PAGE_SHIFT)) |
| 671 | return md; |
| 672 | } |
Keith Owens | e037cda | 2006-07-17 15:41:59 +1000 | [diff] [blame] | 673 | return NULL; |
Bjorn Helgaas | 32e62c6 | 2006-05-05 17:19:50 -0600 | [diff] [blame] | 674 | } |
| 675 | |
Bjorn Helgaas | 80851ef | 2006-01-08 01:04:13 -0800 | [diff] [blame] | 676 | static efi_memory_desc_t * |
| 677 | efi_memory_descriptor (unsigned long phys_addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 | { |
| 679 | void *efi_map_start, *efi_map_end, *p; |
| 680 | efi_memory_desc_t *md; |
| 681 | u64 efi_desc_size; |
| 682 | |
| 683 | efi_map_start = __va(ia64_boot_param->efi_memmap); |
| 684 | efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size; |
| 685 | efi_desc_size = ia64_boot_param->efi_memdesc_size; |
| 686 | |
| 687 | for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) { |
| 688 | md = p; |
| 689 | |
Li Zefan | 685c7f5 | 2007-11-21 14:58:26 -0800 | [diff] [blame] | 690 | if (phys_addr - md->phys_addr < efi_md_size(md)) |
Bjorn Helgaas | 80851ef | 2006-01-08 01:04:13 -0800 | [diff] [blame] | 691 | return md; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | } |
Keith Owens | e037cda | 2006-07-17 15:41:59 +1000 | [diff] [blame] | 693 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | } |
| 695 | |
Bjorn Helgaas | 6d40fc5 | 2007-03-30 10:35:43 -0600 | [diff] [blame] | 696 | static int |
| 697 | efi_memmap_intersects (unsigned long phys_addr, unsigned long size) |
| 698 | { |
| 699 | void *efi_map_start, *efi_map_end, *p; |
| 700 | efi_memory_desc_t *md; |
| 701 | u64 efi_desc_size; |
| 702 | unsigned long end; |
| 703 | |
| 704 | efi_map_start = __va(ia64_boot_param->efi_memmap); |
| 705 | efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size; |
| 706 | efi_desc_size = ia64_boot_param->efi_memdesc_size; |
| 707 | |
| 708 | end = phys_addr + size; |
| 709 | |
| 710 | for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) { |
| 711 | md = p; |
Bjorn Helgaas | 6d40fc5 | 2007-03-30 10:35:43 -0600 | [diff] [blame] | 712 | if (md->phys_addr < end && efi_md_end(md) > phys_addr) |
| 713 | return 1; |
| 714 | } |
| 715 | return 0; |
| 716 | } |
| 717 | |
Bjorn Helgaas | 80851ef | 2006-01-08 01:04:13 -0800 | [diff] [blame] | 718 | u32 |
| 719 | efi_mem_type (unsigned long phys_addr) |
| 720 | { |
| 721 | efi_memory_desc_t *md = efi_memory_descriptor(phys_addr); |
| 722 | |
| 723 | if (md) |
| 724 | return md->type; |
| 725 | return 0; |
| 726 | } |
| 727 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | u64 |
| 729 | efi_mem_attributes (unsigned long phys_addr) |
| 730 | { |
Bjorn Helgaas | 80851ef | 2006-01-08 01:04:13 -0800 | [diff] [blame] | 731 | efi_memory_desc_t *md = efi_memory_descriptor(phys_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 732 | |
Bjorn Helgaas | 80851ef | 2006-01-08 01:04:13 -0800 | [diff] [blame] | 733 | if (md) |
| 734 | return md->attribute; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 735 | return 0; |
| 736 | } |
| 737 | EXPORT_SYMBOL(efi_mem_attributes); |
| 738 | |
Bjorn Helgaas | 32e62c6 | 2006-05-05 17:19:50 -0600 | [diff] [blame] | 739 | u64 |
| 740 | efi_mem_attribute (unsigned long phys_addr, unsigned long size) |
Bjorn Helgaas | 80851ef | 2006-01-08 01:04:13 -0800 | [diff] [blame] | 741 | { |
Bjorn Helgaas | 136939a | 2006-03-26 01:37:05 -0800 | [diff] [blame] | 742 | unsigned long end = phys_addr + size; |
Bjorn Helgaas | 80851ef | 2006-01-08 01:04:13 -0800 | [diff] [blame] | 743 | efi_memory_desc_t *md = efi_memory_descriptor(phys_addr); |
Bjorn Helgaas | 32e62c6 | 2006-05-05 17:19:50 -0600 | [diff] [blame] | 744 | u64 attr; |
| 745 | |
| 746 | if (!md) |
| 747 | return 0; |
Bjorn Helgaas | 80851ef | 2006-01-08 01:04:13 -0800 | [diff] [blame] | 748 | |
Bjorn Helgaas | 136939a | 2006-03-26 01:37:05 -0800 | [diff] [blame] | 749 | /* |
Bjorn Helgaas | 32e62c6 | 2006-05-05 17:19:50 -0600 | [diff] [blame] | 750 | * EFI_MEMORY_RUNTIME is not a memory attribute; it just tells |
| 751 | * the kernel that firmware needs this region mapped. |
Bjorn Helgaas | 136939a | 2006-03-26 01:37:05 -0800 | [diff] [blame] | 752 | */ |
Bjorn Helgaas | 32e62c6 | 2006-05-05 17:19:50 -0600 | [diff] [blame] | 753 | attr = md->attribute & ~EFI_MEMORY_RUNTIME; |
Bjorn Helgaas | 80851ef | 2006-01-08 01:04:13 -0800 | [diff] [blame] | 754 | do { |
Bjorn Helgaas | 136939a | 2006-03-26 01:37:05 -0800 | [diff] [blame] | 755 | unsigned long md_end = efi_md_end(md); |
| 756 | |
| 757 | if (end <= md_end) |
Bjorn Helgaas | 32e62c6 | 2006-05-05 17:19:50 -0600 | [diff] [blame] | 758 | return attr; |
Bjorn Helgaas | 80851ef | 2006-01-08 01:04:13 -0800 | [diff] [blame] | 759 | |
| 760 | md = efi_memory_descriptor(md_end); |
Bjorn Helgaas | 32e62c6 | 2006-05-05 17:19:50 -0600 | [diff] [blame] | 761 | if (!md || (md->attribute & ~EFI_MEMORY_RUNTIME) != attr) |
Bjorn Helgaas | 136939a | 2006-03-26 01:37:05 -0800 | [diff] [blame] | 762 | return 0; |
Bjorn Helgaas | 80851ef | 2006-01-08 01:04:13 -0800 | [diff] [blame] | 763 | } while (md); |
Aron Griffis | 410ab51 | 2008-01-08 22:29:38 -0500 | [diff] [blame] | 764 | return 0; /* never reached */ |
Bjorn Helgaas | 80851ef | 2006-01-08 01:04:13 -0800 | [diff] [blame] | 765 | } |
| 766 | |
Bjorn Helgaas | 32e62c6 | 2006-05-05 17:19:50 -0600 | [diff] [blame] | 767 | u64 |
| 768 | kern_mem_attribute (unsigned long phys_addr, unsigned long size) |
| 769 | { |
| 770 | unsigned long end = phys_addr + size; |
| 771 | struct kern_memdesc *md; |
| 772 | u64 attr; |
| 773 | |
| 774 | /* |
| 775 | * This is a hack for ioremap calls before we set up kern_memmap. |
| 776 | * Maybe we should do efi_memmap_init() earlier instead. |
| 777 | */ |
| 778 | if (!kern_memmap) { |
| 779 | attr = efi_mem_attribute(phys_addr, size); |
| 780 | if (attr & EFI_MEMORY_WB) |
| 781 | return EFI_MEMORY_WB; |
| 782 | return 0; |
| 783 | } |
| 784 | |
| 785 | md = kern_memory_descriptor(phys_addr); |
| 786 | if (!md) |
| 787 | return 0; |
| 788 | |
| 789 | attr = md->attribute; |
| 790 | do { |
| 791 | unsigned long md_end = kmd_end(md); |
| 792 | |
| 793 | if (end <= md_end) |
| 794 | return attr; |
| 795 | |
| 796 | md = kern_memory_descriptor(md_end); |
| 797 | if (!md || md->attribute != attr) |
| 798 | return 0; |
| 799 | } while (md); |
Aron Griffis | 410ab51 | 2008-01-08 22:29:38 -0500 | [diff] [blame] | 800 | return 0; /* never reached */ |
Bjorn Helgaas | 32e62c6 | 2006-05-05 17:19:50 -0600 | [diff] [blame] | 801 | } |
| 802 | EXPORT_SYMBOL(kern_mem_attribute); |
| 803 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 804 | int |
Bjorn Helgaas | 136939a | 2006-03-26 01:37:05 -0800 | [diff] [blame] | 805 | valid_phys_addr_range (unsigned long phys_addr, unsigned long size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 806 | { |
Bjorn Helgaas | 32e62c6 | 2006-05-05 17:19:50 -0600 | [diff] [blame] | 807 | u64 attr; |
| 808 | |
| 809 | /* |
| 810 | * /dev/mem reads and writes use copy_to_user(), which implicitly |
| 811 | * uses a granule-sized kernel identity mapping. It's really |
| 812 | * only safe to do this for regions in kern_memmap. For more |
| 813 | * details, see Documentation/ia64/aliasing.txt. |
| 814 | */ |
| 815 | attr = kern_mem_attribute(phys_addr, size); |
| 816 | if (attr & EFI_MEMORY_WB || attr & EFI_MEMORY_UC) |
| 817 | return 1; |
| 818 | return 0; |
Bjorn Helgaas | 80851ef | 2006-01-08 01:04:13 -0800 | [diff] [blame] | 819 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | |
Bjorn Helgaas | 80851ef | 2006-01-08 01:04:13 -0800 | [diff] [blame] | 821 | int |
Lennert Buytenhek | 06c67be | 2006-07-10 04:45:27 -0700 | [diff] [blame] | 822 | valid_mmap_phys_addr_range (unsigned long pfn, unsigned long size) |
Bjorn Helgaas | 80851ef | 2006-01-08 01:04:13 -0800 | [diff] [blame] | 823 | { |
Bjorn Helgaas | 6d40fc5 | 2007-03-30 10:35:43 -0600 | [diff] [blame] | 824 | unsigned long phys_addr = pfn << PAGE_SHIFT; |
| 825 | u64 attr; |
| 826 | |
| 827 | attr = efi_mem_attribute(phys_addr, size); |
| 828 | |
Bjorn Helgaas | 32e62c6 | 2006-05-05 17:19:50 -0600 | [diff] [blame] | 829 | /* |
Bjorn Helgaas | 6d40fc5 | 2007-03-30 10:35:43 -0600 | [diff] [blame] | 830 | * /dev/mem mmap uses normal user pages, so we don't need the entire |
| 831 | * granule, but the entire region we're mapping must support the same |
| 832 | * attribute. |
Bjorn Helgaas | 32e62c6 | 2006-05-05 17:19:50 -0600 | [diff] [blame] | 833 | */ |
Bjorn Helgaas | 6d40fc5 | 2007-03-30 10:35:43 -0600 | [diff] [blame] | 834 | if (attr & EFI_MEMORY_WB || attr & EFI_MEMORY_UC) |
| 835 | return 1; |
| 836 | |
| 837 | /* |
| 838 | * Intel firmware doesn't tell us about all the MMIO regions, so |
| 839 | * in general we have to allow mmap requests. But if EFI *does* |
| 840 | * tell us about anything inside this region, we should deny it. |
| 841 | * The user can always map a smaller region to avoid the overlap. |
| 842 | */ |
| 843 | if (efi_memmap_intersects(phys_addr, size)) |
| 844 | return 0; |
| 845 | |
Bjorn Helgaas | 32e62c6 | 2006-05-05 17:19:50 -0600 | [diff] [blame] | 846 | return 1; |
| 847 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 848 | |
Bjorn Helgaas | 32e62c6 | 2006-05-05 17:19:50 -0600 | [diff] [blame] | 849 | pgprot_t |
| 850 | phys_mem_access_prot(struct file *file, unsigned long pfn, unsigned long size, |
| 851 | pgprot_t vma_prot) |
| 852 | { |
| 853 | unsigned long phys_addr = pfn << PAGE_SHIFT; |
| 854 | u64 attr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 855 | |
Bjorn Helgaas | 32e62c6 | 2006-05-05 17:19:50 -0600 | [diff] [blame] | 856 | /* |
| 857 | * For /dev/mem mmap, we use user mappings, but if the region is |
| 858 | * in kern_memmap (and hence may be covered by a kernel mapping), |
| 859 | * we must use the same attribute as the kernel mapping. |
| 860 | */ |
| 861 | attr = kern_mem_attribute(phys_addr, size); |
| 862 | if (attr & EFI_MEMORY_WB) |
| 863 | return pgprot_cacheable(vma_prot); |
| 864 | else if (attr & EFI_MEMORY_UC) |
| 865 | return pgprot_noncached(vma_prot); |
| 866 | |
| 867 | /* |
| 868 | * Some chipsets don't support UC access to memory. If |
| 869 | * WB is supported, we prefer that. |
| 870 | */ |
| 871 | if (efi_mem_attribute(phys_addr, size) & EFI_MEMORY_WB) |
| 872 | return pgprot_cacheable(vma_prot); |
| 873 | |
| 874 | return pgprot_noncached(vma_prot); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 875 | } |
| 876 | |
| 877 | int __init |
| 878 | efi_uart_console_only(void) |
| 879 | { |
| 880 | efi_status_t status; |
| 881 | char *s, name[] = "ConOut"; |
| 882 | efi_guid_t guid = EFI_GLOBAL_VARIABLE_GUID; |
| 883 | efi_char16_t *utf16, name_utf16[32]; |
| 884 | unsigned char data[1024]; |
| 885 | unsigned long size = sizeof(data); |
| 886 | struct efi_generic_dev_path *hdr, *end_addr; |
| 887 | int uart = 0; |
| 888 | |
| 889 | /* Convert to UTF-16 */ |
| 890 | utf16 = name_utf16; |
| 891 | s = name; |
| 892 | while (*s) |
| 893 | *utf16++ = *s++ & 0x7f; |
| 894 | *utf16 = 0; |
| 895 | |
| 896 | status = efi.get_variable(name_utf16, &guid, NULL, &size, data); |
| 897 | if (status != EFI_SUCCESS) { |
| 898 | printk(KERN_ERR "No EFI %s variable?\n", name); |
| 899 | return 0; |
| 900 | } |
| 901 | |
| 902 | hdr = (struct efi_generic_dev_path *) data; |
| 903 | end_addr = (struct efi_generic_dev_path *) ((u8 *) data + size); |
| 904 | while (hdr < end_addr) { |
| 905 | if (hdr->type == EFI_DEV_MSG && |
| 906 | hdr->sub_type == EFI_DEV_MSG_UART) |
| 907 | uart = 1; |
| 908 | else if (hdr->type == EFI_DEV_END_PATH || |
| 909 | hdr->type == EFI_DEV_END_PATH2) { |
| 910 | if (!uart) |
| 911 | return 0; |
| 912 | if (hdr->sub_type == EFI_DEV_END_ENTIRE) |
| 913 | return 1; |
| 914 | uart = 0; |
| 915 | } |
Aron Griffis | 7d9aed2 | 2008-02-04 15:31:49 -0800 | [diff] [blame] | 916 | hdr = (struct efi_generic_dev_path *)((u8 *) hdr + hdr->length); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 917 | } |
| 918 | printk(KERN_ERR "Malformed %s value\n", name); |
| 919 | return 0; |
| 920 | } |
Tony Luck | d8c97d5 | 2005-09-08 12:39:59 -0700 | [diff] [blame] | 921 | |
Tony Luck | d8c97d5 | 2005-09-08 12:39:59 -0700 | [diff] [blame] | 922 | /* |
| 923 | * Look for the first granule aligned memory descriptor memory |
| 924 | * that is big enough to hold EFI memory map. Make sure this |
| 925 | * descriptor is atleast granule sized so it does not get trimmed |
| 926 | */ |
| 927 | struct kern_memdesc * |
| 928 | find_memmap_space (void) |
| 929 | { |
| 930 | u64 contig_low=0, contig_high=0; |
| 931 | u64 as = 0, ae; |
| 932 | void *efi_map_start, *efi_map_end, *p, *q; |
| 933 | efi_memory_desc_t *md, *pmd = NULL, *check_md; |
| 934 | u64 space_needed, efi_desc_size; |
| 935 | unsigned long total_mem = 0; |
| 936 | |
| 937 | efi_map_start = __va(ia64_boot_param->efi_memmap); |
| 938 | efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size; |
| 939 | efi_desc_size = ia64_boot_param->efi_memdesc_size; |
| 940 | |
| 941 | /* |
| 942 | * Worst case: we need 3 kernel descriptors for each efi descriptor |
| 943 | * (if every entry has a WB part in the middle, and UC head and tail), |
| 944 | * plus one for the end marker. |
| 945 | */ |
| 946 | space_needed = sizeof(kern_memdesc_t) * |
| 947 | (3 * (ia64_boot_param->efi_memmap_size/efi_desc_size) + 1); |
| 948 | |
| 949 | for (p = efi_map_start; p < efi_map_end; pmd = md, p += efi_desc_size) { |
| 950 | md = p; |
| 951 | if (!efi_wb(md)) { |
| 952 | continue; |
| 953 | } |
Aron Griffis | 7d9aed2 | 2008-02-04 15:31:49 -0800 | [diff] [blame] | 954 | if (pmd == NULL || !efi_wb(pmd) || |
| 955 | efi_md_end(pmd) != md->phys_addr) { |
Tony Luck | d8c97d5 | 2005-09-08 12:39:59 -0700 | [diff] [blame] | 956 | contig_low = GRANULEROUNDUP(md->phys_addr); |
| 957 | contig_high = efi_md_end(md); |
Aron Griffis | 7d9aed2 | 2008-02-04 15:31:49 -0800 | [diff] [blame] | 958 | for (q = p + efi_desc_size; q < efi_map_end; |
| 959 | q += efi_desc_size) { |
Tony Luck | d8c97d5 | 2005-09-08 12:39:59 -0700 | [diff] [blame] | 960 | check_md = q; |
| 961 | if (!efi_wb(check_md)) |
| 962 | break; |
| 963 | if (contig_high != check_md->phys_addr) |
| 964 | break; |
| 965 | contig_high = efi_md_end(check_md); |
| 966 | } |
| 967 | contig_high = GRANULEROUNDDOWN(contig_high); |
| 968 | } |
Christoph Lameter | 66888a6 | 2006-12-04 14:58:35 -0800 | [diff] [blame] | 969 | if (!is_memory_available(md) || md->type == EFI_LOADER_DATA) |
Tony Luck | d8c97d5 | 2005-09-08 12:39:59 -0700 | [diff] [blame] | 970 | continue; |
| 971 | |
| 972 | /* Round ends inward to granule boundaries */ |
| 973 | as = max(contig_low, md->phys_addr); |
| 974 | ae = min(contig_high, efi_md_end(md)); |
| 975 | |
Zou Nan hai | a7956113 | 2006-12-07 09:51:35 -0800 | [diff] [blame] | 976 | /* keep within max_addr= and min_addr= command line arg */ |
| 977 | as = max(as, min_addr); |
Tony Luck | d8c97d5 | 2005-09-08 12:39:59 -0700 | [diff] [blame] | 978 | ae = min(ae, max_addr); |
| 979 | if (ae <= as) |
| 980 | continue; |
| 981 | |
| 982 | /* avoid going over mem= command line arg */ |
| 983 | if (total_mem + (ae - as) > mem_limit) |
| 984 | ae -= total_mem + (ae - as) - mem_limit; |
| 985 | |
| 986 | if (ae <= as) |
| 987 | continue; |
| 988 | |
| 989 | if (ae - as > space_needed) |
| 990 | break; |
| 991 | } |
| 992 | if (p >= efi_map_end) |
| 993 | panic("Can't allocate space for kernel memory descriptors"); |
| 994 | |
| 995 | return __va(as); |
| 996 | } |
| 997 | |
| 998 | /* |
| 999 | * Walk the EFI memory map and gather all memory available for kernel |
| 1000 | * to use. We can allocate partial granules only if the unavailable |
| 1001 | * parts exist, and are WB. |
| 1002 | */ |
Bernhard Walle | cb38085 | 2007-10-18 23:41:00 -0700 | [diff] [blame] | 1003 | unsigned long |
Tony Luck | d8c97d5 | 2005-09-08 12:39:59 -0700 | [diff] [blame] | 1004 | efi_memmap_init(unsigned long *s, unsigned long *e) |
| 1005 | { |
Keith Owens | e037cda | 2006-07-17 15:41:59 +1000 | [diff] [blame] | 1006 | struct kern_memdesc *k, *prev = NULL; |
Tony Luck | d8c97d5 | 2005-09-08 12:39:59 -0700 | [diff] [blame] | 1007 | u64 contig_low=0, contig_high=0; |
| 1008 | u64 as, ae, lim; |
| 1009 | void *efi_map_start, *efi_map_end, *p, *q; |
| 1010 | efi_memory_desc_t *md, *pmd = NULL, *check_md; |
| 1011 | u64 efi_desc_size; |
| 1012 | unsigned long total_mem = 0; |
| 1013 | |
| 1014 | k = kern_memmap = find_memmap_space(); |
| 1015 | |
| 1016 | efi_map_start = __va(ia64_boot_param->efi_memmap); |
| 1017 | efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size; |
| 1018 | efi_desc_size = ia64_boot_param->efi_memdesc_size; |
| 1019 | |
| 1020 | for (p = efi_map_start; p < efi_map_end; pmd = md, p += efi_desc_size) { |
| 1021 | md = p; |
| 1022 | if (!efi_wb(md)) { |
Aron Griffis | 7d9aed2 | 2008-02-04 15:31:49 -0800 | [diff] [blame] | 1023 | if (efi_uc(md) && |
| 1024 | (md->type == EFI_CONVENTIONAL_MEMORY || |
| 1025 | md->type == EFI_BOOT_SERVICES_DATA)) { |
Tony Luck | d8c97d5 | 2005-09-08 12:39:59 -0700 | [diff] [blame] | 1026 | k->attribute = EFI_MEMORY_UC; |
| 1027 | k->start = md->phys_addr; |
| 1028 | k->num_pages = md->num_pages; |
| 1029 | k++; |
| 1030 | } |
| 1031 | continue; |
| 1032 | } |
Aron Griffis | 7d9aed2 | 2008-02-04 15:31:49 -0800 | [diff] [blame] | 1033 | if (pmd == NULL || !efi_wb(pmd) || |
| 1034 | efi_md_end(pmd) != md->phys_addr) { |
Tony Luck | d8c97d5 | 2005-09-08 12:39:59 -0700 | [diff] [blame] | 1035 | contig_low = GRANULEROUNDUP(md->phys_addr); |
| 1036 | contig_high = efi_md_end(md); |
Aron Griffis | 7d9aed2 | 2008-02-04 15:31:49 -0800 | [diff] [blame] | 1037 | for (q = p + efi_desc_size; q < efi_map_end; |
| 1038 | q += efi_desc_size) { |
Tony Luck | d8c97d5 | 2005-09-08 12:39:59 -0700 | [diff] [blame] | 1039 | check_md = q; |
| 1040 | if (!efi_wb(check_md)) |
| 1041 | break; |
| 1042 | if (contig_high != check_md->phys_addr) |
| 1043 | break; |
| 1044 | contig_high = efi_md_end(check_md); |
| 1045 | } |
| 1046 | contig_high = GRANULEROUNDDOWN(contig_high); |
| 1047 | } |
Christoph Lameter | 66888a6 | 2006-12-04 14:58:35 -0800 | [diff] [blame] | 1048 | if (!is_memory_available(md)) |
Tony Luck | d8c97d5 | 2005-09-08 12:39:59 -0700 | [diff] [blame] | 1049 | continue; |
| 1050 | |
Tony Luck | e55fdf1 | 2007-03-07 16:13:25 -0800 | [diff] [blame] | 1051 | #ifdef CONFIG_CRASH_DUMP |
| 1052 | /* saved_max_pfn should ignore max_addr= command line arg */ |
| 1053 | if (saved_max_pfn < (efi_md_end(md) >> PAGE_SHIFT)) |
| 1054 | saved_max_pfn = (efi_md_end(md) >> PAGE_SHIFT); |
| 1055 | #endif |
Tony Luck | d8c97d5 | 2005-09-08 12:39:59 -0700 | [diff] [blame] | 1056 | /* |
| 1057 | * Round ends inward to granule boundaries |
| 1058 | * Give trimmings to uncached allocator |
| 1059 | */ |
| 1060 | if (md->phys_addr < contig_low) { |
| 1061 | lim = min(efi_md_end(md), contig_low); |
| 1062 | if (efi_uc(md)) { |
Aron Griffis | 7d9aed2 | 2008-02-04 15:31:49 -0800 | [diff] [blame] | 1063 | if (k > kern_memmap && |
| 1064 | (k-1)->attribute == EFI_MEMORY_UC && |
Tony Luck | d8c97d5 | 2005-09-08 12:39:59 -0700 | [diff] [blame] | 1065 | kmd_end(k-1) == md->phys_addr) { |
Aron Griffis | 7d9aed2 | 2008-02-04 15:31:49 -0800 | [diff] [blame] | 1066 | (k-1)->num_pages += |
| 1067 | (lim - md->phys_addr) |
| 1068 | >> EFI_PAGE_SHIFT; |
Tony Luck | d8c97d5 | 2005-09-08 12:39:59 -0700 | [diff] [blame] | 1069 | } else { |
| 1070 | k->attribute = EFI_MEMORY_UC; |
| 1071 | k->start = md->phys_addr; |
Aron Griffis | 7d9aed2 | 2008-02-04 15:31:49 -0800 | [diff] [blame] | 1072 | k->num_pages = (lim - md->phys_addr) |
| 1073 | >> EFI_PAGE_SHIFT; |
Tony Luck | d8c97d5 | 2005-09-08 12:39:59 -0700 | [diff] [blame] | 1074 | k++; |
| 1075 | } |
| 1076 | } |
| 1077 | as = contig_low; |
| 1078 | } else |
| 1079 | as = md->phys_addr; |
| 1080 | |
| 1081 | if (efi_md_end(md) > contig_high) { |
| 1082 | lim = max(md->phys_addr, contig_high); |
| 1083 | if (efi_uc(md)) { |
| 1084 | if (lim == md->phys_addr && k > kern_memmap && |
| 1085 | (k-1)->attribute == EFI_MEMORY_UC && |
| 1086 | kmd_end(k-1) == md->phys_addr) { |
| 1087 | (k-1)->num_pages += md->num_pages; |
| 1088 | } else { |
| 1089 | k->attribute = EFI_MEMORY_UC; |
| 1090 | k->start = lim; |
Aron Griffis | 7d9aed2 | 2008-02-04 15:31:49 -0800 | [diff] [blame] | 1091 | k->num_pages = (efi_md_end(md) - lim) |
| 1092 | >> EFI_PAGE_SHIFT; |
Tony Luck | d8c97d5 | 2005-09-08 12:39:59 -0700 | [diff] [blame] | 1093 | k++; |
| 1094 | } |
| 1095 | } |
| 1096 | ae = contig_high; |
| 1097 | } else |
| 1098 | ae = efi_md_end(md); |
| 1099 | |
Zou Nan hai | a7956113 | 2006-12-07 09:51:35 -0800 | [diff] [blame] | 1100 | /* keep within max_addr= and min_addr= command line arg */ |
| 1101 | as = max(as, min_addr); |
Tony Luck | d8c97d5 | 2005-09-08 12:39:59 -0700 | [diff] [blame] | 1102 | ae = min(ae, max_addr); |
| 1103 | if (ae <= as) |
| 1104 | continue; |
| 1105 | |
| 1106 | /* avoid going over mem= command line arg */ |
| 1107 | if (total_mem + (ae - as) > mem_limit) |
| 1108 | ae -= total_mem + (ae - as) - mem_limit; |
| 1109 | |
| 1110 | if (ae <= as) |
| 1111 | continue; |
| 1112 | if (prev && kmd_end(prev) == md->phys_addr) { |
| 1113 | prev->num_pages += (ae - as) >> EFI_PAGE_SHIFT; |
| 1114 | total_mem += ae - as; |
| 1115 | continue; |
| 1116 | } |
| 1117 | k->attribute = EFI_MEMORY_WB; |
| 1118 | k->start = as; |
| 1119 | k->num_pages = (ae - as) >> EFI_PAGE_SHIFT; |
| 1120 | total_mem += ae - as; |
| 1121 | prev = k++; |
| 1122 | } |
| 1123 | k->start = ~0L; /* end-marker */ |
| 1124 | |
| 1125 | /* reserve the memory we are using for kern_memmap */ |
| 1126 | *s = (u64)kern_memmap; |
| 1127 | *e = (u64)++k; |
Bernhard Walle | cb38085 | 2007-10-18 23:41:00 -0700 | [diff] [blame] | 1128 | |
| 1129 | return total_mem; |
Tony Luck | d8c97d5 | 2005-09-08 12:39:59 -0700 | [diff] [blame] | 1130 | } |
Khalid Aziz | be37912 | 2005-09-19 15:42:36 -0700 | [diff] [blame] | 1131 | |
| 1132 | void |
| 1133 | efi_initialize_iomem_resources(struct resource *code_resource, |
Bernhard Walle | 00bf409 | 2007-10-21 16:42:01 -0700 | [diff] [blame] | 1134 | struct resource *data_resource, |
| 1135 | struct resource *bss_resource) |
Khalid Aziz | be37912 | 2005-09-19 15:42:36 -0700 | [diff] [blame] | 1136 | { |
| 1137 | struct resource *res; |
| 1138 | void *efi_map_start, *efi_map_end, *p; |
| 1139 | efi_memory_desc_t *md; |
| 1140 | u64 efi_desc_size; |
| 1141 | char *name; |
| 1142 | unsigned long flags; |
| 1143 | |
| 1144 | efi_map_start = __va(ia64_boot_param->efi_memmap); |
| 1145 | efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size; |
| 1146 | efi_desc_size = ia64_boot_param->efi_memdesc_size; |
| 1147 | |
| 1148 | res = NULL; |
| 1149 | |
| 1150 | for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) { |
| 1151 | md = p; |
| 1152 | |
| 1153 | if (md->num_pages == 0) /* should not happen */ |
| 1154 | continue; |
| 1155 | |
Yasunori Goto | 887c3cb | 2007-11-14 16:59:20 -0800 | [diff] [blame] | 1156 | flags = IORESOURCE_MEM | IORESOURCE_BUSY; |
Khalid Aziz | be37912 | 2005-09-19 15:42:36 -0700 | [diff] [blame] | 1157 | switch (md->type) { |
| 1158 | |
| 1159 | case EFI_MEMORY_MAPPED_IO: |
| 1160 | case EFI_MEMORY_MAPPED_IO_PORT_SPACE: |
| 1161 | continue; |
| 1162 | |
| 1163 | case EFI_LOADER_CODE: |
| 1164 | case EFI_LOADER_DATA: |
| 1165 | case EFI_BOOT_SERVICES_DATA: |
| 1166 | case EFI_BOOT_SERVICES_CODE: |
| 1167 | case EFI_CONVENTIONAL_MEMORY: |
| 1168 | if (md->attribute & EFI_MEMORY_WP) { |
| 1169 | name = "System ROM"; |
| 1170 | flags |= IORESOURCE_READONLY; |
| 1171 | } else { |
| 1172 | name = "System RAM"; |
| 1173 | } |
| 1174 | break; |
| 1175 | |
| 1176 | case EFI_ACPI_MEMORY_NVS: |
| 1177 | name = "ACPI Non-volatile Storage"; |
Khalid Aziz | be37912 | 2005-09-19 15:42:36 -0700 | [diff] [blame] | 1178 | break; |
| 1179 | |
| 1180 | case EFI_UNUSABLE_MEMORY: |
| 1181 | name = "reserved"; |
Yasunori Goto | 887c3cb | 2007-11-14 16:59:20 -0800 | [diff] [blame] | 1182 | flags |= IORESOURCE_DISABLED; |
Khalid Aziz | be37912 | 2005-09-19 15:42:36 -0700 | [diff] [blame] | 1183 | break; |
| 1184 | |
| 1185 | case EFI_RESERVED_TYPE: |
| 1186 | case EFI_RUNTIME_SERVICES_CODE: |
| 1187 | case EFI_RUNTIME_SERVICES_DATA: |
| 1188 | case EFI_ACPI_RECLAIM_MEMORY: |
| 1189 | default: |
| 1190 | name = "reserved"; |
Khalid Aziz | be37912 | 2005-09-19 15:42:36 -0700 | [diff] [blame] | 1191 | break; |
| 1192 | } |
| 1193 | |
Aron Griffis | 7d9aed2 | 2008-02-04 15:31:49 -0800 | [diff] [blame] | 1194 | if ((res = kzalloc(sizeof(struct resource), |
| 1195 | GFP_KERNEL)) == NULL) { |
| 1196 | printk(KERN_ERR |
Aron Griffis | 965e7c8 | 2008-01-08 22:29:37 -0500 | [diff] [blame] | 1197 | "failed to allocate resource for iomem\n"); |
Khalid Aziz | be37912 | 2005-09-19 15:42:36 -0700 | [diff] [blame] | 1198 | return; |
| 1199 | } |
| 1200 | |
| 1201 | res->name = name; |
| 1202 | res->start = md->phys_addr; |
Li Zefan | 685c7f5 | 2007-11-21 14:58:26 -0800 | [diff] [blame] | 1203 | res->end = md->phys_addr + efi_md_size(md) - 1; |
Khalid Aziz | be37912 | 2005-09-19 15:42:36 -0700 | [diff] [blame] | 1204 | res->flags = flags; |
| 1205 | |
| 1206 | if (insert_resource(&iomem_resource, res) < 0) |
| 1207 | kfree(res); |
| 1208 | else { |
| 1209 | /* |
| 1210 | * We don't know which region contains |
| 1211 | * kernel data so we try it repeatedly and |
| 1212 | * let the resource manager test it. |
| 1213 | */ |
| 1214 | insert_resource(res, code_resource); |
| 1215 | insert_resource(res, data_resource); |
Bernhard Walle | 00bf409 | 2007-10-21 16:42:01 -0700 | [diff] [blame] | 1216 | insert_resource(res, bss_resource); |
Zou Nan hai | a7956113 | 2006-12-07 09:51:35 -0800 | [diff] [blame] | 1217 | #ifdef CONFIG_KEXEC |
| 1218 | insert_resource(res, &efi_memmap_res); |
| 1219 | insert_resource(res, &boot_param_res); |
| 1220 | if (crashk_res.end > crashk_res.start) |
| 1221 | insert_resource(res, &crashk_res); |
| 1222 | #endif |
Khalid Aziz | be37912 | 2005-09-19 15:42:36 -0700 | [diff] [blame] | 1223 | } |
| 1224 | } |
| 1225 | } |
Zou Nan hai | a7956113 | 2006-12-07 09:51:35 -0800 | [diff] [blame] | 1226 | |
| 1227 | #ifdef CONFIG_KEXEC |
| 1228 | /* find a block of memory aligned to 64M exclude reserved regions |
| 1229 | rsvd_regions are sorted |
| 1230 | */ |
Horms | 2a3a282 | 2007-03-06 16:28:50 +0900 | [diff] [blame] | 1231 | unsigned long __init |
Aron Griffis | 7d9aed2 | 2008-02-04 15:31:49 -0800 | [diff] [blame] | 1232 | kdump_find_rsvd_region (unsigned long size, struct rsvd_region *r, int n) |
Zou Nan hai | a7956113 | 2006-12-07 09:51:35 -0800 | [diff] [blame] | 1233 | { |
Aron Griffis | 7d9aed2 | 2008-02-04 15:31:49 -0800 | [diff] [blame] | 1234 | int i; |
| 1235 | u64 start, end; |
| 1236 | u64 alignment = 1UL << _PAGE_SIZE_64M; |
| 1237 | void *efi_map_start, *efi_map_end, *p; |
| 1238 | efi_memory_desc_t *md; |
| 1239 | u64 efi_desc_size; |
Zou Nan hai | a7956113 | 2006-12-07 09:51:35 -0800 | [diff] [blame] | 1240 | |
Aron Griffis | 7d9aed2 | 2008-02-04 15:31:49 -0800 | [diff] [blame] | 1241 | efi_map_start = __va(ia64_boot_param->efi_memmap); |
| 1242 | efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size; |
| 1243 | efi_desc_size = ia64_boot_param->efi_memdesc_size; |
Zou Nan hai | a7956113 | 2006-12-07 09:51:35 -0800 | [diff] [blame] | 1244 | |
Aron Griffis | 7d9aed2 | 2008-02-04 15:31:49 -0800 | [diff] [blame] | 1245 | for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) { |
| 1246 | md = p; |
| 1247 | if (!efi_wb(md)) |
| 1248 | continue; |
| 1249 | start = ALIGN(md->phys_addr, alignment); |
| 1250 | end = efi_md_end(md); |
| 1251 | for (i = 0; i < n; i++) { |
| 1252 | if (__pa(r[i].start) >= start && __pa(r[i].end) < end) { |
| 1253 | if (__pa(r[i].start) > start + size) |
| 1254 | return start; |
| 1255 | start = ALIGN(__pa(r[i].end), alignment); |
| 1256 | if (i < n-1 && |
| 1257 | __pa(r[i+1].start) < start + size) |
| 1258 | continue; |
| 1259 | else |
| 1260 | break; |
| 1261 | } |
Zou Nan hai | a7956113 | 2006-12-07 09:51:35 -0800 | [diff] [blame] | 1262 | } |
Aron Griffis | 7d9aed2 | 2008-02-04 15:31:49 -0800 | [diff] [blame] | 1263 | if (end > start + size) |
| 1264 | return start; |
| 1265 | } |
Zou Nan hai | a7956113 | 2006-12-07 09:51:35 -0800 | [diff] [blame] | 1266 | |
Aron Griffis | 7d9aed2 | 2008-02-04 15:31:49 -0800 | [diff] [blame] | 1267 | printk(KERN_WARNING |
| 1268 | "Cannot reserve 0x%lx byte of memory for crashdump\n", size); |
| 1269 | return ~0UL; |
Zou Nan hai | a7956113 | 2006-12-07 09:51:35 -0800 | [diff] [blame] | 1270 | } |
| 1271 | #endif |
Magnus Damm | cee87af | 2007-03-06 02:34:26 -0800 | [diff] [blame] | 1272 | |
| 1273 | #ifdef CONFIG_PROC_VMCORE |
| 1274 | /* locate the size find a the descriptor at a certain address */ |
Simon Horman | 1775fe85 | 2007-10-29 17:10:29 +0900 | [diff] [blame] | 1275 | unsigned long __init |
Magnus Damm | cee87af | 2007-03-06 02:34:26 -0800 | [diff] [blame] | 1276 | vmcore_find_descriptor_size (unsigned long address) |
| 1277 | { |
| 1278 | void *efi_map_start, *efi_map_end, *p; |
| 1279 | efi_memory_desc_t *md; |
| 1280 | u64 efi_desc_size; |
| 1281 | unsigned long ret = 0; |
| 1282 | |
| 1283 | efi_map_start = __va(ia64_boot_param->efi_memmap); |
| 1284 | efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size; |
| 1285 | efi_desc_size = ia64_boot_param->efi_memdesc_size; |
| 1286 | |
| 1287 | for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) { |
| 1288 | md = p; |
| 1289 | if (efi_wb(md) && md->type == EFI_LOADER_DATA |
| 1290 | && md->phys_addr == address) { |
| 1291 | ret = efi_md_size(md); |
| 1292 | break; |
| 1293 | } |
| 1294 | } |
| 1295 | |
| 1296 | if (ret == 0) |
| 1297 | printk(KERN_WARNING "Cannot locate EFI vmcore descriptor\n"); |
| 1298 | |
| 1299 | return ret; |
| 1300 | } |
| 1301 | #endif |