2
0

leon3.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. /*
  2. * QEMU Leon3 System Emulator
  3. *
  4. * SPDX-License-Identifier: MIT
  5. *
  6. * Copyright (c) 2010-2024 AdaCore
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  21. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #include "qemu/osdep.h"
  27. #include "qemu/units.h"
  28. #include "qemu/error-report.h"
  29. #include "qapi/error.h"
  30. #include "qemu/datadir.h"
  31. #include "cpu.h"
  32. #include "hw/irq.h"
  33. #include "qemu/timer.h"
  34. #include "hw/ptimer.h"
  35. #include "hw/qdev-properties.h"
  36. #include "system/system.h"
  37. #include "system/qtest.h"
  38. #include "system/reset.h"
  39. #include "hw/boards.h"
  40. #include "hw/loader.h"
  41. #include "elf.h"
  42. #include "trace.h"
  43. #include "hw/timer/grlib_gptimer.h"
  44. #include "hw/char/grlib_uart.h"
  45. #include "hw/intc/grlib_irqmp.h"
  46. #include "hw/misc/grlib_ahb_apb_pnp.h"
  47. /* Default system clock. */
  48. #define CPU_CLK (40 * 1000 * 1000)
  49. #define LEON3_PROM_FILENAME "u-boot.bin"
  50. #define LEON3_PROM_OFFSET (0x00000000)
  51. #define LEON3_RAM_OFFSET (0x40000000)
  52. #define MAX_CPUS 4
  53. #define LEON3_UART_OFFSET (0x80000100)
  54. #define LEON3_UART_IRQ (3)
  55. #define LEON3_IRQMP_OFFSET (0x80000200)
  56. #define LEON3_TIMER_OFFSET (0x80000300)
  57. #define LEON3_TIMER_IRQ (6)
  58. #define LEON3_TIMER_COUNT (2)
  59. #define LEON3_APB_PNP_OFFSET (0x800FF000)
  60. #define LEON3_AHB_PNP_OFFSET (0xFFFFF000)
  61. typedef struct ResetData {
  62. struct CPUResetData {
  63. int id;
  64. SPARCCPU *cpu;
  65. } info[MAX_CPUS];
  66. uint32_t entry; /* save kernel entry in case of reset */
  67. } ResetData;
  68. static uint32_t *gen_store_u32(uint32_t *code, hwaddr addr, uint32_t val)
  69. {
  70. stl_p(code++, 0x82100000); /* mov %g0, %g1 */
  71. stl_p(code++, 0x84100000); /* mov %g0, %g2 */
  72. stl_p(code++, 0x03000000 +
  73. extract32(addr, 10, 22));
  74. /* sethi %hi(addr), %g1 */
  75. stl_p(code++, 0x82106000 +
  76. extract32(addr, 0, 10));
  77. /* or %g1, addr, %g1 */
  78. stl_p(code++, 0x05000000 +
  79. extract32(val, 10, 22));
  80. /* sethi %hi(val), %g2 */
  81. stl_p(code++, 0x8410a000 +
  82. extract32(val, 0, 10));
  83. /* or %g2, val, %g2 */
  84. stl_p(code++, 0xc4204000); /* st %g2, [ %g1 ] */
  85. return code;
  86. }
  87. /*
  88. * When loading a kernel in RAM the machine is expected to be in a different
  89. * state (eg: initialized by the bootloader). This little code reproduces
  90. * this behavior. Also this code can be executed by the secondary cpus as
  91. * well since it looks at the %asr17 register before doing any
  92. * initialization, it allows to use the same reset address for all the
  93. * cpus.
  94. */
  95. static void write_bootloader(void *ptr, hwaddr kernel_addr)
  96. {
  97. uint32_t *p = ptr;
  98. uint32_t *sec_cpu_branch_p = NULL;
  99. /* If we are running on a secondary CPU, jump directly to the kernel. */
  100. stl_p(p++, 0x85444000); /* rd %asr17, %g2 */
  101. stl_p(p++, 0x8530a01c); /* srl %g2, 0x1c, %g2 */
  102. stl_p(p++, 0x80908000); /* tst %g2 */
  103. /* Filled below. */
  104. sec_cpu_branch_p = p;
  105. stl_p(p++, 0x0BADC0DE); /* bne xxx */
  106. stl_p(p++, 0x01000000); /* nop */
  107. /* Initialize the UARTs */
  108. /* *UART_CONTROL = UART_RECEIVE_ENABLE | UART_TRANSMIT_ENABLE; */
  109. p = gen_store_u32(p, 0x80000108, 3);
  110. /* Initialize the TIMER 0 */
  111. /* *GPTIMER_SCALER_RELOAD = 40 - 1; */
  112. p = gen_store_u32(p, 0x80000304, 39);
  113. /* *GPTIMER0_COUNTER_RELOAD = 0xFFFE; */
  114. p = gen_store_u32(p, 0x80000314, 0xFFFFFFFE);
  115. /* *GPTIMER0_CONFIG = GPTIMER_ENABLE | GPTIMER_RESTART; */
  116. p = gen_store_u32(p, 0x80000318, 3);
  117. /* Now, the relative branch above can be computed. */
  118. stl_p(sec_cpu_branch_p, 0x12800000
  119. + (p - sec_cpu_branch_p));
  120. /* JUMP to the entry point */
  121. stl_p(p++, 0x82100000); /* mov %g0, %g1 */
  122. stl_p(p++, 0x03000000 + extract32(kernel_addr, 10, 22));
  123. /* sethi %hi(kernel_addr), %g1 */
  124. stl_p(p++, 0x82106000 + extract32(kernel_addr, 0, 10));
  125. /* or kernel_addr, %g1 */
  126. stl_p(p++, 0x81c04000); /* jmp %g1 */
  127. stl_p(p++, 0x01000000); /* nop */
  128. }
  129. static void leon3_cpu_reset(void *opaque)
  130. {
  131. struct CPUResetData *info = (struct CPUResetData *) opaque;
  132. int id = info->id;
  133. ResetData *s = container_of(info, ResetData, info[id]);
  134. CPUState *cpu = CPU(s->info[id].cpu);
  135. CPUSPARCState *env = cpu_env(cpu);
  136. cpu_reset(cpu);
  137. cpu->halted = cpu->cpu_index != 0;
  138. env->pc = s->entry;
  139. env->npc = s->entry + 4;
  140. }
  141. static void leon3_cache_control_int(CPUSPARCState *env)
  142. {
  143. uint32_t state = 0;
  144. if (env->cache_control & CACHE_CTRL_IF) {
  145. /* Instruction cache state */
  146. state = env->cache_control & CACHE_STATE_MASK;
  147. if (state == CACHE_ENABLED) {
  148. state = CACHE_FROZEN;
  149. trace_int_helper_icache_freeze();
  150. }
  151. env->cache_control &= ~CACHE_STATE_MASK;
  152. env->cache_control |= state;
  153. }
  154. if (env->cache_control & CACHE_CTRL_DF) {
  155. /* Data cache state */
  156. state = (env->cache_control >> 2) & CACHE_STATE_MASK;
  157. if (state == CACHE_ENABLED) {
  158. state = CACHE_FROZEN;
  159. trace_int_helper_dcache_freeze();
  160. }
  161. env->cache_control &= ~(CACHE_STATE_MASK << 2);
  162. env->cache_control |= (state << 2);
  163. }
  164. }
  165. static void leon3_irq_ack(CPUSPARCState *env, int intno)
  166. {
  167. CPUState *cpu = CPU(env_cpu(env));
  168. grlib_irqmp_ack(env->irq_manager, cpu->cpu_index, intno);
  169. }
  170. /*
  171. * This device assumes that the incoming 'level' value on the
  172. * qemu_irq is the interrupt number, not just a simple 0/1 level.
  173. */
  174. static void leon3_set_pil_in(void *opaque, int n, int level)
  175. {
  176. DeviceState *cpu = opaque;
  177. CPUState *cs = CPU(cpu);
  178. CPUSPARCState *env = cpu_env(cs);
  179. uint32_t pil_in = level;
  180. assert(env != NULL);
  181. env->pil_in = pil_in;
  182. if (env->pil_in && (env->interrupt_index == 0 ||
  183. (env->interrupt_index & ~15) == TT_EXTINT)) {
  184. unsigned int i;
  185. for (i = 15; i > 0; i--) {
  186. if (env->pil_in & (1 << i)) {
  187. int old_interrupt = env->interrupt_index;
  188. env->interrupt_index = TT_EXTINT | i;
  189. if (old_interrupt != env->interrupt_index) {
  190. trace_leon3_set_irq(i);
  191. cpu_interrupt(cs, CPU_INTERRUPT_HARD);
  192. }
  193. break;
  194. }
  195. }
  196. } else if (!env->pil_in && (env->interrupt_index & ~15) == TT_EXTINT) {
  197. trace_leon3_reset_irq(env->interrupt_index & 15);
  198. env->interrupt_index = 0;
  199. cpu_reset_interrupt(cs, CPU_INTERRUPT_HARD);
  200. }
  201. }
  202. static void leon3_start_cpu_async_work(CPUState *cpu, run_on_cpu_data data)
  203. {
  204. cpu->halted = 0;
  205. }
  206. static void leon3_start_cpu(void *opaque, int n, int level)
  207. {
  208. DeviceState *cpu = opaque;
  209. CPUState *cs = CPU(cpu);
  210. assert(level == 1);
  211. async_run_on_cpu(cs, leon3_start_cpu_async_work, RUN_ON_CPU_NULL);
  212. }
  213. static void leon3_irq_manager(CPUSPARCState *env, int intno)
  214. {
  215. leon3_irq_ack(env, intno);
  216. leon3_cache_control_int(env);
  217. }
  218. static void leon3_generic_hw_init(MachineState *machine)
  219. {
  220. ram_addr_t ram_size = machine->ram_size;
  221. const char *bios_name = machine->firmware ?: LEON3_PROM_FILENAME;
  222. const char *kernel_filename = machine->kernel_filename;
  223. SPARCCPU *cpu;
  224. CPUSPARCState *env;
  225. MemoryRegion *address_space_mem = get_system_memory();
  226. MemoryRegion *prom = g_new(MemoryRegion, 1);
  227. int ret;
  228. char *filename;
  229. int bios_size;
  230. int prom_size;
  231. ResetData *reset_info;
  232. DeviceState *dev, *irqmpdev;
  233. int i;
  234. AHBPnp *ahb_pnp;
  235. APBPnp *apb_pnp;
  236. reset_info = g_malloc0(sizeof(ResetData));
  237. for (i = 0; i < machine->smp.cpus; i++) {
  238. /* Init CPU */
  239. cpu = SPARC_CPU(object_new(machine->cpu_type));
  240. qdev_init_gpio_in_named(DEVICE(cpu), leon3_start_cpu, "start_cpu", 1);
  241. qdev_init_gpio_in_named(DEVICE(cpu), leon3_set_pil_in, "pil", 1);
  242. qdev_realize(DEVICE(cpu), NULL, &error_fatal);
  243. env = &cpu->env;
  244. cpu_sparc_set_id(env, i);
  245. /* Reset data */
  246. reset_info->info[i].id = i;
  247. reset_info->info[i].cpu = cpu;
  248. qemu_register_reset(leon3_cpu_reset, &reset_info->info[i]);
  249. }
  250. ahb_pnp = GRLIB_AHB_PNP(qdev_new(TYPE_GRLIB_AHB_PNP));
  251. sysbus_realize_and_unref(SYS_BUS_DEVICE(ahb_pnp), &error_fatal);
  252. sysbus_mmio_map(SYS_BUS_DEVICE(ahb_pnp), 0, LEON3_AHB_PNP_OFFSET);
  253. grlib_ahb_pnp_add_entry(ahb_pnp, 0, 0, GRLIB_VENDOR_GAISLER,
  254. GRLIB_LEON3_DEV, GRLIB_AHB_MASTER,
  255. GRLIB_CPU_AREA);
  256. apb_pnp = GRLIB_APB_PNP(qdev_new(TYPE_GRLIB_APB_PNP));
  257. sysbus_realize_and_unref(SYS_BUS_DEVICE(apb_pnp), &error_fatal);
  258. sysbus_mmio_map(SYS_BUS_DEVICE(apb_pnp), 0, LEON3_APB_PNP_OFFSET);
  259. grlib_ahb_pnp_add_entry(ahb_pnp, LEON3_APB_PNP_OFFSET, 0xFFF,
  260. GRLIB_VENDOR_GAISLER, GRLIB_APBMST_DEV,
  261. GRLIB_AHB_SLAVE, GRLIB_AHBMEM_AREA);
  262. /* Allocate IRQ manager */
  263. irqmpdev = qdev_new(TYPE_GRLIB_IRQMP);
  264. object_property_set_int(OBJECT(irqmpdev), "ncpus", machine->smp.cpus,
  265. &error_fatal);
  266. sysbus_realize_and_unref(SYS_BUS_DEVICE(irqmpdev), &error_fatal);
  267. for (i = 0; i < machine->smp.cpus; i++) {
  268. cpu = reset_info->info[i].cpu;
  269. env = &cpu->env;
  270. qdev_connect_gpio_out_named(irqmpdev, "grlib-start-cpu", i,
  271. qdev_get_gpio_in_named(DEVICE(cpu),
  272. "start_cpu", 0));
  273. qdev_connect_gpio_out_named(irqmpdev, "grlib-irq", i,
  274. qdev_get_gpio_in_named(DEVICE(cpu),
  275. "pil", 0));
  276. env->irq_manager = irqmpdev;
  277. env->qemu_irq_ack = leon3_irq_manager;
  278. }
  279. sysbus_mmio_map(SYS_BUS_DEVICE(irqmpdev), 0, LEON3_IRQMP_OFFSET);
  280. grlib_apb_pnp_add_entry(apb_pnp, LEON3_IRQMP_OFFSET, 0xFFF,
  281. GRLIB_VENDOR_GAISLER, GRLIB_IRQMP_DEV,
  282. 2, 0, GRLIB_APBIO_AREA);
  283. /* Allocate RAM */
  284. if (ram_size > 1 * GiB) {
  285. error_report("Too much memory for this machine: %" PRId64 "MB,"
  286. " maximum 1G",
  287. ram_size / MiB);
  288. exit(1);
  289. }
  290. memory_region_add_subregion(address_space_mem, LEON3_RAM_OFFSET,
  291. machine->ram);
  292. /* Allocate BIOS */
  293. prom_size = 8 * MiB;
  294. memory_region_init_rom(prom, NULL, "Leon3.bios", prom_size, &error_fatal);
  295. memory_region_add_subregion(address_space_mem, LEON3_PROM_OFFSET, prom);
  296. /* Load boot prom */
  297. filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
  298. if (filename) {
  299. bios_size = get_image_size(filename);
  300. } else {
  301. bios_size = -1;
  302. }
  303. if (bios_size > prom_size) {
  304. error_report("could not load prom '%s': file too big", filename);
  305. exit(1);
  306. }
  307. if (bios_size > 0) {
  308. ret = load_image_targphys(filename, LEON3_PROM_OFFSET, bios_size);
  309. if (ret < 0 || ret > prom_size) {
  310. error_report("could not load prom '%s'", filename);
  311. exit(1);
  312. }
  313. } else if (kernel_filename == NULL && !qtest_enabled()) {
  314. error_report("Can't read bios image '%s'", filename
  315. ? filename
  316. : LEON3_PROM_FILENAME);
  317. exit(1);
  318. }
  319. g_free(filename);
  320. /* Can directly load an application. */
  321. if (kernel_filename != NULL) {
  322. long kernel_size;
  323. uint64_t entry;
  324. kernel_size = load_elf(kernel_filename, NULL, NULL, NULL,
  325. &entry, NULL, NULL, NULL,
  326. ELFDATA2MSB, EM_SPARC, 0, 0);
  327. if (kernel_size < 0) {
  328. kernel_size = load_uimage(kernel_filename, NULL, &entry,
  329. NULL, NULL, NULL);
  330. }
  331. if (kernel_size < 0) {
  332. error_report("could not load kernel '%s'", kernel_filename);
  333. exit(1);
  334. }
  335. if (bios_size <= 0) {
  336. /*
  337. * If there is no bios/monitor just start the application but put
  338. * the machine in an initialized state through a little
  339. * bootloader.
  340. */
  341. write_bootloader(memory_region_get_ram_ptr(prom), entry);
  342. reset_info->entry = LEON3_PROM_OFFSET;
  343. for (i = 0; i < machine->smp.cpus; i++) {
  344. reset_info->info[i].cpu->env.pc = LEON3_PROM_OFFSET;
  345. reset_info->info[i].cpu->env.npc = LEON3_PROM_OFFSET + 4;
  346. }
  347. }
  348. }
  349. /* Allocate timers */
  350. dev = qdev_new(TYPE_GRLIB_GPTIMER);
  351. qdev_prop_set_uint32(dev, "nr-timers", LEON3_TIMER_COUNT);
  352. qdev_prop_set_uint32(dev, "frequency", CPU_CLK);
  353. qdev_prop_set_uint32(dev, "irq-line", LEON3_TIMER_IRQ);
  354. sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
  355. sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, LEON3_TIMER_OFFSET);
  356. for (i = 0; i < LEON3_TIMER_COUNT; i++) {
  357. sysbus_connect_irq(SYS_BUS_DEVICE(dev), i,
  358. qdev_get_gpio_in(irqmpdev, LEON3_TIMER_IRQ + i));
  359. }
  360. grlib_apb_pnp_add_entry(apb_pnp, LEON3_TIMER_OFFSET, 0xFFF,
  361. GRLIB_VENDOR_GAISLER, GRLIB_GPTIMER_DEV,
  362. 0, LEON3_TIMER_IRQ, GRLIB_APBIO_AREA);
  363. /* Allocate uart */
  364. dev = qdev_new(TYPE_GRLIB_APB_UART);
  365. qdev_prop_set_chr(dev, "chrdev", serial_hd(0));
  366. sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
  367. sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, LEON3_UART_OFFSET);
  368. sysbus_connect_irq(SYS_BUS_DEVICE(dev), 0,
  369. qdev_get_gpio_in(irqmpdev, LEON3_UART_IRQ));
  370. grlib_apb_pnp_add_entry(apb_pnp, LEON3_UART_OFFSET, 0xFFF,
  371. GRLIB_VENDOR_GAISLER, GRLIB_APBUART_DEV, 1,
  372. LEON3_UART_IRQ, GRLIB_APBIO_AREA);
  373. }
  374. static void leon3_generic_machine_init(MachineClass *mc)
  375. {
  376. mc->desc = "Leon-3 generic";
  377. mc->init = leon3_generic_hw_init;
  378. mc->default_cpu_type = SPARC_CPU_TYPE_NAME("LEON3");
  379. mc->default_ram_id = "leon3.ram";
  380. mc->max_cpus = MAX_CPUS;
  381. }
  382. DEFINE_MACHINE("leon3_generic", leon3_generic_machine_init)