blob: 121295637d0df831fbbf919983ee9a0cd1a7d366 [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>
Olaf Hering93a72052011-03-23 16:43:29 -070026#include <linux/crash_dump.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/kernel.h>
28#include <linux/init.h>
29#include <linux/types.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/time.h>
32#include <linux/efi.h>
Zou Nan haia79561132006-12-07 09:51:35 -080033#include <linux/kexec.h>
Mel Gormaned7ed362007-07-17 04:03:14 -070034#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36#include <asm/io.h>
37#include <asm/kregs.h>
38#include <asm/meminit.h>
39#include <asm/pgtable.h>
40#include <asm/processor.h>
41#include <asm/mca.h>
David Howellsc140d872012-03-28 18:30:02 +010042#include <asm/setup.h>
Fenghua Yu2046b942008-04-04 11:05:59 -070043#include <asm/tlbflush.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45#define EFI_DEBUG 0
46
Leif Lindholm272686b2013-09-05 11:34:54 +010047static __initdata unsigned long palo_phys;
48
49static __initdata efi_config_table_type_t arch_tables[] = {
50 {PROCESSOR_ABSTRACTION_LAYER_OVERWRITE_GUID, "PALO", &palo_phys},
51 {NULL_GUID, NULL, 0},
52};
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054extern efi_status_t efi_call_phys (void *, ...);
55
Linus Torvalds1da177e2005-04-16 15:20:36 -070056static efi_runtime_services_t *runtime;
Matthew Wilcoxe088a4a2009-05-22 13:49:49 -070057static u64 mem_limit = ~0UL, max_addr = ~0UL, min_addr = 0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59#define efi_call_virt(f, args...) (*(f))(args)
60
Aron Griffis7d9aed22008-02-04 15:31:49 -080061#define STUB_GET_TIME(prefix, adjust_arg) \
62static efi_status_t \
63prefix##_get_time (efi_time_t *tm, efi_time_cap_t *tc) \
64{ \
65 struct ia64_fpreg fr[6]; \
66 efi_time_cap_t *atc = NULL; \
67 efi_status_t ret; \
68 \
69 if (tc) \
70 atc = adjust_arg(tc); \
71 ia64_save_scratch_fpregs(fr); \
72 ret = efi_call_##prefix((efi_get_time_t *) __va(runtime->get_time), \
73 adjust_arg(tm), atc); \
74 ia64_load_scratch_fpregs(fr); \
75 return ret; \
Linus Torvalds1da177e2005-04-16 15:20:36 -070076}
77
Aron Griffis7d9aed22008-02-04 15:31:49 -080078#define STUB_SET_TIME(prefix, adjust_arg) \
79static efi_status_t \
80prefix##_set_time (efi_time_t *tm) \
81{ \
82 struct ia64_fpreg fr[6]; \
83 efi_status_t ret; \
84 \
85 ia64_save_scratch_fpregs(fr); \
86 ret = efi_call_##prefix((efi_set_time_t *) __va(runtime->set_time), \
87 adjust_arg(tm)); \
88 ia64_load_scratch_fpregs(fr); \
89 return ret; \
Linus Torvalds1da177e2005-04-16 15:20:36 -070090}
91
Aron Griffis7d9aed22008-02-04 15:31:49 -080092#define STUB_GET_WAKEUP_TIME(prefix, adjust_arg) \
93static efi_status_t \
94prefix##_get_wakeup_time (efi_bool_t *enabled, efi_bool_t *pending, \
95 efi_time_t *tm) \
96{ \
97 struct ia64_fpreg fr[6]; \
98 efi_status_t ret; \
99 \
100 ia64_save_scratch_fpregs(fr); \
101 ret = efi_call_##prefix( \
102 (efi_get_wakeup_time_t *) __va(runtime->get_wakeup_time), \
103 adjust_arg(enabled), adjust_arg(pending), adjust_arg(tm)); \
104 ia64_load_scratch_fpregs(fr); \
105 return ret; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106}
107
Aron Griffis7d9aed22008-02-04 15:31:49 -0800108#define STUB_SET_WAKEUP_TIME(prefix, adjust_arg) \
109static efi_status_t \
110prefix##_set_wakeup_time (efi_bool_t enabled, efi_time_t *tm) \
111{ \
112 struct ia64_fpreg fr[6]; \
113 efi_time_t *atm = NULL; \
114 efi_status_t ret; \
115 \
116 if (tm) \
117 atm = adjust_arg(tm); \
118 ia64_save_scratch_fpregs(fr); \
119 ret = efi_call_##prefix( \
120 (efi_set_wakeup_time_t *) __va(runtime->set_wakeup_time), \
121 enabled, atm); \
122 ia64_load_scratch_fpregs(fr); \
123 return ret; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124}
125
Aron Griffis7d9aed22008-02-04 15:31:49 -0800126#define STUB_GET_VARIABLE(prefix, adjust_arg) \
127static efi_status_t \
128prefix##_get_variable (efi_char16_t *name, efi_guid_t *vendor, u32 *attr, \
129 unsigned long *data_size, void *data) \
130{ \
131 struct ia64_fpreg fr[6]; \
132 u32 *aattr = NULL; \
133 efi_status_t ret; \
134 \
135 if (attr) \
136 aattr = adjust_arg(attr); \
137 ia64_save_scratch_fpregs(fr); \
138 ret = efi_call_##prefix( \
139 (efi_get_variable_t *) __va(runtime->get_variable), \
140 adjust_arg(name), adjust_arg(vendor), aattr, \
141 adjust_arg(data_size), adjust_arg(data)); \
142 ia64_load_scratch_fpregs(fr); \
143 return ret; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144}
145
Aron Griffis7d9aed22008-02-04 15:31:49 -0800146#define STUB_GET_NEXT_VARIABLE(prefix, adjust_arg) \
147static efi_status_t \
148prefix##_get_next_variable (unsigned long *name_size, efi_char16_t *name, \
149 efi_guid_t *vendor) \
150{ \
151 struct ia64_fpreg fr[6]; \
152 efi_status_t ret; \
153 \
154 ia64_save_scratch_fpregs(fr); \
155 ret = efi_call_##prefix( \
156 (efi_get_next_variable_t *) __va(runtime->get_next_variable), \
157 adjust_arg(name_size), adjust_arg(name), adjust_arg(vendor)); \
158 ia64_load_scratch_fpregs(fr); \
159 return ret; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160}
161
Aron Griffis7d9aed22008-02-04 15:31:49 -0800162#define STUB_SET_VARIABLE(prefix, adjust_arg) \
163static efi_status_t \
164prefix##_set_variable (efi_char16_t *name, efi_guid_t *vendor, \
Matthew Garrett1eb9a4b2011-07-06 16:48:49 -0400165 u32 attr, unsigned long data_size, \
Aron Griffis7d9aed22008-02-04 15:31:49 -0800166 void *data) \
167{ \
168 struct ia64_fpreg fr[6]; \
169 efi_status_t ret; \
170 \
171 ia64_save_scratch_fpregs(fr); \
172 ret = efi_call_##prefix( \
173 (efi_set_variable_t *) __va(runtime->set_variable), \
174 adjust_arg(name), adjust_arg(vendor), attr, data_size, \
175 adjust_arg(data)); \
176 ia64_load_scratch_fpregs(fr); \
177 return ret; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178}
179
Aron Griffis7d9aed22008-02-04 15:31:49 -0800180#define STUB_GET_NEXT_HIGH_MONO_COUNT(prefix, adjust_arg) \
181static efi_status_t \
182prefix##_get_next_high_mono_count (u32 *count) \
183{ \
184 struct ia64_fpreg fr[6]; \
185 efi_status_t ret; \
186 \
187 ia64_save_scratch_fpregs(fr); \
188 ret = efi_call_##prefix((efi_get_next_high_mono_count_t *) \
189 __va(runtime->get_next_high_mono_count), \
190 adjust_arg(count)); \
191 ia64_load_scratch_fpregs(fr); \
192 return ret; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193}
194
Aron Griffis7d9aed22008-02-04 15:31:49 -0800195#define STUB_RESET_SYSTEM(prefix, adjust_arg) \
196static void \
197prefix##_reset_system (int reset_type, efi_status_t status, \
198 unsigned long data_size, efi_char16_t *data) \
199{ \
200 struct ia64_fpreg fr[6]; \
201 efi_char16_t *adata = NULL; \
202 \
203 if (data) \
204 adata = adjust_arg(data); \
205 \
206 ia64_save_scratch_fpregs(fr); \
207 efi_call_##prefix( \
208 (efi_reset_system_t *) __va(runtime->reset_system), \
209 reset_type, status, data_size, adata); \
210 /* should not return, but just in case... */ \
211 ia64_load_scratch_fpregs(fr); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212}
213
214#define phys_ptr(arg) ((__typeof__(arg)) ia64_tpa(arg))
215
216STUB_GET_TIME(phys, phys_ptr)
217STUB_SET_TIME(phys, phys_ptr)
218STUB_GET_WAKEUP_TIME(phys, phys_ptr)
219STUB_SET_WAKEUP_TIME(phys, phys_ptr)
220STUB_GET_VARIABLE(phys, phys_ptr)
221STUB_GET_NEXT_VARIABLE(phys, phys_ptr)
222STUB_SET_VARIABLE(phys, phys_ptr)
223STUB_GET_NEXT_HIGH_MONO_COUNT(phys, phys_ptr)
224STUB_RESET_SYSTEM(phys, phys_ptr)
225
226#define id(arg) arg
227
228STUB_GET_TIME(virt, id)
229STUB_SET_TIME(virt, id)
230STUB_GET_WAKEUP_TIME(virt, id)
231STUB_SET_WAKEUP_TIME(virt, id)
232STUB_GET_VARIABLE(virt, id)
233STUB_GET_NEXT_VARIABLE(virt, id)
234STUB_SET_VARIABLE(virt, id)
235STUB_GET_NEXT_HIGH_MONO_COUNT(virt, id)
236STUB_RESET_SYSTEM(virt, id)
237
238void
Arnd Bergmann70f4f932016-06-17 16:48:17 +0200239efi_gettimeofday (struct timespec64 *ts)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240{
241 efi_time_t tm;
242
Li Zefan4b07ae92007-11-06 13:40:39 -0800243 if ((*efi.get_time)(&tm, NULL) != EFI_SUCCESS) {
244 memset(ts, 0, sizeof(*ts));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 return;
Li Zefan4b07ae92007-11-06 13:40:39 -0800246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Arnd Bergmann70f4f932016-06-17 16:48:17 +0200248 ts->tv_sec = mktime64(tm.year, tm.month, tm.day,
Aron Griffis7d9aed22008-02-04 15:31:49 -0800249 tm.hour, tm.minute, tm.second);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 ts->tv_nsec = tm.nanosecond;
251}
252
253static int
Christoph Lameter66888a62006-12-04 14:58:35 -0800254is_memory_available (efi_memory_desc_t *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
256 if (!(md->attribute & EFI_MEMORY_WB))
257 return 0;
258
259 switch (md->type) {
260 case EFI_LOADER_CODE:
261 case EFI_LOADER_DATA:
262 case EFI_BOOT_SERVICES_CODE:
263 case EFI_BOOT_SERVICES_DATA:
264 case EFI_CONVENTIONAL_MEMORY:
265 return 1;
266 }
267 return 0;
268}
269
Tony Luckd8c97d52005-09-08 12:39:59 -0700270typedef struct kern_memdesc {
271 u64 attribute;
272 u64 start;
273 u64 num_pages;
274} kern_memdesc_t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Tony Luckd8c97d52005-09-08 12:39:59 -0700276static kern_memdesc_t *kern_memmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800278#define efi_md_size(md) (md->num_pages << EFI_PAGE_SHIFT)
279
280static inline u64
281kmd_end(kern_memdesc_t *kmd)
282{
283 return (kmd->start + (kmd->num_pages << EFI_PAGE_SHIFT));
284}
285
286static inline u64
287efi_md_end(efi_memory_desc_t *md)
288{
289 return (md->phys_addr + efi_md_size(md));
290}
291
292static inline int
293efi_wb(efi_memory_desc_t *md)
294{
295 return (md->attribute & EFI_MEMORY_WB);
296}
297
298static inline int
299efi_uc(efi_memory_desc_t *md)
300{
301 return (md->attribute & EFI_MEMORY_UC);
302}
303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304static void
Tony Luckd8c97d52005-09-08 12:39:59 -0700305walk (efi_freemem_callback_t callback, void *arg, u64 attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
Tony Luckd8c97d52005-09-08 12:39:59 -0700307 kern_memdesc_t *k;
308 u64 start, end, voff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
Tony Luckd8c97d52005-09-08 12:39:59 -0700310 voff = (attr == EFI_MEMORY_WB) ? PAGE_OFFSET : __IA64_UNCACHED_OFFSET;
311 for (k = kern_memmap; k->start != ~0UL; k++) {
312 if (k->attribute != attr)
313 continue;
314 start = PAGE_ALIGN(k->start);
315 end = (k->start + (k->num_pages << EFI_PAGE_SHIFT)) & PAGE_MASK;
316 if (start < end)
317 if ((*callback)(start + voff, end + voff, arg) < 0)
318 return;
319 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320}
321
322/*
Aron Griffis965e7c82008-01-08 22:29:37 -0500323 * Walk the EFI memory map and call CALLBACK once for each EFI memory
Aron Griffis7d9aed22008-02-04 15:31:49 -0800324 * descriptor that has memory that is available for OS use.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 */
326void
327efi_memmap_walk (efi_freemem_callback_t callback, void *arg)
328{
Tony Luckd8c97d52005-09-08 12:39:59 -0700329 walk(callback, arg, EFI_MEMORY_WB);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330}
331
332/*
Aron Griffis965e7c82008-01-08 22:29:37 -0500333 * Walk the EFI memory map and call CALLBACK once for each EFI memory
Aron Griffis7d9aed22008-02-04 15:31:49 -0800334 * descriptor that has memory that is available for uncached allocator.
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700335 */
Tony Luckd8c97d52005-09-08 12:39:59 -0700336void
337efi_memmap_walk_uc (efi_freemem_callback_t callback, void *arg)
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700338{
Tony Luckd8c97d52005-09-08 12:39:59 -0700339 walk(callback, arg, EFI_MEMORY_UC);
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700340}
341
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700342/*
Aron Griffis965e7c82008-01-08 22:29:37 -0500343 * Look for the PAL_CODE region reported by EFI and map it using an
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 * ITR to enable safe PAL calls in virtual mode. See IA-64 Processor
345 * Abstraction Layer chapter 11 in ADAG
346 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347void *
348efi_get_pal_addr (void)
349{
350 void *efi_map_start, *efi_map_end, *p;
351 efi_memory_desc_t *md;
352 u64 efi_desc_size;
353 int pal_code_count = 0;
354 u64 vaddr, mask;
355
356 efi_map_start = __va(ia64_boot_param->efi_memmap);
357 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
358 efi_desc_size = ia64_boot_param->efi_memdesc_size;
359
360 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
361 md = p;
362 if (md->type != EFI_PAL_CODE)
363 continue;
364
365 if (++pal_code_count > 1) {
Aron Griffis7d9aed22008-02-04 15:31:49 -0800366 printk(KERN_ERR "Too many EFI Pal Code memory ranges, "
Matthew Wilcoxe088a4a2009-05-22 13:49:49 -0700367 "dropped @ %llx\n", md->phys_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 continue;
369 }
370 /*
Aron Griffis7d9aed22008-02-04 15:31:49 -0800371 * The only ITLB entry in region 7 that is used is the one
372 * installed by __start(). That entry covers a 64MB range.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 */
374 mask = ~((1 << KERNEL_TR_PAGE_SHIFT) - 1);
375 vaddr = PAGE_OFFSET + md->phys_addr;
376
377 /*
Aron Griffis7d9aed22008-02-04 15:31:49 -0800378 * We must check that the PAL mapping won't overlap with the
379 * kernel mapping.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 *
Aron Griffis7d9aed22008-02-04 15:31:49 -0800381 * PAL code is guaranteed to be aligned on a power of 2 between
382 * 4k and 256KB and that only one ITR is needed to map it. This
383 * implies that the PAL code is always aligned on its size,
384 * i.e., the closest matching page size supported by the TLB.
385 * Therefore PAL code is guaranteed never to cross a 64MB unless
386 * it is bigger than 64MB (very unlikely!). So for now the
387 * following test is enough to determine whether or not we need
388 * a dedicated ITR for the PAL code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 */
390 if ((vaddr & mask) == (KERNEL_START & mask)) {
Harvey Harrisond4ed8082008-03-04 15:15:00 -0800391 printk(KERN_INFO "%s: no need to install ITR for PAL code\n",
392 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 continue;
394 }
395
Li Zefan685c7f52007-11-21 14:58:26 -0800396 if (efi_md_size(md) > IA64_GRANULE_SIZE)
Aron Griffis965e7c82008-01-08 22:29:37 -0500397 panic("Whoa! PAL code size bigger than a granule!");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
399#if EFI_DEBUG
400 mask = ~((1 << IA64_GRANULE_SHIFT) - 1);
401
Aron Griffis7d9aed22008-02-04 15:31:49 -0800402 printk(KERN_INFO "CPU %d: mapping PAL code "
403 "[0x%lx-0x%lx) into [0x%lx-0x%lx)\n",
404 smp_processor_id(), md->phys_addr,
405 md->phys_addr + efi_md_size(md),
406 vaddr & mask, (vaddr & mask) + IA64_GRANULE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407#endif
408 return __va(md->phys_addr);
409 }
Horms94732522007-02-05 10:17:38 +0900410 printk(KERN_WARNING "%s: no PAL-code memory-descriptor found\n",
Harvey Harrisond4ed8082008-03-04 15:15:00 -0800411 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 return NULL;
413}
414
Fenghua Yu2046b942008-04-04 11:05:59 -0700415
416static u8 __init palo_checksum(u8 *buffer, u32 length)
417{
418 u8 sum = 0;
419 u8 *end = buffer + length;
420
421 while (buffer < end)
422 sum = (u8) (sum + *(buffer++));
423
424 return sum;
425}
426
427/*
428 * Parse and handle PALO table which is published at:
429 * http://www.dig64.org/home/DIG64_PALO_R1_0.pdf
430 */
Leif Lindholm272686b2013-09-05 11:34:54 +0100431static void __init handle_palo(unsigned long phys_addr)
Fenghua Yu2046b942008-04-04 11:05:59 -0700432{
Leif Lindholm272686b2013-09-05 11:34:54 +0100433 struct palo_table *palo = __va(phys_addr);
Fenghua Yu2046b942008-04-04 11:05:59 -0700434 u8 checksum;
435
436 if (strncmp(palo->signature, PALO_SIG, sizeof(PALO_SIG) - 1)) {
437 printk(KERN_INFO "PALO signature incorrect.\n");
438 return;
439 }
440
441 checksum = palo_checksum((u8 *)palo, palo->length);
442 if (checksum) {
443 printk(KERN_INFO "PALO checksum incorrect.\n");
444 return;
445 }
446
Fenghua Yua6c75b862008-03-14 13:57:08 -0700447 setup_ptcg_sem(palo->max_tlb_purges, NPTCG_FROM_PALO);
Fenghua Yu2046b942008-04-04 11:05:59 -0700448}
449
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450void
451efi_map_pal_code (void)
452{
453 void *pal_vaddr = efi_get_pal_addr ();
454 u64 psr;
455
456 if (!pal_vaddr)
457 return;
458
459 /*
460 * Cannot write to CRx with PSR.ic=1
461 */
462 psr = ia64_clear_ic();
Aron Griffis7d9aed22008-02-04 15:31:49 -0800463 ia64_itr(0x1, IA64_TR_PALCODE,
464 GRANULEROUNDDOWN((unsigned long) pal_vaddr),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 pte_val(pfn_pte(__pa(pal_vaddr) >> PAGE_SHIFT, PAGE_KERNEL)),
466 IA64_GRANULE_SHIFT);
467 ia64_set_psr(psr); /* restore psr */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468}
469
470void __init
471efi_init (void)
472{
473 void *efi_map_start, *efi_map_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 efi_char16_t *c16;
475 u64 efi_desc_size;
Zou Nan hai9d78f432006-02-07 11:35:46 +0800476 char *cp, vendor[100] = "unknown";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 int i;
478
Matt Fleming09206382014-01-15 13:49:51 +0000479 set_bit(EFI_BOOT, &efi.flags);
480 set_bit(EFI_64BIT, &efi.flags);
481
Aron Griffis7d9aed22008-02-04 15:31:49 -0800482 /*
Aron Griffis965e7c82008-01-08 22:29:37 -0500483 * It's too early to be able to use the standard kernel command line
Aron Griffis7d9aed22008-02-04 15:31:49 -0800484 * support...
485 */
Alon Bar-Leva8d91b82007-02-12 00:54:12 -0800486 for (cp = boot_command_line; *cp; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 if (memcmp(cp, "mem=", 4) == 0) {
Zou Nan hai9d78f432006-02-07 11:35:46 +0800488 mem_limit = memparse(cp + 4, &cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 } else if (memcmp(cp, "max_addr=", 9) == 0) {
Zou Nan hai9d78f432006-02-07 11:35:46 +0800490 max_addr = GRANULEROUNDDOWN(memparse(cp + 9, &cp));
Zou Nan haia79561132006-12-07 09:51:35 -0800491 } else if (memcmp(cp, "min_addr=", 9) == 0) {
492 min_addr = GRANULEROUNDDOWN(memparse(cp + 9, &cp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 } else {
494 while (*cp != ' ' && *cp)
495 ++cp;
496 while (*cp == ' ')
497 ++cp;
498 }
499 }
Zou Nan haia79561132006-12-07 09:51:35 -0800500 if (min_addr != 0UL)
Matthew Wilcoxe088a4a2009-05-22 13:49:49 -0700501 printk(KERN_INFO "Ignoring memory below %lluMB\n",
Aron Griffis7d9aed22008-02-04 15:31:49 -0800502 min_addr >> 20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 if (max_addr != ~0UL)
Matthew Wilcoxe088a4a2009-05-22 13:49:49 -0700504 printk(KERN_INFO "Ignoring memory above %lluMB\n",
Aron Griffis7d9aed22008-02-04 15:31:49 -0800505 max_addr >> 20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
507 efi.systab = __va(ia64_boot_param->efi_systab);
508
509 /*
510 * Verify the EFI Table
511 */
512 if (efi.systab == NULL)
Aron Griffis965e7c82008-01-08 22:29:37 -0500513 panic("Whoa! Can't find EFI system table.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
Aron Griffis965e7c82008-01-08 22:29:37 -0500515 panic("Whoa! EFI system table signature incorrect\n");
Bjorn Helgaas873ec742007-05-08 00:29:57 -0700516 if ((efi.systab->hdr.revision >> 16) == 0)
517 printk(KERN_WARNING "Warning: EFI system table version "
518 "%d.%02d, expected 1.00 or greater\n",
519 efi.systab->hdr.revision >> 16,
520 efi.systab->hdr.revision & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 /* Show what we know for posterity */
523 c16 = __va(efi.systab->fw_vendor);
524 if (c16) {
Zou Nan haiecdd5da2006-02-07 11:25:55 +0800525 for (i = 0;i < (int) sizeof(vendor) - 1 && *c16; ++i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 vendor[i] = *c16++;
527 vendor[i] = '\0';
528 }
529
530 printk(KERN_INFO "EFI v%u.%.02u by %s:",
Aron Griffis7d9aed22008-02-04 15:31:49 -0800531 efi.systab->hdr.revision >> 16,
532 efi.systab->hdr.revision & 0xffff, vendor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
Fenghua Yu2046b942008-04-04 11:05:59 -0700534 palo_phys = EFI_INVALID_TABLE_ADDR;
535
Leif Lindholm272686b2013-09-05 11:34:54 +0100536 if (efi_config_init(arch_tables) != 0)
537 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
Fenghua Yu2046b942008-04-04 11:05:59 -0700539 if (palo_phys != EFI_INVALID_TABLE_ADDR)
540 handle_palo(palo_phys);
541
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 runtime = __va(efi.systab->runtime);
543 efi.get_time = phys_get_time;
544 efi.set_time = phys_set_time;
545 efi.get_wakeup_time = phys_get_wakeup_time;
546 efi.set_wakeup_time = phys_set_wakeup_time;
547 efi.get_variable = phys_get_variable;
548 efi.get_next_variable = phys_get_next_variable;
549 efi.set_variable = phys_set_variable;
550 efi.get_next_high_mono_count = phys_get_next_high_mono_count;
551 efi.reset_system = phys_reset_system;
552
553 efi_map_start = __va(ia64_boot_param->efi_memmap);
554 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
555 efi_desc_size = ia64_boot_param->efi_memdesc_size;
556
557#if EFI_DEBUG
558 /* print EFI memory map: */
559 {
560 efi_memory_desc_t *md;
561 void *p;
562
Aron Griffis7d9aed22008-02-04 15:31:49 -0800563 for (i = 0, p = efi_map_start; p < efi_map_end;
564 ++i, p += efi_desc_size)
565 {
Simon Horman818c7e82008-02-26 15:24:04 +0900566 const char *unit;
567 unsigned long size;
Laszlo Ersek77b12bcf2014-09-03 13:32:22 +0200568 char buf[64];
Simon Horman818c7e82008-02-26 15:24:04 +0900569
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 md = p;
Simon Horman818c7e82008-02-26 15:24:04 +0900571 size = md->num_pages << EFI_PAGE_SHIFT;
572
573 if ((size >> 40) > 0) {
574 size >>= 40;
575 unit = "TB";
576 } else if ((size >> 30) > 0) {
577 size >>= 30;
578 unit = "GB";
579 } else if ((size >> 20) > 0) {
580 size >>= 20;
581 unit = "MB";
582 } else {
583 size >>= 10;
584 unit = "KB";
585 }
586
Laszlo Ersek77b12bcf2014-09-03 13:32:22 +0200587 printk("mem%02d: %s "
Simon Horman818c7e82008-02-26 15:24:04 +0900588 "range=[0x%016lx-0x%016lx) (%4lu%s)\n",
Laszlo Ersek77b12bcf2014-09-03 13:32:22 +0200589 i, efi_md_typeattr_format(buf, sizeof(buf), md),
590 md->phys_addr,
Simon Horman818c7e82008-02-26 15:24:04 +0900591 md->phys_addr + efi_md_size(md), size, unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 }
593 }
594#endif
595
596 efi_map_pal_code();
597 efi_enter_virtual_mode();
598}
599
600void
601efi_enter_virtual_mode (void)
602{
603 void *efi_map_start, *efi_map_end, *p;
604 efi_memory_desc_t *md;
605 efi_status_t status;
606 u64 efi_desc_size;
607
608 efi_map_start = __va(ia64_boot_param->efi_memmap);
609 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
610 efi_desc_size = ia64_boot_param->efi_memdesc_size;
611
612 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
613 md = p;
614 if (md->attribute & EFI_MEMORY_RUNTIME) {
615 /*
Aron Griffis7d9aed22008-02-04 15:31:49 -0800616 * Some descriptors have multiple bits set, so the
617 * order of the tests is relevant.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 */
619 if (md->attribute & EFI_MEMORY_WB) {
620 md->virt_addr = (u64) __va(md->phys_addr);
621 } else if (md->attribute & EFI_MEMORY_UC) {
622 md->virt_addr = (u64) ioremap(md->phys_addr, 0);
623 } else if (md->attribute & EFI_MEMORY_WC) {
624#if 0
Aron Griffis7d9aed22008-02-04 15:31:49 -0800625 md->virt_addr = ia64_remap(md->phys_addr,
626 (_PAGE_A |
627 _PAGE_P |
628 _PAGE_D |
629 _PAGE_MA_WC |
630 _PAGE_PL_0 |
631 _PAGE_AR_RW));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632#else
633 printk(KERN_INFO "EFI_MEMORY_WC mapping\n");
634 md->virt_addr = (u64) ioremap(md->phys_addr, 0);
635#endif
636 } else if (md->attribute & EFI_MEMORY_WT) {
637#if 0
Aron Griffis7d9aed22008-02-04 15:31:49 -0800638 md->virt_addr = ia64_remap(md->phys_addr,
639 (_PAGE_A |
640 _PAGE_P |
641 _PAGE_D |
642 _PAGE_MA_WT |
643 _PAGE_PL_0 |
644 _PAGE_AR_RW));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645#else
646 printk(KERN_INFO "EFI_MEMORY_WT mapping\n");
647 md->virt_addr = (u64) ioremap(md->phys_addr, 0);
648#endif
649 }
650 }
651 }
652
653 status = efi_call_phys(__va(runtime->set_virtual_address_map),
654 ia64_boot_param->efi_memmap_size,
Aron Griffis7d9aed22008-02-04 15:31:49 -0800655 efi_desc_size,
656 ia64_boot_param->efi_memdesc_version,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 ia64_boot_param->efi_memmap);
658 if (status != EFI_SUCCESS) {
Aron Griffis7d9aed22008-02-04 15:31:49 -0800659 printk(KERN_WARNING "warning: unable to switch EFI into "
660 "virtual mode (status=%lu)\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 return;
662 }
663
Matt Fleming09206382014-01-15 13:49:51 +0000664 set_bit(EFI_RUNTIME_SERVICES, &efi.flags);
665
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 /*
Aron Griffis7d9aed22008-02-04 15:31:49 -0800667 * Now that EFI is in virtual mode, we call the EFI functions more
668 * efficiently:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 */
670 efi.get_time = virt_get_time;
671 efi.set_time = virt_set_time;
672 efi.get_wakeup_time = virt_get_wakeup_time;
673 efi.set_wakeup_time = virt_set_wakeup_time;
674 efi.get_variable = virt_get_variable;
675 efi.get_next_variable = virt_get_next_variable;
676 efi.set_variable = virt_set_variable;
677 efi.get_next_high_mono_count = virt_get_next_high_mono_count;
678 efi.reset_system = virt_reset_system;
679}
680
681/*
Aron Griffis7d9aed22008-02-04 15:31:49 -0800682 * Walk the EFI memory map looking for the I/O port range. There can only be
683 * one entry of this type, other I/O port ranges should be described via ACPI.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 */
685u64
686efi_get_iobase (void)
687{
688 void *efi_map_start, *efi_map_end, *p;
689 efi_memory_desc_t *md;
690 u64 efi_desc_size;
691
692 efi_map_start = __va(ia64_boot_param->efi_memmap);
693 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
694 efi_desc_size = ia64_boot_param->efi_memdesc_size;
695
696 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
697 md = p;
698 if (md->type == EFI_MEMORY_MAPPED_IO_PORT_SPACE) {
699 if (md->attribute & EFI_MEMORY_UC)
700 return md->phys_addr;
701 }
702 }
703 return 0;
704}
705
Bjorn Helgaas32e62c62006-05-05 17:19:50 -0600706static struct kern_memdesc *
707kern_memory_descriptor (unsigned long phys_addr)
708{
709 struct kern_memdesc *md;
710
711 for (md = kern_memmap; md->start != ~0UL; md++) {
712 if (phys_addr - md->start < (md->num_pages << EFI_PAGE_SHIFT))
713 return md;
714 }
Keith Owense037cda2006-07-17 15:41:59 +1000715 return NULL;
Bjorn Helgaas32e62c62006-05-05 17:19:50 -0600716}
717
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800718static efi_memory_desc_t *
719efi_memory_descriptor (unsigned long phys_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720{
721 void *efi_map_start, *efi_map_end, *p;
722 efi_memory_desc_t *md;
723 u64 efi_desc_size;
724
725 efi_map_start = __va(ia64_boot_param->efi_memmap);
726 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
727 efi_desc_size = ia64_boot_param->efi_memdesc_size;
728
729 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
730 md = p;
731
Li Zefan685c7f52007-11-21 14:58:26 -0800732 if (phys_addr - md->phys_addr < efi_md_size(md))
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800733 return md;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 }
Keith Owense037cda2006-07-17 15:41:59 +1000735 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736}
737
Bjorn Helgaas6d40fc52007-03-30 10:35:43 -0600738static int
739efi_memmap_intersects (unsigned long phys_addr, unsigned long size)
740{
741 void *efi_map_start, *efi_map_end, *p;
742 efi_memory_desc_t *md;
743 u64 efi_desc_size;
744 unsigned long end;
745
746 efi_map_start = __va(ia64_boot_param->efi_memmap);
747 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
748 efi_desc_size = ia64_boot_param->efi_memdesc_size;
749
750 end = phys_addr + size;
751
752 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
753 md = p;
Bjorn Helgaas6d40fc52007-03-30 10:35:43 -0600754 if (md->phys_addr < end && efi_md_end(md) > phys_addr)
755 return 1;
756 }
757 return 0;
758}
759
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800760u32
761efi_mem_type (unsigned long phys_addr)
762{
763 efi_memory_desc_t *md = efi_memory_descriptor(phys_addr);
764
765 if (md)
766 return md->type;
767 return 0;
768}
769
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770u64
771efi_mem_attributes (unsigned long phys_addr)
772{
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800773 efi_memory_desc_t *md = efi_memory_descriptor(phys_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800775 if (md)
776 return md->attribute;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 return 0;
778}
779EXPORT_SYMBOL(efi_mem_attributes);
780
Bjorn Helgaas32e62c62006-05-05 17:19:50 -0600781u64
782efi_mem_attribute (unsigned long phys_addr, unsigned long size)
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800783{
Bjorn Helgaas136939a2006-03-26 01:37:05 -0800784 unsigned long end = phys_addr + size;
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800785 efi_memory_desc_t *md = efi_memory_descriptor(phys_addr);
Bjorn Helgaas32e62c62006-05-05 17:19:50 -0600786 u64 attr;
787
788 if (!md)
789 return 0;
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800790
Bjorn Helgaas136939a2006-03-26 01:37:05 -0800791 /*
Bjorn Helgaas32e62c62006-05-05 17:19:50 -0600792 * EFI_MEMORY_RUNTIME is not a memory attribute; it just tells
793 * the kernel that firmware needs this region mapped.
Bjorn Helgaas136939a2006-03-26 01:37:05 -0800794 */
Bjorn Helgaas32e62c62006-05-05 17:19:50 -0600795 attr = md->attribute & ~EFI_MEMORY_RUNTIME;
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800796 do {
Bjorn Helgaas136939a2006-03-26 01:37:05 -0800797 unsigned long md_end = efi_md_end(md);
798
799 if (end <= md_end)
Bjorn Helgaas32e62c62006-05-05 17:19:50 -0600800 return attr;
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800801
802 md = efi_memory_descriptor(md_end);
Bjorn Helgaas32e62c62006-05-05 17:19:50 -0600803 if (!md || (md->attribute & ~EFI_MEMORY_RUNTIME) != attr)
Bjorn Helgaas136939a2006-03-26 01:37:05 -0800804 return 0;
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800805 } while (md);
Aron Griffis410ab512008-01-08 22:29:38 -0500806 return 0; /* never reached */
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800807}
808
Bjorn Helgaas32e62c62006-05-05 17:19:50 -0600809u64
810kern_mem_attribute (unsigned long phys_addr, unsigned long size)
811{
812 unsigned long end = phys_addr + size;
813 struct kern_memdesc *md;
814 u64 attr;
815
816 /*
817 * This is a hack for ioremap calls before we set up kern_memmap.
818 * Maybe we should do efi_memmap_init() earlier instead.
819 */
820 if (!kern_memmap) {
821 attr = efi_mem_attribute(phys_addr, size);
822 if (attr & EFI_MEMORY_WB)
823 return EFI_MEMORY_WB;
824 return 0;
825 }
826
827 md = kern_memory_descriptor(phys_addr);
828 if (!md)
829 return 0;
830
831 attr = md->attribute;
832 do {
833 unsigned long md_end = kmd_end(md);
834
835 if (end <= md_end)
836 return attr;
837
838 md = kern_memory_descriptor(md_end);
839 if (!md || md->attribute != attr)
840 return 0;
841 } while (md);
Aron Griffis410ab512008-01-08 22:29:38 -0500842 return 0; /* never reached */
Bjorn Helgaas32e62c62006-05-05 17:19:50 -0600843}
844EXPORT_SYMBOL(kern_mem_attribute);
845
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846int
Cyril Chemparathy7e6735c2012-09-12 14:05:58 -0400847valid_phys_addr_range (phys_addr_t phys_addr, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848{
Bjorn Helgaas32e62c62006-05-05 17:19:50 -0600849 u64 attr;
850
851 /*
852 * /dev/mem reads and writes use copy_to_user(), which implicitly
853 * uses a granule-sized kernel identity mapping. It's really
854 * only safe to do this for regions in kern_memmap. For more
855 * details, see Documentation/ia64/aliasing.txt.
856 */
857 attr = kern_mem_attribute(phys_addr, size);
858 if (attr & EFI_MEMORY_WB || attr & EFI_MEMORY_UC)
859 return 1;
860 return 0;
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800861}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800863int
Lennert Buytenhek06c67be2006-07-10 04:45:27 -0700864valid_mmap_phys_addr_range (unsigned long pfn, unsigned long size)
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800865{
Bjorn Helgaas6d40fc52007-03-30 10:35:43 -0600866 unsigned long phys_addr = pfn << PAGE_SHIFT;
867 u64 attr;
868
869 attr = efi_mem_attribute(phys_addr, size);
870
Bjorn Helgaas32e62c62006-05-05 17:19:50 -0600871 /*
Bjorn Helgaas6d40fc52007-03-30 10:35:43 -0600872 * /dev/mem mmap uses normal user pages, so we don't need the entire
873 * granule, but the entire region we're mapping must support the same
874 * attribute.
Bjorn Helgaas32e62c62006-05-05 17:19:50 -0600875 */
Bjorn Helgaas6d40fc52007-03-30 10:35:43 -0600876 if (attr & EFI_MEMORY_WB || attr & EFI_MEMORY_UC)
877 return 1;
878
879 /*
880 * Intel firmware doesn't tell us about all the MMIO regions, so
881 * in general we have to allow mmap requests. But if EFI *does*
882 * tell us about anything inside this region, we should deny it.
883 * The user can always map a smaller region to avoid the overlap.
884 */
885 if (efi_memmap_intersects(phys_addr, size))
886 return 0;
887
Bjorn Helgaas32e62c62006-05-05 17:19:50 -0600888 return 1;
889}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
Bjorn Helgaas32e62c62006-05-05 17:19:50 -0600891pgprot_t
892phys_mem_access_prot(struct file *file, unsigned long pfn, unsigned long size,
893 pgprot_t vma_prot)
894{
895 unsigned long phys_addr = pfn << PAGE_SHIFT;
896 u64 attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
Bjorn Helgaas32e62c62006-05-05 17:19:50 -0600898 /*
899 * For /dev/mem mmap, we use user mappings, but if the region is
900 * in kern_memmap (and hence may be covered by a kernel mapping),
901 * we must use the same attribute as the kernel mapping.
902 */
903 attr = kern_mem_attribute(phys_addr, size);
904 if (attr & EFI_MEMORY_WB)
905 return pgprot_cacheable(vma_prot);
906 else if (attr & EFI_MEMORY_UC)
907 return pgprot_noncached(vma_prot);
908
909 /*
910 * Some chipsets don't support UC access to memory. If
911 * WB is supported, we prefer that.
912 */
913 if (efi_mem_attribute(phys_addr, size) & EFI_MEMORY_WB)
914 return pgprot_cacheable(vma_prot);
915
916 return pgprot_noncached(vma_prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917}
918
919int __init
920efi_uart_console_only(void)
921{
922 efi_status_t status;
923 char *s, name[] = "ConOut";
924 efi_guid_t guid = EFI_GLOBAL_VARIABLE_GUID;
925 efi_char16_t *utf16, name_utf16[32];
926 unsigned char data[1024];
927 unsigned long size = sizeof(data);
928 struct efi_generic_dev_path *hdr, *end_addr;
929 int uart = 0;
930
931 /* Convert to UTF-16 */
932 utf16 = name_utf16;
933 s = name;
934 while (*s)
935 *utf16++ = *s++ & 0x7f;
936 *utf16 = 0;
937
938 status = efi.get_variable(name_utf16, &guid, NULL, &size, data);
939 if (status != EFI_SUCCESS) {
940 printk(KERN_ERR "No EFI %s variable?\n", name);
941 return 0;
942 }
943
944 hdr = (struct efi_generic_dev_path *) data;
945 end_addr = (struct efi_generic_dev_path *) ((u8 *) data + size);
946 while (hdr < end_addr) {
947 if (hdr->type == EFI_DEV_MSG &&
948 hdr->sub_type == EFI_DEV_MSG_UART)
949 uart = 1;
950 else if (hdr->type == EFI_DEV_END_PATH ||
951 hdr->type == EFI_DEV_END_PATH2) {
952 if (!uart)
953 return 0;
954 if (hdr->sub_type == EFI_DEV_END_ENTIRE)
955 return 1;
956 uart = 0;
957 }
Aron Griffis7d9aed22008-02-04 15:31:49 -0800958 hdr = (struct efi_generic_dev_path *)((u8 *) hdr + hdr->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 }
960 printk(KERN_ERR "Malformed %s value\n", name);
961 return 0;
962}
Tony Luckd8c97d52005-09-08 12:39:59 -0700963
Tony Luckd8c97d52005-09-08 12:39:59 -0700964/*
965 * Look for the first granule aligned memory descriptor memory
966 * that is big enough to hold EFI memory map. Make sure this
Adam Buchbinder45b79a22016-02-24 10:50:29 -0800967 * descriptor is at least granule sized so it does not get trimmed
Tony Luckd8c97d52005-09-08 12:39:59 -0700968 */
969struct kern_memdesc *
970find_memmap_space (void)
971{
972 u64 contig_low=0, contig_high=0;
973 u64 as = 0, ae;
974 void *efi_map_start, *efi_map_end, *p, *q;
975 efi_memory_desc_t *md, *pmd = NULL, *check_md;
976 u64 space_needed, efi_desc_size;
977 unsigned long total_mem = 0;
978
979 efi_map_start = __va(ia64_boot_param->efi_memmap);
980 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
981 efi_desc_size = ia64_boot_param->efi_memdesc_size;
982
983 /*
984 * Worst case: we need 3 kernel descriptors for each efi descriptor
985 * (if every entry has a WB part in the middle, and UC head and tail),
986 * plus one for the end marker.
987 */
988 space_needed = sizeof(kern_memdesc_t) *
989 (3 * (ia64_boot_param->efi_memmap_size/efi_desc_size) + 1);
990
991 for (p = efi_map_start; p < efi_map_end; pmd = md, p += efi_desc_size) {
992 md = p;
993 if (!efi_wb(md)) {
994 continue;
995 }
Aron Griffis7d9aed22008-02-04 15:31:49 -0800996 if (pmd == NULL || !efi_wb(pmd) ||
997 efi_md_end(pmd) != md->phys_addr) {
Tony Luckd8c97d52005-09-08 12:39:59 -0700998 contig_low = GRANULEROUNDUP(md->phys_addr);
999 contig_high = efi_md_end(md);
Aron Griffis7d9aed22008-02-04 15:31:49 -08001000 for (q = p + efi_desc_size; q < efi_map_end;
1001 q += efi_desc_size) {
Tony Luckd8c97d52005-09-08 12:39:59 -07001002 check_md = q;
1003 if (!efi_wb(check_md))
1004 break;
1005 if (contig_high != check_md->phys_addr)
1006 break;
1007 contig_high = efi_md_end(check_md);
1008 }
1009 contig_high = GRANULEROUNDDOWN(contig_high);
1010 }
Christoph Lameter66888a62006-12-04 14:58:35 -08001011 if (!is_memory_available(md) || md->type == EFI_LOADER_DATA)
Tony Luckd8c97d52005-09-08 12:39:59 -07001012 continue;
1013
1014 /* Round ends inward to granule boundaries */
1015 as = max(contig_low, md->phys_addr);
1016 ae = min(contig_high, efi_md_end(md));
1017
Zou Nan haia79561132006-12-07 09:51:35 -08001018 /* keep within max_addr= and min_addr= command line arg */
1019 as = max(as, min_addr);
Tony Luckd8c97d52005-09-08 12:39:59 -07001020 ae = min(ae, max_addr);
1021 if (ae <= as)
1022 continue;
1023
1024 /* avoid going over mem= command line arg */
1025 if (total_mem + (ae - as) > mem_limit)
1026 ae -= total_mem + (ae - as) - mem_limit;
1027
1028 if (ae <= as)
1029 continue;
1030
1031 if (ae - as > space_needed)
1032 break;
1033 }
1034 if (p >= efi_map_end)
1035 panic("Can't allocate space for kernel memory descriptors");
1036
1037 return __va(as);
1038}
1039
1040/*
1041 * Walk the EFI memory map and gather all memory available for kernel
1042 * to use. We can allocate partial granules only if the unavailable
1043 * parts exist, and are WB.
1044 */
Bernhard Wallecb380852007-10-18 23:41:00 -07001045unsigned long
Matthew Wilcoxe088a4a2009-05-22 13:49:49 -07001046efi_memmap_init(u64 *s, u64 *e)
Tony Luckd8c97d52005-09-08 12:39:59 -07001047{
Keith Owense037cda2006-07-17 15:41:59 +10001048 struct kern_memdesc *k, *prev = NULL;
Tony Luckd8c97d52005-09-08 12:39:59 -07001049 u64 contig_low=0, contig_high=0;
1050 u64 as, ae, lim;
1051 void *efi_map_start, *efi_map_end, *p, *q;
1052 efi_memory_desc_t *md, *pmd = NULL, *check_md;
1053 u64 efi_desc_size;
1054 unsigned long total_mem = 0;
1055
1056 k = kern_memmap = find_memmap_space();
1057
1058 efi_map_start = __va(ia64_boot_param->efi_memmap);
1059 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
1060 efi_desc_size = ia64_boot_param->efi_memdesc_size;
1061
1062 for (p = efi_map_start; p < efi_map_end; pmd = md, p += efi_desc_size) {
1063 md = p;
1064 if (!efi_wb(md)) {
Aron Griffis7d9aed22008-02-04 15:31:49 -08001065 if (efi_uc(md) &&
1066 (md->type == EFI_CONVENTIONAL_MEMORY ||
1067 md->type == EFI_BOOT_SERVICES_DATA)) {
Tony Luckd8c97d52005-09-08 12:39:59 -07001068 k->attribute = EFI_MEMORY_UC;
1069 k->start = md->phys_addr;
1070 k->num_pages = md->num_pages;
1071 k++;
1072 }
1073 continue;
1074 }
Aron Griffis7d9aed22008-02-04 15:31:49 -08001075 if (pmd == NULL || !efi_wb(pmd) ||
1076 efi_md_end(pmd) != md->phys_addr) {
Tony Luckd8c97d52005-09-08 12:39:59 -07001077 contig_low = GRANULEROUNDUP(md->phys_addr);
1078 contig_high = efi_md_end(md);
Aron Griffis7d9aed22008-02-04 15:31:49 -08001079 for (q = p + efi_desc_size; q < efi_map_end;
1080 q += efi_desc_size) {
Tony Luckd8c97d52005-09-08 12:39:59 -07001081 check_md = q;
1082 if (!efi_wb(check_md))
1083 break;
1084 if (contig_high != check_md->phys_addr)
1085 break;
1086 contig_high = efi_md_end(check_md);
1087 }
1088 contig_high = GRANULEROUNDDOWN(contig_high);
1089 }
Christoph Lameter66888a62006-12-04 14:58:35 -08001090 if (!is_memory_available(md))
Tony Luckd8c97d52005-09-08 12:39:59 -07001091 continue;
1092
1093 /*
1094 * Round ends inward to granule boundaries
1095 * Give trimmings to uncached allocator
1096 */
1097 if (md->phys_addr < contig_low) {
1098 lim = min(efi_md_end(md), contig_low);
1099 if (efi_uc(md)) {
Aron Griffis7d9aed22008-02-04 15:31:49 -08001100 if (k > kern_memmap &&
1101 (k-1)->attribute == EFI_MEMORY_UC &&
Tony Luckd8c97d52005-09-08 12:39:59 -07001102 kmd_end(k-1) == md->phys_addr) {
Aron Griffis7d9aed22008-02-04 15:31:49 -08001103 (k-1)->num_pages +=
1104 (lim - md->phys_addr)
1105 >> EFI_PAGE_SHIFT;
Tony Luckd8c97d52005-09-08 12:39:59 -07001106 } else {
1107 k->attribute = EFI_MEMORY_UC;
1108 k->start = md->phys_addr;
Aron Griffis7d9aed22008-02-04 15:31:49 -08001109 k->num_pages = (lim - md->phys_addr)
1110 >> EFI_PAGE_SHIFT;
Tony Luckd8c97d52005-09-08 12:39:59 -07001111 k++;
1112 }
1113 }
1114 as = contig_low;
1115 } else
1116 as = md->phys_addr;
1117
1118 if (efi_md_end(md) > contig_high) {
1119 lim = max(md->phys_addr, contig_high);
1120 if (efi_uc(md)) {
1121 if (lim == md->phys_addr && k > kern_memmap &&
1122 (k-1)->attribute == EFI_MEMORY_UC &&
1123 kmd_end(k-1) == md->phys_addr) {
1124 (k-1)->num_pages += md->num_pages;
1125 } else {
1126 k->attribute = EFI_MEMORY_UC;
1127 k->start = lim;
Aron Griffis7d9aed22008-02-04 15:31:49 -08001128 k->num_pages = (efi_md_end(md) - lim)
1129 >> EFI_PAGE_SHIFT;
Tony Luckd8c97d52005-09-08 12:39:59 -07001130 k++;
1131 }
1132 }
1133 ae = contig_high;
1134 } else
1135 ae = efi_md_end(md);
1136
Zou Nan haia79561132006-12-07 09:51:35 -08001137 /* keep within max_addr= and min_addr= command line arg */
1138 as = max(as, min_addr);
Tony Luckd8c97d52005-09-08 12:39:59 -07001139 ae = min(ae, max_addr);
1140 if (ae <= as)
1141 continue;
1142
1143 /* avoid going over mem= command line arg */
1144 if (total_mem + (ae - as) > mem_limit)
1145 ae -= total_mem + (ae - as) - mem_limit;
1146
1147 if (ae <= as)
1148 continue;
1149 if (prev && kmd_end(prev) == md->phys_addr) {
1150 prev->num_pages += (ae - as) >> EFI_PAGE_SHIFT;
1151 total_mem += ae - as;
1152 continue;
1153 }
1154 k->attribute = EFI_MEMORY_WB;
1155 k->start = as;
1156 k->num_pages = (ae - as) >> EFI_PAGE_SHIFT;
1157 total_mem += ae - as;
1158 prev = k++;
1159 }
1160 k->start = ~0L; /* end-marker */
1161
1162 /* reserve the memory we are using for kern_memmap */
1163 *s = (u64)kern_memmap;
1164 *e = (u64)++k;
Bernhard Wallecb380852007-10-18 23:41:00 -07001165
1166 return total_mem;
Tony Luckd8c97d52005-09-08 12:39:59 -07001167}
Khalid Azizbe379122005-09-19 15:42:36 -07001168
1169void
1170efi_initialize_iomem_resources(struct resource *code_resource,
Bernhard Walle00bf4092007-10-21 16:42:01 -07001171 struct resource *data_resource,
1172 struct resource *bss_resource)
Khalid Azizbe379122005-09-19 15:42:36 -07001173{
1174 struct resource *res;
1175 void *efi_map_start, *efi_map_end, *p;
1176 efi_memory_desc_t *md;
1177 u64 efi_desc_size;
1178 char *name;
Toshi Kani03cb5252016-01-26 21:57:21 +01001179 unsigned long flags, desc;
Khalid Azizbe379122005-09-19 15:42:36 -07001180
1181 efi_map_start = __va(ia64_boot_param->efi_memmap);
1182 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
1183 efi_desc_size = ia64_boot_param->efi_memdesc_size;
1184
1185 res = NULL;
1186
1187 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
1188 md = p;
1189
1190 if (md->num_pages == 0) /* should not happen */
1191 continue;
1192
Yasunori Goto887c3cb2007-11-14 16:59:20 -08001193 flags = IORESOURCE_MEM | IORESOURCE_BUSY;
Toshi Kani03cb5252016-01-26 21:57:21 +01001194 desc = IORES_DESC_NONE;
1195
Khalid Azizbe379122005-09-19 15:42:36 -07001196 switch (md->type) {
1197
1198 case EFI_MEMORY_MAPPED_IO:
1199 case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
1200 continue;
1201
1202 case EFI_LOADER_CODE:
1203 case EFI_LOADER_DATA:
1204 case EFI_BOOT_SERVICES_DATA:
1205 case EFI_BOOT_SERVICES_CODE:
1206 case EFI_CONVENTIONAL_MEMORY:
1207 if (md->attribute & EFI_MEMORY_WP) {
1208 name = "System ROM";
1209 flags |= IORESOURCE_READONLY;
Toshi Kani03cb5252016-01-26 21:57:21 +01001210 } else if (md->attribute == EFI_MEMORY_UC) {
Jay Land3758f82008-09-22 14:21:19 -07001211 name = "Uncached RAM";
Toshi Kani03cb5252016-01-26 21:57:21 +01001212 } else {
Khalid Azizbe379122005-09-19 15:42:36 -07001213 name = "System RAM";
Toshi Kani03cb5252016-01-26 21:57:21 +01001214 flags |= IORESOURCE_SYSRAM;
1215 }
Khalid Azizbe379122005-09-19 15:42:36 -07001216 break;
1217
1218 case EFI_ACPI_MEMORY_NVS:
1219 name = "ACPI Non-volatile Storage";
Toshi Kani03cb5252016-01-26 21:57:21 +01001220 desc = IORES_DESC_ACPI_NV_STORAGE;
Khalid Azizbe379122005-09-19 15:42:36 -07001221 break;
1222
1223 case EFI_UNUSABLE_MEMORY:
1224 name = "reserved";
Yasunori Goto887c3cb2007-11-14 16:59:20 -08001225 flags |= IORESOURCE_DISABLED;
Khalid Azizbe379122005-09-19 15:42:36 -07001226 break;
1227
Dan Williamsad5fb872015-04-03 12:05:28 -04001228 case EFI_PERSISTENT_MEMORY:
1229 name = "Persistent Memory";
Toshi Kani03cb5252016-01-26 21:57:21 +01001230 desc = IORES_DESC_PERSISTENT_MEMORY;
Dan Williamsad5fb872015-04-03 12:05:28 -04001231 break;
1232
Khalid Azizbe379122005-09-19 15:42:36 -07001233 case EFI_RESERVED_TYPE:
1234 case EFI_RUNTIME_SERVICES_CODE:
1235 case EFI_RUNTIME_SERVICES_DATA:
1236 case EFI_ACPI_RECLAIM_MEMORY:
1237 default:
1238 name = "reserved";
Khalid Azizbe379122005-09-19 15:42:36 -07001239 break;
1240 }
1241
Aron Griffis7d9aed22008-02-04 15:31:49 -08001242 if ((res = kzalloc(sizeof(struct resource),
1243 GFP_KERNEL)) == NULL) {
1244 printk(KERN_ERR
Aron Griffis965e7c82008-01-08 22:29:37 -05001245 "failed to allocate resource for iomem\n");
Khalid Azizbe379122005-09-19 15:42:36 -07001246 return;
1247 }
1248
1249 res->name = name;
1250 res->start = md->phys_addr;
Li Zefan685c7f52007-11-21 14:58:26 -08001251 res->end = md->phys_addr + efi_md_size(md) - 1;
Khalid Azizbe379122005-09-19 15:42:36 -07001252 res->flags = flags;
Toshi Kani03cb5252016-01-26 21:57:21 +01001253 res->desc = desc;
Khalid Azizbe379122005-09-19 15:42:36 -07001254
1255 if (insert_resource(&iomem_resource, res) < 0)
1256 kfree(res);
1257 else {
1258 /*
1259 * We don't know which region contains
1260 * kernel data so we try it repeatedly and
1261 * let the resource manager test it.
1262 */
1263 insert_resource(res, code_resource);
1264 insert_resource(res, data_resource);
Bernhard Walle00bf4092007-10-21 16:42:01 -07001265 insert_resource(res, bss_resource);
Zou Nan haia79561132006-12-07 09:51:35 -08001266#ifdef CONFIG_KEXEC
1267 insert_resource(res, &efi_memmap_res);
1268 insert_resource(res, &boot_param_res);
1269 if (crashk_res.end > crashk_res.start)
1270 insert_resource(res, &crashk_res);
1271#endif
Khalid Azizbe379122005-09-19 15:42:36 -07001272 }
1273 }
1274}
Zou Nan haia79561132006-12-07 09:51:35 -08001275
1276#ifdef CONFIG_KEXEC
1277/* find a block of memory aligned to 64M exclude reserved regions
1278 rsvd_regions are sorted
1279 */
Horms2a3a2822007-03-06 16:28:50 +09001280unsigned long __init
Aron Griffis7d9aed22008-02-04 15:31:49 -08001281kdump_find_rsvd_region (unsigned long size, struct rsvd_region *r, int n)
Zou Nan haia79561132006-12-07 09:51:35 -08001282{
Aron Griffis7d9aed22008-02-04 15:31:49 -08001283 int i;
1284 u64 start, end;
1285 u64 alignment = 1UL << _PAGE_SIZE_64M;
1286 void *efi_map_start, *efi_map_end, *p;
1287 efi_memory_desc_t *md;
1288 u64 efi_desc_size;
Zou Nan haia79561132006-12-07 09:51:35 -08001289
Aron Griffis7d9aed22008-02-04 15:31:49 -08001290 efi_map_start = __va(ia64_boot_param->efi_memmap);
1291 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
1292 efi_desc_size = ia64_boot_param->efi_memdesc_size;
Zou Nan haia79561132006-12-07 09:51:35 -08001293
Aron Griffis7d9aed22008-02-04 15:31:49 -08001294 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
1295 md = p;
1296 if (!efi_wb(md))
1297 continue;
1298 start = ALIGN(md->phys_addr, alignment);
1299 end = efi_md_end(md);
1300 for (i = 0; i < n; i++) {
1301 if (__pa(r[i].start) >= start && __pa(r[i].end) < end) {
1302 if (__pa(r[i].start) > start + size)
1303 return start;
1304 start = ALIGN(__pa(r[i].end), alignment);
1305 if (i < n-1 &&
1306 __pa(r[i+1].start) < start + size)
1307 continue;
1308 else
1309 break;
1310 }
Zou Nan haia79561132006-12-07 09:51:35 -08001311 }
Aron Griffis7d9aed22008-02-04 15:31:49 -08001312 if (end > start + size)
1313 return start;
1314 }
Zou Nan haia79561132006-12-07 09:51:35 -08001315
Aron Griffis7d9aed22008-02-04 15:31:49 -08001316 printk(KERN_WARNING
1317 "Cannot reserve 0x%lx byte of memory for crashdump\n", size);
1318 return ~0UL;
Zou Nan haia79561132006-12-07 09:51:35 -08001319}
1320#endif
Magnus Dammcee87af2007-03-06 02:34:26 -08001321
Simon Hormand9a98552008-10-18 20:28:29 -07001322#ifdef CONFIG_CRASH_DUMP
Magnus Dammcee87af2007-03-06 02:34:26 -08001323/* locate the size find a the descriptor at a certain address */
Simon Horman1775fe852007-10-29 17:10:29 +09001324unsigned long __init
Magnus Dammcee87af2007-03-06 02:34:26 -08001325vmcore_find_descriptor_size (unsigned long address)
1326{
1327 void *efi_map_start, *efi_map_end, *p;
1328 efi_memory_desc_t *md;
1329 u64 efi_desc_size;
1330 unsigned long ret = 0;
1331
1332 efi_map_start = __va(ia64_boot_param->efi_memmap);
1333 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
1334 efi_desc_size = ia64_boot_param->efi_memdesc_size;
1335
1336 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
1337 md = p;
1338 if (efi_wb(md) && md->type == EFI_LOADER_DATA
1339 && md->phys_addr == address) {
1340 ret = efi_md_size(md);
1341 break;
1342 }
1343 }
1344
1345 if (ret == 0)
1346 printk(KERN_WARNING "Cannot locate EFI vmcore descriptor\n");
1347
1348 return ret;
1349}
1350#endif