sun4u.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999
  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. #include "vga-pci.h"
  43. //#define DEBUG_IRQ
  44. //#define DEBUG_EBUS
  45. //#define DEBUG_TIMER
  46. #ifdef DEBUG_IRQ
  47. #define CPUIRQ_DPRINTF(fmt, ...) \
  48. do { printf("CPUIRQ: " fmt , ## __VA_ARGS__); } while (0)
  49. #else
  50. #define CPUIRQ_DPRINTF(fmt, ...)
  51. #endif
  52. #ifdef DEBUG_EBUS
  53. #define EBUS_DPRINTF(fmt, ...) \
  54. do { printf("EBUS: " fmt , ## __VA_ARGS__); } while (0)
  55. #else
  56. #define EBUS_DPRINTF(fmt, ...)
  57. #endif
  58. #ifdef DEBUG_TIMER
  59. #define TIMER_DPRINTF(fmt, ...) \
  60. do { printf("TIMER: " fmt , ## __VA_ARGS__); } while (0)
  61. #else
  62. #define TIMER_DPRINTF(fmt, ...)
  63. #endif
  64. #define KERNEL_LOAD_ADDR 0x00404000
  65. #define CMDLINE_ADDR 0x003ff000
  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 IVEC_MAX 0x30
  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 uint64_t sun4u_load_kernel(const char *kernel_filename,
  160. const char *initrd_filename,
  161. ram_addr_t RAM_size, uint64_t *initrd_size,
  162. uint64_t *initrd_addr, uint64_t *kernel_addr,
  163. uint64_t *kernel_entry)
  164. {
  165. int linux_boot;
  166. unsigned int i;
  167. long kernel_size;
  168. uint8_t *ptr;
  169. uint64_t kernel_top;
  170. linux_boot = (kernel_filename != NULL);
  171. kernel_size = 0;
  172. if (linux_boot) {
  173. int bswap_needed;
  174. #ifdef BSWAP_NEEDED
  175. bswap_needed = 1;
  176. #else
  177. bswap_needed = 0;
  178. #endif
  179. kernel_size = load_elf(kernel_filename, NULL, NULL, kernel_entry,
  180. kernel_addr, &kernel_top, 1, ELF_MACHINE, 0);
  181. if (kernel_size < 0) {
  182. *kernel_addr = KERNEL_LOAD_ADDR;
  183. *kernel_entry = KERNEL_LOAD_ADDR;
  184. kernel_size = load_aout(kernel_filename, KERNEL_LOAD_ADDR,
  185. RAM_size - KERNEL_LOAD_ADDR, bswap_needed,
  186. TARGET_PAGE_SIZE);
  187. }
  188. if (kernel_size < 0) {
  189. kernel_size = load_image_targphys(kernel_filename,
  190. KERNEL_LOAD_ADDR,
  191. RAM_size - KERNEL_LOAD_ADDR);
  192. }
  193. if (kernel_size < 0) {
  194. fprintf(stderr, "qemu: could not load kernel '%s'\n",
  195. kernel_filename);
  196. exit(1);
  197. }
  198. /* load initrd above kernel */
  199. *initrd_size = 0;
  200. if (initrd_filename) {
  201. *initrd_addr = TARGET_PAGE_ALIGN(kernel_top);
  202. *initrd_size = load_image_targphys(initrd_filename,
  203. *initrd_addr,
  204. RAM_size - *initrd_addr);
  205. if ((int)*initrd_size < 0) {
  206. fprintf(stderr, "qemu: could not load initial ram disk '%s'\n",
  207. initrd_filename);
  208. exit(1);
  209. }
  210. }
  211. if (*initrd_size > 0) {
  212. for (i = 0; i < 64 * TARGET_PAGE_SIZE; i += TARGET_PAGE_SIZE) {
  213. ptr = rom_ptr(*kernel_addr + i);
  214. if (ldl_p(ptr + 8) == 0x48647253) { /* HdrS */
  215. stl_p(ptr + 24, *initrd_addr + *kernel_addr);
  216. stl_p(ptr + 28, *initrd_size);
  217. break;
  218. }
  219. }
  220. }
  221. }
  222. return kernel_size;
  223. }
  224. void cpu_check_irqs(CPUSPARCState *env)
  225. {
  226. uint32_t pil = env->pil_in |
  227. (env->softint & ~(SOFTINT_TIMER | SOFTINT_STIMER));
  228. /* TT_IVEC has a higher priority (16) than TT_EXTINT (31..17) */
  229. if (env->ivec_status & 0x20) {
  230. return;
  231. }
  232. /* check if TM or SM in SOFTINT are set
  233. setting these also causes interrupt 14 */
  234. if (env->softint & (SOFTINT_TIMER | SOFTINT_STIMER)) {
  235. pil |= 1 << 14;
  236. }
  237. /* The bit corresponding to psrpil is (1<< psrpil), the next bit
  238. is (2 << psrpil). */
  239. if (pil < (2 << env->psrpil)){
  240. if (env->interrupt_request & CPU_INTERRUPT_HARD) {
  241. CPUIRQ_DPRINTF("Reset CPU IRQ (current interrupt %x)\n",
  242. env->interrupt_index);
  243. env->interrupt_index = 0;
  244. cpu_reset_interrupt(env, CPU_INTERRUPT_HARD);
  245. }
  246. return;
  247. }
  248. if (cpu_interrupts_enabled(env)) {
  249. unsigned int i;
  250. for (i = 15; i > env->psrpil; i--) {
  251. if (pil & (1 << i)) {
  252. int old_interrupt = env->interrupt_index;
  253. int new_interrupt = TT_EXTINT | i;
  254. if (unlikely(env->tl > 0 && cpu_tsptr(env)->tt > new_interrupt
  255. && ((cpu_tsptr(env)->tt & 0x1f0) == TT_EXTINT))) {
  256. CPUIRQ_DPRINTF("Not setting CPU IRQ: TL=%d "
  257. "current %x >= pending %x\n",
  258. env->tl, cpu_tsptr(env)->tt, new_interrupt);
  259. } else if (old_interrupt != new_interrupt) {
  260. env->interrupt_index = new_interrupt;
  261. CPUIRQ_DPRINTF("Set CPU IRQ %d old=%x new=%x\n", i,
  262. old_interrupt, new_interrupt);
  263. cpu_interrupt(env, CPU_INTERRUPT_HARD);
  264. }
  265. break;
  266. }
  267. }
  268. } else if (env->interrupt_request & CPU_INTERRUPT_HARD) {
  269. CPUIRQ_DPRINTF("Interrupts disabled, pil=%08x pil_in=%08x softint=%08x "
  270. "current interrupt %x\n",
  271. pil, env->pil_in, env->softint, env->interrupt_index);
  272. env->interrupt_index = 0;
  273. cpu_reset_interrupt(env, CPU_INTERRUPT_HARD);
  274. }
  275. }
  276. static void cpu_kick_irq(CPUSPARCState *env)
  277. {
  278. env->halted = 0;
  279. cpu_check_irqs(env);
  280. qemu_cpu_kick(env);
  281. }
  282. static void cpu_set_ivec_irq(void *opaque, int irq, int level)
  283. {
  284. CPUSPARCState *env = opaque;
  285. if (level) {
  286. if (!(env->ivec_status & 0x20)) {
  287. CPUIRQ_DPRINTF("Raise IVEC IRQ %d\n", irq);
  288. env->halted = 0;
  289. env->interrupt_index = TT_IVEC;
  290. env->ivec_status |= 0x20;
  291. env->ivec_data[0] = (0x1f << 6) | irq;
  292. env->ivec_data[1] = 0;
  293. env->ivec_data[2] = 0;
  294. cpu_interrupt(env, CPU_INTERRUPT_HARD);
  295. }
  296. } else {
  297. if (env->ivec_status & 0x20) {
  298. CPUIRQ_DPRINTF("Lower IVEC IRQ %d\n", irq);
  299. env->ivec_status &= ~0x20;
  300. cpu_reset_interrupt(env, CPU_INTERRUPT_HARD);
  301. }
  302. }
  303. }
  304. typedef struct ResetData {
  305. SPARCCPU *cpu;
  306. uint64_t prom_addr;
  307. } ResetData;
  308. void cpu_put_timer(QEMUFile *f, CPUTimer *s)
  309. {
  310. qemu_put_be32s(f, &s->frequency);
  311. qemu_put_be32s(f, &s->disabled);
  312. qemu_put_be64s(f, &s->disabled_mask);
  313. qemu_put_sbe64s(f, &s->clock_offset);
  314. qemu_put_timer(f, s->qtimer);
  315. }
  316. void cpu_get_timer(QEMUFile *f, CPUTimer *s)
  317. {
  318. qemu_get_be32s(f, &s->frequency);
  319. qemu_get_be32s(f, &s->disabled);
  320. qemu_get_be64s(f, &s->disabled_mask);
  321. qemu_get_sbe64s(f, &s->clock_offset);
  322. qemu_get_timer(f, s->qtimer);
  323. }
  324. static CPUTimer* cpu_timer_create(const char* name, CPUSPARCState *env,
  325. QEMUBHFunc *cb, uint32_t frequency,
  326. uint64_t disabled_mask)
  327. {
  328. CPUTimer *timer = g_malloc0(sizeof (CPUTimer));
  329. timer->name = name;
  330. timer->frequency = frequency;
  331. timer->disabled_mask = disabled_mask;
  332. timer->disabled = 1;
  333. timer->clock_offset = qemu_get_clock_ns(vm_clock);
  334. timer->qtimer = qemu_new_timer_ns(vm_clock, cb, env);
  335. return timer;
  336. }
  337. static void cpu_timer_reset(CPUTimer *timer)
  338. {
  339. timer->disabled = 1;
  340. timer->clock_offset = qemu_get_clock_ns(vm_clock);
  341. qemu_del_timer(timer->qtimer);
  342. }
  343. static void main_cpu_reset(void *opaque)
  344. {
  345. ResetData *s = (ResetData *)opaque;
  346. CPUSPARCState *env = &s->cpu->env;
  347. static unsigned int nr_resets;
  348. cpu_reset(CPU(s->cpu));
  349. cpu_timer_reset(env->tick);
  350. cpu_timer_reset(env->stick);
  351. cpu_timer_reset(env->hstick);
  352. env->gregs[1] = 0; // Memory start
  353. env->gregs[2] = ram_size; // Memory size
  354. env->gregs[3] = 0; // Machine description XXX
  355. if (nr_resets++ == 0) {
  356. /* Power on reset */
  357. env->pc = s->prom_addr + 0x20ULL;
  358. } else {
  359. env->pc = s->prom_addr + 0x40ULL;
  360. }
  361. env->npc = env->pc + 4;
  362. }
  363. static void tick_irq(void *opaque)
  364. {
  365. CPUSPARCState *env = opaque;
  366. CPUTimer* timer = env->tick;
  367. if (timer->disabled) {
  368. CPUIRQ_DPRINTF("tick_irq: softint disabled\n");
  369. return;
  370. } else {
  371. CPUIRQ_DPRINTF("tick: fire\n");
  372. }
  373. env->softint |= SOFTINT_TIMER;
  374. cpu_kick_irq(env);
  375. }
  376. static void stick_irq(void *opaque)
  377. {
  378. CPUSPARCState *env = opaque;
  379. CPUTimer* timer = env->stick;
  380. if (timer->disabled) {
  381. CPUIRQ_DPRINTF("stick_irq: softint disabled\n");
  382. return;
  383. } else {
  384. CPUIRQ_DPRINTF("stick: fire\n");
  385. }
  386. env->softint |= SOFTINT_STIMER;
  387. cpu_kick_irq(env);
  388. }
  389. static void hstick_irq(void *opaque)
  390. {
  391. CPUSPARCState *env = opaque;
  392. CPUTimer* timer = env->hstick;
  393. if (timer->disabled) {
  394. CPUIRQ_DPRINTF("hstick_irq: softint disabled\n");
  395. return;
  396. } else {
  397. CPUIRQ_DPRINTF("hstick: fire\n");
  398. }
  399. env->softint |= SOFTINT_STIMER;
  400. cpu_kick_irq(env);
  401. }
  402. static int64_t cpu_to_timer_ticks(int64_t cpu_ticks, uint32_t frequency)
  403. {
  404. return muldiv64(cpu_ticks, get_ticks_per_sec(), frequency);
  405. }
  406. static uint64_t timer_to_cpu_ticks(int64_t timer_ticks, uint32_t frequency)
  407. {
  408. return muldiv64(timer_ticks, frequency, get_ticks_per_sec());
  409. }
  410. void cpu_tick_set_count(CPUTimer *timer, uint64_t count)
  411. {
  412. uint64_t real_count = count & ~timer->disabled_mask;
  413. uint64_t disabled_bit = count & timer->disabled_mask;
  414. int64_t vm_clock_offset = qemu_get_clock_ns(vm_clock) -
  415. cpu_to_timer_ticks(real_count, timer->frequency);
  416. TIMER_DPRINTF("%s set_count count=0x%016lx (%s) p=%p\n",
  417. timer->name, real_count,
  418. timer->disabled?"disabled":"enabled", timer);
  419. timer->disabled = disabled_bit ? 1 : 0;
  420. timer->clock_offset = vm_clock_offset;
  421. }
  422. uint64_t cpu_tick_get_count(CPUTimer *timer)
  423. {
  424. uint64_t real_count = timer_to_cpu_ticks(
  425. qemu_get_clock_ns(vm_clock) - timer->clock_offset,
  426. timer->frequency);
  427. TIMER_DPRINTF("%s get_count count=0x%016lx (%s) p=%p\n",
  428. timer->name, real_count,
  429. timer->disabled?"disabled":"enabled", timer);
  430. if (timer->disabled)
  431. real_count |= timer->disabled_mask;
  432. return real_count;
  433. }
  434. void cpu_tick_set_limit(CPUTimer *timer, uint64_t limit)
  435. {
  436. int64_t now = qemu_get_clock_ns(vm_clock);
  437. uint64_t real_limit = limit & ~timer->disabled_mask;
  438. timer->disabled = (limit & timer->disabled_mask) ? 1 : 0;
  439. int64_t expires = cpu_to_timer_ticks(real_limit, timer->frequency) +
  440. timer->clock_offset;
  441. if (expires < now) {
  442. expires = now + 1;
  443. }
  444. TIMER_DPRINTF("%s set_limit limit=0x%016lx (%s) p=%p "
  445. "called with limit=0x%016lx at 0x%016lx (delta=0x%016lx)\n",
  446. timer->name, real_limit,
  447. timer->disabled?"disabled":"enabled",
  448. timer, limit,
  449. timer_to_cpu_ticks(now - timer->clock_offset,
  450. timer->frequency),
  451. timer_to_cpu_ticks(expires - now, timer->frequency));
  452. if (!real_limit) {
  453. TIMER_DPRINTF("%s set_limit limit=ZERO - not starting timer\n",
  454. timer->name);
  455. qemu_del_timer(timer->qtimer);
  456. } else if (timer->disabled) {
  457. qemu_del_timer(timer->qtimer);
  458. } else {
  459. qemu_mod_timer(timer->qtimer, expires);
  460. }
  461. }
  462. static void isa_irq_handler(void *opaque, int n, int level)
  463. {
  464. static const int isa_irq_to_ivec[16] = {
  465. [1] = 0x29, /* keyboard */
  466. [4] = 0x2b, /* serial */
  467. [6] = 0x27, /* floppy */
  468. [7] = 0x22, /* parallel */
  469. [12] = 0x2a, /* mouse */
  470. };
  471. qemu_irq *irqs = opaque;
  472. int ivec;
  473. assert(n < 16);
  474. ivec = isa_irq_to_ivec[n];
  475. EBUS_DPRINTF("Set ISA IRQ %d level %d -> ivec 0x%x\n", n, level, ivec);
  476. if (ivec) {
  477. qemu_set_irq(irqs[ivec], level);
  478. }
  479. }
  480. /* EBUS (Eight bit bus) bridge */
  481. static ISABus *
  482. pci_ebus_init(PCIBus *bus, int devfn, qemu_irq *irqs)
  483. {
  484. qemu_irq *isa_irq;
  485. PCIDevice *pci_dev;
  486. ISABus *isa_bus;
  487. pci_dev = pci_create_simple(bus, devfn, "ebus");
  488. isa_bus = DO_UPCAST(ISABus, qbus,
  489. qdev_get_child_bus(&pci_dev->qdev, "isa.0"));
  490. isa_irq = qemu_allocate_irqs(isa_irq_handler, irqs, 16);
  491. isa_bus_irqs(isa_bus, isa_irq);
  492. return isa_bus;
  493. }
  494. static int
  495. pci_ebus_init1(PCIDevice *pci_dev)
  496. {
  497. EbusState *s = DO_UPCAST(EbusState, pci_dev, pci_dev);
  498. isa_bus_new(&pci_dev->qdev, pci_address_space_io(pci_dev));
  499. pci_dev->config[0x04] = 0x06; // command = bus master, pci mem
  500. pci_dev->config[0x05] = 0x00;
  501. pci_dev->config[0x06] = 0xa0; // status = fast back-to-back, 66MHz, no error
  502. pci_dev->config[0x07] = 0x03; // status = medium devsel
  503. pci_dev->config[0x09] = 0x00; // programming i/f
  504. pci_dev->config[0x0D] = 0x0a; // latency_timer
  505. isa_mmio_setup(&s->bar0, 0x1000000);
  506. pci_register_bar(pci_dev, 0, PCI_BASE_ADDRESS_SPACE_MEMORY, &s->bar0);
  507. isa_mmio_setup(&s->bar1, 0x800000);
  508. pci_register_bar(pci_dev, 1, PCI_BASE_ADDRESS_SPACE_MEMORY, &s->bar1);
  509. return 0;
  510. }
  511. static void ebus_class_init(ObjectClass *klass, void *data)
  512. {
  513. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  514. k->init = pci_ebus_init1;
  515. k->vendor_id = PCI_VENDOR_ID_SUN;
  516. k->device_id = PCI_DEVICE_ID_SUN_EBUS;
  517. k->revision = 0x01;
  518. k->class_id = PCI_CLASS_BRIDGE_OTHER;
  519. }
  520. static TypeInfo ebus_info = {
  521. .name = "ebus",
  522. .parent = TYPE_PCI_DEVICE,
  523. .instance_size = sizeof(EbusState),
  524. .class_init = ebus_class_init,
  525. };
  526. typedef struct PROMState {
  527. SysBusDevice busdev;
  528. MemoryRegion prom;
  529. } PROMState;
  530. static uint64_t translate_prom_address(void *opaque, uint64_t addr)
  531. {
  532. target_phys_addr_t *base_addr = (target_phys_addr_t *)opaque;
  533. return addr + *base_addr - PROM_VADDR;
  534. }
  535. /* Boot PROM (OpenBIOS) */
  536. static void prom_init(target_phys_addr_t addr, const char *bios_name)
  537. {
  538. DeviceState *dev;
  539. SysBusDevice *s;
  540. char *filename;
  541. int ret;
  542. dev = qdev_create(NULL, "openprom");
  543. qdev_init_nofail(dev);
  544. s = sysbus_from_qdev(dev);
  545. sysbus_mmio_map(s, 0, addr);
  546. /* load boot prom */
  547. if (bios_name == NULL) {
  548. bios_name = PROM_FILENAME;
  549. }
  550. filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
  551. if (filename) {
  552. ret = load_elf(filename, translate_prom_address, &addr,
  553. NULL, NULL, NULL, 1, ELF_MACHINE, 0);
  554. if (ret < 0 || ret > PROM_SIZE_MAX) {
  555. ret = load_image_targphys(filename, addr, PROM_SIZE_MAX);
  556. }
  557. g_free(filename);
  558. } else {
  559. ret = -1;
  560. }
  561. if (ret < 0 || ret > PROM_SIZE_MAX) {
  562. fprintf(stderr, "qemu: could not load prom '%s'\n", bios_name);
  563. exit(1);
  564. }
  565. }
  566. static int prom_init1(SysBusDevice *dev)
  567. {
  568. PROMState *s = FROM_SYSBUS(PROMState, dev);
  569. memory_region_init_ram(&s->prom, "sun4u.prom", PROM_SIZE_MAX);
  570. vmstate_register_ram_global(&s->prom);
  571. memory_region_set_readonly(&s->prom, true);
  572. sysbus_init_mmio(dev, &s->prom);
  573. return 0;
  574. }
  575. static Property prom_properties[] = {
  576. {/* end of property list */},
  577. };
  578. static void prom_class_init(ObjectClass *klass, void *data)
  579. {
  580. DeviceClass *dc = DEVICE_CLASS(klass);
  581. SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
  582. k->init = prom_init1;
  583. dc->props = prom_properties;
  584. }
  585. static TypeInfo prom_info = {
  586. .name = "openprom",
  587. .parent = TYPE_SYS_BUS_DEVICE,
  588. .instance_size = sizeof(PROMState),
  589. .class_init = prom_class_init,
  590. };
  591. typedef struct RamDevice
  592. {
  593. SysBusDevice busdev;
  594. MemoryRegion ram;
  595. uint64_t size;
  596. } RamDevice;
  597. /* System RAM */
  598. static int ram_init1(SysBusDevice *dev)
  599. {
  600. RamDevice *d = FROM_SYSBUS(RamDevice, dev);
  601. memory_region_init_ram(&d->ram, "sun4u.ram", d->size);
  602. vmstate_register_ram_global(&d->ram);
  603. sysbus_init_mmio(dev, &d->ram);
  604. return 0;
  605. }
  606. static void ram_init(target_phys_addr_t addr, ram_addr_t RAM_size)
  607. {
  608. DeviceState *dev;
  609. SysBusDevice *s;
  610. RamDevice *d;
  611. /* allocate RAM */
  612. dev = qdev_create(NULL, "memory");
  613. s = sysbus_from_qdev(dev);
  614. d = FROM_SYSBUS(RamDevice, s);
  615. d->size = RAM_size;
  616. qdev_init_nofail(dev);
  617. sysbus_mmio_map(s, 0, addr);
  618. }
  619. static Property ram_properties[] = {
  620. DEFINE_PROP_UINT64("size", RamDevice, size, 0),
  621. DEFINE_PROP_END_OF_LIST(),
  622. };
  623. static void ram_class_init(ObjectClass *klass, void *data)
  624. {
  625. DeviceClass *dc = DEVICE_CLASS(klass);
  626. SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
  627. k->init = ram_init1;
  628. dc->props = ram_properties;
  629. }
  630. static TypeInfo ram_info = {
  631. .name = "memory",
  632. .parent = TYPE_SYS_BUS_DEVICE,
  633. .instance_size = sizeof(RamDevice),
  634. .class_init = ram_class_init,
  635. };
  636. static SPARCCPU *cpu_devinit(const char *cpu_model, const struct hwdef *hwdef)
  637. {
  638. SPARCCPU *cpu;
  639. CPUSPARCState *env;
  640. ResetData *reset_info;
  641. uint32_t tick_frequency = 100*1000000;
  642. uint32_t stick_frequency = 100*1000000;
  643. uint32_t hstick_frequency = 100*1000000;
  644. if (cpu_model == NULL) {
  645. cpu_model = hwdef->default_cpu_model;
  646. }
  647. cpu = cpu_sparc_init(cpu_model);
  648. if (cpu == NULL) {
  649. fprintf(stderr, "Unable to find Sparc CPU definition\n");
  650. exit(1);
  651. }
  652. env = &cpu->env;
  653. env->tick = cpu_timer_create("tick", env, tick_irq,
  654. tick_frequency, TICK_NPT_MASK);
  655. env->stick = cpu_timer_create("stick", env, stick_irq,
  656. stick_frequency, TICK_INT_DIS);
  657. env->hstick = cpu_timer_create("hstick", env, hstick_irq,
  658. hstick_frequency, TICK_INT_DIS);
  659. reset_info = g_malloc0(sizeof(ResetData));
  660. reset_info->cpu = cpu;
  661. reset_info->prom_addr = hwdef->prom_addr;
  662. qemu_register_reset(main_cpu_reset, reset_info);
  663. return cpu;
  664. }
  665. static void sun4uv_init(MemoryRegion *address_space_mem,
  666. ram_addr_t RAM_size,
  667. const char *boot_devices,
  668. const char *kernel_filename, const char *kernel_cmdline,
  669. const char *initrd_filename, const char *cpu_model,
  670. const struct hwdef *hwdef)
  671. {
  672. SPARCCPU *cpu;
  673. CPUSPARCState *env;
  674. M48t59State *nvram;
  675. unsigned int i;
  676. uint64_t initrd_addr, initrd_size, kernel_addr, kernel_size, kernel_entry;
  677. PCIBus *pci_bus, *pci_bus2, *pci_bus3;
  678. ISABus *isa_bus;
  679. qemu_irq *ivec_irqs, *pbm_irqs;
  680. DriveInfo *hd[MAX_IDE_BUS * MAX_IDE_DEVS];
  681. DriveInfo *fd[MAX_FD];
  682. void *fw_cfg;
  683. /* init CPUs */
  684. cpu = cpu_devinit(cpu_model, hwdef);
  685. env = &cpu->env;
  686. /* set up devices */
  687. ram_init(0, RAM_size);
  688. prom_init(hwdef->prom_addr, bios_name);
  689. ivec_irqs = qemu_allocate_irqs(cpu_set_ivec_irq, env, IVEC_MAX);
  690. pci_bus = pci_apb_init(APB_SPECIAL_BASE, APB_MEM_BASE, ivec_irqs, &pci_bus2,
  691. &pci_bus3, &pbm_irqs);
  692. pci_vga_init(pci_bus);
  693. // XXX Should be pci_bus3
  694. isa_bus = pci_ebus_init(pci_bus, -1, pbm_irqs);
  695. i = 0;
  696. if (hwdef->console_serial_base) {
  697. serial_mm_init(address_space_mem, hwdef->console_serial_base, 0,
  698. NULL, 115200, serial_hds[i], DEVICE_BIG_ENDIAN);
  699. i++;
  700. }
  701. for(; i < MAX_SERIAL_PORTS; i++) {
  702. if (serial_hds[i]) {
  703. serial_isa_init(isa_bus, i, serial_hds[i]);
  704. }
  705. }
  706. for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
  707. if (parallel_hds[i]) {
  708. parallel_init(isa_bus, i, parallel_hds[i]);
  709. }
  710. }
  711. for(i = 0; i < nb_nics; i++)
  712. pci_nic_init_nofail(&nd_table[i], "ne2k_pci", NULL);
  713. ide_drive_get(hd, MAX_IDE_BUS);
  714. pci_cmd646_ide_init(pci_bus, hd, 1);
  715. isa_create_simple(isa_bus, "i8042");
  716. for(i = 0; i < MAX_FD; i++) {
  717. fd[i] = drive_get(IF_FLOPPY, 0, i);
  718. }
  719. fdctrl_init_isa(isa_bus, fd);
  720. nvram = m48t59_init_isa(isa_bus, 0x0074, NVRAM_SIZE, 59);
  721. initrd_size = 0;
  722. initrd_addr = 0;
  723. kernel_size = sun4u_load_kernel(kernel_filename, initrd_filename,
  724. ram_size, &initrd_size, &initrd_addr,
  725. &kernel_addr, &kernel_entry);
  726. sun4u_NVRAM_set_params(nvram, NVRAM_SIZE, "Sun4u", RAM_size, boot_devices,
  727. kernel_addr, kernel_size,
  728. kernel_cmdline,
  729. initrd_addr, initrd_size,
  730. /* XXX: need an option to load a NVRAM image */
  731. 0,
  732. graphic_width, graphic_height, graphic_depth,
  733. (uint8_t *)&nd_table[0].macaddr);
  734. fw_cfg = fw_cfg_init(BIOS_CFG_IOPORT, BIOS_CFG_IOPORT + 1, 0, 0);
  735. fw_cfg_add_i32(fw_cfg, FW_CFG_ID, 1);
  736. fw_cfg_add_i64(fw_cfg, FW_CFG_RAM_SIZE, (uint64_t)ram_size);
  737. fw_cfg_add_i16(fw_cfg, FW_CFG_MACHINE_ID, hwdef->machine_id);
  738. fw_cfg_add_i64(fw_cfg, FW_CFG_KERNEL_ADDR, kernel_entry);
  739. fw_cfg_add_i64(fw_cfg, FW_CFG_KERNEL_SIZE, kernel_size);
  740. if (kernel_cmdline) {
  741. fw_cfg_add_i32(fw_cfg, FW_CFG_CMDLINE_SIZE,
  742. strlen(kernel_cmdline) + 1);
  743. fw_cfg_add_bytes(fw_cfg, FW_CFG_CMDLINE_DATA,
  744. (uint8_t*)strdup(kernel_cmdline),
  745. strlen(kernel_cmdline) + 1);
  746. } else {
  747. fw_cfg_add_i32(fw_cfg, FW_CFG_CMDLINE_SIZE, 0);
  748. }
  749. fw_cfg_add_i64(fw_cfg, FW_CFG_INITRD_ADDR, initrd_addr);
  750. fw_cfg_add_i64(fw_cfg, FW_CFG_INITRD_SIZE, initrd_size);
  751. fw_cfg_add_i16(fw_cfg, FW_CFG_BOOT_DEVICE, boot_devices[0]);
  752. fw_cfg_add_i16(fw_cfg, FW_CFG_SPARC64_WIDTH, graphic_width);
  753. fw_cfg_add_i16(fw_cfg, FW_CFG_SPARC64_HEIGHT, graphic_height);
  754. fw_cfg_add_i16(fw_cfg, FW_CFG_SPARC64_DEPTH, graphic_depth);
  755. qemu_register_boot_set(fw_cfg_boot_set, fw_cfg);
  756. }
  757. enum {
  758. sun4u_id = 0,
  759. sun4v_id = 64,
  760. niagara_id,
  761. };
  762. static const struct hwdef hwdefs[] = {
  763. /* Sun4u generic PC-like machine */
  764. {
  765. .default_cpu_model = "TI UltraSparc IIi",
  766. .machine_id = sun4u_id,
  767. .prom_addr = 0x1fff0000000ULL,
  768. .console_serial_base = 0,
  769. },
  770. /* Sun4v generic PC-like machine */
  771. {
  772. .default_cpu_model = "Sun UltraSparc T1",
  773. .machine_id = sun4v_id,
  774. .prom_addr = 0x1fff0000000ULL,
  775. .console_serial_base = 0,
  776. },
  777. /* Sun4v generic Niagara machine */
  778. {
  779. .default_cpu_model = "Sun UltraSparc T1",
  780. .machine_id = niagara_id,
  781. .prom_addr = 0xfff0000000ULL,
  782. .console_serial_base = 0xfff0c2c000ULL,
  783. },
  784. };
  785. /* Sun4u hardware initialisation */
  786. static void sun4u_init(ram_addr_t RAM_size,
  787. const char *boot_devices,
  788. const char *kernel_filename, const char *kernel_cmdline,
  789. const char *initrd_filename, const char *cpu_model)
  790. {
  791. sun4uv_init(get_system_memory(), RAM_size, boot_devices, kernel_filename,
  792. kernel_cmdline, initrd_filename, cpu_model, &hwdefs[0]);
  793. }
  794. /* Sun4v hardware initialisation */
  795. static void sun4v_init(ram_addr_t RAM_size,
  796. const char *boot_devices,
  797. const char *kernel_filename, const char *kernel_cmdline,
  798. const char *initrd_filename, const char *cpu_model)
  799. {
  800. sun4uv_init(get_system_memory(), RAM_size, boot_devices, kernel_filename,
  801. kernel_cmdline, initrd_filename, cpu_model, &hwdefs[1]);
  802. }
  803. /* Niagara hardware initialisation */
  804. static void niagara_init(ram_addr_t RAM_size,
  805. const char *boot_devices,
  806. const char *kernel_filename, const char *kernel_cmdline,
  807. const char *initrd_filename, const char *cpu_model)
  808. {
  809. sun4uv_init(get_system_memory(), RAM_size, boot_devices, kernel_filename,
  810. kernel_cmdline, initrd_filename, cpu_model, &hwdefs[2]);
  811. }
  812. static QEMUMachine sun4u_machine = {
  813. .name = "sun4u",
  814. .desc = "Sun4u platform",
  815. .init = sun4u_init,
  816. .max_cpus = 1, // XXX for now
  817. .is_default = 1,
  818. };
  819. static QEMUMachine sun4v_machine = {
  820. .name = "sun4v",
  821. .desc = "Sun4v platform",
  822. .init = sun4v_init,
  823. .max_cpus = 1, // XXX for now
  824. };
  825. static QEMUMachine niagara_machine = {
  826. .name = "Niagara",
  827. .desc = "Sun4v platform, Niagara",
  828. .init = niagara_init,
  829. .max_cpus = 1, // XXX for now
  830. };
  831. static void sun4u_register_types(void)
  832. {
  833. type_register_static(&ebus_info);
  834. type_register_static(&prom_info);
  835. type_register_static(&ram_info);
  836. }
  837. static void sun4u_machine_init(void)
  838. {
  839. qemu_register_machine(&sun4u_machine);
  840. qemu_register_machine(&sun4v_machine);
  841. qemu_register_machine(&niagara_machine);
  842. }
  843. type_init(sun4u_register_types)
  844. machine_init(sun4u_machine_init);