1d66cc84cSDamien Hedde 2d66cc84cSDamien Hedde======================================= 3d66cc84cSDamien HeddeReset in QEMU: the Resettable interface 4d66cc84cSDamien Hedde======================================= 5d66cc84cSDamien Hedde 6d66cc84cSDamien HeddeThe reset of qemu objects is handled using the resettable interface declared 7d66cc84cSDamien Heddein ``include/hw/resettable.h``. 8d66cc84cSDamien Hedde 9d66cc84cSDamien HeddeThis interface allows objects to be grouped (on a tree basis); so that the 10d66cc84cSDamien Heddewhole group can be reset consistently. Each individual member object does not 11d66cc84cSDamien Heddehave to care about others; in particular, problems of order (which object is 12d66cc84cSDamien Heddereset first) are addressed. 13d66cc84cSDamien Hedde 14d66cc84cSDamien HeddeAs of now DeviceClass and BusClass implement this interface. 15d66cc84cSDamien Hedde 16d66cc84cSDamien Hedde 17d66cc84cSDamien HeddeTriggering reset 18d66cc84cSDamien Hedde---------------- 19d66cc84cSDamien Hedde 20d66cc84cSDamien HeddeThis section documents the APIs which "users" of a resettable object should use 21d66cc84cSDamien Heddeto control it. All resettable control functions must be called while holding 22*a4a411fbSStefan Hajnoczithe BQL. 23d66cc84cSDamien Hedde 24d66cc84cSDamien HeddeYou can apply a reset to an object using ``resettable_assert_reset()``. You need 25d66cc84cSDamien Heddeto call ``resettable_release_reset()`` to release the object from reset. To 26d66cc84cSDamien Heddeinstantly reset an object, without keeping it in reset state, just call 27d66cc84cSDamien Hedde``resettable_reset()``. These functions take two parameters: a pointer to the 28d66cc84cSDamien Heddeobject to reset and a reset type. 29d66cc84cSDamien Hedde 30d66cc84cSDamien HeddeSeveral types of reset will be supported. For now only cold reset is defined; 31d66cc84cSDamien Heddeothers may be added later. The Resettable interface handles reset types with an 32d66cc84cSDamien Heddeenum: 33d66cc84cSDamien Hedde 34d66cc84cSDamien Hedde``RESET_TYPE_COLD`` 35d66cc84cSDamien Hedde Cold reset is supported by every resettable object. In QEMU, it means we reset 36d66cc84cSDamien Hedde to the initial state corresponding to the start of QEMU; this might differ 37d66cc84cSDamien Hedde from what is a real hardware cold reset. It differs from other resets (like 38d66cc84cSDamien Hedde warm or bus resets) which may keep certain parts untouched. 39d66cc84cSDamien Hedde 40d66cc84cSDamien HeddeCalling ``resettable_reset()`` is equivalent to calling 41d66cc84cSDamien Hedde``resettable_assert_reset()`` then ``resettable_release_reset()``. It is 42d66cc84cSDamien Heddepossible to interleave multiple calls to these three functions. There may 43d66cc84cSDamien Heddebe several reset sources/controllers of a given object. The interface handles 44d66cc84cSDamien Heddeeverything and the different reset controllers do not need to know anything 45d66cc84cSDamien Heddeabout each others. The object will leave reset state only when each other 46d66cc84cSDamien Heddecontrollers end their reset operation. This point is handled internally by 47d66cc84cSDamien Heddemaintaining a count of in-progress resets; it is crucial to call 48d66cc84cSDamien Hedde``resettable_release_reset()`` one time and only one time per 49d66cc84cSDamien Hedde``resettable_assert_reset()`` call. 50d66cc84cSDamien Hedde 51d66cc84cSDamien HeddeFor now migration of a device or bus in reset is not supported. Care must be 52d66cc84cSDamien Heddetaken not to delay ``resettable_release_reset()`` after its 53d66cc84cSDamien Hedde``resettable_assert_reset()`` counterpart. 54d66cc84cSDamien Hedde 55d66cc84cSDamien HeddeNote that, since resettable is an interface, the API takes a simple Object as 56d66cc84cSDamien Heddeparameter. Still, it is a programming error to call a resettable function on a 57d66cc84cSDamien Heddenon-resettable object and it will trigger a run time assert error. Since most 58d66cc84cSDamien Heddecalls to resettable interface are done through base class functions, such an 59d66cc84cSDamien Heddeerror is not likely to happen. 60d66cc84cSDamien Hedde 61d66cc84cSDamien HeddeFor Devices and Buses, the following helper functions exist: 62d66cc84cSDamien Hedde 63d66cc84cSDamien Hedde- ``device_cold_reset()`` 64d66cc84cSDamien Hedde- ``bus_cold_reset()`` 65d66cc84cSDamien Hedde 66d66cc84cSDamien HeddeThese are simple wrappers around resettable_reset() function; they only cast the 67d66cc84cSDamien HeddeDevice or Bus into an Object and pass the cold reset type. When possible 68d66cc84cSDamien Heddeprefer to use these functions instead of ``resettable_reset()``. 69d66cc84cSDamien Hedde 70d66cc84cSDamien HeddeDevice and bus functions co-exist because there can be semantic differences 71d66cc84cSDamien Heddebetween resetting a bus and resetting the controller bridge which owns it. 72d66cc84cSDamien HeddeFor example, consider a SCSI controller. Resetting the controller puts all 73d66cc84cSDamien Heddeits registers back to what reset state was as well as reset everything on the 74d66cc84cSDamien HeddeSCSI bus, whereas resetting just the SCSI bus only resets everything that's on 75d66cc84cSDamien Heddeit but not the controller. 76d66cc84cSDamien Hedde 77d66cc84cSDamien Hedde 78d66cc84cSDamien HeddeMulti-phase mechanism 79d66cc84cSDamien Hedde--------------------- 80d66cc84cSDamien Hedde 81d66cc84cSDamien HeddeThis section documents the internals of the resettable interface. 82d66cc84cSDamien Hedde 83d66cc84cSDamien HeddeThe resettable interface uses a multi-phase system to relieve objects and 84d66cc84cSDamien Heddemachines from reset ordering problems. To address this, the reset operation 85d66cc84cSDamien Heddeof an object is split into three well defined phases. 86d66cc84cSDamien Hedde 87d66cc84cSDamien HeddeWhen resetting several objects (for example the whole machine at simulation 88d66cc84cSDamien Heddestartup), all first phases of all objects are executed, then all second phases 89d66cc84cSDamien Heddeand then all third phases. 90d66cc84cSDamien Hedde 91d66cc84cSDamien HeddeThe three phases are: 92d66cc84cSDamien Hedde 93d66cc84cSDamien Hedde1. The **enter** phase is executed when the object enters reset. It resets only 94d66cc84cSDamien Hedde local state of the object; it must not do anything that has a side-effect 95d66cc84cSDamien Hedde on other objects, such as raising or lowering a qemu_irq line or reading or 96d66cc84cSDamien Hedde writing guest memory. 97d66cc84cSDamien Hedde 98d66cc84cSDamien Hedde2. The **hold** phase is executed for entry into reset, once every object in the 99d66cc84cSDamien Hedde group which is being reset has had its *enter* phase executed. At this point 100d66cc84cSDamien Hedde devices can do actions that affect other objects. 101d66cc84cSDamien Hedde 102d66cc84cSDamien Hedde3. The **exit** phase is executed when the object leaves the reset state. 103d66cc84cSDamien Hedde Actions affecting other objects are permitted. 104d66cc84cSDamien Hedde 105d66cc84cSDamien HeddeAs said in previous section, the interface maintains a count of reset. This 106d66cc84cSDamien Heddecount is used to ensure phases are executed only when required. *enter* and 107d66cc84cSDamien Hedde*hold* phases are executed only when asserting reset for the first time 108d66cc84cSDamien Hedde(if an object is already in reset state when calling 109d66cc84cSDamien Hedde``resettable_assert_reset()`` or ``resettable_reset()``, they are not 110d66cc84cSDamien Heddeexecuted). 111d66cc84cSDamien HeddeThe *exit* phase is executed only when the last reset operation ends. Therefore 112d66cc84cSDamien Heddethe object does not need to care how many of reset controllers it has and how 113d66cc84cSDamien Heddemany of them have started a reset. 114d66cc84cSDamien Hedde 115d66cc84cSDamien Hedde 116d66cc84cSDamien HeddeHandling reset in a resettable object 117d66cc84cSDamien Hedde------------------------------------- 118d66cc84cSDamien Hedde 119d66cc84cSDamien HeddeThis section documents the APIs that an implementation of a resettable object 120d66cc84cSDamien Heddemust provide and what functions it has access to. It is intended for people 121d66cc84cSDamien Heddewho want to implement or convert a class which has the resettable interface; 122d66cc84cSDamien Heddefor example when specializing an existing device or bus. 123d66cc84cSDamien Hedde 124d66cc84cSDamien HeddeMethods to implement 125d66cc84cSDamien Hedde.................... 126d66cc84cSDamien Hedde 127d66cc84cSDamien HeddeThree methods should be defined or left empty. Each method corresponds to a 128d66cc84cSDamien Heddephase of the reset; they are name ``phases.enter()``, ``phases.hold()`` and 129d66cc84cSDamien Hedde``phases.exit()``. They all take the object as parameter. The *enter* method 130d66cc84cSDamien Heddealso take the reset type as second parameter. 131d66cc84cSDamien Hedde 132d66cc84cSDamien HeddeWhen extending an existing class, these methods may need to be extended too. 133d66cc84cSDamien HeddeThe ``resettable_class_set_parent_phases()`` class function may be used to 134d66cc84cSDamien Heddebackup parent class methods. 135d66cc84cSDamien Hedde 136d66cc84cSDamien HeddeHere follows an example to implement reset for a Device which sets an IO while 137d66cc84cSDamien Heddein reset. 138d66cc84cSDamien Hedde 139d66cc84cSDamien Hedde:: 140d66cc84cSDamien Hedde 141d66cc84cSDamien Hedde static void mydev_reset_enter(Object *obj, ResetType type) 142d66cc84cSDamien Hedde { 143d66cc84cSDamien Hedde MyDevClass *myclass = MYDEV_GET_CLASS(obj); 144d66cc84cSDamien Hedde MyDevState *mydev = MYDEV(obj); 145d66cc84cSDamien Hedde /* call parent class enter phase */ 146d66cc84cSDamien Hedde if (myclass->parent_phases.enter) { 147d66cc84cSDamien Hedde myclass->parent_phases.enter(obj, type); 148d66cc84cSDamien Hedde } 149d66cc84cSDamien Hedde /* initialize local state only */ 150d66cc84cSDamien Hedde mydev->var = 0; 151d66cc84cSDamien Hedde } 152d66cc84cSDamien Hedde 153d66cc84cSDamien Hedde static void mydev_reset_hold(Object *obj) 154d66cc84cSDamien Hedde { 155d66cc84cSDamien Hedde MyDevClass *myclass = MYDEV_GET_CLASS(obj); 156d66cc84cSDamien Hedde MyDevState *mydev = MYDEV(obj); 157d66cc84cSDamien Hedde /* call parent class hold phase */ 158d66cc84cSDamien Hedde if (myclass->parent_phases.hold) { 159d66cc84cSDamien Hedde myclass->parent_phases.hold(obj); 160d66cc84cSDamien Hedde } 161d66cc84cSDamien Hedde /* set an IO */ 162d66cc84cSDamien Hedde qemu_set_irq(mydev->irq, 1); 163d66cc84cSDamien Hedde } 164d66cc84cSDamien Hedde 165d66cc84cSDamien Hedde static void mydev_reset_exit(Object *obj) 166d66cc84cSDamien Hedde { 167d66cc84cSDamien Hedde MyDevClass *myclass = MYDEV_GET_CLASS(obj); 168d66cc84cSDamien Hedde MyDevState *mydev = MYDEV(obj); 169d66cc84cSDamien Hedde /* call parent class exit phase */ 170d66cc84cSDamien Hedde if (myclass->parent_phases.exit) { 171d66cc84cSDamien Hedde myclass->parent_phases.exit(obj); 172d66cc84cSDamien Hedde } 173d66cc84cSDamien Hedde /* clear an IO */ 174d66cc84cSDamien Hedde qemu_set_irq(mydev->irq, 0); 175d66cc84cSDamien Hedde } 176d66cc84cSDamien Hedde 177d66cc84cSDamien Hedde typedef struct MyDevClass { 178d66cc84cSDamien Hedde MyParentClass parent_class; 179d66cc84cSDamien Hedde /* to store eventual parent reset methods */ 180d66cc84cSDamien Hedde ResettablePhases parent_phases; 181d66cc84cSDamien Hedde } MyDevClass; 182d66cc84cSDamien Hedde 183d66cc84cSDamien Hedde static void mydev_class_init(ObjectClass *class, void *data) 184d66cc84cSDamien Hedde { 185d66cc84cSDamien Hedde MyDevClass *myclass = MYDEV_CLASS(class); 186d66cc84cSDamien Hedde ResettableClass *rc = RESETTABLE_CLASS(class); 187fa365d05SAkihiko Odaki resettable_class_set_parent_phases(rc, 188d66cc84cSDamien Hedde mydev_reset_enter, 189d66cc84cSDamien Hedde mydev_reset_hold, 190d66cc84cSDamien Hedde mydev_reset_exit, 191d66cc84cSDamien Hedde &myclass->parent_phases); 192d66cc84cSDamien Hedde } 193d66cc84cSDamien Hedde 194d66cc84cSDamien HeddeIn the above example, we override all three phases. It is possible to override 195d66cc84cSDamien Heddeonly some of them by passing NULL instead of a function pointer to 196fa365d05SAkihiko Odaki``resettable_class_set_parent_phases()``. For example, the following will 197d66cc84cSDamien Heddeonly override the *enter* phase and leave *hold* and *exit* untouched:: 198d66cc84cSDamien Hedde 199fa365d05SAkihiko Odaki resettable_class_set_parent_phases(rc, mydev_reset_enter, NULL, NULL, 200d66cc84cSDamien Hedde &myclass->parent_phases); 201d66cc84cSDamien Hedde 202d66cc84cSDamien HeddeThis is equivalent to providing a trivial implementation of the hold and exit 203d66cc84cSDamien Heddephases which does nothing but call the parent class's implementation of the 204d66cc84cSDamien Heddephase. 205d66cc84cSDamien Hedde 206d66cc84cSDamien HeddePolling the reset state 207d66cc84cSDamien Hedde....................... 208d66cc84cSDamien Hedde 209d66cc84cSDamien HeddeResettable interface provides the ``resettable_is_in_reset()`` function. 210d66cc84cSDamien HeddeThis function returns true if the object parameter is currently under reset. 211d66cc84cSDamien Hedde 212310616d3SDamien HeddeAn object is under reset from the beginning of the *enter* phase (before 213310616d3SDamien Heddeeither its children or its own enter method is called) to the *exit* 214310616d3SDamien Heddephase. During *enter* and *hold* phase only, the function will return that the 215310616d3SDamien Heddeobject is in reset. The state is changed after the *exit* is propagated to 216310616d3SDamien Heddeits children and just before calling the object's own *exit* method. 217d66cc84cSDamien Hedde 218d66cc84cSDamien HeddeThis function may be used if the object behavior has to be adapted 219d66cc84cSDamien Heddewhile in reset state. For example if a device has an irq input, 220d66cc84cSDamien Heddeit will probably need to ignore it while in reset; then it can for 221d66cc84cSDamien Heddeexample check the reset state at the beginning of the irq callback. 222d66cc84cSDamien Hedde 223d66cc84cSDamien HeddeNote that until migration of the reset state is supported, an object 224d66cc84cSDamien Heddeshould not be left in reset. So apart from being currently executing 225d66cc84cSDamien Heddeone of the reset phases, the only cases when this function will return 226d66cc84cSDamien Heddetrue is if an external interaction (like changing an io) is made during 227d66cc84cSDamien Hedde*hold* or *exit* phase of another object in the same reset group. 228d66cc84cSDamien Hedde 229d66cc84cSDamien HeddeHelpers ``device_is_in_reset()`` and ``bus_is_in_reset()`` are also provided 230d66cc84cSDamien Heddefor devices and buses and should be preferred. 231d66cc84cSDamien Hedde 232d66cc84cSDamien Hedde 233d66cc84cSDamien HeddeBase class handling of reset 234d66cc84cSDamien Hedde---------------------------- 235d66cc84cSDamien Hedde 236d66cc84cSDamien HeddeThis section documents parts of the reset mechanism that you only need to know 237d66cc84cSDamien Heddeabout if you are extending it to work with a new base class other than 238d66cc84cSDamien HeddeDeviceClass or BusClass, or maintaining the existing code in those classes. Most 239d66cc84cSDamien Heddepeople can ignore it. 240d66cc84cSDamien Hedde 241d66cc84cSDamien HeddeMethods to implement 242d66cc84cSDamien Hedde.................... 243d66cc84cSDamien Hedde 244d66cc84cSDamien HeddeThere are two other methods that need to exist in a class implementing the 245d66cc84cSDamien Heddeinterface: ``get_state()`` and ``child_foreach()``. 246d66cc84cSDamien Hedde 247d66cc84cSDamien Hedde``get_state()`` is simple. *resettable* is an interface and, as a consequence, 248d66cc84cSDamien Heddedoes not have any class state structure. But in order to factorize the code, we 249d66cc84cSDamien Heddeneed one. This method must return a pointer to ``ResettableState`` structure. 250d66cc84cSDamien HeddeThe structure must be allocated by the base class; preferably it should be 251d66cc84cSDamien Heddelocated inside the object instance structure. 252d66cc84cSDamien Hedde 253d66cc84cSDamien Hedde``child_foreach()`` is more complex. It should execute the given callback on 254d66cc84cSDamien Heddeevery reset child of the given resettable object. All children must be 255d66cc84cSDamien Hedderesettable too. Additional parameters (a reset type and an opaque pointer) must 256d66cc84cSDamien Heddebe passed to the callback too. 257d66cc84cSDamien Hedde 258d66cc84cSDamien HeddeIn ``DeviceClass`` and ``BusClass`` the ``ResettableState`` is located 259d66cc84cSDamien Hedde``DeviceState`` and ``BusState`` structure. ``child_foreach()`` is implemented 260d66cc84cSDamien Heddeto follow the bus hierarchy; for a bus, it calls the function on every child 261d66cc84cSDamien Heddedevice; for a device, it calls the function on every bus child. When we reset 262d66cc84cSDamien Heddethe main system bus, we reset the whole machine bus tree. 263d66cc84cSDamien Hedde 264d66cc84cSDamien HeddeChanging a resettable parent 265d66cc84cSDamien Hedde............................ 266d66cc84cSDamien Hedde 267d66cc84cSDamien HeddeOne thing which should be taken care of by the base class is handling reset 268d66cc84cSDamien Heddehierarchy changes. 269d66cc84cSDamien Hedde 270d66cc84cSDamien HeddeThe reset hierarchy is supposed to be static and built during machine creation. 271d66cc84cSDamien HeddeBut there are actually some exceptions. To cope with this, the resettable API 272d66cc84cSDamien Heddeprovides ``resettable_change_parent()``. This function allows to set, update or 273d66cc84cSDamien Hedderemove the parent of a resettable object after machine creation is done. As 274d66cc84cSDamien Heddeparameters, it takes the object being moved, the old parent if any and the new 275d66cc84cSDamien Heddeparent if any. 276d66cc84cSDamien Hedde 277d66cc84cSDamien HeddeThis function can be used at any time when not in a reset operation. During 278d66cc84cSDamien Heddea reset operation it must be used only in *hold* phase. Using it in *enter* or 279d66cc84cSDamien Hedde*exit* phase is an error. 280d66cc84cSDamien HeddeAlso it should not be used during machine creation, although it is harmless to 281d66cc84cSDamien Heddedo so: the function is a no-op as long as old and new parent are NULL or not 282d66cc84cSDamien Heddein reset. 283d66cc84cSDamien Hedde 284d66cc84cSDamien HeddeThere is currently 2 cases where this function is used: 285d66cc84cSDamien Hedde 286d66cc84cSDamien Hedde1. *device hotplug*; it means a new device is introduced on a live bus. 287d66cc84cSDamien Hedde 288d66cc84cSDamien Hedde2. *hot bus change*; it means an existing live device is added, moved or 289d66cc84cSDamien Hedde removed in the bus hierarchy. At the moment, it occurs only in the raspi 290d66cc84cSDamien Hedde machines for changing the sdbus used by sd card. 291