xref: /openbmc/qemu/hw/ppc/spapr_numa.c (revision 8f86a408241264db605da9a1215a409475a60bd0)
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/ppc/fdt.h"
17 
18 
19 void spapr_numa_associativity_init(SpaprMachineState *spapr,
20                                    MachineState *machine)
21 {
22     int nb_numa_nodes = machine->numa_state->num_nodes;
23     int i;
24 
25     /*
26      * For all associativity arrays: first position is the size,
27      * position MAX_DISTANCE_REF_POINTS is always the numa_id,
28      * represented by the index 'i'.
29      *
30      * This will break on sparse NUMA setups, when/if QEMU starts
31      * to support it, because there will be no more guarantee that
32      * 'i' will be a valid node_id set by the user.
33      */
34     for (i = 0; i < nb_numa_nodes; i++) {
35         spapr->numa_assoc_array[i][0] = cpu_to_be32(MAX_DISTANCE_REF_POINTS);
36         spapr->numa_assoc_array[i][MAX_DISTANCE_REF_POINTS] = cpu_to_be32(i);
37     }
38 }
39 
40 void spapr_numa_write_associativity_dt(SpaprMachineState *spapr, void *fdt,
41                                        int offset, int nodeid)
42 {
43     _FDT((fdt_setprop(fdt, offset, "ibm,associativity",
44                       spapr->numa_assoc_array[nodeid],
45                       sizeof(spapr->numa_assoc_array[nodeid]))));
46 }
47 
48 int spapr_numa_fixup_cpu_dt(SpaprMachineState *spapr, void *fdt,
49                             int offset, PowerPCCPU *cpu)
50 {
51     int vcpu_assoc_size = NUMA_ASSOC_SIZE + 1;
52     uint32_t vcpu_assoc[vcpu_assoc_size];
53     int index = spapr_get_vcpu_id(cpu);
54     int i;
55 
56     /*
57      * VCPUs have an extra 'cpu_id' value in ibm,associativity
58      * compared to other resources. Increment the size at index
59      * 0, copy all associativity domains already set, then put
60      * cpu_id last.
61      */
62     vcpu_assoc[0] = cpu_to_be32(MAX_DISTANCE_REF_POINTS + 1);
63 
64     for (i = 1; i <= MAX_DISTANCE_REF_POINTS; i++) {
65         vcpu_assoc[i] = spapr->numa_assoc_array[cpu->node_id][i];
66     }
67 
68     vcpu_assoc[vcpu_assoc_size - 1] = cpu_to_be32(index);
69 
70     /* Advertise NUMA via ibm,associativity */
71     return fdt_setprop(fdt, offset, "ibm,associativity",
72                        vcpu_assoc, sizeof(vcpu_assoc));
73 }
74 
75 /*
76  * Helper that writes ibm,associativity-reference-points and
77  * max-associativity-domains in the RTAS pointed by @rtas
78  * in the DT @fdt.
79  */
80 void spapr_numa_write_rtas_dt(SpaprMachineState *spapr, void *fdt, int rtas)
81 {
82     SpaprMachineClass *smc = SPAPR_MACHINE_GET_CLASS(spapr);
83     uint32_t refpoints[] = {
84         cpu_to_be32(0x4),
85         cpu_to_be32(0x4),
86         cpu_to_be32(0x2),
87     };
88     uint32_t nr_refpoints = ARRAY_SIZE(refpoints);
89     uint32_t maxdomain = cpu_to_be32(spapr->gpu_numa_id > 1 ? 1 : 0);
90     uint32_t maxdomains[] = {
91         cpu_to_be32(4),
92         maxdomain,
93         maxdomain,
94         maxdomain,
95         cpu_to_be32(spapr->gpu_numa_id),
96     };
97 
98     if (smc->pre_5_1_assoc_refpoints) {
99         nr_refpoints = 2;
100     }
101 
102     _FDT(fdt_setprop(fdt, rtas, "ibm,associativity-reference-points",
103                      refpoints, nr_refpoints * sizeof(refpoints[0])));
104 
105     _FDT(fdt_setprop(fdt, rtas, "ibm,max-associativity-domains",
106                      maxdomains, sizeof(maxdomains)));
107 }
108