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