efi.c 37 KB

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