pnv_adu.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. * QEMU PowerPC PowerNV ADU unit
  3. *
  4. * The ADU unit actually implements XSCOM, which is the bridge between MMIO
  5. * and PIB. However it also includes control and status registers and other
  6. * functions that are exposed as PIB (xscom) registers.
  7. *
  8. * To keep things simple, pnv_xscom.c remains the XSCOM bridge
  9. * implementation, and pnv_adu.c implements the ADU registers and other
  10. * functions.
  11. *
  12. * Copyright (c) 2024, IBM Corporation.
  13. *
  14. * SPDX-License-Identifier: GPL-2.0-or-later
  15. */
  16. #include "qemu/osdep.h"
  17. #include "qemu/log.h"
  18. #include "hw/qdev-properties.h"
  19. #include "hw/ppc/pnv.h"
  20. #include "hw/ppc/pnv_adu.h"
  21. #include "hw/ppc/pnv_chip.h"
  22. #include "hw/ppc/pnv_lpc.h"
  23. #include "hw/ppc/pnv_xscom.h"
  24. #include "trace.h"
  25. #define ADU_LPC_BASE_REG 0x40
  26. #define ADU_LPC_CMD_REG 0x41
  27. #define ADU_LPC_DATA_REG 0x42
  28. #define ADU_LPC_STATUS_REG 0x43
  29. static uint64_t pnv_adu_xscom_read(void *opaque, hwaddr addr, unsigned width)
  30. {
  31. PnvADU *adu = PNV_ADU(opaque);
  32. uint32_t offset = addr >> 3;
  33. uint64_t val = 0;
  34. switch (offset) {
  35. case 0x18: /* Receive status reg */
  36. case 0x12: /* log register */
  37. case 0x13: /* error register */
  38. break;
  39. case ADU_LPC_BASE_REG:
  40. /*
  41. * LPC Address Map in Pervasive ADU Workbook
  42. *
  43. * return PNV10_LPCM_BASE(chip) & PPC_BITMASK(8, 31);
  44. * XXX: implement as class property, or get from LPC?
  45. */
  46. qemu_log_mask(LOG_UNIMP, "ADU: LPC_BASE_REG is not implemented\n");
  47. break;
  48. case ADU_LPC_CMD_REG:
  49. val = adu->lpc_cmd_reg;
  50. break;
  51. case ADU_LPC_DATA_REG:
  52. val = adu->lpc_data_reg;
  53. break;
  54. case ADU_LPC_STATUS_REG:
  55. val = PPC_BIT(0); /* ack / done */
  56. break;
  57. default:
  58. qemu_log_mask(LOG_UNIMP, "ADU Unimplemented read register: Ox%08x\n",
  59. offset);
  60. }
  61. trace_pnv_adu_xscom_read(addr, val);
  62. return val;
  63. }
  64. static bool lpc_cmd_read(PnvADU *adu)
  65. {
  66. return !!(adu->lpc_cmd_reg & PPC_BIT(0));
  67. }
  68. static bool lpc_cmd_write(PnvADU *adu)
  69. {
  70. return !lpc_cmd_read(adu);
  71. }
  72. static uint32_t lpc_cmd_addr(PnvADU *adu)
  73. {
  74. return (adu->lpc_cmd_reg & PPC_BITMASK(32, 63)) >> PPC_BIT_NR(63);
  75. }
  76. static uint32_t lpc_cmd_size(PnvADU *adu)
  77. {
  78. return (adu->lpc_cmd_reg & PPC_BITMASK(5, 11)) >> PPC_BIT_NR(11);
  79. }
  80. static void pnv_adu_xscom_write(void *opaque, hwaddr addr, uint64_t val,
  81. unsigned width)
  82. {
  83. PnvADU *adu = PNV_ADU(opaque);
  84. uint32_t offset = addr >> 3;
  85. trace_pnv_adu_xscom_write(addr, val);
  86. switch (offset) {
  87. case 0x18: /* Receive status reg */
  88. case 0x12: /* log register */
  89. case 0x13: /* error register */
  90. break;
  91. case ADU_LPC_BASE_REG:
  92. qemu_log_mask(LOG_UNIMP,
  93. "ADU: Changing LPC_BASE_REG is not implemented\n");
  94. break;
  95. case ADU_LPC_CMD_REG:
  96. adu->lpc_cmd_reg = val;
  97. if (lpc_cmd_read(adu)) {
  98. uint32_t lpc_addr = lpc_cmd_addr(adu);
  99. uint32_t lpc_size = lpc_cmd_size(adu);
  100. uint64_t data = 0;
  101. if (!is_power_of_2(lpc_size) || lpc_size > sizeof(data)) {
  102. qemu_log_mask(LOG_GUEST_ERROR, "ADU: Unsupported LPC access "
  103. "size:%" PRId32 "\n", lpc_size);
  104. break;
  105. }
  106. pnv_lpc_opb_read(adu->lpc, lpc_addr, (void *)&data, lpc_size);
  107. /*
  108. * ADU access is performed within 8-byte aligned sectors. Smaller
  109. * access sizes don't get formatted to the least significant byte,
  110. * but rather appear in the data reg at the same offset as the
  111. * address in memory. This shifts them into that position.
  112. */
  113. adu->lpc_data_reg = be64_to_cpu(data) >> ((lpc_addr & 7) * 8);
  114. }
  115. break;
  116. case ADU_LPC_DATA_REG:
  117. adu->lpc_data_reg = val;
  118. if (lpc_cmd_write(adu)) {
  119. uint32_t lpc_addr = lpc_cmd_addr(adu);
  120. uint32_t lpc_size = lpc_cmd_size(adu);
  121. uint64_t data;
  122. if (!is_power_of_2(lpc_size) || lpc_size > sizeof(data)) {
  123. qemu_log_mask(LOG_GUEST_ERROR, "ADU: Unsupported LPC access "
  124. "size:%" PRId32 "\n", lpc_size);
  125. break;
  126. }
  127. data = cpu_to_be64(val) >> ((lpc_addr & 7) * 8); /* See above */
  128. pnv_lpc_opb_write(adu->lpc, lpc_addr, (void *)&data, lpc_size);
  129. }
  130. break;
  131. case ADU_LPC_STATUS_REG:
  132. qemu_log_mask(LOG_UNIMP,
  133. "ADU: Changing LPC_STATUS_REG is not implemented\n");
  134. break;
  135. default:
  136. qemu_log_mask(LOG_UNIMP, "ADU Unimplemented write register: Ox%08x\n",
  137. offset);
  138. }
  139. }
  140. const MemoryRegionOps pnv_adu_xscom_ops = {
  141. .read = pnv_adu_xscom_read,
  142. .write = pnv_adu_xscom_write,
  143. .valid.min_access_size = 8,
  144. .valid.max_access_size = 8,
  145. .impl.min_access_size = 8,
  146. .impl.max_access_size = 8,
  147. .endianness = DEVICE_BIG_ENDIAN,
  148. };
  149. static void pnv_adu_realize(DeviceState *dev, Error **errp)
  150. {
  151. PnvADU *adu = PNV_ADU(dev);
  152. assert(adu->lpc);
  153. /* XScom regions for ADU registers */
  154. pnv_xscom_region_init(&adu->xscom_regs, OBJECT(dev),
  155. &pnv_adu_xscom_ops, adu, "xscom-adu",
  156. PNV9_XSCOM_ADU_SIZE);
  157. }
  158. static const Property pnv_adu_properties[] = {
  159. DEFINE_PROP_LINK("lpc", PnvADU, lpc, TYPE_PNV_LPC, PnvLpcController *),
  160. };
  161. static void pnv_adu_class_init(ObjectClass *klass, void *data)
  162. {
  163. DeviceClass *dc = DEVICE_CLASS(klass);
  164. dc->realize = pnv_adu_realize;
  165. dc->desc = "PowerNV ADU";
  166. device_class_set_props(dc, pnv_adu_properties);
  167. dc->user_creatable = false;
  168. }
  169. static const TypeInfo pnv_adu_type_info = {
  170. .name = TYPE_PNV_ADU,
  171. .parent = TYPE_DEVICE,
  172. .instance_size = sizeof(PnvADU),
  173. .class_init = pnv_adu_class_init,
  174. .interfaces = (InterfaceInfo[]) {
  175. { TYPE_PNV_XSCOM_INTERFACE },
  176. { } },
  177. };
  178. static void pnv_adu_register_types(void)
  179. {
  180. type_register_static(&pnv_adu_type_info);
  181. }
  182. type_init(pnv_adu_register_types);