riscv_imsic.c 14 KB

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