qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [Qemu-devel] [PATCH v2 1/2] spapr: introduce SPAPR_MACHINE_NOASSERT()
@ 2017-10-08 17:17 Greg Kurz
  2017-10-08 17:17 ` [Qemu-devel] [PATCH v2 2/2] spapr_pci: fail gracefully with non-pseries machine types Greg Kurz
  2017-10-08 20:05 ` [Qemu-devel] [PATCH v2 1/2] spapr: introduce SPAPR_MACHINE_NOASSERT() Peter Maydell
  0 siblings, 2 replies; 5+ messages in thread
From: Greg Kurz @ 2017-10-08 17:17 UTC (permalink / raw)
  To: qemu-devel; +Cc: qemu-ppc, David Gibson

A spapr-cpu-core device can only be plugged into a pseries machine. This
is checked in spapr_cpu_core_realize() with object_dynamic_cast() instead
of the usual SPAPR_MACHINE() macro because we don't want QEMU to abort.

This patch moves the gory details to a SPAPR_MACHINE_NOASSERT() macro
that acts like the SPAPR_MACHINE() one, except it returns NULL instead
of aborting if its argument doesn't point to a pseries machine type.

This is done for two reasons:
- it makes the code nicer
- it may be used by other pseries-specific devices like PHBs for example

Signed-off-by: Greg Kurz <groug@kaod.org>
---
 hw/ppc/spapr_cpu_core.c |    7 +++----
 include/hw/ppc/spapr.h  |    3 +++
 2 files changed, 6 insertions(+), 4 deletions(-)

diff --git a/hw/ppc/spapr_cpu_core.c b/hw/ppc/spapr_cpu_core.c
index 37beb56e8b18..5fde07614218 100644
--- a/hw/ppc/spapr_cpu_core.c
+++ b/hw/ppc/spapr_cpu_core.c
@@ -199,7 +199,7 @@ error:
 
 static void spapr_cpu_core_realize(DeviceState *dev, Error **errp)
 {
-    sPAPRMachineState *spapr;
+    sPAPRMachineState *spapr = SPAPR_MACHINE_NOASSERT(qdev_get_machine());
     sPAPRCPUCore *sc = SPAPR_CPU_CORE(OBJECT(dev));
     sPAPRCPUCoreClass *scc = SPAPR_CPU_CORE_GET_CLASS(OBJECT(dev));
     CPUCore *cc = CPU_CORE(OBJECT(dev));
@@ -209,9 +209,8 @@ static void spapr_cpu_core_realize(DeviceState *dev, Error **errp)
     void *obj;
     int i, j;
 
-    spapr = (sPAPRMachineState *) qdev_get_machine();
-    if (!object_dynamic_cast((Object *) spapr, TYPE_SPAPR_MACHINE)) {
-        error_setg(errp, "spapr-cpu-core needs a pseries machine");
+    if (!spapr) {
+        error_setg(errp, TYPE_SPAPR_CPU_CORE " needs a pseries machine");
         return;
     }
 
diff --git a/include/hw/ppc/spapr.h b/include/hw/ppc/spapr.h
index c1b365f56431..4933da8083df 100644
--- a/include/hw/ppc/spapr.h
+++ b/include/hw/ppc/spapr.h
@@ -43,6 +43,9 @@ typedef struct sPAPRMachineClass sPAPRMachineClass;
 #define SPAPR_MACHINE_CLASS(klass) \
     OBJECT_CLASS_CHECK(sPAPRMachineClass, klass, TYPE_SPAPR_MACHINE)
 
+#define SPAPR_MACHINE_NOASSERT(obj) \
+    ((sPAPRMachineState *) object_dynamic_cast(obj, TYPE_SPAPR_MACHINE))
+
 typedef enum {
     SPAPR_RESIZE_HPT_DEFAULT = 0,
     SPAPR_RESIZE_HPT_DISABLED,

^ permalink raw reply related	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2017-10-09  5:50 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-10-08 17:17 [Qemu-devel] [PATCH v2 1/2] spapr: introduce SPAPR_MACHINE_NOASSERT() Greg Kurz
2017-10-08 17:17 ` [Qemu-devel] [PATCH v2 2/2] spapr_pci: fail gracefully with non-pseries machine types Greg Kurz
2017-10-08 20:05 ` [Qemu-devel] [PATCH v2 1/2] spapr: introduce SPAPR_MACHINE_NOASSERT() Peter Maydell
2017-10-08 23:26   ` David Gibson
2017-10-09  5:50     ` Greg Kurz

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).