2 * QEMU PowerPC pSeries Logical Partition NUMA associativity handling
4 * Copyright IBM Corp. 2020
7 * Daniel Henrique Barboza <danielhb413@gmail.com>
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.
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"
19 /* Moved from hw/ppc/spapr_pci_nvlink2.c */
20 #define SPAPR_GPU_NUMA_ID (cpu_to_be32(1))
22 static bool spapr_machine_using_legacy_numa(SpaprMachineState
*spapr
)
24 MachineState
*machine
= MACHINE(spapr
);
25 SpaprMachineClass
*smc
= SPAPR_MACHINE_GET_CLASS(machine
);
27 return smc
->pre_5_2_numa_associativity
||
28 machine
->numa_state
->num_nodes
<= 1;
31 static bool spapr_numa_is_symmetrical(MachineState
*ms
)
34 int nb_numa_nodes
= ms
->numa_state
->num_nodes
;
35 NodeInfo
*numa_info
= ms
->numa_state
->nodes
;
37 for (src
= 0; src
< nb_numa_nodes
; src
++) {
38 for (dst
= src
; dst
< nb_numa_nodes
; dst
++) {
39 if (numa_info
[src
].distance
[dst
] !=
40 numa_info
[dst
].distance
[src
]) {
50 * NVLink2-connected GPU RAM needs to be placed on a separate NUMA node.
51 * We assign a new numa ID per GPU in spapr_pci_collect_nvgpu() which is
52 * called from vPHB reset handler so we initialize the counter here.
53 * If no NUMA is configured from the QEMU side, we start from 1 as GPU RAM
54 * must be equally distant from any other node.
55 * The final value of spapr->gpu_numa_id is going to be written to
56 * max-associativity-domains in spapr_build_fdt().
58 unsigned int spapr_numa_initial_nvgpu_numa_id(MachineState
*machine
)
60 return MAX(1, machine
->numa_state
->num_nodes
);
64 * This function will translate the user distances into
65 * what the kernel understand as possible values: 10
66 * (local distance), 20, 40, 80 and 160, and return the equivalent
67 * NUMA level for each. Current heuristic is:
68 * - local distance (10) returns numa_level = 0x4, meaning there is
69 * no rounding for local distance
70 * - distances between 11 and 30 inclusive -> rounded to 20,
72 * - distances between 31 and 60 inclusive -> rounded to 40,
74 * - distances between 61 and 120 inclusive -> rounded to 80,
76 * - everything above 120 returns numa_level = 0 to indicate that
77 * there is no match. This will be calculated as disntace = 160
78 * by the kernel (as of v5.9)
80 static uint8_t spapr_numa_get_numa_level(uint8_t distance
)
84 } else if (distance
> 11 && distance
<= 30) {
86 } else if (distance
> 31 && distance
<= 60) {
88 } else if (distance
> 61 && distance
<= 120) {
95 static void spapr_numa_define_associativity_domains(SpaprMachineState
*spapr
)
97 MachineState
*ms
= MACHINE(spapr
);
98 NodeInfo
*numa_info
= ms
->numa_state
->nodes
;
99 int nb_numa_nodes
= ms
->numa_state
->num_nodes
;
102 for (src
= 0; src
< nb_numa_nodes
; src
++) {
103 for (dst
= src
; dst
< nb_numa_nodes
; dst
++) {
105 * This is how the associativity domain between A and B
108 * - get the distance D between them
109 * - get the correspondent NUMA level 'n_level' for D
110 * - all associativity arrays were initialized with their own
111 * numa_ids, and we're calculating the distance in node_id
112 * ascending order, starting from node id 0 (the first node
113 * retrieved by numa_state). This will have a cascade effect in
114 * the algorithm because the associativity domains that node 0
115 * defines will be carried over to other nodes, and node 1
116 * associativities will be carried over after taking node 0
117 * associativities into account, and so on. This happens because
118 * we'll assign assoc_src as the associativity domain of dst
119 * as well, for all NUMA levels beyond and including n_level.
121 * The PPC kernel expects the associativity domains of node 0 to
122 * be always 0, and this algorithm will grant that by default.
124 uint8_t distance
= numa_info
[src
].distance
[dst
];
125 uint8_t n_level
= spapr_numa_get_numa_level(distance
);
129 * n_level = 0 means that the distance is greater than our last
130 * rounded value (120). In this case there is no NUMA level match
131 * between src and dst and we can skip the remaining of the loop.
133 * The Linux kernel will assume that the distance between src and
134 * dst, in this case of no match, is 10 (local distance) doubled
135 * for each NUMA it didn't match. We have MAX_DISTANCE_REF_POINTS
136 * levels (4), so this gives us 10*2*2*2*2 = 160.
138 * This logic can be seen in the Linux kernel source code, as of
139 * v5.9, in arch/powerpc/mm/numa.c, function __node_distance().
146 * We must assign all assoc_src to dst, starting from n_level
147 * and going up to 0x1.
149 for (i
= n_level
; i
> 0; i
--) {
150 assoc_src
= spapr
->numa_assoc_array
[src
][i
];
151 spapr
->numa_assoc_array
[dst
][i
] = assoc_src
;
158 void spapr_numa_associativity_init(SpaprMachineState
*spapr
,
159 MachineState
*machine
)
161 SpaprMachineClass
*smc
= SPAPR_MACHINE_GET_CLASS(spapr
);
162 int nb_numa_nodes
= machine
->numa_state
->num_nodes
;
163 int i
, j
, max_nodes_with_gpus
;
164 bool using_legacy_numa
= spapr_machine_using_legacy_numa(spapr
);
167 * For all associativity arrays: first position is the size,
168 * position MAX_DISTANCE_REF_POINTS is always the numa_id,
169 * represented by the index 'i'.
171 * This will break on sparse NUMA setups, when/if QEMU starts
172 * to support it, because there will be no more guarantee that
173 * 'i' will be a valid node_id set by the user.
175 for (i
= 0; i
< nb_numa_nodes
; i
++) {
176 spapr
->numa_assoc_array
[i
][0] = cpu_to_be32(MAX_DISTANCE_REF_POINTS
);
177 spapr
->numa_assoc_array
[i
][MAX_DISTANCE_REF_POINTS
] = cpu_to_be32(i
);
180 * Fill all associativity domains of non-zero NUMA nodes with
181 * node_id. This is required because the default value (0) is
182 * considered a match with associativity domains of node 0.
184 if (!using_legacy_numa
&& i
!= 0) {
185 for (j
= 1; j
< MAX_DISTANCE_REF_POINTS
; j
++) {
186 spapr
->numa_assoc_array
[i
][j
] = cpu_to_be32(i
);
192 * Initialize NVLink GPU associativity arrays. We know that
193 * the first GPU will take the first available NUMA id, and
194 * we'll have a maximum of NVGPU_MAX_NUM GPUs in the machine.
195 * At this point we're not sure if there are GPUs or not, but
196 * let's initialize the associativity arrays and allow NVLink
197 * GPUs to be handled like regular NUMA nodes later on.
199 max_nodes_with_gpus
= nb_numa_nodes
+ NVGPU_MAX_NUM
;
201 for (i
= nb_numa_nodes
; i
< max_nodes_with_gpus
; i
++) {
202 spapr
->numa_assoc_array
[i
][0] = cpu_to_be32(MAX_DISTANCE_REF_POINTS
);
204 for (j
= 1; j
< MAX_DISTANCE_REF_POINTS
; j
++) {
205 uint32_t gpu_assoc
= smc
->pre_5_1_assoc_refpoints
?
206 SPAPR_GPU_NUMA_ID
: cpu_to_be32(i
);
207 spapr
->numa_assoc_array
[i
][j
] = gpu_assoc
;
210 spapr
->numa_assoc_array
[i
][MAX_DISTANCE_REF_POINTS
] = cpu_to_be32(i
);
214 * Legacy NUMA guests (pseries-5.1 and older, or guests with only
215 * 1 NUMA node) will not benefit from anything we're going to do
218 if (using_legacy_numa
) {
222 if (!spapr_numa_is_symmetrical(machine
)) {
223 error_report("Asymmetrical NUMA topologies aren't supported "
224 "in the pSeries machine");
228 spapr_numa_define_associativity_domains(spapr
);
231 void spapr_numa_write_associativity_dt(SpaprMachineState
*spapr
, void *fdt
,
232 int offset
, int nodeid
)
234 _FDT((fdt_setprop(fdt
, offset
, "ibm,associativity",
235 spapr
->numa_assoc_array
[nodeid
],
236 sizeof(spapr
->numa_assoc_array
[nodeid
]))));
239 static uint32_t *spapr_numa_get_vcpu_assoc(SpaprMachineState
*spapr
,
242 uint32_t *vcpu_assoc
= g_new(uint32_t, VCPU_ASSOC_SIZE
);
243 int index
= spapr_get_vcpu_id(cpu
);
246 * VCPUs have an extra 'cpu_id' value in ibm,associativity
247 * compared to other resources. Increment the size at index
248 * 0, put cpu_id last, then copy the remaining associativity
251 vcpu_assoc
[0] = cpu_to_be32(MAX_DISTANCE_REF_POINTS
+ 1);
252 vcpu_assoc
[VCPU_ASSOC_SIZE
- 1] = cpu_to_be32(index
);
253 memcpy(vcpu_assoc
+ 1, spapr
->numa_assoc_array
[cpu
->node_id
] + 1,
254 (VCPU_ASSOC_SIZE
- 2) * sizeof(uint32_t));
259 int spapr_numa_fixup_cpu_dt(SpaprMachineState
*spapr
, void *fdt
,
260 int offset
, PowerPCCPU
*cpu
)
262 g_autofree
uint32_t *vcpu_assoc
= NULL
;
264 vcpu_assoc
= spapr_numa_get_vcpu_assoc(spapr
, cpu
);
266 /* Advertise NUMA via ibm,associativity */
267 return fdt_setprop(fdt
, offset
, "ibm,associativity", vcpu_assoc
,
268 VCPU_ASSOC_SIZE
* sizeof(uint32_t));
272 int spapr_numa_write_assoc_lookup_arrays(SpaprMachineState
*spapr
, void *fdt
,
275 MachineState
*machine
= MACHINE(spapr
);
276 int nb_numa_nodes
= machine
->numa_state
->num_nodes
;
277 int nr_nodes
= nb_numa_nodes
? nb_numa_nodes
: 1;
278 uint32_t *int_buf
, *cur_index
, buf_len
;
281 /* ibm,associativity-lookup-arrays */
282 buf_len
= (nr_nodes
* MAX_DISTANCE_REF_POINTS
+ 2) * sizeof(uint32_t);
283 cur_index
= int_buf
= g_malloc0(buf_len
);
284 int_buf
[0] = cpu_to_be32(nr_nodes
);
285 /* Number of entries per associativity list */
286 int_buf
[1] = cpu_to_be32(MAX_DISTANCE_REF_POINTS
);
288 for (i
= 0; i
< nr_nodes
; i
++) {
290 * For the lookup-array we use the ibm,associativity array,
291 * from numa_assoc_array. without the first element (size).
293 uint32_t *associativity
= spapr
->numa_assoc_array
[i
];
294 memcpy(cur_index
, ++associativity
,
295 sizeof(uint32_t) * MAX_DISTANCE_REF_POINTS
);
296 cur_index
+= MAX_DISTANCE_REF_POINTS
;
298 ret
= fdt_setprop(fdt
, offset
, "ibm,associativity-lookup-arrays", int_buf
,
299 (cur_index
- int_buf
) * sizeof(uint32_t));
306 * Helper that writes ibm,associativity-reference-points and
307 * max-associativity-domains in the RTAS pointed by @rtas
310 void spapr_numa_write_rtas_dt(SpaprMachineState
*spapr
, void *fdt
, int rtas
)
312 MachineState
*ms
= MACHINE(spapr
);
313 SpaprMachineClass
*smc
= SPAPR_MACHINE_GET_CLASS(spapr
);
314 uint32_t number_nvgpus_nodes
= spapr
->gpu_numa_id
-
315 spapr_numa_initial_nvgpu_numa_id(ms
);
316 uint32_t refpoints
[] = {
322 uint32_t nr_refpoints
= ARRAY_SIZE(refpoints
);
323 uint32_t maxdomain
= ms
->numa_state
->num_nodes
+ number_nvgpus_nodes
;
324 uint32_t maxdomains
[] = {
326 cpu_to_be32(maxdomain
),
327 cpu_to_be32(maxdomain
),
328 cpu_to_be32(maxdomain
),
329 cpu_to_be32(maxdomain
)
332 if (spapr_machine_using_legacy_numa(spapr
)) {
333 uint32_t legacy_refpoints
[] = {
338 uint32_t legacy_maxdomain
= spapr
->gpu_numa_id
> 1 ? 1 : 0;
339 uint32_t legacy_maxdomains
[] = {
341 cpu_to_be32(legacy_maxdomain
),
342 cpu_to_be32(legacy_maxdomain
),
343 cpu_to_be32(legacy_maxdomain
),
344 cpu_to_be32(spapr
->gpu_numa_id
),
347 G_STATIC_ASSERT(sizeof(legacy_refpoints
) <= sizeof(refpoints
));
348 G_STATIC_ASSERT(sizeof(legacy_maxdomains
) <= sizeof(maxdomains
));
352 memcpy(refpoints
, legacy_refpoints
, sizeof(legacy_refpoints
));
353 memcpy(maxdomains
, legacy_maxdomains
, sizeof(legacy_maxdomains
));
355 /* pseries-5.0 and older reference-points array is {0x4, 0x4} */
356 if (smc
->pre_5_1_assoc_refpoints
) {
361 _FDT(fdt_setprop(fdt
, rtas
, "ibm,associativity-reference-points",
362 refpoints
, nr_refpoints
* sizeof(refpoints
[0])));
364 _FDT(fdt_setprop(fdt
, rtas
, "ibm,max-associativity-domains",
365 maxdomains
, sizeof(maxdomains
)));
368 static target_ulong
h_home_node_associativity(PowerPCCPU
*cpu
,
369 SpaprMachineState
*spapr
,
373 g_autofree
uint32_t *vcpu_assoc
= NULL
;
374 target_ulong flags
= args
[0];
375 target_ulong procno
= args
[1];
379 /* only support procno from H_REGISTER_VPA */
384 tcpu
= spapr_find_cpu(procno
);
390 * Given that we want to be flexible with the sizes and indexes,
391 * we must consider that there is a hard limit of how many
392 * associativities domain we can fit in R4 up to R9, which would be
393 * 12 associativity domains for vcpus. Assert and bail if that's
396 G_STATIC_ASSERT((VCPU_ASSOC_SIZE
- 1) <= 12);
398 vcpu_assoc
= spapr_numa_get_vcpu_assoc(spapr
, tcpu
);
399 /* assoc_idx starts at 1 to skip associativity size */
402 #define ASSOCIATIVITY(a, b) (((uint64_t)(a) << 32) | \
403 ((uint64_t)(b) & 0xffffffff))
405 for (idx
= 0; idx
< 6; idx
++) {
409 * vcpu_assoc[] will contain the associativity domains for tcpu,
410 * including tcpu->node_id and procno, meaning that we don't
411 * need to use these variables here.
413 * We'll read 2 values at a time to fill up the ASSOCIATIVITY()
414 * macro. The ternary will fill the remaining registers with -1
415 * after we went through vcpu_assoc[].
417 a
= assoc_idx
< VCPU_ASSOC_SIZE
?
418 be32_to_cpu(vcpu_assoc
[assoc_idx
++]) : -1;
419 b
= assoc_idx
< VCPU_ASSOC_SIZE
?
420 be32_to_cpu(vcpu_assoc
[assoc_idx
++]) : -1;
422 args
[idx
] = ASSOCIATIVITY(a
, b
);
429 static void spapr_numa_register_types(void)
431 /* Virtual Processor Home Node */
432 spapr_register_hypercall(H_HOME_NODE_ASSOCIATIVITY
,
433 h_home_node_associativity
);
436 type_init(spapr_numa_register_types
)