boston.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  1. /*
  2. * MIPS Boston development board emulation.
  3. *
  4. * Copyright (c) 2016 Imagination Technologies
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "qemu/osdep.h"
  20. #include "qemu/units.h"
  21. #include "elf.h"
  22. #include "hw/boards.h"
  23. #include "hw/char/serial-mm.h"
  24. #include "hw/ide/pci.h"
  25. #include "hw/ide/ahci-pci.h"
  26. #include "hw/loader.h"
  27. #include "hw/loader-fit.h"
  28. #include "hw/mips/bootloader.h"
  29. #include "hw/mips/cps.h"
  30. #include "hw/pci-host/xilinx-pcie.h"
  31. #include "hw/qdev-clock.h"
  32. #include "hw/qdev-properties.h"
  33. #include "qapi/error.h"
  34. #include "qemu/error-report.h"
  35. #include "qemu/guest-random.h"
  36. #include "qemu/log.h"
  37. #include "chardev/char.h"
  38. #include "system/device_tree.h"
  39. #include "system/system.h"
  40. #include "system/qtest.h"
  41. #include "system/runstate.h"
  42. #include "system/reset.h"
  43. #include <libfdt.h>
  44. #include "qom/object.h"
  45. #define TYPE_BOSTON "mips-boston"
  46. typedef struct BostonState BostonState;
  47. DECLARE_INSTANCE_CHECKER(BostonState, BOSTON,
  48. TYPE_BOSTON)
  49. #define FDT_IRQ_TYPE_NONE 0
  50. #define FDT_IRQ_TYPE_LEVEL_HIGH 4
  51. #define FDT_GIC_SHARED 0
  52. #define FDT_GIC_LOCAL 1
  53. #define FDT_BOSTON_CLK_SYS 1
  54. #define FDT_BOSTON_CLK_CPU 2
  55. #define FDT_PCI_IRQ_MAP_PINS 4
  56. #define FDT_PCI_IRQ_MAP_DESCS 6
  57. struct BostonState {
  58. SysBusDevice parent_obj;
  59. MachineState *mach;
  60. MIPSCPSState cps;
  61. SerialMM *uart;
  62. Clock *cpuclk;
  63. CharBackend lcd_display;
  64. char lcd_content[8];
  65. bool lcd_inited;
  66. hwaddr kernel_entry;
  67. hwaddr fdt_base;
  68. };
  69. enum {
  70. BOSTON_LOWDDR,
  71. BOSTON_PCIE0,
  72. BOSTON_PCIE1,
  73. BOSTON_PCIE2,
  74. BOSTON_PCIE2_MMIO,
  75. BOSTON_CM,
  76. BOSTON_GIC,
  77. BOSTON_CDMM,
  78. BOSTON_CPC,
  79. BOSTON_PLATREG,
  80. BOSTON_UART,
  81. BOSTON_LCD,
  82. BOSTON_FLASH,
  83. BOSTON_PCIE1_MMIO,
  84. BOSTON_PCIE0_MMIO,
  85. BOSTON_HIGHDDR,
  86. };
  87. static const MemMapEntry boston_memmap[] = {
  88. [BOSTON_LOWDDR] = { 0x0, 0x10000000 },
  89. [BOSTON_PCIE0] = { 0x10000000, 0x2000000 },
  90. [BOSTON_PCIE1] = { 0x12000000, 0x2000000 },
  91. [BOSTON_PCIE2] = { 0x14000000, 0x2000000 },
  92. [BOSTON_PCIE2_MMIO] = { 0x16000000, 0x100000 },
  93. [BOSTON_CM] = { 0x16100000, 0x20000 },
  94. [BOSTON_GIC] = { 0x16120000, 0x20000 },
  95. [BOSTON_CDMM] = { 0x16140000, 0x8000 },
  96. [BOSTON_CPC] = { 0x16200000, 0x8000 },
  97. [BOSTON_PLATREG] = { 0x17ffd000, 0x1000 },
  98. [BOSTON_UART] = { 0x17ffe000, 0x20 },
  99. [BOSTON_LCD] = { 0x17fff000, 0x8 },
  100. [BOSTON_FLASH] = { 0x18000000, 0x8000000 },
  101. [BOSTON_PCIE1_MMIO] = { 0x20000000, 0x20000000 },
  102. [BOSTON_PCIE0_MMIO] = { 0x40000000, 0x40000000 },
  103. [BOSTON_HIGHDDR] = { 0x80000000, 0x0 },
  104. };
  105. enum boston_plat_reg {
  106. PLAT_FPGA_BUILD = 0x00,
  107. PLAT_CORE_CL = 0x04,
  108. PLAT_WRAPPER_CL = 0x08,
  109. PLAT_SYSCLK_STATUS = 0x0c,
  110. PLAT_SOFTRST_CTL = 0x10,
  111. #define PLAT_SOFTRST_CTL_SYSRESET (1 << 4)
  112. PLAT_DDR3_STATUS = 0x14,
  113. #define PLAT_DDR3_STATUS_LOCKED (1 << 0)
  114. #define PLAT_DDR3_STATUS_CALIBRATED (1 << 2)
  115. PLAT_PCIE_STATUS = 0x18,
  116. #define PLAT_PCIE_STATUS_PCIE0_LOCKED (1 << 0)
  117. #define PLAT_PCIE_STATUS_PCIE1_LOCKED (1 << 8)
  118. #define PLAT_PCIE_STATUS_PCIE2_LOCKED (1 << 16)
  119. PLAT_FLASH_CTL = 0x1c,
  120. PLAT_SPARE0 = 0x20,
  121. PLAT_SPARE1 = 0x24,
  122. PLAT_SPARE2 = 0x28,
  123. PLAT_SPARE3 = 0x2c,
  124. PLAT_MMCM_DIV = 0x30,
  125. #define PLAT_MMCM_DIV_CLK0DIV_SHIFT 0
  126. #define PLAT_MMCM_DIV_INPUT_SHIFT 8
  127. #define PLAT_MMCM_DIV_MUL_SHIFT 16
  128. #define PLAT_MMCM_DIV_CLK1DIV_SHIFT 24
  129. PLAT_BUILD_CFG = 0x34,
  130. #define PLAT_BUILD_CFG_IOCU_EN (1 << 0)
  131. #define PLAT_BUILD_CFG_PCIE0_EN (1 << 1)
  132. #define PLAT_BUILD_CFG_PCIE1_EN (1 << 2)
  133. #define PLAT_BUILD_CFG_PCIE2_EN (1 << 3)
  134. PLAT_DDR_CFG = 0x38,
  135. #define PLAT_DDR_CFG_SIZE (0xf << 0)
  136. #define PLAT_DDR_CFG_MHZ (0xfff << 4)
  137. PLAT_NOC_PCIE0_ADDR = 0x3c,
  138. PLAT_NOC_PCIE1_ADDR = 0x40,
  139. PLAT_NOC_PCIE2_ADDR = 0x44,
  140. PLAT_SYS_CTL = 0x48,
  141. };
  142. static void boston_lcd_event(void *opaque, QEMUChrEvent event)
  143. {
  144. BostonState *s = opaque;
  145. if (event == CHR_EVENT_OPENED && !s->lcd_inited) {
  146. qemu_chr_fe_printf(&s->lcd_display, " ");
  147. s->lcd_inited = true;
  148. }
  149. }
  150. static uint64_t boston_lcd_read(void *opaque, hwaddr addr,
  151. unsigned size)
  152. {
  153. BostonState *s = opaque;
  154. uint64_t val = 0;
  155. switch (size) {
  156. case 8:
  157. val |= (uint64_t)s->lcd_content[(addr + 7) & 0x7] << 56;
  158. val |= (uint64_t)s->lcd_content[(addr + 6) & 0x7] << 48;
  159. val |= (uint64_t)s->lcd_content[(addr + 5) & 0x7] << 40;
  160. val |= (uint64_t)s->lcd_content[(addr + 4) & 0x7] << 32;
  161. /* fall through */
  162. case 4:
  163. val |= (uint64_t)s->lcd_content[(addr + 3) & 0x7] << 24;
  164. val |= (uint64_t)s->lcd_content[(addr + 2) & 0x7] << 16;
  165. /* fall through */
  166. case 2:
  167. val |= (uint64_t)s->lcd_content[(addr + 1) & 0x7] << 8;
  168. /* fall through */
  169. case 1:
  170. val |= (uint64_t)s->lcd_content[(addr + 0) & 0x7];
  171. break;
  172. }
  173. return val;
  174. }
  175. static void boston_lcd_write(void *opaque, hwaddr addr,
  176. uint64_t val, unsigned size)
  177. {
  178. BostonState *s = opaque;
  179. switch (size) {
  180. case 8:
  181. s->lcd_content[(addr + 7) & 0x7] = val >> 56;
  182. s->lcd_content[(addr + 6) & 0x7] = val >> 48;
  183. s->lcd_content[(addr + 5) & 0x7] = val >> 40;
  184. s->lcd_content[(addr + 4) & 0x7] = val >> 32;
  185. /* fall through */
  186. case 4:
  187. s->lcd_content[(addr + 3) & 0x7] = val >> 24;
  188. s->lcd_content[(addr + 2) & 0x7] = val >> 16;
  189. /* fall through */
  190. case 2:
  191. s->lcd_content[(addr + 1) & 0x7] = val >> 8;
  192. /* fall through */
  193. case 1:
  194. s->lcd_content[(addr + 0) & 0x7] = val;
  195. break;
  196. }
  197. qemu_chr_fe_printf(&s->lcd_display,
  198. "\r%-8.8s", s->lcd_content);
  199. }
  200. static const MemoryRegionOps boston_lcd_ops = {
  201. .read = boston_lcd_read,
  202. .write = boston_lcd_write,
  203. .endianness = DEVICE_NATIVE_ENDIAN,
  204. };
  205. static uint64_t boston_platreg_read(void *opaque, hwaddr addr,
  206. unsigned size)
  207. {
  208. BostonState *s = opaque;
  209. uint32_t gic_freq, val;
  210. if (size != 4) {
  211. qemu_log_mask(LOG_UNIMP, "%uB platform register read\n", size);
  212. return 0;
  213. }
  214. switch (addr & 0xffff) {
  215. case PLAT_FPGA_BUILD:
  216. case PLAT_CORE_CL:
  217. case PLAT_WRAPPER_CL:
  218. return 0;
  219. case PLAT_DDR3_STATUS:
  220. return PLAT_DDR3_STATUS_LOCKED | PLAT_DDR3_STATUS_CALIBRATED;
  221. case PLAT_MMCM_DIV:
  222. gic_freq = mips_gictimer_get_freq(s->cps.gic.gic_timer) / 1000000;
  223. val = gic_freq << PLAT_MMCM_DIV_INPUT_SHIFT;
  224. val |= 1 << PLAT_MMCM_DIV_MUL_SHIFT;
  225. val |= 1 << PLAT_MMCM_DIV_CLK0DIV_SHIFT;
  226. val |= 1 << PLAT_MMCM_DIV_CLK1DIV_SHIFT;
  227. return val;
  228. case PLAT_BUILD_CFG:
  229. val = PLAT_BUILD_CFG_PCIE0_EN;
  230. val |= PLAT_BUILD_CFG_PCIE1_EN;
  231. val |= PLAT_BUILD_CFG_PCIE2_EN;
  232. return val;
  233. case PLAT_DDR_CFG:
  234. val = s->mach->ram_size / GiB;
  235. assert(!(val & ~PLAT_DDR_CFG_SIZE));
  236. val |= PLAT_DDR_CFG_MHZ;
  237. return val;
  238. default:
  239. qemu_log_mask(LOG_UNIMP, "Read platform register 0x%" HWADDR_PRIx "\n",
  240. addr & 0xffff);
  241. return 0;
  242. }
  243. }
  244. static void boston_platreg_write(void *opaque, hwaddr addr,
  245. uint64_t val, unsigned size)
  246. {
  247. if (size != 4) {
  248. qemu_log_mask(LOG_UNIMP, "%uB platform register write\n", size);
  249. return;
  250. }
  251. switch (addr & 0xffff) {
  252. case PLAT_FPGA_BUILD:
  253. case PLAT_CORE_CL:
  254. case PLAT_WRAPPER_CL:
  255. case PLAT_DDR3_STATUS:
  256. case PLAT_PCIE_STATUS:
  257. case PLAT_MMCM_DIV:
  258. case PLAT_BUILD_CFG:
  259. case PLAT_DDR_CFG:
  260. /* read only */
  261. break;
  262. case PLAT_SOFTRST_CTL:
  263. if (val & PLAT_SOFTRST_CTL_SYSRESET) {
  264. qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
  265. }
  266. break;
  267. default:
  268. qemu_log_mask(LOG_UNIMP, "Write platform register 0x%" HWADDR_PRIx
  269. " = 0x%" PRIx64 "\n", addr & 0xffff, val);
  270. break;
  271. }
  272. }
  273. static const MemoryRegionOps boston_platreg_ops = {
  274. .read = boston_platreg_read,
  275. .write = boston_platreg_write,
  276. .endianness = DEVICE_NATIVE_ENDIAN,
  277. };
  278. static void mips_boston_instance_init(Object *obj)
  279. {
  280. BostonState *s = BOSTON(obj);
  281. s->cpuclk = qdev_init_clock_out(DEVICE(obj), "cpu-refclk");
  282. clock_set_hz(s->cpuclk, 1000000000); /* 1 GHz */
  283. }
  284. static const TypeInfo boston_device = {
  285. .name = TYPE_BOSTON,
  286. .parent = TYPE_SYS_BUS_DEVICE,
  287. .instance_size = sizeof(BostonState),
  288. .instance_init = mips_boston_instance_init,
  289. };
  290. static void boston_register_types(void)
  291. {
  292. type_register_static(&boston_device);
  293. }
  294. type_init(boston_register_types)
  295. static void gen_firmware(void *p, hwaddr kernel_entry, hwaddr fdt_addr)
  296. {
  297. uint64_t regaddr;
  298. /* Move CM GCRs */
  299. regaddr = cpu_mips_phys_to_kseg1(NULL, GCR_BASE_ADDR + GCR_BASE_OFS),
  300. bl_gen_write_ulong(&p, regaddr,
  301. boston_memmap[BOSTON_CM].base);
  302. /* Move & enable GIC GCRs */
  303. regaddr = cpu_mips_phys_to_kseg1(NULL, boston_memmap[BOSTON_CM].base
  304. + GCR_GIC_BASE_OFS),
  305. bl_gen_write_ulong(&p, regaddr,
  306. boston_memmap[BOSTON_GIC].base | GCR_GIC_BASE_GICEN_MSK);
  307. /* Move & enable CPC GCRs */
  308. regaddr = cpu_mips_phys_to_kseg1(NULL, boston_memmap[BOSTON_CM].base
  309. + GCR_CPC_BASE_OFS),
  310. bl_gen_write_ulong(&p, regaddr,
  311. boston_memmap[BOSTON_CPC].base | GCR_CPC_BASE_CPCEN_MSK);
  312. /*
  313. * Setup argument registers to follow the UHI boot protocol:
  314. *
  315. * a0/$4 = -2
  316. * a1/$5 = virtual address of FDT
  317. * a2/$6 = 0
  318. * a3/$7 = 0
  319. */
  320. bl_gen_jump_kernel(&p,
  321. true, 0, true, (int32_t)-2,
  322. true, fdt_addr, true, 0, true, 0,
  323. kernel_entry);
  324. }
  325. static const void *boston_fdt_filter(void *opaque, const void *fdt_orig,
  326. const void *match_data, hwaddr *load_addr)
  327. {
  328. BostonState *s = BOSTON(opaque);
  329. MachineState *machine = s->mach;
  330. const char *cmdline;
  331. int err;
  332. size_t ram_low_sz, ram_high_sz;
  333. size_t fdt_sz = fdt_totalsize(fdt_orig) * 2;
  334. g_autofree void *fdt = g_malloc0(fdt_sz);
  335. uint8_t rng_seed[32];
  336. err = fdt_open_into(fdt_orig, fdt, fdt_sz);
  337. if (err) {
  338. fprintf(stderr, "unable to open FDT\n");
  339. return NULL;
  340. }
  341. qemu_guest_getrandom_nofail(rng_seed, sizeof(rng_seed));
  342. qemu_fdt_setprop(fdt, "/chosen", "rng-seed", rng_seed, sizeof(rng_seed));
  343. cmdline = (machine->kernel_cmdline && machine->kernel_cmdline[0])
  344. ? machine->kernel_cmdline : " ";
  345. err = qemu_fdt_setprop_string(fdt, "/chosen", "bootargs", cmdline);
  346. if (err < 0) {
  347. fprintf(stderr, "couldn't set /chosen/bootargs\n");
  348. return NULL;
  349. }
  350. ram_low_sz = MIN(256 * MiB, machine->ram_size);
  351. ram_high_sz = machine->ram_size - ram_low_sz;
  352. qemu_fdt_setprop_sized_cells(fdt, "/memory@0", "reg",
  353. 1, boston_memmap[BOSTON_LOWDDR].base, 1, ram_low_sz,
  354. 1, boston_memmap[BOSTON_HIGHDDR].base + ram_low_sz,
  355. 1, ram_high_sz);
  356. fdt = g_realloc(fdt, fdt_totalsize(fdt));
  357. qemu_fdt_dumpdtb(fdt, fdt_sz);
  358. s->fdt_base = *load_addr;
  359. return g_steal_pointer(&fdt);
  360. }
  361. static const void *boston_kernel_filter(void *opaque, const void *kernel,
  362. hwaddr *load_addr, hwaddr *entry_addr)
  363. {
  364. BostonState *s = BOSTON(opaque);
  365. s->kernel_entry = *entry_addr;
  366. return kernel;
  367. }
  368. static const struct fit_loader_match boston_matches[] = {
  369. { "img,boston" },
  370. { NULL },
  371. };
  372. static const struct fit_loader boston_fit_loader = {
  373. .matches = boston_matches,
  374. .addr_to_phys = cpu_mips_kseg0_to_phys,
  375. .fdt_filter = boston_fdt_filter,
  376. .kernel_filter = boston_kernel_filter,
  377. };
  378. static inline XilinxPCIEHost *
  379. xilinx_pcie_init(MemoryRegion *sys_mem, uint32_t bus_nr,
  380. hwaddr cfg_base, uint64_t cfg_size,
  381. hwaddr mmio_base, uint64_t mmio_size,
  382. qemu_irq irq)
  383. {
  384. DeviceState *dev;
  385. MemoryRegion *cfg, *mmio;
  386. dev = qdev_new(TYPE_XILINX_PCIE_HOST);
  387. qdev_prop_set_uint32(dev, "bus_nr", bus_nr);
  388. qdev_prop_set_uint64(dev, "cfg_base", cfg_base);
  389. qdev_prop_set_uint64(dev, "cfg_size", cfg_size);
  390. qdev_prop_set_uint64(dev, "mmio_base", mmio_base);
  391. qdev_prop_set_uint64(dev, "mmio_size", mmio_size);
  392. sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
  393. cfg = sysbus_mmio_get_region(SYS_BUS_DEVICE(dev), 0);
  394. memory_region_add_subregion_overlap(sys_mem, cfg_base, cfg, 0);
  395. mmio = sysbus_mmio_get_region(SYS_BUS_DEVICE(dev), 1);
  396. memory_region_add_subregion_overlap(sys_mem, 0, mmio, 0);
  397. qdev_connect_gpio_out_named(dev, "interrupt_out", 0, irq);
  398. return XILINX_PCIE_HOST(dev);
  399. }
  400. static void fdt_create_pcie(void *fdt, int gic_ph, int irq, hwaddr reg_base,
  401. hwaddr reg_size, hwaddr mmio_base, hwaddr mmio_size)
  402. {
  403. int i;
  404. char *name, *intc_name;
  405. uint32_t intc_ph;
  406. uint32_t interrupt_map[FDT_PCI_IRQ_MAP_PINS][FDT_PCI_IRQ_MAP_DESCS];
  407. intc_ph = qemu_fdt_alloc_phandle(fdt);
  408. name = g_strdup_printf("/soc/pci@%" HWADDR_PRIx, reg_base);
  409. qemu_fdt_add_subnode(fdt, name);
  410. qemu_fdt_setprop_string(fdt, name, "compatible",
  411. "xlnx,axi-pcie-host-1.00.a");
  412. qemu_fdt_setprop_string(fdt, name, "device_type", "pci");
  413. qemu_fdt_setprop_cells(fdt, name, "reg", reg_base, reg_size);
  414. qemu_fdt_setprop_cell(fdt, name, "#address-cells", 3);
  415. qemu_fdt_setprop_cell(fdt, name, "#size-cells", 2);
  416. qemu_fdt_setprop_cell(fdt, name, "#interrupt-cells", 1);
  417. qemu_fdt_setprop_cell(fdt, name, "interrupt-parent", gic_ph);
  418. qemu_fdt_setprop_cells(fdt, name, "interrupts", FDT_GIC_SHARED, irq,
  419. FDT_IRQ_TYPE_LEVEL_HIGH);
  420. qemu_fdt_setprop_cells(fdt, name, "ranges", 0x02000000, 0, mmio_base,
  421. mmio_base, 0, mmio_size);
  422. qemu_fdt_setprop_cells(fdt, name, "bus-range", 0x00, 0xff);
  423. intc_name = g_strdup_printf("%s/interrupt-controller", name);
  424. qemu_fdt_add_subnode(fdt, intc_name);
  425. qemu_fdt_setprop(fdt, intc_name, "interrupt-controller", NULL, 0);
  426. qemu_fdt_setprop_cell(fdt, intc_name, "#address-cells", 0);
  427. qemu_fdt_setprop_cell(fdt, intc_name, "#interrupt-cells", 1);
  428. qemu_fdt_setprop_cell(fdt, intc_name, "phandle", intc_ph);
  429. qemu_fdt_setprop_cells(fdt, name, "interrupt-map-mask", 0, 0, 0, 7);
  430. for (i = 0; i < FDT_PCI_IRQ_MAP_PINS; i++) {
  431. uint32_t *irqmap = interrupt_map[i];
  432. irqmap[0] = cpu_to_be32(0);
  433. irqmap[1] = cpu_to_be32(0);
  434. irqmap[2] = cpu_to_be32(0);
  435. irqmap[3] = cpu_to_be32(i + 1);
  436. irqmap[4] = cpu_to_be32(intc_ph);
  437. irqmap[5] = cpu_to_be32(i + 1);
  438. }
  439. qemu_fdt_setprop(fdt, name, "interrupt-map",
  440. &interrupt_map, sizeof(interrupt_map));
  441. g_free(intc_name);
  442. g_free(name);
  443. }
  444. static const void *create_fdt(BostonState *s,
  445. const MemMapEntry *memmap, int *dt_size)
  446. {
  447. void *fdt;
  448. int cpu;
  449. MachineState *ms = s->mach;
  450. uint32_t platreg_ph, gic_ph, clk_ph;
  451. char *name, *gic_name, *platreg_name, *stdout_name;
  452. static const char * const syscon_compat[2] = {
  453. "img,boston-platform-regs", "syscon"
  454. };
  455. fdt = create_device_tree(dt_size);
  456. if (!fdt) {
  457. error_report("create_device_tree() failed");
  458. exit(1);
  459. }
  460. platreg_ph = qemu_fdt_alloc_phandle(fdt);
  461. gic_ph = qemu_fdt_alloc_phandle(fdt);
  462. clk_ph = qemu_fdt_alloc_phandle(fdt);
  463. qemu_fdt_setprop_string(fdt, "/", "model", "img,boston");
  464. qemu_fdt_setprop_string(fdt, "/", "compatible", "img,boston");
  465. qemu_fdt_setprop_cell(fdt, "/", "#size-cells", 0x1);
  466. qemu_fdt_setprop_cell(fdt, "/", "#address-cells", 0x1);
  467. qemu_fdt_add_subnode(fdt, "/cpus");
  468. qemu_fdt_setprop_cell(fdt, "/cpus", "#size-cells", 0x0);
  469. qemu_fdt_setprop_cell(fdt, "/cpus", "#address-cells", 0x1);
  470. for (cpu = 0; cpu < ms->smp.cpus; cpu++) {
  471. name = g_strdup_printf("/cpus/cpu@%d", cpu);
  472. qemu_fdt_add_subnode(fdt, name);
  473. qemu_fdt_setprop_string(fdt, name, "compatible", "img,mips");
  474. qemu_fdt_setprop_string(fdt, name, "status", "okay");
  475. qemu_fdt_setprop_cell(fdt, name, "reg", cpu);
  476. qemu_fdt_setprop_string(fdt, name, "device_type", "cpu");
  477. qemu_fdt_setprop_cells(fdt, name, "clocks", clk_ph, FDT_BOSTON_CLK_CPU);
  478. g_free(name);
  479. }
  480. qemu_fdt_add_subnode(fdt, "/soc");
  481. qemu_fdt_setprop(fdt, "/soc", "ranges", NULL, 0);
  482. qemu_fdt_setprop_string(fdt, "/soc", "compatible", "simple-bus");
  483. qemu_fdt_setprop_cell(fdt, "/soc", "#size-cells", 0x1);
  484. qemu_fdt_setprop_cell(fdt, "/soc", "#address-cells", 0x1);
  485. fdt_create_pcie(fdt, gic_ph, 2,
  486. memmap[BOSTON_PCIE0].base, memmap[BOSTON_PCIE0].size,
  487. memmap[BOSTON_PCIE0_MMIO].base, memmap[BOSTON_PCIE0_MMIO].size);
  488. fdt_create_pcie(fdt, gic_ph, 1,
  489. memmap[BOSTON_PCIE1].base, memmap[BOSTON_PCIE1].size,
  490. memmap[BOSTON_PCIE1_MMIO].base, memmap[BOSTON_PCIE1_MMIO].size);
  491. fdt_create_pcie(fdt, gic_ph, 0,
  492. memmap[BOSTON_PCIE2].base, memmap[BOSTON_PCIE2].size,
  493. memmap[BOSTON_PCIE2_MMIO].base, memmap[BOSTON_PCIE2_MMIO].size);
  494. /* GIC with it's timer node */
  495. gic_name = g_strdup_printf("/soc/interrupt-controller@%" HWADDR_PRIx,
  496. memmap[BOSTON_GIC].base);
  497. qemu_fdt_add_subnode(fdt, gic_name);
  498. qemu_fdt_setprop_string(fdt, gic_name, "compatible", "mti,gic");
  499. qemu_fdt_setprop_cells(fdt, gic_name, "reg", memmap[BOSTON_GIC].base,
  500. memmap[BOSTON_GIC].size);
  501. qemu_fdt_setprop(fdt, gic_name, "interrupt-controller", NULL, 0);
  502. qemu_fdt_setprop_cell(fdt, gic_name, "#interrupt-cells", 3);
  503. qemu_fdt_setprop_cell(fdt, gic_name, "phandle", gic_ph);
  504. name = g_strdup_printf("%s/timer", gic_name);
  505. qemu_fdt_add_subnode(fdt, name);
  506. qemu_fdt_setprop_string(fdt, name, "compatible", "mti,gic-timer");
  507. qemu_fdt_setprop_cells(fdt, name, "interrupts", FDT_GIC_LOCAL, 1,
  508. FDT_IRQ_TYPE_NONE);
  509. qemu_fdt_setprop_cells(fdt, name, "clocks", clk_ph, FDT_BOSTON_CLK_CPU);
  510. g_free(name);
  511. g_free(gic_name);
  512. /* CDMM node */
  513. name = g_strdup_printf("/soc/cdmm@%" HWADDR_PRIx, memmap[BOSTON_CDMM].base);
  514. qemu_fdt_add_subnode(fdt, name);
  515. qemu_fdt_setprop_string(fdt, name, "compatible", "mti,mips-cdmm");
  516. qemu_fdt_setprop_cells(fdt, name, "reg", memmap[BOSTON_CDMM].base,
  517. memmap[BOSTON_CDMM].size);
  518. g_free(name);
  519. /* CPC node */
  520. name = g_strdup_printf("/soc/cpc@%" HWADDR_PRIx, memmap[BOSTON_CPC].base);
  521. qemu_fdt_add_subnode(fdt, name);
  522. qemu_fdt_setprop_string(fdt, name, "compatible", "mti,mips-cpc");
  523. qemu_fdt_setprop_cells(fdt, name, "reg", memmap[BOSTON_CPC].base,
  524. memmap[BOSTON_CPC].size);
  525. g_free(name);
  526. /* platreg and it's clk node */
  527. platreg_name = g_strdup_printf("/soc/system-controller@%" HWADDR_PRIx,
  528. memmap[BOSTON_PLATREG].base);
  529. qemu_fdt_add_subnode(fdt, platreg_name);
  530. qemu_fdt_setprop_string_array(fdt, platreg_name, "compatible",
  531. (char **)&syscon_compat,
  532. ARRAY_SIZE(syscon_compat));
  533. qemu_fdt_setprop_cells(fdt, platreg_name, "reg",
  534. memmap[BOSTON_PLATREG].base,
  535. memmap[BOSTON_PLATREG].size);
  536. qemu_fdt_setprop_cell(fdt, platreg_name, "phandle", platreg_ph);
  537. name = g_strdup_printf("%s/clock", platreg_name);
  538. qemu_fdt_add_subnode(fdt, name);
  539. qemu_fdt_setprop_string(fdt, name, "compatible", "img,boston-clock");
  540. qemu_fdt_setprop_cell(fdt, name, "#clock-cells", 1);
  541. qemu_fdt_setprop_cell(fdt, name, "phandle", clk_ph);
  542. g_free(name);
  543. g_free(platreg_name);
  544. /* reboot node */
  545. name = g_strdup_printf("/soc/reboot");
  546. qemu_fdt_add_subnode(fdt, name);
  547. qemu_fdt_setprop_string(fdt, name, "compatible", "syscon-reboot");
  548. qemu_fdt_setprop_cell(fdt, name, "regmap", platreg_ph);
  549. qemu_fdt_setprop_cell(fdt, name, "offset", 0x10);
  550. qemu_fdt_setprop_cell(fdt, name, "mask", 0x10);
  551. g_free(name);
  552. /* uart node */
  553. name = g_strdup_printf("/soc/uart@%" HWADDR_PRIx, memmap[BOSTON_UART].base);
  554. qemu_fdt_add_subnode(fdt, name);
  555. qemu_fdt_setprop_string(fdt, name, "compatible", "ns16550a");
  556. qemu_fdt_setprop_cells(fdt, name, "reg", memmap[BOSTON_UART].base,
  557. memmap[BOSTON_UART].size);
  558. qemu_fdt_setprop_cell(fdt, name, "reg-shift", 0x2);
  559. qemu_fdt_setprop_cell(fdt, name, "interrupt-parent", gic_ph);
  560. qemu_fdt_setprop_cells(fdt, name, "interrupts", FDT_GIC_SHARED, 3,
  561. FDT_IRQ_TYPE_LEVEL_HIGH);
  562. qemu_fdt_setprop_cells(fdt, name, "clocks", clk_ph, FDT_BOSTON_CLK_SYS);
  563. qemu_fdt_add_subnode(fdt, "/chosen");
  564. stdout_name = g_strdup_printf("%s:115200", name);
  565. qemu_fdt_setprop_string(fdt, "/chosen", "stdout-path", stdout_name);
  566. g_free(stdout_name);
  567. g_free(name);
  568. /* lcd node */
  569. name = g_strdup_printf("/soc/lcd@%" HWADDR_PRIx, memmap[BOSTON_LCD].base);
  570. qemu_fdt_add_subnode(fdt, name);
  571. qemu_fdt_setprop_string(fdt, name, "compatible", "img,boston-lcd");
  572. qemu_fdt_setprop_cells(fdt, name, "reg", memmap[BOSTON_LCD].base,
  573. memmap[BOSTON_LCD].size);
  574. g_free(name);
  575. name = g_strdup_printf("/memory@0");
  576. qemu_fdt_add_subnode(fdt, name);
  577. qemu_fdt_setprop_string(fdt, name, "device_type", "memory");
  578. g_free(name);
  579. return fdt;
  580. }
  581. static void boston_mach_init(MachineState *machine)
  582. {
  583. DeviceState *dev;
  584. BostonState *s;
  585. MemoryRegion *flash, *ddr_low_alias, *lcd, *platreg;
  586. MemoryRegion *sys_mem = get_system_memory();
  587. XilinxPCIEHost *pcie2;
  588. PCIDevice *pdev;
  589. AHCIPCIState *ich9;
  590. DriveInfo *hd[6];
  591. Chardev *chr;
  592. int fw_size, fit_err;
  593. if ((machine->ram_size % GiB) ||
  594. (machine->ram_size > (2 * GiB))) {
  595. error_report("Memory size must be 1GB or 2GB");
  596. exit(1);
  597. }
  598. dev = qdev_new(TYPE_BOSTON);
  599. sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
  600. s = BOSTON(dev);
  601. s->mach = machine;
  602. if (!cpu_type_supports_cps_smp(machine->cpu_type)) {
  603. error_report("Boston requires CPUs which support CPS");
  604. exit(1);
  605. }
  606. object_initialize_child(OBJECT(machine), "cps", &s->cps, TYPE_MIPS_CPS);
  607. object_property_set_str(OBJECT(&s->cps), "cpu-type", machine->cpu_type,
  608. &error_fatal);
  609. object_property_set_uint(OBJECT(&s->cps), "num-vp", machine->smp.cpus,
  610. &error_fatal);
  611. qdev_connect_clock_in(DEVICE(&s->cps), "clk-in",
  612. qdev_get_clock_out(dev, "cpu-refclk"));
  613. sysbus_realize(SYS_BUS_DEVICE(&s->cps), &error_fatal);
  614. sysbus_mmio_map_overlap(SYS_BUS_DEVICE(&s->cps), 0, 0, 1);
  615. flash = g_new(MemoryRegion, 1);
  616. memory_region_init_rom(flash, NULL, "boston.flash",
  617. boston_memmap[BOSTON_FLASH].size, &error_fatal);
  618. memory_region_add_subregion_overlap(sys_mem,
  619. boston_memmap[BOSTON_FLASH].base,
  620. flash, 0);
  621. memory_region_add_subregion_overlap(sys_mem,
  622. boston_memmap[BOSTON_HIGHDDR].base,
  623. machine->ram, 0);
  624. ddr_low_alias = g_new(MemoryRegion, 1);
  625. memory_region_init_alias(ddr_low_alias, NULL, "boston_low.ddr",
  626. machine->ram, 0,
  627. MIN(machine->ram_size, (256 * MiB)));
  628. memory_region_add_subregion_overlap(sys_mem, 0, ddr_low_alias, 0);
  629. xilinx_pcie_init(sys_mem, 0,
  630. boston_memmap[BOSTON_PCIE0].base,
  631. boston_memmap[BOSTON_PCIE0].size,
  632. boston_memmap[BOSTON_PCIE0_MMIO].base,
  633. boston_memmap[BOSTON_PCIE0_MMIO].size,
  634. get_cps_irq(&s->cps, 2));
  635. xilinx_pcie_init(sys_mem, 1,
  636. boston_memmap[BOSTON_PCIE1].base,
  637. boston_memmap[BOSTON_PCIE1].size,
  638. boston_memmap[BOSTON_PCIE1_MMIO].base,
  639. boston_memmap[BOSTON_PCIE1_MMIO].size,
  640. get_cps_irq(&s->cps, 1));
  641. pcie2 = xilinx_pcie_init(sys_mem, 2,
  642. boston_memmap[BOSTON_PCIE2].base,
  643. boston_memmap[BOSTON_PCIE2].size,
  644. boston_memmap[BOSTON_PCIE2_MMIO].base,
  645. boston_memmap[BOSTON_PCIE2_MMIO].size,
  646. get_cps_irq(&s->cps, 0));
  647. platreg = g_new(MemoryRegion, 1);
  648. memory_region_init_io(platreg, NULL, &boston_platreg_ops, s,
  649. "boston-platregs",
  650. boston_memmap[BOSTON_PLATREG].size);
  651. memory_region_add_subregion_overlap(sys_mem,
  652. boston_memmap[BOSTON_PLATREG].base, platreg, 0);
  653. s->uart = serial_mm_init(sys_mem, boston_memmap[BOSTON_UART].base, 2,
  654. get_cps_irq(&s->cps, 3), 10000000,
  655. serial_hd(0), DEVICE_NATIVE_ENDIAN);
  656. lcd = g_new(MemoryRegion, 1);
  657. memory_region_init_io(lcd, NULL, &boston_lcd_ops, s, "boston-lcd", 0x8);
  658. memory_region_add_subregion_overlap(sys_mem,
  659. boston_memmap[BOSTON_LCD].base, lcd, 0);
  660. chr = qemu_chr_new("lcd", "vc:320x240", NULL);
  661. qemu_chr_fe_init(&s->lcd_display, chr, NULL);
  662. qemu_chr_fe_set_handlers(&s->lcd_display, NULL, NULL,
  663. boston_lcd_event, NULL, s, NULL, true);
  664. pdev = pci_create_simple_multifunction(&PCI_BRIDGE(&pcie2->root)->sec_bus,
  665. PCI_DEVFN(0, 0), TYPE_ICH9_AHCI);
  666. ich9 = ICH9_AHCI(pdev);
  667. g_assert(ARRAY_SIZE(hd) == ich9->ahci.ports);
  668. ide_drive_get(hd, ich9->ahci.ports);
  669. ahci_ide_create_devs(&ich9->ahci, hd);
  670. if (machine->firmware) {
  671. fw_size = load_image_targphys(machine->firmware,
  672. 0x1fc00000, 4 * MiB);
  673. if (fw_size == -1) {
  674. error_report("unable to load firmware image '%s'",
  675. machine->firmware);
  676. exit(1);
  677. }
  678. } else if (machine->kernel_filename) {
  679. uint64_t kernel_entry, kernel_high;
  680. ssize_t kernel_size;
  681. kernel_size = load_elf(machine->kernel_filename, NULL,
  682. cpu_mips_kseg0_to_phys, NULL,
  683. &kernel_entry, NULL, &kernel_high,
  684. NULL, ELFDATA2LSB, EM_MIPS, 1, 0);
  685. if (kernel_size > 0) {
  686. int dt_size;
  687. g_autofree const void *dtb_file_data = NULL;
  688. g_autofree const void *dtb_load_data = NULL;
  689. hwaddr dtb_paddr = QEMU_ALIGN_UP(kernel_high, 64 * KiB);
  690. hwaddr dtb_vaddr = cpu_mips_phys_to_kseg0(NULL, dtb_paddr);
  691. s->kernel_entry = kernel_entry;
  692. if (machine->dtb) {
  693. dtb_file_data = load_device_tree(machine->dtb, &dt_size);
  694. } else {
  695. dtb_file_data = create_fdt(s, boston_memmap, &dt_size);
  696. }
  697. dtb_load_data = boston_fdt_filter(s, dtb_file_data,
  698. NULL, &dtb_vaddr);
  699. /* Calculate real fdt size after filter */
  700. dt_size = fdt_totalsize(dtb_load_data);
  701. rom_add_blob_fixed("dtb", dtb_load_data, dt_size, dtb_paddr);
  702. qemu_register_reset_nosnapshotload(qemu_fdt_randomize_seeds,
  703. rom_ptr(dtb_paddr, dt_size));
  704. } else {
  705. /* Try to load file as FIT */
  706. fit_err = load_fit(&boston_fit_loader, machine->kernel_filename, s);
  707. if (fit_err) {
  708. error_report("unable to load kernel image");
  709. exit(1);
  710. }
  711. }
  712. gen_firmware(memory_region_get_ram_ptr(flash) + 0x7c00000,
  713. s->kernel_entry, s->fdt_base);
  714. } else if (!qtest_enabled()) {
  715. error_report("Please provide either a -kernel or -bios argument");
  716. exit(1);
  717. }
  718. }
  719. static void boston_mach_class_init(MachineClass *mc)
  720. {
  721. mc->desc = "MIPS Boston";
  722. mc->init = boston_mach_init;
  723. mc->block_default_type = IF_IDE;
  724. mc->default_ram_size = 1 * GiB;
  725. mc->default_ram_id = "boston.ddr";
  726. mc->max_cpus = 16;
  727. mc->default_cpu_type = MIPS_CPU_TYPE_NAME("I6400");
  728. }
  729. DEFINE_MACHINE("boston", boston_mach_class_init)