riscv_aplic.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084
  1. /*
  2. * RISC-V APLIC (Advanced Platform Level Interrupt Controller)
  3. *
  4. * Copyright (c) 2021 Western Digital Corporation or its affiliates.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2 or later, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with
  16. * this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "qemu/osdep.h"
  19. #include "qapi/error.h"
  20. #include "qemu/log.h"
  21. #include "qemu/module.h"
  22. #include "qemu/error-report.h"
  23. #include "qemu/bswap.h"
  24. #include "exec/address-spaces.h"
  25. #include "hw/sysbus.h"
  26. #include "hw/pci/msi.h"
  27. #include "hw/boards.h"
  28. #include "hw/qdev-properties.h"
  29. #include "hw/intc/riscv_aplic.h"
  30. #include "hw/irq.h"
  31. #include "target/riscv/cpu.h"
  32. #include "system/system.h"
  33. #include "system/kvm.h"
  34. #include "system/tcg.h"
  35. #include "kvm/kvm_riscv.h"
  36. #include "migration/vmstate.h"
  37. #define APLIC_MAX_IDC (1UL << 14)
  38. #define APLIC_MAX_SOURCE 1024
  39. #define APLIC_MIN_IPRIO_BITS 1
  40. #define APLIC_MAX_IPRIO_BITS 8
  41. #define APLIC_MAX_CHILDREN 1024
  42. #define APLIC_DOMAINCFG 0x0000
  43. #define APLIC_DOMAINCFG_RDONLY 0x80000000
  44. #define APLIC_DOMAINCFG_IE (1 << 8)
  45. #define APLIC_DOMAINCFG_DM (1 << 2)
  46. #define APLIC_DOMAINCFG_BE (1 << 0)
  47. #define APLIC_SOURCECFG_BASE 0x0004
  48. #define APLIC_SOURCECFG_D (1 << 10)
  49. #define APLIC_SOURCECFG_CHILDIDX_MASK 0x000003ff
  50. #define APLIC_SOURCECFG_SM_MASK 0x00000007
  51. #define APLIC_SOURCECFG_SM_INACTIVE 0x0
  52. #define APLIC_SOURCECFG_SM_DETACH 0x1
  53. #define APLIC_SOURCECFG_SM_EDGE_RISE 0x4
  54. #define APLIC_SOURCECFG_SM_EDGE_FALL 0x5
  55. #define APLIC_SOURCECFG_SM_LEVEL_HIGH 0x6
  56. #define APLIC_SOURCECFG_SM_LEVEL_LOW 0x7
  57. #define APLIC_MMSICFGADDR 0x1bc0
  58. #define APLIC_MMSICFGADDRH 0x1bc4
  59. #define APLIC_SMSICFGADDR 0x1bc8
  60. #define APLIC_SMSICFGADDRH 0x1bcc
  61. #define APLIC_xMSICFGADDRH_L (1UL << 31)
  62. #define APLIC_xMSICFGADDRH_HHXS_MASK 0x1f
  63. #define APLIC_xMSICFGADDRH_HHXS_SHIFT 24
  64. #define APLIC_xMSICFGADDRH_LHXS_MASK 0x7
  65. #define APLIC_xMSICFGADDRH_LHXS_SHIFT 20
  66. #define APLIC_xMSICFGADDRH_HHXW_MASK 0x7
  67. #define APLIC_xMSICFGADDRH_HHXW_SHIFT 16
  68. #define APLIC_xMSICFGADDRH_LHXW_MASK 0xf
  69. #define APLIC_xMSICFGADDRH_LHXW_SHIFT 12
  70. #define APLIC_xMSICFGADDRH_BAPPN_MASK 0xfff
  71. #define APLIC_xMSICFGADDR_PPN_SHIFT 12
  72. #define APLIC_xMSICFGADDR_PPN_HART(__lhxs) \
  73. ((1UL << (__lhxs)) - 1)
  74. #define APLIC_xMSICFGADDR_PPN_LHX_MASK(__lhxw) \
  75. ((1UL << (__lhxw)) - 1)
  76. #define APLIC_xMSICFGADDR_PPN_LHX_SHIFT(__lhxs) \
  77. ((__lhxs))
  78. #define APLIC_xMSICFGADDR_PPN_LHX(__lhxw, __lhxs) \
  79. (APLIC_xMSICFGADDR_PPN_LHX_MASK(__lhxw) << \
  80. APLIC_xMSICFGADDR_PPN_LHX_SHIFT(__lhxs))
  81. #define APLIC_xMSICFGADDR_PPN_HHX_MASK(__hhxw) \
  82. ((1UL << (__hhxw)) - 1)
  83. #define APLIC_xMSICFGADDR_PPN_HHX_SHIFT(__hhxs) \
  84. ((__hhxs) + APLIC_xMSICFGADDR_PPN_SHIFT)
  85. #define APLIC_xMSICFGADDR_PPN_HHX(__hhxw, __hhxs) \
  86. (APLIC_xMSICFGADDR_PPN_HHX_MASK(__hhxw) << \
  87. APLIC_xMSICFGADDR_PPN_HHX_SHIFT(__hhxs))
  88. #define APLIC_xMSICFGADDRH_VALID_MASK \
  89. (APLIC_xMSICFGADDRH_L | \
  90. (APLIC_xMSICFGADDRH_HHXS_MASK << APLIC_xMSICFGADDRH_HHXS_SHIFT) | \
  91. (APLIC_xMSICFGADDRH_LHXS_MASK << APLIC_xMSICFGADDRH_LHXS_SHIFT) | \
  92. (APLIC_xMSICFGADDRH_HHXW_MASK << APLIC_xMSICFGADDRH_HHXW_SHIFT) | \
  93. (APLIC_xMSICFGADDRH_LHXW_MASK << APLIC_xMSICFGADDRH_LHXW_SHIFT) | \
  94. APLIC_xMSICFGADDRH_BAPPN_MASK)
  95. #define APLIC_SETIP_BASE 0x1c00
  96. #define APLIC_SETIPNUM 0x1cdc
  97. #define APLIC_CLRIP_BASE 0x1d00
  98. #define APLIC_CLRIPNUM 0x1ddc
  99. #define APLIC_SETIE_BASE 0x1e00
  100. #define APLIC_SETIENUM 0x1edc
  101. #define APLIC_CLRIE_BASE 0x1f00
  102. #define APLIC_CLRIENUM 0x1fdc
  103. #define APLIC_SETIPNUM_LE 0x2000
  104. #define APLIC_SETIPNUM_BE 0x2004
  105. #define APLIC_ISTATE_PENDING (1U << 0)
  106. #define APLIC_ISTATE_ENABLED (1U << 1)
  107. #define APLIC_ISTATE_ENPEND (APLIC_ISTATE_ENABLED | \
  108. APLIC_ISTATE_PENDING)
  109. #define APLIC_ISTATE_INPUT (1U << 8)
  110. #define APLIC_GENMSI 0x3000
  111. #define APLIC_TARGET_BASE 0x3004
  112. #define APLIC_TARGET_HART_IDX_SHIFT 18
  113. #define APLIC_TARGET_HART_IDX_MASK 0x3fff
  114. #define APLIC_TARGET_GUEST_IDX_SHIFT 12
  115. #define APLIC_TARGET_GUEST_IDX_MASK 0x3f
  116. #define APLIC_TARGET_IPRIO_MASK 0xff
  117. #define APLIC_TARGET_EIID_MASK 0x7ff
  118. #define APLIC_IDC_BASE 0x4000
  119. #define APLIC_IDC_SIZE 32
  120. #define APLIC_IDC_IDELIVERY 0x00
  121. #define APLIC_IDC_IFORCE 0x04
  122. #define APLIC_IDC_ITHRESHOLD 0x08
  123. #define APLIC_IDC_TOPI 0x18
  124. #define APLIC_IDC_TOPI_ID_SHIFT 16
  125. #define APLIC_IDC_TOPI_ID_MASK 0x3ff
  126. #define APLIC_IDC_TOPI_PRIO_MASK 0xff
  127. #define APLIC_IDC_CLAIMI 0x1c
  128. /*
  129. * KVM AIA only supports APLIC MSI, fallback to QEMU emulation if we want to use
  130. * APLIC Wired.
  131. */
  132. bool riscv_is_kvm_aia_aplic_imsic(bool msimode)
  133. {
  134. return kvm_irqchip_in_kernel() && msimode;
  135. }
  136. bool riscv_use_emulated_aplic(bool msimode)
  137. {
  138. #ifdef CONFIG_KVM
  139. if (tcg_enabled()) {
  140. return true;
  141. }
  142. if (!riscv_is_kvm_aia_aplic_imsic(msimode)) {
  143. return true;
  144. }
  145. return kvm_kernel_irqchip_split();
  146. #else
  147. return true;
  148. #endif
  149. }
  150. void riscv_aplic_set_kvm_msicfgaddr(RISCVAPLICState *aplic, hwaddr addr)
  151. {
  152. #ifdef CONFIG_KVM
  153. if (riscv_use_emulated_aplic(aplic->msimode)) {
  154. aplic->kvm_msicfgaddr = extract64(addr, 0, 32);
  155. aplic->kvm_msicfgaddrH = extract64(addr, 32, 32);
  156. }
  157. #endif
  158. }
  159. static bool riscv_aplic_irq_rectified_val(RISCVAPLICState *aplic,
  160. uint32_t irq)
  161. {
  162. uint32_t sourcecfg, sm, raw_input, irq_inverted;
  163. if (!irq || aplic->num_irqs <= irq) {
  164. return false;
  165. }
  166. sourcecfg = aplic->sourcecfg[irq];
  167. if (sourcecfg & APLIC_SOURCECFG_D) {
  168. return false;
  169. }
  170. sm = sourcecfg & APLIC_SOURCECFG_SM_MASK;
  171. if (sm == APLIC_SOURCECFG_SM_INACTIVE) {
  172. return false;
  173. }
  174. raw_input = (aplic->state[irq] & APLIC_ISTATE_INPUT) ? 1 : 0;
  175. irq_inverted = (sm == APLIC_SOURCECFG_SM_LEVEL_LOW ||
  176. sm == APLIC_SOURCECFG_SM_EDGE_FALL) ? 1 : 0;
  177. return !!(raw_input ^ irq_inverted);
  178. }
  179. static uint32_t riscv_aplic_read_input_word(RISCVAPLICState *aplic,
  180. uint32_t word)
  181. {
  182. uint32_t i, irq, rectified_val, ret = 0;
  183. for (i = 0; i < 32; i++) {
  184. irq = word * 32 + i;
  185. rectified_val = riscv_aplic_irq_rectified_val(aplic, irq);
  186. ret |= rectified_val << i;
  187. }
  188. return ret;
  189. }
  190. static uint32_t riscv_aplic_read_pending_word(RISCVAPLICState *aplic,
  191. uint32_t word)
  192. {
  193. uint32_t i, irq, ret = 0;
  194. for (i = 0; i < 32; i++) {
  195. irq = word * 32 + i;
  196. if (!irq || aplic->num_irqs <= irq) {
  197. continue;
  198. }
  199. ret |= ((aplic->state[irq] & APLIC_ISTATE_PENDING) ? 1 : 0) << i;
  200. }
  201. return ret;
  202. }
  203. static void riscv_aplic_set_pending_raw(RISCVAPLICState *aplic,
  204. uint32_t irq, bool pending)
  205. {
  206. if (pending) {
  207. aplic->state[irq] |= APLIC_ISTATE_PENDING;
  208. } else {
  209. aplic->state[irq] &= ~APLIC_ISTATE_PENDING;
  210. }
  211. }
  212. static void riscv_aplic_set_pending(RISCVAPLICState *aplic,
  213. uint32_t irq, bool pending)
  214. {
  215. uint32_t sourcecfg, sm;
  216. if ((irq <= 0) || (aplic->num_irqs <= irq)) {
  217. return;
  218. }
  219. sourcecfg = aplic->sourcecfg[irq];
  220. if (sourcecfg & APLIC_SOURCECFG_D) {
  221. return;
  222. }
  223. sm = sourcecfg & APLIC_SOURCECFG_SM_MASK;
  224. if (sm == APLIC_SOURCECFG_SM_INACTIVE) {
  225. return;
  226. }
  227. if ((sm == APLIC_SOURCECFG_SM_LEVEL_HIGH) ||
  228. (sm == APLIC_SOURCECFG_SM_LEVEL_LOW)) {
  229. if (!aplic->msimode) {
  230. return;
  231. }
  232. if (aplic->msimode && !pending) {
  233. goto noskip_write_pending;
  234. }
  235. if ((aplic->state[irq] & APLIC_ISTATE_INPUT) &&
  236. (sm == APLIC_SOURCECFG_SM_LEVEL_LOW)) {
  237. return;
  238. }
  239. if (!(aplic->state[irq] & APLIC_ISTATE_INPUT) &&
  240. (sm == APLIC_SOURCECFG_SM_LEVEL_HIGH)) {
  241. return;
  242. }
  243. }
  244. noskip_write_pending:
  245. riscv_aplic_set_pending_raw(aplic, irq, pending);
  246. }
  247. static void riscv_aplic_set_pending_word(RISCVAPLICState *aplic,
  248. uint32_t word, uint32_t value,
  249. bool pending)
  250. {
  251. uint32_t i, irq;
  252. for (i = 0; i < 32; i++) {
  253. irq = word * 32 + i;
  254. if (!irq || aplic->num_irqs <= irq) {
  255. continue;
  256. }
  257. if (value & (1U << i)) {
  258. riscv_aplic_set_pending(aplic, irq, pending);
  259. }
  260. }
  261. }
  262. static uint32_t riscv_aplic_read_enabled_word(RISCVAPLICState *aplic,
  263. int word)
  264. {
  265. uint32_t i, irq, ret = 0;
  266. for (i = 0; i < 32; i++) {
  267. irq = word * 32 + i;
  268. if (!irq || aplic->num_irqs <= irq) {
  269. continue;
  270. }
  271. ret |= ((aplic->state[irq] & APLIC_ISTATE_ENABLED) ? 1 : 0) << i;
  272. }
  273. return ret;
  274. }
  275. static void riscv_aplic_set_enabled_raw(RISCVAPLICState *aplic,
  276. uint32_t irq, bool enabled)
  277. {
  278. if (enabled) {
  279. aplic->state[irq] |= APLIC_ISTATE_ENABLED;
  280. } else {
  281. aplic->state[irq] &= ~APLIC_ISTATE_ENABLED;
  282. }
  283. }
  284. static void riscv_aplic_set_enabled(RISCVAPLICState *aplic,
  285. uint32_t irq, bool enabled)
  286. {
  287. uint32_t sourcecfg, sm;
  288. if ((irq <= 0) || (aplic->num_irqs <= irq)) {
  289. return;
  290. }
  291. sourcecfg = aplic->sourcecfg[irq];
  292. if (sourcecfg & APLIC_SOURCECFG_D) {
  293. return;
  294. }
  295. sm = sourcecfg & APLIC_SOURCECFG_SM_MASK;
  296. if (sm == APLIC_SOURCECFG_SM_INACTIVE) {
  297. return;
  298. }
  299. riscv_aplic_set_enabled_raw(aplic, irq, enabled);
  300. }
  301. static void riscv_aplic_set_enabled_word(RISCVAPLICState *aplic,
  302. uint32_t word, uint32_t value,
  303. bool enabled)
  304. {
  305. uint32_t i, irq;
  306. for (i = 0; i < 32; i++) {
  307. irq = word * 32 + i;
  308. if (!irq || aplic->num_irqs <= irq) {
  309. continue;
  310. }
  311. if (value & (1U << i)) {
  312. riscv_aplic_set_enabled(aplic, irq, enabled);
  313. }
  314. }
  315. }
  316. static void riscv_aplic_msi_send(RISCVAPLICState *aplic,
  317. uint32_t hart_idx, uint32_t guest_idx,
  318. uint32_t eiid)
  319. {
  320. uint64_t addr;
  321. MemTxResult result;
  322. RISCVAPLICState *aplic_m;
  323. uint32_t lhxs, lhxw, hhxs, hhxw, group_idx, msicfgaddr, msicfgaddrH;
  324. aplic_m = aplic;
  325. if (!aplic->kvm_splitmode) {
  326. while (aplic_m && !aplic_m->mmode) {
  327. aplic_m = aplic_m->parent;
  328. }
  329. if (!aplic_m) {
  330. qemu_log_mask(LOG_GUEST_ERROR, "%s: m-level APLIC not found\n",
  331. __func__);
  332. return;
  333. }
  334. }
  335. if (aplic->mmode) {
  336. msicfgaddr = aplic_m->mmsicfgaddr;
  337. msicfgaddrH = aplic_m->mmsicfgaddrH;
  338. } else {
  339. msicfgaddr = aplic_m->smsicfgaddr;
  340. msicfgaddrH = aplic_m->smsicfgaddrH;
  341. }
  342. lhxs = (msicfgaddrH >> APLIC_xMSICFGADDRH_LHXS_SHIFT) &
  343. APLIC_xMSICFGADDRH_LHXS_MASK;
  344. lhxw = (msicfgaddrH >> APLIC_xMSICFGADDRH_LHXW_SHIFT) &
  345. APLIC_xMSICFGADDRH_LHXW_MASK;
  346. hhxs = (msicfgaddrH >> APLIC_xMSICFGADDRH_HHXS_SHIFT) &
  347. APLIC_xMSICFGADDRH_HHXS_MASK;
  348. hhxw = (msicfgaddrH >> APLIC_xMSICFGADDRH_HHXW_SHIFT) &
  349. APLIC_xMSICFGADDRH_HHXW_MASK;
  350. group_idx = hart_idx >> lhxw;
  351. hart_idx &= APLIC_xMSICFGADDR_PPN_LHX_MASK(lhxw);
  352. addr = msicfgaddr;
  353. addr |= ((uint64_t)(msicfgaddrH & APLIC_xMSICFGADDRH_BAPPN_MASK)) << 32;
  354. addr |= ((uint64_t)(group_idx & APLIC_xMSICFGADDR_PPN_HHX_MASK(hhxw))) <<
  355. APLIC_xMSICFGADDR_PPN_HHX_SHIFT(hhxs);
  356. addr |= ((uint64_t)(hart_idx & APLIC_xMSICFGADDR_PPN_LHX_MASK(lhxw))) <<
  357. APLIC_xMSICFGADDR_PPN_LHX_SHIFT(lhxs);
  358. addr |= (uint64_t)(guest_idx & APLIC_xMSICFGADDR_PPN_HART(lhxs));
  359. addr <<= APLIC_xMSICFGADDR_PPN_SHIFT;
  360. if (aplic->kvm_splitmode) {
  361. addr |= aplic->kvm_msicfgaddr;
  362. addr |= ((uint64_t)aplic->kvm_msicfgaddrH << 32);
  363. }
  364. address_space_stl_le(&address_space_memory, addr,
  365. eiid, MEMTXATTRS_UNSPECIFIED, &result);
  366. if (result != MEMTX_OK) {
  367. qemu_log_mask(LOG_GUEST_ERROR, "%s: MSI write failed for "
  368. "hart_index=%d guest_index=%d eiid=%d\n",
  369. __func__, hart_idx, guest_idx, eiid);
  370. }
  371. }
  372. static void riscv_aplic_msi_irq_update(RISCVAPLICState *aplic, uint32_t irq)
  373. {
  374. uint32_t hart_idx, guest_idx, eiid;
  375. if (!aplic->msimode || (aplic->num_irqs <= irq) ||
  376. !(aplic->domaincfg & APLIC_DOMAINCFG_IE)) {
  377. return;
  378. }
  379. if ((aplic->state[irq] & APLIC_ISTATE_ENPEND) != APLIC_ISTATE_ENPEND) {
  380. return;
  381. }
  382. riscv_aplic_set_pending_raw(aplic, irq, false);
  383. hart_idx = aplic->target[irq] >> APLIC_TARGET_HART_IDX_SHIFT;
  384. hart_idx &= APLIC_TARGET_HART_IDX_MASK;
  385. if (aplic->mmode) {
  386. /* M-level APLIC ignores guest_index */
  387. guest_idx = 0;
  388. } else {
  389. guest_idx = aplic->target[irq] >> APLIC_TARGET_GUEST_IDX_SHIFT;
  390. guest_idx &= APLIC_TARGET_GUEST_IDX_MASK;
  391. }
  392. eiid = aplic->target[irq] & APLIC_TARGET_EIID_MASK;
  393. riscv_aplic_msi_send(aplic, hart_idx, guest_idx, eiid);
  394. }
  395. static uint32_t riscv_aplic_idc_topi(RISCVAPLICState *aplic, uint32_t idc)
  396. {
  397. uint32_t best_irq, best_iprio;
  398. uint32_t irq, iprio, ihartidx, ithres;
  399. if (aplic->num_harts <= idc) {
  400. return 0;
  401. }
  402. ithres = aplic->ithreshold[idc];
  403. best_irq = best_iprio = UINT32_MAX;
  404. for (irq = 1; irq < aplic->num_irqs; irq++) {
  405. if ((aplic->state[irq] & APLIC_ISTATE_ENPEND) !=
  406. APLIC_ISTATE_ENPEND) {
  407. continue;
  408. }
  409. ihartidx = aplic->target[irq] >> APLIC_TARGET_HART_IDX_SHIFT;
  410. ihartidx &= APLIC_TARGET_HART_IDX_MASK;
  411. if (ihartidx != idc) {
  412. continue;
  413. }
  414. iprio = aplic->target[irq] & aplic->iprio_mask;
  415. if (ithres && iprio >= ithres) {
  416. continue;
  417. }
  418. if (iprio < best_iprio) {
  419. best_irq = irq;
  420. best_iprio = iprio;
  421. }
  422. }
  423. if (best_irq < aplic->num_irqs && best_iprio <= aplic->iprio_mask) {
  424. return (best_irq << APLIC_IDC_TOPI_ID_SHIFT) | best_iprio;
  425. }
  426. return 0;
  427. }
  428. static void riscv_aplic_idc_update(RISCVAPLICState *aplic, uint32_t idc)
  429. {
  430. uint32_t topi;
  431. if (aplic->msimode || aplic->num_harts <= idc) {
  432. return;
  433. }
  434. topi = riscv_aplic_idc_topi(aplic, idc);
  435. if ((aplic->domaincfg & APLIC_DOMAINCFG_IE) &&
  436. aplic->idelivery[idc] &&
  437. (aplic->iforce[idc] || topi)) {
  438. qemu_irq_raise(aplic->external_irqs[idc]);
  439. } else {
  440. qemu_irq_lower(aplic->external_irqs[idc]);
  441. }
  442. }
  443. static uint32_t riscv_aplic_idc_claimi(RISCVAPLICState *aplic, uint32_t idc)
  444. {
  445. uint32_t irq, state, sm, topi = riscv_aplic_idc_topi(aplic, idc);
  446. if (!topi) {
  447. aplic->iforce[idc] = 0;
  448. riscv_aplic_idc_update(aplic, idc);
  449. return 0;
  450. }
  451. irq = (topi >> APLIC_IDC_TOPI_ID_SHIFT) & APLIC_IDC_TOPI_ID_MASK;
  452. sm = aplic->sourcecfg[irq] & APLIC_SOURCECFG_SM_MASK;
  453. state = aplic->state[irq];
  454. riscv_aplic_set_pending_raw(aplic, irq, false);
  455. if ((sm == APLIC_SOURCECFG_SM_LEVEL_HIGH) &&
  456. (state & APLIC_ISTATE_INPUT)) {
  457. riscv_aplic_set_pending_raw(aplic, irq, true);
  458. } else if ((sm == APLIC_SOURCECFG_SM_LEVEL_LOW) &&
  459. !(state & APLIC_ISTATE_INPUT)) {
  460. riscv_aplic_set_pending_raw(aplic, irq, true);
  461. }
  462. riscv_aplic_idc_update(aplic, idc);
  463. return topi;
  464. }
  465. static void riscv_aplic_request(void *opaque, int irq, int level)
  466. {
  467. bool update = false;
  468. RISCVAPLICState *aplic = opaque;
  469. uint32_t sourcecfg, childidx, state, idc;
  470. assert((0 < irq) && (irq < aplic->num_irqs));
  471. sourcecfg = aplic->sourcecfg[irq];
  472. if (sourcecfg & APLIC_SOURCECFG_D) {
  473. childidx = sourcecfg & APLIC_SOURCECFG_CHILDIDX_MASK;
  474. if (childidx < aplic->num_children) {
  475. riscv_aplic_request(aplic->children[childidx], irq, level);
  476. }
  477. return;
  478. }
  479. state = aplic->state[irq];
  480. switch (sourcecfg & APLIC_SOURCECFG_SM_MASK) {
  481. case APLIC_SOURCECFG_SM_EDGE_RISE:
  482. if ((level > 0) && !(state & APLIC_ISTATE_INPUT) &&
  483. !(state & APLIC_ISTATE_PENDING)) {
  484. riscv_aplic_set_pending_raw(aplic, irq, true);
  485. update = true;
  486. }
  487. break;
  488. case APLIC_SOURCECFG_SM_EDGE_FALL:
  489. if ((level <= 0) && (state & APLIC_ISTATE_INPUT) &&
  490. !(state & APLIC_ISTATE_PENDING)) {
  491. riscv_aplic_set_pending_raw(aplic, irq, true);
  492. update = true;
  493. }
  494. break;
  495. case APLIC_SOURCECFG_SM_LEVEL_HIGH:
  496. if ((level > 0) && !(state & APLIC_ISTATE_PENDING)) {
  497. riscv_aplic_set_pending_raw(aplic, irq, true);
  498. update = true;
  499. }
  500. break;
  501. case APLIC_SOURCECFG_SM_LEVEL_LOW:
  502. if ((level <= 0) && !(state & APLIC_ISTATE_PENDING)) {
  503. riscv_aplic_set_pending_raw(aplic, irq, true);
  504. update = true;
  505. }
  506. break;
  507. default:
  508. break;
  509. }
  510. if (level <= 0) {
  511. aplic->state[irq] &= ~APLIC_ISTATE_INPUT;
  512. } else {
  513. aplic->state[irq] |= APLIC_ISTATE_INPUT;
  514. }
  515. if (update) {
  516. if (aplic->msimode) {
  517. riscv_aplic_msi_irq_update(aplic, irq);
  518. } else {
  519. idc = aplic->target[irq] >> APLIC_TARGET_HART_IDX_SHIFT;
  520. idc &= APLIC_TARGET_HART_IDX_MASK;
  521. riscv_aplic_idc_update(aplic, idc);
  522. }
  523. }
  524. }
  525. static uint64_t riscv_aplic_read(void *opaque, hwaddr addr, unsigned size)
  526. {
  527. uint32_t irq, word, idc;
  528. RISCVAPLICState *aplic = opaque;
  529. /* Reads must be 4 byte words */
  530. if ((addr & 0x3) != 0) {
  531. goto err;
  532. }
  533. if (addr == APLIC_DOMAINCFG) {
  534. return APLIC_DOMAINCFG_RDONLY | aplic->domaincfg |
  535. (aplic->msimode ? APLIC_DOMAINCFG_DM : 0);
  536. } else if ((APLIC_SOURCECFG_BASE <= addr) &&
  537. (addr < (APLIC_SOURCECFG_BASE + (aplic->num_irqs - 1) * 4))) {
  538. irq = ((addr - APLIC_SOURCECFG_BASE) >> 2) + 1;
  539. return aplic->sourcecfg[irq];
  540. } else if (aplic->mmode && aplic->msimode &&
  541. (addr == APLIC_MMSICFGADDR)) {
  542. return aplic->mmsicfgaddr;
  543. } else if (aplic->mmode && aplic->msimode &&
  544. (addr == APLIC_MMSICFGADDRH)) {
  545. return aplic->mmsicfgaddrH;
  546. } else if (aplic->mmode && aplic->msimode &&
  547. (addr == APLIC_SMSICFGADDR)) {
  548. /*
  549. * Registers SMSICFGADDR and SMSICFGADDRH are implemented only if:
  550. * (a) the interrupt domain is at machine level
  551. * (b) the domain's harts implement supervisor mode
  552. * (c) the domain has one or more child supervisor-level domains
  553. * that support MSI delivery mode (domaincfg.DM is not read-
  554. * only zero in at least one of the supervisor-level child
  555. * domains).
  556. */
  557. return (aplic->num_children) ? aplic->smsicfgaddr : 0;
  558. } else if (aplic->mmode && aplic->msimode &&
  559. (addr == APLIC_SMSICFGADDRH)) {
  560. return (aplic->num_children) ? aplic->smsicfgaddrH : 0;
  561. } else if ((APLIC_SETIP_BASE <= addr) &&
  562. (addr < (APLIC_SETIP_BASE + aplic->bitfield_words * 4))) {
  563. word = (addr - APLIC_SETIP_BASE) >> 2;
  564. return riscv_aplic_read_pending_word(aplic, word);
  565. } else if (addr == APLIC_SETIPNUM) {
  566. return 0;
  567. } else if ((APLIC_CLRIP_BASE <= addr) &&
  568. (addr < (APLIC_CLRIP_BASE + aplic->bitfield_words * 4))) {
  569. word = (addr - APLIC_CLRIP_BASE) >> 2;
  570. return riscv_aplic_read_input_word(aplic, word);
  571. } else if (addr == APLIC_CLRIPNUM) {
  572. return 0;
  573. } else if ((APLIC_SETIE_BASE <= addr) &&
  574. (addr < (APLIC_SETIE_BASE + aplic->bitfield_words * 4))) {
  575. word = (addr - APLIC_SETIE_BASE) >> 2;
  576. return riscv_aplic_read_enabled_word(aplic, word);
  577. } else if (addr == APLIC_SETIENUM) {
  578. return 0;
  579. } else if ((APLIC_CLRIE_BASE <= addr) &&
  580. (addr < (APLIC_CLRIE_BASE + aplic->bitfield_words * 4))) {
  581. return 0;
  582. } else if (addr == APLIC_CLRIENUM) {
  583. return 0;
  584. } else if (addr == APLIC_SETIPNUM_LE) {
  585. return 0;
  586. } else if (addr == APLIC_SETIPNUM_BE) {
  587. return 0;
  588. } else if (addr == APLIC_GENMSI) {
  589. return (aplic->msimode) ? aplic->genmsi : 0;
  590. } else if ((APLIC_TARGET_BASE <= addr) &&
  591. (addr < (APLIC_TARGET_BASE + (aplic->num_irqs - 1) * 4))) {
  592. irq = ((addr - APLIC_TARGET_BASE) >> 2) + 1;
  593. return aplic->target[irq];
  594. } else if (!aplic->msimode && (APLIC_IDC_BASE <= addr) &&
  595. (addr < (APLIC_IDC_BASE + aplic->num_harts * APLIC_IDC_SIZE))) {
  596. idc = (addr - APLIC_IDC_BASE) / APLIC_IDC_SIZE;
  597. switch (addr - (APLIC_IDC_BASE + idc * APLIC_IDC_SIZE)) {
  598. case APLIC_IDC_IDELIVERY:
  599. return aplic->idelivery[idc];
  600. case APLIC_IDC_IFORCE:
  601. return aplic->iforce[idc];
  602. case APLIC_IDC_ITHRESHOLD:
  603. return aplic->ithreshold[idc];
  604. case APLIC_IDC_TOPI:
  605. return riscv_aplic_idc_topi(aplic, idc);
  606. case APLIC_IDC_CLAIMI:
  607. return riscv_aplic_idc_claimi(aplic, idc);
  608. default:
  609. goto err;
  610. };
  611. }
  612. err:
  613. qemu_log_mask(LOG_GUEST_ERROR,
  614. "%s: Invalid register read 0x%" HWADDR_PRIx "\n",
  615. __func__, addr);
  616. return 0;
  617. }
  618. static void riscv_aplic_write(void *opaque, hwaddr addr, uint64_t value,
  619. unsigned size)
  620. {
  621. RISCVAPLICState *aplic = opaque;
  622. uint32_t irq, word, idc = UINT32_MAX;
  623. /* Writes must be 4 byte words */
  624. if ((addr & 0x3) != 0) {
  625. goto err;
  626. }
  627. if (addr == APLIC_DOMAINCFG) {
  628. /* Only IE bit writable at the moment */
  629. value &= APLIC_DOMAINCFG_IE;
  630. aplic->domaincfg = value;
  631. } else if ((APLIC_SOURCECFG_BASE <= addr) &&
  632. (addr < (APLIC_SOURCECFG_BASE + (aplic->num_irqs - 1) * 4))) {
  633. irq = ((addr - APLIC_SOURCECFG_BASE) >> 2) + 1;
  634. if (!aplic->num_children && (value & APLIC_SOURCECFG_D)) {
  635. value = 0;
  636. }
  637. if (value & APLIC_SOURCECFG_D) {
  638. value &= (APLIC_SOURCECFG_D | APLIC_SOURCECFG_CHILDIDX_MASK);
  639. } else {
  640. value &= (APLIC_SOURCECFG_D | APLIC_SOURCECFG_SM_MASK);
  641. }
  642. aplic->sourcecfg[irq] = value;
  643. if ((aplic->sourcecfg[irq] & APLIC_SOURCECFG_D) ||
  644. (aplic->sourcecfg[irq] == 0)) {
  645. riscv_aplic_set_pending_raw(aplic, irq, false);
  646. riscv_aplic_set_enabled_raw(aplic, irq, false);
  647. } else {
  648. if (riscv_aplic_irq_rectified_val(aplic, irq)) {
  649. riscv_aplic_set_pending_raw(aplic, irq, true);
  650. }
  651. }
  652. } else if (aplic->mmode && aplic->msimode &&
  653. (addr == APLIC_MMSICFGADDR)) {
  654. if (!(aplic->mmsicfgaddrH & APLIC_xMSICFGADDRH_L)) {
  655. aplic->mmsicfgaddr = value;
  656. }
  657. } else if (aplic->mmode && aplic->msimode &&
  658. (addr == APLIC_MMSICFGADDRH)) {
  659. if (!(aplic->mmsicfgaddrH & APLIC_xMSICFGADDRH_L)) {
  660. aplic->mmsicfgaddrH = value & APLIC_xMSICFGADDRH_VALID_MASK;
  661. }
  662. } else if (aplic->mmode && aplic->msimode &&
  663. (addr == APLIC_SMSICFGADDR)) {
  664. /*
  665. * Registers SMSICFGADDR and SMSICFGADDRH are implemented only if:
  666. * (a) the interrupt domain is at machine level
  667. * (b) the domain's harts implement supervisor mode
  668. * (c) the domain has one or more child supervisor-level domains
  669. * that support MSI delivery mode (domaincfg.DM is not read-
  670. * only zero in at least one of the supervisor-level child
  671. * domains).
  672. */
  673. if (aplic->num_children &&
  674. !(aplic->mmsicfgaddrH & APLIC_xMSICFGADDRH_L)) {
  675. aplic->smsicfgaddr = value;
  676. }
  677. } else if (aplic->mmode && aplic->msimode &&
  678. (addr == APLIC_SMSICFGADDRH)) {
  679. if (aplic->num_children &&
  680. !(aplic->mmsicfgaddrH & APLIC_xMSICFGADDRH_L)) {
  681. aplic->smsicfgaddrH = value & APLIC_xMSICFGADDRH_VALID_MASK;
  682. }
  683. } else if ((APLIC_SETIP_BASE <= addr) &&
  684. (addr < (APLIC_SETIP_BASE + aplic->bitfield_words * 4))) {
  685. word = (addr - APLIC_SETIP_BASE) >> 2;
  686. riscv_aplic_set_pending_word(aplic, word, value, true);
  687. } else if (addr == APLIC_SETIPNUM) {
  688. riscv_aplic_set_pending(aplic, value, true);
  689. } else if ((APLIC_CLRIP_BASE <= addr) &&
  690. (addr < (APLIC_CLRIP_BASE + aplic->bitfield_words * 4))) {
  691. word = (addr - APLIC_CLRIP_BASE) >> 2;
  692. riscv_aplic_set_pending_word(aplic, word, value, false);
  693. } else if (addr == APLIC_CLRIPNUM) {
  694. riscv_aplic_set_pending(aplic, value, false);
  695. } else if ((APLIC_SETIE_BASE <= addr) &&
  696. (addr < (APLIC_SETIE_BASE + aplic->bitfield_words * 4))) {
  697. word = (addr - APLIC_SETIE_BASE) >> 2;
  698. riscv_aplic_set_enabled_word(aplic, word, value, true);
  699. } else if (addr == APLIC_SETIENUM) {
  700. riscv_aplic_set_enabled(aplic, value, true);
  701. } else if ((APLIC_CLRIE_BASE <= addr) &&
  702. (addr < (APLIC_CLRIE_BASE + aplic->bitfield_words * 4))) {
  703. word = (addr - APLIC_CLRIE_BASE) >> 2;
  704. riscv_aplic_set_enabled_word(aplic, word, value, false);
  705. } else if (addr == APLIC_CLRIENUM) {
  706. riscv_aplic_set_enabled(aplic, value, false);
  707. } else if (addr == APLIC_SETIPNUM_LE) {
  708. riscv_aplic_set_pending(aplic, value, true);
  709. } else if (addr == APLIC_SETIPNUM_BE) {
  710. riscv_aplic_set_pending(aplic, bswap32(value), true);
  711. } else if (addr == APLIC_GENMSI) {
  712. if (aplic->msimode) {
  713. aplic->genmsi = value & ~(APLIC_TARGET_GUEST_IDX_MASK <<
  714. APLIC_TARGET_GUEST_IDX_SHIFT);
  715. riscv_aplic_msi_send(aplic,
  716. value >> APLIC_TARGET_HART_IDX_SHIFT,
  717. 0,
  718. value & APLIC_TARGET_EIID_MASK);
  719. }
  720. } else if ((APLIC_TARGET_BASE <= addr) &&
  721. (addr < (APLIC_TARGET_BASE + (aplic->num_irqs - 1) * 4))) {
  722. irq = ((addr - APLIC_TARGET_BASE) >> 2) + 1;
  723. if (aplic->msimode) {
  724. aplic->target[irq] = value;
  725. } else {
  726. aplic->target[irq] = (value & ~APLIC_TARGET_IPRIO_MASK) |
  727. ((value & aplic->iprio_mask) ?
  728. (value & aplic->iprio_mask) : 1);
  729. }
  730. } else if (!aplic->msimode && (APLIC_IDC_BASE <= addr) &&
  731. (addr < (APLIC_IDC_BASE + aplic->num_harts * APLIC_IDC_SIZE))) {
  732. idc = (addr - APLIC_IDC_BASE) / APLIC_IDC_SIZE;
  733. switch (addr - (APLIC_IDC_BASE + idc * APLIC_IDC_SIZE)) {
  734. case APLIC_IDC_IDELIVERY:
  735. aplic->idelivery[idc] = value & 0x1;
  736. break;
  737. case APLIC_IDC_IFORCE:
  738. aplic->iforce[idc] = value & 0x1;
  739. break;
  740. case APLIC_IDC_ITHRESHOLD:
  741. aplic->ithreshold[idc] = value & aplic->iprio_mask;
  742. break;
  743. default:
  744. goto err;
  745. };
  746. } else {
  747. goto err;
  748. }
  749. if (aplic->msimode) {
  750. for (irq = 1; irq < aplic->num_irqs; irq++) {
  751. riscv_aplic_msi_irq_update(aplic, irq);
  752. }
  753. } else {
  754. if (idc == UINT32_MAX) {
  755. for (idc = 0; idc < aplic->num_harts; idc++) {
  756. riscv_aplic_idc_update(aplic, idc);
  757. }
  758. } else {
  759. riscv_aplic_idc_update(aplic, idc);
  760. }
  761. }
  762. return;
  763. err:
  764. qemu_log_mask(LOG_GUEST_ERROR,
  765. "%s: Invalid register write 0x%" HWADDR_PRIx "\n",
  766. __func__, addr);
  767. }
  768. static const MemoryRegionOps riscv_aplic_ops = {
  769. .read = riscv_aplic_read,
  770. .write = riscv_aplic_write,
  771. .endianness = DEVICE_LITTLE_ENDIAN,
  772. .valid = {
  773. .min_access_size = 4,
  774. .max_access_size = 4
  775. }
  776. };
  777. static void riscv_aplic_realize(DeviceState *dev, Error **errp)
  778. {
  779. uint32_t i;
  780. RISCVAPLICState *aplic = RISCV_APLIC(dev);
  781. if (riscv_use_emulated_aplic(aplic->msimode)) {
  782. aplic->bitfield_words = (aplic->num_irqs + 31) >> 5;
  783. aplic->sourcecfg = g_new0(uint32_t, aplic->num_irqs);
  784. aplic->state = g_new0(uint32_t, aplic->num_irqs);
  785. aplic->target = g_new0(uint32_t, aplic->num_irqs);
  786. if (!aplic->msimode) {
  787. for (i = 0; i < aplic->num_irqs; i++) {
  788. aplic->target[i] = 1;
  789. }
  790. }
  791. aplic->idelivery = g_new0(uint32_t, aplic->num_harts);
  792. aplic->iforce = g_new0(uint32_t, aplic->num_harts);
  793. aplic->ithreshold = g_new0(uint32_t, aplic->num_harts);
  794. memory_region_init_io(&aplic->mmio, OBJECT(dev), &riscv_aplic_ops,
  795. aplic, TYPE_RISCV_APLIC, aplic->aperture_size);
  796. sysbus_init_mmio(SYS_BUS_DEVICE(dev), &aplic->mmio);
  797. if (kvm_enabled()) {
  798. aplic->kvm_splitmode = true;
  799. }
  800. }
  801. /*
  802. * Only root APLICs have hardware IRQ lines. All non-root APLICs
  803. * have IRQ lines delegated by their parent APLIC.
  804. */
  805. if (!aplic->parent) {
  806. if (kvm_enabled() && !riscv_use_emulated_aplic(aplic->msimode)) {
  807. qdev_init_gpio_in(dev, riscv_kvm_aplic_request, aplic->num_irqs);
  808. } else {
  809. qdev_init_gpio_in(dev, riscv_aplic_request, aplic->num_irqs);
  810. }
  811. }
  812. /* Create output IRQ lines for non-MSI mode */
  813. if (!aplic->msimode) {
  814. aplic->external_irqs = g_malloc(sizeof(qemu_irq) * aplic->num_harts);
  815. qdev_init_gpio_out(dev, aplic->external_irqs, aplic->num_harts);
  816. /* Claim the CPU interrupt to be triggered by this APLIC */
  817. for (i = 0; i < aplic->num_harts; i++) {
  818. RISCVCPU *cpu = RISCV_CPU(cpu_by_arch_id(aplic->hartid_base + i));
  819. if (riscv_cpu_claim_interrupts(cpu,
  820. (aplic->mmode) ? MIP_MEIP : MIP_SEIP) < 0) {
  821. error_report("%s already claimed",
  822. (aplic->mmode) ? "MEIP" : "SEIP");
  823. exit(1);
  824. }
  825. }
  826. }
  827. msi_nonbroken = true;
  828. }
  829. static const Property riscv_aplic_properties[] = {
  830. DEFINE_PROP_UINT32("aperture-size", RISCVAPLICState, aperture_size, 0),
  831. DEFINE_PROP_UINT32("hartid-base", RISCVAPLICState, hartid_base, 0),
  832. DEFINE_PROP_UINT32("num-harts", RISCVAPLICState, num_harts, 0),
  833. DEFINE_PROP_UINT32("iprio-mask", RISCVAPLICState, iprio_mask, 0),
  834. DEFINE_PROP_UINT32("num-irqs", RISCVAPLICState, num_irqs, 0),
  835. DEFINE_PROP_BOOL("msimode", RISCVAPLICState, msimode, 0),
  836. DEFINE_PROP_BOOL("mmode", RISCVAPLICState, mmode, 0),
  837. };
  838. static const VMStateDescription vmstate_riscv_aplic = {
  839. .name = "riscv_aplic",
  840. .version_id = 2,
  841. .minimum_version_id = 2,
  842. .fields = (const VMStateField[]) {
  843. VMSTATE_UINT32(domaincfg, RISCVAPLICState),
  844. VMSTATE_UINT32(mmsicfgaddr, RISCVAPLICState),
  845. VMSTATE_UINT32(mmsicfgaddrH, RISCVAPLICState),
  846. VMSTATE_UINT32(smsicfgaddr, RISCVAPLICState),
  847. VMSTATE_UINT32(smsicfgaddrH, RISCVAPLICState),
  848. VMSTATE_UINT32(genmsi, RISCVAPLICState),
  849. VMSTATE_UINT32(kvm_msicfgaddr, RISCVAPLICState),
  850. VMSTATE_UINT32(kvm_msicfgaddrH, RISCVAPLICState),
  851. VMSTATE_VARRAY_UINT32(sourcecfg, RISCVAPLICState,
  852. num_irqs, 0,
  853. vmstate_info_uint32, uint32_t),
  854. VMSTATE_VARRAY_UINT32(state, RISCVAPLICState,
  855. num_irqs, 0,
  856. vmstate_info_uint32, uint32_t),
  857. VMSTATE_VARRAY_UINT32(target, RISCVAPLICState,
  858. num_irqs, 0,
  859. vmstate_info_uint32, uint32_t),
  860. VMSTATE_VARRAY_UINT32(idelivery, RISCVAPLICState,
  861. num_harts, 0,
  862. vmstate_info_uint32, uint32_t),
  863. VMSTATE_VARRAY_UINT32(iforce, RISCVAPLICState,
  864. num_harts, 0,
  865. vmstate_info_uint32, uint32_t),
  866. VMSTATE_VARRAY_UINT32(ithreshold, RISCVAPLICState,
  867. num_harts, 0,
  868. vmstate_info_uint32, uint32_t),
  869. VMSTATE_END_OF_LIST()
  870. }
  871. };
  872. static void riscv_aplic_class_init(ObjectClass *klass, void *data)
  873. {
  874. DeviceClass *dc = DEVICE_CLASS(klass);
  875. device_class_set_props(dc, riscv_aplic_properties);
  876. dc->realize = riscv_aplic_realize;
  877. dc->vmsd = &vmstate_riscv_aplic;
  878. }
  879. static const TypeInfo riscv_aplic_info = {
  880. .name = TYPE_RISCV_APLIC,
  881. .parent = TYPE_SYS_BUS_DEVICE,
  882. .instance_size = sizeof(RISCVAPLICState),
  883. .class_init = riscv_aplic_class_init,
  884. };
  885. static void riscv_aplic_register_types(void)
  886. {
  887. type_register_static(&riscv_aplic_info);
  888. }
  889. type_init(riscv_aplic_register_types)
  890. /*
  891. * Add a APLIC device to another APLIC device as child for
  892. * interrupt delegation.
  893. */
  894. void riscv_aplic_add_child(DeviceState *parent, DeviceState *child)
  895. {
  896. RISCVAPLICState *caplic, *paplic;
  897. assert(parent && child);
  898. caplic = RISCV_APLIC(child);
  899. paplic = RISCV_APLIC(parent);
  900. assert(paplic->num_irqs == caplic->num_irqs);
  901. assert(paplic->num_children <= QEMU_APLIC_MAX_CHILDREN);
  902. caplic->parent = paplic;
  903. paplic->children[paplic->num_children] = caplic;
  904. paplic->num_children++;
  905. }
  906. /*
  907. * Create APLIC device.
  908. */
  909. DeviceState *riscv_aplic_create(hwaddr addr, hwaddr size,
  910. uint32_t hartid_base, uint32_t num_harts, uint32_t num_sources,
  911. uint32_t iprio_bits, bool msimode, bool mmode, DeviceState *parent)
  912. {
  913. DeviceState *dev = qdev_new(TYPE_RISCV_APLIC);
  914. uint32_t i;
  915. assert(num_harts < APLIC_MAX_IDC);
  916. assert((APLIC_IDC_BASE + (num_harts * APLIC_IDC_SIZE)) <= size);
  917. assert(num_sources < APLIC_MAX_SOURCE);
  918. assert(APLIC_MIN_IPRIO_BITS <= iprio_bits);
  919. assert(iprio_bits <= APLIC_MAX_IPRIO_BITS);
  920. qdev_prop_set_uint32(dev, "aperture-size", size);
  921. qdev_prop_set_uint32(dev, "hartid-base", hartid_base);
  922. qdev_prop_set_uint32(dev, "num-harts", num_harts);
  923. qdev_prop_set_uint32(dev, "iprio-mask", ((1U << iprio_bits) - 1));
  924. qdev_prop_set_uint32(dev, "num-irqs", num_sources + 1);
  925. qdev_prop_set_bit(dev, "msimode", msimode);
  926. qdev_prop_set_bit(dev, "mmode", mmode);
  927. if (parent) {
  928. riscv_aplic_add_child(parent, dev);
  929. }
  930. sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
  931. if (riscv_use_emulated_aplic(msimode)) {
  932. sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, addr);
  933. }
  934. if (!msimode) {
  935. for (i = 0; i < num_harts; i++) {
  936. CPUState *cpu = cpu_by_arch_id(hartid_base + i);
  937. qdev_connect_gpio_out_named(dev, NULL, i,
  938. qdev_get_gpio_in(DEVICE(cpu),
  939. (mmode) ? IRQ_M_EXT : IRQ_S_EXT));
  940. }
  941. }
  942. return dev;
  943. }