1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2024, NVIDIA CORPORATION & AFFILIATES. All rights reserved
4  */
5 
6 #include "qemu/osdep.h"
7 #include "hw/acpi/acpi_generic_initiator.h"
8 #include "hw/acpi/aml-build.h"
9 #include "hw/boards.h"
10 #include "hw/pci/pci_device.h"
11 #include "qemu/error-report.h"
12 
13 typedef struct AcpiGenericInitiatorClass {
14     ObjectClass parent_class;
15 } AcpiGenericInitiatorClass;
16 
17 OBJECT_DEFINE_TYPE_WITH_INTERFACES(AcpiGenericInitiator, acpi_generic_initiator,
18                    ACPI_GENERIC_INITIATOR, OBJECT,
19                    { TYPE_USER_CREATABLE },
20                    { NULL })
21 
OBJECT_DECLARE_SIMPLE_TYPE(AcpiGenericInitiator,ACPI_GENERIC_INITIATOR)22 OBJECT_DECLARE_SIMPLE_TYPE(AcpiGenericInitiator, ACPI_GENERIC_INITIATOR)
23 
24 static void acpi_generic_initiator_init(Object *obj)
25 {
26     AcpiGenericInitiator *gi = ACPI_GENERIC_INITIATOR(obj);
27 
28     gi->node = MAX_NODES;
29     gi->pci_dev = NULL;
30 }
31 
acpi_generic_initiator_finalize(Object * obj)32 static void acpi_generic_initiator_finalize(Object *obj)
33 {
34     AcpiGenericInitiator *gi = ACPI_GENERIC_INITIATOR(obj);
35 
36     g_free(gi->pci_dev);
37 }
38 
acpi_generic_initiator_set_pci_device(Object * obj,const char * val,Error ** errp)39 static void acpi_generic_initiator_set_pci_device(Object *obj, const char *val,
40                                                   Error **errp)
41 {
42     AcpiGenericInitiator *gi = ACPI_GENERIC_INITIATOR(obj);
43 
44     gi->pci_dev = g_strdup(val);
45 }
46 
acpi_generic_initiator_set_node(Object * obj,Visitor * v,const char * name,void * opaque,Error ** errp)47 static void acpi_generic_initiator_set_node(Object *obj, Visitor *v,
48                                             const char *name, void *opaque,
49                                             Error **errp)
50 {
51     AcpiGenericInitiator *gi = ACPI_GENERIC_INITIATOR(obj);
52     MachineState *ms = MACHINE(qdev_get_machine());
53     uint32_t value;
54 
55     if (!visit_type_uint32(v, name, &value, errp)) {
56         return;
57     }
58 
59     if (value >= MAX_NODES) {
60         error_printf("%s: Invalid NUMA node specified\n",
61                      TYPE_ACPI_GENERIC_INITIATOR);
62         exit(1);
63     }
64 
65     gi->node = value;
66     ms->numa_state->nodes[gi->node].has_gi = true;
67 }
68 
acpi_generic_initiator_class_init(ObjectClass * oc,void * data)69 static void acpi_generic_initiator_class_init(ObjectClass *oc, void *data)
70 {
71     object_class_property_add_str(oc, "pci-dev", NULL,
72         acpi_generic_initiator_set_pci_device);
73     object_class_property_add(oc, "node", "int", NULL,
74         acpi_generic_initiator_set_node, NULL, NULL);
75 }
76 
77 /*
78  * ACPI 6.3:
79  * Table 5-78 Generic Initiator Affinity Structure
80  */
81 static void
build_srat_generic_pci_initiator_affinity(GArray * table_data,int node,PCIDeviceHandle * handle)82 build_srat_generic_pci_initiator_affinity(GArray *table_data, int node,
83                                           PCIDeviceHandle *handle)
84 {
85     uint8_t index;
86 
87     build_append_int_noprefix(table_data, 5, 1);  /* Type */
88     build_append_int_noprefix(table_data, 32, 1); /* Length */
89     build_append_int_noprefix(table_data, 0, 1);  /* Reserved */
90     build_append_int_noprefix(table_data, 1, 1);  /* Device Handle Type: PCI */
91     build_append_int_noprefix(table_data, node, 4);  /* Proximity Domain */
92 
93     /* Device Handle - PCI */
94     build_append_int_noprefix(table_data, handle->segment, 2);
95     build_append_int_noprefix(table_data, PCI_BUS_NUM(handle->bdf), 1);
96     build_append_int_noprefix(table_data, PCI_BDF_TO_DEVFN(handle->bdf), 1);
97     for (index = 0; index < 12; index++) {
98         build_append_int_noprefix(table_data, 0, 1);
99     }
100 
101     build_append_int_noprefix(table_data, GEN_AFFINITY_ENABLED, 4); /* Flags */
102     build_append_int_noprefix(table_data, 0, 4);     /* Reserved */
103 }
104 
build_all_acpi_generic_initiators(Object * obj,void * opaque)105 static int build_all_acpi_generic_initiators(Object *obj, void *opaque)
106 {
107     MachineState *ms = MACHINE(qdev_get_machine());
108     AcpiGenericInitiator *gi;
109     GArray *table_data = opaque;
110     PCIDeviceHandle dev_handle;
111     PCIDevice *pci_dev;
112     Object *o;
113 
114     if (!object_dynamic_cast(obj, TYPE_ACPI_GENERIC_INITIATOR)) {
115         return 0;
116     }
117 
118     gi = ACPI_GENERIC_INITIATOR(obj);
119     if (gi->node >= ms->numa_state->num_nodes) {
120         error_printf("%s: Specified node %d is invalid.\n",
121                      TYPE_ACPI_GENERIC_INITIATOR, gi->node);
122         exit(1);
123     }
124 
125     o = object_resolve_path_type(gi->pci_dev, TYPE_PCI_DEVICE, NULL);
126     if (!o) {
127         error_printf("%s: Specified device must be a PCI device.\n",
128                      TYPE_ACPI_GENERIC_INITIATOR);
129         exit(1);
130     }
131 
132     pci_dev = PCI_DEVICE(o);
133 
134     dev_handle.segment = 0;
135     dev_handle.bdf = PCI_BUILD_BDF(pci_bus_num(pci_get_bus(pci_dev)),
136                                                pci_dev->devfn);
137 
138     build_srat_generic_pci_initiator_affinity(table_data,
139                                               gi->node, &dev_handle);
140 
141     return 0;
142 }
143 
build_srat_generic_pci_initiator(GArray * table_data)144 void build_srat_generic_pci_initiator(GArray *table_data)
145 {
146     object_child_foreach_recursive(object_get_root(),
147                                    build_all_acpi_generic_initiators,
148                                    table_data);
149 }
150