blob: d8d36b16d80b3fb7957fbeed87fe1fa47036874f [file] [log] [blame]
Wanlong Gao96d0e262014-05-14 17:43:05 +08001/*
2 * NUMA parameter parsing routines
3 *
4 * Copyright (c) 2014 Fujitsu Ltd.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
Peter Maydelld38ea872016-01-29 17:50:05 +000025#include "qemu/osdep.h"
Liu Jingqi9b12dfa2019-12-13 09:19:23 +080026#include "qemu/units.h"
Markus Armbrusterb58c5c22019-08-12 07:23:55 +020027#include "sysemu/hostmem.h"
Eduardo Habkoste35704b2015-02-08 16:51:16 -020028#include "sysemu/numa.h"
Wanlong Gao96d0e262014-05-14 17:43:05 +080029#include "exec/cpu-common.h"
Paolo Bonzini0987d732016-12-21 00:31:36 +080030#include "exec/ramlist.h"
Wanlong Gao96d0e262014-05-14 17:43:05 +080031#include "qemu/bitmap.h"
Wanlong Gao2b631ec2014-05-14 17:43:06 +080032#include "qemu/error-report.h"
Markus Armbrustere688df62018-02-01 12:18:31 +010033#include "qapi/error.h"
Wanlong Gao00421092014-05-14 17:43:08 +080034#include "qapi/opts-visitor.h"
Markus Armbruster8ac25c82019-06-19 22:10:41 +020035#include "qapi/qapi-visit-machine.h"
Eduardo Habkostf8123f22019-07-02 18:57:26 -030036#include "sysemu/qtest.h"
Markus Armbruster2e5b09f2019-07-09 17:20:52 +020037#include "hw/core/cpu.h"
zhanghailiang5b009e42014-11-04 19:49:30 +080038#include "hw/mem/pc-dimm.h"
Markus Armbrusterd6454272019-08-12 07:23:45 +020039#include "migration/vmstate.h"
Markus Armbruster12e94932019-08-12 07:23:52 +020040#include "hw/boards.h"
David Hildenbrand2cc0e2e2018-04-23 18:51:16 +020041#include "hw/mem/memory-device.h"
Eduardo Habkost7dcd1d72015-02-08 16:51:20 -020042#include "qemu/option.h"
43#include "qemu/config-file.h"
Igor Mammedovcc001882017-10-12 11:39:58 +020044#include "qemu/cutils.h"
Wanlong Gao96d0e262014-05-14 17:43:05 +080045
Wanlong Gao00421092014-05-14 17:43:08 +080046QemuOptsList qemu_numa_opts = {
47 .name = "numa",
48 .implied_opt_name = "type",
49 .head = QTAILQ_HEAD_INITIALIZER(qemu_numa_opts.head),
50 .desc = { { 0 } } /* validated with OptsVisitor */
51};
52
Igor Mammedovb69239e2019-07-02 10:07:44 -040053static int have_memdevs;
Igor Mammedov6b61c2c2020-02-19 11:08:39 -050054bool numa_uses_legacy_mem(void)
55{
56 return !have_memdevs;
57}
58
Igor Mammedovb69239e2019-07-02 10:07:44 -040059static int have_mem;
Eduardo Habkost25712ff2015-02-08 16:51:19 -020060static int max_numa_nodeid; /* Highest specified NUMA node ID, plus one.
61 * For all nodes, nodeid < max_numa_nodeid
62 */
Bharata B Raoe75e2a12015-06-29 13:50:27 +053063
Igor Mammedov64c2a8f2017-05-10 13:29:49 +020064static void parse_numa_node(MachineState *ms, NumaNodeOptions *node,
Igor Mammedovcc001882017-10-12 11:39:58 +020065 Error **errp)
Wanlong Gao96d0e262014-05-14 17:43:05 +080066{
Markus Armbrustera22528b2018-10-17 10:26:39 +020067 Error *err = NULL;
Wanlong Gao00421092014-05-14 17:43:08 +080068 uint16_t nodenr;
69 uint16List *cpus = NULL;
Igor Mammedov64c2a8f2017-05-10 13:29:49 +020070 MachineClass *mc = MACHINE_GET_CLASS(ms);
Like Xu5cc87672019-05-19 04:54:21 +080071 unsigned int max_cpus = ms->smp.max_cpus;
Tao Xu7e721e72019-08-09 14:57:24 +080072 NodeInfo *numa_info = ms->numa_state->nodes;
Wanlong Gao96d0e262014-05-14 17:43:05 +080073
Wanlong Gao00421092014-05-14 17:43:08 +080074 if (node->has_nodeid) {
75 nodenr = node->nodeid;
76 } else {
Tao Xuaa570202019-08-09 14:57:22 +080077 nodenr = ms->numa_state->num_nodes;
Wanlong Gao00421092014-05-14 17:43:08 +080078 }
79
80 if (nodenr >= MAX_NODES) {
81 error_setg(errp, "Max number of NUMA nodes reached: %"
Gonglei01bbbcf2015-02-25 12:22:30 +080082 PRIu16 "", nodenr);
Wanlong Gao96d0e262014-05-14 17:43:05 +080083 return;
84 }
85
Eduardo Habkost1945b9d2014-06-26 18:33:19 -030086 if (numa_info[nodenr].present) {
87 error_setg(errp, "Duplicate NUMA nodeid: %" PRIu16, nodenr);
88 return;
89 }
90
Michal Privoznik294aa042021-07-07 15:40:30 +020091 /*
92 * If not set the initiator, set it to MAX_NODES. And if
93 * HMAT is enabled and this node has no cpus, QEMU will raise error.
94 */
95 numa_info[nodenr].initiator = MAX_NODES;
96 if (node->has_initiator) {
97 if (!ms->numa_state->hmat_enabled) {
98 error_setg(errp, "ACPI Heterogeneous Memory Attribute Table "
99 "(HMAT) is disabled, enable it with -machine hmat=on "
100 "before using any of hmat specific options");
101 return;
102 }
103
104 if (node->initiator >= MAX_NODES) {
105 error_report("The initiator id %" PRIu16 " expects an integer "
106 "between 0 and %d", node->initiator,
107 MAX_NODES - 1);
108 return;
109 }
110
111 numa_info[nodenr].initiator = node->initiator;
112 }
113
Wanlong Gao00421092014-05-14 17:43:08 +0800114 for (cpus = node->cpus; cpus; cpus = cpus->next) {
Igor Mammedov7c88e652017-05-10 13:29:50 +0200115 CpuInstanceProperties props;
Eduardo Habkost8979c942015-02-09 17:28:52 -0200116 if (cpus->value >= max_cpus) {
117 error_setg(errp,
118 "CPU index (%" PRIu16 ")"
119 " should be smaller than maxcpus (%d)",
120 cpus->value, max_cpus);
Wanlong Gao00421092014-05-14 17:43:08 +0800121 return;
Wanlong Gao96d0e262014-05-14 17:43:05 +0800122 }
Igor Mammedov7c88e652017-05-10 13:29:50 +0200123 props = mc->cpu_index_to_instance_props(ms, cpus->value);
124 props.node_id = nodenr;
125 props.has_node_id = true;
Markus Armbrustera22528b2018-10-17 10:26:39 +0200126 machine_set_cpu_numa_node(ms, &props, &err);
127 if (err) {
128 error_propagate(errp, err);
129 return;
130 }
Wanlong Gao96d0e262014-05-14 17:43:05 +0800131 }
132
Markus Armbrusterfe8ac1f2022-11-04 17:06:57 +0100133 have_memdevs = have_memdevs || node->memdev;
134 have_mem = have_mem || node->has_mem;
135 if ((node->has_mem && have_memdevs) || (node->memdev && have_mem)) {
Igor Mammedovb69239e2019-07-02 10:07:44 -0400136 error_setg(errp, "numa configuration should use either mem= or memdev=,"
137 "mixing both is not allowed");
Paolo Bonzini7febe362014-05-14 17:43:17 +0800138 return;
139 }
140
Wanlong Gao00421092014-05-14 17:43:08 +0800141 if (node->has_mem) {
Igor Mammedov32a354d2020-06-09 09:56:35 -0400142 if (!mc->numa_mem_supported) {
143 error_setg(errp, "Parameter -numa node,mem is not supported by this"
144 " machine type");
145 error_append_hint(errp, "Use -numa node,memdev instead\n");
146 return;
147 }
148
Igor Mammedovcc001882017-10-12 11:39:58 +0200149 numa_info[nodenr].node_mem = node->mem;
Eduardo Habkostf8123f22019-07-02 18:57:26 -0300150 if (!qtest_enabled()) {
151 warn_report("Parameter -numa node,mem is deprecated,"
152 " use -numa node,memdev instead");
153 }
Wanlong Gao96d0e262014-05-14 17:43:05 +0800154 }
Markus Armbrusterfe8ac1f2022-11-04 17:06:57 +0100155 if (node->memdev) {
Paolo Bonzini7febe362014-05-14 17:43:17 +0800156 Object *o;
157 o = object_resolve_path_type(node->memdev, TYPE_MEMORY_BACKEND, NULL);
158 if (!o) {
159 error_setg(errp, "memdev=%s is ambiguous", node->memdev);
160 return;
161 }
162
163 object_ref(o);
Marc-André Lureau61d7c142017-06-07 20:36:30 +0400164 numa_info[nodenr].node_mem = object_property_get_uint(o, "size", NULL);
Paolo Bonzini7febe362014-05-14 17:43:17 +0800165 numa_info[nodenr].node_memdev = MEMORY_BACKEND(o);
166 }
Tao Xu244b3f42019-12-13 09:19:22 +0800167
Eduardo Habkost1af878e2014-06-26 18:33:18 -0300168 numa_info[nodenr].present = true;
169 max_numa_nodeid = MAX(max_numa_nodeid, nodenr + 1);
Tao Xuaa570202019-08-09 14:57:22 +0800170 ms->numa_state->num_nodes++;
Wanlong Gao96d0e262014-05-14 17:43:05 +0800171}
172
Tao Xuaa570202019-08-09 14:57:22 +0800173static
174void parse_numa_distance(MachineState *ms, NumaDistOptions *dist, Error **errp)
He Chen0f203432017-04-27 10:35:58 +0800175{
176 uint16_t src = dist->src;
177 uint16_t dst = dist->dst;
178 uint8_t val = dist->val;
Tao Xu7e721e72019-08-09 14:57:24 +0800179 NodeInfo *numa_info = ms->numa_state->nodes;
He Chen0f203432017-04-27 10:35:58 +0800180
181 if (src >= MAX_NODES || dst >= MAX_NODES) {
Igor Mammedov74f38e92018-05-16 17:06:14 +0200182 error_setg(errp, "Parameter '%s' expects an integer between 0 and %d",
183 src >= MAX_NODES ? "src" : "dst", MAX_NODES - 1);
He Chen0f203432017-04-27 10:35:58 +0800184 return;
185 }
186
187 if (!numa_info[src].present || !numa_info[dst].present) {
188 error_setg(errp, "Source/Destination NUMA node is missing. "
189 "Please use '-numa node' option to declare it first.");
190 return;
191 }
192
193 if (val < NUMA_DISTANCE_MIN) {
194 error_setg(errp, "NUMA distance (%" PRIu8 ") is invalid, "
195 "it shouldn't be less than %d.",
196 val, NUMA_DISTANCE_MIN);
197 return;
198 }
199
200 if (src == dst && val != NUMA_DISTANCE_MIN) {
201 error_setg(errp, "Local distance of node %d should be %d.",
202 src, NUMA_DISTANCE_MIN);
203 return;
204 }
205
206 numa_info[src].distance[dst] = val;
Tao Xu118154b2019-08-09 14:57:23 +0800207 ms->numa_state->have_numa_distance = true;
He Chen0f203432017-04-27 10:35:58 +0800208}
209
Liu Jingqi9b12dfa2019-12-13 09:19:23 +0800210void parse_numa_hmat_lb(NumaState *numa_state, NumaHmatLBOptions *node,
211 Error **errp)
212{
213 int i, first_bit, last_bit;
214 uint64_t max_entry, temp_base, bitmap_copy;
215 NodeInfo *numa_info = numa_state->nodes;
216 HMAT_LB_Info *hmat_lb =
217 numa_state->hmat_lb[node->hierarchy][node->data_type];
218 HMAT_LB_Data lb_data = {};
219 HMAT_LB_Data *lb_temp;
220
221 /* Error checking */
222 if (node->initiator > numa_state->num_nodes) {
223 error_setg(errp, "Invalid initiator=%d, it should be less than %d",
224 node->initiator, numa_state->num_nodes);
225 return;
226 }
227 if (node->target > numa_state->num_nodes) {
228 error_setg(errp, "Invalid target=%d, it should be less than %d",
229 node->target, numa_state->num_nodes);
230 return;
231 }
232 if (!numa_info[node->initiator].has_cpu) {
233 error_setg(errp, "Invalid initiator=%d, it isn't an "
234 "initiator proximity domain", node->initiator);
235 return;
236 }
237 if (!numa_info[node->target].present) {
238 error_setg(errp, "The target=%d should point to an existing node",
239 node->target);
240 return;
241 }
242
243 if (!hmat_lb) {
244 hmat_lb = g_malloc0(sizeof(*hmat_lb));
245 numa_state->hmat_lb[node->hierarchy][node->data_type] = hmat_lb;
246 hmat_lb->list = g_array_new(false, true, sizeof(HMAT_LB_Data));
247 }
248 hmat_lb->hierarchy = node->hierarchy;
249 hmat_lb->data_type = node->data_type;
250 lb_data.initiator = node->initiator;
251 lb_data.target = node->target;
252
253 if (node->data_type <= HMATLB_DATA_TYPE_WRITE_LATENCY) {
254 /* Input latency data */
255
256 if (!node->has_latency) {
257 error_setg(errp, "Missing 'latency' option");
258 return;
259 }
260 if (node->has_bandwidth) {
261 error_setg(errp, "Invalid option 'bandwidth' since "
262 "the data type is latency");
263 return;
264 }
265
266 /* Detect duplicate configuration */
267 for (i = 0; i < hmat_lb->list->len; i++) {
268 lb_temp = &g_array_index(hmat_lb->list, HMAT_LB_Data, i);
269
270 if (node->initiator == lb_temp->initiator &&
271 node->target == lb_temp->target) {
272 error_setg(errp, "Duplicate configuration of the latency for "
273 "initiator=%d and target=%d", node->initiator,
274 node->target);
275 return;
276 }
277 }
278
279 hmat_lb->base = hmat_lb->base ? hmat_lb->base : UINT64_MAX;
280
281 if (node->latency) {
282 /* Calculate the temporary base and compressed latency */
283 max_entry = node->latency;
284 temp_base = 1;
285 while (QEMU_IS_ALIGNED(max_entry, 10)) {
286 max_entry /= 10;
287 temp_base *= 10;
288 }
289
290 /* Calculate the max compressed latency */
291 temp_base = MIN(hmat_lb->base, temp_base);
292 max_entry = node->latency / hmat_lb->base;
293 max_entry = MAX(hmat_lb->range_bitmap, max_entry);
294
295 /*
296 * For latency hmat_lb->range_bitmap record the max compressed
297 * latency which should be less than 0xFFFF (UINT16_MAX)
298 */
299 if (max_entry >= UINT16_MAX) {
300 error_setg(errp, "Latency %" PRIu64 " between initiator=%d and "
301 "target=%d should not differ from previously entered "
302 "min or max values on more than %d", node->latency,
303 node->initiator, node->target, UINT16_MAX - 1);
304 return;
305 } else {
306 hmat_lb->base = temp_base;
307 hmat_lb->range_bitmap = max_entry;
308 }
309
310 /*
311 * Set lb_info_provided bit 0 as 1,
312 * latency information is provided
313 */
314 numa_info[node->target].lb_info_provided |= BIT(0);
315 }
316 lb_data.data = node->latency;
317 } else if (node->data_type >= HMATLB_DATA_TYPE_ACCESS_BANDWIDTH) {
318 /* Input bandwidth data */
319 if (!node->has_bandwidth) {
320 error_setg(errp, "Missing 'bandwidth' option");
321 return;
322 }
323 if (node->has_latency) {
324 error_setg(errp, "Invalid option 'latency' since "
325 "the data type is bandwidth");
326 return;
327 }
328 if (!QEMU_IS_ALIGNED(node->bandwidth, MiB)) {
329 error_setg(errp, "Bandwidth %" PRIu64 " between initiator=%d and "
330 "target=%d should be 1MB aligned", node->bandwidth,
331 node->initiator, node->target);
332 return;
333 }
334
335 /* Detect duplicate configuration */
336 for (i = 0; i < hmat_lb->list->len; i++) {
337 lb_temp = &g_array_index(hmat_lb->list, HMAT_LB_Data, i);
338
339 if (node->initiator == lb_temp->initiator &&
340 node->target == lb_temp->target) {
341 error_setg(errp, "Duplicate configuration of the bandwidth for "
342 "initiator=%d and target=%d", node->initiator,
343 node->target);
344 return;
345 }
346 }
347
348 hmat_lb->base = hmat_lb->base ? hmat_lb->base : 1;
349
350 if (node->bandwidth) {
351 /* Keep bitmap unchanged when bandwidth out of range */
352 bitmap_copy = hmat_lb->range_bitmap;
353 bitmap_copy |= node->bandwidth;
354 first_bit = ctz64(bitmap_copy);
355 temp_base = UINT64_C(1) << first_bit;
356 max_entry = node->bandwidth / temp_base;
357 last_bit = 64 - clz64(bitmap_copy);
358
359 /*
360 * For bandwidth, first_bit record the base unit of bandwidth bits,
361 * last_bit record the last bit of the max bandwidth. The max
362 * compressed bandwidth should be less than 0xFFFF (UINT16_MAX)
363 */
364 if ((last_bit - first_bit) > UINT16_BITS ||
365 max_entry >= UINT16_MAX) {
366 error_setg(errp, "Bandwidth %" PRIu64 " between initiator=%d "
367 "and target=%d should not differ from previously "
368 "entered values on more than %d", node->bandwidth,
369 node->initiator, node->target, UINT16_MAX - 1);
370 return;
371 } else {
372 hmat_lb->base = temp_base;
373 hmat_lb->range_bitmap = bitmap_copy;
374 }
375
376 /*
377 * Set lb_info_provided bit 1 as 1,
378 * bandwidth information is provided
379 */
380 numa_info[node->target].lb_info_provided |= BIT(1);
381 }
382 lb_data.data = node->bandwidth;
383 } else {
384 assert(0);
385 }
386
387 g_array_append_val(hmat_lb->list, lb_data);
388}
389
Liu Jingqic412a482019-12-13 09:19:24 +0800390void parse_numa_hmat_cache(MachineState *ms, NumaHmatCacheOptions *node,
391 Error **errp)
392{
393 int nb_numa_nodes = ms->numa_state->num_nodes;
394 NodeInfo *numa_info = ms->numa_state->nodes;
395 NumaHmatCacheOptions *hmat_cache = NULL;
396
397 if (node->node_id >= nb_numa_nodes) {
398 error_setg(errp, "Invalid node-id=%" PRIu32 ", it should be less "
399 "than %d", node->node_id, nb_numa_nodes);
400 return;
401 }
402
403 if (numa_info[node->node_id].lb_info_provided != (BIT(0) | BIT(1))) {
404 error_setg(errp, "The latency and bandwidth information of "
405 "node-id=%" PRIu32 " should be provided before memory side "
406 "cache attributes", node->node_id);
407 return;
408 }
409
410 if (node->level < 1 || node->level >= HMAT_LB_LEVELS) {
411 error_setg(errp, "Invalid level=%" PRIu8 ", it should be larger than 0 "
412 "and less than or equal to %d", node->level,
413 HMAT_LB_LEVELS - 1);
414 return;
415 }
416
417 assert(node->associativity < HMAT_CACHE_ASSOCIATIVITY__MAX);
418 assert(node->policy < HMAT_CACHE_WRITE_POLICY__MAX);
419 if (ms->numa_state->hmat_cache[node->node_id][node->level]) {
420 error_setg(errp, "Duplicate configuration of the side cache for "
421 "node-id=%" PRIu32 " and level=%" PRIu8,
422 node->node_id, node->level);
423 return;
424 }
425
426 if ((node->level > 1) &&
Igor Mammedov1b5e8432020-10-06 11:00:02 -0400427 ms->numa_state->hmat_cache[node->node_id][node->level - 1] == NULL) {
428 error_setg(errp, "Cache level=%u shall be defined first",
429 node->level - 1);
430 return;
431 }
432
433 if ((node->level > 1) &&
Igor Mammedovfadb0552020-08-21 06:05:19 -0400434 (node->size <=
Liu Jingqic412a482019-12-13 09:19:24 +0800435 ms->numa_state->hmat_cache[node->node_id][node->level - 1]->size)) {
436 error_setg(errp, "Invalid size=%" PRIu64 ", the size of level=%" PRIu8
Igor Mammedovfadb0552020-08-21 06:05:19 -0400437 " should be larger than the size(%" PRIu64 ") of "
Liu Jingqic412a482019-12-13 09:19:24 +0800438 "level=%u", node->size, node->level,
439 ms->numa_state->hmat_cache[node->node_id]
440 [node->level - 1]->size,
441 node->level - 1);
442 return;
443 }
444
445 if ((node->level < HMAT_LB_LEVELS - 1) &&
446 ms->numa_state->hmat_cache[node->node_id][node->level + 1] &&
Igor Mammedovfadb0552020-08-21 06:05:19 -0400447 (node->size >=
Liu Jingqic412a482019-12-13 09:19:24 +0800448 ms->numa_state->hmat_cache[node->node_id][node->level + 1]->size)) {
449 error_setg(errp, "Invalid size=%" PRIu64 ", the size of level=%" PRIu8
Igor Mammedovfadb0552020-08-21 06:05:19 -0400450 " should be less than the size(%" PRIu64 ") of "
Liu Jingqic412a482019-12-13 09:19:24 +0800451 "level=%u", node->size, node->level,
452 ms->numa_state->hmat_cache[node->node_id]
453 [node->level + 1]->size,
454 node->level + 1);
455 return;
456 }
457
458 hmat_cache = g_malloc0(sizeof(*hmat_cache));
459 memcpy(hmat_cache, node, sizeof(*hmat_cache));
460 ms->numa_state->hmat_cache[node->node_id][node->level] = hmat_cache;
461}
462
Igor Mammedov3319b4e2018-05-04 10:37:40 +0200463void set_numa_options(MachineState *ms, NumaOptions *object, Error **errp)
Wanlong Gao96d0e262014-05-14 17:43:05 +0800464{
Igor Mammedov5275db52019-12-12 13:48:55 +0100465 if (!ms->numa_state) {
Tao Xuaa570202019-08-09 14:57:22 +0800466 error_setg(errp, "NUMA is not supported by this machine-type");
Markus Armbruster992861f2020-07-07 18:06:04 +0200467 return;
Tao Xuaa570202019-08-09 14:57:22 +0800468 }
Wanlong Gao96d0e262014-05-14 17:43:05 +0800469
Eric Blake1fd5d4f2015-10-26 16:34:59 -0600470 switch (object->type) {
Markus Armbrusterd081a492017-02-21 21:46:26 +0100471 case NUMA_OPTIONS_TYPE_NODE:
Markus Armbruster992861f2020-07-07 18:06:04 +0200472 parse_numa_node(ms, &object->u.node, errp);
Wanlong Gao00421092014-05-14 17:43:08 +0800473 break;
He Chen0f203432017-04-27 10:35:58 +0800474 case NUMA_OPTIONS_TYPE_DIST:
Markus Armbruster992861f2020-07-07 18:06:04 +0200475 parse_numa_distance(ms, &object->u.dist, errp);
He Chen0f203432017-04-27 10:35:58 +0800476 break;
Igor Mammedov419fcde2017-05-10 13:30:01 +0200477 case NUMA_OPTIONS_TYPE_CPU:
478 if (!object->u.cpu.has_node_id) {
Markus Armbruster992861f2020-07-07 18:06:04 +0200479 error_setg(errp, "Missing mandatory node-id property");
480 return;
Igor Mammedov419fcde2017-05-10 13:30:01 +0200481 }
Tao Xu7e721e72019-08-09 14:57:24 +0800482 if (!ms->numa_state->nodes[object->u.cpu.node_id].present) {
Markus Armbruster992861f2020-07-07 18:06:04 +0200483 error_setg(errp, "Invalid node-id=%" PRId64 ", NUMA node must be "
484 "defined with -numa node,nodeid=ID before it's used with "
485 "-numa cpu,node-id=ID", object->u.cpu.node_id);
486 return;
Igor Mammedov419fcde2017-05-10 13:30:01 +0200487 }
488
Markus Armbruster992861f2020-07-07 18:06:04 +0200489 machine_set_cpu_numa_node(ms,
490 qapi_NumaCpuOptions_base(&object->u.cpu),
491 errp);
Igor Mammedov419fcde2017-05-10 13:30:01 +0200492 break;
Liu Jingqi9b12dfa2019-12-13 09:19:23 +0800493 case NUMA_OPTIONS_TYPE_HMAT_LB:
494 if (!ms->numa_state->hmat_enabled) {
495 error_setg(errp, "ACPI Heterogeneous Memory Attribute Table "
496 "(HMAT) is disabled, enable it with -machine hmat=on "
497 "before using any of hmat specific options");
498 return;
499 }
500
Markus Armbruster992861f2020-07-07 18:06:04 +0200501 parse_numa_hmat_lb(ms->numa_state, &object->u.hmat_lb, errp);
Liu Jingqi9b12dfa2019-12-13 09:19:23 +0800502 break;
Liu Jingqic412a482019-12-13 09:19:24 +0800503 case NUMA_OPTIONS_TYPE_HMAT_CACHE:
504 if (!ms->numa_state->hmat_enabled) {
505 error_setg(errp, "ACPI Heterogeneous Memory Attribute Table "
506 "(HMAT) is disabled, enable it with -machine hmat=on "
507 "before using any of hmat specific options");
508 return;
509 }
510
Markus Armbruster992861f2020-07-07 18:06:04 +0200511 parse_numa_hmat_cache(ms, &object->u.hmat_cache, errp);
Liu Jingqic412a482019-12-13 09:19:24 +0800512 break;
Wanlong Gao00421092014-05-14 17:43:08 +0800513 default:
514 abort();
Wanlong Gao96d0e262014-05-14 17:43:05 +0800515 }
Igor Mammedov3319b4e2018-05-04 10:37:40 +0200516}
517
Markus Armbruster4f7ec692018-10-17 10:26:52 +0200518static int parse_numa(void *opaque, QemuOpts *opts, Error **errp)
Igor Mammedov3319b4e2018-05-04 10:37:40 +0200519{
520 NumaOptions *object = NULL;
521 MachineState *ms = MACHINE(opaque);
522 Error *err = NULL;
523 Visitor *v = opts_visitor_new(opts);
524
Markus Armbrusterb11a0932020-07-07 18:06:07 +0200525 visit_type_NumaOptions(v, NULL, &object, errp);
Igor Mammedov3319b4e2018-05-04 10:37:40 +0200526 visit_free(v);
Markus Armbrusterb11a0932020-07-07 18:06:07 +0200527 if (!object) {
528 return -1;
Igor Mammedov3319b4e2018-05-04 10:37:40 +0200529 }
530
531 /* Fix up legacy suffix-less format */
532 if ((object->type == NUMA_OPTIONS_TYPE_NODE) && object->u.node.has_mem) {
533 const char *mem_str = qemu_opt_get(opts, "mem");
534 qemu_strtosz_MiB(mem_str, NULL, &object->u.node.mem);
535 }
536
537 set_numa_options(ms, object, &err);
538
Eric Blake96a16162016-02-23 14:14:33 -0700539 qapi_free_NumaOptions(object);
Marc-André Lureau157e94e2016-07-13 02:39:13 +0200540 if (err) {
Markus Armbruster4f7ec692018-10-17 10:26:52 +0200541 error_propagate(errp, err);
Marc-André Lureau157e94e2016-07-13 02:39:13 +0200542 return -1;
543 }
Wanlong Gao00421092014-05-14 17:43:08 +0800544
Marc-André Lureau157e94e2016-07-13 02:39:13 +0200545 return 0;
Wanlong Gao96d0e262014-05-14 17:43:05 +0800546}
547
He Chen0f203432017-04-27 10:35:58 +0800548/* If all node pair distances are symmetric, then only distances
549 * in one direction are enough. If there is even one asymmetric
550 * pair, though, then all distances must be provided. The
551 * distance from a node to itself is always NUMA_DISTANCE_MIN,
552 * so providing it is never necessary.
553 */
Tao Xuaa570202019-08-09 14:57:22 +0800554static void validate_numa_distance(MachineState *ms)
Eduardo Habkost3ef71972015-02-09 17:32:04 -0200555{
He Chen0f203432017-04-27 10:35:58 +0800556 int src, dst;
557 bool is_asymmetrical = false;
Tao Xuaa570202019-08-09 14:57:22 +0800558 int nb_numa_nodes = ms->numa_state->num_nodes;
Tao Xu7e721e72019-08-09 14:57:24 +0800559 NodeInfo *numa_info = ms->numa_state->nodes;
Eduardo Habkost3ef71972015-02-09 17:32:04 -0200560
He Chen0f203432017-04-27 10:35:58 +0800561 for (src = 0; src < nb_numa_nodes; src++) {
562 for (dst = src; dst < nb_numa_nodes; dst++) {
563 if (numa_info[src].distance[dst] == 0 &&
564 numa_info[dst].distance[src] == 0) {
565 if (src != dst) {
566 error_report("The distance between node %d and %d is "
567 "missing, at least one distance value "
568 "between each nodes should be provided.",
569 src, dst);
570 exit(EXIT_FAILURE);
571 }
572 }
Eduardo Habkost3ef71972015-02-09 17:32:04 -0200573
He Chen0f203432017-04-27 10:35:58 +0800574 if (numa_info[src].distance[dst] != 0 &&
575 numa_info[dst].distance[src] != 0 &&
576 numa_info[src].distance[dst] !=
577 numa_info[dst].distance[src]) {
578 is_asymmetrical = true;
579 }
Eduardo Habkost3ef71972015-02-09 17:32:04 -0200580 }
Eduardo Habkost3ef71972015-02-09 17:32:04 -0200581 }
Eduardo Habkost549fc542015-02-09 17:35:04 -0200582
He Chen0f203432017-04-27 10:35:58 +0800583 if (is_asymmetrical) {
584 for (src = 0; src < nb_numa_nodes; src++) {
585 for (dst = 0; dst < nb_numa_nodes; dst++) {
586 if (src != dst && numa_info[src].distance[dst] == 0) {
587 error_report("At least one asymmetrical pair of "
588 "distances is given, please provide distances "
589 "for both directions of all node pairs.");
590 exit(EXIT_FAILURE);
591 }
592 }
593 }
Eduardo Habkost549fc542015-02-09 17:35:04 -0200594 }
Eduardo Habkost3ef71972015-02-09 17:32:04 -0200595}
596
Tao Xuaa570202019-08-09 14:57:22 +0800597static void complete_init_numa_distance(MachineState *ms)
He Chen0f203432017-04-27 10:35:58 +0800598{
599 int src, dst;
Tao Xu7e721e72019-08-09 14:57:24 +0800600 NodeInfo *numa_info = ms->numa_state->nodes;
He Chen0f203432017-04-27 10:35:58 +0800601
602 /* Fixup NUMA distance by symmetric policy because if it is an
603 * asymmetric distance table, it should be a complete table and
604 * there would not be any missing distance except local node, which
605 * is verified by validate_numa_distance above.
606 */
Tao Xuaa570202019-08-09 14:57:22 +0800607 for (src = 0; src < ms->numa_state->num_nodes; src++) {
608 for (dst = 0; dst < ms->numa_state->num_nodes; dst++) {
He Chen0f203432017-04-27 10:35:58 +0800609 if (numa_info[src].distance[dst] == 0) {
610 if (src == dst) {
611 numa_info[src].distance[dst] = NUMA_DISTANCE_MIN;
612 } else {
613 numa_info[src].distance[dst] = numa_info[dst].distance[src];
614 }
615 }
616 }
617 }
618}
619
Igor Mammedov6b61c2c2020-02-19 11:08:39 -0500620static void numa_init_memdev_container(MachineState *ms, MemoryRegion *ram)
621{
622 int i;
623 uint64_t addr = 0;
624
625 for (i = 0; i < ms->numa_state->num_nodes; i++) {
626 uint64_t size = ms->numa_state->nodes[i].node_mem;
627 HostMemoryBackend *backend = ms->numa_state->nodes[i].node_memdev;
628 if (!backend) {
629 continue;
630 }
631 MemoryRegion *seg = machine_consume_memdev(ms, backend);
632 memory_region_add_subregion(ram, addr, seg);
633 addr += size;
634 }
635}
636
Igor Mammedov7a3099f2018-05-04 10:37:39 +0200637void numa_complete_configuration(MachineState *ms)
Wanlong Gao96d0e262014-05-14 17:43:05 +0800638{
Eduardo Habkost12d6e462014-06-26 18:33:20 -0300639 int i;
Igor Mammedovea089ee2017-05-10 13:29:45 +0200640 MachineClass *mc = MACHINE_GET_CLASS(ms);
Tao Xu7e721e72019-08-09 14:57:24 +0800641 NodeInfo *numa_info = ms->numa_state->nodes;
Eduardo Habkost12d6e462014-06-26 18:33:20 -0300642
Dou Liyang7b8be492017-11-14 10:34:01 +0800643 /*
David Hildenbrand195784a2020-06-26 09:22:48 +0200644 * If memory hotplug is enabled (slot > 0) or memory devices are enabled
Paolo Bonzinib326b6e2020-10-28 06:24:22 -0400645 * (ms->maxram_size > ms->ram_size) but without '-numa' options explicitly on
David Hildenbrand195784a2020-06-26 09:22:48 +0200646 * CLI, guests will break.
Dou Liyang7b8be492017-11-14 10:34:01 +0800647 *
648 * Windows: won't enable memory hotplug without SRAT table at all
649 *
650 * Linux: if QEMU is started with initial memory all below 4Gb
651 * and no SRAT table present, guest kernel will use nommu DMA ops,
652 * which breaks 32bit hw drivers when memory is hotplugged and
653 * guest tries to use it with that drivers.
654 *
655 * Enable NUMA implicitly by adding a new NUMA node automatically.
Tao Xu0533ef52019-09-05 16:32:38 +0800656 *
657 * Or if MachineClass::auto_enable_numa is true and no NUMA nodes,
658 * assume there is just one node with whole RAM.
Dou Liyang7b8be492017-11-14 10:34:01 +0800659 */
Tao Xu0533ef52019-09-05 16:32:38 +0800660 if (ms->numa_state->num_nodes == 0 &&
David Hildenbrand195784a2020-06-26 09:22:48 +0200661 ((ms->ram_slots && mc->auto_enable_numa_with_memhp) ||
662 (ms->maxram_size > ms->ram_size && mc->auto_enable_numa_with_memdev) ||
663 mc->auto_enable_numa)) {
Dou Liyang7b8be492017-11-14 10:34:01 +0800664 NumaNodeOptions node = { };
Markus Armbrustera22528b2018-10-17 10:26:39 +0200665 parse_numa_node(ms, &node, &error_abort);
Paolo Bonzinib326b6e2020-10-28 06:24:22 -0400666 numa_info[0].node_mem = ms->ram_size;
Dou Liyang7b8be492017-11-14 10:34:01 +0800667 }
668
Eduardo Habkost12d6e462014-06-26 18:33:20 -0300669 assert(max_numa_nodeid <= MAX_NODES);
670
671 /* No support for sparse NUMA node IDs yet: */
672 for (i = max_numa_nodeid - 1; i >= 0; i--) {
673 /* Report large node IDs first, to make mistakes easier to spot */
674 if (!numa_info[i].present) {
675 error_report("numa: Node ID missing: %d", i);
676 exit(1);
677 }
678 }
679
680 /* This must be always true if all nodes are present: */
Tao Xuaa570202019-08-09 14:57:22 +0800681 assert(ms->numa_state->num_nodes == max_numa_nodeid);
Eduardo Habkost12d6e462014-06-26 18:33:20 -0300682
Tao Xuaa570202019-08-09 14:57:22 +0800683 if (ms->numa_state->num_nodes > 0) {
Wanlong Gao2b631ec2014-05-14 17:43:06 +0800684 uint64_t numa_total;
Wanlong Gao96d0e262014-05-14 17:43:05 +0800685
Wanlong Gao2b631ec2014-05-14 17:43:06 +0800686 numa_total = 0;
Tao Xuaa570202019-08-09 14:57:22 +0800687 for (i = 0; i < ms->numa_state->num_nodes; i++) {
Wanlong Gao8c859012014-05-14 17:43:07 +0800688 numa_total += numa_info[i].node_mem;
Wanlong Gao2b631ec2014-05-14 17:43:06 +0800689 }
Paolo Bonzinib326b6e2020-10-28 06:24:22 -0400690 if (numa_total != ms->ram_size) {
Hu Taoc68233a2014-08-04 16:16:09 +0800691 error_report("total memory for NUMA nodes (0x%" PRIx64 ")"
692 " should equal RAM size (0x" RAM_ADDR_FMT ")",
Paolo Bonzinib326b6e2020-10-28 06:24:22 -0400693 numa_total, ms->ram_size);
Wanlong Gao2b631ec2014-05-14 17:43:06 +0800694 exit(1);
695 }
696
Igor Mammedov6b61c2c2020-02-19 11:08:39 -0500697 if (!numa_uses_legacy_mem() && mc->default_ram_id) {
Paolo Bonzini26f88d82022-04-14 12:52:59 -0400698 if (ms->memdev) {
Igor Mammedovea81f982020-05-11 10:11:03 -0400699 error_report("'-machine memory-backend' and '-numa memdev'"
700 " properties are mutually exclusive");
701 exit(1);
702 }
Igor Mammedov6b61c2c2020-02-19 11:08:39 -0500703 ms->ram = g_new(MemoryRegion, 1);
704 memory_region_init(ms->ram, OBJECT(ms), mc->default_ram_id,
Paolo Bonzinib326b6e2020-10-28 06:24:22 -0400705 ms->ram_size);
Igor Mammedov6b61c2c2020-02-19 11:08:39 -0500706 numa_init_memdev_container(ms, ms->ram);
707 }
He Chen0f203432017-04-27 10:35:58 +0800708 /* QEMU needs at least all unique node pair distances to build
709 * the whole NUMA distance table. QEMU treats the distance table
710 * as symmetric by default, i.e. distance A->B == distance B->A.
711 * Thus, QEMU is able to complete the distance table
712 * initialization even though only distance A->B is provided and
713 * distance B->A is not. QEMU knows the distance of a node to
714 * itself is always 10, so A->A distances may be omitted. When
715 * the distances of two nodes of a pair differ, i.e. distance
716 * A->B != distance B->A, then that means the distance table is
717 * asymmetric. In this case, the distances for both directions
718 * of all node pairs are required.
719 */
Tao Xu118154b2019-08-09 14:57:23 +0800720 if (ms->numa_state->have_numa_distance) {
He Chen0f203432017-04-27 10:35:58 +0800721 /* Validate enough NUMA distance information was provided. */
Tao Xuaa570202019-08-09 14:57:22 +0800722 validate_numa_distance(ms);
He Chen0f203432017-04-27 10:35:58 +0800723
724 /* Validation succeeded, now fill in any missing distances. */
Tao Xuaa570202019-08-09 14:57:22 +0800725 complete_init_numa_distance(ms);
He Chen0f203432017-04-27 10:35:58 +0800726 }
Wanlong Gao96d0e262014-05-14 17:43:05 +0800727 }
728}
729
Igor Mammedov7a3099f2018-05-04 10:37:39 +0200730void parse_numa_opts(MachineState *ms)
731{
Markus Armbruster4f7ec692018-10-17 10:26:52 +0200732 qemu_opts_foreach(qemu_find_opts("numa"), parse_numa, ms, &error_fatal);
Igor Mammedov7a3099f2018-05-04 10:37:39 +0200733}
734
Igor Mammedova0ceb642017-05-30 18:23:56 +0200735void numa_cpu_pre_plug(const CPUArchId *slot, DeviceState *dev, Error **errp)
736{
Igor Mammedova0ceb642017-05-30 18:23:56 +0200737 int node_id = object_property_get_int(OBJECT(dev), "node-id", &error_abort);
738
Igor Mammedova0ceb642017-05-30 18:23:56 +0200739 if (node_id == CPU_UNSET_NUMA_NODE_ID) {
740 /* due to bug in libvirt, it doesn't pass node-id from props on
741 * device_add as expected, so we have to fix it up here */
Igor Mammedovd41f3e72017-05-30 18:23:58 +0200742 if (slot->props.has_node_id) {
Markus Armbruster5325cc32020-07-07 18:05:54 +0200743 object_property_set_int(OBJECT(dev), "node-id",
744 slot->props.node_id, errp);
Igor Mammedovd41f3e72017-05-30 18:23:58 +0200745 }
746 } else if (node_id != slot->props.node_id) {
Laurent Viviera5bf9fb2019-05-29 18:07:47 +0200747 error_setg(errp, "invalid node-id, must be %"PRId64,
748 slot->props.node_id);
Igor Mammedova0ceb642017-05-30 18:23:56 +0200749 }
750}
751
Vadim Galitsyn31959e82017-08-29 17:30:20 +0200752static void numa_stat_memory_devices(NumaNodeMem node_mem[])
zhanghailiang5b009e42014-11-04 19:49:30 +0800753{
David Hildenbrand2cc0e2e2018-04-23 18:51:16 +0200754 MemoryDeviceInfoList *info_list = qmp_memory_device_list();
zhanghailiang5b009e42014-11-04 19:49:30 +0800755 MemoryDeviceInfoList *info;
Vadim Galitsyn31959e82017-08-29 17:30:20 +0200756 PCDIMMDeviceInfo *pcdimm_info;
David Hildenbrandcae02c32019-06-19 15:19:06 +0530757 VirtioPMEMDeviceInfo *vpi;
David Hildenbrand16647a82020-06-26 09:22:41 +0200758 VirtioMEMDeviceInfo *vmi;
Paolo Bonzinibd989ed2021-11-10 13:29:03 +0100759 SgxEPCDeviceInfo *se;
zhanghailiang5b009e42014-11-04 19:49:30 +0800760
zhanghailiang5b009e42014-11-04 19:49:30 +0800761 for (info = info_list; info; info = info->next) {
762 MemoryDeviceInfo *value = info->value;
763
764 if (value) {
Eric Blake1fd5d4f2015-10-26 16:34:59 -0600765 switch (value->type) {
Haozhong Zhang6388e182018-03-11 11:02:12 +0800766 case MEMORY_DEVICE_INFO_KIND_DIMM:
Haozhong Zhang6388e182018-03-11 11:02:12 +0800767 case MEMORY_DEVICE_INFO_KIND_NVDIMM:
David Hildenbrandcae02c32019-06-19 15:19:06 +0530768 pcdimm_info = value->type == MEMORY_DEVICE_INFO_KIND_DIMM ?
769 value->u.dimm.data : value->u.nvdimm.data;
Vadim Galitsyn31959e82017-08-29 17:30:20 +0200770 node_mem[pcdimm_info->node].node_mem += pcdimm_info->size;
David Hildenbrand178003e2018-06-22 16:40:45 +0200771 node_mem[pcdimm_info->node].node_plugged_mem +=
772 pcdimm_info->size;
David Hildenbrandcae02c32019-06-19 15:19:06 +0530773 break;
774 case MEMORY_DEVICE_INFO_KIND_VIRTIO_PMEM:
775 vpi = value->u.virtio_pmem.data;
776 /* TODO: once we support numa, assign to right node */
777 node_mem[0].node_mem += vpi->size;
778 node_mem[0].node_plugged_mem += vpi->size;
779 break;
David Hildenbrand16647a82020-06-26 09:22:41 +0200780 case MEMORY_DEVICE_INFO_KIND_VIRTIO_MEM:
781 vmi = value->u.virtio_mem.data;
782 node_mem[vmi->node].node_mem += vmi->size;
783 node_mem[vmi->node].node_plugged_mem += vmi->size;
784 break;
Paolo Bonzinibd989ed2021-11-10 13:29:03 +0100785 case MEMORY_DEVICE_INFO_KIND_SGX_EPC:
786 se = value->u.sgx_epc.data;
Yang Zhong11058122021-11-01 12:20:05 -0400787 node_mem[se->node].node_mem += se->size;
788 node_mem[se->node].node_plugged_mem = 0;
Paolo Bonzinibd989ed2021-11-10 13:29:03 +0100789 break;
David Hildenbrandcae02c32019-06-19 15:19:06 +0530790 default:
791 g_assert_not_reached();
zhanghailiang5b009e42014-11-04 19:49:30 +0800792 }
793 }
794 }
795 qapi_free_MemoryDeviceInfoList(info_list);
796}
797
Tao Xuaa570202019-08-09 14:57:22 +0800798void query_numa_node_mem(NumaNodeMem node_mem[], MachineState *ms)
zhanghailiang5b009e42014-11-04 19:49:30 +0800799{
800 int i;
801
Tao Xuaa570202019-08-09 14:57:22 +0800802 if (ms->numa_state == NULL || ms->numa_state->num_nodes <= 0) {
zhanghailiang5b009e42014-11-04 19:49:30 +0800803 return;
804 }
805
806 numa_stat_memory_devices(node_mem);
Tao Xuaa570202019-08-09 14:57:22 +0800807 for (i = 0; i < ms->numa_state->num_nodes; i++) {
Tao Xu7e721e72019-08-09 14:57:24 +0800808 node_mem[i].node_mem += ms->numa_state->nodes[i].node_mem;
zhanghailiang5b009e42014-11-04 19:49:30 +0800809 }
810}
811
David Hildenbrand082851a2021-04-29 13:26:59 +0200812static int ram_block_notify_add_single(RAMBlock *rb, void *opaque)
813{
814 const ram_addr_t max_size = qemu_ram_get_max_length(rb);
David Hildenbrand8f443042021-04-29 13:27:00 +0200815 const ram_addr_t size = qemu_ram_get_used_length(rb);
David Hildenbrand082851a2021-04-29 13:26:59 +0200816 void *host = qemu_ram_get_host_addr(rb);
817 RAMBlockNotifier *notifier = opaque;
818
819 if (host) {
David Hildenbrand8f443042021-04-29 13:27:00 +0200820 notifier->ram_block_added(notifier, host, size, max_size);
David Hildenbrand082851a2021-04-29 13:26:59 +0200821 }
822 return 0;
823}
824
Stefan Hajnoczi1f0fea32022-10-13 14:58:58 -0400825static int ram_block_notify_remove_single(RAMBlock *rb, void *opaque)
826{
827 const ram_addr_t max_size = qemu_ram_get_max_length(rb);
828 const ram_addr_t size = qemu_ram_get_used_length(rb);
829 void *host = qemu_ram_get_host_addr(rb);
830 RAMBlockNotifier *notifier = opaque;
831
832 if (host) {
833 notifier->ram_block_removed(notifier, host, size, max_size);
834 }
835 return 0;
836}
837
Paolo Bonzini0987d732016-12-21 00:31:36 +0800838void ram_block_notifier_add(RAMBlockNotifier *n)
839{
840 QLIST_INSERT_HEAD(&ram_list.ramblock_notifiers, n, next);
David Hildenbrande15c7d12021-04-29 13:27:01 +0200841
David Hildenbrand082851a2021-04-29 13:26:59 +0200842 /* Notify about all existing ram blocks. */
David Hildenbrande15c7d12021-04-29 13:27:01 +0200843 if (n->ram_block_added) {
844 qemu_ram_foreach_block(ram_block_notify_add_single, n);
845 }
Paolo Bonzini0987d732016-12-21 00:31:36 +0800846}
847
848void ram_block_notifier_remove(RAMBlockNotifier *n)
849{
850 QLIST_REMOVE(n, next);
Stefan Hajnoczi1f0fea32022-10-13 14:58:58 -0400851
852 if (n->ram_block_removed) {
853 qemu_ram_foreach_block(ram_block_notify_remove_single, n);
854 }
Paolo Bonzini0987d732016-12-21 00:31:36 +0800855}
856
David Hildenbrand8f443042021-04-29 13:27:00 +0200857void ram_block_notify_add(void *host, size_t size, size_t max_size)
Paolo Bonzini0987d732016-12-21 00:31:36 +0800858{
859 RAMBlockNotifier *notifier;
Stefan Hajnoczi4fdd0a12022-10-13 14:59:03 -0400860 RAMBlockNotifier *next;
Paolo Bonzini0987d732016-12-21 00:31:36 +0800861
Stefan Hajnoczi4fdd0a12022-10-13 14:59:03 -0400862 QLIST_FOREACH_SAFE(notifier, &ram_list.ramblock_notifiers, next, next) {
David Hildenbrande15c7d12021-04-29 13:27:01 +0200863 if (notifier->ram_block_added) {
864 notifier->ram_block_added(notifier, host, size, max_size);
865 }
Paolo Bonzini0987d732016-12-21 00:31:36 +0800866 }
867}
868
David Hildenbrand8f443042021-04-29 13:27:00 +0200869void ram_block_notify_remove(void *host, size_t size, size_t max_size)
Paolo Bonzini0987d732016-12-21 00:31:36 +0800870{
871 RAMBlockNotifier *notifier;
Stefan Hajnoczi4fdd0a12022-10-13 14:59:03 -0400872 RAMBlockNotifier *next;
Paolo Bonzini0987d732016-12-21 00:31:36 +0800873
Stefan Hajnoczi4fdd0a12022-10-13 14:59:03 -0400874 QLIST_FOREACH_SAFE(notifier, &ram_list.ramblock_notifiers, next, next) {
David Hildenbrande15c7d12021-04-29 13:27:01 +0200875 if (notifier->ram_block_removed) {
876 notifier->ram_block_removed(notifier, host, size, max_size);
877 }
David Hildenbrand8f443042021-04-29 13:27:00 +0200878 }
879}
880
881void ram_block_notify_resize(void *host, size_t old_size, size_t new_size)
882{
883 RAMBlockNotifier *notifier;
Stefan Hajnoczi4fdd0a12022-10-13 14:59:03 -0400884 RAMBlockNotifier *next;
David Hildenbrand8f443042021-04-29 13:27:00 +0200885
Stefan Hajnoczi4fdd0a12022-10-13 14:59:03 -0400886 QLIST_FOREACH_SAFE(notifier, &ram_list.ramblock_notifiers, next, next) {
David Hildenbrand8f443042021-04-29 13:27:00 +0200887 if (notifier->ram_block_resized) {
888 notifier->ram_block_resized(notifier, host, old_size, new_size);
889 }
Paolo Bonzini0987d732016-12-21 00:31:36 +0800890 }
891}