2
0

riscv_aplic.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088
  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. addr >>= APLIC_xMSICFGADDR_PPN_SHIFT;
  155. aplic->kvm_msicfgaddr = extract64(addr, 0, 32);
  156. aplic->kvm_msicfgaddrH = extract64(addr, 32, 32) &
  157. APLIC_xMSICFGADDRH_VALID_MASK;
  158. }
  159. #endif
  160. }
  161. static bool riscv_aplic_irq_rectified_val(RISCVAPLICState *aplic,
  162. uint32_t irq)
  163. {
  164. uint32_t sourcecfg, sm, raw_input, irq_inverted;
  165. if (!irq || aplic->num_irqs <= irq) {
  166. return false;
  167. }
  168. sourcecfg = aplic->sourcecfg[irq];
  169. if (sourcecfg & APLIC_SOURCECFG_D) {
  170. return false;
  171. }
  172. sm = sourcecfg & APLIC_SOURCECFG_SM_MASK;
  173. if (sm == APLIC_SOURCECFG_SM_INACTIVE) {
  174. return false;
  175. }
  176. raw_input = (aplic->state[irq] & APLIC_ISTATE_INPUT) ? 1 : 0;
  177. irq_inverted = (sm == APLIC_SOURCECFG_SM_LEVEL_LOW ||
  178. sm == APLIC_SOURCECFG_SM_EDGE_FALL) ? 1 : 0;
  179. return !!(raw_input ^ irq_inverted);
  180. }
  181. static uint32_t riscv_aplic_read_input_word(RISCVAPLICState *aplic,
  182. uint32_t word)
  183. {
  184. uint32_t i, irq, rectified_val, ret = 0;
  185. for (i = 0; i < 32; i++) {
  186. irq = word * 32 + i;
  187. rectified_val = riscv_aplic_irq_rectified_val(aplic, irq);
  188. ret |= rectified_val << i;
  189. }
  190. return ret;
  191. }
  192. static uint32_t riscv_aplic_read_pending_word(RISCVAPLICState *aplic,
  193. uint32_t word)
  194. {
  195. uint32_t i, irq, ret = 0;
  196. for (i = 0; i < 32; i++) {
  197. irq = word * 32 + i;
  198. if (!irq || aplic->num_irqs <= irq) {
  199. continue;
  200. }
  201. ret |= ((aplic->state[irq] & APLIC_ISTATE_PENDING) ? 1 : 0) << i;
  202. }
  203. return ret;
  204. }
  205. static void riscv_aplic_set_pending_raw(RISCVAPLICState *aplic,
  206. uint32_t irq, bool pending)
  207. {
  208. if (pending) {
  209. aplic->state[irq] |= APLIC_ISTATE_PENDING;
  210. } else {
  211. aplic->state[irq] &= ~APLIC_ISTATE_PENDING;
  212. }
  213. }
  214. static void riscv_aplic_set_pending(RISCVAPLICState *aplic,
  215. uint32_t irq, bool pending)
  216. {
  217. uint32_t sourcecfg, sm;
  218. if ((irq <= 0) || (aplic->num_irqs <= irq)) {
  219. return;
  220. }
  221. sourcecfg = aplic->sourcecfg[irq];
  222. if (sourcecfg & APLIC_SOURCECFG_D) {
  223. return;
  224. }
  225. sm = sourcecfg & APLIC_SOURCECFG_SM_MASK;
  226. if (sm == APLIC_SOURCECFG_SM_INACTIVE) {
  227. return;
  228. }
  229. if ((sm == APLIC_SOURCECFG_SM_LEVEL_HIGH) ||
  230. (sm == APLIC_SOURCECFG_SM_LEVEL_LOW)) {
  231. if (!aplic->msimode) {
  232. return;
  233. }
  234. if (aplic->msimode && !pending) {
  235. goto noskip_write_pending;
  236. }
  237. if ((aplic->state[irq] & APLIC_ISTATE_INPUT) &&
  238. (sm == APLIC_SOURCECFG_SM_LEVEL_LOW)) {
  239. return;
  240. }
  241. if (!(aplic->state[irq] & APLIC_ISTATE_INPUT) &&
  242. (sm == APLIC_SOURCECFG_SM_LEVEL_HIGH)) {
  243. return;
  244. }
  245. }
  246. noskip_write_pending:
  247. riscv_aplic_set_pending_raw(aplic, irq, pending);
  248. }
  249. static void riscv_aplic_set_pending_word(RISCVAPLICState *aplic,
  250. uint32_t word, uint32_t value,
  251. bool pending)
  252. {
  253. uint32_t i, irq;
  254. for (i = 0; i < 32; i++) {
  255. irq = word * 32 + i;
  256. if (!irq || aplic->num_irqs <= irq) {
  257. continue;
  258. }
  259. if (value & (1U << i)) {
  260. riscv_aplic_set_pending(aplic, irq, pending);
  261. }
  262. }
  263. }
  264. static uint32_t riscv_aplic_read_enabled_word(RISCVAPLICState *aplic,
  265. int word)
  266. {
  267. uint32_t i, irq, ret = 0;
  268. for (i = 0; i < 32; i++) {
  269. irq = word * 32 + i;
  270. if (!irq || aplic->num_irqs <= irq) {
  271. continue;
  272. }
  273. ret |= ((aplic->state[irq] & APLIC_ISTATE_ENABLED) ? 1 : 0) << i;
  274. }
  275. return ret;
  276. }
  277. static void riscv_aplic_set_enabled_raw(RISCVAPLICState *aplic,
  278. uint32_t irq, bool enabled)
  279. {
  280. if (enabled) {
  281. aplic->state[irq] |= APLIC_ISTATE_ENABLED;
  282. } else {
  283. aplic->state[irq] &= ~APLIC_ISTATE_ENABLED;
  284. }
  285. }
  286. static void riscv_aplic_set_enabled(RISCVAPLICState *aplic,
  287. uint32_t irq, bool enabled)
  288. {
  289. uint32_t sourcecfg, sm;
  290. if ((irq <= 0) || (aplic->num_irqs <= irq)) {
  291. return;
  292. }
  293. sourcecfg = aplic->sourcecfg[irq];
  294. if (sourcecfg & APLIC_SOURCECFG_D) {
  295. return;
  296. }
  297. sm = sourcecfg & APLIC_SOURCECFG_SM_MASK;
  298. if (sm == APLIC_SOURCECFG_SM_INACTIVE) {
  299. return;
  300. }
  301. riscv_aplic_set_enabled_raw(aplic, irq, enabled);
  302. }
  303. static void riscv_aplic_set_enabled_word(RISCVAPLICState *aplic,
  304. uint32_t word, uint32_t value,
  305. bool enabled)
  306. {
  307. uint32_t i, irq;
  308. for (i = 0; i < 32; i++) {
  309. irq = word * 32 + i;
  310. if (!irq || aplic->num_irqs <= irq) {
  311. continue;
  312. }
  313. if (value & (1U << i)) {
  314. riscv_aplic_set_enabled(aplic, irq, enabled);
  315. }
  316. }
  317. }
  318. static void riscv_aplic_msi_send(RISCVAPLICState *aplic,
  319. uint32_t hart_idx, uint32_t guest_idx,
  320. uint32_t eiid)
  321. {
  322. uint64_t addr;
  323. MemTxResult result;
  324. RISCVAPLICState *aplic_m;
  325. uint32_t lhxs, lhxw, hhxs, hhxw, group_idx, msicfgaddr, msicfgaddrH;
  326. aplic_m = aplic;
  327. if (!aplic->kvm_splitmode) {
  328. while (aplic_m && !aplic_m->mmode) {
  329. aplic_m = aplic_m->parent;
  330. }
  331. if (!aplic_m) {
  332. qemu_log_mask(LOG_GUEST_ERROR, "%s: m-level APLIC not found\n",
  333. __func__);
  334. return;
  335. }
  336. }
  337. if (aplic->kvm_splitmode) {
  338. msicfgaddr = aplic->kvm_msicfgaddr;
  339. msicfgaddrH = ((uint64_t)aplic->kvm_msicfgaddrH << 32);
  340. } else {
  341. if (aplic->mmode) {
  342. msicfgaddr = aplic_m->mmsicfgaddr;
  343. msicfgaddrH = aplic_m->mmsicfgaddrH;
  344. } else {
  345. msicfgaddr = aplic_m->smsicfgaddr;
  346. msicfgaddrH = aplic_m->smsicfgaddrH;
  347. }
  348. }
  349. lhxs = (msicfgaddrH >> APLIC_xMSICFGADDRH_LHXS_SHIFT) &
  350. APLIC_xMSICFGADDRH_LHXS_MASK;
  351. lhxw = (msicfgaddrH >> APLIC_xMSICFGADDRH_LHXW_SHIFT) &
  352. APLIC_xMSICFGADDRH_LHXW_MASK;
  353. hhxs = (msicfgaddrH >> APLIC_xMSICFGADDRH_HHXS_SHIFT) &
  354. APLIC_xMSICFGADDRH_HHXS_MASK;
  355. hhxw = (msicfgaddrH >> APLIC_xMSICFGADDRH_HHXW_SHIFT) &
  356. APLIC_xMSICFGADDRH_HHXW_MASK;
  357. group_idx = hart_idx >> lhxw;
  358. addr = msicfgaddr;
  359. addr |= ((uint64_t)(msicfgaddrH & APLIC_xMSICFGADDRH_BAPPN_MASK)) << 32;
  360. addr |= ((uint64_t)(group_idx & APLIC_xMSICFGADDR_PPN_HHX_MASK(hhxw))) <<
  361. APLIC_xMSICFGADDR_PPN_HHX_SHIFT(hhxs);
  362. addr |= ((uint64_t)(hart_idx & APLIC_xMSICFGADDR_PPN_LHX_MASK(lhxw))) <<
  363. APLIC_xMSICFGADDR_PPN_LHX_SHIFT(lhxs);
  364. addr |= (uint64_t)(guest_idx & APLIC_xMSICFGADDR_PPN_HART(lhxs));
  365. addr <<= APLIC_xMSICFGADDR_PPN_SHIFT;
  366. address_space_stl_le(&address_space_memory, addr,
  367. eiid, MEMTXATTRS_UNSPECIFIED, &result);
  368. if (result != MEMTX_OK) {
  369. qemu_log_mask(LOG_GUEST_ERROR, "%s: MSI write failed for "
  370. "hart_index=%d guest_index=%d eiid=%d\n",
  371. __func__, hart_idx, guest_idx, eiid);
  372. }
  373. }
  374. static void riscv_aplic_msi_irq_update(RISCVAPLICState *aplic, uint32_t irq)
  375. {
  376. uint32_t hart_idx, guest_idx, eiid;
  377. if (!aplic->msimode || (aplic->num_irqs <= irq) ||
  378. !(aplic->domaincfg & APLIC_DOMAINCFG_IE)) {
  379. return;
  380. }
  381. if ((aplic->state[irq] & APLIC_ISTATE_ENPEND) != APLIC_ISTATE_ENPEND) {
  382. return;
  383. }
  384. riscv_aplic_set_pending_raw(aplic, irq, false);
  385. hart_idx = aplic->target[irq] >> APLIC_TARGET_HART_IDX_SHIFT;
  386. hart_idx &= APLIC_TARGET_HART_IDX_MASK;
  387. if (aplic->mmode) {
  388. /* M-level APLIC ignores guest_index */
  389. guest_idx = 0;
  390. } else {
  391. guest_idx = aplic->target[irq] >> APLIC_TARGET_GUEST_IDX_SHIFT;
  392. guest_idx &= APLIC_TARGET_GUEST_IDX_MASK;
  393. }
  394. eiid = aplic->target[irq] & APLIC_TARGET_EIID_MASK;
  395. riscv_aplic_msi_send(aplic, hart_idx, guest_idx, eiid);
  396. }
  397. static uint32_t riscv_aplic_idc_topi(RISCVAPLICState *aplic, uint32_t idc)
  398. {
  399. uint32_t best_irq, best_iprio;
  400. uint32_t irq, iprio, ihartidx, ithres;
  401. if (aplic->num_harts <= idc) {
  402. return 0;
  403. }
  404. ithres = aplic->ithreshold[idc];
  405. best_irq = best_iprio = UINT32_MAX;
  406. for (irq = 1; irq < aplic->num_irqs; irq++) {
  407. if ((aplic->state[irq] & APLIC_ISTATE_ENPEND) !=
  408. APLIC_ISTATE_ENPEND) {
  409. continue;
  410. }
  411. ihartidx = aplic->target[irq] >> APLIC_TARGET_HART_IDX_SHIFT;
  412. ihartidx &= APLIC_TARGET_HART_IDX_MASK;
  413. if (ihartidx != idc) {
  414. continue;
  415. }
  416. iprio = aplic->target[irq] & aplic->iprio_mask;
  417. if (ithres && iprio >= ithres) {
  418. continue;
  419. }
  420. if (iprio < best_iprio) {
  421. best_irq = irq;
  422. best_iprio = iprio;
  423. }
  424. }
  425. if (best_irq < aplic->num_irqs && best_iprio <= aplic->iprio_mask) {
  426. return (best_irq << APLIC_IDC_TOPI_ID_SHIFT) | best_iprio;
  427. }
  428. return 0;
  429. }
  430. static void riscv_aplic_idc_update(RISCVAPLICState *aplic, uint32_t idc)
  431. {
  432. uint32_t topi;
  433. if (aplic->msimode || aplic->num_harts <= idc) {
  434. return;
  435. }
  436. topi = riscv_aplic_idc_topi(aplic, idc);
  437. if ((aplic->domaincfg & APLIC_DOMAINCFG_IE) &&
  438. aplic->idelivery[idc] &&
  439. (aplic->iforce[idc] || topi)) {
  440. qemu_irq_raise(aplic->external_irqs[idc]);
  441. } else {
  442. qemu_irq_lower(aplic->external_irqs[idc]);
  443. }
  444. }
  445. static uint32_t riscv_aplic_idc_claimi(RISCVAPLICState *aplic, uint32_t idc)
  446. {
  447. uint32_t irq, state, sm, topi = riscv_aplic_idc_topi(aplic, idc);
  448. if (!topi) {
  449. aplic->iforce[idc] = 0;
  450. riscv_aplic_idc_update(aplic, idc);
  451. return 0;
  452. }
  453. irq = (topi >> APLIC_IDC_TOPI_ID_SHIFT) & APLIC_IDC_TOPI_ID_MASK;
  454. sm = aplic->sourcecfg[irq] & APLIC_SOURCECFG_SM_MASK;
  455. state = aplic->state[irq];
  456. riscv_aplic_set_pending_raw(aplic, irq, false);
  457. if ((sm == APLIC_SOURCECFG_SM_LEVEL_HIGH) &&
  458. (state & APLIC_ISTATE_INPUT)) {
  459. riscv_aplic_set_pending_raw(aplic, irq, true);
  460. } else if ((sm == APLIC_SOURCECFG_SM_LEVEL_LOW) &&
  461. !(state & APLIC_ISTATE_INPUT)) {
  462. riscv_aplic_set_pending_raw(aplic, irq, true);
  463. }
  464. riscv_aplic_idc_update(aplic, idc);
  465. return topi;
  466. }
  467. static void riscv_aplic_request(void *opaque, int irq, int level)
  468. {
  469. bool update = false;
  470. RISCVAPLICState *aplic = opaque;
  471. uint32_t sourcecfg, childidx, state, idc;
  472. assert((0 < irq) && (irq < aplic->num_irqs));
  473. sourcecfg = aplic->sourcecfg[irq];
  474. if (sourcecfg & APLIC_SOURCECFG_D) {
  475. childidx = sourcecfg & APLIC_SOURCECFG_CHILDIDX_MASK;
  476. if (childidx < aplic->num_children) {
  477. riscv_aplic_request(aplic->children[childidx], irq, level);
  478. }
  479. return;
  480. }
  481. state = aplic->state[irq];
  482. switch (sourcecfg & APLIC_SOURCECFG_SM_MASK) {
  483. case APLIC_SOURCECFG_SM_EDGE_RISE:
  484. if ((level > 0) && !(state & APLIC_ISTATE_INPUT) &&
  485. !(state & APLIC_ISTATE_PENDING)) {
  486. riscv_aplic_set_pending_raw(aplic, irq, true);
  487. update = true;
  488. }
  489. break;
  490. case APLIC_SOURCECFG_SM_EDGE_FALL:
  491. if ((level <= 0) && (state & APLIC_ISTATE_INPUT) &&
  492. !(state & APLIC_ISTATE_PENDING)) {
  493. riscv_aplic_set_pending_raw(aplic, irq, true);
  494. update = true;
  495. }
  496. break;
  497. case APLIC_SOURCECFG_SM_LEVEL_HIGH:
  498. if ((level > 0) && !(state & APLIC_ISTATE_PENDING)) {
  499. riscv_aplic_set_pending_raw(aplic, irq, true);
  500. update = true;
  501. }
  502. break;
  503. case APLIC_SOURCECFG_SM_LEVEL_LOW:
  504. if ((level <= 0) && !(state & APLIC_ISTATE_PENDING)) {
  505. riscv_aplic_set_pending_raw(aplic, irq, true);
  506. update = true;
  507. }
  508. break;
  509. default:
  510. break;
  511. }
  512. if (level <= 0) {
  513. aplic->state[irq] &= ~APLIC_ISTATE_INPUT;
  514. } else {
  515. aplic->state[irq] |= APLIC_ISTATE_INPUT;
  516. }
  517. if (update) {
  518. if (aplic->msimode) {
  519. riscv_aplic_msi_irq_update(aplic, irq);
  520. } else {
  521. idc = aplic->target[irq] >> APLIC_TARGET_HART_IDX_SHIFT;
  522. idc &= APLIC_TARGET_HART_IDX_MASK;
  523. riscv_aplic_idc_update(aplic, idc);
  524. }
  525. }
  526. }
  527. static uint64_t riscv_aplic_read(void *opaque, hwaddr addr, unsigned size)
  528. {
  529. uint32_t irq, word, idc;
  530. RISCVAPLICState *aplic = opaque;
  531. /* Reads must be 4 byte words */
  532. if ((addr & 0x3) != 0) {
  533. goto err;
  534. }
  535. if (addr == APLIC_DOMAINCFG) {
  536. return APLIC_DOMAINCFG_RDONLY | aplic->domaincfg |
  537. (aplic->msimode ? APLIC_DOMAINCFG_DM : 0);
  538. } else if ((APLIC_SOURCECFG_BASE <= addr) &&
  539. (addr < (APLIC_SOURCECFG_BASE + (aplic->num_irqs - 1) * 4))) {
  540. irq = ((addr - APLIC_SOURCECFG_BASE) >> 2) + 1;
  541. return aplic->sourcecfg[irq];
  542. } else if (aplic->mmode && aplic->msimode &&
  543. (addr == APLIC_MMSICFGADDR)) {
  544. return aplic->mmsicfgaddr;
  545. } else if (aplic->mmode && aplic->msimode &&
  546. (addr == APLIC_MMSICFGADDRH)) {
  547. return aplic->mmsicfgaddrH;
  548. } else if (aplic->mmode && aplic->msimode &&
  549. (addr == APLIC_SMSICFGADDR)) {
  550. /*
  551. * Registers SMSICFGADDR and SMSICFGADDRH are implemented only if:
  552. * (a) the interrupt domain is at machine level
  553. * (b) the domain's harts implement supervisor mode
  554. * (c) the domain has one or more child supervisor-level domains
  555. * that support MSI delivery mode (domaincfg.DM is not read-
  556. * only zero in at least one of the supervisor-level child
  557. * domains).
  558. */
  559. return (aplic->num_children) ? aplic->smsicfgaddr : 0;
  560. } else if (aplic->mmode && aplic->msimode &&
  561. (addr == APLIC_SMSICFGADDRH)) {
  562. return (aplic->num_children) ? aplic->smsicfgaddrH : 0;
  563. } else if ((APLIC_SETIP_BASE <= addr) &&
  564. (addr < (APLIC_SETIP_BASE + aplic->bitfield_words * 4))) {
  565. word = (addr - APLIC_SETIP_BASE) >> 2;
  566. return riscv_aplic_read_pending_word(aplic, word);
  567. } else if (addr == APLIC_SETIPNUM) {
  568. return 0;
  569. } else if ((APLIC_CLRIP_BASE <= addr) &&
  570. (addr < (APLIC_CLRIP_BASE + aplic->bitfield_words * 4))) {
  571. word = (addr - APLIC_CLRIP_BASE) >> 2;
  572. return riscv_aplic_read_input_word(aplic, word);
  573. } else if (addr == APLIC_CLRIPNUM) {
  574. return 0;
  575. } else if ((APLIC_SETIE_BASE <= addr) &&
  576. (addr < (APLIC_SETIE_BASE + aplic->bitfield_words * 4))) {
  577. word = (addr - APLIC_SETIE_BASE) >> 2;
  578. return riscv_aplic_read_enabled_word(aplic, word);
  579. } else if (addr == APLIC_SETIENUM) {
  580. return 0;
  581. } else if ((APLIC_CLRIE_BASE <= addr) &&
  582. (addr < (APLIC_CLRIE_BASE + aplic->bitfield_words * 4))) {
  583. return 0;
  584. } else if (addr == APLIC_CLRIENUM) {
  585. return 0;
  586. } else if (addr == APLIC_SETIPNUM_LE) {
  587. return 0;
  588. } else if (addr == APLIC_SETIPNUM_BE) {
  589. return 0;
  590. } else if (addr == APLIC_GENMSI) {
  591. return (aplic->msimode) ? aplic->genmsi : 0;
  592. } else if ((APLIC_TARGET_BASE <= addr) &&
  593. (addr < (APLIC_TARGET_BASE + (aplic->num_irqs - 1) * 4))) {
  594. irq = ((addr - APLIC_TARGET_BASE) >> 2) + 1;
  595. return aplic->target[irq];
  596. } else if (!aplic->msimode && (APLIC_IDC_BASE <= addr) &&
  597. (addr < (APLIC_IDC_BASE + aplic->num_harts * APLIC_IDC_SIZE))) {
  598. idc = (addr - APLIC_IDC_BASE) / APLIC_IDC_SIZE;
  599. switch (addr - (APLIC_IDC_BASE + idc * APLIC_IDC_SIZE)) {
  600. case APLIC_IDC_IDELIVERY:
  601. return aplic->idelivery[idc];
  602. case APLIC_IDC_IFORCE:
  603. return aplic->iforce[idc];
  604. case APLIC_IDC_ITHRESHOLD:
  605. return aplic->ithreshold[idc];
  606. case APLIC_IDC_TOPI:
  607. return riscv_aplic_idc_topi(aplic, idc);
  608. case APLIC_IDC_CLAIMI:
  609. return riscv_aplic_idc_claimi(aplic, idc);
  610. default:
  611. goto err;
  612. };
  613. }
  614. err:
  615. qemu_log_mask(LOG_GUEST_ERROR,
  616. "%s: Invalid register read 0x%" HWADDR_PRIx "\n",
  617. __func__, addr);
  618. return 0;
  619. }
  620. static void riscv_aplic_write(void *opaque, hwaddr addr, uint64_t value,
  621. unsigned size)
  622. {
  623. RISCVAPLICState *aplic = opaque;
  624. uint32_t irq, word, idc = UINT32_MAX;
  625. /* Writes must be 4 byte words */
  626. if ((addr & 0x3) != 0) {
  627. goto err;
  628. }
  629. if (addr == APLIC_DOMAINCFG) {
  630. /* Only IE bit writable at the moment */
  631. value &= APLIC_DOMAINCFG_IE;
  632. aplic->domaincfg = value;
  633. } else if ((APLIC_SOURCECFG_BASE <= addr) &&
  634. (addr < (APLIC_SOURCECFG_BASE + (aplic->num_irqs - 1) * 4))) {
  635. irq = ((addr - APLIC_SOURCECFG_BASE) >> 2) + 1;
  636. if (!aplic->num_children && (value & APLIC_SOURCECFG_D)) {
  637. value = 0;
  638. }
  639. if (value & APLIC_SOURCECFG_D) {
  640. value &= (APLIC_SOURCECFG_D | APLIC_SOURCECFG_CHILDIDX_MASK);
  641. } else {
  642. value &= (APLIC_SOURCECFG_D | APLIC_SOURCECFG_SM_MASK);
  643. }
  644. aplic->sourcecfg[irq] = value;
  645. if ((aplic->sourcecfg[irq] & APLIC_SOURCECFG_D) ||
  646. (aplic->sourcecfg[irq] == 0)) {
  647. riscv_aplic_set_pending_raw(aplic, irq, false);
  648. riscv_aplic_set_enabled_raw(aplic, irq, false);
  649. } else {
  650. if (riscv_aplic_irq_rectified_val(aplic, irq)) {
  651. riscv_aplic_set_pending_raw(aplic, irq, true);
  652. }
  653. }
  654. } else if (aplic->mmode && aplic->msimode &&
  655. (addr == APLIC_MMSICFGADDR)) {
  656. if (!(aplic->mmsicfgaddrH & APLIC_xMSICFGADDRH_L)) {
  657. aplic->mmsicfgaddr = value;
  658. }
  659. } else if (aplic->mmode && aplic->msimode &&
  660. (addr == APLIC_MMSICFGADDRH)) {
  661. if (!(aplic->mmsicfgaddrH & APLIC_xMSICFGADDRH_L)) {
  662. aplic->mmsicfgaddrH = value & APLIC_xMSICFGADDRH_VALID_MASK;
  663. }
  664. } else if (aplic->mmode && aplic->msimode &&
  665. (addr == APLIC_SMSICFGADDR)) {
  666. /*
  667. * Registers SMSICFGADDR and SMSICFGADDRH are implemented only if:
  668. * (a) the interrupt domain is at machine level
  669. * (b) the domain's harts implement supervisor mode
  670. * (c) the domain has one or more child supervisor-level domains
  671. * that support MSI delivery mode (domaincfg.DM is not read-
  672. * only zero in at least one of the supervisor-level child
  673. * domains).
  674. */
  675. if (aplic->num_children &&
  676. !(aplic->mmsicfgaddrH & APLIC_xMSICFGADDRH_L)) {
  677. aplic->smsicfgaddr = value;
  678. }
  679. } else if (aplic->mmode && aplic->msimode &&
  680. (addr == APLIC_SMSICFGADDRH)) {
  681. if (aplic->num_children &&
  682. !(aplic->mmsicfgaddrH & APLIC_xMSICFGADDRH_L)) {
  683. aplic->smsicfgaddrH = value & APLIC_xMSICFGADDRH_VALID_MASK;
  684. }
  685. } else if ((APLIC_SETIP_BASE <= addr) &&
  686. (addr < (APLIC_SETIP_BASE + aplic->bitfield_words * 4))) {
  687. word = (addr - APLIC_SETIP_BASE) >> 2;
  688. riscv_aplic_set_pending_word(aplic, word, value, true);
  689. } else if (addr == APLIC_SETIPNUM) {
  690. riscv_aplic_set_pending(aplic, value, true);
  691. } else if ((APLIC_CLRIP_BASE <= addr) &&
  692. (addr < (APLIC_CLRIP_BASE + aplic->bitfield_words * 4))) {
  693. word = (addr - APLIC_CLRIP_BASE) >> 2;
  694. riscv_aplic_set_pending_word(aplic, word, value, false);
  695. } else if (addr == APLIC_CLRIPNUM) {
  696. riscv_aplic_set_pending(aplic, value, false);
  697. } else if ((APLIC_SETIE_BASE <= addr) &&
  698. (addr < (APLIC_SETIE_BASE + aplic->bitfield_words * 4))) {
  699. word = (addr - APLIC_SETIE_BASE) >> 2;
  700. riscv_aplic_set_enabled_word(aplic, word, value, true);
  701. } else if (addr == APLIC_SETIENUM) {
  702. riscv_aplic_set_enabled(aplic, value, true);
  703. } else if ((APLIC_CLRIE_BASE <= addr) &&
  704. (addr < (APLIC_CLRIE_BASE + aplic->bitfield_words * 4))) {
  705. word = (addr - APLIC_CLRIE_BASE) >> 2;
  706. riscv_aplic_set_enabled_word(aplic, word, value, false);
  707. } else if (addr == APLIC_CLRIENUM) {
  708. riscv_aplic_set_enabled(aplic, value, false);
  709. } else if (addr == APLIC_SETIPNUM_LE) {
  710. riscv_aplic_set_pending(aplic, value, true);
  711. } else if (addr == APLIC_SETIPNUM_BE) {
  712. riscv_aplic_set_pending(aplic, bswap32(value), true);
  713. } else if (addr == APLIC_GENMSI) {
  714. if (aplic->msimode) {
  715. aplic->genmsi = value & ~(APLIC_TARGET_GUEST_IDX_MASK <<
  716. APLIC_TARGET_GUEST_IDX_SHIFT);
  717. riscv_aplic_msi_send(aplic,
  718. value >> APLIC_TARGET_HART_IDX_SHIFT,
  719. 0,
  720. value & APLIC_TARGET_EIID_MASK);
  721. }
  722. } else if ((APLIC_TARGET_BASE <= addr) &&
  723. (addr < (APLIC_TARGET_BASE + (aplic->num_irqs - 1) * 4))) {
  724. irq = ((addr - APLIC_TARGET_BASE) >> 2) + 1;
  725. if (aplic->msimode) {
  726. aplic->target[irq] = value;
  727. } else {
  728. aplic->target[irq] = (value & ~APLIC_TARGET_IPRIO_MASK) |
  729. ((value & aplic->iprio_mask) ?
  730. (value & aplic->iprio_mask) : 1);
  731. }
  732. } else if (!aplic->msimode && (APLIC_IDC_BASE <= addr) &&
  733. (addr < (APLIC_IDC_BASE + aplic->num_harts * APLIC_IDC_SIZE))) {
  734. idc = (addr - APLIC_IDC_BASE) / APLIC_IDC_SIZE;
  735. switch (addr - (APLIC_IDC_BASE + idc * APLIC_IDC_SIZE)) {
  736. case APLIC_IDC_IDELIVERY:
  737. aplic->idelivery[idc] = value & 0x1;
  738. break;
  739. case APLIC_IDC_IFORCE:
  740. aplic->iforce[idc] = value & 0x1;
  741. break;
  742. case APLIC_IDC_ITHRESHOLD:
  743. aplic->ithreshold[idc] = value & aplic->iprio_mask;
  744. break;
  745. default:
  746. goto err;
  747. };
  748. } else {
  749. goto err;
  750. }
  751. if (aplic->msimode) {
  752. for (irq = 1; irq < aplic->num_irqs; irq++) {
  753. riscv_aplic_msi_irq_update(aplic, irq);
  754. }
  755. } else {
  756. if (idc == UINT32_MAX) {
  757. for (idc = 0; idc < aplic->num_harts; idc++) {
  758. riscv_aplic_idc_update(aplic, idc);
  759. }
  760. } else {
  761. riscv_aplic_idc_update(aplic, idc);
  762. }
  763. }
  764. return;
  765. err:
  766. qemu_log_mask(LOG_GUEST_ERROR,
  767. "%s: Invalid register write 0x%" HWADDR_PRIx "\n",
  768. __func__, addr);
  769. }
  770. static const MemoryRegionOps riscv_aplic_ops = {
  771. .read = riscv_aplic_read,
  772. .write = riscv_aplic_write,
  773. .endianness = DEVICE_LITTLE_ENDIAN,
  774. .valid = {
  775. .min_access_size = 4,
  776. .max_access_size = 4
  777. }
  778. };
  779. static void riscv_aplic_realize(DeviceState *dev, Error **errp)
  780. {
  781. uint32_t i;
  782. RISCVAPLICState *aplic = RISCV_APLIC(dev);
  783. if (riscv_use_emulated_aplic(aplic->msimode)) {
  784. /* Create output IRQ lines for non-MSI mode */
  785. if (!aplic->msimode) {
  786. /* Claim the CPU interrupt to be triggered by this APLIC */
  787. for (i = 0; i < aplic->num_harts; i++) {
  788. RISCVCPU *cpu;
  789. cpu = RISCV_CPU(cpu_by_arch_id(aplic->hartid_base + i));
  790. if (riscv_cpu_claim_interrupts(cpu,
  791. (aplic->mmode) ? MIP_MEIP : MIP_SEIP) < 0) {
  792. error_report("%s already claimed",
  793. (aplic->mmode) ? "MEIP" : "SEIP");
  794. exit(1);
  795. }
  796. }
  797. aplic->external_irqs = g_malloc(sizeof(qemu_irq) *
  798. aplic->num_harts);
  799. qdev_init_gpio_out(dev, aplic->external_irqs, aplic->num_harts);
  800. }
  801. aplic->bitfield_words = (aplic->num_irqs + 31) >> 5;
  802. aplic->sourcecfg = g_new0(uint32_t, aplic->num_irqs);
  803. aplic->state = g_new0(uint32_t, aplic->num_irqs);
  804. aplic->target = g_new0(uint32_t, aplic->num_irqs);
  805. if (!aplic->msimode) {
  806. for (i = 0; i < aplic->num_irqs; i++) {
  807. aplic->target[i] = 1;
  808. }
  809. }
  810. aplic->idelivery = g_new0(uint32_t, aplic->num_harts);
  811. aplic->iforce = g_new0(uint32_t, aplic->num_harts);
  812. aplic->ithreshold = g_new0(uint32_t, aplic->num_harts);
  813. memory_region_init_io(&aplic->mmio, OBJECT(dev), &riscv_aplic_ops,
  814. aplic, TYPE_RISCV_APLIC, aplic->aperture_size);
  815. sysbus_init_mmio(SYS_BUS_DEVICE(dev), &aplic->mmio);
  816. if (kvm_enabled()) {
  817. aplic->kvm_splitmode = true;
  818. }
  819. }
  820. /*
  821. * Only root APLICs have hardware IRQ lines. All non-root APLICs
  822. * have IRQ lines delegated by their parent APLIC.
  823. */
  824. if (!aplic->parent) {
  825. if (kvm_enabled() && !riscv_use_emulated_aplic(aplic->msimode)) {
  826. qdev_init_gpio_in(dev, riscv_kvm_aplic_request, aplic->num_irqs);
  827. } else {
  828. qdev_init_gpio_in(dev, riscv_aplic_request, aplic->num_irqs);
  829. }
  830. }
  831. msi_nonbroken = true;
  832. }
  833. static const Property riscv_aplic_properties[] = {
  834. DEFINE_PROP_UINT32("aperture-size", RISCVAPLICState, aperture_size, 0),
  835. DEFINE_PROP_UINT32("hartid-base", RISCVAPLICState, hartid_base, 0),
  836. DEFINE_PROP_UINT32("num-harts", RISCVAPLICState, num_harts, 0),
  837. DEFINE_PROP_UINT32("iprio-mask", RISCVAPLICState, iprio_mask, 0),
  838. DEFINE_PROP_UINT32("num-irqs", RISCVAPLICState, num_irqs, 0),
  839. DEFINE_PROP_BOOL("msimode", RISCVAPLICState, msimode, 0),
  840. DEFINE_PROP_BOOL("mmode", RISCVAPLICState, mmode, 0),
  841. };
  842. static const VMStateDescription vmstate_riscv_aplic = {
  843. .name = "riscv_aplic",
  844. .version_id = 2,
  845. .minimum_version_id = 2,
  846. .fields = (const VMStateField[]) {
  847. VMSTATE_UINT32(domaincfg, RISCVAPLICState),
  848. VMSTATE_UINT32(mmsicfgaddr, RISCVAPLICState),
  849. VMSTATE_UINT32(mmsicfgaddrH, RISCVAPLICState),
  850. VMSTATE_UINT32(smsicfgaddr, RISCVAPLICState),
  851. VMSTATE_UINT32(smsicfgaddrH, RISCVAPLICState),
  852. VMSTATE_UINT32(genmsi, RISCVAPLICState),
  853. VMSTATE_UINT32(kvm_msicfgaddr, RISCVAPLICState),
  854. VMSTATE_UINT32(kvm_msicfgaddrH, RISCVAPLICState),
  855. VMSTATE_VARRAY_UINT32(sourcecfg, RISCVAPLICState,
  856. num_irqs, 0,
  857. vmstate_info_uint32, uint32_t),
  858. VMSTATE_VARRAY_UINT32(state, RISCVAPLICState,
  859. num_irqs, 0,
  860. vmstate_info_uint32, uint32_t),
  861. VMSTATE_VARRAY_UINT32(target, RISCVAPLICState,
  862. num_irqs, 0,
  863. vmstate_info_uint32, uint32_t),
  864. VMSTATE_VARRAY_UINT32(idelivery, RISCVAPLICState,
  865. num_harts, 0,
  866. vmstate_info_uint32, uint32_t),
  867. VMSTATE_VARRAY_UINT32(iforce, RISCVAPLICState,
  868. num_harts, 0,
  869. vmstate_info_uint32, uint32_t),
  870. VMSTATE_VARRAY_UINT32(ithreshold, RISCVAPLICState,
  871. num_harts, 0,
  872. vmstate_info_uint32, uint32_t),
  873. VMSTATE_END_OF_LIST()
  874. }
  875. };
  876. static void riscv_aplic_class_init(ObjectClass *klass, void *data)
  877. {
  878. DeviceClass *dc = DEVICE_CLASS(klass);
  879. device_class_set_props(dc, riscv_aplic_properties);
  880. dc->realize = riscv_aplic_realize;
  881. dc->vmsd = &vmstate_riscv_aplic;
  882. }
  883. static const TypeInfo riscv_aplic_info = {
  884. .name = TYPE_RISCV_APLIC,
  885. .parent = TYPE_SYS_BUS_DEVICE,
  886. .instance_size = sizeof(RISCVAPLICState),
  887. .class_init = riscv_aplic_class_init,
  888. };
  889. static void riscv_aplic_register_types(void)
  890. {
  891. type_register_static(&riscv_aplic_info);
  892. }
  893. type_init(riscv_aplic_register_types)
  894. /*
  895. * Add a APLIC device to another APLIC device as child for
  896. * interrupt delegation.
  897. */
  898. void riscv_aplic_add_child(DeviceState *parent, DeviceState *child)
  899. {
  900. RISCVAPLICState *caplic, *paplic;
  901. assert(parent && child);
  902. caplic = RISCV_APLIC(child);
  903. paplic = RISCV_APLIC(parent);
  904. assert(paplic->num_irqs == caplic->num_irqs);
  905. assert(paplic->num_children <= QEMU_APLIC_MAX_CHILDREN);
  906. caplic->parent = paplic;
  907. paplic->children[paplic->num_children] = caplic;
  908. paplic->num_children++;
  909. }
  910. /*
  911. * Create APLIC device.
  912. */
  913. DeviceState *riscv_aplic_create(hwaddr addr, hwaddr size,
  914. uint32_t hartid_base, uint32_t num_harts, uint32_t num_sources,
  915. uint32_t iprio_bits, bool msimode, bool mmode, DeviceState *parent)
  916. {
  917. DeviceState *dev = qdev_new(TYPE_RISCV_APLIC);
  918. uint32_t i;
  919. assert(num_harts < APLIC_MAX_IDC);
  920. assert((APLIC_IDC_BASE + (num_harts * APLIC_IDC_SIZE)) <= size);
  921. assert(num_sources < APLIC_MAX_SOURCE);
  922. assert(APLIC_MIN_IPRIO_BITS <= iprio_bits);
  923. assert(iprio_bits <= APLIC_MAX_IPRIO_BITS);
  924. qdev_prop_set_uint32(dev, "aperture-size", size);
  925. qdev_prop_set_uint32(dev, "hartid-base", hartid_base);
  926. qdev_prop_set_uint32(dev, "num-harts", num_harts);
  927. qdev_prop_set_uint32(dev, "iprio-mask", ((1U << iprio_bits) - 1));
  928. qdev_prop_set_uint32(dev, "num-irqs", num_sources + 1);
  929. qdev_prop_set_bit(dev, "msimode", msimode);
  930. qdev_prop_set_bit(dev, "mmode", mmode);
  931. if (parent) {
  932. riscv_aplic_add_child(parent, dev);
  933. }
  934. sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
  935. if (riscv_use_emulated_aplic(msimode)) {
  936. sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, addr);
  937. if (!msimode) {
  938. for (i = 0; i < num_harts; i++) {
  939. CPUState *cpu = cpu_by_arch_id(hartid_base + i);
  940. qdev_connect_gpio_out_named(dev, NULL, i,
  941. qdev_get_gpio_in(DEVICE(cpu),
  942. (mmode) ? IRQ_M_EXT : IRQ_S_EXT));
  943. }
  944. }
  945. }
  946. return dev;
  947. }