fw-emu.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * PAL & SAL emulation.
  4. *
  5. * Copyright (C) 1998-2001 Hewlett-Packard Co
  6. * David Mosberger-Tang <davidm@hpl.hp.com>
  7. */
  8. #ifdef CONFIG_PCI
  9. # include <linux/pci.h>
  10. #endif
  11. #include <linux/efi.h>
  12. #include <asm/io.h>
  13. #include <asm/pal.h>
  14. #include <asm/sal.h>
  15. #include <asm/setup.h>
  16. #include "ssc.h"
  17. #define MB (1024*1024UL)
  18. #define SIMPLE_MEMMAP 1
  19. #if SIMPLE_MEMMAP
  20. # define NUM_MEM_DESCS 4
  21. #else
  22. # define NUM_MEM_DESCS 16
  23. #endif
  24. static char fw_mem[( sizeof(struct ia64_boot_param)
  25. + sizeof(efi_system_table_t)
  26. + sizeof(efi_runtime_services_t)
  27. + 1*sizeof(efi_config_table_t)
  28. + sizeof(struct ia64_sal_systab)
  29. + sizeof(struct ia64_sal_desc_entry_point)
  30. + NUM_MEM_DESCS*(sizeof(efi_memory_desc_t))
  31. + 1024)] __attribute__ ((aligned (8)));
  32. #define SECS_PER_HOUR (60 * 60)
  33. #define SECS_PER_DAY (SECS_PER_HOUR * 24)
  34. /* Compute the `struct tm' representation of *T,
  35. offset OFFSET seconds east of UTC,
  36. and store year, yday, mon, mday, wday, hour, min, sec into *TP.
  37. Return nonzero if successful. */
  38. int
  39. offtime (unsigned long t, efi_time_t *tp)
  40. {
  41. const unsigned short int __mon_yday[2][13] =
  42. {
  43. /* Normal years. */
  44. { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
  45. /* Leap years. */
  46. { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
  47. };
  48. long int days, rem, y;
  49. const unsigned short int *ip;
  50. days = t / SECS_PER_DAY;
  51. rem = t % SECS_PER_DAY;
  52. while (rem < 0) {
  53. rem += SECS_PER_DAY;
  54. --days;
  55. }
  56. while (rem >= SECS_PER_DAY) {
  57. rem -= SECS_PER_DAY;
  58. ++days;
  59. }
  60. tp->hour = rem / SECS_PER_HOUR;
  61. rem %= SECS_PER_HOUR;
  62. tp->minute = rem / 60;
  63. tp->second = rem % 60;
  64. /* January 1, 1970 was a Thursday. */
  65. y = 1970;
  66. # define DIV(a, b) ((a) / (b) - ((a) % (b) < 0))
  67. # define LEAPS_THRU_END_OF(y) (DIV (y, 4) - DIV (y, 100) + DIV (y, 400))
  68. # define __isleap(year) \
  69. ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
  70. while (days < 0 || days >= (__isleap (y) ? 366 : 365)) {
  71. /* Guess a corrected year, assuming 365 days per year. */
  72. long int yg = y + days / 365 - (days % 365 < 0);
  73. /* Adjust DAYS and Y to match the guessed year. */
  74. days -= ((yg - y) * 365 + LEAPS_THRU_END_OF (yg - 1)
  75. - LEAPS_THRU_END_OF (y - 1));
  76. y = yg;
  77. }
  78. tp->year = y;
  79. ip = __mon_yday[__isleap(y)];
  80. for (y = 11; days < (long int) ip[y]; --y)
  81. continue;
  82. days -= ip[y];
  83. tp->month = y + 1;
  84. tp->day = days + 1;
  85. return 1;
  86. }
  87. extern void pal_emulator_static (void);
  88. /* Macro to emulate SAL call using legacy IN and OUT calls to CF8, CFC etc.. */
  89. #define BUILD_CMD(addr) ((0x80000000 | (addr)) & ~3)
  90. #define REG_OFFSET(addr) (0x00000000000000FF & (addr))
  91. #define DEVICE_FUNCTION(addr) (0x000000000000FF00 & (addr))
  92. #define BUS_NUMBER(addr) (0x0000000000FF0000 & (addr))
  93. static efi_status_t
  94. fw_efi_get_time (efi_time_t *tm, efi_time_cap_t *tc)
  95. {
  96. #if defined(CONFIG_IA64_HP_SIM) || defined(CONFIG_IA64_GENERIC)
  97. struct {
  98. int tv_sec; /* must be 32bits to work */
  99. int tv_usec;
  100. } tv32bits;
  101. ssc((unsigned long) &tv32bits, 0, 0, 0, SSC_GET_TOD);
  102. memset(tm, 0, sizeof(*tm));
  103. offtime(tv32bits.tv_sec, tm);
  104. if (tc)
  105. memset(tc, 0, sizeof(*tc));
  106. #else
  107. # error Not implemented yet...
  108. #endif
  109. return EFI_SUCCESS;
  110. }
  111. static void
  112. efi_reset_system (int reset_type, efi_status_t status, unsigned long data_size, efi_char16_t *data)
  113. {
  114. #if defined(CONFIG_IA64_HP_SIM) || defined(CONFIG_IA64_GENERIC)
  115. ssc(status, 0, 0, 0, SSC_EXIT);
  116. #else
  117. # error Not implemented yet...
  118. #endif
  119. }
  120. static efi_status_t
  121. efi_unimplemented (void)
  122. {
  123. return EFI_UNSUPPORTED;
  124. }
  125. static struct sal_ret_values
  126. sal_emulator (long index, unsigned long in1, unsigned long in2,
  127. unsigned long in3, unsigned long in4, unsigned long in5,
  128. unsigned long in6, unsigned long in7)
  129. {
  130. long r9 = 0;
  131. long r10 = 0;
  132. long r11 = 0;
  133. long status;
  134. /*
  135. * Don't do a "switch" here since that gives us code that
  136. * isn't self-relocatable.
  137. */
  138. status = 0;
  139. if (index == SAL_FREQ_BASE) {
  140. if (in1 == SAL_FREQ_BASE_PLATFORM)
  141. r9 = 200000000;
  142. else if (in1 == SAL_FREQ_BASE_INTERVAL_TIMER) {
  143. /*
  144. * Is this supposed to be the cr.itc frequency
  145. * or something platform specific? The SAL
  146. * doc ain't exactly clear on this...
  147. */
  148. r9 = 700000000;
  149. } else if (in1 == SAL_FREQ_BASE_REALTIME_CLOCK)
  150. r9 = 1;
  151. else
  152. status = -1;
  153. } else if (index == SAL_SET_VECTORS) {
  154. ;
  155. } else if (index == SAL_GET_STATE_INFO) {
  156. ;
  157. } else if (index == SAL_GET_STATE_INFO_SIZE) {
  158. ;
  159. } else if (index == SAL_CLEAR_STATE_INFO) {
  160. ;
  161. } else if (index == SAL_MC_RENDEZ) {
  162. ;
  163. } else if (index == SAL_MC_SET_PARAMS) {
  164. ;
  165. } else if (index == SAL_CACHE_FLUSH) {
  166. ;
  167. } else if (index == SAL_CACHE_INIT) {
  168. ;
  169. #ifdef CONFIG_PCI
  170. } else if (index == SAL_PCI_CONFIG_READ) {
  171. /*
  172. * in1 contains the PCI configuration address and in2
  173. * the size of the read. The value that is read is
  174. * returned via the general register r9.
  175. */
  176. outl(BUILD_CMD(in1), 0xCF8);
  177. if (in2 == 1) /* Reading byte */
  178. r9 = inb(0xCFC + ((REG_OFFSET(in1) & 3)));
  179. else if (in2 == 2) /* Reading word */
  180. r9 = inw(0xCFC + ((REG_OFFSET(in1) & 2)));
  181. else /* Reading dword */
  182. r9 = inl(0xCFC);
  183. status = PCIBIOS_SUCCESSFUL;
  184. } else if (index == SAL_PCI_CONFIG_WRITE) {
  185. /*
  186. * in1 contains the PCI configuration address, in2 the
  187. * size of the write, and in3 the actual value to be
  188. * written out.
  189. */
  190. outl(BUILD_CMD(in1), 0xCF8);
  191. if (in2 == 1) /* Writing byte */
  192. outb(in3, 0xCFC + ((REG_OFFSET(in1) & 3)));
  193. else if (in2 == 2) /* Writing word */
  194. outw(in3, 0xCFC + ((REG_OFFSET(in1) & 2)));
  195. else /* Writing dword */
  196. outl(in3, 0xCFC);
  197. status = PCIBIOS_SUCCESSFUL;
  198. #endif /* CONFIG_PCI */
  199. } else if (index == SAL_UPDATE_PAL) {
  200. ;
  201. } else {
  202. status = -1;
  203. }
  204. return ((struct sal_ret_values) {status, r9, r10, r11});
  205. }
  206. struct ia64_boot_param *
  207. sys_fw_init (const char *args, int arglen)
  208. {
  209. efi_system_table_t *efi_systab;
  210. efi_runtime_services_t *efi_runtime;
  211. efi_config_table_t *efi_tables;
  212. struct ia64_sal_systab *sal_systab;
  213. efi_memory_desc_t *efi_memmap, *md;
  214. unsigned long *pal_desc, *sal_desc;
  215. struct ia64_sal_desc_entry_point *sal_ed;
  216. struct ia64_boot_param *bp;
  217. unsigned char checksum = 0;
  218. char *cp, *cmd_line;
  219. int i = 0;
  220. # define MAKE_MD(typ, attr, start, end) \
  221. do { \
  222. md = efi_memmap + i++; \
  223. md->type = typ; \
  224. md->pad = 0; \
  225. md->phys_addr = start; \
  226. md->virt_addr = 0; \
  227. md->num_pages = (end - start) >> 12; \
  228. md->attribute = attr; \
  229. } while (0)
  230. memset(fw_mem, 0, sizeof(fw_mem));
  231. pal_desc = (unsigned long *) &pal_emulator_static;
  232. sal_desc = (unsigned long *) &sal_emulator;
  233. cp = fw_mem;
  234. efi_systab = (void *) cp; cp += sizeof(*efi_systab);
  235. efi_runtime = (void *) cp; cp += sizeof(*efi_runtime);
  236. efi_tables = (void *) cp; cp += sizeof(*efi_tables);
  237. sal_systab = (void *) cp; cp += sizeof(*sal_systab);
  238. sal_ed = (void *) cp; cp += sizeof(*sal_ed);
  239. efi_memmap = (void *) cp; cp += NUM_MEM_DESCS*sizeof(*efi_memmap);
  240. bp = (void *) cp; cp += sizeof(*bp);
  241. cmd_line = (void *) cp;
  242. if (args) {
  243. if (arglen >= 1024)
  244. arglen = 1023;
  245. memcpy(cmd_line, args, arglen);
  246. } else {
  247. arglen = 0;
  248. }
  249. cmd_line[arglen] = '\0';
  250. memset(efi_systab, 0, sizeof(*efi_systab));
  251. efi_systab->hdr.signature = EFI_SYSTEM_TABLE_SIGNATURE;
  252. efi_systab->hdr.revision = ((1 << 16) | 00);
  253. efi_systab->hdr.headersize = sizeof(efi_systab->hdr);
  254. efi_systab->fw_vendor = __pa("H\0e\0w\0l\0e\0t\0t\0-\0P\0a\0c\0k\0a\0r\0d\0\0");
  255. efi_systab->fw_revision = 1;
  256. efi_systab->runtime = (void *) __pa(efi_runtime);
  257. efi_systab->nr_tables = 1;
  258. efi_systab->tables = __pa(efi_tables);
  259. efi_runtime->hdr.signature = EFI_RUNTIME_SERVICES_SIGNATURE;
  260. efi_runtime->hdr.revision = EFI_RUNTIME_SERVICES_REVISION;
  261. efi_runtime->hdr.headersize = sizeof(efi_runtime->hdr);
  262. efi_runtime->get_time = (void *)__pa(&fw_efi_get_time);
  263. efi_runtime->set_time = (void *)__pa(&efi_unimplemented);
  264. efi_runtime->get_wakeup_time = (void *)__pa(&efi_unimplemented);
  265. efi_runtime->set_wakeup_time = (void *)__pa(&efi_unimplemented);
  266. efi_runtime->set_virtual_address_map = (void *)__pa(&efi_unimplemented);
  267. efi_runtime->get_variable = (void *)__pa(&efi_unimplemented);
  268. efi_runtime->get_next_variable = (void *)__pa(&efi_unimplemented);
  269. efi_runtime->set_variable = (void *)__pa(&efi_unimplemented);
  270. efi_runtime->get_next_high_mono_count = (void *)__pa(&efi_unimplemented);
  271. efi_runtime->reset_system = (void *)__pa(&efi_reset_system);
  272. efi_tables->guid = SAL_SYSTEM_TABLE_GUID;
  273. efi_tables->table = __pa(sal_systab);
  274. /* fill in the SAL system table: */
  275. memcpy(sal_systab->signature, "SST_", 4);
  276. sal_systab->size = sizeof(*sal_systab);
  277. sal_systab->sal_rev_minor = 1;
  278. sal_systab->sal_rev_major = 0;
  279. sal_systab->entry_count = 1;
  280. #ifdef CONFIG_IA64_GENERIC
  281. strcpy(sal_systab->oem_id, "Generic");
  282. strcpy(sal_systab->product_id, "IA-64 system");
  283. #endif
  284. #ifdef CONFIG_IA64_HP_SIM
  285. strcpy(sal_systab->oem_id, "Hewlett-Packard");
  286. strcpy(sal_systab->product_id, "HP-simulator");
  287. #endif
  288. /* fill in an entry point: */
  289. sal_ed->type = SAL_DESC_ENTRY_POINT;
  290. sal_ed->pal_proc = __pa(pal_desc[0]);
  291. sal_ed->sal_proc = __pa(sal_desc[0]);
  292. sal_ed->gp = __pa(sal_desc[1]);
  293. for (cp = (char *) sal_systab; cp < (char *) efi_memmap; ++cp)
  294. checksum += *cp;
  295. sal_systab->checksum = -checksum;
  296. #if SIMPLE_MEMMAP
  297. /* simulate free memory at physical address zero */
  298. MAKE_MD(EFI_BOOT_SERVICES_DATA, EFI_MEMORY_WB, 0*MB, 1*MB);
  299. MAKE_MD(EFI_PAL_CODE, EFI_MEMORY_WB, 1*MB, 2*MB);
  300. MAKE_MD(EFI_CONVENTIONAL_MEMORY, EFI_MEMORY_WB, 2*MB, 130*MB);
  301. MAKE_MD(EFI_CONVENTIONAL_MEMORY, EFI_MEMORY_WB, 4096*MB, 4128*MB);
  302. #else
  303. MAKE_MD( 4, 0x9, 0x0000000000000000, 0x0000000000001000);
  304. MAKE_MD( 7, 0x9, 0x0000000000001000, 0x000000000008a000);
  305. MAKE_MD( 4, 0x9, 0x000000000008a000, 0x00000000000a0000);
  306. MAKE_MD( 5, 0x8000000000000009, 0x00000000000c0000, 0x0000000000100000);
  307. MAKE_MD( 7, 0x9, 0x0000000000100000, 0x0000000004400000);
  308. MAKE_MD( 2, 0x9, 0x0000000004400000, 0x0000000004be5000);
  309. MAKE_MD( 7, 0x9, 0x0000000004be5000, 0x000000007f77e000);
  310. MAKE_MD( 6, 0x8000000000000009, 0x000000007f77e000, 0x000000007fb94000);
  311. MAKE_MD( 6, 0x8000000000000009, 0x000000007fb94000, 0x000000007fb95000);
  312. MAKE_MD( 6, 0x8000000000000009, 0x000000007fb95000, 0x000000007fc00000);
  313. MAKE_MD(13, 0x8000000000000009, 0x000000007fc00000, 0x000000007fc3a000);
  314. MAKE_MD( 7, 0x9, 0x000000007fc3a000, 0x000000007fea0000);
  315. MAKE_MD( 5, 0x8000000000000009, 0x000000007fea0000, 0x000000007fea8000);
  316. MAKE_MD( 7, 0x9, 0x000000007fea8000, 0x000000007feab000);
  317. MAKE_MD( 5, 0x8000000000000009, 0x000000007feab000, 0x000000007ffff000);
  318. MAKE_MD( 7, 0x9, 0x00000000ff400000, 0x0000000104000000);
  319. #endif
  320. bp->efi_systab = __pa(&fw_mem);
  321. bp->efi_memmap = __pa(efi_memmap);
  322. bp->efi_memmap_size = NUM_MEM_DESCS*sizeof(efi_memory_desc_t);
  323. bp->efi_memdesc_size = sizeof(efi_memory_desc_t);
  324. bp->efi_memdesc_version = 1;
  325. bp->command_line = __pa(cmd_line);
  326. bp->console_info.num_cols = 80;
  327. bp->console_info.num_rows = 25;
  328. bp->console_info.orig_x = 0;
  329. bp->console_info.orig_y = 24;
  330. bp->fpswa = 0;
  331. return bp;
  332. }