2
0

sun4u.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929
  1. /*
  2. * QEMU Sun4u/Sun4v System Emulator
  3. *
  4. * Copyright (c) 2005 Fabrice Bellard
  5. *
  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:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  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.
  23. */
  24. #include "hw.h"
  25. #include "pci.h"
  26. #include "apb_pci.h"
  27. #include "pc.h"
  28. #include "nvram.h"
  29. #include "fdc.h"
  30. #include "net.h"
  31. #include "qemu-timer.h"
  32. #include "sysemu.h"
  33. #include "boards.h"
  34. #include "firmware_abi.h"
  35. #include "fw_cfg.h"
  36. #include "sysbus.h"
  37. #include "ide.h"
  38. #include "loader.h"
  39. #include "elf.h"
  40. #include "blockdev.h"
  41. #include "exec-memory.h"
  42. //#define DEBUG_IRQ
  43. //#define DEBUG_EBUS
  44. //#define DEBUG_TIMER
  45. #ifdef DEBUG_IRQ
  46. #define CPUIRQ_DPRINTF(fmt, ...) \
  47. do { printf("CPUIRQ: " fmt , ## __VA_ARGS__); } while (0)
  48. #else
  49. #define CPUIRQ_DPRINTF(fmt, ...)
  50. #endif
  51. #ifdef DEBUG_EBUS
  52. #define EBUS_DPRINTF(fmt, ...) \
  53. do { printf("EBUS: " fmt , ## __VA_ARGS__); } while (0)
  54. #else
  55. #define EBUS_DPRINTF(fmt, ...)
  56. #endif
  57. #ifdef DEBUG_TIMER
  58. #define TIMER_DPRINTF(fmt, ...) \
  59. do { printf("TIMER: " fmt , ## __VA_ARGS__); } while (0)
  60. #else
  61. #define TIMER_DPRINTF(fmt, ...)
  62. #endif
  63. #define KERNEL_LOAD_ADDR 0x00404000
  64. #define CMDLINE_ADDR 0x003ff000
  65. #define INITRD_LOAD_ADDR 0x00300000
  66. #define PROM_SIZE_MAX (4 * 1024 * 1024)
  67. #define PROM_VADDR 0x000ffd00000ULL
  68. #define APB_SPECIAL_BASE 0x1fe00000000ULL
  69. #define APB_MEM_BASE 0x1ff00000000ULL
  70. #define APB_PCI_IO_BASE (APB_SPECIAL_BASE + 0x02000000ULL)
  71. #define PROM_FILENAME "openbios-sparc64"
  72. #define NVRAM_SIZE 0x2000
  73. #define MAX_IDE_BUS 2
  74. #define BIOS_CFG_IOPORT 0x510
  75. #define FW_CFG_SPARC64_WIDTH (FW_CFG_ARCH_LOCAL + 0x00)
  76. #define FW_CFG_SPARC64_HEIGHT (FW_CFG_ARCH_LOCAL + 0x01)
  77. #define FW_CFG_SPARC64_DEPTH (FW_CFG_ARCH_LOCAL + 0x02)
  78. #define MAX_PILS 16
  79. #define TICK_MAX 0x7fffffffffffffffULL
  80. struct hwdef {
  81. const char * const default_cpu_model;
  82. uint16_t machine_id;
  83. uint64_t prom_addr;
  84. uint64_t console_serial_base;
  85. };
  86. typedef struct EbusState {
  87. PCIDevice pci_dev;
  88. MemoryRegion bar0;
  89. MemoryRegion bar1;
  90. } EbusState;
  91. int DMA_get_channel_mode (int nchan)
  92. {
  93. return 0;
  94. }
  95. int DMA_read_memory (int nchan, void *buf, int pos, int size)
  96. {
  97. return 0;
  98. }
  99. int DMA_write_memory (int nchan, void *buf, int pos, int size)
  100. {
  101. return 0;
  102. }
  103. void DMA_hold_DREQ (int nchan) {}
  104. void DMA_release_DREQ (int nchan) {}
  105. void DMA_schedule(int nchan) {}
  106. void DMA_init(int high_page_enable, qemu_irq *cpu_request_exit)
  107. {
  108. }
  109. void DMA_register_channel (int nchan,
  110. DMA_transfer_handler transfer_handler,
  111. void *opaque)
  112. {
  113. }
  114. static int fw_cfg_boot_set(void *opaque, const char *boot_device)
  115. {
  116. fw_cfg_add_i16(opaque, FW_CFG_BOOT_DEVICE, boot_device[0]);
  117. return 0;
  118. }
  119. static int sun4u_NVRAM_set_params(M48t59State *nvram, uint16_t NVRAM_size,
  120. const char *arch, ram_addr_t RAM_size,
  121. const char *boot_devices,
  122. uint32_t kernel_image, uint32_t kernel_size,
  123. const char *cmdline,
  124. uint32_t initrd_image, uint32_t initrd_size,
  125. uint32_t NVRAM_image,
  126. int width, int height, int depth,
  127. const uint8_t *macaddr)
  128. {
  129. unsigned int i;
  130. uint32_t start, end;
  131. uint8_t image[0x1ff0];
  132. struct OpenBIOS_nvpart_v1 *part_header;
  133. memset(image, '\0', sizeof(image));
  134. start = 0;
  135. // OpenBIOS nvram variables
  136. // Variable partition
  137. part_header = (struct OpenBIOS_nvpart_v1 *)&image[start];
  138. part_header->signature = OPENBIOS_PART_SYSTEM;
  139. pstrcpy(part_header->name, sizeof(part_header->name), "system");
  140. end = start + sizeof(struct OpenBIOS_nvpart_v1);
  141. for (i = 0; i < nb_prom_envs; i++)
  142. end = OpenBIOS_set_var(image, end, prom_envs[i]);
  143. // End marker
  144. image[end++] = '\0';
  145. end = start + ((end - start + 15) & ~15);
  146. OpenBIOS_finish_partition(part_header, end - start);
  147. // free partition
  148. start = end;
  149. part_header = (struct OpenBIOS_nvpart_v1 *)&image[start];
  150. part_header->signature = OPENBIOS_PART_FREE;
  151. pstrcpy(part_header->name, sizeof(part_header->name), "free");
  152. end = 0x1fd0;
  153. OpenBIOS_finish_partition(part_header, end - start);
  154. Sun_init_header((struct Sun_nvram *)&image[0x1fd8], macaddr, 0x80);
  155. for (i = 0; i < sizeof(image); i++)
  156. m48t59_write(nvram, i, image[i]);
  157. return 0;
  158. }
  159. static unsigned long sun4u_load_kernel(const char *kernel_filename,
  160. const char *initrd_filename,
  161. ram_addr_t RAM_size, long *initrd_size)
  162. {
  163. int linux_boot;
  164. unsigned int i;
  165. long kernel_size;
  166. uint8_t *ptr;
  167. linux_boot = (kernel_filename != NULL);
  168. kernel_size = 0;
  169. if (linux_boot) {
  170. int bswap_needed;
  171. #ifdef BSWAP_NEEDED
  172. bswap_needed = 1;
  173. #else
  174. bswap_needed = 0;
  175. #endif
  176. kernel_size = load_elf(kernel_filename, NULL, NULL, NULL,
  177. NULL, NULL, 1, ELF_MACHINE, 0);
  178. if (kernel_size < 0)
  179. kernel_size = load_aout(kernel_filename, KERNEL_LOAD_ADDR,
  180. RAM_size - KERNEL_LOAD_ADDR, bswap_needed,
  181. TARGET_PAGE_SIZE);
  182. if (kernel_size < 0)
  183. kernel_size = load_image_targphys(kernel_filename,
  184. KERNEL_LOAD_ADDR,
  185. RAM_size - KERNEL_LOAD_ADDR);
  186. if (kernel_size < 0) {
  187. fprintf(stderr, "qemu: could not load kernel '%s'\n",
  188. kernel_filename);
  189. exit(1);
  190. }
  191. /* load initrd */
  192. *initrd_size = 0;
  193. if (initrd_filename) {
  194. *initrd_size = load_image_targphys(initrd_filename,
  195. INITRD_LOAD_ADDR,
  196. RAM_size - INITRD_LOAD_ADDR);
  197. if (*initrd_size < 0) {
  198. fprintf(stderr, "qemu: could not load initial ram disk '%s'\n",
  199. initrd_filename);
  200. exit(1);
  201. }
  202. }
  203. if (*initrd_size > 0) {
  204. for (i = 0; i < 64 * TARGET_PAGE_SIZE; i += TARGET_PAGE_SIZE) {
  205. ptr = rom_ptr(KERNEL_LOAD_ADDR + i);
  206. if (ldl_p(ptr + 8) == 0x48647253) { /* HdrS */
  207. stl_p(ptr + 24, INITRD_LOAD_ADDR + KERNEL_LOAD_ADDR - 0x4000);
  208. stl_p(ptr + 28, *initrd_size);
  209. break;
  210. }
  211. }
  212. }
  213. }
  214. return kernel_size;
  215. }
  216. void cpu_check_irqs(CPUState *env)
  217. {
  218. uint32_t pil = env->pil_in |
  219. (env->softint & ~(SOFTINT_TIMER | SOFTINT_STIMER));
  220. /* check if TM or SM in SOFTINT are set
  221. setting these also causes interrupt 14 */
  222. if (env->softint & (SOFTINT_TIMER | SOFTINT_STIMER)) {
  223. pil |= 1 << 14;
  224. }
  225. /* The bit corresponding to psrpil is (1<< psrpil), the next bit
  226. is (2 << psrpil). */
  227. if (pil < (2 << env->psrpil)){
  228. if (env->interrupt_request & CPU_INTERRUPT_HARD) {
  229. CPUIRQ_DPRINTF("Reset CPU IRQ (current interrupt %x)\n",
  230. env->interrupt_index);
  231. env->interrupt_index = 0;
  232. cpu_reset_interrupt(env, CPU_INTERRUPT_HARD);
  233. }
  234. return;
  235. }
  236. if (cpu_interrupts_enabled(env)) {
  237. unsigned int i;
  238. for (i = 15; i > env->psrpil; i--) {
  239. if (pil & (1 << i)) {
  240. int old_interrupt = env->interrupt_index;
  241. int new_interrupt = TT_EXTINT | i;
  242. if (env->tl > 0 && cpu_tsptr(env)->tt > new_interrupt) {
  243. CPUIRQ_DPRINTF("Not setting CPU IRQ: TL=%d "
  244. "current %x >= pending %x\n",
  245. env->tl, cpu_tsptr(env)->tt, new_interrupt);
  246. } else if (old_interrupt != new_interrupt) {
  247. env->interrupt_index = new_interrupt;
  248. CPUIRQ_DPRINTF("Set CPU IRQ %d old=%x new=%x\n", i,
  249. old_interrupt, new_interrupt);
  250. cpu_interrupt(env, CPU_INTERRUPT_HARD);
  251. }
  252. break;
  253. }
  254. }
  255. } else if (env->interrupt_request & CPU_INTERRUPT_HARD) {
  256. CPUIRQ_DPRINTF("Interrupts disabled, pil=%08x pil_in=%08x softint=%08x "
  257. "current interrupt %x\n",
  258. pil, env->pil_in, env->softint, env->interrupt_index);
  259. env->interrupt_index = 0;
  260. cpu_reset_interrupt(env, CPU_INTERRUPT_HARD);
  261. }
  262. }
  263. static void cpu_kick_irq(CPUState *env)
  264. {
  265. env->halted = 0;
  266. cpu_check_irqs(env);
  267. qemu_cpu_kick(env);
  268. }
  269. static void cpu_set_irq(void *opaque, int irq, int level)
  270. {
  271. CPUState *env = opaque;
  272. if (level) {
  273. CPUIRQ_DPRINTF("Raise CPU IRQ %d\n", irq);
  274. env->pil_in |= 1 << irq;
  275. cpu_kick_irq(env);
  276. } else {
  277. CPUIRQ_DPRINTF("Lower CPU IRQ %d\n", irq);
  278. env->pil_in &= ~(1 << irq);
  279. cpu_check_irqs(env);
  280. }
  281. }
  282. typedef struct ResetData {
  283. CPUState *env;
  284. uint64_t prom_addr;
  285. } ResetData;
  286. void cpu_put_timer(QEMUFile *f, CPUTimer *s)
  287. {
  288. qemu_put_be32s(f, &s->frequency);
  289. qemu_put_be32s(f, &s->disabled);
  290. qemu_put_be64s(f, &s->disabled_mask);
  291. qemu_put_sbe64s(f, &s->clock_offset);
  292. qemu_put_timer(f, s->qtimer);
  293. }
  294. void cpu_get_timer(QEMUFile *f, CPUTimer *s)
  295. {
  296. qemu_get_be32s(f, &s->frequency);
  297. qemu_get_be32s(f, &s->disabled);
  298. qemu_get_be64s(f, &s->disabled_mask);
  299. qemu_get_sbe64s(f, &s->clock_offset);
  300. qemu_get_timer(f, s->qtimer);
  301. }
  302. static CPUTimer* cpu_timer_create(const char* name, CPUState *env,
  303. QEMUBHFunc *cb, uint32_t frequency,
  304. uint64_t disabled_mask)
  305. {
  306. CPUTimer *timer = g_malloc0(sizeof (CPUTimer));
  307. timer->name = name;
  308. timer->frequency = frequency;
  309. timer->disabled_mask = disabled_mask;
  310. timer->disabled = 1;
  311. timer->clock_offset = qemu_get_clock_ns(vm_clock);
  312. timer->qtimer = qemu_new_timer_ns(vm_clock, cb, env);
  313. return timer;
  314. }
  315. static void cpu_timer_reset(CPUTimer *timer)
  316. {
  317. timer->disabled = 1;
  318. timer->clock_offset = qemu_get_clock_ns(vm_clock);
  319. qemu_del_timer(timer->qtimer);
  320. }
  321. static void main_cpu_reset(void *opaque)
  322. {
  323. ResetData *s = (ResetData *)opaque;
  324. CPUState *env = s->env;
  325. static unsigned int nr_resets;
  326. cpu_reset(env);
  327. cpu_timer_reset(env->tick);
  328. cpu_timer_reset(env->stick);
  329. cpu_timer_reset(env->hstick);
  330. env->gregs[1] = 0; // Memory start
  331. env->gregs[2] = ram_size; // Memory size
  332. env->gregs[3] = 0; // Machine description XXX
  333. if (nr_resets++ == 0) {
  334. /* Power on reset */
  335. env->pc = s->prom_addr + 0x20ULL;
  336. } else {
  337. env->pc = s->prom_addr + 0x40ULL;
  338. }
  339. env->npc = env->pc + 4;
  340. }
  341. static void tick_irq(void *opaque)
  342. {
  343. CPUState *env = opaque;
  344. CPUTimer* timer = env->tick;
  345. if (timer->disabled) {
  346. CPUIRQ_DPRINTF("tick_irq: softint disabled\n");
  347. return;
  348. } else {
  349. CPUIRQ_DPRINTF("tick: fire\n");
  350. }
  351. env->softint |= SOFTINT_TIMER;
  352. cpu_kick_irq(env);
  353. }
  354. static void stick_irq(void *opaque)
  355. {
  356. CPUState *env = opaque;
  357. CPUTimer* timer = env->stick;
  358. if (timer->disabled) {
  359. CPUIRQ_DPRINTF("stick_irq: softint disabled\n");
  360. return;
  361. } else {
  362. CPUIRQ_DPRINTF("stick: fire\n");
  363. }
  364. env->softint |= SOFTINT_STIMER;
  365. cpu_kick_irq(env);
  366. }
  367. static void hstick_irq(void *opaque)
  368. {
  369. CPUState *env = opaque;
  370. CPUTimer* timer = env->hstick;
  371. if (timer->disabled) {
  372. CPUIRQ_DPRINTF("hstick_irq: softint disabled\n");
  373. return;
  374. } else {
  375. CPUIRQ_DPRINTF("hstick: fire\n");
  376. }
  377. env->softint |= SOFTINT_STIMER;
  378. cpu_kick_irq(env);
  379. }
  380. static int64_t cpu_to_timer_ticks(int64_t cpu_ticks, uint32_t frequency)
  381. {
  382. return muldiv64(cpu_ticks, get_ticks_per_sec(), frequency);
  383. }
  384. static uint64_t timer_to_cpu_ticks(int64_t timer_ticks, uint32_t frequency)
  385. {
  386. return muldiv64(timer_ticks, frequency, get_ticks_per_sec());
  387. }
  388. void cpu_tick_set_count(CPUTimer *timer, uint64_t count)
  389. {
  390. uint64_t real_count = count & ~timer->disabled_mask;
  391. uint64_t disabled_bit = count & timer->disabled_mask;
  392. int64_t vm_clock_offset = qemu_get_clock_ns(vm_clock) -
  393. cpu_to_timer_ticks(real_count, timer->frequency);
  394. TIMER_DPRINTF("%s set_count count=0x%016lx (%s) p=%p\n",
  395. timer->name, real_count,
  396. timer->disabled?"disabled":"enabled", timer);
  397. timer->disabled = disabled_bit ? 1 : 0;
  398. timer->clock_offset = vm_clock_offset;
  399. }
  400. uint64_t cpu_tick_get_count(CPUTimer *timer)
  401. {
  402. uint64_t real_count = timer_to_cpu_ticks(
  403. qemu_get_clock_ns(vm_clock) - timer->clock_offset,
  404. timer->frequency);
  405. TIMER_DPRINTF("%s get_count count=0x%016lx (%s) p=%p\n",
  406. timer->name, real_count,
  407. timer->disabled?"disabled":"enabled", timer);
  408. if (timer->disabled)
  409. real_count |= timer->disabled_mask;
  410. return real_count;
  411. }
  412. void cpu_tick_set_limit(CPUTimer *timer, uint64_t limit)
  413. {
  414. int64_t now = qemu_get_clock_ns(vm_clock);
  415. uint64_t real_limit = limit & ~timer->disabled_mask;
  416. timer->disabled = (limit & timer->disabled_mask) ? 1 : 0;
  417. int64_t expires = cpu_to_timer_ticks(real_limit, timer->frequency) +
  418. timer->clock_offset;
  419. if (expires < now) {
  420. expires = now + 1;
  421. }
  422. TIMER_DPRINTF("%s set_limit limit=0x%016lx (%s) p=%p "
  423. "called with limit=0x%016lx at 0x%016lx (delta=0x%016lx)\n",
  424. timer->name, real_limit,
  425. timer->disabled?"disabled":"enabled",
  426. timer, limit,
  427. timer_to_cpu_ticks(now - timer->clock_offset,
  428. timer->frequency),
  429. timer_to_cpu_ticks(expires - now, timer->frequency));
  430. if (!real_limit) {
  431. TIMER_DPRINTF("%s set_limit limit=ZERO - not starting timer\n",
  432. timer->name);
  433. qemu_del_timer(timer->qtimer);
  434. } else if (timer->disabled) {
  435. qemu_del_timer(timer->qtimer);
  436. } else {
  437. qemu_mod_timer(timer->qtimer, expires);
  438. }
  439. }
  440. static void dummy_isa_irq_handler(void *opaque, int n, int level)
  441. {
  442. }
  443. /* EBUS (Eight bit bus) bridge */
  444. static void
  445. pci_ebus_init(PCIBus *bus, int devfn)
  446. {
  447. qemu_irq *isa_irq;
  448. pci_create_simple(bus, devfn, "ebus");
  449. isa_irq = qemu_allocate_irqs(dummy_isa_irq_handler, NULL, 16);
  450. isa_bus_irqs(isa_irq);
  451. }
  452. static int
  453. pci_ebus_init1(PCIDevice *pci_dev)
  454. {
  455. EbusState *s = DO_UPCAST(EbusState, pci_dev, pci_dev);
  456. isa_bus_new(&pci_dev->qdev, pci_address_space_io(pci_dev));
  457. pci_dev->config[0x04] = 0x06; // command = bus master, pci mem
  458. pci_dev->config[0x05] = 0x00;
  459. pci_dev->config[0x06] = 0xa0; // status = fast back-to-back, 66MHz, no error
  460. pci_dev->config[0x07] = 0x03; // status = medium devsel
  461. pci_dev->config[0x09] = 0x00; // programming i/f
  462. pci_dev->config[0x0D] = 0x0a; // latency_timer
  463. isa_mmio_setup(&s->bar0, 0x1000000);
  464. pci_register_bar(pci_dev, 0, PCI_BASE_ADDRESS_SPACE_MEMORY, &s->bar0);
  465. isa_mmio_setup(&s->bar1, 0x800000);
  466. pci_register_bar(pci_dev, 1, PCI_BASE_ADDRESS_SPACE_MEMORY, &s->bar1);
  467. return 0;
  468. }
  469. static PCIDeviceInfo ebus_info = {
  470. .qdev.name = "ebus",
  471. .qdev.size = sizeof(EbusState),
  472. .init = pci_ebus_init1,
  473. .vendor_id = PCI_VENDOR_ID_SUN,
  474. .device_id = PCI_DEVICE_ID_SUN_EBUS,
  475. .revision = 0x01,
  476. .class_id = PCI_CLASS_BRIDGE_OTHER,
  477. };
  478. static void pci_ebus_register(void)
  479. {
  480. pci_qdev_register(&ebus_info);
  481. }
  482. device_init(pci_ebus_register);
  483. typedef struct PROMState {
  484. SysBusDevice busdev;
  485. MemoryRegion prom;
  486. } PROMState;
  487. static uint64_t translate_prom_address(void *opaque, uint64_t addr)
  488. {
  489. target_phys_addr_t *base_addr = (target_phys_addr_t *)opaque;
  490. return addr + *base_addr - PROM_VADDR;
  491. }
  492. /* Boot PROM (OpenBIOS) */
  493. static void prom_init(target_phys_addr_t addr, const char *bios_name)
  494. {
  495. DeviceState *dev;
  496. SysBusDevice *s;
  497. char *filename;
  498. int ret;
  499. dev = qdev_create(NULL, "openprom");
  500. qdev_init_nofail(dev);
  501. s = sysbus_from_qdev(dev);
  502. sysbus_mmio_map(s, 0, addr);
  503. /* load boot prom */
  504. if (bios_name == NULL) {
  505. bios_name = PROM_FILENAME;
  506. }
  507. filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
  508. if (filename) {
  509. ret = load_elf(filename, translate_prom_address, &addr,
  510. NULL, NULL, NULL, 1, ELF_MACHINE, 0);
  511. if (ret < 0 || ret > PROM_SIZE_MAX) {
  512. ret = load_image_targphys(filename, addr, PROM_SIZE_MAX);
  513. }
  514. g_free(filename);
  515. } else {
  516. ret = -1;
  517. }
  518. if (ret < 0 || ret > PROM_SIZE_MAX) {
  519. fprintf(stderr, "qemu: could not load prom '%s'\n", bios_name);
  520. exit(1);
  521. }
  522. }
  523. static int prom_init1(SysBusDevice *dev)
  524. {
  525. PROMState *s = FROM_SYSBUS(PROMState, dev);
  526. memory_region_init_ram(&s->prom, NULL, "sun4u.prom", PROM_SIZE_MAX);
  527. memory_region_set_readonly(&s->prom, true);
  528. sysbus_init_mmio_region(dev, &s->prom);
  529. return 0;
  530. }
  531. static SysBusDeviceInfo prom_info = {
  532. .init = prom_init1,
  533. .qdev.name = "openprom",
  534. .qdev.size = sizeof(PROMState),
  535. .qdev.props = (Property[]) {
  536. {/* end of property list */}
  537. }
  538. };
  539. static void prom_register_devices(void)
  540. {
  541. sysbus_register_withprop(&prom_info);
  542. }
  543. device_init(prom_register_devices);
  544. typedef struct RamDevice
  545. {
  546. SysBusDevice busdev;
  547. MemoryRegion ram;
  548. uint64_t size;
  549. } RamDevice;
  550. /* System RAM */
  551. static int ram_init1(SysBusDevice *dev)
  552. {
  553. RamDevice *d = FROM_SYSBUS(RamDevice, dev);
  554. memory_region_init_ram(&d->ram, NULL, "sun4u.ram", d->size);
  555. sysbus_init_mmio_region(dev, &d->ram);
  556. return 0;
  557. }
  558. static void ram_init(target_phys_addr_t addr, ram_addr_t RAM_size)
  559. {
  560. DeviceState *dev;
  561. SysBusDevice *s;
  562. RamDevice *d;
  563. /* allocate RAM */
  564. dev = qdev_create(NULL, "memory");
  565. s = sysbus_from_qdev(dev);
  566. d = FROM_SYSBUS(RamDevice, s);
  567. d->size = RAM_size;
  568. qdev_init_nofail(dev);
  569. sysbus_mmio_map(s, 0, addr);
  570. }
  571. static SysBusDeviceInfo ram_info = {
  572. .init = ram_init1,
  573. .qdev.name = "memory",
  574. .qdev.size = sizeof(RamDevice),
  575. .qdev.props = (Property[]) {
  576. DEFINE_PROP_UINT64("size", RamDevice, size, 0),
  577. DEFINE_PROP_END_OF_LIST(),
  578. }
  579. };
  580. static void ram_register_devices(void)
  581. {
  582. sysbus_register_withprop(&ram_info);
  583. }
  584. device_init(ram_register_devices);
  585. static CPUState *cpu_devinit(const char *cpu_model, const struct hwdef *hwdef)
  586. {
  587. CPUState *env;
  588. ResetData *reset_info;
  589. uint32_t tick_frequency = 100*1000000;
  590. uint32_t stick_frequency = 100*1000000;
  591. uint32_t hstick_frequency = 100*1000000;
  592. if (!cpu_model)
  593. cpu_model = hwdef->default_cpu_model;
  594. env = cpu_init(cpu_model);
  595. if (!env) {
  596. fprintf(stderr, "Unable to find Sparc CPU definition\n");
  597. exit(1);
  598. }
  599. env->tick = cpu_timer_create("tick", env, tick_irq,
  600. tick_frequency, TICK_NPT_MASK);
  601. env->stick = cpu_timer_create("stick", env, stick_irq,
  602. stick_frequency, TICK_INT_DIS);
  603. env->hstick = cpu_timer_create("hstick", env, hstick_irq,
  604. hstick_frequency, TICK_INT_DIS);
  605. reset_info = g_malloc0(sizeof(ResetData));
  606. reset_info->env = env;
  607. reset_info->prom_addr = hwdef->prom_addr;
  608. qemu_register_reset(main_cpu_reset, reset_info);
  609. return env;
  610. }
  611. static void sun4uv_init(MemoryRegion *address_space_mem,
  612. ram_addr_t RAM_size,
  613. const char *boot_devices,
  614. const char *kernel_filename, const char *kernel_cmdline,
  615. const char *initrd_filename, const char *cpu_model,
  616. const struct hwdef *hwdef)
  617. {
  618. CPUState *env;
  619. M48t59State *nvram;
  620. unsigned int i;
  621. long initrd_size, kernel_size;
  622. PCIBus *pci_bus, *pci_bus2, *pci_bus3;
  623. qemu_irq *irq;
  624. DriveInfo *hd[MAX_IDE_BUS * MAX_IDE_DEVS];
  625. DriveInfo *fd[MAX_FD];
  626. void *fw_cfg;
  627. /* init CPUs */
  628. env = cpu_devinit(cpu_model, hwdef);
  629. /* set up devices */
  630. ram_init(0, RAM_size);
  631. prom_init(hwdef->prom_addr, bios_name);
  632. irq = qemu_allocate_irqs(cpu_set_irq, env, MAX_PILS);
  633. pci_bus = pci_apb_init(APB_SPECIAL_BASE, APB_MEM_BASE, irq, &pci_bus2,
  634. &pci_bus3);
  635. pci_vga_init(pci_bus);
  636. // XXX Should be pci_bus3
  637. pci_ebus_init(pci_bus, -1);
  638. i = 0;
  639. if (hwdef->console_serial_base) {
  640. serial_mm_init(address_space_mem, hwdef->console_serial_base, 0,
  641. NULL, 115200, serial_hds[i], DEVICE_BIG_ENDIAN);
  642. i++;
  643. }
  644. for(; i < MAX_SERIAL_PORTS; i++) {
  645. if (serial_hds[i]) {
  646. serial_isa_init(i, serial_hds[i]);
  647. }
  648. }
  649. for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
  650. if (parallel_hds[i]) {
  651. parallel_init(i, parallel_hds[i]);
  652. }
  653. }
  654. for(i = 0; i < nb_nics; i++)
  655. pci_nic_init_nofail(&nd_table[i], "ne2k_pci", NULL);
  656. ide_drive_get(hd, MAX_IDE_BUS);
  657. pci_cmd646_ide_init(pci_bus, hd, 1);
  658. isa_create_simple("i8042");
  659. for(i = 0; i < MAX_FD; i++) {
  660. fd[i] = drive_get(IF_FLOPPY, 0, i);
  661. }
  662. fdctrl_init_isa(fd);
  663. nvram = m48t59_init_isa(0x0074, NVRAM_SIZE, 59);
  664. initrd_size = 0;
  665. kernel_size = sun4u_load_kernel(kernel_filename, initrd_filename,
  666. ram_size, &initrd_size);
  667. sun4u_NVRAM_set_params(nvram, NVRAM_SIZE, "Sun4u", RAM_size, boot_devices,
  668. KERNEL_LOAD_ADDR, kernel_size,
  669. kernel_cmdline,
  670. INITRD_LOAD_ADDR, initrd_size,
  671. /* XXX: need an option to load a NVRAM image */
  672. 0,
  673. graphic_width, graphic_height, graphic_depth,
  674. (uint8_t *)&nd_table[0].macaddr);
  675. fw_cfg = fw_cfg_init(BIOS_CFG_IOPORT, BIOS_CFG_IOPORT + 1, 0, 0);
  676. fw_cfg_add_i32(fw_cfg, FW_CFG_ID, 1);
  677. fw_cfg_add_i64(fw_cfg, FW_CFG_RAM_SIZE, (uint64_t)ram_size);
  678. fw_cfg_add_i16(fw_cfg, FW_CFG_MACHINE_ID, hwdef->machine_id);
  679. fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_ADDR, KERNEL_LOAD_ADDR);
  680. fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_SIZE, kernel_size);
  681. if (kernel_cmdline) {
  682. fw_cfg_add_i32(fw_cfg, FW_CFG_CMDLINE_SIZE,
  683. strlen(kernel_cmdline) + 1);
  684. fw_cfg_add_bytes(fw_cfg, FW_CFG_CMDLINE_DATA,
  685. (uint8_t*)strdup(kernel_cmdline),
  686. strlen(kernel_cmdline) + 1);
  687. } else {
  688. fw_cfg_add_i32(fw_cfg, FW_CFG_CMDLINE_SIZE, 0);
  689. }
  690. fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_ADDR, INITRD_LOAD_ADDR);
  691. fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_SIZE, initrd_size);
  692. fw_cfg_add_i16(fw_cfg, FW_CFG_BOOT_DEVICE, boot_devices[0]);
  693. fw_cfg_add_i16(fw_cfg, FW_CFG_SPARC64_WIDTH, graphic_width);
  694. fw_cfg_add_i16(fw_cfg, FW_CFG_SPARC64_HEIGHT, graphic_height);
  695. fw_cfg_add_i16(fw_cfg, FW_CFG_SPARC64_DEPTH, graphic_depth);
  696. qemu_register_boot_set(fw_cfg_boot_set, fw_cfg);
  697. }
  698. enum {
  699. sun4u_id = 0,
  700. sun4v_id = 64,
  701. niagara_id,
  702. };
  703. static const struct hwdef hwdefs[] = {
  704. /* Sun4u generic PC-like machine */
  705. {
  706. .default_cpu_model = "TI UltraSparc IIi",
  707. .machine_id = sun4u_id,
  708. .prom_addr = 0x1fff0000000ULL,
  709. .console_serial_base = 0,
  710. },
  711. /* Sun4v generic PC-like machine */
  712. {
  713. .default_cpu_model = "Sun UltraSparc T1",
  714. .machine_id = sun4v_id,
  715. .prom_addr = 0x1fff0000000ULL,
  716. .console_serial_base = 0,
  717. },
  718. /* Sun4v generic Niagara machine */
  719. {
  720. .default_cpu_model = "Sun UltraSparc T1",
  721. .machine_id = niagara_id,
  722. .prom_addr = 0xfff0000000ULL,
  723. .console_serial_base = 0xfff0c2c000ULL,
  724. },
  725. };
  726. /* Sun4u hardware initialisation */
  727. static void sun4u_init(ram_addr_t RAM_size,
  728. const char *boot_devices,
  729. const char *kernel_filename, const char *kernel_cmdline,
  730. const char *initrd_filename, const char *cpu_model)
  731. {
  732. sun4uv_init(get_system_memory(), RAM_size, boot_devices, kernel_filename,
  733. kernel_cmdline, initrd_filename, cpu_model, &hwdefs[0]);
  734. }
  735. /* Sun4v hardware initialisation */
  736. static void sun4v_init(ram_addr_t RAM_size,
  737. const char *boot_devices,
  738. const char *kernel_filename, const char *kernel_cmdline,
  739. const char *initrd_filename, const char *cpu_model)
  740. {
  741. sun4uv_init(get_system_memory(), RAM_size, boot_devices, kernel_filename,
  742. kernel_cmdline, initrd_filename, cpu_model, &hwdefs[1]);
  743. }
  744. /* Niagara hardware initialisation */
  745. static void niagara_init(ram_addr_t RAM_size,
  746. const char *boot_devices,
  747. const char *kernel_filename, const char *kernel_cmdline,
  748. const char *initrd_filename, const char *cpu_model)
  749. {
  750. sun4uv_init(get_system_memory(), RAM_size, boot_devices, kernel_filename,
  751. kernel_cmdline, initrd_filename, cpu_model, &hwdefs[2]);
  752. }
  753. static QEMUMachine sun4u_machine = {
  754. .name = "sun4u",
  755. .desc = "Sun4u platform",
  756. .init = sun4u_init,
  757. .max_cpus = 1, // XXX for now
  758. .is_default = 1,
  759. };
  760. static QEMUMachine sun4v_machine = {
  761. .name = "sun4v",
  762. .desc = "Sun4v platform",
  763. .init = sun4v_init,
  764. .max_cpus = 1, // XXX for now
  765. };
  766. static QEMUMachine niagara_machine = {
  767. .name = "Niagara",
  768. .desc = "Sun4v platform, Niagara",
  769. .init = niagara_init,
  770. .max_cpus = 1, // XXX for now
  771. };
  772. static void sun4u_machine_init(void)
  773. {
  774. qemu_register_machine(&sun4u_machine);
  775. qemu_register_machine(&sun4v_machine);
  776. qemu_register_machine(&niagara_machine);
  777. }
  778. machine_init(sun4u_machine_init);