2
0

leon3.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /*
  2. * QEMU Leon3 System Emulator
  3. *
  4. * Copyright (c) 2010-2019 AdaCore
  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-common.h"
  29. #include "cpu.h"
  30. #include "hw/irq.h"
  31. #include "qemu/timer.h"
  32. #include "hw/ptimer.h"
  33. #include "hw/qdev-properties.h"
  34. #include "sysemu/sysemu.h"
  35. #include "sysemu/qtest.h"
  36. #include "sysemu/reset.h"
  37. #include "hw/boards.h"
  38. #include "hw/loader.h"
  39. #include "elf.h"
  40. #include "trace.h"
  41. #include "exec/address-spaces.h"
  42. #include "hw/sparc/grlib.h"
  43. #include "hw/misc/grlib_ahb_apb_pnp.h"
  44. /* Default system clock. */
  45. #define CPU_CLK (40 * 1000 * 1000)
  46. #define LEON3_PROM_FILENAME "u-boot.bin"
  47. #define LEON3_PROM_OFFSET (0x00000000)
  48. #define LEON3_RAM_OFFSET (0x40000000)
  49. #define MAX_PILS 16
  50. #define LEON3_UART_OFFSET (0x80000100)
  51. #define LEON3_UART_IRQ (3)
  52. #define LEON3_IRQMP_OFFSET (0x80000200)
  53. #define LEON3_TIMER_OFFSET (0x80000300)
  54. #define LEON3_TIMER_IRQ (6)
  55. #define LEON3_TIMER_COUNT (2)
  56. #define LEON3_APB_PNP_OFFSET (0x800FF000)
  57. #define LEON3_AHB_PNP_OFFSET (0xFFFFF000)
  58. typedef struct ResetData {
  59. SPARCCPU *cpu;
  60. uint32_t entry; /* save kernel entry in case of reset */
  61. target_ulong sp; /* initial stack pointer */
  62. } ResetData;
  63. static uint32_t *gen_store_u32(uint32_t *code, hwaddr addr, uint32_t val)
  64. {
  65. stl_p(code++, 0x82100000); /* mov %g0, %g1 */
  66. stl_p(code++, 0x84100000); /* mov %g0, %g2 */
  67. stl_p(code++, 0x03000000 +
  68. extract32(addr, 10, 22));
  69. /* sethi %hi(addr), %g1 */
  70. stl_p(code++, 0x82106000 +
  71. extract32(addr, 0, 10));
  72. /* or %g1, addr, %g1 */
  73. stl_p(code++, 0x05000000 +
  74. extract32(val, 10, 22));
  75. /* sethi %hi(val), %g2 */
  76. stl_p(code++, 0x8410a000 +
  77. extract32(val, 0, 10));
  78. /* or %g2, val, %g2 */
  79. stl_p(code++, 0xc4204000); /* st %g2, [ %g1 ] */
  80. return code;
  81. }
  82. /*
  83. * When loading a kernel in RAM the machine is expected to be in a different
  84. * state (eg: initialized by the bootloader). This little code reproduces
  85. * this behavior.
  86. */
  87. static void write_bootloader(CPUSPARCState *env, uint8_t *base,
  88. hwaddr kernel_addr)
  89. {
  90. uint32_t *p = (uint32_t *) base;
  91. /* Initialize the UARTs */
  92. /* *UART_CONTROL = UART_RECEIVE_ENABLE | UART_TRANSMIT_ENABLE; */
  93. p = gen_store_u32(p, 0x80000108, 3);
  94. /* Initialize the TIMER 0 */
  95. /* *GPTIMER_SCALER_RELOAD = 40 - 1; */
  96. p = gen_store_u32(p, 0x80000304, 39);
  97. /* *GPTIMER0_COUNTER_RELOAD = 0xFFFE; */
  98. p = gen_store_u32(p, 0x80000314, 0xFFFFFFFE);
  99. /* *GPTIMER0_CONFIG = GPTIMER_ENABLE | GPTIMER_RESTART; */
  100. p = gen_store_u32(p, 0x80000318, 3);
  101. /* JUMP to the entry point */
  102. stl_p(p++, 0x82100000); /* mov %g0, %g1 */
  103. stl_p(p++, 0x03000000 + extract32(kernel_addr, 10, 22));
  104. /* sethi %hi(kernel_addr), %g1 */
  105. stl_p(p++, 0x82106000 + extract32(kernel_addr, 0, 10));
  106. /* or kernel_addr, %g1 */
  107. stl_p(p++, 0x81c04000); /* jmp %g1 */
  108. stl_p(p++, 0x01000000); /* nop */
  109. }
  110. static void main_cpu_reset(void *opaque)
  111. {
  112. ResetData *s = (ResetData *)opaque;
  113. CPUState *cpu = CPU(s->cpu);
  114. CPUSPARCState *env = &s->cpu->env;
  115. cpu_reset(cpu);
  116. cpu->halted = 0;
  117. env->pc = s->entry;
  118. env->npc = s->entry + 4;
  119. env->regbase[6] = s->sp;
  120. }
  121. void leon3_irq_ack(void *irq_manager, int intno)
  122. {
  123. grlib_irqmp_ack((DeviceState *)irq_manager, intno);
  124. }
  125. static void leon3_set_pil_in(void *opaque, uint32_t pil_in)
  126. {
  127. CPUSPARCState *env = (CPUSPARCState *)opaque;
  128. CPUState *cs;
  129. assert(env != NULL);
  130. env->pil_in = pil_in;
  131. if (env->pil_in && (env->interrupt_index == 0 ||
  132. (env->interrupt_index & ~15) == TT_EXTINT)) {
  133. unsigned int i;
  134. for (i = 15; i > 0; i--) {
  135. if (env->pil_in & (1 << i)) {
  136. int old_interrupt = env->interrupt_index;
  137. env->interrupt_index = TT_EXTINT | i;
  138. if (old_interrupt != env->interrupt_index) {
  139. cs = env_cpu(env);
  140. trace_leon3_set_irq(i);
  141. cpu_interrupt(cs, CPU_INTERRUPT_HARD);
  142. }
  143. break;
  144. }
  145. }
  146. } else if (!env->pil_in && (env->interrupt_index & ~15) == TT_EXTINT) {
  147. cs = env_cpu(env);
  148. trace_leon3_reset_irq(env->interrupt_index & 15);
  149. env->interrupt_index = 0;
  150. cpu_reset_interrupt(cs, CPU_INTERRUPT_HARD);
  151. }
  152. }
  153. static void leon3_generic_hw_init(MachineState *machine)
  154. {
  155. ram_addr_t ram_size = machine->ram_size;
  156. const char *kernel_filename = machine->kernel_filename;
  157. SPARCCPU *cpu;
  158. CPUSPARCState *env;
  159. MemoryRegion *address_space_mem = get_system_memory();
  160. MemoryRegion *ram = g_new(MemoryRegion, 1);
  161. MemoryRegion *prom = g_new(MemoryRegion, 1);
  162. int ret;
  163. char *filename;
  164. qemu_irq *cpu_irqs = NULL;
  165. int bios_size;
  166. int prom_size;
  167. ResetData *reset_info;
  168. DeviceState *dev;
  169. int i;
  170. AHBPnp *ahb_pnp;
  171. APBPnp *apb_pnp;
  172. /* Init CPU */
  173. cpu = SPARC_CPU(cpu_create(machine->cpu_type));
  174. env = &cpu->env;
  175. cpu_sparc_set_id(env, 0);
  176. /* Reset data */
  177. reset_info = g_malloc0(sizeof(ResetData));
  178. reset_info->cpu = cpu;
  179. reset_info->sp = LEON3_RAM_OFFSET + ram_size;
  180. qemu_register_reset(main_cpu_reset, reset_info);
  181. ahb_pnp = GRLIB_AHB_PNP(object_new(TYPE_GRLIB_AHB_PNP));
  182. object_property_set_bool(OBJECT(ahb_pnp), true, "realized", &error_fatal);
  183. sysbus_mmio_map(SYS_BUS_DEVICE(ahb_pnp), 0, LEON3_AHB_PNP_OFFSET);
  184. grlib_ahb_pnp_add_entry(ahb_pnp, 0, 0, GRLIB_VENDOR_GAISLER,
  185. GRLIB_LEON3_DEV, GRLIB_AHB_MASTER,
  186. GRLIB_CPU_AREA);
  187. apb_pnp = GRLIB_APB_PNP(object_new(TYPE_GRLIB_APB_PNP));
  188. object_property_set_bool(OBJECT(apb_pnp), true, "realized", &error_fatal);
  189. sysbus_mmio_map(SYS_BUS_DEVICE(apb_pnp), 0, LEON3_APB_PNP_OFFSET);
  190. grlib_ahb_pnp_add_entry(ahb_pnp, LEON3_APB_PNP_OFFSET, 0xFFF,
  191. GRLIB_VENDOR_GAISLER, GRLIB_APBMST_DEV,
  192. GRLIB_AHB_SLAVE, GRLIB_AHBMEM_AREA);
  193. /* Allocate IRQ manager */
  194. dev = qdev_create(NULL, TYPE_GRLIB_IRQMP);
  195. qdev_prop_set_ptr(dev, "set_pil_in", leon3_set_pil_in);
  196. qdev_prop_set_ptr(dev, "set_pil_in_opaque", env);
  197. qdev_init_nofail(dev);
  198. sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, LEON3_IRQMP_OFFSET);
  199. env->irq_manager = dev;
  200. env->qemu_irq_ack = leon3_irq_manager;
  201. cpu_irqs = qemu_allocate_irqs(grlib_irqmp_set_irq, dev, MAX_PILS);
  202. grlib_apb_pnp_add_entry(apb_pnp, LEON3_IRQMP_OFFSET, 0xFFF,
  203. GRLIB_VENDOR_GAISLER, GRLIB_IRQMP_DEV,
  204. 2, 0, GRLIB_APBIO_AREA);
  205. /* Allocate RAM */
  206. if (ram_size > 1 * GiB) {
  207. error_report("Too much memory for this machine: %" PRId64 "MB,"
  208. " maximum 1G",
  209. ram_size / MiB);
  210. exit(1);
  211. }
  212. memory_region_allocate_system_memory(ram, NULL, "leon3.ram", ram_size);
  213. memory_region_add_subregion(address_space_mem, LEON3_RAM_OFFSET, ram);
  214. /* Allocate BIOS */
  215. prom_size = 8 * MiB;
  216. memory_region_init_ram(prom, NULL, "Leon3.bios", prom_size, &error_fatal);
  217. memory_region_set_readonly(prom, true);
  218. memory_region_add_subregion(address_space_mem, LEON3_PROM_OFFSET, prom);
  219. /* Load boot prom */
  220. if (bios_name == NULL) {
  221. bios_name = LEON3_PROM_FILENAME;
  222. }
  223. filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
  224. if (filename) {
  225. bios_size = get_image_size(filename);
  226. } else {
  227. bios_size = -1;
  228. }
  229. if (bios_size > prom_size) {
  230. error_report("could not load prom '%s': file too big", filename);
  231. exit(1);
  232. }
  233. if (bios_size > 0) {
  234. ret = load_image_targphys(filename, LEON3_PROM_OFFSET, bios_size);
  235. if (ret < 0 || ret > prom_size) {
  236. error_report("could not load prom '%s'", filename);
  237. exit(1);
  238. }
  239. } else if (kernel_filename == NULL && !qtest_enabled()) {
  240. error_report("Can't read bios image '%s'", filename
  241. ? filename
  242. : LEON3_PROM_FILENAME);
  243. exit(1);
  244. }
  245. g_free(filename);
  246. /* Can directly load an application. */
  247. if (kernel_filename != NULL) {
  248. long kernel_size;
  249. uint64_t entry;
  250. kernel_size = load_elf(kernel_filename, NULL, NULL, NULL,
  251. &entry, NULL, NULL,
  252. 1 /* big endian */, EM_SPARC, 0, 0);
  253. if (kernel_size < 0) {
  254. kernel_size = load_uimage(kernel_filename, NULL, &entry,
  255. NULL, NULL, NULL);
  256. }
  257. if (kernel_size < 0) {
  258. error_report("could not load kernel '%s'", kernel_filename);
  259. exit(1);
  260. }
  261. if (bios_size <= 0) {
  262. /*
  263. * If there is no bios/monitor just start the application but put
  264. * the machine in an initialized state through a little
  265. * bootloader.
  266. */
  267. uint8_t *bootloader_entry;
  268. bootloader_entry = memory_region_get_ram_ptr(prom);
  269. write_bootloader(env, bootloader_entry, entry);
  270. env->pc = LEON3_PROM_OFFSET;
  271. env->npc = LEON3_PROM_OFFSET + 4;
  272. reset_info->entry = LEON3_PROM_OFFSET;
  273. }
  274. }
  275. /* Allocate timers */
  276. dev = qdev_create(NULL, TYPE_GRLIB_GPTIMER);
  277. qdev_prop_set_uint32(dev, "nr-timers", LEON3_TIMER_COUNT);
  278. qdev_prop_set_uint32(dev, "frequency", CPU_CLK);
  279. qdev_prop_set_uint32(dev, "irq-line", LEON3_TIMER_IRQ);
  280. qdev_init_nofail(dev);
  281. sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, LEON3_TIMER_OFFSET);
  282. for (i = 0; i < LEON3_TIMER_COUNT; i++) {
  283. sysbus_connect_irq(SYS_BUS_DEVICE(dev), i,
  284. cpu_irqs[LEON3_TIMER_IRQ + i]);
  285. }
  286. grlib_apb_pnp_add_entry(apb_pnp, LEON3_TIMER_OFFSET, 0xFFF,
  287. GRLIB_VENDOR_GAISLER, GRLIB_GPTIMER_DEV,
  288. 0, LEON3_TIMER_IRQ, GRLIB_APBIO_AREA);
  289. /* Allocate uart */
  290. if (serial_hd(0)) {
  291. dev = qdev_create(NULL, TYPE_GRLIB_APB_UART);
  292. qdev_prop_set_chr(dev, "chrdev", serial_hd(0));
  293. qdev_init_nofail(dev);
  294. sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, LEON3_UART_OFFSET);
  295. sysbus_connect_irq(SYS_BUS_DEVICE(dev), 0, cpu_irqs[LEON3_UART_IRQ]);
  296. grlib_apb_pnp_add_entry(apb_pnp, LEON3_UART_OFFSET, 0xFFF,
  297. GRLIB_VENDOR_GAISLER, GRLIB_APBUART_DEV, 1,
  298. LEON3_UART_IRQ, GRLIB_APBIO_AREA);
  299. }
  300. }
  301. static void leon3_generic_machine_init(MachineClass *mc)
  302. {
  303. mc->desc = "Leon-3 generic";
  304. mc->init = leon3_generic_hw_init;
  305. mc->default_cpu_type = SPARC_CPU_TYPE_NAME("LEON3");
  306. }
  307. DEFINE_MACHINE("leon3_generic", leon3_generic_machine_init)