efi.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074
  1. /*
  2. * Extensible Firmware Interface
  3. *
  4. * Based on Extensible Firmware Interface Specification version 0.9 April 30, 1999
  5. *
  6. * Copyright (C) 1999 VA Linux Systems
  7. * Copyright (C) 1999 Walt Drummond <drummond@valinux.com>
  8. * Copyright (C) 1999-2003 Hewlett-Packard Co.
  9. * David Mosberger-Tang <davidm@hpl.hp.com>
  10. * Stephane Eranian <eranian@hpl.hp.com>
  11. *
  12. * All EFI Runtime Services are not implemented yet as EFI only
  13. * supports physical mode addressing on SoftSDV. This is to be fixed
  14. * in a future version. --drummond 1999-07-20
  15. *
  16. * Implemented EFI runtime services and virtual mode calls. --davidm
  17. *
  18. * Goutham Rao: <goutham.rao@intel.com>
  19. * Skip non-WB memory and ignore empty memory ranges.
  20. */
  21. #include <linux/config.h>
  22. #include <linux/module.h>
  23. #include <linux/kernel.h>
  24. #include <linux/init.h>
  25. #include <linux/types.h>
  26. #include <linux/time.h>
  27. #include <linux/efi.h>
  28. #include <asm/io.h>
  29. #include <asm/kregs.h>
  30. #include <asm/meminit.h>
  31. #include <asm/pgtable.h>
  32. #include <asm/processor.h>
  33. #include <asm/mca.h>
  34. #define EFI_DEBUG 0
  35. extern efi_status_t efi_call_phys (void *, ...);
  36. struct efi efi;
  37. EXPORT_SYMBOL(efi);
  38. static efi_runtime_services_t *runtime;
  39. static unsigned long mem_limit = ~0UL, max_addr = ~0UL;
  40. #define efi_call_virt(f, args...) (*(f))(args)
  41. #define STUB_GET_TIME(prefix, adjust_arg) \
  42. static efi_status_t \
  43. prefix##_get_time (efi_time_t *tm, efi_time_cap_t *tc) \
  44. { \
  45. struct ia64_fpreg fr[6]; \
  46. efi_time_cap_t *atc = NULL; \
  47. efi_status_t ret; \
  48. \
  49. if (tc) \
  50. atc = adjust_arg(tc); \
  51. ia64_save_scratch_fpregs(fr); \
  52. ret = efi_call_##prefix((efi_get_time_t *) __va(runtime->get_time), adjust_arg(tm), atc); \
  53. ia64_load_scratch_fpregs(fr); \
  54. return ret; \
  55. }
  56. #define STUB_SET_TIME(prefix, adjust_arg) \
  57. static efi_status_t \
  58. prefix##_set_time (efi_time_t *tm) \
  59. { \
  60. struct ia64_fpreg fr[6]; \
  61. efi_status_t ret; \
  62. \
  63. ia64_save_scratch_fpregs(fr); \
  64. ret = efi_call_##prefix((efi_set_time_t *) __va(runtime->set_time), adjust_arg(tm)); \
  65. ia64_load_scratch_fpregs(fr); \
  66. return ret; \
  67. }
  68. #define STUB_GET_WAKEUP_TIME(prefix, adjust_arg) \
  69. static efi_status_t \
  70. prefix##_get_wakeup_time (efi_bool_t *enabled, efi_bool_t *pending, efi_time_t *tm) \
  71. { \
  72. struct ia64_fpreg fr[6]; \
  73. efi_status_t ret; \
  74. \
  75. ia64_save_scratch_fpregs(fr); \
  76. ret = efi_call_##prefix((efi_get_wakeup_time_t *) __va(runtime->get_wakeup_time), \
  77. adjust_arg(enabled), adjust_arg(pending), adjust_arg(tm)); \
  78. ia64_load_scratch_fpregs(fr); \
  79. return ret; \
  80. }
  81. #define STUB_SET_WAKEUP_TIME(prefix, adjust_arg) \
  82. static efi_status_t \
  83. prefix##_set_wakeup_time (efi_bool_t enabled, efi_time_t *tm) \
  84. { \
  85. struct ia64_fpreg fr[6]; \
  86. efi_time_t *atm = NULL; \
  87. efi_status_t ret; \
  88. \
  89. if (tm) \
  90. atm = adjust_arg(tm); \
  91. ia64_save_scratch_fpregs(fr); \
  92. ret = efi_call_##prefix((efi_set_wakeup_time_t *) __va(runtime->set_wakeup_time), \
  93. enabled, atm); \
  94. ia64_load_scratch_fpregs(fr); \
  95. return ret; \
  96. }
  97. #define STUB_GET_VARIABLE(prefix, adjust_arg) \
  98. static efi_status_t \
  99. prefix##_get_variable (efi_char16_t *name, efi_guid_t *vendor, u32 *attr, \
  100. unsigned long *data_size, void *data) \
  101. { \
  102. struct ia64_fpreg fr[6]; \
  103. u32 *aattr = NULL; \
  104. efi_status_t ret; \
  105. \
  106. if (attr) \
  107. aattr = adjust_arg(attr); \
  108. ia64_save_scratch_fpregs(fr); \
  109. ret = efi_call_##prefix((efi_get_variable_t *) __va(runtime->get_variable), \
  110. adjust_arg(name), adjust_arg(vendor), aattr, \
  111. adjust_arg(data_size), adjust_arg(data)); \
  112. ia64_load_scratch_fpregs(fr); \
  113. return ret; \
  114. }
  115. #define STUB_GET_NEXT_VARIABLE(prefix, adjust_arg) \
  116. static efi_status_t \
  117. prefix##_get_next_variable (unsigned long *name_size, efi_char16_t *name, efi_guid_t *vendor) \
  118. { \
  119. struct ia64_fpreg fr[6]; \
  120. efi_status_t ret; \
  121. \
  122. ia64_save_scratch_fpregs(fr); \
  123. ret = efi_call_##prefix((efi_get_next_variable_t *) __va(runtime->get_next_variable), \
  124. adjust_arg(name_size), adjust_arg(name), adjust_arg(vendor)); \
  125. ia64_load_scratch_fpregs(fr); \
  126. return ret; \
  127. }
  128. #define STUB_SET_VARIABLE(prefix, adjust_arg) \
  129. static efi_status_t \
  130. prefix##_set_variable (efi_char16_t *name, efi_guid_t *vendor, unsigned long attr, \
  131. unsigned long data_size, void *data) \
  132. { \
  133. struct ia64_fpreg fr[6]; \
  134. efi_status_t ret; \
  135. \
  136. ia64_save_scratch_fpregs(fr); \
  137. ret = efi_call_##prefix((efi_set_variable_t *) __va(runtime->set_variable), \
  138. adjust_arg(name), adjust_arg(vendor), attr, data_size, \
  139. adjust_arg(data)); \
  140. ia64_load_scratch_fpregs(fr); \
  141. return ret; \
  142. }
  143. #define STUB_GET_NEXT_HIGH_MONO_COUNT(prefix, adjust_arg) \
  144. static efi_status_t \
  145. prefix##_get_next_high_mono_count (u32 *count) \
  146. { \
  147. struct ia64_fpreg fr[6]; \
  148. efi_status_t ret; \
  149. \
  150. ia64_save_scratch_fpregs(fr); \
  151. ret = efi_call_##prefix((efi_get_next_high_mono_count_t *) \
  152. __va(runtime->get_next_high_mono_count), adjust_arg(count)); \
  153. ia64_load_scratch_fpregs(fr); \
  154. return ret; \
  155. }
  156. #define STUB_RESET_SYSTEM(prefix, adjust_arg) \
  157. static void \
  158. prefix##_reset_system (int reset_type, efi_status_t status, \
  159. unsigned long data_size, efi_char16_t *data) \
  160. { \
  161. struct ia64_fpreg fr[6]; \
  162. efi_char16_t *adata = NULL; \
  163. \
  164. if (data) \
  165. adata = adjust_arg(data); \
  166. \
  167. ia64_save_scratch_fpregs(fr); \
  168. efi_call_##prefix((efi_reset_system_t *) __va(runtime->reset_system), \
  169. reset_type, status, data_size, adata); \
  170. /* should not return, but just in case... */ \
  171. ia64_load_scratch_fpregs(fr); \
  172. }
  173. #define phys_ptr(arg) ((__typeof__(arg)) ia64_tpa(arg))
  174. STUB_GET_TIME(phys, phys_ptr)
  175. STUB_SET_TIME(phys, phys_ptr)
  176. STUB_GET_WAKEUP_TIME(phys, phys_ptr)
  177. STUB_SET_WAKEUP_TIME(phys, phys_ptr)
  178. STUB_GET_VARIABLE(phys, phys_ptr)
  179. STUB_GET_NEXT_VARIABLE(phys, phys_ptr)
  180. STUB_SET_VARIABLE(phys, phys_ptr)
  181. STUB_GET_NEXT_HIGH_MONO_COUNT(phys, phys_ptr)
  182. STUB_RESET_SYSTEM(phys, phys_ptr)
  183. #define id(arg) arg
  184. STUB_GET_TIME(virt, id)
  185. STUB_SET_TIME(virt, id)
  186. STUB_GET_WAKEUP_TIME(virt, id)
  187. STUB_SET_WAKEUP_TIME(virt, id)
  188. STUB_GET_VARIABLE(virt, id)
  189. STUB_GET_NEXT_VARIABLE(virt, id)
  190. STUB_SET_VARIABLE(virt, id)
  191. STUB_GET_NEXT_HIGH_MONO_COUNT(virt, id)
  192. STUB_RESET_SYSTEM(virt, id)
  193. void
  194. efi_gettimeofday (struct timespec *ts)
  195. {
  196. efi_time_t tm;
  197. memset(ts, 0, sizeof(ts));
  198. if ((*efi.get_time)(&tm, NULL) != EFI_SUCCESS)
  199. return;
  200. ts->tv_sec = mktime(tm.year, tm.month, tm.day, tm.hour, tm.minute, tm.second);
  201. ts->tv_nsec = tm.nanosecond;
  202. }
  203. static int
  204. is_available_memory (efi_memory_desc_t *md)
  205. {
  206. if (!(md->attribute & EFI_MEMORY_WB))
  207. return 0;
  208. switch (md->type) {
  209. case EFI_LOADER_CODE:
  210. case EFI_LOADER_DATA:
  211. case EFI_BOOT_SERVICES_CODE:
  212. case EFI_BOOT_SERVICES_DATA:
  213. case EFI_CONVENTIONAL_MEMORY:
  214. return 1;
  215. }
  216. return 0;
  217. }
  218. typedef struct kern_memdesc {
  219. u64 attribute;
  220. u64 start;
  221. u64 num_pages;
  222. } kern_memdesc_t;
  223. static kern_memdesc_t *kern_memmap;
  224. #define efi_md_size(md) (md->num_pages << EFI_PAGE_SHIFT)
  225. static inline u64
  226. kmd_end(kern_memdesc_t *kmd)
  227. {
  228. return (kmd->start + (kmd->num_pages << EFI_PAGE_SHIFT));
  229. }
  230. static inline u64
  231. efi_md_end(efi_memory_desc_t *md)
  232. {
  233. return (md->phys_addr + efi_md_size(md));
  234. }
  235. static inline int
  236. efi_wb(efi_memory_desc_t *md)
  237. {
  238. return (md->attribute & EFI_MEMORY_WB);
  239. }
  240. static inline int
  241. efi_uc(efi_memory_desc_t *md)
  242. {
  243. return (md->attribute & EFI_MEMORY_UC);
  244. }
  245. static void
  246. walk (efi_freemem_callback_t callback, void *arg, u64 attr)
  247. {
  248. kern_memdesc_t *k;
  249. u64 start, end, voff;
  250. voff = (attr == EFI_MEMORY_WB) ? PAGE_OFFSET : __IA64_UNCACHED_OFFSET;
  251. for (k = kern_memmap; k->start != ~0UL; k++) {
  252. if (k->attribute != attr)
  253. continue;
  254. start = PAGE_ALIGN(k->start);
  255. end = (k->start + (k->num_pages << EFI_PAGE_SHIFT)) & PAGE_MASK;
  256. if (start < end)
  257. if ((*callback)(start + voff, end + voff, arg) < 0)
  258. return;
  259. }
  260. }
  261. /*
  262. * Walks the EFI memory map and calls CALLBACK once for each EFI memory descriptor that
  263. * has memory that is available for OS use.
  264. */
  265. void
  266. efi_memmap_walk (efi_freemem_callback_t callback, void *arg)
  267. {
  268. walk(callback, arg, EFI_MEMORY_WB);
  269. }
  270. /*
  271. * Walks the EFI memory map and calls CALLBACK once for each EFI memory descriptor that
  272. * has memory that is available for uncached allocator.
  273. */
  274. void
  275. efi_memmap_walk_uc (efi_freemem_callback_t callback, void *arg)
  276. {
  277. walk(callback, arg, EFI_MEMORY_UC);
  278. }
  279. /*
  280. * Look for the PAL_CODE region reported by EFI and maps it using an
  281. * ITR to enable safe PAL calls in virtual mode. See IA-64 Processor
  282. * Abstraction Layer chapter 11 in ADAG
  283. */
  284. void *
  285. efi_get_pal_addr (void)
  286. {
  287. void *efi_map_start, *efi_map_end, *p;
  288. efi_memory_desc_t *md;
  289. u64 efi_desc_size;
  290. int pal_code_count = 0;
  291. u64 vaddr, mask;
  292. efi_map_start = __va(ia64_boot_param->efi_memmap);
  293. efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
  294. efi_desc_size = ia64_boot_param->efi_memdesc_size;
  295. for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
  296. md = p;
  297. if (md->type != EFI_PAL_CODE)
  298. continue;
  299. if (++pal_code_count > 1) {
  300. printk(KERN_ERR "Too many EFI Pal Code memory ranges, dropped @ %lx\n",
  301. md->phys_addr);
  302. continue;
  303. }
  304. /*
  305. * The only ITLB entry in region 7 that is used is the one installed by
  306. * __start(). That entry covers a 64MB range.
  307. */
  308. mask = ~((1 << KERNEL_TR_PAGE_SHIFT) - 1);
  309. vaddr = PAGE_OFFSET + md->phys_addr;
  310. /*
  311. * We must check that the PAL mapping won't overlap with the kernel
  312. * mapping.
  313. *
  314. * PAL code is guaranteed to be aligned on a power of 2 between 4k and
  315. * 256KB and that only one ITR is needed to map it. This implies that the
  316. * PAL code is always aligned on its size, i.e., the closest matching page
  317. * size supported by the TLB. Therefore PAL code is guaranteed never to
  318. * cross a 64MB unless it is bigger than 64MB (very unlikely!). So for
  319. * now the following test is enough to determine whether or not we need a
  320. * dedicated ITR for the PAL code.
  321. */
  322. if ((vaddr & mask) == (KERNEL_START & mask)) {
  323. printk(KERN_INFO "%s: no need to install ITR for PAL code\n",
  324. __FUNCTION__);
  325. continue;
  326. }
  327. if (md->num_pages << EFI_PAGE_SHIFT > IA64_GRANULE_SIZE)
  328. panic("Woah! PAL code size bigger than a granule!");
  329. #if EFI_DEBUG
  330. mask = ~((1 << IA64_GRANULE_SHIFT) - 1);
  331. printk(KERN_INFO "CPU %d: mapping PAL code [0x%lx-0x%lx) into [0x%lx-0x%lx)\n",
  332. smp_processor_id(), md->phys_addr,
  333. md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT),
  334. vaddr & mask, (vaddr & mask) + IA64_GRANULE_SIZE);
  335. #endif
  336. return __va(md->phys_addr);
  337. }
  338. printk(KERN_WARNING "%s: no PAL-code memory-descriptor found",
  339. __FUNCTION__);
  340. return NULL;
  341. }
  342. void
  343. efi_map_pal_code (void)
  344. {
  345. void *pal_vaddr = efi_get_pal_addr ();
  346. u64 psr;
  347. if (!pal_vaddr)
  348. return;
  349. /*
  350. * Cannot write to CRx with PSR.ic=1
  351. */
  352. psr = ia64_clear_ic();
  353. ia64_itr(0x1, IA64_TR_PALCODE, GRANULEROUNDDOWN((unsigned long) pal_vaddr),
  354. pte_val(pfn_pte(__pa(pal_vaddr) >> PAGE_SHIFT, PAGE_KERNEL)),
  355. IA64_GRANULE_SHIFT);
  356. ia64_set_psr(psr); /* restore psr */
  357. ia64_srlz_i();
  358. }
  359. void __init
  360. efi_init (void)
  361. {
  362. void *efi_map_start, *efi_map_end;
  363. efi_config_table_t *config_tables;
  364. efi_char16_t *c16;
  365. u64 efi_desc_size;
  366. char *cp, vendor[100] = "unknown";
  367. extern char saved_command_line[];
  368. int i;
  369. /* it's too early to be able to use the standard kernel command line support... */
  370. for (cp = saved_command_line; *cp; ) {
  371. if (memcmp(cp, "mem=", 4) == 0) {
  372. mem_limit = memparse(cp + 4, &cp);
  373. } else if (memcmp(cp, "max_addr=", 9) == 0) {
  374. max_addr = GRANULEROUNDDOWN(memparse(cp + 9, &cp));
  375. } else {
  376. while (*cp != ' ' && *cp)
  377. ++cp;
  378. while (*cp == ' ')
  379. ++cp;
  380. }
  381. }
  382. if (max_addr != ~0UL)
  383. printk(KERN_INFO "Ignoring memory above %luMB\n", max_addr >> 20);
  384. efi.systab = __va(ia64_boot_param->efi_systab);
  385. /*
  386. * Verify the EFI Table
  387. */
  388. if (efi.systab == NULL)
  389. panic("Woah! Can't find EFI system table.\n");
  390. if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
  391. panic("Woah! EFI system table signature incorrect\n");
  392. if ((efi.systab->hdr.revision ^ EFI_SYSTEM_TABLE_REVISION) >> 16 != 0)
  393. printk(KERN_WARNING "Warning: EFI system table major version mismatch: "
  394. "got %d.%02d, expected %d.%02d\n",
  395. efi.systab->hdr.revision >> 16, efi.systab->hdr.revision & 0xffff,
  396. EFI_SYSTEM_TABLE_REVISION >> 16, EFI_SYSTEM_TABLE_REVISION & 0xffff);
  397. config_tables = __va(efi.systab->tables);
  398. /* Show what we know for posterity */
  399. c16 = __va(efi.systab->fw_vendor);
  400. if (c16) {
  401. for (i = 0;i < (int) sizeof(vendor) - 1 && *c16; ++i)
  402. vendor[i] = *c16++;
  403. vendor[i] = '\0';
  404. }
  405. printk(KERN_INFO "EFI v%u.%.02u by %s:",
  406. efi.systab->hdr.revision >> 16, efi.systab->hdr.revision & 0xffff, vendor);
  407. efi.mps = EFI_INVALID_TABLE_ADDR;
  408. efi.acpi = EFI_INVALID_TABLE_ADDR;
  409. efi.acpi20 = EFI_INVALID_TABLE_ADDR;
  410. efi.smbios = EFI_INVALID_TABLE_ADDR;
  411. efi.sal_systab = EFI_INVALID_TABLE_ADDR;
  412. efi.boot_info = EFI_INVALID_TABLE_ADDR;
  413. efi.hcdp = EFI_INVALID_TABLE_ADDR;
  414. efi.uga = EFI_INVALID_TABLE_ADDR;
  415. for (i = 0; i < (int) efi.systab->nr_tables; i++) {
  416. if (efi_guidcmp(config_tables[i].guid, MPS_TABLE_GUID) == 0) {
  417. efi.mps = config_tables[i].table;
  418. printk(" MPS=0x%lx", config_tables[i].table);
  419. } else if (efi_guidcmp(config_tables[i].guid, ACPI_20_TABLE_GUID) == 0) {
  420. efi.acpi20 = config_tables[i].table;
  421. printk(" ACPI 2.0=0x%lx", config_tables[i].table);
  422. } else if (efi_guidcmp(config_tables[i].guid, ACPI_TABLE_GUID) == 0) {
  423. efi.acpi = config_tables[i].table;
  424. printk(" ACPI=0x%lx", config_tables[i].table);
  425. } else if (efi_guidcmp(config_tables[i].guid, SMBIOS_TABLE_GUID) == 0) {
  426. efi.smbios = config_tables[i].table;
  427. printk(" SMBIOS=0x%lx", config_tables[i].table);
  428. } else if (efi_guidcmp(config_tables[i].guid, SAL_SYSTEM_TABLE_GUID) == 0) {
  429. efi.sal_systab = config_tables[i].table;
  430. printk(" SALsystab=0x%lx", config_tables[i].table);
  431. } else if (efi_guidcmp(config_tables[i].guid, HCDP_TABLE_GUID) == 0) {
  432. efi.hcdp = config_tables[i].table;
  433. printk(" HCDP=0x%lx", config_tables[i].table);
  434. }
  435. }
  436. printk("\n");
  437. runtime = __va(efi.systab->runtime);
  438. efi.get_time = phys_get_time;
  439. efi.set_time = phys_set_time;
  440. efi.get_wakeup_time = phys_get_wakeup_time;
  441. efi.set_wakeup_time = phys_set_wakeup_time;
  442. efi.get_variable = phys_get_variable;
  443. efi.get_next_variable = phys_get_next_variable;
  444. efi.set_variable = phys_set_variable;
  445. efi.get_next_high_mono_count = phys_get_next_high_mono_count;
  446. efi.reset_system = phys_reset_system;
  447. efi_map_start = __va(ia64_boot_param->efi_memmap);
  448. efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
  449. efi_desc_size = ia64_boot_param->efi_memdesc_size;
  450. #if EFI_DEBUG
  451. /* print EFI memory map: */
  452. {
  453. efi_memory_desc_t *md;
  454. void *p;
  455. for (i = 0, p = efi_map_start; p < efi_map_end; ++i, p += efi_desc_size) {
  456. md = p;
  457. printk("mem%02u: type=%u, attr=0x%lx, range=[0x%016lx-0x%016lx) (%luMB)\n",
  458. i, md->type, md->attribute, md->phys_addr,
  459. md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT),
  460. md->num_pages >> (20 - EFI_PAGE_SHIFT));
  461. }
  462. }
  463. #endif
  464. efi_map_pal_code();
  465. efi_enter_virtual_mode();
  466. }
  467. void
  468. efi_enter_virtual_mode (void)
  469. {
  470. void *efi_map_start, *efi_map_end, *p;
  471. efi_memory_desc_t *md;
  472. efi_status_t status;
  473. u64 efi_desc_size;
  474. efi_map_start = __va(ia64_boot_param->efi_memmap);
  475. efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
  476. efi_desc_size = ia64_boot_param->efi_memdesc_size;
  477. for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
  478. md = p;
  479. if (md->attribute & EFI_MEMORY_RUNTIME) {
  480. /*
  481. * Some descriptors have multiple bits set, so the order of
  482. * the tests is relevant.
  483. */
  484. if (md->attribute & EFI_MEMORY_WB) {
  485. md->virt_addr = (u64) __va(md->phys_addr);
  486. } else if (md->attribute & EFI_MEMORY_UC) {
  487. md->virt_addr = (u64) ioremap(md->phys_addr, 0);
  488. } else if (md->attribute & EFI_MEMORY_WC) {
  489. #if 0
  490. md->virt_addr = ia64_remap(md->phys_addr, (_PAGE_A | _PAGE_P
  491. | _PAGE_D
  492. | _PAGE_MA_WC
  493. | _PAGE_PL_0
  494. | _PAGE_AR_RW));
  495. #else
  496. printk(KERN_INFO "EFI_MEMORY_WC mapping\n");
  497. md->virt_addr = (u64) ioremap(md->phys_addr, 0);
  498. #endif
  499. } else if (md->attribute & EFI_MEMORY_WT) {
  500. #if 0
  501. md->virt_addr = ia64_remap(md->phys_addr, (_PAGE_A | _PAGE_P
  502. | _PAGE_D | _PAGE_MA_WT
  503. | _PAGE_PL_0
  504. | _PAGE_AR_RW));
  505. #else
  506. printk(KERN_INFO "EFI_MEMORY_WT mapping\n");
  507. md->virt_addr = (u64) ioremap(md->phys_addr, 0);
  508. #endif
  509. }
  510. }
  511. }
  512. status = efi_call_phys(__va(runtime->set_virtual_address_map),
  513. ia64_boot_param->efi_memmap_size,
  514. efi_desc_size, ia64_boot_param->efi_memdesc_version,
  515. ia64_boot_param->efi_memmap);
  516. if (status != EFI_SUCCESS) {
  517. printk(KERN_WARNING "warning: unable to switch EFI into virtual mode "
  518. "(status=%lu)\n", status);
  519. return;
  520. }
  521. /*
  522. * Now that EFI is in virtual mode, we call the EFI functions more efficiently:
  523. */
  524. efi.get_time = virt_get_time;
  525. efi.set_time = virt_set_time;
  526. efi.get_wakeup_time = virt_get_wakeup_time;
  527. efi.set_wakeup_time = virt_set_wakeup_time;
  528. efi.get_variable = virt_get_variable;
  529. efi.get_next_variable = virt_get_next_variable;
  530. efi.set_variable = virt_set_variable;
  531. efi.get_next_high_mono_count = virt_get_next_high_mono_count;
  532. efi.reset_system = virt_reset_system;
  533. }
  534. /*
  535. * Walk the EFI memory map looking for the I/O port range. There can only be one entry of
  536. * this type, other I/O port ranges should be described via ACPI.
  537. */
  538. u64
  539. efi_get_iobase (void)
  540. {
  541. void *efi_map_start, *efi_map_end, *p;
  542. efi_memory_desc_t *md;
  543. u64 efi_desc_size;
  544. efi_map_start = __va(ia64_boot_param->efi_memmap);
  545. efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
  546. efi_desc_size = ia64_boot_param->efi_memdesc_size;
  547. for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
  548. md = p;
  549. if (md->type == EFI_MEMORY_MAPPED_IO_PORT_SPACE) {
  550. if (md->attribute & EFI_MEMORY_UC)
  551. return md->phys_addr;
  552. }
  553. }
  554. return 0;
  555. }
  556. static efi_memory_desc_t *
  557. efi_memory_descriptor (unsigned long phys_addr)
  558. {
  559. void *efi_map_start, *efi_map_end, *p;
  560. efi_memory_desc_t *md;
  561. u64 efi_desc_size;
  562. efi_map_start = __va(ia64_boot_param->efi_memmap);
  563. efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
  564. efi_desc_size = ia64_boot_param->efi_memdesc_size;
  565. for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
  566. md = p;
  567. if (phys_addr - md->phys_addr < (md->num_pages << EFI_PAGE_SHIFT))
  568. return md;
  569. }
  570. return 0;
  571. }
  572. static int
  573. efi_memmap_has_mmio (void)
  574. {
  575. void *efi_map_start, *efi_map_end, *p;
  576. efi_memory_desc_t *md;
  577. u64 efi_desc_size;
  578. efi_map_start = __va(ia64_boot_param->efi_memmap);
  579. efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
  580. efi_desc_size = ia64_boot_param->efi_memdesc_size;
  581. for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
  582. md = p;
  583. if (md->type == EFI_MEMORY_MAPPED_IO)
  584. return 1;
  585. }
  586. return 0;
  587. }
  588. u32
  589. efi_mem_type (unsigned long phys_addr)
  590. {
  591. efi_memory_desc_t *md = efi_memory_descriptor(phys_addr);
  592. if (md)
  593. return md->type;
  594. return 0;
  595. }
  596. u64
  597. efi_mem_attributes (unsigned long phys_addr)
  598. {
  599. efi_memory_desc_t *md = efi_memory_descriptor(phys_addr);
  600. if (md)
  601. return md->attribute;
  602. return 0;
  603. }
  604. EXPORT_SYMBOL(efi_mem_attributes);
  605. /*
  606. * Determines whether the memory at phys_addr supports the desired
  607. * attribute (WB, UC, etc). If this returns 1, the caller can safely
  608. * access size bytes at phys_addr with the specified attribute.
  609. */
  610. int
  611. efi_mem_attribute_range (unsigned long phys_addr, unsigned long size, u64 attr)
  612. {
  613. unsigned long end = phys_addr + size;
  614. efi_memory_desc_t *md = efi_memory_descriptor(phys_addr);
  615. /*
  616. * Some firmware doesn't report MMIO regions in the EFI memory
  617. * map. The Intel BigSur (a.k.a. HP i2000) has this problem.
  618. * On those platforms, we have to assume UC is valid everywhere.
  619. */
  620. if (!md || (md->attribute & attr) != attr) {
  621. if (attr == EFI_MEMORY_UC && !efi_memmap_has_mmio())
  622. return 1;
  623. return 0;
  624. }
  625. do {
  626. unsigned long md_end = efi_md_end(md);
  627. if (end <= md_end)
  628. return 1;
  629. md = efi_memory_descriptor(md_end);
  630. if (!md || (md->attribute & attr) != attr)
  631. return 0;
  632. } while (md);
  633. return 0;
  634. }
  635. /*
  636. * For /dev/mem, we only allow read & write system calls to access
  637. * write-back memory, because read & write don't allow the user to
  638. * control access size.
  639. */
  640. int
  641. valid_phys_addr_range (unsigned long phys_addr, unsigned long size)
  642. {
  643. return efi_mem_attribute_range(phys_addr, size, EFI_MEMORY_WB);
  644. }
  645. /*
  646. * We allow mmap of anything in the EFI memory map that supports
  647. * either write-back or uncacheable access. For uncacheable regions,
  648. * the supported access sizes are system-dependent, and the user is
  649. * responsible for using the correct size.
  650. *
  651. * Note that this doesn't currently allow access to hot-added memory,
  652. * because that doesn't appear in the boot-time EFI memory map.
  653. */
  654. int
  655. valid_mmap_phys_addr_range (unsigned long phys_addr, unsigned long size)
  656. {
  657. if (efi_mem_attribute_range(phys_addr, size, EFI_MEMORY_WB))
  658. return 1;
  659. if (efi_mem_attribute_range(phys_addr, size, EFI_MEMORY_UC))
  660. return 1;
  661. return 0;
  662. }
  663. int __init
  664. efi_uart_console_only(void)
  665. {
  666. efi_status_t status;
  667. char *s, name[] = "ConOut";
  668. efi_guid_t guid = EFI_GLOBAL_VARIABLE_GUID;
  669. efi_char16_t *utf16, name_utf16[32];
  670. unsigned char data[1024];
  671. unsigned long size = sizeof(data);
  672. struct efi_generic_dev_path *hdr, *end_addr;
  673. int uart = 0;
  674. /* Convert to UTF-16 */
  675. utf16 = name_utf16;
  676. s = name;
  677. while (*s)
  678. *utf16++ = *s++ & 0x7f;
  679. *utf16 = 0;
  680. status = efi.get_variable(name_utf16, &guid, NULL, &size, data);
  681. if (status != EFI_SUCCESS) {
  682. printk(KERN_ERR "No EFI %s variable?\n", name);
  683. return 0;
  684. }
  685. hdr = (struct efi_generic_dev_path *) data;
  686. end_addr = (struct efi_generic_dev_path *) ((u8 *) data + size);
  687. while (hdr < end_addr) {
  688. if (hdr->type == EFI_DEV_MSG &&
  689. hdr->sub_type == EFI_DEV_MSG_UART)
  690. uart = 1;
  691. else if (hdr->type == EFI_DEV_END_PATH ||
  692. hdr->type == EFI_DEV_END_PATH2) {
  693. if (!uart)
  694. return 0;
  695. if (hdr->sub_type == EFI_DEV_END_ENTIRE)
  696. return 1;
  697. uart = 0;
  698. }
  699. hdr = (struct efi_generic_dev_path *) ((u8 *) hdr + hdr->length);
  700. }
  701. printk(KERN_ERR "Malformed %s value\n", name);
  702. return 0;
  703. }
  704. /*
  705. * Look for the first granule aligned memory descriptor memory
  706. * that is big enough to hold EFI memory map. Make sure this
  707. * descriptor is atleast granule sized so it does not get trimmed
  708. */
  709. struct kern_memdesc *
  710. find_memmap_space (void)
  711. {
  712. u64 contig_low=0, contig_high=0;
  713. u64 as = 0, ae;
  714. void *efi_map_start, *efi_map_end, *p, *q;
  715. efi_memory_desc_t *md, *pmd = NULL, *check_md;
  716. u64 space_needed, efi_desc_size;
  717. unsigned long total_mem = 0;
  718. efi_map_start = __va(ia64_boot_param->efi_memmap);
  719. efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
  720. efi_desc_size = ia64_boot_param->efi_memdesc_size;
  721. /*
  722. * Worst case: we need 3 kernel descriptors for each efi descriptor
  723. * (if every entry has a WB part in the middle, and UC head and tail),
  724. * plus one for the end marker.
  725. */
  726. space_needed = sizeof(kern_memdesc_t) *
  727. (3 * (ia64_boot_param->efi_memmap_size/efi_desc_size) + 1);
  728. for (p = efi_map_start; p < efi_map_end; pmd = md, p += efi_desc_size) {
  729. md = p;
  730. if (!efi_wb(md)) {
  731. continue;
  732. }
  733. if (pmd == NULL || !efi_wb(pmd) || efi_md_end(pmd) != md->phys_addr) {
  734. contig_low = GRANULEROUNDUP(md->phys_addr);
  735. contig_high = efi_md_end(md);
  736. for (q = p + efi_desc_size; q < efi_map_end; q += efi_desc_size) {
  737. check_md = q;
  738. if (!efi_wb(check_md))
  739. break;
  740. if (contig_high != check_md->phys_addr)
  741. break;
  742. contig_high = efi_md_end(check_md);
  743. }
  744. contig_high = GRANULEROUNDDOWN(contig_high);
  745. }
  746. if (!is_available_memory(md) || md->type == EFI_LOADER_DATA)
  747. continue;
  748. /* Round ends inward to granule boundaries */
  749. as = max(contig_low, md->phys_addr);
  750. ae = min(contig_high, efi_md_end(md));
  751. /* keep within max_addr= command line arg */
  752. ae = min(ae, max_addr);
  753. if (ae <= as)
  754. continue;
  755. /* avoid going over mem= command line arg */
  756. if (total_mem + (ae - as) > mem_limit)
  757. ae -= total_mem + (ae - as) - mem_limit;
  758. if (ae <= as)
  759. continue;
  760. if (ae - as > space_needed)
  761. break;
  762. }
  763. if (p >= efi_map_end)
  764. panic("Can't allocate space for kernel memory descriptors");
  765. return __va(as);
  766. }
  767. /*
  768. * Walk the EFI memory map and gather all memory available for kernel
  769. * to use. We can allocate partial granules only if the unavailable
  770. * parts exist, and are WB.
  771. */
  772. void
  773. efi_memmap_init(unsigned long *s, unsigned long *e)
  774. {
  775. struct kern_memdesc *k, *prev = 0;
  776. u64 contig_low=0, contig_high=0;
  777. u64 as, ae, lim;
  778. void *efi_map_start, *efi_map_end, *p, *q;
  779. efi_memory_desc_t *md, *pmd = NULL, *check_md;
  780. u64 efi_desc_size;
  781. unsigned long total_mem = 0;
  782. k = kern_memmap = find_memmap_space();
  783. efi_map_start = __va(ia64_boot_param->efi_memmap);
  784. efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
  785. efi_desc_size = ia64_boot_param->efi_memdesc_size;
  786. for (p = efi_map_start; p < efi_map_end; pmd = md, p += efi_desc_size) {
  787. md = p;
  788. if (!efi_wb(md)) {
  789. if (efi_uc(md) && (md->type == EFI_CONVENTIONAL_MEMORY ||
  790. md->type == EFI_BOOT_SERVICES_DATA)) {
  791. k->attribute = EFI_MEMORY_UC;
  792. k->start = md->phys_addr;
  793. k->num_pages = md->num_pages;
  794. k++;
  795. }
  796. continue;
  797. }
  798. if (pmd == NULL || !efi_wb(pmd) || efi_md_end(pmd) != md->phys_addr) {
  799. contig_low = GRANULEROUNDUP(md->phys_addr);
  800. contig_high = efi_md_end(md);
  801. for (q = p + efi_desc_size; q < efi_map_end; q += efi_desc_size) {
  802. check_md = q;
  803. if (!efi_wb(check_md))
  804. break;
  805. if (contig_high != check_md->phys_addr)
  806. break;
  807. contig_high = efi_md_end(check_md);
  808. }
  809. contig_high = GRANULEROUNDDOWN(contig_high);
  810. }
  811. if (!is_available_memory(md))
  812. continue;
  813. /*
  814. * Round ends inward to granule boundaries
  815. * Give trimmings to uncached allocator
  816. */
  817. if (md->phys_addr < contig_low) {
  818. lim = min(efi_md_end(md), contig_low);
  819. if (efi_uc(md)) {
  820. if (k > kern_memmap && (k-1)->attribute == EFI_MEMORY_UC &&
  821. kmd_end(k-1) == md->phys_addr) {
  822. (k-1)->num_pages += (lim - md->phys_addr) >> EFI_PAGE_SHIFT;
  823. } else {
  824. k->attribute = EFI_MEMORY_UC;
  825. k->start = md->phys_addr;
  826. k->num_pages = (lim - md->phys_addr) >> EFI_PAGE_SHIFT;
  827. k++;
  828. }
  829. }
  830. as = contig_low;
  831. } else
  832. as = md->phys_addr;
  833. if (efi_md_end(md) > contig_high) {
  834. lim = max(md->phys_addr, contig_high);
  835. if (efi_uc(md)) {
  836. if (lim == md->phys_addr && k > kern_memmap &&
  837. (k-1)->attribute == EFI_MEMORY_UC &&
  838. kmd_end(k-1) == md->phys_addr) {
  839. (k-1)->num_pages += md->num_pages;
  840. } else {
  841. k->attribute = EFI_MEMORY_UC;
  842. k->start = lim;
  843. k->num_pages = (efi_md_end(md) - lim) >> EFI_PAGE_SHIFT;
  844. k++;
  845. }
  846. }
  847. ae = contig_high;
  848. } else
  849. ae = efi_md_end(md);
  850. /* keep within max_addr= command line arg */
  851. ae = min(ae, max_addr);
  852. if (ae <= as)
  853. continue;
  854. /* avoid going over mem= command line arg */
  855. if (total_mem + (ae - as) > mem_limit)
  856. ae -= total_mem + (ae - as) - mem_limit;
  857. if (ae <= as)
  858. continue;
  859. if (prev && kmd_end(prev) == md->phys_addr) {
  860. prev->num_pages += (ae - as) >> EFI_PAGE_SHIFT;
  861. total_mem += ae - as;
  862. continue;
  863. }
  864. k->attribute = EFI_MEMORY_WB;
  865. k->start = as;
  866. k->num_pages = (ae - as) >> EFI_PAGE_SHIFT;
  867. total_mem += ae - as;
  868. prev = k++;
  869. }
  870. k->start = ~0L; /* end-marker */
  871. /* reserve the memory we are using for kern_memmap */
  872. *s = (u64)kern_memmap;
  873. *e = (u64)++k;
  874. }
  875. void
  876. efi_initialize_iomem_resources(struct resource *code_resource,
  877. struct resource *data_resource)
  878. {
  879. struct resource *res;
  880. void *efi_map_start, *efi_map_end, *p;
  881. efi_memory_desc_t *md;
  882. u64 efi_desc_size;
  883. char *name;
  884. unsigned long flags;
  885. efi_map_start = __va(ia64_boot_param->efi_memmap);
  886. efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
  887. efi_desc_size = ia64_boot_param->efi_memdesc_size;
  888. res = NULL;
  889. for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
  890. md = p;
  891. if (md->num_pages == 0) /* should not happen */
  892. continue;
  893. flags = IORESOURCE_MEM;
  894. switch (md->type) {
  895. case EFI_MEMORY_MAPPED_IO:
  896. case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
  897. continue;
  898. case EFI_LOADER_CODE:
  899. case EFI_LOADER_DATA:
  900. case EFI_BOOT_SERVICES_DATA:
  901. case EFI_BOOT_SERVICES_CODE:
  902. case EFI_CONVENTIONAL_MEMORY:
  903. if (md->attribute & EFI_MEMORY_WP) {
  904. name = "System ROM";
  905. flags |= IORESOURCE_READONLY;
  906. } else {
  907. name = "System RAM";
  908. }
  909. break;
  910. case EFI_ACPI_MEMORY_NVS:
  911. name = "ACPI Non-volatile Storage";
  912. flags |= IORESOURCE_BUSY;
  913. break;
  914. case EFI_UNUSABLE_MEMORY:
  915. name = "reserved";
  916. flags |= IORESOURCE_BUSY | IORESOURCE_DISABLED;
  917. break;
  918. case EFI_RESERVED_TYPE:
  919. case EFI_RUNTIME_SERVICES_CODE:
  920. case EFI_RUNTIME_SERVICES_DATA:
  921. case EFI_ACPI_RECLAIM_MEMORY:
  922. default:
  923. name = "reserved";
  924. flags |= IORESOURCE_BUSY;
  925. break;
  926. }
  927. if ((res = kzalloc(sizeof(struct resource), GFP_KERNEL)) == NULL) {
  928. printk(KERN_ERR "failed to alocate resource for iomem\n");
  929. return;
  930. }
  931. res->name = name;
  932. res->start = md->phys_addr;
  933. res->end = md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT) - 1;
  934. res->flags = flags;
  935. if (insert_resource(&iomem_resource, res) < 0)
  936. kfree(res);
  937. else {
  938. /*
  939. * We don't know which region contains
  940. * kernel data so we try it repeatedly and
  941. * let the resource manager test it.
  942. */
  943. insert_resource(res, code_resource);
  944. insert_resource(res, data_resource);
  945. }
  946. }
  947. }