efi.c 37 KB

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