2
0

sun4u.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862
  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 "qemu/osdep.h"
  25. #include "qemu/units.h"
  26. #include "qemu/error-report.h"
  27. #include "qapi/error.h"
  28. #include "qemu-common.h"
  29. #include "cpu.h"
  30. #include "hw/pci/pci.h"
  31. #include "hw/pci/pci_bridge.h"
  32. #include "hw/pci/pci_bus.h"
  33. #include "hw/pci/pci_host.h"
  34. #include "hw/qdev-properties.h"
  35. #include "hw/pci-host/sabre.h"
  36. #include "hw/char/serial.h"
  37. #include "hw/char/parallel.h"
  38. #include "hw/rtc/m48t59.h"
  39. #include "migration/vmstate.h"
  40. #include "hw/input/i8042.h"
  41. #include "hw/block/fdc.h"
  42. #include "net/net.h"
  43. #include "qemu/timer.h"
  44. #include "sysemu/runstate.h"
  45. #include "sysemu/sysemu.h"
  46. #include "hw/boards.h"
  47. #include "hw/nvram/sun_nvram.h"
  48. #include "hw/nvram/chrp_nvram.h"
  49. #include "hw/sparc/sparc64.h"
  50. #include "hw/nvram/fw_cfg.h"
  51. #include "hw/sysbus.h"
  52. #include "hw/ide/pci.h"
  53. #include "hw/loader.h"
  54. #include "hw/fw-path-provider.h"
  55. #include "elf.h"
  56. #include "trace.h"
  57. #define KERNEL_LOAD_ADDR 0x00404000
  58. #define CMDLINE_ADDR 0x003ff000
  59. #define PROM_SIZE_MAX (4 * MiB)
  60. #define PROM_VADDR 0x000ffd00000ULL
  61. #define PBM_SPECIAL_BASE 0x1fe00000000ULL
  62. #define PBM_MEM_BASE 0x1ff00000000ULL
  63. #define PBM_PCI_IO_BASE (PBM_SPECIAL_BASE + 0x02000000ULL)
  64. #define PROM_FILENAME "openbios-sparc64"
  65. #define NVRAM_SIZE 0x2000
  66. #define MAX_IDE_BUS 2
  67. #define BIOS_CFG_IOPORT 0x510
  68. #define FW_CFG_SPARC64_WIDTH (FW_CFG_ARCH_LOCAL + 0x00)
  69. #define FW_CFG_SPARC64_HEIGHT (FW_CFG_ARCH_LOCAL + 0x01)
  70. #define FW_CFG_SPARC64_DEPTH (FW_CFG_ARCH_LOCAL + 0x02)
  71. #define IVEC_MAX 0x40
  72. struct hwdef {
  73. uint16_t machine_id;
  74. uint64_t prom_addr;
  75. uint64_t console_serial_base;
  76. };
  77. typedef struct EbusState {
  78. /*< private >*/
  79. PCIDevice parent_obj;
  80. ISABus *isa_bus;
  81. qemu_irq isa_bus_irqs[ISA_NUM_IRQS];
  82. uint64_t console_serial_base;
  83. MemoryRegion bar0;
  84. MemoryRegion bar1;
  85. } EbusState;
  86. #define TYPE_EBUS "ebus"
  87. #define EBUS(obj) OBJECT_CHECK(EbusState, (obj), TYPE_EBUS)
  88. const char *fw_cfg_arch_key_name(uint16_t key)
  89. {
  90. static const struct {
  91. uint16_t key;
  92. const char *name;
  93. } fw_cfg_arch_wellknown_keys[] = {
  94. {FW_CFG_SPARC64_WIDTH, "width"},
  95. {FW_CFG_SPARC64_HEIGHT, "height"},
  96. {FW_CFG_SPARC64_DEPTH, "depth"},
  97. };
  98. for (size_t i = 0; i < ARRAY_SIZE(fw_cfg_arch_wellknown_keys); i++) {
  99. if (fw_cfg_arch_wellknown_keys[i].key == key) {
  100. return fw_cfg_arch_wellknown_keys[i].name;
  101. }
  102. }
  103. return NULL;
  104. }
  105. static void fw_cfg_boot_set(void *opaque, const char *boot_device,
  106. Error **errp)
  107. {
  108. fw_cfg_modify_i16(opaque, FW_CFG_BOOT_DEVICE, boot_device[0]);
  109. }
  110. static int sun4u_NVRAM_set_params(Nvram *nvram, uint16_t NVRAM_size,
  111. const char *arch, ram_addr_t RAM_size,
  112. const char *boot_devices,
  113. uint32_t kernel_image, uint32_t kernel_size,
  114. const char *cmdline,
  115. uint32_t initrd_image, uint32_t initrd_size,
  116. uint32_t NVRAM_image,
  117. int width, int height, int depth,
  118. const uint8_t *macaddr)
  119. {
  120. unsigned int i;
  121. int sysp_end;
  122. uint8_t image[0x1ff0];
  123. NvramClass *k = NVRAM_GET_CLASS(nvram);
  124. memset(image, '\0', sizeof(image));
  125. /* OpenBIOS nvram variables partition */
  126. sysp_end = chrp_nvram_create_system_partition(image, 0);
  127. /* Free space partition */
  128. chrp_nvram_create_free_partition(&image[sysp_end], 0x1fd0 - sysp_end);
  129. Sun_init_header((struct Sun_nvram *)&image[0x1fd8], macaddr, 0x80);
  130. for (i = 0; i < sizeof(image); i++) {
  131. (k->write)(nvram, i, image[i]);
  132. }
  133. return 0;
  134. }
  135. static uint64_t sun4u_load_kernel(const char *kernel_filename,
  136. const char *initrd_filename,
  137. ram_addr_t RAM_size, uint64_t *initrd_size,
  138. uint64_t *initrd_addr, uint64_t *kernel_addr,
  139. uint64_t *kernel_entry)
  140. {
  141. int linux_boot;
  142. unsigned int i;
  143. long kernel_size;
  144. uint8_t *ptr;
  145. uint64_t kernel_top = 0;
  146. linux_boot = (kernel_filename != NULL);
  147. kernel_size = 0;
  148. if (linux_boot) {
  149. int bswap_needed;
  150. #ifdef BSWAP_NEEDED
  151. bswap_needed = 1;
  152. #else
  153. bswap_needed = 0;
  154. #endif
  155. kernel_size = load_elf(kernel_filename, NULL, NULL, NULL, kernel_entry,
  156. kernel_addr, &kernel_top, NULL, 1, EM_SPARCV9, 0,
  157. 0);
  158. if (kernel_size < 0) {
  159. *kernel_addr = KERNEL_LOAD_ADDR;
  160. *kernel_entry = KERNEL_LOAD_ADDR;
  161. kernel_size = load_aout(kernel_filename, KERNEL_LOAD_ADDR,
  162. RAM_size - KERNEL_LOAD_ADDR, bswap_needed,
  163. TARGET_PAGE_SIZE);
  164. }
  165. if (kernel_size < 0) {
  166. kernel_size = load_image_targphys(kernel_filename,
  167. KERNEL_LOAD_ADDR,
  168. RAM_size - KERNEL_LOAD_ADDR);
  169. }
  170. if (kernel_size < 0) {
  171. error_report("could not load kernel '%s'", kernel_filename);
  172. exit(1);
  173. }
  174. /* load initrd above kernel */
  175. *initrd_size = 0;
  176. if (initrd_filename && kernel_top) {
  177. *initrd_addr = TARGET_PAGE_ALIGN(kernel_top);
  178. *initrd_size = load_image_targphys(initrd_filename,
  179. *initrd_addr,
  180. RAM_size - *initrd_addr);
  181. if ((int)*initrd_size < 0) {
  182. error_report("could not load initial ram disk '%s'",
  183. initrd_filename);
  184. exit(1);
  185. }
  186. }
  187. if (*initrd_size > 0) {
  188. for (i = 0; i < 64 * TARGET_PAGE_SIZE; i += TARGET_PAGE_SIZE) {
  189. ptr = rom_ptr(*kernel_addr + i, 32);
  190. if (ptr && ldl_p(ptr + 8) == 0x48647253) { /* HdrS */
  191. stl_p(ptr + 24, *initrd_addr + *kernel_addr);
  192. stl_p(ptr + 28, *initrd_size);
  193. break;
  194. }
  195. }
  196. }
  197. }
  198. return kernel_size;
  199. }
  200. typedef struct ResetData {
  201. SPARCCPU *cpu;
  202. uint64_t prom_addr;
  203. } ResetData;
  204. #define TYPE_SUN4U_POWER "power"
  205. #define SUN4U_POWER(obj) OBJECT_CHECK(PowerDevice, (obj), TYPE_SUN4U_POWER)
  206. typedef struct PowerDevice {
  207. SysBusDevice parent_obj;
  208. MemoryRegion power_mmio;
  209. } PowerDevice;
  210. /* Power */
  211. static uint64_t power_mem_read(void *opaque, hwaddr addr, unsigned size)
  212. {
  213. return 0;
  214. }
  215. static void power_mem_write(void *opaque, hwaddr addr,
  216. uint64_t val, unsigned size)
  217. {
  218. /* According to a real Ultra 5, bit 24 controls the power */
  219. if (val & 0x1000000) {
  220. qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN);
  221. }
  222. }
  223. static const MemoryRegionOps power_mem_ops = {
  224. .read = power_mem_read,
  225. .write = power_mem_write,
  226. .endianness = DEVICE_NATIVE_ENDIAN,
  227. .valid = {
  228. .min_access_size = 4,
  229. .max_access_size = 4,
  230. },
  231. };
  232. static void power_realize(DeviceState *dev, Error **errp)
  233. {
  234. PowerDevice *d = SUN4U_POWER(dev);
  235. SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
  236. memory_region_init_io(&d->power_mmio, OBJECT(dev), &power_mem_ops, d,
  237. "power", sizeof(uint32_t));
  238. sysbus_init_mmio(sbd, &d->power_mmio);
  239. }
  240. static void power_class_init(ObjectClass *klass, void *data)
  241. {
  242. DeviceClass *dc = DEVICE_CLASS(klass);
  243. dc->realize = power_realize;
  244. }
  245. static const TypeInfo power_info = {
  246. .name = TYPE_SUN4U_POWER,
  247. .parent = TYPE_SYS_BUS_DEVICE,
  248. .instance_size = sizeof(PowerDevice),
  249. .class_init = power_class_init,
  250. };
  251. static void ebus_isa_irq_handler(void *opaque, int n, int level)
  252. {
  253. EbusState *s = EBUS(opaque);
  254. qemu_irq irq = s->isa_bus_irqs[n];
  255. /* Pass ISA bus IRQs onto their gpio equivalent */
  256. trace_ebus_isa_irq_handler(n, level);
  257. if (irq) {
  258. qemu_set_irq(irq, level);
  259. }
  260. }
  261. /* EBUS (Eight bit bus) bridge */
  262. static void ebus_realize(PCIDevice *pci_dev, Error **errp)
  263. {
  264. EbusState *s = EBUS(pci_dev);
  265. ISADevice *isa_dev;
  266. SysBusDevice *sbd;
  267. DeviceState *dev;
  268. qemu_irq *isa_irq;
  269. DriveInfo *fd[MAX_FD];
  270. int i;
  271. s->isa_bus = isa_bus_new(DEVICE(pci_dev), get_system_memory(),
  272. pci_address_space_io(pci_dev), errp);
  273. if (!s->isa_bus) {
  274. error_setg(errp, "unable to instantiate EBUS ISA bus");
  275. return;
  276. }
  277. /* ISA bus */
  278. isa_irq = qemu_allocate_irqs(ebus_isa_irq_handler, s, ISA_NUM_IRQS);
  279. isa_bus_irqs(s->isa_bus, isa_irq);
  280. qdev_init_gpio_out_named(DEVICE(s), s->isa_bus_irqs, "isa-irq",
  281. ISA_NUM_IRQS);
  282. /* Serial ports */
  283. i = 0;
  284. if (s->console_serial_base) {
  285. serial_mm_init(pci_address_space(pci_dev), s->console_serial_base,
  286. 0, NULL, 115200, serial_hd(i), DEVICE_BIG_ENDIAN);
  287. i++;
  288. }
  289. serial_hds_isa_init(s->isa_bus, i, MAX_ISA_SERIAL_PORTS);
  290. /* Parallel ports */
  291. parallel_hds_isa_init(s->isa_bus, MAX_PARALLEL_PORTS);
  292. /* Keyboard */
  293. isa_create_simple(s->isa_bus, "i8042");
  294. /* Floppy */
  295. for (i = 0; i < MAX_FD; i++) {
  296. fd[i] = drive_get(IF_FLOPPY, 0, i);
  297. }
  298. isa_dev = isa_new(TYPE_ISA_FDC);
  299. dev = DEVICE(isa_dev);
  300. qdev_prop_set_uint32(dev, "dma", -1);
  301. isa_realize_and_unref(isa_dev, s->isa_bus, &error_fatal);
  302. isa_fdc_init_drives(isa_dev, fd);
  303. /* Power */
  304. dev = qdev_new(TYPE_SUN4U_POWER);
  305. sbd = SYS_BUS_DEVICE(dev);
  306. sysbus_realize_and_unref(sbd, &error_fatal);
  307. memory_region_add_subregion(pci_address_space_io(pci_dev), 0x7240,
  308. sysbus_mmio_get_region(sbd, 0));
  309. /* PCI */
  310. pci_dev->config[0x04] = 0x06; // command = bus master, pci mem
  311. pci_dev->config[0x05] = 0x00;
  312. pci_dev->config[0x06] = 0xa0; // status = fast back-to-back, 66MHz, no error
  313. pci_dev->config[0x07] = 0x03; // status = medium devsel
  314. pci_dev->config[0x09] = 0x00; // programming i/f
  315. pci_dev->config[0x0D] = 0x0a; // latency_timer
  316. memory_region_init_alias(&s->bar0, OBJECT(s), "bar0", get_system_io(),
  317. 0, 0x1000000);
  318. pci_register_bar(pci_dev, 0, PCI_BASE_ADDRESS_SPACE_MEMORY, &s->bar0);
  319. memory_region_init_alias(&s->bar1, OBJECT(s), "bar1", get_system_io(),
  320. 0, 0x8000);
  321. pci_register_bar(pci_dev, 1, PCI_BASE_ADDRESS_SPACE_IO, &s->bar1);
  322. }
  323. static Property ebus_properties[] = {
  324. DEFINE_PROP_UINT64("console-serial-base", EbusState,
  325. console_serial_base, 0),
  326. DEFINE_PROP_END_OF_LIST(),
  327. };
  328. static void ebus_class_init(ObjectClass *klass, void *data)
  329. {
  330. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  331. DeviceClass *dc = DEVICE_CLASS(klass);
  332. k->realize = ebus_realize;
  333. k->vendor_id = PCI_VENDOR_ID_SUN;
  334. k->device_id = PCI_DEVICE_ID_SUN_EBUS;
  335. k->revision = 0x01;
  336. k->class_id = PCI_CLASS_BRIDGE_OTHER;
  337. device_class_set_props(dc, ebus_properties);
  338. }
  339. static const TypeInfo ebus_info = {
  340. .name = TYPE_EBUS,
  341. .parent = TYPE_PCI_DEVICE,
  342. .class_init = ebus_class_init,
  343. .instance_size = sizeof(EbusState),
  344. .interfaces = (InterfaceInfo[]) {
  345. { INTERFACE_CONVENTIONAL_PCI_DEVICE },
  346. { },
  347. },
  348. };
  349. #define TYPE_OPENPROM "openprom"
  350. #define OPENPROM(obj) OBJECT_CHECK(PROMState, (obj), TYPE_OPENPROM)
  351. typedef struct PROMState {
  352. SysBusDevice parent_obj;
  353. MemoryRegion prom;
  354. } PROMState;
  355. static uint64_t translate_prom_address(void *opaque, uint64_t addr)
  356. {
  357. hwaddr *base_addr = (hwaddr *)opaque;
  358. return addr + *base_addr - PROM_VADDR;
  359. }
  360. /* Boot PROM (OpenBIOS) */
  361. static void prom_init(hwaddr addr, const char *bios_name)
  362. {
  363. DeviceState *dev;
  364. SysBusDevice *s;
  365. char *filename;
  366. int ret;
  367. dev = qdev_new(TYPE_OPENPROM);
  368. s = SYS_BUS_DEVICE(dev);
  369. sysbus_realize_and_unref(s, &error_fatal);
  370. sysbus_mmio_map(s, 0, addr);
  371. /* load boot prom */
  372. if (bios_name == NULL) {
  373. bios_name = PROM_FILENAME;
  374. }
  375. filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
  376. if (filename) {
  377. ret = load_elf(filename, NULL, translate_prom_address, &addr,
  378. NULL, NULL, NULL, NULL, 1, EM_SPARCV9, 0, 0);
  379. if (ret < 0 || ret > PROM_SIZE_MAX) {
  380. ret = load_image_targphys(filename, addr, PROM_SIZE_MAX);
  381. }
  382. g_free(filename);
  383. } else {
  384. ret = -1;
  385. }
  386. if (ret < 0 || ret > PROM_SIZE_MAX) {
  387. error_report("could not load prom '%s'", bios_name);
  388. exit(1);
  389. }
  390. }
  391. static void prom_realize(DeviceState *ds, Error **errp)
  392. {
  393. PROMState *s = OPENPROM(ds);
  394. SysBusDevice *dev = SYS_BUS_DEVICE(ds);
  395. Error *local_err = NULL;
  396. memory_region_init_ram_nomigrate(&s->prom, OBJECT(ds), "sun4u.prom",
  397. PROM_SIZE_MAX, &local_err);
  398. if (local_err) {
  399. error_propagate(errp, local_err);
  400. return;
  401. }
  402. vmstate_register_ram_global(&s->prom);
  403. memory_region_set_readonly(&s->prom, true);
  404. sysbus_init_mmio(dev, &s->prom);
  405. }
  406. static Property prom_properties[] = {
  407. {/* end of property list */},
  408. };
  409. static void prom_class_init(ObjectClass *klass, void *data)
  410. {
  411. DeviceClass *dc = DEVICE_CLASS(klass);
  412. device_class_set_props(dc, prom_properties);
  413. dc->realize = prom_realize;
  414. }
  415. static const TypeInfo prom_info = {
  416. .name = TYPE_OPENPROM,
  417. .parent = TYPE_SYS_BUS_DEVICE,
  418. .instance_size = sizeof(PROMState),
  419. .class_init = prom_class_init,
  420. };
  421. #define TYPE_SUN4U_MEMORY "memory"
  422. #define SUN4U_RAM(obj) OBJECT_CHECK(RamDevice, (obj), TYPE_SUN4U_MEMORY)
  423. typedef struct RamDevice {
  424. SysBusDevice parent_obj;
  425. MemoryRegion ram;
  426. uint64_t size;
  427. } RamDevice;
  428. /* System RAM */
  429. static void ram_realize(DeviceState *dev, Error **errp)
  430. {
  431. RamDevice *d = SUN4U_RAM(dev);
  432. SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
  433. memory_region_init_ram_nomigrate(&d->ram, OBJECT(d), "sun4u.ram", d->size,
  434. &error_fatal);
  435. vmstate_register_ram_global(&d->ram);
  436. sysbus_init_mmio(sbd, &d->ram);
  437. }
  438. static void ram_init(hwaddr addr, ram_addr_t RAM_size)
  439. {
  440. DeviceState *dev;
  441. SysBusDevice *s;
  442. RamDevice *d;
  443. /* allocate RAM */
  444. dev = qdev_new(TYPE_SUN4U_MEMORY);
  445. s = SYS_BUS_DEVICE(dev);
  446. d = SUN4U_RAM(dev);
  447. d->size = RAM_size;
  448. sysbus_realize_and_unref(s, &error_fatal);
  449. sysbus_mmio_map(s, 0, addr);
  450. }
  451. static Property ram_properties[] = {
  452. DEFINE_PROP_UINT64("size", RamDevice, size, 0),
  453. DEFINE_PROP_END_OF_LIST(),
  454. };
  455. static void ram_class_init(ObjectClass *klass, void *data)
  456. {
  457. DeviceClass *dc = DEVICE_CLASS(klass);
  458. dc->realize = ram_realize;
  459. device_class_set_props(dc, ram_properties);
  460. }
  461. static const TypeInfo ram_info = {
  462. .name = TYPE_SUN4U_MEMORY,
  463. .parent = TYPE_SYS_BUS_DEVICE,
  464. .instance_size = sizeof(RamDevice),
  465. .class_init = ram_class_init,
  466. };
  467. static void sun4uv_init(MemoryRegion *address_space_mem,
  468. MachineState *machine,
  469. const struct hwdef *hwdef)
  470. {
  471. SPARCCPU *cpu;
  472. Nvram *nvram;
  473. unsigned int i;
  474. uint64_t initrd_addr, initrd_size, kernel_addr, kernel_size, kernel_entry;
  475. SabreState *sabre;
  476. PCIBus *pci_bus, *pci_busA, *pci_busB;
  477. PCIDevice *ebus, *pci_dev;
  478. SysBusDevice *s;
  479. DeviceState *iommu, *dev;
  480. FWCfgState *fw_cfg;
  481. NICInfo *nd;
  482. MACAddr macaddr;
  483. bool onboard_nic;
  484. /* init CPUs */
  485. cpu = sparc64_cpu_devinit(machine->cpu_type, hwdef->prom_addr);
  486. /* IOMMU */
  487. iommu = qdev_new(TYPE_SUN4U_IOMMU);
  488. sysbus_realize_and_unref(SYS_BUS_DEVICE(iommu), &error_fatal);
  489. /* set up devices */
  490. ram_init(0, machine->ram_size);
  491. prom_init(hwdef->prom_addr, bios_name);
  492. /* Init sabre (PCI host bridge) */
  493. sabre = SABRE_DEVICE(qdev_new(TYPE_SABRE));
  494. qdev_prop_set_uint64(DEVICE(sabre), "special-base", PBM_SPECIAL_BASE);
  495. qdev_prop_set_uint64(DEVICE(sabre), "mem-base", PBM_MEM_BASE);
  496. object_property_set_link(OBJECT(sabre), "iommu", OBJECT(iommu),
  497. &error_abort);
  498. sysbus_realize_and_unref(SYS_BUS_DEVICE(sabre), &error_fatal);
  499. /* Wire up PCI interrupts to CPU */
  500. for (i = 0; i < IVEC_MAX; i++) {
  501. qdev_connect_gpio_out_named(DEVICE(sabre), "ivec-irq", i,
  502. qdev_get_gpio_in_named(DEVICE(cpu), "ivec-irq", i));
  503. }
  504. pci_bus = PCI_HOST_BRIDGE(sabre)->bus;
  505. pci_busA = pci_bridge_get_sec_bus(sabre->bridgeA);
  506. pci_busB = pci_bridge_get_sec_bus(sabre->bridgeB);
  507. /* Only in-built Simba APBs can exist on the root bus, slot 0 on busA is
  508. reserved (leaving no slots free after on-board devices) however slots
  509. 0-3 are free on busB */
  510. pci_bus->slot_reserved_mask = 0xfffffffc;
  511. pci_busA->slot_reserved_mask = 0xfffffff1;
  512. pci_busB->slot_reserved_mask = 0xfffffff0;
  513. ebus = pci_new_multifunction(PCI_DEVFN(1, 0), true, TYPE_EBUS);
  514. qdev_prop_set_uint64(DEVICE(ebus), "console-serial-base",
  515. hwdef->console_serial_base);
  516. pci_realize_and_unref(ebus, pci_busA, &error_fatal);
  517. /* Wire up "well-known" ISA IRQs to PBM legacy obio IRQs */
  518. qdev_connect_gpio_out_named(DEVICE(ebus), "isa-irq", 7,
  519. qdev_get_gpio_in_named(DEVICE(sabre), "pbm-irq", OBIO_LPT_IRQ));
  520. qdev_connect_gpio_out_named(DEVICE(ebus), "isa-irq", 6,
  521. qdev_get_gpio_in_named(DEVICE(sabre), "pbm-irq", OBIO_FDD_IRQ));
  522. qdev_connect_gpio_out_named(DEVICE(ebus), "isa-irq", 1,
  523. qdev_get_gpio_in_named(DEVICE(sabre), "pbm-irq", OBIO_KBD_IRQ));
  524. qdev_connect_gpio_out_named(DEVICE(ebus), "isa-irq", 12,
  525. qdev_get_gpio_in_named(DEVICE(sabre), "pbm-irq", OBIO_MSE_IRQ));
  526. qdev_connect_gpio_out_named(DEVICE(ebus), "isa-irq", 4,
  527. qdev_get_gpio_in_named(DEVICE(sabre), "pbm-irq", OBIO_SER_IRQ));
  528. switch (vga_interface_type) {
  529. case VGA_STD:
  530. pci_create_simple(pci_busA, PCI_DEVFN(2, 0), "VGA");
  531. break;
  532. case VGA_NONE:
  533. break;
  534. default:
  535. abort(); /* Should not happen - types are checked in vl.c already */
  536. }
  537. memset(&macaddr, 0, sizeof(MACAddr));
  538. onboard_nic = false;
  539. for (i = 0; i < nb_nics; i++) {
  540. PCIBus *bus;
  541. nd = &nd_table[i];
  542. if (!nd->model || strcmp(nd->model, "sunhme") == 0) {
  543. if (!onboard_nic) {
  544. pci_dev = pci_new_multifunction(PCI_DEVFN(1, 1),
  545. true, "sunhme");
  546. bus = pci_busA;
  547. memcpy(&macaddr, &nd->macaddr.a, sizeof(MACAddr));
  548. onboard_nic = true;
  549. } else {
  550. pci_dev = pci_new(-1, "sunhme");
  551. bus = pci_busB;
  552. }
  553. } else {
  554. pci_dev = pci_new(-1, nd->model);
  555. bus = pci_busB;
  556. }
  557. dev = &pci_dev->qdev;
  558. qdev_set_nic_properties(dev, nd);
  559. pci_realize_and_unref(pci_dev, bus, &error_fatal);
  560. }
  561. /* If we don't have an onboard NIC, grab a default MAC address so that
  562. * we have a valid machine id */
  563. if (!onboard_nic) {
  564. qemu_macaddr_default_if_unset(&macaddr);
  565. }
  566. pci_dev = pci_new(PCI_DEVFN(3, 0), "cmd646-ide");
  567. qdev_prop_set_uint32(&pci_dev->qdev, "secondary", 1);
  568. pci_realize_and_unref(pci_dev, pci_busA, &error_fatal);
  569. pci_ide_create_devs(pci_dev);
  570. /* Map NVRAM into I/O (ebus) space */
  571. nvram = m48t59_init(NULL, 0, 0, NVRAM_SIZE, 1968, 59);
  572. s = SYS_BUS_DEVICE(nvram);
  573. memory_region_add_subregion(pci_address_space_io(ebus), 0x2000,
  574. sysbus_mmio_get_region(s, 0));
  575. initrd_size = 0;
  576. initrd_addr = 0;
  577. kernel_size = sun4u_load_kernel(machine->kernel_filename,
  578. machine->initrd_filename,
  579. ram_size, &initrd_size, &initrd_addr,
  580. &kernel_addr, &kernel_entry);
  581. sun4u_NVRAM_set_params(nvram, NVRAM_SIZE, "Sun4u", machine->ram_size,
  582. machine->boot_order,
  583. kernel_addr, kernel_size,
  584. machine->kernel_cmdline,
  585. initrd_addr, initrd_size,
  586. /* XXX: need an option to load a NVRAM image */
  587. 0,
  588. graphic_width, graphic_height, graphic_depth,
  589. (uint8_t *)&macaddr);
  590. dev = qdev_new(TYPE_FW_CFG_IO);
  591. qdev_prop_set_bit(dev, "dma_enabled", false);
  592. object_property_add_child(OBJECT(ebus), TYPE_FW_CFG, OBJECT(dev));
  593. sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
  594. memory_region_add_subregion(pci_address_space_io(ebus), BIOS_CFG_IOPORT,
  595. &FW_CFG_IO(dev)->comb_iomem);
  596. fw_cfg = FW_CFG(dev);
  597. fw_cfg_add_i16(fw_cfg, FW_CFG_NB_CPUS, (uint16_t)machine->smp.cpus);
  598. fw_cfg_add_i16(fw_cfg, FW_CFG_MAX_CPUS, (uint16_t)machine->smp.max_cpus);
  599. fw_cfg_add_i64(fw_cfg, FW_CFG_RAM_SIZE, (uint64_t)ram_size);
  600. fw_cfg_add_i16(fw_cfg, FW_CFG_MACHINE_ID, hwdef->machine_id);
  601. fw_cfg_add_i64(fw_cfg, FW_CFG_KERNEL_ADDR, kernel_entry);
  602. fw_cfg_add_i64(fw_cfg, FW_CFG_KERNEL_SIZE, kernel_size);
  603. if (machine->kernel_cmdline) {
  604. fw_cfg_add_i32(fw_cfg, FW_CFG_CMDLINE_SIZE,
  605. strlen(machine->kernel_cmdline) + 1);
  606. fw_cfg_add_string(fw_cfg, FW_CFG_CMDLINE_DATA, machine->kernel_cmdline);
  607. } else {
  608. fw_cfg_add_i32(fw_cfg, FW_CFG_CMDLINE_SIZE, 0);
  609. }
  610. fw_cfg_add_i64(fw_cfg, FW_CFG_INITRD_ADDR, initrd_addr);
  611. fw_cfg_add_i64(fw_cfg, FW_CFG_INITRD_SIZE, initrd_size);
  612. fw_cfg_add_i16(fw_cfg, FW_CFG_BOOT_DEVICE, machine->boot_order[0]);
  613. fw_cfg_add_i16(fw_cfg, FW_CFG_SPARC64_WIDTH, graphic_width);
  614. fw_cfg_add_i16(fw_cfg, FW_CFG_SPARC64_HEIGHT, graphic_height);
  615. fw_cfg_add_i16(fw_cfg, FW_CFG_SPARC64_DEPTH, graphic_depth);
  616. qemu_register_boot_set(fw_cfg_boot_set, fw_cfg);
  617. }
  618. enum {
  619. sun4u_id = 0,
  620. sun4v_id = 64,
  621. };
  622. /*
  623. * Implementation of an interface to adjust firmware path
  624. * for the bootindex property handling.
  625. */
  626. static char *sun4u_fw_dev_path(FWPathProvider *p, BusState *bus,
  627. DeviceState *dev)
  628. {
  629. PCIDevice *pci;
  630. IDEBus *ide_bus;
  631. IDEState *ide_s;
  632. int bus_id;
  633. if (!strcmp(object_get_typename(OBJECT(dev)), "pbm-bridge")) {
  634. pci = PCI_DEVICE(dev);
  635. if (PCI_FUNC(pci->devfn)) {
  636. return g_strdup_printf("pci@%x,%x", PCI_SLOT(pci->devfn),
  637. PCI_FUNC(pci->devfn));
  638. } else {
  639. return g_strdup_printf("pci@%x", PCI_SLOT(pci->devfn));
  640. }
  641. }
  642. if (!strcmp(object_get_typename(OBJECT(dev)), "ide-drive")) {
  643. ide_bus = IDE_BUS(qdev_get_parent_bus(dev));
  644. ide_s = idebus_active_if(ide_bus);
  645. bus_id = ide_bus->bus_id;
  646. if (ide_s->drive_kind == IDE_CD) {
  647. return g_strdup_printf("ide@%x/cdrom", bus_id);
  648. }
  649. return g_strdup_printf("ide@%x/disk", bus_id);
  650. }
  651. if (!strcmp(object_get_typename(OBJECT(dev)), "ide-hd")) {
  652. return g_strdup("disk");
  653. }
  654. if (!strcmp(object_get_typename(OBJECT(dev)), "ide-cd")) {
  655. return g_strdup("cdrom");
  656. }
  657. if (!strcmp(object_get_typename(OBJECT(dev)), "virtio-blk-device")) {
  658. return g_strdup("disk");
  659. }
  660. return NULL;
  661. }
  662. static const struct hwdef hwdefs[] = {
  663. /* Sun4u generic PC-like machine */
  664. {
  665. .machine_id = sun4u_id,
  666. .prom_addr = 0x1fff0000000ULL,
  667. .console_serial_base = 0,
  668. },
  669. /* Sun4v generic PC-like machine */
  670. {
  671. .machine_id = sun4v_id,
  672. .prom_addr = 0x1fff0000000ULL,
  673. .console_serial_base = 0,
  674. },
  675. };
  676. /* Sun4u hardware initialisation */
  677. static void sun4u_init(MachineState *machine)
  678. {
  679. sun4uv_init(get_system_memory(), machine, &hwdefs[0]);
  680. }
  681. /* Sun4v hardware initialisation */
  682. static void sun4v_init(MachineState *machine)
  683. {
  684. sun4uv_init(get_system_memory(), machine, &hwdefs[1]);
  685. }
  686. static void sun4u_class_init(ObjectClass *oc, void *data)
  687. {
  688. MachineClass *mc = MACHINE_CLASS(oc);
  689. FWPathProviderClass *fwc = FW_PATH_PROVIDER_CLASS(oc);
  690. mc->desc = "Sun4u platform";
  691. mc->init = sun4u_init;
  692. mc->block_default_type = IF_IDE;
  693. mc->max_cpus = 1; /* XXX for now */
  694. mc->is_default = true;
  695. mc->default_boot_order = "c";
  696. mc->default_cpu_type = SPARC_CPU_TYPE_NAME("TI-UltraSparc-IIi");
  697. mc->ignore_boot_device_suffixes = true;
  698. mc->default_display = "std";
  699. fwc->get_dev_path = sun4u_fw_dev_path;
  700. }
  701. static const TypeInfo sun4u_type = {
  702. .name = MACHINE_TYPE_NAME("sun4u"),
  703. .parent = TYPE_MACHINE,
  704. .class_init = sun4u_class_init,
  705. .interfaces = (InterfaceInfo[]) {
  706. { TYPE_FW_PATH_PROVIDER },
  707. { }
  708. },
  709. };
  710. static void sun4v_class_init(ObjectClass *oc, void *data)
  711. {
  712. MachineClass *mc = MACHINE_CLASS(oc);
  713. mc->desc = "Sun4v platform";
  714. mc->init = sun4v_init;
  715. mc->block_default_type = IF_IDE;
  716. mc->max_cpus = 1; /* XXX for now */
  717. mc->default_boot_order = "c";
  718. mc->default_cpu_type = SPARC_CPU_TYPE_NAME("Sun-UltraSparc-T1");
  719. mc->default_display = "std";
  720. }
  721. static const TypeInfo sun4v_type = {
  722. .name = MACHINE_TYPE_NAME("sun4v"),
  723. .parent = TYPE_MACHINE,
  724. .class_init = sun4v_class_init,
  725. };
  726. static void sun4u_register_types(void)
  727. {
  728. type_register_static(&power_info);
  729. type_register_static(&ebus_info);
  730. type_register_static(&prom_info);
  731. type_register_static(&ram_info);
  732. type_register_static(&sun4u_type);
  733. type_register_static(&sun4v_type);
  734. }
  735. type_init(sun4u_register_types)