1/* 2 * PAL & SAL emulation. 3 * 4 * Copyright (C) 1998-2001 Hewlett-Packard Co 5 * Copyright (C) 1998-2001 David Mosberger-Tang <davidm@hpl.hp.com> 6 * 7 * For the HP simulator, this file gets include in boot/bootloader.c. 8 * For SoftSDV, this file gets included in sys_softsdv.c. 9 */ 10#include <linux/config.h> 11 12#ifdef CONFIG_PCI 13# include <linux/pci.h> 14#endif 15 16#include <linux/efi.h> 17#include <asm/io.h> 18#include <asm/pal.h> 19#include <asm/sal.h> 20 21#define MB (1024*1024UL) 22#define GB (1024*MB) 23#define TB (1024*GB) 24 25#define NUM_MEM_DESCS 4 26 27static char fw_mem[( sizeof(struct ia64_boot_param) 28 + sizeof(efi_system_table_t) 29 + sizeof(efi_runtime_services_t) 30 + 1*sizeof(efi_config_table_t) 31 + sizeof(struct ia64_sal_systab) 32 + sizeof(struct ia64_sal_desc_entry_point) 33 + NUM_MEM_DESCS*(sizeof(efi_memory_desc_t)) 34 + 1024)] __attribute__ ((aligned (8))); 35 36#ifdef CONFIG_IA64_HP_SIM 37 38/* Simulator system calls: */ 39 40#define SSC_EXIT 66 41 42/* 43 * Simulator system call. 44 */ 45static long 46ssc (long arg0, long arg1, long arg2, long arg3, int nr) 47{ 48 register long r8 asm ("r8"); 49 50 asm volatile ("mov r15=%1\n\t" 51 "break 0x80001" 52 : "=r"(r8) 53 : "r"(nr), "r"(arg0), "r"(arg1), "r"(arg2), "r"(arg3)); 54 return r8; 55} 56 57#define SECS_PER_HOUR (60 * 60) 58#define SECS_PER_DAY (SECS_PER_HOUR * 24) 59 60/* Compute the `struct tm' representation of *T, 61 offset OFFSET seconds east of UTC, 62 and store year, yday, mon, mday, wday, hour, min, sec into *TP. 63 Return nonzero if successful. */ 64int 65offtime (unsigned long t, efi_time_t *tp) 66{ 67 const unsigned short int __mon_yday[2][13] = 68 { 69 /* Normal years. */ 70 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 }, 71 /* Leap years. */ 72 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 } 73 }; 74 long int days, rem, y; 75 const unsigned short int *ip; 76 77 days = t / SECS_PER_DAY; 78 rem = t % SECS_PER_DAY; 79 while (rem < 0) { 80 rem += SECS_PER_DAY; 81 --days; 82 } 83 while (rem >= SECS_PER_DAY) { 84 rem -= SECS_PER_DAY; 85 ++days; 86 } 87 tp->hour = rem / SECS_PER_HOUR; 88 rem %= SECS_PER_HOUR; 89 tp->minute = rem / 60; 90 tp->second = rem % 60; 91 /* January 1, 1970 was a Thursday. */ 92 y = 1970; 93 94# define DIV(a, b) ((a) / (b) - ((a) % (b) < 0)) 95# define LEAPS_THRU_END_OF(y) (DIV (y, 4) - DIV (y, 100) + DIV (y, 400)) 96# define __isleap(year) \ 97 ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0)) 98 99 while (days < 0 || days >= (__isleap (y) ? 366 : 365)) { 100 /* Guess a corrected year, assuming 365 days per year. */ 101 long int yg = y + days / 365 - (days % 365 < 0); 102 103 /* Adjust DAYS and Y to match the guessed year. */ 104 days -= ((yg - y) * 365 + LEAPS_THRU_END_OF (yg - 1) 105 - LEAPS_THRU_END_OF (y - 1)); 106 y = yg; 107 } 108 tp->year = y; 109 ip = __mon_yday[__isleap(y)]; 110 for (y = 11; days < (long int) ip[y]; --y) 111 continue; 112 days -= ip[y]; 113 tp->month = y + 1; 114 tp->day = days + 1; 115 return 1; 116} 117 118#endif /* CONFIG_IA64_HP_SIM */ 119 120/* 121 * Very ugly, but we need this in the simulator only. Once we run on 122 * real hw, this can all go away. 123 */ 124extern void pal_emulator_static (void); 125 126asm ( 127" .proc pal_emulator_static\n" 128"pal_emulator_static:" 129" mov r8=-1\n" 130" mov r9=256\n" 131" ;;\n" 132" cmp.gtu p6,p7=r9,r28 /* r28 <= 255? */\n" 133"(p6) br.cond.sptk.few static\n" 134" ;;\n" 135" mov r9=512\n" 136" ;;\n" 137" cmp.gtu p6,p7=r9,r28\n" 138"(p6) br.cond.sptk.few stacked\n" 139" ;;\n" 140"static: cmp.eq p6,p7=6,r28 /* PAL_PTCE_INFO */\n" 141"(p7) br.cond.sptk.few 1f\n" 142" ;;\n" 143" mov r8=0 /* status = 0 */\n" 144" movl r9=0x100000000 /* tc.base */\n" 145" movl r10=0x0000000200000003 /* count[0], count[1] */\n" 146" movl r11=0x1000000000002000 /* stride[0], stride[1] */\n" 147" br.cond.sptk.few rp\n" 148"1: cmp.eq p6,p7=14,r28 /* PAL_FREQ_RATIOS */\n" 149"(p7) br.cond.sptk.few 1f\n" 150" mov r8=0 /* status = 0 */\n" 151" movl r9 =0x100000064 /* proc_ratio (1/100) */\n" 152" movl r10=0x100000100 /* bus_ratio<<32 (1/256) */\n" 153" movl r11=0x100000064 /* itc_ratio<<32 (1/100) */\n" 154" ;;\n" 155"1: cmp.eq p6,p7=19,r28 /* PAL_RSE_INFO */\n" 156"(p7) br.cond.sptk.few 1f\n" 157" mov r8=0 /* status = 0 */\n" 158" mov r9=96 /* num phys stacked */\n" 159" mov r10=0 /* hints */\n" 160" mov r11=0\n" 161" br.cond.sptk.few rp\n" 162"1: cmp.eq p6,p7=1,r28 /* PAL_CACHE_FLUSH */\n" 163"(p7) br.cond.sptk.few 1f\n" 164" mov r9=ar.lc\n" 165" movl r8=524288 /* flush 512k million cache lines (16MB) */\n" 166" ;;\n" 167" mov ar.lc=r8\n" 168" movl r8=0xe000000000000000\n" 169" ;;\n" 170".loop: fc r8\n" 171" add r8=32,r8\n" 172" br.cloop.sptk.few .loop\n" 173" sync.i\n" 174" ;;\n" 175" srlz.i\n" 176" ;;\n" 177" mov ar.lc=r9\n" 178" mov r8=r0\n" 179" ;;\n" 180"1: cmp.eq p6,p7=15,r28 /* PAL_PERF_MON_INFO */\n" 181"(p7) br.cond.sptk.few 1f\n" 182" mov r8=0 /* status = 0 */\n" 183" movl r9 =0x12082004 /* generic=4 width=32 retired=8 cycles=18 */\n" 184" mov r10=0 /* reserved */\n" 185" mov r11=0 /* reserved */\n" 186" mov r16=0xffff /* implemented PMC */\n" 187" mov r17=0xffff /* implemented PMD */\n" 188" add r18=8,r29 /* second index */\n" 189" ;;\n" 190" st8 [r29]=r16,16 /* store implemented PMC */\n" 191" st8 [r18]=r0,16 /* clear remaining bits */\n" 192" ;;\n" 193" st8 [r29]=r0,16 /* store implemented PMC */\n" 194" st8 [r18]=r0,16 /* clear remaining bits */\n" 195" ;;\n" 196" st8 [r29]=r17,16 /* store implemented PMD */\n" 197" st8 [r18]=r0,16 /* clear remaining bits */\n" 198" mov r16=0xf0 /* cycles count capable PMC */\n" 199" ;;\n" 200" st8 [r29]=r0,16 /* store implemented PMC */\n" 201" st8 [r18]=r0,16 /* clear remaining bits */\n" 202" mov r17=0x10 /* retired bundles capable PMC */\n" 203" ;;\n" 204" st8 [r29]=r16,16 /* store cycles capable */\n" 205" st8 [r18]=r0,16 /* clear remaining bits */\n" 206" ;;\n" 207" st8 [r29]=r0,16 /* store implemented PMC */\n" 208" st8 [r18]=r0,16 /* clear remaining bits */\n" 209" ;;\n" 210" st8 [r29]=r17,16 /* store retired bundle capable */\n" 211" st8 [r18]=r0,16 /* clear remaining bits */\n" 212" ;;\n" 213" st8 [r29]=r0,16 /* store implemented PMC */\n" 214" st8 [r18]=r0,16 /* clear remaining bits */\n" 215" ;;\n" 216"1: br.cond.sptk.few rp\n" 217"stacked:\n" 218" br.ret.sptk.few rp\n" 219" .endp pal_emulator_static\n"); 220 221/* Macro to emulate SAL call using legacy IN and OUT calls to CF8, CFC etc.. */ 222 223#define BUILD_CMD(addr) ((0x80000000 | (addr)) & ~3) 224 225#define REG_OFFSET(addr) (0x00000000000000FF & (addr)) 226#define DEVICE_FUNCTION(addr) (0x000000000000FF00 & (addr)) 227#define BUS_NUMBER(addr) (0x0000000000FF0000 & (addr)) 228 229static efi_status_t 230efi_get_time (efi_time_t *tm, efi_time_cap_t *tc) 231{ 232#ifdef CONFIG_IA64_HP_SIM 233 struct { 234 int tv_sec; /* must be 32bits to work */ 235 int tv_usec; 236 } tv32bits; 237 238 ssc((unsigned long) &tv32bits, 0, 0, 0, SSC_GET_TOD); 239 240 memset(tm, 0, sizeof(*tm)); 241 offtime(tv32bits.tv_sec, tm); 242 243 if (tc) 244 memset(tc, 0, sizeof(*tc)); 245#else 246# error Not implemented yet... 247#endif 248 return EFI_SUCCESS; 249} 250 251static void 252efi_reset_system (int reset_type, efi_status_t status, unsigned long data_size, efi_char16_t *data) 253{ 254#ifdef CONFIG_IA64_HP_SIM 255 ssc(status, 0, 0, 0, SSC_EXIT); 256#else 257# error Not implemented yet... 258#endif 259} 260 261static efi_status_t 262efi_unimplemented (void) 263{ 264 return EFI_UNSUPPORTED; 265} 266 267static long 268sal_emulator (long index, unsigned long in1, unsigned long in2, 269 unsigned long in3, unsigned long in4, unsigned long in5, 270 unsigned long in6, unsigned long in7) 271{ 272 register long r9 asm ("r9") = 0; 273 register long r10 asm ("r10") = 0; 274 register long r11 asm ("r11") = 0; 275 long status; 276 277 /* 278 * Don't do a "switch" here since that gives us code that 279 * isn't self-relocatable. 280 */ 281 status = 0; 282 if (index == SAL_FREQ_BASE) { 283 switch (in1) { 284 case SAL_FREQ_BASE_PLATFORM: 285 r9 = 200000000; 286 break; 287 288 case SAL_FREQ_BASE_INTERVAL_TIMER: 289 /* 290 * Is this supposed to be the cr.itc frequency 291 * or something platform specific? The SAL 292 * doc ain't exactly clear on this... 293 */ 294 r9 = 700000000; 295 break; 296 297 case SAL_FREQ_BASE_REALTIME_CLOCK: 298 r9 = 1; 299 break; 300 301 default: 302 status = -1; 303 break; 304 } 305 } else if (index == SAL_SET_VECTORS) { 306 ; 307 } else if (index == SAL_GET_STATE_INFO) { 308 ; 309 } else if (index == SAL_GET_STATE_INFO_SIZE) { 310 ; 311 } else if (index == SAL_CLEAR_STATE_INFO) { 312 ; 313 } else if (index == SAL_MC_RENDEZ) { 314 ; 315 } else if (index == SAL_MC_SET_PARAMS) { 316 ; 317 } else if (index == SAL_CACHE_FLUSH) { 318 ; 319 } else if (index == SAL_CACHE_INIT) { 320 ; 321#ifdef CONFIG_PCI 322 } else if (index == SAL_PCI_CONFIG_READ) { 323 /* 324 * in1 contains the PCI configuration address and in2 325 * the size of the read. The value that is read is 326 * returned via the general register r9. 327 */ 328 outl(BUILD_CMD(in1), 0xCF8); 329 if (in2 == 1) /* Reading byte */ 330 r9 = inb(0xCFC + ((REG_OFFSET(in1) & 3))); 331 else if (in2 == 2) /* Reading word */ 332 r9 = inw(0xCFC + ((REG_OFFSET(in1) & 2))); 333 else /* Reading dword */ 334 r9 = inl(0xCFC); 335 status = PCIBIOS_SUCCESSFUL; 336 } else if (index == SAL_PCI_CONFIG_WRITE) { 337 /* 338 * in1 contains the PCI configuration address, in2 the 339 * size of the write, and in3 the actual value to be 340 * written out. 341 */ 342 outl(BUILD_CMD(in1), 0xCF8); 343 if (in2 == 1) /* Writing byte */ 344 outb(in3, 0xCFC + ((REG_OFFSET(in1) & 3))); 345 else if (in2 == 2) /* Writing word */ 346 outw(in3, 0xCFC + ((REG_OFFSET(in1) & 2))); 347 else /* Writing dword */ 348 outl(in3, 0xCFC); 349 status = PCIBIOS_SUCCESSFUL; 350#endif /* CONFIG_PCI */ 351 } else if (index == SAL_UPDATE_PAL) { 352 ; 353 } else { 354 status = -1; 355 } 356 asm volatile ("" :: "r"(r9), "r"(r10), "r"(r11)); 357 return status; 358} 359 360 361/* 362 * This is here to work around a bug in egcs-1.1.1b that causes the 363 * compiler to crash (seems like a bug in the new alias analysis code. 364 */ 365void * 366id (long addr) 367{ 368 return (void *) addr; 369} 370 371struct ia64_boot_param * 372sys_fw_init (const char *args, int arglen) 373{ 374 efi_system_table_t *efi_systab; 375 efi_runtime_services_t *efi_runtime; 376 efi_config_table_t *efi_tables; 377 struct ia64_sal_systab *sal_systab; 378 efi_memory_desc_t *efi_memmap, *md; 379 unsigned long *pal_desc, *sal_desc; 380 struct ia64_sal_desc_entry_point *sal_ed; 381 struct ia64_boot_param *bp; 382 unsigned char checksum = 0; 383 char *cp, *cmd_line; 384 385 memset(fw_mem, 0, sizeof(fw_mem)); 386 387 pal_desc = (unsigned long *) &pal_emulator_static; 388 sal_desc = (unsigned long *) &sal_emulator; 389 390 cp = fw_mem; 391 efi_systab = (void *) cp; cp += sizeof(*efi_systab); 392 efi_runtime = (void *) cp; cp += sizeof(*efi_runtime); 393 efi_tables = (void *) cp; cp += sizeof(*efi_tables); 394 sal_systab = (void *) cp; cp += sizeof(*sal_systab); 395 sal_ed = (void *) cp; cp += sizeof(*sal_ed); 396 efi_memmap = (void *) cp; cp += NUM_MEM_DESCS*sizeof(*efi_memmap); 397 bp = (void *) cp; cp += sizeof(*bp); 398 cmd_line = (void *) cp; 399 400 if (args) { 401 if (arglen >= 1024) 402 arglen = 1023; 403 memcpy(cmd_line, args, arglen); 404 } else { 405 arglen = 0; 406 } 407 cmd_line[arglen] = '\0'; 408 409 memset(efi_systab, 0, sizeof(efi_systab)); 410 efi_systab->hdr.signature = EFI_SYSTEM_TABLE_SIGNATURE; 411 efi_systab->hdr.revision = EFI_SYSTEM_TABLE_REVISION; 412 efi_systab->hdr.headersize = sizeof(efi_systab->hdr); 413 efi_systab->fw_vendor = __pa("H\0e\0w\0l\0e\0t\0t\0-\0P\0a\0c\0k\0a\0r\0d\0\0"); 414 efi_systab->fw_revision = 1; 415 efi_systab->runtime = __pa(efi_runtime); 416 efi_systab->nr_tables = 1; 417 efi_systab->tables = __pa(efi_tables); 418 419 efi_runtime->hdr.signature = EFI_RUNTIME_SERVICES_SIGNATURE; 420 efi_runtime->hdr.revision = EFI_RUNTIME_SERVICES_REVISION; 421 efi_runtime->hdr.headersize = sizeof(efi_runtime->hdr); 422 efi_runtime->get_time = __pa(&efi_get_time); 423 efi_runtime->set_time = __pa(&efi_unimplemented); 424 efi_runtime->get_wakeup_time = __pa(&efi_unimplemented); 425 efi_runtime->set_wakeup_time = __pa(&efi_unimplemented); 426 efi_runtime->set_virtual_address_map = __pa(&efi_unimplemented); 427 efi_runtime->get_variable = __pa(&efi_unimplemented); 428 efi_runtime->get_next_variable = __pa(&efi_unimplemented); 429 efi_runtime->set_variable = __pa(&efi_unimplemented); 430 efi_runtime->get_next_high_mono_count = __pa(&efi_unimplemented); 431 efi_runtime->reset_system = __pa(&efi_reset_system); 432 433 efi_tables->guid = SAL_SYSTEM_TABLE_GUID; 434 efi_tables->table = __pa(sal_systab); 435 436 /* fill in the SAL system table: */ 437 memcpy(sal_systab->signature, "SST_", 4); 438 sal_systab->size = sizeof(*sal_systab); 439 sal_systab->sal_rev_minor = 1; 440 sal_systab->sal_rev_major = 0; 441 sal_systab->entry_count = 1; 442 443#ifdef CONFIG_IA64_GENERIC 444 strcpy(sal_systab->oem_id, "Generic"); 445 strcpy(sal_systab->product_id, "IA-64 system"); 446#endif 447 448#ifdef CONFIG_IA64_HP_SIM 449 strcpy(sal_systab->oem_id, "Hewlett-Packard"); 450 strcpy(sal_systab->product_id, "HP-simulator"); 451#endif 452 453#ifdef CONFIG_IA64_SDV 454 strcpy(sal_systab->oem_id, "Intel"); 455 strcpy(sal_systab->product_id, "SDV"); 456#endif 457 458 /* fill in an entry point: */ 459 sal_ed->type = SAL_DESC_ENTRY_POINT; 460 sal_ed->pal_proc = __pa(pal_desc[0]); 461 sal_ed->sal_proc = __pa(sal_desc[0]); 462 sal_ed->gp = __pa(sal_desc[1]); 463 464 for (cp = (char *) sal_systab; cp < (char *) efi_memmap; ++cp) 465 checksum += *cp; 466 467 sal_systab->checksum = -checksum; 468 469 /* simulate free memory at physical address zero */ 470 md = &efi_memmap[0]; 471 md->type = EFI_BOOT_SERVICES_DATA; 472 md->pad = 0; 473 md->phys_addr = 0*MB; 474 md->virt_addr = 0; 475 md->num_pages = (1*MB) >> 12; /* 1MB (in 4KB pages) */ 476 md->attribute = EFI_MEMORY_WB | EFI_MEMORY_WC | EFI_MEMORY_UC; 477 478 /* fill in a memory descriptor: */ 479 md = &efi_memmap[1]; 480 md->type = EFI_CONVENTIONAL_MEMORY; 481 md->pad = 0; 482 md->phys_addr = 2*MB; 483 md->virt_addr = 0; 484 md->num_pages = (128*MB) >> 12; /* 128MB (in 4KB pages) */ 485 md->attribute = EFI_MEMORY_WB; 486 487 /* descriptor for firmware emulator: */ 488 md = &efi_memmap[2]; 489 md->type = EFI_PAL_CODE; 490 md->pad = 0; 491 md->phys_addr = 1*MB; 492 md->virt_addr = 1*MB; 493 md->num_pages = (1*MB) >> 12; /* 1MB (in 4KB pages) */ 494 md->attribute = EFI_MEMORY_WB | EFI_MEMORY_WC | EFI_MEMORY_UC; 495 496 /* descriptor for high memory (>4TB): */ 497 md = &efi_memmap[3]; 498 md->type = EFI_CONVENTIONAL_MEMORY; 499 md->pad = 0; 500 md->phys_addr = 4*TB; 501 md->virt_addr = 0; 502 md->num_pages = (64*MB) >> 12; /* 64MB (in 4KB pages) */ 503 md->attribute = EFI_MEMORY_WB; 504 505 bp->efi_systab = __pa(&fw_mem); 506 bp->efi_memmap = __pa(efi_memmap); 507 bp->efi_memmap_size = NUM_MEM_DESCS*sizeof(efi_memory_desc_t); 508 bp->efi_memdesc_size = sizeof(efi_memory_desc_t); 509 bp->efi_memdesc_version = 1; 510 bp->command_line = __pa(cmd_line); 511 bp->console_info.num_cols = 80; 512 bp->console_info.num_rows = 25; 513 bp->console_info.orig_x = 0; 514 bp->console_info.orig_y = 24; 515 bp->fpswa = 0; 516 517 return bp; 518} 519