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