| 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 "hw/ppc/spapr_numa.h" |
| 15 | #include "hw/pci-host/spapr.h" |
| 16 | #include "hw/ppc/fdt.h" |
| 17 | |
| 18 | /* Moved from hw/ppc/spapr_pci_nvlink2.c */ |
| 19 | #define SPAPR_GPU_NUMA_ID (cpu_to_be32(1)) |
| 20 | |
| 21 | /* |
| 22 | * Retrieves max_dist_ref_points of the current NUMA affinity. |
| 23 | */ |
| 24 | static int get_max_dist_ref_points(SpaprMachineState *spapr) |
| 25 | { |
| 26 | if (spapr_ovec_test(spapr->ov5_cas, OV5_FORM2_AFFINITY)) { |
| 27 | return FORM2_DIST_REF_POINTS; |
| 28 | } |
| 29 | |
| 30 | return FORM1_DIST_REF_POINTS; |
| 31 | } |
| 32 | |
| 33 | /* |
| 34 | * Retrieves numa_assoc_size of the current NUMA affinity. |
| 35 | */ |
| 36 | static int get_numa_assoc_size(SpaprMachineState *spapr) |
| 37 | { |
| 38 | if (spapr_ovec_test(spapr->ov5_cas, OV5_FORM2_AFFINITY)) { |
| 39 | return FORM2_NUMA_ASSOC_SIZE; |
| 40 | } |
| 41 | |
| 42 | return FORM1_NUMA_ASSOC_SIZE; |
| 43 | } |
| 44 | |
| 45 | /* |
| 46 | * Retrieves vcpu_assoc_size of the current NUMA affinity. |
| 47 | * |
| 48 | * vcpu_assoc_size is the size of ibm,associativity array |
| 49 | * for CPUs, which has an extra element (vcpu_id) in the end. |
| 50 | */ |
| 51 | static int get_vcpu_assoc_size(SpaprMachineState *spapr) |
| 52 | { |
| 53 | return get_numa_assoc_size(spapr) + 1; |
| 54 | } |
| 55 | |
| 56 | /* |
| 57 | * Retrieves the ibm,associativity array of NUMA node 'node_id' |
| 58 | * for the current NUMA affinity. |
| 59 | */ |
| 60 | static const uint32_t *get_associativity(SpaprMachineState *spapr, int node_id) |
| 61 | { |
| 62 | if (spapr_ovec_test(spapr->ov5_cas, OV5_FORM2_AFFINITY)) { |
| 63 | return spapr->FORM2_assoc_array[node_id]; |
| 64 | } |
| 65 | return spapr->FORM1_assoc_array[node_id]; |
| 66 | } |
| 67 | |
| 68 | /* |
| 69 | * Wrapper that returns node distance from ms->numa_state->nodes |
| 70 | * after handling edge cases where the distance might be absent. |
| 71 | */ |
| 72 | static int get_numa_distance(MachineState *ms, int src, int dst) |
| 73 | { |
| 74 | NodeInfo *numa_info = ms->numa_state->nodes; |
| 75 | int ret = numa_info[src].distance[dst]; |
| 76 | |
| 77 | if (ret != 0) { |
| 78 | return ret; |
| 79 | } |
| 80 | |
| 81 | /* |
| 82 | * In case QEMU adds a default NUMA single node when the user |
| 83 | * did not add any, or where the user did not supply distances, |
| 84 | * the distance will be absent (zero). Return local/remote |
| 85 | * distance in this case. |
| 86 | */ |
| 87 | if (src == dst) { |
| 88 | return NUMA_DISTANCE_MIN; |
| 89 | } |
| 90 | |
| 91 | return NUMA_DISTANCE_DEFAULT; |
| 92 | } |
| 93 | |
| 94 | static bool spapr_numa_is_symmetrical(MachineState *ms) |
| 95 | { |
| 96 | int nb_numa_nodes = ms->numa_state->num_nodes; |
| 97 | int src, dst; |
| 98 | |
| 99 | for (src = 0; src < nb_numa_nodes; src++) { |
| 100 | for (dst = src; dst < nb_numa_nodes; dst++) { |
| 101 | if (get_numa_distance(ms, src, dst) != |
| 102 | get_numa_distance(ms, dst, src)) { |
| 103 | return false; |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | return true; |
| 109 | } |
| 110 | |
| 111 | /* |
| 112 | * This function will translate the user distances into |
| 113 | * what the kernel understand as possible values: 10 |
| 114 | * (local distance), 20, 40, 80 and 160, and return the equivalent |
| 115 | * NUMA level for each. Current heuristic is: |
| 116 | * - local distance (10) returns numa_level = 0x4, meaning there is |
| 117 | * no rounding for local distance |
| 118 | * - distances between 11 and 30 inclusive -> rounded to 20, |
| 119 | * numa_level = 0x3 |
| 120 | * - distances between 31 and 60 inclusive -> rounded to 40, |
| 121 | * numa_level = 0x2 |
| 122 | * - distances between 61 and 120 inclusive -> rounded to 80, |
| 123 | * numa_level = 0x1 |
| 124 | * - everything above 120 returns numa_level = 0 to indicate that |
| 125 | * there is no match. This will be calculated as disntace = 160 |
| 126 | * by the kernel (as of v5.9) |
| 127 | */ |
| 128 | static uint8_t spapr_numa_get_numa_level(uint8_t distance) |
| 129 | { |
| 130 | if (distance == 10) { |
| 131 | return 0x4; |
| 132 | } else if (distance > 11 && distance <= 30) { |
| 133 | return 0x3; |
| 134 | } else if (distance > 31 && distance <= 60) { |
| 135 | return 0x2; |
| 136 | } else if (distance > 61 && distance <= 120) { |
| 137 | return 0x1; |
| 138 | } |
| 139 | |
| 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | static void spapr_numa_define_FORM1_domains(SpaprMachineState *spapr) |
| 144 | { |
| 145 | MachineState *ms = MACHINE(spapr); |
| 146 | int nb_numa_nodes = ms->numa_state->num_nodes; |
| 147 | int src, dst, i, j; |
| 148 | |
| 149 | /* |
| 150 | * Fill all associativity domains of non-zero NUMA nodes with |
| 151 | * node_id. This is required because the default value (0) is |
| 152 | * considered a match with associativity domains of node 0. |
| 153 | */ |
| 154 | for (i = 1; i < nb_numa_nodes; i++) { |
| 155 | for (j = 1; j < FORM1_DIST_REF_POINTS; j++) { |
| 156 | spapr->FORM1_assoc_array[i][j] = cpu_to_be32(i); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | for (src = 0; src < nb_numa_nodes; src++) { |
| 161 | for (dst = src; dst < nb_numa_nodes; dst++) { |
| 162 | /* |
| 163 | * This is how the associativity domain between A and B |
| 164 | * is calculated: |
| 165 | * |
| 166 | * - get the distance D between them |
| 167 | * - get the correspondent NUMA level 'n_level' for D |
| 168 | * - all associativity arrays were initialized with their own |
| 169 | * numa_ids, and we're calculating the distance in node_id |
| 170 | * ascending order, starting from node id 0 (the first node |
| 171 | * retrieved by numa_state). This will have a cascade effect in |
| 172 | * the algorithm because the associativity domains that node 0 |
| 173 | * defines will be carried over to other nodes, and node 1 |
| 174 | * associativities will be carried over after taking node 0 |
| 175 | * associativities into account, and so on. This happens because |
| 176 | * we'll assign assoc_src as the associativity domain of dst |
| 177 | * as well, for all NUMA levels beyond and including n_level. |
| 178 | * |
| 179 | * The PPC kernel expects the associativity domains of node 0 to |
| 180 | * be always 0, and this algorithm will grant that by default. |
| 181 | */ |
| 182 | uint8_t distance = get_numa_distance(ms, src, dst); |
| 183 | uint8_t n_level = spapr_numa_get_numa_level(distance); |
| 184 | uint32_t assoc_src; |
| 185 | |
| 186 | /* |
| 187 | * n_level = 0 means that the distance is greater than our last |
| 188 | * rounded value (120). In this case there is no NUMA level match |
| 189 | * between src and dst and we can skip the remaining of the loop. |
| 190 | * |
| 191 | * The Linux kernel will assume that the distance between src and |
| 192 | * dst, in this case of no match, is 10 (local distance) doubled |
| 193 | * for each NUMA it didn't match. We have FORM1_DIST_REF_POINTS |
| 194 | * levels (4), so this gives us 10*2*2*2*2 = 160. |
| 195 | * |
| 196 | * This logic can be seen in the Linux kernel source code, as of |
| 197 | * v5.9, in arch/powerpc/mm/numa.c, function __node_distance(). |
| 198 | */ |
| 199 | if (n_level == 0) { |
| 200 | continue; |
| 201 | } |
| 202 | |
| 203 | /* |
| 204 | * We must assign all assoc_src to dst, starting from n_level |
| 205 | * and going up to 0x1. |
| 206 | */ |
| 207 | for (i = n_level; i > 0; i--) { |
| 208 | assoc_src = spapr->FORM1_assoc_array[src][i]; |
| 209 | spapr->FORM1_assoc_array[dst][i] = assoc_src; |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | } |
| 215 | |
| 216 | static void spapr_numa_FORM1_affinity_check(MachineState *machine) |
| 217 | { |
| 218 | int i; |
| 219 | |
| 220 | /* |
| 221 | * Check we don't have a memory-less/cpu-less NUMA node |
| 222 | * Firmware relies on the existing memory/cpu topology to provide the |
| 223 | * NUMA topology to the kernel. |
| 224 | * And the linux kernel needs to know the NUMA topology at start |
| 225 | * to be able to hotplug CPUs later. |
| 226 | */ |
| 227 | if (machine->numa_state->num_nodes) { |
| 228 | for (i = 0; i < machine->numa_state->num_nodes; ++i) { |
| 229 | /* check for memory-less node */ |
| 230 | if (machine->numa_state->nodes[i].node_mem == 0) { |
| 231 | CPUState *cs; |
| 232 | int found = 0; |
| 233 | /* check for cpu-less node */ |
| 234 | CPU_FOREACH(cs) { |
| 235 | PowerPCCPU *cpu = POWERPC_CPU(cs); |
| 236 | if (cpu->node_id == i) { |
| 237 | found = 1; |
| 238 | break; |
| 239 | } |
| 240 | } |
| 241 | /* memory-less and cpu-less node */ |
| 242 | if (!found) { |
| 243 | error_report( |
| 244 | "Memory-less/cpu-less nodes are not supported with FORM1 NUMA (node %d)", i); |
| 245 | exit(EXIT_FAILURE); |
| 246 | } |
| 247 | } |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | if (!spapr_numa_is_symmetrical(machine)) { |
| 252 | error_report( |
| 253 | "Asymmetrical NUMA topologies aren't supported in the pSeries machine using FORM1 NUMA"); |
| 254 | exit(EXIT_FAILURE); |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | /* |
| 259 | * Set NUMA machine state data based on FORM1 affinity semantics. |
| 260 | */ |
| 261 | static void spapr_numa_FORM1_affinity_init(SpaprMachineState *spapr, |
| 262 | MachineState *machine) |
| 263 | { |
| 264 | SpaprMachineClass *smc = SPAPR_MACHINE_GET_CLASS(spapr); |
| 265 | int nb_numa_nodes = machine->numa_state->num_nodes; |
| 266 | int i, j; |
| 267 | |
| 268 | /* |
| 269 | * For all associativity arrays: first position is the size, |
| 270 | * position FORM1_DIST_REF_POINTS is always the numa_id, |
| 271 | * represented by the index 'i'. |
| 272 | * |
| 273 | * This will break on sparse NUMA setups, when/if QEMU starts |
| 274 | * to support it, because there will be no more guarantee that |
| 275 | * 'i' will be a valid node_id set by the user. |
| 276 | */ |
| 277 | for (i = 0; i < nb_numa_nodes; i++) { |
| 278 | spapr->FORM1_assoc_array[i][0] = cpu_to_be32(FORM1_DIST_REF_POINTS); |
| 279 | spapr->FORM1_assoc_array[i][FORM1_DIST_REF_POINTS] = cpu_to_be32(i); |
| 280 | } |
| 281 | |
| 282 | for (i = nb_numa_nodes; i < nb_numa_nodes; i++) { |
| 283 | spapr->FORM1_assoc_array[i][0] = cpu_to_be32(FORM1_DIST_REF_POINTS); |
| 284 | |
| 285 | for (j = 1; j < FORM1_DIST_REF_POINTS; j++) { |
| 286 | uint32_t gpu_assoc = smc->pre_5_1_assoc_refpoints ? |
| 287 | SPAPR_GPU_NUMA_ID : cpu_to_be32(i); |
| 288 | spapr->FORM1_assoc_array[i][j] = gpu_assoc; |
| 289 | } |
| 290 | |
| 291 | spapr->FORM1_assoc_array[i][FORM1_DIST_REF_POINTS] = cpu_to_be32(i); |
| 292 | } |
| 293 | |
| 294 | /* |
| 295 | * Guests pseries-5.1 and older uses zeroed associativity domains, |
| 296 | * i.e. no domain definition based on NUMA distance input. |
| 297 | * |
| 298 | * Same thing with guests that have only one NUMA node. |
| 299 | */ |
| 300 | if (smc->pre_5_2_numa_associativity || |
| 301 | machine->numa_state->num_nodes <= 1) { |
| 302 | return; |
| 303 | } |
| 304 | |
| 305 | spapr_numa_define_FORM1_domains(spapr); |
| 306 | } |
| 307 | |
| 308 | /* |
| 309 | * Init NUMA FORM2 machine state data |
| 310 | */ |
| 311 | static void spapr_numa_FORM2_affinity_init(SpaprMachineState *spapr) |
| 312 | { |
| 313 | int i; |
| 314 | |
| 315 | /* |
| 316 | * For all resources but CPUs, FORM2 associativity arrays will |
| 317 | * be a size 2 array with the following format: |
| 318 | * |
| 319 | * ibm,associativity = {1, numa_id} |
| 320 | * |
| 321 | * CPUs will write an additional 'vcpu_id' on top of the arrays |
| 322 | * being initialized here. 'numa_id' is represented by the |
| 323 | * index 'i' of the loop. |
| 324 | */ |
| 325 | for (i = 0; i < NUMA_NODES_MAX_NUM; i++) { |
| 326 | spapr->FORM2_assoc_array[i][0] = cpu_to_be32(1); |
| 327 | spapr->FORM2_assoc_array[i][1] = cpu_to_be32(i); |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | void spapr_numa_associativity_init(SpaprMachineState *spapr, |
| 332 | MachineState *machine) |
| 333 | { |
| 334 | spapr_numa_FORM1_affinity_init(spapr, machine); |
| 335 | spapr_numa_FORM2_affinity_init(spapr); |
| 336 | } |
| 337 | |
| 338 | void spapr_numa_associativity_check(SpaprMachineState *spapr) |
| 339 | { |
| 340 | /* |
| 341 | * FORM2 does not have any restrictions we need to handle |
| 342 | * at CAS time, for now. |
| 343 | */ |
| 344 | if (spapr_ovec_test(spapr->ov5_cas, OV5_FORM2_AFFINITY)) { |
| 345 | return; |
| 346 | } |
| 347 | |
| 348 | spapr_numa_FORM1_affinity_check(MACHINE(spapr)); |
| 349 | } |
| 350 | |
| 351 | void spapr_numa_write_associativity_dt(SpaprMachineState *spapr, void *fdt, |
| 352 | int offset, int nodeid) |
| 353 | { |
| 354 | const uint32_t *associativity = get_associativity(spapr, nodeid); |
| 355 | |
| 356 | _FDT((fdt_setprop(fdt, offset, "ibm,associativity", |
| 357 | associativity, |
| 358 | get_numa_assoc_size(spapr) * sizeof(uint32_t)))); |
| 359 | } |
| 360 | |
| 361 | static uint32_t *spapr_numa_get_vcpu_assoc(SpaprMachineState *spapr, |
| 362 | PowerPCCPU *cpu) |
| 363 | { |
| 364 | const uint32_t *associativity = get_associativity(spapr, cpu->node_id); |
| 365 | int max_distance_ref_points = get_max_dist_ref_points(spapr); |
| 366 | int vcpu_assoc_size = get_vcpu_assoc_size(spapr); |
| 367 | uint32_t *vcpu_assoc = g_new(uint32_t, vcpu_assoc_size); |
| 368 | int index = spapr_get_vcpu_id(cpu); |
| 369 | |
| 370 | /* |
| 371 | * VCPUs have an extra 'cpu_id' value in ibm,associativity |
| 372 | * compared to other resources. Increment the size at index |
| 373 | * 0, put cpu_id last, then copy the remaining associativity |
| 374 | * domains. |
| 375 | */ |
| 376 | vcpu_assoc[0] = cpu_to_be32(max_distance_ref_points + 1); |
| 377 | vcpu_assoc[vcpu_assoc_size - 1] = cpu_to_be32(index); |
| 378 | memcpy(vcpu_assoc + 1, associativity + 1, |
| 379 | (vcpu_assoc_size - 2) * sizeof(uint32_t)); |
| 380 | |
| 381 | return vcpu_assoc; |
| 382 | } |
| 383 | |
| 384 | int spapr_numa_fixup_cpu_dt(SpaprMachineState *spapr, void *fdt, |
| 385 | int offset, PowerPCCPU *cpu) |
| 386 | { |
| 387 | g_autofree uint32_t *vcpu_assoc = NULL; |
| 388 | int vcpu_assoc_size = get_vcpu_assoc_size(spapr); |
| 389 | |
| 390 | vcpu_assoc = spapr_numa_get_vcpu_assoc(spapr, cpu); |
| 391 | |
| 392 | /* Advertise NUMA via ibm,associativity */ |
| 393 | return fdt_setprop(fdt, offset, "ibm,associativity", vcpu_assoc, |
| 394 | vcpu_assoc_size * sizeof(uint32_t)); |
| 395 | } |
| 396 | |
| 397 | |
| 398 | int spapr_numa_write_assoc_lookup_arrays(SpaprMachineState *spapr, void *fdt, |
| 399 | int offset) |
| 400 | { |
| 401 | MachineState *machine = MACHINE(spapr); |
| 402 | int max_distance_ref_points = get_max_dist_ref_points(spapr); |
| 403 | int nb_numa_nodes = machine->numa_state->num_nodes; |
| 404 | int nr_nodes = nb_numa_nodes ? nb_numa_nodes : 1; |
| 405 | g_autofree uint32_t *int_buf = NULL; |
| 406 | uint32_t *cur_index; |
| 407 | int i; |
| 408 | |
| 409 | /* ibm,associativity-lookup-arrays */ |
| 410 | int_buf = g_new0(uint32_t, nr_nodes * max_distance_ref_points + 2); |
| 411 | cur_index = int_buf; |
| 412 | int_buf[0] = cpu_to_be32(nr_nodes); |
| 413 | /* Number of entries per associativity list */ |
| 414 | int_buf[1] = cpu_to_be32(max_distance_ref_points); |
| 415 | cur_index += 2; |
| 416 | for (i = 0; i < nr_nodes; i++) { |
| 417 | /* |
| 418 | * For the lookup-array we use the ibm,associativity array of the |
| 419 | * current NUMA affinity, without the first element (size). |
| 420 | */ |
| 421 | const uint32_t *associativity = get_associativity(spapr, i); |
| 422 | memcpy(cur_index, ++associativity, |
| 423 | sizeof(uint32_t) * max_distance_ref_points); |
| 424 | cur_index += max_distance_ref_points; |
| 425 | } |
| 426 | |
| 427 | return fdt_setprop(fdt, offset, "ibm,associativity-lookup-arrays", |
| 428 | int_buf, (cur_index - int_buf) * sizeof(uint32_t)); |
| 429 | } |
| 430 | |
| 431 | static void spapr_numa_FORM1_write_rtas_dt(SpaprMachineState *spapr, |
| 432 | void *fdt, int rtas) |
| 433 | { |
| 434 | MachineState *ms = MACHINE(spapr); |
| 435 | SpaprMachineClass *smc = SPAPR_MACHINE_GET_CLASS(spapr); |
| 436 | uint32_t refpoints[] = { |
| 437 | cpu_to_be32(0x4), |
| 438 | cpu_to_be32(0x3), |
| 439 | cpu_to_be32(0x2), |
| 440 | cpu_to_be32(0x1), |
| 441 | }; |
| 442 | uint32_t nr_refpoints = ARRAY_SIZE(refpoints); |
| 443 | uint32_t maxdomain = ms->numa_state->num_nodes; |
| 444 | uint32_t maxdomains[] = { |
| 445 | cpu_to_be32(4), |
| 446 | cpu_to_be32(maxdomain), |
| 447 | cpu_to_be32(maxdomain), |
| 448 | cpu_to_be32(maxdomain), |
| 449 | cpu_to_be32(maxdomain) |
| 450 | }; |
| 451 | |
| 452 | if (smc->pre_5_2_numa_associativity || |
| 453 | ms->numa_state->num_nodes <= 1) { |
| 454 | uint32_t legacy_refpoints[] = { |
| 455 | cpu_to_be32(0x4), |
| 456 | cpu_to_be32(0x4), |
| 457 | cpu_to_be32(0x2), |
| 458 | }; |
| 459 | uint32_t legacy_maxdomains[] = { |
| 460 | cpu_to_be32(4), |
| 461 | cpu_to_be32(0), |
| 462 | cpu_to_be32(0), |
| 463 | cpu_to_be32(0), |
| 464 | cpu_to_be32(maxdomain ? maxdomain : 1), |
| 465 | }; |
| 466 | |
| 467 | G_STATIC_ASSERT(sizeof(legacy_refpoints) <= sizeof(refpoints)); |
| 468 | G_STATIC_ASSERT(sizeof(legacy_maxdomains) <= sizeof(maxdomains)); |
| 469 | |
| 470 | nr_refpoints = 3; |
| 471 | |
| 472 | memcpy(refpoints, legacy_refpoints, sizeof(legacy_refpoints)); |
| 473 | memcpy(maxdomains, legacy_maxdomains, sizeof(legacy_maxdomains)); |
| 474 | |
| 475 | /* pseries-5.0 and older reference-points array is {0x4, 0x4} */ |
| 476 | if (smc->pre_5_1_assoc_refpoints) { |
| 477 | nr_refpoints = 2; |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | _FDT(fdt_setprop(fdt, rtas, "ibm,associativity-reference-points", |
| 482 | refpoints, nr_refpoints * sizeof(refpoints[0]))); |
| 483 | |
| 484 | _FDT(fdt_setprop(fdt, rtas, "ibm,max-associativity-domains", |
| 485 | maxdomains, sizeof(maxdomains))); |
| 486 | } |
| 487 | |
| 488 | static void spapr_numa_FORM2_write_rtas_tables(SpaprMachineState *spapr, |
| 489 | void *fdt, int rtas) |
| 490 | { |
| 491 | MachineState *ms = MACHINE(spapr); |
| 492 | int nb_numa_nodes = ms->numa_state->num_nodes; |
| 493 | int distance_table_entries = nb_numa_nodes * nb_numa_nodes; |
| 494 | g_autofree uint32_t *lookup_index_table = NULL; |
| 495 | g_autofree uint8_t *distance_table = NULL; |
| 496 | int src, dst, i, distance_table_size; |
| 497 | |
| 498 | /* |
| 499 | * ibm,numa-lookup-index-table: array with length and a |
| 500 | * list of NUMA ids present in the guest. |
| 501 | */ |
| 502 | lookup_index_table = g_new0(uint32_t, nb_numa_nodes + 1); |
| 503 | lookup_index_table[0] = cpu_to_be32(nb_numa_nodes); |
| 504 | |
| 505 | for (i = 0; i < nb_numa_nodes; i++) { |
| 506 | lookup_index_table[i + 1] = cpu_to_be32(i); |
| 507 | } |
| 508 | |
| 509 | _FDT(fdt_setprop(fdt, rtas, "ibm,numa-lookup-index-table", |
| 510 | lookup_index_table, |
| 511 | (nb_numa_nodes + 1) * sizeof(uint32_t))); |
| 512 | |
| 513 | /* |
| 514 | * ibm,numa-distance-table: contains all node distances. First |
| 515 | * element is the size of the table as uint32, followed up |
| 516 | * by all the uint8 distances from the first NUMA node, then all |
| 517 | * distances from the second NUMA node and so on. |
| 518 | * |
| 519 | * ibm,numa-lookup-index-table is used by guest to navigate this |
| 520 | * array because NUMA ids can be sparse (node 0 is the first, |
| 521 | * node 8 is the second ...). |
| 522 | */ |
| 523 | distance_table_size = distance_table_entries * sizeof(uint8_t) + |
| 524 | sizeof(uint32_t); |
| 525 | distance_table = g_new0(uint8_t, distance_table_size); |
| 526 | stl_be_p(distance_table, distance_table_entries); |
| 527 | |
| 528 | /* Skip the uint32_t array length at the start */ |
| 529 | i = sizeof(uint32_t); |
| 530 | |
| 531 | for (src = 0; src < nb_numa_nodes; src++) { |
| 532 | for (dst = 0; dst < nb_numa_nodes; dst++) { |
| 533 | distance_table[i++] = get_numa_distance(ms, src, dst); |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | _FDT(fdt_setprop(fdt, rtas, "ibm,numa-distance-table", |
| 538 | distance_table, distance_table_size)); |
| 539 | } |
| 540 | |
| 541 | /* |
| 542 | * This helper could be compressed in a single function with |
| 543 | * FORM1 logic since we're setting the same DT values, with the |
| 544 | * difference being a call to spapr_numa_FORM2_write_rtas_tables() |
| 545 | * in the end. The separation was made to avoid clogging FORM1 code |
| 546 | * which already has to deal with compat modes from previous |
| 547 | * QEMU machine types. |
| 548 | */ |
| 549 | static void spapr_numa_FORM2_write_rtas_dt(SpaprMachineState *spapr, |
| 550 | void *fdt, int rtas) |
| 551 | { |
| 552 | MachineState *ms = MACHINE(spapr); |
| 553 | |
| 554 | /* |
| 555 | * In FORM2, ibm,associativity-reference-points will point to |
| 556 | * the element in the ibm,associativity array that contains the |
| 557 | * primary domain index (for FORM2, the first element). |
| 558 | * |
| 559 | * This value (in our case, the numa-id) is then used as an index |
| 560 | * to retrieve all other attributes of the node (distance, |
| 561 | * bandwidth, latency) via ibm,numa-lookup-index-table and other |
| 562 | * ibm,numa-*-table properties. |
| 563 | */ |
| 564 | uint32_t refpoints[] = { cpu_to_be32(1) }; |
| 565 | |
| 566 | uint32_t maxdomain = ms->numa_state->num_nodes; |
| 567 | uint32_t maxdomains[] = { cpu_to_be32(1), cpu_to_be32(maxdomain) }; |
| 568 | |
| 569 | _FDT(fdt_setprop(fdt, rtas, "ibm,associativity-reference-points", |
| 570 | refpoints, sizeof(refpoints))); |
| 571 | |
| 572 | _FDT(fdt_setprop(fdt, rtas, "ibm,max-associativity-domains", |
| 573 | maxdomains, sizeof(maxdomains))); |
| 574 | |
| 575 | spapr_numa_FORM2_write_rtas_tables(spapr, fdt, rtas); |
| 576 | } |
| 577 | |
| 578 | /* |
| 579 | * Helper that writes ibm,associativity-reference-points and |
| 580 | * max-associativity-domains in the RTAS pointed by @rtas |
| 581 | * in the DT @fdt. |
| 582 | */ |
| 583 | void spapr_numa_write_rtas_dt(SpaprMachineState *spapr, void *fdt, int rtas) |
| 584 | { |
| 585 | if (spapr_ovec_test(spapr->ov5_cas, OV5_FORM2_AFFINITY)) { |
| 586 | spapr_numa_FORM2_write_rtas_dt(spapr, fdt, rtas); |
| 587 | return; |
| 588 | } |
| 589 | |
| 590 | spapr_numa_FORM1_write_rtas_dt(spapr, fdt, rtas); |
| 591 | } |
| 592 | |
| 593 | static target_ulong h_home_node_associativity(PowerPCCPU *cpu, |
| 594 | SpaprMachineState *spapr, |
| 595 | target_ulong opcode, |
| 596 | target_ulong *args) |
| 597 | { |
| 598 | g_autofree uint32_t *vcpu_assoc = NULL; |
| 599 | target_ulong flags = args[0]; |
| 600 | target_ulong procno = args[1]; |
| 601 | PowerPCCPU *tcpu; |
| 602 | int idx, assoc_idx; |
| 603 | int vcpu_assoc_size = get_vcpu_assoc_size(spapr); |
| 604 | |
| 605 | /* only support procno from H_REGISTER_VPA */ |
| 606 | if (flags != 0x1) { |
| 607 | return H_FUNCTION; |
| 608 | } |
| 609 | |
| 610 | tcpu = spapr_find_cpu(procno); |
| 611 | if (tcpu == NULL) { |
| 612 | return H_P2; |
| 613 | } |
| 614 | |
| 615 | /* |
| 616 | * Given that we want to be flexible with the sizes and indexes, |
| 617 | * we must consider that there is a hard limit of how many |
| 618 | * associativities domain we can fit in R4 up to R9, which would be |
| 619 | * 12 associativity domains for vcpus. Assert and bail if that's |
| 620 | * not the case. |
| 621 | */ |
| 622 | g_assert((vcpu_assoc_size - 1) <= 12); |
| 623 | |
| 624 | vcpu_assoc = spapr_numa_get_vcpu_assoc(spapr, tcpu); |
| 625 | /* assoc_idx starts at 1 to skip associativity size */ |
| 626 | assoc_idx = 1; |
| 627 | |
| 628 | #define ASSOCIATIVITY(a, b) (((uint64_t)(a) << 32) | \ |
| 629 | ((uint64_t)(b) & 0xffffffff)) |
| 630 | |
| 631 | for (idx = 0; idx < 6; idx++) { |
| 632 | int32_t a, b; |
| 633 | |
| 634 | /* |
| 635 | * vcpu_assoc[] will contain the associativity domains for tcpu, |
| 636 | * including tcpu->node_id and procno, meaning that we don't |
| 637 | * need to use these variables here. |
| 638 | * |
| 639 | * We'll read 2 values at a time to fill up the ASSOCIATIVITY() |
| 640 | * macro. The ternary will fill the remaining registers with -1 |
| 641 | * after we went through vcpu_assoc[]. |
| 642 | */ |
| 643 | a = assoc_idx < vcpu_assoc_size ? |
| 644 | be32_to_cpu(vcpu_assoc[assoc_idx++]) : -1; |
| 645 | b = assoc_idx < vcpu_assoc_size ? |
| 646 | be32_to_cpu(vcpu_assoc[assoc_idx++]) : -1; |
| 647 | |
| 648 | args[idx] = ASSOCIATIVITY(a, b); |
| 649 | } |
| 650 | #undef ASSOCIATIVITY |
| 651 | |
| 652 | return H_SUCCESS; |
| 653 | } |
| 654 | |
| 655 | static void spapr_numa_register_types(void) |
| 656 | { |
| 657 | /* Virtual Processor Home Node */ |
| 658 | spapr_register_hypercall(H_HOME_NODE_ASSOCIATIVITY, |
| 659 | h_home_node_associativity); |
| 660 | } |
| 661 | |
| 662 | type_init(spapr_numa_register_types) |