fw-emu.c 12 KB

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