efi.c 37 KB

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