xref: /openbmc/qemu/hw/ppc/spapr_numa.c (revision 0ee520126a2a1a2539e54b6d0879380cb0bf4ea4)
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 int spapr_numa_write_assoc_lookup_arrays(SpaprMachineState *spapr, void *fdt,
77                                          int offset)
78 {
79     MachineState *machine = MACHINE(spapr);
80     int nb_numa_nodes = machine->numa_state->num_nodes;
81     int nr_nodes = nb_numa_nodes ? nb_numa_nodes : 1;
82     uint32_t *int_buf, *cur_index, buf_len;
83     int ret, i;
84 
85     /* ibm,associativity-lookup-arrays */
86     buf_len = (nr_nodes * MAX_DISTANCE_REF_POINTS + 2) * sizeof(uint32_t);
87     cur_index = int_buf = g_malloc0(buf_len);
88     int_buf[0] = cpu_to_be32(nr_nodes);
89      /* Number of entries per associativity list */
90     int_buf[1] = cpu_to_be32(MAX_DISTANCE_REF_POINTS);
91     cur_index += 2;
92     for (i = 0; i < nr_nodes; i++) {
93         /*
94          * For the lookup-array we use the ibm,associativity array,
95          * from numa_assoc_array. without the first element (size).
96          */
97         uint32_t *associativity = spapr->numa_assoc_array[i];
98         memcpy(cur_index, ++associativity,
99                sizeof(uint32_t) * MAX_DISTANCE_REF_POINTS);
100         cur_index += MAX_DISTANCE_REF_POINTS;
101     }
102     ret = fdt_setprop(fdt, offset, "ibm,associativity-lookup-arrays", int_buf,
103                       (cur_index - int_buf) * sizeof(uint32_t));
104     g_free(int_buf);
105 
106     return ret;
107 }
108 
109 /*
110  * Helper that writes ibm,associativity-reference-points and
111  * max-associativity-domains in the RTAS pointed by @rtas
112  * in the DT @fdt.
113  */
114 void spapr_numa_write_rtas_dt(SpaprMachineState *spapr, void *fdt, int rtas)
115 {
116     SpaprMachineClass *smc = SPAPR_MACHINE_GET_CLASS(spapr);
117     uint32_t refpoints[] = {
118         cpu_to_be32(0x4),
119         cpu_to_be32(0x4),
120         cpu_to_be32(0x2),
121     };
122     uint32_t nr_refpoints = ARRAY_SIZE(refpoints);
123     uint32_t maxdomain = cpu_to_be32(spapr->gpu_numa_id > 1 ? 1 : 0);
124     uint32_t maxdomains[] = {
125         cpu_to_be32(4),
126         maxdomain,
127         maxdomain,
128         maxdomain,
129         cpu_to_be32(spapr->gpu_numa_id),
130     };
131 
132     if (smc->pre_5_1_assoc_refpoints) {
133         nr_refpoints = 2;
134     }
135 
136     _FDT(fdt_setprop(fdt, rtas, "ibm,associativity-reference-points",
137                      refpoints, nr_refpoints * sizeof(refpoints[0])));
138 
139     _FDT(fdt_setprop(fdt, rtas, "ibm,max-associativity-domains",
140                      maxdomains, sizeof(maxdomains)));
141 }
142