sun4u.c 27 KB

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