riscv_imsic.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. /*
  2. * RISC-V IMSIC (Incoming Message Signaled 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_imsic.h"
  30. #include "hw/irq.h"
  31. #include "target/riscv/cpu.h"
  32. #include "target/riscv/cpu_bits.h"
  33. #include "system/system.h"
  34. #include "system/kvm.h"
  35. #include "migration/vmstate.h"
  36. #define IMSIC_MMIO_PAGE_LE 0x00
  37. #define IMSIC_MMIO_PAGE_BE 0x04
  38. #define IMSIC_MIN_ID ((IMSIC_EIPx_BITS * 2) - 1)
  39. #define IMSIC_MAX_ID (IMSIC_TOPEI_IID_MASK)
  40. #define IMSIC_EISTATE_PENDING (1U << 0)
  41. #define IMSIC_EISTATE_ENABLED (1U << 1)
  42. #define IMSIC_EISTATE_ENPEND (IMSIC_EISTATE_ENABLED | \
  43. IMSIC_EISTATE_PENDING)
  44. static uint32_t riscv_imsic_topei(RISCVIMSICState *imsic, uint32_t page)
  45. {
  46. uint32_t i, max_irq, base;
  47. base = page * imsic->num_irqs;
  48. max_irq = (imsic->eithreshold[page] &&
  49. (imsic->eithreshold[page] <= imsic->num_irqs)) ?
  50. imsic->eithreshold[page] : imsic->num_irqs;
  51. for (i = 1; i < max_irq; i++) {
  52. if ((qatomic_read(&imsic->eistate[base + i]) & IMSIC_EISTATE_ENPEND) ==
  53. IMSIC_EISTATE_ENPEND) {
  54. return (i << IMSIC_TOPEI_IID_SHIFT) | i;
  55. }
  56. }
  57. return 0;
  58. }
  59. static void riscv_imsic_update(RISCVIMSICState *imsic, uint32_t page)
  60. {
  61. uint32_t base = page * imsic->num_irqs;
  62. /*
  63. * Lower the interrupt line if necessary, then evaluate the current
  64. * IMSIC state.
  65. * This sequence ensures that any race between evaluating the eistate and
  66. * updating the interrupt line will not result in an incorrectly
  67. * deactivated connected CPU IRQ line.
  68. * If multiple interrupts are pending, this sequence functions identically
  69. * to qemu_irq_pulse.
  70. */
  71. if (qatomic_fetch_and(&imsic->eistate[base], ~IMSIC_EISTATE_ENPEND)) {
  72. qemu_irq_lower(imsic->external_irqs[page]);
  73. }
  74. if (imsic->eidelivery[page] && riscv_imsic_topei(imsic, page)) {
  75. qemu_irq_raise(imsic->external_irqs[page]);
  76. qatomic_or(&imsic->eistate[base], IMSIC_EISTATE_ENPEND);
  77. }
  78. }
  79. static int riscv_imsic_eidelivery_rmw(RISCVIMSICState *imsic, uint32_t page,
  80. target_ulong *val,
  81. target_ulong new_val,
  82. target_ulong wr_mask)
  83. {
  84. target_ulong old_val = imsic->eidelivery[page];
  85. if (val) {
  86. *val = old_val;
  87. }
  88. wr_mask &= 0x1;
  89. imsic->eidelivery[page] = (old_val & ~wr_mask) | (new_val & wr_mask);
  90. riscv_imsic_update(imsic, page);
  91. return 0;
  92. }
  93. static int riscv_imsic_eithreshold_rmw(RISCVIMSICState *imsic, uint32_t page,
  94. target_ulong *val,
  95. target_ulong new_val,
  96. target_ulong wr_mask)
  97. {
  98. target_ulong old_val = imsic->eithreshold[page];
  99. if (val) {
  100. *val = old_val;
  101. }
  102. wr_mask &= IMSIC_MAX_ID;
  103. imsic->eithreshold[page] = (old_val & ~wr_mask) | (new_val & wr_mask);
  104. riscv_imsic_update(imsic, page);
  105. return 0;
  106. }
  107. static int riscv_imsic_topei_rmw(RISCVIMSICState *imsic, uint32_t page,
  108. target_ulong *val, target_ulong new_val,
  109. target_ulong wr_mask)
  110. {
  111. uint32_t base, topei = riscv_imsic_topei(imsic, page);
  112. /* Read pending and enabled interrupt with highest priority */
  113. if (val) {
  114. *val = topei;
  115. }
  116. /* Writes ignore value and clear top pending interrupt */
  117. if (topei && wr_mask) {
  118. topei >>= IMSIC_TOPEI_IID_SHIFT;
  119. base = page * imsic->num_irqs;
  120. if (topei) {
  121. qatomic_and(&imsic->eistate[base + topei], ~IMSIC_EISTATE_PENDING);
  122. }
  123. }
  124. riscv_imsic_update(imsic, page);
  125. return 0;
  126. }
  127. static int riscv_imsic_eix_rmw(RISCVIMSICState *imsic,
  128. uint32_t xlen, uint32_t page,
  129. uint32_t num, bool pend, target_ulong *val,
  130. target_ulong new_val, target_ulong wr_mask)
  131. {
  132. uint32_t i, base, prev;
  133. target_ulong mask;
  134. uint32_t state = (pend) ? IMSIC_EISTATE_PENDING : IMSIC_EISTATE_ENABLED;
  135. if (xlen != 32) {
  136. if (num & 0x1) {
  137. return -EINVAL;
  138. }
  139. num >>= 1;
  140. }
  141. if (num >= (imsic->num_irqs / xlen)) {
  142. return -EINVAL;
  143. }
  144. base = (page * imsic->num_irqs) + (num * xlen);
  145. if (val) {
  146. *val = 0;
  147. }
  148. for (i = 0; i < xlen; i++) {
  149. /* Bit0 of eip0 and eie0 are read-only zero */
  150. if (!num && !i) {
  151. continue;
  152. }
  153. mask = (target_ulong)1 << i;
  154. if (wr_mask & mask) {
  155. if (new_val & mask) {
  156. prev = qatomic_fetch_or(&imsic->eistate[base + i], state);
  157. } else {
  158. prev = qatomic_fetch_and(&imsic->eistate[base + i], ~state);
  159. }
  160. } else {
  161. prev = qatomic_read(&imsic->eistate[base + i]);
  162. }
  163. if (val && (prev & state)) {
  164. *val |= mask;
  165. }
  166. }
  167. riscv_imsic_update(imsic, page);
  168. return 0;
  169. }
  170. static int riscv_imsic_rmw(void *arg, target_ulong reg, target_ulong *val,
  171. target_ulong new_val, target_ulong wr_mask)
  172. {
  173. RISCVIMSICState *imsic = arg;
  174. uint32_t isel, priv, virt, vgein, xlen, page;
  175. priv = AIA_IREG_PRIV(reg);
  176. virt = AIA_IREG_VIRT(reg);
  177. isel = AIA_IREG_ISEL(reg);
  178. vgein = AIA_IREG_VGEIN(reg);
  179. xlen = AIA_IREG_XLEN(reg);
  180. if (imsic->mmode) {
  181. if (priv == PRV_M && !virt) {
  182. page = 0;
  183. } else {
  184. goto err;
  185. }
  186. } else {
  187. if (priv == PRV_S) {
  188. if (virt) {
  189. if (vgein && vgein < imsic->num_pages) {
  190. page = vgein;
  191. } else {
  192. goto err;
  193. }
  194. } else {
  195. page = 0;
  196. }
  197. } else {
  198. goto err;
  199. }
  200. }
  201. switch (isel) {
  202. case ISELECT_IMSIC_EIDELIVERY:
  203. return riscv_imsic_eidelivery_rmw(imsic, page, val,
  204. new_val, wr_mask);
  205. case ISELECT_IMSIC_EITHRESHOLD:
  206. return riscv_imsic_eithreshold_rmw(imsic, page, val,
  207. new_val, wr_mask);
  208. case ISELECT_IMSIC_TOPEI:
  209. return riscv_imsic_topei_rmw(imsic, page, val, new_val, wr_mask);
  210. case ISELECT_IMSIC_EIP0 ... ISELECT_IMSIC_EIP63:
  211. return riscv_imsic_eix_rmw(imsic, xlen, page,
  212. isel - ISELECT_IMSIC_EIP0,
  213. true, val, new_val, wr_mask);
  214. case ISELECT_IMSIC_EIE0 ... ISELECT_IMSIC_EIE63:
  215. return riscv_imsic_eix_rmw(imsic, xlen, page,
  216. isel - ISELECT_IMSIC_EIE0,
  217. false, val, new_val, wr_mask);
  218. default:
  219. break;
  220. };
  221. err:
  222. qemu_log_mask(LOG_GUEST_ERROR,
  223. "%s: Invalid register priv=%d virt=%d isel=%d vgein=%d\n",
  224. __func__, priv, virt, isel, vgein);
  225. return -EINVAL;
  226. }
  227. static uint64_t riscv_imsic_read(void *opaque, hwaddr addr, unsigned size)
  228. {
  229. RISCVIMSICState *imsic = opaque;
  230. /* Reads must be 4 byte words */
  231. if ((addr & 0x3) != 0) {
  232. goto err;
  233. }
  234. /* Reads cannot be out of range */
  235. if (addr > IMSIC_MMIO_SIZE(imsic->num_pages)) {
  236. goto err;
  237. }
  238. return 0;
  239. err:
  240. qemu_log_mask(LOG_GUEST_ERROR,
  241. "%s: Invalid register read 0x%" HWADDR_PRIx "\n",
  242. __func__, addr);
  243. return 0;
  244. }
  245. static void riscv_imsic_write(void *opaque, hwaddr addr, uint64_t value,
  246. unsigned size)
  247. {
  248. RISCVIMSICState *imsic = opaque;
  249. uint32_t page;
  250. /* Writes must be 4 byte words */
  251. if ((addr & 0x3) != 0) {
  252. goto err;
  253. }
  254. /* Writes cannot be out of range */
  255. if (addr > IMSIC_MMIO_SIZE(imsic->num_pages)) {
  256. goto err;
  257. }
  258. #if defined(CONFIG_KVM)
  259. if (kvm_irqchip_in_kernel()) {
  260. struct kvm_msi msi;
  261. msi.address_lo = extract64(imsic->mmio.addr + addr, 0, 32);
  262. msi.address_hi = extract64(imsic->mmio.addr + addr, 32, 32);
  263. msi.data = le32_to_cpu(value);
  264. kvm_vm_ioctl(kvm_state, KVM_SIGNAL_MSI, &msi);
  265. return;
  266. }
  267. #endif
  268. /* Writes only supported for MSI little-endian registers */
  269. page = addr >> IMSIC_MMIO_PAGE_SHIFT;
  270. if ((addr & (IMSIC_MMIO_PAGE_SZ - 1)) == IMSIC_MMIO_PAGE_LE) {
  271. if (value && (value < imsic->num_irqs)) {
  272. qatomic_or(&imsic->eistate[(page * imsic->num_irqs) + value],
  273. IMSIC_EISTATE_PENDING);
  274. /* Update CPU external interrupt status */
  275. riscv_imsic_update(imsic, page);
  276. }
  277. }
  278. return;
  279. err:
  280. qemu_log_mask(LOG_GUEST_ERROR,
  281. "%s: Invalid register write 0x%" HWADDR_PRIx "\n",
  282. __func__, addr);
  283. }
  284. static const MemoryRegionOps riscv_imsic_ops = {
  285. .read = riscv_imsic_read,
  286. .write = riscv_imsic_write,
  287. .endianness = DEVICE_LITTLE_ENDIAN,
  288. .valid = {
  289. .min_access_size = 4,
  290. .max_access_size = 4
  291. }
  292. };
  293. static void riscv_imsic_realize(DeviceState *dev, Error **errp)
  294. {
  295. RISCVIMSICState *imsic = RISCV_IMSIC(dev);
  296. RISCVCPU *rcpu = RISCV_CPU(cpu_by_arch_id(imsic->hartid));
  297. CPUState *cpu = cpu_by_arch_id(imsic->hartid);
  298. CPURISCVState *env = cpu ? cpu_env(cpu) : NULL;
  299. if (!kvm_irqchip_in_kernel()) {
  300. imsic->num_eistate = imsic->num_pages * imsic->num_irqs;
  301. imsic->eidelivery = g_new0(uint32_t, imsic->num_pages);
  302. imsic->eithreshold = g_new0(uint32_t, imsic->num_pages);
  303. imsic->eistate = g_new0(uint32_t, imsic->num_eistate);
  304. }
  305. memory_region_init_io(&imsic->mmio, OBJECT(dev), &riscv_imsic_ops,
  306. imsic, TYPE_RISCV_IMSIC,
  307. IMSIC_MMIO_SIZE(imsic->num_pages));
  308. sysbus_init_mmio(SYS_BUS_DEVICE(dev), &imsic->mmio);
  309. /* Claim the CPU interrupt to be triggered by this IMSIC */
  310. if (riscv_cpu_claim_interrupts(rcpu,
  311. (imsic->mmode) ? MIP_MEIP : MIP_SEIP) < 0) {
  312. error_setg(errp, "%s already claimed",
  313. (imsic->mmode) ? "MEIP" : "SEIP");
  314. return;
  315. }
  316. /* Create output IRQ lines */
  317. imsic->external_irqs = g_malloc(sizeof(qemu_irq) * imsic->num_pages);
  318. qdev_init_gpio_out(dev, imsic->external_irqs, imsic->num_pages);
  319. /* Force select AIA feature and setup CSR read-modify-write callback */
  320. if (env) {
  321. if (!imsic->mmode) {
  322. rcpu->cfg.ext_ssaia = true;
  323. riscv_cpu_set_geilen(env, imsic->num_pages - 1);
  324. } else {
  325. rcpu->cfg.ext_smaia = true;
  326. }
  327. riscv_cpu_set_aia_ireg_rmw_fn(env, (imsic->mmode) ? PRV_M : PRV_S,
  328. riscv_imsic_rmw, imsic);
  329. }
  330. msi_nonbroken = true;
  331. }
  332. static const Property riscv_imsic_properties[] = {
  333. DEFINE_PROP_BOOL("mmode", RISCVIMSICState, mmode, 0),
  334. DEFINE_PROP_UINT32("hartid", RISCVIMSICState, hartid, 0),
  335. DEFINE_PROP_UINT32("num-pages", RISCVIMSICState, num_pages, 0),
  336. DEFINE_PROP_UINT32("num-irqs", RISCVIMSICState, num_irqs, 0),
  337. };
  338. static const VMStateDescription vmstate_riscv_imsic = {
  339. .name = "riscv_imsic",
  340. .version_id = 1,
  341. .minimum_version_id = 1,
  342. .fields = (const VMStateField[]) {
  343. VMSTATE_VARRAY_UINT32(eidelivery, RISCVIMSICState,
  344. num_pages, 0,
  345. vmstate_info_uint32, uint32_t),
  346. VMSTATE_VARRAY_UINT32(eithreshold, RISCVIMSICState,
  347. num_pages, 0,
  348. vmstate_info_uint32, uint32_t),
  349. VMSTATE_VARRAY_UINT32(eistate, RISCVIMSICState,
  350. num_eistate, 0,
  351. vmstate_info_uint32, uint32_t),
  352. VMSTATE_END_OF_LIST()
  353. }
  354. };
  355. static void riscv_imsic_class_init(ObjectClass *klass, void *data)
  356. {
  357. DeviceClass *dc = DEVICE_CLASS(klass);
  358. device_class_set_props(dc, riscv_imsic_properties);
  359. dc->realize = riscv_imsic_realize;
  360. dc->vmsd = &vmstate_riscv_imsic;
  361. }
  362. static const TypeInfo riscv_imsic_info = {
  363. .name = TYPE_RISCV_IMSIC,
  364. .parent = TYPE_SYS_BUS_DEVICE,
  365. .instance_size = sizeof(RISCVIMSICState),
  366. .class_init = riscv_imsic_class_init,
  367. };
  368. static void riscv_imsic_register_types(void)
  369. {
  370. type_register_static(&riscv_imsic_info);
  371. }
  372. type_init(riscv_imsic_register_types)
  373. /*
  374. * Create IMSIC device.
  375. */
  376. DeviceState *riscv_imsic_create(hwaddr addr, uint32_t hartid, bool mmode,
  377. uint32_t num_pages, uint32_t num_ids)
  378. {
  379. DeviceState *dev = qdev_new(TYPE_RISCV_IMSIC);
  380. CPUState *cpu = cpu_by_arch_id(hartid);
  381. uint32_t i;
  382. assert(!(addr & (IMSIC_MMIO_PAGE_SZ - 1)));
  383. if (mmode) {
  384. assert(num_pages == 1);
  385. } else {
  386. assert(num_pages >= 1 && num_pages <= (IRQ_LOCAL_GUEST_MAX + 1));
  387. }
  388. assert(IMSIC_MIN_ID <= num_ids);
  389. assert(num_ids <= IMSIC_MAX_ID);
  390. assert((num_ids & IMSIC_MIN_ID) == IMSIC_MIN_ID);
  391. qdev_prop_set_bit(dev, "mmode", mmode);
  392. qdev_prop_set_uint32(dev, "hartid", hartid);
  393. qdev_prop_set_uint32(dev, "num-pages", num_pages);
  394. qdev_prop_set_uint32(dev, "num-irqs", num_ids + 1);
  395. sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
  396. sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, addr);
  397. for (i = 0; i < num_pages; i++) {
  398. if (!i) {
  399. qdev_connect_gpio_out_named(dev, NULL, i,
  400. qdev_get_gpio_in(DEVICE(cpu),
  401. (mmode) ? IRQ_M_EXT : IRQ_S_EXT));
  402. } else {
  403. qdev_connect_gpio_out_named(dev, NULL, i,
  404. qdev_get_gpio_in(DEVICE(cpu),
  405. IRQ_LOCAL_MAX + i - 1));
  406. }
  407. }
  408. return dev;
  409. }