fw-emu.c 11 KB

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