2 * Copyright (c) 2001 Mitsuru IWASAKI
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
32 #include <sys/condvar.h>
34 #include <sys/fcntl.h>
35 #include <sys/kernel.h>
36 #include <sys/malloc.h>
37 #include <sys/module.h>
39 #include <sys/sysctl.h>
44 #include <contrib/dev/acpica/include/acpi.h>
45 #include <contrib/dev/acpica/include/accommon.h>
46 #include <contrib/dev/acpica/include/actables.h>
48 #include <dev/acpica/acpivar.h>
49 #include <dev/acpica/acpiio.h>
51 #include <machine/nexusvar.h>
54 * APM driver emulation
57 #include <machine/apm_bios.h>
58 #include <machine/pc/bios.h>
60 #include <i386/bios/apm.h>
62 SYSCTL_DECL(_debug_acpi);
64 uint32_t acpi_resume_beep;
65 TUNABLE_INT("debug.acpi.resume_beep", &acpi_resume_beep);
66 SYSCTL_UINT(_debug_acpi, OID_AUTO, resume_beep, CTLFLAG_RW, &acpi_resume_beep,
67 0, "Beep the PC speaker when resuming");
68 uint32_t acpi_reset_video;
69 TUNABLE_INT("hw.acpi.reset_video", &acpi_reset_video);
71 static int intr_model = ACPI_INTR_PIC;
72 static int apm_active;
73 static struct clonedevs *apm_clones;
75 MALLOC_DEFINE(M_APMDEV, "apmdev", "APM device emulation");
77 static d_open_t apmopen;
78 static d_close_t apmclose;
79 static d_write_t apmwrite;
80 static d_ioctl_t apmioctl;
81 static d_poll_t apmpoll;
82 static d_kqfilter_t apmkqfilter;
83 static void apmreadfiltdetach(struct knote *kn);
84 static int apmreadfilt(struct knote *kn, long hint);
85 static struct filterops apm_readfiltops =
86 { 1, NULL, apmreadfiltdetach, apmreadfilt };
88 static struct cdevsw apm_cdevsw = {
89 .d_version = D_VERSION,
90 .d_flags = D_TRACKCLOSE | D_NEEDMINOR,
97 .d_kqfilter = apmkqfilter
101 acpi_capm_convert_battstate(struct acpi_battinfo *battp)
107 if (battp->state & ACPI_BATT_STAT_DISCHARG) {
108 if (battp->cap >= 50)
109 state = 0; /* high */
113 if (battp->state & ACPI_BATT_STAT_CRITICAL)
114 state = 2; /* critical */
115 if (battp->state & ACPI_BATT_STAT_CHARGING)
116 state = 3; /* charging */
118 /* If still unknown, determine it based on the battery capacity. */
119 if (state == APM_UNKNOWN) {
120 if (battp->cap >= 50)
121 state = 0; /* high */
130 acpi_capm_convert_battflags(struct acpi_battinfo *battp)
136 if (battp->cap >= 50)
137 flags |= APM_BATT_HIGH;
139 if (battp->state & ACPI_BATT_STAT_CRITICAL)
140 flags |= APM_BATT_CRITICAL;
142 flags |= APM_BATT_LOW;
144 if (battp->state & ACPI_BATT_STAT_CHARGING)
145 flags |= APM_BATT_CHARGING;
146 if (battp->state == ACPI_BATT_STAT_NOT_PRESENT)
147 flags = APM_BATT_NOT_PRESENT;
153 acpi_capm_get_info(apm_info_t aip)
156 struct acpi_battinfo batt;
158 aip->ai_infoversion = 1;
161 aip->ai_status = apm_active;
162 aip->ai_capabilities= 0xff00; /* unknown */
164 if (acpi_acad_get_acline(&acline))
165 aip->ai_acline = APM_UNKNOWN; /* unknown */
167 aip->ai_acline = acline; /* on/off */
169 if (acpi_battery_get_battinfo(NULL, &batt) != 0) {
170 aip->ai_batt_stat = APM_UNKNOWN;
171 aip->ai_batt_life = APM_UNKNOWN;
172 aip->ai_batt_time = -1; /* unknown */
173 aip->ai_batteries = ~0U; /* unknown */
175 aip->ai_batt_stat = acpi_capm_convert_battstate(&batt);
176 aip->ai_batt_life = batt.cap;
177 aip->ai_batt_time = (batt.min == -1) ? -1 : batt.min * 60;
178 aip->ai_batteries = acpi_battery_get_units();
185 acpi_capm_get_pwstatus(apm_pwstatus_t app)
188 int acline, unit, error;
189 struct acpi_battinfo batt;
191 if (app->ap_device != PMDV_ALLDEV &&
192 (app->ap_device < PMDV_BATT0 || app->ap_device > PMDV_BATT_ALL))
195 if (app->ap_device == PMDV_ALLDEV)
196 error = acpi_battery_get_battinfo(NULL, &batt);
198 unit = app->ap_device - PMDV_BATT0;
199 dev = devclass_get_device(devclass_find("battery"), unit);
201 error = acpi_battery_get_battinfo(dev, &batt);
208 app->ap_batt_stat = acpi_capm_convert_battstate(&batt);
209 app->ap_batt_flag = acpi_capm_convert_battflags(&batt);
210 app->ap_batt_life = batt.cap;
211 app->ap_batt_time = (batt.min == -1) ? -1 : batt.min * 60;
213 if (acpi_acad_get_acline(&acline))
214 app->ap_acline = APM_UNKNOWN;
216 app->ap_acline = acline; /* on/off */
221 /* Create single-use devices for /dev/apm and /dev/apmctl. */
223 apm_clone(void *arg, struct ucred *cred, char *name, int namelen,
230 if (strcmp(name, "apmctl") == 0)
232 else if (strcmp(name, "apm") == 0)
237 /* Always create a new device and unit number. */
239 if (clone_create(&apm_clones, &apm_cdevsw, &unit, dev, 0)) {
241 *dev = make_dev(&apm_cdevsw, unit,
242 UID_ROOT, GID_OPERATOR, 0660, "apmctl%d", unit);
244 *dev = make_dev(&apm_cdevsw, unit,
245 UID_ROOT, GID_OPERATOR, 0664, "apm%d", unit);
249 (*dev)->si_flags |= SI_CHEAPCLONE;
254 /* Create a struct for tracking per-device suspend notification. */
255 static struct apm_clone_data *
256 apm_create_clone(struct cdev *dev, struct acpi_softc *acpi_sc)
258 struct apm_clone_data *clone;
260 clone = malloc(sizeof(*clone), M_APMDEV, M_WAITOK);
262 clone->acpi_sc = acpi_sc;
263 clone->notify_status = APM_EV_NONE;
264 bzero(&clone->sel_read, sizeof(clone->sel_read));
265 knlist_init_mtx(&clone->sel_read.si_note, &acpi_mutex);
268 * The acpi device is always managed by devd(8) and is considered
269 * writable (i.e., ack is required to allow suspend to proceed.)
271 if (strcmp("acpi", devtoname(dev)) == 0)
272 clone->flags = ACPI_EVF_DEVD | ACPI_EVF_WRITE;
274 clone->flags = ACPI_EVF_NONE;
277 STAILQ_INSERT_TAIL(&acpi_sc->apm_cdevs, clone, entries);
283 apmopen(struct cdev *dev, int flag, int fmt, struct thread *td)
285 struct acpi_softc *acpi_sc;
286 struct apm_clone_data *clone;
288 acpi_sc = devclass_get_softc(devclass_find("acpi"), 0);
289 clone = apm_create_clone(dev, acpi_sc);
290 dev->si_drv1 = clone;
292 /* If the device is opened for write, record that. */
293 if ((flag & FWRITE) != 0)
294 clone->flags |= ACPI_EVF_WRITE;
300 apmclose(struct cdev *dev, int flag, int fmt, struct thread *td)
302 struct apm_clone_data *clone;
303 struct acpi_softc *acpi_sc;
305 clone = dev->si_drv1;
306 acpi_sc = clone->acpi_sc;
308 /* We are about to lose a reference so check if suspend should occur */
309 if (acpi_sc->acpi_next_sstate != 0 &&
310 clone->notify_status != APM_EV_ACKED)
311 acpi_AckSleepState(clone, 0);
313 /* Remove this clone's data from the list and free it. */
315 STAILQ_REMOVE(&acpi_sc->apm_cdevs, clone, apm_clone_data, entries);
316 knlist_destroy(&clone->sel_read.si_note);
318 free(clone, M_APMDEV);
319 destroy_dev_sched(dev);
324 apmioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
327 struct apm_clone_data *clone;
328 struct acpi_softc *acpi_sc;
329 struct apm_info info;
330 struct apm_event_info *ev_info;
334 clone = dev->si_drv1;
335 acpi_sc = clone->acpi_sc;
339 if ((flag & FWRITE) == 0)
341 if (acpi_sc->acpi_next_sstate == 0) {
342 if (acpi_sc->acpi_suspend_sx != ACPI_STATE_S5) {
343 error = acpi_ReqSleepState(acpi_sc,
344 acpi_sc->acpi_suspend_sx);
347 "power off via apm suspend not supported\n");
351 error = acpi_AckSleepState(clone, 0);
354 if ((flag & FWRITE) == 0)
356 if (acpi_sc->acpi_next_sstate == 0) {
357 if (acpi_sc->acpi_standby_sx != ACPI_STATE_S5) {
358 error = acpi_ReqSleepState(acpi_sc,
359 acpi_sc->acpi_standby_sx);
362 "power off via apm standby not supported\n");
366 error = acpi_AckSleepState(clone, 0);
368 case APMIO_NEXTEVENT:
369 printf("apm nextevent start\n");
371 if (acpi_sc->acpi_next_sstate != 0 && clone->notify_status ==
373 ev_info = (struct apm_event_info *)addr;
374 if (acpi_sc->acpi_next_sstate <= ACPI_STATE_S3)
375 ev_info->type = PMEV_STANDBYREQ;
377 ev_info->type = PMEV_SUSPENDREQ;
379 clone->notify_status = APM_EV_NOTIFIED;
380 printf("apm event returning %d\n", ev_info->type);
385 case APMIO_GETINFO_OLD:
386 if (acpi_capm_get_info(&info))
388 aiop = (apm_info_old_t)addr;
389 aiop->ai_major = info.ai_major;
390 aiop->ai_minor = info.ai_minor;
391 aiop->ai_acline = info.ai_acline;
392 aiop->ai_batt_stat = info.ai_batt_stat;
393 aiop->ai_batt_life = info.ai_batt_life;
394 aiop->ai_status = info.ai_status;
397 if (acpi_capm_get_info((apm_info_t)addr))
400 case APMIO_GETPWSTATUS:
401 if (acpi_capm_get_pwstatus((apm_pwstatus_t)addr))
405 if ((flag & FWRITE) == 0)
410 if ((flag & FWRITE) == 0)
416 case APMIO_NOTHALTCPU:
419 if ((flag & FWRITE) == 0)
423 if ((flag & FWRITE) == 0)
425 bzero(addr, sizeof(struct apm_bios_arg));
436 apmwrite(struct cdev *dev, struct uio *uio, int ioflag)
438 return (uio->uio_resid);
442 apmpoll(struct cdev *dev, int events, struct thread *td)
444 struct apm_clone_data *clone;
449 clone = dev->si_drv1;
450 if (clone->acpi_sc->acpi_next_sstate)
451 revents |= events & (POLLIN | POLLRDNORM);
453 selrecord(td, &clone->sel_read);
459 apmkqfilter(struct cdev *dev, struct knote *kn)
461 struct apm_clone_data *clone;
464 clone = dev->si_drv1;
466 kn->kn_fop = &apm_readfiltops;
467 knlist_add(&clone->sel_read.si_note, kn, 0);
473 apmreadfiltdetach(struct knote *kn)
475 struct apm_clone_data *clone;
479 knlist_remove(&clone->sel_read.si_note, kn, 0);
484 apmreadfilt(struct knote *kn, long hint)
486 struct apm_clone_data *clone;
491 sleeping = clone->acpi_sc->acpi_next_sstate ? 1 : 0;
497 acpi_machdep_init(device_t dev)
499 struct acpi_softc *acpi_sc;
501 acpi_sc = devclass_get_softc(devclass_find("acpi"), 0);
503 /* Create a clone for /dev/acpi also. */
504 STAILQ_INIT(&acpi_sc->apm_cdevs);
505 acpi_sc->acpi_clone = apm_create_clone(acpi_sc->acpi_dev_t, acpi_sc);
506 clone_setup(&apm_clones);
507 EVENTHANDLER_REGISTER(dev_clone, apm_clone, 0, 1000);
508 acpi_install_wakeup_handler(acpi_sc);
510 if (intr_model == ACPI_INTR_PIC)
511 BUS_CONFIG_INTR(dev, AcpiGbl_FADT.SciInterrupt,
512 INTR_TRIGGER_LEVEL, INTR_POLARITY_LOW);
514 acpi_SetIntrModel(intr_model);
516 SYSCTL_ADD_UINT(&acpi_sc->acpi_sysctl_ctx,
517 SYSCTL_CHILDREN(acpi_sc->acpi_sysctl_tree), OID_AUTO,
518 "reset_video", CTLFLAG_RW, &acpi_reset_video, 0,
519 "Call the VESA reset BIOS vector on the resume path");
525 acpi_SetDefaultIntrModel(int model)
531 /* Check BIOS date. If 1998 or older, disable ACPI. */
533 acpi_machdep_quirks(int *quirks)
538 /* BIOS address 0xffff5 contains the date in the format mm/dd/yy. */
539 va = pmap_mapbios(0xffff0, 16);
540 sscanf(va + 11, "%2d", &year);
541 pmap_unmapbios((vm_offset_t)va, 16);
544 * Date must be >= 1/1/1999 or we don't trust ACPI. Note that this
545 * check must be changed by my 114th birthday.
547 if (year > 90 && year < 99)
548 *quirks = ACPI_Q_BROKEN;
556 __asm __volatile("sti; hlt");
560 * Support for mapping ACPI tables during early boot. This abuses the
561 * crashdump map because the kernel cannot allocate KVA in
562 * pmap_mapbios() when this is used. This makes the following
563 * assumptions about how we use this KVA: pages 0 and 1 are used to
564 * map in the header of each table found via the RSDT or XSDT and
565 * pages 2 to n are used to map in the RSDT or XSDT. This has to use
566 * 2 pages for the table headers in case a header spans a page
569 * XXX: We don't ensure the table fits in the available address space
570 * in the crashdump map.
574 * Map some memory using the crashdump map. 'offset' is an offset in
575 * pages into the crashdump map to use for the start of the mapping.
578 table_map(vm_paddr_t pa, int offset, vm_offset_t length)
583 off = pa & PAGE_MASK;
584 length = roundup(length + off, PAGE_SIZE);
586 va = (vm_offset_t)pmap_kenter_temporary(pa, offset) +
587 (offset * PAGE_SIZE);
588 data = (void *)(va + off);
600 /* Unmap memory previously mapped with table_map(). */
602 table_unmap(void *data, vm_offset_t length)
606 va = (vm_offset_t)data;
607 off = va & PAGE_MASK;
608 length = roundup(length + off, PAGE_SIZE);
619 * Map a table at a given offset into the crashdump map. It first
620 * maps the header to determine the table length and then maps the
624 map_table(vm_paddr_t pa, int offset, const char *sig)
626 ACPI_TABLE_HEADER *header;
630 header = table_map(pa, offset, sizeof(ACPI_TABLE_HEADER));
631 if (strncmp(header->Signature, sig, ACPI_NAME_SIZE) != 0) {
632 table_unmap(header, sizeof(ACPI_TABLE_HEADER));
635 length = header->Length;
636 table_unmap(header, sizeof(ACPI_TABLE_HEADER));
637 table = table_map(pa, offset, length);
638 if (ACPI_FAILURE(AcpiTbChecksum(table, length))) {
640 printf("ACPI: Failed checksum for table %s\n", sig);
641 #if (ACPI_CHECKSUM_ABORT)
642 table_unmap(table, length);
650 * See if a given ACPI table is the requested table. Returns the
651 * length of the able if it matches or zero on failure.
654 probe_table(vm_paddr_t address, const char *sig)
656 ACPI_TABLE_HEADER *table;
658 table = table_map(address, 0, sizeof(ACPI_TABLE_HEADER));
661 printf("ACPI: Failed to map table at 0x%jx\n",
666 printf("Table '%.4s' at 0x%jx\n", table->Signature,
669 if (strncmp(table->Signature, sig, ACPI_NAME_SIZE) != 0) {
670 table_unmap(table, sizeof(ACPI_TABLE_HEADER));
673 table_unmap(table, sizeof(ACPI_TABLE_HEADER));
678 * Try to map a table at a given physical address previously returned
679 * by acpi_find_table().
682 acpi_map_table(vm_paddr_t pa, const char *sig)
685 return (map_table(pa, 0, sig));
688 /* Unmap a table previously mapped via acpi_map_table(). */
690 acpi_unmap_table(void *table)
692 ACPI_TABLE_HEADER *header;
694 header = (ACPI_TABLE_HEADER *)table;
695 table_unmap(table, header->Length);
699 * Return the physical address of the requested table or zero if one
703 acpi_find_table(const char *sig)
705 ACPI_PHYSICAL_ADDRESS rsdp_ptr;
706 ACPI_TABLE_RSDP *rsdp;
707 ACPI_TABLE_RSDT *rsdt;
708 ACPI_TABLE_XSDT *xsdt;
709 ACPI_TABLE_HEADER *table;
713 if (resource_disabled("acpi", 0))
717 * Map in the RSDP. Since ACPI uses AcpiOsMapMemory() which in turn
718 * calls pmap_mapbios() to find the RSDP, we assume that we can use
719 * pmap_mapbios() to map the RSDP.
721 if ((rsdp_ptr = AcpiOsGetRootPointer()) == 0)
723 rsdp = pmap_mapbios(rsdp_ptr, sizeof(ACPI_TABLE_RSDP));
726 printf("ACPI: Failed to map RSDP\n");
731 * For ACPI >= 2.0, use the XSDT if it is available.
732 * Otherwise, use the RSDT. We map the XSDT or RSDT at page 2
733 * in the crashdump area. Pages 0 and 1 are used to map in the
734 * headers of candidate ACPI tables.
737 if (rsdp->Revision >= 2 && rsdp->XsdtPhysicalAddress != 0) {
739 * AcpiOsGetRootPointer only verifies the checksum for
740 * the version 1.0 portion of the RSDP. Version 2.0 has
741 * an additional checksum that we verify first.
743 if (AcpiTbChecksum((UINT8 *)rsdp, ACPI_RSDP_XCHECKSUM_LENGTH)) {
745 printf("ACPI: RSDP failed extended checksum\n");
748 xsdt = map_table(rsdp->XsdtPhysicalAddress, 2, ACPI_SIG_XSDT);
751 printf("ACPI: Failed to map XSDT\n");
754 count = (xsdt->Header.Length - sizeof(ACPI_TABLE_HEADER)) /
756 for (i = 0; i < count; i++)
757 if (probe_table(xsdt->TableOffsetEntry[i], sig)) {
758 addr = xsdt->TableOffsetEntry[i];
761 acpi_unmap_table(xsdt);
763 rsdt = map_table(rsdp->RsdtPhysicalAddress, 2, ACPI_SIG_RSDT);
766 printf("ACPI: Failed to map RSDT\n");
769 count = (rsdt->Header.Length - sizeof(ACPI_TABLE_HEADER)) /
771 for (i = 0; i < count; i++)
772 if (probe_table(rsdt->TableOffsetEntry[i], sig)) {
773 addr = rsdt->TableOffsetEntry[i];
776 acpi_unmap_table(rsdt);
778 pmap_unmapbios((vm_offset_t)rsdp, sizeof(ACPI_TABLE_RSDP));
781 printf("ACPI: No %s table found\n", sig);
785 printf("%s: Found table at 0x%jx\n", sig, (uintmax_t)addr);
788 * Verify that we can map the full table and that its checksum is
791 table = map_table(addr, 0, sig);
794 acpi_unmap_table(table);
800 * ACPI nexus(4) driver.
803 nexus_acpi_probe(device_t dev)
807 error = acpi_identify();
811 return (BUS_PROBE_DEFAULT);
815 nexus_acpi_attach(device_t dev)
818 nexus_init_resources();
819 bus_generic_probe(dev);
820 if (BUS_ADD_CHILD(dev, 10, "acpi", 0) == NULL)
821 panic("failed to add acpi0 device");
823 return (bus_generic_attach(dev));
826 static device_method_t nexus_acpi_methods[] = {
827 /* Device interface */
828 DEVMETHOD(device_probe, nexus_acpi_probe),
829 DEVMETHOD(device_attach, nexus_acpi_attach),
834 DEFINE_CLASS_1(nexus, nexus_acpi_driver, nexus_acpi_methods, 1, nexus_driver);
835 static devclass_t nexus_devclass;
837 DRIVER_MODULE(nexus_acpi, root, nexus_acpi_driver, nexus_devclass, 0, 0);