/src/qemu/hw/virtio/virtio-md-pci.c
Line | Count | Source |
1 | | /* |
2 | | * Abstract virtio based memory device |
3 | | * |
4 | | * Copyright (C) 2023 Red Hat, Inc. |
5 | | * |
6 | | * Authors: |
7 | | * David Hildenbrand <david@redhat.com> |
8 | | * |
9 | | * This work is licensed under the terms of the GNU GPL, version 2. |
10 | | * See the COPYING file in the top-level directory. |
11 | | */ |
12 | | |
13 | | #include "qemu/osdep.h" |
14 | | #include "hw/virtio/virtio-md-pci.h" |
15 | | #include "hw/mem/memory-device.h" |
16 | | #include "qapi/error.h" |
17 | | #include "qemu/error-report.h" |
18 | | |
19 | | void virtio_md_pci_pre_plug(VirtIOMDPCI *vmd, MachineState *ms, Error **errp) |
20 | 0 | { |
21 | 0 | DeviceState *dev = DEVICE(vmd); |
22 | 0 | const HotplugHandler *bus_handler = qdev_get_bus_hotplug_handler(dev); |
23 | 0 | MemoryDeviceState *md = MEMORY_DEVICE(vmd); |
24 | 0 | Error *local_err = NULL; |
25 | |
|
26 | 0 | if (!bus_handler && dev->hotplugged) { |
27 | | /* |
28 | | * Without a bus hotplug handler, we cannot control the plug/unplug |
29 | | * order. We should never reach this point when hotplugging on x86, |
30 | | * however, better add a safety net. |
31 | | */ |
32 | 0 | error_setg(errp, "hotplug of virtio based memory devices not supported" |
33 | 0 | " on this bus."); |
34 | 0 | return; |
35 | 0 | } |
36 | | /* |
37 | | * First, see if we can plug this memory device at all. If that |
38 | | * succeeds, branch of to the actual hotplug handler. |
39 | | */ |
40 | 0 | memory_device_pre_plug(md, ms, &local_err); |
41 | 0 | if (!local_err && bus_handler) { |
42 | 0 | hotplug_handler_pre_plug(bus_handler, dev, &local_err); |
43 | 0 | } |
44 | 0 | error_propagate(errp, local_err); |
45 | 0 | } |
46 | | |
47 | | void virtio_md_pci_plug(VirtIOMDPCI *vmd, MachineState *ms, Error **errp) |
48 | 0 | { |
49 | 0 | DeviceState *dev = DEVICE(vmd); |
50 | 0 | const HotplugHandler *bus_handler = qdev_get_bus_hotplug_handler(dev); |
51 | 0 | MemoryDeviceState *md = MEMORY_DEVICE(vmd); |
52 | 0 | Error *local_err = NULL; |
53 | | |
54 | | /* |
55 | | * Plug the memory device first and then branch off to the actual |
56 | | * hotplug handler. If that one fails, we can easily undo the memory |
57 | | * device bits. |
58 | | */ |
59 | 0 | memory_device_plug(md, ms); |
60 | 0 | if (bus_handler) { |
61 | 0 | hotplug_handler_plug(bus_handler, dev, &local_err); |
62 | 0 | if (local_err) { |
63 | 0 | memory_device_unplug(md, ms); |
64 | 0 | } |
65 | 0 | } |
66 | 0 | error_propagate(errp, local_err); |
67 | 0 | } |
68 | | |
69 | | void virtio_md_pci_unplug_request(VirtIOMDPCI *vmd, MachineState *ms, |
70 | | Error **errp) |
71 | 0 | { |
72 | 0 | VirtIOMDPCIClass *vmdc = VIRTIO_MD_PCI_GET_CLASS(vmd); |
73 | 0 | DeviceState *dev = DEVICE(vmd); |
74 | 0 | const HotplugHandler *bus_handler = qdev_get_bus_hotplug_handler(dev); |
75 | 0 | const HotplugHandlerClass *hdc; |
76 | 0 | Error *local_err = NULL; |
77 | |
|
78 | 0 | if (!vmdc->unplug_request_check) { |
79 | 0 | error_setg(errp, "this virtio based memory devices cannot be unplugged"); |
80 | 0 | return; |
81 | 0 | } |
82 | | |
83 | 0 | if (!bus_handler) { |
84 | 0 | error_setg(errp, "hotunplug of virtio based memory devices not" |
85 | 0 | "supported on this bus"); |
86 | 0 | return; |
87 | 0 | } |
88 | | |
89 | 0 | vmdc->unplug_request_check(vmd, &local_err); |
90 | 0 | if (local_err) { |
91 | 0 | error_propagate(errp, local_err); |
92 | 0 | return; |
93 | 0 | } |
94 | | |
95 | | /* |
96 | | * Forward the async request or turn it into a sync request (handling it |
97 | | * like qdev_unplug()). |
98 | | */ |
99 | 0 | hdc = HOTPLUG_HANDLER_GET_CLASS(bus_handler); |
100 | 0 | if (hdc->unplug_request) { |
101 | 0 | hotplug_handler_unplug_request(bus_handler, dev, &local_err); |
102 | 0 | } else { |
103 | 0 | virtio_md_pci_unplug(vmd, ms, &local_err); |
104 | 0 | if (!local_err) { |
105 | 0 | object_unparent(OBJECT(dev)); |
106 | 0 | } |
107 | 0 | } |
108 | 0 | } |
109 | | |
110 | | void virtio_md_pci_unplug(VirtIOMDPCI *vmd, MachineState *ms, Error **errp) |
111 | 0 | { |
112 | 0 | DeviceState *dev = DEVICE(vmd); |
113 | 0 | const HotplugHandler *bus_handler = qdev_get_bus_hotplug_handler(dev); |
114 | 0 | MemoryDeviceState *md = MEMORY_DEVICE(vmd); |
115 | 0 | Error *local_err = NULL; |
116 | | |
117 | | /* Unplug the memory device while it is still realized. */ |
118 | 0 | memory_device_unplug(md, ms); |
119 | |
|
120 | 0 | if (bus_handler) { |
121 | 0 | hotplug_handler_unplug(bus_handler, dev, &local_err); |
122 | 0 | if (local_err) { |
123 | | /* Not expected to fail ... but still try to recover. */ |
124 | 0 | memory_device_plug(md, ms); |
125 | 0 | error_propagate(errp, local_err); |
126 | 0 | return; |
127 | 0 | } |
128 | 0 | } else { |
129 | | /* Very unexpected, but let's just try to do the right thing. */ |
130 | 0 | warn_report("Unexpected unplug of virtio based memory device"); |
131 | 0 | qdev_unrealize(dev); |
132 | 0 | } |
133 | 0 | } |
134 | | |
135 | | static const TypeInfo virtio_md_pci_info = { |
136 | | .name = TYPE_VIRTIO_MD_PCI, |
137 | | .parent = TYPE_VIRTIO_PCI, |
138 | | .instance_size = sizeof(VirtIOMDPCI), |
139 | | .class_size = sizeof(VirtIOMDPCIClass), |
140 | | .abstract = true, |
141 | | .interfaces = (const InterfaceInfo[]) { |
142 | | { TYPE_MEMORY_DEVICE }, |
143 | | { } |
144 | | }, |
145 | | }; |
146 | | |
147 | | static void virtio_md_pci_register(void) |
148 | 63 | { |
149 | 63 | type_register_static(&virtio_md_pci_info); |
150 | 63 | } |
151 | | type_init(virtio_md_pci_register) |