@@ -1,6 +1,7 @@
# core qdev-related obj files, also used by *-user:
common-obj-y += qdev.o qdev-properties.o
common-obj-y += bus.o reset.o
+common-obj-y += resettable.o
common-obj-$(CONFIG_SOFTMMU) += qdev-fw.o
common-obj-$(CONFIG_SOFTMMU) += fw-path-provider.o
# irq.o needed for qdev GPIO handling:
new file mode 100644
@@ -0,0 +1,69 @@
+/*
+ * Resettable interface.
+ *
+ * Copyright (c) 2019 GreenSocs SAS
+ *
+ * Authors:
+ * Damien Hedde
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ */
+
+#include "qemu/osdep.h"
+#include "qemu/module.h"
+#include "hw/resettable.h"
+
+#define RESETTABLE_GET_CLASS(obj) \
+ OBJECT_GET_CLASS(ResettableClass, (obj), TYPE_RESETTABLE)
+
+void resettable_init_phase(Object *obj, bool cold)
+{
+ ResettableClass *rc = RESETTABLE_GET_CLASS(obj);
+
+ if (rc->phases.init) {
+ rc->phases.init(obj, cold);
+ }
+}
+
+void resettable_hold_phase(Object *obj)
+{
+ ResettableClass *rc = RESETTABLE_GET_CLASS(obj);
+
+ if (rc->phases.hold) {
+ rc->phases.hold(obj);
+ }
+}
+
+void resettable_exit_phase(Object *obj)
+{
+ ResettableClass *rc = RESETTABLE_GET_CLASS(obj);
+
+ if (rc->phases.exit) {
+ rc->phases.exit(obj);
+ }
+}
+
+void resettable_assert_reset(Object *obj, bool cold)
+{
+ resettable_init_phase(obj, cold);
+ resettable_hold_phase(obj);
+}
+
+void resettable_deassert_reset(Object *obj)
+{
+ resettable_exit_phase(obj);
+}
+
+static const TypeInfo resettable_interface_info = {
+ .name = TYPE_RESETTABLE,
+ .parent = TYPE_INTERFACE,
+ .class_size = sizeof(ResettableClass),
+};
+
+static void reset_register_types(void)
+{
+ type_register_static(&resettable_interface_info);
+}
+
+type_init(reset_register_types)
new file mode 100644
@@ -0,0 +1,83 @@
+#ifndef HW_RESETTABLE_H
+#define HW_RESETTABLE_H
+
+#include "qom/object.h"
+
+#define TYPE_RESETTABLE "resettable"
+
+#define RESETTABLE_CLASS(class) \
+ OBJECT_CLASS_CHECK(ResettableClass, (class), TYPE_RESETTABLE)
+
+/*
+ * ResettableClass:
+ * Interface for resettable objects.
+ *
+ * The reset operation is divided in several phases each represented by a
+ * method.
+ *
+ * @phases.init: should reset local state only. Takes a bool @cold argument
+ * specifying whether the reset is cold or warm. It must not do side-effect
+ * on others objects.
+ *
+ * @phases.hold: side-effects action on others objects due to staying in a
+ * resetting state.
+ *
+ * @phases.exit: leave the reset state, may do side-effects action on others
+ * objects.
+ *
+ * "Entering the reset state" corresponds to the init and hold phases.
+ * "Leaving the reset state" corresponds to the exit phase.
+ */
+typedef void (*ResettableInitPhase)(Object *obj, bool cold);
+typedef void (*ResettableHoldPhase)(Object *obj);
+typedef void (*ResettableExitPhase)(Object *obj);
+typedef struct ResettableClass {
+ InterfaceClass parent_class;
+
+ struct ResettablePhases {
+ ResettableInitPhase init;
+ ResettableHoldPhase hold;
+ ResettableExitPhase exit;
+ } phases;
+} ResettableClass;
+typedef struct ResettablePhases ResettablePhases;
+
+/*
+ * Helpers to do a single phase of a Resettable.
+ * Call the corresponding ResettableClass method if it is not NULL.
+ */
+void resettable_init_phase(Object *obj, bool cold);
+void resettable_hold_phase(Object *obj);
+void resettable_exit_phase(Object *obj);
+
+/**
+ * resettable_assert_reset:
+ * Put an object in reset state.
+ * Each time resettable_assert_reset is called, resettable_deassert_reset
+ * must be eventually called once and only once.
+ *
+ * @obj object to reset, must implement Resettable interface.
+ * @cold boolean indicating the type of reset (cold or warm)
+ */
+void resettable_assert_reset(Object *obj, bool cold);
+
+/**
+ * resettable_deassert_reset:
+ * End the reset state if an object.
+ *
+ * @obj object to reset, must implement Resettable interface.
+ */
+void resettable_deassert_reset(Object *obj);
+
+/**
+ * resettable_reset:
+ * Calling this function is equivalent to call @assert_reset then
+ * @deassert_reset.
+ */
+static inline void resettable_reset(Object *obj, bool cold)
+{
+ resettable_assert_reset(obj, cold);
+ resettable_deassert_reset(obj);
+}
+
+#endif
This commit defines an interface allowing multi-phase reset. The phases are INIT, HOLD and EXIT. The reset of a Resettable is controlled with 2 functions: - resettable_assert_reset which starts the reset operation. - resettable_deassert_reset which ends the reset operation. There is also a `resettable_reset` helper function which does assert then deassert allowing to do a proper reset in one call. The interface only contains 3 methods, one per phase. Each method should handle reset of the object and its sub-elements. Signed-off-by: Damien Hedde <damien.hedde@greensocs.com> --- hw/core/Makefile.objs | 1 + hw/core/resettable.c | 69 ++++++++++++++++++++++++++++++++++ include/hw/resettable.h | 83 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 153 insertions(+) create mode 100644 hw/core/resettable.c create mode 100644 include/hw/resettable.h