FreeBSD also has clock_gettime
[qemu/qemu-JZ.git] / hw / sun4u.c
bloba70ad201ab7a7fe2967d707e43295a624e0780ce
1 /*
2 * QEMU Sun4u/Sun4v System Emulator
4 * Copyright (c) 2005 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
24 #include "hw.h"
25 #include "pci.h"
26 #include "pc.h"
27 #include "nvram.h"
28 #include "fdc.h"
29 #include "net.h"
30 #include "qemu-timer.h"
31 #include "sysemu.h"
32 #include "boards.h"
33 #include "firmware_abi.h"
34 #include "fw_cfg.h"
36 //#define DEBUG_IRQ
38 #ifdef DEBUG_IRQ
39 #define DPRINTF(fmt, args...) \
40 do { printf("CPUIRQ: " fmt , ##args); } while (0)
41 #else
42 #define DPRINTF(fmt, args...)
43 #endif
45 #define KERNEL_LOAD_ADDR 0x00404000
46 #define CMDLINE_ADDR 0x003ff000
47 #define INITRD_LOAD_ADDR 0x00300000
48 #define PROM_SIZE_MAX (4 * 1024 * 1024)
49 #define PROM_VADDR 0x000ffd00000ULL
50 #define APB_SPECIAL_BASE 0x1fe00000000ULL
51 #define APB_MEM_BASE 0x1ff00000000ULL
52 #define VGA_BASE (APB_MEM_BASE + 0x400000ULL)
53 #define PROM_FILENAME "openbios-sparc64"
54 #define NVRAM_SIZE 0x2000
55 #define MAX_IDE_BUS 2
56 #define BIOS_CFG_IOPORT 0x510
58 #define MAX_PILS 16
60 struct hwdef {
61 const char * const default_cpu_model;
62 uint16_t machine_id;
63 uint64_t prom_addr;
64 uint64_t console_serial_base;
67 int DMA_get_channel_mode (int nchan)
69 return 0;
71 int DMA_read_memory (int nchan, void *buf, int pos, int size)
73 return 0;
75 int DMA_write_memory (int nchan, void *buf, int pos, int size)
77 return 0;
79 void DMA_hold_DREQ (int nchan) {}
80 void DMA_release_DREQ (int nchan) {}
81 void DMA_schedule(int nchan) {}
82 void DMA_run (void) {}
83 void DMA_init (int high_page_enable) {}
84 void DMA_register_channel (int nchan,
85 DMA_transfer_handler transfer_handler,
86 void *opaque)
90 static int nvram_boot_set(void *opaque, const char *boot_device)
92 unsigned int i;
93 uint8_t image[sizeof(ohwcfg_v3_t)];
94 ohwcfg_v3_t *header = (ohwcfg_v3_t *)ℑ
95 m48t59_t *nvram = (m48t59_t *)opaque;
97 for (i = 0; i < sizeof(image); i++)
98 image[i] = m48t59_read(nvram, i) & 0xff;
100 pstrcpy((char *)header->boot_devices, sizeof(header->boot_devices),
101 boot_device);
102 header->nboot_devices = strlen(boot_device) & 0xff;
103 header->crc = cpu_to_be16(OHW_compute_crc(header, 0x00, 0xF8));
105 for (i = 0; i < sizeof(image); i++)
106 m48t59_write(nvram, i, image[i]);
108 return 0;
111 static int sun4u_NVRAM_set_params (m48t59_t *nvram, uint16_t NVRAM_size,
112 const char *arch,
113 ram_addr_t RAM_size,
114 const char *boot_devices,
115 uint32_t kernel_image, uint32_t kernel_size,
116 const char *cmdline,
117 uint32_t initrd_image, uint32_t initrd_size,
118 uint32_t NVRAM_image,
119 int width, int height, int depth,
120 const uint8_t *macaddr)
122 unsigned int i;
123 uint32_t start, end;
124 uint8_t image[0x1ff0];
125 ohwcfg_v3_t *header = (ohwcfg_v3_t *)&image;
126 struct sparc_arch_cfg *sparc_header;
127 struct OpenBIOS_nvpart_v1 *part_header;
129 memset(image, '\0', sizeof(image));
131 // Try to match PPC NVRAM
132 pstrcpy((char *)header->struct_ident, sizeof(header->struct_ident),
133 "QEMU_BIOS");
134 header->struct_version = cpu_to_be32(3); /* structure v3 */
136 header->nvram_size = cpu_to_be16(NVRAM_size);
137 header->nvram_arch_ptr = cpu_to_be16(sizeof(ohwcfg_v3_t));
138 header->nvram_arch_size = cpu_to_be16(sizeof(struct sparc_arch_cfg));
139 pstrcpy((char *)header->arch, sizeof(header->arch), arch);
140 header->nb_cpus = smp_cpus & 0xff;
141 header->RAM0_base = 0;
142 header->RAM0_size = cpu_to_be64((uint64_t)RAM_size);
143 pstrcpy((char *)header->boot_devices, sizeof(header->boot_devices),
144 boot_devices);
145 header->nboot_devices = strlen(boot_devices) & 0xff;
146 header->kernel_image = cpu_to_be64((uint64_t)kernel_image);
147 header->kernel_size = cpu_to_be64((uint64_t)kernel_size);
148 if (cmdline) {
149 pstrcpy_targphys(CMDLINE_ADDR, TARGET_PAGE_SIZE, cmdline);
150 header->cmdline = cpu_to_be64((uint64_t)CMDLINE_ADDR);
151 header->cmdline_size = cpu_to_be64((uint64_t)strlen(cmdline));
153 header->initrd_image = cpu_to_be64((uint64_t)initrd_image);
154 header->initrd_size = cpu_to_be64((uint64_t)initrd_size);
155 header->NVRAM_image = cpu_to_be64((uint64_t)NVRAM_image);
157 header->width = cpu_to_be16(width);
158 header->height = cpu_to_be16(height);
159 header->depth = cpu_to_be16(depth);
160 if (nographic)
161 header->graphic_flags = cpu_to_be16(OHW_GF_NOGRAPHICS);
163 header->crc = cpu_to_be16(OHW_compute_crc(header, 0x00, 0xF8));
165 // Architecture specific header
166 start = sizeof(ohwcfg_v3_t);
167 sparc_header = (struct sparc_arch_cfg *)&image[start];
168 sparc_header->valid = 0;
169 start += sizeof(struct sparc_arch_cfg);
171 // OpenBIOS nvram variables
172 // Variable partition
173 part_header = (struct OpenBIOS_nvpart_v1 *)&image[start];
174 part_header->signature = OPENBIOS_PART_SYSTEM;
175 pstrcpy(part_header->name, sizeof(part_header->name), "system");
177 end = start + sizeof(struct OpenBIOS_nvpart_v1);
178 for (i = 0; i < nb_prom_envs; i++)
179 end = OpenBIOS_set_var(image, end, prom_envs[i]);
181 // End marker
182 image[end++] = '\0';
184 end = start + ((end - start + 15) & ~15);
185 OpenBIOS_finish_partition(part_header, end - start);
187 // free partition
188 start = end;
189 part_header = (struct OpenBIOS_nvpart_v1 *)&image[start];
190 part_header->signature = OPENBIOS_PART_FREE;
191 pstrcpy(part_header->name, sizeof(part_header->name), "free");
193 end = 0x1fd0;
194 OpenBIOS_finish_partition(part_header, end - start);
196 Sun_init_header((struct Sun_nvram *)&image[0x1fd8], macaddr, 0x80);
198 for (i = 0; i < sizeof(image); i++)
199 m48t59_write(nvram, i, image[i]);
201 qemu_register_boot_set(nvram_boot_set, nvram);
203 return 0;
206 void pic_info(void)
210 void irq_info(void)
214 void cpu_check_irqs(CPUState *env)
216 uint32_t pil = env->pil_in | (env->softint & ~SOFTINT_TIMER) |
217 ((env->softint & SOFTINT_TIMER) << 14);
219 if (pil && (env->interrupt_index == 0 ||
220 (env->interrupt_index & ~15) == TT_EXTINT)) {
221 unsigned int i;
223 for (i = 15; i > 0; i--) {
224 if (pil & (1 << i)) {
225 int old_interrupt = env->interrupt_index;
227 env->interrupt_index = TT_EXTINT | i;
228 if (old_interrupt != env->interrupt_index) {
229 DPRINTF("Set CPU IRQ %d\n", i);
230 cpu_interrupt(env, CPU_INTERRUPT_HARD);
232 break;
235 } else if (!pil && (env->interrupt_index & ~15) == TT_EXTINT) {
236 DPRINTF("Reset CPU IRQ %d\n", env->interrupt_index & 15);
237 env->interrupt_index = 0;
238 cpu_reset_interrupt(env, CPU_INTERRUPT_HARD);
242 static void cpu_set_irq(void *opaque, int irq, int level)
244 CPUState *env = opaque;
246 if (level) {
247 DPRINTF("Raise CPU IRQ %d\n", irq);
248 env->halted = 0;
249 env->pil_in |= 1 << irq;
250 cpu_check_irqs(env);
251 } else {
252 DPRINTF("Lower CPU IRQ %d\n", irq);
253 env->pil_in &= ~(1 << irq);
254 cpu_check_irqs(env);
258 void qemu_system_powerdown(void)
262 typedef struct ResetData {
263 CPUState *env;
264 uint64_t reset_addr;
265 } ResetData;
267 static void main_cpu_reset(void *opaque)
269 ResetData *s = (ResetData *)opaque;
270 CPUState *env = s->env;
272 cpu_reset(env);
273 ptimer_set_limit(env->tick, 0x7fffffffffffffffULL, 1);
274 ptimer_run(env->tick, 0);
275 ptimer_set_limit(env->stick, 0x7fffffffffffffffULL, 1);
276 ptimer_run(env->stick, 0);
277 ptimer_set_limit(env->hstick, 0x7fffffffffffffffULL, 1);
278 ptimer_run(env->hstick, 0);
279 env->gregs[1] = 0; // Memory start
280 env->gregs[2] = ram_size; // Memory size
281 env->gregs[3] = 0; // Machine description XXX
282 env->pc = s->reset_addr;
283 env->npc = env->pc + 4;
286 static void tick_irq(void *opaque)
288 CPUState *env = opaque;
290 env->softint |= SOFTINT_TIMER;
291 cpu_interrupt(env, CPU_INTERRUPT_TIMER);
294 static void stick_irq(void *opaque)
296 CPUState *env = opaque;
298 env->softint |= SOFTINT_TIMER;
299 cpu_interrupt(env, CPU_INTERRUPT_TIMER);
302 static void hstick_irq(void *opaque)
304 CPUState *env = opaque;
306 env->softint |= SOFTINT_TIMER;
307 cpu_interrupt(env, CPU_INTERRUPT_TIMER);
310 void cpu_tick_set_count(void *opaque, uint64_t count)
312 ptimer_set_count(opaque, -count);
315 uint64_t cpu_tick_get_count(void *opaque)
317 return -ptimer_get_count(opaque);
320 void cpu_tick_set_limit(void *opaque, uint64_t limit)
322 ptimer_set_limit(opaque, -limit, 0);
325 static const int ide_iobase[2] = { 0x1f0, 0x170 };
326 static const int ide_iobase2[2] = { 0x3f6, 0x376 };
327 static const int ide_irq[2] = { 14, 15 };
329 static const int serial_io[MAX_SERIAL_PORTS] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8 };
330 static const int serial_irq[MAX_SERIAL_PORTS] = { 4, 3, 4, 3 };
332 static const int parallel_io[MAX_PARALLEL_PORTS] = { 0x378, 0x278, 0x3bc };
333 static const int parallel_irq[MAX_PARALLEL_PORTS] = { 7, 7, 7 };
335 static fdctrl_t *floppy_controller;
337 static void sun4uv_init(ram_addr_t RAM_size, int vga_ram_size,
338 const char *boot_devices, DisplayState *ds,
339 const char *kernel_filename, const char *kernel_cmdline,
340 const char *initrd_filename, const char *cpu_model,
341 const struct hwdef *hwdef)
343 CPUState *env;
344 char buf[1024];
345 m48t59_t *nvram;
346 int ret, linux_boot;
347 unsigned int i;
348 long prom_offset, initrd_size, kernel_size;
349 PCIBus *pci_bus;
350 QEMUBH *bh;
351 qemu_irq *irq;
352 int drive_index;
353 BlockDriverState *hd[MAX_IDE_BUS * MAX_IDE_DEVS];
354 BlockDriverState *fd[MAX_FD];
355 void *fw_cfg;
356 ResetData *reset_info;
358 linux_boot = (kernel_filename != NULL);
360 /* init CPUs */
361 if (!cpu_model)
362 cpu_model = hwdef->default_cpu_model;
364 env = cpu_init(cpu_model);
365 if (!env) {
366 fprintf(stderr, "Unable to find Sparc CPU definition\n");
367 exit(1);
369 bh = qemu_bh_new(tick_irq, env);
370 env->tick = ptimer_init(bh);
371 ptimer_set_period(env->tick, 1ULL);
373 bh = qemu_bh_new(stick_irq, env);
374 env->stick = ptimer_init(bh);
375 ptimer_set_period(env->stick, 1ULL);
377 bh = qemu_bh_new(hstick_irq, env);
378 env->hstick = ptimer_init(bh);
379 ptimer_set_period(env->hstick, 1ULL);
381 reset_info = qemu_mallocz(sizeof(ResetData));
382 reset_info->env = env;
383 reset_info->reset_addr = hwdef->prom_addr + 0x40ULL;
384 qemu_register_reset(main_cpu_reset, reset_info);
385 main_cpu_reset(reset_info);
386 // Override warm reset address with cold start address
387 env->pc = hwdef->prom_addr + 0x20ULL;
388 env->npc = env->pc + 4;
390 /* allocate RAM */
391 cpu_register_physical_memory(0, RAM_size, 0);
393 prom_offset = RAM_size + vga_ram_size;
394 cpu_register_physical_memory(hwdef->prom_addr,
395 (PROM_SIZE_MAX + TARGET_PAGE_SIZE) &
396 TARGET_PAGE_MASK,
397 prom_offset | IO_MEM_ROM);
399 if (bios_name == NULL)
400 bios_name = PROM_FILENAME;
401 snprintf(buf, sizeof(buf), "%s/%s", bios_dir, bios_name);
402 ret = load_elf(buf, hwdef->prom_addr - PROM_VADDR, NULL, NULL, NULL);
403 if (ret < 0) {
404 ret = load_image_targphys(buf, hwdef->prom_addr,
405 (PROM_SIZE_MAX + TARGET_PAGE_SIZE) &
406 TARGET_PAGE_MASK);
407 if (ret < 0) {
408 fprintf(stderr, "qemu: could not load prom '%s'\n",
409 buf);
410 exit(1);
414 kernel_size = 0;
415 initrd_size = 0;
416 if (linux_boot) {
417 /* XXX: put correct offset */
418 kernel_size = load_elf(kernel_filename, 0, NULL, NULL, NULL);
419 if (kernel_size < 0)
420 kernel_size = load_aout(kernel_filename, KERNEL_LOAD_ADDR,
421 ram_size - KERNEL_LOAD_ADDR);
422 if (kernel_size < 0)
423 kernel_size = load_image_targphys(kernel_filename,
424 KERNEL_LOAD_ADDR,
425 ram_size - KERNEL_LOAD_ADDR);
426 if (kernel_size < 0) {
427 fprintf(stderr, "qemu: could not load kernel '%s'\n",
428 kernel_filename);
429 exit(1);
432 /* load initrd */
433 if (initrd_filename) {
434 initrd_size = load_image_targphys(initrd_filename,
435 INITRD_LOAD_ADDR,
436 ram_size - INITRD_LOAD_ADDR);
437 if (initrd_size < 0) {
438 fprintf(stderr, "qemu: could not load initial ram disk '%s'\n",
439 initrd_filename);
440 exit(1);
443 if (initrd_size > 0) {
444 for (i = 0; i < 64 * TARGET_PAGE_SIZE; i += TARGET_PAGE_SIZE) {
445 if (ldl_phys(KERNEL_LOAD_ADDR + i) == 0x48647253) { // HdrS
446 stl_phys(KERNEL_LOAD_ADDR + i + 16, INITRD_LOAD_ADDR);
447 stl_phys(KERNEL_LOAD_ADDR + i + 20, initrd_size);
448 break;
453 pci_bus = pci_apb_init(APB_SPECIAL_BASE, APB_MEM_BASE, NULL);
454 isa_mem_base = VGA_BASE;
455 pci_cirrus_vga_init(pci_bus, ds, phys_ram_base + RAM_size, RAM_size,
456 vga_ram_size);
458 i = 0;
459 if (hwdef->console_serial_base) {
460 serial_mm_init(hwdef->console_serial_base, 0, NULL, 115200,
461 serial_hds[i], 1);
462 i++;
464 for(; i < MAX_SERIAL_PORTS; i++) {
465 if (serial_hds[i]) {
466 serial_init(serial_io[i], NULL/*serial_irq[i]*/, 115200,
467 serial_hds[i]);
471 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
472 if (parallel_hds[i]) {
473 parallel_init(parallel_io[i], NULL/*parallel_irq[i]*/,
474 parallel_hds[i]);
478 for(i = 0; i < nb_nics; i++) {
479 if (!nd_table[i].model)
480 nd_table[i].model = "ne2k_pci";
481 pci_nic_init(pci_bus, &nd_table[i], -1);
484 irq = qemu_allocate_irqs(cpu_set_irq, env, MAX_PILS);
485 if (drive_get_max_bus(IF_IDE) >= MAX_IDE_BUS) {
486 fprintf(stderr, "qemu: too many IDE bus\n");
487 exit(1);
489 for(i = 0; i < MAX_IDE_BUS * MAX_IDE_DEVS; i++) {
490 drive_index = drive_get_index(IF_IDE, i / MAX_IDE_DEVS,
491 i % MAX_IDE_DEVS);
492 if (drive_index != -1)
493 hd[i] = drives_table[drive_index].bdrv;
494 else
495 hd[i] = NULL;
498 // XXX pci_cmd646_ide_init(pci_bus, hd, 1);
499 pci_piix3_ide_init(pci_bus, hd, -1, irq);
500 /* FIXME: wire up interrupts. */
501 i8042_init(NULL/*1*/, NULL/*12*/, 0x60);
502 for(i = 0; i < MAX_FD; i++) {
503 drive_index = drive_get_index(IF_FLOPPY, 0, i);
504 if (drive_index != -1)
505 fd[i] = drives_table[drive_index].bdrv;
506 else
507 fd[i] = NULL;
509 floppy_controller = fdctrl_init(NULL/*6*/, 2, 0, 0x3f0, fd);
510 nvram = m48t59_init(NULL/*8*/, 0, 0x0074, NVRAM_SIZE, 59);
511 sun4u_NVRAM_set_params(nvram, NVRAM_SIZE, "Sun4u", RAM_size, boot_devices,
512 KERNEL_LOAD_ADDR, kernel_size,
513 kernel_cmdline,
514 INITRD_LOAD_ADDR, initrd_size,
515 /* XXX: need an option to load a NVRAM image */
517 graphic_width, graphic_height, graphic_depth,
518 (uint8_t *)&nd_table[0].macaddr);
520 fw_cfg = fw_cfg_init(BIOS_CFG_IOPORT, BIOS_CFG_IOPORT + 1, 0, 0);
521 fw_cfg_add_i32(fw_cfg, FW_CFG_ID, 1);
522 fw_cfg_add_i64(fw_cfg, FW_CFG_RAM_SIZE, (uint64_t)ram_size);
523 fw_cfg_add_i16(fw_cfg, FW_CFG_MACHINE_ID, hwdef->machine_id);
526 enum {
527 sun4u_id = 0,
528 sun4v_id = 64,
529 niagara_id,
532 static const struct hwdef hwdefs[] = {
533 /* Sun4u generic PC-like machine */
535 .default_cpu_model = "TI UltraSparc II",
536 .machine_id = sun4u_id,
537 .prom_addr = 0x1fff0000000ULL,
538 .console_serial_base = 0,
540 /* Sun4v generic PC-like machine */
542 .default_cpu_model = "Sun UltraSparc T1",
543 .machine_id = sun4v_id,
544 .prom_addr = 0x1fff0000000ULL,
545 .console_serial_base = 0,
547 /* Sun4v generic Niagara machine */
549 .default_cpu_model = "Sun UltraSparc T1",
550 .machine_id = niagara_id,
551 .prom_addr = 0xfff0000000ULL,
552 .console_serial_base = 0xfff0c2c000ULL,
556 /* Sun4u hardware initialisation */
557 static void sun4u_init(ram_addr_t RAM_size, int vga_ram_size,
558 const char *boot_devices, DisplayState *ds,
559 const char *kernel_filename, const char *kernel_cmdline,
560 const char *initrd_filename, const char *cpu_model)
562 sun4uv_init(RAM_size, vga_ram_size, boot_devices, ds, kernel_filename,
563 kernel_cmdline, initrd_filename, cpu_model, &hwdefs[0]);
566 /* Sun4v hardware initialisation */
567 static void sun4v_init(ram_addr_t RAM_size, int vga_ram_size,
568 const char *boot_devices, DisplayState *ds,
569 const char *kernel_filename, const char *kernel_cmdline,
570 const char *initrd_filename, const char *cpu_model)
572 sun4uv_init(RAM_size, vga_ram_size, boot_devices, ds, kernel_filename,
573 kernel_cmdline, initrd_filename, cpu_model, &hwdefs[1]);
576 /* Niagara hardware initialisation */
577 static void niagara_init(ram_addr_t RAM_size, int vga_ram_size,
578 const char *boot_devices, DisplayState *ds,
579 const char *kernel_filename, const char *kernel_cmdline,
580 const char *initrd_filename, const char *cpu_model)
582 sun4uv_init(RAM_size, vga_ram_size, boot_devices, ds, kernel_filename,
583 kernel_cmdline, initrd_filename, cpu_model, &hwdefs[2]);
586 QEMUMachine sun4u_machine = {
587 .name = "sun4u",
588 .desc = "Sun4u platform",
589 .init = sun4u_init,
590 .ram_require = PROM_SIZE_MAX + VGA_RAM_SIZE,
591 .nodisk_ok = 1,
592 .max_cpus = 16,
595 QEMUMachine sun4v_machine = {
596 .name = "sun4v",
597 .desc = "Sun4v platform",
598 .init = sun4v_init,
599 .ram_require = PROM_SIZE_MAX + VGA_RAM_SIZE,
600 .nodisk_ok = 1,
601 .max_cpus = 16,
604 QEMUMachine niagara_machine = {
605 .name = "Niagara",
606 .desc = "Sun4v platform, Niagara",
607 .init = niagara_init,
608 .ram_require = PROM_SIZE_MAX + VGA_RAM_SIZE,
609 .nodisk_ok = 1,
610 .max_cpus = 16,