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