2
0

sun4u.c 27 KB

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