efi.c 31 KB

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