pnv_phb4_pec.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. /*
  2. * QEMU PowerPC PowerNV (POWER9) PHB4 model
  3. *
  4. * Copyright (c) 2018-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 "qapi/error.h"
  11. #include "qemu/log.h"
  12. #include "target/ppc/cpu.h"
  13. #include "hw/ppc/fdt.h"
  14. #include "hw/pci-host/pnv_phb4_regs.h"
  15. #include "hw/pci-host/pnv_phb4.h"
  16. #include "hw/ppc/pnv_xscom.h"
  17. #include "hw/pci/pci_bridge.h"
  18. #include "hw/pci/pci_bus.h"
  19. #include "hw/ppc/pnv.h"
  20. #include "hw/ppc/pnv_chip.h"
  21. #include "hw/qdev-properties.h"
  22. #include "system/system.h"
  23. #include <libfdt.h>
  24. #define phb_pec_error(pec, fmt, ...) \
  25. qemu_log_mask(LOG_GUEST_ERROR, "phb4_pec[%d:%d]: " fmt "\n", \
  26. (pec)->chip_id, (pec)->index, ## __VA_ARGS__)
  27. static uint64_t pnv_pec_nest_xscom_read(void *opaque, hwaddr addr,
  28. unsigned size)
  29. {
  30. PnvPhb4PecState *pec = PNV_PHB4_PEC(opaque);
  31. uint32_t reg = addr >> 3;
  32. /* All registers are readable */
  33. return pec->nest_regs[reg];
  34. }
  35. static void pnv_pec_nest_xscom_write(void *opaque, hwaddr addr,
  36. uint64_t val, unsigned size)
  37. {
  38. PnvPhb4PecState *pec = PNV_PHB4_PEC(opaque);
  39. uint32_t reg = addr >> 3;
  40. switch (reg) {
  41. case PEC_NEST_DROP_PRIO_CTRL:
  42. pec->nest_regs[reg] = val & PPC_BITMASK(0, 25);
  43. break;
  44. case PEC_NEST_PBCQ_ERR_INJECT:
  45. pec->nest_regs[reg] = val & PPC_BITMASK(0, 11);
  46. break;
  47. case PEC_NEST_PCI_NEST_CLK_TRACE_CTL:
  48. pec->nest_regs[reg] = val & PPC_BITMASK(0, 16);
  49. break;
  50. case PEC_NEST_PBCQ_PMON_CTRL:
  51. pec->nest_regs[reg] = val & PPC_BITMASK(0, 37);
  52. break;
  53. case PEC_NEST_PBCQ_PBUS_ADDR_EXT:
  54. pec->nest_regs[reg] = val & PPC_BITMASK(0, 6);
  55. break;
  56. case PEC_NEST_PBCQ_PRED_VEC_TIMEOUT:
  57. pec->nest_regs[reg] = val & PPC_BITMASK(0, 15);
  58. break;
  59. case PEC_NEST_PBCQ_READ_STK_OVR:
  60. pec->nest_regs[reg] = val & PPC_BITMASK(0, 48);
  61. break;
  62. case PEC_NEST_PBCQ_WRITE_STK_OVR:
  63. case PEC_NEST_PBCQ_STORE_STK_OVR:
  64. pec->nest_regs[reg] = val & PPC_BITMASK(0, 24);
  65. break;
  66. case PEC_NEST_PBCQ_RETRY_BKOFF_CTRL:
  67. pec->nest_regs[reg] = val & PPC_BITMASK(0, 41);
  68. break;
  69. case PEC_NEST_PBCQ_HW_CONFIG:
  70. case PEC_NEST_CAPP_CTRL:
  71. pec->nest_regs[reg] = val;
  72. break;
  73. default:
  74. phb_pec_error(pec, "%s @0x%"HWADDR_PRIx"=%"PRIx64"\n", __func__,
  75. addr, val);
  76. }
  77. }
  78. static const MemoryRegionOps pnv_pec_nest_xscom_ops = {
  79. .read = pnv_pec_nest_xscom_read,
  80. .write = pnv_pec_nest_xscom_write,
  81. .valid.min_access_size = 8,
  82. .valid.max_access_size = 8,
  83. .impl.min_access_size = 8,
  84. .impl.max_access_size = 8,
  85. .endianness = DEVICE_BIG_ENDIAN,
  86. };
  87. static uint64_t pnv_pec_pci_xscom_read(void *opaque, hwaddr addr,
  88. unsigned size)
  89. {
  90. PnvPhb4PecState *pec = PNV_PHB4_PEC(opaque);
  91. uint32_t reg = addr >> 3;
  92. /* All registers are readable */
  93. return pec->pci_regs[reg];
  94. }
  95. static void pnv_pec_pci_xscom_write(void *opaque, hwaddr addr,
  96. uint64_t val, unsigned size)
  97. {
  98. PnvPhb4PecState *pec = PNV_PHB4_PEC(opaque);
  99. uint32_t reg = addr >> 3;
  100. switch (reg) {
  101. case PEC_PCI_PBAIB_HW_CONFIG:
  102. pec->pci_regs[reg] = val & PPC_BITMASK(0, 42);
  103. break;
  104. case PEC_PCI_PBAIB_HW_OVR:
  105. pec->pci_regs[reg] = val & PPC_BITMASK(0, 15);
  106. break;
  107. case PEC_PCI_PBAIB_READ_STK_OVR:
  108. pec->pci_regs[reg] = val & PPC_BITMASK(0, 48);
  109. break;
  110. default:
  111. phb_pec_error(pec, "%s @0x%"HWADDR_PRIx"=%"PRIx64"\n", __func__,
  112. addr, val);
  113. }
  114. }
  115. static const MemoryRegionOps pnv_pec_pci_xscom_ops = {
  116. .read = pnv_pec_pci_xscom_read,
  117. .write = pnv_pec_pci_xscom_write,
  118. .valid.min_access_size = 8,
  119. .valid.max_access_size = 8,
  120. .impl.min_access_size = 8,
  121. .impl.max_access_size = 8,
  122. .endianness = DEVICE_BIG_ENDIAN,
  123. };
  124. PnvPhb4PecState *pnv_pec_add_phb(PnvChip *chip, PnvPHB *phb, Error **errp)
  125. {
  126. PnvPhb4PecState *pecs = NULL;
  127. int chip_id = phb->chip_id;
  128. int index = phb->phb_id;
  129. int i, j;
  130. if (phb->version == 4) {
  131. Pnv9Chip *chip9 = PNV9_CHIP(chip);
  132. pecs = chip9->pecs;
  133. } else if (phb->version == 5) {
  134. Pnv10Chip *chip10 = PNV10_CHIP(chip);
  135. pecs = chip10->pecs;
  136. } else {
  137. g_assert_not_reached();
  138. }
  139. for (i = 0; i < chip->num_pecs; i++) {
  140. /*
  141. * For each PEC, check the amount of phbs it supports
  142. * and see if the given phb4 index matches an index.
  143. */
  144. PnvPhb4PecState *pec = &pecs[i];
  145. for (j = 0; j < pec->num_phbs; j++) {
  146. if (index == pnv_phb4_pec_get_phb_id(pec, j)) {
  147. pec->phbs[j] = phb;
  148. phb->pec = pec;
  149. return pec;
  150. }
  151. }
  152. }
  153. error_setg(errp,
  154. "pnv-phb4 chip-id %d index %d didn't match any existing PEC",
  155. chip_id, index);
  156. return NULL;
  157. }
  158. static PnvPHB *pnv_pec_default_phb_realize(PnvPhb4PecState *pec,
  159. int stack_no,
  160. Error **errp)
  161. {
  162. PnvPHB *phb = PNV_PHB(qdev_new(TYPE_PNV_PHB));
  163. int phb_id = pnv_phb4_pec_get_phb_id(pec, stack_no);
  164. object_property_add_child(OBJECT(pec), "phb[*]", OBJECT(phb));
  165. object_property_set_link(OBJECT(phb), "pec", OBJECT(pec),
  166. &error_abort);
  167. object_property_set_int(OBJECT(phb), "chip-id", pec->chip_id,
  168. &error_fatal);
  169. object_property_set_int(OBJECT(phb), "index", phb_id,
  170. &error_fatal);
  171. if (!sysbus_realize(SYS_BUS_DEVICE(phb), errp)) {
  172. return NULL;
  173. }
  174. return phb;
  175. }
  176. #define XPEC_P9_PCI_LANE_CFG PPC_BITMASK(10, 11)
  177. #define XPEC_P10_PCI_LANE_CFG PPC_BITMASK(0, 1)
  178. static void pnv_pec_realize(DeviceState *dev, Error **errp)
  179. {
  180. PnvPhb4PecState *pec = PNV_PHB4_PEC(dev);
  181. PnvPhb4PecClass *pecc = PNV_PHB4_PEC_GET_CLASS(pec);
  182. char name[64];
  183. int i;
  184. if (pec->index >= PNV_CHIP_GET_CLASS(pec->chip)->num_pecs) {
  185. error_setg(errp, "invalid PEC index: %d", pec->index);
  186. return;
  187. }
  188. pec->num_phbs = pecc->num_phbs[pec->index];
  189. /* Pervasive chiplet */
  190. object_initialize_child(OBJECT(pec), "nest-pervasive-common",
  191. &pec->nest_pervasive,
  192. TYPE_PNV_NEST_CHIPLET_PERVASIVE);
  193. if (!qdev_realize(DEVICE(&pec->nest_pervasive), NULL, errp)) {
  194. return;
  195. }
  196. /* Set up pervasive chiplet registers */
  197. /*
  198. * Most registers are not set up, this just sets the PCI CONF1 link-width
  199. * field because skiboot probes it.
  200. */
  201. if (pecc->version == PNV_PHB4_VERSION) {
  202. /*
  203. * On P9, PEC2 has configurable 1/2/3-furcation).
  204. * Make it trifurcated (x8, x4, x4) to match pnv_pec_num_phbs.
  205. */
  206. if (pec->index == 2) {
  207. pec->nest_pervasive.control_regs.cplt_cfg1 =
  208. SETFIELD(XPEC_P9_PCI_LANE_CFG,
  209. pec->nest_pervasive.control_regs.cplt_cfg1,
  210. 0b10);
  211. }
  212. } else if (pecc->version == PNV_PHB5_VERSION) {
  213. /*
  214. * On P10, both PECs are configurable 1/2/3-furcation).
  215. * Both are trifurcated to match pnv_phb5_pec_num_stacks.
  216. */
  217. pec->nest_pervasive.control_regs.cplt_cfg1 =
  218. SETFIELD(XPEC_P10_PCI_LANE_CFG,
  219. pec->nest_pervasive.control_regs.cplt_cfg1,
  220. 0b10);
  221. } else {
  222. g_assert_not_reached();
  223. }
  224. /* Create PHBs if running with defaults */
  225. if (defaults_enabled()) {
  226. g_assert(pec->num_phbs <= MAX_PHBS_PER_PEC);
  227. for (i = 0; i < pec->num_phbs; i++) {
  228. pec->phbs[i] = pnv_pec_default_phb_realize(pec, i, errp);
  229. }
  230. }
  231. /* Initialize the XSCOM regions for the PEC registers */
  232. snprintf(name, sizeof(name), "xscom-pec-%d.%d-nest", pec->chip_id,
  233. pec->index);
  234. pnv_xscom_region_init(&pec->nest_regs_mr, OBJECT(dev),
  235. &pnv_pec_nest_xscom_ops, pec, name,
  236. PHB4_PEC_NEST_REGS_COUNT);
  237. snprintf(name, sizeof(name), "xscom-pec-%d.%d-pci", pec->chip_id,
  238. pec->index);
  239. pnv_xscom_region_init(&pec->pci_regs_mr, OBJECT(dev),
  240. &pnv_pec_pci_xscom_ops, pec, name,
  241. PHB4_PEC_PCI_REGS_COUNT);
  242. }
  243. static int pnv_pec_dt_xscom(PnvXScomInterface *dev, void *fdt,
  244. int xscom_offset)
  245. {
  246. PnvPhb4PecState *pec = PNV_PHB4_PEC(dev);
  247. PnvPhb4PecClass *pecc = PNV_PHB4_PEC_GET_CLASS(dev);
  248. uint32_t nbase = pecc->xscom_nest_base(pec);
  249. uint32_t pbase = pecc->xscom_pci_base(pec);
  250. int offset, i;
  251. char *name;
  252. uint32_t reg[] = {
  253. cpu_to_be32(nbase),
  254. cpu_to_be32(pecc->xscom_nest_size),
  255. cpu_to_be32(pbase),
  256. cpu_to_be32(pecc->xscom_pci_size),
  257. };
  258. name = g_strdup_printf("pbcq@%x", nbase);
  259. offset = fdt_add_subnode(fdt, xscom_offset, name);
  260. _FDT(offset);
  261. g_free(name);
  262. _FDT((fdt_setprop(fdt, offset, "reg", reg, sizeof(reg))));
  263. _FDT((fdt_setprop_cell(fdt, offset, "ibm,pec-index", pec->index)));
  264. _FDT((fdt_setprop_cell(fdt, offset, "#address-cells", 1)));
  265. _FDT((fdt_setprop_cell(fdt, offset, "#size-cells", 0)));
  266. _FDT((fdt_setprop(fdt, offset, "compatible", pecc->compat,
  267. pecc->compat_size)));
  268. for (i = 0; i < pec->num_phbs; i++) {
  269. int stk_offset;
  270. if (!pec->phbs[i]) {
  271. continue;
  272. }
  273. name = g_strdup_printf("stack@%x", i);
  274. stk_offset = fdt_add_subnode(fdt, offset, name);
  275. _FDT(stk_offset);
  276. g_free(name);
  277. _FDT((fdt_setprop(fdt, stk_offset, "compatible", pecc->stk_compat,
  278. pecc->stk_compat_size)));
  279. _FDT((fdt_setprop_cell(fdt, stk_offset, "reg", i)));
  280. _FDT((fdt_setprop_cell(fdt, stk_offset, "ibm,phb-index",
  281. pec->phbs[i]->phb_id)));
  282. }
  283. return 0;
  284. }
  285. static const Property pnv_pec_properties[] = {
  286. DEFINE_PROP_UINT32("index", PnvPhb4PecState, index, 0),
  287. DEFINE_PROP_UINT32("chip-id", PnvPhb4PecState, chip_id, 0),
  288. DEFINE_PROP_LINK("chip", PnvPhb4PecState, chip, TYPE_PNV_CHIP,
  289. PnvChip *),
  290. };
  291. #define XPEC_PCI_CPLT_OFFSET 0x1000000ULL
  292. static uint32_t pnv_pec_xscom_cplt_base(PnvPhb4PecState *pec)
  293. {
  294. return PNV9_XSCOM_PEC_NEST_CPLT_BASE + XPEC_PCI_CPLT_OFFSET * pec->index;
  295. }
  296. static uint32_t pnv_pec_xscom_pci_base(PnvPhb4PecState *pec)
  297. {
  298. return PNV9_XSCOM_PEC_PCI_BASE + XPEC_PCI_CPLT_OFFSET * pec->index;
  299. }
  300. static uint32_t pnv_pec_xscom_nest_base(PnvPhb4PecState *pec)
  301. {
  302. return PNV9_XSCOM_PEC_NEST_BASE + 0x400 * pec->index;
  303. }
  304. /*
  305. * PEC0 -> 1 phb
  306. * PEC1 -> 2 phb
  307. * PEC2 -> 3 phbs
  308. */
  309. static const uint32_t pnv_pec_num_phbs[] = { 1, 2, 3 };
  310. static void pnv_pec_class_init(ObjectClass *klass, void *data)
  311. {
  312. DeviceClass *dc = DEVICE_CLASS(klass);
  313. PnvXScomInterfaceClass *xdc = PNV_XSCOM_INTERFACE_CLASS(klass);
  314. PnvPhb4PecClass *pecc = PNV_PHB4_PEC_CLASS(klass);
  315. static const char compat[] = "ibm,power9-pbcq";
  316. static const char stk_compat[] = "ibm,power9-phb-stack";
  317. xdc->dt_xscom = pnv_pec_dt_xscom;
  318. dc->realize = pnv_pec_realize;
  319. device_class_set_props(dc, pnv_pec_properties);
  320. dc->user_creatable = false;
  321. pecc->xscom_cplt_base = pnv_pec_xscom_cplt_base;
  322. pecc->xscom_nest_base = pnv_pec_xscom_nest_base;
  323. pecc->xscom_pci_base = pnv_pec_xscom_pci_base;
  324. pecc->xscom_nest_size = PNV9_XSCOM_PEC_NEST_SIZE;
  325. pecc->xscom_pci_size = PNV9_XSCOM_PEC_PCI_SIZE;
  326. pecc->compat = compat;
  327. pecc->compat_size = sizeof(compat);
  328. pecc->stk_compat = stk_compat;
  329. pecc->stk_compat_size = sizeof(stk_compat);
  330. pecc->version = PNV_PHB4_VERSION;
  331. pecc->phb_type = TYPE_PNV_PHB4;
  332. pecc->num_phbs = pnv_pec_num_phbs;
  333. }
  334. static const TypeInfo pnv_pec_type_info = {
  335. .name = TYPE_PNV_PHB4_PEC,
  336. .parent = TYPE_DEVICE,
  337. .instance_size = sizeof(PnvPhb4PecState),
  338. .class_init = pnv_pec_class_init,
  339. .class_size = sizeof(PnvPhb4PecClass),
  340. .interfaces = (InterfaceInfo[]) {
  341. { TYPE_PNV_XSCOM_INTERFACE },
  342. { }
  343. }
  344. };
  345. /*
  346. * POWER10 definitions
  347. */
  348. static uint32_t pnv_phb5_pec_xscom_cplt_base(PnvPhb4PecState *pec)
  349. {
  350. return PNV10_XSCOM_PEC_NEST_CPLT_BASE + XPEC_PCI_CPLT_OFFSET * pec->index;
  351. }
  352. static uint32_t pnv_phb5_pec_xscom_pci_base(PnvPhb4PecState *pec)
  353. {
  354. return PNV10_XSCOM_PEC_PCI_BASE + 0x1000000 * pec->index;
  355. }
  356. static uint32_t pnv_phb5_pec_xscom_nest_base(PnvPhb4PecState *pec)
  357. {
  358. /* index goes down ... */
  359. return PNV10_XSCOM_PEC_NEST_BASE - 0x1000000 * pec->index;
  360. }
  361. /*
  362. * PEC0 -> 3 stacks
  363. * PEC1 -> 3 stacks
  364. */
  365. static const uint32_t pnv_phb5_pec_num_stacks[] = { 3, 3 };
  366. static void pnv_phb5_pec_class_init(ObjectClass *klass, void *data)
  367. {
  368. PnvPhb4PecClass *pecc = PNV_PHB4_PEC_CLASS(klass);
  369. static const char compat[] = "ibm,power10-pbcq";
  370. static const char stk_compat[] = "ibm,power10-phb-stack";
  371. pecc->xscom_cplt_base = pnv_phb5_pec_xscom_cplt_base;
  372. pecc->xscom_nest_base = pnv_phb5_pec_xscom_nest_base;
  373. pecc->xscom_pci_base = pnv_phb5_pec_xscom_pci_base;
  374. pecc->xscom_nest_size = PNV10_XSCOM_PEC_NEST_SIZE;
  375. pecc->xscom_pci_size = PNV10_XSCOM_PEC_PCI_SIZE;
  376. pecc->compat = compat;
  377. pecc->compat_size = sizeof(compat);
  378. pecc->stk_compat = stk_compat;
  379. pecc->stk_compat_size = sizeof(stk_compat);
  380. pecc->version = PNV_PHB5_VERSION;
  381. pecc->phb_type = TYPE_PNV_PHB5;
  382. pecc->num_phbs = pnv_phb5_pec_num_stacks;
  383. }
  384. static const TypeInfo pnv_phb5_pec_type_info = {
  385. .name = TYPE_PNV_PHB5_PEC,
  386. .parent = TYPE_PNV_PHB4_PEC,
  387. .instance_size = sizeof(PnvPhb4PecState),
  388. .class_init = pnv_phb5_pec_class_init,
  389. .class_size = sizeof(PnvPhb4PecClass),
  390. .interfaces = (InterfaceInfo[]) {
  391. { TYPE_PNV_XSCOM_INTERFACE },
  392. { }
  393. }
  394. };
  395. static void pnv_pec_register_types(void)
  396. {
  397. type_register_static(&pnv_pec_type_info);
  398. type_register_static(&pnv_phb5_pec_type_info);
  399. }
  400. type_init(pnv_pec_register_types);