pnv_phb.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /*
  2. * QEMU PowerPC PowerNV Proxy PHB model
  3. *
  4. * Copyright (c) 2022, 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/visitor.h"
  12. #include "qapi/error.h"
  13. #include "hw/pci-host/pnv_phb.h"
  14. #include "hw/pci-host/pnv_phb3.h"
  15. #include "hw/pci-host/pnv_phb4.h"
  16. #include "hw/ppc/pnv.h"
  17. #include "hw/qdev-properties.h"
  18. #include "qom/object.h"
  19. #include "sysemu/sysemu.h"
  20. /*
  21. * Set the QOM parent and parent bus of an object child. If the device
  22. * state associated with the child has an id, use it as QOM id.
  23. * Otherwise use object_typename[index] as QOM id.
  24. *
  25. * This helper does both operations at the same time because setting
  26. * a new QOM child will erase the bus parent of the device. This happens
  27. * because object_unparent() will call object_property_del_child(),
  28. * which in turn calls the property release callback prop->release if
  29. * it's defined. In our case this callback is set to
  30. * object_finalize_child_property(), which was assigned during the
  31. * first object_property_add_child() call. This callback will end up
  32. * calling device_unparent(), and this function removes the device
  33. * from its parent bus.
  34. *
  35. * The QOM and parent bus to be set aren´t necessarily related, so
  36. * let's receive both as arguments.
  37. */
  38. static bool pnv_parent_fixup(Object *parent, BusState *parent_bus,
  39. Object *child, int index,
  40. Error **errp)
  41. {
  42. g_autofree char *default_id =
  43. g_strdup_printf("%s[%d]", object_get_typename(child), index);
  44. const char *dev_id = DEVICE(child)->id;
  45. if (child->parent == parent) {
  46. return true;
  47. }
  48. object_ref(child);
  49. object_unparent(child);
  50. object_property_add_child(parent, dev_id ? dev_id : default_id, child);
  51. object_unref(child);
  52. if (!qdev_set_parent_bus(DEVICE(child), parent_bus, errp)) {
  53. return false;
  54. }
  55. return true;
  56. }
  57. static Object *pnv_phb_user_get_parent(PnvChip *chip, PnvPHB *phb, Error **errp)
  58. {
  59. if (phb->version == 3) {
  60. return OBJECT(pnv_chip_add_phb(chip, phb));
  61. } else {
  62. return OBJECT(pnv_pec_add_phb(chip, phb, errp));
  63. }
  64. }
  65. /*
  66. * User created devices won't have the initial setup that default
  67. * devices have. This setup consists of assigning a parent device
  68. * (chip for PHB3, PEC for PHB4/5) that will be the QOM/bus parent
  69. * of the PHB.
  70. */
  71. static bool pnv_phb_user_device_init(PnvPHB *phb, Error **errp)
  72. {
  73. PnvMachineState *pnv = PNV_MACHINE(qdev_get_machine());
  74. PnvChip *chip = pnv_get_chip(pnv, phb->chip_id);
  75. Object *parent = NULL;
  76. if (!chip) {
  77. error_setg(errp, "invalid chip id: %d", phb->chip_id);
  78. return false;
  79. }
  80. parent = pnv_phb_user_get_parent(chip, phb, errp);
  81. if (!parent) {
  82. return false;
  83. }
  84. /*
  85. * Reparent user created devices to the chip to build
  86. * correctly the device tree. pnv_xscom_dt() needs every
  87. * PHB to be a child of the chip to build the DT correctly.
  88. */
  89. if (!pnv_parent_fixup(parent, qdev_get_parent_bus(DEVICE(chip)),
  90. OBJECT(phb), phb->phb_id, errp)) {
  91. return false;
  92. }
  93. return true;
  94. }
  95. static void pnv_phb_realize(DeviceState *dev, Error **errp)
  96. {
  97. PnvPHB *phb = PNV_PHB(dev);
  98. PCIHostState *pci = PCI_HOST_BRIDGE(dev);
  99. g_autofree char *phb_typename = NULL;
  100. if (!phb->version) {
  101. error_setg(errp, "version not specified");
  102. return;
  103. }
  104. switch (phb->version) {
  105. case 3:
  106. phb_typename = g_strdup(TYPE_PNV_PHB3);
  107. break;
  108. case 4:
  109. phb_typename = g_strdup(TYPE_PNV_PHB4);
  110. break;
  111. case 5:
  112. phb_typename = g_strdup(TYPE_PNV_PHB5);
  113. break;
  114. default:
  115. g_assert_not_reached();
  116. }
  117. phb->backend = object_new(phb_typename);
  118. object_property_add_child(OBJECT(dev), "phb-backend", phb->backend);
  119. /* Passthrough child device properties to the proxy device */
  120. object_property_set_uint(phb->backend, "index", phb->phb_id, errp);
  121. object_property_set_uint(phb->backend, "chip-id", phb->chip_id, errp);
  122. object_property_set_link(phb->backend, "phb-base", OBJECT(phb), errp);
  123. /*
  124. * Handle user created devices. User devices will not have a
  125. * pointer to a chip (PHB3) and a PEC (PHB4/5).
  126. */
  127. if (!phb->chip && !phb->pec) {
  128. if (!pnv_phb_user_device_init(phb, errp)) {
  129. return;
  130. }
  131. }
  132. if (phb->version == 3) {
  133. object_property_set_link(phb->backend, "chip",
  134. OBJECT(phb->chip), errp);
  135. } else {
  136. object_property_set_link(phb->backend, "pec", OBJECT(phb->pec), errp);
  137. }
  138. if (!qdev_realize(DEVICE(phb->backend), NULL, errp)) {
  139. return;
  140. }
  141. if (phb->version == 3) {
  142. pnv_phb3_bus_init(dev, PNV_PHB3(phb->backend));
  143. } else {
  144. pnv_phb4_bus_init(dev, PNV_PHB4(phb->backend));
  145. }
  146. if (defaults_enabled()) {
  147. PCIDevice *root = pci_new(PCI_DEVFN(0, 0), TYPE_PNV_PHB_ROOT_PORT);
  148. pci_realize_and_unref(root, pci->bus, errp);
  149. }
  150. }
  151. static const char *pnv_phb_root_bus_path(PCIHostState *host_bridge,
  152. PCIBus *rootbus)
  153. {
  154. PnvPHB *phb = PNV_PHB(host_bridge);
  155. snprintf(phb->bus_path, sizeof(phb->bus_path), "00%02x:%02x",
  156. phb->chip_id, phb->phb_id);
  157. return phb->bus_path;
  158. }
  159. static Property pnv_phb_properties[] = {
  160. DEFINE_PROP_UINT32("index", PnvPHB, phb_id, 0),
  161. DEFINE_PROP_UINT32("chip-id", PnvPHB, chip_id, 0),
  162. DEFINE_PROP_UINT32("version", PnvPHB, version, 0),
  163. DEFINE_PROP_LINK("chip", PnvPHB, chip, TYPE_PNV_CHIP, PnvChip *),
  164. DEFINE_PROP_LINK("pec", PnvPHB, pec, TYPE_PNV_PHB4_PEC,
  165. PnvPhb4PecState *),
  166. DEFINE_PROP_END_OF_LIST(),
  167. };
  168. static void pnv_phb_class_init(ObjectClass *klass, void *data)
  169. {
  170. PCIHostBridgeClass *hc = PCI_HOST_BRIDGE_CLASS(klass);
  171. DeviceClass *dc = DEVICE_CLASS(klass);
  172. hc->root_bus_path = pnv_phb_root_bus_path;
  173. dc->realize = pnv_phb_realize;
  174. device_class_set_props(dc, pnv_phb_properties);
  175. set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
  176. dc->user_creatable = true;
  177. }
  178. static void pnv_phb_root_port_reset_hold(Object *obj, ResetType type)
  179. {
  180. PCIERootPortClass *rpc = PCIE_ROOT_PORT_GET_CLASS(obj);
  181. PnvPHBRootPort *phb_rp = PNV_PHB_ROOT_PORT(obj);
  182. PCIDevice *d = PCI_DEVICE(obj);
  183. uint8_t *conf = d->config;
  184. if (rpc->parent_phases.hold) {
  185. rpc->parent_phases.hold(obj, type);
  186. }
  187. if (phb_rp->version == 3) {
  188. return;
  189. }
  190. /* PHB4 and later requires these extra reset steps */
  191. pci_byte_test_and_set_mask(conf + PCI_IO_BASE,
  192. PCI_IO_RANGE_MASK & 0xff);
  193. pci_byte_test_and_clear_mask(conf + PCI_IO_LIMIT,
  194. PCI_IO_RANGE_MASK & 0xff);
  195. pci_set_word(conf + PCI_MEMORY_BASE, 0);
  196. pci_set_word(conf + PCI_MEMORY_LIMIT, 0xfff0);
  197. pci_set_word(conf + PCI_PREF_MEMORY_BASE, 0x1);
  198. pci_set_word(conf + PCI_PREF_MEMORY_LIMIT, 0xfff1);
  199. pci_set_long(conf + PCI_PREF_BASE_UPPER32, 0x1); /* Hack */
  200. pci_set_long(conf + PCI_PREF_LIMIT_UPPER32, 0xffffffff);
  201. pci_config_set_interrupt_pin(conf, 0);
  202. }
  203. static void pnv_phb_root_port_realize(DeviceState *dev, Error **errp)
  204. {
  205. PCIERootPortClass *rpc = PCIE_ROOT_PORT_GET_CLASS(dev);
  206. PnvPHBRootPort *phb_rp = PNV_PHB_ROOT_PORT(dev);
  207. PCIBus *bus = PCI_BUS(qdev_get_parent_bus(dev));
  208. PCIDevice *pci = PCI_DEVICE(dev);
  209. uint16_t device_id = 0;
  210. Error *local_err = NULL;
  211. int chip_id, index;
  212. /*
  213. * 'index' will be used both as a PCIE slot value and to calculate
  214. * QOM id. 'chip_id' is going to be used as PCIE chassis for the
  215. * root port.
  216. */
  217. chip_id = object_property_get_int(OBJECT(bus), "chip-id", &local_err);
  218. if (local_err) {
  219. error_propagate(errp, local_err);
  220. return;
  221. }
  222. index = object_property_get_int(OBJECT(bus), "phb-id", &local_err);
  223. if (local_err) {
  224. error_propagate(errp, local_err);
  225. return;
  226. }
  227. /* Set unique chassis/slot values for the root port */
  228. qdev_prop_set_uint8(dev, "chassis", chip_id);
  229. qdev_prop_set_uint16(dev, "slot", index);
  230. /*
  231. * User created root ports are QOM parented to one of
  232. * the peripheral containers but it's already at the right
  233. * parent bus. Change the QOM parent to be the same as the
  234. * parent bus it's already assigned to.
  235. */
  236. if (!pnv_parent_fixup(OBJECT(bus), BUS(bus), OBJECT(dev),
  237. index, errp)) {
  238. return;
  239. }
  240. rpc->parent_realize(dev, &local_err);
  241. if (local_err) {
  242. error_propagate(errp, local_err);
  243. return;
  244. }
  245. switch (phb_rp->version) {
  246. case 3:
  247. device_id = PNV_PHB3_DEVICE_ID;
  248. break;
  249. case 4:
  250. device_id = PNV_PHB4_DEVICE_ID;
  251. break;
  252. case 5:
  253. device_id = PNV_PHB5_DEVICE_ID;
  254. break;
  255. default:
  256. g_assert_not_reached();
  257. }
  258. pci_config_set_device_id(pci->config, device_id);
  259. pci_config_set_interrupt_pin(pci->config, 0);
  260. }
  261. static Property pnv_phb_root_port_properties[] = {
  262. DEFINE_PROP_UINT32("version", PnvPHBRootPort, version, 0),
  263. DEFINE_PROP_END_OF_LIST(),
  264. };
  265. static void pnv_phb_root_port_class_init(ObjectClass *klass, void *data)
  266. {
  267. DeviceClass *dc = DEVICE_CLASS(klass);
  268. ResettableClass *rc = RESETTABLE_CLASS(klass);
  269. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  270. PCIERootPortClass *rpc = PCIE_ROOT_PORT_CLASS(klass);
  271. dc->desc = "IBM PHB PCIE Root Port";
  272. device_class_set_props(dc, pnv_phb_root_port_properties);
  273. device_class_set_parent_realize(dc, pnv_phb_root_port_realize,
  274. &rpc->parent_realize);
  275. resettable_class_set_parent_phases(rc, NULL, pnv_phb_root_port_reset_hold,
  276. NULL, &rpc->parent_phases);
  277. dc->user_creatable = true;
  278. k->vendor_id = PCI_VENDOR_ID_IBM;
  279. /* device_id will be written during realize() */
  280. k->device_id = 0;
  281. k->revision = 0;
  282. rpc->exp_offset = 0x48;
  283. rpc->aer_offset = 0x100;
  284. }
  285. static const TypeInfo pnv_phb_type_info = {
  286. .name = TYPE_PNV_PHB,
  287. .parent = TYPE_PCIE_HOST_BRIDGE,
  288. .instance_size = sizeof(PnvPHB),
  289. .class_init = pnv_phb_class_init,
  290. };
  291. static const TypeInfo pnv_phb_root_port_info = {
  292. .name = TYPE_PNV_PHB_ROOT_PORT,
  293. .parent = TYPE_PCIE_ROOT_PORT,
  294. .instance_size = sizeof(PnvPHBRootPort),
  295. .class_init = pnv_phb_root_port_class_init,
  296. };
  297. static void pnv_phb_register_types(void)
  298. {
  299. type_register_static(&pnv_phb_type_info);
  300. type_register_static(&pnv_phb_root_port_info);
  301. }
  302. type_init(pnv_phb_register_types)