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