Wanlong Gao | 96d0e26 | 2014-05-14 17:43:05 +0800 | [diff] [blame] | 1 | /* |
| 2 | * NUMA parameter parsing routines |
| 3 | * |
| 4 | * Copyright (c) 2014 Fujitsu Ltd. |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to deal |
| 8 | * in the Software without restriction, including without limitation the rights |
| 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | * copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in |
| 14 | * all copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | * THE SOFTWARE. |
| 23 | */ |
| 24 | |
| 25 | #include "sysemu/sysemu.h" |
| 26 | #include "exec/cpu-common.h" |
| 27 | #include "qemu/bitmap.h" |
| 28 | #include "qom/cpu.h" |
Wanlong Gao | 2b631ec | 2014-05-14 17:43:06 +0800 | [diff] [blame] | 29 | #include "qemu/error-report.h" |
| 30 | #include "include/exec/cpu-common.h" /* for RAM_ADDR_FMT */ |
Wanlong Gao | 0042109 | 2014-05-14 17:43:08 +0800 | [diff] [blame] | 31 | #include "qapi-visit.h" |
| 32 | #include "qapi/opts-visitor.h" |
| 33 | #include "qapi/dealloc-visitor.h" |
| 34 | #include "qapi/qmp/qerror.h" |
Paolo Bonzini | dfabb8b | 2014-05-14 17:43:15 +0800 | [diff] [blame^] | 35 | #include "hw/boards.h" |
Wanlong Gao | 96d0e26 | 2014-05-14 17:43:05 +0800 | [diff] [blame] | 36 | |
Wanlong Gao | 0042109 | 2014-05-14 17:43:08 +0800 | [diff] [blame] | 37 | QemuOptsList qemu_numa_opts = { |
| 38 | .name = "numa", |
| 39 | .implied_opt_name = "type", |
| 40 | .head = QTAILQ_HEAD_INITIALIZER(qemu_numa_opts.head), |
| 41 | .desc = { { 0 } } /* validated with OptsVisitor */ |
| 42 | }; |
| 43 | |
| 44 | static void numa_node_parse(NumaNodeOptions *node, QemuOpts *opts, Error **errp) |
Wanlong Gao | 96d0e26 | 2014-05-14 17:43:05 +0800 | [diff] [blame] | 45 | { |
Wanlong Gao | 0042109 | 2014-05-14 17:43:08 +0800 | [diff] [blame] | 46 | uint16_t nodenr; |
| 47 | uint16List *cpus = NULL; |
Wanlong Gao | 96d0e26 | 2014-05-14 17:43:05 +0800 | [diff] [blame] | 48 | |
Wanlong Gao | 0042109 | 2014-05-14 17:43:08 +0800 | [diff] [blame] | 49 | if (node->has_nodeid) { |
| 50 | nodenr = node->nodeid; |
| 51 | } else { |
| 52 | nodenr = nb_numa_nodes; |
| 53 | } |
| 54 | |
| 55 | if (nodenr >= MAX_NODES) { |
| 56 | error_setg(errp, "Max number of NUMA nodes reached: %" |
| 57 | PRIu16 "\n", nodenr); |
Wanlong Gao | 96d0e26 | 2014-05-14 17:43:05 +0800 | [diff] [blame] | 58 | return; |
| 59 | } |
| 60 | |
Wanlong Gao | 0042109 | 2014-05-14 17:43:08 +0800 | [diff] [blame] | 61 | for (cpus = node->cpus; cpus; cpus = cpus->next) { |
| 62 | if (cpus->value > MAX_CPUMASK_BITS) { |
| 63 | error_setg(errp, "CPU number %" PRIu16 " is bigger than %d", |
| 64 | cpus->value, MAX_CPUMASK_BITS); |
| 65 | return; |
Wanlong Gao | 96d0e26 | 2014-05-14 17:43:05 +0800 | [diff] [blame] | 66 | } |
Wanlong Gao | 0042109 | 2014-05-14 17:43:08 +0800 | [diff] [blame] | 67 | bitmap_set(numa_info[nodenr].node_cpu, cpus->value, 1); |
Wanlong Gao | 96d0e26 | 2014-05-14 17:43:05 +0800 | [diff] [blame] | 68 | } |
| 69 | |
Wanlong Gao | 0042109 | 2014-05-14 17:43:08 +0800 | [diff] [blame] | 70 | if (node->has_mem) { |
| 71 | uint64_t mem_size = node->mem; |
| 72 | const char *mem_str = qemu_opt_get(opts, "mem"); |
| 73 | /* Fix up legacy suffix-less format */ |
| 74 | if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) { |
| 75 | mem_size <<= 20; |
| 76 | } |
| 77 | numa_info[nodenr].node_mem = mem_size; |
Wanlong Gao | 96d0e26 | 2014-05-14 17:43:05 +0800 | [diff] [blame] | 78 | } |
Wanlong Gao | 96d0e26 | 2014-05-14 17:43:05 +0800 | [diff] [blame] | 79 | } |
| 80 | |
Wanlong Gao | 0042109 | 2014-05-14 17:43:08 +0800 | [diff] [blame] | 81 | int numa_init_func(QemuOpts *opts, void *opaque) |
Wanlong Gao | 96d0e26 | 2014-05-14 17:43:05 +0800 | [diff] [blame] | 82 | { |
Wanlong Gao | 0042109 | 2014-05-14 17:43:08 +0800 | [diff] [blame] | 83 | NumaOptions *object = NULL; |
| 84 | Error *err = NULL; |
Wanlong Gao | 96d0e26 | 2014-05-14 17:43:05 +0800 | [diff] [blame] | 85 | |
Wanlong Gao | 0042109 | 2014-05-14 17:43:08 +0800 | [diff] [blame] | 86 | { |
| 87 | OptsVisitor *ov = opts_visitor_new(opts); |
| 88 | visit_type_NumaOptions(opts_get_visitor(ov), &object, NULL, &err); |
| 89 | opts_visitor_cleanup(ov); |
Wanlong Gao | 96d0e26 | 2014-05-14 17:43:05 +0800 | [diff] [blame] | 90 | } |
Wanlong Gao | 96d0e26 | 2014-05-14 17:43:05 +0800 | [diff] [blame] | 91 | |
Wanlong Gao | 0042109 | 2014-05-14 17:43:08 +0800 | [diff] [blame] | 92 | if (err) { |
| 93 | goto error; |
| 94 | } |
Wanlong Gao | 96d0e26 | 2014-05-14 17:43:05 +0800 | [diff] [blame] | 95 | |
Wanlong Gao | 0042109 | 2014-05-14 17:43:08 +0800 | [diff] [blame] | 96 | switch (object->kind) { |
| 97 | case NUMA_OPTIONS_KIND_NODE: |
| 98 | numa_node_parse(object->node, opts, &err); |
| 99 | if (err) { |
| 100 | goto error; |
Wanlong Gao | 96d0e26 | 2014-05-14 17:43:05 +0800 | [diff] [blame] | 101 | } |
| 102 | nb_numa_nodes++; |
Wanlong Gao | 0042109 | 2014-05-14 17:43:08 +0800 | [diff] [blame] | 103 | break; |
| 104 | default: |
| 105 | abort(); |
Wanlong Gao | 96d0e26 | 2014-05-14 17:43:05 +0800 | [diff] [blame] | 106 | } |
Wanlong Gao | 0042109 | 2014-05-14 17:43:08 +0800 | [diff] [blame] | 107 | |
| 108 | return 0; |
| 109 | |
| 110 | error: |
| 111 | qerror_report_err(err); |
| 112 | error_free(err); |
| 113 | |
| 114 | if (object) { |
| 115 | QapiDeallocVisitor *dv = qapi_dealloc_visitor_new(); |
| 116 | visit_type_NumaOptions(qapi_dealloc_get_visitor(dv), |
| 117 | &object, NULL, NULL); |
| 118 | qapi_dealloc_visitor_cleanup(dv); |
| 119 | } |
| 120 | |
| 121 | return -1; |
Wanlong Gao | 96d0e26 | 2014-05-14 17:43:05 +0800 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | void set_numa_nodes(void) |
| 125 | { |
| 126 | if (nb_numa_nodes > 0) { |
Wanlong Gao | 2b631ec | 2014-05-14 17:43:06 +0800 | [diff] [blame] | 127 | uint64_t numa_total; |
Wanlong Gao | 96d0e26 | 2014-05-14 17:43:05 +0800 | [diff] [blame] | 128 | int i; |
| 129 | |
| 130 | if (nb_numa_nodes > MAX_NODES) { |
| 131 | nb_numa_nodes = MAX_NODES; |
| 132 | } |
| 133 | |
| 134 | /* If no memory size if given for any node, assume the default case |
| 135 | * and distribute the available memory equally across all nodes |
| 136 | */ |
| 137 | for (i = 0; i < nb_numa_nodes; i++) { |
Wanlong Gao | 8c85901 | 2014-05-14 17:43:07 +0800 | [diff] [blame] | 138 | if (numa_info[i].node_mem != 0) { |
Wanlong Gao | 96d0e26 | 2014-05-14 17:43:05 +0800 | [diff] [blame] | 139 | break; |
| 140 | } |
| 141 | } |
| 142 | if (i == nb_numa_nodes) { |
| 143 | uint64_t usedmem = 0; |
| 144 | |
| 145 | /* On Linux, the each node's border has to be 8MB aligned, |
| 146 | * the final node gets the rest. |
| 147 | */ |
| 148 | for (i = 0; i < nb_numa_nodes - 1; i++) { |
Wanlong Gao | 8c85901 | 2014-05-14 17:43:07 +0800 | [diff] [blame] | 149 | numa_info[i].node_mem = (ram_size / nb_numa_nodes) & |
| 150 | ~((1 << 23UL) - 1); |
| 151 | usedmem += numa_info[i].node_mem; |
Wanlong Gao | 96d0e26 | 2014-05-14 17:43:05 +0800 | [diff] [blame] | 152 | } |
Wanlong Gao | 8c85901 | 2014-05-14 17:43:07 +0800 | [diff] [blame] | 153 | numa_info[i].node_mem = ram_size - usedmem; |
Wanlong Gao | 96d0e26 | 2014-05-14 17:43:05 +0800 | [diff] [blame] | 154 | } |
| 155 | |
Wanlong Gao | 2b631ec | 2014-05-14 17:43:06 +0800 | [diff] [blame] | 156 | numa_total = 0; |
| 157 | for (i = 0; i < nb_numa_nodes; i++) { |
Wanlong Gao | 8c85901 | 2014-05-14 17:43:07 +0800 | [diff] [blame] | 158 | numa_total += numa_info[i].node_mem; |
Wanlong Gao | 2b631ec | 2014-05-14 17:43:06 +0800 | [diff] [blame] | 159 | } |
| 160 | if (numa_total != ram_size) { |
| 161 | error_report("total memory for NUMA nodes (%" PRIu64 ")" |
| 162 | " should equal RAM size (" RAM_ADDR_FMT ")", |
| 163 | numa_total, ram_size); |
| 164 | exit(1); |
| 165 | } |
| 166 | |
Wanlong Gao | 96d0e26 | 2014-05-14 17:43:05 +0800 | [diff] [blame] | 167 | for (i = 0; i < nb_numa_nodes; i++) { |
Wanlong Gao | 8c85901 | 2014-05-14 17:43:07 +0800 | [diff] [blame] | 168 | if (!bitmap_empty(numa_info[i].node_cpu, MAX_CPUMASK_BITS)) { |
Wanlong Gao | 96d0e26 | 2014-05-14 17:43:05 +0800 | [diff] [blame] | 169 | break; |
| 170 | } |
| 171 | } |
| 172 | /* assigning the VCPUs round-robin is easier to implement, guest OSes |
| 173 | * must cope with this anyway, because there are BIOSes out there in |
| 174 | * real machines which also use this scheme. |
| 175 | */ |
| 176 | if (i == nb_numa_nodes) { |
| 177 | for (i = 0; i < max_cpus; i++) { |
Wanlong Gao | 8c85901 | 2014-05-14 17:43:07 +0800 | [diff] [blame] | 178 | set_bit(i, numa_info[i % nb_numa_nodes].node_cpu); |
Wanlong Gao | 96d0e26 | 2014-05-14 17:43:05 +0800 | [diff] [blame] | 179 | } |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | void set_numa_modes(void) |
| 185 | { |
| 186 | CPUState *cpu; |
| 187 | int i; |
| 188 | |
| 189 | CPU_FOREACH(cpu) { |
| 190 | for (i = 0; i < nb_numa_nodes; i++) { |
Wanlong Gao | 8c85901 | 2014-05-14 17:43:07 +0800 | [diff] [blame] | 191 | if (test_bit(cpu->cpu_index, numa_info[i].node_cpu)) { |
Wanlong Gao | 96d0e26 | 2014-05-14 17:43:05 +0800 | [diff] [blame] | 192 | cpu->numa_node = i; |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | } |
Paolo Bonzini | dfabb8b | 2014-05-14 17:43:15 +0800 | [diff] [blame^] | 197 | |
| 198 | void memory_region_allocate_system_memory(MemoryRegion *mr, Object *owner, |
| 199 | const char *name, |
| 200 | uint64_t ram_size) |
| 201 | { |
| 202 | memory_region_init_ram(mr, owner, name, ram_size); |
| 203 | vmstate_register_ram_global(mr); |
| 204 | } |