Home | History | Annotate | Line # | Download | only in acpi
acpi_cpu.c revision 1.19
      1 /* $NetBSD: acpi_cpu.c,v 1.19 2010/08/17 10:17:52 jruoho Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2010 Jukka Ruohonen <jruohonen (at) iki.fi>
      5  * All rights reserved.
      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 the
     15  *    documentation and/or other materials provided with the distribution.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     27  * SUCH DAMAGE.
     28  */
     29 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: acpi_cpu.c,v 1.19 2010/08/17 10:17:52 jruoho Exp $");
     31 
     32 #include <sys/param.h>
     33 #include <sys/cpu.h>
     34 #include <sys/kernel.h>
     35 #include <sys/kmem.h>
     36 #include <sys/module.h>
     37 #include <sys/mutex.h>
     38 #include <sys/once.h>
     39 
     40 #include <dev/acpi/acpireg.h>
     41 #include <dev/acpi/acpivar.h>
     42 #include <dev/acpi/acpi_cpu.h>
     43 
     44 #include <machine/acpi_machdep.h>
     45 
     46 #define _COMPONENT	  ACPI_BUS_COMPONENT
     47 ACPI_MODULE_NAME	  ("acpi_cpu")
     48 
     49 static int		  acpicpu_match(device_t, cfdata_t, void *);
     50 static void		  acpicpu_attach(device_t, device_t, void *);
     51 static int		  acpicpu_detach(device_t, int);
     52 static int		  acpicpu_once_attach(void);
     53 static int		  acpicpu_once_detach(void);
     54 static void		  acpicpu_start(device_t);
     55 
     56 static int		  acpicpu_object(ACPI_HANDLE, struct acpicpu_object *);
     57 static cpuid_t		  acpicpu_id(uint32_t);
     58 static uint32_t		  acpicpu_cap(struct acpicpu_softc *);
     59 static ACPI_OBJECT	 *acpicpu_cap_init(void);
     60 static ACPI_STATUS	  acpicpu_cap_pdc(ACPI_HANDLE);
     61 static ACPI_STATUS	  acpicpu_cap_osc(ACPI_HANDLE, uint32_t *);
     62 static const char	 *acpicpu_cap_oscerr(uint32_t);
     63 static void		  acpicpu_notify(ACPI_HANDLE, uint32_t, void *);
     64 static bool		  acpicpu_suspend(device_t, const pmf_qual_t *);
     65 static bool		  acpicpu_resume(device_t, const pmf_qual_t *);
     66 
     67 struct acpicpu_softc	**acpicpu_sc = NULL;
     68 
     69 static const char * const acpicpu_hid[] = {
     70 	"ACPI0007",
     71 	NULL
     72 };
     73 
     74 CFATTACH_DECL_NEW(acpicpu, sizeof(struct acpicpu_softc),
     75     acpicpu_match, acpicpu_attach, acpicpu_detach, NULL);
     76 
     77 static int
     78 acpicpu_match(device_t parent, cfdata_t match, void *aux)
     79 {
     80 	struct acpi_attach_args *aa = aux;
     81 	struct acpicpu_object ao;
     82 	int rv;
     83 
     84 	if (aa->aa_node->ad_type != ACPI_TYPE_PROCESSOR)
     85 		return 0;
     86 
     87 	if (acpi_match_hid(aa->aa_node->ad_devinfo, acpicpu_hid) != 0)
     88 		return 1;
     89 
     90 	rv = acpicpu_object(aa->aa_node->ad_handle, &ao);
     91 
     92 	if (rv != 0 || acpicpu_id(ao.ao_procid) == 0xFFFFFF)
     93 		return 0;
     94 
     95 	return 1;
     96 }
     97 
     98 static void
     99 acpicpu_attach(device_t parent, device_t self, void *aux)
    100 {
    101 	struct acpicpu_softc *sc = device_private(self);
    102 	struct acpi_attach_args *aa = aux;
    103 	static ONCE_DECL(once_attach);
    104 	int rv;
    105 
    106 	rv = acpicpu_object(aa->aa_node->ad_handle, &sc->sc_object);
    107 
    108 	if (rv != 0)
    109 		return;
    110 
    111 	rv = RUN_ONCE(&once_attach, acpicpu_once_attach);
    112 
    113 	if (rv != 0)
    114 		return;
    115 
    116 	sc->sc_dev = self;
    117 	sc->sc_cold = true;
    118 	sc->sc_mapped = false;
    119 	sc->sc_passive = false;
    120 	sc->sc_iot = aa->aa_iot;
    121 	sc->sc_node = aa->aa_node;
    122 	sc->sc_cpuid = acpicpu_id(sc->sc_object.ao_procid);
    123 
    124 	if (sc->sc_cpuid == 0xFFFFFF) {
    125 		aprint_error(": invalid CPU ID\n");
    126 		return;
    127 	}
    128 
    129 	if (acpicpu_sc[sc->sc_cpuid] != NULL) {
    130 		aprint_error(": already attached\n");
    131 		return;
    132 	}
    133 
    134 	acpicpu_sc[sc->sc_cpuid] = sc;
    135 
    136 	sc->sc_cap = acpicpu_cap(sc);
    137 	sc->sc_flags |= acpicpu_md_quirks();
    138 
    139 	mutex_init(&sc->sc_mtx, MUTEX_DEFAULT, IPL_NONE);
    140 
    141 	aprint_naive("\n");
    142 	aprint_normal(": ACPI CPU\n");
    143 
    144 	/*
    145 	 * We should claim the bus space. However, we do this only
    146 	 * to announce that the space is in use. As is noted in
    147 	 * ichlpcib(4), we can continue our I/O without bus_space(9).
    148 	 */
    149 	if (sc->sc_object.ao_pblklen == 6 && sc->sc_object.ao_pblkaddr != 0) {
    150 
    151 		rv = bus_space_map(sc->sc_iot, sc->sc_object.ao_pblkaddr,
    152 		    sc->sc_object.ao_pblklen, 0, &sc->sc_ioh);
    153 
    154 		if (rv == 0)
    155 			sc->sc_mapped = true;
    156 	}
    157 
    158 	acpicpu_cstate_attach(self);
    159 	acpicpu_pstate_attach(self);
    160 	acpicpu_tstate_attach(self);
    161 
    162 	(void)config_defer(self, acpicpu_start);
    163 	(void)acpi_register_notify(sc->sc_node, acpicpu_notify);
    164 	(void)pmf_device_register(self, acpicpu_suspend, acpicpu_resume);
    165 }
    166 
    167 static int
    168 acpicpu_detach(device_t self, int flags)
    169 {
    170 	struct acpicpu_softc *sc = device_private(self);
    171 	const bus_addr_t addr = sc->sc_object.ao_pblkaddr;
    172 	static ONCE_DECL(once_detach);
    173 	int rv = 0;
    174 
    175 	sc->sc_cold = true;
    176 	acpi_deregister_notify(sc->sc_node);
    177 
    178 	if ((sc->sc_flags & ACPICPU_FLAG_C) != 0)
    179 		rv = acpicpu_cstate_detach(self);
    180 
    181 	if (rv != 0)
    182 		return rv;
    183 
    184 	if ((sc->sc_flags & ACPICPU_FLAG_P) != 0)
    185 		rv = acpicpu_pstate_detach(self);
    186 
    187 	if (rv != 0)
    188 		return rv;
    189 
    190 	if ((sc->sc_flags & ACPICPU_FLAG_T) != 0)
    191 		rv = acpicpu_tstate_detach(self);
    192 
    193 	if (rv != 0)
    194 		return rv;
    195 
    196 	rv = RUN_ONCE(&once_detach, acpicpu_once_detach);
    197 
    198 	if (rv != 0)
    199 		return rv;
    200 
    201 	if (sc->sc_mapped != false)
    202 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, addr);
    203 
    204 	mutex_destroy(&sc->sc_mtx);
    205 
    206 	return 0;
    207 }
    208 
    209 static int
    210 acpicpu_once_attach(void)
    211 {
    212 	struct acpicpu_softc *sc;
    213 	unsigned int i;
    214 
    215 	acpicpu_sc = kmem_zalloc(maxcpus * sizeof(*sc), KM_SLEEP);
    216 
    217 	if (acpicpu_sc == NULL)
    218 		return ENOMEM;
    219 
    220 	for (i = 0; i < maxcpus; i++)
    221 		acpicpu_sc[i] = NULL;
    222 
    223 	return 0;
    224 }
    225 
    226 static int
    227 acpicpu_once_detach(void)
    228 {
    229 	struct acpicpu_softc *sc;
    230 
    231 	if (acpicpu_sc != NULL)
    232 		kmem_free(acpicpu_sc, maxcpus * sizeof(*sc));
    233 
    234 	return 0;
    235 }
    236 
    237 static void
    238 acpicpu_start(device_t self)
    239 {
    240 	struct acpicpu_softc *sc = device_private(self);
    241 	static bool once = false;
    242 
    243 	if (once != false) {
    244 		sc->sc_cold = false;
    245 		return;
    246 	}
    247 
    248 	/*
    249 	 * Run the state-specific initialization
    250 	 * routines. These should be called only
    251 	 * once, after all ACPI CPUs have attached.
    252 	 */
    253 	if ((sc->sc_flags & ACPICPU_FLAG_C) != 0)
    254 		acpicpu_cstate_start(self);
    255 
    256 	if ((sc->sc_flags & ACPICPU_FLAG_P) != 0)
    257 		acpicpu_pstate_start(self);
    258 
    259 	if ((sc->sc_flags & ACPICPU_FLAG_T) != 0)
    260 		acpicpu_tstate_start(self);
    261 
    262 	aprint_debug_dev(sc->sc_dev, "ACPI CPUs started (cap "
    263 	    "0x%02x, flags 0x%06x)\n", sc->sc_cap, sc->sc_flags);
    264 
    265 	once = true;
    266 	sc->sc_cold = false;
    267 }
    268 
    269 static int
    270 acpicpu_object(ACPI_HANDLE hdl, struct acpicpu_object *ao)
    271 {
    272 	ACPI_OBJECT *obj;
    273 	ACPI_BUFFER buf;
    274 	ACPI_STATUS rv;
    275 
    276 	rv = acpi_eval_struct(hdl, NULL, &buf);
    277 
    278 	if (ACPI_FAILURE(rv))
    279 		return 1;
    280 
    281 	obj = buf.Pointer;
    282 
    283 	if (obj->Type != ACPI_TYPE_PROCESSOR) {
    284 		rv = AE_TYPE;
    285 		goto out;
    286 	}
    287 
    288 	if (obj->Processor.ProcId > (uint32_t)maxcpus) {
    289 		rv = AE_LIMIT;
    290 		goto out;
    291 	}
    292 
    293 	KDASSERT((uint64_t)obj->Processor.PblkAddress < UINT32_MAX);
    294 
    295 	if (ao != NULL) {
    296 		ao->ao_procid = obj->Processor.ProcId;
    297 		ao->ao_pblklen = obj->Processor.PblkLength;
    298 		ao->ao_pblkaddr = obj->Processor.PblkAddress;
    299 	}
    300 
    301 out:
    302 	if (buf.Pointer != NULL)
    303 		ACPI_FREE(buf.Pointer);
    304 
    305 	return ACPI_FAILURE(rv) ? 1 : 0;
    306 }
    307 
    308 static cpuid_t
    309 acpicpu_id(uint32_t id)
    310 {
    311 	CPU_INFO_ITERATOR cii;
    312 	struct cpu_info *ci;
    313 
    314 	for (CPU_INFO_FOREACH(cii, ci)) {
    315 
    316 		if (id == ci->ci_acpiid)
    317 			return id;
    318 	}
    319 
    320 	return 0xFFFFFF;
    321 }
    322 
    323 static uint32_t
    324 acpicpu_cap(struct acpicpu_softc *sc)
    325 {
    326 	uint32_t cap[3] = { 0 };
    327 	ACPI_STATUS rv;
    328 	int err;
    329 
    330 	/*
    331 	 * Set machine-dependent processor capabilities.
    332 	 *
    333 	 * The _PDC was deprecated in ACPI 3.0 in favor of the _OSC,
    334 	 * but firmware may expect that we evaluate it nevertheless.
    335 	 */
    336 	rv = acpicpu_cap_pdc(sc->sc_node->ad_handle);
    337 
    338 	if (ACPI_FAILURE(rv) && rv != AE_NOT_FOUND)
    339 		aprint_error_dev(sc->sc_dev, "failed to evaluate _PDC: "
    340 		    "%s\n", AcpiFormatException(rv));
    341 
    342 	rv = acpicpu_cap_osc(sc->sc_node->ad_handle, cap);
    343 
    344 	if (ACPI_FAILURE(rv) && rv != AE_NOT_FOUND)
    345 		aprint_error_dev(sc->sc_dev, "failed to evaluate _OSC: "
    346 		    "%s\n", AcpiFormatException(rv));
    347 
    348 	if (ACPI_SUCCESS(rv)) {
    349 
    350 		err = cap[0] & ~__BIT(0);
    351 
    352 		if (err != 0) {
    353 			aprint_error_dev(sc->sc_dev, "errors in "
    354 			    "_OSC: %s\n", acpicpu_cap_oscerr(err));
    355 			cap[2] = 0;
    356 		}
    357 	}
    358 
    359 	return cap[2];
    360 }
    361 
    362 static ACPI_OBJECT *
    363 acpicpu_cap_init(void)
    364 {
    365 	static uint32_t cap[3];
    366 	static ACPI_OBJECT obj;
    367 
    368 	cap[0] = ACPICPU_PDC_REVID;
    369 	cap[1] = 1;
    370 	cap[2] = acpicpu_md_cap();
    371 
    372 	obj.Type = ACPI_TYPE_BUFFER;
    373 	obj.Buffer.Length = sizeof(cap);
    374 	obj.Buffer.Pointer = (uint8_t *)cap;
    375 
    376 	return &obj;
    377 }
    378 
    379 static ACPI_STATUS
    380 acpicpu_cap_pdc(ACPI_HANDLE hdl)
    381 {
    382 	ACPI_OBJECT_LIST arg_list;
    383 
    384 	arg_list.Count = 1;
    385 	arg_list.Pointer = acpicpu_cap_init();
    386 
    387 	return AcpiEvaluateObject(hdl, "_PDC", &arg_list, NULL);
    388 }
    389 
    390 static ACPI_STATUS
    391 acpicpu_cap_osc(ACPI_HANDLE hdl, uint32_t *val)
    392 {
    393 	ACPI_OBJECT_LIST arg_list;
    394 	ACPI_OBJECT *cap, *obj;
    395 	ACPI_OBJECT arg[4];
    396 	ACPI_BUFFER buf;
    397 	ACPI_STATUS rv;
    398 
    399 	/* Intel. */
    400 	static uint8_t cpu_oscuuid[16] = {
    401 		0x16, 0xA6, 0x77, 0x40, 0x0C, 0x29, 0xBE, 0x47,
    402 		0x9E, 0xBD, 0xD8, 0x70, 0x58, 0x71, 0x39, 0x53
    403 	};
    404 
    405 	cap = acpicpu_cap_init();
    406 
    407 	arg_list.Count = 4;
    408 	arg_list.Pointer = arg;
    409 
    410 	arg[0].Type = ACPI_TYPE_BUFFER;
    411 	arg[0].Buffer.Length = sizeof(cpu_oscuuid);
    412 	arg[0].Buffer.Pointer = cpu_oscuuid;
    413 
    414 	arg[1].Type = ACPI_TYPE_INTEGER;
    415 	arg[1].Integer.Value = ACPICPU_PDC_REVID;
    416 
    417 	arg[2].Type = ACPI_TYPE_INTEGER;
    418 	arg[2].Integer.Value = cap->Buffer.Length / sizeof(uint32_t);
    419 
    420 	arg[3] = *cap;
    421 
    422 	buf.Pointer = NULL;
    423 	buf.Length = ACPI_ALLOCATE_LOCAL_BUFFER;
    424 
    425 	rv = AcpiEvaluateObject(hdl, "_OSC", &arg_list, &buf);
    426 
    427 	if (ACPI_FAILURE(rv))
    428 		return rv;
    429 
    430 	obj = buf.Pointer;
    431 
    432 	if (obj->Type != ACPI_TYPE_BUFFER) {
    433 		rv = AE_TYPE;
    434 		goto out;
    435 	}
    436 
    437 	if (obj->Buffer.Length != cap->Buffer.Length) {
    438 		rv = AE_BUFFER_OVERFLOW;
    439 		goto out;
    440 	}
    441 
    442 	(void)memcpy(val, obj->Buffer.Pointer, obj->Buffer.Length);
    443 
    444 out:
    445 	if (buf.Pointer != NULL)
    446 		ACPI_FREE(buf.Pointer);
    447 
    448 	return rv;
    449 }
    450 
    451 static const char *
    452 acpicpu_cap_oscerr(uint32_t err)
    453 {
    454 
    455 	KASSERT((err & __BIT(0)) == 0);
    456 
    457 	if ((err & __BIT(1)) != 0)
    458 		return "_OSC failure";
    459 
    460 	if ((err & __BIT(2)) != 0)
    461 		return "unrecognized UUID";
    462 
    463 	if ((err & __BIT(3)) != 0)
    464 		return "unrecognized revision";
    465 
    466 	if ((err & __BIT(4)) != 0)
    467 		return "capabilities masked";
    468 
    469 	return "unknown error";
    470 }
    471 
    472 static void
    473 acpicpu_notify(ACPI_HANDLE hdl, uint32_t evt, void *aux)
    474 {
    475 	ACPI_OSD_EXEC_CALLBACK func;
    476 	struct acpicpu_softc *sc;
    477 	device_t self = aux;
    478 
    479 	sc = device_private(self);
    480 
    481 	if (sc->sc_cold != false)
    482 		return;
    483 
    484 	switch (evt) {
    485 
    486 	case ACPICPU_C_NOTIFY:
    487 
    488 		if ((sc->sc_flags & ACPICPU_FLAG_C) == 0)
    489 			return;
    490 
    491 		func = acpicpu_cstate_callback;
    492 		break;
    493 
    494 	case ACPICPU_P_NOTIFY:
    495 
    496 		if ((sc->sc_flags & ACPICPU_FLAG_P) == 0)
    497 			return;
    498 
    499 		func = acpicpu_pstate_callback;
    500 		break;
    501 
    502 	case ACPICPU_T_NOTIFY:
    503 
    504 		if ((sc->sc_flags & ACPICPU_FLAG_T) == 0)
    505 			return;
    506 
    507 		func = acpicpu_tstate_callback;
    508 		break;
    509 
    510 	default:
    511 		aprint_error_dev(sc->sc_dev,  "unknown notify: 0x%02X\n", evt);
    512 		return;
    513 	}
    514 
    515 	(void)AcpiOsExecute(OSL_NOTIFY_HANDLER, func, sc->sc_dev);
    516 }
    517 
    518 static bool
    519 acpicpu_suspend(device_t self, const pmf_qual_t *qual)
    520 {
    521 	struct acpicpu_softc *sc = device_private(self);
    522 
    523 	if ((sc->sc_flags & ACPICPU_FLAG_C) != 0)
    524 		(void)acpicpu_cstate_suspend(self);
    525 
    526 	if ((sc->sc_flags & ACPICPU_FLAG_P) != 0)
    527 		(void)acpicpu_pstate_suspend(self);
    528 
    529 	if ((sc->sc_flags & ACPICPU_FLAG_T) != 0)
    530 		(void)acpicpu_tstate_suspend(self);
    531 
    532 	sc->sc_cold = true;
    533 
    534 	return true;
    535 }
    536 
    537 static bool
    538 acpicpu_resume(device_t self, const pmf_qual_t *qual)
    539 {
    540 	struct acpicpu_softc *sc = device_private(self);
    541 
    542 	sc->sc_cold = false;
    543 
    544 	if ((sc->sc_flags & ACPICPU_FLAG_C) != 0)
    545 		(void)acpicpu_cstate_resume(self);
    546 
    547 	if ((sc->sc_flags & ACPICPU_FLAG_P) != 0)
    548 		(void)acpicpu_pstate_resume(self);
    549 
    550 	if ((sc->sc_flags & ACPICPU_FLAG_T) != 0)
    551 		(void)acpicpu_tstate_resume(self);
    552 
    553 	return true;
    554 }
    555 
    556 #ifdef _MODULE
    557 
    558 MODULE(MODULE_CLASS_DRIVER, acpicpu, NULL);
    559 CFDRIVER_DECL(acpicpu, DV_DULL, NULL);
    560 
    561 static int acpicpuloc[] = { -1 };
    562 extern struct cfattach acpicpu_ca;
    563 
    564 static struct cfparent acpiparent = {
    565 	"acpinodebus", NULL, DVUNIT_ANY
    566 };
    567 
    568 static struct cfdata acpicpu_cfdata[] = {
    569 	{
    570 		.cf_name = "acpicpu",
    571 		.cf_atname = "acpicpu",
    572 		.cf_unit = 0,
    573 		.cf_fstate = FSTATE_STAR,
    574 		.cf_loc = acpicpuloc,
    575 		.cf_flags = 0,
    576 		.cf_pspec = &acpiparent,
    577 	},
    578 
    579 	{ NULL, NULL, 0, 0, NULL, 0, NULL }
    580 };
    581 
    582 static int
    583 acpicpu_modcmd(modcmd_t cmd, void *context)
    584 {
    585 	int err;
    586 
    587 	switch (cmd) {
    588 
    589 	case MODULE_CMD_INIT:
    590 
    591 		err = config_cfdriver_attach(&acpicpu_cd);
    592 
    593 		if (err != 0)
    594 			return err;
    595 
    596 		err = config_cfattach_attach("acpicpu", &acpicpu_ca);
    597 
    598 		if (err != 0) {
    599 			config_cfdriver_detach(&acpicpu_cd);
    600 			return err;
    601 		}
    602 
    603 		err = config_cfdata_attach(acpicpu_cfdata, 1);
    604 
    605 		if (err != 0) {
    606 			config_cfattach_detach("acpicpu", &acpicpu_ca);
    607 			config_cfdriver_detach(&acpicpu_cd);
    608 			return err;
    609 		}
    610 
    611 		return 0;
    612 
    613 	case MODULE_CMD_FINI:
    614 
    615 		err = config_cfdata_detach(acpicpu_cfdata);
    616 
    617 		if (err != 0)
    618 			return err;
    619 
    620 		config_cfattach_detach("acpicpu", &acpicpu_ca);
    621 		config_cfdriver_detach(&acpicpu_cd);
    622 
    623 		return 0;
    624 
    625 	default:
    626 		return ENOTTY;
    627 	}
    628 }
    629 
    630 #endif	/* _MODULE */
    631