ACPI MADT: Add madt_iterate_entries()
[dragonfly.git] / sys / platform / pc64 / x86_64 / mp_madt.c
1 /*
2  * Copyright (c) 2009 The DragonFly Project.  All rights reserved.
3  * 
4  * This code is derived from software contributed to The DragonFly Project
5  * by Sepherosa Ziehau <sepherosa@gmail.com>
6  * 
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  * 
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37
38 #include <machine/pmap.h>
39 #include <machine/smp.h>
40
41 #define ACPI_RSDP_EBDA_MAPSZ    1024
42 #define ACPI_RSDP_BIOS_MAPSZ    0x20000
43 #define ACPI_RSDP_BIOS_MAPADDR  0xe0000
44
45 #define ACPI_RSDP_ALIGN         16
46
47 #define ACPI_RSDP_SIGLEN        8
48 #define ACPI_RSDP_SIG           "RSD PTR "
49
50 #define ACPI_SDTH_SIGLEN        4
51 #define ACPI_RSDT_SIG           "RSDT"
52 #define ACPI_XSDT_SIG           "XSDT"
53 #define ACPI_MADT_SIG           "APIC"
54
55 /* Root System Description Pointer */
56 struct acpi_rsdp {
57         uint8_t                 rsdp_sig[ACPI_RSDP_SIGLEN];
58         uint8_t                 rsdp_cksum;
59         uint8_t                 rsdp_oem_id[6];
60         uint8_t                 rsdp_rev;
61         uint32_t                rsdp_rsdt;
62         uint32_t                rsdp_len;
63         uint64_t                rsdp_xsdt;
64         uint8_t                 rsdp_ext_cksum;
65         uint8_t                 rsdp_rsvd[3];
66 } __packed;
67
68 /* System Description Table Header */
69 struct acpi_sdth {
70         uint8_t                 sdth_sig[ACPI_SDTH_SIGLEN];
71         uint32_t                sdth_len;
72         uint8_t                 sdth_rev;
73         uint8_t                 sdth_cksum;
74         uint8_t                 sdth_oem_id[6];
75         uint8_t                 sdth_oem_tbid[8];
76         uint32_t                sdth_oem_rev;
77         uint32_t                sdth_crt_id;
78         uint32_t                sdth_crt_rev;
79 } __packed;
80
81 /* Extended System Description Table */
82 struct acpi_xsdt {
83         struct acpi_sdth        xsdt_hdr;
84         uint64_t                xsdt_ents[1];
85 } __packed;
86
87 /* Root System Description Table */
88 struct acpi_rsdt {
89         struct acpi_sdth        rsdt_hdr;
90         uint32_t                rsdt_ents[1];
91 } __packed;
92
93 /* Multiple APIC Description Table */
94 struct acpi_madt {
95         struct acpi_sdth        madt_hdr;
96         uint32_t                madt_lapic_addr;
97         uint32_t                madt_flags;
98         uint8_t                 madt_ents[1];
99 } __packed;
100
101 /* Common parts of MADT APIC structure */
102 struct acpi_madt_ent {
103         uint8_t                 me_type;        /* MADT_ENT_ */
104         uint8_t                 me_len;
105 } __packed;
106
107 #define MADT_ENT_LAPIC          0
108 #define MADT_ENT_IOAPIC         1
109
110 /* MADT Processor Local APIC */
111 struct acpi_madt_lapic {
112         struct acpi_madt_ent    ml_hdr;
113         uint8_t                 ml_cpu_id;
114         uint8_t                 ml_apic_id;
115         uint32_t                ml_flags;       /* MADT_LAPIC_ */
116 } __packed;
117
118 #define MADT_LAPIC_ENABLED      0x1
119
120 /* MADT I/O APIC */
121 struct acpi_madt_ioapic {
122         struct acpi_madt_ent    mio_hdr;
123         uint8_t                 mio_apic_id;
124         uint8_t                 mio_reserved;
125         uint32_t                mio_addr;
126         uint32_t                mio_gsi_base;
127 } __packed;
128
129 typedef vm_paddr_t              (*madt_search_t)(vm_paddr_t);
130 typedef int                     (*madt_iter_t)(void *,
131                                     const struct acpi_madt_ent *);
132
133 static const struct acpi_rsdp   *madt_rsdp_search(const uint8_t *, int);
134 static void                     *madt_sdth_map(vm_paddr_t);
135 static void                     madt_sdth_unmap(struct acpi_sdth *);
136 static vm_paddr_t               madt_search_xsdt(vm_paddr_t);
137 static vm_paddr_t               madt_search_rsdt(vm_paddr_t);
138 static int                      madt_check(vm_paddr_t);
139 static int                      madt_iterate_entries(struct acpi_madt *,
140                                     madt_iter_t, void *);
141
142 extern u_long   ebda_addr;
143
144 vm_paddr_t
145 madt_probe(void)
146 {
147         const struct acpi_rsdp *rsdp;
148         madt_search_t search;
149         vm_paddr_t search_paddr, madt_paddr;
150         vm_size_t mapsz;
151         uint8_t *ptr;
152
153         if (ebda_addr != 0) {
154                 mapsz = ACPI_RSDP_EBDA_MAPSZ;
155                 ptr = pmap_mapdev(ebda_addr, mapsz);
156
157                 rsdp = madt_rsdp_search(ptr, mapsz);
158                 if (rsdp == NULL) {
159                         kprintf("madt: RSDP not in EBDA\n");
160                         pmap_unmapdev((vm_offset_t)ptr, mapsz);
161
162                         ptr = NULL;
163                         mapsz = 0;
164                 } else {
165                         kprintf("madt: RSDP in EBDA\n");
166                         goto found_rsdp;
167                 }
168         }
169
170         mapsz = ACPI_RSDP_BIOS_MAPSZ;
171         ptr = pmap_mapdev(ACPI_RSDP_BIOS_MAPADDR, mapsz);
172
173         rsdp = madt_rsdp_search(ptr, mapsz);
174         if (rsdp == NULL) {
175                 kprintf("madt_probe: no RSDP\n");
176                 pmap_unmapdev((vm_offset_t)ptr, mapsz);
177                 return 0;
178         } else {
179                 kprintf("madt: RSDP in BIOS mem\n");
180         }
181
182 found_rsdp:
183         if (rsdp->rsdp_rev != 2) {
184                 search_paddr = rsdp->rsdp_rsdt;
185                 search = madt_search_rsdt;
186         } else {
187                 search_paddr = rsdp->rsdp_xsdt;
188                 search = madt_search_xsdt;
189         }
190         pmap_unmapdev((vm_offset_t)ptr, mapsz);
191
192         madt_paddr = search(search_paddr);
193         if (madt_paddr == 0) {
194                 kprintf("madt_probe: can't locate MADT\n");
195                 return 0;
196         }
197
198         /* Preliminary checks */
199         if (madt_check(madt_paddr))
200                 return 0;
201         return madt_paddr;
202 }
203
204 static const struct acpi_rsdp *
205 madt_rsdp_search(const uint8_t *target, int size)
206 {
207         const struct acpi_rsdp *rsdp;
208         int i;
209
210         KKASSERT(size > sizeof(*rsdp));
211
212         for (i = 0; i < size - sizeof(*rsdp); i += ACPI_RSDP_ALIGN) {
213                 rsdp = (const struct acpi_rsdp *)&target[i];
214                 if (memcmp(rsdp->rsdp_sig, ACPI_RSDP_SIG,
215                            ACPI_RSDP_SIGLEN) == 0)
216                         return rsdp;
217         }
218         return NULL;
219 }
220
221 static void *
222 madt_sdth_map(vm_paddr_t paddr)
223 {
224         struct acpi_sdth *sdth;
225         vm_size_t mapsz;
226
227         sdth = pmap_mapdev(paddr, sizeof(*sdth));
228         mapsz = sdth->sdth_len;
229         pmap_unmapdev((vm_offset_t)sdth, sizeof(*sdth));
230
231         if (mapsz < sizeof(*sdth))
232                 return NULL;
233
234         return pmap_mapdev(paddr, mapsz);
235 }
236
237 static void
238 madt_sdth_unmap(struct acpi_sdth *sdth)
239 {
240         pmap_unmapdev((vm_offset_t)sdth, sdth->sdth_len);
241 }
242
243 static vm_paddr_t
244 madt_search_xsdt(vm_paddr_t xsdt_paddr)
245 {
246         struct acpi_xsdt *xsdt;
247         vm_paddr_t madt_paddr = 0;
248         int i, nent;
249
250         if (xsdt_paddr == 0) {
251                 kprintf("madt_search_xsdt: XSDT paddr == 0\n");
252                 return 0;
253         }
254
255         xsdt = madt_sdth_map(xsdt_paddr);
256         if (xsdt == NULL) {
257                 kprintf("madt_search_xsdt: can't map XSDT\n");
258                 return 0;
259         }
260
261         if (memcmp(xsdt->xsdt_hdr.sdth_sig, ACPI_XSDT_SIG,
262                    ACPI_SDTH_SIGLEN) != 0) {
263                 kprintf("madt_search_xsdt: not XSDT\n");
264                 goto back;
265         }
266
267         if (xsdt->xsdt_hdr.sdth_rev != 1) {
268                 kprintf("madt_search_xsdt: unsupported XSDT revision %d\n",
269                         xsdt->xsdt_hdr.sdth_rev);
270                 goto back;
271         }
272
273         nent = (xsdt->xsdt_hdr.sdth_len - sizeof(xsdt->xsdt_hdr)) /
274                sizeof(xsdt->xsdt_ents[0]);
275         for (i = 0; i < nent; ++i) {
276                 struct acpi_sdth *sdth;
277
278                 if (xsdt->xsdt_ents[i] == 0)
279                         continue;
280
281                 sdth = madt_sdth_map(xsdt->xsdt_ents[i]);
282                 if (sdth != NULL) {
283                         int ret;
284
285                         ret = memcmp(sdth->sdth_sig, ACPI_MADT_SIG,
286                                      ACPI_SDTH_SIGLEN);
287                         madt_sdth_unmap(sdth);
288
289                         if (ret == 0) {
290                                 kprintf("madt: MADT in XSDT\n");
291                                 madt_paddr = xsdt->xsdt_ents[i];
292                                 break;
293                         }
294                 }
295         }
296 back:
297         madt_sdth_unmap(&xsdt->xsdt_hdr);
298         return madt_paddr;
299 }
300
301 static vm_paddr_t
302 madt_search_rsdt(vm_paddr_t rsdt_paddr)
303 {
304         struct acpi_rsdt *rsdt;
305         vm_paddr_t madt_paddr = 0;
306         int i, nent;
307
308         if (rsdt_paddr == 0) {
309                 kprintf("madt_search_rsdt: RSDT paddr == 0\n");
310                 return 0;
311         }
312
313         rsdt = madt_sdth_map(rsdt_paddr);
314         if (rsdt == NULL) {
315                 kprintf("madt_search_rsdt: can't map RSDT\n");
316                 return 0;
317         }
318
319         if (memcmp(rsdt->rsdt_hdr.sdth_sig, ACPI_RSDT_SIG,
320                    ACPI_SDTH_SIGLEN) != 0) {
321                 kprintf("madt_search_rsdt: not RSDT\n");
322                 goto back;
323         }
324
325         if (rsdt->rsdt_hdr.sdth_rev != 1) {
326                 kprintf("madt_search_rsdt: unsupported RSDT revision %d\n",
327                         rsdt->rsdt_hdr.sdth_rev);
328                 goto back;
329         }
330
331         nent = (rsdt->rsdt_hdr.sdth_len - sizeof(rsdt->rsdt_hdr)) /
332                sizeof(rsdt->rsdt_ents[0]);
333         for (i = 0; i < nent; ++i) {
334                 struct acpi_sdth *sdth;
335
336                 if (rsdt->rsdt_ents[i] == 0)
337                         continue;
338
339                 sdth = madt_sdth_map(rsdt->rsdt_ents[i]);
340                 if (sdth != NULL) {
341                         int ret;
342
343                         ret = memcmp(sdth->sdth_sig, ACPI_MADT_SIG,
344                                      ACPI_SDTH_SIGLEN);
345                         madt_sdth_unmap(sdth);
346
347                         if (ret == 0) {
348                                 kprintf("madt: MADT in RSDT\n");
349                                 madt_paddr = rsdt->rsdt_ents[i];
350                                 break;
351                         }
352                 }
353         }
354 back:
355         madt_sdth_unmap(&rsdt->rsdt_hdr);
356         return madt_paddr;
357 }
358
359 vm_offset_t
360 madt_pass1(vm_paddr_t madt_paddr)
361 {
362         struct acpi_madt *madt;
363         vm_offset_t lapic_addr;
364
365         KKASSERT(madt_paddr != 0);
366
367         madt = madt_sdth_map(madt_paddr);
368         KKASSERT(madt != NULL);
369
370         kprintf("madt: LAPIC address 0x%08x, flags %#x\n",
371                 madt->madt_lapic_addr, madt->madt_flags);
372         lapic_addr = madt->madt_lapic_addr;
373
374         madt_sdth_unmap(&madt->madt_hdr);
375
376         return lapic_addr;
377 }
378
379 int
380 madt_pass2(vm_paddr_t madt_paddr, int bsp_apic_id)
381 {
382         struct acpi_madt *madt;
383         int size, cur, error, cpu, found_bsp;
384
385         kprintf("madt: BSP apic id %d\n", bsp_apic_id);
386
387         KKASSERT(madt_paddr != 0);
388
389         madt = madt_sdth_map(madt_paddr);
390         KKASSERT(madt != NULL);
391
392         size = madt->madt_hdr.sdth_len -
393                (sizeof(*madt) - sizeof(madt->madt_ents));
394         cur = 0;
395         error = 0;
396         found_bsp = 0;
397         cpu = 1;
398
399         while (size - cur > sizeof(struct acpi_madt_ent)) {
400                 const struct acpi_madt_ent *ent;
401
402                 ent = (const struct acpi_madt_ent *)&madt->madt_ents[cur];
403                 if (ent->me_len < sizeof(*ent)) {
404                         kprintf("madt_pass2: invalid MADT entry len %d\n",
405                                 ent->me_len);
406                         error = EINVAL;
407                         break;
408                 }
409                 if (ent->me_len > (size - cur)) {
410                         kprintf("madt_pass2: invalid MADT entry len %d, "
411                                 "> table length\n", ent->me_len);
412                         error = EINVAL;
413                         break;
414                 }
415
416                 cur += ent->me_len;
417
418                 if (ent->me_type == MADT_ENT_LAPIC) {
419                         const struct acpi_madt_lapic *lapic_ent;
420
421                         if (ent->me_len < sizeof(*lapic_ent)) {
422                                 kprintf("madt_pass2: invalid MADT lapic entry "
423                                         "len %d\n", ent->me_len);
424                                 error = EINVAL;
425                                 break;
426                         }
427                         lapic_ent = (const struct acpi_madt_lapic *)ent;
428                         if (lapic_ent->ml_flags & MADT_LAPIC_ENABLED) {
429                                 kprintf("madt: cpu_id %d, apic_id %d\n",
430                                         lapic_ent->ml_cpu_id,
431                                         lapic_ent->ml_apic_id);
432                                 if (lapic_ent->ml_apic_id == bsp_apic_id) {
433                                         mp_set_cpuids(0,
434                                         lapic_ent->ml_apic_id);
435                                         found_bsp = 1;
436                                 } else {
437                                         mp_set_cpuids(cpu,
438                                         lapic_ent->ml_apic_id);
439                                         ++cpu;
440                                 }
441                         }
442                 }
443         }
444         if (!found_bsp) {
445                 kprintf("madt_pass2: BSP is not found\n");
446                 error = EINVAL;
447         }
448         if (cpu == 1) {
449                 kprintf("madt_pass2: no APs\n");
450                 error = EINVAL;
451         }
452         if (!error)
453                 mp_naps = cpu - 1;
454
455         madt_sdth_unmap(&madt->madt_hdr);
456         return error;
457 }
458
459 struct madt_check_cbarg {
460         int     cpu_count;
461 };
462
463 static int
464 madt_check_callback(void *xarg, const struct acpi_madt_ent *ent)
465 {
466         struct madt_check_cbarg *arg = xarg;
467         const struct acpi_madt_lapic *lapic_ent;
468
469         if (ent->me_type != MADT_ENT_LAPIC)
470                 return 0;
471         lapic_ent = (const struct acpi_madt_lapic *)ent;
472
473         if (lapic_ent->ml_flags & MADT_LAPIC_ENABLED)
474                 arg->cpu_count++;
475         return 0;
476 }
477
478 static int
479 madt_check(vm_paddr_t madt_paddr)
480 {
481         struct madt_check_cbarg arg;
482         struct acpi_madt *madt;
483         int error = 0;
484
485         KKASSERT(madt_paddr != 0);
486
487         madt = madt_sdth_map(madt_paddr);
488         KKASSERT(madt != NULL);
489
490         if (madt->madt_hdr.sdth_rev != 1 && madt->madt_hdr.sdth_rev != 2) {
491                 kprintf("madt_check: unsupported MADT revision %d\n",
492                         madt->madt_hdr.sdth_rev);
493                 error = EOPNOTSUPP;
494                 goto back;
495         }
496
497         if (madt->madt_hdr.sdth_len <
498             sizeof(*madt) - sizeof(madt->madt_ents)) {
499                 kprintf("madt_check: invalid MADT length %u\n",
500                         madt->madt_hdr.sdth_len);
501                 error = EINVAL;
502                 goto back;
503         }
504
505         bzero(&arg, sizeof(arg));
506         error = madt_iterate_entries(madt, madt_check_callback, &arg);
507         if (!error) {
508                 if (arg.cpu_count <= 1)
509                         error = EOPNOTSUPP;
510         }
511 back:
512         madt_sdth_unmap(&madt->madt_hdr);
513         return error;
514 }
515
516 static int
517 madt_iterate_entries(struct acpi_madt *madt, madt_iter_t func, void *arg)
518 {
519         int size, cur, error;
520
521         size = madt->madt_hdr.sdth_len -
522                (sizeof(*madt) - sizeof(madt->madt_ents));
523         cur = 0;
524         error = 0;
525
526         while (size - cur > sizeof(struct acpi_madt_ent)) {
527                 const struct acpi_madt_ent *ent;
528
529                 ent = (const struct acpi_madt_ent *)&madt->madt_ents[cur];
530                 if (ent->me_len < sizeof(*ent)) {
531                         kprintf("madt_iterate_entries: invalid MADT "
532                                 "entry len %d\n", ent->me_len);
533                         error = EINVAL;
534                         break;
535                 }
536                 if (ent->me_len > (size - cur)) {
537                         kprintf("madt_iterate_entries: invalid MADT "
538                                 "entry len %d, > table length\n", ent->me_len);
539                         error = EINVAL;
540                         break;
541                 }
542
543                 cur += ent->me_len;
544
545                 /*
546                  * Only Local APIC and I/O APIC are defined in
547                  * ACPI specification 1.0 - 3.0
548                  */
549                 switch (ent->me_type) {
550                 case MADT_ENT_LAPIC:
551                         if (ent->me_len < sizeof(struct acpi_madt_lapic)) {
552                                 kprintf("madt_iterate_entries: invalid MADT "
553                                         "lapic entry len %d\n", ent->me_len);
554                                 error = EINVAL;
555                         }
556                         break;
557
558                 case MADT_ENT_IOAPIC:
559                         if (ent->me_len < sizeof(struct acpi_madt_ioapic)) {
560                                 kprintf("madt_iterate_entries: invalid MADT "
561                                         "ioapic entry len %d\n", ent->me_len);
562                                 error = EINVAL;
563                         }
564                         break;
565                 }
566                 if (error)
567                         break;
568
569                 error = func(arg, ent);
570                 if (error)
571                         break;
572         }
573         return error;
574 }
575
576