efi.c 35 KB

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