blob: 8e8f8b6193eea5038fb9bff9a53baa03b7694f7b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Bjorn Helgaas32e62c62006-05-05 17:19:50 -060011 * (c) Copyright 2006 Hewlett-Packard Development Company, L.P.
12 * Bjorn Helgaas <bjorn.helgaas@hp.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/module.h>
Hormsf4a57092007-03-06 02:34:21 -080024#include <linux/bootmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/kernel.h>
26#include <linux/init.h>
27#include <linux/types.h>
28#include <linux/time.h>
29#include <linux/efi.h>
Zou Nan haia79561132006-12-07 09:51:35 -080030#include <linux/kexec.h>
Mel Gormaned7ed362007-07-17 04:03:14 -070031#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
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
42extern efi_status_t efi_call_phys (void *, ...);
43
44struct efi efi;
45EXPORT_SYMBOL(efi);
46static efi_runtime_services_t *runtime;
Zou Nan haia79561132006-12-07 09:51:35 -080047static unsigned long mem_limit = ~0UL, max_addr = ~0UL, min_addr = 0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49#define efi_call_virt(f, args...) (*(f))(args)
50
51#define STUB_GET_TIME(prefix, adjust_arg) \
52static efi_status_t \
53prefix##_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) \
68static efi_status_t \
69prefix##_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) \
81static efi_status_t \
82prefix##_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) \
95static efi_status_t \
96prefix##_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) \
112static efi_status_t \
113prefix##_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) \
131static efi_status_t \
132prefix##_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) \
145static efi_status_t \
146prefix##_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) \
161static efi_status_t \
162prefix##_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) \
175static void \
176prefix##_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
194STUB_GET_TIME(phys, phys_ptr)
195STUB_SET_TIME(phys, phys_ptr)
196STUB_GET_WAKEUP_TIME(phys, phys_ptr)
197STUB_SET_WAKEUP_TIME(phys, phys_ptr)
198STUB_GET_VARIABLE(phys, phys_ptr)
199STUB_GET_NEXT_VARIABLE(phys, phys_ptr)
200STUB_SET_VARIABLE(phys, phys_ptr)
201STUB_GET_NEXT_HIGH_MONO_COUNT(phys, phys_ptr)
202STUB_RESET_SYSTEM(phys, phys_ptr)
203
204#define id(arg) arg
205
206STUB_GET_TIME(virt, id)
207STUB_SET_TIME(virt, id)
208STUB_GET_WAKEUP_TIME(virt, id)
209STUB_SET_WAKEUP_TIME(virt, id)
210STUB_GET_VARIABLE(virt, id)
211STUB_GET_NEXT_VARIABLE(virt, id)
212STUB_SET_VARIABLE(virt, id)
213STUB_GET_NEXT_HIGH_MONO_COUNT(virt, id)
214STUB_RESET_SYSTEM(virt, id)
215
216void
217efi_gettimeofday (struct timespec *ts)
218{
219 efi_time_t tm;
220
Li Zefan4b07ae92007-11-06 13:40:39 -0800221 if ((*efi.get_time)(&tm, NULL) != EFI_SUCCESS) {
222 memset(ts, 0, sizeof(*ts));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 return;
Li Zefan4b07ae92007-11-06 13:40:39 -0800224 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
226 ts->tv_sec = mktime(tm.year, tm.month, tm.day, tm.hour, tm.minute, tm.second);
227 ts->tv_nsec = tm.nanosecond;
228}
229
230static int
Christoph Lameter66888a62006-12-04 14:58:35 -0800231is_memory_available (efi_memory_desc_t *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232{
233 if (!(md->attribute & EFI_MEMORY_WB))
234 return 0;
235
236 switch (md->type) {
237 case EFI_LOADER_CODE:
238 case EFI_LOADER_DATA:
239 case EFI_BOOT_SERVICES_CODE:
240 case EFI_BOOT_SERVICES_DATA:
241 case EFI_CONVENTIONAL_MEMORY:
242 return 1;
243 }
244 return 0;
245}
246
Tony Luckd8c97d52005-09-08 12:39:59 -0700247typedef struct kern_memdesc {
248 u64 attribute;
249 u64 start;
250 u64 num_pages;
251} kern_memdesc_t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Tony Luckd8c97d52005-09-08 12:39:59 -0700253static kern_memdesc_t *kern_memmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800255#define efi_md_size(md) (md->num_pages << EFI_PAGE_SHIFT)
256
257static inline u64
258kmd_end(kern_memdesc_t *kmd)
259{
260 return (kmd->start + (kmd->num_pages << EFI_PAGE_SHIFT));
261}
262
263static inline u64
264efi_md_end(efi_memory_desc_t *md)
265{
266 return (md->phys_addr + efi_md_size(md));
267}
268
269static inline int
270efi_wb(efi_memory_desc_t *md)
271{
272 return (md->attribute & EFI_MEMORY_WB);
273}
274
275static inline int
276efi_uc(efi_memory_desc_t *md)
277{
278 return (md->attribute & EFI_MEMORY_UC);
279}
280
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281static void
Tony Luckd8c97d52005-09-08 12:39:59 -0700282walk (efi_freemem_callback_t callback, void *arg, u64 attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283{
Tony Luckd8c97d52005-09-08 12:39:59 -0700284 kern_memdesc_t *k;
285 u64 start, end, voff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Tony Luckd8c97d52005-09-08 12:39:59 -0700287 voff = (attr == EFI_MEMORY_WB) ? PAGE_OFFSET : __IA64_UNCACHED_OFFSET;
288 for (k = kern_memmap; k->start != ~0UL; k++) {
289 if (k->attribute != attr)
290 continue;
291 start = PAGE_ALIGN(k->start);
292 end = (k->start + (k->num_pages << EFI_PAGE_SHIFT)) & PAGE_MASK;
293 if (start < end)
294 if ((*callback)(start + voff, end + voff, arg) < 0)
295 return;
296 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297}
298
299/*
300 * Walks the EFI memory map and calls CALLBACK once for each EFI memory descriptor that
301 * has memory that is available for OS use.
302 */
303void
304efi_memmap_walk (efi_freemem_callback_t callback, void *arg)
305{
Tony Luckd8c97d52005-09-08 12:39:59 -0700306 walk(callback, arg, EFI_MEMORY_WB);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307}
308
309/*
Tony Luckd8c97d52005-09-08 12:39:59 -0700310 * Walks the EFI memory map and calls CALLBACK once for each EFI memory descriptor that
311 * has memory that is available for uncached allocator.
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700312 */
Tony Luckd8c97d52005-09-08 12:39:59 -0700313void
314efi_memmap_walk_uc (efi_freemem_callback_t callback, void *arg)
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700315{
Tony Luckd8c97d52005-09-08 12:39:59 -0700316 walk(callback, arg, EFI_MEMORY_UC);
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700317}
318
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700319/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 * Look for the PAL_CODE region reported by EFI and maps it using an
321 * ITR to enable safe PAL calls in virtual mode. See IA-64 Processor
322 * Abstraction Layer chapter 11 in ADAG
323 */
324
325void *
326efi_get_pal_addr (void)
327{
328 void *efi_map_start, *efi_map_end, *p;
329 efi_memory_desc_t *md;
330 u64 efi_desc_size;
331 int pal_code_count = 0;
332 u64 vaddr, mask;
333
334 efi_map_start = __va(ia64_boot_param->efi_memmap);
335 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
336 efi_desc_size = ia64_boot_param->efi_memdesc_size;
337
338 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
339 md = p;
340 if (md->type != EFI_PAL_CODE)
341 continue;
342
343 if (++pal_code_count > 1) {
344 printk(KERN_ERR "Too many EFI Pal Code memory ranges, dropped @ %lx\n",
345 md->phys_addr);
346 continue;
347 }
348 /*
349 * The only ITLB entry in region 7 that is used is the one installed by
350 * __start(). That entry covers a 64MB range.
351 */
352 mask = ~((1 << KERNEL_TR_PAGE_SHIFT) - 1);
353 vaddr = PAGE_OFFSET + md->phys_addr;
354
355 /*
356 * We must check that the PAL mapping won't overlap with the kernel
357 * mapping.
358 *
359 * PAL code is guaranteed to be aligned on a power of 2 between 4k and
360 * 256KB and that only one ITR is needed to map it. This implies that the
361 * PAL code is always aligned on its size, i.e., the closest matching page
362 * size supported by the TLB. Therefore PAL code is guaranteed never to
363 * cross a 64MB unless it is bigger than 64MB (very unlikely!). So for
364 * now the following test is enough to determine whether or not we need a
365 * dedicated ITR for the PAL code.
366 */
367 if ((vaddr & mask) == (KERNEL_START & mask)) {
368 printk(KERN_INFO "%s: no need to install ITR for PAL code\n",
369 __FUNCTION__);
370 continue;
371 }
372
373 if (md->num_pages << EFI_PAGE_SHIFT > IA64_GRANULE_SIZE)
374 panic("Woah! PAL code size bigger than a granule!");
375
376#if EFI_DEBUG
377 mask = ~((1 << IA64_GRANULE_SHIFT) - 1);
378
379 printk(KERN_INFO "CPU %d: mapping PAL code [0x%lx-0x%lx) into [0x%lx-0x%lx)\n",
380 smp_processor_id(), md->phys_addr,
381 md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT),
382 vaddr & mask, (vaddr & mask) + IA64_GRANULE_SIZE);
383#endif
384 return __va(md->phys_addr);
385 }
Horms94732522007-02-05 10:17:38 +0900386 printk(KERN_WARNING "%s: no PAL-code memory-descriptor found\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 __FUNCTION__);
388 return NULL;
389}
390
391void
392efi_map_pal_code (void)
393{
394 void *pal_vaddr = efi_get_pal_addr ();
395 u64 psr;
396
397 if (!pal_vaddr)
398 return;
399
400 /*
401 * Cannot write to CRx with PSR.ic=1
402 */
403 psr = ia64_clear_ic();
404 ia64_itr(0x1, IA64_TR_PALCODE, GRANULEROUNDDOWN((unsigned long) pal_vaddr),
405 pte_val(pfn_pte(__pa(pal_vaddr) >> PAGE_SHIFT, PAGE_KERNEL)),
406 IA64_GRANULE_SHIFT);
407 ia64_set_psr(psr); /* restore psr */
408 ia64_srlz_i();
409}
410
411void __init
412efi_init (void)
413{
414 void *efi_map_start, *efi_map_end;
415 efi_config_table_t *config_tables;
416 efi_char16_t *c16;
417 u64 efi_desc_size;
Zou Nan hai9d78f432006-02-07 11:35:46 +0800418 char *cp, vendor[100] = "unknown";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 int i;
420
421 /* it's too early to be able to use the standard kernel command line support... */
Alon Bar-Leva8d91b82007-02-12 00:54:12 -0800422 for (cp = boot_command_line; *cp; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 if (memcmp(cp, "mem=", 4) == 0) {
Zou Nan hai9d78f432006-02-07 11:35:46 +0800424 mem_limit = memparse(cp + 4, &cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 } else if (memcmp(cp, "max_addr=", 9) == 0) {
Zou Nan hai9d78f432006-02-07 11:35:46 +0800426 max_addr = GRANULEROUNDDOWN(memparse(cp + 9, &cp));
Zou Nan haia79561132006-12-07 09:51:35 -0800427 } else if (memcmp(cp, "min_addr=", 9) == 0) {
428 min_addr = GRANULEROUNDDOWN(memparse(cp + 9, &cp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 } else {
430 while (*cp != ' ' && *cp)
431 ++cp;
432 while (*cp == ' ')
433 ++cp;
434 }
435 }
Zou Nan haia79561132006-12-07 09:51:35 -0800436 if (min_addr != 0UL)
437 printk(KERN_INFO "Ignoring memory below %luMB\n", min_addr >> 20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 if (max_addr != ~0UL)
439 printk(KERN_INFO "Ignoring memory above %luMB\n", max_addr >> 20);
440
441 efi.systab = __va(ia64_boot_param->efi_systab);
442
443 /*
444 * Verify the EFI Table
445 */
446 if (efi.systab == NULL)
447 panic("Woah! Can't find EFI system table.\n");
448 if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
449 panic("Woah! EFI system table signature incorrect\n");
Bjorn Helgaas873ec742007-05-08 00:29:57 -0700450 if ((efi.systab->hdr.revision >> 16) == 0)
451 printk(KERN_WARNING "Warning: EFI system table version "
452 "%d.%02d, expected 1.00 or greater\n",
453 efi.systab->hdr.revision >> 16,
454 efi.systab->hdr.revision & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
456 config_tables = __va(efi.systab->tables);
457
458 /* Show what we know for posterity */
459 c16 = __va(efi.systab->fw_vendor);
460 if (c16) {
Zou Nan haiecdd5da2006-02-07 11:25:55 +0800461 for (i = 0;i < (int) sizeof(vendor) - 1 && *c16; ++i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 vendor[i] = *c16++;
463 vendor[i] = '\0';
464 }
465
466 printk(KERN_INFO "EFI v%u.%.02u by %s:",
467 efi.systab->hdr.revision >> 16, efi.systab->hdr.revision & 0xffff, vendor);
468
Bjorn Helgaasb2c99e32006-03-26 01:37:08 -0800469 efi.mps = EFI_INVALID_TABLE_ADDR;
470 efi.acpi = EFI_INVALID_TABLE_ADDR;
471 efi.acpi20 = EFI_INVALID_TABLE_ADDR;
472 efi.smbios = EFI_INVALID_TABLE_ADDR;
473 efi.sal_systab = EFI_INVALID_TABLE_ADDR;
474 efi.boot_info = EFI_INVALID_TABLE_ADDR;
475 efi.hcdp = EFI_INVALID_TABLE_ADDR;
476 efi.uga = EFI_INVALID_TABLE_ADDR;
477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 for (i = 0; i < (int) efi.systab->nr_tables; i++) {
479 if (efi_guidcmp(config_tables[i].guid, MPS_TABLE_GUID) == 0) {
Bjorn Helgaasb2c99e32006-03-26 01:37:08 -0800480 efi.mps = config_tables[i].table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 printk(" MPS=0x%lx", config_tables[i].table);
482 } else if (efi_guidcmp(config_tables[i].guid, ACPI_20_TABLE_GUID) == 0) {
Bjorn Helgaasb2c99e32006-03-26 01:37:08 -0800483 efi.acpi20 = config_tables[i].table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 printk(" ACPI 2.0=0x%lx", config_tables[i].table);
485 } else if (efi_guidcmp(config_tables[i].guid, ACPI_TABLE_GUID) == 0) {
Bjorn Helgaasb2c99e32006-03-26 01:37:08 -0800486 efi.acpi = config_tables[i].table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 printk(" ACPI=0x%lx", config_tables[i].table);
488 } else if (efi_guidcmp(config_tables[i].guid, SMBIOS_TABLE_GUID) == 0) {
Bjorn Helgaasb2c99e32006-03-26 01:37:08 -0800489 efi.smbios = config_tables[i].table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 printk(" SMBIOS=0x%lx", config_tables[i].table);
491 } else if (efi_guidcmp(config_tables[i].guid, SAL_SYSTEM_TABLE_GUID) == 0) {
Bjorn Helgaasb2c99e32006-03-26 01:37:08 -0800492 efi.sal_systab = config_tables[i].table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 printk(" SALsystab=0x%lx", config_tables[i].table);
494 } else if (efi_guidcmp(config_tables[i].guid, HCDP_TABLE_GUID) == 0) {
Bjorn Helgaasb2c99e32006-03-26 01:37:08 -0800495 efi.hcdp = config_tables[i].table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 printk(" HCDP=0x%lx", config_tables[i].table);
497 }
498 }
499 printk("\n");
500
501 runtime = __va(efi.systab->runtime);
502 efi.get_time = phys_get_time;
503 efi.set_time = phys_set_time;
504 efi.get_wakeup_time = phys_get_wakeup_time;
505 efi.set_wakeup_time = phys_set_wakeup_time;
506 efi.get_variable = phys_get_variable;
507 efi.get_next_variable = phys_get_next_variable;
508 efi.set_variable = phys_set_variable;
509 efi.get_next_high_mono_count = phys_get_next_high_mono_count;
510 efi.reset_system = phys_reset_system;
511
512 efi_map_start = __va(ia64_boot_param->efi_memmap);
513 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
514 efi_desc_size = ia64_boot_param->efi_memdesc_size;
515
516#if EFI_DEBUG
517 /* print EFI memory map: */
518 {
519 efi_memory_desc_t *md;
520 void *p;
521
522 for (i = 0, p = efi_map_start; p < efi_map_end; ++i, p += efi_desc_size) {
523 md = p;
524 printk("mem%02u: type=%u, attr=0x%lx, range=[0x%016lx-0x%016lx) (%luMB)\n",
525 i, md->type, md->attribute, md->phys_addr,
526 md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT),
527 md->num_pages >> (20 - EFI_PAGE_SHIFT));
528 }
529 }
530#endif
531
532 efi_map_pal_code();
533 efi_enter_virtual_mode();
534}
535
536void
537efi_enter_virtual_mode (void)
538{
539 void *efi_map_start, *efi_map_end, *p;
540 efi_memory_desc_t *md;
541 efi_status_t status;
542 u64 efi_desc_size;
543
544 efi_map_start = __va(ia64_boot_param->efi_memmap);
545 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
546 efi_desc_size = ia64_boot_param->efi_memdesc_size;
547
548 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
549 md = p;
550 if (md->attribute & EFI_MEMORY_RUNTIME) {
551 /*
552 * Some descriptors have multiple bits set, so the order of
553 * the tests is relevant.
554 */
555 if (md->attribute & EFI_MEMORY_WB) {
556 md->virt_addr = (u64) __va(md->phys_addr);
557 } else if (md->attribute & EFI_MEMORY_UC) {
558 md->virt_addr = (u64) ioremap(md->phys_addr, 0);
559 } else if (md->attribute & EFI_MEMORY_WC) {
560#if 0
561 md->virt_addr = ia64_remap(md->phys_addr, (_PAGE_A | _PAGE_P
562 | _PAGE_D
563 | _PAGE_MA_WC
564 | _PAGE_PL_0
565 | _PAGE_AR_RW));
566#else
567 printk(KERN_INFO "EFI_MEMORY_WC mapping\n");
568 md->virt_addr = (u64) ioremap(md->phys_addr, 0);
569#endif
570 } else if (md->attribute & EFI_MEMORY_WT) {
571#if 0
572 md->virt_addr = ia64_remap(md->phys_addr, (_PAGE_A | _PAGE_P
573 | _PAGE_D | _PAGE_MA_WT
574 | _PAGE_PL_0
575 | _PAGE_AR_RW));
576#else
577 printk(KERN_INFO "EFI_MEMORY_WT mapping\n");
578 md->virt_addr = (u64) ioremap(md->phys_addr, 0);
579#endif
580 }
581 }
582 }
583
584 status = efi_call_phys(__va(runtime->set_virtual_address_map),
585 ia64_boot_param->efi_memmap_size,
586 efi_desc_size, ia64_boot_param->efi_memdesc_version,
587 ia64_boot_param->efi_memmap);
588 if (status != EFI_SUCCESS) {
589 printk(KERN_WARNING "warning: unable to switch EFI into virtual mode "
590 "(status=%lu)\n", status);
591 return;
592 }
593
594 /*
595 * Now that EFI is in virtual mode, we call the EFI functions more efficiently:
596 */
597 efi.get_time = virt_get_time;
598 efi.set_time = virt_set_time;
599 efi.get_wakeup_time = virt_get_wakeup_time;
600 efi.set_wakeup_time = virt_set_wakeup_time;
601 efi.get_variable = virt_get_variable;
602 efi.get_next_variable = virt_get_next_variable;
603 efi.set_variable = virt_set_variable;
604 efi.get_next_high_mono_count = virt_get_next_high_mono_count;
605 efi.reset_system = virt_reset_system;
606}
607
608/*
609 * Walk the EFI memory map looking for the I/O port range. There can only be one entry of
610 * this type, other I/O port ranges should be described via ACPI.
611 */
612u64
613efi_get_iobase (void)
614{
615 void *efi_map_start, *efi_map_end, *p;
616 efi_memory_desc_t *md;
617 u64 efi_desc_size;
618
619 efi_map_start = __va(ia64_boot_param->efi_memmap);
620 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
621 efi_desc_size = ia64_boot_param->efi_memdesc_size;
622
623 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
624 md = p;
625 if (md->type == EFI_MEMORY_MAPPED_IO_PORT_SPACE) {
626 if (md->attribute & EFI_MEMORY_UC)
627 return md->phys_addr;
628 }
629 }
630 return 0;
631}
632
Bjorn Helgaas32e62c62006-05-05 17:19:50 -0600633static struct kern_memdesc *
634kern_memory_descriptor (unsigned long phys_addr)
635{
636 struct kern_memdesc *md;
637
638 for (md = kern_memmap; md->start != ~0UL; md++) {
639 if (phys_addr - md->start < (md->num_pages << EFI_PAGE_SHIFT))
640 return md;
641 }
Keith Owense037cda2006-07-17 15:41:59 +1000642 return NULL;
Bjorn Helgaas32e62c62006-05-05 17:19:50 -0600643}
644
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800645static efi_memory_desc_t *
646efi_memory_descriptor (unsigned long phys_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647{
648 void *efi_map_start, *efi_map_end, *p;
649 efi_memory_desc_t *md;
650 u64 efi_desc_size;
651
652 efi_map_start = __va(ia64_boot_param->efi_memmap);
653 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
654 efi_desc_size = ia64_boot_param->efi_memdesc_size;
655
656 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
657 md = p;
658
659 if (phys_addr - md->phys_addr < (md->num_pages << EFI_PAGE_SHIFT))
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800660 return md;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 }
Keith Owense037cda2006-07-17 15:41:59 +1000662 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663}
664
Bjorn Helgaas6d40fc52007-03-30 10:35:43 -0600665static int
666efi_memmap_intersects (unsigned long phys_addr, unsigned long size)
667{
668 void *efi_map_start, *efi_map_end, *p;
669 efi_memory_desc_t *md;
670 u64 efi_desc_size;
671 unsigned long end;
672
673 efi_map_start = __va(ia64_boot_param->efi_memmap);
674 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
675 efi_desc_size = ia64_boot_param->efi_memdesc_size;
676
677 end = phys_addr + size;
678
679 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
680 md = p;
681
682 if (md->phys_addr < end && efi_md_end(md) > phys_addr)
683 return 1;
684 }
685 return 0;
686}
687
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800688u32
689efi_mem_type (unsigned long phys_addr)
690{
691 efi_memory_desc_t *md = efi_memory_descriptor(phys_addr);
692
693 if (md)
694 return md->type;
695 return 0;
696}
697
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698u64
699efi_mem_attributes (unsigned long phys_addr)
700{
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800701 efi_memory_desc_t *md = efi_memory_descriptor(phys_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800703 if (md)
704 return md->attribute;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 return 0;
706}
707EXPORT_SYMBOL(efi_mem_attributes);
708
Bjorn Helgaas32e62c62006-05-05 17:19:50 -0600709u64
710efi_mem_attribute (unsigned long phys_addr, unsigned long size)
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800711{
Bjorn Helgaas136939a2006-03-26 01:37:05 -0800712 unsigned long end = phys_addr + size;
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800713 efi_memory_desc_t *md = efi_memory_descriptor(phys_addr);
Bjorn Helgaas32e62c62006-05-05 17:19:50 -0600714 u64 attr;
715
716 if (!md)
717 return 0;
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800718
Bjorn Helgaas136939a2006-03-26 01:37:05 -0800719 /*
Bjorn Helgaas32e62c62006-05-05 17:19:50 -0600720 * EFI_MEMORY_RUNTIME is not a memory attribute; it just tells
721 * the kernel that firmware needs this region mapped.
Bjorn Helgaas136939a2006-03-26 01:37:05 -0800722 */
Bjorn Helgaas32e62c62006-05-05 17:19:50 -0600723 attr = md->attribute & ~EFI_MEMORY_RUNTIME;
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800724 do {
Bjorn Helgaas136939a2006-03-26 01:37:05 -0800725 unsigned long md_end = efi_md_end(md);
726
727 if (end <= md_end)
Bjorn Helgaas32e62c62006-05-05 17:19:50 -0600728 return attr;
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800729
730 md = efi_memory_descriptor(md_end);
Bjorn Helgaas32e62c62006-05-05 17:19:50 -0600731 if (!md || (md->attribute & ~EFI_MEMORY_RUNTIME) != attr)
Bjorn Helgaas136939a2006-03-26 01:37:05 -0800732 return 0;
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800733 } while (md);
734 return 0;
735}
736
Bjorn Helgaas32e62c62006-05-05 17:19:50 -0600737u64
738kern_mem_attribute (unsigned long phys_addr, unsigned long size)
739{
740 unsigned long end = phys_addr + size;
741 struct kern_memdesc *md;
742 u64 attr;
743
744 /*
745 * This is a hack for ioremap calls before we set up kern_memmap.
746 * Maybe we should do efi_memmap_init() earlier instead.
747 */
748 if (!kern_memmap) {
749 attr = efi_mem_attribute(phys_addr, size);
750 if (attr & EFI_MEMORY_WB)
751 return EFI_MEMORY_WB;
752 return 0;
753 }
754
755 md = kern_memory_descriptor(phys_addr);
756 if (!md)
757 return 0;
758
759 attr = md->attribute;
760 do {
761 unsigned long md_end = kmd_end(md);
762
763 if (end <= md_end)
764 return attr;
765
766 md = kern_memory_descriptor(md_end);
767 if (!md || md->attribute != attr)
768 return 0;
769 } while (md);
770 return 0;
771}
772EXPORT_SYMBOL(kern_mem_attribute);
773
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774int
Bjorn Helgaas136939a2006-03-26 01:37:05 -0800775valid_phys_addr_range (unsigned long phys_addr, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776{
Bjorn Helgaas32e62c62006-05-05 17:19:50 -0600777 u64 attr;
778
779 /*
780 * /dev/mem reads and writes use copy_to_user(), which implicitly
781 * uses a granule-sized kernel identity mapping. It's really
782 * only safe to do this for regions in kern_memmap. For more
783 * details, see Documentation/ia64/aliasing.txt.
784 */
785 attr = kern_mem_attribute(phys_addr, size);
786 if (attr & EFI_MEMORY_WB || attr & EFI_MEMORY_UC)
787 return 1;
788 return 0;
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800789}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800791int
Lennert Buytenhek06c67be2006-07-10 04:45:27 -0700792valid_mmap_phys_addr_range (unsigned long pfn, unsigned long size)
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800793{
Bjorn Helgaas6d40fc52007-03-30 10:35:43 -0600794 unsigned long phys_addr = pfn << PAGE_SHIFT;
795 u64 attr;
796
797 attr = efi_mem_attribute(phys_addr, size);
798
Bjorn Helgaas32e62c62006-05-05 17:19:50 -0600799 /*
Bjorn Helgaas6d40fc52007-03-30 10:35:43 -0600800 * /dev/mem mmap uses normal user pages, so we don't need the entire
801 * granule, but the entire region we're mapping must support the same
802 * attribute.
Bjorn Helgaas32e62c62006-05-05 17:19:50 -0600803 */
Bjorn Helgaas6d40fc52007-03-30 10:35:43 -0600804 if (attr & EFI_MEMORY_WB || attr & EFI_MEMORY_UC)
805 return 1;
806
807 /*
808 * Intel firmware doesn't tell us about all the MMIO regions, so
809 * in general we have to allow mmap requests. But if EFI *does*
810 * tell us about anything inside this region, we should deny it.
811 * The user can always map a smaller region to avoid the overlap.
812 */
813 if (efi_memmap_intersects(phys_addr, size))
814 return 0;
815
Bjorn Helgaas32e62c62006-05-05 17:19:50 -0600816 return 1;
817}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
Bjorn Helgaas32e62c62006-05-05 17:19:50 -0600819pgprot_t
820phys_mem_access_prot(struct file *file, unsigned long pfn, unsigned long size,
821 pgprot_t vma_prot)
822{
823 unsigned long phys_addr = pfn << PAGE_SHIFT;
824 u64 attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
Bjorn Helgaas32e62c62006-05-05 17:19:50 -0600826 /*
827 * For /dev/mem mmap, we use user mappings, but if the region is
828 * in kern_memmap (and hence may be covered by a kernel mapping),
829 * we must use the same attribute as the kernel mapping.
830 */
831 attr = kern_mem_attribute(phys_addr, size);
832 if (attr & EFI_MEMORY_WB)
833 return pgprot_cacheable(vma_prot);
834 else if (attr & EFI_MEMORY_UC)
835 return pgprot_noncached(vma_prot);
836
837 /*
838 * Some chipsets don't support UC access to memory. If
839 * WB is supported, we prefer that.
840 */
841 if (efi_mem_attribute(phys_addr, size) & EFI_MEMORY_WB)
842 return pgprot_cacheable(vma_prot);
843
844 return pgprot_noncached(vma_prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845}
846
847int __init
848efi_uart_console_only(void)
849{
850 efi_status_t status;
851 char *s, name[] = "ConOut";
852 efi_guid_t guid = EFI_GLOBAL_VARIABLE_GUID;
853 efi_char16_t *utf16, name_utf16[32];
854 unsigned char data[1024];
855 unsigned long size = sizeof(data);
856 struct efi_generic_dev_path *hdr, *end_addr;
857 int uart = 0;
858
859 /* Convert to UTF-16 */
860 utf16 = name_utf16;
861 s = name;
862 while (*s)
863 *utf16++ = *s++ & 0x7f;
864 *utf16 = 0;
865
866 status = efi.get_variable(name_utf16, &guid, NULL, &size, data);
867 if (status != EFI_SUCCESS) {
868 printk(KERN_ERR "No EFI %s variable?\n", name);
869 return 0;
870 }
871
872 hdr = (struct efi_generic_dev_path *) data;
873 end_addr = (struct efi_generic_dev_path *) ((u8 *) data + size);
874 while (hdr < end_addr) {
875 if (hdr->type == EFI_DEV_MSG &&
876 hdr->sub_type == EFI_DEV_MSG_UART)
877 uart = 1;
878 else if (hdr->type == EFI_DEV_END_PATH ||
879 hdr->type == EFI_DEV_END_PATH2) {
880 if (!uart)
881 return 0;
882 if (hdr->sub_type == EFI_DEV_END_ENTIRE)
883 return 1;
884 uart = 0;
885 }
886 hdr = (struct efi_generic_dev_path *) ((u8 *) hdr + hdr->length);
887 }
888 printk(KERN_ERR "Malformed %s value\n", name);
889 return 0;
890}
Tony Luckd8c97d52005-09-08 12:39:59 -0700891
Tony Luckd8c97d52005-09-08 12:39:59 -0700892/*
893 * Look for the first granule aligned memory descriptor memory
894 * that is big enough to hold EFI memory map. Make sure this
895 * descriptor is atleast granule sized so it does not get trimmed
896 */
897struct kern_memdesc *
898find_memmap_space (void)
899{
900 u64 contig_low=0, contig_high=0;
901 u64 as = 0, ae;
902 void *efi_map_start, *efi_map_end, *p, *q;
903 efi_memory_desc_t *md, *pmd = NULL, *check_md;
904 u64 space_needed, efi_desc_size;
905 unsigned long total_mem = 0;
906
907 efi_map_start = __va(ia64_boot_param->efi_memmap);
908 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
909 efi_desc_size = ia64_boot_param->efi_memdesc_size;
910
911 /*
912 * Worst case: we need 3 kernel descriptors for each efi descriptor
913 * (if every entry has a WB part in the middle, and UC head and tail),
914 * plus one for the end marker.
915 */
916 space_needed = sizeof(kern_memdesc_t) *
917 (3 * (ia64_boot_param->efi_memmap_size/efi_desc_size) + 1);
918
919 for (p = efi_map_start; p < efi_map_end; pmd = md, p += efi_desc_size) {
920 md = p;
921 if (!efi_wb(md)) {
922 continue;
923 }
924 if (pmd == NULL || !efi_wb(pmd) || efi_md_end(pmd) != md->phys_addr) {
925 contig_low = GRANULEROUNDUP(md->phys_addr);
926 contig_high = efi_md_end(md);
927 for (q = p + efi_desc_size; q < efi_map_end; q += efi_desc_size) {
928 check_md = q;
929 if (!efi_wb(check_md))
930 break;
931 if (contig_high != check_md->phys_addr)
932 break;
933 contig_high = efi_md_end(check_md);
934 }
935 contig_high = GRANULEROUNDDOWN(contig_high);
936 }
Christoph Lameter66888a62006-12-04 14:58:35 -0800937 if (!is_memory_available(md) || md->type == EFI_LOADER_DATA)
Tony Luckd8c97d52005-09-08 12:39:59 -0700938 continue;
939
940 /* Round ends inward to granule boundaries */
941 as = max(contig_low, md->phys_addr);
942 ae = min(contig_high, efi_md_end(md));
943
Zou Nan haia79561132006-12-07 09:51:35 -0800944 /* keep within max_addr= and min_addr= command line arg */
945 as = max(as, min_addr);
Tony Luckd8c97d52005-09-08 12:39:59 -0700946 ae = min(ae, max_addr);
947 if (ae <= as)
948 continue;
949
950 /* avoid going over mem= command line arg */
951 if (total_mem + (ae - as) > mem_limit)
952 ae -= total_mem + (ae - as) - mem_limit;
953
954 if (ae <= as)
955 continue;
956
957 if (ae - as > space_needed)
958 break;
959 }
960 if (p >= efi_map_end)
961 panic("Can't allocate space for kernel memory descriptors");
962
963 return __va(as);
964}
965
966/*
967 * Walk the EFI memory map and gather all memory available for kernel
968 * to use. We can allocate partial granules only if the unavailable
969 * parts exist, and are WB.
970 */
Bernhard Wallecb380852007-10-18 23:41:00 -0700971unsigned long
Tony Luckd8c97d52005-09-08 12:39:59 -0700972efi_memmap_init(unsigned long *s, unsigned long *e)
973{
Keith Owense037cda2006-07-17 15:41:59 +1000974 struct kern_memdesc *k, *prev = NULL;
Tony Luckd8c97d52005-09-08 12:39:59 -0700975 u64 contig_low=0, contig_high=0;
976 u64 as, ae, lim;
977 void *efi_map_start, *efi_map_end, *p, *q;
978 efi_memory_desc_t *md, *pmd = NULL, *check_md;
979 u64 efi_desc_size;
980 unsigned long total_mem = 0;
981
982 k = kern_memmap = find_memmap_space();
983
984 efi_map_start = __va(ia64_boot_param->efi_memmap);
985 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
986 efi_desc_size = ia64_boot_param->efi_memdesc_size;
987
988 for (p = efi_map_start; p < efi_map_end; pmd = md, p += efi_desc_size) {
989 md = p;
990 if (!efi_wb(md)) {
991 if (efi_uc(md) && (md->type == EFI_CONVENTIONAL_MEMORY ||
992 md->type == EFI_BOOT_SERVICES_DATA)) {
993 k->attribute = EFI_MEMORY_UC;
994 k->start = md->phys_addr;
995 k->num_pages = md->num_pages;
996 k++;
997 }
998 continue;
999 }
1000 if (pmd == NULL || !efi_wb(pmd) || efi_md_end(pmd) != md->phys_addr) {
1001 contig_low = GRANULEROUNDUP(md->phys_addr);
1002 contig_high = efi_md_end(md);
1003 for (q = p + efi_desc_size; q < efi_map_end; q += efi_desc_size) {
1004 check_md = q;
1005 if (!efi_wb(check_md))
1006 break;
1007 if (contig_high != check_md->phys_addr)
1008 break;
1009 contig_high = efi_md_end(check_md);
1010 }
1011 contig_high = GRANULEROUNDDOWN(contig_high);
1012 }
Christoph Lameter66888a62006-12-04 14:58:35 -08001013 if (!is_memory_available(md))
Tony Luckd8c97d52005-09-08 12:39:59 -07001014 continue;
1015
Tony Lucke55fdf12007-03-07 16:13:25 -08001016#ifdef CONFIG_CRASH_DUMP
1017 /* saved_max_pfn should ignore max_addr= command line arg */
1018 if (saved_max_pfn < (efi_md_end(md) >> PAGE_SHIFT))
1019 saved_max_pfn = (efi_md_end(md) >> PAGE_SHIFT);
1020#endif
Tony Luckd8c97d52005-09-08 12:39:59 -07001021 /*
1022 * Round ends inward to granule boundaries
1023 * Give trimmings to uncached allocator
1024 */
1025 if (md->phys_addr < contig_low) {
1026 lim = min(efi_md_end(md), contig_low);
1027 if (efi_uc(md)) {
1028 if (k > kern_memmap && (k-1)->attribute == EFI_MEMORY_UC &&
1029 kmd_end(k-1) == md->phys_addr) {
1030 (k-1)->num_pages += (lim - md->phys_addr) >> EFI_PAGE_SHIFT;
1031 } else {
1032 k->attribute = EFI_MEMORY_UC;
1033 k->start = md->phys_addr;
1034 k->num_pages = (lim - md->phys_addr) >> EFI_PAGE_SHIFT;
1035 k++;
1036 }
1037 }
1038 as = contig_low;
1039 } else
1040 as = md->phys_addr;
1041
1042 if (efi_md_end(md) > contig_high) {
1043 lim = max(md->phys_addr, contig_high);
1044 if (efi_uc(md)) {
1045 if (lim == md->phys_addr && k > kern_memmap &&
1046 (k-1)->attribute == EFI_MEMORY_UC &&
1047 kmd_end(k-1) == md->phys_addr) {
1048 (k-1)->num_pages += md->num_pages;
1049 } else {
1050 k->attribute = EFI_MEMORY_UC;
1051 k->start = lim;
1052 k->num_pages = (efi_md_end(md) - lim) >> EFI_PAGE_SHIFT;
1053 k++;
1054 }
1055 }
1056 ae = contig_high;
1057 } else
1058 ae = efi_md_end(md);
1059
Zou Nan haia79561132006-12-07 09:51:35 -08001060 /* keep within max_addr= and min_addr= command line arg */
1061 as = max(as, min_addr);
Tony Luckd8c97d52005-09-08 12:39:59 -07001062 ae = min(ae, max_addr);
1063 if (ae <= as)
1064 continue;
1065
1066 /* avoid going over mem= command line arg */
1067 if (total_mem + (ae - as) > mem_limit)
1068 ae -= total_mem + (ae - as) - mem_limit;
1069
1070 if (ae <= as)
1071 continue;
1072 if (prev && kmd_end(prev) == md->phys_addr) {
1073 prev->num_pages += (ae - as) >> EFI_PAGE_SHIFT;
1074 total_mem += ae - as;
1075 continue;
1076 }
1077 k->attribute = EFI_MEMORY_WB;
1078 k->start = as;
1079 k->num_pages = (ae - as) >> EFI_PAGE_SHIFT;
1080 total_mem += ae - as;
1081 prev = k++;
1082 }
1083 k->start = ~0L; /* end-marker */
1084
1085 /* reserve the memory we are using for kern_memmap */
1086 *s = (u64)kern_memmap;
1087 *e = (u64)++k;
Bernhard Wallecb380852007-10-18 23:41:00 -07001088
1089 return total_mem;
Tony Luckd8c97d52005-09-08 12:39:59 -07001090}
Khalid Azizbe379122005-09-19 15:42:36 -07001091
1092void
1093efi_initialize_iomem_resources(struct resource *code_resource,
Bernhard Walle00bf4092007-10-21 16:42:01 -07001094 struct resource *data_resource,
1095 struct resource *bss_resource)
Khalid Azizbe379122005-09-19 15:42:36 -07001096{
1097 struct resource *res;
1098 void *efi_map_start, *efi_map_end, *p;
1099 efi_memory_desc_t *md;
1100 u64 efi_desc_size;
1101 char *name;
1102 unsigned long flags;
1103
1104 efi_map_start = __va(ia64_boot_param->efi_memmap);
1105 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
1106 efi_desc_size = ia64_boot_param->efi_memdesc_size;
1107
1108 res = NULL;
1109
1110 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
1111 md = p;
1112
1113 if (md->num_pages == 0) /* should not happen */
1114 continue;
1115
Yasunori Goto887c3cb2007-11-14 16:59:20 -08001116 flags = IORESOURCE_MEM | IORESOURCE_BUSY;
Khalid Azizbe379122005-09-19 15:42:36 -07001117 switch (md->type) {
1118
1119 case EFI_MEMORY_MAPPED_IO:
1120 case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
1121 continue;
1122
1123 case EFI_LOADER_CODE:
1124 case EFI_LOADER_DATA:
1125 case EFI_BOOT_SERVICES_DATA:
1126 case EFI_BOOT_SERVICES_CODE:
1127 case EFI_CONVENTIONAL_MEMORY:
1128 if (md->attribute & EFI_MEMORY_WP) {
1129 name = "System ROM";
1130 flags |= IORESOURCE_READONLY;
1131 } else {
1132 name = "System RAM";
1133 }
1134 break;
1135
1136 case EFI_ACPI_MEMORY_NVS:
1137 name = "ACPI Non-volatile Storage";
Khalid Azizbe379122005-09-19 15:42:36 -07001138 break;
1139
1140 case EFI_UNUSABLE_MEMORY:
1141 name = "reserved";
Yasunori Goto887c3cb2007-11-14 16:59:20 -08001142 flags |= IORESOURCE_DISABLED;
Khalid Azizbe379122005-09-19 15:42:36 -07001143 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";
Khalid Azizbe379122005-09-19 15:42:36 -07001151 break;
1152 }
1153
Panagiotis Issarisbaf47fb2005-11-09 02:08:42 +01001154 if ((res = kzalloc(sizeof(struct resource), GFP_KERNEL)) == NULL) {
Khalid Azizbe379122005-09-19 15:42:36 -07001155 printk(KERN_ERR "failed to alocate resource for iomem\n");
1156 return;
1157 }
1158
1159 res->name = name;
1160 res->start = md->phys_addr;
1161 res->end = md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT) - 1;
1162 res->flags = flags;
1163
1164 if (insert_resource(&iomem_resource, res) < 0)
1165 kfree(res);
1166 else {
1167 /*
1168 * We don't know which region contains
1169 * kernel data so we try it repeatedly and
1170 * let the resource manager test it.
1171 */
1172 insert_resource(res, code_resource);
1173 insert_resource(res, data_resource);
Bernhard Walle00bf4092007-10-21 16:42:01 -07001174 insert_resource(res, bss_resource);
Zou Nan haia79561132006-12-07 09:51:35 -08001175#ifdef CONFIG_KEXEC
1176 insert_resource(res, &efi_memmap_res);
1177 insert_resource(res, &boot_param_res);
1178 if (crashk_res.end > crashk_res.start)
1179 insert_resource(res, &crashk_res);
1180#endif
Khalid Azizbe379122005-09-19 15:42:36 -07001181 }
1182 }
1183}
Zou Nan haia79561132006-12-07 09:51:35 -08001184
1185#ifdef CONFIG_KEXEC
1186/* find a block of memory aligned to 64M exclude reserved regions
1187 rsvd_regions are sorted
1188 */
Horms2a3a2822007-03-06 16:28:50 +09001189unsigned long __init
Zou Nan haia79561132006-12-07 09:51:35 -08001190kdump_find_rsvd_region (unsigned long size,
1191 struct rsvd_region *r, int n)
1192{
1193 int i;
1194 u64 start, end;
1195 u64 alignment = 1UL << _PAGE_SIZE_64M;
1196 void *efi_map_start, *efi_map_end, *p;
1197 efi_memory_desc_t *md;
1198 u64 efi_desc_size;
1199
1200 efi_map_start = __va(ia64_boot_param->efi_memmap);
1201 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
1202 efi_desc_size = ia64_boot_param->efi_memdesc_size;
1203
1204 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
1205 md = p;
1206 if (!efi_wb(md))
1207 continue;
1208 start = ALIGN(md->phys_addr, alignment);
1209 end = efi_md_end(md);
1210 for (i = 0; i < n; i++) {
1211 if (__pa(r[i].start) >= start && __pa(r[i].end) < end) {
1212 if (__pa(r[i].start) > start + size)
1213 return start;
1214 start = ALIGN(__pa(r[i].end), alignment);
1215 if (i < n-1 && __pa(r[i+1].start) < start + size)
1216 continue;
1217 else
1218 break;
1219 }
1220 }
1221 if (end > start + size)
1222 return start;
1223 }
1224
1225 printk(KERN_WARNING "Cannot reserve 0x%lx byte of memory for crashdump\n",
1226 size);
1227 return ~0UL;
1228}
1229#endif
Magnus Dammcee87af2007-03-06 02:34:26 -08001230
1231#ifdef CONFIG_PROC_VMCORE
1232/* locate the size find a the descriptor at a certain address */
Simon Horman1775fe852007-10-29 17:10:29 +09001233unsigned long __init
Magnus Dammcee87af2007-03-06 02:34:26 -08001234vmcore_find_descriptor_size (unsigned long address)
1235{
1236 void *efi_map_start, *efi_map_end, *p;
1237 efi_memory_desc_t *md;
1238 u64 efi_desc_size;
1239 unsigned long ret = 0;
1240
1241 efi_map_start = __va(ia64_boot_param->efi_memmap);
1242 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
1243 efi_desc_size = ia64_boot_param->efi_memdesc_size;
1244
1245 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
1246 md = p;
1247 if (efi_wb(md) && md->type == EFI_LOADER_DATA
1248 && md->phys_addr == address) {
1249 ret = efi_md_size(md);
1250 break;
1251 }
1252 }
1253
1254 if (ret == 0)
1255 printk(KERN_WARNING "Cannot locate EFI vmcore descriptor\n");
1256
1257 return ret;
1258}
1259#endif