1ac057879SMarkus Armbruster /* 2ac057879SMarkus Armbruster * NUMA parameter parsing routines 3ac057879SMarkus Armbruster * 4ac057879SMarkus Armbruster * Copyright (c) 2014 Fujitsu Ltd. 5ac057879SMarkus Armbruster * 6ac057879SMarkus Armbruster * Permission is hereby granted, free of charge, to any person obtaining a copy 7ac057879SMarkus Armbruster * of this software and associated documentation files (the "Software"), to deal 8ac057879SMarkus Armbruster * in the Software without restriction, including without limitation the rights 9ac057879SMarkus Armbruster * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10ac057879SMarkus Armbruster * copies of the Software, and to permit persons to whom the Software is 11ac057879SMarkus Armbruster * furnished to do so, subject to the following conditions: 12ac057879SMarkus Armbruster * 13ac057879SMarkus Armbruster * The above copyright notice and this permission notice shall be included in 14ac057879SMarkus Armbruster * all copies or substantial portions of the Software. 15ac057879SMarkus Armbruster * 16ac057879SMarkus Armbruster * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17ac057879SMarkus Armbruster * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18ac057879SMarkus Armbruster * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19ac057879SMarkus Armbruster * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20ac057879SMarkus Armbruster * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21ac057879SMarkus Armbruster * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22ac057879SMarkus Armbruster * THE SOFTWARE. 23ac057879SMarkus Armbruster */ 24ac057879SMarkus Armbruster 25ac057879SMarkus Armbruster #include "qemu/osdep.h" 26ac057879SMarkus Armbruster #include "sysemu/numa.h" 27ac057879SMarkus Armbruster #include "exec/cpu-common.h" 28ac057879SMarkus Armbruster #include "exec/ramlist.h" 29ac057879SMarkus Armbruster #include "qemu/bitmap.h" 30ac057879SMarkus Armbruster #include "qemu/error-report.h" 31ac057879SMarkus Armbruster #include "qapi/error.h" 32ac057879SMarkus Armbruster #include "qapi/opts-visitor.h" 33ac057879SMarkus Armbruster #include "qapi/qapi-visit-machine.h" 34f8123f22SEduardo Habkost #include "sysemu/qtest.h" 35ac057879SMarkus Armbruster #include "hw/mem/pc-dimm.h" 36*d6454270SMarkus Armbruster #include "migration/vmstate.h" 37ac057879SMarkus Armbruster #include "hw/mem/memory-device.h" 38ac057879SMarkus Armbruster #include "qemu/option.h" 39ac057879SMarkus Armbruster #include "qemu/config-file.h" 40ac057879SMarkus Armbruster #include "qemu/cutils.h" 41ac057879SMarkus Armbruster 42ac057879SMarkus Armbruster QemuOptsList qemu_numa_opts = { 43ac057879SMarkus Armbruster .name = "numa", 44ac057879SMarkus Armbruster .implied_opt_name = "type", 45ac057879SMarkus Armbruster .head = QTAILQ_HEAD_INITIALIZER(qemu_numa_opts.head), 46ac057879SMarkus Armbruster .desc = { { 0 } } /* validated with OptsVisitor */ 47ac057879SMarkus Armbruster }; 48ac057879SMarkus Armbruster 49b69239e0SIgor Mammedov static int have_memdevs; 50b69239e0SIgor Mammedov static int have_mem; 51ac057879SMarkus Armbruster static int max_numa_nodeid; /* Highest specified NUMA node ID, plus one. 52ac057879SMarkus Armbruster * For all nodes, nodeid < max_numa_nodeid 53ac057879SMarkus Armbruster */ 54ac057879SMarkus Armbruster int nb_numa_nodes; 55ac057879SMarkus Armbruster bool have_numa_distance; 56ac057879SMarkus Armbruster NodeInfo numa_info[MAX_NODES]; 57ac057879SMarkus Armbruster 58ac057879SMarkus Armbruster 59ac057879SMarkus Armbruster static void parse_numa_node(MachineState *ms, NumaNodeOptions *node, 60ac057879SMarkus Armbruster Error **errp) 61ac057879SMarkus Armbruster { 62ac057879SMarkus Armbruster Error *err = NULL; 63ac057879SMarkus Armbruster uint16_t nodenr; 64ac057879SMarkus Armbruster uint16List *cpus = NULL; 65ac057879SMarkus Armbruster MachineClass *mc = MACHINE_GET_CLASS(ms); 665cc8767dSLike Xu unsigned int max_cpus = ms->smp.max_cpus; 67ac057879SMarkus Armbruster 68ac057879SMarkus Armbruster if (node->has_nodeid) { 69ac057879SMarkus Armbruster nodenr = node->nodeid; 70ac057879SMarkus Armbruster } else { 71ac057879SMarkus Armbruster nodenr = nb_numa_nodes; 72ac057879SMarkus Armbruster } 73ac057879SMarkus Armbruster 74ac057879SMarkus Armbruster if (nodenr >= MAX_NODES) { 75ac057879SMarkus Armbruster error_setg(errp, "Max number of NUMA nodes reached: %" 76ac057879SMarkus Armbruster PRIu16 "", nodenr); 77ac057879SMarkus Armbruster return; 78ac057879SMarkus Armbruster } 79ac057879SMarkus Armbruster 80ac057879SMarkus Armbruster if (numa_info[nodenr].present) { 81ac057879SMarkus Armbruster error_setg(errp, "Duplicate NUMA nodeid: %" PRIu16, nodenr); 82ac057879SMarkus Armbruster return; 83ac057879SMarkus Armbruster } 84ac057879SMarkus Armbruster 85ac057879SMarkus Armbruster if (!mc->cpu_index_to_instance_props || !mc->get_default_cpu_node_id) { 86ac057879SMarkus Armbruster error_setg(errp, "NUMA is not supported by this machine-type"); 87ac057879SMarkus Armbruster return; 88ac057879SMarkus Armbruster } 89ac057879SMarkus Armbruster for (cpus = node->cpus; cpus; cpus = cpus->next) { 90ac057879SMarkus Armbruster CpuInstanceProperties props; 91ac057879SMarkus Armbruster if (cpus->value >= max_cpus) { 92ac057879SMarkus Armbruster error_setg(errp, 93ac057879SMarkus Armbruster "CPU index (%" PRIu16 ")" 94ac057879SMarkus Armbruster " should be smaller than maxcpus (%d)", 95ac057879SMarkus Armbruster cpus->value, max_cpus); 96ac057879SMarkus Armbruster return; 97ac057879SMarkus Armbruster } 98ac057879SMarkus Armbruster props = mc->cpu_index_to_instance_props(ms, cpus->value); 99ac057879SMarkus Armbruster props.node_id = nodenr; 100ac057879SMarkus Armbruster props.has_node_id = true; 101ac057879SMarkus Armbruster machine_set_cpu_numa_node(ms, &props, &err); 102ac057879SMarkus Armbruster if (err) { 103ac057879SMarkus Armbruster error_propagate(errp, err); 104ac057879SMarkus Armbruster return; 105ac057879SMarkus Armbruster } 106ac057879SMarkus Armbruster } 107ac057879SMarkus Armbruster 108b69239e0SIgor Mammedov have_memdevs = have_memdevs ? : node->has_memdev; 109b69239e0SIgor Mammedov have_mem = have_mem ? : node->has_mem; 110b69239e0SIgor Mammedov if ((node->has_mem && have_memdevs) || (node->has_memdev && have_mem)) { 111b69239e0SIgor Mammedov error_setg(errp, "numa configuration should use either mem= or memdev=," 112b69239e0SIgor Mammedov "mixing both is not allowed"); 113ac057879SMarkus Armbruster return; 114ac057879SMarkus Armbruster } 115ac057879SMarkus Armbruster 116ac057879SMarkus Armbruster if (node->has_mem) { 117ac057879SMarkus Armbruster numa_info[nodenr].node_mem = node->mem; 118f8123f22SEduardo Habkost if (!qtest_enabled()) { 119cdf80365SIgor Mammedov warn_report("Parameter -numa node,mem is deprecated," 120cdf80365SIgor Mammedov " use -numa node,memdev instead"); 121ac057879SMarkus Armbruster } 122f8123f22SEduardo Habkost } 123ac057879SMarkus Armbruster if (node->has_memdev) { 124ac057879SMarkus Armbruster Object *o; 125ac057879SMarkus Armbruster o = object_resolve_path_type(node->memdev, TYPE_MEMORY_BACKEND, NULL); 126ac057879SMarkus Armbruster if (!o) { 127ac057879SMarkus Armbruster error_setg(errp, "memdev=%s is ambiguous", node->memdev); 128ac057879SMarkus Armbruster return; 129ac057879SMarkus Armbruster } 130ac057879SMarkus Armbruster 131ac057879SMarkus Armbruster object_ref(o); 132ac057879SMarkus Armbruster numa_info[nodenr].node_mem = object_property_get_uint(o, "size", NULL); 133ac057879SMarkus Armbruster numa_info[nodenr].node_memdev = MEMORY_BACKEND(o); 134ac057879SMarkus Armbruster } 135ac057879SMarkus Armbruster numa_info[nodenr].present = true; 136ac057879SMarkus Armbruster max_numa_nodeid = MAX(max_numa_nodeid, nodenr + 1); 137ac057879SMarkus Armbruster nb_numa_nodes++; 138ac057879SMarkus Armbruster } 139ac057879SMarkus Armbruster 140ac057879SMarkus Armbruster static void parse_numa_distance(NumaDistOptions *dist, Error **errp) 141ac057879SMarkus Armbruster { 142ac057879SMarkus Armbruster uint16_t src = dist->src; 143ac057879SMarkus Armbruster uint16_t dst = dist->dst; 144ac057879SMarkus Armbruster uint8_t val = dist->val; 145ac057879SMarkus Armbruster 146ac057879SMarkus Armbruster if (src >= MAX_NODES || dst >= MAX_NODES) { 147ac057879SMarkus Armbruster error_setg(errp, "Parameter '%s' expects an integer between 0 and %d", 148ac057879SMarkus Armbruster src >= MAX_NODES ? "src" : "dst", MAX_NODES - 1); 149ac057879SMarkus Armbruster return; 150ac057879SMarkus Armbruster } 151ac057879SMarkus Armbruster 152ac057879SMarkus Armbruster if (!numa_info[src].present || !numa_info[dst].present) { 153ac057879SMarkus Armbruster error_setg(errp, "Source/Destination NUMA node is missing. " 154ac057879SMarkus Armbruster "Please use '-numa node' option to declare it first."); 155ac057879SMarkus Armbruster return; 156ac057879SMarkus Armbruster } 157ac057879SMarkus Armbruster 158ac057879SMarkus Armbruster if (val < NUMA_DISTANCE_MIN) { 159ac057879SMarkus Armbruster error_setg(errp, "NUMA distance (%" PRIu8 ") is invalid, " 160ac057879SMarkus Armbruster "it shouldn't be less than %d.", 161ac057879SMarkus Armbruster val, NUMA_DISTANCE_MIN); 162ac057879SMarkus Armbruster return; 163ac057879SMarkus Armbruster } 164ac057879SMarkus Armbruster 165ac057879SMarkus Armbruster if (src == dst && val != NUMA_DISTANCE_MIN) { 166ac057879SMarkus Armbruster error_setg(errp, "Local distance of node %d should be %d.", 167ac057879SMarkus Armbruster src, NUMA_DISTANCE_MIN); 168ac057879SMarkus Armbruster return; 169ac057879SMarkus Armbruster } 170ac057879SMarkus Armbruster 171ac057879SMarkus Armbruster numa_info[src].distance[dst] = val; 172ac057879SMarkus Armbruster have_numa_distance = true; 173ac057879SMarkus Armbruster } 174ac057879SMarkus Armbruster 175ac057879SMarkus Armbruster void set_numa_options(MachineState *ms, NumaOptions *object, Error **errp) 176ac057879SMarkus Armbruster { 177ac057879SMarkus Armbruster Error *err = NULL; 178ac057879SMarkus Armbruster 179ac057879SMarkus Armbruster switch (object->type) { 180ac057879SMarkus Armbruster case NUMA_OPTIONS_TYPE_NODE: 181ac057879SMarkus Armbruster parse_numa_node(ms, &object->u.node, &err); 182ac057879SMarkus Armbruster if (err) { 183ac057879SMarkus Armbruster goto end; 184ac057879SMarkus Armbruster } 185ac057879SMarkus Armbruster break; 186ac057879SMarkus Armbruster case NUMA_OPTIONS_TYPE_DIST: 187ac057879SMarkus Armbruster parse_numa_distance(&object->u.dist, &err); 188ac057879SMarkus Armbruster if (err) { 189ac057879SMarkus Armbruster goto end; 190ac057879SMarkus Armbruster } 191ac057879SMarkus Armbruster break; 192ac057879SMarkus Armbruster case NUMA_OPTIONS_TYPE_CPU: 193ac057879SMarkus Armbruster if (!object->u.cpu.has_node_id) { 194ac057879SMarkus Armbruster error_setg(&err, "Missing mandatory node-id property"); 195ac057879SMarkus Armbruster goto end; 196ac057879SMarkus Armbruster } 197ac057879SMarkus Armbruster if (!numa_info[object->u.cpu.node_id].present) { 198ac057879SMarkus Armbruster error_setg(&err, "Invalid node-id=%" PRId64 ", NUMA node must be " 199ac057879SMarkus Armbruster "defined with -numa node,nodeid=ID before it's used with " 200ac057879SMarkus Armbruster "-numa cpu,node-id=ID", object->u.cpu.node_id); 201ac057879SMarkus Armbruster goto end; 202ac057879SMarkus Armbruster } 203ac057879SMarkus Armbruster 204ac057879SMarkus Armbruster machine_set_cpu_numa_node(ms, qapi_NumaCpuOptions_base(&object->u.cpu), 205ac057879SMarkus Armbruster &err); 206ac057879SMarkus Armbruster break; 207ac057879SMarkus Armbruster default: 208ac057879SMarkus Armbruster abort(); 209ac057879SMarkus Armbruster } 210ac057879SMarkus Armbruster 211ac057879SMarkus Armbruster end: 212ac057879SMarkus Armbruster error_propagate(errp, err); 213ac057879SMarkus Armbruster } 214ac057879SMarkus Armbruster 215ac057879SMarkus Armbruster static int parse_numa(void *opaque, QemuOpts *opts, Error **errp) 216ac057879SMarkus Armbruster { 217ac057879SMarkus Armbruster NumaOptions *object = NULL; 218ac057879SMarkus Armbruster MachineState *ms = MACHINE(opaque); 219ac057879SMarkus Armbruster Error *err = NULL; 220ac057879SMarkus Armbruster Visitor *v = opts_visitor_new(opts); 221ac057879SMarkus Armbruster 222ac057879SMarkus Armbruster visit_type_NumaOptions(v, NULL, &object, &err); 223ac057879SMarkus Armbruster visit_free(v); 224ac057879SMarkus Armbruster if (err) { 225ac057879SMarkus Armbruster goto end; 226ac057879SMarkus Armbruster } 227ac057879SMarkus Armbruster 228ac057879SMarkus Armbruster /* Fix up legacy suffix-less format */ 229ac057879SMarkus Armbruster if ((object->type == NUMA_OPTIONS_TYPE_NODE) && object->u.node.has_mem) { 230ac057879SMarkus Armbruster const char *mem_str = qemu_opt_get(opts, "mem"); 231ac057879SMarkus Armbruster qemu_strtosz_MiB(mem_str, NULL, &object->u.node.mem); 232ac057879SMarkus Armbruster } 233ac057879SMarkus Armbruster 234ac057879SMarkus Armbruster set_numa_options(ms, object, &err); 235ac057879SMarkus Armbruster 236ac057879SMarkus Armbruster end: 237ac057879SMarkus Armbruster qapi_free_NumaOptions(object); 238ac057879SMarkus Armbruster if (err) { 239ac057879SMarkus Armbruster error_propagate(errp, err); 240ac057879SMarkus Armbruster return -1; 241ac057879SMarkus Armbruster } 242ac057879SMarkus Armbruster 243ac057879SMarkus Armbruster return 0; 244ac057879SMarkus Armbruster } 245ac057879SMarkus Armbruster 246ac057879SMarkus Armbruster /* If all node pair distances are symmetric, then only distances 247ac057879SMarkus Armbruster * in one direction are enough. If there is even one asymmetric 248ac057879SMarkus Armbruster * pair, though, then all distances must be provided. The 249ac057879SMarkus Armbruster * distance from a node to itself is always NUMA_DISTANCE_MIN, 250ac057879SMarkus Armbruster * so providing it is never necessary. 251ac057879SMarkus Armbruster */ 252ac057879SMarkus Armbruster static void validate_numa_distance(void) 253ac057879SMarkus Armbruster { 254ac057879SMarkus Armbruster int src, dst; 255ac057879SMarkus Armbruster bool is_asymmetrical = false; 256ac057879SMarkus Armbruster 257ac057879SMarkus Armbruster for (src = 0; src < nb_numa_nodes; src++) { 258ac057879SMarkus Armbruster for (dst = src; dst < nb_numa_nodes; dst++) { 259ac057879SMarkus Armbruster if (numa_info[src].distance[dst] == 0 && 260ac057879SMarkus Armbruster numa_info[dst].distance[src] == 0) { 261ac057879SMarkus Armbruster if (src != dst) { 262ac057879SMarkus Armbruster error_report("The distance between node %d and %d is " 263ac057879SMarkus Armbruster "missing, at least one distance value " 264ac057879SMarkus Armbruster "between each nodes should be provided.", 265ac057879SMarkus Armbruster src, dst); 266ac057879SMarkus Armbruster exit(EXIT_FAILURE); 267ac057879SMarkus Armbruster } 268ac057879SMarkus Armbruster } 269ac057879SMarkus Armbruster 270ac057879SMarkus Armbruster if (numa_info[src].distance[dst] != 0 && 271ac057879SMarkus Armbruster numa_info[dst].distance[src] != 0 && 272ac057879SMarkus Armbruster numa_info[src].distance[dst] != 273ac057879SMarkus Armbruster numa_info[dst].distance[src]) { 274ac057879SMarkus Armbruster is_asymmetrical = true; 275ac057879SMarkus Armbruster } 276ac057879SMarkus Armbruster } 277ac057879SMarkus Armbruster } 278ac057879SMarkus Armbruster 279ac057879SMarkus Armbruster if (is_asymmetrical) { 280ac057879SMarkus Armbruster for (src = 0; src < nb_numa_nodes; src++) { 281ac057879SMarkus Armbruster for (dst = 0; dst < nb_numa_nodes; dst++) { 282ac057879SMarkus Armbruster if (src != dst && numa_info[src].distance[dst] == 0) { 283ac057879SMarkus Armbruster error_report("At least one asymmetrical pair of " 284ac057879SMarkus Armbruster "distances is given, please provide distances " 285ac057879SMarkus Armbruster "for both directions of all node pairs."); 286ac057879SMarkus Armbruster exit(EXIT_FAILURE); 287ac057879SMarkus Armbruster } 288ac057879SMarkus Armbruster } 289ac057879SMarkus Armbruster } 290ac057879SMarkus Armbruster } 291ac057879SMarkus Armbruster } 292ac057879SMarkus Armbruster 293ac057879SMarkus Armbruster static void complete_init_numa_distance(void) 294ac057879SMarkus Armbruster { 295ac057879SMarkus Armbruster int src, dst; 296ac057879SMarkus Armbruster 297ac057879SMarkus Armbruster /* Fixup NUMA distance by symmetric policy because if it is an 298ac057879SMarkus Armbruster * asymmetric distance table, it should be a complete table and 299ac057879SMarkus Armbruster * there would not be any missing distance except local node, which 300ac057879SMarkus Armbruster * is verified by validate_numa_distance above. 301ac057879SMarkus Armbruster */ 302ac057879SMarkus Armbruster for (src = 0; src < nb_numa_nodes; src++) { 303ac057879SMarkus Armbruster for (dst = 0; dst < nb_numa_nodes; dst++) { 304ac057879SMarkus Armbruster if (numa_info[src].distance[dst] == 0) { 305ac057879SMarkus Armbruster if (src == dst) { 306ac057879SMarkus Armbruster numa_info[src].distance[dst] = NUMA_DISTANCE_MIN; 307ac057879SMarkus Armbruster } else { 308ac057879SMarkus Armbruster numa_info[src].distance[dst] = numa_info[dst].distance[src]; 309ac057879SMarkus Armbruster } 310ac057879SMarkus Armbruster } 311ac057879SMarkus Armbruster } 312ac057879SMarkus Armbruster } 313ac057879SMarkus Armbruster } 314ac057879SMarkus Armbruster 315ac057879SMarkus Armbruster void numa_legacy_auto_assign_ram(MachineClass *mc, NodeInfo *nodes, 316ac057879SMarkus Armbruster int nb_nodes, ram_addr_t size) 317ac057879SMarkus Armbruster { 318ac057879SMarkus Armbruster int i; 319ac057879SMarkus Armbruster uint64_t usedmem = 0; 320ac057879SMarkus Armbruster 321ac057879SMarkus Armbruster /* Align each node according to the alignment 322ac057879SMarkus Armbruster * requirements of the machine class 323ac057879SMarkus Armbruster */ 324ac057879SMarkus Armbruster 325ac057879SMarkus Armbruster for (i = 0; i < nb_nodes - 1; i++) { 326ac057879SMarkus Armbruster nodes[i].node_mem = (size / nb_nodes) & 327ac057879SMarkus Armbruster ~((1 << mc->numa_mem_align_shift) - 1); 328ac057879SMarkus Armbruster usedmem += nodes[i].node_mem; 329ac057879SMarkus Armbruster } 330ac057879SMarkus Armbruster nodes[i].node_mem = size - usedmem; 331ac057879SMarkus Armbruster } 332ac057879SMarkus Armbruster 333ac057879SMarkus Armbruster void numa_default_auto_assign_ram(MachineClass *mc, NodeInfo *nodes, 334ac057879SMarkus Armbruster int nb_nodes, ram_addr_t size) 335ac057879SMarkus Armbruster { 336ac057879SMarkus Armbruster int i; 337ac057879SMarkus Armbruster uint64_t usedmem = 0, node_mem; 338ac057879SMarkus Armbruster uint64_t granularity = size / nb_nodes; 339ac057879SMarkus Armbruster uint64_t propagate = 0; 340ac057879SMarkus Armbruster 341ac057879SMarkus Armbruster for (i = 0; i < nb_nodes - 1; i++) { 342ac057879SMarkus Armbruster node_mem = (granularity + propagate) & 343ac057879SMarkus Armbruster ~((1 << mc->numa_mem_align_shift) - 1); 344ac057879SMarkus Armbruster propagate = granularity + propagate - node_mem; 345ac057879SMarkus Armbruster nodes[i].node_mem = node_mem; 346ac057879SMarkus Armbruster usedmem += node_mem; 347ac057879SMarkus Armbruster } 348ac057879SMarkus Armbruster nodes[i].node_mem = size - usedmem; 349ac057879SMarkus Armbruster } 350ac057879SMarkus Armbruster 351ac057879SMarkus Armbruster void numa_complete_configuration(MachineState *ms) 352ac057879SMarkus Armbruster { 353ac057879SMarkus Armbruster int i; 354ac057879SMarkus Armbruster MachineClass *mc = MACHINE_GET_CLASS(ms); 355ac057879SMarkus Armbruster 356ac057879SMarkus Armbruster /* 357ac057879SMarkus Armbruster * If memory hotplug is enabled (slots > 0) but without '-numa' 358ac057879SMarkus Armbruster * options explicitly on CLI, guestes will break. 359ac057879SMarkus Armbruster * 360ac057879SMarkus Armbruster * Windows: won't enable memory hotplug without SRAT table at all 361ac057879SMarkus Armbruster * 362ac057879SMarkus Armbruster * Linux: if QEMU is started with initial memory all below 4Gb 363ac057879SMarkus Armbruster * and no SRAT table present, guest kernel will use nommu DMA ops, 364ac057879SMarkus Armbruster * which breaks 32bit hw drivers when memory is hotplugged and 365ac057879SMarkus Armbruster * guest tries to use it with that drivers. 366ac057879SMarkus Armbruster * 367ac057879SMarkus Armbruster * Enable NUMA implicitly by adding a new NUMA node automatically. 368ac057879SMarkus Armbruster */ 369ac057879SMarkus Armbruster if (ms->ram_slots > 0 && nb_numa_nodes == 0 && 370ac057879SMarkus Armbruster mc->auto_enable_numa_with_memhp) { 371ac057879SMarkus Armbruster NumaNodeOptions node = { }; 372ac057879SMarkus Armbruster parse_numa_node(ms, &node, &error_abort); 373ac057879SMarkus Armbruster } 374ac057879SMarkus Armbruster 375ac057879SMarkus Armbruster assert(max_numa_nodeid <= MAX_NODES); 376ac057879SMarkus Armbruster 377ac057879SMarkus Armbruster /* No support for sparse NUMA node IDs yet: */ 378ac057879SMarkus Armbruster for (i = max_numa_nodeid - 1; i >= 0; i--) { 379ac057879SMarkus Armbruster /* Report large node IDs first, to make mistakes easier to spot */ 380ac057879SMarkus Armbruster if (!numa_info[i].present) { 381ac057879SMarkus Armbruster error_report("numa: Node ID missing: %d", i); 382ac057879SMarkus Armbruster exit(1); 383ac057879SMarkus Armbruster } 384ac057879SMarkus Armbruster } 385ac057879SMarkus Armbruster 386ac057879SMarkus Armbruster /* This must be always true if all nodes are present: */ 387ac057879SMarkus Armbruster assert(nb_numa_nodes == max_numa_nodeid); 388ac057879SMarkus Armbruster 389ac057879SMarkus Armbruster if (nb_numa_nodes > 0) { 390ac057879SMarkus Armbruster uint64_t numa_total; 391ac057879SMarkus Armbruster 392ac057879SMarkus Armbruster if (nb_numa_nodes > MAX_NODES) { 393ac057879SMarkus Armbruster nb_numa_nodes = MAX_NODES; 394ac057879SMarkus Armbruster } 395ac057879SMarkus Armbruster 396ac057879SMarkus Armbruster /* If no memory size is given for any node, assume the default case 397ac057879SMarkus Armbruster * and distribute the available memory equally across all nodes 398ac057879SMarkus Armbruster */ 399ac057879SMarkus Armbruster for (i = 0; i < nb_numa_nodes; i++) { 400ac057879SMarkus Armbruster if (numa_info[i].node_mem != 0) { 401ac057879SMarkus Armbruster break; 402ac057879SMarkus Armbruster } 403ac057879SMarkus Armbruster } 404ac057879SMarkus Armbruster if (i == nb_numa_nodes) { 405ac057879SMarkus Armbruster assert(mc->numa_auto_assign_ram); 406ac057879SMarkus Armbruster mc->numa_auto_assign_ram(mc, numa_info, nb_numa_nodes, ram_size); 407f8123f22SEduardo Habkost if (!qtest_enabled()) { 4084bb4a273SIgor Mammedov warn_report("Default splitting of RAM between nodes is deprecated," 4094bb4a273SIgor Mammedov " Use '-numa node,memdev' to explictly define RAM" 4104bb4a273SIgor Mammedov " allocation per node"); 411ac057879SMarkus Armbruster } 412f8123f22SEduardo Habkost } 413ac057879SMarkus Armbruster 414ac057879SMarkus Armbruster numa_total = 0; 415ac057879SMarkus Armbruster for (i = 0; i < nb_numa_nodes; i++) { 416ac057879SMarkus Armbruster numa_total += numa_info[i].node_mem; 417ac057879SMarkus Armbruster } 418ac057879SMarkus Armbruster if (numa_total != ram_size) { 419ac057879SMarkus Armbruster error_report("total memory for NUMA nodes (0x%" PRIx64 ")" 420ac057879SMarkus Armbruster " should equal RAM size (0x" RAM_ADDR_FMT ")", 421ac057879SMarkus Armbruster numa_total, ram_size); 422ac057879SMarkus Armbruster exit(1); 423ac057879SMarkus Armbruster } 424ac057879SMarkus Armbruster 425ac057879SMarkus Armbruster /* QEMU needs at least all unique node pair distances to build 426ac057879SMarkus Armbruster * the whole NUMA distance table. QEMU treats the distance table 427ac057879SMarkus Armbruster * as symmetric by default, i.e. distance A->B == distance B->A. 428ac057879SMarkus Armbruster * Thus, QEMU is able to complete the distance table 429ac057879SMarkus Armbruster * initialization even though only distance A->B is provided and 430ac057879SMarkus Armbruster * distance B->A is not. QEMU knows the distance of a node to 431ac057879SMarkus Armbruster * itself is always 10, so A->A distances may be omitted. When 432ac057879SMarkus Armbruster * the distances of two nodes of a pair differ, i.e. distance 433ac057879SMarkus Armbruster * A->B != distance B->A, then that means the distance table is 434ac057879SMarkus Armbruster * asymmetric. In this case, the distances for both directions 435ac057879SMarkus Armbruster * of all node pairs are required. 436ac057879SMarkus Armbruster */ 437ac057879SMarkus Armbruster if (have_numa_distance) { 438ac057879SMarkus Armbruster /* Validate enough NUMA distance information was provided. */ 439ac057879SMarkus Armbruster validate_numa_distance(); 440ac057879SMarkus Armbruster 441ac057879SMarkus Armbruster /* Validation succeeded, now fill in any missing distances. */ 442ac057879SMarkus Armbruster complete_init_numa_distance(); 443ac057879SMarkus Armbruster } 444ac057879SMarkus Armbruster } 445ac057879SMarkus Armbruster } 446ac057879SMarkus Armbruster 447ac057879SMarkus Armbruster void parse_numa_opts(MachineState *ms) 448ac057879SMarkus Armbruster { 449ac057879SMarkus Armbruster qemu_opts_foreach(qemu_find_opts("numa"), parse_numa, ms, &error_fatal); 450ac057879SMarkus Armbruster } 451ac057879SMarkus Armbruster 452ac057879SMarkus Armbruster void numa_cpu_pre_plug(const CPUArchId *slot, DeviceState *dev, Error **errp) 453ac057879SMarkus Armbruster { 454ac057879SMarkus Armbruster int node_id = object_property_get_int(OBJECT(dev), "node-id", &error_abort); 455ac057879SMarkus Armbruster 456ac057879SMarkus Armbruster if (node_id == CPU_UNSET_NUMA_NODE_ID) { 457ac057879SMarkus Armbruster /* due to bug in libvirt, it doesn't pass node-id from props on 458ac057879SMarkus Armbruster * device_add as expected, so we have to fix it up here */ 459ac057879SMarkus Armbruster if (slot->props.has_node_id) { 460ac057879SMarkus Armbruster object_property_set_int(OBJECT(dev), slot->props.node_id, 461ac057879SMarkus Armbruster "node-id", errp); 462ac057879SMarkus Armbruster } 463ac057879SMarkus Armbruster } else if (node_id != slot->props.node_id) { 464ac057879SMarkus Armbruster error_setg(errp, "invalid node-id, must be %"PRId64, 465ac057879SMarkus Armbruster slot->props.node_id); 466ac057879SMarkus Armbruster } 467ac057879SMarkus Armbruster } 468ac057879SMarkus Armbruster 469ac057879SMarkus Armbruster static void allocate_system_memory_nonnuma(MemoryRegion *mr, Object *owner, 470ac057879SMarkus Armbruster const char *name, 471ac057879SMarkus Armbruster uint64_t ram_size) 472ac057879SMarkus Armbruster { 473ac057879SMarkus Armbruster if (mem_path) { 474ac057879SMarkus Armbruster #ifdef __linux__ 475ac057879SMarkus Armbruster Error *err = NULL; 476ac057879SMarkus Armbruster memory_region_init_ram_from_file(mr, owner, name, ram_size, 0, 0, 477ac057879SMarkus Armbruster mem_path, &err); 478ac057879SMarkus Armbruster if (err) { 479ac057879SMarkus Armbruster error_report_err(err); 480ac057879SMarkus Armbruster if (mem_prealloc) { 481ac057879SMarkus Armbruster exit(1); 482ac057879SMarkus Armbruster } 483cb79224bSIgor Mammedov warn_report("falling back to regular RAM allocation"); 484cb79224bSIgor Mammedov error_printf("This is deprecated. Make sure that -mem-path " 485cb79224bSIgor Mammedov " specified path has sufficient resources to allocate" 486cb79224bSIgor Mammedov " -m specified RAM amount"); 487ac057879SMarkus Armbruster /* Legacy behavior: if allocation failed, fall back to 488ac057879SMarkus Armbruster * regular RAM allocation. 489ac057879SMarkus Armbruster */ 490ac057879SMarkus Armbruster mem_path = NULL; 491ac057879SMarkus Armbruster memory_region_init_ram_nomigrate(mr, owner, name, ram_size, &error_fatal); 492ac057879SMarkus Armbruster } 493ac057879SMarkus Armbruster #else 494ac057879SMarkus Armbruster fprintf(stderr, "-mem-path not supported on this host\n"); 495ac057879SMarkus Armbruster exit(1); 496ac057879SMarkus Armbruster #endif 497ac057879SMarkus Armbruster } else { 498ac057879SMarkus Armbruster memory_region_init_ram_nomigrate(mr, owner, name, ram_size, &error_fatal); 499ac057879SMarkus Armbruster } 500ac057879SMarkus Armbruster vmstate_register_ram_global(mr); 501ac057879SMarkus Armbruster } 502ac057879SMarkus Armbruster 503ac057879SMarkus Armbruster void memory_region_allocate_system_memory(MemoryRegion *mr, Object *owner, 504ac057879SMarkus Armbruster const char *name, 505ac057879SMarkus Armbruster uint64_t ram_size) 506ac057879SMarkus Armbruster { 507ac057879SMarkus Armbruster uint64_t addr = 0; 508ac057879SMarkus Armbruster int i; 509ac057879SMarkus Armbruster 510ac057879SMarkus Armbruster if (nb_numa_nodes == 0 || !have_memdevs) { 511ac057879SMarkus Armbruster allocate_system_memory_nonnuma(mr, owner, name, ram_size); 512ac057879SMarkus Armbruster return; 513ac057879SMarkus Armbruster } 514ac057879SMarkus Armbruster 515ac057879SMarkus Armbruster memory_region_init(mr, owner, name, ram_size); 516ac057879SMarkus Armbruster for (i = 0; i < nb_numa_nodes; i++) { 517ac057879SMarkus Armbruster uint64_t size = numa_info[i].node_mem; 518ac057879SMarkus Armbruster HostMemoryBackend *backend = numa_info[i].node_memdev; 519ac057879SMarkus Armbruster if (!backend) { 520ac057879SMarkus Armbruster continue; 521ac057879SMarkus Armbruster } 522ac057879SMarkus Armbruster MemoryRegion *seg = host_memory_backend_get_memory(backend); 523ac057879SMarkus Armbruster 524ac057879SMarkus Armbruster if (memory_region_is_mapped(seg)) { 525ac057879SMarkus Armbruster char *path = object_get_canonical_path_component(OBJECT(backend)); 526ac057879SMarkus Armbruster error_report("memory backend %s is used multiple times. Each " 527ac057879SMarkus Armbruster "-numa option must use a different memdev value.", 528ac057879SMarkus Armbruster path); 529ac057879SMarkus Armbruster g_free(path); 530ac057879SMarkus Armbruster exit(1); 531ac057879SMarkus Armbruster } 532ac057879SMarkus Armbruster 533ac057879SMarkus Armbruster host_memory_backend_set_mapped(backend, true); 534ac057879SMarkus Armbruster memory_region_add_subregion(mr, addr, seg); 535ac057879SMarkus Armbruster vmstate_register_ram_global(seg); 536ac057879SMarkus Armbruster addr += size; 537ac057879SMarkus Armbruster } 538ac057879SMarkus Armbruster } 539ac057879SMarkus Armbruster 540ac057879SMarkus Armbruster static void numa_stat_memory_devices(NumaNodeMem node_mem[]) 541ac057879SMarkus Armbruster { 542ac057879SMarkus Armbruster MemoryDeviceInfoList *info_list = qmp_memory_device_list(); 543ac057879SMarkus Armbruster MemoryDeviceInfoList *info; 544ac057879SMarkus Armbruster PCDIMMDeviceInfo *pcdimm_info; 545c35d17caSPeter Maydell VirtioPMEMDeviceInfo *vpi; 546ac057879SMarkus Armbruster 547ac057879SMarkus Armbruster for (info = info_list; info; info = info->next) { 548ac057879SMarkus Armbruster MemoryDeviceInfo *value = info->value; 549ac057879SMarkus Armbruster 550ac057879SMarkus Armbruster if (value) { 551ac057879SMarkus Armbruster switch (value->type) { 552ac057879SMarkus Armbruster case MEMORY_DEVICE_INFO_KIND_DIMM: 553ac057879SMarkus Armbruster case MEMORY_DEVICE_INFO_KIND_NVDIMM: 554c35d17caSPeter Maydell pcdimm_info = value->type == MEMORY_DEVICE_INFO_KIND_DIMM ? 555c35d17caSPeter Maydell value->u.dimm.data : value->u.nvdimm.data; 556ac057879SMarkus Armbruster node_mem[pcdimm_info->node].node_mem += pcdimm_info->size; 557ac057879SMarkus Armbruster node_mem[pcdimm_info->node].node_plugged_mem += 558ac057879SMarkus Armbruster pcdimm_info->size; 559c35d17caSPeter Maydell break; 560c35d17caSPeter Maydell case MEMORY_DEVICE_INFO_KIND_VIRTIO_PMEM: 561c35d17caSPeter Maydell vpi = value->u.virtio_pmem.data; 562c35d17caSPeter Maydell /* TODO: once we support numa, assign to right node */ 563c35d17caSPeter Maydell node_mem[0].node_mem += vpi->size; 564c35d17caSPeter Maydell node_mem[0].node_plugged_mem += vpi->size; 565c35d17caSPeter Maydell break; 566c35d17caSPeter Maydell default: 567c35d17caSPeter Maydell g_assert_not_reached(); 568ac057879SMarkus Armbruster } 569ac057879SMarkus Armbruster } 570ac057879SMarkus Armbruster } 571ac057879SMarkus Armbruster qapi_free_MemoryDeviceInfoList(info_list); 572ac057879SMarkus Armbruster } 573ac057879SMarkus Armbruster 574ac057879SMarkus Armbruster void query_numa_node_mem(NumaNodeMem node_mem[]) 575ac057879SMarkus Armbruster { 576ac057879SMarkus Armbruster int i; 577ac057879SMarkus Armbruster 578ac057879SMarkus Armbruster if (nb_numa_nodes <= 0) { 579ac057879SMarkus Armbruster return; 580ac057879SMarkus Armbruster } 581ac057879SMarkus Armbruster 582ac057879SMarkus Armbruster numa_stat_memory_devices(node_mem); 583ac057879SMarkus Armbruster for (i = 0; i < nb_numa_nodes; i++) { 584ac057879SMarkus Armbruster node_mem[i].node_mem += numa_info[i].node_mem; 585ac057879SMarkus Armbruster } 586ac057879SMarkus Armbruster } 587ac057879SMarkus Armbruster 588ac057879SMarkus Armbruster void ram_block_notifier_add(RAMBlockNotifier *n) 589ac057879SMarkus Armbruster { 590ac057879SMarkus Armbruster QLIST_INSERT_HEAD(&ram_list.ramblock_notifiers, n, next); 591ac057879SMarkus Armbruster } 592ac057879SMarkus Armbruster 593ac057879SMarkus Armbruster void ram_block_notifier_remove(RAMBlockNotifier *n) 594ac057879SMarkus Armbruster { 595ac057879SMarkus Armbruster QLIST_REMOVE(n, next); 596ac057879SMarkus Armbruster } 597ac057879SMarkus Armbruster 598ac057879SMarkus Armbruster void ram_block_notify_add(void *host, size_t size) 599ac057879SMarkus Armbruster { 600ac057879SMarkus Armbruster RAMBlockNotifier *notifier; 601ac057879SMarkus Armbruster 602ac057879SMarkus Armbruster QLIST_FOREACH(notifier, &ram_list.ramblock_notifiers, next) { 603ac057879SMarkus Armbruster notifier->ram_block_added(notifier, host, size); 604ac057879SMarkus Armbruster } 605ac057879SMarkus Armbruster } 606ac057879SMarkus Armbruster 607ac057879SMarkus Armbruster void ram_block_notify_remove(void *host, size_t size) 608ac057879SMarkus Armbruster { 609ac057879SMarkus Armbruster RAMBlockNotifier *notifier; 610ac057879SMarkus Armbruster 611ac057879SMarkus Armbruster QLIST_FOREACH(notifier, &ram_list.ramblock_notifiers, next) { 612ac057879SMarkus Armbruster notifier->ram_block_removed(notifier, host, size); 613ac057879SMarkus Armbruster } 614ac057879SMarkus Armbruster } 615