sun4u.c 27 KB

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