pnv_phb3_msi.c 9.0 KB

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