armv7m.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  1. /*
  2. * ARMV7M System emulation.
  3. *
  4. * Copyright (c) 2006-2007 CodeSourcery.
  5. * Written by Paul Brook
  6. *
  7. * This code is licensed under the GPL.
  8. */
  9. #include "qemu/osdep.h"
  10. #include "hw/arm/armv7m.h"
  11. #include "qapi/error.h"
  12. #include "hw/sysbus.h"
  13. #include "hw/arm/boot.h"
  14. #include "hw/loader.h"
  15. #include "hw/qdev-properties.h"
  16. #include "hw/qdev-clock.h"
  17. #include "elf.h"
  18. #include "system/reset.h"
  19. #include "qemu/error-report.h"
  20. #include "qemu/module.h"
  21. #include "qemu/log.h"
  22. #include "target/arm/idau.h"
  23. #include "target/arm/cpu.h"
  24. #include "target/arm/cpu-features.h"
  25. #include "target/arm/cpu-qom.h"
  26. #include "migration/vmstate.h"
  27. /* Bitbanded IO. Each word corresponds to a single bit. */
  28. /* Get the byte address of the real memory for a bitband access. */
  29. static inline hwaddr bitband_addr(BitBandState *s, hwaddr offset)
  30. {
  31. return s->base | (offset & 0x1ffffff) >> 5;
  32. }
  33. static MemTxResult bitband_read(void *opaque, hwaddr offset,
  34. uint64_t *data, unsigned size, MemTxAttrs attrs)
  35. {
  36. BitBandState *s = opaque;
  37. uint8_t buf[4];
  38. MemTxResult res;
  39. int bitpos, bit;
  40. hwaddr addr;
  41. assert(size <= 4);
  42. /* Find address in underlying memory and round down to multiple of size */
  43. addr = bitband_addr(s, offset) & (-size);
  44. res = address_space_read(&s->source_as, addr, attrs, buf, size);
  45. if (res) {
  46. return res;
  47. }
  48. /* Bit position in the N bytes read... */
  49. bitpos = (offset >> 2) & ((size * 8) - 1);
  50. /* ...converted to byte in buffer and bit in byte */
  51. bit = (buf[bitpos >> 3] >> (bitpos & 7)) & 1;
  52. *data = bit;
  53. return MEMTX_OK;
  54. }
  55. static MemTxResult bitband_write(void *opaque, hwaddr offset, uint64_t value,
  56. unsigned size, MemTxAttrs attrs)
  57. {
  58. BitBandState *s = opaque;
  59. uint8_t buf[4];
  60. MemTxResult res;
  61. int bitpos, bit;
  62. hwaddr addr;
  63. assert(size <= 4);
  64. /* Find address in underlying memory and round down to multiple of size */
  65. addr = bitband_addr(s, offset) & (-size);
  66. res = address_space_read(&s->source_as, addr, attrs, buf, size);
  67. if (res) {
  68. return res;
  69. }
  70. /* Bit position in the N bytes read... */
  71. bitpos = (offset >> 2) & ((size * 8) - 1);
  72. /* ...converted to byte in buffer and bit in byte */
  73. bit = 1 << (bitpos & 7);
  74. if (value & 1) {
  75. buf[bitpos >> 3] |= bit;
  76. } else {
  77. buf[bitpos >> 3] &= ~bit;
  78. }
  79. return address_space_write(&s->source_as, addr, attrs, buf, size);
  80. }
  81. static const MemoryRegionOps bitband_ops = {
  82. .read_with_attrs = bitband_read,
  83. .write_with_attrs = bitband_write,
  84. .endianness = DEVICE_NATIVE_ENDIAN,
  85. .impl.min_access_size = 1,
  86. .impl.max_access_size = 4,
  87. .valid.min_access_size = 1,
  88. .valid.max_access_size = 4,
  89. };
  90. static void bitband_init(Object *obj)
  91. {
  92. BitBandState *s = BITBAND(obj);
  93. SysBusDevice *dev = SYS_BUS_DEVICE(obj);
  94. memory_region_init_io(&s->iomem, obj, &bitband_ops, s,
  95. "bitband", 0x02000000);
  96. sysbus_init_mmio(dev, &s->iomem);
  97. }
  98. static void bitband_realize(DeviceState *dev, Error **errp)
  99. {
  100. BitBandState *s = BITBAND(dev);
  101. if (!s->source_memory) {
  102. error_setg(errp, "source-memory property not set");
  103. return;
  104. }
  105. address_space_init(&s->source_as, s->source_memory, "bitband-source");
  106. }
  107. /* Board init. */
  108. static const hwaddr bitband_input_addr[ARMV7M_NUM_BITBANDS] = {
  109. 0x20000000, 0x40000000
  110. };
  111. static const hwaddr bitband_output_addr[ARMV7M_NUM_BITBANDS] = {
  112. 0x22000000, 0x42000000
  113. };
  114. static MemTxResult v7m_sysreg_ns_write(void *opaque, hwaddr addr,
  115. uint64_t value, unsigned size,
  116. MemTxAttrs attrs)
  117. {
  118. MemoryRegion *mr = opaque;
  119. if (attrs.secure) {
  120. /* S accesses to the alias act like NS accesses to the real region */
  121. attrs.secure = 0;
  122. return memory_region_dispatch_write(mr, addr, value,
  123. size_memop(size) | MO_LE, attrs);
  124. } else {
  125. /* NS attrs are RAZ/WI for privileged, and BusFault for user */
  126. if (attrs.user) {
  127. return MEMTX_ERROR;
  128. }
  129. return MEMTX_OK;
  130. }
  131. }
  132. static MemTxResult v7m_sysreg_ns_read(void *opaque, hwaddr addr,
  133. uint64_t *data, unsigned size,
  134. MemTxAttrs attrs)
  135. {
  136. MemoryRegion *mr = opaque;
  137. if (attrs.secure) {
  138. /* S accesses to the alias act like NS accesses to the real region */
  139. attrs.secure = 0;
  140. return memory_region_dispatch_read(mr, addr, data,
  141. size_memop(size) | MO_LE, attrs);
  142. } else {
  143. /* NS attrs are RAZ/WI for privileged, and BusFault for user */
  144. if (attrs.user) {
  145. return MEMTX_ERROR;
  146. }
  147. *data = 0;
  148. return MEMTX_OK;
  149. }
  150. }
  151. static const MemoryRegionOps v7m_sysreg_ns_ops = {
  152. .read_with_attrs = v7m_sysreg_ns_read,
  153. .write_with_attrs = v7m_sysreg_ns_write,
  154. .endianness = DEVICE_LITTLE_ENDIAN,
  155. };
  156. static MemTxResult v7m_systick_write(void *opaque, hwaddr addr,
  157. uint64_t value, unsigned size,
  158. MemTxAttrs attrs)
  159. {
  160. ARMv7MState *s = opaque;
  161. MemoryRegion *mr;
  162. /* Direct the access to the correct systick */
  163. mr = sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->systick[attrs.secure]), 0);
  164. return memory_region_dispatch_write(mr, addr, value,
  165. size_memop(size) | MO_LE, attrs);
  166. }
  167. static MemTxResult v7m_systick_read(void *opaque, hwaddr addr,
  168. uint64_t *data, unsigned size,
  169. MemTxAttrs attrs)
  170. {
  171. ARMv7MState *s = opaque;
  172. MemoryRegion *mr;
  173. /* Direct the access to the correct systick */
  174. mr = sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->systick[attrs.secure]), 0);
  175. return memory_region_dispatch_read(mr, addr, data,
  176. size_memop(size) | MO_LE, attrs);
  177. }
  178. static const MemoryRegionOps v7m_systick_ops = {
  179. .read_with_attrs = v7m_systick_read,
  180. .write_with_attrs = v7m_systick_write,
  181. .endianness = DEVICE_LITTLE_ENDIAN,
  182. };
  183. /*
  184. * Unassigned portions of the PPB space are RAZ/WI for privileged
  185. * accesses, and fault for non-privileged accesses.
  186. */
  187. static MemTxResult ppb_default_read(void *opaque, hwaddr addr,
  188. uint64_t *data, unsigned size,
  189. MemTxAttrs attrs)
  190. {
  191. qemu_log_mask(LOG_UNIMP, "Read of unassigned area of PPB: offset 0x%x\n",
  192. (uint32_t)addr);
  193. if (attrs.user) {
  194. return MEMTX_ERROR;
  195. }
  196. *data = 0;
  197. return MEMTX_OK;
  198. }
  199. static MemTxResult ppb_default_write(void *opaque, hwaddr addr,
  200. uint64_t value, unsigned size,
  201. MemTxAttrs attrs)
  202. {
  203. qemu_log_mask(LOG_UNIMP, "Write of unassigned area of PPB: offset 0x%x\n",
  204. (uint32_t)addr);
  205. if (attrs.user) {
  206. return MEMTX_ERROR;
  207. }
  208. return MEMTX_OK;
  209. }
  210. static const MemoryRegionOps ppb_default_ops = {
  211. .read_with_attrs = ppb_default_read,
  212. .write_with_attrs = ppb_default_write,
  213. .endianness = DEVICE_NATIVE_ENDIAN,
  214. .valid.min_access_size = 1,
  215. .valid.max_access_size = 8,
  216. };
  217. static void armv7m_instance_init(Object *obj)
  218. {
  219. ARMv7MState *s = ARMV7M(obj);
  220. int i;
  221. /* Can't init the cpu here, we don't yet know which model to use */
  222. memory_region_init(&s->container, obj, "armv7m-container", UINT64_MAX);
  223. object_initialize_child(obj, "nvic", &s->nvic, TYPE_NVIC);
  224. object_property_add_alias(obj, "num-irq",
  225. OBJECT(&s->nvic), "num-irq");
  226. object_property_add_alias(obj, "num-prio-bits",
  227. OBJECT(&s->nvic), "num-prio-bits");
  228. object_initialize_child(obj, "systick-reg-ns", &s->systick[M_REG_NS],
  229. TYPE_SYSTICK);
  230. /*
  231. * We can't initialize the secure systick here, as we don't know
  232. * yet if we need it.
  233. */
  234. for (i = 0; i < ARRAY_SIZE(s->bitband); i++) {
  235. object_initialize_child(obj, "bitband[*]", &s->bitband[i],
  236. TYPE_BITBAND);
  237. }
  238. s->refclk = qdev_init_clock_in(DEVICE(obj), "refclk", NULL, NULL, 0);
  239. s->cpuclk = qdev_init_clock_in(DEVICE(obj), "cpuclk", NULL, NULL, 0);
  240. }
  241. static void armv7m_realize(DeviceState *dev, Error **errp)
  242. {
  243. ARMv7MState *s = ARMV7M(dev);
  244. SysBusDevice *sbd;
  245. Error *err = NULL;
  246. int i;
  247. if (!s->board_memory) {
  248. error_setg(errp, "memory property was not set");
  249. return;
  250. }
  251. /* cpuclk must be connected; refclk is optional */
  252. if (!clock_has_source(s->cpuclk)) {
  253. error_setg(errp, "armv7m: cpuclk must be connected");
  254. return;
  255. }
  256. memory_region_add_subregion_overlap(&s->container, 0, s->board_memory, -1);
  257. s->cpu = ARM_CPU(object_new_with_props(s->cpu_type, OBJECT(s), "cpu",
  258. &err, NULL));
  259. if (err != NULL) {
  260. error_propagate(errp, err);
  261. return;
  262. }
  263. object_property_set_link(OBJECT(s->cpu), "memory", OBJECT(&s->container),
  264. &error_abort);
  265. if (object_property_find(OBJECT(s->cpu), "idau")) {
  266. object_property_set_link(OBJECT(s->cpu), "idau", s->idau,
  267. &error_abort);
  268. }
  269. if (object_property_find(OBJECT(s->cpu), "init-svtor")) {
  270. if (!object_property_set_uint(OBJECT(s->cpu), "init-svtor",
  271. s->init_svtor, errp)) {
  272. return;
  273. }
  274. }
  275. if (object_property_find(OBJECT(s->cpu), "init-nsvtor")) {
  276. if (!object_property_set_uint(OBJECT(s->cpu), "init-nsvtor",
  277. s->init_nsvtor, errp)) {
  278. return;
  279. }
  280. }
  281. if (object_property_find(OBJECT(s->cpu), "vfp")) {
  282. if (!object_property_set_bool(OBJECT(s->cpu), "vfp", s->vfp, errp)) {
  283. return;
  284. }
  285. }
  286. if (object_property_find(OBJECT(s->cpu), "dsp")) {
  287. if (!object_property_set_bool(OBJECT(s->cpu), "dsp", s->dsp, errp)) {
  288. return;
  289. }
  290. }
  291. object_property_set_bool(OBJECT(s->cpu), "start-powered-off",
  292. s->start_powered_off, &error_abort);
  293. /*
  294. * Real M-profile hardware can be configured with a different number of
  295. * MPU regions for Secure vs NonSecure. QEMU's CPU implementation doesn't
  296. * support that yet, so catch attempts to select that.
  297. */
  298. if (arm_feature(&s->cpu->env, ARM_FEATURE_M_SECURITY) &&
  299. s->mpu_ns_regions != s->mpu_s_regions) {
  300. error_setg(errp,
  301. "mpu-ns-regions and mpu-s-regions properties must have the same value");
  302. return;
  303. }
  304. if (s->mpu_ns_regions != UINT_MAX &&
  305. object_property_find(OBJECT(s->cpu), "pmsav7-dregion")) {
  306. if (!object_property_set_uint(OBJECT(s->cpu), "pmsav7-dregion",
  307. s->mpu_ns_regions, errp)) {
  308. return;
  309. }
  310. }
  311. /*
  312. * Tell the CPU where the NVIC is; it will fail realize if it doesn't
  313. * have one. Similarly, tell the NVIC where its CPU is.
  314. */
  315. s->cpu->env.nvic = &s->nvic;
  316. s->nvic.cpu = s->cpu;
  317. if (!qdev_realize(DEVICE(s->cpu), NULL, errp)) {
  318. return;
  319. }
  320. /* Note that we must realize the NVIC after the CPU */
  321. if (!sysbus_realize(SYS_BUS_DEVICE(&s->nvic), errp)) {
  322. return;
  323. }
  324. /* Alias the NVIC's input and output GPIOs as our own so the board
  325. * code can wire them up. (We do this in realize because the
  326. * NVIC doesn't create the input GPIO array until realize.)
  327. */
  328. qdev_pass_gpios(DEVICE(&s->nvic), dev, NULL);
  329. qdev_pass_gpios(DEVICE(&s->nvic), dev, "SYSRESETREQ");
  330. qdev_pass_gpios(DEVICE(&s->nvic), dev, "NMI");
  331. /*
  332. * We map various devices into the container MR at their architected
  333. * addresses. In particular, we map everything corresponding to the
  334. * "System PPB" space. This is the range from 0xe0000000 to 0xe00fffff
  335. * and includes the NVIC, the System Control Space (system registers),
  336. * the systick timer, and for CPUs with the Security extension an NS
  337. * banked version of all of these.
  338. *
  339. * The default behaviour for unimplemented registers/ranges
  340. * (for instance the Data Watchpoint and Trace unit at 0xe0001000)
  341. * is to RAZ/WI for privileged access and BusFault for non-privileged
  342. * access.
  343. *
  344. * The NVIC and System Control Space (SCS) starts at 0xe000e000
  345. * and looks like this:
  346. * 0x004 - ICTR
  347. * 0x010 - 0xff - systick
  348. * 0x100..0x7ec - NVIC
  349. * 0x7f0..0xcff - Reserved
  350. * 0xd00..0xd3c - SCS registers
  351. * 0xd40..0xeff - Reserved or Not implemented
  352. * 0xf00 - STIR
  353. *
  354. * Some registers within this space are banked between security states.
  355. * In v8M there is a second range 0xe002e000..0xe002efff which is the
  356. * NonSecure alias SCS; secure accesses to this behave like NS accesses
  357. * to the main SCS range, and non-secure accesses (including when
  358. * the security extension is not implemented) are RAZ/WI.
  359. * Note that both the main SCS range and the alias range are defined
  360. * to be exempt from memory attribution (R_BLJT) and so the memory
  361. * transaction attribute always matches the current CPU security
  362. * state (attrs.secure == env->v7m.secure). In the v7m_sysreg_ns_ops
  363. * wrappers we change attrs.secure to indicate the NS access; so
  364. * generally code determining which banked register to use should
  365. * use attrs.secure; code determining actual behaviour of the system
  366. * should use env->v7m.secure.
  367. *
  368. * Within the PPB space, some MRs overlap, and the priority
  369. * of overlapping regions is:
  370. * - default region (for RAZ/WI and BusFault) : -1
  371. * - system register regions (provided by the NVIC) : 0
  372. * - systick : 1
  373. * This is because the systick device is a small block of registers
  374. * in the middle of the other system control registers.
  375. */
  376. memory_region_init_io(&s->defaultmem, OBJECT(s), &ppb_default_ops, s,
  377. "nvic-default", 0x100000);
  378. memory_region_add_subregion_overlap(&s->container, 0xe0000000,
  379. &s->defaultmem, -1);
  380. /* Wire the NVIC up to the CPU */
  381. sbd = SYS_BUS_DEVICE(&s->nvic);
  382. sysbus_connect_irq(sbd, 0,
  383. qdev_get_gpio_in(DEVICE(s->cpu), ARM_CPU_IRQ));
  384. memory_region_add_subregion(&s->container, 0xe000e000,
  385. sysbus_mmio_get_region(sbd, 0));
  386. if (arm_feature(&s->cpu->env, ARM_FEATURE_V8)) {
  387. /* Create the NS alias region for the NVIC sysregs */
  388. memory_region_init_io(&s->sysreg_ns_mem, OBJECT(s),
  389. &v7m_sysreg_ns_ops,
  390. sysbus_mmio_get_region(sbd, 0),
  391. "nvic_sysregs_ns", 0x1000);
  392. memory_region_add_subregion(&s->container, 0xe002e000,
  393. &s->sysreg_ns_mem);
  394. }
  395. /*
  396. * Create and map the systick devices. Note that we only connect
  397. * refclk if it has been connected to us; otherwise the systick
  398. * device gets the wrong answer for clock_has_source(refclk), because
  399. * it has an immediate source (the ARMv7M's clock object) but not
  400. * an ultimate source, and then it won't correctly auto-select the
  401. * CPU clock as its only possible clock source.
  402. */
  403. if (clock_has_source(s->refclk)) {
  404. qdev_connect_clock_in(DEVICE(&s->systick[M_REG_NS]), "refclk",
  405. s->refclk);
  406. }
  407. qdev_connect_clock_in(DEVICE(&s->systick[M_REG_NS]), "cpuclk", s->cpuclk);
  408. if (!sysbus_realize(SYS_BUS_DEVICE(&s->systick[M_REG_NS]), errp)) {
  409. return;
  410. }
  411. sysbus_connect_irq(SYS_BUS_DEVICE(&s->systick[M_REG_NS]), 0,
  412. qdev_get_gpio_in_named(DEVICE(&s->nvic),
  413. "systick-trigger", M_REG_NS));
  414. if (arm_feature(&s->cpu->env, ARM_FEATURE_M_SECURITY)) {
  415. /*
  416. * We couldn't init the secure systick device in instance_init
  417. * as we didn't know then if the CPU had the security extensions;
  418. * so we have to do it here.
  419. */
  420. object_initialize_child(OBJECT(dev), "systick-reg-s",
  421. &s->systick[M_REG_S], TYPE_SYSTICK);
  422. if (clock_has_source(s->refclk)) {
  423. qdev_connect_clock_in(DEVICE(&s->systick[M_REG_S]), "refclk",
  424. s->refclk);
  425. }
  426. qdev_connect_clock_in(DEVICE(&s->systick[M_REG_S]), "cpuclk",
  427. s->cpuclk);
  428. if (!sysbus_realize(SYS_BUS_DEVICE(&s->systick[M_REG_S]), errp)) {
  429. return;
  430. }
  431. sysbus_connect_irq(SYS_BUS_DEVICE(&s->systick[M_REG_S]), 0,
  432. qdev_get_gpio_in_named(DEVICE(&s->nvic),
  433. "systick-trigger", M_REG_S));
  434. }
  435. memory_region_init_io(&s->systickmem, OBJECT(s),
  436. &v7m_systick_ops, s,
  437. "v7m_systick", 0xe0);
  438. memory_region_add_subregion_overlap(&s->container, 0xe000e010,
  439. &s->systickmem, 1);
  440. if (arm_feature(&s->cpu->env, ARM_FEATURE_V8)) {
  441. memory_region_init_io(&s->systick_ns_mem, OBJECT(s),
  442. &v7m_sysreg_ns_ops, &s->systickmem,
  443. "v7m_systick_ns", 0xe0);
  444. memory_region_add_subregion_overlap(&s->container, 0xe002e010,
  445. &s->systick_ns_mem, 1);
  446. }
  447. /* If the CPU has RAS support, create the RAS register block */
  448. if (cpu_isar_feature(aa32_ras, s->cpu)) {
  449. object_initialize_child(OBJECT(dev), "armv7m-ras",
  450. &s->ras, TYPE_ARMV7M_RAS);
  451. sbd = SYS_BUS_DEVICE(&s->ras);
  452. if (!sysbus_realize(sbd, errp)) {
  453. return;
  454. }
  455. memory_region_add_subregion_overlap(&s->container, 0xe0005000,
  456. sysbus_mmio_get_region(sbd, 0), 1);
  457. }
  458. for (i = 0; i < ARRAY_SIZE(s->bitband); i++) {
  459. if (s->enable_bitband) {
  460. Object *obj = OBJECT(&s->bitband[i]);
  461. sbd = SYS_BUS_DEVICE(&s->bitband[i]);
  462. if (!object_property_set_int(obj, "base",
  463. bitband_input_addr[i], errp)) {
  464. return;
  465. }
  466. object_property_set_link(obj, "source-memory",
  467. OBJECT(s->board_memory), &error_abort);
  468. if (!sysbus_realize(SYS_BUS_DEVICE(obj), errp)) {
  469. return;
  470. }
  471. memory_region_add_subregion(&s->container, bitband_output_addr[i],
  472. sysbus_mmio_get_region(sbd, 0));
  473. } else {
  474. object_unparent(OBJECT(&s->bitband[i]));
  475. }
  476. }
  477. }
  478. static const Property armv7m_properties[] = {
  479. DEFINE_PROP_STRING("cpu-type", ARMv7MState, cpu_type),
  480. DEFINE_PROP_LINK("memory", ARMv7MState, board_memory, TYPE_MEMORY_REGION,
  481. MemoryRegion *),
  482. DEFINE_PROP_LINK("idau", ARMv7MState, idau, TYPE_IDAU_INTERFACE, Object *),
  483. DEFINE_PROP_UINT32("init-svtor", ARMv7MState, init_svtor, 0),
  484. DEFINE_PROP_UINT32("init-nsvtor", ARMv7MState, init_nsvtor, 0),
  485. DEFINE_PROP_BOOL("enable-bitband", ARMv7MState, enable_bitband, false),
  486. DEFINE_PROP_BOOL("start-powered-off", ARMv7MState, start_powered_off,
  487. false),
  488. DEFINE_PROP_BOOL("vfp", ARMv7MState, vfp, true),
  489. DEFINE_PROP_BOOL("dsp", ARMv7MState, dsp, true),
  490. DEFINE_PROP_UINT32("mpu-ns-regions", ARMv7MState, mpu_ns_regions, UINT_MAX),
  491. DEFINE_PROP_UINT32("mpu-s-regions", ARMv7MState, mpu_s_regions, UINT_MAX),
  492. };
  493. static const VMStateDescription vmstate_armv7m = {
  494. .name = "armv7m",
  495. .version_id = 1,
  496. .minimum_version_id = 1,
  497. .fields = (const VMStateField[]) {
  498. VMSTATE_CLOCK(refclk, ARMv7MState),
  499. VMSTATE_CLOCK(cpuclk, ARMv7MState),
  500. VMSTATE_END_OF_LIST()
  501. }
  502. };
  503. static void armv7m_class_init(ObjectClass *klass, void *data)
  504. {
  505. DeviceClass *dc = DEVICE_CLASS(klass);
  506. dc->realize = armv7m_realize;
  507. dc->vmsd = &vmstate_armv7m;
  508. device_class_set_props(dc, armv7m_properties);
  509. }
  510. static const TypeInfo armv7m_info = {
  511. .name = TYPE_ARMV7M,
  512. .parent = TYPE_SYS_BUS_DEVICE,
  513. .instance_size = sizeof(ARMv7MState),
  514. .instance_init = armv7m_instance_init,
  515. .class_init = armv7m_class_init,
  516. };
  517. static void armv7m_reset(void *opaque)
  518. {
  519. ARMCPU *cpu = opaque;
  520. cpu_reset(CPU(cpu));
  521. }
  522. void armv7m_load_kernel(ARMCPU *cpu, const char *kernel_filename,
  523. hwaddr mem_base, int mem_size)
  524. {
  525. ssize_t image_size;
  526. uint64_t entry;
  527. AddressSpace *as;
  528. int asidx;
  529. CPUState *cs = CPU(cpu);
  530. if (arm_feature(&cpu->env, ARM_FEATURE_EL3)) {
  531. asidx = ARMASIdx_S;
  532. } else {
  533. asidx = ARMASIdx_NS;
  534. }
  535. as = cpu_get_address_space(cs, asidx);
  536. if (kernel_filename) {
  537. image_size = load_elf_as(kernel_filename, NULL, NULL, NULL,
  538. &entry, NULL, NULL,
  539. NULL, ELFDATA2LSB, EM_ARM, 1, 0, as);
  540. if (image_size < 0) {
  541. image_size = load_image_targphys_as(kernel_filename, mem_base,
  542. mem_size, as);
  543. }
  544. if (image_size < 0) {
  545. error_report("Could not load kernel '%s'", kernel_filename);
  546. exit(1);
  547. }
  548. }
  549. /* CPU objects (unlike devices) are not automatically reset on system
  550. * reset, so we must always register a handler to do so. Unlike
  551. * A-profile CPUs, we don't need to do anything special in the
  552. * handler to arrange that it starts correctly.
  553. * This is arguably the wrong place to do this, but it matches the
  554. * way A-profile does it. Note that this means that every M profile
  555. * board must call this function!
  556. */
  557. qemu_register_reset(armv7m_reset, cpu);
  558. }
  559. static const Property bitband_properties[] = {
  560. DEFINE_PROP_UINT32("base", BitBandState, base, 0),
  561. DEFINE_PROP_LINK("source-memory", BitBandState, source_memory,
  562. TYPE_MEMORY_REGION, MemoryRegion *),
  563. };
  564. static void bitband_class_init(ObjectClass *klass, void *data)
  565. {
  566. DeviceClass *dc = DEVICE_CLASS(klass);
  567. dc->realize = bitband_realize;
  568. device_class_set_props(dc, bitband_properties);
  569. }
  570. static const TypeInfo bitband_info = {
  571. .name = TYPE_BITBAND,
  572. .parent = TYPE_SYS_BUS_DEVICE,
  573. .instance_size = sizeof(BitBandState),
  574. .instance_init = bitband_init,
  575. .class_init = bitband_class_init,
  576. };
  577. static void armv7m_register_types(void)
  578. {
  579. type_register_static(&bitband_info);
  580. type_register_static(&armv7m_info);
  581. }
  582. type_init(armv7m_register_types)