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