linux-user: fix to handle variably sized SIOCGSTAMP with new kernels
[qemu/ar7.git] / hw / mips / mips_malta.c
blob20e019bf66193bb99d8f389b5cbb874b5b25f531
1 /*
2 * QEMU Malta board support
4 * Copyright (c) 2006 Aurelien Jarno
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.
25 #include "qemu/osdep.h"
26 #include "qemu/units.h"
27 #include "qemu-common.h"
28 #include "cpu.h"
29 #include "hw/hw.h"
30 #include "hw/i386/pc.h"
31 #include "hw/isa/superio.h"
32 #include "hw/dma/i8257.h"
33 #include "hw/char/serial.h"
34 #include "net/net.h"
35 #include "hw/boards.h"
36 #include "hw/i2c/smbus_eeprom.h"
37 #include "hw/block/flash.h"
38 #include "hw/mips/mips.h"
39 #include "hw/mips/cpudevs.h"
40 #include "hw/pci/pci.h"
41 #include "sysemu/sysemu.h"
42 #include "sysemu/arch_init.h"
43 #include "qemu/log.h"
44 #include "hw/mips/bios.h"
45 #include "hw/ide.h"
46 #include "hw/loader.h"
47 #include "elf.h"
48 #include "hw/timer/mc146818rtc.h"
49 #include "hw/timer/i8254.h"
50 #include "exec/address-spaces.h"
51 #include "hw/sysbus.h" /* SysBusDevice */
52 #include "qemu/host-utils.h"
53 #include "sysemu/qtest.h"
54 #include "qapi/error.h"
55 #include "qemu/error-report.h"
56 #include "hw/empty_slot.h"
57 #include "sysemu/kvm.h"
58 #include "hw/semihosting/semihost.h"
59 #include "hw/mips/cps.h"
61 #define ENVP_ADDR 0x80002000l
62 #define ENVP_NB_ENTRIES 16
63 #define ENVP_ENTRY_SIZE 256
65 /* Hardware addresses */
66 #define FLASH_ADDRESS 0x1e000000ULL
67 #define FPGA_ADDRESS 0x1f000000ULL
68 #define RESET_ADDRESS 0x1fc00000ULL
70 #define FLASH_SIZE 0x400000
72 #define MAX_IDE_BUS 2
74 typedef struct {
75 MemoryRegion iomem;
76 MemoryRegion iomem_lo; /* 0 - 0x900 */
77 MemoryRegion iomem_hi; /* 0xa00 - 0x100000 */
78 uint32_t leds;
79 uint32_t brk;
80 uint32_t gpout;
81 uint32_t i2cin;
82 uint32_t i2coe;
83 uint32_t i2cout;
84 uint32_t i2csel;
85 CharBackend display;
86 char display_text[9];
87 SerialState *uart;
88 bool display_inited;
89 } MaltaFPGAState;
91 #define TYPE_MIPS_MALTA "mips-malta"
92 #define MIPS_MALTA(obj) OBJECT_CHECK(MaltaState, (obj), TYPE_MIPS_MALTA)
94 typedef struct {
95 SysBusDevice parent_obj;
97 MIPSCPSState cps;
98 qemu_irq *i8259;
99 } MaltaState;
101 static ISADevice *pit;
103 static struct _loaderparams {
104 int ram_size, ram_low_size;
105 const char *kernel_filename;
106 const char *kernel_cmdline;
107 const char *initrd_filename;
108 } loaderparams;
110 /* Malta FPGA */
111 static void malta_fpga_update_display(void *opaque)
113 char leds_text[9];
114 int i;
115 MaltaFPGAState *s = opaque;
117 for (i = 7 ; i >= 0 ; i--) {
118 if (s->leds & (1 << i))
119 leds_text[i] = '#';
120 else
121 leds_text[i] = ' ';
123 leds_text[8] = '\0';
125 qemu_chr_fe_printf(&s->display, "\e[H\n\n|\e[32m%-8.8s\e[00m|\r\n",
126 leds_text);
127 qemu_chr_fe_printf(&s->display, "\n\n\n\n|\e[31m%-8.8s\e[00m|",
128 s->display_text);
132 * EEPROM 24C01 / 24C02 emulation.
134 * Emulation for serial EEPROMs:
135 * 24C01 - 1024 bit (128 x 8)
136 * 24C02 - 2048 bit (256 x 8)
138 * Typical device names include Microchip 24C02SC or SGS Thomson ST24C02.
141 //~ #define DEBUG
143 #if defined(DEBUG)
144 # define logout(fmt, ...) fprintf(stderr, "MALTA\t%-24s" fmt, __func__, ## __VA_ARGS__)
145 #else
146 # define logout(fmt, ...) ((void)0)
147 #endif
149 struct _eeprom24c0x_t {
150 uint8_t tick;
151 uint8_t address;
152 uint8_t command;
153 uint8_t ack;
154 uint8_t scl;
155 uint8_t sda;
156 uint8_t data;
157 //~ uint16_t size;
158 uint8_t contents[256];
161 typedef struct _eeprom24c0x_t eeprom24c0x_t;
163 static eeprom24c0x_t spd_eeprom = {
164 .contents = {
165 /* 00000000: */ 0x80,0x08,0xFF,0x0D,0x0A,0xFF,0x40,0x00,
166 /* 00000008: */ 0x01,0x75,0x54,0x00,0x82,0x08,0x00,0x01,
167 /* 00000010: */ 0x8F,0x04,0x02,0x01,0x01,0x00,0x00,0x00,
168 /* 00000018: */ 0x00,0x00,0x00,0x14,0x0F,0x14,0x2D,0xFF,
169 /* 00000020: */ 0x15,0x08,0x15,0x08,0x00,0x00,0x00,0x00,
170 /* 00000028: */ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
171 /* 00000030: */ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
172 /* 00000038: */ 0x00,0x00,0x00,0x00,0x00,0x00,0x12,0xD0,
173 /* 00000040: */ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
174 /* 00000048: */ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
175 /* 00000050: */ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
176 /* 00000058: */ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
177 /* 00000060: */ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
178 /* 00000068: */ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
179 /* 00000070: */ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
180 /* 00000078: */ 0x00,0x00,0x00,0x00,0x00,0x00,0x64,0xF4,
184 static void generate_eeprom_spd(uint8_t *eeprom, ram_addr_t ram_size)
186 enum { SDR = 0x4, DDR2 = 0x8 } type;
187 uint8_t *spd = spd_eeprom.contents;
188 uint8_t nbanks = 0;
189 uint16_t density = 0;
190 int i;
192 /* work in terms of MB */
193 ram_size /= MiB;
195 while ((ram_size >= 4) && (nbanks <= 2)) {
196 int sz_log2 = MIN(31 - clz32(ram_size), 14);
197 nbanks++;
198 density |= 1 << (sz_log2 - 2);
199 ram_size -= 1 << sz_log2;
202 /* split to 2 banks if possible */
203 if ((nbanks == 1) && (density > 1)) {
204 nbanks++;
205 density >>= 1;
208 if (density & 0xff00) {
209 density = (density & 0xe0) | ((density >> 8) & 0x1f);
210 type = DDR2;
211 } else if (!(density & 0x1f)) {
212 type = DDR2;
213 } else {
214 type = SDR;
217 if (ram_size) {
218 warn_report("SPD cannot represent final " RAM_ADDR_FMT "MB"
219 " of SDRAM", ram_size);
222 /* fill in SPD memory information */
223 spd[2] = type;
224 spd[5] = nbanks;
225 spd[31] = density;
227 /* checksum */
228 spd[63] = 0;
229 for (i = 0; i < 63; i++) {
230 spd[63] += spd[i];
233 /* copy for SMBUS */
234 memcpy(eeprom, spd, sizeof(spd_eeprom.contents));
237 static void generate_eeprom_serial(uint8_t *eeprom)
239 int i, pos = 0;
240 uint8_t mac[6] = { 0x00 };
241 uint8_t sn[5] = { 0x01, 0x23, 0x45, 0x67, 0x89 };
243 /* version */
244 eeprom[pos++] = 0x01;
246 /* count */
247 eeprom[pos++] = 0x02;
249 /* MAC address */
250 eeprom[pos++] = 0x01; /* MAC */
251 eeprom[pos++] = 0x06; /* length */
252 memcpy(&eeprom[pos], mac, sizeof(mac));
253 pos += sizeof(mac);
255 /* serial number */
256 eeprom[pos++] = 0x02; /* serial */
257 eeprom[pos++] = 0x05; /* length */
258 memcpy(&eeprom[pos], sn, sizeof(sn));
259 pos += sizeof(sn);
261 /* checksum */
262 eeprom[pos] = 0;
263 for (i = 0; i < pos; i++) {
264 eeprom[pos] += eeprom[i];
268 static uint8_t eeprom24c0x_read(eeprom24c0x_t *eeprom)
270 logout("%u: scl = %u, sda = %u, data = 0x%02x\n",
271 eeprom->tick, eeprom->scl, eeprom->sda, eeprom->data);
272 return eeprom->sda;
275 static void eeprom24c0x_write(eeprom24c0x_t *eeprom, int scl, int sda)
277 if (eeprom->scl && scl && (eeprom->sda != sda)) {
278 logout("%u: scl = %u->%u, sda = %u->%u i2c %s\n",
279 eeprom->tick, eeprom->scl, scl, eeprom->sda, sda,
280 sda ? "stop" : "start");
281 if (!sda) {
282 eeprom->tick = 1;
283 eeprom->command = 0;
285 } else if (eeprom->tick == 0 && !eeprom->ack) {
286 /* Waiting for start. */
287 logout("%u: scl = %u->%u, sda = %u->%u wait for i2c start\n",
288 eeprom->tick, eeprom->scl, scl, eeprom->sda, sda);
289 } else if (!eeprom->scl && scl) {
290 logout("%u: scl = %u->%u, sda = %u->%u trigger bit\n",
291 eeprom->tick, eeprom->scl, scl, eeprom->sda, sda);
292 if (eeprom->ack) {
293 logout("\ti2c ack bit = 0\n");
294 sda = 0;
295 eeprom->ack = 0;
296 } else if (eeprom->sda == sda) {
297 uint8_t bit = (sda != 0);
298 logout("\ti2c bit = %d\n", bit);
299 if (eeprom->tick < 9) {
300 eeprom->command <<= 1;
301 eeprom->command += bit;
302 eeprom->tick++;
303 if (eeprom->tick == 9) {
304 logout("\tcommand 0x%04x, %s\n", eeprom->command,
305 bit ? "read" : "write");
306 eeprom->ack = 1;
308 } else if (eeprom->tick < 17) {
309 if (eeprom->command & 1) {
310 sda = ((eeprom->data & 0x80) != 0);
312 eeprom->address <<= 1;
313 eeprom->address += bit;
314 eeprom->tick++;
315 eeprom->data <<= 1;
316 if (eeprom->tick == 17) {
317 eeprom->data = eeprom->contents[eeprom->address];
318 logout("\taddress 0x%04x, data 0x%02x\n",
319 eeprom->address, eeprom->data);
320 eeprom->ack = 1;
321 eeprom->tick = 0;
323 } else if (eeprom->tick >= 17) {
324 sda = 0;
326 } else {
327 logout("\tsda changed with raising scl\n");
329 } else {
330 logout("%u: scl = %u->%u, sda = %u->%u\n", eeprom->tick, eeprom->scl,
331 scl, eeprom->sda, sda);
333 eeprom->scl = scl;
334 eeprom->sda = sda;
337 static uint64_t malta_fpga_read(void *opaque, hwaddr addr,
338 unsigned size)
340 MaltaFPGAState *s = opaque;
341 uint32_t val = 0;
342 uint32_t saddr;
344 saddr = (addr & 0xfffff);
346 switch (saddr) {
348 /* SWITCH Register */
349 case 0x00200:
350 val = 0x00000000; /* All switches closed */
351 break;
353 /* STATUS Register */
354 case 0x00208:
355 #ifdef TARGET_WORDS_BIGENDIAN
356 val = 0x00000012;
357 #else
358 val = 0x00000010;
359 #endif
360 break;
362 /* JMPRS Register */
363 case 0x00210:
364 val = 0x00;
365 break;
367 /* LEDBAR Register */
368 case 0x00408:
369 val = s->leds;
370 break;
372 /* BRKRES Register */
373 case 0x00508:
374 val = s->brk;
375 break;
377 /* UART Registers are handled directly by the serial device */
379 /* GPOUT Register */
380 case 0x00a00:
381 val = s->gpout;
382 break;
384 /* XXX: implement a real I2C controller */
386 /* GPINP Register */
387 case 0x00a08:
388 /* IN = OUT until a real I2C control is implemented */
389 if (s->i2csel)
390 val = s->i2cout;
391 else
392 val = 0x00;
393 break;
395 /* I2CINP Register */
396 case 0x00b00:
397 val = ((s->i2cin & ~1) | eeprom24c0x_read(&spd_eeprom));
398 break;
400 /* I2COE Register */
401 case 0x00b08:
402 val = s->i2coe;
403 break;
405 /* I2COUT Register */
406 case 0x00b10:
407 val = s->i2cout;
408 break;
410 /* I2CSEL Register */
411 case 0x00b18:
412 val = s->i2csel;
413 break;
415 default:
416 #if 0
417 printf ("malta_fpga_read: Bad register offset 0x" TARGET_FMT_lx "\n",
418 addr);
419 #endif
420 break;
422 return val;
425 static void malta_fpga_write(void *opaque, hwaddr addr,
426 uint64_t val, unsigned size)
428 MaltaFPGAState *s = opaque;
429 uint32_t saddr;
431 saddr = (addr & 0xfffff);
433 switch (saddr) {
435 /* SWITCH Register */
436 case 0x00200:
437 break;
439 /* JMPRS Register */
440 case 0x00210:
441 break;
443 /* LEDBAR Register */
444 case 0x00408:
445 s->leds = val & 0xff;
446 malta_fpga_update_display(s);
447 break;
449 /* ASCIIWORD Register */
450 case 0x00410:
451 snprintf(s->display_text, 9, "%08X", (uint32_t)val);
452 malta_fpga_update_display(s);
453 break;
455 /* ASCIIPOS0 to ASCIIPOS7 Registers */
456 case 0x00418:
457 case 0x00420:
458 case 0x00428:
459 case 0x00430:
460 case 0x00438:
461 case 0x00440:
462 case 0x00448:
463 case 0x00450:
464 s->display_text[(saddr - 0x00418) >> 3] = (char) val;
465 malta_fpga_update_display(s);
466 break;
468 /* SOFTRES Register */
469 case 0x00500:
470 if (val == 0x42)
471 qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
472 break;
474 /* BRKRES Register */
475 case 0x00508:
476 s->brk = val & 0xff;
477 break;
479 /* UART Registers are handled directly by the serial device */
481 /* GPOUT Register */
482 case 0x00a00:
483 s->gpout = val & 0xff;
484 break;
486 /* I2COE Register */
487 case 0x00b08:
488 s->i2coe = val & 0x03;
489 break;
491 /* I2COUT Register */
492 case 0x00b10:
493 eeprom24c0x_write(&spd_eeprom, val & 0x02, val & 0x01);
494 s->i2cout = val;
495 break;
497 /* I2CSEL Register */
498 case 0x00b18:
499 s->i2csel = val & 0x01;
500 break;
502 default:
503 #if 0
504 printf ("malta_fpga_write: Bad register offset 0x" TARGET_FMT_lx "\n",
505 addr);
506 #endif
507 break;
511 static const MemoryRegionOps malta_fpga_ops = {
512 .read = malta_fpga_read,
513 .write = malta_fpga_write,
514 .endianness = DEVICE_NATIVE_ENDIAN,
517 static void malta_fpga_reset(void *opaque)
519 MaltaFPGAState *s = opaque;
521 s->leds = 0x00;
522 s->brk = 0x0a;
523 s->gpout = 0x00;
524 s->i2cin = 0x3;
525 s->i2coe = 0x0;
526 s->i2cout = 0x3;
527 s->i2csel = 0x1;
529 s->display_text[8] = '\0';
530 snprintf(s->display_text, 9, " ");
533 static void malta_fgpa_display_event(void *opaque, int event)
535 MaltaFPGAState *s = opaque;
537 if (event == CHR_EVENT_OPENED && !s->display_inited) {
538 qemu_chr_fe_printf(&s->display, "\e[HMalta LEDBAR\r\n");
539 qemu_chr_fe_printf(&s->display, "+--------+\r\n");
540 qemu_chr_fe_printf(&s->display, "+ +\r\n");
541 qemu_chr_fe_printf(&s->display, "+--------+\r\n");
542 qemu_chr_fe_printf(&s->display, "\n");
543 qemu_chr_fe_printf(&s->display, "Malta ASCII\r\n");
544 qemu_chr_fe_printf(&s->display, "+--------+\r\n");
545 qemu_chr_fe_printf(&s->display, "+ +\r\n");
546 qemu_chr_fe_printf(&s->display, "+--------+\r\n");
547 s->display_inited = true;
551 static MaltaFPGAState *malta_fpga_init(MemoryRegion *address_space,
552 hwaddr base, qemu_irq uart_irq, Chardev *uart_chr)
554 MaltaFPGAState *s;
555 Chardev *chr;
557 s = (MaltaFPGAState *)g_malloc0(sizeof(MaltaFPGAState));
559 memory_region_init_io(&s->iomem, NULL, &malta_fpga_ops, s,
560 "malta-fpga", 0x100000);
561 memory_region_init_alias(&s->iomem_lo, NULL, "malta-fpga",
562 &s->iomem, 0, 0x900);
563 memory_region_init_alias(&s->iomem_hi, NULL, "malta-fpga",
564 &s->iomem, 0xa00, 0x10000-0xa00);
566 memory_region_add_subregion(address_space, base, &s->iomem_lo);
567 memory_region_add_subregion(address_space, base + 0xa00, &s->iomem_hi);
569 chr = qemu_chr_new("fpga", "vc:320x200", NULL);
570 qemu_chr_fe_init(&s->display, chr, NULL);
571 qemu_chr_fe_set_handlers(&s->display, NULL, NULL,
572 malta_fgpa_display_event, NULL, s, NULL, true);
574 s->uart = serial_mm_init(address_space, base + 0x900, 3, uart_irq,
575 230400, uart_chr, DEVICE_NATIVE_ENDIAN);
577 malta_fpga_reset(s);
578 qemu_register_reset(malta_fpga_reset, s);
580 return s;
583 /* Network support */
584 static void network_init(PCIBus *pci_bus)
586 int i;
588 for(i = 0; i < nb_nics; i++) {
589 NICInfo *nd = &nd_table[i];
590 const char *default_devaddr = NULL;
592 if (i == 0 && (!nd->model || strcmp(nd->model, "pcnet") == 0))
593 /* The malta board has a PCNet card using PCI SLOT 11 */
594 default_devaddr = "0b";
596 pci_nic_init_nofail(nd, pci_bus, "pcnet", default_devaddr);
600 static void write_bootloader_nanomips(uint8_t *base, int64_t run_addr,
601 int64_t kernel_entry)
603 uint16_t *p;
605 /* Small bootloader */
606 p = (uint16_t *)base;
608 #define NM_HI1(VAL) (((VAL) >> 16) & 0x1f)
609 #define NM_HI2(VAL) \
610 (((VAL) & 0xf000) | (((VAL) >> 19) & 0xffc) | (((VAL) >> 31) & 0x1))
611 #define NM_LO(VAL) ((VAL) & 0xfff)
613 stw_p(p++, 0x2800); stw_p(p++, 0x001c);
614 /* bc to_here */
615 stw_p(p++, 0x8000); stw_p(p++, 0xc000);
616 /* nop */
617 stw_p(p++, 0x8000); stw_p(p++, 0xc000);
618 /* nop */
619 stw_p(p++, 0x8000); stw_p(p++, 0xc000);
620 /* nop */
621 stw_p(p++, 0x8000); stw_p(p++, 0xc000);
622 /* nop */
623 stw_p(p++, 0x8000); stw_p(p++, 0xc000);
624 /* nop */
625 stw_p(p++, 0x8000); stw_p(p++, 0xc000);
626 /* nop */
627 stw_p(p++, 0x8000); stw_p(p++, 0xc000);
628 /* nop */
630 /* to_here: */
631 if (semihosting_get_argc()) {
632 /* Preserve a0 content as arguments have been passed */
633 stw_p(p++, 0x8000); stw_p(p++, 0xc000);
634 /* nop */
635 } else {
636 stw_p(p++, 0x0080); stw_p(p++, 0x0002);
637 /* li a0,2 */
640 stw_p(p++, 0xe3a0 | NM_HI1(ENVP_ADDR - 64));
642 stw_p(p++, NM_HI2(ENVP_ADDR - 64));
643 /* lui sp,%hi(ENVP_ADDR - 64) */
645 stw_p(p++, 0x83bd); stw_p(p++, NM_LO(ENVP_ADDR - 64));
646 /* ori sp,sp,%lo(ENVP_ADDR - 64) */
648 stw_p(p++, 0xe0a0 | NM_HI1(ENVP_ADDR));
650 stw_p(p++, NM_HI2(ENVP_ADDR));
651 /* lui a1,%hi(ENVP_ADDR) */
653 stw_p(p++, 0x80a5); stw_p(p++, NM_LO(ENVP_ADDR));
654 /* ori a1,a1,%lo(ENVP_ADDR) */
656 stw_p(p++, 0xe0c0 | NM_HI1(ENVP_ADDR + 8));
658 stw_p(p++, NM_HI2(ENVP_ADDR + 8));
659 /* lui a2,%hi(ENVP_ADDR + 8) */
661 stw_p(p++, 0x80c6); stw_p(p++, NM_LO(ENVP_ADDR + 8));
662 /* ori a2,a2,%lo(ENVP_ADDR + 8) */
664 stw_p(p++, 0xe0e0 | NM_HI1(loaderparams.ram_low_size));
666 stw_p(p++, NM_HI2(loaderparams.ram_low_size));
667 /* lui a3,%hi(loaderparams.ram_low_size) */
669 stw_p(p++, 0x80e7); stw_p(p++, NM_LO(loaderparams.ram_low_size));
670 /* ori a3,a3,%lo(loaderparams.ram_low_size) */
673 * Load BAR registers as done by YAMON:
675 * - set up PCI0 I/O BARs from 0x18000000 to 0x181fffff
676 * - set up PCI0 MEM0 at 0x10000000, size 0x8000000
677 * - set up PCI0 MEM1 at 0x18200000, size 0xbe00000
680 stw_p(p++, 0xe040); stw_p(p++, 0x0681);
681 /* lui t1, %hi(0xb4000000) */
683 #ifdef TARGET_WORDS_BIGENDIAN
685 stw_p(p++, 0xe020); stw_p(p++, 0x0be1);
686 /* lui t0, %hi(0xdf000000) */
688 /* 0x68 corresponds to GT_ISD (from hw/mips/gt64xxx_pci.c) */
689 stw_p(p++, 0x8422); stw_p(p++, 0x9068);
690 /* sw t0, 0x68(t1) */
692 stw_p(p++, 0xe040); stw_p(p++, 0x077d);
693 /* lui t1, %hi(0xbbe00000) */
695 stw_p(p++, 0xe020); stw_p(p++, 0x0801);
696 /* lui t0, %hi(0xc0000000) */
698 /* 0x48 corresponds to GT_PCI0IOLD */
699 stw_p(p++, 0x8422); stw_p(p++, 0x9048);
700 /* sw t0, 0x48(t1) */
702 stw_p(p++, 0xe020); stw_p(p++, 0x0800);
703 /* lui t0, %hi(0x40000000) */
705 /* 0x50 corresponds to GT_PCI0IOHD */
706 stw_p(p++, 0x8422); stw_p(p++, 0x9050);
707 /* sw t0, 0x50(t1) */
709 stw_p(p++, 0xe020); stw_p(p++, 0x0001);
710 /* lui t0, %hi(0x80000000) */
712 /* 0x58 corresponds to GT_PCI0M0LD */
713 stw_p(p++, 0x8422); stw_p(p++, 0x9058);
714 /* sw t0, 0x58(t1) */
716 stw_p(p++, 0xe020); stw_p(p++, 0x07e0);
717 /* lui t0, %hi(0x3f000000) */
719 /* 0x60 corresponds to GT_PCI0M0HD */
720 stw_p(p++, 0x8422); stw_p(p++, 0x9060);
721 /* sw t0, 0x60(t1) */
723 stw_p(p++, 0xe020); stw_p(p++, 0x0821);
724 /* lui t0, %hi(0xc1000000) */
726 /* 0x80 corresponds to GT_PCI0M1LD */
727 stw_p(p++, 0x8422); stw_p(p++, 0x9080);
728 /* sw t0, 0x80(t1) */
730 stw_p(p++, 0xe020); stw_p(p++, 0x0bc0);
731 /* lui t0, %hi(0x5e000000) */
733 #else
735 stw_p(p++, 0x0020); stw_p(p++, 0x00df);
736 /* addiu[32] t0, $0, 0xdf */
738 /* 0x68 corresponds to GT_ISD */
739 stw_p(p++, 0x8422); stw_p(p++, 0x9068);
740 /* sw t0, 0x68(t1) */
742 /* Use kseg2 remapped address 0x1be00000 */
743 stw_p(p++, 0xe040); stw_p(p++, 0x077d);
744 /* lui t1, %hi(0xbbe00000) */
746 stw_p(p++, 0x0020); stw_p(p++, 0x00c0);
747 /* addiu[32] t0, $0, 0xc0 */
749 /* 0x48 corresponds to GT_PCI0IOLD */
750 stw_p(p++, 0x8422); stw_p(p++, 0x9048);
751 /* sw t0, 0x48(t1) */
753 stw_p(p++, 0x0020); stw_p(p++, 0x0040);
754 /* addiu[32] t0, $0, 0x40 */
756 /* 0x50 corresponds to GT_PCI0IOHD */
757 stw_p(p++, 0x8422); stw_p(p++, 0x9050);
758 /* sw t0, 0x50(t1) */
760 stw_p(p++, 0x0020); stw_p(p++, 0x0080);
761 /* addiu[32] t0, $0, 0x80 */
763 /* 0x58 corresponds to GT_PCI0M0LD */
764 stw_p(p++, 0x8422); stw_p(p++, 0x9058);
765 /* sw t0, 0x58(t1) */
767 stw_p(p++, 0x0020); stw_p(p++, 0x003f);
768 /* addiu[32] t0, $0, 0x3f */
770 /* 0x60 corresponds to GT_PCI0M0HD */
771 stw_p(p++, 0x8422); stw_p(p++, 0x9060);
772 /* sw t0, 0x60(t1) */
774 stw_p(p++, 0x0020); stw_p(p++, 0x00c1);
775 /* addiu[32] t0, $0, 0xc1 */
777 /* 0x80 corresponds to GT_PCI0M1LD */
778 stw_p(p++, 0x8422); stw_p(p++, 0x9080);
779 /* sw t0, 0x80(t1) */
781 stw_p(p++, 0x0020); stw_p(p++, 0x005e);
782 /* addiu[32] t0, $0, 0x5e */
784 #endif
786 /* 0x88 corresponds to GT_PCI0M1HD */
787 stw_p(p++, 0x8422); stw_p(p++, 0x9088);
788 /* sw t0, 0x88(t1) */
790 stw_p(p++, 0xe320 | NM_HI1(kernel_entry));
792 stw_p(p++, NM_HI2(kernel_entry));
793 /* lui t9,%hi(kernel_entry) */
795 stw_p(p++, 0x8339); stw_p(p++, NM_LO(kernel_entry));
796 /* ori t9,t9,%lo(kernel_entry) */
798 stw_p(p++, 0x4bf9); stw_p(p++, 0x0000);
799 /* jalrc t8 */
802 /* ROM and pseudo bootloader
804 The following code implements a very very simple bootloader. It first
805 loads the registers a0 to a3 to the values expected by the OS, and
806 then jump at the kernel address.
808 The bootloader should pass the locations of the kernel arguments and
809 environment variables tables. Those tables contain the 32-bit address
810 of NULL terminated strings. The environment variables table should be
811 terminated by a NULL address.
813 For a simpler implementation, the number of kernel arguments is fixed
814 to two (the name of the kernel and the command line), and the two
815 tables are actually the same one.
817 The registers a0 to a3 should contain the following values:
818 a0 - number of kernel arguments
819 a1 - 32-bit address of the kernel arguments table
820 a2 - 32-bit address of the environment variables table
821 a3 - RAM size in bytes
823 static void write_bootloader(uint8_t *base, int64_t run_addr,
824 int64_t kernel_entry)
826 uint32_t *p;
828 /* Small bootloader */
829 p = (uint32_t *)base;
831 stl_p(p++, 0x08000000 | /* j 0x1fc00580 */
832 ((run_addr + 0x580) & 0x0fffffff) >> 2);
833 stl_p(p++, 0x00000000); /* nop */
835 /* YAMON service vector */
836 stl_p(base + 0x500, run_addr + 0x0580); /* start: */
837 stl_p(base + 0x504, run_addr + 0x083c); /* print_count: */
838 stl_p(base + 0x520, run_addr + 0x0580); /* start: */
839 stl_p(base + 0x52c, run_addr + 0x0800); /* flush_cache: */
840 stl_p(base + 0x534, run_addr + 0x0808); /* print: */
841 stl_p(base + 0x538, run_addr + 0x0800); /* reg_cpu_isr: */
842 stl_p(base + 0x53c, run_addr + 0x0800); /* unred_cpu_isr: */
843 stl_p(base + 0x540, run_addr + 0x0800); /* reg_ic_isr: */
844 stl_p(base + 0x544, run_addr + 0x0800); /* unred_ic_isr: */
845 stl_p(base + 0x548, run_addr + 0x0800); /* reg_esr: */
846 stl_p(base + 0x54c, run_addr + 0x0800); /* unreg_esr: */
847 stl_p(base + 0x550, run_addr + 0x0800); /* getchar: */
848 stl_p(base + 0x554, run_addr + 0x0800); /* syscon_read: */
851 /* Second part of the bootloader */
852 p = (uint32_t *) (base + 0x580);
854 if (semihosting_get_argc()) {
855 /* Preserve a0 content as arguments have been passed */
856 stl_p(p++, 0x00000000); /* nop */
857 } else {
858 stl_p(p++, 0x24040002); /* addiu a0, zero, 2 */
860 stl_p(p++, 0x3c1d0000 | (((ENVP_ADDR - 64) >> 16) & 0xffff)); /* lui sp, high(ENVP_ADDR) */
861 stl_p(p++, 0x37bd0000 | ((ENVP_ADDR - 64) & 0xffff)); /* ori sp, sp, low(ENVP_ADDR) */
862 stl_p(p++, 0x3c050000 | ((ENVP_ADDR >> 16) & 0xffff)); /* lui a1, high(ENVP_ADDR) */
863 stl_p(p++, 0x34a50000 | (ENVP_ADDR & 0xffff)); /* ori a1, a1, low(ENVP_ADDR) */
864 stl_p(p++, 0x3c060000 | (((ENVP_ADDR + 8) >> 16) & 0xffff)); /* lui a2, high(ENVP_ADDR + 8) */
865 stl_p(p++, 0x34c60000 | ((ENVP_ADDR + 8) & 0xffff)); /* ori a2, a2, low(ENVP_ADDR + 8) */
866 stl_p(p++, 0x3c070000 | (loaderparams.ram_low_size >> 16)); /* lui a3, high(ram_low_size) */
867 stl_p(p++, 0x34e70000 | (loaderparams.ram_low_size & 0xffff)); /* ori a3, a3, low(ram_low_size) */
869 /* Load BAR registers as done by YAMON */
870 stl_p(p++, 0x3c09b400); /* lui t1, 0xb400 */
872 #ifdef TARGET_WORDS_BIGENDIAN
873 stl_p(p++, 0x3c08df00); /* lui t0, 0xdf00 */
874 #else
875 stl_p(p++, 0x340800df); /* ori t0, r0, 0x00df */
876 #endif
877 stl_p(p++, 0xad280068); /* sw t0, 0x0068(t1) */
879 stl_p(p++, 0x3c09bbe0); /* lui t1, 0xbbe0 */
881 #ifdef TARGET_WORDS_BIGENDIAN
882 stl_p(p++, 0x3c08c000); /* lui t0, 0xc000 */
883 #else
884 stl_p(p++, 0x340800c0); /* ori t0, r0, 0x00c0 */
885 #endif
886 stl_p(p++, 0xad280048); /* sw t0, 0x0048(t1) */
887 #ifdef TARGET_WORDS_BIGENDIAN
888 stl_p(p++, 0x3c084000); /* lui t0, 0x4000 */
889 #else
890 stl_p(p++, 0x34080040); /* ori t0, r0, 0x0040 */
891 #endif
892 stl_p(p++, 0xad280050); /* sw t0, 0x0050(t1) */
894 #ifdef TARGET_WORDS_BIGENDIAN
895 stl_p(p++, 0x3c088000); /* lui t0, 0x8000 */
896 #else
897 stl_p(p++, 0x34080080); /* ori t0, r0, 0x0080 */
898 #endif
899 stl_p(p++, 0xad280058); /* sw t0, 0x0058(t1) */
900 #ifdef TARGET_WORDS_BIGENDIAN
901 stl_p(p++, 0x3c083f00); /* lui t0, 0x3f00 */
902 #else
903 stl_p(p++, 0x3408003f); /* ori t0, r0, 0x003f */
904 #endif
905 stl_p(p++, 0xad280060); /* sw t0, 0x0060(t1) */
907 #ifdef TARGET_WORDS_BIGENDIAN
908 stl_p(p++, 0x3c08c100); /* lui t0, 0xc100 */
909 #else
910 stl_p(p++, 0x340800c1); /* ori t0, r0, 0x00c1 */
911 #endif
912 stl_p(p++, 0xad280080); /* sw t0, 0x0080(t1) */
913 #ifdef TARGET_WORDS_BIGENDIAN
914 stl_p(p++, 0x3c085e00); /* lui t0, 0x5e00 */
915 #else
916 stl_p(p++, 0x3408005e); /* ori t0, r0, 0x005e */
917 #endif
918 stl_p(p++, 0xad280088); /* sw t0, 0x0088(t1) */
920 /* Jump to kernel code */
921 stl_p(p++, 0x3c1f0000 | ((kernel_entry >> 16) & 0xffff)); /* lui ra, high(kernel_entry) */
922 stl_p(p++, 0x37ff0000 | (kernel_entry & 0xffff)); /* ori ra, ra, low(kernel_entry) */
923 stl_p(p++, 0x03e00009); /* jalr ra */
924 stl_p(p++, 0x00000000); /* nop */
926 /* YAMON subroutines */
927 p = (uint32_t *) (base + 0x800);
928 stl_p(p++, 0x03e00009); /* jalr ra */
929 stl_p(p++, 0x24020000); /* li v0,0 */
930 /* 808 YAMON print */
931 stl_p(p++, 0x03e06821); /* move t5,ra */
932 stl_p(p++, 0x00805821); /* move t3,a0 */
933 stl_p(p++, 0x00a05021); /* move t2,a1 */
934 stl_p(p++, 0x91440000); /* lbu a0,0(t2) */
935 stl_p(p++, 0x254a0001); /* addiu t2,t2,1 */
936 stl_p(p++, 0x10800005); /* beqz a0,834 */
937 stl_p(p++, 0x00000000); /* nop */
938 stl_p(p++, 0x0ff0021c); /* jal 870 */
939 stl_p(p++, 0x00000000); /* nop */
940 stl_p(p++, 0x1000fff9); /* b 814 */
941 stl_p(p++, 0x00000000); /* nop */
942 stl_p(p++, 0x01a00009); /* jalr t5 */
943 stl_p(p++, 0x01602021); /* move a0,t3 */
944 /* 0x83c YAMON print_count */
945 stl_p(p++, 0x03e06821); /* move t5,ra */
946 stl_p(p++, 0x00805821); /* move t3,a0 */
947 stl_p(p++, 0x00a05021); /* move t2,a1 */
948 stl_p(p++, 0x00c06021); /* move t4,a2 */
949 stl_p(p++, 0x91440000); /* lbu a0,0(t2) */
950 stl_p(p++, 0x0ff0021c); /* jal 870 */
951 stl_p(p++, 0x00000000); /* nop */
952 stl_p(p++, 0x254a0001); /* addiu t2,t2,1 */
953 stl_p(p++, 0x258cffff); /* addiu t4,t4,-1 */
954 stl_p(p++, 0x1580fffa); /* bnez t4,84c */
955 stl_p(p++, 0x00000000); /* nop */
956 stl_p(p++, 0x01a00009); /* jalr t5 */
957 stl_p(p++, 0x01602021); /* move a0,t3 */
958 /* 0x870 */
959 stl_p(p++, 0x3c08b800); /* lui t0,0xb400 */
960 stl_p(p++, 0x350803f8); /* ori t0,t0,0x3f8 */
961 stl_p(p++, 0x91090005); /* lbu t1,5(t0) */
962 stl_p(p++, 0x00000000); /* nop */
963 stl_p(p++, 0x31290040); /* andi t1,t1,0x40 */
964 stl_p(p++, 0x1120fffc); /* beqz t1,878 <outch+0x8> */
965 stl_p(p++, 0x00000000); /* nop */
966 stl_p(p++, 0x03e00009); /* jalr ra */
967 stl_p(p++, 0xa1040000); /* sb a0,0(t0) */
971 static void GCC_FMT_ATTR(3, 4) prom_set(uint32_t* prom_buf, int index,
972 const char *string, ...)
974 va_list ap;
975 int32_t table_addr;
977 if (index >= ENVP_NB_ENTRIES)
978 return;
980 if (string == NULL) {
981 prom_buf[index] = 0;
982 return;
985 table_addr = sizeof(int32_t) * ENVP_NB_ENTRIES + index * ENVP_ENTRY_SIZE;
986 prom_buf[index] = tswap32(ENVP_ADDR + table_addr);
988 va_start(ap, string);
989 vsnprintf((char *)prom_buf + table_addr, ENVP_ENTRY_SIZE, string, ap);
990 va_end(ap);
993 /* Kernel */
994 static int64_t load_kernel (void)
996 int64_t kernel_entry, kernel_high, initrd_size;
997 long kernel_size;
998 ram_addr_t initrd_offset;
999 int big_endian;
1000 uint32_t *prom_buf;
1001 long prom_size;
1002 int prom_index = 0;
1003 uint64_t (*xlate_to_kseg0) (void *opaque, uint64_t addr);
1005 #ifdef TARGET_WORDS_BIGENDIAN
1006 big_endian = 1;
1007 #else
1008 big_endian = 0;
1009 #endif
1011 kernel_size = load_elf(loaderparams.kernel_filename, NULL,
1012 cpu_mips_kseg0_to_phys, NULL,
1013 (uint64_t *)&kernel_entry, NULL,
1014 (uint64_t *)&kernel_high, big_endian, EM_MIPS, 1, 0);
1015 if (kernel_size < 0) {
1016 error_report("could not load kernel '%s': %s",
1017 loaderparams.kernel_filename,
1018 load_elf_strerror(kernel_size));
1019 exit(1);
1022 /* Check where the kernel has been linked */
1023 if (kernel_entry & 0x80000000ll) {
1024 if (kvm_enabled()) {
1025 error_report("KVM guest kernels must be linked in useg. "
1026 "Did you forget to enable CONFIG_KVM_GUEST?");
1027 exit(1);
1030 xlate_to_kseg0 = cpu_mips_phys_to_kseg0;
1031 } else {
1032 /* if kernel entry is in useg it is probably a KVM T&E kernel */
1033 mips_um_ksegs_enable();
1035 xlate_to_kseg0 = cpu_mips_kvm_um_phys_to_kseg0;
1038 /* load initrd */
1039 initrd_size = 0;
1040 initrd_offset = 0;
1041 if (loaderparams.initrd_filename) {
1042 initrd_size = get_image_size (loaderparams.initrd_filename);
1043 if (initrd_size > 0) {
1044 /* The kernel allocates the bootmap memory in the low memory after
1045 the initrd. It takes at most 128kiB for 2GB RAM and 4kiB
1046 pages. */
1047 initrd_offset = (loaderparams.ram_low_size - initrd_size
1048 - (128 * KiB)
1049 - ~INITRD_PAGE_MASK) & INITRD_PAGE_MASK;
1050 if (kernel_high >= initrd_offset) {
1051 error_report("memory too small for initial ram disk '%s'",
1052 loaderparams.initrd_filename);
1053 exit(1);
1055 initrd_size = load_image_targphys(loaderparams.initrd_filename,
1056 initrd_offset,
1057 ram_size - initrd_offset);
1059 if (initrd_size == (target_ulong) -1) {
1060 error_report("could not load initial ram disk '%s'",
1061 loaderparams.initrd_filename);
1062 exit(1);
1066 /* Setup prom parameters. */
1067 prom_size = ENVP_NB_ENTRIES * (sizeof(int32_t) + ENVP_ENTRY_SIZE);
1068 prom_buf = g_malloc(prom_size);
1070 prom_set(prom_buf, prom_index++, "%s", loaderparams.kernel_filename);
1071 if (initrd_size > 0) {
1072 prom_set(prom_buf, prom_index++, "rd_start=0x%" PRIx64 " rd_size=%" PRId64 " %s",
1073 xlate_to_kseg0(NULL, initrd_offset), initrd_size,
1074 loaderparams.kernel_cmdline);
1075 } else {
1076 prom_set(prom_buf, prom_index++, "%s", loaderparams.kernel_cmdline);
1079 prom_set(prom_buf, prom_index++, "memsize");
1080 prom_set(prom_buf, prom_index++, "%u", loaderparams.ram_low_size);
1082 prom_set(prom_buf, prom_index++, "ememsize");
1083 prom_set(prom_buf, prom_index++, "%u", loaderparams.ram_size);
1085 prom_set(prom_buf, prom_index++, "modetty0");
1086 prom_set(prom_buf, prom_index++, "38400n8r");
1087 prom_set(prom_buf, prom_index++, NULL);
1089 rom_add_blob_fixed("prom", prom_buf, prom_size,
1090 cpu_mips_kseg0_to_phys(NULL, ENVP_ADDR));
1092 g_free(prom_buf);
1093 return kernel_entry;
1096 static void malta_mips_config(MIPSCPU *cpu)
1098 MachineState *ms = MACHINE(qdev_get_machine());
1099 unsigned int smp_cpus = ms->smp.cpus;
1100 CPUMIPSState *env = &cpu->env;
1101 CPUState *cs = CPU(cpu);
1103 env->mvp->CP0_MVPConf0 |= ((smp_cpus - 1) << CP0MVPC0_PVPE) |
1104 ((smp_cpus * cs->nr_threads - 1) << CP0MVPC0_PTC);
1107 static void main_cpu_reset(void *opaque)
1109 MIPSCPU *cpu = opaque;
1110 CPUMIPSState *env = &cpu->env;
1112 cpu_reset(CPU(cpu));
1114 /* The bootloader does not need to be rewritten as it is located in a
1115 read only location. The kernel location and the arguments table
1116 location does not change. */
1117 if (loaderparams.kernel_filename) {
1118 env->CP0_Status &= ~(1 << CP0St_ERL);
1121 malta_mips_config(cpu);
1123 if (kvm_enabled()) {
1124 /* Start running from the bootloader we wrote to end of RAM */
1125 env->active_tc.PC = 0x40000000 + loaderparams.ram_low_size;
1129 static void create_cpu_without_cps(MachineState *ms,
1130 qemu_irq *cbus_irq, qemu_irq *i8259_irq)
1132 CPUMIPSState *env;
1133 MIPSCPU *cpu;
1134 int i;
1136 for (i = 0; i < ms->smp.cpus; i++) {
1137 cpu = MIPS_CPU(cpu_create(ms->cpu_type));
1139 /* Init internal devices */
1140 cpu_mips_irq_init_cpu(cpu);
1141 cpu_mips_clock_init(cpu);
1142 qemu_register_reset(main_cpu_reset, cpu);
1145 cpu = MIPS_CPU(first_cpu);
1146 env = &cpu->env;
1147 *i8259_irq = env->irq[2];
1148 *cbus_irq = env->irq[4];
1151 static void create_cps(MachineState *ms, MaltaState *s,
1152 qemu_irq *cbus_irq, qemu_irq *i8259_irq)
1154 Error *err = NULL;
1156 sysbus_init_child_obj(OBJECT(s), "cps", OBJECT(&s->cps), sizeof(s->cps),
1157 TYPE_MIPS_CPS);
1158 object_property_set_str(OBJECT(&s->cps), ms->cpu_type, "cpu-type", &err);
1159 object_property_set_int(OBJECT(&s->cps), ms->smp.cpus, "num-vp", &err);
1160 object_property_set_bool(OBJECT(&s->cps), true, "realized", &err);
1161 if (err != NULL) {
1162 error_report("%s", error_get_pretty(err));
1163 exit(1);
1166 sysbus_mmio_map_overlap(SYS_BUS_DEVICE(&s->cps), 0, 0, 1);
1168 *i8259_irq = get_cps_irq(&s->cps, 3);
1169 *cbus_irq = NULL;
1172 static void mips_create_cpu(MachineState *ms, MaltaState *s,
1173 qemu_irq *cbus_irq, qemu_irq *i8259_irq)
1175 if ((ms->smp.cpus > 1) && cpu_supports_cps_smp(ms->cpu_type)) {
1176 create_cps(ms, s, cbus_irq, i8259_irq);
1177 } else {
1178 create_cpu_without_cps(ms, cbus_irq, i8259_irq);
1182 static
1183 void mips_malta_init(MachineState *machine)
1185 ram_addr_t ram_size = machine->ram_size;
1186 ram_addr_t ram_low_size;
1187 const char *kernel_filename = machine->kernel_filename;
1188 const char *kernel_cmdline = machine->kernel_cmdline;
1189 const char *initrd_filename = machine->initrd_filename;
1190 char *filename;
1191 PFlashCFI01 *fl;
1192 MemoryRegion *system_memory = get_system_memory();
1193 MemoryRegion *ram_high = g_new(MemoryRegion, 1);
1194 MemoryRegion *ram_low_preio = g_new(MemoryRegion, 1);
1195 MemoryRegion *ram_low_postio;
1196 MemoryRegion *bios, *bios_copy = g_new(MemoryRegion, 1);
1197 const size_t smbus_eeprom_size = 8 * 256;
1198 uint8_t *smbus_eeprom_buf = g_malloc0(smbus_eeprom_size);
1199 int64_t kernel_entry, bootloader_run_addr;
1200 PCIBus *pci_bus;
1201 ISABus *isa_bus;
1202 qemu_irq *isa_irq;
1203 qemu_irq cbus_irq, i8259_irq;
1204 int piix4_devfn;
1205 I2CBus *smbus;
1206 DriveInfo *dinfo;
1207 DriveInfo *hd[MAX_IDE_BUS * MAX_IDE_DEVS];
1208 int fl_idx = 0;
1209 int be;
1211 DeviceState *dev = qdev_create(NULL, TYPE_MIPS_MALTA);
1212 MaltaState *s = MIPS_MALTA(dev);
1214 /* The whole address space decoded by the GT-64120A doesn't generate
1215 exception when accessing invalid memory. Create an empty slot to
1216 emulate this feature. */
1217 empty_slot_init(0, 0x20000000);
1219 qdev_init_nofail(dev);
1221 /* create CPU */
1222 mips_create_cpu(machine, s, &cbus_irq, &i8259_irq);
1224 /* allocate RAM */
1225 if (ram_size > 2 * GiB) {
1226 error_report("Too much memory for this machine: %" PRId64 "MB,"
1227 " maximum 2048MB", ram_size / MiB);
1228 exit(1);
1231 /* register RAM at high address where it is undisturbed by IO */
1232 memory_region_allocate_system_memory(ram_high, NULL, "mips_malta.ram",
1233 ram_size);
1234 memory_region_add_subregion(system_memory, 0x80000000, ram_high);
1236 /* alias for pre IO hole access */
1237 memory_region_init_alias(ram_low_preio, NULL, "mips_malta_low_preio.ram",
1238 ram_high, 0, MIN(ram_size, 256 * MiB));
1239 memory_region_add_subregion(system_memory, 0, ram_low_preio);
1241 /* alias for post IO hole access, if there is enough RAM */
1242 if (ram_size > 512 * MiB) {
1243 ram_low_postio = g_new(MemoryRegion, 1);
1244 memory_region_init_alias(ram_low_postio, NULL,
1245 "mips_malta_low_postio.ram",
1246 ram_high, 512 * MiB,
1247 ram_size - 512 * MiB);
1248 memory_region_add_subregion(system_memory, 512 * MiB,
1249 ram_low_postio);
1252 #ifdef TARGET_WORDS_BIGENDIAN
1253 be = 1;
1254 #else
1255 be = 0;
1256 #endif
1258 /* FPGA */
1260 /* The CBUS UART is attached to the MIPS CPU INT2 pin, ie interrupt 4 */
1261 malta_fpga_init(system_memory, FPGA_ADDRESS, cbus_irq, serial_hd(2));
1263 /* Load firmware in flash / BIOS. */
1264 dinfo = drive_get(IF_PFLASH, 0, fl_idx);
1265 fl = pflash_cfi01_register(FLASH_ADDRESS, "mips_malta.bios",
1266 FLASH_SIZE,
1267 dinfo ? blk_by_legacy_dinfo(dinfo) : NULL,
1268 65536,
1269 4, 0x0000, 0x0000, 0x0000, 0x0000, be);
1270 bios = pflash_cfi01_get_memory(fl);
1271 fl_idx++;
1272 if (kernel_filename) {
1273 ram_low_size = MIN(ram_size, 256 * MiB);
1274 /* For KVM we reserve 1MB of RAM for running bootloader */
1275 if (kvm_enabled()) {
1276 ram_low_size -= 0x100000;
1277 bootloader_run_addr = 0x40000000 + ram_low_size;
1278 } else {
1279 bootloader_run_addr = 0xbfc00000;
1282 /* Write a small bootloader to the flash location. */
1283 loaderparams.ram_size = ram_size;
1284 loaderparams.ram_low_size = ram_low_size;
1285 loaderparams.kernel_filename = kernel_filename;
1286 loaderparams.kernel_cmdline = kernel_cmdline;
1287 loaderparams.initrd_filename = initrd_filename;
1288 kernel_entry = load_kernel();
1290 if (!cpu_supports_isa(machine->cpu_type, ISA_NANOMIPS32)) {
1291 write_bootloader(memory_region_get_ram_ptr(bios),
1292 bootloader_run_addr, kernel_entry);
1293 } else {
1294 write_bootloader_nanomips(memory_region_get_ram_ptr(bios),
1295 bootloader_run_addr, kernel_entry);
1297 if (kvm_enabled()) {
1298 /* Write the bootloader code @ the end of RAM, 1MB reserved */
1299 write_bootloader(memory_region_get_ram_ptr(ram_low_preio) +
1300 ram_low_size,
1301 bootloader_run_addr, kernel_entry);
1303 } else {
1304 target_long bios_size = FLASH_SIZE;
1305 /* The flash region isn't executable from a KVM guest */
1306 if (kvm_enabled()) {
1307 error_report("KVM enabled but no -kernel argument was specified. "
1308 "Booting from flash is not supported with KVM.");
1309 exit(1);
1311 /* Load firmware from flash. */
1312 if (!dinfo) {
1313 /* Load a BIOS image. */
1314 if (bios_name == NULL) {
1315 bios_name = BIOS_FILENAME;
1317 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
1318 if (filename) {
1319 bios_size = load_image_targphys(filename, FLASH_ADDRESS,
1320 BIOS_SIZE);
1321 g_free(filename);
1322 } else {
1323 bios_size = -1;
1325 if ((bios_size < 0 || bios_size > BIOS_SIZE) &&
1326 !kernel_filename && !qtest_enabled()) {
1327 error_report("Could not load MIPS bios '%s', and no "
1328 "-kernel argument was specified", bios_name);
1329 exit(1);
1332 /* In little endian mode the 32bit words in the bios are swapped,
1333 a neat trick which allows bi-endian firmware. */
1334 #ifndef TARGET_WORDS_BIGENDIAN
1336 uint32_t *end, *addr;
1337 const size_t swapsize = MIN(bios_size, 0x3e0000);
1338 addr = rom_ptr(FLASH_ADDRESS, swapsize);
1339 if (!addr) {
1340 addr = memory_region_get_ram_ptr(bios);
1342 end = (void *)addr + swapsize;
1343 while (addr < end) {
1344 bswap32s(addr);
1345 addr++;
1348 #endif
1352 * Map the BIOS at a 2nd physical location, as on the real board.
1353 * Copy it so that we can patch in the MIPS revision, which cannot be
1354 * handled by an overlapping region as the resulting ROM code subpage
1355 * regions are not executable.
1357 memory_region_init_ram(bios_copy, NULL, "bios.1fc", BIOS_SIZE,
1358 &error_fatal);
1359 if (!rom_copy(memory_region_get_ram_ptr(bios_copy),
1360 FLASH_ADDRESS, BIOS_SIZE)) {
1361 memcpy(memory_region_get_ram_ptr(bios_copy),
1362 memory_region_get_ram_ptr(bios), BIOS_SIZE);
1364 memory_region_set_readonly(bios_copy, true);
1365 memory_region_add_subregion(system_memory, RESET_ADDRESS, bios_copy);
1367 /* Board ID = 0x420 (Malta Board with CoreLV) */
1368 stl_p(memory_region_get_ram_ptr(bios_copy) + 0x10, 0x00000420);
1371 * We have a circular dependency problem: pci_bus depends on isa_irq,
1372 * isa_irq is provided by i8259, i8259 depends on ISA, ISA depends
1373 * on piix4, and piix4 depends on pci_bus. To stop the cycle we have
1374 * qemu_irq_proxy() adds an extra bit of indirection, allowing us
1375 * to resolve the isa_irq -> i8259 dependency after i8259 is initialized.
1377 isa_irq = qemu_irq_proxy(&s->i8259, 16);
1379 /* Northbridge */
1380 pci_bus = gt64120_register(isa_irq);
1382 /* Southbridge */
1383 ide_drive_get(hd, ARRAY_SIZE(hd));
1385 piix4_devfn = piix4_init(pci_bus, &isa_bus, 80);
1387 /* Interrupt controller */
1388 /* The 8259 is attached to the MIPS CPU INT0 pin, ie interrupt 2 */
1389 s->i8259 = i8259_init(isa_bus, i8259_irq);
1391 isa_bus_irqs(isa_bus, s->i8259);
1392 pci_piix4_ide_init(pci_bus, hd, piix4_devfn + 1);
1393 pci_create_simple(pci_bus, piix4_devfn + 2, "piix4-usb-uhci");
1394 smbus = piix4_pm_init(pci_bus, piix4_devfn + 3, 0x1100,
1395 isa_get_irq(NULL, 9), NULL, 0, NULL);
1396 pit = i8254_pit_init(isa_bus, 0x40, 0, NULL);
1397 i8257_dma_init(isa_bus, 0);
1398 mc146818_rtc_init(isa_bus, 2000, NULL);
1400 /* generate SPD EEPROM data */
1401 generate_eeprom_spd(&smbus_eeprom_buf[0 * 256], ram_size);
1402 generate_eeprom_serial(&smbus_eeprom_buf[6 * 256]);
1403 smbus_eeprom_init(smbus, 8, smbus_eeprom_buf, smbus_eeprom_size);
1404 g_free(smbus_eeprom_buf);
1406 /* Super I/O: SMS FDC37M817 */
1407 isa_create_simple(isa_bus, TYPE_FDC37M81X_SUPERIO);
1409 /* Network card */
1410 network_init(pci_bus);
1412 /* Optional PCI video card */
1413 pci_vga_init(pci_bus);
1416 static const TypeInfo mips_malta_device = {
1417 .name = TYPE_MIPS_MALTA,
1418 .parent = TYPE_SYS_BUS_DEVICE,
1419 .instance_size = sizeof(MaltaState),
1422 static void mips_malta_machine_init(MachineClass *mc)
1424 mc->desc = "MIPS Malta Core LV";
1425 mc->init = mips_malta_init;
1426 mc->block_default_type = IF_IDE;
1427 mc->max_cpus = 16;
1428 mc->is_default = 1;
1429 #ifdef TARGET_MIPS64
1430 mc->default_cpu_type = MIPS_CPU_TYPE_NAME("20Kc");
1431 #else
1432 mc->default_cpu_type = MIPS_CPU_TYPE_NAME("24Kf");
1433 #endif
1436 DEFINE_MACHINE("malta", mips_malta_machine_init)
1438 static void mips_malta_register_types(void)
1440 type_register_static(&mips_malta_device);
1443 type_init(mips_malta_register_types)