1 /*        $NetBSD: madt.c,v 1.6 2023/07/13 18:43:34 mrg Exp $         */
2 /*-
3  * Copyright (c) 2001 Doug Rabson
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * $FreeBSD: src/sys/ia64/acpica/madt.c,v 1.20 2007/03/22 18:16:42 jkim Exp $
28  */
29 
30 #include <sys/param.h>
31 #include <sys/reboot.h>
32 #include <sys/mutex.h>
33 
34 #include <machine/md_var.h>
35 #include <machine/sapicvar.h>
36 
37 #include <dev/acpi/acpica.h>
38 #include <dev/acpi/acpivar.h>
39 #include <actables.h>
40 
41 
42 extern uint64_t ia64_lapic_address;
43 
44 static void
print_entry(ACPI_SUBTABLE_HEADER * entry)45 print_entry(ACPI_SUBTABLE_HEADER *entry)
46 {
47 
48           switch (entry->Type) {
49           case ACPI_MADT_TYPE_INTERRUPT_OVERRIDE: {
50                     ACPI_MADT_INTERRUPT_OVERRIDE *iso =
51                         (ACPI_MADT_INTERRUPT_OVERRIDE *)entry;
52                     printf("\tInterrupt source override entry\n");
53                     printf("\t\tBus=%u, Source=%u, Irq=0x%x\n", iso->Bus,
54                         iso->SourceIrq, iso->GlobalIrq);
55                     break;
56           }
57 
58           case ACPI_MADT_TYPE_IO_APIC:
59                     printf("\tI/O APIC entry\n");
60                     break;
61 
62           case ACPI_MADT_TYPE_IO_SAPIC: {
63                     ACPI_MADT_IO_SAPIC *sapic = (ACPI_MADT_IO_SAPIC *)entry;
64                     printf("\tI/O SAPIC entry\n");
65                     printf("\t\tId=0x%x, InterruptBase=0x%x, Address=0x%lx\n",
66                         sapic->Id, sapic->GlobalIrqBase, sapic->Address);
67                     break;
68           }
69 
70           case ACPI_MADT_TYPE_LOCAL_APIC_NMI:
71                     printf("\tLocal APIC NMI entry\n");
72                     break;
73 
74           case ACPI_MADT_TYPE_LOCAL_APIC_OVERRIDE: {
75                     ACPI_MADT_LOCAL_APIC_OVERRIDE *lapic =
76                         (ACPI_MADT_LOCAL_APIC_OVERRIDE *)entry;
77                     printf("\tLocal APIC override entry\n");
78                     printf("\t\tLocal APIC address=0x%jx\n", lapic->Address);
79                     break;
80           }
81 
82           case ACPI_MADT_TYPE_LOCAL_SAPIC: {
83                     ACPI_MADT_LOCAL_SAPIC *sapic = (ACPI_MADT_LOCAL_SAPIC *)entry;
84                     printf("\tLocal SAPIC entry\n");
85                     printf("\t\tProcessorId=0x%x, Id=0x%x, Eid=0x%x",
86                         sapic->ProcessorId, sapic->Id, sapic->Eid);
87                     if (!(sapic->LapicFlags & ACPI_MADT_ENABLED))
88                               printf(" (disabled)");
89                     printf("\n");
90                     break;
91           }
92 
93           case ACPI_MADT_TYPE_NMI_SOURCE:
94                     printf("\tNMI entry\n");
95                     break;
96 
97           case ACPI_MADT_TYPE_INTERRUPT_SOURCE: {
98                     ACPI_MADT_INTERRUPT_SOURCE *pis =
99                         (ACPI_MADT_INTERRUPT_SOURCE *)entry;
100                     printf("\tPlatform interrupt entry\n");
101                     printf("\t\tPolarity=%u, TriggerMode=%u, Id=0x%x, "
102                         "Eid=0x%x, Vector=0x%x, Irq=%d\n",
103                         pis->IntiFlags & ACPI_MADT_POLARITY_MASK,
104                         (pis->IntiFlags & ACPI_MADT_TRIGGER_MASK) >> 2,
105                         pis->Id, pis->Eid, pis->IoSapicVector, pis->GlobalIrq);
106                     break;
107           }
108 
109           case ACPI_MADT_TYPE_LOCAL_APIC:
110                     printf("\tLocal APIC entry\n");
111                     break;
112 
113           default:
114                     printf("\tUnknown type %d entry\n", entry->Type);
115                     break;
116           }
117 }
118 
119 void
ia64_probe_sapics(void)120 ia64_probe_sapics(void)
121 {
122           ACPI_PHYSICAL_ADDRESS rsdp_ptr;
123           ACPI_SUBTABLE_HEADER *entry;
124           ACPI_TABLE_MADT *table;
125           ACPI_TABLE_RSDP *rsdp;
126           ACPI_TABLE_XSDT *xsdt;
127           char *end, *p;
128           int t, tables;
129           extern int has_i8259;
130 
131           if ((rsdp_ptr = AcpiOsGetRootPointer()) == 0)
132                     return;
133 
134           rsdp = (ACPI_TABLE_RSDP *)IA64_PHYS_TO_RR7(rsdp_ptr);
135           xsdt = (ACPI_TABLE_XSDT *)IA64_PHYS_TO_RR7(rsdp->XsdtPhysicalAddress);
136 
137           tables = (UINT64 *)((char *)xsdt + xsdt->Header.Length) -
138               xsdt->TableOffsetEntry;
139 
140           for (t = 0; t < tables; t++) {
141                     table = (ACPI_TABLE_MADT *)
142                         IA64_PHYS_TO_RR7(xsdt->TableOffsetEntry[t]);
143 
144                     if (bootverbose)
145                               printf("Table '%c%c%c%c' at %p\n",
146                                   table->Header.Signature[0],
147                                   table->Header.Signature[1],
148                                   table->Header.Signature[2],
149                                   table->Header.Signature[3], table);
150 
151                     if (strncmp(table->Header.Signature, ACPI_SIG_MADT,
152                         ACPI_NAMESEG_SIZE) != 0 ||
153                         ACPI_FAILURE(AcpiUtChecksum((void *)table,
154                         table->Header.Length)))
155                               continue;
156 
157                     /* Save the address of the processor interrupt block. */
158                     if (bootverbose)
159                               printf("\tLocal APIC address=0x%x\n", table->Address);
160                     ia64_lapic_address = table->Address;
161 
162                     if (table->Flags & ACPI_MADT_PCAT_COMPAT)
163                               has_i8259 = 1;
164 
165                     end = (char *)table + table->Header.Length;
166                     p = (char *)(table + 1);
167                     while (p < end) {
168                               entry = (ACPI_SUBTABLE_HEADER *)p;
169 
170                               if (bootverbose)
171                                         print_entry(entry);
172 
173                               switch (entry->Type) {
174                               case ACPI_MADT_TYPE_IO_SAPIC:
175                               {
176                                         ACPI_MADT_IO_SAPIC *sapic =
177                                             (ACPI_MADT_IO_SAPIC *)entry;
178 
179                                         sapic_create(sapic->Id, sapic->GlobalIrqBase,
180                                             sapic->Address);
181                                         break;
182                               }
183 
184                               case ACPI_MADT_TYPE_LOCAL_APIC_OVERRIDE:
185                               {
186                                         ACPI_MADT_LOCAL_APIC_OVERRIDE *lapic =
187                                             (ACPI_MADT_LOCAL_APIC_OVERRIDE *)entry;
188 
189                                         ia64_lapic_address = lapic->Address;
190                                         break;
191                               }
192 
193 #ifdef MULTIPROCESSOR
194                               case ACPI_MADT_TYPE_LOCAL_SAPIC:
195                               {
196                                         ACPI_MADT_LOCAL_SAPIC *sapic =
197                                             (ACPI_MADT_LOCAL_SAPIC *)entry;
198 
199                                         if (sapic->LapicFlags & ACPI_MADT_ENABLED)
200                                                   cpu_mp_add(sapic->ProcessorId,
201                                                       sapic->Id, sapic->Eid);
202                                         break;
203                               }
204 #endif
205 
206                               default:
207                                         break;
208                               }
209 
210                               p += entry->Length;
211                     }
212           }
213 }
214 
215 /*
216  * Count the number of local SAPIC entries in the APIC table. Every enabled
217  * entry corresponds to a processor.
218  */
219 int
ia64_count_cpus(void)220 ia64_count_cpus(void)
221 {
222           ACPI_PHYSICAL_ADDRESS rsdp_ptr;
223           ACPI_MADT_LOCAL_SAPIC *entry;
224           ACPI_TABLE_MADT *table;
225           ACPI_TABLE_RSDP *rsdp;
226           ACPI_TABLE_XSDT *xsdt;
227           char *end, *p;
228           int cpus, t, tables;
229 
230           if ((rsdp_ptr = AcpiOsGetRootPointer()) == 0)
231                     return 0;
232 
233           rsdp = (ACPI_TABLE_RSDP *)IA64_PHYS_TO_RR7(rsdp_ptr);
234           xsdt = (ACPI_TABLE_XSDT *)IA64_PHYS_TO_RR7(rsdp->XsdtPhysicalAddress);
235 
236           tables = (UINT64 *)((char *)xsdt + xsdt->Header.Length) -
237               xsdt->TableOffsetEntry;
238 
239           cpus = 0;
240 
241           for (t = 0; t < tables; t++) {
242                     table = (ACPI_TABLE_MADT *)
243                         IA64_PHYS_TO_RR7(xsdt->TableOffsetEntry[t]);
244 
245                     if (strncmp(table->Header.Signature, ACPI_SIG_MADT,
246                         ACPI_NAMESEG_SIZE) != 0 ||
247                         ACPI_FAILURE(AcpiUtChecksum((void *)table,
248                               table->Header.Length)))
249                               continue;
250 
251                     end = (char *)table + table->Header.Length;
252                     p = (char *)(table + 1);
253                     while (p < end) {
254                               entry = (ACPI_MADT_LOCAL_SAPIC *)p;
255 
256                               if (entry->Header.Type == ACPI_MADT_TYPE_LOCAL_SAPIC &&
257                                   (entry->LapicFlags & ACPI_MADT_ENABLED))
258                                         cpus++;
259 
260                               p += entry->Header.Length;
261                     }
262           }
263 
264           return cpus;
265 }
266