pnv_phb3_msi.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /*
  2. * QEMU PowerPC PowerNV (POWER8) PHB3 model
  3. *
  4. * Copyright (c) 2014-2020, IBM Corporation.
  5. *
  6. * This code is licensed under the GPL version 2 or later. See the
  7. * COPYING file in the top-level directory.
  8. */
  9. #include "qemu/osdep.h"
  10. #include "qemu/log.h"
  11. #include "qapi/error.h"
  12. #include "hw/pci-host/pnv_phb3_regs.h"
  13. #include "hw/pci-host/pnv_phb3.h"
  14. #include "hw/ppc/pnv.h"
  15. #include "hw/pci/msi.h"
  16. #include "monitor/monitor.h"
  17. #include "hw/irq.h"
  18. #include "hw/qdev-properties.h"
  19. #include "sysemu/reset.h"
  20. static uint64_t phb3_msi_ive_addr(PnvPHB3 *phb, int srcno)
  21. {
  22. uint64_t ivtbar = phb->regs[PHB_IVT_BAR >> 3];
  23. uint64_t phbctl = phb->regs[PHB_CONTROL >> 3];
  24. if (!(ivtbar & PHB_IVT_BAR_ENABLE)) {
  25. qemu_log_mask(LOG_GUEST_ERROR, "Failed access to disable IVT BAR !");
  26. return 0;
  27. }
  28. if (srcno >= (ivtbar & PHB_IVT_LENGTH_MASK)) {
  29. qemu_log_mask(LOG_GUEST_ERROR, "MSI out of bounds (%d vs 0x%"PRIx64")",
  30. srcno, (uint64_t) (ivtbar & PHB_IVT_LENGTH_MASK));
  31. return 0;
  32. }
  33. ivtbar &= PHB_IVT_BASE_ADDRESS_MASK;
  34. if (phbctl & PHB_CTRL_IVE_128_BYTES) {
  35. return ivtbar + 128 * srcno;
  36. } else {
  37. return ivtbar + 16 * srcno;
  38. }
  39. }
  40. static bool phb3_msi_read_ive(PnvPHB3 *phb, int srcno, uint64_t *out_ive)
  41. {
  42. uint64_t ive_addr, ive;
  43. ive_addr = phb3_msi_ive_addr(phb, srcno);
  44. if (!ive_addr) {
  45. return false;
  46. }
  47. if (dma_memory_read(&address_space_memory, ive_addr,
  48. &ive, sizeof(ive), MEMTXATTRS_UNSPECIFIED)) {
  49. qemu_log_mask(LOG_GUEST_ERROR, "Failed to read IVE at 0x%" PRIx64,
  50. ive_addr);
  51. return false;
  52. }
  53. *out_ive = be64_to_cpu(ive);
  54. return true;
  55. }
  56. static void phb3_msi_set_p(Phb3MsiState *msi, int srcno, uint8_t gen)
  57. {
  58. uint64_t ive_addr;
  59. uint8_t p = 0x01 | (gen << 1);
  60. ive_addr = phb3_msi_ive_addr(msi->phb, srcno);
  61. if (!ive_addr) {
  62. return;
  63. }
  64. if (dma_memory_write(&address_space_memory, ive_addr + 4,
  65. &p, 1, MEMTXATTRS_UNSPECIFIED)) {
  66. qemu_log_mask(LOG_GUEST_ERROR,
  67. "Failed to write IVE (set P) at 0x%" PRIx64, ive_addr);
  68. }
  69. }
  70. static void phb3_msi_set_q(Phb3MsiState *msi, int srcno)
  71. {
  72. uint64_t ive_addr;
  73. uint8_t q = 0x01;
  74. ive_addr = phb3_msi_ive_addr(msi->phb, srcno);
  75. if (!ive_addr) {
  76. return;
  77. }
  78. if (dma_memory_write(&address_space_memory, ive_addr + 5,
  79. &q, 1, MEMTXATTRS_UNSPECIFIED)) {
  80. qemu_log_mask(LOG_GUEST_ERROR,
  81. "Failed to write IVE (set Q) at 0x%" PRIx64, ive_addr);
  82. }
  83. }
  84. static void phb3_msi_try_send(Phb3MsiState *msi, int srcno, bool force)
  85. {
  86. ICSState *ics = ICS(msi);
  87. uint64_t ive;
  88. uint64_t server, prio, pq, gen;
  89. if (!phb3_msi_read_ive(msi->phb, srcno, &ive)) {
  90. return;
  91. }
  92. server = GETFIELD(IODA2_IVT_SERVER, ive);
  93. prio = GETFIELD(IODA2_IVT_PRIORITY, ive);
  94. if (!force) {
  95. pq = GETFIELD(IODA2_IVT_Q, ive) | (GETFIELD(IODA2_IVT_P, ive) << 1);
  96. } else {
  97. pq = 0;
  98. }
  99. gen = GETFIELD(IODA2_IVT_GEN, ive);
  100. /*
  101. * The low order 2 bits are the link pointer (Type II interrupts).
  102. * Shift back to get a valid IRQ server.
  103. */
  104. server >>= 2;
  105. switch (pq) {
  106. case 0: /* 00 */
  107. if (prio == 0xff) {
  108. /* Masked, set Q */
  109. phb3_msi_set_q(msi, srcno);
  110. } else {
  111. /* Enabled, set P and send */
  112. phb3_msi_set_p(msi, srcno, gen);
  113. icp_irq(ics, server, srcno + ics->offset, prio);
  114. }
  115. break;
  116. case 2: /* 10 */
  117. /* Already pending, set Q */
  118. phb3_msi_set_q(msi, srcno);
  119. break;
  120. case 1: /* 01 */
  121. case 3: /* 11 */
  122. default:
  123. /* Just drop stuff if Q already set */
  124. break;
  125. }
  126. }
  127. static void phb3_msi_set_irq(void *opaque, int srcno, int val)
  128. {
  129. Phb3MsiState *msi = PHB3_MSI(opaque);
  130. if (val) {
  131. phb3_msi_try_send(msi, srcno, false);
  132. }
  133. }
  134. void pnv_phb3_msi_send(Phb3MsiState *msi, uint64_t addr, uint16_t data,
  135. int32_t dev_pe)
  136. {
  137. ICSState *ics = ICS(msi);
  138. uint64_t ive;
  139. uint16_t pe;
  140. uint32_t src = ((addr >> 4) & 0xffff) | (data & 0x1f);
  141. if (src >= ics->nr_irqs) {
  142. qemu_log_mask(LOG_GUEST_ERROR, "MSI %d out of bounds", src);
  143. return;
  144. }
  145. if (dev_pe >= 0) {
  146. if (!phb3_msi_read_ive(msi->phb, src, &ive)) {
  147. return;
  148. }
  149. pe = GETFIELD(IODA2_IVT_PE, ive);
  150. if (pe != dev_pe) {
  151. qemu_log_mask(LOG_GUEST_ERROR,
  152. "MSI %d send by PE#%d but assigned to PE#%d",
  153. src, dev_pe, pe);
  154. return;
  155. }
  156. }
  157. qemu_irq_pulse(msi->qirqs[src]);
  158. }
  159. void pnv_phb3_msi_ffi(Phb3MsiState *msi, uint64_t val)
  160. {
  161. /* Emit interrupt */
  162. pnv_phb3_msi_send(msi, val, 0, -1);
  163. /* Clear FFI lock */
  164. msi->phb->regs[PHB_FFI_LOCK >> 3] = 0;
  165. }
  166. static void phb3_msi_reject(ICSState *ics, uint32_t nr)
  167. {
  168. Phb3MsiState *msi = PHB3_MSI(ics);
  169. unsigned int srcno = nr - ics->offset;
  170. unsigned int idx = srcno >> 6;
  171. unsigned int bit = 1ull << (srcno & 0x3f);
  172. assert(srcno < PHB3_MAX_MSI);
  173. msi->rba[idx] |= bit;
  174. msi->rba_sum |= (1u << idx);
  175. }
  176. static void phb3_msi_resend(ICSState *ics)
  177. {
  178. Phb3MsiState *msi = PHB3_MSI(ics);
  179. unsigned int i, j;
  180. if (msi->rba_sum == 0) {
  181. return;
  182. }
  183. for (i = 0; i < 32; i++) {
  184. if ((msi->rba_sum & (1u << i)) == 0) {
  185. continue;
  186. }
  187. msi->rba_sum &= ~(1u << i);
  188. for (j = 0; j < 64; j++) {
  189. if ((msi->rba[i] & (1ull << j)) == 0) {
  190. continue;
  191. }
  192. msi->rba[i] &= ~(1ull << j);
  193. phb3_msi_try_send(msi, i * 64 + j, true);
  194. }
  195. }
  196. }
  197. static void phb3_msi_reset_hold(Object *obj)
  198. {
  199. Phb3MsiState *msi = PHB3_MSI(obj);
  200. ICSStateClass *icsc = ICS_GET_CLASS(obj);
  201. if (icsc->parent_phases.hold) {
  202. icsc->parent_phases.hold(obj);
  203. }
  204. memset(msi->rba, 0, sizeof(msi->rba));
  205. msi->rba_sum = 0;
  206. }
  207. void pnv_phb3_msi_update_config(Phb3MsiState *msi, uint32_t base,
  208. uint32_t count)
  209. {
  210. ICSState *ics = ICS(msi);
  211. if (count > PHB3_MAX_MSI) {
  212. count = PHB3_MAX_MSI;
  213. }
  214. ics->nr_irqs = count;
  215. ics->offset = base;
  216. }
  217. static void phb3_msi_realize(DeviceState *dev, Error **errp)
  218. {
  219. Phb3MsiState *msi = PHB3_MSI(dev);
  220. ICSState *ics = ICS(msi);
  221. ICSStateClass *icsc = ICS_GET_CLASS(ics);
  222. Error *local_err = NULL;
  223. assert(msi->phb);
  224. icsc->parent_realize(dev, &local_err);
  225. if (local_err) {
  226. error_propagate(errp, local_err);
  227. return;
  228. }
  229. msi->qirqs = qemu_allocate_irqs(phb3_msi_set_irq, msi, ics->nr_irqs);
  230. }
  231. static void phb3_msi_instance_init(Object *obj)
  232. {
  233. Phb3MsiState *msi = PHB3_MSI(obj);
  234. ICSState *ics = ICS(obj);
  235. object_property_add_link(obj, "phb", TYPE_PNV_PHB3,
  236. (Object **)&msi->phb,
  237. object_property_allow_set_link,
  238. OBJ_PROP_LINK_STRONG);
  239. /* Will be overriden later */
  240. ics->offset = 0;
  241. }
  242. static void phb3_msi_class_init(ObjectClass *klass, void *data)
  243. {
  244. DeviceClass *dc = DEVICE_CLASS(klass);
  245. ICSStateClass *isc = ICS_CLASS(klass);
  246. ResettableClass *rc = RESETTABLE_CLASS(klass);
  247. device_class_set_parent_realize(dc, phb3_msi_realize,
  248. &isc->parent_realize);
  249. resettable_class_set_parent_phases(rc, NULL, phb3_msi_reset_hold, NULL,
  250. &isc->parent_phases);
  251. isc->reject = phb3_msi_reject;
  252. isc->resend = phb3_msi_resend;
  253. }
  254. static const TypeInfo phb3_msi_info = {
  255. .name = TYPE_PHB3_MSI,
  256. .parent = TYPE_ICS,
  257. .instance_size = sizeof(Phb3MsiState),
  258. .class_init = phb3_msi_class_init,
  259. .class_size = sizeof(ICSStateClass),
  260. .instance_init = phb3_msi_instance_init,
  261. };
  262. static void pnv_phb3_msi_register_types(void)
  263. {
  264. type_register_static(&phb3_msi_info);
  265. }
  266. type_init(pnv_phb3_msi_register_types);
  267. void pnv_phb3_msi_pic_print_info(Phb3MsiState *msi, Monitor *mon)
  268. {
  269. ICSState *ics = ICS(msi);
  270. int i;
  271. monitor_printf(mon, "ICS %4x..%4x %p\n",
  272. ics->offset, ics->offset + ics->nr_irqs - 1, ics);
  273. for (i = 0; i < ics->nr_irqs; i++) {
  274. uint64_t ive;
  275. if (!phb3_msi_read_ive(msi->phb, i, &ive)) {
  276. return;
  277. }
  278. if (GETFIELD(IODA2_IVT_PRIORITY, ive) == 0xff) {
  279. continue;
  280. }
  281. monitor_printf(mon, " %4x %c%c server=%04x prio=%02x gen=%d\n",
  282. ics->offset + i,
  283. GETFIELD(IODA2_IVT_P, ive) ? 'P' : '-',
  284. GETFIELD(IODA2_IVT_Q, ive) ? 'Q' : '-',
  285. (uint32_t) GETFIELD(IODA2_IVT_SERVER, ive) >> 2,
  286. (uint32_t) GETFIELD(IODA2_IVT_PRIORITY, ive),
  287. (uint32_t) GETFIELD(IODA2_IVT_GEN, ive));
  288. }
  289. }