fw-emu.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /*
  2. * PAL & SAL emulation.
  3. *
  4. * Copyright (C) 1998-2001 Hewlett-Packard Co
  5. * David Mosberger-Tang <davidm@hpl.hp.com>
  6. */
  7. #ifdef CONFIG_PCI
  8. # include <linux/pci.h>
  9. #endif
  10. #include <linux/efi.h>
  11. #include <asm/io.h>
  12. #include <asm/pal.h>
  13. #include <asm/sal.h>
  14. #include <asm/setup.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. if (in1 == SAL_FREQ_BASE_PLATFORM)
  140. r9 = 200000000;
  141. else if (in1 == SAL_FREQ_BASE_INTERVAL_TIMER) {
  142. /*
  143. * Is this supposed to be the cr.itc frequency
  144. * or something platform specific? The SAL
  145. * doc ain't exactly clear on this...
  146. */
  147. r9 = 700000000;
  148. } else if (in1 == SAL_FREQ_BASE_REALTIME_CLOCK)
  149. r9 = 1;
  150. else
  151. status = -1;
  152. } else if (index == SAL_SET_VECTORS) {
  153. ;
  154. } else if (index == SAL_GET_STATE_INFO) {
  155. ;
  156. } else if (index == SAL_GET_STATE_INFO_SIZE) {
  157. ;
  158. } else if (index == SAL_CLEAR_STATE_INFO) {
  159. ;
  160. } else if (index == SAL_MC_RENDEZ) {
  161. ;
  162. } else if (index == SAL_MC_SET_PARAMS) {
  163. ;
  164. } else if (index == SAL_CACHE_FLUSH) {
  165. ;
  166. } else if (index == SAL_CACHE_INIT) {
  167. ;
  168. #ifdef CONFIG_PCI
  169. } else if (index == SAL_PCI_CONFIG_READ) {
  170. /*
  171. * in1 contains the PCI configuration address and in2
  172. * the size of the read. The value that is read is
  173. * returned via the general register r9.
  174. */
  175. outl(BUILD_CMD(in1), 0xCF8);
  176. if (in2 == 1) /* Reading byte */
  177. r9 = inb(0xCFC + ((REG_OFFSET(in1) & 3)));
  178. else if (in2 == 2) /* Reading word */
  179. r9 = inw(0xCFC + ((REG_OFFSET(in1) & 2)));
  180. else /* Reading dword */
  181. r9 = inl(0xCFC);
  182. status = PCIBIOS_SUCCESSFUL;
  183. } else if (index == SAL_PCI_CONFIG_WRITE) {
  184. /*
  185. * in1 contains the PCI configuration address, in2 the
  186. * size of the write, and in3 the actual value to be
  187. * written out.
  188. */
  189. outl(BUILD_CMD(in1), 0xCF8);
  190. if (in2 == 1) /* Writing byte */
  191. outb(in3, 0xCFC + ((REG_OFFSET(in1) & 3)));
  192. else if (in2 == 2) /* Writing word */
  193. outw(in3, 0xCFC + ((REG_OFFSET(in1) & 2)));
  194. else /* Writing dword */
  195. outl(in3, 0xCFC);
  196. status = PCIBIOS_SUCCESSFUL;
  197. #endif /* CONFIG_PCI */
  198. } else if (index == SAL_UPDATE_PAL) {
  199. ;
  200. } else {
  201. status = -1;
  202. }
  203. return ((struct sal_ret_values) {status, r9, r10, r11});
  204. }
  205. struct ia64_boot_param *
  206. sys_fw_init (const char *args, int arglen)
  207. {
  208. efi_system_table_t *efi_systab;
  209. efi_runtime_services_t *efi_runtime;
  210. efi_config_table_t *efi_tables;
  211. struct ia64_sal_systab *sal_systab;
  212. efi_memory_desc_t *efi_memmap, *md;
  213. unsigned long *pal_desc, *sal_desc;
  214. struct ia64_sal_desc_entry_point *sal_ed;
  215. struct ia64_boot_param *bp;
  216. unsigned char checksum = 0;
  217. char *cp, *cmd_line;
  218. int i = 0;
  219. # define MAKE_MD(typ, attr, start, end) \
  220. do { \
  221. md = efi_memmap + i++; \
  222. md->type = typ; \
  223. md->pad = 0; \
  224. md->phys_addr = start; \
  225. md->virt_addr = 0; \
  226. md->num_pages = (end - start) >> 12; \
  227. md->attribute = attr; \
  228. } while (0)
  229. memset(fw_mem, 0, sizeof(fw_mem));
  230. pal_desc = (unsigned long *) &pal_emulator_static;
  231. sal_desc = (unsigned long *) &sal_emulator;
  232. cp = fw_mem;
  233. efi_systab = (void *) cp; cp += sizeof(*efi_systab);
  234. efi_runtime = (void *) cp; cp += sizeof(*efi_runtime);
  235. efi_tables = (void *) cp; cp += sizeof(*efi_tables);
  236. sal_systab = (void *) cp; cp += sizeof(*sal_systab);
  237. sal_ed = (void *) cp; cp += sizeof(*sal_ed);
  238. efi_memmap = (void *) cp; cp += NUM_MEM_DESCS*sizeof(*efi_memmap);
  239. bp = (void *) cp; cp += sizeof(*bp);
  240. cmd_line = (void *) cp;
  241. if (args) {
  242. if (arglen >= 1024)
  243. arglen = 1023;
  244. memcpy(cmd_line, args, arglen);
  245. } else {
  246. arglen = 0;
  247. }
  248. cmd_line[arglen] = '\0';
  249. memset(efi_systab, 0, sizeof(*efi_systab));
  250. efi_systab->hdr.signature = EFI_SYSTEM_TABLE_SIGNATURE;
  251. efi_systab->hdr.revision = ((1 << 16) | 00);
  252. efi_systab->hdr.headersize = sizeof(efi_systab->hdr);
  253. efi_systab->fw_vendor = __pa("H\0e\0w\0l\0e\0t\0t\0-\0P\0a\0c\0k\0a\0r\0d\0\0");
  254. efi_systab->fw_revision = 1;
  255. efi_systab->runtime = (void *) __pa(efi_runtime);
  256. efi_systab->nr_tables = 1;
  257. efi_systab->tables = __pa(efi_tables);
  258. efi_runtime->hdr.signature = EFI_RUNTIME_SERVICES_SIGNATURE;
  259. efi_runtime->hdr.revision = EFI_RUNTIME_SERVICES_REVISION;
  260. efi_runtime->hdr.headersize = sizeof(efi_runtime->hdr);
  261. efi_runtime->get_time = (void *)__pa(&fw_efi_get_time);
  262. efi_runtime->set_time = (void *)__pa(&efi_unimplemented);
  263. efi_runtime->get_wakeup_time = (void *)__pa(&efi_unimplemented);
  264. efi_runtime->set_wakeup_time = (void *)__pa(&efi_unimplemented);
  265. efi_runtime->set_virtual_address_map = (void *)__pa(&efi_unimplemented);
  266. efi_runtime->get_variable = (void *)__pa(&efi_unimplemented);
  267. efi_runtime->get_next_variable = (void *)__pa(&efi_unimplemented);
  268. efi_runtime->set_variable = (void *)__pa(&efi_unimplemented);
  269. efi_runtime->get_next_high_mono_count = (void *)__pa(&efi_unimplemented);
  270. efi_runtime->reset_system = (void *)__pa(&efi_reset_system);
  271. efi_tables->guid = SAL_SYSTEM_TABLE_GUID;
  272. efi_tables->table = __pa(sal_systab);
  273. /* fill in the SAL system table: */
  274. memcpy(sal_systab->signature, "SST_", 4);
  275. sal_systab->size = sizeof(*sal_systab);
  276. sal_systab->sal_rev_minor = 1;
  277. sal_systab->sal_rev_major = 0;
  278. sal_systab->entry_count = 1;
  279. #ifdef CONFIG_IA64_GENERIC
  280. strcpy(sal_systab->oem_id, "Generic");
  281. strcpy(sal_systab->product_id, "IA-64 system");
  282. #endif
  283. #ifdef CONFIG_IA64_HP_SIM
  284. strcpy(sal_systab->oem_id, "Hewlett-Packard");
  285. strcpy(sal_systab->product_id, "HP-simulator");
  286. #endif
  287. /* fill in an entry point: */
  288. sal_ed->type = SAL_DESC_ENTRY_POINT;
  289. sal_ed->pal_proc = __pa(pal_desc[0]);
  290. sal_ed->sal_proc = __pa(sal_desc[0]);
  291. sal_ed->gp = __pa(sal_desc[1]);
  292. for (cp = (char *) sal_systab; cp < (char *) efi_memmap; ++cp)
  293. checksum += *cp;
  294. sal_systab->checksum = -checksum;
  295. #if SIMPLE_MEMMAP
  296. /* simulate free memory at physical address zero */
  297. MAKE_MD(EFI_BOOT_SERVICES_DATA, EFI_MEMORY_WB, 0*MB, 1*MB);
  298. MAKE_MD(EFI_PAL_CODE, EFI_MEMORY_WB, 1*MB, 2*MB);
  299. MAKE_MD(EFI_CONVENTIONAL_MEMORY, EFI_MEMORY_WB, 2*MB, 130*MB);
  300. MAKE_MD(EFI_CONVENTIONAL_MEMORY, EFI_MEMORY_WB, 4096*MB, 4128*MB);
  301. #else
  302. MAKE_MD( 4, 0x9, 0x0000000000000000, 0x0000000000001000);
  303. MAKE_MD( 7, 0x9, 0x0000000000001000, 0x000000000008a000);
  304. MAKE_MD( 4, 0x9, 0x000000000008a000, 0x00000000000a0000);
  305. MAKE_MD( 5, 0x8000000000000009, 0x00000000000c0000, 0x0000000000100000);
  306. MAKE_MD( 7, 0x9, 0x0000000000100000, 0x0000000004400000);
  307. MAKE_MD( 2, 0x9, 0x0000000004400000, 0x0000000004be5000);
  308. MAKE_MD( 7, 0x9, 0x0000000004be5000, 0x000000007f77e000);
  309. MAKE_MD( 6, 0x8000000000000009, 0x000000007f77e000, 0x000000007fb94000);
  310. MAKE_MD( 6, 0x8000000000000009, 0x000000007fb94000, 0x000000007fb95000);
  311. MAKE_MD( 6, 0x8000000000000009, 0x000000007fb95000, 0x000000007fc00000);
  312. MAKE_MD(13, 0x8000000000000009, 0x000000007fc00000, 0x000000007fc3a000);
  313. MAKE_MD( 7, 0x9, 0x000000007fc3a000, 0x000000007fea0000);
  314. MAKE_MD( 5, 0x8000000000000009, 0x000000007fea0000, 0x000000007fea8000);
  315. MAKE_MD( 7, 0x9, 0x000000007fea8000, 0x000000007feab000);
  316. MAKE_MD( 5, 0x8000000000000009, 0x000000007feab000, 0x000000007ffff000);
  317. MAKE_MD( 7, 0x9, 0x00000000ff400000, 0x0000000104000000);
  318. #endif
  319. bp->efi_systab = __pa(&fw_mem);
  320. bp->efi_memmap = __pa(efi_memmap);
  321. bp->efi_memmap_size = NUM_MEM_DESCS*sizeof(efi_memory_desc_t);
  322. bp->efi_memdesc_size = sizeof(efi_memory_desc_t);
  323. bp->efi_memdesc_version = 1;
  324. bp->command_line = __pa(cmd_line);
  325. bp->console_info.num_cols = 80;
  326. bp->console_info.num_rows = 25;
  327. bp->console_info.orig_x = 0;
  328. bp->console_info.orig_y = 24;
  329. bp->fpswa = 0;
  330. return bp;
  331. }