]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * Extensible Firmware Interface | |
3 | * | |
7d9aed26 AG |
4 | * Based on Extensible Firmware Interface Specification version 0.9 |
5 | * April 30, 1999 | |
1da177e4 LT |
6 | * |
7 | * Copyright (C) 1999 VA Linux Systems | |
8 | * Copyright (C) 1999 Walt Drummond <[email protected]> | |
9 | * Copyright (C) 1999-2003 Hewlett-Packard Co. | |
10 | * David Mosberger-Tang <[email protected]> | |
11 | * Stephane Eranian <[email protected]> | |
32e62c63 BH |
12 | * (c) Copyright 2006 Hewlett-Packard Development Company, L.P. |
13 | * Bjorn Helgaas <[email protected]> | |
1da177e4 LT |
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: <[email protected]> | |
22 | * Skip non-WB memory and ignore empty memory ranges. | |
23 | */ | |
1da177e4 | 24 | #include <linux/module.h> |
f4a57099 | 25 | #include <linux/bootmem.h> |
1da177e4 LT |
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> | |
a7956113 | 31 | #include <linux/kexec.h> |
ed7ed365 | 32 | #include <linux/mm.h> |
1da177e4 LT |
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; | |
a7956113 | 48 | static unsigned long mem_limit = ~0UL, max_addr = ~0UL, min_addr = 0UL; |
1da177e4 LT |
49 | |
50 | #define efi_call_virt(f, args...) (*(f))(args) | |
51 | ||
7d9aed26 AG |
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; \ | |
1da177e4 LT |
67 | } |
68 | ||
7d9aed26 AG |
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; \ | |
1da177e4 LT |
81 | } |
82 | ||
7d9aed26 AG |
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; \ | |
1da177e4 LT |
97 | } |
98 | ||
7d9aed26 AG |
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; \ | |
1da177e4 LT |
115 | } |
116 | ||
7d9aed26 AG |
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; \ | |
1da177e4 LT |
135 | } |
136 | ||
7d9aed26 AG |
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; \ | |
1da177e4 LT |
151 | } |
152 | ||
7d9aed26 AG |
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; \ | |
1da177e4 LT |
169 | } |
170 | ||
7d9aed26 AG |
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; \ | |
1da177e4 LT |
184 | } |
185 | ||
7d9aed26 AG |
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); \ | |
1da177e4 LT |
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 | ||
4b07ae9b LZ |
234 | if ((*efi.get_time)(&tm, NULL) != EFI_SUCCESS) { |
235 | memset(ts, 0, sizeof(*ts)); | |
1da177e4 | 236 | return; |
4b07ae9b | 237 | } |
1da177e4 | 238 | |
7d9aed26 AG |
239 | ts->tv_sec = mktime(tm.year, tm.month, tm.day, |
240 | tm.hour, tm.minute, tm.second); | |
1da177e4 LT |
241 | ts->tv_nsec = tm.nanosecond; |
242 | } | |
243 | ||
244 | static int | |
66888a6e | 245 | is_memory_available (efi_memory_desc_t *md) |
1da177e4 LT |
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 | ||
d8c97d5f TL |
261 | typedef struct kern_memdesc { |
262 | u64 attribute; | |
263 | u64 start; | |
264 | u64 num_pages; | |
265 | } kern_memdesc_t; | |
1da177e4 | 266 | |
d8c97d5f | 267 | static kern_memdesc_t *kern_memmap; |
1da177e4 | 268 | |
80851ef2 BH |
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 | ||
1da177e4 | 295 | static void |
d8c97d5f | 296 | walk (efi_freemem_callback_t callback, void *arg, u64 attr) |
1da177e4 | 297 | { |
d8c97d5f TL |
298 | kern_memdesc_t *k; |
299 | u64 start, end, voff; | |
1da177e4 | 300 | |
d8c97d5f TL |
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 | } | |
1da177e4 LT |
311 | } |
312 | ||
313 | /* | |
965e7c8a | 314 | * Walk the EFI memory map and call CALLBACK once for each EFI memory |
7d9aed26 | 315 | * descriptor that has memory that is available for OS use. |
1da177e4 LT |
316 | */ |
317 | void | |
318 | efi_memmap_walk (efi_freemem_callback_t callback, void *arg) | |
319 | { | |
d8c97d5f | 320 | walk(callback, arg, EFI_MEMORY_WB); |
1da177e4 LT |
321 | } |
322 | ||
f14f75b8 | 323 | /* |
965e7c8a | 324 | * Walk the EFI memory map and call CALLBACK once for each EFI memory |
7d9aed26 | 325 | * descriptor that has memory that is available for uncached allocator. |
f14f75b8 | 326 | */ |
d8c97d5f TL |
327 | void |
328 | efi_memmap_walk_uc (efi_freemem_callback_t callback, void *arg) | |
f14f75b8 | 329 | { |
d8c97d5f | 330 | walk(callback, arg, EFI_MEMORY_UC); |
f14f75b8 JS |
331 | } |
332 | ||
1da177e4 | 333 | /* |
965e7c8a | 334 | * Look for the PAL_CODE region reported by EFI and map it using an |
1da177e4 LT |
335 | * ITR to enable safe PAL calls in virtual mode. See IA-64 Processor |
336 | * Abstraction Layer chapter 11 in ADAG | |
337 | */ | |
1da177e4 LT |
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) { | |
7d9aed26 AG |
357 | printk(KERN_ERR "Too many EFI Pal Code memory ranges, " |
358 | "dropped @ %lx\n", md->phys_addr); | |
1da177e4 LT |
359 | continue; |
360 | } | |
361 | /* | |
7d9aed26 AG |
362 | * The only ITLB entry in region 7 that is used is the one |
363 | * installed by __start(). That entry covers a 64MB range. | |
1da177e4 LT |
364 | */ |
365 | mask = ~((1 << KERNEL_TR_PAGE_SHIFT) - 1); | |
366 | vaddr = PAGE_OFFSET + md->phys_addr; | |
367 | ||
368 | /* | |
7d9aed26 AG |
369 | * We must check that the PAL mapping won't overlap with the |
370 | * kernel mapping. | |
1da177e4 | 371 | * |
7d9aed26 AG |
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. | |
1da177e4 LT |
380 | */ |
381 | if ((vaddr & mask) == (KERNEL_START & mask)) { | |
d4ed8084 HH |
382 | printk(KERN_INFO "%s: no need to install ITR for PAL code\n", |
383 | __func__); | |
1da177e4 LT |
384 | continue; |
385 | } | |
386 | ||
685c7f5d | 387 | if (efi_md_size(md) > IA64_GRANULE_SIZE) |
965e7c8a | 388 | panic("Whoa! PAL code size bigger than a granule!"); |
1da177e4 LT |
389 | |
390 | #if EFI_DEBUG | |
391 | mask = ~((1 << IA64_GRANULE_SHIFT) - 1); | |
392 | ||
7d9aed26 AG |
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); | |
1da177e4 LT |
398 | #endif |
399 | return __va(md->phys_addr); | |
400 | } | |
9473252f | 401 | printk(KERN_WARNING "%s: no PAL-code memory-descriptor found\n", |
d4ed8084 | 402 | __func__); |
1da177e4 LT |
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(); | |
7d9aed26 AG |
419 | ia64_itr(0x1, IA64_TR_PALCODE, |
420 | GRANULEROUNDDOWN((unsigned long) pal_vaddr), | |
1da177e4 LT |
421 | pte_val(pfn_pte(__pa(pal_vaddr) >> PAGE_SHIFT, PAGE_KERNEL)), |
422 | IA64_GRANULE_SHIFT); | |
423 | ia64_set_psr(psr); /* restore psr */ | |
1da177e4 LT |
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; | |
9d78f43d | 433 | char *cp, vendor[100] = "unknown"; |
1da177e4 LT |
434 | int i; |
435 | ||
7d9aed26 | 436 | /* |
965e7c8a | 437 | * It's too early to be able to use the standard kernel command line |
7d9aed26 AG |
438 | * support... |
439 | */ | |
a8d91b84 | 440 | for (cp = boot_command_line; *cp; ) { |
1da177e4 | 441 | if (memcmp(cp, "mem=", 4) == 0) { |
9d78f43d | 442 | mem_limit = memparse(cp + 4, &cp); |
1da177e4 | 443 | } else if (memcmp(cp, "max_addr=", 9) == 0) { |
9d78f43d | 444 | max_addr = GRANULEROUNDDOWN(memparse(cp + 9, &cp)); |
a7956113 ZN |
445 | } else if (memcmp(cp, "min_addr=", 9) == 0) { |
446 | min_addr = GRANULEROUNDDOWN(memparse(cp + 9, &cp)); | |
1da177e4 LT |
447 | } else { |
448 | while (*cp != ' ' && *cp) | |
449 | ++cp; | |
450 | while (*cp == ' ') | |
451 | ++cp; | |
452 | } | |
453 | } | |
a7956113 | 454 | if (min_addr != 0UL) |
7d9aed26 AG |
455 | printk(KERN_INFO "Ignoring memory below %luMB\n", |
456 | min_addr >> 20); | |
1da177e4 | 457 | if (max_addr != ~0UL) |
7d9aed26 AG |
458 | printk(KERN_INFO "Ignoring memory above %luMB\n", |
459 | max_addr >> 20); | |
1da177e4 LT |
460 | |
461 | efi.systab = __va(ia64_boot_param->efi_systab); | |
462 | ||
463 | /* | |
464 | * Verify the EFI Table | |
465 | */ | |
466 | if (efi.systab == NULL) | |
965e7c8a | 467 | panic("Whoa! Can't find EFI system table.\n"); |
1da177e4 | 468 | if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) |
965e7c8a | 469 | panic("Whoa! EFI system table signature incorrect\n"); |
873ec746 BH |
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); | |
1da177e4 LT |
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) { | |
ecdd5dab | 481 | for (i = 0;i < (int) sizeof(vendor) - 1 && *c16; ++i) |
1da177e4 LT |
482 | vendor[i] = *c16++; |
483 | vendor[i] = '\0'; | |
484 | } | |
485 | ||
486 | printk(KERN_INFO "EFI v%u.%.02u by %s:", | |
7d9aed26 AG |
487 | efi.systab->hdr.revision >> 16, |
488 | efi.systab->hdr.revision & 0xffff, vendor); | |
1da177e4 | 489 | |
b2c99e3c BH |
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 | ||
1da177e4 LT |
499 | for (i = 0; i < (int) efi.systab->nr_tables; i++) { |
500 | if (efi_guidcmp(config_tables[i].guid, MPS_TABLE_GUID) == 0) { | |
b2c99e3c | 501 | efi.mps = config_tables[i].table; |
1da177e4 LT |
502 | printk(" MPS=0x%lx", config_tables[i].table); |
503 | } else if (efi_guidcmp(config_tables[i].guid, ACPI_20_TABLE_GUID) == 0) { | |
b2c99e3c | 504 | efi.acpi20 = config_tables[i].table; |
1da177e4 LT |
505 | printk(" ACPI 2.0=0x%lx", config_tables[i].table); |
506 | } else if (efi_guidcmp(config_tables[i].guid, ACPI_TABLE_GUID) == 0) { | |
b2c99e3c | 507 | efi.acpi = config_tables[i].table; |
1da177e4 LT |
508 | printk(" ACPI=0x%lx", config_tables[i].table); |
509 | } else if (efi_guidcmp(config_tables[i].guid, SMBIOS_TABLE_GUID) == 0) { | |
b2c99e3c | 510 | efi.smbios = config_tables[i].table; |
1da177e4 LT |
511 | printk(" SMBIOS=0x%lx", config_tables[i].table); |
512 | } else if (efi_guidcmp(config_tables[i].guid, SAL_SYSTEM_TABLE_GUID) == 0) { | |
b2c99e3c | 513 | efi.sal_systab = config_tables[i].table; |
1da177e4 LT |
514 | printk(" SALsystab=0x%lx", config_tables[i].table); |
515 | } else if (efi_guidcmp(config_tables[i].guid, HCDP_TABLE_GUID) == 0) { | |
b2c99e3c | 516 | efi.hcdp = config_tables[i].table; |
1da177e4 LT |
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 | ||
7d9aed26 AG |
543 | for (i = 0, p = efi_map_start; p < efi_map_end; |
544 | ++i, p += efi_desc_size) | |
545 | { | |
818c7e86 SH |
546 | const char *unit; |
547 | unsigned long size; | |
548 | ||
1da177e4 | 549 | md = p; |
818c7e86 SH |
550 | size = md->num_pages << EFI_PAGE_SHIFT; |
551 | ||
552 | if ((size >> 40) > 0) { | |
553 | size >>= 40; | |
554 | unit = "TB"; | |
555 | } else if ((size >> 30) > 0) { | |
556 | size >>= 30; | |
557 | unit = "GB"; | |
558 | } else if ((size >> 20) > 0) { | |
559 | size >>= 20; | |
560 | unit = "MB"; | |
561 | } else { | |
562 | size >>= 10; | |
563 | unit = "KB"; | |
564 | } | |
565 | ||
566 | printk("mem%02d: type=%2u, attr=0x%016lx, " | |
567 | "range=[0x%016lx-0x%016lx) (%4lu%s)\n", | |
1da177e4 | 568 | i, md->type, md->attribute, md->phys_addr, |
818c7e86 | 569 | md->phys_addr + efi_md_size(md), size, unit); |
1da177e4 LT |
570 | } |
571 | } | |
572 | #endif | |
573 | ||
574 | efi_map_pal_code(); | |
575 | efi_enter_virtual_mode(); | |
576 | } | |
577 | ||
578 | void | |
579 | efi_enter_virtual_mode (void) | |
580 | { | |
581 | void *efi_map_start, *efi_map_end, *p; | |
582 | efi_memory_desc_t *md; | |
583 | efi_status_t status; | |
584 | u64 efi_desc_size; | |
585 | ||
586 | efi_map_start = __va(ia64_boot_param->efi_memmap); | |
587 | efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size; | |
588 | efi_desc_size = ia64_boot_param->efi_memdesc_size; | |
589 | ||
590 | for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) { | |
591 | md = p; | |
592 | if (md->attribute & EFI_MEMORY_RUNTIME) { | |
593 | /* | |
7d9aed26 AG |
594 | * Some descriptors have multiple bits set, so the |
595 | * order of the tests is relevant. | |
1da177e4 LT |
596 | */ |
597 | if (md->attribute & EFI_MEMORY_WB) { | |
598 | md->virt_addr = (u64) __va(md->phys_addr); | |
599 | } else if (md->attribute & EFI_MEMORY_UC) { | |
600 | md->virt_addr = (u64) ioremap(md->phys_addr, 0); | |
601 | } else if (md->attribute & EFI_MEMORY_WC) { | |
602 | #if 0 | |
7d9aed26 AG |
603 | md->virt_addr = ia64_remap(md->phys_addr, |
604 | (_PAGE_A | | |
605 | _PAGE_P | | |
606 | _PAGE_D | | |
607 | _PAGE_MA_WC | | |
608 | _PAGE_PL_0 | | |
609 | _PAGE_AR_RW)); | |
1da177e4 LT |
610 | #else |
611 | printk(KERN_INFO "EFI_MEMORY_WC mapping\n"); | |
612 | md->virt_addr = (u64) ioremap(md->phys_addr, 0); | |
613 | #endif | |
614 | } else if (md->attribute & EFI_MEMORY_WT) { | |
615 | #if 0 | |
7d9aed26 AG |
616 | md->virt_addr = ia64_remap(md->phys_addr, |
617 | (_PAGE_A | | |
618 | _PAGE_P | | |
619 | _PAGE_D | | |
620 | _PAGE_MA_WT | | |
621 | _PAGE_PL_0 | | |
622 | _PAGE_AR_RW)); | |
1da177e4 LT |
623 | #else |
624 | printk(KERN_INFO "EFI_MEMORY_WT mapping\n"); | |
625 | md->virt_addr = (u64) ioremap(md->phys_addr, 0); | |
626 | #endif | |
627 | } | |
628 | } | |
629 | } | |
630 | ||
631 | status = efi_call_phys(__va(runtime->set_virtual_address_map), | |
632 | ia64_boot_param->efi_memmap_size, | |
7d9aed26 AG |
633 | efi_desc_size, |
634 | ia64_boot_param->efi_memdesc_version, | |
1da177e4 LT |
635 | ia64_boot_param->efi_memmap); |
636 | if (status != EFI_SUCCESS) { | |
7d9aed26 AG |
637 | printk(KERN_WARNING "warning: unable to switch EFI into " |
638 | "virtual mode (status=%lu)\n", status); | |
1da177e4 LT |
639 | return; |
640 | } | |
641 | ||
642 | /* | |
7d9aed26 AG |
643 | * Now that EFI is in virtual mode, we call the EFI functions more |
644 | * efficiently: | |
1da177e4 LT |
645 | */ |
646 | efi.get_time = virt_get_time; | |
647 | efi.set_time = virt_set_time; | |
648 | efi.get_wakeup_time = virt_get_wakeup_time; | |
649 | efi.set_wakeup_time = virt_set_wakeup_time; | |
650 | efi.get_variable = virt_get_variable; | |
651 | efi.get_next_variable = virt_get_next_variable; | |
652 | efi.set_variable = virt_set_variable; | |
653 | efi.get_next_high_mono_count = virt_get_next_high_mono_count; | |
654 | efi.reset_system = virt_reset_system; | |
655 | } | |
656 | ||
657 | /* | |
7d9aed26 AG |
658 | * Walk the EFI memory map looking for the I/O port range. There can only be |
659 | * one entry of this type, other I/O port ranges should be described via ACPI. | |
1da177e4 LT |
660 | */ |
661 | u64 | |
662 | efi_get_iobase (void) | |
663 | { | |
664 | void *efi_map_start, *efi_map_end, *p; | |
665 | efi_memory_desc_t *md; | |
666 | u64 efi_desc_size; | |
667 | ||
668 | efi_map_start = __va(ia64_boot_param->efi_memmap); | |
669 | efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size; | |
670 | efi_desc_size = ia64_boot_param->efi_memdesc_size; | |
671 | ||
672 | for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) { | |
673 | md = p; | |
674 | if (md->type == EFI_MEMORY_MAPPED_IO_PORT_SPACE) { | |
675 | if (md->attribute & EFI_MEMORY_UC) | |
676 | return md->phys_addr; | |
677 | } | |
678 | } | |
679 | return 0; | |
680 | } | |
681 | ||
32e62c63 BH |
682 | static struct kern_memdesc * |
683 | kern_memory_descriptor (unsigned long phys_addr) | |
1da177e4 | 684 | { |
32e62c63 | 685 | struct kern_memdesc *md; |
1da177e4 | 686 | |
32e62c63 BH |
687 | for (md = kern_memmap; md->start != ~0UL; md++) { |
688 | if (phys_addr - md->start < (md->num_pages << EFI_PAGE_SHIFT)) | |
80851ef2 | 689 | return md; |
1da177e4 | 690 | } |
e037cda5 | 691 | return NULL; |
1da177e4 LT |
692 | } |
693 | ||
32e62c63 BH |
694 | static efi_memory_desc_t * |
695 | efi_memory_descriptor (unsigned long phys_addr) | |
1da177e4 LT |
696 | { |
697 | void *efi_map_start, *efi_map_end, *p; | |
698 | efi_memory_desc_t *md; | |
699 | u64 efi_desc_size; | |
700 | ||
701 | efi_map_start = __va(ia64_boot_param->efi_memmap); | |
702 | efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size; | |
703 | efi_desc_size = ia64_boot_param->efi_memdesc_size; | |
704 | ||
705 | for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) { | |
706 | md = p; | |
707 | ||
685c7f5d | 708 | if (phys_addr - md->phys_addr < efi_md_size(md)) |
32e62c63 | 709 | return md; |
1da177e4 | 710 | } |
e037cda5 | 711 | return NULL; |
1da177e4 | 712 | } |
80851ef2 | 713 | |
6d40fc51 BH |
714 | static int |
715 | efi_memmap_intersects (unsigned long phys_addr, unsigned long size) | |
716 | { | |
717 | void *efi_map_start, *efi_map_end, *p; | |
718 | efi_memory_desc_t *md; | |
719 | u64 efi_desc_size; | |
720 | unsigned long end; | |
721 | ||
722 | efi_map_start = __va(ia64_boot_param->efi_memmap); | |
723 | efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size; | |
724 | efi_desc_size = ia64_boot_param->efi_memdesc_size; | |
725 | ||
726 | end = phys_addr + size; | |
727 | ||
728 | for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) { | |
729 | md = p; | |
6d40fc51 BH |
730 | if (md->phys_addr < end && efi_md_end(md) > phys_addr) |
731 | return 1; | |
732 | } | |
733 | return 0; | |
734 | } | |
735 | ||
80851ef2 BH |
736 | u32 |
737 | efi_mem_type (unsigned long phys_addr) | |
738 | { | |
739 | efi_memory_desc_t *md = efi_memory_descriptor(phys_addr); | |
740 | ||
741 | if (md) | |
742 | return md->type; | |
743 | return 0; | |
744 | } | |
745 | ||
746 | u64 | |
747 | efi_mem_attributes (unsigned long phys_addr) | |
748 | { | |
749 | efi_memory_desc_t *md = efi_memory_descriptor(phys_addr); | |
750 | ||
751 | if (md) | |
752 | return md->attribute; | |
753 | return 0; | |
754 | } | |
1da177e4 LT |
755 | EXPORT_SYMBOL(efi_mem_attributes); |
756 | ||
32e62c63 BH |
757 | u64 |
758 | efi_mem_attribute (unsigned long phys_addr, unsigned long size) | |
80851ef2 | 759 | { |
136939a2 | 760 | unsigned long end = phys_addr + size; |
80851ef2 | 761 | efi_memory_desc_t *md = efi_memory_descriptor(phys_addr); |
32e62c63 BH |
762 | u64 attr; |
763 | ||
764 | if (!md) | |
765 | return 0; | |
766 | ||
767 | /* | |
768 | * EFI_MEMORY_RUNTIME is not a memory attribute; it just tells | |
769 | * the kernel that firmware needs this region mapped. | |
770 | */ | |
771 | attr = md->attribute & ~EFI_MEMORY_RUNTIME; | |
772 | do { | |
773 | unsigned long md_end = efi_md_end(md); | |
774 | ||
775 | if (end <= md_end) | |
776 | return attr; | |
777 | ||
778 | md = efi_memory_descriptor(md_end); | |
779 | if (!md || (md->attribute & ~EFI_MEMORY_RUNTIME) != attr) | |
780 | return 0; | |
781 | } while (md); | |
410ab512 | 782 | return 0; /* never reached */ |
32e62c63 BH |
783 | } |
784 | ||
785 | u64 | |
786 | kern_mem_attribute (unsigned long phys_addr, unsigned long size) | |
787 | { | |
788 | unsigned long end = phys_addr + size; | |
789 | struct kern_memdesc *md; | |
790 | u64 attr; | |
80851ef2 | 791 | |
136939a2 | 792 | /* |
32e62c63 BH |
793 | * This is a hack for ioremap calls before we set up kern_memmap. |
794 | * Maybe we should do efi_memmap_init() earlier instead. | |
136939a2 | 795 | */ |
32e62c63 BH |
796 | if (!kern_memmap) { |
797 | attr = efi_mem_attribute(phys_addr, size); | |
798 | if (attr & EFI_MEMORY_WB) | |
799 | return EFI_MEMORY_WB; | |
80851ef2 | 800 | return 0; |
136939a2 | 801 | } |
80851ef2 | 802 | |
32e62c63 BH |
803 | md = kern_memory_descriptor(phys_addr); |
804 | if (!md) | |
805 | return 0; | |
806 | ||
807 | attr = md->attribute; | |
80851ef2 | 808 | do { |
32e62c63 | 809 | unsigned long md_end = kmd_end(md); |
136939a2 BH |
810 | |
811 | if (end <= md_end) | |
32e62c63 | 812 | return attr; |
80851ef2 | 813 | |
32e62c63 BH |
814 | md = kern_memory_descriptor(md_end); |
815 | if (!md || md->attribute != attr) | |
136939a2 | 816 | return 0; |
80851ef2 | 817 | } while (md); |
410ab512 | 818 | return 0; /* never reached */ |
80851ef2 | 819 | } |
32e62c63 | 820 | EXPORT_SYMBOL(kern_mem_attribute); |
80851ef2 | 821 | |
1da177e4 | 822 | int |
136939a2 | 823 | valid_phys_addr_range (unsigned long phys_addr, unsigned long size) |
1da177e4 | 824 | { |
32e62c63 BH |
825 | u64 attr; |
826 | ||
827 | /* | |
828 | * /dev/mem reads and writes use copy_to_user(), which implicitly | |
829 | * uses a granule-sized kernel identity mapping. It's really | |
830 | * only safe to do this for regions in kern_memmap. For more | |
831 | * details, see Documentation/ia64/aliasing.txt. | |
832 | */ | |
833 | attr = kern_mem_attribute(phys_addr, size); | |
834 | if (attr & EFI_MEMORY_WB || attr & EFI_MEMORY_UC) | |
835 | return 1; | |
836 | return 0; | |
80851ef2 | 837 | } |
1da177e4 | 838 | |
80851ef2 | 839 | int |
06c67bef | 840 | valid_mmap_phys_addr_range (unsigned long pfn, unsigned long size) |
80851ef2 | 841 | { |
6d40fc51 BH |
842 | unsigned long phys_addr = pfn << PAGE_SHIFT; |
843 | u64 attr; | |
844 | ||
845 | attr = efi_mem_attribute(phys_addr, size); | |
846 | ||
32e62c63 | 847 | /* |
6d40fc51 BH |
848 | * /dev/mem mmap uses normal user pages, so we don't need the entire |
849 | * granule, but the entire region we're mapping must support the same | |
850 | * attribute. | |
32e62c63 | 851 | */ |
6d40fc51 BH |
852 | if (attr & EFI_MEMORY_WB || attr & EFI_MEMORY_UC) |
853 | return 1; | |
854 | ||
855 | /* | |
856 | * Intel firmware doesn't tell us about all the MMIO regions, so | |
857 | * in general we have to allow mmap requests. But if EFI *does* | |
858 | * tell us about anything inside this region, we should deny it. | |
859 | * The user can always map a smaller region to avoid the overlap. | |
860 | */ | |
861 | if (efi_memmap_intersects(phys_addr, size)) | |
862 | return 0; | |
863 | ||
32e62c63 BH |
864 | return 1; |
865 | } | |
1da177e4 | 866 | |
32e62c63 BH |
867 | pgprot_t |
868 | phys_mem_access_prot(struct file *file, unsigned long pfn, unsigned long size, | |
869 | pgprot_t vma_prot) | |
870 | { | |
871 | unsigned long phys_addr = pfn << PAGE_SHIFT; | |
872 | u64 attr; | |
1da177e4 | 873 | |
32e62c63 BH |
874 | /* |
875 | * For /dev/mem mmap, we use user mappings, but if the region is | |
876 | * in kern_memmap (and hence may be covered by a kernel mapping), | |
877 | * we must use the same attribute as the kernel mapping. | |
878 | */ | |
879 | attr = kern_mem_attribute(phys_addr, size); | |
880 | if (attr & EFI_MEMORY_WB) | |
881 | return pgprot_cacheable(vma_prot); | |
882 | else if (attr & EFI_MEMORY_UC) | |
883 | return pgprot_noncached(vma_prot); | |
884 | ||
885 | /* | |
886 | * Some chipsets don't support UC access to memory. If | |
887 | * WB is supported, we prefer that. | |
888 | */ | |
889 | if (efi_mem_attribute(phys_addr, size) & EFI_MEMORY_WB) | |
890 | return pgprot_cacheable(vma_prot); | |
891 | ||
892 | return pgprot_noncached(vma_prot); | |
1da177e4 LT |
893 | } |
894 | ||
895 | int __init | |
896 | efi_uart_console_only(void) | |
897 | { | |
898 | efi_status_t status; | |
899 | char *s, name[] = "ConOut"; | |
900 | efi_guid_t guid = EFI_GLOBAL_VARIABLE_GUID; | |
901 | efi_char16_t *utf16, name_utf16[32]; | |
902 | unsigned char data[1024]; | |
903 | unsigned long size = sizeof(data); | |
904 | struct efi_generic_dev_path *hdr, *end_addr; | |
905 | int uart = 0; | |
906 | ||
907 | /* Convert to UTF-16 */ | |
908 | utf16 = name_utf16; | |
909 | s = name; | |
910 | while (*s) | |
911 | *utf16++ = *s++ & 0x7f; | |
912 | *utf16 = 0; | |
913 | ||
914 | status = efi.get_variable(name_utf16, &guid, NULL, &size, data); | |
915 | if (status != EFI_SUCCESS) { | |
916 | printk(KERN_ERR "No EFI %s variable?\n", name); | |
917 | return 0; | |
918 | } | |
919 | ||
920 | hdr = (struct efi_generic_dev_path *) data; | |
921 | end_addr = (struct efi_generic_dev_path *) ((u8 *) data + size); | |
922 | while (hdr < end_addr) { | |
923 | if (hdr->type == EFI_DEV_MSG && | |
924 | hdr->sub_type == EFI_DEV_MSG_UART) | |
925 | uart = 1; | |
926 | else if (hdr->type == EFI_DEV_END_PATH || | |
927 | hdr->type == EFI_DEV_END_PATH2) { | |
928 | if (!uart) | |
929 | return 0; | |
930 | if (hdr->sub_type == EFI_DEV_END_ENTIRE) | |
931 | return 1; | |
932 | uart = 0; | |
933 | } | |
7d9aed26 | 934 | hdr = (struct efi_generic_dev_path *)((u8 *) hdr + hdr->length); |
1da177e4 LT |
935 | } |
936 | printk(KERN_ERR "Malformed %s value\n", name); | |
937 | return 0; | |
938 | } | |
d8c97d5f | 939 | |
d8c97d5f TL |
940 | /* |
941 | * Look for the first granule aligned memory descriptor memory | |
942 | * that is big enough to hold EFI memory map. Make sure this | |
943 | * descriptor is atleast granule sized so it does not get trimmed | |
944 | */ | |
945 | struct kern_memdesc * | |
946 | find_memmap_space (void) | |
947 | { | |
948 | u64 contig_low=0, contig_high=0; | |
949 | u64 as = 0, ae; | |
950 | void *efi_map_start, *efi_map_end, *p, *q; | |
951 | efi_memory_desc_t *md, *pmd = NULL, *check_md; | |
952 | u64 space_needed, efi_desc_size; | |
953 | unsigned long total_mem = 0; | |
954 | ||
955 | efi_map_start = __va(ia64_boot_param->efi_memmap); | |
956 | efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size; | |
957 | efi_desc_size = ia64_boot_param->efi_memdesc_size; | |
958 | ||
959 | /* | |
960 | * Worst case: we need 3 kernel descriptors for each efi descriptor | |
961 | * (if every entry has a WB part in the middle, and UC head and tail), | |
962 | * plus one for the end marker. | |
963 | */ | |
964 | space_needed = sizeof(kern_memdesc_t) * | |
965 | (3 * (ia64_boot_param->efi_memmap_size/efi_desc_size) + 1); | |
966 | ||
967 | for (p = efi_map_start; p < efi_map_end; pmd = md, p += efi_desc_size) { | |
968 | md = p; | |
969 | if (!efi_wb(md)) { | |
970 | continue; | |
971 | } | |
7d9aed26 AG |
972 | if (pmd == NULL || !efi_wb(pmd) || |
973 | efi_md_end(pmd) != md->phys_addr) { | |
d8c97d5f TL |
974 | contig_low = GRANULEROUNDUP(md->phys_addr); |
975 | contig_high = efi_md_end(md); | |
7d9aed26 AG |
976 | for (q = p + efi_desc_size; q < efi_map_end; |
977 | q += efi_desc_size) { | |
d8c97d5f TL |
978 | check_md = q; |
979 | if (!efi_wb(check_md)) | |
980 | break; | |
981 | if (contig_high != check_md->phys_addr) | |
982 | break; | |
983 | contig_high = efi_md_end(check_md); | |
984 | } | |
985 | contig_high = GRANULEROUNDDOWN(contig_high); | |
986 | } | |
66888a6e | 987 | if (!is_memory_available(md) || md->type == EFI_LOADER_DATA) |
d8c97d5f TL |
988 | continue; |
989 | ||
990 | /* Round ends inward to granule boundaries */ | |
991 | as = max(contig_low, md->phys_addr); | |
992 | ae = min(contig_high, efi_md_end(md)); | |
993 | ||
a7956113 ZN |
994 | /* keep within max_addr= and min_addr= command line arg */ |
995 | as = max(as, min_addr); | |
d8c97d5f TL |
996 | ae = min(ae, max_addr); |
997 | if (ae <= as) | |
998 | continue; | |
999 | ||
1000 | /* avoid going over mem= command line arg */ | |
1001 | if (total_mem + (ae - as) > mem_limit) | |
1002 | ae -= total_mem + (ae - as) - mem_limit; | |
1003 | ||
1004 | if (ae <= as) | |
1005 | continue; | |
1006 | ||
1007 | if (ae - as > space_needed) | |
1008 | break; | |
1009 | } | |
1010 | if (p >= efi_map_end) | |
1011 | panic("Can't allocate space for kernel memory descriptors"); | |
1012 | ||
1013 | return __va(as); | |
1014 | } | |
1015 | ||
1016 | /* | |
1017 | * Walk the EFI memory map and gather all memory available for kernel | |
1018 | * to use. We can allocate partial granules only if the unavailable | |
1019 | * parts exist, and are WB. | |
1020 | */ | |
cb380853 | 1021 | unsigned long |
d8c97d5f TL |
1022 | efi_memmap_init(unsigned long *s, unsigned long *e) |
1023 | { | |
e037cda5 | 1024 | struct kern_memdesc *k, *prev = NULL; |
d8c97d5f TL |
1025 | u64 contig_low=0, contig_high=0; |
1026 | u64 as, ae, lim; | |
1027 | void *efi_map_start, *efi_map_end, *p, *q; | |
1028 | efi_memory_desc_t *md, *pmd = NULL, *check_md; | |
1029 | u64 efi_desc_size; | |
1030 | unsigned long total_mem = 0; | |
1031 | ||
1032 | k = kern_memmap = find_memmap_space(); | |
1033 | ||
1034 | efi_map_start = __va(ia64_boot_param->efi_memmap); | |
1035 | efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size; | |
1036 | efi_desc_size = ia64_boot_param->efi_memdesc_size; | |
1037 | ||
1038 | for (p = efi_map_start; p < efi_map_end; pmd = md, p += efi_desc_size) { | |
1039 | md = p; | |
1040 | if (!efi_wb(md)) { | |
7d9aed26 AG |
1041 | if (efi_uc(md) && |
1042 | (md->type == EFI_CONVENTIONAL_MEMORY || | |
1043 | md->type == EFI_BOOT_SERVICES_DATA)) { | |
d8c97d5f TL |
1044 | k->attribute = EFI_MEMORY_UC; |
1045 | k->start = md->phys_addr; | |
1046 | k->num_pages = md->num_pages; | |
1047 | k++; | |
1048 | } | |
1049 | continue; | |
1050 | } | |
7d9aed26 AG |
1051 | if (pmd == NULL || !efi_wb(pmd) || |
1052 | efi_md_end(pmd) != md->phys_addr) { | |
d8c97d5f TL |
1053 | contig_low = GRANULEROUNDUP(md->phys_addr); |
1054 | contig_high = efi_md_end(md); | |
7d9aed26 AG |
1055 | for (q = p + efi_desc_size; q < efi_map_end; |
1056 | q += efi_desc_size) { | |
d8c97d5f TL |
1057 | check_md = q; |
1058 | if (!efi_wb(check_md)) | |
1059 | break; | |
1060 | if (contig_high != check_md->phys_addr) | |
1061 | break; | |
1062 | contig_high = efi_md_end(check_md); | |
1063 | } | |
1064 | contig_high = GRANULEROUNDDOWN(contig_high); | |
1065 | } | |
66888a6e | 1066 | if (!is_memory_available(md)) |
d8c97d5f TL |
1067 | continue; |
1068 | ||
e55fdf11 TL |
1069 | #ifdef CONFIG_CRASH_DUMP |
1070 | /* saved_max_pfn should ignore max_addr= command line arg */ | |
1071 | if (saved_max_pfn < (efi_md_end(md) >> PAGE_SHIFT)) | |
1072 | saved_max_pfn = (efi_md_end(md) >> PAGE_SHIFT); | |
1073 | #endif | |
d8c97d5f TL |
1074 | /* |
1075 | * Round ends inward to granule boundaries | |
1076 | * Give trimmings to uncached allocator | |
1077 | */ | |
1078 | if (md->phys_addr < contig_low) { | |
1079 | lim = min(efi_md_end(md), contig_low); | |
1080 | if (efi_uc(md)) { | |
7d9aed26 AG |
1081 | if (k > kern_memmap && |
1082 | (k-1)->attribute == EFI_MEMORY_UC && | |
d8c97d5f | 1083 | kmd_end(k-1) == md->phys_addr) { |
7d9aed26 AG |
1084 | (k-1)->num_pages += |
1085 | (lim - md->phys_addr) | |
1086 | >> EFI_PAGE_SHIFT; | |
d8c97d5f TL |
1087 | } else { |
1088 | k->attribute = EFI_MEMORY_UC; | |
1089 | k->start = md->phys_addr; | |
7d9aed26 AG |
1090 | k->num_pages = (lim - md->phys_addr) |
1091 | >> EFI_PAGE_SHIFT; | |
d8c97d5f TL |
1092 | k++; |
1093 | } | |
1094 | } | |
1095 | as = contig_low; | |
1096 | } else | |
1097 | as = md->phys_addr; | |
1098 | ||
1099 | if (efi_md_end(md) > contig_high) { | |
1100 | lim = max(md->phys_addr, contig_high); | |
1101 | if (efi_uc(md)) { | |
1102 | if (lim == md->phys_addr && k > kern_memmap && | |
1103 | (k-1)->attribute == EFI_MEMORY_UC && | |
1104 | kmd_end(k-1) == md->phys_addr) { | |
1105 | (k-1)->num_pages += md->num_pages; | |
1106 | } else { | |
1107 | k->attribute = EFI_MEMORY_UC; | |
1108 | k->start = lim; | |
7d9aed26 AG |
1109 | k->num_pages = (efi_md_end(md) - lim) |
1110 | >> EFI_PAGE_SHIFT; | |
d8c97d5f TL |
1111 | k++; |
1112 | } | |
1113 | } | |
1114 | ae = contig_high; | |
1115 | } else | |
1116 | ae = efi_md_end(md); | |
1117 | ||
a7956113 ZN |
1118 | /* keep within max_addr= and min_addr= command line arg */ |
1119 | as = max(as, min_addr); | |
d8c97d5f TL |
1120 | ae = min(ae, max_addr); |
1121 | if (ae <= as) | |
1122 | continue; | |
1123 | ||
1124 | /* avoid going over mem= command line arg */ | |
1125 | if (total_mem + (ae - as) > mem_limit) | |
1126 | ae -= total_mem + (ae - as) - mem_limit; | |
1127 | ||
1128 | if (ae <= as) | |
1129 | continue; | |
1130 | if (prev && kmd_end(prev) == md->phys_addr) { | |
1131 | prev->num_pages += (ae - as) >> EFI_PAGE_SHIFT; | |
1132 | total_mem += ae - as; | |
1133 | continue; | |
1134 | } | |
1135 | k->attribute = EFI_MEMORY_WB; | |
1136 | k->start = as; | |
1137 | k->num_pages = (ae - as) >> EFI_PAGE_SHIFT; | |
1138 | total_mem += ae - as; | |
1139 | prev = k++; | |
1140 | } | |
1141 | k->start = ~0L; /* end-marker */ | |
1142 | ||
1143 | /* reserve the memory we are using for kern_memmap */ | |
1144 | *s = (u64)kern_memmap; | |
1145 | *e = (u64)++k; | |
cb380853 BW |
1146 | |
1147 | return total_mem; | |
d8c97d5f | 1148 | } |
be379124 KA |
1149 | |
1150 | void | |
1151 | efi_initialize_iomem_resources(struct resource *code_resource, | |
00bf4098 BW |
1152 | struct resource *data_resource, |
1153 | struct resource *bss_resource) | |
be379124 KA |
1154 | { |
1155 | struct resource *res; | |
1156 | void *efi_map_start, *efi_map_end, *p; | |
1157 | efi_memory_desc_t *md; | |
1158 | u64 efi_desc_size; | |
1159 | char *name; | |
1160 | unsigned long flags; | |
1161 | ||
1162 | efi_map_start = __va(ia64_boot_param->efi_memmap); | |
1163 | efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size; | |
1164 | efi_desc_size = ia64_boot_param->efi_memdesc_size; | |
1165 | ||
1166 | res = NULL; | |
1167 | ||
1168 | for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) { | |
1169 | md = p; | |
1170 | ||
1171 | if (md->num_pages == 0) /* should not happen */ | |
1172 | continue; | |
1173 | ||
887c3cb1 | 1174 | flags = IORESOURCE_MEM | IORESOURCE_BUSY; |
be379124 KA |
1175 | switch (md->type) { |
1176 | ||
1177 | case EFI_MEMORY_MAPPED_IO: | |
1178 | case EFI_MEMORY_MAPPED_IO_PORT_SPACE: | |
1179 | continue; | |
1180 | ||
1181 | case EFI_LOADER_CODE: | |
1182 | case EFI_LOADER_DATA: | |
1183 | case EFI_BOOT_SERVICES_DATA: | |
1184 | case EFI_BOOT_SERVICES_CODE: | |
1185 | case EFI_CONVENTIONAL_MEMORY: | |
1186 | if (md->attribute & EFI_MEMORY_WP) { | |
1187 | name = "System ROM"; | |
1188 | flags |= IORESOURCE_READONLY; | |
1189 | } else { | |
1190 | name = "System RAM"; | |
1191 | } | |
1192 | break; | |
1193 | ||
1194 | case EFI_ACPI_MEMORY_NVS: | |
1195 | name = "ACPI Non-volatile Storage"; | |
be379124 KA |
1196 | break; |
1197 | ||
1198 | case EFI_UNUSABLE_MEMORY: | |
1199 | name = "reserved"; | |
887c3cb1 | 1200 | flags |= IORESOURCE_DISABLED; |
be379124 KA |
1201 | break; |
1202 | ||
1203 | case EFI_RESERVED_TYPE: | |
1204 | case EFI_RUNTIME_SERVICES_CODE: | |
1205 | case EFI_RUNTIME_SERVICES_DATA: | |
1206 | case EFI_ACPI_RECLAIM_MEMORY: | |
1207 | default: | |
1208 | name = "reserved"; | |
be379124 KA |
1209 | break; |
1210 | } | |
1211 | ||
7d9aed26 AG |
1212 | if ((res = kzalloc(sizeof(struct resource), |
1213 | GFP_KERNEL)) == NULL) { | |
1214 | printk(KERN_ERR | |
965e7c8a | 1215 | "failed to allocate resource for iomem\n"); |
be379124 KA |
1216 | return; |
1217 | } | |
1218 | ||
1219 | res->name = name; | |
1220 | res->start = md->phys_addr; | |
685c7f5d | 1221 | res->end = md->phys_addr + efi_md_size(md) - 1; |
be379124 KA |
1222 | res->flags = flags; |
1223 | ||
1224 | if (insert_resource(&iomem_resource, res) < 0) | |
1225 | kfree(res); | |
1226 | else { | |
1227 | /* | |
1228 | * We don't know which region contains | |
1229 | * kernel data so we try it repeatedly and | |
1230 | * let the resource manager test it. | |
1231 | */ | |
1232 | insert_resource(res, code_resource); | |
1233 | insert_resource(res, data_resource); | |
00bf4098 | 1234 | insert_resource(res, bss_resource); |
a7956113 ZN |
1235 | #ifdef CONFIG_KEXEC |
1236 | insert_resource(res, &efi_memmap_res); | |
1237 | insert_resource(res, &boot_param_res); | |
1238 | if (crashk_res.end > crashk_res.start) | |
1239 | insert_resource(res, &crashk_res); | |
1240 | #endif | |
be379124 KA |
1241 | } |
1242 | } | |
1243 | } | |
a7956113 ZN |
1244 | |
1245 | #ifdef CONFIG_KEXEC | |
1246 | /* find a block of memory aligned to 64M exclude reserved regions | |
1247 | rsvd_regions are sorted | |
1248 | */ | |
2a3a2827 | 1249 | unsigned long __init |
7d9aed26 | 1250 | kdump_find_rsvd_region (unsigned long size, struct rsvd_region *r, int n) |
a7956113 | 1251 | { |
7d9aed26 AG |
1252 | int i; |
1253 | u64 start, end; | |
1254 | u64 alignment = 1UL << _PAGE_SIZE_64M; | |
1255 | void *efi_map_start, *efi_map_end, *p; | |
1256 | efi_memory_desc_t *md; | |
1257 | u64 efi_desc_size; | |
1258 | ||
1259 | efi_map_start = __va(ia64_boot_param->efi_memmap); | |
1260 | efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size; | |
1261 | efi_desc_size = ia64_boot_param->efi_memdesc_size; | |
1262 | ||
1263 | for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) { | |
1264 | md = p; | |
1265 | if (!efi_wb(md)) | |
1266 | continue; | |
1267 | start = ALIGN(md->phys_addr, alignment); | |
1268 | end = efi_md_end(md); | |
1269 | for (i = 0; i < n; i++) { | |
1270 | if (__pa(r[i].start) >= start && __pa(r[i].end) < end) { | |
1271 | if (__pa(r[i].start) > start + size) | |
1272 | return start; | |
1273 | start = ALIGN(__pa(r[i].end), alignment); | |
1274 | if (i < n-1 && | |
1275 | __pa(r[i+1].start) < start + size) | |
1276 | continue; | |
1277 | else | |
1278 | break; | |
1279 | } | |
a7956113 | 1280 | } |
7d9aed26 AG |
1281 | if (end > start + size) |
1282 | return start; | |
1283 | } | |
1284 | ||
1285 | printk(KERN_WARNING | |
1286 | "Cannot reserve 0x%lx byte of memory for crashdump\n", size); | |
1287 | return ~0UL; | |
a7956113 ZN |
1288 | } |
1289 | #endif | |
cee87af2 MD |
1290 | |
1291 | #ifdef CONFIG_PROC_VMCORE | |
1292 | /* locate the size find a the descriptor at a certain address */ | |
1775fe85 | 1293 | unsigned long __init |
cee87af2 MD |
1294 | vmcore_find_descriptor_size (unsigned long address) |
1295 | { | |
1296 | void *efi_map_start, *efi_map_end, *p; | |
1297 | efi_memory_desc_t *md; | |
1298 | u64 efi_desc_size; | |
1299 | unsigned long ret = 0; | |
1300 | ||
1301 | efi_map_start = __va(ia64_boot_param->efi_memmap); | |
1302 | efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size; | |
1303 | efi_desc_size = ia64_boot_param->efi_memdesc_size; | |
1304 | ||
1305 | for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) { | |
1306 | md = p; | |
1307 | if (efi_wb(md) && md->type == EFI_LOADER_DATA | |
1308 | && md->phys_addr == address) { | |
1309 | ret = efi_md_size(md); | |
1310 | break; | |
1311 | } | |
1312 | } | |
1313 | ||
1314 | if (ret == 0) | |
1315 | printk(KERN_WARNING "Cannot locate EFI vmcore descriptor\n"); | |
1316 | ||
1317 | return ret; | |
1318 | } | |
1319 | #endif |