Damien Hedde | d66cc84 | 2020-01-30 16:02:05 +0000 | [diff] [blame] | 1 | |
| 2 | ======================================= |
| 3 | Reset in QEMU: the Resettable interface |
| 4 | ======================================= |
| 5 | |
| 6 | The reset of qemu objects is handled using the resettable interface declared |
| 7 | in ``include/hw/resettable.h``. |
| 8 | |
| 9 | This interface allows objects to be grouped (on a tree basis); so that the |
| 10 | whole group can be reset consistently. Each individual member object does not |
| 11 | have to care about others; in particular, problems of order (which object is |
| 12 | reset first) are addressed. |
| 13 | |
Peter Maydell | a365572 | 2024-02-20 16:06:22 +0000 | [diff] [blame] | 14 | The main object types which implement this interface are DeviceClass |
| 15 | and BusClass. |
Damien Hedde | d66cc84 | 2020-01-30 16:02:05 +0000 | [diff] [blame] | 16 | |
| 17 | Triggering reset |
| 18 | ---------------- |
| 19 | |
| 20 | This section documents the APIs which "users" of a resettable object should use |
| 21 | to control it. All resettable control functions must be called while holding |
Stefan Hajnoczi | a4a411f | 2024-01-02 10:35:28 -0500 | [diff] [blame] | 22 | the BQL. |
Damien Hedde | d66cc84 | 2020-01-30 16:02:05 +0000 | [diff] [blame] | 23 | |
| 24 | You can apply a reset to an object using ``resettable_assert_reset()``. You need |
| 25 | to call ``resettable_release_reset()`` to release the object from reset. To |
| 26 | instantly reset an object, without keeping it in reset state, just call |
| 27 | ``resettable_reset()``. These functions take two parameters: a pointer to the |
| 28 | object to reset and a reset type. |
| 29 | |
Peter Maydell | 631f46d | 2024-04-12 17:08:09 +0100 | [diff] [blame] | 30 | The Resettable interface handles reset types with an enum ``ResetType``: |
Damien Hedde | d66cc84 | 2020-01-30 16:02:05 +0000 | [diff] [blame] | 31 | |
| 32 | ``RESET_TYPE_COLD`` |
| 33 | Cold reset is supported by every resettable object. In QEMU, it means we reset |
| 34 | to the initial state corresponding to the start of QEMU; this might differ |
| 35 | from what is a real hardware cold reset. It differs from other resets (like |
| 36 | warm or bus resets) which may keep certain parts untouched. |
| 37 | |
Peter Maydell | 631f46d | 2024-04-12 17:08:09 +0100 | [diff] [blame] | 38 | ``RESET_TYPE_SNAPSHOT_LOAD`` |
| 39 | This is called for a reset which is being done to put the system into a |
| 40 | clean state prior to loading a snapshot. (This corresponds to a reset |
| 41 | with ``SHUTDOWN_CAUSE_SNAPSHOT_LOAD``.) Almost all devices should treat |
| 42 | this the same as ``RESET_TYPE_COLD``. The main exception is devices which |
| 43 | have some non-deterministic state they want to reinitialize to a different |
| 44 | value on each cold reset, such as RNG seed information, and which they |
| 45 | must not reinitialize on a snapshot-load reset. |
| 46 | |
Peter Maydell | cf7f61d | 2024-09-13 15:31:43 +0100 | [diff] [blame] | 47 | ``RESET_TYPE_S390_CPU_NORMAL`` |
| 48 | This is only used for S390 CPU objects; it clears interrupts, stops |
| 49 | processing, and clears the TLB, but does not touch register contents. |
| 50 | |
| 51 | ``RESET_TYPE_S390_CPU_INITIAL`` |
| 52 | This is only used for S390 CPU objects; it does everything |
| 53 | ``RESET_TYPE_S390_CPU_NORMAL`` does and also clears the PSW, prefix, |
| 54 | FPC, timer and control registers. It does not touch gprs, fprs or acrs. |
| 55 | |
| 56 | |
Peter Maydell | 631f46d | 2024-04-12 17:08:09 +0100 | [diff] [blame] | 57 | Devices which implement reset methods must treat any unknown ``ResetType`` |
| 58 | as equivalent to ``RESET_TYPE_COLD``; this will reduce the amount of |
| 59 | existing code we need to change if we add more types in future. |
| 60 | |
Damien Hedde | d66cc84 | 2020-01-30 16:02:05 +0000 | [diff] [blame] | 61 | Calling ``resettable_reset()`` is equivalent to calling |
| 62 | ``resettable_assert_reset()`` then ``resettable_release_reset()``. It is |
| 63 | possible to interleave multiple calls to these three functions. There may |
| 64 | be several reset sources/controllers of a given object. The interface handles |
| 65 | everything and the different reset controllers do not need to know anything |
| 66 | about each others. The object will leave reset state only when each other |
| 67 | controllers end their reset operation. This point is handled internally by |
| 68 | maintaining a count of in-progress resets; it is crucial to call |
| 69 | ``resettable_release_reset()`` one time and only one time per |
| 70 | ``resettable_assert_reset()`` call. |
| 71 | |
| 72 | For now migration of a device or bus in reset is not supported. Care must be |
| 73 | taken not to delay ``resettable_release_reset()`` after its |
| 74 | ``resettable_assert_reset()`` counterpart. |
| 75 | |
| 76 | Note that, since resettable is an interface, the API takes a simple Object as |
| 77 | parameter. Still, it is a programming error to call a resettable function on a |
| 78 | non-resettable object and it will trigger a run time assert error. Since most |
| 79 | calls to resettable interface are done through base class functions, such an |
| 80 | error is not likely to happen. |
| 81 | |
| 82 | For Devices and Buses, the following helper functions exist: |
| 83 | |
| 84 | - ``device_cold_reset()`` |
| 85 | - ``bus_cold_reset()`` |
| 86 | |
| 87 | These are simple wrappers around resettable_reset() function; they only cast the |
| 88 | Device or Bus into an Object and pass the cold reset type. When possible |
| 89 | prefer to use these functions instead of ``resettable_reset()``. |
| 90 | |
| 91 | Device and bus functions co-exist because there can be semantic differences |
| 92 | between resetting a bus and resetting the controller bridge which owns it. |
| 93 | For example, consider a SCSI controller. Resetting the controller puts all |
| 94 | its registers back to what reset state was as well as reset everything on the |
| 95 | SCSI bus, whereas resetting just the SCSI bus only resets everything that's on |
| 96 | it but not the controller. |
| 97 | |
| 98 | |
| 99 | Multi-phase mechanism |
| 100 | --------------------- |
| 101 | |
| 102 | This section documents the internals of the resettable interface. |
| 103 | |
| 104 | The resettable interface uses a multi-phase system to relieve objects and |
| 105 | machines from reset ordering problems. To address this, the reset operation |
| 106 | of an object is split into three well defined phases. |
| 107 | |
| 108 | When resetting several objects (for example the whole machine at simulation |
| 109 | startup), all first phases of all objects are executed, then all second phases |
| 110 | and then all third phases. |
| 111 | |
| 112 | The three phases are: |
| 113 | |
| 114 | 1. The **enter** phase is executed when the object enters reset. It resets only |
| 115 | local state of the object; it must not do anything that has a side-effect |
| 116 | on other objects, such as raising or lowering a qemu_irq line or reading or |
| 117 | writing guest memory. |
| 118 | |
| 119 | 2. The **hold** phase is executed for entry into reset, once every object in the |
| 120 | group which is being reset has had its *enter* phase executed. At this point |
| 121 | devices can do actions that affect other objects. |
| 122 | |
| 123 | 3. The **exit** phase is executed when the object leaves the reset state. |
| 124 | Actions affecting other objects are permitted. |
| 125 | |
| 126 | As said in previous section, the interface maintains a count of reset. This |
| 127 | count is used to ensure phases are executed only when required. *enter* and |
| 128 | *hold* phases are executed only when asserting reset for the first time |
| 129 | (if an object is already in reset state when calling |
| 130 | ``resettable_assert_reset()`` or ``resettable_reset()``, they are not |
| 131 | executed). |
| 132 | The *exit* phase is executed only when the last reset operation ends. Therefore |
| 133 | the object does not need to care how many of reset controllers it has and how |
| 134 | many of them have started a reset. |
| 135 | |
| 136 | |
| 137 | Handling reset in a resettable object |
| 138 | ------------------------------------- |
| 139 | |
| 140 | This section documents the APIs that an implementation of a resettable object |
| 141 | must provide and what functions it has access to. It is intended for people |
| 142 | who want to implement or convert a class which has the resettable interface; |
| 143 | for example when specializing an existing device or bus. |
| 144 | |
| 145 | Methods to implement |
| 146 | .................... |
| 147 | |
| 148 | Three methods should be defined or left empty. Each method corresponds to a |
| 149 | phase of the reset; they are name ``phases.enter()``, ``phases.hold()`` and |
| 150 | ``phases.exit()``. They all take the object as parameter. The *enter* method |
| 151 | also take the reset type as second parameter. |
| 152 | |
| 153 | When extending an existing class, these methods may need to be extended too. |
| 154 | The ``resettable_class_set_parent_phases()`` class function may be used to |
| 155 | backup parent class methods. |
| 156 | |
| 157 | Here follows an example to implement reset for a Device which sets an IO while |
| 158 | in reset. |
| 159 | |
| 160 | :: |
| 161 | |
| 162 | static void mydev_reset_enter(Object *obj, ResetType type) |
| 163 | { |
| 164 | MyDevClass *myclass = MYDEV_GET_CLASS(obj); |
| 165 | MyDevState *mydev = MYDEV(obj); |
| 166 | /* call parent class enter phase */ |
| 167 | if (myclass->parent_phases.enter) { |
| 168 | myclass->parent_phases.enter(obj, type); |
| 169 | } |
| 170 | /* initialize local state only */ |
| 171 | mydev->var = 0; |
| 172 | } |
| 173 | |
Peter Maydell | 41d49ec | 2024-04-12 17:08:08 +0100 | [diff] [blame] | 174 | static void mydev_reset_hold(Object *obj, ResetType type) |
Damien Hedde | d66cc84 | 2020-01-30 16:02:05 +0000 | [diff] [blame] | 175 | { |
| 176 | MyDevClass *myclass = MYDEV_GET_CLASS(obj); |
| 177 | MyDevState *mydev = MYDEV(obj); |
| 178 | /* call parent class hold phase */ |
| 179 | if (myclass->parent_phases.hold) { |
Peter Maydell | 41d49ec | 2024-04-12 17:08:08 +0100 | [diff] [blame] | 180 | myclass->parent_phases.hold(obj, type); |
Damien Hedde | d66cc84 | 2020-01-30 16:02:05 +0000 | [diff] [blame] | 181 | } |
| 182 | /* set an IO */ |
| 183 | qemu_set_irq(mydev->irq, 1); |
| 184 | } |
| 185 | |
Peter Maydell | 41d49ec | 2024-04-12 17:08:08 +0100 | [diff] [blame] | 186 | static void mydev_reset_exit(Object *obj, ResetType type) |
Damien Hedde | d66cc84 | 2020-01-30 16:02:05 +0000 | [diff] [blame] | 187 | { |
| 188 | MyDevClass *myclass = MYDEV_GET_CLASS(obj); |
| 189 | MyDevState *mydev = MYDEV(obj); |
| 190 | /* call parent class exit phase */ |
| 191 | if (myclass->parent_phases.exit) { |
Peter Maydell | 41d49ec | 2024-04-12 17:08:08 +0100 | [diff] [blame] | 192 | myclass->parent_phases.exit(obj, type); |
Damien Hedde | d66cc84 | 2020-01-30 16:02:05 +0000 | [diff] [blame] | 193 | } |
| 194 | /* clear an IO */ |
| 195 | qemu_set_irq(mydev->irq, 0); |
| 196 | } |
| 197 | |
| 198 | typedef struct MyDevClass { |
| 199 | MyParentClass parent_class; |
| 200 | /* to store eventual parent reset methods */ |
| 201 | ResettablePhases parent_phases; |
| 202 | } MyDevClass; |
| 203 | |
| 204 | static void mydev_class_init(ObjectClass *class, void *data) |
| 205 | { |
| 206 | MyDevClass *myclass = MYDEV_CLASS(class); |
| 207 | ResettableClass *rc = RESETTABLE_CLASS(class); |
Akihiko Odaki | fa365d0 | 2022-11-25 23:06:45 +0900 | [diff] [blame] | 208 | resettable_class_set_parent_phases(rc, |
| 209 | mydev_reset_enter, |
| 210 | mydev_reset_hold, |
| 211 | mydev_reset_exit, |
| 212 | &myclass->parent_phases); |
Damien Hedde | d66cc84 | 2020-01-30 16:02:05 +0000 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | In the above example, we override all three phases. It is possible to override |
| 216 | only some of them by passing NULL instead of a function pointer to |
Akihiko Odaki | fa365d0 | 2022-11-25 23:06:45 +0900 | [diff] [blame] | 217 | ``resettable_class_set_parent_phases()``. For example, the following will |
Damien Hedde | d66cc84 | 2020-01-30 16:02:05 +0000 | [diff] [blame] | 218 | only override the *enter* phase and leave *hold* and *exit* untouched:: |
| 219 | |
Akihiko Odaki | fa365d0 | 2022-11-25 23:06:45 +0900 | [diff] [blame] | 220 | resettable_class_set_parent_phases(rc, mydev_reset_enter, NULL, NULL, |
| 221 | &myclass->parent_phases); |
Damien Hedde | d66cc84 | 2020-01-30 16:02:05 +0000 | [diff] [blame] | 222 | |
| 223 | This is equivalent to providing a trivial implementation of the hold and exit |
| 224 | phases which does nothing but call the parent class's implementation of the |
| 225 | phase. |
| 226 | |
| 227 | Polling the reset state |
| 228 | ....................... |
| 229 | |
| 230 | Resettable interface provides the ``resettable_is_in_reset()`` function. |
| 231 | This function returns true if the object parameter is currently under reset. |
| 232 | |
Damien Hedde | 310616d | 2022-10-20 15:27:49 +0100 | [diff] [blame] | 233 | An object is under reset from the beginning of the *enter* phase (before |
| 234 | either its children or its own enter method is called) to the *exit* |
| 235 | phase. During *enter* and *hold* phase only, the function will return that the |
| 236 | object is in reset. The state is changed after the *exit* is propagated to |
| 237 | its children and just before calling the object's own *exit* method. |
Damien Hedde | d66cc84 | 2020-01-30 16:02:05 +0000 | [diff] [blame] | 238 | |
| 239 | This function may be used if the object behavior has to be adapted |
| 240 | while in reset state. For example if a device has an irq input, |
| 241 | it will probably need to ignore it while in reset; then it can for |
| 242 | example check the reset state at the beginning of the irq callback. |
| 243 | |
| 244 | Note that until migration of the reset state is supported, an object |
| 245 | should not be left in reset. So apart from being currently executing |
| 246 | one of the reset phases, the only cases when this function will return |
| 247 | true is if an external interaction (like changing an io) is made during |
| 248 | *hold* or *exit* phase of another object in the same reset group. |
| 249 | |
| 250 | Helpers ``device_is_in_reset()`` and ``bus_is_in_reset()`` are also provided |
| 251 | for devices and buses and should be preferred. |
| 252 | |
| 253 | |
| 254 | Base class handling of reset |
| 255 | ---------------------------- |
| 256 | |
| 257 | This section documents parts of the reset mechanism that you only need to know |
| 258 | about if you are extending it to work with a new base class other than |
| 259 | DeviceClass or BusClass, or maintaining the existing code in those classes. Most |
| 260 | people can ignore it. |
| 261 | |
| 262 | Methods to implement |
| 263 | .................... |
| 264 | |
| 265 | There are two other methods that need to exist in a class implementing the |
| 266 | interface: ``get_state()`` and ``child_foreach()``. |
| 267 | |
| 268 | ``get_state()`` is simple. *resettable* is an interface and, as a consequence, |
| 269 | does not have any class state structure. But in order to factorize the code, we |
| 270 | need one. This method must return a pointer to ``ResettableState`` structure. |
| 271 | The structure must be allocated by the base class; preferably it should be |
| 272 | located inside the object instance structure. |
| 273 | |
| 274 | ``child_foreach()`` is more complex. It should execute the given callback on |
| 275 | every reset child of the given resettable object. All children must be |
| 276 | resettable too. Additional parameters (a reset type and an opaque pointer) must |
| 277 | be passed to the callback too. |
| 278 | |
| 279 | In ``DeviceClass`` and ``BusClass`` the ``ResettableState`` is located |
| 280 | ``DeviceState`` and ``BusState`` structure. ``child_foreach()`` is implemented |
| 281 | to follow the bus hierarchy; for a bus, it calls the function on every child |
| 282 | device; for a device, it calls the function on every bus child. When we reset |
| 283 | the main system bus, we reset the whole machine bus tree. |
| 284 | |
| 285 | Changing a resettable parent |
| 286 | ............................ |
| 287 | |
| 288 | One thing which should be taken care of by the base class is handling reset |
| 289 | hierarchy changes. |
| 290 | |
| 291 | The reset hierarchy is supposed to be static and built during machine creation. |
| 292 | But there are actually some exceptions. To cope with this, the resettable API |
| 293 | provides ``resettable_change_parent()``. This function allows to set, update or |
| 294 | remove the parent of a resettable object after machine creation is done. As |
| 295 | parameters, it takes the object being moved, the old parent if any and the new |
| 296 | parent if any. |
| 297 | |
| 298 | This function can be used at any time when not in a reset operation. During |
| 299 | a reset operation it must be used only in *hold* phase. Using it in *enter* or |
| 300 | *exit* phase is an error. |
| 301 | Also it should not be used during machine creation, although it is harmless to |
| 302 | do so: the function is a no-op as long as old and new parent are NULL or not |
| 303 | in reset. |
| 304 | |
| 305 | There is currently 2 cases where this function is used: |
| 306 | |
| 307 | 1. *device hotplug*; it means a new device is introduced on a live bus. |
| 308 | |
| 309 | 2. *hot bus change*; it means an existing live device is added, moved or |
| 310 | removed in the bus hierarchy. At the moment, it occurs only in the raspi |
| 311 | machines for changing the sdbus used by sd card. |
Peter Maydell | a365572 | 2024-02-20 16:06:22 +0000 | [diff] [blame] | 312 | |
| 313 | Reset of the complete system |
| 314 | ---------------------------- |
| 315 | |
| 316 | Reset of the complete system is a little complicated. The typical |
| 317 | flow is: |
| 318 | |
| 319 | 1. Code which wishes to reset the entire system does so by calling |
| 320 | ``qemu_system_reset_request()``. This schedules a reset, but the |
| 321 | reset will happen asynchronously after the function returns. |
| 322 | That makes this safe to call from, for example, device models. |
| 323 | |
| 324 | 2. The function which is called to make the reset happen is |
| 325 | ``qemu_system_reset()``. Generally only core system code should |
| 326 | call this directly. |
| 327 | |
| 328 | 3. ``qemu_system_reset()`` calls the ``MachineClass::reset`` method of |
| 329 | the current machine, if it has one. That method must call |
| 330 | ``qemu_devices_reset()``. If the machine has no reset method, |
| 331 | ``qemu_system_reset()`` calls ``qemu_devices_reset()`` directly. |
| 332 | |
| 333 | 4. ``qemu_devices_reset()`` performs a reset of the system, using |
| 334 | the three-phase mechanism listed above. It resets all objects |
| 335 | that were registered with it using ``qemu_register_resettable()``. |
| 336 | It also calls all the functions registered with it using |
| 337 | ``qemu_register_reset()``. Those functions are called during the |
| 338 | "hold" phase of this reset. |
| 339 | |
| 340 | 5. The most important object that this reset resets is the |
| 341 | 'sysbus' bus. The sysbus bus is the root of the qbus tree. This |
| 342 | means that all devices on the sysbus are reset, and all their |
| 343 | child buses, and all the devices on those child buses. |
| 344 | |
| 345 | 6. Devices which are not on the qbus tree are *not* automatically |
| 346 | reset! (The most obvious example of this is CPU objects, but |
| 347 | anything that directly inherits from ``TYPE_OBJECT`` or ``TYPE_DEVICE`` |
| 348 | rather than from ``TYPE_SYS_BUS_DEVICE`` or some other plugs-into-a-bus |
| 349 | type will be in this category.) You need to therefore arrange for these |
| 350 | to be reset in some other way (e.g. using ``qemu_register_resettable()`` |
| 351 | or ``qemu_register_reset()``). |