]> git.proxmox.com Git - mirror_qemu.git/blame - hw/core/reset.c
Merge tag 'pull-maintainer-may24-160524-2' of https://gitlab.com/stsquad/qemu into...
[mirror_qemu.git] / hw / core / reset.c
CommitLineData
2f7b92a0
PB
1/*
2 * Reset handlers.
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 * Copyright (c) 2016 Red Hat, Inc.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
24 */
25
26#include "qemu/osdep.h"
2f7b92a0 27#include "sysemu/reset.h"
86fae16e
PM
28#include "hw/resettable.h"
29#include "hw/core/resetcontainer.h"
2f7b92a0 30
86fae16e
PM
31/*
32 * Return a pointer to the singleton container that holds all the Resettable
33 * items that will be reset when qemu_devices_reset() is called.
34 */
35static ResettableContainer *get_root_reset_container(void)
36{
37 static ResettableContainer *root_reset_container;
38
39 if (!root_reset_container) {
40 root_reset_container =
41 RESETTABLE_CONTAINER(object_new(TYPE_RESETTABLE_CONTAINER));
42 }
43 return root_reset_container;
44}
2f7b92a0 45
71b3ea37
PM
46/*
47 * This is an Object which implements Resettable simply to call the
48 * callback function in the hold phase.
49 */
50#define TYPE_LEGACY_RESET "legacy-reset"
51OBJECT_DECLARE_SIMPLE_TYPE(LegacyReset, LEGACY_RESET)
52
53struct LegacyReset {
54 Object parent;
55 ResettableState reset_state;
2f7b92a0
PB
56 QEMUResetHandler *func;
57 void *opaque;
7966d70f 58 bool skip_on_snapshot_load;
71b3ea37 59};
2f7b92a0 60
71b3ea37
PM
61OBJECT_DEFINE_SIMPLE_TYPE_WITH_INTERFACES(LegacyReset, legacy_reset, LEGACY_RESET, OBJECT, { TYPE_RESETTABLE_INTERFACE }, { })
62
63static ResettableState *legacy_reset_get_state(Object *obj)
64{
65 LegacyReset *lr = LEGACY_RESET(obj);
66 return &lr->reset_state;
67}
68
ad80e367 69static void legacy_reset_hold(Object *obj, ResetType type)
71b3ea37
PM
70{
71 LegacyReset *lr = LEGACY_RESET(obj);
72
631f46d4 73 if (type == RESET_TYPE_SNAPSHOT_LOAD && lr->skip_on_snapshot_load) {
71b3ea37
PM
74 return;
75 }
76 lr->func(lr->opaque);
77}
78
79static void legacy_reset_init(Object *obj)
80{
81}
82
83static void legacy_reset_finalize(Object *obj)
84{
85}
86
87static void legacy_reset_class_init(ObjectClass *klass, void *data)
88{
89 ResettableClass *rc = RESETTABLE_CLASS(klass);
90
91 rc->get_state = legacy_reset_get_state;
92 rc->phases.hold = legacy_reset_hold;
93}
2f7b92a0
PB
94
95void qemu_register_reset(QEMUResetHandler *func, void *opaque)
96{
71b3ea37
PM
97 Object *obj = object_new(TYPE_LEGACY_RESET);
98 LegacyReset *lr = LEGACY_RESET(obj);
2f7b92a0 99
71b3ea37
PM
100 lr->func = func;
101 lr->opaque = opaque;
102 qemu_register_resettable(obj);
2f7b92a0
PB
103}
104
7966d70f
JD
105void qemu_register_reset_nosnapshotload(QEMUResetHandler *func, void *opaque)
106{
71b3ea37
PM
107 Object *obj = object_new(TYPE_LEGACY_RESET);
108 LegacyReset *lr = LEGACY_RESET(obj);
109
110 lr->func = func;
111 lr->opaque = opaque;
112 lr->skip_on_snapshot_load = true;
113 qemu_register_resettable(obj);
114}
115
116typedef struct FindLegacyInfo {
117 QEMUResetHandler *func;
118 void *opaque;
119 LegacyReset *lr;
120} FindLegacyInfo;
121
122static void find_legacy_reset_cb(Object *obj, void *opaque, ResetType type)
123{
124 LegacyReset *lr;
125 FindLegacyInfo *fli = opaque;
7966d70f 126
71b3ea37
PM
127 /* Not everything in the ResettableContainer will be a LegacyReset */
128 lr = LEGACY_RESET(object_dynamic_cast(obj, TYPE_LEGACY_RESET));
129 if (lr && lr->func == fli->func && lr->opaque == fli->opaque) {
130 fli->lr = lr;
131 }
132}
133
134static LegacyReset *find_legacy_reset(QEMUResetHandler *func, void *opaque)
135{
136 /*
137 * Find the LegacyReset with the specified func and opaque,
138 * by getting the ResettableContainer to call our callback for
139 * every item in it.
140 */
141 ResettableContainer *rootcon = get_root_reset_container();
142 ResettableClass *rc = RESETTABLE_GET_CLASS(rootcon);
143 FindLegacyInfo fli;
144
145 fli.func = func;
146 fli.opaque = opaque;
147 fli.lr = NULL;
148 rc->child_foreach(OBJECT(rootcon), find_legacy_reset_cb,
149 &fli, RESET_TYPE_COLD);
150 return fli.lr;
7966d70f
JD
151}
152
2f7b92a0
PB
153void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
154{
71b3ea37
PM
155 Object *obj = OBJECT(find_legacy_reset(func, opaque));
156
157 if (obj) {
158 qemu_unregister_resettable(obj);
159 object_unref(obj);
2f7b92a0
PB
160 }
161}
162
86fae16e
PM
163void qemu_register_resettable(Object *obj)
164{
165 resettable_container_add(get_root_reset_container(), obj);
166}
167
168void qemu_unregister_resettable(Object *obj)
169{
170 resettable_container_remove(get_root_reset_container(), obj);
171}
172
7966d70f 173void qemu_devices_reset(ShutdownCause reason)
2f7b92a0 174{
631f46d4
PM
175 ResetType type = (reason == SHUTDOWN_CAUSE_SNAPSHOT_LOAD) ?
176 RESET_TYPE_SNAPSHOT_LOAD : RESET_TYPE_COLD;
2f7b92a0 177
86fae16e 178 /* Reset the simulation */
631f46d4 179 resettable_reset(OBJECT(get_root_reset_container()), type);
86fae16e 180}