pnv_phb3_msi.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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 "qemu-common.h"
  13. #include "hw/pci-host/pnv_phb3_regs.h"
  14. #include "hw/pci-host/pnv_phb3.h"
  15. #include "hw/ppc/pnv.h"
  16. #include "hw/pci/msi.h"
  17. #include "monitor/monitor.h"
  18. #include "hw/irq.h"
  19. #include "hw/qdev-properties.h"
  20. #include "sysemu/reset.h"
  21. static uint64_t phb3_msi_ive_addr(PnvPHB3 *phb, int srcno)
  22. {
  23. uint64_t ivtbar = phb->regs[PHB_IVT_BAR >> 3];
  24. uint64_t phbctl = phb->regs[PHB_CONTROL >> 3];
  25. if (!(ivtbar & PHB_IVT_BAR_ENABLE)) {
  26. qemu_log_mask(LOG_GUEST_ERROR, "Failed access to disable IVT BAR !");
  27. return 0;
  28. }
  29. if (srcno >= (ivtbar & PHB_IVT_LENGTH_MASK)) {
  30. qemu_log_mask(LOG_GUEST_ERROR, "MSI out of bounds (%d vs 0x%"PRIx64")",
  31. srcno, (uint64_t) (ivtbar & PHB_IVT_LENGTH_MASK));
  32. return 0;
  33. }
  34. ivtbar &= PHB_IVT_BASE_ADDRESS_MASK;
  35. if (phbctl & PHB_CTRL_IVE_128_BYTES) {
  36. return ivtbar + 128 * srcno;
  37. } else {
  38. return ivtbar + 16 * srcno;
  39. }
  40. }
  41. static bool phb3_msi_read_ive(PnvPHB3 *phb, int srcno, uint64_t *out_ive)
  42. {
  43. uint64_t ive_addr, ive;
  44. ive_addr = phb3_msi_ive_addr(phb, srcno);
  45. if (!ive_addr) {
  46. return false;
  47. }
  48. if (dma_memory_read(&address_space_memory, ive_addr, &ive, sizeof(ive))) {
  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, &p, 1)) {
  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, &q, 1)) {
  78. qemu_log_mask(LOG_GUEST_ERROR,
  79. "Failed to write IVE (set Q) at 0x%" PRIx64, ive_addr);
  80. }
  81. }
  82. static void phb3_msi_try_send(Phb3MsiState *msi, int srcno, bool force)
  83. {
  84. ICSState *ics = ICS(msi);
  85. uint64_t ive;
  86. uint64_t server, prio, pq, gen;
  87. if (!phb3_msi_read_ive(msi->phb, srcno, &ive)) {
  88. return;
  89. }
  90. server = GETFIELD(IODA2_IVT_SERVER, ive);
  91. prio = GETFIELD(IODA2_IVT_PRIORITY, ive);
  92. if (!force) {
  93. pq = GETFIELD(IODA2_IVT_Q, ive) | (GETFIELD(IODA2_IVT_P, ive) << 1);
  94. } else {
  95. pq = 0;
  96. }
  97. gen = GETFIELD(IODA2_IVT_GEN, ive);
  98. /*
  99. * The low order 2 bits are the link pointer (Type II interrupts).
  100. * Shift back to get a valid IRQ server.
  101. */
  102. server >>= 2;
  103. switch (pq) {
  104. case 0: /* 00 */
  105. if (prio == 0xff) {
  106. /* Masked, set Q */
  107. phb3_msi_set_q(msi, srcno);
  108. } else {
  109. /* Enabled, set P and send */
  110. phb3_msi_set_p(msi, srcno, gen);
  111. icp_irq(ics, server, srcno + ics->offset, prio);
  112. }
  113. break;
  114. case 2: /* 10 */
  115. /* Already pending, set Q */
  116. phb3_msi_set_q(msi, srcno);
  117. break;
  118. case 1: /* 01 */
  119. case 3: /* 11 */
  120. default:
  121. /* Just drop stuff if Q already set */
  122. break;
  123. }
  124. }
  125. static void phb3_msi_set_irq(void *opaque, int srcno, int val)
  126. {
  127. Phb3MsiState *msi = PHB3_MSI(opaque);
  128. if (val) {
  129. phb3_msi_try_send(msi, srcno, false);
  130. }
  131. }
  132. void pnv_phb3_msi_send(Phb3MsiState *msi, uint64_t addr, uint16_t data,
  133. int32_t dev_pe)
  134. {
  135. ICSState *ics = ICS(msi);
  136. uint64_t ive;
  137. uint16_t pe;
  138. uint32_t src = ((addr >> 4) & 0xffff) | (data & 0x1f);
  139. if (src >= ics->nr_irqs) {
  140. qemu_log_mask(LOG_GUEST_ERROR, "MSI %d out of bounds", src);
  141. return;
  142. }
  143. if (dev_pe >= 0) {
  144. if (!phb3_msi_read_ive(msi->phb, src, &ive)) {
  145. return;
  146. }
  147. pe = GETFIELD(IODA2_IVT_PE, ive);
  148. if (pe != dev_pe) {
  149. qemu_log_mask(LOG_GUEST_ERROR,
  150. "MSI %d send by PE#%d but assigned to PE#%d",
  151. src, dev_pe, pe);
  152. return;
  153. }
  154. }
  155. qemu_irq_pulse(msi->qirqs[src]);
  156. }
  157. void pnv_phb3_msi_ffi(Phb3MsiState *msi, uint64_t val)
  158. {
  159. /* Emit interrupt */
  160. pnv_phb3_msi_send(msi, val, 0, -1);
  161. /* Clear FFI lock */
  162. msi->phb->regs[PHB_FFI_LOCK >> 3] = 0;
  163. }
  164. static void phb3_msi_reject(ICSState *ics, uint32_t nr)
  165. {
  166. Phb3MsiState *msi = PHB3_MSI(ics);
  167. unsigned int srcno = nr - ics->offset;
  168. unsigned int idx = srcno >> 6;
  169. unsigned int bit = 1ull << (srcno & 0x3f);
  170. assert(srcno < PHB3_MAX_MSI);
  171. msi->rba[idx] |= bit;
  172. msi->rba_sum |= (1u << idx);
  173. }
  174. static void phb3_msi_resend(ICSState *ics)
  175. {
  176. Phb3MsiState *msi = PHB3_MSI(ics);
  177. unsigned int i, j;
  178. if (msi->rba_sum == 0) {
  179. return;
  180. }
  181. for (i = 0; i < 32; i++) {
  182. if ((msi->rba_sum & (1u << i)) == 0) {
  183. continue;
  184. }
  185. msi->rba_sum &= ~(1u << i);
  186. for (j = 0; j < 64; j++) {
  187. if ((msi->rba[i] & (1ull << j)) == 0) {
  188. continue;
  189. }
  190. msi->rba[i] &= ~(1ull << j);
  191. phb3_msi_try_send(msi, i * 64 + j, true);
  192. }
  193. }
  194. }
  195. static void phb3_msi_reset(DeviceState *dev)
  196. {
  197. Phb3MsiState *msi = PHB3_MSI(dev);
  198. ICSStateClass *icsc = ICS_GET_CLASS(dev);
  199. icsc->parent_reset(dev);
  200. memset(msi->rba, 0, sizeof(msi->rba));
  201. msi->rba_sum = 0;
  202. }
  203. static void phb3_msi_reset_handler(void *dev)
  204. {
  205. phb3_msi_reset(dev);
  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. qemu_register_reset(phb3_msi_reset_handler, dev);
  231. }
  232. static void phb3_msi_instance_init(Object *obj)
  233. {
  234. Phb3MsiState *msi = PHB3_MSI(obj);
  235. ICSState *ics = ICS(obj);
  236. object_property_add_link(obj, "phb", TYPE_PNV_PHB3,
  237. (Object **)&msi->phb,
  238. object_property_allow_set_link,
  239. OBJ_PROP_LINK_STRONG);
  240. /* Will be overriden later */
  241. ics->offset = 0;
  242. }
  243. static void phb3_msi_class_init(ObjectClass *klass, void *data)
  244. {
  245. DeviceClass *dc = DEVICE_CLASS(klass);
  246. ICSStateClass *isc = ICS_CLASS(klass);
  247. device_class_set_parent_realize(dc, phb3_msi_realize,
  248. &isc->parent_realize);
  249. device_class_set_parent_reset(dc, phb3_msi_reset,
  250. &isc->parent_reset);
  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. }