1 /* 2 * QEMU PowerPC pSeries Logical Partition NUMA associativity handling 3 * 4 * Copyright IBM Corp. 2020 5 * 6 * Authors: 7 * Daniel Henrique Barboza <danielhb413@gmail.com> 8 * 9 * This work is licensed under the terms of the GNU GPL, version 2 or later. 10 * See the COPYING file in the top-level directory. 11 */ 12 13 #include "qemu/osdep.h" 14 #include "qemu-common.h" 15 #include "hw/ppc/spapr_numa.h" 16 #include "hw/pci-host/spapr.h" 17 #include "hw/ppc/fdt.h" 18 19 /* Moved from hw/ppc/spapr_pci_nvlink2.c */ 20 #define SPAPR_GPU_NUMA_ID (cpu_to_be32(1)) 21 22 static bool spapr_numa_is_symmetrical(MachineState *ms) 23 { 24 int src, dst; 25 int nb_numa_nodes = ms->numa_state->num_nodes; 26 NodeInfo *numa_info = ms->numa_state->nodes; 27 28 for (src = 0; src < nb_numa_nodes; src++) { 29 for (dst = src; dst < nb_numa_nodes; dst++) { 30 if (numa_info[src].distance[dst] != 31 numa_info[dst].distance[src]) { 32 return false; 33 } 34 } 35 } 36 37 return true; 38 } 39 40 /* 41 * NVLink2-connected GPU RAM needs to be placed on a separate NUMA node. 42 * We assign a new numa ID per GPU in spapr_pci_collect_nvgpu() which is 43 * called from vPHB reset handler so we initialize the counter here. 44 * If no NUMA is configured from the QEMU side, we start from 1 as GPU RAM 45 * must be equally distant from any other node. 46 * The final value of spapr->gpu_numa_id is going to be written to 47 * max-associativity-domains in spapr_build_fdt(). 48 */ 49 unsigned int spapr_numa_initial_nvgpu_numa_id(MachineState *machine) 50 { 51 return MAX(1, machine->numa_state->num_nodes); 52 } 53 54 /* 55 * This function will translate the user distances into 56 * what the kernel understand as possible values: 10 57 * (local distance), 20, 40, 80 and 160, and return the equivalent 58 * NUMA level for each. Current heuristic is: 59 * - local distance (10) returns numa_level = 0x4, meaning there is 60 * no rounding for local distance 61 * - distances between 11 and 30 inclusive -> rounded to 20, 62 * numa_level = 0x3 63 * - distances between 31 and 60 inclusive -> rounded to 40, 64 * numa_level = 0x2 65 * - distances between 61 and 120 inclusive -> rounded to 80, 66 * numa_level = 0x1 67 * - everything above 120 returns numa_level = 0 to indicate that 68 * there is no match. This will be calculated as disntace = 160 69 * by the kernel (as of v5.9) 70 */ 71 static uint8_t spapr_numa_get_numa_level(uint8_t distance) 72 { 73 if (distance == 10) { 74 return 0x4; 75 } else if (distance > 11 && distance <= 30) { 76 return 0x3; 77 } else if (distance > 31 && distance <= 60) { 78 return 0x2; 79 } else if (distance > 61 && distance <= 120) { 80 return 0x1; 81 } 82 83 return 0; 84 } 85 86 static void spapr_numa_define_FORM1_domains(SpaprMachineState *spapr) 87 { 88 MachineState *ms = MACHINE(spapr); 89 NodeInfo *numa_info = ms->numa_state->nodes; 90 int nb_numa_nodes = ms->numa_state->num_nodes; 91 int src, dst, i, j; 92 93 /* 94 * Fill all associativity domains of non-zero NUMA nodes with 95 * node_id. This is required because the default value (0) is 96 * considered a match with associativity domains of node 0. 97 */ 98 for (i = 1; i < nb_numa_nodes; i++) { 99 for (j = 1; j < MAX_DISTANCE_REF_POINTS; j++) { 100 spapr->numa_assoc_array[i][j] = cpu_to_be32(i); 101 } 102 } 103 104 for (src = 0; src < nb_numa_nodes; src++) { 105 for (dst = src; dst < nb_numa_nodes; dst++) { 106 /* 107 * This is how the associativity domain between A and B 108 * is calculated: 109 * 110 * - get the distance D between them 111 * - get the correspondent NUMA level 'n_level' for D 112 * - all associativity arrays were initialized with their own 113 * numa_ids, and we're calculating the distance in node_id 114 * ascending order, starting from node id 0 (the first node 115 * retrieved by numa_state). This will have a cascade effect in 116 * the algorithm because the associativity domains that node 0 117 * defines will be carried over to other nodes, and node 1 118 * associativities will be carried over after taking node 0 119 * associativities into account, and so on. This happens because 120 * we'll assign assoc_src as the associativity domain of dst 121 * as well, for all NUMA levels beyond and including n_level. 122 * 123 * The PPC kernel expects the associativity domains of node 0 to 124 * be always 0, and this algorithm will grant that by default. 125 */ 126 uint8_t distance = numa_info[src].distance[dst]; 127 uint8_t n_level = spapr_numa_get_numa_level(distance); 128 uint32_t assoc_src; 129 130 /* 131 * n_level = 0 means that the distance is greater than our last 132 * rounded value (120). In this case there is no NUMA level match 133 * between src and dst and we can skip the remaining of the loop. 134 * 135 * The Linux kernel will assume that the distance between src and 136 * dst, in this case of no match, is 10 (local distance) doubled 137 * for each NUMA it didn't match. We have MAX_DISTANCE_REF_POINTS 138 * levels (4), so this gives us 10*2*2*2*2 = 160. 139 * 140 * This logic can be seen in the Linux kernel source code, as of 141 * v5.9, in arch/powerpc/mm/numa.c, function __node_distance(). 142 */ 143 if (n_level == 0) { 144 continue; 145 } 146 147 /* 148 * We must assign all assoc_src to dst, starting from n_level 149 * and going up to 0x1. 150 */ 151 for (i = n_level; i > 0; i--) { 152 assoc_src = spapr->numa_assoc_array[src][i]; 153 spapr->numa_assoc_array[dst][i] = assoc_src; 154 } 155 } 156 } 157 158 } 159 160 /* 161 * Set NUMA machine state data based on FORM1 affinity semantics. 162 */ 163 static void spapr_numa_FORM1_affinity_init(SpaprMachineState *spapr, 164 MachineState *machine) 165 { 166 SpaprMachineClass *smc = SPAPR_MACHINE_GET_CLASS(spapr); 167 int nb_numa_nodes = machine->numa_state->num_nodes; 168 int i, j, max_nodes_with_gpus; 169 170 /* 171 * For all associativity arrays: first position is the size, 172 * position MAX_DISTANCE_REF_POINTS is always the numa_id, 173 * represented by the index 'i'. 174 * 175 * This will break on sparse NUMA setups, when/if QEMU starts 176 * to support it, because there will be no more guarantee that 177 * 'i' will be a valid node_id set by the user. 178 */ 179 for (i = 0; i < nb_numa_nodes; i++) { 180 spapr->numa_assoc_array[i][0] = cpu_to_be32(MAX_DISTANCE_REF_POINTS); 181 spapr->numa_assoc_array[i][MAX_DISTANCE_REF_POINTS] = cpu_to_be32(i); 182 } 183 184 /* 185 * Initialize NVLink GPU associativity arrays. We know that 186 * the first GPU will take the first available NUMA id, and 187 * we'll have a maximum of NVGPU_MAX_NUM GPUs in the machine. 188 * At this point we're not sure if there are GPUs or not, but 189 * let's initialize the associativity arrays and allow NVLink 190 * GPUs to be handled like regular NUMA nodes later on. 191 */ 192 max_nodes_with_gpus = nb_numa_nodes + NVGPU_MAX_NUM; 193 194 for (i = nb_numa_nodes; i < max_nodes_with_gpus; i++) { 195 spapr->numa_assoc_array[i][0] = cpu_to_be32(MAX_DISTANCE_REF_POINTS); 196 197 for (j = 1; j < MAX_DISTANCE_REF_POINTS; j++) { 198 uint32_t gpu_assoc = smc->pre_5_1_assoc_refpoints ? 199 SPAPR_GPU_NUMA_ID : cpu_to_be32(i); 200 spapr->numa_assoc_array[i][j] = gpu_assoc; 201 } 202 203 spapr->numa_assoc_array[i][MAX_DISTANCE_REF_POINTS] = cpu_to_be32(i); 204 } 205 206 /* 207 * Guests pseries-5.1 and older uses zeroed associativity domains, 208 * i.e. no domain definition based on NUMA distance input. 209 * 210 * Same thing with guests that have only one NUMA node. 211 */ 212 if (smc->pre_5_2_numa_associativity || 213 machine->numa_state->num_nodes <= 1) { 214 return; 215 } 216 217 if (!spapr_numa_is_symmetrical(machine)) { 218 error_report("Asymmetrical NUMA topologies aren't supported " 219 "in the pSeries machine"); 220 exit(EXIT_FAILURE); 221 } 222 223 spapr_numa_define_FORM1_domains(spapr); 224 } 225 226 void spapr_numa_associativity_init(SpaprMachineState *spapr, 227 MachineState *machine) 228 { 229 spapr_numa_FORM1_affinity_init(spapr, machine); 230 } 231 232 void spapr_numa_write_associativity_dt(SpaprMachineState *spapr, void *fdt, 233 int offset, int nodeid) 234 { 235 _FDT((fdt_setprop(fdt, offset, "ibm,associativity", 236 spapr->numa_assoc_array[nodeid], 237 sizeof(spapr->numa_assoc_array[nodeid])))); 238 } 239 240 static uint32_t *spapr_numa_get_vcpu_assoc(SpaprMachineState *spapr, 241 PowerPCCPU *cpu) 242 { 243 uint32_t *vcpu_assoc = g_new(uint32_t, VCPU_ASSOC_SIZE); 244 int index = spapr_get_vcpu_id(cpu); 245 246 /* 247 * VCPUs have an extra 'cpu_id' value in ibm,associativity 248 * compared to other resources. Increment the size at index 249 * 0, put cpu_id last, then copy the remaining associativity 250 * domains. 251 */ 252 vcpu_assoc[0] = cpu_to_be32(MAX_DISTANCE_REF_POINTS + 1); 253 vcpu_assoc[VCPU_ASSOC_SIZE - 1] = cpu_to_be32(index); 254 memcpy(vcpu_assoc + 1, spapr->numa_assoc_array[cpu->node_id] + 1, 255 (VCPU_ASSOC_SIZE - 2) * sizeof(uint32_t)); 256 257 return vcpu_assoc; 258 } 259 260 int spapr_numa_fixup_cpu_dt(SpaprMachineState *spapr, void *fdt, 261 int offset, PowerPCCPU *cpu) 262 { 263 g_autofree uint32_t *vcpu_assoc = NULL; 264 265 vcpu_assoc = spapr_numa_get_vcpu_assoc(spapr, cpu); 266 267 /* Advertise NUMA via ibm,associativity */ 268 return fdt_setprop(fdt, offset, "ibm,associativity", vcpu_assoc, 269 VCPU_ASSOC_SIZE * sizeof(uint32_t)); 270 } 271 272 273 int spapr_numa_write_assoc_lookup_arrays(SpaprMachineState *spapr, void *fdt, 274 int offset) 275 { 276 MachineState *machine = MACHINE(spapr); 277 int nb_numa_nodes = machine->numa_state->num_nodes; 278 int nr_nodes = nb_numa_nodes ? nb_numa_nodes : 1; 279 uint32_t *int_buf, *cur_index, buf_len; 280 int ret, i; 281 282 /* ibm,associativity-lookup-arrays */ 283 buf_len = (nr_nodes * MAX_DISTANCE_REF_POINTS + 2) * sizeof(uint32_t); 284 cur_index = int_buf = g_malloc0(buf_len); 285 int_buf[0] = cpu_to_be32(nr_nodes); 286 /* Number of entries per associativity list */ 287 int_buf[1] = cpu_to_be32(MAX_DISTANCE_REF_POINTS); 288 cur_index += 2; 289 for (i = 0; i < nr_nodes; i++) { 290 /* 291 * For the lookup-array we use the ibm,associativity array, 292 * from numa_assoc_array. without the first element (size). 293 */ 294 uint32_t *associativity = spapr->numa_assoc_array[i]; 295 memcpy(cur_index, ++associativity, 296 sizeof(uint32_t) * MAX_DISTANCE_REF_POINTS); 297 cur_index += MAX_DISTANCE_REF_POINTS; 298 } 299 ret = fdt_setprop(fdt, offset, "ibm,associativity-lookup-arrays", int_buf, 300 (cur_index - int_buf) * sizeof(uint32_t)); 301 g_free(int_buf); 302 303 return ret; 304 } 305 306 static void spapr_numa_FORM1_write_rtas_dt(SpaprMachineState *spapr, 307 void *fdt, int rtas) 308 { 309 MachineState *ms = MACHINE(spapr); 310 SpaprMachineClass *smc = SPAPR_MACHINE_GET_CLASS(spapr); 311 uint32_t number_nvgpus_nodes = spapr->gpu_numa_id - 312 spapr_numa_initial_nvgpu_numa_id(ms); 313 uint32_t refpoints[] = { 314 cpu_to_be32(0x4), 315 cpu_to_be32(0x3), 316 cpu_to_be32(0x2), 317 cpu_to_be32(0x1), 318 }; 319 uint32_t nr_refpoints = ARRAY_SIZE(refpoints); 320 uint32_t maxdomain = ms->numa_state->num_nodes + number_nvgpus_nodes; 321 uint32_t maxdomains[] = { 322 cpu_to_be32(4), 323 cpu_to_be32(maxdomain), 324 cpu_to_be32(maxdomain), 325 cpu_to_be32(maxdomain), 326 cpu_to_be32(maxdomain) 327 }; 328 329 if (smc->pre_5_2_numa_associativity || 330 ms->numa_state->num_nodes <= 1) { 331 uint32_t legacy_refpoints[] = { 332 cpu_to_be32(0x4), 333 cpu_to_be32(0x4), 334 cpu_to_be32(0x2), 335 }; 336 uint32_t legacy_maxdomain = spapr->gpu_numa_id > 1 ? 1 : 0; 337 uint32_t legacy_maxdomains[] = { 338 cpu_to_be32(4), 339 cpu_to_be32(legacy_maxdomain), 340 cpu_to_be32(legacy_maxdomain), 341 cpu_to_be32(legacy_maxdomain), 342 cpu_to_be32(spapr->gpu_numa_id), 343 }; 344 345 G_STATIC_ASSERT(sizeof(legacy_refpoints) <= sizeof(refpoints)); 346 G_STATIC_ASSERT(sizeof(legacy_maxdomains) <= sizeof(maxdomains)); 347 348 nr_refpoints = 3; 349 350 memcpy(refpoints, legacy_refpoints, sizeof(legacy_refpoints)); 351 memcpy(maxdomains, legacy_maxdomains, sizeof(legacy_maxdomains)); 352 353 /* pseries-5.0 and older reference-points array is {0x4, 0x4} */ 354 if (smc->pre_5_1_assoc_refpoints) { 355 nr_refpoints = 2; 356 } 357 } 358 359 _FDT(fdt_setprop(fdt, rtas, "ibm,associativity-reference-points", 360 refpoints, nr_refpoints * sizeof(refpoints[0]))); 361 362 _FDT(fdt_setprop(fdt, rtas, "ibm,max-associativity-domains", 363 maxdomains, sizeof(maxdomains))); 364 } 365 366 /* 367 * Helper that writes ibm,associativity-reference-points and 368 * max-associativity-domains in the RTAS pointed by @rtas 369 * in the DT @fdt. 370 */ 371 void spapr_numa_write_rtas_dt(SpaprMachineState *spapr, void *fdt, int rtas) 372 { 373 spapr_numa_FORM1_write_rtas_dt(spapr, fdt, rtas); 374 } 375 376 static target_ulong h_home_node_associativity(PowerPCCPU *cpu, 377 SpaprMachineState *spapr, 378 target_ulong opcode, 379 target_ulong *args) 380 { 381 g_autofree uint32_t *vcpu_assoc = NULL; 382 target_ulong flags = args[0]; 383 target_ulong procno = args[1]; 384 PowerPCCPU *tcpu; 385 int idx, assoc_idx; 386 387 /* only support procno from H_REGISTER_VPA */ 388 if (flags != 0x1) { 389 return H_FUNCTION; 390 } 391 392 tcpu = spapr_find_cpu(procno); 393 if (tcpu == NULL) { 394 return H_P2; 395 } 396 397 /* 398 * Given that we want to be flexible with the sizes and indexes, 399 * we must consider that there is a hard limit of how many 400 * associativities domain we can fit in R4 up to R9, which would be 401 * 12 associativity domains for vcpus. Assert and bail if that's 402 * not the case. 403 */ 404 G_STATIC_ASSERT((VCPU_ASSOC_SIZE - 1) <= 12); 405 406 vcpu_assoc = spapr_numa_get_vcpu_assoc(spapr, tcpu); 407 /* assoc_idx starts at 1 to skip associativity size */ 408 assoc_idx = 1; 409 410 #define ASSOCIATIVITY(a, b) (((uint64_t)(a) << 32) | \ 411 ((uint64_t)(b) & 0xffffffff)) 412 413 for (idx = 0; idx < 6; idx++) { 414 int32_t a, b; 415 416 /* 417 * vcpu_assoc[] will contain the associativity domains for tcpu, 418 * including tcpu->node_id and procno, meaning that we don't 419 * need to use these variables here. 420 * 421 * We'll read 2 values at a time to fill up the ASSOCIATIVITY() 422 * macro. The ternary will fill the remaining registers with -1 423 * after we went through vcpu_assoc[]. 424 */ 425 a = assoc_idx < VCPU_ASSOC_SIZE ? 426 be32_to_cpu(vcpu_assoc[assoc_idx++]) : -1; 427 b = assoc_idx < VCPU_ASSOC_SIZE ? 428 be32_to_cpu(vcpu_assoc[assoc_idx++]) : -1; 429 430 args[idx] = ASSOCIATIVITY(a, b); 431 } 432 #undef ASSOCIATIVITY 433 434 return H_SUCCESS; 435 } 436 437 static void spapr_numa_register_types(void) 438 { 439 /* Virtual Processor Home Node */ 440 spapr_register_hypercall(H_HOME_NODE_ASSOCIATIVITY, 441 h_home_node_associativity); 442 } 443 444 type_init(spapr_numa_register_types) 445