efi.c 31 KB

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