xref: /qemu/docs/devel/reset.rst (revision 631f46d4ea7cb7ac0e529aacc1e7d832473f96c3)
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
14a365572bSPeter MaydellThe main object types which implement this interface are DeviceClass
15a365572bSPeter Maydelland BusClass.
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
22a4a411fbSStefan 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
30*631f46d4SPeter MaydellThe Resettable interface handles reset types with an enum ``ResetType``:
31d66cc84cSDamien Hedde
32d66cc84cSDamien Hedde``RESET_TYPE_COLD``
33d66cc84cSDamien Hedde  Cold reset is supported by every resettable object. In QEMU, it means we reset
34d66cc84cSDamien Hedde  to the initial state corresponding to the start of QEMU; this might differ
35d66cc84cSDamien Hedde  from what is a real hardware cold reset. It differs from other resets (like
36d66cc84cSDamien Hedde  warm or bus resets) which may keep certain parts untouched.
37d66cc84cSDamien Hedde
38*631f46d4SPeter Maydell``RESET_TYPE_SNAPSHOT_LOAD``
39*631f46d4SPeter Maydell  This is called for a reset which is being done to put the system into a
40*631f46d4SPeter Maydell  clean state prior to loading a snapshot. (This corresponds to a reset
41*631f46d4SPeter Maydell  with ``SHUTDOWN_CAUSE_SNAPSHOT_LOAD``.) Almost all devices should treat
42*631f46d4SPeter Maydell  this the same as ``RESET_TYPE_COLD``. The main exception is devices which
43*631f46d4SPeter Maydell  have some non-deterministic state they want to reinitialize to a different
44*631f46d4SPeter Maydell  value on each cold reset, such as RNG seed information, and which they
45*631f46d4SPeter Maydell  must not reinitialize on a snapshot-load reset.
46*631f46d4SPeter Maydell
47*631f46d4SPeter MaydellDevices which implement reset methods must treat any unknown ``ResetType``
48*631f46d4SPeter Maydellas equivalent to ``RESET_TYPE_COLD``; this will reduce the amount of
49*631f46d4SPeter Maydellexisting code we need to change if we add more types in future.
50*631f46d4SPeter Maydell
51d66cc84cSDamien HeddeCalling ``resettable_reset()`` is equivalent to calling
52d66cc84cSDamien Hedde``resettable_assert_reset()`` then ``resettable_release_reset()``. It is
53d66cc84cSDamien Heddepossible to interleave multiple calls to these three functions. There may
54d66cc84cSDamien Heddebe several reset sources/controllers of a given object. The interface handles
55d66cc84cSDamien Heddeeverything and the different reset controllers do not need to know anything
56d66cc84cSDamien Heddeabout each others. The object will leave reset state only when each other
57d66cc84cSDamien Heddecontrollers end their reset operation. This point is handled internally by
58d66cc84cSDamien Heddemaintaining a count of in-progress resets; it is crucial to call
59d66cc84cSDamien Hedde``resettable_release_reset()`` one time and only one time per
60d66cc84cSDamien Hedde``resettable_assert_reset()`` call.
61d66cc84cSDamien Hedde
62d66cc84cSDamien HeddeFor now migration of a device or bus in reset is not supported. Care must be
63d66cc84cSDamien Heddetaken not to delay ``resettable_release_reset()`` after its
64d66cc84cSDamien Hedde``resettable_assert_reset()`` counterpart.
65d66cc84cSDamien Hedde
66d66cc84cSDamien HeddeNote that, since resettable is an interface, the API takes a simple Object as
67d66cc84cSDamien Heddeparameter. Still, it is a programming error to call a resettable function on a
68d66cc84cSDamien Heddenon-resettable object and it will trigger a run time assert error. Since most
69d66cc84cSDamien Heddecalls to resettable interface are done through base class functions, such an
70d66cc84cSDamien Heddeerror is not likely to happen.
71d66cc84cSDamien Hedde
72d66cc84cSDamien HeddeFor Devices and Buses, the following helper functions exist:
73d66cc84cSDamien Hedde
74d66cc84cSDamien Hedde- ``device_cold_reset()``
75d66cc84cSDamien Hedde- ``bus_cold_reset()``
76d66cc84cSDamien Hedde
77d66cc84cSDamien HeddeThese are simple wrappers around resettable_reset() function; they only cast the
78d66cc84cSDamien HeddeDevice or Bus into an Object and pass the cold reset type. When possible
79d66cc84cSDamien Heddeprefer to use these functions instead of ``resettable_reset()``.
80d66cc84cSDamien Hedde
81d66cc84cSDamien HeddeDevice and bus functions co-exist because there can be semantic differences
82d66cc84cSDamien Heddebetween resetting a bus and resetting the controller bridge which owns it.
83d66cc84cSDamien HeddeFor example, consider a SCSI controller. Resetting the controller puts all
84d66cc84cSDamien Heddeits registers back to what reset state was as well as reset everything on the
85d66cc84cSDamien HeddeSCSI bus, whereas resetting just the SCSI bus only resets everything that's on
86d66cc84cSDamien Heddeit but not the controller.
87d66cc84cSDamien Hedde
88d66cc84cSDamien Hedde
89d66cc84cSDamien HeddeMulti-phase mechanism
90d66cc84cSDamien Hedde---------------------
91d66cc84cSDamien Hedde
92d66cc84cSDamien HeddeThis section documents the internals of the resettable interface.
93d66cc84cSDamien Hedde
94d66cc84cSDamien HeddeThe resettable interface uses a multi-phase system to relieve objects and
95d66cc84cSDamien Heddemachines from reset ordering problems. To address this, the reset operation
96d66cc84cSDamien Heddeof an object is split into three well defined phases.
97d66cc84cSDamien Hedde
98d66cc84cSDamien HeddeWhen resetting several objects (for example the whole machine at simulation
99d66cc84cSDamien Heddestartup), all first phases of all objects are executed, then all second phases
100d66cc84cSDamien Heddeand then all third phases.
101d66cc84cSDamien Hedde
102d66cc84cSDamien HeddeThe three phases are:
103d66cc84cSDamien Hedde
104d66cc84cSDamien Hedde1. The **enter** phase is executed when the object enters reset. It resets only
105d66cc84cSDamien Hedde   local state of the object; it must not do anything that has a side-effect
106d66cc84cSDamien Hedde   on other objects, such as raising or lowering a qemu_irq line or reading or
107d66cc84cSDamien Hedde   writing guest memory.
108d66cc84cSDamien Hedde
109d66cc84cSDamien Hedde2. The **hold** phase is executed for entry into reset, once every object in the
110d66cc84cSDamien Hedde   group which is being reset has had its *enter* phase executed. At this point
111d66cc84cSDamien Hedde   devices can do actions that affect other objects.
112d66cc84cSDamien Hedde
113d66cc84cSDamien Hedde3. The **exit** phase is executed when the object leaves the reset state.
114d66cc84cSDamien Hedde   Actions affecting other objects are permitted.
115d66cc84cSDamien Hedde
116d66cc84cSDamien HeddeAs said in previous section, the interface maintains a count of reset. This
117d66cc84cSDamien Heddecount is used to ensure phases are executed only when required. *enter* and
118d66cc84cSDamien Hedde*hold* phases are executed only when asserting reset for the first time
119d66cc84cSDamien Hedde(if an object is already in reset state when calling
120d66cc84cSDamien Hedde``resettable_assert_reset()`` or ``resettable_reset()``, they are not
121d66cc84cSDamien Heddeexecuted).
122d66cc84cSDamien HeddeThe *exit* phase is executed only when the last reset operation ends. Therefore
123d66cc84cSDamien Heddethe object does not need to care how many of reset controllers it has and how
124d66cc84cSDamien Heddemany of them have started a reset.
125d66cc84cSDamien Hedde
126d66cc84cSDamien Hedde
127d66cc84cSDamien HeddeHandling reset in a resettable object
128d66cc84cSDamien Hedde-------------------------------------
129d66cc84cSDamien Hedde
130d66cc84cSDamien HeddeThis section documents the APIs that an implementation of a resettable object
131d66cc84cSDamien Heddemust provide and what functions it has access to. It is intended for people
132d66cc84cSDamien Heddewho want to implement or convert a class which has the resettable interface;
133d66cc84cSDamien Heddefor example when specializing an existing device or bus.
134d66cc84cSDamien Hedde
135d66cc84cSDamien HeddeMethods to implement
136d66cc84cSDamien Hedde....................
137d66cc84cSDamien Hedde
138d66cc84cSDamien HeddeThree methods should be defined or left empty. Each method corresponds to a
139d66cc84cSDamien Heddephase of the reset; they are name ``phases.enter()``, ``phases.hold()`` and
140d66cc84cSDamien Hedde``phases.exit()``. They all take the object as parameter. The *enter* method
141d66cc84cSDamien Heddealso take the reset type as second parameter.
142d66cc84cSDamien Hedde
143d66cc84cSDamien HeddeWhen extending an existing class, these methods may need to be extended too.
144d66cc84cSDamien HeddeThe ``resettable_class_set_parent_phases()`` class function may be used to
145d66cc84cSDamien Heddebackup parent class methods.
146d66cc84cSDamien Hedde
147d66cc84cSDamien HeddeHere follows an example to implement reset for a Device which sets an IO while
148d66cc84cSDamien Heddein reset.
149d66cc84cSDamien Hedde
150d66cc84cSDamien Hedde::
151d66cc84cSDamien Hedde
152d66cc84cSDamien Hedde    static void mydev_reset_enter(Object *obj, ResetType type)
153d66cc84cSDamien Hedde    {
154d66cc84cSDamien Hedde        MyDevClass *myclass = MYDEV_GET_CLASS(obj);
155d66cc84cSDamien Hedde        MyDevState *mydev = MYDEV(obj);
156d66cc84cSDamien Hedde        /* call parent class enter phase */
157d66cc84cSDamien Hedde        if (myclass->parent_phases.enter) {
158d66cc84cSDamien Hedde            myclass->parent_phases.enter(obj, type);
159d66cc84cSDamien Hedde        }
160d66cc84cSDamien Hedde        /* initialize local state only */
161d66cc84cSDamien Hedde        mydev->var = 0;
162d66cc84cSDamien Hedde    }
163d66cc84cSDamien Hedde
16441d49ec1SPeter Maydell    static void mydev_reset_hold(Object *obj, ResetType type)
165d66cc84cSDamien Hedde    {
166d66cc84cSDamien Hedde        MyDevClass *myclass = MYDEV_GET_CLASS(obj);
167d66cc84cSDamien Hedde        MyDevState *mydev = MYDEV(obj);
168d66cc84cSDamien Hedde        /* call parent class hold phase */
169d66cc84cSDamien Hedde        if (myclass->parent_phases.hold) {
17041d49ec1SPeter Maydell            myclass->parent_phases.hold(obj, type);
171d66cc84cSDamien Hedde        }
172d66cc84cSDamien Hedde        /* set an IO */
173d66cc84cSDamien Hedde        qemu_set_irq(mydev->irq, 1);
174d66cc84cSDamien Hedde    }
175d66cc84cSDamien Hedde
17641d49ec1SPeter Maydell    static void mydev_reset_exit(Object *obj, ResetType type)
177d66cc84cSDamien Hedde    {
178d66cc84cSDamien Hedde        MyDevClass *myclass = MYDEV_GET_CLASS(obj);
179d66cc84cSDamien Hedde        MyDevState *mydev = MYDEV(obj);
180d66cc84cSDamien Hedde        /* call parent class exit phase */
181d66cc84cSDamien Hedde        if (myclass->parent_phases.exit) {
18241d49ec1SPeter Maydell            myclass->parent_phases.exit(obj, type);
183d66cc84cSDamien Hedde        }
184d66cc84cSDamien Hedde        /* clear an IO */
185d66cc84cSDamien Hedde        qemu_set_irq(mydev->irq, 0);
186d66cc84cSDamien Hedde    }
187d66cc84cSDamien Hedde
188d66cc84cSDamien Hedde    typedef struct MyDevClass {
189d66cc84cSDamien Hedde        MyParentClass parent_class;
190d66cc84cSDamien Hedde        /* to store eventual parent reset methods */
191d66cc84cSDamien Hedde        ResettablePhases parent_phases;
192d66cc84cSDamien Hedde    } MyDevClass;
193d66cc84cSDamien Hedde
194d66cc84cSDamien Hedde    static void mydev_class_init(ObjectClass *class, void *data)
195d66cc84cSDamien Hedde    {
196d66cc84cSDamien Hedde        MyDevClass *myclass = MYDEV_CLASS(class);
197d66cc84cSDamien Hedde        ResettableClass *rc = RESETTABLE_CLASS(class);
198fa365d05SAkihiko Odaki        resettable_class_set_parent_phases(rc,
199d66cc84cSDamien Hedde                                           mydev_reset_enter,
200d66cc84cSDamien Hedde                                           mydev_reset_hold,
201d66cc84cSDamien Hedde                                           mydev_reset_exit,
202d66cc84cSDamien Hedde                                           &myclass->parent_phases);
203d66cc84cSDamien Hedde    }
204d66cc84cSDamien Hedde
205d66cc84cSDamien HeddeIn the above example, we override all three phases. It is possible to override
206d66cc84cSDamien Heddeonly some of them by passing NULL instead of a function pointer to
207fa365d05SAkihiko Odaki``resettable_class_set_parent_phases()``. For example, the following will
208d66cc84cSDamien Heddeonly override the *enter* phase and leave *hold* and *exit* untouched::
209d66cc84cSDamien Hedde
210fa365d05SAkihiko Odaki    resettable_class_set_parent_phases(rc, mydev_reset_enter, NULL, NULL,
211d66cc84cSDamien Hedde                                       &myclass->parent_phases);
212d66cc84cSDamien Hedde
213d66cc84cSDamien HeddeThis is equivalent to providing a trivial implementation of the hold and exit
214d66cc84cSDamien Heddephases which does nothing but call the parent class's implementation of the
215d66cc84cSDamien Heddephase.
216d66cc84cSDamien Hedde
217d66cc84cSDamien HeddePolling the reset state
218d66cc84cSDamien Hedde.......................
219d66cc84cSDamien Hedde
220d66cc84cSDamien HeddeResettable interface provides the ``resettable_is_in_reset()`` function.
221d66cc84cSDamien HeddeThis function returns true if the object parameter is currently under reset.
222d66cc84cSDamien Hedde
223310616d3SDamien HeddeAn object is under reset from the beginning of the *enter* phase (before
224310616d3SDamien Heddeeither its children or its own enter method is called) to the *exit*
225310616d3SDamien Heddephase. During *enter* and *hold* phase only, the function will return that the
226310616d3SDamien Heddeobject is in reset. The state is changed after the *exit* is propagated to
227310616d3SDamien Heddeits children and just before calling the object's own *exit* method.
228d66cc84cSDamien Hedde
229d66cc84cSDamien HeddeThis function may be used if the object behavior has to be adapted
230d66cc84cSDamien Heddewhile in reset state. For example if a device has an irq input,
231d66cc84cSDamien Heddeit will probably need to ignore it while in reset; then it can for
232d66cc84cSDamien Heddeexample check the reset state at the beginning of the irq callback.
233d66cc84cSDamien Hedde
234d66cc84cSDamien HeddeNote that until migration of the reset state is supported, an object
235d66cc84cSDamien Heddeshould not be left in reset. So apart from being currently executing
236d66cc84cSDamien Heddeone of the reset phases, the only cases when this function will return
237d66cc84cSDamien Heddetrue is if an external interaction (like changing an io) is made during
238d66cc84cSDamien Hedde*hold* or *exit* phase of another object in the same reset group.
239d66cc84cSDamien Hedde
240d66cc84cSDamien HeddeHelpers ``device_is_in_reset()`` and ``bus_is_in_reset()`` are also provided
241d66cc84cSDamien Heddefor devices and buses and should be preferred.
242d66cc84cSDamien Hedde
243d66cc84cSDamien Hedde
244d66cc84cSDamien HeddeBase class handling of reset
245d66cc84cSDamien Hedde----------------------------
246d66cc84cSDamien Hedde
247d66cc84cSDamien HeddeThis section documents parts of the reset mechanism that you only need to know
248d66cc84cSDamien Heddeabout if you are extending it to work with a new base class other than
249d66cc84cSDamien HeddeDeviceClass or BusClass, or maintaining the existing code in those classes. Most
250d66cc84cSDamien Heddepeople can ignore it.
251d66cc84cSDamien Hedde
252d66cc84cSDamien HeddeMethods to implement
253d66cc84cSDamien Hedde....................
254d66cc84cSDamien Hedde
255d66cc84cSDamien HeddeThere are two other methods that need to exist in a class implementing the
256d66cc84cSDamien Heddeinterface: ``get_state()`` and ``child_foreach()``.
257d66cc84cSDamien Hedde
258d66cc84cSDamien Hedde``get_state()`` is simple. *resettable* is an interface and, as a consequence,
259d66cc84cSDamien Heddedoes not have any class state structure. But in order to factorize the code, we
260d66cc84cSDamien Heddeneed one. This method must return a pointer to ``ResettableState`` structure.
261d66cc84cSDamien HeddeThe structure must be allocated by the base class; preferably it should be
262d66cc84cSDamien Heddelocated inside the object instance structure.
263d66cc84cSDamien Hedde
264d66cc84cSDamien Hedde``child_foreach()`` is more complex. It should execute the given callback on
265d66cc84cSDamien Heddeevery reset child of the given resettable object. All children must be
266d66cc84cSDamien Hedderesettable too. Additional parameters (a reset type and an opaque pointer) must
267d66cc84cSDamien Heddebe passed to the callback too.
268d66cc84cSDamien Hedde
269d66cc84cSDamien HeddeIn ``DeviceClass`` and ``BusClass`` the ``ResettableState`` is located
270d66cc84cSDamien Hedde``DeviceState`` and ``BusState`` structure. ``child_foreach()`` is implemented
271d66cc84cSDamien Heddeto follow the bus hierarchy; for a bus, it calls the function on every child
272d66cc84cSDamien Heddedevice; for a device, it calls the function on every bus child. When we reset
273d66cc84cSDamien Heddethe main system bus, we reset the whole machine bus tree.
274d66cc84cSDamien Hedde
275d66cc84cSDamien HeddeChanging a resettable parent
276d66cc84cSDamien Hedde............................
277d66cc84cSDamien Hedde
278d66cc84cSDamien HeddeOne thing which should be taken care of by the base class is handling reset
279d66cc84cSDamien Heddehierarchy changes.
280d66cc84cSDamien Hedde
281d66cc84cSDamien HeddeThe reset hierarchy is supposed to be static and built during machine creation.
282d66cc84cSDamien HeddeBut there are actually some exceptions. To cope with this, the resettable API
283d66cc84cSDamien Heddeprovides ``resettable_change_parent()``. This function allows to set, update or
284d66cc84cSDamien Hedderemove the parent of a resettable object after machine creation is done. As
285d66cc84cSDamien Heddeparameters, it takes the object being moved, the old parent if any and the new
286d66cc84cSDamien Heddeparent if any.
287d66cc84cSDamien Hedde
288d66cc84cSDamien HeddeThis function can be used at any time when not in a reset operation. During
289d66cc84cSDamien Heddea reset operation it must be used only in *hold* phase. Using it in *enter* or
290d66cc84cSDamien Hedde*exit* phase is an error.
291d66cc84cSDamien HeddeAlso it should not be used during machine creation, although it is harmless to
292d66cc84cSDamien Heddedo so: the function is a no-op as long as old and new parent are NULL or not
293d66cc84cSDamien Heddein reset.
294d66cc84cSDamien Hedde
295d66cc84cSDamien HeddeThere is currently 2 cases where this function is used:
296d66cc84cSDamien Hedde
297d66cc84cSDamien Hedde1. *device hotplug*; it means a new device is introduced on a live bus.
298d66cc84cSDamien Hedde
299d66cc84cSDamien Hedde2. *hot bus change*; it means an existing live device is added, moved or
300d66cc84cSDamien Hedde   removed in the bus hierarchy. At the moment, it occurs only in the raspi
301d66cc84cSDamien Hedde   machines for changing the sdbus used by sd card.
302a365572bSPeter Maydell
303a365572bSPeter MaydellReset of the complete system
304a365572bSPeter Maydell----------------------------
305a365572bSPeter Maydell
306a365572bSPeter MaydellReset of the complete system is a little complicated. The typical
307a365572bSPeter Maydellflow is:
308a365572bSPeter Maydell
309a365572bSPeter Maydell1. Code which wishes to reset the entire system does so by calling
310a365572bSPeter Maydell   ``qemu_system_reset_request()``. This schedules a reset, but the
311a365572bSPeter Maydell   reset will happen asynchronously after the function returns.
312a365572bSPeter Maydell   That makes this safe to call from, for example, device models.
313a365572bSPeter Maydell
314a365572bSPeter Maydell2. The function which is called to make the reset happen is
315a365572bSPeter Maydell   ``qemu_system_reset()``. Generally only core system code should
316a365572bSPeter Maydell   call this directly.
317a365572bSPeter Maydell
318a365572bSPeter Maydell3. ``qemu_system_reset()`` calls the ``MachineClass::reset`` method of
319a365572bSPeter Maydell   the current machine, if it has one. That method must call
320a365572bSPeter Maydell   ``qemu_devices_reset()``. If the machine has no reset method,
321a365572bSPeter Maydell   ``qemu_system_reset()`` calls ``qemu_devices_reset()`` directly.
322a365572bSPeter Maydell
323a365572bSPeter Maydell4. ``qemu_devices_reset()`` performs a reset of the system, using
324a365572bSPeter Maydell   the three-phase mechanism listed above. It resets all objects
325a365572bSPeter Maydell   that were registered with it using ``qemu_register_resettable()``.
326a365572bSPeter Maydell   It also calls all the functions registered with it using
327a365572bSPeter Maydell   ``qemu_register_reset()``. Those functions are called during the
328a365572bSPeter Maydell   "hold" phase of this reset.
329a365572bSPeter Maydell
330a365572bSPeter Maydell5. The most important object that this reset resets is the
331a365572bSPeter Maydell   'sysbus' bus. The sysbus bus is the root of the qbus tree. This
332a365572bSPeter Maydell   means that all devices on the sysbus are reset, and all their
333a365572bSPeter Maydell   child buses, and all the devices on those child buses.
334a365572bSPeter Maydell
335a365572bSPeter Maydell6. Devices which are not on the qbus tree are *not* automatically
336a365572bSPeter Maydell   reset! (The most obvious example of this is CPU objects, but
337a365572bSPeter Maydell   anything that directly inherits from ``TYPE_OBJECT`` or ``TYPE_DEVICE``
338a365572bSPeter Maydell   rather than from ``TYPE_SYS_BUS_DEVICE`` or some other plugs-into-a-bus
339a365572bSPeter Maydell   type will be in this category.) You need to therefore arrange for these
340a365572bSPeter Maydell   to be reset in some other way (e.g. using ``qemu_register_resettable()``
341a365572bSPeter Maydell   or ``qemu_register_reset()``).
342