blob: efaff15d8cf1dc9cad35a767a53b3865d51037b4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Extensible Firmware Interface
3 *
Aron Griffis7d9aed22008-02-04 15:31:49 -08004 * Based on Extensible Firmware Interface Specification version 0.9
5 * April 30, 1999
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * Copyright (C) 1999 VA Linux Systems
8 * Copyright (C) 1999 Walt Drummond <drummond@valinux.com>
9 * Copyright (C) 1999-2003 Hewlett-Packard Co.
10 * David Mosberger-Tang <davidm@hpl.hp.com>
11 * Stephane Eranian <eranian@hpl.hp.com>
Bjorn Helgaas32e62c62006-05-05 17:19:50 -060012 * (c) Copyright 2006 Hewlett-Packard Development Company, L.P.
13 * Bjorn Helgaas <bjorn.helgaas@hp.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 *
15 * All EFI Runtime Services are not implemented yet as EFI only
16 * supports physical mode addressing on SoftSDV. This is to be fixed
17 * in a future version. --drummond 1999-07-20
18 *
19 * Implemented EFI runtime services and virtual mode calls. --davidm
20 *
21 * Goutham Rao: <goutham.rao@intel.com>
22 * Skip non-WB memory and ignore empty memory ranges.
23 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/module.h>
Hormsf4a57092007-03-06 02:34:21 -080025#include <linux/bootmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/kernel.h>
27#include <linux/init.h>
28#include <linux/types.h>
29#include <linux/time.h>
30#include <linux/efi.h>
Zou Nan haia79561132006-12-07 09:51:35 -080031#include <linux/kexec.h>
Mel Gormaned7ed362007-07-17 04:03:14 -070032#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
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>
Fenghua Yu2046b942008-04-04 11:05:59 -070040#include <asm/tlbflush.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42#define EFI_DEBUG 0
43
44extern efi_status_t efi_call_phys (void *, ...);
45
46struct efi efi;
47EXPORT_SYMBOL(efi);
48static efi_runtime_services_t *runtime;
Zou Nan haia79561132006-12-07 09:51:35 -080049static unsigned long mem_limit = ~0UL, max_addr = ~0UL, min_addr = 0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51#define efi_call_virt(f, args...) (*(f))(args)
52
Aron Griffis7d9aed22008-02-04 15:31:49 -080053#define STUB_GET_TIME(prefix, adjust_arg) \
54static efi_status_t \
55prefix##_get_time (efi_time_t *tm, efi_time_cap_t *tc) \
56{ \
57 struct ia64_fpreg fr[6]; \
58 efi_time_cap_t *atc = NULL; \
59 efi_status_t ret; \
60 \
61 if (tc) \
62 atc = adjust_arg(tc); \
63 ia64_save_scratch_fpregs(fr); \
64 ret = efi_call_##prefix((efi_get_time_t *) __va(runtime->get_time), \
65 adjust_arg(tm), atc); \
66 ia64_load_scratch_fpregs(fr); \
67 return ret; \
Linus Torvalds1da177e2005-04-16 15:20:36 -070068}
69
Aron Griffis7d9aed22008-02-04 15:31:49 -080070#define STUB_SET_TIME(prefix, adjust_arg) \
71static efi_status_t \
72prefix##_set_time (efi_time_t *tm) \
73{ \
74 struct ia64_fpreg fr[6]; \
75 efi_status_t ret; \
76 \
77 ia64_save_scratch_fpregs(fr); \
78 ret = efi_call_##prefix((efi_set_time_t *) __va(runtime->set_time), \
79 adjust_arg(tm)); \
80 ia64_load_scratch_fpregs(fr); \
81 return ret; \
Linus Torvalds1da177e2005-04-16 15:20:36 -070082}
83
Aron Griffis7d9aed22008-02-04 15:31:49 -080084#define STUB_GET_WAKEUP_TIME(prefix, adjust_arg) \
85static efi_status_t \
86prefix##_get_wakeup_time (efi_bool_t *enabled, efi_bool_t *pending, \
87 efi_time_t *tm) \
88{ \
89 struct ia64_fpreg fr[6]; \
90 efi_status_t ret; \
91 \
92 ia64_save_scratch_fpregs(fr); \
93 ret = efi_call_##prefix( \
94 (efi_get_wakeup_time_t *) __va(runtime->get_wakeup_time), \
95 adjust_arg(enabled), adjust_arg(pending), adjust_arg(tm)); \
96 ia64_load_scratch_fpregs(fr); \
97 return ret; \
Linus Torvalds1da177e2005-04-16 15:20:36 -070098}
99
Aron Griffis7d9aed22008-02-04 15:31:49 -0800100#define STUB_SET_WAKEUP_TIME(prefix, adjust_arg) \
101static efi_status_t \
102prefix##_set_wakeup_time (efi_bool_t enabled, efi_time_t *tm) \
103{ \
104 struct ia64_fpreg fr[6]; \
105 efi_time_t *atm = NULL; \
106 efi_status_t ret; \
107 \
108 if (tm) \
109 atm = adjust_arg(tm); \
110 ia64_save_scratch_fpregs(fr); \
111 ret = efi_call_##prefix( \
112 (efi_set_wakeup_time_t *) __va(runtime->set_wakeup_time), \
113 enabled, atm); \
114 ia64_load_scratch_fpregs(fr); \
115 return ret; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116}
117
Aron Griffis7d9aed22008-02-04 15:31:49 -0800118#define STUB_GET_VARIABLE(prefix, adjust_arg) \
119static efi_status_t \
120prefix##_get_variable (efi_char16_t *name, efi_guid_t *vendor, u32 *attr, \
121 unsigned long *data_size, void *data) \
122{ \
123 struct ia64_fpreg fr[6]; \
124 u32 *aattr = NULL; \
125 efi_status_t ret; \
126 \
127 if (attr) \
128 aattr = adjust_arg(attr); \
129 ia64_save_scratch_fpregs(fr); \
130 ret = efi_call_##prefix( \
131 (efi_get_variable_t *) __va(runtime->get_variable), \
132 adjust_arg(name), adjust_arg(vendor), aattr, \
133 adjust_arg(data_size), adjust_arg(data)); \
134 ia64_load_scratch_fpregs(fr); \
135 return ret; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136}
137
Aron Griffis7d9aed22008-02-04 15:31:49 -0800138#define STUB_GET_NEXT_VARIABLE(prefix, adjust_arg) \
139static efi_status_t \
140prefix##_get_next_variable (unsigned long *name_size, efi_char16_t *name, \
141 efi_guid_t *vendor) \
142{ \
143 struct ia64_fpreg fr[6]; \
144 efi_status_t ret; \
145 \
146 ia64_save_scratch_fpregs(fr); \
147 ret = efi_call_##prefix( \
148 (efi_get_next_variable_t *) __va(runtime->get_next_variable), \
149 adjust_arg(name_size), adjust_arg(name), adjust_arg(vendor)); \
150 ia64_load_scratch_fpregs(fr); \
151 return ret; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152}
153
Aron Griffis7d9aed22008-02-04 15:31:49 -0800154#define STUB_SET_VARIABLE(prefix, adjust_arg) \
155static efi_status_t \
156prefix##_set_variable (efi_char16_t *name, efi_guid_t *vendor, \
157 unsigned long attr, unsigned long data_size, \
158 void *data) \
159{ \
160 struct ia64_fpreg fr[6]; \
161 efi_status_t ret; \
162 \
163 ia64_save_scratch_fpregs(fr); \
164 ret = efi_call_##prefix( \
165 (efi_set_variable_t *) __va(runtime->set_variable), \
166 adjust_arg(name), adjust_arg(vendor), attr, data_size, \
167 adjust_arg(data)); \
168 ia64_load_scratch_fpregs(fr); \
169 return ret; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170}
171
Aron Griffis7d9aed22008-02-04 15:31:49 -0800172#define STUB_GET_NEXT_HIGH_MONO_COUNT(prefix, adjust_arg) \
173static efi_status_t \
174prefix##_get_next_high_mono_count (u32 *count) \
175{ \
176 struct ia64_fpreg fr[6]; \
177 efi_status_t ret; \
178 \
179 ia64_save_scratch_fpregs(fr); \
180 ret = efi_call_##prefix((efi_get_next_high_mono_count_t *) \
181 __va(runtime->get_next_high_mono_count), \
182 adjust_arg(count)); \
183 ia64_load_scratch_fpregs(fr); \
184 return ret; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185}
186
Aron Griffis7d9aed22008-02-04 15:31:49 -0800187#define STUB_RESET_SYSTEM(prefix, adjust_arg) \
188static void \
189prefix##_reset_system (int reset_type, efi_status_t status, \
190 unsigned long data_size, efi_char16_t *data) \
191{ \
192 struct ia64_fpreg fr[6]; \
193 efi_char16_t *adata = NULL; \
194 \
195 if (data) \
196 adata = adjust_arg(data); \
197 \
198 ia64_save_scratch_fpregs(fr); \
199 efi_call_##prefix( \
200 (efi_reset_system_t *) __va(runtime->reset_system), \
201 reset_type, status, data_size, adata); \
202 /* should not return, but just in case... */ \
203 ia64_load_scratch_fpregs(fr); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204}
205
206#define phys_ptr(arg) ((__typeof__(arg)) ia64_tpa(arg))
207
208STUB_GET_TIME(phys, phys_ptr)
209STUB_SET_TIME(phys, phys_ptr)
210STUB_GET_WAKEUP_TIME(phys, phys_ptr)
211STUB_SET_WAKEUP_TIME(phys, phys_ptr)
212STUB_GET_VARIABLE(phys, phys_ptr)
213STUB_GET_NEXT_VARIABLE(phys, phys_ptr)
214STUB_SET_VARIABLE(phys, phys_ptr)
215STUB_GET_NEXT_HIGH_MONO_COUNT(phys, phys_ptr)
216STUB_RESET_SYSTEM(phys, phys_ptr)
217
218#define id(arg) arg
219
220STUB_GET_TIME(virt, id)
221STUB_SET_TIME(virt, id)
222STUB_GET_WAKEUP_TIME(virt, id)
223STUB_SET_WAKEUP_TIME(virt, id)
224STUB_GET_VARIABLE(virt, id)
225STUB_GET_NEXT_VARIABLE(virt, id)
226STUB_SET_VARIABLE(virt, id)
227STUB_GET_NEXT_HIGH_MONO_COUNT(virt, id)
228STUB_RESET_SYSTEM(virt, id)
229
230void
231efi_gettimeofday (struct timespec *ts)
232{
233 efi_time_t tm;
234
Li Zefan4b07ae92007-11-06 13:40:39 -0800235 if ((*efi.get_time)(&tm, NULL) != EFI_SUCCESS) {
236 memset(ts, 0, sizeof(*ts));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 return;
Li Zefan4b07ae92007-11-06 13:40:39 -0800238 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Aron Griffis7d9aed22008-02-04 15:31:49 -0800240 ts->tv_sec = mktime(tm.year, tm.month, tm.day,
241 tm.hour, tm.minute, tm.second);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 ts->tv_nsec = tm.nanosecond;
243}
244
245static int
Christoph Lameter66888a62006-12-04 14:58:35 -0800246is_memory_available (efi_memory_desc_t *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
248 if (!(md->attribute & EFI_MEMORY_WB))
249 return 0;
250
251 switch (md->type) {
252 case EFI_LOADER_CODE:
253 case EFI_LOADER_DATA:
254 case EFI_BOOT_SERVICES_CODE:
255 case EFI_BOOT_SERVICES_DATA:
256 case EFI_CONVENTIONAL_MEMORY:
257 return 1;
258 }
259 return 0;
260}
261
Tony Luckd8c97d52005-09-08 12:39:59 -0700262typedef struct kern_memdesc {
263 u64 attribute;
264 u64 start;
265 u64 num_pages;
266} kern_memdesc_t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Tony Luckd8c97d52005-09-08 12:39:59 -0700268static kern_memdesc_t *kern_memmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800270#define efi_md_size(md) (md->num_pages << EFI_PAGE_SHIFT)
271
272static inline u64
273kmd_end(kern_memdesc_t *kmd)
274{
275 return (kmd->start + (kmd->num_pages << EFI_PAGE_SHIFT));
276}
277
278static inline u64
279efi_md_end(efi_memory_desc_t *md)
280{
281 return (md->phys_addr + efi_md_size(md));
282}
283
284static inline int
285efi_wb(efi_memory_desc_t *md)
286{
287 return (md->attribute & EFI_MEMORY_WB);
288}
289
290static inline int
291efi_uc(efi_memory_desc_t *md)
292{
293 return (md->attribute & EFI_MEMORY_UC);
294}
295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296static void
Tony Luckd8c97d52005-09-08 12:39:59 -0700297walk (efi_freemem_callback_t callback, void *arg, u64 attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298{
Tony Luckd8c97d52005-09-08 12:39:59 -0700299 kern_memdesc_t *k;
300 u64 start, end, voff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Tony Luckd8c97d52005-09-08 12:39:59 -0700302 voff = (attr == EFI_MEMORY_WB) ? PAGE_OFFSET : __IA64_UNCACHED_OFFSET;
303 for (k = kern_memmap; k->start != ~0UL; k++) {
304 if (k->attribute != attr)
305 continue;
306 start = PAGE_ALIGN(k->start);
307 end = (k->start + (k->num_pages << EFI_PAGE_SHIFT)) & PAGE_MASK;
308 if (start < end)
309 if ((*callback)(start + voff, end + voff, arg) < 0)
310 return;
311 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312}
313
314/*
Aron Griffis965e7c82008-01-08 22:29:37 -0500315 * Walk the EFI memory map and call CALLBACK once for each EFI memory
Aron Griffis7d9aed22008-02-04 15:31:49 -0800316 * descriptor that has memory that is available for OS use.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 */
318void
319efi_memmap_walk (efi_freemem_callback_t callback, void *arg)
320{
Tony Luckd8c97d52005-09-08 12:39:59 -0700321 walk(callback, arg, EFI_MEMORY_WB);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322}
323
324/*
Aron Griffis965e7c82008-01-08 22:29:37 -0500325 * Walk the EFI memory map and call CALLBACK once for each EFI memory
Aron Griffis7d9aed22008-02-04 15:31:49 -0800326 * descriptor that has memory that is available for uncached allocator.
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700327 */
Tony Luckd8c97d52005-09-08 12:39:59 -0700328void
329efi_memmap_walk_uc (efi_freemem_callback_t callback, void *arg)
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700330{
Tony Luckd8c97d52005-09-08 12:39:59 -0700331 walk(callback, arg, EFI_MEMORY_UC);
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700332}
333
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700334/*
Aron Griffis965e7c82008-01-08 22:29:37 -0500335 * Look for the PAL_CODE region reported by EFI and map it using an
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 * ITR to enable safe PAL calls in virtual mode. See IA-64 Processor
337 * Abstraction Layer chapter 11 in ADAG
338 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339void *
340efi_get_pal_addr (void)
341{
342 void *efi_map_start, *efi_map_end, *p;
343 efi_memory_desc_t *md;
344 u64 efi_desc_size;
345 int pal_code_count = 0;
346 u64 vaddr, mask;
347
348 efi_map_start = __va(ia64_boot_param->efi_memmap);
349 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
350 efi_desc_size = ia64_boot_param->efi_memdesc_size;
351
352 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
353 md = p;
354 if (md->type != EFI_PAL_CODE)
355 continue;
356
357 if (++pal_code_count > 1) {
Aron Griffis7d9aed22008-02-04 15:31:49 -0800358 printk(KERN_ERR "Too many EFI Pal Code memory ranges, "
359 "dropped @ %lx\n", md->phys_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 continue;
361 }
362 /*
Aron Griffis7d9aed22008-02-04 15:31:49 -0800363 * The only ITLB entry in region 7 that is used is the one
364 * installed by __start(). That entry covers a 64MB range.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 */
366 mask = ~((1 << KERNEL_TR_PAGE_SHIFT) - 1);
367 vaddr = PAGE_OFFSET + md->phys_addr;
368
369 /*
Aron Griffis7d9aed22008-02-04 15:31:49 -0800370 * We must check that the PAL mapping won't overlap with the
371 * kernel mapping.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 *
Aron Griffis7d9aed22008-02-04 15:31:49 -0800373 * PAL code is guaranteed to be aligned on a power of 2 between
374 * 4k and 256KB and that only one ITR is needed to map it. This
375 * implies that the PAL code is always aligned on its size,
376 * i.e., the closest matching page size supported by the TLB.
377 * Therefore PAL code is guaranteed never to cross a 64MB unless
378 * it is bigger than 64MB (very unlikely!). So for now the
379 * following test is enough to determine whether or not we need
380 * a dedicated ITR for the PAL code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 */
382 if ((vaddr & mask) == (KERNEL_START & mask)) {
Harvey Harrisond4ed8082008-03-04 15:15:00 -0800383 printk(KERN_INFO "%s: no need to install ITR for PAL code\n",
384 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 continue;
386 }
387
Li Zefan685c7f52007-11-21 14:58:26 -0800388 if (efi_md_size(md) > IA64_GRANULE_SIZE)
Aron Griffis965e7c82008-01-08 22:29:37 -0500389 panic("Whoa! PAL code size bigger than a granule!");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
391#if EFI_DEBUG
392 mask = ~((1 << IA64_GRANULE_SHIFT) - 1);
393
Aron Griffis7d9aed22008-02-04 15:31:49 -0800394 printk(KERN_INFO "CPU %d: mapping PAL code "
395 "[0x%lx-0x%lx) into [0x%lx-0x%lx)\n",
396 smp_processor_id(), md->phys_addr,
397 md->phys_addr + efi_md_size(md),
398 vaddr & mask, (vaddr & mask) + IA64_GRANULE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399#endif
400 return __va(md->phys_addr);
401 }
Horms94732522007-02-05 10:17:38 +0900402 printk(KERN_WARNING "%s: no PAL-code memory-descriptor found\n",
Harvey Harrisond4ed8082008-03-04 15:15:00 -0800403 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 return NULL;
405}
406
Fenghua Yu2046b942008-04-04 11:05:59 -0700407
408static u8 __init palo_checksum(u8 *buffer, u32 length)
409{
410 u8 sum = 0;
411 u8 *end = buffer + length;
412
413 while (buffer < end)
414 sum = (u8) (sum + *(buffer++));
415
416 return sum;
417}
418
419/*
420 * Parse and handle PALO table which is published at:
421 * http://www.dig64.org/home/DIG64_PALO_R1_0.pdf
422 */
423static void __init handle_palo(unsigned long palo_phys)
424{
425 struct palo_table *palo = __va(palo_phys);
426 u8 checksum;
427
428 if (strncmp(palo->signature, PALO_SIG, sizeof(PALO_SIG) - 1)) {
429 printk(KERN_INFO "PALO signature incorrect.\n");
430 return;
431 }
432
433 checksum = palo_checksum((u8 *)palo, palo->length);
434 if (checksum) {
435 printk(KERN_INFO "PALO checksum incorrect.\n");
436 return;
437 }
438
Fenghua Yua6c75b862008-03-14 13:57:08 -0700439 setup_ptcg_sem(palo->max_tlb_purges, NPTCG_FROM_PALO);
Fenghua Yu2046b942008-04-04 11:05:59 -0700440}
441
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442void
443efi_map_pal_code (void)
444{
445 void *pal_vaddr = efi_get_pal_addr ();
446 u64 psr;
447
448 if (!pal_vaddr)
449 return;
450
451 /*
452 * Cannot write to CRx with PSR.ic=1
453 */
454 psr = ia64_clear_ic();
Aron Griffis7d9aed22008-02-04 15:31:49 -0800455 ia64_itr(0x1, IA64_TR_PALCODE,
456 GRANULEROUNDDOWN((unsigned long) pal_vaddr),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 pte_val(pfn_pte(__pa(pal_vaddr) >> PAGE_SHIFT, PAGE_KERNEL)),
458 IA64_GRANULE_SHIFT);
459 ia64_set_psr(psr); /* restore psr */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460}
461
462void __init
463efi_init (void)
464{
465 void *efi_map_start, *efi_map_end;
466 efi_config_table_t *config_tables;
467 efi_char16_t *c16;
468 u64 efi_desc_size;
Zou Nan hai9d78f432006-02-07 11:35:46 +0800469 char *cp, vendor[100] = "unknown";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 int i;
Fenghua Yu2046b942008-04-04 11:05:59 -0700471 unsigned long palo_phys;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
Aron Griffis7d9aed22008-02-04 15:31:49 -0800473 /*
Aron Griffis965e7c82008-01-08 22:29:37 -0500474 * It's too early to be able to use the standard kernel command line
Aron Griffis7d9aed22008-02-04 15:31:49 -0800475 * support...
476 */
Alon Bar-Leva8d91b82007-02-12 00:54:12 -0800477 for (cp = boot_command_line; *cp; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 if (memcmp(cp, "mem=", 4) == 0) {
Zou Nan hai9d78f432006-02-07 11:35:46 +0800479 mem_limit = memparse(cp + 4, &cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 } else if (memcmp(cp, "max_addr=", 9) == 0) {
Zou Nan hai9d78f432006-02-07 11:35:46 +0800481 max_addr = GRANULEROUNDDOWN(memparse(cp + 9, &cp));
Zou Nan haia79561132006-12-07 09:51:35 -0800482 } else if (memcmp(cp, "min_addr=", 9) == 0) {
483 min_addr = GRANULEROUNDDOWN(memparse(cp + 9, &cp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 } else {
485 while (*cp != ' ' && *cp)
486 ++cp;
487 while (*cp == ' ')
488 ++cp;
489 }
490 }
Zou Nan haia79561132006-12-07 09:51:35 -0800491 if (min_addr != 0UL)
Aron Griffis7d9aed22008-02-04 15:31:49 -0800492 printk(KERN_INFO "Ignoring memory below %luMB\n",
493 min_addr >> 20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 if (max_addr != ~0UL)
Aron Griffis7d9aed22008-02-04 15:31:49 -0800495 printk(KERN_INFO "Ignoring memory above %luMB\n",
496 max_addr >> 20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
498 efi.systab = __va(ia64_boot_param->efi_systab);
499
500 /*
501 * Verify the EFI Table
502 */
503 if (efi.systab == NULL)
Aron Griffis965e7c82008-01-08 22:29:37 -0500504 panic("Whoa! Can't find EFI system table.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
Aron Griffis965e7c82008-01-08 22:29:37 -0500506 panic("Whoa! EFI system table signature incorrect\n");
Bjorn Helgaas873ec742007-05-08 00:29:57 -0700507 if ((efi.systab->hdr.revision >> 16) == 0)
508 printk(KERN_WARNING "Warning: EFI system table version "
509 "%d.%02d, expected 1.00 or greater\n",
510 efi.systab->hdr.revision >> 16,
511 efi.systab->hdr.revision & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
513 config_tables = __va(efi.systab->tables);
514
515 /* Show what we know for posterity */
516 c16 = __va(efi.systab->fw_vendor);
517 if (c16) {
Zou Nan haiecdd5da2006-02-07 11:25:55 +0800518 for (i = 0;i < (int) sizeof(vendor) - 1 && *c16; ++i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 vendor[i] = *c16++;
520 vendor[i] = '\0';
521 }
522
523 printk(KERN_INFO "EFI v%u.%.02u by %s:",
Aron Griffis7d9aed22008-02-04 15:31:49 -0800524 efi.systab->hdr.revision >> 16,
525 efi.systab->hdr.revision & 0xffff, vendor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Bjorn Helgaasb2c99e32006-03-26 01:37:08 -0800527 efi.mps = EFI_INVALID_TABLE_ADDR;
528 efi.acpi = EFI_INVALID_TABLE_ADDR;
529 efi.acpi20 = EFI_INVALID_TABLE_ADDR;
530 efi.smbios = EFI_INVALID_TABLE_ADDR;
531 efi.sal_systab = EFI_INVALID_TABLE_ADDR;
532 efi.boot_info = EFI_INVALID_TABLE_ADDR;
533 efi.hcdp = EFI_INVALID_TABLE_ADDR;
534 efi.uga = EFI_INVALID_TABLE_ADDR;
535
Fenghua Yu2046b942008-04-04 11:05:59 -0700536 palo_phys = EFI_INVALID_TABLE_ADDR;
537
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 for (i = 0; i < (int) efi.systab->nr_tables; i++) {
539 if (efi_guidcmp(config_tables[i].guid, MPS_TABLE_GUID) == 0) {
Bjorn Helgaasb2c99e32006-03-26 01:37:08 -0800540 efi.mps = config_tables[i].table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 printk(" MPS=0x%lx", config_tables[i].table);
542 } else if (efi_guidcmp(config_tables[i].guid, ACPI_20_TABLE_GUID) == 0) {
Bjorn Helgaasb2c99e32006-03-26 01:37:08 -0800543 efi.acpi20 = config_tables[i].table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 printk(" ACPI 2.0=0x%lx", config_tables[i].table);
545 } else if (efi_guidcmp(config_tables[i].guid, ACPI_TABLE_GUID) == 0) {
Bjorn Helgaasb2c99e32006-03-26 01:37:08 -0800546 efi.acpi = config_tables[i].table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 printk(" ACPI=0x%lx", config_tables[i].table);
548 } else if (efi_guidcmp(config_tables[i].guid, SMBIOS_TABLE_GUID) == 0) {
Bjorn Helgaasb2c99e32006-03-26 01:37:08 -0800549 efi.smbios = config_tables[i].table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 printk(" SMBIOS=0x%lx", config_tables[i].table);
551 } else if (efi_guidcmp(config_tables[i].guid, SAL_SYSTEM_TABLE_GUID) == 0) {
Bjorn Helgaasb2c99e32006-03-26 01:37:08 -0800552 efi.sal_systab = config_tables[i].table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 printk(" SALsystab=0x%lx", config_tables[i].table);
554 } else if (efi_guidcmp(config_tables[i].guid, HCDP_TABLE_GUID) == 0) {
Bjorn Helgaasb2c99e32006-03-26 01:37:08 -0800555 efi.hcdp = config_tables[i].table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 printk(" HCDP=0x%lx", config_tables[i].table);
Fenghua Yu2046b942008-04-04 11:05:59 -0700557 } else if (efi_guidcmp(config_tables[i].guid,
558 PROCESSOR_ABSTRACTION_LAYER_OVERWRITE_GUID) == 0) {
559 palo_phys = config_tables[i].table;
560 printk(" PALO=0x%lx", config_tables[i].table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 }
562 }
563 printk("\n");
564
Fenghua Yu2046b942008-04-04 11:05:59 -0700565 if (palo_phys != EFI_INVALID_TABLE_ADDR)
566 handle_palo(palo_phys);
567
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 runtime = __va(efi.systab->runtime);
569 efi.get_time = phys_get_time;
570 efi.set_time = phys_set_time;
571 efi.get_wakeup_time = phys_get_wakeup_time;
572 efi.set_wakeup_time = phys_set_wakeup_time;
573 efi.get_variable = phys_get_variable;
574 efi.get_next_variable = phys_get_next_variable;
575 efi.set_variable = phys_set_variable;
576 efi.get_next_high_mono_count = phys_get_next_high_mono_count;
577 efi.reset_system = phys_reset_system;
578
579 efi_map_start = __va(ia64_boot_param->efi_memmap);
580 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
581 efi_desc_size = ia64_boot_param->efi_memdesc_size;
582
583#if EFI_DEBUG
584 /* print EFI memory map: */
585 {
586 efi_memory_desc_t *md;
587 void *p;
588
Aron Griffis7d9aed22008-02-04 15:31:49 -0800589 for (i = 0, p = efi_map_start; p < efi_map_end;
590 ++i, p += efi_desc_size)
591 {
Simon Horman818c7e82008-02-26 15:24:04 +0900592 const char *unit;
593 unsigned long size;
594
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 md = p;
Simon Horman818c7e82008-02-26 15:24:04 +0900596 size = md->num_pages << EFI_PAGE_SHIFT;
597
598 if ((size >> 40) > 0) {
599 size >>= 40;
600 unit = "TB";
601 } else if ((size >> 30) > 0) {
602 size >>= 30;
603 unit = "GB";
604 } else if ((size >> 20) > 0) {
605 size >>= 20;
606 unit = "MB";
607 } else {
608 size >>= 10;
609 unit = "KB";
610 }
611
612 printk("mem%02d: type=%2u, attr=0x%016lx, "
613 "range=[0x%016lx-0x%016lx) (%4lu%s)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 i, md->type, md->attribute, md->phys_addr,
Simon Horman818c7e82008-02-26 15:24:04 +0900615 md->phys_addr + efi_md_size(md), size, unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 }
617 }
618#endif
619
620 efi_map_pal_code();
621 efi_enter_virtual_mode();
622}
623
624void
625efi_enter_virtual_mode (void)
626{
627 void *efi_map_start, *efi_map_end, *p;
628 efi_memory_desc_t *md;
629 efi_status_t status;
630 u64 efi_desc_size;
631
632 efi_map_start = __va(ia64_boot_param->efi_memmap);
633 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
634 efi_desc_size = ia64_boot_param->efi_memdesc_size;
635
636 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
637 md = p;
638 if (md->attribute & EFI_MEMORY_RUNTIME) {
639 /*
Aron Griffis7d9aed22008-02-04 15:31:49 -0800640 * Some descriptors have multiple bits set, so the
641 * order of the tests is relevant.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 */
643 if (md->attribute & EFI_MEMORY_WB) {
644 md->virt_addr = (u64) __va(md->phys_addr);
645 } else if (md->attribute & EFI_MEMORY_UC) {
646 md->virt_addr = (u64) ioremap(md->phys_addr, 0);
647 } else if (md->attribute & EFI_MEMORY_WC) {
648#if 0
Aron Griffis7d9aed22008-02-04 15:31:49 -0800649 md->virt_addr = ia64_remap(md->phys_addr,
650 (_PAGE_A |
651 _PAGE_P |
652 _PAGE_D |
653 _PAGE_MA_WC |
654 _PAGE_PL_0 |
655 _PAGE_AR_RW));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656#else
657 printk(KERN_INFO "EFI_MEMORY_WC mapping\n");
658 md->virt_addr = (u64) ioremap(md->phys_addr, 0);
659#endif
660 } else if (md->attribute & EFI_MEMORY_WT) {
661#if 0
Aron Griffis7d9aed22008-02-04 15:31:49 -0800662 md->virt_addr = ia64_remap(md->phys_addr,
663 (_PAGE_A |
664 _PAGE_P |
665 _PAGE_D |
666 _PAGE_MA_WT |
667 _PAGE_PL_0 |
668 _PAGE_AR_RW));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669#else
670 printk(KERN_INFO "EFI_MEMORY_WT mapping\n");
671 md->virt_addr = (u64) ioremap(md->phys_addr, 0);
672#endif
673 }
674 }
675 }
676
677 status = efi_call_phys(__va(runtime->set_virtual_address_map),
678 ia64_boot_param->efi_memmap_size,
Aron Griffis7d9aed22008-02-04 15:31:49 -0800679 efi_desc_size,
680 ia64_boot_param->efi_memdesc_version,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 ia64_boot_param->efi_memmap);
682 if (status != EFI_SUCCESS) {
Aron Griffis7d9aed22008-02-04 15:31:49 -0800683 printk(KERN_WARNING "warning: unable to switch EFI into "
684 "virtual mode (status=%lu)\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 return;
686 }
687
688 /*
Aron Griffis7d9aed22008-02-04 15:31:49 -0800689 * Now that EFI is in virtual mode, we call the EFI functions more
690 * efficiently:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 */
692 efi.get_time = virt_get_time;
693 efi.set_time = virt_set_time;
694 efi.get_wakeup_time = virt_get_wakeup_time;
695 efi.set_wakeup_time = virt_set_wakeup_time;
696 efi.get_variable = virt_get_variable;
697 efi.get_next_variable = virt_get_next_variable;
698 efi.set_variable = virt_set_variable;
699 efi.get_next_high_mono_count = virt_get_next_high_mono_count;
700 efi.reset_system = virt_reset_system;
701}
702
703/*
Aron Griffis7d9aed22008-02-04 15:31:49 -0800704 * Walk the EFI memory map looking for the I/O port range. There can only be
705 * one entry of this type, other I/O port ranges should be described via ACPI.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 */
707u64
708efi_get_iobase (void)
709{
710 void *efi_map_start, *efi_map_end, *p;
711 efi_memory_desc_t *md;
712 u64 efi_desc_size;
713
714 efi_map_start = __va(ia64_boot_param->efi_memmap);
715 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
716 efi_desc_size = ia64_boot_param->efi_memdesc_size;
717
718 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
719 md = p;
720 if (md->type == EFI_MEMORY_MAPPED_IO_PORT_SPACE) {
721 if (md->attribute & EFI_MEMORY_UC)
722 return md->phys_addr;
723 }
724 }
725 return 0;
726}
727
Bjorn Helgaas32e62c62006-05-05 17:19:50 -0600728static struct kern_memdesc *
729kern_memory_descriptor (unsigned long phys_addr)
730{
731 struct kern_memdesc *md;
732
733 for (md = kern_memmap; md->start != ~0UL; md++) {
734 if (phys_addr - md->start < (md->num_pages << EFI_PAGE_SHIFT))
735 return md;
736 }
Keith Owense037cda2006-07-17 15:41:59 +1000737 return NULL;
Bjorn Helgaas32e62c62006-05-05 17:19:50 -0600738}
739
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800740static efi_memory_desc_t *
741efi_memory_descriptor (unsigned long phys_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742{
743 void *efi_map_start, *efi_map_end, *p;
744 efi_memory_desc_t *md;
745 u64 efi_desc_size;
746
747 efi_map_start = __va(ia64_boot_param->efi_memmap);
748 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
749 efi_desc_size = ia64_boot_param->efi_memdesc_size;
750
751 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
752 md = p;
753
Li Zefan685c7f52007-11-21 14:58:26 -0800754 if (phys_addr - md->phys_addr < efi_md_size(md))
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800755 return md;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 }
Keith Owense037cda2006-07-17 15:41:59 +1000757 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758}
759
Bjorn Helgaas6d40fc52007-03-30 10:35:43 -0600760static int
761efi_memmap_intersects (unsigned long phys_addr, unsigned long size)
762{
763 void *efi_map_start, *efi_map_end, *p;
764 efi_memory_desc_t *md;
765 u64 efi_desc_size;
766 unsigned long end;
767
768 efi_map_start = __va(ia64_boot_param->efi_memmap);
769 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
770 efi_desc_size = ia64_boot_param->efi_memdesc_size;
771
772 end = phys_addr + size;
773
774 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
775 md = p;
Bjorn Helgaas6d40fc52007-03-30 10:35:43 -0600776 if (md->phys_addr < end && efi_md_end(md) > phys_addr)
777 return 1;
778 }
779 return 0;
780}
781
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800782u32
783efi_mem_type (unsigned long phys_addr)
784{
785 efi_memory_desc_t *md = efi_memory_descriptor(phys_addr);
786
787 if (md)
788 return md->type;
789 return 0;
790}
791
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792u64
793efi_mem_attributes (unsigned long phys_addr)
794{
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800795 efi_memory_desc_t *md = efi_memory_descriptor(phys_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800797 if (md)
798 return md->attribute;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 return 0;
800}
801EXPORT_SYMBOL(efi_mem_attributes);
802
Bjorn Helgaas32e62c62006-05-05 17:19:50 -0600803u64
804efi_mem_attribute (unsigned long phys_addr, unsigned long size)
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800805{
Bjorn Helgaas136939a2006-03-26 01:37:05 -0800806 unsigned long end = phys_addr + size;
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800807 efi_memory_desc_t *md = efi_memory_descriptor(phys_addr);
Bjorn Helgaas32e62c62006-05-05 17:19:50 -0600808 u64 attr;
809
810 if (!md)
811 return 0;
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800812
Bjorn Helgaas136939a2006-03-26 01:37:05 -0800813 /*
Bjorn Helgaas32e62c62006-05-05 17:19:50 -0600814 * EFI_MEMORY_RUNTIME is not a memory attribute; it just tells
815 * the kernel that firmware needs this region mapped.
Bjorn Helgaas136939a2006-03-26 01:37:05 -0800816 */
Bjorn Helgaas32e62c62006-05-05 17:19:50 -0600817 attr = md->attribute & ~EFI_MEMORY_RUNTIME;
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800818 do {
Bjorn Helgaas136939a2006-03-26 01:37:05 -0800819 unsigned long md_end = efi_md_end(md);
820
821 if (end <= md_end)
Bjorn Helgaas32e62c62006-05-05 17:19:50 -0600822 return attr;
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800823
824 md = efi_memory_descriptor(md_end);
Bjorn Helgaas32e62c62006-05-05 17:19:50 -0600825 if (!md || (md->attribute & ~EFI_MEMORY_RUNTIME) != attr)
Bjorn Helgaas136939a2006-03-26 01:37:05 -0800826 return 0;
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800827 } while (md);
Aron Griffis410ab512008-01-08 22:29:38 -0500828 return 0; /* never reached */
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800829}
830
Bjorn Helgaas32e62c62006-05-05 17:19:50 -0600831u64
832kern_mem_attribute (unsigned long phys_addr, unsigned long size)
833{
834 unsigned long end = phys_addr + size;
835 struct kern_memdesc *md;
836 u64 attr;
837
838 /*
839 * This is a hack for ioremap calls before we set up kern_memmap.
840 * Maybe we should do efi_memmap_init() earlier instead.
841 */
842 if (!kern_memmap) {
843 attr = efi_mem_attribute(phys_addr, size);
844 if (attr & EFI_MEMORY_WB)
845 return EFI_MEMORY_WB;
846 return 0;
847 }
848
849 md = kern_memory_descriptor(phys_addr);
850 if (!md)
851 return 0;
852
853 attr = md->attribute;
854 do {
855 unsigned long md_end = kmd_end(md);
856
857 if (end <= md_end)
858 return attr;
859
860 md = kern_memory_descriptor(md_end);
861 if (!md || md->attribute != attr)
862 return 0;
863 } while (md);
Aron Griffis410ab512008-01-08 22:29:38 -0500864 return 0; /* never reached */
Bjorn Helgaas32e62c62006-05-05 17:19:50 -0600865}
866EXPORT_SYMBOL(kern_mem_attribute);
867
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868int
Bjorn Helgaas136939a2006-03-26 01:37:05 -0800869valid_phys_addr_range (unsigned long phys_addr, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870{
Bjorn Helgaas32e62c62006-05-05 17:19:50 -0600871 u64 attr;
872
873 /*
874 * /dev/mem reads and writes use copy_to_user(), which implicitly
875 * uses a granule-sized kernel identity mapping. It's really
876 * only safe to do this for regions in kern_memmap. For more
877 * details, see Documentation/ia64/aliasing.txt.
878 */
879 attr = kern_mem_attribute(phys_addr, size);
880 if (attr & EFI_MEMORY_WB || attr & EFI_MEMORY_UC)
881 return 1;
882 return 0;
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800883}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800885int
Lennert Buytenhek06c67be2006-07-10 04:45:27 -0700886valid_mmap_phys_addr_range (unsigned long pfn, unsigned long size)
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800887{
Bjorn Helgaas6d40fc52007-03-30 10:35:43 -0600888 unsigned long phys_addr = pfn << PAGE_SHIFT;
889 u64 attr;
890
891 attr = efi_mem_attribute(phys_addr, size);
892
Bjorn Helgaas32e62c62006-05-05 17:19:50 -0600893 /*
Bjorn Helgaas6d40fc52007-03-30 10:35:43 -0600894 * /dev/mem mmap uses normal user pages, so we don't need the entire
895 * granule, but the entire region we're mapping must support the same
896 * attribute.
Bjorn Helgaas32e62c62006-05-05 17:19:50 -0600897 */
Bjorn Helgaas6d40fc52007-03-30 10:35:43 -0600898 if (attr & EFI_MEMORY_WB || attr & EFI_MEMORY_UC)
899 return 1;
900
901 /*
902 * Intel firmware doesn't tell us about all the MMIO regions, so
903 * in general we have to allow mmap requests. But if EFI *does*
904 * tell us about anything inside this region, we should deny it.
905 * The user can always map a smaller region to avoid the overlap.
906 */
907 if (efi_memmap_intersects(phys_addr, size))
908 return 0;
909
Bjorn Helgaas32e62c62006-05-05 17:19:50 -0600910 return 1;
911}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
Bjorn Helgaas32e62c62006-05-05 17:19:50 -0600913pgprot_t
914phys_mem_access_prot(struct file *file, unsigned long pfn, unsigned long size,
915 pgprot_t vma_prot)
916{
917 unsigned long phys_addr = pfn << PAGE_SHIFT;
918 u64 attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
Bjorn Helgaas32e62c62006-05-05 17:19:50 -0600920 /*
921 * For /dev/mem mmap, we use user mappings, but if the region is
922 * in kern_memmap (and hence may be covered by a kernel mapping),
923 * we must use the same attribute as the kernel mapping.
924 */
925 attr = kern_mem_attribute(phys_addr, size);
926 if (attr & EFI_MEMORY_WB)
927 return pgprot_cacheable(vma_prot);
928 else if (attr & EFI_MEMORY_UC)
929 return pgprot_noncached(vma_prot);
930
931 /*
932 * Some chipsets don't support UC access to memory. If
933 * WB is supported, we prefer that.
934 */
935 if (efi_mem_attribute(phys_addr, size) & EFI_MEMORY_WB)
936 return pgprot_cacheable(vma_prot);
937
938 return pgprot_noncached(vma_prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939}
940
941int __init
942efi_uart_console_only(void)
943{
944 efi_status_t status;
945 char *s, name[] = "ConOut";
946 efi_guid_t guid = EFI_GLOBAL_VARIABLE_GUID;
947 efi_char16_t *utf16, name_utf16[32];
948 unsigned char data[1024];
949 unsigned long size = sizeof(data);
950 struct efi_generic_dev_path *hdr, *end_addr;
951 int uart = 0;
952
953 /* Convert to UTF-16 */
954 utf16 = name_utf16;
955 s = name;
956 while (*s)
957 *utf16++ = *s++ & 0x7f;
958 *utf16 = 0;
959
960 status = efi.get_variable(name_utf16, &guid, NULL, &size, data);
961 if (status != EFI_SUCCESS) {
962 printk(KERN_ERR "No EFI %s variable?\n", name);
963 return 0;
964 }
965
966 hdr = (struct efi_generic_dev_path *) data;
967 end_addr = (struct efi_generic_dev_path *) ((u8 *) data + size);
968 while (hdr < end_addr) {
969 if (hdr->type == EFI_DEV_MSG &&
970 hdr->sub_type == EFI_DEV_MSG_UART)
971 uart = 1;
972 else if (hdr->type == EFI_DEV_END_PATH ||
973 hdr->type == EFI_DEV_END_PATH2) {
974 if (!uart)
975 return 0;
976 if (hdr->sub_type == EFI_DEV_END_ENTIRE)
977 return 1;
978 uart = 0;
979 }
Aron Griffis7d9aed22008-02-04 15:31:49 -0800980 hdr = (struct efi_generic_dev_path *)((u8 *) hdr + hdr->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 }
982 printk(KERN_ERR "Malformed %s value\n", name);
983 return 0;
984}
Tony Luckd8c97d52005-09-08 12:39:59 -0700985
Tony Luckd8c97d52005-09-08 12:39:59 -0700986/*
987 * Look for the first granule aligned memory descriptor memory
988 * that is big enough to hold EFI memory map. Make sure this
989 * descriptor is atleast granule sized so it does not get trimmed
990 */
991struct kern_memdesc *
992find_memmap_space (void)
993{
994 u64 contig_low=0, contig_high=0;
995 u64 as = 0, ae;
996 void *efi_map_start, *efi_map_end, *p, *q;
997 efi_memory_desc_t *md, *pmd = NULL, *check_md;
998 u64 space_needed, efi_desc_size;
999 unsigned long total_mem = 0;
1000
1001 efi_map_start = __va(ia64_boot_param->efi_memmap);
1002 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
1003 efi_desc_size = ia64_boot_param->efi_memdesc_size;
1004
1005 /*
1006 * Worst case: we need 3 kernel descriptors for each efi descriptor
1007 * (if every entry has a WB part in the middle, and UC head and tail),
1008 * plus one for the end marker.
1009 */
1010 space_needed = sizeof(kern_memdesc_t) *
1011 (3 * (ia64_boot_param->efi_memmap_size/efi_desc_size) + 1);
1012
1013 for (p = efi_map_start; p < efi_map_end; pmd = md, p += efi_desc_size) {
1014 md = p;
1015 if (!efi_wb(md)) {
1016 continue;
1017 }
Aron Griffis7d9aed22008-02-04 15:31:49 -08001018 if (pmd == NULL || !efi_wb(pmd) ||
1019 efi_md_end(pmd) != md->phys_addr) {
Tony Luckd8c97d52005-09-08 12:39:59 -07001020 contig_low = GRANULEROUNDUP(md->phys_addr);
1021 contig_high = efi_md_end(md);
Aron Griffis7d9aed22008-02-04 15:31:49 -08001022 for (q = p + efi_desc_size; q < efi_map_end;
1023 q += efi_desc_size) {
Tony Luckd8c97d52005-09-08 12:39:59 -07001024 check_md = q;
1025 if (!efi_wb(check_md))
1026 break;
1027 if (contig_high != check_md->phys_addr)
1028 break;
1029 contig_high = efi_md_end(check_md);
1030 }
1031 contig_high = GRANULEROUNDDOWN(contig_high);
1032 }
Christoph Lameter66888a62006-12-04 14:58:35 -08001033 if (!is_memory_available(md) || md->type == EFI_LOADER_DATA)
Tony Luckd8c97d52005-09-08 12:39:59 -07001034 continue;
1035
1036 /* Round ends inward to granule boundaries */
1037 as = max(contig_low, md->phys_addr);
1038 ae = min(contig_high, efi_md_end(md));
1039
Zou Nan haia79561132006-12-07 09:51:35 -08001040 /* keep within max_addr= and min_addr= command line arg */
1041 as = max(as, min_addr);
Tony Luckd8c97d52005-09-08 12:39:59 -07001042 ae = min(ae, max_addr);
1043 if (ae <= as)
1044 continue;
1045
1046 /* avoid going over mem= command line arg */
1047 if (total_mem + (ae - as) > mem_limit)
1048 ae -= total_mem + (ae - as) - mem_limit;
1049
1050 if (ae <= as)
1051 continue;
1052
1053 if (ae - as > space_needed)
1054 break;
1055 }
1056 if (p >= efi_map_end)
1057 panic("Can't allocate space for kernel memory descriptors");
1058
1059 return __va(as);
1060}
1061
1062/*
1063 * Walk the EFI memory map and gather all memory available for kernel
1064 * to use. We can allocate partial granules only if the unavailable
1065 * parts exist, and are WB.
1066 */
Bernhard Wallecb380852007-10-18 23:41:00 -07001067unsigned long
Tony Luckd8c97d52005-09-08 12:39:59 -07001068efi_memmap_init(unsigned long *s, unsigned long *e)
1069{
Keith Owense037cda2006-07-17 15:41:59 +10001070 struct kern_memdesc *k, *prev = NULL;
Tony Luckd8c97d52005-09-08 12:39:59 -07001071 u64 contig_low=0, contig_high=0;
1072 u64 as, ae, lim;
1073 void *efi_map_start, *efi_map_end, *p, *q;
1074 efi_memory_desc_t *md, *pmd = NULL, *check_md;
1075 u64 efi_desc_size;
1076 unsigned long total_mem = 0;
1077
1078 k = kern_memmap = find_memmap_space();
1079
1080 efi_map_start = __va(ia64_boot_param->efi_memmap);
1081 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
1082 efi_desc_size = ia64_boot_param->efi_memdesc_size;
1083
1084 for (p = efi_map_start; p < efi_map_end; pmd = md, p += efi_desc_size) {
1085 md = p;
1086 if (!efi_wb(md)) {
Aron Griffis7d9aed22008-02-04 15:31:49 -08001087 if (efi_uc(md) &&
1088 (md->type == EFI_CONVENTIONAL_MEMORY ||
1089 md->type == EFI_BOOT_SERVICES_DATA)) {
Tony Luckd8c97d52005-09-08 12:39:59 -07001090 k->attribute = EFI_MEMORY_UC;
1091 k->start = md->phys_addr;
1092 k->num_pages = md->num_pages;
1093 k++;
1094 }
1095 continue;
1096 }
Aron Griffis7d9aed22008-02-04 15:31:49 -08001097 if (pmd == NULL || !efi_wb(pmd) ||
1098 efi_md_end(pmd) != md->phys_addr) {
Tony Luckd8c97d52005-09-08 12:39:59 -07001099 contig_low = GRANULEROUNDUP(md->phys_addr);
1100 contig_high = efi_md_end(md);
Aron Griffis7d9aed22008-02-04 15:31:49 -08001101 for (q = p + efi_desc_size; q < efi_map_end;
1102 q += efi_desc_size) {
Tony Luckd8c97d52005-09-08 12:39:59 -07001103 check_md = q;
1104 if (!efi_wb(check_md))
1105 break;
1106 if (contig_high != check_md->phys_addr)
1107 break;
1108 contig_high = efi_md_end(check_md);
1109 }
1110 contig_high = GRANULEROUNDDOWN(contig_high);
1111 }
Christoph Lameter66888a62006-12-04 14:58:35 -08001112 if (!is_memory_available(md))
Tony Luckd8c97d52005-09-08 12:39:59 -07001113 continue;
1114
Tony Lucke55fdf12007-03-07 16:13:25 -08001115#ifdef CONFIG_CRASH_DUMP
1116 /* saved_max_pfn should ignore max_addr= command line arg */
1117 if (saved_max_pfn < (efi_md_end(md) >> PAGE_SHIFT))
1118 saved_max_pfn = (efi_md_end(md) >> PAGE_SHIFT);
1119#endif
Tony Luckd8c97d52005-09-08 12:39:59 -07001120 /*
1121 * Round ends inward to granule boundaries
1122 * Give trimmings to uncached allocator
1123 */
1124 if (md->phys_addr < contig_low) {
1125 lim = min(efi_md_end(md), contig_low);
1126 if (efi_uc(md)) {
Aron Griffis7d9aed22008-02-04 15:31:49 -08001127 if (k > kern_memmap &&
1128 (k-1)->attribute == EFI_MEMORY_UC &&
Tony Luckd8c97d52005-09-08 12:39:59 -07001129 kmd_end(k-1) == md->phys_addr) {
Aron Griffis7d9aed22008-02-04 15:31:49 -08001130 (k-1)->num_pages +=
1131 (lim - md->phys_addr)
1132 >> EFI_PAGE_SHIFT;
Tony Luckd8c97d52005-09-08 12:39:59 -07001133 } else {
1134 k->attribute = EFI_MEMORY_UC;
1135 k->start = md->phys_addr;
Aron Griffis7d9aed22008-02-04 15:31:49 -08001136 k->num_pages = (lim - md->phys_addr)
1137 >> EFI_PAGE_SHIFT;
Tony Luckd8c97d52005-09-08 12:39:59 -07001138 k++;
1139 }
1140 }
1141 as = contig_low;
1142 } else
1143 as = md->phys_addr;
1144
1145 if (efi_md_end(md) > contig_high) {
1146 lim = max(md->phys_addr, contig_high);
1147 if (efi_uc(md)) {
1148 if (lim == md->phys_addr && k > kern_memmap &&
1149 (k-1)->attribute == EFI_MEMORY_UC &&
1150 kmd_end(k-1) == md->phys_addr) {
1151 (k-1)->num_pages += md->num_pages;
1152 } else {
1153 k->attribute = EFI_MEMORY_UC;
1154 k->start = lim;
Aron Griffis7d9aed22008-02-04 15:31:49 -08001155 k->num_pages = (efi_md_end(md) - lim)
1156 >> EFI_PAGE_SHIFT;
Tony Luckd8c97d52005-09-08 12:39:59 -07001157 k++;
1158 }
1159 }
1160 ae = contig_high;
1161 } else
1162 ae = efi_md_end(md);
1163
Zou Nan haia79561132006-12-07 09:51:35 -08001164 /* keep within max_addr= and min_addr= command line arg */
1165 as = max(as, min_addr);
Tony Luckd8c97d52005-09-08 12:39:59 -07001166 ae = min(ae, max_addr);
1167 if (ae <= as)
1168 continue;
1169
1170 /* avoid going over mem= command line arg */
1171 if (total_mem + (ae - as) > mem_limit)
1172 ae -= total_mem + (ae - as) - mem_limit;
1173
1174 if (ae <= as)
1175 continue;
1176 if (prev && kmd_end(prev) == md->phys_addr) {
1177 prev->num_pages += (ae - as) >> EFI_PAGE_SHIFT;
1178 total_mem += ae - as;
1179 continue;
1180 }
1181 k->attribute = EFI_MEMORY_WB;
1182 k->start = as;
1183 k->num_pages = (ae - as) >> EFI_PAGE_SHIFT;
1184 total_mem += ae - as;
1185 prev = k++;
1186 }
1187 k->start = ~0L; /* end-marker */
1188
1189 /* reserve the memory we are using for kern_memmap */
1190 *s = (u64)kern_memmap;
1191 *e = (u64)++k;
Bernhard Wallecb380852007-10-18 23:41:00 -07001192
1193 return total_mem;
Tony Luckd8c97d52005-09-08 12:39:59 -07001194}
Khalid Azizbe379122005-09-19 15:42:36 -07001195
1196void
1197efi_initialize_iomem_resources(struct resource *code_resource,
Bernhard Walle00bf4092007-10-21 16:42:01 -07001198 struct resource *data_resource,
1199 struct resource *bss_resource)
Khalid Azizbe379122005-09-19 15:42:36 -07001200{
1201 struct resource *res;
1202 void *efi_map_start, *efi_map_end, *p;
1203 efi_memory_desc_t *md;
1204 u64 efi_desc_size;
1205 char *name;
1206 unsigned long flags;
1207
1208 efi_map_start = __va(ia64_boot_param->efi_memmap);
1209 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
1210 efi_desc_size = ia64_boot_param->efi_memdesc_size;
1211
1212 res = NULL;
1213
1214 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
1215 md = p;
1216
1217 if (md->num_pages == 0) /* should not happen */
1218 continue;
1219
Yasunori Goto887c3cb2007-11-14 16:59:20 -08001220 flags = IORESOURCE_MEM | IORESOURCE_BUSY;
Khalid Azizbe379122005-09-19 15:42:36 -07001221 switch (md->type) {
1222
1223 case EFI_MEMORY_MAPPED_IO:
1224 case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
1225 continue;
1226
1227 case EFI_LOADER_CODE:
1228 case EFI_LOADER_DATA:
1229 case EFI_BOOT_SERVICES_DATA:
1230 case EFI_BOOT_SERVICES_CODE:
1231 case EFI_CONVENTIONAL_MEMORY:
1232 if (md->attribute & EFI_MEMORY_WP) {
1233 name = "System ROM";
1234 flags |= IORESOURCE_READONLY;
Jay Land3758f82008-09-22 14:21:19 -07001235 } else if (md->attribute == EFI_MEMORY_UC)
1236 name = "Uncached RAM";
1237 else
Khalid Azizbe379122005-09-19 15:42:36 -07001238 name = "System RAM";
Khalid Azizbe379122005-09-19 15:42:36 -07001239 break;
1240
1241 case EFI_ACPI_MEMORY_NVS:
1242 name = "ACPI Non-volatile Storage";
Khalid Azizbe379122005-09-19 15:42:36 -07001243 break;
1244
1245 case EFI_UNUSABLE_MEMORY:
1246 name = "reserved";
Yasunori Goto887c3cb2007-11-14 16:59:20 -08001247 flags |= IORESOURCE_DISABLED;
Khalid Azizbe379122005-09-19 15:42:36 -07001248 break;
1249
1250 case EFI_RESERVED_TYPE:
1251 case EFI_RUNTIME_SERVICES_CODE:
1252 case EFI_RUNTIME_SERVICES_DATA:
1253 case EFI_ACPI_RECLAIM_MEMORY:
1254 default:
1255 name = "reserved";
Khalid Azizbe379122005-09-19 15:42:36 -07001256 break;
1257 }
1258
Aron Griffis7d9aed22008-02-04 15:31:49 -08001259 if ((res = kzalloc(sizeof(struct resource),
1260 GFP_KERNEL)) == NULL) {
1261 printk(KERN_ERR
Aron Griffis965e7c82008-01-08 22:29:37 -05001262 "failed to allocate resource for iomem\n");
Khalid Azizbe379122005-09-19 15:42:36 -07001263 return;
1264 }
1265
1266 res->name = name;
1267 res->start = md->phys_addr;
Li Zefan685c7f52007-11-21 14:58:26 -08001268 res->end = md->phys_addr + efi_md_size(md) - 1;
Khalid Azizbe379122005-09-19 15:42:36 -07001269 res->flags = flags;
1270
1271 if (insert_resource(&iomem_resource, res) < 0)
1272 kfree(res);
1273 else {
1274 /*
1275 * We don't know which region contains
1276 * kernel data so we try it repeatedly and
1277 * let the resource manager test it.
1278 */
1279 insert_resource(res, code_resource);
1280 insert_resource(res, data_resource);
Bernhard Walle00bf4092007-10-21 16:42:01 -07001281 insert_resource(res, bss_resource);
Zou Nan haia79561132006-12-07 09:51:35 -08001282#ifdef CONFIG_KEXEC
1283 insert_resource(res, &efi_memmap_res);
1284 insert_resource(res, &boot_param_res);
1285 if (crashk_res.end > crashk_res.start)
1286 insert_resource(res, &crashk_res);
1287#endif
Khalid Azizbe379122005-09-19 15:42:36 -07001288 }
1289 }
1290}
Zou Nan haia79561132006-12-07 09:51:35 -08001291
1292#ifdef CONFIG_KEXEC
1293/* find a block of memory aligned to 64M exclude reserved regions
1294 rsvd_regions are sorted
1295 */
Horms2a3a2822007-03-06 16:28:50 +09001296unsigned long __init
Aron Griffis7d9aed22008-02-04 15:31:49 -08001297kdump_find_rsvd_region (unsigned long size, struct rsvd_region *r, int n)
Zou Nan haia79561132006-12-07 09:51:35 -08001298{
Aron Griffis7d9aed22008-02-04 15:31:49 -08001299 int i;
1300 u64 start, end;
1301 u64 alignment = 1UL << _PAGE_SIZE_64M;
1302 void *efi_map_start, *efi_map_end, *p;
1303 efi_memory_desc_t *md;
1304 u64 efi_desc_size;
Zou Nan haia79561132006-12-07 09:51:35 -08001305
Aron Griffis7d9aed22008-02-04 15:31:49 -08001306 efi_map_start = __va(ia64_boot_param->efi_memmap);
1307 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
1308 efi_desc_size = ia64_boot_param->efi_memdesc_size;
Zou Nan haia79561132006-12-07 09:51:35 -08001309
Aron Griffis7d9aed22008-02-04 15:31:49 -08001310 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
1311 md = p;
1312 if (!efi_wb(md))
1313 continue;
1314 start = ALIGN(md->phys_addr, alignment);
1315 end = efi_md_end(md);
1316 for (i = 0; i < n; i++) {
1317 if (__pa(r[i].start) >= start && __pa(r[i].end) < end) {
1318 if (__pa(r[i].start) > start + size)
1319 return start;
1320 start = ALIGN(__pa(r[i].end), alignment);
1321 if (i < n-1 &&
1322 __pa(r[i+1].start) < start + size)
1323 continue;
1324 else
1325 break;
1326 }
Zou Nan haia79561132006-12-07 09:51:35 -08001327 }
Aron Griffis7d9aed22008-02-04 15:31:49 -08001328 if (end > start + size)
1329 return start;
1330 }
Zou Nan haia79561132006-12-07 09:51:35 -08001331
Aron Griffis7d9aed22008-02-04 15:31:49 -08001332 printk(KERN_WARNING
1333 "Cannot reserve 0x%lx byte of memory for crashdump\n", size);
1334 return ~0UL;
Zou Nan haia79561132006-12-07 09:51:35 -08001335}
1336#endif
Magnus Dammcee87af2007-03-06 02:34:26 -08001337
Simon Hormand9a98552008-10-18 20:28:29 -07001338#ifdef CONFIG_CRASH_DUMP
Magnus Dammcee87af2007-03-06 02:34:26 -08001339/* locate the size find a the descriptor at a certain address */
Simon Horman1775fe852007-10-29 17:10:29 +09001340unsigned long __init
Magnus Dammcee87af2007-03-06 02:34:26 -08001341vmcore_find_descriptor_size (unsigned long address)
1342{
1343 void *efi_map_start, *efi_map_end, *p;
1344 efi_memory_desc_t *md;
1345 u64 efi_desc_size;
1346 unsigned long ret = 0;
1347
1348 efi_map_start = __va(ia64_boot_param->efi_memmap);
1349 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
1350 efi_desc_size = ia64_boot_param->efi_memdesc_size;
1351
1352 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
1353 md = p;
1354 if (efi_wb(md) && md->type == EFI_LOADER_DATA
1355 && md->phys_addr == address) {
1356 ret = efi_md_size(md);
1357 break;
1358 }
1359 }
1360
1361 if (ret == 0)
1362 printk(KERN_WARNING "Cannot locate EFI vmcore descriptor\n");
1363
1364 return ret;
1365}
1366#endif