1 /* 2 * Resettable interface header. 3 * 4 * Copyright (c) 2019 GreenSocs SAS 5 * 6 * Authors: 7 * Damien Hedde 8 * 9 * This work is licensed under the terms of the GNU GPL, version 2 or later. 10 * See the COPYING file in the top-level directory. 11 */ 12 13 #ifndef HW_RESETTABLE_H 14 #define HW_RESETTABLE_H 15 16 #include "qom/object.h" 17 18 #define TYPE_RESETTABLE_INTERFACE "resettable" 19 20 typedef struct ResettableClass ResettableClass; 21 DECLARE_CLASS_CHECKERS(ResettableClass, RESETTABLE, 22 TYPE_RESETTABLE_INTERFACE) 23 24 25 typedef struct ResettableState ResettableState; 26 27 /** 28 * ResetType: 29 * Types of reset. 30 * 31 * + Cold: reset resulting from a power cycle of the object. 32 * 33 * TODO: Support has to be added to handle more types. In particular, 34 * ResettableState structure needs to be expanded. 35 */ 36 typedef enum ResetType { 37 RESET_TYPE_COLD, 38 RESET_TYPE_SNAPSHOT_LOAD, 39 RESET_TYPE_S390_CPU_INITIAL, 40 RESET_TYPE_S390_CPU_NORMAL, 41 } ResetType; 42 43 /* 44 * ResettableClass: 45 * Interface for resettable objects. 46 * 47 * See docs/devel/reset.rst for more detailed information about how QEMU models 48 * reset. This whole API must only be used when holding the iothread mutex. 49 * 50 * All objects which can be reset must implement this interface; 51 * it is usually provided by a base class such as DeviceClass or BusClass. 52 * Every Resettable object must maintain some state tracking the 53 * progress of a reset operation by providing a ResettableState structure. 54 * The functions defined in this module take care of updating the 55 * state of the reset. 56 * The base class implementation of the interface provides this 57 * state and implements the associated method: get_state. 58 * 59 * Concrete object implementations (typically specific devices 60 * such as a UART model) should provide the functions 61 * for the phases.enter, phases.hold and phases.exit methods, which 62 * they can set in their class init function, either directly or 63 * by calling resettable_class_set_parent_phases(). 64 * The phase methods are guaranteed to only only ever be called once 65 * for any reset event, in the order 'enter', 'hold', 'exit'. 66 * An object will always move quickly from 'enter' to 'hold' 67 * but might remain in 'hold' for an arbitrary period of time 68 * before eventually reset is deasserted and the 'exit' phase is called. 69 * Object implementations should be prepared for functions handling 70 * inbound connections from other devices (such as qemu_irq handler 71 * functions) to be called at any point during reset after their 72 * 'enter' method has been called. 73 * 74 * Users of a resettable object should not call these methods 75 * directly, but instead use the function resettable_reset(). 76 * 77 * @phases.enter: This phase is called when the object enters reset. It 78 * should reset local state of the object, but it must not do anything that 79 * has a side-effect on other objects, such as raising or lowering a qemu_irq 80 * line or reading or writing guest memory. It takes the reset's type as 81 * argument. 82 * 83 * @phases.hold: This phase is called for entry into reset, once every object 84 * in the system which is being reset has had its @phases.enter method called. 85 * At this point devices can do actions that affect other objects. 86 * 87 * @phases.exit: This phase is called when the object leaves the reset state. 88 * Actions affecting other objects are permitted. 89 * 90 * @get_state: Mandatory method which must return a pointer to a 91 * ResettableState. 92 * 93 * @get_transitional_function: transitional method to handle Resettable objects 94 * not yet fully moved to this interface. It will be removed as soon as it is 95 * not needed anymore. This method is optional and may return a pointer to a 96 * function to be used instead of the phases. If the method exists and returns 97 * a non-NULL function pointer then that function is executed as a replacement 98 * of the 'hold' phase method taking the object as argument. The two other phase 99 * methods are not executed. 100 * 101 * @child_foreach: Executes a given callback on every Resettable child. Child 102 * in this context means a child in the qbus tree, so the children of a qbus 103 * are the devices on it, and the children of a device are all the buses it 104 * owns. This is not the same as the QOM object hierarchy. The function takes 105 * additional opaque and ResetType arguments which must be passed unmodified to 106 * the callback. 107 */ 108 typedef void (*ResettableEnterPhase)(Object *obj, ResetType type); 109 typedef void (*ResettableHoldPhase)(Object *obj, ResetType type); 110 typedef void (*ResettableExitPhase)(Object *obj, ResetType type); 111 typedef ResettableState * (*ResettableGetState)(Object *obj); 112 typedef void (*ResettableTrFunction)(Object *obj); 113 typedef ResettableTrFunction (*ResettableGetTrFunction)(Object *obj); 114 typedef void (*ResettableChildCallback)(Object *, void *opaque, 115 ResetType type); 116 typedef void (*ResettableChildForeach)(Object *obj, 117 ResettableChildCallback cb, 118 void *opaque, ResetType type); 119 typedef struct ResettablePhases { 120 ResettableEnterPhase enter; 121 ResettableHoldPhase hold; 122 ResettableExitPhase exit; 123 } ResettablePhases; 124 struct ResettableClass { 125 InterfaceClass parent_class; 126 127 /* Phase methods */ 128 ResettablePhases phases; 129 130 /* State access method */ 131 ResettableGetState get_state; 132 133 /* Transitional method for legacy reset compatibility */ 134 ResettableGetTrFunction get_transitional_function; 135 136 /* Hierarchy handling method */ 137 ResettableChildForeach child_foreach; 138 }; 139 140 /** 141 * ResettableState: 142 * Structure holding reset related state. The fields should not be accessed 143 * directly; the definition is here to allow further inclusion into other 144 * objects. 145 * 146 * @count: Number of reset level the object is into. It is incremented when 147 * the reset operation starts and decremented when it finishes. 148 * @hold_phase_pending: flag which indicates that we need to invoke the 'hold' 149 * phase handler for this object. 150 * @exit_phase_in_progress: true if we are currently in the exit phase 151 */ 152 struct ResettableState { 153 unsigned count; 154 bool hold_phase_pending; 155 bool exit_phase_in_progress; 156 }; 157 158 /** 159 * resettable_state_clear: 160 * Clear the state. It puts the state to the initial (zeroed) state required 161 * to reuse an object. Typically used in realize step of base classes 162 * implementing the interface. 163 */ 164 static inline void resettable_state_clear(ResettableState *state) 165 { 166 memset(state, 0, sizeof(ResettableState)); 167 } 168 169 /** 170 * resettable_reset: 171 * Trigger a reset on an object @obj of type @type. @obj must implement 172 * Resettable interface. 173 * 174 * Calling this function is equivalent to calling @resettable_assert_reset() 175 * then @resettable_release_reset(). 176 */ 177 void resettable_reset(Object *obj, ResetType type); 178 179 /** 180 * resettable_assert_reset: 181 * Put an object @obj into reset. @obj must implement Resettable interface. 182 * 183 * @resettable_release_reset() must eventually be called after this call. 184 * There must be one call to @resettable_release_reset() per call of 185 * @resettable_assert_reset(), with the same type argument. 186 * 187 * NOTE: Until support for migration is added, the @resettable_release_reset() 188 * must not be delayed. It must occur just after @resettable_assert_reset() so 189 * that migration cannot be triggered in between. Prefer using 190 * @resettable_reset() for now. 191 */ 192 void resettable_assert_reset(Object *obj, ResetType type); 193 194 /** 195 * resettable_release_reset: 196 * Release the object @obj from reset. @obj must implement Resettable interface. 197 * 198 * See @resettable_assert_reset() description for details. 199 */ 200 void resettable_release_reset(Object *obj, ResetType type); 201 202 /** 203 * resettable_is_in_reset: 204 * Return true if @obj is under reset. 205 * 206 * @obj must implement Resettable interface. 207 */ 208 bool resettable_is_in_reset(Object *obj); 209 210 /** 211 * resettable_change_parent: 212 * Indicate that the parent of Ressettable @obj is changing from @oldp to @newp. 213 * All 3 objects must implement resettable interface. @oldp or @newp may be 214 * NULL. 215 * 216 * This function will adapt the reset state of @obj so that it is coherent 217 * with the reset state of @newp. It may trigger @resettable_assert_reset() 218 * or @resettable_release_reset(). It will do such things only if the reset 219 * state of @newp and @oldp are different. 220 * 221 * When using this function during reset, it must only be called during 222 * a hold phase method. Calling this during enter or exit phase is an error. 223 */ 224 void resettable_change_parent(Object *obj, Object *newp, Object *oldp); 225 226 /** 227 * resettable_cold_reset_fn: 228 * Helper to call resettable_reset((Object *) opaque, RESET_TYPE_COLD). 229 * 230 * This function is typically useful to register a reset handler with 231 * qemu_register_reset. 232 */ 233 void resettable_cold_reset_fn(void *opaque); 234 235 /** 236 * resettable_class_set_parent_phases: 237 * 238 * Save @rc current reset phases into @parent_phases and override @rc phases 239 * by the given new methods (@enter, @hold and @exit). 240 * Each phase is overridden only if the new one is not NULL allowing to 241 * override a subset of phases. 242 */ 243 void resettable_class_set_parent_phases(ResettableClass *rc, 244 ResettableEnterPhase enter, 245 ResettableHoldPhase hold, 246 ResettableExitPhase exit, 247 ResettablePhases *parent_phases); 248 249 #endif 250