efi.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366
  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. ia64_set_psr(psr); /* restore psr */
  407. }
  408. void __init
  409. efi_init (void)
  410. {
  411. void *efi_map_start, *efi_map_end;
  412. efi_config_table_t *config_tables;
  413. efi_char16_t *c16;
  414. u64 efi_desc_size;
  415. char *cp, vendor[100] = "unknown";
  416. int i;
  417. unsigned long palo_phys;
  418. /*
  419. * It's too early to be able to use the standard kernel command line
  420. * support...
  421. */
  422. for (cp = boot_command_line; *cp; ) {
  423. if (memcmp(cp, "mem=", 4) == 0) {
  424. mem_limit = memparse(cp + 4, &cp);
  425. } else if (memcmp(cp, "max_addr=", 9) == 0) {
  426. max_addr = GRANULEROUNDDOWN(memparse(cp + 9, &cp));
  427. } else if (memcmp(cp, "min_addr=", 9) == 0) {
  428. min_addr = GRANULEROUNDDOWN(memparse(cp + 9, &cp));
  429. } else {
  430. while (*cp != ' ' && *cp)
  431. ++cp;
  432. while (*cp == ' ')
  433. ++cp;
  434. }
  435. }
  436. if (min_addr != 0UL)
  437. printk(KERN_INFO "Ignoring memory below %luMB\n",
  438. min_addr >> 20);
  439. if (max_addr != ~0UL)
  440. printk(KERN_INFO "Ignoring memory above %luMB\n",
  441. max_addr >> 20);
  442. efi.systab = __va(ia64_boot_param->efi_systab);
  443. /*
  444. * Verify the EFI Table
  445. */
  446. if (efi.systab == NULL)
  447. panic("Whoa! Can't find EFI system table.\n");
  448. if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
  449. panic("Whoa! EFI system table signature incorrect\n");
  450. if ((efi.systab->hdr.revision >> 16) == 0)
  451. printk(KERN_WARNING "Warning: EFI system table version "
  452. "%d.%02d, expected 1.00 or greater\n",
  453. efi.systab->hdr.revision >> 16,
  454. efi.systab->hdr.revision & 0xffff);
  455. config_tables = __va(efi.systab->tables);
  456. /* Show what we know for posterity */
  457. c16 = __va(efi.systab->fw_vendor);
  458. if (c16) {
  459. for (i = 0;i < (int) sizeof(vendor) - 1 && *c16; ++i)
  460. vendor[i] = *c16++;
  461. vendor[i] = '\0';
  462. }
  463. printk(KERN_INFO "EFI v%u.%.02u by %s:",
  464. efi.systab->hdr.revision >> 16,
  465. efi.systab->hdr.revision & 0xffff, vendor);
  466. efi.mps = EFI_INVALID_TABLE_ADDR;
  467. efi.acpi = EFI_INVALID_TABLE_ADDR;
  468. efi.acpi20 = EFI_INVALID_TABLE_ADDR;
  469. efi.smbios = EFI_INVALID_TABLE_ADDR;
  470. efi.sal_systab = EFI_INVALID_TABLE_ADDR;
  471. efi.boot_info = EFI_INVALID_TABLE_ADDR;
  472. efi.hcdp = EFI_INVALID_TABLE_ADDR;
  473. efi.uga = EFI_INVALID_TABLE_ADDR;
  474. palo_phys = EFI_INVALID_TABLE_ADDR;
  475. for (i = 0; i < (int) efi.systab->nr_tables; i++) {
  476. if (efi_guidcmp(config_tables[i].guid, MPS_TABLE_GUID) == 0) {
  477. efi.mps = config_tables[i].table;
  478. printk(" MPS=0x%lx", config_tables[i].table);
  479. } else if (efi_guidcmp(config_tables[i].guid, ACPI_20_TABLE_GUID) == 0) {
  480. efi.acpi20 = config_tables[i].table;
  481. printk(" ACPI 2.0=0x%lx", config_tables[i].table);
  482. } else if (efi_guidcmp(config_tables[i].guid, ACPI_TABLE_GUID) == 0) {
  483. efi.acpi = config_tables[i].table;
  484. printk(" ACPI=0x%lx", config_tables[i].table);
  485. } else if (efi_guidcmp(config_tables[i].guid, SMBIOS_TABLE_GUID) == 0) {
  486. efi.smbios = config_tables[i].table;
  487. printk(" SMBIOS=0x%lx", config_tables[i].table);
  488. } else if (efi_guidcmp(config_tables[i].guid, SAL_SYSTEM_TABLE_GUID) == 0) {
  489. efi.sal_systab = config_tables[i].table;
  490. printk(" SALsystab=0x%lx", config_tables[i].table);
  491. } else if (efi_guidcmp(config_tables[i].guid, HCDP_TABLE_GUID) == 0) {
  492. efi.hcdp = config_tables[i].table;
  493. printk(" HCDP=0x%lx", config_tables[i].table);
  494. } else if (efi_guidcmp(config_tables[i].guid,
  495. PROCESSOR_ABSTRACTION_LAYER_OVERWRITE_GUID) == 0) {
  496. palo_phys = config_tables[i].table;
  497. printk(" PALO=0x%lx", config_tables[i].table);
  498. }
  499. }
  500. printk("\n");
  501. if (palo_phys != EFI_INVALID_TABLE_ADDR)
  502. handle_palo(palo_phys);
  503. runtime = __va(efi.systab->runtime);
  504. efi.get_time = phys_get_time;
  505. efi.set_time = phys_set_time;
  506. efi.get_wakeup_time = phys_get_wakeup_time;
  507. efi.set_wakeup_time = phys_set_wakeup_time;
  508. efi.get_variable = phys_get_variable;
  509. efi.get_next_variable = phys_get_next_variable;
  510. efi.set_variable = phys_set_variable;
  511. efi.get_next_high_mono_count = phys_get_next_high_mono_count;
  512. efi.reset_system = phys_reset_system;
  513. efi_map_start = __va(ia64_boot_param->efi_memmap);
  514. efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
  515. efi_desc_size = ia64_boot_param->efi_memdesc_size;
  516. #if EFI_DEBUG
  517. /* print EFI memory map: */
  518. {
  519. efi_memory_desc_t *md;
  520. void *p;
  521. for (i = 0, p = efi_map_start; p < efi_map_end;
  522. ++i, p += efi_desc_size)
  523. {
  524. const char *unit;
  525. unsigned long size;
  526. md = p;
  527. size = md->num_pages << EFI_PAGE_SHIFT;
  528. if ((size >> 40) > 0) {
  529. size >>= 40;
  530. unit = "TB";
  531. } else if ((size >> 30) > 0) {
  532. size >>= 30;
  533. unit = "GB";
  534. } else if ((size >> 20) > 0) {
  535. size >>= 20;
  536. unit = "MB";
  537. } else {
  538. size >>= 10;
  539. unit = "KB";
  540. }
  541. printk("mem%02d: type=%2u, attr=0x%016lx, "
  542. "range=[0x%016lx-0x%016lx) (%4lu%s)\n",
  543. i, md->type, md->attribute, md->phys_addr,
  544. md->phys_addr + efi_md_size(md), size, unit);
  545. }
  546. }
  547. #endif
  548. efi_map_pal_code();
  549. efi_enter_virtual_mode();
  550. }
  551. void
  552. efi_enter_virtual_mode (void)
  553. {
  554. void *efi_map_start, *efi_map_end, *p;
  555. efi_memory_desc_t *md;
  556. efi_status_t status;
  557. u64 efi_desc_size;
  558. efi_map_start = __va(ia64_boot_param->efi_memmap);
  559. efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
  560. efi_desc_size = ia64_boot_param->efi_memdesc_size;
  561. for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
  562. md = p;
  563. if (md->attribute & EFI_MEMORY_RUNTIME) {
  564. /*
  565. * Some descriptors have multiple bits set, so the
  566. * order of the tests is relevant.
  567. */
  568. if (md->attribute & EFI_MEMORY_WB) {
  569. md->virt_addr = (u64) __va(md->phys_addr);
  570. } else if (md->attribute & EFI_MEMORY_UC) {
  571. md->virt_addr = (u64) ioremap(md->phys_addr, 0);
  572. } else if (md->attribute & EFI_MEMORY_WC) {
  573. #if 0
  574. md->virt_addr = ia64_remap(md->phys_addr,
  575. (_PAGE_A |
  576. _PAGE_P |
  577. _PAGE_D |
  578. _PAGE_MA_WC |
  579. _PAGE_PL_0 |
  580. _PAGE_AR_RW));
  581. #else
  582. printk(KERN_INFO "EFI_MEMORY_WC mapping\n");
  583. md->virt_addr = (u64) ioremap(md->phys_addr, 0);
  584. #endif
  585. } else if (md->attribute & EFI_MEMORY_WT) {
  586. #if 0
  587. md->virt_addr = ia64_remap(md->phys_addr,
  588. (_PAGE_A |
  589. _PAGE_P |
  590. _PAGE_D |
  591. _PAGE_MA_WT |
  592. _PAGE_PL_0 |
  593. _PAGE_AR_RW));
  594. #else
  595. printk(KERN_INFO "EFI_MEMORY_WT mapping\n");
  596. md->virt_addr = (u64) ioremap(md->phys_addr, 0);
  597. #endif
  598. }
  599. }
  600. }
  601. status = efi_call_phys(__va(runtime->set_virtual_address_map),
  602. ia64_boot_param->efi_memmap_size,
  603. efi_desc_size,
  604. ia64_boot_param->efi_memdesc_version,
  605. ia64_boot_param->efi_memmap);
  606. if (status != EFI_SUCCESS) {
  607. printk(KERN_WARNING "warning: unable to switch EFI into "
  608. "virtual mode (status=%lu)\n", status);
  609. return;
  610. }
  611. /*
  612. * Now that EFI is in virtual mode, we call the EFI functions more
  613. * efficiently:
  614. */
  615. efi.get_time = virt_get_time;
  616. efi.set_time = virt_set_time;
  617. efi.get_wakeup_time = virt_get_wakeup_time;
  618. efi.set_wakeup_time = virt_set_wakeup_time;
  619. efi.get_variable = virt_get_variable;
  620. efi.get_next_variable = virt_get_next_variable;
  621. efi.set_variable = virt_set_variable;
  622. efi.get_next_high_mono_count = virt_get_next_high_mono_count;
  623. efi.reset_system = virt_reset_system;
  624. }
  625. /*
  626. * Walk the EFI memory map looking for the I/O port range. There can only be
  627. * one entry of this type, other I/O port ranges should be described via ACPI.
  628. */
  629. u64
  630. efi_get_iobase (void)
  631. {
  632. void *efi_map_start, *efi_map_end, *p;
  633. efi_memory_desc_t *md;
  634. u64 efi_desc_size;
  635. efi_map_start = __va(ia64_boot_param->efi_memmap);
  636. efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
  637. efi_desc_size = ia64_boot_param->efi_memdesc_size;
  638. for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
  639. md = p;
  640. if (md->type == EFI_MEMORY_MAPPED_IO_PORT_SPACE) {
  641. if (md->attribute & EFI_MEMORY_UC)
  642. return md->phys_addr;
  643. }
  644. }
  645. return 0;
  646. }
  647. static struct kern_memdesc *
  648. kern_memory_descriptor (unsigned long phys_addr)
  649. {
  650. struct kern_memdesc *md;
  651. for (md = kern_memmap; md->start != ~0UL; md++) {
  652. if (phys_addr - md->start < (md->num_pages << EFI_PAGE_SHIFT))
  653. return md;
  654. }
  655. return NULL;
  656. }
  657. static efi_memory_desc_t *
  658. efi_memory_descriptor (unsigned long phys_addr)
  659. {
  660. void *efi_map_start, *efi_map_end, *p;
  661. efi_memory_desc_t *md;
  662. u64 efi_desc_size;
  663. efi_map_start = __va(ia64_boot_param->efi_memmap);
  664. efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
  665. efi_desc_size = ia64_boot_param->efi_memdesc_size;
  666. for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
  667. md = p;
  668. if (phys_addr - md->phys_addr < efi_md_size(md))
  669. return md;
  670. }
  671. return NULL;
  672. }
  673. static int
  674. efi_memmap_intersects (unsigned long phys_addr, unsigned long size)
  675. {
  676. void *efi_map_start, *efi_map_end, *p;
  677. efi_memory_desc_t *md;
  678. u64 efi_desc_size;
  679. unsigned long end;
  680. efi_map_start = __va(ia64_boot_param->efi_memmap);
  681. efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
  682. efi_desc_size = ia64_boot_param->efi_memdesc_size;
  683. end = phys_addr + size;
  684. for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
  685. md = p;
  686. if (md->phys_addr < end && efi_md_end(md) > phys_addr)
  687. return 1;
  688. }
  689. return 0;
  690. }
  691. u32
  692. efi_mem_type (unsigned long phys_addr)
  693. {
  694. efi_memory_desc_t *md = efi_memory_descriptor(phys_addr);
  695. if (md)
  696. return md->type;
  697. return 0;
  698. }
  699. u64
  700. efi_mem_attributes (unsigned long phys_addr)
  701. {
  702. efi_memory_desc_t *md = efi_memory_descriptor(phys_addr);
  703. if (md)
  704. return md->attribute;
  705. return 0;
  706. }
  707. EXPORT_SYMBOL(efi_mem_attributes);
  708. u64
  709. efi_mem_attribute (unsigned long phys_addr, unsigned long size)
  710. {
  711. unsigned long end = phys_addr + size;
  712. efi_memory_desc_t *md = efi_memory_descriptor(phys_addr);
  713. u64 attr;
  714. if (!md)
  715. return 0;
  716. /*
  717. * EFI_MEMORY_RUNTIME is not a memory attribute; it just tells
  718. * the kernel that firmware needs this region mapped.
  719. */
  720. attr = md->attribute & ~EFI_MEMORY_RUNTIME;
  721. do {
  722. unsigned long md_end = efi_md_end(md);
  723. if (end <= md_end)
  724. return attr;
  725. md = efi_memory_descriptor(md_end);
  726. if (!md || (md->attribute & ~EFI_MEMORY_RUNTIME) != attr)
  727. return 0;
  728. } while (md);
  729. return 0; /* never reached */
  730. }
  731. u64
  732. kern_mem_attribute (unsigned long phys_addr, unsigned long size)
  733. {
  734. unsigned long end = phys_addr + size;
  735. struct kern_memdesc *md;
  736. u64 attr;
  737. /*
  738. * This is a hack for ioremap calls before we set up kern_memmap.
  739. * Maybe we should do efi_memmap_init() earlier instead.
  740. */
  741. if (!kern_memmap) {
  742. attr = efi_mem_attribute(phys_addr, size);
  743. if (attr & EFI_MEMORY_WB)
  744. return EFI_MEMORY_WB;
  745. return 0;
  746. }
  747. md = kern_memory_descriptor(phys_addr);
  748. if (!md)
  749. return 0;
  750. attr = md->attribute;
  751. do {
  752. unsigned long md_end = kmd_end(md);
  753. if (end <= md_end)
  754. return attr;
  755. md = kern_memory_descriptor(md_end);
  756. if (!md || md->attribute != attr)
  757. return 0;
  758. } while (md);
  759. return 0; /* never reached */
  760. }
  761. EXPORT_SYMBOL(kern_mem_attribute);
  762. int
  763. valid_phys_addr_range (unsigned long phys_addr, unsigned long size)
  764. {
  765. u64 attr;
  766. /*
  767. * /dev/mem reads and writes use copy_to_user(), which implicitly
  768. * uses a granule-sized kernel identity mapping. It's really
  769. * only safe to do this for regions in kern_memmap. For more
  770. * details, see Documentation/ia64/aliasing.txt.
  771. */
  772. attr = kern_mem_attribute(phys_addr, size);
  773. if (attr & EFI_MEMORY_WB || attr & EFI_MEMORY_UC)
  774. return 1;
  775. return 0;
  776. }
  777. int
  778. valid_mmap_phys_addr_range (unsigned long pfn, unsigned long size)
  779. {
  780. unsigned long phys_addr = pfn << PAGE_SHIFT;
  781. u64 attr;
  782. attr = efi_mem_attribute(phys_addr, size);
  783. /*
  784. * /dev/mem mmap uses normal user pages, so we don't need the entire
  785. * granule, but the entire region we're mapping must support the same
  786. * attribute.
  787. */
  788. if (attr & EFI_MEMORY_WB || attr & EFI_MEMORY_UC)
  789. return 1;
  790. /*
  791. * Intel firmware doesn't tell us about all the MMIO regions, so
  792. * in general we have to allow mmap requests. But if EFI *does*
  793. * tell us about anything inside this region, we should deny it.
  794. * The user can always map a smaller region to avoid the overlap.
  795. */
  796. if (efi_memmap_intersects(phys_addr, size))
  797. return 0;
  798. return 1;
  799. }
  800. pgprot_t
  801. phys_mem_access_prot(struct file *file, unsigned long pfn, unsigned long size,
  802. pgprot_t vma_prot)
  803. {
  804. unsigned long phys_addr = pfn << PAGE_SHIFT;
  805. u64 attr;
  806. /*
  807. * For /dev/mem mmap, we use user mappings, but if the region is
  808. * in kern_memmap (and hence may be covered by a kernel mapping),
  809. * we must use the same attribute as the kernel mapping.
  810. */
  811. attr = kern_mem_attribute(phys_addr, size);
  812. if (attr & EFI_MEMORY_WB)
  813. return pgprot_cacheable(vma_prot);
  814. else if (attr & EFI_MEMORY_UC)
  815. return pgprot_noncached(vma_prot);
  816. /*
  817. * Some chipsets don't support UC access to memory. If
  818. * WB is supported, we prefer that.
  819. */
  820. if (efi_mem_attribute(phys_addr, size) & EFI_MEMORY_WB)
  821. return pgprot_cacheable(vma_prot);
  822. return pgprot_noncached(vma_prot);
  823. }
  824. int __init
  825. efi_uart_console_only(void)
  826. {
  827. efi_status_t status;
  828. char *s, name[] = "ConOut";
  829. efi_guid_t guid = EFI_GLOBAL_VARIABLE_GUID;
  830. efi_char16_t *utf16, name_utf16[32];
  831. unsigned char data[1024];
  832. unsigned long size = sizeof(data);
  833. struct efi_generic_dev_path *hdr, *end_addr;
  834. int uart = 0;
  835. /* Convert to UTF-16 */
  836. utf16 = name_utf16;
  837. s = name;
  838. while (*s)
  839. *utf16++ = *s++ & 0x7f;
  840. *utf16 = 0;
  841. status = efi.get_variable(name_utf16, &guid, NULL, &size, data);
  842. if (status != EFI_SUCCESS) {
  843. printk(KERN_ERR "No EFI %s variable?\n", name);
  844. return 0;
  845. }
  846. hdr = (struct efi_generic_dev_path *) data;
  847. end_addr = (struct efi_generic_dev_path *) ((u8 *) data + size);
  848. while (hdr < end_addr) {
  849. if (hdr->type == EFI_DEV_MSG &&
  850. hdr->sub_type == EFI_DEV_MSG_UART)
  851. uart = 1;
  852. else if (hdr->type == EFI_DEV_END_PATH ||
  853. hdr->type == EFI_DEV_END_PATH2) {
  854. if (!uart)
  855. return 0;
  856. if (hdr->sub_type == EFI_DEV_END_ENTIRE)
  857. return 1;
  858. uart = 0;
  859. }
  860. hdr = (struct efi_generic_dev_path *)((u8 *) hdr + hdr->length);
  861. }
  862. printk(KERN_ERR "Malformed %s value\n", name);
  863. return 0;
  864. }
  865. /*
  866. * Look for the first granule aligned memory descriptor memory
  867. * that is big enough to hold EFI memory map. Make sure this
  868. * descriptor is atleast granule sized so it does not get trimmed
  869. */
  870. struct kern_memdesc *
  871. find_memmap_space (void)
  872. {
  873. u64 contig_low=0, contig_high=0;
  874. u64 as = 0, ae;
  875. void *efi_map_start, *efi_map_end, *p, *q;
  876. efi_memory_desc_t *md, *pmd = NULL, *check_md;
  877. u64 space_needed, efi_desc_size;
  878. unsigned long total_mem = 0;
  879. efi_map_start = __va(ia64_boot_param->efi_memmap);
  880. efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
  881. efi_desc_size = ia64_boot_param->efi_memdesc_size;
  882. /*
  883. * Worst case: we need 3 kernel descriptors for each efi descriptor
  884. * (if every entry has a WB part in the middle, and UC head and tail),
  885. * plus one for the end marker.
  886. */
  887. space_needed = sizeof(kern_memdesc_t) *
  888. (3 * (ia64_boot_param->efi_memmap_size/efi_desc_size) + 1);
  889. for (p = efi_map_start; p < efi_map_end; pmd = md, p += efi_desc_size) {
  890. md = p;
  891. if (!efi_wb(md)) {
  892. continue;
  893. }
  894. if (pmd == NULL || !efi_wb(pmd) ||
  895. efi_md_end(pmd) != md->phys_addr) {
  896. contig_low = GRANULEROUNDUP(md->phys_addr);
  897. contig_high = efi_md_end(md);
  898. for (q = p + efi_desc_size; q < efi_map_end;
  899. q += efi_desc_size) {
  900. check_md = q;
  901. if (!efi_wb(check_md))
  902. break;
  903. if (contig_high != check_md->phys_addr)
  904. break;
  905. contig_high = efi_md_end(check_md);
  906. }
  907. contig_high = GRANULEROUNDDOWN(contig_high);
  908. }
  909. if (!is_memory_available(md) || md->type == EFI_LOADER_DATA)
  910. continue;
  911. /* Round ends inward to granule boundaries */
  912. as = max(contig_low, md->phys_addr);
  913. ae = min(contig_high, efi_md_end(md));
  914. /* keep within max_addr= and min_addr= command line arg */
  915. as = max(as, min_addr);
  916. ae = min(ae, max_addr);
  917. if (ae <= as)
  918. continue;
  919. /* avoid going over mem= command line arg */
  920. if (total_mem + (ae - as) > mem_limit)
  921. ae -= total_mem + (ae - as) - mem_limit;
  922. if (ae <= as)
  923. continue;
  924. if (ae - as > space_needed)
  925. break;
  926. }
  927. if (p >= efi_map_end)
  928. panic("Can't allocate space for kernel memory descriptors");
  929. return __va(as);
  930. }
  931. /*
  932. * Walk the EFI memory map and gather all memory available for kernel
  933. * to use. We can allocate partial granules only if the unavailable
  934. * parts exist, and are WB.
  935. */
  936. unsigned long
  937. efi_memmap_init(unsigned long *s, unsigned long *e)
  938. {
  939. struct kern_memdesc *k, *prev = NULL;
  940. u64 contig_low=0, contig_high=0;
  941. u64 as, ae, lim;
  942. void *efi_map_start, *efi_map_end, *p, *q;
  943. efi_memory_desc_t *md, *pmd = NULL, *check_md;
  944. u64 efi_desc_size;
  945. unsigned long total_mem = 0;
  946. k = kern_memmap = find_memmap_space();
  947. efi_map_start = __va(ia64_boot_param->efi_memmap);
  948. efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
  949. efi_desc_size = ia64_boot_param->efi_memdesc_size;
  950. for (p = efi_map_start; p < efi_map_end; pmd = md, p += efi_desc_size) {
  951. md = p;
  952. if (!efi_wb(md)) {
  953. if (efi_uc(md) &&
  954. (md->type == EFI_CONVENTIONAL_MEMORY ||
  955. md->type == EFI_BOOT_SERVICES_DATA)) {
  956. k->attribute = EFI_MEMORY_UC;
  957. k->start = md->phys_addr;
  958. k->num_pages = md->num_pages;
  959. k++;
  960. }
  961. continue;
  962. }
  963. if (pmd == NULL || !efi_wb(pmd) ||
  964. efi_md_end(pmd) != md->phys_addr) {
  965. contig_low = GRANULEROUNDUP(md->phys_addr);
  966. contig_high = efi_md_end(md);
  967. for (q = p + efi_desc_size; q < efi_map_end;
  968. q += efi_desc_size) {
  969. check_md = q;
  970. if (!efi_wb(check_md))
  971. break;
  972. if (contig_high != check_md->phys_addr)
  973. break;
  974. contig_high = efi_md_end(check_md);
  975. }
  976. contig_high = GRANULEROUNDDOWN(contig_high);
  977. }
  978. if (!is_memory_available(md))
  979. continue;
  980. #ifdef CONFIG_CRASH_DUMP
  981. /* saved_max_pfn should ignore max_addr= command line arg */
  982. if (saved_max_pfn < (efi_md_end(md) >> PAGE_SHIFT))
  983. saved_max_pfn = (efi_md_end(md) >> PAGE_SHIFT);
  984. #endif
  985. /*
  986. * Round ends inward to granule boundaries
  987. * Give trimmings to uncached allocator
  988. */
  989. if (md->phys_addr < contig_low) {
  990. lim = min(efi_md_end(md), contig_low);
  991. if (efi_uc(md)) {
  992. if (k > kern_memmap &&
  993. (k-1)->attribute == EFI_MEMORY_UC &&
  994. kmd_end(k-1) == md->phys_addr) {
  995. (k-1)->num_pages +=
  996. (lim - md->phys_addr)
  997. >> EFI_PAGE_SHIFT;
  998. } else {
  999. k->attribute = EFI_MEMORY_UC;
  1000. k->start = md->phys_addr;
  1001. k->num_pages = (lim - md->phys_addr)
  1002. >> EFI_PAGE_SHIFT;
  1003. k++;
  1004. }
  1005. }
  1006. as = contig_low;
  1007. } else
  1008. as = md->phys_addr;
  1009. if (efi_md_end(md) > contig_high) {
  1010. lim = max(md->phys_addr, contig_high);
  1011. if (efi_uc(md)) {
  1012. if (lim == md->phys_addr && k > kern_memmap &&
  1013. (k-1)->attribute == EFI_MEMORY_UC &&
  1014. kmd_end(k-1) == md->phys_addr) {
  1015. (k-1)->num_pages += md->num_pages;
  1016. } else {
  1017. k->attribute = EFI_MEMORY_UC;
  1018. k->start = lim;
  1019. k->num_pages = (efi_md_end(md) - lim)
  1020. >> EFI_PAGE_SHIFT;
  1021. k++;
  1022. }
  1023. }
  1024. ae = contig_high;
  1025. } else
  1026. ae = efi_md_end(md);
  1027. /* keep within max_addr= and min_addr= command line arg */
  1028. as = max(as, min_addr);
  1029. ae = min(ae, max_addr);
  1030. if (ae <= as)
  1031. continue;
  1032. /* avoid going over mem= command line arg */
  1033. if (total_mem + (ae - as) > mem_limit)
  1034. ae -= total_mem + (ae - as) - mem_limit;
  1035. if (ae <= as)
  1036. continue;
  1037. if (prev && kmd_end(prev) == md->phys_addr) {
  1038. prev->num_pages += (ae - as) >> EFI_PAGE_SHIFT;
  1039. total_mem += ae - as;
  1040. continue;
  1041. }
  1042. k->attribute = EFI_MEMORY_WB;
  1043. k->start = as;
  1044. k->num_pages = (ae - as) >> EFI_PAGE_SHIFT;
  1045. total_mem += ae - as;
  1046. prev = k++;
  1047. }
  1048. k->start = ~0L; /* end-marker */
  1049. /* reserve the memory we are using for kern_memmap */
  1050. *s = (u64)kern_memmap;
  1051. *e = (u64)++k;
  1052. return total_mem;
  1053. }
  1054. void
  1055. efi_initialize_iomem_resources(struct resource *code_resource,
  1056. struct resource *data_resource,
  1057. struct resource *bss_resource)
  1058. {
  1059. struct resource *res;
  1060. void *efi_map_start, *efi_map_end, *p;
  1061. efi_memory_desc_t *md;
  1062. u64 efi_desc_size;
  1063. char *name;
  1064. unsigned long flags;
  1065. efi_map_start = __va(ia64_boot_param->efi_memmap);
  1066. efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
  1067. efi_desc_size = ia64_boot_param->efi_memdesc_size;
  1068. res = NULL;
  1069. for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
  1070. md = p;
  1071. if (md->num_pages == 0) /* should not happen */
  1072. continue;
  1073. flags = IORESOURCE_MEM | IORESOURCE_BUSY;
  1074. switch (md->type) {
  1075. case EFI_MEMORY_MAPPED_IO:
  1076. case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
  1077. continue;
  1078. case EFI_LOADER_CODE:
  1079. case EFI_LOADER_DATA:
  1080. case EFI_BOOT_SERVICES_DATA:
  1081. case EFI_BOOT_SERVICES_CODE:
  1082. case EFI_CONVENTIONAL_MEMORY:
  1083. if (md->attribute & EFI_MEMORY_WP) {
  1084. name = "System ROM";
  1085. flags |= IORESOURCE_READONLY;
  1086. } else if (md->attribute == EFI_MEMORY_UC)
  1087. name = "Uncached RAM";
  1088. else
  1089. name = "System RAM";
  1090. break;
  1091. case EFI_ACPI_MEMORY_NVS:
  1092. name = "ACPI Non-volatile Storage";
  1093. break;
  1094. case EFI_UNUSABLE_MEMORY:
  1095. name = "reserved";
  1096. flags |= IORESOURCE_DISABLED;
  1097. break;
  1098. case EFI_RESERVED_TYPE:
  1099. case EFI_RUNTIME_SERVICES_CODE:
  1100. case EFI_RUNTIME_SERVICES_DATA:
  1101. case EFI_ACPI_RECLAIM_MEMORY:
  1102. default:
  1103. name = "reserved";
  1104. break;
  1105. }
  1106. if ((res = kzalloc(sizeof(struct resource),
  1107. GFP_KERNEL)) == NULL) {
  1108. printk(KERN_ERR
  1109. "failed to allocate resource for iomem\n");
  1110. return;
  1111. }
  1112. res->name = name;
  1113. res->start = md->phys_addr;
  1114. res->end = md->phys_addr + efi_md_size(md) - 1;
  1115. res->flags = flags;
  1116. if (insert_resource(&iomem_resource, res) < 0)
  1117. kfree(res);
  1118. else {
  1119. /*
  1120. * We don't know which region contains
  1121. * kernel data so we try it repeatedly and
  1122. * let the resource manager test it.
  1123. */
  1124. insert_resource(res, code_resource);
  1125. insert_resource(res, data_resource);
  1126. insert_resource(res, bss_resource);
  1127. #ifdef CONFIG_KEXEC
  1128. insert_resource(res, &efi_memmap_res);
  1129. insert_resource(res, &boot_param_res);
  1130. if (crashk_res.end > crashk_res.start)
  1131. insert_resource(res, &crashk_res);
  1132. #endif
  1133. }
  1134. }
  1135. }
  1136. #ifdef CONFIG_KEXEC
  1137. /* find a block of memory aligned to 64M exclude reserved regions
  1138. rsvd_regions are sorted
  1139. */
  1140. unsigned long __init
  1141. kdump_find_rsvd_region (unsigned long size, struct rsvd_region *r, int n)
  1142. {
  1143. int i;
  1144. u64 start, end;
  1145. u64 alignment = 1UL << _PAGE_SIZE_64M;
  1146. void *efi_map_start, *efi_map_end, *p;
  1147. efi_memory_desc_t *md;
  1148. u64 efi_desc_size;
  1149. efi_map_start = __va(ia64_boot_param->efi_memmap);
  1150. efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
  1151. efi_desc_size = ia64_boot_param->efi_memdesc_size;
  1152. for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
  1153. md = p;
  1154. if (!efi_wb(md))
  1155. continue;
  1156. start = ALIGN(md->phys_addr, alignment);
  1157. end = efi_md_end(md);
  1158. for (i = 0; i < n; i++) {
  1159. if (__pa(r[i].start) >= start && __pa(r[i].end) < end) {
  1160. if (__pa(r[i].start) > start + size)
  1161. return start;
  1162. start = ALIGN(__pa(r[i].end), alignment);
  1163. if (i < n-1 &&
  1164. __pa(r[i+1].start) < start + size)
  1165. continue;
  1166. else
  1167. break;
  1168. }
  1169. }
  1170. if (end > start + size)
  1171. return start;
  1172. }
  1173. printk(KERN_WARNING
  1174. "Cannot reserve 0x%lx byte of memory for crashdump\n", size);
  1175. return ~0UL;
  1176. }
  1177. #endif
  1178. #ifdef CONFIG_PROC_VMCORE
  1179. /* locate the size find a the descriptor at a certain address */
  1180. unsigned long __init
  1181. vmcore_find_descriptor_size (unsigned long address)
  1182. {
  1183. void *efi_map_start, *efi_map_end, *p;
  1184. efi_memory_desc_t *md;
  1185. u64 efi_desc_size;
  1186. unsigned long ret = 0;
  1187. efi_map_start = __va(ia64_boot_param->efi_memmap);
  1188. efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
  1189. efi_desc_size = ia64_boot_param->efi_memdesc_size;
  1190. for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
  1191. md = p;
  1192. if (efi_wb(md) && md->type == EFI_LOADER_DATA
  1193. && md->phys_addr == address) {
  1194. ret = efi_md_size(md);
  1195. break;
  1196. }
  1197. }
  1198. if (ret == 0)
  1199. printk(KERN_WARNING "Cannot locate EFI vmcore descriptor\n");
  1200. return ret;
  1201. }
  1202. #endif