2
0

prep.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824
  1. /*
  2. * QEMU PPC PREP hardware System Emulator
  3. *
  4. * Copyright (c) 2003-2007 Jocelyn Mayer
  5. * Copyright (c) 2017 Hervé Poussineau
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  20. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. * THE SOFTWARE.
  24. */
  25. #include "qemu/osdep.h"
  26. #include "cpu.h"
  27. #include "hw/rtc/m48t59.h"
  28. #include "hw/char/serial.h"
  29. #include "hw/block/fdc.h"
  30. #include "net/net.h"
  31. #include "sysemu/sysemu.h"
  32. #include "hw/isa/isa.h"
  33. #include "hw/pci/pci.h"
  34. #include "hw/pci/pci_host.h"
  35. #include "hw/ppc/ppc.h"
  36. #include "hw/boards.h"
  37. #include "qemu/error-report.h"
  38. #include "qemu/log.h"
  39. #include "hw/ide.h"
  40. #include "hw/irq.h"
  41. #include "hw/loader.h"
  42. #include "hw/rtc/mc146818rtc.h"
  43. #include "hw/isa/pc87312.h"
  44. #include "hw/net/ne2000-isa.h"
  45. #include "sysemu/arch_init.h"
  46. #include "sysemu/kvm.h"
  47. #include "sysemu/qtest.h"
  48. #include "sysemu/reset.h"
  49. #include "exec/address-spaces.h"
  50. #include "trace.h"
  51. #include "elf.h"
  52. #include "qemu/units.h"
  53. #include "kvm_ppc.h"
  54. /* SMP is not enabled, for now */
  55. #define MAX_CPUS 1
  56. #define MAX_IDE_BUS 2
  57. #define CFG_ADDR 0xf0000510
  58. #define BIOS_SIZE (1 * MiB)
  59. #define BIOS_FILENAME "ppc_rom.bin"
  60. #define KERNEL_LOAD_ADDR 0x01000000
  61. #define INITRD_LOAD_ADDR 0x01800000
  62. /* Constants for devices init */
  63. static const int ide_iobase[2] = { 0x1f0, 0x170 };
  64. static const int ide_iobase2[2] = { 0x3f6, 0x376 };
  65. static const int ide_irq[2] = { 13, 13 };
  66. #define NE2000_NB_MAX 6
  67. static uint32_t ne2000_io[NE2000_NB_MAX] = { 0x300, 0x320, 0x340, 0x360, 0x280, 0x380 };
  68. static int ne2000_irq[NE2000_NB_MAX] = { 9, 10, 11, 3, 4, 5 };
  69. /* ISA IO ports bridge */
  70. #define PPC_IO_BASE 0x80000000
  71. /* Fake super-io ports for PREP platform (Intel 82378ZB) */
  72. typedef struct sysctrl_t {
  73. qemu_irq reset_irq;
  74. Nvram *nvram;
  75. uint8_t state;
  76. uint8_t syscontrol;
  77. int contiguous_map;
  78. qemu_irq contiguous_map_irq;
  79. int endian;
  80. } sysctrl_t;
  81. enum {
  82. STATE_HARDFILE = 0x01,
  83. };
  84. static sysctrl_t *sysctrl;
  85. static void PREP_io_800_writeb (void *opaque, uint32_t addr, uint32_t val)
  86. {
  87. sysctrl_t *sysctrl = opaque;
  88. trace_prep_io_800_writeb(addr - PPC_IO_BASE, val);
  89. switch (addr) {
  90. case 0x0092:
  91. /* Special port 92 */
  92. /* Check soft reset asked */
  93. if (val & 0x01) {
  94. qemu_irq_raise(sysctrl->reset_irq);
  95. } else {
  96. qemu_irq_lower(sysctrl->reset_irq);
  97. }
  98. /* Check LE mode */
  99. if (val & 0x02) {
  100. sysctrl->endian = 1;
  101. } else {
  102. sysctrl->endian = 0;
  103. }
  104. break;
  105. case 0x0800:
  106. /* Motorola CPU configuration register : read-only */
  107. break;
  108. case 0x0802:
  109. /* Motorola base module feature register : read-only */
  110. break;
  111. case 0x0803:
  112. /* Motorola base module status register : read-only */
  113. break;
  114. case 0x0808:
  115. /* Hardfile light register */
  116. if (val & 1)
  117. sysctrl->state |= STATE_HARDFILE;
  118. else
  119. sysctrl->state &= ~STATE_HARDFILE;
  120. break;
  121. case 0x0810:
  122. /* Password protect 1 register */
  123. if (sysctrl->nvram != NULL) {
  124. NvramClass *k = NVRAM_GET_CLASS(sysctrl->nvram);
  125. (k->toggle_lock)(sysctrl->nvram, 1);
  126. }
  127. break;
  128. case 0x0812:
  129. /* Password protect 2 register */
  130. if (sysctrl->nvram != NULL) {
  131. NvramClass *k = NVRAM_GET_CLASS(sysctrl->nvram);
  132. (k->toggle_lock)(sysctrl->nvram, 2);
  133. }
  134. break;
  135. case 0x0814:
  136. /* L2 invalidate register */
  137. // tlb_flush(first_cpu, 1);
  138. break;
  139. case 0x081C:
  140. /* system control register */
  141. sysctrl->syscontrol = val & 0x0F;
  142. break;
  143. case 0x0850:
  144. /* I/O map type register */
  145. sysctrl->contiguous_map = val & 0x01;
  146. qemu_set_irq(sysctrl->contiguous_map_irq, sysctrl->contiguous_map);
  147. break;
  148. default:
  149. printf("ERROR: unaffected IO port write: %04" PRIx32
  150. " => %02" PRIx32"\n", addr, val);
  151. break;
  152. }
  153. }
  154. static uint32_t PREP_io_800_readb (void *opaque, uint32_t addr)
  155. {
  156. sysctrl_t *sysctrl = opaque;
  157. uint32_t retval = 0xFF;
  158. switch (addr) {
  159. case 0x0092:
  160. /* Special port 92 */
  161. retval = sysctrl->endian << 1;
  162. break;
  163. case 0x0800:
  164. /* Motorola CPU configuration register */
  165. retval = 0xEF; /* MPC750 */
  166. break;
  167. case 0x0802:
  168. /* Motorola Base module feature register */
  169. retval = 0xAD; /* No ESCC, PMC slot neither ethernet */
  170. break;
  171. case 0x0803:
  172. /* Motorola base module status register */
  173. retval = 0xE0; /* Standard MPC750 */
  174. break;
  175. case 0x080C:
  176. /* Equipment present register:
  177. * no L2 cache
  178. * no upgrade processor
  179. * no cards in PCI slots
  180. * SCSI fuse is bad
  181. */
  182. retval = 0x3C;
  183. break;
  184. case 0x0810:
  185. /* Motorola base module extended feature register */
  186. retval = 0x39; /* No USB, CF and PCI bridge. NVRAM present */
  187. break;
  188. case 0x0814:
  189. /* L2 invalidate: don't care */
  190. break;
  191. case 0x0818:
  192. /* Keylock */
  193. retval = 0x00;
  194. break;
  195. case 0x081C:
  196. /* system control register
  197. * 7 - 6 / 1 - 0: L2 cache enable
  198. */
  199. retval = sysctrl->syscontrol;
  200. break;
  201. case 0x0823:
  202. /* */
  203. retval = 0x03; /* no L2 cache */
  204. break;
  205. case 0x0850:
  206. /* I/O map type register */
  207. retval = sysctrl->contiguous_map;
  208. break;
  209. default:
  210. printf("ERROR: unaffected IO port: %04" PRIx32 " read\n", addr);
  211. break;
  212. }
  213. trace_prep_io_800_readb(addr - PPC_IO_BASE, retval);
  214. return retval;
  215. }
  216. #define NVRAM_SIZE 0x2000
  217. static void fw_cfg_boot_set(void *opaque, const char *boot_device,
  218. Error **errp)
  219. {
  220. fw_cfg_modify_i16(opaque, FW_CFG_BOOT_DEVICE, boot_device[0]);
  221. }
  222. static void ppc_prep_reset(void *opaque)
  223. {
  224. PowerPCCPU *cpu = opaque;
  225. cpu_reset(CPU(cpu));
  226. }
  227. static const MemoryRegionPortio prep_portio_list[] = {
  228. /* System control ports */
  229. { 0x0092, 1, 1, .read = PREP_io_800_readb, .write = PREP_io_800_writeb, },
  230. { 0x0800, 0x52, 1,
  231. .read = PREP_io_800_readb, .write = PREP_io_800_writeb, },
  232. /* Special port to get debug messages from Open-Firmware */
  233. { 0x0F00, 4, 1, .write = PPC_debug_write, },
  234. PORTIO_END_OF_LIST(),
  235. };
  236. static PortioList prep_port_list;
  237. /*****************************************************************************/
  238. /* NVRAM helpers */
  239. static inline uint32_t nvram_read(Nvram *nvram, uint32_t addr)
  240. {
  241. NvramClass *k = NVRAM_GET_CLASS(nvram);
  242. return (k->read)(nvram, addr);
  243. }
  244. static inline void nvram_write(Nvram *nvram, uint32_t addr, uint32_t val)
  245. {
  246. NvramClass *k = NVRAM_GET_CLASS(nvram);
  247. (k->write)(nvram, addr, val);
  248. }
  249. static void NVRAM_set_byte(Nvram *nvram, uint32_t addr, uint8_t value)
  250. {
  251. nvram_write(nvram, addr, value);
  252. }
  253. static uint8_t NVRAM_get_byte(Nvram *nvram, uint32_t addr)
  254. {
  255. return nvram_read(nvram, addr);
  256. }
  257. static void NVRAM_set_word(Nvram *nvram, uint32_t addr, uint16_t value)
  258. {
  259. nvram_write(nvram, addr, value >> 8);
  260. nvram_write(nvram, addr + 1, value & 0xFF);
  261. }
  262. static uint16_t NVRAM_get_word(Nvram *nvram, uint32_t addr)
  263. {
  264. uint16_t tmp;
  265. tmp = nvram_read(nvram, addr) << 8;
  266. tmp |= nvram_read(nvram, addr + 1);
  267. return tmp;
  268. }
  269. static void NVRAM_set_lword(Nvram *nvram, uint32_t addr, uint32_t value)
  270. {
  271. nvram_write(nvram, addr, value >> 24);
  272. nvram_write(nvram, addr + 1, (value >> 16) & 0xFF);
  273. nvram_write(nvram, addr + 2, (value >> 8) & 0xFF);
  274. nvram_write(nvram, addr + 3, value & 0xFF);
  275. }
  276. static void NVRAM_set_string(Nvram *nvram, uint32_t addr, const char *str,
  277. uint32_t max)
  278. {
  279. int i;
  280. for (i = 0; i < max && str[i] != '\0'; i++) {
  281. nvram_write(nvram, addr + i, str[i]);
  282. }
  283. nvram_write(nvram, addr + i, str[i]);
  284. nvram_write(nvram, addr + max - 1, '\0');
  285. }
  286. static uint16_t NVRAM_crc_update (uint16_t prev, uint16_t value)
  287. {
  288. uint16_t tmp;
  289. uint16_t pd, pd1, pd2;
  290. tmp = prev >> 8;
  291. pd = prev ^ value;
  292. pd1 = pd & 0x000F;
  293. pd2 = ((pd >> 4) & 0x000F) ^ pd1;
  294. tmp ^= (pd1 << 3) | (pd1 << 8);
  295. tmp ^= pd2 | (pd2 << 7) | (pd2 << 12);
  296. return tmp;
  297. }
  298. static uint16_t NVRAM_compute_crc (Nvram *nvram, uint32_t start, uint32_t count)
  299. {
  300. uint32_t i;
  301. uint16_t crc = 0xFFFF;
  302. int odd;
  303. odd = count & 1;
  304. count &= ~1;
  305. for (i = 0; i != count; i++) {
  306. crc = NVRAM_crc_update(crc, NVRAM_get_word(nvram, start + i));
  307. }
  308. if (odd) {
  309. crc = NVRAM_crc_update(crc, NVRAM_get_byte(nvram, start + i) << 8);
  310. }
  311. return crc;
  312. }
  313. #define CMDLINE_ADDR 0x017ff000
  314. static int PPC_NVRAM_set_params (Nvram *nvram, uint16_t NVRAM_size,
  315. const char *arch,
  316. uint32_t RAM_size, int boot_device,
  317. uint32_t kernel_image, uint32_t kernel_size,
  318. const char *cmdline,
  319. uint32_t initrd_image, uint32_t initrd_size,
  320. uint32_t NVRAM_image,
  321. int width, int height, int depth)
  322. {
  323. uint16_t crc;
  324. /* Set parameters for Open Hack'Ware BIOS */
  325. NVRAM_set_string(nvram, 0x00, "QEMU_BIOS", 16);
  326. NVRAM_set_lword(nvram, 0x10, 0x00000002); /* structure v2 */
  327. NVRAM_set_word(nvram, 0x14, NVRAM_size);
  328. NVRAM_set_string(nvram, 0x20, arch, 16);
  329. NVRAM_set_lword(nvram, 0x30, RAM_size);
  330. NVRAM_set_byte(nvram, 0x34, boot_device);
  331. NVRAM_set_lword(nvram, 0x38, kernel_image);
  332. NVRAM_set_lword(nvram, 0x3C, kernel_size);
  333. if (cmdline) {
  334. /* XXX: put the cmdline in NVRAM too ? */
  335. pstrcpy_targphys("cmdline", CMDLINE_ADDR, RAM_size - CMDLINE_ADDR,
  336. cmdline);
  337. NVRAM_set_lword(nvram, 0x40, CMDLINE_ADDR);
  338. NVRAM_set_lword(nvram, 0x44, strlen(cmdline));
  339. } else {
  340. NVRAM_set_lword(nvram, 0x40, 0);
  341. NVRAM_set_lword(nvram, 0x44, 0);
  342. }
  343. NVRAM_set_lword(nvram, 0x48, initrd_image);
  344. NVRAM_set_lword(nvram, 0x4C, initrd_size);
  345. NVRAM_set_lword(nvram, 0x50, NVRAM_image);
  346. NVRAM_set_word(nvram, 0x54, width);
  347. NVRAM_set_word(nvram, 0x56, height);
  348. NVRAM_set_word(nvram, 0x58, depth);
  349. crc = NVRAM_compute_crc(nvram, 0x00, 0xF8);
  350. NVRAM_set_word(nvram, 0xFC, crc);
  351. return 0;
  352. }
  353. /* PowerPC PREP hardware initialisation */
  354. static void ppc_prep_init(MachineState *machine)
  355. {
  356. ram_addr_t ram_size = machine->ram_size;
  357. const char *kernel_filename = machine->kernel_filename;
  358. const char *kernel_cmdline = machine->kernel_cmdline;
  359. const char *initrd_filename = machine->initrd_filename;
  360. const char *boot_device = machine->boot_order;
  361. MemoryRegion *sysmem = get_system_memory();
  362. PowerPCCPU *cpu = NULL;
  363. CPUPPCState *env = NULL;
  364. Nvram *m48t59;
  365. #if 0
  366. MemoryRegion *xcsr = g_new(MemoryRegion, 1);
  367. #endif
  368. int linux_boot, i, nb_nics1;
  369. MemoryRegion *ram = g_new(MemoryRegion, 1);
  370. uint32_t kernel_base, initrd_base;
  371. long kernel_size, initrd_size;
  372. DeviceState *dev;
  373. PCIHostState *pcihost;
  374. PCIBus *pci_bus;
  375. PCIDevice *pci;
  376. ISABus *isa_bus;
  377. ISADevice *isa;
  378. int ppc_boot_device;
  379. DriveInfo *hd[MAX_IDE_BUS * MAX_IDE_DEVS];
  380. sysctrl = g_malloc0(sizeof(sysctrl_t));
  381. linux_boot = (kernel_filename != NULL);
  382. /* init CPUs */
  383. for (i = 0; i < machine->smp.cpus; i++) {
  384. cpu = POWERPC_CPU(cpu_create(machine->cpu_type));
  385. env = &cpu->env;
  386. if (env->flags & POWERPC_FLAG_RTC_CLK) {
  387. /* POWER / PowerPC 601 RTC clock frequency is 7.8125 MHz */
  388. cpu_ppc_tb_init(env, 7812500UL);
  389. } else {
  390. /* Set time-base frequency to 100 Mhz */
  391. cpu_ppc_tb_init(env, 100UL * 1000UL * 1000UL);
  392. }
  393. qemu_register_reset(ppc_prep_reset, cpu);
  394. }
  395. /* allocate RAM */
  396. memory_region_allocate_system_memory(ram, NULL, "ppc_prep.ram", ram_size);
  397. memory_region_add_subregion(sysmem, 0, ram);
  398. if (linux_boot) {
  399. kernel_base = KERNEL_LOAD_ADDR;
  400. /* now we can load the kernel */
  401. kernel_size = load_image_targphys(kernel_filename, kernel_base,
  402. ram_size - kernel_base);
  403. if (kernel_size < 0) {
  404. error_report("could not load kernel '%s'", kernel_filename);
  405. exit(1);
  406. }
  407. /* load initrd */
  408. if (initrd_filename) {
  409. initrd_base = INITRD_LOAD_ADDR;
  410. initrd_size = load_image_targphys(initrd_filename, initrd_base,
  411. ram_size - initrd_base);
  412. if (initrd_size < 0) {
  413. error_report("could not load initial ram disk '%s'",
  414. initrd_filename);
  415. exit(1);
  416. }
  417. } else {
  418. initrd_base = 0;
  419. initrd_size = 0;
  420. }
  421. ppc_boot_device = 'm';
  422. } else {
  423. kernel_base = 0;
  424. kernel_size = 0;
  425. initrd_base = 0;
  426. initrd_size = 0;
  427. ppc_boot_device = '\0';
  428. /* For now, OHW cannot boot from the network. */
  429. for (i = 0; boot_device[i] != '\0'; i++) {
  430. if (boot_device[i] >= 'a' && boot_device[i] <= 'f') {
  431. ppc_boot_device = boot_device[i];
  432. break;
  433. }
  434. }
  435. if (ppc_boot_device == '\0') {
  436. error_report("No valid boot device for Mac99 machine");
  437. exit(1);
  438. }
  439. }
  440. if (PPC_INPUT(env) != PPC_FLAGS_INPUT_6xx) {
  441. error_report("Only 6xx bus is supported on PREP machine");
  442. exit(1);
  443. }
  444. dev = qdev_create(NULL, "raven-pcihost");
  445. if (bios_name == NULL) {
  446. bios_name = BIOS_FILENAME;
  447. }
  448. qdev_prop_set_string(dev, "bios-name", bios_name);
  449. qdev_prop_set_uint32(dev, "elf-machine", PPC_ELF_MACHINE);
  450. qdev_prop_set_bit(dev, "is-legacy-prep", true);
  451. pcihost = PCI_HOST_BRIDGE(dev);
  452. object_property_add_child(qdev_get_machine(), "raven", OBJECT(dev), NULL);
  453. qdev_init_nofail(dev);
  454. pci_bus = (PCIBus *)qdev_get_child_bus(dev, "pci.0");
  455. if (pci_bus == NULL) {
  456. error_report("Couldn't create PCI host controller");
  457. exit(1);
  458. }
  459. sysctrl->contiguous_map_irq = qdev_get_gpio_in(dev, 0);
  460. /* PCI -> ISA bridge */
  461. pci = pci_create_simple(pci_bus, PCI_DEVFN(1, 0), "i82378");
  462. cpu = POWERPC_CPU(first_cpu);
  463. qdev_connect_gpio_out(&pci->qdev, 0,
  464. cpu->env.irq_inputs[PPC6xx_INPUT_INT]);
  465. sysbus_connect_irq(&pcihost->busdev, 0, qdev_get_gpio_in(&pci->qdev, 9));
  466. sysbus_connect_irq(&pcihost->busdev, 1, qdev_get_gpio_in(&pci->qdev, 11));
  467. sysbus_connect_irq(&pcihost->busdev, 2, qdev_get_gpio_in(&pci->qdev, 9));
  468. sysbus_connect_irq(&pcihost->busdev, 3, qdev_get_gpio_in(&pci->qdev, 11));
  469. isa_bus = ISA_BUS(qdev_get_child_bus(DEVICE(pci), "isa.0"));
  470. /* Super I/O (parallel + serial ports) */
  471. isa = isa_create(isa_bus, TYPE_PC87312_SUPERIO);
  472. dev = DEVICE(isa);
  473. qdev_prop_set_uint8(dev, "config", 13); /* fdc, ser0, ser1, par0 */
  474. qdev_init_nofail(dev);
  475. /* init basic PC hardware */
  476. pci_vga_init(pci_bus);
  477. nb_nics1 = nb_nics;
  478. if (nb_nics1 > NE2000_NB_MAX)
  479. nb_nics1 = NE2000_NB_MAX;
  480. for(i = 0; i < nb_nics1; i++) {
  481. if (nd_table[i].model == NULL) {
  482. nd_table[i].model = g_strdup("ne2k_isa");
  483. }
  484. if (strcmp(nd_table[i].model, "ne2k_isa") == 0) {
  485. isa_ne2000_init(isa_bus, ne2000_io[i], ne2000_irq[i],
  486. &nd_table[i]);
  487. } else {
  488. pci_nic_init_nofail(&nd_table[i], pci_bus, "ne2k_pci", NULL);
  489. }
  490. }
  491. ide_drive_get(hd, ARRAY_SIZE(hd));
  492. for(i = 0; i < MAX_IDE_BUS; i++) {
  493. isa_ide_init(isa_bus, ide_iobase[i], ide_iobase2[i], ide_irq[i],
  494. hd[2 * i],
  495. hd[2 * i + 1]);
  496. }
  497. cpu = POWERPC_CPU(first_cpu);
  498. sysctrl->reset_irq = cpu->env.irq_inputs[PPC6xx_INPUT_HRESET];
  499. portio_list_init(&prep_port_list, NULL, prep_portio_list, sysctrl, "prep");
  500. portio_list_add(&prep_port_list, isa_address_space_io(isa), 0x0);
  501. /*
  502. * PowerPC control and status register group: unimplemented,
  503. * would be at address 0xFEFF0000.
  504. */
  505. if (machine_usb(machine)) {
  506. pci_create_simple(pci_bus, -1, "pci-ohci");
  507. }
  508. m48t59 = m48t59_init_isa(isa_bus, 0x0074, NVRAM_SIZE, 2000, 59);
  509. if (m48t59 == NULL)
  510. return;
  511. sysctrl->nvram = m48t59;
  512. /* Initialise NVRAM */
  513. PPC_NVRAM_set_params(m48t59, NVRAM_SIZE, "PREP", ram_size,
  514. ppc_boot_device,
  515. kernel_base, kernel_size,
  516. kernel_cmdline,
  517. initrd_base, initrd_size,
  518. /* XXX: need an option to load a NVRAM image */
  519. 0,
  520. graphic_width, graphic_height, graphic_depth);
  521. }
  522. static void prep_machine_init(MachineClass *mc)
  523. {
  524. mc->deprecation_reason = "use 40p machine type instead";
  525. mc->desc = "PowerPC PREP platform";
  526. mc->init = ppc_prep_init;
  527. mc->block_default_type = IF_IDE;
  528. mc->max_cpus = MAX_CPUS;
  529. mc->default_boot_order = "cad";
  530. mc->default_cpu_type = POWERPC_CPU_TYPE_NAME("602");
  531. mc->default_display = "std";
  532. }
  533. static int prep_set_cmos_checksum(DeviceState *dev, void *opaque)
  534. {
  535. uint16_t checksum = *(uint16_t *)opaque;
  536. ISADevice *rtc;
  537. if (object_dynamic_cast(OBJECT(dev), TYPE_MC146818_RTC)) {
  538. rtc = ISA_DEVICE(dev);
  539. rtc_set_memory(rtc, 0x2e, checksum & 0xff);
  540. rtc_set_memory(rtc, 0x3e, checksum & 0xff);
  541. rtc_set_memory(rtc, 0x2f, checksum >> 8);
  542. rtc_set_memory(rtc, 0x3f, checksum >> 8);
  543. object_property_add_alias(qdev_get_machine(), "rtc-time", OBJECT(rtc),
  544. "date", NULL);
  545. }
  546. return 0;
  547. }
  548. static void ibm_40p_init(MachineState *machine)
  549. {
  550. CPUPPCState *env = NULL;
  551. uint16_t cmos_checksum;
  552. PowerPCCPU *cpu;
  553. DeviceState *dev, *i82378_dev;
  554. SysBusDevice *pcihost, *s;
  555. Nvram *m48t59 = NULL;
  556. PCIBus *pci_bus;
  557. ISABus *isa_bus;
  558. void *fw_cfg;
  559. int i;
  560. uint32_t kernel_base = 0, initrd_base = 0;
  561. long kernel_size = 0, initrd_size = 0;
  562. char boot_device;
  563. /* init CPU */
  564. cpu = POWERPC_CPU(cpu_create(machine->cpu_type));
  565. env = &cpu->env;
  566. if (PPC_INPUT(env) != PPC_FLAGS_INPUT_6xx) {
  567. error_report("only 6xx bus is supported on this machine");
  568. exit(1);
  569. }
  570. if (env->flags & POWERPC_FLAG_RTC_CLK) {
  571. /* POWER / PowerPC 601 RTC clock frequency is 7.8125 MHz */
  572. cpu_ppc_tb_init(env, 7812500UL);
  573. } else {
  574. /* Set time-base frequency to 100 Mhz */
  575. cpu_ppc_tb_init(env, 100UL * 1000UL * 1000UL);
  576. }
  577. qemu_register_reset(ppc_prep_reset, cpu);
  578. /* PCI host */
  579. dev = qdev_create(NULL, "raven-pcihost");
  580. if (!bios_name) {
  581. bios_name = "openbios-ppc";
  582. }
  583. qdev_prop_set_string(dev, "bios-name", bios_name);
  584. qdev_prop_set_uint32(dev, "elf-machine", PPC_ELF_MACHINE);
  585. pcihost = SYS_BUS_DEVICE(dev);
  586. object_property_add_child(qdev_get_machine(), "raven", OBJECT(dev), NULL);
  587. qdev_init_nofail(dev);
  588. pci_bus = PCI_BUS(qdev_get_child_bus(dev, "pci.0"));
  589. if (!pci_bus) {
  590. error_report("could not create PCI host controller");
  591. exit(1);
  592. }
  593. /* PCI -> ISA bridge */
  594. i82378_dev = DEVICE(pci_create_simple(pci_bus, PCI_DEVFN(11, 0), "i82378"));
  595. qdev_connect_gpio_out(i82378_dev, 0,
  596. cpu->env.irq_inputs[PPC6xx_INPUT_INT]);
  597. sysbus_connect_irq(pcihost, 0, qdev_get_gpio_in(i82378_dev, 15));
  598. isa_bus = ISA_BUS(qdev_get_child_bus(i82378_dev, "isa.0"));
  599. /* Memory controller */
  600. dev = DEVICE(isa_create(isa_bus, "rs6000-mc"));
  601. qdev_prop_set_uint32(dev, "ram-size", machine->ram_size);
  602. qdev_init_nofail(dev);
  603. /* RTC */
  604. dev = DEVICE(isa_create(isa_bus, TYPE_MC146818_RTC));
  605. qdev_prop_set_int32(dev, "base_year", 1900);
  606. qdev_init_nofail(dev);
  607. /* initialize CMOS checksums */
  608. cmos_checksum = 0x6aa9;
  609. qbus_walk_children(BUS(isa_bus), prep_set_cmos_checksum, NULL, NULL, NULL,
  610. &cmos_checksum);
  611. /* add some more devices */
  612. if (defaults_enabled()) {
  613. m48t59 = NVRAM(isa_create_simple(isa_bus, "isa-m48t59"));
  614. dev = DEVICE(isa_create(isa_bus, "cs4231a"));
  615. qdev_prop_set_uint32(dev, "iobase", 0x830);
  616. qdev_prop_set_uint32(dev, "irq", 10);
  617. qdev_init_nofail(dev);
  618. dev = DEVICE(isa_create(isa_bus, "pc87312"));
  619. qdev_prop_set_uint32(dev, "config", 12);
  620. qdev_init_nofail(dev);
  621. dev = DEVICE(isa_create(isa_bus, "prep-systemio"));
  622. qdev_prop_set_uint32(dev, "ibm-planar-id", 0xfc);
  623. qdev_prop_set_uint32(dev, "equipment", 0xc0);
  624. qdev_init_nofail(dev);
  625. dev = DEVICE(pci_create_simple(pci_bus, PCI_DEVFN(1, 0),
  626. "lsi53c810"));
  627. lsi53c8xx_handle_legacy_cmdline(dev);
  628. qdev_connect_gpio_out(dev, 0, qdev_get_gpio_in(i82378_dev, 13));
  629. /* XXX: s3-trio at PCI_DEVFN(2, 0) */
  630. pci_vga_init(pci_bus);
  631. for (i = 0; i < nb_nics; i++) {
  632. pci_nic_init_nofail(&nd_table[i], pci_bus, "pcnet",
  633. i == 0 ? "3" : NULL);
  634. }
  635. }
  636. /* Prepare firmware configuration for OpenBIOS */
  637. dev = qdev_create(NULL, TYPE_FW_CFG_MEM);
  638. fw_cfg = FW_CFG(dev);
  639. qdev_prop_set_uint32(dev, "data_width", 1);
  640. qdev_prop_set_bit(dev, "dma_enabled", false);
  641. object_property_add_child(OBJECT(qdev_get_machine()), TYPE_FW_CFG,
  642. OBJECT(fw_cfg), NULL);
  643. qdev_init_nofail(dev);
  644. s = SYS_BUS_DEVICE(dev);
  645. sysbus_mmio_map(s, 0, CFG_ADDR);
  646. sysbus_mmio_map(s, 1, CFG_ADDR + 2);
  647. if (machine->kernel_filename) {
  648. /* load kernel */
  649. kernel_base = KERNEL_LOAD_ADDR;
  650. kernel_size = load_image_targphys(machine->kernel_filename,
  651. kernel_base,
  652. machine->ram_size - kernel_base);
  653. if (kernel_size < 0) {
  654. error_report("could not load kernel '%s'",
  655. machine->kernel_filename);
  656. exit(1);
  657. }
  658. fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_ADDR, kernel_base);
  659. fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_SIZE, kernel_size);
  660. /* load initrd */
  661. if (machine->initrd_filename) {
  662. initrd_base = INITRD_LOAD_ADDR;
  663. initrd_size = load_image_targphys(machine->initrd_filename,
  664. initrd_base,
  665. machine->ram_size - initrd_base);
  666. if (initrd_size < 0) {
  667. error_report("could not load initial ram disk '%s'",
  668. machine->initrd_filename);
  669. exit(1);
  670. }
  671. fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_ADDR, initrd_base);
  672. fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_SIZE, initrd_size);
  673. }
  674. if (machine->kernel_cmdline && *machine->kernel_cmdline) {
  675. fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_CMDLINE, CMDLINE_ADDR);
  676. pstrcpy_targphys("cmdline", CMDLINE_ADDR, TARGET_PAGE_SIZE,
  677. machine->kernel_cmdline);
  678. fw_cfg_add_string(fw_cfg, FW_CFG_CMDLINE_DATA,
  679. machine->kernel_cmdline);
  680. fw_cfg_add_i32(fw_cfg, FW_CFG_CMDLINE_SIZE,
  681. strlen(machine->kernel_cmdline) + 1);
  682. }
  683. boot_device = 'm';
  684. } else {
  685. boot_device = machine->boot_order[0];
  686. }
  687. fw_cfg_add_i16(fw_cfg, FW_CFG_MAX_CPUS, (uint16_t)machine->smp.max_cpus);
  688. fw_cfg_add_i64(fw_cfg, FW_CFG_RAM_SIZE, (uint64_t)machine->ram_size);
  689. fw_cfg_add_i16(fw_cfg, FW_CFG_MACHINE_ID, ARCH_PREP);
  690. fw_cfg_add_i16(fw_cfg, FW_CFG_PPC_WIDTH, graphic_width);
  691. fw_cfg_add_i16(fw_cfg, FW_CFG_PPC_HEIGHT, graphic_height);
  692. fw_cfg_add_i16(fw_cfg, FW_CFG_PPC_DEPTH, graphic_depth);
  693. fw_cfg_add_i32(fw_cfg, FW_CFG_PPC_IS_KVM, kvm_enabled());
  694. if (kvm_enabled()) {
  695. uint8_t *hypercall;
  696. fw_cfg_add_i32(fw_cfg, FW_CFG_PPC_TBFREQ, kvmppc_get_tbfreq());
  697. hypercall = g_malloc(16);
  698. kvmppc_get_hypercall(env, hypercall, 16);
  699. fw_cfg_add_bytes(fw_cfg, FW_CFG_PPC_KVM_HC, hypercall, 16);
  700. fw_cfg_add_i32(fw_cfg, FW_CFG_PPC_KVM_PID, getpid());
  701. } else {
  702. fw_cfg_add_i32(fw_cfg, FW_CFG_PPC_TBFREQ, NANOSECONDS_PER_SECOND);
  703. }
  704. fw_cfg_add_i16(fw_cfg, FW_CFG_BOOT_DEVICE, boot_device);
  705. qemu_register_boot_set(fw_cfg_boot_set, fw_cfg);
  706. /* Prepare firmware configuration for Open Hack'Ware */
  707. if (m48t59) {
  708. PPC_NVRAM_set_params(m48t59, NVRAM_SIZE, "PREP", ram_size,
  709. boot_device,
  710. kernel_base, kernel_size,
  711. machine->kernel_cmdline,
  712. initrd_base, initrd_size,
  713. /* XXX: need an option to load a NVRAM image */
  714. 0,
  715. graphic_width, graphic_height, graphic_depth);
  716. }
  717. }
  718. static void ibm_40p_machine_init(MachineClass *mc)
  719. {
  720. mc->desc = "IBM RS/6000 7020 (40p)",
  721. mc->init = ibm_40p_init;
  722. mc->max_cpus = 1;
  723. mc->default_ram_size = 128 * MiB;
  724. mc->block_default_type = IF_SCSI;
  725. mc->default_boot_order = "c";
  726. mc->default_cpu_type = POWERPC_CPU_TYPE_NAME("604");
  727. mc->default_display = "std";
  728. }
  729. DEFINE_MACHINE("40p", ibm_40p_machine_init)
  730. DEFINE_MACHINE("prep", prep_machine_init)