GNU Linux-libre 4.19.286-gnu1
[releases.git] / arch / x86 / kernel / apic / x2apic_phys.c
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/threads.h>
3 #include <linux/cpumask.h>
4 #include <linux/string.h>
5 #include <linux/kernel.h>
6 #include <linux/ctype.h>
7 #include <linux/dmar.h>
8
9 #include <asm/smp.h>
10 #include <asm/ipi.h>
11 #include "x2apic.h"
12
13 int x2apic_phys;
14
15 static struct apic apic_x2apic_phys;
16 static u32 x2apic_max_apicid __ro_after_init;
17
18 void __init x2apic_set_max_apicid(u32 apicid)
19 {
20         x2apic_max_apicid = apicid;
21 }
22
23 static int __init set_x2apic_phys_mode(char *arg)
24 {
25         x2apic_phys = 1;
26         return 0;
27 }
28 early_param("x2apic_phys", set_x2apic_phys_mode);
29
30 static bool x2apic_fadt_phys(void)
31 {
32 #ifdef CONFIG_ACPI
33         if ((acpi_gbl_FADT.header.revision >= FADT2_REVISION_ID) &&
34                 (acpi_gbl_FADT.flags & ACPI_FADT_APIC_PHYSICAL)) {
35                 printk(KERN_DEBUG "System requires x2apic physical mode\n");
36                 return true;
37         }
38 #endif
39         return false;
40 }
41
42 static int x2apic_acpi_madt_oem_check(char *oem_id, char *oem_table_id)
43 {
44         return x2apic_enabled() && (x2apic_phys || x2apic_fadt_phys());
45 }
46
47 static void x2apic_send_IPI(int cpu, int vector)
48 {
49         u32 dest = per_cpu(x86_cpu_to_apicid, cpu);
50
51         /* x2apic MSRs are special and need a special fence: */
52         weak_wrmsr_fence();
53         __x2apic_send_IPI_dest(dest, vector, APIC_DEST_PHYSICAL);
54 }
55
56 static void
57 __x2apic_send_IPI_mask(const struct cpumask *mask, int vector, int apic_dest)
58 {
59         unsigned long query_cpu;
60         unsigned long this_cpu;
61         unsigned long flags;
62
63         /* x2apic MSRs are special and need a special fence: */
64         weak_wrmsr_fence();
65
66         local_irq_save(flags);
67
68         this_cpu = smp_processor_id();
69         for_each_cpu(query_cpu, mask) {
70                 if (apic_dest == APIC_DEST_ALLBUT && this_cpu == query_cpu)
71                         continue;
72                 __x2apic_send_IPI_dest(per_cpu(x86_cpu_to_apicid, query_cpu),
73                                        vector, APIC_DEST_PHYSICAL);
74         }
75         local_irq_restore(flags);
76 }
77
78 static void x2apic_send_IPI_mask(const struct cpumask *mask, int vector)
79 {
80         __x2apic_send_IPI_mask(mask, vector, APIC_DEST_ALLINC);
81 }
82
83 static void
84  x2apic_send_IPI_mask_allbutself(const struct cpumask *mask, int vector)
85 {
86         __x2apic_send_IPI_mask(mask, vector, APIC_DEST_ALLBUT);
87 }
88
89 static void x2apic_send_IPI_allbutself(int vector)
90 {
91         __x2apic_send_IPI_mask(cpu_online_mask, vector, APIC_DEST_ALLBUT);
92 }
93
94 static void x2apic_send_IPI_all(int vector)
95 {
96         __x2apic_send_IPI_mask(cpu_online_mask, vector, APIC_DEST_ALLINC);
97 }
98
99 static void init_x2apic_ldr(void)
100 {
101 }
102
103 static int x2apic_phys_probe(void)
104 {
105         if (x2apic_mode && (x2apic_phys || x2apic_fadt_phys()))
106                 return 1;
107
108         return apic == &apic_x2apic_phys;
109 }
110
111 /* Common x2apic functions, also used by x2apic_cluster */
112 int x2apic_apic_id_valid(u32 apicid)
113 {
114         if (x2apic_max_apicid && apicid > x2apic_max_apicid)
115                 return 0;
116
117         return 1;
118 }
119
120 int x2apic_apic_id_registered(void)
121 {
122         return 1;
123 }
124
125 void __x2apic_send_IPI_dest(unsigned int apicid, int vector, unsigned int dest)
126 {
127         unsigned long cfg = __prepare_ICR(0, vector, dest);
128         native_x2apic_icr_write(cfg, apicid);
129 }
130
131 unsigned int x2apic_get_apic_id(unsigned long id)
132 {
133         return id;
134 }
135
136 u32 x2apic_set_apic_id(unsigned int id)
137 {
138         return id;
139 }
140
141 int x2apic_phys_pkg_id(int initial_apicid, int index_msb)
142 {
143         return initial_apicid >> index_msb;
144 }
145
146 void x2apic_send_IPI_self(int vector)
147 {
148         apic_write(APIC_SELF_IPI, vector);
149 }
150
151 static struct apic apic_x2apic_phys __ro_after_init = {
152
153         .name                           = "physical x2apic",
154         .probe                          = x2apic_phys_probe,
155         .acpi_madt_oem_check            = x2apic_acpi_madt_oem_check,
156         .apic_id_valid                  = x2apic_apic_id_valid,
157         .apic_id_registered             = x2apic_apic_id_registered,
158
159         .irq_delivery_mode              = dest_Fixed,
160         .irq_dest_mode                  = 0, /* physical */
161
162         .disable_esr                    = 0,
163         .dest_logical                   = 0,
164         .check_apicid_used              = NULL,
165
166         .init_apic_ldr                  = init_x2apic_ldr,
167
168         .ioapic_phys_id_map             = NULL,
169         .setup_apic_routing             = NULL,
170         .cpu_present_to_apicid          = default_cpu_present_to_apicid,
171         .apicid_to_cpu_present          = NULL,
172         .check_phys_apicid_present      = default_check_phys_apicid_present,
173         .phys_pkg_id                    = x2apic_phys_pkg_id,
174
175         .get_apic_id                    = x2apic_get_apic_id,
176         .set_apic_id                    = x2apic_set_apic_id,
177
178         .calc_dest_apicid               = apic_default_calc_apicid,
179
180         .send_IPI                       = x2apic_send_IPI,
181         .send_IPI_mask                  = x2apic_send_IPI_mask,
182         .send_IPI_mask_allbutself       = x2apic_send_IPI_mask_allbutself,
183         .send_IPI_allbutself            = x2apic_send_IPI_allbutself,
184         .send_IPI_all                   = x2apic_send_IPI_all,
185         .send_IPI_self                  = x2apic_send_IPI_self,
186
187         .inquire_remote_apic            = NULL,
188
189         .read                           = native_apic_msr_read,
190         .write                          = native_apic_msr_write,
191         .eoi_write                      = native_apic_msr_eoi_write,
192         .icr_read                       = native_x2apic_icr_read,
193         .icr_write                      = native_x2apic_icr_write,
194         .wait_icr_idle                  = native_x2apic_wait_icr_idle,
195         .safe_wait_icr_idle             = native_safe_x2apic_wait_icr_idle,
196 };
197
198 apic_driver(apic_x2apic_phys);