History log of /openbsd-current/sys/dev/acpi/acpi.c
Revision (<<< Hide revision tags) (Show revision tags >>>) Date Author Comments
# 1.432 25-Jun-2024 kettenis

Implement sleep button and EC events as wakeup events. Print the wakeup
reason.

ok mglocker@, deraadt@


# 1.431 11-Jun-2024 kettenis

Avoid powering down PCI devices if we're rebooting. This makes some
machines (e.g. the t410) unhappy.

ok mglocker@


# 1.430 02-Jun-2024 kettenis

Power down PCI devices in S0 as well. Needed for suspend-to-idle.

ok mglocker@


# 1.429 29-May-2024 kettenis

Implement the guts for "suspend-to-idle" on amd64. This enables suspend
on machines that don't support S3. In its current state it doesn't save
a lot of power, but this should improve over time. Implementation of
wakeup methods is incomplete which means that some machine can't resume
at the moment.

ok mglocker@, mlarkin@, stsp@, deraadt@


# 1.428 13-May-2024 kettenis

Ignore button events in the first 10 seconds after resume. On some ACPI
implementations pressing the power button to wake up a machine will cause
a power button notification to happen for that button press This would
initiate a power down, which isn't the user's intent.

Based on earlier diffs from deraadt@ and James Cook.

ok mlarkin@, "I'm not opposed" cheloha@


# 1.427 13-May-2024 jsg

remove prototypes with no matching function
ok mpi@


Revision tags: OPENBSD_7_5_BASE
# 1.426 08-Jan-2024 kettenis

Implement RootPathString support in the LoadTable() AML function. Fixes
booting OpenBSD on some (ancient?) Hyper-V version.

Tested by Henryk Paluch
ok mlarkin@


Revision tags: OPENBSD_7_4_BASE
# 1.425 08-Jul-2023 tobhe

Move /dev/apm related acpi code to acpi_apm.c which is only built on
amd64 and i386. This is the first step towards a machine independent
safe sleep API.

tested by yasuoka@ bluhm@
ok deraadt@ kettenis@


# 1.424 07-Jul-2023 claudio

Add support for multiple batteries to acpithinkpad setchargestart and
setchargestop. With this laptops like x270 or x240 properly set the
thresholds for both batteries.
Tested by kn@ and jmatthew@
OK kettenis@


# 1.423 06-Jul-2023 deraadt

moving acpiioctl to a different file than acpiopen is not going to work
for ramdisks. noticed by anton.
this must be reconsidered.


# 1.422 05-Jul-2023 tobhe

Move acpiioctl to acpi_x86.c, it is only used up on i386 and amd64.

ok kettenis@ deraadt@


# 1.421 29-Jun-2023 dv

acpi(4)/acpibtn(4): use opt-in approach for wakeup GPEs

Previously, any discovered GPE that could be enabled for wake was
being enabled prior to entering ACPI-based S3 or S4. On some newer
machines, this caused S3-based suspend to break as some devices we
don't fully control (e.g. lack of driver) might be capable of waking
the system and will do so almost instantly if the GPE is enabled for
wake up (i.e. the so called "instant wake" issue).

This changes to a model of having device drivers explicitly opt
into using their GPE for wake up.

The first driver to explicitly toggle a GPE for wake is acpibtn(4).

Variations of the above have been in snapshots for 2 weeks, so
tested by many.

ok deraadt@


Revision tags: OPENBSD_7_3_BASE
# 1.420 15-Mar-2023 kettenis

Add missing nameref resolving for the _DEP() package elements that indicate
dependencies between ACPI devices.

ok dlg@, deraadt@


# 1.419 10-Feb-2023 visa

Adjust knote(9) API

Make knote(9) lock the knote list internally, and add knote_locked(9)
for the typical situation where the list is already locked.

Remove the KNOTE(9) macro to simplify the API.

Manual page OK jmc@
OK mpi@ mvs@


Revision tags: OPENBSD_7_2_BASE
# 1.418 13-Sep-2022 kettenis

Split out the code that collects data from acpiac(4), acpibat(4) and
acpisbs(4) for apm(4) and hook it up to the arm64 version of apm(4) on
systems with ACPI.

ok kn@


# 1.417 12-Sep-2022 kettenis

Add support for level-triggered GPIO events.

ok mlarkin@


# 1.416 10-Sep-2022 kettenis

Don't restrict GPIO events to pin numbers below 256. With the _EVT()
method events corresponding to larger numbers are possible.

Fixes lid state detection on the x13s.

ok patrick@


# 1.415 03-Sep-2022 kettenis

Make sure we don't suspend if we have no way to wake up the machine.
The idea is that device drivers that can wake up the machine register
themselves by calling device_register_wakeup(). To prevent regressions
on amd64, we let acpi(4) register itself as a wakeup device if the AML
for the machine defines wakeup devices. This may be refined in the
future.

This diff will prevent people from suspending their arm64 SBCs without
having a way to wake them up. For now the only driver that registers
itself is axppmic(4), which means that at this moment only SBCs with
an allwinner A64 or H5 SoC will actually support suspend/resume.

ok mlarkin@, deraadt@


# 1.414 10-Aug-2022 patrick

Provide the AML root in ACPI's softc so that drivers that need access
don't have to declare it using extern.

Suggested by and with feedback from kettenis@
Tested by deraadt@ on arm64, amd64 and i386
Tested by bluhm@ on amd64 and i386


Revision tags: OPENBSD_7_1_BASE
# 1.413 17-Feb-2022 jsg

reduce includes


# 1.412 10-Feb-2022 visa

Embed klist head in acpi_softc to avoid explicit malloc.

OK kettenis@


# 1.411 09-Feb-2022 deraadt

Move x86-only stubs used by kern/subr_suspend.c into dev/acpi/acpi_x86.c
for sharing between i386 and amd64.
ok mlarkin kettenis


# 1.410 08-Feb-2022 deraadt

The suspend/resume code is a sticky mess of MI, MD, and ACPI sequencing.
This splits out the MI sequencing, backing it with per-architecture helper
functions. Further steps will be neccesary because ACPI and MD are too
tightly coupled, but soon we'll be able to use this code for more architectures
(which depends on figuring out the lowest-level cpu sleeping method)
ok kettenis


# 1.409 04-Feb-2022 robert

set the APM_BATT_CHARGING state if the battery is being charged and try
to calculate the remaining time to be fully charged

ok jca@


# 1.408 02-Feb-2022 deraadt

acpi_addtask() calls malloc() w/ M_NOWAIT (because some calls come from
interrupt context), this however means occasional resource shortage will
result in callbacks registration failing, and unknown consequences for
the task-submitting caller.
Changing this to use pools with a low water mark, decreases the odds
of that problem occuring.
ok kettenis


# 1.407 01-Feb-2022 deraadt

GPE_DIRECT is calling the AML parser from interrupt context, which is
not permitted. Luckily nothing is using GPE_DIRECT anymore, so this code
can be deleted.
ok kettenis


# 1.406 26-Jan-2022 kettenis

An ACPI device needs to be both present and enabled for it to function.
So only attempt to attach hardware that has both bits enabled. This fixes
an issue where com(4) would attach for a disabled serial port leading to
misdetection of the hardware variant and a subsequent hang when /etc/rc
runs ttyflags -a.

ok anton@, deraadt@


# 1.405 12-Jan-2022 patrick

Make acpi_getpropint() return uint64_t, as ACPI integers are in fact that
wide and some _DSD properties depend on it.

ok kettenis@


# 1.404 04-Jan-2022 patrick

acpi_getprop() needs to actually make sure that we're looking at the
correct property. While there adjust acpi_getpropint() as well to
increase similarity with acpi_getprop().

ok kettenis@


# 1.403 16-Dec-2021 anton

Attach com over acpi on amd64. Some hardware uses a different interrupt
assignment compared to the the legacy one supported by com over isa.
This causes the console to halt once userland takes over as no
interrupts are received. The actual address and irq can be read from
ACPI, kettenis@ already added support for arm64 which paved the way for
amd64.

Some consoles that previously attached over isa are now expected to
attach over acpi.

Thanks to patrick@ for testing on arm64.

ok kettenis@


# 1.402 07-Dec-2021 kettenis

Fix parsing of SR_IRQ resource descriptors.

ok patrick@, anton@


# 1.401 02-Nov-2021 deraadt

knf


# 1.400 30-Oct-2021 deraadt

Change hw.perfpolicy=auto by default, at startup. If the system has AC
power connected (default is yes when no driver differentiates) then default
to 100% performance. On battery, use the existing auto algorithm (which is
admittedly somewhat unrefined).
This change overrides the system/BIOS speed and puts OpenBSD in control.
As this happens very early during boot, besides speedups in all usage usage
patterns, some surprises: unhibernate and sysupgrade times are cut in half.
note: on a few architectures, the setperf fn pointer is changed late, and
thus the auto algorithm stops timeing out. kettenis and i will look for
a solution.
in snaps for more than a week.
ok kettenis


Revision tags: OPENBSD_7_0_BASE
# 1.399 20-Jul-2021 mlarkin

Fix trailing whitespace in a few places.

No code change.


# 1.398 09-May-2021 kettenis

Some x86 machines advertise the "hardware reduced" ACPI feature, advertise
S4 and S5 support, but fail to populate the SLEEP_CONTROL_REG and
SLEEP_STATUS_REG descriptions in the FADT. An example of such a machine
is the ASUS Zenbook 14 UM433DA. Any attempt to powerdown the machine
will result in a kernel crash. It turns out that using the legacy ACPI PM
registers works fine on this machien. So fall back on those registers
if SLEEP_CONTROL_REG and/or SLEEP_STATUS_REG aren't provided.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_9_BASE
# 1.397 15-Mar-2021 patrick

Add acpi_iommu_device_map(), which replaces the DMA tag with one that
is blessed with IOMMU magic, if available. This is mainly for arm64,
since on amd64 and i386 the IOMMU only captures PCIe devices, as far
as I know, which uses the pci_probe_device_hook(). This though is for
non-PCI devices attached through ACPI.

ok kettenis@


# 1.396 07-Mar-2021 patrick

Pass standard DMA tag to acpi(4) table drivers.

ok kettenis@


# 1.395 27-Dec-2020 deraadt

acpi_map_address() cannot be wrapped by SMALL_KERNEL anymore, as
it is used by acpihpet.c


# 1.394 25-Dec-2020 visa

Refactor klist insertion and removal

Rename klist_{insert,remove}() to klist_{insert,remove}_locked().
These functions assume that the caller has locked the klist. The current
state of locking remains intact because the kernel lock is still used
with all klists.

Add new functions klist_insert() and klist_remove() that lock the klist
internally. This allows some code simplification.

OK mpi@


# 1.393 17-Dec-2020 kettenis

Fix some issues with referencing named ACPI nodes from Packages.
These references need to be resolved at runtime rather than when
they're parsed such that they pick up the right values for those nodes
which can be changed when for example _INI methods run.

The current approach is to replace these reference with a string that
names the node in question. The problem with that is that packages
can also contain normal strings. Which means that we need special
code that depends on the context where the package is used.

This diff takes a different approach by retaining a reference when
parsing. Code that uses a package will need to resolve this reference
but there is no ambiguiety anymore.

ok patrick@


# 1.392 05-Dec-2020 kettenis

Add support for the _EVT method to the GPIO support code.

ok patrick@


Revision tags: OPENBSD_6_8_BASE
# 1.391 27-Aug-2020 jmatthew

Reorder the acpi attach process so we look at Processor() CPU nodes
after Device() ones, since we should prefer the newer node type.
If we see any Device() nodes, don't attach acpicpu(4) to any Processor()
nodes if they're also present. This also makes acpitz(4) and
acpipwrres(4) devices attach slightly later.

ok kettenis@, also tested by jmc@


# 1.390 16-Aug-2020 gnezdo

Remove sc_maxgpe from acpi_softc which was never read from

Remove a dead conditional from acpi_init_gpes while there.

ok jcs@


# 1.389 21-Jul-2020 deraadt

acpi can use IPL_BIO (a low interrupt) since it only enqueues operations for
later processing. The use of a high interrupt will predate suspend/resume
efforts, we had to redesign acpi to be non-reentrant obviously
discussed with kettenis, in snaps for more than a week


# 1.388 24-Jun-2020 cheloha

kernel: use gettime(9)/getuptime(9) in lieu of time_second(9)/time_uptime(9)

time_second(9) and time_uptime(9) are widely used in the kernel to
quickly get the system UTC or system uptime as a time_t. However,
time_t is 64-bit everywhere, so it is not generally safe to use them
on 32-bit platforms: you have a split-read problem if your hardware
cannot perform atomic 64-bit reads.

This patch replaces time_second(9) with gettime(9), a safer successor
interface, throughout the kernel. Similarly, time_uptime(9) is replaced
with getuptime(9).

There is a performance cost on 32-bit platforms in exchange for
eliminating the split-read problem: instead of two register reads you
now have a lockless read loop to pull the values from the timehands.
This is really not *too* bad in the grand scheme of things, but
compared to what we were doing before it is several times slower.

There is no performance cost on 64-bit (__LP64__) platforms.

With input from visa@, dlg@, and tedu@.

Several bugs squashed by visa@.

ok kettenis@


# 1.387 10-Jun-2020 jca

Give userland a consistent battery and A/C state after resume

Previously we refreshed the state of acpiac(4) and acpibat(4)/acpisbs(4)
by queueing fake ACPI events to be processed by the acpi thread.
acpibat(4) using ACPIDEV_POLL meant that its status could be refreshed
first, resulting in an APM_POWER_CHANGE event sent to userland before
the state of acpiac(4) was up to date. Because of this, apmd(8) could
see A/C unplugged when it was in fact plugged, possibly leading to
a wrong decision.

Instead of using fake ACPI events, refresh the status of acpiac(4) and
acpibat(4)/acpisbs(4) right away at DVACT_WAKEUP time. To reuse the
existing refresh code of those drivers, make their ACPI *_notify()
handlers responsible for sending APM_POWER_CHANGE events instead. Also
queue an APM_POWER_CHANGE event right after resume to retain existing
behavior.

This fixes the consistency of data shown to userland, and makes those
drivers less special.

Initially discussed last year (!) with mpi@ at Miod's place.
Feedback and ok jcs@, deraadt@ agrees with the use of DVACT_WAKEUP here.


# 1.386 29-May-2020 deraadt

dev/rndvar.h no longer has statistical interfaces (removed during various
conversion steps). it only contains kernel prototypes for 4 interfaces,
all of which legitimately belong in sys/systm.h, which are already included
by all enqueue_randomness() users.


# 1.385 14-May-2020 kettenis

Bring back the acpi(4) changes. This time around only use the ACPI _CRS
information on ACPI 5.0 and later.

ok krw@, patrick@


# 1.384 11-May-2020 jca

More accurate remaining power reporting with disparate batteries

Overall remaining power was computed as the average of the remaining
power of each battery, in percents. This doesn't work with batteries of
different capacity, so use the total capacity and remaining power
instead.

Reported by lidstah, successful tests by lidstah and benno@, ok kettenis@


# 1.383 08-May-2020 kettenis

Move parsing of _CRS into acpi(4) and pass the parsed address and interrupt
information in struct acpi_attach_args.

ok mpi@


Revision tags: OPENBSD_6_7_BASE
# 1.382 14-Apr-2020 kettenis

Implement acpi_getprop(), the ACPI equivalent of OF_getprop().

ok patrick@


# 1.381 12-Apr-2020 kettenis

Add support for the _CCA method. This method indicates whether DMA is
cache-coherent or not. To implement this, acpi(4) gets two bus_dma tags
and passes the appropriate one when attaching devices based on _CCA.
On i386/amd64, where for all practical purpose DMA is always cache-coherent,
the two tags are the same. But on arm64 they are distinct.

ok patrick@


# 1.380 07-Apr-2020 visa

Abstract the head of knote lists. This allows extending the lists,
for example, with locking assertions.

OK mpi@, anton@


# 1.379 03-Apr-2020 mpi

Fix incoherencies of errors returned by various *kqfilter routines.

EOPNOTSUPP is returned when there's no "filterops" for a given fd.
EINVAL when the requested filter isn't supported by the "filterops".
ENXIO means the underlying device is no longer valid/present.

ok visa@


# 1.378 20-Feb-2020 visa

Replace field f_isfd with field f_flags in struct filterops to allow
adding more filter properties without cluttering the struct.

OK mpi@, anton@


# 1.377 31-Dec-2019 visa

Use C99 designated initializers with struct filterops. In addition,
make the structs const so that the data are put in .rodata.

OK mpi@, deraadt@, anton@, bluhm@


# 1.376 31-Dec-2019 mpi

Convert infinite sleeps to {m,t}sleep_nsec(9).

ok kn@


# 1.375 31-Dec-2019 jsg

include sys/sensors.h for sensor use
don't depend on indirect inclusion from acpidev.h


Revision tags: OPENBSD_6_6_BASE
# 1.374 07-Sep-2019 kettenis

Revert acpipci(4) changes. Too many systems are broken. We'll try again
after 6.6 as been released. The acpireg.h change stays behind.


# 1.373 28-Aug-2019 kettenis

Use ACPI information to attach PCI busses like we do on arm64. There are a
few additional quirks though, and attaching the PCI busses is delayed to
replicate the existing code more closely. That may be changed in the
future. Also tweak how we handle MSI support and respect to ACPI flag
that says we shouldn't attempt to use MSIs.

Some fallout is expected.

ok patrick@


# 1.372 27-Aug-2019 deraadt

On amd64 the bootup and resume paths for SP/BP/SP were not performing
msr/pat, clock-startup, firmware upgrade, fpu initialization, and cpu
soft-state initialization in the same order. That kind of chaos is
crazy and fragile. Improve the acpi resume call sequence and unify order
of the operations.


# 1.371 02-Jul-2019 jcs

acpi_set_gpehandler: turn edge arg into flags, add support for
passing GPE_DIRECT which directly executes the supplied callback and
clears a level interrupt rather than going through acpi_taskq which
can be slow when other driver callbacks are in the queue.

This should not be used for GPE handlers that need to do anything
ACPI-related.

ok kettenis


# 1.370 10-Jun-2019 kettenis

Print proper ACPI version number.

ok mlarkin@


# 1.369 08-Jun-2019 kettenis

When establishing the mapping between ACPI device nodes and PCI devices,
skip PCI host bridges and devices that aren't there.

ok patrick@


# 1.368 07-Jun-2019 kettenis

Introduce acpi_getsta() and use it to eliminate some duplicated code.

ok patrick@


# 1.367 12-May-2019 kettenis

Make sure the extended PM register descriptions actually exist before
using them. Fixes machines with ancient ACPI 1.0 (such as recent
hypervisors).

ok deraadt@


# 1.366 12-May-2019 kettenis

Add support for mapping ACPI PM registers using the "extended" gasio
representation from the FADT. Mostly fixes the Lanner NCA-1510.

ok lteo@, mlarkin@, deraadt@


# 1.365 11-May-2019 lteo

Fix incorrect error message.

ok kettenis@


# 1.364 08-May-2019 tedu

free size. the allocation is the header plus the length.


Revision tags: OPENBSD_6_5_BASE
# 1.363 04-Apr-2019 kettenis

Avoid potential null-pointer dereference. Found by Kent R. Spillner.

ok deraadt@, jsg@


# 1.362 20-Jan-2019 jsg

fix SMALL_KERNEL build after changes in rev 1.361
reported by naddy@ ok deraadt@


# 1.361 19-Jan-2019 tedu

Add a pwraction sysctl that controls what the power button does on acpi.
By default, nothing changes -- shutdown is initiated. But allows turning
power button into a sleep button if desired.
(grudging) ok from a few parties


# 1.360 26-Oct-2018 kettenis

Add acpipci(4) on amd64. For now this only calls the PCI-specific _OSC
method to let the ACPI implementation know what features we support.


Revision tags: OPENBSD_6_4_BASE
# 1.359 30-Aug-2018 kettenis

Move softbutton check before the check that skips attaching certain acpi
devices. Fixes regression caused by matching on _CID in addition to
matching on _HID.

ok matthieu@, mlarkin@


# 1.358 25-Aug-2018 kettenis

Match ACPI devices based on _CID if no match for _HID is found.

ok mpi@


# 1.357 19-Aug-2018 kettenis

Add support for multiple PCI segments. Only really implemented for arm64
for now as amd64/i386 firmware still caters for legacy OSes that only
support a single PCI segment.

ok patrick@


# 1.356 03-Aug-2018 kettenis

Let ahci(4) match on _CLS instead of _HID when attaching at acpi(4). Avoids
having to add many more _HID entries to the match table.

ok deraadt@, mlarkin@


# 1.355 10-Jul-2018 kettenis

Make legacy interrupts work in acpipci(4).

ok patrick@


# 1.354 05-Jul-2018 kettenis

Add acpipci(4), a driver that supports generic ECAM-compatible PCI host
bridges based on information provided by ACPI.

ok mlarkin@


# 1.353 04-Jul-2018 kettenis

Properly pass around the PCI "chipset tag" in acpi(4) and refactor
acpimcfg(4) to call an MD initialization functions that sets up a tag for
PCI ECAM.

ok guenther@, mlarkin@, krw@


# 1.352 01-Jul-2018 kettenis

Move acpi(4) attach glue into acpi_machdep.c.

ok guenther@, deraadt@


# 1.351 01-Jul-2018 kettenis

Add support for _DSD Device Properties.

ok mlarkin@


# 1.350 30-Jun-2018 kettenis

Add intr_enable() function, intended for MI use to amd64 and i386 and use
this in the acpi(4) suspend/resume code paths.

ok deraadt@


# 1.349 29-Jun-2018 kettenis

Use <stdint.h> types.

ok mpi@, jung@, krw@, deraadt@


# 1.348 29-Jun-2018 mlarkin

ACPI: Allow (with warning) GPE handler reassignment, instead of returning
a failure code.

ok kettenis


# 1.347 26-Jun-2018 mlarkin

knf


# 1.346 26-Jun-2018 kettenis

Let acpi(4) pass down the bus dma tag.

ok deraadt@


# 1.345 25-Jun-2018 kettenis

Reorganize acpi(4) code a bit in preparation for upcoming arm64 support.

ok deraadt@


# 1.344 20-May-2018 kettenis

Wake up the acpi thread after scheduling a gpio event.

ok mpi@


# 1.343 17-May-2018 kettenis

Sprinkle some #ifndef SMALL_KERNEL such that RAMDISK kernels cuild again.

pointed out by stsp@


# 1.342 17-May-2018 kettenis

Implement GenericSerialBus OpRegion support.

ok mlarkin@


# 1.341 27-Mar-2018 kettenis

Add acpicmos(4), a driver that implements SystemCMOS OperationRegion
access support. This fixes machines where the AML doesn't check whether
support for this OperationRegion type has been registered by the OS.

ok mlarkin@


Revision tags: OPENBSD_6_3_BASE
# 1.340 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.339 10-Feb-2018 deraadt

Syncronize filesystems to disk when suspending. Each mountpoint's vnodes
are pushed to disk. Dangling vnodes (unlinked files still in use) and
vnodes undergoing change by long-running syscalls are identified -- and
such filesystems are marked dirty on-disk while we are suspended (in case
power is lost, a fsck will be required). Filesystems without dangling or
busy vnodes are marked clean, resulting in faster boots following
"battery died" circumstances.
Tested by numerous developers, thanks for the feedback.


# 1.338 08-Feb-2018 deraadt

After flushing disks, try again to release all memory. We don't need to
save dirty memory to the hibernate space.


# 1.337 08-Feb-2018 deraadt

Toss all releaseable memory, because fragmentation can get in the way
of allocating the hibernate playpen.


# 1.336 08-Feb-2018 deraadt

sr_quiesce() is a new approach for ensuring that softraid drains
output to the disks.

This is part of a larger suspend/resume filesystem-safety diff, which
has been worked on for a couple of months already. Tests by job, krw,
beck, benno, and others. Sometimes even by snapshot users...


# 1.335 29-Nov-2017 kettenis

Add support for GPIO-signaled events.

ok mlarkin@


# 1.334 16-Nov-2017 jcs

Add PCI attachment for dwiic(4) needed by Intel 100 Series machines

ok kettenis


# 1.333 14-Nov-2017 kettenis

Add a few more PNP IDs to the skip lists.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.332 17-Aug-2017 stsp

Make dwiic(4) attach its ACPI parent dependencies.
This change makes the Asus E200HA keyboard work.

Original analysis and patch by Cesare Gargano
ok kettenis@


# 1.331 09-Aug-2017 dcoppa

Add "PNP0303" (8042 PS/2 Controller) to acpi_isa_hids[]

ok kettenis@


# 1.330 22-Jul-2017 jcs

acpibat: look for _BIX first before falling back on _BIF

ACPI 4.0 deprecated _BIF for battery status so some newer machines
are exporting _BIX which extends _BIF's package with a few extra
fields. When using _BIX, export the new discharge cycle count as a
sensor.

feedback from kettenis and anton
ok deraadt


# 1.329 20-Jul-2017 kettenis

Start a list of isa(4) device names such that we can avoid printing them
as "not configured".

ok mikeb@, millert@, deraadt@


# 1.328 08-Apr-2017 deraadt

Sensors are run as callbacks inside tasks. During suspend a sensor
could be running inside a driver that will be force-detached, or due
to tsleep end up disrupting the softstate/hardstate contract. At
suspend time, quisce all these callbacks by waiting for completion.
This issue has never been observed for real, but may be implicated in
suspend/resume failures.
ok kettenis guenther mlarkin


# 1.327 08-Apr-2017 deraadt

Two prototypes for acpi_maptable; merge to one place


Revision tags: OPENBSD_6_1_BASE
# 1.326 27-Mar-2017 deraadt

Now that hibernate_alloc() only has clean success/failure, don't
need to call hibernate_free() to clean up a partial mess.
ok mlarkin kettenis


# 1.325 13-Mar-2017 deraadt

allow ramdisks to build; ok jcs


# 1.324 12-Mar-2017 jcs

try to attach acpisbs first and if successful, don't attach acpibat


# 1.323 02-Mar-2017 natano

Add a new sysctl machdep.lidaction. The sysctl works as follows:

machdep.lidaction=0 # do nothing
machdep.lidaction=1 # suspend
machdep.lidaction=2 # hibernate

lidsuspend is just an alias for lidaction, so if you change one, the
other one will have the same value. The plan is to remove
machdep.lidsuspend eventually when people have upgraded their
/ets/sysctl.conf.

discussed with deraadt, who came up with the new MIB name
no objections mlarkin
ok stsp halex jcs


# 1.322 28-Feb-2017 natano

Untangle abstract sleep modes from ACPI sleep states. This paves the way
for further work in the area.

idea and ok deraadt
ok mlarkin


# 1.321 22-Feb-2017 jcs

add acpisbs, an acpi smart battery subsystem driver reading data
over smbus

currently disabled because it conflicts with acpibat

ok deraadt, kettenis


# 1.320 14-Jan-2017 kettenis

Revert aml_rdpciaddr changes; breaks several machines that were working
before.


# 1.319 11-Jan-2017 jsg

Increase the size of the buffers used for _HID and _CID strings to be
able to fit "Hyper_V_Gen_Counter_V1".

ok mlarkin@ deraadt@


# 1.318 08-Jan-2017 kettenis

Make aml_rdpciaddr() work for for devices behind bridges. The code now
uses the mapping between AML nodes and PCI devices that we establish.
Because _INIT methods may end up calling aml_rdpciaddr(), make sure we
create that mapping early. Also handle devices that aren't actually present.
These devices are now included in the mapping and reads will return an
all-ones patterm whereas writes are a no-op..

ok mlarkin@


# 1.317 25-Oct-2016 pirofti

Add the resource index to aml_parse_resource() callback function.

This is needed by an upcoming acpiec commit that handles machines breaking
the current ACPI specifications.

Change suggested by and ok kettenis@, guenther@


# 1.316 18-Sep-2016 guenther

If the APIC table (MADT) isn't present then ci_acpi_proc_id won't be set
when acpicpu tries to attach, leading to a NULL dereference. The ACPI
processor object on the Dells where this was reported is bogus and useless
anyway so apply the same test when deciding whether to attach to processor
objects as acpicpu would when matching them to cpus.

Problem reported by Eivind Eide (xenofil (at) gmail.com) and
Johan Huldtgren (johan+openbsd-bugs (at) huldtgren.com).
Testing by Eivind Eide.
ok kettenis@ mlarkin@


# 1.315 03-Sep-2016 naddy

Write the system time back to the RTC every 30 minutes.
This fixes the problem that long-running machines which were not
shut down properly would reboot with a badly offset system time.

hints and ok kettenis@


# 1.314 31-Aug-2016 mlarkin

Don't put a device in the wake device list if _STA says it's not there.

Fixes at least one machine that woke up from sleep / poweroff immediately.

ok kettenis@, tested by kettenis and martijn@


# 1.313 28-Jul-2016 kettenis

Store the acpi processor ID/UID in struct cpu_info, and use it to attach
acpicpu(4) drivers to the right cpu(4).

ok mlarkin@, guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.312 10-Jun-2016 kettenis

The power management timer has been made optional in ACPI 5.0A, so don't
attempt to attach acpitimer(4) if the timer isn't present.

Pointed out by jcs@

ok jcs@


# 1.311 08-May-2016 kettenis

Make sure devices listed by a device's _DEP method are attached before
we attach the device itself. The _DEP methode, introduced in ACPI 5.0,
is a hint that the device in question depends on OpRegion support from
the devices listed by _DEP, so we have to order them the proper way.
To prevent us from attaching those devices again when we encounter them
later walking down the device tree, keep track of our attempts to
attach a device driver to them using aflag in the aml_node structure.

ok guenther@


# 1.310 25-Apr-2016 pirofti

We attach acpihpet(4) and acpiec(4) without using the HID, so do not
report it in the dmesg.

OK deraadt@, mlarkin@.


# 1.309 02-Apr-2016 kettenis

Reduce dmesg spam by knocking out some acpi hardware IDs that don't need a
driver.

ok mpi@


# 1.308 29-Mar-2016 kettenis

Check if a device is present (using _STA) in acpi_foundhid. This prevents
us from reporting devices as "not configured" that aren't actually present,
and allows us to remove duplicated code from several drivers.

ok deraadt@, ok jsg@


# 1.307 29-Mar-2016 kettenis

Implement a cleaner way to prevent acpivout(4) from attaching if we have
a platform-specific method of controlling the backlight.

ok millert@, mlarkin@, jung@


# 1.306 28-Mar-2016 kettenis

Adding each and every new acpi device driver to acpi_foundhid() is
gettinga bit of the burden. And it really isn't how our autoconf
framework is supposed to work for busses that can be enumerated. All
the drivers already check for a matching _HID in their attach
function. So we can just drop the checks in acpi_foundhid() and let
the drivers sort themselves out.

Print a "not configured" line for devices for which we don't attach adriver.
This may be a bit much as there are devices for which we will not have a
driver. This will be removed if it causes too much dmesg spam.

ok deraadt@, mlarkin@


Revision tags: OPENBSD_5_9_BASE
# 1.305 17-Jan-2016 jsg

Take acpi_foundhid() outside of #ifndef SMALL_KERNEL so the ramdisk
kernel can find and attach dwiic(4) and sdhc(4).


# 1.304 17-Jan-2016 jsg

remove an unused global variable


# 1.303 14-Jan-2016 kettenis

Attach dwiic(4) on the Intel Bay Trail i2c controllers as well.


# 1.302 12-Jan-2016 kettenis

Improve emulation of PM registers on Hardware-redcued ACPI by emulating
SPL_TYPx, SLP_EN and WAK_STS using the new SLEEP_STATUS_REG and
SLEEP_CONTROL_REG registers. Makes the transition into S5 (powerdown) work
on the ASUS X205TA.

ok deraadt@, mlarkin@


# 1.301 12-Jan-2016 jcs

Add dwiic, a driver for the Synopsys DesignWare i2c controller found
on the Samsung ATIV Book 9 laptop. This initial version only
supports ACPI config/attachment.

Add ihidev, a HID-over-i2c driver largely based on uhidev. dwiic
handles attaching ihidev devices found in ACPI.

Add ims, a HID-over-i2c mouse/trackpad driver to get basic cursor
and button functionality from HID-compliant i2c trackpads.

ok kettenis deraadt


# 1.300 11-Jan-2016 kettenis

Make sdhc(4) attach to hardware IDs "80860F14" and "PNP0FFF".


# 1.299 09-Jan-2016 kettenis

Initial support for Hardware-Reduced ACPI. This skips the legacy mode checks
by emulating the PM_CNT register such that SCI_EN is always enabled. It also
skip attaching acpitimer(4) since the relevant registers are no longer there.

ok deraadt@, mlarkin@


# 1.298 06-Jan-2016 kettenis

Follow the spec and check the SCI_EN bit to see if ACPI has already
been enabled and don't bother enabling it again if it is. This should fix
systems that don't implement legacy mode and therefore set ACPI_ENABLE and
ACPI_DISABLE to zero.

ok mlarkin@, yasuoka@


# 1.297 23-Nov-2015 reyk

Back out the CRITICAL change, turn it back from 10% to 25%. Some
people with old/broken batteries didn't like this change, and the
underlying heuristics are wrong.


# 1.296 22-Nov-2015 reyk

The "CRITICAL" APM battery level, that is exported/faked by the ACPI
stack, is currently set to 25%. With 25% battery left, a typical
modern laptop will have at least one hour runtime left. It is an
arbitrary value, and other APM power layers in the kernel set CRITICAL
to something like 5% or 10%, so set it to 10% for now.

OK mlarkin@


# 1.295 28-Sep-2015 deraadt

In low-level suspend routines, set cold=2. In tsleep(), use this to
spit out a ddb trace to console. This should allow us to find suspend
or resume routines which break the rules. It depends on the console
output function being non-sleeping.... but that's another codepath which
should try to be safe when cold is set.
ok kettenis


# 1.294 06-Sep-2015 deraadt

clarify malloc arguments (base + length...)


# 1.293 28-Aug-2015 deraadt

fairly simple sizes for free(); ok tedu


# 1.292 20-Aug-2015 kettenis

Respect the access size when reading or writing to pci config space.
This version also makes sure writes are properly aligned, preventing
the panics seen with the previous incarnation of this change.

Fixes battery status passthrough in vmware, brightness keys on my x220
and who knows what else.

ok mlarkin@, deraadt@


Revision tags: OPENBSD_5_8_BASE
# 1.291 04-Aug-2015 deraadt

If we are setting ACPI_PM1_BM_RLD do it only once, and remember to
repeat this action at resume time.
ok mlarkin guenther


# 1.290 29-Jul-2015 deraadt

Something subtle in previous revision is locking up suspend or booting
on some laptops. Mailing lists threads mention x220 and Asus machines.
Reverting for now.


# 1.289 21-Jul-2015 kettenis

iRespect the access size when reading or writing to pci config space.
Fixes battery status passthrough in vmware and who knows what else.

Tested by many.

ok mlarkin@, deraadt@


# 1.288 17-Jul-2015 mlarkin

fix comments


# 1.287 26-Mar-2015 jsg

Use the 0x10 extended capability to determine if the trackstick
buttons are wired to the trackpad and need to be re-routed to the
trackstick. Without this change the buttons on 2015 thinkpads
get picked up as extended buttons that show up as scroll up/down.

Remove the X1 Carbon 2015 (LEN0048) and X250 (LEN0046) from the
top button area/soft buttons quirks list. Also avoid using the
quirk list entirely if the capability bit is set.

Based on work from jcs@ with feedback from mpi@
ok mpi@ jcs@


# 1.286 20-Mar-2015 kettenis

The ACPI standard says that a PCI function number of FFFF can be used to refer
to all functions on a device. This makes no sense in the context of accessing
PCI config space. Nevertheless stsp@ has a machine with AML that tries to
do config space reads from a device where _ADR encodes function number FFFF.
This is almost certainly buggy AML, but since Linux (and presumably Windows)
boot just fine on this machine we need to work around this. Linux effectively
treats function FFFF as function 7, which isn't present on this machine.
Instead we simulate a read from a non-existent device by returning a bit
pattern of all ones. Writes to function FFFF will still panic.

ok mlarkin@


# 1.285 16-Mar-2015 deraadt

Reduce use of SMALL_KERNEL in acpi, bringing acpiec driver and more gpe
handling into RAMDISK. This is now possible because the install media
has ample room. The goal is to reduce special cases where we may be
skipping (unknown) important operations...
ok mlarkin kettenis


# 1.284 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.283 07-Feb-2015 deraadt

New framework that allows hibernate to pass in entropy from it's fresh
boot.
ok mlarkin


# 1.282 06-Feb-2015 mlarkin

Fix a hibernate crash on some machines due to unmapping a page that
may not have been mapped previously (in the failure to hibernate case).

Also ensure that the lowmem ptp is mapped in all cases (not just MP).

ok kettenis


# 1.281 17-Jan-2015 deraadt

resume_randomness() before spinning up other cpus...


# 1.280 15-Jan-2015 jsg

Define a new wscons mouse type for Synaptics clickpad devices that lack
physical buttons. This will be used if the acpi pnpid for the mouse
matches a list provided by Synaptics found in Linux.

Combined with the xenocara changes this will remove the need for an xorg
config file for the x240/t440/t540 generation of thinkpads.

ok matthieu@ shadchin@ kettenis@


# 1.279 11-Jan-2015 kettenis

Tolerate checksum errors in ACPI tables (but complain about them). Apparently
there are machines out there with broken BIOSen with such checksum errors in
thier RSDT tables. Windows runs fine on these machines, so it probably does
not check. Linux ignores checksum errors as well (and prints a warning).

ok deraadt@, krw@


# 1.278 18-Dec-2014 deraadt

suspend and resume the RNG (at what looks like the right spots)
ok reyk djm


# 1.277 09-Dec-2014 doug

Replace some malloc(n*size,...) calls with mallocarray().

ok tedu@ deraadt@


# 1.276 06-Dec-2014 mpi

If "machdep.lidsuspend" is not set, do not go back to sleep if the lid
is still closed when resuming. This allows docked laptop to be resumed
without opening them.

ok mlarkin@


# 1.275 23-Nov-2014 mlarkin

If a machine has no _LIDs, then none can be open. This diff fixes a problem
where machines without any _LIDs were going back to sleep after hibernate
due to the recent suspend-if-lid-closed-on-resume diff. Noticed on qemu,
which doesn't have any _LIDs.

discussed with deraadt


# 1.274 18-Nov-2014 krw

Nuke yet more obvious #include duplications.

ok miod@


# 1.273 08-Nov-2014 mlarkin

If resuming from sleep (zzz/ZZZ) and the lid is still closed, go back to
sleep. This prevents accidental lid flex or slight opening in a backpack
from waking the machine up and leaving it resumed (powered on).

ok deraadt@


# 1.272 26-Sep-2014 kettenis

Rework piglet and pig allocation. Currently the piglet gets allocated
deep down in the suspend path, where it is really hard to recover from
allocation failure. So allocate the piglet early on in the suspend path.
Also change the piglet and piglet allocation functions to use km_alloc(9)
instead of doing pmemrange magic. This removes a bunch of code which, in the
case of the piglet allocation, is broken since it results in a NULL pointer
dereference. Also switch the piglet allocation to not wait. If we can't
allocate 16MB of phys contig memory on a halfway modern machine we're almost
certainly under a lot of memory pressure and we're better off not trying to
hibernate anyway.

ok mlarkin@


# 1.271 25-Sep-2014 kettenis

Bring back rev 1.249. Now that mp_setperf() has been fixed, it should be
safe to restore perflevel when resuming. Should fix the issues people
see with the processor running at full speed heating up the machine while
the apm command reports that the CPUs are running at a low speed.

ok dcoppa@, deraadt@


# 1.270 23-Sep-2014 kettenis

Release the acpi lock around calls to wsdisplay_suspend() and
wsdisplay_resume(). The X server may adjust the display brightness just
before/after the VT switch when suspending/resuming. In fact, it may
already be blocked adjusting the display brightness for some reason
unrelated to the suspend request.

The VT switch request will eventually time out, but the delay is annoying
and the failed VT switch will lead to problems after resume.

Needed for a future update of the xf86-video-intel driver.

ok mlarkin@


# 1.269 19-Sep-2014 kettenis

Use config_suspend_all(9).


# 1.268 08-Sep-2014 kettenis

Clear the PME Status bit on PCI devices in response to Device Wake events.
Fixes the interrupt storm on the 2009 Mac mini with WOL enabled on its
nfe(4) interface.

ok mlarkin@


Revision tags: OPENBSD_5_6_BASE
# 1.267 20-Jul-2014 mlarkin

Support hibernating to softraid crypto volumes.

much help and ok from deraadt@


# 1.266 16-Jul-2014 mlarkin

Reenable hibernate RLE support and flush+zero all memory after unpack.


# 1.265 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.264 12-Jul-2014 kettenis

Add a function to drop all clean pages on the page daemon queues and call
it when we hibernate.

ok mlarkin@, miod@, deraadt@


# 1.263 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.262 11-Jul-2014 mlarkin

Flush the buffercache to 16MB on hibernate and restore its previous max
size (kern.bufcachepercent) on resume, for better hibernate performance.

ok beck@


# 1.261 10-Jul-2014 blambert

remove unneeded header

ok mlarkin@


# 1.260 01-Jul-2014 kettenis

Adding an additonal delay after each access to SystemMemory or SystemIO
during autoconf makes no sense.

ok deraadt@


# 1.259 23-Jun-2014 kettenis

SystemMemory is used to access memory mapped registers on some machines, so we
must use the bus_space(9) API to access it instead of memcpy(9). Also make
sure we properly align access to fields that need more than byte alignment.

ok guenther@


# 1.258 21-May-2014 mlarkin

Fix invalid / missing format specifiers in several dnprintf statements
(used only when ACPI_DEBUG is enabled)

From a diff on tech@ by Fabian Raetz, with a minor change by me, tested
on i386 and amd64 with ACPI_DEBUG enabled and disabled.


# 1.257 25-Apr-2014 mlarkin

Defer the free of the hibernate structures until later, when it is safe to
do so.

Freeing the hibernate structures too early leads to inadvertent calls into
splx when we aren't ready for them yet. Found when trapping splx calls in
the resume path.

discussed with deraadt@ at m2k14


# 1.256 13-Apr-2014 mlarkin

Fix a badly worded debug message that implied the entire machine was going
to sleep, instead of just the acpi thread going to sleep.


# 1.255 31-Mar-2014 mpi

Change the order of operations during a suspend/resume cycle and
call bufq_quiesce() after executing the DVACT_QUIESCE handlers.

This should be safe since no disk nor controller drivers have such
handler but it will allow us to detach sd(4) devices attached to a
USB bus.

Another benefit pointed out by kettenis@ is that drivers that need
to read a firmware from the disk should be able to do it at resume
time in a DVACT_WAKEUP handler.

ok kettenis@, deraadt@


# 1.254 13-Mar-2014 dlg

get rid of the assumption that the head of the alldevs list is the
"mainbus" device. this breaks when mpath is enabled because it
attaches before mainbus and therefore takes the head position.

have autoconf provide device_mainbus() which looks up mainbus_cd,
and use that instead.

discussed with deraadt who just wants mpath stuff to move forward
despite there being many ways to shine this particular turd.


Revision tags: OPENBSD_5_5_BASE
# 1.253 21-Feb-2014 deraadt

Do the lid control further on in the resume sequence, since this is
AML, and will do WAITOK operations. While cold, that is a bad idea.
We seem to be safe with respect to the spec, I do not see any event
debounce issues here.
ok kettenis, tested by many


# 1.252 20-Jan-2014 claudio

Instead of checking sc->sc_revision <= 1 check that the
fadt->hdr_revision <= 1. This is a better check since it checks
the version of the proper descriptor. This allows the alix APU
to reboot since those boards have currently a wrong sc->sc_revision.
Committing this now so that it can be tested easier.
With and OK jsg@ also fine by derradt@


# 1.251 24-Dec-2013 kettenis

If the FADT has its SMI_CMD set to zero, assume we're only ACPI-only hardware
and don't need to disable SMI ownership of the ACPI hardware registers.

ok mlarkin@


# 1.250 23-Dec-2013 kettenis

Revert previous commit. It makes resume hang on some MP systems running
GENERIC.MP.


# 1.249 16-Dec-2013 kettenis

Restore hw.setperf upon resume like we do for apm(4) on i386. Seems we need
to do this fairly late in the resume to avoid hangs.

ok deraadt@, mlarkin@


# 1.248 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


# 1.247 06-Nov-2013 mpi

Add support for Power Resources for Dx states and the necessary hook
for PCI devices. This hook should be called twice, before and after
changing the power state of a PCI device.

Before setting the device to the new state, the ACPI layer will notify
every power resources linked to the device for that state and make sure
they are turned "_ON". After changing the state of the device, it will
decrement the reference of every power resources linked to that device
for the old state and turn them "_OFF" if they are no longer referenced.

This fixes the no-USB after resume problem seen on various ThinkPad,
problem initialy diagnosed with Alexander Polakov.

ok kettenis@, deraadt@


Revision tags: OPENBSD_5_4_BASE
# 1.246 01-Jun-2013 mlarkin

Stop printing ACPI wakeup devices in dmesg after the 16th wakeup device.

Helps with VMware VMs that report hundreds of wakeup devices.

ok reyk@, deraadt@


# 1.245 31-May-2013 bluhm

Fix the build for a kernel without wd and pciide in its config.
OK deraadt@ tedu@


# 1.244 30-May-2013 deraadt

Enforce ca_activate tree-walks over the entire heirarchy for all events,
cleaning up some shutdown-hook related code on the way.
(A few drivers related to sparc are still skipped at kettenis' request)
ok kettenis mlarkin, tested by many others too


# 1.243 18-Apr-2013 deraadt

pin the acpi thread to the BSP, because AML and SMI cannot be trusted. I
was certain we were doing this already, but it turns out no...


Revision tags: OPENBSD_5_3_BASE
# 1.242 24-Dec-2012 guenther

In acpiioctl(), Return though the splx() path if suser() returns an error

ok deraadt@


# 1.241 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


# 1.240 04-Oct-2012 ehrhardt

Use information provided by ACPI to attach secondary PCI host bridges.

ok kettenis@


# 1.239 07-Sep-2012 kettenis

Add a new function called acpi_pci_min_powerstate() that returns the lowest
power state for a PCI device for the power state (currently S3 or S4) we're
transitioning into. This function is probably too simplistic; there is a lot
of (silly) complexity in the ACPI spec.

ok deraadt@


Revision tags: OPENBSD_5_2_BASE
# 1.238 13-Jul-2012 pirofti

Disable acpivideo(4) on ASUS laptops.

There are some models that panic whenever an event is received.
Some cope, some don't. In order to get this right acpiec(4) requires
major rework. Until then let machines boot.

Discussed with deraadt@.


# 1.237 11-Jul-2012 mlarkin

for now, restrict hibernate to root in the acpi ioctl kernel interface.
normal rules for controlling this interface through apmd still apply.

ok deraadt@


# 1.236 09-Jul-2012 deraadt

tiny little oops in untested hibernate block


# 1.235 09-Jul-2012 deraadt

clean up ioctl() case statements


# 1.234 09-Jul-2012 deraadt

acpi_disable_onegpe() is not used; ok pirofti


# 1.233 24-May-2012 kettenis

If _MIN > _MAX consider a WordBus resource to be invalid. Fixes a panic on
the IBM x3550-M4.

ok mikeb@


# 1.232 29-Mar-2012 mlarkin

Missing #ifdef in previous diff


# 1.231 29-Mar-2012 mlarkin

Prevent hibernation attempts on machines with unsupported disk controllers.
The same check is also performed later in the hibernate sequence, but by
then it's too late to cleanly unwind (presently).

ok deraadt


# 1.230 26-Mar-2012 deraadt

simplify conditionals in acpi_sleep_state()
ok mlarkin


# 1.229 26-Mar-2012 deraadt

hook in the hibernate request code; half of this diff is from mlarkin
ok mlarkin


Revision tags: OPENBSD_5_1_BASE
# 1.228 20-Sep-2011 deraadt

Very early in suspend while the clock is still ticking realtime,
save the clock back to the rtc. The zaurus already did this.
ok phessler miod kettenis


Revision tags: OPENBSD_5_0_BASE
# 1.227 02-Jul-2011 nicm

kqueue attach functions should return an errno or 0, not a plain 1. Fix
the obvious cases to return EINVAL and ENXIO.

ok tedu deraadt


# 1.226 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.225 16-Jun-2011 pirofti

Import acpitoshiba driver.

This is originally written by Hiroyuki Aizu, ported and integrated
in our tree by Javier Vazquez <javama4 AT gmail> with tweaks and
suggestions by me.

Import prodded by deraadt@ and the lack of testers on tech@.


# 1.224 27-Apr-2011 jcs

attach acpithinkpad to newer lenovo models like the x120e

ok deraadt@


# 1.223 22-Apr-2011 jordan

Fix uninitialzied variables and formatting strings (-Wxxx errors)


Revision tags: OPENBSD_4_9_BASE
# 1.222 02-Jan-2011 jordan

Re-add code for ACPI taskqueue, eliminates need for state vars in acpi softc
Serialize processing ACPI tasks for sleep/powerdown/GPE event
ok deraadt@


# 1.221 31-Oct-2010 guenther

Revert last commit: it breaks resume on Thinkpads
"then please back it out" deraadt@


# 1.220 26-Oct-2010 jordan

Add task queue for ACPI gpe and notify handlers
This fixes eject on Dell Latitude dock and an issue on the Dell Mini
battery update.
ok deraadt


# 1.219 07-Oct-2010 jordan

Create separate functions for enabling wake and runtime gpes.
Remove old suspend/resume gpewalk function
ok deraadt, mlarkin


# 1.218 05-Oct-2010 jordan

split acpi_enable_onegpe to separate enable/disable functions
ok deraadt


# 1.217 05-Oct-2010 jordan

Added acpi_disable_allgpes function to clear GPE status
ok deraadt


# 1.216 05-Oct-2010 deraadt

clean up acpi_softc use more


# 1.215 05-Oct-2010 deraadt

delete a bit of #if 0 code


# 1.214 05-Oct-2010 deraadt

hide use of our global acpi_softc pointer a bit better


# 1.213 29-Sep-2010 deraadt

grab the acpi thread lock very early in acpi thread startup; not that it
is likely that anyone will race us... but why be careless.
ok kettenis


# 1.212 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.211 29-Aug-2010 jasper

- add a check for FWRITE here too.

ok miod@


# 1.210 27-Aug-2010 kettenis

Make PCI Power Management optional, and only enable it when acpi(4) attaches.

ok deraadt@


Revision tags: OPENBSD_4_8_BASE
# 1.209 08-Aug-2010 kettenis

Close race between the acpi thread and wscons ioctls running in process
context. We might rip this out post-release and replace it with a solution
that serializes things in a more obvious way.

ok deraadt@, miod@ and "those crazy texans who ok anything"


# 1.208 08-Aug-2010 deraadt

Report the acpi sleep states when on a RAMDISK; ok krw


# 1.207 07-Aug-2010 kettenis

Enforce acpi register access rescrictions.

ok mlarkin@, deraadt@, tested by marco@


# 1.206 07-Aug-2010 deraadt

upon resume, notify all the acpiac and acpibat drivers to update
their status. some laptops do not do this automatically.
ok kettenis canacar


# 1.205 06-Aug-2010 marco

Always call _PSW on _LID devices that support it to enable lid open to
resume the machine. Conversely disable it when the machine wakes up.

Tested by several ok deraadt


# 1.204 06-Aug-2010 deraadt

As part of a revamp of the PM1/GPE code, I write this diff to look for
unmanaged EN & STS bits in the PM1 register at interrupt time and report
them. As a side effect this splits the STS acknowledgement into two writes
(for power, and sleep) instead of one. The printf that is added (to spot
unmanaged STS bits) has not yet been triggered as far as we know.

Before the "write to PM1 registers at the right offset" diff went in, this
was not neccessary. But newer thinkpads do not have a working soft power
button without this diff.

We have no idea why.
ok mlarkin kettenis


# 1.203 05-Aug-2010 deraadt

PM1 power button events were generating 'fake events' to acpibtn PWRB,
but if there is no such device these were being tossed. Instead, arrange
for all power button handlers to flag a softc variable which is processed
by the thread; so that one place will know to request a powerdown (psignal
to init)
ok marco mlarkin


# 1.202 05-Aug-2010 deraadt

rename a variable which is annoyingly poorly named


# 1.201 05-Aug-2010 mlarkin

Write to the correct offset for various ACPI pmreg registers. Writing to
the correct register does properly clear the wake status bits and fixed
event bits, which allows some machines to have more than 1 suspend cycle.

bug spotted by deraadt@

tested by deraadt@ and myself on a half-dozen machines and others on
hackers@

ok deraadt@, marco@


# 1.200 05-Aug-2010 deraadt

Fix the power button. The pm1 register is 16 bits in size.
My mistake, a while back.
tested by krw


# 1.199 04-Aug-2010 kettenis

Print supported sleep states.

ok marco@, phessler@, deraadt@


# 1.198 03-Aug-2010 mlarkin

Use the proper flag for re-enabling certain hardware events (power btn,
sleep btn). The (incorrect) flag prevoiously used coincidentally had the
same value, so this shouldn't cause any different behavior than before.

tested on a variety of machines (i386, amd64, sp, mp) - no behavioral
change seen

ok deraadt@


# 1.197 28-Jul-2010 marco

Make all gasio failures look and behave the same.


# 1.196 28-Jul-2010 mlarkin

Delay a little longer on suspend before giving up. Fixes machines that
sometimes don't suspend because the sleep transition takes too long
(symptoms like failing to sleep but the backlight stays on).

ok deraadt@


# 1.195 27-Jul-2010 deraadt

remove XXX; EC can now be attached early when needed; ok jordan


# 1.194 27-Jul-2010 deraadt

more brackets feels good


# 1.193 27-Jul-2010 deraadt

XXX annotate a block in acpi_enter_sleep_state() that is pretty worrying


# 1.192 27-Jul-2010 deraadt

horrific indentation that nearly made me puke


# 1.191 27-Jul-2010 mlarkin

Set the sleeping indicator light on machines that support it, in case SMI
doesn't do it for us. Shows the 'moon' LED on older Thinkpads when
sleeping.

Tested on T43p, W500, T510 by me and on X61s by deraadt@.

ok deraadt@


# 1.190 27-Jul-2010 jordan

Early initialization of acpiec if ECDT table exists
Fixes hang when booting thinkpads while docked
ok deraadt


# 1.189 26-Jul-2010 deraadt

remove merge error which was commited


# 1.188 26-Jul-2010 pirofti

Add support for Sony ACPI hotkeys via a new driver: acpisony(4).

Currently it only works for the suspend button and tries to do right for
the brightness events, but I haven't found a Sony laptop that like to do
right so far.

In the future I want to make the brightness keys work on all Sony's and
also add support for the zoom hotkeys and whatever other funky keys I can
find on those things.

Okay deraadt@.


# 1.187 22-Jul-2010 deraadt

pretty large cleanup of SMALL_KERNEL


# 1.186 21-Jul-2010 deraadt

in the acpi GPE handler, late ack level interrupts not edge interrupts.
Duh.


# 1.185 21-Jul-2010 deraadt

Merge the edge and level gpe handlers into one (now possible since the
gpe structure tells us whether the gpe is edge or level)


# 1.184 21-Jul-2010 deraadt

delete #if 0 code we are not going to use; ok marco


# 1.183 20-Jul-2010 deraadt

make the mask code for acpi_enable_onegpe() match other similar code


# 1.182 20-Jul-2010 deraadt

Handle the acpi interrupt controller with proper edge/level handling,
wrapped in spltty to avoid racing against the interrupt controller.
Repair the gpe bit masking code while there, and do operations in the
right order
ok kettenis mlarkin, help from jordan at figuring out the order of
operations.


# 1.181 20-Jul-2010 matthew

When we're preparing to go to sleep, only call bufq_restart() if other
preparations failed.

ok kettenis@, thib@, deraadt@


# 1.180 19-Jul-2010 deraadt

permit compilation with ACPI_DEBUG


# 1.179 19-Jul-2010 deraadt

there is no need for a seperate dsdt_softc pointer; dsdt does not have a
softc but is just a part of the single acpi driver.
ok jordan


# 1.178 19-Jul-2010 deraadt

acpi_delay() is no longer needed; ok jordan


# 1.177 18-Jul-2010 mlarkin

Clear event status on resume, to avoid going back to sleep/reboot/shutdown
immediately after resume. Tested by a bunch of people on hackers@ on
various hardware. Fixes at least Toshiba NB200 and Gateway LT3103u.

ok marco


# 1.176 13-Jul-2010 deraadt

rename some thread related interfaces so they are logical


# 1.175 10-Jul-2010 jordan

Verify _ADR bdf is correct (some systems return 0xffff)
Works on Dell x9xx and Studio 1555


# 1.174 09-Jul-2010 jordan

Clear the noise


# 1.173 08-Jul-2010 jordan

Add mapping for ACPI device to PCI bdf (match autoconf tree)
Simplify resource parsing function to use buffer argument
Convert namespace linked lists to use queue macros
ok marco@, deraadt@


# 1.172 06-Jul-2010 deraadt

Use spltty() to lock downcalls from apm against the information being
modified by the acpi thread.
ok kettenis


# 1.171 06-Jul-2010 deraadt

Don't use a workq to do the suspend, because it races aginst the acpi
thread. Instead, just tell the acpi thread to do the suspend for us.
This makes apmd & zzz work correctly. While here, have acpithinkpad
attempt to post the event to apm before prompting the suspend itself.
ok kettenis marco mlarkin


# 1.170 05-Jul-2010 mlarkin

Reset register SCI_EN on resume. The ACPI spec clearly states we don't have
to do this, but many machines don't, or forget to, which leads to all sorts
of problems on resume (broken battery stats, overheating, failure to resume
more than once, etc).

tested by many, ok marco@, deraadt@


# 1.169 01-Jul-2010 thib

do bufq quiesce again, since vnd/the bufq_done call in
biodone has been fixed.


# 1.168 01-Jul-2010 jordan

Backout recent AML changes, breaks on R210 and others


# 1.167 01-Jul-2010 jordan

Simplified aml_parse_resource call, cleanup code
ok marco


# 1.166 01-Jul-2010 jordan

Fix subordinate bus # for multi-root PCI buses
ok marco, kettenis


# 1.165 30-Jun-2010 thib

Disable/partially backout the bufq quiesce changes as this
is causing havoc with vnds and release must be buildable.


# 1.164 29-Jun-2010 deraadt

Fix ramdisks (new code was placed inside a SMALL_KERNEL chunk)


# 1.163 29-Jun-2010 jordan

Add support for mapping ACPI to PCI devices
ok kettenis, deraadt


# 1.162 29-Jun-2010 kettenis

Call bufq_quiesce() on suspend to stop all I/O and bufq_restart on resume.
Makes suspend/resume work much better while doing disk I/O.

ok mlarkin@


# 1.161 29-Jun-2010 tedu

60 is a more reasonable guess for number of minutes in an hour than 100.
spotted by TeXitoi. ok deraadt


# 1.160 27-Jun-2010 jordan

Add _acpi_matchhids routine, precursor to pci device match


# 1.159 27-Jun-2010 mlarkin

Clear ACPI fixed event status on resume (power buttons/etc) so that some
machines don't immediately resume after suspending to S3.

Tested by kettenis@ and I on 5 or 6 machines, deraadt@ "yeah, looks good"


# 1.158 27-Jun-2010 jordan

Cleaned up acpi_maptable, adds to table list directly


# 1.157 07-Apr-2010 deraadt

If we have done a wsdisplay_suspend(), and ACPI decides it does not want to
suspend, we must roll back by calling wsdisplay_resume() or the virtual
console switching will be locked forever
ok oga


# 1.156 07-Apr-2010 kettenis

Make suspend/resume work on MP machines (running an MP kernel). Joint work
from mlarkin@ and me, with some amd64 fixes thrown in by deraadt@

ok marco@, deraadt@, pirofti@, mlarkin@


# 1.155 31-Mar-2010 kettenis

Bring /dev/apm support in line with apm(4). Make the suspend button send
a suspend request event to apmd(8) instead of suspending immediately. Also
keep track of whether /dev/apm and /dev/apmctl are currently open, such that
we can still suspend immediately if apmd(8) isn't running.

ok deraadt@, marco@, pirofti@, jsing@, oga@


# 1.154 30-Mar-2010 oga

Prevent the apmd/x races for good.

When we hit suspend time, go through all wsdisplays on the system. if
they are in mode MAPPED, but not MODE_DUMBFB then if possible do a full
vt switch to a !mapped vt, and prevent switching back until resume time.
This has to be called from MD code because this involves userland
running so that X can run the vt switch signal handler. This way, any
case where we are using the "poke registers from userland" model, we
will not be on the hardware when we go down, so the kernel can actually
handle thing properly.

Tested on several acpi laptops (by kettenis@ and ian@), x40 (me and
beck@ at LEAST) and zaurus (me). Maybe others, but if so I forgot who at
this time..

Idea from deraadt somewhere over the Faroe Islands (I thought of a
similar thing myself a while ago). Much prompting from him. Ok and
comments miod@


# 1.153 25-Mar-2010 oga

Fix & vs. && in the kqfilter code.

Based on a comparison with the apm code.

ok deraadt@, kettenis@


Revision tags: OPENBSD_4_7_BASE
# 1.152 26-Nov-2009 mlarkin

Visual (and on some machines, audio) feedback while the machine is
resuming via calls to aml _SST method. This also makes the "moon" LED on
thinkpads to blink during resume and cease when resume is completed.

ok pirofti@


# 1.151 26-Nov-2009 deraadt

Toggle into "cold" when we we disable interrupts, because tsleeping
in the AML interpreter and context switching into userland processes
tends to sometimes pop the ACPI trampoline page out of our address
space. Makes lots more machines work.
ok kettenis mlarkin


# 1.150 24-Nov-2009 jsg

in the resume path evaluate _WAK before dealing with device drivers
like the spec says, not after.

ok mlarkin@


# 1.149 24-Nov-2009 deraadt

abort suspends on SMP systems until that gets written; ok mlarkin


# 1.148 23-Nov-2009 mlarkin

pci should return the result of it's children's suspend/resume calls back
to its parent.

handle suspend failure case and unwind if devices have an activate function
that returns failure.

ok deraadt@, kettenis@


# 1.147 23-Nov-2009 mlarkin

acpi* devices need to call their children on suspend/resume.
ok deraadt@


# 1.146 23-Nov-2009 pirofti

Remove ACPI_SLEEP_ENABLED checks.

This enables by default the suspend/resume paths in the kernel.

Okay deraadt@.


# 1.145 23-Nov-2009 deraadt

knf and spacing that are super annoying


# 1.144 23-Nov-2009 mlarkin

Hook up Theo's new children suspend/resume function, as well as more ACPI
suspend/resume glue.

ok deraadt@


# 1.143 26-Oct-2009 deraadt

Do not do strncmp comparisons on dv_xname because longer device names which
look similar could arrive in the future. Instead, compare directly against
dv_cfdata->cf_driver->cd_name
Issue originally spotted by miod


# 1.142 26-Oct-2009 jordan

Verify checksum+address when loading ACPI tables.
Some systems had invalid entries in RSDT/XSDT.
ok marco@


# 1.141 23-Jul-2009 cnst

New aibs(4) driver for ASUSTeK AI Booster (ACPI ATK0110) hardware monitoring,
with sensor state support through limits provided by the ACPI.

Tested on several ASUS motherboards kindly networked by Sam Fourman Jr.

ok deraadt marco jordan


Revision tags: OPENBSD_4_6_BASE
# 1.140 03-Jun-2009 pirofti

Import acpi power resource driver. This is needed for suspend/resume logic.

Okay jordan@ and marco@.


# 1.139 03-Jun-2009 pirofti

Add acpivideo support. This adds brightness support for all laptops
except thinkpads, they will use the acpithinkpad driver. The driver is
also hooked into wsconsole. So brightness can be adjusted via:

$ wsconsctl display.brightness=<percentage>

This is very helpfull on some laptops that have a nasty bios and get two
steps instead of one when pressing the brightness button.

Tested on various dell, fujitsu, acer, samsung and other laptops.

Okay marco@, miod@. Suggestions from kettenis@.
Lots of reviews and help from miod@, thanks!


# 1.138 03-Jun-2009 jordan

Added ejectable bay for IDE cdroms
ok marco@


# 1.137 30-Apr-2009 marco

Oga changed apm ioctls and forgot to grep. Temporary work around by hitting
the sleep button when we get any request. This interface will chnage at
some point.
ok oga


# 1.136 19-Apr-2009 krw

Call acpi_sleep_walk() when powering down just like when entering
any other sleep state. This tells acpi wake devices to not wake up
the box until manual powering up resets their status. Makes my Dell
GX520 stay powered off in response to 'halt -p'.

In snaps for a while.

ok marco@ (several times) deraadt@


# 1.135 17-Apr-2009 pirofti

Add device ranking support for future sleep/wake developments.

okay marco@


# 1.134 11-Mar-2009 jordan

Cleanup.. use aml_evalinteger instead of aml_evalname
ok marco@


# 1.133 10-Mar-2009 jordan

Fix cut/paste error.. call _BFS not _PTS
ok marco@


Revision tags: OPENBSD_4_5_BASE
# 1.132 21-Feb-2009 marco

overzealous cut/paste; found by Vladimir Kirillov <proger@uaoug.org.ua>


# 1.131 20-Feb-2009 marco

Oops didn't mean for Debugger() to remain there.


# 1.130 19-Feb-2009 marco

suspend/resume bits so that we can develop this in tree. This is disabled.
code from mlarkin and me
help from art,toby,jordan and several others
ok jordan, go for it deraadt


# 1.129 10-Feb-2009 jordan

Fix null pointer for shutdown if acpi not enabled
ok marco@


# 1.128 04-Feb-2009 kettenis

Some systems that claim to be ACPI 2.0 apparently don't provide an XSDT
address. Make the code fall back on the RSDT on those systems.

From Joerg Niendorf <p3y80@internode.on.net>. Fixes PR 5986.


# 1.127 17-Jan-2009 krw

A couple of stray malloc/memset -> malloc(M_ZERO), a bit of knf and
lose a couple of unnecessary casts. No functional change.

Feedback from kettenis@

ok kettenis@ marco@


# 1.126 06-Nov-2008 marco

Collapse all sanity checks of hids into one function.

Originaly from jordan


Revision tags: OPENBSD_4_4_BASE
# 1.125 02-Jul-2008 fgsch

add acpivideo. for now almost an empty skel so others can work on
suspend/resume. eventually it will also manage output switching and
brightness where supported. prodded by marco@


# 1.124 11-Jun-2008 marco

Fix $OpenBSD$ for once and for all


# 1.123 08-Jun-2008 deraadt

aaa_dev was being set from an uninitialized variable; ok toby


# 1.122 07-Jun-2008 marco

Kill ACPIVERBOSE. It was never used and is worthless to boot.

comments jmc
ok toby


# 1.121 06-Jun-2008 marco

Shrink acpi some more for floppies and while here re-enable acpi on the
floppies. Fixes crazy sony vaio kettenis has.

tested by deraadt
ok deraadt jordan kettenis


# 1.120 01-Jun-2008 marco

Fix \n nit in SMALL_KERNEL.


# 1.119 01-Jun-2008 marco

Re-enable new parser. Lots of testing by various folks. Fits all media
now too.

ok kettenis deraadt


# 1.118 16-May-2008 dlg

back out jordans new parser.

it didnt have enough oks from the right people, it did break machines
that were previously working, and it bloated the RAMDISKS and install
media beyond the constraints of the media.

requested by deraadt@


# 1.117 14-May-2008 jordan

Adding new ACPI Parser code
Old guts of code still exists, needs to be torn out next
ok marco@
ok brad@


# 1.116 27-Apr-2008 jcs

add acpithinkpad(4), an acpi support driver for ibm/lenovo
thinkpads.

provides functionality that the bios did on older, non-acpi
thinkpads like respond to screen brightness keys and toggle the
bluetooth device on and off.

ok deraadt@ and marco@


# 1.115 24-Apr-2008 jsing

Introduce acpiasus(4), a driver for the ACPI based hotkeys found in many
ASUS laptops (including the ASUS EeePC) - largely based on NetBSD's
asus(4) driver. On the ASUS EeePC this allows us to enable/disable
wireless, change screen brightness and use the volume keys.

ok jsg@, weingart@


# 1.114 12-Apr-2008 kettenis

Get rid of acpi_s5 global variables; simply send SIGUSR2 instead of SUGUSR1
to tell init(8) to power down the machine.

ok krw@


Revision tags: OPENBSD_4_3_BASE
# 1.113 21-Jan-2008 jsg

Check against RESET_REG_SUP in FADT before doing the acpi reset dance
after all, not doing so appears to break more machines than it fixes.

ok marco@


# 1.112 05-Jan-2008 kettenis

Never attach more acpicpu(4) devices than cpu(4) devices. Always attach
the first acpicpu(4) device on non-MULTIPROCESSOR kernels, regardless of its
ACPI processor ID.

ok mikeb@


# 1.111 05-Dec-2007 deraadt

Hang acpi(4) below bios(4) instead of mainbus(4). This lets us move acpi(4)
before pcibios(4), and then.... if acpi(4) attaches, skip pcibios(4) since
it messes the machines up. Fixes claudio's HP dl320 G5, and almost assuredly
others.
ok kettenis


# 1.110 05-Dec-2007 deraadt

repair acpiopen/acpiclose and friends to handle the /dev/apmctl interface,
so that apmd(8) can actually work correctly against acpi(4), and thus,
even do the fancy -C/-A stuff; also tested by jmc


# 1.109 02-Dec-2007 fgsch

fix the trailing space for real now.


# 1.108 02-Dec-2007 fgsch

avoid trailing spaces on output; marco@ ok.


# 1.107 27-Nov-2007 kettenis

Fix off-by-one that made us attach acpicpu0 and acpicpu1 on GENERIC kernels.

ok marco@


# 1.106 25-Nov-2007 jsg

When available and ACPI is enabled use the ACPI reset method
to reboot. This is required for some quirky sis machines
and likely others to reboot.

Tested by many. 'commit early commit often' deraadt@


# 1.105 19-Nov-2007 deraadt

sigh, amd64 still uses that stupid acpi_enabled variable...


# 1.104 19-Nov-2007 deraadt

softc's come initialized to 0, you know...


# 1.103 19-Nov-2007 deraadt

store revision; from jsg


# 1.102 19-Nov-2007 kettenis

Initialize sc_facs earlier.

ok deraadt@, beck@, canacar@


# 1.101 16-Nov-2007 deraadt

fix the bus_space #define nightmare, so that amd64 and i386 are much more
uniform. as a result shared code like acpi needs less #ifdef's
ok marco kettenis


# 1.100 15-Nov-2007 deraadt

remove the #ifdef ACPI_ENABLE. the EC codepath is either safe enough (and
will be improved), or it is unsafe because AML assumes it must be there
ok almost everyone


# 1.99 15-Nov-2007 mikeb

Make acpicpu(4) attach to the existing CPUs only.

Problem reported by form@. Fix idea by kettenis@.
Lots of help from deraadt@. Tested by deraadt@, form@ and me.

ok deraadt marco


# 1.98 14-Nov-2007 deraadt

spacing


# 1.97 12-Nov-2007 deraadt

correct : vs , in attach-time messages


# 1.96 06-Nov-2007 deraadt

all ACPI_ENABLE kernels need acpi_interrupt()


# 1.95 06-Nov-2007 deraadt

move acpi_read_pmreg() and acpi_write_pmreg() out of SMALL_KERNEL so
that acpi ramdisks can compile (obvious solution)


# 1.94 05-Nov-2007 canacar

Establish the acpi interrupt earlier. Fixes problems with some
GENERIC.MP kernels not getting acpi interrupts. This is a
workaround until the real issue with late establishment is fixed.
ok weingart@, beck@, gwk@


# 1.93 03-Nov-2007 jordan

Added support for displaying wakeup devices
ok beck@,weingart@,gwk@


# 1.92 11-Oct-2007 kettenis

When walking the tree yo find _PRT methods, check whether the device is
actually enabled and functioning. Similar to what we do for _INI.

ok weingart@, beck@


# 1.91 08-Oct-2007 krw

More simple memset(,0,) -> M_ZERO changes. In this batch move to
size(*p) as the first malloc() parameter where p is declared locally
and thus easy to check. Add M_ZERO to gpe_table allocation in acpi.c
even though there is no obvious bzero or memset nearby.


# 1.90 13-Sep-2007 mk

Remove unnecessary memset() (see aml_evalnode(9)) and a bit of KNF.

ok toby


# 1.89 13-Sep-2007 weingart

Implement ACPI 6.5.1 spec tree walk for _STA and _INI.
Fixes mk's laptop. No regressions so far (thank you to the
testers).

ok gwk@, mk@, marco@


Revision tags: OPENBSD_4_2_BASE
# 1.88 17-Apr-2007 mk

Now that acpidock no longer hangs machines during boot (at least
according to the reports I've been getting) it should be safe to enable
it again, so remove the #if 0 stuff around it.

ok marco


# 1.87 11-Apr-2007 jordan

Added changes for C-State
ok marco@


# 1.86 26-Mar-2007 marco

Calculate minutes left more accurately.

From giovanni <qgiovanni@gmail.com>


Revision tags: OPENBSD_4_1_BASE
# 1.85 03-Mar-2007 mk

Stop looking for acpidock until we fix that stuff that breaks all pre-60
series ThinkPads with acpidock(4) enabled.


# 1.84 22-Feb-2007 marco

We were getting no interrupts in .MP. There are 2 fixes; one in
acpi_machdep.c and move the interrupt establishment down after ioapic
has been initialized.

ok jordan


# 1.83 21-Feb-2007 kettenis

Make an acpi-enabled GENERIC.MP work on systems that only support PIC mode.

ok marco@


# 1.82 19-Feb-2007 jordan

Added acpi_map_address function for mapping GAS/IO registers
ok marco@


# 1.81 18-Feb-2007 jordan

Added dynamic perfect hash for opcodes
ok marco@


# 1.80 18-Feb-2007 marco

Shave off another K pre compression.
Shuffle SMALL_KERNEL functions around so that we only have a single #ifndef


# 1.79 17-Feb-2007 marco

Add SMALL_KERNEL throught acpi to make it fit on boot media.
ok deraadt


# 1.78 13-Feb-2007 marco

Add smarter _INI code. This helps with some dissapearing devices.

ok kettenis jordan


# 1.77 31-Jan-2007 gwk

Introduce acpi_hasprocfvs thats "ACPI Has Processor Frequency and Voltage
Scalling" a global flag set by acicpu attach to indicate that ACPI is
handling fvs through the _PSS objects. This will fix disappearing
EST/powernow when booting kernels with acpi enabled, as acpicpu isnt
compiled into GENERIC yet. Also in cases where acpicpu is built in and
legacy EST/powernow work but acpi is missing the _PSS object, the legacy
routines will be used.

tested by dim@, and Nick Nauwelaerts <nick AT nauwelaerts.net>
ok marco@, dim@


# 1.76 22-Jan-2007 mk

Actually look for docking stations by looking for _DCK nodes.

ok marco


# 1.75 02-Jan-2007 marco

On some systems EC must be attached as the first device. Move EC from
generic _HID devices to its own routine to force first attach. Bug found
and debugged by joris@

Code mostly written by joris@


# 1.74 26-Dec-2006 marco

Add polling to devices that require it such as acpibat and acpitz.
Use the same tsleep/wakeup mechanism as the interrupt code to keep it non-
concurrent.


# 1.73 21-Dec-2006 deraadt

even more knf love


# 1.72 21-Dec-2006 marco

Remove some leftover acpiec gpe/interrupt code.


# 1.71 20-Dec-2006 deraadt

oops, name already used


# 1.70 20-Dec-2006 deraadt

rename __acpi_enable_gpe(); ok marco


# 1.69 20-Dec-2006 deraadt

acpi_unmap_pmregs() is not used, ok marco


# 1.68 19-Dec-2006 deraadt

simplify some low-level methods substantially (shrinking code); ok marco jordan


# 1.67 06-Dec-2006 mk

malloc(..., M_NOWAIT) with no NULL check is bad. If allocation fails,
complain, clean up, and bail out of attach.

input and ok gwk


# 1.66 28-Nov-2006 jordan

Fixed clearing arg for acpi_init_pic
ok kettenis@


# 1.65 27-Nov-2006 kettenis

Don't spam dmesg.


# 1.64 27-Nov-2006 jordan

Fixes for acpiprt routing
Post-parse fixup for namerefs
ok kettenis@


# 1.63 25-Nov-2006 marco

Don't handle acpiev events inside the gpe handler since this causes deadlocks.

ok jordan@


# 1.62 15-Nov-2006 kettenis

Hook up acpi apic interrupt routing support to generic acpi code.

ok jordan@


# 1.61 02-Nov-2006 marco

Kill acpiec attach function and add its functionality to the generic _HID
device attach

prodded and ok jordan


# 1.60 02-Nov-2006 marco

Fix comments to reflect reality.


# 1.59 19-Oct-2006 marco

Replace ACPI ioctls with APM ioctls. This makes ACPI talk the same protocol
as APM therefore userspace can now retrieve power information in the same
fashion as all other apm arches (zaurus, macppc, i386 etc).

To use force the use of acpi device like so: apmd -f /dev/acpi

ok jordan@


# 1.58 19-Oct-2006 jordan

Fixed GPE debug levels
Fixed ref counter on temporary bufferfields
DSDT parsing fully working on IBM Thinkpad
ok marco@


# 1.57 15-Oct-2006 jordan

Removed old GPE handler code remnants
ok marco@


# 1.56 12-Oct-2006 jordan

Massive ACPI changes
Fixed ACPI leaks
new ACPI API
ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.55 30-Jun-2006 gwk

KNF, No functional change,
"go at it" marco@


# 1.54 30-Jun-2006 jordan

Support for calling Embedded Controller via new GPE interface
ok marco@


# 1.53 30-Jun-2006 jordan

Added new code for GPE handling
Also works with ACPIEC; need to fixup acpiec.c to remove original interrupt handler
ok marco@


# 1.52 30-Jun-2006 jordan

Added offset argument for acpi read/write pmreg routines (new GPE code)
ok marco@


# 1.51 31-May-2006 todd

do not call acpi_powerdown() unless acpi has successfully initialized
ok gwk@ canacar@


# 1.50 29-May-2006 canacar

Add embedded controller support.
ok marco@ deraadt@ testing & ok gwk@


# 1.49 19-May-2006 canacar

Minimal thermal zone driver. Monitors thermal zone temperature,
shuts down the system if the 'critical temperature' is reached.
ok marco@


# 1.48 11-Apr-2006 marco

KNF.


# 1.47 11-Apr-2006 gwk

Add acpi_resume(struct acpi_softc *sc), which calls some AML methods,
detected at attach time, currently unused.

ok marco@


# 1.46 11-Apr-2006 gwk

Discover some ACPI sleep related methods, and call them at the
appropriate time in acpi_enter_sleep_state().

ok marco@


Revision tags: OPENBSD_3_9_BASE
# 1.45 26-Feb-2006 marco

Add acpicpu as a device.

ok jordan@


# 1.44 23-Feb-2006 marco

remove dead function.

ok jordan@


# 1.43 22-Feb-2006 marco

Print out all tables it found during autoconf. We'll need this data in
the future.


# 1.42 22-Feb-2006 marco

Make dmesg less verbose.

ok jordan.


# 1.41 22-Feb-2006 jordan

Added parsing of SSDT tables
ok marco@


# 1.40 22-Feb-2006 jordan

Added new aml_register to notify by device id
Moved powerdown code to button device
ok marco@


# 1.39 21-Feb-2006 marco

Readjust debug levels that crept in too low over the last few days.
KNF


# 1.38 21-Feb-2006 marco

Rig the power button so that the machine shuts down correctly.


# 1.37 20-Feb-2006 marco

Add acpibtn device. Remains disabled for now.


# 1.36 19-Feb-2006 jordan

Added code for aml_notify GPE notification callback
ok marco@


# 1.35 19-Feb-2006 grange

Fix acpi_enter_sleep_state() by replacing current ugly hack
with mostly correct sleeping state transition code.

ok brad@ marco@


# 1.34 19-Feb-2006 marco

jordan:
Enable interrupts.
Added peripheral code to make this work.

marco:
Replace softintr and other types of interrupts with a handler with context.
Fixed interrupt magic for GPEs.
Some KNF

This is work done by jordan and I but has to be a single commit.


# 1.33 17-Feb-2006 marco

Remove earlier introduced delays. We really only need them during autconf.

ok jordan@


# 1.32 17-Feb-2006 marco

Add a generic delay function as per the ACPI spec.

from jordan@


# 1.31 17-Feb-2006 marco

Use tsleep instead of delay when !cold.


# 1.30 17-Feb-2006 marco

Argh, we need delays between reads/writes. The backend reads (i2c most likely)
are so slow that values come back as 0 sometimes. We have to do this
despite the spec saying we shouldnt have to. Guess having a faster
parser than others can hurt sometimes.

Idea from jordan.


# 1.29 17-Feb-2006 jordan

Fixed code for handling _INI
Rearranged debug statements
ok marco@


# 1.28 16-Feb-2006 jordan

Added stub for calling _INI
Fixed aml_dowhile for NULL return
ok marco@


# 1.27 16-Feb-2006 jordan

Fixed parser/evaluator to retrieve battery info+state
Cleanup of parser
Dell laptops return battery status
ok marco@


# 1.26 06-Feb-2006 brad

fix a typo so this will compile on amd64.


# 1.25 04-Feb-2006 marco

When ACPI_ENABLED wasn't set but the devices were it had an unknown variable.

Pointed out by ian@.


# 1.24 03-Feb-2006 jordan

Rewrote aml parser yet again...
Fixed bus_space_map for i386/amd64
Added _BST and _BIF to acpibat to get status/info
ok marco@


# 1.23 20-Jan-2006 jordan

More evaluator changes
Removed old evaluator code
Fixed _HID value
ok marco@


# 1.22 19-Jan-2006 jordan

Added support for read/write of PCI Config space
Help jason@
ok marco@


# 1.21 18-Jan-2006 jordan

Added new evaluation routines
Added helper methods for buffer/field evaluation
Fixed broken indentation on dsdt.c

ok marco@


# 1.20 17-Jan-2006 jordan

Major overhaul of the aml parser. Evaluater temporarily disabled.

ok marco@


# 1.19 06-Jan-2006 grange

Check for ACPI control availability only if ACPI_ENABLE defined.


# 1.18 06-Jan-2006 grange

Informative error messages on attach, prettier dmesg.


# 1.17 06-Jan-2006 grange

printf -> dnprintf conversion for debug messages.


# 1.16 05-Jan-2006 grange

Find SLP_TYPx values and store them for future use in
acpi_enter_sleep_state().


# 1.15 05-Jan-2006 grange

Random KNF and cleanup


# 1.14 05-Jan-2006 grange

Provide symbolic names for the acpi sleeping states and use it.


# 1.13 28-Dec-2005 marco

Moved DEVNAME to a acpidev.h

ok jordan@


# 1.12 18-Dec-2005 sturm

add back comment about wrong code

ok marco


# 1.11 16-Dec-2005 marco

Remove test for kbd_reset. Prompted deraadt@, ok jordan@


# 1.10 16-Dec-2005 jordan

Added hooks for powerdown/power button press


# 1.9 16-Dec-2005 jordan

Cleaned up common power management register i/o
Added enter sleep state ioctl
Hooked power button interrupt
ok marco@


# 1.8 16-Dec-2005 marco

Remove autoconf chatter


# 1.7 16-Dec-2005 jordan

Moved DSDT AML parsing into parent ACPI driver
ok marco@


# 1.6 14-Dec-2005 marco

Add AC device glue.


# 1.5 14-Dec-2005 marco

Adjust debug printing levels to something more sane.


# 1.4 13-Dec-2005 marco

Add glue to attach devices to apci.


# 1.3 07-Dec-2005 marco

KNF


Revision tags: OPENBSD_3_8_BASE
# 1.2 10-Jul-2005 grange

Spacing cleanup.


# 1.1 02-Jun-2005 tholo

Start on a basic ACPI framework -- does not do much more than read out the
ACPI tables into kernel memory and attach ACPI and HPET timers currently.

In order to test this code, enabling the devices in GENERIC as well as
the ACPI_ENABLE option is needed. This code does not do any thermal
control yet, so this should be done with care depending on the platform.

In the tree so more people can contribute to making this more fully
featured.

Ok niklas@ grange@ tedu@


# 1.431 11-Jun-2024 kettenis

Avoid powering down PCI devices if we're rebooting. This makes some
machines (e.g. the t410) unhappy.

ok mglocker@


# 1.430 02-Jun-2024 kettenis

Power down PCI devices in S0 as well. Needed for suspend-to-idle.

ok mglocker@


# 1.429 29-May-2024 kettenis

Implement the guts for "suspend-to-idle" on amd64. This enables suspend
on machines that don't support S3. In its current state it doesn't save
a lot of power, but this should improve over time. Implementation of
wakeup methods is incomplete which means that some machine can't resume
at the moment.

ok mglocker@, mlarkin@, stsp@, deraadt@


# 1.428 13-May-2024 kettenis

Ignore button events in the first 10 seconds after resume. On some ACPI
implementations pressing the power button to wake up a machine will cause
a power button notification to happen for that button press This would
initiate a power down, which isn't the user's intent.

Based on earlier diffs from deraadt@ and James Cook.

ok mlarkin@, "I'm not opposed" cheloha@


# 1.427 13-May-2024 jsg

remove prototypes with no matching function
ok mpi@


Revision tags: OPENBSD_7_5_BASE
# 1.426 08-Jan-2024 kettenis

Implement RootPathString support in the LoadTable() AML function. Fixes
booting OpenBSD on some (ancient?) Hyper-V version.

Tested by Henryk Paluch
ok mlarkin@


Revision tags: OPENBSD_7_4_BASE
# 1.425 08-Jul-2023 tobhe

Move /dev/apm related acpi code to acpi_apm.c which is only built on
amd64 and i386. This is the first step towards a machine independent
safe sleep API.

tested by yasuoka@ bluhm@
ok deraadt@ kettenis@


# 1.424 07-Jul-2023 claudio

Add support for multiple batteries to acpithinkpad setchargestart and
setchargestop. With this laptops like x270 or x240 properly set the
thresholds for both batteries.
Tested by kn@ and jmatthew@
OK kettenis@


# 1.423 06-Jul-2023 deraadt

moving acpiioctl to a different file than acpiopen is not going to work
for ramdisks. noticed by anton.
this must be reconsidered.


# 1.422 05-Jul-2023 tobhe

Move acpiioctl to acpi_x86.c, it is only used up on i386 and amd64.

ok kettenis@ deraadt@


# 1.421 29-Jun-2023 dv

acpi(4)/acpibtn(4): use opt-in approach for wakeup GPEs

Previously, any discovered GPE that could be enabled for wake was
being enabled prior to entering ACPI-based S3 or S4. On some newer
machines, this caused S3-based suspend to break as some devices we
don't fully control (e.g. lack of driver) might be capable of waking
the system and will do so almost instantly if the GPE is enabled for
wake up (i.e. the so called "instant wake" issue).

This changes to a model of having device drivers explicitly opt
into using their GPE for wake up.

The first driver to explicitly toggle a GPE for wake is acpibtn(4).

Variations of the above have been in snapshots for 2 weeks, so
tested by many.

ok deraadt@


Revision tags: OPENBSD_7_3_BASE
# 1.420 15-Mar-2023 kettenis

Add missing nameref resolving for the _DEP() package elements that indicate
dependencies between ACPI devices.

ok dlg@, deraadt@


# 1.419 10-Feb-2023 visa

Adjust knote(9) API

Make knote(9) lock the knote list internally, and add knote_locked(9)
for the typical situation where the list is already locked.

Remove the KNOTE(9) macro to simplify the API.

Manual page OK jmc@
OK mpi@ mvs@


Revision tags: OPENBSD_7_2_BASE
# 1.418 13-Sep-2022 kettenis

Split out the code that collects data from acpiac(4), acpibat(4) and
acpisbs(4) for apm(4) and hook it up to the arm64 version of apm(4) on
systems with ACPI.

ok kn@


# 1.417 12-Sep-2022 kettenis

Add support for level-triggered GPIO events.

ok mlarkin@


# 1.416 10-Sep-2022 kettenis

Don't restrict GPIO events to pin numbers below 256. With the _EVT()
method events corresponding to larger numbers are possible.

Fixes lid state detection on the x13s.

ok patrick@


# 1.415 03-Sep-2022 kettenis

Make sure we don't suspend if we have no way to wake up the machine.
The idea is that device drivers that can wake up the machine register
themselves by calling device_register_wakeup(). To prevent regressions
on amd64, we let acpi(4) register itself as a wakeup device if the AML
for the machine defines wakeup devices. This may be refined in the
future.

This diff will prevent people from suspending their arm64 SBCs without
having a way to wake them up. For now the only driver that registers
itself is axppmic(4), which means that at this moment only SBCs with
an allwinner A64 or H5 SoC will actually support suspend/resume.

ok mlarkin@, deraadt@


# 1.414 10-Aug-2022 patrick

Provide the AML root in ACPI's softc so that drivers that need access
don't have to declare it using extern.

Suggested by and with feedback from kettenis@
Tested by deraadt@ on arm64, amd64 and i386
Tested by bluhm@ on amd64 and i386


Revision tags: OPENBSD_7_1_BASE
# 1.413 17-Feb-2022 jsg

reduce includes


# 1.412 10-Feb-2022 visa

Embed klist head in acpi_softc to avoid explicit malloc.

OK kettenis@


# 1.411 09-Feb-2022 deraadt

Move x86-only stubs used by kern/subr_suspend.c into dev/acpi/acpi_x86.c
for sharing between i386 and amd64.
ok mlarkin kettenis


# 1.410 08-Feb-2022 deraadt

The suspend/resume code is a sticky mess of MI, MD, and ACPI sequencing.
This splits out the MI sequencing, backing it with per-architecture helper
functions. Further steps will be neccesary because ACPI and MD are too
tightly coupled, but soon we'll be able to use this code for more architectures
(which depends on figuring out the lowest-level cpu sleeping method)
ok kettenis


# 1.409 04-Feb-2022 robert

set the APM_BATT_CHARGING state if the battery is being charged and try
to calculate the remaining time to be fully charged

ok jca@


# 1.408 02-Feb-2022 deraadt

acpi_addtask() calls malloc() w/ M_NOWAIT (because some calls come from
interrupt context), this however means occasional resource shortage will
result in callbacks registration failing, and unknown consequences for
the task-submitting caller.
Changing this to use pools with a low water mark, decreases the odds
of that problem occuring.
ok kettenis


# 1.407 01-Feb-2022 deraadt

GPE_DIRECT is calling the AML parser from interrupt context, which is
not permitted. Luckily nothing is using GPE_DIRECT anymore, so this code
can be deleted.
ok kettenis


# 1.406 26-Jan-2022 kettenis

An ACPI device needs to be both present and enabled for it to function.
So only attempt to attach hardware that has both bits enabled. This fixes
an issue where com(4) would attach for a disabled serial port leading to
misdetection of the hardware variant and a subsequent hang when /etc/rc
runs ttyflags -a.

ok anton@, deraadt@


# 1.405 12-Jan-2022 patrick

Make acpi_getpropint() return uint64_t, as ACPI integers are in fact that
wide and some _DSD properties depend on it.

ok kettenis@


# 1.404 04-Jan-2022 patrick

acpi_getprop() needs to actually make sure that we're looking at the
correct property. While there adjust acpi_getpropint() as well to
increase similarity with acpi_getprop().

ok kettenis@


# 1.403 16-Dec-2021 anton

Attach com over acpi on amd64. Some hardware uses a different interrupt
assignment compared to the the legacy one supported by com over isa.
This causes the console to halt once userland takes over as no
interrupts are received. The actual address and irq can be read from
ACPI, kettenis@ already added support for arm64 which paved the way for
amd64.

Some consoles that previously attached over isa are now expected to
attach over acpi.

Thanks to patrick@ for testing on arm64.

ok kettenis@


# 1.402 07-Dec-2021 kettenis

Fix parsing of SR_IRQ resource descriptors.

ok patrick@, anton@


# 1.401 02-Nov-2021 deraadt

knf


# 1.400 30-Oct-2021 deraadt

Change hw.perfpolicy=auto by default, at startup. If the system has AC
power connected (default is yes when no driver differentiates) then default
to 100% performance. On battery, use the existing auto algorithm (which is
admittedly somewhat unrefined).
This change overrides the system/BIOS speed and puts OpenBSD in control.
As this happens very early during boot, besides speedups in all usage usage
patterns, some surprises: unhibernate and sysupgrade times are cut in half.
note: on a few architectures, the setperf fn pointer is changed late, and
thus the auto algorithm stops timeing out. kettenis and i will look for
a solution.
in snaps for more than a week.
ok kettenis


Revision tags: OPENBSD_7_0_BASE
# 1.399 20-Jul-2021 mlarkin

Fix trailing whitespace in a few places.

No code change.


# 1.398 09-May-2021 kettenis

Some x86 machines advertise the "hardware reduced" ACPI feature, advertise
S4 and S5 support, but fail to populate the SLEEP_CONTROL_REG and
SLEEP_STATUS_REG descriptions in the FADT. An example of such a machine
is the ASUS Zenbook 14 UM433DA. Any attempt to powerdown the machine
will result in a kernel crash. It turns out that using the legacy ACPI PM
registers works fine on this machien. So fall back on those registers
if SLEEP_CONTROL_REG and/or SLEEP_STATUS_REG aren't provided.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_9_BASE
# 1.397 15-Mar-2021 patrick

Add acpi_iommu_device_map(), which replaces the DMA tag with one that
is blessed with IOMMU magic, if available. This is mainly for arm64,
since on amd64 and i386 the IOMMU only captures PCIe devices, as far
as I know, which uses the pci_probe_device_hook(). This though is for
non-PCI devices attached through ACPI.

ok kettenis@


# 1.396 07-Mar-2021 patrick

Pass standard DMA tag to acpi(4) table drivers.

ok kettenis@


# 1.395 27-Dec-2020 deraadt

acpi_map_address() cannot be wrapped by SMALL_KERNEL anymore, as
it is used by acpihpet.c


# 1.394 25-Dec-2020 visa

Refactor klist insertion and removal

Rename klist_{insert,remove}() to klist_{insert,remove}_locked().
These functions assume that the caller has locked the klist. The current
state of locking remains intact because the kernel lock is still used
with all klists.

Add new functions klist_insert() and klist_remove() that lock the klist
internally. This allows some code simplification.

OK mpi@


# 1.393 17-Dec-2020 kettenis

Fix some issues with referencing named ACPI nodes from Packages.
These references need to be resolved at runtime rather than when
they're parsed such that they pick up the right values for those nodes
which can be changed when for example _INI methods run.

The current approach is to replace these reference with a string that
names the node in question. The problem with that is that packages
can also contain normal strings. Which means that we need special
code that depends on the context where the package is used.

This diff takes a different approach by retaining a reference when
parsing. Code that uses a package will need to resolve this reference
but there is no ambiguiety anymore.

ok patrick@


# 1.392 05-Dec-2020 kettenis

Add support for the _EVT method to the GPIO support code.

ok patrick@


Revision tags: OPENBSD_6_8_BASE
# 1.391 27-Aug-2020 jmatthew

Reorder the acpi attach process so we look at Processor() CPU nodes
after Device() ones, since we should prefer the newer node type.
If we see any Device() nodes, don't attach acpicpu(4) to any Processor()
nodes if they're also present. This also makes acpitz(4) and
acpipwrres(4) devices attach slightly later.

ok kettenis@, also tested by jmc@


# 1.390 16-Aug-2020 gnezdo

Remove sc_maxgpe from acpi_softc which was never read from

Remove a dead conditional from acpi_init_gpes while there.

ok jcs@


# 1.389 21-Jul-2020 deraadt

acpi can use IPL_BIO (a low interrupt) since it only enqueues operations for
later processing. The use of a high interrupt will predate suspend/resume
efforts, we had to redesign acpi to be non-reentrant obviously
discussed with kettenis, in snaps for more than a week


# 1.388 24-Jun-2020 cheloha

kernel: use gettime(9)/getuptime(9) in lieu of time_second(9)/time_uptime(9)

time_second(9) and time_uptime(9) are widely used in the kernel to
quickly get the system UTC or system uptime as a time_t. However,
time_t is 64-bit everywhere, so it is not generally safe to use them
on 32-bit platforms: you have a split-read problem if your hardware
cannot perform atomic 64-bit reads.

This patch replaces time_second(9) with gettime(9), a safer successor
interface, throughout the kernel. Similarly, time_uptime(9) is replaced
with getuptime(9).

There is a performance cost on 32-bit platforms in exchange for
eliminating the split-read problem: instead of two register reads you
now have a lockless read loop to pull the values from the timehands.
This is really not *too* bad in the grand scheme of things, but
compared to what we were doing before it is several times slower.

There is no performance cost on 64-bit (__LP64__) platforms.

With input from visa@, dlg@, and tedu@.

Several bugs squashed by visa@.

ok kettenis@


# 1.387 10-Jun-2020 jca

Give userland a consistent battery and A/C state after resume

Previously we refreshed the state of acpiac(4) and acpibat(4)/acpisbs(4)
by queueing fake ACPI events to be processed by the acpi thread.
acpibat(4) using ACPIDEV_POLL meant that its status could be refreshed
first, resulting in an APM_POWER_CHANGE event sent to userland before
the state of acpiac(4) was up to date. Because of this, apmd(8) could
see A/C unplugged when it was in fact plugged, possibly leading to
a wrong decision.

Instead of using fake ACPI events, refresh the status of acpiac(4) and
acpibat(4)/acpisbs(4) right away at DVACT_WAKEUP time. To reuse the
existing refresh code of those drivers, make their ACPI *_notify()
handlers responsible for sending APM_POWER_CHANGE events instead. Also
queue an APM_POWER_CHANGE event right after resume to retain existing
behavior.

This fixes the consistency of data shown to userland, and makes those
drivers less special.

Initially discussed last year (!) with mpi@ at Miod's place.
Feedback and ok jcs@, deraadt@ agrees with the use of DVACT_WAKEUP here.


# 1.386 29-May-2020 deraadt

dev/rndvar.h no longer has statistical interfaces (removed during various
conversion steps). it only contains kernel prototypes for 4 interfaces,
all of which legitimately belong in sys/systm.h, which are already included
by all enqueue_randomness() users.


# 1.385 14-May-2020 kettenis

Bring back the acpi(4) changes. This time around only use the ACPI _CRS
information on ACPI 5.0 and later.

ok krw@, patrick@


# 1.384 11-May-2020 jca

More accurate remaining power reporting with disparate batteries

Overall remaining power was computed as the average of the remaining
power of each battery, in percents. This doesn't work with batteries of
different capacity, so use the total capacity and remaining power
instead.

Reported by lidstah, successful tests by lidstah and benno@, ok kettenis@


# 1.383 08-May-2020 kettenis

Move parsing of _CRS into acpi(4) and pass the parsed address and interrupt
information in struct acpi_attach_args.

ok mpi@


Revision tags: OPENBSD_6_7_BASE
# 1.382 14-Apr-2020 kettenis

Implement acpi_getprop(), the ACPI equivalent of OF_getprop().

ok patrick@


# 1.381 12-Apr-2020 kettenis

Add support for the _CCA method. This method indicates whether DMA is
cache-coherent or not. To implement this, acpi(4) gets two bus_dma tags
and passes the appropriate one when attaching devices based on _CCA.
On i386/amd64, where for all practical purpose DMA is always cache-coherent,
the two tags are the same. But on arm64 they are distinct.

ok patrick@


# 1.380 07-Apr-2020 visa

Abstract the head of knote lists. This allows extending the lists,
for example, with locking assertions.

OK mpi@, anton@


# 1.379 03-Apr-2020 mpi

Fix incoherencies of errors returned by various *kqfilter routines.

EOPNOTSUPP is returned when there's no "filterops" for a given fd.
EINVAL when the requested filter isn't supported by the "filterops".
ENXIO means the underlying device is no longer valid/present.

ok visa@


# 1.378 20-Feb-2020 visa

Replace field f_isfd with field f_flags in struct filterops to allow
adding more filter properties without cluttering the struct.

OK mpi@, anton@


# 1.377 31-Dec-2019 visa

Use C99 designated initializers with struct filterops. In addition,
make the structs const so that the data are put in .rodata.

OK mpi@, deraadt@, anton@, bluhm@


# 1.376 31-Dec-2019 mpi

Convert infinite sleeps to {m,t}sleep_nsec(9).

ok kn@


# 1.375 31-Dec-2019 jsg

include sys/sensors.h for sensor use
don't depend on indirect inclusion from acpidev.h


Revision tags: OPENBSD_6_6_BASE
# 1.374 07-Sep-2019 kettenis

Revert acpipci(4) changes. Too many systems are broken. We'll try again
after 6.6 as been released. The acpireg.h change stays behind.


# 1.373 28-Aug-2019 kettenis

Use ACPI information to attach PCI busses like we do on arm64. There are a
few additional quirks though, and attaching the PCI busses is delayed to
replicate the existing code more closely. That may be changed in the
future. Also tweak how we handle MSI support and respect to ACPI flag
that says we shouldn't attempt to use MSIs.

Some fallout is expected.

ok patrick@


# 1.372 27-Aug-2019 deraadt

On amd64 the bootup and resume paths for SP/BP/SP were not performing
msr/pat, clock-startup, firmware upgrade, fpu initialization, and cpu
soft-state initialization in the same order. That kind of chaos is
crazy and fragile. Improve the acpi resume call sequence and unify order
of the operations.


# 1.371 02-Jul-2019 jcs

acpi_set_gpehandler: turn edge arg into flags, add support for
passing GPE_DIRECT which directly executes the supplied callback and
clears a level interrupt rather than going through acpi_taskq which
can be slow when other driver callbacks are in the queue.

This should not be used for GPE handlers that need to do anything
ACPI-related.

ok kettenis


# 1.370 10-Jun-2019 kettenis

Print proper ACPI version number.

ok mlarkin@


# 1.369 08-Jun-2019 kettenis

When establishing the mapping between ACPI device nodes and PCI devices,
skip PCI host bridges and devices that aren't there.

ok patrick@


# 1.368 07-Jun-2019 kettenis

Introduce acpi_getsta() and use it to eliminate some duplicated code.

ok patrick@


# 1.367 12-May-2019 kettenis

Make sure the extended PM register descriptions actually exist before
using them. Fixes machines with ancient ACPI 1.0 (such as recent
hypervisors).

ok deraadt@


# 1.366 12-May-2019 kettenis

Add support for mapping ACPI PM registers using the "extended" gasio
representation from the FADT. Mostly fixes the Lanner NCA-1510.

ok lteo@, mlarkin@, deraadt@


# 1.365 11-May-2019 lteo

Fix incorrect error message.

ok kettenis@


# 1.364 08-May-2019 tedu

free size. the allocation is the header plus the length.


Revision tags: OPENBSD_6_5_BASE
# 1.363 04-Apr-2019 kettenis

Avoid potential null-pointer dereference. Found by Kent R. Spillner.

ok deraadt@, jsg@


# 1.362 20-Jan-2019 jsg

fix SMALL_KERNEL build after changes in rev 1.361
reported by naddy@ ok deraadt@


# 1.361 19-Jan-2019 tedu

Add a pwraction sysctl that controls what the power button does on acpi.
By default, nothing changes -- shutdown is initiated. But allows turning
power button into a sleep button if desired.
(grudging) ok from a few parties


# 1.360 26-Oct-2018 kettenis

Add acpipci(4) on amd64. For now this only calls the PCI-specific _OSC
method to let the ACPI implementation know what features we support.


Revision tags: OPENBSD_6_4_BASE
# 1.359 30-Aug-2018 kettenis

Move softbutton check before the check that skips attaching certain acpi
devices. Fixes regression caused by matching on _CID in addition to
matching on _HID.

ok matthieu@, mlarkin@


# 1.358 25-Aug-2018 kettenis

Match ACPI devices based on _CID if no match for _HID is found.

ok mpi@


# 1.357 19-Aug-2018 kettenis

Add support for multiple PCI segments. Only really implemented for arm64
for now as amd64/i386 firmware still caters for legacy OSes that only
support a single PCI segment.

ok patrick@


# 1.356 03-Aug-2018 kettenis

Let ahci(4) match on _CLS instead of _HID when attaching at acpi(4). Avoids
having to add many more _HID entries to the match table.

ok deraadt@, mlarkin@


# 1.355 10-Jul-2018 kettenis

Make legacy interrupts work in acpipci(4).

ok patrick@


# 1.354 05-Jul-2018 kettenis

Add acpipci(4), a driver that supports generic ECAM-compatible PCI host
bridges based on information provided by ACPI.

ok mlarkin@


# 1.353 04-Jul-2018 kettenis

Properly pass around the PCI "chipset tag" in acpi(4) and refactor
acpimcfg(4) to call an MD initialization functions that sets up a tag for
PCI ECAM.

ok guenther@, mlarkin@, krw@


# 1.352 01-Jul-2018 kettenis

Move acpi(4) attach glue into acpi_machdep.c.

ok guenther@, deraadt@


# 1.351 01-Jul-2018 kettenis

Add support for _DSD Device Properties.

ok mlarkin@


# 1.350 30-Jun-2018 kettenis

Add intr_enable() function, intended for MI use to amd64 and i386 and use
this in the acpi(4) suspend/resume code paths.

ok deraadt@


# 1.349 29-Jun-2018 kettenis

Use <stdint.h> types.

ok mpi@, jung@, krw@, deraadt@


# 1.348 29-Jun-2018 mlarkin

ACPI: Allow (with warning) GPE handler reassignment, instead of returning
a failure code.

ok kettenis


# 1.347 26-Jun-2018 mlarkin

knf


# 1.346 26-Jun-2018 kettenis

Let acpi(4) pass down the bus dma tag.

ok deraadt@


# 1.345 25-Jun-2018 kettenis

Reorganize acpi(4) code a bit in preparation for upcoming arm64 support.

ok deraadt@


# 1.344 20-May-2018 kettenis

Wake up the acpi thread after scheduling a gpio event.

ok mpi@


# 1.343 17-May-2018 kettenis

Sprinkle some #ifndef SMALL_KERNEL such that RAMDISK kernels cuild again.

pointed out by stsp@


# 1.342 17-May-2018 kettenis

Implement GenericSerialBus OpRegion support.

ok mlarkin@


# 1.341 27-Mar-2018 kettenis

Add acpicmos(4), a driver that implements SystemCMOS OperationRegion
access support. This fixes machines where the AML doesn't check whether
support for this OperationRegion type has been registered by the OS.

ok mlarkin@


Revision tags: OPENBSD_6_3_BASE
# 1.340 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.339 10-Feb-2018 deraadt

Syncronize filesystems to disk when suspending. Each mountpoint's vnodes
are pushed to disk. Dangling vnodes (unlinked files still in use) and
vnodes undergoing change by long-running syscalls are identified -- and
such filesystems are marked dirty on-disk while we are suspended (in case
power is lost, a fsck will be required). Filesystems without dangling or
busy vnodes are marked clean, resulting in faster boots following
"battery died" circumstances.
Tested by numerous developers, thanks for the feedback.


# 1.338 08-Feb-2018 deraadt

After flushing disks, try again to release all memory. We don't need to
save dirty memory to the hibernate space.


# 1.337 08-Feb-2018 deraadt

Toss all releaseable memory, because fragmentation can get in the way
of allocating the hibernate playpen.


# 1.336 08-Feb-2018 deraadt

sr_quiesce() is a new approach for ensuring that softraid drains
output to the disks.

This is part of a larger suspend/resume filesystem-safety diff, which
has been worked on for a couple of months already. Tests by job, krw,
beck, benno, and others. Sometimes even by snapshot users...


# 1.335 29-Nov-2017 kettenis

Add support for GPIO-signaled events.

ok mlarkin@


# 1.334 16-Nov-2017 jcs

Add PCI attachment for dwiic(4) needed by Intel 100 Series machines

ok kettenis


# 1.333 14-Nov-2017 kettenis

Add a few more PNP IDs to the skip lists.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.332 17-Aug-2017 stsp

Make dwiic(4) attach its ACPI parent dependencies.
This change makes the Asus E200HA keyboard work.

Original analysis and patch by Cesare Gargano
ok kettenis@


# 1.331 09-Aug-2017 dcoppa

Add "PNP0303" (8042 PS/2 Controller) to acpi_isa_hids[]

ok kettenis@


# 1.330 22-Jul-2017 jcs

acpibat: look for _BIX first before falling back on _BIF

ACPI 4.0 deprecated _BIF for battery status so some newer machines
are exporting _BIX which extends _BIF's package with a few extra
fields. When using _BIX, export the new discharge cycle count as a
sensor.

feedback from kettenis and anton
ok deraadt


# 1.329 20-Jul-2017 kettenis

Start a list of isa(4) device names such that we can avoid printing them
as "not configured".

ok mikeb@, millert@, deraadt@


# 1.328 08-Apr-2017 deraadt

Sensors are run as callbacks inside tasks. During suspend a sensor
could be running inside a driver that will be force-detached, or due
to tsleep end up disrupting the softstate/hardstate contract. At
suspend time, quisce all these callbacks by waiting for completion.
This issue has never been observed for real, but may be implicated in
suspend/resume failures.
ok kettenis guenther mlarkin


# 1.327 08-Apr-2017 deraadt

Two prototypes for acpi_maptable; merge to one place


Revision tags: OPENBSD_6_1_BASE
# 1.326 27-Mar-2017 deraadt

Now that hibernate_alloc() only has clean success/failure, don't
need to call hibernate_free() to clean up a partial mess.
ok mlarkin kettenis


# 1.325 13-Mar-2017 deraadt

allow ramdisks to build; ok jcs


# 1.324 12-Mar-2017 jcs

try to attach acpisbs first and if successful, don't attach acpibat


# 1.323 02-Mar-2017 natano

Add a new sysctl machdep.lidaction. The sysctl works as follows:

machdep.lidaction=0 # do nothing
machdep.lidaction=1 # suspend
machdep.lidaction=2 # hibernate

lidsuspend is just an alias for lidaction, so if you change one, the
other one will have the same value. The plan is to remove
machdep.lidsuspend eventually when people have upgraded their
/ets/sysctl.conf.

discussed with deraadt, who came up with the new MIB name
no objections mlarkin
ok stsp halex jcs


# 1.322 28-Feb-2017 natano

Untangle abstract sleep modes from ACPI sleep states. This paves the way
for further work in the area.

idea and ok deraadt
ok mlarkin


# 1.321 22-Feb-2017 jcs

add acpisbs, an acpi smart battery subsystem driver reading data
over smbus

currently disabled because it conflicts with acpibat

ok deraadt, kettenis


# 1.320 14-Jan-2017 kettenis

Revert aml_rdpciaddr changes; breaks several machines that were working
before.


# 1.319 11-Jan-2017 jsg

Increase the size of the buffers used for _HID and _CID strings to be
able to fit "Hyper_V_Gen_Counter_V1".

ok mlarkin@ deraadt@


# 1.318 08-Jan-2017 kettenis

Make aml_rdpciaddr() work for for devices behind bridges. The code now
uses the mapping between AML nodes and PCI devices that we establish.
Because _INIT methods may end up calling aml_rdpciaddr(), make sure we
create that mapping early. Also handle devices that aren't actually present.
These devices are now included in the mapping and reads will return an
all-ones patterm whereas writes are a no-op..

ok mlarkin@


# 1.317 25-Oct-2016 pirofti

Add the resource index to aml_parse_resource() callback function.

This is needed by an upcoming acpiec commit that handles machines breaking
the current ACPI specifications.

Change suggested by and ok kettenis@, guenther@


# 1.316 18-Sep-2016 guenther

If the APIC table (MADT) isn't present then ci_acpi_proc_id won't be set
when acpicpu tries to attach, leading to a NULL dereference. The ACPI
processor object on the Dells where this was reported is bogus and useless
anyway so apply the same test when deciding whether to attach to processor
objects as acpicpu would when matching them to cpus.

Problem reported by Eivind Eide (xenofil (at) gmail.com) and
Johan Huldtgren (johan+openbsd-bugs (at) huldtgren.com).
Testing by Eivind Eide.
ok kettenis@ mlarkin@


# 1.315 03-Sep-2016 naddy

Write the system time back to the RTC every 30 minutes.
This fixes the problem that long-running machines which were not
shut down properly would reboot with a badly offset system time.

hints and ok kettenis@


# 1.314 31-Aug-2016 mlarkin

Don't put a device in the wake device list if _STA says it's not there.

Fixes at least one machine that woke up from sleep / poweroff immediately.

ok kettenis@, tested by kettenis and martijn@


# 1.313 28-Jul-2016 kettenis

Store the acpi processor ID/UID in struct cpu_info, and use it to attach
acpicpu(4) drivers to the right cpu(4).

ok mlarkin@, guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.312 10-Jun-2016 kettenis

The power management timer has been made optional in ACPI 5.0A, so don't
attempt to attach acpitimer(4) if the timer isn't present.

Pointed out by jcs@

ok jcs@


# 1.311 08-May-2016 kettenis

Make sure devices listed by a device's _DEP method are attached before
we attach the device itself. The _DEP methode, introduced in ACPI 5.0,
is a hint that the device in question depends on OpRegion support from
the devices listed by _DEP, so we have to order them the proper way.
To prevent us from attaching those devices again when we encounter them
later walking down the device tree, keep track of our attempts to
attach a device driver to them using aflag in the aml_node structure.

ok guenther@


# 1.310 25-Apr-2016 pirofti

We attach acpihpet(4) and acpiec(4) without using the HID, so do not
report it in the dmesg.

OK deraadt@, mlarkin@.


# 1.309 02-Apr-2016 kettenis

Reduce dmesg spam by knocking out some acpi hardware IDs that don't need a
driver.

ok mpi@


# 1.308 29-Mar-2016 kettenis

Check if a device is present (using _STA) in acpi_foundhid. This prevents
us from reporting devices as "not configured" that aren't actually present,
and allows us to remove duplicated code from several drivers.

ok deraadt@, ok jsg@


# 1.307 29-Mar-2016 kettenis

Implement a cleaner way to prevent acpivout(4) from attaching if we have
a platform-specific method of controlling the backlight.

ok millert@, mlarkin@, jung@


# 1.306 28-Mar-2016 kettenis

Adding each and every new acpi device driver to acpi_foundhid() is
gettinga bit of the burden. And it really isn't how our autoconf
framework is supposed to work for busses that can be enumerated. All
the drivers already check for a matching _HID in their attach
function. So we can just drop the checks in acpi_foundhid() and let
the drivers sort themselves out.

Print a "not configured" line for devices for which we don't attach adriver.
This may be a bit much as there are devices for which we will not have a
driver. This will be removed if it causes too much dmesg spam.

ok deraadt@, mlarkin@


Revision tags: OPENBSD_5_9_BASE
# 1.305 17-Jan-2016 jsg

Take acpi_foundhid() outside of #ifndef SMALL_KERNEL so the ramdisk
kernel can find and attach dwiic(4) and sdhc(4).


# 1.304 17-Jan-2016 jsg

remove an unused global variable


# 1.303 14-Jan-2016 kettenis

Attach dwiic(4) on the Intel Bay Trail i2c controllers as well.


# 1.302 12-Jan-2016 kettenis

Improve emulation of PM registers on Hardware-redcued ACPI by emulating
SPL_TYPx, SLP_EN and WAK_STS using the new SLEEP_STATUS_REG and
SLEEP_CONTROL_REG registers. Makes the transition into S5 (powerdown) work
on the ASUS X205TA.

ok deraadt@, mlarkin@


# 1.301 12-Jan-2016 jcs

Add dwiic, a driver for the Synopsys DesignWare i2c controller found
on the Samsung ATIV Book 9 laptop. This initial version only
supports ACPI config/attachment.

Add ihidev, a HID-over-i2c driver largely based on uhidev. dwiic
handles attaching ihidev devices found in ACPI.

Add ims, a HID-over-i2c mouse/trackpad driver to get basic cursor
and button functionality from HID-compliant i2c trackpads.

ok kettenis deraadt


# 1.300 11-Jan-2016 kettenis

Make sdhc(4) attach to hardware IDs "80860F14" and "PNP0FFF".


# 1.299 09-Jan-2016 kettenis

Initial support for Hardware-Reduced ACPI. This skips the legacy mode checks
by emulating the PM_CNT register such that SCI_EN is always enabled. It also
skip attaching acpitimer(4) since the relevant registers are no longer there.

ok deraadt@, mlarkin@


# 1.298 06-Jan-2016 kettenis

Follow the spec and check the SCI_EN bit to see if ACPI has already
been enabled and don't bother enabling it again if it is. This should fix
systems that don't implement legacy mode and therefore set ACPI_ENABLE and
ACPI_DISABLE to zero.

ok mlarkin@, yasuoka@


# 1.297 23-Nov-2015 reyk

Back out the CRITICAL change, turn it back from 10% to 25%. Some
people with old/broken batteries didn't like this change, and the
underlying heuristics are wrong.


# 1.296 22-Nov-2015 reyk

The "CRITICAL" APM battery level, that is exported/faked by the ACPI
stack, is currently set to 25%. With 25% battery left, a typical
modern laptop will have at least one hour runtime left. It is an
arbitrary value, and other APM power layers in the kernel set CRITICAL
to something like 5% or 10%, so set it to 10% for now.

OK mlarkin@


# 1.295 28-Sep-2015 deraadt

In low-level suspend routines, set cold=2. In tsleep(), use this to
spit out a ddb trace to console. This should allow us to find suspend
or resume routines which break the rules. It depends on the console
output function being non-sleeping.... but that's another codepath which
should try to be safe when cold is set.
ok kettenis


# 1.294 06-Sep-2015 deraadt

clarify malloc arguments (base + length...)


# 1.293 28-Aug-2015 deraadt

fairly simple sizes for free(); ok tedu


# 1.292 20-Aug-2015 kettenis

Respect the access size when reading or writing to pci config space.
This version also makes sure writes are properly aligned, preventing
the panics seen with the previous incarnation of this change.

Fixes battery status passthrough in vmware, brightness keys on my x220
and who knows what else.

ok mlarkin@, deraadt@


Revision tags: OPENBSD_5_8_BASE
# 1.291 04-Aug-2015 deraadt

If we are setting ACPI_PM1_BM_RLD do it only once, and remember to
repeat this action at resume time.
ok mlarkin guenther


# 1.290 29-Jul-2015 deraadt

Something subtle in previous revision is locking up suspend or booting
on some laptops. Mailing lists threads mention x220 and Asus machines.
Reverting for now.


# 1.289 21-Jul-2015 kettenis

iRespect the access size when reading or writing to pci config space.
Fixes battery status passthrough in vmware and who knows what else.

Tested by many.

ok mlarkin@, deraadt@


# 1.288 17-Jul-2015 mlarkin

fix comments


# 1.287 26-Mar-2015 jsg

Use the 0x10 extended capability to determine if the trackstick
buttons are wired to the trackpad and need to be re-routed to the
trackstick. Without this change the buttons on 2015 thinkpads
get picked up as extended buttons that show up as scroll up/down.

Remove the X1 Carbon 2015 (LEN0048) and X250 (LEN0046) from the
top button area/soft buttons quirks list. Also avoid using the
quirk list entirely if the capability bit is set.

Based on work from jcs@ with feedback from mpi@
ok mpi@ jcs@


# 1.286 20-Mar-2015 kettenis

The ACPI standard says that a PCI function number of FFFF can be used to refer
to all functions on a device. This makes no sense in the context of accessing
PCI config space. Nevertheless stsp@ has a machine with AML that tries to
do config space reads from a device where _ADR encodes function number FFFF.
This is almost certainly buggy AML, but since Linux (and presumably Windows)
boot just fine on this machine we need to work around this. Linux effectively
treats function FFFF as function 7, which isn't present on this machine.
Instead we simulate a read from a non-existent device by returning a bit
pattern of all ones. Writes to function FFFF will still panic.

ok mlarkin@


# 1.285 16-Mar-2015 deraadt

Reduce use of SMALL_KERNEL in acpi, bringing acpiec driver and more gpe
handling into RAMDISK. This is now possible because the install media
has ample room. The goal is to reduce special cases where we may be
skipping (unknown) important operations...
ok mlarkin kettenis


# 1.284 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.283 07-Feb-2015 deraadt

New framework that allows hibernate to pass in entropy from it's fresh
boot.
ok mlarkin


# 1.282 06-Feb-2015 mlarkin

Fix a hibernate crash on some machines due to unmapping a page that
may not have been mapped previously (in the failure to hibernate case).

Also ensure that the lowmem ptp is mapped in all cases (not just MP).

ok kettenis


# 1.281 17-Jan-2015 deraadt

resume_randomness() before spinning up other cpus...


# 1.280 15-Jan-2015 jsg

Define a new wscons mouse type for Synaptics clickpad devices that lack
physical buttons. This will be used if the acpi pnpid for the mouse
matches a list provided by Synaptics found in Linux.

Combined with the xenocara changes this will remove the need for an xorg
config file for the x240/t440/t540 generation of thinkpads.

ok matthieu@ shadchin@ kettenis@


# 1.279 11-Jan-2015 kettenis

Tolerate checksum errors in ACPI tables (but complain about them). Apparently
there are machines out there with broken BIOSen with such checksum errors in
thier RSDT tables. Windows runs fine on these machines, so it probably does
not check. Linux ignores checksum errors as well (and prints a warning).

ok deraadt@, krw@


# 1.278 18-Dec-2014 deraadt

suspend and resume the RNG (at what looks like the right spots)
ok reyk djm


# 1.277 09-Dec-2014 doug

Replace some malloc(n*size,...) calls with mallocarray().

ok tedu@ deraadt@


# 1.276 06-Dec-2014 mpi

If "machdep.lidsuspend" is not set, do not go back to sleep if the lid
is still closed when resuming. This allows docked laptop to be resumed
without opening them.

ok mlarkin@


# 1.275 23-Nov-2014 mlarkin

If a machine has no _LIDs, then none can be open. This diff fixes a problem
where machines without any _LIDs were going back to sleep after hibernate
due to the recent suspend-if-lid-closed-on-resume diff. Noticed on qemu,
which doesn't have any _LIDs.

discussed with deraadt


# 1.274 18-Nov-2014 krw

Nuke yet more obvious #include duplications.

ok miod@


# 1.273 08-Nov-2014 mlarkin

If resuming from sleep (zzz/ZZZ) and the lid is still closed, go back to
sleep. This prevents accidental lid flex or slight opening in a backpack
from waking the machine up and leaving it resumed (powered on).

ok deraadt@


# 1.272 26-Sep-2014 kettenis

Rework piglet and pig allocation. Currently the piglet gets allocated
deep down in the suspend path, where it is really hard to recover from
allocation failure. So allocate the piglet early on in the suspend path.
Also change the piglet and piglet allocation functions to use km_alloc(9)
instead of doing pmemrange magic. This removes a bunch of code which, in the
case of the piglet allocation, is broken since it results in a NULL pointer
dereference. Also switch the piglet allocation to not wait. If we can't
allocate 16MB of phys contig memory on a halfway modern machine we're almost
certainly under a lot of memory pressure and we're better off not trying to
hibernate anyway.

ok mlarkin@


# 1.271 25-Sep-2014 kettenis

Bring back rev 1.249. Now that mp_setperf() has been fixed, it should be
safe to restore perflevel when resuming. Should fix the issues people
see with the processor running at full speed heating up the machine while
the apm command reports that the CPUs are running at a low speed.

ok dcoppa@, deraadt@


# 1.270 23-Sep-2014 kettenis

Release the acpi lock around calls to wsdisplay_suspend() and
wsdisplay_resume(). The X server may adjust the display brightness just
before/after the VT switch when suspending/resuming. In fact, it may
already be blocked adjusting the display brightness for some reason
unrelated to the suspend request.

The VT switch request will eventually time out, but the delay is annoying
and the failed VT switch will lead to problems after resume.

Needed for a future update of the xf86-video-intel driver.

ok mlarkin@


# 1.269 19-Sep-2014 kettenis

Use config_suspend_all(9).


# 1.268 08-Sep-2014 kettenis

Clear the PME Status bit on PCI devices in response to Device Wake events.
Fixes the interrupt storm on the 2009 Mac mini with WOL enabled on its
nfe(4) interface.

ok mlarkin@


Revision tags: OPENBSD_5_6_BASE
# 1.267 20-Jul-2014 mlarkin

Support hibernating to softraid crypto volumes.

much help and ok from deraadt@


# 1.266 16-Jul-2014 mlarkin

Reenable hibernate RLE support and flush+zero all memory after unpack.


# 1.265 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.264 12-Jul-2014 kettenis

Add a function to drop all clean pages on the page daemon queues and call
it when we hibernate.

ok mlarkin@, miod@, deraadt@


# 1.263 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.262 11-Jul-2014 mlarkin

Flush the buffercache to 16MB on hibernate and restore its previous max
size (kern.bufcachepercent) on resume, for better hibernate performance.

ok beck@


# 1.261 10-Jul-2014 blambert

remove unneeded header

ok mlarkin@


# 1.260 01-Jul-2014 kettenis

Adding an additonal delay after each access to SystemMemory or SystemIO
during autoconf makes no sense.

ok deraadt@


# 1.259 23-Jun-2014 kettenis

SystemMemory is used to access memory mapped registers on some machines, so we
must use the bus_space(9) API to access it instead of memcpy(9). Also make
sure we properly align access to fields that need more than byte alignment.

ok guenther@


# 1.258 21-May-2014 mlarkin

Fix invalid / missing format specifiers in several dnprintf statements
(used only when ACPI_DEBUG is enabled)

From a diff on tech@ by Fabian Raetz, with a minor change by me, tested
on i386 and amd64 with ACPI_DEBUG enabled and disabled.


# 1.257 25-Apr-2014 mlarkin

Defer the free of the hibernate structures until later, when it is safe to
do so.

Freeing the hibernate structures too early leads to inadvertent calls into
splx when we aren't ready for them yet. Found when trapping splx calls in
the resume path.

discussed with deraadt@ at m2k14


# 1.256 13-Apr-2014 mlarkin

Fix a badly worded debug message that implied the entire machine was going
to sleep, instead of just the acpi thread going to sleep.


# 1.255 31-Mar-2014 mpi

Change the order of operations during a suspend/resume cycle and
call bufq_quiesce() after executing the DVACT_QUIESCE handlers.

This should be safe since no disk nor controller drivers have such
handler but it will allow us to detach sd(4) devices attached to a
USB bus.

Another benefit pointed out by kettenis@ is that drivers that need
to read a firmware from the disk should be able to do it at resume
time in a DVACT_WAKEUP handler.

ok kettenis@, deraadt@


# 1.254 13-Mar-2014 dlg

get rid of the assumption that the head of the alldevs list is the
"mainbus" device. this breaks when mpath is enabled because it
attaches before mainbus and therefore takes the head position.

have autoconf provide device_mainbus() which looks up mainbus_cd,
and use that instead.

discussed with deraadt who just wants mpath stuff to move forward
despite there being many ways to shine this particular turd.


Revision tags: OPENBSD_5_5_BASE
# 1.253 21-Feb-2014 deraadt

Do the lid control further on in the resume sequence, since this is
AML, and will do WAITOK operations. While cold, that is a bad idea.
We seem to be safe with respect to the spec, I do not see any event
debounce issues here.
ok kettenis, tested by many


# 1.252 20-Jan-2014 claudio

Instead of checking sc->sc_revision <= 1 check that the
fadt->hdr_revision <= 1. This is a better check since it checks
the version of the proper descriptor. This allows the alix APU
to reboot since those boards have currently a wrong sc->sc_revision.
Committing this now so that it can be tested easier.
With and OK jsg@ also fine by derradt@


# 1.251 24-Dec-2013 kettenis

If the FADT has its SMI_CMD set to zero, assume we're only ACPI-only hardware
and don't need to disable SMI ownership of the ACPI hardware registers.

ok mlarkin@


# 1.250 23-Dec-2013 kettenis

Revert previous commit. It makes resume hang on some MP systems running
GENERIC.MP.


# 1.249 16-Dec-2013 kettenis

Restore hw.setperf upon resume like we do for apm(4) on i386. Seems we need
to do this fairly late in the resume to avoid hangs.

ok deraadt@, mlarkin@


# 1.248 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


# 1.247 06-Nov-2013 mpi

Add support for Power Resources for Dx states and the necessary hook
for PCI devices. This hook should be called twice, before and after
changing the power state of a PCI device.

Before setting the device to the new state, the ACPI layer will notify
every power resources linked to the device for that state and make sure
they are turned "_ON". After changing the state of the device, it will
decrement the reference of every power resources linked to that device
for the old state and turn them "_OFF" if they are no longer referenced.

This fixes the no-USB after resume problem seen on various ThinkPad,
problem initialy diagnosed with Alexander Polakov.

ok kettenis@, deraadt@


Revision tags: OPENBSD_5_4_BASE
# 1.246 01-Jun-2013 mlarkin

Stop printing ACPI wakeup devices in dmesg after the 16th wakeup device.

Helps with VMware VMs that report hundreds of wakeup devices.

ok reyk@, deraadt@


# 1.245 31-May-2013 bluhm

Fix the build for a kernel without wd and pciide in its config.
OK deraadt@ tedu@


# 1.244 30-May-2013 deraadt

Enforce ca_activate tree-walks over the entire heirarchy for all events,
cleaning up some shutdown-hook related code on the way.
(A few drivers related to sparc are still skipped at kettenis' request)
ok kettenis mlarkin, tested by many others too


# 1.243 18-Apr-2013 deraadt

pin the acpi thread to the BSP, because AML and SMI cannot be trusted. I
was certain we were doing this already, but it turns out no...


Revision tags: OPENBSD_5_3_BASE
# 1.242 24-Dec-2012 guenther

In acpiioctl(), Return though the splx() path if suser() returns an error

ok deraadt@


# 1.241 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


# 1.240 04-Oct-2012 ehrhardt

Use information provided by ACPI to attach secondary PCI host bridges.

ok kettenis@


# 1.239 07-Sep-2012 kettenis

Add a new function called acpi_pci_min_powerstate() that returns the lowest
power state for a PCI device for the power state (currently S3 or S4) we're
transitioning into. This function is probably too simplistic; there is a lot
of (silly) complexity in the ACPI spec.

ok deraadt@


Revision tags: OPENBSD_5_2_BASE
# 1.238 13-Jul-2012 pirofti

Disable acpivideo(4) on ASUS laptops.

There are some models that panic whenever an event is received.
Some cope, some don't. In order to get this right acpiec(4) requires
major rework. Until then let machines boot.

Discussed with deraadt@.


# 1.237 11-Jul-2012 mlarkin

for now, restrict hibernate to root in the acpi ioctl kernel interface.
normal rules for controlling this interface through apmd still apply.

ok deraadt@


# 1.236 09-Jul-2012 deraadt

tiny little oops in untested hibernate block


# 1.235 09-Jul-2012 deraadt

clean up ioctl() case statements


# 1.234 09-Jul-2012 deraadt

acpi_disable_onegpe() is not used; ok pirofti


# 1.233 24-May-2012 kettenis

If _MIN > _MAX consider a WordBus resource to be invalid. Fixes a panic on
the IBM x3550-M4.

ok mikeb@


# 1.232 29-Mar-2012 mlarkin

Missing #ifdef in previous diff


# 1.231 29-Mar-2012 mlarkin

Prevent hibernation attempts on machines with unsupported disk controllers.
The same check is also performed later in the hibernate sequence, but by
then it's too late to cleanly unwind (presently).

ok deraadt


# 1.230 26-Mar-2012 deraadt

simplify conditionals in acpi_sleep_state()
ok mlarkin


# 1.229 26-Mar-2012 deraadt

hook in the hibernate request code; half of this diff is from mlarkin
ok mlarkin


Revision tags: OPENBSD_5_1_BASE
# 1.228 20-Sep-2011 deraadt

Very early in suspend while the clock is still ticking realtime,
save the clock back to the rtc. The zaurus already did this.
ok phessler miod kettenis


Revision tags: OPENBSD_5_0_BASE
# 1.227 02-Jul-2011 nicm

kqueue attach functions should return an errno or 0, not a plain 1. Fix
the obvious cases to return EINVAL and ENXIO.

ok tedu deraadt


# 1.226 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.225 16-Jun-2011 pirofti

Import acpitoshiba driver.

This is originally written by Hiroyuki Aizu, ported and integrated
in our tree by Javier Vazquez <javama4 AT gmail> with tweaks and
suggestions by me.

Import prodded by deraadt@ and the lack of testers on tech@.


# 1.224 27-Apr-2011 jcs

attach acpithinkpad to newer lenovo models like the x120e

ok deraadt@


# 1.223 22-Apr-2011 jordan

Fix uninitialzied variables and formatting strings (-Wxxx errors)


Revision tags: OPENBSD_4_9_BASE
# 1.222 02-Jan-2011 jordan

Re-add code for ACPI taskqueue, eliminates need for state vars in acpi softc
Serialize processing ACPI tasks for sleep/powerdown/GPE event
ok deraadt@


# 1.221 31-Oct-2010 guenther

Revert last commit: it breaks resume on Thinkpads
"then please back it out" deraadt@


# 1.220 26-Oct-2010 jordan

Add task queue for ACPI gpe and notify handlers
This fixes eject on Dell Latitude dock and an issue on the Dell Mini
battery update.
ok deraadt


# 1.219 07-Oct-2010 jordan

Create separate functions for enabling wake and runtime gpes.
Remove old suspend/resume gpewalk function
ok deraadt, mlarkin


# 1.218 05-Oct-2010 jordan

split acpi_enable_onegpe to separate enable/disable functions
ok deraadt


# 1.217 05-Oct-2010 jordan

Added acpi_disable_allgpes function to clear GPE status
ok deraadt


# 1.216 05-Oct-2010 deraadt

clean up acpi_softc use more


# 1.215 05-Oct-2010 deraadt

delete a bit of #if 0 code


# 1.214 05-Oct-2010 deraadt

hide use of our global acpi_softc pointer a bit better


# 1.213 29-Sep-2010 deraadt

grab the acpi thread lock very early in acpi thread startup; not that it
is likely that anyone will race us... but why be careless.
ok kettenis


# 1.212 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.211 29-Aug-2010 jasper

- add a check for FWRITE here too.

ok miod@


# 1.210 27-Aug-2010 kettenis

Make PCI Power Management optional, and only enable it when acpi(4) attaches.

ok deraadt@


Revision tags: OPENBSD_4_8_BASE
# 1.209 08-Aug-2010 kettenis

Close race between the acpi thread and wscons ioctls running in process
context. We might rip this out post-release and replace it with a solution
that serializes things in a more obvious way.

ok deraadt@, miod@ and "those crazy texans who ok anything"


# 1.208 08-Aug-2010 deraadt

Report the acpi sleep states when on a RAMDISK; ok krw


# 1.207 07-Aug-2010 kettenis

Enforce acpi register access rescrictions.

ok mlarkin@, deraadt@, tested by marco@


# 1.206 07-Aug-2010 deraadt

upon resume, notify all the acpiac and acpibat drivers to update
their status. some laptops do not do this automatically.
ok kettenis canacar


# 1.205 06-Aug-2010 marco

Always call _PSW on _LID devices that support it to enable lid open to
resume the machine. Conversely disable it when the machine wakes up.

Tested by several ok deraadt


# 1.204 06-Aug-2010 deraadt

As part of a revamp of the PM1/GPE code, I write this diff to look for
unmanaged EN & STS bits in the PM1 register at interrupt time and report
them. As a side effect this splits the STS acknowledgement into two writes
(for power, and sleep) instead of one. The printf that is added (to spot
unmanaged STS bits) has not yet been triggered as far as we know.

Before the "write to PM1 registers at the right offset" diff went in, this
was not neccessary. But newer thinkpads do not have a working soft power
button without this diff.

We have no idea why.
ok mlarkin kettenis


# 1.203 05-Aug-2010 deraadt

PM1 power button events were generating 'fake events' to acpibtn PWRB,
but if there is no such device these were being tossed. Instead, arrange
for all power button handlers to flag a softc variable which is processed
by the thread; so that one place will know to request a powerdown (psignal
to init)
ok marco mlarkin


# 1.202 05-Aug-2010 deraadt

rename a variable which is annoyingly poorly named


# 1.201 05-Aug-2010 mlarkin

Write to the correct offset for various ACPI pmreg registers. Writing to
the correct register does properly clear the wake status bits and fixed
event bits, which allows some machines to have more than 1 suspend cycle.

bug spotted by deraadt@

tested by deraadt@ and myself on a half-dozen machines and others on
hackers@

ok deraadt@, marco@


# 1.200 05-Aug-2010 deraadt

Fix the power button. The pm1 register is 16 bits in size.
My mistake, a while back.
tested by krw


# 1.199 04-Aug-2010 kettenis

Print supported sleep states.

ok marco@, phessler@, deraadt@


# 1.198 03-Aug-2010 mlarkin

Use the proper flag for re-enabling certain hardware events (power btn,
sleep btn). The (incorrect) flag prevoiously used coincidentally had the
same value, so this shouldn't cause any different behavior than before.

tested on a variety of machines (i386, amd64, sp, mp) - no behavioral
change seen

ok deraadt@


# 1.197 28-Jul-2010 marco

Make all gasio failures look and behave the same.


# 1.196 28-Jul-2010 mlarkin

Delay a little longer on suspend before giving up. Fixes machines that
sometimes don't suspend because the sleep transition takes too long
(symptoms like failing to sleep but the backlight stays on).

ok deraadt@


# 1.195 27-Jul-2010 deraadt

remove XXX; EC can now be attached early when needed; ok jordan


# 1.194 27-Jul-2010 deraadt

more brackets feels good


# 1.193 27-Jul-2010 deraadt

XXX annotate a block in acpi_enter_sleep_state() that is pretty worrying


# 1.192 27-Jul-2010 deraadt

horrific indentation that nearly made me puke


# 1.191 27-Jul-2010 mlarkin

Set the sleeping indicator light on machines that support it, in case SMI
doesn't do it for us. Shows the 'moon' LED on older Thinkpads when
sleeping.

Tested on T43p, W500, T510 by me and on X61s by deraadt@.

ok deraadt@


# 1.190 27-Jul-2010 jordan

Early initialization of acpiec if ECDT table exists
Fixes hang when booting thinkpads while docked
ok deraadt


# 1.189 26-Jul-2010 deraadt

remove merge error which was commited


# 1.188 26-Jul-2010 pirofti

Add support for Sony ACPI hotkeys via a new driver: acpisony(4).

Currently it only works for the suspend button and tries to do right for
the brightness events, but I haven't found a Sony laptop that like to do
right so far.

In the future I want to make the brightness keys work on all Sony's and
also add support for the zoom hotkeys and whatever other funky keys I can
find on those things.

Okay deraadt@.


# 1.187 22-Jul-2010 deraadt

pretty large cleanup of SMALL_KERNEL


# 1.186 21-Jul-2010 deraadt

in the acpi GPE handler, late ack level interrupts not edge interrupts.
Duh.


# 1.185 21-Jul-2010 deraadt

Merge the edge and level gpe handlers into one (now possible since the
gpe structure tells us whether the gpe is edge or level)


# 1.184 21-Jul-2010 deraadt

delete #if 0 code we are not going to use; ok marco


# 1.183 20-Jul-2010 deraadt

make the mask code for acpi_enable_onegpe() match other similar code


# 1.182 20-Jul-2010 deraadt

Handle the acpi interrupt controller with proper edge/level handling,
wrapped in spltty to avoid racing against the interrupt controller.
Repair the gpe bit masking code while there, and do operations in the
right order
ok kettenis mlarkin, help from jordan at figuring out the order of
operations.


# 1.181 20-Jul-2010 matthew

When we're preparing to go to sleep, only call bufq_restart() if other
preparations failed.

ok kettenis@, thib@, deraadt@


# 1.180 19-Jul-2010 deraadt

permit compilation with ACPI_DEBUG


# 1.179 19-Jul-2010 deraadt

there is no need for a seperate dsdt_softc pointer; dsdt does not have a
softc but is just a part of the single acpi driver.
ok jordan


# 1.178 19-Jul-2010 deraadt

acpi_delay() is no longer needed; ok jordan


# 1.177 18-Jul-2010 mlarkin

Clear event status on resume, to avoid going back to sleep/reboot/shutdown
immediately after resume. Tested by a bunch of people on hackers@ on
various hardware. Fixes at least Toshiba NB200 and Gateway LT3103u.

ok marco


# 1.176 13-Jul-2010 deraadt

rename some thread related interfaces so they are logical


# 1.175 10-Jul-2010 jordan

Verify _ADR bdf is correct (some systems return 0xffff)
Works on Dell x9xx and Studio 1555


# 1.174 09-Jul-2010 jordan

Clear the noise


# 1.173 08-Jul-2010 jordan

Add mapping for ACPI device to PCI bdf (match autoconf tree)
Simplify resource parsing function to use buffer argument
Convert namespace linked lists to use queue macros
ok marco@, deraadt@


# 1.172 06-Jul-2010 deraadt

Use spltty() to lock downcalls from apm against the information being
modified by the acpi thread.
ok kettenis


# 1.171 06-Jul-2010 deraadt

Don't use a workq to do the suspend, because it races aginst the acpi
thread. Instead, just tell the acpi thread to do the suspend for us.
This makes apmd & zzz work correctly. While here, have acpithinkpad
attempt to post the event to apm before prompting the suspend itself.
ok kettenis marco mlarkin


# 1.170 05-Jul-2010 mlarkin

Reset register SCI_EN on resume. The ACPI spec clearly states we don't have
to do this, but many machines don't, or forget to, which leads to all sorts
of problems on resume (broken battery stats, overheating, failure to resume
more than once, etc).

tested by many, ok marco@, deraadt@


# 1.169 01-Jul-2010 thib

do bufq quiesce again, since vnd/the bufq_done call in
biodone has been fixed.


# 1.168 01-Jul-2010 jordan

Backout recent AML changes, breaks on R210 and others


# 1.167 01-Jul-2010 jordan

Simplified aml_parse_resource call, cleanup code
ok marco


# 1.166 01-Jul-2010 jordan

Fix subordinate bus # for multi-root PCI buses
ok marco, kettenis


# 1.165 30-Jun-2010 thib

Disable/partially backout the bufq quiesce changes as this
is causing havoc with vnds and release must be buildable.


# 1.164 29-Jun-2010 deraadt

Fix ramdisks (new code was placed inside a SMALL_KERNEL chunk)


# 1.163 29-Jun-2010 jordan

Add support for mapping ACPI to PCI devices
ok kettenis, deraadt


# 1.162 29-Jun-2010 kettenis

Call bufq_quiesce() on suspend to stop all I/O and bufq_restart on resume.
Makes suspend/resume work much better while doing disk I/O.

ok mlarkin@


# 1.161 29-Jun-2010 tedu

60 is a more reasonable guess for number of minutes in an hour than 100.
spotted by TeXitoi. ok deraadt


# 1.160 27-Jun-2010 jordan

Add _acpi_matchhids routine, precursor to pci device match


# 1.159 27-Jun-2010 mlarkin

Clear ACPI fixed event status on resume (power buttons/etc) so that some
machines don't immediately resume after suspending to S3.

Tested by kettenis@ and I on 5 or 6 machines, deraadt@ "yeah, looks good"


# 1.158 27-Jun-2010 jordan

Cleaned up acpi_maptable, adds to table list directly


# 1.157 07-Apr-2010 deraadt

If we have done a wsdisplay_suspend(), and ACPI decides it does not want to
suspend, we must roll back by calling wsdisplay_resume() or the virtual
console switching will be locked forever
ok oga


# 1.156 07-Apr-2010 kettenis

Make suspend/resume work on MP machines (running an MP kernel). Joint work
from mlarkin@ and me, with some amd64 fixes thrown in by deraadt@

ok marco@, deraadt@, pirofti@, mlarkin@


# 1.155 31-Mar-2010 kettenis

Bring /dev/apm support in line with apm(4). Make the suspend button send
a suspend request event to apmd(8) instead of suspending immediately. Also
keep track of whether /dev/apm and /dev/apmctl are currently open, such that
we can still suspend immediately if apmd(8) isn't running.

ok deraadt@, marco@, pirofti@, jsing@, oga@


# 1.154 30-Mar-2010 oga

Prevent the apmd/x races for good.

When we hit suspend time, go through all wsdisplays on the system. if
they are in mode MAPPED, but not MODE_DUMBFB then if possible do a full
vt switch to a !mapped vt, and prevent switching back until resume time.
This has to be called from MD code because this involves userland
running so that X can run the vt switch signal handler. This way, any
case where we are using the "poke registers from userland" model, we
will not be on the hardware when we go down, so the kernel can actually
handle thing properly.

Tested on several acpi laptops (by kettenis@ and ian@), x40 (me and
beck@ at LEAST) and zaurus (me). Maybe others, but if so I forgot who at
this time..

Idea from deraadt somewhere over the Faroe Islands (I thought of a
similar thing myself a while ago). Much prompting from him. Ok and
comments miod@


# 1.153 25-Mar-2010 oga

Fix & vs. && in the kqfilter code.

Based on a comparison with the apm code.

ok deraadt@, kettenis@


Revision tags: OPENBSD_4_7_BASE
# 1.152 26-Nov-2009 mlarkin

Visual (and on some machines, audio) feedback while the machine is
resuming via calls to aml _SST method. This also makes the "moon" LED on
thinkpads to blink during resume and cease when resume is completed.

ok pirofti@


# 1.151 26-Nov-2009 deraadt

Toggle into "cold" when we we disable interrupts, because tsleeping
in the AML interpreter and context switching into userland processes
tends to sometimes pop the ACPI trampoline page out of our address
space. Makes lots more machines work.
ok kettenis mlarkin


# 1.150 24-Nov-2009 jsg

in the resume path evaluate _WAK before dealing with device drivers
like the spec says, not after.

ok mlarkin@


# 1.149 24-Nov-2009 deraadt

abort suspends on SMP systems until that gets written; ok mlarkin


# 1.148 23-Nov-2009 mlarkin

pci should return the result of it's children's suspend/resume calls back
to its parent.

handle suspend failure case and unwind if devices have an activate function
that returns failure.

ok deraadt@, kettenis@


# 1.147 23-Nov-2009 mlarkin

acpi* devices need to call their children on suspend/resume.
ok deraadt@


# 1.146 23-Nov-2009 pirofti

Remove ACPI_SLEEP_ENABLED checks.

This enables by default the suspend/resume paths in the kernel.

Okay deraadt@.


# 1.145 23-Nov-2009 deraadt

knf and spacing that are super annoying


# 1.144 23-Nov-2009 mlarkin

Hook up Theo's new children suspend/resume function, as well as more ACPI
suspend/resume glue.

ok deraadt@


# 1.143 26-Oct-2009 deraadt

Do not do strncmp comparisons on dv_xname because longer device names which
look similar could arrive in the future. Instead, compare directly against
dv_cfdata->cf_driver->cd_name
Issue originally spotted by miod


# 1.142 26-Oct-2009 jordan

Verify checksum+address when loading ACPI tables.
Some systems had invalid entries in RSDT/XSDT.
ok marco@


# 1.141 23-Jul-2009 cnst

New aibs(4) driver for ASUSTeK AI Booster (ACPI ATK0110) hardware monitoring,
with sensor state support through limits provided by the ACPI.

Tested on several ASUS motherboards kindly networked by Sam Fourman Jr.

ok deraadt marco jordan


Revision tags: OPENBSD_4_6_BASE
# 1.140 03-Jun-2009 pirofti

Import acpi power resource driver. This is needed for suspend/resume logic.

Okay jordan@ and marco@.


# 1.139 03-Jun-2009 pirofti

Add acpivideo support. This adds brightness support for all laptops
except thinkpads, they will use the acpithinkpad driver. The driver is
also hooked into wsconsole. So brightness can be adjusted via:

$ wsconsctl display.brightness=<percentage>

This is very helpfull on some laptops that have a nasty bios and get two
steps instead of one when pressing the brightness button.

Tested on various dell, fujitsu, acer, samsung and other laptops.

Okay marco@, miod@. Suggestions from kettenis@.
Lots of reviews and help from miod@, thanks!


# 1.138 03-Jun-2009 jordan

Added ejectable bay for IDE cdroms
ok marco@


# 1.137 30-Apr-2009 marco

Oga changed apm ioctls and forgot to grep. Temporary work around by hitting
the sleep button when we get any request. This interface will chnage at
some point.
ok oga


# 1.136 19-Apr-2009 krw

Call acpi_sleep_walk() when powering down just like when entering
any other sleep state. This tells acpi wake devices to not wake up
the box until manual powering up resets their status. Makes my Dell
GX520 stay powered off in response to 'halt -p'.

In snaps for a while.

ok marco@ (several times) deraadt@


# 1.135 17-Apr-2009 pirofti

Add device ranking support for future sleep/wake developments.

okay marco@


# 1.134 11-Mar-2009 jordan

Cleanup.. use aml_evalinteger instead of aml_evalname
ok marco@


# 1.133 10-Mar-2009 jordan

Fix cut/paste error.. call _BFS not _PTS
ok marco@


Revision tags: OPENBSD_4_5_BASE
# 1.132 21-Feb-2009 marco

overzealous cut/paste; found by Vladimir Kirillov <proger@uaoug.org.ua>


# 1.131 20-Feb-2009 marco

Oops didn't mean for Debugger() to remain there.


# 1.130 19-Feb-2009 marco

suspend/resume bits so that we can develop this in tree. This is disabled.
code from mlarkin and me
help from art,toby,jordan and several others
ok jordan, go for it deraadt


# 1.129 10-Feb-2009 jordan

Fix null pointer for shutdown if acpi not enabled
ok marco@


# 1.128 04-Feb-2009 kettenis

Some systems that claim to be ACPI 2.0 apparently don't provide an XSDT
address. Make the code fall back on the RSDT on those systems.

From Joerg Niendorf <p3y80@internode.on.net>. Fixes PR 5986.


# 1.127 17-Jan-2009 krw

A couple of stray malloc/memset -> malloc(M_ZERO), a bit of knf and
lose a couple of unnecessary casts. No functional change.

Feedback from kettenis@

ok kettenis@ marco@


# 1.126 06-Nov-2008 marco

Collapse all sanity checks of hids into one function.

Originaly from jordan


Revision tags: OPENBSD_4_4_BASE
# 1.125 02-Jul-2008 fgsch

add acpivideo. for now almost an empty skel so others can work on
suspend/resume. eventually it will also manage output switching and
brightness where supported. prodded by marco@


# 1.124 11-Jun-2008 marco

Fix $OpenBSD$ for once and for all


# 1.123 08-Jun-2008 deraadt

aaa_dev was being set from an uninitialized variable; ok toby


# 1.122 07-Jun-2008 marco

Kill ACPIVERBOSE. It was never used and is worthless to boot.

comments jmc
ok toby


# 1.121 06-Jun-2008 marco

Shrink acpi some more for floppies and while here re-enable acpi on the
floppies. Fixes crazy sony vaio kettenis has.

tested by deraadt
ok deraadt jordan kettenis


# 1.120 01-Jun-2008 marco

Fix \n nit in SMALL_KERNEL.


# 1.119 01-Jun-2008 marco

Re-enable new parser. Lots of testing by various folks. Fits all media
now too.

ok kettenis deraadt


# 1.118 16-May-2008 dlg

back out jordans new parser.

it didnt have enough oks from the right people, it did break machines
that were previously working, and it bloated the RAMDISKS and install
media beyond the constraints of the media.

requested by deraadt@


# 1.117 14-May-2008 jordan

Adding new ACPI Parser code
Old guts of code still exists, needs to be torn out next
ok marco@
ok brad@


# 1.116 27-Apr-2008 jcs

add acpithinkpad(4), an acpi support driver for ibm/lenovo
thinkpads.

provides functionality that the bios did on older, non-acpi
thinkpads like respond to screen brightness keys and toggle the
bluetooth device on and off.

ok deraadt@ and marco@


# 1.115 24-Apr-2008 jsing

Introduce acpiasus(4), a driver for the ACPI based hotkeys found in many
ASUS laptops (including the ASUS EeePC) - largely based on NetBSD's
asus(4) driver. On the ASUS EeePC this allows us to enable/disable
wireless, change screen brightness and use the volume keys.

ok jsg@, weingart@


# 1.114 12-Apr-2008 kettenis

Get rid of acpi_s5 global variables; simply send SIGUSR2 instead of SUGUSR1
to tell init(8) to power down the machine.

ok krw@


Revision tags: OPENBSD_4_3_BASE
# 1.113 21-Jan-2008 jsg

Check against RESET_REG_SUP in FADT before doing the acpi reset dance
after all, not doing so appears to break more machines than it fixes.

ok marco@


# 1.112 05-Jan-2008 kettenis

Never attach more acpicpu(4) devices than cpu(4) devices. Always attach
the first acpicpu(4) device on non-MULTIPROCESSOR kernels, regardless of its
ACPI processor ID.

ok mikeb@


# 1.111 05-Dec-2007 deraadt

Hang acpi(4) below bios(4) instead of mainbus(4). This lets us move acpi(4)
before pcibios(4), and then.... if acpi(4) attaches, skip pcibios(4) since
it messes the machines up. Fixes claudio's HP dl320 G5, and almost assuredly
others.
ok kettenis


# 1.110 05-Dec-2007 deraadt

repair acpiopen/acpiclose and friends to handle the /dev/apmctl interface,
so that apmd(8) can actually work correctly against acpi(4), and thus,
even do the fancy -C/-A stuff; also tested by jmc


# 1.109 02-Dec-2007 fgsch

fix the trailing space for real now.


# 1.108 02-Dec-2007 fgsch

avoid trailing spaces on output; marco@ ok.


# 1.107 27-Nov-2007 kettenis

Fix off-by-one that made us attach acpicpu0 and acpicpu1 on GENERIC kernels.

ok marco@


# 1.106 25-Nov-2007 jsg

When available and ACPI is enabled use the ACPI reset method
to reboot. This is required for some quirky sis machines
and likely others to reboot.

Tested by many. 'commit early commit often' deraadt@


# 1.105 19-Nov-2007 deraadt

sigh, amd64 still uses that stupid acpi_enabled variable...


# 1.104 19-Nov-2007 deraadt

softc's come initialized to 0, you know...


# 1.103 19-Nov-2007 deraadt

store revision; from jsg


# 1.102 19-Nov-2007 kettenis

Initialize sc_facs earlier.

ok deraadt@, beck@, canacar@


# 1.101 16-Nov-2007 deraadt

fix the bus_space #define nightmare, so that amd64 and i386 are much more
uniform. as a result shared code like acpi needs less #ifdef's
ok marco kettenis


# 1.100 15-Nov-2007 deraadt

remove the #ifdef ACPI_ENABLE. the EC codepath is either safe enough (and
will be improved), or it is unsafe because AML assumes it must be there
ok almost everyone


# 1.99 15-Nov-2007 mikeb

Make acpicpu(4) attach to the existing CPUs only.

Problem reported by form@. Fix idea by kettenis@.
Lots of help from deraadt@. Tested by deraadt@, form@ and me.

ok deraadt marco


# 1.98 14-Nov-2007 deraadt

spacing


# 1.97 12-Nov-2007 deraadt

correct : vs , in attach-time messages


# 1.96 06-Nov-2007 deraadt

all ACPI_ENABLE kernels need acpi_interrupt()


# 1.95 06-Nov-2007 deraadt

move acpi_read_pmreg() and acpi_write_pmreg() out of SMALL_KERNEL so
that acpi ramdisks can compile (obvious solution)


# 1.94 05-Nov-2007 canacar

Establish the acpi interrupt earlier. Fixes problems with some
GENERIC.MP kernels not getting acpi interrupts. This is a
workaround until the real issue with late establishment is fixed.
ok weingart@, beck@, gwk@


# 1.93 03-Nov-2007 jordan

Added support for displaying wakeup devices
ok beck@,weingart@,gwk@


# 1.92 11-Oct-2007 kettenis

When walking the tree yo find _PRT methods, check whether the device is
actually enabled and functioning. Similar to what we do for _INI.

ok weingart@, beck@


# 1.91 08-Oct-2007 krw

More simple memset(,0,) -> M_ZERO changes. In this batch move to
size(*p) as the first malloc() parameter where p is declared locally
and thus easy to check. Add M_ZERO to gpe_table allocation in acpi.c
even though there is no obvious bzero or memset nearby.


# 1.90 13-Sep-2007 mk

Remove unnecessary memset() (see aml_evalnode(9)) and a bit of KNF.

ok toby


# 1.89 13-Sep-2007 weingart

Implement ACPI 6.5.1 spec tree walk for _STA and _INI.
Fixes mk's laptop. No regressions so far (thank you to the
testers).

ok gwk@, mk@, marco@


Revision tags: OPENBSD_4_2_BASE
# 1.88 17-Apr-2007 mk

Now that acpidock no longer hangs machines during boot (at least
according to the reports I've been getting) it should be safe to enable
it again, so remove the #if 0 stuff around it.

ok marco


# 1.87 11-Apr-2007 jordan

Added changes for C-State
ok marco@


# 1.86 26-Mar-2007 marco

Calculate minutes left more accurately.

From giovanni <qgiovanni@gmail.com>


Revision tags: OPENBSD_4_1_BASE
# 1.85 03-Mar-2007 mk

Stop looking for acpidock until we fix that stuff that breaks all pre-60
series ThinkPads with acpidock(4) enabled.


# 1.84 22-Feb-2007 marco

We were getting no interrupts in .MP. There are 2 fixes; one in
acpi_machdep.c and move the interrupt establishment down after ioapic
has been initialized.

ok jordan


# 1.83 21-Feb-2007 kettenis

Make an acpi-enabled GENERIC.MP work on systems that only support PIC mode.

ok marco@


# 1.82 19-Feb-2007 jordan

Added acpi_map_address function for mapping GAS/IO registers
ok marco@


# 1.81 18-Feb-2007 jordan

Added dynamic perfect hash for opcodes
ok marco@


# 1.80 18-Feb-2007 marco

Shave off another K pre compression.
Shuffle SMALL_KERNEL functions around so that we only have a single #ifndef


# 1.79 17-Feb-2007 marco

Add SMALL_KERNEL throught acpi to make it fit on boot media.
ok deraadt


# 1.78 13-Feb-2007 marco

Add smarter _INI code. This helps with some dissapearing devices.

ok kettenis jordan


# 1.77 31-Jan-2007 gwk

Introduce acpi_hasprocfvs thats "ACPI Has Processor Frequency and Voltage
Scalling" a global flag set by acicpu attach to indicate that ACPI is
handling fvs through the _PSS objects. This will fix disappearing
EST/powernow when booting kernels with acpi enabled, as acpicpu isnt
compiled into GENERIC yet. Also in cases where acpicpu is built in and
legacy EST/powernow work but acpi is missing the _PSS object, the legacy
routines will be used.

tested by dim@, and Nick Nauwelaerts <nick AT nauwelaerts.net>
ok marco@, dim@


# 1.76 22-Jan-2007 mk

Actually look for docking stations by looking for _DCK nodes.

ok marco


# 1.75 02-Jan-2007 marco

On some systems EC must be attached as the first device. Move EC from
generic _HID devices to its own routine to force first attach. Bug found
and debugged by joris@

Code mostly written by joris@


# 1.74 26-Dec-2006 marco

Add polling to devices that require it such as acpibat and acpitz.
Use the same tsleep/wakeup mechanism as the interrupt code to keep it non-
concurrent.


# 1.73 21-Dec-2006 deraadt

even more knf love


# 1.72 21-Dec-2006 marco

Remove some leftover acpiec gpe/interrupt code.


# 1.71 20-Dec-2006 deraadt

oops, name already used


# 1.70 20-Dec-2006 deraadt

rename __acpi_enable_gpe(); ok marco


# 1.69 20-Dec-2006 deraadt

acpi_unmap_pmregs() is not used, ok marco


# 1.68 19-Dec-2006 deraadt

simplify some low-level methods substantially (shrinking code); ok marco jordan


# 1.67 06-Dec-2006 mk

malloc(..., M_NOWAIT) with no NULL check is bad. If allocation fails,
complain, clean up, and bail out of attach.

input and ok gwk


# 1.66 28-Nov-2006 jordan

Fixed clearing arg for acpi_init_pic
ok kettenis@


# 1.65 27-Nov-2006 kettenis

Don't spam dmesg.


# 1.64 27-Nov-2006 jordan

Fixes for acpiprt routing
Post-parse fixup for namerefs
ok kettenis@


# 1.63 25-Nov-2006 marco

Don't handle acpiev events inside the gpe handler since this causes deadlocks.

ok jordan@


# 1.62 15-Nov-2006 kettenis

Hook up acpi apic interrupt routing support to generic acpi code.

ok jordan@


# 1.61 02-Nov-2006 marco

Kill acpiec attach function and add its functionality to the generic _HID
device attach

prodded and ok jordan


# 1.60 02-Nov-2006 marco

Fix comments to reflect reality.


# 1.59 19-Oct-2006 marco

Replace ACPI ioctls with APM ioctls. This makes ACPI talk the same protocol
as APM therefore userspace can now retrieve power information in the same
fashion as all other apm arches (zaurus, macppc, i386 etc).

To use force the use of acpi device like so: apmd -f /dev/acpi

ok jordan@


# 1.58 19-Oct-2006 jordan

Fixed GPE debug levels
Fixed ref counter on temporary bufferfields
DSDT parsing fully working on IBM Thinkpad
ok marco@


# 1.57 15-Oct-2006 jordan

Removed old GPE handler code remnants
ok marco@


# 1.56 12-Oct-2006 jordan

Massive ACPI changes
Fixed ACPI leaks
new ACPI API
ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.55 30-Jun-2006 gwk

KNF, No functional change,
"go at it" marco@


# 1.54 30-Jun-2006 jordan

Support for calling Embedded Controller via new GPE interface
ok marco@


# 1.53 30-Jun-2006 jordan

Added new code for GPE handling
Also works with ACPIEC; need to fixup acpiec.c to remove original interrupt handler
ok marco@


# 1.52 30-Jun-2006 jordan

Added offset argument for acpi read/write pmreg routines (new GPE code)
ok marco@


# 1.51 31-May-2006 todd

do not call acpi_powerdown() unless acpi has successfully initialized
ok gwk@ canacar@


# 1.50 29-May-2006 canacar

Add embedded controller support.
ok marco@ deraadt@ testing & ok gwk@


# 1.49 19-May-2006 canacar

Minimal thermal zone driver. Monitors thermal zone temperature,
shuts down the system if the 'critical temperature' is reached.
ok marco@


# 1.48 11-Apr-2006 marco

KNF.


# 1.47 11-Apr-2006 gwk

Add acpi_resume(struct acpi_softc *sc), which calls some AML methods,
detected at attach time, currently unused.

ok marco@


# 1.46 11-Apr-2006 gwk

Discover some ACPI sleep related methods, and call them at the
appropriate time in acpi_enter_sleep_state().

ok marco@


Revision tags: OPENBSD_3_9_BASE
# 1.45 26-Feb-2006 marco

Add acpicpu as a device.

ok jordan@


# 1.44 23-Feb-2006 marco

remove dead function.

ok jordan@


# 1.43 22-Feb-2006 marco

Print out all tables it found during autoconf. We'll need this data in
the future.


# 1.42 22-Feb-2006 marco

Make dmesg less verbose.

ok jordan.


# 1.41 22-Feb-2006 jordan

Added parsing of SSDT tables
ok marco@


# 1.40 22-Feb-2006 jordan

Added new aml_register to notify by device id
Moved powerdown code to button device
ok marco@


# 1.39 21-Feb-2006 marco

Readjust debug levels that crept in too low over the last few days.
KNF


# 1.38 21-Feb-2006 marco

Rig the power button so that the machine shuts down correctly.


# 1.37 20-Feb-2006 marco

Add acpibtn device. Remains disabled for now.


# 1.36 19-Feb-2006 jordan

Added code for aml_notify GPE notification callback
ok marco@


# 1.35 19-Feb-2006 grange

Fix acpi_enter_sleep_state() by replacing current ugly hack
with mostly correct sleeping state transition code.

ok brad@ marco@


# 1.34 19-Feb-2006 marco

jordan:
Enable interrupts.
Added peripheral code to make this work.

marco:
Replace softintr and other types of interrupts with a handler with context.
Fixed interrupt magic for GPEs.
Some KNF

This is work done by jordan and I but has to be a single commit.


# 1.33 17-Feb-2006 marco

Remove earlier introduced delays. We really only need them during autconf.

ok jordan@


# 1.32 17-Feb-2006 marco

Add a generic delay function as per the ACPI spec.

from jordan@


# 1.31 17-Feb-2006 marco

Use tsleep instead of delay when !cold.


# 1.30 17-Feb-2006 marco

Argh, we need delays between reads/writes. The backend reads (i2c most likely)
are so slow that values come back as 0 sometimes. We have to do this
despite the spec saying we shouldnt have to. Guess having a faster
parser than others can hurt sometimes.

Idea from jordan.


# 1.29 17-Feb-2006 jordan

Fixed code for handling _INI
Rearranged debug statements
ok marco@


# 1.28 16-Feb-2006 jordan

Added stub for calling _INI
Fixed aml_dowhile for NULL return
ok marco@


# 1.27 16-Feb-2006 jordan

Fixed parser/evaluator to retrieve battery info+state
Cleanup of parser
Dell laptops return battery status
ok marco@


# 1.26 06-Feb-2006 brad

fix a typo so this will compile on amd64.


# 1.25 04-Feb-2006 marco

When ACPI_ENABLED wasn't set but the devices were it had an unknown variable.

Pointed out by ian@.


# 1.24 03-Feb-2006 jordan

Rewrote aml parser yet again...
Fixed bus_space_map for i386/amd64
Added _BST and _BIF to acpibat to get status/info
ok marco@


# 1.23 20-Jan-2006 jordan

More evaluator changes
Removed old evaluator code
Fixed _HID value
ok marco@


# 1.22 19-Jan-2006 jordan

Added support for read/write of PCI Config space
Help jason@
ok marco@


# 1.21 18-Jan-2006 jordan

Added new evaluation routines
Added helper methods for buffer/field evaluation
Fixed broken indentation on dsdt.c

ok marco@


# 1.20 17-Jan-2006 jordan

Major overhaul of the aml parser. Evaluater temporarily disabled.

ok marco@


# 1.19 06-Jan-2006 grange

Check for ACPI control availability only if ACPI_ENABLE defined.


# 1.18 06-Jan-2006 grange

Informative error messages on attach, prettier dmesg.


# 1.17 06-Jan-2006 grange

printf -> dnprintf conversion for debug messages.


# 1.16 05-Jan-2006 grange

Find SLP_TYPx values and store them for future use in
acpi_enter_sleep_state().


# 1.15 05-Jan-2006 grange

Random KNF and cleanup


# 1.14 05-Jan-2006 grange

Provide symbolic names for the acpi sleeping states and use it.


# 1.13 28-Dec-2005 marco

Moved DEVNAME to a acpidev.h

ok jordan@


# 1.12 18-Dec-2005 sturm

add back comment about wrong code

ok marco


# 1.11 16-Dec-2005 marco

Remove test for kbd_reset. Prompted deraadt@, ok jordan@


# 1.10 16-Dec-2005 jordan

Added hooks for powerdown/power button press


# 1.9 16-Dec-2005 jordan

Cleaned up common power management register i/o
Added enter sleep state ioctl
Hooked power button interrupt
ok marco@


# 1.8 16-Dec-2005 marco

Remove autoconf chatter


# 1.7 16-Dec-2005 jordan

Moved DSDT AML parsing into parent ACPI driver
ok marco@


# 1.6 14-Dec-2005 marco

Add AC device glue.


# 1.5 14-Dec-2005 marco

Adjust debug printing levels to something more sane.


# 1.4 13-Dec-2005 marco

Add glue to attach devices to apci.


# 1.3 07-Dec-2005 marco

KNF


Revision tags: OPENBSD_3_8_BASE
# 1.2 10-Jul-2005 grange

Spacing cleanup.


# 1.1 02-Jun-2005 tholo

Start on a basic ACPI framework -- does not do much more than read out the
ACPI tables into kernel memory and attach ACPI and HPET timers currently.

In order to test this code, enabling the devices in GENERIC as well as
the ACPI_ENABLE option is needed. This code does not do any thermal
control yet, so this should be done with care depending on the platform.

In the tree so more people can contribute to making this more fully
featured.

Ok niklas@ grange@ tedu@


# 1.430 02-Jun-2024 kettenis

Power down PCI devices in S0 as well. Needed for suspend-to-idle.

ok mglocker@


# 1.429 29-May-2024 kettenis

Implement the guts for "suspend-to-idle" on amd64. This enables suspend
on machines that don't support S3. In its current state it doesn't save
a lot of power, but this should improve over time. Implementation of
wakeup methods is incomplete which means that some machine can't resume
at the moment.

ok mglocker@, mlarkin@, stsp@, deraadt@


# 1.428 13-May-2024 kettenis

Ignore button events in the first 10 seconds after resume. On some ACPI
implementations pressing the power button to wake up a machine will cause
a power button notification to happen for that button press This would
initiate a power down, which isn't the user's intent.

Based on earlier diffs from deraadt@ and James Cook.

ok mlarkin@, "I'm not opposed" cheloha@


# 1.427 13-May-2024 jsg

remove prototypes with no matching function
ok mpi@


Revision tags: OPENBSD_7_5_BASE
# 1.426 08-Jan-2024 kettenis

Implement RootPathString support in the LoadTable() AML function. Fixes
booting OpenBSD on some (ancient?) Hyper-V version.

Tested by Henryk Paluch
ok mlarkin@


Revision tags: OPENBSD_7_4_BASE
# 1.425 08-Jul-2023 tobhe

Move /dev/apm related acpi code to acpi_apm.c which is only built on
amd64 and i386. This is the first step towards a machine independent
safe sleep API.

tested by yasuoka@ bluhm@
ok deraadt@ kettenis@


# 1.424 07-Jul-2023 claudio

Add support for multiple batteries to acpithinkpad setchargestart and
setchargestop. With this laptops like x270 or x240 properly set the
thresholds for both batteries.
Tested by kn@ and jmatthew@
OK kettenis@


# 1.423 06-Jul-2023 deraadt

moving acpiioctl to a different file than acpiopen is not going to work
for ramdisks. noticed by anton.
this must be reconsidered.


# 1.422 05-Jul-2023 tobhe

Move acpiioctl to acpi_x86.c, it is only used up on i386 and amd64.

ok kettenis@ deraadt@


# 1.421 29-Jun-2023 dv

acpi(4)/acpibtn(4): use opt-in approach for wakeup GPEs

Previously, any discovered GPE that could be enabled for wake was
being enabled prior to entering ACPI-based S3 or S4. On some newer
machines, this caused S3-based suspend to break as some devices we
don't fully control (e.g. lack of driver) might be capable of waking
the system and will do so almost instantly if the GPE is enabled for
wake up (i.e. the so called "instant wake" issue).

This changes to a model of having device drivers explicitly opt
into using their GPE for wake up.

The first driver to explicitly toggle a GPE for wake is acpibtn(4).

Variations of the above have been in snapshots for 2 weeks, so
tested by many.

ok deraadt@


Revision tags: OPENBSD_7_3_BASE
# 1.420 15-Mar-2023 kettenis

Add missing nameref resolving for the _DEP() package elements that indicate
dependencies between ACPI devices.

ok dlg@, deraadt@


# 1.419 10-Feb-2023 visa

Adjust knote(9) API

Make knote(9) lock the knote list internally, and add knote_locked(9)
for the typical situation where the list is already locked.

Remove the KNOTE(9) macro to simplify the API.

Manual page OK jmc@
OK mpi@ mvs@


Revision tags: OPENBSD_7_2_BASE
# 1.418 13-Sep-2022 kettenis

Split out the code that collects data from acpiac(4), acpibat(4) and
acpisbs(4) for apm(4) and hook it up to the arm64 version of apm(4) on
systems with ACPI.

ok kn@


# 1.417 12-Sep-2022 kettenis

Add support for level-triggered GPIO events.

ok mlarkin@


# 1.416 10-Sep-2022 kettenis

Don't restrict GPIO events to pin numbers below 256. With the _EVT()
method events corresponding to larger numbers are possible.

Fixes lid state detection on the x13s.

ok patrick@


# 1.415 03-Sep-2022 kettenis

Make sure we don't suspend if we have no way to wake up the machine.
The idea is that device drivers that can wake up the machine register
themselves by calling device_register_wakeup(). To prevent regressions
on amd64, we let acpi(4) register itself as a wakeup device if the AML
for the machine defines wakeup devices. This may be refined in the
future.

This diff will prevent people from suspending their arm64 SBCs without
having a way to wake them up. For now the only driver that registers
itself is axppmic(4), which means that at this moment only SBCs with
an allwinner A64 or H5 SoC will actually support suspend/resume.

ok mlarkin@, deraadt@


# 1.414 10-Aug-2022 patrick

Provide the AML root in ACPI's softc so that drivers that need access
don't have to declare it using extern.

Suggested by and with feedback from kettenis@
Tested by deraadt@ on arm64, amd64 and i386
Tested by bluhm@ on amd64 and i386


Revision tags: OPENBSD_7_1_BASE
# 1.413 17-Feb-2022 jsg

reduce includes


# 1.412 10-Feb-2022 visa

Embed klist head in acpi_softc to avoid explicit malloc.

OK kettenis@


# 1.411 09-Feb-2022 deraadt

Move x86-only stubs used by kern/subr_suspend.c into dev/acpi/acpi_x86.c
for sharing between i386 and amd64.
ok mlarkin kettenis


# 1.410 08-Feb-2022 deraadt

The suspend/resume code is a sticky mess of MI, MD, and ACPI sequencing.
This splits out the MI sequencing, backing it with per-architecture helper
functions. Further steps will be neccesary because ACPI and MD are too
tightly coupled, but soon we'll be able to use this code for more architectures
(which depends on figuring out the lowest-level cpu sleeping method)
ok kettenis


# 1.409 04-Feb-2022 robert

set the APM_BATT_CHARGING state if the battery is being charged and try
to calculate the remaining time to be fully charged

ok jca@


# 1.408 02-Feb-2022 deraadt

acpi_addtask() calls malloc() w/ M_NOWAIT (because some calls come from
interrupt context), this however means occasional resource shortage will
result in callbacks registration failing, and unknown consequences for
the task-submitting caller.
Changing this to use pools with a low water mark, decreases the odds
of that problem occuring.
ok kettenis


# 1.407 01-Feb-2022 deraadt

GPE_DIRECT is calling the AML parser from interrupt context, which is
not permitted. Luckily nothing is using GPE_DIRECT anymore, so this code
can be deleted.
ok kettenis


# 1.406 26-Jan-2022 kettenis

An ACPI device needs to be both present and enabled for it to function.
So only attempt to attach hardware that has both bits enabled. This fixes
an issue where com(4) would attach for a disabled serial port leading to
misdetection of the hardware variant and a subsequent hang when /etc/rc
runs ttyflags -a.

ok anton@, deraadt@


# 1.405 12-Jan-2022 patrick

Make acpi_getpropint() return uint64_t, as ACPI integers are in fact that
wide and some _DSD properties depend on it.

ok kettenis@


# 1.404 04-Jan-2022 patrick

acpi_getprop() needs to actually make sure that we're looking at the
correct property. While there adjust acpi_getpropint() as well to
increase similarity with acpi_getprop().

ok kettenis@


# 1.403 16-Dec-2021 anton

Attach com over acpi on amd64. Some hardware uses a different interrupt
assignment compared to the the legacy one supported by com over isa.
This causes the console to halt once userland takes over as no
interrupts are received. The actual address and irq can be read from
ACPI, kettenis@ already added support for arm64 which paved the way for
amd64.

Some consoles that previously attached over isa are now expected to
attach over acpi.

Thanks to patrick@ for testing on arm64.

ok kettenis@


# 1.402 07-Dec-2021 kettenis

Fix parsing of SR_IRQ resource descriptors.

ok patrick@, anton@


# 1.401 02-Nov-2021 deraadt

knf


# 1.400 30-Oct-2021 deraadt

Change hw.perfpolicy=auto by default, at startup. If the system has AC
power connected (default is yes when no driver differentiates) then default
to 100% performance. On battery, use the existing auto algorithm (which is
admittedly somewhat unrefined).
This change overrides the system/BIOS speed and puts OpenBSD in control.
As this happens very early during boot, besides speedups in all usage usage
patterns, some surprises: unhibernate and sysupgrade times are cut in half.
note: on a few architectures, the setperf fn pointer is changed late, and
thus the auto algorithm stops timeing out. kettenis and i will look for
a solution.
in snaps for more than a week.
ok kettenis


Revision tags: OPENBSD_7_0_BASE
# 1.399 20-Jul-2021 mlarkin

Fix trailing whitespace in a few places.

No code change.


# 1.398 09-May-2021 kettenis

Some x86 machines advertise the "hardware reduced" ACPI feature, advertise
S4 and S5 support, but fail to populate the SLEEP_CONTROL_REG and
SLEEP_STATUS_REG descriptions in the FADT. An example of such a machine
is the ASUS Zenbook 14 UM433DA. Any attempt to powerdown the machine
will result in a kernel crash. It turns out that using the legacy ACPI PM
registers works fine on this machien. So fall back on those registers
if SLEEP_CONTROL_REG and/or SLEEP_STATUS_REG aren't provided.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_9_BASE
# 1.397 15-Mar-2021 patrick

Add acpi_iommu_device_map(), which replaces the DMA tag with one that
is blessed with IOMMU magic, if available. This is mainly for arm64,
since on amd64 and i386 the IOMMU only captures PCIe devices, as far
as I know, which uses the pci_probe_device_hook(). This though is for
non-PCI devices attached through ACPI.

ok kettenis@


# 1.396 07-Mar-2021 patrick

Pass standard DMA tag to acpi(4) table drivers.

ok kettenis@


# 1.395 27-Dec-2020 deraadt

acpi_map_address() cannot be wrapped by SMALL_KERNEL anymore, as
it is used by acpihpet.c


# 1.394 25-Dec-2020 visa

Refactor klist insertion and removal

Rename klist_{insert,remove}() to klist_{insert,remove}_locked().
These functions assume that the caller has locked the klist. The current
state of locking remains intact because the kernel lock is still used
with all klists.

Add new functions klist_insert() and klist_remove() that lock the klist
internally. This allows some code simplification.

OK mpi@


# 1.393 17-Dec-2020 kettenis

Fix some issues with referencing named ACPI nodes from Packages.
These references need to be resolved at runtime rather than when
they're parsed such that they pick up the right values for those nodes
which can be changed when for example _INI methods run.

The current approach is to replace these reference with a string that
names the node in question. The problem with that is that packages
can also contain normal strings. Which means that we need special
code that depends on the context where the package is used.

This diff takes a different approach by retaining a reference when
parsing. Code that uses a package will need to resolve this reference
but there is no ambiguiety anymore.

ok patrick@


# 1.392 05-Dec-2020 kettenis

Add support for the _EVT method to the GPIO support code.

ok patrick@


Revision tags: OPENBSD_6_8_BASE
# 1.391 27-Aug-2020 jmatthew

Reorder the acpi attach process so we look at Processor() CPU nodes
after Device() ones, since we should prefer the newer node type.
If we see any Device() nodes, don't attach acpicpu(4) to any Processor()
nodes if they're also present. This also makes acpitz(4) and
acpipwrres(4) devices attach slightly later.

ok kettenis@, also tested by jmc@


# 1.390 16-Aug-2020 gnezdo

Remove sc_maxgpe from acpi_softc which was never read from

Remove a dead conditional from acpi_init_gpes while there.

ok jcs@


# 1.389 21-Jul-2020 deraadt

acpi can use IPL_BIO (a low interrupt) since it only enqueues operations for
later processing. The use of a high interrupt will predate suspend/resume
efforts, we had to redesign acpi to be non-reentrant obviously
discussed with kettenis, in snaps for more than a week


# 1.388 24-Jun-2020 cheloha

kernel: use gettime(9)/getuptime(9) in lieu of time_second(9)/time_uptime(9)

time_second(9) and time_uptime(9) are widely used in the kernel to
quickly get the system UTC or system uptime as a time_t. However,
time_t is 64-bit everywhere, so it is not generally safe to use them
on 32-bit platforms: you have a split-read problem if your hardware
cannot perform atomic 64-bit reads.

This patch replaces time_second(9) with gettime(9), a safer successor
interface, throughout the kernel. Similarly, time_uptime(9) is replaced
with getuptime(9).

There is a performance cost on 32-bit platforms in exchange for
eliminating the split-read problem: instead of two register reads you
now have a lockless read loop to pull the values from the timehands.
This is really not *too* bad in the grand scheme of things, but
compared to what we were doing before it is several times slower.

There is no performance cost on 64-bit (__LP64__) platforms.

With input from visa@, dlg@, and tedu@.

Several bugs squashed by visa@.

ok kettenis@


# 1.387 10-Jun-2020 jca

Give userland a consistent battery and A/C state after resume

Previously we refreshed the state of acpiac(4) and acpibat(4)/acpisbs(4)
by queueing fake ACPI events to be processed by the acpi thread.
acpibat(4) using ACPIDEV_POLL meant that its status could be refreshed
first, resulting in an APM_POWER_CHANGE event sent to userland before
the state of acpiac(4) was up to date. Because of this, apmd(8) could
see A/C unplugged when it was in fact plugged, possibly leading to
a wrong decision.

Instead of using fake ACPI events, refresh the status of acpiac(4) and
acpibat(4)/acpisbs(4) right away at DVACT_WAKEUP time. To reuse the
existing refresh code of those drivers, make their ACPI *_notify()
handlers responsible for sending APM_POWER_CHANGE events instead. Also
queue an APM_POWER_CHANGE event right after resume to retain existing
behavior.

This fixes the consistency of data shown to userland, and makes those
drivers less special.

Initially discussed last year (!) with mpi@ at Miod's place.
Feedback and ok jcs@, deraadt@ agrees with the use of DVACT_WAKEUP here.


# 1.386 29-May-2020 deraadt

dev/rndvar.h no longer has statistical interfaces (removed during various
conversion steps). it only contains kernel prototypes for 4 interfaces,
all of which legitimately belong in sys/systm.h, which are already included
by all enqueue_randomness() users.


# 1.385 14-May-2020 kettenis

Bring back the acpi(4) changes. This time around only use the ACPI _CRS
information on ACPI 5.0 and later.

ok krw@, patrick@


# 1.384 11-May-2020 jca

More accurate remaining power reporting with disparate batteries

Overall remaining power was computed as the average of the remaining
power of each battery, in percents. This doesn't work with batteries of
different capacity, so use the total capacity and remaining power
instead.

Reported by lidstah, successful tests by lidstah and benno@, ok kettenis@


# 1.383 08-May-2020 kettenis

Move parsing of _CRS into acpi(4) and pass the parsed address and interrupt
information in struct acpi_attach_args.

ok mpi@


Revision tags: OPENBSD_6_7_BASE
# 1.382 14-Apr-2020 kettenis

Implement acpi_getprop(), the ACPI equivalent of OF_getprop().

ok patrick@


# 1.381 12-Apr-2020 kettenis

Add support for the _CCA method. This method indicates whether DMA is
cache-coherent or not. To implement this, acpi(4) gets two bus_dma tags
and passes the appropriate one when attaching devices based on _CCA.
On i386/amd64, where for all practical purpose DMA is always cache-coherent,
the two tags are the same. But on arm64 they are distinct.

ok patrick@


# 1.380 07-Apr-2020 visa

Abstract the head of knote lists. This allows extending the lists,
for example, with locking assertions.

OK mpi@, anton@


# 1.379 03-Apr-2020 mpi

Fix incoherencies of errors returned by various *kqfilter routines.

EOPNOTSUPP is returned when there's no "filterops" for a given fd.
EINVAL when the requested filter isn't supported by the "filterops".
ENXIO means the underlying device is no longer valid/present.

ok visa@


# 1.378 20-Feb-2020 visa

Replace field f_isfd with field f_flags in struct filterops to allow
adding more filter properties without cluttering the struct.

OK mpi@, anton@


# 1.377 31-Dec-2019 visa

Use C99 designated initializers with struct filterops. In addition,
make the structs const so that the data are put in .rodata.

OK mpi@, deraadt@, anton@, bluhm@


# 1.376 31-Dec-2019 mpi

Convert infinite sleeps to {m,t}sleep_nsec(9).

ok kn@


# 1.375 31-Dec-2019 jsg

include sys/sensors.h for sensor use
don't depend on indirect inclusion from acpidev.h


Revision tags: OPENBSD_6_6_BASE
# 1.374 07-Sep-2019 kettenis

Revert acpipci(4) changes. Too many systems are broken. We'll try again
after 6.6 as been released. The acpireg.h change stays behind.


# 1.373 28-Aug-2019 kettenis

Use ACPI information to attach PCI busses like we do on arm64. There are a
few additional quirks though, and attaching the PCI busses is delayed to
replicate the existing code more closely. That may be changed in the
future. Also tweak how we handle MSI support and respect to ACPI flag
that says we shouldn't attempt to use MSIs.

Some fallout is expected.

ok patrick@


# 1.372 27-Aug-2019 deraadt

On amd64 the bootup and resume paths for SP/BP/SP were not performing
msr/pat, clock-startup, firmware upgrade, fpu initialization, and cpu
soft-state initialization in the same order. That kind of chaos is
crazy and fragile. Improve the acpi resume call sequence and unify order
of the operations.


# 1.371 02-Jul-2019 jcs

acpi_set_gpehandler: turn edge arg into flags, add support for
passing GPE_DIRECT which directly executes the supplied callback and
clears a level interrupt rather than going through acpi_taskq which
can be slow when other driver callbacks are in the queue.

This should not be used for GPE handlers that need to do anything
ACPI-related.

ok kettenis


# 1.370 10-Jun-2019 kettenis

Print proper ACPI version number.

ok mlarkin@


# 1.369 08-Jun-2019 kettenis

When establishing the mapping between ACPI device nodes and PCI devices,
skip PCI host bridges and devices that aren't there.

ok patrick@


# 1.368 07-Jun-2019 kettenis

Introduce acpi_getsta() and use it to eliminate some duplicated code.

ok patrick@


# 1.367 12-May-2019 kettenis

Make sure the extended PM register descriptions actually exist before
using them. Fixes machines with ancient ACPI 1.0 (such as recent
hypervisors).

ok deraadt@


# 1.366 12-May-2019 kettenis

Add support for mapping ACPI PM registers using the "extended" gasio
representation from the FADT. Mostly fixes the Lanner NCA-1510.

ok lteo@, mlarkin@, deraadt@


# 1.365 11-May-2019 lteo

Fix incorrect error message.

ok kettenis@


# 1.364 08-May-2019 tedu

free size. the allocation is the header plus the length.


Revision tags: OPENBSD_6_5_BASE
# 1.363 04-Apr-2019 kettenis

Avoid potential null-pointer dereference. Found by Kent R. Spillner.

ok deraadt@, jsg@


# 1.362 20-Jan-2019 jsg

fix SMALL_KERNEL build after changes in rev 1.361
reported by naddy@ ok deraadt@


# 1.361 19-Jan-2019 tedu

Add a pwraction sysctl that controls what the power button does on acpi.
By default, nothing changes -- shutdown is initiated. But allows turning
power button into a sleep button if desired.
(grudging) ok from a few parties


# 1.360 26-Oct-2018 kettenis

Add acpipci(4) on amd64. For now this only calls the PCI-specific _OSC
method to let the ACPI implementation know what features we support.


Revision tags: OPENBSD_6_4_BASE
# 1.359 30-Aug-2018 kettenis

Move softbutton check before the check that skips attaching certain acpi
devices. Fixes regression caused by matching on _CID in addition to
matching on _HID.

ok matthieu@, mlarkin@


# 1.358 25-Aug-2018 kettenis

Match ACPI devices based on _CID if no match for _HID is found.

ok mpi@


# 1.357 19-Aug-2018 kettenis

Add support for multiple PCI segments. Only really implemented for arm64
for now as amd64/i386 firmware still caters for legacy OSes that only
support a single PCI segment.

ok patrick@


# 1.356 03-Aug-2018 kettenis

Let ahci(4) match on _CLS instead of _HID when attaching at acpi(4). Avoids
having to add many more _HID entries to the match table.

ok deraadt@, mlarkin@


# 1.355 10-Jul-2018 kettenis

Make legacy interrupts work in acpipci(4).

ok patrick@


# 1.354 05-Jul-2018 kettenis

Add acpipci(4), a driver that supports generic ECAM-compatible PCI host
bridges based on information provided by ACPI.

ok mlarkin@


# 1.353 04-Jul-2018 kettenis

Properly pass around the PCI "chipset tag" in acpi(4) and refactor
acpimcfg(4) to call an MD initialization functions that sets up a tag for
PCI ECAM.

ok guenther@, mlarkin@, krw@


# 1.352 01-Jul-2018 kettenis

Move acpi(4) attach glue into acpi_machdep.c.

ok guenther@, deraadt@


# 1.351 01-Jul-2018 kettenis

Add support for _DSD Device Properties.

ok mlarkin@


# 1.350 30-Jun-2018 kettenis

Add intr_enable() function, intended for MI use to amd64 and i386 and use
this in the acpi(4) suspend/resume code paths.

ok deraadt@


# 1.349 29-Jun-2018 kettenis

Use <stdint.h> types.

ok mpi@, jung@, krw@, deraadt@


# 1.348 29-Jun-2018 mlarkin

ACPI: Allow (with warning) GPE handler reassignment, instead of returning
a failure code.

ok kettenis


# 1.347 26-Jun-2018 mlarkin

knf


# 1.346 26-Jun-2018 kettenis

Let acpi(4) pass down the bus dma tag.

ok deraadt@


# 1.345 25-Jun-2018 kettenis

Reorganize acpi(4) code a bit in preparation for upcoming arm64 support.

ok deraadt@


# 1.344 20-May-2018 kettenis

Wake up the acpi thread after scheduling a gpio event.

ok mpi@


# 1.343 17-May-2018 kettenis

Sprinkle some #ifndef SMALL_KERNEL such that RAMDISK kernels cuild again.

pointed out by stsp@


# 1.342 17-May-2018 kettenis

Implement GenericSerialBus OpRegion support.

ok mlarkin@


# 1.341 27-Mar-2018 kettenis

Add acpicmos(4), a driver that implements SystemCMOS OperationRegion
access support. This fixes machines where the AML doesn't check whether
support for this OperationRegion type has been registered by the OS.

ok mlarkin@


Revision tags: OPENBSD_6_3_BASE
# 1.340 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.339 10-Feb-2018 deraadt

Syncronize filesystems to disk when suspending. Each mountpoint's vnodes
are pushed to disk. Dangling vnodes (unlinked files still in use) and
vnodes undergoing change by long-running syscalls are identified -- and
such filesystems are marked dirty on-disk while we are suspended (in case
power is lost, a fsck will be required). Filesystems without dangling or
busy vnodes are marked clean, resulting in faster boots following
"battery died" circumstances.
Tested by numerous developers, thanks for the feedback.


# 1.338 08-Feb-2018 deraadt

After flushing disks, try again to release all memory. We don't need to
save dirty memory to the hibernate space.


# 1.337 08-Feb-2018 deraadt

Toss all releaseable memory, because fragmentation can get in the way
of allocating the hibernate playpen.


# 1.336 08-Feb-2018 deraadt

sr_quiesce() is a new approach for ensuring that softraid drains
output to the disks.

This is part of a larger suspend/resume filesystem-safety diff, which
has been worked on for a couple of months already. Tests by job, krw,
beck, benno, and others. Sometimes even by snapshot users...


# 1.335 29-Nov-2017 kettenis

Add support for GPIO-signaled events.

ok mlarkin@


# 1.334 16-Nov-2017 jcs

Add PCI attachment for dwiic(4) needed by Intel 100 Series machines

ok kettenis


# 1.333 14-Nov-2017 kettenis

Add a few more PNP IDs to the skip lists.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.332 17-Aug-2017 stsp

Make dwiic(4) attach its ACPI parent dependencies.
This change makes the Asus E200HA keyboard work.

Original analysis and patch by Cesare Gargano
ok kettenis@


# 1.331 09-Aug-2017 dcoppa

Add "PNP0303" (8042 PS/2 Controller) to acpi_isa_hids[]

ok kettenis@


# 1.330 22-Jul-2017 jcs

acpibat: look for _BIX first before falling back on _BIF

ACPI 4.0 deprecated _BIF for battery status so some newer machines
are exporting _BIX which extends _BIF's package with a few extra
fields. When using _BIX, export the new discharge cycle count as a
sensor.

feedback from kettenis and anton
ok deraadt


# 1.329 20-Jul-2017 kettenis

Start a list of isa(4) device names such that we can avoid printing them
as "not configured".

ok mikeb@, millert@, deraadt@


# 1.328 08-Apr-2017 deraadt

Sensors are run as callbacks inside tasks. During suspend a sensor
could be running inside a driver that will be force-detached, or due
to tsleep end up disrupting the softstate/hardstate contract. At
suspend time, quisce all these callbacks by waiting for completion.
This issue has never been observed for real, but may be implicated in
suspend/resume failures.
ok kettenis guenther mlarkin


# 1.327 08-Apr-2017 deraadt

Two prototypes for acpi_maptable; merge to one place


Revision tags: OPENBSD_6_1_BASE
# 1.326 27-Mar-2017 deraadt

Now that hibernate_alloc() only has clean success/failure, don't
need to call hibernate_free() to clean up a partial mess.
ok mlarkin kettenis


# 1.325 13-Mar-2017 deraadt

allow ramdisks to build; ok jcs


# 1.324 12-Mar-2017 jcs

try to attach acpisbs first and if successful, don't attach acpibat


# 1.323 02-Mar-2017 natano

Add a new sysctl machdep.lidaction. The sysctl works as follows:

machdep.lidaction=0 # do nothing
machdep.lidaction=1 # suspend
machdep.lidaction=2 # hibernate

lidsuspend is just an alias for lidaction, so if you change one, the
other one will have the same value. The plan is to remove
machdep.lidsuspend eventually when people have upgraded their
/ets/sysctl.conf.

discussed with deraadt, who came up with the new MIB name
no objections mlarkin
ok stsp halex jcs


# 1.322 28-Feb-2017 natano

Untangle abstract sleep modes from ACPI sleep states. This paves the way
for further work in the area.

idea and ok deraadt
ok mlarkin


# 1.321 22-Feb-2017 jcs

add acpisbs, an acpi smart battery subsystem driver reading data
over smbus

currently disabled because it conflicts with acpibat

ok deraadt, kettenis


# 1.320 14-Jan-2017 kettenis

Revert aml_rdpciaddr changes; breaks several machines that were working
before.


# 1.319 11-Jan-2017 jsg

Increase the size of the buffers used for _HID and _CID strings to be
able to fit "Hyper_V_Gen_Counter_V1".

ok mlarkin@ deraadt@


# 1.318 08-Jan-2017 kettenis

Make aml_rdpciaddr() work for for devices behind bridges. The code now
uses the mapping between AML nodes and PCI devices that we establish.
Because _INIT methods may end up calling aml_rdpciaddr(), make sure we
create that mapping early. Also handle devices that aren't actually present.
These devices are now included in the mapping and reads will return an
all-ones patterm whereas writes are a no-op..

ok mlarkin@


# 1.317 25-Oct-2016 pirofti

Add the resource index to aml_parse_resource() callback function.

This is needed by an upcoming acpiec commit that handles machines breaking
the current ACPI specifications.

Change suggested by and ok kettenis@, guenther@


# 1.316 18-Sep-2016 guenther

If the APIC table (MADT) isn't present then ci_acpi_proc_id won't be set
when acpicpu tries to attach, leading to a NULL dereference. The ACPI
processor object on the Dells where this was reported is bogus and useless
anyway so apply the same test when deciding whether to attach to processor
objects as acpicpu would when matching them to cpus.

Problem reported by Eivind Eide (xenofil (at) gmail.com) and
Johan Huldtgren (johan+openbsd-bugs (at) huldtgren.com).
Testing by Eivind Eide.
ok kettenis@ mlarkin@


# 1.315 03-Sep-2016 naddy

Write the system time back to the RTC every 30 minutes.
This fixes the problem that long-running machines which were not
shut down properly would reboot with a badly offset system time.

hints and ok kettenis@


# 1.314 31-Aug-2016 mlarkin

Don't put a device in the wake device list if _STA says it's not there.

Fixes at least one machine that woke up from sleep / poweroff immediately.

ok kettenis@, tested by kettenis and martijn@


# 1.313 28-Jul-2016 kettenis

Store the acpi processor ID/UID in struct cpu_info, and use it to attach
acpicpu(4) drivers to the right cpu(4).

ok mlarkin@, guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.312 10-Jun-2016 kettenis

The power management timer has been made optional in ACPI 5.0A, so don't
attempt to attach acpitimer(4) if the timer isn't present.

Pointed out by jcs@

ok jcs@


# 1.311 08-May-2016 kettenis

Make sure devices listed by a device's _DEP method are attached before
we attach the device itself. The _DEP methode, introduced in ACPI 5.0,
is a hint that the device in question depends on OpRegion support from
the devices listed by _DEP, so we have to order them the proper way.
To prevent us from attaching those devices again when we encounter them
later walking down the device tree, keep track of our attempts to
attach a device driver to them using aflag in the aml_node structure.

ok guenther@


# 1.310 25-Apr-2016 pirofti

We attach acpihpet(4) and acpiec(4) without using the HID, so do not
report it in the dmesg.

OK deraadt@, mlarkin@.


# 1.309 02-Apr-2016 kettenis

Reduce dmesg spam by knocking out some acpi hardware IDs that don't need a
driver.

ok mpi@


# 1.308 29-Mar-2016 kettenis

Check if a device is present (using _STA) in acpi_foundhid. This prevents
us from reporting devices as "not configured" that aren't actually present,
and allows us to remove duplicated code from several drivers.

ok deraadt@, ok jsg@


# 1.307 29-Mar-2016 kettenis

Implement a cleaner way to prevent acpivout(4) from attaching if we have
a platform-specific method of controlling the backlight.

ok millert@, mlarkin@, jung@


# 1.306 28-Mar-2016 kettenis

Adding each and every new acpi device driver to acpi_foundhid() is
gettinga bit of the burden. And it really isn't how our autoconf
framework is supposed to work for busses that can be enumerated. All
the drivers already check for a matching _HID in their attach
function. So we can just drop the checks in acpi_foundhid() and let
the drivers sort themselves out.

Print a "not configured" line for devices for which we don't attach adriver.
This may be a bit much as there are devices for which we will not have a
driver. This will be removed if it causes too much dmesg spam.

ok deraadt@, mlarkin@


Revision tags: OPENBSD_5_9_BASE
# 1.305 17-Jan-2016 jsg

Take acpi_foundhid() outside of #ifndef SMALL_KERNEL so the ramdisk
kernel can find and attach dwiic(4) and sdhc(4).


# 1.304 17-Jan-2016 jsg

remove an unused global variable


# 1.303 14-Jan-2016 kettenis

Attach dwiic(4) on the Intel Bay Trail i2c controllers as well.


# 1.302 12-Jan-2016 kettenis

Improve emulation of PM registers on Hardware-redcued ACPI by emulating
SPL_TYPx, SLP_EN and WAK_STS using the new SLEEP_STATUS_REG and
SLEEP_CONTROL_REG registers. Makes the transition into S5 (powerdown) work
on the ASUS X205TA.

ok deraadt@, mlarkin@


# 1.301 12-Jan-2016 jcs

Add dwiic, a driver for the Synopsys DesignWare i2c controller found
on the Samsung ATIV Book 9 laptop. This initial version only
supports ACPI config/attachment.

Add ihidev, a HID-over-i2c driver largely based on uhidev. dwiic
handles attaching ihidev devices found in ACPI.

Add ims, a HID-over-i2c mouse/trackpad driver to get basic cursor
and button functionality from HID-compliant i2c trackpads.

ok kettenis deraadt


# 1.300 11-Jan-2016 kettenis

Make sdhc(4) attach to hardware IDs "80860F14" and "PNP0FFF".


# 1.299 09-Jan-2016 kettenis

Initial support for Hardware-Reduced ACPI. This skips the legacy mode checks
by emulating the PM_CNT register such that SCI_EN is always enabled. It also
skip attaching acpitimer(4) since the relevant registers are no longer there.

ok deraadt@, mlarkin@


# 1.298 06-Jan-2016 kettenis

Follow the spec and check the SCI_EN bit to see if ACPI has already
been enabled and don't bother enabling it again if it is. This should fix
systems that don't implement legacy mode and therefore set ACPI_ENABLE and
ACPI_DISABLE to zero.

ok mlarkin@, yasuoka@


# 1.297 23-Nov-2015 reyk

Back out the CRITICAL change, turn it back from 10% to 25%. Some
people with old/broken batteries didn't like this change, and the
underlying heuristics are wrong.


# 1.296 22-Nov-2015 reyk

The "CRITICAL" APM battery level, that is exported/faked by the ACPI
stack, is currently set to 25%. With 25% battery left, a typical
modern laptop will have at least one hour runtime left. It is an
arbitrary value, and other APM power layers in the kernel set CRITICAL
to something like 5% or 10%, so set it to 10% for now.

OK mlarkin@


# 1.295 28-Sep-2015 deraadt

In low-level suspend routines, set cold=2. In tsleep(), use this to
spit out a ddb trace to console. This should allow us to find suspend
or resume routines which break the rules. It depends on the console
output function being non-sleeping.... but that's another codepath which
should try to be safe when cold is set.
ok kettenis


# 1.294 06-Sep-2015 deraadt

clarify malloc arguments (base + length...)


# 1.293 28-Aug-2015 deraadt

fairly simple sizes for free(); ok tedu


# 1.292 20-Aug-2015 kettenis

Respect the access size when reading or writing to pci config space.
This version also makes sure writes are properly aligned, preventing
the panics seen with the previous incarnation of this change.

Fixes battery status passthrough in vmware, brightness keys on my x220
and who knows what else.

ok mlarkin@, deraadt@


Revision tags: OPENBSD_5_8_BASE
# 1.291 04-Aug-2015 deraadt

If we are setting ACPI_PM1_BM_RLD do it only once, and remember to
repeat this action at resume time.
ok mlarkin guenther


# 1.290 29-Jul-2015 deraadt

Something subtle in previous revision is locking up suspend or booting
on some laptops. Mailing lists threads mention x220 and Asus machines.
Reverting for now.


# 1.289 21-Jul-2015 kettenis

iRespect the access size when reading or writing to pci config space.
Fixes battery status passthrough in vmware and who knows what else.

Tested by many.

ok mlarkin@, deraadt@


# 1.288 17-Jul-2015 mlarkin

fix comments


# 1.287 26-Mar-2015 jsg

Use the 0x10 extended capability to determine if the trackstick
buttons are wired to the trackpad and need to be re-routed to the
trackstick. Without this change the buttons on 2015 thinkpads
get picked up as extended buttons that show up as scroll up/down.

Remove the X1 Carbon 2015 (LEN0048) and X250 (LEN0046) from the
top button area/soft buttons quirks list. Also avoid using the
quirk list entirely if the capability bit is set.

Based on work from jcs@ with feedback from mpi@
ok mpi@ jcs@


# 1.286 20-Mar-2015 kettenis

The ACPI standard says that a PCI function number of FFFF can be used to refer
to all functions on a device. This makes no sense in the context of accessing
PCI config space. Nevertheless stsp@ has a machine with AML that tries to
do config space reads from a device where _ADR encodes function number FFFF.
This is almost certainly buggy AML, but since Linux (and presumably Windows)
boot just fine on this machine we need to work around this. Linux effectively
treats function FFFF as function 7, which isn't present on this machine.
Instead we simulate a read from a non-existent device by returning a bit
pattern of all ones. Writes to function FFFF will still panic.

ok mlarkin@


# 1.285 16-Mar-2015 deraadt

Reduce use of SMALL_KERNEL in acpi, bringing acpiec driver and more gpe
handling into RAMDISK. This is now possible because the install media
has ample room. The goal is to reduce special cases where we may be
skipping (unknown) important operations...
ok mlarkin kettenis


# 1.284 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.283 07-Feb-2015 deraadt

New framework that allows hibernate to pass in entropy from it's fresh
boot.
ok mlarkin


# 1.282 06-Feb-2015 mlarkin

Fix a hibernate crash on some machines due to unmapping a page that
may not have been mapped previously (in the failure to hibernate case).

Also ensure that the lowmem ptp is mapped in all cases (not just MP).

ok kettenis


# 1.281 17-Jan-2015 deraadt

resume_randomness() before spinning up other cpus...


# 1.280 15-Jan-2015 jsg

Define a new wscons mouse type for Synaptics clickpad devices that lack
physical buttons. This will be used if the acpi pnpid for the mouse
matches a list provided by Synaptics found in Linux.

Combined with the xenocara changes this will remove the need for an xorg
config file for the x240/t440/t540 generation of thinkpads.

ok matthieu@ shadchin@ kettenis@


# 1.279 11-Jan-2015 kettenis

Tolerate checksum errors in ACPI tables (but complain about them). Apparently
there are machines out there with broken BIOSen with such checksum errors in
thier RSDT tables. Windows runs fine on these machines, so it probably does
not check. Linux ignores checksum errors as well (and prints a warning).

ok deraadt@, krw@


# 1.278 18-Dec-2014 deraadt

suspend and resume the RNG (at what looks like the right spots)
ok reyk djm


# 1.277 09-Dec-2014 doug

Replace some malloc(n*size,...) calls with mallocarray().

ok tedu@ deraadt@


# 1.276 06-Dec-2014 mpi

If "machdep.lidsuspend" is not set, do not go back to sleep if the lid
is still closed when resuming. This allows docked laptop to be resumed
without opening them.

ok mlarkin@


# 1.275 23-Nov-2014 mlarkin

If a machine has no _LIDs, then none can be open. This diff fixes a problem
where machines without any _LIDs were going back to sleep after hibernate
due to the recent suspend-if-lid-closed-on-resume diff. Noticed on qemu,
which doesn't have any _LIDs.

discussed with deraadt


# 1.274 18-Nov-2014 krw

Nuke yet more obvious #include duplications.

ok miod@


# 1.273 08-Nov-2014 mlarkin

If resuming from sleep (zzz/ZZZ) and the lid is still closed, go back to
sleep. This prevents accidental lid flex or slight opening in a backpack
from waking the machine up and leaving it resumed (powered on).

ok deraadt@


# 1.272 26-Sep-2014 kettenis

Rework piglet and pig allocation. Currently the piglet gets allocated
deep down in the suspend path, where it is really hard to recover from
allocation failure. So allocate the piglet early on in the suspend path.
Also change the piglet and piglet allocation functions to use km_alloc(9)
instead of doing pmemrange magic. This removes a bunch of code which, in the
case of the piglet allocation, is broken since it results in a NULL pointer
dereference. Also switch the piglet allocation to not wait. If we can't
allocate 16MB of phys contig memory on a halfway modern machine we're almost
certainly under a lot of memory pressure and we're better off not trying to
hibernate anyway.

ok mlarkin@


# 1.271 25-Sep-2014 kettenis

Bring back rev 1.249. Now that mp_setperf() has been fixed, it should be
safe to restore perflevel when resuming. Should fix the issues people
see with the processor running at full speed heating up the machine while
the apm command reports that the CPUs are running at a low speed.

ok dcoppa@, deraadt@


# 1.270 23-Sep-2014 kettenis

Release the acpi lock around calls to wsdisplay_suspend() and
wsdisplay_resume(). The X server may adjust the display brightness just
before/after the VT switch when suspending/resuming. In fact, it may
already be blocked adjusting the display brightness for some reason
unrelated to the suspend request.

The VT switch request will eventually time out, but the delay is annoying
and the failed VT switch will lead to problems after resume.

Needed for a future update of the xf86-video-intel driver.

ok mlarkin@


# 1.269 19-Sep-2014 kettenis

Use config_suspend_all(9).


# 1.268 08-Sep-2014 kettenis

Clear the PME Status bit on PCI devices in response to Device Wake events.
Fixes the interrupt storm on the 2009 Mac mini with WOL enabled on its
nfe(4) interface.

ok mlarkin@


Revision tags: OPENBSD_5_6_BASE
# 1.267 20-Jul-2014 mlarkin

Support hibernating to softraid crypto volumes.

much help and ok from deraadt@


# 1.266 16-Jul-2014 mlarkin

Reenable hibernate RLE support and flush+zero all memory after unpack.


# 1.265 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.264 12-Jul-2014 kettenis

Add a function to drop all clean pages on the page daemon queues and call
it when we hibernate.

ok mlarkin@, miod@, deraadt@


# 1.263 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.262 11-Jul-2014 mlarkin

Flush the buffercache to 16MB on hibernate and restore its previous max
size (kern.bufcachepercent) on resume, for better hibernate performance.

ok beck@


# 1.261 10-Jul-2014 blambert

remove unneeded header

ok mlarkin@


# 1.260 01-Jul-2014 kettenis

Adding an additonal delay after each access to SystemMemory or SystemIO
during autoconf makes no sense.

ok deraadt@


# 1.259 23-Jun-2014 kettenis

SystemMemory is used to access memory mapped registers on some machines, so we
must use the bus_space(9) API to access it instead of memcpy(9). Also make
sure we properly align access to fields that need more than byte alignment.

ok guenther@


# 1.258 21-May-2014 mlarkin

Fix invalid / missing format specifiers in several dnprintf statements
(used only when ACPI_DEBUG is enabled)

From a diff on tech@ by Fabian Raetz, with a minor change by me, tested
on i386 and amd64 with ACPI_DEBUG enabled and disabled.


# 1.257 25-Apr-2014 mlarkin

Defer the free of the hibernate structures until later, when it is safe to
do so.

Freeing the hibernate structures too early leads to inadvertent calls into
splx when we aren't ready for them yet. Found when trapping splx calls in
the resume path.

discussed with deraadt@ at m2k14


# 1.256 13-Apr-2014 mlarkin

Fix a badly worded debug message that implied the entire machine was going
to sleep, instead of just the acpi thread going to sleep.


# 1.255 31-Mar-2014 mpi

Change the order of operations during a suspend/resume cycle and
call bufq_quiesce() after executing the DVACT_QUIESCE handlers.

This should be safe since no disk nor controller drivers have such
handler but it will allow us to detach sd(4) devices attached to a
USB bus.

Another benefit pointed out by kettenis@ is that drivers that need
to read a firmware from the disk should be able to do it at resume
time in a DVACT_WAKEUP handler.

ok kettenis@, deraadt@


# 1.254 13-Mar-2014 dlg

get rid of the assumption that the head of the alldevs list is the
"mainbus" device. this breaks when mpath is enabled because it
attaches before mainbus and therefore takes the head position.

have autoconf provide device_mainbus() which looks up mainbus_cd,
and use that instead.

discussed with deraadt who just wants mpath stuff to move forward
despite there being many ways to shine this particular turd.


Revision tags: OPENBSD_5_5_BASE
# 1.253 21-Feb-2014 deraadt

Do the lid control further on in the resume sequence, since this is
AML, and will do WAITOK operations. While cold, that is a bad idea.
We seem to be safe with respect to the spec, I do not see any event
debounce issues here.
ok kettenis, tested by many


# 1.252 20-Jan-2014 claudio

Instead of checking sc->sc_revision <= 1 check that the
fadt->hdr_revision <= 1. This is a better check since it checks
the version of the proper descriptor. This allows the alix APU
to reboot since those boards have currently a wrong sc->sc_revision.
Committing this now so that it can be tested easier.
With and OK jsg@ also fine by derradt@


# 1.251 24-Dec-2013 kettenis

If the FADT has its SMI_CMD set to zero, assume we're only ACPI-only hardware
and don't need to disable SMI ownership of the ACPI hardware registers.

ok mlarkin@


# 1.250 23-Dec-2013 kettenis

Revert previous commit. It makes resume hang on some MP systems running
GENERIC.MP.


# 1.249 16-Dec-2013 kettenis

Restore hw.setperf upon resume like we do for apm(4) on i386. Seems we need
to do this fairly late in the resume to avoid hangs.

ok deraadt@, mlarkin@


# 1.248 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


# 1.247 06-Nov-2013 mpi

Add support for Power Resources for Dx states and the necessary hook
for PCI devices. This hook should be called twice, before and after
changing the power state of a PCI device.

Before setting the device to the new state, the ACPI layer will notify
every power resources linked to the device for that state and make sure
they are turned "_ON". After changing the state of the device, it will
decrement the reference of every power resources linked to that device
for the old state and turn them "_OFF" if they are no longer referenced.

This fixes the no-USB after resume problem seen on various ThinkPad,
problem initialy diagnosed with Alexander Polakov.

ok kettenis@, deraadt@


Revision tags: OPENBSD_5_4_BASE
# 1.246 01-Jun-2013 mlarkin

Stop printing ACPI wakeup devices in dmesg after the 16th wakeup device.

Helps with VMware VMs that report hundreds of wakeup devices.

ok reyk@, deraadt@


# 1.245 31-May-2013 bluhm

Fix the build for a kernel without wd and pciide in its config.
OK deraadt@ tedu@


# 1.244 30-May-2013 deraadt

Enforce ca_activate tree-walks over the entire heirarchy for all events,
cleaning up some shutdown-hook related code on the way.
(A few drivers related to sparc are still skipped at kettenis' request)
ok kettenis mlarkin, tested by many others too


# 1.243 18-Apr-2013 deraadt

pin the acpi thread to the BSP, because AML and SMI cannot be trusted. I
was certain we were doing this already, but it turns out no...


Revision tags: OPENBSD_5_3_BASE
# 1.242 24-Dec-2012 guenther

In acpiioctl(), Return though the splx() path if suser() returns an error

ok deraadt@


# 1.241 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


# 1.240 04-Oct-2012 ehrhardt

Use information provided by ACPI to attach secondary PCI host bridges.

ok kettenis@


# 1.239 07-Sep-2012 kettenis

Add a new function called acpi_pci_min_powerstate() that returns the lowest
power state for a PCI device for the power state (currently S3 or S4) we're
transitioning into. This function is probably too simplistic; there is a lot
of (silly) complexity in the ACPI spec.

ok deraadt@


Revision tags: OPENBSD_5_2_BASE
# 1.238 13-Jul-2012 pirofti

Disable acpivideo(4) on ASUS laptops.

There are some models that panic whenever an event is received.
Some cope, some don't. In order to get this right acpiec(4) requires
major rework. Until then let machines boot.

Discussed with deraadt@.


# 1.237 11-Jul-2012 mlarkin

for now, restrict hibernate to root in the acpi ioctl kernel interface.
normal rules for controlling this interface through apmd still apply.

ok deraadt@


# 1.236 09-Jul-2012 deraadt

tiny little oops in untested hibernate block


# 1.235 09-Jul-2012 deraadt

clean up ioctl() case statements


# 1.234 09-Jul-2012 deraadt

acpi_disable_onegpe() is not used; ok pirofti


# 1.233 24-May-2012 kettenis

If _MIN > _MAX consider a WordBus resource to be invalid. Fixes a panic on
the IBM x3550-M4.

ok mikeb@


# 1.232 29-Mar-2012 mlarkin

Missing #ifdef in previous diff


# 1.231 29-Mar-2012 mlarkin

Prevent hibernation attempts on machines with unsupported disk controllers.
The same check is also performed later in the hibernate sequence, but by
then it's too late to cleanly unwind (presently).

ok deraadt


# 1.230 26-Mar-2012 deraadt

simplify conditionals in acpi_sleep_state()
ok mlarkin


# 1.229 26-Mar-2012 deraadt

hook in the hibernate request code; half of this diff is from mlarkin
ok mlarkin


Revision tags: OPENBSD_5_1_BASE
# 1.228 20-Sep-2011 deraadt

Very early in suspend while the clock is still ticking realtime,
save the clock back to the rtc. The zaurus already did this.
ok phessler miod kettenis


Revision tags: OPENBSD_5_0_BASE
# 1.227 02-Jul-2011 nicm

kqueue attach functions should return an errno or 0, not a plain 1. Fix
the obvious cases to return EINVAL and ENXIO.

ok tedu deraadt


# 1.226 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.225 16-Jun-2011 pirofti

Import acpitoshiba driver.

This is originally written by Hiroyuki Aizu, ported and integrated
in our tree by Javier Vazquez <javama4 AT gmail> with tweaks and
suggestions by me.

Import prodded by deraadt@ and the lack of testers on tech@.


# 1.224 27-Apr-2011 jcs

attach acpithinkpad to newer lenovo models like the x120e

ok deraadt@


# 1.223 22-Apr-2011 jordan

Fix uninitialzied variables and formatting strings (-Wxxx errors)


Revision tags: OPENBSD_4_9_BASE
# 1.222 02-Jan-2011 jordan

Re-add code for ACPI taskqueue, eliminates need for state vars in acpi softc
Serialize processing ACPI tasks for sleep/powerdown/GPE event
ok deraadt@


# 1.221 31-Oct-2010 guenther

Revert last commit: it breaks resume on Thinkpads
"then please back it out" deraadt@


# 1.220 26-Oct-2010 jordan

Add task queue for ACPI gpe and notify handlers
This fixes eject on Dell Latitude dock and an issue on the Dell Mini
battery update.
ok deraadt


# 1.219 07-Oct-2010 jordan

Create separate functions for enabling wake and runtime gpes.
Remove old suspend/resume gpewalk function
ok deraadt, mlarkin


# 1.218 05-Oct-2010 jordan

split acpi_enable_onegpe to separate enable/disable functions
ok deraadt


# 1.217 05-Oct-2010 jordan

Added acpi_disable_allgpes function to clear GPE status
ok deraadt


# 1.216 05-Oct-2010 deraadt

clean up acpi_softc use more


# 1.215 05-Oct-2010 deraadt

delete a bit of #if 0 code


# 1.214 05-Oct-2010 deraadt

hide use of our global acpi_softc pointer a bit better


# 1.213 29-Sep-2010 deraadt

grab the acpi thread lock very early in acpi thread startup; not that it
is likely that anyone will race us... but why be careless.
ok kettenis


# 1.212 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.211 29-Aug-2010 jasper

- add a check for FWRITE here too.

ok miod@


# 1.210 27-Aug-2010 kettenis

Make PCI Power Management optional, and only enable it when acpi(4) attaches.

ok deraadt@


Revision tags: OPENBSD_4_8_BASE
# 1.209 08-Aug-2010 kettenis

Close race between the acpi thread and wscons ioctls running in process
context. We might rip this out post-release and replace it with a solution
that serializes things in a more obvious way.

ok deraadt@, miod@ and "those crazy texans who ok anything"


# 1.208 08-Aug-2010 deraadt

Report the acpi sleep states when on a RAMDISK; ok krw


# 1.207 07-Aug-2010 kettenis

Enforce acpi register access rescrictions.

ok mlarkin@, deraadt@, tested by marco@


# 1.206 07-Aug-2010 deraadt

upon resume, notify all the acpiac and acpibat drivers to update
their status. some laptops do not do this automatically.
ok kettenis canacar


# 1.205 06-Aug-2010 marco

Always call _PSW on _LID devices that support it to enable lid open to
resume the machine. Conversely disable it when the machine wakes up.

Tested by several ok deraadt


# 1.204 06-Aug-2010 deraadt

As part of a revamp of the PM1/GPE code, I write this diff to look for
unmanaged EN & STS bits in the PM1 register at interrupt time and report
them. As a side effect this splits the STS acknowledgement into two writes
(for power, and sleep) instead of one. The printf that is added (to spot
unmanaged STS bits) has not yet been triggered as far as we know.

Before the "write to PM1 registers at the right offset" diff went in, this
was not neccessary. But newer thinkpads do not have a working soft power
button without this diff.

We have no idea why.
ok mlarkin kettenis


# 1.203 05-Aug-2010 deraadt

PM1 power button events were generating 'fake events' to acpibtn PWRB,
but if there is no such device these were being tossed. Instead, arrange
for all power button handlers to flag a softc variable which is processed
by the thread; so that one place will know to request a powerdown (psignal
to init)
ok marco mlarkin


# 1.202 05-Aug-2010 deraadt

rename a variable which is annoyingly poorly named


# 1.201 05-Aug-2010 mlarkin

Write to the correct offset for various ACPI pmreg registers. Writing to
the correct register does properly clear the wake status bits and fixed
event bits, which allows some machines to have more than 1 suspend cycle.

bug spotted by deraadt@

tested by deraadt@ and myself on a half-dozen machines and others on
hackers@

ok deraadt@, marco@


# 1.200 05-Aug-2010 deraadt

Fix the power button. The pm1 register is 16 bits in size.
My mistake, a while back.
tested by krw


# 1.199 04-Aug-2010 kettenis

Print supported sleep states.

ok marco@, phessler@, deraadt@


# 1.198 03-Aug-2010 mlarkin

Use the proper flag for re-enabling certain hardware events (power btn,
sleep btn). The (incorrect) flag prevoiously used coincidentally had the
same value, so this shouldn't cause any different behavior than before.

tested on a variety of machines (i386, amd64, sp, mp) - no behavioral
change seen

ok deraadt@


# 1.197 28-Jul-2010 marco

Make all gasio failures look and behave the same.


# 1.196 28-Jul-2010 mlarkin

Delay a little longer on suspend before giving up. Fixes machines that
sometimes don't suspend because the sleep transition takes too long
(symptoms like failing to sleep but the backlight stays on).

ok deraadt@


# 1.195 27-Jul-2010 deraadt

remove XXX; EC can now be attached early when needed; ok jordan


# 1.194 27-Jul-2010 deraadt

more brackets feels good


# 1.193 27-Jul-2010 deraadt

XXX annotate a block in acpi_enter_sleep_state() that is pretty worrying


# 1.192 27-Jul-2010 deraadt

horrific indentation that nearly made me puke


# 1.191 27-Jul-2010 mlarkin

Set the sleeping indicator light on machines that support it, in case SMI
doesn't do it for us. Shows the 'moon' LED on older Thinkpads when
sleeping.

Tested on T43p, W500, T510 by me and on X61s by deraadt@.

ok deraadt@


# 1.190 27-Jul-2010 jordan

Early initialization of acpiec if ECDT table exists
Fixes hang when booting thinkpads while docked
ok deraadt


# 1.189 26-Jul-2010 deraadt

remove merge error which was commited


# 1.188 26-Jul-2010 pirofti

Add support for Sony ACPI hotkeys via a new driver: acpisony(4).

Currently it only works for the suspend button and tries to do right for
the brightness events, but I haven't found a Sony laptop that like to do
right so far.

In the future I want to make the brightness keys work on all Sony's and
also add support for the zoom hotkeys and whatever other funky keys I can
find on those things.

Okay deraadt@.


# 1.187 22-Jul-2010 deraadt

pretty large cleanup of SMALL_KERNEL


# 1.186 21-Jul-2010 deraadt

in the acpi GPE handler, late ack level interrupts not edge interrupts.
Duh.


# 1.185 21-Jul-2010 deraadt

Merge the edge and level gpe handlers into one (now possible since the
gpe structure tells us whether the gpe is edge or level)


# 1.184 21-Jul-2010 deraadt

delete #if 0 code we are not going to use; ok marco


# 1.183 20-Jul-2010 deraadt

make the mask code for acpi_enable_onegpe() match other similar code


# 1.182 20-Jul-2010 deraadt

Handle the acpi interrupt controller with proper edge/level handling,
wrapped in spltty to avoid racing against the interrupt controller.
Repair the gpe bit masking code while there, and do operations in the
right order
ok kettenis mlarkin, help from jordan at figuring out the order of
operations.


# 1.181 20-Jul-2010 matthew

When we're preparing to go to sleep, only call bufq_restart() if other
preparations failed.

ok kettenis@, thib@, deraadt@


# 1.180 19-Jul-2010 deraadt

permit compilation with ACPI_DEBUG


# 1.179 19-Jul-2010 deraadt

there is no need for a seperate dsdt_softc pointer; dsdt does not have a
softc but is just a part of the single acpi driver.
ok jordan


# 1.178 19-Jul-2010 deraadt

acpi_delay() is no longer needed; ok jordan


# 1.177 18-Jul-2010 mlarkin

Clear event status on resume, to avoid going back to sleep/reboot/shutdown
immediately after resume. Tested by a bunch of people on hackers@ on
various hardware. Fixes at least Toshiba NB200 and Gateway LT3103u.

ok marco


# 1.176 13-Jul-2010 deraadt

rename some thread related interfaces so they are logical


# 1.175 10-Jul-2010 jordan

Verify _ADR bdf is correct (some systems return 0xffff)
Works on Dell x9xx and Studio 1555


# 1.174 09-Jul-2010 jordan

Clear the noise


# 1.173 08-Jul-2010 jordan

Add mapping for ACPI device to PCI bdf (match autoconf tree)
Simplify resource parsing function to use buffer argument
Convert namespace linked lists to use queue macros
ok marco@, deraadt@


# 1.172 06-Jul-2010 deraadt

Use spltty() to lock downcalls from apm against the information being
modified by the acpi thread.
ok kettenis


# 1.171 06-Jul-2010 deraadt

Don't use a workq to do the suspend, because it races aginst the acpi
thread. Instead, just tell the acpi thread to do the suspend for us.
This makes apmd & zzz work correctly. While here, have acpithinkpad
attempt to post the event to apm before prompting the suspend itself.
ok kettenis marco mlarkin


# 1.170 05-Jul-2010 mlarkin

Reset register SCI_EN on resume. The ACPI spec clearly states we don't have
to do this, but many machines don't, or forget to, which leads to all sorts
of problems on resume (broken battery stats, overheating, failure to resume
more than once, etc).

tested by many, ok marco@, deraadt@


# 1.169 01-Jul-2010 thib

do bufq quiesce again, since vnd/the bufq_done call in
biodone has been fixed.


# 1.168 01-Jul-2010 jordan

Backout recent AML changes, breaks on R210 and others


# 1.167 01-Jul-2010 jordan

Simplified aml_parse_resource call, cleanup code
ok marco


# 1.166 01-Jul-2010 jordan

Fix subordinate bus # for multi-root PCI buses
ok marco, kettenis


# 1.165 30-Jun-2010 thib

Disable/partially backout the bufq quiesce changes as this
is causing havoc with vnds and release must be buildable.


# 1.164 29-Jun-2010 deraadt

Fix ramdisks (new code was placed inside a SMALL_KERNEL chunk)


# 1.163 29-Jun-2010 jordan

Add support for mapping ACPI to PCI devices
ok kettenis, deraadt


# 1.162 29-Jun-2010 kettenis

Call bufq_quiesce() on suspend to stop all I/O and bufq_restart on resume.
Makes suspend/resume work much better while doing disk I/O.

ok mlarkin@


# 1.161 29-Jun-2010 tedu

60 is a more reasonable guess for number of minutes in an hour than 100.
spotted by TeXitoi. ok deraadt


# 1.160 27-Jun-2010 jordan

Add _acpi_matchhids routine, precursor to pci device match


# 1.159 27-Jun-2010 mlarkin

Clear ACPI fixed event status on resume (power buttons/etc) so that some
machines don't immediately resume after suspending to S3.

Tested by kettenis@ and I on 5 or 6 machines, deraadt@ "yeah, looks good"


# 1.158 27-Jun-2010 jordan

Cleaned up acpi_maptable, adds to table list directly


# 1.157 07-Apr-2010 deraadt

If we have done a wsdisplay_suspend(), and ACPI decides it does not want to
suspend, we must roll back by calling wsdisplay_resume() or the virtual
console switching will be locked forever
ok oga


# 1.156 07-Apr-2010 kettenis

Make suspend/resume work on MP machines (running an MP kernel). Joint work
from mlarkin@ and me, with some amd64 fixes thrown in by deraadt@

ok marco@, deraadt@, pirofti@, mlarkin@


# 1.155 31-Mar-2010 kettenis

Bring /dev/apm support in line with apm(4). Make the suspend button send
a suspend request event to apmd(8) instead of suspending immediately. Also
keep track of whether /dev/apm and /dev/apmctl are currently open, such that
we can still suspend immediately if apmd(8) isn't running.

ok deraadt@, marco@, pirofti@, jsing@, oga@


# 1.154 30-Mar-2010 oga

Prevent the apmd/x races for good.

When we hit suspend time, go through all wsdisplays on the system. if
they are in mode MAPPED, but not MODE_DUMBFB then if possible do a full
vt switch to a !mapped vt, and prevent switching back until resume time.
This has to be called from MD code because this involves userland
running so that X can run the vt switch signal handler. This way, any
case where we are using the "poke registers from userland" model, we
will not be on the hardware when we go down, so the kernel can actually
handle thing properly.

Tested on several acpi laptops (by kettenis@ and ian@), x40 (me and
beck@ at LEAST) and zaurus (me). Maybe others, but if so I forgot who at
this time..

Idea from deraadt somewhere over the Faroe Islands (I thought of a
similar thing myself a while ago). Much prompting from him. Ok and
comments miod@


# 1.153 25-Mar-2010 oga

Fix & vs. && in the kqfilter code.

Based on a comparison with the apm code.

ok deraadt@, kettenis@


Revision tags: OPENBSD_4_7_BASE
# 1.152 26-Nov-2009 mlarkin

Visual (and on some machines, audio) feedback while the machine is
resuming via calls to aml _SST method. This also makes the "moon" LED on
thinkpads to blink during resume and cease when resume is completed.

ok pirofti@


# 1.151 26-Nov-2009 deraadt

Toggle into "cold" when we we disable interrupts, because tsleeping
in the AML interpreter and context switching into userland processes
tends to sometimes pop the ACPI trampoline page out of our address
space. Makes lots more machines work.
ok kettenis mlarkin


# 1.150 24-Nov-2009 jsg

in the resume path evaluate _WAK before dealing with device drivers
like the spec says, not after.

ok mlarkin@


# 1.149 24-Nov-2009 deraadt

abort suspends on SMP systems until that gets written; ok mlarkin


# 1.148 23-Nov-2009 mlarkin

pci should return the result of it's children's suspend/resume calls back
to its parent.

handle suspend failure case and unwind if devices have an activate function
that returns failure.

ok deraadt@, kettenis@


# 1.147 23-Nov-2009 mlarkin

acpi* devices need to call their children on suspend/resume.
ok deraadt@


# 1.146 23-Nov-2009 pirofti

Remove ACPI_SLEEP_ENABLED checks.

This enables by default the suspend/resume paths in the kernel.

Okay deraadt@.


# 1.145 23-Nov-2009 deraadt

knf and spacing that are super annoying


# 1.144 23-Nov-2009 mlarkin

Hook up Theo's new children suspend/resume function, as well as more ACPI
suspend/resume glue.

ok deraadt@


# 1.143 26-Oct-2009 deraadt

Do not do strncmp comparisons on dv_xname because longer device names which
look similar could arrive in the future. Instead, compare directly against
dv_cfdata->cf_driver->cd_name
Issue originally spotted by miod


# 1.142 26-Oct-2009 jordan

Verify checksum+address when loading ACPI tables.
Some systems had invalid entries in RSDT/XSDT.
ok marco@


# 1.141 23-Jul-2009 cnst

New aibs(4) driver for ASUSTeK AI Booster (ACPI ATK0110) hardware monitoring,
with sensor state support through limits provided by the ACPI.

Tested on several ASUS motherboards kindly networked by Sam Fourman Jr.

ok deraadt marco jordan


Revision tags: OPENBSD_4_6_BASE
# 1.140 03-Jun-2009 pirofti

Import acpi power resource driver. This is needed for suspend/resume logic.

Okay jordan@ and marco@.


# 1.139 03-Jun-2009 pirofti

Add acpivideo support. This adds brightness support for all laptops
except thinkpads, they will use the acpithinkpad driver. The driver is
also hooked into wsconsole. So brightness can be adjusted via:

$ wsconsctl display.brightness=<percentage>

This is very helpfull on some laptops that have a nasty bios and get two
steps instead of one when pressing the brightness button.

Tested on various dell, fujitsu, acer, samsung and other laptops.

Okay marco@, miod@. Suggestions from kettenis@.
Lots of reviews and help from miod@, thanks!


# 1.138 03-Jun-2009 jordan

Added ejectable bay for IDE cdroms
ok marco@


# 1.137 30-Apr-2009 marco

Oga changed apm ioctls and forgot to grep. Temporary work around by hitting
the sleep button when we get any request. This interface will chnage at
some point.
ok oga


# 1.136 19-Apr-2009 krw

Call acpi_sleep_walk() when powering down just like when entering
any other sleep state. This tells acpi wake devices to not wake up
the box until manual powering up resets their status. Makes my Dell
GX520 stay powered off in response to 'halt -p'.

In snaps for a while.

ok marco@ (several times) deraadt@


# 1.135 17-Apr-2009 pirofti

Add device ranking support for future sleep/wake developments.

okay marco@


# 1.134 11-Mar-2009 jordan

Cleanup.. use aml_evalinteger instead of aml_evalname
ok marco@


# 1.133 10-Mar-2009 jordan

Fix cut/paste error.. call _BFS not _PTS
ok marco@


Revision tags: OPENBSD_4_5_BASE
# 1.132 21-Feb-2009 marco

overzealous cut/paste; found by Vladimir Kirillov <proger@uaoug.org.ua>


# 1.131 20-Feb-2009 marco

Oops didn't mean for Debugger() to remain there.


# 1.130 19-Feb-2009 marco

suspend/resume bits so that we can develop this in tree. This is disabled.
code from mlarkin and me
help from art,toby,jordan and several others
ok jordan, go for it deraadt


# 1.129 10-Feb-2009 jordan

Fix null pointer for shutdown if acpi not enabled
ok marco@


# 1.128 04-Feb-2009 kettenis

Some systems that claim to be ACPI 2.0 apparently don't provide an XSDT
address. Make the code fall back on the RSDT on those systems.

From Joerg Niendorf <p3y80@internode.on.net>. Fixes PR 5986.


# 1.127 17-Jan-2009 krw

A couple of stray malloc/memset -> malloc(M_ZERO), a bit of knf and
lose a couple of unnecessary casts. No functional change.

Feedback from kettenis@

ok kettenis@ marco@


# 1.126 06-Nov-2008 marco

Collapse all sanity checks of hids into one function.

Originaly from jordan


Revision tags: OPENBSD_4_4_BASE
# 1.125 02-Jul-2008 fgsch

add acpivideo. for now almost an empty skel so others can work on
suspend/resume. eventually it will also manage output switching and
brightness where supported. prodded by marco@


# 1.124 11-Jun-2008 marco

Fix $OpenBSD$ for once and for all


# 1.123 08-Jun-2008 deraadt

aaa_dev was being set from an uninitialized variable; ok toby


# 1.122 07-Jun-2008 marco

Kill ACPIVERBOSE. It was never used and is worthless to boot.

comments jmc
ok toby


# 1.121 06-Jun-2008 marco

Shrink acpi some more for floppies and while here re-enable acpi on the
floppies. Fixes crazy sony vaio kettenis has.

tested by deraadt
ok deraadt jordan kettenis


# 1.120 01-Jun-2008 marco

Fix \n nit in SMALL_KERNEL.


# 1.119 01-Jun-2008 marco

Re-enable new parser. Lots of testing by various folks. Fits all media
now too.

ok kettenis deraadt


# 1.118 16-May-2008 dlg

back out jordans new parser.

it didnt have enough oks from the right people, it did break machines
that were previously working, and it bloated the RAMDISKS and install
media beyond the constraints of the media.

requested by deraadt@


# 1.117 14-May-2008 jordan

Adding new ACPI Parser code
Old guts of code still exists, needs to be torn out next
ok marco@
ok brad@


# 1.116 27-Apr-2008 jcs

add acpithinkpad(4), an acpi support driver for ibm/lenovo
thinkpads.

provides functionality that the bios did on older, non-acpi
thinkpads like respond to screen brightness keys and toggle the
bluetooth device on and off.

ok deraadt@ and marco@


# 1.115 24-Apr-2008 jsing

Introduce acpiasus(4), a driver for the ACPI based hotkeys found in many
ASUS laptops (including the ASUS EeePC) - largely based on NetBSD's
asus(4) driver. On the ASUS EeePC this allows us to enable/disable
wireless, change screen brightness and use the volume keys.

ok jsg@, weingart@


# 1.114 12-Apr-2008 kettenis

Get rid of acpi_s5 global variables; simply send SIGUSR2 instead of SUGUSR1
to tell init(8) to power down the machine.

ok krw@


Revision tags: OPENBSD_4_3_BASE
# 1.113 21-Jan-2008 jsg

Check against RESET_REG_SUP in FADT before doing the acpi reset dance
after all, not doing so appears to break more machines than it fixes.

ok marco@


# 1.112 05-Jan-2008 kettenis

Never attach more acpicpu(4) devices than cpu(4) devices. Always attach
the first acpicpu(4) device on non-MULTIPROCESSOR kernels, regardless of its
ACPI processor ID.

ok mikeb@


# 1.111 05-Dec-2007 deraadt

Hang acpi(4) below bios(4) instead of mainbus(4). This lets us move acpi(4)
before pcibios(4), and then.... if acpi(4) attaches, skip pcibios(4) since
it messes the machines up. Fixes claudio's HP dl320 G5, and almost assuredly
others.
ok kettenis


# 1.110 05-Dec-2007 deraadt

repair acpiopen/acpiclose and friends to handle the /dev/apmctl interface,
so that apmd(8) can actually work correctly against acpi(4), and thus,
even do the fancy -C/-A stuff; also tested by jmc


# 1.109 02-Dec-2007 fgsch

fix the trailing space for real now.


# 1.108 02-Dec-2007 fgsch

avoid trailing spaces on output; marco@ ok.


# 1.107 27-Nov-2007 kettenis

Fix off-by-one that made us attach acpicpu0 and acpicpu1 on GENERIC kernels.

ok marco@


# 1.106 25-Nov-2007 jsg

When available and ACPI is enabled use the ACPI reset method
to reboot. This is required for some quirky sis machines
and likely others to reboot.

Tested by many. 'commit early commit often' deraadt@


# 1.105 19-Nov-2007 deraadt

sigh, amd64 still uses that stupid acpi_enabled variable...


# 1.104 19-Nov-2007 deraadt

softc's come initialized to 0, you know...


# 1.103 19-Nov-2007 deraadt

store revision; from jsg


# 1.102 19-Nov-2007 kettenis

Initialize sc_facs earlier.

ok deraadt@, beck@, canacar@


# 1.101 16-Nov-2007 deraadt

fix the bus_space #define nightmare, so that amd64 and i386 are much more
uniform. as a result shared code like acpi needs less #ifdef's
ok marco kettenis


# 1.100 15-Nov-2007 deraadt

remove the #ifdef ACPI_ENABLE. the EC codepath is either safe enough (and
will be improved), or it is unsafe because AML assumes it must be there
ok almost everyone


# 1.99 15-Nov-2007 mikeb

Make acpicpu(4) attach to the existing CPUs only.

Problem reported by form@. Fix idea by kettenis@.
Lots of help from deraadt@. Tested by deraadt@, form@ and me.

ok deraadt marco


# 1.98 14-Nov-2007 deraadt

spacing


# 1.97 12-Nov-2007 deraadt

correct : vs , in attach-time messages


# 1.96 06-Nov-2007 deraadt

all ACPI_ENABLE kernels need acpi_interrupt()


# 1.95 06-Nov-2007 deraadt

move acpi_read_pmreg() and acpi_write_pmreg() out of SMALL_KERNEL so
that acpi ramdisks can compile (obvious solution)


# 1.94 05-Nov-2007 canacar

Establish the acpi interrupt earlier. Fixes problems with some
GENERIC.MP kernels not getting acpi interrupts. This is a
workaround until the real issue with late establishment is fixed.
ok weingart@, beck@, gwk@


# 1.93 03-Nov-2007 jordan

Added support for displaying wakeup devices
ok beck@,weingart@,gwk@


# 1.92 11-Oct-2007 kettenis

When walking the tree yo find _PRT methods, check whether the device is
actually enabled and functioning. Similar to what we do for _INI.

ok weingart@, beck@


# 1.91 08-Oct-2007 krw

More simple memset(,0,) -> M_ZERO changes. In this batch move to
size(*p) as the first malloc() parameter where p is declared locally
and thus easy to check. Add M_ZERO to gpe_table allocation in acpi.c
even though there is no obvious bzero or memset nearby.


# 1.90 13-Sep-2007 mk

Remove unnecessary memset() (see aml_evalnode(9)) and a bit of KNF.

ok toby


# 1.89 13-Sep-2007 weingart

Implement ACPI 6.5.1 spec tree walk for _STA and _INI.
Fixes mk's laptop. No regressions so far (thank you to the
testers).

ok gwk@, mk@, marco@


Revision tags: OPENBSD_4_2_BASE
# 1.88 17-Apr-2007 mk

Now that acpidock no longer hangs machines during boot (at least
according to the reports I've been getting) it should be safe to enable
it again, so remove the #if 0 stuff around it.

ok marco


# 1.87 11-Apr-2007 jordan

Added changes for C-State
ok marco@


# 1.86 26-Mar-2007 marco

Calculate minutes left more accurately.

From giovanni <qgiovanni@gmail.com>


Revision tags: OPENBSD_4_1_BASE
# 1.85 03-Mar-2007 mk

Stop looking for acpidock until we fix that stuff that breaks all pre-60
series ThinkPads with acpidock(4) enabled.


# 1.84 22-Feb-2007 marco

We were getting no interrupts in .MP. There are 2 fixes; one in
acpi_machdep.c and move the interrupt establishment down after ioapic
has been initialized.

ok jordan


# 1.83 21-Feb-2007 kettenis

Make an acpi-enabled GENERIC.MP work on systems that only support PIC mode.

ok marco@


# 1.82 19-Feb-2007 jordan

Added acpi_map_address function for mapping GAS/IO registers
ok marco@


# 1.81 18-Feb-2007 jordan

Added dynamic perfect hash for opcodes
ok marco@


# 1.80 18-Feb-2007 marco

Shave off another K pre compression.
Shuffle SMALL_KERNEL functions around so that we only have a single #ifndef


# 1.79 17-Feb-2007 marco

Add SMALL_KERNEL throught acpi to make it fit on boot media.
ok deraadt


# 1.78 13-Feb-2007 marco

Add smarter _INI code. This helps with some dissapearing devices.

ok kettenis jordan


# 1.77 31-Jan-2007 gwk

Introduce acpi_hasprocfvs thats "ACPI Has Processor Frequency and Voltage
Scalling" a global flag set by acicpu attach to indicate that ACPI is
handling fvs through the _PSS objects. This will fix disappearing
EST/powernow when booting kernels with acpi enabled, as acpicpu isnt
compiled into GENERIC yet. Also in cases where acpicpu is built in and
legacy EST/powernow work but acpi is missing the _PSS object, the legacy
routines will be used.

tested by dim@, and Nick Nauwelaerts <nick AT nauwelaerts.net>
ok marco@, dim@


# 1.76 22-Jan-2007 mk

Actually look for docking stations by looking for _DCK nodes.

ok marco


# 1.75 02-Jan-2007 marco

On some systems EC must be attached as the first device. Move EC from
generic _HID devices to its own routine to force first attach. Bug found
and debugged by joris@

Code mostly written by joris@


# 1.74 26-Dec-2006 marco

Add polling to devices that require it such as acpibat and acpitz.
Use the same tsleep/wakeup mechanism as the interrupt code to keep it non-
concurrent.


# 1.73 21-Dec-2006 deraadt

even more knf love


# 1.72 21-Dec-2006 marco

Remove some leftover acpiec gpe/interrupt code.


# 1.71 20-Dec-2006 deraadt

oops, name already used


# 1.70 20-Dec-2006 deraadt

rename __acpi_enable_gpe(); ok marco


# 1.69 20-Dec-2006 deraadt

acpi_unmap_pmregs() is not used, ok marco


# 1.68 19-Dec-2006 deraadt

simplify some low-level methods substantially (shrinking code); ok marco jordan


# 1.67 06-Dec-2006 mk

malloc(..., M_NOWAIT) with no NULL check is bad. If allocation fails,
complain, clean up, and bail out of attach.

input and ok gwk


# 1.66 28-Nov-2006 jordan

Fixed clearing arg for acpi_init_pic
ok kettenis@


# 1.65 27-Nov-2006 kettenis

Don't spam dmesg.


# 1.64 27-Nov-2006 jordan

Fixes for acpiprt routing
Post-parse fixup for namerefs
ok kettenis@


# 1.63 25-Nov-2006 marco

Don't handle acpiev events inside the gpe handler since this causes deadlocks.

ok jordan@


# 1.62 15-Nov-2006 kettenis

Hook up acpi apic interrupt routing support to generic acpi code.

ok jordan@


# 1.61 02-Nov-2006 marco

Kill acpiec attach function and add its functionality to the generic _HID
device attach

prodded and ok jordan


# 1.60 02-Nov-2006 marco

Fix comments to reflect reality.


# 1.59 19-Oct-2006 marco

Replace ACPI ioctls with APM ioctls. This makes ACPI talk the same protocol
as APM therefore userspace can now retrieve power information in the same
fashion as all other apm arches (zaurus, macppc, i386 etc).

To use force the use of acpi device like so: apmd -f /dev/acpi

ok jordan@


# 1.58 19-Oct-2006 jordan

Fixed GPE debug levels
Fixed ref counter on temporary bufferfields
DSDT parsing fully working on IBM Thinkpad
ok marco@


# 1.57 15-Oct-2006 jordan

Removed old GPE handler code remnants
ok marco@


# 1.56 12-Oct-2006 jordan

Massive ACPI changes
Fixed ACPI leaks
new ACPI API
ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.55 30-Jun-2006 gwk

KNF, No functional change,
"go at it" marco@


# 1.54 30-Jun-2006 jordan

Support for calling Embedded Controller via new GPE interface
ok marco@


# 1.53 30-Jun-2006 jordan

Added new code for GPE handling
Also works with ACPIEC; need to fixup acpiec.c to remove original interrupt handler
ok marco@


# 1.52 30-Jun-2006 jordan

Added offset argument for acpi read/write pmreg routines (new GPE code)
ok marco@


# 1.51 31-May-2006 todd

do not call acpi_powerdown() unless acpi has successfully initialized
ok gwk@ canacar@


# 1.50 29-May-2006 canacar

Add embedded controller support.
ok marco@ deraadt@ testing & ok gwk@


# 1.49 19-May-2006 canacar

Minimal thermal zone driver. Monitors thermal zone temperature,
shuts down the system if the 'critical temperature' is reached.
ok marco@


# 1.48 11-Apr-2006 marco

KNF.


# 1.47 11-Apr-2006 gwk

Add acpi_resume(struct acpi_softc *sc), which calls some AML methods,
detected at attach time, currently unused.

ok marco@


# 1.46 11-Apr-2006 gwk

Discover some ACPI sleep related methods, and call them at the
appropriate time in acpi_enter_sleep_state().

ok marco@


Revision tags: OPENBSD_3_9_BASE
# 1.45 26-Feb-2006 marco

Add acpicpu as a device.

ok jordan@


# 1.44 23-Feb-2006 marco

remove dead function.

ok jordan@


# 1.43 22-Feb-2006 marco

Print out all tables it found during autoconf. We'll need this data in
the future.


# 1.42 22-Feb-2006 marco

Make dmesg less verbose.

ok jordan.


# 1.41 22-Feb-2006 jordan

Added parsing of SSDT tables
ok marco@


# 1.40 22-Feb-2006 jordan

Added new aml_register to notify by device id
Moved powerdown code to button device
ok marco@


# 1.39 21-Feb-2006 marco

Readjust debug levels that crept in too low over the last few days.
KNF


# 1.38 21-Feb-2006 marco

Rig the power button so that the machine shuts down correctly.


# 1.37 20-Feb-2006 marco

Add acpibtn device. Remains disabled for now.


# 1.36 19-Feb-2006 jordan

Added code for aml_notify GPE notification callback
ok marco@


# 1.35 19-Feb-2006 grange

Fix acpi_enter_sleep_state() by replacing current ugly hack
with mostly correct sleeping state transition code.

ok brad@ marco@


# 1.34 19-Feb-2006 marco

jordan:
Enable interrupts.
Added peripheral code to make this work.

marco:
Replace softintr and other types of interrupts with a handler with context.
Fixed interrupt magic for GPEs.
Some KNF

This is work done by jordan and I but has to be a single commit.


# 1.33 17-Feb-2006 marco

Remove earlier introduced delays. We really only need them during autconf.

ok jordan@


# 1.32 17-Feb-2006 marco

Add a generic delay function as per the ACPI spec.

from jordan@


# 1.31 17-Feb-2006 marco

Use tsleep instead of delay when !cold.


# 1.30 17-Feb-2006 marco

Argh, we need delays between reads/writes. The backend reads (i2c most likely)
are so slow that values come back as 0 sometimes. We have to do this
despite the spec saying we shouldnt have to. Guess having a faster
parser than others can hurt sometimes.

Idea from jordan.


# 1.29 17-Feb-2006 jordan

Fixed code for handling _INI
Rearranged debug statements
ok marco@


# 1.28 16-Feb-2006 jordan

Added stub for calling _INI
Fixed aml_dowhile for NULL return
ok marco@


# 1.27 16-Feb-2006 jordan

Fixed parser/evaluator to retrieve battery info+state
Cleanup of parser
Dell laptops return battery status
ok marco@


# 1.26 06-Feb-2006 brad

fix a typo so this will compile on amd64.


# 1.25 04-Feb-2006 marco

When ACPI_ENABLED wasn't set but the devices were it had an unknown variable.

Pointed out by ian@.


# 1.24 03-Feb-2006 jordan

Rewrote aml parser yet again...
Fixed bus_space_map for i386/amd64
Added _BST and _BIF to acpibat to get status/info
ok marco@


# 1.23 20-Jan-2006 jordan

More evaluator changes
Removed old evaluator code
Fixed _HID value
ok marco@


# 1.22 19-Jan-2006 jordan

Added support for read/write of PCI Config space
Help jason@
ok marco@


# 1.21 18-Jan-2006 jordan

Added new evaluation routines
Added helper methods for buffer/field evaluation
Fixed broken indentation on dsdt.c

ok marco@


# 1.20 17-Jan-2006 jordan

Major overhaul of the aml parser. Evaluater temporarily disabled.

ok marco@


# 1.19 06-Jan-2006 grange

Check for ACPI control availability only if ACPI_ENABLE defined.


# 1.18 06-Jan-2006 grange

Informative error messages on attach, prettier dmesg.


# 1.17 06-Jan-2006 grange

printf -> dnprintf conversion for debug messages.


# 1.16 05-Jan-2006 grange

Find SLP_TYPx values and store them for future use in
acpi_enter_sleep_state().


# 1.15 05-Jan-2006 grange

Random KNF and cleanup


# 1.14 05-Jan-2006 grange

Provide symbolic names for the acpi sleeping states and use it.


# 1.13 28-Dec-2005 marco

Moved DEVNAME to a acpidev.h

ok jordan@


# 1.12 18-Dec-2005 sturm

add back comment about wrong code

ok marco


# 1.11 16-Dec-2005 marco

Remove test for kbd_reset. Prompted deraadt@, ok jordan@


# 1.10 16-Dec-2005 jordan

Added hooks for powerdown/power button press


# 1.9 16-Dec-2005 jordan

Cleaned up common power management register i/o
Added enter sleep state ioctl
Hooked power button interrupt
ok marco@


# 1.8 16-Dec-2005 marco

Remove autoconf chatter


# 1.7 16-Dec-2005 jordan

Moved DSDT AML parsing into parent ACPI driver
ok marco@


# 1.6 14-Dec-2005 marco

Add AC device glue.


# 1.5 14-Dec-2005 marco

Adjust debug printing levels to something more sane.


# 1.4 13-Dec-2005 marco

Add glue to attach devices to apci.


# 1.3 07-Dec-2005 marco

KNF


Revision tags: OPENBSD_3_8_BASE
# 1.2 10-Jul-2005 grange

Spacing cleanup.


# 1.1 02-Jun-2005 tholo

Start on a basic ACPI framework -- does not do much more than read out the
ACPI tables into kernel memory and attach ACPI and HPET timers currently.

In order to test this code, enabling the devices in GENERIC as well as
the ACPI_ENABLE option is needed. This code does not do any thermal
control yet, so this should be done with care depending on the platform.

In the tree so more people can contribute to making this more fully
featured.

Ok niklas@ grange@ tedu@


# 1.429 29-May-2024 kettenis

Implement the guts for "suspend-to-idle" on amd64. This enables suspend
on machines that don't support S3. In its current state it doesn't save
a lot of power, but this should improve over time. Implementation of
wakeup methods is incomplete which means that some machine can't resume
at the moment.

ok mglocker@, mlarkin@, stsp@, deraadt@


# 1.428 13-May-2024 kettenis

Ignore button events in the first 10 seconds after resume. On some ACPI
implementations pressing the power button to wake up a machine will cause
a power button notification to happen for that button press This would
initiate a power down, which isn't the user's intent.

Based on earlier diffs from deraadt@ and James Cook.

ok mlarkin@, "I'm not opposed" cheloha@


# 1.427 13-May-2024 jsg

remove prototypes with no matching function
ok mpi@


Revision tags: OPENBSD_7_5_BASE
# 1.426 08-Jan-2024 kettenis

Implement RootPathString support in the LoadTable() AML function. Fixes
booting OpenBSD on some (ancient?) Hyper-V version.

Tested by Henryk Paluch
ok mlarkin@


Revision tags: OPENBSD_7_4_BASE
# 1.425 08-Jul-2023 tobhe

Move /dev/apm related acpi code to acpi_apm.c which is only built on
amd64 and i386. This is the first step towards a machine independent
safe sleep API.

tested by yasuoka@ bluhm@
ok deraadt@ kettenis@


# 1.424 07-Jul-2023 claudio

Add support for multiple batteries to acpithinkpad setchargestart and
setchargestop. With this laptops like x270 or x240 properly set the
thresholds for both batteries.
Tested by kn@ and jmatthew@
OK kettenis@


# 1.423 06-Jul-2023 deraadt

moving acpiioctl to a different file than acpiopen is not going to work
for ramdisks. noticed by anton.
this must be reconsidered.


# 1.422 05-Jul-2023 tobhe

Move acpiioctl to acpi_x86.c, it is only used up on i386 and amd64.

ok kettenis@ deraadt@


# 1.421 29-Jun-2023 dv

acpi(4)/acpibtn(4): use opt-in approach for wakeup GPEs

Previously, any discovered GPE that could be enabled for wake was
being enabled prior to entering ACPI-based S3 or S4. On some newer
machines, this caused S3-based suspend to break as some devices we
don't fully control (e.g. lack of driver) might be capable of waking
the system and will do so almost instantly if the GPE is enabled for
wake up (i.e. the so called "instant wake" issue).

This changes to a model of having device drivers explicitly opt
into using their GPE for wake up.

The first driver to explicitly toggle a GPE for wake is acpibtn(4).

Variations of the above have been in snapshots for 2 weeks, so
tested by many.

ok deraadt@


Revision tags: OPENBSD_7_3_BASE
# 1.420 15-Mar-2023 kettenis

Add missing nameref resolving for the _DEP() package elements that indicate
dependencies between ACPI devices.

ok dlg@, deraadt@


# 1.419 10-Feb-2023 visa

Adjust knote(9) API

Make knote(9) lock the knote list internally, and add knote_locked(9)
for the typical situation where the list is already locked.

Remove the KNOTE(9) macro to simplify the API.

Manual page OK jmc@
OK mpi@ mvs@


Revision tags: OPENBSD_7_2_BASE
# 1.418 13-Sep-2022 kettenis

Split out the code that collects data from acpiac(4), acpibat(4) and
acpisbs(4) for apm(4) and hook it up to the arm64 version of apm(4) on
systems with ACPI.

ok kn@


# 1.417 12-Sep-2022 kettenis

Add support for level-triggered GPIO events.

ok mlarkin@


# 1.416 10-Sep-2022 kettenis

Don't restrict GPIO events to pin numbers below 256. With the _EVT()
method events corresponding to larger numbers are possible.

Fixes lid state detection on the x13s.

ok patrick@


# 1.415 03-Sep-2022 kettenis

Make sure we don't suspend if we have no way to wake up the machine.
The idea is that device drivers that can wake up the machine register
themselves by calling device_register_wakeup(). To prevent regressions
on amd64, we let acpi(4) register itself as a wakeup device if the AML
for the machine defines wakeup devices. This may be refined in the
future.

This diff will prevent people from suspending their arm64 SBCs without
having a way to wake them up. For now the only driver that registers
itself is axppmic(4), which means that at this moment only SBCs with
an allwinner A64 or H5 SoC will actually support suspend/resume.

ok mlarkin@, deraadt@


# 1.414 10-Aug-2022 patrick

Provide the AML root in ACPI's softc so that drivers that need access
don't have to declare it using extern.

Suggested by and with feedback from kettenis@
Tested by deraadt@ on arm64, amd64 and i386
Tested by bluhm@ on amd64 and i386


Revision tags: OPENBSD_7_1_BASE
# 1.413 17-Feb-2022 jsg

reduce includes


# 1.412 10-Feb-2022 visa

Embed klist head in acpi_softc to avoid explicit malloc.

OK kettenis@


# 1.411 09-Feb-2022 deraadt

Move x86-only stubs used by kern/subr_suspend.c into dev/acpi/acpi_x86.c
for sharing between i386 and amd64.
ok mlarkin kettenis


# 1.410 08-Feb-2022 deraadt

The suspend/resume code is a sticky mess of MI, MD, and ACPI sequencing.
This splits out the MI sequencing, backing it with per-architecture helper
functions. Further steps will be neccesary because ACPI and MD are too
tightly coupled, but soon we'll be able to use this code for more architectures
(which depends on figuring out the lowest-level cpu sleeping method)
ok kettenis


# 1.409 04-Feb-2022 robert

set the APM_BATT_CHARGING state if the battery is being charged and try
to calculate the remaining time to be fully charged

ok jca@


# 1.408 02-Feb-2022 deraadt

acpi_addtask() calls malloc() w/ M_NOWAIT (because some calls come from
interrupt context), this however means occasional resource shortage will
result in callbacks registration failing, and unknown consequences for
the task-submitting caller.
Changing this to use pools with a low water mark, decreases the odds
of that problem occuring.
ok kettenis


# 1.407 01-Feb-2022 deraadt

GPE_DIRECT is calling the AML parser from interrupt context, which is
not permitted. Luckily nothing is using GPE_DIRECT anymore, so this code
can be deleted.
ok kettenis


# 1.406 26-Jan-2022 kettenis

An ACPI device needs to be both present and enabled for it to function.
So only attempt to attach hardware that has both bits enabled. This fixes
an issue where com(4) would attach for a disabled serial port leading to
misdetection of the hardware variant and a subsequent hang when /etc/rc
runs ttyflags -a.

ok anton@, deraadt@


# 1.405 12-Jan-2022 patrick

Make acpi_getpropint() return uint64_t, as ACPI integers are in fact that
wide and some _DSD properties depend on it.

ok kettenis@


# 1.404 04-Jan-2022 patrick

acpi_getprop() needs to actually make sure that we're looking at the
correct property. While there adjust acpi_getpropint() as well to
increase similarity with acpi_getprop().

ok kettenis@


# 1.403 16-Dec-2021 anton

Attach com over acpi on amd64. Some hardware uses a different interrupt
assignment compared to the the legacy one supported by com over isa.
This causes the console to halt once userland takes over as no
interrupts are received. The actual address and irq can be read from
ACPI, kettenis@ already added support for arm64 which paved the way for
amd64.

Some consoles that previously attached over isa are now expected to
attach over acpi.

Thanks to patrick@ for testing on arm64.

ok kettenis@


# 1.402 07-Dec-2021 kettenis

Fix parsing of SR_IRQ resource descriptors.

ok patrick@, anton@


# 1.401 02-Nov-2021 deraadt

knf


# 1.400 30-Oct-2021 deraadt

Change hw.perfpolicy=auto by default, at startup. If the system has AC
power connected (default is yes when no driver differentiates) then default
to 100% performance. On battery, use the existing auto algorithm (which is
admittedly somewhat unrefined).
This change overrides the system/BIOS speed and puts OpenBSD in control.
As this happens very early during boot, besides speedups in all usage usage
patterns, some surprises: unhibernate and sysupgrade times are cut in half.
note: on a few architectures, the setperf fn pointer is changed late, and
thus the auto algorithm stops timeing out. kettenis and i will look for
a solution.
in snaps for more than a week.
ok kettenis


Revision tags: OPENBSD_7_0_BASE
# 1.399 20-Jul-2021 mlarkin

Fix trailing whitespace in a few places.

No code change.


# 1.398 09-May-2021 kettenis

Some x86 machines advertise the "hardware reduced" ACPI feature, advertise
S4 and S5 support, but fail to populate the SLEEP_CONTROL_REG and
SLEEP_STATUS_REG descriptions in the FADT. An example of such a machine
is the ASUS Zenbook 14 UM433DA. Any attempt to powerdown the machine
will result in a kernel crash. It turns out that using the legacy ACPI PM
registers works fine on this machien. So fall back on those registers
if SLEEP_CONTROL_REG and/or SLEEP_STATUS_REG aren't provided.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_9_BASE
# 1.397 15-Mar-2021 patrick

Add acpi_iommu_device_map(), which replaces the DMA tag with one that
is blessed with IOMMU magic, if available. This is mainly for arm64,
since on amd64 and i386 the IOMMU only captures PCIe devices, as far
as I know, which uses the pci_probe_device_hook(). This though is for
non-PCI devices attached through ACPI.

ok kettenis@


# 1.396 07-Mar-2021 patrick

Pass standard DMA tag to acpi(4) table drivers.

ok kettenis@


# 1.395 27-Dec-2020 deraadt

acpi_map_address() cannot be wrapped by SMALL_KERNEL anymore, as
it is used by acpihpet.c


# 1.394 25-Dec-2020 visa

Refactor klist insertion and removal

Rename klist_{insert,remove}() to klist_{insert,remove}_locked().
These functions assume that the caller has locked the klist. The current
state of locking remains intact because the kernel lock is still used
with all klists.

Add new functions klist_insert() and klist_remove() that lock the klist
internally. This allows some code simplification.

OK mpi@


# 1.393 17-Dec-2020 kettenis

Fix some issues with referencing named ACPI nodes from Packages.
These references need to be resolved at runtime rather than when
they're parsed such that they pick up the right values for those nodes
which can be changed when for example _INI methods run.

The current approach is to replace these reference with a string that
names the node in question. The problem with that is that packages
can also contain normal strings. Which means that we need special
code that depends on the context where the package is used.

This diff takes a different approach by retaining a reference when
parsing. Code that uses a package will need to resolve this reference
but there is no ambiguiety anymore.

ok patrick@


# 1.392 05-Dec-2020 kettenis

Add support for the _EVT method to the GPIO support code.

ok patrick@


Revision tags: OPENBSD_6_8_BASE
# 1.391 27-Aug-2020 jmatthew

Reorder the acpi attach process so we look at Processor() CPU nodes
after Device() ones, since we should prefer the newer node type.
If we see any Device() nodes, don't attach acpicpu(4) to any Processor()
nodes if they're also present. This also makes acpitz(4) and
acpipwrres(4) devices attach slightly later.

ok kettenis@, also tested by jmc@


# 1.390 16-Aug-2020 gnezdo

Remove sc_maxgpe from acpi_softc which was never read from

Remove a dead conditional from acpi_init_gpes while there.

ok jcs@


# 1.389 21-Jul-2020 deraadt

acpi can use IPL_BIO (a low interrupt) since it only enqueues operations for
later processing. The use of a high interrupt will predate suspend/resume
efforts, we had to redesign acpi to be non-reentrant obviously
discussed with kettenis, in snaps for more than a week


# 1.388 24-Jun-2020 cheloha

kernel: use gettime(9)/getuptime(9) in lieu of time_second(9)/time_uptime(9)

time_second(9) and time_uptime(9) are widely used in the kernel to
quickly get the system UTC or system uptime as a time_t. However,
time_t is 64-bit everywhere, so it is not generally safe to use them
on 32-bit platforms: you have a split-read problem if your hardware
cannot perform atomic 64-bit reads.

This patch replaces time_second(9) with gettime(9), a safer successor
interface, throughout the kernel. Similarly, time_uptime(9) is replaced
with getuptime(9).

There is a performance cost on 32-bit platforms in exchange for
eliminating the split-read problem: instead of two register reads you
now have a lockless read loop to pull the values from the timehands.
This is really not *too* bad in the grand scheme of things, but
compared to what we were doing before it is several times slower.

There is no performance cost on 64-bit (__LP64__) platforms.

With input from visa@, dlg@, and tedu@.

Several bugs squashed by visa@.

ok kettenis@


# 1.387 10-Jun-2020 jca

Give userland a consistent battery and A/C state after resume

Previously we refreshed the state of acpiac(4) and acpibat(4)/acpisbs(4)
by queueing fake ACPI events to be processed by the acpi thread.
acpibat(4) using ACPIDEV_POLL meant that its status could be refreshed
first, resulting in an APM_POWER_CHANGE event sent to userland before
the state of acpiac(4) was up to date. Because of this, apmd(8) could
see A/C unplugged when it was in fact plugged, possibly leading to
a wrong decision.

Instead of using fake ACPI events, refresh the status of acpiac(4) and
acpibat(4)/acpisbs(4) right away at DVACT_WAKEUP time. To reuse the
existing refresh code of those drivers, make their ACPI *_notify()
handlers responsible for sending APM_POWER_CHANGE events instead. Also
queue an APM_POWER_CHANGE event right after resume to retain existing
behavior.

This fixes the consistency of data shown to userland, and makes those
drivers less special.

Initially discussed last year (!) with mpi@ at Miod's place.
Feedback and ok jcs@, deraadt@ agrees with the use of DVACT_WAKEUP here.


# 1.386 29-May-2020 deraadt

dev/rndvar.h no longer has statistical interfaces (removed during various
conversion steps). it only contains kernel prototypes for 4 interfaces,
all of which legitimately belong in sys/systm.h, which are already included
by all enqueue_randomness() users.


# 1.385 14-May-2020 kettenis

Bring back the acpi(4) changes. This time around only use the ACPI _CRS
information on ACPI 5.0 and later.

ok krw@, patrick@


# 1.384 11-May-2020 jca

More accurate remaining power reporting with disparate batteries

Overall remaining power was computed as the average of the remaining
power of each battery, in percents. This doesn't work with batteries of
different capacity, so use the total capacity and remaining power
instead.

Reported by lidstah, successful tests by lidstah and benno@, ok kettenis@


# 1.383 08-May-2020 kettenis

Move parsing of _CRS into acpi(4) and pass the parsed address and interrupt
information in struct acpi_attach_args.

ok mpi@


Revision tags: OPENBSD_6_7_BASE
# 1.382 14-Apr-2020 kettenis

Implement acpi_getprop(), the ACPI equivalent of OF_getprop().

ok patrick@


# 1.381 12-Apr-2020 kettenis

Add support for the _CCA method. This method indicates whether DMA is
cache-coherent or not. To implement this, acpi(4) gets two bus_dma tags
and passes the appropriate one when attaching devices based on _CCA.
On i386/amd64, where for all practical purpose DMA is always cache-coherent,
the two tags are the same. But on arm64 they are distinct.

ok patrick@


# 1.380 07-Apr-2020 visa

Abstract the head of knote lists. This allows extending the lists,
for example, with locking assertions.

OK mpi@, anton@


# 1.379 03-Apr-2020 mpi

Fix incoherencies of errors returned by various *kqfilter routines.

EOPNOTSUPP is returned when there's no "filterops" for a given fd.
EINVAL when the requested filter isn't supported by the "filterops".
ENXIO means the underlying device is no longer valid/present.

ok visa@


# 1.378 20-Feb-2020 visa

Replace field f_isfd with field f_flags in struct filterops to allow
adding more filter properties without cluttering the struct.

OK mpi@, anton@


# 1.377 31-Dec-2019 visa

Use C99 designated initializers with struct filterops. In addition,
make the structs const so that the data are put in .rodata.

OK mpi@, deraadt@, anton@, bluhm@


# 1.376 31-Dec-2019 mpi

Convert infinite sleeps to {m,t}sleep_nsec(9).

ok kn@


# 1.375 31-Dec-2019 jsg

include sys/sensors.h for sensor use
don't depend on indirect inclusion from acpidev.h


Revision tags: OPENBSD_6_6_BASE
# 1.374 07-Sep-2019 kettenis

Revert acpipci(4) changes. Too many systems are broken. We'll try again
after 6.6 as been released. The acpireg.h change stays behind.


# 1.373 28-Aug-2019 kettenis

Use ACPI information to attach PCI busses like we do on arm64. There are a
few additional quirks though, and attaching the PCI busses is delayed to
replicate the existing code more closely. That may be changed in the
future. Also tweak how we handle MSI support and respect to ACPI flag
that says we shouldn't attempt to use MSIs.

Some fallout is expected.

ok patrick@


# 1.372 27-Aug-2019 deraadt

On amd64 the bootup and resume paths for SP/BP/SP were not performing
msr/pat, clock-startup, firmware upgrade, fpu initialization, and cpu
soft-state initialization in the same order. That kind of chaos is
crazy and fragile. Improve the acpi resume call sequence and unify order
of the operations.


# 1.371 02-Jul-2019 jcs

acpi_set_gpehandler: turn edge arg into flags, add support for
passing GPE_DIRECT which directly executes the supplied callback and
clears a level interrupt rather than going through acpi_taskq which
can be slow when other driver callbacks are in the queue.

This should not be used for GPE handlers that need to do anything
ACPI-related.

ok kettenis


# 1.370 10-Jun-2019 kettenis

Print proper ACPI version number.

ok mlarkin@


# 1.369 08-Jun-2019 kettenis

When establishing the mapping between ACPI device nodes and PCI devices,
skip PCI host bridges and devices that aren't there.

ok patrick@


# 1.368 07-Jun-2019 kettenis

Introduce acpi_getsta() and use it to eliminate some duplicated code.

ok patrick@


# 1.367 12-May-2019 kettenis

Make sure the extended PM register descriptions actually exist before
using them. Fixes machines with ancient ACPI 1.0 (such as recent
hypervisors).

ok deraadt@


# 1.366 12-May-2019 kettenis

Add support for mapping ACPI PM registers using the "extended" gasio
representation from the FADT. Mostly fixes the Lanner NCA-1510.

ok lteo@, mlarkin@, deraadt@


# 1.365 11-May-2019 lteo

Fix incorrect error message.

ok kettenis@


# 1.364 08-May-2019 tedu

free size. the allocation is the header plus the length.


Revision tags: OPENBSD_6_5_BASE
# 1.363 04-Apr-2019 kettenis

Avoid potential null-pointer dereference. Found by Kent R. Spillner.

ok deraadt@, jsg@


# 1.362 20-Jan-2019 jsg

fix SMALL_KERNEL build after changes in rev 1.361
reported by naddy@ ok deraadt@


# 1.361 19-Jan-2019 tedu

Add a pwraction sysctl that controls what the power button does on acpi.
By default, nothing changes -- shutdown is initiated. But allows turning
power button into a sleep button if desired.
(grudging) ok from a few parties


# 1.360 26-Oct-2018 kettenis

Add acpipci(4) on amd64. For now this only calls the PCI-specific _OSC
method to let the ACPI implementation know what features we support.


Revision tags: OPENBSD_6_4_BASE
# 1.359 30-Aug-2018 kettenis

Move softbutton check before the check that skips attaching certain acpi
devices. Fixes regression caused by matching on _CID in addition to
matching on _HID.

ok matthieu@, mlarkin@


# 1.358 25-Aug-2018 kettenis

Match ACPI devices based on _CID if no match for _HID is found.

ok mpi@


# 1.357 19-Aug-2018 kettenis

Add support for multiple PCI segments. Only really implemented for arm64
for now as amd64/i386 firmware still caters for legacy OSes that only
support a single PCI segment.

ok patrick@


# 1.356 03-Aug-2018 kettenis

Let ahci(4) match on _CLS instead of _HID when attaching at acpi(4). Avoids
having to add many more _HID entries to the match table.

ok deraadt@, mlarkin@


# 1.355 10-Jul-2018 kettenis

Make legacy interrupts work in acpipci(4).

ok patrick@


# 1.354 05-Jul-2018 kettenis

Add acpipci(4), a driver that supports generic ECAM-compatible PCI host
bridges based on information provided by ACPI.

ok mlarkin@


# 1.353 04-Jul-2018 kettenis

Properly pass around the PCI "chipset tag" in acpi(4) and refactor
acpimcfg(4) to call an MD initialization functions that sets up a tag for
PCI ECAM.

ok guenther@, mlarkin@, krw@


# 1.352 01-Jul-2018 kettenis

Move acpi(4) attach glue into acpi_machdep.c.

ok guenther@, deraadt@


# 1.351 01-Jul-2018 kettenis

Add support for _DSD Device Properties.

ok mlarkin@


# 1.350 30-Jun-2018 kettenis

Add intr_enable() function, intended for MI use to amd64 and i386 and use
this in the acpi(4) suspend/resume code paths.

ok deraadt@


# 1.349 29-Jun-2018 kettenis

Use <stdint.h> types.

ok mpi@, jung@, krw@, deraadt@


# 1.348 29-Jun-2018 mlarkin

ACPI: Allow (with warning) GPE handler reassignment, instead of returning
a failure code.

ok kettenis


# 1.347 26-Jun-2018 mlarkin

knf


# 1.346 26-Jun-2018 kettenis

Let acpi(4) pass down the bus dma tag.

ok deraadt@


# 1.345 25-Jun-2018 kettenis

Reorganize acpi(4) code a bit in preparation for upcoming arm64 support.

ok deraadt@


# 1.344 20-May-2018 kettenis

Wake up the acpi thread after scheduling a gpio event.

ok mpi@


# 1.343 17-May-2018 kettenis

Sprinkle some #ifndef SMALL_KERNEL such that RAMDISK kernels cuild again.

pointed out by stsp@


# 1.342 17-May-2018 kettenis

Implement GenericSerialBus OpRegion support.

ok mlarkin@


# 1.341 27-Mar-2018 kettenis

Add acpicmos(4), a driver that implements SystemCMOS OperationRegion
access support. This fixes machines where the AML doesn't check whether
support for this OperationRegion type has been registered by the OS.

ok mlarkin@


Revision tags: OPENBSD_6_3_BASE
# 1.340 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.339 10-Feb-2018 deraadt

Syncronize filesystems to disk when suspending. Each mountpoint's vnodes
are pushed to disk. Dangling vnodes (unlinked files still in use) and
vnodes undergoing change by long-running syscalls are identified -- and
such filesystems are marked dirty on-disk while we are suspended (in case
power is lost, a fsck will be required). Filesystems without dangling or
busy vnodes are marked clean, resulting in faster boots following
"battery died" circumstances.
Tested by numerous developers, thanks for the feedback.


# 1.338 08-Feb-2018 deraadt

After flushing disks, try again to release all memory. We don't need to
save dirty memory to the hibernate space.


# 1.337 08-Feb-2018 deraadt

Toss all releaseable memory, because fragmentation can get in the way
of allocating the hibernate playpen.


# 1.336 08-Feb-2018 deraadt

sr_quiesce() is a new approach for ensuring that softraid drains
output to the disks.

This is part of a larger suspend/resume filesystem-safety diff, which
has been worked on for a couple of months already. Tests by job, krw,
beck, benno, and others. Sometimes even by snapshot users...


# 1.335 29-Nov-2017 kettenis

Add support for GPIO-signaled events.

ok mlarkin@


# 1.334 16-Nov-2017 jcs

Add PCI attachment for dwiic(4) needed by Intel 100 Series machines

ok kettenis


# 1.333 14-Nov-2017 kettenis

Add a few more PNP IDs to the skip lists.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.332 17-Aug-2017 stsp

Make dwiic(4) attach its ACPI parent dependencies.
This change makes the Asus E200HA keyboard work.

Original analysis and patch by Cesare Gargano
ok kettenis@


# 1.331 09-Aug-2017 dcoppa

Add "PNP0303" (8042 PS/2 Controller) to acpi_isa_hids[]

ok kettenis@


# 1.330 22-Jul-2017 jcs

acpibat: look for _BIX first before falling back on _BIF

ACPI 4.0 deprecated _BIF for battery status so some newer machines
are exporting _BIX which extends _BIF's package with a few extra
fields. When using _BIX, export the new discharge cycle count as a
sensor.

feedback from kettenis and anton
ok deraadt


# 1.329 20-Jul-2017 kettenis

Start a list of isa(4) device names such that we can avoid printing them
as "not configured".

ok mikeb@, millert@, deraadt@


# 1.328 08-Apr-2017 deraadt

Sensors are run as callbacks inside tasks. During suspend a sensor
could be running inside a driver that will be force-detached, or due
to tsleep end up disrupting the softstate/hardstate contract. At
suspend time, quisce all these callbacks by waiting for completion.
This issue has never been observed for real, but may be implicated in
suspend/resume failures.
ok kettenis guenther mlarkin


# 1.327 08-Apr-2017 deraadt

Two prototypes for acpi_maptable; merge to one place


Revision tags: OPENBSD_6_1_BASE
# 1.326 27-Mar-2017 deraadt

Now that hibernate_alloc() only has clean success/failure, don't
need to call hibernate_free() to clean up a partial mess.
ok mlarkin kettenis


# 1.325 13-Mar-2017 deraadt

allow ramdisks to build; ok jcs


# 1.324 12-Mar-2017 jcs

try to attach acpisbs first and if successful, don't attach acpibat


# 1.323 02-Mar-2017 natano

Add a new sysctl machdep.lidaction. The sysctl works as follows:

machdep.lidaction=0 # do nothing
machdep.lidaction=1 # suspend
machdep.lidaction=2 # hibernate

lidsuspend is just an alias for lidaction, so if you change one, the
other one will have the same value. The plan is to remove
machdep.lidsuspend eventually when people have upgraded their
/ets/sysctl.conf.

discussed with deraadt, who came up with the new MIB name
no objections mlarkin
ok stsp halex jcs


# 1.322 28-Feb-2017 natano

Untangle abstract sleep modes from ACPI sleep states. This paves the way
for further work in the area.

idea and ok deraadt
ok mlarkin


# 1.321 22-Feb-2017 jcs

add acpisbs, an acpi smart battery subsystem driver reading data
over smbus

currently disabled because it conflicts with acpibat

ok deraadt, kettenis


# 1.320 14-Jan-2017 kettenis

Revert aml_rdpciaddr changes; breaks several machines that were working
before.


# 1.319 11-Jan-2017 jsg

Increase the size of the buffers used for _HID and _CID strings to be
able to fit "Hyper_V_Gen_Counter_V1".

ok mlarkin@ deraadt@


# 1.318 08-Jan-2017 kettenis

Make aml_rdpciaddr() work for for devices behind bridges. The code now
uses the mapping between AML nodes and PCI devices that we establish.
Because _INIT methods may end up calling aml_rdpciaddr(), make sure we
create that mapping early. Also handle devices that aren't actually present.
These devices are now included in the mapping and reads will return an
all-ones patterm whereas writes are a no-op..

ok mlarkin@


# 1.317 25-Oct-2016 pirofti

Add the resource index to aml_parse_resource() callback function.

This is needed by an upcoming acpiec commit that handles machines breaking
the current ACPI specifications.

Change suggested by and ok kettenis@, guenther@


# 1.316 18-Sep-2016 guenther

If the APIC table (MADT) isn't present then ci_acpi_proc_id won't be set
when acpicpu tries to attach, leading to a NULL dereference. The ACPI
processor object on the Dells where this was reported is bogus and useless
anyway so apply the same test when deciding whether to attach to processor
objects as acpicpu would when matching them to cpus.

Problem reported by Eivind Eide (xenofil (at) gmail.com) and
Johan Huldtgren (johan+openbsd-bugs (at) huldtgren.com).
Testing by Eivind Eide.
ok kettenis@ mlarkin@


# 1.315 03-Sep-2016 naddy

Write the system time back to the RTC every 30 minutes.
This fixes the problem that long-running machines which were not
shut down properly would reboot with a badly offset system time.

hints and ok kettenis@


# 1.314 31-Aug-2016 mlarkin

Don't put a device in the wake device list if _STA says it's not there.

Fixes at least one machine that woke up from sleep / poweroff immediately.

ok kettenis@, tested by kettenis and martijn@


# 1.313 28-Jul-2016 kettenis

Store the acpi processor ID/UID in struct cpu_info, and use it to attach
acpicpu(4) drivers to the right cpu(4).

ok mlarkin@, guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.312 10-Jun-2016 kettenis

The power management timer has been made optional in ACPI 5.0A, so don't
attempt to attach acpitimer(4) if the timer isn't present.

Pointed out by jcs@

ok jcs@


# 1.311 08-May-2016 kettenis

Make sure devices listed by a device's _DEP method are attached before
we attach the device itself. The _DEP methode, introduced in ACPI 5.0,
is a hint that the device in question depends on OpRegion support from
the devices listed by _DEP, so we have to order them the proper way.
To prevent us from attaching those devices again when we encounter them
later walking down the device tree, keep track of our attempts to
attach a device driver to them using aflag in the aml_node structure.

ok guenther@


# 1.310 25-Apr-2016 pirofti

We attach acpihpet(4) and acpiec(4) without using the HID, so do not
report it in the dmesg.

OK deraadt@, mlarkin@.


# 1.309 02-Apr-2016 kettenis

Reduce dmesg spam by knocking out some acpi hardware IDs that don't need a
driver.

ok mpi@


# 1.308 29-Mar-2016 kettenis

Check if a device is present (using _STA) in acpi_foundhid. This prevents
us from reporting devices as "not configured" that aren't actually present,
and allows us to remove duplicated code from several drivers.

ok deraadt@, ok jsg@


# 1.307 29-Mar-2016 kettenis

Implement a cleaner way to prevent acpivout(4) from attaching if we have
a platform-specific method of controlling the backlight.

ok millert@, mlarkin@, jung@


# 1.306 28-Mar-2016 kettenis

Adding each and every new acpi device driver to acpi_foundhid() is
gettinga bit of the burden. And it really isn't how our autoconf
framework is supposed to work for busses that can be enumerated. All
the drivers already check for a matching _HID in their attach
function. So we can just drop the checks in acpi_foundhid() and let
the drivers sort themselves out.

Print a "not configured" line for devices for which we don't attach adriver.
This may be a bit much as there are devices for which we will not have a
driver. This will be removed if it causes too much dmesg spam.

ok deraadt@, mlarkin@


Revision tags: OPENBSD_5_9_BASE
# 1.305 17-Jan-2016 jsg

Take acpi_foundhid() outside of #ifndef SMALL_KERNEL so the ramdisk
kernel can find and attach dwiic(4) and sdhc(4).


# 1.304 17-Jan-2016 jsg

remove an unused global variable


# 1.303 14-Jan-2016 kettenis

Attach dwiic(4) on the Intel Bay Trail i2c controllers as well.


# 1.302 12-Jan-2016 kettenis

Improve emulation of PM registers on Hardware-redcued ACPI by emulating
SPL_TYPx, SLP_EN and WAK_STS using the new SLEEP_STATUS_REG and
SLEEP_CONTROL_REG registers. Makes the transition into S5 (powerdown) work
on the ASUS X205TA.

ok deraadt@, mlarkin@


# 1.301 12-Jan-2016 jcs

Add dwiic, a driver for the Synopsys DesignWare i2c controller found
on the Samsung ATIV Book 9 laptop. This initial version only
supports ACPI config/attachment.

Add ihidev, a HID-over-i2c driver largely based on uhidev. dwiic
handles attaching ihidev devices found in ACPI.

Add ims, a HID-over-i2c mouse/trackpad driver to get basic cursor
and button functionality from HID-compliant i2c trackpads.

ok kettenis deraadt


# 1.300 11-Jan-2016 kettenis

Make sdhc(4) attach to hardware IDs "80860F14" and "PNP0FFF".


# 1.299 09-Jan-2016 kettenis

Initial support for Hardware-Reduced ACPI. This skips the legacy mode checks
by emulating the PM_CNT register such that SCI_EN is always enabled. It also
skip attaching acpitimer(4) since the relevant registers are no longer there.

ok deraadt@, mlarkin@


# 1.298 06-Jan-2016 kettenis

Follow the spec and check the SCI_EN bit to see if ACPI has already
been enabled and don't bother enabling it again if it is. This should fix
systems that don't implement legacy mode and therefore set ACPI_ENABLE and
ACPI_DISABLE to zero.

ok mlarkin@, yasuoka@


# 1.297 23-Nov-2015 reyk

Back out the CRITICAL change, turn it back from 10% to 25%. Some
people with old/broken batteries didn't like this change, and the
underlying heuristics are wrong.


# 1.296 22-Nov-2015 reyk

The "CRITICAL" APM battery level, that is exported/faked by the ACPI
stack, is currently set to 25%. With 25% battery left, a typical
modern laptop will have at least one hour runtime left. It is an
arbitrary value, and other APM power layers in the kernel set CRITICAL
to something like 5% or 10%, so set it to 10% for now.

OK mlarkin@


# 1.295 28-Sep-2015 deraadt

In low-level suspend routines, set cold=2. In tsleep(), use this to
spit out a ddb trace to console. This should allow us to find suspend
or resume routines which break the rules. It depends on the console
output function being non-sleeping.... but that's another codepath which
should try to be safe when cold is set.
ok kettenis


# 1.294 06-Sep-2015 deraadt

clarify malloc arguments (base + length...)


# 1.293 28-Aug-2015 deraadt

fairly simple sizes for free(); ok tedu


# 1.292 20-Aug-2015 kettenis

Respect the access size when reading or writing to pci config space.
This version also makes sure writes are properly aligned, preventing
the panics seen with the previous incarnation of this change.

Fixes battery status passthrough in vmware, brightness keys on my x220
and who knows what else.

ok mlarkin@, deraadt@


Revision tags: OPENBSD_5_8_BASE
# 1.291 04-Aug-2015 deraadt

If we are setting ACPI_PM1_BM_RLD do it only once, and remember to
repeat this action at resume time.
ok mlarkin guenther


# 1.290 29-Jul-2015 deraadt

Something subtle in previous revision is locking up suspend or booting
on some laptops. Mailing lists threads mention x220 and Asus machines.
Reverting for now.


# 1.289 21-Jul-2015 kettenis

iRespect the access size when reading or writing to pci config space.
Fixes battery status passthrough in vmware and who knows what else.

Tested by many.

ok mlarkin@, deraadt@


# 1.288 17-Jul-2015 mlarkin

fix comments


# 1.287 26-Mar-2015 jsg

Use the 0x10 extended capability to determine if the trackstick
buttons are wired to the trackpad and need to be re-routed to the
trackstick. Without this change the buttons on 2015 thinkpads
get picked up as extended buttons that show up as scroll up/down.

Remove the X1 Carbon 2015 (LEN0048) and X250 (LEN0046) from the
top button area/soft buttons quirks list. Also avoid using the
quirk list entirely if the capability bit is set.

Based on work from jcs@ with feedback from mpi@
ok mpi@ jcs@


# 1.286 20-Mar-2015 kettenis

The ACPI standard says that a PCI function number of FFFF can be used to refer
to all functions on a device. This makes no sense in the context of accessing
PCI config space. Nevertheless stsp@ has a machine with AML that tries to
do config space reads from a device where _ADR encodes function number FFFF.
This is almost certainly buggy AML, but since Linux (and presumably Windows)
boot just fine on this machine we need to work around this. Linux effectively
treats function FFFF as function 7, which isn't present on this machine.
Instead we simulate a read from a non-existent device by returning a bit
pattern of all ones. Writes to function FFFF will still panic.

ok mlarkin@


# 1.285 16-Mar-2015 deraadt

Reduce use of SMALL_KERNEL in acpi, bringing acpiec driver and more gpe
handling into RAMDISK. This is now possible because the install media
has ample room. The goal is to reduce special cases where we may be
skipping (unknown) important operations...
ok mlarkin kettenis


# 1.284 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.283 07-Feb-2015 deraadt

New framework that allows hibernate to pass in entropy from it's fresh
boot.
ok mlarkin


# 1.282 06-Feb-2015 mlarkin

Fix a hibernate crash on some machines due to unmapping a page that
may not have been mapped previously (in the failure to hibernate case).

Also ensure that the lowmem ptp is mapped in all cases (not just MP).

ok kettenis


# 1.281 17-Jan-2015 deraadt

resume_randomness() before spinning up other cpus...


# 1.280 15-Jan-2015 jsg

Define a new wscons mouse type for Synaptics clickpad devices that lack
physical buttons. This will be used if the acpi pnpid for the mouse
matches a list provided by Synaptics found in Linux.

Combined with the xenocara changes this will remove the need for an xorg
config file for the x240/t440/t540 generation of thinkpads.

ok matthieu@ shadchin@ kettenis@


# 1.279 11-Jan-2015 kettenis

Tolerate checksum errors in ACPI tables (but complain about them). Apparently
there are machines out there with broken BIOSen with such checksum errors in
thier RSDT tables. Windows runs fine on these machines, so it probably does
not check. Linux ignores checksum errors as well (and prints a warning).

ok deraadt@, krw@


# 1.278 18-Dec-2014 deraadt

suspend and resume the RNG (at what looks like the right spots)
ok reyk djm


# 1.277 09-Dec-2014 doug

Replace some malloc(n*size,...) calls with mallocarray().

ok tedu@ deraadt@


# 1.276 06-Dec-2014 mpi

If "machdep.lidsuspend" is not set, do not go back to sleep if the lid
is still closed when resuming. This allows docked laptop to be resumed
without opening them.

ok mlarkin@


# 1.275 23-Nov-2014 mlarkin

If a machine has no _LIDs, then none can be open. This diff fixes a problem
where machines without any _LIDs were going back to sleep after hibernate
due to the recent suspend-if-lid-closed-on-resume diff. Noticed on qemu,
which doesn't have any _LIDs.

discussed with deraadt


# 1.274 18-Nov-2014 krw

Nuke yet more obvious #include duplications.

ok miod@


# 1.273 08-Nov-2014 mlarkin

If resuming from sleep (zzz/ZZZ) and the lid is still closed, go back to
sleep. This prevents accidental lid flex or slight opening in a backpack
from waking the machine up and leaving it resumed (powered on).

ok deraadt@


# 1.272 26-Sep-2014 kettenis

Rework piglet and pig allocation. Currently the piglet gets allocated
deep down in the suspend path, where it is really hard to recover from
allocation failure. So allocate the piglet early on in the suspend path.
Also change the piglet and piglet allocation functions to use km_alloc(9)
instead of doing pmemrange magic. This removes a bunch of code which, in the
case of the piglet allocation, is broken since it results in a NULL pointer
dereference. Also switch the piglet allocation to not wait. If we can't
allocate 16MB of phys contig memory on a halfway modern machine we're almost
certainly under a lot of memory pressure and we're better off not trying to
hibernate anyway.

ok mlarkin@


# 1.271 25-Sep-2014 kettenis

Bring back rev 1.249. Now that mp_setperf() has been fixed, it should be
safe to restore perflevel when resuming. Should fix the issues people
see with the processor running at full speed heating up the machine while
the apm command reports that the CPUs are running at a low speed.

ok dcoppa@, deraadt@


# 1.270 23-Sep-2014 kettenis

Release the acpi lock around calls to wsdisplay_suspend() and
wsdisplay_resume(). The X server may adjust the display brightness just
before/after the VT switch when suspending/resuming. In fact, it may
already be blocked adjusting the display brightness for some reason
unrelated to the suspend request.

The VT switch request will eventually time out, but the delay is annoying
and the failed VT switch will lead to problems after resume.

Needed for a future update of the xf86-video-intel driver.

ok mlarkin@


# 1.269 19-Sep-2014 kettenis

Use config_suspend_all(9).


# 1.268 08-Sep-2014 kettenis

Clear the PME Status bit on PCI devices in response to Device Wake events.
Fixes the interrupt storm on the 2009 Mac mini with WOL enabled on its
nfe(4) interface.

ok mlarkin@


Revision tags: OPENBSD_5_6_BASE
# 1.267 20-Jul-2014 mlarkin

Support hibernating to softraid crypto volumes.

much help and ok from deraadt@


# 1.266 16-Jul-2014 mlarkin

Reenable hibernate RLE support and flush+zero all memory after unpack.


# 1.265 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.264 12-Jul-2014 kettenis

Add a function to drop all clean pages on the page daemon queues and call
it when we hibernate.

ok mlarkin@, miod@, deraadt@


# 1.263 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.262 11-Jul-2014 mlarkin

Flush the buffercache to 16MB on hibernate and restore its previous max
size (kern.bufcachepercent) on resume, for better hibernate performance.

ok beck@


# 1.261 10-Jul-2014 blambert

remove unneeded header

ok mlarkin@


# 1.260 01-Jul-2014 kettenis

Adding an additonal delay after each access to SystemMemory or SystemIO
during autoconf makes no sense.

ok deraadt@


# 1.259 23-Jun-2014 kettenis

SystemMemory is used to access memory mapped registers on some machines, so we
must use the bus_space(9) API to access it instead of memcpy(9). Also make
sure we properly align access to fields that need more than byte alignment.

ok guenther@


# 1.258 21-May-2014 mlarkin

Fix invalid / missing format specifiers in several dnprintf statements
(used only when ACPI_DEBUG is enabled)

From a diff on tech@ by Fabian Raetz, with a minor change by me, tested
on i386 and amd64 with ACPI_DEBUG enabled and disabled.


# 1.257 25-Apr-2014 mlarkin

Defer the free of the hibernate structures until later, when it is safe to
do so.

Freeing the hibernate structures too early leads to inadvertent calls into
splx when we aren't ready for them yet. Found when trapping splx calls in
the resume path.

discussed with deraadt@ at m2k14


# 1.256 13-Apr-2014 mlarkin

Fix a badly worded debug message that implied the entire machine was going
to sleep, instead of just the acpi thread going to sleep.


# 1.255 31-Mar-2014 mpi

Change the order of operations during a suspend/resume cycle and
call bufq_quiesce() after executing the DVACT_QUIESCE handlers.

This should be safe since no disk nor controller drivers have such
handler but it will allow us to detach sd(4) devices attached to a
USB bus.

Another benefit pointed out by kettenis@ is that drivers that need
to read a firmware from the disk should be able to do it at resume
time in a DVACT_WAKEUP handler.

ok kettenis@, deraadt@


# 1.254 13-Mar-2014 dlg

get rid of the assumption that the head of the alldevs list is the
"mainbus" device. this breaks when mpath is enabled because it
attaches before mainbus and therefore takes the head position.

have autoconf provide device_mainbus() which looks up mainbus_cd,
and use that instead.

discussed with deraadt who just wants mpath stuff to move forward
despite there being many ways to shine this particular turd.


Revision tags: OPENBSD_5_5_BASE
# 1.253 21-Feb-2014 deraadt

Do the lid control further on in the resume sequence, since this is
AML, and will do WAITOK operations. While cold, that is a bad idea.
We seem to be safe with respect to the spec, I do not see any event
debounce issues here.
ok kettenis, tested by many


# 1.252 20-Jan-2014 claudio

Instead of checking sc->sc_revision <= 1 check that the
fadt->hdr_revision <= 1. This is a better check since it checks
the version of the proper descriptor. This allows the alix APU
to reboot since those boards have currently a wrong sc->sc_revision.
Committing this now so that it can be tested easier.
With and OK jsg@ also fine by derradt@


# 1.251 24-Dec-2013 kettenis

If the FADT has its SMI_CMD set to zero, assume we're only ACPI-only hardware
and don't need to disable SMI ownership of the ACPI hardware registers.

ok mlarkin@


# 1.250 23-Dec-2013 kettenis

Revert previous commit. It makes resume hang on some MP systems running
GENERIC.MP.


# 1.249 16-Dec-2013 kettenis

Restore hw.setperf upon resume like we do for apm(4) on i386. Seems we need
to do this fairly late in the resume to avoid hangs.

ok deraadt@, mlarkin@


# 1.248 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


# 1.247 06-Nov-2013 mpi

Add support for Power Resources for Dx states and the necessary hook
for PCI devices. This hook should be called twice, before and after
changing the power state of a PCI device.

Before setting the device to the new state, the ACPI layer will notify
every power resources linked to the device for that state and make sure
they are turned "_ON". After changing the state of the device, it will
decrement the reference of every power resources linked to that device
for the old state and turn them "_OFF" if they are no longer referenced.

This fixes the no-USB after resume problem seen on various ThinkPad,
problem initialy diagnosed with Alexander Polakov.

ok kettenis@, deraadt@


Revision tags: OPENBSD_5_4_BASE
# 1.246 01-Jun-2013 mlarkin

Stop printing ACPI wakeup devices in dmesg after the 16th wakeup device.

Helps with VMware VMs that report hundreds of wakeup devices.

ok reyk@, deraadt@


# 1.245 31-May-2013 bluhm

Fix the build for a kernel without wd and pciide in its config.
OK deraadt@ tedu@


# 1.244 30-May-2013 deraadt

Enforce ca_activate tree-walks over the entire heirarchy for all events,
cleaning up some shutdown-hook related code on the way.
(A few drivers related to sparc are still skipped at kettenis' request)
ok kettenis mlarkin, tested by many others too


# 1.243 18-Apr-2013 deraadt

pin the acpi thread to the BSP, because AML and SMI cannot be trusted. I
was certain we were doing this already, but it turns out no...


Revision tags: OPENBSD_5_3_BASE
# 1.242 24-Dec-2012 guenther

In acpiioctl(), Return though the splx() path if suser() returns an error

ok deraadt@


# 1.241 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


# 1.240 04-Oct-2012 ehrhardt

Use information provided by ACPI to attach secondary PCI host bridges.

ok kettenis@


# 1.239 07-Sep-2012 kettenis

Add a new function called acpi_pci_min_powerstate() that returns the lowest
power state for a PCI device for the power state (currently S3 or S4) we're
transitioning into. This function is probably too simplistic; there is a lot
of (silly) complexity in the ACPI spec.

ok deraadt@


Revision tags: OPENBSD_5_2_BASE
# 1.238 13-Jul-2012 pirofti

Disable acpivideo(4) on ASUS laptops.

There are some models that panic whenever an event is received.
Some cope, some don't. In order to get this right acpiec(4) requires
major rework. Until then let machines boot.

Discussed with deraadt@.


# 1.237 11-Jul-2012 mlarkin

for now, restrict hibernate to root in the acpi ioctl kernel interface.
normal rules for controlling this interface through apmd still apply.

ok deraadt@


# 1.236 09-Jul-2012 deraadt

tiny little oops in untested hibernate block


# 1.235 09-Jul-2012 deraadt

clean up ioctl() case statements


# 1.234 09-Jul-2012 deraadt

acpi_disable_onegpe() is not used; ok pirofti


# 1.233 24-May-2012 kettenis

If _MIN > _MAX consider a WordBus resource to be invalid. Fixes a panic on
the IBM x3550-M4.

ok mikeb@


# 1.232 29-Mar-2012 mlarkin

Missing #ifdef in previous diff


# 1.231 29-Mar-2012 mlarkin

Prevent hibernation attempts on machines with unsupported disk controllers.
The same check is also performed later in the hibernate sequence, but by
then it's too late to cleanly unwind (presently).

ok deraadt


# 1.230 26-Mar-2012 deraadt

simplify conditionals in acpi_sleep_state()
ok mlarkin


# 1.229 26-Mar-2012 deraadt

hook in the hibernate request code; half of this diff is from mlarkin
ok mlarkin


Revision tags: OPENBSD_5_1_BASE
# 1.228 20-Sep-2011 deraadt

Very early in suspend while the clock is still ticking realtime,
save the clock back to the rtc. The zaurus already did this.
ok phessler miod kettenis


Revision tags: OPENBSD_5_0_BASE
# 1.227 02-Jul-2011 nicm

kqueue attach functions should return an errno or 0, not a plain 1. Fix
the obvious cases to return EINVAL and ENXIO.

ok tedu deraadt


# 1.226 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.225 16-Jun-2011 pirofti

Import acpitoshiba driver.

This is originally written by Hiroyuki Aizu, ported and integrated
in our tree by Javier Vazquez <javama4 AT gmail> with tweaks and
suggestions by me.

Import prodded by deraadt@ and the lack of testers on tech@.


# 1.224 27-Apr-2011 jcs

attach acpithinkpad to newer lenovo models like the x120e

ok deraadt@


# 1.223 22-Apr-2011 jordan

Fix uninitialzied variables and formatting strings (-Wxxx errors)


Revision tags: OPENBSD_4_9_BASE
# 1.222 02-Jan-2011 jordan

Re-add code for ACPI taskqueue, eliminates need for state vars in acpi softc
Serialize processing ACPI tasks for sleep/powerdown/GPE event
ok deraadt@


# 1.221 31-Oct-2010 guenther

Revert last commit: it breaks resume on Thinkpads
"then please back it out" deraadt@


# 1.220 26-Oct-2010 jordan

Add task queue for ACPI gpe and notify handlers
This fixes eject on Dell Latitude dock and an issue on the Dell Mini
battery update.
ok deraadt


# 1.219 07-Oct-2010 jordan

Create separate functions for enabling wake and runtime gpes.
Remove old suspend/resume gpewalk function
ok deraadt, mlarkin


# 1.218 05-Oct-2010 jordan

split acpi_enable_onegpe to separate enable/disable functions
ok deraadt


# 1.217 05-Oct-2010 jordan

Added acpi_disable_allgpes function to clear GPE status
ok deraadt


# 1.216 05-Oct-2010 deraadt

clean up acpi_softc use more


# 1.215 05-Oct-2010 deraadt

delete a bit of #if 0 code


# 1.214 05-Oct-2010 deraadt

hide use of our global acpi_softc pointer a bit better


# 1.213 29-Sep-2010 deraadt

grab the acpi thread lock very early in acpi thread startup; not that it
is likely that anyone will race us... but why be careless.
ok kettenis


# 1.212 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.211 29-Aug-2010 jasper

- add a check for FWRITE here too.

ok miod@


# 1.210 27-Aug-2010 kettenis

Make PCI Power Management optional, and only enable it when acpi(4) attaches.

ok deraadt@


Revision tags: OPENBSD_4_8_BASE
# 1.209 08-Aug-2010 kettenis

Close race between the acpi thread and wscons ioctls running in process
context. We might rip this out post-release and replace it with a solution
that serializes things in a more obvious way.

ok deraadt@, miod@ and "those crazy texans who ok anything"


# 1.208 08-Aug-2010 deraadt

Report the acpi sleep states when on a RAMDISK; ok krw


# 1.207 07-Aug-2010 kettenis

Enforce acpi register access rescrictions.

ok mlarkin@, deraadt@, tested by marco@


# 1.206 07-Aug-2010 deraadt

upon resume, notify all the acpiac and acpibat drivers to update
their status. some laptops do not do this automatically.
ok kettenis canacar


# 1.205 06-Aug-2010 marco

Always call _PSW on _LID devices that support it to enable lid open to
resume the machine. Conversely disable it when the machine wakes up.

Tested by several ok deraadt


# 1.204 06-Aug-2010 deraadt

As part of a revamp of the PM1/GPE code, I write this diff to look for
unmanaged EN & STS bits in the PM1 register at interrupt time and report
them. As a side effect this splits the STS acknowledgement into two writes
(for power, and sleep) instead of one. The printf that is added (to spot
unmanaged STS bits) has not yet been triggered as far as we know.

Before the "write to PM1 registers at the right offset" diff went in, this
was not neccessary. But newer thinkpads do not have a working soft power
button without this diff.

We have no idea why.
ok mlarkin kettenis


# 1.203 05-Aug-2010 deraadt

PM1 power button events were generating 'fake events' to acpibtn PWRB,
but if there is no such device these were being tossed. Instead, arrange
for all power button handlers to flag a softc variable which is processed
by the thread; so that one place will know to request a powerdown (psignal
to init)
ok marco mlarkin


# 1.202 05-Aug-2010 deraadt

rename a variable which is annoyingly poorly named


# 1.201 05-Aug-2010 mlarkin

Write to the correct offset for various ACPI pmreg registers. Writing to
the correct register does properly clear the wake status bits and fixed
event bits, which allows some machines to have more than 1 suspend cycle.

bug spotted by deraadt@

tested by deraadt@ and myself on a half-dozen machines and others on
hackers@

ok deraadt@, marco@


# 1.200 05-Aug-2010 deraadt

Fix the power button. The pm1 register is 16 bits in size.
My mistake, a while back.
tested by krw


# 1.199 04-Aug-2010 kettenis

Print supported sleep states.

ok marco@, phessler@, deraadt@


# 1.198 03-Aug-2010 mlarkin

Use the proper flag for re-enabling certain hardware events (power btn,
sleep btn). The (incorrect) flag prevoiously used coincidentally had the
same value, so this shouldn't cause any different behavior than before.

tested on a variety of machines (i386, amd64, sp, mp) - no behavioral
change seen

ok deraadt@


# 1.197 28-Jul-2010 marco

Make all gasio failures look and behave the same.


# 1.196 28-Jul-2010 mlarkin

Delay a little longer on suspend before giving up. Fixes machines that
sometimes don't suspend because the sleep transition takes too long
(symptoms like failing to sleep but the backlight stays on).

ok deraadt@


# 1.195 27-Jul-2010 deraadt

remove XXX; EC can now be attached early when needed; ok jordan


# 1.194 27-Jul-2010 deraadt

more brackets feels good


# 1.193 27-Jul-2010 deraadt

XXX annotate a block in acpi_enter_sleep_state() that is pretty worrying


# 1.192 27-Jul-2010 deraadt

horrific indentation that nearly made me puke


# 1.191 27-Jul-2010 mlarkin

Set the sleeping indicator light on machines that support it, in case SMI
doesn't do it for us. Shows the 'moon' LED on older Thinkpads when
sleeping.

Tested on T43p, W500, T510 by me and on X61s by deraadt@.

ok deraadt@


# 1.190 27-Jul-2010 jordan

Early initialization of acpiec if ECDT table exists
Fixes hang when booting thinkpads while docked
ok deraadt


# 1.189 26-Jul-2010 deraadt

remove merge error which was commited


# 1.188 26-Jul-2010 pirofti

Add support for Sony ACPI hotkeys via a new driver: acpisony(4).

Currently it only works for the suspend button and tries to do right for
the brightness events, but I haven't found a Sony laptop that like to do
right so far.

In the future I want to make the brightness keys work on all Sony's and
also add support for the zoom hotkeys and whatever other funky keys I can
find on those things.

Okay deraadt@.


# 1.187 22-Jul-2010 deraadt

pretty large cleanup of SMALL_KERNEL


# 1.186 21-Jul-2010 deraadt

in the acpi GPE handler, late ack level interrupts not edge interrupts.
Duh.


# 1.185 21-Jul-2010 deraadt

Merge the edge and level gpe handlers into one (now possible since the
gpe structure tells us whether the gpe is edge or level)


# 1.184 21-Jul-2010 deraadt

delete #if 0 code we are not going to use; ok marco


# 1.183 20-Jul-2010 deraadt

make the mask code for acpi_enable_onegpe() match other similar code


# 1.182 20-Jul-2010 deraadt

Handle the acpi interrupt controller with proper edge/level handling,
wrapped in spltty to avoid racing against the interrupt controller.
Repair the gpe bit masking code while there, and do operations in the
right order
ok kettenis mlarkin, help from jordan at figuring out the order of
operations.


# 1.181 20-Jul-2010 matthew

When we're preparing to go to sleep, only call bufq_restart() if other
preparations failed.

ok kettenis@, thib@, deraadt@


# 1.180 19-Jul-2010 deraadt

permit compilation with ACPI_DEBUG


# 1.179 19-Jul-2010 deraadt

there is no need for a seperate dsdt_softc pointer; dsdt does not have a
softc but is just a part of the single acpi driver.
ok jordan


# 1.178 19-Jul-2010 deraadt

acpi_delay() is no longer needed; ok jordan


# 1.177 18-Jul-2010 mlarkin

Clear event status on resume, to avoid going back to sleep/reboot/shutdown
immediately after resume. Tested by a bunch of people on hackers@ on
various hardware. Fixes at least Toshiba NB200 and Gateway LT3103u.

ok marco


# 1.176 13-Jul-2010 deraadt

rename some thread related interfaces so they are logical


# 1.175 10-Jul-2010 jordan

Verify _ADR bdf is correct (some systems return 0xffff)
Works on Dell x9xx and Studio 1555


# 1.174 09-Jul-2010 jordan

Clear the noise


# 1.173 08-Jul-2010 jordan

Add mapping for ACPI device to PCI bdf (match autoconf tree)
Simplify resource parsing function to use buffer argument
Convert namespace linked lists to use queue macros
ok marco@, deraadt@


# 1.172 06-Jul-2010 deraadt

Use spltty() to lock downcalls from apm against the information being
modified by the acpi thread.
ok kettenis


# 1.171 06-Jul-2010 deraadt

Don't use a workq to do the suspend, because it races aginst the acpi
thread. Instead, just tell the acpi thread to do the suspend for us.
This makes apmd & zzz work correctly. While here, have acpithinkpad
attempt to post the event to apm before prompting the suspend itself.
ok kettenis marco mlarkin


# 1.170 05-Jul-2010 mlarkin

Reset register SCI_EN on resume. The ACPI spec clearly states we don't have
to do this, but many machines don't, or forget to, which leads to all sorts
of problems on resume (broken battery stats, overheating, failure to resume
more than once, etc).

tested by many, ok marco@, deraadt@


# 1.169 01-Jul-2010 thib

do bufq quiesce again, since vnd/the bufq_done call in
biodone has been fixed.


# 1.168 01-Jul-2010 jordan

Backout recent AML changes, breaks on R210 and others


# 1.167 01-Jul-2010 jordan

Simplified aml_parse_resource call, cleanup code
ok marco


# 1.166 01-Jul-2010 jordan

Fix subordinate bus # for multi-root PCI buses
ok marco, kettenis


# 1.165 30-Jun-2010 thib

Disable/partially backout the bufq quiesce changes as this
is causing havoc with vnds and release must be buildable.


# 1.164 29-Jun-2010 deraadt

Fix ramdisks (new code was placed inside a SMALL_KERNEL chunk)


# 1.163 29-Jun-2010 jordan

Add support for mapping ACPI to PCI devices
ok kettenis, deraadt


# 1.162 29-Jun-2010 kettenis

Call bufq_quiesce() on suspend to stop all I/O and bufq_restart on resume.
Makes suspend/resume work much better while doing disk I/O.

ok mlarkin@


# 1.161 29-Jun-2010 tedu

60 is a more reasonable guess for number of minutes in an hour than 100.
spotted by TeXitoi. ok deraadt


# 1.160 27-Jun-2010 jordan

Add _acpi_matchhids routine, precursor to pci device match


# 1.159 27-Jun-2010 mlarkin

Clear ACPI fixed event status on resume (power buttons/etc) so that some
machines don't immediately resume after suspending to S3.

Tested by kettenis@ and I on 5 or 6 machines, deraadt@ "yeah, looks good"


# 1.158 27-Jun-2010 jordan

Cleaned up acpi_maptable, adds to table list directly


# 1.157 07-Apr-2010 deraadt

If we have done a wsdisplay_suspend(), and ACPI decides it does not want to
suspend, we must roll back by calling wsdisplay_resume() or the virtual
console switching will be locked forever
ok oga


# 1.156 07-Apr-2010 kettenis

Make suspend/resume work on MP machines (running an MP kernel). Joint work
from mlarkin@ and me, with some amd64 fixes thrown in by deraadt@

ok marco@, deraadt@, pirofti@, mlarkin@


# 1.155 31-Mar-2010 kettenis

Bring /dev/apm support in line with apm(4). Make the suspend button send
a suspend request event to apmd(8) instead of suspending immediately. Also
keep track of whether /dev/apm and /dev/apmctl are currently open, such that
we can still suspend immediately if apmd(8) isn't running.

ok deraadt@, marco@, pirofti@, jsing@, oga@


# 1.154 30-Mar-2010 oga

Prevent the apmd/x races for good.

When we hit suspend time, go through all wsdisplays on the system. if
they are in mode MAPPED, but not MODE_DUMBFB then if possible do a full
vt switch to a !mapped vt, and prevent switching back until resume time.
This has to be called from MD code because this involves userland
running so that X can run the vt switch signal handler. This way, any
case where we are using the "poke registers from userland" model, we
will not be on the hardware when we go down, so the kernel can actually
handle thing properly.

Tested on several acpi laptops (by kettenis@ and ian@), x40 (me and
beck@ at LEAST) and zaurus (me). Maybe others, but if so I forgot who at
this time..

Idea from deraadt somewhere over the Faroe Islands (I thought of a
similar thing myself a while ago). Much prompting from him. Ok and
comments miod@


# 1.153 25-Mar-2010 oga

Fix & vs. && in the kqfilter code.

Based on a comparison with the apm code.

ok deraadt@, kettenis@


Revision tags: OPENBSD_4_7_BASE
# 1.152 26-Nov-2009 mlarkin

Visual (and on some machines, audio) feedback while the machine is
resuming via calls to aml _SST method. This also makes the "moon" LED on
thinkpads to blink during resume and cease when resume is completed.

ok pirofti@


# 1.151 26-Nov-2009 deraadt

Toggle into "cold" when we we disable interrupts, because tsleeping
in the AML interpreter and context switching into userland processes
tends to sometimes pop the ACPI trampoline page out of our address
space. Makes lots more machines work.
ok kettenis mlarkin


# 1.150 24-Nov-2009 jsg

in the resume path evaluate _WAK before dealing with device drivers
like the spec says, not after.

ok mlarkin@


# 1.149 24-Nov-2009 deraadt

abort suspends on SMP systems until that gets written; ok mlarkin


# 1.148 23-Nov-2009 mlarkin

pci should return the result of it's children's suspend/resume calls back
to its parent.

handle suspend failure case and unwind if devices have an activate function
that returns failure.

ok deraadt@, kettenis@


# 1.147 23-Nov-2009 mlarkin

acpi* devices need to call their children on suspend/resume.
ok deraadt@


# 1.146 23-Nov-2009 pirofti

Remove ACPI_SLEEP_ENABLED checks.

This enables by default the suspend/resume paths in the kernel.

Okay deraadt@.


# 1.145 23-Nov-2009 deraadt

knf and spacing that are super annoying


# 1.144 23-Nov-2009 mlarkin

Hook up Theo's new children suspend/resume function, as well as more ACPI
suspend/resume glue.

ok deraadt@


# 1.143 26-Oct-2009 deraadt

Do not do strncmp comparisons on dv_xname because longer device names which
look similar could arrive in the future. Instead, compare directly against
dv_cfdata->cf_driver->cd_name
Issue originally spotted by miod


# 1.142 26-Oct-2009 jordan

Verify checksum+address when loading ACPI tables.
Some systems had invalid entries in RSDT/XSDT.
ok marco@


# 1.141 23-Jul-2009 cnst

New aibs(4) driver for ASUSTeK AI Booster (ACPI ATK0110) hardware monitoring,
with sensor state support through limits provided by the ACPI.

Tested on several ASUS motherboards kindly networked by Sam Fourman Jr.

ok deraadt marco jordan


Revision tags: OPENBSD_4_6_BASE
# 1.140 03-Jun-2009 pirofti

Import acpi power resource driver. This is needed for suspend/resume logic.

Okay jordan@ and marco@.


# 1.139 03-Jun-2009 pirofti

Add acpivideo support. This adds brightness support for all laptops
except thinkpads, they will use the acpithinkpad driver. The driver is
also hooked into wsconsole. So brightness can be adjusted via:

$ wsconsctl display.brightness=<percentage>

This is very helpfull on some laptops that have a nasty bios and get two
steps instead of one when pressing the brightness button.

Tested on various dell, fujitsu, acer, samsung and other laptops.

Okay marco@, miod@. Suggestions from kettenis@.
Lots of reviews and help from miod@, thanks!


# 1.138 03-Jun-2009 jordan

Added ejectable bay for IDE cdroms
ok marco@


# 1.137 30-Apr-2009 marco

Oga changed apm ioctls and forgot to grep. Temporary work around by hitting
the sleep button when we get any request. This interface will chnage at
some point.
ok oga


# 1.136 19-Apr-2009 krw

Call acpi_sleep_walk() when powering down just like when entering
any other sleep state. This tells acpi wake devices to not wake up
the box until manual powering up resets their status. Makes my Dell
GX520 stay powered off in response to 'halt -p'.

In snaps for a while.

ok marco@ (several times) deraadt@


# 1.135 17-Apr-2009 pirofti

Add device ranking support for future sleep/wake developments.

okay marco@


# 1.134 11-Mar-2009 jordan

Cleanup.. use aml_evalinteger instead of aml_evalname
ok marco@


# 1.133 10-Mar-2009 jordan

Fix cut/paste error.. call _BFS not _PTS
ok marco@


Revision tags: OPENBSD_4_5_BASE
# 1.132 21-Feb-2009 marco

overzealous cut/paste; found by Vladimir Kirillov <proger@uaoug.org.ua>


# 1.131 20-Feb-2009 marco

Oops didn't mean for Debugger() to remain there.


# 1.130 19-Feb-2009 marco

suspend/resume bits so that we can develop this in tree. This is disabled.
code from mlarkin and me
help from art,toby,jordan and several others
ok jordan, go for it deraadt


# 1.129 10-Feb-2009 jordan

Fix null pointer for shutdown if acpi not enabled
ok marco@


# 1.128 04-Feb-2009 kettenis

Some systems that claim to be ACPI 2.0 apparently don't provide an XSDT
address. Make the code fall back on the RSDT on those systems.

From Joerg Niendorf <p3y80@internode.on.net>. Fixes PR 5986.


# 1.127 17-Jan-2009 krw

A couple of stray malloc/memset -> malloc(M_ZERO), a bit of knf and
lose a couple of unnecessary casts. No functional change.

Feedback from kettenis@

ok kettenis@ marco@


# 1.126 06-Nov-2008 marco

Collapse all sanity checks of hids into one function.

Originaly from jordan


Revision tags: OPENBSD_4_4_BASE
# 1.125 02-Jul-2008 fgsch

add acpivideo. for now almost an empty skel so others can work on
suspend/resume. eventually it will also manage output switching and
brightness where supported. prodded by marco@


# 1.124 11-Jun-2008 marco

Fix $OpenBSD$ for once and for all


# 1.123 08-Jun-2008 deraadt

aaa_dev was being set from an uninitialized variable; ok toby


# 1.122 07-Jun-2008 marco

Kill ACPIVERBOSE. It was never used and is worthless to boot.

comments jmc
ok toby


# 1.121 06-Jun-2008 marco

Shrink acpi some more for floppies and while here re-enable acpi on the
floppies. Fixes crazy sony vaio kettenis has.

tested by deraadt
ok deraadt jordan kettenis


# 1.120 01-Jun-2008 marco

Fix \n nit in SMALL_KERNEL.


# 1.119 01-Jun-2008 marco

Re-enable new parser. Lots of testing by various folks. Fits all media
now too.

ok kettenis deraadt


# 1.118 16-May-2008 dlg

back out jordans new parser.

it didnt have enough oks from the right people, it did break machines
that were previously working, and it bloated the RAMDISKS and install
media beyond the constraints of the media.

requested by deraadt@


# 1.117 14-May-2008 jordan

Adding new ACPI Parser code
Old guts of code still exists, needs to be torn out next
ok marco@
ok brad@


# 1.116 27-Apr-2008 jcs

add acpithinkpad(4), an acpi support driver for ibm/lenovo
thinkpads.

provides functionality that the bios did on older, non-acpi
thinkpads like respond to screen brightness keys and toggle the
bluetooth device on and off.

ok deraadt@ and marco@


# 1.115 24-Apr-2008 jsing

Introduce acpiasus(4), a driver for the ACPI based hotkeys found in many
ASUS laptops (including the ASUS EeePC) - largely based on NetBSD's
asus(4) driver. On the ASUS EeePC this allows us to enable/disable
wireless, change screen brightness and use the volume keys.

ok jsg@, weingart@


# 1.114 12-Apr-2008 kettenis

Get rid of acpi_s5 global variables; simply send SIGUSR2 instead of SUGUSR1
to tell init(8) to power down the machine.

ok krw@


Revision tags: OPENBSD_4_3_BASE
# 1.113 21-Jan-2008 jsg

Check against RESET_REG_SUP in FADT before doing the acpi reset dance
after all, not doing so appears to break more machines than it fixes.

ok marco@


# 1.112 05-Jan-2008 kettenis

Never attach more acpicpu(4) devices than cpu(4) devices. Always attach
the first acpicpu(4) device on non-MULTIPROCESSOR kernels, regardless of its
ACPI processor ID.

ok mikeb@


# 1.111 05-Dec-2007 deraadt

Hang acpi(4) below bios(4) instead of mainbus(4). This lets us move acpi(4)
before pcibios(4), and then.... if acpi(4) attaches, skip pcibios(4) since
it messes the machines up. Fixes claudio's HP dl320 G5, and almost assuredly
others.
ok kettenis


# 1.110 05-Dec-2007 deraadt

repair acpiopen/acpiclose and friends to handle the /dev/apmctl interface,
so that apmd(8) can actually work correctly against acpi(4), and thus,
even do the fancy -C/-A stuff; also tested by jmc


# 1.109 02-Dec-2007 fgsch

fix the trailing space for real now.


# 1.108 02-Dec-2007 fgsch

avoid trailing spaces on output; marco@ ok.


# 1.107 27-Nov-2007 kettenis

Fix off-by-one that made us attach acpicpu0 and acpicpu1 on GENERIC kernels.

ok marco@


# 1.106 25-Nov-2007 jsg

When available and ACPI is enabled use the ACPI reset method
to reboot. This is required for some quirky sis machines
and likely others to reboot.

Tested by many. 'commit early commit often' deraadt@


# 1.105 19-Nov-2007 deraadt

sigh, amd64 still uses that stupid acpi_enabled variable...


# 1.104 19-Nov-2007 deraadt

softc's come initialized to 0, you know...


# 1.103 19-Nov-2007 deraadt

store revision; from jsg


# 1.102 19-Nov-2007 kettenis

Initialize sc_facs earlier.

ok deraadt@, beck@, canacar@


# 1.101 16-Nov-2007 deraadt

fix the bus_space #define nightmare, so that amd64 and i386 are much more
uniform. as a result shared code like acpi needs less #ifdef's
ok marco kettenis


# 1.100 15-Nov-2007 deraadt

remove the #ifdef ACPI_ENABLE. the EC codepath is either safe enough (and
will be improved), or it is unsafe because AML assumes it must be there
ok almost everyone


# 1.99 15-Nov-2007 mikeb

Make acpicpu(4) attach to the existing CPUs only.

Problem reported by form@. Fix idea by kettenis@.
Lots of help from deraadt@. Tested by deraadt@, form@ and me.

ok deraadt marco


# 1.98 14-Nov-2007 deraadt

spacing


# 1.97 12-Nov-2007 deraadt

correct : vs , in attach-time messages


# 1.96 06-Nov-2007 deraadt

all ACPI_ENABLE kernels need acpi_interrupt()


# 1.95 06-Nov-2007 deraadt

move acpi_read_pmreg() and acpi_write_pmreg() out of SMALL_KERNEL so
that acpi ramdisks can compile (obvious solution)


# 1.94 05-Nov-2007 canacar

Establish the acpi interrupt earlier. Fixes problems with some
GENERIC.MP kernels not getting acpi interrupts. This is a
workaround until the real issue with late establishment is fixed.
ok weingart@, beck@, gwk@


# 1.93 03-Nov-2007 jordan

Added support for displaying wakeup devices
ok beck@,weingart@,gwk@


# 1.92 11-Oct-2007 kettenis

When walking the tree yo find _PRT methods, check whether the device is
actually enabled and functioning. Similar to what we do for _INI.

ok weingart@, beck@


# 1.91 08-Oct-2007 krw

More simple memset(,0,) -> M_ZERO changes. In this batch move to
size(*p) as the first malloc() parameter where p is declared locally
and thus easy to check. Add M_ZERO to gpe_table allocation in acpi.c
even though there is no obvious bzero or memset nearby.


# 1.90 13-Sep-2007 mk

Remove unnecessary memset() (see aml_evalnode(9)) and a bit of KNF.

ok toby


# 1.89 13-Sep-2007 weingart

Implement ACPI 6.5.1 spec tree walk for _STA and _INI.
Fixes mk's laptop. No regressions so far (thank you to the
testers).

ok gwk@, mk@, marco@


Revision tags: OPENBSD_4_2_BASE
# 1.88 17-Apr-2007 mk

Now that acpidock no longer hangs machines during boot (at least
according to the reports I've been getting) it should be safe to enable
it again, so remove the #if 0 stuff around it.

ok marco


# 1.87 11-Apr-2007 jordan

Added changes for C-State
ok marco@


# 1.86 26-Mar-2007 marco

Calculate minutes left more accurately.

From giovanni <qgiovanni@gmail.com>


Revision tags: OPENBSD_4_1_BASE
# 1.85 03-Mar-2007 mk

Stop looking for acpidock until we fix that stuff that breaks all pre-60
series ThinkPads with acpidock(4) enabled.


# 1.84 22-Feb-2007 marco

We were getting no interrupts in .MP. There are 2 fixes; one in
acpi_machdep.c and move the interrupt establishment down after ioapic
has been initialized.

ok jordan


# 1.83 21-Feb-2007 kettenis

Make an acpi-enabled GENERIC.MP work on systems that only support PIC mode.

ok marco@


# 1.82 19-Feb-2007 jordan

Added acpi_map_address function for mapping GAS/IO registers
ok marco@


# 1.81 18-Feb-2007 jordan

Added dynamic perfect hash for opcodes
ok marco@


# 1.80 18-Feb-2007 marco

Shave off another K pre compression.
Shuffle SMALL_KERNEL functions around so that we only have a single #ifndef


# 1.79 17-Feb-2007 marco

Add SMALL_KERNEL throught acpi to make it fit on boot media.
ok deraadt


# 1.78 13-Feb-2007 marco

Add smarter _INI code. This helps with some dissapearing devices.

ok kettenis jordan


# 1.77 31-Jan-2007 gwk

Introduce acpi_hasprocfvs thats "ACPI Has Processor Frequency and Voltage
Scalling" a global flag set by acicpu attach to indicate that ACPI is
handling fvs through the _PSS objects. This will fix disappearing
EST/powernow when booting kernels with acpi enabled, as acpicpu isnt
compiled into GENERIC yet. Also in cases where acpicpu is built in and
legacy EST/powernow work but acpi is missing the _PSS object, the legacy
routines will be used.

tested by dim@, and Nick Nauwelaerts <nick AT nauwelaerts.net>
ok marco@, dim@


# 1.76 22-Jan-2007 mk

Actually look for docking stations by looking for _DCK nodes.

ok marco


# 1.75 02-Jan-2007 marco

On some systems EC must be attached as the first device. Move EC from
generic _HID devices to its own routine to force first attach. Bug found
and debugged by joris@

Code mostly written by joris@


# 1.74 26-Dec-2006 marco

Add polling to devices that require it such as acpibat and acpitz.
Use the same tsleep/wakeup mechanism as the interrupt code to keep it non-
concurrent.


# 1.73 21-Dec-2006 deraadt

even more knf love


# 1.72 21-Dec-2006 marco

Remove some leftover acpiec gpe/interrupt code.


# 1.71 20-Dec-2006 deraadt

oops, name already used


# 1.70 20-Dec-2006 deraadt

rename __acpi_enable_gpe(); ok marco


# 1.69 20-Dec-2006 deraadt

acpi_unmap_pmregs() is not used, ok marco


# 1.68 19-Dec-2006 deraadt

simplify some low-level methods substantially (shrinking code); ok marco jordan


# 1.67 06-Dec-2006 mk

malloc(..., M_NOWAIT) with no NULL check is bad. If allocation fails,
complain, clean up, and bail out of attach.

input and ok gwk


# 1.66 28-Nov-2006 jordan

Fixed clearing arg for acpi_init_pic
ok kettenis@


# 1.65 27-Nov-2006 kettenis

Don't spam dmesg.


# 1.64 27-Nov-2006 jordan

Fixes for acpiprt routing
Post-parse fixup for namerefs
ok kettenis@


# 1.63 25-Nov-2006 marco

Don't handle acpiev events inside the gpe handler since this causes deadlocks.

ok jordan@


# 1.62 15-Nov-2006 kettenis

Hook up acpi apic interrupt routing support to generic acpi code.

ok jordan@


# 1.61 02-Nov-2006 marco

Kill acpiec attach function and add its functionality to the generic _HID
device attach

prodded and ok jordan


# 1.60 02-Nov-2006 marco

Fix comments to reflect reality.


# 1.59 19-Oct-2006 marco

Replace ACPI ioctls with APM ioctls. This makes ACPI talk the same protocol
as APM therefore userspace can now retrieve power information in the same
fashion as all other apm arches (zaurus, macppc, i386 etc).

To use force the use of acpi device like so: apmd -f /dev/acpi

ok jordan@


# 1.58 19-Oct-2006 jordan

Fixed GPE debug levels
Fixed ref counter on temporary bufferfields
DSDT parsing fully working on IBM Thinkpad
ok marco@


# 1.57 15-Oct-2006 jordan

Removed old GPE handler code remnants
ok marco@


# 1.56 12-Oct-2006 jordan

Massive ACPI changes
Fixed ACPI leaks
new ACPI API
ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.55 30-Jun-2006 gwk

KNF, No functional change,
"go at it" marco@


# 1.54 30-Jun-2006 jordan

Support for calling Embedded Controller via new GPE interface
ok marco@


# 1.53 30-Jun-2006 jordan

Added new code for GPE handling
Also works with ACPIEC; need to fixup acpiec.c to remove original interrupt handler
ok marco@


# 1.52 30-Jun-2006 jordan

Added offset argument for acpi read/write pmreg routines (new GPE code)
ok marco@


# 1.51 31-May-2006 todd

do not call acpi_powerdown() unless acpi has successfully initialized
ok gwk@ canacar@


# 1.50 29-May-2006 canacar

Add embedded controller support.
ok marco@ deraadt@ testing & ok gwk@


# 1.49 19-May-2006 canacar

Minimal thermal zone driver. Monitors thermal zone temperature,
shuts down the system if the 'critical temperature' is reached.
ok marco@


# 1.48 11-Apr-2006 marco

KNF.


# 1.47 11-Apr-2006 gwk

Add acpi_resume(struct acpi_softc *sc), which calls some AML methods,
detected at attach time, currently unused.

ok marco@


# 1.46 11-Apr-2006 gwk

Discover some ACPI sleep related methods, and call them at the
appropriate time in acpi_enter_sleep_state().

ok marco@


Revision tags: OPENBSD_3_9_BASE
# 1.45 26-Feb-2006 marco

Add acpicpu as a device.

ok jordan@


# 1.44 23-Feb-2006 marco

remove dead function.

ok jordan@


# 1.43 22-Feb-2006 marco

Print out all tables it found during autoconf. We'll need this data in
the future.


# 1.42 22-Feb-2006 marco

Make dmesg less verbose.

ok jordan.


# 1.41 22-Feb-2006 jordan

Added parsing of SSDT tables
ok marco@


# 1.40 22-Feb-2006 jordan

Added new aml_register to notify by device id
Moved powerdown code to button device
ok marco@


# 1.39 21-Feb-2006 marco

Readjust debug levels that crept in too low over the last few days.
KNF


# 1.38 21-Feb-2006 marco

Rig the power button so that the machine shuts down correctly.


# 1.37 20-Feb-2006 marco

Add acpibtn device. Remains disabled for now.


# 1.36 19-Feb-2006 jordan

Added code for aml_notify GPE notification callback
ok marco@


# 1.35 19-Feb-2006 grange

Fix acpi_enter_sleep_state() by replacing current ugly hack
with mostly correct sleeping state transition code.

ok brad@ marco@


# 1.34 19-Feb-2006 marco

jordan:
Enable interrupts.
Added peripheral code to make this work.

marco:
Replace softintr and other types of interrupts with a handler with context.
Fixed interrupt magic for GPEs.
Some KNF

This is work done by jordan and I but has to be a single commit.


# 1.33 17-Feb-2006 marco

Remove earlier introduced delays. We really only need them during autconf.

ok jordan@


# 1.32 17-Feb-2006 marco

Add a generic delay function as per the ACPI spec.

from jordan@


# 1.31 17-Feb-2006 marco

Use tsleep instead of delay when !cold.


# 1.30 17-Feb-2006 marco

Argh, we need delays between reads/writes. The backend reads (i2c most likely)
are so slow that values come back as 0 sometimes. We have to do this
despite the spec saying we shouldnt have to. Guess having a faster
parser than others can hurt sometimes.

Idea from jordan.


# 1.29 17-Feb-2006 jordan

Fixed code for handling _INI
Rearranged debug statements
ok marco@


# 1.28 16-Feb-2006 jordan

Added stub for calling _INI
Fixed aml_dowhile for NULL return
ok marco@


# 1.27 16-Feb-2006 jordan

Fixed parser/evaluator to retrieve battery info+state
Cleanup of parser
Dell laptops return battery status
ok marco@


# 1.26 06-Feb-2006 brad

fix a typo so this will compile on amd64.


# 1.25 04-Feb-2006 marco

When ACPI_ENABLED wasn't set but the devices were it had an unknown variable.

Pointed out by ian@.


# 1.24 03-Feb-2006 jordan

Rewrote aml parser yet again...
Fixed bus_space_map for i386/amd64
Added _BST and _BIF to acpibat to get status/info
ok marco@


# 1.23 20-Jan-2006 jordan

More evaluator changes
Removed old evaluator code
Fixed _HID value
ok marco@


# 1.22 19-Jan-2006 jordan

Added support for read/write of PCI Config space
Help jason@
ok marco@


# 1.21 18-Jan-2006 jordan

Added new evaluation routines
Added helper methods for buffer/field evaluation
Fixed broken indentation on dsdt.c

ok marco@


# 1.20 17-Jan-2006 jordan

Major overhaul of the aml parser. Evaluater temporarily disabled.

ok marco@


# 1.19 06-Jan-2006 grange

Check for ACPI control availability only if ACPI_ENABLE defined.


# 1.18 06-Jan-2006 grange

Informative error messages on attach, prettier dmesg.


# 1.17 06-Jan-2006 grange

printf -> dnprintf conversion for debug messages.


# 1.16 05-Jan-2006 grange

Find SLP_TYPx values and store them for future use in
acpi_enter_sleep_state().


# 1.15 05-Jan-2006 grange

Random KNF and cleanup


# 1.14 05-Jan-2006 grange

Provide symbolic names for the acpi sleeping states and use it.


# 1.13 28-Dec-2005 marco

Moved DEVNAME to a acpidev.h

ok jordan@


# 1.12 18-Dec-2005 sturm

add back comment about wrong code

ok marco


# 1.11 16-Dec-2005 marco

Remove test for kbd_reset. Prompted deraadt@, ok jordan@


# 1.10 16-Dec-2005 jordan

Added hooks for powerdown/power button press


# 1.9 16-Dec-2005 jordan

Cleaned up common power management register i/o
Added enter sleep state ioctl
Hooked power button interrupt
ok marco@


# 1.8 16-Dec-2005 marco

Remove autoconf chatter


# 1.7 16-Dec-2005 jordan

Moved DSDT AML parsing into parent ACPI driver
ok marco@


# 1.6 14-Dec-2005 marco

Add AC device glue.


# 1.5 14-Dec-2005 marco

Adjust debug printing levels to something more sane.


# 1.4 13-Dec-2005 marco

Add glue to attach devices to apci.


# 1.3 07-Dec-2005 marco

KNF


Revision tags: OPENBSD_3_8_BASE
# 1.2 10-Jul-2005 grange

Spacing cleanup.


# 1.1 02-Jun-2005 tholo

Start on a basic ACPI framework -- does not do much more than read out the
ACPI tables into kernel memory and attach ACPI and HPET timers currently.

In order to test this code, enabling the devices in GENERIC as well as
the ACPI_ENABLE option is needed. This code does not do any thermal
control yet, so this should be done with care depending on the platform.

In the tree so more people can contribute to making this more fully
featured.

Ok niklas@ grange@ tedu@


# 1.428 13-May-2024 kettenis

Ignore button events in the first 10 seconds after resume. On some ACPI
implementations pressing the power button to wake up a machine will cause
a power button notification to happen for that button press This would
initiate a power down, which isn't the user's intent.

Based on earlier diffs from deraadt@ and James Cook.

ok mlarkin@, "I'm not opposed" cheloha@


# 1.427 13-May-2024 jsg

remove prototypes with no matching function
ok mpi@


Revision tags: OPENBSD_7_5_BASE
# 1.426 08-Jan-2024 kettenis

Implement RootPathString support in the LoadTable() AML function. Fixes
booting OpenBSD on some (ancient?) Hyper-V version.

Tested by Henryk Paluch
ok mlarkin@


Revision tags: OPENBSD_7_4_BASE
# 1.425 08-Jul-2023 tobhe

Move /dev/apm related acpi code to acpi_apm.c which is only built on
amd64 and i386. This is the first step towards a machine independent
safe sleep API.

tested by yasuoka@ bluhm@
ok deraadt@ kettenis@


# 1.424 07-Jul-2023 claudio

Add support for multiple batteries to acpithinkpad setchargestart and
setchargestop. With this laptops like x270 or x240 properly set the
thresholds for both batteries.
Tested by kn@ and jmatthew@
OK kettenis@


# 1.423 06-Jul-2023 deraadt

moving acpiioctl to a different file than acpiopen is not going to work
for ramdisks. noticed by anton.
this must be reconsidered.


# 1.422 05-Jul-2023 tobhe

Move acpiioctl to acpi_x86.c, it is only used up on i386 and amd64.

ok kettenis@ deraadt@


# 1.421 29-Jun-2023 dv

acpi(4)/acpibtn(4): use opt-in approach for wakeup GPEs

Previously, any discovered GPE that could be enabled for wake was
being enabled prior to entering ACPI-based S3 or S4. On some newer
machines, this caused S3-based suspend to break as some devices we
don't fully control (e.g. lack of driver) might be capable of waking
the system and will do so almost instantly if the GPE is enabled for
wake up (i.e. the so called "instant wake" issue).

This changes to a model of having device drivers explicitly opt
into using their GPE for wake up.

The first driver to explicitly toggle a GPE for wake is acpibtn(4).

Variations of the above have been in snapshots for 2 weeks, so
tested by many.

ok deraadt@


Revision tags: OPENBSD_7_3_BASE
# 1.420 15-Mar-2023 kettenis

Add missing nameref resolving for the _DEP() package elements that indicate
dependencies between ACPI devices.

ok dlg@, deraadt@


# 1.419 10-Feb-2023 visa

Adjust knote(9) API

Make knote(9) lock the knote list internally, and add knote_locked(9)
for the typical situation where the list is already locked.

Remove the KNOTE(9) macro to simplify the API.

Manual page OK jmc@
OK mpi@ mvs@


Revision tags: OPENBSD_7_2_BASE
# 1.418 13-Sep-2022 kettenis

Split out the code that collects data from acpiac(4), acpibat(4) and
acpisbs(4) for apm(4) and hook it up to the arm64 version of apm(4) on
systems with ACPI.

ok kn@


# 1.417 12-Sep-2022 kettenis

Add support for level-triggered GPIO events.

ok mlarkin@


# 1.416 10-Sep-2022 kettenis

Don't restrict GPIO events to pin numbers below 256. With the _EVT()
method events corresponding to larger numbers are possible.

Fixes lid state detection on the x13s.

ok patrick@


# 1.415 03-Sep-2022 kettenis

Make sure we don't suspend if we have no way to wake up the machine.
The idea is that device drivers that can wake up the machine register
themselves by calling device_register_wakeup(). To prevent regressions
on amd64, we let acpi(4) register itself as a wakeup device if the AML
for the machine defines wakeup devices. This may be refined in the
future.

This diff will prevent people from suspending their arm64 SBCs without
having a way to wake them up. For now the only driver that registers
itself is axppmic(4), which means that at this moment only SBCs with
an allwinner A64 or H5 SoC will actually support suspend/resume.

ok mlarkin@, deraadt@


# 1.414 10-Aug-2022 patrick

Provide the AML root in ACPI's softc so that drivers that need access
don't have to declare it using extern.

Suggested by and with feedback from kettenis@
Tested by deraadt@ on arm64, amd64 and i386
Tested by bluhm@ on amd64 and i386


Revision tags: OPENBSD_7_1_BASE
# 1.413 17-Feb-2022 jsg

reduce includes


# 1.412 10-Feb-2022 visa

Embed klist head in acpi_softc to avoid explicit malloc.

OK kettenis@


# 1.411 09-Feb-2022 deraadt

Move x86-only stubs used by kern/subr_suspend.c into dev/acpi/acpi_x86.c
for sharing between i386 and amd64.
ok mlarkin kettenis


# 1.410 08-Feb-2022 deraadt

The suspend/resume code is a sticky mess of MI, MD, and ACPI sequencing.
This splits out the MI sequencing, backing it with per-architecture helper
functions. Further steps will be neccesary because ACPI and MD are too
tightly coupled, but soon we'll be able to use this code for more architectures
(which depends on figuring out the lowest-level cpu sleeping method)
ok kettenis


# 1.409 04-Feb-2022 robert

set the APM_BATT_CHARGING state if the battery is being charged and try
to calculate the remaining time to be fully charged

ok jca@


# 1.408 02-Feb-2022 deraadt

acpi_addtask() calls malloc() w/ M_NOWAIT (because some calls come from
interrupt context), this however means occasional resource shortage will
result in callbacks registration failing, and unknown consequences for
the task-submitting caller.
Changing this to use pools with a low water mark, decreases the odds
of that problem occuring.
ok kettenis


# 1.407 01-Feb-2022 deraadt

GPE_DIRECT is calling the AML parser from interrupt context, which is
not permitted. Luckily nothing is using GPE_DIRECT anymore, so this code
can be deleted.
ok kettenis


# 1.406 26-Jan-2022 kettenis

An ACPI device needs to be both present and enabled for it to function.
So only attempt to attach hardware that has both bits enabled. This fixes
an issue where com(4) would attach for a disabled serial port leading to
misdetection of the hardware variant and a subsequent hang when /etc/rc
runs ttyflags -a.

ok anton@, deraadt@


# 1.405 12-Jan-2022 patrick

Make acpi_getpropint() return uint64_t, as ACPI integers are in fact that
wide and some _DSD properties depend on it.

ok kettenis@


# 1.404 04-Jan-2022 patrick

acpi_getprop() needs to actually make sure that we're looking at the
correct property. While there adjust acpi_getpropint() as well to
increase similarity with acpi_getprop().

ok kettenis@


# 1.403 16-Dec-2021 anton

Attach com over acpi on amd64. Some hardware uses a different interrupt
assignment compared to the the legacy one supported by com over isa.
This causes the console to halt once userland takes over as no
interrupts are received. The actual address and irq can be read from
ACPI, kettenis@ already added support for arm64 which paved the way for
amd64.

Some consoles that previously attached over isa are now expected to
attach over acpi.

Thanks to patrick@ for testing on arm64.

ok kettenis@


# 1.402 07-Dec-2021 kettenis

Fix parsing of SR_IRQ resource descriptors.

ok patrick@, anton@


# 1.401 02-Nov-2021 deraadt

knf


# 1.400 30-Oct-2021 deraadt

Change hw.perfpolicy=auto by default, at startup. If the system has AC
power connected (default is yes when no driver differentiates) then default
to 100% performance. On battery, use the existing auto algorithm (which is
admittedly somewhat unrefined).
This change overrides the system/BIOS speed and puts OpenBSD in control.
As this happens very early during boot, besides speedups in all usage usage
patterns, some surprises: unhibernate and sysupgrade times are cut in half.
note: on a few architectures, the setperf fn pointer is changed late, and
thus the auto algorithm stops timeing out. kettenis and i will look for
a solution.
in snaps for more than a week.
ok kettenis


Revision tags: OPENBSD_7_0_BASE
# 1.399 20-Jul-2021 mlarkin

Fix trailing whitespace in a few places.

No code change.


# 1.398 09-May-2021 kettenis

Some x86 machines advertise the "hardware reduced" ACPI feature, advertise
S4 and S5 support, but fail to populate the SLEEP_CONTROL_REG and
SLEEP_STATUS_REG descriptions in the FADT. An example of such a machine
is the ASUS Zenbook 14 UM433DA. Any attempt to powerdown the machine
will result in a kernel crash. It turns out that using the legacy ACPI PM
registers works fine on this machien. So fall back on those registers
if SLEEP_CONTROL_REG and/or SLEEP_STATUS_REG aren't provided.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_9_BASE
# 1.397 15-Mar-2021 patrick

Add acpi_iommu_device_map(), which replaces the DMA tag with one that
is blessed with IOMMU magic, if available. This is mainly for arm64,
since on amd64 and i386 the IOMMU only captures PCIe devices, as far
as I know, which uses the pci_probe_device_hook(). This though is for
non-PCI devices attached through ACPI.

ok kettenis@


# 1.396 07-Mar-2021 patrick

Pass standard DMA tag to acpi(4) table drivers.

ok kettenis@


# 1.395 27-Dec-2020 deraadt

acpi_map_address() cannot be wrapped by SMALL_KERNEL anymore, as
it is used by acpihpet.c


# 1.394 25-Dec-2020 visa

Refactor klist insertion and removal

Rename klist_{insert,remove}() to klist_{insert,remove}_locked().
These functions assume that the caller has locked the klist. The current
state of locking remains intact because the kernel lock is still used
with all klists.

Add new functions klist_insert() and klist_remove() that lock the klist
internally. This allows some code simplification.

OK mpi@


# 1.393 17-Dec-2020 kettenis

Fix some issues with referencing named ACPI nodes from Packages.
These references need to be resolved at runtime rather than when
they're parsed such that they pick up the right values for those nodes
which can be changed when for example _INI methods run.

The current approach is to replace these reference with a string that
names the node in question. The problem with that is that packages
can also contain normal strings. Which means that we need special
code that depends on the context where the package is used.

This diff takes a different approach by retaining a reference when
parsing. Code that uses a package will need to resolve this reference
but there is no ambiguiety anymore.

ok patrick@


# 1.392 05-Dec-2020 kettenis

Add support for the _EVT method to the GPIO support code.

ok patrick@


Revision tags: OPENBSD_6_8_BASE
# 1.391 27-Aug-2020 jmatthew

Reorder the acpi attach process so we look at Processor() CPU nodes
after Device() ones, since we should prefer the newer node type.
If we see any Device() nodes, don't attach acpicpu(4) to any Processor()
nodes if they're also present. This also makes acpitz(4) and
acpipwrres(4) devices attach slightly later.

ok kettenis@, also tested by jmc@


# 1.390 16-Aug-2020 gnezdo

Remove sc_maxgpe from acpi_softc which was never read from

Remove a dead conditional from acpi_init_gpes while there.

ok jcs@


# 1.389 21-Jul-2020 deraadt

acpi can use IPL_BIO (a low interrupt) since it only enqueues operations for
later processing. The use of a high interrupt will predate suspend/resume
efforts, we had to redesign acpi to be non-reentrant obviously
discussed with kettenis, in snaps for more than a week


# 1.388 24-Jun-2020 cheloha

kernel: use gettime(9)/getuptime(9) in lieu of time_second(9)/time_uptime(9)

time_second(9) and time_uptime(9) are widely used in the kernel to
quickly get the system UTC or system uptime as a time_t. However,
time_t is 64-bit everywhere, so it is not generally safe to use them
on 32-bit platforms: you have a split-read problem if your hardware
cannot perform atomic 64-bit reads.

This patch replaces time_second(9) with gettime(9), a safer successor
interface, throughout the kernel. Similarly, time_uptime(9) is replaced
with getuptime(9).

There is a performance cost on 32-bit platforms in exchange for
eliminating the split-read problem: instead of two register reads you
now have a lockless read loop to pull the values from the timehands.
This is really not *too* bad in the grand scheme of things, but
compared to what we were doing before it is several times slower.

There is no performance cost on 64-bit (__LP64__) platforms.

With input from visa@, dlg@, and tedu@.

Several bugs squashed by visa@.

ok kettenis@


# 1.387 10-Jun-2020 jca

Give userland a consistent battery and A/C state after resume

Previously we refreshed the state of acpiac(4) and acpibat(4)/acpisbs(4)
by queueing fake ACPI events to be processed by the acpi thread.
acpibat(4) using ACPIDEV_POLL meant that its status could be refreshed
first, resulting in an APM_POWER_CHANGE event sent to userland before
the state of acpiac(4) was up to date. Because of this, apmd(8) could
see A/C unplugged when it was in fact plugged, possibly leading to
a wrong decision.

Instead of using fake ACPI events, refresh the status of acpiac(4) and
acpibat(4)/acpisbs(4) right away at DVACT_WAKEUP time. To reuse the
existing refresh code of those drivers, make their ACPI *_notify()
handlers responsible for sending APM_POWER_CHANGE events instead. Also
queue an APM_POWER_CHANGE event right after resume to retain existing
behavior.

This fixes the consistency of data shown to userland, and makes those
drivers less special.

Initially discussed last year (!) with mpi@ at Miod's place.
Feedback and ok jcs@, deraadt@ agrees with the use of DVACT_WAKEUP here.


# 1.386 29-May-2020 deraadt

dev/rndvar.h no longer has statistical interfaces (removed during various
conversion steps). it only contains kernel prototypes for 4 interfaces,
all of which legitimately belong in sys/systm.h, which are already included
by all enqueue_randomness() users.


# 1.385 14-May-2020 kettenis

Bring back the acpi(4) changes. This time around only use the ACPI _CRS
information on ACPI 5.0 and later.

ok krw@, patrick@


# 1.384 11-May-2020 jca

More accurate remaining power reporting with disparate batteries

Overall remaining power was computed as the average of the remaining
power of each battery, in percents. This doesn't work with batteries of
different capacity, so use the total capacity and remaining power
instead.

Reported by lidstah, successful tests by lidstah and benno@, ok kettenis@


# 1.383 08-May-2020 kettenis

Move parsing of _CRS into acpi(4) and pass the parsed address and interrupt
information in struct acpi_attach_args.

ok mpi@


Revision tags: OPENBSD_6_7_BASE
# 1.382 14-Apr-2020 kettenis

Implement acpi_getprop(), the ACPI equivalent of OF_getprop().

ok patrick@


# 1.381 12-Apr-2020 kettenis

Add support for the _CCA method. This method indicates whether DMA is
cache-coherent or not. To implement this, acpi(4) gets two bus_dma tags
and passes the appropriate one when attaching devices based on _CCA.
On i386/amd64, where for all practical purpose DMA is always cache-coherent,
the two tags are the same. But on arm64 they are distinct.

ok patrick@


# 1.380 07-Apr-2020 visa

Abstract the head of knote lists. This allows extending the lists,
for example, with locking assertions.

OK mpi@, anton@


# 1.379 03-Apr-2020 mpi

Fix incoherencies of errors returned by various *kqfilter routines.

EOPNOTSUPP is returned when there's no "filterops" for a given fd.
EINVAL when the requested filter isn't supported by the "filterops".
ENXIO means the underlying device is no longer valid/present.

ok visa@


# 1.378 20-Feb-2020 visa

Replace field f_isfd with field f_flags in struct filterops to allow
adding more filter properties without cluttering the struct.

OK mpi@, anton@


# 1.377 31-Dec-2019 visa

Use C99 designated initializers with struct filterops. In addition,
make the structs const so that the data are put in .rodata.

OK mpi@, deraadt@, anton@, bluhm@


# 1.376 31-Dec-2019 mpi

Convert infinite sleeps to {m,t}sleep_nsec(9).

ok kn@


# 1.375 31-Dec-2019 jsg

include sys/sensors.h for sensor use
don't depend on indirect inclusion from acpidev.h


Revision tags: OPENBSD_6_6_BASE
# 1.374 07-Sep-2019 kettenis

Revert acpipci(4) changes. Too many systems are broken. We'll try again
after 6.6 as been released. The acpireg.h change stays behind.


# 1.373 28-Aug-2019 kettenis

Use ACPI information to attach PCI busses like we do on arm64. There are a
few additional quirks though, and attaching the PCI busses is delayed to
replicate the existing code more closely. That may be changed in the
future. Also tweak how we handle MSI support and respect to ACPI flag
that says we shouldn't attempt to use MSIs.

Some fallout is expected.

ok patrick@


# 1.372 27-Aug-2019 deraadt

On amd64 the bootup and resume paths for SP/BP/SP were not performing
msr/pat, clock-startup, firmware upgrade, fpu initialization, and cpu
soft-state initialization in the same order. That kind of chaos is
crazy and fragile. Improve the acpi resume call sequence and unify order
of the operations.


# 1.371 02-Jul-2019 jcs

acpi_set_gpehandler: turn edge arg into flags, add support for
passing GPE_DIRECT which directly executes the supplied callback and
clears a level interrupt rather than going through acpi_taskq which
can be slow when other driver callbacks are in the queue.

This should not be used for GPE handlers that need to do anything
ACPI-related.

ok kettenis


# 1.370 10-Jun-2019 kettenis

Print proper ACPI version number.

ok mlarkin@


# 1.369 08-Jun-2019 kettenis

When establishing the mapping between ACPI device nodes and PCI devices,
skip PCI host bridges and devices that aren't there.

ok patrick@


# 1.368 07-Jun-2019 kettenis

Introduce acpi_getsta() and use it to eliminate some duplicated code.

ok patrick@


# 1.367 12-May-2019 kettenis

Make sure the extended PM register descriptions actually exist before
using them. Fixes machines with ancient ACPI 1.0 (such as recent
hypervisors).

ok deraadt@


# 1.366 12-May-2019 kettenis

Add support for mapping ACPI PM registers using the "extended" gasio
representation from the FADT. Mostly fixes the Lanner NCA-1510.

ok lteo@, mlarkin@, deraadt@


# 1.365 11-May-2019 lteo

Fix incorrect error message.

ok kettenis@


# 1.364 08-May-2019 tedu

free size. the allocation is the header plus the length.


Revision tags: OPENBSD_6_5_BASE
# 1.363 04-Apr-2019 kettenis

Avoid potential null-pointer dereference. Found by Kent R. Spillner.

ok deraadt@, jsg@


# 1.362 20-Jan-2019 jsg

fix SMALL_KERNEL build after changes in rev 1.361
reported by naddy@ ok deraadt@


# 1.361 19-Jan-2019 tedu

Add a pwraction sysctl that controls what the power button does on acpi.
By default, nothing changes -- shutdown is initiated. But allows turning
power button into a sleep button if desired.
(grudging) ok from a few parties


# 1.360 26-Oct-2018 kettenis

Add acpipci(4) on amd64. For now this only calls the PCI-specific _OSC
method to let the ACPI implementation know what features we support.


Revision tags: OPENBSD_6_4_BASE
# 1.359 30-Aug-2018 kettenis

Move softbutton check before the check that skips attaching certain acpi
devices. Fixes regression caused by matching on _CID in addition to
matching on _HID.

ok matthieu@, mlarkin@


# 1.358 25-Aug-2018 kettenis

Match ACPI devices based on _CID if no match for _HID is found.

ok mpi@


# 1.357 19-Aug-2018 kettenis

Add support for multiple PCI segments. Only really implemented for arm64
for now as amd64/i386 firmware still caters for legacy OSes that only
support a single PCI segment.

ok patrick@


# 1.356 03-Aug-2018 kettenis

Let ahci(4) match on _CLS instead of _HID when attaching at acpi(4). Avoids
having to add many more _HID entries to the match table.

ok deraadt@, mlarkin@


# 1.355 10-Jul-2018 kettenis

Make legacy interrupts work in acpipci(4).

ok patrick@


# 1.354 05-Jul-2018 kettenis

Add acpipci(4), a driver that supports generic ECAM-compatible PCI host
bridges based on information provided by ACPI.

ok mlarkin@


# 1.353 04-Jul-2018 kettenis

Properly pass around the PCI "chipset tag" in acpi(4) and refactor
acpimcfg(4) to call an MD initialization functions that sets up a tag for
PCI ECAM.

ok guenther@, mlarkin@, krw@


# 1.352 01-Jul-2018 kettenis

Move acpi(4) attach glue into acpi_machdep.c.

ok guenther@, deraadt@


# 1.351 01-Jul-2018 kettenis

Add support for _DSD Device Properties.

ok mlarkin@


# 1.350 30-Jun-2018 kettenis

Add intr_enable() function, intended for MI use to amd64 and i386 and use
this in the acpi(4) suspend/resume code paths.

ok deraadt@


# 1.349 29-Jun-2018 kettenis

Use <stdint.h> types.

ok mpi@, jung@, krw@, deraadt@


# 1.348 29-Jun-2018 mlarkin

ACPI: Allow (with warning) GPE handler reassignment, instead of returning
a failure code.

ok kettenis


# 1.347 26-Jun-2018 mlarkin

knf


# 1.346 26-Jun-2018 kettenis

Let acpi(4) pass down the bus dma tag.

ok deraadt@


# 1.345 25-Jun-2018 kettenis

Reorganize acpi(4) code a bit in preparation for upcoming arm64 support.

ok deraadt@


# 1.344 20-May-2018 kettenis

Wake up the acpi thread after scheduling a gpio event.

ok mpi@


# 1.343 17-May-2018 kettenis

Sprinkle some #ifndef SMALL_KERNEL such that RAMDISK kernels cuild again.

pointed out by stsp@


# 1.342 17-May-2018 kettenis

Implement GenericSerialBus OpRegion support.

ok mlarkin@


# 1.341 27-Mar-2018 kettenis

Add acpicmos(4), a driver that implements SystemCMOS OperationRegion
access support. This fixes machines where the AML doesn't check whether
support for this OperationRegion type has been registered by the OS.

ok mlarkin@


Revision tags: OPENBSD_6_3_BASE
# 1.340 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.339 10-Feb-2018 deraadt

Syncronize filesystems to disk when suspending. Each mountpoint's vnodes
are pushed to disk. Dangling vnodes (unlinked files still in use) and
vnodes undergoing change by long-running syscalls are identified -- and
such filesystems are marked dirty on-disk while we are suspended (in case
power is lost, a fsck will be required). Filesystems without dangling or
busy vnodes are marked clean, resulting in faster boots following
"battery died" circumstances.
Tested by numerous developers, thanks for the feedback.


# 1.338 08-Feb-2018 deraadt

After flushing disks, try again to release all memory. We don't need to
save dirty memory to the hibernate space.


# 1.337 08-Feb-2018 deraadt

Toss all releaseable memory, because fragmentation can get in the way
of allocating the hibernate playpen.


# 1.336 08-Feb-2018 deraadt

sr_quiesce() is a new approach for ensuring that softraid drains
output to the disks.

This is part of a larger suspend/resume filesystem-safety diff, which
has been worked on for a couple of months already. Tests by job, krw,
beck, benno, and others. Sometimes even by snapshot users...


# 1.335 29-Nov-2017 kettenis

Add support for GPIO-signaled events.

ok mlarkin@


# 1.334 16-Nov-2017 jcs

Add PCI attachment for dwiic(4) needed by Intel 100 Series machines

ok kettenis


# 1.333 14-Nov-2017 kettenis

Add a few more PNP IDs to the skip lists.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.332 17-Aug-2017 stsp

Make dwiic(4) attach its ACPI parent dependencies.
This change makes the Asus E200HA keyboard work.

Original analysis and patch by Cesare Gargano
ok kettenis@


# 1.331 09-Aug-2017 dcoppa

Add "PNP0303" (8042 PS/2 Controller) to acpi_isa_hids[]

ok kettenis@


# 1.330 22-Jul-2017 jcs

acpibat: look for _BIX first before falling back on _BIF

ACPI 4.0 deprecated _BIF for battery status so some newer machines
are exporting _BIX which extends _BIF's package with a few extra
fields. When using _BIX, export the new discharge cycle count as a
sensor.

feedback from kettenis and anton
ok deraadt


# 1.329 20-Jul-2017 kettenis

Start a list of isa(4) device names such that we can avoid printing them
as "not configured".

ok mikeb@, millert@, deraadt@


# 1.328 08-Apr-2017 deraadt

Sensors are run as callbacks inside tasks. During suspend a sensor
could be running inside a driver that will be force-detached, or due
to tsleep end up disrupting the softstate/hardstate contract. At
suspend time, quisce all these callbacks by waiting for completion.
This issue has never been observed for real, but may be implicated in
suspend/resume failures.
ok kettenis guenther mlarkin


# 1.327 08-Apr-2017 deraadt

Two prototypes for acpi_maptable; merge to one place


Revision tags: OPENBSD_6_1_BASE
# 1.326 27-Mar-2017 deraadt

Now that hibernate_alloc() only has clean success/failure, don't
need to call hibernate_free() to clean up a partial mess.
ok mlarkin kettenis


# 1.325 13-Mar-2017 deraadt

allow ramdisks to build; ok jcs


# 1.324 12-Mar-2017 jcs

try to attach acpisbs first and if successful, don't attach acpibat


# 1.323 02-Mar-2017 natano

Add a new sysctl machdep.lidaction. The sysctl works as follows:

machdep.lidaction=0 # do nothing
machdep.lidaction=1 # suspend
machdep.lidaction=2 # hibernate

lidsuspend is just an alias for lidaction, so if you change one, the
other one will have the same value. The plan is to remove
machdep.lidsuspend eventually when people have upgraded their
/ets/sysctl.conf.

discussed with deraadt, who came up with the new MIB name
no objections mlarkin
ok stsp halex jcs


# 1.322 28-Feb-2017 natano

Untangle abstract sleep modes from ACPI sleep states. This paves the way
for further work in the area.

idea and ok deraadt
ok mlarkin


# 1.321 22-Feb-2017 jcs

add acpisbs, an acpi smart battery subsystem driver reading data
over smbus

currently disabled because it conflicts with acpibat

ok deraadt, kettenis


# 1.320 14-Jan-2017 kettenis

Revert aml_rdpciaddr changes; breaks several machines that were working
before.


# 1.319 11-Jan-2017 jsg

Increase the size of the buffers used for _HID and _CID strings to be
able to fit "Hyper_V_Gen_Counter_V1".

ok mlarkin@ deraadt@


# 1.318 08-Jan-2017 kettenis

Make aml_rdpciaddr() work for for devices behind bridges. The code now
uses the mapping between AML nodes and PCI devices that we establish.
Because _INIT methods may end up calling aml_rdpciaddr(), make sure we
create that mapping early. Also handle devices that aren't actually present.
These devices are now included in the mapping and reads will return an
all-ones patterm whereas writes are a no-op..

ok mlarkin@


# 1.317 25-Oct-2016 pirofti

Add the resource index to aml_parse_resource() callback function.

This is needed by an upcoming acpiec commit that handles machines breaking
the current ACPI specifications.

Change suggested by and ok kettenis@, guenther@


# 1.316 18-Sep-2016 guenther

If the APIC table (MADT) isn't present then ci_acpi_proc_id won't be set
when acpicpu tries to attach, leading to a NULL dereference. The ACPI
processor object on the Dells where this was reported is bogus and useless
anyway so apply the same test when deciding whether to attach to processor
objects as acpicpu would when matching them to cpus.

Problem reported by Eivind Eide (xenofil (at) gmail.com) and
Johan Huldtgren (johan+openbsd-bugs (at) huldtgren.com).
Testing by Eivind Eide.
ok kettenis@ mlarkin@


# 1.315 03-Sep-2016 naddy

Write the system time back to the RTC every 30 minutes.
This fixes the problem that long-running machines which were not
shut down properly would reboot with a badly offset system time.

hints and ok kettenis@


# 1.314 31-Aug-2016 mlarkin

Don't put a device in the wake device list if _STA says it's not there.

Fixes at least one machine that woke up from sleep / poweroff immediately.

ok kettenis@, tested by kettenis and martijn@


# 1.313 28-Jul-2016 kettenis

Store the acpi processor ID/UID in struct cpu_info, and use it to attach
acpicpu(4) drivers to the right cpu(4).

ok mlarkin@, guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.312 10-Jun-2016 kettenis

The power management timer has been made optional in ACPI 5.0A, so don't
attempt to attach acpitimer(4) if the timer isn't present.

Pointed out by jcs@

ok jcs@


# 1.311 08-May-2016 kettenis

Make sure devices listed by a device's _DEP method are attached before
we attach the device itself. The _DEP methode, introduced in ACPI 5.0,
is a hint that the device in question depends on OpRegion support from
the devices listed by _DEP, so we have to order them the proper way.
To prevent us from attaching those devices again when we encounter them
later walking down the device tree, keep track of our attempts to
attach a device driver to them using aflag in the aml_node structure.

ok guenther@


# 1.310 25-Apr-2016 pirofti

We attach acpihpet(4) and acpiec(4) without using the HID, so do not
report it in the dmesg.

OK deraadt@, mlarkin@.


# 1.309 02-Apr-2016 kettenis

Reduce dmesg spam by knocking out some acpi hardware IDs that don't need a
driver.

ok mpi@


# 1.308 29-Mar-2016 kettenis

Check if a device is present (using _STA) in acpi_foundhid. This prevents
us from reporting devices as "not configured" that aren't actually present,
and allows us to remove duplicated code from several drivers.

ok deraadt@, ok jsg@


# 1.307 29-Mar-2016 kettenis

Implement a cleaner way to prevent acpivout(4) from attaching if we have
a platform-specific method of controlling the backlight.

ok millert@, mlarkin@, jung@


# 1.306 28-Mar-2016 kettenis

Adding each and every new acpi device driver to acpi_foundhid() is
gettinga bit of the burden. And it really isn't how our autoconf
framework is supposed to work for busses that can be enumerated. All
the drivers already check for a matching _HID in their attach
function. So we can just drop the checks in acpi_foundhid() and let
the drivers sort themselves out.

Print a "not configured" line for devices for which we don't attach adriver.
This may be a bit much as there are devices for which we will not have a
driver. This will be removed if it causes too much dmesg spam.

ok deraadt@, mlarkin@


Revision tags: OPENBSD_5_9_BASE
# 1.305 17-Jan-2016 jsg

Take acpi_foundhid() outside of #ifndef SMALL_KERNEL so the ramdisk
kernel can find and attach dwiic(4) and sdhc(4).


# 1.304 17-Jan-2016 jsg

remove an unused global variable


# 1.303 14-Jan-2016 kettenis

Attach dwiic(4) on the Intel Bay Trail i2c controllers as well.


# 1.302 12-Jan-2016 kettenis

Improve emulation of PM registers on Hardware-redcued ACPI by emulating
SPL_TYPx, SLP_EN and WAK_STS using the new SLEEP_STATUS_REG and
SLEEP_CONTROL_REG registers. Makes the transition into S5 (powerdown) work
on the ASUS X205TA.

ok deraadt@, mlarkin@


# 1.301 12-Jan-2016 jcs

Add dwiic, a driver for the Synopsys DesignWare i2c controller found
on the Samsung ATIV Book 9 laptop. This initial version only
supports ACPI config/attachment.

Add ihidev, a HID-over-i2c driver largely based on uhidev. dwiic
handles attaching ihidev devices found in ACPI.

Add ims, a HID-over-i2c mouse/trackpad driver to get basic cursor
and button functionality from HID-compliant i2c trackpads.

ok kettenis deraadt


# 1.300 11-Jan-2016 kettenis

Make sdhc(4) attach to hardware IDs "80860F14" and "PNP0FFF".


# 1.299 09-Jan-2016 kettenis

Initial support for Hardware-Reduced ACPI. This skips the legacy mode checks
by emulating the PM_CNT register such that SCI_EN is always enabled. It also
skip attaching acpitimer(4) since the relevant registers are no longer there.

ok deraadt@, mlarkin@


# 1.298 06-Jan-2016 kettenis

Follow the spec and check the SCI_EN bit to see if ACPI has already
been enabled and don't bother enabling it again if it is. This should fix
systems that don't implement legacy mode and therefore set ACPI_ENABLE and
ACPI_DISABLE to zero.

ok mlarkin@, yasuoka@


# 1.297 23-Nov-2015 reyk

Back out the CRITICAL change, turn it back from 10% to 25%. Some
people with old/broken batteries didn't like this change, and the
underlying heuristics are wrong.


# 1.296 22-Nov-2015 reyk

The "CRITICAL" APM battery level, that is exported/faked by the ACPI
stack, is currently set to 25%. With 25% battery left, a typical
modern laptop will have at least one hour runtime left. It is an
arbitrary value, and other APM power layers in the kernel set CRITICAL
to something like 5% or 10%, so set it to 10% for now.

OK mlarkin@


# 1.295 28-Sep-2015 deraadt

In low-level suspend routines, set cold=2. In tsleep(), use this to
spit out a ddb trace to console. This should allow us to find suspend
or resume routines which break the rules. It depends on the console
output function being non-sleeping.... but that's another codepath which
should try to be safe when cold is set.
ok kettenis


# 1.294 06-Sep-2015 deraadt

clarify malloc arguments (base + length...)


# 1.293 28-Aug-2015 deraadt

fairly simple sizes for free(); ok tedu


# 1.292 20-Aug-2015 kettenis

Respect the access size when reading or writing to pci config space.
This version also makes sure writes are properly aligned, preventing
the panics seen with the previous incarnation of this change.

Fixes battery status passthrough in vmware, brightness keys on my x220
and who knows what else.

ok mlarkin@, deraadt@


Revision tags: OPENBSD_5_8_BASE
# 1.291 04-Aug-2015 deraadt

If we are setting ACPI_PM1_BM_RLD do it only once, and remember to
repeat this action at resume time.
ok mlarkin guenther


# 1.290 29-Jul-2015 deraadt

Something subtle in previous revision is locking up suspend or booting
on some laptops. Mailing lists threads mention x220 and Asus machines.
Reverting for now.


# 1.289 21-Jul-2015 kettenis

iRespect the access size when reading or writing to pci config space.
Fixes battery status passthrough in vmware and who knows what else.

Tested by many.

ok mlarkin@, deraadt@


# 1.288 17-Jul-2015 mlarkin

fix comments


# 1.287 26-Mar-2015 jsg

Use the 0x10 extended capability to determine if the trackstick
buttons are wired to the trackpad and need to be re-routed to the
trackstick. Without this change the buttons on 2015 thinkpads
get picked up as extended buttons that show up as scroll up/down.

Remove the X1 Carbon 2015 (LEN0048) and X250 (LEN0046) from the
top button area/soft buttons quirks list. Also avoid using the
quirk list entirely if the capability bit is set.

Based on work from jcs@ with feedback from mpi@
ok mpi@ jcs@


# 1.286 20-Mar-2015 kettenis

The ACPI standard says that a PCI function number of FFFF can be used to refer
to all functions on a device. This makes no sense in the context of accessing
PCI config space. Nevertheless stsp@ has a machine with AML that tries to
do config space reads from a device where _ADR encodes function number FFFF.
This is almost certainly buggy AML, but since Linux (and presumably Windows)
boot just fine on this machine we need to work around this. Linux effectively
treats function FFFF as function 7, which isn't present on this machine.
Instead we simulate a read from a non-existent device by returning a bit
pattern of all ones. Writes to function FFFF will still panic.

ok mlarkin@


# 1.285 16-Mar-2015 deraadt

Reduce use of SMALL_KERNEL in acpi, bringing acpiec driver and more gpe
handling into RAMDISK. This is now possible because the install media
has ample room. The goal is to reduce special cases where we may be
skipping (unknown) important operations...
ok mlarkin kettenis


# 1.284 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.283 07-Feb-2015 deraadt

New framework that allows hibernate to pass in entropy from it's fresh
boot.
ok mlarkin


# 1.282 06-Feb-2015 mlarkin

Fix a hibernate crash on some machines due to unmapping a page that
may not have been mapped previously (in the failure to hibernate case).

Also ensure that the lowmem ptp is mapped in all cases (not just MP).

ok kettenis


# 1.281 17-Jan-2015 deraadt

resume_randomness() before spinning up other cpus...


# 1.280 15-Jan-2015 jsg

Define a new wscons mouse type for Synaptics clickpad devices that lack
physical buttons. This will be used if the acpi pnpid for the mouse
matches a list provided by Synaptics found in Linux.

Combined with the xenocara changes this will remove the need for an xorg
config file for the x240/t440/t540 generation of thinkpads.

ok matthieu@ shadchin@ kettenis@


# 1.279 11-Jan-2015 kettenis

Tolerate checksum errors in ACPI tables (but complain about them). Apparently
there are machines out there with broken BIOSen with such checksum errors in
thier RSDT tables. Windows runs fine on these machines, so it probably does
not check. Linux ignores checksum errors as well (and prints a warning).

ok deraadt@, krw@


# 1.278 18-Dec-2014 deraadt

suspend and resume the RNG (at what looks like the right spots)
ok reyk djm


# 1.277 09-Dec-2014 doug

Replace some malloc(n*size,...) calls with mallocarray().

ok tedu@ deraadt@


# 1.276 06-Dec-2014 mpi

If "machdep.lidsuspend" is not set, do not go back to sleep if the lid
is still closed when resuming. This allows docked laptop to be resumed
without opening them.

ok mlarkin@


# 1.275 23-Nov-2014 mlarkin

If a machine has no _LIDs, then none can be open. This diff fixes a problem
where machines without any _LIDs were going back to sleep after hibernate
due to the recent suspend-if-lid-closed-on-resume diff. Noticed on qemu,
which doesn't have any _LIDs.

discussed with deraadt


# 1.274 18-Nov-2014 krw

Nuke yet more obvious #include duplications.

ok miod@


# 1.273 08-Nov-2014 mlarkin

If resuming from sleep (zzz/ZZZ) and the lid is still closed, go back to
sleep. This prevents accidental lid flex or slight opening in a backpack
from waking the machine up and leaving it resumed (powered on).

ok deraadt@


# 1.272 26-Sep-2014 kettenis

Rework piglet and pig allocation. Currently the piglet gets allocated
deep down in the suspend path, where it is really hard to recover from
allocation failure. So allocate the piglet early on in the suspend path.
Also change the piglet and piglet allocation functions to use km_alloc(9)
instead of doing pmemrange magic. This removes a bunch of code which, in the
case of the piglet allocation, is broken since it results in a NULL pointer
dereference. Also switch the piglet allocation to not wait. If we can't
allocate 16MB of phys contig memory on a halfway modern machine we're almost
certainly under a lot of memory pressure and we're better off not trying to
hibernate anyway.

ok mlarkin@


# 1.271 25-Sep-2014 kettenis

Bring back rev 1.249. Now that mp_setperf() has been fixed, it should be
safe to restore perflevel when resuming. Should fix the issues people
see with the processor running at full speed heating up the machine while
the apm command reports that the CPUs are running at a low speed.

ok dcoppa@, deraadt@


# 1.270 23-Sep-2014 kettenis

Release the acpi lock around calls to wsdisplay_suspend() and
wsdisplay_resume(). The X server may adjust the display brightness just
before/after the VT switch when suspending/resuming. In fact, it may
already be blocked adjusting the display brightness for some reason
unrelated to the suspend request.

The VT switch request will eventually time out, but the delay is annoying
and the failed VT switch will lead to problems after resume.

Needed for a future update of the xf86-video-intel driver.

ok mlarkin@


# 1.269 19-Sep-2014 kettenis

Use config_suspend_all(9).


# 1.268 08-Sep-2014 kettenis

Clear the PME Status bit on PCI devices in response to Device Wake events.
Fixes the interrupt storm on the 2009 Mac mini with WOL enabled on its
nfe(4) interface.

ok mlarkin@


Revision tags: OPENBSD_5_6_BASE
# 1.267 20-Jul-2014 mlarkin

Support hibernating to softraid crypto volumes.

much help and ok from deraadt@


# 1.266 16-Jul-2014 mlarkin

Reenable hibernate RLE support and flush+zero all memory after unpack.


# 1.265 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.264 12-Jul-2014 kettenis

Add a function to drop all clean pages on the page daemon queues and call
it when we hibernate.

ok mlarkin@, miod@, deraadt@


# 1.263 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.262 11-Jul-2014 mlarkin

Flush the buffercache to 16MB on hibernate and restore its previous max
size (kern.bufcachepercent) on resume, for better hibernate performance.

ok beck@


# 1.261 10-Jul-2014 blambert

remove unneeded header

ok mlarkin@


# 1.260 01-Jul-2014 kettenis

Adding an additonal delay after each access to SystemMemory or SystemIO
during autoconf makes no sense.

ok deraadt@


# 1.259 23-Jun-2014 kettenis

SystemMemory is used to access memory mapped registers on some machines, so we
must use the bus_space(9) API to access it instead of memcpy(9). Also make
sure we properly align access to fields that need more than byte alignment.

ok guenther@


# 1.258 21-May-2014 mlarkin

Fix invalid / missing format specifiers in several dnprintf statements
(used only when ACPI_DEBUG is enabled)

From a diff on tech@ by Fabian Raetz, with a minor change by me, tested
on i386 and amd64 with ACPI_DEBUG enabled and disabled.


# 1.257 25-Apr-2014 mlarkin

Defer the free of the hibernate structures until later, when it is safe to
do so.

Freeing the hibernate structures too early leads to inadvertent calls into
splx when we aren't ready for them yet. Found when trapping splx calls in
the resume path.

discussed with deraadt@ at m2k14


# 1.256 13-Apr-2014 mlarkin

Fix a badly worded debug message that implied the entire machine was going
to sleep, instead of just the acpi thread going to sleep.


# 1.255 31-Mar-2014 mpi

Change the order of operations during a suspend/resume cycle and
call bufq_quiesce() after executing the DVACT_QUIESCE handlers.

This should be safe since no disk nor controller drivers have such
handler but it will allow us to detach sd(4) devices attached to a
USB bus.

Another benefit pointed out by kettenis@ is that drivers that need
to read a firmware from the disk should be able to do it at resume
time in a DVACT_WAKEUP handler.

ok kettenis@, deraadt@


# 1.254 13-Mar-2014 dlg

get rid of the assumption that the head of the alldevs list is the
"mainbus" device. this breaks when mpath is enabled because it
attaches before mainbus and therefore takes the head position.

have autoconf provide device_mainbus() which looks up mainbus_cd,
and use that instead.

discussed with deraadt who just wants mpath stuff to move forward
despite there being many ways to shine this particular turd.


Revision tags: OPENBSD_5_5_BASE
# 1.253 21-Feb-2014 deraadt

Do the lid control further on in the resume sequence, since this is
AML, and will do WAITOK operations. While cold, that is a bad idea.
We seem to be safe with respect to the spec, I do not see any event
debounce issues here.
ok kettenis, tested by many


# 1.252 20-Jan-2014 claudio

Instead of checking sc->sc_revision <= 1 check that the
fadt->hdr_revision <= 1. This is a better check since it checks
the version of the proper descriptor. This allows the alix APU
to reboot since those boards have currently a wrong sc->sc_revision.
Committing this now so that it can be tested easier.
With and OK jsg@ also fine by derradt@


# 1.251 24-Dec-2013 kettenis

If the FADT has its SMI_CMD set to zero, assume we're only ACPI-only hardware
and don't need to disable SMI ownership of the ACPI hardware registers.

ok mlarkin@


# 1.250 23-Dec-2013 kettenis

Revert previous commit. It makes resume hang on some MP systems running
GENERIC.MP.


# 1.249 16-Dec-2013 kettenis

Restore hw.setperf upon resume like we do for apm(4) on i386. Seems we need
to do this fairly late in the resume to avoid hangs.

ok deraadt@, mlarkin@


# 1.248 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


# 1.247 06-Nov-2013 mpi

Add support for Power Resources for Dx states and the necessary hook
for PCI devices. This hook should be called twice, before and after
changing the power state of a PCI device.

Before setting the device to the new state, the ACPI layer will notify
every power resources linked to the device for that state and make sure
they are turned "_ON". After changing the state of the device, it will
decrement the reference of every power resources linked to that device
for the old state and turn them "_OFF" if they are no longer referenced.

This fixes the no-USB after resume problem seen on various ThinkPad,
problem initialy diagnosed with Alexander Polakov.

ok kettenis@, deraadt@


Revision tags: OPENBSD_5_4_BASE
# 1.246 01-Jun-2013 mlarkin

Stop printing ACPI wakeup devices in dmesg after the 16th wakeup device.

Helps with VMware VMs that report hundreds of wakeup devices.

ok reyk@, deraadt@


# 1.245 31-May-2013 bluhm

Fix the build for a kernel without wd and pciide in its config.
OK deraadt@ tedu@


# 1.244 30-May-2013 deraadt

Enforce ca_activate tree-walks over the entire heirarchy for all events,
cleaning up some shutdown-hook related code on the way.
(A few drivers related to sparc are still skipped at kettenis' request)
ok kettenis mlarkin, tested by many others too


# 1.243 18-Apr-2013 deraadt

pin the acpi thread to the BSP, because AML and SMI cannot be trusted. I
was certain we were doing this already, but it turns out no...


Revision tags: OPENBSD_5_3_BASE
# 1.242 24-Dec-2012 guenther

In acpiioctl(), Return though the splx() path if suser() returns an error

ok deraadt@


# 1.241 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


# 1.240 04-Oct-2012 ehrhardt

Use information provided by ACPI to attach secondary PCI host bridges.

ok kettenis@


# 1.239 07-Sep-2012 kettenis

Add a new function called acpi_pci_min_powerstate() that returns the lowest
power state for a PCI device for the power state (currently S3 or S4) we're
transitioning into. This function is probably too simplistic; there is a lot
of (silly) complexity in the ACPI spec.

ok deraadt@


Revision tags: OPENBSD_5_2_BASE
# 1.238 13-Jul-2012 pirofti

Disable acpivideo(4) on ASUS laptops.

There are some models that panic whenever an event is received.
Some cope, some don't. In order to get this right acpiec(4) requires
major rework. Until then let machines boot.

Discussed with deraadt@.


# 1.237 11-Jul-2012 mlarkin

for now, restrict hibernate to root in the acpi ioctl kernel interface.
normal rules for controlling this interface through apmd still apply.

ok deraadt@


# 1.236 09-Jul-2012 deraadt

tiny little oops in untested hibernate block


# 1.235 09-Jul-2012 deraadt

clean up ioctl() case statements


# 1.234 09-Jul-2012 deraadt

acpi_disable_onegpe() is not used; ok pirofti


# 1.233 24-May-2012 kettenis

If _MIN > _MAX consider a WordBus resource to be invalid. Fixes a panic on
the IBM x3550-M4.

ok mikeb@


# 1.232 29-Mar-2012 mlarkin

Missing #ifdef in previous diff


# 1.231 29-Mar-2012 mlarkin

Prevent hibernation attempts on machines with unsupported disk controllers.
The same check is also performed later in the hibernate sequence, but by
then it's too late to cleanly unwind (presently).

ok deraadt


# 1.230 26-Mar-2012 deraadt

simplify conditionals in acpi_sleep_state()
ok mlarkin


# 1.229 26-Mar-2012 deraadt

hook in the hibernate request code; half of this diff is from mlarkin
ok mlarkin


Revision tags: OPENBSD_5_1_BASE
# 1.228 20-Sep-2011 deraadt

Very early in suspend while the clock is still ticking realtime,
save the clock back to the rtc. The zaurus already did this.
ok phessler miod kettenis


Revision tags: OPENBSD_5_0_BASE
# 1.227 02-Jul-2011 nicm

kqueue attach functions should return an errno or 0, not a plain 1. Fix
the obvious cases to return EINVAL and ENXIO.

ok tedu deraadt


# 1.226 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.225 16-Jun-2011 pirofti

Import acpitoshiba driver.

This is originally written by Hiroyuki Aizu, ported and integrated
in our tree by Javier Vazquez <javama4 AT gmail> with tweaks and
suggestions by me.

Import prodded by deraadt@ and the lack of testers on tech@.


# 1.224 27-Apr-2011 jcs

attach acpithinkpad to newer lenovo models like the x120e

ok deraadt@


# 1.223 22-Apr-2011 jordan

Fix uninitialzied variables and formatting strings (-Wxxx errors)


Revision tags: OPENBSD_4_9_BASE
# 1.222 02-Jan-2011 jordan

Re-add code for ACPI taskqueue, eliminates need for state vars in acpi softc
Serialize processing ACPI tasks for sleep/powerdown/GPE event
ok deraadt@


# 1.221 31-Oct-2010 guenther

Revert last commit: it breaks resume on Thinkpads
"then please back it out" deraadt@


# 1.220 26-Oct-2010 jordan

Add task queue for ACPI gpe and notify handlers
This fixes eject on Dell Latitude dock and an issue on the Dell Mini
battery update.
ok deraadt


# 1.219 07-Oct-2010 jordan

Create separate functions for enabling wake and runtime gpes.
Remove old suspend/resume gpewalk function
ok deraadt, mlarkin


# 1.218 05-Oct-2010 jordan

split acpi_enable_onegpe to separate enable/disable functions
ok deraadt


# 1.217 05-Oct-2010 jordan

Added acpi_disable_allgpes function to clear GPE status
ok deraadt


# 1.216 05-Oct-2010 deraadt

clean up acpi_softc use more


# 1.215 05-Oct-2010 deraadt

delete a bit of #if 0 code


# 1.214 05-Oct-2010 deraadt

hide use of our global acpi_softc pointer a bit better


# 1.213 29-Sep-2010 deraadt

grab the acpi thread lock very early in acpi thread startup; not that it
is likely that anyone will race us... but why be careless.
ok kettenis


# 1.212 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.211 29-Aug-2010 jasper

- add a check for FWRITE here too.

ok miod@


# 1.210 27-Aug-2010 kettenis

Make PCI Power Management optional, and only enable it when acpi(4) attaches.

ok deraadt@


Revision tags: OPENBSD_4_8_BASE
# 1.209 08-Aug-2010 kettenis

Close race between the acpi thread and wscons ioctls running in process
context. We might rip this out post-release and replace it with a solution
that serializes things in a more obvious way.

ok deraadt@, miod@ and "those crazy texans who ok anything"


# 1.208 08-Aug-2010 deraadt

Report the acpi sleep states when on a RAMDISK; ok krw


# 1.207 07-Aug-2010 kettenis

Enforce acpi register access rescrictions.

ok mlarkin@, deraadt@, tested by marco@


# 1.206 07-Aug-2010 deraadt

upon resume, notify all the acpiac and acpibat drivers to update
their status. some laptops do not do this automatically.
ok kettenis canacar


# 1.205 06-Aug-2010 marco

Always call _PSW on _LID devices that support it to enable lid open to
resume the machine. Conversely disable it when the machine wakes up.

Tested by several ok deraadt


# 1.204 06-Aug-2010 deraadt

As part of a revamp of the PM1/GPE code, I write this diff to look for
unmanaged EN & STS bits in the PM1 register at interrupt time and report
them. As a side effect this splits the STS acknowledgement into two writes
(for power, and sleep) instead of one. The printf that is added (to spot
unmanaged STS bits) has not yet been triggered as far as we know.

Before the "write to PM1 registers at the right offset" diff went in, this
was not neccessary. But newer thinkpads do not have a working soft power
button without this diff.

We have no idea why.
ok mlarkin kettenis


# 1.203 05-Aug-2010 deraadt

PM1 power button events were generating 'fake events' to acpibtn PWRB,
but if there is no such device these were being tossed. Instead, arrange
for all power button handlers to flag a softc variable which is processed
by the thread; so that one place will know to request a powerdown (psignal
to init)
ok marco mlarkin


# 1.202 05-Aug-2010 deraadt

rename a variable which is annoyingly poorly named


# 1.201 05-Aug-2010 mlarkin

Write to the correct offset for various ACPI pmreg registers. Writing to
the correct register does properly clear the wake status bits and fixed
event bits, which allows some machines to have more than 1 suspend cycle.

bug spotted by deraadt@

tested by deraadt@ and myself on a half-dozen machines and others on
hackers@

ok deraadt@, marco@


# 1.200 05-Aug-2010 deraadt

Fix the power button. The pm1 register is 16 bits in size.
My mistake, a while back.
tested by krw


# 1.199 04-Aug-2010 kettenis

Print supported sleep states.

ok marco@, phessler@, deraadt@


# 1.198 03-Aug-2010 mlarkin

Use the proper flag for re-enabling certain hardware events (power btn,
sleep btn). The (incorrect) flag prevoiously used coincidentally had the
same value, so this shouldn't cause any different behavior than before.

tested on a variety of machines (i386, amd64, sp, mp) - no behavioral
change seen

ok deraadt@


# 1.197 28-Jul-2010 marco

Make all gasio failures look and behave the same.


# 1.196 28-Jul-2010 mlarkin

Delay a little longer on suspend before giving up. Fixes machines that
sometimes don't suspend because the sleep transition takes too long
(symptoms like failing to sleep but the backlight stays on).

ok deraadt@


# 1.195 27-Jul-2010 deraadt

remove XXX; EC can now be attached early when needed; ok jordan


# 1.194 27-Jul-2010 deraadt

more brackets feels good


# 1.193 27-Jul-2010 deraadt

XXX annotate a block in acpi_enter_sleep_state() that is pretty worrying


# 1.192 27-Jul-2010 deraadt

horrific indentation that nearly made me puke


# 1.191 27-Jul-2010 mlarkin

Set the sleeping indicator light on machines that support it, in case SMI
doesn't do it for us. Shows the 'moon' LED on older Thinkpads when
sleeping.

Tested on T43p, W500, T510 by me and on X61s by deraadt@.

ok deraadt@


# 1.190 27-Jul-2010 jordan

Early initialization of acpiec if ECDT table exists
Fixes hang when booting thinkpads while docked
ok deraadt


# 1.189 26-Jul-2010 deraadt

remove merge error which was commited


# 1.188 26-Jul-2010 pirofti

Add support for Sony ACPI hotkeys via a new driver: acpisony(4).

Currently it only works for the suspend button and tries to do right for
the brightness events, but I haven't found a Sony laptop that like to do
right so far.

In the future I want to make the brightness keys work on all Sony's and
also add support for the zoom hotkeys and whatever other funky keys I can
find on those things.

Okay deraadt@.


# 1.187 22-Jul-2010 deraadt

pretty large cleanup of SMALL_KERNEL


# 1.186 21-Jul-2010 deraadt

in the acpi GPE handler, late ack level interrupts not edge interrupts.
Duh.


# 1.185 21-Jul-2010 deraadt

Merge the edge and level gpe handlers into one (now possible since the
gpe structure tells us whether the gpe is edge or level)


# 1.184 21-Jul-2010 deraadt

delete #if 0 code we are not going to use; ok marco


# 1.183 20-Jul-2010 deraadt

make the mask code for acpi_enable_onegpe() match other similar code


# 1.182 20-Jul-2010 deraadt

Handle the acpi interrupt controller with proper edge/level handling,
wrapped in spltty to avoid racing against the interrupt controller.
Repair the gpe bit masking code while there, and do operations in the
right order
ok kettenis mlarkin, help from jordan at figuring out the order of
operations.


# 1.181 20-Jul-2010 matthew

When we're preparing to go to sleep, only call bufq_restart() if other
preparations failed.

ok kettenis@, thib@, deraadt@


# 1.180 19-Jul-2010 deraadt

permit compilation with ACPI_DEBUG


# 1.179 19-Jul-2010 deraadt

there is no need for a seperate dsdt_softc pointer; dsdt does not have a
softc but is just a part of the single acpi driver.
ok jordan


# 1.178 19-Jul-2010 deraadt

acpi_delay() is no longer needed; ok jordan


# 1.177 18-Jul-2010 mlarkin

Clear event status on resume, to avoid going back to sleep/reboot/shutdown
immediately after resume. Tested by a bunch of people on hackers@ on
various hardware. Fixes at least Toshiba NB200 and Gateway LT3103u.

ok marco


# 1.176 13-Jul-2010 deraadt

rename some thread related interfaces so they are logical


# 1.175 10-Jul-2010 jordan

Verify _ADR bdf is correct (some systems return 0xffff)
Works on Dell x9xx and Studio 1555


# 1.174 09-Jul-2010 jordan

Clear the noise


# 1.173 08-Jul-2010 jordan

Add mapping for ACPI device to PCI bdf (match autoconf tree)
Simplify resource parsing function to use buffer argument
Convert namespace linked lists to use queue macros
ok marco@, deraadt@


# 1.172 06-Jul-2010 deraadt

Use spltty() to lock downcalls from apm against the information being
modified by the acpi thread.
ok kettenis


# 1.171 06-Jul-2010 deraadt

Don't use a workq to do the suspend, because it races aginst the acpi
thread. Instead, just tell the acpi thread to do the suspend for us.
This makes apmd & zzz work correctly. While here, have acpithinkpad
attempt to post the event to apm before prompting the suspend itself.
ok kettenis marco mlarkin


# 1.170 05-Jul-2010 mlarkin

Reset register SCI_EN on resume. The ACPI spec clearly states we don't have
to do this, but many machines don't, or forget to, which leads to all sorts
of problems on resume (broken battery stats, overheating, failure to resume
more than once, etc).

tested by many, ok marco@, deraadt@


# 1.169 01-Jul-2010 thib

do bufq quiesce again, since vnd/the bufq_done call in
biodone has been fixed.


# 1.168 01-Jul-2010 jordan

Backout recent AML changes, breaks on R210 and others


# 1.167 01-Jul-2010 jordan

Simplified aml_parse_resource call, cleanup code
ok marco


# 1.166 01-Jul-2010 jordan

Fix subordinate bus # for multi-root PCI buses
ok marco, kettenis


# 1.165 30-Jun-2010 thib

Disable/partially backout the bufq quiesce changes as this
is causing havoc with vnds and release must be buildable.


# 1.164 29-Jun-2010 deraadt

Fix ramdisks (new code was placed inside a SMALL_KERNEL chunk)


# 1.163 29-Jun-2010 jordan

Add support for mapping ACPI to PCI devices
ok kettenis, deraadt


# 1.162 29-Jun-2010 kettenis

Call bufq_quiesce() on suspend to stop all I/O and bufq_restart on resume.
Makes suspend/resume work much better while doing disk I/O.

ok mlarkin@


# 1.161 29-Jun-2010 tedu

60 is a more reasonable guess for number of minutes in an hour than 100.
spotted by TeXitoi. ok deraadt


# 1.160 27-Jun-2010 jordan

Add _acpi_matchhids routine, precursor to pci device match


# 1.159 27-Jun-2010 mlarkin

Clear ACPI fixed event status on resume (power buttons/etc) so that some
machines don't immediately resume after suspending to S3.

Tested by kettenis@ and I on 5 or 6 machines, deraadt@ "yeah, looks good"


# 1.158 27-Jun-2010 jordan

Cleaned up acpi_maptable, adds to table list directly


# 1.157 07-Apr-2010 deraadt

If we have done a wsdisplay_suspend(), and ACPI decides it does not want to
suspend, we must roll back by calling wsdisplay_resume() or the virtual
console switching will be locked forever
ok oga


# 1.156 07-Apr-2010 kettenis

Make suspend/resume work on MP machines (running an MP kernel). Joint work
from mlarkin@ and me, with some amd64 fixes thrown in by deraadt@

ok marco@, deraadt@, pirofti@, mlarkin@


# 1.155 31-Mar-2010 kettenis

Bring /dev/apm support in line with apm(4). Make the suspend button send
a suspend request event to apmd(8) instead of suspending immediately. Also
keep track of whether /dev/apm and /dev/apmctl are currently open, such that
we can still suspend immediately if apmd(8) isn't running.

ok deraadt@, marco@, pirofti@, jsing@, oga@


# 1.154 30-Mar-2010 oga

Prevent the apmd/x races for good.

When we hit suspend time, go through all wsdisplays on the system. if
they are in mode MAPPED, but not MODE_DUMBFB then if possible do a full
vt switch to a !mapped vt, and prevent switching back until resume time.
This has to be called from MD code because this involves userland
running so that X can run the vt switch signal handler. This way, any
case where we are using the "poke registers from userland" model, we
will not be on the hardware when we go down, so the kernel can actually
handle thing properly.

Tested on several acpi laptops (by kettenis@ and ian@), x40 (me and
beck@ at LEAST) and zaurus (me). Maybe others, but if so I forgot who at
this time..

Idea from deraadt somewhere over the Faroe Islands (I thought of a
similar thing myself a while ago). Much prompting from him. Ok and
comments miod@


# 1.153 25-Mar-2010 oga

Fix & vs. && in the kqfilter code.

Based on a comparison with the apm code.

ok deraadt@, kettenis@


Revision tags: OPENBSD_4_7_BASE
# 1.152 26-Nov-2009 mlarkin

Visual (and on some machines, audio) feedback while the machine is
resuming via calls to aml _SST method. This also makes the "moon" LED on
thinkpads to blink during resume and cease when resume is completed.

ok pirofti@


# 1.151 26-Nov-2009 deraadt

Toggle into "cold" when we we disable interrupts, because tsleeping
in the AML interpreter and context switching into userland processes
tends to sometimes pop the ACPI trampoline page out of our address
space. Makes lots more machines work.
ok kettenis mlarkin


# 1.150 24-Nov-2009 jsg

in the resume path evaluate _WAK before dealing with device drivers
like the spec says, not after.

ok mlarkin@


# 1.149 24-Nov-2009 deraadt

abort suspends on SMP systems until that gets written; ok mlarkin


# 1.148 23-Nov-2009 mlarkin

pci should return the result of it's children's suspend/resume calls back
to its parent.

handle suspend failure case and unwind if devices have an activate function
that returns failure.

ok deraadt@, kettenis@


# 1.147 23-Nov-2009 mlarkin

acpi* devices need to call their children on suspend/resume.
ok deraadt@


# 1.146 23-Nov-2009 pirofti

Remove ACPI_SLEEP_ENABLED checks.

This enables by default the suspend/resume paths in the kernel.

Okay deraadt@.


# 1.145 23-Nov-2009 deraadt

knf and spacing that are super annoying


# 1.144 23-Nov-2009 mlarkin

Hook up Theo's new children suspend/resume function, as well as more ACPI
suspend/resume glue.

ok deraadt@


# 1.143 26-Oct-2009 deraadt

Do not do strncmp comparisons on dv_xname because longer device names which
look similar could arrive in the future. Instead, compare directly against
dv_cfdata->cf_driver->cd_name
Issue originally spotted by miod


# 1.142 26-Oct-2009 jordan

Verify checksum+address when loading ACPI tables.
Some systems had invalid entries in RSDT/XSDT.
ok marco@


# 1.141 23-Jul-2009 cnst

New aibs(4) driver for ASUSTeK AI Booster (ACPI ATK0110) hardware monitoring,
with sensor state support through limits provided by the ACPI.

Tested on several ASUS motherboards kindly networked by Sam Fourman Jr.

ok deraadt marco jordan


Revision tags: OPENBSD_4_6_BASE
# 1.140 03-Jun-2009 pirofti

Import acpi power resource driver. This is needed for suspend/resume logic.

Okay jordan@ and marco@.


# 1.139 03-Jun-2009 pirofti

Add acpivideo support. This adds brightness support for all laptops
except thinkpads, they will use the acpithinkpad driver. The driver is
also hooked into wsconsole. So brightness can be adjusted via:

$ wsconsctl display.brightness=<percentage>

This is very helpfull on some laptops that have a nasty bios and get two
steps instead of one when pressing the brightness button.

Tested on various dell, fujitsu, acer, samsung and other laptops.

Okay marco@, miod@. Suggestions from kettenis@.
Lots of reviews and help from miod@, thanks!


# 1.138 03-Jun-2009 jordan

Added ejectable bay for IDE cdroms
ok marco@


# 1.137 30-Apr-2009 marco

Oga changed apm ioctls and forgot to grep. Temporary work around by hitting
the sleep button when we get any request. This interface will chnage at
some point.
ok oga


# 1.136 19-Apr-2009 krw

Call acpi_sleep_walk() when powering down just like when entering
any other sleep state. This tells acpi wake devices to not wake up
the box until manual powering up resets their status. Makes my Dell
GX520 stay powered off in response to 'halt -p'.

In snaps for a while.

ok marco@ (several times) deraadt@


# 1.135 17-Apr-2009 pirofti

Add device ranking support for future sleep/wake developments.

okay marco@


# 1.134 11-Mar-2009 jordan

Cleanup.. use aml_evalinteger instead of aml_evalname
ok marco@


# 1.133 10-Mar-2009 jordan

Fix cut/paste error.. call _BFS not _PTS
ok marco@


Revision tags: OPENBSD_4_5_BASE
# 1.132 21-Feb-2009 marco

overzealous cut/paste; found by Vladimir Kirillov <proger@uaoug.org.ua>


# 1.131 20-Feb-2009 marco

Oops didn't mean for Debugger() to remain there.


# 1.130 19-Feb-2009 marco

suspend/resume bits so that we can develop this in tree. This is disabled.
code from mlarkin and me
help from art,toby,jordan and several others
ok jordan, go for it deraadt


# 1.129 10-Feb-2009 jordan

Fix null pointer for shutdown if acpi not enabled
ok marco@


# 1.128 04-Feb-2009 kettenis

Some systems that claim to be ACPI 2.0 apparently don't provide an XSDT
address. Make the code fall back on the RSDT on those systems.

From Joerg Niendorf <p3y80@internode.on.net>. Fixes PR 5986.


# 1.127 17-Jan-2009 krw

A couple of stray malloc/memset -> malloc(M_ZERO), a bit of knf and
lose a couple of unnecessary casts. No functional change.

Feedback from kettenis@

ok kettenis@ marco@


# 1.126 06-Nov-2008 marco

Collapse all sanity checks of hids into one function.

Originaly from jordan


Revision tags: OPENBSD_4_4_BASE
# 1.125 02-Jul-2008 fgsch

add acpivideo. for now almost an empty skel so others can work on
suspend/resume. eventually it will also manage output switching and
brightness where supported. prodded by marco@


# 1.124 11-Jun-2008 marco

Fix $OpenBSD$ for once and for all


# 1.123 08-Jun-2008 deraadt

aaa_dev was being set from an uninitialized variable; ok toby


# 1.122 07-Jun-2008 marco

Kill ACPIVERBOSE. It was never used and is worthless to boot.

comments jmc
ok toby


# 1.121 06-Jun-2008 marco

Shrink acpi some more for floppies and while here re-enable acpi on the
floppies. Fixes crazy sony vaio kettenis has.

tested by deraadt
ok deraadt jordan kettenis


# 1.120 01-Jun-2008 marco

Fix \n nit in SMALL_KERNEL.


# 1.119 01-Jun-2008 marco

Re-enable new parser. Lots of testing by various folks. Fits all media
now too.

ok kettenis deraadt


# 1.118 16-May-2008 dlg

back out jordans new parser.

it didnt have enough oks from the right people, it did break machines
that were previously working, and it bloated the RAMDISKS and install
media beyond the constraints of the media.

requested by deraadt@


# 1.117 14-May-2008 jordan

Adding new ACPI Parser code
Old guts of code still exists, needs to be torn out next
ok marco@
ok brad@


# 1.116 27-Apr-2008 jcs

add acpithinkpad(4), an acpi support driver for ibm/lenovo
thinkpads.

provides functionality that the bios did on older, non-acpi
thinkpads like respond to screen brightness keys and toggle the
bluetooth device on and off.

ok deraadt@ and marco@


# 1.115 24-Apr-2008 jsing

Introduce acpiasus(4), a driver for the ACPI based hotkeys found in many
ASUS laptops (including the ASUS EeePC) - largely based on NetBSD's
asus(4) driver. On the ASUS EeePC this allows us to enable/disable
wireless, change screen brightness and use the volume keys.

ok jsg@, weingart@


# 1.114 12-Apr-2008 kettenis

Get rid of acpi_s5 global variables; simply send SIGUSR2 instead of SUGUSR1
to tell init(8) to power down the machine.

ok krw@


Revision tags: OPENBSD_4_3_BASE
# 1.113 21-Jan-2008 jsg

Check against RESET_REG_SUP in FADT before doing the acpi reset dance
after all, not doing so appears to break more machines than it fixes.

ok marco@


# 1.112 05-Jan-2008 kettenis

Never attach more acpicpu(4) devices than cpu(4) devices. Always attach
the first acpicpu(4) device on non-MULTIPROCESSOR kernels, regardless of its
ACPI processor ID.

ok mikeb@


# 1.111 05-Dec-2007 deraadt

Hang acpi(4) below bios(4) instead of mainbus(4). This lets us move acpi(4)
before pcibios(4), and then.... if acpi(4) attaches, skip pcibios(4) since
it messes the machines up. Fixes claudio's HP dl320 G5, and almost assuredly
others.
ok kettenis


# 1.110 05-Dec-2007 deraadt

repair acpiopen/acpiclose and friends to handle the /dev/apmctl interface,
so that apmd(8) can actually work correctly against acpi(4), and thus,
even do the fancy -C/-A stuff; also tested by jmc


# 1.109 02-Dec-2007 fgsch

fix the trailing space for real now.


# 1.108 02-Dec-2007 fgsch

avoid trailing spaces on output; marco@ ok.


# 1.107 27-Nov-2007 kettenis

Fix off-by-one that made us attach acpicpu0 and acpicpu1 on GENERIC kernels.

ok marco@


# 1.106 25-Nov-2007 jsg

When available and ACPI is enabled use the ACPI reset method
to reboot. This is required for some quirky sis machines
and likely others to reboot.

Tested by many. 'commit early commit often' deraadt@


# 1.105 19-Nov-2007 deraadt

sigh, amd64 still uses that stupid acpi_enabled variable...


# 1.104 19-Nov-2007 deraadt

softc's come initialized to 0, you know...


# 1.103 19-Nov-2007 deraadt

store revision; from jsg


# 1.102 19-Nov-2007 kettenis

Initialize sc_facs earlier.

ok deraadt@, beck@, canacar@


# 1.101 16-Nov-2007 deraadt

fix the bus_space #define nightmare, so that amd64 and i386 are much more
uniform. as a result shared code like acpi needs less #ifdef's
ok marco kettenis


# 1.100 15-Nov-2007 deraadt

remove the #ifdef ACPI_ENABLE. the EC codepath is either safe enough (and
will be improved), or it is unsafe because AML assumes it must be there
ok almost everyone


# 1.99 15-Nov-2007 mikeb

Make acpicpu(4) attach to the existing CPUs only.

Problem reported by form@. Fix idea by kettenis@.
Lots of help from deraadt@. Tested by deraadt@, form@ and me.

ok deraadt marco


# 1.98 14-Nov-2007 deraadt

spacing


# 1.97 12-Nov-2007 deraadt

correct : vs , in attach-time messages


# 1.96 06-Nov-2007 deraadt

all ACPI_ENABLE kernels need acpi_interrupt()


# 1.95 06-Nov-2007 deraadt

move acpi_read_pmreg() and acpi_write_pmreg() out of SMALL_KERNEL so
that acpi ramdisks can compile (obvious solution)


# 1.94 05-Nov-2007 canacar

Establish the acpi interrupt earlier. Fixes problems with some
GENERIC.MP kernels not getting acpi interrupts. This is a
workaround until the real issue with late establishment is fixed.
ok weingart@, beck@, gwk@


# 1.93 03-Nov-2007 jordan

Added support for displaying wakeup devices
ok beck@,weingart@,gwk@


# 1.92 11-Oct-2007 kettenis

When walking the tree yo find _PRT methods, check whether the device is
actually enabled and functioning. Similar to what we do for _INI.

ok weingart@, beck@


# 1.91 08-Oct-2007 krw

More simple memset(,0,) -> M_ZERO changes. In this batch move to
size(*p) as the first malloc() parameter where p is declared locally
and thus easy to check. Add M_ZERO to gpe_table allocation in acpi.c
even though there is no obvious bzero or memset nearby.


# 1.90 13-Sep-2007 mk

Remove unnecessary memset() (see aml_evalnode(9)) and a bit of KNF.

ok toby


# 1.89 13-Sep-2007 weingart

Implement ACPI 6.5.1 spec tree walk for _STA and _INI.
Fixes mk's laptop. No regressions so far (thank you to the
testers).

ok gwk@, mk@, marco@


Revision tags: OPENBSD_4_2_BASE
# 1.88 17-Apr-2007 mk

Now that acpidock no longer hangs machines during boot (at least
according to the reports I've been getting) it should be safe to enable
it again, so remove the #if 0 stuff around it.

ok marco


# 1.87 11-Apr-2007 jordan

Added changes for C-State
ok marco@


# 1.86 26-Mar-2007 marco

Calculate minutes left more accurately.

From giovanni <qgiovanni@gmail.com>


Revision tags: OPENBSD_4_1_BASE
# 1.85 03-Mar-2007 mk

Stop looking for acpidock until we fix that stuff that breaks all pre-60
series ThinkPads with acpidock(4) enabled.


# 1.84 22-Feb-2007 marco

We were getting no interrupts in .MP. There are 2 fixes; one in
acpi_machdep.c and move the interrupt establishment down after ioapic
has been initialized.

ok jordan


# 1.83 21-Feb-2007 kettenis

Make an acpi-enabled GENERIC.MP work on systems that only support PIC mode.

ok marco@


# 1.82 19-Feb-2007 jordan

Added acpi_map_address function for mapping GAS/IO registers
ok marco@


# 1.81 18-Feb-2007 jordan

Added dynamic perfect hash for opcodes
ok marco@


# 1.80 18-Feb-2007 marco

Shave off another K pre compression.
Shuffle SMALL_KERNEL functions around so that we only have a single #ifndef


# 1.79 17-Feb-2007 marco

Add SMALL_KERNEL throught acpi to make it fit on boot media.
ok deraadt


# 1.78 13-Feb-2007 marco

Add smarter _INI code. This helps with some dissapearing devices.

ok kettenis jordan


# 1.77 31-Jan-2007 gwk

Introduce acpi_hasprocfvs thats "ACPI Has Processor Frequency and Voltage
Scalling" a global flag set by acicpu attach to indicate that ACPI is
handling fvs through the _PSS objects. This will fix disappearing
EST/powernow when booting kernels with acpi enabled, as acpicpu isnt
compiled into GENERIC yet. Also in cases where acpicpu is built in and
legacy EST/powernow work but acpi is missing the _PSS object, the legacy
routines will be used.

tested by dim@, and Nick Nauwelaerts <nick AT nauwelaerts.net>
ok marco@, dim@


# 1.76 22-Jan-2007 mk

Actually look for docking stations by looking for _DCK nodes.

ok marco


# 1.75 02-Jan-2007 marco

On some systems EC must be attached as the first device. Move EC from
generic _HID devices to its own routine to force first attach. Bug found
and debugged by joris@

Code mostly written by joris@


# 1.74 26-Dec-2006 marco

Add polling to devices that require it such as acpibat and acpitz.
Use the same tsleep/wakeup mechanism as the interrupt code to keep it non-
concurrent.


# 1.73 21-Dec-2006 deraadt

even more knf love


# 1.72 21-Dec-2006 marco

Remove some leftover acpiec gpe/interrupt code.


# 1.71 20-Dec-2006 deraadt

oops, name already used


# 1.70 20-Dec-2006 deraadt

rename __acpi_enable_gpe(); ok marco


# 1.69 20-Dec-2006 deraadt

acpi_unmap_pmregs() is not used, ok marco


# 1.68 19-Dec-2006 deraadt

simplify some low-level methods substantially (shrinking code); ok marco jordan


# 1.67 06-Dec-2006 mk

malloc(..., M_NOWAIT) with no NULL check is bad. If allocation fails,
complain, clean up, and bail out of attach.

input and ok gwk


# 1.66 28-Nov-2006 jordan

Fixed clearing arg for acpi_init_pic
ok kettenis@


# 1.65 27-Nov-2006 kettenis

Don't spam dmesg.


# 1.64 27-Nov-2006 jordan

Fixes for acpiprt routing
Post-parse fixup for namerefs
ok kettenis@


# 1.63 25-Nov-2006 marco

Don't handle acpiev events inside the gpe handler since this causes deadlocks.

ok jordan@


# 1.62 15-Nov-2006 kettenis

Hook up acpi apic interrupt routing support to generic acpi code.

ok jordan@


# 1.61 02-Nov-2006 marco

Kill acpiec attach function and add its functionality to the generic _HID
device attach

prodded and ok jordan


# 1.60 02-Nov-2006 marco

Fix comments to reflect reality.


# 1.59 19-Oct-2006 marco

Replace ACPI ioctls with APM ioctls. This makes ACPI talk the same protocol
as APM therefore userspace can now retrieve power information in the same
fashion as all other apm arches (zaurus, macppc, i386 etc).

To use force the use of acpi device like so: apmd -f /dev/acpi

ok jordan@


# 1.58 19-Oct-2006 jordan

Fixed GPE debug levels
Fixed ref counter on temporary bufferfields
DSDT parsing fully working on IBM Thinkpad
ok marco@


# 1.57 15-Oct-2006 jordan

Removed old GPE handler code remnants
ok marco@


# 1.56 12-Oct-2006 jordan

Massive ACPI changes
Fixed ACPI leaks
new ACPI API
ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.55 30-Jun-2006 gwk

KNF, No functional change,
"go at it" marco@


# 1.54 30-Jun-2006 jordan

Support for calling Embedded Controller via new GPE interface
ok marco@


# 1.53 30-Jun-2006 jordan

Added new code for GPE handling
Also works with ACPIEC; need to fixup acpiec.c to remove original interrupt handler
ok marco@


# 1.52 30-Jun-2006 jordan

Added offset argument for acpi read/write pmreg routines (new GPE code)
ok marco@


# 1.51 31-May-2006 todd

do not call acpi_powerdown() unless acpi has successfully initialized
ok gwk@ canacar@


# 1.50 29-May-2006 canacar

Add embedded controller support.
ok marco@ deraadt@ testing & ok gwk@


# 1.49 19-May-2006 canacar

Minimal thermal zone driver. Monitors thermal zone temperature,
shuts down the system if the 'critical temperature' is reached.
ok marco@


# 1.48 11-Apr-2006 marco

KNF.


# 1.47 11-Apr-2006 gwk

Add acpi_resume(struct acpi_softc *sc), which calls some AML methods,
detected at attach time, currently unused.

ok marco@


# 1.46 11-Apr-2006 gwk

Discover some ACPI sleep related methods, and call them at the
appropriate time in acpi_enter_sleep_state().

ok marco@


Revision tags: OPENBSD_3_9_BASE
# 1.45 26-Feb-2006 marco

Add acpicpu as a device.

ok jordan@


# 1.44 23-Feb-2006 marco

remove dead function.

ok jordan@


# 1.43 22-Feb-2006 marco

Print out all tables it found during autoconf. We'll need this data in
the future.


# 1.42 22-Feb-2006 marco

Make dmesg less verbose.

ok jordan.


# 1.41 22-Feb-2006 jordan

Added parsing of SSDT tables
ok marco@


# 1.40 22-Feb-2006 jordan

Added new aml_register to notify by device id
Moved powerdown code to button device
ok marco@


# 1.39 21-Feb-2006 marco

Readjust debug levels that crept in too low over the last few days.
KNF


# 1.38 21-Feb-2006 marco

Rig the power button so that the machine shuts down correctly.


# 1.37 20-Feb-2006 marco

Add acpibtn device. Remains disabled for now.


# 1.36 19-Feb-2006 jordan

Added code for aml_notify GPE notification callback
ok marco@


# 1.35 19-Feb-2006 grange

Fix acpi_enter_sleep_state() by replacing current ugly hack
with mostly correct sleeping state transition code.

ok brad@ marco@


# 1.34 19-Feb-2006 marco

jordan:
Enable interrupts.
Added peripheral code to make this work.

marco:
Replace softintr and other types of interrupts with a handler with context.
Fixed interrupt magic for GPEs.
Some KNF

This is work done by jordan and I but has to be a single commit.


# 1.33 17-Feb-2006 marco

Remove earlier introduced delays. We really only need them during autconf.

ok jordan@


# 1.32 17-Feb-2006 marco

Add a generic delay function as per the ACPI spec.

from jordan@


# 1.31 17-Feb-2006 marco

Use tsleep instead of delay when !cold.


# 1.30 17-Feb-2006 marco

Argh, we need delays between reads/writes. The backend reads (i2c most likely)
are so slow that values come back as 0 sometimes. We have to do this
despite the spec saying we shouldnt have to. Guess having a faster
parser than others can hurt sometimes.

Idea from jordan.


# 1.29 17-Feb-2006 jordan

Fixed code for handling _INI
Rearranged debug statements
ok marco@


# 1.28 16-Feb-2006 jordan

Added stub for calling _INI
Fixed aml_dowhile for NULL return
ok marco@


# 1.27 16-Feb-2006 jordan

Fixed parser/evaluator to retrieve battery info+state
Cleanup of parser
Dell laptops return battery status
ok marco@


# 1.26 06-Feb-2006 brad

fix a typo so this will compile on amd64.


# 1.25 04-Feb-2006 marco

When ACPI_ENABLED wasn't set but the devices were it had an unknown variable.

Pointed out by ian@.


# 1.24 03-Feb-2006 jordan

Rewrote aml parser yet again...
Fixed bus_space_map for i386/amd64
Added _BST and _BIF to acpibat to get status/info
ok marco@


# 1.23 20-Jan-2006 jordan

More evaluator changes
Removed old evaluator code
Fixed _HID value
ok marco@


# 1.22 19-Jan-2006 jordan

Added support for read/write of PCI Config space
Help jason@
ok marco@


# 1.21 18-Jan-2006 jordan

Added new evaluation routines
Added helper methods for buffer/field evaluation
Fixed broken indentation on dsdt.c

ok marco@


# 1.20 17-Jan-2006 jordan

Major overhaul of the aml parser. Evaluater temporarily disabled.

ok marco@


# 1.19 06-Jan-2006 grange

Check for ACPI control availability only if ACPI_ENABLE defined.


# 1.18 06-Jan-2006 grange

Informative error messages on attach, prettier dmesg.


# 1.17 06-Jan-2006 grange

printf -> dnprintf conversion for debug messages.


# 1.16 05-Jan-2006 grange

Find SLP_TYPx values and store them for future use in
acpi_enter_sleep_state().


# 1.15 05-Jan-2006 grange

Random KNF and cleanup


# 1.14 05-Jan-2006 grange

Provide symbolic names for the acpi sleeping states and use it.


# 1.13 28-Dec-2005 marco

Moved DEVNAME to a acpidev.h

ok jordan@


# 1.12 18-Dec-2005 sturm

add back comment about wrong code

ok marco


# 1.11 16-Dec-2005 marco

Remove test for kbd_reset. Prompted deraadt@, ok jordan@


# 1.10 16-Dec-2005 jordan

Added hooks for powerdown/power button press


# 1.9 16-Dec-2005 jordan

Cleaned up common power management register i/o
Added enter sleep state ioctl
Hooked power button interrupt
ok marco@


# 1.8 16-Dec-2005 marco

Remove autoconf chatter


# 1.7 16-Dec-2005 jordan

Moved DSDT AML parsing into parent ACPI driver
ok marco@


# 1.6 14-Dec-2005 marco

Add AC device glue.


# 1.5 14-Dec-2005 marco

Adjust debug printing levels to something more sane.


# 1.4 13-Dec-2005 marco

Add glue to attach devices to apci.


# 1.3 07-Dec-2005 marco

KNF


Revision tags: OPENBSD_3_8_BASE
# 1.2 10-Jul-2005 grange

Spacing cleanup.


# 1.1 02-Jun-2005 tholo

Start on a basic ACPI framework -- does not do much more than read out the
ACPI tables into kernel memory and attach ACPI and HPET timers currently.

In order to test this code, enabling the devices in GENERIC as well as
the ACPI_ENABLE option is needed. This code does not do any thermal
control yet, so this should be done with care depending on the platform.

In the tree so more people can contribute to making this more fully
featured.

Ok niklas@ grange@ tedu@


# 1.428 13-May-2024 kettenis

Ignore button events in the first 10 seconds after resume. On some ACPI
implementations pressing the power button to wake up a machine will cause
a power button notification to happen for that button press This would
initiate a power down, which isn't the user's intent.

Based on earlier diffs from deraadt@ and James Cook.

ok mlarkin@, "I'm not opposed" cheloha@


# 1.427 13-May-2024 jsg

remove prototypes with no matching function
ok mpi@


Revision tags: OPENBSD_7_5_BASE
# 1.426 08-Jan-2024 kettenis

Implement RootPathString support in the LoadTable() AML function. Fixes
booting OpenBSD on some (ancient?) Hyper-V version.

Tested by Henryk Paluch
ok mlarkin@


Revision tags: OPENBSD_7_4_BASE
# 1.425 08-Jul-2023 tobhe

Move /dev/apm related acpi code to acpi_apm.c which is only built on
amd64 and i386. This is the first step towards a machine independent
safe sleep API.

tested by yasuoka@ bluhm@
ok deraadt@ kettenis@


# 1.424 07-Jul-2023 claudio

Add support for multiple batteries to acpithinkpad setchargestart and
setchargestop. With this laptops like x270 or x240 properly set the
thresholds for both batteries.
Tested by kn@ and jmatthew@
OK kettenis@


# 1.423 06-Jul-2023 deraadt

moving acpiioctl to a different file than acpiopen is not going to work
for ramdisks. noticed by anton.
this must be reconsidered.


# 1.422 05-Jul-2023 tobhe

Move acpiioctl to acpi_x86.c, it is only used up on i386 and amd64.

ok kettenis@ deraadt@


# 1.421 29-Jun-2023 dv

acpi(4)/acpibtn(4): use opt-in approach for wakeup GPEs

Previously, any discovered GPE that could be enabled for wake was
being enabled prior to entering ACPI-based S3 or S4. On some newer
machines, this caused S3-based suspend to break as some devices we
don't fully control (e.g. lack of driver) might be capable of waking
the system and will do so almost instantly if the GPE is enabled for
wake up (i.e. the so called "instant wake" issue).

This changes to a model of having device drivers explicitly opt
into using their GPE for wake up.

The first driver to explicitly toggle a GPE for wake is acpibtn(4).

Variations of the above have been in snapshots for 2 weeks, so
tested by many.

ok deraadt@


Revision tags: OPENBSD_7_3_BASE
# 1.420 15-Mar-2023 kettenis

Add missing nameref resolving for the _DEP() package elements that indicate
dependencies between ACPI devices.

ok dlg@, deraadt@


# 1.419 10-Feb-2023 visa

Adjust knote(9) API

Make knote(9) lock the knote list internally, and add knote_locked(9)
for the typical situation where the list is already locked.

Remove the KNOTE(9) macro to simplify the API.

Manual page OK jmc@
OK mpi@ mvs@


Revision tags: OPENBSD_7_2_BASE
# 1.418 13-Sep-2022 kettenis

Split out the code that collects data from acpiac(4), acpibat(4) and
acpisbs(4) for apm(4) and hook it up to the arm64 version of apm(4) on
systems with ACPI.

ok kn@


# 1.417 12-Sep-2022 kettenis

Add support for level-triggered GPIO events.

ok mlarkin@


# 1.416 10-Sep-2022 kettenis

Don't restrict GPIO events to pin numbers below 256. With the _EVT()
method events corresponding to larger numbers are possible.

Fixes lid state detection on the x13s.

ok patrick@


# 1.415 03-Sep-2022 kettenis

Make sure we don't suspend if we have no way to wake up the machine.
The idea is that device drivers that can wake up the machine register
themselves by calling device_register_wakeup(). To prevent regressions
on amd64, we let acpi(4) register itself as a wakeup device if the AML
for the machine defines wakeup devices. This may be refined in the
future.

This diff will prevent people from suspending their arm64 SBCs without
having a way to wake them up. For now the only driver that registers
itself is axppmic(4), which means that at this moment only SBCs with
an allwinner A64 or H5 SoC will actually support suspend/resume.

ok mlarkin@, deraadt@


# 1.414 10-Aug-2022 patrick

Provide the AML root in ACPI's softc so that drivers that need access
don't have to declare it using extern.

Suggested by and with feedback from kettenis@
Tested by deraadt@ on arm64, amd64 and i386
Tested by bluhm@ on amd64 and i386


Revision tags: OPENBSD_7_1_BASE
# 1.413 17-Feb-2022 jsg

reduce includes


# 1.412 10-Feb-2022 visa

Embed klist head in acpi_softc to avoid explicit malloc.

OK kettenis@


# 1.411 09-Feb-2022 deraadt

Move x86-only stubs used by kern/subr_suspend.c into dev/acpi/acpi_x86.c
for sharing between i386 and amd64.
ok mlarkin kettenis


# 1.410 08-Feb-2022 deraadt

The suspend/resume code is a sticky mess of MI, MD, and ACPI sequencing.
This splits out the MI sequencing, backing it with per-architecture helper
functions. Further steps will be neccesary because ACPI and MD are too
tightly coupled, but soon we'll be able to use this code for more architectures
(which depends on figuring out the lowest-level cpu sleeping method)
ok kettenis


# 1.409 04-Feb-2022 robert

set the APM_BATT_CHARGING state if the battery is being charged and try
to calculate the remaining time to be fully charged

ok jca@


# 1.408 02-Feb-2022 deraadt

acpi_addtask() calls malloc() w/ M_NOWAIT (because some calls come from
interrupt context), this however means occasional resource shortage will
result in callbacks registration failing, and unknown consequences for
the task-submitting caller.
Changing this to use pools with a low water mark, decreases the odds
of that problem occuring.
ok kettenis


# 1.407 01-Feb-2022 deraadt

GPE_DIRECT is calling the AML parser from interrupt context, which is
not permitted. Luckily nothing is using GPE_DIRECT anymore, so this code
can be deleted.
ok kettenis


# 1.406 26-Jan-2022 kettenis

An ACPI device needs to be both present and enabled for it to function.
So only attempt to attach hardware that has both bits enabled. This fixes
an issue where com(4) would attach for a disabled serial port leading to
misdetection of the hardware variant and a subsequent hang when /etc/rc
runs ttyflags -a.

ok anton@, deraadt@


# 1.405 12-Jan-2022 patrick

Make acpi_getpropint() return uint64_t, as ACPI integers are in fact that
wide and some _DSD properties depend on it.

ok kettenis@


# 1.404 04-Jan-2022 patrick

acpi_getprop() needs to actually make sure that we're looking at the
correct property. While there adjust acpi_getpropint() as well to
increase similarity with acpi_getprop().

ok kettenis@


# 1.403 16-Dec-2021 anton

Attach com over acpi on amd64. Some hardware uses a different interrupt
assignment compared to the the legacy one supported by com over isa.
This causes the console to halt once userland takes over as no
interrupts are received. The actual address and irq can be read from
ACPI, kettenis@ already added support for arm64 which paved the way for
amd64.

Some consoles that previously attached over isa are now expected to
attach over acpi.

Thanks to patrick@ for testing on arm64.

ok kettenis@


# 1.402 07-Dec-2021 kettenis

Fix parsing of SR_IRQ resource descriptors.

ok patrick@, anton@


# 1.401 02-Nov-2021 deraadt

knf


# 1.400 30-Oct-2021 deraadt

Change hw.perfpolicy=auto by default, at startup. If the system has AC
power connected (default is yes when no driver differentiates) then default
to 100% performance. On battery, use the existing auto algorithm (which is
admittedly somewhat unrefined).
This change overrides the system/BIOS speed and puts OpenBSD in control.
As this happens very early during boot, besides speedups in all usage usage
patterns, some surprises: unhibernate and sysupgrade times are cut in half.
note: on a few architectures, the setperf fn pointer is changed late, and
thus the auto algorithm stops timeing out. kettenis and i will look for
a solution.
in snaps for more than a week.
ok kettenis


Revision tags: OPENBSD_7_0_BASE
# 1.399 20-Jul-2021 mlarkin

Fix trailing whitespace in a few places.

No code change.


# 1.398 09-May-2021 kettenis

Some x86 machines advertise the "hardware reduced" ACPI feature, advertise
S4 and S5 support, but fail to populate the SLEEP_CONTROL_REG and
SLEEP_STATUS_REG descriptions in the FADT. An example of such a machine
is the ASUS Zenbook 14 UM433DA. Any attempt to powerdown the machine
will result in a kernel crash. It turns out that using the legacy ACPI PM
registers works fine on this machien. So fall back on those registers
if SLEEP_CONTROL_REG and/or SLEEP_STATUS_REG aren't provided.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_9_BASE
# 1.397 15-Mar-2021 patrick

Add acpi_iommu_device_map(), which replaces the DMA tag with one that
is blessed with IOMMU magic, if available. This is mainly for arm64,
since on amd64 and i386 the IOMMU only captures PCIe devices, as far
as I know, which uses the pci_probe_device_hook(). This though is for
non-PCI devices attached through ACPI.

ok kettenis@


# 1.396 07-Mar-2021 patrick

Pass standard DMA tag to acpi(4) table drivers.

ok kettenis@


# 1.395 27-Dec-2020 deraadt

acpi_map_address() cannot be wrapped by SMALL_KERNEL anymore, as
it is used by acpihpet.c


# 1.394 25-Dec-2020 visa

Refactor klist insertion and removal

Rename klist_{insert,remove}() to klist_{insert,remove}_locked().
These functions assume that the caller has locked the klist. The current
state of locking remains intact because the kernel lock is still used
with all klists.

Add new functions klist_insert() and klist_remove() that lock the klist
internally. This allows some code simplification.

OK mpi@


# 1.393 17-Dec-2020 kettenis

Fix some issues with referencing named ACPI nodes from Packages.
These references need to be resolved at runtime rather than when
they're parsed such that they pick up the right values for those nodes
which can be changed when for example _INI methods run.

The current approach is to replace these reference with a string that
names the node in question. The problem with that is that packages
can also contain normal strings. Which means that we need special
code that depends on the context where the package is used.

This diff takes a different approach by retaining a reference when
parsing. Code that uses a package will need to resolve this reference
but there is no ambiguiety anymore.

ok patrick@


# 1.392 05-Dec-2020 kettenis

Add support for the _EVT method to the GPIO support code.

ok patrick@


Revision tags: OPENBSD_6_8_BASE
# 1.391 27-Aug-2020 jmatthew

Reorder the acpi attach process so we look at Processor() CPU nodes
after Device() ones, since we should prefer the newer node type.
If we see any Device() nodes, don't attach acpicpu(4) to any Processor()
nodes if they're also present. This also makes acpitz(4) and
acpipwrres(4) devices attach slightly later.

ok kettenis@, also tested by jmc@


# 1.390 16-Aug-2020 gnezdo

Remove sc_maxgpe from acpi_softc which was never read from

Remove a dead conditional from acpi_init_gpes while there.

ok jcs@


# 1.389 21-Jul-2020 deraadt

acpi can use IPL_BIO (a low interrupt) since it only enqueues operations for
later processing. The use of a high interrupt will predate suspend/resume
efforts, we had to redesign acpi to be non-reentrant obviously
discussed with kettenis, in snaps for more than a week


# 1.388 24-Jun-2020 cheloha

kernel: use gettime(9)/getuptime(9) in lieu of time_second(9)/time_uptime(9)

time_second(9) and time_uptime(9) are widely used in the kernel to
quickly get the system UTC or system uptime as a time_t. However,
time_t is 64-bit everywhere, so it is not generally safe to use them
on 32-bit platforms: you have a split-read problem if your hardware
cannot perform atomic 64-bit reads.

This patch replaces time_second(9) with gettime(9), a safer successor
interface, throughout the kernel. Similarly, time_uptime(9) is replaced
with getuptime(9).

There is a performance cost on 32-bit platforms in exchange for
eliminating the split-read problem: instead of two register reads you
now have a lockless read loop to pull the values from the timehands.
This is really not *too* bad in the grand scheme of things, but
compared to what we were doing before it is several times slower.

There is no performance cost on 64-bit (__LP64__) platforms.

With input from visa@, dlg@, and tedu@.

Several bugs squashed by visa@.

ok kettenis@


# 1.387 10-Jun-2020 jca

Give userland a consistent battery and A/C state after resume

Previously we refreshed the state of acpiac(4) and acpibat(4)/acpisbs(4)
by queueing fake ACPI events to be processed by the acpi thread.
acpibat(4) using ACPIDEV_POLL meant that its status could be refreshed
first, resulting in an APM_POWER_CHANGE event sent to userland before
the state of acpiac(4) was up to date. Because of this, apmd(8) could
see A/C unplugged when it was in fact plugged, possibly leading to
a wrong decision.

Instead of using fake ACPI events, refresh the status of acpiac(4) and
acpibat(4)/acpisbs(4) right away at DVACT_WAKEUP time. To reuse the
existing refresh code of those drivers, make their ACPI *_notify()
handlers responsible for sending APM_POWER_CHANGE events instead. Also
queue an APM_POWER_CHANGE event right after resume to retain existing
behavior.

This fixes the consistency of data shown to userland, and makes those
drivers less special.

Initially discussed last year (!) with mpi@ at Miod's place.
Feedback and ok jcs@, deraadt@ agrees with the use of DVACT_WAKEUP here.


# 1.386 29-May-2020 deraadt

dev/rndvar.h no longer has statistical interfaces (removed during various
conversion steps). it only contains kernel prototypes for 4 interfaces,
all of which legitimately belong in sys/systm.h, which are already included
by all enqueue_randomness() users.


# 1.385 14-May-2020 kettenis

Bring back the acpi(4) changes. This time around only use the ACPI _CRS
information on ACPI 5.0 and later.

ok krw@, patrick@


# 1.384 11-May-2020 jca

More accurate remaining power reporting with disparate batteries

Overall remaining power was computed as the average of the remaining
power of each battery, in percents. This doesn't work with batteries of
different capacity, so use the total capacity and remaining power
instead.

Reported by lidstah, successful tests by lidstah and benno@, ok kettenis@


# 1.383 08-May-2020 kettenis

Move parsing of _CRS into acpi(4) and pass the parsed address and interrupt
information in struct acpi_attach_args.

ok mpi@


Revision tags: OPENBSD_6_7_BASE
# 1.382 14-Apr-2020 kettenis

Implement acpi_getprop(), the ACPI equivalent of OF_getprop().

ok patrick@


# 1.381 12-Apr-2020 kettenis

Add support for the _CCA method. This method indicates whether DMA is
cache-coherent or not. To implement this, acpi(4) gets two bus_dma tags
and passes the appropriate one when attaching devices based on _CCA.
On i386/amd64, where for all practical purpose DMA is always cache-coherent,
the two tags are the same. But on arm64 they are distinct.

ok patrick@


# 1.380 07-Apr-2020 visa

Abstract the head of knote lists. This allows extending the lists,
for example, with locking assertions.

OK mpi@, anton@


# 1.379 03-Apr-2020 mpi

Fix incoherencies of errors returned by various *kqfilter routines.

EOPNOTSUPP is returned when there's no "filterops" for a given fd.
EINVAL when the requested filter isn't supported by the "filterops".
ENXIO means the underlying device is no longer valid/present.

ok visa@


# 1.378 20-Feb-2020 visa

Replace field f_isfd with field f_flags in struct filterops to allow
adding more filter properties without cluttering the struct.

OK mpi@, anton@


# 1.377 31-Dec-2019 visa

Use C99 designated initializers with struct filterops. In addition,
make the structs const so that the data are put in .rodata.

OK mpi@, deraadt@, anton@, bluhm@


# 1.376 31-Dec-2019 mpi

Convert infinite sleeps to {m,t}sleep_nsec(9).

ok kn@


# 1.375 31-Dec-2019 jsg

include sys/sensors.h for sensor use
don't depend on indirect inclusion from acpidev.h


Revision tags: OPENBSD_6_6_BASE
# 1.374 07-Sep-2019 kettenis

Revert acpipci(4) changes. Too many systems are broken. We'll try again
after 6.6 as been released. The acpireg.h change stays behind.


# 1.373 28-Aug-2019 kettenis

Use ACPI information to attach PCI busses like we do on arm64. There are a
few additional quirks though, and attaching the PCI busses is delayed to
replicate the existing code more closely. That may be changed in the
future. Also tweak how we handle MSI support and respect to ACPI flag
that says we shouldn't attempt to use MSIs.

Some fallout is expected.

ok patrick@


# 1.372 27-Aug-2019 deraadt

On amd64 the bootup and resume paths for SP/BP/SP were not performing
msr/pat, clock-startup, firmware upgrade, fpu initialization, and cpu
soft-state initialization in the same order. That kind of chaos is
crazy and fragile. Improve the acpi resume call sequence and unify order
of the operations.


# 1.371 02-Jul-2019 jcs

acpi_set_gpehandler: turn edge arg into flags, add support for
passing GPE_DIRECT which directly executes the supplied callback and
clears a level interrupt rather than going through acpi_taskq which
can be slow when other driver callbacks are in the queue.

This should not be used for GPE handlers that need to do anything
ACPI-related.

ok kettenis


# 1.370 10-Jun-2019 kettenis

Print proper ACPI version number.

ok mlarkin@


# 1.369 08-Jun-2019 kettenis

When establishing the mapping between ACPI device nodes and PCI devices,
skip PCI host bridges and devices that aren't there.

ok patrick@


# 1.368 07-Jun-2019 kettenis

Introduce acpi_getsta() and use it to eliminate some duplicated code.

ok patrick@


# 1.367 12-May-2019 kettenis

Make sure the extended PM register descriptions actually exist before
using them. Fixes machines with ancient ACPI 1.0 (such as recent
hypervisors).

ok deraadt@


# 1.366 12-May-2019 kettenis

Add support for mapping ACPI PM registers using the "extended" gasio
representation from the FADT. Mostly fixes the Lanner NCA-1510.

ok lteo@, mlarkin@, deraadt@


# 1.365 11-May-2019 lteo

Fix incorrect error message.

ok kettenis@


# 1.364 08-May-2019 tedu

free size. the allocation is the header plus the length.


Revision tags: OPENBSD_6_5_BASE
# 1.363 04-Apr-2019 kettenis

Avoid potential null-pointer dereference. Found by Kent R. Spillner.

ok deraadt@, jsg@


# 1.362 20-Jan-2019 jsg

fix SMALL_KERNEL build after changes in rev 1.361
reported by naddy@ ok deraadt@


# 1.361 19-Jan-2019 tedu

Add a pwraction sysctl that controls what the power button does on acpi.
By default, nothing changes -- shutdown is initiated. But allows turning
power button into a sleep button if desired.
(grudging) ok from a few parties


# 1.360 26-Oct-2018 kettenis

Add acpipci(4) on amd64. For now this only calls the PCI-specific _OSC
method to let the ACPI implementation know what features we support.


Revision tags: OPENBSD_6_4_BASE
# 1.359 30-Aug-2018 kettenis

Move softbutton check before the check that skips attaching certain acpi
devices. Fixes regression caused by matching on _CID in addition to
matching on _HID.

ok matthieu@, mlarkin@


# 1.358 25-Aug-2018 kettenis

Match ACPI devices based on _CID if no match for _HID is found.

ok mpi@


# 1.357 19-Aug-2018 kettenis

Add support for multiple PCI segments. Only really implemented for arm64
for now as amd64/i386 firmware still caters for legacy OSes that only
support a single PCI segment.

ok patrick@


# 1.356 03-Aug-2018 kettenis

Let ahci(4) match on _CLS instead of _HID when attaching at acpi(4). Avoids
having to add many more _HID entries to the match table.

ok deraadt@, mlarkin@


# 1.355 10-Jul-2018 kettenis

Make legacy interrupts work in acpipci(4).

ok patrick@


# 1.354 05-Jul-2018 kettenis

Add acpipci(4), a driver that supports generic ECAM-compatible PCI host
bridges based on information provided by ACPI.

ok mlarkin@


# 1.353 04-Jul-2018 kettenis

Properly pass around the PCI "chipset tag" in acpi(4) and refactor
acpimcfg(4) to call an MD initialization functions that sets up a tag for
PCI ECAM.

ok guenther@, mlarkin@, krw@


# 1.352 01-Jul-2018 kettenis

Move acpi(4) attach glue into acpi_machdep.c.

ok guenther@, deraadt@


# 1.351 01-Jul-2018 kettenis

Add support for _DSD Device Properties.

ok mlarkin@


# 1.350 30-Jun-2018 kettenis

Add intr_enable() function, intended for MI use to amd64 and i386 and use
this in the acpi(4) suspend/resume code paths.

ok deraadt@


# 1.349 29-Jun-2018 kettenis

Use <stdint.h> types.

ok mpi@, jung@, krw@, deraadt@


# 1.348 29-Jun-2018 mlarkin

ACPI: Allow (with warning) GPE handler reassignment, instead of returning
a failure code.

ok kettenis


# 1.347 26-Jun-2018 mlarkin

knf


# 1.346 26-Jun-2018 kettenis

Let acpi(4) pass down the bus dma tag.

ok deraadt@


# 1.345 25-Jun-2018 kettenis

Reorganize acpi(4) code a bit in preparation for upcoming arm64 support.

ok deraadt@


# 1.344 20-May-2018 kettenis

Wake up the acpi thread after scheduling a gpio event.

ok mpi@


# 1.343 17-May-2018 kettenis

Sprinkle some #ifndef SMALL_KERNEL such that RAMDISK kernels cuild again.

pointed out by stsp@


# 1.342 17-May-2018 kettenis

Implement GenericSerialBus OpRegion support.

ok mlarkin@


# 1.341 27-Mar-2018 kettenis

Add acpicmos(4), a driver that implements SystemCMOS OperationRegion
access support. This fixes machines where the AML doesn't check whether
support for this OperationRegion type has been registered by the OS.

ok mlarkin@


Revision tags: OPENBSD_6_3_BASE
# 1.340 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.339 10-Feb-2018 deraadt

Syncronize filesystems to disk when suspending. Each mountpoint's vnodes
are pushed to disk. Dangling vnodes (unlinked files still in use) and
vnodes undergoing change by long-running syscalls are identified -- and
such filesystems are marked dirty on-disk while we are suspended (in case
power is lost, a fsck will be required). Filesystems without dangling or
busy vnodes are marked clean, resulting in faster boots following
"battery died" circumstances.
Tested by numerous developers, thanks for the feedback.


# 1.338 08-Feb-2018 deraadt

After flushing disks, try again to release all memory. We don't need to
save dirty memory to the hibernate space.


# 1.337 08-Feb-2018 deraadt

Toss all releaseable memory, because fragmentation can get in the way
of allocating the hibernate playpen.


# 1.336 08-Feb-2018 deraadt

sr_quiesce() is a new approach for ensuring that softraid drains
output to the disks.

This is part of a larger suspend/resume filesystem-safety diff, which
has been worked on for a couple of months already. Tests by job, krw,
beck, benno, and others. Sometimes even by snapshot users...


# 1.335 29-Nov-2017 kettenis

Add support for GPIO-signaled events.

ok mlarkin@


# 1.334 16-Nov-2017 jcs

Add PCI attachment for dwiic(4) needed by Intel 100 Series machines

ok kettenis


# 1.333 14-Nov-2017 kettenis

Add a few more PNP IDs to the skip lists.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.332 17-Aug-2017 stsp

Make dwiic(4) attach its ACPI parent dependencies.
This change makes the Asus E200HA keyboard work.

Original analysis and patch by Cesare Gargano
ok kettenis@


# 1.331 09-Aug-2017 dcoppa

Add "PNP0303" (8042 PS/2 Controller) to acpi_isa_hids[]

ok kettenis@


# 1.330 22-Jul-2017 jcs

acpibat: look for _BIX first before falling back on _BIF

ACPI 4.0 deprecated _BIF for battery status so some newer machines
are exporting _BIX which extends _BIF's package with a few extra
fields. When using _BIX, export the new discharge cycle count as a
sensor.

feedback from kettenis and anton
ok deraadt


# 1.329 20-Jul-2017 kettenis

Start a list of isa(4) device names such that we can avoid printing them
as "not configured".

ok mikeb@, millert@, deraadt@


# 1.328 08-Apr-2017 deraadt

Sensors are run as callbacks inside tasks. During suspend a sensor
could be running inside a driver that will be force-detached, or due
to tsleep end up disrupting the softstate/hardstate contract. At
suspend time, quisce all these callbacks by waiting for completion.
This issue has never been observed for real, but may be implicated in
suspend/resume failures.
ok kettenis guenther mlarkin


# 1.327 08-Apr-2017 deraadt

Two prototypes for acpi_maptable; merge to one place


Revision tags: OPENBSD_6_1_BASE
# 1.326 27-Mar-2017 deraadt

Now that hibernate_alloc() only has clean success/failure, don't
need to call hibernate_free() to clean up a partial mess.
ok mlarkin kettenis


# 1.325 13-Mar-2017 deraadt

allow ramdisks to build; ok jcs


# 1.324 12-Mar-2017 jcs

try to attach acpisbs first and if successful, don't attach acpibat


# 1.323 02-Mar-2017 natano

Add a new sysctl machdep.lidaction. The sysctl works as follows:

machdep.lidaction=0 # do nothing
machdep.lidaction=1 # suspend
machdep.lidaction=2 # hibernate

lidsuspend is just an alias for lidaction, so if you change one, the
other one will have the same value. The plan is to remove
machdep.lidsuspend eventually when people have upgraded their
/ets/sysctl.conf.

discussed with deraadt, who came up with the new MIB name
no objections mlarkin
ok stsp halex jcs


# 1.322 28-Feb-2017 natano

Untangle abstract sleep modes from ACPI sleep states. This paves the way
for further work in the area.

idea and ok deraadt
ok mlarkin


# 1.321 22-Feb-2017 jcs

add acpisbs, an acpi smart battery subsystem driver reading data
over smbus

currently disabled because it conflicts with acpibat

ok deraadt, kettenis


# 1.320 14-Jan-2017 kettenis

Revert aml_rdpciaddr changes; breaks several machines that were working
before.


# 1.319 11-Jan-2017 jsg

Increase the size of the buffers used for _HID and _CID strings to be
able to fit "Hyper_V_Gen_Counter_V1".

ok mlarkin@ deraadt@


# 1.318 08-Jan-2017 kettenis

Make aml_rdpciaddr() work for for devices behind bridges. The code now
uses the mapping between AML nodes and PCI devices that we establish.
Because _INIT methods may end up calling aml_rdpciaddr(), make sure we
create that mapping early. Also handle devices that aren't actually present.
These devices are now included in the mapping and reads will return an
all-ones patterm whereas writes are a no-op..

ok mlarkin@


# 1.317 25-Oct-2016 pirofti

Add the resource index to aml_parse_resource() callback function.

This is needed by an upcoming acpiec commit that handles machines breaking
the current ACPI specifications.

Change suggested by and ok kettenis@, guenther@


# 1.316 18-Sep-2016 guenther

If the APIC table (MADT) isn't present then ci_acpi_proc_id won't be set
when acpicpu tries to attach, leading to a NULL dereference. The ACPI
processor object on the Dells where this was reported is bogus and useless
anyway so apply the same test when deciding whether to attach to processor
objects as acpicpu would when matching them to cpus.

Problem reported by Eivind Eide (xenofil (at) gmail.com) and
Johan Huldtgren (johan+openbsd-bugs (at) huldtgren.com).
Testing by Eivind Eide.
ok kettenis@ mlarkin@


# 1.315 03-Sep-2016 naddy

Write the system time back to the RTC every 30 minutes.
This fixes the problem that long-running machines which were not
shut down properly would reboot with a badly offset system time.

hints and ok kettenis@


# 1.314 31-Aug-2016 mlarkin

Don't put a device in the wake device list if _STA says it's not there.

Fixes at least one machine that woke up from sleep / poweroff immediately.

ok kettenis@, tested by kettenis and martijn@


# 1.313 28-Jul-2016 kettenis

Store the acpi processor ID/UID in struct cpu_info, and use it to attach
acpicpu(4) drivers to the right cpu(4).

ok mlarkin@, guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.312 10-Jun-2016 kettenis

The power management timer has been made optional in ACPI 5.0A, so don't
attempt to attach acpitimer(4) if the timer isn't present.

Pointed out by jcs@

ok jcs@


# 1.311 08-May-2016 kettenis

Make sure devices listed by a device's _DEP method are attached before
we attach the device itself. The _DEP methode, introduced in ACPI 5.0,
is a hint that the device in question depends on OpRegion support from
the devices listed by _DEP, so we have to order them the proper way.
To prevent us from attaching those devices again when we encounter them
later walking down the device tree, keep track of our attempts to
attach a device driver to them using aflag in the aml_node structure.

ok guenther@


# 1.310 25-Apr-2016 pirofti

We attach acpihpet(4) and acpiec(4) without using the HID, so do not
report it in the dmesg.

OK deraadt@, mlarkin@.


# 1.309 02-Apr-2016 kettenis

Reduce dmesg spam by knocking out some acpi hardware IDs that don't need a
driver.

ok mpi@


# 1.308 29-Mar-2016 kettenis

Check if a device is present (using _STA) in acpi_foundhid. This prevents
us from reporting devices as "not configured" that aren't actually present,
and allows us to remove duplicated code from several drivers.

ok deraadt@, ok jsg@


# 1.307 29-Mar-2016 kettenis

Implement a cleaner way to prevent acpivout(4) from attaching if we have
a platform-specific method of controlling the backlight.

ok millert@, mlarkin@, jung@


# 1.306 28-Mar-2016 kettenis

Adding each and every new acpi device driver to acpi_foundhid() is
gettinga bit of the burden. And it really isn't how our autoconf
framework is supposed to work for busses that can be enumerated. All
the drivers already check for a matching _HID in their attach
function. So we can just drop the checks in acpi_foundhid() and let
the drivers sort themselves out.

Print a "not configured" line for devices for which we don't attach adriver.
This may be a bit much as there are devices for which we will not have a
driver. This will be removed if it causes too much dmesg spam.

ok deraadt@, mlarkin@


Revision tags: OPENBSD_5_9_BASE
# 1.305 17-Jan-2016 jsg

Take acpi_foundhid() outside of #ifndef SMALL_KERNEL so the ramdisk
kernel can find and attach dwiic(4) and sdhc(4).


# 1.304 17-Jan-2016 jsg

remove an unused global variable


# 1.303 14-Jan-2016 kettenis

Attach dwiic(4) on the Intel Bay Trail i2c controllers as well.


# 1.302 12-Jan-2016 kettenis

Improve emulation of PM registers on Hardware-redcued ACPI by emulating
SPL_TYPx, SLP_EN and WAK_STS using the new SLEEP_STATUS_REG and
SLEEP_CONTROL_REG registers. Makes the transition into S5 (powerdown) work
on the ASUS X205TA.

ok deraadt@, mlarkin@


# 1.301 12-Jan-2016 jcs

Add dwiic, a driver for the Synopsys DesignWare i2c controller found
on the Samsung ATIV Book 9 laptop. This initial version only
supports ACPI config/attachment.

Add ihidev, a HID-over-i2c driver largely based on uhidev. dwiic
handles attaching ihidev devices found in ACPI.

Add ims, a HID-over-i2c mouse/trackpad driver to get basic cursor
and button functionality from HID-compliant i2c trackpads.

ok kettenis deraadt


# 1.300 11-Jan-2016 kettenis

Make sdhc(4) attach to hardware IDs "80860F14" and "PNP0FFF".


# 1.299 09-Jan-2016 kettenis

Initial support for Hardware-Reduced ACPI. This skips the legacy mode checks
by emulating the PM_CNT register such that SCI_EN is always enabled. It also
skip attaching acpitimer(4) since the relevant registers are no longer there.

ok deraadt@, mlarkin@


# 1.298 06-Jan-2016 kettenis

Follow the spec and check the SCI_EN bit to see if ACPI has already
been enabled and don't bother enabling it again if it is. This should fix
systems that don't implement legacy mode and therefore set ACPI_ENABLE and
ACPI_DISABLE to zero.

ok mlarkin@, yasuoka@


# 1.297 23-Nov-2015 reyk

Back out the CRITICAL change, turn it back from 10% to 25%. Some
people with old/broken batteries didn't like this change, and the
underlying heuristics are wrong.


# 1.296 22-Nov-2015 reyk

The "CRITICAL" APM battery level, that is exported/faked by the ACPI
stack, is currently set to 25%. With 25% battery left, a typical
modern laptop will have at least one hour runtime left. It is an
arbitrary value, and other APM power layers in the kernel set CRITICAL
to something like 5% or 10%, so set it to 10% for now.

OK mlarkin@


# 1.295 28-Sep-2015 deraadt

In low-level suspend routines, set cold=2. In tsleep(), use this to
spit out a ddb trace to console. This should allow us to find suspend
or resume routines which break the rules. It depends on the console
output function being non-sleeping.... but that's another codepath which
should try to be safe when cold is set.
ok kettenis


# 1.294 06-Sep-2015 deraadt

clarify malloc arguments (base + length...)


# 1.293 28-Aug-2015 deraadt

fairly simple sizes for free(); ok tedu


# 1.292 20-Aug-2015 kettenis

Respect the access size when reading or writing to pci config space.
This version also makes sure writes are properly aligned, preventing
the panics seen with the previous incarnation of this change.

Fixes battery status passthrough in vmware, brightness keys on my x220
and who knows what else.

ok mlarkin@, deraadt@


Revision tags: OPENBSD_5_8_BASE
# 1.291 04-Aug-2015 deraadt

If we are setting ACPI_PM1_BM_RLD do it only once, and remember to
repeat this action at resume time.
ok mlarkin guenther


# 1.290 29-Jul-2015 deraadt

Something subtle in previous revision is locking up suspend or booting
on some laptops. Mailing lists threads mention x220 and Asus machines.
Reverting for now.


# 1.289 21-Jul-2015 kettenis

iRespect the access size when reading or writing to pci config space.
Fixes battery status passthrough in vmware and who knows what else.

Tested by many.

ok mlarkin@, deraadt@


# 1.288 17-Jul-2015 mlarkin

fix comments


# 1.287 26-Mar-2015 jsg

Use the 0x10 extended capability to determine if the trackstick
buttons are wired to the trackpad and need to be re-routed to the
trackstick. Without this change the buttons on 2015 thinkpads
get picked up as extended buttons that show up as scroll up/down.

Remove the X1 Carbon 2015 (LEN0048) and X250 (LEN0046) from the
top button area/soft buttons quirks list. Also avoid using the
quirk list entirely if the capability bit is set.

Based on work from jcs@ with feedback from mpi@
ok mpi@ jcs@


# 1.286 20-Mar-2015 kettenis

The ACPI standard says that a PCI function number of FFFF can be used to refer
to all functions on a device. This makes no sense in the context of accessing
PCI config space. Nevertheless stsp@ has a machine with AML that tries to
do config space reads from a device where _ADR encodes function number FFFF.
This is almost certainly buggy AML, but since Linux (and presumably Windows)
boot just fine on this machine we need to work around this. Linux effectively
treats function FFFF as function 7, which isn't present on this machine.
Instead we simulate a read from a non-existent device by returning a bit
pattern of all ones. Writes to function FFFF will still panic.

ok mlarkin@


# 1.285 16-Mar-2015 deraadt

Reduce use of SMALL_KERNEL in acpi, bringing acpiec driver and more gpe
handling into RAMDISK. This is now possible because the install media
has ample room. The goal is to reduce special cases where we may be
skipping (unknown) important operations...
ok mlarkin kettenis


# 1.284 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.283 07-Feb-2015 deraadt

New framework that allows hibernate to pass in entropy from it's fresh
boot.
ok mlarkin


# 1.282 06-Feb-2015 mlarkin

Fix a hibernate crash on some machines due to unmapping a page that
may not have been mapped previously (in the failure to hibernate case).

Also ensure that the lowmem ptp is mapped in all cases (not just MP).

ok kettenis


# 1.281 17-Jan-2015 deraadt

resume_randomness() before spinning up other cpus...


# 1.280 15-Jan-2015 jsg

Define a new wscons mouse type for Synaptics clickpad devices that lack
physical buttons. This will be used if the acpi pnpid for the mouse
matches a list provided by Synaptics found in Linux.

Combined with the xenocara changes this will remove the need for an xorg
config file for the x240/t440/t540 generation of thinkpads.

ok matthieu@ shadchin@ kettenis@


# 1.279 11-Jan-2015 kettenis

Tolerate checksum errors in ACPI tables (but complain about them). Apparently
there are machines out there with broken BIOSen with such checksum errors in
thier RSDT tables. Windows runs fine on these machines, so it probably does
not check. Linux ignores checksum errors as well (and prints a warning).

ok deraadt@, krw@


# 1.278 18-Dec-2014 deraadt

suspend and resume the RNG (at what looks like the right spots)
ok reyk djm


# 1.277 09-Dec-2014 doug

Replace some malloc(n*size,...) calls with mallocarray().

ok tedu@ deraadt@


# 1.276 06-Dec-2014 mpi

If "machdep.lidsuspend" is not set, do not go back to sleep if the lid
is still closed when resuming. This allows docked laptop to be resumed
without opening them.

ok mlarkin@


# 1.275 23-Nov-2014 mlarkin

If a machine has no _LIDs, then none can be open. This diff fixes a problem
where machines without any _LIDs were going back to sleep after hibernate
due to the recent suspend-if-lid-closed-on-resume diff. Noticed on qemu,
which doesn't have any _LIDs.

discussed with deraadt


# 1.274 18-Nov-2014 krw

Nuke yet more obvious #include duplications.

ok miod@


# 1.273 08-Nov-2014 mlarkin

If resuming from sleep (zzz/ZZZ) and the lid is still closed, go back to
sleep. This prevents accidental lid flex or slight opening in a backpack
from waking the machine up and leaving it resumed (powered on).

ok deraadt@


# 1.272 26-Sep-2014 kettenis

Rework piglet and pig allocation. Currently the piglet gets allocated
deep down in the suspend path, where it is really hard to recover from
allocation failure. So allocate the piglet early on in the suspend path.
Also change the piglet and piglet allocation functions to use km_alloc(9)
instead of doing pmemrange magic. This removes a bunch of code which, in the
case of the piglet allocation, is broken since it results in a NULL pointer
dereference. Also switch the piglet allocation to not wait. If we can't
allocate 16MB of phys contig memory on a halfway modern machine we're almost
certainly under a lot of memory pressure and we're better off not trying to
hibernate anyway.

ok mlarkin@


# 1.271 25-Sep-2014 kettenis

Bring back rev 1.249. Now that mp_setperf() has been fixed, it should be
safe to restore perflevel when resuming. Should fix the issues people
see with the processor running at full speed heating up the machine while
the apm command reports that the CPUs are running at a low speed.

ok dcoppa@, deraadt@


# 1.270 23-Sep-2014 kettenis

Release the acpi lock around calls to wsdisplay_suspend() and
wsdisplay_resume(). The X server may adjust the display brightness just
before/after the VT switch when suspending/resuming. In fact, it may
already be blocked adjusting the display brightness for some reason
unrelated to the suspend request.

The VT switch request will eventually time out, but the delay is annoying
and the failed VT switch will lead to problems after resume.

Needed for a future update of the xf86-video-intel driver.

ok mlarkin@


# 1.269 19-Sep-2014 kettenis

Use config_suspend_all(9).


# 1.268 08-Sep-2014 kettenis

Clear the PME Status bit on PCI devices in response to Device Wake events.
Fixes the interrupt storm on the 2009 Mac mini with WOL enabled on its
nfe(4) interface.

ok mlarkin@


Revision tags: OPENBSD_5_6_BASE
# 1.267 20-Jul-2014 mlarkin

Support hibernating to softraid crypto volumes.

much help and ok from deraadt@


# 1.266 16-Jul-2014 mlarkin

Reenable hibernate RLE support and flush+zero all memory after unpack.


# 1.265 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.264 12-Jul-2014 kettenis

Add a function to drop all clean pages on the page daemon queues and call
it when we hibernate.

ok mlarkin@, miod@, deraadt@


# 1.263 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.262 11-Jul-2014 mlarkin

Flush the buffercache to 16MB on hibernate and restore its previous max
size (kern.bufcachepercent) on resume, for better hibernate performance.

ok beck@


# 1.261 10-Jul-2014 blambert

remove unneeded header

ok mlarkin@


# 1.260 01-Jul-2014 kettenis

Adding an additonal delay after each access to SystemMemory or SystemIO
during autoconf makes no sense.

ok deraadt@


# 1.259 23-Jun-2014 kettenis

SystemMemory is used to access memory mapped registers on some machines, so we
must use the bus_space(9) API to access it instead of memcpy(9). Also make
sure we properly align access to fields that need more than byte alignment.

ok guenther@


# 1.258 21-May-2014 mlarkin

Fix invalid / missing format specifiers in several dnprintf statements
(used only when ACPI_DEBUG is enabled)

From a diff on tech@ by Fabian Raetz, with a minor change by me, tested
on i386 and amd64 with ACPI_DEBUG enabled and disabled.


# 1.257 25-Apr-2014 mlarkin

Defer the free of the hibernate structures until later, when it is safe to
do so.

Freeing the hibernate structures too early leads to inadvertent calls into
splx when we aren't ready for them yet. Found when trapping splx calls in
the resume path.

discussed with deraadt@ at m2k14


# 1.256 13-Apr-2014 mlarkin

Fix a badly worded debug message that implied the entire machine was going
to sleep, instead of just the acpi thread going to sleep.


# 1.255 31-Mar-2014 mpi

Change the order of operations during a suspend/resume cycle and
call bufq_quiesce() after executing the DVACT_QUIESCE handlers.

This should be safe since no disk nor controller drivers have such
handler but it will allow us to detach sd(4) devices attached to a
USB bus.

Another benefit pointed out by kettenis@ is that drivers that need
to read a firmware from the disk should be able to do it at resume
time in a DVACT_WAKEUP handler.

ok kettenis@, deraadt@


# 1.254 13-Mar-2014 dlg

get rid of the assumption that the head of the alldevs list is the
"mainbus" device. this breaks when mpath is enabled because it
attaches before mainbus and therefore takes the head position.

have autoconf provide device_mainbus() which looks up mainbus_cd,
and use that instead.

discussed with deraadt who just wants mpath stuff to move forward
despite there being many ways to shine this particular turd.


Revision tags: OPENBSD_5_5_BASE
# 1.253 21-Feb-2014 deraadt

Do the lid control further on in the resume sequence, since this is
AML, and will do WAITOK operations. While cold, that is a bad idea.
We seem to be safe with respect to the spec, I do not see any event
debounce issues here.
ok kettenis, tested by many


# 1.252 20-Jan-2014 claudio

Instead of checking sc->sc_revision <= 1 check that the
fadt->hdr_revision <= 1. This is a better check since it checks
the version of the proper descriptor. This allows the alix APU
to reboot since those boards have currently a wrong sc->sc_revision.
Committing this now so that it can be tested easier.
With and OK jsg@ also fine by derradt@


# 1.251 24-Dec-2013 kettenis

If the FADT has its SMI_CMD set to zero, assume we're only ACPI-only hardware
and don't need to disable SMI ownership of the ACPI hardware registers.

ok mlarkin@


# 1.250 23-Dec-2013 kettenis

Revert previous commit. It makes resume hang on some MP systems running
GENERIC.MP.


# 1.249 16-Dec-2013 kettenis

Restore hw.setperf upon resume like we do for apm(4) on i386. Seems we need
to do this fairly late in the resume to avoid hangs.

ok deraadt@, mlarkin@


# 1.248 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


# 1.247 06-Nov-2013 mpi

Add support for Power Resources for Dx states and the necessary hook
for PCI devices. This hook should be called twice, before and after
changing the power state of a PCI device.

Before setting the device to the new state, the ACPI layer will notify
every power resources linked to the device for that state and make sure
they are turned "_ON". After changing the state of the device, it will
decrement the reference of every power resources linked to that device
for the old state and turn them "_OFF" if they are no longer referenced.

This fixes the no-USB after resume problem seen on various ThinkPad,
problem initialy diagnosed with Alexander Polakov.

ok kettenis@, deraadt@


Revision tags: OPENBSD_5_4_BASE
# 1.246 01-Jun-2013 mlarkin

Stop printing ACPI wakeup devices in dmesg after the 16th wakeup device.

Helps with VMware VMs that report hundreds of wakeup devices.

ok reyk@, deraadt@


# 1.245 31-May-2013 bluhm

Fix the build for a kernel without wd and pciide in its config.
OK deraadt@ tedu@


# 1.244 30-May-2013 deraadt

Enforce ca_activate tree-walks over the entire heirarchy for all events,
cleaning up some shutdown-hook related code on the way.
(A few drivers related to sparc are still skipped at kettenis' request)
ok kettenis mlarkin, tested by many others too


# 1.243 18-Apr-2013 deraadt

pin the acpi thread to the BSP, because AML and SMI cannot be trusted. I
was certain we were doing this already, but it turns out no...


Revision tags: OPENBSD_5_3_BASE
# 1.242 24-Dec-2012 guenther

In acpiioctl(), Return though the splx() path if suser() returns an error

ok deraadt@


# 1.241 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


# 1.240 04-Oct-2012 ehrhardt

Use information provided by ACPI to attach secondary PCI host bridges.

ok kettenis@


# 1.239 07-Sep-2012 kettenis

Add a new function called acpi_pci_min_powerstate() that returns the lowest
power state for a PCI device for the power state (currently S3 or S4) we're
transitioning into. This function is probably too simplistic; there is a lot
of (silly) complexity in the ACPI spec.

ok deraadt@


Revision tags: OPENBSD_5_2_BASE
# 1.238 13-Jul-2012 pirofti

Disable acpivideo(4) on ASUS laptops.

There are some models that panic whenever an event is received.
Some cope, some don't. In order to get this right acpiec(4) requires
major rework. Until then let machines boot.

Discussed with deraadt@.


# 1.237 11-Jul-2012 mlarkin

for now, restrict hibernate to root in the acpi ioctl kernel interface.
normal rules for controlling this interface through apmd still apply.

ok deraadt@


# 1.236 09-Jul-2012 deraadt

tiny little oops in untested hibernate block


# 1.235 09-Jul-2012 deraadt

clean up ioctl() case statements


# 1.234 09-Jul-2012 deraadt

acpi_disable_onegpe() is not used; ok pirofti


# 1.233 24-May-2012 kettenis

If _MIN > _MAX consider a WordBus resource to be invalid. Fixes a panic on
the IBM x3550-M4.

ok mikeb@


# 1.232 29-Mar-2012 mlarkin

Missing #ifdef in previous diff


# 1.231 29-Mar-2012 mlarkin

Prevent hibernation attempts on machines with unsupported disk controllers.
The same check is also performed later in the hibernate sequence, but by
then it's too late to cleanly unwind (presently).

ok deraadt


# 1.230 26-Mar-2012 deraadt

simplify conditionals in acpi_sleep_state()
ok mlarkin


# 1.229 26-Mar-2012 deraadt

hook in the hibernate request code; half of this diff is from mlarkin
ok mlarkin


Revision tags: OPENBSD_5_1_BASE
# 1.228 20-Sep-2011 deraadt

Very early in suspend while the clock is still ticking realtime,
save the clock back to the rtc. The zaurus already did this.
ok phessler miod kettenis


Revision tags: OPENBSD_5_0_BASE
# 1.227 02-Jul-2011 nicm

kqueue attach functions should return an errno or 0, not a plain 1. Fix
the obvious cases to return EINVAL and ENXIO.

ok tedu deraadt


# 1.226 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.225 16-Jun-2011 pirofti

Import acpitoshiba driver.

This is originally written by Hiroyuki Aizu, ported and integrated
in our tree by Javier Vazquez <javama4 AT gmail> with tweaks and
suggestions by me.

Import prodded by deraadt@ and the lack of testers on tech@.


# 1.224 27-Apr-2011 jcs

attach acpithinkpad to newer lenovo models like the x120e

ok deraadt@


# 1.223 22-Apr-2011 jordan

Fix uninitialzied variables and formatting strings (-Wxxx errors)


Revision tags: OPENBSD_4_9_BASE
# 1.222 02-Jan-2011 jordan

Re-add code for ACPI taskqueue, eliminates need for state vars in acpi softc
Serialize processing ACPI tasks for sleep/powerdown/GPE event
ok deraadt@


# 1.221 31-Oct-2010 guenther

Revert last commit: it breaks resume on Thinkpads
"then please back it out" deraadt@


# 1.220 26-Oct-2010 jordan

Add task queue for ACPI gpe and notify handlers
This fixes eject on Dell Latitude dock and an issue on the Dell Mini
battery update.
ok deraadt


# 1.219 07-Oct-2010 jordan

Create separate functions for enabling wake and runtime gpes.
Remove old suspend/resume gpewalk function
ok deraadt, mlarkin


# 1.218 05-Oct-2010 jordan

split acpi_enable_onegpe to separate enable/disable functions
ok deraadt


# 1.217 05-Oct-2010 jordan

Added acpi_disable_allgpes function to clear GPE status
ok deraadt


# 1.216 05-Oct-2010 deraadt

clean up acpi_softc use more


# 1.215 05-Oct-2010 deraadt

delete a bit of #if 0 code


# 1.214 05-Oct-2010 deraadt

hide use of our global acpi_softc pointer a bit better


# 1.213 29-Sep-2010 deraadt

grab the acpi thread lock very early in acpi thread startup; not that it
is likely that anyone will race us... but why be careless.
ok kettenis


# 1.212 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.211 29-Aug-2010 jasper

- add a check for FWRITE here too.

ok miod@


# 1.210 27-Aug-2010 kettenis

Make PCI Power Management optional, and only enable it when acpi(4) attaches.

ok deraadt@


Revision tags: OPENBSD_4_8_BASE
# 1.209 08-Aug-2010 kettenis

Close race between the acpi thread and wscons ioctls running in process
context. We might rip this out post-release and replace it with a solution
that serializes things in a more obvious way.

ok deraadt@, miod@ and "those crazy texans who ok anything"


# 1.208 08-Aug-2010 deraadt

Report the acpi sleep states when on a RAMDISK; ok krw


# 1.207 07-Aug-2010 kettenis

Enforce acpi register access rescrictions.

ok mlarkin@, deraadt@, tested by marco@


# 1.206 07-Aug-2010 deraadt

upon resume, notify all the acpiac and acpibat drivers to update
their status. some laptops do not do this automatically.
ok kettenis canacar


# 1.205 06-Aug-2010 marco

Always call _PSW on _LID devices that support it to enable lid open to
resume the machine. Conversely disable it when the machine wakes up.

Tested by several ok deraadt


# 1.204 06-Aug-2010 deraadt

As part of a revamp of the PM1/GPE code, I write this diff to look for
unmanaged EN & STS bits in the PM1 register at interrupt time and report
them. As a side effect this splits the STS acknowledgement into two writes
(for power, and sleep) instead of one. The printf that is added (to spot
unmanaged STS bits) has not yet been triggered as far as we know.

Before the "write to PM1 registers at the right offset" diff went in, this
was not neccessary. But newer thinkpads do not have a working soft power
button without this diff.

We have no idea why.
ok mlarkin kettenis


# 1.203 05-Aug-2010 deraadt

PM1 power button events were generating 'fake events' to acpibtn PWRB,
but if there is no such device these were being tossed. Instead, arrange
for all power button handlers to flag a softc variable which is processed
by the thread; so that one place will know to request a powerdown (psignal
to init)
ok marco mlarkin


# 1.202 05-Aug-2010 deraadt

rename a variable which is annoyingly poorly named


# 1.201 05-Aug-2010 mlarkin

Write to the correct offset for various ACPI pmreg registers. Writing to
the correct register does properly clear the wake status bits and fixed
event bits, which allows some machines to have more than 1 suspend cycle.

bug spotted by deraadt@

tested by deraadt@ and myself on a half-dozen machines and others on
hackers@

ok deraadt@, marco@


# 1.200 05-Aug-2010 deraadt

Fix the power button. The pm1 register is 16 bits in size.
My mistake, a while back.
tested by krw


# 1.199 04-Aug-2010 kettenis

Print supported sleep states.

ok marco@, phessler@, deraadt@


# 1.198 03-Aug-2010 mlarkin

Use the proper flag for re-enabling certain hardware events (power btn,
sleep btn). The (incorrect) flag prevoiously used coincidentally had the
same value, so this shouldn't cause any different behavior than before.

tested on a variety of machines (i386, amd64, sp, mp) - no behavioral
change seen

ok deraadt@


# 1.197 28-Jul-2010 marco

Make all gasio failures look and behave the same.


# 1.196 28-Jul-2010 mlarkin

Delay a little longer on suspend before giving up. Fixes machines that
sometimes don't suspend because the sleep transition takes too long
(symptoms like failing to sleep but the backlight stays on).

ok deraadt@


# 1.195 27-Jul-2010 deraadt

remove XXX; EC can now be attached early when needed; ok jordan


# 1.194 27-Jul-2010 deraadt

more brackets feels good


# 1.193 27-Jul-2010 deraadt

XXX annotate a block in acpi_enter_sleep_state() that is pretty worrying


# 1.192 27-Jul-2010 deraadt

horrific indentation that nearly made me puke


# 1.191 27-Jul-2010 mlarkin

Set the sleeping indicator light on machines that support it, in case SMI
doesn't do it for us. Shows the 'moon' LED on older Thinkpads when
sleeping.

Tested on T43p, W500, T510 by me and on X61s by deraadt@.

ok deraadt@


# 1.190 27-Jul-2010 jordan

Early initialization of acpiec if ECDT table exists
Fixes hang when booting thinkpads while docked
ok deraadt


# 1.189 26-Jul-2010 deraadt

remove merge error which was commited


# 1.188 26-Jul-2010 pirofti

Add support for Sony ACPI hotkeys via a new driver: acpisony(4).

Currently it only works for the suspend button and tries to do right for
the brightness events, but I haven't found a Sony laptop that like to do
right so far.

In the future I want to make the brightness keys work on all Sony's and
also add support for the zoom hotkeys and whatever other funky keys I can
find on those things.

Okay deraadt@.


# 1.187 22-Jul-2010 deraadt

pretty large cleanup of SMALL_KERNEL


# 1.186 21-Jul-2010 deraadt

in the acpi GPE handler, late ack level interrupts not edge interrupts.
Duh.


# 1.185 21-Jul-2010 deraadt

Merge the edge and level gpe handlers into one (now possible since the
gpe structure tells us whether the gpe is edge or level)


# 1.184 21-Jul-2010 deraadt

delete #if 0 code we are not going to use; ok marco


# 1.183 20-Jul-2010 deraadt

make the mask code for acpi_enable_onegpe() match other similar code


# 1.182 20-Jul-2010 deraadt

Handle the acpi interrupt controller with proper edge/level handling,
wrapped in spltty to avoid racing against the interrupt controller.
Repair the gpe bit masking code while there, and do operations in the
right order
ok kettenis mlarkin, help from jordan at figuring out the order of
operations.


# 1.181 20-Jul-2010 matthew

When we're preparing to go to sleep, only call bufq_restart() if other
preparations failed.

ok kettenis@, thib@, deraadt@


# 1.180 19-Jul-2010 deraadt

permit compilation with ACPI_DEBUG


# 1.179 19-Jul-2010 deraadt

there is no need for a seperate dsdt_softc pointer; dsdt does not have a
softc but is just a part of the single acpi driver.
ok jordan


# 1.178 19-Jul-2010 deraadt

acpi_delay() is no longer needed; ok jordan


# 1.177 18-Jul-2010 mlarkin

Clear event status on resume, to avoid going back to sleep/reboot/shutdown
immediately after resume. Tested by a bunch of people on hackers@ on
various hardware. Fixes at least Toshiba NB200 and Gateway LT3103u.

ok marco


# 1.176 13-Jul-2010 deraadt

rename some thread related interfaces so they are logical


# 1.175 10-Jul-2010 jordan

Verify _ADR bdf is correct (some systems return 0xffff)
Works on Dell x9xx and Studio 1555


# 1.174 09-Jul-2010 jordan

Clear the noise


# 1.173 08-Jul-2010 jordan

Add mapping for ACPI device to PCI bdf (match autoconf tree)
Simplify resource parsing function to use buffer argument
Convert namespace linked lists to use queue macros
ok marco@, deraadt@


# 1.172 06-Jul-2010 deraadt

Use spltty() to lock downcalls from apm against the information being
modified by the acpi thread.
ok kettenis


# 1.171 06-Jul-2010 deraadt

Don't use a workq to do the suspend, because it races aginst the acpi
thread. Instead, just tell the acpi thread to do the suspend for us.
This makes apmd & zzz work correctly. While here, have acpithinkpad
attempt to post the event to apm before prompting the suspend itself.
ok kettenis marco mlarkin


# 1.170 05-Jul-2010 mlarkin

Reset register SCI_EN on resume. The ACPI spec clearly states we don't have
to do this, but many machines don't, or forget to, which leads to all sorts
of problems on resume (broken battery stats, overheating, failure to resume
more than once, etc).

tested by many, ok marco@, deraadt@


# 1.169 01-Jul-2010 thib

do bufq quiesce again, since vnd/the bufq_done call in
biodone has been fixed.


# 1.168 01-Jul-2010 jordan

Backout recent AML changes, breaks on R210 and others


# 1.167 01-Jul-2010 jordan

Simplified aml_parse_resource call, cleanup code
ok marco


# 1.166 01-Jul-2010 jordan

Fix subordinate bus # for multi-root PCI buses
ok marco, kettenis


# 1.165 30-Jun-2010 thib

Disable/partially backout the bufq quiesce changes as this
is causing havoc with vnds and release must be buildable.


# 1.164 29-Jun-2010 deraadt

Fix ramdisks (new code was placed inside a SMALL_KERNEL chunk)


# 1.163 29-Jun-2010 jordan

Add support for mapping ACPI to PCI devices
ok kettenis, deraadt


# 1.162 29-Jun-2010 kettenis

Call bufq_quiesce() on suspend to stop all I/O and bufq_restart on resume.
Makes suspend/resume work much better while doing disk I/O.

ok mlarkin@


# 1.161 29-Jun-2010 tedu

60 is a more reasonable guess for number of minutes in an hour than 100.
spotted by TeXitoi. ok deraadt


# 1.160 27-Jun-2010 jordan

Add _acpi_matchhids routine, precursor to pci device match


# 1.159 27-Jun-2010 mlarkin

Clear ACPI fixed event status on resume (power buttons/etc) so that some
machines don't immediately resume after suspending to S3.

Tested by kettenis@ and I on 5 or 6 machines, deraadt@ "yeah, looks good"


# 1.158 27-Jun-2010 jordan

Cleaned up acpi_maptable, adds to table list directly


# 1.157 07-Apr-2010 deraadt

If we have done a wsdisplay_suspend(), and ACPI decides it does not want to
suspend, we must roll back by calling wsdisplay_resume() or the virtual
console switching will be locked forever
ok oga


# 1.156 07-Apr-2010 kettenis

Make suspend/resume work on MP machines (running an MP kernel). Joint work
from mlarkin@ and me, with some amd64 fixes thrown in by deraadt@

ok marco@, deraadt@, pirofti@, mlarkin@


# 1.155 31-Mar-2010 kettenis

Bring /dev/apm support in line with apm(4). Make the suspend button send
a suspend request event to apmd(8) instead of suspending immediately. Also
keep track of whether /dev/apm and /dev/apmctl are currently open, such that
we can still suspend immediately if apmd(8) isn't running.

ok deraadt@, marco@, pirofti@, jsing@, oga@


# 1.154 30-Mar-2010 oga

Prevent the apmd/x races for good.

When we hit suspend time, go through all wsdisplays on the system. if
they are in mode MAPPED, but not MODE_DUMBFB then if possible do a full
vt switch to a !mapped vt, and prevent switching back until resume time.
This has to be called from MD code because this involves userland
running so that X can run the vt switch signal handler. This way, any
case where we are using the "poke registers from userland" model, we
will not be on the hardware when we go down, so the kernel can actually
handle thing properly.

Tested on several acpi laptops (by kettenis@ and ian@), x40 (me and
beck@ at LEAST) and zaurus (me). Maybe others, but if so I forgot who at
this time..

Idea from deraadt somewhere over the Faroe Islands (I thought of a
similar thing myself a while ago). Much prompting from him. Ok and
comments miod@


# 1.153 25-Mar-2010 oga

Fix & vs. && in the kqfilter code.

Based on a comparison with the apm code.

ok deraadt@, kettenis@


Revision tags: OPENBSD_4_7_BASE
# 1.152 26-Nov-2009 mlarkin

Visual (and on some machines, audio) feedback while the machine is
resuming via calls to aml _SST method. This also makes the "moon" LED on
thinkpads to blink during resume and cease when resume is completed.

ok pirofti@


# 1.151 26-Nov-2009 deraadt

Toggle into "cold" when we we disable interrupts, because tsleeping
in the AML interpreter and context switching into userland processes
tends to sometimes pop the ACPI trampoline page out of our address
space. Makes lots more machines work.
ok kettenis mlarkin


# 1.150 24-Nov-2009 jsg

in the resume path evaluate _WAK before dealing with device drivers
like the spec says, not after.

ok mlarkin@


# 1.149 24-Nov-2009 deraadt

abort suspends on SMP systems until that gets written; ok mlarkin


# 1.148 23-Nov-2009 mlarkin

pci should return the result of it's children's suspend/resume calls back
to its parent.

handle suspend failure case and unwind if devices have an activate function
that returns failure.

ok deraadt@, kettenis@


# 1.147 23-Nov-2009 mlarkin

acpi* devices need to call their children on suspend/resume.
ok deraadt@


# 1.146 23-Nov-2009 pirofti

Remove ACPI_SLEEP_ENABLED checks.

This enables by default the suspend/resume paths in the kernel.

Okay deraadt@.


# 1.145 23-Nov-2009 deraadt

knf and spacing that are super annoying


# 1.144 23-Nov-2009 mlarkin

Hook up Theo's new children suspend/resume function, as well as more ACPI
suspend/resume glue.

ok deraadt@


# 1.143 26-Oct-2009 deraadt

Do not do strncmp comparisons on dv_xname because longer device names which
look similar could arrive in the future. Instead, compare directly against
dv_cfdata->cf_driver->cd_name
Issue originally spotted by miod


# 1.142 26-Oct-2009 jordan

Verify checksum+address when loading ACPI tables.
Some systems had invalid entries in RSDT/XSDT.
ok marco@


# 1.141 23-Jul-2009 cnst

New aibs(4) driver for ASUSTeK AI Booster (ACPI ATK0110) hardware monitoring,
with sensor state support through limits provided by the ACPI.

Tested on several ASUS motherboards kindly networked by Sam Fourman Jr.

ok deraadt marco jordan


Revision tags: OPENBSD_4_6_BASE
# 1.140 03-Jun-2009 pirofti

Import acpi power resource driver. This is needed for suspend/resume logic.

Okay jordan@ and marco@.


# 1.139 03-Jun-2009 pirofti

Add acpivideo support. This adds brightness support for all laptops
except thinkpads, they will use the acpithinkpad driver. The driver is
also hooked into wsconsole. So brightness can be adjusted via:

$ wsconsctl display.brightness=<percentage>

This is very helpfull on some laptops that have a nasty bios and get two
steps instead of one when pressing the brightness button.

Tested on various dell, fujitsu, acer, samsung and other laptops.

Okay marco@, miod@. Suggestions from kettenis@.
Lots of reviews and help from miod@, thanks!


# 1.138 03-Jun-2009 jordan

Added ejectable bay for IDE cdroms
ok marco@


# 1.137 30-Apr-2009 marco

Oga changed apm ioctls and forgot to grep. Temporary work around by hitting
the sleep button when we get any request. This interface will chnage at
some point.
ok oga


# 1.136 19-Apr-2009 krw

Call acpi_sleep_walk() when powering down just like when entering
any other sleep state. This tells acpi wake devices to not wake up
the box until manual powering up resets their status. Makes my Dell
GX520 stay powered off in response to 'halt -p'.

In snaps for a while.

ok marco@ (several times) deraadt@


# 1.135 17-Apr-2009 pirofti

Add device ranking support for future sleep/wake developments.

okay marco@


# 1.134 11-Mar-2009 jordan

Cleanup.. use aml_evalinteger instead of aml_evalname
ok marco@


# 1.133 10-Mar-2009 jordan

Fix cut/paste error.. call _BFS not _PTS
ok marco@


Revision tags: OPENBSD_4_5_BASE
# 1.132 21-Feb-2009 marco

overzealous cut/paste; found by Vladimir Kirillov <proger@uaoug.org.ua>


# 1.131 20-Feb-2009 marco

Oops didn't mean for Debugger() to remain there.


# 1.130 19-Feb-2009 marco

suspend/resume bits so that we can develop this in tree. This is disabled.
code from mlarkin and me
help from art,toby,jordan and several others
ok jordan, go for it deraadt


# 1.129 10-Feb-2009 jordan

Fix null pointer for shutdown if acpi not enabled
ok marco@


# 1.128 04-Feb-2009 kettenis

Some systems that claim to be ACPI 2.0 apparently don't provide an XSDT
address. Make the code fall back on the RSDT on those systems.

From Joerg Niendorf <p3y80@internode.on.net>. Fixes PR 5986.


# 1.127 17-Jan-2009 krw

A couple of stray malloc/memset -> malloc(M_ZERO), a bit of knf and
lose a couple of unnecessary casts. No functional change.

Feedback from kettenis@

ok kettenis@ marco@


# 1.126 06-Nov-2008 marco

Collapse all sanity checks of hids into one function.

Originaly from jordan


Revision tags: OPENBSD_4_4_BASE
# 1.125 02-Jul-2008 fgsch

add acpivideo. for now almost an empty skel so others can work on
suspend/resume. eventually it will also manage output switching and
brightness where supported. prodded by marco@


# 1.124 11-Jun-2008 marco

Fix $OpenBSD$ for once and for all


# 1.123 08-Jun-2008 deraadt

aaa_dev was being set from an uninitialized variable; ok toby


# 1.122 07-Jun-2008 marco

Kill ACPIVERBOSE. It was never used and is worthless to boot.

comments jmc
ok toby


# 1.121 06-Jun-2008 marco

Shrink acpi some more for floppies and while here re-enable acpi on the
floppies. Fixes crazy sony vaio kettenis has.

tested by deraadt
ok deraadt jordan kettenis


# 1.120 01-Jun-2008 marco

Fix \n nit in SMALL_KERNEL.


# 1.119 01-Jun-2008 marco

Re-enable new parser. Lots of testing by various folks. Fits all media
now too.

ok kettenis deraadt


# 1.118 16-May-2008 dlg

back out jordans new parser.

it didnt have enough oks from the right people, it did break machines
that were previously working, and it bloated the RAMDISKS and install
media beyond the constraints of the media.

requested by deraadt@


# 1.117 14-May-2008 jordan

Adding new ACPI Parser code
Old guts of code still exists, needs to be torn out next
ok marco@
ok brad@


# 1.116 27-Apr-2008 jcs

add acpithinkpad(4), an acpi support driver for ibm/lenovo
thinkpads.

provides functionality that the bios did on older, non-acpi
thinkpads like respond to screen brightness keys and toggle the
bluetooth device on and off.

ok deraadt@ and marco@


# 1.115 24-Apr-2008 jsing

Introduce acpiasus(4), a driver for the ACPI based hotkeys found in many
ASUS laptops (including the ASUS EeePC) - largely based on NetBSD's
asus(4) driver. On the ASUS EeePC this allows us to enable/disable
wireless, change screen brightness and use the volume keys.

ok jsg@, weingart@


# 1.114 12-Apr-2008 kettenis

Get rid of acpi_s5 global variables; simply send SIGUSR2 instead of SUGUSR1
to tell init(8) to power down the machine.

ok krw@


Revision tags: OPENBSD_4_3_BASE
# 1.113 21-Jan-2008 jsg

Check against RESET_REG_SUP in FADT before doing the acpi reset dance
after all, not doing so appears to break more machines than it fixes.

ok marco@


# 1.112 05-Jan-2008 kettenis

Never attach more acpicpu(4) devices than cpu(4) devices. Always attach
the first acpicpu(4) device on non-MULTIPROCESSOR kernels, regardless of its
ACPI processor ID.

ok mikeb@


# 1.111 05-Dec-2007 deraadt

Hang acpi(4) below bios(4) instead of mainbus(4). This lets us move acpi(4)
before pcibios(4), and then.... if acpi(4) attaches, skip pcibios(4) since
it messes the machines up. Fixes claudio's HP dl320 G5, and almost assuredly
others.
ok kettenis


# 1.110 05-Dec-2007 deraadt

repair acpiopen/acpiclose and friends to handle the /dev/apmctl interface,
so that apmd(8) can actually work correctly against acpi(4), and thus,
even do the fancy -C/-A stuff; also tested by jmc


# 1.109 02-Dec-2007 fgsch

fix the trailing space for real now.


# 1.108 02-Dec-2007 fgsch

avoid trailing spaces on output; marco@ ok.


# 1.107 27-Nov-2007 kettenis

Fix off-by-one that made us attach acpicpu0 and acpicpu1 on GENERIC kernels.

ok marco@


# 1.106 25-Nov-2007 jsg

When available and ACPI is enabled use the ACPI reset method
to reboot. This is required for some quirky sis machines
and likely others to reboot.

Tested by many. 'commit early commit often' deraadt@


# 1.105 19-Nov-2007 deraadt

sigh, amd64 still uses that stupid acpi_enabled variable...


# 1.104 19-Nov-2007 deraadt

softc's come initialized to 0, you know...


# 1.103 19-Nov-2007 deraadt

store revision; from jsg


# 1.102 19-Nov-2007 kettenis

Initialize sc_facs earlier.

ok deraadt@, beck@, canacar@


# 1.101 16-Nov-2007 deraadt

fix the bus_space #define nightmare, so that amd64 and i386 are much more
uniform. as a result shared code like acpi needs less #ifdef's
ok marco kettenis


# 1.100 15-Nov-2007 deraadt

remove the #ifdef ACPI_ENABLE. the EC codepath is either safe enough (and
will be improved), or it is unsafe because AML assumes it must be there
ok almost everyone


# 1.99 15-Nov-2007 mikeb

Make acpicpu(4) attach to the existing CPUs only.

Problem reported by form@. Fix idea by kettenis@.
Lots of help from deraadt@. Tested by deraadt@, form@ and me.

ok deraadt marco


# 1.98 14-Nov-2007 deraadt

spacing


# 1.97 12-Nov-2007 deraadt

correct : vs , in attach-time messages


# 1.96 06-Nov-2007 deraadt

all ACPI_ENABLE kernels need acpi_interrupt()


# 1.95 06-Nov-2007 deraadt

move acpi_read_pmreg() and acpi_write_pmreg() out of SMALL_KERNEL so
that acpi ramdisks can compile (obvious solution)


# 1.94 05-Nov-2007 canacar

Establish the acpi interrupt earlier. Fixes problems with some
GENERIC.MP kernels not getting acpi interrupts. This is a
workaround until the real issue with late establishment is fixed.
ok weingart@, beck@, gwk@


# 1.93 03-Nov-2007 jordan

Added support for displaying wakeup devices
ok beck@,weingart@,gwk@


# 1.92 11-Oct-2007 kettenis

When walking the tree yo find _PRT methods, check whether the device is
actually enabled and functioning. Similar to what we do for _INI.

ok weingart@, beck@


# 1.91 08-Oct-2007 krw

More simple memset(,0,) -> M_ZERO changes. In this batch move to
size(*p) as the first malloc() parameter where p is declared locally
and thus easy to check. Add M_ZERO to gpe_table allocation in acpi.c
even though there is no obvious bzero or memset nearby.


# 1.90 13-Sep-2007 mk

Remove unnecessary memset() (see aml_evalnode(9)) and a bit of KNF.

ok toby


# 1.89 13-Sep-2007 weingart

Implement ACPI 6.5.1 spec tree walk for _STA and _INI.
Fixes mk's laptop. No regressions so far (thank you to the
testers).

ok gwk@, mk@, marco@


Revision tags: OPENBSD_4_2_BASE
# 1.88 17-Apr-2007 mk

Now that acpidock no longer hangs machines during boot (at least
according to the reports I've been getting) it should be safe to enable
it again, so remove the #if 0 stuff around it.

ok marco


# 1.87 11-Apr-2007 jordan

Added changes for C-State
ok marco@


# 1.86 26-Mar-2007 marco

Calculate minutes left more accurately.

From giovanni <qgiovanni@gmail.com>


Revision tags: OPENBSD_4_1_BASE
# 1.85 03-Mar-2007 mk

Stop looking for acpidock until we fix that stuff that breaks all pre-60
series ThinkPads with acpidock(4) enabled.


# 1.84 22-Feb-2007 marco

We were getting no interrupts in .MP. There are 2 fixes; one in
acpi_machdep.c and move the interrupt establishment down after ioapic
has been initialized.

ok jordan


# 1.83 21-Feb-2007 kettenis

Make an acpi-enabled GENERIC.MP work on systems that only support PIC mode.

ok marco@


# 1.82 19-Feb-2007 jordan

Added acpi_map_address function for mapping GAS/IO registers
ok marco@


# 1.81 18-Feb-2007 jordan

Added dynamic perfect hash for opcodes
ok marco@


# 1.80 18-Feb-2007 marco

Shave off another K pre compression.
Shuffle SMALL_KERNEL functions around so that we only have a single #ifndef


# 1.79 17-Feb-2007 marco

Add SMALL_KERNEL throught acpi to make it fit on boot media.
ok deraadt


# 1.78 13-Feb-2007 marco

Add smarter _INI code. This helps with some dissapearing devices.

ok kettenis jordan


# 1.77 31-Jan-2007 gwk

Introduce acpi_hasprocfvs thats "ACPI Has Processor Frequency and Voltage
Scalling" a global flag set by acicpu attach to indicate that ACPI is
handling fvs through the _PSS objects. This will fix disappearing
EST/powernow when booting kernels with acpi enabled, as acpicpu isnt
compiled into GENERIC yet. Also in cases where acpicpu is built in and
legacy EST/powernow work but acpi is missing the _PSS object, the legacy
routines will be used.

tested by dim@, and Nick Nauwelaerts <nick AT nauwelaerts.net>
ok marco@, dim@


# 1.76 22-Jan-2007 mk

Actually look for docking stations by looking for _DCK nodes.

ok marco


# 1.75 02-Jan-2007 marco

On some systems EC must be attached as the first device. Move EC from
generic _HID devices to its own routine to force first attach. Bug found
and debugged by joris@

Code mostly written by joris@


# 1.74 26-Dec-2006 marco

Add polling to devices that require it such as acpibat and acpitz.
Use the same tsleep/wakeup mechanism as the interrupt code to keep it non-
concurrent.


# 1.73 21-Dec-2006 deraadt

even more knf love


# 1.72 21-Dec-2006 marco

Remove some leftover acpiec gpe/interrupt code.


# 1.71 20-Dec-2006 deraadt

oops, name already used


# 1.70 20-Dec-2006 deraadt

rename __acpi_enable_gpe(); ok marco


# 1.69 20-Dec-2006 deraadt

acpi_unmap_pmregs() is not used, ok marco


# 1.68 19-Dec-2006 deraadt

simplify some low-level methods substantially (shrinking code); ok marco jordan


# 1.67 06-Dec-2006 mk

malloc(..., M_NOWAIT) with no NULL check is bad. If allocation fails,
complain, clean up, and bail out of attach.

input and ok gwk


# 1.66 28-Nov-2006 jordan

Fixed clearing arg for acpi_init_pic
ok kettenis@


# 1.65 27-Nov-2006 kettenis

Don't spam dmesg.


# 1.64 27-Nov-2006 jordan

Fixes for acpiprt routing
Post-parse fixup for namerefs
ok kettenis@


# 1.63 25-Nov-2006 marco

Don't handle acpiev events inside the gpe handler since this causes deadlocks.

ok jordan@


# 1.62 15-Nov-2006 kettenis

Hook up acpi apic interrupt routing support to generic acpi code.

ok jordan@


# 1.61 02-Nov-2006 marco

Kill acpiec attach function and add its functionality to the generic _HID
device attach

prodded and ok jordan


# 1.60 02-Nov-2006 marco

Fix comments to reflect reality.


# 1.59 19-Oct-2006 marco

Replace ACPI ioctls with APM ioctls. This makes ACPI talk the same protocol
as APM therefore userspace can now retrieve power information in the same
fashion as all other apm arches (zaurus, macppc, i386 etc).

To use force the use of acpi device like so: apmd -f /dev/acpi

ok jordan@


# 1.58 19-Oct-2006 jordan

Fixed GPE debug levels
Fixed ref counter on temporary bufferfields
DSDT parsing fully working on IBM Thinkpad
ok marco@


# 1.57 15-Oct-2006 jordan

Removed old GPE handler code remnants
ok marco@


# 1.56 12-Oct-2006 jordan

Massive ACPI changes
Fixed ACPI leaks
new ACPI API
ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.55 30-Jun-2006 gwk

KNF, No functional change,
"go at it" marco@


# 1.54 30-Jun-2006 jordan

Support for calling Embedded Controller via new GPE interface
ok marco@


# 1.53 30-Jun-2006 jordan

Added new code for GPE handling
Also works with ACPIEC; need to fixup acpiec.c to remove original interrupt handler
ok marco@


# 1.52 30-Jun-2006 jordan

Added offset argument for acpi read/write pmreg routines (new GPE code)
ok marco@


# 1.51 31-May-2006 todd

do not call acpi_powerdown() unless acpi has successfully initialized
ok gwk@ canacar@


# 1.50 29-May-2006 canacar

Add embedded controller support.
ok marco@ deraadt@ testing & ok gwk@


# 1.49 19-May-2006 canacar

Minimal thermal zone driver. Monitors thermal zone temperature,
shuts down the system if the 'critical temperature' is reached.
ok marco@


# 1.48 11-Apr-2006 marco

KNF.


# 1.47 11-Apr-2006 gwk

Add acpi_resume(struct acpi_softc *sc), which calls some AML methods,
detected at attach time, currently unused.

ok marco@


# 1.46 11-Apr-2006 gwk

Discover some ACPI sleep related methods, and call them at the
appropriate time in acpi_enter_sleep_state().

ok marco@


Revision tags: OPENBSD_3_9_BASE
# 1.45 26-Feb-2006 marco

Add acpicpu as a device.

ok jordan@


# 1.44 23-Feb-2006 marco

remove dead function.

ok jordan@


# 1.43 22-Feb-2006 marco

Print out all tables it found during autoconf. We'll need this data in
the future.


# 1.42 22-Feb-2006 marco

Make dmesg less verbose.

ok jordan.


# 1.41 22-Feb-2006 jordan

Added parsing of SSDT tables
ok marco@


# 1.40 22-Feb-2006 jordan

Added new aml_register to notify by device id
Moved powerdown code to button device
ok marco@


# 1.39 21-Feb-2006 marco

Readjust debug levels that crept in too low over the last few days.
KNF


# 1.38 21-Feb-2006 marco

Rig the power button so that the machine shuts down correctly.


# 1.37 20-Feb-2006 marco

Add acpibtn device. Remains disabled for now.


# 1.36 19-Feb-2006 jordan

Added code for aml_notify GPE notification callback
ok marco@


# 1.35 19-Feb-2006 grange

Fix acpi_enter_sleep_state() by replacing current ugly hack
with mostly correct sleeping state transition code.

ok brad@ marco@


# 1.34 19-Feb-2006 marco

jordan:
Enable interrupts.
Added peripheral code to make this work.

marco:
Replace softintr and other types of interrupts with a handler with context.
Fixed interrupt magic for GPEs.
Some KNF

This is work done by jordan and I but has to be a single commit.


# 1.33 17-Feb-2006 marco

Remove earlier introduced delays. We really only need them during autconf.

ok jordan@


# 1.32 17-Feb-2006 marco

Add a generic delay function as per the ACPI spec.

from jordan@


# 1.31 17-Feb-2006 marco

Use tsleep instead of delay when !cold.


# 1.30 17-Feb-2006 marco

Argh, we need delays between reads/writes. The backend reads (i2c most likely)
are so slow that values come back as 0 sometimes. We have to do this
despite the spec saying we shouldnt have to. Guess having a faster
parser than others can hurt sometimes.

Idea from jordan.


# 1.29 17-Feb-2006 jordan

Fixed code for handling _INI
Rearranged debug statements
ok marco@


# 1.28 16-Feb-2006 jordan

Added stub for calling _INI
Fixed aml_dowhile for NULL return
ok marco@


# 1.27 16-Feb-2006 jordan

Fixed parser/evaluator to retrieve battery info+state
Cleanup of parser
Dell laptops return battery status
ok marco@


# 1.26 06-Feb-2006 brad

fix a typo so this will compile on amd64.


# 1.25 04-Feb-2006 marco

When ACPI_ENABLED wasn't set but the devices were it had an unknown variable.

Pointed out by ian@.


# 1.24 03-Feb-2006 jordan

Rewrote aml parser yet again...
Fixed bus_space_map for i386/amd64
Added _BST and _BIF to acpibat to get status/info
ok marco@


# 1.23 20-Jan-2006 jordan

More evaluator changes
Removed old evaluator code
Fixed _HID value
ok marco@


# 1.22 19-Jan-2006 jordan

Added support for read/write of PCI Config space
Help jason@
ok marco@


# 1.21 18-Jan-2006 jordan

Added new evaluation routines
Added helper methods for buffer/field evaluation
Fixed broken indentation on dsdt.c

ok marco@


# 1.20 17-Jan-2006 jordan

Major overhaul of the aml parser. Evaluater temporarily disabled.

ok marco@


# 1.19 06-Jan-2006 grange

Check for ACPI control availability only if ACPI_ENABLE defined.


# 1.18 06-Jan-2006 grange

Informative error messages on attach, prettier dmesg.


# 1.17 06-Jan-2006 grange

printf -> dnprintf conversion for debug messages.


# 1.16 05-Jan-2006 grange

Find SLP_TYPx values and store them for future use in
acpi_enter_sleep_state().


# 1.15 05-Jan-2006 grange

Random KNF and cleanup


# 1.14 05-Jan-2006 grange

Provide symbolic names for the acpi sleeping states and use it.


# 1.13 28-Dec-2005 marco

Moved DEVNAME to a acpidev.h

ok jordan@


# 1.12 18-Dec-2005 sturm

add back comment about wrong code

ok marco


# 1.11 16-Dec-2005 marco

Remove test for kbd_reset. Prompted deraadt@, ok jordan@


# 1.10 16-Dec-2005 jordan

Added hooks for powerdown/power button press


# 1.9 16-Dec-2005 jordan

Cleaned up common power management register i/o
Added enter sleep state ioctl
Hooked power button interrupt
ok marco@


# 1.8 16-Dec-2005 marco

Remove autoconf chatter


# 1.7 16-Dec-2005 jordan

Moved DSDT AML parsing into parent ACPI driver
ok marco@


# 1.6 14-Dec-2005 marco

Add AC device glue.


# 1.5 14-Dec-2005 marco

Adjust debug printing levels to something more sane.


# 1.4 13-Dec-2005 marco

Add glue to attach devices to apci.


# 1.3 07-Dec-2005 marco

KNF


Revision tags: OPENBSD_3_8_BASE
# 1.2 10-Jul-2005 grange

Spacing cleanup.


# 1.1 02-Jun-2005 tholo

Start on a basic ACPI framework -- does not do much more than read out the
ACPI tables into kernel memory and attach ACPI and HPET timers currently.

In order to test this code, enabling the devices in GENERIC as well as
the ACPI_ENABLE option is needed. This code does not do any thermal
control yet, so this should be done with care depending on the platform.

In the tree so more people can contribute to making this more fully
featured.

Ok niklas@ grange@ tedu@


# 1.426 08-Jan-2024 kettenis

Implement RootPathString support in the LoadTable() AML function. Fixes
booting OpenBSD on some (ancient?) Hyper-V version.

Tested by Henryk Paluch
ok mlarkin@


Revision tags: OPENBSD_7_4_BASE
# 1.425 08-Jul-2023 tobhe

Move /dev/apm related acpi code to acpi_apm.c which is only built on
amd64 and i386. This is the first step towards a machine independent
safe sleep API.

tested by yasuoka@ bluhm@
ok deraadt@ kettenis@


# 1.424 07-Jul-2023 claudio

Add support for multiple batteries to acpithinkpad setchargestart and
setchargestop. With this laptops like x270 or x240 properly set the
thresholds for both batteries.
Tested by kn@ and jmatthew@
OK kettenis@


# 1.423 06-Jul-2023 deraadt

moving acpiioctl to a different file than acpiopen is not going to work
for ramdisks. noticed by anton.
this must be reconsidered.


# 1.422 05-Jul-2023 tobhe

Move acpiioctl to acpi_x86.c, it is only used up on i386 and amd64.

ok kettenis@ deraadt@


# 1.421 29-Jun-2023 dv

acpi(4)/acpibtn(4): use opt-in approach for wakeup GPEs

Previously, any discovered GPE that could be enabled for wake was
being enabled prior to entering ACPI-based S3 or S4. On some newer
machines, this caused S3-based suspend to break as some devices we
don't fully control (e.g. lack of driver) might be capable of waking
the system and will do so almost instantly if the GPE is enabled for
wake up (i.e. the so called "instant wake" issue).

This changes to a model of having device drivers explicitly opt
into using their GPE for wake up.

The first driver to explicitly toggle a GPE for wake is acpibtn(4).

Variations of the above have been in snapshots for 2 weeks, so
tested by many.

ok deraadt@


Revision tags: OPENBSD_7_3_BASE
# 1.420 15-Mar-2023 kettenis

Add missing nameref resolving for the _DEP() package elements that indicate
dependencies between ACPI devices.

ok dlg@, deraadt@


# 1.419 10-Feb-2023 visa

Adjust knote(9) API

Make knote(9) lock the knote list internally, and add knote_locked(9)
for the typical situation where the list is already locked.

Remove the KNOTE(9) macro to simplify the API.

Manual page OK jmc@
OK mpi@ mvs@


Revision tags: OPENBSD_7_2_BASE
# 1.418 13-Sep-2022 kettenis

Split out the code that collects data from acpiac(4), acpibat(4) and
acpisbs(4) for apm(4) and hook it up to the arm64 version of apm(4) on
systems with ACPI.

ok kn@


# 1.417 12-Sep-2022 kettenis

Add support for level-triggered GPIO events.

ok mlarkin@


# 1.416 10-Sep-2022 kettenis

Don't restrict GPIO events to pin numbers below 256. With the _EVT()
method events corresponding to larger numbers are possible.

Fixes lid state detection on the x13s.

ok patrick@


# 1.415 03-Sep-2022 kettenis

Make sure we don't suspend if we have no way to wake up the machine.
The idea is that device drivers that can wake up the machine register
themselves by calling device_register_wakeup(). To prevent regressions
on amd64, we let acpi(4) register itself as a wakeup device if the AML
for the machine defines wakeup devices. This may be refined in the
future.

This diff will prevent people from suspending their arm64 SBCs without
having a way to wake them up. For now the only driver that registers
itself is axppmic(4), which means that at this moment only SBCs with
an allwinner A64 or H5 SoC will actually support suspend/resume.

ok mlarkin@, deraadt@


# 1.414 10-Aug-2022 patrick

Provide the AML root in ACPI's softc so that drivers that need access
don't have to declare it using extern.

Suggested by and with feedback from kettenis@
Tested by deraadt@ on arm64, amd64 and i386
Tested by bluhm@ on amd64 and i386


Revision tags: OPENBSD_7_1_BASE
# 1.413 17-Feb-2022 jsg

reduce includes


# 1.412 10-Feb-2022 visa

Embed klist head in acpi_softc to avoid explicit malloc.

OK kettenis@


# 1.411 09-Feb-2022 deraadt

Move x86-only stubs used by kern/subr_suspend.c into dev/acpi/acpi_x86.c
for sharing between i386 and amd64.
ok mlarkin kettenis


# 1.410 08-Feb-2022 deraadt

The suspend/resume code is a sticky mess of MI, MD, and ACPI sequencing.
This splits out the MI sequencing, backing it with per-architecture helper
functions. Further steps will be neccesary because ACPI and MD are too
tightly coupled, but soon we'll be able to use this code for more architectures
(which depends on figuring out the lowest-level cpu sleeping method)
ok kettenis


# 1.409 04-Feb-2022 robert

set the APM_BATT_CHARGING state if the battery is being charged and try
to calculate the remaining time to be fully charged

ok jca@


# 1.408 02-Feb-2022 deraadt

acpi_addtask() calls malloc() w/ M_NOWAIT (because some calls come from
interrupt context), this however means occasional resource shortage will
result in callbacks registration failing, and unknown consequences for
the task-submitting caller.
Changing this to use pools with a low water mark, decreases the odds
of that problem occuring.
ok kettenis


# 1.407 01-Feb-2022 deraadt

GPE_DIRECT is calling the AML parser from interrupt context, which is
not permitted. Luckily nothing is using GPE_DIRECT anymore, so this code
can be deleted.
ok kettenis


# 1.406 26-Jan-2022 kettenis

An ACPI device needs to be both present and enabled for it to function.
So only attempt to attach hardware that has both bits enabled. This fixes
an issue where com(4) would attach for a disabled serial port leading to
misdetection of the hardware variant and a subsequent hang when /etc/rc
runs ttyflags -a.

ok anton@, deraadt@


# 1.405 12-Jan-2022 patrick

Make acpi_getpropint() return uint64_t, as ACPI integers are in fact that
wide and some _DSD properties depend on it.

ok kettenis@


# 1.404 04-Jan-2022 patrick

acpi_getprop() needs to actually make sure that we're looking at the
correct property. While there adjust acpi_getpropint() as well to
increase similarity with acpi_getprop().

ok kettenis@


# 1.403 16-Dec-2021 anton

Attach com over acpi on amd64. Some hardware uses a different interrupt
assignment compared to the the legacy one supported by com over isa.
This causes the console to halt once userland takes over as no
interrupts are received. The actual address and irq can be read from
ACPI, kettenis@ already added support for arm64 which paved the way for
amd64.

Some consoles that previously attached over isa are now expected to
attach over acpi.

Thanks to patrick@ for testing on arm64.

ok kettenis@


# 1.402 07-Dec-2021 kettenis

Fix parsing of SR_IRQ resource descriptors.

ok patrick@, anton@


# 1.401 02-Nov-2021 deraadt

knf


# 1.400 30-Oct-2021 deraadt

Change hw.perfpolicy=auto by default, at startup. If the system has AC
power connected (default is yes when no driver differentiates) then default
to 100% performance. On battery, use the existing auto algorithm (which is
admittedly somewhat unrefined).
This change overrides the system/BIOS speed and puts OpenBSD in control.
As this happens very early during boot, besides speedups in all usage usage
patterns, some surprises: unhibernate and sysupgrade times are cut in half.
note: on a few architectures, the setperf fn pointer is changed late, and
thus the auto algorithm stops timeing out. kettenis and i will look for
a solution.
in snaps for more than a week.
ok kettenis


Revision tags: OPENBSD_7_0_BASE
# 1.399 20-Jul-2021 mlarkin

Fix trailing whitespace in a few places.

No code change.


# 1.398 09-May-2021 kettenis

Some x86 machines advertise the "hardware reduced" ACPI feature, advertise
S4 and S5 support, but fail to populate the SLEEP_CONTROL_REG and
SLEEP_STATUS_REG descriptions in the FADT. An example of such a machine
is the ASUS Zenbook 14 UM433DA. Any attempt to powerdown the machine
will result in a kernel crash. It turns out that using the legacy ACPI PM
registers works fine on this machien. So fall back on those registers
if SLEEP_CONTROL_REG and/or SLEEP_STATUS_REG aren't provided.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_9_BASE
# 1.397 15-Mar-2021 patrick

Add acpi_iommu_device_map(), which replaces the DMA tag with one that
is blessed with IOMMU magic, if available. This is mainly for arm64,
since on amd64 and i386 the IOMMU only captures PCIe devices, as far
as I know, which uses the pci_probe_device_hook(). This though is for
non-PCI devices attached through ACPI.

ok kettenis@


# 1.396 07-Mar-2021 patrick

Pass standard DMA tag to acpi(4) table drivers.

ok kettenis@


# 1.395 27-Dec-2020 deraadt

acpi_map_address() cannot be wrapped by SMALL_KERNEL anymore, as
it is used by acpihpet.c


# 1.394 25-Dec-2020 visa

Refactor klist insertion and removal

Rename klist_{insert,remove}() to klist_{insert,remove}_locked().
These functions assume that the caller has locked the klist. The current
state of locking remains intact because the kernel lock is still used
with all klists.

Add new functions klist_insert() and klist_remove() that lock the klist
internally. This allows some code simplification.

OK mpi@


# 1.393 17-Dec-2020 kettenis

Fix some issues with referencing named ACPI nodes from Packages.
These references need to be resolved at runtime rather than when
they're parsed such that they pick up the right values for those nodes
which can be changed when for example _INI methods run.

The current approach is to replace these reference with a string that
names the node in question. The problem with that is that packages
can also contain normal strings. Which means that we need special
code that depends on the context where the package is used.

This diff takes a different approach by retaining a reference when
parsing. Code that uses a package will need to resolve this reference
but there is no ambiguiety anymore.

ok patrick@


# 1.392 05-Dec-2020 kettenis

Add support for the _EVT method to the GPIO support code.

ok patrick@


Revision tags: OPENBSD_6_8_BASE
# 1.391 27-Aug-2020 jmatthew

Reorder the acpi attach process so we look at Processor() CPU nodes
after Device() ones, since we should prefer the newer node type.
If we see any Device() nodes, don't attach acpicpu(4) to any Processor()
nodes if they're also present. This also makes acpitz(4) and
acpipwrres(4) devices attach slightly later.

ok kettenis@, also tested by jmc@


# 1.390 16-Aug-2020 gnezdo

Remove sc_maxgpe from acpi_softc which was never read from

Remove a dead conditional from acpi_init_gpes while there.

ok jcs@


# 1.389 21-Jul-2020 deraadt

acpi can use IPL_BIO (a low interrupt) since it only enqueues operations for
later processing. The use of a high interrupt will predate suspend/resume
efforts, we had to redesign acpi to be non-reentrant obviously
discussed with kettenis, in snaps for more than a week


# 1.388 24-Jun-2020 cheloha

kernel: use gettime(9)/getuptime(9) in lieu of time_second(9)/time_uptime(9)

time_second(9) and time_uptime(9) are widely used in the kernel to
quickly get the system UTC or system uptime as a time_t. However,
time_t is 64-bit everywhere, so it is not generally safe to use them
on 32-bit platforms: you have a split-read problem if your hardware
cannot perform atomic 64-bit reads.

This patch replaces time_second(9) with gettime(9), a safer successor
interface, throughout the kernel. Similarly, time_uptime(9) is replaced
with getuptime(9).

There is a performance cost on 32-bit platforms in exchange for
eliminating the split-read problem: instead of two register reads you
now have a lockless read loop to pull the values from the timehands.
This is really not *too* bad in the grand scheme of things, but
compared to what we were doing before it is several times slower.

There is no performance cost on 64-bit (__LP64__) platforms.

With input from visa@, dlg@, and tedu@.

Several bugs squashed by visa@.

ok kettenis@


# 1.387 10-Jun-2020 jca

Give userland a consistent battery and A/C state after resume

Previously we refreshed the state of acpiac(4) and acpibat(4)/acpisbs(4)
by queueing fake ACPI events to be processed by the acpi thread.
acpibat(4) using ACPIDEV_POLL meant that its status could be refreshed
first, resulting in an APM_POWER_CHANGE event sent to userland before
the state of acpiac(4) was up to date. Because of this, apmd(8) could
see A/C unplugged when it was in fact plugged, possibly leading to
a wrong decision.

Instead of using fake ACPI events, refresh the status of acpiac(4) and
acpibat(4)/acpisbs(4) right away at DVACT_WAKEUP time. To reuse the
existing refresh code of those drivers, make their ACPI *_notify()
handlers responsible for sending APM_POWER_CHANGE events instead. Also
queue an APM_POWER_CHANGE event right after resume to retain existing
behavior.

This fixes the consistency of data shown to userland, and makes those
drivers less special.

Initially discussed last year (!) with mpi@ at Miod's place.
Feedback and ok jcs@, deraadt@ agrees with the use of DVACT_WAKEUP here.


# 1.386 29-May-2020 deraadt

dev/rndvar.h no longer has statistical interfaces (removed during various
conversion steps). it only contains kernel prototypes for 4 interfaces,
all of which legitimately belong in sys/systm.h, which are already included
by all enqueue_randomness() users.


# 1.385 14-May-2020 kettenis

Bring back the acpi(4) changes. This time around only use the ACPI _CRS
information on ACPI 5.0 and later.

ok krw@, patrick@


# 1.384 11-May-2020 jca

More accurate remaining power reporting with disparate batteries

Overall remaining power was computed as the average of the remaining
power of each battery, in percents. This doesn't work with batteries of
different capacity, so use the total capacity and remaining power
instead.

Reported by lidstah, successful tests by lidstah and benno@, ok kettenis@


# 1.383 08-May-2020 kettenis

Move parsing of _CRS into acpi(4) and pass the parsed address and interrupt
information in struct acpi_attach_args.

ok mpi@


Revision tags: OPENBSD_6_7_BASE
# 1.382 14-Apr-2020 kettenis

Implement acpi_getprop(), the ACPI equivalent of OF_getprop().

ok patrick@


# 1.381 12-Apr-2020 kettenis

Add support for the _CCA method. This method indicates whether DMA is
cache-coherent or not. To implement this, acpi(4) gets two bus_dma tags
and passes the appropriate one when attaching devices based on _CCA.
On i386/amd64, where for all practical purpose DMA is always cache-coherent,
the two tags are the same. But on arm64 they are distinct.

ok patrick@


# 1.380 07-Apr-2020 visa

Abstract the head of knote lists. This allows extending the lists,
for example, with locking assertions.

OK mpi@, anton@


# 1.379 03-Apr-2020 mpi

Fix incoherencies of errors returned by various *kqfilter routines.

EOPNOTSUPP is returned when there's no "filterops" for a given fd.
EINVAL when the requested filter isn't supported by the "filterops".
ENXIO means the underlying device is no longer valid/present.

ok visa@


# 1.378 20-Feb-2020 visa

Replace field f_isfd with field f_flags in struct filterops to allow
adding more filter properties without cluttering the struct.

OK mpi@, anton@


# 1.377 31-Dec-2019 visa

Use C99 designated initializers with struct filterops. In addition,
make the structs const so that the data are put in .rodata.

OK mpi@, deraadt@, anton@, bluhm@


# 1.376 31-Dec-2019 mpi

Convert infinite sleeps to {m,t}sleep_nsec(9).

ok kn@


# 1.375 31-Dec-2019 jsg

include sys/sensors.h for sensor use
don't depend on indirect inclusion from acpidev.h


Revision tags: OPENBSD_6_6_BASE
# 1.374 07-Sep-2019 kettenis

Revert acpipci(4) changes. Too many systems are broken. We'll try again
after 6.6 as been released. The acpireg.h change stays behind.


# 1.373 28-Aug-2019 kettenis

Use ACPI information to attach PCI busses like we do on arm64. There are a
few additional quirks though, and attaching the PCI busses is delayed to
replicate the existing code more closely. That may be changed in the
future. Also tweak how we handle MSI support and respect to ACPI flag
that says we shouldn't attempt to use MSIs.

Some fallout is expected.

ok patrick@


# 1.372 27-Aug-2019 deraadt

On amd64 the bootup and resume paths for SP/BP/SP were not performing
msr/pat, clock-startup, firmware upgrade, fpu initialization, and cpu
soft-state initialization in the same order. That kind of chaos is
crazy and fragile. Improve the acpi resume call sequence and unify order
of the operations.


# 1.371 02-Jul-2019 jcs

acpi_set_gpehandler: turn edge arg into flags, add support for
passing GPE_DIRECT which directly executes the supplied callback and
clears a level interrupt rather than going through acpi_taskq which
can be slow when other driver callbacks are in the queue.

This should not be used for GPE handlers that need to do anything
ACPI-related.

ok kettenis


# 1.370 10-Jun-2019 kettenis

Print proper ACPI version number.

ok mlarkin@


# 1.369 08-Jun-2019 kettenis

When establishing the mapping between ACPI device nodes and PCI devices,
skip PCI host bridges and devices that aren't there.

ok patrick@


# 1.368 07-Jun-2019 kettenis

Introduce acpi_getsta() and use it to eliminate some duplicated code.

ok patrick@


# 1.367 12-May-2019 kettenis

Make sure the extended PM register descriptions actually exist before
using them. Fixes machines with ancient ACPI 1.0 (such as recent
hypervisors).

ok deraadt@


# 1.366 12-May-2019 kettenis

Add support for mapping ACPI PM registers using the "extended" gasio
representation from the FADT. Mostly fixes the Lanner NCA-1510.

ok lteo@, mlarkin@, deraadt@


# 1.365 11-May-2019 lteo

Fix incorrect error message.

ok kettenis@


# 1.364 08-May-2019 tedu

free size. the allocation is the header plus the length.


Revision tags: OPENBSD_6_5_BASE
# 1.363 04-Apr-2019 kettenis

Avoid potential null-pointer dereference. Found by Kent R. Spillner.

ok deraadt@, jsg@


# 1.362 20-Jan-2019 jsg

fix SMALL_KERNEL build after changes in rev 1.361
reported by naddy@ ok deraadt@


# 1.361 19-Jan-2019 tedu

Add a pwraction sysctl that controls what the power button does on acpi.
By default, nothing changes -- shutdown is initiated. But allows turning
power button into a sleep button if desired.
(grudging) ok from a few parties


# 1.360 26-Oct-2018 kettenis

Add acpipci(4) on amd64. For now this only calls the PCI-specific _OSC
method to let the ACPI implementation know what features we support.


Revision tags: OPENBSD_6_4_BASE
# 1.359 30-Aug-2018 kettenis

Move softbutton check before the check that skips attaching certain acpi
devices. Fixes regression caused by matching on _CID in addition to
matching on _HID.

ok matthieu@, mlarkin@


# 1.358 25-Aug-2018 kettenis

Match ACPI devices based on _CID if no match for _HID is found.

ok mpi@


# 1.357 19-Aug-2018 kettenis

Add support for multiple PCI segments. Only really implemented for arm64
for now as amd64/i386 firmware still caters for legacy OSes that only
support a single PCI segment.

ok patrick@


# 1.356 03-Aug-2018 kettenis

Let ahci(4) match on _CLS instead of _HID when attaching at acpi(4). Avoids
having to add many more _HID entries to the match table.

ok deraadt@, mlarkin@


# 1.355 10-Jul-2018 kettenis

Make legacy interrupts work in acpipci(4).

ok patrick@


# 1.354 05-Jul-2018 kettenis

Add acpipci(4), a driver that supports generic ECAM-compatible PCI host
bridges based on information provided by ACPI.

ok mlarkin@


# 1.353 04-Jul-2018 kettenis

Properly pass around the PCI "chipset tag" in acpi(4) and refactor
acpimcfg(4) to call an MD initialization functions that sets up a tag for
PCI ECAM.

ok guenther@, mlarkin@, krw@


# 1.352 01-Jul-2018 kettenis

Move acpi(4) attach glue into acpi_machdep.c.

ok guenther@, deraadt@


# 1.351 01-Jul-2018 kettenis

Add support for _DSD Device Properties.

ok mlarkin@


# 1.350 30-Jun-2018 kettenis

Add intr_enable() function, intended for MI use to amd64 and i386 and use
this in the acpi(4) suspend/resume code paths.

ok deraadt@


# 1.349 29-Jun-2018 kettenis

Use <stdint.h> types.

ok mpi@, jung@, krw@, deraadt@


# 1.348 29-Jun-2018 mlarkin

ACPI: Allow (with warning) GPE handler reassignment, instead of returning
a failure code.

ok kettenis


# 1.347 26-Jun-2018 mlarkin

knf


# 1.346 26-Jun-2018 kettenis

Let acpi(4) pass down the bus dma tag.

ok deraadt@


# 1.345 25-Jun-2018 kettenis

Reorganize acpi(4) code a bit in preparation for upcoming arm64 support.

ok deraadt@


# 1.344 20-May-2018 kettenis

Wake up the acpi thread after scheduling a gpio event.

ok mpi@


# 1.343 17-May-2018 kettenis

Sprinkle some #ifndef SMALL_KERNEL such that RAMDISK kernels cuild again.

pointed out by stsp@


# 1.342 17-May-2018 kettenis

Implement GenericSerialBus OpRegion support.

ok mlarkin@


# 1.341 27-Mar-2018 kettenis

Add acpicmos(4), a driver that implements SystemCMOS OperationRegion
access support. This fixes machines where the AML doesn't check whether
support for this OperationRegion type has been registered by the OS.

ok mlarkin@


Revision tags: OPENBSD_6_3_BASE
# 1.340 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.339 10-Feb-2018 deraadt

Syncronize filesystems to disk when suspending. Each mountpoint's vnodes
are pushed to disk. Dangling vnodes (unlinked files still in use) and
vnodes undergoing change by long-running syscalls are identified -- and
such filesystems are marked dirty on-disk while we are suspended (in case
power is lost, a fsck will be required). Filesystems without dangling or
busy vnodes are marked clean, resulting in faster boots following
"battery died" circumstances.
Tested by numerous developers, thanks for the feedback.


# 1.338 08-Feb-2018 deraadt

After flushing disks, try again to release all memory. We don't need to
save dirty memory to the hibernate space.


# 1.337 08-Feb-2018 deraadt

Toss all releaseable memory, because fragmentation can get in the way
of allocating the hibernate playpen.


# 1.336 08-Feb-2018 deraadt

sr_quiesce() is a new approach for ensuring that softraid drains
output to the disks.

This is part of a larger suspend/resume filesystem-safety diff, which
has been worked on for a couple of months already. Tests by job, krw,
beck, benno, and others. Sometimes even by snapshot users...


# 1.335 29-Nov-2017 kettenis

Add support for GPIO-signaled events.

ok mlarkin@


# 1.334 16-Nov-2017 jcs

Add PCI attachment for dwiic(4) needed by Intel 100 Series machines

ok kettenis


# 1.333 14-Nov-2017 kettenis

Add a few more PNP IDs to the skip lists.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.332 17-Aug-2017 stsp

Make dwiic(4) attach its ACPI parent dependencies.
This change makes the Asus E200HA keyboard work.

Original analysis and patch by Cesare Gargano
ok kettenis@


# 1.331 09-Aug-2017 dcoppa

Add "PNP0303" (8042 PS/2 Controller) to acpi_isa_hids[]

ok kettenis@


# 1.330 22-Jul-2017 jcs

acpibat: look for _BIX first before falling back on _BIF

ACPI 4.0 deprecated _BIF for battery status so some newer machines
are exporting _BIX which extends _BIF's package with a few extra
fields. When using _BIX, export the new discharge cycle count as a
sensor.

feedback from kettenis and anton
ok deraadt


# 1.329 20-Jul-2017 kettenis

Start a list of isa(4) device names such that we can avoid printing them
as "not configured".

ok mikeb@, millert@, deraadt@


# 1.328 08-Apr-2017 deraadt

Sensors are run as callbacks inside tasks. During suspend a sensor
could be running inside a driver that will be force-detached, or due
to tsleep end up disrupting the softstate/hardstate contract. At
suspend time, quisce all these callbacks by waiting for completion.
This issue has never been observed for real, but may be implicated in
suspend/resume failures.
ok kettenis guenther mlarkin


# 1.327 08-Apr-2017 deraadt

Two prototypes for acpi_maptable; merge to one place


Revision tags: OPENBSD_6_1_BASE
# 1.326 27-Mar-2017 deraadt

Now that hibernate_alloc() only has clean success/failure, don't
need to call hibernate_free() to clean up a partial mess.
ok mlarkin kettenis


# 1.325 13-Mar-2017 deraadt

allow ramdisks to build; ok jcs


# 1.324 12-Mar-2017 jcs

try to attach acpisbs first and if successful, don't attach acpibat


# 1.323 02-Mar-2017 natano

Add a new sysctl machdep.lidaction. The sysctl works as follows:

machdep.lidaction=0 # do nothing
machdep.lidaction=1 # suspend
machdep.lidaction=2 # hibernate

lidsuspend is just an alias for lidaction, so if you change one, the
other one will have the same value. The plan is to remove
machdep.lidsuspend eventually when people have upgraded their
/ets/sysctl.conf.

discussed with deraadt, who came up with the new MIB name
no objections mlarkin
ok stsp halex jcs


# 1.322 28-Feb-2017 natano

Untangle abstract sleep modes from ACPI sleep states. This paves the way
for further work in the area.

idea and ok deraadt
ok mlarkin


# 1.321 22-Feb-2017 jcs

add acpisbs, an acpi smart battery subsystem driver reading data
over smbus

currently disabled because it conflicts with acpibat

ok deraadt, kettenis


# 1.320 14-Jan-2017 kettenis

Revert aml_rdpciaddr changes; breaks several machines that were working
before.


# 1.319 11-Jan-2017 jsg

Increase the size of the buffers used for _HID and _CID strings to be
able to fit "Hyper_V_Gen_Counter_V1".

ok mlarkin@ deraadt@


# 1.318 08-Jan-2017 kettenis

Make aml_rdpciaddr() work for for devices behind bridges. The code now
uses the mapping between AML nodes and PCI devices that we establish.
Because _INIT methods may end up calling aml_rdpciaddr(), make sure we
create that mapping early. Also handle devices that aren't actually present.
These devices are now included in the mapping and reads will return an
all-ones patterm whereas writes are a no-op..

ok mlarkin@


# 1.317 25-Oct-2016 pirofti

Add the resource index to aml_parse_resource() callback function.

This is needed by an upcoming acpiec commit that handles machines breaking
the current ACPI specifications.

Change suggested by and ok kettenis@, guenther@


# 1.316 18-Sep-2016 guenther

If the APIC table (MADT) isn't present then ci_acpi_proc_id won't be set
when acpicpu tries to attach, leading to a NULL dereference. The ACPI
processor object on the Dells where this was reported is bogus and useless
anyway so apply the same test when deciding whether to attach to processor
objects as acpicpu would when matching them to cpus.

Problem reported by Eivind Eide (xenofil (at) gmail.com) and
Johan Huldtgren (johan+openbsd-bugs (at) huldtgren.com).
Testing by Eivind Eide.
ok kettenis@ mlarkin@


# 1.315 03-Sep-2016 naddy

Write the system time back to the RTC every 30 minutes.
This fixes the problem that long-running machines which were not
shut down properly would reboot with a badly offset system time.

hints and ok kettenis@


# 1.314 31-Aug-2016 mlarkin

Don't put a device in the wake device list if _STA says it's not there.

Fixes at least one machine that woke up from sleep / poweroff immediately.

ok kettenis@, tested by kettenis and martijn@


# 1.313 28-Jul-2016 kettenis

Store the acpi processor ID/UID in struct cpu_info, and use it to attach
acpicpu(4) drivers to the right cpu(4).

ok mlarkin@, guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.312 10-Jun-2016 kettenis

The power management timer has been made optional in ACPI 5.0A, so don't
attempt to attach acpitimer(4) if the timer isn't present.

Pointed out by jcs@

ok jcs@


# 1.311 08-May-2016 kettenis

Make sure devices listed by a device's _DEP method are attached before
we attach the device itself. The _DEP methode, introduced in ACPI 5.0,
is a hint that the device in question depends on OpRegion support from
the devices listed by _DEP, so we have to order them the proper way.
To prevent us from attaching those devices again when we encounter them
later walking down the device tree, keep track of our attempts to
attach a device driver to them using aflag in the aml_node structure.

ok guenther@


# 1.310 25-Apr-2016 pirofti

We attach acpihpet(4) and acpiec(4) without using the HID, so do not
report it in the dmesg.

OK deraadt@, mlarkin@.


# 1.309 02-Apr-2016 kettenis

Reduce dmesg spam by knocking out some acpi hardware IDs that don't need a
driver.

ok mpi@


# 1.308 29-Mar-2016 kettenis

Check if a device is present (using _STA) in acpi_foundhid. This prevents
us from reporting devices as "not configured" that aren't actually present,
and allows us to remove duplicated code from several drivers.

ok deraadt@, ok jsg@


# 1.307 29-Mar-2016 kettenis

Implement a cleaner way to prevent acpivout(4) from attaching if we have
a platform-specific method of controlling the backlight.

ok millert@, mlarkin@, jung@


# 1.306 28-Mar-2016 kettenis

Adding each and every new acpi device driver to acpi_foundhid() is
gettinga bit of the burden. And it really isn't how our autoconf
framework is supposed to work for busses that can be enumerated. All
the drivers already check for a matching _HID in their attach
function. So we can just drop the checks in acpi_foundhid() and let
the drivers sort themselves out.

Print a "not configured" line for devices for which we don't attach adriver.
This may be a bit much as there are devices for which we will not have a
driver. This will be removed if it causes too much dmesg spam.

ok deraadt@, mlarkin@


Revision tags: OPENBSD_5_9_BASE
# 1.305 17-Jan-2016 jsg

Take acpi_foundhid() outside of #ifndef SMALL_KERNEL so the ramdisk
kernel can find and attach dwiic(4) and sdhc(4).


# 1.304 17-Jan-2016 jsg

remove an unused global variable


# 1.303 14-Jan-2016 kettenis

Attach dwiic(4) on the Intel Bay Trail i2c controllers as well.


# 1.302 12-Jan-2016 kettenis

Improve emulation of PM registers on Hardware-redcued ACPI by emulating
SPL_TYPx, SLP_EN and WAK_STS using the new SLEEP_STATUS_REG and
SLEEP_CONTROL_REG registers. Makes the transition into S5 (powerdown) work
on the ASUS X205TA.

ok deraadt@, mlarkin@


# 1.301 12-Jan-2016 jcs

Add dwiic, a driver for the Synopsys DesignWare i2c controller found
on the Samsung ATIV Book 9 laptop. This initial version only
supports ACPI config/attachment.

Add ihidev, a HID-over-i2c driver largely based on uhidev. dwiic
handles attaching ihidev devices found in ACPI.

Add ims, a HID-over-i2c mouse/trackpad driver to get basic cursor
and button functionality from HID-compliant i2c trackpads.

ok kettenis deraadt


# 1.300 11-Jan-2016 kettenis

Make sdhc(4) attach to hardware IDs "80860F14" and "PNP0FFF".


# 1.299 09-Jan-2016 kettenis

Initial support for Hardware-Reduced ACPI. This skips the legacy mode checks
by emulating the PM_CNT register such that SCI_EN is always enabled. It also
skip attaching acpitimer(4) since the relevant registers are no longer there.

ok deraadt@, mlarkin@


# 1.298 06-Jan-2016 kettenis

Follow the spec and check the SCI_EN bit to see if ACPI has already
been enabled and don't bother enabling it again if it is. This should fix
systems that don't implement legacy mode and therefore set ACPI_ENABLE and
ACPI_DISABLE to zero.

ok mlarkin@, yasuoka@


# 1.297 23-Nov-2015 reyk

Back out the CRITICAL change, turn it back from 10% to 25%. Some
people with old/broken batteries didn't like this change, and the
underlying heuristics are wrong.


# 1.296 22-Nov-2015 reyk

The "CRITICAL" APM battery level, that is exported/faked by the ACPI
stack, is currently set to 25%. With 25% battery left, a typical
modern laptop will have at least one hour runtime left. It is an
arbitrary value, and other APM power layers in the kernel set CRITICAL
to something like 5% or 10%, so set it to 10% for now.

OK mlarkin@


# 1.295 28-Sep-2015 deraadt

In low-level suspend routines, set cold=2. In tsleep(), use this to
spit out a ddb trace to console. This should allow us to find suspend
or resume routines which break the rules. It depends on the console
output function being non-sleeping.... but that's another codepath which
should try to be safe when cold is set.
ok kettenis


# 1.294 06-Sep-2015 deraadt

clarify malloc arguments (base + length...)


# 1.293 28-Aug-2015 deraadt

fairly simple sizes for free(); ok tedu


# 1.292 20-Aug-2015 kettenis

Respect the access size when reading or writing to pci config space.
This version also makes sure writes are properly aligned, preventing
the panics seen with the previous incarnation of this change.

Fixes battery status passthrough in vmware, brightness keys on my x220
and who knows what else.

ok mlarkin@, deraadt@


Revision tags: OPENBSD_5_8_BASE
# 1.291 04-Aug-2015 deraadt

If we are setting ACPI_PM1_BM_RLD do it only once, and remember to
repeat this action at resume time.
ok mlarkin guenther


# 1.290 29-Jul-2015 deraadt

Something subtle in previous revision is locking up suspend or booting
on some laptops. Mailing lists threads mention x220 and Asus machines.
Reverting for now.


# 1.289 21-Jul-2015 kettenis

iRespect the access size when reading or writing to pci config space.
Fixes battery status passthrough in vmware and who knows what else.

Tested by many.

ok mlarkin@, deraadt@


# 1.288 17-Jul-2015 mlarkin

fix comments


# 1.287 26-Mar-2015 jsg

Use the 0x10 extended capability to determine if the trackstick
buttons are wired to the trackpad and need to be re-routed to the
trackstick. Without this change the buttons on 2015 thinkpads
get picked up as extended buttons that show up as scroll up/down.

Remove the X1 Carbon 2015 (LEN0048) and X250 (LEN0046) from the
top button area/soft buttons quirks list. Also avoid using the
quirk list entirely if the capability bit is set.

Based on work from jcs@ with feedback from mpi@
ok mpi@ jcs@


# 1.286 20-Mar-2015 kettenis

The ACPI standard says that a PCI function number of FFFF can be used to refer
to all functions on a device. This makes no sense in the context of accessing
PCI config space. Nevertheless stsp@ has a machine with AML that tries to
do config space reads from a device where _ADR encodes function number FFFF.
This is almost certainly buggy AML, but since Linux (and presumably Windows)
boot just fine on this machine we need to work around this. Linux effectively
treats function FFFF as function 7, which isn't present on this machine.
Instead we simulate a read from a non-existent device by returning a bit
pattern of all ones. Writes to function FFFF will still panic.

ok mlarkin@


# 1.285 16-Mar-2015 deraadt

Reduce use of SMALL_KERNEL in acpi, bringing acpiec driver and more gpe
handling into RAMDISK. This is now possible because the install media
has ample room. The goal is to reduce special cases where we may be
skipping (unknown) important operations...
ok mlarkin kettenis


# 1.284 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.283 07-Feb-2015 deraadt

New framework that allows hibernate to pass in entropy from it's fresh
boot.
ok mlarkin


# 1.282 06-Feb-2015 mlarkin

Fix a hibernate crash on some machines due to unmapping a page that
may not have been mapped previously (in the failure to hibernate case).

Also ensure that the lowmem ptp is mapped in all cases (not just MP).

ok kettenis


# 1.281 17-Jan-2015 deraadt

resume_randomness() before spinning up other cpus...


# 1.280 15-Jan-2015 jsg

Define a new wscons mouse type for Synaptics clickpad devices that lack
physical buttons. This will be used if the acpi pnpid for the mouse
matches a list provided by Synaptics found in Linux.

Combined with the xenocara changes this will remove the need for an xorg
config file for the x240/t440/t540 generation of thinkpads.

ok matthieu@ shadchin@ kettenis@


# 1.279 11-Jan-2015 kettenis

Tolerate checksum errors in ACPI tables (but complain about them). Apparently
there are machines out there with broken BIOSen with such checksum errors in
thier RSDT tables. Windows runs fine on these machines, so it probably does
not check. Linux ignores checksum errors as well (and prints a warning).

ok deraadt@, krw@


# 1.278 18-Dec-2014 deraadt

suspend and resume the RNG (at what looks like the right spots)
ok reyk djm


# 1.277 09-Dec-2014 doug

Replace some malloc(n*size,...) calls with mallocarray().

ok tedu@ deraadt@


# 1.276 06-Dec-2014 mpi

If "machdep.lidsuspend" is not set, do not go back to sleep if the lid
is still closed when resuming. This allows docked laptop to be resumed
without opening them.

ok mlarkin@


# 1.275 23-Nov-2014 mlarkin

If a machine has no _LIDs, then none can be open. This diff fixes a problem
where machines without any _LIDs were going back to sleep after hibernate
due to the recent suspend-if-lid-closed-on-resume diff. Noticed on qemu,
which doesn't have any _LIDs.

discussed with deraadt


# 1.274 18-Nov-2014 krw

Nuke yet more obvious #include duplications.

ok miod@


# 1.273 08-Nov-2014 mlarkin

If resuming from sleep (zzz/ZZZ) and the lid is still closed, go back to
sleep. This prevents accidental lid flex or slight opening in a backpack
from waking the machine up and leaving it resumed (powered on).

ok deraadt@


# 1.272 26-Sep-2014 kettenis

Rework piglet and pig allocation. Currently the piglet gets allocated
deep down in the suspend path, where it is really hard to recover from
allocation failure. So allocate the piglet early on in the suspend path.
Also change the piglet and piglet allocation functions to use km_alloc(9)
instead of doing pmemrange magic. This removes a bunch of code which, in the
case of the piglet allocation, is broken since it results in a NULL pointer
dereference. Also switch the piglet allocation to not wait. If we can't
allocate 16MB of phys contig memory on a halfway modern machine we're almost
certainly under a lot of memory pressure and we're better off not trying to
hibernate anyway.

ok mlarkin@


# 1.271 25-Sep-2014 kettenis

Bring back rev 1.249. Now that mp_setperf() has been fixed, it should be
safe to restore perflevel when resuming. Should fix the issues people
see with the processor running at full speed heating up the machine while
the apm command reports that the CPUs are running at a low speed.

ok dcoppa@, deraadt@


# 1.270 23-Sep-2014 kettenis

Release the acpi lock around calls to wsdisplay_suspend() and
wsdisplay_resume(). The X server may adjust the display brightness just
before/after the VT switch when suspending/resuming. In fact, it may
already be blocked adjusting the display brightness for some reason
unrelated to the suspend request.

The VT switch request will eventually time out, but the delay is annoying
and the failed VT switch will lead to problems after resume.

Needed for a future update of the xf86-video-intel driver.

ok mlarkin@


# 1.269 19-Sep-2014 kettenis

Use config_suspend_all(9).


# 1.268 08-Sep-2014 kettenis

Clear the PME Status bit on PCI devices in response to Device Wake events.
Fixes the interrupt storm on the 2009 Mac mini with WOL enabled on its
nfe(4) interface.

ok mlarkin@


Revision tags: OPENBSD_5_6_BASE
# 1.267 20-Jul-2014 mlarkin

Support hibernating to softraid crypto volumes.

much help and ok from deraadt@


# 1.266 16-Jul-2014 mlarkin

Reenable hibernate RLE support and flush+zero all memory after unpack.


# 1.265 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.264 12-Jul-2014 kettenis

Add a function to drop all clean pages on the page daemon queues and call
it when we hibernate.

ok mlarkin@, miod@, deraadt@


# 1.263 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.262 11-Jul-2014 mlarkin

Flush the buffercache to 16MB on hibernate and restore its previous max
size (kern.bufcachepercent) on resume, for better hibernate performance.

ok beck@


# 1.261 10-Jul-2014 blambert

remove unneeded header

ok mlarkin@


# 1.260 01-Jul-2014 kettenis

Adding an additonal delay after each access to SystemMemory or SystemIO
during autoconf makes no sense.

ok deraadt@


# 1.259 23-Jun-2014 kettenis

SystemMemory is used to access memory mapped registers on some machines, so we
must use the bus_space(9) API to access it instead of memcpy(9). Also make
sure we properly align access to fields that need more than byte alignment.

ok guenther@


# 1.258 21-May-2014 mlarkin

Fix invalid / missing format specifiers in several dnprintf statements
(used only when ACPI_DEBUG is enabled)

From a diff on tech@ by Fabian Raetz, with a minor change by me, tested
on i386 and amd64 with ACPI_DEBUG enabled and disabled.


# 1.257 25-Apr-2014 mlarkin

Defer the free of the hibernate structures until later, when it is safe to
do so.

Freeing the hibernate structures too early leads to inadvertent calls into
splx when we aren't ready for them yet. Found when trapping splx calls in
the resume path.

discussed with deraadt@ at m2k14


# 1.256 13-Apr-2014 mlarkin

Fix a badly worded debug message that implied the entire machine was going
to sleep, instead of just the acpi thread going to sleep.


# 1.255 31-Mar-2014 mpi

Change the order of operations during a suspend/resume cycle and
call bufq_quiesce() after executing the DVACT_QUIESCE handlers.

This should be safe since no disk nor controller drivers have such
handler but it will allow us to detach sd(4) devices attached to a
USB bus.

Another benefit pointed out by kettenis@ is that drivers that need
to read a firmware from the disk should be able to do it at resume
time in a DVACT_WAKEUP handler.

ok kettenis@, deraadt@


# 1.254 13-Mar-2014 dlg

get rid of the assumption that the head of the alldevs list is the
"mainbus" device. this breaks when mpath is enabled because it
attaches before mainbus and therefore takes the head position.

have autoconf provide device_mainbus() which looks up mainbus_cd,
and use that instead.

discussed with deraadt who just wants mpath stuff to move forward
despite there being many ways to shine this particular turd.


Revision tags: OPENBSD_5_5_BASE
# 1.253 21-Feb-2014 deraadt

Do the lid control further on in the resume sequence, since this is
AML, and will do WAITOK operations. While cold, that is a bad idea.
We seem to be safe with respect to the spec, I do not see any event
debounce issues here.
ok kettenis, tested by many


# 1.252 20-Jan-2014 claudio

Instead of checking sc->sc_revision <= 1 check that the
fadt->hdr_revision <= 1. This is a better check since it checks
the version of the proper descriptor. This allows the alix APU
to reboot since those boards have currently a wrong sc->sc_revision.
Committing this now so that it can be tested easier.
With and OK jsg@ also fine by derradt@


# 1.251 24-Dec-2013 kettenis

If the FADT has its SMI_CMD set to zero, assume we're only ACPI-only hardware
and don't need to disable SMI ownership of the ACPI hardware registers.

ok mlarkin@


# 1.250 23-Dec-2013 kettenis

Revert previous commit. It makes resume hang on some MP systems running
GENERIC.MP.


# 1.249 16-Dec-2013 kettenis

Restore hw.setperf upon resume like we do for apm(4) on i386. Seems we need
to do this fairly late in the resume to avoid hangs.

ok deraadt@, mlarkin@


# 1.248 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


# 1.247 06-Nov-2013 mpi

Add support for Power Resources for Dx states and the necessary hook
for PCI devices. This hook should be called twice, before and after
changing the power state of a PCI device.

Before setting the device to the new state, the ACPI layer will notify
every power resources linked to the device for that state and make sure
they are turned "_ON". After changing the state of the device, it will
decrement the reference of every power resources linked to that device
for the old state and turn them "_OFF" if they are no longer referenced.

This fixes the no-USB after resume problem seen on various ThinkPad,
problem initialy diagnosed with Alexander Polakov.

ok kettenis@, deraadt@


Revision tags: OPENBSD_5_4_BASE
# 1.246 01-Jun-2013 mlarkin

Stop printing ACPI wakeup devices in dmesg after the 16th wakeup device.

Helps with VMware VMs that report hundreds of wakeup devices.

ok reyk@, deraadt@


# 1.245 31-May-2013 bluhm

Fix the build for a kernel without wd and pciide in its config.
OK deraadt@ tedu@


# 1.244 30-May-2013 deraadt

Enforce ca_activate tree-walks over the entire heirarchy for all events,
cleaning up some shutdown-hook related code on the way.
(A few drivers related to sparc are still skipped at kettenis' request)
ok kettenis mlarkin, tested by many others too


# 1.243 18-Apr-2013 deraadt

pin the acpi thread to the BSP, because AML and SMI cannot be trusted. I
was certain we were doing this already, but it turns out no...


Revision tags: OPENBSD_5_3_BASE
# 1.242 24-Dec-2012 guenther

In acpiioctl(), Return though the splx() path if suser() returns an error

ok deraadt@


# 1.241 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


# 1.240 04-Oct-2012 ehrhardt

Use information provided by ACPI to attach secondary PCI host bridges.

ok kettenis@


# 1.239 07-Sep-2012 kettenis

Add a new function called acpi_pci_min_powerstate() that returns the lowest
power state for a PCI device for the power state (currently S3 or S4) we're
transitioning into. This function is probably too simplistic; there is a lot
of (silly) complexity in the ACPI spec.

ok deraadt@


Revision tags: OPENBSD_5_2_BASE
# 1.238 13-Jul-2012 pirofti

Disable acpivideo(4) on ASUS laptops.

There are some models that panic whenever an event is received.
Some cope, some don't. In order to get this right acpiec(4) requires
major rework. Until then let machines boot.

Discussed with deraadt@.


# 1.237 11-Jul-2012 mlarkin

for now, restrict hibernate to root in the acpi ioctl kernel interface.
normal rules for controlling this interface through apmd still apply.

ok deraadt@


# 1.236 09-Jul-2012 deraadt

tiny little oops in untested hibernate block


# 1.235 09-Jul-2012 deraadt

clean up ioctl() case statements


# 1.234 09-Jul-2012 deraadt

acpi_disable_onegpe() is not used; ok pirofti


# 1.233 24-May-2012 kettenis

If _MIN > _MAX consider a WordBus resource to be invalid. Fixes a panic on
the IBM x3550-M4.

ok mikeb@


# 1.232 29-Mar-2012 mlarkin

Missing #ifdef in previous diff


# 1.231 29-Mar-2012 mlarkin

Prevent hibernation attempts on machines with unsupported disk controllers.
The same check is also performed later in the hibernate sequence, but by
then it's too late to cleanly unwind (presently).

ok deraadt


# 1.230 26-Mar-2012 deraadt

simplify conditionals in acpi_sleep_state()
ok mlarkin


# 1.229 26-Mar-2012 deraadt

hook in the hibernate request code; half of this diff is from mlarkin
ok mlarkin


Revision tags: OPENBSD_5_1_BASE
# 1.228 20-Sep-2011 deraadt

Very early in suspend while the clock is still ticking realtime,
save the clock back to the rtc. The zaurus already did this.
ok phessler miod kettenis


Revision tags: OPENBSD_5_0_BASE
# 1.227 02-Jul-2011 nicm

kqueue attach functions should return an errno or 0, not a plain 1. Fix
the obvious cases to return EINVAL and ENXIO.

ok tedu deraadt


# 1.226 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.225 16-Jun-2011 pirofti

Import acpitoshiba driver.

This is originally written by Hiroyuki Aizu, ported and integrated
in our tree by Javier Vazquez <javama4 AT gmail> with tweaks and
suggestions by me.

Import prodded by deraadt@ and the lack of testers on tech@.


# 1.224 27-Apr-2011 jcs

attach acpithinkpad to newer lenovo models like the x120e

ok deraadt@


# 1.223 22-Apr-2011 jordan

Fix uninitialzied variables and formatting strings (-Wxxx errors)


Revision tags: OPENBSD_4_9_BASE
# 1.222 02-Jan-2011 jordan

Re-add code for ACPI taskqueue, eliminates need for state vars in acpi softc
Serialize processing ACPI tasks for sleep/powerdown/GPE event
ok deraadt@


# 1.221 31-Oct-2010 guenther

Revert last commit: it breaks resume on Thinkpads
"then please back it out" deraadt@


# 1.220 26-Oct-2010 jordan

Add task queue for ACPI gpe and notify handlers
This fixes eject on Dell Latitude dock and an issue on the Dell Mini
battery update.
ok deraadt


# 1.219 07-Oct-2010 jordan

Create separate functions for enabling wake and runtime gpes.
Remove old suspend/resume gpewalk function
ok deraadt, mlarkin


# 1.218 05-Oct-2010 jordan

split acpi_enable_onegpe to separate enable/disable functions
ok deraadt


# 1.217 05-Oct-2010 jordan

Added acpi_disable_allgpes function to clear GPE status
ok deraadt


# 1.216 05-Oct-2010 deraadt

clean up acpi_softc use more


# 1.215 05-Oct-2010 deraadt

delete a bit of #if 0 code


# 1.214 05-Oct-2010 deraadt

hide use of our global acpi_softc pointer a bit better


# 1.213 29-Sep-2010 deraadt

grab the acpi thread lock very early in acpi thread startup; not that it
is likely that anyone will race us... but why be careless.
ok kettenis


# 1.212 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.211 29-Aug-2010 jasper

- add a check for FWRITE here too.

ok miod@


# 1.210 27-Aug-2010 kettenis

Make PCI Power Management optional, and only enable it when acpi(4) attaches.

ok deraadt@


Revision tags: OPENBSD_4_8_BASE
# 1.209 08-Aug-2010 kettenis

Close race between the acpi thread and wscons ioctls running in process
context. We might rip this out post-release and replace it with a solution
that serializes things in a more obvious way.

ok deraadt@, miod@ and "those crazy texans who ok anything"


# 1.208 08-Aug-2010 deraadt

Report the acpi sleep states when on a RAMDISK; ok krw


# 1.207 07-Aug-2010 kettenis

Enforce acpi register access rescrictions.

ok mlarkin@, deraadt@, tested by marco@


# 1.206 07-Aug-2010 deraadt

upon resume, notify all the acpiac and acpibat drivers to update
their status. some laptops do not do this automatically.
ok kettenis canacar


# 1.205 06-Aug-2010 marco

Always call _PSW on _LID devices that support it to enable lid open to
resume the machine. Conversely disable it when the machine wakes up.

Tested by several ok deraadt


# 1.204 06-Aug-2010 deraadt

As part of a revamp of the PM1/GPE code, I write this diff to look for
unmanaged EN & STS bits in the PM1 register at interrupt time and report
them. As a side effect this splits the STS acknowledgement into two writes
(for power, and sleep) instead of one. The printf that is added (to spot
unmanaged STS bits) has not yet been triggered as far as we know.

Before the "write to PM1 registers at the right offset" diff went in, this
was not neccessary. But newer thinkpads do not have a working soft power
button without this diff.

We have no idea why.
ok mlarkin kettenis


# 1.203 05-Aug-2010 deraadt

PM1 power button events were generating 'fake events' to acpibtn PWRB,
but if there is no such device these were being tossed. Instead, arrange
for all power button handlers to flag a softc variable which is processed
by the thread; so that one place will know to request a powerdown (psignal
to init)
ok marco mlarkin


# 1.202 05-Aug-2010 deraadt

rename a variable which is annoyingly poorly named


# 1.201 05-Aug-2010 mlarkin

Write to the correct offset for various ACPI pmreg registers. Writing to
the correct register does properly clear the wake status bits and fixed
event bits, which allows some machines to have more than 1 suspend cycle.

bug spotted by deraadt@

tested by deraadt@ and myself on a half-dozen machines and others on
hackers@

ok deraadt@, marco@


# 1.200 05-Aug-2010 deraadt

Fix the power button. The pm1 register is 16 bits in size.
My mistake, a while back.
tested by krw


# 1.199 04-Aug-2010 kettenis

Print supported sleep states.

ok marco@, phessler@, deraadt@


# 1.198 03-Aug-2010 mlarkin

Use the proper flag for re-enabling certain hardware events (power btn,
sleep btn). The (incorrect) flag prevoiously used coincidentally had the
same value, so this shouldn't cause any different behavior than before.

tested on a variety of machines (i386, amd64, sp, mp) - no behavioral
change seen

ok deraadt@


# 1.197 28-Jul-2010 marco

Make all gasio failures look and behave the same.


# 1.196 28-Jul-2010 mlarkin

Delay a little longer on suspend before giving up. Fixes machines that
sometimes don't suspend because the sleep transition takes too long
(symptoms like failing to sleep but the backlight stays on).

ok deraadt@


# 1.195 27-Jul-2010 deraadt

remove XXX; EC can now be attached early when needed; ok jordan


# 1.194 27-Jul-2010 deraadt

more brackets feels good


# 1.193 27-Jul-2010 deraadt

XXX annotate a block in acpi_enter_sleep_state() that is pretty worrying


# 1.192 27-Jul-2010 deraadt

horrific indentation that nearly made me puke


# 1.191 27-Jul-2010 mlarkin

Set the sleeping indicator light on machines that support it, in case SMI
doesn't do it for us. Shows the 'moon' LED on older Thinkpads when
sleeping.

Tested on T43p, W500, T510 by me and on X61s by deraadt@.

ok deraadt@


# 1.190 27-Jul-2010 jordan

Early initialization of acpiec if ECDT table exists
Fixes hang when booting thinkpads while docked
ok deraadt


# 1.189 26-Jul-2010 deraadt

remove merge error which was commited


# 1.188 26-Jul-2010 pirofti

Add support for Sony ACPI hotkeys via a new driver: acpisony(4).

Currently it only works for the suspend button and tries to do right for
the brightness events, but I haven't found a Sony laptop that like to do
right so far.

In the future I want to make the brightness keys work on all Sony's and
also add support for the zoom hotkeys and whatever other funky keys I can
find on those things.

Okay deraadt@.


# 1.187 22-Jul-2010 deraadt

pretty large cleanup of SMALL_KERNEL


# 1.186 21-Jul-2010 deraadt

in the acpi GPE handler, late ack level interrupts not edge interrupts.
Duh.


# 1.185 21-Jul-2010 deraadt

Merge the edge and level gpe handlers into one (now possible since the
gpe structure tells us whether the gpe is edge or level)


# 1.184 21-Jul-2010 deraadt

delete #if 0 code we are not going to use; ok marco


# 1.183 20-Jul-2010 deraadt

make the mask code for acpi_enable_onegpe() match other similar code


# 1.182 20-Jul-2010 deraadt

Handle the acpi interrupt controller with proper edge/level handling,
wrapped in spltty to avoid racing against the interrupt controller.
Repair the gpe bit masking code while there, and do operations in the
right order
ok kettenis mlarkin, help from jordan at figuring out the order of
operations.


# 1.181 20-Jul-2010 matthew

When we're preparing to go to sleep, only call bufq_restart() if other
preparations failed.

ok kettenis@, thib@, deraadt@


# 1.180 19-Jul-2010 deraadt

permit compilation with ACPI_DEBUG


# 1.179 19-Jul-2010 deraadt

there is no need for a seperate dsdt_softc pointer; dsdt does not have a
softc but is just a part of the single acpi driver.
ok jordan


# 1.178 19-Jul-2010 deraadt

acpi_delay() is no longer needed; ok jordan


# 1.177 18-Jul-2010 mlarkin

Clear event status on resume, to avoid going back to sleep/reboot/shutdown
immediately after resume. Tested by a bunch of people on hackers@ on
various hardware. Fixes at least Toshiba NB200 and Gateway LT3103u.

ok marco


# 1.176 13-Jul-2010 deraadt

rename some thread related interfaces so they are logical


# 1.175 10-Jul-2010 jordan

Verify _ADR bdf is correct (some systems return 0xffff)
Works on Dell x9xx and Studio 1555


# 1.174 09-Jul-2010 jordan

Clear the noise


# 1.173 08-Jul-2010 jordan

Add mapping for ACPI device to PCI bdf (match autoconf tree)
Simplify resource parsing function to use buffer argument
Convert namespace linked lists to use queue macros
ok marco@, deraadt@


# 1.172 06-Jul-2010 deraadt

Use spltty() to lock downcalls from apm against the information being
modified by the acpi thread.
ok kettenis


# 1.171 06-Jul-2010 deraadt

Don't use a workq to do the suspend, because it races aginst the acpi
thread. Instead, just tell the acpi thread to do the suspend for us.
This makes apmd & zzz work correctly. While here, have acpithinkpad
attempt to post the event to apm before prompting the suspend itself.
ok kettenis marco mlarkin


# 1.170 05-Jul-2010 mlarkin

Reset register SCI_EN on resume. The ACPI spec clearly states we don't have
to do this, but many machines don't, or forget to, which leads to all sorts
of problems on resume (broken battery stats, overheating, failure to resume
more than once, etc).

tested by many, ok marco@, deraadt@


# 1.169 01-Jul-2010 thib

do bufq quiesce again, since vnd/the bufq_done call in
biodone has been fixed.


# 1.168 01-Jul-2010 jordan

Backout recent AML changes, breaks on R210 and others


# 1.167 01-Jul-2010 jordan

Simplified aml_parse_resource call, cleanup code
ok marco


# 1.166 01-Jul-2010 jordan

Fix subordinate bus # for multi-root PCI buses
ok marco, kettenis


# 1.165 30-Jun-2010 thib

Disable/partially backout the bufq quiesce changes as this
is causing havoc with vnds and release must be buildable.


# 1.164 29-Jun-2010 deraadt

Fix ramdisks (new code was placed inside a SMALL_KERNEL chunk)


# 1.163 29-Jun-2010 jordan

Add support for mapping ACPI to PCI devices
ok kettenis, deraadt


# 1.162 29-Jun-2010 kettenis

Call bufq_quiesce() on suspend to stop all I/O and bufq_restart on resume.
Makes suspend/resume work much better while doing disk I/O.

ok mlarkin@


# 1.161 29-Jun-2010 tedu

60 is a more reasonable guess for number of minutes in an hour than 100.
spotted by TeXitoi. ok deraadt


# 1.160 27-Jun-2010 jordan

Add _acpi_matchhids routine, precursor to pci device match


# 1.159 27-Jun-2010 mlarkin

Clear ACPI fixed event status on resume (power buttons/etc) so that some
machines don't immediately resume after suspending to S3.

Tested by kettenis@ and I on 5 or 6 machines, deraadt@ "yeah, looks good"


# 1.158 27-Jun-2010 jordan

Cleaned up acpi_maptable, adds to table list directly


# 1.157 07-Apr-2010 deraadt

If we have done a wsdisplay_suspend(), and ACPI decides it does not want to
suspend, we must roll back by calling wsdisplay_resume() or the virtual
console switching will be locked forever
ok oga


# 1.156 07-Apr-2010 kettenis

Make suspend/resume work on MP machines (running an MP kernel). Joint work
from mlarkin@ and me, with some amd64 fixes thrown in by deraadt@

ok marco@, deraadt@, pirofti@, mlarkin@


# 1.155 31-Mar-2010 kettenis

Bring /dev/apm support in line with apm(4). Make the suspend button send
a suspend request event to apmd(8) instead of suspending immediately. Also
keep track of whether /dev/apm and /dev/apmctl are currently open, such that
we can still suspend immediately if apmd(8) isn't running.

ok deraadt@, marco@, pirofti@, jsing@, oga@


# 1.154 30-Mar-2010 oga

Prevent the apmd/x races for good.

When we hit suspend time, go through all wsdisplays on the system. if
they are in mode MAPPED, but not MODE_DUMBFB then if possible do a full
vt switch to a !mapped vt, and prevent switching back until resume time.
This has to be called from MD code because this involves userland
running so that X can run the vt switch signal handler. This way, any
case where we are using the "poke registers from userland" model, we
will not be on the hardware when we go down, so the kernel can actually
handle thing properly.

Tested on several acpi laptops (by kettenis@ and ian@), x40 (me and
beck@ at LEAST) and zaurus (me). Maybe others, but if so I forgot who at
this time..

Idea from deraadt somewhere over the Faroe Islands (I thought of a
similar thing myself a while ago). Much prompting from him. Ok and
comments miod@


# 1.153 25-Mar-2010 oga

Fix & vs. && in the kqfilter code.

Based on a comparison with the apm code.

ok deraadt@, kettenis@


Revision tags: OPENBSD_4_7_BASE
# 1.152 26-Nov-2009 mlarkin

Visual (and on some machines, audio) feedback while the machine is
resuming via calls to aml _SST method. This also makes the "moon" LED on
thinkpads to blink during resume and cease when resume is completed.

ok pirofti@


# 1.151 26-Nov-2009 deraadt

Toggle into "cold" when we we disable interrupts, because tsleeping
in the AML interpreter and context switching into userland processes
tends to sometimes pop the ACPI trampoline page out of our address
space. Makes lots more machines work.
ok kettenis mlarkin


# 1.150 24-Nov-2009 jsg

in the resume path evaluate _WAK before dealing with device drivers
like the spec says, not after.

ok mlarkin@


# 1.149 24-Nov-2009 deraadt

abort suspends on SMP systems until that gets written; ok mlarkin


# 1.148 23-Nov-2009 mlarkin

pci should return the result of it's children's suspend/resume calls back
to its parent.

handle suspend failure case and unwind if devices have an activate function
that returns failure.

ok deraadt@, kettenis@


# 1.147 23-Nov-2009 mlarkin

acpi* devices need to call their children on suspend/resume.
ok deraadt@


# 1.146 23-Nov-2009 pirofti

Remove ACPI_SLEEP_ENABLED checks.

This enables by default the suspend/resume paths in the kernel.

Okay deraadt@.


# 1.145 23-Nov-2009 deraadt

knf and spacing that are super annoying


# 1.144 23-Nov-2009 mlarkin

Hook up Theo's new children suspend/resume function, as well as more ACPI
suspend/resume glue.

ok deraadt@


# 1.143 26-Oct-2009 deraadt

Do not do strncmp comparisons on dv_xname because longer device names which
look similar could arrive in the future. Instead, compare directly against
dv_cfdata->cf_driver->cd_name
Issue originally spotted by miod


# 1.142 26-Oct-2009 jordan

Verify checksum+address when loading ACPI tables.
Some systems had invalid entries in RSDT/XSDT.
ok marco@


# 1.141 23-Jul-2009 cnst

New aibs(4) driver for ASUSTeK AI Booster (ACPI ATK0110) hardware monitoring,
with sensor state support through limits provided by the ACPI.

Tested on several ASUS motherboards kindly networked by Sam Fourman Jr.

ok deraadt marco jordan


Revision tags: OPENBSD_4_6_BASE
# 1.140 03-Jun-2009 pirofti

Import acpi power resource driver. This is needed for suspend/resume logic.

Okay jordan@ and marco@.


# 1.139 03-Jun-2009 pirofti

Add acpivideo support. This adds brightness support for all laptops
except thinkpads, they will use the acpithinkpad driver. The driver is
also hooked into wsconsole. So brightness can be adjusted via:

$ wsconsctl display.brightness=<percentage>

This is very helpfull on some laptops that have a nasty bios and get two
steps instead of one when pressing the brightness button.

Tested on various dell, fujitsu, acer, samsung and other laptops.

Okay marco@, miod@. Suggestions from kettenis@.
Lots of reviews and help from miod@, thanks!


# 1.138 03-Jun-2009 jordan

Added ejectable bay for IDE cdroms
ok marco@


# 1.137 30-Apr-2009 marco

Oga changed apm ioctls and forgot to grep. Temporary work around by hitting
the sleep button when we get any request. This interface will chnage at
some point.
ok oga


# 1.136 19-Apr-2009 krw

Call acpi_sleep_walk() when powering down just like when entering
any other sleep state. This tells acpi wake devices to not wake up
the box until manual powering up resets their status. Makes my Dell
GX520 stay powered off in response to 'halt -p'.

In snaps for a while.

ok marco@ (several times) deraadt@


# 1.135 17-Apr-2009 pirofti

Add device ranking support for future sleep/wake developments.

okay marco@


# 1.134 11-Mar-2009 jordan

Cleanup.. use aml_evalinteger instead of aml_evalname
ok marco@


# 1.133 10-Mar-2009 jordan

Fix cut/paste error.. call _BFS not _PTS
ok marco@


Revision tags: OPENBSD_4_5_BASE
# 1.132 21-Feb-2009 marco

overzealous cut/paste; found by Vladimir Kirillov <proger@uaoug.org.ua>


# 1.131 20-Feb-2009 marco

Oops didn't mean for Debugger() to remain there.


# 1.130 19-Feb-2009 marco

suspend/resume bits so that we can develop this in tree. This is disabled.
code from mlarkin and me
help from art,toby,jordan and several others
ok jordan, go for it deraadt


# 1.129 10-Feb-2009 jordan

Fix null pointer for shutdown if acpi not enabled
ok marco@


# 1.128 04-Feb-2009 kettenis

Some systems that claim to be ACPI 2.0 apparently don't provide an XSDT
address. Make the code fall back on the RSDT on those systems.

From Joerg Niendorf <p3y80@internode.on.net>. Fixes PR 5986.


# 1.127 17-Jan-2009 krw

A couple of stray malloc/memset -> malloc(M_ZERO), a bit of knf and
lose a couple of unnecessary casts. No functional change.

Feedback from kettenis@

ok kettenis@ marco@


# 1.126 06-Nov-2008 marco

Collapse all sanity checks of hids into one function.

Originaly from jordan


Revision tags: OPENBSD_4_4_BASE
# 1.125 02-Jul-2008 fgsch

add acpivideo. for now almost an empty skel so others can work on
suspend/resume. eventually it will also manage output switching and
brightness where supported. prodded by marco@


# 1.124 11-Jun-2008 marco

Fix $OpenBSD$ for once and for all


# 1.123 08-Jun-2008 deraadt

aaa_dev was being set from an uninitialized variable; ok toby


# 1.122 07-Jun-2008 marco

Kill ACPIVERBOSE. It was never used and is worthless to boot.

comments jmc
ok toby


# 1.121 06-Jun-2008 marco

Shrink acpi some more for floppies and while here re-enable acpi on the
floppies. Fixes crazy sony vaio kettenis has.

tested by deraadt
ok deraadt jordan kettenis


# 1.120 01-Jun-2008 marco

Fix \n nit in SMALL_KERNEL.


# 1.119 01-Jun-2008 marco

Re-enable new parser. Lots of testing by various folks. Fits all media
now too.

ok kettenis deraadt


# 1.118 16-May-2008 dlg

back out jordans new parser.

it didnt have enough oks from the right people, it did break machines
that were previously working, and it bloated the RAMDISKS and install
media beyond the constraints of the media.

requested by deraadt@


# 1.117 14-May-2008 jordan

Adding new ACPI Parser code
Old guts of code still exists, needs to be torn out next
ok marco@
ok brad@


# 1.116 27-Apr-2008 jcs

add acpithinkpad(4), an acpi support driver for ibm/lenovo
thinkpads.

provides functionality that the bios did on older, non-acpi
thinkpads like respond to screen brightness keys and toggle the
bluetooth device on and off.

ok deraadt@ and marco@


# 1.115 24-Apr-2008 jsing

Introduce acpiasus(4), a driver for the ACPI based hotkeys found in many
ASUS laptops (including the ASUS EeePC) - largely based on NetBSD's
asus(4) driver. On the ASUS EeePC this allows us to enable/disable
wireless, change screen brightness and use the volume keys.

ok jsg@, weingart@


# 1.114 12-Apr-2008 kettenis

Get rid of acpi_s5 global variables; simply send SIGUSR2 instead of SUGUSR1
to tell init(8) to power down the machine.

ok krw@


Revision tags: OPENBSD_4_3_BASE
# 1.113 21-Jan-2008 jsg

Check against RESET_REG_SUP in FADT before doing the acpi reset dance
after all, not doing so appears to break more machines than it fixes.

ok marco@


# 1.112 05-Jan-2008 kettenis

Never attach more acpicpu(4) devices than cpu(4) devices. Always attach
the first acpicpu(4) device on non-MULTIPROCESSOR kernels, regardless of its
ACPI processor ID.

ok mikeb@


# 1.111 05-Dec-2007 deraadt

Hang acpi(4) below bios(4) instead of mainbus(4). This lets us move acpi(4)
before pcibios(4), and then.... if acpi(4) attaches, skip pcibios(4) since
it messes the machines up. Fixes claudio's HP dl320 G5, and almost assuredly
others.
ok kettenis


# 1.110 05-Dec-2007 deraadt

repair acpiopen/acpiclose and friends to handle the /dev/apmctl interface,
so that apmd(8) can actually work correctly against acpi(4), and thus,
even do the fancy -C/-A stuff; also tested by jmc


# 1.109 02-Dec-2007 fgsch

fix the trailing space for real now.


# 1.108 02-Dec-2007 fgsch

avoid trailing spaces on output; marco@ ok.


# 1.107 27-Nov-2007 kettenis

Fix off-by-one that made us attach acpicpu0 and acpicpu1 on GENERIC kernels.

ok marco@


# 1.106 25-Nov-2007 jsg

When available and ACPI is enabled use the ACPI reset method
to reboot. This is required for some quirky sis machines
and likely others to reboot.

Tested by many. 'commit early commit often' deraadt@


# 1.105 19-Nov-2007 deraadt

sigh, amd64 still uses that stupid acpi_enabled variable...


# 1.104 19-Nov-2007 deraadt

softc's come initialized to 0, you know...


# 1.103 19-Nov-2007 deraadt

store revision; from jsg


# 1.102 19-Nov-2007 kettenis

Initialize sc_facs earlier.

ok deraadt@, beck@, canacar@


# 1.101 16-Nov-2007 deraadt

fix the bus_space #define nightmare, so that amd64 and i386 are much more
uniform. as a result shared code like acpi needs less #ifdef's
ok marco kettenis


# 1.100 15-Nov-2007 deraadt

remove the #ifdef ACPI_ENABLE. the EC codepath is either safe enough (and
will be improved), or it is unsafe because AML assumes it must be there
ok almost everyone


# 1.99 15-Nov-2007 mikeb

Make acpicpu(4) attach to the existing CPUs only.

Problem reported by form@. Fix idea by kettenis@.
Lots of help from deraadt@. Tested by deraadt@, form@ and me.

ok deraadt marco


# 1.98 14-Nov-2007 deraadt

spacing


# 1.97 12-Nov-2007 deraadt

correct : vs , in attach-time messages


# 1.96 06-Nov-2007 deraadt

all ACPI_ENABLE kernels need acpi_interrupt()


# 1.95 06-Nov-2007 deraadt

move acpi_read_pmreg() and acpi_write_pmreg() out of SMALL_KERNEL so
that acpi ramdisks can compile (obvious solution)


# 1.94 05-Nov-2007 canacar

Establish the acpi interrupt earlier. Fixes problems with some
GENERIC.MP kernels not getting acpi interrupts. This is a
workaround until the real issue with late establishment is fixed.
ok weingart@, beck@, gwk@


# 1.93 03-Nov-2007 jordan

Added support for displaying wakeup devices
ok beck@,weingart@,gwk@


# 1.92 11-Oct-2007 kettenis

When walking the tree yo find _PRT methods, check whether the device is
actually enabled and functioning. Similar to what we do for _INI.

ok weingart@, beck@


# 1.91 08-Oct-2007 krw

More simple memset(,0,) -> M_ZERO changes. In this batch move to
size(*p) as the first malloc() parameter where p is declared locally
and thus easy to check. Add M_ZERO to gpe_table allocation in acpi.c
even though there is no obvious bzero or memset nearby.


# 1.90 13-Sep-2007 mk

Remove unnecessary memset() (see aml_evalnode(9)) and a bit of KNF.

ok toby


# 1.89 13-Sep-2007 weingart

Implement ACPI 6.5.1 spec tree walk for _STA and _INI.
Fixes mk's laptop. No regressions so far (thank you to the
testers).

ok gwk@, mk@, marco@


Revision tags: OPENBSD_4_2_BASE
# 1.88 17-Apr-2007 mk

Now that acpidock no longer hangs machines during boot (at least
according to the reports I've been getting) it should be safe to enable
it again, so remove the #if 0 stuff around it.

ok marco


# 1.87 11-Apr-2007 jordan

Added changes for C-State
ok marco@


# 1.86 26-Mar-2007 marco

Calculate minutes left more accurately.

From giovanni <qgiovanni@gmail.com>


Revision tags: OPENBSD_4_1_BASE
# 1.85 03-Mar-2007 mk

Stop looking for acpidock until we fix that stuff that breaks all pre-60
series ThinkPads with acpidock(4) enabled.


# 1.84 22-Feb-2007 marco

We were getting no interrupts in .MP. There are 2 fixes; one in
acpi_machdep.c and move the interrupt establishment down after ioapic
has been initialized.

ok jordan


# 1.83 21-Feb-2007 kettenis

Make an acpi-enabled GENERIC.MP work on systems that only support PIC mode.

ok marco@


# 1.82 19-Feb-2007 jordan

Added acpi_map_address function for mapping GAS/IO registers
ok marco@


# 1.81 18-Feb-2007 jordan

Added dynamic perfect hash for opcodes
ok marco@


# 1.80 18-Feb-2007 marco

Shave off another K pre compression.
Shuffle SMALL_KERNEL functions around so that we only have a single #ifndef


# 1.79 17-Feb-2007 marco

Add SMALL_KERNEL throught acpi to make it fit on boot media.
ok deraadt


# 1.78 13-Feb-2007 marco

Add smarter _INI code. This helps with some dissapearing devices.

ok kettenis jordan


# 1.77 31-Jan-2007 gwk

Introduce acpi_hasprocfvs thats "ACPI Has Processor Frequency and Voltage
Scalling" a global flag set by acicpu attach to indicate that ACPI is
handling fvs through the _PSS objects. This will fix disappearing
EST/powernow when booting kernels with acpi enabled, as acpicpu isnt
compiled into GENERIC yet. Also in cases where acpicpu is built in and
legacy EST/powernow work but acpi is missing the _PSS object, the legacy
routines will be used.

tested by dim@, and Nick Nauwelaerts <nick AT nauwelaerts.net>
ok marco@, dim@


# 1.76 22-Jan-2007 mk

Actually look for docking stations by looking for _DCK nodes.

ok marco


# 1.75 02-Jan-2007 marco

On some systems EC must be attached as the first device. Move EC from
generic _HID devices to its own routine to force first attach. Bug found
and debugged by joris@

Code mostly written by joris@


# 1.74 26-Dec-2006 marco

Add polling to devices that require it such as acpibat and acpitz.
Use the same tsleep/wakeup mechanism as the interrupt code to keep it non-
concurrent.


# 1.73 21-Dec-2006 deraadt

even more knf love


# 1.72 21-Dec-2006 marco

Remove some leftover acpiec gpe/interrupt code.


# 1.71 20-Dec-2006 deraadt

oops, name already used


# 1.70 20-Dec-2006 deraadt

rename __acpi_enable_gpe(); ok marco


# 1.69 20-Dec-2006 deraadt

acpi_unmap_pmregs() is not used, ok marco


# 1.68 19-Dec-2006 deraadt

simplify some low-level methods substantially (shrinking code); ok marco jordan


# 1.67 06-Dec-2006 mk

malloc(..., M_NOWAIT) with no NULL check is bad. If allocation fails,
complain, clean up, and bail out of attach.

input and ok gwk


# 1.66 28-Nov-2006 jordan

Fixed clearing arg for acpi_init_pic
ok kettenis@


# 1.65 27-Nov-2006 kettenis

Don't spam dmesg.


# 1.64 27-Nov-2006 jordan

Fixes for acpiprt routing
Post-parse fixup for namerefs
ok kettenis@


# 1.63 25-Nov-2006 marco

Don't handle acpiev events inside the gpe handler since this causes deadlocks.

ok jordan@


# 1.62 15-Nov-2006 kettenis

Hook up acpi apic interrupt routing support to generic acpi code.

ok jordan@


# 1.61 02-Nov-2006 marco

Kill acpiec attach function and add its functionality to the generic _HID
device attach

prodded and ok jordan


# 1.60 02-Nov-2006 marco

Fix comments to reflect reality.


# 1.59 19-Oct-2006 marco

Replace ACPI ioctls with APM ioctls. This makes ACPI talk the same protocol
as APM therefore userspace can now retrieve power information in the same
fashion as all other apm arches (zaurus, macppc, i386 etc).

To use force the use of acpi device like so: apmd -f /dev/acpi

ok jordan@


# 1.58 19-Oct-2006 jordan

Fixed GPE debug levels
Fixed ref counter on temporary bufferfields
DSDT parsing fully working on IBM Thinkpad
ok marco@


# 1.57 15-Oct-2006 jordan

Removed old GPE handler code remnants
ok marco@


# 1.56 12-Oct-2006 jordan

Massive ACPI changes
Fixed ACPI leaks
new ACPI API
ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.55 30-Jun-2006 gwk

KNF, No functional change,
"go at it" marco@


# 1.54 30-Jun-2006 jordan

Support for calling Embedded Controller via new GPE interface
ok marco@


# 1.53 30-Jun-2006 jordan

Added new code for GPE handling
Also works with ACPIEC; need to fixup acpiec.c to remove original interrupt handler
ok marco@


# 1.52 30-Jun-2006 jordan

Added offset argument for acpi read/write pmreg routines (new GPE code)
ok marco@


# 1.51 31-May-2006 todd

do not call acpi_powerdown() unless acpi has successfully initialized
ok gwk@ canacar@


# 1.50 29-May-2006 canacar

Add embedded controller support.
ok marco@ deraadt@ testing & ok gwk@


# 1.49 19-May-2006 canacar

Minimal thermal zone driver. Monitors thermal zone temperature,
shuts down the system if the 'critical temperature' is reached.
ok marco@


# 1.48 11-Apr-2006 marco

KNF.


# 1.47 11-Apr-2006 gwk

Add acpi_resume(struct acpi_softc *sc), which calls some AML methods,
detected at attach time, currently unused.

ok marco@


# 1.46 11-Apr-2006 gwk

Discover some ACPI sleep related methods, and call them at the
appropriate time in acpi_enter_sleep_state().

ok marco@


Revision tags: OPENBSD_3_9_BASE
# 1.45 26-Feb-2006 marco

Add acpicpu as a device.

ok jordan@


# 1.44 23-Feb-2006 marco

remove dead function.

ok jordan@


# 1.43 22-Feb-2006 marco

Print out all tables it found during autoconf. We'll need this data in
the future.


# 1.42 22-Feb-2006 marco

Make dmesg less verbose.

ok jordan.


# 1.41 22-Feb-2006 jordan

Added parsing of SSDT tables
ok marco@


# 1.40 22-Feb-2006 jordan

Added new aml_register to notify by device id
Moved powerdown code to button device
ok marco@


# 1.39 21-Feb-2006 marco

Readjust debug levels that crept in too low over the last few days.
KNF


# 1.38 21-Feb-2006 marco

Rig the power button so that the machine shuts down correctly.


# 1.37 20-Feb-2006 marco

Add acpibtn device. Remains disabled for now.


# 1.36 19-Feb-2006 jordan

Added code for aml_notify GPE notification callback
ok marco@


# 1.35 19-Feb-2006 grange

Fix acpi_enter_sleep_state() by replacing current ugly hack
with mostly correct sleeping state transition code.

ok brad@ marco@


# 1.34 19-Feb-2006 marco

jordan:
Enable interrupts.
Added peripheral code to make this work.

marco:
Replace softintr and other types of interrupts with a handler with context.
Fixed interrupt magic for GPEs.
Some KNF

This is work done by jordan and I but has to be a single commit.


# 1.33 17-Feb-2006 marco

Remove earlier introduced delays. We really only need them during autconf.

ok jordan@


# 1.32 17-Feb-2006 marco

Add a generic delay function as per the ACPI spec.

from jordan@


# 1.31 17-Feb-2006 marco

Use tsleep instead of delay when !cold.


# 1.30 17-Feb-2006 marco

Argh, we need delays between reads/writes. The backend reads (i2c most likely)
are so slow that values come back as 0 sometimes. We have to do this
despite the spec saying we shouldnt have to. Guess having a faster
parser than others can hurt sometimes.

Idea from jordan.


# 1.29 17-Feb-2006 jordan

Fixed code for handling _INI
Rearranged debug statements
ok marco@


# 1.28 16-Feb-2006 jordan

Added stub for calling _INI
Fixed aml_dowhile for NULL return
ok marco@


# 1.27 16-Feb-2006 jordan

Fixed parser/evaluator to retrieve battery info+state
Cleanup of parser
Dell laptops return battery status
ok marco@


# 1.26 06-Feb-2006 brad

fix a typo so this will compile on amd64.


# 1.25 04-Feb-2006 marco

When ACPI_ENABLED wasn't set but the devices were it had an unknown variable.

Pointed out by ian@.


# 1.24 03-Feb-2006 jordan

Rewrote aml parser yet again...
Fixed bus_space_map for i386/amd64
Added _BST and _BIF to acpibat to get status/info
ok marco@


# 1.23 20-Jan-2006 jordan

More evaluator changes
Removed old evaluator code
Fixed _HID value
ok marco@


# 1.22 19-Jan-2006 jordan

Added support for read/write of PCI Config space
Help jason@
ok marco@


# 1.21 18-Jan-2006 jordan

Added new evaluation routines
Added helper methods for buffer/field evaluation
Fixed broken indentation on dsdt.c

ok marco@


# 1.20 17-Jan-2006 jordan

Major overhaul of the aml parser. Evaluater temporarily disabled.

ok marco@


# 1.19 06-Jan-2006 grange

Check for ACPI control availability only if ACPI_ENABLE defined.


# 1.18 06-Jan-2006 grange

Informative error messages on attach, prettier dmesg.


# 1.17 06-Jan-2006 grange

printf -> dnprintf conversion for debug messages.


# 1.16 05-Jan-2006 grange

Find SLP_TYPx values and store them for future use in
acpi_enter_sleep_state().


# 1.15 05-Jan-2006 grange

Random KNF and cleanup


# 1.14 05-Jan-2006 grange

Provide symbolic names for the acpi sleeping states and use it.


# 1.13 28-Dec-2005 marco

Moved DEVNAME to a acpidev.h

ok jordan@


# 1.12 18-Dec-2005 sturm

add back comment about wrong code

ok marco


# 1.11 16-Dec-2005 marco

Remove test for kbd_reset. Prompted deraadt@, ok jordan@


# 1.10 16-Dec-2005 jordan

Added hooks for powerdown/power button press


# 1.9 16-Dec-2005 jordan

Cleaned up common power management register i/o
Added enter sleep state ioctl
Hooked power button interrupt
ok marco@


# 1.8 16-Dec-2005 marco

Remove autoconf chatter


# 1.7 16-Dec-2005 jordan

Moved DSDT AML parsing into parent ACPI driver
ok marco@


# 1.6 14-Dec-2005 marco

Add AC device glue.


# 1.5 14-Dec-2005 marco

Adjust debug printing levels to something more sane.


# 1.4 13-Dec-2005 marco

Add glue to attach devices to apci.


# 1.3 07-Dec-2005 marco

KNF


Revision tags: OPENBSD_3_8_BASE
# 1.2 10-Jul-2005 grange

Spacing cleanup.


# 1.1 02-Jun-2005 tholo

Start on a basic ACPI framework -- does not do much more than read out the
ACPI tables into kernel memory and attach ACPI and HPET timers currently.

In order to test this code, enabling the devices in GENERIC as well as
the ACPI_ENABLE option is needed. This code does not do any thermal
control yet, so this should be done with care depending on the platform.

In the tree so more people can contribute to making this more fully
featured.

Ok niklas@ grange@ tedu@


# 1.425 08-Jul-2023 tobhe

Move /dev/apm related acpi code to acpi_apm.c which is only built on
amd64 and i386. This is the first step towards a machine independent
safe sleep API.

tested by yasuoka@ bluhm@
ok deraadt@ kettenis@


# 1.424 07-Jul-2023 claudio

Add support for multiple batteries to acpithinkpad setchargestart and
setchargestop. With this laptops like x270 or x240 properly set the
thresholds for both batteries.
Tested by kn@ and jmatthew@
OK kettenis@


# 1.423 06-Jul-2023 deraadt

moving acpiioctl to a different file than acpiopen is not going to work
for ramdisks. noticed by anton.
this must be reconsidered.


# 1.422 05-Jul-2023 tobhe

Move acpiioctl to acpi_x86.c, it is only used up on i386 and amd64.

ok kettenis@ deraadt@


# 1.421 29-Jun-2023 dv

acpi(4)/acpibtn(4): use opt-in approach for wakeup GPEs

Previously, any discovered GPE that could be enabled for wake was
being enabled prior to entering ACPI-based S3 or S4. On some newer
machines, this caused S3-based suspend to break as some devices we
don't fully control (e.g. lack of driver) might be capable of waking
the system and will do so almost instantly if the GPE is enabled for
wake up (i.e. the so called "instant wake" issue).

This changes to a model of having device drivers explicitly opt
into using their GPE for wake up.

The first driver to explicitly toggle a GPE for wake is acpibtn(4).

Variations of the above have been in snapshots for 2 weeks, so
tested by many.

ok deraadt@


Revision tags: OPENBSD_7_3_BASE
# 1.420 15-Mar-2023 kettenis

Add missing nameref resolving for the _DEP() package elements that indicate
dependencies between ACPI devices.

ok dlg@, deraadt@


# 1.419 10-Feb-2023 visa

Adjust knote(9) API

Make knote(9) lock the knote list internally, and add knote_locked(9)
for the typical situation where the list is already locked.

Remove the KNOTE(9) macro to simplify the API.

Manual page OK jmc@
OK mpi@ mvs@


Revision tags: OPENBSD_7_2_BASE
# 1.418 13-Sep-2022 kettenis

Split out the code that collects data from acpiac(4), acpibat(4) and
acpisbs(4) for apm(4) and hook it up to the arm64 version of apm(4) on
systems with ACPI.

ok kn@


# 1.417 12-Sep-2022 kettenis

Add support for level-triggered GPIO events.

ok mlarkin@


# 1.416 10-Sep-2022 kettenis

Don't restrict GPIO events to pin numbers below 256. With the _EVT()
method events corresponding to larger numbers are possible.

Fixes lid state detection on the x13s.

ok patrick@


# 1.415 03-Sep-2022 kettenis

Make sure we don't suspend if we have no way to wake up the machine.
The idea is that device drivers that can wake up the machine register
themselves by calling device_register_wakeup(). To prevent regressions
on amd64, we let acpi(4) register itself as a wakeup device if the AML
for the machine defines wakeup devices. This may be refined in the
future.

This diff will prevent people from suspending their arm64 SBCs without
having a way to wake them up. For now the only driver that registers
itself is axppmic(4), which means that at this moment only SBCs with
an allwinner A64 or H5 SoC will actually support suspend/resume.

ok mlarkin@, deraadt@


# 1.414 10-Aug-2022 patrick

Provide the AML root in ACPI's softc so that drivers that need access
don't have to declare it using extern.

Suggested by and with feedback from kettenis@
Tested by deraadt@ on arm64, amd64 and i386
Tested by bluhm@ on amd64 and i386


Revision tags: OPENBSD_7_1_BASE
# 1.413 17-Feb-2022 jsg

reduce includes


# 1.412 10-Feb-2022 visa

Embed klist head in acpi_softc to avoid explicit malloc.

OK kettenis@


# 1.411 09-Feb-2022 deraadt

Move x86-only stubs used by kern/subr_suspend.c into dev/acpi/acpi_x86.c
for sharing between i386 and amd64.
ok mlarkin kettenis


# 1.410 08-Feb-2022 deraadt

The suspend/resume code is a sticky mess of MI, MD, and ACPI sequencing.
This splits out the MI sequencing, backing it with per-architecture helper
functions. Further steps will be neccesary because ACPI and MD are too
tightly coupled, but soon we'll be able to use this code for more architectures
(which depends on figuring out the lowest-level cpu sleeping method)
ok kettenis


# 1.409 04-Feb-2022 robert

set the APM_BATT_CHARGING state if the battery is being charged and try
to calculate the remaining time to be fully charged

ok jca@


# 1.408 02-Feb-2022 deraadt

acpi_addtask() calls malloc() w/ M_NOWAIT (because some calls come from
interrupt context), this however means occasional resource shortage will
result in callbacks registration failing, and unknown consequences for
the task-submitting caller.
Changing this to use pools with a low water mark, decreases the odds
of that problem occuring.
ok kettenis


# 1.407 01-Feb-2022 deraadt

GPE_DIRECT is calling the AML parser from interrupt context, which is
not permitted. Luckily nothing is using GPE_DIRECT anymore, so this code
can be deleted.
ok kettenis


# 1.406 26-Jan-2022 kettenis

An ACPI device needs to be both present and enabled for it to function.
So only attempt to attach hardware that has both bits enabled. This fixes
an issue where com(4) would attach for a disabled serial port leading to
misdetection of the hardware variant and a subsequent hang when /etc/rc
runs ttyflags -a.

ok anton@, deraadt@


# 1.405 12-Jan-2022 patrick

Make acpi_getpropint() return uint64_t, as ACPI integers are in fact that
wide and some _DSD properties depend on it.

ok kettenis@


# 1.404 04-Jan-2022 patrick

acpi_getprop() needs to actually make sure that we're looking at the
correct property. While there adjust acpi_getpropint() as well to
increase similarity with acpi_getprop().

ok kettenis@


# 1.403 16-Dec-2021 anton

Attach com over acpi on amd64. Some hardware uses a different interrupt
assignment compared to the the legacy one supported by com over isa.
This causes the console to halt once userland takes over as no
interrupts are received. The actual address and irq can be read from
ACPI, kettenis@ already added support for arm64 which paved the way for
amd64.

Some consoles that previously attached over isa are now expected to
attach over acpi.

Thanks to patrick@ for testing on arm64.

ok kettenis@


# 1.402 07-Dec-2021 kettenis

Fix parsing of SR_IRQ resource descriptors.

ok patrick@, anton@


# 1.401 02-Nov-2021 deraadt

knf


# 1.400 30-Oct-2021 deraadt

Change hw.perfpolicy=auto by default, at startup. If the system has AC
power connected (default is yes when no driver differentiates) then default
to 100% performance. On battery, use the existing auto algorithm (which is
admittedly somewhat unrefined).
This change overrides the system/BIOS speed and puts OpenBSD in control.
As this happens very early during boot, besides speedups in all usage usage
patterns, some surprises: unhibernate and sysupgrade times are cut in half.
note: on a few architectures, the setperf fn pointer is changed late, and
thus the auto algorithm stops timeing out. kettenis and i will look for
a solution.
in snaps for more than a week.
ok kettenis


Revision tags: OPENBSD_7_0_BASE
# 1.399 20-Jul-2021 mlarkin

Fix trailing whitespace in a few places.

No code change.


# 1.398 09-May-2021 kettenis

Some x86 machines advertise the "hardware reduced" ACPI feature, advertise
S4 and S5 support, but fail to populate the SLEEP_CONTROL_REG and
SLEEP_STATUS_REG descriptions in the FADT. An example of such a machine
is the ASUS Zenbook 14 UM433DA. Any attempt to powerdown the machine
will result in a kernel crash. It turns out that using the legacy ACPI PM
registers works fine on this machien. So fall back on those registers
if SLEEP_CONTROL_REG and/or SLEEP_STATUS_REG aren't provided.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_9_BASE
# 1.397 15-Mar-2021 patrick

Add acpi_iommu_device_map(), which replaces the DMA tag with one that
is blessed with IOMMU magic, if available. This is mainly for arm64,
since on amd64 and i386 the IOMMU only captures PCIe devices, as far
as I know, which uses the pci_probe_device_hook(). This though is for
non-PCI devices attached through ACPI.

ok kettenis@


# 1.396 07-Mar-2021 patrick

Pass standard DMA tag to acpi(4) table drivers.

ok kettenis@


# 1.395 27-Dec-2020 deraadt

acpi_map_address() cannot be wrapped by SMALL_KERNEL anymore, as
it is used by acpihpet.c


# 1.394 25-Dec-2020 visa

Refactor klist insertion and removal

Rename klist_{insert,remove}() to klist_{insert,remove}_locked().
These functions assume that the caller has locked the klist. The current
state of locking remains intact because the kernel lock is still used
with all klists.

Add new functions klist_insert() and klist_remove() that lock the klist
internally. This allows some code simplification.

OK mpi@


# 1.393 17-Dec-2020 kettenis

Fix some issues with referencing named ACPI nodes from Packages.
These references need to be resolved at runtime rather than when
they're parsed such that they pick up the right values for those nodes
which can be changed when for example _INI methods run.

The current approach is to replace these reference with a string that
names the node in question. The problem with that is that packages
can also contain normal strings. Which means that we need special
code that depends on the context where the package is used.

This diff takes a different approach by retaining a reference when
parsing. Code that uses a package will need to resolve this reference
but there is no ambiguiety anymore.

ok patrick@


# 1.392 05-Dec-2020 kettenis

Add support for the _EVT method to the GPIO support code.

ok patrick@


Revision tags: OPENBSD_6_8_BASE
# 1.391 27-Aug-2020 jmatthew

Reorder the acpi attach process so we look at Processor() CPU nodes
after Device() ones, since we should prefer the newer node type.
If we see any Device() nodes, don't attach acpicpu(4) to any Processor()
nodes if they're also present. This also makes acpitz(4) and
acpipwrres(4) devices attach slightly later.

ok kettenis@, also tested by jmc@


# 1.390 16-Aug-2020 gnezdo

Remove sc_maxgpe from acpi_softc which was never read from

Remove a dead conditional from acpi_init_gpes while there.

ok jcs@


# 1.389 21-Jul-2020 deraadt

acpi can use IPL_BIO (a low interrupt) since it only enqueues operations for
later processing. The use of a high interrupt will predate suspend/resume
efforts, we had to redesign acpi to be non-reentrant obviously
discussed with kettenis, in snaps for more than a week


# 1.388 24-Jun-2020 cheloha

kernel: use gettime(9)/getuptime(9) in lieu of time_second(9)/time_uptime(9)

time_second(9) and time_uptime(9) are widely used in the kernel to
quickly get the system UTC or system uptime as a time_t. However,
time_t is 64-bit everywhere, so it is not generally safe to use them
on 32-bit platforms: you have a split-read problem if your hardware
cannot perform atomic 64-bit reads.

This patch replaces time_second(9) with gettime(9), a safer successor
interface, throughout the kernel. Similarly, time_uptime(9) is replaced
with getuptime(9).

There is a performance cost on 32-bit platforms in exchange for
eliminating the split-read problem: instead of two register reads you
now have a lockless read loop to pull the values from the timehands.
This is really not *too* bad in the grand scheme of things, but
compared to what we were doing before it is several times slower.

There is no performance cost on 64-bit (__LP64__) platforms.

With input from visa@, dlg@, and tedu@.

Several bugs squashed by visa@.

ok kettenis@


# 1.387 10-Jun-2020 jca

Give userland a consistent battery and A/C state after resume

Previously we refreshed the state of acpiac(4) and acpibat(4)/acpisbs(4)
by queueing fake ACPI events to be processed by the acpi thread.
acpibat(4) using ACPIDEV_POLL meant that its status could be refreshed
first, resulting in an APM_POWER_CHANGE event sent to userland before
the state of acpiac(4) was up to date. Because of this, apmd(8) could
see A/C unplugged when it was in fact plugged, possibly leading to
a wrong decision.

Instead of using fake ACPI events, refresh the status of acpiac(4) and
acpibat(4)/acpisbs(4) right away at DVACT_WAKEUP time. To reuse the
existing refresh code of those drivers, make their ACPI *_notify()
handlers responsible for sending APM_POWER_CHANGE events instead. Also
queue an APM_POWER_CHANGE event right after resume to retain existing
behavior.

This fixes the consistency of data shown to userland, and makes those
drivers less special.

Initially discussed last year (!) with mpi@ at Miod's place.
Feedback and ok jcs@, deraadt@ agrees with the use of DVACT_WAKEUP here.


# 1.386 29-May-2020 deraadt

dev/rndvar.h no longer has statistical interfaces (removed during various
conversion steps). it only contains kernel prototypes for 4 interfaces,
all of which legitimately belong in sys/systm.h, which are already included
by all enqueue_randomness() users.


# 1.385 14-May-2020 kettenis

Bring back the acpi(4) changes. This time around only use the ACPI _CRS
information on ACPI 5.0 and later.

ok krw@, patrick@


# 1.384 11-May-2020 jca

More accurate remaining power reporting with disparate batteries

Overall remaining power was computed as the average of the remaining
power of each battery, in percents. This doesn't work with batteries of
different capacity, so use the total capacity and remaining power
instead.

Reported by lidstah, successful tests by lidstah and benno@, ok kettenis@


# 1.383 08-May-2020 kettenis

Move parsing of _CRS into acpi(4) and pass the parsed address and interrupt
information in struct acpi_attach_args.

ok mpi@


Revision tags: OPENBSD_6_7_BASE
# 1.382 14-Apr-2020 kettenis

Implement acpi_getprop(), the ACPI equivalent of OF_getprop().

ok patrick@


# 1.381 12-Apr-2020 kettenis

Add support for the _CCA method. This method indicates whether DMA is
cache-coherent or not. To implement this, acpi(4) gets two bus_dma tags
and passes the appropriate one when attaching devices based on _CCA.
On i386/amd64, where for all practical purpose DMA is always cache-coherent,
the two tags are the same. But on arm64 they are distinct.

ok patrick@


# 1.380 07-Apr-2020 visa

Abstract the head of knote lists. This allows extending the lists,
for example, with locking assertions.

OK mpi@, anton@


# 1.379 03-Apr-2020 mpi

Fix incoherencies of errors returned by various *kqfilter routines.

EOPNOTSUPP is returned when there's no "filterops" for a given fd.
EINVAL when the requested filter isn't supported by the "filterops".
ENXIO means the underlying device is no longer valid/present.

ok visa@


# 1.378 20-Feb-2020 visa

Replace field f_isfd with field f_flags in struct filterops to allow
adding more filter properties without cluttering the struct.

OK mpi@, anton@


# 1.377 31-Dec-2019 visa

Use C99 designated initializers with struct filterops. In addition,
make the structs const so that the data are put in .rodata.

OK mpi@, deraadt@, anton@, bluhm@


# 1.376 31-Dec-2019 mpi

Convert infinite sleeps to {m,t}sleep_nsec(9).

ok kn@


# 1.375 31-Dec-2019 jsg

include sys/sensors.h for sensor use
don't depend on indirect inclusion from acpidev.h


Revision tags: OPENBSD_6_6_BASE
# 1.374 07-Sep-2019 kettenis

Revert acpipci(4) changes. Too many systems are broken. We'll try again
after 6.6 as been released. The acpireg.h change stays behind.


# 1.373 28-Aug-2019 kettenis

Use ACPI information to attach PCI busses like we do on arm64. There are a
few additional quirks though, and attaching the PCI busses is delayed to
replicate the existing code more closely. That may be changed in the
future. Also tweak how we handle MSI support and respect to ACPI flag
that says we shouldn't attempt to use MSIs.

Some fallout is expected.

ok patrick@


# 1.372 27-Aug-2019 deraadt

On amd64 the bootup and resume paths for SP/BP/SP were not performing
msr/pat, clock-startup, firmware upgrade, fpu initialization, and cpu
soft-state initialization in the same order. That kind of chaos is
crazy and fragile. Improve the acpi resume call sequence and unify order
of the operations.


# 1.371 02-Jul-2019 jcs

acpi_set_gpehandler: turn edge arg into flags, add support for
passing GPE_DIRECT which directly executes the supplied callback and
clears a level interrupt rather than going through acpi_taskq which
can be slow when other driver callbacks are in the queue.

This should not be used for GPE handlers that need to do anything
ACPI-related.

ok kettenis


# 1.370 10-Jun-2019 kettenis

Print proper ACPI version number.

ok mlarkin@


# 1.369 08-Jun-2019 kettenis

When establishing the mapping between ACPI device nodes and PCI devices,
skip PCI host bridges and devices that aren't there.

ok patrick@


# 1.368 07-Jun-2019 kettenis

Introduce acpi_getsta() and use it to eliminate some duplicated code.

ok patrick@


# 1.367 12-May-2019 kettenis

Make sure the extended PM register descriptions actually exist before
using them. Fixes machines with ancient ACPI 1.0 (such as recent
hypervisors).

ok deraadt@


# 1.366 12-May-2019 kettenis

Add support for mapping ACPI PM registers using the "extended" gasio
representation from the FADT. Mostly fixes the Lanner NCA-1510.

ok lteo@, mlarkin@, deraadt@


# 1.365 11-May-2019 lteo

Fix incorrect error message.

ok kettenis@


# 1.364 08-May-2019 tedu

free size. the allocation is the header plus the length.


Revision tags: OPENBSD_6_5_BASE
# 1.363 04-Apr-2019 kettenis

Avoid potential null-pointer dereference. Found by Kent R. Spillner.

ok deraadt@, jsg@


# 1.362 20-Jan-2019 jsg

fix SMALL_KERNEL build after changes in rev 1.361
reported by naddy@ ok deraadt@


# 1.361 19-Jan-2019 tedu

Add a pwraction sysctl that controls what the power button does on acpi.
By default, nothing changes -- shutdown is initiated. But allows turning
power button into a sleep button if desired.
(grudging) ok from a few parties


# 1.360 26-Oct-2018 kettenis

Add acpipci(4) on amd64. For now this only calls the PCI-specific _OSC
method to let the ACPI implementation know what features we support.


Revision tags: OPENBSD_6_4_BASE
# 1.359 30-Aug-2018 kettenis

Move softbutton check before the check that skips attaching certain acpi
devices. Fixes regression caused by matching on _CID in addition to
matching on _HID.

ok matthieu@, mlarkin@


# 1.358 25-Aug-2018 kettenis

Match ACPI devices based on _CID if no match for _HID is found.

ok mpi@


# 1.357 19-Aug-2018 kettenis

Add support for multiple PCI segments. Only really implemented for arm64
for now as amd64/i386 firmware still caters for legacy OSes that only
support a single PCI segment.

ok patrick@


# 1.356 03-Aug-2018 kettenis

Let ahci(4) match on _CLS instead of _HID when attaching at acpi(4). Avoids
having to add many more _HID entries to the match table.

ok deraadt@, mlarkin@


# 1.355 10-Jul-2018 kettenis

Make legacy interrupts work in acpipci(4).

ok patrick@


# 1.354 05-Jul-2018 kettenis

Add acpipci(4), a driver that supports generic ECAM-compatible PCI host
bridges based on information provided by ACPI.

ok mlarkin@


# 1.353 04-Jul-2018 kettenis

Properly pass around the PCI "chipset tag" in acpi(4) and refactor
acpimcfg(4) to call an MD initialization functions that sets up a tag for
PCI ECAM.

ok guenther@, mlarkin@, krw@


# 1.352 01-Jul-2018 kettenis

Move acpi(4) attach glue into acpi_machdep.c.

ok guenther@, deraadt@


# 1.351 01-Jul-2018 kettenis

Add support for _DSD Device Properties.

ok mlarkin@


# 1.350 30-Jun-2018 kettenis

Add intr_enable() function, intended for MI use to amd64 and i386 and use
this in the acpi(4) suspend/resume code paths.

ok deraadt@


# 1.349 29-Jun-2018 kettenis

Use <stdint.h> types.

ok mpi@, jung@, krw@, deraadt@


# 1.348 29-Jun-2018 mlarkin

ACPI: Allow (with warning) GPE handler reassignment, instead of returning
a failure code.

ok kettenis


# 1.347 26-Jun-2018 mlarkin

knf


# 1.346 26-Jun-2018 kettenis

Let acpi(4) pass down the bus dma tag.

ok deraadt@


# 1.345 25-Jun-2018 kettenis

Reorganize acpi(4) code a bit in preparation for upcoming arm64 support.

ok deraadt@


# 1.344 20-May-2018 kettenis

Wake up the acpi thread after scheduling a gpio event.

ok mpi@


# 1.343 17-May-2018 kettenis

Sprinkle some #ifndef SMALL_KERNEL such that RAMDISK kernels cuild again.

pointed out by stsp@


# 1.342 17-May-2018 kettenis

Implement GenericSerialBus OpRegion support.

ok mlarkin@


# 1.341 27-Mar-2018 kettenis

Add acpicmos(4), a driver that implements SystemCMOS OperationRegion
access support. This fixes machines where the AML doesn't check whether
support for this OperationRegion type has been registered by the OS.

ok mlarkin@


Revision tags: OPENBSD_6_3_BASE
# 1.340 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.339 10-Feb-2018 deraadt

Syncronize filesystems to disk when suspending. Each mountpoint's vnodes
are pushed to disk. Dangling vnodes (unlinked files still in use) and
vnodes undergoing change by long-running syscalls are identified -- and
such filesystems are marked dirty on-disk while we are suspended (in case
power is lost, a fsck will be required). Filesystems without dangling or
busy vnodes are marked clean, resulting in faster boots following
"battery died" circumstances.
Tested by numerous developers, thanks for the feedback.


# 1.338 08-Feb-2018 deraadt

After flushing disks, try again to release all memory. We don't need to
save dirty memory to the hibernate space.


# 1.337 08-Feb-2018 deraadt

Toss all releaseable memory, because fragmentation can get in the way
of allocating the hibernate playpen.


# 1.336 08-Feb-2018 deraadt

sr_quiesce() is a new approach for ensuring that softraid drains
output to the disks.

This is part of a larger suspend/resume filesystem-safety diff, which
has been worked on for a couple of months already. Tests by job, krw,
beck, benno, and others. Sometimes even by snapshot users...


# 1.335 29-Nov-2017 kettenis

Add support for GPIO-signaled events.

ok mlarkin@


# 1.334 16-Nov-2017 jcs

Add PCI attachment for dwiic(4) needed by Intel 100 Series machines

ok kettenis


# 1.333 14-Nov-2017 kettenis

Add a few more PNP IDs to the skip lists.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.332 17-Aug-2017 stsp

Make dwiic(4) attach its ACPI parent dependencies.
This change makes the Asus E200HA keyboard work.

Original analysis and patch by Cesare Gargano
ok kettenis@


# 1.331 09-Aug-2017 dcoppa

Add "PNP0303" (8042 PS/2 Controller) to acpi_isa_hids[]

ok kettenis@


# 1.330 22-Jul-2017 jcs

acpibat: look for _BIX first before falling back on _BIF

ACPI 4.0 deprecated _BIF for battery status so some newer machines
are exporting _BIX which extends _BIF's package with a few extra
fields. When using _BIX, export the new discharge cycle count as a
sensor.

feedback from kettenis and anton
ok deraadt


# 1.329 20-Jul-2017 kettenis

Start a list of isa(4) device names such that we can avoid printing them
as "not configured".

ok mikeb@, millert@, deraadt@


# 1.328 08-Apr-2017 deraadt

Sensors are run as callbacks inside tasks. During suspend a sensor
could be running inside a driver that will be force-detached, or due
to tsleep end up disrupting the softstate/hardstate contract. At
suspend time, quisce all these callbacks by waiting for completion.
This issue has never been observed for real, but may be implicated in
suspend/resume failures.
ok kettenis guenther mlarkin


# 1.327 08-Apr-2017 deraadt

Two prototypes for acpi_maptable; merge to one place


Revision tags: OPENBSD_6_1_BASE
# 1.326 27-Mar-2017 deraadt

Now that hibernate_alloc() only has clean success/failure, don't
need to call hibernate_free() to clean up a partial mess.
ok mlarkin kettenis


# 1.325 13-Mar-2017 deraadt

allow ramdisks to build; ok jcs


# 1.324 12-Mar-2017 jcs

try to attach acpisbs first and if successful, don't attach acpibat


# 1.323 02-Mar-2017 natano

Add a new sysctl machdep.lidaction. The sysctl works as follows:

machdep.lidaction=0 # do nothing
machdep.lidaction=1 # suspend
machdep.lidaction=2 # hibernate

lidsuspend is just an alias for lidaction, so if you change one, the
other one will have the same value. The plan is to remove
machdep.lidsuspend eventually when people have upgraded their
/ets/sysctl.conf.

discussed with deraadt, who came up with the new MIB name
no objections mlarkin
ok stsp halex jcs


# 1.322 28-Feb-2017 natano

Untangle abstract sleep modes from ACPI sleep states. This paves the way
for further work in the area.

idea and ok deraadt
ok mlarkin


# 1.321 22-Feb-2017 jcs

add acpisbs, an acpi smart battery subsystem driver reading data
over smbus

currently disabled because it conflicts with acpibat

ok deraadt, kettenis


# 1.320 14-Jan-2017 kettenis

Revert aml_rdpciaddr changes; breaks several machines that were working
before.


# 1.319 11-Jan-2017 jsg

Increase the size of the buffers used for _HID and _CID strings to be
able to fit "Hyper_V_Gen_Counter_V1".

ok mlarkin@ deraadt@


# 1.318 08-Jan-2017 kettenis

Make aml_rdpciaddr() work for for devices behind bridges. The code now
uses the mapping between AML nodes and PCI devices that we establish.
Because _INIT methods may end up calling aml_rdpciaddr(), make sure we
create that mapping early. Also handle devices that aren't actually present.
These devices are now included in the mapping and reads will return an
all-ones patterm whereas writes are a no-op..

ok mlarkin@


# 1.317 25-Oct-2016 pirofti

Add the resource index to aml_parse_resource() callback function.

This is needed by an upcoming acpiec commit that handles machines breaking
the current ACPI specifications.

Change suggested by and ok kettenis@, guenther@


# 1.316 18-Sep-2016 guenther

If the APIC table (MADT) isn't present then ci_acpi_proc_id won't be set
when acpicpu tries to attach, leading to a NULL dereference. The ACPI
processor object on the Dells where this was reported is bogus and useless
anyway so apply the same test when deciding whether to attach to processor
objects as acpicpu would when matching them to cpus.

Problem reported by Eivind Eide (xenofil (at) gmail.com) and
Johan Huldtgren (johan+openbsd-bugs (at) huldtgren.com).
Testing by Eivind Eide.
ok kettenis@ mlarkin@


# 1.315 03-Sep-2016 naddy

Write the system time back to the RTC every 30 minutes.
This fixes the problem that long-running machines which were not
shut down properly would reboot with a badly offset system time.

hints and ok kettenis@


# 1.314 31-Aug-2016 mlarkin

Don't put a device in the wake device list if _STA says it's not there.

Fixes at least one machine that woke up from sleep / poweroff immediately.

ok kettenis@, tested by kettenis and martijn@


# 1.313 28-Jul-2016 kettenis

Store the acpi processor ID/UID in struct cpu_info, and use it to attach
acpicpu(4) drivers to the right cpu(4).

ok mlarkin@, guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.312 10-Jun-2016 kettenis

The power management timer has been made optional in ACPI 5.0A, so don't
attempt to attach acpitimer(4) if the timer isn't present.

Pointed out by jcs@

ok jcs@


# 1.311 08-May-2016 kettenis

Make sure devices listed by a device's _DEP method are attached before
we attach the device itself. The _DEP methode, introduced in ACPI 5.0,
is a hint that the device in question depends on OpRegion support from
the devices listed by _DEP, so we have to order them the proper way.
To prevent us from attaching those devices again when we encounter them
later walking down the device tree, keep track of our attempts to
attach a device driver to them using aflag in the aml_node structure.

ok guenther@


# 1.310 25-Apr-2016 pirofti

We attach acpihpet(4) and acpiec(4) without using the HID, so do not
report it in the dmesg.

OK deraadt@, mlarkin@.


# 1.309 02-Apr-2016 kettenis

Reduce dmesg spam by knocking out some acpi hardware IDs that don't need a
driver.

ok mpi@


# 1.308 29-Mar-2016 kettenis

Check if a device is present (using _STA) in acpi_foundhid. This prevents
us from reporting devices as "not configured" that aren't actually present,
and allows us to remove duplicated code from several drivers.

ok deraadt@, ok jsg@


# 1.307 29-Mar-2016 kettenis

Implement a cleaner way to prevent acpivout(4) from attaching if we have
a platform-specific method of controlling the backlight.

ok millert@, mlarkin@, jung@


# 1.306 28-Mar-2016 kettenis

Adding each and every new acpi device driver to acpi_foundhid() is
gettinga bit of the burden. And it really isn't how our autoconf
framework is supposed to work for busses that can be enumerated. All
the drivers already check for a matching _HID in their attach
function. So we can just drop the checks in acpi_foundhid() and let
the drivers sort themselves out.

Print a "not configured" line for devices for which we don't attach adriver.
This may be a bit much as there are devices for which we will not have a
driver. This will be removed if it causes too much dmesg spam.

ok deraadt@, mlarkin@


Revision tags: OPENBSD_5_9_BASE
# 1.305 17-Jan-2016 jsg

Take acpi_foundhid() outside of #ifndef SMALL_KERNEL so the ramdisk
kernel can find and attach dwiic(4) and sdhc(4).


# 1.304 17-Jan-2016 jsg

remove an unused global variable


# 1.303 14-Jan-2016 kettenis

Attach dwiic(4) on the Intel Bay Trail i2c controllers as well.


# 1.302 12-Jan-2016 kettenis

Improve emulation of PM registers on Hardware-redcued ACPI by emulating
SPL_TYPx, SLP_EN and WAK_STS using the new SLEEP_STATUS_REG and
SLEEP_CONTROL_REG registers. Makes the transition into S5 (powerdown) work
on the ASUS X205TA.

ok deraadt@, mlarkin@


# 1.301 12-Jan-2016 jcs

Add dwiic, a driver for the Synopsys DesignWare i2c controller found
on the Samsung ATIV Book 9 laptop. This initial version only
supports ACPI config/attachment.

Add ihidev, a HID-over-i2c driver largely based on uhidev. dwiic
handles attaching ihidev devices found in ACPI.

Add ims, a HID-over-i2c mouse/trackpad driver to get basic cursor
and button functionality from HID-compliant i2c trackpads.

ok kettenis deraadt


# 1.300 11-Jan-2016 kettenis

Make sdhc(4) attach to hardware IDs "80860F14" and "PNP0FFF".


# 1.299 09-Jan-2016 kettenis

Initial support for Hardware-Reduced ACPI. This skips the legacy mode checks
by emulating the PM_CNT register such that SCI_EN is always enabled. It also
skip attaching acpitimer(4) since the relevant registers are no longer there.

ok deraadt@, mlarkin@


# 1.298 06-Jan-2016 kettenis

Follow the spec and check the SCI_EN bit to see if ACPI has already
been enabled and don't bother enabling it again if it is. This should fix
systems that don't implement legacy mode and therefore set ACPI_ENABLE and
ACPI_DISABLE to zero.

ok mlarkin@, yasuoka@


# 1.297 23-Nov-2015 reyk

Back out the CRITICAL change, turn it back from 10% to 25%. Some
people with old/broken batteries didn't like this change, and the
underlying heuristics are wrong.


# 1.296 22-Nov-2015 reyk

The "CRITICAL" APM battery level, that is exported/faked by the ACPI
stack, is currently set to 25%. With 25% battery left, a typical
modern laptop will have at least one hour runtime left. It is an
arbitrary value, and other APM power layers in the kernel set CRITICAL
to something like 5% or 10%, so set it to 10% for now.

OK mlarkin@


# 1.295 28-Sep-2015 deraadt

In low-level suspend routines, set cold=2. In tsleep(), use this to
spit out a ddb trace to console. This should allow us to find suspend
or resume routines which break the rules. It depends on the console
output function being non-sleeping.... but that's another codepath which
should try to be safe when cold is set.
ok kettenis


# 1.294 06-Sep-2015 deraadt

clarify malloc arguments (base + length...)


# 1.293 28-Aug-2015 deraadt

fairly simple sizes for free(); ok tedu


# 1.292 20-Aug-2015 kettenis

Respect the access size when reading or writing to pci config space.
This version also makes sure writes are properly aligned, preventing
the panics seen with the previous incarnation of this change.

Fixes battery status passthrough in vmware, brightness keys on my x220
and who knows what else.

ok mlarkin@, deraadt@


Revision tags: OPENBSD_5_8_BASE
# 1.291 04-Aug-2015 deraadt

If we are setting ACPI_PM1_BM_RLD do it only once, and remember to
repeat this action at resume time.
ok mlarkin guenther


# 1.290 29-Jul-2015 deraadt

Something subtle in previous revision is locking up suspend or booting
on some laptops. Mailing lists threads mention x220 and Asus machines.
Reverting for now.


# 1.289 21-Jul-2015 kettenis

iRespect the access size when reading or writing to pci config space.
Fixes battery status passthrough in vmware and who knows what else.

Tested by many.

ok mlarkin@, deraadt@


# 1.288 17-Jul-2015 mlarkin

fix comments


# 1.287 26-Mar-2015 jsg

Use the 0x10 extended capability to determine if the trackstick
buttons are wired to the trackpad and need to be re-routed to the
trackstick. Without this change the buttons on 2015 thinkpads
get picked up as extended buttons that show up as scroll up/down.

Remove the X1 Carbon 2015 (LEN0048) and X250 (LEN0046) from the
top button area/soft buttons quirks list. Also avoid using the
quirk list entirely if the capability bit is set.

Based on work from jcs@ with feedback from mpi@
ok mpi@ jcs@


# 1.286 20-Mar-2015 kettenis

The ACPI standard says that a PCI function number of FFFF can be used to refer
to all functions on a device. This makes no sense in the context of accessing
PCI config space. Nevertheless stsp@ has a machine with AML that tries to
do config space reads from a device where _ADR encodes function number FFFF.
This is almost certainly buggy AML, but since Linux (and presumably Windows)
boot just fine on this machine we need to work around this. Linux effectively
treats function FFFF as function 7, which isn't present on this machine.
Instead we simulate a read from a non-existent device by returning a bit
pattern of all ones. Writes to function FFFF will still panic.

ok mlarkin@


# 1.285 16-Mar-2015 deraadt

Reduce use of SMALL_KERNEL in acpi, bringing acpiec driver and more gpe
handling into RAMDISK. This is now possible because the install media
has ample room. The goal is to reduce special cases where we may be
skipping (unknown) important operations...
ok mlarkin kettenis


# 1.284 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.283 07-Feb-2015 deraadt

New framework that allows hibernate to pass in entropy from it's fresh
boot.
ok mlarkin


# 1.282 06-Feb-2015 mlarkin

Fix a hibernate crash on some machines due to unmapping a page that
may not have been mapped previously (in the failure to hibernate case).

Also ensure that the lowmem ptp is mapped in all cases (not just MP).

ok kettenis


# 1.281 17-Jan-2015 deraadt

resume_randomness() before spinning up other cpus...


# 1.280 15-Jan-2015 jsg

Define a new wscons mouse type for Synaptics clickpad devices that lack
physical buttons. This will be used if the acpi pnpid for the mouse
matches a list provided by Synaptics found in Linux.

Combined with the xenocara changes this will remove the need for an xorg
config file for the x240/t440/t540 generation of thinkpads.

ok matthieu@ shadchin@ kettenis@


# 1.279 11-Jan-2015 kettenis

Tolerate checksum errors in ACPI tables (but complain about them). Apparently
there are machines out there with broken BIOSen with such checksum errors in
thier RSDT tables. Windows runs fine on these machines, so it probably does
not check. Linux ignores checksum errors as well (and prints a warning).

ok deraadt@, krw@


# 1.278 18-Dec-2014 deraadt

suspend and resume the RNG (at what looks like the right spots)
ok reyk djm


# 1.277 09-Dec-2014 doug

Replace some malloc(n*size,...) calls with mallocarray().

ok tedu@ deraadt@


# 1.276 06-Dec-2014 mpi

If "machdep.lidsuspend" is not set, do not go back to sleep if the lid
is still closed when resuming. This allows docked laptop to be resumed
without opening them.

ok mlarkin@


# 1.275 23-Nov-2014 mlarkin

If a machine has no _LIDs, then none can be open. This diff fixes a problem
where machines without any _LIDs were going back to sleep after hibernate
due to the recent suspend-if-lid-closed-on-resume diff. Noticed on qemu,
which doesn't have any _LIDs.

discussed with deraadt


# 1.274 18-Nov-2014 krw

Nuke yet more obvious #include duplications.

ok miod@


# 1.273 08-Nov-2014 mlarkin

If resuming from sleep (zzz/ZZZ) and the lid is still closed, go back to
sleep. This prevents accidental lid flex or slight opening in a backpack
from waking the machine up and leaving it resumed (powered on).

ok deraadt@


# 1.272 26-Sep-2014 kettenis

Rework piglet and pig allocation. Currently the piglet gets allocated
deep down in the suspend path, where it is really hard to recover from
allocation failure. So allocate the piglet early on in the suspend path.
Also change the piglet and piglet allocation functions to use km_alloc(9)
instead of doing pmemrange magic. This removes a bunch of code which, in the
case of the piglet allocation, is broken since it results in a NULL pointer
dereference. Also switch the piglet allocation to not wait. If we can't
allocate 16MB of phys contig memory on a halfway modern machine we're almost
certainly under a lot of memory pressure and we're better off not trying to
hibernate anyway.

ok mlarkin@


# 1.271 25-Sep-2014 kettenis

Bring back rev 1.249. Now that mp_setperf() has been fixed, it should be
safe to restore perflevel when resuming. Should fix the issues people
see with the processor running at full speed heating up the machine while
the apm command reports that the CPUs are running at a low speed.

ok dcoppa@, deraadt@


# 1.270 23-Sep-2014 kettenis

Release the acpi lock around calls to wsdisplay_suspend() and
wsdisplay_resume(). The X server may adjust the display brightness just
before/after the VT switch when suspending/resuming. In fact, it may
already be blocked adjusting the display brightness for some reason
unrelated to the suspend request.

The VT switch request will eventually time out, but the delay is annoying
and the failed VT switch will lead to problems after resume.

Needed for a future update of the xf86-video-intel driver.

ok mlarkin@


# 1.269 19-Sep-2014 kettenis

Use config_suspend_all(9).


# 1.268 08-Sep-2014 kettenis

Clear the PME Status bit on PCI devices in response to Device Wake events.
Fixes the interrupt storm on the 2009 Mac mini with WOL enabled on its
nfe(4) interface.

ok mlarkin@


Revision tags: OPENBSD_5_6_BASE
# 1.267 20-Jul-2014 mlarkin

Support hibernating to softraid crypto volumes.

much help and ok from deraadt@


# 1.266 16-Jul-2014 mlarkin

Reenable hibernate RLE support and flush+zero all memory after unpack.


# 1.265 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.264 12-Jul-2014 kettenis

Add a function to drop all clean pages on the page daemon queues and call
it when we hibernate.

ok mlarkin@, miod@, deraadt@


# 1.263 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.262 11-Jul-2014 mlarkin

Flush the buffercache to 16MB on hibernate and restore its previous max
size (kern.bufcachepercent) on resume, for better hibernate performance.

ok beck@


# 1.261 10-Jul-2014 blambert

remove unneeded header

ok mlarkin@


# 1.260 01-Jul-2014 kettenis

Adding an additonal delay after each access to SystemMemory or SystemIO
during autoconf makes no sense.

ok deraadt@


# 1.259 23-Jun-2014 kettenis

SystemMemory is used to access memory mapped registers on some machines, so we
must use the bus_space(9) API to access it instead of memcpy(9). Also make
sure we properly align access to fields that need more than byte alignment.

ok guenther@


# 1.258 21-May-2014 mlarkin

Fix invalid / missing format specifiers in several dnprintf statements
(used only when ACPI_DEBUG is enabled)

From a diff on tech@ by Fabian Raetz, with a minor change by me, tested
on i386 and amd64 with ACPI_DEBUG enabled and disabled.


# 1.257 25-Apr-2014 mlarkin

Defer the free of the hibernate structures until later, when it is safe to
do so.

Freeing the hibernate structures too early leads to inadvertent calls into
splx when we aren't ready for them yet. Found when trapping splx calls in
the resume path.

discussed with deraadt@ at m2k14


# 1.256 13-Apr-2014 mlarkin

Fix a badly worded debug message that implied the entire machine was going
to sleep, instead of just the acpi thread going to sleep.


# 1.255 31-Mar-2014 mpi

Change the order of operations during a suspend/resume cycle and
call bufq_quiesce() after executing the DVACT_QUIESCE handlers.

This should be safe since no disk nor controller drivers have such
handler but it will allow us to detach sd(4) devices attached to a
USB bus.

Another benefit pointed out by kettenis@ is that drivers that need
to read a firmware from the disk should be able to do it at resume
time in a DVACT_WAKEUP handler.

ok kettenis@, deraadt@


# 1.254 13-Mar-2014 dlg

get rid of the assumption that the head of the alldevs list is the
"mainbus" device. this breaks when mpath is enabled because it
attaches before mainbus and therefore takes the head position.

have autoconf provide device_mainbus() which looks up mainbus_cd,
and use that instead.

discussed with deraadt who just wants mpath stuff to move forward
despite there being many ways to shine this particular turd.


Revision tags: OPENBSD_5_5_BASE
# 1.253 21-Feb-2014 deraadt

Do the lid control further on in the resume sequence, since this is
AML, and will do WAITOK operations. While cold, that is a bad idea.
We seem to be safe with respect to the spec, I do not see any event
debounce issues here.
ok kettenis, tested by many


# 1.252 20-Jan-2014 claudio

Instead of checking sc->sc_revision <= 1 check that the
fadt->hdr_revision <= 1. This is a better check since it checks
the version of the proper descriptor. This allows the alix APU
to reboot since those boards have currently a wrong sc->sc_revision.
Committing this now so that it can be tested easier.
With and OK jsg@ also fine by derradt@


# 1.251 24-Dec-2013 kettenis

If the FADT has its SMI_CMD set to zero, assume we're only ACPI-only hardware
and don't need to disable SMI ownership of the ACPI hardware registers.

ok mlarkin@


# 1.250 23-Dec-2013 kettenis

Revert previous commit. It makes resume hang on some MP systems running
GENERIC.MP.


# 1.249 16-Dec-2013 kettenis

Restore hw.setperf upon resume like we do for apm(4) on i386. Seems we need
to do this fairly late in the resume to avoid hangs.

ok deraadt@, mlarkin@


# 1.248 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


# 1.247 06-Nov-2013 mpi

Add support for Power Resources for Dx states and the necessary hook
for PCI devices. This hook should be called twice, before and after
changing the power state of a PCI device.

Before setting the device to the new state, the ACPI layer will notify
every power resources linked to the device for that state and make sure
they are turned "_ON". After changing the state of the device, it will
decrement the reference of every power resources linked to that device
for the old state and turn them "_OFF" if they are no longer referenced.

This fixes the no-USB after resume problem seen on various ThinkPad,
problem initialy diagnosed with Alexander Polakov.

ok kettenis@, deraadt@


Revision tags: OPENBSD_5_4_BASE
# 1.246 01-Jun-2013 mlarkin

Stop printing ACPI wakeup devices in dmesg after the 16th wakeup device.

Helps with VMware VMs that report hundreds of wakeup devices.

ok reyk@, deraadt@


# 1.245 31-May-2013 bluhm

Fix the build for a kernel without wd and pciide in its config.
OK deraadt@ tedu@


# 1.244 30-May-2013 deraadt

Enforce ca_activate tree-walks over the entire heirarchy for all events,
cleaning up some shutdown-hook related code on the way.
(A few drivers related to sparc are still skipped at kettenis' request)
ok kettenis mlarkin, tested by many others too


# 1.243 18-Apr-2013 deraadt

pin the acpi thread to the BSP, because AML and SMI cannot be trusted. I
was certain we were doing this already, but it turns out no...


Revision tags: OPENBSD_5_3_BASE
# 1.242 24-Dec-2012 guenther

In acpiioctl(), Return though the splx() path if suser() returns an error

ok deraadt@


# 1.241 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


# 1.240 04-Oct-2012 ehrhardt

Use information provided by ACPI to attach secondary PCI host bridges.

ok kettenis@


# 1.239 07-Sep-2012 kettenis

Add a new function called acpi_pci_min_powerstate() that returns the lowest
power state for a PCI device for the power state (currently S3 or S4) we're
transitioning into. This function is probably too simplistic; there is a lot
of (silly) complexity in the ACPI spec.

ok deraadt@


Revision tags: OPENBSD_5_2_BASE
# 1.238 13-Jul-2012 pirofti

Disable acpivideo(4) on ASUS laptops.

There are some models that panic whenever an event is received.
Some cope, some don't. In order to get this right acpiec(4) requires
major rework. Until then let machines boot.

Discussed with deraadt@.


# 1.237 11-Jul-2012 mlarkin

for now, restrict hibernate to root in the acpi ioctl kernel interface.
normal rules for controlling this interface through apmd still apply.

ok deraadt@


# 1.236 09-Jul-2012 deraadt

tiny little oops in untested hibernate block


# 1.235 09-Jul-2012 deraadt

clean up ioctl() case statements


# 1.234 09-Jul-2012 deraadt

acpi_disable_onegpe() is not used; ok pirofti


# 1.233 24-May-2012 kettenis

If _MIN > _MAX consider a WordBus resource to be invalid. Fixes a panic on
the IBM x3550-M4.

ok mikeb@


# 1.232 29-Mar-2012 mlarkin

Missing #ifdef in previous diff


# 1.231 29-Mar-2012 mlarkin

Prevent hibernation attempts on machines with unsupported disk controllers.
The same check is also performed later in the hibernate sequence, but by
then it's too late to cleanly unwind (presently).

ok deraadt


# 1.230 26-Mar-2012 deraadt

simplify conditionals in acpi_sleep_state()
ok mlarkin


# 1.229 26-Mar-2012 deraadt

hook in the hibernate request code; half of this diff is from mlarkin
ok mlarkin


Revision tags: OPENBSD_5_1_BASE
# 1.228 20-Sep-2011 deraadt

Very early in suspend while the clock is still ticking realtime,
save the clock back to the rtc. The zaurus already did this.
ok phessler miod kettenis


Revision tags: OPENBSD_5_0_BASE
# 1.227 02-Jul-2011 nicm

kqueue attach functions should return an errno or 0, not a plain 1. Fix
the obvious cases to return EINVAL and ENXIO.

ok tedu deraadt


# 1.226 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.225 16-Jun-2011 pirofti

Import acpitoshiba driver.

This is originally written by Hiroyuki Aizu, ported and integrated
in our tree by Javier Vazquez <javama4 AT gmail> with tweaks and
suggestions by me.

Import prodded by deraadt@ and the lack of testers on tech@.


# 1.224 27-Apr-2011 jcs

attach acpithinkpad to newer lenovo models like the x120e

ok deraadt@


# 1.223 22-Apr-2011 jordan

Fix uninitialzied variables and formatting strings (-Wxxx errors)


Revision tags: OPENBSD_4_9_BASE
# 1.222 02-Jan-2011 jordan

Re-add code for ACPI taskqueue, eliminates need for state vars in acpi softc
Serialize processing ACPI tasks for sleep/powerdown/GPE event
ok deraadt@


# 1.221 31-Oct-2010 guenther

Revert last commit: it breaks resume on Thinkpads
"then please back it out" deraadt@


# 1.220 26-Oct-2010 jordan

Add task queue for ACPI gpe and notify handlers
This fixes eject on Dell Latitude dock and an issue on the Dell Mini
battery update.
ok deraadt


# 1.219 07-Oct-2010 jordan

Create separate functions for enabling wake and runtime gpes.
Remove old suspend/resume gpewalk function
ok deraadt, mlarkin


# 1.218 05-Oct-2010 jordan

split acpi_enable_onegpe to separate enable/disable functions
ok deraadt


# 1.217 05-Oct-2010 jordan

Added acpi_disable_allgpes function to clear GPE status
ok deraadt


# 1.216 05-Oct-2010 deraadt

clean up acpi_softc use more


# 1.215 05-Oct-2010 deraadt

delete a bit of #if 0 code


# 1.214 05-Oct-2010 deraadt

hide use of our global acpi_softc pointer a bit better


# 1.213 29-Sep-2010 deraadt

grab the acpi thread lock very early in acpi thread startup; not that it
is likely that anyone will race us... but why be careless.
ok kettenis


# 1.212 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.211 29-Aug-2010 jasper

- add a check for FWRITE here too.

ok miod@


# 1.210 27-Aug-2010 kettenis

Make PCI Power Management optional, and only enable it when acpi(4) attaches.

ok deraadt@


Revision tags: OPENBSD_4_8_BASE
# 1.209 08-Aug-2010 kettenis

Close race between the acpi thread and wscons ioctls running in process
context. We might rip this out post-release and replace it with a solution
that serializes things in a more obvious way.

ok deraadt@, miod@ and "those crazy texans who ok anything"


# 1.208 08-Aug-2010 deraadt

Report the acpi sleep states when on a RAMDISK; ok krw


# 1.207 07-Aug-2010 kettenis

Enforce acpi register access rescrictions.

ok mlarkin@, deraadt@, tested by marco@


# 1.206 07-Aug-2010 deraadt

upon resume, notify all the acpiac and acpibat drivers to update
their status. some laptops do not do this automatically.
ok kettenis canacar


# 1.205 06-Aug-2010 marco

Always call _PSW on _LID devices that support it to enable lid open to
resume the machine. Conversely disable it when the machine wakes up.

Tested by several ok deraadt


# 1.204 06-Aug-2010 deraadt

As part of a revamp of the PM1/GPE code, I write this diff to look for
unmanaged EN & STS bits in the PM1 register at interrupt time and report
them. As a side effect this splits the STS acknowledgement into two writes
(for power, and sleep) instead of one. The printf that is added (to spot
unmanaged STS bits) has not yet been triggered as far as we know.

Before the "write to PM1 registers at the right offset" diff went in, this
was not neccessary. But newer thinkpads do not have a working soft power
button without this diff.

We have no idea why.
ok mlarkin kettenis


# 1.203 05-Aug-2010 deraadt

PM1 power button events were generating 'fake events' to acpibtn PWRB,
but if there is no such device these were being tossed. Instead, arrange
for all power button handlers to flag a softc variable which is processed
by the thread; so that one place will know to request a powerdown (psignal
to init)
ok marco mlarkin


# 1.202 05-Aug-2010 deraadt

rename a variable which is annoyingly poorly named


# 1.201 05-Aug-2010 mlarkin

Write to the correct offset for various ACPI pmreg registers. Writing to
the correct register does properly clear the wake status bits and fixed
event bits, which allows some machines to have more than 1 suspend cycle.

bug spotted by deraadt@

tested by deraadt@ and myself on a half-dozen machines and others on
hackers@

ok deraadt@, marco@


# 1.200 05-Aug-2010 deraadt

Fix the power button. The pm1 register is 16 bits in size.
My mistake, a while back.
tested by krw


# 1.199 04-Aug-2010 kettenis

Print supported sleep states.

ok marco@, phessler@, deraadt@


# 1.198 03-Aug-2010 mlarkin

Use the proper flag for re-enabling certain hardware events (power btn,
sleep btn). The (incorrect) flag prevoiously used coincidentally had the
same value, so this shouldn't cause any different behavior than before.

tested on a variety of machines (i386, amd64, sp, mp) - no behavioral
change seen

ok deraadt@


# 1.197 28-Jul-2010 marco

Make all gasio failures look and behave the same.


# 1.196 28-Jul-2010 mlarkin

Delay a little longer on suspend before giving up. Fixes machines that
sometimes don't suspend because the sleep transition takes too long
(symptoms like failing to sleep but the backlight stays on).

ok deraadt@


# 1.195 27-Jul-2010 deraadt

remove XXX; EC can now be attached early when needed; ok jordan


# 1.194 27-Jul-2010 deraadt

more brackets feels good


# 1.193 27-Jul-2010 deraadt

XXX annotate a block in acpi_enter_sleep_state() that is pretty worrying


# 1.192 27-Jul-2010 deraadt

horrific indentation that nearly made me puke


# 1.191 27-Jul-2010 mlarkin

Set the sleeping indicator light on machines that support it, in case SMI
doesn't do it for us. Shows the 'moon' LED on older Thinkpads when
sleeping.

Tested on T43p, W500, T510 by me and on X61s by deraadt@.

ok deraadt@


# 1.190 27-Jul-2010 jordan

Early initialization of acpiec if ECDT table exists
Fixes hang when booting thinkpads while docked
ok deraadt


# 1.189 26-Jul-2010 deraadt

remove merge error which was commited


# 1.188 26-Jul-2010 pirofti

Add support for Sony ACPI hotkeys via a new driver: acpisony(4).

Currently it only works for the suspend button and tries to do right for
the brightness events, but I haven't found a Sony laptop that like to do
right so far.

In the future I want to make the brightness keys work on all Sony's and
also add support for the zoom hotkeys and whatever other funky keys I can
find on those things.

Okay deraadt@.


# 1.187 22-Jul-2010 deraadt

pretty large cleanup of SMALL_KERNEL


# 1.186 21-Jul-2010 deraadt

in the acpi GPE handler, late ack level interrupts not edge interrupts.
Duh.


# 1.185 21-Jul-2010 deraadt

Merge the edge and level gpe handlers into one (now possible since the
gpe structure tells us whether the gpe is edge or level)


# 1.184 21-Jul-2010 deraadt

delete #if 0 code we are not going to use; ok marco


# 1.183 20-Jul-2010 deraadt

make the mask code for acpi_enable_onegpe() match other similar code


# 1.182 20-Jul-2010 deraadt

Handle the acpi interrupt controller with proper edge/level handling,
wrapped in spltty to avoid racing against the interrupt controller.
Repair the gpe bit masking code while there, and do operations in the
right order
ok kettenis mlarkin, help from jordan at figuring out the order of
operations.


# 1.181 20-Jul-2010 matthew

When we're preparing to go to sleep, only call bufq_restart() if other
preparations failed.

ok kettenis@, thib@, deraadt@


# 1.180 19-Jul-2010 deraadt

permit compilation with ACPI_DEBUG


# 1.179 19-Jul-2010 deraadt

there is no need for a seperate dsdt_softc pointer; dsdt does not have a
softc but is just a part of the single acpi driver.
ok jordan


# 1.178 19-Jul-2010 deraadt

acpi_delay() is no longer needed; ok jordan


# 1.177 18-Jul-2010 mlarkin

Clear event status on resume, to avoid going back to sleep/reboot/shutdown
immediately after resume. Tested by a bunch of people on hackers@ on
various hardware. Fixes at least Toshiba NB200 and Gateway LT3103u.

ok marco


# 1.176 13-Jul-2010 deraadt

rename some thread related interfaces so they are logical


# 1.175 10-Jul-2010 jordan

Verify _ADR bdf is correct (some systems return 0xffff)
Works on Dell x9xx and Studio 1555


# 1.174 09-Jul-2010 jordan

Clear the noise


# 1.173 08-Jul-2010 jordan

Add mapping for ACPI device to PCI bdf (match autoconf tree)
Simplify resource parsing function to use buffer argument
Convert namespace linked lists to use queue macros
ok marco@, deraadt@


# 1.172 06-Jul-2010 deraadt

Use spltty() to lock downcalls from apm against the information being
modified by the acpi thread.
ok kettenis


# 1.171 06-Jul-2010 deraadt

Don't use a workq to do the suspend, because it races aginst the acpi
thread. Instead, just tell the acpi thread to do the suspend for us.
This makes apmd & zzz work correctly. While here, have acpithinkpad
attempt to post the event to apm before prompting the suspend itself.
ok kettenis marco mlarkin


# 1.170 05-Jul-2010 mlarkin

Reset register SCI_EN on resume. The ACPI spec clearly states we don't have
to do this, but many machines don't, or forget to, which leads to all sorts
of problems on resume (broken battery stats, overheating, failure to resume
more than once, etc).

tested by many, ok marco@, deraadt@


# 1.169 01-Jul-2010 thib

do bufq quiesce again, since vnd/the bufq_done call in
biodone has been fixed.


# 1.168 01-Jul-2010 jordan

Backout recent AML changes, breaks on R210 and others


# 1.167 01-Jul-2010 jordan

Simplified aml_parse_resource call, cleanup code
ok marco


# 1.166 01-Jul-2010 jordan

Fix subordinate bus # for multi-root PCI buses
ok marco, kettenis


# 1.165 30-Jun-2010 thib

Disable/partially backout the bufq quiesce changes as this
is causing havoc with vnds and release must be buildable.


# 1.164 29-Jun-2010 deraadt

Fix ramdisks (new code was placed inside a SMALL_KERNEL chunk)


# 1.163 29-Jun-2010 jordan

Add support for mapping ACPI to PCI devices
ok kettenis, deraadt


# 1.162 29-Jun-2010 kettenis

Call bufq_quiesce() on suspend to stop all I/O and bufq_restart on resume.
Makes suspend/resume work much better while doing disk I/O.

ok mlarkin@


# 1.161 29-Jun-2010 tedu

60 is a more reasonable guess for number of minutes in an hour than 100.
spotted by TeXitoi. ok deraadt


# 1.160 27-Jun-2010 jordan

Add _acpi_matchhids routine, precursor to pci device match


# 1.159 27-Jun-2010 mlarkin

Clear ACPI fixed event status on resume (power buttons/etc) so that some
machines don't immediately resume after suspending to S3.

Tested by kettenis@ and I on 5 or 6 machines, deraadt@ "yeah, looks good"


# 1.158 27-Jun-2010 jordan

Cleaned up acpi_maptable, adds to table list directly


# 1.157 07-Apr-2010 deraadt

If we have done a wsdisplay_suspend(), and ACPI decides it does not want to
suspend, we must roll back by calling wsdisplay_resume() or the virtual
console switching will be locked forever
ok oga


# 1.156 07-Apr-2010 kettenis

Make suspend/resume work on MP machines (running an MP kernel). Joint work
from mlarkin@ and me, with some amd64 fixes thrown in by deraadt@

ok marco@, deraadt@, pirofti@, mlarkin@


# 1.155 31-Mar-2010 kettenis

Bring /dev/apm support in line with apm(4). Make the suspend button send
a suspend request event to apmd(8) instead of suspending immediately. Also
keep track of whether /dev/apm and /dev/apmctl are currently open, such that
we can still suspend immediately if apmd(8) isn't running.

ok deraadt@, marco@, pirofti@, jsing@, oga@


# 1.154 30-Mar-2010 oga

Prevent the apmd/x races for good.

When we hit suspend time, go through all wsdisplays on the system. if
they are in mode MAPPED, but not MODE_DUMBFB then if possible do a full
vt switch to a !mapped vt, and prevent switching back until resume time.
This has to be called from MD code because this involves userland
running so that X can run the vt switch signal handler. This way, any
case where we are using the "poke registers from userland" model, we
will not be on the hardware when we go down, so the kernel can actually
handle thing properly.

Tested on several acpi laptops (by kettenis@ and ian@), x40 (me and
beck@ at LEAST) and zaurus (me). Maybe others, but if so I forgot who at
this time..

Idea from deraadt somewhere over the Faroe Islands (I thought of a
similar thing myself a while ago). Much prompting from him. Ok and
comments miod@


# 1.153 25-Mar-2010 oga

Fix & vs. && in the kqfilter code.

Based on a comparison with the apm code.

ok deraadt@, kettenis@


Revision tags: OPENBSD_4_7_BASE
# 1.152 26-Nov-2009 mlarkin

Visual (and on some machines, audio) feedback while the machine is
resuming via calls to aml _SST method. This also makes the "moon" LED on
thinkpads to blink during resume and cease when resume is completed.

ok pirofti@


# 1.151 26-Nov-2009 deraadt

Toggle into "cold" when we we disable interrupts, because tsleeping
in the AML interpreter and context switching into userland processes
tends to sometimes pop the ACPI trampoline page out of our address
space. Makes lots more machines work.
ok kettenis mlarkin


# 1.150 24-Nov-2009 jsg

in the resume path evaluate _WAK before dealing with device drivers
like the spec says, not after.

ok mlarkin@


# 1.149 24-Nov-2009 deraadt

abort suspends on SMP systems until that gets written; ok mlarkin


# 1.148 23-Nov-2009 mlarkin

pci should return the result of it's children's suspend/resume calls back
to its parent.

handle suspend failure case and unwind if devices have an activate function
that returns failure.

ok deraadt@, kettenis@


# 1.147 23-Nov-2009 mlarkin

acpi* devices need to call their children on suspend/resume.
ok deraadt@


# 1.146 23-Nov-2009 pirofti

Remove ACPI_SLEEP_ENABLED checks.

This enables by default the suspend/resume paths in the kernel.

Okay deraadt@.


# 1.145 23-Nov-2009 deraadt

knf and spacing that are super annoying


# 1.144 23-Nov-2009 mlarkin

Hook up Theo's new children suspend/resume function, as well as more ACPI
suspend/resume glue.

ok deraadt@


# 1.143 26-Oct-2009 deraadt

Do not do strncmp comparisons on dv_xname because longer device names which
look similar could arrive in the future. Instead, compare directly against
dv_cfdata->cf_driver->cd_name
Issue originally spotted by miod


# 1.142 26-Oct-2009 jordan

Verify checksum+address when loading ACPI tables.
Some systems had invalid entries in RSDT/XSDT.
ok marco@


# 1.141 23-Jul-2009 cnst

New aibs(4) driver for ASUSTeK AI Booster (ACPI ATK0110) hardware monitoring,
with sensor state support through limits provided by the ACPI.

Tested on several ASUS motherboards kindly networked by Sam Fourman Jr.

ok deraadt marco jordan


Revision tags: OPENBSD_4_6_BASE
# 1.140 03-Jun-2009 pirofti

Import acpi power resource driver. This is needed for suspend/resume logic.

Okay jordan@ and marco@.


# 1.139 03-Jun-2009 pirofti

Add acpivideo support. This adds brightness support for all laptops
except thinkpads, they will use the acpithinkpad driver. The driver is
also hooked into wsconsole. So brightness can be adjusted via:

$ wsconsctl display.brightness=<percentage>

This is very helpfull on some laptops that have a nasty bios and get two
steps instead of one when pressing the brightness button.

Tested on various dell, fujitsu, acer, samsung and other laptops.

Okay marco@, miod@. Suggestions from kettenis@.
Lots of reviews and help from miod@, thanks!


# 1.138 03-Jun-2009 jordan

Added ejectable bay for IDE cdroms
ok marco@


# 1.137 30-Apr-2009 marco

Oga changed apm ioctls and forgot to grep. Temporary work around by hitting
the sleep button when we get any request. This interface will chnage at
some point.
ok oga


# 1.136 19-Apr-2009 krw

Call acpi_sleep_walk() when powering down just like when entering
any other sleep state. This tells acpi wake devices to not wake up
the box until manual powering up resets their status. Makes my Dell
GX520 stay powered off in response to 'halt -p'.

In snaps for a while.

ok marco@ (several times) deraadt@


# 1.135 17-Apr-2009 pirofti

Add device ranking support for future sleep/wake developments.

okay marco@


# 1.134 11-Mar-2009 jordan

Cleanup.. use aml_evalinteger instead of aml_evalname
ok marco@


# 1.133 10-Mar-2009 jordan

Fix cut/paste error.. call _BFS not _PTS
ok marco@


Revision tags: OPENBSD_4_5_BASE
# 1.132 21-Feb-2009 marco

overzealous cut/paste; found by Vladimir Kirillov <proger@uaoug.org.ua>


# 1.131 20-Feb-2009 marco

Oops didn't mean for Debugger() to remain there.


# 1.130 19-Feb-2009 marco

suspend/resume bits so that we can develop this in tree. This is disabled.
code from mlarkin and me
help from art,toby,jordan and several others
ok jordan, go for it deraadt


# 1.129 10-Feb-2009 jordan

Fix null pointer for shutdown if acpi not enabled
ok marco@


# 1.128 04-Feb-2009 kettenis

Some systems that claim to be ACPI 2.0 apparently don't provide an XSDT
address. Make the code fall back on the RSDT on those systems.

From Joerg Niendorf <p3y80@internode.on.net>. Fixes PR 5986.


# 1.127 17-Jan-2009 krw

A couple of stray malloc/memset -> malloc(M_ZERO), a bit of knf and
lose a couple of unnecessary casts. No functional change.

Feedback from kettenis@

ok kettenis@ marco@


# 1.126 06-Nov-2008 marco

Collapse all sanity checks of hids into one function.

Originaly from jordan


Revision tags: OPENBSD_4_4_BASE
# 1.125 02-Jul-2008 fgsch

add acpivideo. for now almost an empty skel so others can work on
suspend/resume. eventually it will also manage output switching and
brightness where supported. prodded by marco@


# 1.124 11-Jun-2008 marco

Fix $OpenBSD$ for once and for all


# 1.123 08-Jun-2008 deraadt

aaa_dev was being set from an uninitialized variable; ok toby


# 1.122 07-Jun-2008 marco

Kill ACPIVERBOSE. It was never used and is worthless to boot.

comments jmc
ok toby


# 1.121 06-Jun-2008 marco

Shrink acpi some more for floppies and while here re-enable acpi on the
floppies. Fixes crazy sony vaio kettenis has.

tested by deraadt
ok deraadt jordan kettenis


# 1.120 01-Jun-2008 marco

Fix \n nit in SMALL_KERNEL.


# 1.119 01-Jun-2008 marco

Re-enable new parser. Lots of testing by various folks. Fits all media
now too.

ok kettenis deraadt


# 1.118 16-May-2008 dlg

back out jordans new parser.

it didnt have enough oks from the right people, it did break machines
that were previously working, and it bloated the RAMDISKS and install
media beyond the constraints of the media.

requested by deraadt@


# 1.117 14-May-2008 jordan

Adding new ACPI Parser code
Old guts of code still exists, needs to be torn out next
ok marco@
ok brad@


# 1.116 27-Apr-2008 jcs

add acpithinkpad(4), an acpi support driver for ibm/lenovo
thinkpads.

provides functionality that the bios did on older, non-acpi
thinkpads like respond to screen brightness keys and toggle the
bluetooth device on and off.

ok deraadt@ and marco@


# 1.115 24-Apr-2008 jsing

Introduce acpiasus(4), a driver for the ACPI based hotkeys found in many
ASUS laptops (including the ASUS EeePC) - largely based on NetBSD's
asus(4) driver. On the ASUS EeePC this allows us to enable/disable
wireless, change screen brightness and use the volume keys.

ok jsg@, weingart@


# 1.114 12-Apr-2008 kettenis

Get rid of acpi_s5 global variables; simply send SIGUSR2 instead of SUGUSR1
to tell init(8) to power down the machine.

ok krw@


Revision tags: OPENBSD_4_3_BASE
# 1.113 21-Jan-2008 jsg

Check against RESET_REG_SUP in FADT before doing the acpi reset dance
after all, not doing so appears to break more machines than it fixes.

ok marco@


# 1.112 05-Jan-2008 kettenis

Never attach more acpicpu(4) devices than cpu(4) devices. Always attach
the first acpicpu(4) device on non-MULTIPROCESSOR kernels, regardless of its
ACPI processor ID.

ok mikeb@


# 1.111 05-Dec-2007 deraadt

Hang acpi(4) below bios(4) instead of mainbus(4). This lets us move acpi(4)
before pcibios(4), and then.... if acpi(4) attaches, skip pcibios(4) since
it messes the machines up. Fixes claudio's HP dl320 G5, and almost assuredly
others.
ok kettenis


# 1.110 05-Dec-2007 deraadt

repair acpiopen/acpiclose and friends to handle the /dev/apmctl interface,
so that apmd(8) can actually work correctly against acpi(4), and thus,
even do the fancy -C/-A stuff; also tested by jmc


# 1.109 02-Dec-2007 fgsch

fix the trailing space for real now.


# 1.108 02-Dec-2007 fgsch

avoid trailing spaces on output; marco@ ok.


# 1.107 27-Nov-2007 kettenis

Fix off-by-one that made us attach acpicpu0 and acpicpu1 on GENERIC kernels.

ok marco@


# 1.106 25-Nov-2007 jsg

When available and ACPI is enabled use the ACPI reset method
to reboot. This is required for some quirky sis machines
and likely others to reboot.

Tested by many. 'commit early commit often' deraadt@


# 1.105 19-Nov-2007 deraadt

sigh, amd64 still uses that stupid acpi_enabled variable...


# 1.104 19-Nov-2007 deraadt

softc's come initialized to 0, you know...


# 1.103 19-Nov-2007 deraadt

store revision; from jsg


# 1.102 19-Nov-2007 kettenis

Initialize sc_facs earlier.

ok deraadt@, beck@, canacar@


# 1.101 16-Nov-2007 deraadt

fix the bus_space #define nightmare, so that amd64 and i386 are much more
uniform. as a result shared code like acpi needs less #ifdef's
ok marco kettenis


# 1.100 15-Nov-2007 deraadt

remove the #ifdef ACPI_ENABLE. the EC codepath is either safe enough (and
will be improved), or it is unsafe because AML assumes it must be there
ok almost everyone


# 1.99 15-Nov-2007 mikeb

Make acpicpu(4) attach to the existing CPUs only.

Problem reported by form@. Fix idea by kettenis@.
Lots of help from deraadt@. Tested by deraadt@, form@ and me.

ok deraadt marco


# 1.98 14-Nov-2007 deraadt

spacing


# 1.97 12-Nov-2007 deraadt

correct : vs , in attach-time messages


# 1.96 06-Nov-2007 deraadt

all ACPI_ENABLE kernels need acpi_interrupt()


# 1.95 06-Nov-2007 deraadt

move acpi_read_pmreg() and acpi_write_pmreg() out of SMALL_KERNEL so
that acpi ramdisks can compile (obvious solution)


# 1.94 05-Nov-2007 canacar

Establish the acpi interrupt earlier. Fixes problems with some
GENERIC.MP kernels not getting acpi interrupts. This is a
workaround until the real issue with late establishment is fixed.
ok weingart@, beck@, gwk@


# 1.93 03-Nov-2007 jordan

Added support for displaying wakeup devices
ok beck@,weingart@,gwk@


# 1.92 11-Oct-2007 kettenis

When walking the tree yo find _PRT methods, check whether the device is
actually enabled and functioning. Similar to what we do for _INI.

ok weingart@, beck@


# 1.91 08-Oct-2007 krw

More simple memset(,0,) -> M_ZERO changes. In this batch move to
size(*p) as the first malloc() parameter where p is declared locally
and thus easy to check. Add M_ZERO to gpe_table allocation in acpi.c
even though there is no obvious bzero or memset nearby.


# 1.90 13-Sep-2007 mk

Remove unnecessary memset() (see aml_evalnode(9)) and a bit of KNF.

ok toby


# 1.89 13-Sep-2007 weingart

Implement ACPI 6.5.1 spec tree walk for _STA and _INI.
Fixes mk's laptop. No regressions so far (thank you to the
testers).

ok gwk@, mk@, marco@


Revision tags: OPENBSD_4_2_BASE
# 1.88 17-Apr-2007 mk

Now that acpidock no longer hangs machines during boot (at least
according to the reports I've been getting) it should be safe to enable
it again, so remove the #if 0 stuff around it.

ok marco


# 1.87 11-Apr-2007 jordan

Added changes for C-State
ok marco@


# 1.86 26-Mar-2007 marco

Calculate minutes left more accurately.

From giovanni <qgiovanni@gmail.com>


Revision tags: OPENBSD_4_1_BASE
# 1.85 03-Mar-2007 mk

Stop looking for acpidock until we fix that stuff that breaks all pre-60
series ThinkPads with acpidock(4) enabled.


# 1.84 22-Feb-2007 marco

We were getting no interrupts in .MP. There are 2 fixes; one in
acpi_machdep.c and move the interrupt establishment down after ioapic
has been initialized.

ok jordan


# 1.83 21-Feb-2007 kettenis

Make an acpi-enabled GENERIC.MP work on systems that only support PIC mode.

ok marco@


# 1.82 19-Feb-2007 jordan

Added acpi_map_address function for mapping GAS/IO registers
ok marco@


# 1.81 18-Feb-2007 jordan

Added dynamic perfect hash for opcodes
ok marco@


# 1.80 18-Feb-2007 marco

Shave off another K pre compression.
Shuffle SMALL_KERNEL functions around so that we only have a single #ifndef


# 1.79 17-Feb-2007 marco

Add SMALL_KERNEL throught acpi to make it fit on boot media.
ok deraadt


# 1.78 13-Feb-2007 marco

Add smarter _INI code. This helps with some dissapearing devices.

ok kettenis jordan


# 1.77 31-Jan-2007 gwk

Introduce acpi_hasprocfvs thats "ACPI Has Processor Frequency and Voltage
Scalling" a global flag set by acicpu attach to indicate that ACPI is
handling fvs through the _PSS objects. This will fix disappearing
EST/powernow when booting kernels with acpi enabled, as acpicpu isnt
compiled into GENERIC yet. Also in cases where acpicpu is built in and
legacy EST/powernow work but acpi is missing the _PSS object, the legacy
routines will be used.

tested by dim@, and Nick Nauwelaerts <nick AT nauwelaerts.net>
ok marco@, dim@


# 1.76 22-Jan-2007 mk

Actually look for docking stations by looking for _DCK nodes.

ok marco


# 1.75 02-Jan-2007 marco

On some systems EC must be attached as the first device. Move EC from
generic _HID devices to its own routine to force first attach. Bug found
and debugged by joris@

Code mostly written by joris@


# 1.74 26-Dec-2006 marco

Add polling to devices that require it such as acpibat and acpitz.
Use the same tsleep/wakeup mechanism as the interrupt code to keep it non-
concurrent.


# 1.73 21-Dec-2006 deraadt

even more knf love


# 1.72 21-Dec-2006 marco

Remove some leftover acpiec gpe/interrupt code.


# 1.71 20-Dec-2006 deraadt

oops, name already used


# 1.70 20-Dec-2006 deraadt

rename __acpi_enable_gpe(); ok marco


# 1.69 20-Dec-2006 deraadt

acpi_unmap_pmregs() is not used, ok marco


# 1.68 19-Dec-2006 deraadt

simplify some low-level methods substantially (shrinking code); ok marco jordan


# 1.67 06-Dec-2006 mk

malloc(..., M_NOWAIT) with no NULL check is bad. If allocation fails,
complain, clean up, and bail out of attach.

input and ok gwk


# 1.66 28-Nov-2006 jordan

Fixed clearing arg for acpi_init_pic
ok kettenis@


# 1.65 27-Nov-2006 kettenis

Don't spam dmesg.


# 1.64 27-Nov-2006 jordan

Fixes for acpiprt routing
Post-parse fixup for namerefs
ok kettenis@


# 1.63 25-Nov-2006 marco

Don't handle acpiev events inside the gpe handler since this causes deadlocks.

ok jordan@


# 1.62 15-Nov-2006 kettenis

Hook up acpi apic interrupt routing support to generic acpi code.

ok jordan@


# 1.61 02-Nov-2006 marco

Kill acpiec attach function and add its functionality to the generic _HID
device attach

prodded and ok jordan


# 1.60 02-Nov-2006 marco

Fix comments to reflect reality.


# 1.59 19-Oct-2006 marco

Replace ACPI ioctls with APM ioctls. This makes ACPI talk the same protocol
as APM therefore userspace can now retrieve power information in the same
fashion as all other apm arches (zaurus, macppc, i386 etc).

To use force the use of acpi device like so: apmd -f /dev/acpi

ok jordan@


# 1.58 19-Oct-2006 jordan

Fixed GPE debug levels
Fixed ref counter on temporary bufferfields
DSDT parsing fully working on IBM Thinkpad
ok marco@


# 1.57 15-Oct-2006 jordan

Removed old GPE handler code remnants
ok marco@


# 1.56 12-Oct-2006 jordan

Massive ACPI changes
Fixed ACPI leaks
new ACPI API
ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.55 30-Jun-2006 gwk

KNF, No functional change,
"go at it" marco@


# 1.54 30-Jun-2006 jordan

Support for calling Embedded Controller via new GPE interface
ok marco@


# 1.53 30-Jun-2006 jordan

Added new code for GPE handling
Also works with ACPIEC; need to fixup acpiec.c to remove original interrupt handler
ok marco@


# 1.52 30-Jun-2006 jordan

Added offset argument for acpi read/write pmreg routines (new GPE code)
ok marco@


# 1.51 31-May-2006 todd

do not call acpi_powerdown() unless acpi has successfully initialized
ok gwk@ canacar@


# 1.50 29-May-2006 canacar

Add embedded controller support.
ok marco@ deraadt@ testing & ok gwk@


# 1.49 19-May-2006 canacar

Minimal thermal zone driver. Monitors thermal zone temperature,
shuts down the system if the 'critical temperature' is reached.
ok marco@


# 1.48 11-Apr-2006 marco

KNF.


# 1.47 11-Apr-2006 gwk

Add acpi_resume(struct acpi_softc *sc), which calls some AML methods,
detected at attach time, currently unused.

ok marco@


# 1.46 11-Apr-2006 gwk

Discover some ACPI sleep related methods, and call them at the
appropriate time in acpi_enter_sleep_state().

ok marco@


Revision tags: OPENBSD_3_9_BASE
# 1.45 26-Feb-2006 marco

Add acpicpu as a device.

ok jordan@


# 1.44 23-Feb-2006 marco

remove dead function.

ok jordan@


# 1.43 22-Feb-2006 marco

Print out all tables it found during autoconf. We'll need this data in
the future.


# 1.42 22-Feb-2006 marco

Make dmesg less verbose.

ok jordan.


# 1.41 22-Feb-2006 jordan

Added parsing of SSDT tables
ok marco@


# 1.40 22-Feb-2006 jordan

Added new aml_register to notify by device id
Moved powerdown code to button device
ok marco@


# 1.39 21-Feb-2006 marco

Readjust debug levels that crept in too low over the last few days.
KNF


# 1.38 21-Feb-2006 marco

Rig the power button so that the machine shuts down correctly.


# 1.37 20-Feb-2006 marco

Add acpibtn device. Remains disabled for now.


# 1.36 19-Feb-2006 jordan

Added code for aml_notify GPE notification callback
ok marco@


# 1.35 19-Feb-2006 grange

Fix acpi_enter_sleep_state() by replacing current ugly hack
with mostly correct sleeping state transition code.

ok brad@ marco@


# 1.34 19-Feb-2006 marco

jordan:
Enable interrupts.
Added peripheral code to make this work.

marco:
Replace softintr and other types of interrupts with a handler with context.
Fixed interrupt magic for GPEs.
Some KNF

This is work done by jordan and I but has to be a single commit.


# 1.33 17-Feb-2006 marco

Remove earlier introduced delays. We really only need them during autconf.

ok jordan@


# 1.32 17-Feb-2006 marco

Add a generic delay function as per the ACPI spec.

from jordan@


# 1.31 17-Feb-2006 marco

Use tsleep instead of delay when !cold.


# 1.30 17-Feb-2006 marco

Argh, we need delays between reads/writes. The backend reads (i2c most likely)
are so slow that values come back as 0 sometimes. We have to do this
despite the spec saying we shouldnt have to. Guess having a faster
parser than others can hurt sometimes.

Idea from jordan.


# 1.29 17-Feb-2006 jordan

Fixed code for handling _INI
Rearranged debug statements
ok marco@


# 1.28 16-Feb-2006 jordan

Added stub for calling _INI
Fixed aml_dowhile for NULL return
ok marco@


# 1.27 16-Feb-2006 jordan

Fixed parser/evaluator to retrieve battery info+state
Cleanup of parser
Dell laptops return battery status
ok marco@


# 1.26 06-Feb-2006 brad

fix a typo so this will compile on amd64.


# 1.25 04-Feb-2006 marco

When ACPI_ENABLED wasn't set but the devices were it had an unknown variable.

Pointed out by ian@.


# 1.24 03-Feb-2006 jordan

Rewrote aml parser yet again...
Fixed bus_space_map for i386/amd64
Added _BST and _BIF to acpibat to get status/info
ok marco@


# 1.23 20-Jan-2006 jordan

More evaluator changes
Removed old evaluator code
Fixed _HID value
ok marco@


# 1.22 19-Jan-2006 jordan

Added support for read/write of PCI Config space
Help jason@
ok marco@


# 1.21 18-Jan-2006 jordan

Added new evaluation routines
Added helper methods for buffer/field evaluation
Fixed broken indentation on dsdt.c

ok marco@


# 1.20 17-Jan-2006 jordan

Major overhaul of the aml parser. Evaluater temporarily disabled.

ok marco@


# 1.19 06-Jan-2006 grange

Check for ACPI control availability only if ACPI_ENABLE defined.


# 1.18 06-Jan-2006 grange

Informative error messages on attach, prettier dmesg.


# 1.17 06-Jan-2006 grange

printf -> dnprintf conversion for debug messages.


# 1.16 05-Jan-2006 grange

Find SLP_TYPx values and store them for future use in
acpi_enter_sleep_state().


# 1.15 05-Jan-2006 grange

Random KNF and cleanup


# 1.14 05-Jan-2006 grange

Provide symbolic names for the acpi sleeping states and use it.


# 1.13 28-Dec-2005 marco

Moved DEVNAME to a acpidev.h

ok jordan@


# 1.12 18-Dec-2005 sturm

add back comment about wrong code

ok marco


# 1.11 16-Dec-2005 marco

Remove test for kbd_reset. Prompted deraadt@, ok jordan@


# 1.10 16-Dec-2005 jordan

Added hooks for powerdown/power button press


# 1.9 16-Dec-2005 jordan

Cleaned up common power management register i/o
Added enter sleep state ioctl
Hooked power button interrupt
ok marco@


# 1.8 16-Dec-2005 marco

Remove autoconf chatter


# 1.7 16-Dec-2005 jordan

Moved DSDT AML parsing into parent ACPI driver
ok marco@


# 1.6 14-Dec-2005 marco

Add AC device glue.


# 1.5 14-Dec-2005 marco

Adjust debug printing levels to something more sane.


# 1.4 13-Dec-2005 marco

Add glue to attach devices to apci.


# 1.3 07-Dec-2005 marco

KNF


Revision tags: OPENBSD_3_8_BASE
# 1.2 10-Jul-2005 grange

Spacing cleanup.


# 1.1 02-Jun-2005 tholo

Start on a basic ACPI framework -- does not do much more than read out the
ACPI tables into kernel memory and attach ACPI and HPET timers currently.

In order to test this code, enabling the devices in GENERIC as well as
the ACPI_ENABLE option is needed. This code does not do any thermal
control yet, so this should be done with care depending on the platform.

In the tree so more people can contribute to making this more fully
featured.

Ok niklas@ grange@ tedu@


# 1.423 06-Jul-2023 deraadt

moving acpiioctl to a different file than acpiopen is not going to work
for ramdisks. noticed by anton.
this must be reconsidered.


# 1.422 05-Jul-2023 tobhe

Move acpiioctl to acpi_x86.c, it is only used up on i386 and amd64.

ok kettenis@ deraadt@


# 1.421 29-Jun-2023 dv

acpi(4)/acpibtn(4): use opt-in approach for wakeup GPEs

Previously, any discovered GPE that could be enabled for wake was
being enabled prior to entering ACPI-based S3 or S4. On some newer
machines, this caused S3-based suspend to break as some devices we
don't fully control (e.g. lack of driver) might be capable of waking
the system and will do so almost instantly if the GPE is enabled for
wake up (i.e. the so called "instant wake" issue).

This changes to a model of having device drivers explicitly opt
into using their GPE for wake up.

The first driver to explicitly toggle a GPE for wake is acpibtn(4).

Variations of the above have been in snapshots for 2 weeks, so
tested by many.

ok deraadt@


Revision tags: OPENBSD_7_3_BASE
# 1.420 15-Mar-2023 kettenis

Add missing nameref resolving for the _DEP() package elements that indicate
dependencies between ACPI devices.

ok dlg@, deraadt@


# 1.419 10-Feb-2023 visa

Adjust knote(9) API

Make knote(9) lock the knote list internally, and add knote_locked(9)
for the typical situation where the list is already locked.

Remove the KNOTE(9) macro to simplify the API.

Manual page OK jmc@
OK mpi@ mvs@


Revision tags: OPENBSD_7_2_BASE
# 1.418 13-Sep-2022 kettenis

Split out the code that collects data from acpiac(4), acpibat(4) and
acpisbs(4) for apm(4) and hook it up to the arm64 version of apm(4) on
systems with ACPI.

ok kn@


# 1.417 12-Sep-2022 kettenis

Add support for level-triggered GPIO events.

ok mlarkin@


# 1.416 10-Sep-2022 kettenis

Don't restrict GPIO events to pin numbers below 256. With the _EVT()
method events corresponding to larger numbers are possible.

Fixes lid state detection on the x13s.

ok patrick@


# 1.415 03-Sep-2022 kettenis

Make sure we don't suspend if we have no way to wake up the machine.
The idea is that device drivers that can wake up the machine register
themselves by calling device_register_wakeup(). To prevent regressions
on amd64, we let acpi(4) register itself as a wakeup device if the AML
for the machine defines wakeup devices. This may be refined in the
future.

This diff will prevent people from suspending their arm64 SBCs without
having a way to wake them up. For now the only driver that registers
itself is axppmic(4), which means that at this moment only SBCs with
an allwinner A64 or H5 SoC will actually support suspend/resume.

ok mlarkin@, deraadt@


# 1.414 10-Aug-2022 patrick

Provide the AML root in ACPI's softc so that drivers that need access
don't have to declare it using extern.

Suggested by and with feedback from kettenis@
Tested by deraadt@ on arm64, amd64 and i386
Tested by bluhm@ on amd64 and i386


Revision tags: OPENBSD_7_1_BASE
# 1.413 17-Feb-2022 jsg

reduce includes


# 1.412 10-Feb-2022 visa

Embed klist head in acpi_softc to avoid explicit malloc.

OK kettenis@


# 1.411 09-Feb-2022 deraadt

Move x86-only stubs used by kern/subr_suspend.c into dev/acpi/acpi_x86.c
for sharing between i386 and amd64.
ok mlarkin kettenis


# 1.410 08-Feb-2022 deraadt

The suspend/resume code is a sticky mess of MI, MD, and ACPI sequencing.
This splits out the MI sequencing, backing it with per-architecture helper
functions. Further steps will be neccesary because ACPI and MD are too
tightly coupled, but soon we'll be able to use this code for more architectures
(which depends on figuring out the lowest-level cpu sleeping method)
ok kettenis


# 1.409 04-Feb-2022 robert

set the APM_BATT_CHARGING state if the battery is being charged and try
to calculate the remaining time to be fully charged

ok jca@


# 1.408 02-Feb-2022 deraadt

acpi_addtask() calls malloc() w/ M_NOWAIT (because some calls come from
interrupt context), this however means occasional resource shortage will
result in callbacks registration failing, and unknown consequences for
the task-submitting caller.
Changing this to use pools with a low water mark, decreases the odds
of that problem occuring.
ok kettenis


# 1.407 01-Feb-2022 deraadt

GPE_DIRECT is calling the AML parser from interrupt context, which is
not permitted. Luckily nothing is using GPE_DIRECT anymore, so this code
can be deleted.
ok kettenis


# 1.406 26-Jan-2022 kettenis

An ACPI device needs to be both present and enabled for it to function.
So only attempt to attach hardware that has both bits enabled. This fixes
an issue where com(4) would attach for a disabled serial port leading to
misdetection of the hardware variant and a subsequent hang when /etc/rc
runs ttyflags -a.

ok anton@, deraadt@


# 1.405 12-Jan-2022 patrick

Make acpi_getpropint() return uint64_t, as ACPI integers are in fact that
wide and some _DSD properties depend on it.

ok kettenis@


# 1.404 04-Jan-2022 patrick

acpi_getprop() needs to actually make sure that we're looking at the
correct property. While there adjust acpi_getpropint() as well to
increase similarity with acpi_getprop().

ok kettenis@


# 1.403 16-Dec-2021 anton

Attach com over acpi on amd64. Some hardware uses a different interrupt
assignment compared to the the legacy one supported by com over isa.
This causes the console to halt once userland takes over as no
interrupts are received. The actual address and irq can be read from
ACPI, kettenis@ already added support for arm64 which paved the way for
amd64.

Some consoles that previously attached over isa are now expected to
attach over acpi.

Thanks to patrick@ for testing on arm64.

ok kettenis@


# 1.402 07-Dec-2021 kettenis

Fix parsing of SR_IRQ resource descriptors.

ok patrick@, anton@


# 1.401 02-Nov-2021 deraadt

knf


# 1.400 30-Oct-2021 deraadt

Change hw.perfpolicy=auto by default, at startup. If the system has AC
power connected (default is yes when no driver differentiates) then default
to 100% performance. On battery, use the existing auto algorithm (which is
admittedly somewhat unrefined).
This change overrides the system/BIOS speed and puts OpenBSD in control.
As this happens very early during boot, besides speedups in all usage usage
patterns, some surprises: unhibernate and sysupgrade times are cut in half.
note: on a few architectures, the setperf fn pointer is changed late, and
thus the auto algorithm stops timeing out. kettenis and i will look for
a solution.
in snaps for more than a week.
ok kettenis


Revision tags: OPENBSD_7_0_BASE
# 1.399 20-Jul-2021 mlarkin

Fix trailing whitespace in a few places.

No code change.


# 1.398 09-May-2021 kettenis

Some x86 machines advertise the "hardware reduced" ACPI feature, advertise
S4 and S5 support, but fail to populate the SLEEP_CONTROL_REG and
SLEEP_STATUS_REG descriptions in the FADT. An example of such a machine
is the ASUS Zenbook 14 UM433DA. Any attempt to powerdown the machine
will result in a kernel crash. It turns out that using the legacy ACPI PM
registers works fine on this machien. So fall back on those registers
if SLEEP_CONTROL_REG and/or SLEEP_STATUS_REG aren't provided.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_9_BASE
# 1.397 15-Mar-2021 patrick

Add acpi_iommu_device_map(), which replaces the DMA tag with one that
is blessed with IOMMU magic, if available. This is mainly for arm64,
since on amd64 and i386 the IOMMU only captures PCIe devices, as far
as I know, which uses the pci_probe_device_hook(). This though is for
non-PCI devices attached through ACPI.

ok kettenis@


# 1.396 07-Mar-2021 patrick

Pass standard DMA tag to acpi(4) table drivers.

ok kettenis@


# 1.395 27-Dec-2020 deraadt

acpi_map_address() cannot be wrapped by SMALL_KERNEL anymore, as
it is used by acpihpet.c


# 1.394 25-Dec-2020 visa

Refactor klist insertion and removal

Rename klist_{insert,remove}() to klist_{insert,remove}_locked().
These functions assume that the caller has locked the klist. The current
state of locking remains intact because the kernel lock is still used
with all klists.

Add new functions klist_insert() and klist_remove() that lock the klist
internally. This allows some code simplification.

OK mpi@


# 1.393 17-Dec-2020 kettenis

Fix some issues with referencing named ACPI nodes from Packages.
These references need to be resolved at runtime rather than when
they're parsed such that they pick up the right values for those nodes
which can be changed when for example _INI methods run.

The current approach is to replace these reference with a string that
names the node in question. The problem with that is that packages
can also contain normal strings. Which means that we need special
code that depends on the context where the package is used.

This diff takes a different approach by retaining a reference when
parsing. Code that uses a package will need to resolve this reference
but there is no ambiguiety anymore.

ok patrick@


# 1.392 05-Dec-2020 kettenis

Add support for the _EVT method to the GPIO support code.

ok patrick@


Revision tags: OPENBSD_6_8_BASE
# 1.391 27-Aug-2020 jmatthew

Reorder the acpi attach process so we look at Processor() CPU nodes
after Device() ones, since we should prefer the newer node type.
If we see any Device() nodes, don't attach acpicpu(4) to any Processor()
nodes if they're also present. This also makes acpitz(4) and
acpipwrres(4) devices attach slightly later.

ok kettenis@, also tested by jmc@


# 1.390 16-Aug-2020 gnezdo

Remove sc_maxgpe from acpi_softc which was never read from

Remove a dead conditional from acpi_init_gpes while there.

ok jcs@


# 1.389 21-Jul-2020 deraadt

acpi can use IPL_BIO (a low interrupt) since it only enqueues operations for
later processing. The use of a high interrupt will predate suspend/resume
efforts, we had to redesign acpi to be non-reentrant obviously
discussed with kettenis, in snaps for more than a week


# 1.388 24-Jun-2020 cheloha

kernel: use gettime(9)/getuptime(9) in lieu of time_second(9)/time_uptime(9)

time_second(9) and time_uptime(9) are widely used in the kernel to
quickly get the system UTC or system uptime as a time_t. However,
time_t is 64-bit everywhere, so it is not generally safe to use them
on 32-bit platforms: you have a split-read problem if your hardware
cannot perform atomic 64-bit reads.

This patch replaces time_second(9) with gettime(9), a safer successor
interface, throughout the kernel. Similarly, time_uptime(9) is replaced
with getuptime(9).

There is a performance cost on 32-bit platforms in exchange for
eliminating the split-read problem: instead of two register reads you
now have a lockless read loop to pull the values from the timehands.
This is really not *too* bad in the grand scheme of things, but
compared to what we were doing before it is several times slower.

There is no performance cost on 64-bit (__LP64__) platforms.

With input from visa@, dlg@, and tedu@.

Several bugs squashed by visa@.

ok kettenis@


# 1.387 10-Jun-2020 jca

Give userland a consistent battery and A/C state after resume

Previously we refreshed the state of acpiac(4) and acpibat(4)/acpisbs(4)
by queueing fake ACPI events to be processed by the acpi thread.
acpibat(4) using ACPIDEV_POLL meant that its status could be refreshed
first, resulting in an APM_POWER_CHANGE event sent to userland before
the state of acpiac(4) was up to date. Because of this, apmd(8) could
see A/C unplugged when it was in fact plugged, possibly leading to
a wrong decision.

Instead of using fake ACPI events, refresh the status of acpiac(4) and
acpibat(4)/acpisbs(4) right away at DVACT_WAKEUP time. To reuse the
existing refresh code of those drivers, make their ACPI *_notify()
handlers responsible for sending APM_POWER_CHANGE events instead. Also
queue an APM_POWER_CHANGE event right after resume to retain existing
behavior.

This fixes the consistency of data shown to userland, and makes those
drivers less special.

Initially discussed last year (!) with mpi@ at Miod's place.
Feedback and ok jcs@, deraadt@ agrees with the use of DVACT_WAKEUP here.


# 1.386 29-May-2020 deraadt

dev/rndvar.h no longer has statistical interfaces (removed during various
conversion steps). it only contains kernel prototypes for 4 interfaces,
all of which legitimately belong in sys/systm.h, which are already included
by all enqueue_randomness() users.


# 1.385 14-May-2020 kettenis

Bring back the acpi(4) changes. This time around only use the ACPI _CRS
information on ACPI 5.0 and later.

ok krw@, patrick@


# 1.384 11-May-2020 jca

More accurate remaining power reporting with disparate batteries

Overall remaining power was computed as the average of the remaining
power of each battery, in percents. This doesn't work with batteries of
different capacity, so use the total capacity and remaining power
instead.

Reported by lidstah, successful tests by lidstah and benno@, ok kettenis@


# 1.383 08-May-2020 kettenis

Move parsing of _CRS into acpi(4) and pass the parsed address and interrupt
information in struct acpi_attach_args.

ok mpi@


Revision tags: OPENBSD_6_7_BASE
# 1.382 14-Apr-2020 kettenis

Implement acpi_getprop(), the ACPI equivalent of OF_getprop().

ok patrick@


# 1.381 12-Apr-2020 kettenis

Add support for the _CCA method. This method indicates whether DMA is
cache-coherent or not. To implement this, acpi(4) gets two bus_dma tags
and passes the appropriate one when attaching devices based on _CCA.
On i386/amd64, where for all practical purpose DMA is always cache-coherent,
the two tags are the same. But on arm64 they are distinct.

ok patrick@


# 1.380 07-Apr-2020 visa

Abstract the head of knote lists. This allows extending the lists,
for example, with locking assertions.

OK mpi@, anton@


# 1.379 03-Apr-2020 mpi

Fix incoherencies of errors returned by various *kqfilter routines.

EOPNOTSUPP is returned when there's no "filterops" for a given fd.
EINVAL when the requested filter isn't supported by the "filterops".
ENXIO means the underlying device is no longer valid/present.

ok visa@


# 1.378 20-Feb-2020 visa

Replace field f_isfd with field f_flags in struct filterops to allow
adding more filter properties without cluttering the struct.

OK mpi@, anton@


# 1.377 31-Dec-2019 visa

Use C99 designated initializers with struct filterops. In addition,
make the structs const so that the data are put in .rodata.

OK mpi@, deraadt@, anton@, bluhm@


# 1.376 31-Dec-2019 mpi

Convert infinite sleeps to {m,t}sleep_nsec(9).

ok kn@


# 1.375 31-Dec-2019 jsg

include sys/sensors.h for sensor use
don't depend on indirect inclusion from acpidev.h


Revision tags: OPENBSD_6_6_BASE
# 1.374 07-Sep-2019 kettenis

Revert acpipci(4) changes. Too many systems are broken. We'll try again
after 6.6 as been released. The acpireg.h change stays behind.


# 1.373 28-Aug-2019 kettenis

Use ACPI information to attach PCI busses like we do on arm64. There are a
few additional quirks though, and attaching the PCI busses is delayed to
replicate the existing code more closely. That may be changed in the
future. Also tweak how we handle MSI support and respect to ACPI flag
that says we shouldn't attempt to use MSIs.

Some fallout is expected.

ok patrick@


# 1.372 27-Aug-2019 deraadt

On amd64 the bootup and resume paths for SP/BP/SP were not performing
msr/pat, clock-startup, firmware upgrade, fpu initialization, and cpu
soft-state initialization in the same order. That kind of chaos is
crazy and fragile. Improve the acpi resume call sequence and unify order
of the operations.


# 1.371 02-Jul-2019 jcs

acpi_set_gpehandler: turn edge arg into flags, add support for
passing GPE_DIRECT which directly executes the supplied callback and
clears a level interrupt rather than going through acpi_taskq which
can be slow when other driver callbacks are in the queue.

This should not be used for GPE handlers that need to do anything
ACPI-related.

ok kettenis


# 1.370 10-Jun-2019 kettenis

Print proper ACPI version number.

ok mlarkin@


# 1.369 08-Jun-2019 kettenis

When establishing the mapping between ACPI device nodes and PCI devices,
skip PCI host bridges and devices that aren't there.

ok patrick@


# 1.368 07-Jun-2019 kettenis

Introduce acpi_getsta() and use it to eliminate some duplicated code.

ok patrick@


# 1.367 12-May-2019 kettenis

Make sure the extended PM register descriptions actually exist before
using them. Fixes machines with ancient ACPI 1.0 (such as recent
hypervisors).

ok deraadt@


# 1.366 12-May-2019 kettenis

Add support for mapping ACPI PM registers using the "extended" gasio
representation from the FADT. Mostly fixes the Lanner NCA-1510.

ok lteo@, mlarkin@, deraadt@


# 1.365 11-May-2019 lteo

Fix incorrect error message.

ok kettenis@


# 1.364 08-May-2019 tedu

free size. the allocation is the header plus the length.


Revision tags: OPENBSD_6_5_BASE
# 1.363 04-Apr-2019 kettenis

Avoid potential null-pointer dereference. Found by Kent R. Spillner.

ok deraadt@, jsg@


# 1.362 20-Jan-2019 jsg

fix SMALL_KERNEL build after changes in rev 1.361
reported by naddy@ ok deraadt@


# 1.361 19-Jan-2019 tedu

Add a pwraction sysctl that controls what the power button does on acpi.
By default, nothing changes -- shutdown is initiated. But allows turning
power button into a sleep button if desired.
(grudging) ok from a few parties


# 1.360 26-Oct-2018 kettenis

Add acpipci(4) on amd64. For now this only calls the PCI-specific _OSC
method to let the ACPI implementation know what features we support.


Revision tags: OPENBSD_6_4_BASE
# 1.359 30-Aug-2018 kettenis

Move softbutton check before the check that skips attaching certain acpi
devices. Fixes regression caused by matching on _CID in addition to
matching on _HID.

ok matthieu@, mlarkin@


# 1.358 25-Aug-2018 kettenis

Match ACPI devices based on _CID if no match for _HID is found.

ok mpi@


# 1.357 19-Aug-2018 kettenis

Add support for multiple PCI segments. Only really implemented for arm64
for now as amd64/i386 firmware still caters for legacy OSes that only
support a single PCI segment.

ok patrick@


# 1.356 03-Aug-2018 kettenis

Let ahci(4) match on _CLS instead of _HID when attaching at acpi(4). Avoids
having to add many more _HID entries to the match table.

ok deraadt@, mlarkin@


# 1.355 10-Jul-2018 kettenis

Make legacy interrupts work in acpipci(4).

ok patrick@


# 1.354 05-Jul-2018 kettenis

Add acpipci(4), a driver that supports generic ECAM-compatible PCI host
bridges based on information provided by ACPI.

ok mlarkin@


# 1.353 04-Jul-2018 kettenis

Properly pass around the PCI "chipset tag" in acpi(4) and refactor
acpimcfg(4) to call an MD initialization functions that sets up a tag for
PCI ECAM.

ok guenther@, mlarkin@, krw@


# 1.352 01-Jul-2018 kettenis

Move acpi(4) attach glue into acpi_machdep.c.

ok guenther@, deraadt@


# 1.351 01-Jul-2018 kettenis

Add support for _DSD Device Properties.

ok mlarkin@


# 1.350 30-Jun-2018 kettenis

Add intr_enable() function, intended for MI use to amd64 and i386 and use
this in the acpi(4) suspend/resume code paths.

ok deraadt@


# 1.349 29-Jun-2018 kettenis

Use <stdint.h> types.

ok mpi@, jung@, krw@, deraadt@


# 1.348 29-Jun-2018 mlarkin

ACPI: Allow (with warning) GPE handler reassignment, instead of returning
a failure code.

ok kettenis


# 1.347 26-Jun-2018 mlarkin

knf


# 1.346 26-Jun-2018 kettenis

Let acpi(4) pass down the bus dma tag.

ok deraadt@


# 1.345 25-Jun-2018 kettenis

Reorganize acpi(4) code a bit in preparation for upcoming arm64 support.

ok deraadt@


# 1.344 20-May-2018 kettenis

Wake up the acpi thread after scheduling a gpio event.

ok mpi@


# 1.343 17-May-2018 kettenis

Sprinkle some #ifndef SMALL_KERNEL such that RAMDISK kernels cuild again.

pointed out by stsp@


# 1.342 17-May-2018 kettenis

Implement GenericSerialBus OpRegion support.

ok mlarkin@


# 1.341 27-Mar-2018 kettenis

Add acpicmos(4), a driver that implements SystemCMOS OperationRegion
access support. This fixes machines where the AML doesn't check whether
support for this OperationRegion type has been registered by the OS.

ok mlarkin@


Revision tags: OPENBSD_6_3_BASE
# 1.340 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.339 10-Feb-2018 deraadt

Syncronize filesystems to disk when suspending. Each mountpoint's vnodes
are pushed to disk. Dangling vnodes (unlinked files still in use) and
vnodes undergoing change by long-running syscalls are identified -- and
such filesystems are marked dirty on-disk while we are suspended (in case
power is lost, a fsck will be required). Filesystems without dangling or
busy vnodes are marked clean, resulting in faster boots following
"battery died" circumstances.
Tested by numerous developers, thanks for the feedback.


# 1.338 08-Feb-2018 deraadt

After flushing disks, try again to release all memory. We don't need to
save dirty memory to the hibernate space.


# 1.337 08-Feb-2018 deraadt

Toss all releaseable memory, because fragmentation can get in the way
of allocating the hibernate playpen.


# 1.336 08-Feb-2018 deraadt

sr_quiesce() is a new approach for ensuring that softraid drains
output to the disks.

This is part of a larger suspend/resume filesystem-safety diff, which
has been worked on for a couple of months already. Tests by job, krw,
beck, benno, and others. Sometimes even by snapshot users...


# 1.335 29-Nov-2017 kettenis

Add support for GPIO-signaled events.

ok mlarkin@


# 1.334 16-Nov-2017 jcs

Add PCI attachment for dwiic(4) needed by Intel 100 Series machines

ok kettenis


# 1.333 14-Nov-2017 kettenis

Add a few more PNP IDs to the skip lists.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.332 17-Aug-2017 stsp

Make dwiic(4) attach its ACPI parent dependencies.
This change makes the Asus E200HA keyboard work.

Original analysis and patch by Cesare Gargano
ok kettenis@


# 1.331 09-Aug-2017 dcoppa

Add "PNP0303" (8042 PS/2 Controller) to acpi_isa_hids[]

ok kettenis@


# 1.330 22-Jul-2017 jcs

acpibat: look for _BIX first before falling back on _BIF

ACPI 4.0 deprecated _BIF for battery status so some newer machines
are exporting _BIX which extends _BIF's package with a few extra
fields. When using _BIX, export the new discharge cycle count as a
sensor.

feedback from kettenis and anton
ok deraadt


# 1.329 20-Jul-2017 kettenis

Start a list of isa(4) device names such that we can avoid printing them
as "not configured".

ok mikeb@, millert@, deraadt@


# 1.328 08-Apr-2017 deraadt

Sensors are run as callbacks inside tasks. During suspend a sensor
could be running inside a driver that will be force-detached, or due
to tsleep end up disrupting the softstate/hardstate contract. At
suspend time, quisce all these callbacks by waiting for completion.
This issue has never been observed for real, but may be implicated in
suspend/resume failures.
ok kettenis guenther mlarkin


# 1.327 08-Apr-2017 deraadt

Two prototypes for acpi_maptable; merge to one place


Revision tags: OPENBSD_6_1_BASE
# 1.326 27-Mar-2017 deraadt

Now that hibernate_alloc() only has clean success/failure, don't
need to call hibernate_free() to clean up a partial mess.
ok mlarkin kettenis


# 1.325 13-Mar-2017 deraadt

allow ramdisks to build; ok jcs


# 1.324 12-Mar-2017 jcs

try to attach acpisbs first and if successful, don't attach acpibat


# 1.323 02-Mar-2017 natano

Add a new sysctl machdep.lidaction. The sysctl works as follows:

machdep.lidaction=0 # do nothing
machdep.lidaction=1 # suspend
machdep.lidaction=2 # hibernate

lidsuspend is just an alias for lidaction, so if you change one, the
other one will have the same value. The plan is to remove
machdep.lidsuspend eventually when people have upgraded their
/ets/sysctl.conf.

discussed with deraadt, who came up with the new MIB name
no objections mlarkin
ok stsp halex jcs


# 1.322 28-Feb-2017 natano

Untangle abstract sleep modes from ACPI sleep states. This paves the way
for further work in the area.

idea and ok deraadt
ok mlarkin


# 1.321 22-Feb-2017 jcs

add acpisbs, an acpi smart battery subsystem driver reading data
over smbus

currently disabled because it conflicts with acpibat

ok deraadt, kettenis


# 1.320 14-Jan-2017 kettenis

Revert aml_rdpciaddr changes; breaks several machines that were working
before.


# 1.319 11-Jan-2017 jsg

Increase the size of the buffers used for _HID and _CID strings to be
able to fit "Hyper_V_Gen_Counter_V1".

ok mlarkin@ deraadt@


# 1.318 08-Jan-2017 kettenis

Make aml_rdpciaddr() work for for devices behind bridges. The code now
uses the mapping between AML nodes and PCI devices that we establish.
Because _INIT methods may end up calling aml_rdpciaddr(), make sure we
create that mapping early. Also handle devices that aren't actually present.
These devices are now included in the mapping and reads will return an
all-ones patterm whereas writes are a no-op..

ok mlarkin@


# 1.317 25-Oct-2016 pirofti

Add the resource index to aml_parse_resource() callback function.

This is needed by an upcoming acpiec commit that handles machines breaking
the current ACPI specifications.

Change suggested by and ok kettenis@, guenther@


# 1.316 18-Sep-2016 guenther

If the APIC table (MADT) isn't present then ci_acpi_proc_id won't be set
when acpicpu tries to attach, leading to a NULL dereference. The ACPI
processor object on the Dells where this was reported is bogus and useless
anyway so apply the same test when deciding whether to attach to processor
objects as acpicpu would when matching them to cpus.

Problem reported by Eivind Eide (xenofil (at) gmail.com) and
Johan Huldtgren (johan+openbsd-bugs (at) huldtgren.com).
Testing by Eivind Eide.
ok kettenis@ mlarkin@


# 1.315 03-Sep-2016 naddy

Write the system time back to the RTC every 30 minutes.
This fixes the problem that long-running machines which were not
shut down properly would reboot with a badly offset system time.

hints and ok kettenis@


# 1.314 31-Aug-2016 mlarkin

Don't put a device in the wake device list if _STA says it's not there.

Fixes at least one machine that woke up from sleep / poweroff immediately.

ok kettenis@, tested by kettenis and martijn@


# 1.313 28-Jul-2016 kettenis

Store the acpi processor ID/UID in struct cpu_info, and use it to attach
acpicpu(4) drivers to the right cpu(4).

ok mlarkin@, guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.312 10-Jun-2016 kettenis

The power management timer has been made optional in ACPI 5.0A, so don't
attempt to attach acpitimer(4) if the timer isn't present.

Pointed out by jcs@

ok jcs@


# 1.311 08-May-2016 kettenis

Make sure devices listed by a device's _DEP method are attached before
we attach the device itself. The _DEP methode, introduced in ACPI 5.0,
is a hint that the device in question depends on OpRegion support from
the devices listed by _DEP, so we have to order them the proper way.
To prevent us from attaching those devices again when we encounter them
later walking down the device tree, keep track of our attempts to
attach a device driver to them using aflag in the aml_node structure.

ok guenther@


# 1.310 25-Apr-2016 pirofti

We attach acpihpet(4) and acpiec(4) without using the HID, so do not
report it in the dmesg.

OK deraadt@, mlarkin@.


# 1.309 02-Apr-2016 kettenis

Reduce dmesg spam by knocking out some acpi hardware IDs that don't need a
driver.

ok mpi@


# 1.308 29-Mar-2016 kettenis

Check if a device is present (using _STA) in acpi_foundhid. This prevents
us from reporting devices as "not configured" that aren't actually present,
and allows us to remove duplicated code from several drivers.

ok deraadt@, ok jsg@


# 1.307 29-Mar-2016 kettenis

Implement a cleaner way to prevent acpivout(4) from attaching if we have
a platform-specific method of controlling the backlight.

ok millert@, mlarkin@, jung@


# 1.306 28-Mar-2016 kettenis

Adding each and every new acpi device driver to acpi_foundhid() is
gettinga bit of the burden. And it really isn't how our autoconf
framework is supposed to work for busses that can be enumerated. All
the drivers already check for a matching _HID in their attach
function. So we can just drop the checks in acpi_foundhid() and let
the drivers sort themselves out.

Print a "not configured" line for devices for which we don't attach adriver.
This may be a bit much as there are devices for which we will not have a
driver. This will be removed if it causes too much dmesg spam.

ok deraadt@, mlarkin@


Revision tags: OPENBSD_5_9_BASE
# 1.305 17-Jan-2016 jsg

Take acpi_foundhid() outside of #ifndef SMALL_KERNEL so the ramdisk
kernel can find and attach dwiic(4) and sdhc(4).


# 1.304 17-Jan-2016 jsg

remove an unused global variable


# 1.303 14-Jan-2016 kettenis

Attach dwiic(4) on the Intel Bay Trail i2c controllers as well.


# 1.302 12-Jan-2016 kettenis

Improve emulation of PM registers on Hardware-redcued ACPI by emulating
SPL_TYPx, SLP_EN and WAK_STS using the new SLEEP_STATUS_REG and
SLEEP_CONTROL_REG registers. Makes the transition into S5 (powerdown) work
on the ASUS X205TA.

ok deraadt@, mlarkin@


# 1.301 12-Jan-2016 jcs

Add dwiic, a driver for the Synopsys DesignWare i2c controller found
on the Samsung ATIV Book 9 laptop. This initial version only
supports ACPI config/attachment.

Add ihidev, a HID-over-i2c driver largely based on uhidev. dwiic
handles attaching ihidev devices found in ACPI.

Add ims, a HID-over-i2c mouse/trackpad driver to get basic cursor
and button functionality from HID-compliant i2c trackpads.

ok kettenis deraadt


# 1.300 11-Jan-2016 kettenis

Make sdhc(4) attach to hardware IDs "80860F14" and "PNP0FFF".


# 1.299 09-Jan-2016 kettenis

Initial support for Hardware-Reduced ACPI. This skips the legacy mode checks
by emulating the PM_CNT register such that SCI_EN is always enabled. It also
skip attaching acpitimer(4) since the relevant registers are no longer there.

ok deraadt@, mlarkin@


# 1.298 06-Jan-2016 kettenis

Follow the spec and check the SCI_EN bit to see if ACPI has already
been enabled and don't bother enabling it again if it is. This should fix
systems that don't implement legacy mode and therefore set ACPI_ENABLE and
ACPI_DISABLE to zero.

ok mlarkin@, yasuoka@


# 1.297 23-Nov-2015 reyk

Back out the CRITICAL change, turn it back from 10% to 25%. Some
people with old/broken batteries didn't like this change, and the
underlying heuristics are wrong.


# 1.296 22-Nov-2015 reyk

The "CRITICAL" APM battery level, that is exported/faked by the ACPI
stack, is currently set to 25%. With 25% battery left, a typical
modern laptop will have at least one hour runtime left. It is an
arbitrary value, and other APM power layers in the kernel set CRITICAL
to something like 5% or 10%, so set it to 10% for now.

OK mlarkin@


# 1.295 28-Sep-2015 deraadt

In low-level suspend routines, set cold=2. In tsleep(), use this to
spit out a ddb trace to console. This should allow us to find suspend
or resume routines which break the rules. It depends on the console
output function being non-sleeping.... but that's another codepath which
should try to be safe when cold is set.
ok kettenis


# 1.294 06-Sep-2015 deraadt

clarify malloc arguments (base + length...)


# 1.293 28-Aug-2015 deraadt

fairly simple sizes for free(); ok tedu


# 1.292 20-Aug-2015 kettenis

Respect the access size when reading or writing to pci config space.
This version also makes sure writes are properly aligned, preventing
the panics seen with the previous incarnation of this change.

Fixes battery status passthrough in vmware, brightness keys on my x220
and who knows what else.

ok mlarkin@, deraadt@


Revision tags: OPENBSD_5_8_BASE
# 1.291 04-Aug-2015 deraadt

If we are setting ACPI_PM1_BM_RLD do it only once, and remember to
repeat this action at resume time.
ok mlarkin guenther


# 1.290 29-Jul-2015 deraadt

Something subtle in previous revision is locking up suspend or booting
on some laptops. Mailing lists threads mention x220 and Asus machines.
Reverting for now.


# 1.289 21-Jul-2015 kettenis

iRespect the access size when reading or writing to pci config space.
Fixes battery status passthrough in vmware and who knows what else.

Tested by many.

ok mlarkin@, deraadt@


# 1.288 17-Jul-2015 mlarkin

fix comments


# 1.287 26-Mar-2015 jsg

Use the 0x10 extended capability to determine if the trackstick
buttons are wired to the trackpad and need to be re-routed to the
trackstick. Without this change the buttons on 2015 thinkpads
get picked up as extended buttons that show up as scroll up/down.

Remove the X1 Carbon 2015 (LEN0048) and X250 (LEN0046) from the
top button area/soft buttons quirks list. Also avoid using the
quirk list entirely if the capability bit is set.

Based on work from jcs@ with feedback from mpi@
ok mpi@ jcs@


# 1.286 20-Mar-2015 kettenis

The ACPI standard says that a PCI function number of FFFF can be used to refer
to all functions on a device. This makes no sense in the context of accessing
PCI config space. Nevertheless stsp@ has a machine with AML that tries to
do config space reads from a device where _ADR encodes function number FFFF.
This is almost certainly buggy AML, but since Linux (and presumably Windows)
boot just fine on this machine we need to work around this. Linux effectively
treats function FFFF as function 7, which isn't present on this machine.
Instead we simulate a read from a non-existent device by returning a bit
pattern of all ones. Writes to function FFFF will still panic.

ok mlarkin@


# 1.285 16-Mar-2015 deraadt

Reduce use of SMALL_KERNEL in acpi, bringing acpiec driver and more gpe
handling into RAMDISK. This is now possible because the install media
has ample room. The goal is to reduce special cases where we may be
skipping (unknown) important operations...
ok mlarkin kettenis


# 1.284 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.283 07-Feb-2015 deraadt

New framework that allows hibernate to pass in entropy from it's fresh
boot.
ok mlarkin


# 1.282 06-Feb-2015 mlarkin

Fix a hibernate crash on some machines due to unmapping a page that
may not have been mapped previously (in the failure to hibernate case).

Also ensure that the lowmem ptp is mapped in all cases (not just MP).

ok kettenis


# 1.281 17-Jan-2015 deraadt

resume_randomness() before spinning up other cpus...


# 1.280 15-Jan-2015 jsg

Define a new wscons mouse type for Synaptics clickpad devices that lack
physical buttons. This will be used if the acpi pnpid for the mouse
matches a list provided by Synaptics found in Linux.

Combined with the xenocara changes this will remove the need for an xorg
config file for the x240/t440/t540 generation of thinkpads.

ok matthieu@ shadchin@ kettenis@


# 1.279 11-Jan-2015 kettenis

Tolerate checksum errors in ACPI tables (but complain about them). Apparently
there are machines out there with broken BIOSen with such checksum errors in
thier RSDT tables. Windows runs fine on these machines, so it probably does
not check. Linux ignores checksum errors as well (and prints a warning).

ok deraadt@, krw@


# 1.278 18-Dec-2014 deraadt

suspend and resume the RNG (at what looks like the right spots)
ok reyk djm


# 1.277 09-Dec-2014 doug

Replace some malloc(n*size,...) calls with mallocarray().

ok tedu@ deraadt@


# 1.276 06-Dec-2014 mpi

If "machdep.lidsuspend" is not set, do not go back to sleep if the lid
is still closed when resuming. This allows docked laptop to be resumed
without opening them.

ok mlarkin@


# 1.275 23-Nov-2014 mlarkin

If a machine has no _LIDs, then none can be open. This diff fixes a problem
where machines without any _LIDs were going back to sleep after hibernate
due to the recent suspend-if-lid-closed-on-resume diff. Noticed on qemu,
which doesn't have any _LIDs.

discussed with deraadt


# 1.274 18-Nov-2014 krw

Nuke yet more obvious #include duplications.

ok miod@


# 1.273 08-Nov-2014 mlarkin

If resuming from sleep (zzz/ZZZ) and the lid is still closed, go back to
sleep. This prevents accidental lid flex or slight opening in a backpack
from waking the machine up and leaving it resumed (powered on).

ok deraadt@


# 1.272 26-Sep-2014 kettenis

Rework piglet and pig allocation. Currently the piglet gets allocated
deep down in the suspend path, where it is really hard to recover from
allocation failure. So allocate the piglet early on in the suspend path.
Also change the piglet and piglet allocation functions to use km_alloc(9)
instead of doing pmemrange magic. This removes a bunch of code which, in the
case of the piglet allocation, is broken since it results in a NULL pointer
dereference. Also switch the piglet allocation to not wait. If we can't
allocate 16MB of phys contig memory on a halfway modern machine we're almost
certainly under a lot of memory pressure and we're better off not trying to
hibernate anyway.

ok mlarkin@


# 1.271 25-Sep-2014 kettenis

Bring back rev 1.249. Now that mp_setperf() has been fixed, it should be
safe to restore perflevel when resuming. Should fix the issues people
see with the processor running at full speed heating up the machine while
the apm command reports that the CPUs are running at a low speed.

ok dcoppa@, deraadt@


# 1.270 23-Sep-2014 kettenis

Release the acpi lock around calls to wsdisplay_suspend() and
wsdisplay_resume(). The X server may adjust the display brightness just
before/after the VT switch when suspending/resuming. In fact, it may
already be blocked adjusting the display brightness for some reason
unrelated to the suspend request.

The VT switch request will eventually time out, but the delay is annoying
and the failed VT switch will lead to problems after resume.

Needed for a future update of the xf86-video-intel driver.

ok mlarkin@


# 1.269 19-Sep-2014 kettenis

Use config_suspend_all(9).


# 1.268 08-Sep-2014 kettenis

Clear the PME Status bit on PCI devices in response to Device Wake events.
Fixes the interrupt storm on the 2009 Mac mini with WOL enabled on its
nfe(4) interface.

ok mlarkin@


Revision tags: OPENBSD_5_6_BASE
# 1.267 20-Jul-2014 mlarkin

Support hibernating to softraid crypto volumes.

much help and ok from deraadt@


# 1.266 16-Jul-2014 mlarkin

Reenable hibernate RLE support and flush+zero all memory after unpack.


# 1.265 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.264 12-Jul-2014 kettenis

Add a function to drop all clean pages on the page daemon queues and call
it when we hibernate.

ok mlarkin@, miod@, deraadt@


# 1.263 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.262 11-Jul-2014 mlarkin

Flush the buffercache to 16MB on hibernate and restore its previous max
size (kern.bufcachepercent) on resume, for better hibernate performance.

ok beck@


# 1.261 10-Jul-2014 blambert

remove unneeded header

ok mlarkin@


# 1.260 01-Jul-2014 kettenis

Adding an additonal delay after each access to SystemMemory or SystemIO
during autoconf makes no sense.

ok deraadt@


# 1.259 23-Jun-2014 kettenis

SystemMemory is used to access memory mapped registers on some machines, so we
must use the bus_space(9) API to access it instead of memcpy(9). Also make
sure we properly align access to fields that need more than byte alignment.

ok guenther@


# 1.258 21-May-2014 mlarkin

Fix invalid / missing format specifiers in several dnprintf statements
(used only when ACPI_DEBUG is enabled)

From a diff on tech@ by Fabian Raetz, with a minor change by me, tested
on i386 and amd64 with ACPI_DEBUG enabled and disabled.


# 1.257 25-Apr-2014 mlarkin

Defer the free of the hibernate structures until later, when it is safe to
do so.

Freeing the hibernate structures too early leads to inadvertent calls into
splx when we aren't ready for them yet. Found when trapping splx calls in
the resume path.

discussed with deraadt@ at m2k14


# 1.256 13-Apr-2014 mlarkin

Fix a badly worded debug message that implied the entire machine was going
to sleep, instead of just the acpi thread going to sleep.


# 1.255 31-Mar-2014 mpi

Change the order of operations during a suspend/resume cycle and
call bufq_quiesce() after executing the DVACT_QUIESCE handlers.

This should be safe since no disk nor controller drivers have such
handler but it will allow us to detach sd(4) devices attached to a
USB bus.

Another benefit pointed out by kettenis@ is that drivers that need
to read a firmware from the disk should be able to do it at resume
time in a DVACT_WAKEUP handler.

ok kettenis@, deraadt@


# 1.254 13-Mar-2014 dlg

get rid of the assumption that the head of the alldevs list is the
"mainbus" device. this breaks when mpath is enabled because it
attaches before mainbus and therefore takes the head position.

have autoconf provide device_mainbus() which looks up mainbus_cd,
and use that instead.

discussed with deraadt who just wants mpath stuff to move forward
despite there being many ways to shine this particular turd.


Revision tags: OPENBSD_5_5_BASE
# 1.253 21-Feb-2014 deraadt

Do the lid control further on in the resume sequence, since this is
AML, and will do WAITOK operations. While cold, that is a bad idea.
We seem to be safe with respect to the spec, I do not see any event
debounce issues here.
ok kettenis, tested by many


# 1.252 20-Jan-2014 claudio

Instead of checking sc->sc_revision <= 1 check that the
fadt->hdr_revision <= 1. This is a better check since it checks
the version of the proper descriptor. This allows the alix APU
to reboot since those boards have currently a wrong sc->sc_revision.
Committing this now so that it can be tested easier.
With and OK jsg@ also fine by derradt@


# 1.251 24-Dec-2013 kettenis

If the FADT has its SMI_CMD set to zero, assume we're only ACPI-only hardware
and don't need to disable SMI ownership of the ACPI hardware registers.

ok mlarkin@


# 1.250 23-Dec-2013 kettenis

Revert previous commit. It makes resume hang on some MP systems running
GENERIC.MP.


# 1.249 16-Dec-2013 kettenis

Restore hw.setperf upon resume like we do for apm(4) on i386. Seems we need
to do this fairly late in the resume to avoid hangs.

ok deraadt@, mlarkin@


# 1.248 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


# 1.247 06-Nov-2013 mpi

Add support for Power Resources for Dx states and the necessary hook
for PCI devices. This hook should be called twice, before and after
changing the power state of a PCI device.

Before setting the device to the new state, the ACPI layer will notify
every power resources linked to the device for that state and make sure
they are turned "_ON". After changing the state of the device, it will
decrement the reference of every power resources linked to that device
for the old state and turn them "_OFF" if they are no longer referenced.

This fixes the no-USB after resume problem seen on various ThinkPad,
problem initialy diagnosed with Alexander Polakov.

ok kettenis@, deraadt@


Revision tags: OPENBSD_5_4_BASE
# 1.246 01-Jun-2013 mlarkin

Stop printing ACPI wakeup devices in dmesg after the 16th wakeup device.

Helps with VMware VMs that report hundreds of wakeup devices.

ok reyk@, deraadt@


# 1.245 31-May-2013 bluhm

Fix the build for a kernel without wd and pciide in its config.
OK deraadt@ tedu@


# 1.244 30-May-2013 deraadt

Enforce ca_activate tree-walks over the entire heirarchy for all events,
cleaning up some shutdown-hook related code on the way.
(A few drivers related to sparc are still skipped at kettenis' request)
ok kettenis mlarkin, tested by many others too


# 1.243 18-Apr-2013 deraadt

pin the acpi thread to the BSP, because AML and SMI cannot be trusted. I
was certain we were doing this already, but it turns out no...


Revision tags: OPENBSD_5_3_BASE
# 1.242 24-Dec-2012 guenther

In acpiioctl(), Return though the splx() path if suser() returns an error

ok deraadt@


# 1.241 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


# 1.240 04-Oct-2012 ehrhardt

Use information provided by ACPI to attach secondary PCI host bridges.

ok kettenis@


# 1.239 07-Sep-2012 kettenis

Add a new function called acpi_pci_min_powerstate() that returns the lowest
power state for a PCI device for the power state (currently S3 or S4) we're
transitioning into. This function is probably too simplistic; there is a lot
of (silly) complexity in the ACPI spec.

ok deraadt@


Revision tags: OPENBSD_5_2_BASE
# 1.238 13-Jul-2012 pirofti

Disable acpivideo(4) on ASUS laptops.

There are some models that panic whenever an event is received.
Some cope, some don't. In order to get this right acpiec(4) requires
major rework. Until then let machines boot.

Discussed with deraadt@.


# 1.237 11-Jul-2012 mlarkin

for now, restrict hibernate to root in the acpi ioctl kernel interface.
normal rules for controlling this interface through apmd still apply.

ok deraadt@


# 1.236 09-Jul-2012 deraadt

tiny little oops in untested hibernate block


# 1.235 09-Jul-2012 deraadt

clean up ioctl() case statements


# 1.234 09-Jul-2012 deraadt

acpi_disable_onegpe() is not used; ok pirofti


# 1.233 24-May-2012 kettenis

If _MIN > _MAX consider a WordBus resource to be invalid. Fixes a panic on
the IBM x3550-M4.

ok mikeb@


# 1.232 29-Mar-2012 mlarkin

Missing #ifdef in previous diff


# 1.231 29-Mar-2012 mlarkin

Prevent hibernation attempts on machines with unsupported disk controllers.
The same check is also performed later in the hibernate sequence, but by
then it's too late to cleanly unwind (presently).

ok deraadt


# 1.230 26-Mar-2012 deraadt

simplify conditionals in acpi_sleep_state()
ok mlarkin


# 1.229 26-Mar-2012 deraadt

hook in the hibernate request code; half of this diff is from mlarkin
ok mlarkin


Revision tags: OPENBSD_5_1_BASE
# 1.228 20-Sep-2011 deraadt

Very early in suspend while the clock is still ticking realtime,
save the clock back to the rtc. The zaurus already did this.
ok phessler miod kettenis


Revision tags: OPENBSD_5_0_BASE
# 1.227 02-Jul-2011 nicm

kqueue attach functions should return an errno or 0, not a plain 1. Fix
the obvious cases to return EINVAL and ENXIO.

ok tedu deraadt


# 1.226 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.225 16-Jun-2011 pirofti

Import acpitoshiba driver.

This is originally written by Hiroyuki Aizu, ported and integrated
in our tree by Javier Vazquez <javama4 AT gmail> with tweaks and
suggestions by me.

Import prodded by deraadt@ and the lack of testers on tech@.


# 1.224 27-Apr-2011 jcs

attach acpithinkpad to newer lenovo models like the x120e

ok deraadt@


# 1.223 22-Apr-2011 jordan

Fix uninitialzied variables and formatting strings (-Wxxx errors)


Revision tags: OPENBSD_4_9_BASE
# 1.222 02-Jan-2011 jordan

Re-add code for ACPI taskqueue, eliminates need for state vars in acpi softc
Serialize processing ACPI tasks for sleep/powerdown/GPE event
ok deraadt@


# 1.221 31-Oct-2010 guenther

Revert last commit: it breaks resume on Thinkpads
"then please back it out" deraadt@


# 1.220 26-Oct-2010 jordan

Add task queue for ACPI gpe and notify handlers
This fixes eject on Dell Latitude dock and an issue on the Dell Mini
battery update.
ok deraadt


# 1.219 07-Oct-2010 jordan

Create separate functions for enabling wake and runtime gpes.
Remove old suspend/resume gpewalk function
ok deraadt, mlarkin


# 1.218 05-Oct-2010 jordan

split acpi_enable_onegpe to separate enable/disable functions
ok deraadt


# 1.217 05-Oct-2010 jordan

Added acpi_disable_allgpes function to clear GPE status
ok deraadt


# 1.216 05-Oct-2010 deraadt

clean up acpi_softc use more


# 1.215 05-Oct-2010 deraadt

delete a bit of #if 0 code


# 1.214 05-Oct-2010 deraadt

hide use of our global acpi_softc pointer a bit better


# 1.213 29-Sep-2010 deraadt

grab the acpi thread lock very early in acpi thread startup; not that it
is likely that anyone will race us... but why be careless.
ok kettenis


# 1.212 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.211 29-Aug-2010 jasper

- add a check for FWRITE here too.

ok miod@


# 1.210 27-Aug-2010 kettenis

Make PCI Power Management optional, and only enable it when acpi(4) attaches.

ok deraadt@


Revision tags: OPENBSD_4_8_BASE
# 1.209 08-Aug-2010 kettenis

Close race between the acpi thread and wscons ioctls running in process
context. We might rip this out post-release and replace it with a solution
that serializes things in a more obvious way.

ok deraadt@, miod@ and "those crazy texans who ok anything"


# 1.208 08-Aug-2010 deraadt

Report the acpi sleep states when on a RAMDISK; ok krw


# 1.207 07-Aug-2010 kettenis

Enforce acpi register access rescrictions.

ok mlarkin@, deraadt@, tested by marco@


# 1.206 07-Aug-2010 deraadt

upon resume, notify all the acpiac and acpibat drivers to update
their status. some laptops do not do this automatically.
ok kettenis canacar


# 1.205 06-Aug-2010 marco

Always call _PSW on _LID devices that support it to enable lid open to
resume the machine. Conversely disable it when the machine wakes up.

Tested by several ok deraadt


# 1.204 06-Aug-2010 deraadt

As part of a revamp of the PM1/GPE code, I write this diff to look for
unmanaged EN & STS bits in the PM1 register at interrupt time and report
them. As a side effect this splits the STS acknowledgement into two writes
(for power, and sleep) instead of one. The printf that is added (to spot
unmanaged STS bits) has not yet been triggered as far as we know.

Before the "write to PM1 registers at the right offset" diff went in, this
was not neccessary. But newer thinkpads do not have a working soft power
button without this diff.

We have no idea why.
ok mlarkin kettenis


# 1.203 05-Aug-2010 deraadt

PM1 power button events were generating 'fake events' to acpibtn PWRB,
but if there is no such device these were being tossed. Instead, arrange
for all power button handlers to flag a softc variable which is processed
by the thread; so that one place will know to request a powerdown (psignal
to init)
ok marco mlarkin


# 1.202 05-Aug-2010 deraadt

rename a variable which is annoyingly poorly named


# 1.201 05-Aug-2010 mlarkin

Write to the correct offset for various ACPI pmreg registers. Writing to
the correct register does properly clear the wake status bits and fixed
event bits, which allows some machines to have more than 1 suspend cycle.

bug spotted by deraadt@

tested by deraadt@ and myself on a half-dozen machines and others on
hackers@

ok deraadt@, marco@


# 1.200 05-Aug-2010 deraadt

Fix the power button. The pm1 register is 16 bits in size.
My mistake, a while back.
tested by krw


# 1.199 04-Aug-2010 kettenis

Print supported sleep states.

ok marco@, phessler@, deraadt@


# 1.198 03-Aug-2010 mlarkin

Use the proper flag for re-enabling certain hardware events (power btn,
sleep btn). The (incorrect) flag prevoiously used coincidentally had the
same value, so this shouldn't cause any different behavior than before.

tested on a variety of machines (i386, amd64, sp, mp) - no behavioral
change seen

ok deraadt@


# 1.197 28-Jul-2010 marco

Make all gasio failures look and behave the same.


# 1.196 28-Jul-2010 mlarkin

Delay a little longer on suspend before giving up. Fixes machines that
sometimes don't suspend because the sleep transition takes too long
(symptoms like failing to sleep but the backlight stays on).

ok deraadt@


# 1.195 27-Jul-2010 deraadt

remove XXX; EC can now be attached early when needed; ok jordan


# 1.194 27-Jul-2010 deraadt

more brackets feels good


# 1.193 27-Jul-2010 deraadt

XXX annotate a block in acpi_enter_sleep_state() that is pretty worrying


# 1.192 27-Jul-2010 deraadt

horrific indentation that nearly made me puke


# 1.191 27-Jul-2010 mlarkin

Set the sleeping indicator light on machines that support it, in case SMI
doesn't do it for us. Shows the 'moon' LED on older Thinkpads when
sleeping.

Tested on T43p, W500, T510 by me and on X61s by deraadt@.

ok deraadt@


# 1.190 27-Jul-2010 jordan

Early initialization of acpiec if ECDT table exists
Fixes hang when booting thinkpads while docked
ok deraadt


# 1.189 26-Jul-2010 deraadt

remove merge error which was commited


# 1.188 26-Jul-2010 pirofti

Add support for Sony ACPI hotkeys via a new driver: acpisony(4).

Currently it only works for the suspend button and tries to do right for
the brightness events, but I haven't found a Sony laptop that like to do
right so far.

In the future I want to make the brightness keys work on all Sony's and
also add support for the zoom hotkeys and whatever other funky keys I can
find on those things.

Okay deraadt@.


# 1.187 22-Jul-2010 deraadt

pretty large cleanup of SMALL_KERNEL


# 1.186 21-Jul-2010 deraadt

in the acpi GPE handler, late ack level interrupts not edge interrupts.
Duh.


# 1.185 21-Jul-2010 deraadt

Merge the edge and level gpe handlers into one (now possible since the
gpe structure tells us whether the gpe is edge or level)


# 1.184 21-Jul-2010 deraadt

delete #if 0 code we are not going to use; ok marco


# 1.183 20-Jul-2010 deraadt

make the mask code for acpi_enable_onegpe() match other similar code


# 1.182 20-Jul-2010 deraadt

Handle the acpi interrupt controller with proper edge/level handling,
wrapped in spltty to avoid racing against the interrupt controller.
Repair the gpe bit masking code while there, and do operations in the
right order
ok kettenis mlarkin, help from jordan at figuring out the order of
operations.


# 1.181 20-Jul-2010 matthew

When we're preparing to go to sleep, only call bufq_restart() if other
preparations failed.

ok kettenis@, thib@, deraadt@


# 1.180 19-Jul-2010 deraadt

permit compilation with ACPI_DEBUG


# 1.179 19-Jul-2010 deraadt

there is no need for a seperate dsdt_softc pointer; dsdt does not have a
softc but is just a part of the single acpi driver.
ok jordan


# 1.178 19-Jul-2010 deraadt

acpi_delay() is no longer needed; ok jordan


# 1.177 18-Jul-2010 mlarkin

Clear event status on resume, to avoid going back to sleep/reboot/shutdown
immediately after resume. Tested by a bunch of people on hackers@ on
various hardware. Fixes at least Toshiba NB200 and Gateway LT3103u.

ok marco


# 1.176 13-Jul-2010 deraadt

rename some thread related interfaces so they are logical


# 1.175 10-Jul-2010 jordan

Verify _ADR bdf is correct (some systems return 0xffff)
Works on Dell x9xx and Studio 1555


# 1.174 09-Jul-2010 jordan

Clear the noise


# 1.173 08-Jul-2010 jordan

Add mapping for ACPI device to PCI bdf (match autoconf tree)
Simplify resource parsing function to use buffer argument
Convert namespace linked lists to use queue macros
ok marco@, deraadt@


# 1.172 06-Jul-2010 deraadt

Use spltty() to lock downcalls from apm against the information being
modified by the acpi thread.
ok kettenis


# 1.171 06-Jul-2010 deraadt

Don't use a workq to do the suspend, because it races aginst the acpi
thread. Instead, just tell the acpi thread to do the suspend for us.
This makes apmd & zzz work correctly. While here, have acpithinkpad
attempt to post the event to apm before prompting the suspend itself.
ok kettenis marco mlarkin


# 1.170 05-Jul-2010 mlarkin

Reset register SCI_EN on resume. The ACPI spec clearly states we don't have
to do this, but many machines don't, or forget to, which leads to all sorts
of problems on resume (broken battery stats, overheating, failure to resume
more than once, etc).

tested by many, ok marco@, deraadt@


# 1.169 01-Jul-2010 thib

do bufq quiesce again, since vnd/the bufq_done call in
biodone has been fixed.


# 1.168 01-Jul-2010 jordan

Backout recent AML changes, breaks on R210 and others


# 1.167 01-Jul-2010 jordan

Simplified aml_parse_resource call, cleanup code
ok marco


# 1.166 01-Jul-2010 jordan

Fix subordinate bus # for multi-root PCI buses
ok marco, kettenis


# 1.165 30-Jun-2010 thib

Disable/partially backout the bufq quiesce changes as this
is causing havoc with vnds and release must be buildable.


# 1.164 29-Jun-2010 deraadt

Fix ramdisks (new code was placed inside a SMALL_KERNEL chunk)


# 1.163 29-Jun-2010 jordan

Add support for mapping ACPI to PCI devices
ok kettenis, deraadt


# 1.162 29-Jun-2010 kettenis

Call bufq_quiesce() on suspend to stop all I/O and bufq_restart on resume.
Makes suspend/resume work much better while doing disk I/O.

ok mlarkin@


# 1.161 29-Jun-2010 tedu

60 is a more reasonable guess for number of minutes in an hour than 100.
spotted by TeXitoi. ok deraadt


# 1.160 27-Jun-2010 jordan

Add _acpi_matchhids routine, precursor to pci device match


# 1.159 27-Jun-2010 mlarkin

Clear ACPI fixed event status on resume (power buttons/etc) so that some
machines don't immediately resume after suspending to S3.

Tested by kettenis@ and I on 5 or 6 machines, deraadt@ "yeah, looks good"


# 1.158 27-Jun-2010 jordan

Cleaned up acpi_maptable, adds to table list directly


# 1.157 07-Apr-2010 deraadt

If we have done a wsdisplay_suspend(), and ACPI decides it does not want to
suspend, we must roll back by calling wsdisplay_resume() or the virtual
console switching will be locked forever
ok oga


# 1.156 07-Apr-2010 kettenis

Make suspend/resume work on MP machines (running an MP kernel). Joint work
from mlarkin@ and me, with some amd64 fixes thrown in by deraadt@

ok marco@, deraadt@, pirofti@, mlarkin@


# 1.155 31-Mar-2010 kettenis

Bring /dev/apm support in line with apm(4). Make the suspend button send
a suspend request event to apmd(8) instead of suspending immediately. Also
keep track of whether /dev/apm and /dev/apmctl are currently open, such that
we can still suspend immediately if apmd(8) isn't running.

ok deraadt@, marco@, pirofti@, jsing@, oga@


# 1.154 30-Mar-2010 oga

Prevent the apmd/x races for good.

When we hit suspend time, go through all wsdisplays on the system. if
they are in mode MAPPED, but not MODE_DUMBFB then if possible do a full
vt switch to a !mapped vt, and prevent switching back until resume time.
This has to be called from MD code because this involves userland
running so that X can run the vt switch signal handler. This way, any
case where we are using the "poke registers from userland" model, we
will not be on the hardware when we go down, so the kernel can actually
handle thing properly.

Tested on several acpi laptops (by kettenis@ and ian@), x40 (me and
beck@ at LEAST) and zaurus (me). Maybe others, but if so I forgot who at
this time..

Idea from deraadt somewhere over the Faroe Islands (I thought of a
similar thing myself a while ago). Much prompting from him. Ok and
comments miod@


# 1.153 25-Mar-2010 oga

Fix & vs. && in the kqfilter code.

Based on a comparison with the apm code.

ok deraadt@, kettenis@


Revision tags: OPENBSD_4_7_BASE
# 1.152 26-Nov-2009 mlarkin

Visual (and on some machines, audio) feedback while the machine is
resuming via calls to aml _SST method. This also makes the "moon" LED on
thinkpads to blink during resume and cease when resume is completed.

ok pirofti@


# 1.151 26-Nov-2009 deraadt

Toggle into "cold" when we we disable interrupts, because tsleeping
in the AML interpreter and context switching into userland processes
tends to sometimes pop the ACPI trampoline page out of our address
space. Makes lots more machines work.
ok kettenis mlarkin


# 1.150 24-Nov-2009 jsg

in the resume path evaluate _WAK before dealing with device drivers
like the spec says, not after.

ok mlarkin@


# 1.149 24-Nov-2009 deraadt

abort suspends on SMP systems until that gets written; ok mlarkin


# 1.148 23-Nov-2009 mlarkin

pci should return the result of it's children's suspend/resume calls back
to its parent.

handle suspend failure case and unwind if devices have an activate function
that returns failure.

ok deraadt@, kettenis@


# 1.147 23-Nov-2009 mlarkin

acpi* devices need to call their children on suspend/resume.
ok deraadt@


# 1.146 23-Nov-2009 pirofti

Remove ACPI_SLEEP_ENABLED checks.

This enables by default the suspend/resume paths in the kernel.

Okay deraadt@.


# 1.145 23-Nov-2009 deraadt

knf and spacing that are super annoying


# 1.144 23-Nov-2009 mlarkin

Hook up Theo's new children suspend/resume function, as well as more ACPI
suspend/resume glue.

ok deraadt@


# 1.143 26-Oct-2009 deraadt

Do not do strncmp comparisons on dv_xname because longer device names which
look similar could arrive in the future. Instead, compare directly against
dv_cfdata->cf_driver->cd_name
Issue originally spotted by miod


# 1.142 26-Oct-2009 jordan

Verify checksum+address when loading ACPI tables.
Some systems had invalid entries in RSDT/XSDT.
ok marco@


# 1.141 23-Jul-2009 cnst

New aibs(4) driver for ASUSTeK AI Booster (ACPI ATK0110) hardware monitoring,
with sensor state support through limits provided by the ACPI.

Tested on several ASUS motherboards kindly networked by Sam Fourman Jr.

ok deraadt marco jordan


Revision tags: OPENBSD_4_6_BASE
# 1.140 03-Jun-2009 pirofti

Import acpi power resource driver. This is needed for suspend/resume logic.

Okay jordan@ and marco@.


# 1.139 03-Jun-2009 pirofti

Add acpivideo support. This adds brightness support for all laptops
except thinkpads, they will use the acpithinkpad driver. The driver is
also hooked into wsconsole. So brightness can be adjusted via:

$ wsconsctl display.brightness=<percentage>

This is very helpfull on some laptops that have a nasty bios and get two
steps instead of one when pressing the brightness button.

Tested on various dell, fujitsu, acer, samsung and other laptops.

Okay marco@, miod@. Suggestions from kettenis@.
Lots of reviews and help from miod@, thanks!


# 1.138 03-Jun-2009 jordan

Added ejectable bay for IDE cdroms
ok marco@


# 1.137 30-Apr-2009 marco

Oga changed apm ioctls and forgot to grep. Temporary work around by hitting
the sleep button when we get any request. This interface will chnage at
some point.
ok oga


# 1.136 19-Apr-2009 krw

Call acpi_sleep_walk() when powering down just like when entering
any other sleep state. This tells acpi wake devices to not wake up
the box until manual powering up resets their status. Makes my Dell
GX520 stay powered off in response to 'halt -p'.

In snaps for a while.

ok marco@ (several times) deraadt@


# 1.135 17-Apr-2009 pirofti

Add device ranking support for future sleep/wake developments.

okay marco@


# 1.134 11-Mar-2009 jordan

Cleanup.. use aml_evalinteger instead of aml_evalname
ok marco@


# 1.133 10-Mar-2009 jordan

Fix cut/paste error.. call _BFS not _PTS
ok marco@


Revision tags: OPENBSD_4_5_BASE
# 1.132 21-Feb-2009 marco

overzealous cut/paste; found by Vladimir Kirillov <proger@uaoug.org.ua>


# 1.131 20-Feb-2009 marco

Oops didn't mean for Debugger() to remain there.


# 1.130 19-Feb-2009 marco

suspend/resume bits so that we can develop this in tree. This is disabled.
code from mlarkin and me
help from art,toby,jordan and several others
ok jordan, go for it deraadt


# 1.129 10-Feb-2009 jordan

Fix null pointer for shutdown if acpi not enabled
ok marco@


# 1.128 04-Feb-2009 kettenis

Some systems that claim to be ACPI 2.0 apparently don't provide an XSDT
address. Make the code fall back on the RSDT on those systems.

From Joerg Niendorf <p3y80@internode.on.net>. Fixes PR 5986.


# 1.127 17-Jan-2009 krw

A couple of stray malloc/memset -> malloc(M_ZERO), a bit of knf and
lose a couple of unnecessary casts. No functional change.

Feedback from kettenis@

ok kettenis@ marco@


# 1.126 06-Nov-2008 marco

Collapse all sanity checks of hids into one function.

Originaly from jordan


Revision tags: OPENBSD_4_4_BASE
# 1.125 02-Jul-2008 fgsch

add acpivideo. for now almost an empty skel so others can work on
suspend/resume. eventually it will also manage output switching and
brightness where supported. prodded by marco@


# 1.124 11-Jun-2008 marco

Fix $OpenBSD$ for once and for all


# 1.123 08-Jun-2008 deraadt

aaa_dev was being set from an uninitialized variable; ok toby


# 1.122 07-Jun-2008 marco

Kill ACPIVERBOSE. It was never used and is worthless to boot.

comments jmc
ok toby


# 1.121 06-Jun-2008 marco

Shrink acpi some more for floppies and while here re-enable acpi on the
floppies. Fixes crazy sony vaio kettenis has.

tested by deraadt
ok deraadt jordan kettenis


# 1.120 01-Jun-2008 marco

Fix \n nit in SMALL_KERNEL.


# 1.119 01-Jun-2008 marco

Re-enable new parser. Lots of testing by various folks. Fits all media
now too.

ok kettenis deraadt


# 1.118 16-May-2008 dlg

back out jordans new parser.

it didnt have enough oks from the right people, it did break machines
that were previously working, and it bloated the RAMDISKS and install
media beyond the constraints of the media.

requested by deraadt@


# 1.117 14-May-2008 jordan

Adding new ACPI Parser code
Old guts of code still exists, needs to be torn out next
ok marco@
ok brad@


# 1.116 27-Apr-2008 jcs

add acpithinkpad(4), an acpi support driver for ibm/lenovo
thinkpads.

provides functionality that the bios did on older, non-acpi
thinkpads like respond to screen brightness keys and toggle the
bluetooth device on and off.

ok deraadt@ and marco@


# 1.115 24-Apr-2008 jsing

Introduce acpiasus(4), a driver for the ACPI based hotkeys found in many
ASUS laptops (including the ASUS EeePC) - largely based on NetBSD's
asus(4) driver. On the ASUS EeePC this allows us to enable/disable
wireless, change screen brightness and use the volume keys.

ok jsg@, weingart@


# 1.114 12-Apr-2008 kettenis

Get rid of acpi_s5 global variables; simply send SIGUSR2 instead of SUGUSR1
to tell init(8) to power down the machine.

ok krw@


Revision tags: OPENBSD_4_3_BASE
# 1.113 21-Jan-2008 jsg

Check against RESET_REG_SUP in FADT before doing the acpi reset dance
after all, not doing so appears to break more machines than it fixes.

ok marco@


# 1.112 05-Jan-2008 kettenis

Never attach more acpicpu(4) devices than cpu(4) devices. Always attach
the first acpicpu(4) device on non-MULTIPROCESSOR kernels, regardless of its
ACPI processor ID.

ok mikeb@


# 1.111 05-Dec-2007 deraadt

Hang acpi(4) below bios(4) instead of mainbus(4). This lets us move acpi(4)
before pcibios(4), and then.... if acpi(4) attaches, skip pcibios(4) since
it messes the machines up. Fixes claudio's HP dl320 G5, and almost assuredly
others.
ok kettenis


# 1.110 05-Dec-2007 deraadt

repair acpiopen/acpiclose and friends to handle the /dev/apmctl interface,
so that apmd(8) can actually work correctly against acpi(4), and thus,
even do the fancy -C/-A stuff; also tested by jmc


# 1.109 02-Dec-2007 fgsch

fix the trailing space for real now.


# 1.108 02-Dec-2007 fgsch

avoid trailing spaces on output; marco@ ok.


# 1.107 27-Nov-2007 kettenis

Fix off-by-one that made us attach acpicpu0 and acpicpu1 on GENERIC kernels.

ok marco@


# 1.106 25-Nov-2007 jsg

When available and ACPI is enabled use the ACPI reset method
to reboot. This is required for some quirky sis machines
and likely others to reboot.

Tested by many. 'commit early commit often' deraadt@


# 1.105 19-Nov-2007 deraadt

sigh, amd64 still uses that stupid acpi_enabled variable...


# 1.104 19-Nov-2007 deraadt

softc's come initialized to 0, you know...


# 1.103 19-Nov-2007 deraadt

store revision; from jsg


# 1.102 19-Nov-2007 kettenis

Initialize sc_facs earlier.

ok deraadt@, beck@, canacar@


# 1.101 16-Nov-2007 deraadt

fix the bus_space #define nightmare, so that amd64 and i386 are much more
uniform. as a result shared code like acpi needs less #ifdef's
ok marco kettenis


# 1.100 15-Nov-2007 deraadt

remove the #ifdef ACPI_ENABLE. the EC codepath is either safe enough (and
will be improved), or it is unsafe because AML assumes it must be there
ok almost everyone


# 1.99 15-Nov-2007 mikeb

Make acpicpu(4) attach to the existing CPUs only.

Problem reported by form@. Fix idea by kettenis@.
Lots of help from deraadt@. Tested by deraadt@, form@ and me.

ok deraadt marco


# 1.98 14-Nov-2007 deraadt

spacing


# 1.97 12-Nov-2007 deraadt

correct : vs , in attach-time messages


# 1.96 06-Nov-2007 deraadt

all ACPI_ENABLE kernels need acpi_interrupt()


# 1.95 06-Nov-2007 deraadt

move acpi_read_pmreg() and acpi_write_pmreg() out of SMALL_KERNEL so
that acpi ramdisks can compile (obvious solution)


# 1.94 05-Nov-2007 canacar

Establish the acpi interrupt earlier. Fixes problems with some
GENERIC.MP kernels not getting acpi interrupts. This is a
workaround until the real issue with late establishment is fixed.
ok weingart@, beck@, gwk@


# 1.93 03-Nov-2007 jordan

Added support for displaying wakeup devices
ok beck@,weingart@,gwk@


# 1.92 11-Oct-2007 kettenis

When walking the tree yo find _PRT methods, check whether the device is
actually enabled and functioning. Similar to what we do for _INI.

ok weingart@, beck@


# 1.91 08-Oct-2007 krw

More simple memset(,0,) -> M_ZERO changes. In this batch move to
size(*p) as the first malloc() parameter where p is declared locally
and thus easy to check. Add M_ZERO to gpe_table allocation in acpi.c
even though there is no obvious bzero or memset nearby.


# 1.90 13-Sep-2007 mk

Remove unnecessary memset() (see aml_evalnode(9)) and a bit of KNF.

ok toby


# 1.89 13-Sep-2007 weingart

Implement ACPI 6.5.1 spec tree walk for _STA and _INI.
Fixes mk's laptop. No regressions so far (thank you to the
testers).

ok gwk@, mk@, marco@


Revision tags: OPENBSD_4_2_BASE
# 1.88 17-Apr-2007 mk

Now that acpidock no longer hangs machines during boot (at least
according to the reports I've been getting) it should be safe to enable
it again, so remove the #if 0 stuff around it.

ok marco


# 1.87 11-Apr-2007 jordan

Added changes for C-State
ok marco@


# 1.86 26-Mar-2007 marco

Calculate minutes left more accurately.

From giovanni <qgiovanni@gmail.com>


Revision tags: OPENBSD_4_1_BASE
# 1.85 03-Mar-2007 mk

Stop looking for acpidock until we fix that stuff that breaks all pre-60
series ThinkPads with acpidock(4) enabled.


# 1.84 22-Feb-2007 marco

We were getting no interrupts in .MP. There are 2 fixes; one in
acpi_machdep.c and move the interrupt establishment down after ioapic
has been initialized.

ok jordan


# 1.83 21-Feb-2007 kettenis

Make an acpi-enabled GENERIC.MP work on systems that only support PIC mode.

ok marco@


# 1.82 19-Feb-2007 jordan

Added acpi_map_address function for mapping GAS/IO registers
ok marco@


# 1.81 18-Feb-2007 jordan

Added dynamic perfect hash for opcodes
ok marco@


# 1.80 18-Feb-2007 marco

Shave off another K pre compression.
Shuffle SMALL_KERNEL functions around so that we only have a single #ifndef


# 1.79 17-Feb-2007 marco

Add SMALL_KERNEL throught acpi to make it fit on boot media.
ok deraadt


# 1.78 13-Feb-2007 marco

Add smarter _INI code. This helps with some dissapearing devices.

ok kettenis jordan


# 1.77 31-Jan-2007 gwk

Introduce acpi_hasprocfvs thats "ACPI Has Processor Frequency and Voltage
Scalling" a global flag set by acicpu attach to indicate that ACPI is
handling fvs through the _PSS objects. This will fix disappearing
EST/powernow when booting kernels with acpi enabled, as acpicpu isnt
compiled into GENERIC yet. Also in cases where acpicpu is built in and
legacy EST/powernow work but acpi is missing the _PSS object, the legacy
routines will be used.

tested by dim@, and Nick Nauwelaerts <nick AT nauwelaerts.net>
ok marco@, dim@


# 1.76 22-Jan-2007 mk

Actually look for docking stations by looking for _DCK nodes.

ok marco


# 1.75 02-Jan-2007 marco

On some systems EC must be attached as the first device. Move EC from
generic _HID devices to its own routine to force first attach. Bug found
and debugged by joris@

Code mostly written by joris@


# 1.74 26-Dec-2006 marco

Add polling to devices that require it such as acpibat and acpitz.
Use the same tsleep/wakeup mechanism as the interrupt code to keep it non-
concurrent.


# 1.73 21-Dec-2006 deraadt

even more knf love


# 1.72 21-Dec-2006 marco

Remove some leftover acpiec gpe/interrupt code.


# 1.71 20-Dec-2006 deraadt

oops, name already used


# 1.70 20-Dec-2006 deraadt

rename __acpi_enable_gpe(); ok marco


# 1.69 20-Dec-2006 deraadt

acpi_unmap_pmregs() is not used, ok marco


# 1.68 19-Dec-2006 deraadt

simplify some low-level methods substantially (shrinking code); ok marco jordan


# 1.67 06-Dec-2006 mk

malloc(..., M_NOWAIT) with no NULL check is bad. If allocation fails,
complain, clean up, and bail out of attach.

input and ok gwk


# 1.66 28-Nov-2006 jordan

Fixed clearing arg for acpi_init_pic
ok kettenis@


# 1.65 27-Nov-2006 kettenis

Don't spam dmesg.


# 1.64 27-Nov-2006 jordan

Fixes for acpiprt routing
Post-parse fixup for namerefs
ok kettenis@


# 1.63 25-Nov-2006 marco

Don't handle acpiev events inside the gpe handler since this causes deadlocks.

ok jordan@


# 1.62 15-Nov-2006 kettenis

Hook up acpi apic interrupt routing support to generic acpi code.

ok jordan@


# 1.61 02-Nov-2006 marco

Kill acpiec attach function and add its functionality to the generic _HID
device attach

prodded and ok jordan


# 1.60 02-Nov-2006 marco

Fix comments to reflect reality.


# 1.59 19-Oct-2006 marco

Replace ACPI ioctls with APM ioctls. This makes ACPI talk the same protocol
as APM therefore userspace can now retrieve power information in the same
fashion as all other apm arches (zaurus, macppc, i386 etc).

To use force the use of acpi device like so: apmd -f /dev/acpi

ok jordan@


# 1.58 19-Oct-2006 jordan

Fixed GPE debug levels
Fixed ref counter on temporary bufferfields
DSDT parsing fully working on IBM Thinkpad
ok marco@


# 1.57 15-Oct-2006 jordan

Removed old GPE handler code remnants
ok marco@


# 1.56 12-Oct-2006 jordan

Massive ACPI changes
Fixed ACPI leaks
new ACPI API
ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.55 30-Jun-2006 gwk

KNF, No functional change,
"go at it" marco@


# 1.54 30-Jun-2006 jordan

Support for calling Embedded Controller via new GPE interface
ok marco@


# 1.53 30-Jun-2006 jordan

Added new code for GPE handling
Also works with ACPIEC; need to fixup acpiec.c to remove original interrupt handler
ok marco@


# 1.52 30-Jun-2006 jordan

Added offset argument for acpi read/write pmreg routines (new GPE code)
ok marco@


# 1.51 31-May-2006 todd

do not call acpi_powerdown() unless acpi has successfully initialized
ok gwk@ canacar@


# 1.50 29-May-2006 canacar

Add embedded controller support.
ok marco@ deraadt@ testing & ok gwk@


# 1.49 19-May-2006 canacar

Minimal thermal zone driver. Monitors thermal zone temperature,
shuts down the system if the 'critical temperature' is reached.
ok marco@


# 1.48 11-Apr-2006 marco

KNF.


# 1.47 11-Apr-2006 gwk

Add acpi_resume(struct acpi_softc *sc), which calls some AML methods,
detected at attach time, currently unused.

ok marco@


# 1.46 11-Apr-2006 gwk

Discover some ACPI sleep related methods, and call them at the
appropriate time in acpi_enter_sleep_state().

ok marco@


Revision tags: OPENBSD_3_9_BASE
# 1.45 26-Feb-2006 marco

Add acpicpu as a device.

ok jordan@


# 1.44 23-Feb-2006 marco

remove dead function.

ok jordan@


# 1.43 22-Feb-2006 marco

Print out all tables it found during autoconf. We'll need this data in
the future.


# 1.42 22-Feb-2006 marco

Make dmesg less verbose.

ok jordan.


# 1.41 22-Feb-2006 jordan

Added parsing of SSDT tables
ok marco@


# 1.40 22-Feb-2006 jordan

Added new aml_register to notify by device id
Moved powerdown code to button device
ok marco@


# 1.39 21-Feb-2006 marco

Readjust debug levels that crept in too low over the last few days.
KNF


# 1.38 21-Feb-2006 marco

Rig the power button so that the machine shuts down correctly.


# 1.37 20-Feb-2006 marco

Add acpibtn device. Remains disabled for now.


# 1.36 19-Feb-2006 jordan

Added code for aml_notify GPE notification callback
ok marco@


# 1.35 19-Feb-2006 grange

Fix acpi_enter_sleep_state() by replacing current ugly hack
with mostly correct sleeping state transition code.

ok brad@ marco@


# 1.34 19-Feb-2006 marco

jordan:
Enable interrupts.
Added peripheral code to make this work.

marco:
Replace softintr and other types of interrupts with a handler with context.
Fixed interrupt magic for GPEs.
Some KNF

This is work done by jordan and I but has to be a single commit.


# 1.33 17-Feb-2006 marco

Remove earlier introduced delays. We really only need them during autconf.

ok jordan@


# 1.32 17-Feb-2006 marco

Add a generic delay function as per the ACPI spec.

from jordan@


# 1.31 17-Feb-2006 marco

Use tsleep instead of delay when !cold.


# 1.30 17-Feb-2006 marco

Argh, we need delays between reads/writes. The backend reads (i2c most likely)
are so slow that values come back as 0 sometimes. We have to do this
despite the spec saying we shouldnt have to. Guess having a faster
parser than others can hurt sometimes.

Idea from jordan.


# 1.29 17-Feb-2006 jordan

Fixed code for handling _INI
Rearranged debug statements
ok marco@


# 1.28 16-Feb-2006 jordan

Added stub for calling _INI
Fixed aml_dowhile for NULL return
ok marco@


# 1.27 16-Feb-2006 jordan

Fixed parser/evaluator to retrieve battery info+state
Cleanup of parser
Dell laptops return battery status
ok marco@


# 1.26 06-Feb-2006 brad

fix a typo so this will compile on amd64.


# 1.25 04-Feb-2006 marco

When ACPI_ENABLED wasn't set but the devices were it had an unknown variable.

Pointed out by ian@.


# 1.24 03-Feb-2006 jordan

Rewrote aml parser yet again...
Fixed bus_space_map for i386/amd64
Added _BST and _BIF to acpibat to get status/info
ok marco@


# 1.23 20-Jan-2006 jordan

More evaluator changes
Removed old evaluator code
Fixed _HID value
ok marco@


# 1.22 19-Jan-2006 jordan

Added support for read/write of PCI Config space
Help jason@
ok marco@


# 1.21 18-Jan-2006 jordan

Added new evaluation routines
Added helper methods for buffer/field evaluation
Fixed broken indentation on dsdt.c

ok marco@


# 1.20 17-Jan-2006 jordan

Major overhaul of the aml parser. Evaluater temporarily disabled.

ok marco@


# 1.19 06-Jan-2006 grange

Check for ACPI control availability only if ACPI_ENABLE defined.


# 1.18 06-Jan-2006 grange

Informative error messages on attach, prettier dmesg.


# 1.17 06-Jan-2006 grange

printf -> dnprintf conversion for debug messages.


# 1.16 05-Jan-2006 grange

Find SLP_TYPx values and store them for future use in
acpi_enter_sleep_state().


# 1.15 05-Jan-2006 grange

Random KNF and cleanup


# 1.14 05-Jan-2006 grange

Provide symbolic names for the acpi sleeping states and use it.


# 1.13 28-Dec-2005 marco

Moved DEVNAME to a acpidev.h

ok jordan@


# 1.12 18-Dec-2005 sturm

add back comment about wrong code

ok marco


# 1.11 16-Dec-2005 marco

Remove test for kbd_reset. Prompted deraadt@, ok jordan@


# 1.10 16-Dec-2005 jordan

Added hooks for powerdown/power button press


# 1.9 16-Dec-2005 jordan

Cleaned up common power management register i/o
Added enter sleep state ioctl
Hooked power button interrupt
ok marco@


# 1.8 16-Dec-2005 marco

Remove autoconf chatter


# 1.7 16-Dec-2005 jordan

Moved DSDT AML parsing into parent ACPI driver
ok marco@


# 1.6 14-Dec-2005 marco

Add AC device glue.


# 1.5 14-Dec-2005 marco

Adjust debug printing levels to something more sane.


# 1.4 13-Dec-2005 marco

Add glue to attach devices to apci.


# 1.3 07-Dec-2005 marco

KNF


Revision tags: OPENBSD_3_8_BASE
# 1.2 10-Jul-2005 grange

Spacing cleanup.


# 1.1 02-Jun-2005 tholo

Start on a basic ACPI framework -- does not do much more than read out the
ACPI tables into kernel memory and attach ACPI and HPET timers currently.

In order to test this code, enabling the devices in GENERIC as well as
the ACPI_ENABLE option is needed. This code does not do any thermal
control yet, so this should be done with care depending on the platform.

In the tree so more people can contribute to making this more fully
featured.

Ok niklas@ grange@ tedu@


# 1.421 29-Jun-2023 dv

acpi(4)/acpibtn(4): use opt-in approach for wakeup GPEs

Previously, any discovered GPE that could be enabled for wake was
being enabled prior to entering ACPI-based S3 or S4. On some newer
machines, this caused S3-based suspend to break as some devices we
don't fully control (e.g. lack of driver) might be capable of waking
the system and will do so almost instantly if the GPE is enabled for
wake up (i.e. the so called "instant wake" issue).

This changes to a model of having device drivers explicitly opt
into using their GPE for wake up.

The first driver to explicitly toggle a GPE for wake is acpibtn(4).

Variations of the above have been in snapshots for 2 weeks, so
tested by many.

ok deraadt@


Revision tags: OPENBSD_7_3_BASE
# 1.420 15-Mar-2023 kettenis

Add missing nameref resolving for the _DEP() package elements that indicate
dependencies between ACPI devices.

ok dlg@, deraadt@


# 1.419 10-Feb-2023 visa

Adjust knote(9) API

Make knote(9) lock the knote list internally, and add knote_locked(9)
for the typical situation where the list is already locked.

Remove the KNOTE(9) macro to simplify the API.

Manual page OK jmc@
OK mpi@ mvs@


Revision tags: OPENBSD_7_2_BASE
# 1.418 13-Sep-2022 kettenis

Split out the code that collects data from acpiac(4), acpibat(4) and
acpisbs(4) for apm(4) and hook it up to the arm64 version of apm(4) on
systems with ACPI.

ok kn@


# 1.417 12-Sep-2022 kettenis

Add support for level-triggered GPIO events.

ok mlarkin@


# 1.416 10-Sep-2022 kettenis

Don't restrict GPIO events to pin numbers below 256. With the _EVT()
method events corresponding to larger numbers are possible.

Fixes lid state detection on the x13s.

ok patrick@


# 1.415 03-Sep-2022 kettenis

Make sure we don't suspend if we have no way to wake up the machine.
The idea is that device drivers that can wake up the machine register
themselves by calling device_register_wakeup(). To prevent regressions
on amd64, we let acpi(4) register itself as a wakeup device if the AML
for the machine defines wakeup devices. This may be refined in the
future.

This diff will prevent people from suspending their arm64 SBCs without
having a way to wake them up. For now the only driver that registers
itself is axppmic(4), which means that at this moment only SBCs with
an allwinner A64 or H5 SoC will actually support suspend/resume.

ok mlarkin@, deraadt@


# 1.414 10-Aug-2022 patrick

Provide the AML root in ACPI's softc so that drivers that need access
don't have to declare it using extern.

Suggested by and with feedback from kettenis@
Tested by deraadt@ on arm64, amd64 and i386
Tested by bluhm@ on amd64 and i386


Revision tags: OPENBSD_7_1_BASE
# 1.413 17-Feb-2022 jsg

reduce includes


# 1.412 10-Feb-2022 visa

Embed klist head in acpi_softc to avoid explicit malloc.

OK kettenis@


# 1.411 09-Feb-2022 deraadt

Move x86-only stubs used by kern/subr_suspend.c into dev/acpi/acpi_x86.c
for sharing between i386 and amd64.
ok mlarkin kettenis


# 1.410 08-Feb-2022 deraadt

The suspend/resume code is a sticky mess of MI, MD, and ACPI sequencing.
This splits out the MI sequencing, backing it with per-architecture helper
functions. Further steps will be neccesary because ACPI and MD are too
tightly coupled, but soon we'll be able to use this code for more architectures
(which depends on figuring out the lowest-level cpu sleeping method)
ok kettenis


# 1.409 04-Feb-2022 robert

set the APM_BATT_CHARGING state if the battery is being charged and try
to calculate the remaining time to be fully charged

ok jca@


# 1.408 02-Feb-2022 deraadt

acpi_addtask() calls malloc() w/ M_NOWAIT (because some calls come from
interrupt context), this however means occasional resource shortage will
result in callbacks registration failing, and unknown consequences for
the task-submitting caller.
Changing this to use pools with a low water mark, decreases the odds
of that problem occuring.
ok kettenis


# 1.407 01-Feb-2022 deraadt

GPE_DIRECT is calling the AML parser from interrupt context, which is
not permitted. Luckily nothing is using GPE_DIRECT anymore, so this code
can be deleted.
ok kettenis


# 1.406 26-Jan-2022 kettenis

An ACPI device needs to be both present and enabled for it to function.
So only attempt to attach hardware that has both bits enabled. This fixes
an issue where com(4) would attach for a disabled serial port leading to
misdetection of the hardware variant and a subsequent hang when /etc/rc
runs ttyflags -a.

ok anton@, deraadt@


# 1.405 12-Jan-2022 patrick

Make acpi_getpropint() return uint64_t, as ACPI integers are in fact that
wide and some _DSD properties depend on it.

ok kettenis@


# 1.404 04-Jan-2022 patrick

acpi_getprop() needs to actually make sure that we're looking at the
correct property. While there adjust acpi_getpropint() as well to
increase similarity with acpi_getprop().

ok kettenis@


# 1.403 16-Dec-2021 anton

Attach com over acpi on amd64. Some hardware uses a different interrupt
assignment compared to the the legacy one supported by com over isa.
This causes the console to halt once userland takes over as no
interrupts are received. The actual address and irq can be read from
ACPI, kettenis@ already added support for arm64 which paved the way for
amd64.

Some consoles that previously attached over isa are now expected to
attach over acpi.

Thanks to patrick@ for testing on arm64.

ok kettenis@


# 1.402 07-Dec-2021 kettenis

Fix parsing of SR_IRQ resource descriptors.

ok patrick@, anton@


# 1.401 02-Nov-2021 deraadt

knf


# 1.400 30-Oct-2021 deraadt

Change hw.perfpolicy=auto by default, at startup. If the system has AC
power connected (default is yes when no driver differentiates) then default
to 100% performance. On battery, use the existing auto algorithm (which is
admittedly somewhat unrefined).
This change overrides the system/BIOS speed and puts OpenBSD in control.
As this happens very early during boot, besides speedups in all usage usage
patterns, some surprises: unhibernate and sysupgrade times are cut in half.
note: on a few architectures, the setperf fn pointer is changed late, and
thus the auto algorithm stops timeing out. kettenis and i will look for
a solution.
in snaps for more than a week.
ok kettenis


Revision tags: OPENBSD_7_0_BASE
# 1.399 20-Jul-2021 mlarkin

Fix trailing whitespace in a few places.

No code change.


# 1.398 09-May-2021 kettenis

Some x86 machines advertise the "hardware reduced" ACPI feature, advertise
S4 and S5 support, but fail to populate the SLEEP_CONTROL_REG and
SLEEP_STATUS_REG descriptions in the FADT. An example of such a machine
is the ASUS Zenbook 14 UM433DA. Any attempt to powerdown the machine
will result in a kernel crash. It turns out that using the legacy ACPI PM
registers works fine on this machien. So fall back on those registers
if SLEEP_CONTROL_REG and/or SLEEP_STATUS_REG aren't provided.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_9_BASE
# 1.397 15-Mar-2021 patrick

Add acpi_iommu_device_map(), which replaces the DMA tag with one that
is blessed with IOMMU magic, if available. This is mainly for arm64,
since on amd64 and i386 the IOMMU only captures PCIe devices, as far
as I know, which uses the pci_probe_device_hook(). This though is for
non-PCI devices attached through ACPI.

ok kettenis@


# 1.396 07-Mar-2021 patrick

Pass standard DMA tag to acpi(4) table drivers.

ok kettenis@


# 1.395 27-Dec-2020 deraadt

acpi_map_address() cannot be wrapped by SMALL_KERNEL anymore, as
it is used by acpihpet.c


# 1.394 25-Dec-2020 visa

Refactor klist insertion and removal

Rename klist_{insert,remove}() to klist_{insert,remove}_locked().
These functions assume that the caller has locked the klist. The current
state of locking remains intact because the kernel lock is still used
with all klists.

Add new functions klist_insert() and klist_remove() that lock the klist
internally. This allows some code simplification.

OK mpi@


# 1.393 17-Dec-2020 kettenis

Fix some issues with referencing named ACPI nodes from Packages.
These references need to be resolved at runtime rather than when
they're parsed such that they pick up the right values for those nodes
which can be changed when for example _INI methods run.

The current approach is to replace these reference with a string that
names the node in question. The problem with that is that packages
can also contain normal strings. Which means that we need special
code that depends on the context where the package is used.

This diff takes a different approach by retaining a reference when
parsing. Code that uses a package will need to resolve this reference
but there is no ambiguiety anymore.

ok patrick@


# 1.392 05-Dec-2020 kettenis

Add support for the _EVT method to the GPIO support code.

ok patrick@


Revision tags: OPENBSD_6_8_BASE
# 1.391 27-Aug-2020 jmatthew

Reorder the acpi attach process so we look at Processor() CPU nodes
after Device() ones, since we should prefer the newer node type.
If we see any Device() nodes, don't attach acpicpu(4) to any Processor()
nodes if they're also present. This also makes acpitz(4) and
acpipwrres(4) devices attach slightly later.

ok kettenis@, also tested by jmc@


# 1.390 16-Aug-2020 gnezdo

Remove sc_maxgpe from acpi_softc which was never read from

Remove a dead conditional from acpi_init_gpes while there.

ok jcs@


# 1.389 21-Jul-2020 deraadt

acpi can use IPL_BIO (a low interrupt) since it only enqueues operations for
later processing. The use of a high interrupt will predate suspend/resume
efforts, we had to redesign acpi to be non-reentrant obviously
discussed with kettenis, in snaps for more than a week


# 1.388 24-Jun-2020 cheloha

kernel: use gettime(9)/getuptime(9) in lieu of time_second(9)/time_uptime(9)

time_second(9) and time_uptime(9) are widely used in the kernel to
quickly get the system UTC or system uptime as a time_t. However,
time_t is 64-bit everywhere, so it is not generally safe to use them
on 32-bit platforms: you have a split-read problem if your hardware
cannot perform atomic 64-bit reads.

This patch replaces time_second(9) with gettime(9), a safer successor
interface, throughout the kernel. Similarly, time_uptime(9) is replaced
with getuptime(9).

There is a performance cost on 32-bit platforms in exchange for
eliminating the split-read problem: instead of two register reads you
now have a lockless read loop to pull the values from the timehands.
This is really not *too* bad in the grand scheme of things, but
compared to what we were doing before it is several times slower.

There is no performance cost on 64-bit (__LP64__) platforms.

With input from visa@, dlg@, and tedu@.

Several bugs squashed by visa@.

ok kettenis@


# 1.387 10-Jun-2020 jca

Give userland a consistent battery and A/C state after resume

Previously we refreshed the state of acpiac(4) and acpibat(4)/acpisbs(4)
by queueing fake ACPI events to be processed by the acpi thread.
acpibat(4) using ACPIDEV_POLL meant that its status could be refreshed
first, resulting in an APM_POWER_CHANGE event sent to userland before
the state of acpiac(4) was up to date. Because of this, apmd(8) could
see A/C unplugged when it was in fact plugged, possibly leading to
a wrong decision.

Instead of using fake ACPI events, refresh the status of acpiac(4) and
acpibat(4)/acpisbs(4) right away at DVACT_WAKEUP time. To reuse the
existing refresh code of those drivers, make their ACPI *_notify()
handlers responsible for sending APM_POWER_CHANGE events instead. Also
queue an APM_POWER_CHANGE event right after resume to retain existing
behavior.

This fixes the consistency of data shown to userland, and makes those
drivers less special.

Initially discussed last year (!) with mpi@ at Miod's place.
Feedback and ok jcs@, deraadt@ agrees with the use of DVACT_WAKEUP here.


# 1.386 29-May-2020 deraadt

dev/rndvar.h no longer has statistical interfaces (removed during various
conversion steps). it only contains kernel prototypes for 4 interfaces,
all of which legitimately belong in sys/systm.h, which are already included
by all enqueue_randomness() users.


# 1.385 14-May-2020 kettenis

Bring back the acpi(4) changes. This time around only use the ACPI _CRS
information on ACPI 5.0 and later.

ok krw@, patrick@


# 1.384 11-May-2020 jca

More accurate remaining power reporting with disparate batteries

Overall remaining power was computed as the average of the remaining
power of each battery, in percents. This doesn't work with batteries of
different capacity, so use the total capacity and remaining power
instead.

Reported by lidstah, successful tests by lidstah and benno@, ok kettenis@


# 1.383 08-May-2020 kettenis

Move parsing of _CRS into acpi(4) and pass the parsed address and interrupt
information in struct acpi_attach_args.

ok mpi@


Revision tags: OPENBSD_6_7_BASE
# 1.382 14-Apr-2020 kettenis

Implement acpi_getprop(), the ACPI equivalent of OF_getprop().

ok patrick@


# 1.381 12-Apr-2020 kettenis

Add support for the _CCA method. This method indicates whether DMA is
cache-coherent or not. To implement this, acpi(4) gets two bus_dma tags
and passes the appropriate one when attaching devices based on _CCA.
On i386/amd64, where for all practical purpose DMA is always cache-coherent,
the two tags are the same. But on arm64 they are distinct.

ok patrick@


# 1.380 07-Apr-2020 visa

Abstract the head of knote lists. This allows extending the lists,
for example, with locking assertions.

OK mpi@, anton@


# 1.379 03-Apr-2020 mpi

Fix incoherencies of errors returned by various *kqfilter routines.

EOPNOTSUPP is returned when there's no "filterops" for a given fd.
EINVAL when the requested filter isn't supported by the "filterops".
ENXIO means the underlying device is no longer valid/present.

ok visa@


# 1.378 20-Feb-2020 visa

Replace field f_isfd with field f_flags in struct filterops to allow
adding more filter properties without cluttering the struct.

OK mpi@, anton@


# 1.377 31-Dec-2019 visa

Use C99 designated initializers with struct filterops. In addition,
make the structs const so that the data are put in .rodata.

OK mpi@, deraadt@, anton@, bluhm@


# 1.376 31-Dec-2019 mpi

Convert infinite sleeps to {m,t}sleep_nsec(9).

ok kn@


# 1.375 31-Dec-2019 jsg

include sys/sensors.h for sensor use
don't depend on indirect inclusion from acpidev.h


Revision tags: OPENBSD_6_6_BASE
# 1.374 07-Sep-2019 kettenis

Revert acpipci(4) changes. Too many systems are broken. We'll try again
after 6.6 as been released. The acpireg.h change stays behind.


# 1.373 28-Aug-2019 kettenis

Use ACPI information to attach PCI busses like we do on arm64. There are a
few additional quirks though, and attaching the PCI busses is delayed to
replicate the existing code more closely. That may be changed in the
future. Also tweak how we handle MSI support and respect to ACPI flag
that says we shouldn't attempt to use MSIs.

Some fallout is expected.

ok patrick@


# 1.372 27-Aug-2019 deraadt

On amd64 the bootup and resume paths for SP/BP/SP were not performing
msr/pat, clock-startup, firmware upgrade, fpu initialization, and cpu
soft-state initialization in the same order. That kind of chaos is
crazy and fragile. Improve the acpi resume call sequence and unify order
of the operations.


# 1.371 02-Jul-2019 jcs

acpi_set_gpehandler: turn edge arg into flags, add support for
passing GPE_DIRECT which directly executes the supplied callback and
clears a level interrupt rather than going through acpi_taskq which
can be slow when other driver callbacks are in the queue.

This should not be used for GPE handlers that need to do anything
ACPI-related.

ok kettenis


# 1.370 10-Jun-2019 kettenis

Print proper ACPI version number.

ok mlarkin@


# 1.369 08-Jun-2019 kettenis

When establishing the mapping between ACPI device nodes and PCI devices,
skip PCI host bridges and devices that aren't there.

ok patrick@


# 1.368 07-Jun-2019 kettenis

Introduce acpi_getsta() and use it to eliminate some duplicated code.

ok patrick@


# 1.367 12-May-2019 kettenis

Make sure the extended PM register descriptions actually exist before
using them. Fixes machines with ancient ACPI 1.0 (such as recent
hypervisors).

ok deraadt@


# 1.366 12-May-2019 kettenis

Add support for mapping ACPI PM registers using the "extended" gasio
representation from the FADT. Mostly fixes the Lanner NCA-1510.

ok lteo@, mlarkin@, deraadt@


# 1.365 11-May-2019 lteo

Fix incorrect error message.

ok kettenis@


# 1.364 08-May-2019 tedu

free size. the allocation is the header plus the length.


Revision tags: OPENBSD_6_5_BASE
# 1.363 04-Apr-2019 kettenis

Avoid potential null-pointer dereference. Found by Kent R. Spillner.

ok deraadt@, jsg@


# 1.362 20-Jan-2019 jsg

fix SMALL_KERNEL build after changes in rev 1.361
reported by naddy@ ok deraadt@


# 1.361 19-Jan-2019 tedu

Add a pwraction sysctl that controls what the power button does on acpi.
By default, nothing changes -- shutdown is initiated. But allows turning
power button into a sleep button if desired.
(grudging) ok from a few parties


# 1.360 26-Oct-2018 kettenis

Add acpipci(4) on amd64. For now this only calls the PCI-specific _OSC
method to let the ACPI implementation know what features we support.


Revision tags: OPENBSD_6_4_BASE
# 1.359 30-Aug-2018 kettenis

Move softbutton check before the check that skips attaching certain acpi
devices. Fixes regression caused by matching on _CID in addition to
matching on _HID.

ok matthieu@, mlarkin@


# 1.358 25-Aug-2018 kettenis

Match ACPI devices based on _CID if no match for _HID is found.

ok mpi@


# 1.357 19-Aug-2018 kettenis

Add support for multiple PCI segments. Only really implemented for arm64
for now as amd64/i386 firmware still caters for legacy OSes that only
support a single PCI segment.

ok patrick@


# 1.356 03-Aug-2018 kettenis

Let ahci(4) match on _CLS instead of _HID when attaching at acpi(4). Avoids
having to add many more _HID entries to the match table.

ok deraadt@, mlarkin@


# 1.355 10-Jul-2018 kettenis

Make legacy interrupts work in acpipci(4).

ok patrick@


# 1.354 05-Jul-2018 kettenis

Add acpipci(4), a driver that supports generic ECAM-compatible PCI host
bridges based on information provided by ACPI.

ok mlarkin@


# 1.353 04-Jul-2018 kettenis

Properly pass around the PCI "chipset tag" in acpi(4) and refactor
acpimcfg(4) to call an MD initialization functions that sets up a tag for
PCI ECAM.

ok guenther@, mlarkin@, krw@


# 1.352 01-Jul-2018 kettenis

Move acpi(4) attach glue into acpi_machdep.c.

ok guenther@, deraadt@


# 1.351 01-Jul-2018 kettenis

Add support for _DSD Device Properties.

ok mlarkin@


# 1.350 30-Jun-2018 kettenis

Add intr_enable() function, intended for MI use to amd64 and i386 and use
this in the acpi(4) suspend/resume code paths.

ok deraadt@


# 1.349 29-Jun-2018 kettenis

Use <stdint.h> types.

ok mpi@, jung@, krw@, deraadt@


# 1.348 29-Jun-2018 mlarkin

ACPI: Allow (with warning) GPE handler reassignment, instead of returning
a failure code.

ok kettenis


# 1.347 26-Jun-2018 mlarkin

knf


# 1.346 26-Jun-2018 kettenis

Let acpi(4) pass down the bus dma tag.

ok deraadt@


# 1.345 25-Jun-2018 kettenis

Reorganize acpi(4) code a bit in preparation for upcoming arm64 support.

ok deraadt@


# 1.344 20-May-2018 kettenis

Wake up the acpi thread after scheduling a gpio event.

ok mpi@


# 1.343 17-May-2018 kettenis

Sprinkle some #ifndef SMALL_KERNEL such that RAMDISK kernels cuild again.

pointed out by stsp@


# 1.342 17-May-2018 kettenis

Implement GenericSerialBus OpRegion support.

ok mlarkin@


# 1.341 27-Mar-2018 kettenis

Add acpicmos(4), a driver that implements SystemCMOS OperationRegion
access support. This fixes machines where the AML doesn't check whether
support for this OperationRegion type has been registered by the OS.

ok mlarkin@


Revision tags: OPENBSD_6_3_BASE
# 1.340 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.339 10-Feb-2018 deraadt

Syncronize filesystems to disk when suspending. Each mountpoint's vnodes
are pushed to disk. Dangling vnodes (unlinked files still in use) and
vnodes undergoing change by long-running syscalls are identified -- and
such filesystems are marked dirty on-disk while we are suspended (in case
power is lost, a fsck will be required). Filesystems without dangling or
busy vnodes are marked clean, resulting in faster boots following
"battery died" circumstances.
Tested by numerous developers, thanks for the feedback.


# 1.338 08-Feb-2018 deraadt

After flushing disks, try again to release all memory. We don't need to
save dirty memory to the hibernate space.


# 1.337 08-Feb-2018 deraadt

Toss all releaseable memory, because fragmentation can get in the way
of allocating the hibernate playpen.


# 1.336 08-Feb-2018 deraadt

sr_quiesce() is a new approach for ensuring that softraid drains
output to the disks.

This is part of a larger suspend/resume filesystem-safety diff, which
has been worked on for a couple of months already. Tests by job, krw,
beck, benno, and others. Sometimes even by snapshot users...


# 1.335 29-Nov-2017 kettenis

Add support for GPIO-signaled events.

ok mlarkin@


# 1.334 16-Nov-2017 jcs

Add PCI attachment for dwiic(4) needed by Intel 100 Series machines

ok kettenis


# 1.333 14-Nov-2017 kettenis

Add a few more PNP IDs to the skip lists.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.332 17-Aug-2017 stsp

Make dwiic(4) attach its ACPI parent dependencies.
This change makes the Asus E200HA keyboard work.

Original analysis and patch by Cesare Gargano
ok kettenis@


# 1.331 09-Aug-2017 dcoppa

Add "PNP0303" (8042 PS/2 Controller) to acpi_isa_hids[]

ok kettenis@


# 1.330 22-Jul-2017 jcs

acpibat: look for _BIX first before falling back on _BIF

ACPI 4.0 deprecated _BIF for battery status so some newer machines
are exporting _BIX which extends _BIF's package with a few extra
fields. When using _BIX, export the new discharge cycle count as a
sensor.

feedback from kettenis and anton
ok deraadt


# 1.329 20-Jul-2017 kettenis

Start a list of isa(4) device names such that we can avoid printing them
as "not configured".

ok mikeb@, millert@, deraadt@


# 1.328 08-Apr-2017 deraadt

Sensors are run as callbacks inside tasks. During suspend a sensor
could be running inside a driver that will be force-detached, or due
to tsleep end up disrupting the softstate/hardstate contract. At
suspend time, quisce all these callbacks by waiting for completion.
This issue has never been observed for real, but may be implicated in
suspend/resume failures.
ok kettenis guenther mlarkin


# 1.327 08-Apr-2017 deraadt

Two prototypes for acpi_maptable; merge to one place


Revision tags: OPENBSD_6_1_BASE
# 1.326 27-Mar-2017 deraadt

Now that hibernate_alloc() only has clean success/failure, don't
need to call hibernate_free() to clean up a partial mess.
ok mlarkin kettenis


# 1.325 13-Mar-2017 deraadt

allow ramdisks to build; ok jcs


# 1.324 12-Mar-2017 jcs

try to attach acpisbs first and if successful, don't attach acpibat


# 1.323 02-Mar-2017 natano

Add a new sysctl machdep.lidaction. The sysctl works as follows:

machdep.lidaction=0 # do nothing
machdep.lidaction=1 # suspend
machdep.lidaction=2 # hibernate

lidsuspend is just an alias for lidaction, so if you change one, the
other one will have the same value. The plan is to remove
machdep.lidsuspend eventually when people have upgraded their
/ets/sysctl.conf.

discussed with deraadt, who came up with the new MIB name
no objections mlarkin
ok stsp halex jcs


# 1.322 28-Feb-2017 natano

Untangle abstract sleep modes from ACPI sleep states. This paves the way
for further work in the area.

idea and ok deraadt
ok mlarkin


# 1.321 22-Feb-2017 jcs

add acpisbs, an acpi smart battery subsystem driver reading data
over smbus

currently disabled because it conflicts with acpibat

ok deraadt, kettenis


# 1.320 14-Jan-2017 kettenis

Revert aml_rdpciaddr changes; breaks several machines that were working
before.


# 1.319 11-Jan-2017 jsg

Increase the size of the buffers used for _HID and _CID strings to be
able to fit "Hyper_V_Gen_Counter_V1".

ok mlarkin@ deraadt@


# 1.318 08-Jan-2017 kettenis

Make aml_rdpciaddr() work for for devices behind bridges. The code now
uses the mapping between AML nodes and PCI devices that we establish.
Because _INIT methods may end up calling aml_rdpciaddr(), make sure we
create that mapping early. Also handle devices that aren't actually present.
These devices are now included in the mapping and reads will return an
all-ones patterm whereas writes are a no-op..

ok mlarkin@


# 1.317 25-Oct-2016 pirofti

Add the resource index to aml_parse_resource() callback function.

This is needed by an upcoming acpiec commit that handles machines breaking
the current ACPI specifications.

Change suggested by and ok kettenis@, guenther@


# 1.316 18-Sep-2016 guenther

If the APIC table (MADT) isn't present then ci_acpi_proc_id won't be set
when acpicpu tries to attach, leading to a NULL dereference. The ACPI
processor object on the Dells where this was reported is bogus and useless
anyway so apply the same test when deciding whether to attach to processor
objects as acpicpu would when matching them to cpus.

Problem reported by Eivind Eide (xenofil (at) gmail.com) and
Johan Huldtgren (johan+openbsd-bugs (at) huldtgren.com).
Testing by Eivind Eide.
ok kettenis@ mlarkin@


# 1.315 03-Sep-2016 naddy

Write the system time back to the RTC every 30 minutes.
This fixes the problem that long-running machines which were not
shut down properly would reboot with a badly offset system time.

hints and ok kettenis@


# 1.314 31-Aug-2016 mlarkin

Don't put a device in the wake device list if _STA says it's not there.

Fixes at least one machine that woke up from sleep / poweroff immediately.

ok kettenis@, tested by kettenis and martijn@


# 1.313 28-Jul-2016 kettenis

Store the acpi processor ID/UID in struct cpu_info, and use it to attach
acpicpu(4) drivers to the right cpu(4).

ok mlarkin@, guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.312 10-Jun-2016 kettenis

The power management timer has been made optional in ACPI 5.0A, so don't
attempt to attach acpitimer(4) if the timer isn't present.

Pointed out by jcs@

ok jcs@


# 1.311 08-May-2016 kettenis

Make sure devices listed by a device's _DEP method are attached before
we attach the device itself. The _DEP methode, introduced in ACPI 5.0,
is a hint that the device in question depends on OpRegion support from
the devices listed by _DEP, so we have to order them the proper way.
To prevent us from attaching those devices again when we encounter them
later walking down the device tree, keep track of our attempts to
attach a device driver to them using aflag in the aml_node structure.

ok guenther@


# 1.310 25-Apr-2016 pirofti

We attach acpihpet(4) and acpiec(4) without using the HID, so do not
report it in the dmesg.

OK deraadt@, mlarkin@.


# 1.309 02-Apr-2016 kettenis

Reduce dmesg spam by knocking out some acpi hardware IDs that don't need a
driver.

ok mpi@


# 1.308 29-Mar-2016 kettenis

Check if a device is present (using _STA) in acpi_foundhid. This prevents
us from reporting devices as "not configured" that aren't actually present,
and allows us to remove duplicated code from several drivers.

ok deraadt@, ok jsg@


# 1.307 29-Mar-2016 kettenis

Implement a cleaner way to prevent acpivout(4) from attaching if we have
a platform-specific method of controlling the backlight.

ok millert@, mlarkin@, jung@


# 1.306 28-Mar-2016 kettenis

Adding each and every new acpi device driver to acpi_foundhid() is
gettinga bit of the burden. And it really isn't how our autoconf
framework is supposed to work for busses that can be enumerated. All
the drivers already check for a matching _HID in their attach
function. So we can just drop the checks in acpi_foundhid() and let
the drivers sort themselves out.

Print a "not configured" line for devices for which we don't attach adriver.
This may be a bit much as there are devices for which we will not have a
driver. This will be removed if it causes too much dmesg spam.

ok deraadt@, mlarkin@


Revision tags: OPENBSD_5_9_BASE
# 1.305 17-Jan-2016 jsg

Take acpi_foundhid() outside of #ifndef SMALL_KERNEL so the ramdisk
kernel can find and attach dwiic(4) and sdhc(4).


# 1.304 17-Jan-2016 jsg

remove an unused global variable


# 1.303 14-Jan-2016 kettenis

Attach dwiic(4) on the Intel Bay Trail i2c controllers as well.


# 1.302 12-Jan-2016 kettenis

Improve emulation of PM registers on Hardware-redcued ACPI by emulating
SPL_TYPx, SLP_EN and WAK_STS using the new SLEEP_STATUS_REG and
SLEEP_CONTROL_REG registers. Makes the transition into S5 (powerdown) work
on the ASUS X205TA.

ok deraadt@, mlarkin@


# 1.301 12-Jan-2016 jcs

Add dwiic, a driver for the Synopsys DesignWare i2c controller found
on the Samsung ATIV Book 9 laptop. This initial version only
supports ACPI config/attachment.

Add ihidev, a HID-over-i2c driver largely based on uhidev. dwiic
handles attaching ihidev devices found in ACPI.

Add ims, a HID-over-i2c mouse/trackpad driver to get basic cursor
and button functionality from HID-compliant i2c trackpads.

ok kettenis deraadt


# 1.300 11-Jan-2016 kettenis

Make sdhc(4) attach to hardware IDs "80860F14" and "PNP0FFF".


# 1.299 09-Jan-2016 kettenis

Initial support for Hardware-Reduced ACPI. This skips the legacy mode checks
by emulating the PM_CNT register such that SCI_EN is always enabled. It also
skip attaching acpitimer(4) since the relevant registers are no longer there.

ok deraadt@, mlarkin@


# 1.298 06-Jan-2016 kettenis

Follow the spec and check the SCI_EN bit to see if ACPI has already
been enabled and don't bother enabling it again if it is. This should fix
systems that don't implement legacy mode and therefore set ACPI_ENABLE and
ACPI_DISABLE to zero.

ok mlarkin@, yasuoka@


# 1.297 23-Nov-2015 reyk

Back out the CRITICAL change, turn it back from 10% to 25%. Some
people with old/broken batteries didn't like this change, and the
underlying heuristics are wrong.


# 1.296 22-Nov-2015 reyk

The "CRITICAL" APM battery level, that is exported/faked by the ACPI
stack, is currently set to 25%. With 25% battery left, a typical
modern laptop will have at least one hour runtime left. It is an
arbitrary value, and other APM power layers in the kernel set CRITICAL
to something like 5% or 10%, so set it to 10% for now.

OK mlarkin@


# 1.295 28-Sep-2015 deraadt

In low-level suspend routines, set cold=2. In tsleep(), use this to
spit out a ddb trace to console. This should allow us to find suspend
or resume routines which break the rules. It depends on the console
output function being non-sleeping.... but that's another codepath which
should try to be safe when cold is set.
ok kettenis


# 1.294 06-Sep-2015 deraadt

clarify malloc arguments (base + length...)


# 1.293 28-Aug-2015 deraadt

fairly simple sizes for free(); ok tedu


# 1.292 20-Aug-2015 kettenis

Respect the access size when reading or writing to pci config space.
This version also makes sure writes are properly aligned, preventing
the panics seen with the previous incarnation of this change.

Fixes battery status passthrough in vmware, brightness keys on my x220
and who knows what else.

ok mlarkin@, deraadt@


Revision tags: OPENBSD_5_8_BASE
# 1.291 04-Aug-2015 deraadt

If we are setting ACPI_PM1_BM_RLD do it only once, and remember to
repeat this action at resume time.
ok mlarkin guenther


# 1.290 29-Jul-2015 deraadt

Something subtle in previous revision is locking up suspend or booting
on some laptops. Mailing lists threads mention x220 and Asus machines.
Reverting for now.


# 1.289 21-Jul-2015 kettenis

iRespect the access size when reading or writing to pci config space.
Fixes battery status passthrough in vmware and who knows what else.

Tested by many.

ok mlarkin@, deraadt@


# 1.288 17-Jul-2015 mlarkin

fix comments


# 1.287 26-Mar-2015 jsg

Use the 0x10 extended capability to determine if the trackstick
buttons are wired to the trackpad and need to be re-routed to the
trackstick. Without this change the buttons on 2015 thinkpads
get picked up as extended buttons that show up as scroll up/down.

Remove the X1 Carbon 2015 (LEN0048) and X250 (LEN0046) from the
top button area/soft buttons quirks list. Also avoid using the
quirk list entirely if the capability bit is set.

Based on work from jcs@ with feedback from mpi@
ok mpi@ jcs@


# 1.286 20-Mar-2015 kettenis

The ACPI standard says that a PCI function number of FFFF can be used to refer
to all functions on a device. This makes no sense in the context of accessing
PCI config space. Nevertheless stsp@ has a machine with AML that tries to
do config space reads from a device where _ADR encodes function number FFFF.
This is almost certainly buggy AML, but since Linux (and presumably Windows)
boot just fine on this machine we need to work around this. Linux effectively
treats function FFFF as function 7, which isn't present on this machine.
Instead we simulate a read from a non-existent device by returning a bit
pattern of all ones. Writes to function FFFF will still panic.

ok mlarkin@


# 1.285 16-Mar-2015 deraadt

Reduce use of SMALL_KERNEL in acpi, bringing acpiec driver and more gpe
handling into RAMDISK. This is now possible because the install media
has ample room. The goal is to reduce special cases where we may be
skipping (unknown) important operations...
ok mlarkin kettenis


# 1.284 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.283 07-Feb-2015 deraadt

New framework that allows hibernate to pass in entropy from it's fresh
boot.
ok mlarkin


# 1.282 06-Feb-2015 mlarkin

Fix a hibernate crash on some machines due to unmapping a page that
may not have been mapped previously (in the failure to hibernate case).

Also ensure that the lowmem ptp is mapped in all cases (not just MP).

ok kettenis


# 1.281 17-Jan-2015 deraadt

resume_randomness() before spinning up other cpus...


# 1.280 15-Jan-2015 jsg

Define a new wscons mouse type for Synaptics clickpad devices that lack
physical buttons. This will be used if the acpi pnpid for the mouse
matches a list provided by Synaptics found in Linux.

Combined with the xenocara changes this will remove the need for an xorg
config file for the x240/t440/t540 generation of thinkpads.

ok matthieu@ shadchin@ kettenis@


# 1.279 11-Jan-2015 kettenis

Tolerate checksum errors in ACPI tables (but complain about them). Apparently
there are machines out there with broken BIOSen with such checksum errors in
thier RSDT tables. Windows runs fine on these machines, so it probably does
not check. Linux ignores checksum errors as well (and prints a warning).

ok deraadt@, krw@


# 1.278 18-Dec-2014 deraadt

suspend and resume the RNG (at what looks like the right spots)
ok reyk djm


# 1.277 09-Dec-2014 doug

Replace some malloc(n*size,...) calls with mallocarray().

ok tedu@ deraadt@


# 1.276 06-Dec-2014 mpi

If "machdep.lidsuspend" is not set, do not go back to sleep if the lid
is still closed when resuming. This allows docked laptop to be resumed
without opening them.

ok mlarkin@


# 1.275 23-Nov-2014 mlarkin

If a machine has no _LIDs, then none can be open. This diff fixes a problem
where machines without any _LIDs were going back to sleep after hibernate
due to the recent suspend-if-lid-closed-on-resume diff. Noticed on qemu,
which doesn't have any _LIDs.

discussed with deraadt


# 1.274 18-Nov-2014 krw

Nuke yet more obvious #include duplications.

ok miod@


# 1.273 08-Nov-2014 mlarkin

If resuming from sleep (zzz/ZZZ) and the lid is still closed, go back to
sleep. This prevents accidental lid flex or slight opening in a backpack
from waking the machine up and leaving it resumed (powered on).

ok deraadt@


# 1.272 26-Sep-2014 kettenis

Rework piglet and pig allocation. Currently the piglet gets allocated
deep down in the suspend path, where it is really hard to recover from
allocation failure. So allocate the piglet early on in the suspend path.
Also change the piglet and piglet allocation functions to use km_alloc(9)
instead of doing pmemrange magic. This removes a bunch of code which, in the
case of the piglet allocation, is broken since it results in a NULL pointer
dereference. Also switch the piglet allocation to not wait. If we can't
allocate 16MB of phys contig memory on a halfway modern machine we're almost
certainly under a lot of memory pressure and we're better off not trying to
hibernate anyway.

ok mlarkin@


# 1.271 25-Sep-2014 kettenis

Bring back rev 1.249. Now that mp_setperf() has been fixed, it should be
safe to restore perflevel when resuming. Should fix the issues people
see with the processor running at full speed heating up the machine while
the apm command reports that the CPUs are running at a low speed.

ok dcoppa@, deraadt@


# 1.270 23-Sep-2014 kettenis

Release the acpi lock around calls to wsdisplay_suspend() and
wsdisplay_resume(). The X server may adjust the display brightness just
before/after the VT switch when suspending/resuming. In fact, it may
already be blocked adjusting the display brightness for some reason
unrelated to the suspend request.

The VT switch request will eventually time out, but the delay is annoying
and the failed VT switch will lead to problems after resume.

Needed for a future update of the xf86-video-intel driver.

ok mlarkin@


# 1.269 19-Sep-2014 kettenis

Use config_suspend_all(9).


# 1.268 08-Sep-2014 kettenis

Clear the PME Status bit on PCI devices in response to Device Wake events.
Fixes the interrupt storm on the 2009 Mac mini with WOL enabled on its
nfe(4) interface.

ok mlarkin@


Revision tags: OPENBSD_5_6_BASE
# 1.267 20-Jul-2014 mlarkin

Support hibernating to softraid crypto volumes.

much help and ok from deraadt@


# 1.266 16-Jul-2014 mlarkin

Reenable hibernate RLE support and flush+zero all memory after unpack.


# 1.265 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.264 12-Jul-2014 kettenis

Add a function to drop all clean pages on the page daemon queues and call
it when we hibernate.

ok mlarkin@, miod@, deraadt@


# 1.263 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.262 11-Jul-2014 mlarkin

Flush the buffercache to 16MB on hibernate and restore its previous max
size (kern.bufcachepercent) on resume, for better hibernate performance.

ok beck@


# 1.261 10-Jul-2014 blambert

remove unneeded header

ok mlarkin@


# 1.260 01-Jul-2014 kettenis

Adding an additonal delay after each access to SystemMemory or SystemIO
during autoconf makes no sense.

ok deraadt@


# 1.259 23-Jun-2014 kettenis

SystemMemory is used to access memory mapped registers on some machines, so we
must use the bus_space(9) API to access it instead of memcpy(9). Also make
sure we properly align access to fields that need more than byte alignment.

ok guenther@


# 1.258 21-May-2014 mlarkin

Fix invalid / missing format specifiers in several dnprintf statements
(used only when ACPI_DEBUG is enabled)

From a diff on tech@ by Fabian Raetz, with a minor change by me, tested
on i386 and amd64 with ACPI_DEBUG enabled and disabled.


# 1.257 25-Apr-2014 mlarkin

Defer the free of the hibernate structures until later, when it is safe to
do so.

Freeing the hibernate structures too early leads to inadvertent calls into
splx when we aren't ready for them yet. Found when trapping splx calls in
the resume path.

discussed with deraadt@ at m2k14


# 1.256 13-Apr-2014 mlarkin

Fix a badly worded debug message that implied the entire machine was going
to sleep, instead of just the acpi thread going to sleep.


# 1.255 31-Mar-2014 mpi

Change the order of operations during a suspend/resume cycle and
call bufq_quiesce() after executing the DVACT_QUIESCE handlers.

This should be safe since no disk nor controller drivers have such
handler but it will allow us to detach sd(4) devices attached to a
USB bus.

Another benefit pointed out by kettenis@ is that drivers that need
to read a firmware from the disk should be able to do it at resume
time in a DVACT_WAKEUP handler.

ok kettenis@, deraadt@


# 1.254 13-Mar-2014 dlg

get rid of the assumption that the head of the alldevs list is the
"mainbus" device. this breaks when mpath is enabled because it
attaches before mainbus and therefore takes the head position.

have autoconf provide device_mainbus() which looks up mainbus_cd,
and use that instead.

discussed with deraadt who just wants mpath stuff to move forward
despite there being many ways to shine this particular turd.


Revision tags: OPENBSD_5_5_BASE
# 1.253 21-Feb-2014 deraadt

Do the lid control further on in the resume sequence, since this is
AML, and will do WAITOK operations. While cold, that is a bad idea.
We seem to be safe with respect to the spec, I do not see any event
debounce issues here.
ok kettenis, tested by many


# 1.252 20-Jan-2014 claudio

Instead of checking sc->sc_revision <= 1 check that the
fadt->hdr_revision <= 1. This is a better check since it checks
the version of the proper descriptor. This allows the alix APU
to reboot since those boards have currently a wrong sc->sc_revision.
Committing this now so that it can be tested easier.
With and OK jsg@ also fine by derradt@


# 1.251 24-Dec-2013 kettenis

If the FADT has its SMI_CMD set to zero, assume we're only ACPI-only hardware
and don't need to disable SMI ownership of the ACPI hardware registers.

ok mlarkin@


# 1.250 23-Dec-2013 kettenis

Revert previous commit. It makes resume hang on some MP systems running
GENERIC.MP.


# 1.249 16-Dec-2013 kettenis

Restore hw.setperf upon resume like we do for apm(4) on i386. Seems we need
to do this fairly late in the resume to avoid hangs.

ok deraadt@, mlarkin@


# 1.248 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


# 1.247 06-Nov-2013 mpi

Add support for Power Resources for Dx states and the necessary hook
for PCI devices. This hook should be called twice, before and after
changing the power state of a PCI device.

Before setting the device to the new state, the ACPI layer will notify
every power resources linked to the device for that state and make sure
they are turned "_ON". After changing the state of the device, it will
decrement the reference of every power resources linked to that device
for the old state and turn them "_OFF" if they are no longer referenced.

This fixes the no-USB after resume problem seen on various ThinkPad,
problem initialy diagnosed with Alexander Polakov.

ok kettenis@, deraadt@


Revision tags: OPENBSD_5_4_BASE
# 1.246 01-Jun-2013 mlarkin

Stop printing ACPI wakeup devices in dmesg after the 16th wakeup device.

Helps with VMware VMs that report hundreds of wakeup devices.

ok reyk@, deraadt@


# 1.245 31-May-2013 bluhm

Fix the build for a kernel without wd and pciide in its config.
OK deraadt@ tedu@


# 1.244 30-May-2013 deraadt

Enforce ca_activate tree-walks over the entire heirarchy for all events,
cleaning up some shutdown-hook related code on the way.
(A few drivers related to sparc are still skipped at kettenis' request)
ok kettenis mlarkin, tested by many others too


# 1.243 18-Apr-2013 deraadt

pin the acpi thread to the BSP, because AML and SMI cannot be trusted. I
was certain we were doing this already, but it turns out no...


Revision tags: OPENBSD_5_3_BASE
# 1.242 24-Dec-2012 guenther

In acpiioctl(), Return though the splx() path if suser() returns an error

ok deraadt@


# 1.241 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


# 1.240 04-Oct-2012 ehrhardt

Use information provided by ACPI to attach secondary PCI host bridges.

ok kettenis@


# 1.239 07-Sep-2012 kettenis

Add a new function called acpi_pci_min_powerstate() that returns the lowest
power state for a PCI device for the power state (currently S3 or S4) we're
transitioning into. This function is probably too simplistic; there is a lot
of (silly) complexity in the ACPI spec.

ok deraadt@


Revision tags: OPENBSD_5_2_BASE
# 1.238 13-Jul-2012 pirofti

Disable acpivideo(4) on ASUS laptops.

There are some models that panic whenever an event is received.
Some cope, some don't. In order to get this right acpiec(4) requires
major rework. Until then let machines boot.

Discussed with deraadt@.


# 1.237 11-Jul-2012 mlarkin

for now, restrict hibernate to root in the acpi ioctl kernel interface.
normal rules for controlling this interface through apmd still apply.

ok deraadt@


# 1.236 09-Jul-2012 deraadt

tiny little oops in untested hibernate block


# 1.235 09-Jul-2012 deraadt

clean up ioctl() case statements


# 1.234 09-Jul-2012 deraadt

acpi_disable_onegpe() is not used; ok pirofti


# 1.233 24-May-2012 kettenis

If _MIN > _MAX consider a WordBus resource to be invalid. Fixes a panic on
the IBM x3550-M4.

ok mikeb@


# 1.232 29-Mar-2012 mlarkin

Missing #ifdef in previous diff


# 1.231 29-Mar-2012 mlarkin

Prevent hibernation attempts on machines with unsupported disk controllers.
The same check is also performed later in the hibernate sequence, but by
then it's too late to cleanly unwind (presently).

ok deraadt


# 1.230 26-Mar-2012 deraadt

simplify conditionals in acpi_sleep_state()
ok mlarkin


# 1.229 26-Mar-2012 deraadt

hook in the hibernate request code; half of this diff is from mlarkin
ok mlarkin


Revision tags: OPENBSD_5_1_BASE
# 1.228 20-Sep-2011 deraadt

Very early in suspend while the clock is still ticking realtime,
save the clock back to the rtc. The zaurus already did this.
ok phessler miod kettenis


Revision tags: OPENBSD_5_0_BASE
# 1.227 02-Jul-2011 nicm

kqueue attach functions should return an errno or 0, not a plain 1. Fix
the obvious cases to return EINVAL and ENXIO.

ok tedu deraadt


# 1.226 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.225 16-Jun-2011 pirofti

Import acpitoshiba driver.

This is originally written by Hiroyuki Aizu, ported and integrated
in our tree by Javier Vazquez <javama4 AT gmail> with tweaks and
suggestions by me.

Import prodded by deraadt@ and the lack of testers on tech@.


# 1.224 27-Apr-2011 jcs

attach acpithinkpad to newer lenovo models like the x120e

ok deraadt@


# 1.223 22-Apr-2011 jordan

Fix uninitialzied variables and formatting strings (-Wxxx errors)


Revision tags: OPENBSD_4_9_BASE
# 1.222 02-Jan-2011 jordan

Re-add code for ACPI taskqueue, eliminates need for state vars in acpi softc
Serialize processing ACPI tasks for sleep/powerdown/GPE event
ok deraadt@


# 1.221 31-Oct-2010 guenther

Revert last commit: it breaks resume on Thinkpads
"then please back it out" deraadt@


# 1.220 26-Oct-2010 jordan

Add task queue for ACPI gpe and notify handlers
This fixes eject on Dell Latitude dock and an issue on the Dell Mini
battery update.
ok deraadt


# 1.219 07-Oct-2010 jordan

Create separate functions for enabling wake and runtime gpes.
Remove old suspend/resume gpewalk function
ok deraadt, mlarkin


# 1.218 05-Oct-2010 jordan

split acpi_enable_onegpe to separate enable/disable functions
ok deraadt


# 1.217 05-Oct-2010 jordan

Added acpi_disable_allgpes function to clear GPE status
ok deraadt


# 1.216 05-Oct-2010 deraadt

clean up acpi_softc use more


# 1.215 05-Oct-2010 deraadt

delete a bit of #if 0 code


# 1.214 05-Oct-2010 deraadt

hide use of our global acpi_softc pointer a bit better


# 1.213 29-Sep-2010 deraadt

grab the acpi thread lock very early in acpi thread startup; not that it
is likely that anyone will race us... but why be careless.
ok kettenis


# 1.212 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.211 29-Aug-2010 jasper

- add a check for FWRITE here too.

ok miod@


# 1.210 27-Aug-2010 kettenis

Make PCI Power Management optional, and only enable it when acpi(4) attaches.

ok deraadt@


Revision tags: OPENBSD_4_8_BASE
# 1.209 08-Aug-2010 kettenis

Close race between the acpi thread and wscons ioctls running in process
context. We might rip this out post-release and replace it with a solution
that serializes things in a more obvious way.

ok deraadt@, miod@ and "those crazy texans who ok anything"


# 1.208 08-Aug-2010 deraadt

Report the acpi sleep states when on a RAMDISK; ok krw


# 1.207 07-Aug-2010 kettenis

Enforce acpi register access rescrictions.

ok mlarkin@, deraadt@, tested by marco@


# 1.206 07-Aug-2010 deraadt

upon resume, notify all the acpiac and acpibat drivers to update
their status. some laptops do not do this automatically.
ok kettenis canacar


# 1.205 06-Aug-2010 marco

Always call _PSW on _LID devices that support it to enable lid open to
resume the machine. Conversely disable it when the machine wakes up.

Tested by several ok deraadt


# 1.204 06-Aug-2010 deraadt

As part of a revamp of the PM1/GPE code, I write this diff to look for
unmanaged EN & STS bits in the PM1 register at interrupt time and report
them. As a side effect this splits the STS acknowledgement into two writes
(for power, and sleep) instead of one. The printf that is added (to spot
unmanaged STS bits) has not yet been triggered as far as we know.

Before the "write to PM1 registers at the right offset" diff went in, this
was not neccessary. But newer thinkpads do not have a working soft power
button without this diff.

We have no idea why.
ok mlarkin kettenis


# 1.203 05-Aug-2010 deraadt

PM1 power button events were generating 'fake events' to acpibtn PWRB,
but if there is no such device these were being tossed. Instead, arrange
for all power button handlers to flag a softc variable which is processed
by the thread; so that one place will know to request a powerdown (psignal
to init)
ok marco mlarkin


# 1.202 05-Aug-2010 deraadt

rename a variable which is annoyingly poorly named


# 1.201 05-Aug-2010 mlarkin

Write to the correct offset for various ACPI pmreg registers. Writing to
the correct register does properly clear the wake status bits and fixed
event bits, which allows some machines to have more than 1 suspend cycle.

bug spotted by deraadt@

tested by deraadt@ and myself on a half-dozen machines and others on
hackers@

ok deraadt@, marco@


# 1.200 05-Aug-2010 deraadt

Fix the power button. The pm1 register is 16 bits in size.
My mistake, a while back.
tested by krw


# 1.199 04-Aug-2010 kettenis

Print supported sleep states.

ok marco@, phessler@, deraadt@


# 1.198 03-Aug-2010 mlarkin

Use the proper flag for re-enabling certain hardware events (power btn,
sleep btn). The (incorrect) flag prevoiously used coincidentally had the
same value, so this shouldn't cause any different behavior than before.

tested on a variety of machines (i386, amd64, sp, mp) - no behavioral
change seen

ok deraadt@


# 1.197 28-Jul-2010 marco

Make all gasio failures look and behave the same.


# 1.196 28-Jul-2010 mlarkin

Delay a little longer on suspend before giving up. Fixes machines that
sometimes don't suspend because the sleep transition takes too long
(symptoms like failing to sleep but the backlight stays on).

ok deraadt@


# 1.195 27-Jul-2010 deraadt

remove XXX; EC can now be attached early when needed; ok jordan


# 1.194 27-Jul-2010 deraadt

more brackets feels good


# 1.193 27-Jul-2010 deraadt

XXX annotate a block in acpi_enter_sleep_state() that is pretty worrying


# 1.192 27-Jul-2010 deraadt

horrific indentation that nearly made me puke


# 1.191 27-Jul-2010 mlarkin

Set the sleeping indicator light on machines that support it, in case SMI
doesn't do it for us. Shows the 'moon' LED on older Thinkpads when
sleeping.

Tested on T43p, W500, T510 by me and on X61s by deraadt@.

ok deraadt@


# 1.190 27-Jul-2010 jordan

Early initialization of acpiec if ECDT table exists
Fixes hang when booting thinkpads while docked
ok deraadt


# 1.189 26-Jul-2010 deraadt

remove merge error which was commited


# 1.188 26-Jul-2010 pirofti

Add support for Sony ACPI hotkeys via a new driver: acpisony(4).

Currently it only works for the suspend button and tries to do right for
the brightness events, but I haven't found a Sony laptop that like to do
right so far.

In the future I want to make the brightness keys work on all Sony's and
also add support for the zoom hotkeys and whatever other funky keys I can
find on those things.

Okay deraadt@.


# 1.187 22-Jul-2010 deraadt

pretty large cleanup of SMALL_KERNEL


# 1.186 21-Jul-2010 deraadt

in the acpi GPE handler, late ack level interrupts not edge interrupts.
Duh.


# 1.185 21-Jul-2010 deraadt

Merge the edge and level gpe handlers into one (now possible since the
gpe structure tells us whether the gpe is edge or level)


# 1.184 21-Jul-2010 deraadt

delete #if 0 code we are not going to use; ok marco


# 1.183 20-Jul-2010 deraadt

make the mask code for acpi_enable_onegpe() match other similar code


# 1.182 20-Jul-2010 deraadt

Handle the acpi interrupt controller with proper edge/level handling,
wrapped in spltty to avoid racing against the interrupt controller.
Repair the gpe bit masking code while there, and do operations in the
right order
ok kettenis mlarkin, help from jordan at figuring out the order of
operations.


# 1.181 20-Jul-2010 matthew

When we're preparing to go to sleep, only call bufq_restart() if other
preparations failed.

ok kettenis@, thib@, deraadt@


# 1.180 19-Jul-2010 deraadt

permit compilation with ACPI_DEBUG


# 1.179 19-Jul-2010 deraadt

there is no need for a seperate dsdt_softc pointer; dsdt does not have a
softc but is just a part of the single acpi driver.
ok jordan


# 1.178 19-Jul-2010 deraadt

acpi_delay() is no longer needed; ok jordan


# 1.177 18-Jul-2010 mlarkin

Clear event status on resume, to avoid going back to sleep/reboot/shutdown
immediately after resume. Tested by a bunch of people on hackers@ on
various hardware. Fixes at least Toshiba NB200 and Gateway LT3103u.

ok marco


# 1.176 13-Jul-2010 deraadt

rename some thread related interfaces so they are logical


# 1.175 10-Jul-2010 jordan

Verify _ADR bdf is correct (some systems return 0xffff)
Works on Dell x9xx and Studio 1555


# 1.174 09-Jul-2010 jordan

Clear the noise


# 1.173 08-Jul-2010 jordan

Add mapping for ACPI device to PCI bdf (match autoconf tree)
Simplify resource parsing function to use buffer argument
Convert namespace linked lists to use queue macros
ok marco@, deraadt@


# 1.172 06-Jul-2010 deraadt

Use spltty() to lock downcalls from apm against the information being
modified by the acpi thread.
ok kettenis


# 1.171 06-Jul-2010 deraadt

Don't use a workq to do the suspend, because it races aginst the acpi
thread. Instead, just tell the acpi thread to do the suspend for us.
This makes apmd & zzz work correctly. While here, have acpithinkpad
attempt to post the event to apm before prompting the suspend itself.
ok kettenis marco mlarkin


# 1.170 05-Jul-2010 mlarkin

Reset register SCI_EN on resume. The ACPI spec clearly states we don't have
to do this, but many machines don't, or forget to, which leads to all sorts
of problems on resume (broken battery stats, overheating, failure to resume
more than once, etc).

tested by many, ok marco@, deraadt@


# 1.169 01-Jul-2010 thib

do bufq quiesce again, since vnd/the bufq_done call in
biodone has been fixed.


# 1.168 01-Jul-2010 jordan

Backout recent AML changes, breaks on R210 and others


# 1.167 01-Jul-2010 jordan

Simplified aml_parse_resource call, cleanup code
ok marco


# 1.166 01-Jul-2010 jordan

Fix subordinate bus # for multi-root PCI buses
ok marco, kettenis


# 1.165 30-Jun-2010 thib

Disable/partially backout the bufq quiesce changes as this
is causing havoc with vnds and release must be buildable.


# 1.164 29-Jun-2010 deraadt

Fix ramdisks (new code was placed inside a SMALL_KERNEL chunk)


# 1.163 29-Jun-2010 jordan

Add support for mapping ACPI to PCI devices
ok kettenis, deraadt


# 1.162 29-Jun-2010 kettenis

Call bufq_quiesce() on suspend to stop all I/O and bufq_restart on resume.
Makes suspend/resume work much better while doing disk I/O.

ok mlarkin@


# 1.161 29-Jun-2010 tedu

60 is a more reasonable guess for number of minutes in an hour than 100.
spotted by TeXitoi. ok deraadt


# 1.160 27-Jun-2010 jordan

Add _acpi_matchhids routine, precursor to pci device match


# 1.159 27-Jun-2010 mlarkin

Clear ACPI fixed event status on resume (power buttons/etc) so that some
machines don't immediately resume after suspending to S3.

Tested by kettenis@ and I on 5 or 6 machines, deraadt@ "yeah, looks good"


# 1.158 27-Jun-2010 jordan

Cleaned up acpi_maptable, adds to table list directly


# 1.157 07-Apr-2010 deraadt

If we have done a wsdisplay_suspend(), and ACPI decides it does not want to
suspend, we must roll back by calling wsdisplay_resume() or the virtual
console switching will be locked forever
ok oga


# 1.156 07-Apr-2010 kettenis

Make suspend/resume work on MP machines (running an MP kernel). Joint work
from mlarkin@ and me, with some amd64 fixes thrown in by deraadt@

ok marco@, deraadt@, pirofti@, mlarkin@


# 1.155 31-Mar-2010 kettenis

Bring /dev/apm support in line with apm(4). Make the suspend button send
a suspend request event to apmd(8) instead of suspending immediately. Also
keep track of whether /dev/apm and /dev/apmctl are currently open, such that
we can still suspend immediately if apmd(8) isn't running.

ok deraadt@, marco@, pirofti@, jsing@, oga@


# 1.154 30-Mar-2010 oga

Prevent the apmd/x races for good.

When we hit suspend time, go through all wsdisplays on the system. if
they are in mode MAPPED, but not MODE_DUMBFB then if possible do a full
vt switch to a !mapped vt, and prevent switching back until resume time.
This has to be called from MD code because this involves userland
running so that X can run the vt switch signal handler. This way, any
case where we are using the "poke registers from userland" model, we
will not be on the hardware when we go down, so the kernel can actually
handle thing properly.

Tested on several acpi laptops (by kettenis@ and ian@), x40 (me and
beck@ at LEAST) and zaurus (me). Maybe others, but if so I forgot who at
this time..

Idea from deraadt somewhere over the Faroe Islands (I thought of a
similar thing myself a while ago). Much prompting from him. Ok and
comments miod@


# 1.153 25-Mar-2010 oga

Fix & vs. && in the kqfilter code.

Based on a comparison with the apm code.

ok deraadt@, kettenis@


Revision tags: OPENBSD_4_7_BASE
# 1.152 26-Nov-2009 mlarkin

Visual (and on some machines, audio) feedback while the machine is
resuming via calls to aml _SST method. This also makes the "moon" LED on
thinkpads to blink during resume and cease when resume is completed.

ok pirofti@


# 1.151 26-Nov-2009 deraadt

Toggle into "cold" when we we disable interrupts, because tsleeping
in the AML interpreter and context switching into userland processes
tends to sometimes pop the ACPI trampoline page out of our address
space. Makes lots more machines work.
ok kettenis mlarkin


# 1.150 24-Nov-2009 jsg

in the resume path evaluate _WAK before dealing with device drivers
like the spec says, not after.

ok mlarkin@


# 1.149 24-Nov-2009 deraadt

abort suspends on SMP systems until that gets written; ok mlarkin


# 1.148 23-Nov-2009 mlarkin

pci should return the result of it's children's suspend/resume calls back
to its parent.

handle suspend failure case and unwind if devices have an activate function
that returns failure.

ok deraadt@, kettenis@


# 1.147 23-Nov-2009 mlarkin

acpi* devices need to call their children on suspend/resume.
ok deraadt@


# 1.146 23-Nov-2009 pirofti

Remove ACPI_SLEEP_ENABLED checks.

This enables by default the suspend/resume paths in the kernel.

Okay deraadt@.


# 1.145 23-Nov-2009 deraadt

knf and spacing that are super annoying


# 1.144 23-Nov-2009 mlarkin

Hook up Theo's new children suspend/resume function, as well as more ACPI
suspend/resume glue.

ok deraadt@


# 1.143 26-Oct-2009 deraadt

Do not do strncmp comparisons on dv_xname because longer device names which
look similar could arrive in the future. Instead, compare directly against
dv_cfdata->cf_driver->cd_name
Issue originally spotted by miod


# 1.142 26-Oct-2009 jordan

Verify checksum+address when loading ACPI tables.
Some systems had invalid entries in RSDT/XSDT.
ok marco@


# 1.141 23-Jul-2009 cnst

New aibs(4) driver for ASUSTeK AI Booster (ACPI ATK0110) hardware monitoring,
with sensor state support through limits provided by the ACPI.

Tested on several ASUS motherboards kindly networked by Sam Fourman Jr.

ok deraadt marco jordan


Revision tags: OPENBSD_4_6_BASE
# 1.140 03-Jun-2009 pirofti

Import acpi power resource driver. This is needed for suspend/resume logic.

Okay jordan@ and marco@.


# 1.139 03-Jun-2009 pirofti

Add acpivideo support. This adds brightness support for all laptops
except thinkpads, they will use the acpithinkpad driver. The driver is
also hooked into wsconsole. So brightness can be adjusted via:

$ wsconsctl display.brightness=<percentage>

This is very helpfull on some laptops that have a nasty bios and get two
steps instead of one when pressing the brightness button.

Tested on various dell, fujitsu, acer, samsung and other laptops.

Okay marco@, miod@. Suggestions from kettenis@.
Lots of reviews and help from miod@, thanks!


# 1.138 03-Jun-2009 jordan

Added ejectable bay for IDE cdroms
ok marco@


# 1.137 30-Apr-2009 marco

Oga changed apm ioctls and forgot to grep. Temporary work around by hitting
the sleep button when we get any request. This interface will chnage at
some point.
ok oga


# 1.136 19-Apr-2009 krw

Call acpi_sleep_walk() when powering down just like when entering
any other sleep state. This tells acpi wake devices to not wake up
the box until manual powering up resets their status. Makes my Dell
GX520 stay powered off in response to 'halt -p'.

In snaps for a while.

ok marco@ (several times) deraadt@


# 1.135 17-Apr-2009 pirofti

Add device ranking support for future sleep/wake developments.

okay marco@


# 1.134 11-Mar-2009 jordan

Cleanup.. use aml_evalinteger instead of aml_evalname
ok marco@


# 1.133 10-Mar-2009 jordan

Fix cut/paste error.. call _BFS not _PTS
ok marco@


Revision tags: OPENBSD_4_5_BASE
# 1.132 21-Feb-2009 marco

overzealous cut/paste; found by Vladimir Kirillov <proger@uaoug.org.ua>


# 1.131 20-Feb-2009 marco

Oops didn't mean for Debugger() to remain there.


# 1.130 19-Feb-2009 marco

suspend/resume bits so that we can develop this in tree. This is disabled.
code from mlarkin and me
help from art,toby,jordan and several others
ok jordan, go for it deraadt


# 1.129 10-Feb-2009 jordan

Fix null pointer for shutdown if acpi not enabled
ok marco@


# 1.128 04-Feb-2009 kettenis

Some systems that claim to be ACPI 2.0 apparently don't provide an XSDT
address. Make the code fall back on the RSDT on those systems.

From Joerg Niendorf <p3y80@internode.on.net>. Fixes PR 5986.


# 1.127 17-Jan-2009 krw

A couple of stray malloc/memset -> malloc(M_ZERO), a bit of knf and
lose a couple of unnecessary casts. No functional change.

Feedback from kettenis@

ok kettenis@ marco@


# 1.126 06-Nov-2008 marco

Collapse all sanity checks of hids into one function.

Originaly from jordan


Revision tags: OPENBSD_4_4_BASE
# 1.125 02-Jul-2008 fgsch

add acpivideo. for now almost an empty skel so others can work on
suspend/resume. eventually it will also manage output switching and
brightness where supported. prodded by marco@


# 1.124 11-Jun-2008 marco

Fix $OpenBSD$ for once and for all


# 1.123 08-Jun-2008 deraadt

aaa_dev was being set from an uninitialized variable; ok toby


# 1.122 07-Jun-2008 marco

Kill ACPIVERBOSE. It was never used and is worthless to boot.

comments jmc
ok toby


# 1.121 06-Jun-2008 marco

Shrink acpi some more for floppies and while here re-enable acpi on the
floppies. Fixes crazy sony vaio kettenis has.

tested by deraadt
ok deraadt jordan kettenis


# 1.120 01-Jun-2008 marco

Fix \n nit in SMALL_KERNEL.


# 1.119 01-Jun-2008 marco

Re-enable new parser. Lots of testing by various folks. Fits all media
now too.

ok kettenis deraadt


# 1.118 16-May-2008 dlg

back out jordans new parser.

it didnt have enough oks from the right people, it did break machines
that were previously working, and it bloated the RAMDISKS and install
media beyond the constraints of the media.

requested by deraadt@


# 1.117 14-May-2008 jordan

Adding new ACPI Parser code
Old guts of code still exists, needs to be torn out next
ok marco@
ok brad@


# 1.116 27-Apr-2008 jcs

add acpithinkpad(4), an acpi support driver for ibm/lenovo
thinkpads.

provides functionality that the bios did on older, non-acpi
thinkpads like respond to screen brightness keys and toggle the
bluetooth device on and off.

ok deraadt@ and marco@


# 1.115 24-Apr-2008 jsing

Introduce acpiasus(4), a driver for the ACPI based hotkeys found in many
ASUS laptops (including the ASUS EeePC) - largely based on NetBSD's
asus(4) driver. On the ASUS EeePC this allows us to enable/disable
wireless, change screen brightness and use the volume keys.

ok jsg@, weingart@


# 1.114 12-Apr-2008 kettenis

Get rid of acpi_s5 global variables; simply send SIGUSR2 instead of SUGUSR1
to tell init(8) to power down the machine.

ok krw@


Revision tags: OPENBSD_4_3_BASE
# 1.113 21-Jan-2008 jsg

Check against RESET_REG_SUP in FADT before doing the acpi reset dance
after all, not doing so appears to break more machines than it fixes.

ok marco@


# 1.112 05-Jan-2008 kettenis

Never attach more acpicpu(4) devices than cpu(4) devices. Always attach
the first acpicpu(4) device on non-MULTIPROCESSOR kernels, regardless of its
ACPI processor ID.

ok mikeb@


# 1.111 05-Dec-2007 deraadt

Hang acpi(4) below bios(4) instead of mainbus(4). This lets us move acpi(4)
before pcibios(4), and then.... if acpi(4) attaches, skip pcibios(4) since
it messes the machines up. Fixes claudio's HP dl320 G5, and almost assuredly
others.
ok kettenis


# 1.110 05-Dec-2007 deraadt

repair acpiopen/acpiclose and friends to handle the /dev/apmctl interface,
so that apmd(8) can actually work correctly against acpi(4), and thus,
even do the fancy -C/-A stuff; also tested by jmc


# 1.109 02-Dec-2007 fgsch

fix the trailing space for real now.


# 1.108 02-Dec-2007 fgsch

avoid trailing spaces on output; marco@ ok.


# 1.107 27-Nov-2007 kettenis

Fix off-by-one that made us attach acpicpu0 and acpicpu1 on GENERIC kernels.

ok marco@


# 1.106 25-Nov-2007 jsg

When available and ACPI is enabled use the ACPI reset method
to reboot. This is required for some quirky sis machines
and likely others to reboot.

Tested by many. 'commit early commit often' deraadt@


# 1.105 19-Nov-2007 deraadt

sigh, amd64 still uses that stupid acpi_enabled variable...


# 1.104 19-Nov-2007 deraadt

softc's come initialized to 0, you know...


# 1.103 19-Nov-2007 deraadt

store revision; from jsg


# 1.102 19-Nov-2007 kettenis

Initialize sc_facs earlier.

ok deraadt@, beck@, canacar@


# 1.101 16-Nov-2007 deraadt

fix the bus_space #define nightmare, so that amd64 and i386 are much more
uniform. as a result shared code like acpi needs less #ifdef's
ok marco kettenis


# 1.100 15-Nov-2007 deraadt

remove the #ifdef ACPI_ENABLE. the EC codepath is either safe enough (and
will be improved), or it is unsafe because AML assumes it must be there
ok almost everyone


# 1.99 15-Nov-2007 mikeb

Make acpicpu(4) attach to the existing CPUs only.

Problem reported by form@. Fix idea by kettenis@.
Lots of help from deraadt@. Tested by deraadt@, form@ and me.

ok deraadt marco


# 1.98 14-Nov-2007 deraadt

spacing


# 1.97 12-Nov-2007 deraadt

correct : vs , in attach-time messages


# 1.96 06-Nov-2007 deraadt

all ACPI_ENABLE kernels need acpi_interrupt()


# 1.95 06-Nov-2007 deraadt

move acpi_read_pmreg() and acpi_write_pmreg() out of SMALL_KERNEL so
that acpi ramdisks can compile (obvious solution)


# 1.94 05-Nov-2007 canacar

Establish the acpi interrupt earlier. Fixes problems with some
GENERIC.MP kernels not getting acpi interrupts. This is a
workaround until the real issue with late establishment is fixed.
ok weingart@, beck@, gwk@


# 1.93 03-Nov-2007 jordan

Added support for displaying wakeup devices
ok beck@,weingart@,gwk@


# 1.92 11-Oct-2007 kettenis

When walking the tree yo find _PRT methods, check whether the device is
actually enabled and functioning. Similar to what we do for _INI.

ok weingart@, beck@


# 1.91 08-Oct-2007 krw

More simple memset(,0,) -> M_ZERO changes. In this batch move to
size(*p) as the first malloc() parameter where p is declared locally
and thus easy to check. Add M_ZERO to gpe_table allocation in acpi.c
even though there is no obvious bzero or memset nearby.


# 1.90 13-Sep-2007 mk

Remove unnecessary memset() (see aml_evalnode(9)) and a bit of KNF.

ok toby


# 1.89 13-Sep-2007 weingart

Implement ACPI 6.5.1 spec tree walk for _STA and _INI.
Fixes mk's laptop. No regressions so far (thank you to the
testers).

ok gwk@, mk@, marco@


Revision tags: OPENBSD_4_2_BASE
# 1.88 17-Apr-2007 mk

Now that acpidock no longer hangs machines during boot (at least
according to the reports I've been getting) it should be safe to enable
it again, so remove the #if 0 stuff around it.

ok marco


# 1.87 11-Apr-2007 jordan

Added changes for C-State
ok marco@


# 1.86 26-Mar-2007 marco

Calculate minutes left more accurately.

From giovanni <qgiovanni@gmail.com>


Revision tags: OPENBSD_4_1_BASE
# 1.85 03-Mar-2007 mk

Stop looking for acpidock until we fix that stuff that breaks all pre-60
series ThinkPads with acpidock(4) enabled.


# 1.84 22-Feb-2007 marco

We were getting no interrupts in .MP. There are 2 fixes; one in
acpi_machdep.c and move the interrupt establishment down after ioapic
has been initialized.

ok jordan


# 1.83 21-Feb-2007 kettenis

Make an acpi-enabled GENERIC.MP work on systems that only support PIC mode.

ok marco@


# 1.82 19-Feb-2007 jordan

Added acpi_map_address function for mapping GAS/IO registers
ok marco@


# 1.81 18-Feb-2007 jordan

Added dynamic perfect hash for opcodes
ok marco@


# 1.80 18-Feb-2007 marco

Shave off another K pre compression.
Shuffle SMALL_KERNEL functions around so that we only have a single #ifndef


# 1.79 17-Feb-2007 marco

Add SMALL_KERNEL throught acpi to make it fit on boot media.
ok deraadt


# 1.78 13-Feb-2007 marco

Add smarter _INI code. This helps with some dissapearing devices.

ok kettenis jordan


# 1.77 31-Jan-2007 gwk

Introduce acpi_hasprocfvs thats "ACPI Has Processor Frequency and Voltage
Scalling" a global flag set by acicpu attach to indicate that ACPI is
handling fvs through the _PSS objects. This will fix disappearing
EST/powernow when booting kernels with acpi enabled, as acpicpu isnt
compiled into GENERIC yet. Also in cases where acpicpu is built in and
legacy EST/powernow work but acpi is missing the _PSS object, the legacy
routines will be used.

tested by dim@, and Nick Nauwelaerts <nick AT nauwelaerts.net>
ok marco@, dim@


# 1.76 22-Jan-2007 mk

Actually look for docking stations by looking for _DCK nodes.

ok marco


# 1.75 02-Jan-2007 marco

On some systems EC must be attached as the first device. Move EC from
generic _HID devices to its own routine to force first attach. Bug found
and debugged by joris@

Code mostly written by joris@


# 1.74 26-Dec-2006 marco

Add polling to devices that require it such as acpibat and acpitz.
Use the same tsleep/wakeup mechanism as the interrupt code to keep it non-
concurrent.


# 1.73 21-Dec-2006 deraadt

even more knf love


# 1.72 21-Dec-2006 marco

Remove some leftover acpiec gpe/interrupt code.


# 1.71 20-Dec-2006 deraadt

oops, name already used


# 1.70 20-Dec-2006 deraadt

rename __acpi_enable_gpe(); ok marco


# 1.69 20-Dec-2006 deraadt

acpi_unmap_pmregs() is not used, ok marco


# 1.68 19-Dec-2006 deraadt

simplify some low-level methods substantially (shrinking code); ok marco jordan


# 1.67 06-Dec-2006 mk

malloc(..., M_NOWAIT) with no NULL check is bad. If allocation fails,
complain, clean up, and bail out of attach.

input and ok gwk


# 1.66 28-Nov-2006 jordan

Fixed clearing arg for acpi_init_pic
ok kettenis@


# 1.65 27-Nov-2006 kettenis

Don't spam dmesg.


# 1.64 27-Nov-2006 jordan

Fixes for acpiprt routing
Post-parse fixup for namerefs
ok kettenis@


# 1.63 25-Nov-2006 marco

Don't handle acpiev events inside the gpe handler since this causes deadlocks.

ok jordan@


# 1.62 15-Nov-2006 kettenis

Hook up acpi apic interrupt routing support to generic acpi code.

ok jordan@


# 1.61 02-Nov-2006 marco

Kill acpiec attach function and add its functionality to the generic _HID
device attach

prodded and ok jordan


# 1.60 02-Nov-2006 marco

Fix comments to reflect reality.


# 1.59 19-Oct-2006 marco

Replace ACPI ioctls with APM ioctls. This makes ACPI talk the same protocol
as APM therefore userspace can now retrieve power information in the same
fashion as all other apm arches (zaurus, macppc, i386 etc).

To use force the use of acpi device like so: apmd -f /dev/acpi

ok jordan@


# 1.58 19-Oct-2006 jordan

Fixed GPE debug levels
Fixed ref counter on temporary bufferfields
DSDT parsing fully working on IBM Thinkpad
ok marco@


# 1.57 15-Oct-2006 jordan

Removed old GPE handler code remnants
ok marco@


# 1.56 12-Oct-2006 jordan

Massive ACPI changes
Fixed ACPI leaks
new ACPI API
ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.55 30-Jun-2006 gwk

KNF, No functional change,
"go at it" marco@


# 1.54 30-Jun-2006 jordan

Support for calling Embedded Controller via new GPE interface
ok marco@


# 1.53 30-Jun-2006 jordan

Added new code for GPE handling
Also works with ACPIEC; need to fixup acpiec.c to remove original interrupt handler
ok marco@


# 1.52 30-Jun-2006 jordan

Added offset argument for acpi read/write pmreg routines (new GPE code)
ok marco@


# 1.51 31-May-2006 todd

do not call acpi_powerdown() unless acpi has successfully initialized
ok gwk@ canacar@


# 1.50 29-May-2006 canacar

Add embedded controller support.
ok marco@ deraadt@ testing & ok gwk@


# 1.49 19-May-2006 canacar

Minimal thermal zone driver. Monitors thermal zone temperature,
shuts down the system if the 'critical temperature' is reached.
ok marco@


# 1.48 11-Apr-2006 marco

KNF.


# 1.47 11-Apr-2006 gwk

Add acpi_resume(struct acpi_softc *sc), which calls some AML methods,
detected at attach time, currently unused.

ok marco@


# 1.46 11-Apr-2006 gwk

Discover some ACPI sleep related methods, and call them at the
appropriate time in acpi_enter_sleep_state().

ok marco@


Revision tags: OPENBSD_3_9_BASE
# 1.45 26-Feb-2006 marco

Add acpicpu as a device.

ok jordan@


# 1.44 23-Feb-2006 marco

remove dead function.

ok jordan@


# 1.43 22-Feb-2006 marco

Print out all tables it found during autoconf. We'll need this data in
the future.


# 1.42 22-Feb-2006 marco

Make dmesg less verbose.

ok jordan.


# 1.41 22-Feb-2006 jordan

Added parsing of SSDT tables
ok marco@


# 1.40 22-Feb-2006 jordan

Added new aml_register to notify by device id
Moved powerdown code to button device
ok marco@


# 1.39 21-Feb-2006 marco

Readjust debug levels that crept in too low over the last few days.
KNF


# 1.38 21-Feb-2006 marco

Rig the power button so that the machine shuts down correctly.


# 1.37 20-Feb-2006 marco

Add acpibtn device. Remains disabled for now.


# 1.36 19-Feb-2006 jordan

Added code for aml_notify GPE notification callback
ok marco@


# 1.35 19-Feb-2006 grange

Fix acpi_enter_sleep_state() by replacing current ugly hack
with mostly correct sleeping state transition code.

ok brad@ marco@


# 1.34 19-Feb-2006 marco

jordan:
Enable interrupts.
Added peripheral code to make this work.

marco:
Replace softintr and other types of interrupts with a handler with context.
Fixed interrupt magic for GPEs.
Some KNF

This is work done by jordan and I but has to be a single commit.


# 1.33 17-Feb-2006 marco

Remove earlier introduced delays. We really only need them during autconf.

ok jordan@


# 1.32 17-Feb-2006 marco

Add a generic delay function as per the ACPI spec.

from jordan@


# 1.31 17-Feb-2006 marco

Use tsleep instead of delay when !cold.


# 1.30 17-Feb-2006 marco

Argh, we need delays between reads/writes. The backend reads (i2c most likely)
are so slow that values come back as 0 sometimes. We have to do this
despite the spec saying we shouldnt have to. Guess having a faster
parser than others can hurt sometimes.

Idea from jordan.


# 1.29 17-Feb-2006 jordan

Fixed code for handling _INI
Rearranged debug statements
ok marco@


# 1.28 16-Feb-2006 jordan

Added stub for calling _INI
Fixed aml_dowhile for NULL return
ok marco@


# 1.27 16-Feb-2006 jordan

Fixed parser/evaluator to retrieve battery info+state
Cleanup of parser
Dell laptops return battery status
ok marco@


# 1.26 06-Feb-2006 brad

fix a typo so this will compile on amd64.


# 1.25 04-Feb-2006 marco

When ACPI_ENABLED wasn't set but the devices were it had an unknown variable.

Pointed out by ian@.


# 1.24 03-Feb-2006 jordan

Rewrote aml parser yet again...
Fixed bus_space_map for i386/amd64
Added _BST and _BIF to acpibat to get status/info
ok marco@


# 1.23 20-Jan-2006 jordan

More evaluator changes
Removed old evaluator code
Fixed _HID value
ok marco@


# 1.22 19-Jan-2006 jordan

Added support for read/write of PCI Config space
Help jason@
ok marco@


# 1.21 18-Jan-2006 jordan

Added new evaluation routines
Added helper methods for buffer/field evaluation
Fixed broken indentation on dsdt.c

ok marco@


# 1.20 17-Jan-2006 jordan

Major overhaul of the aml parser. Evaluater temporarily disabled.

ok marco@


# 1.19 06-Jan-2006 grange

Check for ACPI control availability only if ACPI_ENABLE defined.


# 1.18 06-Jan-2006 grange

Informative error messages on attach, prettier dmesg.


# 1.17 06-Jan-2006 grange

printf -> dnprintf conversion for debug messages.


# 1.16 05-Jan-2006 grange

Find SLP_TYPx values and store them for future use in
acpi_enter_sleep_state().


# 1.15 05-Jan-2006 grange

Random KNF and cleanup


# 1.14 05-Jan-2006 grange

Provide symbolic names for the acpi sleeping states and use it.


# 1.13 28-Dec-2005 marco

Moved DEVNAME to a acpidev.h

ok jordan@


# 1.12 18-Dec-2005 sturm

add back comment about wrong code

ok marco


# 1.11 16-Dec-2005 marco

Remove test for kbd_reset. Prompted deraadt@, ok jordan@


# 1.10 16-Dec-2005 jordan

Added hooks for powerdown/power button press


# 1.9 16-Dec-2005 jordan

Cleaned up common power management register i/o
Added enter sleep state ioctl
Hooked power button interrupt
ok marco@


# 1.8 16-Dec-2005 marco

Remove autoconf chatter


# 1.7 16-Dec-2005 jordan

Moved DSDT AML parsing into parent ACPI driver
ok marco@


# 1.6 14-Dec-2005 marco

Add AC device glue.


# 1.5 14-Dec-2005 marco

Adjust debug printing levels to something more sane.


# 1.4 13-Dec-2005 marco

Add glue to attach devices to apci.


# 1.3 07-Dec-2005 marco

KNF


Revision tags: OPENBSD_3_8_BASE
# 1.2 10-Jul-2005 grange

Spacing cleanup.


# 1.1 02-Jun-2005 tholo

Start on a basic ACPI framework -- does not do much more than read out the
ACPI tables into kernel memory and attach ACPI and HPET timers currently.

In order to test this code, enabling the devices in GENERIC as well as
the ACPI_ENABLE option is needed. This code does not do any thermal
control yet, so this should be done with care depending on the platform.

In the tree so more people can contribute to making this more fully
featured.

Ok niklas@ grange@ tedu@


# 1.420 15-Mar-2023 kettenis

Add missing nameref resolving for the _DEP() package elements that indicate
dependencies between ACPI devices.

ok dlg@, deraadt@


# 1.419 10-Feb-2023 visa

Adjust knote(9) API

Make knote(9) lock the knote list internally, and add knote_locked(9)
for the typical situation where the list is already locked.

Remove the KNOTE(9) macro to simplify the API.

Manual page OK jmc@
OK mpi@ mvs@


Revision tags: OPENBSD_7_2_BASE
# 1.418 13-Sep-2022 kettenis

Split out the code that collects data from acpiac(4), acpibat(4) and
acpisbs(4) for apm(4) and hook it up to the arm64 version of apm(4) on
systems with ACPI.

ok kn@


# 1.417 12-Sep-2022 kettenis

Add support for level-triggered GPIO events.

ok mlarkin@


# 1.416 10-Sep-2022 kettenis

Don't restrict GPIO events to pin numbers below 256. With the _EVT()
method events corresponding to larger numbers are possible.

Fixes lid state detection on the x13s.

ok patrick@


# 1.415 03-Sep-2022 kettenis

Make sure we don't suspend if we have no way to wake up the machine.
The idea is that device drivers that can wake up the machine register
themselves by calling device_register_wakeup(). To prevent regressions
on amd64, we let acpi(4) register itself as a wakeup device if the AML
for the machine defines wakeup devices. This may be refined in the
future.

This diff will prevent people from suspending their arm64 SBCs without
having a way to wake them up. For now the only driver that registers
itself is axppmic(4), which means that at this moment only SBCs with
an allwinner A64 or H5 SoC will actually support suspend/resume.

ok mlarkin@, deraadt@


# 1.414 10-Aug-2022 patrick

Provide the AML root in ACPI's softc so that drivers that need access
don't have to declare it using extern.

Suggested by and with feedback from kettenis@
Tested by deraadt@ on arm64, amd64 and i386
Tested by bluhm@ on amd64 and i386


Revision tags: OPENBSD_7_1_BASE
# 1.413 17-Feb-2022 jsg

reduce includes


# 1.412 10-Feb-2022 visa

Embed klist head in acpi_softc to avoid explicit malloc.

OK kettenis@


# 1.411 09-Feb-2022 deraadt

Move x86-only stubs used by kern/subr_suspend.c into dev/acpi/acpi_x86.c
for sharing between i386 and amd64.
ok mlarkin kettenis


# 1.410 08-Feb-2022 deraadt

The suspend/resume code is a sticky mess of MI, MD, and ACPI sequencing.
This splits out the MI sequencing, backing it with per-architecture helper
functions. Further steps will be neccesary because ACPI and MD are too
tightly coupled, but soon we'll be able to use this code for more architectures
(which depends on figuring out the lowest-level cpu sleeping method)
ok kettenis


# 1.409 04-Feb-2022 robert

set the APM_BATT_CHARGING state if the battery is being charged and try
to calculate the remaining time to be fully charged

ok jca@


# 1.408 02-Feb-2022 deraadt

acpi_addtask() calls malloc() w/ M_NOWAIT (because some calls come from
interrupt context), this however means occasional resource shortage will
result in callbacks registration failing, and unknown consequences for
the task-submitting caller.
Changing this to use pools with a low water mark, decreases the odds
of that problem occuring.
ok kettenis


# 1.407 01-Feb-2022 deraadt

GPE_DIRECT is calling the AML parser from interrupt context, which is
not permitted. Luckily nothing is using GPE_DIRECT anymore, so this code
can be deleted.
ok kettenis


# 1.406 26-Jan-2022 kettenis

An ACPI device needs to be both present and enabled for it to function.
So only attempt to attach hardware that has both bits enabled. This fixes
an issue where com(4) would attach for a disabled serial port leading to
misdetection of the hardware variant and a subsequent hang when /etc/rc
runs ttyflags -a.

ok anton@, deraadt@


# 1.405 12-Jan-2022 patrick

Make acpi_getpropint() return uint64_t, as ACPI integers are in fact that
wide and some _DSD properties depend on it.

ok kettenis@


# 1.404 04-Jan-2022 patrick

acpi_getprop() needs to actually make sure that we're looking at the
correct property. While there adjust acpi_getpropint() as well to
increase similarity with acpi_getprop().

ok kettenis@


# 1.403 16-Dec-2021 anton

Attach com over acpi on amd64. Some hardware uses a different interrupt
assignment compared to the the legacy one supported by com over isa.
This causes the console to halt once userland takes over as no
interrupts are received. The actual address and irq can be read from
ACPI, kettenis@ already added support for arm64 which paved the way for
amd64.

Some consoles that previously attached over isa are now expected to
attach over acpi.

Thanks to patrick@ for testing on arm64.

ok kettenis@


# 1.402 07-Dec-2021 kettenis

Fix parsing of SR_IRQ resource descriptors.

ok patrick@, anton@


# 1.401 02-Nov-2021 deraadt

knf


# 1.400 30-Oct-2021 deraadt

Change hw.perfpolicy=auto by default, at startup. If the system has AC
power connected (default is yes when no driver differentiates) then default
to 100% performance. On battery, use the existing auto algorithm (which is
admittedly somewhat unrefined).
This change overrides the system/BIOS speed and puts OpenBSD in control.
As this happens very early during boot, besides speedups in all usage usage
patterns, some surprises: unhibernate and sysupgrade times are cut in half.
note: on a few architectures, the setperf fn pointer is changed late, and
thus the auto algorithm stops timeing out. kettenis and i will look for
a solution.
in snaps for more than a week.
ok kettenis


Revision tags: OPENBSD_7_0_BASE
# 1.399 20-Jul-2021 mlarkin

Fix trailing whitespace in a few places.

No code change.


# 1.398 09-May-2021 kettenis

Some x86 machines advertise the "hardware reduced" ACPI feature, advertise
S4 and S5 support, but fail to populate the SLEEP_CONTROL_REG and
SLEEP_STATUS_REG descriptions in the FADT. An example of such a machine
is the ASUS Zenbook 14 UM433DA. Any attempt to powerdown the machine
will result in a kernel crash. It turns out that using the legacy ACPI PM
registers works fine on this machien. So fall back on those registers
if SLEEP_CONTROL_REG and/or SLEEP_STATUS_REG aren't provided.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_9_BASE
# 1.397 15-Mar-2021 patrick

Add acpi_iommu_device_map(), which replaces the DMA tag with one that
is blessed with IOMMU magic, if available. This is mainly for arm64,
since on amd64 and i386 the IOMMU only captures PCIe devices, as far
as I know, which uses the pci_probe_device_hook(). This though is for
non-PCI devices attached through ACPI.

ok kettenis@


# 1.396 07-Mar-2021 patrick

Pass standard DMA tag to acpi(4) table drivers.

ok kettenis@


# 1.395 27-Dec-2020 deraadt

acpi_map_address() cannot be wrapped by SMALL_KERNEL anymore, as
it is used by acpihpet.c


# 1.394 25-Dec-2020 visa

Refactor klist insertion and removal

Rename klist_{insert,remove}() to klist_{insert,remove}_locked().
These functions assume that the caller has locked the klist. The current
state of locking remains intact because the kernel lock is still used
with all klists.

Add new functions klist_insert() and klist_remove() that lock the klist
internally. This allows some code simplification.

OK mpi@


# 1.393 17-Dec-2020 kettenis

Fix some issues with referencing named ACPI nodes from Packages.
These references need to be resolved at runtime rather than when
they're parsed such that they pick up the right values for those nodes
which can be changed when for example _INI methods run.

The current approach is to replace these reference with a string that
names the node in question. The problem with that is that packages
can also contain normal strings. Which means that we need special
code that depends on the context where the package is used.

This diff takes a different approach by retaining a reference when
parsing. Code that uses a package will need to resolve this reference
but there is no ambiguiety anymore.

ok patrick@


# 1.392 05-Dec-2020 kettenis

Add support for the _EVT method to the GPIO support code.

ok patrick@


Revision tags: OPENBSD_6_8_BASE
# 1.391 27-Aug-2020 jmatthew

Reorder the acpi attach process so we look at Processor() CPU nodes
after Device() ones, since we should prefer the newer node type.
If we see any Device() nodes, don't attach acpicpu(4) to any Processor()
nodes if they're also present. This also makes acpitz(4) and
acpipwrres(4) devices attach slightly later.

ok kettenis@, also tested by jmc@


# 1.390 16-Aug-2020 gnezdo

Remove sc_maxgpe from acpi_softc which was never read from

Remove a dead conditional from acpi_init_gpes while there.

ok jcs@


# 1.389 21-Jul-2020 deraadt

acpi can use IPL_BIO (a low interrupt) since it only enqueues operations for
later processing. The use of a high interrupt will predate suspend/resume
efforts, we had to redesign acpi to be non-reentrant obviously
discussed with kettenis, in snaps for more than a week


# 1.388 24-Jun-2020 cheloha

kernel: use gettime(9)/getuptime(9) in lieu of time_second(9)/time_uptime(9)

time_second(9) and time_uptime(9) are widely used in the kernel to
quickly get the system UTC or system uptime as a time_t. However,
time_t is 64-bit everywhere, so it is not generally safe to use them
on 32-bit platforms: you have a split-read problem if your hardware
cannot perform atomic 64-bit reads.

This patch replaces time_second(9) with gettime(9), a safer successor
interface, throughout the kernel. Similarly, time_uptime(9) is replaced
with getuptime(9).

There is a performance cost on 32-bit platforms in exchange for
eliminating the split-read problem: instead of two register reads you
now have a lockless read loop to pull the values from the timehands.
This is really not *too* bad in the grand scheme of things, but
compared to what we were doing before it is several times slower.

There is no performance cost on 64-bit (__LP64__) platforms.

With input from visa@, dlg@, and tedu@.

Several bugs squashed by visa@.

ok kettenis@


# 1.387 10-Jun-2020 jca

Give userland a consistent battery and A/C state after resume

Previously we refreshed the state of acpiac(4) and acpibat(4)/acpisbs(4)
by queueing fake ACPI events to be processed by the acpi thread.
acpibat(4) using ACPIDEV_POLL meant that its status could be refreshed
first, resulting in an APM_POWER_CHANGE event sent to userland before
the state of acpiac(4) was up to date. Because of this, apmd(8) could
see A/C unplugged when it was in fact plugged, possibly leading to
a wrong decision.

Instead of using fake ACPI events, refresh the status of acpiac(4) and
acpibat(4)/acpisbs(4) right away at DVACT_WAKEUP time. To reuse the
existing refresh code of those drivers, make their ACPI *_notify()
handlers responsible for sending APM_POWER_CHANGE events instead. Also
queue an APM_POWER_CHANGE event right after resume to retain existing
behavior.

This fixes the consistency of data shown to userland, and makes those
drivers less special.

Initially discussed last year (!) with mpi@ at Miod's place.
Feedback and ok jcs@, deraadt@ agrees with the use of DVACT_WAKEUP here.


# 1.386 29-May-2020 deraadt

dev/rndvar.h no longer has statistical interfaces (removed during various
conversion steps). it only contains kernel prototypes for 4 interfaces,
all of which legitimately belong in sys/systm.h, which are already included
by all enqueue_randomness() users.


# 1.385 14-May-2020 kettenis

Bring back the acpi(4) changes. This time around only use the ACPI _CRS
information on ACPI 5.0 and later.

ok krw@, patrick@


# 1.384 11-May-2020 jca

More accurate remaining power reporting with disparate batteries

Overall remaining power was computed as the average of the remaining
power of each battery, in percents. This doesn't work with batteries of
different capacity, so use the total capacity and remaining power
instead.

Reported by lidstah, successful tests by lidstah and benno@, ok kettenis@


# 1.383 08-May-2020 kettenis

Move parsing of _CRS into acpi(4) and pass the parsed address and interrupt
information in struct acpi_attach_args.

ok mpi@


Revision tags: OPENBSD_6_7_BASE
# 1.382 14-Apr-2020 kettenis

Implement acpi_getprop(), the ACPI equivalent of OF_getprop().

ok patrick@


# 1.381 12-Apr-2020 kettenis

Add support for the _CCA method. This method indicates whether DMA is
cache-coherent or not. To implement this, acpi(4) gets two bus_dma tags
and passes the appropriate one when attaching devices based on _CCA.
On i386/amd64, where for all practical purpose DMA is always cache-coherent,
the two tags are the same. But on arm64 they are distinct.

ok patrick@


# 1.380 07-Apr-2020 visa

Abstract the head of knote lists. This allows extending the lists,
for example, with locking assertions.

OK mpi@, anton@


# 1.379 03-Apr-2020 mpi

Fix incoherencies of errors returned by various *kqfilter routines.

EOPNOTSUPP is returned when there's no "filterops" for a given fd.
EINVAL when the requested filter isn't supported by the "filterops".
ENXIO means the underlying device is no longer valid/present.

ok visa@


# 1.378 20-Feb-2020 visa

Replace field f_isfd with field f_flags in struct filterops to allow
adding more filter properties without cluttering the struct.

OK mpi@, anton@


# 1.377 31-Dec-2019 visa

Use C99 designated initializers with struct filterops. In addition,
make the structs const so that the data are put in .rodata.

OK mpi@, deraadt@, anton@, bluhm@


# 1.376 31-Dec-2019 mpi

Convert infinite sleeps to {m,t}sleep_nsec(9).

ok kn@


# 1.375 31-Dec-2019 jsg

include sys/sensors.h for sensor use
don't depend on indirect inclusion from acpidev.h


Revision tags: OPENBSD_6_6_BASE
# 1.374 07-Sep-2019 kettenis

Revert acpipci(4) changes. Too many systems are broken. We'll try again
after 6.6 as been released. The acpireg.h change stays behind.


# 1.373 28-Aug-2019 kettenis

Use ACPI information to attach PCI busses like we do on arm64. There are a
few additional quirks though, and attaching the PCI busses is delayed to
replicate the existing code more closely. That may be changed in the
future. Also tweak how we handle MSI support and respect to ACPI flag
that says we shouldn't attempt to use MSIs.

Some fallout is expected.

ok patrick@


# 1.372 27-Aug-2019 deraadt

On amd64 the bootup and resume paths for SP/BP/SP were not performing
msr/pat, clock-startup, firmware upgrade, fpu initialization, and cpu
soft-state initialization in the same order. That kind of chaos is
crazy and fragile. Improve the acpi resume call sequence and unify order
of the operations.


# 1.371 02-Jul-2019 jcs

acpi_set_gpehandler: turn edge arg into flags, add support for
passing GPE_DIRECT which directly executes the supplied callback and
clears a level interrupt rather than going through acpi_taskq which
can be slow when other driver callbacks are in the queue.

This should not be used for GPE handlers that need to do anything
ACPI-related.

ok kettenis


# 1.370 10-Jun-2019 kettenis

Print proper ACPI version number.

ok mlarkin@


# 1.369 08-Jun-2019 kettenis

When establishing the mapping between ACPI device nodes and PCI devices,
skip PCI host bridges and devices that aren't there.

ok patrick@


# 1.368 07-Jun-2019 kettenis

Introduce acpi_getsta() and use it to eliminate some duplicated code.

ok patrick@


# 1.367 12-May-2019 kettenis

Make sure the extended PM register descriptions actually exist before
using them. Fixes machines with ancient ACPI 1.0 (such as recent
hypervisors).

ok deraadt@


# 1.366 12-May-2019 kettenis

Add support for mapping ACPI PM registers using the "extended" gasio
representation from the FADT. Mostly fixes the Lanner NCA-1510.

ok lteo@, mlarkin@, deraadt@


# 1.365 11-May-2019 lteo

Fix incorrect error message.

ok kettenis@


# 1.364 08-May-2019 tedu

free size. the allocation is the header plus the length.


Revision tags: OPENBSD_6_5_BASE
# 1.363 04-Apr-2019 kettenis

Avoid potential null-pointer dereference. Found by Kent R. Spillner.

ok deraadt@, jsg@


# 1.362 20-Jan-2019 jsg

fix SMALL_KERNEL build after changes in rev 1.361
reported by naddy@ ok deraadt@


# 1.361 19-Jan-2019 tedu

Add a pwraction sysctl that controls what the power button does on acpi.
By default, nothing changes -- shutdown is initiated. But allows turning
power button into a sleep button if desired.
(grudging) ok from a few parties


# 1.360 26-Oct-2018 kettenis

Add acpipci(4) on amd64. For now this only calls the PCI-specific _OSC
method to let the ACPI implementation know what features we support.


Revision tags: OPENBSD_6_4_BASE
# 1.359 30-Aug-2018 kettenis

Move softbutton check before the check that skips attaching certain acpi
devices. Fixes regression caused by matching on _CID in addition to
matching on _HID.

ok matthieu@, mlarkin@


# 1.358 25-Aug-2018 kettenis

Match ACPI devices based on _CID if no match for _HID is found.

ok mpi@


# 1.357 19-Aug-2018 kettenis

Add support for multiple PCI segments. Only really implemented for arm64
for now as amd64/i386 firmware still caters for legacy OSes that only
support a single PCI segment.

ok patrick@


# 1.356 03-Aug-2018 kettenis

Let ahci(4) match on _CLS instead of _HID when attaching at acpi(4). Avoids
having to add many more _HID entries to the match table.

ok deraadt@, mlarkin@


# 1.355 10-Jul-2018 kettenis

Make legacy interrupts work in acpipci(4).

ok patrick@


# 1.354 05-Jul-2018 kettenis

Add acpipci(4), a driver that supports generic ECAM-compatible PCI host
bridges based on information provided by ACPI.

ok mlarkin@


# 1.353 04-Jul-2018 kettenis

Properly pass around the PCI "chipset tag" in acpi(4) and refactor
acpimcfg(4) to call an MD initialization functions that sets up a tag for
PCI ECAM.

ok guenther@, mlarkin@, krw@


# 1.352 01-Jul-2018 kettenis

Move acpi(4) attach glue into acpi_machdep.c.

ok guenther@, deraadt@


# 1.351 01-Jul-2018 kettenis

Add support for _DSD Device Properties.

ok mlarkin@


# 1.350 30-Jun-2018 kettenis

Add intr_enable() function, intended for MI use to amd64 and i386 and use
this in the acpi(4) suspend/resume code paths.

ok deraadt@


# 1.349 29-Jun-2018 kettenis

Use <stdint.h> types.

ok mpi@, jung@, krw@, deraadt@


# 1.348 29-Jun-2018 mlarkin

ACPI: Allow (with warning) GPE handler reassignment, instead of returning
a failure code.

ok kettenis


# 1.347 26-Jun-2018 mlarkin

knf


# 1.346 26-Jun-2018 kettenis

Let acpi(4) pass down the bus dma tag.

ok deraadt@


# 1.345 25-Jun-2018 kettenis

Reorganize acpi(4) code a bit in preparation for upcoming arm64 support.

ok deraadt@


# 1.344 20-May-2018 kettenis

Wake up the acpi thread after scheduling a gpio event.

ok mpi@


# 1.343 17-May-2018 kettenis

Sprinkle some #ifndef SMALL_KERNEL such that RAMDISK kernels cuild again.

pointed out by stsp@


# 1.342 17-May-2018 kettenis

Implement GenericSerialBus OpRegion support.

ok mlarkin@


# 1.341 27-Mar-2018 kettenis

Add acpicmos(4), a driver that implements SystemCMOS OperationRegion
access support. This fixes machines where the AML doesn't check whether
support for this OperationRegion type has been registered by the OS.

ok mlarkin@


Revision tags: OPENBSD_6_3_BASE
# 1.340 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.339 10-Feb-2018 deraadt

Syncronize filesystems to disk when suspending. Each mountpoint's vnodes
are pushed to disk. Dangling vnodes (unlinked files still in use) and
vnodes undergoing change by long-running syscalls are identified -- and
such filesystems are marked dirty on-disk while we are suspended (in case
power is lost, a fsck will be required). Filesystems without dangling or
busy vnodes are marked clean, resulting in faster boots following
"battery died" circumstances.
Tested by numerous developers, thanks for the feedback.


# 1.338 08-Feb-2018 deraadt

After flushing disks, try again to release all memory. We don't need to
save dirty memory to the hibernate space.


# 1.337 08-Feb-2018 deraadt

Toss all releaseable memory, because fragmentation can get in the way
of allocating the hibernate playpen.


# 1.336 08-Feb-2018 deraadt

sr_quiesce() is a new approach for ensuring that softraid drains
output to the disks.

This is part of a larger suspend/resume filesystem-safety diff, which
has been worked on for a couple of months already. Tests by job, krw,
beck, benno, and others. Sometimes even by snapshot users...


# 1.335 29-Nov-2017 kettenis

Add support for GPIO-signaled events.

ok mlarkin@


# 1.334 16-Nov-2017 jcs

Add PCI attachment for dwiic(4) needed by Intel 100 Series machines

ok kettenis


# 1.333 14-Nov-2017 kettenis

Add a few more PNP IDs to the skip lists.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.332 17-Aug-2017 stsp

Make dwiic(4) attach its ACPI parent dependencies.
This change makes the Asus E200HA keyboard work.

Original analysis and patch by Cesare Gargano
ok kettenis@


# 1.331 09-Aug-2017 dcoppa

Add "PNP0303" (8042 PS/2 Controller) to acpi_isa_hids[]

ok kettenis@


# 1.330 22-Jul-2017 jcs

acpibat: look for _BIX first before falling back on _BIF

ACPI 4.0 deprecated _BIF for battery status so some newer machines
are exporting _BIX which extends _BIF's package with a few extra
fields. When using _BIX, export the new discharge cycle count as a
sensor.

feedback from kettenis and anton
ok deraadt


# 1.329 20-Jul-2017 kettenis

Start a list of isa(4) device names such that we can avoid printing them
as "not configured".

ok mikeb@, millert@, deraadt@


# 1.328 08-Apr-2017 deraadt

Sensors are run as callbacks inside tasks. During suspend a sensor
could be running inside a driver that will be force-detached, or due
to tsleep end up disrupting the softstate/hardstate contract. At
suspend time, quisce all these callbacks by waiting for completion.
This issue has never been observed for real, but may be implicated in
suspend/resume failures.
ok kettenis guenther mlarkin


# 1.327 08-Apr-2017 deraadt

Two prototypes for acpi_maptable; merge to one place


Revision tags: OPENBSD_6_1_BASE
# 1.326 27-Mar-2017 deraadt

Now that hibernate_alloc() only has clean success/failure, don't
need to call hibernate_free() to clean up a partial mess.
ok mlarkin kettenis


# 1.325 13-Mar-2017 deraadt

allow ramdisks to build; ok jcs


# 1.324 12-Mar-2017 jcs

try to attach acpisbs first and if successful, don't attach acpibat


# 1.323 02-Mar-2017 natano

Add a new sysctl machdep.lidaction. The sysctl works as follows:

machdep.lidaction=0 # do nothing
machdep.lidaction=1 # suspend
machdep.lidaction=2 # hibernate

lidsuspend is just an alias for lidaction, so if you change one, the
other one will have the same value. The plan is to remove
machdep.lidsuspend eventually when people have upgraded their
/ets/sysctl.conf.

discussed with deraadt, who came up with the new MIB name
no objections mlarkin
ok stsp halex jcs


# 1.322 28-Feb-2017 natano

Untangle abstract sleep modes from ACPI sleep states. This paves the way
for further work in the area.

idea and ok deraadt
ok mlarkin


# 1.321 22-Feb-2017 jcs

add acpisbs, an acpi smart battery subsystem driver reading data
over smbus

currently disabled because it conflicts with acpibat

ok deraadt, kettenis


# 1.320 14-Jan-2017 kettenis

Revert aml_rdpciaddr changes; breaks several machines that were working
before.


# 1.319 11-Jan-2017 jsg

Increase the size of the buffers used for _HID and _CID strings to be
able to fit "Hyper_V_Gen_Counter_V1".

ok mlarkin@ deraadt@


# 1.318 08-Jan-2017 kettenis

Make aml_rdpciaddr() work for for devices behind bridges. The code now
uses the mapping between AML nodes and PCI devices that we establish.
Because _INIT methods may end up calling aml_rdpciaddr(), make sure we
create that mapping early. Also handle devices that aren't actually present.
These devices are now included in the mapping and reads will return an
all-ones patterm whereas writes are a no-op..

ok mlarkin@


# 1.317 25-Oct-2016 pirofti

Add the resource index to aml_parse_resource() callback function.

This is needed by an upcoming acpiec commit that handles machines breaking
the current ACPI specifications.

Change suggested by and ok kettenis@, guenther@


# 1.316 18-Sep-2016 guenther

If the APIC table (MADT) isn't present then ci_acpi_proc_id won't be set
when acpicpu tries to attach, leading to a NULL dereference. The ACPI
processor object on the Dells where this was reported is bogus and useless
anyway so apply the same test when deciding whether to attach to processor
objects as acpicpu would when matching them to cpus.

Problem reported by Eivind Eide (xenofil (at) gmail.com) and
Johan Huldtgren (johan+openbsd-bugs (at) huldtgren.com).
Testing by Eivind Eide.
ok kettenis@ mlarkin@


# 1.315 03-Sep-2016 naddy

Write the system time back to the RTC every 30 minutes.
This fixes the problem that long-running machines which were not
shut down properly would reboot with a badly offset system time.

hints and ok kettenis@


# 1.314 31-Aug-2016 mlarkin

Don't put a device in the wake device list if _STA says it's not there.

Fixes at least one machine that woke up from sleep / poweroff immediately.

ok kettenis@, tested by kettenis and martijn@


# 1.313 28-Jul-2016 kettenis

Store the acpi processor ID/UID in struct cpu_info, and use it to attach
acpicpu(4) drivers to the right cpu(4).

ok mlarkin@, guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.312 10-Jun-2016 kettenis

The power management timer has been made optional in ACPI 5.0A, so don't
attempt to attach acpitimer(4) if the timer isn't present.

Pointed out by jcs@

ok jcs@


# 1.311 08-May-2016 kettenis

Make sure devices listed by a device's _DEP method are attached before
we attach the device itself. The _DEP methode, introduced in ACPI 5.0,
is a hint that the device in question depends on OpRegion support from
the devices listed by _DEP, so we have to order them the proper way.
To prevent us from attaching those devices again when we encounter them
later walking down the device tree, keep track of our attempts to
attach a device driver to them using aflag in the aml_node structure.

ok guenther@


# 1.310 25-Apr-2016 pirofti

We attach acpihpet(4) and acpiec(4) without using the HID, so do not
report it in the dmesg.

OK deraadt@, mlarkin@.


# 1.309 02-Apr-2016 kettenis

Reduce dmesg spam by knocking out some acpi hardware IDs that don't need a
driver.

ok mpi@


# 1.308 29-Mar-2016 kettenis

Check if a device is present (using _STA) in acpi_foundhid. This prevents
us from reporting devices as "not configured" that aren't actually present,
and allows us to remove duplicated code from several drivers.

ok deraadt@, ok jsg@


# 1.307 29-Mar-2016 kettenis

Implement a cleaner way to prevent acpivout(4) from attaching if we have
a platform-specific method of controlling the backlight.

ok millert@, mlarkin@, jung@


# 1.306 28-Mar-2016 kettenis

Adding each and every new acpi device driver to acpi_foundhid() is
gettinga bit of the burden. And it really isn't how our autoconf
framework is supposed to work for busses that can be enumerated. All
the drivers already check for a matching _HID in their attach
function. So we can just drop the checks in acpi_foundhid() and let
the drivers sort themselves out.

Print a "not configured" line for devices for which we don't attach adriver.
This may be a bit much as there are devices for which we will not have a
driver. This will be removed if it causes too much dmesg spam.

ok deraadt@, mlarkin@


Revision tags: OPENBSD_5_9_BASE
# 1.305 17-Jan-2016 jsg

Take acpi_foundhid() outside of #ifndef SMALL_KERNEL so the ramdisk
kernel can find and attach dwiic(4) and sdhc(4).


# 1.304 17-Jan-2016 jsg

remove an unused global variable


# 1.303 14-Jan-2016 kettenis

Attach dwiic(4) on the Intel Bay Trail i2c controllers as well.


# 1.302 12-Jan-2016 kettenis

Improve emulation of PM registers on Hardware-redcued ACPI by emulating
SPL_TYPx, SLP_EN and WAK_STS using the new SLEEP_STATUS_REG and
SLEEP_CONTROL_REG registers. Makes the transition into S5 (powerdown) work
on the ASUS X205TA.

ok deraadt@, mlarkin@


# 1.301 12-Jan-2016 jcs

Add dwiic, a driver for the Synopsys DesignWare i2c controller found
on the Samsung ATIV Book 9 laptop. This initial version only
supports ACPI config/attachment.

Add ihidev, a HID-over-i2c driver largely based on uhidev. dwiic
handles attaching ihidev devices found in ACPI.

Add ims, a HID-over-i2c mouse/trackpad driver to get basic cursor
and button functionality from HID-compliant i2c trackpads.

ok kettenis deraadt


# 1.300 11-Jan-2016 kettenis

Make sdhc(4) attach to hardware IDs "80860F14" and "PNP0FFF".


# 1.299 09-Jan-2016 kettenis

Initial support for Hardware-Reduced ACPI. This skips the legacy mode checks
by emulating the PM_CNT register such that SCI_EN is always enabled. It also
skip attaching acpitimer(4) since the relevant registers are no longer there.

ok deraadt@, mlarkin@


# 1.298 06-Jan-2016 kettenis

Follow the spec and check the SCI_EN bit to see if ACPI has already
been enabled and don't bother enabling it again if it is. This should fix
systems that don't implement legacy mode and therefore set ACPI_ENABLE and
ACPI_DISABLE to zero.

ok mlarkin@, yasuoka@


# 1.297 23-Nov-2015 reyk

Back out the CRITICAL change, turn it back from 10% to 25%. Some
people with old/broken batteries didn't like this change, and the
underlying heuristics are wrong.


# 1.296 22-Nov-2015 reyk

The "CRITICAL" APM battery level, that is exported/faked by the ACPI
stack, is currently set to 25%. With 25% battery left, a typical
modern laptop will have at least one hour runtime left. It is an
arbitrary value, and other APM power layers in the kernel set CRITICAL
to something like 5% or 10%, so set it to 10% for now.

OK mlarkin@


# 1.295 28-Sep-2015 deraadt

In low-level suspend routines, set cold=2. In tsleep(), use this to
spit out a ddb trace to console. This should allow us to find suspend
or resume routines which break the rules. It depends on the console
output function being non-sleeping.... but that's another codepath which
should try to be safe when cold is set.
ok kettenis


# 1.294 06-Sep-2015 deraadt

clarify malloc arguments (base + length...)


# 1.293 28-Aug-2015 deraadt

fairly simple sizes for free(); ok tedu


# 1.292 20-Aug-2015 kettenis

Respect the access size when reading or writing to pci config space.
This version also makes sure writes are properly aligned, preventing
the panics seen with the previous incarnation of this change.

Fixes battery status passthrough in vmware, brightness keys on my x220
and who knows what else.

ok mlarkin@, deraadt@


Revision tags: OPENBSD_5_8_BASE
# 1.291 04-Aug-2015 deraadt

If we are setting ACPI_PM1_BM_RLD do it only once, and remember to
repeat this action at resume time.
ok mlarkin guenther


# 1.290 29-Jul-2015 deraadt

Something subtle in previous revision is locking up suspend or booting
on some laptops. Mailing lists threads mention x220 and Asus machines.
Reverting for now.


# 1.289 21-Jul-2015 kettenis

iRespect the access size when reading or writing to pci config space.
Fixes battery status passthrough in vmware and who knows what else.

Tested by many.

ok mlarkin@, deraadt@


# 1.288 17-Jul-2015 mlarkin

fix comments


# 1.287 26-Mar-2015 jsg

Use the 0x10 extended capability to determine if the trackstick
buttons are wired to the trackpad and need to be re-routed to the
trackstick. Without this change the buttons on 2015 thinkpads
get picked up as extended buttons that show up as scroll up/down.

Remove the X1 Carbon 2015 (LEN0048) and X250 (LEN0046) from the
top button area/soft buttons quirks list. Also avoid using the
quirk list entirely if the capability bit is set.

Based on work from jcs@ with feedback from mpi@
ok mpi@ jcs@


# 1.286 20-Mar-2015 kettenis

The ACPI standard says that a PCI function number of FFFF can be used to refer
to all functions on a device. This makes no sense in the context of accessing
PCI config space. Nevertheless stsp@ has a machine with AML that tries to
do config space reads from a device where _ADR encodes function number FFFF.
This is almost certainly buggy AML, but since Linux (and presumably Windows)
boot just fine on this machine we need to work around this. Linux effectively
treats function FFFF as function 7, which isn't present on this machine.
Instead we simulate a read from a non-existent device by returning a bit
pattern of all ones. Writes to function FFFF will still panic.

ok mlarkin@


# 1.285 16-Mar-2015 deraadt

Reduce use of SMALL_KERNEL in acpi, bringing acpiec driver and more gpe
handling into RAMDISK. This is now possible because the install media
has ample room. The goal is to reduce special cases where we may be
skipping (unknown) important operations...
ok mlarkin kettenis


# 1.284 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.283 07-Feb-2015 deraadt

New framework that allows hibernate to pass in entropy from it's fresh
boot.
ok mlarkin


# 1.282 06-Feb-2015 mlarkin

Fix a hibernate crash on some machines due to unmapping a page that
may not have been mapped previously (in the failure to hibernate case).

Also ensure that the lowmem ptp is mapped in all cases (not just MP).

ok kettenis


# 1.281 17-Jan-2015 deraadt

resume_randomness() before spinning up other cpus...


# 1.280 15-Jan-2015 jsg

Define a new wscons mouse type for Synaptics clickpad devices that lack
physical buttons. This will be used if the acpi pnpid for the mouse
matches a list provided by Synaptics found in Linux.

Combined with the xenocara changes this will remove the need for an xorg
config file for the x240/t440/t540 generation of thinkpads.

ok matthieu@ shadchin@ kettenis@


# 1.279 11-Jan-2015 kettenis

Tolerate checksum errors in ACPI tables (but complain about them). Apparently
there are machines out there with broken BIOSen with such checksum errors in
thier RSDT tables. Windows runs fine on these machines, so it probably does
not check. Linux ignores checksum errors as well (and prints a warning).

ok deraadt@, krw@


# 1.278 18-Dec-2014 deraadt

suspend and resume the RNG (at what looks like the right spots)
ok reyk djm


# 1.277 09-Dec-2014 doug

Replace some malloc(n*size,...) calls with mallocarray().

ok tedu@ deraadt@


# 1.276 06-Dec-2014 mpi

If "machdep.lidsuspend" is not set, do not go back to sleep if the lid
is still closed when resuming. This allows docked laptop to be resumed
without opening them.

ok mlarkin@


# 1.275 23-Nov-2014 mlarkin

If a machine has no _LIDs, then none can be open. This diff fixes a problem
where machines without any _LIDs were going back to sleep after hibernate
due to the recent suspend-if-lid-closed-on-resume diff. Noticed on qemu,
which doesn't have any _LIDs.

discussed with deraadt


# 1.274 18-Nov-2014 krw

Nuke yet more obvious #include duplications.

ok miod@


# 1.273 08-Nov-2014 mlarkin

If resuming from sleep (zzz/ZZZ) and the lid is still closed, go back to
sleep. This prevents accidental lid flex or slight opening in a backpack
from waking the machine up and leaving it resumed (powered on).

ok deraadt@


# 1.272 26-Sep-2014 kettenis

Rework piglet and pig allocation. Currently the piglet gets allocated
deep down in the suspend path, where it is really hard to recover from
allocation failure. So allocate the piglet early on in the suspend path.
Also change the piglet and piglet allocation functions to use km_alloc(9)
instead of doing pmemrange magic. This removes a bunch of code which, in the
case of the piglet allocation, is broken since it results in a NULL pointer
dereference. Also switch the piglet allocation to not wait. If we can't
allocate 16MB of phys contig memory on a halfway modern machine we're almost
certainly under a lot of memory pressure and we're better off not trying to
hibernate anyway.

ok mlarkin@


# 1.271 25-Sep-2014 kettenis

Bring back rev 1.249. Now that mp_setperf() has been fixed, it should be
safe to restore perflevel when resuming. Should fix the issues people
see with the processor running at full speed heating up the machine while
the apm command reports that the CPUs are running at a low speed.

ok dcoppa@, deraadt@


# 1.270 23-Sep-2014 kettenis

Release the acpi lock around calls to wsdisplay_suspend() and
wsdisplay_resume(). The X server may adjust the display brightness just
before/after the VT switch when suspending/resuming. In fact, it may
already be blocked adjusting the display brightness for some reason
unrelated to the suspend request.

The VT switch request will eventually time out, but the delay is annoying
and the failed VT switch will lead to problems after resume.

Needed for a future update of the xf86-video-intel driver.

ok mlarkin@


# 1.269 19-Sep-2014 kettenis

Use config_suspend_all(9).


# 1.268 08-Sep-2014 kettenis

Clear the PME Status bit on PCI devices in response to Device Wake events.
Fixes the interrupt storm on the 2009 Mac mini with WOL enabled on its
nfe(4) interface.

ok mlarkin@


Revision tags: OPENBSD_5_6_BASE
# 1.267 20-Jul-2014 mlarkin

Support hibernating to softraid crypto volumes.

much help and ok from deraadt@


# 1.266 16-Jul-2014 mlarkin

Reenable hibernate RLE support and flush+zero all memory after unpack.


# 1.265 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.264 12-Jul-2014 kettenis

Add a function to drop all clean pages on the page daemon queues and call
it when we hibernate.

ok mlarkin@, miod@, deraadt@


# 1.263 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.262 11-Jul-2014 mlarkin

Flush the buffercache to 16MB on hibernate and restore its previous max
size (kern.bufcachepercent) on resume, for better hibernate performance.

ok beck@


# 1.261 10-Jul-2014 blambert

remove unneeded header

ok mlarkin@


# 1.260 01-Jul-2014 kettenis

Adding an additonal delay after each access to SystemMemory or SystemIO
during autoconf makes no sense.

ok deraadt@


# 1.259 23-Jun-2014 kettenis

SystemMemory is used to access memory mapped registers on some machines, so we
must use the bus_space(9) API to access it instead of memcpy(9). Also make
sure we properly align access to fields that need more than byte alignment.

ok guenther@


# 1.258 21-May-2014 mlarkin

Fix invalid / missing format specifiers in several dnprintf statements
(used only when ACPI_DEBUG is enabled)

From a diff on tech@ by Fabian Raetz, with a minor change by me, tested
on i386 and amd64 with ACPI_DEBUG enabled and disabled.


# 1.257 25-Apr-2014 mlarkin

Defer the free of the hibernate structures until later, when it is safe to
do so.

Freeing the hibernate structures too early leads to inadvertent calls into
splx when we aren't ready for them yet. Found when trapping splx calls in
the resume path.

discussed with deraadt@ at m2k14


# 1.256 13-Apr-2014 mlarkin

Fix a badly worded debug message that implied the entire machine was going
to sleep, instead of just the acpi thread going to sleep.


# 1.255 31-Mar-2014 mpi

Change the order of operations during a suspend/resume cycle and
call bufq_quiesce() after executing the DVACT_QUIESCE handlers.

This should be safe since no disk nor controller drivers have such
handler but it will allow us to detach sd(4) devices attached to a
USB bus.

Another benefit pointed out by kettenis@ is that drivers that need
to read a firmware from the disk should be able to do it at resume
time in a DVACT_WAKEUP handler.

ok kettenis@, deraadt@


# 1.254 13-Mar-2014 dlg

get rid of the assumption that the head of the alldevs list is the
"mainbus" device. this breaks when mpath is enabled because it
attaches before mainbus and therefore takes the head position.

have autoconf provide device_mainbus() which looks up mainbus_cd,
and use that instead.

discussed with deraadt who just wants mpath stuff to move forward
despite there being many ways to shine this particular turd.


Revision tags: OPENBSD_5_5_BASE
# 1.253 21-Feb-2014 deraadt

Do the lid control further on in the resume sequence, since this is
AML, and will do WAITOK operations. While cold, that is a bad idea.
We seem to be safe with respect to the spec, I do not see any event
debounce issues here.
ok kettenis, tested by many


# 1.252 20-Jan-2014 claudio

Instead of checking sc->sc_revision <= 1 check that the
fadt->hdr_revision <= 1. This is a better check since it checks
the version of the proper descriptor. This allows the alix APU
to reboot since those boards have currently a wrong sc->sc_revision.
Committing this now so that it can be tested easier.
With and OK jsg@ also fine by derradt@


# 1.251 24-Dec-2013 kettenis

If the FADT has its SMI_CMD set to zero, assume we're only ACPI-only hardware
and don't need to disable SMI ownership of the ACPI hardware registers.

ok mlarkin@


# 1.250 23-Dec-2013 kettenis

Revert previous commit. It makes resume hang on some MP systems running
GENERIC.MP.


# 1.249 16-Dec-2013 kettenis

Restore hw.setperf upon resume like we do for apm(4) on i386. Seems we need
to do this fairly late in the resume to avoid hangs.

ok deraadt@, mlarkin@


# 1.248 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


# 1.247 06-Nov-2013 mpi

Add support for Power Resources for Dx states and the necessary hook
for PCI devices. This hook should be called twice, before and after
changing the power state of a PCI device.

Before setting the device to the new state, the ACPI layer will notify
every power resources linked to the device for that state and make sure
they are turned "_ON". After changing the state of the device, it will
decrement the reference of every power resources linked to that device
for the old state and turn them "_OFF" if they are no longer referenced.

This fixes the no-USB after resume problem seen on various ThinkPad,
problem initialy diagnosed with Alexander Polakov.

ok kettenis@, deraadt@


Revision tags: OPENBSD_5_4_BASE
# 1.246 01-Jun-2013 mlarkin

Stop printing ACPI wakeup devices in dmesg after the 16th wakeup device.

Helps with VMware VMs that report hundreds of wakeup devices.

ok reyk@, deraadt@


# 1.245 31-May-2013 bluhm

Fix the build for a kernel without wd and pciide in its config.
OK deraadt@ tedu@


# 1.244 30-May-2013 deraadt

Enforce ca_activate tree-walks over the entire heirarchy for all events,
cleaning up some shutdown-hook related code on the way.
(A few drivers related to sparc are still skipped at kettenis' request)
ok kettenis mlarkin, tested by many others too


# 1.243 18-Apr-2013 deraadt

pin the acpi thread to the BSP, because AML and SMI cannot be trusted. I
was certain we were doing this already, but it turns out no...


Revision tags: OPENBSD_5_3_BASE
# 1.242 24-Dec-2012 guenther

In acpiioctl(), Return though the splx() path if suser() returns an error

ok deraadt@


# 1.241 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


# 1.240 04-Oct-2012 ehrhardt

Use information provided by ACPI to attach secondary PCI host bridges.

ok kettenis@


# 1.239 07-Sep-2012 kettenis

Add a new function called acpi_pci_min_powerstate() that returns the lowest
power state for a PCI device for the power state (currently S3 or S4) we're
transitioning into. This function is probably too simplistic; there is a lot
of (silly) complexity in the ACPI spec.

ok deraadt@


Revision tags: OPENBSD_5_2_BASE
# 1.238 13-Jul-2012 pirofti

Disable acpivideo(4) on ASUS laptops.

There are some models that panic whenever an event is received.
Some cope, some don't. In order to get this right acpiec(4) requires
major rework. Until then let machines boot.

Discussed with deraadt@.


# 1.237 11-Jul-2012 mlarkin

for now, restrict hibernate to root in the acpi ioctl kernel interface.
normal rules for controlling this interface through apmd still apply.

ok deraadt@


# 1.236 09-Jul-2012 deraadt

tiny little oops in untested hibernate block


# 1.235 09-Jul-2012 deraadt

clean up ioctl() case statements


# 1.234 09-Jul-2012 deraadt

acpi_disable_onegpe() is not used; ok pirofti


# 1.233 24-May-2012 kettenis

If _MIN > _MAX consider a WordBus resource to be invalid. Fixes a panic on
the IBM x3550-M4.

ok mikeb@


# 1.232 29-Mar-2012 mlarkin

Missing #ifdef in previous diff


# 1.231 29-Mar-2012 mlarkin

Prevent hibernation attempts on machines with unsupported disk controllers.
The same check is also performed later in the hibernate sequence, but by
then it's too late to cleanly unwind (presently).

ok deraadt


# 1.230 26-Mar-2012 deraadt

simplify conditionals in acpi_sleep_state()
ok mlarkin


# 1.229 26-Mar-2012 deraadt

hook in the hibernate request code; half of this diff is from mlarkin
ok mlarkin


Revision tags: OPENBSD_5_1_BASE
# 1.228 20-Sep-2011 deraadt

Very early in suspend while the clock is still ticking realtime,
save the clock back to the rtc. The zaurus already did this.
ok phessler miod kettenis


Revision tags: OPENBSD_5_0_BASE
# 1.227 02-Jul-2011 nicm

kqueue attach functions should return an errno or 0, not a plain 1. Fix
the obvious cases to return EINVAL and ENXIO.

ok tedu deraadt


# 1.226 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.225 16-Jun-2011 pirofti

Import acpitoshiba driver.

This is originally written by Hiroyuki Aizu, ported and integrated
in our tree by Javier Vazquez <javama4 AT gmail> with tweaks and
suggestions by me.

Import prodded by deraadt@ and the lack of testers on tech@.


# 1.224 27-Apr-2011 jcs

attach acpithinkpad to newer lenovo models like the x120e

ok deraadt@


# 1.223 22-Apr-2011 jordan

Fix uninitialzied variables and formatting strings (-Wxxx errors)


Revision tags: OPENBSD_4_9_BASE
# 1.222 02-Jan-2011 jordan

Re-add code for ACPI taskqueue, eliminates need for state vars in acpi softc
Serialize processing ACPI tasks for sleep/powerdown/GPE event
ok deraadt@


# 1.221 31-Oct-2010 guenther

Revert last commit: it breaks resume on Thinkpads
"then please back it out" deraadt@


# 1.220 26-Oct-2010 jordan

Add task queue for ACPI gpe and notify handlers
This fixes eject on Dell Latitude dock and an issue on the Dell Mini
battery update.
ok deraadt


# 1.219 07-Oct-2010 jordan

Create separate functions for enabling wake and runtime gpes.
Remove old suspend/resume gpewalk function
ok deraadt, mlarkin


# 1.218 05-Oct-2010 jordan

split acpi_enable_onegpe to separate enable/disable functions
ok deraadt


# 1.217 05-Oct-2010 jordan

Added acpi_disable_allgpes function to clear GPE status
ok deraadt


# 1.216 05-Oct-2010 deraadt

clean up acpi_softc use more


# 1.215 05-Oct-2010 deraadt

delete a bit of #if 0 code


# 1.214 05-Oct-2010 deraadt

hide use of our global acpi_softc pointer a bit better


# 1.213 29-Sep-2010 deraadt

grab the acpi thread lock very early in acpi thread startup; not that it
is likely that anyone will race us... but why be careless.
ok kettenis


# 1.212 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.211 29-Aug-2010 jasper

- add a check for FWRITE here too.

ok miod@


# 1.210 27-Aug-2010 kettenis

Make PCI Power Management optional, and only enable it when acpi(4) attaches.

ok deraadt@


Revision tags: OPENBSD_4_8_BASE
# 1.209 08-Aug-2010 kettenis

Close race between the acpi thread and wscons ioctls running in process
context. We might rip this out post-release and replace it with a solution
that serializes things in a more obvious way.

ok deraadt@, miod@ and "those crazy texans who ok anything"


# 1.208 08-Aug-2010 deraadt

Report the acpi sleep states when on a RAMDISK; ok krw


# 1.207 07-Aug-2010 kettenis

Enforce acpi register access rescrictions.

ok mlarkin@, deraadt@, tested by marco@


# 1.206 07-Aug-2010 deraadt

upon resume, notify all the acpiac and acpibat drivers to update
their status. some laptops do not do this automatically.
ok kettenis canacar


# 1.205 06-Aug-2010 marco

Always call _PSW on _LID devices that support it to enable lid open to
resume the machine. Conversely disable it when the machine wakes up.

Tested by several ok deraadt


# 1.204 06-Aug-2010 deraadt

As part of a revamp of the PM1/GPE code, I write this diff to look for
unmanaged EN & STS bits in the PM1 register at interrupt time and report
them. As a side effect this splits the STS acknowledgement into two writes
(for power, and sleep) instead of one. The printf that is added (to spot
unmanaged STS bits) has not yet been triggered as far as we know.

Before the "write to PM1 registers at the right offset" diff went in, this
was not neccessary. But newer thinkpads do not have a working soft power
button without this diff.

We have no idea why.
ok mlarkin kettenis


# 1.203 05-Aug-2010 deraadt

PM1 power button events were generating 'fake events' to acpibtn PWRB,
but if there is no such device these were being tossed. Instead, arrange
for all power button handlers to flag a softc variable which is processed
by the thread; so that one place will know to request a powerdown (psignal
to init)
ok marco mlarkin


# 1.202 05-Aug-2010 deraadt

rename a variable which is annoyingly poorly named


# 1.201 05-Aug-2010 mlarkin

Write to the correct offset for various ACPI pmreg registers. Writing to
the correct register does properly clear the wake status bits and fixed
event bits, which allows some machines to have more than 1 suspend cycle.

bug spotted by deraadt@

tested by deraadt@ and myself on a half-dozen machines and others on
hackers@

ok deraadt@, marco@


# 1.200 05-Aug-2010 deraadt

Fix the power button. The pm1 register is 16 bits in size.
My mistake, a while back.
tested by krw


# 1.199 04-Aug-2010 kettenis

Print supported sleep states.

ok marco@, phessler@, deraadt@


# 1.198 03-Aug-2010 mlarkin

Use the proper flag for re-enabling certain hardware events (power btn,
sleep btn). The (incorrect) flag prevoiously used coincidentally had the
same value, so this shouldn't cause any different behavior than before.

tested on a variety of machines (i386, amd64, sp, mp) - no behavioral
change seen

ok deraadt@


# 1.197 28-Jul-2010 marco

Make all gasio failures look and behave the same.


# 1.196 28-Jul-2010 mlarkin

Delay a little longer on suspend before giving up. Fixes machines that
sometimes don't suspend because the sleep transition takes too long
(symptoms like failing to sleep but the backlight stays on).

ok deraadt@


# 1.195 27-Jul-2010 deraadt

remove XXX; EC can now be attached early when needed; ok jordan


# 1.194 27-Jul-2010 deraadt

more brackets feels good


# 1.193 27-Jul-2010 deraadt

XXX annotate a block in acpi_enter_sleep_state() that is pretty worrying


# 1.192 27-Jul-2010 deraadt

horrific indentation that nearly made me puke


# 1.191 27-Jul-2010 mlarkin

Set the sleeping indicator light on machines that support it, in case SMI
doesn't do it for us. Shows the 'moon' LED on older Thinkpads when
sleeping.

Tested on T43p, W500, T510 by me and on X61s by deraadt@.

ok deraadt@


# 1.190 27-Jul-2010 jordan

Early initialization of acpiec if ECDT table exists
Fixes hang when booting thinkpads while docked
ok deraadt


# 1.189 26-Jul-2010 deraadt

remove merge error which was commited


# 1.188 26-Jul-2010 pirofti

Add support for Sony ACPI hotkeys via a new driver: acpisony(4).

Currently it only works for the suspend button and tries to do right for
the brightness events, but I haven't found a Sony laptop that like to do
right so far.

In the future I want to make the brightness keys work on all Sony's and
also add support for the zoom hotkeys and whatever other funky keys I can
find on those things.

Okay deraadt@.


# 1.187 22-Jul-2010 deraadt

pretty large cleanup of SMALL_KERNEL


# 1.186 21-Jul-2010 deraadt

in the acpi GPE handler, late ack level interrupts not edge interrupts.
Duh.


# 1.185 21-Jul-2010 deraadt

Merge the edge and level gpe handlers into one (now possible since the
gpe structure tells us whether the gpe is edge or level)


# 1.184 21-Jul-2010 deraadt

delete #if 0 code we are not going to use; ok marco


# 1.183 20-Jul-2010 deraadt

make the mask code for acpi_enable_onegpe() match other similar code


# 1.182 20-Jul-2010 deraadt

Handle the acpi interrupt controller with proper edge/level handling,
wrapped in spltty to avoid racing against the interrupt controller.
Repair the gpe bit masking code while there, and do operations in the
right order
ok kettenis mlarkin, help from jordan at figuring out the order of
operations.


# 1.181 20-Jul-2010 matthew

When we're preparing to go to sleep, only call bufq_restart() if other
preparations failed.

ok kettenis@, thib@, deraadt@


# 1.180 19-Jul-2010 deraadt

permit compilation with ACPI_DEBUG


# 1.179 19-Jul-2010 deraadt

there is no need for a seperate dsdt_softc pointer; dsdt does not have a
softc but is just a part of the single acpi driver.
ok jordan


# 1.178 19-Jul-2010 deraadt

acpi_delay() is no longer needed; ok jordan


# 1.177 18-Jul-2010 mlarkin

Clear event status on resume, to avoid going back to sleep/reboot/shutdown
immediately after resume. Tested by a bunch of people on hackers@ on
various hardware. Fixes at least Toshiba NB200 and Gateway LT3103u.

ok marco


# 1.176 13-Jul-2010 deraadt

rename some thread related interfaces so they are logical


# 1.175 10-Jul-2010 jordan

Verify _ADR bdf is correct (some systems return 0xffff)
Works on Dell x9xx and Studio 1555


# 1.174 09-Jul-2010 jordan

Clear the noise


# 1.173 08-Jul-2010 jordan

Add mapping for ACPI device to PCI bdf (match autoconf tree)
Simplify resource parsing function to use buffer argument
Convert namespace linked lists to use queue macros
ok marco@, deraadt@


# 1.172 06-Jul-2010 deraadt

Use spltty() to lock downcalls from apm against the information being
modified by the acpi thread.
ok kettenis


# 1.171 06-Jul-2010 deraadt

Don't use a workq to do the suspend, because it races aginst the acpi
thread. Instead, just tell the acpi thread to do the suspend for us.
This makes apmd & zzz work correctly. While here, have acpithinkpad
attempt to post the event to apm before prompting the suspend itself.
ok kettenis marco mlarkin


# 1.170 05-Jul-2010 mlarkin

Reset register SCI_EN on resume. The ACPI spec clearly states we don't have
to do this, but many machines don't, or forget to, which leads to all sorts
of problems on resume (broken battery stats, overheating, failure to resume
more than once, etc).

tested by many, ok marco@, deraadt@


# 1.169 01-Jul-2010 thib

do bufq quiesce again, since vnd/the bufq_done call in
biodone has been fixed.


# 1.168 01-Jul-2010 jordan

Backout recent AML changes, breaks on R210 and others


# 1.167 01-Jul-2010 jordan

Simplified aml_parse_resource call, cleanup code
ok marco


# 1.166 01-Jul-2010 jordan

Fix subordinate bus # for multi-root PCI buses
ok marco, kettenis


# 1.165 30-Jun-2010 thib

Disable/partially backout the bufq quiesce changes as this
is causing havoc with vnds and release must be buildable.


# 1.164 29-Jun-2010 deraadt

Fix ramdisks (new code was placed inside a SMALL_KERNEL chunk)


# 1.163 29-Jun-2010 jordan

Add support for mapping ACPI to PCI devices
ok kettenis, deraadt


# 1.162 29-Jun-2010 kettenis

Call bufq_quiesce() on suspend to stop all I/O and bufq_restart on resume.
Makes suspend/resume work much better while doing disk I/O.

ok mlarkin@


# 1.161 29-Jun-2010 tedu

60 is a more reasonable guess for number of minutes in an hour than 100.
spotted by TeXitoi. ok deraadt


# 1.160 27-Jun-2010 jordan

Add _acpi_matchhids routine, precursor to pci device match


# 1.159 27-Jun-2010 mlarkin

Clear ACPI fixed event status on resume (power buttons/etc) so that some
machines don't immediately resume after suspending to S3.

Tested by kettenis@ and I on 5 or 6 machines, deraadt@ "yeah, looks good"


# 1.158 27-Jun-2010 jordan

Cleaned up acpi_maptable, adds to table list directly


# 1.157 07-Apr-2010 deraadt

If we have done a wsdisplay_suspend(), and ACPI decides it does not want to
suspend, we must roll back by calling wsdisplay_resume() or the virtual
console switching will be locked forever
ok oga


# 1.156 07-Apr-2010 kettenis

Make suspend/resume work on MP machines (running an MP kernel). Joint work
from mlarkin@ and me, with some amd64 fixes thrown in by deraadt@

ok marco@, deraadt@, pirofti@, mlarkin@


# 1.155 31-Mar-2010 kettenis

Bring /dev/apm support in line with apm(4). Make the suspend button send
a suspend request event to apmd(8) instead of suspending immediately. Also
keep track of whether /dev/apm and /dev/apmctl are currently open, such that
we can still suspend immediately if apmd(8) isn't running.

ok deraadt@, marco@, pirofti@, jsing@, oga@


# 1.154 30-Mar-2010 oga

Prevent the apmd/x races for good.

When we hit suspend time, go through all wsdisplays on the system. if
they are in mode MAPPED, but not MODE_DUMBFB then if possible do a full
vt switch to a !mapped vt, and prevent switching back until resume time.
This has to be called from MD code because this involves userland
running so that X can run the vt switch signal handler. This way, any
case where we are using the "poke registers from userland" model, we
will not be on the hardware when we go down, so the kernel can actually
handle thing properly.

Tested on several acpi laptops (by kettenis@ and ian@), x40 (me and
beck@ at LEAST) and zaurus (me). Maybe others, but if so I forgot who at
this time..

Idea from deraadt somewhere over the Faroe Islands (I thought of a
similar thing myself a while ago). Much prompting from him. Ok and
comments miod@


# 1.153 25-Mar-2010 oga

Fix & vs. && in the kqfilter code.

Based on a comparison with the apm code.

ok deraadt@, kettenis@


Revision tags: OPENBSD_4_7_BASE
# 1.152 26-Nov-2009 mlarkin

Visual (and on some machines, audio) feedback while the machine is
resuming via calls to aml _SST method. This also makes the "moon" LED on
thinkpads to blink during resume and cease when resume is completed.

ok pirofti@


# 1.151 26-Nov-2009 deraadt

Toggle into "cold" when we we disable interrupts, because tsleeping
in the AML interpreter and context switching into userland processes
tends to sometimes pop the ACPI trampoline page out of our address
space. Makes lots more machines work.
ok kettenis mlarkin


# 1.150 24-Nov-2009 jsg

in the resume path evaluate _WAK before dealing with device drivers
like the spec says, not after.

ok mlarkin@


# 1.149 24-Nov-2009 deraadt

abort suspends on SMP systems until that gets written; ok mlarkin


# 1.148 23-Nov-2009 mlarkin

pci should return the result of it's children's suspend/resume calls back
to its parent.

handle suspend failure case and unwind if devices have an activate function
that returns failure.

ok deraadt@, kettenis@


# 1.147 23-Nov-2009 mlarkin

acpi* devices need to call their children on suspend/resume.
ok deraadt@


# 1.146 23-Nov-2009 pirofti

Remove ACPI_SLEEP_ENABLED checks.

This enables by default the suspend/resume paths in the kernel.

Okay deraadt@.


# 1.145 23-Nov-2009 deraadt

knf and spacing that are super annoying


# 1.144 23-Nov-2009 mlarkin

Hook up Theo's new children suspend/resume function, as well as more ACPI
suspend/resume glue.

ok deraadt@


# 1.143 26-Oct-2009 deraadt

Do not do strncmp comparisons on dv_xname because longer device names which
look similar could arrive in the future. Instead, compare directly against
dv_cfdata->cf_driver->cd_name
Issue originally spotted by miod


# 1.142 26-Oct-2009 jordan

Verify checksum+address when loading ACPI tables.
Some systems had invalid entries in RSDT/XSDT.
ok marco@


# 1.141 23-Jul-2009 cnst

New aibs(4) driver for ASUSTeK AI Booster (ACPI ATK0110) hardware monitoring,
with sensor state support through limits provided by the ACPI.

Tested on several ASUS motherboards kindly networked by Sam Fourman Jr.

ok deraadt marco jordan


Revision tags: OPENBSD_4_6_BASE
# 1.140 03-Jun-2009 pirofti

Import acpi power resource driver. This is needed for suspend/resume logic.

Okay jordan@ and marco@.


# 1.139 03-Jun-2009 pirofti

Add acpivideo support. This adds brightness support for all laptops
except thinkpads, they will use the acpithinkpad driver. The driver is
also hooked into wsconsole. So brightness can be adjusted via:

$ wsconsctl display.brightness=<percentage>

This is very helpfull on some laptops that have a nasty bios and get two
steps instead of one when pressing the brightness button.

Tested on various dell, fujitsu, acer, samsung and other laptops.

Okay marco@, miod@. Suggestions from kettenis@.
Lots of reviews and help from miod@, thanks!


# 1.138 03-Jun-2009 jordan

Added ejectable bay for IDE cdroms
ok marco@


# 1.137 30-Apr-2009 marco

Oga changed apm ioctls and forgot to grep. Temporary work around by hitting
the sleep button when we get any request. This interface will chnage at
some point.
ok oga


# 1.136 19-Apr-2009 krw

Call acpi_sleep_walk() when powering down just like when entering
any other sleep state. This tells acpi wake devices to not wake up
the box until manual powering up resets their status. Makes my Dell
GX520 stay powered off in response to 'halt -p'.

In snaps for a while.

ok marco@ (several times) deraadt@


# 1.135 17-Apr-2009 pirofti

Add device ranking support for future sleep/wake developments.

okay marco@


# 1.134 11-Mar-2009 jordan

Cleanup.. use aml_evalinteger instead of aml_evalname
ok marco@


# 1.133 10-Mar-2009 jordan

Fix cut/paste error.. call _BFS not _PTS
ok marco@


Revision tags: OPENBSD_4_5_BASE
# 1.132 21-Feb-2009 marco

overzealous cut/paste; found by Vladimir Kirillov <proger@uaoug.org.ua>


# 1.131 20-Feb-2009 marco

Oops didn't mean for Debugger() to remain there.


# 1.130 19-Feb-2009 marco

suspend/resume bits so that we can develop this in tree. This is disabled.
code from mlarkin and me
help from art,toby,jordan and several others
ok jordan, go for it deraadt


# 1.129 10-Feb-2009 jordan

Fix null pointer for shutdown if acpi not enabled
ok marco@


# 1.128 04-Feb-2009 kettenis

Some systems that claim to be ACPI 2.0 apparently don't provide an XSDT
address. Make the code fall back on the RSDT on those systems.

From Joerg Niendorf <p3y80@internode.on.net>. Fixes PR 5986.


# 1.127 17-Jan-2009 krw

A couple of stray malloc/memset -> malloc(M_ZERO), a bit of knf and
lose a couple of unnecessary casts. No functional change.

Feedback from kettenis@

ok kettenis@ marco@


# 1.126 06-Nov-2008 marco

Collapse all sanity checks of hids into one function.

Originaly from jordan


Revision tags: OPENBSD_4_4_BASE
# 1.125 02-Jul-2008 fgsch

add acpivideo. for now almost an empty skel so others can work on
suspend/resume. eventually it will also manage output switching and
brightness where supported. prodded by marco@


# 1.124 11-Jun-2008 marco

Fix $OpenBSD$ for once and for all


# 1.123 08-Jun-2008 deraadt

aaa_dev was being set from an uninitialized variable; ok toby


# 1.122 07-Jun-2008 marco

Kill ACPIVERBOSE. It was never used and is worthless to boot.

comments jmc
ok toby


# 1.121 06-Jun-2008 marco

Shrink acpi some more for floppies and while here re-enable acpi on the
floppies. Fixes crazy sony vaio kettenis has.

tested by deraadt
ok deraadt jordan kettenis


# 1.120 01-Jun-2008 marco

Fix \n nit in SMALL_KERNEL.


# 1.119 01-Jun-2008 marco

Re-enable new parser. Lots of testing by various folks. Fits all media
now too.

ok kettenis deraadt


# 1.118 16-May-2008 dlg

back out jordans new parser.

it didnt have enough oks from the right people, it did break machines
that were previously working, and it bloated the RAMDISKS and install
media beyond the constraints of the media.

requested by deraadt@


# 1.117 14-May-2008 jordan

Adding new ACPI Parser code
Old guts of code still exists, needs to be torn out next
ok marco@
ok brad@


# 1.116 27-Apr-2008 jcs

add acpithinkpad(4), an acpi support driver for ibm/lenovo
thinkpads.

provides functionality that the bios did on older, non-acpi
thinkpads like respond to screen brightness keys and toggle the
bluetooth device on and off.

ok deraadt@ and marco@


# 1.115 24-Apr-2008 jsing

Introduce acpiasus(4), a driver for the ACPI based hotkeys found in many
ASUS laptops (including the ASUS EeePC) - largely based on NetBSD's
asus(4) driver. On the ASUS EeePC this allows us to enable/disable
wireless, change screen brightness and use the volume keys.

ok jsg@, weingart@


# 1.114 12-Apr-2008 kettenis

Get rid of acpi_s5 global variables; simply send SIGUSR2 instead of SUGUSR1
to tell init(8) to power down the machine.

ok krw@


Revision tags: OPENBSD_4_3_BASE
# 1.113 21-Jan-2008 jsg

Check against RESET_REG_SUP in FADT before doing the acpi reset dance
after all, not doing so appears to break more machines than it fixes.

ok marco@


# 1.112 05-Jan-2008 kettenis

Never attach more acpicpu(4) devices than cpu(4) devices. Always attach
the first acpicpu(4) device on non-MULTIPROCESSOR kernels, regardless of its
ACPI processor ID.

ok mikeb@


# 1.111 05-Dec-2007 deraadt

Hang acpi(4) below bios(4) instead of mainbus(4). This lets us move acpi(4)
before pcibios(4), and then.... if acpi(4) attaches, skip pcibios(4) since
it messes the machines up. Fixes claudio's HP dl320 G5, and almost assuredly
others.
ok kettenis


# 1.110 05-Dec-2007 deraadt

repair acpiopen/acpiclose and friends to handle the /dev/apmctl interface,
so that apmd(8) can actually work correctly against acpi(4), and thus,
even do the fancy -C/-A stuff; also tested by jmc


# 1.109 02-Dec-2007 fgsch

fix the trailing space for real now.


# 1.108 02-Dec-2007 fgsch

avoid trailing spaces on output; marco@ ok.


# 1.107 27-Nov-2007 kettenis

Fix off-by-one that made us attach acpicpu0 and acpicpu1 on GENERIC kernels.

ok marco@


# 1.106 25-Nov-2007 jsg

When available and ACPI is enabled use the ACPI reset method
to reboot. This is required for some quirky sis machines
and likely others to reboot.

Tested by many. 'commit early commit often' deraadt@


# 1.105 19-Nov-2007 deraadt

sigh, amd64 still uses that stupid acpi_enabled variable...


# 1.104 19-Nov-2007 deraadt

softc's come initialized to 0, you know...


# 1.103 19-Nov-2007 deraadt

store revision; from jsg


# 1.102 19-Nov-2007 kettenis

Initialize sc_facs earlier.

ok deraadt@, beck@, canacar@


# 1.101 16-Nov-2007 deraadt

fix the bus_space #define nightmare, so that amd64 and i386 are much more
uniform. as a result shared code like acpi needs less #ifdef's
ok marco kettenis


# 1.100 15-Nov-2007 deraadt

remove the #ifdef ACPI_ENABLE. the EC codepath is either safe enough (and
will be improved), or it is unsafe because AML assumes it must be there
ok almost everyone


# 1.99 15-Nov-2007 mikeb

Make acpicpu(4) attach to the existing CPUs only.

Problem reported by form@. Fix idea by kettenis@.
Lots of help from deraadt@. Tested by deraadt@, form@ and me.

ok deraadt marco


# 1.98 14-Nov-2007 deraadt

spacing


# 1.97 12-Nov-2007 deraadt

correct : vs , in attach-time messages


# 1.96 06-Nov-2007 deraadt

all ACPI_ENABLE kernels need acpi_interrupt()


# 1.95 06-Nov-2007 deraadt

move acpi_read_pmreg() and acpi_write_pmreg() out of SMALL_KERNEL so
that acpi ramdisks can compile (obvious solution)


# 1.94 05-Nov-2007 canacar

Establish the acpi interrupt earlier. Fixes problems with some
GENERIC.MP kernels not getting acpi interrupts. This is a
workaround until the real issue with late establishment is fixed.
ok weingart@, beck@, gwk@


# 1.93 03-Nov-2007 jordan

Added support for displaying wakeup devices
ok beck@,weingart@,gwk@


# 1.92 11-Oct-2007 kettenis

When walking the tree yo find _PRT methods, check whether the device is
actually enabled and functioning. Similar to what we do for _INI.

ok weingart@, beck@


# 1.91 08-Oct-2007 krw

More simple memset(,0,) -> M_ZERO changes. In this batch move to
size(*p) as the first malloc() parameter where p is declared locally
and thus easy to check. Add M_ZERO to gpe_table allocation in acpi.c
even though there is no obvious bzero or memset nearby.


# 1.90 13-Sep-2007 mk

Remove unnecessary memset() (see aml_evalnode(9)) and a bit of KNF.

ok toby


# 1.89 13-Sep-2007 weingart

Implement ACPI 6.5.1 spec tree walk for _STA and _INI.
Fixes mk's laptop. No regressions so far (thank you to the
testers).

ok gwk@, mk@, marco@


Revision tags: OPENBSD_4_2_BASE
# 1.88 17-Apr-2007 mk

Now that acpidock no longer hangs machines during boot (at least
according to the reports I've been getting) it should be safe to enable
it again, so remove the #if 0 stuff around it.

ok marco


# 1.87 11-Apr-2007 jordan

Added changes for C-State
ok marco@


# 1.86 26-Mar-2007 marco

Calculate minutes left more accurately.

From giovanni <qgiovanni@gmail.com>


Revision tags: OPENBSD_4_1_BASE
# 1.85 03-Mar-2007 mk

Stop looking for acpidock until we fix that stuff that breaks all pre-60
series ThinkPads with acpidock(4) enabled.


# 1.84 22-Feb-2007 marco

We were getting no interrupts in .MP. There are 2 fixes; one in
acpi_machdep.c and move the interrupt establishment down after ioapic
has been initialized.

ok jordan


# 1.83 21-Feb-2007 kettenis

Make an acpi-enabled GENERIC.MP work on systems that only support PIC mode.

ok marco@


# 1.82 19-Feb-2007 jordan

Added acpi_map_address function for mapping GAS/IO registers
ok marco@


# 1.81 18-Feb-2007 jordan

Added dynamic perfect hash for opcodes
ok marco@


# 1.80 18-Feb-2007 marco

Shave off another K pre compression.
Shuffle SMALL_KERNEL functions around so that we only have a single #ifndef


# 1.79 17-Feb-2007 marco

Add SMALL_KERNEL throught acpi to make it fit on boot media.
ok deraadt


# 1.78 13-Feb-2007 marco

Add smarter _INI code. This helps with some dissapearing devices.

ok kettenis jordan


# 1.77 31-Jan-2007 gwk

Introduce acpi_hasprocfvs thats "ACPI Has Processor Frequency and Voltage
Scalling" a global flag set by acicpu attach to indicate that ACPI is
handling fvs through the _PSS objects. This will fix disappearing
EST/powernow when booting kernels with acpi enabled, as acpicpu isnt
compiled into GENERIC yet. Also in cases where acpicpu is built in and
legacy EST/powernow work but acpi is missing the _PSS object, the legacy
routines will be used.

tested by dim@, and Nick Nauwelaerts <nick AT nauwelaerts.net>
ok marco@, dim@


# 1.76 22-Jan-2007 mk

Actually look for docking stations by looking for _DCK nodes.

ok marco


# 1.75 02-Jan-2007 marco

On some systems EC must be attached as the first device. Move EC from
generic _HID devices to its own routine to force first attach. Bug found
and debugged by joris@

Code mostly written by joris@


# 1.74 26-Dec-2006 marco

Add polling to devices that require it such as acpibat and acpitz.
Use the same tsleep/wakeup mechanism as the interrupt code to keep it non-
concurrent.


# 1.73 21-Dec-2006 deraadt

even more knf love


# 1.72 21-Dec-2006 marco

Remove some leftover acpiec gpe/interrupt code.


# 1.71 20-Dec-2006 deraadt

oops, name already used


# 1.70 20-Dec-2006 deraadt

rename __acpi_enable_gpe(); ok marco


# 1.69 20-Dec-2006 deraadt

acpi_unmap_pmregs() is not used, ok marco


# 1.68 19-Dec-2006 deraadt

simplify some low-level methods substantially (shrinking code); ok marco jordan


# 1.67 06-Dec-2006 mk

malloc(..., M_NOWAIT) with no NULL check is bad. If allocation fails,
complain, clean up, and bail out of attach.

input and ok gwk


# 1.66 28-Nov-2006 jordan

Fixed clearing arg for acpi_init_pic
ok kettenis@


# 1.65 27-Nov-2006 kettenis

Don't spam dmesg.


# 1.64 27-Nov-2006 jordan

Fixes for acpiprt routing
Post-parse fixup for namerefs
ok kettenis@


# 1.63 25-Nov-2006 marco

Don't handle acpiev events inside the gpe handler since this causes deadlocks.

ok jordan@


# 1.62 15-Nov-2006 kettenis

Hook up acpi apic interrupt routing support to generic acpi code.

ok jordan@


# 1.61 02-Nov-2006 marco

Kill acpiec attach function and add its functionality to the generic _HID
device attach

prodded and ok jordan


# 1.60 02-Nov-2006 marco

Fix comments to reflect reality.


# 1.59 19-Oct-2006 marco

Replace ACPI ioctls with APM ioctls. This makes ACPI talk the same protocol
as APM therefore userspace can now retrieve power information in the same
fashion as all other apm arches (zaurus, macppc, i386 etc).

To use force the use of acpi device like so: apmd -f /dev/acpi

ok jordan@


# 1.58 19-Oct-2006 jordan

Fixed GPE debug levels
Fixed ref counter on temporary bufferfields
DSDT parsing fully working on IBM Thinkpad
ok marco@


# 1.57 15-Oct-2006 jordan

Removed old GPE handler code remnants
ok marco@


# 1.56 12-Oct-2006 jordan

Massive ACPI changes
Fixed ACPI leaks
new ACPI API
ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.55 30-Jun-2006 gwk

KNF, No functional change,
"go at it" marco@


# 1.54 30-Jun-2006 jordan

Support for calling Embedded Controller via new GPE interface
ok marco@


# 1.53 30-Jun-2006 jordan

Added new code for GPE handling
Also works with ACPIEC; need to fixup acpiec.c to remove original interrupt handler
ok marco@


# 1.52 30-Jun-2006 jordan

Added offset argument for acpi read/write pmreg routines (new GPE code)
ok marco@


# 1.51 31-May-2006 todd

do not call acpi_powerdown() unless acpi has successfully initialized
ok gwk@ canacar@


# 1.50 29-May-2006 canacar

Add embedded controller support.
ok marco@ deraadt@ testing & ok gwk@


# 1.49 19-May-2006 canacar

Minimal thermal zone driver. Monitors thermal zone temperature,
shuts down the system if the 'critical temperature' is reached.
ok marco@


# 1.48 11-Apr-2006 marco

KNF.


# 1.47 11-Apr-2006 gwk

Add acpi_resume(struct acpi_softc *sc), which calls some AML methods,
detected at attach time, currently unused.

ok marco@


# 1.46 11-Apr-2006 gwk

Discover some ACPI sleep related methods, and call them at the
appropriate time in acpi_enter_sleep_state().

ok marco@


Revision tags: OPENBSD_3_9_BASE
# 1.45 26-Feb-2006 marco

Add acpicpu as a device.

ok jordan@


# 1.44 23-Feb-2006 marco

remove dead function.

ok jordan@


# 1.43 22-Feb-2006 marco

Print out all tables it found during autoconf. We'll need this data in
the future.


# 1.42 22-Feb-2006 marco

Make dmesg less verbose.

ok jordan.


# 1.41 22-Feb-2006 jordan

Added parsing of SSDT tables
ok marco@


# 1.40 22-Feb-2006 jordan

Added new aml_register to notify by device id
Moved powerdown code to button device
ok marco@


# 1.39 21-Feb-2006 marco

Readjust debug levels that crept in too low over the last few days.
KNF


# 1.38 21-Feb-2006 marco

Rig the power button so that the machine shuts down correctly.


# 1.37 20-Feb-2006 marco

Add acpibtn device. Remains disabled for now.


# 1.36 19-Feb-2006 jordan

Added code for aml_notify GPE notification callback
ok marco@


# 1.35 19-Feb-2006 grange

Fix acpi_enter_sleep_state() by replacing current ugly hack
with mostly correct sleeping state transition code.

ok brad@ marco@


# 1.34 19-Feb-2006 marco

jordan:
Enable interrupts.
Added peripheral code to make this work.

marco:
Replace softintr and other types of interrupts with a handler with context.
Fixed interrupt magic for GPEs.
Some KNF

This is work done by jordan and I but has to be a single commit.


# 1.33 17-Feb-2006 marco

Remove earlier introduced delays. We really only need them during autconf.

ok jordan@


# 1.32 17-Feb-2006 marco

Add a generic delay function as per the ACPI spec.

from jordan@


# 1.31 17-Feb-2006 marco

Use tsleep instead of delay when !cold.


# 1.30 17-Feb-2006 marco

Argh, we need delays between reads/writes. The backend reads (i2c most likely)
are so slow that values come back as 0 sometimes. We have to do this
despite the spec saying we shouldnt have to. Guess having a faster
parser than others can hurt sometimes.

Idea from jordan.


# 1.29 17-Feb-2006 jordan

Fixed code for handling _INI
Rearranged debug statements
ok marco@


# 1.28 16-Feb-2006 jordan

Added stub for calling _INI
Fixed aml_dowhile for NULL return
ok marco@


# 1.27 16-Feb-2006 jordan

Fixed parser/evaluator to retrieve battery info+state
Cleanup of parser
Dell laptops return battery status
ok marco@


# 1.26 06-Feb-2006 brad

fix a typo so this will compile on amd64.


# 1.25 04-Feb-2006 marco

When ACPI_ENABLED wasn't set but the devices were it had an unknown variable.

Pointed out by ian@.


# 1.24 03-Feb-2006 jordan

Rewrote aml parser yet again...
Fixed bus_space_map for i386/amd64
Added _BST and _BIF to acpibat to get status/info
ok marco@


# 1.23 20-Jan-2006 jordan

More evaluator changes
Removed old evaluator code
Fixed _HID value
ok marco@


# 1.22 19-Jan-2006 jordan

Added support for read/write of PCI Config space
Help jason@
ok marco@


# 1.21 18-Jan-2006 jordan

Added new evaluation routines
Added helper methods for buffer/field evaluation
Fixed broken indentation on dsdt.c

ok marco@


# 1.20 17-Jan-2006 jordan

Major overhaul of the aml parser. Evaluater temporarily disabled.

ok marco@


# 1.19 06-Jan-2006 grange

Check for ACPI control availability only if ACPI_ENABLE defined.


# 1.18 06-Jan-2006 grange

Informative error messages on attach, prettier dmesg.


# 1.17 06-Jan-2006 grange

printf -> dnprintf conversion for debug messages.


# 1.16 05-Jan-2006 grange

Find SLP_TYPx values and store them for future use in
acpi_enter_sleep_state().


# 1.15 05-Jan-2006 grange

Random KNF and cleanup


# 1.14 05-Jan-2006 grange

Provide symbolic names for the acpi sleeping states and use it.


# 1.13 28-Dec-2005 marco

Moved DEVNAME to a acpidev.h

ok jordan@


# 1.12 18-Dec-2005 sturm

add back comment about wrong code

ok marco


# 1.11 16-Dec-2005 marco

Remove test for kbd_reset. Prompted deraadt@, ok jordan@


# 1.10 16-Dec-2005 jordan

Added hooks for powerdown/power button press


# 1.9 16-Dec-2005 jordan

Cleaned up common power management register i/o
Added enter sleep state ioctl
Hooked power button interrupt
ok marco@


# 1.8 16-Dec-2005 marco

Remove autoconf chatter


# 1.7 16-Dec-2005 jordan

Moved DSDT AML parsing into parent ACPI driver
ok marco@


# 1.6 14-Dec-2005 marco

Add AC device glue.


# 1.5 14-Dec-2005 marco

Adjust debug printing levels to something more sane.


# 1.4 13-Dec-2005 marco

Add glue to attach devices to apci.


# 1.3 07-Dec-2005 marco

KNF


Revision tags: OPENBSD_3_8_BASE
# 1.2 10-Jul-2005 grange

Spacing cleanup.


# 1.1 02-Jun-2005 tholo

Start on a basic ACPI framework -- does not do much more than read out the
ACPI tables into kernel memory and attach ACPI and HPET timers currently.

In order to test this code, enabling the devices in GENERIC as well as
the ACPI_ENABLE option is needed. This code does not do any thermal
control yet, so this should be done with care depending on the platform.

In the tree so more people can contribute to making this more fully
featured.

Ok niklas@ grange@ tedu@


# 1.419 10-Feb-2023 visa

Adjust knote(9) API

Make knote(9) lock the knote list internally, and add knote_locked(9)
for the typical situation where the list is already locked.

Remove the KNOTE(9) macro to simplify the API.

Manual page OK jmc@
OK mpi@ mvs@


Revision tags: OPENBSD_7_2_BASE
# 1.418 13-Sep-2022 kettenis

Split out the code that collects data from acpiac(4), acpibat(4) and
acpisbs(4) for apm(4) and hook it up to the arm64 version of apm(4) on
systems with ACPI.

ok kn@


# 1.417 12-Sep-2022 kettenis

Add support for level-triggered GPIO events.

ok mlarkin@


# 1.416 10-Sep-2022 kettenis

Don't restrict GPIO events to pin numbers below 256. With the _EVT()
method events corresponding to larger numbers are possible.

Fixes lid state detection on the x13s.

ok patrick@


# 1.415 03-Sep-2022 kettenis

Make sure we don't suspend if we have no way to wake up the machine.
The idea is that device drivers that can wake up the machine register
themselves by calling device_register_wakeup(). To prevent regressions
on amd64, we let acpi(4) register itself as a wakeup device if the AML
for the machine defines wakeup devices. This may be refined in the
future.

This diff will prevent people from suspending their arm64 SBCs without
having a way to wake them up. For now the only driver that registers
itself is axppmic(4), which means that at this moment only SBCs with
an allwinner A64 or H5 SoC will actually support suspend/resume.

ok mlarkin@, deraadt@


# 1.414 10-Aug-2022 patrick

Provide the AML root in ACPI's softc so that drivers that need access
don't have to declare it using extern.

Suggested by and with feedback from kettenis@
Tested by deraadt@ on arm64, amd64 and i386
Tested by bluhm@ on amd64 and i386


Revision tags: OPENBSD_7_1_BASE
# 1.413 17-Feb-2022 jsg

reduce includes


# 1.412 10-Feb-2022 visa

Embed klist head in acpi_softc to avoid explicit malloc.

OK kettenis@


# 1.411 09-Feb-2022 deraadt

Move x86-only stubs used by kern/subr_suspend.c into dev/acpi/acpi_x86.c
for sharing between i386 and amd64.
ok mlarkin kettenis


# 1.410 08-Feb-2022 deraadt

The suspend/resume code is a sticky mess of MI, MD, and ACPI sequencing.
This splits out the MI sequencing, backing it with per-architecture helper
functions. Further steps will be neccesary because ACPI and MD are too
tightly coupled, but soon we'll be able to use this code for more architectures
(which depends on figuring out the lowest-level cpu sleeping method)
ok kettenis


# 1.409 04-Feb-2022 robert

set the APM_BATT_CHARGING state if the battery is being charged and try
to calculate the remaining time to be fully charged

ok jca@


# 1.408 02-Feb-2022 deraadt

acpi_addtask() calls malloc() w/ M_NOWAIT (because some calls come from
interrupt context), this however means occasional resource shortage will
result in callbacks registration failing, and unknown consequences for
the task-submitting caller.
Changing this to use pools with a low water mark, decreases the odds
of that problem occuring.
ok kettenis


# 1.407 01-Feb-2022 deraadt

GPE_DIRECT is calling the AML parser from interrupt context, which is
not permitted. Luckily nothing is using GPE_DIRECT anymore, so this code
can be deleted.
ok kettenis


# 1.406 26-Jan-2022 kettenis

An ACPI device needs to be both present and enabled for it to function.
So only attempt to attach hardware that has both bits enabled. This fixes
an issue where com(4) would attach for a disabled serial port leading to
misdetection of the hardware variant and a subsequent hang when /etc/rc
runs ttyflags -a.

ok anton@, deraadt@


# 1.405 12-Jan-2022 patrick

Make acpi_getpropint() return uint64_t, as ACPI integers are in fact that
wide and some _DSD properties depend on it.

ok kettenis@


# 1.404 04-Jan-2022 patrick

acpi_getprop() needs to actually make sure that we're looking at the
correct property. While there adjust acpi_getpropint() as well to
increase similarity with acpi_getprop().

ok kettenis@


# 1.403 16-Dec-2021 anton

Attach com over acpi on amd64. Some hardware uses a different interrupt
assignment compared to the the legacy one supported by com over isa.
This causes the console to halt once userland takes over as no
interrupts are received. The actual address and irq can be read from
ACPI, kettenis@ already added support for arm64 which paved the way for
amd64.

Some consoles that previously attached over isa are now expected to
attach over acpi.

Thanks to patrick@ for testing on arm64.

ok kettenis@


# 1.402 07-Dec-2021 kettenis

Fix parsing of SR_IRQ resource descriptors.

ok patrick@, anton@


# 1.401 02-Nov-2021 deraadt

knf


# 1.400 30-Oct-2021 deraadt

Change hw.perfpolicy=auto by default, at startup. If the system has AC
power connected (default is yes when no driver differentiates) then default
to 100% performance. On battery, use the existing auto algorithm (which is
admittedly somewhat unrefined).
This change overrides the system/BIOS speed and puts OpenBSD in control.
As this happens very early during boot, besides speedups in all usage usage
patterns, some surprises: unhibernate and sysupgrade times are cut in half.
note: on a few architectures, the setperf fn pointer is changed late, and
thus the auto algorithm stops timeing out. kettenis and i will look for
a solution.
in snaps for more than a week.
ok kettenis


Revision tags: OPENBSD_7_0_BASE
# 1.399 20-Jul-2021 mlarkin

Fix trailing whitespace in a few places.

No code change.


# 1.398 09-May-2021 kettenis

Some x86 machines advertise the "hardware reduced" ACPI feature, advertise
S4 and S5 support, but fail to populate the SLEEP_CONTROL_REG and
SLEEP_STATUS_REG descriptions in the FADT. An example of such a machine
is the ASUS Zenbook 14 UM433DA. Any attempt to powerdown the machine
will result in a kernel crash. It turns out that using the legacy ACPI PM
registers works fine on this machien. So fall back on those registers
if SLEEP_CONTROL_REG and/or SLEEP_STATUS_REG aren't provided.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_9_BASE
# 1.397 15-Mar-2021 patrick

Add acpi_iommu_device_map(), which replaces the DMA tag with one that
is blessed with IOMMU magic, if available. This is mainly for arm64,
since on amd64 and i386 the IOMMU only captures PCIe devices, as far
as I know, which uses the pci_probe_device_hook(). This though is for
non-PCI devices attached through ACPI.

ok kettenis@


# 1.396 07-Mar-2021 patrick

Pass standard DMA tag to acpi(4) table drivers.

ok kettenis@


# 1.395 27-Dec-2020 deraadt

acpi_map_address() cannot be wrapped by SMALL_KERNEL anymore, as
it is used by acpihpet.c


# 1.394 25-Dec-2020 visa

Refactor klist insertion and removal

Rename klist_{insert,remove}() to klist_{insert,remove}_locked().
These functions assume that the caller has locked the klist. The current
state of locking remains intact because the kernel lock is still used
with all klists.

Add new functions klist_insert() and klist_remove() that lock the klist
internally. This allows some code simplification.

OK mpi@


# 1.393 17-Dec-2020 kettenis

Fix some issues with referencing named ACPI nodes from Packages.
These references need to be resolved at runtime rather than when
they're parsed such that they pick up the right values for those nodes
which can be changed when for example _INI methods run.

The current approach is to replace these reference with a string that
names the node in question. The problem with that is that packages
can also contain normal strings. Which means that we need special
code that depends on the context where the package is used.

This diff takes a different approach by retaining a reference when
parsing. Code that uses a package will need to resolve this reference
but there is no ambiguiety anymore.

ok patrick@


# 1.392 05-Dec-2020 kettenis

Add support for the _EVT method to the GPIO support code.

ok patrick@


Revision tags: OPENBSD_6_8_BASE
# 1.391 27-Aug-2020 jmatthew

Reorder the acpi attach process so we look at Processor() CPU nodes
after Device() ones, since we should prefer the newer node type.
If we see any Device() nodes, don't attach acpicpu(4) to any Processor()
nodes if they're also present. This also makes acpitz(4) and
acpipwrres(4) devices attach slightly later.

ok kettenis@, also tested by jmc@


# 1.390 16-Aug-2020 gnezdo

Remove sc_maxgpe from acpi_softc which was never read from

Remove a dead conditional from acpi_init_gpes while there.

ok jcs@


# 1.389 21-Jul-2020 deraadt

acpi can use IPL_BIO (a low interrupt) since it only enqueues operations for
later processing. The use of a high interrupt will predate suspend/resume
efforts, we had to redesign acpi to be non-reentrant obviously
discussed with kettenis, in snaps for more than a week


# 1.388 24-Jun-2020 cheloha

kernel: use gettime(9)/getuptime(9) in lieu of time_second(9)/time_uptime(9)

time_second(9) and time_uptime(9) are widely used in the kernel to
quickly get the system UTC or system uptime as a time_t. However,
time_t is 64-bit everywhere, so it is not generally safe to use them
on 32-bit platforms: you have a split-read problem if your hardware
cannot perform atomic 64-bit reads.

This patch replaces time_second(9) with gettime(9), a safer successor
interface, throughout the kernel. Similarly, time_uptime(9) is replaced
with getuptime(9).

There is a performance cost on 32-bit platforms in exchange for
eliminating the split-read problem: instead of two register reads you
now have a lockless read loop to pull the values from the timehands.
This is really not *too* bad in the grand scheme of things, but
compared to what we were doing before it is several times slower.

There is no performance cost on 64-bit (__LP64__) platforms.

With input from visa@, dlg@, and tedu@.

Several bugs squashed by visa@.

ok kettenis@


# 1.387 10-Jun-2020 jca

Give userland a consistent battery and A/C state after resume

Previously we refreshed the state of acpiac(4) and acpibat(4)/acpisbs(4)
by queueing fake ACPI events to be processed by the acpi thread.
acpibat(4) using ACPIDEV_POLL meant that its status could be refreshed
first, resulting in an APM_POWER_CHANGE event sent to userland before
the state of acpiac(4) was up to date. Because of this, apmd(8) could
see A/C unplugged when it was in fact plugged, possibly leading to
a wrong decision.

Instead of using fake ACPI events, refresh the status of acpiac(4) and
acpibat(4)/acpisbs(4) right away at DVACT_WAKEUP time. To reuse the
existing refresh code of those drivers, make their ACPI *_notify()
handlers responsible for sending APM_POWER_CHANGE events instead. Also
queue an APM_POWER_CHANGE event right after resume to retain existing
behavior.

This fixes the consistency of data shown to userland, and makes those
drivers less special.

Initially discussed last year (!) with mpi@ at Miod's place.
Feedback and ok jcs@, deraadt@ agrees with the use of DVACT_WAKEUP here.


# 1.386 29-May-2020 deraadt

dev/rndvar.h no longer has statistical interfaces (removed during various
conversion steps). it only contains kernel prototypes for 4 interfaces,
all of which legitimately belong in sys/systm.h, which are already included
by all enqueue_randomness() users.


# 1.385 14-May-2020 kettenis

Bring back the acpi(4) changes. This time around only use the ACPI _CRS
information on ACPI 5.0 and later.

ok krw@, patrick@


# 1.384 11-May-2020 jca

More accurate remaining power reporting with disparate batteries

Overall remaining power was computed as the average of the remaining
power of each battery, in percents. This doesn't work with batteries of
different capacity, so use the total capacity and remaining power
instead.

Reported by lidstah, successful tests by lidstah and benno@, ok kettenis@


# 1.383 08-May-2020 kettenis

Move parsing of _CRS into acpi(4) and pass the parsed address and interrupt
information in struct acpi_attach_args.

ok mpi@


Revision tags: OPENBSD_6_7_BASE
# 1.382 14-Apr-2020 kettenis

Implement acpi_getprop(), the ACPI equivalent of OF_getprop().

ok patrick@


# 1.381 12-Apr-2020 kettenis

Add support for the _CCA method. This method indicates whether DMA is
cache-coherent or not. To implement this, acpi(4) gets two bus_dma tags
and passes the appropriate one when attaching devices based on _CCA.
On i386/amd64, where for all practical purpose DMA is always cache-coherent,
the two tags are the same. But on arm64 they are distinct.

ok patrick@


# 1.380 07-Apr-2020 visa

Abstract the head of knote lists. This allows extending the lists,
for example, with locking assertions.

OK mpi@, anton@


# 1.379 03-Apr-2020 mpi

Fix incoherencies of errors returned by various *kqfilter routines.

EOPNOTSUPP is returned when there's no "filterops" for a given fd.
EINVAL when the requested filter isn't supported by the "filterops".
ENXIO means the underlying device is no longer valid/present.

ok visa@


# 1.378 20-Feb-2020 visa

Replace field f_isfd with field f_flags in struct filterops to allow
adding more filter properties without cluttering the struct.

OK mpi@, anton@


# 1.377 31-Dec-2019 visa

Use C99 designated initializers with struct filterops. In addition,
make the structs const so that the data are put in .rodata.

OK mpi@, deraadt@, anton@, bluhm@


# 1.376 31-Dec-2019 mpi

Convert infinite sleeps to {m,t}sleep_nsec(9).

ok kn@


# 1.375 31-Dec-2019 jsg

include sys/sensors.h for sensor use
don't depend on indirect inclusion from acpidev.h


Revision tags: OPENBSD_6_6_BASE
# 1.374 07-Sep-2019 kettenis

Revert acpipci(4) changes. Too many systems are broken. We'll try again
after 6.6 as been released. The acpireg.h change stays behind.


# 1.373 28-Aug-2019 kettenis

Use ACPI information to attach PCI busses like we do on arm64. There are a
few additional quirks though, and attaching the PCI busses is delayed to
replicate the existing code more closely. That may be changed in the
future. Also tweak how we handle MSI support and respect to ACPI flag
that says we shouldn't attempt to use MSIs.

Some fallout is expected.

ok patrick@


# 1.372 27-Aug-2019 deraadt

On amd64 the bootup and resume paths for SP/BP/SP were not performing
msr/pat, clock-startup, firmware upgrade, fpu initialization, and cpu
soft-state initialization in the same order. That kind of chaos is
crazy and fragile. Improve the acpi resume call sequence and unify order
of the operations.


# 1.371 02-Jul-2019 jcs

acpi_set_gpehandler: turn edge arg into flags, add support for
passing GPE_DIRECT which directly executes the supplied callback and
clears a level interrupt rather than going through acpi_taskq which
can be slow when other driver callbacks are in the queue.

This should not be used for GPE handlers that need to do anything
ACPI-related.

ok kettenis


# 1.370 10-Jun-2019 kettenis

Print proper ACPI version number.

ok mlarkin@


# 1.369 08-Jun-2019 kettenis

When establishing the mapping between ACPI device nodes and PCI devices,
skip PCI host bridges and devices that aren't there.

ok patrick@


# 1.368 07-Jun-2019 kettenis

Introduce acpi_getsta() and use it to eliminate some duplicated code.

ok patrick@


# 1.367 12-May-2019 kettenis

Make sure the extended PM register descriptions actually exist before
using them. Fixes machines with ancient ACPI 1.0 (such as recent
hypervisors).

ok deraadt@


# 1.366 12-May-2019 kettenis

Add support for mapping ACPI PM registers using the "extended" gasio
representation from the FADT. Mostly fixes the Lanner NCA-1510.

ok lteo@, mlarkin@, deraadt@


# 1.365 11-May-2019 lteo

Fix incorrect error message.

ok kettenis@


# 1.364 08-May-2019 tedu

free size. the allocation is the header plus the length.


Revision tags: OPENBSD_6_5_BASE
# 1.363 04-Apr-2019 kettenis

Avoid potential null-pointer dereference. Found by Kent R. Spillner.

ok deraadt@, jsg@


# 1.362 20-Jan-2019 jsg

fix SMALL_KERNEL build after changes in rev 1.361
reported by naddy@ ok deraadt@


# 1.361 19-Jan-2019 tedu

Add a pwraction sysctl that controls what the power button does on acpi.
By default, nothing changes -- shutdown is initiated. But allows turning
power button into a sleep button if desired.
(grudging) ok from a few parties


# 1.360 26-Oct-2018 kettenis

Add acpipci(4) on amd64. For now this only calls the PCI-specific _OSC
method to let the ACPI implementation know what features we support.


Revision tags: OPENBSD_6_4_BASE
# 1.359 30-Aug-2018 kettenis

Move softbutton check before the check that skips attaching certain acpi
devices. Fixes regression caused by matching on _CID in addition to
matching on _HID.

ok matthieu@, mlarkin@


# 1.358 25-Aug-2018 kettenis

Match ACPI devices based on _CID if no match for _HID is found.

ok mpi@


# 1.357 19-Aug-2018 kettenis

Add support for multiple PCI segments. Only really implemented for arm64
for now as amd64/i386 firmware still caters for legacy OSes that only
support a single PCI segment.

ok patrick@


# 1.356 03-Aug-2018 kettenis

Let ahci(4) match on _CLS instead of _HID when attaching at acpi(4). Avoids
having to add many more _HID entries to the match table.

ok deraadt@, mlarkin@


# 1.355 10-Jul-2018 kettenis

Make legacy interrupts work in acpipci(4).

ok patrick@


# 1.354 05-Jul-2018 kettenis

Add acpipci(4), a driver that supports generic ECAM-compatible PCI host
bridges based on information provided by ACPI.

ok mlarkin@


# 1.353 04-Jul-2018 kettenis

Properly pass around the PCI "chipset tag" in acpi(4) and refactor
acpimcfg(4) to call an MD initialization functions that sets up a tag for
PCI ECAM.

ok guenther@, mlarkin@, krw@


# 1.352 01-Jul-2018 kettenis

Move acpi(4) attach glue into acpi_machdep.c.

ok guenther@, deraadt@


# 1.351 01-Jul-2018 kettenis

Add support for _DSD Device Properties.

ok mlarkin@


# 1.350 30-Jun-2018 kettenis

Add intr_enable() function, intended for MI use to amd64 and i386 and use
this in the acpi(4) suspend/resume code paths.

ok deraadt@


# 1.349 29-Jun-2018 kettenis

Use <stdint.h> types.

ok mpi@, jung@, krw@, deraadt@


# 1.348 29-Jun-2018 mlarkin

ACPI: Allow (with warning) GPE handler reassignment, instead of returning
a failure code.

ok kettenis


# 1.347 26-Jun-2018 mlarkin

knf


# 1.346 26-Jun-2018 kettenis

Let acpi(4) pass down the bus dma tag.

ok deraadt@


# 1.345 25-Jun-2018 kettenis

Reorganize acpi(4) code a bit in preparation for upcoming arm64 support.

ok deraadt@


# 1.344 20-May-2018 kettenis

Wake up the acpi thread after scheduling a gpio event.

ok mpi@


# 1.343 17-May-2018 kettenis

Sprinkle some #ifndef SMALL_KERNEL such that RAMDISK kernels cuild again.

pointed out by stsp@


# 1.342 17-May-2018 kettenis

Implement GenericSerialBus OpRegion support.

ok mlarkin@


# 1.341 27-Mar-2018 kettenis

Add acpicmos(4), a driver that implements SystemCMOS OperationRegion
access support. This fixes machines where the AML doesn't check whether
support for this OperationRegion type has been registered by the OS.

ok mlarkin@


Revision tags: OPENBSD_6_3_BASE
# 1.340 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.339 10-Feb-2018 deraadt

Syncronize filesystems to disk when suspending. Each mountpoint's vnodes
are pushed to disk. Dangling vnodes (unlinked files still in use) and
vnodes undergoing change by long-running syscalls are identified -- and
such filesystems are marked dirty on-disk while we are suspended (in case
power is lost, a fsck will be required). Filesystems without dangling or
busy vnodes are marked clean, resulting in faster boots following
"battery died" circumstances.
Tested by numerous developers, thanks for the feedback.


# 1.338 08-Feb-2018 deraadt

After flushing disks, try again to release all memory. We don't need to
save dirty memory to the hibernate space.


# 1.337 08-Feb-2018 deraadt

Toss all releaseable memory, because fragmentation can get in the way
of allocating the hibernate playpen.


# 1.336 08-Feb-2018 deraadt

sr_quiesce() is a new approach for ensuring that softraid drains
output to the disks.

This is part of a larger suspend/resume filesystem-safety diff, which
has been worked on for a couple of months already. Tests by job, krw,
beck, benno, and others. Sometimes even by snapshot users...


# 1.335 29-Nov-2017 kettenis

Add support for GPIO-signaled events.

ok mlarkin@


# 1.334 16-Nov-2017 jcs

Add PCI attachment for dwiic(4) needed by Intel 100 Series machines

ok kettenis


# 1.333 14-Nov-2017 kettenis

Add a few more PNP IDs to the skip lists.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.332 17-Aug-2017 stsp

Make dwiic(4) attach its ACPI parent dependencies.
This change makes the Asus E200HA keyboard work.

Original analysis and patch by Cesare Gargano
ok kettenis@


# 1.331 09-Aug-2017 dcoppa

Add "PNP0303" (8042 PS/2 Controller) to acpi_isa_hids[]

ok kettenis@


# 1.330 22-Jul-2017 jcs

acpibat: look for _BIX first before falling back on _BIF

ACPI 4.0 deprecated _BIF for battery status so some newer machines
are exporting _BIX which extends _BIF's package with a few extra
fields. When using _BIX, export the new discharge cycle count as a
sensor.

feedback from kettenis and anton
ok deraadt


# 1.329 20-Jul-2017 kettenis

Start a list of isa(4) device names such that we can avoid printing them
as "not configured".

ok mikeb@, millert@, deraadt@


# 1.328 08-Apr-2017 deraadt

Sensors are run as callbacks inside tasks. During suspend a sensor
could be running inside a driver that will be force-detached, or due
to tsleep end up disrupting the softstate/hardstate contract. At
suspend time, quisce all these callbacks by waiting for completion.
This issue has never been observed for real, but may be implicated in
suspend/resume failures.
ok kettenis guenther mlarkin


# 1.327 08-Apr-2017 deraadt

Two prototypes for acpi_maptable; merge to one place


Revision tags: OPENBSD_6_1_BASE
# 1.326 27-Mar-2017 deraadt

Now that hibernate_alloc() only has clean success/failure, don't
need to call hibernate_free() to clean up a partial mess.
ok mlarkin kettenis


# 1.325 13-Mar-2017 deraadt

allow ramdisks to build; ok jcs


# 1.324 12-Mar-2017 jcs

try to attach acpisbs first and if successful, don't attach acpibat


# 1.323 02-Mar-2017 natano

Add a new sysctl machdep.lidaction. The sysctl works as follows:

machdep.lidaction=0 # do nothing
machdep.lidaction=1 # suspend
machdep.lidaction=2 # hibernate

lidsuspend is just an alias for lidaction, so if you change one, the
other one will have the same value. The plan is to remove
machdep.lidsuspend eventually when people have upgraded their
/ets/sysctl.conf.

discussed with deraadt, who came up with the new MIB name
no objections mlarkin
ok stsp halex jcs


# 1.322 28-Feb-2017 natano

Untangle abstract sleep modes from ACPI sleep states. This paves the way
for further work in the area.

idea and ok deraadt
ok mlarkin


# 1.321 22-Feb-2017 jcs

add acpisbs, an acpi smart battery subsystem driver reading data
over smbus

currently disabled because it conflicts with acpibat

ok deraadt, kettenis


# 1.320 14-Jan-2017 kettenis

Revert aml_rdpciaddr changes; breaks several machines that were working
before.


# 1.319 11-Jan-2017 jsg

Increase the size of the buffers used for _HID and _CID strings to be
able to fit "Hyper_V_Gen_Counter_V1".

ok mlarkin@ deraadt@


# 1.318 08-Jan-2017 kettenis

Make aml_rdpciaddr() work for for devices behind bridges. The code now
uses the mapping between AML nodes and PCI devices that we establish.
Because _INIT methods may end up calling aml_rdpciaddr(), make sure we
create that mapping early. Also handle devices that aren't actually present.
These devices are now included in the mapping and reads will return an
all-ones patterm whereas writes are a no-op..

ok mlarkin@


# 1.317 25-Oct-2016 pirofti

Add the resource index to aml_parse_resource() callback function.

This is needed by an upcoming acpiec commit that handles machines breaking
the current ACPI specifications.

Change suggested by and ok kettenis@, guenther@


# 1.316 18-Sep-2016 guenther

If the APIC table (MADT) isn't present then ci_acpi_proc_id won't be set
when acpicpu tries to attach, leading to a NULL dereference. The ACPI
processor object on the Dells where this was reported is bogus and useless
anyway so apply the same test when deciding whether to attach to processor
objects as acpicpu would when matching them to cpus.

Problem reported by Eivind Eide (xenofil (at) gmail.com) and
Johan Huldtgren (johan+openbsd-bugs (at) huldtgren.com).
Testing by Eivind Eide.
ok kettenis@ mlarkin@


# 1.315 03-Sep-2016 naddy

Write the system time back to the RTC every 30 minutes.
This fixes the problem that long-running machines which were not
shut down properly would reboot with a badly offset system time.

hints and ok kettenis@


# 1.314 31-Aug-2016 mlarkin

Don't put a device in the wake device list if _STA says it's not there.

Fixes at least one machine that woke up from sleep / poweroff immediately.

ok kettenis@, tested by kettenis and martijn@


# 1.313 28-Jul-2016 kettenis

Store the acpi processor ID/UID in struct cpu_info, and use it to attach
acpicpu(4) drivers to the right cpu(4).

ok mlarkin@, guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.312 10-Jun-2016 kettenis

The power management timer has been made optional in ACPI 5.0A, so don't
attempt to attach acpitimer(4) if the timer isn't present.

Pointed out by jcs@

ok jcs@


# 1.311 08-May-2016 kettenis

Make sure devices listed by a device's _DEP method are attached before
we attach the device itself. The _DEP methode, introduced in ACPI 5.0,
is a hint that the device in question depends on OpRegion support from
the devices listed by _DEP, so we have to order them the proper way.
To prevent us from attaching those devices again when we encounter them
later walking down the device tree, keep track of our attempts to
attach a device driver to them using aflag in the aml_node structure.

ok guenther@


# 1.310 25-Apr-2016 pirofti

We attach acpihpet(4) and acpiec(4) without using the HID, so do not
report it in the dmesg.

OK deraadt@, mlarkin@.


# 1.309 02-Apr-2016 kettenis

Reduce dmesg spam by knocking out some acpi hardware IDs that don't need a
driver.

ok mpi@


# 1.308 29-Mar-2016 kettenis

Check if a device is present (using _STA) in acpi_foundhid. This prevents
us from reporting devices as "not configured" that aren't actually present,
and allows us to remove duplicated code from several drivers.

ok deraadt@, ok jsg@


# 1.307 29-Mar-2016 kettenis

Implement a cleaner way to prevent acpivout(4) from attaching if we have
a platform-specific method of controlling the backlight.

ok millert@, mlarkin@, jung@


# 1.306 28-Mar-2016 kettenis

Adding each and every new acpi device driver to acpi_foundhid() is
gettinga bit of the burden. And it really isn't how our autoconf
framework is supposed to work for busses that can be enumerated. All
the drivers already check for a matching _HID in their attach
function. So we can just drop the checks in acpi_foundhid() and let
the drivers sort themselves out.

Print a "not configured" line for devices for which we don't attach adriver.
This may be a bit much as there are devices for which we will not have a
driver. This will be removed if it causes too much dmesg spam.

ok deraadt@, mlarkin@


Revision tags: OPENBSD_5_9_BASE
# 1.305 17-Jan-2016 jsg

Take acpi_foundhid() outside of #ifndef SMALL_KERNEL so the ramdisk
kernel can find and attach dwiic(4) and sdhc(4).


# 1.304 17-Jan-2016 jsg

remove an unused global variable


# 1.303 14-Jan-2016 kettenis

Attach dwiic(4) on the Intel Bay Trail i2c controllers as well.


# 1.302 12-Jan-2016 kettenis

Improve emulation of PM registers on Hardware-redcued ACPI by emulating
SPL_TYPx, SLP_EN and WAK_STS using the new SLEEP_STATUS_REG and
SLEEP_CONTROL_REG registers. Makes the transition into S5 (powerdown) work
on the ASUS X205TA.

ok deraadt@, mlarkin@


# 1.301 12-Jan-2016 jcs

Add dwiic, a driver for the Synopsys DesignWare i2c controller found
on the Samsung ATIV Book 9 laptop. This initial version only
supports ACPI config/attachment.

Add ihidev, a HID-over-i2c driver largely based on uhidev. dwiic
handles attaching ihidev devices found in ACPI.

Add ims, a HID-over-i2c mouse/trackpad driver to get basic cursor
and button functionality from HID-compliant i2c trackpads.

ok kettenis deraadt


# 1.300 11-Jan-2016 kettenis

Make sdhc(4) attach to hardware IDs "80860F14" and "PNP0FFF".


# 1.299 09-Jan-2016 kettenis

Initial support for Hardware-Reduced ACPI. This skips the legacy mode checks
by emulating the PM_CNT register such that SCI_EN is always enabled. It also
skip attaching acpitimer(4) since the relevant registers are no longer there.

ok deraadt@, mlarkin@


# 1.298 06-Jan-2016 kettenis

Follow the spec and check the SCI_EN bit to see if ACPI has already
been enabled and don't bother enabling it again if it is. This should fix
systems that don't implement legacy mode and therefore set ACPI_ENABLE and
ACPI_DISABLE to zero.

ok mlarkin@, yasuoka@


# 1.297 23-Nov-2015 reyk

Back out the CRITICAL change, turn it back from 10% to 25%. Some
people with old/broken batteries didn't like this change, and the
underlying heuristics are wrong.


# 1.296 22-Nov-2015 reyk

The "CRITICAL" APM battery level, that is exported/faked by the ACPI
stack, is currently set to 25%. With 25% battery left, a typical
modern laptop will have at least one hour runtime left. It is an
arbitrary value, and other APM power layers in the kernel set CRITICAL
to something like 5% or 10%, so set it to 10% for now.

OK mlarkin@


# 1.295 28-Sep-2015 deraadt

In low-level suspend routines, set cold=2. In tsleep(), use this to
spit out a ddb trace to console. This should allow us to find suspend
or resume routines which break the rules. It depends on the console
output function being non-sleeping.... but that's another codepath which
should try to be safe when cold is set.
ok kettenis


# 1.294 06-Sep-2015 deraadt

clarify malloc arguments (base + length...)


# 1.293 28-Aug-2015 deraadt

fairly simple sizes for free(); ok tedu


# 1.292 20-Aug-2015 kettenis

Respect the access size when reading or writing to pci config space.
This version also makes sure writes are properly aligned, preventing
the panics seen with the previous incarnation of this change.

Fixes battery status passthrough in vmware, brightness keys on my x220
and who knows what else.

ok mlarkin@, deraadt@


Revision tags: OPENBSD_5_8_BASE
# 1.291 04-Aug-2015 deraadt

If we are setting ACPI_PM1_BM_RLD do it only once, and remember to
repeat this action at resume time.
ok mlarkin guenther


# 1.290 29-Jul-2015 deraadt

Something subtle in previous revision is locking up suspend or booting
on some laptops. Mailing lists threads mention x220 and Asus machines.
Reverting for now.


# 1.289 21-Jul-2015 kettenis

iRespect the access size when reading or writing to pci config space.
Fixes battery status passthrough in vmware and who knows what else.

Tested by many.

ok mlarkin@, deraadt@


# 1.288 17-Jul-2015 mlarkin

fix comments


# 1.287 26-Mar-2015 jsg

Use the 0x10 extended capability to determine if the trackstick
buttons are wired to the trackpad and need to be re-routed to the
trackstick. Without this change the buttons on 2015 thinkpads
get picked up as extended buttons that show up as scroll up/down.

Remove the X1 Carbon 2015 (LEN0048) and X250 (LEN0046) from the
top button area/soft buttons quirks list. Also avoid using the
quirk list entirely if the capability bit is set.

Based on work from jcs@ with feedback from mpi@
ok mpi@ jcs@


# 1.286 20-Mar-2015 kettenis

The ACPI standard says that a PCI function number of FFFF can be used to refer
to all functions on a device. This makes no sense in the context of accessing
PCI config space. Nevertheless stsp@ has a machine with AML that tries to
do config space reads from a device where _ADR encodes function number FFFF.
This is almost certainly buggy AML, but since Linux (and presumably Windows)
boot just fine on this machine we need to work around this. Linux effectively
treats function FFFF as function 7, which isn't present on this machine.
Instead we simulate a read from a non-existent device by returning a bit
pattern of all ones. Writes to function FFFF will still panic.

ok mlarkin@


# 1.285 16-Mar-2015 deraadt

Reduce use of SMALL_KERNEL in acpi, bringing acpiec driver and more gpe
handling into RAMDISK. This is now possible because the install media
has ample room. The goal is to reduce special cases where we may be
skipping (unknown) important operations...
ok mlarkin kettenis


# 1.284 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.283 07-Feb-2015 deraadt

New framework that allows hibernate to pass in entropy from it's fresh
boot.
ok mlarkin


# 1.282 06-Feb-2015 mlarkin

Fix a hibernate crash on some machines due to unmapping a page that
may not have been mapped previously (in the failure to hibernate case).

Also ensure that the lowmem ptp is mapped in all cases (not just MP).

ok kettenis


# 1.281 17-Jan-2015 deraadt

resume_randomness() before spinning up other cpus...


# 1.280 15-Jan-2015 jsg

Define a new wscons mouse type for Synaptics clickpad devices that lack
physical buttons. This will be used if the acpi pnpid for the mouse
matches a list provided by Synaptics found in Linux.

Combined with the xenocara changes this will remove the need for an xorg
config file for the x240/t440/t540 generation of thinkpads.

ok matthieu@ shadchin@ kettenis@


# 1.279 11-Jan-2015 kettenis

Tolerate checksum errors in ACPI tables (but complain about them). Apparently
there are machines out there with broken BIOSen with such checksum errors in
thier RSDT tables. Windows runs fine on these machines, so it probably does
not check. Linux ignores checksum errors as well (and prints a warning).

ok deraadt@, krw@


# 1.278 18-Dec-2014 deraadt

suspend and resume the RNG (at what looks like the right spots)
ok reyk djm


# 1.277 09-Dec-2014 doug

Replace some malloc(n*size,...) calls with mallocarray().

ok tedu@ deraadt@


# 1.276 06-Dec-2014 mpi

If "machdep.lidsuspend" is not set, do not go back to sleep if the lid
is still closed when resuming. This allows docked laptop to be resumed
without opening them.

ok mlarkin@


# 1.275 23-Nov-2014 mlarkin

If a machine has no _LIDs, then none can be open. This diff fixes a problem
where machines without any _LIDs were going back to sleep after hibernate
due to the recent suspend-if-lid-closed-on-resume diff. Noticed on qemu,
which doesn't have any _LIDs.

discussed with deraadt


# 1.274 18-Nov-2014 krw

Nuke yet more obvious #include duplications.

ok miod@


# 1.273 08-Nov-2014 mlarkin

If resuming from sleep (zzz/ZZZ) and the lid is still closed, go back to
sleep. This prevents accidental lid flex or slight opening in a backpack
from waking the machine up and leaving it resumed (powered on).

ok deraadt@


# 1.272 26-Sep-2014 kettenis

Rework piglet and pig allocation. Currently the piglet gets allocated
deep down in the suspend path, where it is really hard to recover from
allocation failure. So allocate the piglet early on in the suspend path.
Also change the piglet and piglet allocation functions to use km_alloc(9)
instead of doing pmemrange magic. This removes a bunch of code which, in the
case of the piglet allocation, is broken since it results in a NULL pointer
dereference. Also switch the piglet allocation to not wait. If we can't
allocate 16MB of phys contig memory on a halfway modern machine we're almost
certainly under a lot of memory pressure and we're better off not trying to
hibernate anyway.

ok mlarkin@


# 1.271 25-Sep-2014 kettenis

Bring back rev 1.249. Now that mp_setperf() has been fixed, it should be
safe to restore perflevel when resuming. Should fix the issues people
see with the processor running at full speed heating up the machine while
the apm command reports that the CPUs are running at a low speed.

ok dcoppa@, deraadt@


# 1.270 23-Sep-2014 kettenis

Release the acpi lock around calls to wsdisplay_suspend() and
wsdisplay_resume(). The X server may adjust the display brightness just
before/after the VT switch when suspending/resuming. In fact, it may
already be blocked adjusting the display brightness for some reason
unrelated to the suspend request.

The VT switch request will eventually time out, but the delay is annoying
and the failed VT switch will lead to problems after resume.

Needed for a future update of the xf86-video-intel driver.

ok mlarkin@


# 1.269 19-Sep-2014 kettenis

Use config_suspend_all(9).


# 1.268 08-Sep-2014 kettenis

Clear the PME Status bit on PCI devices in response to Device Wake events.
Fixes the interrupt storm on the 2009 Mac mini with WOL enabled on its
nfe(4) interface.

ok mlarkin@


Revision tags: OPENBSD_5_6_BASE
# 1.267 20-Jul-2014 mlarkin

Support hibernating to softraid crypto volumes.

much help and ok from deraadt@


# 1.266 16-Jul-2014 mlarkin

Reenable hibernate RLE support and flush+zero all memory after unpack.


# 1.265 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.264 12-Jul-2014 kettenis

Add a function to drop all clean pages on the page daemon queues and call
it when we hibernate.

ok mlarkin@, miod@, deraadt@


# 1.263 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.262 11-Jul-2014 mlarkin

Flush the buffercache to 16MB on hibernate and restore its previous max
size (kern.bufcachepercent) on resume, for better hibernate performance.

ok beck@


# 1.261 10-Jul-2014 blambert

remove unneeded header

ok mlarkin@


# 1.260 01-Jul-2014 kettenis

Adding an additonal delay after each access to SystemMemory or SystemIO
during autoconf makes no sense.

ok deraadt@


# 1.259 23-Jun-2014 kettenis

SystemMemory is used to access memory mapped registers on some machines, so we
must use the bus_space(9) API to access it instead of memcpy(9). Also make
sure we properly align access to fields that need more than byte alignment.

ok guenther@


# 1.258 21-May-2014 mlarkin

Fix invalid / missing format specifiers in several dnprintf statements
(used only when ACPI_DEBUG is enabled)

From a diff on tech@ by Fabian Raetz, with a minor change by me, tested
on i386 and amd64 with ACPI_DEBUG enabled and disabled.


# 1.257 25-Apr-2014 mlarkin

Defer the free of the hibernate structures until later, when it is safe to
do so.

Freeing the hibernate structures too early leads to inadvertent calls into
splx when we aren't ready for them yet. Found when trapping splx calls in
the resume path.

discussed with deraadt@ at m2k14


# 1.256 13-Apr-2014 mlarkin

Fix a badly worded debug message that implied the entire machine was going
to sleep, instead of just the acpi thread going to sleep.


# 1.255 31-Mar-2014 mpi

Change the order of operations during a suspend/resume cycle and
call bufq_quiesce() after executing the DVACT_QUIESCE handlers.

This should be safe since no disk nor controller drivers have such
handler but it will allow us to detach sd(4) devices attached to a
USB bus.

Another benefit pointed out by kettenis@ is that drivers that need
to read a firmware from the disk should be able to do it at resume
time in a DVACT_WAKEUP handler.

ok kettenis@, deraadt@


# 1.254 13-Mar-2014 dlg

get rid of the assumption that the head of the alldevs list is the
"mainbus" device. this breaks when mpath is enabled because it
attaches before mainbus and therefore takes the head position.

have autoconf provide device_mainbus() which looks up mainbus_cd,
and use that instead.

discussed with deraadt who just wants mpath stuff to move forward
despite there being many ways to shine this particular turd.


Revision tags: OPENBSD_5_5_BASE
# 1.253 21-Feb-2014 deraadt

Do the lid control further on in the resume sequence, since this is
AML, and will do WAITOK operations. While cold, that is a bad idea.
We seem to be safe with respect to the spec, I do not see any event
debounce issues here.
ok kettenis, tested by many


# 1.252 20-Jan-2014 claudio

Instead of checking sc->sc_revision <= 1 check that the
fadt->hdr_revision <= 1. This is a better check since it checks
the version of the proper descriptor. This allows the alix APU
to reboot since those boards have currently a wrong sc->sc_revision.
Committing this now so that it can be tested easier.
With and OK jsg@ also fine by derradt@


# 1.251 24-Dec-2013 kettenis

If the FADT has its SMI_CMD set to zero, assume we're only ACPI-only hardware
and don't need to disable SMI ownership of the ACPI hardware registers.

ok mlarkin@


# 1.250 23-Dec-2013 kettenis

Revert previous commit. It makes resume hang on some MP systems running
GENERIC.MP.


# 1.249 16-Dec-2013 kettenis

Restore hw.setperf upon resume like we do for apm(4) on i386. Seems we need
to do this fairly late in the resume to avoid hangs.

ok deraadt@, mlarkin@


# 1.248 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


# 1.247 06-Nov-2013 mpi

Add support for Power Resources for Dx states and the necessary hook
for PCI devices. This hook should be called twice, before and after
changing the power state of a PCI device.

Before setting the device to the new state, the ACPI layer will notify
every power resources linked to the device for that state and make sure
they are turned "_ON". After changing the state of the device, it will
decrement the reference of every power resources linked to that device
for the old state and turn them "_OFF" if they are no longer referenced.

This fixes the no-USB after resume problem seen on various ThinkPad,
problem initialy diagnosed with Alexander Polakov.

ok kettenis@, deraadt@


Revision tags: OPENBSD_5_4_BASE
# 1.246 01-Jun-2013 mlarkin

Stop printing ACPI wakeup devices in dmesg after the 16th wakeup device.

Helps with VMware VMs that report hundreds of wakeup devices.

ok reyk@, deraadt@


# 1.245 31-May-2013 bluhm

Fix the build for a kernel without wd and pciide in its config.
OK deraadt@ tedu@


# 1.244 30-May-2013 deraadt

Enforce ca_activate tree-walks over the entire heirarchy for all events,
cleaning up some shutdown-hook related code on the way.
(A few drivers related to sparc are still skipped at kettenis' request)
ok kettenis mlarkin, tested by many others too


# 1.243 18-Apr-2013 deraadt

pin the acpi thread to the BSP, because AML and SMI cannot be trusted. I
was certain we were doing this already, but it turns out no...


Revision tags: OPENBSD_5_3_BASE
# 1.242 24-Dec-2012 guenther

In acpiioctl(), Return though the splx() path if suser() returns an error

ok deraadt@


# 1.241 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


# 1.240 04-Oct-2012 ehrhardt

Use information provided by ACPI to attach secondary PCI host bridges.

ok kettenis@


# 1.239 07-Sep-2012 kettenis

Add a new function called acpi_pci_min_powerstate() that returns the lowest
power state for a PCI device for the power state (currently S3 or S4) we're
transitioning into. This function is probably too simplistic; there is a lot
of (silly) complexity in the ACPI spec.

ok deraadt@


Revision tags: OPENBSD_5_2_BASE
# 1.238 13-Jul-2012 pirofti

Disable acpivideo(4) on ASUS laptops.

There are some models that panic whenever an event is received.
Some cope, some don't. In order to get this right acpiec(4) requires
major rework. Until then let machines boot.

Discussed with deraadt@.


# 1.237 11-Jul-2012 mlarkin

for now, restrict hibernate to root in the acpi ioctl kernel interface.
normal rules for controlling this interface through apmd still apply.

ok deraadt@


# 1.236 09-Jul-2012 deraadt

tiny little oops in untested hibernate block


# 1.235 09-Jul-2012 deraadt

clean up ioctl() case statements


# 1.234 09-Jul-2012 deraadt

acpi_disable_onegpe() is not used; ok pirofti


# 1.233 24-May-2012 kettenis

If _MIN > _MAX consider a WordBus resource to be invalid. Fixes a panic on
the IBM x3550-M4.

ok mikeb@


# 1.232 29-Mar-2012 mlarkin

Missing #ifdef in previous diff


# 1.231 29-Mar-2012 mlarkin

Prevent hibernation attempts on machines with unsupported disk controllers.
The same check is also performed later in the hibernate sequence, but by
then it's too late to cleanly unwind (presently).

ok deraadt


# 1.230 26-Mar-2012 deraadt

simplify conditionals in acpi_sleep_state()
ok mlarkin


# 1.229 26-Mar-2012 deraadt

hook in the hibernate request code; half of this diff is from mlarkin
ok mlarkin


Revision tags: OPENBSD_5_1_BASE
# 1.228 20-Sep-2011 deraadt

Very early in suspend while the clock is still ticking realtime,
save the clock back to the rtc. The zaurus already did this.
ok phessler miod kettenis


Revision tags: OPENBSD_5_0_BASE
# 1.227 02-Jul-2011 nicm

kqueue attach functions should return an errno or 0, not a plain 1. Fix
the obvious cases to return EINVAL and ENXIO.

ok tedu deraadt


# 1.226 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.225 16-Jun-2011 pirofti

Import acpitoshiba driver.

This is originally written by Hiroyuki Aizu, ported and integrated
in our tree by Javier Vazquez <javama4 AT gmail> with tweaks and
suggestions by me.

Import prodded by deraadt@ and the lack of testers on tech@.


# 1.224 27-Apr-2011 jcs

attach acpithinkpad to newer lenovo models like the x120e

ok deraadt@


# 1.223 22-Apr-2011 jordan

Fix uninitialzied variables and formatting strings (-Wxxx errors)


Revision tags: OPENBSD_4_9_BASE
# 1.222 02-Jan-2011 jordan

Re-add code for ACPI taskqueue, eliminates need for state vars in acpi softc
Serialize processing ACPI tasks for sleep/powerdown/GPE event
ok deraadt@


# 1.221 31-Oct-2010 guenther

Revert last commit: it breaks resume on Thinkpads
"then please back it out" deraadt@


# 1.220 26-Oct-2010 jordan

Add task queue for ACPI gpe and notify handlers
This fixes eject on Dell Latitude dock and an issue on the Dell Mini
battery update.
ok deraadt


# 1.219 07-Oct-2010 jordan

Create separate functions for enabling wake and runtime gpes.
Remove old suspend/resume gpewalk function
ok deraadt, mlarkin


# 1.218 05-Oct-2010 jordan

split acpi_enable_onegpe to separate enable/disable functions
ok deraadt


# 1.217 05-Oct-2010 jordan

Added acpi_disable_allgpes function to clear GPE status
ok deraadt


# 1.216 05-Oct-2010 deraadt

clean up acpi_softc use more


# 1.215 05-Oct-2010 deraadt

delete a bit of #if 0 code


# 1.214 05-Oct-2010 deraadt

hide use of our global acpi_softc pointer a bit better


# 1.213 29-Sep-2010 deraadt

grab the acpi thread lock very early in acpi thread startup; not that it
is likely that anyone will race us... but why be careless.
ok kettenis


# 1.212 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.211 29-Aug-2010 jasper

- add a check for FWRITE here too.

ok miod@


# 1.210 27-Aug-2010 kettenis

Make PCI Power Management optional, and only enable it when acpi(4) attaches.

ok deraadt@


Revision tags: OPENBSD_4_8_BASE
# 1.209 08-Aug-2010 kettenis

Close race between the acpi thread and wscons ioctls running in process
context. We might rip this out post-release and replace it with a solution
that serializes things in a more obvious way.

ok deraadt@, miod@ and "those crazy texans who ok anything"


# 1.208 08-Aug-2010 deraadt

Report the acpi sleep states when on a RAMDISK; ok krw


# 1.207 07-Aug-2010 kettenis

Enforce acpi register access rescrictions.

ok mlarkin@, deraadt@, tested by marco@


# 1.206 07-Aug-2010 deraadt

upon resume, notify all the acpiac and acpibat drivers to update
their status. some laptops do not do this automatically.
ok kettenis canacar


# 1.205 06-Aug-2010 marco

Always call _PSW on _LID devices that support it to enable lid open to
resume the machine. Conversely disable it when the machine wakes up.

Tested by several ok deraadt


# 1.204 06-Aug-2010 deraadt

As part of a revamp of the PM1/GPE code, I write this diff to look for
unmanaged EN & STS bits in the PM1 register at interrupt time and report
them. As a side effect this splits the STS acknowledgement into two writes
(for power, and sleep) instead of one. The printf that is added (to spot
unmanaged STS bits) has not yet been triggered as far as we know.

Before the "write to PM1 registers at the right offset" diff went in, this
was not neccessary. But newer thinkpads do not have a working soft power
button without this diff.

We have no idea why.
ok mlarkin kettenis


# 1.203 05-Aug-2010 deraadt

PM1 power button events were generating 'fake events' to acpibtn PWRB,
but if there is no such device these were being tossed. Instead, arrange
for all power button handlers to flag a softc variable which is processed
by the thread; so that one place will know to request a powerdown (psignal
to init)
ok marco mlarkin


# 1.202 05-Aug-2010 deraadt

rename a variable which is annoyingly poorly named


# 1.201 05-Aug-2010 mlarkin

Write to the correct offset for various ACPI pmreg registers. Writing to
the correct register does properly clear the wake status bits and fixed
event bits, which allows some machines to have more than 1 suspend cycle.

bug spotted by deraadt@

tested by deraadt@ and myself on a half-dozen machines and others on
hackers@

ok deraadt@, marco@


# 1.200 05-Aug-2010 deraadt

Fix the power button. The pm1 register is 16 bits in size.
My mistake, a while back.
tested by krw


# 1.199 04-Aug-2010 kettenis

Print supported sleep states.

ok marco@, phessler@, deraadt@


# 1.198 03-Aug-2010 mlarkin

Use the proper flag for re-enabling certain hardware events (power btn,
sleep btn). The (incorrect) flag prevoiously used coincidentally had the
same value, so this shouldn't cause any different behavior than before.

tested on a variety of machines (i386, amd64, sp, mp) - no behavioral
change seen

ok deraadt@


# 1.197 28-Jul-2010 marco

Make all gasio failures look and behave the same.


# 1.196 28-Jul-2010 mlarkin

Delay a little longer on suspend before giving up. Fixes machines that
sometimes don't suspend because the sleep transition takes too long
(symptoms like failing to sleep but the backlight stays on).

ok deraadt@


# 1.195 27-Jul-2010 deraadt

remove XXX; EC can now be attached early when needed; ok jordan


# 1.194 27-Jul-2010 deraadt

more brackets feels good


# 1.193 27-Jul-2010 deraadt

XXX annotate a block in acpi_enter_sleep_state() that is pretty worrying


# 1.192 27-Jul-2010 deraadt

horrific indentation that nearly made me puke


# 1.191 27-Jul-2010 mlarkin

Set the sleeping indicator light on machines that support it, in case SMI
doesn't do it for us. Shows the 'moon' LED on older Thinkpads when
sleeping.

Tested on T43p, W500, T510 by me and on X61s by deraadt@.

ok deraadt@


# 1.190 27-Jul-2010 jordan

Early initialization of acpiec if ECDT table exists
Fixes hang when booting thinkpads while docked
ok deraadt


# 1.189 26-Jul-2010 deraadt

remove merge error which was commited


# 1.188 26-Jul-2010 pirofti

Add support for Sony ACPI hotkeys via a new driver: acpisony(4).

Currently it only works for the suspend button and tries to do right for
the brightness events, but I haven't found a Sony laptop that like to do
right so far.

In the future I want to make the brightness keys work on all Sony's and
also add support for the zoom hotkeys and whatever other funky keys I can
find on those things.

Okay deraadt@.


# 1.187 22-Jul-2010 deraadt

pretty large cleanup of SMALL_KERNEL


# 1.186 21-Jul-2010 deraadt

in the acpi GPE handler, late ack level interrupts not edge interrupts.
Duh.


# 1.185 21-Jul-2010 deraadt

Merge the edge and level gpe handlers into one (now possible since the
gpe structure tells us whether the gpe is edge or level)


# 1.184 21-Jul-2010 deraadt

delete #if 0 code we are not going to use; ok marco


# 1.183 20-Jul-2010 deraadt

make the mask code for acpi_enable_onegpe() match other similar code


# 1.182 20-Jul-2010 deraadt

Handle the acpi interrupt controller with proper edge/level handling,
wrapped in spltty to avoid racing against the interrupt controller.
Repair the gpe bit masking code while there, and do operations in the
right order
ok kettenis mlarkin, help from jordan at figuring out the order of
operations.


# 1.181 20-Jul-2010 matthew

When we're preparing to go to sleep, only call bufq_restart() if other
preparations failed.

ok kettenis@, thib@, deraadt@


# 1.180 19-Jul-2010 deraadt

permit compilation with ACPI_DEBUG


# 1.179 19-Jul-2010 deraadt

there is no need for a seperate dsdt_softc pointer; dsdt does not have a
softc but is just a part of the single acpi driver.
ok jordan


# 1.178 19-Jul-2010 deraadt

acpi_delay() is no longer needed; ok jordan


# 1.177 18-Jul-2010 mlarkin

Clear event status on resume, to avoid going back to sleep/reboot/shutdown
immediately after resume. Tested by a bunch of people on hackers@ on
various hardware. Fixes at least Toshiba NB200 and Gateway LT3103u.

ok marco


# 1.176 13-Jul-2010 deraadt

rename some thread related interfaces so they are logical


# 1.175 10-Jul-2010 jordan

Verify _ADR bdf is correct (some systems return 0xffff)
Works on Dell x9xx and Studio 1555


# 1.174 09-Jul-2010 jordan

Clear the noise


# 1.173 08-Jul-2010 jordan

Add mapping for ACPI device to PCI bdf (match autoconf tree)
Simplify resource parsing function to use buffer argument
Convert namespace linked lists to use queue macros
ok marco@, deraadt@


# 1.172 06-Jul-2010 deraadt

Use spltty() to lock downcalls from apm against the information being
modified by the acpi thread.
ok kettenis


# 1.171 06-Jul-2010 deraadt

Don't use a workq to do the suspend, because it races aginst the acpi
thread. Instead, just tell the acpi thread to do the suspend for us.
This makes apmd & zzz work correctly. While here, have acpithinkpad
attempt to post the event to apm before prompting the suspend itself.
ok kettenis marco mlarkin


# 1.170 05-Jul-2010 mlarkin

Reset register SCI_EN on resume. The ACPI spec clearly states we don't have
to do this, but many machines don't, or forget to, which leads to all sorts
of problems on resume (broken battery stats, overheating, failure to resume
more than once, etc).

tested by many, ok marco@, deraadt@


# 1.169 01-Jul-2010 thib

do bufq quiesce again, since vnd/the bufq_done call in
biodone has been fixed.


# 1.168 01-Jul-2010 jordan

Backout recent AML changes, breaks on R210 and others


# 1.167 01-Jul-2010 jordan

Simplified aml_parse_resource call, cleanup code
ok marco


# 1.166 01-Jul-2010 jordan

Fix subordinate bus # for multi-root PCI buses
ok marco, kettenis


# 1.165 30-Jun-2010 thib

Disable/partially backout the bufq quiesce changes as this
is causing havoc with vnds and release must be buildable.


# 1.164 29-Jun-2010 deraadt

Fix ramdisks (new code was placed inside a SMALL_KERNEL chunk)


# 1.163 29-Jun-2010 jordan

Add support for mapping ACPI to PCI devices
ok kettenis, deraadt


# 1.162 29-Jun-2010 kettenis

Call bufq_quiesce() on suspend to stop all I/O and bufq_restart on resume.
Makes suspend/resume work much better while doing disk I/O.

ok mlarkin@


# 1.161 29-Jun-2010 tedu

60 is a more reasonable guess for number of minutes in an hour than 100.
spotted by TeXitoi. ok deraadt


# 1.160 27-Jun-2010 jordan

Add _acpi_matchhids routine, precursor to pci device match


# 1.159 27-Jun-2010 mlarkin

Clear ACPI fixed event status on resume (power buttons/etc) so that some
machines don't immediately resume after suspending to S3.

Tested by kettenis@ and I on 5 or 6 machines, deraadt@ "yeah, looks good"


# 1.158 27-Jun-2010 jordan

Cleaned up acpi_maptable, adds to table list directly


# 1.157 07-Apr-2010 deraadt

If we have done a wsdisplay_suspend(), and ACPI decides it does not want to
suspend, we must roll back by calling wsdisplay_resume() or the virtual
console switching will be locked forever
ok oga


# 1.156 07-Apr-2010 kettenis

Make suspend/resume work on MP machines (running an MP kernel). Joint work
from mlarkin@ and me, with some amd64 fixes thrown in by deraadt@

ok marco@, deraadt@, pirofti@, mlarkin@


# 1.155 31-Mar-2010 kettenis

Bring /dev/apm support in line with apm(4). Make the suspend button send
a suspend request event to apmd(8) instead of suspending immediately. Also
keep track of whether /dev/apm and /dev/apmctl are currently open, such that
we can still suspend immediately if apmd(8) isn't running.

ok deraadt@, marco@, pirofti@, jsing@, oga@


# 1.154 30-Mar-2010 oga

Prevent the apmd/x races for good.

When we hit suspend time, go through all wsdisplays on the system. if
they are in mode MAPPED, but not MODE_DUMBFB then if possible do a full
vt switch to a !mapped vt, and prevent switching back until resume time.
This has to be called from MD code because this involves userland
running so that X can run the vt switch signal handler. This way, any
case where we are using the "poke registers from userland" model, we
will not be on the hardware when we go down, so the kernel can actually
handle thing properly.

Tested on several acpi laptops (by kettenis@ and ian@), x40 (me and
beck@ at LEAST) and zaurus (me). Maybe others, but if so I forgot who at
this time..

Idea from deraadt somewhere over the Faroe Islands (I thought of a
similar thing myself a while ago). Much prompting from him. Ok and
comments miod@


# 1.153 25-Mar-2010 oga

Fix & vs. && in the kqfilter code.

Based on a comparison with the apm code.

ok deraadt@, kettenis@


Revision tags: OPENBSD_4_7_BASE
# 1.152 26-Nov-2009 mlarkin

Visual (and on some machines, audio) feedback while the machine is
resuming via calls to aml _SST method. This also makes the "moon" LED on
thinkpads to blink during resume and cease when resume is completed.

ok pirofti@


# 1.151 26-Nov-2009 deraadt

Toggle into "cold" when we we disable interrupts, because tsleeping
in the AML interpreter and context switching into userland processes
tends to sometimes pop the ACPI trampoline page out of our address
space. Makes lots more machines work.
ok kettenis mlarkin


# 1.150 24-Nov-2009 jsg

in the resume path evaluate _WAK before dealing with device drivers
like the spec says, not after.

ok mlarkin@


# 1.149 24-Nov-2009 deraadt

abort suspends on SMP systems until that gets written; ok mlarkin


# 1.148 23-Nov-2009 mlarkin

pci should return the result of it's children's suspend/resume calls back
to its parent.

handle suspend failure case and unwind if devices have an activate function
that returns failure.

ok deraadt@, kettenis@


# 1.147 23-Nov-2009 mlarkin

acpi* devices need to call their children on suspend/resume.
ok deraadt@


# 1.146 23-Nov-2009 pirofti

Remove ACPI_SLEEP_ENABLED checks.

This enables by default the suspend/resume paths in the kernel.

Okay deraadt@.


# 1.145 23-Nov-2009 deraadt

knf and spacing that are super annoying


# 1.144 23-Nov-2009 mlarkin

Hook up Theo's new children suspend/resume function, as well as more ACPI
suspend/resume glue.

ok deraadt@


# 1.143 26-Oct-2009 deraadt

Do not do strncmp comparisons on dv_xname because longer device names which
look similar could arrive in the future. Instead, compare directly against
dv_cfdata->cf_driver->cd_name
Issue originally spotted by miod


# 1.142 26-Oct-2009 jordan

Verify checksum+address when loading ACPI tables.
Some systems had invalid entries in RSDT/XSDT.
ok marco@


# 1.141 23-Jul-2009 cnst

New aibs(4) driver for ASUSTeK AI Booster (ACPI ATK0110) hardware monitoring,
with sensor state support through limits provided by the ACPI.

Tested on several ASUS motherboards kindly networked by Sam Fourman Jr.

ok deraadt marco jordan


Revision tags: OPENBSD_4_6_BASE
# 1.140 03-Jun-2009 pirofti

Import acpi power resource driver. This is needed for suspend/resume logic.

Okay jordan@ and marco@.


# 1.139 03-Jun-2009 pirofti

Add acpivideo support. This adds brightness support for all laptops
except thinkpads, they will use the acpithinkpad driver. The driver is
also hooked into wsconsole. So brightness can be adjusted via:

$ wsconsctl display.brightness=<percentage>

This is very helpfull on some laptops that have a nasty bios and get two
steps instead of one when pressing the brightness button.

Tested on various dell, fujitsu, acer, samsung and other laptops.

Okay marco@, miod@. Suggestions from kettenis@.
Lots of reviews and help from miod@, thanks!


# 1.138 03-Jun-2009 jordan

Added ejectable bay for IDE cdroms
ok marco@


# 1.137 30-Apr-2009 marco

Oga changed apm ioctls and forgot to grep. Temporary work around by hitting
the sleep button when we get any request. This interface will chnage at
some point.
ok oga


# 1.136 19-Apr-2009 krw

Call acpi_sleep_walk() when powering down just like when entering
any other sleep state. This tells acpi wake devices to not wake up
the box until manual powering up resets their status. Makes my Dell
GX520 stay powered off in response to 'halt -p'.

In snaps for a while.

ok marco@ (several times) deraadt@


# 1.135 17-Apr-2009 pirofti

Add device ranking support for future sleep/wake developments.

okay marco@


# 1.134 11-Mar-2009 jordan

Cleanup.. use aml_evalinteger instead of aml_evalname
ok marco@


# 1.133 10-Mar-2009 jordan

Fix cut/paste error.. call _BFS not _PTS
ok marco@


Revision tags: OPENBSD_4_5_BASE
# 1.132 21-Feb-2009 marco

overzealous cut/paste; found by Vladimir Kirillov <proger@uaoug.org.ua>


# 1.131 20-Feb-2009 marco

Oops didn't mean for Debugger() to remain there.


# 1.130 19-Feb-2009 marco

suspend/resume bits so that we can develop this in tree. This is disabled.
code from mlarkin and me
help from art,toby,jordan and several others
ok jordan, go for it deraadt


# 1.129 10-Feb-2009 jordan

Fix null pointer for shutdown if acpi not enabled
ok marco@


# 1.128 04-Feb-2009 kettenis

Some systems that claim to be ACPI 2.0 apparently don't provide an XSDT
address. Make the code fall back on the RSDT on those systems.

From Joerg Niendorf <p3y80@internode.on.net>. Fixes PR 5986.


# 1.127 17-Jan-2009 krw

A couple of stray malloc/memset -> malloc(M_ZERO), a bit of knf and
lose a couple of unnecessary casts. No functional change.

Feedback from kettenis@

ok kettenis@ marco@


# 1.126 06-Nov-2008 marco

Collapse all sanity checks of hids into one function.

Originaly from jordan


Revision tags: OPENBSD_4_4_BASE
# 1.125 02-Jul-2008 fgsch

add acpivideo. for now almost an empty skel so others can work on
suspend/resume. eventually it will also manage output switching and
brightness where supported. prodded by marco@


# 1.124 11-Jun-2008 marco

Fix $OpenBSD$ for once and for all


# 1.123 08-Jun-2008 deraadt

aaa_dev was being set from an uninitialized variable; ok toby


# 1.122 07-Jun-2008 marco

Kill ACPIVERBOSE. It was never used and is worthless to boot.

comments jmc
ok toby


# 1.121 06-Jun-2008 marco

Shrink acpi some more for floppies and while here re-enable acpi on the
floppies. Fixes crazy sony vaio kettenis has.

tested by deraadt
ok deraadt jordan kettenis


# 1.120 01-Jun-2008 marco

Fix \n nit in SMALL_KERNEL.


# 1.119 01-Jun-2008 marco

Re-enable new parser. Lots of testing by various folks. Fits all media
now too.

ok kettenis deraadt


# 1.118 16-May-2008 dlg

back out jordans new parser.

it didnt have enough oks from the right people, it did break machines
that were previously working, and it bloated the RAMDISKS and install
media beyond the constraints of the media.

requested by deraadt@


# 1.117 14-May-2008 jordan

Adding new ACPI Parser code
Old guts of code still exists, needs to be torn out next
ok marco@
ok brad@


# 1.116 27-Apr-2008 jcs

add acpithinkpad(4), an acpi support driver for ibm/lenovo
thinkpads.

provides functionality that the bios did on older, non-acpi
thinkpads like respond to screen brightness keys and toggle the
bluetooth device on and off.

ok deraadt@ and marco@


# 1.115 24-Apr-2008 jsing

Introduce acpiasus(4), a driver for the ACPI based hotkeys found in many
ASUS laptops (including the ASUS EeePC) - largely based on NetBSD's
asus(4) driver. On the ASUS EeePC this allows us to enable/disable
wireless, change screen brightness and use the volume keys.

ok jsg@, weingart@


# 1.114 12-Apr-2008 kettenis

Get rid of acpi_s5 global variables; simply send SIGUSR2 instead of SUGUSR1
to tell init(8) to power down the machine.

ok krw@


Revision tags: OPENBSD_4_3_BASE
# 1.113 21-Jan-2008 jsg

Check against RESET_REG_SUP in FADT before doing the acpi reset dance
after all, not doing so appears to break more machines than it fixes.

ok marco@


# 1.112 05-Jan-2008 kettenis

Never attach more acpicpu(4) devices than cpu(4) devices. Always attach
the first acpicpu(4) device on non-MULTIPROCESSOR kernels, regardless of its
ACPI processor ID.

ok mikeb@


# 1.111 05-Dec-2007 deraadt

Hang acpi(4) below bios(4) instead of mainbus(4). This lets us move acpi(4)
before pcibios(4), and then.... if acpi(4) attaches, skip pcibios(4) since
it messes the machines up. Fixes claudio's HP dl320 G5, and almost assuredly
others.
ok kettenis


# 1.110 05-Dec-2007 deraadt

repair acpiopen/acpiclose and friends to handle the /dev/apmctl interface,
so that apmd(8) can actually work correctly against acpi(4), and thus,
even do the fancy -C/-A stuff; also tested by jmc


# 1.109 02-Dec-2007 fgsch

fix the trailing space for real now.


# 1.108 02-Dec-2007 fgsch

avoid trailing spaces on output; marco@ ok.


# 1.107 27-Nov-2007 kettenis

Fix off-by-one that made us attach acpicpu0 and acpicpu1 on GENERIC kernels.

ok marco@


# 1.106 25-Nov-2007 jsg

When available and ACPI is enabled use the ACPI reset method
to reboot. This is required for some quirky sis machines
and likely others to reboot.

Tested by many. 'commit early commit often' deraadt@


# 1.105 19-Nov-2007 deraadt

sigh, amd64 still uses that stupid acpi_enabled variable...


# 1.104 19-Nov-2007 deraadt

softc's come initialized to 0, you know...


# 1.103 19-Nov-2007 deraadt

store revision; from jsg


# 1.102 19-Nov-2007 kettenis

Initialize sc_facs earlier.

ok deraadt@, beck@, canacar@


# 1.101 16-Nov-2007 deraadt

fix the bus_space #define nightmare, so that amd64 and i386 are much more
uniform. as a result shared code like acpi needs less #ifdef's
ok marco kettenis


# 1.100 15-Nov-2007 deraadt

remove the #ifdef ACPI_ENABLE. the EC codepath is either safe enough (and
will be improved), or it is unsafe because AML assumes it must be there
ok almost everyone


# 1.99 15-Nov-2007 mikeb

Make acpicpu(4) attach to the existing CPUs only.

Problem reported by form@. Fix idea by kettenis@.
Lots of help from deraadt@. Tested by deraadt@, form@ and me.

ok deraadt marco


# 1.98 14-Nov-2007 deraadt

spacing


# 1.97 12-Nov-2007 deraadt

correct : vs , in attach-time messages


# 1.96 06-Nov-2007 deraadt

all ACPI_ENABLE kernels need acpi_interrupt()


# 1.95 06-Nov-2007 deraadt

move acpi_read_pmreg() and acpi_write_pmreg() out of SMALL_KERNEL so
that acpi ramdisks can compile (obvious solution)


# 1.94 05-Nov-2007 canacar

Establish the acpi interrupt earlier. Fixes problems with some
GENERIC.MP kernels not getting acpi interrupts. This is a
workaround until the real issue with late establishment is fixed.
ok weingart@, beck@, gwk@


# 1.93 03-Nov-2007 jordan

Added support for displaying wakeup devices
ok beck@,weingart@,gwk@


# 1.92 11-Oct-2007 kettenis

When walking the tree yo find _PRT methods, check whether the device is
actually enabled and functioning. Similar to what we do for _INI.

ok weingart@, beck@


# 1.91 08-Oct-2007 krw

More simple memset(,0,) -> M_ZERO changes. In this batch move to
size(*p) as the first malloc() parameter where p is declared locally
and thus easy to check. Add M_ZERO to gpe_table allocation in acpi.c
even though there is no obvious bzero or memset nearby.


# 1.90 13-Sep-2007 mk

Remove unnecessary memset() (see aml_evalnode(9)) and a bit of KNF.

ok toby


# 1.89 13-Sep-2007 weingart

Implement ACPI 6.5.1 spec tree walk for _STA and _INI.
Fixes mk's laptop. No regressions so far (thank you to the
testers).

ok gwk@, mk@, marco@


Revision tags: OPENBSD_4_2_BASE
# 1.88 17-Apr-2007 mk

Now that acpidock no longer hangs machines during boot (at least
according to the reports I've been getting) it should be safe to enable
it again, so remove the #if 0 stuff around it.

ok marco


# 1.87 11-Apr-2007 jordan

Added changes for C-State
ok marco@


# 1.86 26-Mar-2007 marco

Calculate minutes left more accurately.

From giovanni <qgiovanni@gmail.com>


Revision tags: OPENBSD_4_1_BASE
# 1.85 03-Mar-2007 mk

Stop looking for acpidock until we fix that stuff that breaks all pre-60
series ThinkPads with acpidock(4) enabled.


# 1.84 22-Feb-2007 marco

We were getting no interrupts in .MP. There are 2 fixes; one in
acpi_machdep.c and move the interrupt establishment down after ioapic
has been initialized.

ok jordan


# 1.83 21-Feb-2007 kettenis

Make an acpi-enabled GENERIC.MP work on systems that only support PIC mode.

ok marco@


# 1.82 19-Feb-2007 jordan

Added acpi_map_address function for mapping GAS/IO registers
ok marco@


# 1.81 18-Feb-2007 jordan

Added dynamic perfect hash for opcodes
ok marco@


# 1.80 18-Feb-2007 marco

Shave off another K pre compression.
Shuffle SMALL_KERNEL functions around so that we only have a single #ifndef


# 1.79 17-Feb-2007 marco

Add SMALL_KERNEL throught acpi to make it fit on boot media.
ok deraadt


# 1.78 13-Feb-2007 marco

Add smarter _INI code. This helps with some dissapearing devices.

ok kettenis jordan


# 1.77 31-Jan-2007 gwk

Introduce acpi_hasprocfvs thats "ACPI Has Processor Frequency and Voltage
Scalling" a global flag set by acicpu attach to indicate that ACPI is
handling fvs through the _PSS objects. This will fix disappearing
EST/powernow when booting kernels with acpi enabled, as acpicpu isnt
compiled into GENERIC yet. Also in cases where acpicpu is built in and
legacy EST/powernow work but acpi is missing the _PSS object, the legacy
routines will be used.

tested by dim@, and Nick Nauwelaerts <nick AT nauwelaerts.net>
ok marco@, dim@


# 1.76 22-Jan-2007 mk

Actually look for docking stations by looking for _DCK nodes.

ok marco


# 1.75 02-Jan-2007 marco

On some systems EC must be attached as the first device. Move EC from
generic _HID devices to its own routine to force first attach. Bug found
and debugged by joris@

Code mostly written by joris@


# 1.74 26-Dec-2006 marco

Add polling to devices that require it such as acpibat and acpitz.
Use the same tsleep/wakeup mechanism as the interrupt code to keep it non-
concurrent.


# 1.73 21-Dec-2006 deraadt

even more knf love


# 1.72 21-Dec-2006 marco

Remove some leftover acpiec gpe/interrupt code.


# 1.71 20-Dec-2006 deraadt

oops, name already used


# 1.70 20-Dec-2006 deraadt

rename __acpi_enable_gpe(); ok marco


# 1.69 20-Dec-2006 deraadt

acpi_unmap_pmregs() is not used, ok marco


# 1.68 19-Dec-2006 deraadt

simplify some low-level methods substantially (shrinking code); ok marco jordan


# 1.67 06-Dec-2006 mk

malloc(..., M_NOWAIT) with no NULL check is bad. If allocation fails,
complain, clean up, and bail out of attach.

input and ok gwk


# 1.66 28-Nov-2006 jordan

Fixed clearing arg for acpi_init_pic
ok kettenis@


# 1.65 27-Nov-2006 kettenis

Don't spam dmesg.


# 1.64 27-Nov-2006 jordan

Fixes for acpiprt routing
Post-parse fixup for namerefs
ok kettenis@


# 1.63 25-Nov-2006 marco

Don't handle acpiev events inside the gpe handler since this causes deadlocks.

ok jordan@


# 1.62 15-Nov-2006 kettenis

Hook up acpi apic interrupt routing support to generic acpi code.

ok jordan@


# 1.61 02-Nov-2006 marco

Kill acpiec attach function and add its functionality to the generic _HID
device attach

prodded and ok jordan


# 1.60 02-Nov-2006 marco

Fix comments to reflect reality.


# 1.59 19-Oct-2006 marco

Replace ACPI ioctls with APM ioctls. This makes ACPI talk the same protocol
as APM therefore userspace can now retrieve power information in the same
fashion as all other apm arches (zaurus, macppc, i386 etc).

To use force the use of acpi device like so: apmd -f /dev/acpi

ok jordan@


# 1.58 19-Oct-2006 jordan

Fixed GPE debug levels
Fixed ref counter on temporary bufferfields
DSDT parsing fully working on IBM Thinkpad
ok marco@


# 1.57 15-Oct-2006 jordan

Removed old GPE handler code remnants
ok marco@


# 1.56 12-Oct-2006 jordan

Massive ACPI changes
Fixed ACPI leaks
new ACPI API
ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.55 30-Jun-2006 gwk

KNF, No functional change,
"go at it" marco@


# 1.54 30-Jun-2006 jordan

Support for calling Embedded Controller via new GPE interface
ok marco@


# 1.53 30-Jun-2006 jordan

Added new code for GPE handling
Also works with ACPIEC; need to fixup acpiec.c to remove original interrupt handler
ok marco@


# 1.52 30-Jun-2006 jordan

Added offset argument for acpi read/write pmreg routines (new GPE code)
ok marco@


# 1.51 31-May-2006 todd

do not call acpi_powerdown() unless acpi has successfully initialized
ok gwk@ canacar@


# 1.50 29-May-2006 canacar

Add embedded controller support.
ok marco@ deraadt@ testing & ok gwk@


# 1.49 19-May-2006 canacar

Minimal thermal zone driver. Monitors thermal zone temperature,
shuts down the system if the 'critical temperature' is reached.
ok marco@


# 1.48 11-Apr-2006 marco

KNF.


# 1.47 11-Apr-2006 gwk

Add acpi_resume(struct acpi_softc *sc), which calls some AML methods,
detected at attach time, currently unused.

ok marco@


# 1.46 11-Apr-2006 gwk

Discover some ACPI sleep related methods, and call them at the
appropriate time in acpi_enter_sleep_state().

ok marco@


Revision tags: OPENBSD_3_9_BASE
# 1.45 26-Feb-2006 marco

Add acpicpu as a device.

ok jordan@


# 1.44 23-Feb-2006 marco

remove dead function.

ok jordan@


# 1.43 22-Feb-2006 marco

Print out all tables it found during autoconf. We'll need this data in
the future.


# 1.42 22-Feb-2006 marco

Make dmesg less verbose.

ok jordan.


# 1.41 22-Feb-2006 jordan

Added parsing of SSDT tables
ok marco@


# 1.40 22-Feb-2006 jordan

Added new aml_register to notify by device id
Moved powerdown code to button device
ok marco@


# 1.39 21-Feb-2006 marco

Readjust debug levels that crept in too low over the last few days.
KNF


# 1.38 21-Feb-2006 marco

Rig the power button so that the machine shuts down correctly.


# 1.37 20-Feb-2006 marco

Add acpibtn device. Remains disabled for now.


# 1.36 19-Feb-2006 jordan

Added code for aml_notify GPE notification callback
ok marco@


# 1.35 19-Feb-2006 grange

Fix acpi_enter_sleep_state() by replacing current ugly hack
with mostly correct sleeping state transition code.

ok brad@ marco@


# 1.34 19-Feb-2006 marco

jordan:
Enable interrupts.
Added peripheral code to make this work.

marco:
Replace softintr and other types of interrupts with a handler with context.
Fixed interrupt magic for GPEs.
Some KNF

This is work done by jordan and I but has to be a single commit.


# 1.33 17-Feb-2006 marco

Remove earlier introduced delays. We really only need them during autconf.

ok jordan@


# 1.32 17-Feb-2006 marco

Add a generic delay function as per the ACPI spec.

from jordan@


# 1.31 17-Feb-2006 marco

Use tsleep instead of delay when !cold.


# 1.30 17-Feb-2006 marco

Argh, we need delays between reads/writes. The backend reads (i2c most likely)
are so slow that values come back as 0 sometimes. We have to do this
despite the spec saying we shouldnt have to. Guess having a faster
parser than others can hurt sometimes.

Idea from jordan.


# 1.29 17-Feb-2006 jordan

Fixed code for handling _INI
Rearranged debug statements
ok marco@


# 1.28 16-Feb-2006 jordan

Added stub for calling _INI
Fixed aml_dowhile for NULL return
ok marco@


# 1.27 16-Feb-2006 jordan

Fixed parser/evaluator to retrieve battery info+state
Cleanup of parser
Dell laptops return battery status
ok marco@


# 1.26 06-Feb-2006 brad

fix a typo so this will compile on amd64.


# 1.25 04-Feb-2006 marco

When ACPI_ENABLED wasn't set but the devices were it had an unknown variable.

Pointed out by ian@.


# 1.24 03-Feb-2006 jordan

Rewrote aml parser yet again...
Fixed bus_space_map for i386/amd64
Added _BST and _BIF to acpibat to get status/info
ok marco@


# 1.23 20-Jan-2006 jordan

More evaluator changes
Removed old evaluator code
Fixed _HID value
ok marco@


# 1.22 19-Jan-2006 jordan

Added support for read/write of PCI Config space
Help jason@
ok marco@


# 1.21 18-Jan-2006 jordan

Added new evaluation routines
Added helper methods for buffer/field evaluation
Fixed broken indentation on dsdt.c

ok marco@


# 1.20 17-Jan-2006 jordan

Major overhaul of the aml parser. Evaluater temporarily disabled.

ok marco@


# 1.19 06-Jan-2006 grange

Check for ACPI control availability only if ACPI_ENABLE defined.


# 1.18 06-Jan-2006 grange

Informative error messages on attach, prettier dmesg.


# 1.17 06-Jan-2006 grange

printf -> dnprintf conversion for debug messages.


# 1.16 05-Jan-2006 grange

Find SLP_TYPx values and store them for future use in
acpi_enter_sleep_state().


# 1.15 05-Jan-2006 grange

Random KNF and cleanup


# 1.14 05-Jan-2006 grange

Provide symbolic names for the acpi sleeping states and use it.


# 1.13 28-Dec-2005 marco

Moved DEVNAME to a acpidev.h

ok jordan@


# 1.12 18-Dec-2005 sturm

add back comment about wrong code

ok marco


# 1.11 16-Dec-2005 marco

Remove test for kbd_reset. Prompted deraadt@, ok jordan@


# 1.10 16-Dec-2005 jordan

Added hooks for powerdown/power button press


# 1.9 16-Dec-2005 jordan

Cleaned up common power management register i/o
Added enter sleep state ioctl
Hooked power button interrupt
ok marco@


# 1.8 16-Dec-2005 marco

Remove autoconf chatter


# 1.7 16-Dec-2005 jordan

Moved DSDT AML parsing into parent ACPI driver
ok marco@


# 1.6 14-Dec-2005 marco

Add AC device glue.


# 1.5 14-Dec-2005 marco

Adjust debug printing levels to something more sane.


# 1.4 13-Dec-2005 marco

Add glue to attach devices to apci.


# 1.3 07-Dec-2005 marco

KNF


Revision tags: OPENBSD_3_8_BASE
# 1.2 10-Jul-2005 grange

Spacing cleanup.


# 1.1 02-Jun-2005 tholo

Start on a basic ACPI framework -- does not do much more than read out the
ACPI tables into kernel memory and attach ACPI and HPET timers currently.

In order to test this code, enabling the devices in GENERIC as well as
the ACPI_ENABLE option is needed. This code does not do any thermal
control yet, so this should be done with care depending on the platform.

In the tree so more people can contribute to making this more fully
featured.

Ok niklas@ grange@ tedu@


# 1.418 13-Sep-2022 kettenis

Split out the code that collects data from acpiac(4), acpibat(4) and
acpisbs(4) for apm(4) and hook it up to the arm64 version of apm(4) on
systems with ACPI.

ok kn@


# 1.417 12-Sep-2022 kettenis

Add support for level-triggered GPIO events.

ok mlarkin@


# 1.416 10-Sep-2022 kettenis

Don't restrict GPIO events to pin numbers below 256. With the _EVT()
method events corresponding to larger numbers are possible.

Fixes lid state detection on the x13s.

ok patrick@


# 1.415 03-Sep-2022 kettenis

Make sure we don't suspend if we have no way to wake up the machine.
The idea is that device drivers that can wake up the machine register
themselves by calling device_register_wakeup(). To prevent regressions
on amd64, we let acpi(4) register itself as a wakeup device if the AML
for the machine defines wakeup devices. This may be refined in the
future.

This diff will prevent people from suspending their arm64 SBCs without
having a way to wake them up. For now the only driver that registers
itself is axppmic(4), which means that at this moment only SBCs with
an allwinner A64 or H5 SoC will actually support suspend/resume.

ok mlarkin@, deraadt@


# 1.414 10-Aug-2022 patrick

Provide the AML root in ACPI's softc so that drivers that need access
don't have to declare it using extern.

Suggested by and with feedback from kettenis@
Tested by deraadt@ on arm64, amd64 and i386
Tested by bluhm@ on amd64 and i386


Revision tags: OPENBSD_7_1_BASE
# 1.413 17-Feb-2022 jsg

reduce includes


# 1.412 10-Feb-2022 visa

Embed klist head in acpi_softc to avoid explicit malloc.

OK kettenis@


# 1.411 09-Feb-2022 deraadt

Move x86-only stubs used by kern/subr_suspend.c into dev/acpi/acpi_x86.c
for sharing between i386 and amd64.
ok mlarkin kettenis


# 1.410 08-Feb-2022 deraadt

The suspend/resume code is a sticky mess of MI, MD, and ACPI sequencing.
This splits out the MI sequencing, backing it with per-architecture helper
functions. Further steps will be neccesary because ACPI and MD are too
tightly coupled, but soon we'll be able to use this code for more architectures
(which depends on figuring out the lowest-level cpu sleeping method)
ok kettenis


# 1.409 04-Feb-2022 robert

set the APM_BATT_CHARGING state if the battery is being charged and try
to calculate the remaining time to be fully charged

ok jca@


# 1.408 02-Feb-2022 deraadt

acpi_addtask() calls malloc() w/ M_NOWAIT (because some calls come from
interrupt context), this however means occasional resource shortage will
result in callbacks registration failing, and unknown consequences for
the task-submitting caller.
Changing this to use pools with a low water mark, decreases the odds
of that problem occuring.
ok kettenis


# 1.407 01-Feb-2022 deraadt

GPE_DIRECT is calling the AML parser from interrupt context, which is
not permitted. Luckily nothing is using GPE_DIRECT anymore, so this code
can be deleted.
ok kettenis


# 1.406 26-Jan-2022 kettenis

An ACPI device needs to be both present and enabled for it to function.
So only attempt to attach hardware that has both bits enabled. This fixes
an issue where com(4) would attach for a disabled serial port leading to
misdetection of the hardware variant and a subsequent hang when /etc/rc
runs ttyflags -a.

ok anton@, deraadt@


# 1.405 12-Jan-2022 patrick

Make acpi_getpropint() return uint64_t, as ACPI integers are in fact that
wide and some _DSD properties depend on it.

ok kettenis@


# 1.404 04-Jan-2022 patrick

acpi_getprop() needs to actually make sure that we're looking at the
correct property. While there adjust acpi_getpropint() as well to
increase similarity with acpi_getprop().

ok kettenis@


# 1.403 16-Dec-2021 anton

Attach com over acpi on amd64. Some hardware uses a different interrupt
assignment compared to the the legacy one supported by com over isa.
This causes the console to halt once userland takes over as no
interrupts are received. The actual address and irq can be read from
ACPI, kettenis@ already added support for arm64 which paved the way for
amd64.

Some consoles that previously attached over isa are now expected to
attach over acpi.

Thanks to patrick@ for testing on arm64.

ok kettenis@


# 1.402 07-Dec-2021 kettenis

Fix parsing of SR_IRQ resource descriptors.

ok patrick@, anton@


# 1.401 02-Nov-2021 deraadt

knf


# 1.400 30-Oct-2021 deraadt

Change hw.perfpolicy=auto by default, at startup. If the system has AC
power connected (default is yes when no driver differentiates) then default
to 100% performance. On battery, use the existing auto algorithm (which is
admittedly somewhat unrefined).
This change overrides the system/BIOS speed and puts OpenBSD in control.
As this happens very early during boot, besides speedups in all usage usage
patterns, some surprises: unhibernate and sysupgrade times are cut in half.
note: on a few architectures, the setperf fn pointer is changed late, and
thus the auto algorithm stops timeing out. kettenis and i will look for
a solution.
in snaps for more than a week.
ok kettenis


Revision tags: OPENBSD_7_0_BASE
# 1.399 20-Jul-2021 mlarkin

Fix trailing whitespace in a few places.

No code change.


# 1.398 09-May-2021 kettenis

Some x86 machines advertise the "hardware reduced" ACPI feature, advertise
S4 and S5 support, but fail to populate the SLEEP_CONTROL_REG and
SLEEP_STATUS_REG descriptions in the FADT. An example of such a machine
is the ASUS Zenbook 14 UM433DA. Any attempt to powerdown the machine
will result in a kernel crash. It turns out that using the legacy ACPI PM
registers works fine on this machien. So fall back on those registers
if SLEEP_CONTROL_REG and/or SLEEP_STATUS_REG aren't provided.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_9_BASE
# 1.397 15-Mar-2021 patrick

Add acpi_iommu_device_map(), which replaces the DMA tag with one that
is blessed with IOMMU magic, if available. This is mainly for arm64,
since on amd64 and i386 the IOMMU only captures PCIe devices, as far
as I know, which uses the pci_probe_device_hook(). This though is for
non-PCI devices attached through ACPI.

ok kettenis@


# 1.396 07-Mar-2021 patrick

Pass standard DMA tag to acpi(4) table drivers.

ok kettenis@


# 1.395 27-Dec-2020 deraadt

acpi_map_address() cannot be wrapped by SMALL_KERNEL anymore, as
it is used by acpihpet.c


# 1.394 25-Dec-2020 visa

Refactor klist insertion and removal

Rename klist_{insert,remove}() to klist_{insert,remove}_locked().
These functions assume that the caller has locked the klist. The current
state of locking remains intact because the kernel lock is still used
with all klists.

Add new functions klist_insert() and klist_remove() that lock the klist
internally. This allows some code simplification.

OK mpi@


# 1.393 17-Dec-2020 kettenis

Fix some issues with referencing named ACPI nodes from Packages.
These references need to be resolved at runtime rather than when
they're parsed such that they pick up the right values for those nodes
which can be changed when for example _INI methods run.

The current approach is to replace these reference with a string that
names the node in question. The problem with that is that packages
can also contain normal strings. Which means that we need special
code that depends on the context where the package is used.

This diff takes a different approach by retaining a reference when
parsing. Code that uses a package will need to resolve this reference
but there is no ambiguiety anymore.

ok patrick@


# 1.392 05-Dec-2020 kettenis

Add support for the _EVT method to the GPIO support code.

ok patrick@


Revision tags: OPENBSD_6_8_BASE
# 1.391 27-Aug-2020 jmatthew

Reorder the acpi attach process so we look at Processor() CPU nodes
after Device() ones, since we should prefer the newer node type.
If we see any Device() nodes, don't attach acpicpu(4) to any Processor()
nodes if they're also present. This also makes acpitz(4) and
acpipwrres(4) devices attach slightly later.

ok kettenis@, also tested by jmc@


# 1.390 16-Aug-2020 gnezdo

Remove sc_maxgpe from acpi_softc which was never read from

Remove a dead conditional from acpi_init_gpes while there.

ok jcs@


# 1.389 21-Jul-2020 deraadt

acpi can use IPL_BIO (a low interrupt) since it only enqueues operations for
later processing. The use of a high interrupt will predate suspend/resume
efforts, we had to redesign acpi to be non-reentrant obviously
discussed with kettenis, in snaps for more than a week


# 1.388 24-Jun-2020 cheloha

kernel: use gettime(9)/getuptime(9) in lieu of time_second(9)/time_uptime(9)

time_second(9) and time_uptime(9) are widely used in the kernel to
quickly get the system UTC or system uptime as a time_t. However,
time_t is 64-bit everywhere, so it is not generally safe to use them
on 32-bit platforms: you have a split-read problem if your hardware
cannot perform atomic 64-bit reads.

This patch replaces time_second(9) with gettime(9), a safer successor
interface, throughout the kernel. Similarly, time_uptime(9) is replaced
with getuptime(9).

There is a performance cost on 32-bit platforms in exchange for
eliminating the split-read problem: instead of two register reads you
now have a lockless read loop to pull the values from the timehands.
This is really not *too* bad in the grand scheme of things, but
compared to what we were doing before it is several times slower.

There is no performance cost on 64-bit (__LP64__) platforms.

With input from visa@, dlg@, and tedu@.

Several bugs squashed by visa@.

ok kettenis@


# 1.387 10-Jun-2020 jca

Give userland a consistent battery and A/C state after resume

Previously we refreshed the state of acpiac(4) and acpibat(4)/acpisbs(4)
by queueing fake ACPI events to be processed by the acpi thread.
acpibat(4) using ACPIDEV_POLL meant that its status could be refreshed
first, resulting in an APM_POWER_CHANGE event sent to userland before
the state of acpiac(4) was up to date. Because of this, apmd(8) could
see A/C unplugged when it was in fact plugged, possibly leading to
a wrong decision.

Instead of using fake ACPI events, refresh the status of acpiac(4) and
acpibat(4)/acpisbs(4) right away at DVACT_WAKEUP time. To reuse the
existing refresh code of those drivers, make their ACPI *_notify()
handlers responsible for sending APM_POWER_CHANGE events instead. Also
queue an APM_POWER_CHANGE event right after resume to retain existing
behavior.

This fixes the consistency of data shown to userland, and makes those
drivers less special.

Initially discussed last year (!) with mpi@ at Miod's place.
Feedback and ok jcs@, deraadt@ agrees with the use of DVACT_WAKEUP here.


# 1.386 29-May-2020 deraadt

dev/rndvar.h no longer has statistical interfaces (removed during various
conversion steps). it only contains kernel prototypes for 4 interfaces,
all of which legitimately belong in sys/systm.h, which are already included
by all enqueue_randomness() users.


# 1.385 14-May-2020 kettenis

Bring back the acpi(4) changes. This time around only use the ACPI _CRS
information on ACPI 5.0 and later.

ok krw@, patrick@


# 1.384 11-May-2020 jca

More accurate remaining power reporting with disparate batteries

Overall remaining power was computed as the average of the remaining
power of each battery, in percents. This doesn't work with batteries of
different capacity, so use the total capacity and remaining power
instead.

Reported by lidstah, successful tests by lidstah and benno@, ok kettenis@


# 1.383 08-May-2020 kettenis

Move parsing of _CRS into acpi(4) and pass the parsed address and interrupt
information in struct acpi_attach_args.

ok mpi@


Revision tags: OPENBSD_6_7_BASE
# 1.382 14-Apr-2020 kettenis

Implement acpi_getprop(), the ACPI equivalent of OF_getprop().

ok patrick@


# 1.381 12-Apr-2020 kettenis

Add support for the _CCA method. This method indicates whether DMA is
cache-coherent or not. To implement this, acpi(4) gets two bus_dma tags
and passes the appropriate one when attaching devices based on _CCA.
On i386/amd64, where for all practical purpose DMA is always cache-coherent,
the two tags are the same. But on arm64 they are distinct.

ok patrick@


# 1.380 07-Apr-2020 visa

Abstract the head of knote lists. This allows extending the lists,
for example, with locking assertions.

OK mpi@, anton@


# 1.379 03-Apr-2020 mpi

Fix incoherencies of errors returned by various *kqfilter routines.

EOPNOTSUPP is returned when there's no "filterops" for a given fd.
EINVAL when the requested filter isn't supported by the "filterops".
ENXIO means the underlying device is no longer valid/present.

ok visa@


# 1.378 20-Feb-2020 visa

Replace field f_isfd with field f_flags in struct filterops to allow
adding more filter properties without cluttering the struct.

OK mpi@, anton@


# 1.377 31-Dec-2019 visa

Use C99 designated initializers with struct filterops. In addition,
make the structs const so that the data are put in .rodata.

OK mpi@, deraadt@, anton@, bluhm@


# 1.376 31-Dec-2019 mpi

Convert infinite sleeps to {m,t}sleep_nsec(9).

ok kn@


# 1.375 31-Dec-2019 jsg

include sys/sensors.h for sensor use
don't depend on indirect inclusion from acpidev.h


Revision tags: OPENBSD_6_6_BASE
# 1.374 07-Sep-2019 kettenis

Revert acpipci(4) changes. Too many systems are broken. We'll try again
after 6.6 as been released. The acpireg.h change stays behind.


# 1.373 28-Aug-2019 kettenis

Use ACPI information to attach PCI busses like we do on arm64. There are a
few additional quirks though, and attaching the PCI busses is delayed to
replicate the existing code more closely. That may be changed in the
future. Also tweak how we handle MSI support and respect to ACPI flag
that says we shouldn't attempt to use MSIs.

Some fallout is expected.

ok patrick@


# 1.372 27-Aug-2019 deraadt

On amd64 the bootup and resume paths for SP/BP/SP were not performing
msr/pat, clock-startup, firmware upgrade, fpu initialization, and cpu
soft-state initialization in the same order. That kind of chaos is
crazy and fragile. Improve the acpi resume call sequence and unify order
of the operations.


# 1.371 02-Jul-2019 jcs

acpi_set_gpehandler: turn edge arg into flags, add support for
passing GPE_DIRECT which directly executes the supplied callback and
clears a level interrupt rather than going through acpi_taskq which
can be slow when other driver callbacks are in the queue.

This should not be used for GPE handlers that need to do anything
ACPI-related.

ok kettenis


# 1.370 10-Jun-2019 kettenis

Print proper ACPI version number.

ok mlarkin@


# 1.369 08-Jun-2019 kettenis

When establishing the mapping between ACPI device nodes and PCI devices,
skip PCI host bridges and devices that aren't there.

ok patrick@


# 1.368 07-Jun-2019 kettenis

Introduce acpi_getsta() and use it to eliminate some duplicated code.

ok patrick@


# 1.367 12-May-2019 kettenis

Make sure the extended PM register descriptions actually exist before
using them. Fixes machines with ancient ACPI 1.0 (such as recent
hypervisors).

ok deraadt@


# 1.366 12-May-2019 kettenis

Add support for mapping ACPI PM registers using the "extended" gasio
representation from the FADT. Mostly fixes the Lanner NCA-1510.

ok lteo@, mlarkin@, deraadt@


# 1.365 11-May-2019 lteo

Fix incorrect error message.

ok kettenis@


# 1.364 08-May-2019 tedu

free size. the allocation is the header plus the length.


Revision tags: OPENBSD_6_5_BASE
# 1.363 04-Apr-2019 kettenis

Avoid potential null-pointer dereference. Found by Kent R. Spillner.

ok deraadt@, jsg@


# 1.362 20-Jan-2019 jsg

fix SMALL_KERNEL build after changes in rev 1.361
reported by naddy@ ok deraadt@


# 1.361 19-Jan-2019 tedu

Add a pwraction sysctl that controls what the power button does on acpi.
By default, nothing changes -- shutdown is initiated. But allows turning
power button into a sleep button if desired.
(grudging) ok from a few parties


# 1.360 26-Oct-2018 kettenis

Add acpipci(4) on amd64. For now this only calls the PCI-specific _OSC
method to let the ACPI implementation know what features we support.


Revision tags: OPENBSD_6_4_BASE
# 1.359 30-Aug-2018 kettenis

Move softbutton check before the check that skips attaching certain acpi
devices. Fixes regression caused by matching on _CID in addition to
matching on _HID.

ok matthieu@, mlarkin@


# 1.358 25-Aug-2018 kettenis

Match ACPI devices based on _CID if no match for _HID is found.

ok mpi@


# 1.357 19-Aug-2018 kettenis

Add support for multiple PCI segments. Only really implemented for arm64
for now as amd64/i386 firmware still caters for legacy OSes that only
support a single PCI segment.

ok patrick@


# 1.356 03-Aug-2018 kettenis

Let ahci(4) match on _CLS instead of _HID when attaching at acpi(4). Avoids
having to add many more _HID entries to the match table.

ok deraadt@, mlarkin@


# 1.355 10-Jul-2018 kettenis

Make legacy interrupts work in acpipci(4).

ok patrick@


# 1.354 05-Jul-2018 kettenis

Add acpipci(4), a driver that supports generic ECAM-compatible PCI host
bridges based on information provided by ACPI.

ok mlarkin@


# 1.353 04-Jul-2018 kettenis

Properly pass around the PCI "chipset tag" in acpi(4) and refactor
acpimcfg(4) to call an MD initialization functions that sets up a tag for
PCI ECAM.

ok guenther@, mlarkin@, krw@


# 1.352 01-Jul-2018 kettenis

Move acpi(4) attach glue into acpi_machdep.c.

ok guenther@, deraadt@


# 1.351 01-Jul-2018 kettenis

Add support for _DSD Device Properties.

ok mlarkin@


# 1.350 30-Jun-2018 kettenis

Add intr_enable() function, intended for MI use to amd64 and i386 and use
this in the acpi(4) suspend/resume code paths.

ok deraadt@


# 1.349 29-Jun-2018 kettenis

Use <stdint.h> types.

ok mpi@, jung@, krw@, deraadt@


# 1.348 29-Jun-2018 mlarkin

ACPI: Allow (with warning) GPE handler reassignment, instead of returning
a failure code.

ok kettenis


# 1.347 26-Jun-2018 mlarkin

knf


# 1.346 26-Jun-2018 kettenis

Let acpi(4) pass down the bus dma tag.

ok deraadt@


# 1.345 25-Jun-2018 kettenis

Reorganize acpi(4) code a bit in preparation for upcoming arm64 support.

ok deraadt@


# 1.344 20-May-2018 kettenis

Wake up the acpi thread after scheduling a gpio event.

ok mpi@


# 1.343 17-May-2018 kettenis

Sprinkle some #ifndef SMALL_KERNEL such that RAMDISK kernels cuild again.

pointed out by stsp@


# 1.342 17-May-2018 kettenis

Implement GenericSerialBus OpRegion support.

ok mlarkin@


# 1.341 27-Mar-2018 kettenis

Add acpicmos(4), a driver that implements SystemCMOS OperationRegion
access support. This fixes machines where the AML doesn't check whether
support for this OperationRegion type has been registered by the OS.

ok mlarkin@


Revision tags: OPENBSD_6_3_BASE
# 1.340 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.339 10-Feb-2018 deraadt

Syncronize filesystems to disk when suspending. Each mountpoint's vnodes
are pushed to disk. Dangling vnodes (unlinked files still in use) and
vnodes undergoing change by long-running syscalls are identified -- and
such filesystems are marked dirty on-disk while we are suspended (in case
power is lost, a fsck will be required). Filesystems without dangling or
busy vnodes are marked clean, resulting in faster boots following
"battery died" circumstances.
Tested by numerous developers, thanks for the feedback.


# 1.338 08-Feb-2018 deraadt

After flushing disks, try again to release all memory. We don't need to
save dirty memory to the hibernate space.


# 1.337 08-Feb-2018 deraadt

Toss all releaseable memory, because fragmentation can get in the way
of allocating the hibernate playpen.


# 1.336 08-Feb-2018 deraadt

sr_quiesce() is a new approach for ensuring that softraid drains
output to the disks.

This is part of a larger suspend/resume filesystem-safety diff, which
has been worked on for a couple of months already. Tests by job, krw,
beck, benno, and others. Sometimes even by snapshot users...


# 1.335 29-Nov-2017 kettenis

Add support for GPIO-signaled events.

ok mlarkin@


# 1.334 16-Nov-2017 jcs

Add PCI attachment for dwiic(4) needed by Intel 100 Series machines

ok kettenis


# 1.333 14-Nov-2017 kettenis

Add a few more PNP IDs to the skip lists.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.332 17-Aug-2017 stsp

Make dwiic(4) attach its ACPI parent dependencies.
This change makes the Asus E200HA keyboard work.

Original analysis and patch by Cesare Gargano
ok kettenis@


# 1.331 09-Aug-2017 dcoppa

Add "PNP0303" (8042 PS/2 Controller) to acpi_isa_hids[]

ok kettenis@


# 1.330 22-Jul-2017 jcs

acpibat: look for _BIX first before falling back on _BIF

ACPI 4.0 deprecated _BIF for battery status so some newer machines
are exporting _BIX which extends _BIF's package with a few extra
fields. When using _BIX, export the new discharge cycle count as a
sensor.

feedback from kettenis and anton
ok deraadt


# 1.329 20-Jul-2017 kettenis

Start a list of isa(4) device names such that we can avoid printing them
as "not configured".

ok mikeb@, millert@, deraadt@


# 1.328 08-Apr-2017 deraadt

Sensors are run as callbacks inside tasks. During suspend a sensor
could be running inside a driver that will be force-detached, or due
to tsleep end up disrupting the softstate/hardstate contract. At
suspend time, quisce all these callbacks by waiting for completion.
This issue has never been observed for real, but may be implicated in
suspend/resume failures.
ok kettenis guenther mlarkin


# 1.327 08-Apr-2017 deraadt

Two prototypes for acpi_maptable; merge to one place


Revision tags: OPENBSD_6_1_BASE
# 1.326 27-Mar-2017 deraadt

Now that hibernate_alloc() only has clean success/failure, don't
need to call hibernate_free() to clean up a partial mess.
ok mlarkin kettenis


# 1.325 13-Mar-2017 deraadt

allow ramdisks to build; ok jcs


# 1.324 12-Mar-2017 jcs

try to attach acpisbs first and if successful, don't attach acpibat


# 1.323 02-Mar-2017 natano

Add a new sysctl machdep.lidaction. The sysctl works as follows:

machdep.lidaction=0 # do nothing
machdep.lidaction=1 # suspend
machdep.lidaction=2 # hibernate

lidsuspend is just an alias for lidaction, so if you change one, the
other one will have the same value. The plan is to remove
machdep.lidsuspend eventually when people have upgraded their
/ets/sysctl.conf.

discussed with deraadt, who came up with the new MIB name
no objections mlarkin
ok stsp halex jcs


# 1.322 28-Feb-2017 natano

Untangle abstract sleep modes from ACPI sleep states. This paves the way
for further work in the area.

idea and ok deraadt
ok mlarkin


# 1.321 22-Feb-2017 jcs

add acpisbs, an acpi smart battery subsystem driver reading data
over smbus

currently disabled because it conflicts with acpibat

ok deraadt, kettenis


# 1.320 14-Jan-2017 kettenis

Revert aml_rdpciaddr changes; breaks several machines that were working
before.


# 1.319 11-Jan-2017 jsg

Increase the size of the buffers used for _HID and _CID strings to be
able to fit "Hyper_V_Gen_Counter_V1".

ok mlarkin@ deraadt@


# 1.318 08-Jan-2017 kettenis

Make aml_rdpciaddr() work for for devices behind bridges. The code now
uses the mapping between AML nodes and PCI devices that we establish.
Because _INIT methods may end up calling aml_rdpciaddr(), make sure we
create that mapping early. Also handle devices that aren't actually present.
These devices are now included in the mapping and reads will return an
all-ones patterm whereas writes are a no-op..

ok mlarkin@


# 1.317 25-Oct-2016 pirofti

Add the resource index to aml_parse_resource() callback function.

This is needed by an upcoming acpiec commit that handles machines breaking
the current ACPI specifications.

Change suggested by and ok kettenis@, guenther@


# 1.316 18-Sep-2016 guenther

If the APIC table (MADT) isn't present then ci_acpi_proc_id won't be set
when acpicpu tries to attach, leading to a NULL dereference. The ACPI
processor object on the Dells where this was reported is bogus and useless
anyway so apply the same test when deciding whether to attach to processor
objects as acpicpu would when matching them to cpus.

Problem reported by Eivind Eide (xenofil (at) gmail.com) and
Johan Huldtgren (johan+openbsd-bugs (at) huldtgren.com).
Testing by Eivind Eide.
ok kettenis@ mlarkin@


# 1.315 03-Sep-2016 naddy

Write the system time back to the RTC every 30 minutes.
This fixes the problem that long-running machines which were not
shut down properly would reboot with a badly offset system time.

hints and ok kettenis@


# 1.314 31-Aug-2016 mlarkin

Don't put a device in the wake device list if _STA says it's not there.

Fixes at least one machine that woke up from sleep / poweroff immediately.

ok kettenis@, tested by kettenis and martijn@


# 1.313 28-Jul-2016 kettenis

Store the acpi processor ID/UID in struct cpu_info, and use it to attach
acpicpu(4) drivers to the right cpu(4).

ok mlarkin@, guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.312 10-Jun-2016 kettenis

The power management timer has been made optional in ACPI 5.0A, so don't
attempt to attach acpitimer(4) if the timer isn't present.

Pointed out by jcs@

ok jcs@


# 1.311 08-May-2016 kettenis

Make sure devices listed by a device's _DEP method are attached before
we attach the device itself. The _DEP methode, introduced in ACPI 5.0,
is a hint that the device in question depends on OpRegion support from
the devices listed by _DEP, so we have to order them the proper way.
To prevent us from attaching those devices again when we encounter them
later walking down the device tree, keep track of our attempts to
attach a device driver to them using aflag in the aml_node structure.

ok guenther@


# 1.310 25-Apr-2016 pirofti

We attach acpihpet(4) and acpiec(4) without using the HID, so do not
report it in the dmesg.

OK deraadt@, mlarkin@.


# 1.309 02-Apr-2016 kettenis

Reduce dmesg spam by knocking out some acpi hardware IDs that don't need a
driver.

ok mpi@


# 1.308 29-Mar-2016 kettenis

Check if a device is present (using _STA) in acpi_foundhid. This prevents
us from reporting devices as "not configured" that aren't actually present,
and allows us to remove duplicated code from several drivers.

ok deraadt@, ok jsg@


# 1.307 29-Mar-2016 kettenis

Implement a cleaner way to prevent acpivout(4) from attaching if we have
a platform-specific method of controlling the backlight.

ok millert@, mlarkin@, jung@


# 1.306 28-Mar-2016 kettenis

Adding each and every new acpi device driver to acpi_foundhid() is
gettinga bit of the burden. And it really isn't how our autoconf
framework is supposed to work for busses that can be enumerated. All
the drivers already check for a matching _HID in their attach
function. So we can just drop the checks in acpi_foundhid() and let
the drivers sort themselves out.

Print a "not configured" line for devices for which we don't attach adriver.
This may be a bit much as there are devices for which we will not have a
driver. This will be removed if it causes too much dmesg spam.

ok deraadt@, mlarkin@


Revision tags: OPENBSD_5_9_BASE
# 1.305 17-Jan-2016 jsg

Take acpi_foundhid() outside of #ifndef SMALL_KERNEL so the ramdisk
kernel can find and attach dwiic(4) and sdhc(4).


# 1.304 17-Jan-2016 jsg

remove an unused global variable


# 1.303 14-Jan-2016 kettenis

Attach dwiic(4) on the Intel Bay Trail i2c controllers as well.


# 1.302 12-Jan-2016 kettenis

Improve emulation of PM registers on Hardware-redcued ACPI by emulating
SPL_TYPx, SLP_EN and WAK_STS using the new SLEEP_STATUS_REG and
SLEEP_CONTROL_REG registers. Makes the transition into S5 (powerdown) work
on the ASUS X205TA.

ok deraadt@, mlarkin@


# 1.301 12-Jan-2016 jcs

Add dwiic, a driver for the Synopsys DesignWare i2c controller found
on the Samsung ATIV Book 9 laptop. This initial version only
supports ACPI config/attachment.

Add ihidev, a HID-over-i2c driver largely based on uhidev. dwiic
handles attaching ihidev devices found in ACPI.

Add ims, a HID-over-i2c mouse/trackpad driver to get basic cursor
and button functionality from HID-compliant i2c trackpads.

ok kettenis deraadt


# 1.300 11-Jan-2016 kettenis

Make sdhc(4) attach to hardware IDs "80860F14" and "PNP0FFF".


# 1.299 09-Jan-2016 kettenis

Initial support for Hardware-Reduced ACPI. This skips the legacy mode checks
by emulating the PM_CNT register such that SCI_EN is always enabled. It also
skip attaching acpitimer(4) since the relevant registers are no longer there.

ok deraadt@, mlarkin@


# 1.298 06-Jan-2016 kettenis

Follow the spec and check the SCI_EN bit to see if ACPI has already
been enabled and don't bother enabling it again if it is. This should fix
systems that don't implement legacy mode and therefore set ACPI_ENABLE and
ACPI_DISABLE to zero.

ok mlarkin@, yasuoka@


# 1.297 23-Nov-2015 reyk

Back out the CRITICAL change, turn it back from 10% to 25%. Some
people with old/broken batteries didn't like this change, and the
underlying heuristics are wrong.


# 1.296 22-Nov-2015 reyk

The "CRITICAL" APM battery level, that is exported/faked by the ACPI
stack, is currently set to 25%. With 25% battery left, a typical
modern laptop will have at least one hour runtime left. It is an
arbitrary value, and other APM power layers in the kernel set CRITICAL
to something like 5% or 10%, so set it to 10% for now.

OK mlarkin@


# 1.295 28-Sep-2015 deraadt

In low-level suspend routines, set cold=2. In tsleep(), use this to
spit out a ddb trace to console. This should allow us to find suspend
or resume routines which break the rules. It depends on the console
output function being non-sleeping.... but that's another codepath which
should try to be safe when cold is set.
ok kettenis


# 1.294 06-Sep-2015 deraadt

clarify malloc arguments (base + length...)


# 1.293 28-Aug-2015 deraadt

fairly simple sizes for free(); ok tedu


# 1.292 20-Aug-2015 kettenis

Respect the access size when reading or writing to pci config space.
This version also makes sure writes are properly aligned, preventing
the panics seen with the previous incarnation of this change.

Fixes battery status passthrough in vmware, brightness keys on my x220
and who knows what else.

ok mlarkin@, deraadt@


Revision tags: OPENBSD_5_8_BASE
# 1.291 04-Aug-2015 deraadt

If we are setting ACPI_PM1_BM_RLD do it only once, and remember to
repeat this action at resume time.
ok mlarkin guenther


# 1.290 29-Jul-2015 deraadt

Something subtle in previous revision is locking up suspend or booting
on some laptops. Mailing lists threads mention x220 and Asus machines.
Reverting for now.


# 1.289 21-Jul-2015 kettenis

iRespect the access size when reading or writing to pci config space.
Fixes battery status passthrough in vmware and who knows what else.

Tested by many.

ok mlarkin@, deraadt@


# 1.288 17-Jul-2015 mlarkin

fix comments


# 1.287 26-Mar-2015 jsg

Use the 0x10 extended capability to determine if the trackstick
buttons are wired to the trackpad and need to be re-routed to the
trackstick. Without this change the buttons on 2015 thinkpads
get picked up as extended buttons that show up as scroll up/down.

Remove the X1 Carbon 2015 (LEN0048) and X250 (LEN0046) from the
top button area/soft buttons quirks list. Also avoid using the
quirk list entirely if the capability bit is set.

Based on work from jcs@ with feedback from mpi@
ok mpi@ jcs@


# 1.286 20-Mar-2015 kettenis

The ACPI standard says that a PCI function number of FFFF can be used to refer
to all functions on a device. This makes no sense in the context of accessing
PCI config space. Nevertheless stsp@ has a machine with AML that tries to
do config space reads from a device where _ADR encodes function number FFFF.
This is almost certainly buggy AML, but since Linux (and presumably Windows)
boot just fine on this machine we need to work around this. Linux effectively
treats function FFFF as function 7, which isn't present on this machine.
Instead we simulate a read from a non-existent device by returning a bit
pattern of all ones. Writes to function FFFF will still panic.

ok mlarkin@


# 1.285 16-Mar-2015 deraadt

Reduce use of SMALL_KERNEL in acpi, bringing acpiec driver and more gpe
handling into RAMDISK. This is now possible because the install media
has ample room. The goal is to reduce special cases where we may be
skipping (unknown) important operations...
ok mlarkin kettenis


# 1.284 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.283 07-Feb-2015 deraadt

New framework that allows hibernate to pass in entropy from it's fresh
boot.
ok mlarkin


# 1.282 06-Feb-2015 mlarkin

Fix a hibernate crash on some machines due to unmapping a page that
may not have been mapped previously (in the failure to hibernate case).

Also ensure that the lowmem ptp is mapped in all cases (not just MP).

ok kettenis


# 1.281 17-Jan-2015 deraadt

resume_randomness() before spinning up other cpus...


# 1.280 15-Jan-2015 jsg

Define a new wscons mouse type for Synaptics clickpad devices that lack
physical buttons. This will be used if the acpi pnpid for the mouse
matches a list provided by Synaptics found in Linux.

Combined with the xenocara changes this will remove the need for an xorg
config file for the x240/t440/t540 generation of thinkpads.

ok matthieu@ shadchin@ kettenis@


# 1.279 11-Jan-2015 kettenis

Tolerate checksum errors in ACPI tables (but complain about them). Apparently
there are machines out there with broken BIOSen with such checksum errors in
thier RSDT tables. Windows runs fine on these machines, so it probably does
not check. Linux ignores checksum errors as well (and prints a warning).

ok deraadt@, krw@


# 1.278 18-Dec-2014 deraadt

suspend and resume the RNG (at what looks like the right spots)
ok reyk djm


# 1.277 09-Dec-2014 doug

Replace some malloc(n*size,...) calls with mallocarray().

ok tedu@ deraadt@


# 1.276 06-Dec-2014 mpi

If "machdep.lidsuspend" is not set, do not go back to sleep if the lid
is still closed when resuming. This allows docked laptop to be resumed
without opening them.

ok mlarkin@


# 1.275 23-Nov-2014 mlarkin

If a machine has no _LIDs, then none can be open. This diff fixes a problem
where machines without any _LIDs were going back to sleep after hibernate
due to the recent suspend-if-lid-closed-on-resume diff. Noticed on qemu,
which doesn't have any _LIDs.

discussed with deraadt


# 1.274 18-Nov-2014 krw

Nuke yet more obvious #include duplications.

ok miod@


# 1.273 08-Nov-2014 mlarkin

If resuming from sleep (zzz/ZZZ) and the lid is still closed, go back to
sleep. This prevents accidental lid flex or slight opening in a backpack
from waking the machine up and leaving it resumed (powered on).

ok deraadt@


# 1.272 26-Sep-2014 kettenis

Rework piglet and pig allocation. Currently the piglet gets allocated
deep down in the suspend path, where it is really hard to recover from
allocation failure. So allocate the piglet early on in the suspend path.
Also change the piglet and piglet allocation functions to use km_alloc(9)
instead of doing pmemrange magic. This removes a bunch of code which, in the
case of the piglet allocation, is broken since it results in a NULL pointer
dereference. Also switch the piglet allocation to not wait. If we can't
allocate 16MB of phys contig memory on a halfway modern machine we're almost
certainly under a lot of memory pressure and we're better off not trying to
hibernate anyway.

ok mlarkin@


# 1.271 25-Sep-2014 kettenis

Bring back rev 1.249. Now that mp_setperf() has been fixed, it should be
safe to restore perflevel when resuming. Should fix the issues people
see with the processor running at full speed heating up the machine while
the apm command reports that the CPUs are running at a low speed.

ok dcoppa@, deraadt@


# 1.270 23-Sep-2014 kettenis

Release the acpi lock around calls to wsdisplay_suspend() and
wsdisplay_resume(). The X server may adjust the display brightness just
before/after the VT switch when suspending/resuming. In fact, it may
already be blocked adjusting the display brightness for some reason
unrelated to the suspend request.

The VT switch request will eventually time out, but the delay is annoying
and the failed VT switch will lead to problems after resume.

Needed for a future update of the xf86-video-intel driver.

ok mlarkin@


# 1.269 19-Sep-2014 kettenis

Use config_suspend_all(9).


# 1.268 08-Sep-2014 kettenis

Clear the PME Status bit on PCI devices in response to Device Wake events.
Fixes the interrupt storm on the 2009 Mac mini with WOL enabled on its
nfe(4) interface.

ok mlarkin@


Revision tags: OPENBSD_5_6_BASE
# 1.267 20-Jul-2014 mlarkin

Support hibernating to softraid crypto volumes.

much help and ok from deraadt@


# 1.266 16-Jul-2014 mlarkin

Reenable hibernate RLE support and flush+zero all memory after unpack.


# 1.265 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.264 12-Jul-2014 kettenis

Add a function to drop all clean pages on the page daemon queues and call
it when we hibernate.

ok mlarkin@, miod@, deraadt@


# 1.263 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.262 11-Jul-2014 mlarkin

Flush the buffercache to 16MB on hibernate and restore its previous max
size (kern.bufcachepercent) on resume, for better hibernate performance.

ok beck@


# 1.261 10-Jul-2014 blambert

remove unneeded header

ok mlarkin@


# 1.260 01-Jul-2014 kettenis

Adding an additonal delay after each access to SystemMemory or SystemIO
during autoconf makes no sense.

ok deraadt@


# 1.259 23-Jun-2014 kettenis

SystemMemory is used to access memory mapped registers on some machines, so we
must use the bus_space(9) API to access it instead of memcpy(9). Also make
sure we properly align access to fields that need more than byte alignment.

ok guenther@


# 1.258 21-May-2014 mlarkin

Fix invalid / missing format specifiers in several dnprintf statements
(used only when ACPI_DEBUG is enabled)

From a diff on tech@ by Fabian Raetz, with a minor change by me, tested
on i386 and amd64 with ACPI_DEBUG enabled and disabled.


# 1.257 25-Apr-2014 mlarkin

Defer the free of the hibernate structures until later, when it is safe to
do so.

Freeing the hibernate structures too early leads to inadvertent calls into
splx when we aren't ready for them yet. Found when trapping splx calls in
the resume path.

discussed with deraadt@ at m2k14


# 1.256 13-Apr-2014 mlarkin

Fix a badly worded debug message that implied the entire machine was going
to sleep, instead of just the acpi thread going to sleep.


# 1.255 31-Mar-2014 mpi

Change the order of operations during a suspend/resume cycle and
call bufq_quiesce() after executing the DVACT_QUIESCE handlers.

This should be safe since no disk nor controller drivers have such
handler but it will allow us to detach sd(4) devices attached to a
USB bus.

Another benefit pointed out by kettenis@ is that drivers that need
to read a firmware from the disk should be able to do it at resume
time in a DVACT_WAKEUP handler.

ok kettenis@, deraadt@


# 1.254 13-Mar-2014 dlg

get rid of the assumption that the head of the alldevs list is the
"mainbus" device. this breaks when mpath is enabled because it
attaches before mainbus and therefore takes the head position.

have autoconf provide device_mainbus() which looks up mainbus_cd,
and use that instead.

discussed with deraadt who just wants mpath stuff to move forward
despite there being many ways to shine this particular turd.


Revision tags: OPENBSD_5_5_BASE
# 1.253 21-Feb-2014 deraadt

Do the lid control further on in the resume sequence, since this is
AML, and will do WAITOK operations. While cold, that is a bad idea.
We seem to be safe with respect to the spec, I do not see any event
debounce issues here.
ok kettenis, tested by many


# 1.252 20-Jan-2014 claudio

Instead of checking sc->sc_revision <= 1 check that the
fadt->hdr_revision <= 1. This is a better check since it checks
the version of the proper descriptor. This allows the alix APU
to reboot since those boards have currently a wrong sc->sc_revision.
Committing this now so that it can be tested easier.
With and OK jsg@ also fine by derradt@


# 1.251 24-Dec-2013 kettenis

If the FADT has its SMI_CMD set to zero, assume we're only ACPI-only hardware
and don't need to disable SMI ownership of the ACPI hardware registers.

ok mlarkin@


# 1.250 23-Dec-2013 kettenis

Revert previous commit. It makes resume hang on some MP systems running
GENERIC.MP.


# 1.249 16-Dec-2013 kettenis

Restore hw.setperf upon resume like we do for apm(4) on i386. Seems we need
to do this fairly late in the resume to avoid hangs.

ok deraadt@, mlarkin@


# 1.248 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


# 1.247 06-Nov-2013 mpi

Add support for Power Resources for Dx states and the necessary hook
for PCI devices. This hook should be called twice, before and after
changing the power state of a PCI device.

Before setting the device to the new state, the ACPI layer will notify
every power resources linked to the device for that state and make sure
they are turned "_ON". After changing the state of the device, it will
decrement the reference of every power resources linked to that device
for the old state and turn them "_OFF" if they are no longer referenced.

This fixes the no-USB after resume problem seen on various ThinkPad,
problem initialy diagnosed with Alexander Polakov.

ok kettenis@, deraadt@


Revision tags: OPENBSD_5_4_BASE
# 1.246 01-Jun-2013 mlarkin

Stop printing ACPI wakeup devices in dmesg after the 16th wakeup device.

Helps with VMware VMs that report hundreds of wakeup devices.

ok reyk@, deraadt@


# 1.245 31-May-2013 bluhm

Fix the build for a kernel without wd and pciide in its config.
OK deraadt@ tedu@


# 1.244 30-May-2013 deraadt

Enforce ca_activate tree-walks over the entire heirarchy for all events,
cleaning up some shutdown-hook related code on the way.
(A few drivers related to sparc are still skipped at kettenis' request)
ok kettenis mlarkin, tested by many others too


# 1.243 18-Apr-2013 deraadt

pin the acpi thread to the BSP, because AML and SMI cannot be trusted. I
was certain we were doing this already, but it turns out no...


Revision tags: OPENBSD_5_3_BASE
# 1.242 24-Dec-2012 guenther

In acpiioctl(), Return though the splx() path if suser() returns an error

ok deraadt@


# 1.241 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


# 1.240 04-Oct-2012 ehrhardt

Use information provided by ACPI to attach secondary PCI host bridges.

ok kettenis@


# 1.239 07-Sep-2012 kettenis

Add a new function called acpi_pci_min_powerstate() that returns the lowest
power state for a PCI device for the power state (currently S3 or S4) we're
transitioning into. This function is probably too simplistic; there is a lot
of (silly) complexity in the ACPI spec.

ok deraadt@


Revision tags: OPENBSD_5_2_BASE
# 1.238 13-Jul-2012 pirofti

Disable acpivideo(4) on ASUS laptops.

There are some models that panic whenever an event is received.
Some cope, some don't. In order to get this right acpiec(4) requires
major rework. Until then let machines boot.

Discussed with deraadt@.


# 1.237 11-Jul-2012 mlarkin

for now, restrict hibernate to root in the acpi ioctl kernel interface.
normal rules for controlling this interface through apmd still apply.

ok deraadt@


# 1.236 09-Jul-2012 deraadt

tiny little oops in untested hibernate block


# 1.235 09-Jul-2012 deraadt

clean up ioctl() case statements


# 1.234 09-Jul-2012 deraadt

acpi_disable_onegpe() is not used; ok pirofti


# 1.233 24-May-2012 kettenis

If _MIN > _MAX consider a WordBus resource to be invalid. Fixes a panic on
the IBM x3550-M4.

ok mikeb@


# 1.232 29-Mar-2012 mlarkin

Missing #ifdef in previous diff


# 1.231 29-Mar-2012 mlarkin

Prevent hibernation attempts on machines with unsupported disk controllers.
The same check is also performed later in the hibernate sequence, but by
then it's too late to cleanly unwind (presently).

ok deraadt


# 1.230 26-Mar-2012 deraadt

simplify conditionals in acpi_sleep_state()
ok mlarkin


# 1.229 26-Mar-2012 deraadt

hook in the hibernate request code; half of this diff is from mlarkin
ok mlarkin


Revision tags: OPENBSD_5_1_BASE
# 1.228 20-Sep-2011 deraadt

Very early in suspend while the clock is still ticking realtime,
save the clock back to the rtc. The zaurus already did this.
ok phessler miod kettenis


Revision tags: OPENBSD_5_0_BASE
# 1.227 02-Jul-2011 nicm

kqueue attach functions should return an errno or 0, not a plain 1. Fix
the obvious cases to return EINVAL and ENXIO.

ok tedu deraadt


# 1.226 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.225 16-Jun-2011 pirofti

Import acpitoshiba driver.

This is originally written by Hiroyuki Aizu, ported and integrated
in our tree by Javier Vazquez <javama4 AT gmail> with tweaks and
suggestions by me.

Import prodded by deraadt@ and the lack of testers on tech@.


# 1.224 27-Apr-2011 jcs

attach acpithinkpad to newer lenovo models like the x120e

ok deraadt@


# 1.223 22-Apr-2011 jordan

Fix uninitialzied variables and formatting strings (-Wxxx errors)


Revision tags: OPENBSD_4_9_BASE
# 1.222 02-Jan-2011 jordan

Re-add code for ACPI taskqueue, eliminates need for state vars in acpi softc
Serialize processing ACPI tasks for sleep/powerdown/GPE event
ok deraadt@


# 1.221 31-Oct-2010 guenther

Revert last commit: it breaks resume on Thinkpads
"then please back it out" deraadt@


# 1.220 26-Oct-2010 jordan

Add task queue for ACPI gpe and notify handlers
This fixes eject on Dell Latitude dock and an issue on the Dell Mini
battery update.
ok deraadt


# 1.219 07-Oct-2010 jordan

Create separate functions for enabling wake and runtime gpes.
Remove old suspend/resume gpewalk function
ok deraadt, mlarkin


# 1.218 05-Oct-2010 jordan

split acpi_enable_onegpe to separate enable/disable functions
ok deraadt


# 1.217 05-Oct-2010 jordan

Added acpi_disable_allgpes function to clear GPE status
ok deraadt


# 1.216 05-Oct-2010 deraadt

clean up acpi_softc use more


# 1.215 05-Oct-2010 deraadt

delete a bit of #if 0 code


# 1.214 05-Oct-2010 deraadt

hide use of our global acpi_softc pointer a bit better


# 1.213 29-Sep-2010 deraadt

grab the acpi thread lock very early in acpi thread startup; not that it
is likely that anyone will race us... but why be careless.
ok kettenis


# 1.212 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.211 29-Aug-2010 jasper

- add a check for FWRITE here too.

ok miod@


# 1.210 27-Aug-2010 kettenis

Make PCI Power Management optional, and only enable it when acpi(4) attaches.

ok deraadt@


Revision tags: OPENBSD_4_8_BASE
# 1.209 08-Aug-2010 kettenis

Close race between the acpi thread and wscons ioctls running in process
context. We might rip this out post-release and replace it with a solution
that serializes things in a more obvious way.

ok deraadt@, miod@ and "those crazy texans who ok anything"


# 1.208 08-Aug-2010 deraadt

Report the acpi sleep states when on a RAMDISK; ok krw


# 1.207 07-Aug-2010 kettenis

Enforce acpi register access rescrictions.

ok mlarkin@, deraadt@, tested by marco@


# 1.206 07-Aug-2010 deraadt

upon resume, notify all the acpiac and acpibat drivers to update
their status. some laptops do not do this automatically.
ok kettenis canacar


# 1.205 06-Aug-2010 marco

Always call _PSW on _LID devices that support it to enable lid open to
resume the machine. Conversely disable it when the machine wakes up.

Tested by several ok deraadt


# 1.204 06-Aug-2010 deraadt

As part of a revamp of the PM1/GPE code, I write this diff to look for
unmanaged EN & STS bits in the PM1 register at interrupt time and report
them. As a side effect this splits the STS acknowledgement into two writes
(for power, and sleep) instead of one. The printf that is added (to spot
unmanaged STS bits) has not yet been triggered as far as we know.

Before the "write to PM1 registers at the right offset" diff went in, this
was not neccessary. But newer thinkpads do not have a working soft power
button without this diff.

We have no idea why.
ok mlarkin kettenis


# 1.203 05-Aug-2010 deraadt

PM1 power button events were generating 'fake events' to acpibtn PWRB,
but if there is no such device these were being tossed. Instead, arrange
for all power button handlers to flag a softc variable which is processed
by the thread; so that one place will know to request a powerdown (psignal
to init)
ok marco mlarkin


# 1.202 05-Aug-2010 deraadt

rename a variable which is annoyingly poorly named


# 1.201 05-Aug-2010 mlarkin

Write to the correct offset for various ACPI pmreg registers. Writing to
the correct register does properly clear the wake status bits and fixed
event bits, which allows some machines to have more than 1 suspend cycle.

bug spotted by deraadt@

tested by deraadt@ and myself on a half-dozen machines and others on
hackers@

ok deraadt@, marco@


# 1.200 05-Aug-2010 deraadt

Fix the power button. The pm1 register is 16 bits in size.
My mistake, a while back.
tested by krw


# 1.199 04-Aug-2010 kettenis

Print supported sleep states.

ok marco@, phessler@, deraadt@


# 1.198 03-Aug-2010 mlarkin

Use the proper flag for re-enabling certain hardware events (power btn,
sleep btn). The (incorrect) flag prevoiously used coincidentally had the
same value, so this shouldn't cause any different behavior than before.

tested on a variety of machines (i386, amd64, sp, mp) - no behavioral
change seen

ok deraadt@


# 1.197 28-Jul-2010 marco

Make all gasio failures look and behave the same.


# 1.196 28-Jul-2010 mlarkin

Delay a little longer on suspend before giving up. Fixes machines that
sometimes don't suspend because the sleep transition takes too long
(symptoms like failing to sleep but the backlight stays on).

ok deraadt@


# 1.195 27-Jul-2010 deraadt

remove XXX; EC can now be attached early when needed; ok jordan


# 1.194 27-Jul-2010 deraadt

more brackets feels good


# 1.193 27-Jul-2010 deraadt

XXX annotate a block in acpi_enter_sleep_state() that is pretty worrying


# 1.192 27-Jul-2010 deraadt

horrific indentation that nearly made me puke


# 1.191 27-Jul-2010 mlarkin

Set the sleeping indicator light on machines that support it, in case SMI
doesn't do it for us. Shows the 'moon' LED on older Thinkpads when
sleeping.

Tested on T43p, W500, T510 by me and on X61s by deraadt@.

ok deraadt@


# 1.190 27-Jul-2010 jordan

Early initialization of acpiec if ECDT table exists
Fixes hang when booting thinkpads while docked
ok deraadt


# 1.189 26-Jul-2010 deraadt

remove merge error which was commited


# 1.188 26-Jul-2010 pirofti

Add support for Sony ACPI hotkeys via a new driver: acpisony(4).

Currently it only works for the suspend button and tries to do right for
the brightness events, but I haven't found a Sony laptop that like to do
right so far.

In the future I want to make the brightness keys work on all Sony's and
also add support for the zoom hotkeys and whatever other funky keys I can
find on those things.

Okay deraadt@.


# 1.187 22-Jul-2010 deraadt

pretty large cleanup of SMALL_KERNEL


# 1.186 21-Jul-2010 deraadt

in the acpi GPE handler, late ack level interrupts not edge interrupts.
Duh.


# 1.185 21-Jul-2010 deraadt

Merge the edge and level gpe handlers into one (now possible since the
gpe structure tells us whether the gpe is edge or level)


# 1.184 21-Jul-2010 deraadt

delete #if 0 code we are not going to use; ok marco


# 1.183 20-Jul-2010 deraadt

make the mask code for acpi_enable_onegpe() match other similar code


# 1.182 20-Jul-2010 deraadt

Handle the acpi interrupt controller with proper edge/level handling,
wrapped in spltty to avoid racing against the interrupt controller.
Repair the gpe bit masking code while there, and do operations in the
right order
ok kettenis mlarkin, help from jordan at figuring out the order of
operations.


# 1.181 20-Jul-2010 matthew

When we're preparing to go to sleep, only call bufq_restart() if other
preparations failed.

ok kettenis@, thib@, deraadt@


# 1.180 19-Jul-2010 deraadt

permit compilation with ACPI_DEBUG


# 1.179 19-Jul-2010 deraadt

there is no need for a seperate dsdt_softc pointer; dsdt does not have a
softc but is just a part of the single acpi driver.
ok jordan


# 1.178 19-Jul-2010 deraadt

acpi_delay() is no longer needed; ok jordan


# 1.177 18-Jul-2010 mlarkin

Clear event status on resume, to avoid going back to sleep/reboot/shutdown
immediately after resume. Tested by a bunch of people on hackers@ on
various hardware. Fixes at least Toshiba NB200 and Gateway LT3103u.

ok marco


# 1.176 13-Jul-2010 deraadt

rename some thread related interfaces so they are logical


# 1.175 10-Jul-2010 jordan

Verify _ADR bdf is correct (some systems return 0xffff)
Works on Dell x9xx and Studio 1555


# 1.174 09-Jul-2010 jordan

Clear the noise


# 1.173 08-Jul-2010 jordan

Add mapping for ACPI device to PCI bdf (match autoconf tree)
Simplify resource parsing function to use buffer argument
Convert namespace linked lists to use queue macros
ok marco@, deraadt@


# 1.172 06-Jul-2010 deraadt

Use spltty() to lock downcalls from apm against the information being
modified by the acpi thread.
ok kettenis


# 1.171 06-Jul-2010 deraadt

Don't use a workq to do the suspend, because it races aginst the acpi
thread. Instead, just tell the acpi thread to do the suspend for us.
This makes apmd & zzz work correctly. While here, have acpithinkpad
attempt to post the event to apm before prompting the suspend itself.
ok kettenis marco mlarkin


# 1.170 05-Jul-2010 mlarkin

Reset register SCI_EN on resume. The ACPI spec clearly states we don't have
to do this, but many machines don't, or forget to, which leads to all sorts
of problems on resume (broken battery stats, overheating, failure to resume
more than once, etc).

tested by many, ok marco@, deraadt@


# 1.169 01-Jul-2010 thib

do bufq quiesce again, since vnd/the bufq_done call in
biodone has been fixed.


# 1.168 01-Jul-2010 jordan

Backout recent AML changes, breaks on R210 and others


# 1.167 01-Jul-2010 jordan

Simplified aml_parse_resource call, cleanup code
ok marco


# 1.166 01-Jul-2010 jordan

Fix subordinate bus # for multi-root PCI buses
ok marco, kettenis


# 1.165 30-Jun-2010 thib

Disable/partially backout the bufq quiesce changes as this
is causing havoc with vnds and release must be buildable.


# 1.164 29-Jun-2010 deraadt

Fix ramdisks (new code was placed inside a SMALL_KERNEL chunk)


# 1.163 29-Jun-2010 jordan

Add support for mapping ACPI to PCI devices
ok kettenis, deraadt


# 1.162 29-Jun-2010 kettenis

Call bufq_quiesce() on suspend to stop all I/O and bufq_restart on resume.
Makes suspend/resume work much better while doing disk I/O.

ok mlarkin@


# 1.161 29-Jun-2010 tedu

60 is a more reasonable guess for number of minutes in an hour than 100.
spotted by TeXitoi. ok deraadt


# 1.160 27-Jun-2010 jordan

Add _acpi_matchhids routine, precursor to pci device match


# 1.159 27-Jun-2010 mlarkin

Clear ACPI fixed event status on resume (power buttons/etc) so that some
machines don't immediately resume after suspending to S3.

Tested by kettenis@ and I on 5 or 6 machines, deraadt@ "yeah, looks good"


# 1.158 27-Jun-2010 jordan

Cleaned up acpi_maptable, adds to table list directly


# 1.157 07-Apr-2010 deraadt

If we have done a wsdisplay_suspend(), and ACPI decides it does not want to
suspend, we must roll back by calling wsdisplay_resume() or the virtual
console switching will be locked forever
ok oga


# 1.156 07-Apr-2010 kettenis

Make suspend/resume work on MP machines (running an MP kernel). Joint work
from mlarkin@ and me, with some amd64 fixes thrown in by deraadt@

ok marco@, deraadt@, pirofti@, mlarkin@


# 1.155 31-Mar-2010 kettenis

Bring /dev/apm support in line with apm(4). Make the suspend button send
a suspend request event to apmd(8) instead of suspending immediately. Also
keep track of whether /dev/apm and /dev/apmctl are currently open, such that
we can still suspend immediately if apmd(8) isn't running.

ok deraadt@, marco@, pirofti@, jsing@, oga@


# 1.154 30-Mar-2010 oga

Prevent the apmd/x races for good.

When we hit suspend time, go through all wsdisplays on the system. if
they are in mode MAPPED, but not MODE_DUMBFB then if possible do a full
vt switch to a !mapped vt, and prevent switching back until resume time.
This has to be called from MD code because this involves userland
running so that X can run the vt switch signal handler. This way, any
case where we are using the "poke registers from userland" model, we
will not be on the hardware when we go down, so the kernel can actually
handle thing properly.

Tested on several acpi laptops (by kettenis@ and ian@), x40 (me and
beck@ at LEAST) and zaurus (me). Maybe others, but if so I forgot who at
this time..

Idea from deraadt somewhere over the Faroe Islands (I thought of a
similar thing myself a while ago). Much prompting from him. Ok and
comments miod@


# 1.153 25-Mar-2010 oga

Fix & vs. && in the kqfilter code.

Based on a comparison with the apm code.

ok deraadt@, kettenis@


Revision tags: OPENBSD_4_7_BASE
# 1.152 26-Nov-2009 mlarkin

Visual (and on some machines, audio) feedback while the machine is
resuming via calls to aml _SST method. This also makes the "moon" LED on
thinkpads to blink during resume and cease when resume is completed.

ok pirofti@


# 1.151 26-Nov-2009 deraadt

Toggle into "cold" when we we disable interrupts, because tsleeping
in the AML interpreter and context switching into userland processes
tends to sometimes pop the ACPI trampoline page out of our address
space. Makes lots more machines work.
ok kettenis mlarkin


# 1.150 24-Nov-2009 jsg

in the resume path evaluate _WAK before dealing with device drivers
like the spec says, not after.

ok mlarkin@


# 1.149 24-Nov-2009 deraadt

abort suspends on SMP systems until that gets written; ok mlarkin


# 1.148 23-Nov-2009 mlarkin

pci should return the result of it's children's suspend/resume calls back
to its parent.

handle suspend failure case and unwind if devices have an activate function
that returns failure.

ok deraadt@, kettenis@


# 1.147 23-Nov-2009 mlarkin

acpi* devices need to call their children on suspend/resume.
ok deraadt@


# 1.146 23-Nov-2009 pirofti

Remove ACPI_SLEEP_ENABLED checks.

This enables by default the suspend/resume paths in the kernel.

Okay deraadt@.


# 1.145 23-Nov-2009 deraadt

knf and spacing that are super annoying


# 1.144 23-Nov-2009 mlarkin

Hook up Theo's new children suspend/resume function, as well as more ACPI
suspend/resume glue.

ok deraadt@


# 1.143 26-Oct-2009 deraadt

Do not do strncmp comparisons on dv_xname because longer device names which
look similar could arrive in the future. Instead, compare directly against
dv_cfdata->cf_driver->cd_name
Issue originally spotted by miod


# 1.142 26-Oct-2009 jordan

Verify checksum+address when loading ACPI tables.
Some systems had invalid entries in RSDT/XSDT.
ok marco@


# 1.141 23-Jul-2009 cnst

New aibs(4) driver for ASUSTeK AI Booster (ACPI ATK0110) hardware monitoring,
with sensor state support through limits provided by the ACPI.

Tested on several ASUS motherboards kindly networked by Sam Fourman Jr.

ok deraadt marco jordan


Revision tags: OPENBSD_4_6_BASE
# 1.140 03-Jun-2009 pirofti

Import acpi power resource driver. This is needed for suspend/resume logic.

Okay jordan@ and marco@.


# 1.139 03-Jun-2009 pirofti

Add acpivideo support. This adds brightness support for all laptops
except thinkpads, they will use the acpithinkpad driver. The driver is
also hooked into wsconsole. So brightness can be adjusted via:

$ wsconsctl display.brightness=<percentage>

This is very helpfull on some laptops that have a nasty bios and get two
steps instead of one when pressing the brightness button.

Tested on various dell, fujitsu, acer, samsung and other laptops.

Okay marco@, miod@. Suggestions from kettenis@.
Lots of reviews and help from miod@, thanks!


# 1.138 03-Jun-2009 jordan

Added ejectable bay for IDE cdroms
ok marco@


# 1.137 30-Apr-2009 marco

Oga changed apm ioctls and forgot to grep. Temporary work around by hitting
the sleep button when we get any request. This interface will chnage at
some point.
ok oga


# 1.136 19-Apr-2009 krw

Call acpi_sleep_walk() when powering down just like when entering
any other sleep state. This tells acpi wake devices to not wake up
the box until manual powering up resets their status. Makes my Dell
GX520 stay powered off in response to 'halt -p'.

In snaps for a while.

ok marco@ (several times) deraadt@


# 1.135 17-Apr-2009 pirofti

Add device ranking support for future sleep/wake developments.

okay marco@


# 1.134 11-Mar-2009 jordan

Cleanup.. use aml_evalinteger instead of aml_evalname
ok marco@


# 1.133 10-Mar-2009 jordan

Fix cut/paste error.. call _BFS not _PTS
ok marco@


Revision tags: OPENBSD_4_5_BASE
# 1.132 21-Feb-2009 marco

overzealous cut/paste; found by Vladimir Kirillov <proger@uaoug.org.ua>


# 1.131 20-Feb-2009 marco

Oops didn't mean for Debugger() to remain there.


# 1.130 19-Feb-2009 marco

suspend/resume bits so that we can develop this in tree. This is disabled.
code from mlarkin and me
help from art,toby,jordan and several others
ok jordan, go for it deraadt


# 1.129 10-Feb-2009 jordan

Fix null pointer for shutdown if acpi not enabled
ok marco@


# 1.128 04-Feb-2009 kettenis

Some systems that claim to be ACPI 2.0 apparently don't provide an XSDT
address. Make the code fall back on the RSDT on those systems.

From Joerg Niendorf <p3y80@internode.on.net>. Fixes PR 5986.


# 1.127 17-Jan-2009 krw

A couple of stray malloc/memset -> malloc(M_ZERO), a bit of knf and
lose a couple of unnecessary casts. No functional change.

Feedback from kettenis@

ok kettenis@ marco@


# 1.126 06-Nov-2008 marco

Collapse all sanity checks of hids into one function.

Originaly from jordan


Revision tags: OPENBSD_4_4_BASE
# 1.125 02-Jul-2008 fgsch

add acpivideo. for now almost an empty skel so others can work on
suspend/resume. eventually it will also manage output switching and
brightness where supported. prodded by marco@


# 1.124 11-Jun-2008 marco

Fix $OpenBSD$ for once and for all


# 1.123 08-Jun-2008 deraadt

aaa_dev was being set from an uninitialized variable; ok toby


# 1.122 07-Jun-2008 marco

Kill ACPIVERBOSE. It was never used and is worthless to boot.

comments jmc
ok toby


# 1.121 06-Jun-2008 marco

Shrink acpi some more for floppies and while here re-enable acpi on the
floppies. Fixes crazy sony vaio kettenis has.

tested by deraadt
ok deraadt jordan kettenis


# 1.120 01-Jun-2008 marco

Fix \n nit in SMALL_KERNEL.


# 1.119 01-Jun-2008 marco

Re-enable new parser. Lots of testing by various folks. Fits all media
now too.

ok kettenis deraadt


# 1.118 16-May-2008 dlg

back out jordans new parser.

it didnt have enough oks from the right people, it did break machines
that were previously working, and it bloated the RAMDISKS and install
media beyond the constraints of the media.

requested by deraadt@


# 1.117 14-May-2008 jordan

Adding new ACPI Parser code
Old guts of code still exists, needs to be torn out next
ok marco@
ok brad@


# 1.116 27-Apr-2008 jcs

add acpithinkpad(4), an acpi support driver for ibm/lenovo
thinkpads.

provides functionality that the bios did on older, non-acpi
thinkpads like respond to screen brightness keys and toggle the
bluetooth device on and off.

ok deraadt@ and marco@


# 1.115 24-Apr-2008 jsing

Introduce acpiasus(4), a driver for the ACPI based hotkeys found in many
ASUS laptops (including the ASUS EeePC) - largely based on NetBSD's
asus(4) driver. On the ASUS EeePC this allows us to enable/disable
wireless, change screen brightness and use the volume keys.

ok jsg@, weingart@


# 1.114 12-Apr-2008 kettenis

Get rid of acpi_s5 global variables; simply send SIGUSR2 instead of SUGUSR1
to tell init(8) to power down the machine.

ok krw@


Revision tags: OPENBSD_4_3_BASE
# 1.113 21-Jan-2008 jsg

Check against RESET_REG_SUP in FADT before doing the acpi reset dance
after all, not doing so appears to break more machines than it fixes.

ok marco@


# 1.112 05-Jan-2008 kettenis

Never attach more acpicpu(4) devices than cpu(4) devices. Always attach
the first acpicpu(4) device on non-MULTIPROCESSOR kernels, regardless of its
ACPI processor ID.

ok mikeb@


# 1.111 05-Dec-2007 deraadt

Hang acpi(4) below bios(4) instead of mainbus(4). This lets us move acpi(4)
before pcibios(4), and then.... if acpi(4) attaches, skip pcibios(4) since
it messes the machines up. Fixes claudio's HP dl320 G5, and almost assuredly
others.
ok kettenis


# 1.110 05-Dec-2007 deraadt

repair acpiopen/acpiclose and friends to handle the /dev/apmctl interface,
so that apmd(8) can actually work correctly against acpi(4), and thus,
even do the fancy -C/-A stuff; also tested by jmc


# 1.109 02-Dec-2007 fgsch

fix the trailing space for real now.


# 1.108 02-Dec-2007 fgsch

avoid trailing spaces on output; marco@ ok.


# 1.107 27-Nov-2007 kettenis

Fix off-by-one that made us attach acpicpu0 and acpicpu1 on GENERIC kernels.

ok marco@


# 1.106 25-Nov-2007 jsg

When available and ACPI is enabled use the ACPI reset method
to reboot. This is required for some quirky sis machines
and likely others to reboot.

Tested by many. 'commit early commit often' deraadt@


# 1.105 19-Nov-2007 deraadt

sigh, amd64 still uses that stupid acpi_enabled variable...


# 1.104 19-Nov-2007 deraadt

softc's come initialized to 0, you know...


# 1.103 19-Nov-2007 deraadt

store revision; from jsg


# 1.102 19-Nov-2007 kettenis

Initialize sc_facs earlier.

ok deraadt@, beck@, canacar@


# 1.101 16-Nov-2007 deraadt

fix the bus_space #define nightmare, so that amd64 and i386 are much more
uniform. as a result shared code like acpi needs less #ifdef's
ok marco kettenis


# 1.100 15-Nov-2007 deraadt

remove the #ifdef ACPI_ENABLE. the EC codepath is either safe enough (and
will be improved), or it is unsafe because AML assumes it must be there
ok almost everyone


# 1.99 15-Nov-2007 mikeb

Make acpicpu(4) attach to the existing CPUs only.

Problem reported by form@. Fix idea by kettenis@.
Lots of help from deraadt@. Tested by deraadt@, form@ and me.

ok deraadt marco


# 1.98 14-Nov-2007 deraadt

spacing


# 1.97 12-Nov-2007 deraadt

correct : vs , in attach-time messages


# 1.96 06-Nov-2007 deraadt

all ACPI_ENABLE kernels need acpi_interrupt()


# 1.95 06-Nov-2007 deraadt

move acpi_read_pmreg() and acpi_write_pmreg() out of SMALL_KERNEL so
that acpi ramdisks can compile (obvious solution)


# 1.94 05-Nov-2007 canacar

Establish the acpi interrupt earlier. Fixes problems with some
GENERIC.MP kernels not getting acpi interrupts. This is a
workaround until the real issue with late establishment is fixed.
ok weingart@, beck@, gwk@


# 1.93 03-Nov-2007 jordan

Added support for displaying wakeup devices
ok beck@,weingart@,gwk@


# 1.92 11-Oct-2007 kettenis

When walking the tree yo find _PRT methods, check whether the device is
actually enabled and functioning. Similar to what we do for _INI.

ok weingart@, beck@


# 1.91 08-Oct-2007 krw

More simple memset(,0,) -> M_ZERO changes. In this batch move to
size(*p) as the first malloc() parameter where p is declared locally
and thus easy to check. Add M_ZERO to gpe_table allocation in acpi.c
even though there is no obvious bzero or memset nearby.


# 1.90 13-Sep-2007 mk

Remove unnecessary memset() (see aml_evalnode(9)) and a bit of KNF.

ok toby


# 1.89 13-Sep-2007 weingart

Implement ACPI 6.5.1 spec tree walk for _STA and _INI.
Fixes mk's laptop. No regressions so far (thank you to the
testers).

ok gwk@, mk@, marco@


Revision tags: OPENBSD_4_2_BASE
# 1.88 17-Apr-2007 mk

Now that acpidock no longer hangs machines during boot (at least
according to the reports I've been getting) it should be safe to enable
it again, so remove the #if 0 stuff around it.

ok marco


# 1.87 11-Apr-2007 jordan

Added changes for C-State
ok marco@


# 1.86 26-Mar-2007 marco

Calculate minutes left more accurately.

From giovanni <qgiovanni@gmail.com>


Revision tags: OPENBSD_4_1_BASE
# 1.85 03-Mar-2007 mk

Stop looking for acpidock until we fix that stuff that breaks all pre-60
series ThinkPads with acpidock(4) enabled.


# 1.84 22-Feb-2007 marco

We were getting no interrupts in .MP. There are 2 fixes; one in
acpi_machdep.c and move the interrupt establishment down after ioapic
has been initialized.

ok jordan


# 1.83 21-Feb-2007 kettenis

Make an acpi-enabled GENERIC.MP work on systems that only support PIC mode.

ok marco@


# 1.82 19-Feb-2007 jordan

Added acpi_map_address function for mapping GAS/IO registers
ok marco@


# 1.81 18-Feb-2007 jordan

Added dynamic perfect hash for opcodes
ok marco@


# 1.80 18-Feb-2007 marco

Shave off another K pre compression.
Shuffle SMALL_KERNEL functions around so that we only have a single #ifndef


# 1.79 17-Feb-2007 marco

Add SMALL_KERNEL throught acpi to make it fit on boot media.
ok deraadt


# 1.78 13-Feb-2007 marco

Add smarter _INI code. This helps with some dissapearing devices.

ok kettenis jordan


# 1.77 31-Jan-2007 gwk

Introduce acpi_hasprocfvs thats "ACPI Has Processor Frequency and Voltage
Scalling" a global flag set by acicpu attach to indicate that ACPI is
handling fvs through the _PSS objects. This will fix disappearing
EST/powernow when booting kernels with acpi enabled, as acpicpu isnt
compiled into GENERIC yet. Also in cases where acpicpu is built in and
legacy EST/powernow work but acpi is missing the _PSS object, the legacy
routines will be used.

tested by dim@, and Nick Nauwelaerts <nick AT nauwelaerts.net>
ok marco@, dim@


# 1.76 22-Jan-2007 mk

Actually look for docking stations by looking for _DCK nodes.

ok marco


# 1.75 02-Jan-2007 marco

On some systems EC must be attached as the first device. Move EC from
generic _HID devices to its own routine to force first attach. Bug found
and debugged by joris@

Code mostly written by joris@


# 1.74 26-Dec-2006 marco

Add polling to devices that require it such as acpibat and acpitz.
Use the same tsleep/wakeup mechanism as the interrupt code to keep it non-
concurrent.


# 1.73 21-Dec-2006 deraadt

even more knf love


# 1.72 21-Dec-2006 marco

Remove some leftover acpiec gpe/interrupt code.


# 1.71 20-Dec-2006 deraadt

oops, name already used


# 1.70 20-Dec-2006 deraadt

rename __acpi_enable_gpe(); ok marco


# 1.69 20-Dec-2006 deraadt

acpi_unmap_pmregs() is not used, ok marco


# 1.68 19-Dec-2006 deraadt

simplify some low-level methods substantially (shrinking code); ok marco jordan


# 1.67 06-Dec-2006 mk

malloc(..., M_NOWAIT) with no NULL check is bad. If allocation fails,
complain, clean up, and bail out of attach.

input and ok gwk


# 1.66 28-Nov-2006 jordan

Fixed clearing arg for acpi_init_pic
ok kettenis@


# 1.65 27-Nov-2006 kettenis

Don't spam dmesg.


# 1.64 27-Nov-2006 jordan

Fixes for acpiprt routing
Post-parse fixup for namerefs
ok kettenis@


# 1.63 25-Nov-2006 marco

Don't handle acpiev events inside the gpe handler since this causes deadlocks.

ok jordan@


# 1.62 15-Nov-2006 kettenis

Hook up acpi apic interrupt routing support to generic acpi code.

ok jordan@


# 1.61 02-Nov-2006 marco

Kill acpiec attach function and add its functionality to the generic _HID
device attach

prodded and ok jordan


# 1.60 02-Nov-2006 marco

Fix comments to reflect reality.


# 1.59 19-Oct-2006 marco

Replace ACPI ioctls with APM ioctls. This makes ACPI talk the same protocol
as APM therefore userspace can now retrieve power information in the same
fashion as all other apm arches (zaurus, macppc, i386 etc).

To use force the use of acpi device like so: apmd -f /dev/acpi

ok jordan@


# 1.58 19-Oct-2006 jordan

Fixed GPE debug levels
Fixed ref counter on temporary bufferfields
DSDT parsing fully working on IBM Thinkpad
ok marco@


# 1.57 15-Oct-2006 jordan

Removed old GPE handler code remnants
ok marco@


# 1.56 12-Oct-2006 jordan

Massive ACPI changes
Fixed ACPI leaks
new ACPI API
ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.55 30-Jun-2006 gwk

KNF, No functional change,
"go at it" marco@


# 1.54 30-Jun-2006 jordan

Support for calling Embedded Controller via new GPE interface
ok marco@


# 1.53 30-Jun-2006 jordan

Added new code for GPE handling
Also works with ACPIEC; need to fixup acpiec.c to remove original interrupt handler
ok marco@


# 1.52 30-Jun-2006 jordan

Added offset argument for acpi read/write pmreg routines (new GPE code)
ok marco@


# 1.51 31-May-2006 todd

do not call acpi_powerdown() unless acpi has successfully initialized
ok gwk@ canacar@


# 1.50 29-May-2006 canacar

Add embedded controller support.
ok marco@ deraadt@ testing & ok gwk@


# 1.49 19-May-2006 canacar

Minimal thermal zone driver. Monitors thermal zone temperature,
shuts down the system if the 'critical temperature' is reached.
ok marco@


# 1.48 11-Apr-2006 marco

KNF.


# 1.47 11-Apr-2006 gwk

Add acpi_resume(struct acpi_softc *sc), which calls some AML methods,
detected at attach time, currently unused.

ok marco@


# 1.46 11-Apr-2006 gwk

Discover some ACPI sleep related methods, and call them at the
appropriate time in acpi_enter_sleep_state().

ok marco@


Revision tags: OPENBSD_3_9_BASE
# 1.45 26-Feb-2006 marco

Add acpicpu as a device.

ok jordan@


# 1.44 23-Feb-2006 marco

remove dead function.

ok jordan@


# 1.43 22-Feb-2006 marco

Print out all tables it found during autoconf. We'll need this data in
the future.


# 1.42 22-Feb-2006 marco

Make dmesg less verbose.

ok jordan.


# 1.41 22-Feb-2006 jordan

Added parsing of SSDT tables
ok marco@


# 1.40 22-Feb-2006 jordan

Added new aml_register to notify by device id
Moved powerdown code to button device
ok marco@


# 1.39 21-Feb-2006 marco

Readjust debug levels that crept in too low over the last few days.
KNF


# 1.38 21-Feb-2006 marco

Rig the power button so that the machine shuts down correctly.


# 1.37 20-Feb-2006 marco

Add acpibtn device. Remains disabled for now.


# 1.36 19-Feb-2006 jordan

Added code for aml_notify GPE notification callback
ok marco@


# 1.35 19-Feb-2006 grange

Fix acpi_enter_sleep_state() by replacing current ugly hack
with mostly correct sleeping state transition code.

ok brad@ marco@


# 1.34 19-Feb-2006 marco

jordan:
Enable interrupts.
Added peripheral code to make this work.

marco:
Replace softintr and other types of interrupts with a handler with context.
Fixed interrupt magic for GPEs.
Some KNF

This is work done by jordan and I but has to be a single commit.


# 1.33 17-Feb-2006 marco

Remove earlier introduced delays. We really only need them during autconf.

ok jordan@


# 1.32 17-Feb-2006 marco

Add a generic delay function as per the ACPI spec.

from jordan@


# 1.31 17-Feb-2006 marco

Use tsleep instead of delay when !cold.


# 1.30 17-Feb-2006 marco

Argh, we need delays between reads/writes. The backend reads (i2c most likely)
are so slow that values come back as 0 sometimes. We have to do this
despite the spec saying we shouldnt have to. Guess having a faster
parser than others can hurt sometimes.

Idea from jordan.


# 1.29 17-Feb-2006 jordan

Fixed code for handling _INI
Rearranged debug statements
ok marco@


# 1.28 16-Feb-2006 jordan

Added stub for calling _INI
Fixed aml_dowhile for NULL return
ok marco@


# 1.27 16-Feb-2006 jordan

Fixed parser/evaluator to retrieve battery info+state
Cleanup of parser
Dell laptops return battery status
ok marco@


# 1.26 06-Feb-2006 brad

fix a typo so this will compile on amd64.


# 1.25 04-Feb-2006 marco

When ACPI_ENABLED wasn't set but the devices were it had an unknown variable.

Pointed out by ian@.


# 1.24 03-Feb-2006 jordan

Rewrote aml parser yet again...
Fixed bus_space_map for i386/amd64
Added _BST and _BIF to acpibat to get status/info
ok marco@


# 1.23 20-Jan-2006 jordan

More evaluator changes
Removed old evaluator code
Fixed _HID value
ok marco@


# 1.22 19-Jan-2006 jordan

Added support for read/write of PCI Config space
Help jason@
ok marco@


# 1.21 18-Jan-2006 jordan

Added new evaluation routines
Added helper methods for buffer/field evaluation
Fixed broken indentation on dsdt.c

ok marco@


# 1.20 17-Jan-2006 jordan

Major overhaul of the aml parser. Evaluater temporarily disabled.

ok marco@


# 1.19 06-Jan-2006 grange

Check for ACPI control availability only if ACPI_ENABLE defined.


# 1.18 06-Jan-2006 grange

Informative error messages on attach, prettier dmesg.


# 1.17 06-Jan-2006 grange

printf -> dnprintf conversion for debug messages.


# 1.16 05-Jan-2006 grange

Find SLP_TYPx values and store them for future use in
acpi_enter_sleep_state().


# 1.15 05-Jan-2006 grange

Random KNF and cleanup


# 1.14 05-Jan-2006 grange

Provide symbolic names for the acpi sleeping states and use it.


# 1.13 28-Dec-2005 marco

Moved DEVNAME to a acpidev.h

ok jordan@


# 1.12 18-Dec-2005 sturm

add back comment about wrong code

ok marco


# 1.11 16-Dec-2005 marco

Remove test for kbd_reset. Prompted deraadt@, ok jordan@


# 1.10 16-Dec-2005 jordan

Added hooks for powerdown/power button press


# 1.9 16-Dec-2005 jordan

Cleaned up common power management register i/o
Added enter sleep state ioctl
Hooked power button interrupt
ok marco@


# 1.8 16-Dec-2005 marco

Remove autoconf chatter


# 1.7 16-Dec-2005 jordan

Moved DSDT AML parsing into parent ACPI driver
ok marco@


# 1.6 14-Dec-2005 marco

Add AC device glue.


# 1.5 14-Dec-2005 marco

Adjust debug printing levels to something more sane.


# 1.4 13-Dec-2005 marco

Add glue to attach devices to apci.


# 1.3 07-Dec-2005 marco

KNF


Revision tags: OPENBSD_3_8_BASE
# 1.2 10-Jul-2005 grange

Spacing cleanup.


# 1.1 02-Jun-2005 tholo

Start on a basic ACPI framework -- does not do much more than read out the
ACPI tables into kernel memory and attach ACPI and HPET timers currently.

In order to test this code, enabling the devices in GENERIC as well as
the ACPI_ENABLE option is needed. This code does not do any thermal
control yet, so this should be done with care depending on the platform.

In the tree so more people can contribute to making this more fully
featured.

Ok niklas@ grange@ tedu@


# 1.417 12-Sep-2022 kettenis

Add support for level-triggered GPIO events.

ok mlarkin@


# 1.416 10-Sep-2022 kettenis

Don't restrict GPIO events to pin numbers below 256. With the _EVT()
method events corresponding to larger numbers are possible.

Fixes lid state detection on the x13s.

ok patrick@


# 1.415 03-Sep-2022 kettenis

Make sure we don't suspend if we have no way to wake up the machine.
The idea is that device drivers that can wake up the machine register
themselves by calling device_register_wakeup(). To prevent regressions
on amd64, we let acpi(4) register itself as a wakeup device if the AML
for the machine defines wakeup devices. This may be refined in the
future.

This diff will prevent people from suspending their arm64 SBCs without
having a way to wake them up. For now the only driver that registers
itself is axppmic(4), which means that at this moment only SBCs with
an allwinner A64 or H5 SoC will actually support suspend/resume.

ok mlarkin@, deraadt@


# 1.414 10-Aug-2022 patrick

Provide the AML root in ACPI's softc so that drivers that need access
don't have to declare it using extern.

Suggested by and with feedback from kettenis@
Tested by deraadt@ on arm64, amd64 and i386
Tested by bluhm@ on amd64 and i386


Revision tags: OPENBSD_7_1_BASE
# 1.413 17-Feb-2022 jsg

reduce includes


# 1.412 10-Feb-2022 visa

Embed klist head in acpi_softc to avoid explicit malloc.

OK kettenis@


# 1.411 09-Feb-2022 deraadt

Move x86-only stubs used by kern/subr_suspend.c into dev/acpi/acpi_x86.c
for sharing between i386 and amd64.
ok mlarkin kettenis


# 1.410 08-Feb-2022 deraadt

The suspend/resume code is a sticky mess of MI, MD, and ACPI sequencing.
This splits out the MI sequencing, backing it with per-architecture helper
functions. Further steps will be neccesary because ACPI and MD are too
tightly coupled, but soon we'll be able to use this code for more architectures
(which depends on figuring out the lowest-level cpu sleeping method)
ok kettenis


# 1.409 04-Feb-2022 robert

set the APM_BATT_CHARGING state if the battery is being charged and try
to calculate the remaining time to be fully charged

ok jca@


# 1.408 02-Feb-2022 deraadt

acpi_addtask() calls malloc() w/ M_NOWAIT (because some calls come from
interrupt context), this however means occasional resource shortage will
result in callbacks registration failing, and unknown consequences for
the task-submitting caller.
Changing this to use pools with a low water mark, decreases the odds
of that problem occuring.
ok kettenis


# 1.407 01-Feb-2022 deraadt

GPE_DIRECT is calling the AML parser from interrupt context, which is
not permitted. Luckily nothing is using GPE_DIRECT anymore, so this code
can be deleted.
ok kettenis


# 1.406 26-Jan-2022 kettenis

An ACPI device needs to be both present and enabled for it to function.
So only attempt to attach hardware that has both bits enabled. This fixes
an issue where com(4) would attach for a disabled serial port leading to
misdetection of the hardware variant and a subsequent hang when /etc/rc
runs ttyflags -a.

ok anton@, deraadt@


# 1.405 12-Jan-2022 patrick

Make acpi_getpropint() return uint64_t, as ACPI integers are in fact that
wide and some _DSD properties depend on it.

ok kettenis@


# 1.404 04-Jan-2022 patrick

acpi_getprop() needs to actually make sure that we're looking at the
correct property. While there adjust acpi_getpropint() as well to
increase similarity with acpi_getprop().

ok kettenis@


# 1.403 16-Dec-2021 anton

Attach com over acpi on amd64. Some hardware uses a different interrupt
assignment compared to the the legacy one supported by com over isa.
This causes the console to halt once userland takes over as no
interrupts are received. The actual address and irq can be read from
ACPI, kettenis@ already added support for arm64 which paved the way for
amd64.

Some consoles that previously attached over isa are now expected to
attach over acpi.

Thanks to patrick@ for testing on arm64.

ok kettenis@


# 1.402 07-Dec-2021 kettenis

Fix parsing of SR_IRQ resource descriptors.

ok patrick@, anton@


# 1.401 02-Nov-2021 deraadt

knf


# 1.400 30-Oct-2021 deraadt

Change hw.perfpolicy=auto by default, at startup. If the system has AC
power connected (default is yes when no driver differentiates) then default
to 100% performance. On battery, use the existing auto algorithm (which is
admittedly somewhat unrefined).
This change overrides the system/BIOS speed and puts OpenBSD in control.
As this happens very early during boot, besides speedups in all usage usage
patterns, some surprises: unhibernate and sysupgrade times are cut in half.
note: on a few architectures, the setperf fn pointer is changed late, and
thus the auto algorithm stops timeing out. kettenis and i will look for
a solution.
in snaps for more than a week.
ok kettenis


Revision tags: OPENBSD_7_0_BASE
# 1.399 20-Jul-2021 mlarkin

Fix trailing whitespace in a few places.

No code change.


# 1.398 09-May-2021 kettenis

Some x86 machines advertise the "hardware reduced" ACPI feature, advertise
S4 and S5 support, but fail to populate the SLEEP_CONTROL_REG and
SLEEP_STATUS_REG descriptions in the FADT. An example of such a machine
is the ASUS Zenbook 14 UM433DA. Any attempt to powerdown the machine
will result in a kernel crash. It turns out that using the legacy ACPI PM
registers works fine on this machien. So fall back on those registers
if SLEEP_CONTROL_REG and/or SLEEP_STATUS_REG aren't provided.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_9_BASE
# 1.397 15-Mar-2021 patrick

Add acpi_iommu_device_map(), which replaces the DMA tag with one that
is blessed with IOMMU magic, if available. This is mainly for arm64,
since on amd64 and i386 the IOMMU only captures PCIe devices, as far
as I know, which uses the pci_probe_device_hook(). This though is for
non-PCI devices attached through ACPI.

ok kettenis@


# 1.396 07-Mar-2021 patrick

Pass standard DMA tag to acpi(4) table drivers.

ok kettenis@


# 1.395 27-Dec-2020 deraadt

acpi_map_address() cannot be wrapped by SMALL_KERNEL anymore, as
it is used by acpihpet.c


# 1.394 25-Dec-2020 visa

Refactor klist insertion and removal

Rename klist_{insert,remove}() to klist_{insert,remove}_locked().
These functions assume that the caller has locked the klist. The current
state of locking remains intact because the kernel lock is still used
with all klists.

Add new functions klist_insert() and klist_remove() that lock the klist
internally. This allows some code simplification.

OK mpi@


# 1.393 17-Dec-2020 kettenis

Fix some issues with referencing named ACPI nodes from Packages.
These references need to be resolved at runtime rather than when
they're parsed such that they pick up the right values for those nodes
which can be changed when for example _INI methods run.

The current approach is to replace these reference with a string that
names the node in question. The problem with that is that packages
can also contain normal strings. Which means that we need special
code that depends on the context where the package is used.

This diff takes a different approach by retaining a reference when
parsing. Code that uses a package will need to resolve this reference
but there is no ambiguiety anymore.

ok patrick@


# 1.392 05-Dec-2020 kettenis

Add support for the _EVT method to the GPIO support code.

ok patrick@


Revision tags: OPENBSD_6_8_BASE
# 1.391 27-Aug-2020 jmatthew

Reorder the acpi attach process so we look at Processor() CPU nodes
after Device() ones, since we should prefer the newer node type.
If we see any Device() nodes, don't attach acpicpu(4) to any Processor()
nodes if they're also present. This also makes acpitz(4) and
acpipwrres(4) devices attach slightly later.

ok kettenis@, also tested by jmc@


# 1.390 16-Aug-2020 gnezdo

Remove sc_maxgpe from acpi_softc which was never read from

Remove a dead conditional from acpi_init_gpes while there.

ok jcs@


# 1.389 21-Jul-2020 deraadt

acpi can use IPL_BIO (a low interrupt) since it only enqueues operations for
later processing. The use of a high interrupt will predate suspend/resume
efforts, we had to redesign acpi to be non-reentrant obviously
discussed with kettenis, in snaps for more than a week


# 1.388 24-Jun-2020 cheloha

kernel: use gettime(9)/getuptime(9) in lieu of time_second(9)/time_uptime(9)

time_second(9) and time_uptime(9) are widely used in the kernel to
quickly get the system UTC or system uptime as a time_t. However,
time_t is 64-bit everywhere, so it is not generally safe to use them
on 32-bit platforms: you have a split-read problem if your hardware
cannot perform atomic 64-bit reads.

This patch replaces time_second(9) with gettime(9), a safer successor
interface, throughout the kernel. Similarly, time_uptime(9) is replaced
with getuptime(9).

There is a performance cost on 32-bit platforms in exchange for
eliminating the split-read problem: instead of two register reads you
now have a lockless read loop to pull the values from the timehands.
This is really not *too* bad in the grand scheme of things, but
compared to what we were doing before it is several times slower.

There is no performance cost on 64-bit (__LP64__) platforms.

With input from visa@, dlg@, and tedu@.

Several bugs squashed by visa@.

ok kettenis@


# 1.387 10-Jun-2020 jca

Give userland a consistent battery and A/C state after resume

Previously we refreshed the state of acpiac(4) and acpibat(4)/acpisbs(4)
by queueing fake ACPI events to be processed by the acpi thread.
acpibat(4) using ACPIDEV_POLL meant that its status could be refreshed
first, resulting in an APM_POWER_CHANGE event sent to userland before
the state of acpiac(4) was up to date. Because of this, apmd(8) could
see A/C unplugged when it was in fact plugged, possibly leading to
a wrong decision.

Instead of using fake ACPI events, refresh the status of acpiac(4) and
acpibat(4)/acpisbs(4) right away at DVACT_WAKEUP time. To reuse the
existing refresh code of those drivers, make their ACPI *_notify()
handlers responsible for sending APM_POWER_CHANGE events instead. Also
queue an APM_POWER_CHANGE event right after resume to retain existing
behavior.

This fixes the consistency of data shown to userland, and makes those
drivers less special.

Initially discussed last year (!) with mpi@ at Miod's place.
Feedback and ok jcs@, deraadt@ agrees with the use of DVACT_WAKEUP here.


# 1.386 29-May-2020 deraadt

dev/rndvar.h no longer has statistical interfaces (removed during various
conversion steps). it only contains kernel prototypes for 4 interfaces,
all of which legitimately belong in sys/systm.h, which are already included
by all enqueue_randomness() users.


# 1.385 14-May-2020 kettenis

Bring back the acpi(4) changes. This time around only use the ACPI _CRS
information on ACPI 5.0 and later.

ok krw@, patrick@


# 1.384 11-May-2020 jca

More accurate remaining power reporting with disparate batteries

Overall remaining power was computed as the average of the remaining
power of each battery, in percents. This doesn't work with batteries of
different capacity, so use the total capacity and remaining power
instead.

Reported by lidstah, successful tests by lidstah and benno@, ok kettenis@


# 1.383 08-May-2020 kettenis

Move parsing of _CRS into acpi(4) and pass the parsed address and interrupt
information in struct acpi_attach_args.

ok mpi@


Revision tags: OPENBSD_6_7_BASE
# 1.382 14-Apr-2020 kettenis

Implement acpi_getprop(), the ACPI equivalent of OF_getprop().

ok patrick@


# 1.381 12-Apr-2020 kettenis

Add support for the _CCA method. This method indicates whether DMA is
cache-coherent or not. To implement this, acpi(4) gets two bus_dma tags
and passes the appropriate one when attaching devices based on _CCA.
On i386/amd64, where for all practical purpose DMA is always cache-coherent,
the two tags are the same. But on arm64 they are distinct.

ok patrick@


# 1.380 07-Apr-2020 visa

Abstract the head of knote lists. This allows extending the lists,
for example, with locking assertions.

OK mpi@, anton@


# 1.379 03-Apr-2020 mpi

Fix incoherencies of errors returned by various *kqfilter routines.

EOPNOTSUPP is returned when there's no "filterops" for a given fd.
EINVAL when the requested filter isn't supported by the "filterops".
ENXIO means the underlying device is no longer valid/present.

ok visa@


# 1.378 20-Feb-2020 visa

Replace field f_isfd with field f_flags in struct filterops to allow
adding more filter properties without cluttering the struct.

OK mpi@, anton@


# 1.377 31-Dec-2019 visa

Use C99 designated initializers with struct filterops. In addition,
make the structs const so that the data are put in .rodata.

OK mpi@, deraadt@, anton@, bluhm@


# 1.376 31-Dec-2019 mpi

Convert infinite sleeps to {m,t}sleep_nsec(9).

ok kn@


# 1.375 31-Dec-2019 jsg

include sys/sensors.h for sensor use
don't depend on indirect inclusion from acpidev.h


Revision tags: OPENBSD_6_6_BASE
# 1.374 07-Sep-2019 kettenis

Revert acpipci(4) changes. Too many systems are broken. We'll try again
after 6.6 as been released. The acpireg.h change stays behind.


# 1.373 28-Aug-2019 kettenis

Use ACPI information to attach PCI busses like we do on arm64. There are a
few additional quirks though, and attaching the PCI busses is delayed to
replicate the existing code more closely. That may be changed in the
future. Also tweak how we handle MSI support and respect to ACPI flag
that says we shouldn't attempt to use MSIs.

Some fallout is expected.

ok patrick@


# 1.372 27-Aug-2019 deraadt

On amd64 the bootup and resume paths for SP/BP/SP were not performing
msr/pat, clock-startup, firmware upgrade, fpu initialization, and cpu
soft-state initialization in the same order. That kind of chaos is
crazy and fragile. Improve the acpi resume call sequence and unify order
of the operations.


# 1.371 02-Jul-2019 jcs

acpi_set_gpehandler: turn edge arg into flags, add support for
passing GPE_DIRECT which directly executes the supplied callback and
clears a level interrupt rather than going through acpi_taskq which
can be slow when other driver callbacks are in the queue.

This should not be used for GPE handlers that need to do anything
ACPI-related.

ok kettenis


# 1.370 10-Jun-2019 kettenis

Print proper ACPI version number.

ok mlarkin@


# 1.369 08-Jun-2019 kettenis

When establishing the mapping between ACPI device nodes and PCI devices,
skip PCI host bridges and devices that aren't there.

ok patrick@


# 1.368 07-Jun-2019 kettenis

Introduce acpi_getsta() and use it to eliminate some duplicated code.

ok patrick@


# 1.367 12-May-2019 kettenis

Make sure the extended PM register descriptions actually exist before
using them. Fixes machines with ancient ACPI 1.0 (such as recent
hypervisors).

ok deraadt@


# 1.366 12-May-2019 kettenis

Add support for mapping ACPI PM registers using the "extended" gasio
representation from the FADT. Mostly fixes the Lanner NCA-1510.

ok lteo@, mlarkin@, deraadt@


# 1.365 11-May-2019 lteo

Fix incorrect error message.

ok kettenis@


# 1.364 08-May-2019 tedu

free size. the allocation is the header plus the length.


Revision tags: OPENBSD_6_5_BASE
# 1.363 04-Apr-2019 kettenis

Avoid potential null-pointer dereference. Found by Kent R. Spillner.

ok deraadt@, jsg@


# 1.362 20-Jan-2019 jsg

fix SMALL_KERNEL build after changes in rev 1.361
reported by naddy@ ok deraadt@


# 1.361 19-Jan-2019 tedu

Add a pwraction sysctl that controls what the power button does on acpi.
By default, nothing changes -- shutdown is initiated. But allows turning
power button into a sleep button if desired.
(grudging) ok from a few parties


# 1.360 26-Oct-2018 kettenis

Add acpipci(4) on amd64. For now this only calls the PCI-specific _OSC
method to let the ACPI implementation know what features we support.


Revision tags: OPENBSD_6_4_BASE
# 1.359 30-Aug-2018 kettenis

Move softbutton check before the check that skips attaching certain acpi
devices. Fixes regression caused by matching on _CID in addition to
matching on _HID.

ok matthieu@, mlarkin@


# 1.358 25-Aug-2018 kettenis

Match ACPI devices based on _CID if no match for _HID is found.

ok mpi@


# 1.357 19-Aug-2018 kettenis

Add support for multiple PCI segments. Only really implemented for arm64
for now as amd64/i386 firmware still caters for legacy OSes that only
support a single PCI segment.

ok patrick@


# 1.356 03-Aug-2018 kettenis

Let ahci(4) match on _CLS instead of _HID when attaching at acpi(4). Avoids
having to add many more _HID entries to the match table.

ok deraadt@, mlarkin@


# 1.355 10-Jul-2018 kettenis

Make legacy interrupts work in acpipci(4).

ok patrick@


# 1.354 05-Jul-2018 kettenis

Add acpipci(4), a driver that supports generic ECAM-compatible PCI host
bridges based on information provided by ACPI.

ok mlarkin@


# 1.353 04-Jul-2018 kettenis

Properly pass around the PCI "chipset tag" in acpi(4) and refactor
acpimcfg(4) to call an MD initialization functions that sets up a tag for
PCI ECAM.

ok guenther@, mlarkin@, krw@


# 1.352 01-Jul-2018 kettenis

Move acpi(4) attach glue into acpi_machdep.c.

ok guenther@, deraadt@


# 1.351 01-Jul-2018 kettenis

Add support for _DSD Device Properties.

ok mlarkin@


# 1.350 30-Jun-2018 kettenis

Add intr_enable() function, intended for MI use to amd64 and i386 and use
this in the acpi(4) suspend/resume code paths.

ok deraadt@


# 1.349 29-Jun-2018 kettenis

Use <stdint.h> types.

ok mpi@, jung@, krw@, deraadt@


# 1.348 29-Jun-2018 mlarkin

ACPI: Allow (with warning) GPE handler reassignment, instead of returning
a failure code.

ok kettenis


# 1.347 26-Jun-2018 mlarkin

knf


# 1.346 26-Jun-2018 kettenis

Let acpi(4) pass down the bus dma tag.

ok deraadt@


# 1.345 25-Jun-2018 kettenis

Reorganize acpi(4) code a bit in preparation for upcoming arm64 support.

ok deraadt@


# 1.344 20-May-2018 kettenis

Wake up the acpi thread after scheduling a gpio event.

ok mpi@


# 1.343 17-May-2018 kettenis

Sprinkle some #ifndef SMALL_KERNEL such that RAMDISK kernels cuild again.

pointed out by stsp@


# 1.342 17-May-2018 kettenis

Implement GenericSerialBus OpRegion support.

ok mlarkin@


# 1.341 27-Mar-2018 kettenis

Add acpicmos(4), a driver that implements SystemCMOS OperationRegion
access support. This fixes machines where the AML doesn't check whether
support for this OperationRegion type has been registered by the OS.

ok mlarkin@


Revision tags: OPENBSD_6_3_BASE
# 1.340 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.339 10-Feb-2018 deraadt

Syncronize filesystems to disk when suspending. Each mountpoint's vnodes
are pushed to disk. Dangling vnodes (unlinked files still in use) and
vnodes undergoing change by long-running syscalls are identified -- and
such filesystems are marked dirty on-disk while we are suspended (in case
power is lost, a fsck will be required). Filesystems without dangling or
busy vnodes are marked clean, resulting in faster boots following
"battery died" circumstances.
Tested by numerous developers, thanks for the feedback.


# 1.338 08-Feb-2018 deraadt

After flushing disks, try again to release all memory. We don't need to
save dirty memory to the hibernate space.


# 1.337 08-Feb-2018 deraadt

Toss all releaseable memory, because fragmentation can get in the way
of allocating the hibernate playpen.


# 1.336 08-Feb-2018 deraadt

sr_quiesce() is a new approach for ensuring that softraid drains
output to the disks.

This is part of a larger suspend/resume filesystem-safety diff, which
has been worked on for a couple of months already. Tests by job, krw,
beck, benno, and others. Sometimes even by snapshot users...


# 1.335 29-Nov-2017 kettenis

Add support for GPIO-signaled events.

ok mlarkin@


# 1.334 16-Nov-2017 jcs

Add PCI attachment for dwiic(4) needed by Intel 100 Series machines

ok kettenis


# 1.333 14-Nov-2017 kettenis

Add a few more PNP IDs to the skip lists.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.332 17-Aug-2017 stsp

Make dwiic(4) attach its ACPI parent dependencies.
This change makes the Asus E200HA keyboard work.

Original analysis and patch by Cesare Gargano
ok kettenis@


# 1.331 09-Aug-2017 dcoppa

Add "PNP0303" (8042 PS/2 Controller) to acpi_isa_hids[]

ok kettenis@


# 1.330 22-Jul-2017 jcs

acpibat: look for _BIX first before falling back on _BIF

ACPI 4.0 deprecated _BIF for battery status so some newer machines
are exporting _BIX which extends _BIF's package with a few extra
fields. When using _BIX, export the new discharge cycle count as a
sensor.

feedback from kettenis and anton
ok deraadt


# 1.329 20-Jul-2017 kettenis

Start a list of isa(4) device names such that we can avoid printing them
as "not configured".

ok mikeb@, millert@, deraadt@


# 1.328 08-Apr-2017 deraadt

Sensors are run as callbacks inside tasks. During suspend a sensor
could be running inside a driver that will be force-detached, or due
to tsleep end up disrupting the softstate/hardstate contract. At
suspend time, quisce all these callbacks by waiting for completion.
This issue has never been observed for real, but may be implicated in
suspend/resume failures.
ok kettenis guenther mlarkin


# 1.327 08-Apr-2017 deraadt

Two prototypes for acpi_maptable; merge to one place


Revision tags: OPENBSD_6_1_BASE
# 1.326 27-Mar-2017 deraadt

Now that hibernate_alloc() only has clean success/failure, don't
need to call hibernate_free() to clean up a partial mess.
ok mlarkin kettenis


# 1.325 13-Mar-2017 deraadt

allow ramdisks to build; ok jcs


# 1.324 12-Mar-2017 jcs

try to attach acpisbs first and if successful, don't attach acpibat


# 1.323 02-Mar-2017 natano

Add a new sysctl machdep.lidaction. The sysctl works as follows:

machdep.lidaction=0 # do nothing
machdep.lidaction=1 # suspend
machdep.lidaction=2 # hibernate

lidsuspend is just an alias for lidaction, so if you change one, the
other one will have the same value. The plan is to remove
machdep.lidsuspend eventually when people have upgraded their
/ets/sysctl.conf.

discussed with deraadt, who came up with the new MIB name
no objections mlarkin
ok stsp halex jcs


# 1.322 28-Feb-2017 natano

Untangle abstract sleep modes from ACPI sleep states. This paves the way
for further work in the area.

idea and ok deraadt
ok mlarkin


# 1.321 22-Feb-2017 jcs

add acpisbs, an acpi smart battery subsystem driver reading data
over smbus

currently disabled because it conflicts with acpibat

ok deraadt, kettenis


# 1.320 14-Jan-2017 kettenis

Revert aml_rdpciaddr changes; breaks several machines that were working
before.


# 1.319 11-Jan-2017 jsg

Increase the size of the buffers used for _HID and _CID strings to be
able to fit "Hyper_V_Gen_Counter_V1".

ok mlarkin@ deraadt@


# 1.318 08-Jan-2017 kettenis

Make aml_rdpciaddr() work for for devices behind bridges. The code now
uses the mapping between AML nodes and PCI devices that we establish.
Because _INIT methods may end up calling aml_rdpciaddr(), make sure we
create that mapping early. Also handle devices that aren't actually present.
These devices are now included in the mapping and reads will return an
all-ones patterm whereas writes are a no-op..

ok mlarkin@


# 1.317 25-Oct-2016 pirofti

Add the resource index to aml_parse_resource() callback function.

This is needed by an upcoming acpiec commit that handles machines breaking
the current ACPI specifications.

Change suggested by and ok kettenis@, guenther@


# 1.316 18-Sep-2016 guenther

If the APIC table (MADT) isn't present then ci_acpi_proc_id won't be set
when acpicpu tries to attach, leading to a NULL dereference. The ACPI
processor object on the Dells where this was reported is bogus and useless
anyway so apply the same test when deciding whether to attach to processor
objects as acpicpu would when matching them to cpus.

Problem reported by Eivind Eide (xenofil (at) gmail.com) and
Johan Huldtgren (johan+openbsd-bugs (at) huldtgren.com).
Testing by Eivind Eide.
ok kettenis@ mlarkin@


# 1.315 03-Sep-2016 naddy

Write the system time back to the RTC every 30 minutes.
This fixes the problem that long-running machines which were not
shut down properly would reboot with a badly offset system time.

hints and ok kettenis@


# 1.314 31-Aug-2016 mlarkin

Don't put a device in the wake device list if _STA says it's not there.

Fixes at least one machine that woke up from sleep / poweroff immediately.

ok kettenis@, tested by kettenis and martijn@


# 1.313 28-Jul-2016 kettenis

Store the acpi processor ID/UID in struct cpu_info, and use it to attach
acpicpu(4) drivers to the right cpu(4).

ok mlarkin@, guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.312 10-Jun-2016 kettenis

The power management timer has been made optional in ACPI 5.0A, so don't
attempt to attach acpitimer(4) if the timer isn't present.

Pointed out by jcs@

ok jcs@


# 1.311 08-May-2016 kettenis

Make sure devices listed by a device's _DEP method are attached before
we attach the device itself. The _DEP methode, introduced in ACPI 5.0,
is a hint that the device in question depends on OpRegion support from
the devices listed by _DEP, so we have to order them the proper way.
To prevent us from attaching those devices again when we encounter them
later walking down the device tree, keep track of our attempts to
attach a device driver to them using aflag in the aml_node structure.

ok guenther@


# 1.310 25-Apr-2016 pirofti

We attach acpihpet(4) and acpiec(4) without using the HID, so do not
report it in the dmesg.

OK deraadt@, mlarkin@.


# 1.309 02-Apr-2016 kettenis

Reduce dmesg spam by knocking out some acpi hardware IDs that don't need a
driver.

ok mpi@


# 1.308 29-Mar-2016 kettenis

Check if a device is present (using _STA) in acpi_foundhid. This prevents
us from reporting devices as "not configured" that aren't actually present,
and allows us to remove duplicated code from several drivers.

ok deraadt@, ok jsg@


# 1.307 29-Mar-2016 kettenis

Implement a cleaner way to prevent acpivout(4) from attaching if we have
a platform-specific method of controlling the backlight.

ok millert@, mlarkin@, jung@


# 1.306 28-Mar-2016 kettenis

Adding each and every new acpi device driver to acpi_foundhid() is
gettinga bit of the burden. And it really isn't how our autoconf
framework is supposed to work for busses that can be enumerated. All
the drivers already check for a matching _HID in their attach
function. So we can just drop the checks in acpi_foundhid() and let
the drivers sort themselves out.

Print a "not configured" line for devices for which we don't attach adriver.
This may be a bit much as there are devices for which we will not have a
driver. This will be removed if it causes too much dmesg spam.

ok deraadt@, mlarkin@


Revision tags: OPENBSD_5_9_BASE
# 1.305 17-Jan-2016 jsg

Take acpi_foundhid() outside of #ifndef SMALL_KERNEL so the ramdisk
kernel can find and attach dwiic(4) and sdhc(4).


# 1.304 17-Jan-2016 jsg

remove an unused global variable


# 1.303 14-Jan-2016 kettenis

Attach dwiic(4) on the Intel Bay Trail i2c controllers as well.


# 1.302 12-Jan-2016 kettenis

Improve emulation of PM registers on Hardware-redcued ACPI by emulating
SPL_TYPx, SLP_EN and WAK_STS using the new SLEEP_STATUS_REG and
SLEEP_CONTROL_REG registers. Makes the transition into S5 (powerdown) work
on the ASUS X205TA.

ok deraadt@, mlarkin@


# 1.301 12-Jan-2016 jcs

Add dwiic, a driver for the Synopsys DesignWare i2c controller found
on the Samsung ATIV Book 9 laptop. This initial version only
supports ACPI config/attachment.

Add ihidev, a HID-over-i2c driver largely based on uhidev. dwiic
handles attaching ihidev devices found in ACPI.

Add ims, a HID-over-i2c mouse/trackpad driver to get basic cursor
and button functionality from HID-compliant i2c trackpads.

ok kettenis deraadt


# 1.300 11-Jan-2016 kettenis

Make sdhc(4) attach to hardware IDs "80860F14" and "PNP0FFF".


# 1.299 09-Jan-2016 kettenis

Initial support for Hardware-Reduced ACPI. This skips the legacy mode checks
by emulating the PM_CNT register such that SCI_EN is always enabled. It also
skip attaching acpitimer(4) since the relevant registers are no longer there.

ok deraadt@, mlarkin@


# 1.298 06-Jan-2016 kettenis

Follow the spec and check the SCI_EN bit to see if ACPI has already
been enabled and don't bother enabling it again if it is. This should fix
systems that don't implement legacy mode and therefore set ACPI_ENABLE and
ACPI_DISABLE to zero.

ok mlarkin@, yasuoka@


# 1.297 23-Nov-2015 reyk

Back out the CRITICAL change, turn it back from 10% to 25%. Some
people with old/broken batteries didn't like this change, and the
underlying heuristics are wrong.


# 1.296 22-Nov-2015 reyk

The "CRITICAL" APM battery level, that is exported/faked by the ACPI
stack, is currently set to 25%. With 25% battery left, a typical
modern laptop will have at least one hour runtime left. It is an
arbitrary value, and other APM power layers in the kernel set CRITICAL
to something like 5% or 10%, so set it to 10% for now.

OK mlarkin@


# 1.295 28-Sep-2015 deraadt

In low-level suspend routines, set cold=2. In tsleep(), use this to
spit out a ddb trace to console. This should allow us to find suspend
or resume routines which break the rules. It depends on the console
output function being non-sleeping.... but that's another codepath which
should try to be safe when cold is set.
ok kettenis


# 1.294 06-Sep-2015 deraadt

clarify malloc arguments (base + length...)


# 1.293 28-Aug-2015 deraadt

fairly simple sizes for free(); ok tedu


# 1.292 20-Aug-2015 kettenis

Respect the access size when reading or writing to pci config space.
This version also makes sure writes are properly aligned, preventing
the panics seen with the previous incarnation of this change.

Fixes battery status passthrough in vmware, brightness keys on my x220
and who knows what else.

ok mlarkin@, deraadt@


Revision tags: OPENBSD_5_8_BASE
# 1.291 04-Aug-2015 deraadt

If we are setting ACPI_PM1_BM_RLD do it only once, and remember to
repeat this action at resume time.
ok mlarkin guenther


# 1.290 29-Jul-2015 deraadt

Something subtle in previous revision is locking up suspend or booting
on some laptops. Mailing lists threads mention x220 and Asus machines.
Reverting for now.


# 1.289 21-Jul-2015 kettenis

iRespect the access size when reading or writing to pci config space.
Fixes battery status passthrough in vmware and who knows what else.

Tested by many.

ok mlarkin@, deraadt@


# 1.288 17-Jul-2015 mlarkin

fix comments


# 1.287 26-Mar-2015 jsg

Use the 0x10 extended capability to determine if the trackstick
buttons are wired to the trackpad and need to be re-routed to the
trackstick. Without this change the buttons on 2015 thinkpads
get picked up as extended buttons that show up as scroll up/down.

Remove the X1 Carbon 2015 (LEN0048) and X250 (LEN0046) from the
top button area/soft buttons quirks list. Also avoid using the
quirk list entirely if the capability bit is set.

Based on work from jcs@ with feedback from mpi@
ok mpi@ jcs@


# 1.286 20-Mar-2015 kettenis

The ACPI standard says that a PCI function number of FFFF can be used to refer
to all functions on a device. This makes no sense in the context of accessing
PCI config space. Nevertheless stsp@ has a machine with AML that tries to
do config space reads from a device where _ADR encodes function number FFFF.
This is almost certainly buggy AML, but since Linux (and presumably Windows)
boot just fine on this machine we need to work around this. Linux effectively
treats function FFFF as function 7, which isn't present on this machine.
Instead we simulate a read from a non-existent device by returning a bit
pattern of all ones. Writes to function FFFF will still panic.

ok mlarkin@


# 1.285 16-Mar-2015 deraadt

Reduce use of SMALL_KERNEL in acpi, bringing acpiec driver and more gpe
handling into RAMDISK. This is now possible because the install media
has ample room. The goal is to reduce special cases where we may be
skipping (unknown) important operations...
ok mlarkin kettenis


# 1.284 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.283 07-Feb-2015 deraadt

New framework that allows hibernate to pass in entropy from it's fresh
boot.
ok mlarkin


# 1.282 06-Feb-2015 mlarkin

Fix a hibernate crash on some machines due to unmapping a page that
may not have been mapped previously (in the failure to hibernate case).

Also ensure that the lowmem ptp is mapped in all cases (not just MP).

ok kettenis


# 1.281 17-Jan-2015 deraadt

resume_randomness() before spinning up other cpus...


# 1.280 15-Jan-2015 jsg

Define a new wscons mouse type for Synaptics clickpad devices that lack
physical buttons. This will be used if the acpi pnpid for the mouse
matches a list provided by Synaptics found in Linux.

Combined with the xenocara changes this will remove the need for an xorg
config file for the x240/t440/t540 generation of thinkpads.

ok matthieu@ shadchin@ kettenis@


# 1.279 11-Jan-2015 kettenis

Tolerate checksum errors in ACPI tables (but complain about them). Apparently
there are machines out there with broken BIOSen with such checksum errors in
thier RSDT tables. Windows runs fine on these machines, so it probably does
not check. Linux ignores checksum errors as well (and prints a warning).

ok deraadt@, krw@


# 1.278 18-Dec-2014 deraadt

suspend and resume the RNG (at what looks like the right spots)
ok reyk djm


# 1.277 09-Dec-2014 doug

Replace some malloc(n*size,...) calls with mallocarray().

ok tedu@ deraadt@


# 1.276 06-Dec-2014 mpi

If "machdep.lidsuspend" is not set, do not go back to sleep if the lid
is still closed when resuming. This allows docked laptop to be resumed
without opening them.

ok mlarkin@


# 1.275 23-Nov-2014 mlarkin

If a machine has no _LIDs, then none can be open. This diff fixes a problem
where machines without any _LIDs were going back to sleep after hibernate
due to the recent suspend-if-lid-closed-on-resume diff. Noticed on qemu,
which doesn't have any _LIDs.

discussed with deraadt


# 1.274 18-Nov-2014 krw

Nuke yet more obvious #include duplications.

ok miod@


# 1.273 08-Nov-2014 mlarkin

If resuming from sleep (zzz/ZZZ) and the lid is still closed, go back to
sleep. This prevents accidental lid flex or slight opening in a backpack
from waking the machine up and leaving it resumed (powered on).

ok deraadt@


# 1.272 26-Sep-2014 kettenis

Rework piglet and pig allocation. Currently the piglet gets allocated
deep down in the suspend path, where it is really hard to recover from
allocation failure. So allocate the piglet early on in the suspend path.
Also change the piglet and piglet allocation functions to use km_alloc(9)
instead of doing pmemrange magic. This removes a bunch of code which, in the
case of the piglet allocation, is broken since it results in a NULL pointer
dereference. Also switch the piglet allocation to not wait. If we can't
allocate 16MB of phys contig memory on a halfway modern machine we're almost
certainly under a lot of memory pressure and we're better off not trying to
hibernate anyway.

ok mlarkin@


# 1.271 25-Sep-2014 kettenis

Bring back rev 1.249. Now that mp_setperf() has been fixed, it should be
safe to restore perflevel when resuming. Should fix the issues people
see with the processor running at full speed heating up the machine while
the apm command reports that the CPUs are running at a low speed.

ok dcoppa@, deraadt@


# 1.270 23-Sep-2014 kettenis

Release the acpi lock around calls to wsdisplay_suspend() and
wsdisplay_resume(). The X server may adjust the display brightness just
before/after the VT switch when suspending/resuming. In fact, it may
already be blocked adjusting the display brightness for some reason
unrelated to the suspend request.

The VT switch request will eventually time out, but the delay is annoying
and the failed VT switch will lead to problems after resume.

Needed for a future update of the xf86-video-intel driver.

ok mlarkin@


# 1.269 19-Sep-2014 kettenis

Use config_suspend_all(9).


# 1.268 08-Sep-2014 kettenis

Clear the PME Status bit on PCI devices in response to Device Wake events.
Fixes the interrupt storm on the 2009 Mac mini with WOL enabled on its
nfe(4) interface.

ok mlarkin@


Revision tags: OPENBSD_5_6_BASE
# 1.267 20-Jul-2014 mlarkin

Support hibernating to softraid crypto volumes.

much help and ok from deraadt@


# 1.266 16-Jul-2014 mlarkin

Reenable hibernate RLE support and flush+zero all memory after unpack.


# 1.265 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.264 12-Jul-2014 kettenis

Add a function to drop all clean pages on the page daemon queues and call
it when we hibernate.

ok mlarkin@, miod@, deraadt@


# 1.263 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.262 11-Jul-2014 mlarkin

Flush the buffercache to 16MB on hibernate and restore its previous max
size (kern.bufcachepercent) on resume, for better hibernate performance.

ok beck@


# 1.261 10-Jul-2014 blambert

remove unneeded header

ok mlarkin@


# 1.260 01-Jul-2014 kettenis

Adding an additonal delay after each access to SystemMemory or SystemIO
during autoconf makes no sense.

ok deraadt@


# 1.259 23-Jun-2014 kettenis

SystemMemory is used to access memory mapped registers on some machines, so we
must use the bus_space(9) API to access it instead of memcpy(9). Also make
sure we properly align access to fields that need more than byte alignment.

ok guenther@


# 1.258 21-May-2014 mlarkin

Fix invalid / missing format specifiers in several dnprintf statements
(used only when ACPI_DEBUG is enabled)

From a diff on tech@ by Fabian Raetz, with a minor change by me, tested
on i386 and amd64 with ACPI_DEBUG enabled and disabled.


# 1.257 25-Apr-2014 mlarkin

Defer the free of the hibernate structures until later, when it is safe to
do so.

Freeing the hibernate structures too early leads to inadvertent calls into
splx when we aren't ready for them yet. Found when trapping splx calls in
the resume path.

discussed with deraadt@ at m2k14


# 1.256 13-Apr-2014 mlarkin

Fix a badly worded debug message that implied the entire machine was going
to sleep, instead of just the acpi thread going to sleep.


# 1.255 31-Mar-2014 mpi

Change the order of operations during a suspend/resume cycle and
call bufq_quiesce() after executing the DVACT_QUIESCE handlers.

This should be safe since no disk nor controller drivers have such
handler but it will allow us to detach sd(4) devices attached to a
USB bus.

Another benefit pointed out by kettenis@ is that drivers that need
to read a firmware from the disk should be able to do it at resume
time in a DVACT_WAKEUP handler.

ok kettenis@, deraadt@


# 1.254 13-Mar-2014 dlg

get rid of the assumption that the head of the alldevs list is the
"mainbus" device. this breaks when mpath is enabled because it
attaches before mainbus and therefore takes the head position.

have autoconf provide device_mainbus() which looks up mainbus_cd,
and use that instead.

discussed with deraadt who just wants mpath stuff to move forward
despite there being many ways to shine this particular turd.


Revision tags: OPENBSD_5_5_BASE
# 1.253 21-Feb-2014 deraadt

Do the lid control further on in the resume sequence, since this is
AML, and will do WAITOK operations. While cold, that is a bad idea.
We seem to be safe with respect to the spec, I do not see any event
debounce issues here.
ok kettenis, tested by many


# 1.252 20-Jan-2014 claudio

Instead of checking sc->sc_revision <= 1 check that the
fadt->hdr_revision <= 1. This is a better check since it checks
the version of the proper descriptor. This allows the alix APU
to reboot since those boards have currently a wrong sc->sc_revision.
Committing this now so that it can be tested easier.
With and OK jsg@ also fine by derradt@


# 1.251 24-Dec-2013 kettenis

If the FADT has its SMI_CMD set to zero, assume we're only ACPI-only hardware
and don't need to disable SMI ownership of the ACPI hardware registers.

ok mlarkin@


# 1.250 23-Dec-2013 kettenis

Revert previous commit. It makes resume hang on some MP systems running
GENERIC.MP.


# 1.249 16-Dec-2013 kettenis

Restore hw.setperf upon resume like we do for apm(4) on i386. Seems we need
to do this fairly late in the resume to avoid hangs.

ok deraadt@, mlarkin@


# 1.248 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


# 1.247 06-Nov-2013 mpi

Add support for Power Resources for Dx states and the necessary hook
for PCI devices. This hook should be called twice, before and after
changing the power state of a PCI device.

Before setting the device to the new state, the ACPI layer will notify
every power resources linked to the device for that state and make sure
they are turned "_ON". After changing the state of the device, it will
decrement the reference of every power resources linked to that device
for the old state and turn them "_OFF" if they are no longer referenced.

This fixes the no-USB after resume problem seen on various ThinkPad,
problem initialy diagnosed with Alexander Polakov.

ok kettenis@, deraadt@


Revision tags: OPENBSD_5_4_BASE
# 1.246 01-Jun-2013 mlarkin

Stop printing ACPI wakeup devices in dmesg after the 16th wakeup device.

Helps with VMware VMs that report hundreds of wakeup devices.

ok reyk@, deraadt@


# 1.245 31-May-2013 bluhm

Fix the build for a kernel without wd and pciide in its config.
OK deraadt@ tedu@


# 1.244 30-May-2013 deraadt

Enforce ca_activate tree-walks over the entire heirarchy for all events,
cleaning up some shutdown-hook related code on the way.
(A few drivers related to sparc are still skipped at kettenis' request)
ok kettenis mlarkin, tested by many others too


# 1.243 18-Apr-2013 deraadt

pin the acpi thread to the BSP, because AML and SMI cannot be trusted. I
was certain we were doing this already, but it turns out no...


Revision tags: OPENBSD_5_3_BASE
# 1.242 24-Dec-2012 guenther

In acpiioctl(), Return though the splx() path if suser() returns an error

ok deraadt@


# 1.241 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


# 1.240 04-Oct-2012 ehrhardt

Use information provided by ACPI to attach secondary PCI host bridges.

ok kettenis@


# 1.239 07-Sep-2012 kettenis

Add a new function called acpi_pci_min_powerstate() that returns the lowest
power state for a PCI device for the power state (currently S3 or S4) we're
transitioning into. This function is probably too simplistic; there is a lot
of (silly) complexity in the ACPI spec.

ok deraadt@


Revision tags: OPENBSD_5_2_BASE
# 1.238 13-Jul-2012 pirofti

Disable acpivideo(4) on ASUS laptops.

There are some models that panic whenever an event is received.
Some cope, some don't. In order to get this right acpiec(4) requires
major rework. Until then let machines boot.

Discussed with deraadt@.


# 1.237 11-Jul-2012 mlarkin

for now, restrict hibernate to root in the acpi ioctl kernel interface.
normal rules for controlling this interface through apmd still apply.

ok deraadt@


# 1.236 09-Jul-2012 deraadt

tiny little oops in untested hibernate block


# 1.235 09-Jul-2012 deraadt

clean up ioctl() case statements


# 1.234 09-Jul-2012 deraadt

acpi_disable_onegpe() is not used; ok pirofti


# 1.233 24-May-2012 kettenis

If _MIN > _MAX consider a WordBus resource to be invalid. Fixes a panic on
the IBM x3550-M4.

ok mikeb@


# 1.232 29-Mar-2012 mlarkin

Missing #ifdef in previous diff


# 1.231 29-Mar-2012 mlarkin

Prevent hibernation attempts on machines with unsupported disk controllers.
The same check is also performed later in the hibernate sequence, but by
then it's too late to cleanly unwind (presently).

ok deraadt


# 1.230 26-Mar-2012 deraadt

simplify conditionals in acpi_sleep_state()
ok mlarkin


# 1.229 26-Mar-2012 deraadt

hook in the hibernate request code; half of this diff is from mlarkin
ok mlarkin


Revision tags: OPENBSD_5_1_BASE
# 1.228 20-Sep-2011 deraadt

Very early in suspend while the clock is still ticking realtime,
save the clock back to the rtc. The zaurus already did this.
ok phessler miod kettenis


Revision tags: OPENBSD_5_0_BASE
# 1.227 02-Jul-2011 nicm

kqueue attach functions should return an errno or 0, not a plain 1. Fix
the obvious cases to return EINVAL and ENXIO.

ok tedu deraadt


# 1.226 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.225 16-Jun-2011 pirofti

Import acpitoshiba driver.

This is originally written by Hiroyuki Aizu, ported and integrated
in our tree by Javier Vazquez <javama4 AT gmail> with tweaks and
suggestions by me.

Import prodded by deraadt@ and the lack of testers on tech@.


# 1.224 27-Apr-2011 jcs

attach acpithinkpad to newer lenovo models like the x120e

ok deraadt@


# 1.223 22-Apr-2011 jordan

Fix uninitialzied variables and formatting strings (-Wxxx errors)


Revision tags: OPENBSD_4_9_BASE
# 1.222 02-Jan-2011 jordan

Re-add code for ACPI taskqueue, eliminates need for state vars in acpi softc
Serialize processing ACPI tasks for sleep/powerdown/GPE event
ok deraadt@


# 1.221 31-Oct-2010 guenther

Revert last commit: it breaks resume on Thinkpads
"then please back it out" deraadt@


# 1.220 26-Oct-2010 jordan

Add task queue for ACPI gpe and notify handlers
This fixes eject on Dell Latitude dock and an issue on the Dell Mini
battery update.
ok deraadt


# 1.219 07-Oct-2010 jordan

Create separate functions for enabling wake and runtime gpes.
Remove old suspend/resume gpewalk function
ok deraadt, mlarkin


# 1.218 05-Oct-2010 jordan

split acpi_enable_onegpe to separate enable/disable functions
ok deraadt


# 1.217 05-Oct-2010 jordan

Added acpi_disable_allgpes function to clear GPE status
ok deraadt


# 1.216 05-Oct-2010 deraadt

clean up acpi_softc use more


# 1.215 05-Oct-2010 deraadt

delete a bit of #if 0 code


# 1.214 05-Oct-2010 deraadt

hide use of our global acpi_softc pointer a bit better


# 1.213 29-Sep-2010 deraadt

grab the acpi thread lock very early in acpi thread startup; not that it
is likely that anyone will race us... but why be careless.
ok kettenis


# 1.212 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.211 29-Aug-2010 jasper

- add a check for FWRITE here too.

ok miod@


# 1.210 27-Aug-2010 kettenis

Make PCI Power Management optional, and only enable it when acpi(4) attaches.

ok deraadt@


Revision tags: OPENBSD_4_8_BASE
# 1.209 08-Aug-2010 kettenis

Close race between the acpi thread and wscons ioctls running in process
context. We might rip this out post-release and replace it with a solution
that serializes things in a more obvious way.

ok deraadt@, miod@ and "those crazy texans who ok anything"


# 1.208 08-Aug-2010 deraadt

Report the acpi sleep states when on a RAMDISK; ok krw


# 1.207 07-Aug-2010 kettenis

Enforce acpi register access rescrictions.

ok mlarkin@, deraadt@, tested by marco@


# 1.206 07-Aug-2010 deraadt

upon resume, notify all the acpiac and acpibat drivers to update
their status. some laptops do not do this automatically.
ok kettenis canacar


# 1.205 06-Aug-2010 marco

Always call _PSW on _LID devices that support it to enable lid open to
resume the machine. Conversely disable it when the machine wakes up.

Tested by several ok deraadt


# 1.204 06-Aug-2010 deraadt

As part of a revamp of the PM1/GPE code, I write this diff to look for
unmanaged EN & STS bits in the PM1 register at interrupt time and report
them. As a side effect this splits the STS acknowledgement into two writes
(for power, and sleep) instead of one. The printf that is added (to spot
unmanaged STS bits) has not yet been triggered as far as we know.

Before the "write to PM1 registers at the right offset" diff went in, this
was not neccessary. But newer thinkpads do not have a working soft power
button without this diff.

We have no idea why.
ok mlarkin kettenis


# 1.203 05-Aug-2010 deraadt

PM1 power button events were generating 'fake events' to acpibtn PWRB,
but if there is no such device these were being tossed. Instead, arrange
for all power button handlers to flag a softc variable which is processed
by the thread; so that one place will know to request a powerdown (psignal
to init)
ok marco mlarkin


# 1.202 05-Aug-2010 deraadt

rename a variable which is annoyingly poorly named


# 1.201 05-Aug-2010 mlarkin

Write to the correct offset for various ACPI pmreg registers. Writing to
the correct register does properly clear the wake status bits and fixed
event bits, which allows some machines to have more than 1 suspend cycle.

bug spotted by deraadt@

tested by deraadt@ and myself on a half-dozen machines and others on
hackers@

ok deraadt@, marco@


# 1.200 05-Aug-2010 deraadt

Fix the power button. The pm1 register is 16 bits in size.
My mistake, a while back.
tested by krw


# 1.199 04-Aug-2010 kettenis

Print supported sleep states.

ok marco@, phessler@, deraadt@


# 1.198 03-Aug-2010 mlarkin

Use the proper flag for re-enabling certain hardware events (power btn,
sleep btn). The (incorrect) flag prevoiously used coincidentally had the
same value, so this shouldn't cause any different behavior than before.

tested on a variety of machines (i386, amd64, sp, mp) - no behavioral
change seen

ok deraadt@


# 1.197 28-Jul-2010 marco

Make all gasio failures look and behave the same.


# 1.196 28-Jul-2010 mlarkin

Delay a little longer on suspend before giving up. Fixes machines that
sometimes don't suspend because the sleep transition takes too long
(symptoms like failing to sleep but the backlight stays on).

ok deraadt@


# 1.195 27-Jul-2010 deraadt

remove XXX; EC can now be attached early when needed; ok jordan


# 1.194 27-Jul-2010 deraadt

more brackets feels good


# 1.193 27-Jul-2010 deraadt

XXX annotate a block in acpi_enter_sleep_state() that is pretty worrying


# 1.192 27-Jul-2010 deraadt

horrific indentation that nearly made me puke


# 1.191 27-Jul-2010 mlarkin

Set the sleeping indicator light on machines that support it, in case SMI
doesn't do it for us. Shows the 'moon' LED on older Thinkpads when
sleeping.

Tested on T43p, W500, T510 by me and on X61s by deraadt@.

ok deraadt@


# 1.190 27-Jul-2010 jordan

Early initialization of acpiec if ECDT table exists
Fixes hang when booting thinkpads while docked
ok deraadt


# 1.189 26-Jul-2010 deraadt

remove merge error which was commited


# 1.188 26-Jul-2010 pirofti

Add support for Sony ACPI hotkeys via a new driver: acpisony(4).

Currently it only works for the suspend button and tries to do right for
the brightness events, but I haven't found a Sony laptop that like to do
right so far.

In the future I want to make the brightness keys work on all Sony's and
also add support for the zoom hotkeys and whatever other funky keys I can
find on those things.

Okay deraadt@.


# 1.187 22-Jul-2010 deraadt

pretty large cleanup of SMALL_KERNEL


# 1.186 21-Jul-2010 deraadt

in the acpi GPE handler, late ack level interrupts not edge interrupts.
Duh.


# 1.185 21-Jul-2010 deraadt

Merge the edge and level gpe handlers into one (now possible since the
gpe structure tells us whether the gpe is edge or level)


# 1.184 21-Jul-2010 deraadt

delete #if 0 code we are not going to use; ok marco


# 1.183 20-Jul-2010 deraadt

make the mask code for acpi_enable_onegpe() match other similar code


# 1.182 20-Jul-2010 deraadt

Handle the acpi interrupt controller with proper edge/level handling,
wrapped in spltty to avoid racing against the interrupt controller.
Repair the gpe bit masking code while there, and do operations in the
right order
ok kettenis mlarkin, help from jordan at figuring out the order of
operations.


# 1.181 20-Jul-2010 matthew

When we're preparing to go to sleep, only call bufq_restart() if other
preparations failed.

ok kettenis@, thib@, deraadt@


# 1.180 19-Jul-2010 deraadt

permit compilation with ACPI_DEBUG


# 1.179 19-Jul-2010 deraadt

there is no need for a seperate dsdt_softc pointer; dsdt does not have a
softc but is just a part of the single acpi driver.
ok jordan


# 1.178 19-Jul-2010 deraadt

acpi_delay() is no longer needed; ok jordan


# 1.177 18-Jul-2010 mlarkin

Clear event status on resume, to avoid going back to sleep/reboot/shutdown
immediately after resume. Tested by a bunch of people on hackers@ on
various hardware. Fixes at least Toshiba NB200 and Gateway LT3103u.

ok marco


# 1.176 13-Jul-2010 deraadt

rename some thread related interfaces so they are logical


# 1.175 10-Jul-2010 jordan

Verify _ADR bdf is correct (some systems return 0xffff)
Works on Dell x9xx and Studio 1555


# 1.174 09-Jul-2010 jordan

Clear the noise


# 1.173 08-Jul-2010 jordan

Add mapping for ACPI device to PCI bdf (match autoconf tree)
Simplify resource parsing function to use buffer argument
Convert namespace linked lists to use queue macros
ok marco@, deraadt@


# 1.172 06-Jul-2010 deraadt

Use spltty() to lock downcalls from apm against the information being
modified by the acpi thread.
ok kettenis


# 1.171 06-Jul-2010 deraadt

Don't use a workq to do the suspend, because it races aginst the acpi
thread. Instead, just tell the acpi thread to do the suspend for us.
This makes apmd & zzz work correctly. While here, have acpithinkpad
attempt to post the event to apm before prompting the suspend itself.
ok kettenis marco mlarkin


# 1.170 05-Jul-2010 mlarkin

Reset register SCI_EN on resume. The ACPI spec clearly states we don't have
to do this, but many machines don't, or forget to, which leads to all sorts
of problems on resume (broken battery stats, overheating, failure to resume
more than once, etc).

tested by many, ok marco@, deraadt@


# 1.169 01-Jul-2010 thib

do bufq quiesce again, since vnd/the bufq_done call in
biodone has been fixed.


# 1.168 01-Jul-2010 jordan

Backout recent AML changes, breaks on R210 and others


# 1.167 01-Jul-2010 jordan

Simplified aml_parse_resource call, cleanup code
ok marco


# 1.166 01-Jul-2010 jordan

Fix subordinate bus # for multi-root PCI buses
ok marco, kettenis


# 1.165 30-Jun-2010 thib

Disable/partially backout the bufq quiesce changes as this
is causing havoc with vnds and release must be buildable.


# 1.164 29-Jun-2010 deraadt

Fix ramdisks (new code was placed inside a SMALL_KERNEL chunk)


# 1.163 29-Jun-2010 jordan

Add support for mapping ACPI to PCI devices
ok kettenis, deraadt


# 1.162 29-Jun-2010 kettenis

Call bufq_quiesce() on suspend to stop all I/O and bufq_restart on resume.
Makes suspend/resume work much better while doing disk I/O.

ok mlarkin@


# 1.161 29-Jun-2010 tedu

60 is a more reasonable guess for number of minutes in an hour than 100.
spotted by TeXitoi. ok deraadt


# 1.160 27-Jun-2010 jordan

Add _acpi_matchhids routine, precursor to pci device match


# 1.159 27-Jun-2010 mlarkin

Clear ACPI fixed event status on resume (power buttons/etc) so that some
machines don't immediately resume after suspending to S3.

Tested by kettenis@ and I on 5 or 6 machines, deraadt@ "yeah, looks good"


# 1.158 27-Jun-2010 jordan

Cleaned up acpi_maptable, adds to table list directly


# 1.157 07-Apr-2010 deraadt

If we have done a wsdisplay_suspend(), and ACPI decides it does not want to
suspend, we must roll back by calling wsdisplay_resume() or the virtual
console switching will be locked forever
ok oga


# 1.156 07-Apr-2010 kettenis

Make suspend/resume work on MP machines (running an MP kernel). Joint work
from mlarkin@ and me, with some amd64 fixes thrown in by deraadt@

ok marco@, deraadt@, pirofti@, mlarkin@


# 1.155 31-Mar-2010 kettenis

Bring /dev/apm support in line with apm(4). Make the suspend button send
a suspend request event to apmd(8) instead of suspending immediately. Also
keep track of whether /dev/apm and /dev/apmctl are currently open, such that
we can still suspend immediately if apmd(8) isn't running.

ok deraadt@, marco@, pirofti@, jsing@, oga@


# 1.154 30-Mar-2010 oga

Prevent the apmd/x races for good.

When we hit suspend time, go through all wsdisplays on the system. if
they are in mode MAPPED, but not MODE_DUMBFB then if possible do a full
vt switch to a !mapped vt, and prevent switching back until resume time.
This has to be called from MD code because this involves userland
running so that X can run the vt switch signal handler. This way, any
case where we are using the "poke registers from userland" model, we
will not be on the hardware when we go down, so the kernel can actually
handle thing properly.

Tested on several acpi laptops (by kettenis@ and ian@), x40 (me and
beck@ at LEAST) and zaurus (me). Maybe others, but if so I forgot who at
this time..

Idea from deraadt somewhere over the Faroe Islands (I thought of a
similar thing myself a while ago). Much prompting from him. Ok and
comments miod@


# 1.153 25-Mar-2010 oga

Fix & vs. && in the kqfilter code.

Based on a comparison with the apm code.

ok deraadt@, kettenis@


Revision tags: OPENBSD_4_7_BASE
# 1.152 26-Nov-2009 mlarkin

Visual (and on some machines, audio) feedback while the machine is
resuming via calls to aml _SST method. This also makes the "moon" LED on
thinkpads to blink during resume and cease when resume is completed.

ok pirofti@


# 1.151 26-Nov-2009 deraadt

Toggle into "cold" when we we disable interrupts, because tsleeping
in the AML interpreter and context switching into userland processes
tends to sometimes pop the ACPI trampoline page out of our address
space. Makes lots more machines work.
ok kettenis mlarkin


# 1.150 24-Nov-2009 jsg

in the resume path evaluate _WAK before dealing with device drivers
like the spec says, not after.

ok mlarkin@


# 1.149 24-Nov-2009 deraadt

abort suspends on SMP systems until that gets written; ok mlarkin


# 1.148 23-Nov-2009 mlarkin

pci should return the result of it's children's suspend/resume calls back
to its parent.

handle suspend failure case and unwind if devices have an activate function
that returns failure.

ok deraadt@, kettenis@


# 1.147 23-Nov-2009 mlarkin

acpi* devices need to call their children on suspend/resume.
ok deraadt@


# 1.146 23-Nov-2009 pirofti

Remove ACPI_SLEEP_ENABLED checks.

This enables by default the suspend/resume paths in the kernel.

Okay deraadt@.


# 1.145 23-Nov-2009 deraadt

knf and spacing that are super annoying


# 1.144 23-Nov-2009 mlarkin

Hook up Theo's new children suspend/resume function, as well as more ACPI
suspend/resume glue.

ok deraadt@


# 1.143 26-Oct-2009 deraadt

Do not do strncmp comparisons on dv_xname because longer device names which
look similar could arrive in the future. Instead, compare directly against
dv_cfdata->cf_driver->cd_name
Issue originally spotted by miod


# 1.142 26-Oct-2009 jordan

Verify checksum+address when loading ACPI tables.
Some systems had invalid entries in RSDT/XSDT.
ok marco@


# 1.141 23-Jul-2009 cnst

New aibs(4) driver for ASUSTeK AI Booster (ACPI ATK0110) hardware monitoring,
with sensor state support through limits provided by the ACPI.

Tested on several ASUS motherboards kindly networked by Sam Fourman Jr.

ok deraadt marco jordan


Revision tags: OPENBSD_4_6_BASE
# 1.140 03-Jun-2009 pirofti

Import acpi power resource driver. This is needed for suspend/resume logic.

Okay jordan@ and marco@.


# 1.139 03-Jun-2009 pirofti

Add acpivideo support. This adds brightness support for all laptops
except thinkpads, they will use the acpithinkpad driver. The driver is
also hooked into wsconsole. So brightness can be adjusted via:

$ wsconsctl display.brightness=<percentage>

This is very helpfull on some laptops that have a nasty bios and get two
steps instead of one when pressing the brightness button.

Tested on various dell, fujitsu, acer, samsung and other laptops.

Okay marco@, miod@. Suggestions from kettenis@.
Lots of reviews and help from miod@, thanks!


# 1.138 03-Jun-2009 jordan

Added ejectable bay for IDE cdroms
ok marco@


# 1.137 30-Apr-2009 marco

Oga changed apm ioctls and forgot to grep. Temporary work around by hitting
the sleep button when we get any request. This interface will chnage at
some point.
ok oga


# 1.136 19-Apr-2009 krw

Call acpi_sleep_walk() when powering down just like when entering
any other sleep state. This tells acpi wake devices to not wake up
the box until manual powering up resets their status. Makes my Dell
GX520 stay powered off in response to 'halt -p'.

In snaps for a while.

ok marco@ (several times) deraadt@


# 1.135 17-Apr-2009 pirofti

Add device ranking support for future sleep/wake developments.

okay marco@


# 1.134 11-Mar-2009 jordan

Cleanup.. use aml_evalinteger instead of aml_evalname
ok marco@


# 1.133 10-Mar-2009 jordan

Fix cut/paste error.. call _BFS not _PTS
ok marco@


Revision tags: OPENBSD_4_5_BASE
# 1.132 21-Feb-2009 marco

overzealous cut/paste; found by Vladimir Kirillov <proger@uaoug.org.ua>


# 1.131 20-Feb-2009 marco

Oops didn't mean for Debugger() to remain there.


# 1.130 19-Feb-2009 marco

suspend/resume bits so that we can develop this in tree. This is disabled.
code from mlarkin and me
help from art,toby,jordan and several others
ok jordan, go for it deraadt


# 1.129 10-Feb-2009 jordan

Fix null pointer for shutdown if acpi not enabled
ok marco@


# 1.128 04-Feb-2009 kettenis

Some systems that claim to be ACPI 2.0 apparently don't provide an XSDT
address. Make the code fall back on the RSDT on those systems.

From Joerg Niendorf <p3y80@internode.on.net>. Fixes PR 5986.


# 1.127 17-Jan-2009 krw

A couple of stray malloc/memset -> malloc(M_ZERO), a bit of knf and
lose a couple of unnecessary casts. No functional change.

Feedback from kettenis@

ok kettenis@ marco@


# 1.126 06-Nov-2008 marco

Collapse all sanity checks of hids into one function.

Originaly from jordan


Revision tags: OPENBSD_4_4_BASE
# 1.125 02-Jul-2008 fgsch

add acpivideo. for now almost an empty skel so others can work on
suspend/resume. eventually it will also manage output switching and
brightness where supported. prodded by marco@


# 1.124 11-Jun-2008 marco

Fix $OpenBSD$ for once and for all


# 1.123 08-Jun-2008 deraadt

aaa_dev was being set from an uninitialized variable; ok toby


# 1.122 07-Jun-2008 marco

Kill ACPIVERBOSE. It was never used and is worthless to boot.

comments jmc
ok toby


# 1.121 06-Jun-2008 marco

Shrink acpi some more for floppies and while here re-enable acpi on the
floppies. Fixes crazy sony vaio kettenis has.

tested by deraadt
ok deraadt jordan kettenis


# 1.120 01-Jun-2008 marco

Fix \n nit in SMALL_KERNEL.


# 1.119 01-Jun-2008 marco

Re-enable new parser. Lots of testing by various folks. Fits all media
now too.

ok kettenis deraadt


# 1.118 16-May-2008 dlg

back out jordans new parser.

it didnt have enough oks from the right people, it did break machines
that were previously working, and it bloated the RAMDISKS and install
media beyond the constraints of the media.

requested by deraadt@


# 1.117 14-May-2008 jordan

Adding new ACPI Parser code
Old guts of code still exists, needs to be torn out next
ok marco@
ok brad@


# 1.116 27-Apr-2008 jcs

add acpithinkpad(4), an acpi support driver for ibm/lenovo
thinkpads.

provides functionality that the bios did on older, non-acpi
thinkpads like respond to screen brightness keys and toggle the
bluetooth device on and off.

ok deraadt@ and marco@


# 1.115 24-Apr-2008 jsing

Introduce acpiasus(4), a driver for the ACPI based hotkeys found in many
ASUS laptops (including the ASUS EeePC) - largely based on NetBSD's
asus(4) driver. On the ASUS EeePC this allows us to enable/disable
wireless, change screen brightness and use the volume keys.

ok jsg@, weingart@


# 1.114 12-Apr-2008 kettenis

Get rid of acpi_s5 global variables; simply send SIGUSR2 instead of SUGUSR1
to tell init(8) to power down the machine.

ok krw@


Revision tags: OPENBSD_4_3_BASE
# 1.113 21-Jan-2008 jsg

Check against RESET_REG_SUP in FADT before doing the acpi reset dance
after all, not doing so appears to break more machines than it fixes.

ok marco@


# 1.112 05-Jan-2008 kettenis

Never attach more acpicpu(4) devices than cpu(4) devices. Always attach
the first acpicpu(4) device on non-MULTIPROCESSOR kernels, regardless of its
ACPI processor ID.

ok mikeb@


# 1.111 05-Dec-2007 deraadt

Hang acpi(4) below bios(4) instead of mainbus(4). This lets us move acpi(4)
before pcibios(4), and then.... if acpi(4) attaches, skip pcibios(4) since
it messes the machines up. Fixes claudio's HP dl320 G5, and almost assuredly
others.
ok kettenis


# 1.110 05-Dec-2007 deraadt

repair acpiopen/acpiclose and friends to handle the /dev/apmctl interface,
so that apmd(8) can actually work correctly against acpi(4), and thus,
even do the fancy -C/-A stuff; also tested by jmc


# 1.109 02-Dec-2007 fgsch

fix the trailing space for real now.


# 1.108 02-Dec-2007 fgsch

avoid trailing spaces on output; marco@ ok.


# 1.107 27-Nov-2007 kettenis

Fix off-by-one that made us attach acpicpu0 and acpicpu1 on GENERIC kernels.

ok marco@


# 1.106 25-Nov-2007 jsg

When available and ACPI is enabled use the ACPI reset method
to reboot. This is required for some quirky sis machines
and likely others to reboot.

Tested by many. 'commit early commit often' deraadt@


# 1.105 19-Nov-2007 deraadt

sigh, amd64 still uses that stupid acpi_enabled variable...


# 1.104 19-Nov-2007 deraadt

softc's come initialized to 0, you know...


# 1.103 19-Nov-2007 deraadt

store revision; from jsg


# 1.102 19-Nov-2007 kettenis

Initialize sc_facs earlier.

ok deraadt@, beck@, canacar@


# 1.101 16-Nov-2007 deraadt

fix the bus_space #define nightmare, so that amd64 and i386 are much more
uniform. as a result shared code like acpi needs less #ifdef's
ok marco kettenis


# 1.100 15-Nov-2007 deraadt

remove the #ifdef ACPI_ENABLE. the EC codepath is either safe enough (and
will be improved), or it is unsafe because AML assumes it must be there
ok almost everyone


# 1.99 15-Nov-2007 mikeb

Make acpicpu(4) attach to the existing CPUs only.

Problem reported by form@. Fix idea by kettenis@.
Lots of help from deraadt@. Tested by deraadt@, form@ and me.

ok deraadt marco


# 1.98 14-Nov-2007 deraadt

spacing


# 1.97 12-Nov-2007 deraadt

correct : vs , in attach-time messages


# 1.96 06-Nov-2007 deraadt

all ACPI_ENABLE kernels need acpi_interrupt()


# 1.95 06-Nov-2007 deraadt

move acpi_read_pmreg() and acpi_write_pmreg() out of SMALL_KERNEL so
that acpi ramdisks can compile (obvious solution)


# 1.94 05-Nov-2007 canacar

Establish the acpi interrupt earlier. Fixes problems with some
GENERIC.MP kernels not getting acpi interrupts. This is a
workaround until the real issue with late establishment is fixed.
ok weingart@, beck@, gwk@


# 1.93 03-Nov-2007 jordan

Added support for displaying wakeup devices
ok beck@,weingart@,gwk@


# 1.92 11-Oct-2007 kettenis

When walking the tree yo find _PRT methods, check whether the device is
actually enabled and functioning. Similar to what we do for _INI.

ok weingart@, beck@


# 1.91 08-Oct-2007 krw

More simple memset(,0,) -> M_ZERO changes. In this batch move to
size(*p) as the first malloc() parameter where p is declared locally
and thus easy to check. Add M_ZERO to gpe_table allocation in acpi.c
even though there is no obvious bzero or memset nearby.


# 1.90 13-Sep-2007 mk

Remove unnecessary memset() (see aml_evalnode(9)) and a bit of KNF.

ok toby


# 1.89 13-Sep-2007 weingart

Implement ACPI 6.5.1 spec tree walk for _STA and _INI.
Fixes mk's laptop. No regressions so far (thank you to the
testers).

ok gwk@, mk@, marco@


Revision tags: OPENBSD_4_2_BASE
# 1.88 17-Apr-2007 mk

Now that acpidock no longer hangs machines during boot (at least
according to the reports I've been getting) it should be safe to enable
it again, so remove the #if 0 stuff around it.

ok marco


# 1.87 11-Apr-2007 jordan

Added changes for C-State
ok marco@


# 1.86 26-Mar-2007 marco

Calculate minutes left more accurately.

From giovanni <qgiovanni@gmail.com>


Revision tags: OPENBSD_4_1_BASE
# 1.85 03-Mar-2007 mk

Stop looking for acpidock until we fix that stuff that breaks all pre-60
series ThinkPads with acpidock(4) enabled.


# 1.84 22-Feb-2007 marco

We were getting no interrupts in .MP. There are 2 fixes; one in
acpi_machdep.c and move the interrupt establishment down after ioapic
has been initialized.

ok jordan


# 1.83 21-Feb-2007 kettenis

Make an acpi-enabled GENERIC.MP work on systems that only support PIC mode.

ok marco@


# 1.82 19-Feb-2007 jordan

Added acpi_map_address function for mapping GAS/IO registers
ok marco@


# 1.81 18-Feb-2007 jordan

Added dynamic perfect hash for opcodes
ok marco@


# 1.80 18-Feb-2007 marco

Shave off another K pre compression.
Shuffle SMALL_KERNEL functions around so that we only have a single #ifndef


# 1.79 17-Feb-2007 marco

Add SMALL_KERNEL throught acpi to make it fit on boot media.
ok deraadt


# 1.78 13-Feb-2007 marco

Add smarter _INI code. This helps with some dissapearing devices.

ok kettenis jordan


# 1.77 31-Jan-2007 gwk

Introduce acpi_hasprocfvs thats "ACPI Has Processor Frequency and Voltage
Scalling" a global flag set by acicpu attach to indicate that ACPI is
handling fvs through the _PSS objects. This will fix disappearing
EST/powernow when booting kernels with acpi enabled, as acpicpu isnt
compiled into GENERIC yet. Also in cases where acpicpu is built in and
legacy EST/powernow work but acpi is missing the _PSS object, the legacy
routines will be used.

tested by dim@, and Nick Nauwelaerts <nick AT nauwelaerts.net>
ok marco@, dim@


# 1.76 22-Jan-2007 mk

Actually look for docking stations by looking for _DCK nodes.

ok marco


# 1.75 02-Jan-2007 marco

On some systems EC must be attached as the first device. Move EC from
generic _HID devices to its own routine to force first attach. Bug found
and debugged by joris@

Code mostly written by joris@


# 1.74 26-Dec-2006 marco

Add polling to devices that require it such as acpibat and acpitz.
Use the same tsleep/wakeup mechanism as the interrupt code to keep it non-
concurrent.


# 1.73 21-Dec-2006 deraadt

even more knf love


# 1.72 21-Dec-2006 marco

Remove some leftover acpiec gpe/interrupt code.


# 1.71 20-Dec-2006 deraadt

oops, name already used


# 1.70 20-Dec-2006 deraadt

rename __acpi_enable_gpe(); ok marco


# 1.69 20-Dec-2006 deraadt

acpi_unmap_pmregs() is not used, ok marco


# 1.68 19-Dec-2006 deraadt

simplify some low-level methods substantially (shrinking code); ok marco jordan


# 1.67 06-Dec-2006 mk

malloc(..., M_NOWAIT) with no NULL check is bad. If allocation fails,
complain, clean up, and bail out of attach.

input and ok gwk


# 1.66 28-Nov-2006 jordan

Fixed clearing arg for acpi_init_pic
ok kettenis@


# 1.65 27-Nov-2006 kettenis

Don't spam dmesg.


# 1.64 27-Nov-2006 jordan

Fixes for acpiprt routing
Post-parse fixup for namerefs
ok kettenis@


# 1.63 25-Nov-2006 marco

Don't handle acpiev events inside the gpe handler since this causes deadlocks.

ok jordan@


# 1.62 15-Nov-2006 kettenis

Hook up acpi apic interrupt routing support to generic acpi code.

ok jordan@


# 1.61 02-Nov-2006 marco

Kill acpiec attach function and add its functionality to the generic _HID
device attach

prodded and ok jordan


# 1.60 02-Nov-2006 marco

Fix comments to reflect reality.


# 1.59 19-Oct-2006 marco

Replace ACPI ioctls with APM ioctls. This makes ACPI talk the same protocol
as APM therefore userspace can now retrieve power information in the same
fashion as all other apm arches (zaurus, macppc, i386 etc).

To use force the use of acpi device like so: apmd -f /dev/acpi

ok jordan@


# 1.58 19-Oct-2006 jordan

Fixed GPE debug levels
Fixed ref counter on temporary bufferfields
DSDT parsing fully working on IBM Thinkpad
ok marco@


# 1.57 15-Oct-2006 jordan

Removed old GPE handler code remnants
ok marco@


# 1.56 12-Oct-2006 jordan

Massive ACPI changes
Fixed ACPI leaks
new ACPI API
ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.55 30-Jun-2006 gwk

KNF, No functional change,
"go at it" marco@


# 1.54 30-Jun-2006 jordan

Support for calling Embedded Controller via new GPE interface
ok marco@


# 1.53 30-Jun-2006 jordan

Added new code for GPE handling
Also works with ACPIEC; need to fixup acpiec.c to remove original interrupt handler
ok marco@


# 1.52 30-Jun-2006 jordan

Added offset argument for acpi read/write pmreg routines (new GPE code)
ok marco@


# 1.51 31-May-2006 todd

do not call acpi_powerdown() unless acpi has successfully initialized
ok gwk@ canacar@


# 1.50 29-May-2006 canacar

Add embedded controller support.
ok marco@ deraadt@ testing & ok gwk@


# 1.49 19-May-2006 canacar

Minimal thermal zone driver. Monitors thermal zone temperature,
shuts down the system if the 'critical temperature' is reached.
ok marco@


# 1.48 11-Apr-2006 marco

KNF.


# 1.47 11-Apr-2006 gwk

Add acpi_resume(struct acpi_softc *sc), which calls some AML methods,
detected at attach time, currently unused.

ok marco@


# 1.46 11-Apr-2006 gwk

Discover some ACPI sleep related methods, and call them at the
appropriate time in acpi_enter_sleep_state().

ok marco@


Revision tags: OPENBSD_3_9_BASE
# 1.45 26-Feb-2006 marco

Add acpicpu as a device.

ok jordan@


# 1.44 23-Feb-2006 marco

remove dead function.

ok jordan@


# 1.43 22-Feb-2006 marco

Print out all tables it found during autoconf. We'll need this data in
the future.


# 1.42 22-Feb-2006 marco

Make dmesg less verbose.

ok jordan.


# 1.41 22-Feb-2006 jordan

Added parsing of SSDT tables
ok marco@


# 1.40 22-Feb-2006 jordan

Added new aml_register to notify by device id
Moved powerdown code to button device
ok marco@


# 1.39 21-Feb-2006 marco

Readjust debug levels that crept in too low over the last few days.
KNF


# 1.38 21-Feb-2006 marco

Rig the power button so that the machine shuts down correctly.


# 1.37 20-Feb-2006 marco

Add acpibtn device. Remains disabled for now.


# 1.36 19-Feb-2006 jordan

Added code for aml_notify GPE notification callback
ok marco@


# 1.35 19-Feb-2006 grange

Fix acpi_enter_sleep_state() by replacing current ugly hack
with mostly correct sleeping state transition code.

ok brad@ marco@


# 1.34 19-Feb-2006 marco

jordan:
Enable interrupts.
Added peripheral code to make this work.

marco:
Replace softintr and other types of interrupts with a handler with context.
Fixed interrupt magic for GPEs.
Some KNF

This is work done by jordan and I but has to be a single commit.


# 1.33 17-Feb-2006 marco

Remove earlier introduced delays. We really only need them during autconf.

ok jordan@


# 1.32 17-Feb-2006 marco

Add a generic delay function as per the ACPI spec.

from jordan@


# 1.31 17-Feb-2006 marco

Use tsleep instead of delay when !cold.


# 1.30 17-Feb-2006 marco

Argh, we need delays between reads/writes. The backend reads (i2c most likely)
are so slow that values come back as 0 sometimes. We have to do this
despite the spec saying we shouldnt have to. Guess having a faster
parser than others can hurt sometimes.

Idea from jordan.


# 1.29 17-Feb-2006 jordan

Fixed code for handling _INI
Rearranged debug statements
ok marco@


# 1.28 16-Feb-2006 jordan

Added stub for calling _INI
Fixed aml_dowhile for NULL return
ok marco@


# 1.27 16-Feb-2006 jordan

Fixed parser/evaluator to retrieve battery info+state
Cleanup of parser
Dell laptops return battery status
ok marco@


# 1.26 06-Feb-2006 brad

fix a typo so this will compile on amd64.


# 1.25 04-Feb-2006 marco

When ACPI_ENABLED wasn't set but the devices were it had an unknown variable.

Pointed out by ian@.


# 1.24 03-Feb-2006 jordan

Rewrote aml parser yet again...
Fixed bus_space_map for i386/amd64
Added _BST and _BIF to acpibat to get status/info
ok marco@


# 1.23 20-Jan-2006 jordan

More evaluator changes
Removed old evaluator code
Fixed _HID value
ok marco@


# 1.22 19-Jan-2006 jordan

Added support for read/write of PCI Config space
Help jason@
ok marco@


# 1.21 18-Jan-2006 jordan

Added new evaluation routines
Added helper methods for buffer/field evaluation
Fixed broken indentation on dsdt.c

ok marco@


# 1.20 17-Jan-2006 jordan

Major overhaul of the aml parser. Evaluater temporarily disabled.

ok marco@


# 1.19 06-Jan-2006 grange

Check for ACPI control availability only if ACPI_ENABLE defined.


# 1.18 06-Jan-2006 grange

Informative error messages on attach, prettier dmesg.


# 1.17 06-Jan-2006 grange

printf -> dnprintf conversion for debug messages.


# 1.16 05-Jan-2006 grange

Find SLP_TYPx values and store them for future use in
acpi_enter_sleep_state().


# 1.15 05-Jan-2006 grange

Random KNF and cleanup


# 1.14 05-Jan-2006 grange

Provide symbolic names for the acpi sleeping states and use it.


# 1.13 28-Dec-2005 marco

Moved DEVNAME to a acpidev.h

ok jordan@


# 1.12 18-Dec-2005 sturm

add back comment about wrong code

ok marco


# 1.11 16-Dec-2005 marco

Remove test for kbd_reset. Prompted deraadt@, ok jordan@


# 1.10 16-Dec-2005 jordan

Added hooks for powerdown/power button press


# 1.9 16-Dec-2005 jordan

Cleaned up common power management register i/o
Added enter sleep state ioctl
Hooked power button interrupt
ok marco@


# 1.8 16-Dec-2005 marco

Remove autoconf chatter


# 1.7 16-Dec-2005 jordan

Moved DSDT AML parsing into parent ACPI driver
ok marco@


# 1.6 14-Dec-2005 marco

Add AC device glue.


# 1.5 14-Dec-2005 marco

Adjust debug printing levels to something more sane.


# 1.4 13-Dec-2005 marco

Add glue to attach devices to apci.


# 1.3 07-Dec-2005 marco

KNF


Revision tags: OPENBSD_3_8_BASE
# 1.2 10-Jul-2005 grange

Spacing cleanup.


# 1.1 02-Jun-2005 tholo

Start on a basic ACPI framework -- does not do much more than read out the
ACPI tables into kernel memory and attach ACPI and HPET timers currently.

In order to test this code, enabling the devices in GENERIC as well as
the ACPI_ENABLE option is needed. This code does not do any thermal
control yet, so this should be done with care depending on the platform.

In the tree so more people can contribute to making this more fully
featured.

Ok niklas@ grange@ tedu@


# 1.416 10-Sep-2022 kettenis

Don't restrict GPIO events to pin numbers below 256. With the _EVT()
method events corresponding to larger numbers are possible.

Fixes lid state detection on the x13s.

ok patrick@


# 1.415 03-Sep-2022 kettenis

Make sure we don't suspend if we have no way to wake up the machine.
The idea is that device drivers that can wake up the machine register
themselves by calling device_register_wakeup(). To prevent regressions
on amd64, we let acpi(4) register itself as a wakeup device if the AML
for the machine defines wakeup devices. This may be refined in the
future.

This diff will prevent people from suspending their arm64 SBCs without
having a way to wake them up. For now the only driver that registers
itself is axppmic(4), which means that at this moment only SBCs with
an allwinner A64 or H5 SoC will actually support suspend/resume.

ok mlarkin@, deraadt@


# 1.414 10-Aug-2022 patrick

Provide the AML root in ACPI's softc so that drivers that need access
don't have to declare it using extern.

Suggested by and with feedback from kettenis@
Tested by deraadt@ on arm64, amd64 and i386
Tested by bluhm@ on amd64 and i386


Revision tags: OPENBSD_7_1_BASE
# 1.413 17-Feb-2022 jsg

reduce includes


# 1.412 10-Feb-2022 visa

Embed klist head in acpi_softc to avoid explicit malloc.

OK kettenis@


# 1.411 09-Feb-2022 deraadt

Move x86-only stubs used by kern/subr_suspend.c into dev/acpi/acpi_x86.c
for sharing between i386 and amd64.
ok mlarkin kettenis


# 1.410 08-Feb-2022 deraadt

The suspend/resume code is a sticky mess of MI, MD, and ACPI sequencing.
This splits out the MI sequencing, backing it with per-architecture helper
functions. Further steps will be neccesary because ACPI and MD are too
tightly coupled, but soon we'll be able to use this code for more architectures
(which depends on figuring out the lowest-level cpu sleeping method)
ok kettenis


# 1.409 04-Feb-2022 robert

set the APM_BATT_CHARGING state if the battery is being charged and try
to calculate the remaining time to be fully charged

ok jca@


# 1.408 02-Feb-2022 deraadt

acpi_addtask() calls malloc() w/ M_NOWAIT (because some calls come from
interrupt context), this however means occasional resource shortage will
result in callbacks registration failing, and unknown consequences for
the task-submitting caller.
Changing this to use pools with a low water mark, decreases the odds
of that problem occuring.
ok kettenis


# 1.407 01-Feb-2022 deraadt

GPE_DIRECT is calling the AML parser from interrupt context, which is
not permitted. Luckily nothing is using GPE_DIRECT anymore, so this code
can be deleted.
ok kettenis


# 1.406 26-Jan-2022 kettenis

An ACPI device needs to be both present and enabled for it to function.
So only attempt to attach hardware that has both bits enabled. This fixes
an issue where com(4) would attach for a disabled serial port leading to
misdetection of the hardware variant and a subsequent hang when /etc/rc
runs ttyflags -a.

ok anton@, deraadt@


# 1.405 12-Jan-2022 patrick

Make acpi_getpropint() return uint64_t, as ACPI integers are in fact that
wide and some _DSD properties depend on it.

ok kettenis@


# 1.404 04-Jan-2022 patrick

acpi_getprop() needs to actually make sure that we're looking at the
correct property. While there adjust acpi_getpropint() as well to
increase similarity with acpi_getprop().

ok kettenis@


# 1.403 16-Dec-2021 anton

Attach com over acpi on amd64. Some hardware uses a different interrupt
assignment compared to the the legacy one supported by com over isa.
This causes the console to halt once userland takes over as no
interrupts are received. The actual address and irq can be read from
ACPI, kettenis@ already added support for arm64 which paved the way for
amd64.

Some consoles that previously attached over isa are now expected to
attach over acpi.

Thanks to patrick@ for testing on arm64.

ok kettenis@


# 1.402 07-Dec-2021 kettenis

Fix parsing of SR_IRQ resource descriptors.

ok patrick@, anton@


# 1.401 02-Nov-2021 deraadt

knf


# 1.400 30-Oct-2021 deraadt

Change hw.perfpolicy=auto by default, at startup. If the system has AC
power connected (default is yes when no driver differentiates) then default
to 100% performance. On battery, use the existing auto algorithm (which is
admittedly somewhat unrefined).
This change overrides the system/BIOS speed and puts OpenBSD in control.
As this happens very early during boot, besides speedups in all usage usage
patterns, some surprises: unhibernate and sysupgrade times are cut in half.
note: on a few architectures, the setperf fn pointer is changed late, and
thus the auto algorithm stops timeing out. kettenis and i will look for
a solution.
in snaps for more than a week.
ok kettenis


Revision tags: OPENBSD_7_0_BASE
# 1.399 20-Jul-2021 mlarkin

Fix trailing whitespace in a few places.

No code change.


# 1.398 09-May-2021 kettenis

Some x86 machines advertise the "hardware reduced" ACPI feature, advertise
S4 and S5 support, but fail to populate the SLEEP_CONTROL_REG and
SLEEP_STATUS_REG descriptions in the FADT. An example of such a machine
is the ASUS Zenbook 14 UM433DA. Any attempt to powerdown the machine
will result in a kernel crash. It turns out that using the legacy ACPI PM
registers works fine on this machien. So fall back on those registers
if SLEEP_CONTROL_REG and/or SLEEP_STATUS_REG aren't provided.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_9_BASE
# 1.397 15-Mar-2021 patrick

Add acpi_iommu_device_map(), which replaces the DMA tag with one that
is blessed with IOMMU magic, if available. This is mainly for arm64,
since on amd64 and i386 the IOMMU only captures PCIe devices, as far
as I know, which uses the pci_probe_device_hook(). This though is for
non-PCI devices attached through ACPI.

ok kettenis@


# 1.396 07-Mar-2021 patrick

Pass standard DMA tag to acpi(4) table drivers.

ok kettenis@


# 1.395 27-Dec-2020 deraadt

acpi_map_address() cannot be wrapped by SMALL_KERNEL anymore, as
it is used by acpihpet.c


# 1.394 25-Dec-2020 visa

Refactor klist insertion and removal

Rename klist_{insert,remove}() to klist_{insert,remove}_locked().
These functions assume that the caller has locked the klist. The current
state of locking remains intact because the kernel lock is still used
with all klists.

Add new functions klist_insert() and klist_remove() that lock the klist
internally. This allows some code simplification.

OK mpi@


# 1.393 17-Dec-2020 kettenis

Fix some issues with referencing named ACPI nodes from Packages.
These references need to be resolved at runtime rather than when
they're parsed such that they pick up the right values for those nodes
which can be changed when for example _INI methods run.

The current approach is to replace these reference with a string that
names the node in question. The problem with that is that packages
can also contain normal strings. Which means that we need special
code that depends on the context where the package is used.

This diff takes a different approach by retaining a reference when
parsing. Code that uses a package will need to resolve this reference
but there is no ambiguiety anymore.

ok patrick@


# 1.392 05-Dec-2020 kettenis

Add support for the _EVT method to the GPIO support code.

ok patrick@


Revision tags: OPENBSD_6_8_BASE
# 1.391 27-Aug-2020 jmatthew

Reorder the acpi attach process so we look at Processor() CPU nodes
after Device() ones, since we should prefer the newer node type.
If we see any Device() nodes, don't attach acpicpu(4) to any Processor()
nodes if they're also present. This also makes acpitz(4) and
acpipwrres(4) devices attach slightly later.

ok kettenis@, also tested by jmc@


# 1.390 16-Aug-2020 gnezdo

Remove sc_maxgpe from acpi_softc which was never read from

Remove a dead conditional from acpi_init_gpes while there.

ok jcs@


# 1.389 21-Jul-2020 deraadt

acpi can use IPL_BIO (a low interrupt) since it only enqueues operations for
later processing. The use of a high interrupt will predate suspend/resume
efforts, we had to redesign acpi to be non-reentrant obviously
discussed with kettenis, in snaps for more than a week


# 1.388 24-Jun-2020 cheloha

kernel: use gettime(9)/getuptime(9) in lieu of time_second(9)/time_uptime(9)

time_second(9) and time_uptime(9) are widely used in the kernel to
quickly get the system UTC or system uptime as a time_t. However,
time_t is 64-bit everywhere, so it is not generally safe to use them
on 32-bit platforms: you have a split-read problem if your hardware
cannot perform atomic 64-bit reads.

This patch replaces time_second(9) with gettime(9), a safer successor
interface, throughout the kernel. Similarly, time_uptime(9) is replaced
with getuptime(9).

There is a performance cost on 32-bit platforms in exchange for
eliminating the split-read problem: instead of two register reads you
now have a lockless read loop to pull the values from the timehands.
This is really not *too* bad in the grand scheme of things, but
compared to what we were doing before it is several times slower.

There is no performance cost on 64-bit (__LP64__) platforms.

With input from visa@, dlg@, and tedu@.

Several bugs squashed by visa@.

ok kettenis@


# 1.387 10-Jun-2020 jca

Give userland a consistent battery and A/C state after resume

Previously we refreshed the state of acpiac(4) and acpibat(4)/acpisbs(4)
by queueing fake ACPI events to be processed by the acpi thread.
acpibat(4) using ACPIDEV_POLL meant that its status could be refreshed
first, resulting in an APM_POWER_CHANGE event sent to userland before
the state of acpiac(4) was up to date. Because of this, apmd(8) could
see A/C unplugged when it was in fact plugged, possibly leading to
a wrong decision.

Instead of using fake ACPI events, refresh the status of acpiac(4) and
acpibat(4)/acpisbs(4) right away at DVACT_WAKEUP time. To reuse the
existing refresh code of those drivers, make their ACPI *_notify()
handlers responsible for sending APM_POWER_CHANGE events instead. Also
queue an APM_POWER_CHANGE event right after resume to retain existing
behavior.

This fixes the consistency of data shown to userland, and makes those
drivers less special.

Initially discussed last year (!) with mpi@ at Miod's place.
Feedback and ok jcs@, deraadt@ agrees with the use of DVACT_WAKEUP here.


# 1.386 29-May-2020 deraadt

dev/rndvar.h no longer has statistical interfaces (removed during various
conversion steps). it only contains kernel prototypes for 4 interfaces,
all of which legitimately belong in sys/systm.h, which are already included
by all enqueue_randomness() users.


# 1.385 14-May-2020 kettenis

Bring back the acpi(4) changes. This time around only use the ACPI _CRS
information on ACPI 5.0 and later.

ok krw@, patrick@


# 1.384 11-May-2020 jca

More accurate remaining power reporting with disparate batteries

Overall remaining power was computed as the average of the remaining
power of each battery, in percents. This doesn't work with batteries of
different capacity, so use the total capacity and remaining power
instead.

Reported by lidstah, successful tests by lidstah and benno@, ok kettenis@


# 1.383 08-May-2020 kettenis

Move parsing of _CRS into acpi(4) and pass the parsed address and interrupt
information in struct acpi_attach_args.

ok mpi@


Revision tags: OPENBSD_6_7_BASE
# 1.382 14-Apr-2020 kettenis

Implement acpi_getprop(), the ACPI equivalent of OF_getprop().

ok patrick@


# 1.381 12-Apr-2020 kettenis

Add support for the _CCA method. This method indicates whether DMA is
cache-coherent or not. To implement this, acpi(4) gets two bus_dma tags
and passes the appropriate one when attaching devices based on _CCA.
On i386/amd64, where for all practical purpose DMA is always cache-coherent,
the two tags are the same. But on arm64 they are distinct.

ok patrick@


# 1.380 07-Apr-2020 visa

Abstract the head of knote lists. This allows extending the lists,
for example, with locking assertions.

OK mpi@, anton@


# 1.379 03-Apr-2020 mpi

Fix incoherencies of errors returned by various *kqfilter routines.

EOPNOTSUPP is returned when there's no "filterops" for a given fd.
EINVAL when the requested filter isn't supported by the "filterops".
ENXIO means the underlying device is no longer valid/present.

ok visa@


# 1.378 20-Feb-2020 visa

Replace field f_isfd with field f_flags in struct filterops to allow
adding more filter properties without cluttering the struct.

OK mpi@, anton@


# 1.377 31-Dec-2019 visa

Use C99 designated initializers with struct filterops. In addition,
make the structs const so that the data are put in .rodata.

OK mpi@, deraadt@, anton@, bluhm@


# 1.376 31-Dec-2019 mpi

Convert infinite sleeps to {m,t}sleep_nsec(9).

ok kn@


# 1.375 31-Dec-2019 jsg

include sys/sensors.h for sensor use
don't depend on indirect inclusion from acpidev.h


Revision tags: OPENBSD_6_6_BASE
# 1.374 07-Sep-2019 kettenis

Revert acpipci(4) changes. Too many systems are broken. We'll try again
after 6.6 as been released. The acpireg.h change stays behind.


# 1.373 28-Aug-2019 kettenis

Use ACPI information to attach PCI busses like we do on arm64. There are a
few additional quirks though, and attaching the PCI busses is delayed to
replicate the existing code more closely. That may be changed in the
future. Also tweak how we handle MSI support and respect to ACPI flag
that says we shouldn't attempt to use MSIs.

Some fallout is expected.

ok patrick@


# 1.372 27-Aug-2019 deraadt

On amd64 the bootup and resume paths for SP/BP/SP were not performing
msr/pat, clock-startup, firmware upgrade, fpu initialization, and cpu
soft-state initialization in the same order. That kind of chaos is
crazy and fragile. Improve the acpi resume call sequence and unify order
of the operations.


# 1.371 02-Jul-2019 jcs

acpi_set_gpehandler: turn edge arg into flags, add support for
passing GPE_DIRECT which directly executes the supplied callback and
clears a level interrupt rather than going through acpi_taskq which
can be slow when other driver callbacks are in the queue.

This should not be used for GPE handlers that need to do anything
ACPI-related.

ok kettenis


# 1.370 10-Jun-2019 kettenis

Print proper ACPI version number.

ok mlarkin@


# 1.369 08-Jun-2019 kettenis

When establishing the mapping between ACPI device nodes and PCI devices,
skip PCI host bridges and devices that aren't there.

ok patrick@


# 1.368 07-Jun-2019 kettenis

Introduce acpi_getsta() and use it to eliminate some duplicated code.

ok patrick@


# 1.367 12-May-2019 kettenis

Make sure the extended PM register descriptions actually exist before
using them. Fixes machines with ancient ACPI 1.0 (such as recent
hypervisors).

ok deraadt@


# 1.366 12-May-2019 kettenis

Add support for mapping ACPI PM registers using the "extended" gasio
representation from the FADT. Mostly fixes the Lanner NCA-1510.

ok lteo@, mlarkin@, deraadt@


# 1.365 11-May-2019 lteo

Fix incorrect error message.

ok kettenis@


# 1.364 08-May-2019 tedu

free size. the allocation is the header plus the length.


Revision tags: OPENBSD_6_5_BASE
# 1.363 04-Apr-2019 kettenis

Avoid potential null-pointer dereference. Found by Kent R. Spillner.

ok deraadt@, jsg@


# 1.362 20-Jan-2019 jsg

fix SMALL_KERNEL build after changes in rev 1.361
reported by naddy@ ok deraadt@


# 1.361 19-Jan-2019 tedu

Add a pwraction sysctl that controls what the power button does on acpi.
By default, nothing changes -- shutdown is initiated. But allows turning
power button into a sleep button if desired.
(grudging) ok from a few parties


# 1.360 26-Oct-2018 kettenis

Add acpipci(4) on amd64. For now this only calls the PCI-specific _OSC
method to let the ACPI implementation know what features we support.


Revision tags: OPENBSD_6_4_BASE
# 1.359 30-Aug-2018 kettenis

Move softbutton check before the check that skips attaching certain acpi
devices. Fixes regression caused by matching on _CID in addition to
matching on _HID.

ok matthieu@, mlarkin@


# 1.358 25-Aug-2018 kettenis

Match ACPI devices based on _CID if no match for _HID is found.

ok mpi@


# 1.357 19-Aug-2018 kettenis

Add support for multiple PCI segments. Only really implemented for arm64
for now as amd64/i386 firmware still caters for legacy OSes that only
support a single PCI segment.

ok patrick@


# 1.356 03-Aug-2018 kettenis

Let ahci(4) match on _CLS instead of _HID when attaching at acpi(4). Avoids
having to add many more _HID entries to the match table.

ok deraadt@, mlarkin@


# 1.355 10-Jul-2018 kettenis

Make legacy interrupts work in acpipci(4).

ok patrick@


# 1.354 05-Jul-2018 kettenis

Add acpipci(4), a driver that supports generic ECAM-compatible PCI host
bridges based on information provided by ACPI.

ok mlarkin@


# 1.353 04-Jul-2018 kettenis

Properly pass around the PCI "chipset tag" in acpi(4) and refactor
acpimcfg(4) to call an MD initialization functions that sets up a tag for
PCI ECAM.

ok guenther@, mlarkin@, krw@


# 1.352 01-Jul-2018 kettenis

Move acpi(4) attach glue into acpi_machdep.c.

ok guenther@, deraadt@


# 1.351 01-Jul-2018 kettenis

Add support for _DSD Device Properties.

ok mlarkin@


# 1.350 30-Jun-2018 kettenis

Add intr_enable() function, intended for MI use to amd64 and i386 and use
this in the acpi(4) suspend/resume code paths.

ok deraadt@


# 1.349 29-Jun-2018 kettenis

Use <stdint.h> types.

ok mpi@, jung@, krw@, deraadt@


# 1.348 29-Jun-2018 mlarkin

ACPI: Allow (with warning) GPE handler reassignment, instead of returning
a failure code.

ok kettenis


# 1.347 26-Jun-2018 mlarkin

knf


# 1.346 26-Jun-2018 kettenis

Let acpi(4) pass down the bus dma tag.

ok deraadt@


# 1.345 25-Jun-2018 kettenis

Reorganize acpi(4) code a bit in preparation for upcoming arm64 support.

ok deraadt@


# 1.344 20-May-2018 kettenis

Wake up the acpi thread after scheduling a gpio event.

ok mpi@


# 1.343 17-May-2018 kettenis

Sprinkle some #ifndef SMALL_KERNEL such that RAMDISK kernels cuild again.

pointed out by stsp@


# 1.342 17-May-2018 kettenis

Implement GenericSerialBus OpRegion support.

ok mlarkin@


# 1.341 27-Mar-2018 kettenis

Add acpicmos(4), a driver that implements SystemCMOS OperationRegion
access support. This fixes machines where the AML doesn't check whether
support for this OperationRegion type has been registered by the OS.

ok mlarkin@


Revision tags: OPENBSD_6_3_BASE
# 1.340 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.339 10-Feb-2018 deraadt

Syncronize filesystems to disk when suspending. Each mountpoint's vnodes
are pushed to disk. Dangling vnodes (unlinked files still in use) and
vnodes undergoing change by long-running syscalls are identified -- and
such filesystems are marked dirty on-disk while we are suspended (in case
power is lost, a fsck will be required). Filesystems without dangling or
busy vnodes are marked clean, resulting in faster boots following
"battery died" circumstances.
Tested by numerous developers, thanks for the feedback.


# 1.338 08-Feb-2018 deraadt

After flushing disks, try again to release all memory. We don't need to
save dirty memory to the hibernate space.


# 1.337 08-Feb-2018 deraadt

Toss all releaseable memory, because fragmentation can get in the way
of allocating the hibernate playpen.


# 1.336 08-Feb-2018 deraadt

sr_quiesce() is a new approach for ensuring that softraid drains
output to the disks.

This is part of a larger suspend/resume filesystem-safety diff, which
has been worked on for a couple of months already. Tests by job, krw,
beck, benno, and others. Sometimes even by snapshot users...


# 1.335 29-Nov-2017 kettenis

Add support for GPIO-signaled events.

ok mlarkin@


# 1.334 16-Nov-2017 jcs

Add PCI attachment for dwiic(4) needed by Intel 100 Series machines

ok kettenis


# 1.333 14-Nov-2017 kettenis

Add a few more PNP IDs to the skip lists.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.332 17-Aug-2017 stsp

Make dwiic(4) attach its ACPI parent dependencies.
This change makes the Asus E200HA keyboard work.

Original analysis and patch by Cesare Gargano
ok kettenis@


# 1.331 09-Aug-2017 dcoppa

Add "PNP0303" (8042 PS/2 Controller) to acpi_isa_hids[]

ok kettenis@


# 1.330 22-Jul-2017 jcs

acpibat: look for _BIX first before falling back on _BIF

ACPI 4.0 deprecated _BIF for battery status so some newer machines
are exporting _BIX which extends _BIF's package with a few extra
fields. When using _BIX, export the new discharge cycle count as a
sensor.

feedback from kettenis and anton
ok deraadt


# 1.329 20-Jul-2017 kettenis

Start a list of isa(4) device names such that we can avoid printing them
as "not configured".

ok mikeb@, millert@, deraadt@


# 1.328 08-Apr-2017 deraadt

Sensors are run as callbacks inside tasks. During suspend a sensor
could be running inside a driver that will be force-detached, or due
to tsleep end up disrupting the softstate/hardstate contract. At
suspend time, quisce all these callbacks by waiting for completion.
This issue has never been observed for real, but may be implicated in
suspend/resume failures.
ok kettenis guenther mlarkin


# 1.327 08-Apr-2017 deraadt

Two prototypes for acpi_maptable; merge to one place


Revision tags: OPENBSD_6_1_BASE
# 1.326 27-Mar-2017 deraadt

Now that hibernate_alloc() only has clean success/failure, don't
need to call hibernate_free() to clean up a partial mess.
ok mlarkin kettenis


# 1.325 13-Mar-2017 deraadt

allow ramdisks to build; ok jcs


# 1.324 12-Mar-2017 jcs

try to attach acpisbs first and if successful, don't attach acpibat


# 1.323 02-Mar-2017 natano

Add a new sysctl machdep.lidaction. The sysctl works as follows:

machdep.lidaction=0 # do nothing
machdep.lidaction=1 # suspend
machdep.lidaction=2 # hibernate

lidsuspend is just an alias for lidaction, so if you change one, the
other one will have the same value. The plan is to remove
machdep.lidsuspend eventually when people have upgraded their
/ets/sysctl.conf.

discussed with deraadt, who came up with the new MIB name
no objections mlarkin
ok stsp halex jcs


# 1.322 28-Feb-2017 natano

Untangle abstract sleep modes from ACPI sleep states. This paves the way
for further work in the area.

idea and ok deraadt
ok mlarkin


# 1.321 22-Feb-2017 jcs

add acpisbs, an acpi smart battery subsystem driver reading data
over smbus

currently disabled because it conflicts with acpibat

ok deraadt, kettenis


# 1.320 14-Jan-2017 kettenis

Revert aml_rdpciaddr changes; breaks several machines that were working
before.


# 1.319 11-Jan-2017 jsg

Increase the size of the buffers used for _HID and _CID strings to be
able to fit "Hyper_V_Gen_Counter_V1".

ok mlarkin@ deraadt@


# 1.318 08-Jan-2017 kettenis

Make aml_rdpciaddr() work for for devices behind bridges. The code now
uses the mapping between AML nodes and PCI devices that we establish.
Because _INIT methods may end up calling aml_rdpciaddr(), make sure we
create that mapping early. Also handle devices that aren't actually present.
These devices are now included in the mapping and reads will return an
all-ones patterm whereas writes are a no-op..

ok mlarkin@


# 1.317 25-Oct-2016 pirofti

Add the resource index to aml_parse_resource() callback function.

This is needed by an upcoming acpiec commit that handles machines breaking
the current ACPI specifications.

Change suggested by and ok kettenis@, guenther@


# 1.316 18-Sep-2016 guenther

If the APIC table (MADT) isn't present then ci_acpi_proc_id won't be set
when acpicpu tries to attach, leading to a NULL dereference. The ACPI
processor object on the Dells where this was reported is bogus and useless
anyway so apply the same test when deciding whether to attach to processor
objects as acpicpu would when matching them to cpus.

Problem reported by Eivind Eide (xenofil (at) gmail.com) and
Johan Huldtgren (johan+openbsd-bugs (at) huldtgren.com).
Testing by Eivind Eide.
ok kettenis@ mlarkin@


# 1.315 03-Sep-2016 naddy

Write the system time back to the RTC every 30 minutes.
This fixes the problem that long-running machines which were not
shut down properly would reboot with a badly offset system time.

hints and ok kettenis@


# 1.314 31-Aug-2016 mlarkin

Don't put a device in the wake device list if _STA says it's not there.

Fixes at least one machine that woke up from sleep / poweroff immediately.

ok kettenis@, tested by kettenis and martijn@


# 1.313 28-Jul-2016 kettenis

Store the acpi processor ID/UID in struct cpu_info, and use it to attach
acpicpu(4) drivers to the right cpu(4).

ok mlarkin@, guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.312 10-Jun-2016 kettenis

The power management timer has been made optional in ACPI 5.0A, so don't
attempt to attach acpitimer(4) if the timer isn't present.

Pointed out by jcs@

ok jcs@


# 1.311 08-May-2016 kettenis

Make sure devices listed by a device's _DEP method are attached before
we attach the device itself. The _DEP methode, introduced in ACPI 5.0,
is a hint that the device in question depends on OpRegion support from
the devices listed by _DEP, so we have to order them the proper way.
To prevent us from attaching those devices again when we encounter them
later walking down the device tree, keep track of our attempts to
attach a device driver to them using aflag in the aml_node structure.

ok guenther@


# 1.310 25-Apr-2016 pirofti

We attach acpihpet(4) and acpiec(4) without using the HID, so do not
report it in the dmesg.

OK deraadt@, mlarkin@.


# 1.309 02-Apr-2016 kettenis

Reduce dmesg spam by knocking out some acpi hardware IDs that don't need a
driver.

ok mpi@


# 1.308 29-Mar-2016 kettenis

Check if a device is present (using _STA) in acpi_foundhid. This prevents
us from reporting devices as "not configured" that aren't actually present,
and allows us to remove duplicated code from several drivers.

ok deraadt@, ok jsg@


# 1.307 29-Mar-2016 kettenis

Implement a cleaner way to prevent acpivout(4) from attaching if we have
a platform-specific method of controlling the backlight.

ok millert@, mlarkin@, jung@


# 1.306 28-Mar-2016 kettenis

Adding each and every new acpi device driver to acpi_foundhid() is
gettinga bit of the burden. And it really isn't how our autoconf
framework is supposed to work for busses that can be enumerated. All
the drivers already check for a matching _HID in their attach
function. So we can just drop the checks in acpi_foundhid() and let
the drivers sort themselves out.

Print a "not configured" line for devices for which we don't attach adriver.
This may be a bit much as there are devices for which we will not have a
driver. This will be removed if it causes too much dmesg spam.

ok deraadt@, mlarkin@


Revision tags: OPENBSD_5_9_BASE
# 1.305 17-Jan-2016 jsg

Take acpi_foundhid() outside of #ifndef SMALL_KERNEL so the ramdisk
kernel can find and attach dwiic(4) and sdhc(4).


# 1.304 17-Jan-2016 jsg

remove an unused global variable


# 1.303 14-Jan-2016 kettenis

Attach dwiic(4) on the Intel Bay Trail i2c controllers as well.


# 1.302 12-Jan-2016 kettenis

Improve emulation of PM registers on Hardware-redcued ACPI by emulating
SPL_TYPx, SLP_EN and WAK_STS using the new SLEEP_STATUS_REG and
SLEEP_CONTROL_REG registers. Makes the transition into S5 (powerdown) work
on the ASUS X205TA.

ok deraadt@, mlarkin@


# 1.301 12-Jan-2016 jcs

Add dwiic, a driver for the Synopsys DesignWare i2c controller found
on the Samsung ATIV Book 9 laptop. This initial version only
supports ACPI config/attachment.

Add ihidev, a HID-over-i2c driver largely based on uhidev. dwiic
handles attaching ihidev devices found in ACPI.

Add ims, a HID-over-i2c mouse/trackpad driver to get basic cursor
and button functionality from HID-compliant i2c trackpads.

ok kettenis deraadt


# 1.300 11-Jan-2016 kettenis

Make sdhc(4) attach to hardware IDs "80860F14" and "PNP0FFF".


# 1.299 09-Jan-2016 kettenis

Initial support for Hardware-Reduced ACPI. This skips the legacy mode checks
by emulating the PM_CNT register such that SCI_EN is always enabled. It also
skip attaching acpitimer(4) since the relevant registers are no longer there.

ok deraadt@, mlarkin@


# 1.298 06-Jan-2016 kettenis

Follow the spec and check the SCI_EN bit to see if ACPI has already
been enabled and don't bother enabling it again if it is. This should fix
systems that don't implement legacy mode and therefore set ACPI_ENABLE and
ACPI_DISABLE to zero.

ok mlarkin@, yasuoka@


# 1.297 23-Nov-2015 reyk

Back out the CRITICAL change, turn it back from 10% to 25%. Some
people with old/broken batteries didn't like this change, and the
underlying heuristics are wrong.


# 1.296 22-Nov-2015 reyk

The "CRITICAL" APM battery level, that is exported/faked by the ACPI
stack, is currently set to 25%. With 25% battery left, a typical
modern laptop will have at least one hour runtime left. It is an
arbitrary value, and other APM power layers in the kernel set CRITICAL
to something like 5% or 10%, so set it to 10% for now.

OK mlarkin@


# 1.295 28-Sep-2015 deraadt

In low-level suspend routines, set cold=2. In tsleep(), use this to
spit out a ddb trace to console. This should allow us to find suspend
or resume routines which break the rules. It depends on the console
output function being non-sleeping.... but that's another codepath which
should try to be safe when cold is set.
ok kettenis


# 1.294 06-Sep-2015 deraadt

clarify malloc arguments (base + length...)


# 1.293 28-Aug-2015 deraadt

fairly simple sizes for free(); ok tedu


# 1.292 20-Aug-2015 kettenis

Respect the access size when reading or writing to pci config space.
This version also makes sure writes are properly aligned, preventing
the panics seen with the previous incarnation of this change.

Fixes battery status passthrough in vmware, brightness keys on my x220
and who knows what else.

ok mlarkin@, deraadt@


Revision tags: OPENBSD_5_8_BASE
# 1.291 04-Aug-2015 deraadt

If we are setting ACPI_PM1_BM_RLD do it only once, and remember to
repeat this action at resume time.
ok mlarkin guenther


# 1.290 29-Jul-2015 deraadt

Something subtle in previous revision is locking up suspend or booting
on some laptops. Mailing lists threads mention x220 and Asus machines.
Reverting for now.


# 1.289 21-Jul-2015 kettenis

iRespect the access size when reading or writing to pci config space.
Fixes battery status passthrough in vmware and who knows what else.

Tested by many.

ok mlarkin@, deraadt@


# 1.288 17-Jul-2015 mlarkin

fix comments


# 1.287 26-Mar-2015 jsg

Use the 0x10 extended capability to determine if the trackstick
buttons are wired to the trackpad and need to be re-routed to the
trackstick. Without this change the buttons on 2015 thinkpads
get picked up as extended buttons that show up as scroll up/down.

Remove the X1 Carbon 2015 (LEN0048) and X250 (LEN0046) from the
top button area/soft buttons quirks list. Also avoid using the
quirk list entirely if the capability bit is set.

Based on work from jcs@ with feedback from mpi@
ok mpi@ jcs@


# 1.286 20-Mar-2015 kettenis

The ACPI standard says that a PCI function number of FFFF can be used to refer
to all functions on a device. This makes no sense in the context of accessing
PCI config space. Nevertheless stsp@ has a machine with AML that tries to
do config space reads from a device where _ADR encodes function number FFFF.
This is almost certainly buggy AML, but since Linux (and presumably Windows)
boot just fine on this machine we need to work around this. Linux effectively
treats function FFFF as function 7, which isn't present on this machine.
Instead we simulate a read from a non-existent device by returning a bit
pattern of all ones. Writes to function FFFF will still panic.

ok mlarkin@


# 1.285 16-Mar-2015 deraadt

Reduce use of SMALL_KERNEL in acpi, bringing acpiec driver and more gpe
handling into RAMDISK. This is now possible because the install media
has ample room. The goal is to reduce special cases where we may be
skipping (unknown) important operations...
ok mlarkin kettenis


# 1.284 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.283 07-Feb-2015 deraadt

New framework that allows hibernate to pass in entropy from it's fresh
boot.
ok mlarkin


# 1.282 06-Feb-2015 mlarkin

Fix a hibernate crash on some machines due to unmapping a page that
may not have been mapped previously (in the failure to hibernate case).

Also ensure that the lowmem ptp is mapped in all cases (not just MP).

ok kettenis


# 1.281 17-Jan-2015 deraadt

resume_randomness() before spinning up other cpus...


# 1.280 15-Jan-2015 jsg

Define a new wscons mouse type for Synaptics clickpad devices that lack
physical buttons. This will be used if the acpi pnpid for the mouse
matches a list provided by Synaptics found in Linux.

Combined with the xenocara changes this will remove the need for an xorg
config file for the x240/t440/t540 generation of thinkpads.

ok matthieu@ shadchin@ kettenis@


# 1.279 11-Jan-2015 kettenis

Tolerate checksum errors in ACPI tables (but complain about them). Apparently
there are machines out there with broken BIOSen with such checksum errors in
thier RSDT tables. Windows runs fine on these machines, so it probably does
not check. Linux ignores checksum errors as well (and prints a warning).

ok deraadt@, krw@


# 1.278 18-Dec-2014 deraadt

suspend and resume the RNG (at what looks like the right spots)
ok reyk djm


# 1.277 09-Dec-2014 doug

Replace some malloc(n*size,...) calls with mallocarray().

ok tedu@ deraadt@


# 1.276 06-Dec-2014 mpi

If "machdep.lidsuspend" is not set, do not go back to sleep if the lid
is still closed when resuming. This allows docked laptop to be resumed
without opening them.

ok mlarkin@


# 1.275 23-Nov-2014 mlarkin

If a machine has no _LIDs, then none can be open. This diff fixes a problem
where machines without any _LIDs were going back to sleep after hibernate
due to the recent suspend-if-lid-closed-on-resume diff. Noticed on qemu,
which doesn't have any _LIDs.

discussed with deraadt


# 1.274 18-Nov-2014 krw

Nuke yet more obvious #include duplications.

ok miod@


# 1.273 08-Nov-2014 mlarkin

If resuming from sleep (zzz/ZZZ) and the lid is still closed, go back to
sleep. This prevents accidental lid flex or slight opening in a backpack
from waking the machine up and leaving it resumed (powered on).

ok deraadt@


# 1.272 26-Sep-2014 kettenis

Rework piglet and pig allocation. Currently the piglet gets allocated
deep down in the suspend path, where it is really hard to recover from
allocation failure. So allocate the piglet early on in the suspend path.
Also change the piglet and piglet allocation functions to use km_alloc(9)
instead of doing pmemrange magic. This removes a bunch of code which, in the
case of the piglet allocation, is broken since it results in a NULL pointer
dereference. Also switch the piglet allocation to not wait. If we can't
allocate 16MB of phys contig memory on a halfway modern machine we're almost
certainly under a lot of memory pressure and we're better off not trying to
hibernate anyway.

ok mlarkin@


# 1.271 25-Sep-2014 kettenis

Bring back rev 1.249. Now that mp_setperf() has been fixed, it should be
safe to restore perflevel when resuming. Should fix the issues people
see with the processor running at full speed heating up the machine while
the apm command reports that the CPUs are running at a low speed.

ok dcoppa@, deraadt@


# 1.270 23-Sep-2014 kettenis

Release the acpi lock around calls to wsdisplay_suspend() and
wsdisplay_resume(). The X server may adjust the display brightness just
before/after the VT switch when suspending/resuming. In fact, it may
already be blocked adjusting the display brightness for some reason
unrelated to the suspend request.

The VT switch request will eventually time out, but the delay is annoying
and the failed VT switch will lead to problems after resume.

Needed for a future update of the xf86-video-intel driver.

ok mlarkin@


# 1.269 19-Sep-2014 kettenis

Use config_suspend_all(9).


# 1.268 08-Sep-2014 kettenis

Clear the PME Status bit on PCI devices in response to Device Wake events.
Fixes the interrupt storm on the 2009 Mac mini with WOL enabled on its
nfe(4) interface.

ok mlarkin@


Revision tags: OPENBSD_5_6_BASE
# 1.267 20-Jul-2014 mlarkin

Support hibernating to softraid crypto volumes.

much help and ok from deraadt@


# 1.266 16-Jul-2014 mlarkin

Reenable hibernate RLE support and flush+zero all memory after unpack.


# 1.265 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.264 12-Jul-2014 kettenis

Add a function to drop all clean pages on the page daemon queues and call
it when we hibernate.

ok mlarkin@, miod@, deraadt@


# 1.263 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.262 11-Jul-2014 mlarkin

Flush the buffercache to 16MB on hibernate and restore its previous max
size (kern.bufcachepercent) on resume, for better hibernate performance.

ok beck@


# 1.261 10-Jul-2014 blambert

remove unneeded header

ok mlarkin@


# 1.260 01-Jul-2014 kettenis

Adding an additonal delay after each access to SystemMemory or SystemIO
during autoconf makes no sense.

ok deraadt@


# 1.259 23-Jun-2014 kettenis

SystemMemory is used to access memory mapped registers on some machines, so we
must use the bus_space(9) API to access it instead of memcpy(9). Also make
sure we properly align access to fields that need more than byte alignment.

ok guenther@


# 1.258 21-May-2014 mlarkin

Fix invalid / missing format specifiers in several dnprintf statements
(used only when ACPI_DEBUG is enabled)

From a diff on tech@ by Fabian Raetz, with a minor change by me, tested
on i386 and amd64 with ACPI_DEBUG enabled and disabled.


# 1.257 25-Apr-2014 mlarkin

Defer the free of the hibernate structures until later, when it is safe to
do so.

Freeing the hibernate structures too early leads to inadvertent calls into
splx when we aren't ready for them yet. Found when trapping splx calls in
the resume path.

discussed with deraadt@ at m2k14


# 1.256 13-Apr-2014 mlarkin

Fix a badly worded debug message that implied the entire machine was going
to sleep, instead of just the acpi thread going to sleep.


# 1.255 31-Mar-2014 mpi

Change the order of operations during a suspend/resume cycle and
call bufq_quiesce() after executing the DVACT_QUIESCE handlers.

This should be safe since no disk nor controller drivers have such
handler but it will allow us to detach sd(4) devices attached to a
USB bus.

Another benefit pointed out by kettenis@ is that drivers that need
to read a firmware from the disk should be able to do it at resume
time in a DVACT_WAKEUP handler.

ok kettenis@, deraadt@


# 1.254 13-Mar-2014 dlg

get rid of the assumption that the head of the alldevs list is the
"mainbus" device. this breaks when mpath is enabled because it
attaches before mainbus and therefore takes the head position.

have autoconf provide device_mainbus() which looks up mainbus_cd,
and use that instead.

discussed with deraadt who just wants mpath stuff to move forward
despite there being many ways to shine this particular turd.


Revision tags: OPENBSD_5_5_BASE
# 1.253 21-Feb-2014 deraadt

Do the lid control further on in the resume sequence, since this is
AML, and will do WAITOK operations. While cold, that is a bad idea.
We seem to be safe with respect to the spec, I do not see any event
debounce issues here.
ok kettenis, tested by many


# 1.252 20-Jan-2014 claudio

Instead of checking sc->sc_revision <= 1 check that the
fadt->hdr_revision <= 1. This is a better check since it checks
the version of the proper descriptor. This allows the alix APU
to reboot since those boards have currently a wrong sc->sc_revision.
Committing this now so that it can be tested easier.
With and OK jsg@ also fine by derradt@


# 1.251 24-Dec-2013 kettenis

If the FADT has its SMI_CMD set to zero, assume we're only ACPI-only hardware
and don't need to disable SMI ownership of the ACPI hardware registers.

ok mlarkin@


# 1.250 23-Dec-2013 kettenis

Revert previous commit. It makes resume hang on some MP systems running
GENERIC.MP.


# 1.249 16-Dec-2013 kettenis

Restore hw.setperf upon resume like we do for apm(4) on i386. Seems we need
to do this fairly late in the resume to avoid hangs.

ok deraadt@, mlarkin@


# 1.248 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


# 1.247 06-Nov-2013 mpi

Add support for Power Resources for Dx states and the necessary hook
for PCI devices. This hook should be called twice, before and after
changing the power state of a PCI device.

Before setting the device to the new state, the ACPI layer will notify
every power resources linked to the device for that state and make sure
they are turned "_ON". After changing the state of the device, it will
decrement the reference of every power resources linked to that device
for the old state and turn them "_OFF" if they are no longer referenced.

This fixes the no-USB after resume problem seen on various ThinkPad,
problem initialy diagnosed with Alexander Polakov.

ok kettenis@, deraadt@


Revision tags: OPENBSD_5_4_BASE
# 1.246 01-Jun-2013 mlarkin

Stop printing ACPI wakeup devices in dmesg after the 16th wakeup device.

Helps with VMware VMs that report hundreds of wakeup devices.

ok reyk@, deraadt@


# 1.245 31-May-2013 bluhm

Fix the build for a kernel without wd and pciide in its config.
OK deraadt@ tedu@


# 1.244 30-May-2013 deraadt

Enforce ca_activate tree-walks over the entire heirarchy for all events,
cleaning up some shutdown-hook related code on the way.
(A few drivers related to sparc are still skipped at kettenis' request)
ok kettenis mlarkin, tested by many others too


# 1.243 18-Apr-2013 deraadt

pin the acpi thread to the BSP, because AML and SMI cannot be trusted. I
was certain we were doing this already, but it turns out no...


Revision tags: OPENBSD_5_3_BASE
# 1.242 24-Dec-2012 guenther

In acpiioctl(), Return though the splx() path if suser() returns an error

ok deraadt@


# 1.241 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


# 1.240 04-Oct-2012 ehrhardt

Use information provided by ACPI to attach secondary PCI host bridges.

ok kettenis@


# 1.239 07-Sep-2012 kettenis

Add a new function called acpi_pci_min_powerstate() that returns the lowest
power state for a PCI device for the power state (currently S3 or S4) we're
transitioning into. This function is probably too simplistic; there is a lot
of (silly) complexity in the ACPI spec.

ok deraadt@


Revision tags: OPENBSD_5_2_BASE
# 1.238 13-Jul-2012 pirofti

Disable acpivideo(4) on ASUS laptops.

There are some models that panic whenever an event is received.
Some cope, some don't. In order to get this right acpiec(4) requires
major rework. Until then let machines boot.

Discussed with deraadt@.


# 1.237 11-Jul-2012 mlarkin

for now, restrict hibernate to root in the acpi ioctl kernel interface.
normal rules for controlling this interface through apmd still apply.

ok deraadt@


# 1.236 09-Jul-2012 deraadt

tiny little oops in untested hibernate block


# 1.235 09-Jul-2012 deraadt

clean up ioctl() case statements


# 1.234 09-Jul-2012 deraadt

acpi_disable_onegpe() is not used; ok pirofti


# 1.233 24-May-2012 kettenis

If _MIN > _MAX consider a WordBus resource to be invalid. Fixes a panic on
the IBM x3550-M4.

ok mikeb@


# 1.232 29-Mar-2012 mlarkin

Missing #ifdef in previous diff


# 1.231 29-Mar-2012 mlarkin

Prevent hibernation attempts on machines with unsupported disk controllers.
The same check is also performed later in the hibernate sequence, but by
then it's too late to cleanly unwind (presently).

ok deraadt


# 1.230 26-Mar-2012 deraadt

simplify conditionals in acpi_sleep_state()
ok mlarkin


# 1.229 26-Mar-2012 deraadt

hook in the hibernate request code; half of this diff is from mlarkin
ok mlarkin


Revision tags: OPENBSD_5_1_BASE
# 1.228 20-Sep-2011 deraadt

Very early in suspend while the clock is still ticking realtime,
save the clock back to the rtc. The zaurus already did this.
ok phessler miod kettenis


Revision tags: OPENBSD_5_0_BASE
# 1.227 02-Jul-2011 nicm

kqueue attach functions should return an errno or 0, not a plain 1. Fix
the obvious cases to return EINVAL and ENXIO.

ok tedu deraadt


# 1.226 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.225 16-Jun-2011 pirofti

Import acpitoshiba driver.

This is originally written by Hiroyuki Aizu, ported and integrated
in our tree by Javier Vazquez <javama4 AT gmail> with tweaks and
suggestions by me.

Import prodded by deraadt@ and the lack of testers on tech@.


# 1.224 27-Apr-2011 jcs

attach acpithinkpad to newer lenovo models like the x120e

ok deraadt@


# 1.223 22-Apr-2011 jordan

Fix uninitialzied variables and formatting strings (-Wxxx errors)


Revision tags: OPENBSD_4_9_BASE
# 1.222 02-Jan-2011 jordan

Re-add code for ACPI taskqueue, eliminates need for state vars in acpi softc
Serialize processing ACPI tasks for sleep/powerdown/GPE event
ok deraadt@


# 1.221 31-Oct-2010 guenther

Revert last commit: it breaks resume on Thinkpads
"then please back it out" deraadt@


# 1.220 26-Oct-2010 jordan

Add task queue for ACPI gpe and notify handlers
This fixes eject on Dell Latitude dock and an issue on the Dell Mini
battery update.
ok deraadt


# 1.219 07-Oct-2010 jordan

Create separate functions for enabling wake and runtime gpes.
Remove old suspend/resume gpewalk function
ok deraadt, mlarkin


# 1.218 05-Oct-2010 jordan

split acpi_enable_onegpe to separate enable/disable functions
ok deraadt


# 1.217 05-Oct-2010 jordan

Added acpi_disable_allgpes function to clear GPE status
ok deraadt


# 1.216 05-Oct-2010 deraadt

clean up acpi_softc use more


# 1.215 05-Oct-2010 deraadt

delete a bit of #if 0 code


# 1.214 05-Oct-2010 deraadt

hide use of our global acpi_softc pointer a bit better


# 1.213 29-Sep-2010 deraadt

grab the acpi thread lock very early in acpi thread startup; not that it
is likely that anyone will race us... but why be careless.
ok kettenis


# 1.212 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.211 29-Aug-2010 jasper

- add a check for FWRITE here too.

ok miod@


# 1.210 27-Aug-2010 kettenis

Make PCI Power Management optional, and only enable it when acpi(4) attaches.

ok deraadt@


Revision tags: OPENBSD_4_8_BASE
# 1.209 08-Aug-2010 kettenis

Close race between the acpi thread and wscons ioctls running in process
context. We might rip this out post-release and replace it with a solution
that serializes things in a more obvious way.

ok deraadt@, miod@ and "those crazy texans who ok anything"


# 1.208 08-Aug-2010 deraadt

Report the acpi sleep states when on a RAMDISK; ok krw


# 1.207 07-Aug-2010 kettenis

Enforce acpi register access rescrictions.

ok mlarkin@, deraadt@, tested by marco@


# 1.206 07-Aug-2010 deraadt

upon resume, notify all the acpiac and acpibat drivers to update
their status. some laptops do not do this automatically.
ok kettenis canacar


# 1.205 06-Aug-2010 marco

Always call _PSW on _LID devices that support it to enable lid open to
resume the machine. Conversely disable it when the machine wakes up.

Tested by several ok deraadt


# 1.204 06-Aug-2010 deraadt

As part of a revamp of the PM1/GPE code, I write this diff to look for
unmanaged EN & STS bits in the PM1 register at interrupt time and report
them. As a side effect this splits the STS acknowledgement into two writes
(for power, and sleep) instead of one. The printf that is added (to spot
unmanaged STS bits) has not yet been triggered as far as we know.

Before the "write to PM1 registers at the right offset" diff went in, this
was not neccessary. But newer thinkpads do not have a working soft power
button without this diff.

We have no idea why.
ok mlarkin kettenis


# 1.203 05-Aug-2010 deraadt

PM1 power button events were generating 'fake events' to acpibtn PWRB,
but if there is no such device these were being tossed. Instead, arrange
for all power button handlers to flag a softc variable which is processed
by the thread; so that one place will know to request a powerdown (psignal
to init)
ok marco mlarkin


# 1.202 05-Aug-2010 deraadt

rename a variable which is annoyingly poorly named


# 1.201 05-Aug-2010 mlarkin

Write to the correct offset for various ACPI pmreg registers. Writing to
the correct register does properly clear the wake status bits and fixed
event bits, which allows some machines to have more than 1 suspend cycle.

bug spotted by deraadt@

tested by deraadt@ and myself on a half-dozen machines and others on
hackers@

ok deraadt@, marco@


# 1.200 05-Aug-2010 deraadt

Fix the power button. The pm1 register is 16 bits in size.
My mistake, a while back.
tested by krw


# 1.199 04-Aug-2010 kettenis

Print supported sleep states.

ok marco@, phessler@, deraadt@


# 1.198 03-Aug-2010 mlarkin

Use the proper flag for re-enabling certain hardware events (power btn,
sleep btn). The (incorrect) flag prevoiously used coincidentally had the
same value, so this shouldn't cause any different behavior than before.

tested on a variety of machines (i386, amd64, sp, mp) - no behavioral
change seen

ok deraadt@


# 1.197 28-Jul-2010 marco

Make all gasio failures look and behave the same.


# 1.196 28-Jul-2010 mlarkin

Delay a little longer on suspend before giving up. Fixes machines that
sometimes don't suspend because the sleep transition takes too long
(symptoms like failing to sleep but the backlight stays on).

ok deraadt@


# 1.195 27-Jul-2010 deraadt

remove XXX; EC can now be attached early when needed; ok jordan


# 1.194 27-Jul-2010 deraadt

more brackets feels good


# 1.193 27-Jul-2010 deraadt

XXX annotate a block in acpi_enter_sleep_state() that is pretty worrying


# 1.192 27-Jul-2010 deraadt

horrific indentation that nearly made me puke


# 1.191 27-Jul-2010 mlarkin

Set the sleeping indicator light on machines that support it, in case SMI
doesn't do it for us. Shows the 'moon' LED on older Thinkpads when
sleeping.

Tested on T43p, W500, T510 by me and on X61s by deraadt@.

ok deraadt@


# 1.190 27-Jul-2010 jordan

Early initialization of acpiec if ECDT table exists
Fixes hang when booting thinkpads while docked
ok deraadt


# 1.189 26-Jul-2010 deraadt

remove merge error which was commited


# 1.188 26-Jul-2010 pirofti

Add support for Sony ACPI hotkeys via a new driver: acpisony(4).

Currently it only works for the suspend button and tries to do right for
the brightness events, but I haven't found a Sony laptop that like to do
right so far.

In the future I want to make the brightness keys work on all Sony's and
also add support for the zoom hotkeys and whatever other funky keys I can
find on those things.

Okay deraadt@.


# 1.187 22-Jul-2010 deraadt

pretty large cleanup of SMALL_KERNEL


# 1.186 21-Jul-2010 deraadt

in the acpi GPE handler, late ack level interrupts not edge interrupts.
Duh.


# 1.185 21-Jul-2010 deraadt

Merge the edge and level gpe handlers into one (now possible since the
gpe structure tells us whether the gpe is edge or level)


# 1.184 21-Jul-2010 deraadt

delete #if 0 code we are not going to use; ok marco


# 1.183 20-Jul-2010 deraadt

make the mask code for acpi_enable_onegpe() match other similar code


# 1.182 20-Jul-2010 deraadt

Handle the acpi interrupt controller with proper edge/level handling,
wrapped in spltty to avoid racing against the interrupt controller.
Repair the gpe bit masking code while there, and do operations in the
right order
ok kettenis mlarkin, help from jordan at figuring out the order of
operations.


# 1.181 20-Jul-2010 matthew

When we're preparing to go to sleep, only call bufq_restart() if other
preparations failed.

ok kettenis@, thib@, deraadt@


# 1.180 19-Jul-2010 deraadt

permit compilation with ACPI_DEBUG


# 1.179 19-Jul-2010 deraadt

there is no need for a seperate dsdt_softc pointer; dsdt does not have a
softc but is just a part of the single acpi driver.
ok jordan


# 1.178 19-Jul-2010 deraadt

acpi_delay() is no longer needed; ok jordan


# 1.177 18-Jul-2010 mlarkin

Clear event status on resume, to avoid going back to sleep/reboot/shutdown
immediately after resume. Tested by a bunch of people on hackers@ on
various hardware. Fixes at least Toshiba NB200 and Gateway LT3103u.

ok marco


# 1.176 13-Jul-2010 deraadt

rename some thread related interfaces so they are logical


# 1.175 10-Jul-2010 jordan

Verify _ADR bdf is correct (some systems return 0xffff)
Works on Dell x9xx and Studio 1555


# 1.174 09-Jul-2010 jordan

Clear the noise


# 1.173 08-Jul-2010 jordan

Add mapping for ACPI device to PCI bdf (match autoconf tree)
Simplify resource parsing function to use buffer argument
Convert namespace linked lists to use queue macros
ok marco@, deraadt@


# 1.172 06-Jul-2010 deraadt

Use spltty() to lock downcalls from apm against the information being
modified by the acpi thread.
ok kettenis


# 1.171 06-Jul-2010 deraadt

Don't use a workq to do the suspend, because it races aginst the acpi
thread. Instead, just tell the acpi thread to do the suspend for us.
This makes apmd & zzz work correctly. While here, have acpithinkpad
attempt to post the event to apm before prompting the suspend itself.
ok kettenis marco mlarkin


# 1.170 05-Jul-2010 mlarkin

Reset register SCI_EN on resume. The ACPI spec clearly states we don't have
to do this, but many machines don't, or forget to, which leads to all sorts
of problems on resume (broken battery stats, overheating, failure to resume
more than once, etc).

tested by many, ok marco@, deraadt@


# 1.169 01-Jul-2010 thib

do bufq quiesce again, since vnd/the bufq_done call in
biodone has been fixed.


# 1.168 01-Jul-2010 jordan

Backout recent AML changes, breaks on R210 and others


# 1.167 01-Jul-2010 jordan

Simplified aml_parse_resource call, cleanup code
ok marco


# 1.166 01-Jul-2010 jordan

Fix subordinate bus # for multi-root PCI buses
ok marco, kettenis


# 1.165 30-Jun-2010 thib

Disable/partially backout the bufq quiesce changes as this
is causing havoc with vnds and release must be buildable.


# 1.164 29-Jun-2010 deraadt

Fix ramdisks (new code was placed inside a SMALL_KERNEL chunk)


# 1.163 29-Jun-2010 jordan

Add support for mapping ACPI to PCI devices
ok kettenis, deraadt


# 1.162 29-Jun-2010 kettenis

Call bufq_quiesce() on suspend to stop all I/O and bufq_restart on resume.
Makes suspend/resume work much better while doing disk I/O.

ok mlarkin@


# 1.161 29-Jun-2010 tedu

60 is a more reasonable guess for number of minutes in an hour than 100.
spotted by TeXitoi. ok deraadt


# 1.160 27-Jun-2010 jordan

Add _acpi_matchhids routine, precursor to pci device match


# 1.159 27-Jun-2010 mlarkin

Clear ACPI fixed event status on resume (power buttons/etc) so that some
machines don't immediately resume after suspending to S3.

Tested by kettenis@ and I on 5 or 6 machines, deraadt@ "yeah, looks good"


# 1.158 27-Jun-2010 jordan

Cleaned up acpi_maptable, adds to table list directly


# 1.157 07-Apr-2010 deraadt

If we have done a wsdisplay_suspend(), and ACPI decides it does not want to
suspend, we must roll back by calling wsdisplay_resume() or the virtual
console switching will be locked forever
ok oga


# 1.156 07-Apr-2010 kettenis

Make suspend/resume work on MP machines (running an MP kernel). Joint work
from mlarkin@ and me, with some amd64 fixes thrown in by deraadt@

ok marco@, deraadt@, pirofti@, mlarkin@


# 1.155 31-Mar-2010 kettenis

Bring /dev/apm support in line with apm(4). Make the suspend button send
a suspend request event to apmd(8) instead of suspending immediately. Also
keep track of whether /dev/apm and /dev/apmctl are currently open, such that
we can still suspend immediately if apmd(8) isn't running.

ok deraadt@, marco@, pirofti@, jsing@, oga@


# 1.154 30-Mar-2010 oga

Prevent the apmd/x races for good.

When we hit suspend time, go through all wsdisplays on the system. if
they are in mode MAPPED, but not MODE_DUMBFB then if possible do a full
vt switch to a !mapped vt, and prevent switching back until resume time.
This has to be called from MD code because this involves userland
running so that X can run the vt switch signal handler. This way, any
case where we are using the "poke registers from userland" model, we
will not be on the hardware when we go down, so the kernel can actually
handle thing properly.

Tested on several acpi laptops (by kettenis@ and ian@), x40 (me and
beck@ at LEAST) and zaurus (me). Maybe others, but if so I forgot who at
this time..

Idea from deraadt somewhere over the Faroe Islands (I thought of a
similar thing myself a while ago). Much prompting from him. Ok and
comments miod@


# 1.153 25-Mar-2010 oga

Fix & vs. && in the kqfilter code.

Based on a comparison with the apm code.

ok deraadt@, kettenis@


Revision tags: OPENBSD_4_7_BASE
# 1.152 26-Nov-2009 mlarkin

Visual (and on some machines, audio) feedback while the machine is
resuming via calls to aml _SST method. This also makes the "moon" LED on
thinkpads to blink during resume and cease when resume is completed.

ok pirofti@


# 1.151 26-Nov-2009 deraadt

Toggle into "cold" when we we disable interrupts, because tsleeping
in the AML interpreter and context switching into userland processes
tends to sometimes pop the ACPI trampoline page out of our address
space. Makes lots more machines work.
ok kettenis mlarkin


# 1.150 24-Nov-2009 jsg

in the resume path evaluate _WAK before dealing with device drivers
like the spec says, not after.

ok mlarkin@


# 1.149 24-Nov-2009 deraadt

abort suspends on SMP systems until that gets written; ok mlarkin


# 1.148 23-Nov-2009 mlarkin

pci should return the result of it's children's suspend/resume calls back
to its parent.

handle suspend failure case and unwind if devices have an activate function
that returns failure.

ok deraadt@, kettenis@


# 1.147 23-Nov-2009 mlarkin

acpi* devices need to call their children on suspend/resume.
ok deraadt@


# 1.146 23-Nov-2009 pirofti

Remove ACPI_SLEEP_ENABLED checks.

This enables by default the suspend/resume paths in the kernel.

Okay deraadt@.


# 1.145 23-Nov-2009 deraadt

knf and spacing that are super annoying


# 1.144 23-Nov-2009 mlarkin

Hook up Theo's new children suspend/resume function, as well as more ACPI
suspend/resume glue.

ok deraadt@


# 1.143 26-Oct-2009 deraadt

Do not do strncmp comparisons on dv_xname because longer device names which
look similar could arrive in the future. Instead, compare directly against
dv_cfdata->cf_driver->cd_name
Issue originally spotted by miod


# 1.142 26-Oct-2009 jordan

Verify checksum+address when loading ACPI tables.
Some systems had invalid entries in RSDT/XSDT.
ok marco@


# 1.141 23-Jul-2009 cnst

New aibs(4) driver for ASUSTeK AI Booster (ACPI ATK0110) hardware monitoring,
with sensor state support through limits provided by the ACPI.

Tested on several ASUS motherboards kindly networked by Sam Fourman Jr.

ok deraadt marco jordan


Revision tags: OPENBSD_4_6_BASE
# 1.140 03-Jun-2009 pirofti

Import acpi power resource driver. This is needed for suspend/resume logic.

Okay jordan@ and marco@.


# 1.139 03-Jun-2009 pirofti

Add acpivideo support. This adds brightness support for all laptops
except thinkpads, they will use the acpithinkpad driver. The driver is
also hooked into wsconsole. So brightness can be adjusted via:

$ wsconsctl display.brightness=<percentage>

This is very helpfull on some laptops that have a nasty bios and get two
steps instead of one when pressing the brightness button.

Tested on various dell, fujitsu, acer, samsung and other laptops.

Okay marco@, miod@. Suggestions from kettenis@.
Lots of reviews and help from miod@, thanks!


# 1.138 03-Jun-2009 jordan

Added ejectable bay for IDE cdroms
ok marco@


# 1.137 30-Apr-2009 marco

Oga changed apm ioctls and forgot to grep. Temporary work around by hitting
the sleep button when we get any request. This interface will chnage at
some point.
ok oga


# 1.136 19-Apr-2009 krw

Call acpi_sleep_walk() when powering down just like when entering
any other sleep state. This tells acpi wake devices to not wake up
the box until manual powering up resets their status. Makes my Dell
GX520 stay powered off in response to 'halt -p'.

In snaps for a while.

ok marco@ (several times) deraadt@


# 1.135 17-Apr-2009 pirofti

Add device ranking support for future sleep/wake developments.

okay marco@


# 1.134 11-Mar-2009 jordan

Cleanup.. use aml_evalinteger instead of aml_evalname
ok marco@


# 1.133 10-Mar-2009 jordan

Fix cut/paste error.. call _BFS not _PTS
ok marco@


Revision tags: OPENBSD_4_5_BASE
# 1.132 21-Feb-2009 marco

overzealous cut/paste; found by Vladimir Kirillov <proger@uaoug.org.ua>


# 1.131 20-Feb-2009 marco

Oops didn't mean for Debugger() to remain there.


# 1.130 19-Feb-2009 marco

suspend/resume bits so that we can develop this in tree. This is disabled.
code from mlarkin and me
help from art,toby,jordan and several others
ok jordan, go for it deraadt


# 1.129 10-Feb-2009 jordan

Fix null pointer for shutdown if acpi not enabled
ok marco@


# 1.128 04-Feb-2009 kettenis

Some systems that claim to be ACPI 2.0 apparently don't provide an XSDT
address. Make the code fall back on the RSDT on those systems.

From Joerg Niendorf <p3y80@internode.on.net>. Fixes PR 5986.


# 1.127 17-Jan-2009 krw

A couple of stray malloc/memset -> malloc(M_ZERO), a bit of knf and
lose a couple of unnecessary casts. No functional change.

Feedback from kettenis@

ok kettenis@ marco@


# 1.126 06-Nov-2008 marco

Collapse all sanity checks of hids into one function.

Originaly from jordan


Revision tags: OPENBSD_4_4_BASE
# 1.125 02-Jul-2008 fgsch

add acpivideo. for now almost an empty skel so others can work on
suspend/resume. eventually it will also manage output switching and
brightness where supported. prodded by marco@


# 1.124 11-Jun-2008 marco

Fix $OpenBSD$ for once and for all


# 1.123 08-Jun-2008 deraadt

aaa_dev was being set from an uninitialized variable; ok toby


# 1.122 07-Jun-2008 marco

Kill ACPIVERBOSE. It was never used and is worthless to boot.

comments jmc
ok toby


# 1.121 06-Jun-2008 marco

Shrink acpi some more for floppies and while here re-enable acpi on the
floppies. Fixes crazy sony vaio kettenis has.

tested by deraadt
ok deraadt jordan kettenis


# 1.120 01-Jun-2008 marco

Fix \n nit in SMALL_KERNEL.


# 1.119 01-Jun-2008 marco

Re-enable new parser. Lots of testing by various folks. Fits all media
now too.

ok kettenis deraadt


# 1.118 16-May-2008 dlg

back out jordans new parser.

it didnt have enough oks from the right people, it did break machines
that were previously working, and it bloated the RAMDISKS and install
media beyond the constraints of the media.

requested by deraadt@


# 1.117 14-May-2008 jordan

Adding new ACPI Parser code
Old guts of code still exists, needs to be torn out next
ok marco@
ok brad@


# 1.116 27-Apr-2008 jcs

add acpithinkpad(4), an acpi support driver for ibm/lenovo
thinkpads.

provides functionality that the bios did on older, non-acpi
thinkpads like respond to screen brightness keys and toggle the
bluetooth device on and off.

ok deraadt@ and marco@


# 1.115 24-Apr-2008 jsing

Introduce acpiasus(4), a driver for the ACPI based hotkeys found in many
ASUS laptops (including the ASUS EeePC) - largely based on NetBSD's
asus(4) driver. On the ASUS EeePC this allows us to enable/disable
wireless, change screen brightness and use the volume keys.

ok jsg@, weingart@


# 1.114 12-Apr-2008 kettenis

Get rid of acpi_s5 global variables; simply send SIGUSR2 instead of SUGUSR1
to tell init(8) to power down the machine.

ok krw@


Revision tags: OPENBSD_4_3_BASE
# 1.113 21-Jan-2008 jsg

Check against RESET_REG_SUP in FADT before doing the acpi reset dance
after all, not doing so appears to break more machines than it fixes.

ok marco@


# 1.112 05-Jan-2008 kettenis

Never attach more acpicpu(4) devices than cpu(4) devices. Always attach
the first acpicpu(4) device on non-MULTIPROCESSOR kernels, regardless of its
ACPI processor ID.

ok mikeb@


# 1.111 05-Dec-2007 deraadt

Hang acpi(4) below bios(4) instead of mainbus(4). This lets us move acpi(4)
before pcibios(4), and then.... if acpi(4) attaches, skip pcibios(4) since
it messes the machines up. Fixes claudio's HP dl320 G5, and almost assuredly
others.
ok kettenis


# 1.110 05-Dec-2007 deraadt

repair acpiopen/acpiclose and friends to handle the /dev/apmctl interface,
so that apmd(8) can actually work correctly against acpi(4), and thus,
even do the fancy -C/-A stuff; also tested by jmc


# 1.109 02-Dec-2007 fgsch

fix the trailing space for real now.


# 1.108 02-Dec-2007 fgsch

avoid trailing spaces on output; marco@ ok.


# 1.107 27-Nov-2007 kettenis

Fix off-by-one that made us attach acpicpu0 and acpicpu1 on GENERIC kernels.

ok marco@


# 1.106 25-Nov-2007 jsg

When available and ACPI is enabled use the ACPI reset method
to reboot. This is required for some quirky sis machines
and likely others to reboot.

Tested by many. 'commit early commit often' deraadt@


# 1.105 19-Nov-2007 deraadt

sigh, amd64 still uses that stupid acpi_enabled variable...


# 1.104 19-Nov-2007 deraadt

softc's come initialized to 0, you know...


# 1.103 19-Nov-2007 deraadt

store revision; from jsg


# 1.102 19-Nov-2007 kettenis

Initialize sc_facs earlier.

ok deraadt@, beck@, canacar@


# 1.101 16-Nov-2007 deraadt

fix the bus_space #define nightmare, so that amd64 and i386 are much more
uniform. as a result shared code like acpi needs less #ifdef's
ok marco kettenis


# 1.100 15-Nov-2007 deraadt

remove the #ifdef ACPI_ENABLE. the EC codepath is either safe enough (and
will be improved), or it is unsafe because AML assumes it must be there
ok almost everyone


# 1.99 15-Nov-2007 mikeb

Make acpicpu(4) attach to the existing CPUs only.

Problem reported by form@. Fix idea by kettenis@.
Lots of help from deraadt@. Tested by deraadt@, form@ and me.

ok deraadt marco


# 1.98 14-Nov-2007 deraadt

spacing


# 1.97 12-Nov-2007 deraadt

correct : vs , in attach-time messages


# 1.96 06-Nov-2007 deraadt

all ACPI_ENABLE kernels need acpi_interrupt()


# 1.95 06-Nov-2007 deraadt

move acpi_read_pmreg() and acpi_write_pmreg() out of SMALL_KERNEL so
that acpi ramdisks can compile (obvious solution)


# 1.94 05-Nov-2007 canacar

Establish the acpi interrupt earlier. Fixes problems with some
GENERIC.MP kernels not getting acpi interrupts. This is a
workaround until the real issue with late establishment is fixed.
ok weingart@, beck@, gwk@


# 1.93 03-Nov-2007 jordan

Added support for displaying wakeup devices
ok beck@,weingart@,gwk@


# 1.92 11-Oct-2007 kettenis

When walking the tree yo find _PRT methods, check whether the device is
actually enabled and functioning. Similar to what we do for _INI.

ok weingart@, beck@


# 1.91 08-Oct-2007 krw

More simple memset(,0,) -> M_ZERO changes. In this batch move to
size(*p) as the first malloc() parameter where p is declared locally
and thus easy to check. Add M_ZERO to gpe_table allocation in acpi.c
even though there is no obvious bzero or memset nearby.


# 1.90 13-Sep-2007 mk

Remove unnecessary memset() (see aml_evalnode(9)) and a bit of KNF.

ok toby


# 1.89 13-Sep-2007 weingart

Implement ACPI 6.5.1 spec tree walk for _STA and _INI.
Fixes mk's laptop. No regressions so far (thank you to the
testers).

ok gwk@, mk@, marco@


Revision tags: OPENBSD_4_2_BASE
# 1.88 17-Apr-2007 mk

Now that acpidock no longer hangs machines during boot (at least
according to the reports I've been getting) it should be safe to enable
it again, so remove the #if 0 stuff around it.

ok marco


# 1.87 11-Apr-2007 jordan

Added changes for C-State
ok marco@


# 1.86 26-Mar-2007 marco

Calculate minutes left more accurately.

From giovanni <qgiovanni@gmail.com>


Revision tags: OPENBSD_4_1_BASE
# 1.85 03-Mar-2007 mk

Stop looking for acpidock until we fix that stuff that breaks all pre-60
series ThinkPads with acpidock(4) enabled.


# 1.84 22-Feb-2007 marco

We were getting no interrupts in .MP. There are 2 fixes; one in
acpi_machdep.c and move the interrupt establishment down after ioapic
has been initialized.

ok jordan


# 1.83 21-Feb-2007 kettenis

Make an acpi-enabled GENERIC.MP work on systems that only support PIC mode.

ok marco@


# 1.82 19-Feb-2007 jordan

Added acpi_map_address function for mapping GAS/IO registers
ok marco@


# 1.81 18-Feb-2007 jordan

Added dynamic perfect hash for opcodes
ok marco@


# 1.80 18-Feb-2007 marco

Shave off another K pre compression.
Shuffle SMALL_KERNEL functions around so that we only have a single #ifndef


# 1.79 17-Feb-2007 marco

Add SMALL_KERNEL throught acpi to make it fit on boot media.
ok deraadt


# 1.78 13-Feb-2007 marco

Add smarter _INI code. This helps with some dissapearing devices.

ok kettenis jordan


# 1.77 31-Jan-2007 gwk

Introduce acpi_hasprocfvs thats "ACPI Has Processor Frequency and Voltage
Scalling" a global flag set by acicpu attach to indicate that ACPI is
handling fvs through the _PSS objects. This will fix disappearing
EST/powernow when booting kernels with acpi enabled, as acpicpu isnt
compiled into GENERIC yet. Also in cases where acpicpu is built in and
legacy EST/powernow work but acpi is missing the _PSS object, the legacy
routines will be used.

tested by dim@, and Nick Nauwelaerts <nick AT nauwelaerts.net>
ok marco@, dim@


# 1.76 22-Jan-2007 mk

Actually look for docking stations by looking for _DCK nodes.

ok marco


# 1.75 02-Jan-2007 marco

On some systems EC must be attached as the first device. Move EC from
generic _HID devices to its own routine to force first attach. Bug found
and debugged by joris@

Code mostly written by joris@


# 1.74 26-Dec-2006 marco

Add polling to devices that require it such as acpibat and acpitz.
Use the same tsleep/wakeup mechanism as the interrupt code to keep it non-
concurrent.


# 1.73 21-Dec-2006 deraadt

even more knf love


# 1.72 21-Dec-2006 marco

Remove some leftover acpiec gpe/interrupt code.


# 1.71 20-Dec-2006 deraadt

oops, name already used


# 1.70 20-Dec-2006 deraadt

rename __acpi_enable_gpe(); ok marco


# 1.69 20-Dec-2006 deraadt

acpi_unmap_pmregs() is not used, ok marco


# 1.68 19-Dec-2006 deraadt

simplify some low-level methods substantially (shrinking code); ok marco jordan


# 1.67 06-Dec-2006 mk

malloc(..., M_NOWAIT) with no NULL check is bad. If allocation fails,
complain, clean up, and bail out of attach.

input and ok gwk


# 1.66 28-Nov-2006 jordan

Fixed clearing arg for acpi_init_pic
ok kettenis@


# 1.65 27-Nov-2006 kettenis

Don't spam dmesg.


# 1.64 27-Nov-2006 jordan

Fixes for acpiprt routing
Post-parse fixup for namerefs
ok kettenis@


# 1.63 25-Nov-2006 marco

Don't handle acpiev events inside the gpe handler since this causes deadlocks.

ok jordan@


# 1.62 15-Nov-2006 kettenis

Hook up acpi apic interrupt routing support to generic acpi code.

ok jordan@


# 1.61 02-Nov-2006 marco

Kill acpiec attach function and add its functionality to the generic _HID
device attach

prodded and ok jordan


# 1.60 02-Nov-2006 marco

Fix comments to reflect reality.


# 1.59 19-Oct-2006 marco

Replace ACPI ioctls with APM ioctls. This makes ACPI talk the same protocol
as APM therefore userspace can now retrieve power information in the same
fashion as all other apm arches (zaurus, macppc, i386 etc).

To use force the use of acpi device like so: apmd -f /dev/acpi

ok jordan@


# 1.58 19-Oct-2006 jordan

Fixed GPE debug levels
Fixed ref counter on temporary bufferfields
DSDT parsing fully working on IBM Thinkpad
ok marco@


# 1.57 15-Oct-2006 jordan

Removed old GPE handler code remnants
ok marco@


# 1.56 12-Oct-2006 jordan

Massive ACPI changes
Fixed ACPI leaks
new ACPI API
ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.55 30-Jun-2006 gwk

KNF, No functional change,
"go at it" marco@


# 1.54 30-Jun-2006 jordan

Support for calling Embedded Controller via new GPE interface
ok marco@


# 1.53 30-Jun-2006 jordan

Added new code for GPE handling
Also works with ACPIEC; need to fixup acpiec.c to remove original interrupt handler
ok marco@


# 1.52 30-Jun-2006 jordan

Added offset argument for acpi read/write pmreg routines (new GPE code)
ok marco@


# 1.51 31-May-2006 todd

do not call acpi_powerdown() unless acpi has successfully initialized
ok gwk@ canacar@


# 1.50 29-May-2006 canacar

Add embedded controller support.
ok marco@ deraadt@ testing & ok gwk@


# 1.49 19-May-2006 canacar

Minimal thermal zone driver. Monitors thermal zone temperature,
shuts down the system if the 'critical temperature' is reached.
ok marco@


# 1.48 11-Apr-2006 marco

KNF.


# 1.47 11-Apr-2006 gwk

Add acpi_resume(struct acpi_softc *sc), which calls some AML methods,
detected at attach time, currently unused.

ok marco@


# 1.46 11-Apr-2006 gwk

Discover some ACPI sleep related methods, and call them at the
appropriate time in acpi_enter_sleep_state().

ok marco@


Revision tags: OPENBSD_3_9_BASE
# 1.45 26-Feb-2006 marco

Add acpicpu as a device.

ok jordan@


# 1.44 23-Feb-2006 marco

remove dead function.

ok jordan@


# 1.43 22-Feb-2006 marco

Print out all tables it found during autoconf. We'll need this data in
the future.


# 1.42 22-Feb-2006 marco

Make dmesg less verbose.

ok jordan.


# 1.41 22-Feb-2006 jordan

Added parsing of SSDT tables
ok marco@


# 1.40 22-Feb-2006 jordan

Added new aml_register to notify by device id
Moved powerdown code to button device
ok marco@


# 1.39 21-Feb-2006 marco

Readjust debug levels that crept in too low over the last few days.
KNF


# 1.38 21-Feb-2006 marco

Rig the power button so that the machine shuts down correctly.


# 1.37 20-Feb-2006 marco

Add acpibtn device. Remains disabled for now.


# 1.36 19-Feb-2006 jordan

Added code for aml_notify GPE notification callback
ok marco@


# 1.35 19-Feb-2006 grange

Fix acpi_enter_sleep_state() by replacing current ugly hack
with mostly correct sleeping state transition code.

ok brad@ marco@


# 1.34 19-Feb-2006 marco

jordan:
Enable interrupts.
Added peripheral code to make this work.

marco:
Replace softintr and other types of interrupts with a handler with context.
Fixed interrupt magic for GPEs.
Some KNF

This is work done by jordan and I but has to be a single commit.


# 1.33 17-Feb-2006 marco

Remove earlier introduced delays. We really only need them during autconf.

ok jordan@


# 1.32 17-Feb-2006 marco

Add a generic delay function as per the ACPI spec.

from jordan@


# 1.31 17-Feb-2006 marco

Use tsleep instead of delay when !cold.


# 1.30 17-Feb-2006 marco

Argh, we need delays between reads/writes. The backend reads (i2c most likely)
are so slow that values come back as 0 sometimes. We have to do this
despite the spec saying we shouldnt have to. Guess having a faster
parser than others can hurt sometimes.

Idea from jordan.


# 1.29 17-Feb-2006 jordan

Fixed code for handling _INI
Rearranged debug statements
ok marco@


# 1.28 16-Feb-2006 jordan

Added stub for calling _INI
Fixed aml_dowhile for NULL return
ok marco@


# 1.27 16-Feb-2006 jordan

Fixed parser/evaluator to retrieve battery info+state
Cleanup of parser
Dell laptops return battery status
ok marco@


# 1.26 06-Feb-2006 brad

fix a typo so this will compile on amd64.


# 1.25 04-Feb-2006 marco

When ACPI_ENABLED wasn't set but the devices were it had an unknown variable.

Pointed out by ian@.


# 1.24 03-Feb-2006 jordan

Rewrote aml parser yet again...
Fixed bus_space_map for i386/amd64
Added _BST and _BIF to acpibat to get status/info
ok marco@


# 1.23 20-Jan-2006 jordan

More evaluator changes
Removed old evaluator code
Fixed _HID value
ok marco@


# 1.22 19-Jan-2006 jordan

Added support for read/write of PCI Config space
Help jason@
ok marco@


# 1.21 18-Jan-2006 jordan

Added new evaluation routines
Added helper methods for buffer/field evaluation
Fixed broken indentation on dsdt.c

ok marco@


# 1.20 17-Jan-2006 jordan

Major overhaul of the aml parser. Evaluater temporarily disabled.

ok marco@


# 1.19 06-Jan-2006 grange

Check for ACPI control availability only if ACPI_ENABLE defined.


# 1.18 06-Jan-2006 grange

Informative error messages on attach, prettier dmesg.


# 1.17 06-Jan-2006 grange

printf -> dnprintf conversion for debug messages.


# 1.16 05-Jan-2006 grange

Find SLP_TYPx values and store them for future use in
acpi_enter_sleep_state().


# 1.15 05-Jan-2006 grange

Random KNF and cleanup


# 1.14 05-Jan-2006 grange

Provide symbolic names for the acpi sleeping states and use it.


# 1.13 28-Dec-2005 marco

Moved DEVNAME to a acpidev.h

ok jordan@


# 1.12 18-Dec-2005 sturm

add back comment about wrong code

ok marco


# 1.11 16-Dec-2005 marco

Remove test for kbd_reset. Prompted deraadt@, ok jordan@


# 1.10 16-Dec-2005 jordan

Added hooks for powerdown/power button press


# 1.9 16-Dec-2005 jordan

Cleaned up common power management register i/o
Added enter sleep state ioctl
Hooked power button interrupt
ok marco@


# 1.8 16-Dec-2005 marco

Remove autoconf chatter


# 1.7 16-Dec-2005 jordan

Moved DSDT AML parsing into parent ACPI driver
ok marco@


# 1.6 14-Dec-2005 marco

Add AC device glue.


# 1.5 14-Dec-2005 marco

Adjust debug printing levels to something more sane.


# 1.4 13-Dec-2005 marco

Add glue to attach devices to apci.


# 1.3 07-Dec-2005 marco

KNF


Revision tags: OPENBSD_3_8_BASE
# 1.2 10-Jul-2005 grange

Spacing cleanup.


# 1.1 02-Jun-2005 tholo

Start on a basic ACPI framework -- does not do much more than read out the
ACPI tables into kernel memory and attach ACPI and HPET timers currently.

In order to test this code, enabling the devices in GENERIC as well as
the ACPI_ENABLE option is needed. This code does not do any thermal
control yet, so this should be done with care depending on the platform.

In the tree so more people can contribute to making this more fully
featured.

Ok niklas@ grange@ tedu@


# 1.415 03-Sep-2022 kettenis

Make sure we don't suspend if we have no way to wake up the machine.
The idea is that device drivers that can wake up the machine register
themselves by calling device_register_wakeup(). To prevent regressions
on amd64, we let acpi(4) register itself as a wakeup device if the AML
for the machine defines wakeup devices. This may be refined in the
future.

This diff will prevent people from suspending their arm64 SBCs without
having a way to wake them up. For now the only driver that registers
itself is axppmic(4), which means that at this moment only SBCs with
an allwinner A64 or H5 SoC will actually support suspend/resume.

ok mlarkin@, deraadt@


# 1.414 10-Aug-2022 patrick

Provide the AML root in ACPI's softc so that drivers that need access
don't have to declare it using extern.

Suggested by and with feedback from kettenis@
Tested by deraadt@ on arm64, amd64 and i386
Tested by bluhm@ on amd64 and i386


Revision tags: OPENBSD_7_1_BASE
# 1.413 17-Feb-2022 jsg

reduce includes


# 1.412 10-Feb-2022 visa

Embed klist head in acpi_softc to avoid explicit malloc.

OK kettenis@


# 1.411 09-Feb-2022 deraadt

Move x86-only stubs used by kern/subr_suspend.c into dev/acpi/acpi_x86.c
for sharing between i386 and amd64.
ok mlarkin kettenis


# 1.410 08-Feb-2022 deraadt

The suspend/resume code is a sticky mess of MI, MD, and ACPI sequencing.
This splits out the MI sequencing, backing it with per-architecture helper
functions. Further steps will be neccesary because ACPI and MD are too
tightly coupled, but soon we'll be able to use this code for more architectures
(which depends on figuring out the lowest-level cpu sleeping method)
ok kettenis


# 1.409 04-Feb-2022 robert

set the APM_BATT_CHARGING state if the battery is being charged and try
to calculate the remaining time to be fully charged

ok jca@


# 1.408 02-Feb-2022 deraadt

acpi_addtask() calls malloc() w/ M_NOWAIT (because some calls come from
interrupt context), this however means occasional resource shortage will
result in callbacks registration failing, and unknown consequences for
the task-submitting caller.
Changing this to use pools with a low water mark, decreases the odds
of that problem occuring.
ok kettenis


# 1.407 01-Feb-2022 deraadt

GPE_DIRECT is calling the AML parser from interrupt context, which is
not permitted. Luckily nothing is using GPE_DIRECT anymore, so this code
can be deleted.
ok kettenis


# 1.406 26-Jan-2022 kettenis

An ACPI device needs to be both present and enabled for it to function.
So only attempt to attach hardware that has both bits enabled. This fixes
an issue where com(4) would attach for a disabled serial port leading to
misdetection of the hardware variant and a subsequent hang when /etc/rc
runs ttyflags -a.

ok anton@, deraadt@


# 1.405 12-Jan-2022 patrick

Make acpi_getpropint() return uint64_t, as ACPI integers are in fact that
wide and some _DSD properties depend on it.

ok kettenis@


# 1.404 04-Jan-2022 patrick

acpi_getprop() needs to actually make sure that we're looking at the
correct property. While there adjust acpi_getpropint() as well to
increase similarity with acpi_getprop().

ok kettenis@


# 1.403 16-Dec-2021 anton

Attach com over acpi on amd64. Some hardware uses a different interrupt
assignment compared to the the legacy one supported by com over isa.
This causes the console to halt once userland takes over as no
interrupts are received. The actual address and irq can be read from
ACPI, kettenis@ already added support for arm64 which paved the way for
amd64.

Some consoles that previously attached over isa are now expected to
attach over acpi.

Thanks to patrick@ for testing on arm64.

ok kettenis@


# 1.402 07-Dec-2021 kettenis

Fix parsing of SR_IRQ resource descriptors.

ok patrick@, anton@


# 1.401 02-Nov-2021 deraadt

knf


# 1.400 30-Oct-2021 deraadt

Change hw.perfpolicy=auto by default, at startup. If the system has AC
power connected (default is yes when no driver differentiates) then default
to 100% performance. On battery, use the existing auto algorithm (which is
admittedly somewhat unrefined).
This change overrides the system/BIOS speed and puts OpenBSD in control.
As this happens very early during boot, besides speedups in all usage usage
patterns, some surprises: unhibernate and sysupgrade times are cut in half.
note: on a few architectures, the setperf fn pointer is changed late, and
thus the auto algorithm stops timeing out. kettenis and i will look for
a solution.
in snaps for more than a week.
ok kettenis


Revision tags: OPENBSD_7_0_BASE
# 1.399 20-Jul-2021 mlarkin

Fix trailing whitespace in a few places.

No code change.


# 1.398 09-May-2021 kettenis

Some x86 machines advertise the "hardware reduced" ACPI feature, advertise
S4 and S5 support, but fail to populate the SLEEP_CONTROL_REG and
SLEEP_STATUS_REG descriptions in the FADT. An example of such a machine
is the ASUS Zenbook 14 UM433DA. Any attempt to powerdown the machine
will result in a kernel crash. It turns out that using the legacy ACPI PM
registers works fine on this machien. So fall back on those registers
if SLEEP_CONTROL_REG and/or SLEEP_STATUS_REG aren't provided.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_9_BASE
# 1.397 15-Mar-2021 patrick

Add acpi_iommu_device_map(), which replaces the DMA tag with one that
is blessed with IOMMU magic, if available. This is mainly for arm64,
since on amd64 and i386 the IOMMU only captures PCIe devices, as far
as I know, which uses the pci_probe_device_hook(). This though is for
non-PCI devices attached through ACPI.

ok kettenis@


# 1.396 07-Mar-2021 patrick

Pass standard DMA tag to acpi(4) table drivers.

ok kettenis@


# 1.395 27-Dec-2020 deraadt

acpi_map_address() cannot be wrapped by SMALL_KERNEL anymore, as
it is used by acpihpet.c


# 1.394 25-Dec-2020 visa

Refactor klist insertion and removal

Rename klist_{insert,remove}() to klist_{insert,remove}_locked().
These functions assume that the caller has locked the klist. The current
state of locking remains intact because the kernel lock is still used
with all klists.

Add new functions klist_insert() and klist_remove() that lock the klist
internally. This allows some code simplification.

OK mpi@


# 1.393 17-Dec-2020 kettenis

Fix some issues with referencing named ACPI nodes from Packages.
These references need to be resolved at runtime rather than when
they're parsed such that they pick up the right values for those nodes
which can be changed when for example _INI methods run.

The current approach is to replace these reference with a string that
names the node in question. The problem with that is that packages
can also contain normal strings. Which means that we need special
code that depends on the context where the package is used.

This diff takes a different approach by retaining a reference when
parsing. Code that uses a package will need to resolve this reference
but there is no ambiguiety anymore.

ok patrick@


# 1.392 05-Dec-2020 kettenis

Add support for the _EVT method to the GPIO support code.

ok patrick@


Revision tags: OPENBSD_6_8_BASE
# 1.391 27-Aug-2020 jmatthew

Reorder the acpi attach process so we look at Processor() CPU nodes
after Device() ones, since we should prefer the newer node type.
If we see any Device() nodes, don't attach acpicpu(4) to any Processor()
nodes if they're also present. This also makes acpitz(4) and
acpipwrres(4) devices attach slightly later.

ok kettenis@, also tested by jmc@


# 1.390 16-Aug-2020 gnezdo

Remove sc_maxgpe from acpi_softc which was never read from

Remove a dead conditional from acpi_init_gpes while there.

ok jcs@


# 1.389 21-Jul-2020 deraadt

acpi can use IPL_BIO (a low interrupt) since it only enqueues operations for
later processing. The use of a high interrupt will predate suspend/resume
efforts, we had to redesign acpi to be non-reentrant obviously
discussed with kettenis, in snaps for more than a week


# 1.388 24-Jun-2020 cheloha

kernel: use gettime(9)/getuptime(9) in lieu of time_second(9)/time_uptime(9)

time_second(9) and time_uptime(9) are widely used in the kernel to
quickly get the system UTC or system uptime as a time_t. However,
time_t is 64-bit everywhere, so it is not generally safe to use them
on 32-bit platforms: you have a split-read problem if your hardware
cannot perform atomic 64-bit reads.

This patch replaces time_second(9) with gettime(9), a safer successor
interface, throughout the kernel. Similarly, time_uptime(9) is replaced
with getuptime(9).

There is a performance cost on 32-bit platforms in exchange for
eliminating the split-read problem: instead of two register reads you
now have a lockless read loop to pull the values from the timehands.
This is really not *too* bad in the grand scheme of things, but
compared to what we were doing before it is several times slower.

There is no performance cost on 64-bit (__LP64__) platforms.

With input from visa@, dlg@, and tedu@.

Several bugs squashed by visa@.

ok kettenis@


# 1.387 10-Jun-2020 jca

Give userland a consistent battery and A/C state after resume

Previously we refreshed the state of acpiac(4) and acpibat(4)/acpisbs(4)
by queueing fake ACPI events to be processed by the acpi thread.
acpibat(4) using ACPIDEV_POLL meant that its status could be refreshed
first, resulting in an APM_POWER_CHANGE event sent to userland before
the state of acpiac(4) was up to date. Because of this, apmd(8) could
see A/C unplugged when it was in fact plugged, possibly leading to
a wrong decision.

Instead of using fake ACPI events, refresh the status of acpiac(4) and
acpibat(4)/acpisbs(4) right away at DVACT_WAKEUP time. To reuse the
existing refresh code of those drivers, make their ACPI *_notify()
handlers responsible for sending APM_POWER_CHANGE events instead. Also
queue an APM_POWER_CHANGE event right after resume to retain existing
behavior.

This fixes the consistency of data shown to userland, and makes those
drivers less special.

Initially discussed last year (!) with mpi@ at Miod's place.
Feedback and ok jcs@, deraadt@ agrees with the use of DVACT_WAKEUP here.


# 1.386 29-May-2020 deraadt

dev/rndvar.h no longer has statistical interfaces (removed during various
conversion steps). it only contains kernel prototypes for 4 interfaces,
all of which legitimately belong in sys/systm.h, which are already included
by all enqueue_randomness() users.


# 1.385 14-May-2020 kettenis

Bring back the acpi(4) changes. This time around only use the ACPI _CRS
information on ACPI 5.0 and later.

ok krw@, patrick@


# 1.384 11-May-2020 jca

More accurate remaining power reporting with disparate batteries

Overall remaining power was computed as the average of the remaining
power of each battery, in percents. This doesn't work with batteries of
different capacity, so use the total capacity and remaining power
instead.

Reported by lidstah, successful tests by lidstah and benno@, ok kettenis@


# 1.383 08-May-2020 kettenis

Move parsing of _CRS into acpi(4) and pass the parsed address and interrupt
information in struct acpi_attach_args.

ok mpi@


Revision tags: OPENBSD_6_7_BASE
# 1.382 14-Apr-2020 kettenis

Implement acpi_getprop(), the ACPI equivalent of OF_getprop().

ok patrick@


# 1.381 12-Apr-2020 kettenis

Add support for the _CCA method. This method indicates whether DMA is
cache-coherent or not. To implement this, acpi(4) gets two bus_dma tags
and passes the appropriate one when attaching devices based on _CCA.
On i386/amd64, where for all practical purpose DMA is always cache-coherent,
the two tags are the same. But on arm64 they are distinct.

ok patrick@


# 1.380 07-Apr-2020 visa

Abstract the head of knote lists. This allows extending the lists,
for example, with locking assertions.

OK mpi@, anton@


# 1.379 03-Apr-2020 mpi

Fix incoherencies of errors returned by various *kqfilter routines.

EOPNOTSUPP is returned when there's no "filterops" for a given fd.
EINVAL when the requested filter isn't supported by the "filterops".
ENXIO means the underlying device is no longer valid/present.

ok visa@


# 1.378 20-Feb-2020 visa

Replace field f_isfd with field f_flags in struct filterops to allow
adding more filter properties without cluttering the struct.

OK mpi@, anton@


# 1.377 31-Dec-2019 visa

Use C99 designated initializers with struct filterops. In addition,
make the structs const so that the data are put in .rodata.

OK mpi@, deraadt@, anton@, bluhm@


# 1.376 31-Dec-2019 mpi

Convert infinite sleeps to {m,t}sleep_nsec(9).

ok kn@


# 1.375 31-Dec-2019 jsg

include sys/sensors.h for sensor use
don't depend on indirect inclusion from acpidev.h


Revision tags: OPENBSD_6_6_BASE
# 1.374 07-Sep-2019 kettenis

Revert acpipci(4) changes. Too many systems are broken. We'll try again
after 6.6 as been released. The acpireg.h change stays behind.


# 1.373 28-Aug-2019 kettenis

Use ACPI information to attach PCI busses like we do on arm64. There are a
few additional quirks though, and attaching the PCI busses is delayed to
replicate the existing code more closely. That may be changed in the
future. Also tweak how we handle MSI support and respect to ACPI flag
that says we shouldn't attempt to use MSIs.

Some fallout is expected.

ok patrick@


# 1.372 27-Aug-2019 deraadt

On amd64 the bootup and resume paths for SP/BP/SP were not performing
msr/pat, clock-startup, firmware upgrade, fpu initialization, and cpu
soft-state initialization in the same order. That kind of chaos is
crazy and fragile. Improve the acpi resume call sequence and unify order
of the operations.


# 1.371 02-Jul-2019 jcs

acpi_set_gpehandler: turn edge arg into flags, add support for
passing GPE_DIRECT which directly executes the supplied callback and
clears a level interrupt rather than going through acpi_taskq which
can be slow when other driver callbacks are in the queue.

This should not be used for GPE handlers that need to do anything
ACPI-related.

ok kettenis


# 1.370 10-Jun-2019 kettenis

Print proper ACPI version number.

ok mlarkin@


# 1.369 08-Jun-2019 kettenis

When establishing the mapping between ACPI device nodes and PCI devices,
skip PCI host bridges and devices that aren't there.

ok patrick@


# 1.368 07-Jun-2019 kettenis

Introduce acpi_getsta() and use it to eliminate some duplicated code.

ok patrick@


# 1.367 12-May-2019 kettenis

Make sure the extended PM register descriptions actually exist before
using them. Fixes machines with ancient ACPI 1.0 (such as recent
hypervisors).

ok deraadt@


# 1.366 12-May-2019 kettenis

Add support for mapping ACPI PM registers using the "extended" gasio
representation from the FADT. Mostly fixes the Lanner NCA-1510.

ok lteo@, mlarkin@, deraadt@


# 1.365 11-May-2019 lteo

Fix incorrect error message.

ok kettenis@


# 1.364 08-May-2019 tedu

free size. the allocation is the header plus the length.


Revision tags: OPENBSD_6_5_BASE
# 1.363 04-Apr-2019 kettenis

Avoid potential null-pointer dereference. Found by Kent R. Spillner.

ok deraadt@, jsg@


# 1.362 20-Jan-2019 jsg

fix SMALL_KERNEL build after changes in rev 1.361
reported by naddy@ ok deraadt@


# 1.361 19-Jan-2019 tedu

Add a pwraction sysctl that controls what the power button does on acpi.
By default, nothing changes -- shutdown is initiated. But allows turning
power button into a sleep button if desired.
(grudging) ok from a few parties


# 1.360 26-Oct-2018 kettenis

Add acpipci(4) on amd64. For now this only calls the PCI-specific _OSC
method to let the ACPI implementation know what features we support.


Revision tags: OPENBSD_6_4_BASE
# 1.359 30-Aug-2018 kettenis

Move softbutton check before the check that skips attaching certain acpi
devices. Fixes regression caused by matching on _CID in addition to
matching on _HID.

ok matthieu@, mlarkin@


# 1.358 25-Aug-2018 kettenis

Match ACPI devices based on _CID if no match for _HID is found.

ok mpi@


# 1.357 19-Aug-2018 kettenis

Add support for multiple PCI segments. Only really implemented for arm64
for now as amd64/i386 firmware still caters for legacy OSes that only
support a single PCI segment.

ok patrick@


# 1.356 03-Aug-2018 kettenis

Let ahci(4) match on _CLS instead of _HID when attaching at acpi(4). Avoids
having to add many more _HID entries to the match table.

ok deraadt@, mlarkin@


# 1.355 10-Jul-2018 kettenis

Make legacy interrupts work in acpipci(4).

ok patrick@


# 1.354 05-Jul-2018 kettenis

Add acpipci(4), a driver that supports generic ECAM-compatible PCI host
bridges based on information provided by ACPI.

ok mlarkin@


# 1.353 04-Jul-2018 kettenis

Properly pass around the PCI "chipset tag" in acpi(4) and refactor
acpimcfg(4) to call an MD initialization functions that sets up a tag for
PCI ECAM.

ok guenther@, mlarkin@, krw@


# 1.352 01-Jul-2018 kettenis

Move acpi(4) attach glue into acpi_machdep.c.

ok guenther@, deraadt@


# 1.351 01-Jul-2018 kettenis

Add support for _DSD Device Properties.

ok mlarkin@


# 1.350 30-Jun-2018 kettenis

Add intr_enable() function, intended for MI use to amd64 and i386 and use
this in the acpi(4) suspend/resume code paths.

ok deraadt@


# 1.349 29-Jun-2018 kettenis

Use <stdint.h> types.

ok mpi@, jung@, krw@, deraadt@


# 1.348 29-Jun-2018 mlarkin

ACPI: Allow (with warning) GPE handler reassignment, instead of returning
a failure code.

ok kettenis


# 1.347 26-Jun-2018 mlarkin

knf


# 1.346 26-Jun-2018 kettenis

Let acpi(4) pass down the bus dma tag.

ok deraadt@


# 1.345 25-Jun-2018 kettenis

Reorganize acpi(4) code a bit in preparation for upcoming arm64 support.

ok deraadt@


# 1.344 20-May-2018 kettenis

Wake up the acpi thread after scheduling a gpio event.

ok mpi@


# 1.343 17-May-2018 kettenis

Sprinkle some #ifndef SMALL_KERNEL such that RAMDISK kernels cuild again.

pointed out by stsp@


# 1.342 17-May-2018 kettenis

Implement GenericSerialBus OpRegion support.

ok mlarkin@


# 1.341 27-Mar-2018 kettenis

Add acpicmos(4), a driver that implements SystemCMOS OperationRegion
access support. This fixes machines where the AML doesn't check whether
support for this OperationRegion type has been registered by the OS.

ok mlarkin@


Revision tags: OPENBSD_6_3_BASE
# 1.340 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.339 10-Feb-2018 deraadt

Syncronize filesystems to disk when suspending. Each mountpoint's vnodes
are pushed to disk. Dangling vnodes (unlinked files still in use) and
vnodes undergoing change by long-running syscalls are identified -- and
such filesystems are marked dirty on-disk while we are suspended (in case
power is lost, a fsck will be required). Filesystems without dangling or
busy vnodes are marked clean, resulting in faster boots following
"battery died" circumstances.
Tested by numerous developers, thanks for the feedback.


# 1.338 08-Feb-2018 deraadt

After flushing disks, try again to release all memory. We don't need to
save dirty memory to the hibernate space.


# 1.337 08-Feb-2018 deraadt

Toss all releaseable memory, because fragmentation can get in the way
of allocating the hibernate playpen.


# 1.336 08-Feb-2018 deraadt

sr_quiesce() is a new approach for ensuring that softraid drains
output to the disks.

This is part of a larger suspend/resume filesystem-safety diff, which
has been worked on for a couple of months already. Tests by job, krw,
beck, benno, and others. Sometimes even by snapshot users...


# 1.335 29-Nov-2017 kettenis

Add support for GPIO-signaled events.

ok mlarkin@


# 1.334 16-Nov-2017 jcs

Add PCI attachment for dwiic(4) needed by Intel 100 Series machines

ok kettenis


# 1.333 14-Nov-2017 kettenis

Add a few more PNP IDs to the skip lists.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.332 17-Aug-2017 stsp

Make dwiic(4) attach its ACPI parent dependencies.
This change makes the Asus E200HA keyboard work.

Original analysis and patch by Cesare Gargano
ok kettenis@


# 1.331 09-Aug-2017 dcoppa

Add "PNP0303" (8042 PS/2 Controller) to acpi_isa_hids[]

ok kettenis@


# 1.330 22-Jul-2017 jcs

acpibat: look for _BIX first before falling back on _BIF

ACPI 4.0 deprecated _BIF for battery status so some newer machines
are exporting _BIX which extends _BIF's package with a few extra
fields. When using _BIX, export the new discharge cycle count as a
sensor.

feedback from kettenis and anton
ok deraadt


# 1.329 20-Jul-2017 kettenis

Start a list of isa(4) device names such that we can avoid printing them
as "not configured".

ok mikeb@, millert@, deraadt@


# 1.328 08-Apr-2017 deraadt

Sensors are run as callbacks inside tasks. During suspend a sensor
could be running inside a driver that will be force-detached, or due
to tsleep end up disrupting the softstate/hardstate contract. At
suspend time, quisce all these callbacks by waiting for completion.
This issue has never been observed for real, but may be implicated in
suspend/resume failures.
ok kettenis guenther mlarkin


# 1.327 08-Apr-2017 deraadt

Two prototypes for acpi_maptable; merge to one place


Revision tags: OPENBSD_6_1_BASE
# 1.326 27-Mar-2017 deraadt

Now that hibernate_alloc() only has clean success/failure, don't
need to call hibernate_free() to clean up a partial mess.
ok mlarkin kettenis


# 1.325 13-Mar-2017 deraadt

allow ramdisks to build; ok jcs


# 1.324 12-Mar-2017 jcs

try to attach acpisbs first and if successful, don't attach acpibat


# 1.323 02-Mar-2017 natano

Add a new sysctl machdep.lidaction. The sysctl works as follows:

machdep.lidaction=0 # do nothing
machdep.lidaction=1 # suspend
machdep.lidaction=2 # hibernate

lidsuspend is just an alias for lidaction, so if you change one, the
other one will have the same value. The plan is to remove
machdep.lidsuspend eventually when people have upgraded their
/ets/sysctl.conf.

discussed with deraadt, who came up with the new MIB name
no objections mlarkin
ok stsp halex jcs


# 1.322 28-Feb-2017 natano

Untangle abstract sleep modes from ACPI sleep states. This paves the way
for further work in the area.

idea and ok deraadt
ok mlarkin


# 1.321 22-Feb-2017 jcs

add acpisbs, an acpi smart battery subsystem driver reading data
over smbus

currently disabled because it conflicts with acpibat

ok deraadt, kettenis


# 1.320 14-Jan-2017 kettenis

Revert aml_rdpciaddr changes; breaks several machines that were working
before.


# 1.319 11-Jan-2017 jsg

Increase the size of the buffers used for _HID and _CID strings to be
able to fit "Hyper_V_Gen_Counter_V1".

ok mlarkin@ deraadt@


# 1.318 08-Jan-2017 kettenis

Make aml_rdpciaddr() work for for devices behind bridges. The code now
uses the mapping between AML nodes and PCI devices that we establish.
Because _INIT methods may end up calling aml_rdpciaddr(), make sure we
create that mapping early. Also handle devices that aren't actually present.
These devices are now included in the mapping and reads will return an
all-ones patterm whereas writes are a no-op..

ok mlarkin@


# 1.317 25-Oct-2016 pirofti

Add the resource index to aml_parse_resource() callback function.

This is needed by an upcoming acpiec commit that handles machines breaking
the current ACPI specifications.

Change suggested by and ok kettenis@, guenther@


# 1.316 18-Sep-2016 guenther

If the APIC table (MADT) isn't present then ci_acpi_proc_id won't be set
when acpicpu tries to attach, leading to a NULL dereference. The ACPI
processor object on the Dells where this was reported is bogus and useless
anyway so apply the same test when deciding whether to attach to processor
objects as acpicpu would when matching them to cpus.

Problem reported by Eivind Eide (xenofil (at) gmail.com) and
Johan Huldtgren (johan+openbsd-bugs (at) huldtgren.com).
Testing by Eivind Eide.
ok kettenis@ mlarkin@


# 1.315 03-Sep-2016 naddy

Write the system time back to the RTC every 30 minutes.
This fixes the problem that long-running machines which were not
shut down properly would reboot with a badly offset system time.

hints and ok kettenis@


# 1.314 31-Aug-2016 mlarkin

Don't put a device in the wake device list if _STA says it's not there.

Fixes at least one machine that woke up from sleep / poweroff immediately.

ok kettenis@, tested by kettenis and martijn@


# 1.313 28-Jul-2016 kettenis

Store the acpi processor ID/UID in struct cpu_info, and use it to attach
acpicpu(4) drivers to the right cpu(4).

ok mlarkin@, guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.312 10-Jun-2016 kettenis

The power management timer has been made optional in ACPI 5.0A, so don't
attempt to attach acpitimer(4) if the timer isn't present.

Pointed out by jcs@

ok jcs@


# 1.311 08-May-2016 kettenis

Make sure devices listed by a device's _DEP method are attached before
we attach the device itself. The _DEP methode, introduced in ACPI 5.0,
is a hint that the device in question depends on OpRegion support from
the devices listed by _DEP, so we have to order them the proper way.
To prevent us from attaching those devices again when we encounter them
later walking down the device tree, keep track of our attempts to
attach a device driver to them using aflag in the aml_node structure.

ok guenther@


# 1.310 25-Apr-2016 pirofti

We attach acpihpet(4) and acpiec(4) without using the HID, so do not
report it in the dmesg.

OK deraadt@, mlarkin@.


# 1.309 02-Apr-2016 kettenis

Reduce dmesg spam by knocking out some acpi hardware IDs that don't need a
driver.

ok mpi@


# 1.308 29-Mar-2016 kettenis

Check if a device is present (using _STA) in acpi_foundhid. This prevents
us from reporting devices as "not configured" that aren't actually present,
and allows us to remove duplicated code from several drivers.

ok deraadt@, ok jsg@


# 1.307 29-Mar-2016 kettenis

Implement a cleaner way to prevent acpivout(4) from attaching if we have
a platform-specific method of controlling the backlight.

ok millert@, mlarkin@, jung@


# 1.306 28-Mar-2016 kettenis

Adding each and every new acpi device driver to acpi_foundhid() is
gettinga bit of the burden. And it really isn't how our autoconf
framework is supposed to work for busses that can be enumerated. All
the drivers already check for a matching _HID in their attach
function. So we can just drop the checks in acpi_foundhid() and let
the drivers sort themselves out.

Print a "not configured" line for devices for which we don't attach adriver.
This may be a bit much as there are devices for which we will not have a
driver. This will be removed if it causes too much dmesg spam.

ok deraadt@, mlarkin@


Revision tags: OPENBSD_5_9_BASE
# 1.305 17-Jan-2016 jsg

Take acpi_foundhid() outside of #ifndef SMALL_KERNEL so the ramdisk
kernel can find and attach dwiic(4) and sdhc(4).


# 1.304 17-Jan-2016 jsg

remove an unused global variable


# 1.303 14-Jan-2016 kettenis

Attach dwiic(4) on the Intel Bay Trail i2c controllers as well.


# 1.302 12-Jan-2016 kettenis

Improve emulation of PM registers on Hardware-redcued ACPI by emulating
SPL_TYPx, SLP_EN and WAK_STS using the new SLEEP_STATUS_REG and
SLEEP_CONTROL_REG registers. Makes the transition into S5 (powerdown) work
on the ASUS X205TA.

ok deraadt@, mlarkin@


# 1.301 12-Jan-2016 jcs

Add dwiic, a driver for the Synopsys DesignWare i2c controller found
on the Samsung ATIV Book 9 laptop. This initial version only
supports ACPI config/attachment.

Add ihidev, a HID-over-i2c driver largely based on uhidev. dwiic
handles attaching ihidev devices found in ACPI.

Add ims, a HID-over-i2c mouse/trackpad driver to get basic cursor
and button functionality from HID-compliant i2c trackpads.

ok kettenis deraadt


# 1.300 11-Jan-2016 kettenis

Make sdhc(4) attach to hardware IDs "80860F14" and "PNP0FFF".


# 1.299 09-Jan-2016 kettenis

Initial support for Hardware-Reduced ACPI. This skips the legacy mode checks
by emulating the PM_CNT register such that SCI_EN is always enabled. It also
skip attaching acpitimer(4) since the relevant registers are no longer there.

ok deraadt@, mlarkin@


# 1.298 06-Jan-2016 kettenis

Follow the spec and check the SCI_EN bit to see if ACPI has already
been enabled and don't bother enabling it again if it is. This should fix
systems that don't implement legacy mode and therefore set ACPI_ENABLE and
ACPI_DISABLE to zero.

ok mlarkin@, yasuoka@


# 1.297 23-Nov-2015 reyk

Back out the CRITICAL change, turn it back from 10% to 25%. Some
people with old/broken batteries didn't like this change, and the
underlying heuristics are wrong.


# 1.296 22-Nov-2015 reyk

The "CRITICAL" APM battery level, that is exported/faked by the ACPI
stack, is currently set to 25%. With 25% battery left, a typical
modern laptop will have at least one hour runtime left. It is an
arbitrary value, and other APM power layers in the kernel set CRITICAL
to something like 5% or 10%, so set it to 10% for now.

OK mlarkin@


# 1.295 28-Sep-2015 deraadt

In low-level suspend routines, set cold=2. In tsleep(), use this to
spit out a ddb trace to console. This should allow us to find suspend
or resume routines which break the rules. It depends on the console
output function being non-sleeping.... but that's another codepath which
should try to be safe when cold is set.
ok kettenis


# 1.294 06-Sep-2015 deraadt

clarify malloc arguments (base + length...)


# 1.293 28-Aug-2015 deraadt

fairly simple sizes for free(); ok tedu


# 1.292 20-Aug-2015 kettenis

Respect the access size when reading or writing to pci config space.
This version also makes sure writes are properly aligned, preventing
the panics seen with the previous incarnation of this change.

Fixes battery status passthrough in vmware, brightness keys on my x220
and who knows what else.

ok mlarkin@, deraadt@


Revision tags: OPENBSD_5_8_BASE
# 1.291 04-Aug-2015 deraadt

If we are setting ACPI_PM1_BM_RLD do it only once, and remember to
repeat this action at resume time.
ok mlarkin guenther


# 1.290 29-Jul-2015 deraadt

Something subtle in previous revision is locking up suspend or booting
on some laptops. Mailing lists threads mention x220 and Asus machines.
Reverting for now.


# 1.289 21-Jul-2015 kettenis

iRespect the access size when reading or writing to pci config space.
Fixes battery status passthrough in vmware and who knows what else.

Tested by many.

ok mlarkin@, deraadt@


# 1.288 17-Jul-2015 mlarkin

fix comments


# 1.287 26-Mar-2015 jsg

Use the 0x10 extended capability to determine if the trackstick
buttons are wired to the trackpad and need to be re-routed to the
trackstick. Without this change the buttons on 2015 thinkpads
get picked up as extended buttons that show up as scroll up/down.

Remove the X1 Carbon 2015 (LEN0048) and X250 (LEN0046) from the
top button area/soft buttons quirks list. Also avoid using the
quirk list entirely if the capability bit is set.

Based on work from jcs@ with feedback from mpi@
ok mpi@ jcs@


# 1.286 20-Mar-2015 kettenis

The ACPI standard says that a PCI function number of FFFF can be used to refer
to all functions on a device. This makes no sense in the context of accessing
PCI config space. Nevertheless stsp@ has a machine with AML that tries to
do config space reads from a device where _ADR encodes function number FFFF.
This is almost certainly buggy AML, but since Linux (and presumably Windows)
boot just fine on this machine we need to work around this. Linux effectively
treats function FFFF as function 7, which isn't present on this machine.
Instead we simulate a read from a non-existent device by returning a bit
pattern of all ones. Writes to function FFFF will still panic.

ok mlarkin@


# 1.285 16-Mar-2015 deraadt

Reduce use of SMALL_KERNEL in acpi, bringing acpiec driver and more gpe
handling into RAMDISK. This is now possible because the install media
has ample room. The goal is to reduce special cases where we may be
skipping (unknown) important operations...
ok mlarkin kettenis


# 1.284 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.283 07-Feb-2015 deraadt

New framework that allows hibernate to pass in entropy from it's fresh
boot.
ok mlarkin


# 1.282 06-Feb-2015 mlarkin

Fix a hibernate crash on some machines due to unmapping a page that
may not have been mapped previously (in the failure to hibernate case).

Also ensure that the lowmem ptp is mapped in all cases (not just MP).

ok kettenis


# 1.281 17-Jan-2015 deraadt

resume_randomness() before spinning up other cpus...


# 1.280 15-Jan-2015 jsg

Define a new wscons mouse type for Synaptics clickpad devices that lack
physical buttons. This will be used if the acpi pnpid for the mouse
matches a list provided by Synaptics found in Linux.

Combined with the xenocara changes this will remove the need for an xorg
config file for the x240/t440/t540 generation of thinkpads.

ok matthieu@ shadchin@ kettenis@


# 1.279 11-Jan-2015 kettenis

Tolerate checksum errors in ACPI tables (but complain about them). Apparently
there are machines out there with broken BIOSen with such checksum errors in
thier RSDT tables. Windows runs fine on these machines, so it probably does
not check. Linux ignores checksum errors as well (and prints a warning).

ok deraadt@, krw@


# 1.278 18-Dec-2014 deraadt

suspend and resume the RNG (at what looks like the right spots)
ok reyk djm


# 1.277 09-Dec-2014 doug

Replace some malloc(n*size,...) calls with mallocarray().

ok tedu@ deraadt@


# 1.276 06-Dec-2014 mpi

If "machdep.lidsuspend" is not set, do not go back to sleep if the lid
is still closed when resuming. This allows docked laptop to be resumed
without opening them.

ok mlarkin@


# 1.275 23-Nov-2014 mlarkin

If a machine has no _LIDs, then none can be open. This diff fixes a problem
where machines without any _LIDs were going back to sleep after hibernate
due to the recent suspend-if-lid-closed-on-resume diff. Noticed on qemu,
which doesn't have any _LIDs.

discussed with deraadt


# 1.274 18-Nov-2014 krw

Nuke yet more obvious #include duplications.

ok miod@


# 1.273 08-Nov-2014 mlarkin

If resuming from sleep (zzz/ZZZ) and the lid is still closed, go back to
sleep. This prevents accidental lid flex or slight opening in a backpack
from waking the machine up and leaving it resumed (powered on).

ok deraadt@


# 1.272 26-Sep-2014 kettenis

Rework piglet and pig allocation. Currently the piglet gets allocated
deep down in the suspend path, where it is really hard to recover from
allocation failure. So allocate the piglet early on in the suspend path.
Also change the piglet and piglet allocation functions to use km_alloc(9)
instead of doing pmemrange magic. This removes a bunch of code which, in the
case of the piglet allocation, is broken since it results in a NULL pointer
dereference. Also switch the piglet allocation to not wait. If we can't
allocate 16MB of phys contig memory on a halfway modern machine we're almost
certainly under a lot of memory pressure and we're better off not trying to
hibernate anyway.

ok mlarkin@


# 1.271 25-Sep-2014 kettenis

Bring back rev 1.249. Now that mp_setperf() has been fixed, it should be
safe to restore perflevel when resuming. Should fix the issues people
see with the processor running at full speed heating up the machine while
the apm command reports that the CPUs are running at a low speed.

ok dcoppa@, deraadt@


# 1.270 23-Sep-2014 kettenis

Release the acpi lock around calls to wsdisplay_suspend() and
wsdisplay_resume(). The X server may adjust the display brightness just
before/after the VT switch when suspending/resuming. In fact, it may
already be blocked adjusting the display brightness for some reason
unrelated to the suspend request.

The VT switch request will eventually time out, but the delay is annoying
and the failed VT switch will lead to problems after resume.

Needed for a future update of the xf86-video-intel driver.

ok mlarkin@


# 1.269 19-Sep-2014 kettenis

Use config_suspend_all(9).


# 1.268 08-Sep-2014 kettenis

Clear the PME Status bit on PCI devices in response to Device Wake events.
Fixes the interrupt storm on the 2009 Mac mini with WOL enabled on its
nfe(4) interface.

ok mlarkin@


Revision tags: OPENBSD_5_6_BASE
# 1.267 20-Jul-2014 mlarkin

Support hibernating to softraid crypto volumes.

much help and ok from deraadt@


# 1.266 16-Jul-2014 mlarkin

Reenable hibernate RLE support and flush+zero all memory after unpack.


# 1.265 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.264 12-Jul-2014 kettenis

Add a function to drop all clean pages on the page daemon queues and call
it when we hibernate.

ok mlarkin@, miod@, deraadt@


# 1.263 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.262 11-Jul-2014 mlarkin

Flush the buffercache to 16MB on hibernate and restore its previous max
size (kern.bufcachepercent) on resume, for better hibernate performance.

ok beck@


# 1.261 10-Jul-2014 blambert

remove unneeded header

ok mlarkin@


# 1.260 01-Jul-2014 kettenis

Adding an additonal delay after each access to SystemMemory or SystemIO
during autoconf makes no sense.

ok deraadt@


# 1.259 23-Jun-2014 kettenis

SystemMemory is used to access memory mapped registers on some machines, so we
must use the bus_space(9) API to access it instead of memcpy(9). Also make
sure we properly align access to fields that need more than byte alignment.

ok guenther@


# 1.258 21-May-2014 mlarkin

Fix invalid / missing format specifiers in several dnprintf statements
(used only when ACPI_DEBUG is enabled)

From a diff on tech@ by Fabian Raetz, with a minor change by me, tested
on i386 and amd64 with ACPI_DEBUG enabled and disabled.


# 1.257 25-Apr-2014 mlarkin

Defer the free of the hibernate structures until later, when it is safe to
do so.

Freeing the hibernate structures too early leads to inadvertent calls into
splx when we aren't ready for them yet. Found when trapping splx calls in
the resume path.

discussed with deraadt@ at m2k14


# 1.256 13-Apr-2014 mlarkin

Fix a badly worded debug message that implied the entire machine was going
to sleep, instead of just the acpi thread going to sleep.


# 1.255 31-Mar-2014 mpi

Change the order of operations during a suspend/resume cycle and
call bufq_quiesce() after executing the DVACT_QUIESCE handlers.

This should be safe since no disk nor controller drivers have such
handler but it will allow us to detach sd(4) devices attached to a
USB bus.

Another benefit pointed out by kettenis@ is that drivers that need
to read a firmware from the disk should be able to do it at resume
time in a DVACT_WAKEUP handler.

ok kettenis@, deraadt@


# 1.254 13-Mar-2014 dlg

get rid of the assumption that the head of the alldevs list is the
"mainbus" device. this breaks when mpath is enabled because it
attaches before mainbus and therefore takes the head position.

have autoconf provide device_mainbus() which looks up mainbus_cd,
and use that instead.

discussed with deraadt who just wants mpath stuff to move forward
despite there being many ways to shine this particular turd.


Revision tags: OPENBSD_5_5_BASE
# 1.253 21-Feb-2014 deraadt

Do the lid control further on in the resume sequence, since this is
AML, and will do WAITOK operations. While cold, that is a bad idea.
We seem to be safe with respect to the spec, I do not see any event
debounce issues here.
ok kettenis, tested by many


# 1.252 20-Jan-2014 claudio

Instead of checking sc->sc_revision <= 1 check that the
fadt->hdr_revision <= 1. This is a better check since it checks
the version of the proper descriptor. This allows the alix APU
to reboot since those boards have currently a wrong sc->sc_revision.
Committing this now so that it can be tested easier.
With and OK jsg@ also fine by derradt@


# 1.251 24-Dec-2013 kettenis

If the FADT has its SMI_CMD set to zero, assume we're only ACPI-only hardware
and don't need to disable SMI ownership of the ACPI hardware registers.

ok mlarkin@


# 1.250 23-Dec-2013 kettenis

Revert previous commit. It makes resume hang on some MP systems running
GENERIC.MP.


# 1.249 16-Dec-2013 kettenis

Restore hw.setperf upon resume like we do for apm(4) on i386. Seems we need
to do this fairly late in the resume to avoid hangs.

ok deraadt@, mlarkin@


# 1.248 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


# 1.247 06-Nov-2013 mpi

Add support for Power Resources for Dx states and the necessary hook
for PCI devices. This hook should be called twice, before and after
changing the power state of a PCI device.

Before setting the device to the new state, the ACPI layer will notify
every power resources linked to the device for that state and make sure
they are turned "_ON". After changing the state of the device, it will
decrement the reference of every power resources linked to that device
for the old state and turn them "_OFF" if they are no longer referenced.

This fixes the no-USB after resume problem seen on various ThinkPad,
problem initialy diagnosed with Alexander Polakov.

ok kettenis@, deraadt@


Revision tags: OPENBSD_5_4_BASE
# 1.246 01-Jun-2013 mlarkin

Stop printing ACPI wakeup devices in dmesg after the 16th wakeup device.

Helps with VMware VMs that report hundreds of wakeup devices.

ok reyk@, deraadt@


# 1.245 31-May-2013 bluhm

Fix the build for a kernel without wd and pciide in its config.
OK deraadt@ tedu@


# 1.244 30-May-2013 deraadt

Enforce ca_activate tree-walks over the entire heirarchy for all events,
cleaning up some shutdown-hook related code on the way.
(A few drivers related to sparc are still skipped at kettenis' request)
ok kettenis mlarkin, tested by many others too


# 1.243 18-Apr-2013 deraadt

pin the acpi thread to the BSP, because AML and SMI cannot be trusted. I
was certain we were doing this already, but it turns out no...


Revision tags: OPENBSD_5_3_BASE
# 1.242 24-Dec-2012 guenther

In acpiioctl(), Return though the splx() path if suser() returns an error

ok deraadt@


# 1.241 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


# 1.240 04-Oct-2012 ehrhardt

Use information provided by ACPI to attach secondary PCI host bridges.

ok kettenis@


# 1.239 07-Sep-2012 kettenis

Add a new function called acpi_pci_min_powerstate() that returns the lowest
power state for a PCI device for the power state (currently S3 or S4) we're
transitioning into. This function is probably too simplistic; there is a lot
of (silly) complexity in the ACPI spec.

ok deraadt@


Revision tags: OPENBSD_5_2_BASE
# 1.238 13-Jul-2012 pirofti

Disable acpivideo(4) on ASUS laptops.

There are some models that panic whenever an event is received.
Some cope, some don't. In order to get this right acpiec(4) requires
major rework. Until then let machines boot.

Discussed with deraadt@.


# 1.237 11-Jul-2012 mlarkin

for now, restrict hibernate to root in the acpi ioctl kernel interface.
normal rules for controlling this interface through apmd still apply.

ok deraadt@


# 1.236 09-Jul-2012 deraadt

tiny little oops in untested hibernate block


# 1.235 09-Jul-2012 deraadt

clean up ioctl() case statements


# 1.234 09-Jul-2012 deraadt

acpi_disable_onegpe() is not used; ok pirofti


# 1.233 24-May-2012 kettenis

If _MIN > _MAX consider a WordBus resource to be invalid. Fixes a panic on
the IBM x3550-M4.

ok mikeb@


# 1.232 29-Mar-2012 mlarkin

Missing #ifdef in previous diff


# 1.231 29-Mar-2012 mlarkin

Prevent hibernation attempts on machines with unsupported disk controllers.
The same check is also performed later in the hibernate sequence, but by
then it's too late to cleanly unwind (presently).

ok deraadt


# 1.230 26-Mar-2012 deraadt

simplify conditionals in acpi_sleep_state()
ok mlarkin


# 1.229 26-Mar-2012 deraadt

hook in the hibernate request code; half of this diff is from mlarkin
ok mlarkin


Revision tags: OPENBSD_5_1_BASE
# 1.228 20-Sep-2011 deraadt

Very early in suspend while the clock is still ticking realtime,
save the clock back to the rtc. The zaurus already did this.
ok phessler miod kettenis


Revision tags: OPENBSD_5_0_BASE
# 1.227 02-Jul-2011 nicm

kqueue attach functions should return an errno or 0, not a plain 1. Fix
the obvious cases to return EINVAL and ENXIO.

ok tedu deraadt


# 1.226 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.225 16-Jun-2011 pirofti

Import acpitoshiba driver.

This is originally written by Hiroyuki Aizu, ported and integrated
in our tree by Javier Vazquez <javama4 AT gmail> with tweaks and
suggestions by me.

Import prodded by deraadt@ and the lack of testers on tech@.


# 1.224 27-Apr-2011 jcs

attach acpithinkpad to newer lenovo models like the x120e

ok deraadt@


# 1.223 22-Apr-2011 jordan

Fix uninitialzied variables and formatting strings (-Wxxx errors)


Revision tags: OPENBSD_4_9_BASE
# 1.222 02-Jan-2011 jordan

Re-add code for ACPI taskqueue, eliminates need for state vars in acpi softc
Serialize processing ACPI tasks for sleep/powerdown/GPE event
ok deraadt@


# 1.221 31-Oct-2010 guenther

Revert last commit: it breaks resume on Thinkpads
"then please back it out" deraadt@


# 1.220 26-Oct-2010 jordan

Add task queue for ACPI gpe and notify handlers
This fixes eject on Dell Latitude dock and an issue on the Dell Mini
battery update.
ok deraadt


# 1.219 07-Oct-2010 jordan

Create separate functions for enabling wake and runtime gpes.
Remove old suspend/resume gpewalk function
ok deraadt, mlarkin


# 1.218 05-Oct-2010 jordan

split acpi_enable_onegpe to separate enable/disable functions
ok deraadt


# 1.217 05-Oct-2010 jordan

Added acpi_disable_allgpes function to clear GPE status
ok deraadt


# 1.216 05-Oct-2010 deraadt

clean up acpi_softc use more


# 1.215 05-Oct-2010 deraadt

delete a bit of #if 0 code


# 1.214 05-Oct-2010 deraadt

hide use of our global acpi_softc pointer a bit better


# 1.213 29-Sep-2010 deraadt

grab the acpi thread lock very early in acpi thread startup; not that it
is likely that anyone will race us... but why be careless.
ok kettenis


# 1.212 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.211 29-Aug-2010 jasper

- add a check for FWRITE here too.

ok miod@


# 1.210 27-Aug-2010 kettenis

Make PCI Power Management optional, and only enable it when acpi(4) attaches.

ok deraadt@


Revision tags: OPENBSD_4_8_BASE
# 1.209 08-Aug-2010 kettenis

Close race between the acpi thread and wscons ioctls running in process
context. We might rip this out post-release and replace it with a solution
that serializes things in a more obvious way.

ok deraadt@, miod@ and "those crazy texans who ok anything"


# 1.208 08-Aug-2010 deraadt

Report the acpi sleep states when on a RAMDISK; ok krw


# 1.207 07-Aug-2010 kettenis

Enforce acpi register access rescrictions.

ok mlarkin@, deraadt@, tested by marco@


# 1.206 07-Aug-2010 deraadt

upon resume, notify all the acpiac and acpibat drivers to update
their status. some laptops do not do this automatically.
ok kettenis canacar


# 1.205 06-Aug-2010 marco

Always call _PSW on _LID devices that support it to enable lid open to
resume the machine. Conversely disable it when the machine wakes up.

Tested by several ok deraadt


# 1.204 06-Aug-2010 deraadt

As part of a revamp of the PM1/GPE code, I write this diff to look for
unmanaged EN & STS bits in the PM1 register at interrupt time and report
them. As a side effect this splits the STS acknowledgement into two writes
(for power, and sleep) instead of one. The printf that is added (to spot
unmanaged STS bits) has not yet been triggered as far as we know.

Before the "write to PM1 registers at the right offset" diff went in, this
was not neccessary. But newer thinkpads do not have a working soft power
button without this diff.

We have no idea why.
ok mlarkin kettenis


# 1.203 05-Aug-2010 deraadt

PM1 power button events were generating 'fake events' to acpibtn PWRB,
but if there is no such device these were being tossed. Instead, arrange
for all power button handlers to flag a softc variable which is processed
by the thread; so that one place will know to request a powerdown (psignal
to init)
ok marco mlarkin


# 1.202 05-Aug-2010 deraadt

rename a variable which is annoyingly poorly named


# 1.201 05-Aug-2010 mlarkin

Write to the correct offset for various ACPI pmreg registers. Writing to
the correct register does properly clear the wake status bits and fixed
event bits, which allows some machines to have more than 1 suspend cycle.

bug spotted by deraadt@

tested by deraadt@ and myself on a half-dozen machines and others on
hackers@

ok deraadt@, marco@


# 1.200 05-Aug-2010 deraadt

Fix the power button. The pm1 register is 16 bits in size.
My mistake, a while back.
tested by krw


# 1.199 04-Aug-2010 kettenis

Print supported sleep states.

ok marco@, phessler@, deraadt@


# 1.198 03-Aug-2010 mlarkin

Use the proper flag for re-enabling certain hardware events (power btn,
sleep btn). The (incorrect) flag prevoiously used coincidentally had the
same value, so this shouldn't cause any different behavior than before.

tested on a variety of machines (i386, amd64, sp, mp) - no behavioral
change seen

ok deraadt@


# 1.197 28-Jul-2010 marco

Make all gasio failures look and behave the same.


# 1.196 28-Jul-2010 mlarkin

Delay a little longer on suspend before giving up. Fixes machines that
sometimes don't suspend because the sleep transition takes too long
(symptoms like failing to sleep but the backlight stays on).

ok deraadt@


# 1.195 27-Jul-2010 deraadt

remove XXX; EC can now be attached early when needed; ok jordan


# 1.194 27-Jul-2010 deraadt

more brackets feels good


# 1.193 27-Jul-2010 deraadt

XXX annotate a block in acpi_enter_sleep_state() that is pretty worrying


# 1.192 27-Jul-2010 deraadt

horrific indentation that nearly made me puke


# 1.191 27-Jul-2010 mlarkin

Set the sleeping indicator light on machines that support it, in case SMI
doesn't do it for us. Shows the 'moon' LED on older Thinkpads when
sleeping.

Tested on T43p, W500, T510 by me and on X61s by deraadt@.

ok deraadt@


# 1.190 27-Jul-2010 jordan

Early initialization of acpiec if ECDT table exists
Fixes hang when booting thinkpads while docked
ok deraadt


# 1.189 26-Jul-2010 deraadt

remove merge error which was commited


# 1.188 26-Jul-2010 pirofti

Add support for Sony ACPI hotkeys via a new driver: acpisony(4).

Currently it only works for the suspend button and tries to do right for
the brightness events, but I haven't found a Sony laptop that like to do
right so far.

In the future I want to make the brightness keys work on all Sony's and
also add support for the zoom hotkeys and whatever other funky keys I can
find on those things.

Okay deraadt@.


# 1.187 22-Jul-2010 deraadt

pretty large cleanup of SMALL_KERNEL


# 1.186 21-Jul-2010 deraadt

in the acpi GPE handler, late ack level interrupts not edge interrupts.
Duh.


# 1.185 21-Jul-2010 deraadt

Merge the edge and level gpe handlers into one (now possible since the
gpe structure tells us whether the gpe is edge or level)


# 1.184 21-Jul-2010 deraadt

delete #if 0 code we are not going to use; ok marco


# 1.183 20-Jul-2010 deraadt

make the mask code for acpi_enable_onegpe() match other similar code


# 1.182 20-Jul-2010 deraadt

Handle the acpi interrupt controller with proper edge/level handling,
wrapped in spltty to avoid racing against the interrupt controller.
Repair the gpe bit masking code while there, and do operations in the
right order
ok kettenis mlarkin, help from jordan at figuring out the order of
operations.


# 1.181 20-Jul-2010 matthew

When we're preparing to go to sleep, only call bufq_restart() if other
preparations failed.

ok kettenis@, thib@, deraadt@


# 1.180 19-Jul-2010 deraadt

permit compilation with ACPI_DEBUG


# 1.179 19-Jul-2010 deraadt

there is no need for a seperate dsdt_softc pointer; dsdt does not have a
softc but is just a part of the single acpi driver.
ok jordan


# 1.178 19-Jul-2010 deraadt

acpi_delay() is no longer needed; ok jordan


# 1.177 18-Jul-2010 mlarkin

Clear event status on resume, to avoid going back to sleep/reboot/shutdown
immediately after resume. Tested by a bunch of people on hackers@ on
various hardware. Fixes at least Toshiba NB200 and Gateway LT3103u.

ok marco


# 1.176 13-Jul-2010 deraadt

rename some thread related interfaces so they are logical


# 1.175 10-Jul-2010 jordan

Verify _ADR bdf is correct (some systems return 0xffff)
Works on Dell x9xx and Studio 1555


# 1.174 09-Jul-2010 jordan

Clear the noise


# 1.173 08-Jul-2010 jordan

Add mapping for ACPI device to PCI bdf (match autoconf tree)
Simplify resource parsing function to use buffer argument
Convert namespace linked lists to use queue macros
ok marco@, deraadt@


# 1.172 06-Jul-2010 deraadt

Use spltty() to lock downcalls from apm against the information being
modified by the acpi thread.
ok kettenis


# 1.171 06-Jul-2010 deraadt

Don't use a workq to do the suspend, because it races aginst the acpi
thread. Instead, just tell the acpi thread to do the suspend for us.
This makes apmd & zzz work correctly. While here, have acpithinkpad
attempt to post the event to apm before prompting the suspend itself.
ok kettenis marco mlarkin


# 1.170 05-Jul-2010 mlarkin

Reset register SCI_EN on resume. The ACPI spec clearly states we don't have
to do this, but many machines don't, or forget to, which leads to all sorts
of problems on resume (broken battery stats, overheating, failure to resume
more than once, etc).

tested by many, ok marco@, deraadt@


# 1.169 01-Jul-2010 thib

do bufq quiesce again, since vnd/the bufq_done call in
biodone has been fixed.


# 1.168 01-Jul-2010 jordan

Backout recent AML changes, breaks on R210 and others


# 1.167 01-Jul-2010 jordan

Simplified aml_parse_resource call, cleanup code
ok marco


# 1.166 01-Jul-2010 jordan

Fix subordinate bus # for multi-root PCI buses
ok marco, kettenis


# 1.165 30-Jun-2010 thib

Disable/partially backout the bufq quiesce changes as this
is causing havoc with vnds and release must be buildable.


# 1.164 29-Jun-2010 deraadt

Fix ramdisks (new code was placed inside a SMALL_KERNEL chunk)


# 1.163 29-Jun-2010 jordan

Add support for mapping ACPI to PCI devices
ok kettenis, deraadt


# 1.162 29-Jun-2010 kettenis

Call bufq_quiesce() on suspend to stop all I/O and bufq_restart on resume.
Makes suspend/resume work much better while doing disk I/O.

ok mlarkin@


# 1.161 29-Jun-2010 tedu

60 is a more reasonable guess for number of minutes in an hour than 100.
spotted by TeXitoi. ok deraadt


# 1.160 27-Jun-2010 jordan

Add _acpi_matchhids routine, precursor to pci device match


# 1.159 27-Jun-2010 mlarkin

Clear ACPI fixed event status on resume (power buttons/etc) so that some
machines don't immediately resume after suspending to S3.

Tested by kettenis@ and I on 5 or 6 machines, deraadt@ "yeah, looks good"


# 1.158 27-Jun-2010 jordan

Cleaned up acpi_maptable, adds to table list directly


# 1.157 07-Apr-2010 deraadt

If we have done a wsdisplay_suspend(), and ACPI decides it does not want to
suspend, we must roll back by calling wsdisplay_resume() or the virtual
console switching will be locked forever
ok oga


# 1.156 07-Apr-2010 kettenis

Make suspend/resume work on MP machines (running an MP kernel). Joint work
from mlarkin@ and me, with some amd64 fixes thrown in by deraadt@

ok marco@, deraadt@, pirofti@, mlarkin@


# 1.155 31-Mar-2010 kettenis

Bring /dev/apm support in line with apm(4). Make the suspend button send
a suspend request event to apmd(8) instead of suspending immediately. Also
keep track of whether /dev/apm and /dev/apmctl are currently open, such that
we can still suspend immediately if apmd(8) isn't running.

ok deraadt@, marco@, pirofti@, jsing@, oga@


# 1.154 30-Mar-2010 oga

Prevent the apmd/x races for good.

When we hit suspend time, go through all wsdisplays on the system. if
they are in mode MAPPED, but not MODE_DUMBFB then if possible do a full
vt switch to a !mapped vt, and prevent switching back until resume time.
This has to be called from MD code because this involves userland
running so that X can run the vt switch signal handler. This way, any
case where we are using the "poke registers from userland" model, we
will not be on the hardware when we go down, so the kernel can actually
handle thing properly.

Tested on several acpi laptops (by kettenis@ and ian@), x40 (me and
beck@ at LEAST) and zaurus (me). Maybe others, but if so I forgot who at
this time..

Idea from deraadt somewhere over the Faroe Islands (I thought of a
similar thing myself a while ago). Much prompting from him. Ok and
comments miod@


# 1.153 25-Mar-2010 oga

Fix & vs. && in the kqfilter code.

Based on a comparison with the apm code.

ok deraadt@, kettenis@


Revision tags: OPENBSD_4_7_BASE
# 1.152 26-Nov-2009 mlarkin

Visual (and on some machines, audio) feedback while the machine is
resuming via calls to aml _SST method. This also makes the "moon" LED on
thinkpads to blink during resume and cease when resume is completed.

ok pirofti@


# 1.151 26-Nov-2009 deraadt

Toggle into "cold" when we we disable interrupts, because tsleeping
in the AML interpreter and context switching into userland processes
tends to sometimes pop the ACPI trampoline page out of our address
space. Makes lots more machines work.
ok kettenis mlarkin


# 1.150 24-Nov-2009 jsg

in the resume path evaluate _WAK before dealing with device drivers
like the spec says, not after.

ok mlarkin@


# 1.149 24-Nov-2009 deraadt

abort suspends on SMP systems until that gets written; ok mlarkin


# 1.148 23-Nov-2009 mlarkin

pci should return the result of it's children's suspend/resume calls back
to its parent.

handle suspend failure case and unwind if devices have an activate function
that returns failure.

ok deraadt@, kettenis@


# 1.147 23-Nov-2009 mlarkin

acpi* devices need to call their children on suspend/resume.
ok deraadt@


# 1.146 23-Nov-2009 pirofti

Remove ACPI_SLEEP_ENABLED checks.

This enables by default the suspend/resume paths in the kernel.

Okay deraadt@.


# 1.145 23-Nov-2009 deraadt

knf and spacing that are super annoying


# 1.144 23-Nov-2009 mlarkin

Hook up Theo's new children suspend/resume function, as well as more ACPI
suspend/resume glue.

ok deraadt@


# 1.143 26-Oct-2009 deraadt

Do not do strncmp comparisons on dv_xname because longer device names which
look similar could arrive in the future. Instead, compare directly against
dv_cfdata->cf_driver->cd_name
Issue originally spotted by miod


# 1.142 26-Oct-2009 jordan

Verify checksum+address when loading ACPI tables.
Some systems had invalid entries in RSDT/XSDT.
ok marco@


# 1.141 23-Jul-2009 cnst

New aibs(4) driver for ASUSTeK AI Booster (ACPI ATK0110) hardware monitoring,
with sensor state support through limits provided by the ACPI.

Tested on several ASUS motherboards kindly networked by Sam Fourman Jr.

ok deraadt marco jordan


Revision tags: OPENBSD_4_6_BASE
# 1.140 03-Jun-2009 pirofti

Import acpi power resource driver. This is needed for suspend/resume logic.

Okay jordan@ and marco@.


# 1.139 03-Jun-2009 pirofti

Add acpivideo support. This adds brightness support for all laptops
except thinkpads, they will use the acpithinkpad driver. The driver is
also hooked into wsconsole. So brightness can be adjusted via:

$ wsconsctl display.brightness=<percentage>

This is very helpfull on some laptops that have a nasty bios and get two
steps instead of one when pressing the brightness button.

Tested on various dell, fujitsu, acer, samsung and other laptops.

Okay marco@, miod@. Suggestions from kettenis@.
Lots of reviews and help from miod@, thanks!


# 1.138 03-Jun-2009 jordan

Added ejectable bay for IDE cdroms
ok marco@


# 1.137 30-Apr-2009 marco

Oga changed apm ioctls and forgot to grep. Temporary work around by hitting
the sleep button when we get any request. This interface will chnage at
some point.
ok oga


# 1.136 19-Apr-2009 krw

Call acpi_sleep_walk() when powering down just like when entering
any other sleep state. This tells acpi wake devices to not wake up
the box until manual powering up resets their status. Makes my Dell
GX520 stay powered off in response to 'halt -p'.

In snaps for a while.

ok marco@ (several times) deraadt@


# 1.135 17-Apr-2009 pirofti

Add device ranking support for future sleep/wake developments.

okay marco@


# 1.134 11-Mar-2009 jordan

Cleanup.. use aml_evalinteger instead of aml_evalname
ok marco@


# 1.133 10-Mar-2009 jordan

Fix cut/paste error.. call _BFS not _PTS
ok marco@


Revision tags: OPENBSD_4_5_BASE
# 1.132 21-Feb-2009 marco

overzealous cut/paste; found by Vladimir Kirillov <proger@uaoug.org.ua>


# 1.131 20-Feb-2009 marco

Oops didn't mean for Debugger() to remain there.


# 1.130 19-Feb-2009 marco

suspend/resume bits so that we can develop this in tree. This is disabled.
code from mlarkin and me
help from art,toby,jordan and several others
ok jordan, go for it deraadt


# 1.129 10-Feb-2009 jordan

Fix null pointer for shutdown if acpi not enabled
ok marco@


# 1.128 04-Feb-2009 kettenis

Some systems that claim to be ACPI 2.0 apparently don't provide an XSDT
address. Make the code fall back on the RSDT on those systems.

From Joerg Niendorf <p3y80@internode.on.net>. Fixes PR 5986.


# 1.127 17-Jan-2009 krw

A couple of stray malloc/memset -> malloc(M_ZERO), a bit of knf and
lose a couple of unnecessary casts. No functional change.

Feedback from kettenis@

ok kettenis@ marco@


# 1.126 06-Nov-2008 marco

Collapse all sanity checks of hids into one function.

Originaly from jordan


Revision tags: OPENBSD_4_4_BASE
# 1.125 02-Jul-2008 fgsch

add acpivideo. for now almost an empty skel so others can work on
suspend/resume. eventually it will also manage output switching and
brightness where supported. prodded by marco@


# 1.124 11-Jun-2008 marco

Fix $OpenBSD$ for once and for all


# 1.123 08-Jun-2008 deraadt

aaa_dev was being set from an uninitialized variable; ok toby


# 1.122 07-Jun-2008 marco

Kill ACPIVERBOSE. It was never used and is worthless to boot.

comments jmc
ok toby


# 1.121 06-Jun-2008 marco

Shrink acpi some more for floppies and while here re-enable acpi on the
floppies. Fixes crazy sony vaio kettenis has.

tested by deraadt
ok deraadt jordan kettenis


# 1.120 01-Jun-2008 marco

Fix \n nit in SMALL_KERNEL.


# 1.119 01-Jun-2008 marco

Re-enable new parser. Lots of testing by various folks. Fits all media
now too.

ok kettenis deraadt


# 1.118 16-May-2008 dlg

back out jordans new parser.

it didnt have enough oks from the right people, it did break machines
that were previously working, and it bloated the RAMDISKS and install
media beyond the constraints of the media.

requested by deraadt@


# 1.117 14-May-2008 jordan

Adding new ACPI Parser code
Old guts of code still exists, needs to be torn out next
ok marco@
ok brad@


# 1.116 27-Apr-2008 jcs

add acpithinkpad(4), an acpi support driver for ibm/lenovo
thinkpads.

provides functionality that the bios did on older, non-acpi
thinkpads like respond to screen brightness keys and toggle the
bluetooth device on and off.

ok deraadt@ and marco@


# 1.115 24-Apr-2008 jsing

Introduce acpiasus(4), a driver for the ACPI based hotkeys found in many
ASUS laptops (including the ASUS EeePC) - largely based on NetBSD's
asus(4) driver. On the ASUS EeePC this allows us to enable/disable
wireless, change screen brightness and use the volume keys.

ok jsg@, weingart@


# 1.114 12-Apr-2008 kettenis

Get rid of acpi_s5 global variables; simply send SIGUSR2 instead of SUGUSR1
to tell init(8) to power down the machine.

ok krw@


Revision tags: OPENBSD_4_3_BASE
# 1.113 21-Jan-2008 jsg

Check against RESET_REG_SUP in FADT before doing the acpi reset dance
after all, not doing so appears to break more machines than it fixes.

ok marco@


# 1.112 05-Jan-2008 kettenis

Never attach more acpicpu(4) devices than cpu(4) devices. Always attach
the first acpicpu(4) device on non-MULTIPROCESSOR kernels, regardless of its
ACPI processor ID.

ok mikeb@


# 1.111 05-Dec-2007 deraadt

Hang acpi(4) below bios(4) instead of mainbus(4). This lets us move acpi(4)
before pcibios(4), and then.... if acpi(4) attaches, skip pcibios(4) since
it messes the machines up. Fixes claudio's HP dl320 G5, and almost assuredly
others.
ok kettenis


# 1.110 05-Dec-2007 deraadt

repair acpiopen/acpiclose and friends to handle the /dev/apmctl interface,
so that apmd(8) can actually work correctly against acpi(4), and thus,
even do the fancy -C/-A stuff; also tested by jmc


# 1.109 02-Dec-2007 fgsch

fix the trailing space for real now.


# 1.108 02-Dec-2007 fgsch

avoid trailing spaces on output; marco@ ok.


# 1.107 27-Nov-2007 kettenis

Fix off-by-one that made us attach acpicpu0 and acpicpu1 on GENERIC kernels.

ok marco@


# 1.106 25-Nov-2007 jsg

When available and ACPI is enabled use the ACPI reset method
to reboot. This is required for some quirky sis machines
and likely others to reboot.

Tested by many. 'commit early commit often' deraadt@


# 1.105 19-Nov-2007 deraadt

sigh, amd64 still uses that stupid acpi_enabled variable...


# 1.104 19-Nov-2007 deraadt

softc's come initialized to 0, you know...


# 1.103 19-Nov-2007 deraadt

store revision; from jsg


# 1.102 19-Nov-2007 kettenis

Initialize sc_facs earlier.

ok deraadt@, beck@, canacar@


# 1.101 16-Nov-2007 deraadt

fix the bus_space #define nightmare, so that amd64 and i386 are much more
uniform. as a result shared code like acpi needs less #ifdef's
ok marco kettenis


# 1.100 15-Nov-2007 deraadt

remove the #ifdef ACPI_ENABLE. the EC codepath is either safe enough (and
will be improved), or it is unsafe because AML assumes it must be there
ok almost everyone


# 1.99 15-Nov-2007 mikeb

Make acpicpu(4) attach to the existing CPUs only.

Problem reported by form@. Fix idea by kettenis@.
Lots of help from deraadt@. Tested by deraadt@, form@ and me.

ok deraadt marco


# 1.98 14-Nov-2007 deraadt

spacing


# 1.97 12-Nov-2007 deraadt

correct : vs , in attach-time messages


# 1.96 06-Nov-2007 deraadt

all ACPI_ENABLE kernels need acpi_interrupt()


# 1.95 06-Nov-2007 deraadt

move acpi_read_pmreg() and acpi_write_pmreg() out of SMALL_KERNEL so
that acpi ramdisks can compile (obvious solution)


# 1.94 05-Nov-2007 canacar

Establish the acpi interrupt earlier. Fixes problems with some
GENERIC.MP kernels not getting acpi interrupts. This is a
workaround until the real issue with late establishment is fixed.
ok weingart@, beck@, gwk@


# 1.93 03-Nov-2007 jordan

Added support for displaying wakeup devices
ok beck@,weingart@,gwk@


# 1.92 11-Oct-2007 kettenis

When walking the tree yo find _PRT methods, check whether the device is
actually enabled and functioning. Similar to what we do for _INI.

ok weingart@, beck@


# 1.91 08-Oct-2007 krw

More simple memset(,0,) -> M_ZERO changes. In this batch move to
size(*p) as the first malloc() parameter where p is declared locally
and thus easy to check. Add M_ZERO to gpe_table allocation in acpi.c
even though there is no obvious bzero or memset nearby.


# 1.90 13-Sep-2007 mk

Remove unnecessary memset() (see aml_evalnode(9)) and a bit of KNF.

ok toby


# 1.89 13-Sep-2007 weingart

Implement ACPI 6.5.1 spec tree walk for _STA and _INI.
Fixes mk's laptop. No regressions so far (thank you to the
testers).

ok gwk@, mk@, marco@


Revision tags: OPENBSD_4_2_BASE
# 1.88 17-Apr-2007 mk

Now that acpidock no longer hangs machines during boot (at least
according to the reports I've been getting) it should be safe to enable
it again, so remove the #if 0 stuff around it.

ok marco


# 1.87 11-Apr-2007 jordan

Added changes for C-State
ok marco@


# 1.86 26-Mar-2007 marco

Calculate minutes left more accurately.

From giovanni <qgiovanni@gmail.com>


Revision tags: OPENBSD_4_1_BASE
# 1.85 03-Mar-2007 mk

Stop looking for acpidock until we fix that stuff that breaks all pre-60
series ThinkPads with acpidock(4) enabled.


# 1.84 22-Feb-2007 marco

We were getting no interrupts in .MP. There are 2 fixes; one in
acpi_machdep.c and move the interrupt establishment down after ioapic
has been initialized.

ok jordan


# 1.83 21-Feb-2007 kettenis

Make an acpi-enabled GENERIC.MP work on systems that only support PIC mode.

ok marco@


# 1.82 19-Feb-2007 jordan

Added acpi_map_address function for mapping GAS/IO registers
ok marco@


# 1.81 18-Feb-2007 jordan

Added dynamic perfect hash for opcodes
ok marco@


# 1.80 18-Feb-2007 marco

Shave off another K pre compression.
Shuffle SMALL_KERNEL functions around so that we only have a single #ifndef


# 1.79 17-Feb-2007 marco

Add SMALL_KERNEL throught acpi to make it fit on boot media.
ok deraadt


# 1.78 13-Feb-2007 marco

Add smarter _INI code. This helps with some dissapearing devices.

ok kettenis jordan


# 1.77 31-Jan-2007 gwk

Introduce acpi_hasprocfvs thats "ACPI Has Processor Frequency and Voltage
Scalling" a global flag set by acicpu attach to indicate that ACPI is
handling fvs through the _PSS objects. This will fix disappearing
EST/powernow when booting kernels with acpi enabled, as acpicpu isnt
compiled into GENERIC yet. Also in cases where acpicpu is built in and
legacy EST/powernow work but acpi is missing the _PSS object, the legacy
routines will be used.

tested by dim@, and Nick Nauwelaerts <nick AT nauwelaerts.net>
ok marco@, dim@


# 1.76 22-Jan-2007 mk

Actually look for docking stations by looking for _DCK nodes.

ok marco


# 1.75 02-Jan-2007 marco

On some systems EC must be attached as the first device. Move EC from
generic _HID devices to its own routine to force first attach. Bug found
and debugged by joris@

Code mostly written by joris@


# 1.74 26-Dec-2006 marco

Add polling to devices that require it such as acpibat and acpitz.
Use the same tsleep/wakeup mechanism as the interrupt code to keep it non-
concurrent.


# 1.73 21-Dec-2006 deraadt

even more knf love


# 1.72 21-Dec-2006 marco

Remove some leftover acpiec gpe/interrupt code.


# 1.71 20-Dec-2006 deraadt

oops, name already used


# 1.70 20-Dec-2006 deraadt

rename __acpi_enable_gpe(); ok marco


# 1.69 20-Dec-2006 deraadt

acpi_unmap_pmregs() is not used, ok marco


# 1.68 19-Dec-2006 deraadt

simplify some low-level methods substantially (shrinking code); ok marco jordan


# 1.67 06-Dec-2006 mk

malloc(..., M_NOWAIT) with no NULL check is bad. If allocation fails,
complain, clean up, and bail out of attach.

input and ok gwk


# 1.66 28-Nov-2006 jordan

Fixed clearing arg for acpi_init_pic
ok kettenis@


# 1.65 27-Nov-2006 kettenis

Don't spam dmesg.


# 1.64 27-Nov-2006 jordan

Fixes for acpiprt routing
Post-parse fixup for namerefs
ok kettenis@


# 1.63 25-Nov-2006 marco

Don't handle acpiev events inside the gpe handler since this causes deadlocks.

ok jordan@


# 1.62 15-Nov-2006 kettenis

Hook up acpi apic interrupt routing support to generic acpi code.

ok jordan@


# 1.61 02-Nov-2006 marco

Kill acpiec attach function and add its functionality to the generic _HID
device attach

prodded and ok jordan


# 1.60 02-Nov-2006 marco

Fix comments to reflect reality.


# 1.59 19-Oct-2006 marco

Replace ACPI ioctls with APM ioctls. This makes ACPI talk the same protocol
as APM therefore userspace can now retrieve power information in the same
fashion as all other apm arches (zaurus, macppc, i386 etc).

To use force the use of acpi device like so: apmd -f /dev/acpi

ok jordan@


# 1.58 19-Oct-2006 jordan

Fixed GPE debug levels
Fixed ref counter on temporary bufferfields
DSDT parsing fully working on IBM Thinkpad
ok marco@


# 1.57 15-Oct-2006 jordan

Removed old GPE handler code remnants
ok marco@


# 1.56 12-Oct-2006 jordan

Massive ACPI changes
Fixed ACPI leaks
new ACPI API
ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.55 30-Jun-2006 gwk

KNF, No functional change,
"go at it" marco@


# 1.54 30-Jun-2006 jordan

Support for calling Embedded Controller via new GPE interface
ok marco@


# 1.53 30-Jun-2006 jordan

Added new code for GPE handling
Also works with ACPIEC; need to fixup acpiec.c to remove original interrupt handler
ok marco@


# 1.52 30-Jun-2006 jordan

Added offset argument for acpi read/write pmreg routines (new GPE code)
ok marco@


# 1.51 31-May-2006 todd

do not call acpi_powerdown() unless acpi has successfully initialized
ok gwk@ canacar@


# 1.50 29-May-2006 canacar

Add embedded controller support.
ok marco@ deraadt@ testing & ok gwk@


# 1.49 19-May-2006 canacar

Minimal thermal zone driver. Monitors thermal zone temperature,
shuts down the system if the 'critical temperature' is reached.
ok marco@


# 1.48 11-Apr-2006 marco

KNF.


# 1.47 11-Apr-2006 gwk

Add acpi_resume(struct acpi_softc *sc), which calls some AML methods,
detected at attach time, currently unused.

ok marco@


# 1.46 11-Apr-2006 gwk

Discover some ACPI sleep related methods, and call them at the
appropriate time in acpi_enter_sleep_state().

ok marco@


Revision tags: OPENBSD_3_9_BASE
# 1.45 26-Feb-2006 marco

Add acpicpu as a device.

ok jordan@


# 1.44 23-Feb-2006 marco

remove dead function.

ok jordan@


# 1.43 22-Feb-2006 marco

Print out all tables it found during autoconf. We'll need this data in
the future.


# 1.42 22-Feb-2006 marco

Make dmesg less verbose.

ok jordan.


# 1.41 22-Feb-2006 jordan

Added parsing of SSDT tables
ok marco@


# 1.40 22-Feb-2006 jordan

Added new aml_register to notify by device id
Moved powerdown code to button device
ok marco@


# 1.39 21-Feb-2006 marco

Readjust debug levels that crept in too low over the last few days.
KNF


# 1.38 21-Feb-2006 marco

Rig the power button so that the machine shuts down correctly.


# 1.37 20-Feb-2006 marco

Add acpibtn device. Remains disabled for now.


# 1.36 19-Feb-2006 jordan

Added code for aml_notify GPE notification callback
ok marco@


# 1.35 19-Feb-2006 grange

Fix acpi_enter_sleep_state() by replacing current ugly hack
with mostly correct sleeping state transition code.

ok brad@ marco@


# 1.34 19-Feb-2006 marco

jordan:
Enable interrupts.
Added peripheral code to make this work.

marco:
Replace softintr and other types of interrupts with a handler with context.
Fixed interrupt magic for GPEs.
Some KNF

This is work done by jordan and I but has to be a single commit.


# 1.33 17-Feb-2006 marco

Remove earlier introduced delays. We really only need them during autconf.

ok jordan@


# 1.32 17-Feb-2006 marco

Add a generic delay function as per the ACPI spec.

from jordan@


# 1.31 17-Feb-2006 marco

Use tsleep instead of delay when !cold.


# 1.30 17-Feb-2006 marco

Argh, we need delays between reads/writes. The backend reads (i2c most likely)
are so slow that values come back as 0 sometimes. We have to do this
despite the spec saying we shouldnt have to. Guess having a faster
parser than others can hurt sometimes.

Idea from jordan.


# 1.29 17-Feb-2006 jordan

Fixed code for handling _INI
Rearranged debug statements
ok marco@


# 1.28 16-Feb-2006 jordan

Added stub for calling _INI
Fixed aml_dowhile for NULL return
ok marco@


# 1.27 16-Feb-2006 jordan

Fixed parser/evaluator to retrieve battery info+state
Cleanup of parser
Dell laptops return battery status
ok marco@


# 1.26 06-Feb-2006 brad

fix a typo so this will compile on amd64.


# 1.25 04-Feb-2006 marco

When ACPI_ENABLED wasn't set but the devices were it had an unknown variable.

Pointed out by ian@.


# 1.24 03-Feb-2006 jordan

Rewrote aml parser yet again...
Fixed bus_space_map for i386/amd64
Added _BST and _BIF to acpibat to get status/info
ok marco@


# 1.23 20-Jan-2006 jordan

More evaluator changes
Removed old evaluator code
Fixed _HID value
ok marco@


# 1.22 19-Jan-2006 jordan

Added support for read/write of PCI Config space
Help jason@
ok marco@


# 1.21 18-Jan-2006 jordan

Added new evaluation routines
Added helper methods for buffer/field evaluation
Fixed broken indentation on dsdt.c

ok marco@


# 1.20 17-Jan-2006 jordan

Major overhaul of the aml parser. Evaluater temporarily disabled.

ok marco@


# 1.19 06-Jan-2006 grange

Check for ACPI control availability only if ACPI_ENABLE defined.


# 1.18 06-Jan-2006 grange

Informative error messages on attach, prettier dmesg.


# 1.17 06-Jan-2006 grange

printf -> dnprintf conversion for debug messages.


# 1.16 05-Jan-2006 grange

Find SLP_TYPx values and store them for future use in
acpi_enter_sleep_state().


# 1.15 05-Jan-2006 grange

Random KNF and cleanup


# 1.14 05-Jan-2006 grange

Provide symbolic names for the acpi sleeping states and use it.


# 1.13 28-Dec-2005 marco

Moved DEVNAME to a acpidev.h

ok jordan@


# 1.12 18-Dec-2005 sturm

add back comment about wrong code

ok marco


# 1.11 16-Dec-2005 marco

Remove test for kbd_reset. Prompted deraadt@, ok jordan@


# 1.10 16-Dec-2005 jordan

Added hooks for powerdown/power button press


# 1.9 16-Dec-2005 jordan

Cleaned up common power management register i/o
Added enter sleep state ioctl
Hooked power button interrupt
ok marco@


# 1.8 16-Dec-2005 marco

Remove autoconf chatter


# 1.7 16-Dec-2005 jordan

Moved DSDT AML parsing into parent ACPI driver
ok marco@


# 1.6 14-Dec-2005 marco

Add AC device glue.


# 1.5 14-Dec-2005 marco

Adjust debug printing levels to something more sane.


# 1.4 13-Dec-2005 marco

Add glue to attach devices to apci.


# 1.3 07-Dec-2005 marco

KNF


Revision tags: OPENBSD_3_8_BASE
# 1.2 10-Jul-2005 grange

Spacing cleanup.


# 1.1 02-Jun-2005 tholo

Start on a basic ACPI framework -- does not do much more than read out the
ACPI tables into kernel memory and attach ACPI and HPET timers currently.

In order to test this code, enabling the devices in GENERIC as well as
the ACPI_ENABLE option is needed. This code does not do any thermal
control yet, so this should be done with care depending on the platform.

In the tree so more people can contribute to making this more fully
featured.

Ok niklas@ grange@ tedu@


# 1.414 10-Aug-2022 patrick

Provide the AML root in ACPI's softc so that drivers that need access
don't have to declare it using extern.

Suggested by and with feedback from kettenis@
Tested by deraadt@ on arm64, amd64 and i386
Tested by bluhm@ on amd64 and i386


Revision tags: OPENBSD_7_1_BASE
# 1.413 17-Feb-2022 jsg

reduce includes


# 1.412 10-Feb-2022 visa

Embed klist head in acpi_softc to avoid explicit malloc.

OK kettenis@


# 1.411 09-Feb-2022 deraadt

Move x86-only stubs used by kern/subr_suspend.c into dev/acpi/acpi_x86.c
for sharing between i386 and amd64.
ok mlarkin kettenis


# 1.410 08-Feb-2022 deraadt

The suspend/resume code is a sticky mess of MI, MD, and ACPI sequencing.
This splits out the MI sequencing, backing it with per-architecture helper
functions. Further steps will be neccesary because ACPI and MD are too
tightly coupled, but soon we'll be able to use this code for more architectures
(which depends on figuring out the lowest-level cpu sleeping method)
ok kettenis


# 1.409 04-Feb-2022 robert

set the APM_BATT_CHARGING state if the battery is being charged and try
to calculate the remaining time to be fully charged

ok jca@


# 1.408 02-Feb-2022 deraadt

acpi_addtask() calls malloc() w/ M_NOWAIT (because some calls come from
interrupt context), this however means occasional resource shortage will
result in callbacks registration failing, and unknown consequences for
the task-submitting caller.
Changing this to use pools with a low water mark, decreases the odds
of that problem occuring.
ok kettenis


# 1.407 01-Feb-2022 deraadt

GPE_DIRECT is calling the AML parser from interrupt context, which is
not permitted. Luckily nothing is using GPE_DIRECT anymore, so this code
can be deleted.
ok kettenis


# 1.406 26-Jan-2022 kettenis

An ACPI device needs to be both present and enabled for it to function.
So only attempt to attach hardware that has both bits enabled. This fixes
an issue where com(4) would attach for a disabled serial port leading to
misdetection of the hardware variant and a subsequent hang when /etc/rc
runs ttyflags -a.

ok anton@, deraadt@


# 1.405 12-Jan-2022 patrick

Make acpi_getpropint() return uint64_t, as ACPI integers are in fact that
wide and some _DSD properties depend on it.

ok kettenis@


# 1.404 04-Jan-2022 patrick

acpi_getprop() needs to actually make sure that we're looking at the
correct property. While there adjust acpi_getpropint() as well to
increase similarity with acpi_getprop().

ok kettenis@


# 1.403 16-Dec-2021 anton

Attach com over acpi on amd64. Some hardware uses a different interrupt
assignment compared to the the legacy one supported by com over isa.
This causes the console to halt once userland takes over as no
interrupts are received. The actual address and irq can be read from
ACPI, kettenis@ already added support for arm64 which paved the way for
amd64.

Some consoles that previously attached over isa are now expected to
attach over acpi.

Thanks to patrick@ for testing on arm64.

ok kettenis@


# 1.402 07-Dec-2021 kettenis

Fix parsing of SR_IRQ resource descriptors.

ok patrick@, anton@


# 1.401 02-Nov-2021 deraadt

knf


# 1.400 30-Oct-2021 deraadt

Change hw.perfpolicy=auto by default, at startup. If the system has AC
power connected (default is yes when no driver differentiates) then default
to 100% performance. On battery, use the existing auto algorithm (which is
admittedly somewhat unrefined).
This change overrides the system/BIOS speed and puts OpenBSD in control.
As this happens very early during boot, besides speedups in all usage usage
patterns, some surprises: unhibernate and sysupgrade times are cut in half.
note: on a few architectures, the setperf fn pointer is changed late, and
thus the auto algorithm stops timeing out. kettenis and i will look for
a solution.
in snaps for more than a week.
ok kettenis


Revision tags: OPENBSD_7_0_BASE
# 1.399 20-Jul-2021 mlarkin

Fix trailing whitespace in a few places.

No code change.


# 1.398 09-May-2021 kettenis

Some x86 machines advertise the "hardware reduced" ACPI feature, advertise
S4 and S5 support, but fail to populate the SLEEP_CONTROL_REG and
SLEEP_STATUS_REG descriptions in the FADT. An example of such a machine
is the ASUS Zenbook 14 UM433DA. Any attempt to powerdown the machine
will result in a kernel crash. It turns out that using the legacy ACPI PM
registers works fine on this machien. So fall back on those registers
if SLEEP_CONTROL_REG and/or SLEEP_STATUS_REG aren't provided.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_9_BASE
# 1.397 15-Mar-2021 patrick

Add acpi_iommu_device_map(), which replaces the DMA tag with one that
is blessed with IOMMU magic, if available. This is mainly for arm64,
since on amd64 and i386 the IOMMU only captures PCIe devices, as far
as I know, which uses the pci_probe_device_hook(). This though is for
non-PCI devices attached through ACPI.

ok kettenis@


# 1.396 07-Mar-2021 patrick

Pass standard DMA tag to acpi(4) table drivers.

ok kettenis@


# 1.395 27-Dec-2020 deraadt

acpi_map_address() cannot be wrapped by SMALL_KERNEL anymore, as
it is used by acpihpet.c


# 1.394 25-Dec-2020 visa

Refactor klist insertion and removal

Rename klist_{insert,remove}() to klist_{insert,remove}_locked().
These functions assume that the caller has locked the klist. The current
state of locking remains intact because the kernel lock is still used
with all klists.

Add new functions klist_insert() and klist_remove() that lock the klist
internally. This allows some code simplification.

OK mpi@


# 1.393 17-Dec-2020 kettenis

Fix some issues with referencing named ACPI nodes from Packages.
These references need to be resolved at runtime rather than when
they're parsed such that they pick up the right values for those nodes
which can be changed when for example _INI methods run.

The current approach is to replace these reference with a string that
names the node in question. The problem with that is that packages
can also contain normal strings. Which means that we need special
code that depends on the context where the package is used.

This diff takes a different approach by retaining a reference when
parsing. Code that uses a package will need to resolve this reference
but there is no ambiguiety anymore.

ok patrick@


# 1.392 05-Dec-2020 kettenis

Add support for the _EVT method to the GPIO support code.

ok patrick@


Revision tags: OPENBSD_6_8_BASE
# 1.391 27-Aug-2020 jmatthew

Reorder the acpi attach process so we look at Processor() CPU nodes
after Device() ones, since we should prefer the newer node type.
If we see any Device() nodes, don't attach acpicpu(4) to any Processor()
nodes if they're also present. This also makes acpitz(4) and
acpipwrres(4) devices attach slightly later.

ok kettenis@, also tested by jmc@


# 1.390 16-Aug-2020 gnezdo

Remove sc_maxgpe from acpi_softc which was never read from

Remove a dead conditional from acpi_init_gpes while there.

ok jcs@


# 1.389 21-Jul-2020 deraadt

acpi can use IPL_BIO (a low interrupt) since it only enqueues operations for
later processing. The use of a high interrupt will predate suspend/resume
efforts, we had to redesign acpi to be non-reentrant obviously
discussed with kettenis, in snaps for more than a week


# 1.388 24-Jun-2020 cheloha

kernel: use gettime(9)/getuptime(9) in lieu of time_second(9)/time_uptime(9)

time_second(9) and time_uptime(9) are widely used in the kernel to
quickly get the system UTC or system uptime as a time_t. However,
time_t is 64-bit everywhere, so it is not generally safe to use them
on 32-bit platforms: you have a split-read problem if your hardware
cannot perform atomic 64-bit reads.

This patch replaces time_second(9) with gettime(9), a safer successor
interface, throughout the kernel. Similarly, time_uptime(9) is replaced
with getuptime(9).

There is a performance cost on 32-bit platforms in exchange for
eliminating the split-read problem: instead of two register reads you
now have a lockless read loop to pull the values from the timehands.
This is really not *too* bad in the grand scheme of things, but
compared to what we were doing before it is several times slower.

There is no performance cost on 64-bit (__LP64__) platforms.

With input from visa@, dlg@, and tedu@.

Several bugs squashed by visa@.

ok kettenis@


# 1.387 10-Jun-2020 jca

Give userland a consistent battery and A/C state after resume

Previously we refreshed the state of acpiac(4) and acpibat(4)/acpisbs(4)
by queueing fake ACPI events to be processed by the acpi thread.
acpibat(4) using ACPIDEV_POLL meant that its status could be refreshed
first, resulting in an APM_POWER_CHANGE event sent to userland before
the state of acpiac(4) was up to date. Because of this, apmd(8) could
see A/C unplugged when it was in fact plugged, possibly leading to
a wrong decision.

Instead of using fake ACPI events, refresh the status of acpiac(4) and
acpibat(4)/acpisbs(4) right away at DVACT_WAKEUP time. To reuse the
existing refresh code of those drivers, make their ACPI *_notify()
handlers responsible for sending APM_POWER_CHANGE events instead. Also
queue an APM_POWER_CHANGE event right after resume to retain existing
behavior.

This fixes the consistency of data shown to userland, and makes those
drivers less special.

Initially discussed last year (!) with mpi@ at Miod's place.
Feedback and ok jcs@, deraadt@ agrees with the use of DVACT_WAKEUP here.


# 1.386 29-May-2020 deraadt

dev/rndvar.h no longer has statistical interfaces (removed during various
conversion steps). it only contains kernel prototypes for 4 interfaces,
all of which legitimately belong in sys/systm.h, which are already included
by all enqueue_randomness() users.


# 1.385 14-May-2020 kettenis

Bring back the acpi(4) changes. This time around only use the ACPI _CRS
information on ACPI 5.0 and later.

ok krw@, patrick@


# 1.384 11-May-2020 jca

More accurate remaining power reporting with disparate batteries

Overall remaining power was computed as the average of the remaining
power of each battery, in percents. This doesn't work with batteries of
different capacity, so use the total capacity and remaining power
instead.

Reported by lidstah, successful tests by lidstah and benno@, ok kettenis@


# 1.383 08-May-2020 kettenis

Move parsing of _CRS into acpi(4) and pass the parsed address and interrupt
information in struct acpi_attach_args.

ok mpi@


Revision tags: OPENBSD_6_7_BASE
# 1.382 14-Apr-2020 kettenis

Implement acpi_getprop(), the ACPI equivalent of OF_getprop().

ok patrick@


# 1.381 12-Apr-2020 kettenis

Add support for the _CCA method. This method indicates whether DMA is
cache-coherent or not. To implement this, acpi(4) gets two bus_dma tags
and passes the appropriate one when attaching devices based on _CCA.
On i386/amd64, where for all practical purpose DMA is always cache-coherent,
the two tags are the same. But on arm64 they are distinct.

ok patrick@


# 1.380 07-Apr-2020 visa

Abstract the head of knote lists. This allows extending the lists,
for example, with locking assertions.

OK mpi@, anton@


# 1.379 03-Apr-2020 mpi

Fix incoherencies of errors returned by various *kqfilter routines.

EOPNOTSUPP is returned when there's no "filterops" for a given fd.
EINVAL when the requested filter isn't supported by the "filterops".
ENXIO means the underlying device is no longer valid/present.

ok visa@


# 1.378 20-Feb-2020 visa

Replace field f_isfd with field f_flags in struct filterops to allow
adding more filter properties without cluttering the struct.

OK mpi@, anton@


# 1.377 31-Dec-2019 visa

Use C99 designated initializers with struct filterops. In addition,
make the structs const so that the data are put in .rodata.

OK mpi@, deraadt@, anton@, bluhm@


# 1.376 31-Dec-2019 mpi

Convert infinite sleeps to {m,t}sleep_nsec(9).

ok kn@


# 1.375 31-Dec-2019 jsg

include sys/sensors.h for sensor use
don't depend on indirect inclusion from acpidev.h


Revision tags: OPENBSD_6_6_BASE
# 1.374 07-Sep-2019 kettenis

Revert acpipci(4) changes. Too many systems are broken. We'll try again
after 6.6 as been released. The acpireg.h change stays behind.


# 1.373 28-Aug-2019 kettenis

Use ACPI information to attach PCI busses like we do on arm64. There are a
few additional quirks though, and attaching the PCI busses is delayed to
replicate the existing code more closely. That may be changed in the
future. Also tweak how we handle MSI support and respect to ACPI flag
that says we shouldn't attempt to use MSIs.

Some fallout is expected.

ok patrick@


# 1.372 27-Aug-2019 deraadt

On amd64 the bootup and resume paths for SP/BP/SP were not performing
msr/pat, clock-startup, firmware upgrade, fpu initialization, and cpu
soft-state initialization in the same order. That kind of chaos is
crazy and fragile. Improve the acpi resume call sequence and unify order
of the operations.


# 1.371 02-Jul-2019 jcs

acpi_set_gpehandler: turn edge arg into flags, add support for
passing GPE_DIRECT which directly executes the supplied callback and
clears a level interrupt rather than going through acpi_taskq which
can be slow when other driver callbacks are in the queue.

This should not be used for GPE handlers that need to do anything
ACPI-related.

ok kettenis


# 1.370 10-Jun-2019 kettenis

Print proper ACPI version number.

ok mlarkin@


# 1.369 08-Jun-2019 kettenis

When establishing the mapping between ACPI device nodes and PCI devices,
skip PCI host bridges and devices that aren't there.

ok patrick@


# 1.368 07-Jun-2019 kettenis

Introduce acpi_getsta() and use it to eliminate some duplicated code.

ok patrick@


# 1.367 12-May-2019 kettenis

Make sure the extended PM register descriptions actually exist before
using them. Fixes machines with ancient ACPI 1.0 (such as recent
hypervisors).

ok deraadt@


# 1.366 12-May-2019 kettenis

Add support for mapping ACPI PM registers using the "extended" gasio
representation from the FADT. Mostly fixes the Lanner NCA-1510.

ok lteo@, mlarkin@, deraadt@


# 1.365 11-May-2019 lteo

Fix incorrect error message.

ok kettenis@


# 1.364 08-May-2019 tedu

free size. the allocation is the header plus the length.


Revision tags: OPENBSD_6_5_BASE
# 1.363 04-Apr-2019 kettenis

Avoid potential null-pointer dereference. Found by Kent R. Spillner.

ok deraadt@, jsg@


# 1.362 20-Jan-2019 jsg

fix SMALL_KERNEL build after changes in rev 1.361
reported by naddy@ ok deraadt@


# 1.361 19-Jan-2019 tedu

Add a pwraction sysctl that controls what the power button does on acpi.
By default, nothing changes -- shutdown is initiated. But allows turning
power button into a sleep button if desired.
(grudging) ok from a few parties


# 1.360 26-Oct-2018 kettenis

Add acpipci(4) on amd64. For now this only calls the PCI-specific _OSC
method to let the ACPI implementation know what features we support.


Revision tags: OPENBSD_6_4_BASE
# 1.359 30-Aug-2018 kettenis

Move softbutton check before the check that skips attaching certain acpi
devices. Fixes regression caused by matching on _CID in addition to
matching on _HID.

ok matthieu@, mlarkin@


# 1.358 25-Aug-2018 kettenis

Match ACPI devices based on _CID if no match for _HID is found.

ok mpi@


# 1.357 19-Aug-2018 kettenis

Add support for multiple PCI segments. Only really implemented for arm64
for now as amd64/i386 firmware still caters for legacy OSes that only
support a single PCI segment.

ok patrick@


# 1.356 03-Aug-2018 kettenis

Let ahci(4) match on _CLS instead of _HID when attaching at acpi(4). Avoids
having to add many more _HID entries to the match table.

ok deraadt@, mlarkin@


# 1.355 10-Jul-2018 kettenis

Make legacy interrupts work in acpipci(4).

ok patrick@


# 1.354 05-Jul-2018 kettenis

Add acpipci(4), a driver that supports generic ECAM-compatible PCI host
bridges based on information provided by ACPI.

ok mlarkin@


# 1.353 04-Jul-2018 kettenis

Properly pass around the PCI "chipset tag" in acpi(4) and refactor
acpimcfg(4) to call an MD initialization functions that sets up a tag for
PCI ECAM.

ok guenther@, mlarkin@, krw@


# 1.352 01-Jul-2018 kettenis

Move acpi(4) attach glue into acpi_machdep.c.

ok guenther@, deraadt@


# 1.351 01-Jul-2018 kettenis

Add support for _DSD Device Properties.

ok mlarkin@


# 1.350 30-Jun-2018 kettenis

Add intr_enable() function, intended for MI use to amd64 and i386 and use
this in the acpi(4) suspend/resume code paths.

ok deraadt@


# 1.349 29-Jun-2018 kettenis

Use <stdint.h> types.

ok mpi@, jung@, krw@, deraadt@


# 1.348 29-Jun-2018 mlarkin

ACPI: Allow (with warning) GPE handler reassignment, instead of returning
a failure code.

ok kettenis


# 1.347 26-Jun-2018 mlarkin

knf


# 1.346 26-Jun-2018 kettenis

Let acpi(4) pass down the bus dma tag.

ok deraadt@


# 1.345 25-Jun-2018 kettenis

Reorganize acpi(4) code a bit in preparation for upcoming arm64 support.

ok deraadt@


# 1.344 20-May-2018 kettenis

Wake up the acpi thread after scheduling a gpio event.

ok mpi@


# 1.343 17-May-2018 kettenis

Sprinkle some #ifndef SMALL_KERNEL such that RAMDISK kernels cuild again.

pointed out by stsp@


# 1.342 17-May-2018 kettenis

Implement GenericSerialBus OpRegion support.

ok mlarkin@


# 1.341 27-Mar-2018 kettenis

Add acpicmos(4), a driver that implements SystemCMOS OperationRegion
access support. This fixes machines where the AML doesn't check whether
support for this OperationRegion type has been registered by the OS.

ok mlarkin@


Revision tags: OPENBSD_6_3_BASE
# 1.340 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.339 10-Feb-2018 deraadt

Syncronize filesystems to disk when suspending. Each mountpoint's vnodes
are pushed to disk. Dangling vnodes (unlinked files still in use) and
vnodes undergoing change by long-running syscalls are identified -- and
such filesystems are marked dirty on-disk while we are suspended (in case
power is lost, a fsck will be required). Filesystems without dangling or
busy vnodes are marked clean, resulting in faster boots following
"battery died" circumstances.
Tested by numerous developers, thanks for the feedback.


# 1.338 08-Feb-2018 deraadt

After flushing disks, try again to release all memory. We don't need to
save dirty memory to the hibernate space.


# 1.337 08-Feb-2018 deraadt

Toss all releaseable memory, because fragmentation can get in the way
of allocating the hibernate playpen.


# 1.336 08-Feb-2018 deraadt

sr_quiesce() is a new approach for ensuring that softraid drains
output to the disks.

This is part of a larger suspend/resume filesystem-safety diff, which
has been worked on for a couple of months already. Tests by job, krw,
beck, benno, and others. Sometimes even by snapshot users...


# 1.335 29-Nov-2017 kettenis

Add support for GPIO-signaled events.

ok mlarkin@


# 1.334 16-Nov-2017 jcs

Add PCI attachment for dwiic(4) needed by Intel 100 Series machines

ok kettenis


# 1.333 14-Nov-2017 kettenis

Add a few more PNP IDs to the skip lists.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.332 17-Aug-2017 stsp

Make dwiic(4) attach its ACPI parent dependencies.
This change makes the Asus E200HA keyboard work.

Original analysis and patch by Cesare Gargano
ok kettenis@


# 1.331 09-Aug-2017 dcoppa

Add "PNP0303" (8042 PS/2 Controller) to acpi_isa_hids[]

ok kettenis@


# 1.330 22-Jul-2017 jcs

acpibat: look for _BIX first before falling back on _BIF

ACPI 4.0 deprecated _BIF for battery status so some newer machines
are exporting _BIX which extends _BIF's package with a few extra
fields. When using _BIX, export the new discharge cycle count as a
sensor.

feedback from kettenis and anton
ok deraadt


# 1.329 20-Jul-2017 kettenis

Start a list of isa(4) device names such that we can avoid printing them
as "not configured".

ok mikeb@, millert@, deraadt@


# 1.328 08-Apr-2017 deraadt

Sensors are run as callbacks inside tasks. During suspend a sensor
could be running inside a driver that will be force-detached, or due
to tsleep end up disrupting the softstate/hardstate contract. At
suspend time, quisce all these callbacks by waiting for completion.
This issue has never been observed for real, but may be implicated in
suspend/resume failures.
ok kettenis guenther mlarkin


# 1.327 08-Apr-2017 deraadt

Two prototypes for acpi_maptable; merge to one place


Revision tags: OPENBSD_6_1_BASE
# 1.326 27-Mar-2017 deraadt

Now that hibernate_alloc() only has clean success/failure, don't
need to call hibernate_free() to clean up a partial mess.
ok mlarkin kettenis


# 1.325 13-Mar-2017 deraadt

allow ramdisks to build; ok jcs


# 1.324 12-Mar-2017 jcs

try to attach acpisbs first and if successful, don't attach acpibat


# 1.323 02-Mar-2017 natano

Add a new sysctl machdep.lidaction. The sysctl works as follows:

machdep.lidaction=0 # do nothing
machdep.lidaction=1 # suspend
machdep.lidaction=2 # hibernate

lidsuspend is just an alias for lidaction, so if you change one, the
other one will have the same value. The plan is to remove
machdep.lidsuspend eventually when people have upgraded their
/ets/sysctl.conf.

discussed with deraadt, who came up with the new MIB name
no objections mlarkin
ok stsp halex jcs


# 1.322 28-Feb-2017 natano

Untangle abstract sleep modes from ACPI sleep states. This paves the way
for further work in the area.

idea and ok deraadt
ok mlarkin


# 1.321 22-Feb-2017 jcs

add acpisbs, an acpi smart battery subsystem driver reading data
over smbus

currently disabled because it conflicts with acpibat

ok deraadt, kettenis


# 1.320 14-Jan-2017 kettenis

Revert aml_rdpciaddr changes; breaks several machines that were working
before.


# 1.319 11-Jan-2017 jsg

Increase the size of the buffers used for _HID and _CID strings to be
able to fit "Hyper_V_Gen_Counter_V1".

ok mlarkin@ deraadt@


# 1.318 08-Jan-2017 kettenis

Make aml_rdpciaddr() work for for devices behind bridges. The code now
uses the mapping between AML nodes and PCI devices that we establish.
Because _INIT methods may end up calling aml_rdpciaddr(), make sure we
create that mapping early. Also handle devices that aren't actually present.
These devices are now included in the mapping and reads will return an
all-ones patterm whereas writes are a no-op..

ok mlarkin@


# 1.317 25-Oct-2016 pirofti

Add the resource index to aml_parse_resource() callback function.

This is needed by an upcoming acpiec commit that handles machines breaking
the current ACPI specifications.

Change suggested by and ok kettenis@, guenther@


# 1.316 18-Sep-2016 guenther

If the APIC table (MADT) isn't present then ci_acpi_proc_id won't be set
when acpicpu tries to attach, leading to a NULL dereference. The ACPI
processor object on the Dells where this was reported is bogus and useless
anyway so apply the same test when deciding whether to attach to processor
objects as acpicpu would when matching them to cpus.

Problem reported by Eivind Eide (xenofil (at) gmail.com) and
Johan Huldtgren (johan+openbsd-bugs (at) huldtgren.com).
Testing by Eivind Eide.
ok kettenis@ mlarkin@


# 1.315 03-Sep-2016 naddy

Write the system time back to the RTC every 30 minutes.
This fixes the problem that long-running machines which were not
shut down properly would reboot with a badly offset system time.

hints and ok kettenis@


# 1.314 31-Aug-2016 mlarkin

Don't put a device in the wake device list if _STA says it's not there.

Fixes at least one machine that woke up from sleep / poweroff immediately.

ok kettenis@, tested by kettenis and martijn@


# 1.313 28-Jul-2016 kettenis

Store the acpi processor ID/UID in struct cpu_info, and use it to attach
acpicpu(4) drivers to the right cpu(4).

ok mlarkin@, guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.312 10-Jun-2016 kettenis

The power management timer has been made optional in ACPI 5.0A, so don't
attempt to attach acpitimer(4) if the timer isn't present.

Pointed out by jcs@

ok jcs@


# 1.311 08-May-2016 kettenis

Make sure devices listed by a device's _DEP method are attached before
we attach the device itself. The _DEP methode, introduced in ACPI 5.0,
is a hint that the device in question depends on OpRegion support from
the devices listed by _DEP, so we have to order them the proper way.
To prevent us from attaching those devices again when we encounter them
later walking down the device tree, keep track of our attempts to
attach a device driver to them using aflag in the aml_node structure.

ok guenther@


# 1.310 25-Apr-2016 pirofti

We attach acpihpet(4) and acpiec(4) without using the HID, so do not
report it in the dmesg.

OK deraadt@, mlarkin@.


# 1.309 02-Apr-2016 kettenis

Reduce dmesg spam by knocking out some acpi hardware IDs that don't need a
driver.

ok mpi@


# 1.308 29-Mar-2016 kettenis

Check if a device is present (using _STA) in acpi_foundhid. This prevents
us from reporting devices as "not configured" that aren't actually present,
and allows us to remove duplicated code from several drivers.

ok deraadt@, ok jsg@


# 1.307 29-Mar-2016 kettenis

Implement a cleaner way to prevent acpivout(4) from attaching if we have
a platform-specific method of controlling the backlight.

ok millert@, mlarkin@, jung@


# 1.306 28-Mar-2016 kettenis

Adding each and every new acpi device driver to acpi_foundhid() is
gettinga bit of the burden. And it really isn't how our autoconf
framework is supposed to work for busses that can be enumerated. All
the drivers already check for a matching _HID in their attach
function. So we can just drop the checks in acpi_foundhid() and let
the drivers sort themselves out.

Print a "not configured" line for devices for which we don't attach adriver.
This may be a bit much as there are devices for which we will not have a
driver. This will be removed if it causes too much dmesg spam.

ok deraadt@, mlarkin@


Revision tags: OPENBSD_5_9_BASE
# 1.305 17-Jan-2016 jsg

Take acpi_foundhid() outside of #ifndef SMALL_KERNEL so the ramdisk
kernel can find and attach dwiic(4) and sdhc(4).


# 1.304 17-Jan-2016 jsg

remove an unused global variable


# 1.303 14-Jan-2016 kettenis

Attach dwiic(4) on the Intel Bay Trail i2c controllers as well.


# 1.302 12-Jan-2016 kettenis

Improve emulation of PM registers on Hardware-redcued ACPI by emulating
SPL_TYPx, SLP_EN and WAK_STS using the new SLEEP_STATUS_REG and
SLEEP_CONTROL_REG registers. Makes the transition into S5 (powerdown) work
on the ASUS X205TA.

ok deraadt@, mlarkin@


# 1.301 12-Jan-2016 jcs

Add dwiic, a driver for the Synopsys DesignWare i2c controller found
on the Samsung ATIV Book 9 laptop. This initial version only
supports ACPI config/attachment.

Add ihidev, a HID-over-i2c driver largely based on uhidev. dwiic
handles attaching ihidev devices found in ACPI.

Add ims, a HID-over-i2c mouse/trackpad driver to get basic cursor
and button functionality from HID-compliant i2c trackpads.

ok kettenis deraadt


# 1.300 11-Jan-2016 kettenis

Make sdhc(4) attach to hardware IDs "80860F14" and "PNP0FFF".


# 1.299 09-Jan-2016 kettenis

Initial support for Hardware-Reduced ACPI. This skips the legacy mode checks
by emulating the PM_CNT register such that SCI_EN is always enabled. It also
skip attaching acpitimer(4) since the relevant registers are no longer there.

ok deraadt@, mlarkin@


# 1.298 06-Jan-2016 kettenis

Follow the spec and check the SCI_EN bit to see if ACPI has already
been enabled and don't bother enabling it again if it is. This should fix
systems that don't implement legacy mode and therefore set ACPI_ENABLE and
ACPI_DISABLE to zero.

ok mlarkin@, yasuoka@


# 1.297 23-Nov-2015 reyk

Back out the CRITICAL change, turn it back from 10% to 25%. Some
people with old/broken batteries didn't like this change, and the
underlying heuristics are wrong.


# 1.296 22-Nov-2015 reyk

The "CRITICAL" APM battery level, that is exported/faked by the ACPI
stack, is currently set to 25%. With 25% battery left, a typical
modern laptop will have at least one hour runtime left. It is an
arbitrary value, and other APM power layers in the kernel set CRITICAL
to something like 5% or 10%, so set it to 10% for now.

OK mlarkin@


# 1.295 28-Sep-2015 deraadt

In low-level suspend routines, set cold=2. In tsleep(), use this to
spit out a ddb trace to console. This should allow us to find suspend
or resume routines which break the rules. It depends on the console
output function being non-sleeping.... but that's another codepath which
should try to be safe when cold is set.
ok kettenis


# 1.294 06-Sep-2015 deraadt

clarify malloc arguments (base + length...)


# 1.293 28-Aug-2015 deraadt

fairly simple sizes for free(); ok tedu


# 1.292 20-Aug-2015 kettenis

Respect the access size when reading or writing to pci config space.
This version also makes sure writes are properly aligned, preventing
the panics seen with the previous incarnation of this change.

Fixes battery status passthrough in vmware, brightness keys on my x220
and who knows what else.

ok mlarkin@, deraadt@


Revision tags: OPENBSD_5_8_BASE
# 1.291 04-Aug-2015 deraadt

If we are setting ACPI_PM1_BM_RLD do it only once, and remember to
repeat this action at resume time.
ok mlarkin guenther


# 1.290 29-Jul-2015 deraadt

Something subtle in previous revision is locking up suspend or booting
on some laptops. Mailing lists threads mention x220 and Asus machines.
Reverting for now.


# 1.289 21-Jul-2015 kettenis

iRespect the access size when reading or writing to pci config space.
Fixes battery status passthrough in vmware and who knows what else.

Tested by many.

ok mlarkin@, deraadt@


# 1.288 17-Jul-2015 mlarkin

fix comments


# 1.287 26-Mar-2015 jsg

Use the 0x10 extended capability to determine if the trackstick
buttons are wired to the trackpad and need to be re-routed to the
trackstick. Without this change the buttons on 2015 thinkpads
get picked up as extended buttons that show up as scroll up/down.

Remove the X1 Carbon 2015 (LEN0048) and X250 (LEN0046) from the
top button area/soft buttons quirks list. Also avoid using the
quirk list entirely if the capability bit is set.

Based on work from jcs@ with feedback from mpi@
ok mpi@ jcs@


# 1.286 20-Mar-2015 kettenis

The ACPI standard says that a PCI function number of FFFF can be used to refer
to all functions on a device. This makes no sense in the context of accessing
PCI config space. Nevertheless stsp@ has a machine with AML that tries to
do config space reads from a device where _ADR encodes function number FFFF.
This is almost certainly buggy AML, but since Linux (and presumably Windows)
boot just fine on this machine we need to work around this. Linux effectively
treats function FFFF as function 7, which isn't present on this machine.
Instead we simulate a read from a non-existent device by returning a bit
pattern of all ones. Writes to function FFFF will still panic.

ok mlarkin@


# 1.285 16-Mar-2015 deraadt

Reduce use of SMALL_KERNEL in acpi, bringing acpiec driver and more gpe
handling into RAMDISK. This is now possible because the install media
has ample room. The goal is to reduce special cases where we may be
skipping (unknown) important operations...
ok mlarkin kettenis


# 1.284 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.283 07-Feb-2015 deraadt

New framework that allows hibernate to pass in entropy from it's fresh
boot.
ok mlarkin


# 1.282 06-Feb-2015 mlarkin

Fix a hibernate crash on some machines due to unmapping a page that
may not have been mapped previously (in the failure to hibernate case).

Also ensure that the lowmem ptp is mapped in all cases (not just MP).

ok kettenis


# 1.281 17-Jan-2015 deraadt

resume_randomness() before spinning up other cpus...


# 1.280 15-Jan-2015 jsg

Define a new wscons mouse type for Synaptics clickpad devices that lack
physical buttons. This will be used if the acpi pnpid for the mouse
matches a list provided by Synaptics found in Linux.

Combined with the xenocara changes this will remove the need for an xorg
config file for the x240/t440/t540 generation of thinkpads.

ok matthieu@ shadchin@ kettenis@


# 1.279 11-Jan-2015 kettenis

Tolerate checksum errors in ACPI tables (but complain about them). Apparently
there are machines out there with broken BIOSen with such checksum errors in
thier RSDT tables. Windows runs fine on these machines, so it probably does
not check. Linux ignores checksum errors as well (and prints a warning).

ok deraadt@, krw@


# 1.278 18-Dec-2014 deraadt

suspend and resume the RNG (at what looks like the right spots)
ok reyk djm


# 1.277 09-Dec-2014 doug

Replace some malloc(n*size,...) calls with mallocarray().

ok tedu@ deraadt@


# 1.276 06-Dec-2014 mpi

If "machdep.lidsuspend" is not set, do not go back to sleep if the lid
is still closed when resuming. This allows docked laptop to be resumed
without opening them.

ok mlarkin@


# 1.275 23-Nov-2014 mlarkin

If a machine has no _LIDs, then none can be open. This diff fixes a problem
where machines without any _LIDs were going back to sleep after hibernate
due to the recent suspend-if-lid-closed-on-resume diff. Noticed on qemu,
which doesn't have any _LIDs.

discussed with deraadt


# 1.274 18-Nov-2014 krw

Nuke yet more obvious #include duplications.

ok miod@


# 1.273 08-Nov-2014 mlarkin

If resuming from sleep (zzz/ZZZ) and the lid is still closed, go back to
sleep. This prevents accidental lid flex or slight opening in a backpack
from waking the machine up and leaving it resumed (powered on).

ok deraadt@


# 1.272 26-Sep-2014 kettenis

Rework piglet and pig allocation. Currently the piglet gets allocated
deep down in the suspend path, where it is really hard to recover from
allocation failure. So allocate the piglet early on in the suspend path.
Also change the piglet and piglet allocation functions to use km_alloc(9)
instead of doing pmemrange magic. This removes a bunch of code which, in the
case of the piglet allocation, is broken since it results in a NULL pointer
dereference. Also switch the piglet allocation to not wait. If we can't
allocate 16MB of phys contig memory on a halfway modern machine we're almost
certainly under a lot of memory pressure and we're better off not trying to
hibernate anyway.

ok mlarkin@


# 1.271 25-Sep-2014 kettenis

Bring back rev 1.249. Now that mp_setperf() has been fixed, it should be
safe to restore perflevel when resuming. Should fix the issues people
see with the processor running at full speed heating up the machine while
the apm command reports that the CPUs are running at a low speed.

ok dcoppa@, deraadt@


# 1.270 23-Sep-2014 kettenis

Release the acpi lock around calls to wsdisplay_suspend() and
wsdisplay_resume(). The X server may adjust the display brightness just
before/after the VT switch when suspending/resuming. In fact, it may
already be blocked adjusting the display brightness for some reason
unrelated to the suspend request.

The VT switch request will eventually time out, but the delay is annoying
and the failed VT switch will lead to problems after resume.

Needed for a future update of the xf86-video-intel driver.

ok mlarkin@


# 1.269 19-Sep-2014 kettenis

Use config_suspend_all(9).


# 1.268 08-Sep-2014 kettenis

Clear the PME Status bit on PCI devices in response to Device Wake events.
Fixes the interrupt storm on the 2009 Mac mini with WOL enabled on its
nfe(4) interface.

ok mlarkin@


Revision tags: OPENBSD_5_6_BASE
# 1.267 20-Jul-2014 mlarkin

Support hibernating to softraid crypto volumes.

much help and ok from deraadt@


# 1.266 16-Jul-2014 mlarkin

Reenable hibernate RLE support and flush+zero all memory after unpack.


# 1.265 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.264 12-Jul-2014 kettenis

Add a function to drop all clean pages on the page daemon queues and call
it when we hibernate.

ok mlarkin@, miod@, deraadt@


# 1.263 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.262 11-Jul-2014 mlarkin

Flush the buffercache to 16MB on hibernate and restore its previous max
size (kern.bufcachepercent) on resume, for better hibernate performance.

ok beck@


# 1.261 10-Jul-2014 blambert

remove unneeded header

ok mlarkin@


# 1.260 01-Jul-2014 kettenis

Adding an additonal delay after each access to SystemMemory or SystemIO
during autoconf makes no sense.

ok deraadt@


# 1.259 23-Jun-2014 kettenis

SystemMemory is used to access memory mapped registers on some machines, so we
must use the bus_space(9) API to access it instead of memcpy(9). Also make
sure we properly align access to fields that need more than byte alignment.

ok guenther@


# 1.258 21-May-2014 mlarkin

Fix invalid / missing format specifiers in several dnprintf statements
(used only when ACPI_DEBUG is enabled)

From a diff on tech@ by Fabian Raetz, with a minor change by me, tested
on i386 and amd64 with ACPI_DEBUG enabled and disabled.


# 1.257 25-Apr-2014 mlarkin

Defer the free of the hibernate structures until later, when it is safe to
do so.

Freeing the hibernate structures too early leads to inadvertent calls into
splx when we aren't ready for them yet. Found when trapping splx calls in
the resume path.

discussed with deraadt@ at m2k14


# 1.256 13-Apr-2014 mlarkin

Fix a badly worded debug message that implied the entire machine was going
to sleep, instead of just the acpi thread going to sleep.


# 1.255 31-Mar-2014 mpi

Change the order of operations during a suspend/resume cycle and
call bufq_quiesce() after executing the DVACT_QUIESCE handlers.

This should be safe since no disk nor controller drivers have such
handler but it will allow us to detach sd(4) devices attached to a
USB bus.

Another benefit pointed out by kettenis@ is that drivers that need
to read a firmware from the disk should be able to do it at resume
time in a DVACT_WAKEUP handler.

ok kettenis@, deraadt@


# 1.254 13-Mar-2014 dlg

get rid of the assumption that the head of the alldevs list is the
"mainbus" device. this breaks when mpath is enabled because it
attaches before mainbus and therefore takes the head position.

have autoconf provide device_mainbus() which looks up mainbus_cd,
and use that instead.

discussed with deraadt who just wants mpath stuff to move forward
despite there being many ways to shine this particular turd.


Revision tags: OPENBSD_5_5_BASE
# 1.253 21-Feb-2014 deraadt

Do the lid control further on in the resume sequence, since this is
AML, and will do WAITOK operations. While cold, that is a bad idea.
We seem to be safe with respect to the spec, I do not see any event
debounce issues here.
ok kettenis, tested by many


# 1.252 20-Jan-2014 claudio

Instead of checking sc->sc_revision <= 1 check that the
fadt->hdr_revision <= 1. This is a better check since it checks
the version of the proper descriptor. This allows the alix APU
to reboot since those boards have currently a wrong sc->sc_revision.
Committing this now so that it can be tested easier.
With and OK jsg@ also fine by derradt@


# 1.251 24-Dec-2013 kettenis

If the FADT has its SMI_CMD set to zero, assume we're only ACPI-only hardware
and don't need to disable SMI ownership of the ACPI hardware registers.

ok mlarkin@


# 1.250 23-Dec-2013 kettenis

Revert previous commit. It makes resume hang on some MP systems running
GENERIC.MP.


# 1.249 16-Dec-2013 kettenis

Restore hw.setperf upon resume like we do for apm(4) on i386. Seems we need
to do this fairly late in the resume to avoid hangs.

ok deraadt@, mlarkin@


# 1.248 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


# 1.247 06-Nov-2013 mpi

Add support for Power Resources for Dx states and the necessary hook
for PCI devices. This hook should be called twice, before and after
changing the power state of a PCI device.

Before setting the device to the new state, the ACPI layer will notify
every power resources linked to the device for that state and make sure
they are turned "_ON". After changing the state of the device, it will
decrement the reference of every power resources linked to that device
for the old state and turn them "_OFF" if they are no longer referenced.

This fixes the no-USB after resume problem seen on various ThinkPad,
problem initialy diagnosed with Alexander Polakov.

ok kettenis@, deraadt@


Revision tags: OPENBSD_5_4_BASE
# 1.246 01-Jun-2013 mlarkin

Stop printing ACPI wakeup devices in dmesg after the 16th wakeup device.

Helps with VMware VMs that report hundreds of wakeup devices.

ok reyk@, deraadt@


# 1.245 31-May-2013 bluhm

Fix the build for a kernel without wd and pciide in its config.
OK deraadt@ tedu@


# 1.244 30-May-2013 deraadt

Enforce ca_activate tree-walks over the entire heirarchy for all events,
cleaning up some shutdown-hook related code on the way.
(A few drivers related to sparc are still skipped at kettenis' request)
ok kettenis mlarkin, tested by many others too


# 1.243 18-Apr-2013 deraadt

pin the acpi thread to the BSP, because AML and SMI cannot be trusted. I
was certain we were doing this already, but it turns out no...


Revision tags: OPENBSD_5_3_BASE
# 1.242 24-Dec-2012 guenther

In acpiioctl(), Return though the splx() path if suser() returns an error

ok deraadt@


# 1.241 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


# 1.240 04-Oct-2012 ehrhardt

Use information provided by ACPI to attach secondary PCI host bridges.

ok kettenis@


# 1.239 07-Sep-2012 kettenis

Add a new function called acpi_pci_min_powerstate() that returns the lowest
power state for a PCI device for the power state (currently S3 or S4) we're
transitioning into. This function is probably too simplistic; there is a lot
of (silly) complexity in the ACPI spec.

ok deraadt@


Revision tags: OPENBSD_5_2_BASE
# 1.238 13-Jul-2012 pirofti

Disable acpivideo(4) on ASUS laptops.

There are some models that panic whenever an event is received.
Some cope, some don't. In order to get this right acpiec(4) requires
major rework. Until then let machines boot.

Discussed with deraadt@.


# 1.237 11-Jul-2012 mlarkin

for now, restrict hibernate to root in the acpi ioctl kernel interface.
normal rules for controlling this interface through apmd still apply.

ok deraadt@


# 1.236 09-Jul-2012 deraadt

tiny little oops in untested hibernate block


# 1.235 09-Jul-2012 deraadt

clean up ioctl() case statements


# 1.234 09-Jul-2012 deraadt

acpi_disable_onegpe() is not used; ok pirofti


# 1.233 24-May-2012 kettenis

If _MIN > _MAX consider a WordBus resource to be invalid. Fixes a panic on
the IBM x3550-M4.

ok mikeb@


# 1.232 29-Mar-2012 mlarkin

Missing #ifdef in previous diff


# 1.231 29-Mar-2012 mlarkin

Prevent hibernation attempts on machines with unsupported disk controllers.
The same check is also performed later in the hibernate sequence, but by
then it's too late to cleanly unwind (presently).

ok deraadt


# 1.230 26-Mar-2012 deraadt

simplify conditionals in acpi_sleep_state()
ok mlarkin


# 1.229 26-Mar-2012 deraadt

hook in the hibernate request code; half of this diff is from mlarkin
ok mlarkin


Revision tags: OPENBSD_5_1_BASE
# 1.228 20-Sep-2011 deraadt

Very early in suspend while the clock is still ticking realtime,
save the clock back to the rtc. The zaurus already did this.
ok phessler miod kettenis


Revision tags: OPENBSD_5_0_BASE
# 1.227 02-Jul-2011 nicm

kqueue attach functions should return an errno or 0, not a plain 1. Fix
the obvious cases to return EINVAL and ENXIO.

ok tedu deraadt


# 1.226 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.225 16-Jun-2011 pirofti

Import acpitoshiba driver.

This is originally written by Hiroyuki Aizu, ported and integrated
in our tree by Javier Vazquez <javama4 AT gmail> with tweaks and
suggestions by me.

Import prodded by deraadt@ and the lack of testers on tech@.


# 1.224 27-Apr-2011 jcs

attach acpithinkpad to newer lenovo models like the x120e

ok deraadt@


# 1.223 22-Apr-2011 jordan

Fix uninitialzied variables and formatting strings (-Wxxx errors)


Revision tags: OPENBSD_4_9_BASE
# 1.222 02-Jan-2011 jordan

Re-add code for ACPI taskqueue, eliminates need for state vars in acpi softc
Serialize processing ACPI tasks for sleep/powerdown/GPE event
ok deraadt@


# 1.221 31-Oct-2010 guenther

Revert last commit: it breaks resume on Thinkpads
"then please back it out" deraadt@


# 1.220 26-Oct-2010 jordan

Add task queue for ACPI gpe and notify handlers
This fixes eject on Dell Latitude dock and an issue on the Dell Mini
battery update.
ok deraadt


# 1.219 07-Oct-2010 jordan

Create separate functions for enabling wake and runtime gpes.
Remove old suspend/resume gpewalk function
ok deraadt, mlarkin


# 1.218 05-Oct-2010 jordan

split acpi_enable_onegpe to separate enable/disable functions
ok deraadt


# 1.217 05-Oct-2010 jordan

Added acpi_disable_allgpes function to clear GPE status
ok deraadt


# 1.216 05-Oct-2010 deraadt

clean up acpi_softc use more


# 1.215 05-Oct-2010 deraadt

delete a bit of #if 0 code


# 1.214 05-Oct-2010 deraadt

hide use of our global acpi_softc pointer a bit better


# 1.213 29-Sep-2010 deraadt

grab the acpi thread lock very early in acpi thread startup; not that it
is likely that anyone will race us... but why be careless.
ok kettenis


# 1.212 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.211 29-Aug-2010 jasper

- add a check for FWRITE here too.

ok miod@


# 1.210 27-Aug-2010 kettenis

Make PCI Power Management optional, and only enable it when acpi(4) attaches.

ok deraadt@


Revision tags: OPENBSD_4_8_BASE
# 1.209 08-Aug-2010 kettenis

Close race between the acpi thread and wscons ioctls running in process
context. We might rip this out post-release and replace it with a solution
that serializes things in a more obvious way.

ok deraadt@, miod@ and "those crazy texans who ok anything"


# 1.208 08-Aug-2010 deraadt

Report the acpi sleep states when on a RAMDISK; ok krw


# 1.207 07-Aug-2010 kettenis

Enforce acpi register access rescrictions.

ok mlarkin@, deraadt@, tested by marco@


# 1.206 07-Aug-2010 deraadt

upon resume, notify all the acpiac and acpibat drivers to update
their status. some laptops do not do this automatically.
ok kettenis canacar


# 1.205 06-Aug-2010 marco

Always call _PSW on _LID devices that support it to enable lid open to
resume the machine. Conversely disable it when the machine wakes up.

Tested by several ok deraadt


# 1.204 06-Aug-2010 deraadt

As part of a revamp of the PM1/GPE code, I write this diff to look for
unmanaged EN & STS bits in the PM1 register at interrupt time and report
them. As a side effect this splits the STS acknowledgement into two writes
(for power, and sleep) instead of one. The printf that is added (to spot
unmanaged STS bits) has not yet been triggered as far as we know.

Before the "write to PM1 registers at the right offset" diff went in, this
was not neccessary. But newer thinkpads do not have a working soft power
button without this diff.

We have no idea why.
ok mlarkin kettenis


# 1.203 05-Aug-2010 deraadt

PM1 power button events were generating 'fake events' to acpibtn PWRB,
but if there is no such device these were being tossed. Instead, arrange
for all power button handlers to flag a softc variable which is processed
by the thread; so that one place will know to request a powerdown (psignal
to init)
ok marco mlarkin


# 1.202 05-Aug-2010 deraadt

rename a variable which is annoyingly poorly named


# 1.201 05-Aug-2010 mlarkin

Write to the correct offset for various ACPI pmreg registers. Writing to
the correct register does properly clear the wake status bits and fixed
event bits, which allows some machines to have more than 1 suspend cycle.

bug spotted by deraadt@

tested by deraadt@ and myself on a half-dozen machines and others on
hackers@

ok deraadt@, marco@


# 1.200 05-Aug-2010 deraadt

Fix the power button. The pm1 register is 16 bits in size.
My mistake, a while back.
tested by krw


# 1.199 04-Aug-2010 kettenis

Print supported sleep states.

ok marco@, phessler@, deraadt@


# 1.198 03-Aug-2010 mlarkin

Use the proper flag for re-enabling certain hardware events (power btn,
sleep btn). The (incorrect) flag prevoiously used coincidentally had the
same value, so this shouldn't cause any different behavior than before.

tested on a variety of machines (i386, amd64, sp, mp) - no behavioral
change seen

ok deraadt@


# 1.197 28-Jul-2010 marco

Make all gasio failures look and behave the same.


# 1.196 28-Jul-2010 mlarkin

Delay a little longer on suspend before giving up. Fixes machines that
sometimes don't suspend because the sleep transition takes too long
(symptoms like failing to sleep but the backlight stays on).

ok deraadt@


# 1.195 27-Jul-2010 deraadt

remove XXX; EC can now be attached early when needed; ok jordan


# 1.194 27-Jul-2010 deraadt

more brackets feels good


# 1.193 27-Jul-2010 deraadt

XXX annotate a block in acpi_enter_sleep_state() that is pretty worrying


# 1.192 27-Jul-2010 deraadt

horrific indentation that nearly made me puke


# 1.191 27-Jul-2010 mlarkin

Set the sleeping indicator light on machines that support it, in case SMI
doesn't do it for us. Shows the 'moon' LED on older Thinkpads when
sleeping.

Tested on T43p, W500, T510 by me and on X61s by deraadt@.

ok deraadt@


# 1.190 27-Jul-2010 jordan

Early initialization of acpiec if ECDT table exists
Fixes hang when booting thinkpads while docked
ok deraadt


# 1.189 26-Jul-2010 deraadt

remove merge error which was commited


# 1.188 26-Jul-2010 pirofti

Add support for Sony ACPI hotkeys via a new driver: acpisony(4).

Currently it only works for the suspend button and tries to do right for
the brightness events, but I haven't found a Sony laptop that like to do
right so far.

In the future I want to make the brightness keys work on all Sony's and
also add support for the zoom hotkeys and whatever other funky keys I can
find on those things.

Okay deraadt@.


# 1.187 22-Jul-2010 deraadt

pretty large cleanup of SMALL_KERNEL


# 1.186 21-Jul-2010 deraadt

in the acpi GPE handler, late ack level interrupts not edge interrupts.
Duh.


# 1.185 21-Jul-2010 deraadt

Merge the edge and level gpe handlers into one (now possible since the
gpe structure tells us whether the gpe is edge or level)


# 1.184 21-Jul-2010 deraadt

delete #if 0 code we are not going to use; ok marco


# 1.183 20-Jul-2010 deraadt

make the mask code for acpi_enable_onegpe() match other similar code


# 1.182 20-Jul-2010 deraadt

Handle the acpi interrupt controller with proper edge/level handling,
wrapped in spltty to avoid racing against the interrupt controller.
Repair the gpe bit masking code while there, and do operations in the
right order
ok kettenis mlarkin, help from jordan at figuring out the order of
operations.


# 1.181 20-Jul-2010 matthew

When we're preparing to go to sleep, only call bufq_restart() if other
preparations failed.

ok kettenis@, thib@, deraadt@


# 1.180 19-Jul-2010 deraadt

permit compilation with ACPI_DEBUG


# 1.179 19-Jul-2010 deraadt

there is no need for a seperate dsdt_softc pointer; dsdt does not have a
softc but is just a part of the single acpi driver.
ok jordan


# 1.178 19-Jul-2010 deraadt

acpi_delay() is no longer needed; ok jordan


# 1.177 18-Jul-2010 mlarkin

Clear event status on resume, to avoid going back to sleep/reboot/shutdown
immediately after resume. Tested by a bunch of people on hackers@ on
various hardware. Fixes at least Toshiba NB200 and Gateway LT3103u.

ok marco


# 1.176 13-Jul-2010 deraadt

rename some thread related interfaces so they are logical


# 1.175 10-Jul-2010 jordan

Verify _ADR bdf is correct (some systems return 0xffff)
Works on Dell x9xx and Studio 1555


# 1.174 09-Jul-2010 jordan

Clear the noise


# 1.173 08-Jul-2010 jordan

Add mapping for ACPI device to PCI bdf (match autoconf tree)
Simplify resource parsing function to use buffer argument
Convert namespace linked lists to use queue macros
ok marco@, deraadt@


# 1.172 06-Jul-2010 deraadt

Use spltty() to lock downcalls from apm against the information being
modified by the acpi thread.
ok kettenis


# 1.171 06-Jul-2010 deraadt

Don't use a workq to do the suspend, because it races aginst the acpi
thread. Instead, just tell the acpi thread to do the suspend for us.
This makes apmd & zzz work correctly. While here, have acpithinkpad
attempt to post the event to apm before prompting the suspend itself.
ok kettenis marco mlarkin


# 1.170 05-Jul-2010 mlarkin

Reset register SCI_EN on resume. The ACPI spec clearly states we don't have
to do this, but many machines don't, or forget to, which leads to all sorts
of problems on resume (broken battery stats, overheating, failure to resume
more than once, etc).

tested by many, ok marco@, deraadt@


# 1.169 01-Jul-2010 thib

do bufq quiesce again, since vnd/the bufq_done call in
biodone has been fixed.


# 1.168 01-Jul-2010 jordan

Backout recent AML changes, breaks on R210 and others


# 1.167 01-Jul-2010 jordan

Simplified aml_parse_resource call, cleanup code
ok marco


# 1.166 01-Jul-2010 jordan

Fix subordinate bus # for multi-root PCI buses
ok marco, kettenis


# 1.165 30-Jun-2010 thib

Disable/partially backout the bufq quiesce changes as this
is causing havoc with vnds and release must be buildable.


# 1.164 29-Jun-2010 deraadt

Fix ramdisks (new code was placed inside a SMALL_KERNEL chunk)


# 1.163 29-Jun-2010 jordan

Add support for mapping ACPI to PCI devices
ok kettenis, deraadt


# 1.162 29-Jun-2010 kettenis

Call bufq_quiesce() on suspend to stop all I/O and bufq_restart on resume.
Makes suspend/resume work much better while doing disk I/O.

ok mlarkin@


# 1.161 29-Jun-2010 tedu

60 is a more reasonable guess for number of minutes in an hour than 100.
spotted by TeXitoi. ok deraadt


# 1.160 27-Jun-2010 jordan

Add _acpi_matchhids routine, precursor to pci device match


# 1.159 27-Jun-2010 mlarkin

Clear ACPI fixed event status on resume (power buttons/etc) so that some
machines don't immediately resume after suspending to S3.

Tested by kettenis@ and I on 5 or 6 machines, deraadt@ "yeah, looks good"


# 1.158 27-Jun-2010 jordan

Cleaned up acpi_maptable, adds to table list directly


# 1.157 07-Apr-2010 deraadt

If we have done a wsdisplay_suspend(), and ACPI decides it does not want to
suspend, we must roll back by calling wsdisplay_resume() or the virtual
console switching will be locked forever
ok oga


# 1.156 07-Apr-2010 kettenis

Make suspend/resume work on MP machines (running an MP kernel). Joint work
from mlarkin@ and me, with some amd64 fixes thrown in by deraadt@

ok marco@, deraadt@, pirofti@, mlarkin@


# 1.155 31-Mar-2010 kettenis

Bring /dev/apm support in line with apm(4). Make the suspend button send
a suspend request event to apmd(8) instead of suspending immediately. Also
keep track of whether /dev/apm and /dev/apmctl are currently open, such that
we can still suspend immediately if apmd(8) isn't running.

ok deraadt@, marco@, pirofti@, jsing@, oga@


# 1.154 30-Mar-2010 oga

Prevent the apmd/x races for good.

When we hit suspend time, go through all wsdisplays on the system. if
they are in mode MAPPED, but not MODE_DUMBFB then if possible do a full
vt switch to a !mapped vt, and prevent switching back until resume time.
This has to be called from MD code because this involves userland
running so that X can run the vt switch signal handler. This way, any
case where we are using the "poke registers from userland" model, we
will not be on the hardware when we go down, so the kernel can actually
handle thing properly.

Tested on several acpi laptops (by kettenis@ and ian@), x40 (me and
beck@ at LEAST) and zaurus (me). Maybe others, but if so I forgot who at
this time..

Idea from deraadt somewhere over the Faroe Islands (I thought of a
similar thing myself a while ago). Much prompting from him. Ok and
comments miod@


# 1.153 25-Mar-2010 oga

Fix & vs. && in the kqfilter code.

Based on a comparison with the apm code.

ok deraadt@, kettenis@


Revision tags: OPENBSD_4_7_BASE
# 1.152 26-Nov-2009 mlarkin

Visual (and on some machines, audio) feedback while the machine is
resuming via calls to aml _SST method. This also makes the "moon" LED on
thinkpads to blink during resume and cease when resume is completed.

ok pirofti@


# 1.151 26-Nov-2009 deraadt

Toggle into "cold" when we we disable interrupts, because tsleeping
in the AML interpreter and context switching into userland processes
tends to sometimes pop the ACPI trampoline page out of our address
space. Makes lots more machines work.
ok kettenis mlarkin


# 1.150 24-Nov-2009 jsg

in the resume path evaluate _WAK before dealing with device drivers
like the spec says, not after.

ok mlarkin@


# 1.149 24-Nov-2009 deraadt

abort suspends on SMP systems until that gets written; ok mlarkin


# 1.148 23-Nov-2009 mlarkin

pci should return the result of it's children's suspend/resume calls back
to its parent.

handle suspend failure case and unwind if devices have an activate function
that returns failure.

ok deraadt@, kettenis@


# 1.147 23-Nov-2009 mlarkin

acpi* devices need to call their children on suspend/resume.
ok deraadt@


# 1.146 23-Nov-2009 pirofti

Remove ACPI_SLEEP_ENABLED checks.

This enables by default the suspend/resume paths in the kernel.

Okay deraadt@.


# 1.145 23-Nov-2009 deraadt

knf and spacing that are super annoying


# 1.144 23-Nov-2009 mlarkin

Hook up Theo's new children suspend/resume function, as well as more ACPI
suspend/resume glue.

ok deraadt@


# 1.143 26-Oct-2009 deraadt

Do not do strncmp comparisons on dv_xname because longer device names which
look similar could arrive in the future. Instead, compare directly against
dv_cfdata->cf_driver->cd_name
Issue originally spotted by miod


# 1.142 26-Oct-2009 jordan

Verify checksum+address when loading ACPI tables.
Some systems had invalid entries in RSDT/XSDT.
ok marco@


# 1.141 23-Jul-2009 cnst

New aibs(4) driver for ASUSTeK AI Booster (ACPI ATK0110) hardware monitoring,
with sensor state support through limits provided by the ACPI.

Tested on several ASUS motherboards kindly networked by Sam Fourman Jr.

ok deraadt marco jordan


Revision tags: OPENBSD_4_6_BASE
# 1.140 03-Jun-2009 pirofti

Import acpi power resource driver. This is needed for suspend/resume logic.

Okay jordan@ and marco@.


# 1.139 03-Jun-2009 pirofti

Add acpivideo support. This adds brightness support for all laptops
except thinkpads, they will use the acpithinkpad driver. The driver is
also hooked into wsconsole. So brightness can be adjusted via:

$ wsconsctl display.brightness=<percentage>

This is very helpfull on some laptops that have a nasty bios and get two
steps instead of one when pressing the brightness button.

Tested on various dell, fujitsu, acer, samsung and other laptops.

Okay marco@, miod@. Suggestions from kettenis@.
Lots of reviews and help from miod@, thanks!


# 1.138 03-Jun-2009 jordan

Added ejectable bay for IDE cdroms
ok marco@


# 1.137 30-Apr-2009 marco

Oga changed apm ioctls and forgot to grep. Temporary work around by hitting
the sleep button when we get any request. This interface will chnage at
some point.
ok oga


# 1.136 19-Apr-2009 krw

Call acpi_sleep_walk() when powering down just like when entering
any other sleep state. This tells acpi wake devices to not wake up
the box until manual powering up resets their status. Makes my Dell
GX520 stay powered off in response to 'halt -p'.

In snaps for a while.

ok marco@ (several times) deraadt@


# 1.135 17-Apr-2009 pirofti

Add device ranking support for future sleep/wake developments.

okay marco@


# 1.134 11-Mar-2009 jordan

Cleanup.. use aml_evalinteger instead of aml_evalname
ok marco@


# 1.133 10-Mar-2009 jordan

Fix cut/paste error.. call _BFS not _PTS
ok marco@


Revision tags: OPENBSD_4_5_BASE
# 1.132 21-Feb-2009 marco

overzealous cut/paste; found by Vladimir Kirillov <proger@uaoug.org.ua>


# 1.131 20-Feb-2009 marco

Oops didn't mean for Debugger() to remain there.


# 1.130 19-Feb-2009 marco

suspend/resume bits so that we can develop this in tree. This is disabled.
code from mlarkin and me
help from art,toby,jordan and several others
ok jordan, go for it deraadt


# 1.129 10-Feb-2009 jordan

Fix null pointer for shutdown if acpi not enabled
ok marco@


# 1.128 04-Feb-2009 kettenis

Some systems that claim to be ACPI 2.0 apparently don't provide an XSDT
address. Make the code fall back on the RSDT on those systems.

From Joerg Niendorf <p3y80@internode.on.net>. Fixes PR 5986.


# 1.127 17-Jan-2009 krw

A couple of stray malloc/memset -> malloc(M_ZERO), a bit of knf and
lose a couple of unnecessary casts. No functional change.

Feedback from kettenis@

ok kettenis@ marco@


# 1.126 06-Nov-2008 marco

Collapse all sanity checks of hids into one function.

Originaly from jordan


Revision tags: OPENBSD_4_4_BASE
# 1.125 02-Jul-2008 fgsch

add acpivideo. for now almost an empty skel so others can work on
suspend/resume. eventually it will also manage output switching and
brightness where supported. prodded by marco@


# 1.124 11-Jun-2008 marco

Fix $OpenBSD$ for once and for all


# 1.123 08-Jun-2008 deraadt

aaa_dev was being set from an uninitialized variable; ok toby


# 1.122 07-Jun-2008 marco

Kill ACPIVERBOSE. It was never used and is worthless to boot.

comments jmc
ok toby


# 1.121 06-Jun-2008 marco

Shrink acpi some more for floppies and while here re-enable acpi on the
floppies. Fixes crazy sony vaio kettenis has.

tested by deraadt
ok deraadt jordan kettenis


# 1.120 01-Jun-2008 marco

Fix \n nit in SMALL_KERNEL.


# 1.119 01-Jun-2008 marco

Re-enable new parser. Lots of testing by various folks. Fits all media
now too.

ok kettenis deraadt


# 1.118 16-May-2008 dlg

back out jordans new parser.

it didnt have enough oks from the right people, it did break machines
that were previously working, and it bloated the RAMDISKS and install
media beyond the constraints of the media.

requested by deraadt@


# 1.117 14-May-2008 jordan

Adding new ACPI Parser code
Old guts of code still exists, needs to be torn out next
ok marco@
ok brad@


# 1.116 27-Apr-2008 jcs

add acpithinkpad(4), an acpi support driver for ibm/lenovo
thinkpads.

provides functionality that the bios did on older, non-acpi
thinkpads like respond to screen brightness keys and toggle the
bluetooth device on and off.

ok deraadt@ and marco@


# 1.115 24-Apr-2008 jsing

Introduce acpiasus(4), a driver for the ACPI based hotkeys found in many
ASUS laptops (including the ASUS EeePC) - largely based on NetBSD's
asus(4) driver. On the ASUS EeePC this allows us to enable/disable
wireless, change screen brightness and use the volume keys.

ok jsg@, weingart@


# 1.114 12-Apr-2008 kettenis

Get rid of acpi_s5 global variables; simply send SIGUSR2 instead of SUGUSR1
to tell init(8) to power down the machine.

ok krw@


Revision tags: OPENBSD_4_3_BASE
# 1.113 21-Jan-2008 jsg

Check against RESET_REG_SUP in FADT before doing the acpi reset dance
after all, not doing so appears to break more machines than it fixes.

ok marco@


# 1.112 05-Jan-2008 kettenis

Never attach more acpicpu(4) devices than cpu(4) devices. Always attach
the first acpicpu(4) device on non-MULTIPROCESSOR kernels, regardless of its
ACPI processor ID.

ok mikeb@


# 1.111 05-Dec-2007 deraadt

Hang acpi(4) below bios(4) instead of mainbus(4). This lets us move acpi(4)
before pcibios(4), and then.... if acpi(4) attaches, skip pcibios(4) since
it messes the machines up. Fixes claudio's HP dl320 G5, and almost assuredly
others.
ok kettenis


# 1.110 05-Dec-2007 deraadt

repair acpiopen/acpiclose and friends to handle the /dev/apmctl interface,
so that apmd(8) can actually work correctly against acpi(4), and thus,
even do the fancy -C/-A stuff; also tested by jmc


# 1.109 02-Dec-2007 fgsch

fix the trailing space for real now.


# 1.108 02-Dec-2007 fgsch

avoid trailing spaces on output; marco@ ok.


# 1.107 27-Nov-2007 kettenis

Fix off-by-one that made us attach acpicpu0 and acpicpu1 on GENERIC kernels.

ok marco@


# 1.106 25-Nov-2007 jsg

When available and ACPI is enabled use the ACPI reset method
to reboot. This is required for some quirky sis machines
and likely others to reboot.

Tested by many. 'commit early commit often' deraadt@


# 1.105 19-Nov-2007 deraadt

sigh, amd64 still uses that stupid acpi_enabled variable...


# 1.104 19-Nov-2007 deraadt

softc's come initialized to 0, you know...


# 1.103 19-Nov-2007 deraadt

store revision; from jsg


# 1.102 19-Nov-2007 kettenis

Initialize sc_facs earlier.

ok deraadt@, beck@, canacar@


# 1.101 16-Nov-2007 deraadt

fix the bus_space #define nightmare, so that amd64 and i386 are much more
uniform. as a result shared code like acpi needs less #ifdef's
ok marco kettenis


# 1.100 15-Nov-2007 deraadt

remove the #ifdef ACPI_ENABLE. the EC codepath is either safe enough (and
will be improved), or it is unsafe because AML assumes it must be there
ok almost everyone


# 1.99 15-Nov-2007 mikeb

Make acpicpu(4) attach to the existing CPUs only.

Problem reported by form@. Fix idea by kettenis@.
Lots of help from deraadt@. Tested by deraadt@, form@ and me.

ok deraadt marco


# 1.98 14-Nov-2007 deraadt

spacing


# 1.97 12-Nov-2007 deraadt

correct : vs , in attach-time messages


# 1.96 06-Nov-2007 deraadt

all ACPI_ENABLE kernels need acpi_interrupt()


# 1.95 06-Nov-2007 deraadt

move acpi_read_pmreg() and acpi_write_pmreg() out of SMALL_KERNEL so
that acpi ramdisks can compile (obvious solution)


# 1.94 05-Nov-2007 canacar

Establish the acpi interrupt earlier. Fixes problems with some
GENERIC.MP kernels not getting acpi interrupts. This is a
workaround until the real issue with late establishment is fixed.
ok weingart@, beck@, gwk@


# 1.93 03-Nov-2007 jordan

Added support for displaying wakeup devices
ok beck@,weingart@,gwk@


# 1.92 11-Oct-2007 kettenis

When walking the tree yo find _PRT methods, check whether the device is
actually enabled and functioning. Similar to what we do for _INI.

ok weingart@, beck@


# 1.91 08-Oct-2007 krw

More simple memset(,0,) -> M_ZERO changes. In this batch move to
size(*p) as the first malloc() parameter where p is declared locally
and thus easy to check. Add M_ZERO to gpe_table allocation in acpi.c
even though there is no obvious bzero or memset nearby.


# 1.90 13-Sep-2007 mk

Remove unnecessary memset() (see aml_evalnode(9)) and a bit of KNF.

ok toby


# 1.89 13-Sep-2007 weingart

Implement ACPI 6.5.1 spec tree walk for _STA and _INI.
Fixes mk's laptop. No regressions so far (thank you to the
testers).

ok gwk@, mk@, marco@


Revision tags: OPENBSD_4_2_BASE
# 1.88 17-Apr-2007 mk

Now that acpidock no longer hangs machines during boot (at least
according to the reports I've been getting) it should be safe to enable
it again, so remove the #if 0 stuff around it.

ok marco


# 1.87 11-Apr-2007 jordan

Added changes for C-State
ok marco@


# 1.86 26-Mar-2007 marco

Calculate minutes left more accurately.

From giovanni <qgiovanni@gmail.com>


Revision tags: OPENBSD_4_1_BASE
# 1.85 03-Mar-2007 mk

Stop looking for acpidock until we fix that stuff that breaks all pre-60
series ThinkPads with acpidock(4) enabled.


# 1.84 22-Feb-2007 marco

We were getting no interrupts in .MP. There are 2 fixes; one in
acpi_machdep.c and move the interrupt establishment down after ioapic
has been initialized.

ok jordan


# 1.83 21-Feb-2007 kettenis

Make an acpi-enabled GENERIC.MP work on systems that only support PIC mode.

ok marco@


# 1.82 19-Feb-2007 jordan

Added acpi_map_address function for mapping GAS/IO registers
ok marco@


# 1.81 18-Feb-2007 jordan

Added dynamic perfect hash for opcodes
ok marco@


# 1.80 18-Feb-2007 marco

Shave off another K pre compression.
Shuffle SMALL_KERNEL functions around so that we only have a single #ifndef


# 1.79 17-Feb-2007 marco

Add SMALL_KERNEL throught acpi to make it fit on boot media.
ok deraadt


# 1.78 13-Feb-2007 marco

Add smarter _INI code. This helps with some dissapearing devices.

ok kettenis jordan


# 1.77 31-Jan-2007 gwk

Introduce acpi_hasprocfvs thats "ACPI Has Processor Frequency and Voltage
Scalling" a global flag set by acicpu attach to indicate that ACPI is
handling fvs through the _PSS objects. This will fix disappearing
EST/powernow when booting kernels with acpi enabled, as acpicpu isnt
compiled into GENERIC yet. Also in cases where acpicpu is built in and
legacy EST/powernow work but acpi is missing the _PSS object, the legacy
routines will be used.

tested by dim@, and Nick Nauwelaerts <nick AT nauwelaerts.net>
ok marco@, dim@


# 1.76 22-Jan-2007 mk

Actually look for docking stations by looking for _DCK nodes.

ok marco


# 1.75 02-Jan-2007 marco

On some systems EC must be attached as the first device. Move EC from
generic _HID devices to its own routine to force first attach. Bug found
and debugged by joris@

Code mostly written by joris@


# 1.74 26-Dec-2006 marco

Add polling to devices that require it such as acpibat and acpitz.
Use the same tsleep/wakeup mechanism as the interrupt code to keep it non-
concurrent.


# 1.73 21-Dec-2006 deraadt

even more knf love


# 1.72 21-Dec-2006 marco

Remove some leftover acpiec gpe/interrupt code.


# 1.71 20-Dec-2006 deraadt

oops, name already used


# 1.70 20-Dec-2006 deraadt

rename __acpi_enable_gpe(); ok marco


# 1.69 20-Dec-2006 deraadt

acpi_unmap_pmregs() is not used, ok marco


# 1.68 19-Dec-2006 deraadt

simplify some low-level methods substantially (shrinking code); ok marco jordan


# 1.67 06-Dec-2006 mk

malloc(..., M_NOWAIT) with no NULL check is bad. If allocation fails,
complain, clean up, and bail out of attach.

input and ok gwk


# 1.66 28-Nov-2006 jordan

Fixed clearing arg for acpi_init_pic
ok kettenis@


# 1.65 27-Nov-2006 kettenis

Don't spam dmesg.


# 1.64 27-Nov-2006 jordan

Fixes for acpiprt routing
Post-parse fixup for namerefs
ok kettenis@


# 1.63 25-Nov-2006 marco

Don't handle acpiev events inside the gpe handler since this causes deadlocks.

ok jordan@


# 1.62 15-Nov-2006 kettenis

Hook up acpi apic interrupt routing support to generic acpi code.

ok jordan@


# 1.61 02-Nov-2006 marco

Kill acpiec attach function and add its functionality to the generic _HID
device attach

prodded and ok jordan


# 1.60 02-Nov-2006 marco

Fix comments to reflect reality.


# 1.59 19-Oct-2006 marco

Replace ACPI ioctls with APM ioctls. This makes ACPI talk the same protocol
as APM therefore userspace can now retrieve power information in the same
fashion as all other apm arches (zaurus, macppc, i386 etc).

To use force the use of acpi device like so: apmd -f /dev/acpi

ok jordan@


# 1.58 19-Oct-2006 jordan

Fixed GPE debug levels
Fixed ref counter on temporary bufferfields
DSDT parsing fully working on IBM Thinkpad
ok marco@


# 1.57 15-Oct-2006 jordan

Removed old GPE handler code remnants
ok marco@


# 1.56 12-Oct-2006 jordan

Massive ACPI changes
Fixed ACPI leaks
new ACPI API
ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.55 30-Jun-2006 gwk

KNF, No functional change,
"go at it" marco@


# 1.54 30-Jun-2006 jordan

Support for calling Embedded Controller via new GPE interface
ok marco@


# 1.53 30-Jun-2006 jordan

Added new code for GPE handling
Also works with ACPIEC; need to fixup acpiec.c to remove original interrupt handler
ok marco@


# 1.52 30-Jun-2006 jordan

Added offset argument for acpi read/write pmreg routines (new GPE code)
ok marco@


# 1.51 31-May-2006 todd

do not call acpi_powerdown() unless acpi has successfully initialized
ok gwk@ canacar@


# 1.50 29-May-2006 canacar

Add embedded controller support.
ok marco@ deraadt@ testing & ok gwk@


# 1.49 19-May-2006 canacar

Minimal thermal zone driver. Monitors thermal zone temperature,
shuts down the system if the 'critical temperature' is reached.
ok marco@


# 1.48 11-Apr-2006 marco

KNF.


# 1.47 11-Apr-2006 gwk

Add acpi_resume(struct acpi_softc *sc), which calls some AML methods,
detected at attach time, currently unused.

ok marco@


# 1.46 11-Apr-2006 gwk

Discover some ACPI sleep related methods, and call them at the
appropriate time in acpi_enter_sleep_state().

ok marco@


Revision tags: OPENBSD_3_9_BASE
# 1.45 26-Feb-2006 marco

Add acpicpu as a device.

ok jordan@


# 1.44 23-Feb-2006 marco

remove dead function.

ok jordan@


# 1.43 22-Feb-2006 marco

Print out all tables it found during autoconf. We'll need this data in
the future.


# 1.42 22-Feb-2006 marco

Make dmesg less verbose.

ok jordan.


# 1.41 22-Feb-2006 jordan

Added parsing of SSDT tables
ok marco@


# 1.40 22-Feb-2006 jordan

Added new aml_register to notify by device id
Moved powerdown code to button device
ok marco@


# 1.39 21-Feb-2006 marco

Readjust debug levels that crept in too low over the last few days.
KNF


# 1.38 21-Feb-2006 marco

Rig the power button so that the machine shuts down correctly.


# 1.37 20-Feb-2006 marco

Add acpibtn device. Remains disabled for now.


# 1.36 19-Feb-2006 jordan

Added code for aml_notify GPE notification callback
ok marco@


# 1.35 19-Feb-2006 grange

Fix acpi_enter_sleep_state() by replacing current ugly hack
with mostly correct sleeping state transition code.

ok brad@ marco@


# 1.34 19-Feb-2006 marco

jordan:
Enable interrupts.
Added peripheral code to make this work.

marco:
Replace softintr and other types of interrupts with a handler with context.
Fixed interrupt magic for GPEs.
Some KNF

This is work done by jordan and I but has to be a single commit.


# 1.33 17-Feb-2006 marco

Remove earlier introduced delays. We really only need them during autconf.

ok jordan@


# 1.32 17-Feb-2006 marco

Add a generic delay function as per the ACPI spec.

from jordan@


# 1.31 17-Feb-2006 marco

Use tsleep instead of delay when !cold.


# 1.30 17-Feb-2006 marco

Argh, we need delays between reads/writes. The backend reads (i2c most likely)
are so slow that values come back as 0 sometimes. We have to do this
despite the spec saying we shouldnt have to. Guess having a faster
parser than others can hurt sometimes.

Idea from jordan.


# 1.29 17-Feb-2006 jordan

Fixed code for handling _INI
Rearranged debug statements
ok marco@


# 1.28 16-Feb-2006 jordan

Added stub for calling _INI
Fixed aml_dowhile for NULL return
ok marco@


# 1.27 16-Feb-2006 jordan

Fixed parser/evaluator to retrieve battery info+state
Cleanup of parser
Dell laptops return battery status
ok marco@


# 1.26 06-Feb-2006 brad

fix a typo so this will compile on amd64.


# 1.25 04-Feb-2006 marco

When ACPI_ENABLED wasn't set but the devices were it had an unknown variable.

Pointed out by ian@.


# 1.24 03-Feb-2006 jordan

Rewrote aml parser yet again...
Fixed bus_space_map for i386/amd64
Added _BST and _BIF to acpibat to get status/info
ok marco@


# 1.23 20-Jan-2006 jordan

More evaluator changes
Removed old evaluator code
Fixed _HID value
ok marco@


# 1.22 19-Jan-2006 jordan

Added support for read/write of PCI Config space
Help jason@
ok marco@


# 1.21 18-Jan-2006 jordan

Added new evaluation routines
Added helper methods for buffer/field evaluation
Fixed broken indentation on dsdt.c

ok marco@


# 1.20 17-Jan-2006 jordan

Major overhaul of the aml parser. Evaluater temporarily disabled.

ok marco@


# 1.19 06-Jan-2006 grange

Check for ACPI control availability only if ACPI_ENABLE defined.


# 1.18 06-Jan-2006 grange

Informative error messages on attach, prettier dmesg.


# 1.17 06-Jan-2006 grange

printf -> dnprintf conversion for debug messages.


# 1.16 05-Jan-2006 grange

Find SLP_TYPx values and store them for future use in
acpi_enter_sleep_state().


# 1.15 05-Jan-2006 grange

Random KNF and cleanup


# 1.14 05-Jan-2006 grange

Provide symbolic names for the acpi sleeping states and use it.


# 1.13 28-Dec-2005 marco

Moved DEVNAME to a acpidev.h

ok jordan@


# 1.12 18-Dec-2005 sturm

add back comment about wrong code

ok marco


# 1.11 16-Dec-2005 marco

Remove test for kbd_reset. Prompted deraadt@, ok jordan@


# 1.10 16-Dec-2005 jordan

Added hooks for powerdown/power button press


# 1.9 16-Dec-2005 jordan

Cleaned up common power management register i/o
Added enter sleep state ioctl
Hooked power button interrupt
ok marco@


# 1.8 16-Dec-2005 marco

Remove autoconf chatter


# 1.7 16-Dec-2005 jordan

Moved DSDT AML parsing into parent ACPI driver
ok marco@


# 1.6 14-Dec-2005 marco

Add AC device glue.


# 1.5 14-Dec-2005 marco

Adjust debug printing levels to something more sane.


# 1.4 13-Dec-2005 marco

Add glue to attach devices to apci.


# 1.3 07-Dec-2005 marco

KNF


Revision tags: OPENBSD_3_8_BASE
# 1.2 10-Jul-2005 grange

Spacing cleanup.


# 1.1 02-Jun-2005 tholo

Start on a basic ACPI framework -- does not do much more than read out the
ACPI tables into kernel memory and attach ACPI and HPET timers currently.

In order to test this code, enabling the devices in GENERIC as well as
the ACPI_ENABLE option is needed. This code does not do any thermal
control yet, so this should be done with care depending on the platform.

In the tree so more people can contribute to making this more fully
featured.

Ok niklas@ grange@ tedu@


# 1.413 17-Feb-2022 jsg

reduce includes


# 1.412 10-Feb-2022 visa

Embed klist head in acpi_softc to avoid explicit malloc.

OK kettenis@


# 1.411 09-Feb-2022 deraadt

Move x86-only stubs used by kern/subr_suspend.c into dev/acpi/acpi_x86.c
for sharing between i386 and amd64.
ok mlarkin kettenis


# 1.410 08-Feb-2022 deraadt

The suspend/resume code is a sticky mess of MI, MD, and ACPI sequencing.
This splits out the MI sequencing, backing it with per-architecture helper
functions. Further steps will be neccesary because ACPI and MD are too
tightly coupled, but soon we'll be able to use this code for more architectures
(which depends on figuring out the lowest-level cpu sleeping method)
ok kettenis


# 1.409 04-Feb-2022 robert

set the APM_BATT_CHARGING state if the battery is being charged and try
to calculate the remaining time to be fully charged

ok jca@


# 1.408 02-Feb-2022 deraadt

acpi_addtask() calls malloc() w/ M_NOWAIT (because some calls come from
interrupt context), this however means occasional resource shortage will
result in callbacks registration failing, and unknown consequences for
the task-submitting caller.
Changing this to use pools with a low water mark, decreases the odds
of that problem occuring.
ok kettenis


# 1.407 01-Feb-2022 deraadt

GPE_DIRECT is calling the AML parser from interrupt context, which is
not permitted. Luckily nothing is using GPE_DIRECT anymore, so this code
can be deleted.
ok kettenis


# 1.406 26-Jan-2022 kettenis

An ACPI device needs to be both present and enabled for it to function.
So only attempt to attach hardware that has both bits enabled. This fixes
an issue where com(4) would attach for a disabled serial port leading to
misdetection of the hardware variant and a subsequent hang when /etc/rc
runs ttyflags -a.

ok anton@, deraadt@


# 1.405 12-Jan-2022 patrick

Make acpi_getpropint() return uint64_t, as ACPI integers are in fact that
wide and some _DSD properties depend on it.

ok kettenis@


# 1.404 04-Jan-2022 patrick

acpi_getprop() needs to actually make sure that we're looking at the
correct property. While there adjust acpi_getpropint() as well to
increase similarity with acpi_getprop().

ok kettenis@


# 1.403 16-Dec-2021 anton

Attach com over acpi on amd64. Some hardware uses a different interrupt
assignment compared to the the legacy one supported by com over isa.
This causes the console to halt once userland takes over as no
interrupts are received. The actual address and irq can be read from
ACPI, kettenis@ already added support for arm64 which paved the way for
amd64.

Some consoles that previously attached over isa are now expected to
attach over acpi.

Thanks to patrick@ for testing on arm64.

ok kettenis@


# 1.402 07-Dec-2021 kettenis

Fix parsing of SR_IRQ resource descriptors.

ok patrick@, anton@


# 1.401 02-Nov-2021 deraadt

knf


# 1.400 30-Oct-2021 deraadt

Change hw.perfpolicy=auto by default, at startup. If the system has AC
power connected (default is yes when no driver differentiates) then default
to 100% performance. On battery, use the existing auto algorithm (which is
admittedly somewhat unrefined).
This change overrides the system/BIOS speed and puts OpenBSD in control.
As this happens very early during boot, besides speedups in all usage usage
patterns, some surprises: unhibernate and sysupgrade times are cut in half.
note: on a few architectures, the setperf fn pointer is changed late, and
thus the auto algorithm stops timeing out. kettenis and i will look for
a solution.
in snaps for more than a week.
ok kettenis


Revision tags: OPENBSD_7_0_BASE
# 1.399 20-Jul-2021 mlarkin

Fix trailing whitespace in a few places.

No code change.


# 1.398 09-May-2021 kettenis

Some x86 machines advertise the "hardware reduced" ACPI feature, advertise
S4 and S5 support, but fail to populate the SLEEP_CONTROL_REG and
SLEEP_STATUS_REG descriptions in the FADT. An example of such a machine
is the ASUS Zenbook 14 UM433DA. Any attempt to powerdown the machine
will result in a kernel crash. It turns out that using the legacy ACPI PM
registers works fine on this machien. So fall back on those registers
if SLEEP_CONTROL_REG and/or SLEEP_STATUS_REG aren't provided.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_9_BASE
# 1.397 15-Mar-2021 patrick

Add acpi_iommu_device_map(), which replaces the DMA tag with one that
is blessed with IOMMU magic, if available. This is mainly for arm64,
since on amd64 and i386 the IOMMU only captures PCIe devices, as far
as I know, which uses the pci_probe_device_hook(). This though is for
non-PCI devices attached through ACPI.

ok kettenis@


# 1.396 07-Mar-2021 patrick

Pass standard DMA tag to acpi(4) table drivers.

ok kettenis@


# 1.395 27-Dec-2020 deraadt

acpi_map_address() cannot be wrapped by SMALL_KERNEL anymore, as
it is used by acpihpet.c


# 1.394 25-Dec-2020 visa

Refactor klist insertion and removal

Rename klist_{insert,remove}() to klist_{insert,remove}_locked().
These functions assume that the caller has locked the klist. The current
state of locking remains intact because the kernel lock is still used
with all klists.

Add new functions klist_insert() and klist_remove() that lock the klist
internally. This allows some code simplification.

OK mpi@


# 1.393 17-Dec-2020 kettenis

Fix some issues with referencing named ACPI nodes from Packages.
These references need to be resolved at runtime rather than when
they're parsed such that they pick up the right values for those nodes
which can be changed when for example _INI methods run.

The current approach is to replace these reference with a string that
names the node in question. The problem with that is that packages
can also contain normal strings. Which means that we need special
code that depends on the context where the package is used.

This diff takes a different approach by retaining a reference when
parsing. Code that uses a package will need to resolve this reference
but there is no ambiguiety anymore.

ok patrick@


# 1.392 05-Dec-2020 kettenis

Add support for the _EVT method to the GPIO support code.

ok patrick@


Revision tags: OPENBSD_6_8_BASE
# 1.391 27-Aug-2020 jmatthew

Reorder the acpi attach process so we look at Processor() CPU nodes
after Device() ones, since we should prefer the newer node type.
If we see any Device() nodes, don't attach acpicpu(4) to any Processor()
nodes if they're also present. This also makes acpitz(4) and
acpipwrres(4) devices attach slightly later.

ok kettenis@, also tested by jmc@


# 1.390 16-Aug-2020 gnezdo

Remove sc_maxgpe from acpi_softc which was never read from

Remove a dead conditional from acpi_init_gpes while there.

ok jcs@


# 1.389 21-Jul-2020 deraadt

acpi can use IPL_BIO (a low interrupt) since it only enqueues operations for
later processing. The use of a high interrupt will predate suspend/resume
efforts, we had to redesign acpi to be non-reentrant obviously
discussed with kettenis, in snaps for more than a week


# 1.388 24-Jun-2020 cheloha

kernel: use gettime(9)/getuptime(9) in lieu of time_second(9)/time_uptime(9)

time_second(9) and time_uptime(9) are widely used in the kernel to
quickly get the system UTC or system uptime as a time_t. However,
time_t is 64-bit everywhere, so it is not generally safe to use them
on 32-bit platforms: you have a split-read problem if your hardware
cannot perform atomic 64-bit reads.

This patch replaces time_second(9) with gettime(9), a safer successor
interface, throughout the kernel. Similarly, time_uptime(9) is replaced
with getuptime(9).

There is a performance cost on 32-bit platforms in exchange for
eliminating the split-read problem: instead of two register reads you
now have a lockless read loop to pull the values from the timehands.
This is really not *too* bad in the grand scheme of things, but
compared to what we were doing before it is several times slower.

There is no performance cost on 64-bit (__LP64__) platforms.

With input from visa@, dlg@, and tedu@.

Several bugs squashed by visa@.

ok kettenis@


# 1.387 10-Jun-2020 jca

Give userland a consistent battery and A/C state after resume

Previously we refreshed the state of acpiac(4) and acpibat(4)/acpisbs(4)
by queueing fake ACPI events to be processed by the acpi thread.
acpibat(4) using ACPIDEV_POLL meant that its status could be refreshed
first, resulting in an APM_POWER_CHANGE event sent to userland before
the state of acpiac(4) was up to date. Because of this, apmd(8) could
see A/C unplugged when it was in fact plugged, possibly leading to
a wrong decision.

Instead of using fake ACPI events, refresh the status of acpiac(4) and
acpibat(4)/acpisbs(4) right away at DVACT_WAKEUP time. To reuse the
existing refresh code of those drivers, make their ACPI *_notify()
handlers responsible for sending APM_POWER_CHANGE events instead. Also
queue an APM_POWER_CHANGE event right after resume to retain existing
behavior.

This fixes the consistency of data shown to userland, and makes those
drivers less special.

Initially discussed last year (!) with mpi@ at Miod's place.
Feedback and ok jcs@, deraadt@ agrees with the use of DVACT_WAKEUP here.


# 1.386 29-May-2020 deraadt

dev/rndvar.h no longer has statistical interfaces (removed during various
conversion steps). it only contains kernel prototypes for 4 interfaces,
all of which legitimately belong in sys/systm.h, which are already included
by all enqueue_randomness() users.


# 1.385 14-May-2020 kettenis

Bring back the acpi(4) changes. This time around only use the ACPI _CRS
information on ACPI 5.0 and later.

ok krw@, patrick@


# 1.384 11-May-2020 jca

More accurate remaining power reporting with disparate batteries

Overall remaining power was computed as the average of the remaining
power of each battery, in percents. This doesn't work with batteries of
different capacity, so use the total capacity and remaining power
instead.

Reported by lidstah, successful tests by lidstah and benno@, ok kettenis@


# 1.383 08-May-2020 kettenis

Move parsing of _CRS into acpi(4) and pass the parsed address and interrupt
information in struct acpi_attach_args.

ok mpi@


Revision tags: OPENBSD_6_7_BASE
# 1.382 14-Apr-2020 kettenis

Implement acpi_getprop(), the ACPI equivalent of OF_getprop().

ok patrick@


# 1.381 12-Apr-2020 kettenis

Add support for the _CCA method. This method indicates whether DMA is
cache-coherent or not. To implement this, acpi(4) gets two bus_dma tags
and passes the appropriate one when attaching devices based on _CCA.
On i386/amd64, where for all practical purpose DMA is always cache-coherent,
the two tags are the same. But on arm64 they are distinct.

ok patrick@


# 1.380 07-Apr-2020 visa

Abstract the head of knote lists. This allows extending the lists,
for example, with locking assertions.

OK mpi@, anton@


# 1.379 03-Apr-2020 mpi

Fix incoherencies of errors returned by various *kqfilter routines.

EOPNOTSUPP is returned when there's no "filterops" for a given fd.
EINVAL when the requested filter isn't supported by the "filterops".
ENXIO means the underlying device is no longer valid/present.

ok visa@


# 1.378 20-Feb-2020 visa

Replace field f_isfd with field f_flags in struct filterops to allow
adding more filter properties without cluttering the struct.

OK mpi@, anton@


# 1.377 31-Dec-2019 visa

Use C99 designated initializers with struct filterops. In addition,
make the structs const so that the data are put in .rodata.

OK mpi@, deraadt@, anton@, bluhm@


# 1.376 31-Dec-2019 mpi

Convert infinite sleeps to {m,t}sleep_nsec(9).

ok kn@


# 1.375 31-Dec-2019 jsg

include sys/sensors.h for sensor use
don't depend on indirect inclusion from acpidev.h


Revision tags: OPENBSD_6_6_BASE
# 1.374 07-Sep-2019 kettenis

Revert acpipci(4) changes. Too many systems are broken. We'll try again
after 6.6 as been released. The acpireg.h change stays behind.


# 1.373 28-Aug-2019 kettenis

Use ACPI information to attach PCI busses like we do on arm64. There are a
few additional quirks though, and attaching the PCI busses is delayed to
replicate the existing code more closely. That may be changed in the
future. Also tweak how we handle MSI support and respect to ACPI flag
that says we shouldn't attempt to use MSIs.

Some fallout is expected.

ok patrick@


# 1.372 27-Aug-2019 deraadt

On amd64 the bootup and resume paths for SP/BP/SP were not performing
msr/pat, clock-startup, firmware upgrade, fpu initialization, and cpu
soft-state initialization in the same order. That kind of chaos is
crazy and fragile. Improve the acpi resume call sequence and unify order
of the operations.


# 1.371 02-Jul-2019 jcs

acpi_set_gpehandler: turn edge arg into flags, add support for
passing GPE_DIRECT which directly executes the supplied callback and
clears a level interrupt rather than going through acpi_taskq which
can be slow when other driver callbacks are in the queue.

This should not be used for GPE handlers that need to do anything
ACPI-related.

ok kettenis


# 1.370 10-Jun-2019 kettenis

Print proper ACPI version number.

ok mlarkin@


# 1.369 08-Jun-2019 kettenis

When establishing the mapping between ACPI device nodes and PCI devices,
skip PCI host bridges and devices that aren't there.

ok patrick@


# 1.368 07-Jun-2019 kettenis

Introduce acpi_getsta() and use it to eliminate some duplicated code.

ok patrick@


# 1.367 12-May-2019 kettenis

Make sure the extended PM register descriptions actually exist before
using them. Fixes machines with ancient ACPI 1.0 (such as recent
hypervisors).

ok deraadt@


# 1.366 12-May-2019 kettenis

Add support for mapping ACPI PM registers using the "extended" gasio
representation from the FADT. Mostly fixes the Lanner NCA-1510.

ok lteo@, mlarkin@, deraadt@


# 1.365 11-May-2019 lteo

Fix incorrect error message.

ok kettenis@


# 1.364 08-May-2019 tedu

free size. the allocation is the header plus the length.


Revision tags: OPENBSD_6_5_BASE
# 1.363 04-Apr-2019 kettenis

Avoid potential null-pointer dereference. Found by Kent R. Spillner.

ok deraadt@, jsg@


# 1.362 20-Jan-2019 jsg

fix SMALL_KERNEL build after changes in rev 1.361
reported by naddy@ ok deraadt@


# 1.361 19-Jan-2019 tedu

Add a pwraction sysctl that controls what the power button does on acpi.
By default, nothing changes -- shutdown is initiated. But allows turning
power button into a sleep button if desired.
(grudging) ok from a few parties


# 1.360 26-Oct-2018 kettenis

Add acpipci(4) on amd64. For now this only calls the PCI-specific _OSC
method to let the ACPI implementation know what features we support.


Revision tags: OPENBSD_6_4_BASE
# 1.359 30-Aug-2018 kettenis

Move softbutton check before the check that skips attaching certain acpi
devices. Fixes regression caused by matching on _CID in addition to
matching on _HID.

ok matthieu@, mlarkin@


# 1.358 25-Aug-2018 kettenis

Match ACPI devices based on _CID if no match for _HID is found.

ok mpi@


# 1.357 19-Aug-2018 kettenis

Add support for multiple PCI segments. Only really implemented for arm64
for now as amd64/i386 firmware still caters for legacy OSes that only
support a single PCI segment.

ok patrick@


# 1.356 03-Aug-2018 kettenis

Let ahci(4) match on _CLS instead of _HID when attaching at acpi(4). Avoids
having to add many more _HID entries to the match table.

ok deraadt@, mlarkin@


# 1.355 10-Jul-2018 kettenis

Make legacy interrupts work in acpipci(4).

ok patrick@


# 1.354 05-Jul-2018 kettenis

Add acpipci(4), a driver that supports generic ECAM-compatible PCI host
bridges based on information provided by ACPI.

ok mlarkin@


# 1.353 04-Jul-2018 kettenis

Properly pass around the PCI "chipset tag" in acpi(4) and refactor
acpimcfg(4) to call an MD initialization functions that sets up a tag for
PCI ECAM.

ok guenther@, mlarkin@, krw@


# 1.352 01-Jul-2018 kettenis

Move acpi(4) attach glue into acpi_machdep.c.

ok guenther@, deraadt@


# 1.351 01-Jul-2018 kettenis

Add support for _DSD Device Properties.

ok mlarkin@


# 1.350 30-Jun-2018 kettenis

Add intr_enable() function, intended for MI use to amd64 and i386 and use
this in the acpi(4) suspend/resume code paths.

ok deraadt@


# 1.349 29-Jun-2018 kettenis

Use <stdint.h> types.

ok mpi@, jung@, krw@, deraadt@


# 1.348 29-Jun-2018 mlarkin

ACPI: Allow (with warning) GPE handler reassignment, instead of returning
a failure code.

ok kettenis


# 1.347 26-Jun-2018 mlarkin

knf


# 1.346 26-Jun-2018 kettenis

Let acpi(4) pass down the bus dma tag.

ok deraadt@


# 1.345 25-Jun-2018 kettenis

Reorganize acpi(4) code a bit in preparation for upcoming arm64 support.

ok deraadt@


# 1.344 20-May-2018 kettenis

Wake up the acpi thread after scheduling a gpio event.

ok mpi@


# 1.343 17-May-2018 kettenis

Sprinkle some #ifndef SMALL_KERNEL such that RAMDISK kernels cuild again.

pointed out by stsp@


# 1.342 17-May-2018 kettenis

Implement GenericSerialBus OpRegion support.

ok mlarkin@


# 1.341 27-Mar-2018 kettenis

Add acpicmos(4), a driver that implements SystemCMOS OperationRegion
access support. This fixes machines where the AML doesn't check whether
support for this OperationRegion type has been registered by the OS.

ok mlarkin@


Revision tags: OPENBSD_6_3_BASE
# 1.340 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.339 10-Feb-2018 deraadt

Syncronize filesystems to disk when suspending. Each mountpoint's vnodes
are pushed to disk. Dangling vnodes (unlinked files still in use) and
vnodes undergoing change by long-running syscalls are identified -- and
such filesystems are marked dirty on-disk while we are suspended (in case
power is lost, a fsck will be required). Filesystems without dangling or
busy vnodes are marked clean, resulting in faster boots following
"battery died" circumstances.
Tested by numerous developers, thanks for the feedback.


# 1.338 08-Feb-2018 deraadt

After flushing disks, try again to release all memory. We don't need to
save dirty memory to the hibernate space.


# 1.337 08-Feb-2018 deraadt

Toss all releaseable memory, because fragmentation can get in the way
of allocating the hibernate playpen.


# 1.336 08-Feb-2018 deraadt

sr_quiesce() is a new approach for ensuring that softraid drains
output to the disks.

This is part of a larger suspend/resume filesystem-safety diff, which
has been worked on for a couple of months already. Tests by job, krw,
beck, benno, and others. Sometimes even by snapshot users...


# 1.335 29-Nov-2017 kettenis

Add support for GPIO-signaled events.

ok mlarkin@


# 1.334 16-Nov-2017 jcs

Add PCI attachment for dwiic(4) needed by Intel 100 Series machines

ok kettenis


# 1.333 14-Nov-2017 kettenis

Add a few more PNP IDs to the skip lists.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.332 17-Aug-2017 stsp

Make dwiic(4) attach its ACPI parent dependencies.
This change makes the Asus E200HA keyboard work.

Original analysis and patch by Cesare Gargano
ok kettenis@


# 1.331 09-Aug-2017 dcoppa

Add "PNP0303" (8042 PS/2 Controller) to acpi_isa_hids[]

ok kettenis@


# 1.330 22-Jul-2017 jcs

acpibat: look for _BIX first before falling back on _BIF

ACPI 4.0 deprecated _BIF for battery status so some newer machines
are exporting _BIX which extends _BIF's package with a few extra
fields. When using _BIX, export the new discharge cycle count as a
sensor.

feedback from kettenis and anton
ok deraadt


# 1.329 20-Jul-2017 kettenis

Start a list of isa(4) device names such that we can avoid printing them
as "not configured".

ok mikeb@, millert@, deraadt@


# 1.328 08-Apr-2017 deraadt

Sensors are run as callbacks inside tasks. During suspend a sensor
could be running inside a driver that will be force-detached, or due
to tsleep end up disrupting the softstate/hardstate contract. At
suspend time, quisce all these callbacks by waiting for completion.
This issue has never been observed for real, but may be implicated in
suspend/resume failures.
ok kettenis guenther mlarkin


# 1.327 08-Apr-2017 deraadt

Two prototypes for acpi_maptable; merge to one place


Revision tags: OPENBSD_6_1_BASE
# 1.326 27-Mar-2017 deraadt

Now that hibernate_alloc() only has clean success/failure, don't
need to call hibernate_free() to clean up a partial mess.
ok mlarkin kettenis


# 1.325 13-Mar-2017 deraadt

allow ramdisks to build; ok jcs


# 1.324 12-Mar-2017 jcs

try to attach acpisbs first and if successful, don't attach acpibat


# 1.323 02-Mar-2017 natano

Add a new sysctl machdep.lidaction. The sysctl works as follows:

machdep.lidaction=0 # do nothing
machdep.lidaction=1 # suspend
machdep.lidaction=2 # hibernate

lidsuspend is just an alias for lidaction, so if you change one, the
other one will have the same value. The plan is to remove
machdep.lidsuspend eventually when people have upgraded their
/ets/sysctl.conf.

discussed with deraadt, who came up with the new MIB name
no objections mlarkin
ok stsp halex jcs


# 1.322 28-Feb-2017 natano

Untangle abstract sleep modes from ACPI sleep states. This paves the way
for further work in the area.

idea and ok deraadt
ok mlarkin


# 1.321 22-Feb-2017 jcs

add acpisbs, an acpi smart battery subsystem driver reading data
over smbus

currently disabled because it conflicts with acpibat

ok deraadt, kettenis


# 1.320 14-Jan-2017 kettenis

Revert aml_rdpciaddr changes; breaks several machines that were working
before.


# 1.319 11-Jan-2017 jsg

Increase the size of the buffers used for _HID and _CID strings to be
able to fit "Hyper_V_Gen_Counter_V1".

ok mlarkin@ deraadt@


# 1.318 08-Jan-2017 kettenis

Make aml_rdpciaddr() work for for devices behind bridges. The code now
uses the mapping between AML nodes and PCI devices that we establish.
Because _INIT methods may end up calling aml_rdpciaddr(), make sure we
create that mapping early. Also handle devices that aren't actually present.
These devices are now included in the mapping and reads will return an
all-ones patterm whereas writes are a no-op..

ok mlarkin@


# 1.317 25-Oct-2016 pirofti

Add the resource index to aml_parse_resource() callback function.

This is needed by an upcoming acpiec commit that handles machines breaking
the current ACPI specifications.

Change suggested by and ok kettenis@, guenther@


# 1.316 18-Sep-2016 guenther

If the APIC table (MADT) isn't present then ci_acpi_proc_id won't be set
when acpicpu tries to attach, leading to a NULL dereference. The ACPI
processor object on the Dells where this was reported is bogus and useless
anyway so apply the same test when deciding whether to attach to processor
objects as acpicpu would when matching them to cpus.

Problem reported by Eivind Eide (xenofil (at) gmail.com) and
Johan Huldtgren (johan+openbsd-bugs (at) huldtgren.com).
Testing by Eivind Eide.
ok kettenis@ mlarkin@


# 1.315 03-Sep-2016 naddy

Write the system time back to the RTC every 30 minutes.
This fixes the problem that long-running machines which were not
shut down properly would reboot with a badly offset system time.

hints and ok kettenis@


# 1.314 31-Aug-2016 mlarkin

Don't put a device in the wake device list if _STA says it's not there.

Fixes at least one machine that woke up from sleep / poweroff immediately.

ok kettenis@, tested by kettenis and martijn@


# 1.313 28-Jul-2016 kettenis

Store the acpi processor ID/UID in struct cpu_info, and use it to attach
acpicpu(4) drivers to the right cpu(4).

ok mlarkin@, guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.312 10-Jun-2016 kettenis

The power management timer has been made optional in ACPI 5.0A, so don't
attempt to attach acpitimer(4) if the timer isn't present.

Pointed out by jcs@

ok jcs@


# 1.311 08-May-2016 kettenis

Make sure devices listed by a device's _DEP method are attached before
we attach the device itself. The _DEP methode, introduced in ACPI 5.0,
is a hint that the device in question depends on OpRegion support from
the devices listed by _DEP, so we have to order them the proper way.
To prevent us from attaching those devices again when we encounter them
later walking down the device tree, keep track of our attempts to
attach a device driver to them using aflag in the aml_node structure.

ok guenther@


# 1.310 25-Apr-2016 pirofti

We attach acpihpet(4) and acpiec(4) without using the HID, so do not
report it in the dmesg.

OK deraadt@, mlarkin@.


# 1.309 02-Apr-2016 kettenis

Reduce dmesg spam by knocking out some acpi hardware IDs that don't need a
driver.

ok mpi@


# 1.308 29-Mar-2016 kettenis

Check if a device is present (using _STA) in acpi_foundhid. This prevents
us from reporting devices as "not configured" that aren't actually present,
and allows us to remove duplicated code from several drivers.

ok deraadt@, ok jsg@


# 1.307 29-Mar-2016 kettenis

Implement a cleaner way to prevent acpivout(4) from attaching if we have
a platform-specific method of controlling the backlight.

ok millert@, mlarkin@, jung@


# 1.306 28-Mar-2016 kettenis

Adding each and every new acpi device driver to acpi_foundhid() is
gettinga bit of the burden. And it really isn't how our autoconf
framework is supposed to work for busses that can be enumerated. All
the drivers already check for a matching _HID in their attach
function. So we can just drop the checks in acpi_foundhid() and let
the drivers sort themselves out.

Print a "not configured" line for devices for which we don't attach adriver.
This may be a bit much as there are devices for which we will not have a
driver. This will be removed if it causes too much dmesg spam.

ok deraadt@, mlarkin@


Revision tags: OPENBSD_5_9_BASE
# 1.305 17-Jan-2016 jsg

Take acpi_foundhid() outside of #ifndef SMALL_KERNEL so the ramdisk
kernel can find and attach dwiic(4) and sdhc(4).


# 1.304 17-Jan-2016 jsg

remove an unused global variable


# 1.303 14-Jan-2016 kettenis

Attach dwiic(4) on the Intel Bay Trail i2c controllers as well.


# 1.302 12-Jan-2016 kettenis

Improve emulation of PM registers on Hardware-redcued ACPI by emulating
SPL_TYPx, SLP_EN and WAK_STS using the new SLEEP_STATUS_REG and
SLEEP_CONTROL_REG registers. Makes the transition into S5 (powerdown) work
on the ASUS X205TA.

ok deraadt@, mlarkin@


# 1.301 12-Jan-2016 jcs

Add dwiic, a driver for the Synopsys DesignWare i2c controller found
on the Samsung ATIV Book 9 laptop. This initial version only
supports ACPI config/attachment.

Add ihidev, a HID-over-i2c driver largely based on uhidev. dwiic
handles attaching ihidev devices found in ACPI.

Add ims, a HID-over-i2c mouse/trackpad driver to get basic cursor
and button functionality from HID-compliant i2c trackpads.

ok kettenis deraadt


# 1.300 11-Jan-2016 kettenis

Make sdhc(4) attach to hardware IDs "80860F14" and "PNP0FFF".


# 1.299 09-Jan-2016 kettenis

Initial support for Hardware-Reduced ACPI. This skips the legacy mode checks
by emulating the PM_CNT register such that SCI_EN is always enabled. It also
skip attaching acpitimer(4) since the relevant registers are no longer there.

ok deraadt@, mlarkin@


# 1.298 06-Jan-2016 kettenis

Follow the spec and check the SCI_EN bit to see if ACPI has already
been enabled and don't bother enabling it again if it is. This should fix
systems that don't implement legacy mode and therefore set ACPI_ENABLE and
ACPI_DISABLE to zero.

ok mlarkin@, yasuoka@


# 1.297 23-Nov-2015 reyk

Back out the CRITICAL change, turn it back from 10% to 25%. Some
people with old/broken batteries didn't like this change, and the
underlying heuristics are wrong.


# 1.296 22-Nov-2015 reyk

The "CRITICAL" APM battery level, that is exported/faked by the ACPI
stack, is currently set to 25%. With 25% battery left, a typical
modern laptop will have at least one hour runtime left. It is an
arbitrary value, and other APM power layers in the kernel set CRITICAL
to something like 5% or 10%, so set it to 10% for now.

OK mlarkin@


# 1.295 28-Sep-2015 deraadt

In low-level suspend routines, set cold=2. In tsleep(), use this to
spit out a ddb trace to console. This should allow us to find suspend
or resume routines which break the rules. It depends on the console
output function being non-sleeping.... but that's another codepath which
should try to be safe when cold is set.
ok kettenis


# 1.294 06-Sep-2015 deraadt

clarify malloc arguments (base + length...)


# 1.293 28-Aug-2015 deraadt

fairly simple sizes for free(); ok tedu


# 1.292 20-Aug-2015 kettenis

Respect the access size when reading or writing to pci config space.
This version also makes sure writes are properly aligned, preventing
the panics seen with the previous incarnation of this change.

Fixes battery status passthrough in vmware, brightness keys on my x220
and who knows what else.

ok mlarkin@, deraadt@


Revision tags: OPENBSD_5_8_BASE
# 1.291 04-Aug-2015 deraadt

If we are setting ACPI_PM1_BM_RLD do it only once, and remember to
repeat this action at resume time.
ok mlarkin guenther


# 1.290 29-Jul-2015 deraadt

Something subtle in previous revision is locking up suspend or booting
on some laptops. Mailing lists threads mention x220 and Asus machines.
Reverting for now.


# 1.289 21-Jul-2015 kettenis

iRespect the access size when reading or writing to pci config space.
Fixes battery status passthrough in vmware and who knows what else.

Tested by many.

ok mlarkin@, deraadt@


# 1.288 17-Jul-2015 mlarkin

fix comments


# 1.287 26-Mar-2015 jsg

Use the 0x10 extended capability to determine if the trackstick
buttons are wired to the trackpad and need to be re-routed to the
trackstick. Without this change the buttons on 2015 thinkpads
get picked up as extended buttons that show up as scroll up/down.

Remove the X1 Carbon 2015 (LEN0048) and X250 (LEN0046) from the
top button area/soft buttons quirks list. Also avoid using the
quirk list entirely if the capability bit is set.

Based on work from jcs@ with feedback from mpi@
ok mpi@ jcs@


# 1.286 20-Mar-2015 kettenis

The ACPI standard says that a PCI function number of FFFF can be used to refer
to all functions on a device. This makes no sense in the context of accessing
PCI config space. Nevertheless stsp@ has a machine with AML that tries to
do config space reads from a device where _ADR encodes function number FFFF.
This is almost certainly buggy AML, but since Linux (and presumably Windows)
boot just fine on this machine we need to work around this. Linux effectively
treats function FFFF as function 7, which isn't present on this machine.
Instead we simulate a read from a non-existent device by returning a bit
pattern of all ones. Writes to function FFFF will still panic.

ok mlarkin@


# 1.285 16-Mar-2015 deraadt

Reduce use of SMALL_KERNEL in acpi, bringing acpiec driver and more gpe
handling into RAMDISK. This is now possible because the install media
has ample room. The goal is to reduce special cases where we may be
skipping (unknown) important operations...
ok mlarkin kettenis


# 1.284 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.283 07-Feb-2015 deraadt

New framework that allows hibernate to pass in entropy from it's fresh
boot.
ok mlarkin


# 1.282 06-Feb-2015 mlarkin

Fix a hibernate crash on some machines due to unmapping a page that
may not have been mapped previously (in the failure to hibernate case).

Also ensure that the lowmem ptp is mapped in all cases (not just MP).

ok kettenis


# 1.281 17-Jan-2015 deraadt

resume_randomness() before spinning up other cpus...


# 1.280 15-Jan-2015 jsg

Define a new wscons mouse type for Synaptics clickpad devices that lack
physical buttons. This will be used if the acpi pnpid for the mouse
matches a list provided by Synaptics found in Linux.

Combined with the xenocara changes this will remove the need for an xorg
config file for the x240/t440/t540 generation of thinkpads.

ok matthieu@ shadchin@ kettenis@


# 1.279 11-Jan-2015 kettenis

Tolerate checksum errors in ACPI tables (but complain about them). Apparently
there are machines out there with broken BIOSen with such checksum errors in
thier RSDT tables. Windows runs fine on these machines, so it probably does
not check. Linux ignores checksum errors as well (and prints a warning).

ok deraadt@, krw@


# 1.278 18-Dec-2014 deraadt

suspend and resume the RNG (at what looks like the right spots)
ok reyk djm


# 1.277 09-Dec-2014 doug

Replace some malloc(n*size,...) calls with mallocarray().

ok tedu@ deraadt@


# 1.276 06-Dec-2014 mpi

If "machdep.lidsuspend" is not set, do not go back to sleep if the lid
is still closed when resuming. This allows docked laptop to be resumed
without opening them.

ok mlarkin@


# 1.275 23-Nov-2014 mlarkin

If a machine has no _LIDs, then none can be open. This diff fixes a problem
where machines without any _LIDs were going back to sleep after hibernate
due to the recent suspend-if-lid-closed-on-resume diff. Noticed on qemu,
which doesn't have any _LIDs.

discussed with deraadt


# 1.274 18-Nov-2014 krw

Nuke yet more obvious #include duplications.

ok miod@


# 1.273 08-Nov-2014 mlarkin

If resuming from sleep (zzz/ZZZ) and the lid is still closed, go back to
sleep. This prevents accidental lid flex or slight opening in a backpack
from waking the machine up and leaving it resumed (powered on).

ok deraadt@


# 1.272 26-Sep-2014 kettenis

Rework piglet and pig allocation. Currently the piglet gets allocated
deep down in the suspend path, where it is really hard to recover from
allocation failure. So allocate the piglet early on in the suspend path.
Also change the piglet and piglet allocation functions to use km_alloc(9)
instead of doing pmemrange magic. This removes a bunch of code which, in the
case of the piglet allocation, is broken since it results in a NULL pointer
dereference. Also switch the piglet allocation to not wait. If we can't
allocate 16MB of phys contig memory on a halfway modern machine we're almost
certainly under a lot of memory pressure and we're better off not trying to
hibernate anyway.

ok mlarkin@


# 1.271 25-Sep-2014 kettenis

Bring back rev 1.249. Now that mp_setperf() has been fixed, it should be
safe to restore perflevel when resuming. Should fix the issues people
see with the processor running at full speed heating up the machine while
the apm command reports that the CPUs are running at a low speed.

ok dcoppa@, deraadt@


# 1.270 23-Sep-2014 kettenis

Release the acpi lock around calls to wsdisplay_suspend() and
wsdisplay_resume(). The X server may adjust the display brightness just
before/after the VT switch when suspending/resuming. In fact, it may
already be blocked adjusting the display brightness for some reason
unrelated to the suspend request.

The VT switch request will eventually time out, but the delay is annoying
and the failed VT switch will lead to problems after resume.

Needed for a future update of the xf86-video-intel driver.

ok mlarkin@


# 1.269 19-Sep-2014 kettenis

Use config_suspend_all(9).


# 1.268 08-Sep-2014 kettenis

Clear the PME Status bit on PCI devices in response to Device Wake events.
Fixes the interrupt storm on the 2009 Mac mini with WOL enabled on its
nfe(4) interface.

ok mlarkin@


Revision tags: OPENBSD_5_6_BASE
# 1.267 20-Jul-2014 mlarkin

Support hibernating to softraid crypto volumes.

much help and ok from deraadt@


# 1.266 16-Jul-2014 mlarkin

Reenable hibernate RLE support and flush+zero all memory after unpack.


# 1.265 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.264 12-Jul-2014 kettenis

Add a function to drop all clean pages on the page daemon queues and call
it when we hibernate.

ok mlarkin@, miod@, deraadt@


# 1.263 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.262 11-Jul-2014 mlarkin

Flush the buffercache to 16MB on hibernate and restore its previous max
size (kern.bufcachepercent) on resume, for better hibernate performance.

ok beck@


# 1.261 10-Jul-2014 blambert

remove unneeded header

ok mlarkin@


# 1.260 01-Jul-2014 kettenis

Adding an additonal delay after each access to SystemMemory or SystemIO
during autoconf makes no sense.

ok deraadt@


# 1.259 23-Jun-2014 kettenis

SystemMemory is used to access memory mapped registers on some machines, so we
must use the bus_space(9) API to access it instead of memcpy(9). Also make
sure we properly align access to fields that need more than byte alignment.

ok guenther@


# 1.258 21-May-2014 mlarkin

Fix invalid / missing format specifiers in several dnprintf statements
(used only when ACPI_DEBUG is enabled)

From a diff on tech@ by Fabian Raetz, with a minor change by me, tested
on i386 and amd64 with ACPI_DEBUG enabled and disabled.


# 1.257 25-Apr-2014 mlarkin

Defer the free of the hibernate structures until later, when it is safe to
do so.

Freeing the hibernate structures too early leads to inadvertent calls into
splx when we aren't ready for them yet. Found when trapping splx calls in
the resume path.

discussed with deraadt@ at m2k14


# 1.256 13-Apr-2014 mlarkin

Fix a badly worded debug message that implied the entire machine was going
to sleep, instead of just the acpi thread going to sleep.


# 1.255 31-Mar-2014 mpi

Change the order of operations during a suspend/resume cycle and
call bufq_quiesce() after executing the DVACT_QUIESCE handlers.

This should be safe since no disk nor controller drivers have such
handler but it will allow us to detach sd(4) devices attached to a
USB bus.

Another benefit pointed out by kettenis@ is that drivers that need
to read a firmware from the disk should be able to do it at resume
time in a DVACT_WAKEUP handler.

ok kettenis@, deraadt@


# 1.254 13-Mar-2014 dlg

get rid of the assumption that the head of the alldevs list is the
"mainbus" device. this breaks when mpath is enabled because it
attaches before mainbus and therefore takes the head position.

have autoconf provide device_mainbus() which looks up mainbus_cd,
and use that instead.

discussed with deraadt who just wants mpath stuff to move forward
despite there being many ways to shine this particular turd.


Revision tags: OPENBSD_5_5_BASE
# 1.253 21-Feb-2014 deraadt

Do the lid control further on in the resume sequence, since this is
AML, and will do WAITOK operations. While cold, that is a bad idea.
We seem to be safe with respect to the spec, I do not see any event
debounce issues here.
ok kettenis, tested by many


# 1.252 20-Jan-2014 claudio

Instead of checking sc->sc_revision <= 1 check that the
fadt->hdr_revision <= 1. This is a better check since it checks
the version of the proper descriptor. This allows the alix APU
to reboot since those boards have currently a wrong sc->sc_revision.
Committing this now so that it can be tested easier.
With and OK jsg@ also fine by derradt@


# 1.251 24-Dec-2013 kettenis

If the FADT has its SMI_CMD set to zero, assume we're only ACPI-only hardware
and don't need to disable SMI ownership of the ACPI hardware registers.

ok mlarkin@


# 1.250 23-Dec-2013 kettenis

Revert previous commit. It makes resume hang on some MP systems running
GENERIC.MP.


# 1.249 16-Dec-2013 kettenis

Restore hw.setperf upon resume like we do for apm(4) on i386. Seems we need
to do this fairly late in the resume to avoid hangs.

ok deraadt@, mlarkin@


# 1.248 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


# 1.247 06-Nov-2013 mpi

Add support for Power Resources for Dx states and the necessary hook
for PCI devices. This hook should be called twice, before and after
changing the power state of a PCI device.

Before setting the device to the new state, the ACPI layer will notify
every power resources linked to the device for that state and make sure
they are turned "_ON". After changing the state of the device, it will
decrement the reference of every power resources linked to that device
for the old state and turn them "_OFF" if they are no longer referenced.

This fixes the no-USB after resume problem seen on various ThinkPad,
problem initialy diagnosed with Alexander Polakov.

ok kettenis@, deraadt@


Revision tags: OPENBSD_5_4_BASE
# 1.246 01-Jun-2013 mlarkin

Stop printing ACPI wakeup devices in dmesg after the 16th wakeup device.

Helps with VMware VMs that report hundreds of wakeup devices.

ok reyk@, deraadt@


# 1.245 31-May-2013 bluhm

Fix the build for a kernel without wd and pciide in its config.
OK deraadt@ tedu@


# 1.244 30-May-2013 deraadt

Enforce ca_activate tree-walks over the entire heirarchy for all events,
cleaning up some shutdown-hook related code on the way.
(A few drivers related to sparc are still skipped at kettenis' request)
ok kettenis mlarkin, tested by many others too


# 1.243 18-Apr-2013 deraadt

pin the acpi thread to the BSP, because AML and SMI cannot be trusted. I
was certain we were doing this already, but it turns out no...


Revision tags: OPENBSD_5_3_BASE
# 1.242 24-Dec-2012 guenther

In acpiioctl(), Return though the splx() path if suser() returns an error

ok deraadt@


# 1.241 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


# 1.240 04-Oct-2012 ehrhardt

Use information provided by ACPI to attach secondary PCI host bridges.

ok kettenis@


# 1.239 07-Sep-2012 kettenis

Add a new function called acpi_pci_min_powerstate() that returns the lowest
power state for a PCI device for the power state (currently S3 or S4) we're
transitioning into. This function is probably too simplistic; there is a lot
of (silly) complexity in the ACPI spec.

ok deraadt@


Revision tags: OPENBSD_5_2_BASE
# 1.238 13-Jul-2012 pirofti

Disable acpivideo(4) on ASUS laptops.

There are some models that panic whenever an event is received.
Some cope, some don't. In order to get this right acpiec(4) requires
major rework. Until then let machines boot.

Discussed with deraadt@.


# 1.237 11-Jul-2012 mlarkin

for now, restrict hibernate to root in the acpi ioctl kernel interface.
normal rules for controlling this interface through apmd still apply.

ok deraadt@


# 1.236 09-Jul-2012 deraadt

tiny little oops in untested hibernate block


# 1.235 09-Jul-2012 deraadt

clean up ioctl() case statements


# 1.234 09-Jul-2012 deraadt

acpi_disable_onegpe() is not used; ok pirofti


# 1.233 24-May-2012 kettenis

If _MIN > _MAX consider a WordBus resource to be invalid. Fixes a panic on
the IBM x3550-M4.

ok mikeb@


# 1.232 29-Mar-2012 mlarkin

Missing #ifdef in previous diff


# 1.231 29-Mar-2012 mlarkin

Prevent hibernation attempts on machines with unsupported disk controllers.
The same check is also performed later in the hibernate sequence, but by
then it's too late to cleanly unwind (presently).

ok deraadt


# 1.230 26-Mar-2012 deraadt

simplify conditionals in acpi_sleep_state()
ok mlarkin


# 1.229 26-Mar-2012 deraadt

hook in the hibernate request code; half of this diff is from mlarkin
ok mlarkin


Revision tags: OPENBSD_5_1_BASE
# 1.228 20-Sep-2011 deraadt

Very early in suspend while the clock is still ticking realtime,
save the clock back to the rtc. The zaurus already did this.
ok phessler miod kettenis


Revision tags: OPENBSD_5_0_BASE
# 1.227 02-Jul-2011 nicm

kqueue attach functions should return an errno or 0, not a plain 1. Fix
the obvious cases to return EINVAL and ENXIO.

ok tedu deraadt


# 1.226 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.225 16-Jun-2011 pirofti

Import acpitoshiba driver.

This is originally written by Hiroyuki Aizu, ported and integrated
in our tree by Javier Vazquez <javama4 AT gmail> with tweaks and
suggestions by me.

Import prodded by deraadt@ and the lack of testers on tech@.


# 1.224 27-Apr-2011 jcs

attach acpithinkpad to newer lenovo models like the x120e

ok deraadt@


# 1.223 22-Apr-2011 jordan

Fix uninitialzied variables and formatting strings (-Wxxx errors)


Revision tags: OPENBSD_4_9_BASE
# 1.222 02-Jan-2011 jordan

Re-add code for ACPI taskqueue, eliminates need for state vars in acpi softc
Serialize processing ACPI tasks for sleep/powerdown/GPE event
ok deraadt@


# 1.221 31-Oct-2010 guenther

Revert last commit: it breaks resume on Thinkpads
"then please back it out" deraadt@


# 1.220 26-Oct-2010 jordan

Add task queue for ACPI gpe and notify handlers
This fixes eject on Dell Latitude dock and an issue on the Dell Mini
battery update.
ok deraadt


# 1.219 07-Oct-2010 jordan

Create separate functions for enabling wake and runtime gpes.
Remove old suspend/resume gpewalk function
ok deraadt, mlarkin


# 1.218 05-Oct-2010 jordan

split acpi_enable_onegpe to separate enable/disable functions
ok deraadt


# 1.217 05-Oct-2010 jordan

Added acpi_disable_allgpes function to clear GPE status
ok deraadt


# 1.216 05-Oct-2010 deraadt

clean up acpi_softc use more


# 1.215 05-Oct-2010 deraadt

delete a bit of #if 0 code


# 1.214 05-Oct-2010 deraadt

hide use of our global acpi_softc pointer a bit better


# 1.213 29-Sep-2010 deraadt

grab the acpi thread lock very early in acpi thread startup; not that it
is likely that anyone will race us... but why be careless.
ok kettenis


# 1.212 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.211 29-Aug-2010 jasper

- add a check for FWRITE here too.

ok miod@


# 1.210 27-Aug-2010 kettenis

Make PCI Power Management optional, and only enable it when acpi(4) attaches.

ok deraadt@


Revision tags: OPENBSD_4_8_BASE
# 1.209 08-Aug-2010 kettenis

Close race between the acpi thread and wscons ioctls running in process
context. We might rip this out post-release and replace it with a solution
that serializes things in a more obvious way.

ok deraadt@, miod@ and "those crazy texans who ok anything"


# 1.208 08-Aug-2010 deraadt

Report the acpi sleep states when on a RAMDISK; ok krw


# 1.207 07-Aug-2010 kettenis

Enforce acpi register access rescrictions.

ok mlarkin@, deraadt@, tested by marco@


# 1.206 07-Aug-2010 deraadt

upon resume, notify all the acpiac and acpibat drivers to update
their status. some laptops do not do this automatically.
ok kettenis canacar


# 1.205 06-Aug-2010 marco

Always call _PSW on _LID devices that support it to enable lid open to
resume the machine. Conversely disable it when the machine wakes up.

Tested by several ok deraadt


# 1.204 06-Aug-2010 deraadt

As part of a revamp of the PM1/GPE code, I write this diff to look for
unmanaged EN & STS bits in the PM1 register at interrupt time and report
them. As a side effect this splits the STS acknowledgement into two writes
(for power, and sleep) instead of one. The printf that is added (to spot
unmanaged STS bits) has not yet been triggered as far as we know.

Before the "write to PM1 registers at the right offset" diff went in, this
was not neccessary. But newer thinkpads do not have a working soft power
button without this diff.

We have no idea why.
ok mlarkin kettenis


# 1.203 05-Aug-2010 deraadt

PM1 power button events were generating 'fake events' to acpibtn PWRB,
but if there is no such device these were being tossed. Instead, arrange
for all power button handlers to flag a softc variable which is processed
by the thread; so that one place will know to request a powerdown (psignal
to init)
ok marco mlarkin


# 1.202 05-Aug-2010 deraadt

rename a variable which is annoyingly poorly named


# 1.201 05-Aug-2010 mlarkin

Write to the correct offset for various ACPI pmreg registers. Writing to
the correct register does properly clear the wake status bits and fixed
event bits, which allows some machines to have more than 1 suspend cycle.

bug spotted by deraadt@

tested by deraadt@ and myself on a half-dozen machines and others on
hackers@

ok deraadt@, marco@


# 1.200 05-Aug-2010 deraadt

Fix the power button. The pm1 register is 16 bits in size.
My mistake, a while back.
tested by krw


# 1.199 04-Aug-2010 kettenis

Print supported sleep states.

ok marco@, phessler@, deraadt@


# 1.198 03-Aug-2010 mlarkin

Use the proper flag for re-enabling certain hardware events (power btn,
sleep btn). The (incorrect) flag prevoiously used coincidentally had the
same value, so this shouldn't cause any different behavior than before.

tested on a variety of machines (i386, amd64, sp, mp) - no behavioral
change seen

ok deraadt@


# 1.197 28-Jul-2010 marco

Make all gasio failures look and behave the same.


# 1.196 28-Jul-2010 mlarkin

Delay a little longer on suspend before giving up. Fixes machines that
sometimes don't suspend because the sleep transition takes too long
(symptoms like failing to sleep but the backlight stays on).

ok deraadt@


# 1.195 27-Jul-2010 deraadt

remove XXX; EC can now be attached early when needed; ok jordan


# 1.194 27-Jul-2010 deraadt

more brackets feels good


# 1.193 27-Jul-2010 deraadt

XXX annotate a block in acpi_enter_sleep_state() that is pretty worrying


# 1.192 27-Jul-2010 deraadt

horrific indentation that nearly made me puke


# 1.191 27-Jul-2010 mlarkin

Set the sleeping indicator light on machines that support it, in case SMI
doesn't do it for us. Shows the 'moon' LED on older Thinkpads when
sleeping.

Tested on T43p, W500, T510 by me and on X61s by deraadt@.

ok deraadt@


# 1.190 27-Jul-2010 jordan

Early initialization of acpiec if ECDT table exists
Fixes hang when booting thinkpads while docked
ok deraadt


# 1.189 26-Jul-2010 deraadt

remove merge error which was commited


# 1.188 26-Jul-2010 pirofti

Add support for Sony ACPI hotkeys via a new driver: acpisony(4).

Currently it only works for the suspend button and tries to do right for
the brightness events, but I haven't found a Sony laptop that like to do
right so far.

In the future I want to make the brightness keys work on all Sony's and
also add support for the zoom hotkeys and whatever other funky keys I can
find on those things.

Okay deraadt@.


# 1.187 22-Jul-2010 deraadt

pretty large cleanup of SMALL_KERNEL


# 1.186 21-Jul-2010 deraadt

in the acpi GPE handler, late ack level interrupts not edge interrupts.
Duh.


# 1.185 21-Jul-2010 deraadt

Merge the edge and level gpe handlers into one (now possible since the
gpe structure tells us whether the gpe is edge or level)


# 1.184 21-Jul-2010 deraadt

delete #if 0 code we are not going to use; ok marco


# 1.183 20-Jul-2010 deraadt

make the mask code for acpi_enable_onegpe() match other similar code


# 1.182 20-Jul-2010 deraadt

Handle the acpi interrupt controller with proper edge/level handling,
wrapped in spltty to avoid racing against the interrupt controller.
Repair the gpe bit masking code while there, and do operations in the
right order
ok kettenis mlarkin, help from jordan at figuring out the order of
operations.


# 1.181 20-Jul-2010 matthew

When we're preparing to go to sleep, only call bufq_restart() if other
preparations failed.

ok kettenis@, thib@, deraadt@


# 1.180 19-Jul-2010 deraadt

permit compilation with ACPI_DEBUG


# 1.179 19-Jul-2010 deraadt

there is no need for a seperate dsdt_softc pointer; dsdt does not have a
softc but is just a part of the single acpi driver.
ok jordan


# 1.178 19-Jul-2010 deraadt

acpi_delay() is no longer needed; ok jordan


# 1.177 18-Jul-2010 mlarkin

Clear event status on resume, to avoid going back to sleep/reboot/shutdown
immediately after resume. Tested by a bunch of people on hackers@ on
various hardware. Fixes at least Toshiba NB200 and Gateway LT3103u.

ok marco


# 1.176 13-Jul-2010 deraadt

rename some thread related interfaces so they are logical


# 1.175 10-Jul-2010 jordan

Verify _ADR bdf is correct (some systems return 0xffff)
Works on Dell x9xx and Studio 1555


# 1.174 09-Jul-2010 jordan

Clear the noise


# 1.173 08-Jul-2010 jordan

Add mapping for ACPI device to PCI bdf (match autoconf tree)
Simplify resource parsing function to use buffer argument
Convert namespace linked lists to use queue macros
ok marco@, deraadt@


# 1.172 06-Jul-2010 deraadt

Use spltty() to lock downcalls from apm against the information being
modified by the acpi thread.
ok kettenis


# 1.171 06-Jul-2010 deraadt

Don't use a workq to do the suspend, because it races aginst the acpi
thread. Instead, just tell the acpi thread to do the suspend for us.
This makes apmd & zzz work correctly. While here, have acpithinkpad
attempt to post the event to apm before prompting the suspend itself.
ok kettenis marco mlarkin


# 1.170 05-Jul-2010 mlarkin

Reset register SCI_EN on resume. The ACPI spec clearly states we don't have
to do this, but many machines don't, or forget to, which leads to all sorts
of problems on resume (broken battery stats, overheating, failure to resume
more than once, etc).

tested by many, ok marco@, deraadt@


# 1.169 01-Jul-2010 thib

do bufq quiesce again, since vnd/the bufq_done call in
biodone has been fixed.


# 1.168 01-Jul-2010 jordan

Backout recent AML changes, breaks on R210 and others


# 1.167 01-Jul-2010 jordan

Simplified aml_parse_resource call, cleanup code
ok marco


# 1.166 01-Jul-2010 jordan

Fix subordinate bus # for multi-root PCI buses
ok marco, kettenis


# 1.165 30-Jun-2010 thib

Disable/partially backout the bufq quiesce changes as this
is causing havoc with vnds and release must be buildable.


# 1.164 29-Jun-2010 deraadt

Fix ramdisks (new code was placed inside a SMALL_KERNEL chunk)


# 1.163 29-Jun-2010 jordan

Add support for mapping ACPI to PCI devices
ok kettenis, deraadt


# 1.162 29-Jun-2010 kettenis

Call bufq_quiesce() on suspend to stop all I/O and bufq_restart on resume.
Makes suspend/resume work much better while doing disk I/O.

ok mlarkin@


# 1.161 29-Jun-2010 tedu

60 is a more reasonable guess for number of minutes in an hour than 100.
spotted by TeXitoi. ok deraadt


# 1.160 27-Jun-2010 jordan

Add _acpi_matchhids routine, precursor to pci device match


# 1.159 27-Jun-2010 mlarkin

Clear ACPI fixed event status on resume (power buttons/etc) so that some
machines don't immediately resume after suspending to S3.

Tested by kettenis@ and I on 5 or 6 machines, deraadt@ "yeah, looks good"


# 1.158 27-Jun-2010 jordan

Cleaned up acpi_maptable, adds to table list directly


# 1.157 07-Apr-2010 deraadt

If we have done a wsdisplay_suspend(), and ACPI decides it does not want to
suspend, we must roll back by calling wsdisplay_resume() or the virtual
console switching will be locked forever
ok oga


# 1.156 07-Apr-2010 kettenis

Make suspend/resume work on MP machines (running an MP kernel). Joint work
from mlarkin@ and me, with some amd64 fixes thrown in by deraadt@

ok marco@, deraadt@, pirofti@, mlarkin@


# 1.155 31-Mar-2010 kettenis

Bring /dev/apm support in line with apm(4). Make the suspend button send
a suspend request event to apmd(8) instead of suspending immediately. Also
keep track of whether /dev/apm and /dev/apmctl are currently open, such that
we can still suspend immediately if apmd(8) isn't running.

ok deraadt@, marco@, pirofti@, jsing@, oga@


# 1.154 30-Mar-2010 oga

Prevent the apmd/x races for good.

When we hit suspend time, go through all wsdisplays on the system. if
they are in mode MAPPED, but not MODE_DUMBFB then if possible do a full
vt switch to a !mapped vt, and prevent switching back until resume time.
This has to be called from MD code because this involves userland
running so that X can run the vt switch signal handler. This way, any
case where we are using the "poke registers from userland" model, we
will not be on the hardware when we go down, so the kernel can actually
handle thing properly.

Tested on several acpi laptops (by kettenis@ and ian@), x40 (me and
beck@ at LEAST) and zaurus (me). Maybe others, but if so I forgot who at
this time..

Idea from deraadt somewhere over the Faroe Islands (I thought of a
similar thing myself a while ago). Much prompting from him. Ok and
comments miod@


# 1.153 25-Mar-2010 oga

Fix & vs. && in the kqfilter code.

Based on a comparison with the apm code.

ok deraadt@, kettenis@


Revision tags: OPENBSD_4_7_BASE
# 1.152 26-Nov-2009 mlarkin

Visual (and on some machines, audio) feedback while the machine is
resuming via calls to aml _SST method. This also makes the "moon" LED on
thinkpads to blink during resume and cease when resume is completed.

ok pirofti@


# 1.151 26-Nov-2009 deraadt

Toggle into "cold" when we we disable interrupts, because tsleeping
in the AML interpreter and context switching into userland processes
tends to sometimes pop the ACPI trampoline page out of our address
space. Makes lots more machines work.
ok kettenis mlarkin


# 1.150 24-Nov-2009 jsg

in the resume path evaluate _WAK before dealing with device drivers
like the spec says, not after.

ok mlarkin@


# 1.149 24-Nov-2009 deraadt

abort suspends on SMP systems until that gets written; ok mlarkin


# 1.148 23-Nov-2009 mlarkin

pci should return the result of it's children's suspend/resume calls back
to its parent.

handle suspend failure case and unwind if devices have an activate function
that returns failure.

ok deraadt@, kettenis@


# 1.147 23-Nov-2009 mlarkin

acpi* devices need to call their children on suspend/resume.
ok deraadt@


# 1.146 23-Nov-2009 pirofti

Remove ACPI_SLEEP_ENABLED checks.

This enables by default the suspend/resume paths in the kernel.

Okay deraadt@.


# 1.145 23-Nov-2009 deraadt

knf and spacing that are super annoying


# 1.144 23-Nov-2009 mlarkin

Hook up Theo's new children suspend/resume function, as well as more ACPI
suspend/resume glue.

ok deraadt@


# 1.143 26-Oct-2009 deraadt

Do not do strncmp comparisons on dv_xname because longer device names which
look similar could arrive in the future. Instead, compare directly against
dv_cfdata->cf_driver->cd_name
Issue originally spotted by miod


# 1.142 26-Oct-2009 jordan

Verify checksum+address when loading ACPI tables.
Some systems had invalid entries in RSDT/XSDT.
ok marco@


# 1.141 23-Jul-2009 cnst

New aibs(4) driver for ASUSTeK AI Booster (ACPI ATK0110) hardware monitoring,
with sensor state support through limits provided by the ACPI.

Tested on several ASUS motherboards kindly networked by Sam Fourman Jr.

ok deraadt marco jordan


Revision tags: OPENBSD_4_6_BASE
# 1.140 03-Jun-2009 pirofti

Import acpi power resource driver. This is needed for suspend/resume logic.

Okay jordan@ and marco@.


# 1.139 03-Jun-2009 pirofti

Add acpivideo support. This adds brightness support for all laptops
except thinkpads, they will use the acpithinkpad driver. The driver is
also hooked into wsconsole. So brightness can be adjusted via:

$ wsconsctl display.brightness=<percentage>

This is very helpfull on some laptops that have a nasty bios and get two
steps instead of one when pressing the brightness button.

Tested on various dell, fujitsu, acer, samsung and other laptops.

Okay marco@, miod@. Suggestions from kettenis@.
Lots of reviews and help from miod@, thanks!


# 1.138 03-Jun-2009 jordan

Added ejectable bay for IDE cdroms
ok marco@


# 1.137 30-Apr-2009 marco

Oga changed apm ioctls and forgot to grep. Temporary work around by hitting
the sleep button when we get any request. This interface will chnage at
some point.
ok oga


# 1.136 19-Apr-2009 krw

Call acpi_sleep_walk() when powering down just like when entering
any other sleep state. This tells acpi wake devices to not wake up
the box until manual powering up resets their status. Makes my Dell
GX520 stay powered off in response to 'halt -p'.

In snaps for a while.

ok marco@ (several times) deraadt@


# 1.135 17-Apr-2009 pirofti

Add device ranking support for future sleep/wake developments.

okay marco@


# 1.134 11-Mar-2009 jordan

Cleanup.. use aml_evalinteger instead of aml_evalname
ok marco@


# 1.133 10-Mar-2009 jordan

Fix cut/paste error.. call _BFS not _PTS
ok marco@


Revision tags: OPENBSD_4_5_BASE
# 1.132 21-Feb-2009 marco

overzealous cut/paste; found by Vladimir Kirillov <proger@uaoug.org.ua>


# 1.131 20-Feb-2009 marco

Oops didn't mean for Debugger() to remain there.


# 1.130 19-Feb-2009 marco

suspend/resume bits so that we can develop this in tree. This is disabled.
code from mlarkin and me
help from art,toby,jordan and several others
ok jordan, go for it deraadt


# 1.129 10-Feb-2009 jordan

Fix null pointer for shutdown if acpi not enabled
ok marco@


# 1.128 04-Feb-2009 kettenis

Some systems that claim to be ACPI 2.0 apparently don't provide an XSDT
address. Make the code fall back on the RSDT on those systems.

From Joerg Niendorf <p3y80@internode.on.net>. Fixes PR 5986.


# 1.127 17-Jan-2009 krw

A couple of stray malloc/memset -> malloc(M_ZERO), a bit of knf and
lose a couple of unnecessary casts. No functional change.

Feedback from kettenis@

ok kettenis@ marco@


# 1.126 06-Nov-2008 marco

Collapse all sanity checks of hids into one function.

Originaly from jordan


Revision tags: OPENBSD_4_4_BASE
# 1.125 02-Jul-2008 fgsch

add acpivideo. for now almost an empty skel so others can work on
suspend/resume. eventually it will also manage output switching and
brightness where supported. prodded by marco@


# 1.124 11-Jun-2008 marco

Fix $OpenBSD$ for once and for all


# 1.123 08-Jun-2008 deraadt

aaa_dev was being set from an uninitialized variable; ok toby


# 1.122 07-Jun-2008 marco

Kill ACPIVERBOSE. It was never used and is worthless to boot.

comments jmc
ok toby


# 1.121 06-Jun-2008 marco

Shrink acpi some more for floppies and while here re-enable acpi on the
floppies. Fixes crazy sony vaio kettenis has.

tested by deraadt
ok deraadt jordan kettenis


# 1.120 01-Jun-2008 marco

Fix \n nit in SMALL_KERNEL.


# 1.119 01-Jun-2008 marco

Re-enable new parser. Lots of testing by various folks. Fits all media
now too.

ok kettenis deraadt


# 1.118 16-May-2008 dlg

back out jordans new parser.

it didnt have enough oks from the right people, it did break machines
that were previously working, and it bloated the RAMDISKS and install
media beyond the constraints of the media.

requested by deraadt@


# 1.117 14-May-2008 jordan

Adding new ACPI Parser code
Old guts of code still exists, needs to be torn out next
ok marco@
ok brad@


# 1.116 27-Apr-2008 jcs

add acpithinkpad(4), an acpi support driver for ibm/lenovo
thinkpads.

provides functionality that the bios did on older, non-acpi
thinkpads like respond to screen brightness keys and toggle the
bluetooth device on and off.

ok deraadt@ and marco@


# 1.115 24-Apr-2008 jsing

Introduce acpiasus(4), a driver for the ACPI based hotkeys found in many
ASUS laptops (including the ASUS EeePC) - largely based on NetBSD's
asus(4) driver. On the ASUS EeePC this allows us to enable/disable
wireless, change screen brightness and use the volume keys.

ok jsg@, weingart@


# 1.114 12-Apr-2008 kettenis

Get rid of acpi_s5 global variables; simply send SIGUSR2 instead of SUGUSR1
to tell init(8) to power down the machine.

ok krw@


Revision tags: OPENBSD_4_3_BASE
# 1.113 21-Jan-2008 jsg

Check against RESET_REG_SUP in FADT before doing the acpi reset dance
after all, not doing so appears to break more machines than it fixes.

ok marco@


# 1.112 05-Jan-2008 kettenis

Never attach more acpicpu(4) devices than cpu(4) devices. Always attach
the first acpicpu(4) device on non-MULTIPROCESSOR kernels, regardless of its
ACPI processor ID.

ok mikeb@


# 1.111 05-Dec-2007 deraadt

Hang acpi(4) below bios(4) instead of mainbus(4). This lets us move acpi(4)
before pcibios(4), and then.... if acpi(4) attaches, skip pcibios(4) since
it messes the machines up. Fixes claudio's HP dl320 G5, and almost assuredly
others.
ok kettenis


# 1.110 05-Dec-2007 deraadt

repair acpiopen/acpiclose and friends to handle the /dev/apmctl interface,
so that apmd(8) can actually work correctly against acpi(4), and thus,
even do the fancy -C/-A stuff; also tested by jmc


# 1.109 02-Dec-2007 fgsch

fix the trailing space for real now.


# 1.108 02-Dec-2007 fgsch

avoid trailing spaces on output; marco@ ok.


# 1.107 27-Nov-2007 kettenis

Fix off-by-one that made us attach acpicpu0 and acpicpu1 on GENERIC kernels.

ok marco@


# 1.106 25-Nov-2007 jsg

When available and ACPI is enabled use the ACPI reset method
to reboot. This is required for some quirky sis machines
and likely others to reboot.

Tested by many. 'commit early commit often' deraadt@


# 1.105 19-Nov-2007 deraadt

sigh, amd64 still uses that stupid acpi_enabled variable...


# 1.104 19-Nov-2007 deraadt

softc's come initialized to 0, you know...


# 1.103 19-Nov-2007 deraadt

store revision; from jsg


# 1.102 19-Nov-2007 kettenis

Initialize sc_facs earlier.

ok deraadt@, beck@, canacar@


# 1.101 16-Nov-2007 deraadt

fix the bus_space #define nightmare, so that amd64 and i386 are much more
uniform. as a result shared code like acpi needs less #ifdef's
ok marco kettenis


# 1.100 15-Nov-2007 deraadt

remove the #ifdef ACPI_ENABLE. the EC codepath is either safe enough (and
will be improved), or it is unsafe because AML assumes it must be there
ok almost everyone


# 1.99 15-Nov-2007 mikeb

Make acpicpu(4) attach to the existing CPUs only.

Problem reported by form@. Fix idea by kettenis@.
Lots of help from deraadt@. Tested by deraadt@, form@ and me.

ok deraadt marco


# 1.98 14-Nov-2007 deraadt

spacing


# 1.97 12-Nov-2007 deraadt

correct : vs , in attach-time messages


# 1.96 06-Nov-2007 deraadt

all ACPI_ENABLE kernels need acpi_interrupt()


# 1.95 06-Nov-2007 deraadt

move acpi_read_pmreg() and acpi_write_pmreg() out of SMALL_KERNEL so
that acpi ramdisks can compile (obvious solution)


# 1.94 05-Nov-2007 canacar

Establish the acpi interrupt earlier. Fixes problems with some
GENERIC.MP kernels not getting acpi interrupts. This is a
workaround until the real issue with late establishment is fixed.
ok weingart@, beck@, gwk@


# 1.93 03-Nov-2007 jordan

Added support for displaying wakeup devices
ok beck@,weingart@,gwk@


# 1.92 11-Oct-2007 kettenis

When walking the tree yo find _PRT methods, check whether the device is
actually enabled and functioning. Similar to what we do for _INI.

ok weingart@, beck@


# 1.91 08-Oct-2007 krw

More simple memset(,0,) -> M_ZERO changes. In this batch move to
size(*p) as the first malloc() parameter where p is declared locally
and thus easy to check. Add M_ZERO to gpe_table allocation in acpi.c
even though there is no obvious bzero or memset nearby.


# 1.90 13-Sep-2007 mk

Remove unnecessary memset() (see aml_evalnode(9)) and a bit of KNF.

ok toby


# 1.89 13-Sep-2007 weingart

Implement ACPI 6.5.1 spec tree walk for _STA and _INI.
Fixes mk's laptop. No regressions so far (thank you to the
testers).

ok gwk@, mk@, marco@


Revision tags: OPENBSD_4_2_BASE
# 1.88 17-Apr-2007 mk

Now that acpidock no longer hangs machines during boot (at least
according to the reports I've been getting) it should be safe to enable
it again, so remove the #if 0 stuff around it.

ok marco


# 1.87 11-Apr-2007 jordan

Added changes for C-State
ok marco@


# 1.86 26-Mar-2007 marco

Calculate minutes left more accurately.

From giovanni <qgiovanni@gmail.com>


Revision tags: OPENBSD_4_1_BASE
# 1.85 03-Mar-2007 mk

Stop looking for acpidock until we fix that stuff that breaks all pre-60
series ThinkPads with acpidock(4) enabled.


# 1.84 22-Feb-2007 marco

We were getting no interrupts in .MP. There are 2 fixes; one in
acpi_machdep.c and move the interrupt establishment down after ioapic
has been initialized.

ok jordan


# 1.83 21-Feb-2007 kettenis

Make an acpi-enabled GENERIC.MP work on systems that only support PIC mode.

ok marco@


# 1.82 19-Feb-2007 jordan

Added acpi_map_address function for mapping GAS/IO registers
ok marco@


# 1.81 18-Feb-2007 jordan

Added dynamic perfect hash for opcodes
ok marco@


# 1.80 18-Feb-2007 marco

Shave off another K pre compression.
Shuffle SMALL_KERNEL functions around so that we only have a single #ifndef


# 1.79 17-Feb-2007 marco

Add SMALL_KERNEL throught acpi to make it fit on boot media.
ok deraadt


# 1.78 13-Feb-2007 marco

Add smarter _INI code. This helps with some dissapearing devices.

ok kettenis jordan


# 1.77 31-Jan-2007 gwk

Introduce acpi_hasprocfvs thats "ACPI Has Processor Frequency and Voltage
Scalling" a global flag set by acicpu attach to indicate that ACPI is
handling fvs through the _PSS objects. This will fix disappearing
EST/powernow when booting kernels with acpi enabled, as acpicpu isnt
compiled into GENERIC yet. Also in cases where acpicpu is built in and
legacy EST/powernow work but acpi is missing the _PSS object, the legacy
routines will be used.

tested by dim@, and Nick Nauwelaerts <nick AT nauwelaerts.net>
ok marco@, dim@


# 1.76 22-Jan-2007 mk

Actually look for docking stations by looking for _DCK nodes.

ok marco


# 1.75 02-Jan-2007 marco

On some systems EC must be attached as the first device. Move EC from
generic _HID devices to its own routine to force first attach. Bug found
and debugged by joris@

Code mostly written by joris@


# 1.74 26-Dec-2006 marco

Add polling to devices that require it such as acpibat and acpitz.
Use the same tsleep/wakeup mechanism as the interrupt code to keep it non-
concurrent.


# 1.73 21-Dec-2006 deraadt

even more knf love


# 1.72 21-Dec-2006 marco

Remove some leftover acpiec gpe/interrupt code.


# 1.71 20-Dec-2006 deraadt

oops, name already used


# 1.70 20-Dec-2006 deraadt

rename __acpi_enable_gpe(); ok marco


# 1.69 20-Dec-2006 deraadt

acpi_unmap_pmregs() is not used, ok marco


# 1.68 19-Dec-2006 deraadt

simplify some low-level methods substantially (shrinking code); ok marco jordan


# 1.67 06-Dec-2006 mk

malloc(..., M_NOWAIT) with no NULL check is bad. If allocation fails,
complain, clean up, and bail out of attach.

input and ok gwk


# 1.66 28-Nov-2006 jordan

Fixed clearing arg for acpi_init_pic
ok kettenis@


# 1.65 27-Nov-2006 kettenis

Don't spam dmesg.


# 1.64 27-Nov-2006 jordan

Fixes for acpiprt routing
Post-parse fixup for namerefs
ok kettenis@


# 1.63 25-Nov-2006 marco

Don't handle acpiev events inside the gpe handler since this causes deadlocks.

ok jordan@


# 1.62 15-Nov-2006 kettenis

Hook up acpi apic interrupt routing support to generic acpi code.

ok jordan@


# 1.61 02-Nov-2006 marco

Kill acpiec attach function and add its functionality to the generic _HID
device attach

prodded and ok jordan


# 1.60 02-Nov-2006 marco

Fix comments to reflect reality.


# 1.59 19-Oct-2006 marco

Replace ACPI ioctls with APM ioctls. This makes ACPI talk the same protocol
as APM therefore userspace can now retrieve power information in the same
fashion as all other apm arches (zaurus, macppc, i386 etc).

To use force the use of acpi device like so: apmd -f /dev/acpi

ok jordan@


# 1.58 19-Oct-2006 jordan

Fixed GPE debug levels
Fixed ref counter on temporary bufferfields
DSDT parsing fully working on IBM Thinkpad
ok marco@


# 1.57 15-Oct-2006 jordan

Removed old GPE handler code remnants
ok marco@


# 1.56 12-Oct-2006 jordan

Massive ACPI changes
Fixed ACPI leaks
new ACPI API
ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.55 30-Jun-2006 gwk

KNF, No functional change,
"go at it" marco@


# 1.54 30-Jun-2006 jordan

Support for calling Embedded Controller via new GPE interface
ok marco@


# 1.53 30-Jun-2006 jordan

Added new code for GPE handling
Also works with ACPIEC; need to fixup acpiec.c to remove original interrupt handler
ok marco@


# 1.52 30-Jun-2006 jordan

Added offset argument for acpi read/write pmreg routines (new GPE code)
ok marco@


# 1.51 31-May-2006 todd

do not call acpi_powerdown() unless acpi has successfully initialized
ok gwk@ canacar@


# 1.50 29-May-2006 canacar

Add embedded controller support.
ok marco@ deraadt@ testing & ok gwk@


# 1.49 19-May-2006 canacar

Minimal thermal zone driver. Monitors thermal zone temperature,
shuts down the system if the 'critical temperature' is reached.
ok marco@


# 1.48 11-Apr-2006 marco

KNF.


# 1.47 11-Apr-2006 gwk

Add acpi_resume(struct acpi_softc *sc), which calls some AML methods,
detected at attach time, currently unused.

ok marco@


# 1.46 11-Apr-2006 gwk

Discover some ACPI sleep related methods, and call them at the
appropriate time in acpi_enter_sleep_state().

ok marco@


Revision tags: OPENBSD_3_9_BASE
# 1.45 26-Feb-2006 marco

Add acpicpu as a device.

ok jordan@


# 1.44 23-Feb-2006 marco

remove dead function.

ok jordan@


# 1.43 22-Feb-2006 marco

Print out all tables it found during autoconf. We'll need this data in
the future.


# 1.42 22-Feb-2006 marco

Make dmesg less verbose.

ok jordan.


# 1.41 22-Feb-2006 jordan

Added parsing of SSDT tables
ok marco@


# 1.40 22-Feb-2006 jordan

Added new aml_register to notify by device id
Moved powerdown code to button device
ok marco@


# 1.39 21-Feb-2006 marco

Readjust debug levels that crept in too low over the last few days.
KNF


# 1.38 21-Feb-2006 marco

Rig the power button so that the machine shuts down correctly.


# 1.37 20-Feb-2006 marco

Add acpibtn device. Remains disabled for now.


# 1.36 19-Feb-2006 jordan

Added code for aml_notify GPE notification callback
ok marco@


# 1.35 19-Feb-2006 grange

Fix acpi_enter_sleep_state() by replacing current ugly hack
with mostly correct sleeping state transition code.

ok brad@ marco@


# 1.34 19-Feb-2006 marco

jordan:
Enable interrupts.
Added peripheral code to make this work.

marco:
Replace softintr and other types of interrupts with a handler with context.
Fixed interrupt magic for GPEs.
Some KNF

This is work done by jordan and I but has to be a single commit.


# 1.33 17-Feb-2006 marco

Remove earlier introduced delays. We really only need them during autconf.

ok jordan@


# 1.32 17-Feb-2006 marco

Add a generic delay function as per the ACPI spec.

from jordan@


# 1.31 17-Feb-2006 marco

Use tsleep instead of delay when !cold.


# 1.30 17-Feb-2006 marco

Argh, we need delays between reads/writes. The backend reads (i2c most likely)
are so slow that values come back as 0 sometimes. We have to do this
despite the spec saying we shouldnt have to. Guess having a faster
parser than others can hurt sometimes.

Idea from jordan.


# 1.29 17-Feb-2006 jordan

Fixed code for handling _INI
Rearranged debug statements
ok marco@


# 1.28 16-Feb-2006 jordan

Added stub for calling _INI
Fixed aml_dowhile for NULL return
ok marco@


# 1.27 16-Feb-2006 jordan

Fixed parser/evaluator to retrieve battery info+state
Cleanup of parser
Dell laptops return battery status
ok marco@


# 1.26 06-Feb-2006 brad

fix a typo so this will compile on amd64.


# 1.25 04-Feb-2006 marco

When ACPI_ENABLED wasn't set but the devices were it had an unknown variable.

Pointed out by ian@.


# 1.24 03-Feb-2006 jordan

Rewrote aml parser yet again...
Fixed bus_space_map for i386/amd64
Added _BST and _BIF to acpibat to get status/info
ok marco@


# 1.23 20-Jan-2006 jordan

More evaluator changes
Removed old evaluator code
Fixed _HID value
ok marco@


# 1.22 19-Jan-2006 jordan

Added support for read/write of PCI Config space
Help jason@
ok marco@


# 1.21 18-Jan-2006 jordan

Added new evaluation routines
Added helper methods for buffer/field evaluation
Fixed broken indentation on dsdt.c

ok marco@


# 1.20 17-Jan-2006 jordan

Major overhaul of the aml parser. Evaluater temporarily disabled.

ok marco@


# 1.19 06-Jan-2006 grange

Check for ACPI control availability only if ACPI_ENABLE defined.


# 1.18 06-Jan-2006 grange

Informative error messages on attach, prettier dmesg.


# 1.17 06-Jan-2006 grange

printf -> dnprintf conversion for debug messages.


# 1.16 05-Jan-2006 grange

Find SLP_TYPx values and store them for future use in
acpi_enter_sleep_state().


# 1.15 05-Jan-2006 grange

Random KNF and cleanup


# 1.14 05-Jan-2006 grange

Provide symbolic names for the acpi sleeping states and use it.


# 1.13 28-Dec-2005 marco

Moved DEVNAME to a acpidev.h

ok jordan@


# 1.12 18-Dec-2005 sturm

add back comment about wrong code

ok marco


# 1.11 16-Dec-2005 marco

Remove test for kbd_reset. Prompted deraadt@, ok jordan@


# 1.10 16-Dec-2005 jordan

Added hooks for powerdown/power button press


# 1.9 16-Dec-2005 jordan

Cleaned up common power management register i/o
Added enter sleep state ioctl
Hooked power button interrupt
ok marco@


# 1.8 16-Dec-2005 marco

Remove autoconf chatter


# 1.7 16-Dec-2005 jordan

Moved DSDT AML parsing into parent ACPI driver
ok marco@


# 1.6 14-Dec-2005 marco

Add AC device glue.


# 1.5 14-Dec-2005 marco

Adjust debug printing levels to something more sane.


# 1.4 13-Dec-2005 marco

Add glue to attach devices to apci.


# 1.3 07-Dec-2005 marco

KNF


Revision tags: OPENBSD_3_8_BASE
# 1.2 10-Jul-2005 grange

Spacing cleanup.


# 1.1 02-Jun-2005 tholo

Start on a basic ACPI framework -- does not do much more than read out the
ACPI tables into kernel memory and attach ACPI and HPET timers currently.

In order to test this code, enabling the devices in GENERIC as well as
the ACPI_ENABLE option is needed. This code does not do any thermal
control yet, so this should be done with care depending on the platform.

In the tree so more people can contribute to making this more fully
featured.

Ok niklas@ grange@ tedu@


# 1.412 10-Feb-2022 visa

Embed klist head in acpi_softc to avoid explicit malloc.

OK kettenis@


# 1.411 09-Feb-2022 deraadt

Move x86-only stubs used by kern/subr_suspend.c into dev/acpi/acpi_x86.c
for sharing between i386 and amd64.
ok mlarkin kettenis


# 1.410 08-Feb-2022 deraadt

The suspend/resume code is a sticky mess of MI, MD, and ACPI sequencing.
This splits out the MI sequencing, backing it with per-architecture helper
functions. Further steps will be neccesary because ACPI and MD are too
tightly coupled, but soon we'll be able to use this code for more architectures
(which depends on figuring out the lowest-level cpu sleeping method)
ok kettenis


# 1.409 04-Feb-2022 robert

set the APM_BATT_CHARGING state if the battery is being charged and try
to calculate the remaining time to be fully charged

ok jca@


# 1.408 02-Feb-2022 deraadt

acpi_addtask() calls malloc() w/ M_NOWAIT (because some calls come from
interrupt context), this however means occasional resource shortage will
result in callbacks registration failing, and unknown consequences for
the task-submitting caller.
Changing this to use pools with a low water mark, decreases the odds
of that problem occuring.
ok kettenis


# 1.407 01-Feb-2022 deraadt

GPE_DIRECT is calling the AML parser from interrupt context, which is
not permitted. Luckily nothing is using GPE_DIRECT anymore, so this code
can be deleted.
ok kettenis


# 1.406 26-Jan-2022 kettenis

An ACPI device needs to be both present and enabled for it to function.
So only attempt to attach hardware that has both bits enabled. This fixes
an issue where com(4) would attach for a disabled serial port leading to
misdetection of the hardware variant and a subsequent hang when /etc/rc
runs ttyflags -a.

ok anton@, deraadt@


# 1.405 12-Jan-2022 patrick

Make acpi_getpropint() return uint64_t, as ACPI integers are in fact that
wide and some _DSD properties depend on it.

ok kettenis@


# 1.404 04-Jan-2022 patrick

acpi_getprop() needs to actually make sure that we're looking at the
correct property. While there adjust acpi_getpropint() as well to
increase similarity with acpi_getprop().

ok kettenis@


# 1.403 16-Dec-2021 anton

Attach com over acpi on amd64. Some hardware uses a different interrupt
assignment compared to the the legacy one supported by com over isa.
This causes the console to halt once userland takes over as no
interrupts are received. The actual address and irq can be read from
ACPI, kettenis@ already added support for arm64 which paved the way for
amd64.

Some consoles that previously attached over isa are now expected to
attach over acpi.

Thanks to patrick@ for testing on arm64.

ok kettenis@


# 1.402 07-Dec-2021 kettenis

Fix parsing of SR_IRQ resource descriptors.

ok patrick@, anton@


# 1.401 02-Nov-2021 deraadt

knf


# 1.400 30-Oct-2021 deraadt

Change hw.perfpolicy=auto by default, at startup. If the system has AC
power connected (default is yes when no driver differentiates) then default
to 100% performance. On battery, use the existing auto algorithm (which is
admittedly somewhat unrefined).
This change overrides the system/BIOS speed and puts OpenBSD in control.
As this happens very early during boot, besides speedups in all usage usage
patterns, some surprises: unhibernate and sysupgrade times are cut in half.
note: on a few architectures, the setperf fn pointer is changed late, and
thus the auto algorithm stops timeing out. kettenis and i will look for
a solution.
in snaps for more than a week.
ok kettenis


Revision tags: OPENBSD_7_0_BASE
# 1.399 20-Jul-2021 mlarkin

Fix trailing whitespace in a few places.

No code change.


# 1.398 09-May-2021 kettenis

Some x86 machines advertise the "hardware reduced" ACPI feature, advertise
S4 and S5 support, but fail to populate the SLEEP_CONTROL_REG and
SLEEP_STATUS_REG descriptions in the FADT. An example of such a machine
is the ASUS Zenbook 14 UM433DA. Any attempt to powerdown the machine
will result in a kernel crash. It turns out that using the legacy ACPI PM
registers works fine on this machien. So fall back on those registers
if SLEEP_CONTROL_REG and/or SLEEP_STATUS_REG aren't provided.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_9_BASE
# 1.397 15-Mar-2021 patrick

Add acpi_iommu_device_map(), which replaces the DMA tag with one that
is blessed with IOMMU magic, if available. This is mainly for arm64,
since on amd64 and i386 the IOMMU only captures PCIe devices, as far
as I know, which uses the pci_probe_device_hook(). This though is for
non-PCI devices attached through ACPI.

ok kettenis@


# 1.396 07-Mar-2021 patrick

Pass standard DMA tag to acpi(4) table drivers.

ok kettenis@


# 1.395 27-Dec-2020 deraadt

acpi_map_address() cannot be wrapped by SMALL_KERNEL anymore, as
it is used by acpihpet.c


# 1.394 25-Dec-2020 visa

Refactor klist insertion and removal

Rename klist_{insert,remove}() to klist_{insert,remove}_locked().
These functions assume that the caller has locked the klist. The current
state of locking remains intact because the kernel lock is still used
with all klists.

Add new functions klist_insert() and klist_remove() that lock the klist
internally. This allows some code simplification.

OK mpi@


# 1.393 17-Dec-2020 kettenis

Fix some issues with referencing named ACPI nodes from Packages.
These references need to be resolved at runtime rather than when
they're parsed such that they pick up the right values for those nodes
which can be changed when for example _INI methods run.

The current approach is to replace these reference with a string that
names the node in question. The problem with that is that packages
can also contain normal strings. Which means that we need special
code that depends on the context where the package is used.

This diff takes a different approach by retaining a reference when
parsing. Code that uses a package will need to resolve this reference
but there is no ambiguiety anymore.

ok patrick@


# 1.392 05-Dec-2020 kettenis

Add support for the _EVT method to the GPIO support code.

ok patrick@


Revision tags: OPENBSD_6_8_BASE
# 1.391 27-Aug-2020 jmatthew

Reorder the acpi attach process so we look at Processor() CPU nodes
after Device() ones, since we should prefer the newer node type.
If we see any Device() nodes, don't attach acpicpu(4) to any Processor()
nodes if they're also present. This also makes acpitz(4) and
acpipwrres(4) devices attach slightly later.

ok kettenis@, also tested by jmc@


# 1.390 16-Aug-2020 gnezdo

Remove sc_maxgpe from acpi_softc which was never read from

Remove a dead conditional from acpi_init_gpes while there.

ok jcs@


# 1.389 21-Jul-2020 deraadt

acpi can use IPL_BIO (a low interrupt) since it only enqueues operations for
later processing. The use of a high interrupt will predate suspend/resume
efforts, we had to redesign acpi to be non-reentrant obviously
discussed with kettenis, in snaps for more than a week


# 1.388 24-Jun-2020 cheloha

kernel: use gettime(9)/getuptime(9) in lieu of time_second(9)/time_uptime(9)

time_second(9) and time_uptime(9) are widely used in the kernel to
quickly get the system UTC or system uptime as a time_t. However,
time_t is 64-bit everywhere, so it is not generally safe to use them
on 32-bit platforms: you have a split-read problem if your hardware
cannot perform atomic 64-bit reads.

This patch replaces time_second(9) with gettime(9), a safer successor
interface, throughout the kernel. Similarly, time_uptime(9) is replaced
with getuptime(9).

There is a performance cost on 32-bit platforms in exchange for
eliminating the split-read problem: instead of two register reads you
now have a lockless read loop to pull the values from the timehands.
This is really not *too* bad in the grand scheme of things, but
compared to what we were doing before it is several times slower.

There is no performance cost on 64-bit (__LP64__) platforms.

With input from visa@, dlg@, and tedu@.

Several bugs squashed by visa@.

ok kettenis@


# 1.387 10-Jun-2020 jca

Give userland a consistent battery and A/C state after resume

Previously we refreshed the state of acpiac(4) and acpibat(4)/acpisbs(4)
by queueing fake ACPI events to be processed by the acpi thread.
acpibat(4) using ACPIDEV_POLL meant that its status could be refreshed
first, resulting in an APM_POWER_CHANGE event sent to userland before
the state of acpiac(4) was up to date. Because of this, apmd(8) could
see A/C unplugged when it was in fact plugged, possibly leading to
a wrong decision.

Instead of using fake ACPI events, refresh the status of acpiac(4) and
acpibat(4)/acpisbs(4) right away at DVACT_WAKEUP time. To reuse the
existing refresh code of those drivers, make their ACPI *_notify()
handlers responsible for sending APM_POWER_CHANGE events instead. Also
queue an APM_POWER_CHANGE event right after resume to retain existing
behavior.

This fixes the consistency of data shown to userland, and makes those
drivers less special.

Initially discussed last year (!) with mpi@ at Miod's place.
Feedback and ok jcs@, deraadt@ agrees with the use of DVACT_WAKEUP here.


# 1.386 29-May-2020 deraadt

dev/rndvar.h no longer has statistical interfaces (removed during various
conversion steps). it only contains kernel prototypes for 4 interfaces,
all of which legitimately belong in sys/systm.h, which are already included
by all enqueue_randomness() users.


# 1.385 14-May-2020 kettenis

Bring back the acpi(4) changes. This time around only use the ACPI _CRS
information on ACPI 5.0 and later.

ok krw@, patrick@


# 1.384 11-May-2020 jca

More accurate remaining power reporting with disparate batteries

Overall remaining power was computed as the average of the remaining
power of each battery, in percents. This doesn't work with batteries of
different capacity, so use the total capacity and remaining power
instead.

Reported by lidstah, successful tests by lidstah and benno@, ok kettenis@


# 1.383 08-May-2020 kettenis

Move parsing of _CRS into acpi(4) and pass the parsed address and interrupt
information in struct acpi_attach_args.

ok mpi@


Revision tags: OPENBSD_6_7_BASE
# 1.382 14-Apr-2020 kettenis

Implement acpi_getprop(), the ACPI equivalent of OF_getprop().

ok patrick@


# 1.381 12-Apr-2020 kettenis

Add support for the _CCA method. This method indicates whether DMA is
cache-coherent or not. To implement this, acpi(4) gets two bus_dma tags
and passes the appropriate one when attaching devices based on _CCA.
On i386/amd64, where for all practical purpose DMA is always cache-coherent,
the two tags are the same. But on arm64 they are distinct.

ok patrick@


# 1.380 07-Apr-2020 visa

Abstract the head of knote lists. This allows extending the lists,
for example, with locking assertions.

OK mpi@, anton@


# 1.379 03-Apr-2020 mpi

Fix incoherencies of errors returned by various *kqfilter routines.

EOPNOTSUPP is returned when there's no "filterops" for a given fd.
EINVAL when the requested filter isn't supported by the "filterops".
ENXIO means the underlying device is no longer valid/present.

ok visa@


# 1.378 20-Feb-2020 visa

Replace field f_isfd with field f_flags in struct filterops to allow
adding more filter properties without cluttering the struct.

OK mpi@, anton@


# 1.377 31-Dec-2019 visa

Use C99 designated initializers with struct filterops. In addition,
make the structs const so that the data are put in .rodata.

OK mpi@, deraadt@, anton@, bluhm@


# 1.376 31-Dec-2019 mpi

Convert infinite sleeps to {m,t}sleep_nsec(9).

ok kn@


# 1.375 31-Dec-2019 jsg

include sys/sensors.h for sensor use
don't depend on indirect inclusion from acpidev.h


Revision tags: OPENBSD_6_6_BASE
# 1.374 07-Sep-2019 kettenis

Revert acpipci(4) changes. Too many systems are broken. We'll try again
after 6.6 as been released. The acpireg.h change stays behind.


# 1.373 28-Aug-2019 kettenis

Use ACPI information to attach PCI busses like we do on arm64. There are a
few additional quirks though, and attaching the PCI busses is delayed to
replicate the existing code more closely. That may be changed in the
future. Also tweak how we handle MSI support and respect to ACPI flag
that says we shouldn't attempt to use MSIs.

Some fallout is expected.

ok patrick@


# 1.372 27-Aug-2019 deraadt

On amd64 the bootup and resume paths for SP/BP/SP were not performing
msr/pat, clock-startup, firmware upgrade, fpu initialization, and cpu
soft-state initialization in the same order. That kind of chaos is
crazy and fragile. Improve the acpi resume call sequence and unify order
of the operations.


# 1.371 02-Jul-2019 jcs

acpi_set_gpehandler: turn edge arg into flags, add support for
passing GPE_DIRECT which directly executes the supplied callback and
clears a level interrupt rather than going through acpi_taskq which
can be slow when other driver callbacks are in the queue.

This should not be used for GPE handlers that need to do anything
ACPI-related.

ok kettenis


# 1.370 10-Jun-2019 kettenis

Print proper ACPI version number.

ok mlarkin@


# 1.369 08-Jun-2019 kettenis

When establishing the mapping between ACPI device nodes and PCI devices,
skip PCI host bridges and devices that aren't there.

ok patrick@


# 1.368 07-Jun-2019 kettenis

Introduce acpi_getsta() and use it to eliminate some duplicated code.

ok patrick@


# 1.367 12-May-2019 kettenis

Make sure the extended PM register descriptions actually exist before
using them. Fixes machines with ancient ACPI 1.0 (such as recent
hypervisors).

ok deraadt@


# 1.366 12-May-2019 kettenis

Add support for mapping ACPI PM registers using the "extended" gasio
representation from the FADT. Mostly fixes the Lanner NCA-1510.

ok lteo@, mlarkin@, deraadt@


# 1.365 11-May-2019 lteo

Fix incorrect error message.

ok kettenis@


# 1.364 08-May-2019 tedu

free size. the allocation is the header plus the length.


Revision tags: OPENBSD_6_5_BASE
# 1.363 04-Apr-2019 kettenis

Avoid potential null-pointer dereference. Found by Kent R. Spillner.

ok deraadt@, jsg@


# 1.362 20-Jan-2019 jsg

fix SMALL_KERNEL build after changes in rev 1.361
reported by naddy@ ok deraadt@


# 1.361 19-Jan-2019 tedu

Add a pwraction sysctl that controls what the power button does on acpi.
By default, nothing changes -- shutdown is initiated. But allows turning
power button into a sleep button if desired.
(grudging) ok from a few parties


# 1.360 26-Oct-2018 kettenis

Add acpipci(4) on amd64. For now this only calls the PCI-specific _OSC
method to let the ACPI implementation know what features we support.


Revision tags: OPENBSD_6_4_BASE
# 1.359 30-Aug-2018 kettenis

Move softbutton check before the check that skips attaching certain acpi
devices. Fixes regression caused by matching on _CID in addition to
matching on _HID.

ok matthieu@, mlarkin@


# 1.358 25-Aug-2018 kettenis

Match ACPI devices based on _CID if no match for _HID is found.

ok mpi@


# 1.357 19-Aug-2018 kettenis

Add support for multiple PCI segments. Only really implemented for arm64
for now as amd64/i386 firmware still caters for legacy OSes that only
support a single PCI segment.

ok patrick@


# 1.356 03-Aug-2018 kettenis

Let ahci(4) match on _CLS instead of _HID when attaching at acpi(4). Avoids
having to add many more _HID entries to the match table.

ok deraadt@, mlarkin@


# 1.355 10-Jul-2018 kettenis

Make legacy interrupts work in acpipci(4).

ok patrick@


# 1.354 05-Jul-2018 kettenis

Add acpipci(4), a driver that supports generic ECAM-compatible PCI host
bridges based on information provided by ACPI.

ok mlarkin@


# 1.353 04-Jul-2018 kettenis

Properly pass around the PCI "chipset tag" in acpi(4) and refactor
acpimcfg(4) to call an MD initialization functions that sets up a tag for
PCI ECAM.

ok guenther@, mlarkin@, krw@


# 1.352 01-Jul-2018 kettenis

Move acpi(4) attach glue into acpi_machdep.c.

ok guenther@, deraadt@


# 1.351 01-Jul-2018 kettenis

Add support for _DSD Device Properties.

ok mlarkin@


# 1.350 30-Jun-2018 kettenis

Add intr_enable() function, intended for MI use to amd64 and i386 and use
this in the acpi(4) suspend/resume code paths.

ok deraadt@


# 1.349 29-Jun-2018 kettenis

Use <stdint.h> types.

ok mpi@, jung@, krw@, deraadt@


# 1.348 29-Jun-2018 mlarkin

ACPI: Allow (with warning) GPE handler reassignment, instead of returning
a failure code.

ok kettenis


# 1.347 26-Jun-2018 mlarkin

knf


# 1.346 26-Jun-2018 kettenis

Let acpi(4) pass down the bus dma tag.

ok deraadt@


# 1.345 25-Jun-2018 kettenis

Reorganize acpi(4) code a bit in preparation for upcoming arm64 support.

ok deraadt@


# 1.344 20-May-2018 kettenis

Wake up the acpi thread after scheduling a gpio event.

ok mpi@


# 1.343 17-May-2018 kettenis

Sprinkle some #ifndef SMALL_KERNEL such that RAMDISK kernels cuild again.

pointed out by stsp@


# 1.342 17-May-2018 kettenis

Implement GenericSerialBus OpRegion support.

ok mlarkin@


# 1.341 27-Mar-2018 kettenis

Add acpicmos(4), a driver that implements SystemCMOS OperationRegion
access support. This fixes machines where the AML doesn't check whether
support for this OperationRegion type has been registered by the OS.

ok mlarkin@


Revision tags: OPENBSD_6_3_BASE
# 1.340 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.339 10-Feb-2018 deraadt

Syncronize filesystems to disk when suspending. Each mountpoint's vnodes
are pushed to disk. Dangling vnodes (unlinked files still in use) and
vnodes undergoing change by long-running syscalls are identified -- and
such filesystems are marked dirty on-disk while we are suspended (in case
power is lost, a fsck will be required). Filesystems without dangling or
busy vnodes are marked clean, resulting in faster boots following
"battery died" circumstances.
Tested by numerous developers, thanks for the feedback.


# 1.338 08-Feb-2018 deraadt

After flushing disks, try again to release all memory. We don't need to
save dirty memory to the hibernate space.


# 1.337 08-Feb-2018 deraadt

Toss all releaseable memory, because fragmentation can get in the way
of allocating the hibernate playpen.


# 1.336 08-Feb-2018 deraadt

sr_quiesce() is a new approach for ensuring that softraid drains
output to the disks.

This is part of a larger suspend/resume filesystem-safety diff, which
has been worked on for a couple of months already. Tests by job, krw,
beck, benno, and others. Sometimes even by snapshot users...


# 1.335 29-Nov-2017 kettenis

Add support for GPIO-signaled events.

ok mlarkin@


# 1.334 16-Nov-2017 jcs

Add PCI attachment for dwiic(4) needed by Intel 100 Series machines

ok kettenis


# 1.333 14-Nov-2017 kettenis

Add a few more PNP IDs to the skip lists.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.332 17-Aug-2017 stsp

Make dwiic(4) attach its ACPI parent dependencies.
This change makes the Asus E200HA keyboard work.

Original analysis and patch by Cesare Gargano
ok kettenis@


# 1.331 09-Aug-2017 dcoppa

Add "PNP0303" (8042 PS/2 Controller) to acpi_isa_hids[]

ok kettenis@


# 1.330 22-Jul-2017 jcs

acpibat: look for _BIX first before falling back on _BIF

ACPI 4.0 deprecated _BIF for battery status so some newer machines
are exporting _BIX which extends _BIF's package with a few extra
fields. When using _BIX, export the new discharge cycle count as a
sensor.

feedback from kettenis and anton
ok deraadt


# 1.329 20-Jul-2017 kettenis

Start a list of isa(4) device names such that we can avoid printing them
as "not configured".

ok mikeb@, millert@, deraadt@


# 1.328 08-Apr-2017 deraadt

Sensors are run as callbacks inside tasks. During suspend a sensor
could be running inside a driver that will be force-detached, or due
to tsleep end up disrupting the softstate/hardstate contract. At
suspend time, quisce all these callbacks by waiting for completion.
This issue has never been observed for real, but may be implicated in
suspend/resume failures.
ok kettenis guenther mlarkin


# 1.327 08-Apr-2017 deraadt

Two prototypes for acpi_maptable; merge to one place


Revision tags: OPENBSD_6_1_BASE
# 1.326 27-Mar-2017 deraadt

Now that hibernate_alloc() only has clean success/failure, don't
need to call hibernate_free() to clean up a partial mess.
ok mlarkin kettenis


# 1.325 13-Mar-2017 deraadt

allow ramdisks to build; ok jcs


# 1.324 12-Mar-2017 jcs

try to attach acpisbs first and if successful, don't attach acpibat


# 1.323 02-Mar-2017 natano

Add a new sysctl machdep.lidaction. The sysctl works as follows:

machdep.lidaction=0 # do nothing
machdep.lidaction=1 # suspend
machdep.lidaction=2 # hibernate

lidsuspend is just an alias for lidaction, so if you change one, the
other one will have the same value. The plan is to remove
machdep.lidsuspend eventually when people have upgraded their
/ets/sysctl.conf.

discussed with deraadt, who came up with the new MIB name
no objections mlarkin
ok stsp halex jcs


# 1.322 28-Feb-2017 natano

Untangle abstract sleep modes from ACPI sleep states. This paves the way
for further work in the area.

idea and ok deraadt
ok mlarkin


# 1.321 22-Feb-2017 jcs

add acpisbs, an acpi smart battery subsystem driver reading data
over smbus

currently disabled because it conflicts with acpibat

ok deraadt, kettenis


# 1.320 14-Jan-2017 kettenis

Revert aml_rdpciaddr changes; breaks several machines that were working
before.


# 1.319 11-Jan-2017 jsg

Increase the size of the buffers used for _HID and _CID strings to be
able to fit "Hyper_V_Gen_Counter_V1".

ok mlarkin@ deraadt@


# 1.318 08-Jan-2017 kettenis

Make aml_rdpciaddr() work for for devices behind bridges. The code now
uses the mapping between AML nodes and PCI devices that we establish.
Because _INIT methods may end up calling aml_rdpciaddr(), make sure we
create that mapping early. Also handle devices that aren't actually present.
These devices are now included in the mapping and reads will return an
all-ones patterm whereas writes are a no-op..

ok mlarkin@


# 1.317 25-Oct-2016 pirofti

Add the resource index to aml_parse_resource() callback function.

This is needed by an upcoming acpiec commit that handles machines breaking
the current ACPI specifications.

Change suggested by and ok kettenis@, guenther@


# 1.316 18-Sep-2016 guenther

If the APIC table (MADT) isn't present then ci_acpi_proc_id won't be set
when acpicpu tries to attach, leading to a NULL dereference. The ACPI
processor object on the Dells where this was reported is bogus and useless
anyway so apply the same test when deciding whether to attach to processor
objects as acpicpu would when matching them to cpus.

Problem reported by Eivind Eide (xenofil (at) gmail.com) and
Johan Huldtgren (johan+openbsd-bugs (at) huldtgren.com).
Testing by Eivind Eide.
ok kettenis@ mlarkin@


# 1.315 03-Sep-2016 naddy

Write the system time back to the RTC every 30 minutes.
This fixes the problem that long-running machines which were not
shut down properly would reboot with a badly offset system time.

hints and ok kettenis@


# 1.314 31-Aug-2016 mlarkin

Don't put a device in the wake device list if _STA says it's not there.

Fixes at least one machine that woke up from sleep / poweroff immediately.

ok kettenis@, tested by kettenis and martijn@


# 1.313 28-Jul-2016 kettenis

Store the acpi processor ID/UID in struct cpu_info, and use it to attach
acpicpu(4) drivers to the right cpu(4).

ok mlarkin@, guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.312 10-Jun-2016 kettenis

The power management timer has been made optional in ACPI 5.0A, so don't
attempt to attach acpitimer(4) if the timer isn't present.

Pointed out by jcs@

ok jcs@


# 1.311 08-May-2016 kettenis

Make sure devices listed by a device's _DEP method are attached before
we attach the device itself. The _DEP methode, introduced in ACPI 5.0,
is a hint that the device in question depends on OpRegion support from
the devices listed by _DEP, so we have to order them the proper way.
To prevent us from attaching those devices again when we encounter them
later walking down the device tree, keep track of our attempts to
attach a device driver to them using aflag in the aml_node structure.

ok guenther@


# 1.310 25-Apr-2016 pirofti

We attach acpihpet(4) and acpiec(4) without using the HID, so do not
report it in the dmesg.

OK deraadt@, mlarkin@.


# 1.309 02-Apr-2016 kettenis

Reduce dmesg spam by knocking out some acpi hardware IDs that don't need a
driver.

ok mpi@


# 1.308 29-Mar-2016 kettenis

Check if a device is present (using _STA) in acpi_foundhid. This prevents
us from reporting devices as "not configured" that aren't actually present,
and allows us to remove duplicated code from several drivers.

ok deraadt@, ok jsg@


# 1.307 29-Mar-2016 kettenis

Implement a cleaner way to prevent acpivout(4) from attaching if we have
a platform-specific method of controlling the backlight.

ok millert@, mlarkin@, jung@


# 1.306 28-Mar-2016 kettenis

Adding each and every new acpi device driver to acpi_foundhid() is
gettinga bit of the burden. And it really isn't how our autoconf
framework is supposed to work for busses that can be enumerated. All
the drivers already check for a matching _HID in their attach
function. So we can just drop the checks in acpi_foundhid() and let
the drivers sort themselves out.

Print a "not configured" line for devices for which we don't attach adriver.
This may be a bit much as there are devices for which we will not have a
driver. This will be removed if it causes too much dmesg spam.

ok deraadt@, mlarkin@


Revision tags: OPENBSD_5_9_BASE
# 1.305 17-Jan-2016 jsg

Take acpi_foundhid() outside of #ifndef SMALL_KERNEL so the ramdisk
kernel can find and attach dwiic(4) and sdhc(4).


# 1.304 17-Jan-2016 jsg

remove an unused global variable


# 1.303 14-Jan-2016 kettenis

Attach dwiic(4) on the Intel Bay Trail i2c controllers as well.


# 1.302 12-Jan-2016 kettenis

Improve emulation of PM registers on Hardware-redcued ACPI by emulating
SPL_TYPx, SLP_EN and WAK_STS using the new SLEEP_STATUS_REG and
SLEEP_CONTROL_REG registers. Makes the transition into S5 (powerdown) work
on the ASUS X205TA.

ok deraadt@, mlarkin@


# 1.301 12-Jan-2016 jcs

Add dwiic, a driver for the Synopsys DesignWare i2c controller found
on the Samsung ATIV Book 9 laptop. This initial version only
supports ACPI config/attachment.

Add ihidev, a HID-over-i2c driver largely based on uhidev. dwiic
handles attaching ihidev devices found in ACPI.

Add ims, a HID-over-i2c mouse/trackpad driver to get basic cursor
and button functionality from HID-compliant i2c trackpads.

ok kettenis deraadt


# 1.300 11-Jan-2016 kettenis

Make sdhc(4) attach to hardware IDs "80860F14" and "PNP0FFF".


# 1.299 09-Jan-2016 kettenis

Initial support for Hardware-Reduced ACPI. This skips the legacy mode checks
by emulating the PM_CNT register such that SCI_EN is always enabled. It also
skip attaching acpitimer(4) since the relevant registers are no longer there.

ok deraadt@, mlarkin@


# 1.298 06-Jan-2016 kettenis

Follow the spec and check the SCI_EN bit to see if ACPI has already
been enabled and don't bother enabling it again if it is. This should fix
systems that don't implement legacy mode and therefore set ACPI_ENABLE and
ACPI_DISABLE to zero.

ok mlarkin@, yasuoka@


# 1.297 23-Nov-2015 reyk

Back out the CRITICAL change, turn it back from 10% to 25%. Some
people with old/broken batteries didn't like this change, and the
underlying heuristics are wrong.


# 1.296 22-Nov-2015 reyk

The "CRITICAL" APM battery level, that is exported/faked by the ACPI
stack, is currently set to 25%. With 25% battery left, a typical
modern laptop will have at least one hour runtime left. It is an
arbitrary value, and other APM power layers in the kernel set CRITICAL
to something like 5% or 10%, so set it to 10% for now.

OK mlarkin@


# 1.295 28-Sep-2015 deraadt

In low-level suspend routines, set cold=2. In tsleep(), use this to
spit out a ddb trace to console. This should allow us to find suspend
or resume routines which break the rules. It depends on the console
output function being non-sleeping.... but that's another codepath which
should try to be safe when cold is set.
ok kettenis


# 1.294 06-Sep-2015 deraadt

clarify malloc arguments (base + length...)


# 1.293 28-Aug-2015 deraadt

fairly simple sizes for free(); ok tedu


# 1.292 20-Aug-2015 kettenis

Respect the access size when reading or writing to pci config space.
This version also makes sure writes are properly aligned, preventing
the panics seen with the previous incarnation of this change.

Fixes battery status passthrough in vmware, brightness keys on my x220
and who knows what else.

ok mlarkin@, deraadt@


Revision tags: OPENBSD_5_8_BASE
# 1.291 04-Aug-2015 deraadt

If we are setting ACPI_PM1_BM_RLD do it only once, and remember to
repeat this action at resume time.
ok mlarkin guenther


# 1.290 29-Jul-2015 deraadt

Something subtle in previous revision is locking up suspend or booting
on some laptops. Mailing lists threads mention x220 and Asus machines.
Reverting for now.


# 1.289 21-Jul-2015 kettenis

iRespect the access size when reading or writing to pci config space.
Fixes battery status passthrough in vmware and who knows what else.

Tested by many.

ok mlarkin@, deraadt@


# 1.288 17-Jul-2015 mlarkin

fix comments


# 1.287 26-Mar-2015 jsg

Use the 0x10 extended capability to determine if the trackstick
buttons are wired to the trackpad and need to be re-routed to the
trackstick. Without this change the buttons on 2015 thinkpads
get picked up as extended buttons that show up as scroll up/down.

Remove the X1 Carbon 2015 (LEN0048) and X250 (LEN0046) from the
top button area/soft buttons quirks list. Also avoid using the
quirk list entirely if the capability bit is set.

Based on work from jcs@ with feedback from mpi@
ok mpi@ jcs@


# 1.286 20-Mar-2015 kettenis

The ACPI standard says that a PCI function number of FFFF can be used to refer
to all functions on a device. This makes no sense in the context of accessing
PCI config space. Nevertheless stsp@ has a machine with AML that tries to
do config space reads from a device where _ADR encodes function number FFFF.
This is almost certainly buggy AML, but since Linux (and presumably Windows)
boot just fine on this machine we need to work around this. Linux effectively
treats function FFFF as function 7, which isn't present on this machine.
Instead we simulate a read from a non-existent device by returning a bit
pattern of all ones. Writes to function FFFF will still panic.

ok mlarkin@


# 1.285 16-Mar-2015 deraadt

Reduce use of SMALL_KERNEL in acpi, bringing acpiec driver and more gpe
handling into RAMDISK. This is now possible because the install media
has ample room. The goal is to reduce special cases where we may be
skipping (unknown) important operations...
ok mlarkin kettenis


# 1.284 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.283 07-Feb-2015 deraadt

New framework that allows hibernate to pass in entropy from it's fresh
boot.
ok mlarkin


# 1.282 06-Feb-2015 mlarkin

Fix a hibernate crash on some machines due to unmapping a page that
may not have been mapped previously (in the failure to hibernate case).

Also ensure that the lowmem ptp is mapped in all cases (not just MP).

ok kettenis


# 1.281 17-Jan-2015 deraadt

resume_randomness() before spinning up other cpus...


# 1.280 15-Jan-2015 jsg

Define a new wscons mouse type for Synaptics clickpad devices that lack
physical buttons. This will be used if the acpi pnpid for the mouse
matches a list provided by Synaptics found in Linux.

Combined with the xenocara changes this will remove the need for an xorg
config file for the x240/t440/t540 generation of thinkpads.

ok matthieu@ shadchin@ kettenis@


# 1.279 11-Jan-2015 kettenis

Tolerate checksum errors in ACPI tables (but complain about them). Apparently
there are machines out there with broken BIOSen with such checksum errors in
thier RSDT tables. Windows runs fine on these machines, so it probably does
not check. Linux ignores checksum errors as well (and prints a warning).

ok deraadt@, krw@


# 1.278 18-Dec-2014 deraadt

suspend and resume the RNG (at what looks like the right spots)
ok reyk djm


# 1.277 09-Dec-2014 doug

Replace some malloc(n*size,...) calls with mallocarray().

ok tedu@ deraadt@


# 1.276 06-Dec-2014 mpi

If "machdep.lidsuspend" is not set, do not go back to sleep if the lid
is still closed when resuming. This allows docked laptop to be resumed
without opening them.

ok mlarkin@


# 1.275 23-Nov-2014 mlarkin

If a machine has no _LIDs, then none can be open. This diff fixes a problem
where machines without any _LIDs were going back to sleep after hibernate
due to the recent suspend-if-lid-closed-on-resume diff. Noticed on qemu,
which doesn't have any _LIDs.

discussed with deraadt


# 1.274 18-Nov-2014 krw

Nuke yet more obvious #include duplications.

ok miod@


# 1.273 08-Nov-2014 mlarkin

If resuming from sleep (zzz/ZZZ) and the lid is still closed, go back to
sleep. This prevents accidental lid flex or slight opening in a backpack
from waking the machine up and leaving it resumed (powered on).

ok deraadt@


# 1.272 26-Sep-2014 kettenis

Rework piglet and pig allocation. Currently the piglet gets allocated
deep down in the suspend path, where it is really hard to recover from
allocation failure. So allocate the piglet early on in the suspend path.
Also change the piglet and piglet allocation functions to use km_alloc(9)
instead of doing pmemrange magic. This removes a bunch of code which, in the
case of the piglet allocation, is broken since it results in a NULL pointer
dereference. Also switch the piglet allocation to not wait. If we can't
allocate 16MB of phys contig memory on a halfway modern machine we're almost
certainly under a lot of memory pressure and we're better off not trying to
hibernate anyway.

ok mlarkin@


# 1.271 25-Sep-2014 kettenis

Bring back rev 1.249. Now that mp_setperf() has been fixed, it should be
safe to restore perflevel when resuming. Should fix the issues people
see with the processor running at full speed heating up the machine while
the apm command reports that the CPUs are running at a low speed.

ok dcoppa@, deraadt@


# 1.270 23-Sep-2014 kettenis

Release the acpi lock around calls to wsdisplay_suspend() and
wsdisplay_resume(). The X server may adjust the display brightness just
before/after the VT switch when suspending/resuming. In fact, it may
already be blocked adjusting the display brightness for some reason
unrelated to the suspend request.

The VT switch request will eventually time out, but the delay is annoying
and the failed VT switch will lead to problems after resume.

Needed for a future update of the xf86-video-intel driver.

ok mlarkin@


# 1.269 19-Sep-2014 kettenis

Use config_suspend_all(9).


# 1.268 08-Sep-2014 kettenis

Clear the PME Status bit on PCI devices in response to Device Wake events.
Fixes the interrupt storm on the 2009 Mac mini with WOL enabled on its
nfe(4) interface.

ok mlarkin@


Revision tags: OPENBSD_5_6_BASE
# 1.267 20-Jul-2014 mlarkin

Support hibernating to softraid crypto volumes.

much help and ok from deraadt@


# 1.266 16-Jul-2014 mlarkin

Reenable hibernate RLE support and flush+zero all memory after unpack.


# 1.265 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.264 12-Jul-2014 kettenis

Add a function to drop all clean pages on the page daemon queues and call
it when we hibernate.

ok mlarkin@, miod@, deraadt@


# 1.263 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.262 11-Jul-2014 mlarkin

Flush the buffercache to 16MB on hibernate and restore its previous max
size (kern.bufcachepercent) on resume, for better hibernate performance.

ok beck@


# 1.261 10-Jul-2014 blambert

remove unneeded header

ok mlarkin@


# 1.260 01-Jul-2014 kettenis

Adding an additonal delay after each access to SystemMemory or SystemIO
during autoconf makes no sense.

ok deraadt@


# 1.259 23-Jun-2014 kettenis

SystemMemory is used to access memory mapped registers on some machines, so we
must use the bus_space(9) API to access it instead of memcpy(9). Also make
sure we properly align access to fields that need more than byte alignment.

ok guenther@


# 1.258 21-May-2014 mlarkin

Fix invalid / missing format specifiers in several dnprintf statements
(used only when ACPI_DEBUG is enabled)

From a diff on tech@ by Fabian Raetz, with a minor change by me, tested
on i386 and amd64 with ACPI_DEBUG enabled and disabled.


# 1.257 25-Apr-2014 mlarkin

Defer the free of the hibernate structures until later, when it is safe to
do so.

Freeing the hibernate structures too early leads to inadvertent calls into
splx when we aren't ready for them yet. Found when trapping splx calls in
the resume path.

discussed with deraadt@ at m2k14


# 1.256 13-Apr-2014 mlarkin

Fix a badly worded debug message that implied the entire machine was going
to sleep, instead of just the acpi thread going to sleep.


# 1.255 31-Mar-2014 mpi

Change the order of operations during a suspend/resume cycle and
call bufq_quiesce() after executing the DVACT_QUIESCE handlers.

This should be safe since no disk nor controller drivers have such
handler but it will allow us to detach sd(4) devices attached to a
USB bus.

Another benefit pointed out by kettenis@ is that drivers that need
to read a firmware from the disk should be able to do it at resume
time in a DVACT_WAKEUP handler.

ok kettenis@, deraadt@


# 1.254 13-Mar-2014 dlg

get rid of the assumption that the head of the alldevs list is the
"mainbus" device. this breaks when mpath is enabled because it
attaches before mainbus and therefore takes the head position.

have autoconf provide device_mainbus() which looks up mainbus_cd,
and use that instead.

discussed with deraadt who just wants mpath stuff to move forward
despite there being many ways to shine this particular turd.


Revision tags: OPENBSD_5_5_BASE
# 1.253 21-Feb-2014 deraadt

Do the lid control further on in the resume sequence, since this is
AML, and will do WAITOK operations. While cold, that is a bad idea.
We seem to be safe with respect to the spec, I do not see any event
debounce issues here.
ok kettenis, tested by many


# 1.252 20-Jan-2014 claudio

Instead of checking sc->sc_revision <= 1 check that the
fadt->hdr_revision <= 1. This is a better check since it checks
the version of the proper descriptor. This allows the alix APU
to reboot since those boards have currently a wrong sc->sc_revision.
Committing this now so that it can be tested easier.
With and OK jsg@ also fine by derradt@


# 1.251 24-Dec-2013 kettenis

If the FADT has its SMI_CMD set to zero, assume we're only ACPI-only hardware
and don't need to disable SMI ownership of the ACPI hardware registers.

ok mlarkin@


# 1.250 23-Dec-2013 kettenis

Revert previous commit. It makes resume hang on some MP systems running
GENERIC.MP.


# 1.249 16-Dec-2013 kettenis

Restore hw.setperf upon resume like we do for apm(4) on i386. Seems we need
to do this fairly late in the resume to avoid hangs.

ok deraadt@, mlarkin@


# 1.248 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


# 1.247 06-Nov-2013 mpi

Add support for Power Resources for Dx states and the necessary hook
for PCI devices. This hook should be called twice, before and after
changing the power state of a PCI device.

Before setting the device to the new state, the ACPI layer will notify
every power resources linked to the device for that state and make sure
they are turned "_ON". After changing the state of the device, it will
decrement the reference of every power resources linked to that device
for the old state and turn them "_OFF" if they are no longer referenced.

This fixes the no-USB after resume problem seen on various ThinkPad,
problem initialy diagnosed with Alexander Polakov.

ok kettenis@, deraadt@


Revision tags: OPENBSD_5_4_BASE
# 1.246 01-Jun-2013 mlarkin

Stop printing ACPI wakeup devices in dmesg after the 16th wakeup device.

Helps with VMware VMs that report hundreds of wakeup devices.

ok reyk@, deraadt@


# 1.245 31-May-2013 bluhm

Fix the build for a kernel without wd and pciide in its config.
OK deraadt@ tedu@


# 1.244 30-May-2013 deraadt

Enforce ca_activate tree-walks over the entire heirarchy for all events,
cleaning up some shutdown-hook related code on the way.
(A few drivers related to sparc are still skipped at kettenis' request)
ok kettenis mlarkin, tested by many others too


# 1.243 18-Apr-2013 deraadt

pin the acpi thread to the BSP, because AML and SMI cannot be trusted. I
was certain we were doing this already, but it turns out no...


Revision tags: OPENBSD_5_3_BASE
# 1.242 24-Dec-2012 guenther

In acpiioctl(), Return though the splx() path if suser() returns an error

ok deraadt@


# 1.241 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


# 1.240 04-Oct-2012 ehrhardt

Use information provided by ACPI to attach secondary PCI host bridges.

ok kettenis@


# 1.239 07-Sep-2012 kettenis

Add a new function called acpi_pci_min_powerstate() that returns the lowest
power state for a PCI device for the power state (currently S3 or S4) we're
transitioning into. This function is probably too simplistic; there is a lot
of (silly) complexity in the ACPI spec.

ok deraadt@


Revision tags: OPENBSD_5_2_BASE
# 1.238 13-Jul-2012 pirofti

Disable acpivideo(4) on ASUS laptops.

There are some models that panic whenever an event is received.
Some cope, some don't. In order to get this right acpiec(4) requires
major rework. Until then let machines boot.

Discussed with deraadt@.


# 1.237 11-Jul-2012 mlarkin

for now, restrict hibernate to root in the acpi ioctl kernel interface.
normal rules for controlling this interface through apmd still apply.

ok deraadt@


# 1.236 09-Jul-2012 deraadt

tiny little oops in untested hibernate block


# 1.235 09-Jul-2012 deraadt

clean up ioctl() case statements


# 1.234 09-Jul-2012 deraadt

acpi_disable_onegpe() is not used; ok pirofti


# 1.233 24-May-2012 kettenis

If _MIN > _MAX consider a WordBus resource to be invalid. Fixes a panic on
the IBM x3550-M4.

ok mikeb@


# 1.232 29-Mar-2012 mlarkin

Missing #ifdef in previous diff


# 1.231 29-Mar-2012 mlarkin

Prevent hibernation attempts on machines with unsupported disk controllers.
The same check is also performed later in the hibernate sequence, but by
then it's too late to cleanly unwind (presently).

ok deraadt


# 1.230 26-Mar-2012 deraadt

simplify conditionals in acpi_sleep_state()
ok mlarkin


# 1.229 26-Mar-2012 deraadt

hook in the hibernate request code; half of this diff is from mlarkin
ok mlarkin


Revision tags: OPENBSD_5_1_BASE
# 1.228 20-Sep-2011 deraadt

Very early in suspend while the clock is still ticking realtime,
save the clock back to the rtc. The zaurus already did this.
ok phessler miod kettenis


Revision tags: OPENBSD_5_0_BASE
# 1.227 02-Jul-2011 nicm

kqueue attach functions should return an errno or 0, not a plain 1. Fix
the obvious cases to return EINVAL and ENXIO.

ok tedu deraadt


# 1.226 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.225 16-Jun-2011 pirofti

Import acpitoshiba driver.

This is originally written by Hiroyuki Aizu, ported and integrated
in our tree by Javier Vazquez <javama4 AT gmail> with tweaks and
suggestions by me.

Import prodded by deraadt@ and the lack of testers on tech@.


# 1.224 27-Apr-2011 jcs

attach acpithinkpad to newer lenovo models like the x120e

ok deraadt@


# 1.223 22-Apr-2011 jordan

Fix uninitialzied variables and formatting strings (-Wxxx errors)


Revision tags: OPENBSD_4_9_BASE
# 1.222 02-Jan-2011 jordan

Re-add code for ACPI taskqueue, eliminates need for state vars in acpi softc
Serialize processing ACPI tasks for sleep/powerdown/GPE event
ok deraadt@


# 1.221 31-Oct-2010 guenther

Revert last commit: it breaks resume on Thinkpads
"then please back it out" deraadt@


# 1.220 26-Oct-2010 jordan

Add task queue for ACPI gpe and notify handlers
This fixes eject on Dell Latitude dock and an issue on the Dell Mini
battery update.
ok deraadt


# 1.219 07-Oct-2010 jordan

Create separate functions for enabling wake and runtime gpes.
Remove old suspend/resume gpewalk function
ok deraadt, mlarkin


# 1.218 05-Oct-2010 jordan

split acpi_enable_onegpe to separate enable/disable functions
ok deraadt


# 1.217 05-Oct-2010 jordan

Added acpi_disable_allgpes function to clear GPE status
ok deraadt


# 1.216 05-Oct-2010 deraadt

clean up acpi_softc use more


# 1.215 05-Oct-2010 deraadt

delete a bit of #if 0 code


# 1.214 05-Oct-2010 deraadt

hide use of our global acpi_softc pointer a bit better


# 1.213 29-Sep-2010 deraadt

grab the acpi thread lock very early in acpi thread startup; not that it
is likely that anyone will race us... but why be careless.
ok kettenis


# 1.212 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.211 29-Aug-2010 jasper

- add a check for FWRITE here too.

ok miod@


# 1.210 27-Aug-2010 kettenis

Make PCI Power Management optional, and only enable it when acpi(4) attaches.

ok deraadt@


Revision tags: OPENBSD_4_8_BASE
# 1.209 08-Aug-2010 kettenis

Close race between the acpi thread and wscons ioctls running in process
context. We might rip this out post-release and replace it with a solution
that serializes things in a more obvious way.

ok deraadt@, miod@ and "those crazy texans who ok anything"


# 1.208 08-Aug-2010 deraadt

Report the acpi sleep states when on a RAMDISK; ok krw


# 1.207 07-Aug-2010 kettenis

Enforce acpi register access rescrictions.

ok mlarkin@, deraadt@, tested by marco@


# 1.206 07-Aug-2010 deraadt

upon resume, notify all the acpiac and acpibat drivers to update
their status. some laptops do not do this automatically.
ok kettenis canacar


# 1.205 06-Aug-2010 marco

Always call _PSW on _LID devices that support it to enable lid open to
resume the machine. Conversely disable it when the machine wakes up.

Tested by several ok deraadt


# 1.204 06-Aug-2010 deraadt

As part of a revamp of the PM1/GPE code, I write this diff to look for
unmanaged EN & STS bits in the PM1 register at interrupt time and report
them. As a side effect this splits the STS acknowledgement into two writes
(for power, and sleep) instead of one. The printf that is added (to spot
unmanaged STS bits) has not yet been triggered as far as we know.

Before the "write to PM1 registers at the right offset" diff went in, this
was not neccessary. But newer thinkpads do not have a working soft power
button without this diff.

We have no idea why.
ok mlarkin kettenis


# 1.203 05-Aug-2010 deraadt

PM1 power button events were generating 'fake events' to acpibtn PWRB,
but if there is no such device these were being tossed. Instead, arrange
for all power button handlers to flag a softc variable which is processed
by the thread; so that one place will know to request a powerdown (psignal
to init)
ok marco mlarkin


# 1.202 05-Aug-2010 deraadt

rename a variable which is annoyingly poorly named


# 1.201 05-Aug-2010 mlarkin

Write to the correct offset for various ACPI pmreg registers. Writing to
the correct register does properly clear the wake status bits and fixed
event bits, which allows some machines to have more than 1 suspend cycle.

bug spotted by deraadt@

tested by deraadt@ and myself on a half-dozen machines and others on
hackers@

ok deraadt@, marco@


# 1.200 05-Aug-2010 deraadt

Fix the power button. The pm1 register is 16 bits in size.
My mistake, a while back.
tested by krw


# 1.199 04-Aug-2010 kettenis

Print supported sleep states.

ok marco@, phessler@, deraadt@


# 1.198 03-Aug-2010 mlarkin

Use the proper flag for re-enabling certain hardware events (power btn,
sleep btn). The (incorrect) flag prevoiously used coincidentally had the
same value, so this shouldn't cause any different behavior than before.

tested on a variety of machines (i386, amd64, sp, mp) - no behavioral
change seen

ok deraadt@


# 1.197 28-Jul-2010 marco

Make all gasio failures look and behave the same.


# 1.196 28-Jul-2010 mlarkin

Delay a little longer on suspend before giving up. Fixes machines that
sometimes don't suspend because the sleep transition takes too long
(symptoms like failing to sleep but the backlight stays on).

ok deraadt@


# 1.195 27-Jul-2010 deraadt

remove XXX; EC can now be attached early when needed; ok jordan


# 1.194 27-Jul-2010 deraadt

more brackets feels good


# 1.193 27-Jul-2010 deraadt

XXX annotate a block in acpi_enter_sleep_state() that is pretty worrying


# 1.192 27-Jul-2010 deraadt

horrific indentation that nearly made me puke


# 1.191 27-Jul-2010 mlarkin

Set the sleeping indicator light on machines that support it, in case SMI
doesn't do it for us. Shows the 'moon' LED on older Thinkpads when
sleeping.

Tested on T43p, W500, T510 by me and on X61s by deraadt@.

ok deraadt@


# 1.190 27-Jul-2010 jordan

Early initialization of acpiec if ECDT table exists
Fixes hang when booting thinkpads while docked
ok deraadt


# 1.189 26-Jul-2010 deraadt

remove merge error which was commited


# 1.188 26-Jul-2010 pirofti

Add support for Sony ACPI hotkeys via a new driver: acpisony(4).

Currently it only works for the suspend button and tries to do right for
the brightness events, but I haven't found a Sony laptop that like to do
right so far.

In the future I want to make the brightness keys work on all Sony's and
also add support for the zoom hotkeys and whatever other funky keys I can
find on those things.

Okay deraadt@.


# 1.187 22-Jul-2010 deraadt

pretty large cleanup of SMALL_KERNEL


# 1.186 21-Jul-2010 deraadt

in the acpi GPE handler, late ack level interrupts not edge interrupts.
Duh.


# 1.185 21-Jul-2010 deraadt

Merge the edge and level gpe handlers into one (now possible since the
gpe structure tells us whether the gpe is edge or level)


# 1.184 21-Jul-2010 deraadt

delete #if 0 code we are not going to use; ok marco


# 1.183 20-Jul-2010 deraadt

make the mask code for acpi_enable_onegpe() match other similar code


# 1.182 20-Jul-2010 deraadt

Handle the acpi interrupt controller with proper edge/level handling,
wrapped in spltty to avoid racing against the interrupt controller.
Repair the gpe bit masking code while there, and do operations in the
right order
ok kettenis mlarkin, help from jordan at figuring out the order of
operations.


# 1.181 20-Jul-2010 matthew

When we're preparing to go to sleep, only call bufq_restart() if other
preparations failed.

ok kettenis@, thib@, deraadt@


# 1.180 19-Jul-2010 deraadt

permit compilation with ACPI_DEBUG


# 1.179 19-Jul-2010 deraadt

there is no need for a seperate dsdt_softc pointer; dsdt does not have a
softc but is just a part of the single acpi driver.
ok jordan


# 1.178 19-Jul-2010 deraadt

acpi_delay() is no longer needed; ok jordan


# 1.177 18-Jul-2010 mlarkin

Clear event status on resume, to avoid going back to sleep/reboot/shutdown
immediately after resume. Tested by a bunch of people on hackers@ on
various hardware. Fixes at least Toshiba NB200 and Gateway LT3103u.

ok marco


# 1.176 13-Jul-2010 deraadt

rename some thread related interfaces so they are logical


# 1.175 10-Jul-2010 jordan

Verify _ADR bdf is correct (some systems return 0xffff)
Works on Dell x9xx and Studio 1555


# 1.174 09-Jul-2010 jordan

Clear the noise


# 1.173 08-Jul-2010 jordan

Add mapping for ACPI device to PCI bdf (match autoconf tree)
Simplify resource parsing function to use buffer argument
Convert namespace linked lists to use queue macros
ok marco@, deraadt@


# 1.172 06-Jul-2010 deraadt

Use spltty() to lock downcalls from apm against the information being
modified by the acpi thread.
ok kettenis


# 1.171 06-Jul-2010 deraadt

Don't use a workq to do the suspend, because it races aginst the acpi
thread. Instead, just tell the acpi thread to do the suspend for us.
This makes apmd & zzz work correctly. While here, have acpithinkpad
attempt to post the event to apm before prompting the suspend itself.
ok kettenis marco mlarkin


# 1.170 05-Jul-2010 mlarkin

Reset register SCI_EN on resume. The ACPI spec clearly states we don't have
to do this, but many machines don't, or forget to, which leads to all sorts
of problems on resume (broken battery stats, overheating, failure to resume
more than once, etc).

tested by many, ok marco@, deraadt@


# 1.169 01-Jul-2010 thib

do bufq quiesce again, since vnd/the bufq_done call in
biodone has been fixed.


# 1.168 01-Jul-2010 jordan

Backout recent AML changes, breaks on R210 and others


# 1.167 01-Jul-2010 jordan

Simplified aml_parse_resource call, cleanup code
ok marco


# 1.166 01-Jul-2010 jordan

Fix subordinate bus # for multi-root PCI buses
ok marco, kettenis


# 1.165 30-Jun-2010 thib

Disable/partially backout the bufq quiesce changes as this
is causing havoc with vnds and release must be buildable.


# 1.164 29-Jun-2010 deraadt

Fix ramdisks (new code was placed inside a SMALL_KERNEL chunk)


# 1.163 29-Jun-2010 jordan

Add support for mapping ACPI to PCI devices
ok kettenis, deraadt


# 1.162 29-Jun-2010 kettenis

Call bufq_quiesce() on suspend to stop all I/O and bufq_restart on resume.
Makes suspend/resume work much better while doing disk I/O.

ok mlarkin@


# 1.161 29-Jun-2010 tedu

60 is a more reasonable guess for number of minutes in an hour than 100.
spotted by TeXitoi. ok deraadt


# 1.160 27-Jun-2010 jordan

Add _acpi_matchhids routine, precursor to pci device match


# 1.159 27-Jun-2010 mlarkin

Clear ACPI fixed event status on resume (power buttons/etc) so that some
machines don't immediately resume after suspending to S3.

Tested by kettenis@ and I on 5 or 6 machines, deraadt@ "yeah, looks good"


# 1.158 27-Jun-2010 jordan

Cleaned up acpi_maptable, adds to table list directly


# 1.157 07-Apr-2010 deraadt

If we have done a wsdisplay_suspend(), and ACPI decides it does not want to
suspend, we must roll back by calling wsdisplay_resume() or the virtual
console switching will be locked forever
ok oga


# 1.156 07-Apr-2010 kettenis

Make suspend/resume work on MP machines (running an MP kernel). Joint work
from mlarkin@ and me, with some amd64 fixes thrown in by deraadt@

ok marco@, deraadt@, pirofti@, mlarkin@


# 1.155 31-Mar-2010 kettenis

Bring /dev/apm support in line with apm(4). Make the suspend button send
a suspend request event to apmd(8) instead of suspending immediately. Also
keep track of whether /dev/apm and /dev/apmctl are currently open, such that
we can still suspend immediately if apmd(8) isn't running.

ok deraadt@, marco@, pirofti@, jsing@, oga@


# 1.154 30-Mar-2010 oga

Prevent the apmd/x races for good.

When we hit suspend time, go through all wsdisplays on the system. if
they are in mode MAPPED, but not MODE_DUMBFB then if possible do a full
vt switch to a !mapped vt, and prevent switching back until resume time.
This has to be called from MD code because this involves userland
running so that X can run the vt switch signal handler. This way, any
case where we are using the "poke registers from userland" model, we
will not be on the hardware when we go down, so the kernel can actually
handle thing properly.

Tested on several acpi laptops (by kettenis@ and ian@), x40 (me and
beck@ at LEAST) and zaurus (me). Maybe others, but if so I forgot who at
this time..

Idea from deraadt somewhere over the Faroe Islands (I thought of a
similar thing myself a while ago). Much prompting from him. Ok and
comments miod@


# 1.153 25-Mar-2010 oga

Fix & vs. && in the kqfilter code.

Based on a comparison with the apm code.

ok deraadt@, kettenis@


Revision tags: OPENBSD_4_7_BASE
# 1.152 26-Nov-2009 mlarkin

Visual (and on some machines, audio) feedback while the machine is
resuming via calls to aml _SST method. This also makes the "moon" LED on
thinkpads to blink during resume and cease when resume is completed.

ok pirofti@


# 1.151 26-Nov-2009 deraadt

Toggle into "cold" when we we disable interrupts, because tsleeping
in the AML interpreter and context switching into userland processes
tends to sometimes pop the ACPI trampoline page out of our address
space. Makes lots more machines work.
ok kettenis mlarkin


# 1.150 24-Nov-2009 jsg

in the resume path evaluate _WAK before dealing with device drivers
like the spec says, not after.

ok mlarkin@


# 1.149 24-Nov-2009 deraadt

abort suspends on SMP systems until that gets written; ok mlarkin


# 1.148 23-Nov-2009 mlarkin

pci should return the result of it's children's suspend/resume calls back
to its parent.

handle suspend failure case and unwind if devices have an activate function
that returns failure.

ok deraadt@, kettenis@


# 1.147 23-Nov-2009 mlarkin

acpi* devices need to call their children on suspend/resume.
ok deraadt@


# 1.146 23-Nov-2009 pirofti

Remove ACPI_SLEEP_ENABLED checks.

This enables by default the suspend/resume paths in the kernel.

Okay deraadt@.


# 1.145 23-Nov-2009 deraadt

knf and spacing that are super annoying


# 1.144 23-Nov-2009 mlarkin

Hook up Theo's new children suspend/resume function, as well as more ACPI
suspend/resume glue.

ok deraadt@


# 1.143 26-Oct-2009 deraadt

Do not do strncmp comparisons on dv_xname because longer device names which
look similar could arrive in the future. Instead, compare directly against
dv_cfdata->cf_driver->cd_name
Issue originally spotted by miod


# 1.142 26-Oct-2009 jordan

Verify checksum+address when loading ACPI tables.
Some systems had invalid entries in RSDT/XSDT.
ok marco@


# 1.141 23-Jul-2009 cnst

New aibs(4) driver for ASUSTeK AI Booster (ACPI ATK0110) hardware monitoring,
with sensor state support through limits provided by the ACPI.

Tested on several ASUS motherboards kindly networked by Sam Fourman Jr.

ok deraadt marco jordan


Revision tags: OPENBSD_4_6_BASE
# 1.140 03-Jun-2009 pirofti

Import acpi power resource driver. This is needed for suspend/resume logic.

Okay jordan@ and marco@.


# 1.139 03-Jun-2009 pirofti

Add acpivideo support. This adds brightness support for all laptops
except thinkpads, they will use the acpithinkpad driver. The driver is
also hooked into wsconsole. So brightness can be adjusted via:

$ wsconsctl display.brightness=<percentage>

This is very helpfull on some laptops that have a nasty bios and get two
steps instead of one when pressing the brightness button.

Tested on various dell, fujitsu, acer, samsung and other laptops.

Okay marco@, miod@. Suggestions from kettenis@.
Lots of reviews and help from miod@, thanks!


# 1.138 03-Jun-2009 jordan

Added ejectable bay for IDE cdroms
ok marco@


# 1.137 30-Apr-2009 marco

Oga changed apm ioctls and forgot to grep. Temporary work around by hitting
the sleep button when we get any request. This interface will chnage at
some point.
ok oga


# 1.136 19-Apr-2009 krw

Call acpi_sleep_walk() when powering down just like when entering
any other sleep state. This tells acpi wake devices to not wake up
the box until manual powering up resets their status. Makes my Dell
GX520 stay powered off in response to 'halt -p'.

In snaps for a while.

ok marco@ (several times) deraadt@


# 1.135 17-Apr-2009 pirofti

Add device ranking support for future sleep/wake developments.

okay marco@


# 1.134 11-Mar-2009 jordan

Cleanup.. use aml_evalinteger instead of aml_evalname
ok marco@


# 1.133 10-Mar-2009 jordan

Fix cut/paste error.. call _BFS not _PTS
ok marco@


Revision tags: OPENBSD_4_5_BASE
# 1.132 21-Feb-2009 marco

overzealous cut/paste; found by Vladimir Kirillov <proger@uaoug.org.ua>


# 1.131 20-Feb-2009 marco

Oops didn't mean for Debugger() to remain there.


# 1.130 19-Feb-2009 marco

suspend/resume bits so that we can develop this in tree. This is disabled.
code from mlarkin and me
help from art,toby,jordan and several others
ok jordan, go for it deraadt


# 1.129 10-Feb-2009 jordan

Fix null pointer for shutdown if acpi not enabled
ok marco@


# 1.128 04-Feb-2009 kettenis

Some systems that claim to be ACPI 2.0 apparently don't provide an XSDT
address. Make the code fall back on the RSDT on those systems.

From Joerg Niendorf <p3y80@internode.on.net>. Fixes PR 5986.


# 1.127 17-Jan-2009 krw

A couple of stray malloc/memset -> malloc(M_ZERO), a bit of knf and
lose a couple of unnecessary casts. No functional change.

Feedback from kettenis@

ok kettenis@ marco@


# 1.126 06-Nov-2008 marco

Collapse all sanity checks of hids into one function.

Originaly from jordan


Revision tags: OPENBSD_4_4_BASE
# 1.125 02-Jul-2008 fgsch

add acpivideo. for now almost an empty skel so others can work on
suspend/resume. eventually it will also manage output switching and
brightness where supported. prodded by marco@


# 1.124 11-Jun-2008 marco

Fix $OpenBSD$ for once and for all


# 1.123 08-Jun-2008 deraadt

aaa_dev was being set from an uninitialized variable; ok toby


# 1.122 07-Jun-2008 marco

Kill ACPIVERBOSE. It was never used and is worthless to boot.

comments jmc
ok toby


# 1.121 06-Jun-2008 marco

Shrink acpi some more for floppies and while here re-enable acpi on the
floppies. Fixes crazy sony vaio kettenis has.

tested by deraadt
ok deraadt jordan kettenis


# 1.120 01-Jun-2008 marco

Fix \n nit in SMALL_KERNEL.


# 1.119 01-Jun-2008 marco

Re-enable new parser. Lots of testing by various folks. Fits all media
now too.

ok kettenis deraadt


# 1.118 16-May-2008 dlg

back out jordans new parser.

it didnt have enough oks from the right people, it did break machines
that were previously working, and it bloated the RAMDISKS and install
media beyond the constraints of the media.

requested by deraadt@


# 1.117 14-May-2008 jordan

Adding new ACPI Parser code
Old guts of code still exists, needs to be torn out next
ok marco@
ok brad@


# 1.116 27-Apr-2008 jcs

add acpithinkpad(4), an acpi support driver for ibm/lenovo
thinkpads.

provides functionality that the bios did on older, non-acpi
thinkpads like respond to screen brightness keys and toggle the
bluetooth device on and off.

ok deraadt@ and marco@


# 1.115 24-Apr-2008 jsing

Introduce acpiasus(4), a driver for the ACPI based hotkeys found in many
ASUS laptops (including the ASUS EeePC) - largely based on NetBSD's
asus(4) driver. On the ASUS EeePC this allows us to enable/disable
wireless, change screen brightness and use the volume keys.

ok jsg@, weingart@


# 1.114 12-Apr-2008 kettenis

Get rid of acpi_s5 global variables; simply send SIGUSR2 instead of SUGUSR1
to tell init(8) to power down the machine.

ok krw@


Revision tags: OPENBSD_4_3_BASE
# 1.113 21-Jan-2008 jsg

Check against RESET_REG_SUP in FADT before doing the acpi reset dance
after all, not doing so appears to break more machines than it fixes.

ok marco@


# 1.112 05-Jan-2008 kettenis

Never attach more acpicpu(4) devices than cpu(4) devices. Always attach
the first acpicpu(4) device on non-MULTIPROCESSOR kernels, regardless of its
ACPI processor ID.

ok mikeb@


# 1.111 05-Dec-2007 deraadt

Hang acpi(4) below bios(4) instead of mainbus(4). This lets us move acpi(4)
before pcibios(4), and then.... if acpi(4) attaches, skip pcibios(4) since
it messes the machines up. Fixes claudio's HP dl320 G5, and almost assuredly
others.
ok kettenis


# 1.110 05-Dec-2007 deraadt

repair acpiopen/acpiclose and friends to handle the /dev/apmctl interface,
so that apmd(8) can actually work correctly against acpi(4), and thus,
even do the fancy -C/-A stuff; also tested by jmc


# 1.109 02-Dec-2007 fgsch

fix the trailing space for real now.


# 1.108 02-Dec-2007 fgsch

avoid trailing spaces on output; marco@ ok.


# 1.107 27-Nov-2007 kettenis

Fix off-by-one that made us attach acpicpu0 and acpicpu1 on GENERIC kernels.

ok marco@


# 1.106 25-Nov-2007 jsg

When available and ACPI is enabled use the ACPI reset method
to reboot. This is required for some quirky sis machines
and likely others to reboot.

Tested by many. 'commit early commit often' deraadt@


# 1.105 19-Nov-2007 deraadt

sigh, amd64 still uses that stupid acpi_enabled variable...


# 1.104 19-Nov-2007 deraadt

softc's come initialized to 0, you know...


# 1.103 19-Nov-2007 deraadt

store revision; from jsg


# 1.102 19-Nov-2007 kettenis

Initialize sc_facs earlier.

ok deraadt@, beck@, canacar@


# 1.101 16-Nov-2007 deraadt

fix the bus_space #define nightmare, so that amd64 and i386 are much more
uniform. as a result shared code like acpi needs less #ifdef's
ok marco kettenis


# 1.100 15-Nov-2007 deraadt

remove the #ifdef ACPI_ENABLE. the EC codepath is either safe enough (and
will be improved), or it is unsafe because AML assumes it must be there
ok almost everyone


# 1.99 15-Nov-2007 mikeb

Make acpicpu(4) attach to the existing CPUs only.

Problem reported by form@. Fix idea by kettenis@.
Lots of help from deraadt@. Tested by deraadt@, form@ and me.

ok deraadt marco


# 1.98 14-Nov-2007 deraadt

spacing


# 1.97 12-Nov-2007 deraadt

correct : vs , in attach-time messages


# 1.96 06-Nov-2007 deraadt

all ACPI_ENABLE kernels need acpi_interrupt()


# 1.95 06-Nov-2007 deraadt

move acpi_read_pmreg() and acpi_write_pmreg() out of SMALL_KERNEL so
that acpi ramdisks can compile (obvious solution)


# 1.94 05-Nov-2007 canacar

Establish the acpi interrupt earlier. Fixes problems with some
GENERIC.MP kernels not getting acpi interrupts. This is a
workaround until the real issue with late establishment is fixed.
ok weingart@, beck@, gwk@


# 1.93 03-Nov-2007 jordan

Added support for displaying wakeup devices
ok beck@,weingart@,gwk@


# 1.92 11-Oct-2007 kettenis

When walking the tree yo find _PRT methods, check whether the device is
actually enabled and functioning. Similar to what we do for _INI.

ok weingart@, beck@


# 1.91 08-Oct-2007 krw

More simple memset(,0,) -> M_ZERO changes. In this batch move to
size(*p) as the first malloc() parameter where p is declared locally
and thus easy to check. Add M_ZERO to gpe_table allocation in acpi.c
even though there is no obvious bzero or memset nearby.


# 1.90 13-Sep-2007 mk

Remove unnecessary memset() (see aml_evalnode(9)) and a bit of KNF.

ok toby


# 1.89 13-Sep-2007 weingart

Implement ACPI 6.5.1 spec tree walk for _STA and _INI.
Fixes mk's laptop. No regressions so far (thank you to the
testers).

ok gwk@, mk@, marco@


Revision tags: OPENBSD_4_2_BASE
# 1.88 17-Apr-2007 mk

Now that acpidock no longer hangs machines during boot (at least
according to the reports I've been getting) it should be safe to enable
it again, so remove the #if 0 stuff around it.

ok marco


# 1.87 11-Apr-2007 jordan

Added changes for C-State
ok marco@


# 1.86 26-Mar-2007 marco

Calculate minutes left more accurately.

From giovanni <qgiovanni@gmail.com>


Revision tags: OPENBSD_4_1_BASE
# 1.85 03-Mar-2007 mk

Stop looking for acpidock until we fix that stuff that breaks all pre-60
series ThinkPads with acpidock(4) enabled.


# 1.84 22-Feb-2007 marco

We were getting no interrupts in .MP. There are 2 fixes; one in
acpi_machdep.c and move the interrupt establishment down after ioapic
has been initialized.

ok jordan


# 1.83 21-Feb-2007 kettenis

Make an acpi-enabled GENERIC.MP work on systems that only support PIC mode.

ok marco@


# 1.82 19-Feb-2007 jordan

Added acpi_map_address function for mapping GAS/IO registers
ok marco@


# 1.81 18-Feb-2007 jordan

Added dynamic perfect hash for opcodes
ok marco@


# 1.80 18-Feb-2007 marco

Shave off another K pre compression.
Shuffle SMALL_KERNEL functions around so that we only have a single #ifndef


# 1.79 17-Feb-2007 marco

Add SMALL_KERNEL throught acpi to make it fit on boot media.
ok deraadt


# 1.78 13-Feb-2007 marco

Add smarter _INI code. This helps with some dissapearing devices.

ok kettenis jordan


# 1.77 31-Jan-2007 gwk

Introduce acpi_hasprocfvs thats "ACPI Has Processor Frequency and Voltage
Scalling" a global flag set by acicpu attach to indicate that ACPI is
handling fvs through the _PSS objects. This will fix disappearing
EST/powernow when booting kernels with acpi enabled, as acpicpu isnt
compiled into GENERIC yet. Also in cases where acpicpu is built in and
legacy EST/powernow work but acpi is missing the _PSS object, the legacy
routines will be used.

tested by dim@, and Nick Nauwelaerts <nick AT nauwelaerts.net>
ok marco@, dim@


# 1.76 22-Jan-2007 mk

Actually look for docking stations by looking for _DCK nodes.

ok marco


# 1.75 02-Jan-2007 marco

On some systems EC must be attached as the first device. Move EC from
generic _HID devices to its own routine to force first attach. Bug found
and debugged by joris@

Code mostly written by joris@


# 1.74 26-Dec-2006 marco

Add polling to devices that require it such as acpibat and acpitz.
Use the same tsleep/wakeup mechanism as the interrupt code to keep it non-
concurrent.


# 1.73 21-Dec-2006 deraadt

even more knf love


# 1.72 21-Dec-2006 marco

Remove some leftover acpiec gpe/interrupt code.


# 1.71 20-Dec-2006 deraadt

oops, name already used


# 1.70 20-Dec-2006 deraadt

rename __acpi_enable_gpe(); ok marco


# 1.69 20-Dec-2006 deraadt

acpi_unmap_pmregs() is not used, ok marco


# 1.68 19-Dec-2006 deraadt

simplify some low-level methods substantially (shrinking code); ok marco jordan


# 1.67 06-Dec-2006 mk

malloc(..., M_NOWAIT) with no NULL check is bad. If allocation fails,
complain, clean up, and bail out of attach.

input and ok gwk


# 1.66 28-Nov-2006 jordan

Fixed clearing arg for acpi_init_pic
ok kettenis@


# 1.65 27-Nov-2006 kettenis

Don't spam dmesg.


# 1.64 27-Nov-2006 jordan

Fixes for acpiprt routing
Post-parse fixup for namerefs
ok kettenis@


# 1.63 25-Nov-2006 marco

Don't handle acpiev events inside the gpe handler since this causes deadlocks.

ok jordan@


# 1.62 15-Nov-2006 kettenis

Hook up acpi apic interrupt routing support to generic acpi code.

ok jordan@


# 1.61 02-Nov-2006 marco

Kill acpiec attach function and add its functionality to the generic _HID
device attach

prodded and ok jordan


# 1.60 02-Nov-2006 marco

Fix comments to reflect reality.


# 1.59 19-Oct-2006 marco

Replace ACPI ioctls with APM ioctls. This makes ACPI talk the same protocol
as APM therefore userspace can now retrieve power information in the same
fashion as all other apm arches (zaurus, macppc, i386 etc).

To use force the use of acpi device like so: apmd -f /dev/acpi

ok jordan@


# 1.58 19-Oct-2006 jordan

Fixed GPE debug levels
Fixed ref counter on temporary bufferfields
DSDT parsing fully working on IBM Thinkpad
ok marco@


# 1.57 15-Oct-2006 jordan

Removed old GPE handler code remnants
ok marco@


# 1.56 12-Oct-2006 jordan

Massive ACPI changes
Fixed ACPI leaks
new ACPI API
ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.55 30-Jun-2006 gwk

KNF, No functional change,
"go at it" marco@


# 1.54 30-Jun-2006 jordan

Support for calling Embedded Controller via new GPE interface
ok marco@


# 1.53 30-Jun-2006 jordan

Added new code for GPE handling
Also works with ACPIEC; need to fixup acpiec.c to remove original interrupt handler
ok marco@


# 1.52 30-Jun-2006 jordan

Added offset argument for acpi read/write pmreg routines (new GPE code)
ok marco@


# 1.51 31-May-2006 todd

do not call acpi_powerdown() unless acpi has successfully initialized
ok gwk@ canacar@


# 1.50 29-May-2006 canacar

Add embedded controller support.
ok marco@ deraadt@ testing & ok gwk@


# 1.49 19-May-2006 canacar

Minimal thermal zone driver. Monitors thermal zone temperature,
shuts down the system if the 'critical temperature' is reached.
ok marco@


# 1.48 11-Apr-2006 marco

KNF.


# 1.47 11-Apr-2006 gwk

Add acpi_resume(struct acpi_softc *sc), which calls some AML methods,
detected at attach time, currently unused.

ok marco@


# 1.46 11-Apr-2006 gwk

Discover some ACPI sleep related methods, and call them at the
appropriate time in acpi_enter_sleep_state().

ok marco@


Revision tags: OPENBSD_3_9_BASE
# 1.45 26-Feb-2006 marco

Add acpicpu as a device.

ok jordan@


# 1.44 23-Feb-2006 marco

remove dead function.

ok jordan@


# 1.43 22-Feb-2006 marco

Print out all tables it found during autoconf. We'll need this data in
the future.


# 1.42 22-Feb-2006 marco

Make dmesg less verbose.

ok jordan.


# 1.41 22-Feb-2006 jordan

Added parsing of SSDT tables
ok marco@


# 1.40 22-Feb-2006 jordan

Added new aml_register to notify by device id
Moved powerdown code to button device
ok marco@


# 1.39 21-Feb-2006 marco

Readjust debug levels that crept in too low over the last few days.
KNF


# 1.38 21-Feb-2006 marco

Rig the power button so that the machine shuts down correctly.


# 1.37 20-Feb-2006 marco

Add acpibtn device. Remains disabled for now.


# 1.36 19-Feb-2006 jordan

Added code for aml_notify GPE notification callback
ok marco@


# 1.35 19-Feb-2006 grange

Fix acpi_enter_sleep_state() by replacing current ugly hack
with mostly correct sleeping state transition code.

ok brad@ marco@


# 1.34 19-Feb-2006 marco

jordan:
Enable interrupts.
Added peripheral code to make this work.

marco:
Replace softintr and other types of interrupts with a handler with context.
Fixed interrupt magic for GPEs.
Some KNF

This is work done by jordan and I but has to be a single commit.


# 1.33 17-Feb-2006 marco

Remove earlier introduced delays. We really only need them during autconf.

ok jordan@


# 1.32 17-Feb-2006 marco

Add a generic delay function as per the ACPI spec.

from jordan@


# 1.31 17-Feb-2006 marco

Use tsleep instead of delay when !cold.


# 1.30 17-Feb-2006 marco

Argh, we need delays between reads/writes. The backend reads (i2c most likely)
are so slow that values come back as 0 sometimes. We have to do this
despite the spec saying we shouldnt have to. Guess having a faster
parser than others can hurt sometimes.

Idea from jordan.


# 1.29 17-Feb-2006 jordan

Fixed code for handling _INI
Rearranged debug statements
ok marco@


# 1.28 16-Feb-2006 jordan

Added stub for calling _INI
Fixed aml_dowhile for NULL return
ok marco@


# 1.27 16-Feb-2006 jordan

Fixed parser/evaluator to retrieve battery info+state
Cleanup of parser
Dell laptops return battery status
ok marco@


# 1.26 06-Feb-2006 brad

fix a typo so this will compile on amd64.


# 1.25 04-Feb-2006 marco

When ACPI_ENABLED wasn't set but the devices were it had an unknown variable.

Pointed out by ian@.


# 1.24 03-Feb-2006 jordan

Rewrote aml parser yet again...
Fixed bus_space_map for i386/amd64
Added _BST and _BIF to acpibat to get status/info
ok marco@


# 1.23 20-Jan-2006 jordan

More evaluator changes
Removed old evaluator code
Fixed _HID value
ok marco@


# 1.22 19-Jan-2006 jordan

Added support for read/write of PCI Config space
Help jason@
ok marco@


# 1.21 18-Jan-2006 jordan

Added new evaluation routines
Added helper methods for buffer/field evaluation
Fixed broken indentation on dsdt.c

ok marco@


# 1.20 17-Jan-2006 jordan

Major overhaul of the aml parser. Evaluater temporarily disabled.

ok marco@


# 1.19 06-Jan-2006 grange

Check for ACPI control availability only if ACPI_ENABLE defined.


# 1.18 06-Jan-2006 grange

Informative error messages on attach, prettier dmesg.


# 1.17 06-Jan-2006 grange

printf -> dnprintf conversion for debug messages.


# 1.16 05-Jan-2006 grange

Find SLP_TYPx values and store them for future use in
acpi_enter_sleep_state().


# 1.15 05-Jan-2006 grange

Random KNF and cleanup


# 1.14 05-Jan-2006 grange

Provide symbolic names for the acpi sleeping states and use it.


# 1.13 28-Dec-2005 marco

Moved DEVNAME to a acpidev.h

ok jordan@


# 1.12 18-Dec-2005 sturm

add back comment about wrong code

ok marco


# 1.11 16-Dec-2005 marco

Remove test for kbd_reset. Prompted deraadt@, ok jordan@


# 1.10 16-Dec-2005 jordan

Added hooks for powerdown/power button press


# 1.9 16-Dec-2005 jordan

Cleaned up common power management register i/o
Added enter sleep state ioctl
Hooked power button interrupt
ok marco@


# 1.8 16-Dec-2005 marco

Remove autoconf chatter


# 1.7 16-Dec-2005 jordan

Moved DSDT AML parsing into parent ACPI driver
ok marco@


# 1.6 14-Dec-2005 marco

Add AC device glue.


# 1.5 14-Dec-2005 marco

Adjust debug printing levels to something more sane.


# 1.4 13-Dec-2005 marco

Add glue to attach devices to apci.


# 1.3 07-Dec-2005 marco

KNF


Revision tags: OPENBSD_3_8_BASE
# 1.2 10-Jul-2005 grange

Spacing cleanup.


# 1.1 02-Jun-2005 tholo

Start on a basic ACPI framework -- does not do much more than read out the
ACPI tables into kernel memory and attach ACPI and HPET timers currently.

In order to test this code, enabling the devices in GENERIC as well as
the ACPI_ENABLE option is needed. This code does not do any thermal
control yet, so this should be done with care depending on the platform.

In the tree so more people can contribute to making this more fully
featured.

Ok niklas@ grange@ tedu@


# 1.410 08-Feb-2022 deraadt

The suspend/resume code is a sticky mess of MI, MD, and ACPI sequencing.
This splits out the MI sequencing, backing it with per-architecture helper
functions. Further steps will be neccesary because ACPI and MD are too
tightly coupled, but soon we'll be able to use this code for more architectures
(which depends on figuring out the lowest-level cpu sleeping method)
ok kettenis


# 1.409 04-Feb-2022 robert

set the APM_BATT_CHARGING state if the battery is being charged and try
to calculate the remaining time to be fully charged

ok jca@


# 1.408 02-Feb-2022 deraadt

acpi_addtask() calls malloc() w/ M_NOWAIT (because some calls come from
interrupt context), this however means occasional resource shortage will
result in callbacks registration failing, and unknown consequences for
the task-submitting caller.
Changing this to use pools with a low water mark, decreases the odds
of that problem occuring.
ok kettenis


# 1.407 01-Feb-2022 deraadt

GPE_DIRECT is calling the AML parser from interrupt context, which is
not permitted. Luckily nothing is using GPE_DIRECT anymore, so this code
can be deleted.
ok kettenis


# 1.406 26-Jan-2022 kettenis

An ACPI device needs to be both present and enabled for it to function.
So only attempt to attach hardware that has both bits enabled. This fixes
an issue where com(4) would attach for a disabled serial port leading to
misdetection of the hardware variant and a subsequent hang when /etc/rc
runs ttyflags -a.

ok anton@, deraadt@


# 1.405 12-Jan-2022 patrick

Make acpi_getpropint() return uint64_t, as ACPI integers are in fact that
wide and some _DSD properties depend on it.

ok kettenis@


# 1.404 04-Jan-2022 patrick

acpi_getprop() needs to actually make sure that we're looking at the
correct property. While there adjust acpi_getpropint() as well to
increase similarity with acpi_getprop().

ok kettenis@


# 1.403 16-Dec-2021 anton

Attach com over acpi on amd64. Some hardware uses a different interrupt
assignment compared to the the legacy one supported by com over isa.
This causes the console to halt once userland takes over as no
interrupts are received. The actual address and irq can be read from
ACPI, kettenis@ already added support for arm64 which paved the way for
amd64.

Some consoles that previously attached over isa are now expected to
attach over acpi.

Thanks to patrick@ for testing on arm64.

ok kettenis@


# 1.402 07-Dec-2021 kettenis

Fix parsing of SR_IRQ resource descriptors.

ok patrick@, anton@


# 1.401 02-Nov-2021 deraadt

knf


# 1.400 30-Oct-2021 deraadt

Change hw.perfpolicy=auto by default, at startup. If the system has AC
power connected (default is yes when no driver differentiates) then default
to 100% performance. On battery, use the existing auto algorithm (which is
admittedly somewhat unrefined).
This change overrides the system/BIOS speed and puts OpenBSD in control.
As this happens very early during boot, besides speedups in all usage usage
patterns, some surprises: unhibernate and sysupgrade times are cut in half.
note: on a few architectures, the setperf fn pointer is changed late, and
thus the auto algorithm stops timeing out. kettenis and i will look for
a solution.
in snaps for more than a week.
ok kettenis


Revision tags: OPENBSD_7_0_BASE
# 1.399 20-Jul-2021 mlarkin

Fix trailing whitespace in a few places.

No code change.


# 1.398 09-May-2021 kettenis

Some x86 machines advertise the "hardware reduced" ACPI feature, advertise
S4 and S5 support, but fail to populate the SLEEP_CONTROL_REG and
SLEEP_STATUS_REG descriptions in the FADT. An example of such a machine
is the ASUS Zenbook 14 UM433DA. Any attempt to powerdown the machine
will result in a kernel crash. It turns out that using the legacy ACPI PM
registers works fine on this machien. So fall back on those registers
if SLEEP_CONTROL_REG and/or SLEEP_STATUS_REG aren't provided.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_9_BASE
# 1.397 15-Mar-2021 patrick

Add acpi_iommu_device_map(), which replaces the DMA tag with one that
is blessed with IOMMU magic, if available. This is mainly for arm64,
since on amd64 and i386 the IOMMU only captures PCIe devices, as far
as I know, which uses the pci_probe_device_hook(). This though is for
non-PCI devices attached through ACPI.

ok kettenis@


# 1.396 07-Mar-2021 patrick

Pass standard DMA tag to acpi(4) table drivers.

ok kettenis@


# 1.395 27-Dec-2020 deraadt

acpi_map_address() cannot be wrapped by SMALL_KERNEL anymore, as
it is used by acpihpet.c


# 1.394 25-Dec-2020 visa

Refactor klist insertion and removal

Rename klist_{insert,remove}() to klist_{insert,remove}_locked().
These functions assume that the caller has locked the klist. The current
state of locking remains intact because the kernel lock is still used
with all klists.

Add new functions klist_insert() and klist_remove() that lock the klist
internally. This allows some code simplification.

OK mpi@


# 1.393 17-Dec-2020 kettenis

Fix some issues with referencing named ACPI nodes from Packages.
These references need to be resolved at runtime rather than when
they're parsed such that they pick up the right values for those nodes
which can be changed when for example _INI methods run.

The current approach is to replace these reference with a string that
names the node in question. The problem with that is that packages
can also contain normal strings. Which means that we need special
code that depends on the context where the package is used.

This diff takes a different approach by retaining a reference when
parsing. Code that uses a package will need to resolve this reference
but there is no ambiguiety anymore.

ok patrick@


# 1.392 05-Dec-2020 kettenis

Add support for the _EVT method to the GPIO support code.

ok patrick@


Revision tags: OPENBSD_6_8_BASE
# 1.391 27-Aug-2020 jmatthew

Reorder the acpi attach process so we look at Processor() CPU nodes
after Device() ones, since we should prefer the newer node type.
If we see any Device() nodes, don't attach acpicpu(4) to any Processor()
nodes if they're also present. This also makes acpitz(4) and
acpipwrres(4) devices attach slightly later.

ok kettenis@, also tested by jmc@


# 1.390 16-Aug-2020 gnezdo

Remove sc_maxgpe from acpi_softc which was never read from

Remove a dead conditional from acpi_init_gpes while there.

ok jcs@


# 1.389 21-Jul-2020 deraadt

acpi can use IPL_BIO (a low interrupt) since it only enqueues operations for
later processing. The use of a high interrupt will predate suspend/resume
efforts, we had to redesign acpi to be non-reentrant obviously
discussed with kettenis, in snaps for more than a week


# 1.388 24-Jun-2020 cheloha

kernel: use gettime(9)/getuptime(9) in lieu of time_second(9)/time_uptime(9)

time_second(9) and time_uptime(9) are widely used in the kernel to
quickly get the system UTC or system uptime as a time_t. However,
time_t is 64-bit everywhere, so it is not generally safe to use them
on 32-bit platforms: you have a split-read problem if your hardware
cannot perform atomic 64-bit reads.

This patch replaces time_second(9) with gettime(9), a safer successor
interface, throughout the kernel. Similarly, time_uptime(9) is replaced
with getuptime(9).

There is a performance cost on 32-bit platforms in exchange for
eliminating the split-read problem: instead of two register reads you
now have a lockless read loop to pull the values from the timehands.
This is really not *too* bad in the grand scheme of things, but
compared to what we were doing before it is several times slower.

There is no performance cost on 64-bit (__LP64__) platforms.

With input from visa@, dlg@, and tedu@.

Several bugs squashed by visa@.

ok kettenis@


# 1.387 10-Jun-2020 jca

Give userland a consistent battery and A/C state after resume

Previously we refreshed the state of acpiac(4) and acpibat(4)/acpisbs(4)
by queueing fake ACPI events to be processed by the acpi thread.
acpibat(4) using ACPIDEV_POLL meant that its status could be refreshed
first, resulting in an APM_POWER_CHANGE event sent to userland before
the state of acpiac(4) was up to date. Because of this, apmd(8) could
see A/C unplugged when it was in fact plugged, possibly leading to
a wrong decision.

Instead of using fake ACPI events, refresh the status of acpiac(4) and
acpibat(4)/acpisbs(4) right away at DVACT_WAKEUP time. To reuse the
existing refresh code of those drivers, make their ACPI *_notify()
handlers responsible for sending APM_POWER_CHANGE events instead. Also
queue an APM_POWER_CHANGE event right after resume to retain existing
behavior.

This fixes the consistency of data shown to userland, and makes those
drivers less special.

Initially discussed last year (!) with mpi@ at Miod's place.
Feedback and ok jcs@, deraadt@ agrees with the use of DVACT_WAKEUP here.


# 1.386 29-May-2020 deraadt

dev/rndvar.h no longer has statistical interfaces (removed during various
conversion steps). it only contains kernel prototypes for 4 interfaces,
all of which legitimately belong in sys/systm.h, which are already included
by all enqueue_randomness() users.


# 1.385 14-May-2020 kettenis

Bring back the acpi(4) changes. This time around only use the ACPI _CRS
information on ACPI 5.0 and later.

ok krw@, patrick@


# 1.384 11-May-2020 jca

More accurate remaining power reporting with disparate batteries

Overall remaining power was computed as the average of the remaining
power of each battery, in percents. This doesn't work with batteries of
different capacity, so use the total capacity and remaining power
instead.

Reported by lidstah, successful tests by lidstah and benno@, ok kettenis@


# 1.383 08-May-2020 kettenis

Move parsing of _CRS into acpi(4) and pass the parsed address and interrupt
information in struct acpi_attach_args.

ok mpi@


Revision tags: OPENBSD_6_7_BASE
# 1.382 14-Apr-2020 kettenis

Implement acpi_getprop(), the ACPI equivalent of OF_getprop().

ok patrick@


# 1.381 12-Apr-2020 kettenis

Add support for the _CCA method. This method indicates whether DMA is
cache-coherent or not. To implement this, acpi(4) gets two bus_dma tags
and passes the appropriate one when attaching devices based on _CCA.
On i386/amd64, where for all practical purpose DMA is always cache-coherent,
the two tags are the same. But on arm64 they are distinct.

ok patrick@


# 1.380 07-Apr-2020 visa

Abstract the head of knote lists. This allows extending the lists,
for example, with locking assertions.

OK mpi@, anton@


# 1.379 03-Apr-2020 mpi

Fix incoherencies of errors returned by various *kqfilter routines.

EOPNOTSUPP is returned when there's no "filterops" for a given fd.
EINVAL when the requested filter isn't supported by the "filterops".
ENXIO means the underlying device is no longer valid/present.

ok visa@


# 1.378 20-Feb-2020 visa

Replace field f_isfd with field f_flags in struct filterops to allow
adding more filter properties without cluttering the struct.

OK mpi@, anton@


# 1.377 31-Dec-2019 visa

Use C99 designated initializers with struct filterops. In addition,
make the structs const so that the data are put in .rodata.

OK mpi@, deraadt@, anton@, bluhm@


# 1.376 31-Dec-2019 mpi

Convert infinite sleeps to {m,t}sleep_nsec(9).

ok kn@


# 1.375 31-Dec-2019 jsg

include sys/sensors.h for sensor use
don't depend on indirect inclusion from acpidev.h


Revision tags: OPENBSD_6_6_BASE
# 1.374 07-Sep-2019 kettenis

Revert acpipci(4) changes. Too many systems are broken. We'll try again
after 6.6 as been released. The acpireg.h change stays behind.


# 1.373 28-Aug-2019 kettenis

Use ACPI information to attach PCI busses like we do on arm64. There are a
few additional quirks though, and attaching the PCI busses is delayed to
replicate the existing code more closely. That may be changed in the
future. Also tweak how we handle MSI support and respect to ACPI flag
that says we shouldn't attempt to use MSIs.

Some fallout is expected.

ok patrick@


# 1.372 27-Aug-2019 deraadt

On amd64 the bootup and resume paths for SP/BP/SP were not performing
msr/pat, clock-startup, firmware upgrade, fpu initialization, and cpu
soft-state initialization in the same order. That kind of chaos is
crazy and fragile. Improve the acpi resume call sequence and unify order
of the operations.


# 1.371 02-Jul-2019 jcs

acpi_set_gpehandler: turn edge arg into flags, add support for
passing GPE_DIRECT which directly executes the supplied callback and
clears a level interrupt rather than going through acpi_taskq which
can be slow when other driver callbacks are in the queue.

This should not be used for GPE handlers that need to do anything
ACPI-related.

ok kettenis


# 1.370 10-Jun-2019 kettenis

Print proper ACPI version number.

ok mlarkin@


# 1.369 08-Jun-2019 kettenis

When establishing the mapping between ACPI device nodes and PCI devices,
skip PCI host bridges and devices that aren't there.

ok patrick@


# 1.368 07-Jun-2019 kettenis

Introduce acpi_getsta() and use it to eliminate some duplicated code.

ok patrick@


# 1.367 12-May-2019 kettenis

Make sure the extended PM register descriptions actually exist before
using them. Fixes machines with ancient ACPI 1.0 (such as recent
hypervisors).

ok deraadt@


# 1.366 12-May-2019 kettenis

Add support for mapping ACPI PM registers using the "extended" gasio
representation from the FADT. Mostly fixes the Lanner NCA-1510.

ok lteo@, mlarkin@, deraadt@


# 1.365 11-May-2019 lteo

Fix incorrect error message.

ok kettenis@


# 1.364 08-May-2019 tedu

free size. the allocation is the header plus the length.


Revision tags: OPENBSD_6_5_BASE
# 1.363 04-Apr-2019 kettenis

Avoid potential null-pointer dereference. Found by Kent R. Spillner.

ok deraadt@, jsg@


# 1.362 20-Jan-2019 jsg

fix SMALL_KERNEL build after changes in rev 1.361
reported by naddy@ ok deraadt@


# 1.361 19-Jan-2019 tedu

Add a pwraction sysctl that controls what the power button does on acpi.
By default, nothing changes -- shutdown is initiated. But allows turning
power button into a sleep button if desired.
(grudging) ok from a few parties


# 1.360 26-Oct-2018 kettenis

Add acpipci(4) on amd64. For now this only calls the PCI-specific _OSC
method to let the ACPI implementation know what features we support.


Revision tags: OPENBSD_6_4_BASE
# 1.359 30-Aug-2018 kettenis

Move softbutton check before the check that skips attaching certain acpi
devices. Fixes regression caused by matching on _CID in addition to
matching on _HID.

ok matthieu@, mlarkin@


# 1.358 25-Aug-2018 kettenis

Match ACPI devices based on _CID if no match for _HID is found.

ok mpi@


# 1.357 19-Aug-2018 kettenis

Add support for multiple PCI segments. Only really implemented for arm64
for now as amd64/i386 firmware still caters for legacy OSes that only
support a single PCI segment.

ok patrick@


# 1.356 03-Aug-2018 kettenis

Let ahci(4) match on _CLS instead of _HID when attaching at acpi(4). Avoids
having to add many more _HID entries to the match table.

ok deraadt@, mlarkin@


# 1.355 10-Jul-2018 kettenis

Make legacy interrupts work in acpipci(4).

ok patrick@


# 1.354 05-Jul-2018 kettenis

Add acpipci(4), a driver that supports generic ECAM-compatible PCI host
bridges based on information provided by ACPI.

ok mlarkin@


# 1.353 04-Jul-2018 kettenis

Properly pass around the PCI "chipset tag" in acpi(4) and refactor
acpimcfg(4) to call an MD initialization functions that sets up a tag for
PCI ECAM.

ok guenther@, mlarkin@, krw@


# 1.352 01-Jul-2018 kettenis

Move acpi(4) attach glue into acpi_machdep.c.

ok guenther@, deraadt@


# 1.351 01-Jul-2018 kettenis

Add support for _DSD Device Properties.

ok mlarkin@


# 1.350 30-Jun-2018 kettenis

Add intr_enable() function, intended for MI use to amd64 and i386 and use
this in the acpi(4) suspend/resume code paths.

ok deraadt@


# 1.349 29-Jun-2018 kettenis

Use <stdint.h> types.

ok mpi@, jung@, krw@, deraadt@


# 1.348 29-Jun-2018 mlarkin

ACPI: Allow (with warning) GPE handler reassignment, instead of returning
a failure code.

ok kettenis


# 1.347 26-Jun-2018 mlarkin

knf


# 1.346 26-Jun-2018 kettenis

Let acpi(4) pass down the bus dma tag.

ok deraadt@


# 1.345 25-Jun-2018 kettenis

Reorganize acpi(4) code a bit in preparation for upcoming arm64 support.

ok deraadt@


# 1.344 20-May-2018 kettenis

Wake up the acpi thread after scheduling a gpio event.

ok mpi@


# 1.343 17-May-2018 kettenis

Sprinkle some #ifndef SMALL_KERNEL such that RAMDISK kernels cuild again.

pointed out by stsp@


# 1.342 17-May-2018 kettenis

Implement GenericSerialBus OpRegion support.

ok mlarkin@


# 1.341 27-Mar-2018 kettenis

Add acpicmos(4), a driver that implements SystemCMOS OperationRegion
access support. This fixes machines where the AML doesn't check whether
support for this OperationRegion type has been registered by the OS.

ok mlarkin@


Revision tags: OPENBSD_6_3_BASE
# 1.340 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.339 10-Feb-2018 deraadt

Syncronize filesystems to disk when suspending. Each mountpoint's vnodes
are pushed to disk. Dangling vnodes (unlinked files still in use) and
vnodes undergoing change by long-running syscalls are identified -- and
such filesystems are marked dirty on-disk while we are suspended (in case
power is lost, a fsck will be required). Filesystems without dangling or
busy vnodes are marked clean, resulting in faster boots following
"battery died" circumstances.
Tested by numerous developers, thanks for the feedback.


# 1.338 08-Feb-2018 deraadt

After flushing disks, try again to release all memory. We don't need to
save dirty memory to the hibernate space.


# 1.337 08-Feb-2018 deraadt

Toss all releaseable memory, because fragmentation can get in the way
of allocating the hibernate playpen.


# 1.336 08-Feb-2018 deraadt

sr_quiesce() is a new approach for ensuring that softraid drains
output to the disks.

This is part of a larger suspend/resume filesystem-safety diff, which
has been worked on for a couple of months already. Tests by job, krw,
beck, benno, and others. Sometimes even by snapshot users...


# 1.335 29-Nov-2017 kettenis

Add support for GPIO-signaled events.

ok mlarkin@


# 1.334 16-Nov-2017 jcs

Add PCI attachment for dwiic(4) needed by Intel 100 Series machines

ok kettenis


# 1.333 14-Nov-2017 kettenis

Add a few more PNP IDs to the skip lists.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.332 17-Aug-2017 stsp

Make dwiic(4) attach its ACPI parent dependencies.
This change makes the Asus E200HA keyboard work.

Original analysis and patch by Cesare Gargano
ok kettenis@


# 1.331 09-Aug-2017 dcoppa

Add "PNP0303" (8042 PS/2 Controller) to acpi_isa_hids[]

ok kettenis@


# 1.330 22-Jul-2017 jcs

acpibat: look for _BIX first before falling back on _BIF

ACPI 4.0 deprecated _BIF for battery status so some newer machines
are exporting _BIX which extends _BIF's package with a few extra
fields. When using _BIX, export the new discharge cycle count as a
sensor.

feedback from kettenis and anton
ok deraadt


# 1.329 20-Jul-2017 kettenis

Start a list of isa(4) device names such that we can avoid printing them
as "not configured".

ok mikeb@, millert@, deraadt@


# 1.328 08-Apr-2017 deraadt

Sensors are run as callbacks inside tasks. During suspend a sensor
could be running inside a driver that will be force-detached, or due
to tsleep end up disrupting the softstate/hardstate contract. At
suspend time, quisce all these callbacks by waiting for completion.
This issue has never been observed for real, but may be implicated in
suspend/resume failures.
ok kettenis guenther mlarkin


# 1.327 08-Apr-2017 deraadt

Two prototypes for acpi_maptable; merge to one place


Revision tags: OPENBSD_6_1_BASE
# 1.326 27-Mar-2017 deraadt

Now that hibernate_alloc() only has clean success/failure, don't
need to call hibernate_free() to clean up a partial mess.
ok mlarkin kettenis


# 1.325 13-Mar-2017 deraadt

allow ramdisks to build; ok jcs


# 1.324 12-Mar-2017 jcs

try to attach acpisbs first and if successful, don't attach acpibat


# 1.323 02-Mar-2017 natano

Add a new sysctl machdep.lidaction. The sysctl works as follows:

machdep.lidaction=0 # do nothing
machdep.lidaction=1 # suspend
machdep.lidaction=2 # hibernate

lidsuspend is just an alias for lidaction, so if you change one, the
other one will have the same value. The plan is to remove
machdep.lidsuspend eventually when people have upgraded their
/ets/sysctl.conf.

discussed with deraadt, who came up with the new MIB name
no objections mlarkin
ok stsp halex jcs


# 1.322 28-Feb-2017 natano

Untangle abstract sleep modes from ACPI sleep states. This paves the way
for further work in the area.

idea and ok deraadt
ok mlarkin


# 1.321 22-Feb-2017 jcs

add acpisbs, an acpi smart battery subsystem driver reading data
over smbus

currently disabled because it conflicts with acpibat

ok deraadt, kettenis


# 1.320 14-Jan-2017 kettenis

Revert aml_rdpciaddr changes; breaks several machines that were working
before.


# 1.319 11-Jan-2017 jsg

Increase the size of the buffers used for _HID and _CID strings to be
able to fit "Hyper_V_Gen_Counter_V1".

ok mlarkin@ deraadt@


# 1.318 08-Jan-2017 kettenis

Make aml_rdpciaddr() work for for devices behind bridges. The code now
uses the mapping between AML nodes and PCI devices that we establish.
Because _INIT methods may end up calling aml_rdpciaddr(), make sure we
create that mapping early. Also handle devices that aren't actually present.
These devices are now included in the mapping and reads will return an
all-ones patterm whereas writes are a no-op..

ok mlarkin@


# 1.317 25-Oct-2016 pirofti

Add the resource index to aml_parse_resource() callback function.

This is needed by an upcoming acpiec commit that handles machines breaking
the current ACPI specifications.

Change suggested by and ok kettenis@, guenther@


# 1.316 18-Sep-2016 guenther

If the APIC table (MADT) isn't present then ci_acpi_proc_id won't be set
when acpicpu tries to attach, leading to a NULL dereference. The ACPI
processor object on the Dells where this was reported is bogus and useless
anyway so apply the same test when deciding whether to attach to processor
objects as acpicpu would when matching them to cpus.

Problem reported by Eivind Eide (xenofil (at) gmail.com) and
Johan Huldtgren (johan+openbsd-bugs (at) huldtgren.com).
Testing by Eivind Eide.
ok kettenis@ mlarkin@


# 1.315 03-Sep-2016 naddy

Write the system time back to the RTC every 30 minutes.
This fixes the problem that long-running machines which were not
shut down properly would reboot with a badly offset system time.

hints and ok kettenis@


# 1.314 31-Aug-2016 mlarkin

Don't put a device in the wake device list if _STA says it's not there.

Fixes at least one machine that woke up from sleep / poweroff immediately.

ok kettenis@, tested by kettenis and martijn@


# 1.313 28-Jul-2016 kettenis

Store the acpi processor ID/UID in struct cpu_info, and use it to attach
acpicpu(4) drivers to the right cpu(4).

ok mlarkin@, guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.312 10-Jun-2016 kettenis

The power management timer has been made optional in ACPI 5.0A, so don't
attempt to attach acpitimer(4) if the timer isn't present.

Pointed out by jcs@

ok jcs@


# 1.311 08-May-2016 kettenis

Make sure devices listed by a device's _DEP method are attached before
we attach the device itself. The _DEP methode, introduced in ACPI 5.0,
is a hint that the device in question depends on OpRegion support from
the devices listed by _DEP, so we have to order them the proper way.
To prevent us from attaching those devices again when we encounter them
later walking down the device tree, keep track of our attempts to
attach a device driver to them using aflag in the aml_node structure.

ok guenther@


# 1.310 25-Apr-2016 pirofti

We attach acpihpet(4) and acpiec(4) without using the HID, so do not
report it in the dmesg.

OK deraadt@, mlarkin@.


# 1.309 02-Apr-2016 kettenis

Reduce dmesg spam by knocking out some acpi hardware IDs that don't need a
driver.

ok mpi@


# 1.308 29-Mar-2016 kettenis

Check if a device is present (using _STA) in acpi_foundhid. This prevents
us from reporting devices as "not configured" that aren't actually present,
and allows us to remove duplicated code from several drivers.

ok deraadt@, ok jsg@


# 1.307 29-Mar-2016 kettenis

Implement a cleaner way to prevent acpivout(4) from attaching if we have
a platform-specific method of controlling the backlight.

ok millert@, mlarkin@, jung@


# 1.306 28-Mar-2016 kettenis

Adding each and every new acpi device driver to acpi_foundhid() is
gettinga bit of the burden. And it really isn't how our autoconf
framework is supposed to work for busses that can be enumerated. All
the drivers already check for a matching _HID in their attach
function. So we can just drop the checks in acpi_foundhid() and let
the drivers sort themselves out.

Print a "not configured" line for devices for which we don't attach adriver.
This may be a bit much as there are devices for which we will not have a
driver. This will be removed if it causes too much dmesg spam.

ok deraadt@, mlarkin@


Revision tags: OPENBSD_5_9_BASE
# 1.305 17-Jan-2016 jsg

Take acpi_foundhid() outside of #ifndef SMALL_KERNEL so the ramdisk
kernel can find and attach dwiic(4) and sdhc(4).


# 1.304 17-Jan-2016 jsg

remove an unused global variable


# 1.303 14-Jan-2016 kettenis

Attach dwiic(4) on the Intel Bay Trail i2c controllers as well.


# 1.302 12-Jan-2016 kettenis

Improve emulation of PM registers on Hardware-redcued ACPI by emulating
SPL_TYPx, SLP_EN and WAK_STS using the new SLEEP_STATUS_REG and
SLEEP_CONTROL_REG registers. Makes the transition into S5 (powerdown) work
on the ASUS X205TA.

ok deraadt@, mlarkin@


# 1.301 12-Jan-2016 jcs

Add dwiic, a driver for the Synopsys DesignWare i2c controller found
on the Samsung ATIV Book 9 laptop. This initial version only
supports ACPI config/attachment.

Add ihidev, a HID-over-i2c driver largely based on uhidev. dwiic
handles attaching ihidev devices found in ACPI.

Add ims, a HID-over-i2c mouse/trackpad driver to get basic cursor
and button functionality from HID-compliant i2c trackpads.

ok kettenis deraadt


# 1.300 11-Jan-2016 kettenis

Make sdhc(4) attach to hardware IDs "80860F14" and "PNP0FFF".


# 1.299 09-Jan-2016 kettenis

Initial support for Hardware-Reduced ACPI. This skips the legacy mode checks
by emulating the PM_CNT register such that SCI_EN is always enabled. It also
skip attaching acpitimer(4) since the relevant registers are no longer there.

ok deraadt@, mlarkin@


# 1.298 06-Jan-2016 kettenis

Follow the spec and check the SCI_EN bit to see if ACPI has already
been enabled and don't bother enabling it again if it is. This should fix
systems that don't implement legacy mode and therefore set ACPI_ENABLE and
ACPI_DISABLE to zero.

ok mlarkin@, yasuoka@


# 1.297 23-Nov-2015 reyk

Back out the CRITICAL change, turn it back from 10% to 25%. Some
people with old/broken batteries didn't like this change, and the
underlying heuristics are wrong.


# 1.296 22-Nov-2015 reyk

The "CRITICAL" APM battery level, that is exported/faked by the ACPI
stack, is currently set to 25%. With 25% battery left, a typical
modern laptop will have at least one hour runtime left. It is an
arbitrary value, and other APM power layers in the kernel set CRITICAL
to something like 5% or 10%, so set it to 10% for now.

OK mlarkin@


# 1.295 28-Sep-2015 deraadt

In low-level suspend routines, set cold=2. In tsleep(), use this to
spit out a ddb trace to console. This should allow us to find suspend
or resume routines which break the rules. It depends on the console
output function being non-sleeping.... but that's another codepath which
should try to be safe when cold is set.
ok kettenis


# 1.294 06-Sep-2015 deraadt

clarify malloc arguments (base + length...)


# 1.293 28-Aug-2015 deraadt

fairly simple sizes for free(); ok tedu


# 1.292 20-Aug-2015 kettenis

Respect the access size when reading or writing to pci config space.
This version also makes sure writes are properly aligned, preventing
the panics seen with the previous incarnation of this change.

Fixes battery status passthrough in vmware, brightness keys on my x220
and who knows what else.

ok mlarkin@, deraadt@


Revision tags: OPENBSD_5_8_BASE
# 1.291 04-Aug-2015 deraadt

If we are setting ACPI_PM1_BM_RLD do it only once, and remember to
repeat this action at resume time.
ok mlarkin guenther


# 1.290 29-Jul-2015 deraadt

Something subtle in previous revision is locking up suspend or booting
on some laptops. Mailing lists threads mention x220 and Asus machines.
Reverting for now.


# 1.289 21-Jul-2015 kettenis

iRespect the access size when reading or writing to pci config space.
Fixes battery status passthrough in vmware and who knows what else.

Tested by many.

ok mlarkin@, deraadt@


# 1.288 17-Jul-2015 mlarkin

fix comments


# 1.287 26-Mar-2015 jsg

Use the 0x10 extended capability to determine if the trackstick
buttons are wired to the trackpad and need to be re-routed to the
trackstick. Without this change the buttons on 2015 thinkpads
get picked up as extended buttons that show up as scroll up/down.

Remove the X1 Carbon 2015 (LEN0048) and X250 (LEN0046) from the
top button area/soft buttons quirks list. Also avoid using the
quirk list entirely if the capability bit is set.

Based on work from jcs@ with feedback from mpi@
ok mpi@ jcs@


# 1.286 20-Mar-2015 kettenis

The ACPI standard says that a PCI function number of FFFF can be used to refer
to all functions on a device. This makes no sense in the context of accessing
PCI config space. Nevertheless stsp@ has a machine with AML that tries to
do config space reads from a device where _ADR encodes function number FFFF.
This is almost certainly buggy AML, but since Linux (and presumably Windows)
boot just fine on this machine we need to work around this. Linux effectively
treats function FFFF as function 7, which isn't present on this machine.
Instead we simulate a read from a non-existent device by returning a bit
pattern of all ones. Writes to function FFFF will still panic.

ok mlarkin@


# 1.285 16-Mar-2015 deraadt

Reduce use of SMALL_KERNEL in acpi, bringing acpiec driver and more gpe
handling into RAMDISK. This is now possible because the install media
has ample room. The goal is to reduce special cases where we may be
skipping (unknown) important operations...
ok mlarkin kettenis


# 1.284 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.283 07-Feb-2015 deraadt

New framework that allows hibernate to pass in entropy from it's fresh
boot.
ok mlarkin


# 1.282 06-Feb-2015 mlarkin

Fix a hibernate crash on some machines due to unmapping a page that
may not have been mapped previously (in the failure to hibernate case).

Also ensure that the lowmem ptp is mapped in all cases (not just MP).

ok kettenis


# 1.281 17-Jan-2015 deraadt

resume_randomness() before spinning up other cpus...


# 1.280 15-Jan-2015 jsg

Define a new wscons mouse type for Synaptics clickpad devices that lack
physical buttons. This will be used if the acpi pnpid for the mouse
matches a list provided by Synaptics found in Linux.

Combined with the xenocara changes this will remove the need for an xorg
config file for the x240/t440/t540 generation of thinkpads.

ok matthieu@ shadchin@ kettenis@


# 1.279 11-Jan-2015 kettenis

Tolerate checksum errors in ACPI tables (but complain about them). Apparently
there are machines out there with broken BIOSen with such checksum errors in
thier RSDT tables. Windows runs fine on these machines, so it probably does
not check. Linux ignores checksum errors as well (and prints a warning).

ok deraadt@, krw@


# 1.278 18-Dec-2014 deraadt

suspend and resume the RNG (at what looks like the right spots)
ok reyk djm


# 1.277 09-Dec-2014 doug

Replace some malloc(n*size,...) calls with mallocarray().

ok tedu@ deraadt@


# 1.276 06-Dec-2014 mpi

If "machdep.lidsuspend" is not set, do not go back to sleep if the lid
is still closed when resuming. This allows docked laptop to be resumed
without opening them.

ok mlarkin@


# 1.275 23-Nov-2014 mlarkin

If a machine has no _LIDs, then none can be open. This diff fixes a problem
where machines without any _LIDs were going back to sleep after hibernate
due to the recent suspend-if-lid-closed-on-resume diff. Noticed on qemu,
which doesn't have any _LIDs.

discussed with deraadt


# 1.274 18-Nov-2014 krw

Nuke yet more obvious #include duplications.

ok miod@


# 1.273 08-Nov-2014 mlarkin

If resuming from sleep (zzz/ZZZ) and the lid is still closed, go back to
sleep. This prevents accidental lid flex or slight opening in a backpack
from waking the machine up and leaving it resumed (powered on).

ok deraadt@


# 1.272 26-Sep-2014 kettenis

Rework piglet and pig allocation. Currently the piglet gets allocated
deep down in the suspend path, where it is really hard to recover from
allocation failure. So allocate the piglet early on in the suspend path.
Also change the piglet and piglet allocation functions to use km_alloc(9)
instead of doing pmemrange magic. This removes a bunch of code which, in the
case of the piglet allocation, is broken since it results in a NULL pointer
dereference. Also switch the piglet allocation to not wait. If we can't
allocate 16MB of phys contig memory on a halfway modern machine we're almost
certainly under a lot of memory pressure and we're better off not trying to
hibernate anyway.

ok mlarkin@


# 1.271 25-Sep-2014 kettenis

Bring back rev 1.249. Now that mp_setperf() has been fixed, it should be
safe to restore perflevel when resuming. Should fix the issues people
see with the processor running at full speed heating up the machine while
the apm command reports that the CPUs are running at a low speed.

ok dcoppa@, deraadt@


# 1.270 23-Sep-2014 kettenis

Release the acpi lock around calls to wsdisplay_suspend() and
wsdisplay_resume(). The X server may adjust the display brightness just
before/after the VT switch when suspending/resuming. In fact, it may
already be blocked adjusting the display brightness for some reason
unrelated to the suspend request.

The VT switch request will eventually time out, but the delay is annoying
and the failed VT switch will lead to problems after resume.

Needed for a future update of the xf86-video-intel driver.

ok mlarkin@


# 1.269 19-Sep-2014 kettenis

Use config_suspend_all(9).


# 1.268 08-Sep-2014 kettenis

Clear the PME Status bit on PCI devices in response to Device Wake events.
Fixes the interrupt storm on the 2009 Mac mini with WOL enabled on its
nfe(4) interface.

ok mlarkin@


Revision tags: OPENBSD_5_6_BASE
# 1.267 20-Jul-2014 mlarkin

Support hibernating to softraid crypto volumes.

much help and ok from deraadt@


# 1.266 16-Jul-2014 mlarkin

Reenable hibernate RLE support and flush+zero all memory after unpack.


# 1.265 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.264 12-Jul-2014 kettenis

Add a function to drop all clean pages on the page daemon queues and call
it when we hibernate.

ok mlarkin@, miod@, deraadt@


# 1.263 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.262 11-Jul-2014 mlarkin

Flush the buffercache to 16MB on hibernate and restore its previous max
size (kern.bufcachepercent) on resume, for better hibernate performance.

ok beck@


# 1.261 10-Jul-2014 blambert

remove unneeded header

ok mlarkin@


# 1.260 01-Jul-2014 kettenis

Adding an additonal delay after each access to SystemMemory or SystemIO
during autoconf makes no sense.

ok deraadt@


# 1.259 23-Jun-2014 kettenis

SystemMemory is used to access memory mapped registers on some machines, so we
must use the bus_space(9) API to access it instead of memcpy(9). Also make
sure we properly align access to fields that need more than byte alignment.

ok guenther@


# 1.258 21-May-2014 mlarkin

Fix invalid / missing format specifiers in several dnprintf statements
(used only when ACPI_DEBUG is enabled)

From a diff on tech@ by Fabian Raetz, with a minor change by me, tested
on i386 and amd64 with ACPI_DEBUG enabled and disabled.


# 1.257 25-Apr-2014 mlarkin

Defer the free of the hibernate structures until later, when it is safe to
do so.

Freeing the hibernate structures too early leads to inadvertent calls into
splx when we aren't ready for them yet. Found when trapping splx calls in
the resume path.

discussed with deraadt@ at m2k14


# 1.256 13-Apr-2014 mlarkin

Fix a badly worded debug message that implied the entire machine was going
to sleep, instead of just the acpi thread going to sleep.


# 1.255 31-Mar-2014 mpi

Change the order of operations during a suspend/resume cycle and
call bufq_quiesce() after executing the DVACT_QUIESCE handlers.

This should be safe since no disk nor controller drivers have such
handler but it will allow us to detach sd(4) devices attached to a
USB bus.

Another benefit pointed out by kettenis@ is that drivers that need
to read a firmware from the disk should be able to do it at resume
time in a DVACT_WAKEUP handler.

ok kettenis@, deraadt@


# 1.254 13-Mar-2014 dlg

get rid of the assumption that the head of the alldevs list is the
"mainbus" device. this breaks when mpath is enabled because it
attaches before mainbus and therefore takes the head position.

have autoconf provide device_mainbus() which looks up mainbus_cd,
and use that instead.

discussed with deraadt who just wants mpath stuff to move forward
despite there being many ways to shine this particular turd.


Revision tags: OPENBSD_5_5_BASE
# 1.253 21-Feb-2014 deraadt

Do the lid control further on in the resume sequence, since this is
AML, and will do WAITOK operations. While cold, that is a bad idea.
We seem to be safe with respect to the spec, I do not see any event
debounce issues here.
ok kettenis, tested by many


# 1.252 20-Jan-2014 claudio

Instead of checking sc->sc_revision <= 1 check that the
fadt->hdr_revision <= 1. This is a better check since it checks
the version of the proper descriptor. This allows the alix APU
to reboot since those boards have currently a wrong sc->sc_revision.
Committing this now so that it can be tested easier.
With and OK jsg@ also fine by derradt@


# 1.251 24-Dec-2013 kettenis

If the FADT has its SMI_CMD set to zero, assume we're only ACPI-only hardware
and don't need to disable SMI ownership of the ACPI hardware registers.

ok mlarkin@


# 1.250 23-Dec-2013 kettenis

Revert previous commit. It makes resume hang on some MP systems running
GENERIC.MP.


# 1.249 16-Dec-2013 kettenis

Restore hw.setperf upon resume like we do for apm(4) on i386. Seems we need
to do this fairly late in the resume to avoid hangs.

ok deraadt@, mlarkin@


# 1.248 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


# 1.247 06-Nov-2013 mpi

Add support for Power Resources for Dx states and the necessary hook
for PCI devices. This hook should be called twice, before and after
changing the power state of a PCI device.

Before setting the device to the new state, the ACPI layer will notify
every power resources linked to the device for that state and make sure
they are turned "_ON". After changing the state of the device, it will
decrement the reference of every power resources linked to that device
for the old state and turn them "_OFF" if they are no longer referenced.

This fixes the no-USB after resume problem seen on various ThinkPad,
problem initialy diagnosed with Alexander Polakov.

ok kettenis@, deraadt@


Revision tags: OPENBSD_5_4_BASE
# 1.246 01-Jun-2013 mlarkin

Stop printing ACPI wakeup devices in dmesg after the 16th wakeup device.

Helps with VMware VMs that report hundreds of wakeup devices.

ok reyk@, deraadt@


# 1.245 31-May-2013 bluhm

Fix the build for a kernel without wd and pciide in its config.
OK deraadt@ tedu@


# 1.244 30-May-2013 deraadt

Enforce ca_activate tree-walks over the entire heirarchy for all events,
cleaning up some shutdown-hook related code on the way.
(A few drivers related to sparc are still skipped at kettenis' request)
ok kettenis mlarkin, tested by many others too


# 1.243 18-Apr-2013 deraadt

pin the acpi thread to the BSP, because AML and SMI cannot be trusted. I
was certain we were doing this already, but it turns out no...


Revision tags: OPENBSD_5_3_BASE
# 1.242 24-Dec-2012 guenther

In acpiioctl(), Return though the splx() path if suser() returns an error

ok deraadt@


# 1.241 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


# 1.240 04-Oct-2012 ehrhardt

Use information provided by ACPI to attach secondary PCI host bridges.

ok kettenis@


# 1.239 07-Sep-2012 kettenis

Add a new function called acpi_pci_min_powerstate() that returns the lowest
power state for a PCI device for the power state (currently S3 or S4) we're
transitioning into. This function is probably too simplistic; there is a lot
of (silly) complexity in the ACPI spec.

ok deraadt@


Revision tags: OPENBSD_5_2_BASE
# 1.238 13-Jul-2012 pirofti

Disable acpivideo(4) on ASUS laptops.

There are some models that panic whenever an event is received.
Some cope, some don't. In order to get this right acpiec(4) requires
major rework. Until then let machines boot.

Discussed with deraadt@.


# 1.237 11-Jul-2012 mlarkin

for now, restrict hibernate to root in the acpi ioctl kernel interface.
normal rules for controlling this interface through apmd still apply.

ok deraadt@


# 1.236 09-Jul-2012 deraadt

tiny little oops in untested hibernate block


# 1.235 09-Jul-2012 deraadt

clean up ioctl() case statements


# 1.234 09-Jul-2012 deraadt

acpi_disable_onegpe() is not used; ok pirofti


# 1.233 24-May-2012 kettenis

If _MIN > _MAX consider a WordBus resource to be invalid. Fixes a panic on
the IBM x3550-M4.

ok mikeb@


# 1.232 29-Mar-2012 mlarkin

Missing #ifdef in previous diff


# 1.231 29-Mar-2012 mlarkin

Prevent hibernation attempts on machines with unsupported disk controllers.
The same check is also performed later in the hibernate sequence, but by
then it's too late to cleanly unwind (presently).

ok deraadt


# 1.230 26-Mar-2012 deraadt

simplify conditionals in acpi_sleep_state()
ok mlarkin


# 1.229 26-Mar-2012 deraadt

hook in the hibernate request code; half of this diff is from mlarkin
ok mlarkin


Revision tags: OPENBSD_5_1_BASE
# 1.228 20-Sep-2011 deraadt

Very early in suspend while the clock is still ticking realtime,
save the clock back to the rtc. The zaurus already did this.
ok phessler miod kettenis


Revision tags: OPENBSD_5_0_BASE
# 1.227 02-Jul-2011 nicm

kqueue attach functions should return an errno or 0, not a plain 1. Fix
the obvious cases to return EINVAL and ENXIO.

ok tedu deraadt


# 1.226 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.225 16-Jun-2011 pirofti

Import acpitoshiba driver.

This is originally written by Hiroyuki Aizu, ported and integrated
in our tree by Javier Vazquez <javama4 AT gmail> with tweaks and
suggestions by me.

Import prodded by deraadt@ and the lack of testers on tech@.


# 1.224 27-Apr-2011 jcs

attach acpithinkpad to newer lenovo models like the x120e

ok deraadt@


# 1.223 22-Apr-2011 jordan

Fix uninitialzied variables and formatting strings (-Wxxx errors)


Revision tags: OPENBSD_4_9_BASE
# 1.222 02-Jan-2011 jordan

Re-add code for ACPI taskqueue, eliminates need for state vars in acpi softc
Serialize processing ACPI tasks for sleep/powerdown/GPE event
ok deraadt@


# 1.221 31-Oct-2010 guenther

Revert last commit: it breaks resume on Thinkpads
"then please back it out" deraadt@


# 1.220 26-Oct-2010 jordan

Add task queue for ACPI gpe and notify handlers
This fixes eject on Dell Latitude dock and an issue on the Dell Mini
battery update.
ok deraadt


# 1.219 07-Oct-2010 jordan

Create separate functions for enabling wake and runtime gpes.
Remove old suspend/resume gpewalk function
ok deraadt, mlarkin


# 1.218 05-Oct-2010 jordan

split acpi_enable_onegpe to separate enable/disable functions
ok deraadt


# 1.217 05-Oct-2010 jordan

Added acpi_disable_allgpes function to clear GPE status
ok deraadt


# 1.216 05-Oct-2010 deraadt

clean up acpi_softc use more


# 1.215 05-Oct-2010 deraadt

delete a bit of #if 0 code


# 1.214 05-Oct-2010 deraadt

hide use of our global acpi_softc pointer a bit better


# 1.213 29-Sep-2010 deraadt

grab the acpi thread lock very early in acpi thread startup; not that it
is likely that anyone will race us... but why be careless.
ok kettenis


# 1.212 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.211 29-Aug-2010 jasper

- add a check for FWRITE here too.

ok miod@


# 1.210 27-Aug-2010 kettenis

Make PCI Power Management optional, and only enable it when acpi(4) attaches.

ok deraadt@


Revision tags: OPENBSD_4_8_BASE
# 1.209 08-Aug-2010 kettenis

Close race between the acpi thread and wscons ioctls running in process
context. We might rip this out post-release and replace it with a solution
that serializes things in a more obvious way.

ok deraadt@, miod@ and "those crazy texans who ok anything"


# 1.208 08-Aug-2010 deraadt

Report the acpi sleep states when on a RAMDISK; ok krw


# 1.207 07-Aug-2010 kettenis

Enforce acpi register access rescrictions.

ok mlarkin@, deraadt@, tested by marco@


# 1.206 07-Aug-2010 deraadt

upon resume, notify all the acpiac and acpibat drivers to update
their status. some laptops do not do this automatically.
ok kettenis canacar


# 1.205 06-Aug-2010 marco

Always call _PSW on _LID devices that support it to enable lid open to
resume the machine. Conversely disable it when the machine wakes up.

Tested by several ok deraadt


# 1.204 06-Aug-2010 deraadt

As part of a revamp of the PM1/GPE code, I write this diff to look for
unmanaged EN & STS bits in the PM1 register at interrupt time and report
them. As a side effect this splits the STS acknowledgement into two writes
(for power, and sleep) instead of one. The printf that is added (to spot
unmanaged STS bits) has not yet been triggered as far as we know.

Before the "write to PM1 registers at the right offset" diff went in, this
was not neccessary. But newer thinkpads do not have a working soft power
button without this diff.

We have no idea why.
ok mlarkin kettenis


# 1.203 05-Aug-2010 deraadt

PM1 power button events were generating 'fake events' to acpibtn PWRB,
but if there is no such device these were being tossed. Instead, arrange
for all power button handlers to flag a softc variable which is processed
by the thread; so that one place will know to request a powerdown (psignal
to init)
ok marco mlarkin


# 1.202 05-Aug-2010 deraadt

rename a variable which is annoyingly poorly named


# 1.201 05-Aug-2010 mlarkin

Write to the correct offset for various ACPI pmreg registers. Writing to
the correct register does properly clear the wake status bits and fixed
event bits, which allows some machines to have more than 1 suspend cycle.

bug spotted by deraadt@

tested by deraadt@ and myself on a half-dozen machines and others on
hackers@

ok deraadt@, marco@


# 1.200 05-Aug-2010 deraadt

Fix the power button. The pm1 register is 16 bits in size.
My mistake, a while back.
tested by krw


# 1.199 04-Aug-2010 kettenis

Print supported sleep states.

ok marco@, phessler@, deraadt@


# 1.198 03-Aug-2010 mlarkin

Use the proper flag for re-enabling certain hardware events (power btn,
sleep btn). The (incorrect) flag prevoiously used coincidentally had the
same value, so this shouldn't cause any different behavior than before.

tested on a variety of machines (i386, amd64, sp, mp) - no behavioral
change seen

ok deraadt@


# 1.197 28-Jul-2010 marco

Make all gasio failures look and behave the same.


# 1.196 28-Jul-2010 mlarkin

Delay a little longer on suspend before giving up. Fixes machines that
sometimes don't suspend because the sleep transition takes too long
(symptoms like failing to sleep but the backlight stays on).

ok deraadt@


# 1.195 27-Jul-2010 deraadt

remove XXX; EC can now be attached early when needed; ok jordan


# 1.194 27-Jul-2010 deraadt

more brackets feels good


# 1.193 27-Jul-2010 deraadt

XXX annotate a block in acpi_enter_sleep_state() that is pretty worrying


# 1.192 27-Jul-2010 deraadt

horrific indentation that nearly made me puke


# 1.191 27-Jul-2010 mlarkin

Set the sleeping indicator light on machines that support it, in case SMI
doesn't do it for us. Shows the 'moon' LED on older Thinkpads when
sleeping.

Tested on T43p, W500, T510 by me and on X61s by deraadt@.

ok deraadt@


# 1.190 27-Jul-2010 jordan

Early initialization of acpiec if ECDT table exists
Fixes hang when booting thinkpads while docked
ok deraadt


# 1.189 26-Jul-2010 deraadt

remove merge error which was commited


# 1.188 26-Jul-2010 pirofti

Add support for Sony ACPI hotkeys via a new driver: acpisony(4).

Currently it only works for the suspend button and tries to do right for
the brightness events, but I haven't found a Sony laptop that like to do
right so far.

In the future I want to make the brightness keys work on all Sony's and
also add support for the zoom hotkeys and whatever other funky keys I can
find on those things.

Okay deraadt@.


# 1.187 22-Jul-2010 deraadt

pretty large cleanup of SMALL_KERNEL


# 1.186 21-Jul-2010 deraadt

in the acpi GPE handler, late ack level interrupts not edge interrupts.
Duh.


# 1.185 21-Jul-2010 deraadt

Merge the edge and level gpe handlers into one (now possible since the
gpe structure tells us whether the gpe is edge or level)


# 1.184 21-Jul-2010 deraadt

delete #if 0 code we are not going to use; ok marco


# 1.183 20-Jul-2010 deraadt

make the mask code for acpi_enable_onegpe() match other similar code


# 1.182 20-Jul-2010 deraadt

Handle the acpi interrupt controller with proper edge/level handling,
wrapped in spltty to avoid racing against the interrupt controller.
Repair the gpe bit masking code while there, and do operations in the
right order
ok kettenis mlarkin, help from jordan at figuring out the order of
operations.


# 1.181 20-Jul-2010 matthew

When we're preparing to go to sleep, only call bufq_restart() if other
preparations failed.

ok kettenis@, thib@, deraadt@


# 1.180 19-Jul-2010 deraadt

permit compilation with ACPI_DEBUG


# 1.179 19-Jul-2010 deraadt

there is no need for a seperate dsdt_softc pointer; dsdt does not have a
softc but is just a part of the single acpi driver.
ok jordan


# 1.178 19-Jul-2010 deraadt

acpi_delay() is no longer needed; ok jordan


# 1.177 18-Jul-2010 mlarkin

Clear event status on resume, to avoid going back to sleep/reboot/shutdown
immediately after resume. Tested by a bunch of people on hackers@ on
various hardware. Fixes at least Toshiba NB200 and Gateway LT3103u.

ok marco


# 1.176 13-Jul-2010 deraadt

rename some thread related interfaces so they are logical


# 1.175 10-Jul-2010 jordan

Verify _ADR bdf is correct (some systems return 0xffff)
Works on Dell x9xx and Studio 1555


# 1.174 09-Jul-2010 jordan

Clear the noise


# 1.173 08-Jul-2010 jordan

Add mapping for ACPI device to PCI bdf (match autoconf tree)
Simplify resource parsing function to use buffer argument
Convert namespace linked lists to use queue macros
ok marco@, deraadt@


# 1.172 06-Jul-2010 deraadt

Use spltty() to lock downcalls from apm against the information being
modified by the acpi thread.
ok kettenis


# 1.171 06-Jul-2010 deraadt

Don't use a workq to do the suspend, because it races aginst the acpi
thread. Instead, just tell the acpi thread to do the suspend for us.
This makes apmd & zzz work correctly. While here, have acpithinkpad
attempt to post the event to apm before prompting the suspend itself.
ok kettenis marco mlarkin


# 1.170 05-Jul-2010 mlarkin

Reset register SCI_EN on resume. The ACPI spec clearly states we don't have
to do this, but many machines don't, or forget to, which leads to all sorts
of problems on resume (broken battery stats, overheating, failure to resume
more than once, etc).

tested by many, ok marco@, deraadt@


# 1.169 01-Jul-2010 thib

do bufq quiesce again, since vnd/the bufq_done call in
biodone has been fixed.


# 1.168 01-Jul-2010 jordan

Backout recent AML changes, breaks on R210 and others


# 1.167 01-Jul-2010 jordan

Simplified aml_parse_resource call, cleanup code
ok marco


# 1.166 01-Jul-2010 jordan

Fix subordinate bus # for multi-root PCI buses
ok marco, kettenis


# 1.165 30-Jun-2010 thib

Disable/partially backout the bufq quiesce changes as this
is causing havoc with vnds and release must be buildable.


# 1.164 29-Jun-2010 deraadt

Fix ramdisks (new code was placed inside a SMALL_KERNEL chunk)


# 1.163 29-Jun-2010 jordan

Add support for mapping ACPI to PCI devices
ok kettenis, deraadt


# 1.162 29-Jun-2010 kettenis

Call bufq_quiesce() on suspend to stop all I/O and bufq_restart on resume.
Makes suspend/resume work much better while doing disk I/O.

ok mlarkin@


# 1.161 29-Jun-2010 tedu

60 is a more reasonable guess for number of minutes in an hour than 100.
spotted by TeXitoi. ok deraadt


# 1.160 27-Jun-2010 jordan

Add _acpi_matchhids routine, precursor to pci device match


# 1.159 27-Jun-2010 mlarkin

Clear ACPI fixed event status on resume (power buttons/etc) so that some
machines don't immediately resume after suspending to S3.

Tested by kettenis@ and I on 5 or 6 machines, deraadt@ "yeah, looks good"


# 1.158 27-Jun-2010 jordan

Cleaned up acpi_maptable, adds to table list directly


# 1.157 07-Apr-2010 deraadt

If we have done a wsdisplay_suspend(), and ACPI decides it does not want to
suspend, we must roll back by calling wsdisplay_resume() or the virtual
console switching will be locked forever
ok oga


# 1.156 07-Apr-2010 kettenis

Make suspend/resume work on MP machines (running an MP kernel). Joint work
from mlarkin@ and me, with some amd64 fixes thrown in by deraadt@

ok marco@, deraadt@, pirofti@, mlarkin@


# 1.155 31-Mar-2010 kettenis

Bring /dev/apm support in line with apm(4). Make the suspend button send
a suspend request event to apmd(8) instead of suspending immediately. Also
keep track of whether /dev/apm and /dev/apmctl are currently open, such that
we can still suspend immediately if apmd(8) isn't running.

ok deraadt@, marco@, pirofti@, jsing@, oga@


# 1.154 30-Mar-2010 oga

Prevent the apmd/x races for good.

When we hit suspend time, go through all wsdisplays on the system. if
they are in mode MAPPED, but not MODE_DUMBFB then if possible do a full
vt switch to a !mapped vt, and prevent switching back until resume time.
This has to be called from MD code because this involves userland
running so that X can run the vt switch signal handler. This way, any
case where we are using the "poke registers from userland" model, we
will not be on the hardware when we go down, so the kernel can actually
handle thing properly.

Tested on several acpi laptops (by kettenis@ and ian@), x40 (me and
beck@ at LEAST) and zaurus (me). Maybe others, but if so I forgot who at
this time..

Idea from deraadt somewhere over the Faroe Islands (I thought of a
similar thing myself a while ago). Much prompting from him. Ok and
comments miod@


# 1.153 25-Mar-2010 oga

Fix & vs. && in the kqfilter code.

Based on a comparison with the apm code.

ok deraadt@, kettenis@


Revision tags: OPENBSD_4_7_BASE
# 1.152 26-Nov-2009 mlarkin

Visual (and on some machines, audio) feedback while the machine is
resuming via calls to aml _SST method. This also makes the "moon" LED on
thinkpads to blink during resume and cease when resume is completed.

ok pirofti@


# 1.151 26-Nov-2009 deraadt

Toggle into "cold" when we we disable interrupts, because tsleeping
in the AML interpreter and context switching into userland processes
tends to sometimes pop the ACPI trampoline page out of our address
space. Makes lots more machines work.
ok kettenis mlarkin


# 1.150 24-Nov-2009 jsg

in the resume path evaluate _WAK before dealing with device drivers
like the spec says, not after.

ok mlarkin@


# 1.149 24-Nov-2009 deraadt

abort suspends on SMP systems until that gets written; ok mlarkin


# 1.148 23-Nov-2009 mlarkin

pci should return the result of it's children's suspend/resume calls back
to its parent.

handle suspend failure case and unwind if devices have an activate function
that returns failure.

ok deraadt@, kettenis@


# 1.147 23-Nov-2009 mlarkin

acpi* devices need to call their children on suspend/resume.
ok deraadt@


# 1.146 23-Nov-2009 pirofti

Remove ACPI_SLEEP_ENABLED checks.

This enables by default the suspend/resume paths in the kernel.

Okay deraadt@.


# 1.145 23-Nov-2009 deraadt

knf and spacing that are super annoying


# 1.144 23-Nov-2009 mlarkin

Hook up Theo's new children suspend/resume function, as well as more ACPI
suspend/resume glue.

ok deraadt@


# 1.143 26-Oct-2009 deraadt

Do not do strncmp comparisons on dv_xname because longer device names which
look similar could arrive in the future. Instead, compare directly against
dv_cfdata->cf_driver->cd_name
Issue originally spotted by miod


# 1.142 26-Oct-2009 jordan

Verify checksum+address when loading ACPI tables.
Some systems had invalid entries in RSDT/XSDT.
ok marco@


# 1.141 23-Jul-2009 cnst

New aibs(4) driver for ASUSTeK AI Booster (ACPI ATK0110) hardware monitoring,
with sensor state support through limits provided by the ACPI.

Tested on several ASUS motherboards kindly networked by Sam Fourman Jr.

ok deraadt marco jordan


Revision tags: OPENBSD_4_6_BASE
# 1.140 03-Jun-2009 pirofti

Import acpi power resource driver. This is needed for suspend/resume logic.

Okay jordan@ and marco@.


# 1.139 03-Jun-2009 pirofti

Add acpivideo support. This adds brightness support for all laptops
except thinkpads, they will use the acpithinkpad driver. The driver is
also hooked into wsconsole. So brightness can be adjusted via:

$ wsconsctl display.brightness=<percentage>

This is very helpfull on some laptops that have a nasty bios and get two
steps instead of one when pressing the brightness button.

Tested on various dell, fujitsu, acer, samsung and other laptops.

Okay marco@, miod@. Suggestions from kettenis@.
Lots of reviews and help from miod@, thanks!


# 1.138 03-Jun-2009 jordan

Added ejectable bay for IDE cdroms
ok marco@


# 1.137 30-Apr-2009 marco

Oga changed apm ioctls and forgot to grep. Temporary work around by hitting
the sleep button when we get any request. This interface will chnage at
some point.
ok oga


# 1.136 19-Apr-2009 krw

Call acpi_sleep_walk() when powering down just like when entering
any other sleep state. This tells acpi wake devices to not wake up
the box until manual powering up resets their status. Makes my Dell
GX520 stay powered off in response to 'halt -p'.

In snaps for a while.

ok marco@ (several times) deraadt@


# 1.135 17-Apr-2009 pirofti

Add device ranking support for future sleep/wake developments.

okay marco@


# 1.134 11-Mar-2009 jordan

Cleanup.. use aml_evalinteger instead of aml_evalname
ok marco@


# 1.133 10-Mar-2009 jordan

Fix cut/paste error.. call _BFS not _PTS
ok marco@


Revision tags: OPENBSD_4_5_BASE
# 1.132 21-Feb-2009 marco

overzealous cut/paste; found by Vladimir Kirillov <proger@uaoug.org.ua>


# 1.131 20-Feb-2009 marco

Oops didn't mean for Debugger() to remain there.


# 1.130 19-Feb-2009 marco

suspend/resume bits so that we can develop this in tree. This is disabled.
code from mlarkin and me
help from art,toby,jordan and several others
ok jordan, go for it deraadt


# 1.129 10-Feb-2009 jordan

Fix null pointer for shutdown if acpi not enabled
ok marco@


# 1.128 04-Feb-2009 kettenis

Some systems that claim to be ACPI 2.0 apparently don't provide an XSDT
address. Make the code fall back on the RSDT on those systems.

From Joerg Niendorf <p3y80@internode.on.net>. Fixes PR 5986.


# 1.127 17-Jan-2009 krw

A couple of stray malloc/memset -> malloc(M_ZERO), a bit of knf and
lose a couple of unnecessary casts. No functional change.

Feedback from kettenis@

ok kettenis@ marco@


# 1.126 06-Nov-2008 marco

Collapse all sanity checks of hids into one function.

Originaly from jordan


Revision tags: OPENBSD_4_4_BASE
# 1.125 02-Jul-2008 fgsch

add acpivideo. for now almost an empty skel so others can work on
suspend/resume. eventually it will also manage output switching and
brightness where supported. prodded by marco@


# 1.124 11-Jun-2008 marco

Fix $OpenBSD$ for once and for all


# 1.123 08-Jun-2008 deraadt

aaa_dev was being set from an uninitialized variable; ok toby


# 1.122 07-Jun-2008 marco

Kill ACPIVERBOSE. It was never used and is worthless to boot.

comments jmc
ok toby


# 1.121 06-Jun-2008 marco

Shrink acpi some more for floppies and while here re-enable acpi on the
floppies. Fixes crazy sony vaio kettenis has.

tested by deraadt
ok deraadt jordan kettenis


# 1.120 01-Jun-2008 marco

Fix \n nit in SMALL_KERNEL.


# 1.119 01-Jun-2008 marco

Re-enable new parser. Lots of testing by various folks. Fits all media
now too.

ok kettenis deraadt


# 1.118 16-May-2008 dlg

back out jordans new parser.

it didnt have enough oks from the right people, it did break machines
that were previously working, and it bloated the RAMDISKS and install
media beyond the constraints of the media.

requested by deraadt@


# 1.117 14-May-2008 jordan

Adding new ACPI Parser code
Old guts of code still exists, needs to be torn out next
ok marco@
ok brad@


# 1.116 27-Apr-2008 jcs

add acpithinkpad(4), an acpi support driver for ibm/lenovo
thinkpads.

provides functionality that the bios did on older, non-acpi
thinkpads like respond to screen brightness keys and toggle the
bluetooth device on and off.

ok deraadt@ and marco@


# 1.115 24-Apr-2008 jsing

Introduce acpiasus(4), a driver for the ACPI based hotkeys found in many
ASUS laptops (including the ASUS EeePC) - largely based on NetBSD's
asus(4) driver. On the ASUS EeePC this allows us to enable/disable
wireless, change screen brightness and use the volume keys.

ok jsg@, weingart@


# 1.114 12-Apr-2008 kettenis

Get rid of acpi_s5 global variables; simply send SIGUSR2 instead of SUGUSR1
to tell init(8) to power down the machine.

ok krw@


Revision tags: OPENBSD_4_3_BASE
# 1.113 21-Jan-2008 jsg

Check against RESET_REG_SUP in FADT before doing the acpi reset dance
after all, not doing so appears to break more machines than it fixes.

ok marco@


# 1.112 05-Jan-2008 kettenis

Never attach more acpicpu(4) devices than cpu(4) devices. Always attach
the first acpicpu(4) device on non-MULTIPROCESSOR kernels, regardless of its
ACPI processor ID.

ok mikeb@


# 1.111 05-Dec-2007 deraadt

Hang acpi(4) below bios(4) instead of mainbus(4). This lets us move acpi(4)
before pcibios(4), and then.... if acpi(4) attaches, skip pcibios(4) since
it messes the machines up. Fixes claudio's HP dl320 G5, and almost assuredly
others.
ok kettenis


# 1.110 05-Dec-2007 deraadt

repair acpiopen/acpiclose and friends to handle the /dev/apmctl interface,
so that apmd(8) can actually work correctly against acpi(4), and thus,
even do the fancy -C/-A stuff; also tested by jmc


# 1.109 02-Dec-2007 fgsch

fix the trailing space for real now.


# 1.108 02-Dec-2007 fgsch

avoid trailing spaces on output; marco@ ok.


# 1.107 27-Nov-2007 kettenis

Fix off-by-one that made us attach acpicpu0 and acpicpu1 on GENERIC kernels.

ok marco@


# 1.106 25-Nov-2007 jsg

When available and ACPI is enabled use the ACPI reset method
to reboot. This is required for some quirky sis machines
and likely others to reboot.

Tested by many. 'commit early commit often' deraadt@


# 1.105 19-Nov-2007 deraadt

sigh, amd64 still uses that stupid acpi_enabled variable...


# 1.104 19-Nov-2007 deraadt

softc's come initialized to 0, you know...


# 1.103 19-Nov-2007 deraadt

store revision; from jsg


# 1.102 19-Nov-2007 kettenis

Initialize sc_facs earlier.

ok deraadt@, beck@, canacar@


# 1.101 16-Nov-2007 deraadt

fix the bus_space #define nightmare, so that amd64 and i386 are much more
uniform. as a result shared code like acpi needs less #ifdef's
ok marco kettenis


# 1.100 15-Nov-2007 deraadt

remove the #ifdef ACPI_ENABLE. the EC codepath is either safe enough (and
will be improved), or it is unsafe because AML assumes it must be there
ok almost everyone


# 1.99 15-Nov-2007 mikeb

Make acpicpu(4) attach to the existing CPUs only.

Problem reported by form@. Fix idea by kettenis@.
Lots of help from deraadt@. Tested by deraadt@, form@ and me.

ok deraadt marco


# 1.98 14-Nov-2007 deraadt

spacing


# 1.97 12-Nov-2007 deraadt

correct : vs , in attach-time messages


# 1.96 06-Nov-2007 deraadt

all ACPI_ENABLE kernels need acpi_interrupt()


# 1.95 06-Nov-2007 deraadt

move acpi_read_pmreg() and acpi_write_pmreg() out of SMALL_KERNEL so
that acpi ramdisks can compile (obvious solution)


# 1.94 05-Nov-2007 canacar

Establish the acpi interrupt earlier. Fixes problems with some
GENERIC.MP kernels not getting acpi interrupts. This is a
workaround until the real issue with late establishment is fixed.
ok weingart@, beck@, gwk@


# 1.93 03-Nov-2007 jordan

Added support for displaying wakeup devices
ok beck@,weingart@,gwk@


# 1.92 11-Oct-2007 kettenis

When walking the tree yo find _PRT methods, check whether the device is
actually enabled and functioning. Similar to what we do for _INI.

ok weingart@, beck@


# 1.91 08-Oct-2007 krw

More simple memset(,0,) -> M_ZERO changes. In this batch move to
size(*p) as the first malloc() parameter where p is declared locally
and thus easy to check. Add M_ZERO to gpe_table allocation in acpi.c
even though there is no obvious bzero or memset nearby.


# 1.90 13-Sep-2007 mk

Remove unnecessary memset() (see aml_evalnode(9)) and a bit of KNF.

ok toby


# 1.89 13-Sep-2007 weingart

Implement ACPI 6.5.1 spec tree walk for _STA and _INI.
Fixes mk's laptop. No regressions so far (thank you to the
testers).

ok gwk@, mk@, marco@


Revision tags: OPENBSD_4_2_BASE
# 1.88 17-Apr-2007 mk

Now that acpidock no longer hangs machines during boot (at least
according to the reports I've been getting) it should be safe to enable
it again, so remove the #if 0 stuff around it.

ok marco


# 1.87 11-Apr-2007 jordan

Added changes for C-State
ok marco@


# 1.86 26-Mar-2007 marco

Calculate minutes left more accurately.

From giovanni <qgiovanni@gmail.com>


Revision tags: OPENBSD_4_1_BASE
# 1.85 03-Mar-2007 mk

Stop looking for acpidock until we fix that stuff that breaks all pre-60
series ThinkPads with acpidock(4) enabled.


# 1.84 22-Feb-2007 marco

We were getting no interrupts in .MP. There are 2 fixes; one in
acpi_machdep.c and move the interrupt establishment down after ioapic
has been initialized.

ok jordan


# 1.83 21-Feb-2007 kettenis

Make an acpi-enabled GENERIC.MP work on systems that only support PIC mode.

ok marco@


# 1.82 19-Feb-2007 jordan

Added acpi_map_address function for mapping GAS/IO registers
ok marco@


# 1.81 18-Feb-2007 jordan

Added dynamic perfect hash for opcodes
ok marco@


# 1.80 18-Feb-2007 marco

Shave off another K pre compression.
Shuffle SMALL_KERNEL functions around so that we only have a single #ifndef


# 1.79 17-Feb-2007 marco

Add SMALL_KERNEL throught acpi to make it fit on boot media.
ok deraadt


# 1.78 13-Feb-2007 marco

Add smarter _INI code. This helps with some dissapearing devices.

ok kettenis jordan


# 1.77 31-Jan-2007 gwk

Introduce acpi_hasprocfvs thats "ACPI Has Processor Frequency and Voltage
Scalling" a global flag set by acicpu attach to indicate that ACPI is
handling fvs through the _PSS objects. This will fix disappearing
EST/powernow when booting kernels with acpi enabled, as acpicpu isnt
compiled into GENERIC yet. Also in cases where acpicpu is built in and
legacy EST/powernow work but acpi is missing the _PSS object, the legacy
routines will be used.

tested by dim@, and Nick Nauwelaerts <nick AT nauwelaerts.net>
ok marco@, dim@


# 1.76 22-Jan-2007 mk

Actually look for docking stations by looking for _DCK nodes.

ok marco


# 1.75 02-Jan-2007 marco

On some systems EC must be attached as the first device. Move EC from
generic _HID devices to its own routine to force first attach. Bug found
and debugged by joris@

Code mostly written by joris@


# 1.74 26-Dec-2006 marco

Add polling to devices that require it such as acpibat and acpitz.
Use the same tsleep/wakeup mechanism as the interrupt code to keep it non-
concurrent.


# 1.73 21-Dec-2006 deraadt

even more knf love


# 1.72 21-Dec-2006 marco

Remove some leftover acpiec gpe/interrupt code.


# 1.71 20-Dec-2006 deraadt

oops, name already used


# 1.70 20-Dec-2006 deraadt

rename __acpi_enable_gpe(); ok marco


# 1.69 20-Dec-2006 deraadt

acpi_unmap_pmregs() is not used, ok marco


# 1.68 19-Dec-2006 deraadt

simplify some low-level methods substantially (shrinking code); ok marco jordan


# 1.67 06-Dec-2006 mk

malloc(..., M_NOWAIT) with no NULL check is bad. If allocation fails,
complain, clean up, and bail out of attach.

input and ok gwk


# 1.66 28-Nov-2006 jordan

Fixed clearing arg for acpi_init_pic
ok kettenis@


# 1.65 27-Nov-2006 kettenis

Don't spam dmesg.


# 1.64 27-Nov-2006 jordan

Fixes for acpiprt routing
Post-parse fixup for namerefs
ok kettenis@


# 1.63 25-Nov-2006 marco

Don't handle acpiev events inside the gpe handler since this causes deadlocks.

ok jordan@


# 1.62 15-Nov-2006 kettenis

Hook up acpi apic interrupt routing support to generic acpi code.

ok jordan@


# 1.61 02-Nov-2006 marco

Kill acpiec attach function and add its functionality to the generic _HID
device attach

prodded and ok jordan


# 1.60 02-Nov-2006 marco

Fix comments to reflect reality.


# 1.59 19-Oct-2006 marco

Replace ACPI ioctls with APM ioctls. This makes ACPI talk the same protocol
as APM therefore userspace can now retrieve power information in the same
fashion as all other apm arches (zaurus, macppc, i386 etc).

To use force the use of acpi device like so: apmd -f /dev/acpi

ok jordan@


# 1.58 19-Oct-2006 jordan

Fixed GPE debug levels
Fixed ref counter on temporary bufferfields
DSDT parsing fully working on IBM Thinkpad
ok marco@


# 1.57 15-Oct-2006 jordan

Removed old GPE handler code remnants
ok marco@


# 1.56 12-Oct-2006 jordan

Massive ACPI changes
Fixed ACPI leaks
new ACPI API
ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.55 30-Jun-2006 gwk

KNF, No functional change,
"go at it" marco@


# 1.54 30-Jun-2006 jordan

Support for calling Embedded Controller via new GPE interface
ok marco@


# 1.53 30-Jun-2006 jordan

Added new code for GPE handling
Also works with ACPIEC; need to fixup acpiec.c to remove original interrupt handler
ok marco@


# 1.52 30-Jun-2006 jordan

Added offset argument for acpi read/write pmreg routines (new GPE code)
ok marco@


# 1.51 31-May-2006 todd

do not call acpi_powerdown() unless acpi has successfully initialized
ok gwk@ canacar@


# 1.50 29-May-2006 canacar

Add embedded controller support.
ok marco@ deraadt@ testing & ok gwk@


# 1.49 19-May-2006 canacar

Minimal thermal zone driver. Monitors thermal zone temperature,
shuts down the system if the 'critical temperature' is reached.
ok marco@


# 1.48 11-Apr-2006 marco

KNF.


# 1.47 11-Apr-2006 gwk

Add acpi_resume(struct acpi_softc *sc), which calls some AML methods,
detected at attach time, currently unused.

ok marco@


# 1.46 11-Apr-2006 gwk

Discover some ACPI sleep related methods, and call them at the
appropriate time in acpi_enter_sleep_state().

ok marco@


Revision tags: OPENBSD_3_9_BASE
# 1.45 26-Feb-2006 marco

Add acpicpu as a device.

ok jordan@


# 1.44 23-Feb-2006 marco

remove dead function.

ok jordan@


# 1.43 22-Feb-2006 marco

Print out all tables it found during autoconf. We'll need this data in
the future.


# 1.42 22-Feb-2006 marco

Make dmesg less verbose.

ok jordan.


# 1.41 22-Feb-2006 jordan

Added parsing of SSDT tables
ok marco@


# 1.40 22-Feb-2006 jordan

Added new aml_register to notify by device id
Moved powerdown code to button device
ok marco@


# 1.39 21-Feb-2006 marco

Readjust debug levels that crept in too low over the last few days.
KNF


# 1.38 21-Feb-2006 marco

Rig the power button so that the machine shuts down correctly.


# 1.37 20-Feb-2006 marco

Add acpibtn device. Remains disabled for now.


# 1.36 19-Feb-2006 jordan

Added code for aml_notify GPE notification callback
ok marco@


# 1.35 19-Feb-2006 grange

Fix acpi_enter_sleep_state() by replacing current ugly hack
with mostly correct sleeping state transition code.

ok brad@ marco@


# 1.34 19-Feb-2006 marco

jordan:
Enable interrupts.
Added peripheral code to make this work.

marco:
Replace softintr and other types of interrupts with a handler with context.
Fixed interrupt magic for GPEs.
Some KNF

This is work done by jordan and I but has to be a single commit.


# 1.33 17-Feb-2006 marco

Remove earlier introduced delays. We really only need them during autconf.

ok jordan@


# 1.32 17-Feb-2006 marco

Add a generic delay function as per the ACPI spec.

from jordan@


# 1.31 17-Feb-2006 marco

Use tsleep instead of delay when !cold.


# 1.30 17-Feb-2006 marco

Argh, we need delays between reads/writes. The backend reads (i2c most likely)
are so slow that values come back as 0 sometimes. We have to do this
despite the spec saying we shouldnt have to. Guess having a faster
parser than others can hurt sometimes.

Idea from jordan.


# 1.29 17-Feb-2006 jordan

Fixed code for handling _INI
Rearranged debug statements
ok marco@


# 1.28 16-Feb-2006 jordan

Added stub for calling _INI
Fixed aml_dowhile for NULL return
ok marco@


# 1.27 16-Feb-2006 jordan

Fixed parser/evaluator to retrieve battery info+state
Cleanup of parser
Dell laptops return battery status
ok marco@


# 1.26 06-Feb-2006 brad

fix a typo so this will compile on amd64.


# 1.25 04-Feb-2006 marco

When ACPI_ENABLED wasn't set but the devices were it had an unknown variable.

Pointed out by ian@.


# 1.24 03-Feb-2006 jordan

Rewrote aml parser yet again...
Fixed bus_space_map for i386/amd64
Added _BST and _BIF to acpibat to get status/info
ok marco@


# 1.23 20-Jan-2006 jordan

More evaluator changes
Removed old evaluator code
Fixed _HID value
ok marco@


# 1.22 19-Jan-2006 jordan

Added support for read/write of PCI Config space
Help jason@
ok marco@


# 1.21 18-Jan-2006 jordan

Added new evaluation routines
Added helper methods for buffer/field evaluation
Fixed broken indentation on dsdt.c

ok marco@


# 1.20 17-Jan-2006 jordan

Major overhaul of the aml parser. Evaluater temporarily disabled.

ok marco@


# 1.19 06-Jan-2006 grange

Check for ACPI control availability only if ACPI_ENABLE defined.


# 1.18 06-Jan-2006 grange

Informative error messages on attach, prettier dmesg.


# 1.17 06-Jan-2006 grange

printf -> dnprintf conversion for debug messages.


# 1.16 05-Jan-2006 grange

Find SLP_TYPx values and store them for future use in
acpi_enter_sleep_state().


# 1.15 05-Jan-2006 grange

Random KNF and cleanup


# 1.14 05-Jan-2006 grange

Provide symbolic names for the acpi sleeping states and use it.


# 1.13 28-Dec-2005 marco

Moved DEVNAME to a acpidev.h

ok jordan@


# 1.12 18-Dec-2005 sturm

add back comment about wrong code

ok marco


# 1.11 16-Dec-2005 marco

Remove test for kbd_reset. Prompted deraadt@, ok jordan@


# 1.10 16-Dec-2005 jordan

Added hooks for powerdown/power button press


# 1.9 16-Dec-2005 jordan

Cleaned up common power management register i/o
Added enter sleep state ioctl
Hooked power button interrupt
ok marco@


# 1.8 16-Dec-2005 marco

Remove autoconf chatter


# 1.7 16-Dec-2005 jordan

Moved DSDT AML parsing into parent ACPI driver
ok marco@


# 1.6 14-Dec-2005 marco

Add AC device glue.


# 1.5 14-Dec-2005 marco

Adjust debug printing levels to something more sane.


# 1.4 13-Dec-2005 marco

Add glue to attach devices to apci.


# 1.3 07-Dec-2005 marco

KNF


Revision tags: OPENBSD_3_8_BASE
# 1.2 10-Jul-2005 grange

Spacing cleanup.


# 1.1 02-Jun-2005 tholo

Start on a basic ACPI framework -- does not do much more than read out the
ACPI tables into kernel memory and attach ACPI and HPET timers currently.

In order to test this code, enabling the devices in GENERIC as well as
the ACPI_ENABLE option is needed. This code does not do any thermal
control yet, so this should be done with care depending on the platform.

In the tree so more people can contribute to making this more fully
featured.

Ok niklas@ grange@ tedu@


# 1.409 04-Feb-2022 robert

set the APM_BATT_CHARGING state if the battery is being charged and try
to calculate the remaining time to be fully charged

ok jca@


# 1.408 02-Feb-2022 deraadt

acpi_addtask() calls malloc() w/ M_NOWAIT (because some calls come from
interrupt context), this however means occasional resource shortage will
result in callbacks registration failing, and unknown consequences for
the task-submitting caller.
Changing this to use pools with a low water mark, decreases the odds
of that problem occuring.
ok kettenis


# 1.407 01-Feb-2022 deraadt

GPE_DIRECT is calling the AML parser from interrupt context, which is
not permitted. Luckily nothing is using GPE_DIRECT anymore, so this code
can be deleted.
ok kettenis


# 1.406 26-Jan-2022 kettenis

An ACPI device needs to be both present and enabled for it to function.
So only attempt to attach hardware that has both bits enabled. This fixes
an issue where com(4) would attach for a disabled serial port leading to
misdetection of the hardware variant and a subsequent hang when /etc/rc
runs ttyflags -a.

ok anton@, deraadt@


# 1.405 12-Jan-2022 patrick

Make acpi_getpropint() return uint64_t, as ACPI integers are in fact that
wide and some _DSD properties depend on it.

ok kettenis@


# 1.404 04-Jan-2022 patrick

acpi_getprop() needs to actually make sure that we're looking at the
correct property. While there adjust acpi_getpropint() as well to
increase similarity with acpi_getprop().

ok kettenis@


# 1.403 16-Dec-2021 anton

Attach com over acpi on amd64. Some hardware uses a different interrupt
assignment compared to the the legacy one supported by com over isa.
This causes the console to halt once userland takes over as no
interrupts are received. The actual address and irq can be read from
ACPI, kettenis@ already added support for arm64 which paved the way for
amd64.

Some consoles that previously attached over isa are now expected to
attach over acpi.

Thanks to patrick@ for testing on arm64.

ok kettenis@


# 1.402 07-Dec-2021 kettenis

Fix parsing of SR_IRQ resource descriptors.

ok patrick@, anton@


# 1.401 02-Nov-2021 deraadt

knf


# 1.400 30-Oct-2021 deraadt

Change hw.perfpolicy=auto by default, at startup. If the system has AC
power connected (default is yes when no driver differentiates) then default
to 100% performance. On battery, use the existing auto algorithm (which is
admittedly somewhat unrefined).
This change overrides the system/BIOS speed and puts OpenBSD in control.
As this happens very early during boot, besides speedups in all usage usage
patterns, some surprises: unhibernate and sysupgrade times are cut in half.
note: on a few architectures, the setperf fn pointer is changed late, and
thus the auto algorithm stops timeing out. kettenis and i will look for
a solution.
in snaps for more than a week.
ok kettenis


Revision tags: OPENBSD_7_0_BASE
# 1.399 20-Jul-2021 mlarkin

Fix trailing whitespace in a few places.

No code change.


# 1.398 09-May-2021 kettenis

Some x86 machines advertise the "hardware reduced" ACPI feature, advertise
S4 and S5 support, but fail to populate the SLEEP_CONTROL_REG and
SLEEP_STATUS_REG descriptions in the FADT. An example of such a machine
is the ASUS Zenbook 14 UM433DA. Any attempt to powerdown the machine
will result in a kernel crash. It turns out that using the legacy ACPI PM
registers works fine on this machien. So fall back on those registers
if SLEEP_CONTROL_REG and/or SLEEP_STATUS_REG aren't provided.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_9_BASE
# 1.397 15-Mar-2021 patrick

Add acpi_iommu_device_map(), which replaces the DMA tag with one that
is blessed with IOMMU magic, if available. This is mainly for arm64,
since on amd64 and i386 the IOMMU only captures PCIe devices, as far
as I know, which uses the pci_probe_device_hook(). This though is for
non-PCI devices attached through ACPI.

ok kettenis@


# 1.396 07-Mar-2021 patrick

Pass standard DMA tag to acpi(4) table drivers.

ok kettenis@


# 1.395 27-Dec-2020 deraadt

acpi_map_address() cannot be wrapped by SMALL_KERNEL anymore, as
it is used by acpihpet.c


# 1.394 25-Dec-2020 visa

Refactor klist insertion and removal

Rename klist_{insert,remove}() to klist_{insert,remove}_locked().
These functions assume that the caller has locked the klist. The current
state of locking remains intact because the kernel lock is still used
with all klists.

Add new functions klist_insert() and klist_remove() that lock the klist
internally. This allows some code simplification.

OK mpi@


# 1.393 17-Dec-2020 kettenis

Fix some issues with referencing named ACPI nodes from Packages.
These references need to be resolved at runtime rather than when
they're parsed such that they pick up the right values for those nodes
which can be changed when for example _INI methods run.

The current approach is to replace these reference with a string that
names the node in question. The problem with that is that packages
can also contain normal strings. Which means that we need special
code that depends on the context where the package is used.

This diff takes a different approach by retaining a reference when
parsing. Code that uses a package will need to resolve this reference
but there is no ambiguiety anymore.

ok patrick@


# 1.392 05-Dec-2020 kettenis

Add support for the _EVT method to the GPIO support code.

ok patrick@


Revision tags: OPENBSD_6_8_BASE
# 1.391 27-Aug-2020 jmatthew

Reorder the acpi attach process so we look at Processor() CPU nodes
after Device() ones, since we should prefer the newer node type.
If we see any Device() nodes, don't attach acpicpu(4) to any Processor()
nodes if they're also present. This also makes acpitz(4) and
acpipwrres(4) devices attach slightly later.

ok kettenis@, also tested by jmc@


# 1.390 16-Aug-2020 gnezdo

Remove sc_maxgpe from acpi_softc which was never read from

Remove a dead conditional from acpi_init_gpes while there.

ok jcs@


# 1.389 21-Jul-2020 deraadt

acpi can use IPL_BIO (a low interrupt) since it only enqueues operations for
later processing. The use of a high interrupt will predate suspend/resume
efforts, we had to redesign acpi to be non-reentrant obviously
discussed with kettenis, in snaps for more than a week


# 1.388 24-Jun-2020 cheloha

kernel: use gettime(9)/getuptime(9) in lieu of time_second(9)/time_uptime(9)

time_second(9) and time_uptime(9) are widely used in the kernel to
quickly get the system UTC or system uptime as a time_t. However,
time_t is 64-bit everywhere, so it is not generally safe to use them
on 32-bit platforms: you have a split-read problem if your hardware
cannot perform atomic 64-bit reads.

This patch replaces time_second(9) with gettime(9), a safer successor
interface, throughout the kernel. Similarly, time_uptime(9) is replaced
with getuptime(9).

There is a performance cost on 32-bit platforms in exchange for
eliminating the split-read problem: instead of two register reads you
now have a lockless read loop to pull the values from the timehands.
This is really not *too* bad in the grand scheme of things, but
compared to what we were doing before it is several times slower.

There is no performance cost on 64-bit (__LP64__) platforms.

With input from visa@, dlg@, and tedu@.

Several bugs squashed by visa@.

ok kettenis@


# 1.387 10-Jun-2020 jca

Give userland a consistent battery and A/C state after resume

Previously we refreshed the state of acpiac(4) and acpibat(4)/acpisbs(4)
by queueing fake ACPI events to be processed by the acpi thread.
acpibat(4) using ACPIDEV_POLL meant that its status could be refreshed
first, resulting in an APM_POWER_CHANGE event sent to userland before
the state of acpiac(4) was up to date. Because of this, apmd(8) could
see A/C unplugged when it was in fact plugged, possibly leading to
a wrong decision.

Instead of using fake ACPI events, refresh the status of acpiac(4) and
acpibat(4)/acpisbs(4) right away at DVACT_WAKEUP time. To reuse the
existing refresh code of those drivers, make their ACPI *_notify()
handlers responsible for sending APM_POWER_CHANGE events instead. Also
queue an APM_POWER_CHANGE event right after resume to retain existing
behavior.

This fixes the consistency of data shown to userland, and makes those
drivers less special.

Initially discussed last year (!) with mpi@ at Miod's place.
Feedback and ok jcs@, deraadt@ agrees with the use of DVACT_WAKEUP here.


# 1.386 29-May-2020 deraadt

dev/rndvar.h no longer has statistical interfaces (removed during various
conversion steps). it only contains kernel prototypes for 4 interfaces,
all of which legitimately belong in sys/systm.h, which are already included
by all enqueue_randomness() users.


# 1.385 14-May-2020 kettenis

Bring back the acpi(4) changes. This time around only use the ACPI _CRS
information on ACPI 5.0 and later.

ok krw@, patrick@


# 1.384 11-May-2020 jca

More accurate remaining power reporting with disparate batteries

Overall remaining power was computed as the average of the remaining
power of each battery, in percents. This doesn't work with batteries of
different capacity, so use the total capacity and remaining power
instead.

Reported by lidstah, successful tests by lidstah and benno@, ok kettenis@


# 1.383 08-May-2020 kettenis

Move parsing of _CRS into acpi(4) and pass the parsed address and interrupt
information in struct acpi_attach_args.

ok mpi@


Revision tags: OPENBSD_6_7_BASE
# 1.382 14-Apr-2020 kettenis

Implement acpi_getprop(), the ACPI equivalent of OF_getprop().

ok patrick@


# 1.381 12-Apr-2020 kettenis

Add support for the _CCA method. This method indicates whether DMA is
cache-coherent or not. To implement this, acpi(4) gets two bus_dma tags
and passes the appropriate one when attaching devices based on _CCA.
On i386/amd64, where for all practical purpose DMA is always cache-coherent,
the two tags are the same. But on arm64 they are distinct.

ok patrick@


# 1.380 07-Apr-2020 visa

Abstract the head of knote lists. This allows extending the lists,
for example, with locking assertions.

OK mpi@, anton@


# 1.379 03-Apr-2020 mpi

Fix incoherencies of errors returned by various *kqfilter routines.

EOPNOTSUPP is returned when there's no "filterops" for a given fd.
EINVAL when the requested filter isn't supported by the "filterops".
ENXIO means the underlying device is no longer valid/present.

ok visa@


# 1.378 20-Feb-2020 visa

Replace field f_isfd with field f_flags in struct filterops to allow
adding more filter properties without cluttering the struct.

OK mpi@, anton@


# 1.377 31-Dec-2019 visa

Use C99 designated initializers with struct filterops. In addition,
make the structs const so that the data are put in .rodata.

OK mpi@, deraadt@, anton@, bluhm@


# 1.376 31-Dec-2019 mpi

Convert infinite sleeps to {m,t}sleep_nsec(9).

ok kn@


# 1.375 31-Dec-2019 jsg

include sys/sensors.h for sensor use
don't depend on indirect inclusion from acpidev.h


Revision tags: OPENBSD_6_6_BASE
# 1.374 07-Sep-2019 kettenis

Revert acpipci(4) changes. Too many systems are broken. We'll try again
after 6.6 as been released. The acpireg.h change stays behind.


# 1.373 28-Aug-2019 kettenis

Use ACPI information to attach PCI busses like we do on arm64. There are a
few additional quirks though, and attaching the PCI busses is delayed to
replicate the existing code more closely. That may be changed in the
future. Also tweak how we handle MSI support and respect to ACPI flag
that says we shouldn't attempt to use MSIs.

Some fallout is expected.

ok patrick@


# 1.372 27-Aug-2019 deraadt

On amd64 the bootup and resume paths for SP/BP/SP were not performing
msr/pat, clock-startup, firmware upgrade, fpu initialization, and cpu
soft-state initialization in the same order. That kind of chaos is
crazy and fragile. Improve the acpi resume call sequence and unify order
of the operations.


# 1.371 02-Jul-2019 jcs

acpi_set_gpehandler: turn edge arg into flags, add support for
passing GPE_DIRECT which directly executes the supplied callback and
clears a level interrupt rather than going through acpi_taskq which
can be slow when other driver callbacks are in the queue.

This should not be used for GPE handlers that need to do anything
ACPI-related.

ok kettenis


# 1.370 10-Jun-2019 kettenis

Print proper ACPI version number.

ok mlarkin@


# 1.369 08-Jun-2019 kettenis

When establishing the mapping between ACPI device nodes and PCI devices,
skip PCI host bridges and devices that aren't there.

ok patrick@


# 1.368 07-Jun-2019 kettenis

Introduce acpi_getsta() and use it to eliminate some duplicated code.

ok patrick@


# 1.367 12-May-2019 kettenis

Make sure the extended PM register descriptions actually exist before
using them. Fixes machines with ancient ACPI 1.0 (such as recent
hypervisors).

ok deraadt@


# 1.366 12-May-2019 kettenis

Add support for mapping ACPI PM registers using the "extended" gasio
representation from the FADT. Mostly fixes the Lanner NCA-1510.

ok lteo@, mlarkin@, deraadt@


# 1.365 11-May-2019 lteo

Fix incorrect error message.

ok kettenis@


# 1.364 08-May-2019 tedu

free size. the allocation is the header plus the length.


Revision tags: OPENBSD_6_5_BASE
# 1.363 04-Apr-2019 kettenis

Avoid potential null-pointer dereference. Found by Kent R. Spillner.

ok deraadt@, jsg@


# 1.362 20-Jan-2019 jsg

fix SMALL_KERNEL build after changes in rev 1.361
reported by naddy@ ok deraadt@


# 1.361 19-Jan-2019 tedu

Add a pwraction sysctl that controls what the power button does on acpi.
By default, nothing changes -- shutdown is initiated. But allows turning
power button into a sleep button if desired.
(grudging) ok from a few parties


# 1.360 26-Oct-2018 kettenis

Add acpipci(4) on amd64. For now this only calls the PCI-specific _OSC
method to let the ACPI implementation know what features we support.


Revision tags: OPENBSD_6_4_BASE
# 1.359 30-Aug-2018 kettenis

Move softbutton check before the check that skips attaching certain acpi
devices. Fixes regression caused by matching on _CID in addition to
matching on _HID.

ok matthieu@, mlarkin@


# 1.358 25-Aug-2018 kettenis

Match ACPI devices based on _CID if no match for _HID is found.

ok mpi@


# 1.357 19-Aug-2018 kettenis

Add support for multiple PCI segments. Only really implemented for arm64
for now as amd64/i386 firmware still caters for legacy OSes that only
support a single PCI segment.

ok patrick@


# 1.356 03-Aug-2018 kettenis

Let ahci(4) match on _CLS instead of _HID when attaching at acpi(4). Avoids
having to add many more _HID entries to the match table.

ok deraadt@, mlarkin@


# 1.355 10-Jul-2018 kettenis

Make legacy interrupts work in acpipci(4).

ok patrick@


# 1.354 05-Jul-2018 kettenis

Add acpipci(4), a driver that supports generic ECAM-compatible PCI host
bridges based on information provided by ACPI.

ok mlarkin@


# 1.353 04-Jul-2018 kettenis

Properly pass around the PCI "chipset tag" in acpi(4) and refactor
acpimcfg(4) to call an MD initialization functions that sets up a tag for
PCI ECAM.

ok guenther@, mlarkin@, krw@


# 1.352 01-Jul-2018 kettenis

Move acpi(4) attach glue into acpi_machdep.c.

ok guenther@, deraadt@


# 1.351 01-Jul-2018 kettenis

Add support for _DSD Device Properties.

ok mlarkin@


# 1.350 30-Jun-2018 kettenis

Add intr_enable() function, intended for MI use to amd64 and i386 and use
this in the acpi(4) suspend/resume code paths.

ok deraadt@


# 1.349 29-Jun-2018 kettenis

Use <stdint.h> types.

ok mpi@, jung@, krw@, deraadt@


# 1.348 29-Jun-2018 mlarkin

ACPI: Allow (with warning) GPE handler reassignment, instead of returning
a failure code.

ok kettenis


# 1.347 26-Jun-2018 mlarkin

knf


# 1.346 26-Jun-2018 kettenis

Let acpi(4) pass down the bus dma tag.

ok deraadt@


# 1.345 25-Jun-2018 kettenis

Reorganize acpi(4) code a bit in preparation for upcoming arm64 support.

ok deraadt@


# 1.344 20-May-2018 kettenis

Wake up the acpi thread after scheduling a gpio event.

ok mpi@


# 1.343 17-May-2018 kettenis

Sprinkle some #ifndef SMALL_KERNEL such that RAMDISK kernels cuild again.

pointed out by stsp@


# 1.342 17-May-2018 kettenis

Implement GenericSerialBus OpRegion support.

ok mlarkin@


# 1.341 27-Mar-2018 kettenis

Add acpicmos(4), a driver that implements SystemCMOS OperationRegion
access support. This fixes machines where the AML doesn't check whether
support for this OperationRegion type has been registered by the OS.

ok mlarkin@


Revision tags: OPENBSD_6_3_BASE
# 1.340 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.339 10-Feb-2018 deraadt

Syncronize filesystems to disk when suspending. Each mountpoint's vnodes
are pushed to disk. Dangling vnodes (unlinked files still in use) and
vnodes undergoing change by long-running syscalls are identified -- and
such filesystems are marked dirty on-disk while we are suspended (in case
power is lost, a fsck will be required). Filesystems without dangling or
busy vnodes are marked clean, resulting in faster boots following
"battery died" circumstances.
Tested by numerous developers, thanks for the feedback.


# 1.338 08-Feb-2018 deraadt

After flushing disks, try again to release all memory. We don't need to
save dirty memory to the hibernate space.


# 1.337 08-Feb-2018 deraadt

Toss all releaseable memory, because fragmentation can get in the way
of allocating the hibernate playpen.


# 1.336 08-Feb-2018 deraadt

sr_quiesce() is a new approach for ensuring that softraid drains
output to the disks.

This is part of a larger suspend/resume filesystem-safety diff, which
has been worked on for a couple of months already. Tests by job, krw,
beck, benno, and others. Sometimes even by snapshot users...


# 1.335 29-Nov-2017 kettenis

Add support for GPIO-signaled events.

ok mlarkin@


# 1.334 16-Nov-2017 jcs

Add PCI attachment for dwiic(4) needed by Intel 100 Series machines

ok kettenis


# 1.333 14-Nov-2017 kettenis

Add a few more PNP IDs to the skip lists.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.332 17-Aug-2017 stsp

Make dwiic(4) attach its ACPI parent dependencies.
This change makes the Asus E200HA keyboard work.

Original analysis and patch by Cesare Gargano
ok kettenis@


# 1.331 09-Aug-2017 dcoppa

Add "PNP0303" (8042 PS/2 Controller) to acpi_isa_hids[]

ok kettenis@


# 1.330 22-Jul-2017 jcs

acpibat: look for _BIX first before falling back on _BIF

ACPI 4.0 deprecated _BIF for battery status so some newer machines
are exporting _BIX which extends _BIF's package with a few extra
fields. When using _BIX, export the new discharge cycle count as a
sensor.

feedback from kettenis and anton
ok deraadt


# 1.329 20-Jul-2017 kettenis

Start a list of isa(4) device names such that we can avoid printing them
as "not configured".

ok mikeb@, millert@, deraadt@


# 1.328 08-Apr-2017 deraadt

Sensors are run as callbacks inside tasks. During suspend a sensor
could be running inside a driver that will be force-detached, or due
to tsleep end up disrupting the softstate/hardstate contract. At
suspend time, quisce all these callbacks by waiting for completion.
This issue has never been observed for real, but may be implicated in
suspend/resume failures.
ok kettenis guenther mlarkin


# 1.327 08-Apr-2017 deraadt

Two prototypes for acpi_maptable; merge to one place


Revision tags: OPENBSD_6_1_BASE
# 1.326 27-Mar-2017 deraadt

Now that hibernate_alloc() only has clean success/failure, don't
need to call hibernate_free() to clean up a partial mess.
ok mlarkin kettenis


# 1.325 13-Mar-2017 deraadt

allow ramdisks to build; ok jcs


# 1.324 12-Mar-2017 jcs

try to attach acpisbs first and if successful, don't attach acpibat


# 1.323 02-Mar-2017 natano

Add a new sysctl machdep.lidaction. The sysctl works as follows:

machdep.lidaction=0 # do nothing
machdep.lidaction=1 # suspend
machdep.lidaction=2 # hibernate

lidsuspend is just an alias for lidaction, so if you change one, the
other one will have the same value. The plan is to remove
machdep.lidsuspend eventually when people have upgraded their
/ets/sysctl.conf.

discussed with deraadt, who came up with the new MIB name
no objections mlarkin
ok stsp halex jcs


# 1.322 28-Feb-2017 natano

Untangle abstract sleep modes from ACPI sleep states. This paves the way
for further work in the area.

idea and ok deraadt
ok mlarkin


# 1.321 22-Feb-2017 jcs

add acpisbs, an acpi smart battery subsystem driver reading data
over smbus

currently disabled because it conflicts with acpibat

ok deraadt, kettenis


# 1.320 14-Jan-2017 kettenis

Revert aml_rdpciaddr changes; breaks several machines that were working
before.


# 1.319 11-Jan-2017 jsg

Increase the size of the buffers used for _HID and _CID strings to be
able to fit "Hyper_V_Gen_Counter_V1".

ok mlarkin@ deraadt@


# 1.318 08-Jan-2017 kettenis

Make aml_rdpciaddr() work for for devices behind bridges. The code now
uses the mapping between AML nodes and PCI devices that we establish.
Because _INIT methods may end up calling aml_rdpciaddr(), make sure we
create that mapping early. Also handle devices that aren't actually present.
These devices are now included in the mapping and reads will return an
all-ones patterm whereas writes are a no-op..

ok mlarkin@


# 1.317 25-Oct-2016 pirofti

Add the resource index to aml_parse_resource() callback function.

This is needed by an upcoming acpiec commit that handles machines breaking
the current ACPI specifications.

Change suggested by and ok kettenis@, guenther@


# 1.316 18-Sep-2016 guenther

If the APIC table (MADT) isn't present then ci_acpi_proc_id won't be set
when acpicpu tries to attach, leading to a NULL dereference. The ACPI
processor object on the Dells where this was reported is bogus and useless
anyway so apply the same test when deciding whether to attach to processor
objects as acpicpu would when matching them to cpus.

Problem reported by Eivind Eide (xenofil (at) gmail.com) and
Johan Huldtgren (johan+openbsd-bugs (at) huldtgren.com).
Testing by Eivind Eide.
ok kettenis@ mlarkin@


# 1.315 03-Sep-2016 naddy

Write the system time back to the RTC every 30 minutes.
This fixes the problem that long-running machines which were not
shut down properly would reboot with a badly offset system time.

hints and ok kettenis@


# 1.314 31-Aug-2016 mlarkin

Don't put a device in the wake device list if _STA says it's not there.

Fixes at least one machine that woke up from sleep / poweroff immediately.

ok kettenis@, tested by kettenis and martijn@


# 1.313 28-Jul-2016 kettenis

Store the acpi processor ID/UID in struct cpu_info, and use it to attach
acpicpu(4) drivers to the right cpu(4).

ok mlarkin@, guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.312 10-Jun-2016 kettenis

The power management timer has been made optional in ACPI 5.0A, so don't
attempt to attach acpitimer(4) if the timer isn't present.

Pointed out by jcs@

ok jcs@


# 1.311 08-May-2016 kettenis

Make sure devices listed by a device's _DEP method are attached before
we attach the device itself. The _DEP methode, introduced in ACPI 5.0,
is a hint that the device in question depends on OpRegion support from
the devices listed by _DEP, so we have to order them the proper way.
To prevent us from attaching those devices again when we encounter them
later walking down the device tree, keep track of our attempts to
attach a device driver to them using aflag in the aml_node structure.

ok guenther@


# 1.310 25-Apr-2016 pirofti

We attach acpihpet(4) and acpiec(4) without using the HID, so do not
report it in the dmesg.

OK deraadt@, mlarkin@.


# 1.309 02-Apr-2016 kettenis

Reduce dmesg spam by knocking out some acpi hardware IDs that don't need a
driver.

ok mpi@


# 1.308 29-Mar-2016 kettenis

Check if a device is present (using _STA) in acpi_foundhid. This prevents
us from reporting devices as "not configured" that aren't actually present,
and allows us to remove duplicated code from several drivers.

ok deraadt@, ok jsg@


# 1.307 29-Mar-2016 kettenis

Implement a cleaner way to prevent acpivout(4) from attaching if we have
a platform-specific method of controlling the backlight.

ok millert@, mlarkin@, jung@


# 1.306 28-Mar-2016 kettenis

Adding each and every new acpi device driver to acpi_foundhid() is
gettinga bit of the burden. And it really isn't how our autoconf
framework is supposed to work for busses that can be enumerated. All
the drivers already check for a matching _HID in their attach
function. So we can just drop the checks in acpi_foundhid() and let
the drivers sort themselves out.

Print a "not configured" line for devices for which we don't attach adriver.
This may be a bit much as there are devices for which we will not have a
driver. This will be removed if it causes too much dmesg spam.

ok deraadt@, mlarkin@


Revision tags: OPENBSD_5_9_BASE
# 1.305 17-Jan-2016 jsg

Take acpi_foundhid() outside of #ifndef SMALL_KERNEL so the ramdisk
kernel can find and attach dwiic(4) and sdhc(4).


# 1.304 17-Jan-2016 jsg

remove an unused global variable


# 1.303 14-Jan-2016 kettenis

Attach dwiic(4) on the Intel Bay Trail i2c controllers as well.


# 1.302 12-Jan-2016 kettenis

Improve emulation of PM registers on Hardware-redcued ACPI by emulating
SPL_TYPx, SLP_EN and WAK_STS using the new SLEEP_STATUS_REG and
SLEEP_CONTROL_REG registers. Makes the transition into S5 (powerdown) work
on the ASUS X205TA.

ok deraadt@, mlarkin@


# 1.301 12-Jan-2016 jcs

Add dwiic, a driver for the Synopsys DesignWare i2c controller found
on the Samsung ATIV Book 9 laptop. This initial version only
supports ACPI config/attachment.

Add ihidev, a HID-over-i2c driver largely based on uhidev. dwiic
handles attaching ihidev devices found in ACPI.

Add ims, a HID-over-i2c mouse/trackpad driver to get basic cursor
and button functionality from HID-compliant i2c trackpads.

ok kettenis deraadt


# 1.300 11-Jan-2016 kettenis

Make sdhc(4) attach to hardware IDs "80860F14" and "PNP0FFF".


# 1.299 09-Jan-2016 kettenis

Initial support for Hardware-Reduced ACPI. This skips the legacy mode checks
by emulating the PM_CNT register such that SCI_EN is always enabled. It also
skip attaching acpitimer(4) since the relevant registers are no longer there.

ok deraadt@, mlarkin@


# 1.298 06-Jan-2016 kettenis

Follow the spec and check the SCI_EN bit to see if ACPI has already
been enabled and don't bother enabling it again if it is. This should fix
systems that don't implement legacy mode and therefore set ACPI_ENABLE and
ACPI_DISABLE to zero.

ok mlarkin@, yasuoka@


# 1.297 23-Nov-2015 reyk

Back out the CRITICAL change, turn it back from 10% to 25%. Some
people with old/broken batteries didn't like this change, and the
underlying heuristics are wrong.


# 1.296 22-Nov-2015 reyk

The "CRITICAL" APM battery level, that is exported/faked by the ACPI
stack, is currently set to 25%. With 25% battery left, a typical
modern laptop will have at least one hour runtime left. It is an
arbitrary value, and other APM power layers in the kernel set CRITICAL
to something like 5% or 10%, so set it to 10% for now.

OK mlarkin@


# 1.295 28-Sep-2015 deraadt

In low-level suspend routines, set cold=2. In tsleep(), use this to
spit out a ddb trace to console. This should allow us to find suspend
or resume routines which break the rules. It depends on the console
output function being non-sleeping.... but that's another codepath which
should try to be safe when cold is set.
ok kettenis


# 1.294 06-Sep-2015 deraadt

clarify malloc arguments (base + length...)


# 1.293 28-Aug-2015 deraadt

fairly simple sizes for free(); ok tedu


# 1.292 20-Aug-2015 kettenis

Respect the access size when reading or writing to pci config space.
This version also makes sure writes are properly aligned, preventing
the panics seen with the previous incarnation of this change.

Fixes battery status passthrough in vmware, brightness keys on my x220
and who knows what else.

ok mlarkin@, deraadt@


Revision tags: OPENBSD_5_8_BASE
# 1.291 04-Aug-2015 deraadt

If we are setting ACPI_PM1_BM_RLD do it only once, and remember to
repeat this action at resume time.
ok mlarkin guenther


# 1.290 29-Jul-2015 deraadt

Something subtle in previous revision is locking up suspend or booting
on some laptops. Mailing lists threads mention x220 and Asus machines.
Reverting for now.


# 1.289 21-Jul-2015 kettenis

iRespect the access size when reading or writing to pci config space.
Fixes battery status passthrough in vmware and who knows what else.

Tested by many.

ok mlarkin@, deraadt@


# 1.288 17-Jul-2015 mlarkin

fix comments


# 1.287 26-Mar-2015 jsg

Use the 0x10 extended capability to determine if the trackstick
buttons are wired to the trackpad and need to be re-routed to the
trackstick. Without this change the buttons on 2015 thinkpads
get picked up as extended buttons that show up as scroll up/down.

Remove the X1 Carbon 2015 (LEN0048) and X250 (LEN0046) from the
top button area/soft buttons quirks list. Also avoid using the
quirk list entirely if the capability bit is set.

Based on work from jcs@ with feedback from mpi@
ok mpi@ jcs@


# 1.286 20-Mar-2015 kettenis

The ACPI standard says that a PCI function number of FFFF can be used to refer
to all functions on a device. This makes no sense in the context of accessing
PCI config space. Nevertheless stsp@ has a machine with AML that tries to
do config space reads from a device where _ADR encodes function number FFFF.
This is almost certainly buggy AML, but since Linux (and presumably Windows)
boot just fine on this machine we need to work around this. Linux effectively
treats function FFFF as function 7, which isn't present on this machine.
Instead we simulate a read from a non-existent device by returning a bit
pattern of all ones. Writes to function FFFF will still panic.

ok mlarkin@


# 1.285 16-Mar-2015 deraadt

Reduce use of SMALL_KERNEL in acpi, bringing acpiec driver and more gpe
handling into RAMDISK. This is now possible because the install media
has ample room. The goal is to reduce special cases where we may be
skipping (unknown) important operations...
ok mlarkin kettenis


# 1.284 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.283 07-Feb-2015 deraadt

New framework that allows hibernate to pass in entropy from it's fresh
boot.
ok mlarkin


# 1.282 06-Feb-2015 mlarkin

Fix a hibernate crash on some machines due to unmapping a page that
may not have been mapped previously (in the failure to hibernate case).

Also ensure that the lowmem ptp is mapped in all cases (not just MP).

ok kettenis


# 1.281 17-Jan-2015 deraadt

resume_randomness() before spinning up other cpus...


# 1.280 15-Jan-2015 jsg

Define a new wscons mouse type for Synaptics clickpad devices that lack
physical buttons. This will be used if the acpi pnpid for the mouse
matches a list provided by Synaptics found in Linux.

Combined with the xenocara changes this will remove the need for an xorg
config file for the x240/t440/t540 generation of thinkpads.

ok matthieu@ shadchin@ kettenis@


# 1.279 11-Jan-2015 kettenis

Tolerate checksum errors in ACPI tables (but complain about them). Apparently
there are machines out there with broken BIOSen with such checksum errors in
thier RSDT tables. Windows runs fine on these machines, so it probably does
not check. Linux ignores checksum errors as well (and prints a warning).

ok deraadt@, krw@


# 1.278 18-Dec-2014 deraadt

suspend and resume the RNG (at what looks like the right spots)
ok reyk djm


# 1.277 09-Dec-2014 doug

Replace some malloc(n*size,...) calls with mallocarray().

ok tedu@ deraadt@


# 1.276 06-Dec-2014 mpi

If "machdep.lidsuspend" is not set, do not go back to sleep if the lid
is still closed when resuming. This allows docked laptop to be resumed
without opening them.

ok mlarkin@


# 1.275 23-Nov-2014 mlarkin

If a machine has no _LIDs, then none can be open. This diff fixes a problem
where machines without any _LIDs were going back to sleep after hibernate
due to the recent suspend-if-lid-closed-on-resume diff. Noticed on qemu,
which doesn't have any _LIDs.

discussed with deraadt


# 1.274 18-Nov-2014 krw

Nuke yet more obvious #include duplications.

ok miod@


# 1.273 08-Nov-2014 mlarkin

If resuming from sleep (zzz/ZZZ) and the lid is still closed, go back to
sleep. This prevents accidental lid flex or slight opening in a backpack
from waking the machine up and leaving it resumed (powered on).

ok deraadt@


# 1.272 26-Sep-2014 kettenis

Rework piglet and pig allocation. Currently the piglet gets allocated
deep down in the suspend path, where it is really hard to recover from
allocation failure. So allocate the piglet early on in the suspend path.
Also change the piglet and piglet allocation functions to use km_alloc(9)
instead of doing pmemrange magic. This removes a bunch of code which, in the
case of the piglet allocation, is broken since it results in a NULL pointer
dereference. Also switch the piglet allocation to not wait. If we can't
allocate 16MB of phys contig memory on a halfway modern machine we're almost
certainly under a lot of memory pressure and we're better off not trying to
hibernate anyway.

ok mlarkin@


# 1.271 25-Sep-2014 kettenis

Bring back rev 1.249. Now that mp_setperf() has been fixed, it should be
safe to restore perflevel when resuming. Should fix the issues people
see with the processor running at full speed heating up the machine while
the apm command reports that the CPUs are running at a low speed.

ok dcoppa@, deraadt@


# 1.270 23-Sep-2014 kettenis

Release the acpi lock around calls to wsdisplay_suspend() and
wsdisplay_resume(). The X server may adjust the display brightness just
before/after the VT switch when suspending/resuming. In fact, it may
already be blocked adjusting the display brightness for some reason
unrelated to the suspend request.

The VT switch request will eventually time out, but the delay is annoying
and the failed VT switch will lead to problems after resume.

Needed for a future update of the xf86-video-intel driver.

ok mlarkin@


# 1.269 19-Sep-2014 kettenis

Use config_suspend_all(9).


# 1.268 08-Sep-2014 kettenis

Clear the PME Status bit on PCI devices in response to Device Wake events.
Fixes the interrupt storm on the 2009 Mac mini with WOL enabled on its
nfe(4) interface.

ok mlarkin@


Revision tags: OPENBSD_5_6_BASE
# 1.267 20-Jul-2014 mlarkin

Support hibernating to softraid crypto volumes.

much help and ok from deraadt@


# 1.266 16-Jul-2014 mlarkin

Reenable hibernate RLE support and flush+zero all memory after unpack.


# 1.265 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.264 12-Jul-2014 kettenis

Add a function to drop all clean pages on the page daemon queues and call
it when we hibernate.

ok mlarkin@, miod@, deraadt@


# 1.263 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.262 11-Jul-2014 mlarkin

Flush the buffercache to 16MB on hibernate and restore its previous max
size (kern.bufcachepercent) on resume, for better hibernate performance.

ok beck@


# 1.261 10-Jul-2014 blambert

remove unneeded header

ok mlarkin@


# 1.260 01-Jul-2014 kettenis

Adding an additonal delay after each access to SystemMemory or SystemIO
during autoconf makes no sense.

ok deraadt@


# 1.259 23-Jun-2014 kettenis

SystemMemory is used to access memory mapped registers on some machines, so we
must use the bus_space(9) API to access it instead of memcpy(9). Also make
sure we properly align access to fields that need more than byte alignment.

ok guenther@


# 1.258 21-May-2014 mlarkin

Fix invalid / missing format specifiers in several dnprintf statements
(used only when ACPI_DEBUG is enabled)

From a diff on tech@ by Fabian Raetz, with a minor change by me, tested
on i386 and amd64 with ACPI_DEBUG enabled and disabled.


# 1.257 25-Apr-2014 mlarkin

Defer the free of the hibernate structures until later, when it is safe to
do so.

Freeing the hibernate structures too early leads to inadvertent calls into
splx when we aren't ready for them yet. Found when trapping splx calls in
the resume path.

discussed with deraadt@ at m2k14


# 1.256 13-Apr-2014 mlarkin

Fix a badly worded debug message that implied the entire machine was going
to sleep, instead of just the acpi thread going to sleep.


# 1.255 31-Mar-2014 mpi

Change the order of operations during a suspend/resume cycle and
call bufq_quiesce() after executing the DVACT_QUIESCE handlers.

This should be safe since no disk nor controller drivers have such
handler but it will allow us to detach sd(4) devices attached to a
USB bus.

Another benefit pointed out by kettenis@ is that drivers that need
to read a firmware from the disk should be able to do it at resume
time in a DVACT_WAKEUP handler.

ok kettenis@, deraadt@


# 1.254 13-Mar-2014 dlg

get rid of the assumption that the head of the alldevs list is the
"mainbus" device. this breaks when mpath is enabled because it
attaches before mainbus and therefore takes the head position.

have autoconf provide device_mainbus() which looks up mainbus_cd,
and use that instead.

discussed with deraadt who just wants mpath stuff to move forward
despite there being many ways to shine this particular turd.


Revision tags: OPENBSD_5_5_BASE
# 1.253 21-Feb-2014 deraadt

Do the lid control further on in the resume sequence, since this is
AML, and will do WAITOK operations. While cold, that is a bad idea.
We seem to be safe with respect to the spec, I do not see any event
debounce issues here.
ok kettenis, tested by many


# 1.252 20-Jan-2014 claudio

Instead of checking sc->sc_revision <= 1 check that the
fadt->hdr_revision <= 1. This is a better check since it checks
the version of the proper descriptor. This allows the alix APU
to reboot since those boards have currently a wrong sc->sc_revision.
Committing this now so that it can be tested easier.
With and OK jsg@ also fine by derradt@


# 1.251 24-Dec-2013 kettenis

If the FADT has its SMI_CMD set to zero, assume we're only ACPI-only hardware
and don't need to disable SMI ownership of the ACPI hardware registers.

ok mlarkin@


# 1.250 23-Dec-2013 kettenis

Revert previous commit. It makes resume hang on some MP systems running
GENERIC.MP.


# 1.249 16-Dec-2013 kettenis

Restore hw.setperf upon resume like we do for apm(4) on i386. Seems we need
to do this fairly late in the resume to avoid hangs.

ok deraadt@, mlarkin@


# 1.248 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


# 1.247 06-Nov-2013 mpi

Add support for Power Resources for Dx states and the necessary hook
for PCI devices. This hook should be called twice, before and after
changing the power state of a PCI device.

Before setting the device to the new state, the ACPI layer will notify
every power resources linked to the device for that state and make sure
they are turned "_ON". After changing the state of the device, it will
decrement the reference of every power resources linked to that device
for the old state and turn them "_OFF" if they are no longer referenced.

This fixes the no-USB after resume problem seen on various ThinkPad,
problem initialy diagnosed with Alexander Polakov.

ok kettenis@, deraadt@


Revision tags: OPENBSD_5_4_BASE
# 1.246 01-Jun-2013 mlarkin

Stop printing ACPI wakeup devices in dmesg after the 16th wakeup device.

Helps with VMware VMs that report hundreds of wakeup devices.

ok reyk@, deraadt@


# 1.245 31-May-2013 bluhm

Fix the build for a kernel without wd and pciide in its config.
OK deraadt@ tedu@


# 1.244 30-May-2013 deraadt

Enforce ca_activate tree-walks over the entire heirarchy for all events,
cleaning up some shutdown-hook related code on the way.
(A few drivers related to sparc are still skipped at kettenis' request)
ok kettenis mlarkin, tested by many others too


# 1.243 18-Apr-2013 deraadt

pin the acpi thread to the BSP, because AML and SMI cannot be trusted. I
was certain we were doing this already, but it turns out no...


Revision tags: OPENBSD_5_3_BASE
# 1.242 24-Dec-2012 guenther

In acpiioctl(), Return though the splx() path if suser() returns an error

ok deraadt@


# 1.241 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


# 1.240 04-Oct-2012 ehrhardt

Use information provided by ACPI to attach secondary PCI host bridges.

ok kettenis@


# 1.239 07-Sep-2012 kettenis

Add a new function called acpi_pci_min_powerstate() that returns the lowest
power state for a PCI device for the power state (currently S3 or S4) we're
transitioning into. This function is probably too simplistic; there is a lot
of (silly) complexity in the ACPI spec.

ok deraadt@


Revision tags: OPENBSD_5_2_BASE
# 1.238 13-Jul-2012 pirofti

Disable acpivideo(4) on ASUS laptops.

There are some models that panic whenever an event is received.
Some cope, some don't. In order to get this right acpiec(4) requires
major rework. Until then let machines boot.

Discussed with deraadt@.


# 1.237 11-Jul-2012 mlarkin

for now, restrict hibernate to root in the acpi ioctl kernel interface.
normal rules for controlling this interface through apmd still apply.

ok deraadt@


# 1.236 09-Jul-2012 deraadt

tiny little oops in untested hibernate block


# 1.235 09-Jul-2012 deraadt

clean up ioctl() case statements


# 1.234 09-Jul-2012 deraadt

acpi_disable_onegpe() is not used; ok pirofti


# 1.233 24-May-2012 kettenis

If _MIN > _MAX consider a WordBus resource to be invalid. Fixes a panic on
the IBM x3550-M4.

ok mikeb@


# 1.232 29-Mar-2012 mlarkin

Missing #ifdef in previous diff


# 1.231 29-Mar-2012 mlarkin

Prevent hibernation attempts on machines with unsupported disk controllers.
The same check is also performed later in the hibernate sequence, but by
then it's too late to cleanly unwind (presently).

ok deraadt


# 1.230 26-Mar-2012 deraadt

simplify conditionals in acpi_sleep_state()
ok mlarkin


# 1.229 26-Mar-2012 deraadt

hook in the hibernate request code; half of this diff is from mlarkin
ok mlarkin


Revision tags: OPENBSD_5_1_BASE
# 1.228 20-Sep-2011 deraadt

Very early in suspend while the clock is still ticking realtime,
save the clock back to the rtc. The zaurus already did this.
ok phessler miod kettenis


Revision tags: OPENBSD_5_0_BASE
# 1.227 02-Jul-2011 nicm

kqueue attach functions should return an errno or 0, not a plain 1. Fix
the obvious cases to return EINVAL and ENXIO.

ok tedu deraadt


# 1.226 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.225 16-Jun-2011 pirofti

Import acpitoshiba driver.

This is originally written by Hiroyuki Aizu, ported and integrated
in our tree by Javier Vazquez <javama4 AT gmail> with tweaks and
suggestions by me.

Import prodded by deraadt@ and the lack of testers on tech@.


# 1.224 27-Apr-2011 jcs

attach acpithinkpad to newer lenovo models like the x120e

ok deraadt@


# 1.223 22-Apr-2011 jordan

Fix uninitialzied variables and formatting strings (-Wxxx errors)


Revision tags: OPENBSD_4_9_BASE
# 1.222 02-Jan-2011 jordan

Re-add code for ACPI taskqueue, eliminates need for state vars in acpi softc
Serialize processing ACPI tasks for sleep/powerdown/GPE event
ok deraadt@


# 1.221 31-Oct-2010 guenther

Revert last commit: it breaks resume on Thinkpads
"then please back it out" deraadt@


# 1.220 26-Oct-2010 jordan

Add task queue for ACPI gpe and notify handlers
This fixes eject on Dell Latitude dock and an issue on the Dell Mini
battery update.
ok deraadt


# 1.219 07-Oct-2010 jordan

Create separate functions for enabling wake and runtime gpes.
Remove old suspend/resume gpewalk function
ok deraadt, mlarkin


# 1.218 05-Oct-2010 jordan

split acpi_enable_onegpe to separate enable/disable functions
ok deraadt


# 1.217 05-Oct-2010 jordan

Added acpi_disable_allgpes function to clear GPE status
ok deraadt


# 1.216 05-Oct-2010 deraadt

clean up acpi_softc use more


# 1.215 05-Oct-2010 deraadt

delete a bit of #if 0 code


# 1.214 05-Oct-2010 deraadt

hide use of our global acpi_softc pointer a bit better


# 1.213 29-Sep-2010 deraadt

grab the acpi thread lock very early in acpi thread startup; not that it
is likely that anyone will race us... but why be careless.
ok kettenis


# 1.212 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.211 29-Aug-2010 jasper

- add a check for FWRITE here too.

ok miod@


# 1.210 27-Aug-2010 kettenis

Make PCI Power Management optional, and only enable it when acpi(4) attaches.

ok deraadt@


Revision tags: OPENBSD_4_8_BASE
# 1.209 08-Aug-2010 kettenis

Close race between the acpi thread and wscons ioctls running in process
context. We might rip this out post-release and replace it with a solution
that serializes things in a more obvious way.

ok deraadt@, miod@ and "those crazy texans who ok anything"


# 1.208 08-Aug-2010 deraadt

Report the acpi sleep states when on a RAMDISK; ok krw


# 1.207 07-Aug-2010 kettenis

Enforce acpi register access rescrictions.

ok mlarkin@, deraadt@, tested by marco@


# 1.206 07-Aug-2010 deraadt

upon resume, notify all the acpiac and acpibat drivers to update
their status. some laptops do not do this automatically.
ok kettenis canacar


# 1.205 06-Aug-2010 marco

Always call _PSW on _LID devices that support it to enable lid open to
resume the machine. Conversely disable it when the machine wakes up.

Tested by several ok deraadt


# 1.204 06-Aug-2010 deraadt

As part of a revamp of the PM1/GPE code, I write this diff to look for
unmanaged EN & STS bits in the PM1 register at interrupt time and report
them. As a side effect this splits the STS acknowledgement into two writes
(for power, and sleep) instead of one. The printf that is added (to spot
unmanaged STS bits) has not yet been triggered as far as we know.

Before the "write to PM1 registers at the right offset" diff went in, this
was not neccessary. But newer thinkpads do not have a working soft power
button without this diff.

We have no idea why.
ok mlarkin kettenis


# 1.203 05-Aug-2010 deraadt

PM1 power button events were generating 'fake events' to acpibtn PWRB,
but if there is no such device these were being tossed. Instead, arrange
for all power button handlers to flag a softc variable which is processed
by the thread; so that one place will know to request a powerdown (psignal
to init)
ok marco mlarkin


# 1.202 05-Aug-2010 deraadt

rename a variable which is annoyingly poorly named


# 1.201 05-Aug-2010 mlarkin

Write to the correct offset for various ACPI pmreg registers. Writing to
the correct register does properly clear the wake status bits and fixed
event bits, which allows some machines to have more than 1 suspend cycle.

bug spotted by deraadt@

tested by deraadt@ and myself on a half-dozen machines and others on
hackers@

ok deraadt@, marco@


# 1.200 05-Aug-2010 deraadt

Fix the power button. The pm1 register is 16 bits in size.
My mistake, a while back.
tested by krw


# 1.199 04-Aug-2010 kettenis

Print supported sleep states.

ok marco@, phessler@, deraadt@


# 1.198 03-Aug-2010 mlarkin

Use the proper flag for re-enabling certain hardware events (power btn,
sleep btn). The (incorrect) flag prevoiously used coincidentally had the
same value, so this shouldn't cause any different behavior than before.

tested on a variety of machines (i386, amd64, sp, mp) - no behavioral
change seen

ok deraadt@


# 1.197 28-Jul-2010 marco

Make all gasio failures look and behave the same.


# 1.196 28-Jul-2010 mlarkin

Delay a little longer on suspend before giving up. Fixes machines that
sometimes don't suspend because the sleep transition takes too long
(symptoms like failing to sleep but the backlight stays on).

ok deraadt@


# 1.195 27-Jul-2010 deraadt

remove XXX; EC can now be attached early when needed; ok jordan


# 1.194 27-Jul-2010 deraadt

more brackets feels good


# 1.193 27-Jul-2010 deraadt

XXX annotate a block in acpi_enter_sleep_state() that is pretty worrying


# 1.192 27-Jul-2010 deraadt

horrific indentation that nearly made me puke


# 1.191 27-Jul-2010 mlarkin

Set the sleeping indicator light on machines that support it, in case SMI
doesn't do it for us. Shows the 'moon' LED on older Thinkpads when
sleeping.

Tested on T43p, W500, T510 by me and on X61s by deraadt@.

ok deraadt@


# 1.190 27-Jul-2010 jordan

Early initialization of acpiec if ECDT table exists
Fixes hang when booting thinkpads while docked
ok deraadt


# 1.189 26-Jul-2010 deraadt

remove merge error which was commited


# 1.188 26-Jul-2010 pirofti

Add support for Sony ACPI hotkeys via a new driver: acpisony(4).

Currently it only works for the suspend button and tries to do right for
the brightness events, but I haven't found a Sony laptop that like to do
right so far.

In the future I want to make the brightness keys work on all Sony's and
also add support for the zoom hotkeys and whatever other funky keys I can
find on those things.

Okay deraadt@.


# 1.187 22-Jul-2010 deraadt

pretty large cleanup of SMALL_KERNEL


# 1.186 21-Jul-2010 deraadt

in the acpi GPE handler, late ack level interrupts not edge interrupts.
Duh.


# 1.185 21-Jul-2010 deraadt

Merge the edge and level gpe handlers into one (now possible since the
gpe structure tells us whether the gpe is edge or level)


# 1.184 21-Jul-2010 deraadt

delete #if 0 code we are not going to use; ok marco


# 1.183 20-Jul-2010 deraadt

make the mask code for acpi_enable_onegpe() match other similar code


# 1.182 20-Jul-2010 deraadt

Handle the acpi interrupt controller with proper edge/level handling,
wrapped in spltty to avoid racing against the interrupt controller.
Repair the gpe bit masking code while there, and do operations in the
right order
ok kettenis mlarkin, help from jordan at figuring out the order of
operations.


# 1.181 20-Jul-2010 matthew

When we're preparing to go to sleep, only call bufq_restart() if other
preparations failed.

ok kettenis@, thib@, deraadt@


# 1.180 19-Jul-2010 deraadt

permit compilation with ACPI_DEBUG


# 1.179 19-Jul-2010 deraadt

there is no need for a seperate dsdt_softc pointer; dsdt does not have a
softc but is just a part of the single acpi driver.
ok jordan


# 1.178 19-Jul-2010 deraadt

acpi_delay() is no longer needed; ok jordan


# 1.177 18-Jul-2010 mlarkin

Clear event status on resume, to avoid going back to sleep/reboot/shutdown
immediately after resume. Tested by a bunch of people on hackers@ on
various hardware. Fixes at least Toshiba NB200 and Gateway LT3103u.

ok marco


# 1.176 13-Jul-2010 deraadt

rename some thread related interfaces so they are logical


# 1.175 10-Jul-2010 jordan

Verify _ADR bdf is correct (some systems return 0xffff)
Works on Dell x9xx and Studio 1555


# 1.174 09-Jul-2010 jordan

Clear the noise


# 1.173 08-Jul-2010 jordan

Add mapping for ACPI device to PCI bdf (match autoconf tree)
Simplify resource parsing function to use buffer argument
Convert namespace linked lists to use queue macros
ok marco@, deraadt@


# 1.172 06-Jul-2010 deraadt

Use spltty() to lock downcalls from apm against the information being
modified by the acpi thread.
ok kettenis


# 1.171 06-Jul-2010 deraadt

Don't use a workq to do the suspend, because it races aginst the acpi
thread. Instead, just tell the acpi thread to do the suspend for us.
This makes apmd & zzz work correctly. While here, have acpithinkpad
attempt to post the event to apm before prompting the suspend itself.
ok kettenis marco mlarkin


# 1.170 05-Jul-2010 mlarkin

Reset register SCI_EN on resume. The ACPI spec clearly states we don't have
to do this, but many machines don't, or forget to, which leads to all sorts
of problems on resume (broken battery stats, overheating, failure to resume
more than once, etc).

tested by many, ok marco@, deraadt@


# 1.169 01-Jul-2010 thib

do bufq quiesce again, since vnd/the bufq_done call in
biodone has been fixed.


# 1.168 01-Jul-2010 jordan

Backout recent AML changes, breaks on R210 and others


# 1.167 01-Jul-2010 jordan

Simplified aml_parse_resource call, cleanup code
ok marco


# 1.166 01-Jul-2010 jordan

Fix subordinate bus # for multi-root PCI buses
ok marco, kettenis


# 1.165 30-Jun-2010 thib

Disable/partially backout the bufq quiesce changes as this
is causing havoc with vnds and release must be buildable.


# 1.164 29-Jun-2010 deraadt

Fix ramdisks (new code was placed inside a SMALL_KERNEL chunk)


# 1.163 29-Jun-2010 jordan

Add support for mapping ACPI to PCI devices
ok kettenis, deraadt


# 1.162 29-Jun-2010 kettenis

Call bufq_quiesce() on suspend to stop all I/O and bufq_restart on resume.
Makes suspend/resume work much better while doing disk I/O.

ok mlarkin@


# 1.161 29-Jun-2010 tedu

60 is a more reasonable guess for number of minutes in an hour than 100.
spotted by TeXitoi. ok deraadt


# 1.160 27-Jun-2010 jordan

Add _acpi_matchhids routine, precursor to pci device match


# 1.159 27-Jun-2010 mlarkin

Clear ACPI fixed event status on resume (power buttons/etc) so that some
machines don't immediately resume after suspending to S3.

Tested by kettenis@ and I on 5 or 6 machines, deraadt@ "yeah, looks good"


# 1.158 27-Jun-2010 jordan

Cleaned up acpi_maptable, adds to table list directly


# 1.157 07-Apr-2010 deraadt

If we have done a wsdisplay_suspend(), and ACPI decides it does not want to
suspend, we must roll back by calling wsdisplay_resume() or the virtual
console switching will be locked forever
ok oga


# 1.156 07-Apr-2010 kettenis

Make suspend/resume work on MP machines (running an MP kernel). Joint work
from mlarkin@ and me, with some amd64 fixes thrown in by deraadt@

ok marco@, deraadt@, pirofti@, mlarkin@


# 1.155 31-Mar-2010 kettenis

Bring /dev/apm support in line with apm(4). Make the suspend button send
a suspend request event to apmd(8) instead of suspending immediately. Also
keep track of whether /dev/apm and /dev/apmctl are currently open, such that
we can still suspend immediately if apmd(8) isn't running.

ok deraadt@, marco@, pirofti@, jsing@, oga@


# 1.154 30-Mar-2010 oga

Prevent the apmd/x races for good.

When we hit suspend time, go through all wsdisplays on the system. if
they are in mode MAPPED, but not MODE_DUMBFB then if possible do a full
vt switch to a !mapped vt, and prevent switching back until resume time.
This has to be called from MD code because this involves userland
running so that X can run the vt switch signal handler. This way, any
case where we are using the "poke registers from userland" model, we
will not be on the hardware when we go down, so the kernel can actually
handle thing properly.

Tested on several acpi laptops (by kettenis@ and ian@), x40 (me and
beck@ at LEAST) and zaurus (me). Maybe others, but if so I forgot who at
this time..

Idea from deraadt somewhere over the Faroe Islands (I thought of a
similar thing myself a while ago). Much prompting from him. Ok and
comments miod@


# 1.153 25-Mar-2010 oga

Fix & vs. && in the kqfilter code.

Based on a comparison with the apm code.

ok deraadt@, kettenis@


Revision tags: OPENBSD_4_7_BASE
# 1.152 26-Nov-2009 mlarkin

Visual (and on some machines, audio) feedback while the machine is
resuming via calls to aml _SST method. This also makes the "moon" LED on
thinkpads to blink during resume and cease when resume is completed.

ok pirofti@


# 1.151 26-Nov-2009 deraadt

Toggle into "cold" when we we disable interrupts, because tsleeping
in the AML interpreter and context switching into userland processes
tends to sometimes pop the ACPI trampoline page out of our address
space. Makes lots more machines work.
ok kettenis mlarkin


# 1.150 24-Nov-2009 jsg

in the resume path evaluate _WAK before dealing with device drivers
like the spec says, not after.

ok mlarkin@


# 1.149 24-Nov-2009 deraadt

abort suspends on SMP systems until that gets written; ok mlarkin


# 1.148 23-Nov-2009 mlarkin

pci should return the result of it's children's suspend/resume calls back
to its parent.

handle suspend failure case and unwind if devices have an activate function
that returns failure.

ok deraadt@, kettenis@


# 1.147 23-Nov-2009 mlarkin

acpi* devices need to call their children on suspend/resume.
ok deraadt@


# 1.146 23-Nov-2009 pirofti

Remove ACPI_SLEEP_ENABLED checks.

This enables by default the suspend/resume paths in the kernel.

Okay deraadt@.


# 1.145 23-Nov-2009 deraadt

knf and spacing that are super annoying


# 1.144 23-Nov-2009 mlarkin

Hook up Theo's new children suspend/resume function, as well as more ACPI
suspend/resume glue.

ok deraadt@


# 1.143 26-Oct-2009 deraadt

Do not do strncmp comparisons on dv_xname because longer device names which
look similar could arrive in the future. Instead, compare directly against
dv_cfdata->cf_driver->cd_name
Issue originally spotted by miod


# 1.142 26-Oct-2009 jordan

Verify checksum+address when loading ACPI tables.
Some systems had invalid entries in RSDT/XSDT.
ok marco@


# 1.141 23-Jul-2009 cnst

New aibs(4) driver for ASUSTeK AI Booster (ACPI ATK0110) hardware monitoring,
with sensor state support through limits provided by the ACPI.

Tested on several ASUS motherboards kindly networked by Sam Fourman Jr.

ok deraadt marco jordan


Revision tags: OPENBSD_4_6_BASE
# 1.140 03-Jun-2009 pirofti

Import acpi power resource driver. This is needed for suspend/resume logic.

Okay jordan@ and marco@.


# 1.139 03-Jun-2009 pirofti

Add acpivideo support. This adds brightness support for all laptops
except thinkpads, they will use the acpithinkpad driver. The driver is
also hooked into wsconsole. So brightness can be adjusted via:

$ wsconsctl display.brightness=<percentage>

This is very helpfull on some laptops that have a nasty bios and get two
steps instead of one when pressing the brightness button.

Tested on various dell, fujitsu, acer, samsung and other laptops.

Okay marco@, miod@. Suggestions from kettenis@.
Lots of reviews and help from miod@, thanks!


# 1.138 03-Jun-2009 jordan

Added ejectable bay for IDE cdroms
ok marco@


# 1.137 30-Apr-2009 marco

Oga changed apm ioctls and forgot to grep. Temporary work around by hitting
the sleep button when we get any request. This interface will chnage at
some point.
ok oga


# 1.136 19-Apr-2009 krw

Call acpi_sleep_walk() when powering down just like when entering
any other sleep state. This tells acpi wake devices to not wake up
the box until manual powering up resets their status. Makes my Dell
GX520 stay powered off in response to 'halt -p'.

In snaps for a while.

ok marco@ (several times) deraadt@


# 1.135 17-Apr-2009 pirofti

Add device ranking support for future sleep/wake developments.

okay marco@


# 1.134 11-Mar-2009 jordan

Cleanup.. use aml_evalinteger instead of aml_evalname
ok marco@


# 1.133 10-Mar-2009 jordan

Fix cut/paste error.. call _BFS not _PTS
ok marco@


Revision tags: OPENBSD_4_5_BASE
# 1.132 21-Feb-2009 marco

overzealous cut/paste; found by Vladimir Kirillov <proger@uaoug.org.ua>


# 1.131 20-Feb-2009 marco

Oops didn't mean for Debugger() to remain there.


# 1.130 19-Feb-2009 marco

suspend/resume bits so that we can develop this in tree. This is disabled.
code from mlarkin and me
help from art,toby,jordan and several others
ok jordan, go for it deraadt


# 1.129 10-Feb-2009 jordan

Fix null pointer for shutdown if acpi not enabled
ok marco@


# 1.128 04-Feb-2009 kettenis

Some systems that claim to be ACPI 2.0 apparently don't provide an XSDT
address. Make the code fall back on the RSDT on those systems.

From Joerg Niendorf <p3y80@internode.on.net>. Fixes PR 5986.


# 1.127 17-Jan-2009 krw

A couple of stray malloc/memset -> malloc(M_ZERO), a bit of knf and
lose a couple of unnecessary casts. No functional change.

Feedback from kettenis@

ok kettenis@ marco@


# 1.126 06-Nov-2008 marco

Collapse all sanity checks of hids into one function.

Originaly from jordan


Revision tags: OPENBSD_4_4_BASE
# 1.125 02-Jul-2008 fgsch

add acpivideo. for now almost an empty skel so others can work on
suspend/resume. eventually it will also manage output switching and
brightness where supported. prodded by marco@


# 1.124 11-Jun-2008 marco

Fix $OpenBSD$ for once and for all


# 1.123 08-Jun-2008 deraadt

aaa_dev was being set from an uninitialized variable; ok toby


# 1.122 07-Jun-2008 marco

Kill ACPIVERBOSE. It was never used and is worthless to boot.

comments jmc
ok toby


# 1.121 06-Jun-2008 marco

Shrink acpi some more for floppies and while here re-enable acpi on the
floppies. Fixes crazy sony vaio kettenis has.

tested by deraadt
ok deraadt jordan kettenis


# 1.120 01-Jun-2008 marco

Fix \n nit in SMALL_KERNEL.


# 1.119 01-Jun-2008 marco

Re-enable new parser. Lots of testing by various folks. Fits all media
now too.

ok kettenis deraadt


# 1.118 16-May-2008 dlg

back out jordans new parser.

it didnt have enough oks from the right people, it did break machines
that were previously working, and it bloated the RAMDISKS and install
media beyond the constraints of the media.

requested by deraadt@


# 1.117 14-May-2008 jordan

Adding new ACPI Parser code
Old guts of code still exists, needs to be torn out next
ok marco@
ok brad@


# 1.116 27-Apr-2008 jcs

add acpithinkpad(4), an acpi support driver for ibm/lenovo
thinkpads.

provides functionality that the bios did on older, non-acpi
thinkpads like respond to screen brightness keys and toggle the
bluetooth device on and off.

ok deraadt@ and marco@


# 1.115 24-Apr-2008 jsing

Introduce acpiasus(4), a driver for the ACPI based hotkeys found in many
ASUS laptops (including the ASUS EeePC) - largely based on NetBSD's
asus(4) driver. On the ASUS EeePC this allows us to enable/disable
wireless, change screen brightness and use the volume keys.

ok jsg@, weingart@


# 1.114 12-Apr-2008 kettenis

Get rid of acpi_s5 global variables; simply send SIGUSR2 instead of SUGUSR1
to tell init(8) to power down the machine.

ok krw@


Revision tags: OPENBSD_4_3_BASE
# 1.113 21-Jan-2008 jsg

Check against RESET_REG_SUP in FADT before doing the acpi reset dance
after all, not doing so appears to break more machines than it fixes.

ok marco@


# 1.112 05-Jan-2008 kettenis

Never attach more acpicpu(4) devices than cpu(4) devices. Always attach
the first acpicpu(4) device on non-MULTIPROCESSOR kernels, regardless of its
ACPI processor ID.

ok mikeb@


# 1.111 05-Dec-2007 deraadt

Hang acpi(4) below bios(4) instead of mainbus(4). This lets us move acpi(4)
before pcibios(4), and then.... if acpi(4) attaches, skip pcibios(4) since
it messes the machines up. Fixes claudio's HP dl320 G5, and almost assuredly
others.
ok kettenis


# 1.110 05-Dec-2007 deraadt

repair acpiopen/acpiclose and friends to handle the /dev/apmctl interface,
so that apmd(8) can actually work correctly against acpi(4), and thus,
even do the fancy -C/-A stuff; also tested by jmc


# 1.109 02-Dec-2007 fgsch

fix the trailing space for real now.


# 1.108 02-Dec-2007 fgsch

avoid trailing spaces on output; marco@ ok.


# 1.107 27-Nov-2007 kettenis

Fix off-by-one that made us attach acpicpu0 and acpicpu1 on GENERIC kernels.

ok marco@


# 1.106 25-Nov-2007 jsg

When available and ACPI is enabled use the ACPI reset method
to reboot. This is required for some quirky sis machines
and likely others to reboot.

Tested by many. 'commit early commit often' deraadt@


# 1.105 19-Nov-2007 deraadt

sigh, amd64 still uses that stupid acpi_enabled variable...


# 1.104 19-Nov-2007 deraadt

softc's come initialized to 0, you know...


# 1.103 19-Nov-2007 deraadt

store revision; from jsg


# 1.102 19-Nov-2007 kettenis

Initialize sc_facs earlier.

ok deraadt@, beck@, canacar@


# 1.101 16-Nov-2007 deraadt

fix the bus_space #define nightmare, so that amd64 and i386 are much more
uniform. as a result shared code like acpi needs less #ifdef's
ok marco kettenis


# 1.100 15-Nov-2007 deraadt

remove the #ifdef ACPI_ENABLE. the EC codepath is either safe enough (and
will be improved), or it is unsafe because AML assumes it must be there
ok almost everyone


# 1.99 15-Nov-2007 mikeb

Make acpicpu(4) attach to the existing CPUs only.

Problem reported by form@. Fix idea by kettenis@.
Lots of help from deraadt@. Tested by deraadt@, form@ and me.

ok deraadt marco


# 1.98 14-Nov-2007 deraadt

spacing


# 1.97 12-Nov-2007 deraadt

correct : vs , in attach-time messages


# 1.96 06-Nov-2007 deraadt

all ACPI_ENABLE kernels need acpi_interrupt()


# 1.95 06-Nov-2007 deraadt

move acpi_read_pmreg() and acpi_write_pmreg() out of SMALL_KERNEL so
that acpi ramdisks can compile (obvious solution)


# 1.94 05-Nov-2007 canacar

Establish the acpi interrupt earlier. Fixes problems with some
GENERIC.MP kernels not getting acpi interrupts. This is a
workaround until the real issue with late establishment is fixed.
ok weingart@, beck@, gwk@


# 1.93 03-Nov-2007 jordan

Added support for displaying wakeup devices
ok beck@,weingart@,gwk@


# 1.92 11-Oct-2007 kettenis

When walking the tree yo find _PRT methods, check whether the device is
actually enabled and functioning. Similar to what we do for _INI.

ok weingart@, beck@


# 1.91 08-Oct-2007 krw

More simple memset(,0,) -> M_ZERO changes. In this batch move to
size(*p) as the first malloc() parameter where p is declared locally
and thus easy to check. Add M_ZERO to gpe_table allocation in acpi.c
even though there is no obvious bzero or memset nearby.


# 1.90 13-Sep-2007 mk

Remove unnecessary memset() (see aml_evalnode(9)) and a bit of KNF.

ok toby


# 1.89 13-Sep-2007 weingart

Implement ACPI 6.5.1 spec tree walk for _STA and _INI.
Fixes mk's laptop. No regressions so far (thank you to the
testers).

ok gwk@, mk@, marco@


Revision tags: OPENBSD_4_2_BASE
# 1.88 17-Apr-2007 mk

Now that acpidock no longer hangs machines during boot (at least
according to the reports I've been getting) it should be safe to enable
it again, so remove the #if 0 stuff around it.

ok marco


# 1.87 11-Apr-2007 jordan

Added changes for C-State
ok marco@


# 1.86 26-Mar-2007 marco

Calculate minutes left more accurately.

From giovanni <qgiovanni@gmail.com>


Revision tags: OPENBSD_4_1_BASE
# 1.85 03-Mar-2007 mk

Stop looking for acpidock until we fix that stuff that breaks all pre-60
series ThinkPads with acpidock(4) enabled.


# 1.84 22-Feb-2007 marco

We were getting no interrupts in .MP. There are 2 fixes; one in
acpi_machdep.c and move the interrupt establishment down after ioapic
has been initialized.

ok jordan


# 1.83 21-Feb-2007 kettenis

Make an acpi-enabled GENERIC.MP work on systems that only support PIC mode.

ok marco@


# 1.82 19-Feb-2007 jordan

Added acpi_map_address function for mapping GAS/IO registers
ok marco@


# 1.81 18-Feb-2007 jordan

Added dynamic perfect hash for opcodes
ok marco@


# 1.80 18-Feb-2007 marco

Shave off another K pre compression.
Shuffle SMALL_KERNEL functions around so that we only have a single #ifndef


# 1.79 17-Feb-2007 marco

Add SMALL_KERNEL throught acpi to make it fit on boot media.
ok deraadt


# 1.78 13-Feb-2007 marco

Add smarter _INI code. This helps with some dissapearing devices.

ok kettenis jordan


# 1.77 31-Jan-2007 gwk

Introduce acpi_hasprocfvs thats "ACPI Has Processor Frequency and Voltage
Scalling" a global flag set by acicpu attach to indicate that ACPI is
handling fvs through the _PSS objects. This will fix disappearing
EST/powernow when booting kernels with acpi enabled, as acpicpu isnt
compiled into GENERIC yet. Also in cases where acpicpu is built in and
legacy EST/powernow work but acpi is missing the _PSS object, the legacy
routines will be used.

tested by dim@, and Nick Nauwelaerts <nick AT nauwelaerts.net>
ok marco@, dim@


# 1.76 22-Jan-2007 mk

Actually look for docking stations by looking for _DCK nodes.

ok marco


# 1.75 02-Jan-2007 marco

On some systems EC must be attached as the first device. Move EC from
generic _HID devices to its own routine to force first attach. Bug found
and debugged by joris@

Code mostly written by joris@


# 1.74 26-Dec-2006 marco

Add polling to devices that require it such as acpibat and acpitz.
Use the same tsleep/wakeup mechanism as the interrupt code to keep it non-
concurrent.


# 1.73 21-Dec-2006 deraadt

even more knf love


# 1.72 21-Dec-2006 marco

Remove some leftover acpiec gpe/interrupt code.


# 1.71 20-Dec-2006 deraadt

oops, name already used


# 1.70 20-Dec-2006 deraadt

rename __acpi_enable_gpe(); ok marco


# 1.69 20-Dec-2006 deraadt

acpi_unmap_pmregs() is not used, ok marco


# 1.68 19-Dec-2006 deraadt

simplify some low-level methods substantially (shrinking code); ok marco jordan


# 1.67 06-Dec-2006 mk

malloc(..., M_NOWAIT) with no NULL check is bad. If allocation fails,
complain, clean up, and bail out of attach.

input and ok gwk


# 1.66 28-Nov-2006 jordan

Fixed clearing arg for acpi_init_pic
ok kettenis@


# 1.65 27-Nov-2006 kettenis

Don't spam dmesg.


# 1.64 27-Nov-2006 jordan

Fixes for acpiprt routing
Post-parse fixup for namerefs
ok kettenis@


# 1.63 25-Nov-2006 marco

Don't handle acpiev events inside the gpe handler since this causes deadlocks.

ok jordan@


# 1.62 15-Nov-2006 kettenis

Hook up acpi apic interrupt routing support to generic acpi code.

ok jordan@


# 1.61 02-Nov-2006 marco

Kill acpiec attach function and add its functionality to the generic _HID
device attach

prodded and ok jordan


# 1.60 02-Nov-2006 marco

Fix comments to reflect reality.


# 1.59 19-Oct-2006 marco

Replace ACPI ioctls with APM ioctls. This makes ACPI talk the same protocol
as APM therefore userspace can now retrieve power information in the same
fashion as all other apm arches (zaurus, macppc, i386 etc).

To use force the use of acpi device like so: apmd -f /dev/acpi

ok jordan@


# 1.58 19-Oct-2006 jordan

Fixed GPE debug levels
Fixed ref counter on temporary bufferfields
DSDT parsing fully working on IBM Thinkpad
ok marco@


# 1.57 15-Oct-2006 jordan

Removed old GPE handler code remnants
ok marco@


# 1.56 12-Oct-2006 jordan

Massive ACPI changes
Fixed ACPI leaks
new ACPI API
ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.55 30-Jun-2006 gwk

KNF, No functional change,
"go at it" marco@


# 1.54 30-Jun-2006 jordan

Support for calling Embedded Controller via new GPE interface
ok marco@


# 1.53 30-Jun-2006 jordan

Added new code for GPE handling
Also works with ACPIEC; need to fixup acpiec.c to remove original interrupt handler
ok marco@


# 1.52 30-Jun-2006 jordan

Added offset argument for acpi read/write pmreg routines (new GPE code)
ok marco@


# 1.51 31-May-2006 todd

do not call acpi_powerdown() unless acpi has successfully initialized
ok gwk@ canacar@


# 1.50 29-May-2006 canacar

Add embedded controller support.
ok marco@ deraadt@ testing & ok gwk@


# 1.49 19-May-2006 canacar

Minimal thermal zone driver. Monitors thermal zone temperature,
shuts down the system if the 'critical temperature' is reached.
ok marco@


# 1.48 11-Apr-2006 marco

KNF.


# 1.47 11-Apr-2006 gwk

Add acpi_resume(struct acpi_softc *sc), which calls some AML methods,
detected at attach time, currently unused.

ok marco@


# 1.46 11-Apr-2006 gwk

Discover some ACPI sleep related methods, and call them at the
appropriate time in acpi_enter_sleep_state().

ok marco@


Revision tags: OPENBSD_3_9_BASE
# 1.45 26-Feb-2006 marco

Add acpicpu as a device.

ok jordan@


# 1.44 23-Feb-2006 marco

remove dead function.

ok jordan@


# 1.43 22-Feb-2006 marco

Print out all tables it found during autoconf. We'll need this data in
the future.


# 1.42 22-Feb-2006 marco

Make dmesg less verbose.

ok jordan.


# 1.41 22-Feb-2006 jordan

Added parsing of SSDT tables
ok marco@


# 1.40 22-Feb-2006 jordan

Added new aml_register to notify by device id
Moved powerdown code to button device
ok marco@


# 1.39 21-Feb-2006 marco

Readjust debug levels that crept in too low over the last few days.
KNF


# 1.38 21-Feb-2006 marco

Rig the power button so that the machine shuts down correctly.


# 1.37 20-Feb-2006 marco

Add acpibtn device. Remains disabled for now.


# 1.36 19-Feb-2006 jordan

Added code for aml_notify GPE notification callback
ok marco@


# 1.35 19-Feb-2006 grange

Fix acpi_enter_sleep_state() by replacing current ugly hack
with mostly correct sleeping state transition code.

ok brad@ marco@


# 1.34 19-Feb-2006 marco

jordan:
Enable interrupts.
Added peripheral code to make this work.

marco:
Replace softintr and other types of interrupts with a handler with context.
Fixed interrupt magic for GPEs.
Some KNF

This is work done by jordan and I but has to be a single commit.


# 1.33 17-Feb-2006 marco

Remove earlier introduced delays. We really only need them during autconf.

ok jordan@


# 1.32 17-Feb-2006 marco

Add a generic delay function as per the ACPI spec.

from jordan@


# 1.31 17-Feb-2006 marco

Use tsleep instead of delay when !cold.


# 1.30 17-Feb-2006 marco

Argh, we need delays between reads/writes. The backend reads (i2c most likely)
are so slow that values come back as 0 sometimes. We have to do this
despite the spec saying we shouldnt have to. Guess having a faster
parser than others can hurt sometimes.

Idea from jordan.


# 1.29 17-Feb-2006 jordan

Fixed code for handling _INI
Rearranged debug statements
ok marco@


# 1.28 16-Feb-2006 jordan

Added stub for calling _INI
Fixed aml_dowhile for NULL return
ok marco@


# 1.27 16-Feb-2006 jordan

Fixed parser/evaluator to retrieve battery info+state
Cleanup of parser
Dell laptops return battery status
ok marco@


# 1.26 06-Feb-2006 brad

fix a typo so this will compile on amd64.


# 1.25 04-Feb-2006 marco

When ACPI_ENABLED wasn't set but the devices were it had an unknown variable.

Pointed out by ian@.


# 1.24 03-Feb-2006 jordan

Rewrote aml parser yet again...
Fixed bus_space_map for i386/amd64
Added _BST and _BIF to acpibat to get status/info
ok marco@


# 1.23 20-Jan-2006 jordan

More evaluator changes
Removed old evaluator code
Fixed _HID value
ok marco@


# 1.22 19-Jan-2006 jordan

Added support for read/write of PCI Config space
Help jason@
ok marco@


# 1.21 18-Jan-2006 jordan

Added new evaluation routines
Added helper methods for buffer/field evaluation
Fixed broken indentation on dsdt.c

ok marco@


# 1.20 17-Jan-2006 jordan

Major overhaul of the aml parser. Evaluater temporarily disabled.

ok marco@


# 1.19 06-Jan-2006 grange

Check for ACPI control availability only if ACPI_ENABLE defined.


# 1.18 06-Jan-2006 grange

Informative error messages on attach, prettier dmesg.


# 1.17 06-Jan-2006 grange

printf -> dnprintf conversion for debug messages.


# 1.16 05-Jan-2006 grange

Find SLP_TYPx values and store them for future use in
acpi_enter_sleep_state().


# 1.15 05-Jan-2006 grange

Random KNF and cleanup


# 1.14 05-Jan-2006 grange

Provide symbolic names for the acpi sleeping states and use it.


# 1.13 28-Dec-2005 marco

Moved DEVNAME to a acpidev.h

ok jordan@


# 1.12 18-Dec-2005 sturm

add back comment about wrong code

ok marco


# 1.11 16-Dec-2005 marco

Remove test for kbd_reset. Prompted deraadt@, ok jordan@


# 1.10 16-Dec-2005 jordan

Added hooks for powerdown/power button press


# 1.9 16-Dec-2005 jordan

Cleaned up common power management register i/o
Added enter sleep state ioctl
Hooked power button interrupt
ok marco@


# 1.8 16-Dec-2005 marco

Remove autoconf chatter


# 1.7 16-Dec-2005 jordan

Moved DSDT AML parsing into parent ACPI driver
ok marco@


# 1.6 14-Dec-2005 marco

Add AC device glue.


# 1.5 14-Dec-2005 marco

Adjust debug printing levels to something more sane.


# 1.4 13-Dec-2005 marco

Add glue to attach devices to apci.


# 1.3 07-Dec-2005 marco

KNF


Revision tags: OPENBSD_3_8_BASE
# 1.2 10-Jul-2005 grange

Spacing cleanup.


# 1.1 02-Jun-2005 tholo

Start on a basic ACPI framework -- does not do much more than read out the
ACPI tables into kernel memory and attach ACPI and HPET timers currently.

In order to test this code, enabling the devices in GENERIC as well as
the ACPI_ENABLE option is needed. This code does not do any thermal
control yet, so this should be done with care depending on the platform.

In the tree so more people can contribute to making this more fully
featured.

Ok niklas@ grange@ tedu@


# 1.408 02-Feb-2022 deraadt

acpi_addtask() calls malloc() w/ M_NOWAIT (because some calls come from
interrupt context), this however means occasional resource shortage will
result in callbacks registration failing, and unknown consequences for
the task-submitting caller.
Changing this to use pools with a low water mark, decreases the odds
of that problem occuring.
ok kettenis


# 1.407 01-Feb-2022 deraadt

GPE_DIRECT is calling the AML parser from interrupt context, which is
not permitted. Luckily nothing is using GPE_DIRECT anymore, so this code
can be deleted.
ok kettenis


# 1.406 26-Jan-2022 kettenis

An ACPI device needs to be both present and enabled for it to function.
So only attempt to attach hardware that has both bits enabled. This fixes
an issue where com(4) would attach for a disabled serial port leading to
misdetection of the hardware variant and a subsequent hang when /etc/rc
runs ttyflags -a.

ok anton@, deraadt@


# 1.405 12-Jan-2022 patrick

Make acpi_getpropint() return uint64_t, as ACPI integers are in fact that
wide and some _DSD properties depend on it.

ok kettenis@


# 1.404 04-Jan-2022 patrick

acpi_getprop() needs to actually make sure that we're looking at the
correct property. While there adjust acpi_getpropint() as well to
increase similarity with acpi_getprop().

ok kettenis@


# 1.403 16-Dec-2021 anton

Attach com over acpi on amd64. Some hardware uses a different interrupt
assignment compared to the the legacy one supported by com over isa.
This causes the console to halt once userland takes over as no
interrupts are received. The actual address and irq can be read from
ACPI, kettenis@ already added support for arm64 which paved the way for
amd64.

Some consoles that previously attached over isa are now expected to
attach over acpi.

Thanks to patrick@ for testing on arm64.

ok kettenis@


# 1.402 07-Dec-2021 kettenis

Fix parsing of SR_IRQ resource descriptors.

ok patrick@, anton@


# 1.401 02-Nov-2021 deraadt

knf


# 1.400 30-Oct-2021 deraadt

Change hw.perfpolicy=auto by default, at startup. If the system has AC
power connected (default is yes when no driver differentiates) then default
to 100% performance. On battery, use the existing auto algorithm (which is
admittedly somewhat unrefined).
This change overrides the system/BIOS speed and puts OpenBSD in control.
As this happens very early during boot, besides speedups in all usage usage
patterns, some surprises: unhibernate and sysupgrade times are cut in half.
note: on a few architectures, the setperf fn pointer is changed late, and
thus the auto algorithm stops timeing out. kettenis and i will look for
a solution.
in snaps for more than a week.
ok kettenis


Revision tags: OPENBSD_7_0_BASE
# 1.399 20-Jul-2021 mlarkin

Fix trailing whitespace in a few places.

No code change.


# 1.398 09-May-2021 kettenis

Some x86 machines advertise the "hardware reduced" ACPI feature, advertise
S4 and S5 support, but fail to populate the SLEEP_CONTROL_REG and
SLEEP_STATUS_REG descriptions in the FADT. An example of such a machine
is the ASUS Zenbook 14 UM433DA. Any attempt to powerdown the machine
will result in a kernel crash. It turns out that using the legacy ACPI PM
registers works fine on this machien. So fall back on those registers
if SLEEP_CONTROL_REG and/or SLEEP_STATUS_REG aren't provided.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_9_BASE
# 1.397 15-Mar-2021 patrick

Add acpi_iommu_device_map(), which replaces the DMA tag with one that
is blessed with IOMMU magic, if available. This is mainly for arm64,
since on amd64 and i386 the IOMMU only captures PCIe devices, as far
as I know, which uses the pci_probe_device_hook(). This though is for
non-PCI devices attached through ACPI.

ok kettenis@


# 1.396 07-Mar-2021 patrick

Pass standard DMA tag to acpi(4) table drivers.

ok kettenis@


# 1.395 27-Dec-2020 deraadt

acpi_map_address() cannot be wrapped by SMALL_KERNEL anymore, as
it is used by acpihpet.c


# 1.394 25-Dec-2020 visa

Refactor klist insertion and removal

Rename klist_{insert,remove}() to klist_{insert,remove}_locked().
These functions assume that the caller has locked the klist. The current
state of locking remains intact because the kernel lock is still used
with all klists.

Add new functions klist_insert() and klist_remove() that lock the klist
internally. This allows some code simplification.

OK mpi@


# 1.393 17-Dec-2020 kettenis

Fix some issues with referencing named ACPI nodes from Packages.
These references need to be resolved at runtime rather than when
they're parsed such that they pick up the right values for those nodes
which can be changed when for example _INI methods run.

The current approach is to replace these reference with a string that
names the node in question. The problem with that is that packages
can also contain normal strings. Which means that we need special
code that depends on the context where the package is used.

This diff takes a different approach by retaining a reference when
parsing. Code that uses a package will need to resolve this reference
but there is no ambiguiety anymore.

ok patrick@


# 1.392 05-Dec-2020 kettenis

Add support for the _EVT method to the GPIO support code.

ok patrick@


Revision tags: OPENBSD_6_8_BASE
# 1.391 27-Aug-2020 jmatthew

Reorder the acpi attach process so we look at Processor() CPU nodes
after Device() ones, since we should prefer the newer node type.
If we see any Device() nodes, don't attach acpicpu(4) to any Processor()
nodes if they're also present. This also makes acpitz(4) and
acpipwrres(4) devices attach slightly later.

ok kettenis@, also tested by jmc@


# 1.390 16-Aug-2020 gnezdo

Remove sc_maxgpe from acpi_softc which was never read from

Remove a dead conditional from acpi_init_gpes while there.

ok jcs@


# 1.389 21-Jul-2020 deraadt

acpi can use IPL_BIO (a low interrupt) since it only enqueues operations for
later processing. The use of a high interrupt will predate suspend/resume
efforts, we had to redesign acpi to be non-reentrant obviously
discussed with kettenis, in snaps for more than a week


# 1.388 24-Jun-2020 cheloha

kernel: use gettime(9)/getuptime(9) in lieu of time_second(9)/time_uptime(9)

time_second(9) and time_uptime(9) are widely used in the kernel to
quickly get the system UTC or system uptime as a time_t. However,
time_t is 64-bit everywhere, so it is not generally safe to use them
on 32-bit platforms: you have a split-read problem if your hardware
cannot perform atomic 64-bit reads.

This patch replaces time_second(9) with gettime(9), a safer successor
interface, throughout the kernel. Similarly, time_uptime(9) is replaced
with getuptime(9).

There is a performance cost on 32-bit platforms in exchange for
eliminating the split-read problem: instead of two register reads you
now have a lockless read loop to pull the values from the timehands.
This is really not *too* bad in the grand scheme of things, but
compared to what we were doing before it is several times slower.

There is no performance cost on 64-bit (__LP64__) platforms.

With input from visa@, dlg@, and tedu@.

Several bugs squashed by visa@.

ok kettenis@


# 1.387 10-Jun-2020 jca

Give userland a consistent battery and A/C state after resume

Previously we refreshed the state of acpiac(4) and acpibat(4)/acpisbs(4)
by queueing fake ACPI events to be processed by the acpi thread.
acpibat(4) using ACPIDEV_POLL meant that its status could be refreshed
first, resulting in an APM_POWER_CHANGE event sent to userland before
the state of acpiac(4) was up to date. Because of this, apmd(8) could
see A/C unplugged when it was in fact plugged, possibly leading to
a wrong decision.

Instead of using fake ACPI events, refresh the status of acpiac(4) and
acpibat(4)/acpisbs(4) right away at DVACT_WAKEUP time. To reuse the
existing refresh code of those drivers, make their ACPI *_notify()
handlers responsible for sending APM_POWER_CHANGE events instead. Also
queue an APM_POWER_CHANGE event right after resume to retain existing
behavior.

This fixes the consistency of data shown to userland, and makes those
drivers less special.

Initially discussed last year (!) with mpi@ at Miod's place.
Feedback and ok jcs@, deraadt@ agrees with the use of DVACT_WAKEUP here.


# 1.386 29-May-2020 deraadt

dev/rndvar.h no longer has statistical interfaces (removed during various
conversion steps). it only contains kernel prototypes for 4 interfaces,
all of which legitimately belong in sys/systm.h, which are already included
by all enqueue_randomness() users.


# 1.385 14-May-2020 kettenis

Bring back the acpi(4) changes. This time around only use the ACPI _CRS
information on ACPI 5.0 and later.

ok krw@, patrick@


# 1.384 11-May-2020 jca

More accurate remaining power reporting with disparate batteries

Overall remaining power was computed as the average of the remaining
power of each battery, in percents. This doesn't work with batteries of
different capacity, so use the total capacity and remaining power
instead.

Reported by lidstah, successful tests by lidstah and benno@, ok kettenis@


# 1.383 08-May-2020 kettenis

Move parsing of _CRS into acpi(4) and pass the parsed address and interrupt
information in struct acpi_attach_args.

ok mpi@


Revision tags: OPENBSD_6_7_BASE
# 1.382 14-Apr-2020 kettenis

Implement acpi_getprop(), the ACPI equivalent of OF_getprop().

ok patrick@


# 1.381 12-Apr-2020 kettenis

Add support for the _CCA method. This method indicates whether DMA is
cache-coherent or not. To implement this, acpi(4) gets two bus_dma tags
and passes the appropriate one when attaching devices based on _CCA.
On i386/amd64, where for all practical purpose DMA is always cache-coherent,
the two tags are the same. But on arm64 they are distinct.

ok patrick@


# 1.380 07-Apr-2020 visa

Abstract the head of knote lists. This allows extending the lists,
for example, with locking assertions.

OK mpi@, anton@


# 1.379 03-Apr-2020 mpi

Fix incoherencies of errors returned by various *kqfilter routines.

EOPNOTSUPP is returned when there's no "filterops" for a given fd.
EINVAL when the requested filter isn't supported by the "filterops".
ENXIO means the underlying device is no longer valid/present.

ok visa@


# 1.378 20-Feb-2020 visa

Replace field f_isfd with field f_flags in struct filterops to allow
adding more filter properties without cluttering the struct.

OK mpi@, anton@


# 1.377 31-Dec-2019 visa

Use C99 designated initializers with struct filterops. In addition,
make the structs const so that the data are put in .rodata.

OK mpi@, deraadt@, anton@, bluhm@


# 1.376 31-Dec-2019 mpi

Convert infinite sleeps to {m,t}sleep_nsec(9).

ok kn@


# 1.375 31-Dec-2019 jsg

include sys/sensors.h for sensor use
don't depend on indirect inclusion from acpidev.h


Revision tags: OPENBSD_6_6_BASE
# 1.374 07-Sep-2019 kettenis

Revert acpipci(4) changes. Too many systems are broken. We'll try again
after 6.6 as been released. The acpireg.h change stays behind.


# 1.373 28-Aug-2019 kettenis

Use ACPI information to attach PCI busses like we do on arm64. There are a
few additional quirks though, and attaching the PCI busses is delayed to
replicate the existing code more closely. That may be changed in the
future. Also tweak how we handle MSI support and respect to ACPI flag
that says we shouldn't attempt to use MSIs.

Some fallout is expected.

ok patrick@


# 1.372 27-Aug-2019 deraadt

On amd64 the bootup and resume paths for SP/BP/SP were not performing
msr/pat, clock-startup, firmware upgrade, fpu initialization, and cpu
soft-state initialization in the same order. That kind of chaos is
crazy and fragile. Improve the acpi resume call sequence and unify order
of the operations.


# 1.371 02-Jul-2019 jcs

acpi_set_gpehandler: turn edge arg into flags, add support for
passing GPE_DIRECT which directly executes the supplied callback and
clears a level interrupt rather than going through acpi_taskq which
can be slow when other driver callbacks are in the queue.

This should not be used for GPE handlers that need to do anything
ACPI-related.

ok kettenis


# 1.370 10-Jun-2019 kettenis

Print proper ACPI version number.

ok mlarkin@


# 1.369 08-Jun-2019 kettenis

When establishing the mapping between ACPI device nodes and PCI devices,
skip PCI host bridges and devices that aren't there.

ok patrick@


# 1.368 07-Jun-2019 kettenis

Introduce acpi_getsta() and use it to eliminate some duplicated code.

ok patrick@


# 1.367 12-May-2019 kettenis

Make sure the extended PM register descriptions actually exist before
using them. Fixes machines with ancient ACPI 1.0 (such as recent
hypervisors).

ok deraadt@


# 1.366 12-May-2019 kettenis

Add support for mapping ACPI PM registers using the "extended" gasio
representation from the FADT. Mostly fixes the Lanner NCA-1510.

ok lteo@, mlarkin@, deraadt@


# 1.365 11-May-2019 lteo

Fix incorrect error message.

ok kettenis@


# 1.364 08-May-2019 tedu

free size. the allocation is the header plus the length.


Revision tags: OPENBSD_6_5_BASE
# 1.363 04-Apr-2019 kettenis

Avoid potential null-pointer dereference. Found by Kent R. Spillner.

ok deraadt@, jsg@


# 1.362 20-Jan-2019 jsg

fix SMALL_KERNEL build after changes in rev 1.361
reported by naddy@ ok deraadt@


# 1.361 19-Jan-2019 tedu

Add a pwraction sysctl that controls what the power button does on acpi.
By default, nothing changes -- shutdown is initiated. But allows turning
power button into a sleep button if desired.
(grudging) ok from a few parties


# 1.360 26-Oct-2018 kettenis

Add acpipci(4) on amd64. For now this only calls the PCI-specific _OSC
method to let the ACPI implementation know what features we support.


Revision tags: OPENBSD_6_4_BASE
# 1.359 30-Aug-2018 kettenis

Move softbutton check before the check that skips attaching certain acpi
devices. Fixes regression caused by matching on _CID in addition to
matching on _HID.

ok matthieu@, mlarkin@


# 1.358 25-Aug-2018 kettenis

Match ACPI devices based on _CID if no match for _HID is found.

ok mpi@


# 1.357 19-Aug-2018 kettenis

Add support for multiple PCI segments. Only really implemented for arm64
for now as amd64/i386 firmware still caters for legacy OSes that only
support a single PCI segment.

ok patrick@


# 1.356 03-Aug-2018 kettenis

Let ahci(4) match on _CLS instead of _HID when attaching at acpi(4). Avoids
having to add many more _HID entries to the match table.

ok deraadt@, mlarkin@


# 1.355 10-Jul-2018 kettenis

Make legacy interrupts work in acpipci(4).

ok patrick@


# 1.354 05-Jul-2018 kettenis

Add acpipci(4), a driver that supports generic ECAM-compatible PCI host
bridges based on information provided by ACPI.

ok mlarkin@


# 1.353 04-Jul-2018 kettenis

Properly pass around the PCI "chipset tag" in acpi(4) and refactor
acpimcfg(4) to call an MD initialization functions that sets up a tag for
PCI ECAM.

ok guenther@, mlarkin@, krw@


# 1.352 01-Jul-2018 kettenis

Move acpi(4) attach glue into acpi_machdep.c.

ok guenther@, deraadt@


# 1.351 01-Jul-2018 kettenis

Add support for _DSD Device Properties.

ok mlarkin@


# 1.350 30-Jun-2018 kettenis

Add intr_enable() function, intended for MI use to amd64 and i386 and use
this in the acpi(4) suspend/resume code paths.

ok deraadt@


# 1.349 29-Jun-2018 kettenis

Use <stdint.h> types.

ok mpi@, jung@, krw@, deraadt@


# 1.348 29-Jun-2018 mlarkin

ACPI: Allow (with warning) GPE handler reassignment, instead of returning
a failure code.

ok kettenis


# 1.347 26-Jun-2018 mlarkin

knf


# 1.346 26-Jun-2018 kettenis

Let acpi(4) pass down the bus dma tag.

ok deraadt@


# 1.345 25-Jun-2018 kettenis

Reorganize acpi(4) code a bit in preparation for upcoming arm64 support.

ok deraadt@


# 1.344 20-May-2018 kettenis

Wake up the acpi thread after scheduling a gpio event.

ok mpi@


# 1.343 17-May-2018 kettenis

Sprinkle some #ifndef SMALL_KERNEL such that RAMDISK kernels cuild again.

pointed out by stsp@


# 1.342 17-May-2018 kettenis

Implement GenericSerialBus OpRegion support.

ok mlarkin@


# 1.341 27-Mar-2018 kettenis

Add acpicmos(4), a driver that implements SystemCMOS OperationRegion
access support. This fixes machines where the AML doesn't check whether
support for this OperationRegion type has been registered by the OS.

ok mlarkin@


Revision tags: OPENBSD_6_3_BASE
# 1.340 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.339 10-Feb-2018 deraadt

Syncronize filesystems to disk when suspending. Each mountpoint's vnodes
are pushed to disk. Dangling vnodes (unlinked files still in use) and
vnodes undergoing change by long-running syscalls are identified -- and
such filesystems are marked dirty on-disk while we are suspended (in case
power is lost, a fsck will be required). Filesystems without dangling or
busy vnodes are marked clean, resulting in faster boots following
"battery died" circumstances.
Tested by numerous developers, thanks for the feedback.


# 1.338 08-Feb-2018 deraadt

After flushing disks, try again to release all memory. We don't need to
save dirty memory to the hibernate space.


# 1.337 08-Feb-2018 deraadt

Toss all releaseable memory, because fragmentation can get in the way
of allocating the hibernate playpen.


# 1.336 08-Feb-2018 deraadt

sr_quiesce() is a new approach for ensuring that softraid drains
output to the disks.

This is part of a larger suspend/resume filesystem-safety diff, which
has been worked on for a couple of months already. Tests by job, krw,
beck, benno, and others. Sometimes even by snapshot users...


# 1.335 29-Nov-2017 kettenis

Add support for GPIO-signaled events.

ok mlarkin@


# 1.334 16-Nov-2017 jcs

Add PCI attachment for dwiic(4) needed by Intel 100 Series machines

ok kettenis


# 1.333 14-Nov-2017 kettenis

Add a few more PNP IDs to the skip lists.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.332 17-Aug-2017 stsp

Make dwiic(4) attach its ACPI parent dependencies.
This change makes the Asus E200HA keyboard work.

Original analysis and patch by Cesare Gargano
ok kettenis@


# 1.331 09-Aug-2017 dcoppa

Add "PNP0303" (8042 PS/2 Controller) to acpi_isa_hids[]

ok kettenis@


# 1.330 22-Jul-2017 jcs

acpibat: look for _BIX first before falling back on _BIF

ACPI 4.0 deprecated _BIF for battery status so some newer machines
are exporting _BIX which extends _BIF's package with a few extra
fields. When using _BIX, export the new discharge cycle count as a
sensor.

feedback from kettenis and anton
ok deraadt


# 1.329 20-Jul-2017 kettenis

Start a list of isa(4) device names such that we can avoid printing them
as "not configured".

ok mikeb@, millert@, deraadt@


# 1.328 08-Apr-2017 deraadt

Sensors are run as callbacks inside tasks. During suspend a sensor
could be running inside a driver that will be force-detached, or due
to tsleep end up disrupting the softstate/hardstate contract. At
suspend time, quisce all these callbacks by waiting for completion.
This issue has never been observed for real, but may be implicated in
suspend/resume failures.
ok kettenis guenther mlarkin


# 1.327 08-Apr-2017 deraadt

Two prototypes for acpi_maptable; merge to one place


Revision tags: OPENBSD_6_1_BASE
# 1.326 27-Mar-2017 deraadt

Now that hibernate_alloc() only has clean success/failure, don't
need to call hibernate_free() to clean up a partial mess.
ok mlarkin kettenis


# 1.325 13-Mar-2017 deraadt

allow ramdisks to build; ok jcs


# 1.324 12-Mar-2017 jcs

try to attach acpisbs first and if successful, don't attach acpibat


# 1.323 02-Mar-2017 natano

Add a new sysctl machdep.lidaction. The sysctl works as follows:

machdep.lidaction=0 # do nothing
machdep.lidaction=1 # suspend
machdep.lidaction=2 # hibernate

lidsuspend is just an alias for lidaction, so if you change one, the
other one will have the same value. The plan is to remove
machdep.lidsuspend eventually when people have upgraded their
/ets/sysctl.conf.

discussed with deraadt, who came up with the new MIB name
no objections mlarkin
ok stsp halex jcs


# 1.322 28-Feb-2017 natano

Untangle abstract sleep modes from ACPI sleep states. This paves the way
for further work in the area.

idea and ok deraadt
ok mlarkin


# 1.321 22-Feb-2017 jcs

add acpisbs, an acpi smart battery subsystem driver reading data
over smbus

currently disabled because it conflicts with acpibat

ok deraadt, kettenis


# 1.320 14-Jan-2017 kettenis

Revert aml_rdpciaddr changes; breaks several machines that were working
before.


# 1.319 11-Jan-2017 jsg

Increase the size of the buffers used for _HID and _CID strings to be
able to fit "Hyper_V_Gen_Counter_V1".

ok mlarkin@ deraadt@


# 1.318 08-Jan-2017 kettenis

Make aml_rdpciaddr() work for for devices behind bridges. The code now
uses the mapping between AML nodes and PCI devices that we establish.
Because _INIT methods may end up calling aml_rdpciaddr(), make sure we
create that mapping early. Also handle devices that aren't actually present.
These devices are now included in the mapping and reads will return an
all-ones patterm whereas writes are a no-op..

ok mlarkin@


# 1.317 25-Oct-2016 pirofti

Add the resource index to aml_parse_resource() callback function.

This is needed by an upcoming acpiec commit that handles machines breaking
the current ACPI specifications.

Change suggested by and ok kettenis@, guenther@


# 1.316 18-Sep-2016 guenther

If the APIC table (MADT) isn't present then ci_acpi_proc_id won't be set
when acpicpu tries to attach, leading to a NULL dereference. The ACPI
processor object on the Dells where this was reported is bogus and useless
anyway so apply the same test when deciding whether to attach to processor
objects as acpicpu would when matching them to cpus.

Problem reported by Eivind Eide (xenofil (at) gmail.com) and
Johan Huldtgren (johan+openbsd-bugs (at) huldtgren.com).
Testing by Eivind Eide.
ok kettenis@ mlarkin@


# 1.315 03-Sep-2016 naddy

Write the system time back to the RTC every 30 minutes.
This fixes the problem that long-running machines which were not
shut down properly would reboot with a badly offset system time.

hints and ok kettenis@


# 1.314 31-Aug-2016 mlarkin

Don't put a device in the wake device list if _STA says it's not there.

Fixes at least one machine that woke up from sleep / poweroff immediately.

ok kettenis@, tested by kettenis and martijn@


# 1.313 28-Jul-2016 kettenis

Store the acpi processor ID/UID in struct cpu_info, and use it to attach
acpicpu(4) drivers to the right cpu(4).

ok mlarkin@, guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.312 10-Jun-2016 kettenis

The power management timer has been made optional in ACPI 5.0A, so don't
attempt to attach acpitimer(4) if the timer isn't present.

Pointed out by jcs@

ok jcs@


# 1.311 08-May-2016 kettenis

Make sure devices listed by a device's _DEP method are attached before
we attach the device itself. The _DEP methode, introduced in ACPI 5.0,
is a hint that the device in question depends on OpRegion support from
the devices listed by _DEP, so we have to order them the proper way.
To prevent us from attaching those devices again when we encounter them
later walking down the device tree, keep track of our attempts to
attach a device driver to them using aflag in the aml_node structure.

ok guenther@


# 1.310 25-Apr-2016 pirofti

We attach acpihpet(4) and acpiec(4) without using the HID, so do not
report it in the dmesg.

OK deraadt@, mlarkin@.


# 1.309 02-Apr-2016 kettenis

Reduce dmesg spam by knocking out some acpi hardware IDs that don't need a
driver.

ok mpi@


# 1.308 29-Mar-2016 kettenis

Check if a device is present (using _STA) in acpi_foundhid. This prevents
us from reporting devices as "not configured" that aren't actually present,
and allows us to remove duplicated code from several drivers.

ok deraadt@, ok jsg@


# 1.307 29-Mar-2016 kettenis

Implement a cleaner way to prevent acpivout(4) from attaching if we have
a platform-specific method of controlling the backlight.

ok millert@, mlarkin@, jung@


# 1.306 28-Mar-2016 kettenis

Adding each and every new acpi device driver to acpi_foundhid() is
gettinga bit of the burden. And it really isn't how our autoconf
framework is supposed to work for busses that can be enumerated. All
the drivers already check for a matching _HID in their attach
function. So we can just drop the checks in acpi_foundhid() and let
the drivers sort themselves out.

Print a "not configured" line for devices for which we don't attach adriver.
This may be a bit much as there are devices for which we will not have a
driver. This will be removed if it causes too much dmesg spam.

ok deraadt@, mlarkin@


Revision tags: OPENBSD_5_9_BASE
# 1.305 17-Jan-2016 jsg

Take acpi_foundhid() outside of #ifndef SMALL_KERNEL so the ramdisk
kernel can find and attach dwiic(4) and sdhc(4).


# 1.304 17-Jan-2016 jsg

remove an unused global variable


# 1.303 14-Jan-2016 kettenis

Attach dwiic(4) on the Intel Bay Trail i2c controllers as well.


# 1.302 12-Jan-2016 kettenis

Improve emulation of PM registers on Hardware-redcued ACPI by emulating
SPL_TYPx, SLP_EN and WAK_STS using the new SLEEP_STATUS_REG and
SLEEP_CONTROL_REG registers. Makes the transition into S5 (powerdown) work
on the ASUS X205TA.

ok deraadt@, mlarkin@


# 1.301 12-Jan-2016 jcs

Add dwiic, a driver for the Synopsys DesignWare i2c controller found
on the Samsung ATIV Book 9 laptop. This initial version only
supports ACPI config/attachment.

Add ihidev, a HID-over-i2c driver largely based on uhidev. dwiic
handles attaching ihidev devices found in ACPI.

Add ims, a HID-over-i2c mouse/trackpad driver to get basic cursor
and button functionality from HID-compliant i2c trackpads.

ok kettenis deraadt


# 1.300 11-Jan-2016 kettenis

Make sdhc(4) attach to hardware IDs "80860F14" and "PNP0FFF".


# 1.299 09-Jan-2016 kettenis

Initial support for Hardware-Reduced ACPI. This skips the legacy mode checks
by emulating the PM_CNT register such that SCI_EN is always enabled. It also
skip attaching acpitimer(4) since the relevant registers are no longer there.

ok deraadt@, mlarkin@


# 1.298 06-Jan-2016 kettenis

Follow the spec and check the SCI_EN bit to see if ACPI has already
been enabled and don't bother enabling it again if it is. This should fix
systems that don't implement legacy mode and therefore set ACPI_ENABLE and
ACPI_DISABLE to zero.

ok mlarkin@, yasuoka@


# 1.297 23-Nov-2015 reyk

Back out the CRITICAL change, turn it back from 10% to 25%. Some
people with old/broken batteries didn't like this change, and the
underlying heuristics are wrong.


# 1.296 22-Nov-2015 reyk

The "CRITICAL" APM battery level, that is exported/faked by the ACPI
stack, is currently set to 25%. With 25% battery left, a typical
modern laptop will have at least one hour runtime left. It is an
arbitrary value, and other APM power layers in the kernel set CRITICAL
to something like 5% or 10%, so set it to 10% for now.

OK mlarkin@


# 1.295 28-Sep-2015 deraadt

In low-level suspend routines, set cold=2. In tsleep(), use this to
spit out a ddb trace to console. This should allow us to find suspend
or resume routines which break the rules. It depends on the console
output function being non-sleeping.... but that's another codepath which
should try to be safe when cold is set.
ok kettenis


# 1.294 06-Sep-2015 deraadt

clarify malloc arguments (base + length...)


# 1.293 28-Aug-2015 deraadt

fairly simple sizes for free(); ok tedu


# 1.292 20-Aug-2015 kettenis

Respect the access size when reading or writing to pci config space.
This version also makes sure writes are properly aligned, preventing
the panics seen with the previous incarnation of this change.

Fixes battery status passthrough in vmware, brightness keys on my x220
and who knows what else.

ok mlarkin@, deraadt@


Revision tags: OPENBSD_5_8_BASE
# 1.291 04-Aug-2015 deraadt

If we are setting ACPI_PM1_BM_RLD do it only once, and remember to
repeat this action at resume time.
ok mlarkin guenther


# 1.290 29-Jul-2015 deraadt

Something subtle in previous revision is locking up suspend or booting
on some laptops. Mailing lists threads mention x220 and Asus machines.
Reverting for now.


# 1.289 21-Jul-2015 kettenis

iRespect the access size when reading or writing to pci config space.
Fixes battery status passthrough in vmware and who knows what else.

Tested by many.

ok mlarkin@, deraadt@


# 1.288 17-Jul-2015 mlarkin

fix comments


# 1.287 26-Mar-2015 jsg

Use the 0x10 extended capability to determine if the trackstick
buttons are wired to the trackpad and need to be re-routed to the
trackstick. Without this change the buttons on 2015 thinkpads
get picked up as extended buttons that show up as scroll up/down.

Remove the X1 Carbon 2015 (LEN0048) and X250 (LEN0046) from the
top button area/soft buttons quirks list. Also avoid using the
quirk list entirely if the capability bit is set.

Based on work from jcs@ with feedback from mpi@
ok mpi@ jcs@


# 1.286 20-Mar-2015 kettenis

The ACPI standard says that a PCI function number of FFFF can be used to refer
to all functions on a device. This makes no sense in the context of accessing
PCI config space. Nevertheless stsp@ has a machine with AML that tries to
do config space reads from a device where _ADR encodes function number FFFF.
This is almost certainly buggy AML, but since Linux (and presumably Windows)
boot just fine on this machine we need to work around this. Linux effectively
treats function FFFF as function 7, which isn't present on this machine.
Instead we simulate a read from a non-existent device by returning a bit
pattern of all ones. Writes to function FFFF will still panic.

ok mlarkin@


# 1.285 16-Mar-2015 deraadt

Reduce use of SMALL_KERNEL in acpi, bringing acpiec driver and more gpe
handling into RAMDISK. This is now possible because the install media
has ample room. The goal is to reduce special cases where we may be
skipping (unknown) important operations...
ok mlarkin kettenis


# 1.284 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.283 07-Feb-2015 deraadt

New framework that allows hibernate to pass in entropy from it's fresh
boot.
ok mlarkin


# 1.282 06-Feb-2015 mlarkin

Fix a hibernate crash on some machines due to unmapping a page that
may not have been mapped previously (in the failure to hibernate case).

Also ensure that the lowmem ptp is mapped in all cases (not just MP).

ok kettenis


# 1.281 17-Jan-2015 deraadt

resume_randomness() before spinning up other cpus...


# 1.280 15-Jan-2015 jsg

Define a new wscons mouse type for Synaptics clickpad devices that lack
physical buttons. This will be used if the acpi pnpid for the mouse
matches a list provided by Synaptics found in Linux.

Combined with the xenocara changes this will remove the need for an xorg
config file for the x240/t440/t540 generation of thinkpads.

ok matthieu@ shadchin@ kettenis@


# 1.279 11-Jan-2015 kettenis

Tolerate checksum errors in ACPI tables (but complain about them). Apparently
there are machines out there with broken BIOSen with such checksum errors in
thier RSDT tables. Windows runs fine on these machines, so it probably does
not check. Linux ignores checksum errors as well (and prints a warning).

ok deraadt@, krw@


# 1.278 18-Dec-2014 deraadt

suspend and resume the RNG (at what looks like the right spots)
ok reyk djm


# 1.277 09-Dec-2014 doug

Replace some malloc(n*size,...) calls with mallocarray().

ok tedu@ deraadt@


# 1.276 06-Dec-2014 mpi

If "machdep.lidsuspend" is not set, do not go back to sleep if the lid
is still closed when resuming. This allows docked laptop to be resumed
without opening them.

ok mlarkin@


# 1.275 23-Nov-2014 mlarkin

If a machine has no _LIDs, then none can be open. This diff fixes a problem
where machines without any _LIDs were going back to sleep after hibernate
due to the recent suspend-if-lid-closed-on-resume diff. Noticed on qemu,
which doesn't have any _LIDs.

discussed with deraadt


# 1.274 18-Nov-2014 krw

Nuke yet more obvious #include duplications.

ok miod@


# 1.273 08-Nov-2014 mlarkin

If resuming from sleep (zzz/ZZZ) and the lid is still closed, go back to
sleep. This prevents accidental lid flex or slight opening in a backpack
from waking the machine up and leaving it resumed (powered on).

ok deraadt@


# 1.272 26-Sep-2014 kettenis

Rework piglet and pig allocation. Currently the piglet gets allocated
deep down in the suspend path, where it is really hard to recover from
allocation failure. So allocate the piglet early on in the suspend path.
Also change the piglet and piglet allocation functions to use km_alloc(9)
instead of doing pmemrange magic. This removes a bunch of code which, in the
case of the piglet allocation, is broken since it results in a NULL pointer
dereference. Also switch the piglet allocation to not wait. If we can't
allocate 16MB of phys contig memory on a halfway modern machine we're almost
certainly under a lot of memory pressure and we're better off not trying to
hibernate anyway.

ok mlarkin@


# 1.271 25-Sep-2014 kettenis

Bring back rev 1.249. Now that mp_setperf() has been fixed, it should be
safe to restore perflevel when resuming. Should fix the issues people
see with the processor running at full speed heating up the machine while
the apm command reports that the CPUs are running at a low speed.

ok dcoppa@, deraadt@


# 1.270 23-Sep-2014 kettenis

Release the acpi lock around calls to wsdisplay_suspend() and
wsdisplay_resume(). The X server may adjust the display brightness just
before/after the VT switch when suspending/resuming. In fact, it may
already be blocked adjusting the display brightness for some reason
unrelated to the suspend request.

The VT switch request will eventually time out, but the delay is annoying
and the failed VT switch will lead to problems after resume.

Needed for a future update of the xf86-video-intel driver.

ok mlarkin@


# 1.269 19-Sep-2014 kettenis

Use config_suspend_all(9).


# 1.268 08-Sep-2014 kettenis

Clear the PME Status bit on PCI devices in response to Device Wake events.
Fixes the interrupt storm on the 2009 Mac mini with WOL enabled on its
nfe(4) interface.

ok mlarkin@


Revision tags: OPENBSD_5_6_BASE
# 1.267 20-Jul-2014 mlarkin

Support hibernating to softraid crypto volumes.

much help and ok from deraadt@


# 1.266 16-Jul-2014 mlarkin

Reenable hibernate RLE support and flush+zero all memory after unpack.


# 1.265 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.264 12-Jul-2014 kettenis

Add a function to drop all clean pages on the page daemon queues and call
it when we hibernate.

ok mlarkin@, miod@, deraadt@


# 1.263 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.262 11-Jul-2014 mlarkin

Flush the buffercache to 16MB on hibernate and restore its previous max
size (kern.bufcachepercent) on resume, for better hibernate performance.

ok beck@


# 1.261 10-Jul-2014 blambert

remove unneeded header

ok mlarkin@


# 1.260 01-Jul-2014 kettenis

Adding an additonal delay after each access to SystemMemory or SystemIO
during autoconf makes no sense.

ok deraadt@


# 1.259 23-Jun-2014 kettenis

SystemMemory is used to access memory mapped registers on some machines, so we
must use the bus_space(9) API to access it instead of memcpy(9). Also make
sure we properly align access to fields that need more than byte alignment.

ok guenther@


# 1.258 21-May-2014 mlarkin

Fix invalid / missing format specifiers in several dnprintf statements
(used only when ACPI_DEBUG is enabled)

From a diff on tech@ by Fabian Raetz, with a minor change by me, tested
on i386 and amd64 with ACPI_DEBUG enabled and disabled.


# 1.257 25-Apr-2014 mlarkin

Defer the free of the hibernate structures until later, when it is safe to
do so.

Freeing the hibernate structures too early leads to inadvertent calls into
splx when we aren't ready for them yet. Found when trapping splx calls in
the resume path.

discussed with deraadt@ at m2k14


# 1.256 13-Apr-2014 mlarkin

Fix a badly worded debug message that implied the entire machine was going
to sleep, instead of just the acpi thread going to sleep.


# 1.255 31-Mar-2014 mpi

Change the order of operations during a suspend/resume cycle and
call bufq_quiesce() after executing the DVACT_QUIESCE handlers.

This should be safe since no disk nor controller drivers have such
handler but it will allow us to detach sd(4) devices attached to a
USB bus.

Another benefit pointed out by kettenis@ is that drivers that need
to read a firmware from the disk should be able to do it at resume
time in a DVACT_WAKEUP handler.

ok kettenis@, deraadt@


# 1.254 13-Mar-2014 dlg

get rid of the assumption that the head of the alldevs list is the
"mainbus" device. this breaks when mpath is enabled because it
attaches before mainbus and therefore takes the head position.

have autoconf provide device_mainbus() which looks up mainbus_cd,
and use that instead.

discussed with deraadt who just wants mpath stuff to move forward
despite there being many ways to shine this particular turd.


Revision tags: OPENBSD_5_5_BASE
# 1.253 21-Feb-2014 deraadt

Do the lid control further on in the resume sequence, since this is
AML, and will do WAITOK operations. While cold, that is a bad idea.
We seem to be safe with respect to the spec, I do not see any event
debounce issues here.
ok kettenis, tested by many


# 1.252 20-Jan-2014 claudio

Instead of checking sc->sc_revision <= 1 check that the
fadt->hdr_revision <= 1. This is a better check since it checks
the version of the proper descriptor. This allows the alix APU
to reboot since those boards have currently a wrong sc->sc_revision.
Committing this now so that it can be tested easier.
With and OK jsg@ also fine by derradt@


# 1.251 24-Dec-2013 kettenis

If the FADT has its SMI_CMD set to zero, assume we're only ACPI-only hardware
and don't need to disable SMI ownership of the ACPI hardware registers.

ok mlarkin@


# 1.250 23-Dec-2013 kettenis

Revert previous commit. It makes resume hang on some MP systems running
GENERIC.MP.


# 1.249 16-Dec-2013 kettenis

Restore hw.setperf upon resume like we do for apm(4) on i386. Seems we need
to do this fairly late in the resume to avoid hangs.

ok deraadt@, mlarkin@


# 1.248 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


# 1.247 06-Nov-2013 mpi

Add support for Power Resources for Dx states and the necessary hook
for PCI devices. This hook should be called twice, before and after
changing the power state of a PCI device.

Before setting the device to the new state, the ACPI layer will notify
every power resources linked to the device for that state and make sure
they are turned "_ON". After changing the state of the device, it will
decrement the reference of every power resources linked to that device
for the old state and turn them "_OFF" if they are no longer referenced.

This fixes the no-USB after resume problem seen on various ThinkPad,
problem initialy diagnosed with Alexander Polakov.

ok kettenis@, deraadt@


Revision tags: OPENBSD_5_4_BASE
# 1.246 01-Jun-2013 mlarkin

Stop printing ACPI wakeup devices in dmesg after the 16th wakeup device.

Helps with VMware VMs that report hundreds of wakeup devices.

ok reyk@, deraadt@


# 1.245 31-May-2013 bluhm

Fix the build for a kernel without wd and pciide in its config.
OK deraadt@ tedu@


# 1.244 30-May-2013 deraadt

Enforce ca_activate tree-walks over the entire heirarchy for all events,
cleaning up some shutdown-hook related code on the way.
(A few drivers related to sparc are still skipped at kettenis' request)
ok kettenis mlarkin, tested by many others too


# 1.243 18-Apr-2013 deraadt

pin the acpi thread to the BSP, because AML and SMI cannot be trusted. I
was certain we were doing this already, but it turns out no...


Revision tags: OPENBSD_5_3_BASE
# 1.242 24-Dec-2012 guenther

In acpiioctl(), Return though the splx() path if suser() returns an error

ok deraadt@


# 1.241 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


# 1.240 04-Oct-2012 ehrhardt

Use information provided by ACPI to attach secondary PCI host bridges.

ok kettenis@


# 1.239 07-Sep-2012 kettenis

Add a new function called acpi_pci_min_powerstate() that returns the lowest
power state for a PCI device for the power state (currently S3 or S4) we're
transitioning into. This function is probably too simplistic; there is a lot
of (silly) complexity in the ACPI spec.

ok deraadt@


Revision tags: OPENBSD_5_2_BASE
# 1.238 13-Jul-2012 pirofti

Disable acpivideo(4) on ASUS laptops.

There are some models that panic whenever an event is received.
Some cope, some don't. In order to get this right acpiec(4) requires
major rework. Until then let machines boot.

Discussed with deraadt@.


# 1.237 11-Jul-2012 mlarkin

for now, restrict hibernate to root in the acpi ioctl kernel interface.
normal rules for controlling this interface through apmd still apply.

ok deraadt@


# 1.236 09-Jul-2012 deraadt

tiny little oops in untested hibernate block


# 1.235 09-Jul-2012 deraadt

clean up ioctl() case statements


# 1.234 09-Jul-2012 deraadt

acpi_disable_onegpe() is not used; ok pirofti


# 1.233 24-May-2012 kettenis

If _MIN > _MAX consider a WordBus resource to be invalid. Fixes a panic on
the IBM x3550-M4.

ok mikeb@


# 1.232 29-Mar-2012 mlarkin

Missing #ifdef in previous diff


# 1.231 29-Mar-2012 mlarkin

Prevent hibernation attempts on machines with unsupported disk controllers.
The same check is also performed later in the hibernate sequence, but by
then it's too late to cleanly unwind (presently).

ok deraadt


# 1.230 26-Mar-2012 deraadt

simplify conditionals in acpi_sleep_state()
ok mlarkin


# 1.229 26-Mar-2012 deraadt

hook in the hibernate request code; half of this diff is from mlarkin
ok mlarkin


Revision tags: OPENBSD_5_1_BASE
# 1.228 20-Sep-2011 deraadt

Very early in suspend while the clock is still ticking realtime,
save the clock back to the rtc. The zaurus already did this.
ok phessler miod kettenis


Revision tags: OPENBSD_5_0_BASE
# 1.227 02-Jul-2011 nicm

kqueue attach functions should return an errno or 0, not a plain 1. Fix
the obvious cases to return EINVAL and ENXIO.

ok tedu deraadt


# 1.226 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.225 16-Jun-2011 pirofti

Import acpitoshiba driver.

This is originally written by Hiroyuki Aizu, ported and integrated
in our tree by Javier Vazquez <javama4 AT gmail> with tweaks and
suggestions by me.

Import prodded by deraadt@ and the lack of testers on tech@.


# 1.224 27-Apr-2011 jcs

attach acpithinkpad to newer lenovo models like the x120e

ok deraadt@


# 1.223 22-Apr-2011 jordan

Fix uninitialzied variables and formatting strings (-Wxxx errors)


Revision tags: OPENBSD_4_9_BASE
# 1.222 02-Jan-2011 jordan

Re-add code for ACPI taskqueue, eliminates need for state vars in acpi softc
Serialize processing ACPI tasks for sleep/powerdown/GPE event
ok deraadt@


# 1.221 31-Oct-2010 guenther

Revert last commit: it breaks resume on Thinkpads
"then please back it out" deraadt@


# 1.220 26-Oct-2010 jordan

Add task queue for ACPI gpe and notify handlers
This fixes eject on Dell Latitude dock and an issue on the Dell Mini
battery update.
ok deraadt


# 1.219 07-Oct-2010 jordan

Create separate functions for enabling wake and runtime gpes.
Remove old suspend/resume gpewalk function
ok deraadt, mlarkin


# 1.218 05-Oct-2010 jordan

split acpi_enable_onegpe to separate enable/disable functions
ok deraadt


# 1.217 05-Oct-2010 jordan

Added acpi_disable_allgpes function to clear GPE status
ok deraadt


# 1.216 05-Oct-2010 deraadt

clean up acpi_softc use more


# 1.215 05-Oct-2010 deraadt

delete a bit of #if 0 code


# 1.214 05-Oct-2010 deraadt

hide use of our global acpi_softc pointer a bit better


# 1.213 29-Sep-2010 deraadt

grab the acpi thread lock very early in acpi thread startup; not that it
is likely that anyone will race us... but why be careless.
ok kettenis


# 1.212 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.211 29-Aug-2010 jasper

- add a check for FWRITE here too.

ok miod@


# 1.210 27-Aug-2010 kettenis

Make PCI Power Management optional, and only enable it when acpi(4) attaches.

ok deraadt@


Revision tags: OPENBSD_4_8_BASE
# 1.209 08-Aug-2010 kettenis

Close race between the acpi thread and wscons ioctls running in process
context. We might rip this out post-release and replace it with a solution
that serializes things in a more obvious way.

ok deraadt@, miod@ and "those crazy texans who ok anything"


# 1.208 08-Aug-2010 deraadt

Report the acpi sleep states when on a RAMDISK; ok krw


# 1.207 07-Aug-2010 kettenis

Enforce acpi register access rescrictions.

ok mlarkin@, deraadt@, tested by marco@


# 1.206 07-Aug-2010 deraadt

upon resume, notify all the acpiac and acpibat drivers to update
their status. some laptops do not do this automatically.
ok kettenis canacar


# 1.205 06-Aug-2010 marco

Always call _PSW on _LID devices that support it to enable lid open to
resume the machine. Conversely disable it when the machine wakes up.

Tested by several ok deraadt


# 1.204 06-Aug-2010 deraadt

As part of a revamp of the PM1/GPE code, I write this diff to look for
unmanaged EN & STS bits in the PM1 register at interrupt time and report
them. As a side effect this splits the STS acknowledgement into two writes
(for power, and sleep) instead of one. The printf that is added (to spot
unmanaged STS bits) has not yet been triggered as far as we know.

Before the "write to PM1 registers at the right offset" diff went in, this
was not neccessary. But newer thinkpads do not have a working soft power
button without this diff.

We have no idea why.
ok mlarkin kettenis


# 1.203 05-Aug-2010 deraadt

PM1 power button events were generating 'fake events' to acpibtn PWRB,
but if there is no such device these were being tossed. Instead, arrange
for all power button handlers to flag a softc variable which is processed
by the thread; so that one place will know to request a powerdown (psignal
to init)
ok marco mlarkin


# 1.202 05-Aug-2010 deraadt

rename a variable which is annoyingly poorly named


# 1.201 05-Aug-2010 mlarkin

Write to the correct offset for various ACPI pmreg registers. Writing to
the correct register does properly clear the wake status bits and fixed
event bits, which allows some machines to have more than 1 suspend cycle.

bug spotted by deraadt@

tested by deraadt@ and myself on a half-dozen machines and others on
hackers@

ok deraadt@, marco@


# 1.200 05-Aug-2010 deraadt

Fix the power button. The pm1 register is 16 bits in size.
My mistake, a while back.
tested by krw


# 1.199 04-Aug-2010 kettenis

Print supported sleep states.

ok marco@, phessler@, deraadt@


# 1.198 03-Aug-2010 mlarkin

Use the proper flag for re-enabling certain hardware events (power btn,
sleep btn). The (incorrect) flag prevoiously used coincidentally had the
same value, so this shouldn't cause any different behavior than before.

tested on a variety of machines (i386, amd64, sp, mp) - no behavioral
change seen

ok deraadt@


# 1.197 28-Jul-2010 marco

Make all gasio failures look and behave the same.


# 1.196 28-Jul-2010 mlarkin

Delay a little longer on suspend before giving up. Fixes machines that
sometimes don't suspend because the sleep transition takes too long
(symptoms like failing to sleep but the backlight stays on).

ok deraadt@


# 1.195 27-Jul-2010 deraadt

remove XXX; EC can now be attached early when needed; ok jordan


# 1.194 27-Jul-2010 deraadt

more brackets feels good


# 1.193 27-Jul-2010 deraadt

XXX annotate a block in acpi_enter_sleep_state() that is pretty worrying


# 1.192 27-Jul-2010 deraadt

horrific indentation that nearly made me puke


# 1.191 27-Jul-2010 mlarkin

Set the sleeping indicator light on machines that support it, in case SMI
doesn't do it for us. Shows the 'moon' LED on older Thinkpads when
sleeping.

Tested on T43p, W500, T510 by me and on X61s by deraadt@.

ok deraadt@


# 1.190 27-Jul-2010 jordan

Early initialization of acpiec if ECDT table exists
Fixes hang when booting thinkpads while docked
ok deraadt


# 1.189 26-Jul-2010 deraadt

remove merge error which was commited


# 1.188 26-Jul-2010 pirofti

Add support for Sony ACPI hotkeys via a new driver: acpisony(4).

Currently it only works for the suspend button and tries to do right for
the brightness events, but I haven't found a Sony laptop that like to do
right so far.

In the future I want to make the brightness keys work on all Sony's and
also add support for the zoom hotkeys and whatever other funky keys I can
find on those things.

Okay deraadt@.


# 1.187 22-Jul-2010 deraadt

pretty large cleanup of SMALL_KERNEL


# 1.186 21-Jul-2010 deraadt

in the acpi GPE handler, late ack level interrupts not edge interrupts.
Duh.


# 1.185 21-Jul-2010 deraadt

Merge the edge and level gpe handlers into one (now possible since the
gpe structure tells us whether the gpe is edge or level)


# 1.184 21-Jul-2010 deraadt

delete #if 0 code we are not going to use; ok marco


# 1.183 20-Jul-2010 deraadt

make the mask code for acpi_enable_onegpe() match other similar code


# 1.182 20-Jul-2010 deraadt

Handle the acpi interrupt controller with proper edge/level handling,
wrapped in spltty to avoid racing against the interrupt controller.
Repair the gpe bit masking code while there, and do operations in the
right order
ok kettenis mlarkin, help from jordan at figuring out the order of
operations.


# 1.181 20-Jul-2010 matthew

When we're preparing to go to sleep, only call bufq_restart() if other
preparations failed.

ok kettenis@, thib@, deraadt@


# 1.180 19-Jul-2010 deraadt

permit compilation with ACPI_DEBUG


# 1.179 19-Jul-2010 deraadt

there is no need for a seperate dsdt_softc pointer; dsdt does not have a
softc but is just a part of the single acpi driver.
ok jordan


# 1.178 19-Jul-2010 deraadt

acpi_delay() is no longer needed; ok jordan


# 1.177 18-Jul-2010 mlarkin

Clear event status on resume, to avoid going back to sleep/reboot/shutdown
immediately after resume. Tested by a bunch of people on hackers@ on
various hardware. Fixes at least Toshiba NB200 and Gateway LT3103u.

ok marco


# 1.176 13-Jul-2010 deraadt

rename some thread related interfaces so they are logical


# 1.175 10-Jul-2010 jordan

Verify _ADR bdf is correct (some systems return 0xffff)
Works on Dell x9xx and Studio 1555


# 1.174 09-Jul-2010 jordan

Clear the noise


# 1.173 08-Jul-2010 jordan

Add mapping for ACPI device to PCI bdf (match autoconf tree)
Simplify resource parsing function to use buffer argument
Convert namespace linked lists to use queue macros
ok marco@, deraadt@


# 1.172 06-Jul-2010 deraadt

Use spltty() to lock downcalls from apm against the information being
modified by the acpi thread.
ok kettenis


# 1.171 06-Jul-2010 deraadt

Don't use a workq to do the suspend, because it races aginst the acpi
thread. Instead, just tell the acpi thread to do the suspend for us.
This makes apmd & zzz work correctly. While here, have acpithinkpad
attempt to post the event to apm before prompting the suspend itself.
ok kettenis marco mlarkin


# 1.170 05-Jul-2010 mlarkin

Reset register SCI_EN on resume. The ACPI spec clearly states we don't have
to do this, but many machines don't, or forget to, which leads to all sorts
of problems on resume (broken battery stats, overheating, failure to resume
more than once, etc).

tested by many, ok marco@, deraadt@


# 1.169 01-Jul-2010 thib

do bufq quiesce again, since vnd/the bufq_done call in
biodone has been fixed.


# 1.168 01-Jul-2010 jordan

Backout recent AML changes, breaks on R210 and others


# 1.167 01-Jul-2010 jordan

Simplified aml_parse_resource call, cleanup code
ok marco


# 1.166 01-Jul-2010 jordan

Fix subordinate bus # for multi-root PCI buses
ok marco, kettenis


# 1.165 30-Jun-2010 thib

Disable/partially backout the bufq quiesce changes as this
is causing havoc with vnds and release must be buildable.


# 1.164 29-Jun-2010 deraadt

Fix ramdisks (new code was placed inside a SMALL_KERNEL chunk)


# 1.163 29-Jun-2010 jordan

Add support for mapping ACPI to PCI devices
ok kettenis, deraadt


# 1.162 29-Jun-2010 kettenis

Call bufq_quiesce() on suspend to stop all I/O and bufq_restart on resume.
Makes suspend/resume work much better while doing disk I/O.

ok mlarkin@


# 1.161 29-Jun-2010 tedu

60 is a more reasonable guess for number of minutes in an hour than 100.
spotted by TeXitoi. ok deraadt


# 1.160 27-Jun-2010 jordan

Add _acpi_matchhids routine, precursor to pci device match


# 1.159 27-Jun-2010 mlarkin

Clear ACPI fixed event status on resume (power buttons/etc) so that some
machines don't immediately resume after suspending to S3.

Tested by kettenis@ and I on 5 or 6 machines, deraadt@ "yeah, looks good"


# 1.158 27-Jun-2010 jordan

Cleaned up acpi_maptable, adds to table list directly


# 1.157 07-Apr-2010 deraadt

If we have done a wsdisplay_suspend(), and ACPI decides it does not want to
suspend, we must roll back by calling wsdisplay_resume() or the virtual
console switching will be locked forever
ok oga


# 1.156 07-Apr-2010 kettenis

Make suspend/resume work on MP machines (running an MP kernel). Joint work
from mlarkin@ and me, with some amd64 fixes thrown in by deraadt@

ok marco@, deraadt@, pirofti@, mlarkin@


# 1.155 31-Mar-2010 kettenis

Bring /dev/apm support in line with apm(4). Make the suspend button send
a suspend request event to apmd(8) instead of suspending immediately. Also
keep track of whether /dev/apm and /dev/apmctl are currently open, such that
we can still suspend immediately if apmd(8) isn't running.

ok deraadt@, marco@, pirofti@, jsing@, oga@


# 1.154 30-Mar-2010 oga

Prevent the apmd/x races for good.

When we hit suspend time, go through all wsdisplays on the system. if
they are in mode MAPPED, but not MODE_DUMBFB then if possible do a full
vt switch to a !mapped vt, and prevent switching back until resume time.
This has to be called from MD code because this involves userland
running so that X can run the vt switch signal handler. This way, any
case where we are using the "poke registers from userland" model, we
will not be on the hardware when we go down, so the kernel can actually
handle thing properly.

Tested on several acpi laptops (by kettenis@ and ian@), x40 (me and
beck@ at LEAST) and zaurus (me). Maybe others, but if so I forgot who at
this time..

Idea from deraadt somewhere over the Faroe Islands (I thought of a
similar thing myself a while ago). Much prompting from him. Ok and
comments miod@


# 1.153 25-Mar-2010 oga

Fix & vs. && in the kqfilter code.

Based on a comparison with the apm code.

ok deraadt@, kettenis@


Revision tags: OPENBSD_4_7_BASE
# 1.152 26-Nov-2009 mlarkin

Visual (and on some machines, audio) feedback while the machine is
resuming via calls to aml _SST method. This also makes the "moon" LED on
thinkpads to blink during resume and cease when resume is completed.

ok pirofti@


# 1.151 26-Nov-2009 deraadt

Toggle into "cold" when we we disable interrupts, because tsleeping
in the AML interpreter and context switching into userland processes
tends to sometimes pop the ACPI trampoline page out of our address
space. Makes lots more machines work.
ok kettenis mlarkin


# 1.150 24-Nov-2009 jsg

in the resume path evaluate _WAK before dealing with device drivers
like the spec says, not after.

ok mlarkin@


# 1.149 24-Nov-2009 deraadt

abort suspends on SMP systems until that gets written; ok mlarkin


# 1.148 23-Nov-2009 mlarkin

pci should return the result of it's children's suspend/resume calls back
to its parent.

handle suspend failure case and unwind if devices have an activate function
that returns failure.

ok deraadt@, kettenis@


# 1.147 23-Nov-2009 mlarkin

acpi* devices need to call their children on suspend/resume.
ok deraadt@


# 1.146 23-Nov-2009 pirofti

Remove ACPI_SLEEP_ENABLED checks.

This enables by default the suspend/resume paths in the kernel.

Okay deraadt@.


# 1.145 23-Nov-2009 deraadt

knf and spacing that are super annoying


# 1.144 23-Nov-2009 mlarkin

Hook up Theo's new children suspend/resume function, as well as more ACPI
suspend/resume glue.

ok deraadt@


# 1.143 26-Oct-2009 deraadt

Do not do strncmp comparisons on dv_xname because longer device names which
look similar could arrive in the future. Instead, compare directly against
dv_cfdata->cf_driver->cd_name
Issue originally spotted by miod


# 1.142 26-Oct-2009 jordan

Verify checksum+address when loading ACPI tables.
Some systems had invalid entries in RSDT/XSDT.
ok marco@


# 1.141 23-Jul-2009 cnst

New aibs(4) driver for ASUSTeK AI Booster (ACPI ATK0110) hardware monitoring,
with sensor state support through limits provided by the ACPI.

Tested on several ASUS motherboards kindly networked by Sam Fourman Jr.

ok deraadt marco jordan


Revision tags: OPENBSD_4_6_BASE
# 1.140 03-Jun-2009 pirofti

Import acpi power resource driver. This is needed for suspend/resume logic.

Okay jordan@ and marco@.


# 1.139 03-Jun-2009 pirofti

Add acpivideo support. This adds brightness support for all laptops
except thinkpads, they will use the acpithinkpad driver. The driver is
also hooked into wsconsole. So brightness can be adjusted via:

$ wsconsctl display.brightness=<percentage>

This is very helpfull on some laptops that have a nasty bios and get two
steps instead of one when pressing the brightness button.

Tested on various dell, fujitsu, acer, samsung and other laptops.

Okay marco@, miod@. Suggestions from kettenis@.
Lots of reviews and help from miod@, thanks!


# 1.138 03-Jun-2009 jordan

Added ejectable bay for IDE cdroms
ok marco@


# 1.137 30-Apr-2009 marco

Oga changed apm ioctls and forgot to grep. Temporary work around by hitting
the sleep button when we get any request. This interface will chnage at
some point.
ok oga


# 1.136 19-Apr-2009 krw

Call acpi_sleep_walk() when powering down just like when entering
any other sleep state. This tells acpi wake devices to not wake up
the box until manual powering up resets their status. Makes my Dell
GX520 stay powered off in response to 'halt -p'.

In snaps for a while.

ok marco@ (several times) deraadt@


# 1.135 17-Apr-2009 pirofti

Add device ranking support for future sleep/wake developments.

okay marco@


# 1.134 11-Mar-2009 jordan

Cleanup.. use aml_evalinteger instead of aml_evalname
ok marco@


# 1.133 10-Mar-2009 jordan

Fix cut/paste error.. call _BFS not _PTS
ok marco@


Revision tags: OPENBSD_4_5_BASE
# 1.132 21-Feb-2009 marco

overzealous cut/paste; found by Vladimir Kirillov <proger@uaoug.org.ua>


# 1.131 20-Feb-2009 marco

Oops didn't mean for Debugger() to remain there.


# 1.130 19-Feb-2009 marco

suspend/resume bits so that we can develop this in tree. This is disabled.
code from mlarkin and me
help from art,toby,jordan and several others
ok jordan, go for it deraadt


# 1.129 10-Feb-2009 jordan

Fix null pointer for shutdown if acpi not enabled
ok marco@


# 1.128 04-Feb-2009 kettenis

Some systems that claim to be ACPI 2.0 apparently don't provide an XSDT
address. Make the code fall back on the RSDT on those systems.

From Joerg Niendorf <p3y80@internode.on.net>. Fixes PR 5986.


# 1.127 17-Jan-2009 krw

A couple of stray malloc/memset -> malloc(M_ZERO), a bit of knf and
lose a couple of unnecessary casts. No functional change.

Feedback from kettenis@

ok kettenis@ marco@


# 1.126 06-Nov-2008 marco

Collapse all sanity checks of hids into one function.

Originaly from jordan


Revision tags: OPENBSD_4_4_BASE
# 1.125 02-Jul-2008 fgsch

add acpivideo. for now almost an empty skel so others can work on
suspend/resume. eventually it will also manage output switching and
brightness where supported. prodded by marco@


# 1.124 11-Jun-2008 marco

Fix $OpenBSD$ for once and for all


# 1.123 08-Jun-2008 deraadt

aaa_dev was being set from an uninitialized variable; ok toby


# 1.122 07-Jun-2008 marco

Kill ACPIVERBOSE. It was never used and is worthless to boot.

comments jmc
ok toby


# 1.121 06-Jun-2008 marco

Shrink acpi some more for floppies and while here re-enable acpi on the
floppies. Fixes crazy sony vaio kettenis has.

tested by deraadt
ok deraadt jordan kettenis


# 1.120 01-Jun-2008 marco

Fix \n nit in SMALL_KERNEL.


# 1.119 01-Jun-2008 marco

Re-enable new parser. Lots of testing by various folks. Fits all media
now too.

ok kettenis deraadt


# 1.118 16-May-2008 dlg

back out jordans new parser.

it didnt have enough oks from the right people, it did break machines
that were previously working, and it bloated the RAMDISKS and install
media beyond the constraints of the media.

requested by deraadt@


# 1.117 14-May-2008 jordan

Adding new ACPI Parser code
Old guts of code still exists, needs to be torn out next
ok marco@
ok brad@


# 1.116 27-Apr-2008 jcs

add acpithinkpad(4), an acpi support driver for ibm/lenovo
thinkpads.

provides functionality that the bios did on older, non-acpi
thinkpads like respond to screen brightness keys and toggle the
bluetooth device on and off.

ok deraadt@ and marco@


# 1.115 24-Apr-2008 jsing

Introduce acpiasus(4), a driver for the ACPI based hotkeys found in many
ASUS laptops (including the ASUS EeePC) - largely based on NetBSD's
asus(4) driver. On the ASUS EeePC this allows us to enable/disable
wireless, change screen brightness and use the volume keys.

ok jsg@, weingart@


# 1.114 12-Apr-2008 kettenis

Get rid of acpi_s5 global variables; simply send SIGUSR2 instead of SUGUSR1
to tell init(8) to power down the machine.

ok krw@


Revision tags: OPENBSD_4_3_BASE
# 1.113 21-Jan-2008 jsg

Check against RESET_REG_SUP in FADT before doing the acpi reset dance
after all, not doing so appears to break more machines than it fixes.

ok marco@


# 1.112 05-Jan-2008 kettenis

Never attach more acpicpu(4) devices than cpu(4) devices. Always attach
the first acpicpu(4) device on non-MULTIPROCESSOR kernels, regardless of its
ACPI processor ID.

ok mikeb@


# 1.111 05-Dec-2007 deraadt

Hang acpi(4) below bios(4) instead of mainbus(4). This lets us move acpi(4)
before pcibios(4), and then.... if acpi(4) attaches, skip pcibios(4) since
it messes the machines up. Fixes claudio's HP dl320 G5, and almost assuredly
others.
ok kettenis


# 1.110 05-Dec-2007 deraadt

repair acpiopen/acpiclose and friends to handle the /dev/apmctl interface,
so that apmd(8) can actually work correctly against acpi(4), and thus,
even do the fancy -C/-A stuff; also tested by jmc


# 1.109 02-Dec-2007 fgsch

fix the trailing space for real now.


# 1.108 02-Dec-2007 fgsch

avoid trailing spaces on output; marco@ ok.


# 1.107 27-Nov-2007 kettenis

Fix off-by-one that made us attach acpicpu0 and acpicpu1 on GENERIC kernels.

ok marco@


# 1.106 25-Nov-2007 jsg

When available and ACPI is enabled use the ACPI reset method
to reboot. This is required for some quirky sis machines
and likely others to reboot.

Tested by many. 'commit early commit often' deraadt@


# 1.105 19-Nov-2007 deraadt

sigh, amd64 still uses that stupid acpi_enabled variable...


# 1.104 19-Nov-2007 deraadt

softc's come initialized to 0, you know...


# 1.103 19-Nov-2007 deraadt

store revision; from jsg


# 1.102 19-Nov-2007 kettenis

Initialize sc_facs earlier.

ok deraadt@, beck@, canacar@


# 1.101 16-Nov-2007 deraadt

fix the bus_space #define nightmare, so that amd64 and i386 are much more
uniform. as a result shared code like acpi needs less #ifdef's
ok marco kettenis


# 1.100 15-Nov-2007 deraadt

remove the #ifdef ACPI_ENABLE. the EC codepath is either safe enough (and
will be improved), or it is unsafe because AML assumes it must be there
ok almost everyone


# 1.99 15-Nov-2007 mikeb

Make acpicpu(4) attach to the existing CPUs only.

Problem reported by form@. Fix idea by kettenis@.
Lots of help from deraadt@. Tested by deraadt@, form@ and me.

ok deraadt marco


# 1.98 14-Nov-2007 deraadt

spacing


# 1.97 12-Nov-2007 deraadt

correct : vs , in attach-time messages


# 1.96 06-Nov-2007 deraadt

all ACPI_ENABLE kernels need acpi_interrupt()


# 1.95 06-Nov-2007 deraadt

move acpi_read_pmreg() and acpi_write_pmreg() out of SMALL_KERNEL so
that acpi ramdisks can compile (obvious solution)


# 1.94 05-Nov-2007 canacar

Establish the acpi interrupt earlier. Fixes problems with some
GENERIC.MP kernels not getting acpi interrupts. This is a
workaround until the real issue with late establishment is fixed.
ok weingart@, beck@, gwk@


# 1.93 03-Nov-2007 jordan

Added support for displaying wakeup devices
ok beck@,weingart@,gwk@


# 1.92 11-Oct-2007 kettenis

When walking the tree yo find _PRT methods, check whether the device is
actually enabled and functioning. Similar to what we do for _INI.

ok weingart@, beck@


# 1.91 08-Oct-2007 krw

More simple memset(,0,) -> M_ZERO changes. In this batch move to
size(*p) as the first malloc() parameter where p is declared locally
and thus easy to check. Add M_ZERO to gpe_table allocation in acpi.c
even though there is no obvious bzero or memset nearby.


# 1.90 13-Sep-2007 mk

Remove unnecessary memset() (see aml_evalnode(9)) and a bit of KNF.

ok toby


# 1.89 13-Sep-2007 weingart

Implement ACPI 6.5.1 spec tree walk for _STA and _INI.
Fixes mk's laptop. No regressions so far (thank you to the
testers).

ok gwk@, mk@, marco@


Revision tags: OPENBSD_4_2_BASE
# 1.88 17-Apr-2007 mk

Now that acpidock no longer hangs machines during boot (at least
according to the reports I've been getting) it should be safe to enable
it again, so remove the #if 0 stuff around it.

ok marco


# 1.87 11-Apr-2007 jordan

Added changes for C-State
ok marco@


# 1.86 26-Mar-2007 marco

Calculate minutes left more accurately.

From giovanni <qgiovanni@gmail.com>


Revision tags: OPENBSD_4_1_BASE
# 1.85 03-Mar-2007 mk

Stop looking for acpidock until we fix that stuff that breaks all pre-60
series ThinkPads with acpidock(4) enabled.


# 1.84 22-Feb-2007 marco

We were getting no interrupts in .MP. There are 2 fixes; one in
acpi_machdep.c and move the interrupt establishment down after ioapic
has been initialized.

ok jordan


# 1.83 21-Feb-2007 kettenis

Make an acpi-enabled GENERIC.MP work on systems that only support PIC mode.

ok marco@


# 1.82 19-Feb-2007 jordan

Added acpi_map_address function for mapping GAS/IO registers
ok marco@


# 1.81 18-Feb-2007 jordan

Added dynamic perfect hash for opcodes
ok marco@


# 1.80 18-Feb-2007 marco

Shave off another K pre compression.
Shuffle SMALL_KERNEL functions around so that we only have a single #ifndef


# 1.79 17-Feb-2007 marco

Add SMALL_KERNEL throught acpi to make it fit on boot media.
ok deraadt


# 1.78 13-Feb-2007 marco

Add smarter _INI code. This helps with some dissapearing devices.

ok kettenis jordan


# 1.77 31-Jan-2007 gwk

Introduce acpi_hasprocfvs thats "ACPI Has Processor Frequency and Voltage
Scalling" a global flag set by acicpu attach to indicate that ACPI is
handling fvs through the _PSS objects. This will fix disappearing
EST/powernow when booting kernels with acpi enabled, as acpicpu isnt
compiled into GENERIC yet. Also in cases where acpicpu is built in and
legacy EST/powernow work but acpi is missing the _PSS object, the legacy
routines will be used.

tested by dim@, and Nick Nauwelaerts <nick AT nauwelaerts.net>
ok marco@, dim@


# 1.76 22-Jan-2007 mk

Actually look for docking stations by looking for _DCK nodes.

ok marco


# 1.75 02-Jan-2007 marco

On some systems EC must be attached as the first device. Move EC from
generic _HID devices to its own routine to force first attach. Bug found
and debugged by joris@

Code mostly written by joris@


# 1.74 26-Dec-2006 marco

Add polling to devices that require it such as acpibat and acpitz.
Use the same tsleep/wakeup mechanism as the interrupt code to keep it non-
concurrent.


# 1.73 21-Dec-2006 deraadt

even more knf love


# 1.72 21-Dec-2006 marco

Remove some leftover acpiec gpe/interrupt code.


# 1.71 20-Dec-2006 deraadt

oops, name already used


# 1.70 20-Dec-2006 deraadt

rename __acpi_enable_gpe(); ok marco


# 1.69 20-Dec-2006 deraadt

acpi_unmap_pmregs() is not used, ok marco


# 1.68 19-Dec-2006 deraadt

simplify some low-level methods substantially (shrinking code); ok marco jordan


# 1.67 06-Dec-2006 mk

malloc(..., M_NOWAIT) with no NULL check is bad. If allocation fails,
complain, clean up, and bail out of attach.

input and ok gwk


# 1.66 28-Nov-2006 jordan

Fixed clearing arg for acpi_init_pic
ok kettenis@


# 1.65 27-Nov-2006 kettenis

Don't spam dmesg.


# 1.64 27-Nov-2006 jordan

Fixes for acpiprt routing
Post-parse fixup for namerefs
ok kettenis@


# 1.63 25-Nov-2006 marco

Don't handle acpiev events inside the gpe handler since this causes deadlocks.

ok jordan@


# 1.62 15-Nov-2006 kettenis

Hook up acpi apic interrupt routing support to generic acpi code.

ok jordan@


# 1.61 02-Nov-2006 marco

Kill acpiec attach function and add its functionality to the generic _HID
device attach

prodded and ok jordan


# 1.60 02-Nov-2006 marco

Fix comments to reflect reality.


# 1.59 19-Oct-2006 marco

Replace ACPI ioctls with APM ioctls. This makes ACPI talk the same protocol
as APM therefore userspace can now retrieve power information in the same
fashion as all other apm arches (zaurus, macppc, i386 etc).

To use force the use of acpi device like so: apmd -f /dev/acpi

ok jordan@


# 1.58 19-Oct-2006 jordan

Fixed GPE debug levels
Fixed ref counter on temporary bufferfields
DSDT parsing fully working on IBM Thinkpad
ok marco@


# 1.57 15-Oct-2006 jordan

Removed old GPE handler code remnants
ok marco@


# 1.56 12-Oct-2006 jordan

Massive ACPI changes
Fixed ACPI leaks
new ACPI API
ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.55 30-Jun-2006 gwk

KNF, No functional change,
"go at it" marco@


# 1.54 30-Jun-2006 jordan

Support for calling Embedded Controller via new GPE interface
ok marco@


# 1.53 30-Jun-2006 jordan

Added new code for GPE handling
Also works with ACPIEC; need to fixup acpiec.c to remove original interrupt handler
ok marco@


# 1.52 30-Jun-2006 jordan

Added offset argument for acpi read/write pmreg routines (new GPE code)
ok marco@


# 1.51 31-May-2006 todd

do not call acpi_powerdown() unless acpi has successfully initialized
ok gwk@ canacar@


# 1.50 29-May-2006 canacar

Add embedded controller support.
ok marco@ deraadt@ testing & ok gwk@


# 1.49 19-May-2006 canacar

Minimal thermal zone driver. Monitors thermal zone temperature,
shuts down the system if the 'critical temperature' is reached.
ok marco@


# 1.48 11-Apr-2006 marco

KNF.


# 1.47 11-Apr-2006 gwk

Add acpi_resume(struct acpi_softc *sc), which calls some AML methods,
detected at attach time, currently unused.

ok marco@


# 1.46 11-Apr-2006 gwk

Discover some ACPI sleep related methods, and call them at the
appropriate time in acpi_enter_sleep_state().

ok marco@


Revision tags: OPENBSD_3_9_BASE
# 1.45 26-Feb-2006 marco

Add acpicpu as a device.

ok jordan@


# 1.44 23-Feb-2006 marco

remove dead function.

ok jordan@


# 1.43 22-Feb-2006 marco

Print out all tables it found during autoconf. We'll need this data in
the future.


# 1.42 22-Feb-2006 marco

Make dmesg less verbose.

ok jordan.


# 1.41 22-Feb-2006 jordan

Added parsing of SSDT tables
ok marco@


# 1.40 22-Feb-2006 jordan

Added new aml_register to notify by device id
Moved powerdown code to button device
ok marco@


# 1.39 21-Feb-2006 marco

Readjust debug levels that crept in too low over the last few days.
KNF


# 1.38 21-Feb-2006 marco

Rig the power button so that the machine shuts down correctly.


# 1.37 20-Feb-2006 marco

Add acpibtn device. Remains disabled for now.


# 1.36 19-Feb-2006 jordan

Added code for aml_notify GPE notification callback
ok marco@


# 1.35 19-Feb-2006 grange

Fix acpi_enter_sleep_state() by replacing current ugly hack
with mostly correct sleeping state transition code.

ok brad@ marco@


# 1.34 19-Feb-2006 marco

jordan:
Enable interrupts.
Added peripheral code to make this work.

marco:
Replace softintr and other types of interrupts with a handler with context.
Fixed interrupt magic for GPEs.
Some KNF

This is work done by jordan and I but has to be a single commit.


# 1.33 17-Feb-2006 marco

Remove earlier introduced delays. We really only need them during autconf.

ok jordan@


# 1.32 17-Feb-2006 marco

Add a generic delay function as per the ACPI spec.

from jordan@


# 1.31 17-Feb-2006 marco

Use tsleep instead of delay when !cold.


# 1.30 17-Feb-2006 marco

Argh, we need delays between reads/writes. The backend reads (i2c most likely)
are so slow that values come back as 0 sometimes. We have to do this
despite the spec saying we shouldnt have to. Guess having a faster
parser than others can hurt sometimes.

Idea from jordan.


# 1.29 17-Feb-2006 jordan

Fixed code for handling _INI
Rearranged debug statements
ok marco@


# 1.28 16-Feb-2006 jordan

Added stub for calling _INI
Fixed aml_dowhile for NULL return
ok marco@


# 1.27 16-Feb-2006 jordan

Fixed parser/evaluator to retrieve battery info+state
Cleanup of parser
Dell laptops return battery status
ok marco@


# 1.26 06-Feb-2006 brad

fix a typo so this will compile on amd64.


# 1.25 04-Feb-2006 marco

When ACPI_ENABLED wasn't set but the devices were it had an unknown variable.

Pointed out by ian@.


# 1.24 03-Feb-2006 jordan

Rewrote aml parser yet again...
Fixed bus_space_map for i386/amd64
Added _BST and _BIF to acpibat to get status/info
ok marco@


# 1.23 20-Jan-2006 jordan

More evaluator changes
Removed old evaluator code
Fixed _HID value
ok marco@


# 1.22 19-Jan-2006 jordan

Added support for read/write of PCI Config space
Help jason@
ok marco@


# 1.21 18-Jan-2006 jordan

Added new evaluation routines
Added helper methods for buffer/field evaluation
Fixed broken indentation on dsdt.c

ok marco@


# 1.20 17-Jan-2006 jordan

Major overhaul of the aml parser. Evaluater temporarily disabled.

ok marco@


# 1.19 06-Jan-2006 grange

Check for ACPI control availability only if ACPI_ENABLE defined.


# 1.18 06-Jan-2006 grange

Informative error messages on attach, prettier dmesg.


# 1.17 06-Jan-2006 grange

printf -> dnprintf conversion for debug messages.


# 1.16 05-Jan-2006 grange

Find SLP_TYPx values and store them for future use in
acpi_enter_sleep_state().


# 1.15 05-Jan-2006 grange

Random KNF and cleanup


# 1.14 05-Jan-2006 grange

Provide symbolic names for the acpi sleeping states and use it.


# 1.13 28-Dec-2005 marco

Moved DEVNAME to a acpidev.h

ok jordan@


# 1.12 18-Dec-2005 sturm

add back comment about wrong code

ok marco


# 1.11 16-Dec-2005 marco

Remove test for kbd_reset. Prompted deraadt@, ok jordan@


# 1.10 16-Dec-2005 jordan

Added hooks for powerdown/power button press


# 1.9 16-Dec-2005 jordan

Cleaned up common power management register i/o
Added enter sleep state ioctl
Hooked power button interrupt
ok marco@


# 1.8 16-Dec-2005 marco

Remove autoconf chatter


# 1.7 16-Dec-2005 jordan

Moved DSDT AML parsing into parent ACPI driver
ok marco@


# 1.6 14-Dec-2005 marco

Add AC device glue.


# 1.5 14-Dec-2005 marco

Adjust debug printing levels to something more sane.


# 1.4 13-Dec-2005 marco

Add glue to attach devices to apci.


# 1.3 07-Dec-2005 marco

KNF


Revision tags: OPENBSD_3_8_BASE
# 1.2 10-Jul-2005 grange

Spacing cleanup.


# 1.1 02-Jun-2005 tholo

Start on a basic ACPI framework -- does not do much more than read out the
ACPI tables into kernel memory and attach ACPI and HPET timers currently.

In order to test this code, enabling the devices in GENERIC as well as
the ACPI_ENABLE option is needed. This code does not do any thermal
control yet, so this should be done with care depending on the platform.

In the tree so more people can contribute to making this more fully
featured.

Ok niklas@ grange@ tedu@


# 1.406 26-Jan-2022 kettenis

An ACPI device needs to be both present and enabled for it to function.
So only attempt to attach hardware that has both bits enabled. This fixes
an issue where com(4) would attach for a disabled serial port leading to
misdetection of the hardware variant and a subsequent hang when /etc/rc
runs ttyflags -a.

ok anton@, deraadt@


# 1.405 12-Jan-2022 patrick

Make acpi_getpropint() return uint64_t, as ACPI integers are in fact that
wide and some _DSD properties depend on it.

ok kettenis@


# 1.404 04-Jan-2022 patrick

acpi_getprop() needs to actually make sure that we're looking at the
correct property. While there adjust acpi_getpropint() as well to
increase similarity with acpi_getprop().

ok kettenis@


# 1.403 16-Dec-2021 anton

Attach com over acpi on amd64. Some hardware uses a different interrupt
assignment compared to the the legacy one supported by com over isa.
This causes the console to halt once userland takes over as no
interrupts are received. The actual address and irq can be read from
ACPI, kettenis@ already added support for arm64 which paved the way for
amd64.

Some consoles that previously attached over isa are now expected to
attach over acpi.

Thanks to patrick@ for testing on arm64.

ok kettenis@


# 1.402 07-Dec-2021 kettenis

Fix parsing of SR_IRQ resource descriptors.

ok patrick@, anton@


# 1.401 02-Nov-2021 deraadt

knf


# 1.400 30-Oct-2021 deraadt

Change hw.perfpolicy=auto by default, at startup. If the system has AC
power connected (default is yes when no driver differentiates) then default
to 100% performance. On battery, use the existing auto algorithm (which is
admittedly somewhat unrefined).
This change overrides the system/BIOS speed and puts OpenBSD in control.
As this happens very early during boot, besides speedups in all usage usage
patterns, some surprises: unhibernate and sysupgrade times are cut in half.
note: on a few architectures, the setperf fn pointer is changed late, and
thus the auto algorithm stops timeing out. kettenis and i will look for
a solution.
in snaps for more than a week.
ok kettenis


Revision tags: OPENBSD_7_0_BASE
# 1.399 20-Jul-2021 mlarkin

Fix trailing whitespace in a few places.

No code change.


# 1.398 09-May-2021 kettenis

Some x86 machines advertise the "hardware reduced" ACPI feature, advertise
S4 and S5 support, but fail to populate the SLEEP_CONTROL_REG and
SLEEP_STATUS_REG descriptions in the FADT. An example of such a machine
is the ASUS Zenbook 14 UM433DA. Any attempt to powerdown the machine
will result in a kernel crash. It turns out that using the legacy ACPI PM
registers works fine on this machien. So fall back on those registers
if SLEEP_CONTROL_REG and/or SLEEP_STATUS_REG aren't provided.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_9_BASE
# 1.397 15-Mar-2021 patrick

Add acpi_iommu_device_map(), which replaces the DMA tag with one that
is blessed with IOMMU magic, if available. This is mainly for arm64,
since on amd64 and i386 the IOMMU only captures PCIe devices, as far
as I know, which uses the pci_probe_device_hook(). This though is for
non-PCI devices attached through ACPI.

ok kettenis@


# 1.396 07-Mar-2021 patrick

Pass standard DMA tag to acpi(4) table drivers.

ok kettenis@


# 1.395 27-Dec-2020 deraadt

acpi_map_address() cannot be wrapped by SMALL_KERNEL anymore, as
it is used by acpihpet.c


# 1.394 25-Dec-2020 visa

Refactor klist insertion and removal

Rename klist_{insert,remove}() to klist_{insert,remove}_locked().
These functions assume that the caller has locked the klist. The current
state of locking remains intact because the kernel lock is still used
with all klists.

Add new functions klist_insert() and klist_remove() that lock the klist
internally. This allows some code simplification.

OK mpi@


# 1.393 17-Dec-2020 kettenis

Fix some issues with referencing named ACPI nodes from Packages.
These references need to be resolved at runtime rather than when
they're parsed such that they pick up the right values for those nodes
which can be changed when for example _INI methods run.

The current approach is to replace these reference with a string that
names the node in question. The problem with that is that packages
can also contain normal strings. Which means that we need special
code that depends on the context where the package is used.

This diff takes a different approach by retaining a reference when
parsing. Code that uses a package will need to resolve this reference
but there is no ambiguiety anymore.

ok patrick@


# 1.392 05-Dec-2020 kettenis

Add support for the _EVT method to the GPIO support code.

ok patrick@


Revision tags: OPENBSD_6_8_BASE
# 1.391 27-Aug-2020 jmatthew

Reorder the acpi attach process so we look at Processor() CPU nodes
after Device() ones, since we should prefer the newer node type.
If we see any Device() nodes, don't attach acpicpu(4) to any Processor()
nodes if they're also present. This also makes acpitz(4) and
acpipwrres(4) devices attach slightly later.

ok kettenis@, also tested by jmc@


# 1.390 16-Aug-2020 gnezdo

Remove sc_maxgpe from acpi_softc which was never read from

Remove a dead conditional from acpi_init_gpes while there.

ok jcs@


# 1.389 21-Jul-2020 deraadt

acpi can use IPL_BIO (a low interrupt) since it only enqueues operations for
later processing. The use of a high interrupt will predate suspend/resume
efforts, we had to redesign acpi to be non-reentrant obviously
discussed with kettenis, in snaps for more than a week


# 1.388 24-Jun-2020 cheloha

kernel: use gettime(9)/getuptime(9) in lieu of time_second(9)/time_uptime(9)

time_second(9) and time_uptime(9) are widely used in the kernel to
quickly get the system UTC or system uptime as a time_t. However,
time_t is 64-bit everywhere, so it is not generally safe to use them
on 32-bit platforms: you have a split-read problem if your hardware
cannot perform atomic 64-bit reads.

This patch replaces time_second(9) with gettime(9), a safer successor
interface, throughout the kernel. Similarly, time_uptime(9) is replaced
with getuptime(9).

There is a performance cost on 32-bit platforms in exchange for
eliminating the split-read problem: instead of two register reads you
now have a lockless read loop to pull the values from the timehands.
This is really not *too* bad in the grand scheme of things, but
compared to what we were doing before it is several times slower.

There is no performance cost on 64-bit (__LP64__) platforms.

With input from visa@, dlg@, and tedu@.

Several bugs squashed by visa@.

ok kettenis@


# 1.387 10-Jun-2020 jca

Give userland a consistent battery and A/C state after resume

Previously we refreshed the state of acpiac(4) and acpibat(4)/acpisbs(4)
by queueing fake ACPI events to be processed by the acpi thread.
acpibat(4) using ACPIDEV_POLL meant that its status could be refreshed
first, resulting in an APM_POWER_CHANGE event sent to userland before
the state of acpiac(4) was up to date. Because of this, apmd(8) could
see A/C unplugged when it was in fact plugged, possibly leading to
a wrong decision.

Instead of using fake ACPI events, refresh the status of acpiac(4) and
acpibat(4)/acpisbs(4) right away at DVACT_WAKEUP time. To reuse the
existing refresh code of those drivers, make their ACPI *_notify()
handlers responsible for sending APM_POWER_CHANGE events instead. Also
queue an APM_POWER_CHANGE event right after resume to retain existing
behavior.

This fixes the consistency of data shown to userland, and makes those
drivers less special.

Initially discussed last year (!) with mpi@ at Miod's place.
Feedback and ok jcs@, deraadt@ agrees with the use of DVACT_WAKEUP here.


# 1.386 29-May-2020 deraadt

dev/rndvar.h no longer has statistical interfaces (removed during various
conversion steps). it only contains kernel prototypes for 4 interfaces,
all of which legitimately belong in sys/systm.h, which are already included
by all enqueue_randomness() users.


# 1.385 14-May-2020 kettenis

Bring back the acpi(4) changes. This time around only use the ACPI _CRS
information on ACPI 5.0 and later.

ok krw@, patrick@


# 1.384 11-May-2020 jca

More accurate remaining power reporting with disparate batteries

Overall remaining power was computed as the average of the remaining
power of each battery, in percents. This doesn't work with batteries of
different capacity, so use the total capacity and remaining power
instead.

Reported by lidstah, successful tests by lidstah and benno@, ok kettenis@


# 1.383 08-May-2020 kettenis

Move parsing of _CRS into acpi(4) and pass the parsed address and interrupt
information in struct acpi_attach_args.

ok mpi@


Revision tags: OPENBSD_6_7_BASE
# 1.382 14-Apr-2020 kettenis

Implement acpi_getprop(), the ACPI equivalent of OF_getprop().

ok patrick@


# 1.381 12-Apr-2020 kettenis

Add support for the _CCA method. This method indicates whether DMA is
cache-coherent or not. To implement this, acpi(4) gets two bus_dma tags
and passes the appropriate one when attaching devices based on _CCA.
On i386/amd64, where for all practical purpose DMA is always cache-coherent,
the two tags are the same. But on arm64 they are distinct.

ok patrick@


# 1.380 07-Apr-2020 visa

Abstract the head of knote lists. This allows extending the lists,
for example, with locking assertions.

OK mpi@, anton@


# 1.379 03-Apr-2020 mpi

Fix incoherencies of errors returned by various *kqfilter routines.

EOPNOTSUPP is returned when there's no "filterops" for a given fd.
EINVAL when the requested filter isn't supported by the "filterops".
ENXIO means the underlying device is no longer valid/present.

ok visa@


# 1.378 20-Feb-2020 visa

Replace field f_isfd with field f_flags in struct filterops to allow
adding more filter properties without cluttering the struct.

OK mpi@, anton@


# 1.377 31-Dec-2019 visa

Use C99 designated initializers with struct filterops. In addition,
make the structs const so that the data are put in .rodata.

OK mpi@, deraadt@, anton@, bluhm@


# 1.376 31-Dec-2019 mpi

Convert infinite sleeps to {m,t}sleep_nsec(9).

ok kn@


# 1.375 31-Dec-2019 jsg

include sys/sensors.h for sensor use
don't depend on indirect inclusion from acpidev.h


Revision tags: OPENBSD_6_6_BASE
# 1.374 07-Sep-2019 kettenis

Revert acpipci(4) changes. Too many systems are broken. We'll try again
after 6.6 as been released. The acpireg.h change stays behind.


# 1.373 28-Aug-2019 kettenis

Use ACPI information to attach PCI busses like we do on arm64. There are a
few additional quirks though, and attaching the PCI busses is delayed to
replicate the existing code more closely. That may be changed in the
future. Also tweak how we handle MSI support and respect to ACPI flag
that says we shouldn't attempt to use MSIs.

Some fallout is expected.

ok patrick@


# 1.372 27-Aug-2019 deraadt

On amd64 the bootup and resume paths for SP/BP/SP were not performing
msr/pat, clock-startup, firmware upgrade, fpu initialization, and cpu
soft-state initialization in the same order. That kind of chaos is
crazy and fragile. Improve the acpi resume call sequence and unify order
of the operations.


# 1.371 02-Jul-2019 jcs

acpi_set_gpehandler: turn edge arg into flags, add support for
passing GPE_DIRECT which directly executes the supplied callback and
clears a level interrupt rather than going through acpi_taskq which
can be slow when other driver callbacks are in the queue.

This should not be used for GPE handlers that need to do anything
ACPI-related.

ok kettenis


# 1.370 10-Jun-2019 kettenis

Print proper ACPI version number.

ok mlarkin@


# 1.369 08-Jun-2019 kettenis

When establishing the mapping between ACPI device nodes and PCI devices,
skip PCI host bridges and devices that aren't there.

ok patrick@


# 1.368 07-Jun-2019 kettenis

Introduce acpi_getsta() and use it to eliminate some duplicated code.

ok patrick@


# 1.367 12-May-2019 kettenis

Make sure the extended PM register descriptions actually exist before
using them. Fixes machines with ancient ACPI 1.0 (such as recent
hypervisors).

ok deraadt@


# 1.366 12-May-2019 kettenis

Add support for mapping ACPI PM registers using the "extended" gasio
representation from the FADT. Mostly fixes the Lanner NCA-1510.

ok lteo@, mlarkin@, deraadt@


# 1.365 11-May-2019 lteo

Fix incorrect error message.

ok kettenis@


# 1.364 08-May-2019 tedu

free size. the allocation is the header plus the length.


Revision tags: OPENBSD_6_5_BASE
# 1.363 04-Apr-2019 kettenis

Avoid potential null-pointer dereference. Found by Kent R. Spillner.

ok deraadt@, jsg@


# 1.362 20-Jan-2019 jsg

fix SMALL_KERNEL build after changes in rev 1.361
reported by naddy@ ok deraadt@


# 1.361 19-Jan-2019 tedu

Add a pwraction sysctl that controls what the power button does on acpi.
By default, nothing changes -- shutdown is initiated. But allows turning
power button into a sleep button if desired.
(grudging) ok from a few parties


# 1.360 26-Oct-2018 kettenis

Add acpipci(4) on amd64. For now this only calls the PCI-specific _OSC
method to let the ACPI implementation know what features we support.


Revision tags: OPENBSD_6_4_BASE
# 1.359 30-Aug-2018 kettenis

Move softbutton check before the check that skips attaching certain acpi
devices. Fixes regression caused by matching on _CID in addition to
matching on _HID.

ok matthieu@, mlarkin@


# 1.358 25-Aug-2018 kettenis

Match ACPI devices based on _CID if no match for _HID is found.

ok mpi@


# 1.357 19-Aug-2018 kettenis

Add support for multiple PCI segments. Only really implemented for arm64
for now as amd64/i386 firmware still caters for legacy OSes that only
support a single PCI segment.

ok patrick@


# 1.356 03-Aug-2018 kettenis

Let ahci(4) match on _CLS instead of _HID when attaching at acpi(4). Avoids
having to add many more _HID entries to the match table.

ok deraadt@, mlarkin@


# 1.355 10-Jul-2018 kettenis

Make legacy interrupts work in acpipci(4).

ok patrick@


# 1.354 05-Jul-2018 kettenis

Add acpipci(4), a driver that supports generic ECAM-compatible PCI host
bridges based on information provided by ACPI.

ok mlarkin@


# 1.353 04-Jul-2018 kettenis

Properly pass around the PCI "chipset tag" in acpi(4) and refactor
acpimcfg(4) to call an MD initialization functions that sets up a tag for
PCI ECAM.

ok guenther@, mlarkin@, krw@


# 1.352 01-Jul-2018 kettenis

Move acpi(4) attach glue into acpi_machdep.c.

ok guenther@, deraadt@


# 1.351 01-Jul-2018 kettenis

Add support for _DSD Device Properties.

ok mlarkin@


# 1.350 30-Jun-2018 kettenis

Add intr_enable() function, intended for MI use to amd64 and i386 and use
this in the acpi(4) suspend/resume code paths.

ok deraadt@


# 1.349 29-Jun-2018 kettenis

Use <stdint.h> types.

ok mpi@, jung@, krw@, deraadt@


# 1.348 29-Jun-2018 mlarkin

ACPI: Allow (with warning) GPE handler reassignment, instead of returning
a failure code.

ok kettenis


# 1.347 26-Jun-2018 mlarkin

knf


# 1.346 26-Jun-2018 kettenis

Let acpi(4) pass down the bus dma tag.

ok deraadt@


# 1.345 25-Jun-2018 kettenis

Reorganize acpi(4) code a bit in preparation for upcoming arm64 support.

ok deraadt@


# 1.344 20-May-2018 kettenis

Wake up the acpi thread after scheduling a gpio event.

ok mpi@


# 1.343 17-May-2018 kettenis

Sprinkle some #ifndef SMALL_KERNEL such that RAMDISK kernels cuild again.

pointed out by stsp@


# 1.342 17-May-2018 kettenis

Implement GenericSerialBus OpRegion support.

ok mlarkin@


# 1.341 27-Mar-2018 kettenis

Add acpicmos(4), a driver that implements SystemCMOS OperationRegion
access support. This fixes machines where the AML doesn't check whether
support for this OperationRegion type has been registered by the OS.

ok mlarkin@


Revision tags: OPENBSD_6_3_BASE
# 1.340 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.339 10-Feb-2018 deraadt

Syncronize filesystems to disk when suspending. Each mountpoint's vnodes
are pushed to disk. Dangling vnodes (unlinked files still in use) and
vnodes undergoing change by long-running syscalls are identified -- and
such filesystems are marked dirty on-disk while we are suspended (in case
power is lost, a fsck will be required). Filesystems without dangling or
busy vnodes are marked clean, resulting in faster boots following
"battery died" circumstances.
Tested by numerous developers, thanks for the feedback.


# 1.338 08-Feb-2018 deraadt

After flushing disks, try again to release all memory. We don't need to
save dirty memory to the hibernate space.


# 1.337 08-Feb-2018 deraadt

Toss all releaseable memory, because fragmentation can get in the way
of allocating the hibernate playpen.


# 1.336 08-Feb-2018 deraadt

sr_quiesce() is a new approach for ensuring that softraid drains
output to the disks.

This is part of a larger suspend/resume filesystem-safety diff, which
has been worked on for a couple of months already. Tests by job, krw,
beck, benno, and others. Sometimes even by snapshot users...


# 1.335 29-Nov-2017 kettenis

Add support for GPIO-signaled events.

ok mlarkin@


# 1.334 16-Nov-2017 jcs

Add PCI attachment for dwiic(4) needed by Intel 100 Series machines

ok kettenis


# 1.333 14-Nov-2017 kettenis

Add a few more PNP IDs to the skip lists.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.332 17-Aug-2017 stsp

Make dwiic(4) attach its ACPI parent dependencies.
This change makes the Asus E200HA keyboard work.

Original analysis and patch by Cesare Gargano
ok kettenis@


# 1.331 09-Aug-2017 dcoppa

Add "PNP0303" (8042 PS/2 Controller) to acpi_isa_hids[]

ok kettenis@


# 1.330 22-Jul-2017 jcs

acpibat: look for _BIX first before falling back on _BIF

ACPI 4.0 deprecated _BIF for battery status so some newer machines
are exporting _BIX which extends _BIF's package with a few extra
fields. When using _BIX, export the new discharge cycle count as a
sensor.

feedback from kettenis and anton
ok deraadt


# 1.329 20-Jul-2017 kettenis

Start a list of isa(4) device names such that we can avoid printing them
as "not configured".

ok mikeb@, millert@, deraadt@


# 1.328 08-Apr-2017 deraadt

Sensors are run as callbacks inside tasks. During suspend a sensor
could be running inside a driver that will be force-detached, or due
to tsleep end up disrupting the softstate/hardstate contract. At
suspend time, quisce all these callbacks by waiting for completion.
This issue has never been observed for real, but may be implicated in
suspend/resume failures.
ok kettenis guenther mlarkin


# 1.327 08-Apr-2017 deraadt

Two prototypes for acpi_maptable; merge to one place


Revision tags: OPENBSD_6_1_BASE
# 1.326 27-Mar-2017 deraadt

Now that hibernate_alloc() only has clean success/failure, don't
need to call hibernate_free() to clean up a partial mess.
ok mlarkin kettenis


# 1.325 13-Mar-2017 deraadt

allow ramdisks to build; ok jcs


# 1.324 12-Mar-2017 jcs

try to attach acpisbs first and if successful, don't attach acpibat


# 1.323 02-Mar-2017 natano

Add a new sysctl machdep.lidaction. The sysctl works as follows:

machdep.lidaction=0 # do nothing
machdep.lidaction=1 # suspend
machdep.lidaction=2 # hibernate

lidsuspend is just an alias for lidaction, so if you change one, the
other one will have the same value. The plan is to remove
machdep.lidsuspend eventually when people have upgraded their
/ets/sysctl.conf.

discussed with deraadt, who came up with the new MIB name
no objections mlarkin
ok stsp halex jcs


# 1.322 28-Feb-2017 natano

Untangle abstract sleep modes from ACPI sleep states. This paves the way
for further work in the area.

idea and ok deraadt
ok mlarkin


# 1.321 22-Feb-2017 jcs

add acpisbs, an acpi smart battery subsystem driver reading data
over smbus

currently disabled because it conflicts with acpibat

ok deraadt, kettenis


# 1.320 14-Jan-2017 kettenis

Revert aml_rdpciaddr changes; breaks several machines that were working
before.


# 1.319 11-Jan-2017 jsg

Increase the size of the buffers used for _HID and _CID strings to be
able to fit "Hyper_V_Gen_Counter_V1".

ok mlarkin@ deraadt@


# 1.318 08-Jan-2017 kettenis

Make aml_rdpciaddr() work for for devices behind bridges. The code now
uses the mapping between AML nodes and PCI devices that we establish.
Because _INIT methods may end up calling aml_rdpciaddr(), make sure we
create that mapping early. Also handle devices that aren't actually present.
These devices are now included in the mapping and reads will return an
all-ones patterm whereas writes are a no-op..

ok mlarkin@


# 1.317 25-Oct-2016 pirofti

Add the resource index to aml_parse_resource() callback function.

This is needed by an upcoming acpiec commit that handles machines breaking
the current ACPI specifications.

Change suggested by and ok kettenis@, guenther@


# 1.316 18-Sep-2016 guenther

If the APIC table (MADT) isn't present then ci_acpi_proc_id won't be set
when acpicpu tries to attach, leading to a NULL dereference. The ACPI
processor object on the Dells where this was reported is bogus and useless
anyway so apply the same test when deciding whether to attach to processor
objects as acpicpu would when matching them to cpus.

Problem reported by Eivind Eide (xenofil (at) gmail.com) and
Johan Huldtgren (johan+openbsd-bugs (at) huldtgren.com).
Testing by Eivind Eide.
ok kettenis@ mlarkin@


# 1.315 03-Sep-2016 naddy

Write the system time back to the RTC every 30 minutes.
This fixes the problem that long-running machines which were not
shut down properly would reboot with a badly offset system time.

hints and ok kettenis@


# 1.314 31-Aug-2016 mlarkin

Don't put a device in the wake device list if _STA says it's not there.

Fixes at least one machine that woke up from sleep / poweroff immediately.

ok kettenis@, tested by kettenis and martijn@


# 1.313 28-Jul-2016 kettenis

Store the acpi processor ID/UID in struct cpu_info, and use it to attach
acpicpu(4) drivers to the right cpu(4).

ok mlarkin@, guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.312 10-Jun-2016 kettenis

The power management timer has been made optional in ACPI 5.0A, so don't
attempt to attach acpitimer(4) if the timer isn't present.

Pointed out by jcs@

ok jcs@


# 1.311 08-May-2016 kettenis

Make sure devices listed by a device's _DEP method are attached before
we attach the device itself. The _DEP methode, introduced in ACPI 5.0,
is a hint that the device in question depends on OpRegion support from
the devices listed by _DEP, so we have to order them the proper way.
To prevent us from attaching those devices again when we encounter them
later walking down the device tree, keep track of our attempts to
attach a device driver to them using aflag in the aml_node structure.

ok guenther@


# 1.310 25-Apr-2016 pirofti

We attach acpihpet(4) and acpiec(4) without using the HID, so do not
report it in the dmesg.

OK deraadt@, mlarkin@.


# 1.309 02-Apr-2016 kettenis

Reduce dmesg spam by knocking out some acpi hardware IDs that don't need a
driver.

ok mpi@


# 1.308 29-Mar-2016 kettenis

Check if a device is present (using _STA) in acpi_foundhid. This prevents
us from reporting devices as "not configured" that aren't actually present,
and allows us to remove duplicated code from several drivers.

ok deraadt@, ok jsg@


# 1.307 29-Mar-2016 kettenis

Implement a cleaner way to prevent acpivout(4) from attaching if we have
a platform-specific method of controlling the backlight.

ok millert@, mlarkin@, jung@


# 1.306 28-Mar-2016 kettenis

Adding each and every new acpi device driver to acpi_foundhid() is
gettinga bit of the burden. And it really isn't how our autoconf
framework is supposed to work for busses that can be enumerated. All
the drivers already check for a matching _HID in their attach
function. So we can just drop the checks in acpi_foundhid() and let
the drivers sort themselves out.

Print a "not configured" line for devices for which we don't attach adriver.
This may be a bit much as there are devices for which we will not have a
driver. This will be removed if it causes too much dmesg spam.

ok deraadt@, mlarkin@


Revision tags: OPENBSD_5_9_BASE
# 1.305 17-Jan-2016 jsg

Take acpi_foundhid() outside of #ifndef SMALL_KERNEL so the ramdisk
kernel can find and attach dwiic(4) and sdhc(4).


# 1.304 17-Jan-2016 jsg

remove an unused global variable


# 1.303 14-Jan-2016 kettenis

Attach dwiic(4) on the Intel Bay Trail i2c controllers as well.


# 1.302 12-Jan-2016 kettenis

Improve emulation of PM registers on Hardware-redcued ACPI by emulating
SPL_TYPx, SLP_EN and WAK_STS using the new SLEEP_STATUS_REG and
SLEEP_CONTROL_REG registers. Makes the transition into S5 (powerdown) work
on the ASUS X205TA.

ok deraadt@, mlarkin@


# 1.301 12-Jan-2016 jcs

Add dwiic, a driver for the Synopsys DesignWare i2c controller found
on the Samsung ATIV Book 9 laptop. This initial version only
supports ACPI config/attachment.

Add ihidev, a HID-over-i2c driver largely based on uhidev. dwiic
handles attaching ihidev devices found in ACPI.

Add ims, a HID-over-i2c mouse/trackpad driver to get basic cursor
and button functionality from HID-compliant i2c trackpads.

ok kettenis deraadt


# 1.300 11-Jan-2016 kettenis

Make sdhc(4) attach to hardware IDs "80860F14" and "PNP0FFF".


# 1.299 09-Jan-2016 kettenis

Initial support for Hardware-Reduced ACPI. This skips the legacy mode checks
by emulating the PM_CNT register such that SCI_EN is always enabled. It also
skip attaching acpitimer(4) since the relevant registers are no longer there.

ok deraadt@, mlarkin@


# 1.298 06-Jan-2016 kettenis

Follow the spec and check the SCI_EN bit to see if ACPI has already
been enabled and don't bother enabling it again if it is. This should fix
systems that don't implement legacy mode and therefore set ACPI_ENABLE and
ACPI_DISABLE to zero.

ok mlarkin@, yasuoka@


# 1.297 23-Nov-2015 reyk

Back out the CRITICAL change, turn it back from 10% to 25%. Some
people with old/broken batteries didn't like this change, and the
underlying heuristics are wrong.


# 1.296 22-Nov-2015 reyk

The "CRITICAL" APM battery level, that is exported/faked by the ACPI
stack, is currently set to 25%. With 25% battery left, a typical
modern laptop will have at least one hour runtime left. It is an
arbitrary value, and other APM power layers in the kernel set CRITICAL
to something like 5% or 10%, so set it to 10% for now.

OK mlarkin@


# 1.295 28-Sep-2015 deraadt

In low-level suspend routines, set cold=2. In tsleep(), use this to
spit out a ddb trace to console. This should allow us to find suspend
or resume routines which break the rules. It depends on the console
output function being non-sleeping.... but that's another codepath which
should try to be safe when cold is set.
ok kettenis


# 1.294 06-Sep-2015 deraadt

clarify malloc arguments (base + length...)


# 1.293 28-Aug-2015 deraadt

fairly simple sizes for free(); ok tedu


# 1.292 20-Aug-2015 kettenis

Respect the access size when reading or writing to pci config space.
This version also makes sure writes are properly aligned, preventing
the panics seen with the previous incarnation of this change.

Fixes battery status passthrough in vmware, brightness keys on my x220
and who knows what else.

ok mlarkin@, deraadt@


Revision tags: OPENBSD_5_8_BASE
# 1.291 04-Aug-2015 deraadt

If we are setting ACPI_PM1_BM_RLD do it only once, and remember to
repeat this action at resume time.
ok mlarkin guenther


# 1.290 29-Jul-2015 deraadt

Something subtle in previous revision is locking up suspend or booting
on some laptops. Mailing lists threads mention x220 and Asus machines.
Reverting for now.


# 1.289 21-Jul-2015 kettenis

iRespect the access size when reading or writing to pci config space.
Fixes battery status passthrough in vmware and who knows what else.

Tested by many.

ok mlarkin@, deraadt@


# 1.288 17-Jul-2015 mlarkin

fix comments


# 1.287 26-Mar-2015 jsg

Use the 0x10 extended capability to determine if the trackstick
buttons are wired to the trackpad and need to be re-routed to the
trackstick. Without this change the buttons on 2015 thinkpads
get picked up as extended buttons that show up as scroll up/down.

Remove the X1 Carbon 2015 (LEN0048) and X250 (LEN0046) from the
top button area/soft buttons quirks list. Also avoid using the
quirk list entirely if the capability bit is set.

Based on work from jcs@ with feedback from mpi@
ok mpi@ jcs@


# 1.286 20-Mar-2015 kettenis

The ACPI standard says that a PCI function number of FFFF can be used to refer
to all functions on a device. This makes no sense in the context of accessing
PCI config space. Nevertheless stsp@ has a machine with AML that tries to
do config space reads from a device where _ADR encodes function number FFFF.
This is almost certainly buggy AML, but since Linux (and presumably Windows)
boot just fine on this machine we need to work around this. Linux effectively
treats function FFFF as function 7, which isn't present on this machine.
Instead we simulate a read from a non-existent device by returning a bit
pattern of all ones. Writes to function FFFF will still panic.

ok mlarkin@


# 1.285 16-Mar-2015 deraadt

Reduce use of SMALL_KERNEL in acpi, bringing acpiec driver and more gpe
handling into RAMDISK. This is now possible because the install media
has ample room. The goal is to reduce special cases where we may be
skipping (unknown) important operations...
ok mlarkin kettenis


# 1.284 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.283 07-Feb-2015 deraadt

New framework that allows hibernate to pass in entropy from it's fresh
boot.
ok mlarkin


# 1.282 06-Feb-2015 mlarkin

Fix a hibernate crash on some machines due to unmapping a page that
may not have been mapped previously (in the failure to hibernate case).

Also ensure that the lowmem ptp is mapped in all cases (not just MP).

ok kettenis


# 1.281 17-Jan-2015 deraadt

resume_randomness() before spinning up other cpus...


# 1.280 15-Jan-2015 jsg

Define a new wscons mouse type for Synaptics clickpad devices that lack
physical buttons. This will be used if the acpi pnpid for the mouse
matches a list provided by Synaptics found in Linux.

Combined with the xenocara changes this will remove the need for an xorg
config file for the x240/t440/t540 generation of thinkpads.

ok matthieu@ shadchin@ kettenis@


# 1.279 11-Jan-2015 kettenis

Tolerate checksum errors in ACPI tables (but complain about them). Apparently
there are machines out there with broken BIOSen with such checksum errors in
thier RSDT tables. Windows runs fine on these machines, so it probably does
not check. Linux ignores checksum errors as well (and prints a warning).

ok deraadt@, krw@


# 1.278 18-Dec-2014 deraadt

suspend and resume the RNG (at what looks like the right spots)
ok reyk djm


# 1.277 09-Dec-2014 doug

Replace some malloc(n*size,...) calls with mallocarray().

ok tedu@ deraadt@


# 1.276 06-Dec-2014 mpi

If "machdep.lidsuspend" is not set, do not go back to sleep if the lid
is still closed when resuming. This allows docked laptop to be resumed
without opening them.

ok mlarkin@


# 1.275 23-Nov-2014 mlarkin

If a machine has no _LIDs, then none can be open. This diff fixes a problem
where machines without any _LIDs were going back to sleep after hibernate
due to the recent suspend-if-lid-closed-on-resume diff. Noticed on qemu,
which doesn't have any _LIDs.

discussed with deraadt


# 1.274 18-Nov-2014 krw

Nuke yet more obvious #include duplications.

ok miod@


# 1.273 08-Nov-2014 mlarkin

If resuming from sleep (zzz/ZZZ) and the lid is still closed, go back to
sleep. This prevents accidental lid flex or slight opening in a backpack
from waking the machine up and leaving it resumed (powered on).

ok deraadt@


# 1.272 26-Sep-2014 kettenis

Rework piglet and pig allocation. Currently the piglet gets allocated
deep down in the suspend path, where it is really hard to recover from
allocation failure. So allocate the piglet early on in the suspend path.
Also change the piglet and piglet allocation functions to use km_alloc(9)
instead of doing pmemrange magic. This removes a bunch of code which, in the
case of the piglet allocation, is broken since it results in a NULL pointer
dereference. Also switch the piglet allocation to not wait. If we can't
allocate 16MB of phys contig memory on a halfway modern machine we're almost
certainly under a lot of memory pressure and we're better off not trying to
hibernate anyway.

ok mlarkin@


# 1.271 25-Sep-2014 kettenis

Bring back rev 1.249. Now that mp_setperf() has been fixed, it should be
safe to restore perflevel when resuming. Should fix the issues people
see with the processor running at full speed heating up the machine while
the apm command reports that the CPUs are running at a low speed.

ok dcoppa@, deraadt@


# 1.270 23-Sep-2014 kettenis

Release the acpi lock around calls to wsdisplay_suspend() and
wsdisplay_resume(). The X server may adjust the display brightness just
before/after the VT switch when suspending/resuming. In fact, it may
already be blocked adjusting the display brightness for some reason
unrelated to the suspend request.

The VT switch request will eventually time out, but the delay is annoying
and the failed VT switch will lead to problems after resume.

Needed for a future update of the xf86-video-intel driver.

ok mlarkin@


# 1.269 19-Sep-2014 kettenis

Use config_suspend_all(9).


# 1.268 08-Sep-2014 kettenis

Clear the PME Status bit on PCI devices in response to Device Wake events.
Fixes the interrupt storm on the 2009 Mac mini with WOL enabled on its
nfe(4) interface.

ok mlarkin@


Revision tags: OPENBSD_5_6_BASE
# 1.267 20-Jul-2014 mlarkin

Support hibernating to softraid crypto volumes.

much help and ok from deraadt@


# 1.266 16-Jul-2014 mlarkin

Reenable hibernate RLE support and flush+zero all memory after unpack.


# 1.265 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.264 12-Jul-2014 kettenis

Add a function to drop all clean pages on the page daemon queues and call
it when we hibernate.

ok mlarkin@, miod@, deraadt@


# 1.263 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.262 11-Jul-2014 mlarkin

Flush the buffercache to 16MB on hibernate and restore its previous max
size (kern.bufcachepercent) on resume, for better hibernate performance.

ok beck@


# 1.261 10-Jul-2014 blambert

remove unneeded header

ok mlarkin@


# 1.260 01-Jul-2014 kettenis

Adding an additonal delay after each access to SystemMemory or SystemIO
during autoconf makes no sense.

ok deraadt@


# 1.259 23-Jun-2014 kettenis

SystemMemory is used to access memory mapped registers on some machines, so we
must use the bus_space(9) API to access it instead of memcpy(9). Also make
sure we properly align access to fields that need more than byte alignment.

ok guenther@


# 1.258 21-May-2014 mlarkin

Fix invalid / missing format specifiers in several dnprintf statements
(used only when ACPI_DEBUG is enabled)

From a diff on tech@ by Fabian Raetz, with a minor change by me, tested
on i386 and amd64 with ACPI_DEBUG enabled and disabled.


# 1.257 25-Apr-2014 mlarkin

Defer the free of the hibernate structures until later, when it is safe to
do so.

Freeing the hibernate structures too early leads to inadvertent calls into
splx when we aren't ready for them yet. Found when trapping splx calls in
the resume path.

discussed with deraadt@ at m2k14


# 1.256 13-Apr-2014 mlarkin

Fix a badly worded debug message that implied the entire machine was going
to sleep, instead of just the acpi thread going to sleep.


# 1.255 31-Mar-2014 mpi

Change the order of operations during a suspend/resume cycle and
call bufq_quiesce() after executing the DVACT_QUIESCE handlers.

This should be safe since no disk nor controller drivers have such
handler but it will allow us to detach sd(4) devices attached to a
USB bus.

Another benefit pointed out by kettenis@ is that drivers that need
to read a firmware from the disk should be able to do it at resume
time in a DVACT_WAKEUP handler.

ok kettenis@, deraadt@


# 1.254 13-Mar-2014 dlg

get rid of the assumption that the head of the alldevs list is the
"mainbus" device. this breaks when mpath is enabled because it
attaches before mainbus and therefore takes the head position.

have autoconf provide device_mainbus() which looks up mainbus_cd,
and use that instead.

discussed with deraadt who just wants mpath stuff to move forward
despite there being many ways to shine this particular turd.


Revision tags: OPENBSD_5_5_BASE
# 1.253 21-Feb-2014 deraadt

Do the lid control further on in the resume sequence, since this is
AML, and will do WAITOK operations. While cold, that is a bad idea.
We seem to be safe with respect to the spec, I do not see any event
debounce issues here.
ok kettenis, tested by many


# 1.252 20-Jan-2014 claudio

Instead of checking sc->sc_revision <= 1 check that the
fadt->hdr_revision <= 1. This is a better check since it checks
the version of the proper descriptor. This allows the alix APU
to reboot since those boards have currently a wrong sc->sc_revision.
Committing this now so that it can be tested easier.
With and OK jsg@ also fine by derradt@


# 1.251 24-Dec-2013 kettenis

If the FADT has its SMI_CMD set to zero, assume we're only ACPI-only hardware
and don't need to disable SMI ownership of the ACPI hardware registers.

ok mlarkin@


# 1.250 23-Dec-2013 kettenis

Revert previous commit. It makes resume hang on some MP systems running
GENERIC.MP.


# 1.249 16-Dec-2013 kettenis

Restore hw.setperf upon resume like we do for apm(4) on i386. Seems we need
to do this fairly late in the resume to avoid hangs.

ok deraadt@, mlarkin@


# 1.248 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


# 1.247 06-Nov-2013 mpi

Add support for Power Resources for Dx states and the necessary hook
for PCI devices. This hook should be called twice, before and after
changing the power state of a PCI device.

Before setting the device to the new state, the ACPI layer will notify
every power resources linked to the device for that state and make sure
they are turned "_ON". After changing the state of the device, it will
decrement the reference of every power resources linked to that device
for the old state and turn them "_OFF" if they are no longer referenced.

This fixes the no-USB after resume problem seen on various ThinkPad,
problem initialy diagnosed with Alexander Polakov.

ok kettenis@, deraadt@


Revision tags: OPENBSD_5_4_BASE
# 1.246 01-Jun-2013 mlarkin

Stop printing ACPI wakeup devices in dmesg after the 16th wakeup device.

Helps with VMware VMs that report hundreds of wakeup devices.

ok reyk@, deraadt@


# 1.245 31-May-2013 bluhm

Fix the build for a kernel without wd and pciide in its config.
OK deraadt@ tedu@


# 1.244 30-May-2013 deraadt

Enforce ca_activate tree-walks over the entire heirarchy for all events,
cleaning up some shutdown-hook related code on the way.
(A few drivers related to sparc are still skipped at kettenis' request)
ok kettenis mlarkin, tested by many others too


# 1.243 18-Apr-2013 deraadt

pin the acpi thread to the BSP, because AML and SMI cannot be trusted. I
was certain we were doing this already, but it turns out no...


Revision tags: OPENBSD_5_3_BASE
# 1.242 24-Dec-2012 guenther

In acpiioctl(), Return though the splx() path if suser() returns an error

ok deraadt@


# 1.241 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


# 1.240 04-Oct-2012 ehrhardt

Use information provided by ACPI to attach secondary PCI host bridges.

ok kettenis@


# 1.239 07-Sep-2012 kettenis

Add a new function called acpi_pci_min_powerstate() that returns the lowest
power state for a PCI device for the power state (currently S3 or S4) we're
transitioning into. This function is probably too simplistic; there is a lot
of (silly) complexity in the ACPI spec.

ok deraadt@


Revision tags: OPENBSD_5_2_BASE
# 1.238 13-Jul-2012 pirofti

Disable acpivideo(4) on ASUS laptops.

There are some models that panic whenever an event is received.
Some cope, some don't. In order to get this right acpiec(4) requires
major rework. Until then let machines boot.

Discussed with deraadt@.


# 1.237 11-Jul-2012 mlarkin

for now, restrict hibernate to root in the acpi ioctl kernel interface.
normal rules for controlling this interface through apmd still apply.

ok deraadt@


# 1.236 09-Jul-2012 deraadt

tiny little oops in untested hibernate block


# 1.235 09-Jul-2012 deraadt

clean up ioctl() case statements


# 1.234 09-Jul-2012 deraadt

acpi_disable_onegpe() is not used; ok pirofti


# 1.233 24-May-2012 kettenis

If _MIN > _MAX consider a WordBus resource to be invalid. Fixes a panic on
the IBM x3550-M4.

ok mikeb@


# 1.232 29-Mar-2012 mlarkin

Missing #ifdef in previous diff


# 1.231 29-Mar-2012 mlarkin

Prevent hibernation attempts on machines with unsupported disk controllers.
The same check is also performed later in the hibernate sequence, but by
then it's too late to cleanly unwind (presently).

ok deraadt


# 1.230 26-Mar-2012 deraadt

simplify conditionals in acpi_sleep_state()
ok mlarkin


# 1.229 26-Mar-2012 deraadt

hook in the hibernate request code; half of this diff is from mlarkin
ok mlarkin


Revision tags: OPENBSD_5_1_BASE
# 1.228 20-Sep-2011 deraadt

Very early in suspend while the clock is still ticking realtime,
save the clock back to the rtc. The zaurus already did this.
ok phessler miod kettenis


Revision tags: OPENBSD_5_0_BASE
# 1.227 02-Jul-2011 nicm

kqueue attach functions should return an errno or 0, not a plain 1. Fix
the obvious cases to return EINVAL and ENXIO.

ok tedu deraadt


# 1.226 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.225 16-Jun-2011 pirofti

Import acpitoshiba driver.

This is originally written by Hiroyuki Aizu, ported and integrated
in our tree by Javier Vazquez <javama4 AT gmail> with tweaks and
suggestions by me.

Import prodded by deraadt@ and the lack of testers on tech@.


# 1.224 27-Apr-2011 jcs

attach acpithinkpad to newer lenovo models like the x120e

ok deraadt@


# 1.223 22-Apr-2011 jordan

Fix uninitialzied variables and formatting strings (-Wxxx errors)


Revision tags: OPENBSD_4_9_BASE
# 1.222 02-Jan-2011 jordan

Re-add code for ACPI taskqueue, eliminates need for state vars in acpi softc
Serialize processing ACPI tasks for sleep/powerdown/GPE event
ok deraadt@


# 1.221 31-Oct-2010 guenther

Revert last commit: it breaks resume on Thinkpads
"then please back it out" deraadt@


# 1.220 26-Oct-2010 jordan

Add task queue for ACPI gpe and notify handlers
This fixes eject on Dell Latitude dock and an issue on the Dell Mini
battery update.
ok deraadt


# 1.219 07-Oct-2010 jordan

Create separate functions for enabling wake and runtime gpes.
Remove old suspend/resume gpewalk function
ok deraadt, mlarkin


# 1.218 05-Oct-2010 jordan

split acpi_enable_onegpe to separate enable/disable functions
ok deraadt


# 1.217 05-Oct-2010 jordan

Added acpi_disable_allgpes function to clear GPE status
ok deraadt


# 1.216 05-Oct-2010 deraadt

clean up acpi_softc use more


# 1.215 05-Oct-2010 deraadt

delete a bit of #if 0 code


# 1.214 05-Oct-2010 deraadt

hide use of our global acpi_softc pointer a bit better


# 1.213 29-Sep-2010 deraadt

grab the acpi thread lock very early in acpi thread startup; not that it
is likely that anyone will race us... but why be careless.
ok kettenis


# 1.212 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.211 29-Aug-2010 jasper

- add a check for FWRITE here too.

ok miod@


# 1.210 27-Aug-2010 kettenis

Make PCI Power Management optional, and only enable it when acpi(4) attaches.

ok deraadt@


Revision tags: OPENBSD_4_8_BASE
# 1.209 08-Aug-2010 kettenis

Close race between the acpi thread and wscons ioctls running in process
context. We might rip this out post-release and replace it with a solution
that serializes things in a more obvious way.

ok deraadt@, miod@ and "those crazy texans who ok anything"


# 1.208 08-Aug-2010 deraadt

Report the acpi sleep states when on a RAMDISK; ok krw


# 1.207 07-Aug-2010 kettenis

Enforce acpi register access rescrictions.

ok mlarkin@, deraadt@, tested by marco@


# 1.206 07-Aug-2010 deraadt

upon resume, notify all the acpiac and acpibat drivers to update
their status. some laptops do not do this automatically.
ok kettenis canacar


# 1.205 06-Aug-2010 marco

Always call _PSW on _LID devices that support it to enable lid open to
resume the machine. Conversely disable it when the machine wakes up.

Tested by several ok deraadt


# 1.204 06-Aug-2010 deraadt

As part of a revamp of the PM1/GPE code, I write this diff to look for
unmanaged EN & STS bits in the PM1 register at interrupt time and report
them. As a side effect this splits the STS acknowledgement into two writes
(for power, and sleep) instead of one. The printf that is added (to spot
unmanaged STS bits) has not yet been triggered as far as we know.

Before the "write to PM1 registers at the right offset" diff went in, this
was not neccessary. But newer thinkpads do not have a working soft power
button without this diff.

We have no idea why.
ok mlarkin kettenis


# 1.203 05-Aug-2010 deraadt

PM1 power button events were generating 'fake events' to acpibtn PWRB,
but if there is no such device these were being tossed. Instead, arrange
for all power button handlers to flag a softc variable which is processed
by the thread; so that one place will know to request a powerdown (psignal
to init)
ok marco mlarkin


# 1.202 05-Aug-2010 deraadt

rename a variable which is annoyingly poorly named


# 1.201 05-Aug-2010 mlarkin

Write to the correct offset for various ACPI pmreg registers. Writing to
the correct register does properly clear the wake status bits and fixed
event bits, which allows some machines to have more than 1 suspend cycle.

bug spotted by deraadt@

tested by deraadt@ and myself on a half-dozen machines and others on
hackers@

ok deraadt@, marco@


# 1.200 05-Aug-2010 deraadt

Fix the power button. The pm1 register is 16 bits in size.
My mistake, a while back.
tested by krw


# 1.199 04-Aug-2010 kettenis

Print supported sleep states.

ok marco@, phessler@, deraadt@


# 1.198 03-Aug-2010 mlarkin

Use the proper flag for re-enabling certain hardware events (power btn,
sleep btn). The (incorrect) flag prevoiously used coincidentally had the
same value, so this shouldn't cause any different behavior than before.

tested on a variety of machines (i386, amd64, sp, mp) - no behavioral
change seen

ok deraadt@


# 1.197 28-Jul-2010 marco

Make all gasio failures look and behave the same.


# 1.196 28-Jul-2010 mlarkin

Delay a little longer on suspend before giving up. Fixes machines that
sometimes don't suspend because the sleep transition takes too long
(symptoms like failing to sleep but the backlight stays on).

ok deraadt@


# 1.195 27-Jul-2010 deraadt

remove XXX; EC can now be attached early when needed; ok jordan


# 1.194 27-Jul-2010 deraadt

more brackets feels good


# 1.193 27-Jul-2010 deraadt

XXX annotate a block in acpi_enter_sleep_state() that is pretty worrying


# 1.192 27-Jul-2010 deraadt

horrific indentation that nearly made me puke


# 1.191 27-Jul-2010 mlarkin

Set the sleeping indicator light on machines that support it, in case SMI
doesn't do it for us. Shows the 'moon' LED on older Thinkpads when
sleeping.

Tested on T43p, W500, T510 by me and on X61s by deraadt@.

ok deraadt@


# 1.190 27-Jul-2010 jordan

Early initialization of acpiec if ECDT table exists
Fixes hang when booting thinkpads while docked
ok deraadt


# 1.189 26-Jul-2010 deraadt

remove merge error which was commited


# 1.188 26-Jul-2010 pirofti

Add support for Sony ACPI hotkeys via a new driver: acpisony(4).

Currently it only works for the suspend button and tries to do right for
the brightness events, but I haven't found a Sony laptop that like to do
right so far.

In the future I want to make the brightness keys work on all Sony's and
also add support for the zoom hotkeys and whatever other funky keys I can
find on those things.

Okay deraadt@.


# 1.187 22-Jul-2010 deraadt

pretty large cleanup of SMALL_KERNEL


# 1.186 21-Jul-2010 deraadt

in the acpi GPE handler, late ack level interrupts not edge interrupts.
Duh.


# 1.185 21-Jul-2010 deraadt

Merge the edge and level gpe handlers into one (now possible since the
gpe structure tells us whether the gpe is edge or level)


# 1.184 21-Jul-2010 deraadt

delete #if 0 code we are not going to use; ok marco


# 1.183 20-Jul-2010 deraadt

make the mask code for acpi_enable_onegpe() match other similar code


# 1.182 20-Jul-2010 deraadt

Handle the acpi interrupt controller with proper edge/level handling,
wrapped in spltty to avoid racing against the interrupt controller.
Repair the gpe bit masking code while there, and do operations in the
right order
ok kettenis mlarkin, help from jordan at figuring out the order of
operations.


# 1.181 20-Jul-2010 matthew

When we're preparing to go to sleep, only call bufq_restart() if other
preparations failed.

ok kettenis@, thib@, deraadt@


# 1.180 19-Jul-2010 deraadt

permit compilation with ACPI_DEBUG


# 1.179 19-Jul-2010 deraadt

there is no need for a seperate dsdt_softc pointer; dsdt does not have a
softc but is just a part of the single acpi driver.
ok jordan


# 1.178 19-Jul-2010 deraadt

acpi_delay() is no longer needed; ok jordan


# 1.177 18-Jul-2010 mlarkin

Clear event status on resume, to avoid going back to sleep/reboot/shutdown
immediately after resume. Tested by a bunch of people on hackers@ on
various hardware. Fixes at least Toshiba NB200 and Gateway LT3103u.

ok marco


# 1.176 13-Jul-2010 deraadt

rename some thread related interfaces so they are logical


# 1.175 10-Jul-2010 jordan

Verify _ADR bdf is correct (some systems return 0xffff)
Works on Dell x9xx and Studio 1555


# 1.174 09-Jul-2010 jordan

Clear the noise


# 1.173 08-Jul-2010 jordan

Add mapping for ACPI device to PCI bdf (match autoconf tree)
Simplify resource parsing function to use buffer argument
Convert namespace linked lists to use queue macros
ok marco@, deraadt@


# 1.172 06-Jul-2010 deraadt

Use spltty() to lock downcalls from apm against the information being
modified by the acpi thread.
ok kettenis


# 1.171 06-Jul-2010 deraadt

Don't use a workq to do the suspend, because it races aginst the acpi
thread. Instead, just tell the acpi thread to do the suspend for us.
This makes apmd & zzz work correctly. While here, have acpithinkpad
attempt to post the event to apm before prompting the suspend itself.
ok kettenis marco mlarkin


# 1.170 05-Jul-2010 mlarkin

Reset register SCI_EN on resume. The ACPI spec clearly states we don't have
to do this, but many machines don't, or forget to, which leads to all sorts
of problems on resume (broken battery stats, overheating, failure to resume
more than once, etc).

tested by many, ok marco@, deraadt@


# 1.169 01-Jul-2010 thib

do bufq quiesce again, since vnd/the bufq_done call in
biodone has been fixed.


# 1.168 01-Jul-2010 jordan

Backout recent AML changes, breaks on R210 and others


# 1.167 01-Jul-2010 jordan

Simplified aml_parse_resource call, cleanup code
ok marco


# 1.166 01-Jul-2010 jordan

Fix subordinate bus # for multi-root PCI buses
ok marco, kettenis


# 1.165 30-Jun-2010 thib

Disable/partially backout the bufq quiesce changes as this
is causing havoc with vnds and release must be buildable.


# 1.164 29-Jun-2010 deraadt

Fix ramdisks (new code was placed inside a SMALL_KERNEL chunk)


# 1.163 29-Jun-2010 jordan

Add support for mapping ACPI to PCI devices
ok kettenis, deraadt


# 1.162 29-Jun-2010 kettenis

Call bufq_quiesce() on suspend to stop all I/O and bufq_restart on resume.
Makes suspend/resume work much better while doing disk I/O.

ok mlarkin@


# 1.161 29-Jun-2010 tedu

60 is a more reasonable guess for number of minutes in an hour than 100.
spotted by TeXitoi. ok deraadt


# 1.160 27-Jun-2010 jordan

Add _acpi_matchhids routine, precursor to pci device match


# 1.159 27-Jun-2010 mlarkin

Clear ACPI fixed event status on resume (power buttons/etc) so that some
machines don't immediately resume after suspending to S3.

Tested by kettenis@ and I on 5 or 6 machines, deraadt@ "yeah, looks good"


# 1.158 27-Jun-2010 jordan

Cleaned up acpi_maptable, adds to table list directly


# 1.157 07-Apr-2010 deraadt

If we have done a wsdisplay_suspend(), and ACPI decides it does not want to
suspend, we must roll back by calling wsdisplay_resume() or the virtual
console switching will be locked forever
ok oga


# 1.156 07-Apr-2010 kettenis

Make suspend/resume work on MP machines (running an MP kernel). Joint work
from mlarkin@ and me, with some amd64 fixes thrown in by deraadt@

ok marco@, deraadt@, pirofti@, mlarkin@


# 1.155 31-Mar-2010 kettenis

Bring /dev/apm support in line with apm(4). Make the suspend button send
a suspend request event to apmd(8) instead of suspending immediately. Also
keep track of whether /dev/apm and /dev/apmctl are currently open, such that
we can still suspend immediately if apmd(8) isn't running.

ok deraadt@, marco@, pirofti@, jsing@, oga@


# 1.154 30-Mar-2010 oga

Prevent the apmd/x races for good.

When we hit suspend time, go through all wsdisplays on the system. if
they are in mode MAPPED, but not MODE_DUMBFB then if possible do a full
vt switch to a !mapped vt, and prevent switching back until resume time.
This has to be called from MD code because this involves userland
running so that X can run the vt switch signal handler. This way, any
case where we are using the "poke registers from userland" model, we
will not be on the hardware when we go down, so the kernel can actually
handle thing properly.

Tested on several acpi laptops (by kettenis@ and ian@), x40 (me and
beck@ at LEAST) and zaurus (me). Maybe others, but if so I forgot who at
this time..

Idea from deraadt somewhere over the Faroe Islands (I thought of a
similar thing myself a while ago). Much prompting from him. Ok and
comments miod@


# 1.153 25-Mar-2010 oga

Fix & vs. && in the kqfilter code.

Based on a comparison with the apm code.

ok deraadt@, kettenis@


Revision tags: OPENBSD_4_7_BASE
# 1.152 26-Nov-2009 mlarkin

Visual (and on some machines, audio) feedback while the machine is
resuming via calls to aml _SST method. This also makes the "moon" LED on
thinkpads to blink during resume and cease when resume is completed.

ok pirofti@


# 1.151 26-Nov-2009 deraadt

Toggle into "cold" when we we disable interrupts, because tsleeping
in the AML interpreter and context switching into userland processes
tends to sometimes pop the ACPI trampoline page out of our address
space. Makes lots more machines work.
ok kettenis mlarkin


# 1.150 24-Nov-2009 jsg

in the resume path evaluate _WAK before dealing with device drivers
like the spec says, not after.

ok mlarkin@


# 1.149 24-Nov-2009 deraadt

abort suspends on SMP systems until that gets written; ok mlarkin


# 1.148 23-Nov-2009 mlarkin

pci should return the result of it's children's suspend/resume calls back
to its parent.

handle suspend failure case and unwind if devices have an activate function
that returns failure.

ok deraadt@, kettenis@


# 1.147 23-Nov-2009 mlarkin

acpi* devices need to call their children on suspend/resume.
ok deraadt@


# 1.146 23-Nov-2009 pirofti

Remove ACPI_SLEEP_ENABLED checks.

This enables by default the suspend/resume paths in the kernel.

Okay deraadt@.


# 1.145 23-Nov-2009 deraadt

knf and spacing that are super annoying


# 1.144 23-Nov-2009 mlarkin

Hook up Theo's new children suspend/resume function, as well as more ACPI
suspend/resume glue.

ok deraadt@


# 1.143 26-Oct-2009 deraadt

Do not do strncmp comparisons on dv_xname because longer device names which
look similar could arrive in the future. Instead, compare directly against
dv_cfdata->cf_driver->cd_name
Issue originally spotted by miod


# 1.142 26-Oct-2009 jordan

Verify checksum+address when loading ACPI tables.
Some systems had invalid entries in RSDT/XSDT.
ok marco@


# 1.141 23-Jul-2009 cnst

New aibs(4) driver for ASUSTeK AI Booster (ACPI ATK0110) hardware monitoring,
with sensor state support through limits provided by the ACPI.

Tested on several ASUS motherboards kindly networked by Sam Fourman Jr.

ok deraadt marco jordan


Revision tags: OPENBSD_4_6_BASE
# 1.140 03-Jun-2009 pirofti

Import acpi power resource driver. This is needed for suspend/resume logic.

Okay jordan@ and marco@.


# 1.139 03-Jun-2009 pirofti

Add acpivideo support. This adds brightness support for all laptops
except thinkpads, they will use the acpithinkpad driver. The driver is
also hooked into wsconsole. So brightness can be adjusted via:

$ wsconsctl display.brightness=<percentage>

This is very helpfull on some laptops that have a nasty bios and get two
steps instead of one when pressing the brightness button.

Tested on various dell, fujitsu, acer, samsung and other laptops.

Okay marco@, miod@. Suggestions from kettenis@.
Lots of reviews and help from miod@, thanks!


# 1.138 03-Jun-2009 jordan

Added ejectable bay for IDE cdroms
ok marco@


# 1.137 30-Apr-2009 marco

Oga changed apm ioctls and forgot to grep. Temporary work around by hitting
the sleep button when we get any request. This interface will chnage at
some point.
ok oga


# 1.136 19-Apr-2009 krw

Call acpi_sleep_walk() when powering down just like when entering
any other sleep state. This tells acpi wake devices to not wake up
the box until manual powering up resets their status. Makes my Dell
GX520 stay powered off in response to 'halt -p'.

In snaps for a while.

ok marco@ (several times) deraadt@


# 1.135 17-Apr-2009 pirofti

Add device ranking support for future sleep/wake developments.

okay marco@


# 1.134 11-Mar-2009 jordan

Cleanup.. use aml_evalinteger instead of aml_evalname
ok marco@


# 1.133 10-Mar-2009 jordan

Fix cut/paste error.. call _BFS not _PTS
ok marco@


Revision tags: OPENBSD_4_5_BASE
# 1.132 21-Feb-2009 marco

overzealous cut/paste; found by Vladimir Kirillov <proger@uaoug.org.ua>


# 1.131 20-Feb-2009 marco

Oops didn't mean for Debugger() to remain there.


# 1.130 19-Feb-2009 marco

suspend/resume bits so that we can develop this in tree. This is disabled.
code from mlarkin and me
help from art,toby,jordan and several others
ok jordan, go for it deraadt


# 1.129 10-Feb-2009 jordan

Fix null pointer for shutdown if acpi not enabled
ok marco@


# 1.128 04-Feb-2009 kettenis

Some systems that claim to be ACPI 2.0 apparently don't provide an XSDT
address. Make the code fall back on the RSDT on those systems.

From Joerg Niendorf <p3y80@internode.on.net>. Fixes PR 5986.


# 1.127 17-Jan-2009 krw

A couple of stray malloc/memset -> malloc(M_ZERO), a bit of knf and
lose a couple of unnecessary casts. No functional change.

Feedback from kettenis@

ok kettenis@ marco@


# 1.126 06-Nov-2008 marco

Collapse all sanity checks of hids into one function.

Originaly from jordan


Revision tags: OPENBSD_4_4_BASE
# 1.125 02-Jul-2008 fgsch

add acpivideo. for now almost an empty skel so others can work on
suspend/resume. eventually it will also manage output switching and
brightness where supported. prodded by marco@


# 1.124 11-Jun-2008 marco

Fix $OpenBSD$ for once and for all


# 1.123 08-Jun-2008 deraadt

aaa_dev was being set from an uninitialized variable; ok toby


# 1.122 07-Jun-2008 marco

Kill ACPIVERBOSE. It was never used and is worthless to boot.

comments jmc
ok toby


# 1.121 06-Jun-2008 marco

Shrink acpi some more for floppies and while here re-enable acpi on the
floppies. Fixes crazy sony vaio kettenis has.

tested by deraadt
ok deraadt jordan kettenis


# 1.120 01-Jun-2008 marco

Fix \n nit in SMALL_KERNEL.


# 1.119 01-Jun-2008 marco

Re-enable new parser. Lots of testing by various folks. Fits all media
now too.

ok kettenis deraadt


# 1.118 16-May-2008 dlg

back out jordans new parser.

it didnt have enough oks from the right people, it did break machines
that were previously working, and it bloated the RAMDISKS and install
media beyond the constraints of the media.

requested by deraadt@


# 1.117 14-May-2008 jordan

Adding new ACPI Parser code
Old guts of code still exists, needs to be torn out next
ok marco@
ok brad@


# 1.116 27-Apr-2008 jcs

add acpithinkpad(4), an acpi support driver for ibm/lenovo
thinkpads.

provides functionality that the bios did on older, non-acpi
thinkpads like respond to screen brightness keys and toggle the
bluetooth device on and off.

ok deraadt@ and marco@


# 1.115 24-Apr-2008 jsing

Introduce acpiasus(4), a driver for the ACPI based hotkeys found in many
ASUS laptops (including the ASUS EeePC) - largely based on NetBSD's
asus(4) driver. On the ASUS EeePC this allows us to enable/disable
wireless, change screen brightness and use the volume keys.

ok jsg@, weingart@


# 1.114 12-Apr-2008 kettenis

Get rid of acpi_s5 global variables; simply send SIGUSR2 instead of SUGUSR1
to tell init(8) to power down the machine.

ok krw@


Revision tags: OPENBSD_4_3_BASE
# 1.113 21-Jan-2008 jsg

Check against RESET_REG_SUP in FADT before doing the acpi reset dance
after all, not doing so appears to break more machines than it fixes.

ok marco@


# 1.112 05-Jan-2008 kettenis

Never attach more acpicpu(4) devices than cpu(4) devices. Always attach
the first acpicpu(4) device on non-MULTIPROCESSOR kernels, regardless of its
ACPI processor ID.

ok mikeb@


# 1.111 05-Dec-2007 deraadt

Hang acpi(4) below bios(4) instead of mainbus(4). This lets us move acpi(4)
before pcibios(4), and then.... if acpi(4) attaches, skip pcibios(4) since
it messes the machines up. Fixes claudio's HP dl320 G5, and almost assuredly
others.
ok kettenis


# 1.110 05-Dec-2007 deraadt

repair acpiopen/acpiclose and friends to handle the /dev/apmctl interface,
so that apmd(8) can actually work correctly against acpi(4), and thus,
even do the fancy -C/-A stuff; also tested by jmc


# 1.109 02-Dec-2007 fgsch

fix the trailing space for real now.


# 1.108 02-Dec-2007 fgsch

avoid trailing spaces on output; marco@ ok.


# 1.107 27-Nov-2007 kettenis

Fix off-by-one that made us attach acpicpu0 and acpicpu1 on GENERIC kernels.

ok marco@


# 1.106 25-Nov-2007 jsg

When available and ACPI is enabled use the ACPI reset method
to reboot. This is required for some quirky sis machines
and likely others to reboot.

Tested by many. 'commit early commit often' deraadt@


# 1.105 19-Nov-2007 deraadt

sigh, amd64 still uses that stupid acpi_enabled variable...


# 1.104 19-Nov-2007 deraadt

softc's come initialized to 0, you know...


# 1.103 19-Nov-2007 deraadt

store revision; from jsg


# 1.102 19-Nov-2007 kettenis

Initialize sc_facs earlier.

ok deraadt@, beck@, canacar@


# 1.101 16-Nov-2007 deraadt

fix the bus_space #define nightmare, so that amd64 and i386 are much more
uniform. as a result shared code like acpi needs less #ifdef's
ok marco kettenis


# 1.100 15-Nov-2007 deraadt

remove the #ifdef ACPI_ENABLE. the EC codepath is either safe enough (and
will be improved), or it is unsafe because AML assumes it must be there
ok almost everyone


# 1.99 15-Nov-2007 mikeb

Make acpicpu(4) attach to the existing CPUs only.

Problem reported by form@. Fix idea by kettenis@.
Lots of help from deraadt@. Tested by deraadt@, form@ and me.

ok deraadt marco


# 1.98 14-Nov-2007 deraadt

spacing


# 1.97 12-Nov-2007 deraadt

correct : vs , in attach-time messages


# 1.96 06-Nov-2007 deraadt

all ACPI_ENABLE kernels need acpi_interrupt()


# 1.95 06-Nov-2007 deraadt

move acpi_read_pmreg() and acpi_write_pmreg() out of SMALL_KERNEL so
that acpi ramdisks can compile (obvious solution)


# 1.94 05-Nov-2007 canacar

Establish the acpi interrupt earlier. Fixes problems with some
GENERIC.MP kernels not getting acpi interrupts. This is a
workaround until the real issue with late establishment is fixed.
ok weingart@, beck@, gwk@


# 1.93 03-Nov-2007 jordan

Added support for displaying wakeup devices
ok beck@,weingart@,gwk@


# 1.92 11-Oct-2007 kettenis

When walking the tree yo find _PRT methods, check whether the device is
actually enabled and functioning. Similar to what we do for _INI.

ok weingart@, beck@


# 1.91 08-Oct-2007 krw

More simple memset(,0,) -> M_ZERO changes. In this batch move to
size(*p) as the first malloc() parameter where p is declared locally
and thus easy to check. Add M_ZERO to gpe_table allocation in acpi.c
even though there is no obvious bzero or memset nearby.


# 1.90 13-Sep-2007 mk

Remove unnecessary memset() (see aml_evalnode(9)) and a bit of KNF.

ok toby


# 1.89 13-Sep-2007 weingart

Implement ACPI 6.5.1 spec tree walk for _STA and _INI.
Fixes mk's laptop. No regressions so far (thank you to the
testers).

ok gwk@, mk@, marco@


Revision tags: OPENBSD_4_2_BASE
# 1.88 17-Apr-2007 mk

Now that acpidock no longer hangs machines during boot (at least
according to the reports I've been getting) it should be safe to enable
it again, so remove the #if 0 stuff around it.

ok marco


# 1.87 11-Apr-2007 jordan

Added changes for C-State
ok marco@


# 1.86 26-Mar-2007 marco

Calculate minutes left more accurately.

From giovanni <qgiovanni@gmail.com>


Revision tags: OPENBSD_4_1_BASE
# 1.85 03-Mar-2007 mk

Stop looking for acpidock until we fix that stuff that breaks all pre-60
series ThinkPads with acpidock(4) enabled.


# 1.84 22-Feb-2007 marco

We were getting no interrupts in .MP. There are 2 fixes; one in
acpi_machdep.c and move the interrupt establishment down after ioapic
has been initialized.

ok jordan


# 1.83 21-Feb-2007 kettenis

Make an acpi-enabled GENERIC.MP work on systems that only support PIC mode.

ok marco@


# 1.82 19-Feb-2007 jordan

Added acpi_map_address function for mapping GAS/IO registers
ok marco@


# 1.81 18-Feb-2007 jordan

Added dynamic perfect hash for opcodes
ok marco@


# 1.80 18-Feb-2007 marco

Shave off another K pre compression.
Shuffle SMALL_KERNEL functions around so that we only have a single #ifndef


# 1.79 17-Feb-2007 marco

Add SMALL_KERNEL throught acpi to make it fit on boot media.
ok deraadt


# 1.78 13-Feb-2007 marco

Add smarter _INI code. This helps with some dissapearing devices.

ok kettenis jordan


# 1.77 31-Jan-2007 gwk

Introduce acpi_hasprocfvs thats "ACPI Has Processor Frequency and Voltage
Scalling" a global flag set by acicpu attach to indicate that ACPI is
handling fvs through the _PSS objects. This will fix disappearing
EST/powernow when booting kernels with acpi enabled, as acpicpu isnt
compiled into GENERIC yet. Also in cases where acpicpu is built in and
legacy EST/powernow work but acpi is missing the _PSS object, the legacy
routines will be used.

tested by dim@, and Nick Nauwelaerts <nick AT nauwelaerts.net>
ok marco@, dim@


# 1.76 22-Jan-2007 mk

Actually look for docking stations by looking for _DCK nodes.

ok marco


# 1.75 02-Jan-2007 marco

On some systems EC must be attached as the first device. Move EC from
generic _HID devices to its own routine to force first attach. Bug found
and debugged by joris@

Code mostly written by joris@


# 1.74 26-Dec-2006 marco

Add polling to devices that require it such as acpibat and acpitz.
Use the same tsleep/wakeup mechanism as the interrupt code to keep it non-
concurrent.


# 1.73 21-Dec-2006 deraadt

even more knf love


# 1.72 21-Dec-2006 marco

Remove some leftover acpiec gpe/interrupt code.


# 1.71 20-Dec-2006 deraadt

oops, name already used


# 1.70 20-Dec-2006 deraadt

rename __acpi_enable_gpe(); ok marco


# 1.69 20-Dec-2006 deraadt

acpi_unmap_pmregs() is not used, ok marco


# 1.68 19-Dec-2006 deraadt

simplify some low-level methods substantially (shrinking code); ok marco jordan


# 1.67 06-Dec-2006 mk

malloc(..., M_NOWAIT) with no NULL check is bad. If allocation fails,
complain, clean up, and bail out of attach.

input and ok gwk


# 1.66 28-Nov-2006 jordan

Fixed clearing arg for acpi_init_pic
ok kettenis@


# 1.65 27-Nov-2006 kettenis

Don't spam dmesg.


# 1.64 27-Nov-2006 jordan

Fixes for acpiprt routing
Post-parse fixup for namerefs
ok kettenis@


# 1.63 25-Nov-2006 marco

Don't handle acpiev events inside the gpe handler since this causes deadlocks.

ok jordan@


# 1.62 15-Nov-2006 kettenis

Hook up acpi apic interrupt routing support to generic acpi code.

ok jordan@


# 1.61 02-Nov-2006 marco

Kill acpiec attach function and add its functionality to the generic _HID
device attach

prodded and ok jordan


# 1.60 02-Nov-2006 marco

Fix comments to reflect reality.


# 1.59 19-Oct-2006 marco

Replace ACPI ioctls with APM ioctls. This makes ACPI talk the same protocol
as APM therefore userspace can now retrieve power information in the same
fashion as all other apm arches (zaurus, macppc, i386 etc).

To use force the use of acpi device like so: apmd -f /dev/acpi

ok jordan@


# 1.58 19-Oct-2006 jordan

Fixed GPE debug levels
Fixed ref counter on temporary bufferfields
DSDT parsing fully working on IBM Thinkpad
ok marco@


# 1.57 15-Oct-2006 jordan

Removed old GPE handler code remnants
ok marco@


# 1.56 12-Oct-2006 jordan

Massive ACPI changes
Fixed ACPI leaks
new ACPI API
ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.55 30-Jun-2006 gwk

KNF, No functional change,
"go at it" marco@


# 1.54 30-Jun-2006 jordan

Support for calling Embedded Controller via new GPE interface
ok marco@


# 1.53 30-Jun-2006 jordan

Added new code for GPE handling
Also works with ACPIEC; need to fixup acpiec.c to remove original interrupt handler
ok marco@


# 1.52 30-Jun-2006 jordan

Added offset argument for acpi read/write pmreg routines (new GPE code)
ok marco@


# 1.51 31-May-2006 todd

do not call acpi_powerdown() unless acpi has successfully initialized
ok gwk@ canacar@


# 1.50 29-May-2006 canacar

Add embedded controller support.
ok marco@ deraadt@ testing & ok gwk@


# 1.49 19-May-2006 canacar

Minimal thermal zone driver. Monitors thermal zone temperature,
shuts down the system if the 'critical temperature' is reached.
ok marco@


# 1.48 11-Apr-2006 marco

KNF.


# 1.47 11-Apr-2006 gwk

Add acpi_resume(struct acpi_softc *sc), which calls some AML methods,
detected at attach time, currently unused.

ok marco@


# 1.46 11-Apr-2006 gwk

Discover some ACPI sleep related methods, and call them at the
appropriate time in acpi_enter_sleep_state().

ok marco@


Revision tags: OPENBSD_3_9_BASE
# 1.45 26-Feb-2006 marco

Add acpicpu as a device.

ok jordan@


# 1.44 23-Feb-2006 marco

remove dead function.

ok jordan@


# 1.43 22-Feb-2006 marco

Print out all tables it found during autoconf. We'll need this data in
the future.


# 1.42 22-Feb-2006 marco

Make dmesg less verbose.

ok jordan.


# 1.41 22-Feb-2006 jordan

Added parsing of SSDT tables
ok marco@


# 1.40 22-Feb-2006 jordan

Added new aml_register to notify by device id
Moved powerdown code to button device
ok marco@


# 1.39 21-Feb-2006 marco

Readjust debug levels that crept in too low over the last few days.
KNF


# 1.38 21-Feb-2006 marco

Rig the power button so that the machine shuts down correctly.


# 1.37 20-Feb-2006 marco

Add acpibtn device. Remains disabled for now.


# 1.36 19-Feb-2006 jordan

Added code for aml_notify GPE notification callback
ok marco@


# 1.35 19-Feb-2006 grange

Fix acpi_enter_sleep_state() by replacing current ugly hack
with mostly correct sleeping state transition code.

ok brad@ marco@


# 1.34 19-Feb-2006 marco

jordan:
Enable interrupts.
Added peripheral code to make this work.

marco:
Replace softintr and other types of interrupts with a handler with context.
Fixed interrupt magic for GPEs.
Some KNF

This is work done by jordan and I but has to be a single commit.


# 1.33 17-Feb-2006 marco

Remove earlier introduced delays. We really only need them during autconf.

ok jordan@


# 1.32 17-Feb-2006 marco

Add a generic delay function as per the ACPI spec.

from jordan@


# 1.31 17-Feb-2006 marco

Use tsleep instead of delay when !cold.


# 1.30 17-Feb-2006 marco

Argh, we need delays between reads/writes. The backend reads (i2c most likely)
are so slow that values come back as 0 sometimes. We have to do this
despite the spec saying we shouldnt have to. Guess having a faster
parser than others can hurt sometimes.

Idea from jordan.


# 1.29 17-Feb-2006 jordan

Fixed code for handling _INI
Rearranged debug statements
ok marco@


# 1.28 16-Feb-2006 jordan

Added stub for calling _INI
Fixed aml_dowhile for NULL return
ok marco@


# 1.27 16-Feb-2006 jordan

Fixed parser/evaluator to retrieve battery info+state
Cleanup of parser
Dell laptops return battery status
ok marco@


# 1.26 06-Feb-2006 brad

fix a typo so this will compile on amd64.


# 1.25 04-Feb-2006 marco

When ACPI_ENABLED wasn't set but the devices were it had an unknown variable.

Pointed out by ian@.


# 1.24 03-Feb-2006 jordan

Rewrote aml parser yet again...
Fixed bus_space_map for i386/amd64
Added _BST and _BIF to acpibat to get status/info
ok marco@


# 1.23 20-Jan-2006 jordan

More evaluator changes
Removed old evaluator code
Fixed _HID value
ok marco@


# 1.22 19-Jan-2006 jordan

Added support for read/write of PCI Config space
Help jason@
ok marco@


# 1.21 18-Jan-2006 jordan

Added new evaluation routines
Added helper methods for buffer/field evaluation
Fixed broken indentation on dsdt.c

ok marco@


# 1.20 17-Jan-2006 jordan

Major overhaul of the aml parser. Evaluater temporarily disabled.

ok marco@


# 1.19 06-Jan-2006 grange

Check for ACPI control availability only if ACPI_ENABLE defined.


# 1.18 06-Jan-2006 grange

Informative error messages on attach, prettier dmesg.


# 1.17 06-Jan-2006 grange

printf -> dnprintf conversion for debug messages.


# 1.16 05-Jan-2006 grange

Find SLP_TYPx values and store them for future use in
acpi_enter_sleep_state().


# 1.15 05-Jan-2006 grange

Random KNF and cleanup


# 1.14 05-Jan-2006 grange

Provide symbolic names for the acpi sleeping states and use it.


# 1.13 28-Dec-2005 marco

Moved DEVNAME to a acpidev.h

ok jordan@


# 1.12 18-Dec-2005 sturm

add back comment about wrong code

ok marco


# 1.11 16-Dec-2005 marco

Remove test for kbd_reset. Prompted deraadt@, ok jordan@


# 1.10 16-Dec-2005 jordan

Added hooks for powerdown/power button press


# 1.9 16-Dec-2005 jordan

Cleaned up common power management register i/o
Added enter sleep state ioctl
Hooked power button interrupt
ok marco@


# 1.8 16-Dec-2005 marco

Remove autoconf chatter


# 1.7 16-Dec-2005 jordan

Moved DSDT AML parsing into parent ACPI driver
ok marco@


# 1.6 14-Dec-2005 marco

Add AC device glue.


# 1.5 14-Dec-2005 marco

Adjust debug printing levels to something more sane.


# 1.4 13-Dec-2005 marco

Add glue to attach devices to apci.


# 1.3 07-Dec-2005 marco

KNF


Revision tags: OPENBSD_3_8_BASE
# 1.2 10-Jul-2005 grange

Spacing cleanup.


# 1.1 02-Jun-2005 tholo

Start on a basic ACPI framework -- does not do much more than read out the
ACPI tables into kernel memory and attach ACPI and HPET timers currently.

In order to test this code, enabling the devices in GENERIC as well as
the ACPI_ENABLE option is needed. This code does not do any thermal
control yet, so this should be done with care depending on the platform.

In the tree so more people can contribute to making this more fully
featured.

Ok niklas@ grange@ tedu@


# 1.405 12-Jan-2022 patrick

Make acpi_getpropint() return uint64_t, as ACPI integers are in fact that
wide and some _DSD properties depend on it.

ok kettenis@


# 1.404 04-Jan-2022 patrick

acpi_getprop() needs to actually make sure that we're looking at the
correct property. While there adjust acpi_getpropint() as well to
increase similarity with acpi_getprop().

ok kettenis@


# 1.403 16-Dec-2021 anton

Attach com over acpi on amd64. Some hardware uses a different interrupt
assignment compared to the the legacy one supported by com over isa.
This causes the console to halt once userland takes over as no
interrupts are received. The actual address and irq can be read from
ACPI, kettenis@ already added support for arm64 which paved the way for
amd64.

Some consoles that previously attached over isa are now expected to
attach over acpi.

Thanks to patrick@ for testing on arm64.

ok kettenis@


# 1.402 07-Dec-2021 kettenis

Fix parsing of SR_IRQ resource descriptors.

ok patrick@, anton@


# 1.401 02-Nov-2021 deraadt

knf


# 1.400 30-Oct-2021 deraadt

Change hw.perfpolicy=auto by default, at startup. If the system has AC
power connected (default is yes when no driver differentiates) then default
to 100% performance. On battery, use the existing auto algorithm (which is
admittedly somewhat unrefined).
This change overrides the system/BIOS speed and puts OpenBSD in control.
As this happens very early during boot, besides speedups in all usage usage
patterns, some surprises: unhibernate and sysupgrade times are cut in half.
note: on a few architectures, the setperf fn pointer is changed late, and
thus the auto algorithm stops timeing out. kettenis and i will look for
a solution.
in snaps for more than a week.
ok kettenis


Revision tags: OPENBSD_7_0_BASE
# 1.399 20-Jul-2021 mlarkin

Fix trailing whitespace in a few places.

No code change.


# 1.398 09-May-2021 kettenis

Some x86 machines advertise the "hardware reduced" ACPI feature, advertise
S4 and S5 support, but fail to populate the SLEEP_CONTROL_REG and
SLEEP_STATUS_REG descriptions in the FADT. An example of such a machine
is the ASUS Zenbook 14 UM433DA. Any attempt to powerdown the machine
will result in a kernel crash. It turns out that using the legacy ACPI PM
registers works fine on this machien. So fall back on those registers
if SLEEP_CONTROL_REG and/or SLEEP_STATUS_REG aren't provided.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_9_BASE
# 1.397 15-Mar-2021 patrick

Add acpi_iommu_device_map(), which replaces the DMA tag with one that
is blessed with IOMMU magic, if available. This is mainly for arm64,
since on amd64 and i386 the IOMMU only captures PCIe devices, as far
as I know, which uses the pci_probe_device_hook(). This though is for
non-PCI devices attached through ACPI.

ok kettenis@


# 1.396 07-Mar-2021 patrick

Pass standard DMA tag to acpi(4) table drivers.

ok kettenis@


# 1.395 27-Dec-2020 deraadt

acpi_map_address() cannot be wrapped by SMALL_KERNEL anymore, as
it is used by acpihpet.c


# 1.394 25-Dec-2020 visa

Refactor klist insertion and removal

Rename klist_{insert,remove}() to klist_{insert,remove}_locked().
These functions assume that the caller has locked the klist. The current
state of locking remains intact because the kernel lock is still used
with all klists.

Add new functions klist_insert() and klist_remove() that lock the klist
internally. This allows some code simplification.

OK mpi@


# 1.393 17-Dec-2020 kettenis

Fix some issues with referencing named ACPI nodes from Packages.
These references need to be resolved at runtime rather than when
they're parsed such that they pick up the right values for those nodes
which can be changed when for example _INI methods run.

The current approach is to replace these reference with a string that
names the node in question. The problem with that is that packages
can also contain normal strings. Which means that we need special
code that depends on the context where the package is used.

This diff takes a different approach by retaining a reference when
parsing. Code that uses a package will need to resolve this reference
but there is no ambiguiety anymore.

ok patrick@


# 1.392 05-Dec-2020 kettenis

Add support for the _EVT method to the GPIO support code.

ok patrick@


Revision tags: OPENBSD_6_8_BASE
# 1.391 27-Aug-2020 jmatthew

Reorder the acpi attach process so we look at Processor() CPU nodes
after Device() ones, since we should prefer the newer node type.
If we see any Device() nodes, don't attach acpicpu(4) to any Processor()
nodes if they're also present. This also makes acpitz(4) and
acpipwrres(4) devices attach slightly later.

ok kettenis@, also tested by jmc@


# 1.390 16-Aug-2020 gnezdo

Remove sc_maxgpe from acpi_softc which was never read from

Remove a dead conditional from acpi_init_gpes while there.

ok jcs@


# 1.389 21-Jul-2020 deraadt

acpi can use IPL_BIO (a low interrupt) since it only enqueues operations for
later processing. The use of a high interrupt will predate suspend/resume
efforts, we had to redesign acpi to be non-reentrant obviously
discussed with kettenis, in snaps for more than a week


# 1.388 24-Jun-2020 cheloha

kernel: use gettime(9)/getuptime(9) in lieu of time_second(9)/time_uptime(9)

time_second(9) and time_uptime(9) are widely used in the kernel to
quickly get the system UTC or system uptime as a time_t. However,
time_t is 64-bit everywhere, so it is not generally safe to use them
on 32-bit platforms: you have a split-read problem if your hardware
cannot perform atomic 64-bit reads.

This patch replaces time_second(9) with gettime(9), a safer successor
interface, throughout the kernel. Similarly, time_uptime(9) is replaced
with getuptime(9).

There is a performance cost on 32-bit platforms in exchange for
eliminating the split-read problem: instead of two register reads you
now have a lockless read loop to pull the values from the timehands.
This is really not *too* bad in the grand scheme of things, but
compared to what we were doing before it is several times slower.

There is no performance cost on 64-bit (__LP64__) platforms.

With input from visa@, dlg@, and tedu@.

Several bugs squashed by visa@.

ok kettenis@


# 1.387 10-Jun-2020 jca

Give userland a consistent battery and A/C state after resume

Previously we refreshed the state of acpiac(4) and acpibat(4)/acpisbs(4)
by queueing fake ACPI events to be processed by the acpi thread.
acpibat(4) using ACPIDEV_POLL meant that its status could be refreshed
first, resulting in an APM_POWER_CHANGE event sent to userland before
the state of acpiac(4) was up to date. Because of this, apmd(8) could
see A/C unplugged when it was in fact plugged, possibly leading to
a wrong decision.

Instead of using fake ACPI events, refresh the status of acpiac(4) and
acpibat(4)/acpisbs(4) right away at DVACT_WAKEUP time. To reuse the
existing refresh code of those drivers, make their ACPI *_notify()
handlers responsible for sending APM_POWER_CHANGE events instead. Also
queue an APM_POWER_CHANGE event right after resume to retain existing
behavior.

This fixes the consistency of data shown to userland, and makes those
drivers less special.

Initially discussed last year (!) with mpi@ at Miod's place.
Feedback and ok jcs@, deraadt@ agrees with the use of DVACT_WAKEUP here.


# 1.386 29-May-2020 deraadt

dev/rndvar.h no longer has statistical interfaces (removed during various
conversion steps). it only contains kernel prototypes for 4 interfaces,
all of which legitimately belong in sys/systm.h, which are already included
by all enqueue_randomness() users.


# 1.385 14-May-2020 kettenis

Bring back the acpi(4) changes. This time around only use the ACPI _CRS
information on ACPI 5.0 and later.

ok krw@, patrick@


# 1.384 11-May-2020 jca

More accurate remaining power reporting with disparate batteries

Overall remaining power was computed as the average of the remaining
power of each battery, in percents. This doesn't work with batteries of
different capacity, so use the total capacity and remaining power
instead.

Reported by lidstah, successful tests by lidstah and benno@, ok kettenis@


# 1.383 08-May-2020 kettenis

Move parsing of _CRS into acpi(4) and pass the parsed address and interrupt
information in struct acpi_attach_args.

ok mpi@


Revision tags: OPENBSD_6_7_BASE
# 1.382 14-Apr-2020 kettenis

Implement acpi_getprop(), the ACPI equivalent of OF_getprop().

ok patrick@


# 1.381 12-Apr-2020 kettenis

Add support for the _CCA method. This method indicates whether DMA is
cache-coherent or not. To implement this, acpi(4) gets two bus_dma tags
and passes the appropriate one when attaching devices based on _CCA.
On i386/amd64, where for all practical purpose DMA is always cache-coherent,
the two tags are the same. But on arm64 they are distinct.

ok patrick@


# 1.380 07-Apr-2020 visa

Abstract the head of knote lists. This allows extending the lists,
for example, with locking assertions.

OK mpi@, anton@


# 1.379 03-Apr-2020 mpi

Fix incoherencies of errors returned by various *kqfilter routines.

EOPNOTSUPP is returned when there's no "filterops" for a given fd.
EINVAL when the requested filter isn't supported by the "filterops".
ENXIO means the underlying device is no longer valid/present.

ok visa@


# 1.378 20-Feb-2020 visa

Replace field f_isfd with field f_flags in struct filterops to allow
adding more filter properties without cluttering the struct.

OK mpi@, anton@


# 1.377 31-Dec-2019 visa

Use C99 designated initializers with struct filterops. In addition,
make the structs const so that the data are put in .rodata.

OK mpi@, deraadt@, anton@, bluhm@


# 1.376 31-Dec-2019 mpi

Convert infinite sleeps to {m,t}sleep_nsec(9).

ok kn@


# 1.375 31-Dec-2019 jsg

include sys/sensors.h for sensor use
don't depend on indirect inclusion from acpidev.h


Revision tags: OPENBSD_6_6_BASE
# 1.374 07-Sep-2019 kettenis

Revert acpipci(4) changes. Too many systems are broken. We'll try again
after 6.6 as been released. The acpireg.h change stays behind.


# 1.373 28-Aug-2019 kettenis

Use ACPI information to attach PCI busses like we do on arm64. There are a
few additional quirks though, and attaching the PCI busses is delayed to
replicate the existing code more closely. That may be changed in the
future. Also tweak how we handle MSI support and respect to ACPI flag
that says we shouldn't attempt to use MSIs.

Some fallout is expected.

ok patrick@


# 1.372 27-Aug-2019 deraadt

On amd64 the bootup and resume paths for SP/BP/SP were not performing
msr/pat, clock-startup, firmware upgrade, fpu initialization, and cpu
soft-state initialization in the same order. That kind of chaos is
crazy and fragile. Improve the acpi resume call sequence and unify order
of the operations.


# 1.371 02-Jul-2019 jcs

acpi_set_gpehandler: turn edge arg into flags, add support for
passing GPE_DIRECT which directly executes the supplied callback and
clears a level interrupt rather than going through acpi_taskq which
can be slow when other driver callbacks are in the queue.

This should not be used for GPE handlers that need to do anything
ACPI-related.

ok kettenis


# 1.370 10-Jun-2019 kettenis

Print proper ACPI version number.

ok mlarkin@


# 1.369 08-Jun-2019 kettenis

When establishing the mapping between ACPI device nodes and PCI devices,
skip PCI host bridges and devices that aren't there.

ok patrick@


# 1.368 07-Jun-2019 kettenis

Introduce acpi_getsta() and use it to eliminate some duplicated code.

ok patrick@


# 1.367 12-May-2019 kettenis

Make sure the extended PM register descriptions actually exist before
using them. Fixes machines with ancient ACPI 1.0 (such as recent
hypervisors).

ok deraadt@


# 1.366 12-May-2019 kettenis

Add support for mapping ACPI PM registers using the "extended" gasio
representation from the FADT. Mostly fixes the Lanner NCA-1510.

ok lteo@, mlarkin@, deraadt@


# 1.365 11-May-2019 lteo

Fix incorrect error message.

ok kettenis@


# 1.364 08-May-2019 tedu

free size. the allocation is the header plus the length.


Revision tags: OPENBSD_6_5_BASE
# 1.363 04-Apr-2019 kettenis

Avoid potential null-pointer dereference. Found by Kent R. Spillner.

ok deraadt@, jsg@


# 1.362 20-Jan-2019 jsg

fix SMALL_KERNEL build after changes in rev 1.361
reported by naddy@ ok deraadt@


# 1.361 19-Jan-2019 tedu

Add a pwraction sysctl that controls what the power button does on acpi.
By default, nothing changes -- shutdown is initiated. But allows turning
power button into a sleep button if desired.
(grudging) ok from a few parties


# 1.360 26-Oct-2018 kettenis

Add acpipci(4) on amd64. For now this only calls the PCI-specific _OSC
method to let the ACPI implementation know what features we support.


Revision tags: OPENBSD_6_4_BASE
# 1.359 30-Aug-2018 kettenis

Move softbutton check before the check that skips attaching certain acpi
devices. Fixes regression caused by matching on _CID in addition to
matching on _HID.

ok matthieu@, mlarkin@


# 1.358 25-Aug-2018 kettenis

Match ACPI devices based on _CID if no match for _HID is found.

ok mpi@


# 1.357 19-Aug-2018 kettenis

Add support for multiple PCI segments. Only really implemented for arm64
for now as amd64/i386 firmware still caters for legacy OSes that only
support a single PCI segment.

ok patrick@


# 1.356 03-Aug-2018 kettenis

Let ahci(4) match on _CLS instead of _HID when attaching at acpi(4). Avoids
having to add many more _HID entries to the match table.

ok deraadt@, mlarkin@


# 1.355 10-Jul-2018 kettenis

Make legacy interrupts work in acpipci(4).

ok patrick@


# 1.354 05-Jul-2018 kettenis

Add acpipci(4), a driver that supports generic ECAM-compatible PCI host
bridges based on information provided by ACPI.

ok mlarkin@


# 1.353 04-Jul-2018 kettenis

Properly pass around the PCI "chipset tag" in acpi(4) and refactor
acpimcfg(4) to call an MD initialization functions that sets up a tag for
PCI ECAM.

ok guenther@, mlarkin@, krw@


# 1.352 01-Jul-2018 kettenis

Move acpi(4) attach glue into acpi_machdep.c.

ok guenther@, deraadt@


# 1.351 01-Jul-2018 kettenis

Add support for _DSD Device Properties.

ok mlarkin@


# 1.350 30-Jun-2018 kettenis

Add intr_enable() function, intended for MI use to amd64 and i386 and use
this in the acpi(4) suspend/resume code paths.

ok deraadt@


# 1.349 29-Jun-2018 kettenis

Use <stdint.h> types.

ok mpi@, jung@, krw@, deraadt@


# 1.348 29-Jun-2018 mlarkin

ACPI: Allow (with warning) GPE handler reassignment, instead of returning
a failure code.

ok kettenis


# 1.347 26-Jun-2018 mlarkin

knf


# 1.346 26-Jun-2018 kettenis

Let acpi(4) pass down the bus dma tag.

ok deraadt@


# 1.345 25-Jun-2018 kettenis

Reorganize acpi(4) code a bit in preparation for upcoming arm64 support.

ok deraadt@


# 1.344 20-May-2018 kettenis

Wake up the acpi thread after scheduling a gpio event.

ok mpi@


# 1.343 17-May-2018 kettenis

Sprinkle some #ifndef SMALL_KERNEL such that RAMDISK kernels cuild again.

pointed out by stsp@


# 1.342 17-May-2018 kettenis

Implement GenericSerialBus OpRegion support.

ok mlarkin@


# 1.341 27-Mar-2018 kettenis

Add acpicmos(4), a driver that implements SystemCMOS OperationRegion
access support. This fixes machines where the AML doesn't check whether
support for this OperationRegion type has been registered by the OS.

ok mlarkin@


Revision tags: OPENBSD_6_3_BASE
# 1.340 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.339 10-Feb-2018 deraadt

Syncronize filesystems to disk when suspending. Each mountpoint's vnodes
are pushed to disk. Dangling vnodes (unlinked files still in use) and
vnodes undergoing change by long-running syscalls are identified -- and
such filesystems are marked dirty on-disk while we are suspended (in case
power is lost, a fsck will be required). Filesystems without dangling or
busy vnodes are marked clean, resulting in faster boots following
"battery died" circumstances.
Tested by numerous developers, thanks for the feedback.


# 1.338 08-Feb-2018 deraadt

After flushing disks, try again to release all memory. We don't need to
save dirty memory to the hibernate space.


# 1.337 08-Feb-2018 deraadt

Toss all releaseable memory, because fragmentation can get in the way
of allocating the hibernate playpen.


# 1.336 08-Feb-2018 deraadt

sr_quiesce() is a new approach for ensuring that softraid drains
output to the disks.

This is part of a larger suspend/resume filesystem-safety diff, which
has been worked on for a couple of months already. Tests by job, krw,
beck, benno, and others. Sometimes even by snapshot users...


# 1.335 29-Nov-2017 kettenis

Add support for GPIO-signaled events.

ok mlarkin@


# 1.334 16-Nov-2017 jcs

Add PCI attachment for dwiic(4) needed by Intel 100 Series machines

ok kettenis


# 1.333 14-Nov-2017 kettenis

Add a few more PNP IDs to the skip lists.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.332 17-Aug-2017 stsp

Make dwiic(4) attach its ACPI parent dependencies.
This change makes the Asus E200HA keyboard work.

Original analysis and patch by Cesare Gargano
ok kettenis@


# 1.331 09-Aug-2017 dcoppa

Add "PNP0303" (8042 PS/2 Controller) to acpi_isa_hids[]

ok kettenis@


# 1.330 22-Jul-2017 jcs

acpibat: look for _BIX first before falling back on _BIF

ACPI 4.0 deprecated _BIF for battery status so some newer machines
are exporting _BIX which extends _BIF's package with a few extra
fields. When using _BIX, export the new discharge cycle count as a
sensor.

feedback from kettenis and anton
ok deraadt


# 1.329 20-Jul-2017 kettenis

Start a list of isa(4) device names such that we can avoid printing them
as "not configured".

ok mikeb@, millert@, deraadt@


# 1.328 08-Apr-2017 deraadt

Sensors are run as callbacks inside tasks. During suspend a sensor
could be running inside a driver that will be force-detached, or due
to tsleep end up disrupting the softstate/hardstate contract. At
suspend time, quisce all these callbacks by waiting for completion.
This issue has never been observed for real, but may be implicated in
suspend/resume failures.
ok kettenis guenther mlarkin


# 1.327 08-Apr-2017 deraadt

Two prototypes for acpi_maptable; merge to one place


Revision tags: OPENBSD_6_1_BASE
# 1.326 27-Mar-2017 deraadt

Now that hibernate_alloc() only has clean success/failure, don't
need to call hibernate_free() to clean up a partial mess.
ok mlarkin kettenis


# 1.325 13-Mar-2017 deraadt

allow ramdisks to build; ok jcs


# 1.324 12-Mar-2017 jcs

try to attach acpisbs first and if successful, don't attach acpibat


# 1.323 02-Mar-2017 natano

Add a new sysctl machdep.lidaction. The sysctl works as follows:

machdep.lidaction=0 # do nothing
machdep.lidaction=1 # suspend
machdep.lidaction=2 # hibernate

lidsuspend is just an alias for lidaction, so if you change one, the
other one will have the same value. The plan is to remove
machdep.lidsuspend eventually when people have upgraded their
/ets/sysctl.conf.

discussed with deraadt, who came up with the new MIB name
no objections mlarkin
ok stsp halex jcs


# 1.322 28-Feb-2017 natano

Untangle abstract sleep modes from ACPI sleep states. This paves the way
for further work in the area.

idea and ok deraadt
ok mlarkin


# 1.321 22-Feb-2017 jcs

add acpisbs, an acpi smart battery subsystem driver reading data
over smbus

currently disabled because it conflicts with acpibat

ok deraadt, kettenis


# 1.320 14-Jan-2017 kettenis

Revert aml_rdpciaddr changes; breaks several machines that were working
before.


# 1.319 11-Jan-2017 jsg

Increase the size of the buffers used for _HID and _CID strings to be
able to fit "Hyper_V_Gen_Counter_V1".

ok mlarkin@ deraadt@


# 1.318 08-Jan-2017 kettenis

Make aml_rdpciaddr() work for for devices behind bridges. The code now
uses the mapping between AML nodes and PCI devices that we establish.
Because _INIT methods may end up calling aml_rdpciaddr(), make sure we
create that mapping early. Also handle devices that aren't actually present.
These devices are now included in the mapping and reads will return an
all-ones patterm whereas writes are a no-op..

ok mlarkin@


# 1.317 25-Oct-2016 pirofti

Add the resource index to aml_parse_resource() callback function.

This is needed by an upcoming acpiec commit that handles machines breaking
the current ACPI specifications.

Change suggested by and ok kettenis@, guenther@


# 1.316 18-Sep-2016 guenther

If the APIC table (MADT) isn't present then ci_acpi_proc_id won't be set
when acpicpu tries to attach, leading to a NULL dereference. The ACPI
processor object on the Dells where this was reported is bogus and useless
anyway so apply the same test when deciding whether to attach to processor
objects as acpicpu would when matching them to cpus.

Problem reported by Eivind Eide (xenofil (at) gmail.com) and
Johan Huldtgren (johan+openbsd-bugs (at) huldtgren.com).
Testing by Eivind Eide.
ok kettenis@ mlarkin@


# 1.315 03-Sep-2016 naddy

Write the system time back to the RTC every 30 minutes.
This fixes the problem that long-running machines which were not
shut down properly would reboot with a badly offset system time.

hints and ok kettenis@


# 1.314 31-Aug-2016 mlarkin

Don't put a device in the wake device list if _STA says it's not there.

Fixes at least one machine that woke up from sleep / poweroff immediately.

ok kettenis@, tested by kettenis and martijn@


# 1.313 28-Jul-2016 kettenis

Store the acpi processor ID/UID in struct cpu_info, and use it to attach
acpicpu(4) drivers to the right cpu(4).

ok mlarkin@, guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.312 10-Jun-2016 kettenis

The power management timer has been made optional in ACPI 5.0A, so don't
attempt to attach acpitimer(4) if the timer isn't present.

Pointed out by jcs@

ok jcs@


# 1.311 08-May-2016 kettenis

Make sure devices listed by a device's _DEP method are attached before
we attach the device itself. The _DEP methode, introduced in ACPI 5.0,
is a hint that the device in question depends on OpRegion support from
the devices listed by _DEP, so we have to order them the proper way.
To prevent us from attaching those devices again when we encounter them
later walking down the device tree, keep track of our attempts to
attach a device driver to them using aflag in the aml_node structure.

ok guenther@


# 1.310 25-Apr-2016 pirofti

We attach acpihpet(4) and acpiec(4) without using the HID, so do not
report it in the dmesg.

OK deraadt@, mlarkin@.


# 1.309 02-Apr-2016 kettenis

Reduce dmesg spam by knocking out some acpi hardware IDs that don't need a
driver.

ok mpi@


# 1.308 29-Mar-2016 kettenis

Check if a device is present (using _STA) in acpi_foundhid. This prevents
us from reporting devices as "not configured" that aren't actually present,
and allows us to remove duplicated code from several drivers.

ok deraadt@, ok jsg@


# 1.307 29-Mar-2016 kettenis

Implement a cleaner way to prevent acpivout(4) from attaching if we have
a platform-specific method of controlling the backlight.

ok millert@, mlarkin@, jung@


# 1.306 28-Mar-2016 kettenis

Adding each and every new acpi device driver to acpi_foundhid() is
gettinga bit of the burden. And it really isn't how our autoconf
framework is supposed to work for busses that can be enumerated. All
the drivers already check for a matching _HID in their attach
function. So we can just drop the checks in acpi_foundhid() and let
the drivers sort themselves out.

Print a "not configured" line for devices for which we don't attach adriver.
This may be a bit much as there are devices for which we will not have a
driver. This will be removed if it causes too much dmesg spam.

ok deraadt@, mlarkin@


Revision tags: OPENBSD_5_9_BASE
# 1.305 17-Jan-2016 jsg

Take acpi_foundhid() outside of #ifndef SMALL_KERNEL so the ramdisk
kernel can find and attach dwiic(4) and sdhc(4).


# 1.304 17-Jan-2016 jsg

remove an unused global variable


# 1.303 14-Jan-2016 kettenis

Attach dwiic(4) on the Intel Bay Trail i2c controllers as well.


# 1.302 12-Jan-2016 kettenis

Improve emulation of PM registers on Hardware-redcued ACPI by emulating
SPL_TYPx, SLP_EN and WAK_STS using the new SLEEP_STATUS_REG and
SLEEP_CONTROL_REG registers. Makes the transition into S5 (powerdown) work
on the ASUS X205TA.

ok deraadt@, mlarkin@


# 1.301 12-Jan-2016 jcs

Add dwiic, a driver for the Synopsys DesignWare i2c controller found
on the Samsung ATIV Book 9 laptop. This initial version only
supports ACPI config/attachment.

Add ihidev, a HID-over-i2c driver largely based on uhidev. dwiic
handles attaching ihidev devices found in ACPI.

Add ims, a HID-over-i2c mouse/trackpad driver to get basic cursor
and button functionality from HID-compliant i2c trackpads.

ok kettenis deraadt


# 1.300 11-Jan-2016 kettenis

Make sdhc(4) attach to hardware IDs "80860F14" and "PNP0FFF".


# 1.299 09-Jan-2016 kettenis

Initial support for Hardware-Reduced ACPI. This skips the legacy mode checks
by emulating the PM_CNT register such that SCI_EN is always enabled. It also
skip attaching acpitimer(4) since the relevant registers are no longer there.

ok deraadt@, mlarkin@


# 1.298 06-Jan-2016 kettenis

Follow the spec and check the SCI_EN bit to see if ACPI has already
been enabled and don't bother enabling it again if it is. This should fix
systems that don't implement legacy mode and therefore set ACPI_ENABLE and
ACPI_DISABLE to zero.

ok mlarkin@, yasuoka@


# 1.297 23-Nov-2015 reyk

Back out the CRITICAL change, turn it back from 10% to 25%. Some
people with old/broken batteries didn't like this change, and the
underlying heuristics are wrong.


# 1.296 22-Nov-2015 reyk

The "CRITICAL" APM battery level, that is exported/faked by the ACPI
stack, is currently set to 25%. With 25% battery left, a typical
modern laptop will have at least one hour runtime left. It is an
arbitrary value, and other APM power layers in the kernel set CRITICAL
to something like 5% or 10%, so set it to 10% for now.

OK mlarkin@


# 1.295 28-Sep-2015 deraadt

In low-level suspend routines, set cold=2. In tsleep(), use this to
spit out a ddb trace to console. This should allow us to find suspend
or resume routines which break the rules. It depends on the console
output function being non-sleeping.... but that's another codepath which
should try to be safe when cold is set.
ok kettenis


# 1.294 06-Sep-2015 deraadt

clarify malloc arguments (base + length...)


# 1.293 28-Aug-2015 deraadt

fairly simple sizes for free(); ok tedu


# 1.292 20-Aug-2015 kettenis

Respect the access size when reading or writing to pci config space.
This version also makes sure writes are properly aligned, preventing
the panics seen with the previous incarnation of this change.

Fixes battery status passthrough in vmware, brightness keys on my x220
and who knows what else.

ok mlarkin@, deraadt@


Revision tags: OPENBSD_5_8_BASE
# 1.291 04-Aug-2015 deraadt

If we are setting ACPI_PM1_BM_RLD do it only once, and remember to
repeat this action at resume time.
ok mlarkin guenther


# 1.290 29-Jul-2015 deraadt

Something subtle in previous revision is locking up suspend or booting
on some laptops. Mailing lists threads mention x220 and Asus machines.
Reverting for now.


# 1.289 21-Jul-2015 kettenis

iRespect the access size when reading or writing to pci config space.
Fixes battery status passthrough in vmware and who knows what else.

Tested by many.

ok mlarkin@, deraadt@


# 1.288 17-Jul-2015 mlarkin

fix comments


# 1.287 26-Mar-2015 jsg

Use the 0x10 extended capability to determine if the trackstick
buttons are wired to the trackpad and need to be re-routed to the
trackstick. Without this change the buttons on 2015 thinkpads
get picked up as extended buttons that show up as scroll up/down.

Remove the X1 Carbon 2015 (LEN0048) and X250 (LEN0046) from the
top button area/soft buttons quirks list. Also avoid using the
quirk list entirely if the capability bit is set.

Based on work from jcs@ with feedback from mpi@
ok mpi@ jcs@


# 1.286 20-Mar-2015 kettenis

The ACPI standard says that a PCI function number of FFFF can be used to refer
to all functions on a device. This makes no sense in the context of accessing
PCI config space. Nevertheless stsp@ has a machine with AML that tries to
do config space reads from a device where _ADR encodes function number FFFF.
This is almost certainly buggy AML, but since Linux (and presumably Windows)
boot just fine on this machine we need to work around this. Linux effectively
treats function FFFF as function 7, which isn't present on this machine.
Instead we simulate a read from a non-existent device by returning a bit
pattern of all ones. Writes to function FFFF will still panic.

ok mlarkin@


# 1.285 16-Mar-2015 deraadt

Reduce use of SMALL_KERNEL in acpi, bringing acpiec driver and more gpe
handling into RAMDISK. This is now possible because the install media
has ample room. The goal is to reduce special cases where we may be
skipping (unknown) important operations...
ok mlarkin kettenis


# 1.284 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.283 07-Feb-2015 deraadt

New framework that allows hibernate to pass in entropy from it's fresh
boot.
ok mlarkin


# 1.282 06-Feb-2015 mlarkin

Fix a hibernate crash on some machines due to unmapping a page that
may not have been mapped previously (in the failure to hibernate case).

Also ensure that the lowmem ptp is mapped in all cases (not just MP).

ok kettenis


# 1.281 17-Jan-2015 deraadt

resume_randomness() before spinning up other cpus...


# 1.280 15-Jan-2015 jsg

Define a new wscons mouse type for Synaptics clickpad devices that lack
physical buttons. This will be used if the acpi pnpid for the mouse
matches a list provided by Synaptics found in Linux.

Combined with the xenocara changes this will remove the need for an xorg
config file for the x240/t440/t540 generation of thinkpads.

ok matthieu@ shadchin@ kettenis@


# 1.279 11-Jan-2015 kettenis

Tolerate checksum errors in ACPI tables (but complain about them). Apparently
there are machines out there with broken BIOSen with such checksum errors in
thier RSDT tables. Windows runs fine on these machines, so it probably does
not check. Linux ignores checksum errors as well (and prints a warning).

ok deraadt@, krw@


# 1.278 18-Dec-2014 deraadt

suspend and resume the RNG (at what looks like the right spots)
ok reyk djm


# 1.277 09-Dec-2014 doug

Replace some malloc(n*size,...) calls with mallocarray().

ok tedu@ deraadt@


# 1.276 06-Dec-2014 mpi

If "machdep.lidsuspend" is not set, do not go back to sleep if the lid
is still closed when resuming. This allows docked laptop to be resumed
without opening them.

ok mlarkin@


# 1.275 23-Nov-2014 mlarkin

If a machine has no _LIDs, then none can be open. This diff fixes a problem
where machines without any _LIDs were going back to sleep after hibernate
due to the recent suspend-if-lid-closed-on-resume diff. Noticed on qemu,
which doesn't have any _LIDs.

discussed with deraadt


# 1.274 18-Nov-2014 krw

Nuke yet more obvious #include duplications.

ok miod@


# 1.273 08-Nov-2014 mlarkin

If resuming from sleep (zzz/ZZZ) and the lid is still closed, go back to
sleep. This prevents accidental lid flex or slight opening in a backpack
from waking the machine up and leaving it resumed (powered on).

ok deraadt@


# 1.272 26-Sep-2014 kettenis

Rework piglet and pig allocation. Currently the piglet gets allocated
deep down in the suspend path, where it is really hard to recover from
allocation failure. So allocate the piglet early on in the suspend path.
Also change the piglet and piglet allocation functions to use km_alloc(9)
instead of doing pmemrange magic. This removes a bunch of code which, in the
case of the piglet allocation, is broken since it results in a NULL pointer
dereference. Also switch the piglet allocation to not wait. If we can't
allocate 16MB of phys contig memory on a halfway modern machine we're almost
certainly under a lot of memory pressure and we're better off not trying to
hibernate anyway.

ok mlarkin@


# 1.271 25-Sep-2014 kettenis

Bring back rev 1.249. Now that mp_setperf() has been fixed, it should be
safe to restore perflevel when resuming. Should fix the issues people
see with the processor running at full speed heating up the machine while
the apm command reports that the CPUs are running at a low speed.

ok dcoppa@, deraadt@


# 1.270 23-Sep-2014 kettenis

Release the acpi lock around calls to wsdisplay_suspend() and
wsdisplay_resume(). The X server may adjust the display brightness just
before/after the VT switch when suspending/resuming. In fact, it may
already be blocked adjusting the display brightness for some reason
unrelated to the suspend request.

The VT switch request will eventually time out, but the delay is annoying
and the failed VT switch will lead to problems after resume.

Needed for a future update of the xf86-video-intel driver.

ok mlarkin@


# 1.269 19-Sep-2014 kettenis

Use config_suspend_all(9).


# 1.268 08-Sep-2014 kettenis

Clear the PME Status bit on PCI devices in response to Device Wake events.
Fixes the interrupt storm on the 2009 Mac mini with WOL enabled on its
nfe(4) interface.

ok mlarkin@


Revision tags: OPENBSD_5_6_BASE
# 1.267 20-Jul-2014 mlarkin

Support hibernating to softraid crypto volumes.

much help and ok from deraadt@


# 1.266 16-Jul-2014 mlarkin

Reenable hibernate RLE support and flush+zero all memory after unpack.


# 1.265 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.264 12-Jul-2014 kettenis

Add a function to drop all clean pages on the page daemon queues and call
it when we hibernate.

ok mlarkin@, miod@, deraadt@


# 1.263 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.262 11-Jul-2014 mlarkin

Flush the buffercache to 16MB on hibernate and restore its previous max
size (kern.bufcachepercent) on resume, for better hibernate performance.

ok beck@


# 1.261 10-Jul-2014 blambert

remove unneeded header

ok mlarkin@


# 1.260 01-Jul-2014 kettenis

Adding an additonal delay after each access to SystemMemory or SystemIO
during autoconf makes no sense.

ok deraadt@


# 1.259 23-Jun-2014 kettenis

SystemMemory is used to access memory mapped registers on some machines, so we
must use the bus_space(9) API to access it instead of memcpy(9). Also make
sure we properly align access to fields that need more than byte alignment.

ok guenther@


# 1.258 21-May-2014 mlarkin

Fix invalid / missing format specifiers in several dnprintf statements
(used only when ACPI_DEBUG is enabled)

From a diff on tech@ by Fabian Raetz, with a minor change by me, tested
on i386 and amd64 with ACPI_DEBUG enabled and disabled.


# 1.257 25-Apr-2014 mlarkin

Defer the free of the hibernate structures until later, when it is safe to
do so.

Freeing the hibernate structures too early leads to inadvertent calls into
splx when we aren't ready for them yet. Found when trapping splx calls in
the resume path.

discussed with deraadt@ at m2k14


# 1.256 13-Apr-2014 mlarkin

Fix a badly worded debug message that implied the entire machine was going
to sleep, instead of just the acpi thread going to sleep.


# 1.255 31-Mar-2014 mpi

Change the order of operations during a suspend/resume cycle and
call bufq_quiesce() after executing the DVACT_QUIESCE handlers.

This should be safe since no disk nor controller drivers have such
handler but it will allow us to detach sd(4) devices attached to a
USB bus.

Another benefit pointed out by kettenis@ is that drivers that need
to read a firmware from the disk should be able to do it at resume
time in a DVACT_WAKEUP handler.

ok kettenis@, deraadt@


# 1.254 13-Mar-2014 dlg

get rid of the assumption that the head of the alldevs list is the
"mainbus" device. this breaks when mpath is enabled because it
attaches before mainbus and therefore takes the head position.

have autoconf provide device_mainbus() which looks up mainbus_cd,
and use that instead.

discussed with deraadt who just wants mpath stuff to move forward
despite there being many ways to shine this particular turd.


Revision tags: OPENBSD_5_5_BASE
# 1.253 21-Feb-2014 deraadt

Do the lid control further on in the resume sequence, since this is
AML, and will do WAITOK operations. While cold, that is a bad idea.
We seem to be safe with respect to the spec, I do not see any event
debounce issues here.
ok kettenis, tested by many


# 1.252 20-Jan-2014 claudio

Instead of checking sc->sc_revision <= 1 check that the
fadt->hdr_revision <= 1. This is a better check since it checks
the version of the proper descriptor. This allows the alix APU
to reboot since those boards have currently a wrong sc->sc_revision.
Committing this now so that it can be tested easier.
With and OK jsg@ also fine by derradt@


# 1.251 24-Dec-2013 kettenis

If the FADT has its SMI_CMD set to zero, assume we're only ACPI-only hardware
and don't need to disable SMI ownership of the ACPI hardware registers.

ok mlarkin@


# 1.250 23-Dec-2013 kettenis

Revert previous commit. It makes resume hang on some MP systems running
GENERIC.MP.


# 1.249 16-Dec-2013 kettenis

Restore hw.setperf upon resume like we do for apm(4) on i386. Seems we need
to do this fairly late in the resume to avoid hangs.

ok deraadt@, mlarkin@


# 1.248 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


# 1.247 06-Nov-2013 mpi

Add support for Power Resources for Dx states and the necessary hook
for PCI devices. This hook should be called twice, before and after
changing the power state of a PCI device.

Before setting the device to the new state, the ACPI layer will notify
every power resources linked to the device for that state and make sure
they are turned "_ON". After changing the state of the device, it will
decrement the reference of every power resources linked to that device
for the old state and turn them "_OFF" if they are no longer referenced.

This fixes the no-USB after resume problem seen on various ThinkPad,
problem initialy diagnosed with Alexander Polakov.

ok kettenis@, deraadt@


Revision tags: OPENBSD_5_4_BASE
# 1.246 01-Jun-2013 mlarkin

Stop printing ACPI wakeup devices in dmesg after the 16th wakeup device.

Helps with VMware VMs that report hundreds of wakeup devices.

ok reyk@, deraadt@


# 1.245 31-May-2013 bluhm

Fix the build for a kernel without wd and pciide in its config.
OK deraadt@ tedu@


# 1.244 30-May-2013 deraadt

Enforce ca_activate tree-walks over the entire heirarchy for all events,
cleaning up some shutdown-hook related code on the way.
(A few drivers related to sparc are still skipped at kettenis' request)
ok kettenis mlarkin, tested by many others too


# 1.243 18-Apr-2013 deraadt

pin the acpi thread to the BSP, because AML and SMI cannot be trusted. I
was certain we were doing this already, but it turns out no...


Revision tags: OPENBSD_5_3_BASE
# 1.242 24-Dec-2012 guenther

In acpiioctl(), Return though the splx() path if suser() returns an error

ok deraadt@


# 1.241 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


# 1.240 04-Oct-2012 ehrhardt

Use information provided by ACPI to attach secondary PCI host bridges.

ok kettenis@


# 1.239 07-Sep-2012 kettenis

Add a new function called acpi_pci_min_powerstate() that returns the lowest
power state for a PCI device for the power state (currently S3 or S4) we're
transitioning into. This function is probably too simplistic; there is a lot
of (silly) complexity in the ACPI spec.

ok deraadt@


Revision tags: OPENBSD_5_2_BASE
# 1.238 13-Jul-2012 pirofti

Disable acpivideo(4) on ASUS laptops.

There are some models that panic whenever an event is received.
Some cope, some don't. In order to get this right acpiec(4) requires
major rework. Until then let machines boot.

Discussed with deraadt@.


# 1.237 11-Jul-2012 mlarkin

for now, restrict hibernate to root in the acpi ioctl kernel interface.
normal rules for controlling this interface through apmd still apply.

ok deraadt@


# 1.236 09-Jul-2012 deraadt

tiny little oops in untested hibernate block


# 1.235 09-Jul-2012 deraadt

clean up ioctl() case statements


# 1.234 09-Jul-2012 deraadt

acpi_disable_onegpe() is not used; ok pirofti


# 1.233 24-May-2012 kettenis

If _MIN > _MAX consider a WordBus resource to be invalid. Fixes a panic on
the IBM x3550-M4.

ok mikeb@


# 1.232 29-Mar-2012 mlarkin

Missing #ifdef in previous diff


# 1.231 29-Mar-2012 mlarkin

Prevent hibernation attempts on machines with unsupported disk controllers.
The same check is also performed later in the hibernate sequence, but by
then it's too late to cleanly unwind (presently).

ok deraadt


# 1.230 26-Mar-2012 deraadt

simplify conditionals in acpi_sleep_state()
ok mlarkin


# 1.229 26-Mar-2012 deraadt

hook in the hibernate request code; half of this diff is from mlarkin
ok mlarkin


Revision tags: OPENBSD_5_1_BASE
# 1.228 20-Sep-2011 deraadt

Very early in suspend while the clock is still ticking realtime,
save the clock back to the rtc. The zaurus already did this.
ok phessler miod kettenis


Revision tags: OPENBSD_5_0_BASE
# 1.227 02-Jul-2011 nicm

kqueue attach functions should return an errno or 0, not a plain 1. Fix
the obvious cases to return EINVAL and ENXIO.

ok tedu deraadt


# 1.226 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.225 16-Jun-2011 pirofti

Import acpitoshiba driver.

This is originally written by Hiroyuki Aizu, ported and integrated
in our tree by Javier Vazquez <javama4 AT gmail> with tweaks and
suggestions by me.

Import prodded by deraadt@ and the lack of testers on tech@.


# 1.224 27-Apr-2011 jcs

attach acpithinkpad to newer lenovo models like the x120e

ok deraadt@


# 1.223 22-Apr-2011 jordan

Fix uninitialzied variables and formatting strings (-Wxxx errors)


Revision tags: OPENBSD_4_9_BASE
# 1.222 02-Jan-2011 jordan

Re-add code for ACPI taskqueue, eliminates need for state vars in acpi softc
Serialize processing ACPI tasks for sleep/powerdown/GPE event
ok deraadt@


# 1.221 31-Oct-2010 guenther

Revert last commit: it breaks resume on Thinkpads
"then please back it out" deraadt@


# 1.220 26-Oct-2010 jordan

Add task queue for ACPI gpe and notify handlers
This fixes eject on Dell Latitude dock and an issue on the Dell Mini
battery update.
ok deraadt


# 1.219 07-Oct-2010 jordan

Create separate functions for enabling wake and runtime gpes.
Remove old suspend/resume gpewalk function
ok deraadt, mlarkin


# 1.218 05-Oct-2010 jordan

split acpi_enable_onegpe to separate enable/disable functions
ok deraadt


# 1.217 05-Oct-2010 jordan

Added acpi_disable_allgpes function to clear GPE status
ok deraadt


# 1.216 05-Oct-2010 deraadt

clean up acpi_softc use more


# 1.215 05-Oct-2010 deraadt

delete a bit of #if 0 code


# 1.214 05-Oct-2010 deraadt

hide use of our global acpi_softc pointer a bit better


# 1.213 29-Sep-2010 deraadt

grab the acpi thread lock very early in acpi thread startup; not that it
is likely that anyone will race us... but why be careless.
ok kettenis


# 1.212 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.211 29-Aug-2010 jasper

- add a check for FWRITE here too.

ok miod@


# 1.210 27-Aug-2010 kettenis

Make PCI Power Management optional, and only enable it when acpi(4) attaches.

ok deraadt@


Revision tags: OPENBSD_4_8_BASE
# 1.209 08-Aug-2010 kettenis

Close race between the acpi thread and wscons ioctls running in process
context. We might rip this out post-release and replace it with a solution
that serializes things in a more obvious way.

ok deraadt@, miod@ and "those crazy texans who ok anything"


# 1.208 08-Aug-2010 deraadt

Report the acpi sleep states when on a RAMDISK; ok krw


# 1.207 07-Aug-2010 kettenis

Enforce acpi register access rescrictions.

ok mlarkin@, deraadt@, tested by marco@


# 1.206 07-Aug-2010 deraadt

upon resume, notify all the acpiac and acpibat drivers to update
their status. some laptops do not do this automatically.
ok kettenis canacar


# 1.205 06-Aug-2010 marco

Always call _PSW on _LID devices that support it to enable lid open to
resume the machine. Conversely disable it when the machine wakes up.

Tested by several ok deraadt


# 1.204 06-Aug-2010 deraadt

As part of a revamp of the PM1/GPE code, I write this diff to look for
unmanaged EN & STS bits in the PM1 register at interrupt time and report
them. As a side effect this splits the STS acknowledgement into two writes
(for power, and sleep) instead of one. The printf that is added (to spot
unmanaged STS bits) has not yet been triggered as far as we know.

Before the "write to PM1 registers at the right offset" diff went in, this
was not neccessary. But newer thinkpads do not have a working soft power
button without this diff.

We have no idea why.
ok mlarkin kettenis


# 1.203 05-Aug-2010 deraadt

PM1 power button events were generating 'fake events' to acpibtn PWRB,
but if there is no such device these were being tossed. Instead, arrange
for all power button handlers to flag a softc variable which is processed
by the thread; so that one place will know to request a powerdown (psignal
to init)
ok marco mlarkin


# 1.202 05-Aug-2010 deraadt

rename a variable which is annoyingly poorly named


# 1.201 05-Aug-2010 mlarkin

Write to the correct offset for various ACPI pmreg registers. Writing to
the correct register does properly clear the wake status bits and fixed
event bits, which allows some machines to have more than 1 suspend cycle.

bug spotted by deraadt@

tested by deraadt@ and myself on a half-dozen machines and others on
hackers@

ok deraadt@, marco@


# 1.200 05-Aug-2010 deraadt

Fix the power button. The pm1 register is 16 bits in size.
My mistake, a while back.
tested by krw


# 1.199 04-Aug-2010 kettenis

Print supported sleep states.

ok marco@, phessler@, deraadt@


# 1.198 03-Aug-2010 mlarkin

Use the proper flag for re-enabling certain hardware events (power btn,
sleep btn). The (incorrect) flag prevoiously used coincidentally had the
same value, so this shouldn't cause any different behavior than before.

tested on a variety of machines (i386, amd64, sp, mp) - no behavioral
change seen

ok deraadt@


# 1.197 28-Jul-2010 marco

Make all gasio failures look and behave the same.


# 1.196 28-Jul-2010 mlarkin

Delay a little longer on suspend before giving up. Fixes machines that
sometimes don't suspend because the sleep transition takes too long
(symptoms like failing to sleep but the backlight stays on).

ok deraadt@


# 1.195 27-Jul-2010 deraadt

remove XXX; EC can now be attached early when needed; ok jordan


# 1.194 27-Jul-2010 deraadt

more brackets feels good


# 1.193 27-Jul-2010 deraadt

XXX annotate a block in acpi_enter_sleep_state() that is pretty worrying


# 1.192 27-Jul-2010 deraadt

horrific indentation that nearly made me puke


# 1.191 27-Jul-2010 mlarkin

Set the sleeping indicator light on machines that support it, in case SMI
doesn't do it for us. Shows the 'moon' LED on older Thinkpads when
sleeping.

Tested on T43p, W500, T510 by me and on X61s by deraadt@.

ok deraadt@


# 1.190 27-Jul-2010 jordan

Early initialization of acpiec if ECDT table exists
Fixes hang when booting thinkpads while docked
ok deraadt


# 1.189 26-Jul-2010 deraadt

remove merge error which was commited


# 1.188 26-Jul-2010 pirofti

Add support for Sony ACPI hotkeys via a new driver: acpisony(4).

Currently it only works for the suspend button and tries to do right for
the brightness events, but I haven't found a Sony laptop that like to do
right so far.

In the future I want to make the brightness keys work on all Sony's and
also add support for the zoom hotkeys and whatever other funky keys I can
find on those things.

Okay deraadt@.


# 1.187 22-Jul-2010 deraadt

pretty large cleanup of SMALL_KERNEL


# 1.186 21-Jul-2010 deraadt

in the acpi GPE handler, late ack level interrupts not edge interrupts.
Duh.


# 1.185 21-Jul-2010 deraadt

Merge the edge and level gpe handlers into one (now possible since the
gpe structure tells us whether the gpe is edge or level)


# 1.184 21-Jul-2010 deraadt

delete #if 0 code we are not going to use; ok marco


# 1.183 20-Jul-2010 deraadt

make the mask code for acpi_enable_onegpe() match other similar code


# 1.182 20-Jul-2010 deraadt

Handle the acpi interrupt controller with proper edge/level handling,
wrapped in spltty to avoid racing against the interrupt controller.
Repair the gpe bit masking code while there, and do operations in the
right order
ok kettenis mlarkin, help from jordan at figuring out the order of
operations.


# 1.181 20-Jul-2010 matthew

When we're preparing to go to sleep, only call bufq_restart() if other
preparations failed.

ok kettenis@, thib@, deraadt@


# 1.180 19-Jul-2010 deraadt

permit compilation with ACPI_DEBUG


# 1.179 19-Jul-2010 deraadt

there is no need for a seperate dsdt_softc pointer; dsdt does not have a
softc but is just a part of the single acpi driver.
ok jordan


# 1.178 19-Jul-2010 deraadt

acpi_delay() is no longer needed; ok jordan


# 1.177 18-Jul-2010 mlarkin

Clear event status on resume, to avoid going back to sleep/reboot/shutdown
immediately after resume. Tested by a bunch of people on hackers@ on
various hardware. Fixes at least Toshiba NB200 and Gateway LT3103u.

ok marco


# 1.176 13-Jul-2010 deraadt

rename some thread related interfaces so they are logical


# 1.175 10-Jul-2010 jordan

Verify _ADR bdf is correct (some systems return 0xffff)
Works on Dell x9xx and Studio 1555


# 1.174 09-Jul-2010 jordan

Clear the noise


# 1.173 08-Jul-2010 jordan

Add mapping for ACPI device to PCI bdf (match autoconf tree)
Simplify resource parsing function to use buffer argument
Convert namespace linked lists to use queue macros
ok marco@, deraadt@


# 1.172 06-Jul-2010 deraadt

Use spltty() to lock downcalls from apm against the information being
modified by the acpi thread.
ok kettenis


# 1.171 06-Jul-2010 deraadt

Don't use a workq to do the suspend, because it races aginst the acpi
thread. Instead, just tell the acpi thread to do the suspend for us.
This makes apmd & zzz work correctly. While here, have acpithinkpad
attempt to post the event to apm before prompting the suspend itself.
ok kettenis marco mlarkin


# 1.170 05-Jul-2010 mlarkin

Reset register SCI_EN on resume. The ACPI spec clearly states we don't have
to do this, but many machines don't, or forget to, which leads to all sorts
of problems on resume (broken battery stats, overheating, failure to resume
more than once, etc).

tested by many, ok marco@, deraadt@


# 1.169 01-Jul-2010 thib

do bufq quiesce again, since vnd/the bufq_done call in
biodone has been fixed.


# 1.168 01-Jul-2010 jordan

Backout recent AML changes, breaks on R210 and others


# 1.167 01-Jul-2010 jordan

Simplified aml_parse_resource call, cleanup code
ok marco


# 1.166 01-Jul-2010 jordan

Fix subordinate bus # for multi-root PCI buses
ok marco, kettenis


# 1.165 30-Jun-2010 thib

Disable/partially backout the bufq quiesce changes as this
is causing havoc with vnds and release must be buildable.


# 1.164 29-Jun-2010 deraadt

Fix ramdisks (new code was placed inside a SMALL_KERNEL chunk)


# 1.163 29-Jun-2010 jordan

Add support for mapping ACPI to PCI devices
ok kettenis, deraadt


# 1.162 29-Jun-2010 kettenis

Call bufq_quiesce() on suspend to stop all I/O and bufq_restart on resume.
Makes suspend/resume work much better while doing disk I/O.

ok mlarkin@


# 1.161 29-Jun-2010 tedu

60 is a more reasonable guess for number of minutes in an hour than 100.
spotted by TeXitoi. ok deraadt


# 1.160 27-Jun-2010 jordan

Add _acpi_matchhids routine, precursor to pci device match


# 1.159 27-Jun-2010 mlarkin

Clear ACPI fixed event status on resume (power buttons/etc) so that some
machines don't immediately resume after suspending to S3.

Tested by kettenis@ and I on 5 or 6 machines, deraadt@ "yeah, looks good"


# 1.158 27-Jun-2010 jordan

Cleaned up acpi_maptable, adds to table list directly


# 1.157 07-Apr-2010 deraadt

If we have done a wsdisplay_suspend(), and ACPI decides it does not want to
suspend, we must roll back by calling wsdisplay_resume() or the virtual
console switching will be locked forever
ok oga


# 1.156 07-Apr-2010 kettenis

Make suspend/resume work on MP machines (running an MP kernel). Joint work
from mlarkin@ and me, with some amd64 fixes thrown in by deraadt@

ok marco@, deraadt@, pirofti@, mlarkin@


# 1.155 31-Mar-2010 kettenis

Bring /dev/apm support in line with apm(4). Make the suspend button send
a suspend request event to apmd(8) instead of suspending immediately. Also
keep track of whether /dev/apm and /dev/apmctl are currently open, such that
we can still suspend immediately if apmd(8) isn't running.

ok deraadt@, marco@, pirofti@, jsing@, oga@


# 1.154 30-Mar-2010 oga

Prevent the apmd/x races for good.

When we hit suspend time, go through all wsdisplays on the system. if
they are in mode MAPPED, but not MODE_DUMBFB then if possible do a full
vt switch to a !mapped vt, and prevent switching back until resume time.
This has to be called from MD code because this involves userland
running so that X can run the vt switch signal handler. This way, any
case where we are using the "poke registers from userland" model, we
will not be on the hardware when we go down, so the kernel can actually
handle thing properly.

Tested on several acpi laptops (by kettenis@ and ian@), x40 (me and
beck@ at LEAST) and zaurus (me). Maybe others, but if so I forgot who at
this time..

Idea from deraadt somewhere over the Faroe Islands (I thought of a
similar thing myself a while ago). Much prompting from him. Ok and
comments miod@


# 1.153 25-Mar-2010 oga

Fix & vs. && in the kqfilter code.

Based on a comparison with the apm code.

ok deraadt@, kettenis@


Revision tags: OPENBSD_4_7_BASE
# 1.152 26-Nov-2009 mlarkin

Visual (and on some machines, audio) feedback while the machine is
resuming via calls to aml _SST method. This also makes the "moon" LED on
thinkpads to blink during resume and cease when resume is completed.

ok pirofti@


# 1.151 26-Nov-2009 deraadt

Toggle into "cold" when we we disable interrupts, because tsleeping
in the AML interpreter and context switching into userland processes
tends to sometimes pop the ACPI trampoline page out of our address
space. Makes lots more machines work.
ok kettenis mlarkin


# 1.150 24-Nov-2009 jsg

in the resume path evaluate _WAK before dealing with device drivers
like the spec says, not after.

ok mlarkin@


# 1.149 24-Nov-2009 deraadt

abort suspends on SMP systems until that gets written; ok mlarkin


# 1.148 23-Nov-2009 mlarkin

pci should return the result of it's children's suspend/resume calls back
to its parent.

handle suspend failure case and unwind if devices have an activate function
that returns failure.

ok deraadt@, kettenis@


# 1.147 23-Nov-2009 mlarkin

acpi* devices need to call their children on suspend/resume.
ok deraadt@


# 1.146 23-Nov-2009 pirofti

Remove ACPI_SLEEP_ENABLED checks.

This enables by default the suspend/resume paths in the kernel.

Okay deraadt@.


# 1.145 23-Nov-2009 deraadt

knf and spacing that are super annoying


# 1.144 23-Nov-2009 mlarkin

Hook up Theo's new children suspend/resume function, as well as more ACPI
suspend/resume glue.

ok deraadt@


# 1.143 26-Oct-2009 deraadt

Do not do strncmp comparisons on dv_xname because longer device names which
look similar could arrive in the future. Instead, compare directly against
dv_cfdata->cf_driver->cd_name
Issue originally spotted by miod


# 1.142 26-Oct-2009 jordan

Verify checksum+address when loading ACPI tables.
Some systems had invalid entries in RSDT/XSDT.
ok marco@


# 1.141 23-Jul-2009 cnst

New aibs(4) driver for ASUSTeK AI Booster (ACPI ATK0110) hardware monitoring,
with sensor state support through limits provided by the ACPI.

Tested on several ASUS motherboards kindly networked by Sam Fourman Jr.

ok deraadt marco jordan


Revision tags: OPENBSD_4_6_BASE
# 1.140 03-Jun-2009 pirofti

Import acpi power resource driver. This is needed for suspend/resume logic.

Okay jordan@ and marco@.


# 1.139 03-Jun-2009 pirofti

Add acpivideo support. This adds brightness support for all laptops
except thinkpads, they will use the acpithinkpad driver. The driver is
also hooked into wsconsole. So brightness can be adjusted via:

$ wsconsctl display.brightness=<percentage>

This is very helpfull on some laptops that have a nasty bios and get two
steps instead of one when pressing the brightness button.

Tested on various dell, fujitsu, acer, samsung and other laptops.

Okay marco@, miod@. Suggestions from kettenis@.
Lots of reviews and help from miod@, thanks!


# 1.138 03-Jun-2009 jordan

Added ejectable bay for IDE cdroms
ok marco@


# 1.137 30-Apr-2009 marco

Oga changed apm ioctls and forgot to grep. Temporary work around by hitting
the sleep button when we get any request. This interface will chnage at
some point.
ok oga


# 1.136 19-Apr-2009 krw

Call acpi_sleep_walk() when powering down just like when entering
any other sleep state. This tells acpi wake devices to not wake up
the box until manual powering up resets their status. Makes my Dell
GX520 stay powered off in response to 'halt -p'.

In snaps for a while.

ok marco@ (several times) deraadt@


# 1.135 17-Apr-2009 pirofti

Add device ranking support for future sleep/wake developments.

okay marco@


# 1.134 11-Mar-2009 jordan

Cleanup.. use aml_evalinteger instead of aml_evalname
ok marco@


# 1.133 10-Mar-2009 jordan

Fix cut/paste error.. call _BFS not _PTS
ok marco@


Revision tags: OPENBSD_4_5_BASE
# 1.132 21-Feb-2009 marco

overzealous cut/paste; found by Vladimir Kirillov <proger@uaoug.org.ua>


# 1.131 20-Feb-2009 marco

Oops didn't mean for Debugger() to remain there.


# 1.130 19-Feb-2009 marco

suspend/resume bits so that we can develop this in tree. This is disabled.
code from mlarkin and me
help from art,toby,jordan and several others
ok jordan, go for it deraadt


# 1.129 10-Feb-2009 jordan

Fix null pointer for shutdown if acpi not enabled
ok marco@


# 1.128 04-Feb-2009 kettenis

Some systems that claim to be ACPI 2.0 apparently don't provide an XSDT
address. Make the code fall back on the RSDT on those systems.

From Joerg Niendorf <p3y80@internode.on.net>. Fixes PR 5986.


# 1.127 17-Jan-2009 krw

A couple of stray malloc/memset -> malloc(M_ZERO), a bit of knf and
lose a couple of unnecessary casts. No functional change.

Feedback from kettenis@

ok kettenis@ marco@


# 1.126 06-Nov-2008 marco

Collapse all sanity checks of hids into one function.

Originaly from jordan


Revision tags: OPENBSD_4_4_BASE
# 1.125 02-Jul-2008 fgsch

add acpivideo. for now almost an empty skel so others can work on
suspend/resume. eventually it will also manage output switching and
brightness where supported. prodded by marco@


# 1.124 11-Jun-2008 marco

Fix $OpenBSD$ for once and for all


# 1.123 08-Jun-2008 deraadt

aaa_dev was being set from an uninitialized variable; ok toby


# 1.122 07-Jun-2008 marco

Kill ACPIVERBOSE. It was never used and is worthless to boot.

comments jmc
ok toby


# 1.121 06-Jun-2008 marco

Shrink acpi some more for floppies and while here re-enable acpi on the
floppies. Fixes crazy sony vaio kettenis has.

tested by deraadt
ok deraadt jordan kettenis


# 1.120 01-Jun-2008 marco

Fix \n nit in SMALL_KERNEL.


# 1.119 01-Jun-2008 marco

Re-enable new parser. Lots of testing by various folks. Fits all media
now too.

ok kettenis deraadt


# 1.118 16-May-2008 dlg

back out jordans new parser.

it didnt have enough oks from the right people, it did break machines
that were previously working, and it bloated the RAMDISKS and install
media beyond the constraints of the media.

requested by deraadt@


# 1.117 14-May-2008 jordan

Adding new ACPI Parser code
Old guts of code still exists, needs to be torn out next
ok marco@
ok brad@


# 1.116 27-Apr-2008 jcs

add acpithinkpad(4), an acpi support driver for ibm/lenovo
thinkpads.

provides functionality that the bios did on older, non-acpi
thinkpads like respond to screen brightness keys and toggle the
bluetooth device on and off.

ok deraadt@ and marco@


# 1.115 24-Apr-2008 jsing

Introduce acpiasus(4), a driver for the ACPI based hotkeys found in many
ASUS laptops (including the ASUS EeePC) - largely based on NetBSD's
asus(4) driver. On the ASUS EeePC this allows us to enable/disable
wireless, change screen brightness and use the volume keys.

ok jsg@, weingart@


# 1.114 12-Apr-2008 kettenis

Get rid of acpi_s5 global variables; simply send SIGUSR2 instead of SUGUSR1
to tell init(8) to power down the machine.

ok krw@


Revision tags: OPENBSD_4_3_BASE
# 1.113 21-Jan-2008 jsg

Check against RESET_REG_SUP in FADT before doing the acpi reset dance
after all, not doing so appears to break more machines than it fixes.

ok marco@


# 1.112 05-Jan-2008 kettenis

Never attach more acpicpu(4) devices than cpu(4) devices. Always attach
the first acpicpu(4) device on non-MULTIPROCESSOR kernels, regardless of its
ACPI processor ID.

ok mikeb@


# 1.111 05-Dec-2007 deraadt

Hang acpi(4) below bios(4) instead of mainbus(4). This lets us move acpi(4)
before pcibios(4), and then.... if acpi(4) attaches, skip pcibios(4) since
it messes the machines up. Fixes claudio's HP dl320 G5, and almost assuredly
others.
ok kettenis


# 1.110 05-Dec-2007 deraadt

repair acpiopen/acpiclose and friends to handle the /dev/apmctl interface,
so that apmd(8) can actually work correctly against acpi(4), and thus,
even do the fancy -C/-A stuff; also tested by jmc


# 1.109 02-Dec-2007 fgsch

fix the trailing space for real now.


# 1.108 02-Dec-2007 fgsch

avoid trailing spaces on output; marco@ ok.


# 1.107 27-Nov-2007 kettenis

Fix off-by-one that made us attach acpicpu0 and acpicpu1 on GENERIC kernels.

ok marco@


# 1.106 25-Nov-2007 jsg

When available and ACPI is enabled use the ACPI reset method
to reboot. This is required for some quirky sis machines
and likely others to reboot.

Tested by many. 'commit early commit often' deraadt@


# 1.105 19-Nov-2007 deraadt

sigh, amd64 still uses that stupid acpi_enabled variable...


# 1.104 19-Nov-2007 deraadt

softc's come initialized to 0, you know...


# 1.103 19-Nov-2007 deraadt

store revision; from jsg


# 1.102 19-Nov-2007 kettenis

Initialize sc_facs earlier.

ok deraadt@, beck@, canacar@


# 1.101 16-Nov-2007 deraadt

fix the bus_space #define nightmare, so that amd64 and i386 are much more
uniform. as a result shared code like acpi needs less #ifdef's
ok marco kettenis


# 1.100 15-Nov-2007 deraadt

remove the #ifdef ACPI_ENABLE. the EC codepath is either safe enough (and
will be improved), or it is unsafe because AML assumes it must be there
ok almost everyone


# 1.99 15-Nov-2007 mikeb

Make acpicpu(4) attach to the existing CPUs only.

Problem reported by form@. Fix idea by kettenis@.
Lots of help from deraadt@. Tested by deraadt@, form@ and me.

ok deraadt marco


# 1.98 14-Nov-2007 deraadt

spacing


# 1.97 12-Nov-2007 deraadt

correct : vs , in attach-time messages


# 1.96 06-Nov-2007 deraadt

all ACPI_ENABLE kernels need acpi_interrupt()


# 1.95 06-Nov-2007 deraadt

move acpi_read_pmreg() and acpi_write_pmreg() out of SMALL_KERNEL so
that acpi ramdisks can compile (obvious solution)


# 1.94 05-Nov-2007 canacar

Establish the acpi interrupt earlier. Fixes problems with some
GENERIC.MP kernels not getting acpi interrupts. This is a
workaround until the real issue with late establishment is fixed.
ok weingart@, beck@, gwk@


# 1.93 03-Nov-2007 jordan

Added support for displaying wakeup devices
ok beck@,weingart@,gwk@


# 1.92 11-Oct-2007 kettenis

When walking the tree yo find _PRT methods, check whether the device is
actually enabled and functioning. Similar to what we do for _INI.

ok weingart@, beck@


# 1.91 08-Oct-2007 krw

More simple memset(,0,) -> M_ZERO changes. In this batch move to
size(*p) as the first malloc() parameter where p is declared locally
and thus easy to check. Add M_ZERO to gpe_table allocation in acpi.c
even though there is no obvious bzero or memset nearby.


# 1.90 13-Sep-2007 mk

Remove unnecessary memset() (see aml_evalnode(9)) and a bit of KNF.

ok toby


# 1.89 13-Sep-2007 weingart

Implement ACPI 6.5.1 spec tree walk for _STA and _INI.
Fixes mk's laptop. No regressions so far (thank you to the
testers).

ok gwk@, mk@, marco@


Revision tags: OPENBSD_4_2_BASE
# 1.88 17-Apr-2007 mk

Now that acpidock no longer hangs machines during boot (at least
according to the reports I've been getting) it should be safe to enable
it again, so remove the #if 0 stuff around it.

ok marco


# 1.87 11-Apr-2007 jordan

Added changes for C-State
ok marco@


# 1.86 26-Mar-2007 marco

Calculate minutes left more accurately.

From giovanni <qgiovanni@gmail.com>


Revision tags: OPENBSD_4_1_BASE
# 1.85 03-Mar-2007 mk

Stop looking for acpidock until we fix that stuff that breaks all pre-60
series ThinkPads with acpidock(4) enabled.


# 1.84 22-Feb-2007 marco

We were getting no interrupts in .MP. There are 2 fixes; one in
acpi_machdep.c and move the interrupt establishment down after ioapic
has been initialized.

ok jordan


# 1.83 21-Feb-2007 kettenis

Make an acpi-enabled GENERIC.MP work on systems that only support PIC mode.

ok marco@


# 1.82 19-Feb-2007 jordan

Added acpi_map_address function for mapping GAS/IO registers
ok marco@


# 1.81 18-Feb-2007 jordan

Added dynamic perfect hash for opcodes
ok marco@


# 1.80 18-Feb-2007 marco

Shave off another K pre compression.
Shuffle SMALL_KERNEL functions around so that we only have a single #ifndef


# 1.79 17-Feb-2007 marco

Add SMALL_KERNEL throught acpi to make it fit on boot media.
ok deraadt


# 1.78 13-Feb-2007 marco

Add smarter _INI code. This helps with some dissapearing devices.

ok kettenis jordan


# 1.77 31-Jan-2007 gwk

Introduce acpi_hasprocfvs thats "ACPI Has Processor Frequency and Voltage
Scalling" a global flag set by acicpu attach to indicate that ACPI is
handling fvs through the _PSS objects. This will fix disappearing
EST/powernow when booting kernels with acpi enabled, as acpicpu isnt
compiled into GENERIC yet. Also in cases where acpicpu is built in and
legacy EST/powernow work but acpi is missing the _PSS object, the legacy
routines will be used.

tested by dim@, and Nick Nauwelaerts <nick AT nauwelaerts.net>
ok marco@, dim@


# 1.76 22-Jan-2007 mk

Actually look for docking stations by looking for _DCK nodes.

ok marco


# 1.75 02-Jan-2007 marco

On some systems EC must be attached as the first device. Move EC from
generic _HID devices to its own routine to force first attach. Bug found
and debugged by joris@

Code mostly written by joris@


# 1.74 26-Dec-2006 marco

Add polling to devices that require it such as acpibat and acpitz.
Use the same tsleep/wakeup mechanism as the interrupt code to keep it non-
concurrent.


# 1.73 21-Dec-2006 deraadt

even more knf love


# 1.72 21-Dec-2006 marco

Remove some leftover acpiec gpe/interrupt code.


# 1.71 20-Dec-2006 deraadt

oops, name already used


# 1.70 20-Dec-2006 deraadt

rename __acpi_enable_gpe(); ok marco


# 1.69 20-Dec-2006 deraadt

acpi_unmap_pmregs() is not used, ok marco


# 1.68 19-Dec-2006 deraadt

simplify some low-level methods substantially (shrinking code); ok marco jordan


# 1.67 06-Dec-2006 mk

malloc(..., M_NOWAIT) with no NULL check is bad. If allocation fails,
complain, clean up, and bail out of attach.

input and ok gwk


# 1.66 28-Nov-2006 jordan

Fixed clearing arg for acpi_init_pic
ok kettenis@


# 1.65 27-Nov-2006 kettenis

Don't spam dmesg.


# 1.64 27-Nov-2006 jordan

Fixes for acpiprt routing
Post-parse fixup for namerefs
ok kettenis@


# 1.63 25-Nov-2006 marco

Don't handle acpiev events inside the gpe handler since this causes deadlocks.

ok jordan@


# 1.62 15-Nov-2006 kettenis

Hook up acpi apic interrupt routing support to generic acpi code.

ok jordan@


# 1.61 02-Nov-2006 marco

Kill acpiec attach function and add its functionality to the generic _HID
device attach

prodded and ok jordan


# 1.60 02-Nov-2006 marco

Fix comments to reflect reality.


# 1.59 19-Oct-2006 marco

Replace ACPI ioctls with APM ioctls. This makes ACPI talk the same protocol
as APM therefore userspace can now retrieve power information in the same
fashion as all other apm arches (zaurus, macppc, i386 etc).

To use force the use of acpi device like so: apmd -f /dev/acpi

ok jordan@


# 1.58 19-Oct-2006 jordan

Fixed GPE debug levels
Fixed ref counter on temporary bufferfields
DSDT parsing fully working on IBM Thinkpad
ok marco@


# 1.57 15-Oct-2006 jordan

Removed old GPE handler code remnants
ok marco@


# 1.56 12-Oct-2006 jordan

Massive ACPI changes
Fixed ACPI leaks
new ACPI API
ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.55 30-Jun-2006 gwk

KNF, No functional change,
"go at it" marco@


# 1.54 30-Jun-2006 jordan

Support for calling Embedded Controller via new GPE interface
ok marco@


# 1.53 30-Jun-2006 jordan

Added new code for GPE handling
Also works with ACPIEC; need to fixup acpiec.c to remove original interrupt handler
ok marco@


# 1.52 30-Jun-2006 jordan

Added offset argument for acpi read/write pmreg routines (new GPE code)
ok marco@


# 1.51 31-May-2006 todd

do not call acpi_powerdown() unless acpi has successfully initialized
ok gwk@ canacar@


# 1.50 29-May-2006 canacar

Add embedded controller support.
ok marco@ deraadt@ testing & ok gwk@


# 1.49 19-May-2006 canacar

Minimal thermal zone driver. Monitors thermal zone temperature,
shuts down the system if the 'critical temperature' is reached.
ok marco@


# 1.48 11-Apr-2006 marco

KNF.


# 1.47 11-Apr-2006 gwk

Add acpi_resume(struct acpi_softc *sc), which calls some AML methods,
detected at attach time, currently unused.

ok marco@


# 1.46 11-Apr-2006 gwk

Discover some ACPI sleep related methods, and call them at the
appropriate time in acpi_enter_sleep_state().

ok marco@


Revision tags: OPENBSD_3_9_BASE
# 1.45 26-Feb-2006 marco

Add acpicpu as a device.

ok jordan@


# 1.44 23-Feb-2006 marco

remove dead function.

ok jordan@


# 1.43 22-Feb-2006 marco

Print out all tables it found during autoconf. We'll need this data in
the future.


# 1.42 22-Feb-2006 marco

Make dmesg less verbose.

ok jordan.


# 1.41 22-Feb-2006 jordan

Added parsing of SSDT tables
ok marco@


# 1.40 22-Feb-2006 jordan

Added new aml_register to notify by device id
Moved powerdown code to button device
ok marco@


# 1.39 21-Feb-2006 marco

Readjust debug levels that crept in too low over the last few days.
KNF


# 1.38 21-Feb-2006 marco

Rig the power button so that the machine shuts down correctly.


# 1.37 20-Feb-2006 marco

Add acpibtn device. Remains disabled for now.


# 1.36 19-Feb-2006 jordan

Added code for aml_notify GPE notification callback
ok marco@


# 1.35 19-Feb-2006 grange

Fix acpi_enter_sleep_state() by replacing current ugly hack
with mostly correct sleeping state transition code.

ok brad@ marco@


# 1.34 19-Feb-2006 marco

jordan:
Enable interrupts.
Added peripheral code to make this work.

marco:
Replace softintr and other types of interrupts with a handler with context.
Fixed interrupt magic for GPEs.
Some KNF

This is work done by jordan and I but has to be a single commit.


# 1.33 17-Feb-2006 marco

Remove earlier introduced delays. We really only need them during autconf.

ok jordan@


# 1.32 17-Feb-2006 marco

Add a generic delay function as per the ACPI spec.

from jordan@


# 1.31 17-Feb-2006 marco

Use tsleep instead of delay when !cold.


# 1.30 17-Feb-2006 marco

Argh, we need delays between reads/writes. The backend reads (i2c most likely)
are so slow that values come back as 0 sometimes. We have to do this
despite the spec saying we shouldnt have to. Guess having a faster
parser than others can hurt sometimes.

Idea from jordan.


# 1.29 17-Feb-2006 jordan

Fixed code for handling _INI
Rearranged debug statements
ok marco@


# 1.28 16-Feb-2006 jordan

Added stub for calling _INI
Fixed aml_dowhile for NULL return
ok marco@


# 1.27 16-Feb-2006 jordan

Fixed parser/evaluator to retrieve battery info+state
Cleanup of parser
Dell laptops return battery status
ok marco@


# 1.26 06-Feb-2006 brad

fix a typo so this will compile on amd64.


# 1.25 04-Feb-2006 marco

When ACPI_ENABLED wasn't set but the devices were it had an unknown variable.

Pointed out by ian@.


# 1.24 03-Feb-2006 jordan

Rewrote aml parser yet again...
Fixed bus_space_map for i386/amd64
Added _BST and _BIF to acpibat to get status/info
ok marco@


# 1.23 20-Jan-2006 jordan

More evaluator changes
Removed old evaluator code
Fixed _HID value
ok marco@


# 1.22 19-Jan-2006 jordan

Added support for read/write of PCI Config space
Help jason@
ok marco@


# 1.21 18-Jan-2006 jordan

Added new evaluation routines
Added helper methods for buffer/field evaluation
Fixed broken indentation on dsdt.c

ok marco@


# 1.20 17-Jan-2006 jordan

Major overhaul of the aml parser. Evaluater temporarily disabled.

ok marco@


# 1.19 06-Jan-2006 grange

Check for ACPI control availability only if ACPI_ENABLE defined.


# 1.18 06-Jan-2006 grange

Informative error messages on attach, prettier dmesg.


# 1.17 06-Jan-2006 grange

printf -> dnprintf conversion for debug messages.


# 1.16 05-Jan-2006 grange

Find SLP_TYPx values and store them for future use in
acpi_enter_sleep_state().


# 1.15 05-Jan-2006 grange

Random KNF and cleanup


# 1.14 05-Jan-2006 grange

Provide symbolic names for the acpi sleeping states and use it.


# 1.13 28-Dec-2005 marco

Moved DEVNAME to a acpidev.h

ok jordan@


# 1.12 18-Dec-2005 sturm

add back comment about wrong code

ok marco


# 1.11 16-Dec-2005 marco

Remove test for kbd_reset. Prompted deraadt@, ok jordan@


# 1.10 16-Dec-2005 jordan

Added hooks for powerdown/power button press


# 1.9 16-Dec-2005 jordan

Cleaned up common power management register i/o
Added enter sleep state ioctl
Hooked power button interrupt
ok marco@


# 1.8 16-Dec-2005 marco

Remove autoconf chatter


# 1.7 16-Dec-2005 jordan

Moved DSDT AML parsing into parent ACPI driver
ok marco@


# 1.6 14-Dec-2005 marco

Add AC device glue.


# 1.5 14-Dec-2005 marco

Adjust debug printing levels to something more sane.


# 1.4 13-Dec-2005 marco

Add glue to attach devices to apci.


# 1.3 07-Dec-2005 marco

KNF


Revision tags: OPENBSD_3_8_BASE
# 1.2 10-Jul-2005 grange

Spacing cleanup.


# 1.1 02-Jun-2005 tholo

Start on a basic ACPI framework -- does not do much more than read out the
ACPI tables into kernel memory and attach ACPI and HPET timers currently.

In order to test this code, enabling the devices in GENERIC as well as
the ACPI_ENABLE option is needed. This code does not do any thermal
control yet, so this should be done with care depending on the platform.

In the tree so more people can contribute to making this more fully
featured.

Ok niklas@ grange@ tedu@


# 1.404 04-Jan-2022 patrick

acpi_getprop() needs to actually make sure that we're looking at the
correct property. While there adjust acpi_getpropint() as well to
increase similarity with acpi_getprop().

ok kettenis@


# 1.403 16-Dec-2021 anton

Attach com over acpi on amd64. Some hardware uses a different interrupt
assignment compared to the the legacy one supported by com over isa.
This causes the console to halt once userland takes over as no
interrupts are received. The actual address and irq can be read from
ACPI, kettenis@ already added support for arm64 which paved the way for
amd64.

Some consoles that previously attached over isa are now expected to
attach over acpi.

Thanks to patrick@ for testing on arm64.

ok kettenis@


# 1.402 07-Dec-2021 kettenis

Fix parsing of SR_IRQ resource descriptors.

ok patrick@, anton@


# 1.401 02-Nov-2021 deraadt

knf


# 1.400 30-Oct-2021 deraadt

Change hw.perfpolicy=auto by default, at startup. If the system has AC
power connected (default is yes when no driver differentiates) then default
to 100% performance. On battery, use the existing auto algorithm (which is
admittedly somewhat unrefined).
This change overrides the system/BIOS speed and puts OpenBSD in control.
As this happens very early during boot, besides speedups in all usage usage
patterns, some surprises: unhibernate and sysupgrade times are cut in half.
note: on a few architectures, the setperf fn pointer is changed late, and
thus the auto algorithm stops timeing out. kettenis and i will look for
a solution.
in snaps for more than a week.
ok kettenis


Revision tags: OPENBSD_7_0_BASE
# 1.399 20-Jul-2021 mlarkin

Fix trailing whitespace in a few places.

No code change.


# 1.398 09-May-2021 kettenis

Some x86 machines advertise the "hardware reduced" ACPI feature, advertise
S4 and S5 support, but fail to populate the SLEEP_CONTROL_REG and
SLEEP_STATUS_REG descriptions in the FADT. An example of such a machine
is the ASUS Zenbook 14 UM433DA. Any attempt to powerdown the machine
will result in a kernel crash. It turns out that using the legacy ACPI PM
registers works fine on this machien. So fall back on those registers
if SLEEP_CONTROL_REG and/or SLEEP_STATUS_REG aren't provided.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_9_BASE
# 1.397 15-Mar-2021 patrick

Add acpi_iommu_device_map(), which replaces the DMA tag with one that
is blessed with IOMMU magic, if available. This is mainly for arm64,
since on amd64 and i386 the IOMMU only captures PCIe devices, as far
as I know, which uses the pci_probe_device_hook(). This though is for
non-PCI devices attached through ACPI.

ok kettenis@


# 1.396 07-Mar-2021 patrick

Pass standard DMA tag to acpi(4) table drivers.

ok kettenis@


# 1.395 27-Dec-2020 deraadt

acpi_map_address() cannot be wrapped by SMALL_KERNEL anymore, as
it is used by acpihpet.c


# 1.394 25-Dec-2020 visa

Refactor klist insertion and removal

Rename klist_{insert,remove}() to klist_{insert,remove}_locked().
These functions assume that the caller has locked the klist. The current
state of locking remains intact because the kernel lock is still used
with all klists.

Add new functions klist_insert() and klist_remove() that lock the klist
internally. This allows some code simplification.

OK mpi@


# 1.393 17-Dec-2020 kettenis

Fix some issues with referencing named ACPI nodes from Packages.
These references need to be resolved at runtime rather than when
they're parsed such that they pick up the right values for those nodes
which can be changed when for example _INI methods run.

The current approach is to replace these reference with a string that
names the node in question. The problem with that is that packages
can also contain normal strings. Which means that we need special
code that depends on the context where the package is used.

This diff takes a different approach by retaining a reference when
parsing. Code that uses a package will need to resolve this reference
but there is no ambiguiety anymore.

ok patrick@


# 1.392 05-Dec-2020 kettenis

Add support for the _EVT method to the GPIO support code.

ok patrick@


Revision tags: OPENBSD_6_8_BASE
# 1.391 27-Aug-2020 jmatthew

Reorder the acpi attach process so we look at Processor() CPU nodes
after Device() ones, since we should prefer the newer node type.
If we see any Device() nodes, don't attach acpicpu(4) to any Processor()
nodes if they're also present. This also makes acpitz(4) and
acpipwrres(4) devices attach slightly later.

ok kettenis@, also tested by jmc@


# 1.390 16-Aug-2020 gnezdo

Remove sc_maxgpe from acpi_softc which was never read from

Remove a dead conditional from acpi_init_gpes while there.

ok jcs@


# 1.389 21-Jul-2020 deraadt

acpi can use IPL_BIO (a low interrupt) since it only enqueues operations for
later processing. The use of a high interrupt will predate suspend/resume
efforts, we had to redesign acpi to be non-reentrant obviously
discussed with kettenis, in snaps for more than a week


# 1.388 24-Jun-2020 cheloha

kernel: use gettime(9)/getuptime(9) in lieu of time_second(9)/time_uptime(9)

time_second(9) and time_uptime(9) are widely used in the kernel to
quickly get the system UTC or system uptime as a time_t. However,
time_t is 64-bit everywhere, so it is not generally safe to use them
on 32-bit platforms: you have a split-read problem if your hardware
cannot perform atomic 64-bit reads.

This patch replaces time_second(9) with gettime(9), a safer successor
interface, throughout the kernel. Similarly, time_uptime(9) is replaced
with getuptime(9).

There is a performance cost on 32-bit platforms in exchange for
eliminating the split-read problem: instead of two register reads you
now have a lockless read loop to pull the values from the timehands.
This is really not *too* bad in the grand scheme of things, but
compared to what we were doing before it is several times slower.

There is no performance cost on 64-bit (__LP64__) platforms.

With input from visa@, dlg@, and tedu@.

Several bugs squashed by visa@.

ok kettenis@


# 1.387 10-Jun-2020 jca

Give userland a consistent battery and A/C state after resume

Previously we refreshed the state of acpiac(4) and acpibat(4)/acpisbs(4)
by queueing fake ACPI events to be processed by the acpi thread.
acpibat(4) using ACPIDEV_POLL meant that its status could be refreshed
first, resulting in an APM_POWER_CHANGE event sent to userland before
the state of acpiac(4) was up to date. Because of this, apmd(8) could
see A/C unplugged when it was in fact plugged, possibly leading to
a wrong decision.

Instead of using fake ACPI events, refresh the status of acpiac(4) and
acpibat(4)/acpisbs(4) right away at DVACT_WAKEUP time. To reuse the
existing refresh code of those drivers, make their ACPI *_notify()
handlers responsible for sending APM_POWER_CHANGE events instead. Also
queue an APM_POWER_CHANGE event right after resume to retain existing
behavior.

This fixes the consistency of data shown to userland, and makes those
drivers less special.

Initially discussed last year (!) with mpi@ at Miod's place.
Feedback and ok jcs@, deraadt@ agrees with the use of DVACT_WAKEUP here.


# 1.386 29-May-2020 deraadt

dev/rndvar.h no longer has statistical interfaces (removed during various
conversion steps). it only contains kernel prototypes for 4 interfaces,
all of which legitimately belong in sys/systm.h, which are already included
by all enqueue_randomness() users.


# 1.385 14-May-2020 kettenis

Bring back the acpi(4) changes. This time around only use the ACPI _CRS
information on ACPI 5.0 and later.

ok krw@, patrick@


# 1.384 11-May-2020 jca

More accurate remaining power reporting with disparate batteries

Overall remaining power was computed as the average of the remaining
power of each battery, in percents. This doesn't work with batteries of
different capacity, so use the total capacity and remaining power
instead.

Reported by lidstah, successful tests by lidstah and benno@, ok kettenis@


# 1.383 08-May-2020 kettenis

Move parsing of _CRS into acpi(4) and pass the parsed address and interrupt
information in struct acpi_attach_args.

ok mpi@


Revision tags: OPENBSD_6_7_BASE
# 1.382 14-Apr-2020 kettenis

Implement acpi_getprop(), the ACPI equivalent of OF_getprop().

ok patrick@


# 1.381 12-Apr-2020 kettenis

Add support for the _CCA method. This method indicates whether DMA is
cache-coherent or not. To implement this, acpi(4) gets two bus_dma tags
and passes the appropriate one when attaching devices based on _CCA.
On i386/amd64, where for all practical purpose DMA is always cache-coherent,
the two tags are the same. But on arm64 they are distinct.

ok patrick@


# 1.380 07-Apr-2020 visa

Abstract the head of knote lists. This allows extending the lists,
for example, with locking assertions.

OK mpi@, anton@


# 1.379 03-Apr-2020 mpi

Fix incoherencies of errors returned by various *kqfilter routines.

EOPNOTSUPP is returned when there's no "filterops" for a given fd.
EINVAL when the requested filter isn't supported by the "filterops".
ENXIO means the underlying device is no longer valid/present.

ok visa@


# 1.378 20-Feb-2020 visa

Replace field f_isfd with field f_flags in struct filterops to allow
adding more filter properties without cluttering the struct.

OK mpi@, anton@


# 1.377 31-Dec-2019 visa

Use C99 designated initializers with struct filterops. In addition,
make the structs const so that the data are put in .rodata.

OK mpi@, deraadt@, anton@, bluhm@


# 1.376 31-Dec-2019 mpi

Convert infinite sleeps to {m,t}sleep_nsec(9).

ok kn@


# 1.375 31-Dec-2019 jsg

include sys/sensors.h for sensor use
don't depend on indirect inclusion from acpidev.h


Revision tags: OPENBSD_6_6_BASE
# 1.374 07-Sep-2019 kettenis

Revert acpipci(4) changes. Too many systems are broken. We'll try again
after 6.6 as been released. The acpireg.h change stays behind.


# 1.373 28-Aug-2019 kettenis

Use ACPI information to attach PCI busses like we do on arm64. There are a
few additional quirks though, and attaching the PCI busses is delayed to
replicate the existing code more closely. That may be changed in the
future. Also tweak how we handle MSI support and respect to ACPI flag
that says we shouldn't attempt to use MSIs.

Some fallout is expected.

ok patrick@


# 1.372 27-Aug-2019 deraadt

On amd64 the bootup and resume paths for SP/BP/SP were not performing
msr/pat, clock-startup, firmware upgrade, fpu initialization, and cpu
soft-state initialization in the same order. That kind of chaos is
crazy and fragile. Improve the acpi resume call sequence and unify order
of the operations.


# 1.371 02-Jul-2019 jcs

acpi_set_gpehandler: turn edge arg into flags, add support for
passing GPE_DIRECT which directly executes the supplied callback and
clears a level interrupt rather than going through acpi_taskq which
can be slow when other driver callbacks are in the queue.

This should not be used for GPE handlers that need to do anything
ACPI-related.

ok kettenis


# 1.370 10-Jun-2019 kettenis

Print proper ACPI version number.

ok mlarkin@


# 1.369 08-Jun-2019 kettenis

When establishing the mapping between ACPI device nodes and PCI devices,
skip PCI host bridges and devices that aren't there.

ok patrick@


# 1.368 07-Jun-2019 kettenis

Introduce acpi_getsta() and use it to eliminate some duplicated code.

ok patrick@


# 1.367 12-May-2019 kettenis

Make sure the extended PM register descriptions actually exist before
using them. Fixes machines with ancient ACPI 1.0 (such as recent
hypervisors).

ok deraadt@


# 1.366 12-May-2019 kettenis

Add support for mapping ACPI PM registers using the "extended" gasio
representation from the FADT. Mostly fixes the Lanner NCA-1510.

ok lteo@, mlarkin@, deraadt@


# 1.365 11-May-2019 lteo

Fix incorrect error message.

ok kettenis@


# 1.364 08-May-2019 tedu

free size. the allocation is the header plus the length.


Revision tags: OPENBSD_6_5_BASE
# 1.363 04-Apr-2019 kettenis

Avoid potential null-pointer dereference. Found by Kent R. Spillner.

ok deraadt@, jsg@


# 1.362 20-Jan-2019 jsg

fix SMALL_KERNEL build after changes in rev 1.361
reported by naddy@ ok deraadt@


# 1.361 19-Jan-2019 tedu

Add a pwraction sysctl that controls what the power button does on acpi.
By default, nothing changes -- shutdown is initiated. But allows turning
power button into a sleep button if desired.
(grudging) ok from a few parties


# 1.360 26-Oct-2018 kettenis

Add acpipci(4) on amd64. For now this only calls the PCI-specific _OSC
method to let the ACPI implementation know what features we support.


Revision tags: OPENBSD_6_4_BASE
# 1.359 30-Aug-2018 kettenis

Move softbutton check before the check that skips attaching certain acpi
devices. Fixes regression caused by matching on _CID in addition to
matching on _HID.

ok matthieu@, mlarkin@


# 1.358 25-Aug-2018 kettenis

Match ACPI devices based on _CID if no match for _HID is found.

ok mpi@


# 1.357 19-Aug-2018 kettenis

Add support for multiple PCI segments. Only really implemented for arm64
for now as amd64/i386 firmware still caters for legacy OSes that only
support a single PCI segment.

ok patrick@


# 1.356 03-Aug-2018 kettenis

Let ahci(4) match on _CLS instead of _HID when attaching at acpi(4). Avoids
having to add many more _HID entries to the match table.

ok deraadt@, mlarkin@


# 1.355 10-Jul-2018 kettenis

Make legacy interrupts work in acpipci(4).

ok patrick@


# 1.354 05-Jul-2018 kettenis

Add acpipci(4), a driver that supports generic ECAM-compatible PCI host
bridges based on information provided by ACPI.

ok mlarkin@


# 1.353 04-Jul-2018 kettenis

Properly pass around the PCI "chipset tag" in acpi(4) and refactor
acpimcfg(4) to call an MD initialization functions that sets up a tag for
PCI ECAM.

ok guenther@, mlarkin@, krw@


# 1.352 01-Jul-2018 kettenis

Move acpi(4) attach glue into acpi_machdep.c.

ok guenther@, deraadt@


# 1.351 01-Jul-2018 kettenis

Add support for _DSD Device Properties.

ok mlarkin@


# 1.350 30-Jun-2018 kettenis

Add intr_enable() function, intended for MI use to amd64 and i386 and use
this in the acpi(4) suspend/resume code paths.

ok deraadt@


# 1.349 29-Jun-2018 kettenis

Use <stdint.h> types.

ok mpi@, jung@, krw@, deraadt@


# 1.348 29-Jun-2018 mlarkin

ACPI: Allow (with warning) GPE handler reassignment, instead of returning
a failure code.

ok kettenis


# 1.347 26-Jun-2018 mlarkin

knf


# 1.346 26-Jun-2018 kettenis

Let acpi(4) pass down the bus dma tag.

ok deraadt@


# 1.345 25-Jun-2018 kettenis

Reorganize acpi(4) code a bit in preparation for upcoming arm64 support.

ok deraadt@


# 1.344 20-May-2018 kettenis

Wake up the acpi thread after scheduling a gpio event.

ok mpi@


# 1.343 17-May-2018 kettenis

Sprinkle some #ifndef SMALL_KERNEL such that RAMDISK kernels cuild again.

pointed out by stsp@


# 1.342 17-May-2018 kettenis

Implement GenericSerialBus OpRegion support.

ok mlarkin@


# 1.341 27-Mar-2018 kettenis

Add acpicmos(4), a driver that implements SystemCMOS OperationRegion
access support. This fixes machines where the AML doesn't check whether
support for this OperationRegion type has been registered by the OS.

ok mlarkin@


Revision tags: OPENBSD_6_3_BASE
# 1.340 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.339 10-Feb-2018 deraadt

Syncronize filesystems to disk when suspending. Each mountpoint's vnodes
are pushed to disk. Dangling vnodes (unlinked files still in use) and
vnodes undergoing change by long-running syscalls are identified -- and
such filesystems are marked dirty on-disk while we are suspended (in case
power is lost, a fsck will be required). Filesystems without dangling or
busy vnodes are marked clean, resulting in faster boots following
"battery died" circumstances.
Tested by numerous developers, thanks for the feedback.


# 1.338 08-Feb-2018 deraadt

After flushing disks, try again to release all memory. We don't need to
save dirty memory to the hibernate space.


# 1.337 08-Feb-2018 deraadt

Toss all releaseable memory, because fragmentation can get in the way
of allocating the hibernate playpen.


# 1.336 08-Feb-2018 deraadt

sr_quiesce() is a new approach for ensuring that softraid drains
output to the disks.

This is part of a larger suspend/resume filesystem-safety diff, which
has been worked on for a couple of months already. Tests by job, krw,
beck, benno, and others. Sometimes even by snapshot users...


# 1.335 29-Nov-2017 kettenis

Add support for GPIO-signaled events.

ok mlarkin@


# 1.334 16-Nov-2017 jcs

Add PCI attachment for dwiic(4) needed by Intel 100 Series machines

ok kettenis


# 1.333 14-Nov-2017 kettenis

Add a few more PNP IDs to the skip lists.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.332 17-Aug-2017 stsp

Make dwiic(4) attach its ACPI parent dependencies.
This change makes the Asus E200HA keyboard work.

Original analysis and patch by Cesare Gargano
ok kettenis@


# 1.331 09-Aug-2017 dcoppa

Add "PNP0303" (8042 PS/2 Controller) to acpi_isa_hids[]

ok kettenis@


# 1.330 22-Jul-2017 jcs

acpibat: look for _BIX first before falling back on _BIF

ACPI 4.0 deprecated _BIF for battery status so some newer machines
are exporting _BIX which extends _BIF's package with a few extra
fields. When using _BIX, export the new discharge cycle count as a
sensor.

feedback from kettenis and anton
ok deraadt


# 1.329 20-Jul-2017 kettenis

Start a list of isa(4) device names such that we can avoid printing them
as "not configured".

ok mikeb@, millert@, deraadt@


# 1.328 08-Apr-2017 deraadt

Sensors are run as callbacks inside tasks. During suspend a sensor
could be running inside a driver that will be force-detached, or due
to tsleep end up disrupting the softstate/hardstate contract. At
suspend time, quisce all these callbacks by waiting for completion.
This issue has never been observed for real, but may be implicated in
suspend/resume failures.
ok kettenis guenther mlarkin


# 1.327 08-Apr-2017 deraadt

Two prototypes for acpi_maptable; merge to one place


Revision tags: OPENBSD_6_1_BASE
# 1.326 27-Mar-2017 deraadt

Now that hibernate_alloc() only has clean success/failure, don't
need to call hibernate_free() to clean up a partial mess.
ok mlarkin kettenis


# 1.325 13-Mar-2017 deraadt

allow ramdisks to build; ok jcs


# 1.324 12-Mar-2017 jcs

try to attach acpisbs first and if successful, don't attach acpibat


# 1.323 02-Mar-2017 natano

Add a new sysctl machdep.lidaction. The sysctl works as follows:

machdep.lidaction=0 # do nothing
machdep.lidaction=1 # suspend
machdep.lidaction=2 # hibernate

lidsuspend is just an alias for lidaction, so if you change one, the
other one will have the same value. The plan is to remove
machdep.lidsuspend eventually when people have upgraded their
/ets/sysctl.conf.

discussed with deraadt, who came up with the new MIB name
no objections mlarkin
ok stsp halex jcs


# 1.322 28-Feb-2017 natano

Untangle abstract sleep modes from ACPI sleep states. This paves the way
for further work in the area.

idea and ok deraadt
ok mlarkin


# 1.321 22-Feb-2017 jcs

add acpisbs, an acpi smart battery subsystem driver reading data
over smbus

currently disabled because it conflicts with acpibat

ok deraadt, kettenis


# 1.320 14-Jan-2017 kettenis

Revert aml_rdpciaddr changes; breaks several machines that were working
before.


# 1.319 11-Jan-2017 jsg

Increase the size of the buffers used for _HID and _CID strings to be
able to fit "Hyper_V_Gen_Counter_V1".

ok mlarkin@ deraadt@


# 1.318 08-Jan-2017 kettenis

Make aml_rdpciaddr() work for for devices behind bridges. The code now
uses the mapping between AML nodes and PCI devices that we establish.
Because _INIT methods may end up calling aml_rdpciaddr(), make sure we
create that mapping early. Also handle devices that aren't actually present.
These devices are now included in the mapping and reads will return an
all-ones patterm whereas writes are a no-op..

ok mlarkin@


# 1.317 25-Oct-2016 pirofti

Add the resource index to aml_parse_resource() callback function.

This is needed by an upcoming acpiec commit that handles machines breaking
the current ACPI specifications.

Change suggested by and ok kettenis@, guenther@


# 1.316 18-Sep-2016 guenther

If the APIC table (MADT) isn't present then ci_acpi_proc_id won't be set
when acpicpu tries to attach, leading to a NULL dereference. The ACPI
processor object on the Dells where this was reported is bogus and useless
anyway so apply the same test when deciding whether to attach to processor
objects as acpicpu would when matching them to cpus.

Problem reported by Eivind Eide (xenofil (at) gmail.com) and
Johan Huldtgren (johan+openbsd-bugs (at) huldtgren.com).
Testing by Eivind Eide.
ok kettenis@ mlarkin@


# 1.315 03-Sep-2016 naddy

Write the system time back to the RTC every 30 minutes.
This fixes the problem that long-running machines which were not
shut down properly would reboot with a badly offset system time.

hints and ok kettenis@


# 1.314 31-Aug-2016 mlarkin

Don't put a device in the wake device list if _STA says it's not there.

Fixes at least one machine that woke up from sleep / poweroff immediately.

ok kettenis@, tested by kettenis and martijn@


# 1.313 28-Jul-2016 kettenis

Store the acpi processor ID/UID in struct cpu_info, and use it to attach
acpicpu(4) drivers to the right cpu(4).

ok mlarkin@, guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.312 10-Jun-2016 kettenis

The power management timer has been made optional in ACPI 5.0A, so don't
attempt to attach acpitimer(4) if the timer isn't present.

Pointed out by jcs@

ok jcs@


# 1.311 08-May-2016 kettenis

Make sure devices listed by a device's _DEP method are attached before
we attach the device itself. The _DEP methode, introduced in ACPI 5.0,
is a hint that the device in question depends on OpRegion support from
the devices listed by _DEP, so we have to order them the proper way.
To prevent us from attaching those devices again when we encounter them
later walking down the device tree, keep track of our attempts to
attach a device driver to them using aflag in the aml_node structure.

ok guenther@


# 1.310 25-Apr-2016 pirofti

We attach acpihpet(4) and acpiec(4) without using the HID, so do not
report it in the dmesg.

OK deraadt@, mlarkin@.


# 1.309 02-Apr-2016 kettenis

Reduce dmesg spam by knocking out some acpi hardware IDs that don't need a
driver.

ok mpi@


# 1.308 29-Mar-2016 kettenis

Check if a device is present (using _STA) in acpi_foundhid. This prevents
us from reporting devices as "not configured" that aren't actually present,
and allows us to remove duplicated code from several drivers.

ok deraadt@, ok jsg@


# 1.307 29-Mar-2016 kettenis

Implement a cleaner way to prevent acpivout(4) from attaching if we have
a platform-specific method of controlling the backlight.

ok millert@, mlarkin@, jung@


# 1.306 28-Mar-2016 kettenis

Adding each and every new acpi device driver to acpi_foundhid() is
gettinga bit of the burden. And it really isn't how our autoconf
framework is supposed to work for busses that can be enumerated. All
the drivers already check for a matching _HID in their attach
function. So we can just drop the checks in acpi_foundhid() and let
the drivers sort themselves out.

Print a "not configured" line for devices for which we don't attach adriver.
This may be a bit much as there are devices for which we will not have a
driver. This will be removed if it causes too much dmesg spam.

ok deraadt@, mlarkin@


Revision tags: OPENBSD_5_9_BASE
# 1.305 17-Jan-2016 jsg

Take acpi_foundhid() outside of #ifndef SMALL_KERNEL so the ramdisk
kernel can find and attach dwiic(4) and sdhc(4).


# 1.304 17-Jan-2016 jsg

remove an unused global variable


# 1.303 14-Jan-2016 kettenis

Attach dwiic(4) on the Intel Bay Trail i2c controllers as well.


# 1.302 12-Jan-2016 kettenis

Improve emulation of PM registers on Hardware-redcued ACPI by emulating
SPL_TYPx, SLP_EN and WAK_STS using the new SLEEP_STATUS_REG and
SLEEP_CONTROL_REG registers. Makes the transition into S5 (powerdown) work
on the ASUS X205TA.

ok deraadt@, mlarkin@


# 1.301 12-Jan-2016 jcs

Add dwiic, a driver for the Synopsys DesignWare i2c controller found
on the Samsung ATIV Book 9 laptop. This initial version only
supports ACPI config/attachment.

Add ihidev, a HID-over-i2c driver largely based on uhidev. dwiic
handles attaching ihidev devices found in ACPI.

Add ims, a HID-over-i2c mouse/trackpad driver to get basic cursor
and button functionality from HID-compliant i2c trackpads.

ok kettenis deraadt


# 1.300 11-Jan-2016 kettenis

Make sdhc(4) attach to hardware IDs "80860F14" and "PNP0FFF".


# 1.299 09-Jan-2016 kettenis

Initial support for Hardware-Reduced ACPI. This skips the legacy mode checks
by emulating the PM_CNT register such that SCI_EN is always enabled. It also
skip attaching acpitimer(4) since the relevant registers are no longer there.

ok deraadt@, mlarkin@


# 1.298 06-Jan-2016 kettenis

Follow the spec and check the SCI_EN bit to see if ACPI has already
been enabled and don't bother enabling it again if it is. This should fix
systems that don't implement legacy mode and therefore set ACPI_ENABLE and
ACPI_DISABLE to zero.

ok mlarkin@, yasuoka@


# 1.297 23-Nov-2015 reyk

Back out the CRITICAL change, turn it back from 10% to 25%. Some
people with old/broken batteries didn't like this change, and the
underlying heuristics are wrong.


# 1.296 22-Nov-2015 reyk

The "CRITICAL" APM battery level, that is exported/faked by the ACPI
stack, is currently set to 25%. With 25% battery left, a typical
modern laptop will have at least one hour runtime left. It is an
arbitrary value, and other APM power layers in the kernel set CRITICAL
to something like 5% or 10%, so set it to 10% for now.

OK mlarkin@


# 1.295 28-Sep-2015 deraadt

In low-level suspend routines, set cold=2. In tsleep(), use this to
spit out a ddb trace to console. This should allow us to find suspend
or resume routines which break the rules. It depends on the console
output function being non-sleeping.... but that's another codepath which
should try to be safe when cold is set.
ok kettenis


# 1.294 06-Sep-2015 deraadt

clarify malloc arguments (base + length...)


# 1.293 28-Aug-2015 deraadt

fairly simple sizes for free(); ok tedu


# 1.292 20-Aug-2015 kettenis

Respect the access size when reading or writing to pci config space.
This version also makes sure writes are properly aligned, preventing
the panics seen with the previous incarnation of this change.

Fixes battery status passthrough in vmware, brightness keys on my x220
and who knows what else.

ok mlarkin@, deraadt@


Revision tags: OPENBSD_5_8_BASE
# 1.291 04-Aug-2015 deraadt

If we are setting ACPI_PM1_BM_RLD do it only once, and remember to
repeat this action at resume time.
ok mlarkin guenther


# 1.290 29-Jul-2015 deraadt

Something subtle in previous revision is locking up suspend or booting
on some laptops. Mailing lists threads mention x220 and Asus machines.
Reverting for now.


# 1.289 21-Jul-2015 kettenis

iRespect the access size when reading or writing to pci config space.
Fixes battery status passthrough in vmware and who knows what else.

Tested by many.

ok mlarkin@, deraadt@


# 1.288 17-Jul-2015 mlarkin

fix comments


# 1.287 26-Mar-2015 jsg

Use the 0x10 extended capability to determine if the trackstick
buttons are wired to the trackpad and need to be re-routed to the
trackstick. Without this change the buttons on 2015 thinkpads
get picked up as extended buttons that show up as scroll up/down.

Remove the X1 Carbon 2015 (LEN0048) and X250 (LEN0046) from the
top button area/soft buttons quirks list. Also avoid using the
quirk list entirely if the capability bit is set.

Based on work from jcs@ with feedback from mpi@
ok mpi@ jcs@


# 1.286 20-Mar-2015 kettenis

The ACPI standard says that a PCI function number of FFFF can be used to refer
to all functions on a device. This makes no sense in the context of accessing
PCI config space. Nevertheless stsp@ has a machine with AML that tries to
do config space reads from a device where _ADR encodes function number FFFF.
This is almost certainly buggy AML, but since Linux (and presumably Windows)
boot just fine on this machine we need to work around this. Linux effectively
treats function FFFF as function 7, which isn't present on this machine.
Instead we simulate a read from a non-existent device by returning a bit
pattern of all ones. Writes to function FFFF will still panic.

ok mlarkin@


# 1.285 16-Mar-2015 deraadt

Reduce use of SMALL_KERNEL in acpi, bringing acpiec driver and more gpe
handling into RAMDISK. This is now possible because the install media
has ample room. The goal is to reduce special cases where we may be
skipping (unknown) important operations...
ok mlarkin kettenis


# 1.284 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.283 07-Feb-2015 deraadt

New framework that allows hibernate to pass in entropy from it's fresh
boot.
ok mlarkin


# 1.282 06-Feb-2015 mlarkin

Fix a hibernate crash on some machines due to unmapping a page that
may not have been mapped previously (in the failure to hibernate case).

Also ensure that the lowmem ptp is mapped in all cases (not just MP).

ok kettenis


# 1.281 17-Jan-2015 deraadt

resume_randomness() before spinning up other cpus...


# 1.280 15-Jan-2015 jsg

Define a new wscons mouse type for Synaptics clickpad devices that lack
physical buttons. This will be used if the acpi pnpid for the mouse
matches a list provided by Synaptics found in Linux.

Combined with the xenocara changes this will remove the need for an xorg
config file for the x240/t440/t540 generation of thinkpads.

ok matthieu@ shadchin@ kettenis@


# 1.279 11-Jan-2015 kettenis

Tolerate checksum errors in ACPI tables (but complain about them). Apparently
there are machines out there with broken BIOSen with such checksum errors in
thier RSDT tables. Windows runs fine on these machines, so it probably does
not check. Linux ignores checksum errors as well (and prints a warning).

ok deraadt@, krw@


# 1.278 18-Dec-2014 deraadt

suspend and resume the RNG (at what looks like the right spots)
ok reyk djm


# 1.277 09-Dec-2014 doug

Replace some malloc(n*size,...) calls with mallocarray().

ok tedu@ deraadt@


# 1.276 06-Dec-2014 mpi

If "machdep.lidsuspend" is not set, do not go back to sleep if the lid
is still closed when resuming. This allows docked laptop to be resumed
without opening them.

ok mlarkin@


# 1.275 23-Nov-2014 mlarkin

If a machine has no _LIDs, then none can be open. This diff fixes a problem
where machines without any _LIDs were going back to sleep after hibernate
due to the recent suspend-if-lid-closed-on-resume diff. Noticed on qemu,
which doesn't have any _LIDs.

discussed with deraadt


# 1.274 18-Nov-2014 krw

Nuke yet more obvious #include duplications.

ok miod@


# 1.273 08-Nov-2014 mlarkin

If resuming from sleep (zzz/ZZZ) and the lid is still closed, go back to
sleep. This prevents accidental lid flex or slight opening in a backpack
from waking the machine up and leaving it resumed (powered on).

ok deraadt@


# 1.272 26-Sep-2014 kettenis

Rework piglet and pig allocation. Currently the piglet gets allocated
deep down in the suspend path, where it is really hard to recover from
allocation failure. So allocate the piglet early on in the suspend path.
Also change the piglet and piglet allocation functions to use km_alloc(9)
instead of doing pmemrange magic. This removes a bunch of code which, in the
case of the piglet allocation, is broken since it results in a NULL pointer
dereference. Also switch the piglet allocation to not wait. If we can't
allocate 16MB of phys contig memory on a halfway modern machine we're almost
certainly under a lot of memory pressure and we're better off not trying to
hibernate anyway.

ok mlarkin@


# 1.271 25-Sep-2014 kettenis

Bring back rev 1.249. Now that mp_setperf() has been fixed, it should be
safe to restore perflevel when resuming. Should fix the issues people
see with the processor running at full speed heating up the machine while
the apm command reports that the CPUs are running at a low speed.

ok dcoppa@, deraadt@


# 1.270 23-Sep-2014 kettenis

Release the acpi lock around calls to wsdisplay_suspend() and
wsdisplay_resume(). The X server may adjust the display brightness just
before/after the VT switch when suspending/resuming. In fact, it may
already be blocked adjusting the display brightness for some reason
unrelated to the suspend request.

The VT switch request will eventually time out, but the delay is annoying
and the failed VT switch will lead to problems after resume.

Needed for a future update of the xf86-video-intel driver.

ok mlarkin@


# 1.269 19-Sep-2014 kettenis

Use config_suspend_all(9).


# 1.268 08-Sep-2014 kettenis

Clear the PME Status bit on PCI devices in response to Device Wake events.
Fixes the interrupt storm on the 2009 Mac mini with WOL enabled on its
nfe(4) interface.

ok mlarkin@


Revision tags: OPENBSD_5_6_BASE
# 1.267 20-Jul-2014 mlarkin

Support hibernating to softraid crypto volumes.

much help and ok from deraadt@


# 1.266 16-Jul-2014 mlarkin

Reenable hibernate RLE support and flush+zero all memory after unpack.


# 1.265 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.264 12-Jul-2014 kettenis

Add a function to drop all clean pages on the page daemon queues and call
it when we hibernate.

ok mlarkin@, miod@, deraadt@


# 1.263 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.262 11-Jul-2014 mlarkin

Flush the buffercache to 16MB on hibernate and restore its previous max
size (kern.bufcachepercent) on resume, for better hibernate performance.

ok beck@


# 1.261 10-Jul-2014 blambert

remove unneeded header

ok mlarkin@


# 1.260 01-Jul-2014 kettenis

Adding an additonal delay after each access to SystemMemory or SystemIO
during autoconf makes no sense.

ok deraadt@


# 1.259 23-Jun-2014 kettenis

SystemMemory is used to access memory mapped registers on some machines, so we
must use the bus_space(9) API to access it instead of memcpy(9). Also make
sure we properly align access to fields that need more than byte alignment.

ok guenther@


# 1.258 21-May-2014 mlarkin

Fix invalid / missing format specifiers in several dnprintf statements
(used only when ACPI_DEBUG is enabled)

From a diff on tech@ by Fabian Raetz, with a minor change by me, tested
on i386 and amd64 with ACPI_DEBUG enabled and disabled.


# 1.257 25-Apr-2014 mlarkin

Defer the free of the hibernate structures until later, when it is safe to
do so.

Freeing the hibernate structures too early leads to inadvertent calls into
splx when we aren't ready for them yet. Found when trapping splx calls in
the resume path.

discussed with deraadt@ at m2k14


# 1.256 13-Apr-2014 mlarkin

Fix a badly worded debug message that implied the entire machine was going
to sleep, instead of just the acpi thread going to sleep.


# 1.255 31-Mar-2014 mpi

Change the order of operations during a suspend/resume cycle and
call bufq_quiesce() after executing the DVACT_QUIESCE handlers.

This should be safe since no disk nor controller drivers have such
handler but it will allow us to detach sd(4) devices attached to a
USB bus.

Another benefit pointed out by kettenis@ is that drivers that need
to read a firmware from the disk should be able to do it at resume
time in a DVACT_WAKEUP handler.

ok kettenis@, deraadt@


# 1.254 13-Mar-2014 dlg

get rid of the assumption that the head of the alldevs list is the
"mainbus" device. this breaks when mpath is enabled because it
attaches before mainbus and therefore takes the head position.

have autoconf provide device_mainbus() which looks up mainbus_cd,
and use that instead.

discussed with deraadt who just wants mpath stuff to move forward
despite there being many ways to shine this particular turd.


Revision tags: OPENBSD_5_5_BASE
# 1.253 21-Feb-2014 deraadt

Do the lid control further on in the resume sequence, since this is
AML, and will do WAITOK operations. While cold, that is a bad idea.
We seem to be safe with respect to the spec, I do not see any event
debounce issues here.
ok kettenis, tested by many


# 1.252 20-Jan-2014 claudio

Instead of checking sc->sc_revision <= 1 check that the
fadt->hdr_revision <= 1. This is a better check since it checks
the version of the proper descriptor. This allows the alix APU
to reboot since those boards have currently a wrong sc->sc_revision.
Committing this now so that it can be tested easier.
With and OK jsg@ also fine by derradt@


# 1.251 24-Dec-2013 kettenis

If the FADT has its SMI_CMD set to zero, assume we're only ACPI-only hardware
and don't need to disable SMI ownership of the ACPI hardware registers.

ok mlarkin@


# 1.250 23-Dec-2013 kettenis

Revert previous commit. It makes resume hang on some MP systems running
GENERIC.MP.


# 1.249 16-Dec-2013 kettenis

Restore hw.setperf upon resume like we do for apm(4) on i386. Seems we need
to do this fairly late in the resume to avoid hangs.

ok deraadt@, mlarkin@


# 1.248 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


# 1.247 06-Nov-2013 mpi

Add support for Power Resources for Dx states and the necessary hook
for PCI devices. This hook should be called twice, before and after
changing the power state of a PCI device.

Before setting the device to the new state, the ACPI layer will notify
every power resources linked to the device for that state and make sure
they are turned "_ON". After changing the state of the device, it will
decrement the reference of every power resources linked to that device
for the old state and turn them "_OFF" if they are no longer referenced.

This fixes the no-USB after resume problem seen on various ThinkPad,
problem initialy diagnosed with Alexander Polakov.

ok kettenis@, deraadt@


Revision tags: OPENBSD_5_4_BASE
# 1.246 01-Jun-2013 mlarkin

Stop printing ACPI wakeup devices in dmesg after the 16th wakeup device.

Helps with VMware VMs that report hundreds of wakeup devices.

ok reyk@, deraadt@


# 1.245 31-May-2013 bluhm

Fix the build for a kernel without wd and pciide in its config.
OK deraadt@ tedu@


# 1.244 30-May-2013 deraadt

Enforce ca_activate tree-walks over the entire heirarchy for all events,
cleaning up some shutdown-hook related code on the way.
(A few drivers related to sparc are still skipped at kettenis' request)
ok kettenis mlarkin, tested by many others too


# 1.243 18-Apr-2013 deraadt

pin the acpi thread to the BSP, because AML and SMI cannot be trusted. I
was certain we were doing this already, but it turns out no...


Revision tags: OPENBSD_5_3_BASE
# 1.242 24-Dec-2012 guenther

In acpiioctl(), Return though the splx() path if suser() returns an error

ok deraadt@


# 1.241 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


# 1.240 04-Oct-2012 ehrhardt

Use information provided by ACPI to attach secondary PCI host bridges.

ok kettenis@


# 1.239 07-Sep-2012 kettenis

Add a new function called acpi_pci_min_powerstate() that returns the lowest
power state for a PCI device for the power state (currently S3 or S4) we're
transitioning into. This function is probably too simplistic; there is a lot
of (silly) complexity in the ACPI spec.

ok deraadt@


Revision tags: OPENBSD_5_2_BASE
# 1.238 13-Jul-2012 pirofti

Disable acpivideo(4) on ASUS laptops.

There are some models that panic whenever an event is received.
Some cope, some don't. In order to get this right acpiec(4) requires
major rework. Until then let machines boot.

Discussed with deraadt@.


# 1.237 11-Jul-2012 mlarkin

for now, restrict hibernate to root in the acpi ioctl kernel interface.
normal rules for controlling this interface through apmd still apply.

ok deraadt@


# 1.236 09-Jul-2012 deraadt

tiny little oops in untested hibernate block


# 1.235 09-Jul-2012 deraadt

clean up ioctl() case statements


# 1.234 09-Jul-2012 deraadt

acpi_disable_onegpe() is not used; ok pirofti


# 1.233 24-May-2012 kettenis

If _MIN > _MAX consider a WordBus resource to be invalid. Fixes a panic on
the IBM x3550-M4.

ok mikeb@


# 1.232 29-Mar-2012 mlarkin

Missing #ifdef in previous diff


# 1.231 29-Mar-2012 mlarkin

Prevent hibernation attempts on machines with unsupported disk controllers.
The same check is also performed later in the hibernate sequence, but by
then it's too late to cleanly unwind (presently).

ok deraadt


# 1.230 26-Mar-2012 deraadt

simplify conditionals in acpi_sleep_state()
ok mlarkin


# 1.229 26-Mar-2012 deraadt

hook in the hibernate request code; half of this diff is from mlarkin
ok mlarkin


Revision tags: OPENBSD_5_1_BASE
# 1.228 20-Sep-2011 deraadt

Very early in suspend while the clock is still ticking realtime,
save the clock back to the rtc. The zaurus already did this.
ok phessler miod kettenis


Revision tags: OPENBSD_5_0_BASE
# 1.227 02-Jul-2011 nicm

kqueue attach functions should return an errno or 0, not a plain 1. Fix
the obvious cases to return EINVAL and ENXIO.

ok tedu deraadt


# 1.226 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.225 16-Jun-2011 pirofti

Import acpitoshiba driver.

This is originally written by Hiroyuki Aizu, ported and integrated
in our tree by Javier Vazquez <javama4 AT gmail> with tweaks and
suggestions by me.

Import prodded by deraadt@ and the lack of testers on tech@.


# 1.224 27-Apr-2011 jcs

attach acpithinkpad to newer lenovo models like the x120e

ok deraadt@


# 1.223 22-Apr-2011 jordan

Fix uninitialzied variables and formatting strings (-Wxxx errors)


Revision tags: OPENBSD_4_9_BASE
# 1.222 02-Jan-2011 jordan

Re-add code for ACPI taskqueue, eliminates need for state vars in acpi softc
Serialize processing ACPI tasks for sleep/powerdown/GPE event
ok deraadt@


# 1.221 31-Oct-2010 guenther

Revert last commit: it breaks resume on Thinkpads
"then please back it out" deraadt@


# 1.220 26-Oct-2010 jordan

Add task queue for ACPI gpe and notify handlers
This fixes eject on Dell Latitude dock and an issue on the Dell Mini
battery update.
ok deraadt


# 1.219 07-Oct-2010 jordan

Create separate functions for enabling wake and runtime gpes.
Remove old suspend/resume gpewalk function
ok deraadt, mlarkin


# 1.218 05-Oct-2010 jordan

split acpi_enable_onegpe to separate enable/disable functions
ok deraadt


# 1.217 05-Oct-2010 jordan

Added acpi_disable_allgpes function to clear GPE status
ok deraadt


# 1.216 05-Oct-2010 deraadt

clean up acpi_softc use more


# 1.215 05-Oct-2010 deraadt

delete a bit of #if 0 code


# 1.214 05-Oct-2010 deraadt

hide use of our global acpi_softc pointer a bit better


# 1.213 29-Sep-2010 deraadt

grab the acpi thread lock very early in acpi thread startup; not that it
is likely that anyone will race us... but why be careless.
ok kettenis


# 1.212 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.211 29-Aug-2010 jasper

- add a check for FWRITE here too.

ok miod@


# 1.210 27-Aug-2010 kettenis

Make PCI Power Management optional, and only enable it when acpi(4) attaches.

ok deraadt@


Revision tags: OPENBSD_4_8_BASE
# 1.209 08-Aug-2010 kettenis

Close race between the acpi thread and wscons ioctls running in process
context. We might rip this out post-release and replace it with a solution
that serializes things in a more obvious way.

ok deraadt@, miod@ and "those crazy texans who ok anything"


# 1.208 08-Aug-2010 deraadt

Report the acpi sleep states when on a RAMDISK; ok krw


# 1.207 07-Aug-2010 kettenis

Enforce acpi register access rescrictions.

ok mlarkin@, deraadt@, tested by marco@


# 1.206 07-Aug-2010 deraadt

upon resume, notify all the acpiac and acpibat drivers to update
their status. some laptops do not do this automatically.
ok kettenis canacar


# 1.205 06-Aug-2010 marco

Always call _PSW on _LID devices that support it to enable lid open to
resume the machine. Conversely disable it when the machine wakes up.

Tested by several ok deraadt


# 1.204 06-Aug-2010 deraadt

As part of a revamp of the PM1/GPE code, I write this diff to look for
unmanaged EN & STS bits in the PM1 register at interrupt time and report
them. As a side effect this splits the STS acknowledgement into two writes
(for power, and sleep) instead of one. The printf that is added (to spot
unmanaged STS bits) has not yet been triggered as far as we know.

Before the "write to PM1 registers at the right offset" diff went in, this
was not neccessary. But newer thinkpads do not have a working soft power
button without this diff.

We have no idea why.
ok mlarkin kettenis


# 1.203 05-Aug-2010 deraadt

PM1 power button events were generating 'fake events' to acpibtn PWRB,
but if there is no such device these were being tossed. Instead, arrange
for all power button handlers to flag a softc variable which is processed
by the thread; so that one place will know to request a powerdown (psignal
to init)
ok marco mlarkin


# 1.202 05-Aug-2010 deraadt

rename a variable which is annoyingly poorly named


# 1.201 05-Aug-2010 mlarkin

Write to the correct offset for various ACPI pmreg registers. Writing to
the correct register does properly clear the wake status bits and fixed
event bits, which allows some machines to have more than 1 suspend cycle.

bug spotted by deraadt@

tested by deraadt@ and myself on a half-dozen machines and others on
hackers@

ok deraadt@, marco@


# 1.200 05-Aug-2010 deraadt

Fix the power button. The pm1 register is 16 bits in size.
My mistake, a while back.
tested by krw


# 1.199 04-Aug-2010 kettenis

Print supported sleep states.

ok marco@, phessler@, deraadt@


# 1.198 03-Aug-2010 mlarkin

Use the proper flag for re-enabling certain hardware events (power btn,
sleep btn). The (incorrect) flag prevoiously used coincidentally had the
same value, so this shouldn't cause any different behavior than before.

tested on a variety of machines (i386, amd64, sp, mp) - no behavioral
change seen

ok deraadt@


# 1.197 28-Jul-2010 marco

Make all gasio failures look and behave the same.


# 1.196 28-Jul-2010 mlarkin

Delay a little longer on suspend before giving up. Fixes machines that
sometimes don't suspend because the sleep transition takes too long
(symptoms like failing to sleep but the backlight stays on).

ok deraadt@


# 1.195 27-Jul-2010 deraadt

remove XXX; EC can now be attached early when needed; ok jordan


# 1.194 27-Jul-2010 deraadt

more brackets feels good


# 1.193 27-Jul-2010 deraadt

XXX annotate a block in acpi_enter_sleep_state() that is pretty worrying


# 1.192 27-Jul-2010 deraadt

horrific indentation that nearly made me puke


# 1.191 27-Jul-2010 mlarkin

Set the sleeping indicator light on machines that support it, in case SMI
doesn't do it for us. Shows the 'moon' LED on older Thinkpads when
sleeping.

Tested on T43p, W500, T510 by me and on X61s by deraadt@.

ok deraadt@


# 1.190 27-Jul-2010 jordan

Early initialization of acpiec if ECDT table exists
Fixes hang when booting thinkpads while docked
ok deraadt


# 1.189 26-Jul-2010 deraadt

remove merge error which was commited


# 1.188 26-Jul-2010 pirofti

Add support for Sony ACPI hotkeys via a new driver: acpisony(4).

Currently it only works for the suspend button and tries to do right for
the brightness events, but I haven't found a Sony laptop that like to do
right so far.

In the future I want to make the brightness keys work on all Sony's and
also add support for the zoom hotkeys and whatever other funky keys I can
find on those things.

Okay deraadt@.


# 1.187 22-Jul-2010 deraadt

pretty large cleanup of SMALL_KERNEL


# 1.186 21-Jul-2010 deraadt

in the acpi GPE handler, late ack level interrupts not edge interrupts.
Duh.


# 1.185 21-Jul-2010 deraadt

Merge the edge and level gpe handlers into one (now possible since the
gpe structure tells us whether the gpe is edge or level)


# 1.184 21-Jul-2010 deraadt

delete #if 0 code we are not going to use; ok marco


# 1.183 20-Jul-2010 deraadt

make the mask code for acpi_enable_onegpe() match other similar code


# 1.182 20-Jul-2010 deraadt

Handle the acpi interrupt controller with proper edge/level handling,
wrapped in spltty to avoid racing against the interrupt controller.
Repair the gpe bit masking code while there, and do operations in the
right order
ok kettenis mlarkin, help from jordan at figuring out the order of
operations.


# 1.181 20-Jul-2010 matthew

When we're preparing to go to sleep, only call bufq_restart() if other
preparations failed.

ok kettenis@, thib@, deraadt@


# 1.180 19-Jul-2010 deraadt

permit compilation with ACPI_DEBUG


# 1.179 19-Jul-2010 deraadt

there is no need for a seperate dsdt_softc pointer; dsdt does not have a
softc but is just a part of the single acpi driver.
ok jordan


# 1.178 19-Jul-2010 deraadt

acpi_delay() is no longer needed; ok jordan


# 1.177 18-Jul-2010 mlarkin

Clear event status on resume, to avoid going back to sleep/reboot/shutdown
immediately after resume. Tested by a bunch of people on hackers@ on
various hardware. Fixes at least Toshiba NB200 and Gateway LT3103u.

ok marco


# 1.176 13-Jul-2010 deraadt

rename some thread related interfaces so they are logical


# 1.175 10-Jul-2010 jordan

Verify _ADR bdf is correct (some systems return 0xffff)
Works on Dell x9xx and Studio 1555


# 1.174 09-Jul-2010 jordan

Clear the noise


# 1.173 08-Jul-2010 jordan

Add mapping for ACPI device to PCI bdf (match autoconf tree)
Simplify resource parsing function to use buffer argument
Convert namespace linked lists to use queue macros
ok marco@, deraadt@


# 1.172 06-Jul-2010 deraadt

Use spltty() to lock downcalls from apm against the information being
modified by the acpi thread.
ok kettenis


# 1.171 06-Jul-2010 deraadt

Don't use a workq to do the suspend, because it races aginst the acpi
thread. Instead, just tell the acpi thread to do the suspend for us.
This makes apmd & zzz work correctly. While here, have acpithinkpad
attempt to post the event to apm before prompting the suspend itself.
ok kettenis marco mlarkin


# 1.170 05-Jul-2010 mlarkin

Reset register SCI_EN on resume. The ACPI spec clearly states we don't have
to do this, but many machines don't, or forget to, which leads to all sorts
of problems on resume (broken battery stats, overheating, failure to resume
more than once, etc).

tested by many, ok marco@, deraadt@


# 1.169 01-Jul-2010 thib

do bufq quiesce again, since vnd/the bufq_done call in
biodone has been fixed.


# 1.168 01-Jul-2010 jordan

Backout recent AML changes, breaks on R210 and others


# 1.167 01-Jul-2010 jordan

Simplified aml_parse_resource call, cleanup code
ok marco


# 1.166 01-Jul-2010 jordan

Fix subordinate bus # for multi-root PCI buses
ok marco, kettenis


# 1.165 30-Jun-2010 thib

Disable/partially backout the bufq quiesce changes as this
is causing havoc with vnds and release must be buildable.


# 1.164 29-Jun-2010 deraadt

Fix ramdisks (new code was placed inside a SMALL_KERNEL chunk)


# 1.163 29-Jun-2010 jordan

Add support for mapping ACPI to PCI devices
ok kettenis, deraadt


# 1.162 29-Jun-2010 kettenis

Call bufq_quiesce() on suspend to stop all I/O and bufq_restart on resume.
Makes suspend/resume work much better while doing disk I/O.

ok mlarkin@


# 1.161 29-Jun-2010 tedu

60 is a more reasonable guess for number of minutes in an hour than 100.
spotted by TeXitoi. ok deraadt


# 1.160 27-Jun-2010 jordan

Add _acpi_matchhids routine, precursor to pci device match


# 1.159 27-Jun-2010 mlarkin

Clear ACPI fixed event status on resume (power buttons/etc) so that some
machines don't immediately resume after suspending to S3.

Tested by kettenis@ and I on 5 or 6 machines, deraadt@ "yeah, looks good"


# 1.158 27-Jun-2010 jordan

Cleaned up acpi_maptable, adds to table list directly


# 1.157 07-Apr-2010 deraadt

If we have done a wsdisplay_suspend(), and ACPI decides it does not want to
suspend, we must roll back by calling wsdisplay_resume() or the virtual
console switching will be locked forever
ok oga


# 1.156 07-Apr-2010 kettenis

Make suspend/resume work on MP machines (running an MP kernel). Joint work
from mlarkin@ and me, with some amd64 fixes thrown in by deraadt@

ok marco@, deraadt@, pirofti@, mlarkin@


# 1.155 31-Mar-2010 kettenis

Bring /dev/apm support in line with apm(4). Make the suspend button send
a suspend request event to apmd(8) instead of suspending immediately. Also
keep track of whether /dev/apm and /dev/apmctl are currently open, such that
we can still suspend immediately if apmd(8) isn't running.

ok deraadt@, marco@, pirofti@, jsing@, oga@


# 1.154 30-Mar-2010 oga

Prevent the apmd/x races for good.

When we hit suspend time, go through all wsdisplays on the system. if
they are in mode MAPPED, but not MODE_DUMBFB then if possible do a full
vt switch to a !mapped vt, and prevent switching back until resume time.
This has to be called from MD code because this involves userland
running so that X can run the vt switch signal handler. This way, any
case where we are using the "poke registers from userland" model, we
will not be on the hardware when we go down, so the kernel can actually
handle thing properly.

Tested on several acpi laptops (by kettenis@ and ian@), x40 (me and
beck@ at LEAST) and zaurus (me). Maybe others, but if so I forgot who at
this time..

Idea from deraadt somewhere over the Faroe Islands (I thought of a
similar thing myself a while ago). Much prompting from him. Ok and
comments miod@


# 1.153 25-Mar-2010 oga

Fix & vs. && in the kqfilter code.

Based on a comparison with the apm code.

ok deraadt@, kettenis@


Revision tags: OPENBSD_4_7_BASE
# 1.152 26-Nov-2009 mlarkin

Visual (and on some machines, audio) feedback while the machine is
resuming via calls to aml _SST method. This also makes the "moon" LED on
thinkpads to blink during resume and cease when resume is completed.

ok pirofti@


# 1.151 26-Nov-2009 deraadt

Toggle into "cold" when we we disable interrupts, because tsleeping
in the AML interpreter and context switching into userland processes
tends to sometimes pop the ACPI trampoline page out of our address
space. Makes lots more machines work.
ok kettenis mlarkin


# 1.150 24-Nov-2009 jsg

in the resume path evaluate _WAK before dealing with device drivers
like the spec says, not after.

ok mlarkin@


# 1.149 24-Nov-2009 deraadt

abort suspends on SMP systems until that gets written; ok mlarkin


# 1.148 23-Nov-2009 mlarkin

pci should return the result of it's children's suspend/resume calls back
to its parent.

handle suspend failure case and unwind if devices have an activate function
that returns failure.

ok deraadt@, kettenis@


# 1.147 23-Nov-2009 mlarkin

acpi* devices need to call their children on suspend/resume.
ok deraadt@


# 1.146 23-Nov-2009 pirofti

Remove ACPI_SLEEP_ENABLED checks.

This enables by default the suspend/resume paths in the kernel.

Okay deraadt@.


# 1.145 23-Nov-2009 deraadt

knf and spacing that are super annoying


# 1.144 23-Nov-2009 mlarkin

Hook up Theo's new children suspend/resume function, as well as more ACPI
suspend/resume glue.

ok deraadt@


# 1.143 26-Oct-2009 deraadt

Do not do strncmp comparisons on dv_xname because longer device names which
look similar could arrive in the future. Instead, compare directly against
dv_cfdata->cf_driver->cd_name
Issue originally spotted by miod


# 1.142 26-Oct-2009 jordan

Verify checksum+address when loading ACPI tables.
Some systems had invalid entries in RSDT/XSDT.
ok marco@


# 1.141 23-Jul-2009 cnst

New aibs(4) driver for ASUSTeK AI Booster (ACPI ATK0110) hardware monitoring,
with sensor state support through limits provided by the ACPI.

Tested on several ASUS motherboards kindly networked by Sam Fourman Jr.

ok deraadt marco jordan


Revision tags: OPENBSD_4_6_BASE
# 1.140 03-Jun-2009 pirofti

Import acpi power resource driver. This is needed for suspend/resume logic.

Okay jordan@ and marco@.


# 1.139 03-Jun-2009 pirofti

Add acpivideo support. This adds brightness support for all laptops
except thinkpads, they will use the acpithinkpad driver. The driver is
also hooked into wsconsole. So brightness can be adjusted via:

$ wsconsctl display.brightness=<percentage>

This is very helpfull on some laptops that have a nasty bios and get two
steps instead of one when pressing the brightness button.

Tested on various dell, fujitsu, acer, samsung and other laptops.

Okay marco@, miod@. Suggestions from kettenis@.
Lots of reviews and help from miod@, thanks!


# 1.138 03-Jun-2009 jordan

Added ejectable bay for IDE cdroms
ok marco@


# 1.137 30-Apr-2009 marco

Oga changed apm ioctls and forgot to grep. Temporary work around by hitting
the sleep button when we get any request. This interface will chnage at
some point.
ok oga


# 1.136 19-Apr-2009 krw

Call acpi_sleep_walk() when powering down just like when entering
any other sleep state. This tells acpi wake devices to not wake up
the box until manual powering up resets their status. Makes my Dell
GX520 stay powered off in response to 'halt -p'.

In snaps for a while.

ok marco@ (several times) deraadt@


# 1.135 17-Apr-2009 pirofti

Add device ranking support for future sleep/wake developments.

okay marco@


# 1.134 11-Mar-2009 jordan

Cleanup.. use aml_evalinteger instead of aml_evalname
ok marco@


# 1.133 10-Mar-2009 jordan

Fix cut/paste error.. call _BFS not _PTS
ok marco@


Revision tags: OPENBSD_4_5_BASE
# 1.132 21-Feb-2009 marco

overzealous cut/paste; found by Vladimir Kirillov <proger@uaoug.org.ua>


# 1.131 20-Feb-2009 marco

Oops didn't mean for Debugger() to remain there.


# 1.130 19-Feb-2009 marco

suspend/resume bits so that we can develop this in tree. This is disabled.
code from mlarkin and me
help from art,toby,jordan and several others
ok jordan, go for it deraadt


# 1.129 10-Feb-2009 jordan

Fix null pointer for shutdown if acpi not enabled
ok marco@


# 1.128 04-Feb-2009 kettenis

Some systems that claim to be ACPI 2.0 apparently don't provide an XSDT
address. Make the code fall back on the RSDT on those systems.

From Joerg Niendorf <p3y80@internode.on.net>. Fixes PR 5986.


# 1.127 17-Jan-2009 krw

A couple of stray malloc/memset -> malloc(M_ZERO), a bit of knf and
lose a couple of unnecessary casts. No functional change.

Feedback from kettenis@

ok kettenis@ marco@


# 1.126 06-Nov-2008 marco

Collapse all sanity checks of hids into one function.

Originaly from jordan


Revision tags: OPENBSD_4_4_BASE
# 1.125 02-Jul-2008 fgsch

add acpivideo. for now almost an empty skel so others can work on
suspend/resume. eventually it will also manage output switching and
brightness where supported. prodded by marco@


# 1.124 11-Jun-2008 marco

Fix $OpenBSD$ for once and for all


# 1.123 08-Jun-2008 deraadt

aaa_dev was being set from an uninitialized variable; ok toby


# 1.122 07-Jun-2008 marco

Kill ACPIVERBOSE. It was never used and is worthless to boot.

comments jmc
ok toby


# 1.121 06-Jun-2008 marco

Shrink acpi some more for floppies and while here re-enable acpi on the
floppies. Fixes crazy sony vaio kettenis has.

tested by deraadt
ok deraadt jordan kettenis


# 1.120 01-Jun-2008 marco

Fix \n nit in SMALL_KERNEL.


# 1.119 01-Jun-2008 marco

Re-enable new parser. Lots of testing by various folks. Fits all media
now too.

ok kettenis deraadt


# 1.118 16-May-2008 dlg

back out jordans new parser.

it didnt have enough oks from the right people, it did break machines
that were previously working, and it bloated the RAMDISKS and install
media beyond the constraints of the media.

requested by deraadt@


# 1.117 14-May-2008 jordan

Adding new ACPI Parser code
Old guts of code still exists, needs to be torn out next
ok marco@
ok brad@


# 1.116 27-Apr-2008 jcs

add acpithinkpad(4), an acpi support driver for ibm/lenovo
thinkpads.

provides functionality that the bios did on older, non-acpi
thinkpads like respond to screen brightness keys and toggle the
bluetooth device on and off.

ok deraadt@ and marco@


# 1.115 24-Apr-2008 jsing

Introduce acpiasus(4), a driver for the ACPI based hotkeys found in many
ASUS laptops (including the ASUS EeePC) - largely based on NetBSD's
asus(4) driver. On the ASUS EeePC this allows us to enable/disable
wireless, change screen brightness and use the volume keys.

ok jsg@, weingart@


# 1.114 12-Apr-2008 kettenis

Get rid of acpi_s5 global variables; simply send SIGUSR2 instead of SUGUSR1
to tell init(8) to power down the machine.

ok krw@


Revision tags: OPENBSD_4_3_BASE
# 1.113 21-Jan-2008 jsg

Check against RESET_REG_SUP in FADT before doing the acpi reset dance
after all, not doing so appears to break more machines than it fixes.

ok marco@


# 1.112 05-Jan-2008 kettenis

Never attach more acpicpu(4) devices than cpu(4) devices. Always attach
the first acpicpu(4) device on non-MULTIPROCESSOR kernels, regardless of its
ACPI processor ID.

ok mikeb@


# 1.111 05-Dec-2007 deraadt

Hang acpi(4) below bios(4) instead of mainbus(4). This lets us move acpi(4)
before pcibios(4), and then.... if acpi(4) attaches, skip pcibios(4) since
it messes the machines up. Fixes claudio's HP dl320 G5, and almost assuredly
others.
ok kettenis


# 1.110 05-Dec-2007 deraadt

repair acpiopen/acpiclose and friends to handle the /dev/apmctl interface,
so that apmd(8) can actually work correctly against acpi(4), and thus,
even do the fancy -C/-A stuff; also tested by jmc


# 1.109 02-Dec-2007 fgsch

fix the trailing space for real now.


# 1.108 02-Dec-2007 fgsch

avoid trailing spaces on output; marco@ ok.


# 1.107 27-Nov-2007 kettenis

Fix off-by-one that made us attach acpicpu0 and acpicpu1 on GENERIC kernels.

ok marco@


# 1.106 25-Nov-2007 jsg

When available and ACPI is enabled use the ACPI reset method
to reboot. This is required for some quirky sis machines
and likely others to reboot.

Tested by many. 'commit early commit often' deraadt@


# 1.105 19-Nov-2007 deraadt

sigh, amd64 still uses that stupid acpi_enabled variable...


# 1.104 19-Nov-2007 deraadt

softc's come initialized to 0, you know...


# 1.103 19-Nov-2007 deraadt

store revision; from jsg


# 1.102 19-Nov-2007 kettenis

Initialize sc_facs earlier.

ok deraadt@, beck@, canacar@


# 1.101 16-Nov-2007 deraadt

fix the bus_space #define nightmare, so that amd64 and i386 are much more
uniform. as a result shared code like acpi needs less #ifdef's
ok marco kettenis


# 1.100 15-Nov-2007 deraadt

remove the #ifdef ACPI_ENABLE. the EC codepath is either safe enough (and
will be improved), or it is unsafe because AML assumes it must be there
ok almost everyone


# 1.99 15-Nov-2007 mikeb

Make acpicpu(4) attach to the existing CPUs only.

Problem reported by form@. Fix idea by kettenis@.
Lots of help from deraadt@. Tested by deraadt@, form@ and me.

ok deraadt marco


# 1.98 14-Nov-2007 deraadt

spacing


# 1.97 12-Nov-2007 deraadt

correct : vs , in attach-time messages


# 1.96 06-Nov-2007 deraadt

all ACPI_ENABLE kernels need acpi_interrupt()


# 1.95 06-Nov-2007 deraadt

move acpi_read_pmreg() and acpi_write_pmreg() out of SMALL_KERNEL so
that acpi ramdisks can compile (obvious solution)


# 1.94 05-Nov-2007 canacar

Establish the acpi interrupt earlier. Fixes problems with some
GENERIC.MP kernels not getting acpi interrupts. This is a
workaround until the real issue with late establishment is fixed.
ok weingart@, beck@, gwk@


# 1.93 03-Nov-2007 jordan

Added support for displaying wakeup devices
ok beck@,weingart@,gwk@


# 1.92 11-Oct-2007 kettenis

When walking the tree yo find _PRT methods, check whether the device is
actually enabled and functioning. Similar to what we do for _INI.

ok weingart@, beck@


# 1.91 08-Oct-2007 krw

More simple memset(,0,) -> M_ZERO changes. In this batch move to
size(*p) as the first malloc() parameter where p is declared locally
and thus easy to check. Add M_ZERO to gpe_table allocation in acpi.c
even though there is no obvious bzero or memset nearby.


# 1.90 13-Sep-2007 mk

Remove unnecessary memset() (see aml_evalnode(9)) and a bit of KNF.

ok toby


# 1.89 13-Sep-2007 weingart

Implement ACPI 6.5.1 spec tree walk for _STA and _INI.
Fixes mk's laptop. No regressions so far (thank you to the
testers).

ok gwk@, mk@, marco@


Revision tags: OPENBSD_4_2_BASE
# 1.88 17-Apr-2007 mk

Now that acpidock no longer hangs machines during boot (at least
according to the reports I've been getting) it should be safe to enable
it again, so remove the #if 0 stuff around it.

ok marco


# 1.87 11-Apr-2007 jordan

Added changes for C-State
ok marco@


# 1.86 26-Mar-2007 marco

Calculate minutes left more accurately.

From giovanni <qgiovanni@gmail.com>


Revision tags: OPENBSD_4_1_BASE
# 1.85 03-Mar-2007 mk

Stop looking for acpidock until we fix that stuff that breaks all pre-60
series ThinkPads with acpidock(4) enabled.


# 1.84 22-Feb-2007 marco

We were getting no interrupts in .MP. There are 2 fixes; one in
acpi_machdep.c and move the interrupt establishment down after ioapic
has been initialized.

ok jordan


# 1.83 21-Feb-2007 kettenis

Make an acpi-enabled GENERIC.MP work on systems that only support PIC mode.

ok marco@


# 1.82 19-Feb-2007 jordan

Added acpi_map_address function for mapping GAS/IO registers
ok marco@


# 1.81 18-Feb-2007 jordan

Added dynamic perfect hash for opcodes
ok marco@


# 1.80 18-Feb-2007 marco

Shave off another K pre compression.
Shuffle SMALL_KERNEL functions around so that we only have a single #ifndef


# 1.79 17-Feb-2007 marco

Add SMALL_KERNEL throught acpi to make it fit on boot media.
ok deraadt


# 1.78 13-Feb-2007 marco

Add smarter _INI code. This helps with some dissapearing devices.

ok kettenis jordan


# 1.77 31-Jan-2007 gwk

Introduce acpi_hasprocfvs thats "ACPI Has Processor Frequency and Voltage
Scalling" a global flag set by acicpu attach to indicate that ACPI is
handling fvs through the _PSS objects. This will fix disappearing
EST/powernow when booting kernels with acpi enabled, as acpicpu isnt
compiled into GENERIC yet. Also in cases where acpicpu is built in and
legacy EST/powernow work but acpi is missing the _PSS object, the legacy
routines will be used.

tested by dim@, and Nick Nauwelaerts <nick AT nauwelaerts.net>
ok marco@, dim@


# 1.76 22-Jan-2007 mk

Actually look for docking stations by looking for _DCK nodes.

ok marco


# 1.75 02-Jan-2007 marco

On some systems EC must be attached as the first device. Move EC from
generic _HID devices to its own routine to force first attach. Bug found
and debugged by joris@

Code mostly written by joris@


# 1.74 26-Dec-2006 marco

Add polling to devices that require it such as acpibat and acpitz.
Use the same tsleep/wakeup mechanism as the interrupt code to keep it non-
concurrent.


# 1.73 21-Dec-2006 deraadt

even more knf love


# 1.72 21-Dec-2006 marco

Remove some leftover acpiec gpe/interrupt code.


# 1.71 20-Dec-2006 deraadt

oops, name already used


# 1.70 20-Dec-2006 deraadt

rename __acpi_enable_gpe(); ok marco


# 1.69 20-Dec-2006 deraadt

acpi_unmap_pmregs() is not used, ok marco


# 1.68 19-Dec-2006 deraadt

simplify some low-level methods substantially (shrinking code); ok marco jordan


# 1.67 06-Dec-2006 mk

malloc(..., M_NOWAIT) with no NULL check is bad. If allocation fails,
complain, clean up, and bail out of attach.

input and ok gwk


# 1.66 28-Nov-2006 jordan

Fixed clearing arg for acpi_init_pic
ok kettenis@


# 1.65 27-Nov-2006 kettenis

Don't spam dmesg.


# 1.64 27-Nov-2006 jordan

Fixes for acpiprt routing
Post-parse fixup for namerefs
ok kettenis@


# 1.63 25-Nov-2006 marco

Don't handle acpiev events inside the gpe handler since this causes deadlocks.

ok jordan@


# 1.62 15-Nov-2006 kettenis

Hook up acpi apic interrupt routing support to generic acpi code.

ok jordan@


# 1.61 02-Nov-2006 marco

Kill acpiec attach function and add its functionality to the generic _HID
device attach

prodded and ok jordan


# 1.60 02-Nov-2006 marco

Fix comments to reflect reality.


# 1.59 19-Oct-2006 marco

Replace ACPI ioctls with APM ioctls. This makes ACPI talk the same protocol
as APM therefore userspace can now retrieve power information in the same
fashion as all other apm arches (zaurus, macppc, i386 etc).

To use force the use of acpi device like so: apmd -f /dev/acpi

ok jordan@


# 1.58 19-Oct-2006 jordan

Fixed GPE debug levels
Fixed ref counter on temporary bufferfields
DSDT parsing fully working on IBM Thinkpad
ok marco@


# 1.57 15-Oct-2006 jordan

Removed old GPE handler code remnants
ok marco@


# 1.56 12-Oct-2006 jordan

Massive ACPI changes
Fixed ACPI leaks
new ACPI API
ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.55 30-Jun-2006 gwk

KNF, No functional change,
"go at it" marco@


# 1.54 30-Jun-2006 jordan

Support for calling Embedded Controller via new GPE interface
ok marco@


# 1.53 30-Jun-2006 jordan

Added new code for GPE handling
Also works with ACPIEC; need to fixup acpiec.c to remove original interrupt handler
ok marco@


# 1.52 30-Jun-2006 jordan

Added offset argument for acpi read/write pmreg routines (new GPE code)
ok marco@


# 1.51 31-May-2006 todd

do not call acpi_powerdown() unless acpi has successfully initialized
ok gwk@ canacar@


# 1.50 29-May-2006 canacar

Add embedded controller support.
ok marco@ deraadt@ testing & ok gwk@


# 1.49 19-May-2006 canacar

Minimal thermal zone driver. Monitors thermal zone temperature,
shuts down the system if the 'critical temperature' is reached.
ok marco@


# 1.48 11-Apr-2006 marco

KNF.


# 1.47 11-Apr-2006 gwk

Add acpi_resume(struct acpi_softc *sc), which calls some AML methods,
detected at attach time, currently unused.

ok marco@


# 1.46 11-Apr-2006 gwk

Discover some ACPI sleep related methods, and call them at the
appropriate time in acpi_enter_sleep_state().

ok marco@


Revision tags: OPENBSD_3_9_BASE
# 1.45 26-Feb-2006 marco

Add acpicpu as a device.

ok jordan@


# 1.44 23-Feb-2006 marco

remove dead function.

ok jordan@


# 1.43 22-Feb-2006 marco

Print out all tables it found during autoconf. We'll need this data in
the future.


# 1.42 22-Feb-2006 marco

Make dmesg less verbose.

ok jordan.


# 1.41 22-Feb-2006 jordan

Added parsing of SSDT tables
ok marco@


# 1.40 22-Feb-2006 jordan

Added new aml_register to notify by device id
Moved powerdown code to button device
ok marco@


# 1.39 21-Feb-2006 marco

Readjust debug levels that crept in too low over the last few days.
KNF


# 1.38 21-Feb-2006 marco

Rig the power button so that the machine shuts down correctly.


# 1.37 20-Feb-2006 marco

Add acpibtn device. Remains disabled for now.


# 1.36 19-Feb-2006 jordan

Added code for aml_notify GPE notification callback
ok marco@


# 1.35 19-Feb-2006 grange

Fix acpi_enter_sleep_state() by replacing current ugly hack
with mostly correct sleeping state transition code.

ok brad@ marco@


# 1.34 19-Feb-2006 marco

jordan:
Enable interrupts.
Added peripheral code to make this work.

marco:
Replace softintr and other types of interrupts with a handler with context.
Fixed interrupt magic for GPEs.
Some KNF

This is work done by jordan and I but has to be a single commit.


# 1.33 17-Feb-2006 marco

Remove earlier introduced delays. We really only need them during autconf.

ok jordan@


# 1.32 17-Feb-2006 marco

Add a generic delay function as per the ACPI spec.

from jordan@


# 1.31 17-Feb-2006 marco

Use tsleep instead of delay when !cold.


# 1.30 17-Feb-2006 marco

Argh, we need delays between reads/writes. The backend reads (i2c most likely)
are so slow that values come back as 0 sometimes. We have to do this
despite the spec saying we shouldnt have to. Guess having a faster
parser than others can hurt sometimes.

Idea from jordan.


# 1.29 17-Feb-2006 jordan

Fixed code for handling _INI
Rearranged debug statements
ok marco@


# 1.28 16-Feb-2006 jordan

Added stub for calling _INI
Fixed aml_dowhile for NULL return
ok marco@


# 1.27 16-Feb-2006 jordan

Fixed parser/evaluator to retrieve battery info+state
Cleanup of parser
Dell laptops return battery status
ok marco@


# 1.26 06-Feb-2006 brad

fix a typo so this will compile on amd64.


# 1.25 04-Feb-2006 marco

When ACPI_ENABLED wasn't set but the devices were it had an unknown variable.

Pointed out by ian@.


# 1.24 03-Feb-2006 jordan

Rewrote aml parser yet again...
Fixed bus_space_map for i386/amd64
Added _BST and _BIF to acpibat to get status/info
ok marco@


# 1.23 20-Jan-2006 jordan

More evaluator changes
Removed old evaluator code
Fixed _HID value
ok marco@


# 1.22 19-Jan-2006 jordan

Added support for read/write of PCI Config space
Help jason@
ok marco@


# 1.21 18-Jan-2006 jordan

Added new evaluation routines
Added helper methods for buffer/field evaluation
Fixed broken indentation on dsdt.c

ok marco@


# 1.20 17-Jan-2006 jordan

Major overhaul of the aml parser. Evaluater temporarily disabled.

ok marco@


# 1.19 06-Jan-2006 grange

Check for ACPI control availability only if ACPI_ENABLE defined.


# 1.18 06-Jan-2006 grange

Informative error messages on attach, prettier dmesg.


# 1.17 06-Jan-2006 grange

printf -> dnprintf conversion for debug messages.


# 1.16 05-Jan-2006 grange

Find SLP_TYPx values and store them for future use in
acpi_enter_sleep_state().


# 1.15 05-Jan-2006 grange

Random KNF and cleanup


# 1.14 05-Jan-2006 grange

Provide symbolic names for the acpi sleeping states and use it.


# 1.13 28-Dec-2005 marco

Moved DEVNAME to a acpidev.h

ok jordan@


# 1.12 18-Dec-2005 sturm

add back comment about wrong code

ok marco


# 1.11 16-Dec-2005 marco

Remove test for kbd_reset. Prompted deraadt@, ok jordan@


# 1.10 16-Dec-2005 jordan

Added hooks for powerdown/power button press


# 1.9 16-Dec-2005 jordan

Cleaned up common power management register i/o
Added enter sleep state ioctl
Hooked power button interrupt
ok marco@


# 1.8 16-Dec-2005 marco

Remove autoconf chatter


# 1.7 16-Dec-2005 jordan

Moved DSDT AML parsing into parent ACPI driver
ok marco@


# 1.6 14-Dec-2005 marco

Add AC device glue.


# 1.5 14-Dec-2005 marco

Adjust debug printing levels to something more sane.


# 1.4 13-Dec-2005 marco

Add glue to attach devices to apci.


# 1.3 07-Dec-2005 marco

KNF


Revision tags: OPENBSD_3_8_BASE
# 1.2 10-Jul-2005 grange

Spacing cleanup.


# 1.1 02-Jun-2005 tholo

Start on a basic ACPI framework -- does not do much more than read out the
ACPI tables into kernel memory and attach ACPI and HPET timers currently.

In order to test this code, enabling the devices in GENERIC as well as
the ACPI_ENABLE option is needed. This code does not do any thermal
control yet, so this should be done with care depending on the platform.

In the tree so more people can contribute to making this more fully
featured.

Ok niklas@ grange@ tedu@


# 1.403 16-Dec-2021 anton

Attach com over acpi on amd64. Some hardware uses a different interrupt
assignment compared to the the legacy one supported by com over isa.
This causes the console to halt once userland takes over as no
interrupts are received. The actual address and irq can be read from
ACPI, kettenis@ already added support for arm64 which paved the way for
amd64.

Some consoles that previously attached over isa are now expected to
attach over acpi.

Thanks to patrick@ for testing on arm64.

ok kettenis@


# 1.402 07-Dec-2021 kettenis

Fix parsing of SR_IRQ resource descriptors.

ok patrick@, anton@


# 1.401 02-Nov-2021 deraadt

knf


# 1.400 30-Oct-2021 deraadt

Change hw.perfpolicy=auto by default, at startup. If the system has AC
power connected (default is yes when no driver differentiates) then default
to 100% performance. On battery, use the existing auto algorithm (which is
admittedly somewhat unrefined).
This change overrides the system/BIOS speed and puts OpenBSD in control.
As this happens very early during boot, besides speedups in all usage usage
patterns, some surprises: unhibernate and sysupgrade times are cut in half.
note: on a few architectures, the setperf fn pointer is changed late, and
thus the auto algorithm stops timeing out. kettenis and i will look for
a solution.
in snaps for more than a week.
ok kettenis


Revision tags: OPENBSD_7_0_BASE
# 1.399 20-Jul-2021 mlarkin

Fix trailing whitespace in a few places.

No code change.


# 1.398 09-May-2021 kettenis

Some x86 machines advertise the "hardware reduced" ACPI feature, advertise
S4 and S5 support, but fail to populate the SLEEP_CONTROL_REG and
SLEEP_STATUS_REG descriptions in the FADT. An example of such a machine
is the ASUS Zenbook 14 UM433DA. Any attempt to powerdown the machine
will result in a kernel crash. It turns out that using the legacy ACPI PM
registers works fine on this machien. So fall back on those registers
if SLEEP_CONTROL_REG and/or SLEEP_STATUS_REG aren't provided.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_9_BASE
# 1.397 15-Mar-2021 patrick

Add acpi_iommu_device_map(), which replaces the DMA tag with one that
is blessed with IOMMU magic, if available. This is mainly for arm64,
since on amd64 and i386 the IOMMU only captures PCIe devices, as far
as I know, which uses the pci_probe_device_hook(). This though is for
non-PCI devices attached through ACPI.

ok kettenis@


# 1.396 07-Mar-2021 patrick

Pass standard DMA tag to acpi(4) table drivers.

ok kettenis@


# 1.395 27-Dec-2020 deraadt

acpi_map_address() cannot be wrapped by SMALL_KERNEL anymore, as
it is used by acpihpet.c


# 1.394 25-Dec-2020 visa

Refactor klist insertion and removal

Rename klist_{insert,remove}() to klist_{insert,remove}_locked().
These functions assume that the caller has locked the klist. The current
state of locking remains intact because the kernel lock is still used
with all klists.

Add new functions klist_insert() and klist_remove() that lock the klist
internally. This allows some code simplification.

OK mpi@


# 1.393 17-Dec-2020 kettenis

Fix some issues with referencing named ACPI nodes from Packages.
These references need to be resolved at runtime rather than when
they're parsed such that they pick up the right values for those nodes
which can be changed when for example _INI methods run.

The current approach is to replace these reference with a string that
names the node in question. The problem with that is that packages
can also contain normal strings. Which means that we need special
code that depends on the context where the package is used.

This diff takes a different approach by retaining a reference when
parsing. Code that uses a package will need to resolve this reference
but there is no ambiguiety anymore.

ok patrick@


# 1.392 05-Dec-2020 kettenis

Add support for the _EVT method to the GPIO support code.

ok patrick@


Revision tags: OPENBSD_6_8_BASE
# 1.391 27-Aug-2020 jmatthew

Reorder the acpi attach process so we look at Processor() CPU nodes
after Device() ones, since we should prefer the newer node type.
If we see any Device() nodes, don't attach acpicpu(4) to any Processor()
nodes if they're also present. This also makes acpitz(4) and
acpipwrres(4) devices attach slightly later.

ok kettenis@, also tested by jmc@


# 1.390 16-Aug-2020 gnezdo

Remove sc_maxgpe from acpi_softc which was never read from

Remove a dead conditional from acpi_init_gpes while there.

ok jcs@


# 1.389 21-Jul-2020 deraadt

acpi can use IPL_BIO (a low interrupt) since it only enqueues operations for
later processing. The use of a high interrupt will predate suspend/resume
efforts, we had to redesign acpi to be non-reentrant obviously
discussed with kettenis, in snaps for more than a week


# 1.388 24-Jun-2020 cheloha

kernel: use gettime(9)/getuptime(9) in lieu of time_second(9)/time_uptime(9)

time_second(9) and time_uptime(9) are widely used in the kernel to
quickly get the system UTC or system uptime as a time_t. However,
time_t is 64-bit everywhere, so it is not generally safe to use them
on 32-bit platforms: you have a split-read problem if your hardware
cannot perform atomic 64-bit reads.

This patch replaces time_second(9) with gettime(9), a safer successor
interface, throughout the kernel. Similarly, time_uptime(9) is replaced
with getuptime(9).

There is a performance cost on 32-bit platforms in exchange for
eliminating the split-read problem: instead of two register reads you
now have a lockless read loop to pull the values from the timehands.
This is really not *too* bad in the grand scheme of things, but
compared to what we were doing before it is several times slower.

There is no performance cost on 64-bit (__LP64__) platforms.

With input from visa@, dlg@, and tedu@.

Several bugs squashed by visa@.

ok kettenis@


# 1.387 10-Jun-2020 jca

Give userland a consistent battery and A/C state after resume

Previously we refreshed the state of acpiac(4) and acpibat(4)/acpisbs(4)
by queueing fake ACPI events to be processed by the acpi thread.
acpibat(4) using ACPIDEV_POLL meant that its status could be refreshed
first, resulting in an APM_POWER_CHANGE event sent to userland before
the state of acpiac(4) was up to date. Because of this, apmd(8) could
see A/C unplugged when it was in fact plugged, possibly leading to
a wrong decision.

Instead of using fake ACPI events, refresh the status of acpiac(4) and
acpibat(4)/acpisbs(4) right away at DVACT_WAKEUP time. To reuse the
existing refresh code of those drivers, make their ACPI *_notify()
handlers responsible for sending APM_POWER_CHANGE events instead. Also
queue an APM_POWER_CHANGE event right after resume to retain existing
behavior.

This fixes the consistency of data shown to userland, and makes those
drivers less special.

Initially discussed last year (!) with mpi@ at Miod's place.
Feedback and ok jcs@, deraadt@ agrees with the use of DVACT_WAKEUP here.


# 1.386 29-May-2020 deraadt

dev/rndvar.h no longer has statistical interfaces (removed during various
conversion steps). it only contains kernel prototypes for 4 interfaces,
all of which legitimately belong in sys/systm.h, which are already included
by all enqueue_randomness() users.


# 1.385 14-May-2020 kettenis

Bring back the acpi(4) changes. This time around only use the ACPI _CRS
information on ACPI 5.0 and later.

ok krw@, patrick@


# 1.384 11-May-2020 jca

More accurate remaining power reporting with disparate batteries

Overall remaining power was computed as the average of the remaining
power of each battery, in percents. This doesn't work with batteries of
different capacity, so use the total capacity and remaining power
instead.

Reported by lidstah, successful tests by lidstah and benno@, ok kettenis@


# 1.383 08-May-2020 kettenis

Move parsing of _CRS into acpi(4) and pass the parsed address and interrupt
information in struct acpi_attach_args.

ok mpi@


Revision tags: OPENBSD_6_7_BASE
# 1.382 14-Apr-2020 kettenis

Implement acpi_getprop(), the ACPI equivalent of OF_getprop().

ok patrick@


# 1.381 12-Apr-2020 kettenis

Add support for the _CCA method. This method indicates whether DMA is
cache-coherent or not. To implement this, acpi(4) gets two bus_dma tags
and passes the appropriate one when attaching devices based on _CCA.
On i386/amd64, where for all practical purpose DMA is always cache-coherent,
the two tags are the same. But on arm64 they are distinct.

ok patrick@


# 1.380 07-Apr-2020 visa

Abstract the head of knote lists. This allows extending the lists,
for example, with locking assertions.

OK mpi@, anton@


# 1.379 03-Apr-2020 mpi

Fix incoherencies of errors returned by various *kqfilter routines.

EOPNOTSUPP is returned when there's no "filterops" for a given fd.
EINVAL when the requested filter isn't supported by the "filterops".
ENXIO means the underlying device is no longer valid/present.

ok visa@


# 1.378 20-Feb-2020 visa

Replace field f_isfd with field f_flags in struct filterops to allow
adding more filter properties without cluttering the struct.

OK mpi@, anton@


# 1.377 31-Dec-2019 visa

Use C99 designated initializers with struct filterops. In addition,
make the structs const so that the data are put in .rodata.

OK mpi@, deraadt@, anton@, bluhm@


# 1.376 31-Dec-2019 mpi

Convert infinite sleeps to {m,t}sleep_nsec(9).

ok kn@


# 1.375 31-Dec-2019 jsg

include sys/sensors.h for sensor use
don't depend on indirect inclusion from acpidev.h


Revision tags: OPENBSD_6_6_BASE
# 1.374 07-Sep-2019 kettenis

Revert acpipci(4) changes. Too many systems are broken. We'll try again
after 6.6 as been released. The acpireg.h change stays behind.


# 1.373 28-Aug-2019 kettenis

Use ACPI information to attach PCI busses like we do on arm64. There are a
few additional quirks though, and attaching the PCI busses is delayed to
replicate the existing code more closely. That may be changed in the
future. Also tweak how we handle MSI support and respect to ACPI flag
that says we shouldn't attempt to use MSIs.

Some fallout is expected.

ok patrick@


# 1.372 27-Aug-2019 deraadt

On amd64 the bootup and resume paths for SP/BP/SP were not performing
msr/pat, clock-startup, firmware upgrade, fpu initialization, and cpu
soft-state initialization in the same order. That kind of chaos is
crazy and fragile. Improve the acpi resume call sequence and unify order
of the operations.


# 1.371 02-Jul-2019 jcs

acpi_set_gpehandler: turn edge arg into flags, add support for
passing GPE_DIRECT which directly executes the supplied callback and
clears a level interrupt rather than going through acpi_taskq which
can be slow when other driver callbacks are in the queue.

This should not be used for GPE handlers that need to do anything
ACPI-related.

ok kettenis


# 1.370 10-Jun-2019 kettenis

Print proper ACPI version number.

ok mlarkin@


# 1.369 08-Jun-2019 kettenis

When establishing the mapping between ACPI device nodes and PCI devices,
skip PCI host bridges and devices that aren't there.

ok patrick@


# 1.368 07-Jun-2019 kettenis

Introduce acpi_getsta() and use it to eliminate some duplicated code.

ok patrick@


# 1.367 12-May-2019 kettenis

Make sure the extended PM register descriptions actually exist before
using them. Fixes machines with ancient ACPI 1.0 (such as recent
hypervisors).

ok deraadt@


# 1.366 12-May-2019 kettenis

Add support for mapping ACPI PM registers using the "extended" gasio
representation from the FADT. Mostly fixes the Lanner NCA-1510.

ok lteo@, mlarkin@, deraadt@


# 1.365 11-May-2019 lteo

Fix incorrect error message.

ok kettenis@


# 1.364 08-May-2019 tedu

free size. the allocation is the header plus the length.


Revision tags: OPENBSD_6_5_BASE
# 1.363 04-Apr-2019 kettenis

Avoid potential null-pointer dereference. Found by Kent R. Spillner.

ok deraadt@, jsg@


# 1.362 20-Jan-2019 jsg

fix SMALL_KERNEL build after changes in rev 1.361
reported by naddy@ ok deraadt@


# 1.361 19-Jan-2019 tedu

Add a pwraction sysctl that controls what the power button does on acpi.
By default, nothing changes -- shutdown is initiated. But allows turning
power button into a sleep button if desired.
(grudging) ok from a few parties


# 1.360 26-Oct-2018 kettenis

Add acpipci(4) on amd64. For now this only calls the PCI-specific _OSC
method to let the ACPI implementation know what features we support.


Revision tags: OPENBSD_6_4_BASE
# 1.359 30-Aug-2018 kettenis

Move softbutton check before the check that skips attaching certain acpi
devices. Fixes regression caused by matching on _CID in addition to
matching on _HID.

ok matthieu@, mlarkin@


# 1.358 25-Aug-2018 kettenis

Match ACPI devices based on _CID if no match for _HID is found.

ok mpi@


# 1.357 19-Aug-2018 kettenis

Add support for multiple PCI segments. Only really implemented for arm64
for now as amd64/i386 firmware still caters for legacy OSes that only
support a single PCI segment.

ok patrick@


# 1.356 03-Aug-2018 kettenis

Let ahci(4) match on _CLS instead of _HID when attaching at acpi(4). Avoids
having to add many more _HID entries to the match table.

ok deraadt@, mlarkin@


# 1.355 10-Jul-2018 kettenis

Make legacy interrupts work in acpipci(4).

ok patrick@


# 1.354 05-Jul-2018 kettenis

Add acpipci(4), a driver that supports generic ECAM-compatible PCI host
bridges based on information provided by ACPI.

ok mlarkin@


# 1.353 04-Jul-2018 kettenis

Properly pass around the PCI "chipset tag" in acpi(4) and refactor
acpimcfg(4) to call an MD initialization functions that sets up a tag for
PCI ECAM.

ok guenther@, mlarkin@, krw@


# 1.352 01-Jul-2018 kettenis

Move acpi(4) attach glue into acpi_machdep.c.

ok guenther@, deraadt@


# 1.351 01-Jul-2018 kettenis

Add support for _DSD Device Properties.

ok mlarkin@


# 1.350 30-Jun-2018 kettenis

Add intr_enable() function, intended for MI use to amd64 and i386 and use
this in the acpi(4) suspend/resume code paths.

ok deraadt@


# 1.349 29-Jun-2018 kettenis

Use <stdint.h> types.

ok mpi@, jung@, krw@, deraadt@


# 1.348 29-Jun-2018 mlarkin

ACPI: Allow (with warning) GPE handler reassignment, instead of returning
a failure code.

ok kettenis


# 1.347 26-Jun-2018 mlarkin

knf


# 1.346 26-Jun-2018 kettenis

Let acpi(4) pass down the bus dma tag.

ok deraadt@


# 1.345 25-Jun-2018 kettenis

Reorganize acpi(4) code a bit in preparation for upcoming arm64 support.

ok deraadt@


# 1.344 20-May-2018 kettenis

Wake up the acpi thread after scheduling a gpio event.

ok mpi@


# 1.343 17-May-2018 kettenis

Sprinkle some #ifndef SMALL_KERNEL such that RAMDISK kernels cuild again.

pointed out by stsp@


# 1.342 17-May-2018 kettenis

Implement GenericSerialBus OpRegion support.

ok mlarkin@


# 1.341 27-Mar-2018 kettenis

Add acpicmos(4), a driver that implements SystemCMOS OperationRegion
access support. This fixes machines where the AML doesn't check whether
support for this OperationRegion type has been registered by the OS.

ok mlarkin@


Revision tags: OPENBSD_6_3_BASE
# 1.340 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.339 10-Feb-2018 deraadt

Syncronize filesystems to disk when suspending. Each mountpoint's vnodes
are pushed to disk. Dangling vnodes (unlinked files still in use) and
vnodes undergoing change by long-running syscalls are identified -- and
such filesystems are marked dirty on-disk while we are suspended (in case
power is lost, a fsck will be required). Filesystems without dangling or
busy vnodes are marked clean, resulting in faster boots following
"battery died" circumstances.
Tested by numerous developers, thanks for the feedback.


# 1.338 08-Feb-2018 deraadt

After flushing disks, try again to release all memory. We don't need to
save dirty memory to the hibernate space.


# 1.337 08-Feb-2018 deraadt

Toss all releaseable memory, because fragmentation can get in the way
of allocating the hibernate playpen.


# 1.336 08-Feb-2018 deraadt

sr_quiesce() is a new approach for ensuring that softraid drains
output to the disks.

This is part of a larger suspend/resume filesystem-safety diff, which
has been worked on for a couple of months already. Tests by job, krw,
beck, benno, and others. Sometimes even by snapshot users...


# 1.335 29-Nov-2017 kettenis

Add support for GPIO-signaled events.

ok mlarkin@


# 1.334 16-Nov-2017 jcs

Add PCI attachment for dwiic(4) needed by Intel 100 Series machines

ok kettenis


# 1.333 14-Nov-2017 kettenis

Add a few more PNP IDs to the skip lists.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.332 17-Aug-2017 stsp

Make dwiic(4) attach its ACPI parent dependencies.
This change makes the Asus E200HA keyboard work.

Original analysis and patch by Cesare Gargano
ok kettenis@


# 1.331 09-Aug-2017 dcoppa

Add "PNP0303" (8042 PS/2 Controller) to acpi_isa_hids[]

ok kettenis@


# 1.330 22-Jul-2017 jcs

acpibat: look for _BIX first before falling back on _BIF

ACPI 4.0 deprecated _BIF for battery status so some newer machines
are exporting _BIX which extends _BIF's package with a few extra
fields. When using _BIX, export the new discharge cycle count as a
sensor.

feedback from kettenis and anton
ok deraadt


# 1.329 20-Jul-2017 kettenis

Start a list of isa(4) device names such that we can avoid printing them
as "not configured".

ok mikeb@, millert@, deraadt@


# 1.328 08-Apr-2017 deraadt

Sensors are run as callbacks inside tasks. During suspend a sensor
could be running inside a driver that will be force-detached, or due
to tsleep end up disrupting the softstate/hardstate contract. At
suspend time, quisce all these callbacks by waiting for completion.
This issue has never been observed for real, but may be implicated in
suspend/resume failures.
ok kettenis guenther mlarkin


# 1.327 08-Apr-2017 deraadt

Two prototypes for acpi_maptable; merge to one place


Revision tags: OPENBSD_6_1_BASE
# 1.326 27-Mar-2017 deraadt

Now that hibernate_alloc() only has clean success/failure, don't
need to call hibernate_free() to clean up a partial mess.
ok mlarkin kettenis


# 1.325 13-Mar-2017 deraadt

allow ramdisks to build; ok jcs


# 1.324 12-Mar-2017 jcs

try to attach acpisbs first and if successful, don't attach acpibat


# 1.323 02-Mar-2017 natano

Add a new sysctl machdep.lidaction. The sysctl works as follows:

machdep.lidaction=0 # do nothing
machdep.lidaction=1 # suspend
machdep.lidaction=2 # hibernate

lidsuspend is just an alias for lidaction, so if you change one, the
other one will have the same value. The plan is to remove
machdep.lidsuspend eventually when people have upgraded their
/ets/sysctl.conf.

discussed with deraadt, who came up with the new MIB name
no objections mlarkin
ok stsp halex jcs


# 1.322 28-Feb-2017 natano

Untangle abstract sleep modes from ACPI sleep states. This paves the way
for further work in the area.

idea and ok deraadt
ok mlarkin


# 1.321 22-Feb-2017 jcs

add acpisbs, an acpi smart battery subsystem driver reading data
over smbus

currently disabled because it conflicts with acpibat

ok deraadt, kettenis


# 1.320 14-Jan-2017 kettenis

Revert aml_rdpciaddr changes; breaks several machines that were working
before.


# 1.319 11-Jan-2017 jsg

Increase the size of the buffers used for _HID and _CID strings to be
able to fit "Hyper_V_Gen_Counter_V1".

ok mlarkin@ deraadt@


# 1.318 08-Jan-2017 kettenis

Make aml_rdpciaddr() work for for devices behind bridges. The code now
uses the mapping between AML nodes and PCI devices that we establish.
Because _INIT methods may end up calling aml_rdpciaddr(), make sure we
create that mapping early. Also handle devices that aren't actually present.
These devices are now included in the mapping and reads will return an
all-ones patterm whereas writes are a no-op..

ok mlarkin@


# 1.317 25-Oct-2016 pirofti

Add the resource index to aml_parse_resource() callback function.

This is needed by an upcoming acpiec commit that handles machines breaking
the current ACPI specifications.

Change suggested by and ok kettenis@, guenther@


# 1.316 18-Sep-2016 guenther

If the APIC table (MADT) isn't present then ci_acpi_proc_id won't be set
when acpicpu tries to attach, leading to a NULL dereference. The ACPI
processor object on the Dells where this was reported is bogus and useless
anyway so apply the same test when deciding whether to attach to processor
objects as acpicpu would when matching them to cpus.

Problem reported by Eivind Eide (xenofil (at) gmail.com) and
Johan Huldtgren (johan+openbsd-bugs (at) huldtgren.com).
Testing by Eivind Eide.
ok kettenis@ mlarkin@


# 1.315 03-Sep-2016 naddy

Write the system time back to the RTC every 30 minutes.
This fixes the problem that long-running machines which were not
shut down properly would reboot with a badly offset system time.

hints and ok kettenis@


# 1.314 31-Aug-2016 mlarkin

Don't put a device in the wake device list if _STA says it's not there.

Fixes at least one machine that woke up from sleep / poweroff immediately.

ok kettenis@, tested by kettenis and martijn@


# 1.313 28-Jul-2016 kettenis

Store the acpi processor ID/UID in struct cpu_info, and use it to attach
acpicpu(4) drivers to the right cpu(4).

ok mlarkin@, guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.312 10-Jun-2016 kettenis

The power management timer has been made optional in ACPI 5.0A, so don't
attempt to attach acpitimer(4) if the timer isn't present.

Pointed out by jcs@

ok jcs@


# 1.311 08-May-2016 kettenis

Make sure devices listed by a device's _DEP method are attached before
we attach the device itself. The _DEP methode, introduced in ACPI 5.0,
is a hint that the device in question depends on OpRegion support from
the devices listed by _DEP, so we have to order them the proper way.
To prevent us from attaching those devices again when we encounter them
later walking down the device tree, keep track of our attempts to
attach a device driver to them using aflag in the aml_node structure.

ok guenther@


# 1.310 25-Apr-2016 pirofti

We attach acpihpet(4) and acpiec(4) without using the HID, so do not
report it in the dmesg.

OK deraadt@, mlarkin@.


# 1.309 02-Apr-2016 kettenis

Reduce dmesg spam by knocking out some acpi hardware IDs that don't need a
driver.

ok mpi@


# 1.308 29-Mar-2016 kettenis

Check if a device is present (using _STA) in acpi_foundhid. This prevents
us from reporting devices as "not configured" that aren't actually present,
and allows us to remove duplicated code from several drivers.

ok deraadt@, ok jsg@


# 1.307 29-Mar-2016 kettenis

Implement a cleaner way to prevent acpivout(4) from attaching if we have
a platform-specific method of controlling the backlight.

ok millert@, mlarkin@, jung@


# 1.306 28-Mar-2016 kettenis

Adding each and every new acpi device driver to acpi_foundhid() is
gettinga bit of the burden. And it really isn't how our autoconf
framework is supposed to work for busses that can be enumerated. All
the drivers already check for a matching _HID in their attach
function. So we can just drop the checks in acpi_foundhid() and let
the drivers sort themselves out.

Print a "not configured" line for devices for which we don't attach adriver.
This may be a bit much as there are devices for which we will not have a
driver. This will be removed if it causes too much dmesg spam.

ok deraadt@, mlarkin@


Revision tags: OPENBSD_5_9_BASE
# 1.305 17-Jan-2016 jsg

Take acpi_foundhid() outside of #ifndef SMALL_KERNEL so the ramdisk
kernel can find and attach dwiic(4) and sdhc(4).


# 1.304 17-Jan-2016 jsg

remove an unused global variable


# 1.303 14-Jan-2016 kettenis

Attach dwiic(4) on the Intel Bay Trail i2c controllers as well.


# 1.302 12-Jan-2016 kettenis

Improve emulation of PM registers on Hardware-redcued ACPI by emulating
SPL_TYPx, SLP_EN and WAK_STS using the new SLEEP_STATUS_REG and
SLEEP_CONTROL_REG registers. Makes the transition into S5 (powerdown) work
on the ASUS X205TA.

ok deraadt@, mlarkin@


# 1.301 12-Jan-2016 jcs

Add dwiic, a driver for the Synopsys DesignWare i2c controller found
on the Samsung ATIV Book 9 laptop. This initial version only
supports ACPI config/attachment.

Add ihidev, a HID-over-i2c driver largely based on uhidev. dwiic
handles attaching ihidev devices found in ACPI.

Add ims, a HID-over-i2c mouse/trackpad driver to get basic cursor
and button functionality from HID-compliant i2c trackpads.

ok kettenis deraadt


# 1.300 11-Jan-2016 kettenis

Make sdhc(4) attach to hardware IDs "80860F14" and "PNP0FFF".


# 1.299 09-Jan-2016 kettenis

Initial support for Hardware-Reduced ACPI. This skips the legacy mode checks
by emulating the PM_CNT register such that SCI_EN is always enabled. It also
skip attaching acpitimer(4) since the relevant registers are no longer there.

ok deraadt@, mlarkin@


# 1.298 06-Jan-2016 kettenis

Follow the spec and check the SCI_EN bit to see if ACPI has already
been enabled and don't bother enabling it again if it is. This should fix
systems that don't implement legacy mode and therefore set ACPI_ENABLE and
ACPI_DISABLE to zero.

ok mlarkin@, yasuoka@


# 1.297 23-Nov-2015 reyk

Back out the CRITICAL change, turn it back from 10% to 25%. Some
people with old/broken batteries didn't like this change, and the
underlying heuristics are wrong.


# 1.296 22-Nov-2015 reyk

The "CRITICAL" APM battery level, that is exported/faked by the ACPI
stack, is currently set to 25%. With 25% battery left, a typical
modern laptop will have at least one hour runtime left. It is an
arbitrary value, and other APM power layers in the kernel set CRITICAL
to something like 5% or 10%, so set it to 10% for now.

OK mlarkin@


# 1.295 28-Sep-2015 deraadt

In low-level suspend routines, set cold=2. In tsleep(), use this to
spit out a ddb trace to console. This should allow us to find suspend
or resume routines which break the rules. It depends on the console
output function being non-sleeping.... but that's another codepath which
should try to be safe when cold is set.
ok kettenis


# 1.294 06-Sep-2015 deraadt

clarify malloc arguments (base + length...)


# 1.293 28-Aug-2015 deraadt

fairly simple sizes for free(); ok tedu


# 1.292 20-Aug-2015 kettenis

Respect the access size when reading or writing to pci config space.
This version also makes sure writes are properly aligned, preventing
the panics seen with the previous incarnation of this change.

Fixes battery status passthrough in vmware, brightness keys on my x220
and who knows what else.

ok mlarkin@, deraadt@


Revision tags: OPENBSD_5_8_BASE
# 1.291 04-Aug-2015 deraadt

If we are setting ACPI_PM1_BM_RLD do it only once, and remember to
repeat this action at resume time.
ok mlarkin guenther


# 1.290 29-Jul-2015 deraadt

Something subtle in previous revision is locking up suspend or booting
on some laptops. Mailing lists threads mention x220 and Asus machines.
Reverting for now.


# 1.289 21-Jul-2015 kettenis

iRespect the access size when reading or writing to pci config space.
Fixes battery status passthrough in vmware and who knows what else.

Tested by many.

ok mlarkin@, deraadt@


# 1.288 17-Jul-2015 mlarkin

fix comments


# 1.287 26-Mar-2015 jsg

Use the 0x10 extended capability to determine if the trackstick
buttons are wired to the trackpad and need to be re-routed to the
trackstick. Without this change the buttons on 2015 thinkpads
get picked up as extended buttons that show up as scroll up/down.

Remove the X1 Carbon 2015 (LEN0048) and X250 (LEN0046) from the
top button area/soft buttons quirks list. Also avoid using the
quirk list entirely if the capability bit is set.

Based on work from jcs@ with feedback from mpi@
ok mpi@ jcs@


# 1.286 20-Mar-2015 kettenis

The ACPI standard says that a PCI function number of FFFF can be used to refer
to all functions on a device. This makes no sense in the context of accessing
PCI config space. Nevertheless stsp@ has a machine with AML that tries to
do config space reads from a device where _ADR encodes function number FFFF.
This is almost certainly buggy AML, but since Linux (and presumably Windows)
boot just fine on this machine we need to work around this. Linux effectively
treats function FFFF as function 7, which isn't present on this machine.
Instead we simulate a read from a non-existent device by returning a bit
pattern of all ones. Writes to function FFFF will still panic.

ok mlarkin@


# 1.285 16-Mar-2015 deraadt

Reduce use of SMALL_KERNEL in acpi, bringing acpiec driver and more gpe
handling into RAMDISK. This is now possible because the install media
has ample room. The goal is to reduce special cases where we may be
skipping (unknown) important operations...
ok mlarkin kettenis


# 1.284 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.283 07-Feb-2015 deraadt

New framework that allows hibernate to pass in entropy from it's fresh
boot.
ok mlarkin


# 1.282 06-Feb-2015 mlarkin

Fix a hibernate crash on some machines due to unmapping a page that
may not have been mapped previously (in the failure to hibernate case).

Also ensure that the lowmem ptp is mapped in all cases (not just MP).

ok kettenis


# 1.281 17-Jan-2015 deraadt

resume_randomness() before spinning up other cpus...


# 1.280 15-Jan-2015 jsg

Define a new wscons mouse type for Synaptics clickpad devices that lack
physical buttons. This will be used if the acpi pnpid for the mouse
matches a list provided by Synaptics found in Linux.

Combined with the xenocara changes this will remove the need for an xorg
config file for the x240/t440/t540 generation of thinkpads.

ok matthieu@ shadchin@ kettenis@


# 1.279 11-Jan-2015 kettenis

Tolerate checksum errors in ACPI tables (but complain about them). Apparently
there are machines out there with broken BIOSen with such checksum errors in
thier RSDT tables. Windows runs fine on these machines, so it probably does
not check. Linux ignores checksum errors as well (and prints a warning).

ok deraadt@, krw@


# 1.278 18-Dec-2014 deraadt

suspend and resume the RNG (at what looks like the right spots)
ok reyk djm


# 1.277 09-Dec-2014 doug

Replace some malloc(n*size,...) calls with mallocarray().

ok tedu@ deraadt@


# 1.276 06-Dec-2014 mpi

If "machdep.lidsuspend" is not set, do not go back to sleep if the lid
is still closed when resuming. This allows docked laptop to be resumed
without opening them.

ok mlarkin@


# 1.275 23-Nov-2014 mlarkin

If a machine has no _LIDs, then none can be open. This diff fixes a problem
where machines without any _LIDs were going back to sleep after hibernate
due to the recent suspend-if-lid-closed-on-resume diff. Noticed on qemu,
which doesn't have any _LIDs.

discussed with deraadt


# 1.274 18-Nov-2014 krw

Nuke yet more obvious #include duplications.

ok miod@


# 1.273 08-Nov-2014 mlarkin

If resuming from sleep (zzz/ZZZ) and the lid is still closed, go back to
sleep. This prevents accidental lid flex or slight opening in a backpack
from waking the machine up and leaving it resumed (powered on).

ok deraadt@


# 1.272 26-Sep-2014 kettenis

Rework piglet and pig allocation. Currently the piglet gets allocated
deep down in the suspend path, where it is really hard to recover from
allocation failure. So allocate the piglet early on in the suspend path.
Also change the piglet and piglet allocation functions to use km_alloc(9)
instead of doing pmemrange magic. This removes a bunch of code which, in the
case of the piglet allocation, is broken since it results in a NULL pointer
dereference. Also switch the piglet allocation to not wait. If we can't
allocate 16MB of phys contig memory on a halfway modern machine we're almost
certainly under a lot of memory pressure and we're better off not trying to
hibernate anyway.

ok mlarkin@


# 1.271 25-Sep-2014 kettenis

Bring back rev 1.249. Now that mp_setperf() has been fixed, it should be
safe to restore perflevel when resuming. Should fix the issues people
see with the processor running at full speed heating up the machine while
the apm command reports that the CPUs are running at a low speed.

ok dcoppa@, deraadt@


# 1.270 23-Sep-2014 kettenis

Release the acpi lock around calls to wsdisplay_suspend() and
wsdisplay_resume(). The X server may adjust the display brightness just
before/after the VT switch when suspending/resuming. In fact, it may
already be blocked adjusting the display brightness for some reason
unrelated to the suspend request.

The VT switch request will eventually time out, but the delay is annoying
and the failed VT switch will lead to problems after resume.

Needed for a future update of the xf86-video-intel driver.

ok mlarkin@


# 1.269 19-Sep-2014 kettenis

Use config_suspend_all(9).


# 1.268 08-Sep-2014 kettenis

Clear the PME Status bit on PCI devices in response to Device Wake events.
Fixes the interrupt storm on the 2009 Mac mini with WOL enabled on its
nfe(4) interface.

ok mlarkin@


Revision tags: OPENBSD_5_6_BASE
# 1.267 20-Jul-2014 mlarkin

Support hibernating to softraid crypto volumes.

much help and ok from deraadt@


# 1.266 16-Jul-2014 mlarkin

Reenable hibernate RLE support and flush+zero all memory after unpack.


# 1.265 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.264 12-Jul-2014 kettenis

Add a function to drop all clean pages on the page daemon queues and call
it when we hibernate.

ok mlarkin@, miod@, deraadt@


# 1.263 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.262 11-Jul-2014 mlarkin

Flush the buffercache to 16MB on hibernate and restore its previous max
size (kern.bufcachepercent) on resume, for better hibernate performance.

ok beck@


# 1.261 10-Jul-2014 blambert

remove unneeded header

ok mlarkin@


# 1.260 01-Jul-2014 kettenis

Adding an additonal delay after each access to SystemMemory or SystemIO
during autoconf makes no sense.

ok deraadt@


# 1.259 23-Jun-2014 kettenis

SystemMemory is used to access memory mapped registers on some machines, so we
must use the bus_space(9) API to access it instead of memcpy(9). Also make
sure we properly align access to fields that need more than byte alignment.

ok guenther@


# 1.258 21-May-2014 mlarkin

Fix invalid / missing format specifiers in several dnprintf statements
(used only when ACPI_DEBUG is enabled)

From a diff on tech@ by Fabian Raetz, with a minor change by me, tested
on i386 and amd64 with ACPI_DEBUG enabled and disabled.


# 1.257 25-Apr-2014 mlarkin

Defer the free of the hibernate structures until later, when it is safe to
do so.

Freeing the hibernate structures too early leads to inadvertent calls into
splx when we aren't ready for them yet. Found when trapping splx calls in
the resume path.

discussed with deraadt@ at m2k14


# 1.256 13-Apr-2014 mlarkin

Fix a badly worded debug message that implied the entire machine was going
to sleep, instead of just the acpi thread going to sleep.


# 1.255 31-Mar-2014 mpi

Change the order of operations during a suspend/resume cycle and
call bufq_quiesce() after executing the DVACT_QUIESCE handlers.

This should be safe since no disk nor controller drivers have such
handler but it will allow us to detach sd(4) devices attached to a
USB bus.

Another benefit pointed out by kettenis@ is that drivers that need
to read a firmware from the disk should be able to do it at resume
time in a DVACT_WAKEUP handler.

ok kettenis@, deraadt@


# 1.254 13-Mar-2014 dlg

get rid of the assumption that the head of the alldevs list is the
"mainbus" device. this breaks when mpath is enabled because it
attaches before mainbus and therefore takes the head position.

have autoconf provide device_mainbus() which looks up mainbus_cd,
and use that instead.

discussed with deraadt who just wants mpath stuff to move forward
despite there being many ways to shine this particular turd.


Revision tags: OPENBSD_5_5_BASE
# 1.253 21-Feb-2014 deraadt

Do the lid control further on in the resume sequence, since this is
AML, and will do WAITOK operations. While cold, that is a bad idea.
We seem to be safe with respect to the spec, I do not see any event
debounce issues here.
ok kettenis, tested by many


# 1.252 20-Jan-2014 claudio

Instead of checking sc->sc_revision <= 1 check that the
fadt->hdr_revision <= 1. This is a better check since it checks
the version of the proper descriptor. This allows the alix APU
to reboot since those boards have currently a wrong sc->sc_revision.
Committing this now so that it can be tested easier.
With and OK jsg@ also fine by derradt@


# 1.251 24-Dec-2013 kettenis

If the FADT has its SMI_CMD set to zero, assume we're only ACPI-only hardware
and don't need to disable SMI ownership of the ACPI hardware registers.

ok mlarkin@


# 1.250 23-Dec-2013 kettenis

Revert previous commit. It makes resume hang on some MP systems running
GENERIC.MP.


# 1.249 16-Dec-2013 kettenis

Restore hw.setperf upon resume like we do for apm(4) on i386. Seems we need
to do this fairly late in the resume to avoid hangs.

ok deraadt@, mlarkin@


# 1.248 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


# 1.247 06-Nov-2013 mpi

Add support for Power Resources for Dx states and the necessary hook
for PCI devices. This hook should be called twice, before and after
changing the power state of a PCI device.

Before setting the device to the new state, the ACPI layer will notify
every power resources linked to the device for that state and make sure
they are turned "_ON". After changing the state of the device, it will
decrement the reference of every power resources linked to that device
for the old state and turn them "_OFF" if they are no longer referenced.

This fixes the no-USB after resume problem seen on various ThinkPad,
problem initialy diagnosed with Alexander Polakov.

ok kettenis@, deraadt@


Revision tags: OPENBSD_5_4_BASE
# 1.246 01-Jun-2013 mlarkin

Stop printing ACPI wakeup devices in dmesg after the 16th wakeup device.

Helps with VMware VMs that report hundreds of wakeup devices.

ok reyk@, deraadt@


# 1.245 31-May-2013 bluhm

Fix the build for a kernel without wd and pciide in its config.
OK deraadt@ tedu@


# 1.244 30-May-2013 deraadt

Enforce ca_activate tree-walks over the entire heirarchy for all events,
cleaning up some shutdown-hook related code on the way.
(A few drivers related to sparc are still skipped at kettenis' request)
ok kettenis mlarkin, tested by many others too


# 1.243 18-Apr-2013 deraadt

pin the acpi thread to the BSP, because AML and SMI cannot be trusted. I
was certain we were doing this already, but it turns out no...


Revision tags: OPENBSD_5_3_BASE
# 1.242 24-Dec-2012 guenther

In acpiioctl(), Return though the splx() path if suser() returns an error

ok deraadt@


# 1.241 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


# 1.240 04-Oct-2012 ehrhardt

Use information provided by ACPI to attach secondary PCI host bridges.

ok kettenis@


# 1.239 07-Sep-2012 kettenis

Add a new function called acpi_pci_min_powerstate() that returns the lowest
power state for a PCI device for the power state (currently S3 or S4) we're
transitioning into. This function is probably too simplistic; there is a lot
of (silly) complexity in the ACPI spec.

ok deraadt@


Revision tags: OPENBSD_5_2_BASE
# 1.238 13-Jul-2012 pirofti

Disable acpivideo(4) on ASUS laptops.

There are some models that panic whenever an event is received.
Some cope, some don't. In order to get this right acpiec(4) requires
major rework. Until then let machines boot.

Discussed with deraadt@.


# 1.237 11-Jul-2012 mlarkin

for now, restrict hibernate to root in the acpi ioctl kernel interface.
normal rules for controlling this interface through apmd still apply.

ok deraadt@


# 1.236 09-Jul-2012 deraadt

tiny little oops in untested hibernate block


# 1.235 09-Jul-2012 deraadt

clean up ioctl() case statements


# 1.234 09-Jul-2012 deraadt

acpi_disable_onegpe() is not used; ok pirofti


# 1.233 24-May-2012 kettenis

If _MIN > _MAX consider a WordBus resource to be invalid. Fixes a panic on
the IBM x3550-M4.

ok mikeb@


# 1.232 29-Mar-2012 mlarkin

Missing #ifdef in previous diff


# 1.231 29-Mar-2012 mlarkin

Prevent hibernation attempts on machines with unsupported disk controllers.
The same check is also performed later in the hibernate sequence, but by
then it's too late to cleanly unwind (presently).

ok deraadt


# 1.230 26-Mar-2012 deraadt

simplify conditionals in acpi_sleep_state()
ok mlarkin


# 1.229 26-Mar-2012 deraadt

hook in the hibernate request code; half of this diff is from mlarkin
ok mlarkin


Revision tags: OPENBSD_5_1_BASE
# 1.228 20-Sep-2011 deraadt

Very early in suspend while the clock is still ticking realtime,
save the clock back to the rtc. The zaurus already did this.
ok phessler miod kettenis


Revision tags: OPENBSD_5_0_BASE
# 1.227 02-Jul-2011 nicm

kqueue attach functions should return an errno or 0, not a plain 1. Fix
the obvious cases to return EINVAL and ENXIO.

ok tedu deraadt


# 1.226 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.225 16-Jun-2011 pirofti

Import acpitoshiba driver.

This is originally written by Hiroyuki Aizu, ported and integrated
in our tree by Javier Vazquez <javama4 AT gmail> with tweaks and
suggestions by me.

Import prodded by deraadt@ and the lack of testers on tech@.


# 1.224 27-Apr-2011 jcs

attach acpithinkpad to newer lenovo models like the x120e

ok deraadt@


# 1.223 22-Apr-2011 jordan

Fix uninitialzied variables and formatting strings (-Wxxx errors)


Revision tags: OPENBSD_4_9_BASE
# 1.222 02-Jan-2011 jordan

Re-add code for ACPI taskqueue, eliminates need for state vars in acpi softc
Serialize processing ACPI tasks for sleep/powerdown/GPE event
ok deraadt@


# 1.221 31-Oct-2010 guenther

Revert last commit: it breaks resume on Thinkpads
"then please back it out" deraadt@


# 1.220 26-Oct-2010 jordan

Add task queue for ACPI gpe and notify handlers
This fixes eject on Dell Latitude dock and an issue on the Dell Mini
battery update.
ok deraadt


# 1.219 07-Oct-2010 jordan

Create separate functions for enabling wake and runtime gpes.
Remove old suspend/resume gpewalk function
ok deraadt, mlarkin


# 1.218 05-Oct-2010 jordan

split acpi_enable_onegpe to separate enable/disable functions
ok deraadt


# 1.217 05-Oct-2010 jordan

Added acpi_disable_allgpes function to clear GPE status
ok deraadt


# 1.216 05-Oct-2010 deraadt

clean up acpi_softc use more


# 1.215 05-Oct-2010 deraadt

delete a bit of #if 0 code


# 1.214 05-Oct-2010 deraadt

hide use of our global acpi_softc pointer a bit better


# 1.213 29-Sep-2010 deraadt

grab the acpi thread lock very early in acpi thread startup; not that it
is likely that anyone will race us... but why be careless.
ok kettenis


# 1.212 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.211 29-Aug-2010 jasper

- add a check for FWRITE here too.

ok miod@


# 1.210 27-Aug-2010 kettenis

Make PCI Power Management optional, and only enable it when acpi(4) attaches.

ok deraadt@


Revision tags: OPENBSD_4_8_BASE
# 1.209 08-Aug-2010 kettenis

Close race between the acpi thread and wscons ioctls running in process
context. We might rip this out post-release and replace it with a solution
that serializes things in a more obvious way.

ok deraadt@, miod@ and "those crazy texans who ok anything"


# 1.208 08-Aug-2010 deraadt

Report the acpi sleep states when on a RAMDISK; ok krw


# 1.207 07-Aug-2010 kettenis

Enforce acpi register access rescrictions.

ok mlarkin@, deraadt@, tested by marco@


# 1.206 07-Aug-2010 deraadt

upon resume, notify all the acpiac and acpibat drivers to update
their status. some laptops do not do this automatically.
ok kettenis canacar


# 1.205 06-Aug-2010 marco

Always call _PSW on _LID devices that support it to enable lid open to
resume the machine. Conversely disable it when the machine wakes up.

Tested by several ok deraadt


# 1.204 06-Aug-2010 deraadt

As part of a revamp of the PM1/GPE code, I write this diff to look for
unmanaged EN & STS bits in the PM1 register at interrupt time and report
them. As a side effect this splits the STS acknowledgement into two writes
(for power, and sleep) instead of one. The printf that is added (to spot
unmanaged STS bits) has not yet been triggered as far as we know.

Before the "write to PM1 registers at the right offset" diff went in, this
was not neccessary. But newer thinkpads do not have a working soft power
button without this diff.

We have no idea why.
ok mlarkin kettenis


# 1.203 05-Aug-2010 deraadt

PM1 power button events were generating 'fake events' to acpibtn PWRB,
but if there is no such device these were being tossed. Instead, arrange
for all power button handlers to flag a softc variable which is processed
by the thread; so that one place will know to request a powerdown (psignal
to init)
ok marco mlarkin


# 1.202 05-Aug-2010 deraadt

rename a variable which is annoyingly poorly named


# 1.201 05-Aug-2010 mlarkin

Write to the correct offset for various ACPI pmreg registers. Writing to
the correct register does properly clear the wake status bits and fixed
event bits, which allows some machines to have more than 1 suspend cycle.

bug spotted by deraadt@

tested by deraadt@ and myself on a half-dozen machines and others on
hackers@

ok deraadt@, marco@


# 1.200 05-Aug-2010 deraadt

Fix the power button. The pm1 register is 16 bits in size.
My mistake, a while back.
tested by krw


# 1.199 04-Aug-2010 kettenis

Print supported sleep states.

ok marco@, phessler@, deraadt@


# 1.198 03-Aug-2010 mlarkin

Use the proper flag for re-enabling certain hardware events (power btn,
sleep btn). The (incorrect) flag prevoiously used coincidentally had the
same value, so this shouldn't cause any different behavior than before.

tested on a variety of machines (i386, amd64, sp, mp) - no behavioral
change seen

ok deraadt@


# 1.197 28-Jul-2010 marco

Make all gasio failures look and behave the same.


# 1.196 28-Jul-2010 mlarkin

Delay a little longer on suspend before giving up. Fixes machines that
sometimes don't suspend because the sleep transition takes too long
(symptoms like failing to sleep but the backlight stays on).

ok deraadt@


# 1.195 27-Jul-2010 deraadt

remove XXX; EC can now be attached early when needed; ok jordan


# 1.194 27-Jul-2010 deraadt

more brackets feels good


# 1.193 27-Jul-2010 deraadt

XXX annotate a block in acpi_enter_sleep_state() that is pretty worrying


# 1.192 27-Jul-2010 deraadt

horrific indentation that nearly made me puke


# 1.191 27-Jul-2010 mlarkin

Set the sleeping indicator light on machines that support it, in case SMI
doesn't do it for us. Shows the 'moon' LED on older Thinkpads when
sleeping.

Tested on T43p, W500, T510 by me and on X61s by deraadt@.

ok deraadt@


# 1.190 27-Jul-2010 jordan

Early initialization of acpiec if ECDT table exists
Fixes hang when booting thinkpads while docked
ok deraadt


# 1.189 26-Jul-2010 deraadt

remove merge error which was commited


# 1.188 26-Jul-2010 pirofti

Add support for Sony ACPI hotkeys via a new driver: acpisony(4).

Currently it only works for the suspend button and tries to do right for
the brightness events, but I haven't found a Sony laptop that like to do
right so far.

In the future I want to make the brightness keys work on all Sony's and
also add support for the zoom hotkeys and whatever other funky keys I can
find on those things.

Okay deraadt@.


# 1.187 22-Jul-2010 deraadt

pretty large cleanup of SMALL_KERNEL


# 1.186 21-Jul-2010 deraadt

in the acpi GPE handler, late ack level interrupts not edge interrupts.
Duh.


# 1.185 21-Jul-2010 deraadt

Merge the edge and level gpe handlers into one (now possible since the
gpe structure tells us whether the gpe is edge or level)


# 1.184 21-Jul-2010 deraadt

delete #if 0 code we are not going to use; ok marco


# 1.183 20-Jul-2010 deraadt

make the mask code for acpi_enable_onegpe() match other similar code


# 1.182 20-Jul-2010 deraadt

Handle the acpi interrupt controller with proper edge/level handling,
wrapped in spltty to avoid racing against the interrupt controller.
Repair the gpe bit masking code while there, and do operations in the
right order
ok kettenis mlarkin, help from jordan at figuring out the order of
operations.


# 1.181 20-Jul-2010 matthew

When we're preparing to go to sleep, only call bufq_restart() if other
preparations failed.

ok kettenis@, thib@, deraadt@


# 1.180 19-Jul-2010 deraadt

permit compilation with ACPI_DEBUG


# 1.179 19-Jul-2010 deraadt

there is no need for a seperate dsdt_softc pointer; dsdt does not have a
softc but is just a part of the single acpi driver.
ok jordan


# 1.178 19-Jul-2010 deraadt

acpi_delay() is no longer needed; ok jordan


# 1.177 18-Jul-2010 mlarkin

Clear event status on resume, to avoid going back to sleep/reboot/shutdown
immediately after resume. Tested by a bunch of people on hackers@ on
various hardware. Fixes at least Toshiba NB200 and Gateway LT3103u.

ok marco


# 1.176 13-Jul-2010 deraadt

rename some thread related interfaces so they are logical


# 1.175 10-Jul-2010 jordan

Verify _ADR bdf is correct (some systems return 0xffff)
Works on Dell x9xx and Studio 1555


# 1.174 09-Jul-2010 jordan

Clear the noise


# 1.173 08-Jul-2010 jordan

Add mapping for ACPI device to PCI bdf (match autoconf tree)
Simplify resource parsing function to use buffer argument
Convert namespace linked lists to use queue macros
ok marco@, deraadt@


# 1.172 06-Jul-2010 deraadt

Use spltty() to lock downcalls from apm against the information being
modified by the acpi thread.
ok kettenis


# 1.171 06-Jul-2010 deraadt

Don't use a workq to do the suspend, because it races aginst the acpi
thread. Instead, just tell the acpi thread to do the suspend for us.
This makes apmd & zzz work correctly. While here, have acpithinkpad
attempt to post the event to apm before prompting the suspend itself.
ok kettenis marco mlarkin


# 1.170 05-Jul-2010 mlarkin

Reset register SCI_EN on resume. The ACPI spec clearly states we don't have
to do this, but many machines don't, or forget to, which leads to all sorts
of problems on resume (broken battery stats, overheating, failure to resume
more than once, etc).

tested by many, ok marco@, deraadt@


# 1.169 01-Jul-2010 thib

do bufq quiesce again, since vnd/the bufq_done call in
biodone has been fixed.


# 1.168 01-Jul-2010 jordan

Backout recent AML changes, breaks on R210 and others


# 1.167 01-Jul-2010 jordan

Simplified aml_parse_resource call, cleanup code
ok marco


# 1.166 01-Jul-2010 jordan

Fix subordinate bus # for multi-root PCI buses
ok marco, kettenis


# 1.165 30-Jun-2010 thib

Disable/partially backout the bufq quiesce changes as this
is causing havoc with vnds and release must be buildable.


# 1.164 29-Jun-2010 deraadt

Fix ramdisks (new code was placed inside a SMALL_KERNEL chunk)


# 1.163 29-Jun-2010 jordan

Add support for mapping ACPI to PCI devices
ok kettenis, deraadt


# 1.162 29-Jun-2010 kettenis

Call bufq_quiesce() on suspend to stop all I/O and bufq_restart on resume.
Makes suspend/resume work much better while doing disk I/O.

ok mlarkin@


# 1.161 29-Jun-2010 tedu

60 is a more reasonable guess for number of minutes in an hour than 100.
spotted by TeXitoi. ok deraadt


# 1.160 27-Jun-2010 jordan

Add _acpi_matchhids routine, precursor to pci device match


# 1.159 27-Jun-2010 mlarkin

Clear ACPI fixed event status on resume (power buttons/etc) so that some
machines don't immediately resume after suspending to S3.

Tested by kettenis@ and I on 5 or 6 machines, deraadt@ "yeah, looks good"


# 1.158 27-Jun-2010 jordan

Cleaned up acpi_maptable, adds to table list directly


# 1.157 07-Apr-2010 deraadt

If we have done a wsdisplay_suspend(), and ACPI decides it does not want to
suspend, we must roll back by calling wsdisplay_resume() or the virtual
console switching will be locked forever
ok oga


# 1.156 07-Apr-2010 kettenis

Make suspend/resume work on MP machines (running an MP kernel). Joint work
from mlarkin@ and me, with some amd64 fixes thrown in by deraadt@

ok marco@, deraadt@, pirofti@, mlarkin@


# 1.155 31-Mar-2010 kettenis

Bring /dev/apm support in line with apm(4). Make the suspend button send
a suspend request event to apmd(8) instead of suspending immediately. Also
keep track of whether /dev/apm and /dev/apmctl are currently open, such that
we can still suspend immediately if apmd(8) isn't running.

ok deraadt@, marco@, pirofti@, jsing@, oga@


# 1.154 30-Mar-2010 oga

Prevent the apmd/x races for good.

When we hit suspend time, go through all wsdisplays on the system. if
they are in mode MAPPED, but not MODE_DUMBFB then if possible do a full
vt switch to a !mapped vt, and prevent switching back until resume time.
This has to be called from MD code because this involves userland
running so that X can run the vt switch signal handler. This way, any
case where we are using the "poke registers from userland" model, we
will not be on the hardware when we go down, so the kernel can actually
handle thing properly.

Tested on several acpi laptops (by kettenis@ and ian@), x40 (me and
beck@ at LEAST) and zaurus (me). Maybe others, but if so I forgot who at
this time..

Idea from deraadt somewhere over the Faroe Islands (I thought of a
similar thing myself a while ago). Much prompting from him. Ok and
comments miod@


# 1.153 25-Mar-2010 oga

Fix & vs. && in the kqfilter code.

Based on a comparison with the apm code.

ok deraadt@, kettenis@


Revision tags: OPENBSD_4_7_BASE
# 1.152 26-Nov-2009 mlarkin

Visual (and on some machines, audio) feedback while the machine is
resuming via calls to aml _SST method. This also makes the "moon" LED on
thinkpads to blink during resume and cease when resume is completed.

ok pirofti@


# 1.151 26-Nov-2009 deraadt

Toggle into "cold" when we we disable interrupts, because tsleeping
in the AML interpreter and context switching into userland processes
tends to sometimes pop the ACPI trampoline page out of our address
space. Makes lots more machines work.
ok kettenis mlarkin


# 1.150 24-Nov-2009 jsg

in the resume path evaluate _WAK before dealing with device drivers
like the spec says, not after.

ok mlarkin@


# 1.149 24-Nov-2009 deraadt

abort suspends on SMP systems until that gets written; ok mlarkin


# 1.148 23-Nov-2009 mlarkin

pci should return the result of it's children's suspend/resume calls back
to its parent.

handle suspend failure case and unwind if devices have an activate function
that returns failure.

ok deraadt@, kettenis@


# 1.147 23-Nov-2009 mlarkin

acpi* devices need to call their children on suspend/resume.
ok deraadt@


# 1.146 23-Nov-2009 pirofti

Remove ACPI_SLEEP_ENABLED checks.

This enables by default the suspend/resume paths in the kernel.

Okay deraadt@.


# 1.145 23-Nov-2009 deraadt

knf and spacing that are super annoying


# 1.144 23-Nov-2009 mlarkin

Hook up Theo's new children suspend/resume function, as well as more ACPI
suspend/resume glue.

ok deraadt@


# 1.143 26-Oct-2009 deraadt

Do not do strncmp comparisons on dv_xname because longer device names which
look similar could arrive in the future. Instead, compare directly against
dv_cfdata->cf_driver->cd_name
Issue originally spotted by miod


# 1.142 26-Oct-2009 jordan

Verify checksum+address when loading ACPI tables.
Some systems had invalid entries in RSDT/XSDT.
ok marco@


# 1.141 23-Jul-2009 cnst

New aibs(4) driver for ASUSTeK AI Booster (ACPI ATK0110) hardware monitoring,
with sensor state support through limits provided by the ACPI.

Tested on several ASUS motherboards kindly networked by Sam Fourman Jr.

ok deraadt marco jordan


Revision tags: OPENBSD_4_6_BASE
# 1.140 03-Jun-2009 pirofti

Import acpi power resource driver. This is needed for suspend/resume logic.

Okay jordan@ and marco@.


# 1.139 03-Jun-2009 pirofti

Add acpivideo support. This adds brightness support for all laptops
except thinkpads, they will use the acpithinkpad driver. The driver is
also hooked into wsconsole. So brightness can be adjusted via:

$ wsconsctl display.brightness=<percentage>

This is very helpfull on some laptops that have a nasty bios and get two
steps instead of one when pressing the brightness button.

Tested on various dell, fujitsu, acer, samsung and other laptops.

Okay marco@, miod@. Suggestions from kettenis@.
Lots of reviews and help from miod@, thanks!


# 1.138 03-Jun-2009 jordan

Added ejectable bay for IDE cdroms
ok marco@


# 1.137 30-Apr-2009 marco

Oga changed apm ioctls and forgot to grep. Temporary work around by hitting
the sleep button when we get any request. This interface will chnage at
some point.
ok oga


# 1.136 19-Apr-2009 krw

Call acpi_sleep_walk() when powering down just like when entering
any other sleep state. This tells acpi wake devices to not wake up
the box until manual powering up resets their status. Makes my Dell
GX520 stay powered off in response to 'halt -p'.

In snaps for a while.

ok marco@ (several times) deraadt@


# 1.135 17-Apr-2009 pirofti

Add device ranking support for future sleep/wake developments.

okay marco@


# 1.134 11-Mar-2009 jordan

Cleanup.. use aml_evalinteger instead of aml_evalname
ok marco@


# 1.133 10-Mar-2009 jordan

Fix cut/paste error.. call _BFS not _PTS
ok marco@


Revision tags: OPENBSD_4_5_BASE
# 1.132 21-Feb-2009 marco

overzealous cut/paste; found by Vladimir Kirillov <proger@uaoug.org.ua>


# 1.131 20-Feb-2009 marco

Oops didn't mean for Debugger() to remain there.


# 1.130 19-Feb-2009 marco

suspend/resume bits so that we can develop this in tree. This is disabled.
code from mlarkin and me
help from art,toby,jordan and several others
ok jordan, go for it deraadt


# 1.129 10-Feb-2009 jordan

Fix null pointer for shutdown if acpi not enabled
ok marco@


# 1.128 04-Feb-2009 kettenis

Some systems that claim to be ACPI 2.0 apparently don't provide an XSDT
address. Make the code fall back on the RSDT on those systems.

From Joerg Niendorf <p3y80@internode.on.net>. Fixes PR 5986.


# 1.127 17-Jan-2009 krw

A couple of stray malloc/memset -> malloc(M_ZERO), a bit of knf and
lose a couple of unnecessary casts. No functional change.

Feedback from kettenis@

ok kettenis@ marco@


# 1.126 06-Nov-2008 marco

Collapse all sanity checks of hids into one function.

Originaly from jordan


Revision tags: OPENBSD_4_4_BASE
# 1.125 02-Jul-2008 fgsch

add acpivideo. for now almost an empty skel so others can work on
suspend/resume. eventually it will also manage output switching and
brightness where supported. prodded by marco@


# 1.124 11-Jun-2008 marco

Fix $OpenBSD$ for once and for all


# 1.123 08-Jun-2008 deraadt

aaa_dev was being set from an uninitialized variable; ok toby


# 1.122 07-Jun-2008 marco

Kill ACPIVERBOSE. It was never used and is worthless to boot.

comments jmc
ok toby


# 1.121 06-Jun-2008 marco

Shrink acpi some more for floppies and while here re-enable acpi on the
floppies. Fixes crazy sony vaio kettenis has.

tested by deraadt
ok deraadt jordan kettenis


# 1.120 01-Jun-2008 marco

Fix \n nit in SMALL_KERNEL.


# 1.119 01-Jun-2008 marco

Re-enable new parser. Lots of testing by various folks. Fits all media
now too.

ok kettenis deraadt


# 1.118 16-May-2008 dlg

back out jordans new parser.

it didnt have enough oks from the right people, it did break machines
that were previously working, and it bloated the RAMDISKS and install
media beyond the constraints of the media.

requested by deraadt@


# 1.117 14-May-2008 jordan

Adding new ACPI Parser code
Old guts of code still exists, needs to be torn out next
ok marco@
ok brad@


# 1.116 27-Apr-2008 jcs

add acpithinkpad(4), an acpi support driver for ibm/lenovo
thinkpads.

provides functionality that the bios did on older, non-acpi
thinkpads like respond to screen brightness keys and toggle the
bluetooth device on and off.

ok deraadt@ and marco@


# 1.115 24-Apr-2008 jsing

Introduce acpiasus(4), a driver for the ACPI based hotkeys found in many
ASUS laptops (including the ASUS EeePC) - largely based on NetBSD's
asus(4) driver. On the ASUS EeePC this allows us to enable/disable
wireless, change screen brightness and use the volume keys.

ok jsg@, weingart@


# 1.114 12-Apr-2008 kettenis

Get rid of acpi_s5 global variables; simply send SIGUSR2 instead of SUGUSR1
to tell init(8) to power down the machine.

ok krw@


Revision tags: OPENBSD_4_3_BASE
# 1.113 21-Jan-2008 jsg

Check against RESET_REG_SUP in FADT before doing the acpi reset dance
after all, not doing so appears to break more machines than it fixes.

ok marco@


# 1.112 05-Jan-2008 kettenis

Never attach more acpicpu(4) devices than cpu(4) devices. Always attach
the first acpicpu(4) device on non-MULTIPROCESSOR kernels, regardless of its
ACPI processor ID.

ok mikeb@


# 1.111 05-Dec-2007 deraadt

Hang acpi(4) below bios(4) instead of mainbus(4). This lets us move acpi(4)
before pcibios(4), and then.... if acpi(4) attaches, skip pcibios(4) since
it messes the machines up. Fixes claudio's HP dl320 G5, and almost assuredly
others.
ok kettenis


# 1.110 05-Dec-2007 deraadt

repair acpiopen/acpiclose and friends to handle the /dev/apmctl interface,
so that apmd(8) can actually work correctly against acpi(4), and thus,
even do the fancy -C/-A stuff; also tested by jmc


# 1.109 02-Dec-2007 fgsch

fix the trailing space for real now.


# 1.108 02-Dec-2007 fgsch

avoid trailing spaces on output; marco@ ok.


# 1.107 27-Nov-2007 kettenis

Fix off-by-one that made us attach acpicpu0 and acpicpu1 on GENERIC kernels.

ok marco@


# 1.106 25-Nov-2007 jsg

When available and ACPI is enabled use the ACPI reset method
to reboot. This is required for some quirky sis machines
and likely others to reboot.

Tested by many. 'commit early commit often' deraadt@


# 1.105 19-Nov-2007 deraadt

sigh, amd64 still uses that stupid acpi_enabled variable...


# 1.104 19-Nov-2007 deraadt

softc's come initialized to 0, you know...


# 1.103 19-Nov-2007 deraadt

store revision; from jsg


# 1.102 19-Nov-2007 kettenis

Initialize sc_facs earlier.

ok deraadt@, beck@, canacar@


# 1.101 16-Nov-2007 deraadt

fix the bus_space #define nightmare, so that amd64 and i386 are much more
uniform. as a result shared code like acpi needs less #ifdef's
ok marco kettenis


# 1.100 15-Nov-2007 deraadt

remove the #ifdef ACPI_ENABLE. the EC codepath is either safe enough (and
will be improved), or it is unsafe because AML assumes it must be there
ok almost everyone


# 1.99 15-Nov-2007 mikeb

Make acpicpu(4) attach to the existing CPUs only.

Problem reported by form@. Fix idea by kettenis@.
Lots of help from deraadt@. Tested by deraadt@, form@ and me.

ok deraadt marco


# 1.98 14-Nov-2007 deraadt

spacing


# 1.97 12-Nov-2007 deraadt

correct : vs , in attach-time messages


# 1.96 06-Nov-2007 deraadt

all ACPI_ENABLE kernels need acpi_interrupt()


# 1.95 06-Nov-2007 deraadt

move acpi_read_pmreg() and acpi_write_pmreg() out of SMALL_KERNEL so
that acpi ramdisks can compile (obvious solution)


# 1.94 05-Nov-2007 canacar

Establish the acpi interrupt earlier. Fixes problems with some
GENERIC.MP kernels not getting acpi interrupts. This is a
workaround until the real issue with late establishment is fixed.
ok weingart@, beck@, gwk@


# 1.93 03-Nov-2007 jordan

Added support for displaying wakeup devices
ok beck@,weingart@,gwk@


# 1.92 11-Oct-2007 kettenis

When walking the tree yo find _PRT methods, check whether the device is
actually enabled and functioning. Similar to what we do for _INI.

ok weingart@, beck@


# 1.91 08-Oct-2007 krw

More simple memset(,0,) -> M_ZERO changes. In this batch move to
size(*p) as the first malloc() parameter where p is declared locally
and thus easy to check. Add M_ZERO to gpe_table allocation in acpi.c
even though there is no obvious bzero or memset nearby.


# 1.90 13-Sep-2007 mk

Remove unnecessary memset() (see aml_evalnode(9)) and a bit of KNF.

ok toby


# 1.89 13-Sep-2007 weingart

Implement ACPI 6.5.1 spec tree walk for _STA and _INI.
Fixes mk's laptop. No regressions so far (thank you to the
testers).

ok gwk@, mk@, marco@


Revision tags: OPENBSD_4_2_BASE
# 1.88 17-Apr-2007 mk

Now that acpidock no longer hangs machines during boot (at least
according to the reports I've been getting) it should be safe to enable
it again, so remove the #if 0 stuff around it.

ok marco


# 1.87 11-Apr-2007 jordan

Added changes for C-State
ok marco@


# 1.86 26-Mar-2007 marco

Calculate minutes left more accurately.

From giovanni <qgiovanni@gmail.com>


Revision tags: OPENBSD_4_1_BASE
# 1.85 03-Mar-2007 mk

Stop looking for acpidock until we fix that stuff that breaks all pre-60
series ThinkPads with acpidock(4) enabled.


# 1.84 22-Feb-2007 marco

We were getting no interrupts in .MP. There are 2 fixes; one in
acpi_machdep.c and move the interrupt establishment down after ioapic
has been initialized.

ok jordan


# 1.83 21-Feb-2007 kettenis

Make an acpi-enabled GENERIC.MP work on systems that only support PIC mode.

ok marco@


# 1.82 19-Feb-2007 jordan

Added acpi_map_address function for mapping GAS/IO registers
ok marco@


# 1.81 18-Feb-2007 jordan

Added dynamic perfect hash for opcodes
ok marco@


# 1.80 18-Feb-2007 marco

Shave off another K pre compression.
Shuffle SMALL_KERNEL functions around so that we only have a single #ifndef


# 1.79 17-Feb-2007 marco

Add SMALL_KERNEL throught acpi to make it fit on boot media.
ok deraadt


# 1.78 13-Feb-2007 marco

Add smarter _INI code. This helps with some dissapearing devices.

ok kettenis jordan


# 1.77 31-Jan-2007 gwk

Introduce acpi_hasprocfvs thats "ACPI Has Processor Frequency and Voltage
Scalling" a global flag set by acicpu attach to indicate that ACPI is
handling fvs through the _PSS objects. This will fix disappearing
EST/powernow when booting kernels with acpi enabled, as acpicpu isnt
compiled into GENERIC yet. Also in cases where acpicpu is built in and
legacy EST/powernow work but acpi is missing the _PSS object, the legacy
routines will be used.

tested by dim@, and Nick Nauwelaerts <nick AT nauwelaerts.net>
ok marco@, dim@


# 1.76 22-Jan-2007 mk

Actually look for docking stations by looking for _DCK nodes.

ok marco


# 1.75 02-Jan-2007 marco

On some systems EC must be attached as the first device. Move EC from
generic _HID devices to its own routine to force first attach. Bug found
and debugged by joris@

Code mostly written by joris@


# 1.74 26-Dec-2006 marco

Add polling to devices that require it such as acpibat and acpitz.
Use the same tsleep/wakeup mechanism as the interrupt code to keep it non-
concurrent.


# 1.73 21-Dec-2006 deraadt

even more knf love


# 1.72 21-Dec-2006 marco

Remove some leftover acpiec gpe/interrupt code.


# 1.71 20-Dec-2006 deraadt

oops, name already used


# 1.70 20-Dec-2006 deraadt

rename __acpi_enable_gpe(); ok marco


# 1.69 20-Dec-2006 deraadt

acpi_unmap_pmregs() is not used, ok marco


# 1.68 19-Dec-2006 deraadt

simplify some low-level methods substantially (shrinking code); ok marco jordan


# 1.67 06-Dec-2006 mk

malloc(..., M_NOWAIT) with no NULL check is bad. If allocation fails,
complain, clean up, and bail out of attach.

input and ok gwk


# 1.66 28-Nov-2006 jordan

Fixed clearing arg for acpi_init_pic
ok kettenis@


# 1.65 27-Nov-2006 kettenis

Don't spam dmesg.


# 1.64 27-Nov-2006 jordan

Fixes for acpiprt routing
Post-parse fixup for namerefs
ok kettenis@


# 1.63 25-Nov-2006 marco

Don't handle acpiev events inside the gpe handler since this causes deadlocks.

ok jordan@


# 1.62 15-Nov-2006 kettenis

Hook up acpi apic interrupt routing support to generic acpi code.

ok jordan@


# 1.61 02-Nov-2006 marco

Kill acpiec attach function and add its functionality to the generic _HID
device attach

prodded and ok jordan


# 1.60 02-Nov-2006 marco

Fix comments to reflect reality.


# 1.59 19-Oct-2006 marco

Replace ACPI ioctls with APM ioctls. This makes ACPI talk the same protocol
as APM therefore userspace can now retrieve power information in the same
fashion as all other apm arches (zaurus, macppc, i386 etc).

To use force the use of acpi device like so: apmd -f /dev/acpi

ok jordan@


# 1.58 19-Oct-2006 jordan

Fixed GPE debug levels
Fixed ref counter on temporary bufferfields
DSDT parsing fully working on IBM Thinkpad
ok marco@


# 1.57 15-Oct-2006 jordan

Removed old GPE handler code remnants
ok marco@


# 1.56 12-Oct-2006 jordan

Massive ACPI changes
Fixed ACPI leaks
new ACPI API
ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.55 30-Jun-2006 gwk

KNF, No functional change,
"go at it" marco@


# 1.54 30-Jun-2006 jordan

Support for calling Embedded Controller via new GPE interface
ok marco@


# 1.53 30-Jun-2006 jordan

Added new code for GPE handling
Also works with ACPIEC; need to fixup acpiec.c to remove original interrupt handler
ok marco@


# 1.52 30-Jun-2006 jordan

Added offset argument for acpi read/write pmreg routines (new GPE code)
ok marco@


# 1.51 31-May-2006 todd

do not call acpi_powerdown() unless acpi has successfully initialized
ok gwk@ canacar@


# 1.50 29-May-2006 canacar

Add embedded controller support.
ok marco@ deraadt@ testing & ok gwk@


# 1.49 19-May-2006 canacar

Minimal thermal zone driver. Monitors thermal zone temperature,
shuts down the system if the 'critical temperature' is reached.
ok marco@


# 1.48 11-Apr-2006 marco

KNF.


# 1.47 11-Apr-2006 gwk

Add acpi_resume(struct acpi_softc *sc), which calls some AML methods,
detected at attach time, currently unused.

ok marco@


# 1.46 11-Apr-2006 gwk

Discover some ACPI sleep related methods, and call them at the
appropriate time in acpi_enter_sleep_state().

ok marco@


Revision tags: OPENBSD_3_9_BASE
# 1.45 26-Feb-2006 marco

Add acpicpu as a device.

ok jordan@


# 1.44 23-Feb-2006 marco

remove dead function.

ok jordan@


# 1.43 22-Feb-2006 marco

Print out all tables it found during autoconf. We'll need this data in
the future.


# 1.42 22-Feb-2006 marco

Make dmesg less verbose.

ok jordan.


# 1.41 22-Feb-2006 jordan

Added parsing of SSDT tables
ok marco@


# 1.40 22-Feb-2006 jordan

Added new aml_register to notify by device id
Moved powerdown code to button device
ok marco@


# 1.39 21-Feb-2006 marco

Readjust debug levels that crept in too low over the last few days.
KNF


# 1.38 21-Feb-2006 marco

Rig the power button so that the machine shuts down correctly.


# 1.37 20-Feb-2006 marco

Add acpibtn device. Remains disabled for now.


# 1.36 19-Feb-2006 jordan

Added code for aml_notify GPE notification callback
ok marco@


# 1.35 19-Feb-2006 grange

Fix acpi_enter_sleep_state() by replacing current ugly hack
with mostly correct sleeping state transition code.

ok brad@ marco@


# 1.34 19-Feb-2006 marco

jordan:
Enable interrupts.
Added peripheral code to make this work.

marco:
Replace softintr and other types of interrupts with a handler with context.
Fixed interrupt magic for GPEs.
Some KNF

This is work done by jordan and I but has to be a single commit.


# 1.33 17-Feb-2006 marco

Remove earlier introduced delays. We really only need them during autconf.

ok jordan@


# 1.32 17-Feb-2006 marco

Add a generic delay function as per the ACPI spec.

from jordan@


# 1.31 17-Feb-2006 marco

Use tsleep instead of delay when !cold.


# 1.30 17-Feb-2006 marco

Argh, we need delays between reads/writes. The backend reads (i2c most likely)
are so slow that values come back as 0 sometimes. We have to do this
despite the spec saying we shouldnt have to. Guess having a faster
parser than others can hurt sometimes.

Idea from jordan.


# 1.29 17-Feb-2006 jordan

Fixed code for handling _INI
Rearranged debug statements
ok marco@


# 1.28 16-Feb-2006 jordan

Added stub for calling _INI
Fixed aml_dowhile for NULL return
ok marco@


# 1.27 16-Feb-2006 jordan

Fixed parser/evaluator to retrieve battery info+state
Cleanup of parser
Dell laptops return battery status
ok marco@


# 1.26 06-Feb-2006 brad

fix a typo so this will compile on amd64.


# 1.25 04-Feb-2006 marco

When ACPI_ENABLED wasn't set but the devices were it had an unknown variable.

Pointed out by ian@.


# 1.24 03-Feb-2006 jordan

Rewrote aml parser yet again...
Fixed bus_space_map for i386/amd64
Added _BST and _BIF to acpibat to get status/info
ok marco@


# 1.23 20-Jan-2006 jordan

More evaluator changes
Removed old evaluator code
Fixed _HID value
ok marco@


# 1.22 19-Jan-2006 jordan

Added support for read/write of PCI Config space
Help jason@
ok marco@


# 1.21 18-Jan-2006 jordan

Added new evaluation routines
Added helper methods for buffer/field evaluation
Fixed broken indentation on dsdt.c

ok marco@


# 1.20 17-Jan-2006 jordan

Major overhaul of the aml parser. Evaluater temporarily disabled.

ok marco@


# 1.19 06-Jan-2006 grange

Check for ACPI control availability only if ACPI_ENABLE defined.


# 1.18 06-Jan-2006 grange

Informative error messages on attach, prettier dmesg.


# 1.17 06-Jan-2006 grange

printf -> dnprintf conversion for debug messages.


# 1.16 05-Jan-2006 grange

Find SLP_TYPx values and store them for future use in
acpi_enter_sleep_state().


# 1.15 05-Jan-2006 grange

Random KNF and cleanup


# 1.14 05-Jan-2006 grange

Provide symbolic names for the acpi sleeping states and use it.


# 1.13 28-Dec-2005 marco

Moved DEVNAME to a acpidev.h

ok jordan@


# 1.12 18-Dec-2005 sturm

add back comment about wrong code

ok marco


# 1.11 16-Dec-2005 marco

Remove test for kbd_reset. Prompted deraadt@, ok jordan@


# 1.10 16-Dec-2005 jordan

Added hooks for powerdown/power button press


# 1.9 16-Dec-2005 jordan

Cleaned up common power management register i/o
Added enter sleep state ioctl
Hooked power button interrupt
ok marco@


# 1.8 16-Dec-2005 marco

Remove autoconf chatter


# 1.7 16-Dec-2005 jordan

Moved DSDT AML parsing into parent ACPI driver
ok marco@


# 1.6 14-Dec-2005 marco

Add AC device glue.


# 1.5 14-Dec-2005 marco

Adjust debug printing levels to something more sane.


# 1.4 13-Dec-2005 marco

Add glue to attach devices to apci.


# 1.3 07-Dec-2005 marco

KNF


Revision tags: OPENBSD_3_8_BASE
# 1.2 10-Jul-2005 grange

Spacing cleanup.


# 1.1 02-Jun-2005 tholo

Start on a basic ACPI framework -- does not do much more than read out the
ACPI tables into kernel memory and attach ACPI and HPET timers currently.

In order to test this code, enabling the devices in GENERIC as well as
the ACPI_ENABLE option is needed. This code does not do any thermal
control yet, so this should be done with care depending on the platform.

In the tree so more people can contribute to making this more fully
featured.

Ok niklas@ grange@ tedu@


# 1.402 07-Dec-2021 kettenis

Fix parsing of SR_IRQ resource descriptors.

ok patrick@, anton@


# 1.401 02-Nov-2021 deraadt

knf


# 1.400 30-Oct-2021 deraadt

Change hw.perfpolicy=auto by default, at startup. If the system has AC
power connected (default is yes when no driver differentiates) then default
to 100% performance. On battery, use the existing auto algorithm (which is
admittedly somewhat unrefined).
This change overrides the system/BIOS speed and puts OpenBSD in control.
As this happens very early during boot, besides speedups in all usage usage
patterns, some surprises: unhibernate and sysupgrade times are cut in half.
note: on a few architectures, the setperf fn pointer is changed late, and
thus the auto algorithm stops timeing out. kettenis and i will look for
a solution.
in snaps for more than a week.
ok kettenis


Revision tags: OPENBSD_7_0_BASE
# 1.399 20-Jul-2021 mlarkin

Fix trailing whitespace in a few places.

No code change.


# 1.398 09-May-2021 kettenis

Some x86 machines advertise the "hardware reduced" ACPI feature, advertise
S4 and S5 support, but fail to populate the SLEEP_CONTROL_REG and
SLEEP_STATUS_REG descriptions in the FADT. An example of such a machine
is the ASUS Zenbook 14 UM433DA. Any attempt to powerdown the machine
will result in a kernel crash. It turns out that using the legacy ACPI PM
registers works fine on this machien. So fall back on those registers
if SLEEP_CONTROL_REG and/or SLEEP_STATUS_REG aren't provided.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_9_BASE
# 1.397 15-Mar-2021 patrick

Add acpi_iommu_device_map(), which replaces the DMA tag with one that
is blessed with IOMMU magic, if available. This is mainly for arm64,
since on amd64 and i386 the IOMMU only captures PCIe devices, as far
as I know, which uses the pci_probe_device_hook(). This though is for
non-PCI devices attached through ACPI.

ok kettenis@


# 1.396 07-Mar-2021 patrick

Pass standard DMA tag to acpi(4) table drivers.

ok kettenis@


# 1.395 27-Dec-2020 deraadt

acpi_map_address() cannot be wrapped by SMALL_KERNEL anymore, as
it is used by acpihpet.c


# 1.394 25-Dec-2020 visa

Refactor klist insertion and removal

Rename klist_{insert,remove}() to klist_{insert,remove}_locked().
These functions assume that the caller has locked the klist. The current
state of locking remains intact because the kernel lock is still used
with all klists.

Add new functions klist_insert() and klist_remove() that lock the klist
internally. This allows some code simplification.

OK mpi@


# 1.393 17-Dec-2020 kettenis

Fix some issues with referencing named ACPI nodes from Packages.
These references need to be resolved at runtime rather than when
they're parsed such that they pick up the right values for those nodes
which can be changed when for example _INI methods run.

The current approach is to replace these reference with a string that
names the node in question. The problem with that is that packages
can also contain normal strings. Which means that we need special
code that depends on the context where the package is used.

This diff takes a different approach by retaining a reference when
parsing. Code that uses a package will need to resolve this reference
but there is no ambiguiety anymore.

ok patrick@


# 1.392 05-Dec-2020 kettenis

Add support for the _EVT method to the GPIO support code.

ok patrick@


Revision tags: OPENBSD_6_8_BASE
# 1.391 27-Aug-2020 jmatthew

Reorder the acpi attach process so we look at Processor() CPU nodes
after Device() ones, since we should prefer the newer node type.
If we see any Device() nodes, don't attach acpicpu(4) to any Processor()
nodes if they're also present. This also makes acpitz(4) and
acpipwrres(4) devices attach slightly later.

ok kettenis@, also tested by jmc@


# 1.390 16-Aug-2020 gnezdo

Remove sc_maxgpe from acpi_softc which was never read from

Remove a dead conditional from acpi_init_gpes while there.

ok jcs@


# 1.389 21-Jul-2020 deraadt

acpi can use IPL_BIO (a low interrupt) since it only enqueues operations for
later processing. The use of a high interrupt will predate suspend/resume
efforts, we had to redesign acpi to be non-reentrant obviously
discussed with kettenis, in snaps for more than a week


# 1.388 24-Jun-2020 cheloha

kernel: use gettime(9)/getuptime(9) in lieu of time_second(9)/time_uptime(9)

time_second(9) and time_uptime(9) are widely used in the kernel to
quickly get the system UTC or system uptime as a time_t. However,
time_t is 64-bit everywhere, so it is not generally safe to use them
on 32-bit platforms: you have a split-read problem if your hardware
cannot perform atomic 64-bit reads.

This patch replaces time_second(9) with gettime(9), a safer successor
interface, throughout the kernel. Similarly, time_uptime(9) is replaced
with getuptime(9).

There is a performance cost on 32-bit platforms in exchange for
eliminating the split-read problem: instead of two register reads you
now have a lockless read loop to pull the values from the timehands.
This is really not *too* bad in the grand scheme of things, but
compared to what we were doing before it is several times slower.

There is no performance cost on 64-bit (__LP64__) platforms.

With input from visa@, dlg@, and tedu@.

Several bugs squashed by visa@.

ok kettenis@


# 1.387 10-Jun-2020 jca

Give userland a consistent battery and A/C state after resume

Previously we refreshed the state of acpiac(4) and acpibat(4)/acpisbs(4)
by queueing fake ACPI events to be processed by the acpi thread.
acpibat(4) using ACPIDEV_POLL meant that its status could be refreshed
first, resulting in an APM_POWER_CHANGE event sent to userland before
the state of acpiac(4) was up to date. Because of this, apmd(8) could
see A/C unplugged when it was in fact plugged, possibly leading to
a wrong decision.

Instead of using fake ACPI events, refresh the status of acpiac(4) and
acpibat(4)/acpisbs(4) right away at DVACT_WAKEUP time. To reuse the
existing refresh code of those drivers, make their ACPI *_notify()
handlers responsible for sending APM_POWER_CHANGE events instead. Also
queue an APM_POWER_CHANGE event right after resume to retain existing
behavior.

This fixes the consistency of data shown to userland, and makes those
drivers less special.

Initially discussed last year (!) with mpi@ at Miod's place.
Feedback and ok jcs@, deraadt@ agrees with the use of DVACT_WAKEUP here.


# 1.386 29-May-2020 deraadt

dev/rndvar.h no longer has statistical interfaces (removed during various
conversion steps). it only contains kernel prototypes for 4 interfaces,
all of which legitimately belong in sys/systm.h, which are already included
by all enqueue_randomness() users.


# 1.385 14-May-2020 kettenis

Bring back the acpi(4) changes. This time around only use the ACPI _CRS
information on ACPI 5.0 and later.

ok krw@, patrick@


# 1.384 11-May-2020 jca

More accurate remaining power reporting with disparate batteries

Overall remaining power was computed as the average of the remaining
power of each battery, in percents. This doesn't work with batteries of
different capacity, so use the total capacity and remaining power
instead.

Reported by lidstah, successful tests by lidstah and benno@, ok kettenis@


# 1.383 08-May-2020 kettenis

Move parsing of _CRS into acpi(4) and pass the parsed address and interrupt
information in struct acpi_attach_args.

ok mpi@


Revision tags: OPENBSD_6_7_BASE
# 1.382 14-Apr-2020 kettenis

Implement acpi_getprop(), the ACPI equivalent of OF_getprop().

ok patrick@


# 1.381 12-Apr-2020 kettenis

Add support for the _CCA method. This method indicates whether DMA is
cache-coherent or not. To implement this, acpi(4) gets two bus_dma tags
and passes the appropriate one when attaching devices based on _CCA.
On i386/amd64, where for all practical purpose DMA is always cache-coherent,
the two tags are the same. But on arm64 they are distinct.

ok patrick@


# 1.380 07-Apr-2020 visa

Abstract the head of knote lists. This allows extending the lists,
for example, with locking assertions.

OK mpi@, anton@


# 1.379 03-Apr-2020 mpi

Fix incoherencies of errors returned by various *kqfilter routines.

EOPNOTSUPP is returned when there's no "filterops" for a given fd.
EINVAL when the requested filter isn't supported by the "filterops".
ENXIO means the underlying device is no longer valid/present.

ok visa@


# 1.378 20-Feb-2020 visa

Replace field f_isfd with field f_flags in struct filterops to allow
adding more filter properties without cluttering the struct.

OK mpi@, anton@


# 1.377 31-Dec-2019 visa

Use C99 designated initializers with struct filterops. In addition,
make the structs const so that the data are put in .rodata.

OK mpi@, deraadt@, anton@, bluhm@


# 1.376 31-Dec-2019 mpi

Convert infinite sleeps to {m,t}sleep_nsec(9).

ok kn@


# 1.375 31-Dec-2019 jsg

include sys/sensors.h for sensor use
don't depend on indirect inclusion from acpidev.h


Revision tags: OPENBSD_6_6_BASE
# 1.374 07-Sep-2019 kettenis

Revert acpipci(4) changes. Too many systems are broken. We'll try again
after 6.6 as been released. The acpireg.h change stays behind.


# 1.373 28-Aug-2019 kettenis

Use ACPI information to attach PCI busses like we do on arm64. There are a
few additional quirks though, and attaching the PCI busses is delayed to
replicate the existing code more closely. That may be changed in the
future. Also tweak how we handle MSI support and respect to ACPI flag
that says we shouldn't attempt to use MSIs.

Some fallout is expected.

ok patrick@


# 1.372 27-Aug-2019 deraadt

On amd64 the bootup and resume paths for SP/BP/SP were not performing
msr/pat, clock-startup, firmware upgrade, fpu initialization, and cpu
soft-state initialization in the same order. That kind of chaos is
crazy and fragile. Improve the acpi resume call sequence and unify order
of the operations.


# 1.371 02-Jul-2019 jcs

acpi_set_gpehandler: turn edge arg into flags, add support for
passing GPE_DIRECT which directly executes the supplied callback and
clears a level interrupt rather than going through acpi_taskq which
can be slow when other driver callbacks are in the queue.

This should not be used for GPE handlers that need to do anything
ACPI-related.

ok kettenis


# 1.370 10-Jun-2019 kettenis

Print proper ACPI version number.

ok mlarkin@


# 1.369 08-Jun-2019 kettenis

When establishing the mapping between ACPI device nodes and PCI devices,
skip PCI host bridges and devices that aren't there.

ok patrick@


# 1.368 07-Jun-2019 kettenis

Introduce acpi_getsta() and use it to eliminate some duplicated code.

ok patrick@


# 1.367 12-May-2019 kettenis

Make sure the extended PM register descriptions actually exist before
using them. Fixes machines with ancient ACPI 1.0 (such as recent
hypervisors).

ok deraadt@


# 1.366 12-May-2019 kettenis

Add support for mapping ACPI PM registers using the "extended" gasio
representation from the FADT. Mostly fixes the Lanner NCA-1510.

ok lteo@, mlarkin@, deraadt@


# 1.365 11-May-2019 lteo

Fix incorrect error message.

ok kettenis@


# 1.364 08-May-2019 tedu

free size. the allocation is the header plus the length.


Revision tags: OPENBSD_6_5_BASE
# 1.363 04-Apr-2019 kettenis

Avoid potential null-pointer dereference. Found by Kent R. Spillner.

ok deraadt@, jsg@


# 1.362 20-Jan-2019 jsg

fix SMALL_KERNEL build after changes in rev 1.361
reported by naddy@ ok deraadt@


# 1.361 19-Jan-2019 tedu

Add a pwraction sysctl that controls what the power button does on acpi.
By default, nothing changes -- shutdown is initiated. But allows turning
power button into a sleep button if desired.
(grudging) ok from a few parties


# 1.360 26-Oct-2018 kettenis

Add acpipci(4) on amd64. For now this only calls the PCI-specific _OSC
method to let the ACPI implementation know what features we support.


Revision tags: OPENBSD_6_4_BASE
# 1.359 30-Aug-2018 kettenis

Move softbutton check before the check that skips attaching certain acpi
devices. Fixes regression caused by matching on _CID in addition to
matching on _HID.

ok matthieu@, mlarkin@


# 1.358 25-Aug-2018 kettenis

Match ACPI devices based on _CID if no match for _HID is found.

ok mpi@


# 1.357 19-Aug-2018 kettenis

Add support for multiple PCI segments. Only really implemented for arm64
for now as amd64/i386 firmware still caters for legacy OSes that only
support a single PCI segment.

ok patrick@


# 1.356 03-Aug-2018 kettenis

Let ahci(4) match on _CLS instead of _HID when attaching at acpi(4). Avoids
having to add many more _HID entries to the match table.

ok deraadt@, mlarkin@


# 1.355 10-Jul-2018 kettenis

Make legacy interrupts work in acpipci(4).

ok patrick@


# 1.354 05-Jul-2018 kettenis

Add acpipci(4), a driver that supports generic ECAM-compatible PCI host
bridges based on information provided by ACPI.

ok mlarkin@


# 1.353 04-Jul-2018 kettenis

Properly pass around the PCI "chipset tag" in acpi(4) and refactor
acpimcfg(4) to call an MD initialization functions that sets up a tag for
PCI ECAM.

ok guenther@, mlarkin@, krw@


# 1.352 01-Jul-2018 kettenis

Move acpi(4) attach glue into acpi_machdep.c.

ok guenther@, deraadt@


# 1.351 01-Jul-2018 kettenis

Add support for _DSD Device Properties.

ok mlarkin@


# 1.350 30-Jun-2018 kettenis

Add intr_enable() function, intended for MI use to amd64 and i386 and use
this in the acpi(4) suspend/resume code paths.

ok deraadt@


# 1.349 29-Jun-2018 kettenis

Use <stdint.h> types.

ok mpi@, jung@, krw@, deraadt@


# 1.348 29-Jun-2018 mlarkin

ACPI: Allow (with warning) GPE handler reassignment, instead of returning
a failure code.

ok kettenis


# 1.347 26-Jun-2018 mlarkin

knf


# 1.346 26-Jun-2018 kettenis

Let acpi(4) pass down the bus dma tag.

ok deraadt@


# 1.345 25-Jun-2018 kettenis

Reorganize acpi(4) code a bit in preparation for upcoming arm64 support.

ok deraadt@


# 1.344 20-May-2018 kettenis

Wake up the acpi thread after scheduling a gpio event.

ok mpi@


# 1.343 17-May-2018 kettenis

Sprinkle some #ifndef SMALL_KERNEL such that RAMDISK kernels cuild again.

pointed out by stsp@


# 1.342 17-May-2018 kettenis

Implement GenericSerialBus OpRegion support.

ok mlarkin@


# 1.341 27-Mar-2018 kettenis

Add acpicmos(4), a driver that implements SystemCMOS OperationRegion
access support. This fixes machines where the AML doesn't check whether
support for this OperationRegion type has been registered by the OS.

ok mlarkin@


Revision tags: OPENBSD_6_3_BASE
# 1.340 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.339 10-Feb-2018 deraadt

Syncronize filesystems to disk when suspending. Each mountpoint's vnodes
are pushed to disk. Dangling vnodes (unlinked files still in use) and
vnodes undergoing change by long-running syscalls are identified -- and
such filesystems are marked dirty on-disk while we are suspended (in case
power is lost, a fsck will be required). Filesystems without dangling or
busy vnodes are marked clean, resulting in faster boots following
"battery died" circumstances.
Tested by numerous developers, thanks for the feedback.


# 1.338 08-Feb-2018 deraadt

After flushing disks, try again to release all memory. We don't need to
save dirty memory to the hibernate space.


# 1.337 08-Feb-2018 deraadt

Toss all releaseable memory, because fragmentation can get in the way
of allocating the hibernate playpen.


# 1.336 08-Feb-2018 deraadt

sr_quiesce() is a new approach for ensuring that softraid drains
output to the disks.

This is part of a larger suspend/resume filesystem-safety diff, which
has been worked on for a couple of months already. Tests by job, krw,
beck, benno, and others. Sometimes even by snapshot users...


# 1.335 29-Nov-2017 kettenis

Add support for GPIO-signaled events.

ok mlarkin@


# 1.334 16-Nov-2017 jcs

Add PCI attachment for dwiic(4) needed by Intel 100 Series machines

ok kettenis


# 1.333 14-Nov-2017 kettenis

Add a few more PNP IDs to the skip lists.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.332 17-Aug-2017 stsp

Make dwiic(4) attach its ACPI parent dependencies.
This change makes the Asus E200HA keyboard work.

Original analysis and patch by Cesare Gargano
ok kettenis@


# 1.331 09-Aug-2017 dcoppa

Add "PNP0303" (8042 PS/2 Controller) to acpi_isa_hids[]

ok kettenis@


# 1.330 22-Jul-2017 jcs

acpibat: look for _BIX first before falling back on _BIF

ACPI 4.0 deprecated _BIF for battery status so some newer machines
are exporting _BIX which extends _BIF's package with a few extra
fields. When using _BIX, export the new discharge cycle count as a
sensor.

feedback from kettenis and anton
ok deraadt


# 1.329 20-Jul-2017 kettenis

Start a list of isa(4) device names such that we can avoid printing them
as "not configured".

ok mikeb@, millert@, deraadt@


# 1.328 08-Apr-2017 deraadt

Sensors are run as callbacks inside tasks. During suspend a sensor
could be running inside a driver that will be force-detached, or due
to tsleep end up disrupting the softstate/hardstate contract. At
suspend time, quisce all these callbacks by waiting for completion.
This issue has never been observed for real, but may be implicated in
suspend/resume failures.
ok kettenis guenther mlarkin


# 1.327 08-Apr-2017 deraadt

Two prototypes for acpi_maptable; merge to one place


Revision tags: OPENBSD_6_1_BASE
# 1.326 27-Mar-2017 deraadt

Now that hibernate_alloc() only has clean success/failure, don't
need to call hibernate_free() to clean up a partial mess.
ok mlarkin kettenis


# 1.325 13-Mar-2017 deraadt

allow ramdisks to build; ok jcs


# 1.324 12-Mar-2017 jcs

try to attach acpisbs first and if successful, don't attach acpibat


# 1.323 02-Mar-2017 natano

Add a new sysctl machdep.lidaction. The sysctl works as follows:

machdep.lidaction=0 # do nothing
machdep.lidaction=1 # suspend
machdep.lidaction=2 # hibernate

lidsuspend is just an alias for lidaction, so if you change one, the
other one will have the same value. The plan is to remove
machdep.lidsuspend eventually when people have upgraded their
/ets/sysctl.conf.

discussed with deraadt, who came up with the new MIB name
no objections mlarkin
ok stsp halex jcs


# 1.322 28-Feb-2017 natano

Untangle abstract sleep modes from ACPI sleep states. This paves the way
for further work in the area.

idea and ok deraadt
ok mlarkin


# 1.321 22-Feb-2017 jcs

add acpisbs, an acpi smart battery subsystem driver reading data
over smbus

currently disabled because it conflicts with acpibat

ok deraadt, kettenis


# 1.320 14-Jan-2017 kettenis

Revert aml_rdpciaddr changes; breaks several machines that were working
before.


# 1.319 11-Jan-2017 jsg

Increase the size of the buffers used for _HID and _CID strings to be
able to fit "Hyper_V_Gen_Counter_V1".

ok mlarkin@ deraadt@


# 1.318 08-Jan-2017 kettenis

Make aml_rdpciaddr() work for for devices behind bridges. The code now
uses the mapping between AML nodes and PCI devices that we establish.
Because _INIT methods may end up calling aml_rdpciaddr(), make sure we
create that mapping early. Also handle devices that aren't actually present.
These devices are now included in the mapping and reads will return an
all-ones patterm whereas writes are a no-op..

ok mlarkin@


# 1.317 25-Oct-2016 pirofti

Add the resource index to aml_parse_resource() callback function.

This is needed by an upcoming acpiec commit that handles machines breaking
the current ACPI specifications.

Change suggested by and ok kettenis@, guenther@


# 1.316 18-Sep-2016 guenther

If the APIC table (MADT) isn't present then ci_acpi_proc_id won't be set
when acpicpu tries to attach, leading to a NULL dereference. The ACPI
processor object on the Dells where this was reported is bogus and useless
anyway so apply the same test when deciding whether to attach to processor
objects as acpicpu would when matching them to cpus.

Problem reported by Eivind Eide (xenofil (at) gmail.com) and
Johan Huldtgren (johan+openbsd-bugs (at) huldtgren.com).
Testing by Eivind Eide.
ok kettenis@ mlarkin@


# 1.315 03-Sep-2016 naddy

Write the system time back to the RTC every 30 minutes.
This fixes the problem that long-running machines which were not
shut down properly would reboot with a badly offset system time.

hints and ok kettenis@


# 1.314 31-Aug-2016 mlarkin

Don't put a device in the wake device list if _STA says it's not there.

Fixes at least one machine that woke up from sleep / poweroff immediately.

ok kettenis@, tested by kettenis and martijn@


# 1.313 28-Jul-2016 kettenis

Store the acpi processor ID/UID in struct cpu_info, and use it to attach
acpicpu(4) drivers to the right cpu(4).

ok mlarkin@, guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.312 10-Jun-2016 kettenis

The power management timer has been made optional in ACPI 5.0A, so don't
attempt to attach acpitimer(4) if the timer isn't present.

Pointed out by jcs@

ok jcs@


# 1.311 08-May-2016 kettenis

Make sure devices listed by a device's _DEP method are attached before
we attach the device itself. The _DEP methode, introduced in ACPI 5.0,
is a hint that the device in question depends on OpRegion support from
the devices listed by _DEP, so we have to order them the proper way.
To prevent us from attaching those devices again when we encounter them
later walking down the device tree, keep track of our attempts to
attach a device driver to them using aflag in the aml_node structure.

ok guenther@


# 1.310 25-Apr-2016 pirofti

We attach acpihpet(4) and acpiec(4) without using the HID, so do not
report it in the dmesg.

OK deraadt@, mlarkin@.


# 1.309 02-Apr-2016 kettenis

Reduce dmesg spam by knocking out some acpi hardware IDs that don't need a
driver.

ok mpi@


# 1.308 29-Mar-2016 kettenis

Check if a device is present (using _STA) in acpi_foundhid. This prevents
us from reporting devices as "not configured" that aren't actually present,
and allows us to remove duplicated code from several drivers.

ok deraadt@, ok jsg@


# 1.307 29-Mar-2016 kettenis

Implement a cleaner way to prevent acpivout(4) from attaching if we have
a platform-specific method of controlling the backlight.

ok millert@, mlarkin@, jung@


# 1.306 28-Mar-2016 kettenis

Adding each and every new acpi device driver to acpi_foundhid() is
gettinga bit of the burden. And it really isn't how our autoconf
framework is supposed to work for busses that can be enumerated. All
the drivers already check for a matching _HID in their attach
function. So we can just drop the checks in acpi_foundhid() and let
the drivers sort themselves out.

Print a "not configured" line for devices for which we don't attach adriver.
This may be a bit much as there are devices for which we will not have a
driver. This will be removed if it causes too much dmesg spam.

ok deraadt@, mlarkin@


Revision tags: OPENBSD_5_9_BASE
# 1.305 17-Jan-2016 jsg

Take acpi_foundhid() outside of #ifndef SMALL_KERNEL so the ramdisk
kernel can find and attach dwiic(4) and sdhc(4).


# 1.304 17-Jan-2016 jsg

remove an unused global variable


# 1.303 14-Jan-2016 kettenis

Attach dwiic(4) on the Intel Bay Trail i2c controllers as well.


# 1.302 12-Jan-2016 kettenis

Improve emulation of PM registers on Hardware-redcued ACPI by emulating
SPL_TYPx, SLP_EN and WAK_STS using the new SLEEP_STATUS_REG and
SLEEP_CONTROL_REG registers. Makes the transition into S5 (powerdown) work
on the ASUS X205TA.

ok deraadt@, mlarkin@


# 1.301 12-Jan-2016 jcs

Add dwiic, a driver for the Synopsys DesignWare i2c controller found
on the Samsung ATIV Book 9 laptop. This initial version only
supports ACPI config/attachment.

Add ihidev, a HID-over-i2c driver largely based on uhidev. dwiic
handles attaching ihidev devices found in ACPI.

Add ims, a HID-over-i2c mouse/trackpad driver to get basic cursor
and button functionality from HID-compliant i2c trackpads.

ok kettenis deraadt


# 1.300 11-Jan-2016 kettenis

Make sdhc(4) attach to hardware IDs "80860F14" and "PNP0FFF".


# 1.299 09-Jan-2016 kettenis

Initial support for Hardware-Reduced ACPI. This skips the legacy mode checks
by emulating the PM_CNT register such that SCI_EN is always enabled. It also
skip attaching acpitimer(4) since the relevant registers are no longer there.

ok deraadt@, mlarkin@


# 1.298 06-Jan-2016 kettenis

Follow the spec and check the SCI_EN bit to see if ACPI has already
been enabled and don't bother enabling it again if it is. This should fix
systems that don't implement legacy mode and therefore set ACPI_ENABLE and
ACPI_DISABLE to zero.

ok mlarkin@, yasuoka@


# 1.297 23-Nov-2015 reyk

Back out the CRITICAL change, turn it back from 10% to 25%. Some
people with old/broken batteries didn't like this change, and the
underlying heuristics are wrong.


# 1.296 22-Nov-2015 reyk

The "CRITICAL" APM battery level, that is exported/faked by the ACPI
stack, is currently set to 25%. With 25% battery left, a typical
modern laptop will have at least one hour runtime left. It is an
arbitrary value, and other APM power layers in the kernel set CRITICAL
to something like 5% or 10%, so set it to 10% for now.

OK mlarkin@


# 1.295 28-Sep-2015 deraadt

In low-level suspend routines, set cold=2. In tsleep(), use this to
spit out a ddb trace to console. This should allow us to find suspend
or resume routines which break the rules. It depends on the console
output function being non-sleeping.... but that's another codepath which
should try to be safe when cold is set.
ok kettenis


# 1.294 06-Sep-2015 deraadt

clarify malloc arguments (base + length...)


# 1.293 28-Aug-2015 deraadt

fairly simple sizes for free(); ok tedu


# 1.292 20-Aug-2015 kettenis

Respect the access size when reading or writing to pci config space.
This version also makes sure writes are properly aligned, preventing
the panics seen with the previous incarnation of this change.

Fixes battery status passthrough in vmware, brightness keys on my x220
and who knows what else.

ok mlarkin@, deraadt@


Revision tags: OPENBSD_5_8_BASE
# 1.291 04-Aug-2015 deraadt

If we are setting ACPI_PM1_BM_RLD do it only once, and remember to
repeat this action at resume time.
ok mlarkin guenther


# 1.290 29-Jul-2015 deraadt

Something subtle in previous revision is locking up suspend or booting
on some laptops. Mailing lists threads mention x220 and Asus machines.
Reverting for now.


# 1.289 21-Jul-2015 kettenis

iRespect the access size when reading or writing to pci config space.
Fixes battery status passthrough in vmware and who knows what else.

Tested by many.

ok mlarkin@, deraadt@


# 1.288 17-Jul-2015 mlarkin

fix comments


# 1.287 26-Mar-2015 jsg

Use the 0x10 extended capability to determine if the trackstick
buttons are wired to the trackpad and need to be re-routed to the
trackstick. Without this change the buttons on 2015 thinkpads
get picked up as extended buttons that show up as scroll up/down.

Remove the X1 Carbon 2015 (LEN0048) and X250 (LEN0046) from the
top button area/soft buttons quirks list. Also avoid using the
quirk list entirely if the capability bit is set.

Based on work from jcs@ with feedback from mpi@
ok mpi@ jcs@


# 1.286 20-Mar-2015 kettenis

The ACPI standard says that a PCI function number of FFFF can be used to refer
to all functions on a device. This makes no sense in the context of accessing
PCI config space. Nevertheless stsp@ has a machine with AML that tries to
do config space reads from a device where _ADR encodes function number FFFF.
This is almost certainly buggy AML, but since Linux (and presumably Windows)
boot just fine on this machine we need to work around this. Linux effectively
treats function FFFF as function 7, which isn't present on this machine.
Instead we simulate a read from a non-existent device by returning a bit
pattern of all ones. Writes to function FFFF will still panic.

ok mlarkin@


# 1.285 16-Mar-2015 deraadt

Reduce use of SMALL_KERNEL in acpi, bringing acpiec driver and more gpe
handling into RAMDISK. This is now possible because the install media
has ample room. The goal is to reduce special cases where we may be
skipping (unknown) important operations...
ok mlarkin kettenis


# 1.284 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.283 07-Feb-2015 deraadt

New framework that allows hibernate to pass in entropy from it's fresh
boot.
ok mlarkin


# 1.282 06-Feb-2015 mlarkin

Fix a hibernate crash on some machines due to unmapping a page that
may not have been mapped previously (in the failure to hibernate case).

Also ensure that the lowmem ptp is mapped in all cases (not just MP).

ok kettenis


# 1.281 17-Jan-2015 deraadt

resume_randomness() before spinning up other cpus...


# 1.280 15-Jan-2015 jsg

Define a new wscons mouse type for Synaptics clickpad devices that lack
physical buttons. This will be used if the acpi pnpid for the mouse
matches a list provided by Synaptics found in Linux.

Combined with the xenocara changes this will remove the need for an xorg
config file for the x240/t440/t540 generation of thinkpads.

ok matthieu@ shadchin@ kettenis@


# 1.279 11-Jan-2015 kettenis

Tolerate checksum errors in ACPI tables (but complain about them). Apparently
there are machines out there with broken BIOSen with such checksum errors in
thier RSDT tables. Windows runs fine on these machines, so it probably does
not check. Linux ignores checksum errors as well (and prints a warning).

ok deraadt@, krw@


# 1.278 18-Dec-2014 deraadt

suspend and resume the RNG (at what looks like the right spots)
ok reyk djm


# 1.277 09-Dec-2014 doug

Replace some malloc(n*size,...) calls with mallocarray().

ok tedu@ deraadt@


# 1.276 06-Dec-2014 mpi

If "machdep.lidsuspend" is not set, do not go back to sleep if the lid
is still closed when resuming. This allows docked laptop to be resumed
without opening them.

ok mlarkin@


# 1.275 23-Nov-2014 mlarkin

If a machine has no _LIDs, then none can be open. This diff fixes a problem
where machines without any _LIDs were going back to sleep after hibernate
due to the recent suspend-if-lid-closed-on-resume diff. Noticed on qemu,
which doesn't have any _LIDs.

discussed with deraadt


# 1.274 18-Nov-2014 krw

Nuke yet more obvious #include duplications.

ok miod@


# 1.273 08-Nov-2014 mlarkin

If resuming from sleep (zzz/ZZZ) and the lid is still closed, go back to
sleep. This prevents accidental lid flex or slight opening in a backpack
from waking the machine up and leaving it resumed (powered on).

ok deraadt@


# 1.272 26-Sep-2014 kettenis

Rework piglet and pig allocation. Currently the piglet gets allocated
deep down in the suspend path, where it is really hard to recover from
allocation failure. So allocate the piglet early on in the suspend path.
Also change the piglet and piglet allocation functions to use km_alloc(9)
instead of doing pmemrange magic. This removes a bunch of code which, in the
case of the piglet allocation, is broken since it results in a NULL pointer
dereference. Also switch the piglet allocation to not wait. If we can't
allocate 16MB of phys contig memory on a halfway modern machine we're almost
certainly under a lot of memory pressure and we're better off not trying to
hibernate anyway.

ok mlarkin@


# 1.271 25-Sep-2014 kettenis

Bring back rev 1.249. Now that mp_setperf() has been fixed, it should be
safe to restore perflevel when resuming. Should fix the issues people
see with the processor running at full speed heating up the machine while
the apm command reports that the CPUs are running at a low speed.

ok dcoppa@, deraadt@


# 1.270 23-Sep-2014 kettenis

Release the acpi lock around calls to wsdisplay_suspend() and
wsdisplay_resume(). The X server may adjust the display brightness just
before/after the VT switch when suspending/resuming. In fact, it may
already be blocked adjusting the display brightness for some reason
unrelated to the suspend request.

The VT switch request will eventually time out, but the delay is annoying
and the failed VT switch will lead to problems after resume.

Needed for a future update of the xf86-video-intel driver.

ok mlarkin@


# 1.269 19-Sep-2014 kettenis

Use config_suspend_all(9).


# 1.268 08-Sep-2014 kettenis

Clear the PME Status bit on PCI devices in response to Device Wake events.
Fixes the interrupt storm on the 2009 Mac mini with WOL enabled on its
nfe(4) interface.

ok mlarkin@


Revision tags: OPENBSD_5_6_BASE
# 1.267 20-Jul-2014 mlarkin

Support hibernating to softraid crypto volumes.

much help and ok from deraadt@


# 1.266 16-Jul-2014 mlarkin

Reenable hibernate RLE support and flush+zero all memory after unpack.


# 1.265 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.264 12-Jul-2014 kettenis

Add a function to drop all clean pages on the page daemon queues and call
it when we hibernate.

ok mlarkin@, miod@, deraadt@


# 1.263 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.262 11-Jul-2014 mlarkin

Flush the buffercache to 16MB on hibernate and restore its previous max
size (kern.bufcachepercent) on resume, for better hibernate performance.

ok beck@


# 1.261 10-Jul-2014 blambert

remove unneeded header

ok mlarkin@


# 1.260 01-Jul-2014 kettenis

Adding an additonal delay after each access to SystemMemory or SystemIO
during autoconf makes no sense.

ok deraadt@


# 1.259 23-Jun-2014 kettenis

SystemMemory is used to access memory mapped registers on some machines, so we
must use the bus_space(9) API to access it instead of memcpy(9). Also make
sure we properly align access to fields that need more than byte alignment.

ok guenther@


# 1.258 21-May-2014 mlarkin

Fix invalid / missing format specifiers in several dnprintf statements
(used only when ACPI_DEBUG is enabled)

From a diff on tech@ by Fabian Raetz, with a minor change by me, tested
on i386 and amd64 with ACPI_DEBUG enabled and disabled.


# 1.257 25-Apr-2014 mlarkin

Defer the free of the hibernate structures until later, when it is safe to
do so.

Freeing the hibernate structures too early leads to inadvertent calls into
splx when we aren't ready for them yet. Found when trapping splx calls in
the resume path.

discussed with deraadt@ at m2k14


# 1.256 13-Apr-2014 mlarkin

Fix a badly worded debug message that implied the entire machine was going
to sleep, instead of just the acpi thread going to sleep.


# 1.255 31-Mar-2014 mpi

Change the order of operations during a suspend/resume cycle and
call bufq_quiesce() after executing the DVACT_QUIESCE handlers.

This should be safe since no disk nor controller drivers have such
handler but it will allow us to detach sd(4) devices attached to a
USB bus.

Another benefit pointed out by kettenis@ is that drivers that need
to read a firmware from the disk should be able to do it at resume
time in a DVACT_WAKEUP handler.

ok kettenis@, deraadt@


# 1.254 13-Mar-2014 dlg

get rid of the assumption that the head of the alldevs list is the
"mainbus" device. this breaks when mpath is enabled because it
attaches before mainbus and therefore takes the head position.

have autoconf provide device_mainbus() which looks up mainbus_cd,
and use that instead.

discussed with deraadt who just wants mpath stuff to move forward
despite there being many ways to shine this particular turd.


Revision tags: OPENBSD_5_5_BASE
# 1.253 21-Feb-2014 deraadt

Do the lid control further on in the resume sequence, since this is
AML, and will do WAITOK operations. While cold, that is a bad idea.
We seem to be safe with respect to the spec, I do not see any event
debounce issues here.
ok kettenis, tested by many


# 1.252 20-Jan-2014 claudio

Instead of checking sc->sc_revision <= 1 check that the
fadt->hdr_revision <= 1. This is a better check since it checks
the version of the proper descriptor. This allows the alix APU
to reboot since those boards have currently a wrong sc->sc_revision.
Committing this now so that it can be tested easier.
With and OK jsg@ also fine by derradt@


# 1.251 24-Dec-2013 kettenis

If the FADT has its SMI_CMD set to zero, assume we're only ACPI-only hardware
and don't need to disable SMI ownership of the ACPI hardware registers.

ok mlarkin@


# 1.250 23-Dec-2013 kettenis

Revert previous commit. It makes resume hang on some MP systems running
GENERIC.MP.


# 1.249 16-Dec-2013 kettenis

Restore hw.setperf upon resume like we do for apm(4) on i386. Seems we need
to do this fairly late in the resume to avoid hangs.

ok deraadt@, mlarkin@


# 1.248 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


# 1.247 06-Nov-2013 mpi

Add support for Power Resources for Dx states and the necessary hook
for PCI devices. This hook should be called twice, before and after
changing the power state of a PCI device.

Before setting the device to the new state, the ACPI layer will notify
every power resources linked to the device for that state and make sure
they are turned "_ON". After changing the state of the device, it will
decrement the reference of every power resources linked to that device
for the old state and turn them "_OFF" if they are no longer referenced.

This fixes the no-USB after resume problem seen on various ThinkPad,
problem initialy diagnosed with Alexander Polakov.

ok kettenis@, deraadt@


Revision tags: OPENBSD_5_4_BASE
# 1.246 01-Jun-2013 mlarkin

Stop printing ACPI wakeup devices in dmesg after the 16th wakeup device.

Helps with VMware VMs that report hundreds of wakeup devices.

ok reyk@, deraadt@


# 1.245 31-May-2013 bluhm

Fix the build for a kernel without wd and pciide in its config.
OK deraadt@ tedu@


# 1.244 30-May-2013 deraadt

Enforce ca_activate tree-walks over the entire heirarchy for all events,
cleaning up some shutdown-hook related code on the way.
(A few drivers related to sparc are still skipped at kettenis' request)
ok kettenis mlarkin, tested by many others too


# 1.243 18-Apr-2013 deraadt

pin the acpi thread to the BSP, because AML and SMI cannot be trusted. I
was certain we were doing this already, but it turns out no...


Revision tags: OPENBSD_5_3_BASE
# 1.242 24-Dec-2012 guenther

In acpiioctl(), Return though the splx() path if suser() returns an error

ok deraadt@


# 1.241 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


# 1.240 04-Oct-2012 ehrhardt

Use information provided by ACPI to attach secondary PCI host bridges.

ok kettenis@


# 1.239 07-Sep-2012 kettenis

Add a new function called acpi_pci_min_powerstate() that returns the lowest
power state for a PCI device for the power state (currently S3 or S4) we're
transitioning into. This function is probably too simplistic; there is a lot
of (silly) complexity in the ACPI spec.

ok deraadt@


Revision tags: OPENBSD_5_2_BASE
# 1.238 13-Jul-2012 pirofti

Disable acpivideo(4) on ASUS laptops.

There are some models that panic whenever an event is received.
Some cope, some don't. In order to get this right acpiec(4) requires
major rework. Until then let machines boot.

Discussed with deraadt@.


# 1.237 11-Jul-2012 mlarkin

for now, restrict hibernate to root in the acpi ioctl kernel interface.
normal rules for controlling this interface through apmd still apply.

ok deraadt@


# 1.236 09-Jul-2012 deraadt

tiny little oops in untested hibernate block


# 1.235 09-Jul-2012 deraadt

clean up ioctl() case statements


# 1.234 09-Jul-2012 deraadt

acpi_disable_onegpe() is not used; ok pirofti


# 1.233 24-May-2012 kettenis

If _MIN > _MAX consider a WordBus resource to be invalid. Fixes a panic on
the IBM x3550-M4.

ok mikeb@


# 1.232 29-Mar-2012 mlarkin

Missing #ifdef in previous diff


# 1.231 29-Mar-2012 mlarkin

Prevent hibernation attempts on machines with unsupported disk controllers.
The same check is also performed later in the hibernate sequence, but by
then it's too late to cleanly unwind (presently).

ok deraadt


# 1.230 26-Mar-2012 deraadt

simplify conditionals in acpi_sleep_state()
ok mlarkin


# 1.229 26-Mar-2012 deraadt

hook in the hibernate request code; half of this diff is from mlarkin
ok mlarkin


Revision tags: OPENBSD_5_1_BASE
# 1.228 20-Sep-2011 deraadt

Very early in suspend while the clock is still ticking realtime,
save the clock back to the rtc. The zaurus already did this.
ok phessler miod kettenis


Revision tags: OPENBSD_5_0_BASE
# 1.227 02-Jul-2011 nicm

kqueue attach functions should return an errno or 0, not a plain 1. Fix
the obvious cases to return EINVAL and ENXIO.

ok tedu deraadt


# 1.226 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.225 16-Jun-2011 pirofti

Import acpitoshiba driver.

This is originally written by Hiroyuki Aizu, ported and integrated
in our tree by Javier Vazquez <javama4 AT gmail> with tweaks and
suggestions by me.

Import prodded by deraadt@ and the lack of testers on tech@.


# 1.224 27-Apr-2011 jcs

attach acpithinkpad to newer lenovo models like the x120e

ok deraadt@


# 1.223 22-Apr-2011 jordan

Fix uninitialzied variables and formatting strings (-Wxxx errors)


Revision tags: OPENBSD_4_9_BASE
# 1.222 02-Jan-2011 jordan

Re-add code for ACPI taskqueue, eliminates need for state vars in acpi softc
Serialize processing ACPI tasks for sleep/powerdown/GPE event
ok deraadt@


# 1.221 31-Oct-2010 guenther

Revert last commit: it breaks resume on Thinkpads
"then please back it out" deraadt@


# 1.220 26-Oct-2010 jordan

Add task queue for ACPI gpe and notify handlers
This fixes eject on Dell Latitude dock and an issue on the Dell Mini
battery update.
ok deraadt


# 1.219 07-Oct-2010 jordan

Create separate functions for enabling wake and runtime gpes.
Remove old suspend/resume gpewalk function
ok deraadt, mlarkin


# 1.218 05-Oct-2010 jordan

split acpi_enable_onegpe to separate enable/disable functions
ok deraadt


# 1.217 05-Oct-2010 jordan

Added acpi_disable_allgpes function to clear GPE status
ok deraadt


# 1.216 05-Oct-2010 deraadt

clean up acpi_softc use more


# 1.215 05-Oct-2010 deraadt

delete a bit of #if 0 code


# 1.214 05-Oct-2010 deraadt

hide use of our global acpi_softc pointer a bit better


# 1.213 29-Sep-2010 deraadt

grab the acpi thread lock very early in acpi thread startup; not that it
is likely that anyone will race us... but why be careless.
ok kettenis


# 1.212 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.211 29-Aug-2010 jasper

- add a check for FWRITE here too.

ok miod@


# 1.210 27-Aug-2010 kettenis

Make PCI Power Management optional, and only enable it when acpi(4) attaches.

ok deraadt@


Revision tags: OPENBSD_4_8_BASE
# 1.209 08-Aug-2010 kettenis

Close race between the acpi thread and wscons ioctls running in process
context. We might rip this out post-release and replace it with a solution
that serializes things in a more obvious way.

ok deraadt@, miod@ and "those crazy texans who ok anything"


# 1.208 08-Aug-2010 deraadt

Report the acpi sleep states when on a RAMDISK; ok krw


# 1.207 07-Aug-2010 kettenis

Enforce acpi register access rescrictions.

ok mlarkin@, deraadt@, tested by marco@


# 1.206 07-Aug-2010 deraadt

upon resume, notify all the acpiac and acpibat drivers to update
their status. some laptops do not do this automatically.
ok kettenis canacar


# 1.205 06-Aug-2010 marco

Always call _PSW on _LID devices that support it to enable lid open to
resume the machine. Conversely disable it when the machine wakes up.

Tested by several ok deraadt


# 1.204 06-Aug-2010 deraadt

As part of a revamp of the PM1/GPE code, I write this diff to look for
unmanaged EN & STS bits in the PM1 register at interrupt time and report
them. As a side effect this splits the STS acknowledgement into two writes
(for power, and sleep) instead of one. The printf that is added (to spot
unmanaged STS bits) has not yet been triggered as far as we know.

Before the "write to PM1 registers at the right offset" diff went in, this
was not neccessary. But newer thinkpads do not have a working soft power
button without this diff.

We have no idea why.
ok mlarkin kettenis


# 1.203 05-Aug-2010 deraadt

PM1 power button events were generating 'fake events' to acpibtn PWRB,
but if there is no such device these were being tossed. Instead, arrange
for all power button handlers to flag a softc variable which is processed
by the thread; so that one place will know to request a powerdown (psignal
to init)
ok marco mlarkin


# 1.202 05-Aug-2010 deraadt

rename a variable which is annoyingly poorly named


# 1.201 05-Aug-2010 mlarkin

Write to the correct offset for various ACPI pmreg registers. Writing to
the correct register does properly clear the wake status bits and fixed
event bits, which allows some machines to have more than 1 suspend cycle.

bug spotted by deraadt@

tested by deraadt@ and myself on a half-dozen machines and others on
hackers@

ok deraadt@, marco@


# 1.200 05-Aug-2010 deraadt

Fix the power button. The pm1 register is 16 bits in size.
My mistake, a while back.
tested by krw


# 1.199 04-Aug-2010 kettenis

Print supported sleep states.

ok marco@, phessler@, deraadt@


# 1.198 03-Aug-2010 mlarkin

Use the proper flag for re-enabling certain hardware events (power btn,
sleep btn). The (incorrect) flag prevoiously used coincidentally had the
same value, so this shouldn't cause any different behavior than before.

tested on a variety of machines (i386, amd64, sp, mp) - no behavioral
change seen

ok deraadt@


# 1.197 28-Jul-2010 marco

Make all gasio failures look and behave the same.


# 1.196 28-Jul-2010 mlarkin

Delay a little longer on suspend before giving up. Fixes machines that
sometimes don't suspend because the sleep transition takes too long
(symptoms like failing to sleep but the backlight stays on).

ok deraadt@


# 1.195 27-Jul-2010 deraadt

remove XXX; EC can now be attached early when needed; ok jordan


# 1.194 27-Jul-2010 deraadt

more brackets feels good


# 1.193 27-Jul-2010 deraadt

XXX annotate a block in acpi_enter_sleep_state() that is pretty worrying


# 1.192 27-Jul-2010 deraadt

horrific indentation that nearly made me puke


# 1.191 27-Jul-2010 mlarkin

Set the sleeping indicator light on machines that support it, in case SMI
doesn't do it for us. Shows the 'moon' LED on older Thinkpads when
sleeping.

Tested on T43p, W500, T510 by me and on X61s by deraadt@.

ok deraadt@


# 1.190 27-Jul-2010 jordan

Early initialization of acpiec if ECDT table exists
Fixes hang when booting thinkpads while docked
ok deraadt


# 1.189 26-Jul-2010 deraadt

remove merge error which was commited


# 1.188 26-Jul-2010 pirofti

Add support for Sony ACPI hotkeys via a new driver: acpisony(4).

Currently it only works for the suspend button and tries to do right for
the brightness events, but I haven't found a Sony laptop that like to do
right so far.

In the future I want to make the brightness keys work on all Sony's and
also add support for the zoom hotkeys and whatever other funky keys I can
find on those things.

Okay deraadt@.


# 1.187 22-Jul-2010 deraadt

pretty large cleanup of SMALL_KERNEL


# 1.186 21-Jul-2010 deraadt

in the acpi GPE handler, late ack level interrupts not edge interrupts.
Duh.


# 1.185 21-Jul-2010 deraadt

Merge the edge and level gpe handlers into one (now possible since the
gpe structure tells us whether the gpe is edge or level)


# 1.184 21-Jul-2010 deraadt

delete #if 0 code we are not going to use; ok marco


# 1.183 20-Jul-2010 deraadt

make the mask code for acpi_enable_onegpe() match other similar code


# 1.182 20-Jul-2010 deraadt

Handle the acpi interrupt controller with proper edge/level handling,
wrapped in spltty to avoid racing against the interrupt controller.
Repair the gpe bit masking code while there, and do operations in the
right order
ok kettenis mlarkin, help from jordan at figuring out the order of
operations.


# 1.181 20-Jul-2010 matthew

When we're preparing to go to sleep, only call bufq_restart() if other
preparations failed.

ok kettenis@, thib@, deraadt@


# 1.180 19-Jul-2010 deraadt

permit compilation with ACPI_DEBUG


# 1.179 19-Jul-2010 deraadt

there is no need for a seperate dsdt_softc pointer; dsdt does not have a
softc but is just a part of the single acpi driver.
ok jordan


# 1.178 19-Jul-2010 deraadt

acpi_delay() is no longer needed; ok jordan


# 1.177 18-Jul-2010 mlarkin

Clear event status on resume, to avoid going back to sleep/reboot/shutdown
immediately after resume. Tested by a bunch of people on hackers@ on
various hardware. Fixes at least Toshiba NB200 and Gateway LT3103u.

ok marco


# 1.176 13-Jul-2010 deraadt

rename some thread related interfaces so they are logical


# 1.175 10-Jul-2010 jordan

Verify _ADR bdf is correct (some systems return 0xffff)
Works on Dell x9xx and Studio 1555


# 1.174 09-Jul-2010 jordan

Clear the noise


# 1.173 08-Jul-2010 jordan

Add mapping for ACPI device to PCI bdf (match autoconf tree)
Simplify resource parsing function to use buffer argument
Convert namespace linked lists to use queue macros
ok marco@, deraadt@


# 1.172 06-Jul-2010 deraadt

Use spltty() to lock downcalls from apm against the information being
modified by the acpi thread.
ok kettenis


# 1.171 06-Jul-2010 deraadt

Don't use a workq to do the suspend, because it races aginst the acpi
thread. Instead, just tell the acpi thread to do the suspend for us.
This makes apmd & zzz work correctly. While here, have acpithinkpad
attempt to post the event to apm before prompting the suspend itself.
ok kettenis marco mlarkin


# 1.170 05-Jul-2010 mlarkin

Reset register SCI_EN on resume. The ACPI spec clearly states we don't have
to do this, but many machines don't, or forget to, which leads to all sorts
of problems on resume (broken battery stats, overheating, failure to resume
more than once, etc).

tested by many, ok marco@, deraadt@


# 1.169 01-Jul-2010 thib

do bufq quiesce again, since vnd/the bufq_done call in
biodone has been fixed.


# 1.168 01-Jul-2010 jordan

Backout recent AML changes, breaks on R210 and others


# 1.167 01-Jul-2010 jordan

Simplified aml_parse_resource call, cleanup code
ok marco


# 1.166 01-Jul-2010 jordan

Fix subordinate bus # for multi-root PCI buses
ok marco, kettenis


# 1.165 30-Jun-2010 thib

Disable/partially backout the bufq quiesce changes as this
is causing havoc with vnds and release must be buildable.


# 1.164 29-Jun-2010 deraadt

Fix ramdisks (new code was placed inside a SMALL_KERNEL chunk)


# 1.163 29-Jun-2010 jordan

Add support for mapping ACPI to PCI devices
ok kettenis, deraadt


# 1.162 29-Jun-2010 kettenis

Call bufq_quiesce() on suspend to stop all I/O and bufq_restart on resume.
Makes suspend/resume work much better while doing disk I/O.

ok mlarkin@


# 1.161 29-Jun-2010 tedu

60 is a more reasonable guess for number of minutes in an hour than 100.
spotted by TeXitoi. ok deraadt


# 1.160 27-Jun-2010 jordan

Add _acpi_matchhids routine, precursor to pci device match


# 1.159 27-Jun-2010 mlarkin

Clear ACPI fixed event status on resume (power buttons/etc) so that some
machines don't immediately resume after suspending to S3.

Tested by kettenis@ and I on 5 or 6 machines, deraadt@ "yeah, looks good"


# 1.158 27-Jun-2010 jordan

Cleaned up acpi_maptable, adds to table list directly


# 1.157 07-Apr-2010 deraadt

If we have done a wsdisplay_suspend(), and ACPI decides it does not want to
suspend, we must roll back by calling wsdisplay_resume() or the virtual
console switching will be locked forever
ok oga


# 1.156 07-Apr-2010 kettenis

Make suspend/resume work on MP machines (running an MP kernel). Joint work
from mlarkin@ and me, with some amd64 fixes thrown in by deraadt@

ok marco@, deraadt@, pirofti@, mlarkin@


# 1.155 31-Mar-2010 kettenis

Bring /dev/apm support in line with apm(4). Make the suspend button send
a suspend request event to apmd(8) instead of suspending immediately. Also
keep track of whether /dev/apm and /dev/apmctl are currently open, such that
we can still suspend immediately if apmd(8) isn't running.

ok deraadt@, marco@, pirofti@, jsing@, oga@


# 1.154 30-Mar-2010 oga

Prevent the apmd/x races for good.

When we hit suspend time, go through all wsdisplays on the system. if
they are in mode MAPPED, but not MODE_DUMBFB then if possible do a full
vt switch to a !mapped vt, and prevent switching back until resume time.
This has to be called from MD code because this involves userland
running so that X can run the vt switch signal handler. This way, any
case where we are using the "poke registers from userland" model, we
will not be on the hardware when we go down, so the kernel can actually
handle thing properly.

Tested on several acpi laptops (by kettenis@ and ian@), x40 (me and
beck@ at LEAST) and zaurus (me). Maybe others, but if so I forgot who at
this time..

Idea from deraadt somewhere over the Faroe Islands (I thought of a
similar thing myself a while ago). Much prompting from him. Ok and
comments miod@


# 1.153 25-Mar-2010 oga

Fix & vs. && in the kqfilter code.

Based on a comparison with the apm code.

ok deraadt@, kettenis@


Revision tags: OPENBSD_4_7_BASE
# 1.152 26-Nov-2009 mlarkin

Visual (and on some machines, audio) feedback while the machine is
resuming via calls to aml _SST method. This also makes the "moon" LED on
thinkpads to blink during resume and cease when resume is completed.

ok pirofti@


# 1.151 26-Nov-2009 deraadt

Toggle into "cold" when we we disable interrupts, because tsleeping
in the AML interpreter and context switching into userland processes
tends to sometimes pop the ACPI trampoline page out of our address
space. Makes lots more machines work.
ok kettenis mlarkin


# 1.150 24-Nov-2009 jsg

in the resume path evaluate _WAK before dealing with device drivers
like the spec says, not after.

ok mlarkin@


# 1.149 24-Nov-2009 deraadt

abort suspends on SMP systems until that gets written; ok mlarkin


# 1.148 23-Nov-2009 mlarkin

pci should return the result of it's children's suspend/resume calls back
to its parent.

handle suspend failure case and unwind if devices have an activate function
that returns failure.

ok deraadt@, kettenis@


# 1.147 23-Nov-2009 mlarkin

acpi* devices need to call their children on suspend/resume.
ok deraadt@


# 1.146 23-Nov-2009 pirofti

Remove ACPI_SLEEP_ENABLED checks.

This enables by default the suspend/resume paths in the kernel.

Okay deraadt@.


# 1.145 23-Nov-2009 deraadt

knf and spacing that are super annoying


# 1.144 23-Nov-2009 mlarkin

Hook up Theo's new children suspend/resume function, as well as more ACPI
suspend/resume glue.

ok deraadt@


# 1.143 26-Oct-2009 deraadt

Do not do strncmp comparisons on dv_xname because longer device names which
look similar could arrive in the future. Instead, compare directly against
dv_cfdata->cf_driver->cd_name
Issue originally spotted by miod


# 1.142 26-Oct-2009 jordan

Verify checksum+address when loading ACPI tables.
Some systems had invalid entries in RSDT/XSDT.
ok marco@


# 1.141 23-Jul-2009 cnst

New aibs(4) driver for ASUSTeK AI Booster (ACPI ATK0110) hardware monitoring,
with sensor state support through limits provided by the ACPI.

Tested on several ASUS motherboards kindly networked by Sam Fourman Jr.

ok deraadt marco jordan


Revision tags: OPENBSD_4_6_BASE
# 1.140 03-Jun-2009 pirofti

Import acpi power resource driver. This is needed for suspend/resume logic.

Okay jordan@ and marco@.


# 1.139 03-Jun-2009 pirofti

Add acpivideo support. This adds brightness support for all laptops
except thinkpads, they will use the acpithinkpad driver. The driver is
also hooked into wsconsole. So brightness can be adjusted via:

$ wsconsctl display.brightness=<percentage>

This is very helpfull on some laptops that have a nasty bios and get two
steps instead of one when pressing the brightness button.

Tested on various dell, fujitsu, acer, samsung and other laptops.

Okay marco@, miod@. Suggestions from kettenis@.
Lots of reviews and help from miod@, thanks!


# 1.138 03-Jun-2009 jordan

Added ejectable bay for IDE cdroms
ok marco@


# 1.137 30-Apr-2009 marco

Oga changed apm ioctls and forgot to grep. Temporary work around by hitting
the sleep button when we get any request. This interface will chnage at
some point.
ok oga


# 1.136 19-Apr-2009 krw

Call acpi_sleep_walk() when powering down just like when entering
any other sleep state. This tells acpi wake devices to not wake up
the box until manual powering up resets their status. Makes my Dell
GX520 stay powered off in response to 'halt -p'.

In snaps for a while.

ok marco@ (several times) deraadt@


# 1.135 17-Apr-2009 pirofti

Add device ranking support for future sleep/wake developments.

okay marco@


# 1.134 11-Mar-2009 jordan

Cleanup.. use aml_evalinteger instead of aml_evalname
ok marco@


# 1.133 10-Mar-2009 jordan

Fix cut/paste error.. call _BFS not _PTS
ok marco@


Revision tags: OPENBSD_4_5_BASE
# 1.132 21-Feb-2009 marco

overzealous cut/paste; found by Vladimir Kirillov <proger@uaoug.org.ua>


# 1.131 20-Feb-2009 marco

Oops didn't mean for Debugger() to remain there.


# 1.130 19-Feb-2009 marco

suspend/resume bits so that we can develop this in tree. This is disabled.
code from mlarkin and me
help from art,toby,jordan and several others
ok jordan, go for it deraadt


# 1.129 10-Feb-2009 jordan

Fix null pointer for shutdown if acpi not enabled
ok marco@


# 1.128 04-Feb-2009 kettenis

Some systems that claim to be ACPI 2.0 apparently don't provide an XSDT
address. Make the code fall back on the RSDT on those systems.

From Joerg Niendorf <p3y80@internode.on.net>. Fixes PR 5986.


# 1.127 17-Jan-2009 krw

A couple of stray malloc/memset -> malloc(M_ZERO), a bit of knf and
lose a couple of unnecessary casts. No functional change.

Feedback from kettenis@

ok kettenis@ marco@


# 1.126 06-Nov-2008 marco

Collapse all sanity checks of hids into one function.

Originaly from jordan


Revision tags: OPENBSD_4_4_BASE
# 1.125 02-Jul-2008 fgsch

add acpivideo. for now almost an empty skel so others can work on
suspend/resume. eventually it will also manage output switching and
brightness where supported. prodded by marco@


# 1.124 11-Jun-2008 marco

Fix $OpenBSD$ for once and for all


# 1.123 08-Jun-2008 deraadt

aaa_dev was being set from an uninitialized variable; ok toby


# 1.122 07-Jun-2008 marco

Kill ACPIVERBOSE. It was never used and is worthless to boot.

comments jmc
ok toby


# 1.121 06-Jun-2008 marco

Shrink acpi some more for floppies and while here re-enable acpi on the
floppies. Fixes crazy sony vaio kettenis has.

tested by deraadt
ok deraadt jordan kettenis


# 1.120 01-Jun-2008 marco

Fix \n nit in SMALL_KERNEL.


# 1.119 01-Jun-2008 marco

Re-enable new parser. Lots of testing by various folks. Fits all media
now too.

ok kettenis deraadt


# 1.118 16-May-2008 dlg

back out jordans new parser.

it didnt have enough oks from the right people, it did break machines
that were previously working, and it bloated the RAMDISKS and install
media beyond the constraints of the media.

requested by deraadt@


# 1.117 14-May-2008 jordan

Adding new ACPI Parser code
Old guts of code still exists, needs to be torn out next
ok marco@
ok brad@


# 1.116 27-Apr-2008 jcs

add acpithinkpad(4), an acpi support driver for ibm/lenovo
thinkpads.

provides functionality that the bios did on older, non-acpi
thinkpads like respond to screen brightness keys and toggle the
bluetooth device on and off.

ok deraadt@ and marco@


# 1.115 24-Apr-2008 jsing

Introduce acpiasus(4), a driver for the ACPI based hotkeys found in many
ASUS laptops (including the ASUS EeePC) - largely based on NetBSD's
asus(4) driver. On the ASUS EeePC this allows us to enable/disable
wireless, change screen brightness and use the volume keys.

ok jsg@, weingart@


# 1.114 12-Apr-2008 kettenis

Get rid of acpi_s5 global variables; simply send SIGUSR2 instead of SUGUSR1
to tell init(8) to power down the machine.

ok krw@


Revision tags: OPENBSD_4_3_BASE
# 1.113 21-Jan-2008 jsg

Check against RESET_REG_SUP in FADT before doing the acpi reset dance
after all, not doing so appears to break more machines than it fixes.

ok marco@


# 1.112 05-Jan-2008 kettenis

Never attach more acpicpu(4) devices than cpu(4) devices. Always attach
the first acpicpu(4) device on non-MULTIPROCESSOR kernels, regardless of its
ACPI processor ID.

ok mikeb@


# 1.111 05-Dec-2007 deraadt

Hang acpi(4) below bios(4) instead of mainbus(4). This lets us move acpi(4)
before pcibios(4), and then.... if acpi(4) attaches, skip pcibios(4) since
it messes the machines up. Fixes claudio's HP dl320 G5, and almost assuredly
others.
ok kettenis


# 1.110 05-Dec-2007 deraadt

repair acpiopen/acpiclose and friends to handle the /dev/apmctl interface,
so that apmd(8) can actually work correctly against acpi(4), and thus,
even do the fancy -C/-A stuff; also tested by jmc


# 1.109 02-Dec-2007 fgsch

fix the trailing space for real now.


# 1.108 02-Dec-2007 fgsch

avoid trailing spaces on output; marco@ ok.


# 1.107 27-Nov-2007 kettenis

Fix off-by-one that made us attach acpicpu0 and acpicpu1 on GENERIC kernels.

ok marco@


# 1.106 25-Nov-2007 jsg

When available and ACPI is enabled use the ACPI reset method
to reboot. This is required for some quirky sis machines
and likely others to reboot.

Tested by many. 'commit early commit often' deraadt@


# 1.105 19-Nov-2007 deraadt

sigh, amd64 still uses that stupid acpi_enabled variable...


# 1.104 19-Nov-2007 deraadt

softc's come initialized to 0, you know...


# 1.103 19-Nov-2007 deraadt

store revision; from jsg


# 1.102 19-Nov-2007 kettenis

Initialize sc_facs earlier.

ok deraadt@, beck@, canacar@


# 1.101 16-Nov-2007 deraadt

fix the bus_space #define nightmare, so that amd64 and i386 are much more
uniform. as a result shared code like acpi needs less #ifdef's
ok marco kettenis


# 1.100 15-Nov-2007 deraadt

remove the #ifdef ACPI_ENABLE. the EC codepath is either safe enough (and
will be improved), or it is unsafe because AML assumes it must be there
ok almost everyone


# 1.99 15-Nov-2007 mikeb

Make acpicpu(4) attach to the existing CPUs only.

Problem reported by form@. Fix idea by kettenis@.
Lots of help from deraadt@. Tested by deraadt@, form@ and me.

ok deraadt marco


# 1.98 14-Nov-2007 deraadt

spacing


# 1.97 12-Nov-2007 deraadt

correct : vs , in attach-time messages


# 1.96 06-Nov-2007 deraadt

all ACPI_ENABLE kernels need acpi_interrupt()


# 1.95 06-Nov-2007 deraadt

move acpi_read_pmreg() and acpi_write_pmreg() out of SMALL_KERNEL so
that acpi ramdisks can compile (obvious solution)


# 1.94 05-Nov-2007 canacar

Establish the acpi interrupt earlier. Fixes problems with some
GENERIC.MP kernels not getting acpi interrupts. This is a
workaround until the real issue with late establishment is fixed.
ok weingart@, beck@, gwk@


# 1.93 03-Nov-2007 jordan

Added support for displaying wakeup devices
ok beck@,weingart@,gwk@


# 1.92 11-Oct-2007 kettenis

When walking the tree yo find _PRT methods, check whether the device is
actually enabled and functioning. Similar to what we do for _INI.

ok weingart@, beck@


# 1.91 08-Oct-2007 krw

More simple memset(,0,) -> M_ZERO changes. In this batch move to
size(*p) as the first malloc() parameter where p is declared locally
and thus easy to check. Add M_ZERO to gpe_table allocation in acpi.c
even though there is no obvious bzero or memset nearby.


# 1.90 13-Sep-2007 mk

Remove unnecessary memset() (see aml_evalnode(9)) and a bit of KNF.

ok toby


# 1.89 13-Sep-2007 weingart

Implement ACPI 6.5.1 spec tree walk for _STA and _INI.
Fixes mk's laptop. No regressions so far (thank you to the
testers).

ok gwk@, mk@, marco@


Revision tags: OPENBSD_4_2_BASE
# 1.88 17-Apr-2007 mk

Now that acpidock no longer hangs machines during boot (at least
according to the reports I've been getting) it should be safe to enable
it again, so remove the #if 0 stuff around it.

ok marco


# 1.87 11-Apr-2007 jordan

Added changes for C-State
ok marco@


# 1.86 26-Mar-2007 marco

Calculate minutes left more accurately.

From giovanni <qgiovanni@gmail.com>


Revision tags: OPENBSD_4_1_BASE
# 1.85 03-Mar-2007 mk

Stop looking for acpidock until we fix that stuff that breaks all pre-60
series ThinkPads with acpidock(4) enabled.


# 1.84 22-Feb-2007 marco

We were getting no interrupts in .MP. There are 2 fixes; one in
acpi_machdep.c and move the interrupt establishment down after ioapic
has been initialized.

ok jordan


# 1.83 21-Feb-2007 kettenis

Make an acpi-enabled GENERIC.MP work on systems that only support PIC mode.

ok marco@


# 1.82 19-Feb-2007 jordan

Added acpi_map_address function for mapping GAS/IO registers
ok marco@


# 1.81 18-Feb-2007 jordan

Added dynamic perfect hash for opcodes
ok marco@


# 1.80 18-Feb-2007 marco

Shave off another K pre compression.
Shuffle SMALL_KERNEL functions around so that we only have a single #ifndef


# 1.79 17-Feb-2007 marco

Add SMALL_KERNEL throught acpi to make it fit on boot media.
ok deraadt


# 1.78 13-Feb-2007 marco

Add smarter _INI code. This helps with some dissapearing devices.

ok kettenis jordan


# 1.77 31-Jan-2007 gwk

Introduce acpi_hasprocfvs thats "ACPI Has Processor Frequency and Voltage
Scalling" a global flag set by acicpu attach to indicate that ACPI is
handling fvs through the _PSS objects. This will fix disappearing
EST/powernow when booting kernels with acpi enabled, as acpicpu isnt
compiled into GENERIC yet. Also in cases where acpicpu is built in and
legacy EST/powernow work but acpi is missing the _PSS object, the legacy
routines will be used.

tested by dim@, and Nick Nauwelaerts <nick AT nauwelaerts.net>
ok marco@, dim@


# 1.76 22-Jan-2007 mk

Actually look for docking stations by looking for _DCK nodes.

ok marco


# 1.75 02-Jan-2007 marco

On some systems EC must be attached as the first device. Move EC from
generic _HID devices to its own routine to force first attach. Bug found
and debugged by joris@

Code mostly written by joris@


# 1.74 26-Dec-2006 marco

Add polling to devices that require it such as acpibat and acpitz.
Use the same tsleep/wakeup mechanism as the interrupt code to keep it non-
concurrent.


# 1.73 21-Dec-2006 deraadt

even more knf love


# 1.72 21-Dec-2006 marco

Remove some leftover acpiec gpe/interrupt code.


# 1.71 20-Dec-2006 deraadt

oops, name already used


# 1.70 20-Dec-2006 deraadt

rename __acpi_enable_gpe(); ok marco


# 1.69 20-Dec-2006 deraadt

acpi_unmap_pmregs() is not used, ok marco


# 1.68 19-Dec-2006 deraadt

simplify some low-level methods substantially (shrinking code); ok marco jordan


# 1.67 06-Dec-2006 mk

malloc(..., M_NOWAIT) with no NULL check is bad. If allocation fails,
complain, clean up, and bail out of attach.

input and ok gwk


# 1.66 28-Nov-2006 jordan

Fixed clearing arg for acpi_init_pic
ok kettenis@


# 1.65 27-Nov-2006 kettenis

Don't spam dmesg.


# 1.64 27-Nov-2006 jordan

Fixes for acpiprt routing
Post-parse fixup for namerefs
ok kettenis@


# 1.63 25-Nov-2006 marco

Don't handle acpiev events inside the gpe handler since this causes deadlocks.

ok jordan@


# 1.62 15-Nov-2006 kettenis

Hook up acpi apic interrupt routing support to generic acpi code.

ok jordan@


# 1.61 02-Nov-2006 marco

Kill acpiec attach function and add its functionality to the generic _HID
device attach

prodded and ok jordan


# 1.60 02-Nov-2006 marco

Fix comments to reflect reality.


# 1.59 19-Oct-2006 marco

Replace ACPI ioctls with APM ioctls. This makes ACPI talk the same protocol
as APM therefore userspace can now retrieve power information in the same
fashion as all other apm arches (zaurus, macppc, i386 etc).

To use force the use of acpi device like so: apmd -f /dev/acpi

ok jordan@


# 1.58 19-Oct-2006 jordan

Fixed GPE debug levels
Fixed ref counter on temporary bufferfields
DSDT parsing fully working on IBM Thinkpad
ok marco@


# 1.57 15-Oct-2006 jordan

Removed old GPE handler code remnants
ok marco@


# 1.56 12-Oct-2006 jordan

Massive ACPI changes
Fixed ACPI leaks
new ACPI API
ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.55 30-Jun-2006 gwk

KNF, No functional change,
"go at it" marco@


# 1.54 30-Jun-2006 jordan

Support for calling Embedded Controller via new GPE interface
ok marco@


# 1.53 30-Jun-2006 jordan

Added new code for GPE handling
Also works with ACPIEC; need to fixup acpiec.c to remove original interrupt handler
ok marco@


# 1.52 30-Jun-2006 jordan

Added offset argument for acpi read/write pmreg routines (new GPE code)
ok marco@


# 1.51 31-May-2006 todd

do not call acpi_powerdown() unless acpi has successfully initialized
ok gwk@ canacar@


# 1.50 29-May-2006 canacar

Add embedded controller support.
ok marco@ deraadt@ testing & ok gwk@


# 1.49 19-May-2006 canacar

Minimal thermal zone driver. Monitors thermal zone temperature,
shuts down the system if the 'critical temperature' is reached.
ok marco@


# 1.48 11-Apr-2006 marco

KNF.


# 1.47 11-Apr-2006 gwk

Add acpi_resume(struct acpi_softc *sc), which calls some AML methods,
detected at attach time, currently unused.

ok marco@


# 1.46 11-Apr-2006 gwk

Discover some ACPI sleep related methods, and call them at the
appropriate time in acpi_enter_sleep_state().

ok marco@


Revision tags: OPENBSD_3_9_BASE
# 1.45 26-Feb-2006 marco

Add acpicpu as a device.

ok jordan@


# 1.44 23-Feb-2006 marco

remove dead function.

ok jordan@


# 1.43 22-Feb-2006 marco

Print out all tables it found during autoconf. We'll need this data in
the future.


# 1.42 22-Feb-2006 marco

Make dmesg less verbose.

ok jordan.


# 1.41 22-Feb-2006 jordan

Added parsing of SSDT tables
ok marco@


# 1.40 22-Feb-2006 jordan

Added new aml_register to notify by device id
Moved powerdown code to button device
ok marco@


# 1.39 21-Feb-2006 marco

Readjust debug levels that crept in too low over the last few days.
KNF


# 1.38 21-Feb-2006 marco

Rig the power button so that the machine shuts down correctly.


# 1.37 20-Feb-2006 marco

Add acpibtn device. Remains disabled for now.


# 1.36 19-Feb-2006 jordan

Added code for aml_notify GPE notification callback
ok marco@


# 1.35 19-Feb-2006 grange

Fix acpi_enter_sleep_state() by replacing current ugly hack
with mostly correct sleeping state transition code.

ok brad@ marco@


# 1.34 19-Feb-2006 marco

jordan:
Enable interrupts.
Added peripheral code to make this work.

marco:
Replace softintr and other types of interrupts with a handler with context.
Fixed interrupt magic for GPEs.
Some KNF

This is work done by jordan and I but has to be a single commit.


# 1.33 17-Feb-2006 marco

Remove earlier introduced delays. We really only need them during autconf.

ok jordan@


# 1.32 17-Feb-2006 marco

Add a generic delay function as per the ACPI spec.

from jordan@


# 1.31 17-Feb-2006 marco

Use tsleep instead of delay when !cold.


# 1.30 17-Feb-2006 marco

Argh, we need delays between reads/writes. The backend reads (i2c most likely)
are so slow that values come back as 0 sometimes. We have to do this
despite the spec saying we shouldnt have to. Guess having a faster
parser than others can hurt sometimes.

Idea from jordan.


# 1.29 17-Feb-2006 jordan

Fixed code for handling _INI
Rearranged debug statements
ok marco@


# 1.28 16-Feb-2006 jordan

Added stub for calling _INI
Fixed aml_dowhile for NULL return
ok marco@


# 1.27 16-Feb-2006 jordan

Fixed parser/evaluator to retrieve battery info+state
Cleanup of parser
Dell laptops return battery status
ok marco@


# 1.26 06-Feb-2006 brad

fix a typo so this will compile on amd64.


# 1.25 04-Feb-2006 marco

When ACPI_ENABLED wasn't set but the devices were it had an unknown variable.

Pointed out by ian@.


# 1.24 03-Feb-2006 jordan

Rewrote aml parser yet again...
Fixed bus_space_map for i386/amd64
Added _BST and _BIF to acpibat to get status/info
ok marco@


# 1.23 20-Jan-2006 jordan

More evaluator changes
Removed old evaluator code
Fixed _HID value
ok marco@


# 1.22 19-Jan-2006 jordan

Added support for read/write of PCI Config space
Help jason@
ok marco@


# 1.21 18-Jan-2006 jordan

Added new evaluation routines
Added helper methods for buffer/field evaluation
Fixed broken indentation on dsdt.c

ok marco@


# 1.20 17-Jan-2006 jordan

Major overhaul of the aml parser. Evaluater temporarily disabled.

ok marco@


# 1.19 06-Jan-2006 grange

Check for ACPI control availability only if ACPI_ENABLE defined.


# 1.18 06-Jan-2006 grange

Informative error messages on attach, prettier dmesg.


# 1.17 06-Jan-2006 grange

printf -> dnprintf conversion for debug messages.


# 1.16 05-Jan-2006 grange

Find SLP_TYPx values and store them for future use in
acpi_enter_sleep_state().


# 1.15 05-Jan-2006 grange

Random KNF and cleanup


# 1.14 05-Jan-2006 grange

Provide symbolic names for the acpi sleeping states and use it.


# 1.13 28-Dec-2005 marco

Moved DEVNAME to a acpidev.h

ok jordan@


# 1.12 18-Dec-2005 sturm

add back comment about wrong code

ok marco


# 1.11 16-Dec-2005 marco

Remove test for kbd_reset. Prompted deraadt@, ok jordan@


# 1.10 16-Dec-2005 jordan

Added hooks for powerdown/power button press


# 1.9 16-Dec-2005 jordan

Cleaned up common power management register i/o
Added enter sleep state ioctl
Hooked power button interrupt
ok marco@


# 1.8 16-Dec-2005 marco

Remove autoconf chatter


# 1.7 16-Dec-2005 jordan

Moved DSDT AML parsing into parent ACPI driver
ok marco@


# 1.6 14-Dec-2005 marco

Add AC device glue.


# 1.5 14-Dec-2005 marco

Adjust debug printing levels to something more sane.


# 1.4 13-Dec-2005 marco

Add glue to attach devices to apci.


# 1.3 07-Dec-2005 marco

KNF


Revision tags: OPENBSD_3_8_BASE
# 1.2 10-Jul-2005 grange

Spacing cleanup.


# 1.1 02-Jun-2005 tholo

Start on a basic ACPI framework -- does not do much more than read out the
ACPI tables into kernel memory and attach ACPI and HPET timers currently.

In order to test this code, enabling the devices in GENERIC as well as
the ACPI_ENABLE option is needed. This code does not do any thermal
control yet, so this should be done with care depending on the platform.

In the tree so more people can contribute to making this more fully
featured.

Ok niklas@ grange@ tedu@


# 1.401 02-Nov-2021 deraadt

knf


# 1.400 30-Oct-2021 deraadt

Change hw.perfpolicy=auto by default, at startup. If the system has AC
power connected (default is yes when no driver differentiates) then default
to 100% performance. On battery, use the existing auto algorithm (which is
admittedly somewhat unrefined).
This change overrides the system/BIOS speed and puts OpenBSD in control.
As this happens very early during boot, besides speedups in all usage usage
patterns, some surprises: unhibernate and sysupgrade times are cut in half.
note: on a few architectures, the setperf fn pointer is changed late, and
thus the auto algorithm stops timeing out. kettenis and i will look for
a solution.
in snaps for more than a week.
ok kettenis


Revision tags: OPENBSD_7_0_BASE
# 1.399 20-Jul-2021 mlarkin

Fix trailing whitespace in a few places.

No code change.


# 1.398 09-May-2021 kettenis

Some x86 machines advertise the "hardware reduced" ACPI feature, advertise
S4 and S5 support, but fail to populate the SLEEP_CONTROL_REG and
SLEEP_STATUS_REG descriptions in the FADT. An example of such a machine
is the ASUS Zenbook 14 UM433DA. Any attempt to powerdown the machine
will result in a kernel crash. It turns out that using the legacy ACPI PM
registers works fine on this machien. So fall back on those registers
if SLEEP_CONTROL_REG and/or SLEEP_STATUS_REG aren't provided.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_9_BASE
# 1.397 15-Mar-2021 patrick

Add acpi_iommu_device_map(), which replaces the DMA tag with one that
is blessed with IOMMU magic, if available. This is mainly for arm64,
since on amd64 and i386 the IOMMU only captures PCIe devices, as far
as I know, which uses the pci_probe_device_hook(). This though is for
non-PCI devices attached through ACPI.

ok kettenis@


# 1.396 07-Mar-2021 patrick

Pass standard DMA tag to acpi(4) table drivers.

ok kettenis@


# 1.395 27-Dec-2020 deraadt

acpi_map_address() cannot be wrapped by SMALL_KERNEL anymore, as
it is used by acpihpet.c


# 1.394 25-Dec-2020 visa

Refactor klist insertion and removal

Rename klist_{insert,remove}() to klist_{insert,remove}_locked().
These functions assume that the caller has locked the klist. The current
state of locking remains intact because the kernel lock is still used
with all klists.

Add new functions klist_insert() and klist_remove() that lock the klist
internally. This allows some code simplification.

OK mpi@


# 1.393 17-Dec-2020 kettenis

Fix some issues with referencing named ACPI nodes from Packages.
These references need to be resolved at runtime rather than when
they're parsed such that they pick up the right values for those nodes
which can be changed when for example _INI methods run.

The current approach is to replace these reference with a string that
names the node in question. The problem with that is that packages
can also contain normal strings. Which means that we need special
code that depends on the context where the package is used.

This diff takes a different approach by retaining a reference when
parsing. Code that uses a package will need to resolve this reference
but there is no ambiguiety anymore.

ok patrick@


# 1.392 05-Dec-2020 kettenis

Add support for the _EVT method to the GPIO support code.

ok patrick@


Revision tags: OPENBSD_6_8_BASE
# 1.391 27-Aug-2020 jmatthew

Reorder the acpi attach process so we look at Processor() CPU nodes
after Device() ones, since we should prefer the newer node type.
If we see any Device() nodes, don't attach acpicpu(4) to any Processor()
nodes if they're also present. This also makes acpitz(4) and
acpipwrres(4) devices attach slightly later.

ok kettenis@, also tested by jmc@


# 1.390 16-Aug-2020 gnezdo

Remove sc_maxgpe from acpi_softc which was never read from

Remove a dead conditional from acpi_init_gpes while there.

ok jcs@


# 1.389 21-Jul-2020 deraadt

acpi can use IPL_BIO (a low interrupt) since it only enqueues operations for
later processing. The use of a high interrupt will predate suspend/resume
efforts, we had to redesign acpi to be non-reentrant obviously
discussed with kettenis, in snaps for more than a week


# 1.388 24-Jun-2020 cheloha

kernel: use gettime(9)/getuptime(9) in lieu of time_second(9)/time_uptime(9)

time_second(9) and time_uptime(9) are widely used in the kernel to
quickly get the system UTC or system uptime as a time_t. However,
time_t is 64-bit everywhere, so it is not generally safe to use them
on 32-bit platforms: you have a split-read problem if your hardware
cannot perform atomic 64-bit reads.

This patch replaces time_second(9) with gettime(9), a safer successor
interface, throughout the kernel. Similarly, time_uptime(9) is replaced
with getuptime(9).

There is a performance cost on 32-bit platforms in exchange for
eliminating the split-read problem: instead of two register reads you
now have a lockless read loop to pull the values from the timehands.
This is really not *too* bad in the grand scheme of things, but
compared to what we were doing before it is several times slower.

There is no performance cost on 64-bit (__LP64__) platforms.

With input from visa@, dlg@, and tedu@.

Several bugs squashed by visa@.

ok kettenis@


# 1.387 10-Jun-2020 jca

Give userland a consistent battery and A/C state after resume

Previously we refreshed the state of acpiac(4) and acpibat(4)/acpisbs(4)
by queueing fake ACPI events to be processed by the acpi thread.
acpibat(4) using ACPIDEV_POLL meant that its status could be refreshed
first, resulting in an APM_POWER_CHANGE event sent to userland before
the state of acpiac(4) was up to date. Because of this, apmd(8) could
see A/C unplugged when it was in fact plugged, possibly leading to
a wrong decision.

Instead of using fake ACPI events, refresh the status of acpiac(4) and
acpibat(4)/acpisbs(4) right away at DVACT_WAKEUP time. To reuse the
existing refresh code of those drivers, make their ACPI *_notify()
handlers responsible for sending APM_POWER_CHANGE events instead. Also
queue an APM_POWER_CHANGE event right after resume to retain existing
behavior.

This fixes the consistency of data shown to userland, and makes those
drivers less special.

Initially discussed last year (!) with mpi@ at Miod's place.
Feedback and ok jcs@, deraadt@ agrees with the use of DVACT_WAKEUP here.


# 1.386 29-May-2020 deraadt

dev/rndvar.h no longer has statistical interfaces (removed during various
conversion steps). it only contains kernel prototypes for 4 interfaces,
all of which legitimately belong in sys/systm.h, which are already included
by all enqueue_randomness() users.


# 1.385 14-May-2020 kettenis

Bring back the acpi(4) changes. This time around only use the ACPI _CRS
information on ACPI 5.0 and later.

ok krw@, patrick@


# 1.384 11-May-2020 jca

More accurate remaining power reporting with disparate batteries

Overall remaining power was computed as the average of the remaining
power of each battery, in percents. This doesn't work with batteries of
different capacity, so use the total capacity and remaining power
instead.

Reported by lidstah, successful tests by lidstah and benno@, ok kettenis@


# 1.383 08-May-2020 kettenis

Move parsing of _CRS into acpi(4) and pass the parsed address and interrupt
information in struct acpi_attach_args.

ok mpi@


Revision tags: OPENBSD_6_7_BASE
# 1.382 14-Apr-2020 kettenis

Implement acpi_getprop(), the ACPI equivalent of OF_getprop().

ok patrick@


# 1.381 12-Apr-2020 kettenis

Add support for the _CCA method. This method indicates whether DMA is
cache-coherent or not. To implement this, acpi(4) gets two bus_dma tags
and passes the appropriate one when attaching devices based on _CCA.
On i386/amd64, where for all practical purpose DMA is always cache-coherent,
the two tags are the same. But on arm64 they are distinct.

ok patrick@


# 1.380 07-Apr-2020 visa

Abstract the head of knote lists. This allows extending the lists,
for example, with locking assertions.

OK mpi@, anton@


# 1.379 03-Apr-2020 mpi

Fix incoherencies of errors returned by various *kqfilter routines.

EOPNOTSUPP is returned when there's no "filterops" for a given fd.
EINVAL when the requested filter isn't supported by the "filterops".
ENXIO means the underlying device is no longer valid/present.

ok visa@


# 1.378 20-Feb-2020 visa

Replace field f_isfd with field f_flags in struct filterops to allow
adding more filter properties without cluttering the struct.

OK mpi@, anton@


# 1.377 31-Dec-2019 visa

Use C99 designated initializers with struct filterops. In addition,
make the structs const so that the data are put in .rodata.

OK mpi@, deraadt@, anton@, bluhm@


# 1.376 31-Dec-2019 mpi

Convert infinite sleeps to {m,t}sleep_nsec(9).

ok kn@


# 1.375 31-Dec-2019 jsg

include sys/sensors.h for sensor use
don't depend on indirect inclusion from acpidev.h


Revision tags: OPENBSD_6_6_BASE
# 1.374 07-Sep-2019 kettenis

Revert acpipci(4) changes. Too many systems are broken. We'll try again
after 6.6 as been released. The acpireg.h change stays behind.


# 1.373 28-Aug-2019 kettenis

Use ACPI information to attach PCI busses like we do on arm64. There are a
few additional quirks though, and attaching the PCI busses is delayed to
replicate the existing code more closely. That may be changed in the
future. Also tweak how we handle MSI support and respect to ACPI flag
that says we shouldn't attempt to use MSIs.

Some fallout is expected.

ok patrick@


# 1.372 27-Aug-2019 deraadt

On amd64 the bootup and resume paths for SP/BP/SP were not performing
msr/pat, clock-startup, firmware upgrade, fpu initialization, and cpu
soft-state initialization in the same order. That kind of chaos is
crazy and fragile. Improve the acpi resume call sequence and unify order
of the operations.


# 1.371 02-Jul-2019 jcs

acpi_set_gpehandler: turn edge arg into flags, add support for
passing GPE_DIRECT which directly executes the supplied callback and
clears a level interrupt rather than going through acpi_taskq which
can be slow when other driver callbacks are in the queue.

This should not be used for GPE handlers that need to do anything
ACPI-related.

ok kettenis


# 1.370 10-Jun-2019 kettenis

Print proper ACPI version number.

ok mlarkin@


# 1.369 08-Jun-2019 kettenis

When establishing the mapping between ACPI device nodes and PCI devices,
skip PCI host bridges and devices that aren't there.

ok patrick@


# 1.368 07-Jun-2019 kettenis

Introduce acpi_getsta() and use it to eliminate some duplicated code.

ok patrick@


# 1.367 12-May-2019 kettenis

Make sure the extended PM register descriptions actually exist before
using them. Fixes machines with ancient ACPI 1.0 (such as recent
hypervisors).

ok deraadt@


# 1.366 12-May-2019 kettenis

Add support for mapping ACPI PM registers using the "extended" gasio
representation from the FADT. Mostly fixes the Lanner NCA-1510.

ok lteo@, mlarkin@, deraadt@


# 1.365 11-May-2019 lteo

Fix incorrect error message.

ok kettenis@


# 1.364 08-May-2019 tedu

free size. the allocation is the header plus the length.


Revision tags: OPENBSD_6_5_BASE
# 1.363 04-Apr-2019 kettenis

Avoid potential null-pointer dereference. Found by Kent R. Spillner.

ok deraadt@, jsg@


# 1.362 20-Jan-2019 jsg

fix SMALL_KERNEL build after changes in rev 1.361
reported by naddy@ ok deraadt@


# 1.361 19-Jan-2019 tedu

Add a pwraction sysctl that controls what the power button does on acpi.
By default, nothing changes -- shutdown is initiated. But allows turning
power button into a sleep button if desired.
(grudging) ok from a few parties


# 1.360 26-Oct-2018 kettenis

Add acpipci(4) on amd64. For now this only calls the PCI-specific _OSC
method to let the ACPI implementation know what features we support.


Revision tags: OPENBSD_6_4_BASE
# 1.359 30-Aug-2018 kettenis

Move softbutton check before the check that skips attaching certain acpi
devices. Fixes regression caused by matching on _CID in addition to
matching on _HID.

ok matthieu@, mlarkin@


# 1.358 25-Aug-2018 kettenis

Match ACPI devices based on _CID if no match for _HID is found.

ok mpi@


# 1.357 19-Aug-2018 kettenis

Add support for multiple PCI segments. Only really implemented for arm64
for now as amd64/i386 firmware still caters for legacy OSes that only
support a single PCI segment.

ok patrick@


# 1.356 03-Aug-2018 kettenis

Let ahci(4) match on _CLS instead of _HID when attaching at acpi(4). Avoids
having to add many more _HID entries to the match table.

ok deraadt@, mlarkin@


# 1.355 10-Jul-2018 kettenis

Make legacy interrupts work in acpipci(4).

ok patrick@


# 1.354 05-Jul-2018 kettenis

Add acpipci(4), a driver that supports generic ECAM-compatible PCI host
bridges based on information provided by ACPI.

ok mlarkin@


# 1.353 04-Jul-2018 kettenis

Properly pass around the PCI "chipset tag" in acpi(4) and refactor
acpimcfg(4) to call an MD initialization functions that sets up a tag for
PCI ECAM.

ok guenther@, mlarkin@, krw@


# 1.352 01-Jul-2018 kettenis

Move acpi(4) attach glue into acpi_machdep.c.

ok guenther@, deraadt@


# 1.351 01-Jul-2018 kettenis

Add support for _DSD Device Properties.

ok mlarkin@


# 1.350 30-Jun-2018 kettenis

Add intr_enable() function, intended for MI use to amd64 and i386 and use
this in the acpi(4) suspend/resume code paths.

ok deraadt@


# 1.349 29-Jun-2018 kettenis

Use <stdint.h> types.

ok mpi@, jung@, krw@, deraadt@


# 1.348 29-Jun-2018 mlarkin

ACPI: Allow (with warning) GPE handler reassignment, instead of returning
a failure code.

ok kettenis


# 1.347 26-Jun-2018 mlarkin

knf


# 1.346 26-Jun-2018 kettenis

Let acpi(4) pass down the bus dma tag.

ok deraadt@


# 1.345 25-Jun-2018 kettenis

Reorganize acpi(4) code a bit in preparation for upcoming arm64 support.

ok deraadt@


# 1.344 20-May-2018 kettenis

Wake up the acpi thread after scheduling a gpio event.

ok mpi@


# 1.343 17-May-2018 kettenis

Sprinkle some #ifndef SMALL_KERNEL such that RAMDISK kernels cuild again.

pointed out by stsp@


# 1.342 17-May-2018 kettenis

Implement GenericSerialBus OpRegion support.

ok mlarkin@


# 1.341 27-Mar-2018 kettenis

Add acpicmos(4), a driver that implements SystemCMOS OperationRegion
access support. This fixes machines where the AML doesn't check whether
support for this OperationRegion type has been registered by the OS.

ok mlarkin@


Revision tags: OPENBSD_6_3_BASE
# 1.340 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.339 10-Feb-2018 deraadt

Syncronize filesystems to disk when suspending. Each mountpoint's vnodes
are pushed to disk. Dangling vnodes (unlinked files still in use) and
vnodes undergoing change by long-running syscalls are identified -- and
such filesystems are marked dirty on-disk while we are suspended (in case
power is lost, a fsck will be required). Filesystems without dangling or
busy vnodes are marked clean, resulting in faster boots following
"battery died" circumstances.
Tested by numerous developers, thanks for the feedback.


# 1.338 08-Feb-2018 deraadt

After flushing disks, try again to release all memory. We don't need to
save dirty memory to the hibernate space.


# 1.337 08-Feb-2018 deraadt

Toss all releaseable memory, because fragmentation can get in the way
of allocating the hibernate playpen.


# 1.336 08-Feb-2018 deraadt

sr_quiesce() is a new approach for ensuring that softraid drains
output to the disks.

This is part of a larger suspend/resume filesystem-safety diff, which
has been worked on for a couple of months already. Tests by job, krw,
beck, benno, and others. Sometimes even by snapshot users...


# 1.335 29-Nov-2017 kettenis

Add support for GPIO-signaled events.

ok mlarkin@


# 1.334 16-Nov-2017 jcs

Add PCI attachment for dwiic(4) needed by Intel 100 Series machines

ok kettenis


# 1.333 14-Nov-2017 kettenis

Add a few more PNP IDs to the skip lists.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.332 17-Aug-2017 stsp

Make dwiic(4) attach its ACPI parent dependencies.
This change makes the Asus E200HA keyboard work.

Original analysis and patch by Cesare Gargano
ok kettenis@


# 1.331 09-Aug-2017 dcoppa

Add "PNP0303" (8042 PS/2 Controller) to acpi_isa_hids[]

ok kettenis@


# 1.330 22-Jul-2017 jcs

acpibat: look for _BIX first before falling back on _BIF

ACPI 4.0 deprecated _BIF for battery status so some newer machines
are exporting _BIX which extends _BIF's package with a few extra
fields. When using _BIX, export the new discharge cycle count as a
sensor.

feedback from kettenis and anton
ok deraadt


# 1.329 20-Jul-2017 kettenis

Start a list of isa(4) device names such that we can avoid printing them
as "not configured".

ok mikeb@, millert@, deraadt@


# 1.328 08-Apr-2017 deraadt

Sensors are run as callbacks inside tasks. During suspend a sensor
could be running inside a driver that will be force-detached, or due
to tsleep end up disrupting the softstate/hardstate contract. At
suspend time, quisce all these callbacks by waiting for completion.
This issue has never been observed for real, but may be implicated in
suspend/resume failures.
ok kettenis guenther mlarkin


# 1.327 08-Apr-2017 deraadt

Two prototypes for acpi_maptable; merge to one place


Revision tags: OPENBSD_6_1_BASE
# 1.326 27-Mar-2017 deraadt

Now that hibernate_alloc() only has clean success/failure, don't
need to call hibernate_free() to clean up a partial mess.
ok mlarkin kettenis


# 1.325 13-Mar-2017 deraadt

allow ramdisks to build; ok jcs


# 1.324 12-Mar-2017 jcs

try to attach acpisbs first and if successful, don't attach acpibat


# 1.323 02-Mar-2017 natano

Add a new sysctl machdep.lidaction. The sysctl works as follows:

machdep.lidaction=0 # do nothing
machdep.lidaction=1 # suspend
machdep.lidaction=2 # hibernate

lidsuspend is just an alias for lidaction, so if you change one, the
other one will have the same value. The plan is to remove
machdep.lidsuspend eventually when people have upgraded their
/ets/sysctl.conf.

discussed with deraadt, who came up with the new MIB name
no objections mlarkin
ok stsp halex jcs


# 1.322 28-Feb-2017 natano

Untangle abstract sleep modes from ACPI sleep states. This paves the way
for further work in the area.

idea and ok deraadt
ok mlarkin


# 1.321 22-Feb-2017 jcs

add acpisbs, an acpi smart battery subsystem driver reading data
over smbus

currently disabled because it conflicts with acpibat

ok deraadt, kettenis


# 1.320 14-Jan-2017 kettenis

Revert aml_rdpciaddr changes; breaks several machines that were working
before.


# 1.319 11-Jan-2017 jsg

Increase the size of the buffers used for _HID and _CID strings to be
able to fit "Hyper_V_Gen_Counter_V1".

ok mlarkin@ deraadt@


# 1.318 08-Jan-2017 kettenis

Make aml_rdpciaddr() work for for devices behind bridges. The code now
uses the mapping between AML nodes and PCI devices that we establish.
Because _INIT methods may end up calling aml_rdpciaddr(), make sure we
create that mapping early. Also handle devices that aren't actually present.
These devices are now included in the mapping and reads will return an
all-ones patterm whereas writes are a no-op..

ok mlarkin@


# 1.317 25-Oct-2016 pirofti

Add the resource index to aml_parse_resource() callback function.

This is needed by an upcoming acpiec commit that handles machines breaking
the current ACPI specifications.

Change suggested by and ok kettenis@, guenther@


# 1.316 18-Sep-2016 guenther

If the APIC table (MADT) isn't present then ci_acpi_proc_id won't be set
when acpicpu tries to attach, leading to a NULL dereference. The ACPI
processor object on the Dells where this was reported is bogus and useless
anyway so apply the same test when deciding whether to attach to processor
objects as acpicpu would when matching them to cpus.

Problem reported by Eivind Eide (xenofil (at) gmail.com) and
Johan Huldtgren (johan+openbsd-bugs (at) huldtgren.com).
Testing by Eivind Eide.
ok kettenis@ mlarkin@


# 1.315 03-Sep-2016 naddy

Write the system time back to the RTC every 30 minutes.
This fixes the problem that long-running machines which were not
shut down properly would reboot with a badly offset system time.

hints and ok kettenis@


# 1.314 31-Aug-2016 mlarkin

Don't put a device in the wake device list if _STA says it's not there.

Fixes at least one machine that woke up from sleep / poweroff immediately.

ok kettenis@, tested by kettenis and martijn@


# 1.313 28-Jul-2016 kettenis

Store the acpi processor ID/UID in struct cpu_info, and use it to attach
acpicpu(4) drivers to the right cpu(4).

ok mlarkin@, guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.312 10-Jun-2016 kettenis

The power management timer has been made optional in ACPI 5.0A, so don't
attempt to attach acpitimer(4) if the timer isn't present.

Pointed out by jcs@

ok jcs@


# 1.311 08-May-2016 kettenis

Make sure devices listed by a device's _DEP method are attached before
we attach the device itself. The _DEP methode, introduced in ACPI 5.0,
is a hint that the device in question depends on OpRegion support from
the devices listed by _DEP, so we have to order them the proper way.
To prevent us from attaching those devices again when we encounter them
later walking down the device tree, keep track of our attempts to
attach a device driver to them using aflag in the aml_node structure.

ok guenther@


# 1.310 25-Apr-2016 pirofti

We attach acpihpet(4) and acpiec(4) without using the HID, so do not
report it in the dmesg.

OK deraadt@, mlarkin@.


# 1.309 02-Apr-2016 kettenis

Reduce dmesg spam by knocking out some acpi hardware IDs that don't need a
driver.

ok mpi@


# 1.308 29-Mar-2016 kettenis

Check if a device is present (using _STA) in acpi_foundhid. This prevents
us from reporting devices as "not configured" that aren't actually present,
and allows us to remove duplicated code from several drivers.

ok deraadt@, ok jsg@


# 1.307 29-Mar-2016 kettenis

Implement a cleaner way to prevent acpivout(4) from attaching if we have
a platform-specific method of controlling the backlight.

ok millert@, mlarkin@, jung@


# 1.306 28-Mar-2016 kettenis

Adding each and every new acpi device driver to acpi_foundhid() is
gettinga bit of the burden. And it really isn't how our autoconf
framework is supposed to work for busses that can be enumerated. All
the drivers already check for a matching _HID in their attach
function. So we can just drop the checks in acpi_foundhid() and let
the drivers sort themselves out.

Print a "not configured" line for devices for which we don't attach adriver.
This may be a bit much as there are devices for which we will not have a
driver. This will be removed if it causes too much dmesg spam.

ok deraadt@, mlarkin@


Revision tags: OPENBSD_5_9_BASE
# 1.305 17-Jan-2016 jsg

Take acpi_foundhid() outside of #ifndef SMALL_KERNEL so the ramdisk
kernel can find and attach dwiic(4) and sdhc(4).


# 1.304 17-Jan-2016 jsg

remove an unused global variable


# 1.303 14-Jan-2016 kettenis

Attach dwiic(4) on the Intel Bay Trail i2c controllers as well.


# 1.302 12-Jan-2016 kettenis

Improve emulation of PM registers on Hardware-redcued ACPI by emulating
SPL_TYPx, SLP_EN and WAK_STS using the new SLEEP_STATUS_REG and
SLEEP_CONTROL_REG registers. Makes the transition into S5 (powerdown) work
on the ASUS X205TA.

ok deraadt@, mlarkin@


# 1.301 12-Jan-2016 jcs

Add dwiic, a driver for the Synopsys DesignWare i2c controller found
on the Samsung ATIV Book 9 laptop. This initial version only
supports ACPI config/attachment.

Add ihidev, a HID-over-i2c driver largely based on uhidev. dwiic
handles attaching ihidev devices found in ACPI.

Add ims, a HID-over-i2c mouse/trackpad driver to get basic cursor
and button functionality from HID-compliant i2c trackpads.

ok kettenis deraadt


# 1.300 11-Jan-2016 kettenis

Make sdhc(4) attach to hardware IDs "80860F14" and "PNP0FFF".


# 1.299 09-Jan-2016 kettenis

Initial support for Hardware-Reduced ACPI. This skips the legacy mode checks
by emulating the PM_CNT register such that SCI_EN is always enabled. It also
skip attaching acpitimer(4) since the relevant registers are no longer there.

ok deraadt@, mlarkin@


# 1.298 06-Jan-2016 kettenis

Follow the spec and check the SCI_EN bit to see if ACPI has already
been enabled and don't bother enabling it again if it is. This should fix
systems that don't implement legacy mode and therefore set ACPI_ENABLE and
ACPI_DISABLE to zero.

ok mlarkin@, yasuoka@


# 1.297 23-Nov-2015 reyk

Back out the CRITICAL change, turn it back from 10% to 25%. Some
people with old/broken batteries didn't like this change, and the
underlying heuristics are wrong.


# 1.296 22-Nov-2015 reyk

The "CRITICAL" APM battery level, that is exported/faked by the ACPI
stack, is currently set to 25%. With 25% battery left, a typical
modern laptop will have at least one hour runtime left. It is an
arbitrary value, and other APM power layers in the kernel set CRITICAL
to something like 5% or 10%, so set it to 10% for now.

OK mlarkin@


# 1.295 28-Sep-2015 deraadt

In low-level suspend routines, set cold=2. In tsleep(), use this to
spit out a ddb trace to console. This should allow us to find suspend
or resume routines which break the rules. It depends on the console
output function being non-sleeping.... but that's another codepath which
should try to be safe when cold is set.
ok kettenis


# 1.294 06-Sep-2015 deraadt

clarify malloc arguments (base + length...)


# 1.293 28-Aug-2015 deraadt

fairly simple sizes for free(); ok tedu


# 1.292 20-Aug-2015 kettenis

Respect the access size when reading or writing to pci config space.
This version also makes sure writes are properly aligned, preventing
the panics seen with the previous incarnation of this change.

Fixes battery status passthrough in vmware, brightness keys on my x220
and who knows what else.

ok mlarkin@, deraadt@


Revision tags: OPENBSD_5_8_BASE
# 1.291 04-Aug-2015 deraadt

If we are setting ACPI_PM1_BM_RLD do it only once, and remember to
repeat this action at resume time.
ok mlarkin guenther


# 1.290 29-Jul-2015 deraadt

Something subtle in previous revision is locking up suspend or booting
on some laptops. Mailing lists threads mention x220 and Asus machines.
Reverting for now.


# 1.289 21-Jul-2015 kettenis

iRespect the access size when reading or writing to pci config space.
Fixes battery status passthrough in vmware and who knows what else.

Tested by many.

ok mlarkin@, deraadt@


# 1.288 17-Jul-2015 mlarkin

fix comments


# 1.287 26-Mar-2015 jsg

Use the 0x10 extended capability to determine if the trackstick
buttons are wired to the trackpad and need to be re-routed to the
trackstick. Without this change the buttons on 2015 thinkpads
get picked up as extended buttons that show up as scroll up/down.

Remove the X1 Carbon 2015 (LEN0048) and X250 (LEN0046) from the
top button area/soft buttons quirks list. Also avoid using the
quirk list entirely if the capability bit is set.

Based on work from jcs@ with feedback from mpi@
ok mpi@ jcs@


# 1.286 20-Mar-2015 kettenis

The ACPI standard says that a PCI function number of FFFF can be used to refer
to all functions on a device. This makes no sense in the context of accessing
PCI config space. Nevertheless stsp@ has a machine with AML that tries to
do config space reads from a device where _ADR encodes function number FFFF.
This is almost certainly buggy AML, but since Linux (and presumably Windows)
boot just fine on this machine we need to work around this. Linux effectively
treats function FFFF as function 7, which isn't present on this machine.
Instead we simulate a read from a non-existent device by returning a bit
pattern of all ones. Writes to function FFFF will still panic.

ok mlarkin@


# 1.285 16-Mar-2015 deraadt

Reduce use of SMALL_KERNEL in acpi, bringing acpiec driver and more gpe
handling into RAMDISK. This is now possible because the install media
has ample room. The goal is to reduce special cases where we may be
skipping (unknown) important operations...
ok mlarkin kettenis


# 1.284 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.283 07-Feb-2015 deraadt

New framework that allows hibernate to pass in entropy from it's fresh
boot.
ok mlarkin


# 1.282 06-Feb-2015 mlarkin

Fix a hibernate crash on some machines due to unmapping a page that
may not have been mapped previously (in the failure to hibernate case).

Also ensure that the lowmem ptp is mapped in all cases (not just MP).

ok kettenis


# 1.281 17-Jan-2015 deraadt

resume_randomness() before spinning up other cpus...


# 1.280 15-Jan-2015 jsg

Define a new wscons mouse type for Synaptics clickpad devices that lack
physical buttons. This will be used if the acpi pnpid for the mouse
matches a list provided by Synaptics found in Linux.

Combined with the xenocara changes this will remove the need for an xorg
config file for the x240/t440/t540 generation of thinkpads.

ok matthieu@ shadchin@ kettenis@


# 1.279 11-Jan-2015 kettenis

Tolerate checksum errors in ACPI tables (but complain about them). Apparently
there are machines out there with broken BIOSen with such checksum errors in
thier RSDT tables. Windows runs fine on these machines, so it probably does
not check. Linux ignores checksum errors as well (and prints a warning).

ok deraadt@, krw@


# 1.278 18-Dec-2014 deraadt

suspend and resume the RNG (at what looks like the right spots)
ok reyk djm


# 1.277 09-Dec-2014 doug

Replace some malloc(n*size,...) calls with mallocarray().

ok tedu@ deraadt@


# 1.276 06-Dec-2014 mpi

If "machdep.lidsuspend" is not set, do not go back to sleep if the lid
is still closed when resuming. This allows docked laptop to be resumed
without opening them.

ok mlarkin@


# 1.275 23-Nov-2014 mlarkin

If a machine has no _LIDs, then none can be open. This diff fixes a problem
where machines without any _LIDs were going back to sleep after hibernate
due to the recent suspend-if-lid-closed-on-resume diff. Noticed on qemu,
which doesn't have any _LIDs.

discussed with deraadt


# 1.274 18-Nov-2014 krw

Nuke yet more obvious #include duplications.

ok miod@


# 1.273 08-Nov-2014 mlarkin

If resuming from sleep (zzz/ZZZ) and the lid is still closed, go back to
sleep. This prevents accidental lid flex or slight opening in a backpack
from waking the machine up and leaving it resumed (powered on).

ok deraadt@


# 1.272 26-Sep-2014 kettenis

Rework piglet and pig allocation. Currently the piglet gets allocated
deep down in the suspend path, where it is really hard to recover from
allocation failure. So allocate the piglet early on in the suspend path.
Also change the piglet and piglet allocation functions to use km_alloc(9)
instead of doing pmemrange magic. This removes a bunch of code which, in the
case of the piglet allocation, is broken since it results in a NULL pointer
dereference. Also switch the piglet allocation to not wait. If we can't
allocate 16MB of phys contig memory on a halfway modern machine we're almost
certainly under a lot of memory pressure and we're better off not trying to
hibernate anyway.

ok mlarkin@


# 1.271 25-Sep-2014 kettenis

Bring back rev 1.249. Now that mp_setperf() has been fixed, it should be
safe to restore perflevel when resuming. Should fix the issues people
see with the processor running at full speed heating up the machine while
the apm command reports that the CPUs are running at a low speed.

ok dcoppa@, deraadt@


# 1.270 23-Sep-2014 kettenis

Release the acpi lock around calls to wsdisplay_suspend() and
wsdisplay_resume(). The X server may adjust the display brightness just
before/after the VT switch when suspending/resuming. In fact, it may
already be blocked adjusting the display brightness for some reason
unrelated to the suspend request.

The VT switch request will eventually time out, but the delay is annoying
and the failed VT switch will lead to problems after resume.

Needed for a future update of the xf86-video-intel driver.

ok mlarkin@


# 1.269 19-Sep-2014 kettenis

Use config_suspend_all(9).


# 1.268 08-Sep-2014 kettenis

Clear the PME Status bit on PCI devices in response to Device Wake events.
Fixes the interrupt storm on the 2009 Mac mini with WOL enabled on its
nfe(4) interface.

ok mlarkin@


Revision tags: OPENBSD_5_6_BASE
# 1.267 20-Jul-2014 mlarkin

Support hibernating to softraid crypto volumes.

much help and ok from deraadt@


# 1.266 16-Jul-2014 mlarkin

Reenable hibernate RLE support and flush+zero all memory after unpack.


# 1.265 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.264 12-Jul-2014 kettenis

Add a function to drop all clean pages on the page daemon queues and call
it when we hibernate.

ok mlarkin@, miod@, deraadt@


# 1.263 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.262 11-Jul-2014 mlarkin

Flush the buffercache to 16MB on hibernate and restore its previous max
size (kern.bufcachepercent) on resume, for better hibernate performance.

ok beck@


# 1.261 10-Jul-2014 blambert

remove unneeded header

ok mlarkin@


# 1.260 01-Jul-2014 kettenis

Adding an additonal delay after each access to SystemMemory or SystemIO
during autoconf makes no sense.

ok deraadt@


# 1.259 23-Jun-2014 kettenis

SystemMemory is used to access memory mapped registers on some machines, so we
must use the bus_space(9) API to access it instead of memcpy(9). Also make
sure we properly align access to fields that need more than byte alignment.

ok guenther@


# 1.258 21-May-2014 mlarkin

Fix invalid / missing format specifiers in several dnprintf statements
(used only when ACPI_DEBUG is enabled)

From a diff on tech@ by Fabian Raetz, with a minor change by me, tested
on i386 and amd64 with ACPI_DEBUG enabled and disabled.


# 1.257 25-Apr-2014 mlarkin

Defer the free of the hibernate structures until later, when it is safe to
do so.

Freeing the hibernate structures too early leads to inadvertent calls into
splx when we aren't ready for them yet. Found when trapping splx calls in
the resume path.

discussed with deraadt@ at m2k14


# 1.256 13-Apr-2014 mlarkin

Fix a badly worded debug message that implied the entire machine was going
to sleep, instead of just the acpi thread going to sleep.


# 1.255 31-Mar-2014 mpi

Change the order of operations during a suspend/resume cycle and
call bufq_quiesce() after executing the DVACT_QUIESCE handlers.

This should be safe since no disk nor controller drivers have such
handler but it will allow us to detach sd(4) devices attached to a
USB bus.

Another benefit pointed out by kettenis@ is that drivers that need
to read a firmware from the disk should be able to do it at resume
time in a DVACT_WAKEUP handler.

ok kettenis@, deraadt@


# 1.254 13-Mar-2014 dlg

get rid of the assumption that the head of the alldevs list is the
"mainbus" device. this breaks when mpath is enabled because it
attaches before mainbus and therefore takes the head position.

have autoconf provide device_mainbus() which looks up mainbus_cd,
and use that instead.

discussed with deraadt who just wants mpath stuff to move forward
despite there being many ways to shine this particular turd.


Revision tags: OPENBSD_5_5_BASE
# 1.253 21-Feb-2014 deraadt

Do the lid control further on in the resume sequence, since this is
AML, and will do WAITOK operations. While cold, that is a bad idea.
We seem to be safe with respect to the spec, I do not see any event
debounce issues here.
ok kettenis, tested by many


# 1.252 20-Jan-2014 claudio

Instead of checking sc->sc_revision <= 1 check that the
fadt->hdr_revision <= 1. This is a better check since it checks
the version of the proper descriptor. This allows the alix APU
to reboot since those boards have currently a wrong sc->sc_revision.
Committing this now so that it can be tested easier.
With and OK jsg@ also fine by derradt@


# 1.251 24-Dec-2013 kettenis

If the FADT has its SMI_CMD set to zero, assume we're only ACPI-only hardware
and don't need to disable SMI ownership of the ACPI hardware registers.

ok mlarkin@


# 1.250 23-Dec-2013 kettenis

Revert previous commit. It makes resume hang on some MP systems running
GENERIC.MP.


# 1.249 16-Dec-2013 kettenis

Restore hw.setperf upon resume like we do for apm(4) on i386. Seems we need
to do this fairly late in the resume to avoid hangs.

ok deraadt@, mlarkin@


# 1.248 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


# 1.247 06-Nov-2013 mpi

Add support for Power Resources for Dx states and the necessary hook
for PCI devices. This hook should be called twice, before and after
changing the power state of a PCI device.

Before setting the device to the new state, the ACPI layer will notify
every power resources linked to the device for that state and make sure
they are turned "_ON". After changing the state of the device, it will
decrement the reference of every power resources linked to that device
for the old state and turn them "_OFF" if they are no longer referenced.

This fixes the no-USB after resume problem seen on various ThinkPad,
problem initialy diagnosed with Alexander Polakov.

ok kettenis@, deraadt@


Revision tags: OPENBSD_5_4_BASE
# 1.246 01-Jun-2013 mlarkin

Stop printing ACPI wakeup devices in dmesg after the 16th wakeup device.

Helps with VMware VMs that report hundreds of wakeup devices.

ok reyk@, deraadt@


# 1.245 31-May-2013 bluhm

Fix the build for a kernel without wd and pciide in its config.
OK deraadt@ tedu@


# 1.244 30-May-2013 deraadt

Enforce ca_activate tree-walks over the entire heirarchy for all events,
cleaning up some shutdown-hook related code on the way.
(A few drivers related to sparc are still skipped at kettenis' request)
ok kettenis mlarkin, tested by many others too


# 1.243 18-Apr-2013 deraadt

pin the acpi thread to the BSP, because AML and SMI cannot be trusted. I
was certain we were doing this already, but it turns out no...


Revision tags: OPENBSD_5_3_BASE
# 1.242 24-Dec-2012 guenther

In acpiioctl(), Return though the splx() path if suser() returns an error

ok deraadt@


# 1.241 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


# 1.240 04-Oct-2012 ehrhardt

Use information provided by ACPI to attach secondary PCI host bridges.

ok kettenis@


# 1.239 07-Sep-2012 kettenis

Add a new function called acpi_pci_min_powerstate() that returns the lowest
power state for a PCI device for the power state (currently S3 or S4) we're
transitioning into. This function is probably too simplistic; there is a lot
of (silly) complexity in the ACPI spec.

ok deraadt@


Revision tags: OPENBSD_5_2_BASE
# 1.238 13-Jul-2012 pirofti

Disable acpivideo(4) on ASUS laptops.

There are some models that panic whenever an event is received.
Some cope, some don't. In order to get this right acpiec(4) requires
major rework. Until then let machines boot.

Discussed with deraadt@.


# 1.237 11-Jul-2012 mlarkin

for now, restrict hibernate to root in the acpi ioctl kernel interface.
normal rules for controlling this interface through apmd still apply.

ok deraadt@


# 1.236 09-Jul-2012 deraadt

tiny little oops in untested hibernate block


# 1.235 09-Jul-2012 deraadt

clean up ioctl() case statements


# 1.234 09-Jul-2012 deraadt

acpi_disable_onegpe() is not used; ok pirofti


# 1.233 24-May-2012 kettenis

If _MIN > _MAX consider a WordBus resource to be invalid. Fixes a panic on
the IBM x3550-M4.

ok mikeb@


# 1.232 29-Mar-2012 mlarkin

Missing #ifdef in previous diff


# 1.231 29-Mar-2012 mlarkin

Prevent hibernation attempts on machines with unsupported disk controllers.
The same check is also performed later in the hibernate sequence, but by
then it's too late to cleanly unwind (presently).

ok deraadt


# 1.230 26-Mar-2012 deraadt

simplify conditionals in acpi_sleep_state()
ok mlarkin


# 1.229 26-Mar-2012 deraadt

hook in the hibernate request code; half of this diff is from mlarkin
ok mlarkin


Revision tags: OPENBSD_5_1_BASE
# 1.228 20-Sep-2011 deraadt

Very early in suspend while the clock is still ticking realtime,
save the clock back to the rtc. The zaurus already did this.
ok phessler miod kettenis


Revision tags: OPENBSD_5_0_BASE
# 1.227 02-Jul-2011 nicm

kqueue attach functions should return an errno or 0, not a plain 1. Fix
the obvious cases to return EINVAL and ENXIO.

ok tedu deraadt


# 1.226 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.225 16-Jun-2011 pirofti

Import acpitoshiba driver.

This is originally written by Hiroyuki Aizu, ported and integrated
in our tree by Javier Vazquez <javama4 AT gmail> with tweaks and
suggestions by me.

Import prodded by deraadt@ and the lack of testers on tech@.


# 1.224 27-Apr-2011 jcs

attach acpithinkpad to newer lenovo models like the x120e

ok deraadt@


# 1.223 22-Apr-2011 jordan

Fix uninitialzied variables and formatting strings (-Wxxx errors)


Revision tags: OPENBSD_4_9_BASE
# 1.222 02-Jan-2011 jordan

Re-add code for ACPI taskqueue, eliminates need for state vars in acpi softc
Serialize processing ACPI tasks for sleep/powerdown/GPE event
ok deraadt@


# 1.221 31-Oct-2010 guenther

Revert last commit: it breaks resume on Thinkpads
"then please back it out" deraadt@


# 1.220 26-Oct-2010 jordan

Add task queue for ACPI gpe and notify handlers
This fixes eject on Dell Latitude dock and an issue on the Dell Mini
battery update.
ok deraadt


# 1.219 07-Oct-2010 jordan

Create separate functions for enabling wake and runtime gpes.
Remove old suspend/resume gpewalk function
ok deraadt, mlarkin


# 1.218 05-Oct-2010 jordan

split acpi_enable_onegpe to separate enable/disable functions
ok deraadt


# 1.217 05-Oct-2010 jordan

Added acpi_disable_allgpes function to clear GPE status
ok deraadt


# 1.216 05-Oct-2010 deraadt

clean up acpi_softc use more


# 1.215 05-Oct-2010 deraadt

delete a bit of #if 0 code


# 1.214 05-Oct-2010 deraadt

hide use of our global acpi_softc pointer a bit better


# 1.213 29-Sep-2010 deraadt

grab the acpi thread lock very early in acpi thread startup; not that it
is likely that anyone will race us... but why be careless.
ok kettenis


# 1.212 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.211 29-Aug-2010 jasper

- add a check for FWRITE here too.

ok miod@


# 1.210 27-Aug-2010 kettenis

Make PCI Power Management optional, and only enable it when acpi(4) attaches.

ok deraadt@


Revision tags: OPENBSD_4_8_BASE
# 1.209 08-Aug-2010 kettenis

Close race between the acpi thread and wscons ioctls running in process
context. We might rip this out post-release and replace it with a solution
that serializes things in a more obvious way.

ok deraadt@, miod@ and "those crazy texans who ok anything"


# 1.208 08-Aug-2010 deraadt

Report the acpi sleep states when on a RAMDISK; ok krw


# 1.207 07-Aug-2010 kettenis

Enforce acpi register access rescrictions.

ok mlarkin@, deraadt@, tested by marco@


# 1.206 07-Aug-2010 deraadt

upon resume, notify all the acpiac and acpibat drivers to update
their status. some laptops do not do this automatically.
ok kettenis canacar


# 1.205 06-Aug-2010 marco

Always call _PSW on _LID devices that support it to enable lid open to
resume the machine. Conversely disable it when the machine wakes up.

Tested by several ok deraadt


# 1.204 06-Aug-2010 deraadt

As part of a revamp of the PM1/GPE code, I write this diff to look for
unmanaged EN & STS bits in the PM1 register at interrupt time and report
them. As a side effect this splits the STS acknowledgement into two writes
(for power, and sleep) instead of one. The printf that is added (to spot
unmanaged STS bits) has not yet been triggered as far as we know.

Before the "write to PM1 registers at the right offset" diff went in, this
was not neccessary. But newer thinkpads do not have a working soft power
button without this diff.

We have no idea why.
ok mlarkin kettenis


# 1.203 05-Aug-2010 deraadt

PM1 power button events were generating 'fake events' to acpibtn PWRB,
but if there is no such device these were being tossed. Instead, arrange
for all power button handlers to flag a softc variable which is processed
by the thread; so that one place will know to request a powerdown (psignal
to init)
ok marco mlarkin


# 1.202 05-Aug-2010 deraadt

rename a variable which is annoyingly poorly named


# 1.201 05-Aug-2010 mlarkin

Write to the correct offset for various ACPI pmreg registers. Writing to
the correct register does properly clear the wake status bits and fixed
event bits, which allows some machines to have more than 1 suspend cycle.

bug spotted by deraadt@

tested by deraadt@ and myself on a half-dozen machines and others on
hackers@

ok deraadt@, marco@


# 1.200 05-Aug-2010 deraadt

Fix the power button. The pm1 register is 16 bits in size.
My mistake, a while back.
tested by krw


# 1.199 04-Aug-2010 kettenis

Print supported sleep states.

ok marco@, phessler@, deraadt@


# 1.198 03-Aug-2010 mlarkin

Use the proper flag for re-enabling certain hardware events (power btn,
sleep btn). The (incorrect) flag prevoiously used coincidentally had the
same value, so this shouldn't cause any different behavior than before.

tested on a variety of machines (i386, amd64, sp, mp) - no behavioral
change seen

ok deraadt@


# 1.197 28-Jul-2010 marco

Make all gasio failures look and behave the same.


# 1.196 28-Jul-2010 mlarkin

Delay a little longer on suspend before giving up. Fixes machines that
sometimes don't suspend because the sleep transition takes too long
(symptoms like failing to sleep but the backlight stays on).

ok deraadt@


# 1.195 27-Jul-2010 deraadt

remove XXX; EC can now be attached early when needed; ok jordan


# 1.194 27-Jul-2010 deraadt

more brackets feels good


# 1.193 27-Jul-2010 deraadt

XXX annotate a block in acpi_enter_sleep_state() that is pretty worrying


# 1.192 27-Jul-2010 deraadt

horrific indentation that nearly made me puke


# 1.191 27-Jul-2010 mlarkin

Set the sleeping indicator light on machines that support it, in case SMI
doesn't do it for us. Shows the 'moon' LED on older Thinkpads when
sleeping.

Tested on T43p, W500, T510 by me and on X61s by deraadt@.

ok deraadt@


# 1.190 27-Jul-2010 jordan

Early initialization of acpiec if ECDT table exists
Fixes hang when booting thinkpads while docked
ok deraadt


# 1.189 26-Jul-2010 deraadt

remove merge error which was commited


# 1.188 26-Jul-2010 pirofti

Add support for Sony ACPI hotkeys via a new driver: acpisony(4).

Currently it only works for the suspend button and tries to do right for
the brightness events, but I haven't found a Sony laptop that like to do
right so far.

In the future I want to make the brightness keys work on all Sony's and
also add support for the zoom hotkeys and whatever other funky keys I can
find on those things.

Okay deraadt@.


# 1.187 22-Jul-2010 deraadt

pretty large cleanup of SMALL_KERNEL


# 1.186 21-Jul-2010 deraadt

in the acpi GPE handler, late ack level interrupts not edge interrupts.
Duh.


# 1.185 21-Jul-2010 deraadt

Merge the edge and level gpe handlers into one (now possible since the
gpe structure tells us whether the gpe is edge or level)


# 1.184 21-Jul-2010 deraadt

delete #if 0 code we are not going to use; ok marco


# 1.183 20-Jul-2010 deraadt

make the mask code for acpi_enable_onegpe() match other similar code


# 1.182 20-Jul-2010 deraadt

Handle the acpi interrupt controller with proper edge/level handling,
wrapped in spltty to avoid racing against the interrupt controller.
Repair the gpe bit masking code while there, and do operations in the
right order
ok kettenis mlarkin, help from jordan at figuring out the order of
operations.


# 1.181 20-Jul-2010 matthew

When we're preparing to go to sleep, only call bufq_restart() if other
preparations failed.

ok kettenis@, thib@, deraadt@


# 1.180 19-Jul-2010 deraadt

permit compilation with ACPI_DEBUG


# 1.179 19-Jul-2010 deraadt

there is no need for a seperate dsdt_softc pointer; dsdt does not have a
softc but is just a part of the single acpi driver.
ok jordan


# 1.178 19-Jul-2010 deraadt

acpi_delay() is no longer needed; ok jordan


# 1.177 18-Jul-2010 mlarkin

Clear event status on resume, to avoid going back to sleep/reboot/shutdown
immediately after resume. Tested by a bunch of people on hackers@ on
various hardware. Fixes at least Toshiba NB200 and Gateway LT3103u.

ok marco


# 1.176 13-Jul-2010 deraadt

rename some thread related interfaces so they are logical


# 1.175 10-Jul-2010 jordan

Verify _ADR bdf is correct (some systems return 0xffff)
Works on Dell x9xx and Studio 1555


# 1.174 09-Jul-2010 jordan

Clear the noise


# 1.173 08-Jul-2010 jordan

Add mapping for ACPI device to PCI bdf (match autoconf tree)
Simplify resource parsing function to use buffer argument
Convert namespace linked lists to use queue macros
ok marco@, deraadt@


# 1.172 06-Jul-2010 deraadt

Use spltty() to lock downcalls from apm against the information being
modified by the acpi thread.
ok kettenis


# 1.171 06-Jul-2010 deraadt

Don't use a workq to do the suspend, because it races aginst the acpi
thread. Instead, just tell the acpi thread to do the suspend for us.
This makes apmd & zzz work correctly. While here, have acpithinkpad
attempt to post the event to apm before prompting the suspend itself.
ok kettenis marco mlarkin


# 1.170 05-Jul-2010 mlarkin

Reset register SCI_EN on resume. The ACPI spec clearly states we don't have
to do this, but many machines don't, or forget to, which leads to all sorts
of problems on resume (broken battery stats, overheating, failure to resume
more than once, etc).

tested by many, ok marco@, deraadt@


# 1.169 01-Jul-2010 thib

do bufq quiesce again, since vnd/the bufq_done call in
biodone has been fixed.


# 1.168 01-Jul-2010 jordan

Backout recent AML changes, breaks on R210 and others


# 1.167 01-Jul-2010 jordan

Simplified aml_parse_resource call, cleanup code
ok marco


# 1.166 01-Jul-2010 jordan

Fix subordinate bus # for multi-root PCI buses
ok marco, kettenis


# 1.165 30-Jun-2010 thib

Disable/partially backout the bufq quiesce changes as this
is causing havoc with vnds and release must be buildable.


# 1.164 29-Jun-2010 deraadt

Fix ramdisks (new code was placed inside a SMALL_KERNEL chunk)


# 1.163 29-Jun-2010 jordan

Add support for mapping ACPI to PCI devices
ok kettenis, deraadt


# 1.162 29-Jun-2010 kettenis

Call bufq_quiesce() on suspend to stop all I/O and bufq_restart on resume.
Makes suspend/resume work much better while doing disk I/O.

ok mlarkin@


# 1.161 29-Jun-2010 tedu

60 is a more reasonable guess for number of minutes in an hour than 100.
spotted by TeXitoi. ok deraadt


# 1.160 27-Jun-2010 jordan

Add _acpi_matchhids routine, precursor to pci device match


# 1.159 27-Jun-2010 mlarkin

Clear ACPI fixed event status on resume (power buttons/etc) so that some
machines don't immediately resume after suspending to S3.

Tested by kettenis@ and I on 5 or 6 machines, deraadt@ "yeah, looks good"


# 1.158 27-Jun-2010 jordan

Cleaned up acpi_maptable, adds to table list directly


# 1.157 07-Apr-2010 deraadt

If we have done a wsdisplay_suspend(), and ACPI decides it does not want to
suspend, we must roll back by calling wsdisplay_resume() or the virtual
console switching will be locked forever
ok oga


# 1.156 07-Apr-2010 kettenis

Make suspend/resume work on MP machines (running an MP kernel). Joint work
from mlarkin@ and me, with some amd64 fixes thrown in by deraadt@

ok marco@, deraadt@, pirofti@, mlarkin@


# 1.155 31-Mar-2010 kettenis

Bring /dev/apm support in line with apm(4). Make the suspend button send
a suspend request event to apmd(8) instead of suspending immediately. Also
keep track of whether /dev/apm and /dev/apmctl are currently open, such that
we can still suspend immediately if apmd(8) isn't running.

ok deraadt@, marco@, pirofti@, jsing@, oga@


# 1.154 30-Mar-2010 oga

Prevent the apmd/x races for good.

When we hit suspend time, go through all wsdisplays on the system. if
they are in mode MAPPED, but not MODE_DUMBFB then if possible do a full
vt switch to a !mapped vt, and prevent switching back until resume time.
This has to be called from MD code because this involves userland
running so that X can run the vt switch signal handler. This way, any
case where we are using the "poke registers from userland" model, we
will not be on the hardware when we go down, so the kernel can actually
handle thing properly.

Tested on several acpi laptops (by kettenis@ and ian@), x40 (me and
beck@ at LEAST) and zaurus (me). Maybe others, but if so I forgot who at
this time..

Idea from deraadt somewhere over the Faroe Islands (I thought of a
similar thing myself a while ago). Much prompting from him. Ok and
comments miod@


# 1.153 25-Mar-2010 oga

Fix & vs. && in the kqfilter code.

Based on a comparison with the apm code.

ok deraadt@, kettenis@


Revision tags: OPENBSD_4_7_BASE
# 1.152 26-Nov-2009 mlarkin

Visual (and on some machines, audio) feedback while the machine is
resuming via calls to aml _SST method. This also makes the "moon" LED on
thinkpads to blink during resume and cease when resume is completed.

ok pirofti@


# 1.151 26-Nov-2009 deraadt

Toggle into "cold" when we we disable interrupts, because tsleeping
in the AML interpreter and context switching into userland processes
tends to sometimes pop the ACPI trampoline page out of our address
space. Makes lots more machines work.
ok kettenis mlarkin


# 1.150 24-Nov-2009 jsg

in the resume path evaluate _WAK before dealing with device drivers
like the spec says, not after.

ok mlarkin@


# 1.149 24-Nov-2009 deraadt

abort suspends on SMP systems until that gets written; ok mlarkin


# 1.148 23-Nov-2009 mlarkin

pci should return the result of it's children's suspend/resume calls back
to its parent.

handle suspend failure case and unwind if devices have an activate function
that returns failure.

ok deraadt@, kettenis@


# 1.147 23-Nov-2009 mlarkin

acpi* devices need to call their children on suspend/resume.
ok deraadt@


# 1.146 23-Nov-2009 pirofti

Remove ACPI_SLEEP_ENABLED checks.

This enables by default the suspend/resume paths in the kernel.

Okay deraadt@.


# 1.145 23-Nov-2009 deraadt

knf and spacing that are super annoying


# 1.144 23-Nov-2009 mlarkin

Hook up Theo's new children suspend/resume function, as well as more ACPI
suspend/resume glue.

ok deraadt@


# 1.143 26-Oct-2009 deraadt

Do not do strncmp comparisons on dv_xname because longer device names which
look similar could arrive in the future. Instead, compare directly against
dv_cfdata->cf_driver->cd_name
Issue originally spotted by miod


# 1.142 26-Oct-2009 jordan

Verify checksum+address when loading ACPI tables.
Some systems had invalid entries in RSDT/XSDT.
ok marco@


# 1.141 23-Jul-2009 cnst

New aibs(4) driver for ASUSTeK AI Booster (ACPI ATK0110) hardware monitoring,
with sensor state support through limits provided by the ACPI.

Tested on several ASUS motherboards kindly networked by Sam Fourman Jr.

ok deraadt marco jordan


Revision tags: OPENBSD_4_6_BASE
# 1.140 03-Jun-2009 pirofti

Import acpi power resource driver. This is needed for suspend/resume logic.

Okay jordan@ and marco@.


# 1.139 03-Jun-2009 pirofti

Add acpivideo support. This adds brightness support for all laptops
except thinkpads, they will use the acpithinkpad driver. The driver is
also hooked into wsconsole. So brightness can be adjusted via:

$ wsconsctl display.brightness=<percentage>

This is very helpfull on some laptops that have a nasty bios and get two
steps instead of one when pressing the brightness button.

Tested on various dell, fujitsu, acer, samsung and other laptops.

Okay marco@, miod@. Suggestions from kettenis@.
Lots of reviews and help from miod@, thanks!


# 1.138 03-Jun-2009 jordan

Added ejectable bay for IDE cdroms
ok marco@


# 1.137 30-Apr-2009 marco

Oga changed apm ioctls and forgot to grep. Temporary work around by hitting
the sleep button when we get any request. This interface will chnage at
some point.
ok oga


# 1.136 19-Apr-2009 krw

Call acpi_sleep_walk() when powering down just like when entering
any other sleep state. This tells acpi wake devices to not wake up
the box until manual powering up resets their status. Makes my Dell
GX520 stay powered off in response to 'halt -p'.

In snaps for a while.

ok marco@ (several times) deraadt@


# 1.135 17-Apr-2009 pirofti

Add device ranking support for future sleep/wake developments.

okay marco@


# 1.134 11-Mar-2009 jordan

Cleanup.. use aml_evalinteger instead of aml_evalname
ok marco@


# 1.133 10-Mar-2009 jordan

Fix cut/paste error.. call _BFS not _PTS
ok marco@


Revision tags: OPENBSD_4_5_BASE
# 1.132 21-Feb-2009 marco

overzealous cut/paste; found by Vladimir Kirillov <proger@uaoug.org.ua>


# 1.131 20-Feb-2009 marco

Oops didn't mean for Debugger() to remain there.


# 1.130 19-Feb-2009 marco

suspend/resume bits so that we can develop this in tree. This is disabled.
code from mlarkin and me
help from art,toby,jordan and several others
ok jordan, go for it deraadt


# 1.129 10-Feb-2009 jordan

Fix null pointer for shutdown if acpi not enabled
ok marco@


# 1.128 04-Feb-2009 kettenis

Some systems that claim to be ACPI 2.0 apparently don't provide an XSDT
address. Make the code fall back on the RSDT on those systems.

From Joerg Niendorf <p3y80@internode.on.net>. Fixes PR 5986.


# 1.127 17-Jan-2009 krw

A couple of stray malloc/memset -> malloc(M_ZERO), a bit of knf and
lose a couple of unnecessary casts. No functional change.

Feedback from kettenis@

ok kettenis@ marco@


# 1.126 06-Nov-2008 marco

Collapse all sanity checks of hids into one function.

Originaly from jordan


Revision tags: OPENBSD_4_4_BASE
# 1.125 02-Jul-2008 fgsch

add acpivideo. for now almost an empty skel so others can work on
suspend/resume. eventually it will also manage output switching and
brightness where supported. prodded by marco@


# 1.124 11-Jun-2008 marco

Fix $OpenBSD$ for once and for all


# 1.123 08-Jun-2008 deraadt

aaa_dev was being set from an uninitialized variable; ok toby


# 1.122 07-Jun-2008 marco

Kill ACPIVERBOSE. It was never used and is worthless to boot.

comments jmc
ok toby


# 1.121 06-Jun-2008 marco

Shrink acpi some more for floppies and while here re-enable acpi on the
floppies. Fixes crazy sony vaio kettenis has.

tested by deraadt
ok deraadt jordan kettenis


# 1.120 01-Jun-2008 marco

Fix \n nit in SMALL_KERNEL.


# 1.119 01-Jun-2008 marco

Re-enable new parser. Lots of testing by various folks. Fits all media
now too.

ok kettenis deraadt


# 1.118 16-May-2008 dlg

back out jordans new parser.

it didnt have enough oks from the right people, it did break machines
that were previously working, and it bloated the RAMDISKS and install
media beyond the constraints of the media.

requested by deraadt@


# 1.117 14-May-2008 jordan

Adding new ACPI Parser code
Old guts of code still exists, needs to be torn out next
ok marco@
ok brad@


# 1.116 27-Apr-2008 jcs

add acpithinkpad(4), an acpi support driver for ibm/lenovo
thinkpads.

provides functionality that the bios did on older, non-acpi
thinkpads like respond to screen brightness keys and toggle the
bluetooth device on and off.

ok deraadt@ and marco@


# 1.115 24-Apr-2008 jsing

Introduce acpiasus(4), a driver for the ACPI based hotkeys found in many
ASUS laptops (including the ASUS EeePC) - largely based on NetBSD's
asus(4) driver. On the ASUS EeePC this allows us to enable/disable
wireless, change screen brightness and use the volume keys.

ok jsg@, weingart@


# 1.114 12-Apr-2008 kettenis

Get rid of acpi_s5 global variables; simply send SIGUSR2 instead of SUGUSR1
to tell init(8) to power down the machine.

ok krw@


Revision tags: OPENBSD_4_3_BASE
# 1.113 21-Jan-2008 jsg

Check against RESET_REG_SUP in FADT before doing the acpi reset dance
after all, not doing so appears to break more machines than it fixes.

ok marco@


# 1.112 05-Jan-2008 kettenis

Never attach more acpicpu(4) devices than cpu(4) devices. Always attach
the first acpicpu(4) device on non-MULTIPROCESSOR kernels, regardless of its
ACPI processor ID.

ok mikeb@


# 1.111 05-Dec-2007 deraadt

Hang acpi(4) below bios(4) instead of mainbus(4). This lets us move acpi(4)
before pcibios(4), and then.... if acpi(4) attaches, skip pcibios(4) since
it messes the machines up. Fixes claudio's HP dl320 G5, and almost assuredly
others.
ok kettenis


# 1.110 05-Dec-2007 deraadt

repair acpiopen/acpiclose and friends to handle the /dev/apmctl interface,
so that apmd(8) can actually work correctly against acpi(4), and thus,
even do the fancy -C/-A stuff; also tested by jmc


# 1.109 02-Dec-2007 fgsch

fix the trailing space for real now.


# 1.108 02-Dec-2007 fgsch

avoid trailing spaces on output; marco@ ok.


# 1.107 27-Nov-2007 kettenis

Fix off-by-one that made us attach acpicpu0 and acpicpu1 on GENERIC kernels.

ok marco@


# 1.106 25-Nov-2007 jsg

When available and ACPI is enabled use the ACPI reset method
to reboot. This is required for some quirky sis machines
and likely others to reboot.

Tested by many. 'commit early commit often' deraadt@


# 1.105 19-Nov-2007 deraadt

sigh, amd64 still uses that stupid acpi_enabled variable...


# 1.104 19-Nov-2007 deraadt

softc's come initialized to 0, you know...


# 1.103 19-Nov-2007 deraadt

store revision; from jsg


# 1.102 19-Nov-2007 kettenis

Initialize sc_facs earlier.

ok deraadt@, beck@, canacar@


# 1.101 16-Nov-2007 deraadt

fix the bus_space #define nightmare, so that amd64 and i386 are much more
uniform. as a result shared code like acpi needs less #ifdef's
ok marco kettenis


# 1.100 15-Nov-2007 deraadt

remove the #ifdef ACPI_ENABLE. the EC codepath is either safe enough (and
will be improved), or it is unsafe because AML assumes it must be there
ok almost everyone


# 1.99 15-Nov-2007 mikeb

Make acpicpu(4) attach to the existing CPUs only.

Problem reported by form@. Fix idea by kettenis@.
Lots of help from deraadt@. Tested by deraadt@, form@ and me.

ok deraadt marco


# 1.98 14-Nov-2007 deraadt

spacing


# 1.97 12-Nov-2007 deraadt

correct : vs , in attach-time messages


# 1.96 06-Nov-2007 deraadt

all ACPI_ENABLE kernels need acpi_interrupt()


# 1.95 06-Nov-2007 deraadt

move acpi_read_pmreg() and acpi_write_pmreg() out of SMALL_KERNEL so
that acpi ramdisks can compile (obvious solution)


# 1.94 05-Nov-2007 canacar

Establish the acpi interrupt earlier. Fixes problems with some
GENERIC.MP kernels not getting acpi interrupts. This is a
workaround until the real issue with late establishment is fixed.
ok weingart@, beck@, gwk@


# 1.93 03-Nov-2007 jordan

Added support for displaying wakeup devices
ok beck@,weingart@,gwk@


# 1.92 11-Oct-2007 kettenis

When walking the tree yo find _PRT methods, check whether the device is
actually enabled and functioning. Similar to what we do for _INI.

ok weingart@, beck@


# 1.91 08-Oct-2007 krw

More simple memset(,0,) -> M_ZERO changes. In this batch move to
size(*p) as the first malloc() parameter where p is declared locally
and thus easy to check. Add M_ZERO to gpe_table allocation in acpi.c
even though there is no obvious bzero or memset nearby.


# 1.90 13-Sep-2007 mk

Remove unnecessary memset() (see aml_evalnode(9)) and a bit of KNF.

ok toby


# 1.89 13-Sep-2007 weingart

Implement ACPI 6.5.1 spec tree walk for _STA and _INI.
Fixes mk's laptop. No regressions so far (thank you to the
testers).

ok gwk@, mk@, marco@


Revision tags: OPENBSD_4_2_BASE
# 1.88 17-Apr-2007 mk

Now that acpidock no longer hangs machines during boot (at least
according to the reports I've been getting) it should be safe to enable
it again, so remove the #if 0 stuff around it.

ok marco


# 1.87 11-Apr-2007 jordan

Added changes for C-State
ok marco@


# 1.86 26-Mar-2007 marco

Calculate minutes left more accurately.

From giovanni <qgiovanni@gmail.com>


Revision tags: OPENBSD_4_1_BASE
# 1.85 03-Mar-2007 mk

Stop looking for acpidock until we fix that stuff that breaks all pre-60
series ThinkPads with acpidock(4) enabled.


# 1.84 22-Feb-2007 marco

We were getting no interrupts in .MP. There are 2 fixes; one in
acpi_machdep.c and move the interrupt establishment down after ioapic
has been initialized.

ok jordan


# 1.83 21-Feb-2007 kettenis

Make an acpi-enabled GENERIC.MP work on systems that only support PIC mode.

ok marco@


# 1.82 19-Feb-2007 jordan

Added acpi_map_address function for mapping GAS/IO registers
ok marco@


# 1.81 18-Feb-2007 jordan

Added dynamic perfect hash for opcodes
ok marco@


# 1.80 18-Feb-2007 marco

Shave off another K pre compression.
Shuffle SMALL_KERNEL functions around so that we only have a single #ifndef


# 1.79 17-Feb-2007 marco

Add SMALL_KERNEL throught acpi to make it fit on boot media.
ok deraadt


# 1.78 13-Feb-2007 marco

Add smarter _INI code. This helps with some dissapearing devices.

ok kettenis jordan


# 1.77 31-Jan-2007 gwk

Introduce acpi_hasprocfvs thats "ACPI Has Processor Frequency and Voltage
Scalling" a global flag set by acicpu attach to indicate that ACPI is
handling fvs through the _PSS objects. This will fix disappearing
EST/powernow when booting kernels with acpi enabled, as acpicpu isnt
compiled into GENERIC yet. Also in cases where acpicpu is built in and
legacy EST/powernow work but acpi is missing the _PSS object, the legacy
routines will be used.

tested by dim@, and Nick Nauwelaerts <nick AT nauwelaerts.net>
ok marco@, dim@


# 1.76 22-Jan-2007 mk

Actually look for docking stations by looking for _DCK nodes.

ok marco


# 1.75 02-Jan-2007 marco

On some systems EC must be attached as the first device. Move EC from
generic _HID devices to its own routine to force first attach. Bug found
and debugged by joris@

Code mostly written by joris@


# 1.74 26-Dec-2006 marco

Add polling to devices that require it such as acpibat and acpitz.
Use the same tsleep/wakeup mechanism as the interrupt code to keep it non-
concurrent.


# 1.73 21-Dec-2006 deraadt

even more knf love


# 1.72 21-Dec-2006 marco

Remove some leftover acpiec gpe/interrupt code.


# 1.71 20-Dec-2006 deraadt

oops, name already used


# 1.70 20-Dec-2006 deraadt

rename __acpi_enable_gpe(); ok marco


# 1.69 20-Dec-2006 deraadt

acpi_unmap_pmregs() is not used, ok marco


# 1.68 19-Dec-2006 deraadt

simplify some low-level methods substantially (shrinking code); ok marco jordan


# 1.67 06-Dec-2006 mk

malloc(..., M_NOWAIT) with no NULL check is bad. If allocation fails,
complain, clean up, and bail out of attach.

input and ok gwk


# 1.66 28-Nov-2006 jordan

Fixed clearing arg for acpi_init_pic
ok kettenis@


# 1.65 27-Nov-2006 kettenis

Don't spam dmesg.


# 1.64 27-Nov-2006 jordan

Fixes for acpiprt routing
Post-parse fixup for namerefs
ok kettenis@


# 1.63 25-Nov-2006 marco

Don't handle acpiev events inside the gpe handler since this causes deadlocks.

ok jordan@


# 1.62 15-Nov-2006 kettenis

Hook up acpi apic interrupt routing support to generic acpi code.

ok jordan@


# 1.61 02-Nov-2006 marco

Kill acpiec attach function and add its functionality to the generic _HID
device attach

prodded and ok jordan


# 1.60 02-Nov-2006 marco

Fix comments to reflect reality.


# 1.59 19-Oct-2006 marco

Replace ACPI ioctls with APM ioctls. This makes ACPI talk the same protocol
as APM therefore userspace can now retrieve power information in the same
fashion as all other apm arches (zaurus, macppc, i386 etc).

To use force the use of acpi device like so: apmd -f /dev/acpi

ok jordan@


# 1.58 19-Oct-2006 jordan

Fixed GPE debug levels
Fixed ref counter on temporary bufferfields
DSDT parsing fully working on IBM Thinkpad
ok marco@


# 1.57 15-Oct-2006 jordan

Removed old GPE handler code remnants
ok marco@


# 1.56 12-Oct-2006 jordan

Massive ACPI changes
Fixed ACPI leaks
new ACPI API
ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.55 30-Jun-2006 gwk

KNF, No functional change,
"go at it" marco@


# 1.54 30-Jun-2006 jordan

Support for calling Embedded Controller via new GPE interface
ok marco@


# 1.53 30-Jun-2006 jordan

Added new code for GPE handling
Also works with ACPIEC; need to fixup acpiec.c to remove original interrupt handler
ok marco@


# 1.52 30-Jun-2006 jordan

Added offset argument for acpi read/write pmreg routines (new GPE code)
ok marco@


# 1.51 31-May-2006 todd

do not call acpi_powerdown() unless acpi has successfully initialized
ok gwk@ canacar@


# 1.50 29-May-2006 canacar

Add embedded controller support.
ok marco@ deraadt@ testing & ok gwk@


# 1.49 19-May-2006 canacar

Minimal thermal zone driver. Monitors thermal zone temperature,
shuts down the system if the 'critical temperature' is reached.
ok marco@


# 1.48 11-Apr-2006 marco

KNF.


# 1.47 11-Apr-2006 gwk

Add acpi_resume(struct acpi_softc *sc), which calls some AML methods,
detected at attach time, currently unused.

ok marco@


# 1.46 11-Apr-2006 gwk

Discover some ACPI sleep related methods, and call them at the
appropriate time in acpi_enter_sleep_state().

ok marco@


Revision tags: OPENBSD_3_9_BASE
# 1.45 26-Feb-2006 marco

Add acpicpu as a device.

ok jordan@


# 1.44 23-Feb-2006 marco

remove dead function.

ok jordan@


# 1.43 22-Feb-2006 marco

Print out all tables it found during autoconf. We'll need this data in
the future.


# 1.42 22-Feb-2006 marco

Make dmesg less verbose.

ok jordan.


# 1.41 22-Feb-2006 jordan

Added parsing of SSDT tables
ok marco@


# 1.40 22-Feb-2006 jordan

Added new aml_register to notify by device id
Moved powerdown code to button device
ok marco@


# 1.39 21-Feb-2006 marco

Readjust debug levels that crept in too low over the last few days.
KNF


# 1.38 21-Feb-2006 marco

Rig the power button so that the machine shuts down correctly.


# 1.37 20-Feb-2006 marco

Add acpibtn device. Remains disabled for now.


# 1.36 19-Feb-2006 jordan

Added code for aml_notify GPE notification callback
ok marco@


# 1.35 19-Feb-2006 grange

Fix acpi_enter_sleep_state() by replacing current ugly hack
with mostly correct sleeping state transition code.

ok brad@ marco@


# 1.34 19-Feb-2006 marco

jordan:
Enable interrupts.
Added peripheral code to make this work.

marco:
Replace softintr and other types of interrupts with a handler with context.
Fixed interrupt magic for GPEs.
Some KNF

This is work done by jordan and I but has to be a single commit.


# 1.33 17-Feb-2006 marco

Remove earlier introduced delays. We really only need them during autconf.

ok jordan@


# 1.32 17-Feb-2006 marco

Add a generic delay function as per the ACPI spec.

from jordan@


# 1.31 17-Feb-2006 marco

Use tsleep instead of delay when !cold.


# 1.30 17-Feb-2006 marco

Argh, we need delays between reads/writes. The backend reads (i2c most likely)
are so slow that values come back as 0 sometimes. We have to do this
despite the spec saying we shouldnt have to. Guess having a faster
parser than others can hurt sometimes.

Idea from jordan.


# 1.29 17-Feb-2006 jordan

Fixed code for handling _INI
Rearranged debug statements
ok marco@


# 1.28 16-Feb-2006 jordan

Added stub for calling _INI
Fixed aml_dowhile for NULL return
ok marco@


# 1.27 16-Feb-2006 jordan

Fixed parser/evaluator to retrieve battery info+state
Cleanup of parser
Dell laptops return battery status
ok marco@


# 1.26 06-Feb-2006 brad

fix a typo so this will compile on amd64.


# 1.25 04-Feb-2006 marco

When ACPI_ENABLED wasn't set but the devices were it had an unknown variable.

Pointed out by ian@.


# 1.24 03-Feb-2006 jordan

Rewrote aml parser yet again...
Fixed bus_space_map for i386/amd64
Added _BST and _BIF to acpibat to get status/info
ok marco@


# 1.23 20-Jan-2006 jordan

More evaluator changes
Removed old evaluator code
Fixed _HID value
ok marco@


# 1.22 19-Jan-2006 jordan

Added support for read/write of PCI Config space
Help jason@
ok marco@


# 1.21 18-Jan-2006 jordan

Added new evaluation routines
Added helper methods for buffer/field evaluation
Fixed broken indentation on dsdt.c

ok marco@


# 1.20 17-Jan-2006 jordan

Major overhaul of the aml parser. Evaluater temporarily disabled.

ok marco@


# 1.19 06-Jan-2006 grange

Check for ACPI control availability only if ACPI_ENABLE defined.


# 1.18 06-Jan-2006 grange

Informative error messages on attach, prettier dmesg.


# 1.17 06-Jan-2006 grange

printf -> dnprintf conversion for debug messages.


# 1.16 05-Jan-2006 grange

Find SLP_TYPx values and store them for future use in
acpi_enter_sleep_state().


# 1.15 05-Jan-2006 grange

Random KNF and cleanup


# 1.14 05-Jan-2006 grange

Provide symbolic names for the acpi sleeping states and use it.


# 1.13 28-Dec-2005 marco

Moved DEVNAME to a acpidev.h

ok jordan@


# 1.12 18-Dec-2005 sturm

add back comment about wrong code

ok marco


# 1.11 16-Dec-2005 marco

Remove test for kbd_reset. Prompted deraadt@, ok jordan@


# 1.10 16-Dec-2005 jordan

Added hooks for powerdown/power button press


# 1.9 16-Dec-2005 jordan

Cleaned up common power management register i/o
Added enter sleep state ioctl
Hooked power button interrupt
ok marco@


# 1.8 16-Dec-2005 marco

Remove autoconf chatter


# 1.7 16-Dec-2005 jordan

Moved DSDT AML parsing into parent ACPI driver
ok marco@


# 1.6 14-Dec-2005 marco

Add AC device glue.


# 1.5 14-Dec-2005 marco

Adjust debug printing levels to something more sane.


# 1.4 13-Dec-2005 marco

Add glue to attach devices to apci.


# 1.3 07-Dec-2005 marco

KNF


Revision tags: OPENBSD_3_8_BASE
# 1.2 10-Jul-2005 grange

Spacing cleanup.


# 1.1 02-Jun-2005 tholo

Start on a basic ACPI framework -- does not do much more than read out the
ACPI tables into kernel memory and attach ACPI and HPET timers currently.

In order to test this code, enabling the devices in GENERIC as well as
the ACPI_ENABLE option is needed. This code does not do any thermal
control yet, so this should be done with care depending on the platform.

In the tree so more people can contribute to making this more fully
featured.

Ok niklas@ grange@ tedu@


# 1.400 30-Oct-2021 deraadt

Change hw.perfpolicy=auto by default, at startup. If the system has AC
power connected (default is yes when no driver differentiates) then default
to 100% performance. On battery, use the existing auto algorithm (which is
admittedly somewhat unrefined).
This change overrides the system/BIOS speed and puts OpenBSD in control.
As this happens very early during boot, besides speedups in all usage usage
patterns, some surprises: unhibernate and sysupgrade times are cut in half.
note: on a few architectures, the setperf fn pointer is changed late, and
thus the auto algorithm stops timeing out. kettenis and i will look for
a solution.
in snaps for more than a week.
ok kettenis


Revision tags: OPENBSD_7_0_BASE
# 1.399 20-Jul-2021 mlarkin

Fix trailing whitespace in a few places.

No code change.


# 1.398 09-May-2021 kettenis

Some x86 machines advertise the "hardware reduced" ACPI feature, advertise
S4 and S5 support, but fail to populate the SLEEP_CONTROL_REG and
SLEEP_STATUS_REG descriptions in the FADT. An example of such a machine
is the ASUS Zenbook 14 UM433DA. Any attempt to powerdown the machine
will result in a kernel crash. It turns out that using the legacy ACPI PM
registers works fine on this machien. So fall back on those registers
if SLEEP_CONTROL_REG and/or SLEEP_STATUS_REG aren't provided.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_9_BASE
# 1.397 15-Mar-2021 patrick

Add acpi_iommu_device_map(), which replaces the DMA tag with one that
is blessed with IOMMU magic, if available. This is mainly for arm64,
since on amd64 and i386 the IOMMU only captures PCIe devices, as far
as I know, which uses the pci_probe_device_hook(). This though is for
non-PCI devices attached through ACPI.

ok kettenis@


# 1.396 07-Mar-2021 patrick

Pass standard DMA tag to acpi(4) table drivers.

ok kettenis@


# 1.395 27-Dec-2020 deraadt

acpi_map_address() cannot be wrapped by SMALL_KERNEL anymore, as
it is used by acpihpet.c


# 1.394 25-Dec-2020 visa

Refactor klist insertion and removal

Rename klist_{insert,remove}() to klist_{insert,remove}_locked().
These functions assume that the caller has locked the klist. The current
state of locking remains intact because the kernel lock is still used
with all klists.

Add new functions klist_insert() and klist_remove() that lock the klist
internally. This allows some code simplification.

OK mpi@


# 1.393 17-Dec-2020 kettenis

Fix some issues with referencing named ACPI nodes from Packages.
These references need to be resolved at runtime rather than when
they're parsed such that they pick up the right values for those nodes
which can be changed when for example _INI methods run.

The current approach is to replace these reference with a string that
names the node in question. The problem with that is that packages
can also contain normal strings. Which means that we need special
code that depends on the context where the package is used.

This diff takes a different approach by retaining a reference when
parsing. Code that uses a package will need to resolve this reference
but there is no ambiguiety anymore.

ok patrick@


# 1.392 05-Dec-2020 kettenis

Add support for the _EVT method to the GPIO support code.

ok patrick@


Revision tags: OPENBSD_6_8_BASE
# 1.391 27-Aug-2020 jmatthew

Reorder the acpi attach process so we look at Processor() CPU nodes
after Device() ones, since we should prefer the newer node type.
If we see any Device() nodes, don't attach acpicpu(4) to any Processor()
nodes if they're also present. This also makes acpitz(4) and
acpipwrres(4) devices attach slightly later.

ok kettenis@, also tested by jmc@


# 1.390 16-Aug-2020 gnezdo

Remove sc_maxgpe from acpi_softc which was never read from

Remove a dead conditional from acpi_init_gpes while there.

ok jcs@


# 1.389 21-Jul-2020 deraadt

acpi can use IPL_BIO (a low interrupt) since it only enqueues operations for
later processing. The use of a high interrupt will predate suspend/resume
efforts, we had to redesign acpi to be non-reentrant obviously
discussed with kettenis, in snaps for more than a week


# 1.388 24-Jun-2020 cheloha

kernel: use gettime(9)/getuptime(9) in lieu of time_second(9)/time_uptime(9)

time_second(9) and time_uptime(9) are widely used in the kernel to
quickly get the system UTC or system uptime as a time_t. However,
time_t is 64-bit everywhere, so it is not generally safe to use them
on 32-bit platforms: you have a split-read problem if your hardware
cannot perform atomic 64-bit reads.

This patch replaces time_second(9) with gettime(9), a safer successor
interface, throughout the kernel. Similarly, time_uptime(9) is replaced
with getuptime(9).

There is a performance cost on 32-bit platforms in exchange for
eliminating the split-read problem: instead of two register reads you
now have a lockless read loop to pull the values from the timehands.
This is really not *too* bad in the grand scheme of things, but
compared to what we were doing before it is several times slower.

There is no performance cost on 64-bit (__LP64__) platforms.

With input from visa@, dlg@, and tedu@.

Several bugs squashed by visa@.

ok kettenis@


# 1.387 10-Jun-2020 jca

Give userland a consistent battery and A/C state after resume

Previously we refreshed the state of acpiac(4) and acpibat(4)/acpisbs(4)
by queueing fake ACPI events to be processed by the acpi thread.
acpibat(4) using ACPIDEV_POLL meant that its status could be refreshed
first, resulting in an APM_POWER_CHANGE event sent to userland before
the state of acpiac(4) was up to date. Because of this, apmd(8) could
see A/C unplugged when it was in fact plugged, possibly leading to
a wrong decision.

Instead of using fake ACPI events, refresh the status of acpiac(4) and
acpibat(4)/acpisbs(4) right away at DVACT_WAKEUP time. To reuse the
existing refresh code of those drivers, make their ACPI *_notify()
handlers responsible for sending APM_POWER_CHANGE events instead. Also
queue an APM_POWER_CHANGE event right after resume to retain existing
behavior.

This fixes the consistency of data shown to userland, and makes those
drivers less special.

Initially discussed last year (!) with mpi@ at Miod's place.
Feedback and ok jcs@, deraadt@ agrees with the use of DVACT_WAKEUP here.


# 1.386 29-May-2020 deraadt

dev/rndvar.h no longer has statistical interfaces (removed during various
conversion steps). it only contains kernel prototypes for 4 interfaces,
all of which legitimately belong in sys/systm.h, which are already included
by all enqueue_randomness() users.


# 1.385 14-May-2020 kettenis

Bring back the acpi(4) changes. This time around only use the ACPI _CRS
information on ACPI 5.0 and later.

ok krw@, patrick@


# 1.384 11-May-2020 jca

More accurate remaining power reporting with disparate batteries

Overall remaining power was computed as the average of the remaining
power of each battery, in percents. This doesn't work with batteries of
different capacity, so use the total capacity and remaining power
instead.

Reported by lidstah, successful tests by lidstah and benno@, ok kettenis@


# 1.383 08-May-2020 kettenis

Move parsing of _CRS into acpi(4) and pass the parsed address and interrupt
information in struct acpi_attach_args.

ok mpi@


Revision tags: OPENBSD_6_7_BASE
# 1.382 14-Apr-2020 kettenis

Implement acpi_getprop(), the ACPI equivalent of OF_getprop().

ok patrick@


# 1.381 12-Apr-2020 kettenis

Add support for the _CCA method. This method indicates whether DMA is
cache-coherent or not. To implement this, acpi(4) gets two bus_dma tags
and passes the appropriate one when attaching devices based on _CCA.
On i386/amd64, where for all practical purpose DMA is always cache-coherent,
the two tags are the same. But on arm64 they are distinct.

ok patrick@


# 1.380 07-Apr-2020 visa

Abstract the head of knote lists. This allows extending the lists,
for example, with locking assertions.

OK mpi@, anton@


# 1.379 03-Apr-2020 mpi

Fix incoherencies of errors returned by various *kqfilter routines.

EOPNOTSUPP is returned when there's no "filterops" for a given fd.
EINVAL when the requested filter isn't supported by the "filterops".
ENXIO means the underlying device is no longer valid/present.

ok visa@


# 1.378 20-Feb-2020 visa

Replace field f_isfd with field f_flags in struct filterops to allow
adding more filter properties without cluttering the struct.

OK mpi@, anton@


# 1.377 31-Dec-2019 visa

Use C99 designated initializers with struct filterops. In addition,
make the structs const so that the data are put in .rodata.

OK mpi@, deraadt@, anton@, bluhm@


# 1.376 31-Dec-2019 mpi

Convert infinite sleeps to {m,t}sleep_nsec(9).

ok kn@


# 1.375 31-Dec-2019 jsg

include sys/sensors.h for sensor use
don't depend on indirect inclusion from acpidev.h


Revision tags: OPENBSD_6_6_BASE
# 1.374 07-Sep-2019 kettenis

Revert acpipci(4) changes. Too many systems are broken. We'll try again
after 6.6 as been released. The acpireg.h change stays behind.


# 1.373 28-Aug-2019 kettenis

Use ACPI information to attach PCI busses like we do on arm64. There are a
few additional quirks though, and attaching the PCI busses is delayed to
replicate the existing code more closely. That may be changed in the
future. Also tweak how we handle MSI support and respect to ACPI flag
that says we shouldn't attempt to use MSIs.

Some fallout is expected.

ok patrick@


# 1.372 27-Aug-2019 deraadt

On amd64 the bootup and resume paths for SP/BP/SP were not performing
msr/pat, clock-startup, firmware upgrade, fpu initialization, and cpu
soft-state initialization in the same order. That kind of chaos is
crazy and fragile. Improve the acpi resume call sequence and unify order
of the operations.


# 1.371 02-Jul-2019 jcs

acpi_set_gpehandler: turn edge arg into flags, add support for
passing GPE_DIRECT which directly executes the supplied callback and
clears a level interrupt rather than going through acpi_taskq which
can be slow when other driver callbacks are in the queue.

This should not be used for GPE handlers that need to do anything
ACPI-related.

ok kettenis


# 1.370 10-Jun-2019 kettenis

Print proper ACPI version number.

ok mlarkin@


# 1.369 08-Jun-2019 kettenis

When establishing the mapping between ACPI device nodes and PCI devices,
skip PCI host bridges and devices that aren't there.

ok patrick@


# 1.368 07-Jun-2019 kettenis

Introduce acpi_getsta() and use it to eliminate some duplicated code.

ok patrick@


# 1.367 12-May-2019 kettenis

Make sure the extended PM register descriptions actually exist before
using them. Fixes machines with ancient ACPI 1.0 (such as recent
hypervisors).

ok deraadt@


# 1.366 12-May-2019 kettenis

Add support for mapping ACPI PM registers using the "extended" gasio
representation from the FADT. Mostly fixes the Lanner NCA-1510.

ok lteo@, mlarkin@, deraadt@


# 1.365 11-May-2019 lteo

Fix incorrect error message.

ok kettenis@


# 1.364 08-May-2019 tedu

free size. the allocation is the header plus the length.


Revision tags: OPENBSD_6_5_BASE
# 1.363 04-Apr-2019 kettenis

Avoid potential null-pointer dereference. Found by Kent R. Spillner.

ok deraadt@, jsg@


# 1.362 20-Jan-2019 jsg

fix SMALL_KERNEL build after changes in rev 1.361
reported by naddy@ ok deraadt@


# 1.361 19-Jan-2019 tedu

Add a pwraction sysctl that controls what the power button does on acpi.
By default, nothing changes -- shutdown is initiated. But allows turning
power button into a sleep button if desired.
(grudging) ok from a few parties


# 1.360 26-Oct-2018 kettenis

Add acpipci(4) on amd64. For now this only calls the PCI-specific _OSC
method to let the ACPI implementation know what features we support.


Revision tags: OPENBSD_6_4_BASE
# 1.359 30-Aug-2018 kettenis

Move softbutton check before the check that skips attaching certain acpi
devices. Fixes regression caused by matching on _CID in addition to
matching on _HID.

ok matthieu@, mlarkin@


# 1.358 25-Aug-2018 kettenis

Match ACPI devices based on _CID if no match for _HID is found.

ok mpi@


# 1.357 19-Aug-2018 kettenis

Add support for multiple PCI segments. Only really implemented for arm64
for now as amd64/i386 firmware still caters for legacy OSes that only
support a single PCI segment.

ok patrick@


# 1.356 03-Aug-2018 kettenis

Let ahci(4) match on _CLS instead of _HID when attaching at acpi(4). Avoids
having to add many more _HID entries to the match table.

ok deraadt@, mlarkin@


# 1.355 10-Jul-2018 kettenis

Make legacy interrupts work in acpipci(4).

ok patrick@


# 1.354 05-Jul-2018 kettenis

Add acpipci(4), a driver that supports generic ECAM-compatible PCI host
bridges based on information provided by ACPI.

ok mlarkin@


# 1.353 04-Jul-2018 kettenis

Properly pass around the PCI "chipset tag" in acpi(4) and refactor
acpimcfg(4) to call an MD initialization functions that sets up a tag for
PCI ECAM.

ok guenther@, mlarkin@, krw@


# 1.352 01-Jul-2018 kettenis

Move acpi(4) attach glue into acpi_machdep.c.

ok guenther@, deraadt@


# 1.351 01-Jul-2018 kettenis

Add support for _DSD Device Properties.

ok mlarkin@


# 1.350 30-Jun-2018 kettenis

Add intr_enable() function, intended for MI use to amd64 and i386 and use
this in the acpi(4) suspend/resume code paths.

ok deraadt@


# 1.349 29-Jun-2018 kettenis

Use <stdint.h> types.

ok mpi@, jung@, krw@, deraadt@


# 1.348 29-Jun-2018 mlarkin

ACPI: Allow (with warning) GPE handler reassignment, instead of returning
a failure code.

ok kettenis


# 1.347 26-Jun-2018 mlarkin

knf


# 1.346 26-Jun-2018 kettenis

Let acpi(4) pass down the bus dma tag.

ok deraadt@


# 1.345 25-Jun-2018 kettenis

Reorganize acpi(4) code a bit in preparation for upcoming arm64 support.

ok deraadt@


# 1.344 20-May-2018 kettenis

Wake up the acpi thread after scheduling a gpio event.

ok mpi@


# 1.343 17-May-2018 kettenis

Sprinkle some #ifndef SMALL_KERNEL such that RAMDISK kernels cuild again.

pointed out by stsp@


# 1.342 17-May-2018 kettenis

Implement GenericSerialBus OpRegion support.

ok mlarkin@


# 1.341 27-Mar-2018 kettenis

Add acpicmos(4), a driver that implements SystemCMOS OperationRegion
access support. This fixes machines where the AML doesn't check whether
support for this OperationRegion type has been registered by the OS.

ok mlarkin@


Revision tags: OPENBSD_6_3_BASE
# 1.340 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.339 10-Feb-2018 deraadt

Syncronize filesystems to disk when suspending. Each mountpoint's vnodes
are pushed to disk. Dangling vnodes (unlinked files still in use) and
vnodes undergoing change by long-running syscalls are identified -- and
such filesystems are marked dirty on-disk while we are suspended (in case
power is lost, a fsck will be required). Filesystems without dangling or
busy vnodes are marked clean, resulting in faster boots following
"battery died" circumstances.
Tested by numerous developers, thanks for the feedback.


# 1.338 08-Feb-2018 deraadt

After flushing disks, try again to release all memory. We don't need to
save dirty memory to the hibernate space.


# 1.337 08-Feb-2018 deraadt

Toss all releaseable memory, because fragmentation can get in the way
of allocating the hibernate playpen.


# 1.336 08-Feb-2018 deraadt

sr_quiesce() is a new approach for ensuring that softraid drains
output to the disks.

This is part of a larger suspend/resume filesystem-safety diff, which
has been worked on for a couple of months already. Tests by job, krw,
beck, benno, and others. Sometimes even by snapshot users...


# 1.335 29-Nov-2017 kettenis

Add support for GPIO-signaled events.

ok mlarkin@


# 1.334 16-Nov-2017 jcs

Add PCI attachment for dwiic(4) needed by Intel 100 Series machines

ok kettenis


# 1.333 14-Nov-2017 kettenis

Add a few more PNP IDs to the skip lists.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.332 17-Aug-2017 stsp

Make dwiic(4) attach its ACPI parent dependencies.
This change makes the Asus E200HA keyboard work.

Original analysis and patch by Cesare Gargano
ok kettenis@


# 1.331 09-Aug-2017 dcoppa

Add "PNP0303" (8042 PS/2 Controller) to acpi_isa_hids[]

ok kettenis@


# 1.330 22-Jul-2017 jcs

acpibat: look for _BIX first before falling back on _BIF

ACPI 4.0 deprecated _BIF for battery status so some newer machines
are exporting _BIX which extends _BIF's package with a few extra
fields. When using _BIX, export the new discharge cycle count as a
sensor.

feedback from kettenis and anton
ok deraadt


# 1.329 20-Jul-2017 kettenis

Start a list of isa(4) device names such that we can avoid printing them
as "not configured".

ok mikeb@, millert@, deraadt@


# 1.328 08-Apr-2017 deraadt

Sensors are run as callbacks inside tasks. During suspend a sensor
could be running inside a driver that will be force-detached, or due
to tsleep end up disrupting the softstate/hardstate contract. At
suspend time, quisce all these callbacks by waiting for completion.
This issue has never been observed for real, but may be implicated in
suspend/resume failures.
ok kettenis guenther mlarkin


# 1.327 08-Apr-2017 deraadt

Two prototypes for acpi_maptable; merge to one place


Revision tags: OPENBSD_6_1_BASE
# 1.326 27-Mar-2017 deraadt

Now that hibernate_alloc() only has clean success/failure, don't
need to call hibernate_free() to clean up a partial mess.
ok mlarkin kettenis


# 1.325 13-Mar-2017 deraadt

allow ramdisks to build; ok jcs


# 1.324 12-Mar-2017 jcs

try to attach acpisbs first and if successful, don't attach acpibat


# 1.323 02-Mar-2017 natano

Add a new sysctl machdep.lidaction. The sysctl works as follows:

machdep.lidaction=0 # do nothing
machdep.lidaction=1 # suspend
machdep.lidaction=2 # hibernate

lidsuspend is just an alias for lidaction, so if you change one, the
other one will have the same value. The plan is to remove
machdep.lidsuspend eventually when people have upgraded their
/ets/sysctl.conf.

discussed with deraadt, who came up with the new MIB name
no objections mlarkin
ok stsp halex jcs


# 1.322 28-Feb-2017 natano

Untangle abstract sleep modes from ACPI sleep states. This paves the way
for further work in the area.

idea and ok deraadt
ok mlarkin


# 1.321 22-Feb-2017 jcs

add acpisbs, an acpi smart battery subsystem driver reading data
over smbus

currently disabled because it conflicts with acpibat

ok deraadt, kettenis


# 1.320 14-Jan-2017 kettenis

Revert aml_rdpciaddr changes; breaks several machines that were working
before.


# 1.319 11-Jan-2017 jsg

Increase the size of the buffers used for _HID and _CID strings to be
able to fit "Hyper_V_Gen_Counter_V1".

ok mlarkin@ deraadt@


# 1.318 08-Jan-2017 kettenis

Make aml_rdpciaddr() work for for devices behind bridges. The code now
uses the mapping between AML nodes and PCI devices that we establish.
Because _INIT methods may end up calling aml_rdpciaddr(), make sure we
create that mapping early. Also handle devices that aren't actually present.
These devices are now included in the mapping and reads will return an
all-ones patterm whereas writes are a no-op..

ok mlarkin@


# 1.317 25-Oct-2016 pirofti

Add the resource index to aml_parse_resource() callback function.

This is needed by an upcoming acpiec commit that handles machines breaking
the current ACPI specifications.

Change suggested by and ok kettenis@, guenther@


# 1.316 18-Sep-2016 guenther

If the APIC table (MADT) isn't present then ci_acpi_proc_id won't be set
when acpicpu tries to attach, leading to a NULL dereference. The ACPI
processor object on the Dells where this was reported is bogus and useless
anyway so apply the same test when deciding whether to attach to processor
objects as acpicpu would when matching them to cpus.

Problem reported by Eivind Eide (xenofil (at) gmail.com) and
Johan Huldtgren (johan+openbsd-bugs (at) huldtgren.com).
Testing by Eivind Eide.
ok kettenis@ mlarkin@


# 1.315 03-Sep-2016 naddy

Write the system time back to the RTC every 30 minutes.
This fixes the problem that long-running machines which were not
shut down properly would reboot with a badly offset system time.

hints and ok kettenis@


# 1.314 31-Aug-2016 mlarkin

Don't put a device in the wake device list if _STA says it's not there.

Fixes at least one machine that woke up from sleep / poweroff immediately.

ok kettenis@, tested by kettenis and martijn@


# 1.313 28-Jul-2016 kettenis

Store the acpi processor ID/UID in struct cpu_info, and use it to attach
acpicpu(4) drivers to the right cpu(4).

ok mlarkin@, guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.312 10-Jun-2016 kettenis

The power management timer has been made optional in ACPI 5.0A, so don't
attempt to attach acpitimer(4) if the timer isn't present.

Pointed out by jcs@

ok jcs@


# 1.311 08-May-2016 kettenis

Make sure devices listed by a device's _DEP method are attached before
we attach the device itself. The _DEP methode, introduced in ACPI 5.0,
is a hint that the device in question depends on OpRegion support from
the devices listed by _DEP, so we have to order them the proper way.
To prevent us from attaching those devices again when we encounter them
later walking down the device tree, keep track of our attempts to
attach a device driver to them using aflag in the aml_node structure.

ok guenther@


# 1.310 25-Apr-2016 pirofti

We attach acpihpet(4) and acpiec(4) without using the HID, so do not
report it in the dmesg.

OK deraadt@, mlarkin@.


# 1.309 02-Apr-2016 kettenis

Reduce dmesg spam by knocking out some acpi hardware IDs that don't need a
driver.

ok mpi@


# 1.308 29-Mar-2016 kettenis

Check if a device is present (using _STA) in acpi_foundhid. This prevents
us from reporting devices as "not configured" that aren't actually present,
and allows us to remove duplicated code from several drivers.

ok deraadt@, ok jsg@


# 1.307 29-Mar-2016 kettenis

Implement a cleaner way to prevent acpivout(4) from attaching if we have
a platform-specific method of controlling the backlight.

ok millert@, mlarkin@, jung@


# 1.306 28-Mar-2016 kettenis

Adding each and every new acpi device driver to acpi_foundhid() is
gettinga bit of the burden. And it really isn't how our autoconf
framework is supposed to work for busses that can be enumerated. All
the drivers already check for a matching _HID in their attach
function. So we can just drop the checks in acpi_foundhid() and let
the drivers sort themselves out.

Print a "not configured" line for devices for which we don't attach adriver.
This may be a bit much as there are devices for which we will not have a
driver. This will be removed if it causes too much dmesg spam.

ok deraadt@, mlarkin@


Revision tags: OPENBSD_5_9_BASE
# 1.305 17-Jan-2016 jsg

Take acpi_foundhid() outside of #ifndef SMALL_KERNEL so the ramdisk
kernel can find and attach dwiic(4) and sdhc(4).


# 1.304 17-Jan-2016 jsg

remove an unused global variable


# 1.303 14-Jan-2016 kettenis

Attach dwiic(4) on the Intel Bay Trail i2c controllers as well.


# 1.302 12-Jan-2016 kettenis

Improve emulation of PM registers on Hardware-redcued ACPI by emulating
SPL_TYPx, SLP_EN and WAK_STS using the new SLEEP_STATUS_REG and
SLEEP_CONTROL_REG registers. Makes the transition into S5 (powerdown) work
on the ASUS X205TA.

ok deraadt@, mlarkin@


# 1.301 12-Jan-2016 jcs

Add dwiic, a driver for the Synopsys DesignWare i2c controller found
on the Samsung ATIV Book 9 laptop. This initial version only
supports ACPI config/attachment.

Add ihidev, a HID-over-i2c driver largely based on uhidev. dwiic
handles attaching ihidev devices found in ACPI.

Add ims, a HID-over-i2c mouse/trackpad driver to get basic cursor
and button functionality from HID-compliant i2c trackpads.

ok kettenis deraadt


# 1.300 11-Jan-2016 kettenis

Make sdhc(4) attach to hardware IDs "80860F14" and "PNP0FFF".


# 1.299 09-Jan-2016 kettenis

Initial support for Hardware-Reduced ACPI. This skips the legacy mode checks
by emulating the PM_CNT register such that SCI_EN is always enabled. It also
skip attaching acpitimer(4) since the relevant registers are no longer there.

ok deraadt@, mlarkin@


# 1.298 06-Jan-2016 kettenis

Follow the spec and check the SCI_EN bit to see if ACPI has already
been enabled and don't bother enabling it again if it is. This should fix
systems that don't implement legacy mode and therefore set ACPI_ENABLE and
ACPI_DISABLE to zero.

ok mlarkin@, yasuoka@


# 1.297 23-Nov-2015 reyk

Back out the CRITICAL change, turn it back from 10% to 25%. Some
people with old/broken batteries didn't like this change, and the
underlying heuristics are wrong.


# 1.296 22-Nov-2015 reyk

The "CRITICAL" APM battery level, that is exported/faked by the ACPI
stack, is currently set to 25%. With 25% battery left, a typical
modern laptop will have at least one hour runtime left. It is an
arbitrary value, and other APM power layers in the kernel set CRITICAL
to something like 5% or 10%, so set it to 10% for now.

OK mlarkin@


# 1.295 28-Sep-2015 deraadt

In low-level suspend routines, set cold=2. In tsleep(), use this to
spit out a ddb trace to console. This should allow us to find suspend
or resume routines which break the rules. It depends on the console
output function being non-sleeping.... but that's another codepath which
should try to be safe when cold is set.
ok kettenis


# 1.294 06-Sep-2015 deraadt

clarify malloc arguments (base + length...)


# 1.293 28-Aug-2015 deraadt

fairly simple sizes for free(); ok tedu


# 1.292 20-Aug-2015 kettenis

Respect the access size when reading or writing to pci config space.
This version also makes sure writes are properly aligned, preventing
the panics seen with the previous incarnation of this change.

Fixes battery status passthrough in vmware, brightness keys on my x220
and who knows what else.

ok mlarkin@, deraadt@


Revision tags: OPENBSD_5_8_BASE
# 1.291 04-Aug-2015 deraadt

If we are setting ACPI_PM1_BM_RLD do it only once, and remember to
repeat this action at resume time.
ok mlarkin guenther


# 1.290 29-Jul-2015 deraadt

Something subtle in previous revision is locking up suspend or booting
on some laptops. Mailing lists threads mention x220 and Asus machines.
Reverting for now.


# 1.289 21-Jul-2015 kettenis

iRespect the access size when reading or writing to pci config space.
Fixes battery status passthrough in vmware and who knows what else.

Tested by many.

ok mlarkin@, deraadt@


# 1.288 17-Jul-2015 mlarkin

fix comments


# 1.287 26-Mar-2015 jsg

Use the 0x10 extended capability to determine if the trackstick
buttons are wired to the trackpad and need to be re-routed to the
trackstick. Without this change the buttons on 2015 thinkpads
get picked up as extended buttons that show up as scroll up/down.

Remove the X1 Carbon 2015 (LEN0048) and X250 (LEN0046) from the
top button area/soft buttons quirks list. Also avoid using the
quirk list entirely if the capability bit is set.

Based on work from jcs@ with feedback from mpi@
ok mpi@ jcs@


# 1.286 20-Mar-2015 kettenis

The ACPI standard says that a PCI function number of FFFF can be used to refer
to all functions on a device. This makes no sense in the context of accessing
PCI config space. Nevertheless stsp@ has a machine with AML that tries to
do config space reads from a device where _ADR encodes function number FFFF.
This is almost certainly buggy AML, but since Linux (and presumably Windows)
boot just fine on this machine we need to work around this. Linux effectively
treats function FFFF as function 7, which isn't present on this machine.
Instead we simulate a read from a non-existent device by returning a bit
pattern of all ones. Writes to function FFFF will still panic.

ok mlarkin@


# 1.285 16-Mar-2015 deraadt

Reduce use of SMALL_KERNEL in acpi, bringing acpiec driver and more gpe
handling into RAMDISK. This is now possible because the install media
has ample room. The goal is to reduce special cases where we may be
skipping (unknown) important operations...
ok mlarkin kettenis


# 1.284 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.283 07-Feb-2015 deraadt

New framework that allows hibernate to pass in entropy from it's fresh
boot.
ok mlarkin


# 1.282 06-Feb-2015 mlarkin

Fix a hibernate crash on some machines due to unmapping a page that
may not have been mapped previously (in the failure to hibernate case).

Also ensure that the lowmem ptp is mapped in all cases (not just MP).

ok kettenis


# 1.281 17-Jan-2015 deraadt

resume_randomness() before spinning up other cpus...


# 1.280 15-Jan-2015 jsg

Define a new wscons mouse type for Synaptics clickpad devices that lack
physical buttons. This will be used if the acpi pnpid for the mouse
matches a list provided by Synaptics found in Linux.

Combined with the xenocara changes this will remove the need for an xorg
config file for the x240/t440/t540 generation of thinkpads.

ok matthieu@ shadchin@ kettenis@


# 1.279 11-Jan-2015 kettenis

Tolerate checksum errors in ACPI tables (but complain about them). Apparently
there are machines out there with broken BIOSen with such checksum errors in
thier RSDT tables. Windows runs fine on these machines, so it probably does
not check. Linux ignores checksum errors as well (and prints a warning).

ok deraadt@, krw@


# 1.278 18-Dec-2014 deraadt

suspend and resume the RNG (at what looks like the right spots)
ok reyk djm


# 1.277 09-Dec-2014 doug

Replace some malloc(n*size,...) calls with mallocarray().

ok tedu@ deraadt@


# 1.276 06-Dec-2014 mpi

If "machdep.lidsuspend" is not set, do not go back to sleep if the lid
is still closed when resuming. This allows docked laptop to be resumed
without opening them.

ok mlarkin@


# 1.275 23-Nov-2014 mlarkin

If a machine has no _LIDs, then none can be open. This diff fixes a problem
where machines without any _LIDs were going back to sleep after hibernate
due to the recent suspend-if-lid-closed-on-resume diff. Noticed on qemu,
which doesn't have any _LIDs.

discussed with deraadt


# 1.274 18-Nov-2014 krw

Nuke yet more obvious #include duplications.

ok miod@


# 1.273 08-Nov-2014 mlarkin

If resuming from sleep (zzz/ZZZ) and the lid is still closed, go back to
sleep. This prevents accidental lid flex or slight opening in a backpack
from waking the machine up and leaving it resumed (powered on).

ok deraadt@


# 1.272 26-Sep-2014 kettenis

Rework piglet and pig allocation. Currently the piglet gets allocated
deep down in the suspend path, where it is really hard to recover from
allocation failure. So allocate the piglet early on in the suspend path.
Also change the piglet and piglet allocation functions to use km_alloc(9)
instead of doing pmemrange magic. This removes a bunch of code which, in the
case of the piglet allocation, is broken since it results in a NULL pointer
dereference. Also switch the piglet allocation to not wait. If we can't
allocate 16MB of phys contig memory on a halfway modern machine we're almost
certainly under a lot of memory pressure and we're better off not trying to
hibernate anyway.

ok mlarkin@


# 1.271 25-Sep-2014 kettenis

Bring back rev 1.249. Now that mp_setperf() has been fixed, it should be
safe to restore perflevel when resuming. Should fix the issues people
see with the processor running at full speed heating up the machine while
the apm command reports that the CPUs are running at a low speed.

ok dcoppa@, deraadt@


# 1.270 23-Sep-2014 kettenis

Release the acpi lock around calls to wsdisplay_suspend() and
wsdisplay_resume(). The X server may adjust the display brightness just
before/after the VT switch when suspending/resuming. In fact, it may
already be blocked adjusting the display brightness for some reason
unrelated to the suspend request.

The VT switch request will eventually time out, but the delay is annoying
and the failed VT switch will lead to problems after resume.

Needed for a future update of the xf86-video-intel driver.

ok mlarkin@


# 1.269 19-Sep-2014 kettenis

Use config_suspend_all(9).


# 1.268 08-Sep-2014 kettenis

Clear the PME Status bit on PCI devices in response to Device Wake events.
Fixes the interrupt storm on the 2009 Mac mini with WOL enabled on its
nfe(4) interface.

ok mlarkin@


Revision tags: OPENBSD_5_6_BASE
# 1.267 20-Jul-2014 mlarkin

Support hibernating to softraid crypto volumes.

much help and ok from deraadt@


# 1.266 16-Jul-2014 mlarkin

Reenable hibernate RLE support and flush+zero all memory after unpack.


# 1.265 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.264 12-Jul-2014 kettenis

Add a function to drop all clean pages on the page daemon queues and call
it when we hibernate.

ok mlarkin@, miod@, deraadt@


# 1.263 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.262 11-Jul-2014 mlarkin

Flush the buffercache to 16MB on hibernate and restore its previous max
size (kern.bufcachepercent) on resume, for better hibernate performance.

ok beck@


# 1.261 10-Jul-2014 blambert

remove unneeded header

ok mlarkin@


# 1.260 01-Jul-2014 kettenis

Adding an additonal delay after each access to SystemMemory or SystemIO
during autoconf makes no sense.

ok deraadt@


# 1.259 23-Jun-2014 kettenis

SystemMemory is used to access memory mapped registers on some machines, so we
must use the bus_space(9) API to access it instead of memcpy(9). Also make
sure we properly align access to fields that need more than byte alignment.

ok guenther@


# 1.258 21-May-2014 mlarkin

Fix invalid / missing format specifiers in several dnprintf statements
(used only when ACPI_DEBUG is enabled)

From a diff on tech@ by Fabian Raetz, with a minor change by me, tested
on i386 and amd64 with ACPI_DEBUG enabled and disabled.


# 1.257 25-Apr-2014 mlarkin

Defer the free of the hibernate structures until later, when it is safe to
do so.

Freeing the hibernate structures too early leads to inadvertent calls into
splx when we aren't ready for them yet. Found when trapping splx calls in
the resume path.

discussed with deraadt@ at m2k14


# 1.256 13-Apr-2014 mlarkin

Fix a badly worded debug message that implied the entire machine was going
to sleep, instead of just the acpi thread going to sleep.


# 1.255 31-Mar-2014 mpi

Change the order of operations during a suspend/resume cycle and
call bufq_quiesce() after executing the DVACT_QUIESCE handlers.

This should be safe since no disk nor controller drivers have such
handler but it will allow us to detach sd(4) devices attached to a
USB bus.

Another benefit pointed out by kettenis@ is that drivers that need
to read a firmware from the disk should be able to do it at resume
time in a DVACT_WAKEUP handler.

ok kettenis@, deraadt@


# 1.254 13-Mar-2014 dlg

get rid of the assumption that the head of the alldevs list is the
"mainbus" device. this breaks when mpath is enabled because it
attaches before mainbus and therefore takes the head position.

have autoconf provide device_mainbus() which looks up mainbus_cd,
and use that instead.

discussed with deraadt who just wants mpath stuff to move forward
despite there being many ways to shine this particular turd.


Revision tags: OPENBSD_5_5_BASE
# 1.253 21-Feb-2014 deraadt

Do the lid control further on in the resume sequence, since this is
AML, and will do WAITOK operations. While cold, that is a bad idea.
We seem to be safe with respect to the spec, I do not see any event
debounce issues here.
ok kettenis, tested by many


# 1.252 20-Jan-2014 claudio

Instead of checking sc->sc_revision <= 1 check that the
fadt->hdr_revision <= 1. This is a better check since it checks
the version of the proper descriptor. This allows the alix APU
to reboot since those boards have currently a wrong sc->sc_revision.
Committing this now so that it can be tested easier.
With and OK jsg@ also fine by derradt@


# 1.251 24-Dec-2013 kettenis

If the FADT has its SMI_CMD set to zero, assume we're only ACPI-only hardware
and don't need to disable SMI ownership of the ACPI hardware registers.

ok mlarkin@


# 1.250 23-Dec-2013 kettenis

Revert previous commit. It makes resume hang on some MP systems running
GENERIC.MP.


# 1.249 16-Dec-2013 kettenis

Restore hw.setperf upon resume like we do for apm(4) on i386. Seems we need
to do this fairly late in the resume to avoid hangs.

ok deraadt@, mlarkin@


# 1.248 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


# 1.247 06-Nov-2013 mpi

Add support for Power Resources for Dx states and the necessary hook
for PCI devices. This hook should be called twice, before and after
changing the power state of a PCI device.

Before setting the device to the new state, the ACPI layer will notify
every power resources linked to the device for that state and make sure
they are turned "_ON". After changing the state of the device, it will
decrement the reference of every power resources linked to that device
for the old state and turn them "_OFF" if they are no longer referenced.

This fixes the no-USB after resume problem seen on various ThinkPad,
problem initialy diagnosed with Alexander Polakov.

ok kettenis@, deraadt@


Revision tags: OPENBSD_5_4_BASE
# 1.246 01-Jun-2013 mlarkin

Stop printing ACPI wakeup devices in dmesg after the 16th wakeup device.

Helps with VMware VMs that report hundreds of wakeup devices.

ok reyk@, deraadt@


# 1.245 31-May-2013 bluhm

Fix the build for a kernel without wd and pciide in its config.
OK deraadt@ tedu@


# 1.244 30-May-2013 deraadt

Enforce ca_activate tree-walks over the entire heirarchy for all events,
cleaning up some shutdown-hook related code on the way.
(A few drivers related to sparc are still skipped at kettenis' request)
ok kettenis mlarkin, tested by many others too


# 1.243 18-Apr-2013 deraadt

pin the acpi thread to the BSP, because AML and SMI cannot be trusted. I
was certain we were doing this already, but it turns out no...


Revision tags: OPENBSD_5_3_BASE
# 1.242 24-Dec-2012 guenther

In acpiioctl(), Return though the splx() path if suser() returns an error

ok deraadt@


# 1.241 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


# 1.240 04-Oct-2012 ehrhardt

Use information provided by ACPI to attach secondary PCI host bridges.

ok kettenis@


# 1.239 07-Sep-2012 kettenis

Add a new function called acpi_pci_min_powerstate() that returns the lowest
power state for a PCI device for the power state (currently S3 or S4) we're
transitioning into. This function is probably too simplistic; there is a lot
of (silly) complexity in the ACPI spec.

ok deraadt@


Revision tags: OPENBSD_5_2_BASE
# 1.238 13-Jul-2012 pirofti

Disable acpivideo(4) on ASUS laptops.

There are some models that panic whenever an event is received.
Some cope, some don't. In order to get this right acpiec(4) requires
major rework. Until then let machines boot.

Discussed with deraadt@.


# 1.237 11-Jul-2012 mlarkin

for now, restrict hibernate to root in the acpi ioctl kernel interface.
normal rules for controlling this interface through apmd still apply.

ok deraadt@


# 1.236 09-Jul-2012 deraadt

tiny little oops in untested hibernate block


# 1.235 09-Jul-2012 deraadt

clean up ioctl() case statements


# 1.234 09-Jul-2012 deraadt

acpi_disable_onegpe() is not used; ok pirofti


# 1.233 24-May-2012 kettenis

If _MIN > _MAX consider a WordBus resource to be invalid. Fixes a panic on
the IBM x3550-M4.

ok mikeb@


# 1.232 29-Mar-2012 mlarkin

Missing #ifdef in previous diff


# 1.231 29-Mar-2012 mlarkin

Prevent hibernation attempts on machines with unsupported disk controllers.
The same check is also performed later in the hibernate sequence, but by
then it's too late to cleanly unwind (presently).

ok deraadt


# 1.230 26-Mar-2012 deraadt

simplify conditionals in acpi_sleep_state()
ok mlarkin


# 1.229 26-Mar-2012 deraadt

hook in the hibernate request code; half of this diff is from mlarkin
ok mlarkin


Revision tags: OPENBSD_5_1_BASE
# 1.228 20-Sep-2011 deraadt

Very early in suspend while the clock is still ticking realtime,
save the clock back to the rtc. The zaurus already did this.
ok phessler miod kettenis


Revision tags: OPENBSD_5_0_BASE
# 1.227 02-Jul-2011 nicm

kqueue attach functions should return an errno or 0, not a plain 1. Fix
the obvious cases to return EINVAL and ENXIO.

ok tedu deraadt


# 1.226 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.225 16-Jun-2011 pirofti

Import acpitoshiba driver.

This is originally written by Hiroyuki Aizu, ported and integrated
in our tree by Javier Vazquez <javama4 AT gmail> with tweaks and
suggestions by me.

Import prodded by deraadt@ and the lack of testers on tech@.


# 1.224 27-Apr-2011 jcs

attach acpithinkpad to newer lenovo models like the x120e

ok deraadt@


# 1.223 22-Apr-2011 jordan

Fix uninitialzied variables and formatting strings (-Wxxx errors)


Revision tags: OPENBSD_4_9_BASE
# 1.222 02-Jan-2011 jordan

Re-add code for ACPI taskqueue, eliminates need for state vars in acpi softc
Serialize processing ACPI tasks for sleep/powerdown/GPE event
ok deraadt@


# 1.221 31-Oct-2010 guenther

Revert last commit: it breaks resume on Thinkpads
"then please back it out" deraadt@


# 1.220 26-Oct-2010 jordan

Add task queue for ACPI gpe and notify handlers
This fixes eject on Dell Latitude dock and an issue on the Dell Mini
battery update.
ok deraadt


# 1.219 07-Oct-2010 jordan

Create separate functions for enabling wake and runtime gpes.
Remove old suspend/resume gpewalk function
ok deraadt, mlarkin


# 1.218 05-Oct-2010 jordan

split acpi_enable_onegpe to separate enable/disable functions
ok deraadt


# 1.217 05-Oct-2010 jordan

Added acpi_disable_allgpes function to clear GPE status
ok deraadt


# 1.216 05-Oct-2010 deraadt

clean up acpi_softc use more


# 1.215 05-Oct-2010 deraadt

delete a bit of #if 0 code


# 1.214 05-Oct-2010 deraadt

hide use of our global acpi_softc pointer a bit better


# 1.213 29-Sep-2010 deraadt

grab the acpi thread lock very early in acpi thread startup; not that it
is likely that anyone will race us... but why be careless.
ok kettenis


# 1.212 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.211 29-Aug-2010 jasper

- add a check for FWRITE here too.

ok miod@


# 1.210 27-Aug-2010 kettenis

Make PCI Power Management optional, and only enable it when acpi(4) attaches.

ok deraadt@


Revision tags: OPENBSD_4_8_BASE
# 1.209 08-Aug-2010 kettenis

Close race between the acpi thread and wscons ioctls running in process
context. We might rip this out post-release and replace it with a solution
that serializes things in a more obvious way.

ok deraadt@, miod@ and "those crazy texans who ok anything"


# 1.208 08-Aug-2010 deraadt

Report the acpi sleep states when on a RAMDISK; ok krw


# 1.207 07-Aug-2010 kettenis

Enforce acpi register access rescrictions.

ok mlarkin@, deraadt@, tested by marco@


# 1.206 07-Aug-2010 deraadt

upon resume, notify all the acpiac and acpibat drivers to update
their status. some laptops do not do this automatically.
ok kettenis canacar


# 1.205 06-Aug-2010 marco

Always call _PSW on _LID devices that support it to enable lid open to
resume the machine. Conversely disable it when the machine wakes up.

Tested by several ok deraadt


# 1.204 06-Aug-2010 deraadt

As part of a revamp of the PM1/GPE code, I write this diff to look for
unmanaged EN & STS bits in the PM1 register at interrupt time and report
them. As a side effect this splits the STS acknowledgement into two writes
(for power, and sleep) instead of one. The printf that is added (to spot
unmanaged STS bits) has not yet been triggered as far as we know.

Before the "write to PM1 registers at the right offset" diff went in, this
was not neccessary. But newer thinkpads do not have a working soft power
button without this diff.

We have no idea why.
ok mlarkin kettenis


# 1.203 05-Aug-2010 deraadt

PM1 power button events were generating 'fake events' to acpibtn PWRB,
but if there is no such device these were being tossed. Instead, arrange
for all power button handlers to flag a softc variable which is processed
by the thread; so that one place will know to request a powerdown (psignal
to init)
ok marco mlarkin


# 1.202 05-Aug-2010 deraadt

rename a variable which is annoyingly poorly named


# 1.201 05-Aug-2010 mlarkin

Write to the correct offset for various ACPI pmreg registers. Writing to
the correct register does properly clear the wake status bits and fixed
event bits, which allows some machines to have more than 1 suspend cycle.

bug spotted by deraadt@

tested by deraadt@ and myself on a half-dozen machines and others on
hackers@

ok deraadt@, marco@


# 1.200 05-Aug-2010 deraadt

Fix the power button. The pm1 register is 16 bits in size.
My mistake, a while back.
tested by krw


# 1.199 04-Aug-2010 kettenis

Print supported sleep states.

ok marco@, phessler@, deraadt@


# 1.198 03-Aug-2010 mlarkin

Use the proper flag for re-enabling certain hardware events (power btn,
sleep btn). The (incorrect) flag prevoiously used coincidentally had the
same value, so this shouldn't cause any different behavior than before.

tested on a variety of machines (i386, amd64, sp, mp) - no behavioral
change seen

ok deraadt@


# 1.197 28-Jul-2010 marco

Make all gasio failures look and behave the same.


# 1.196 28-Jul-2010 mlarkin

Delay a little longer on suspend before giving up. Fixes machines that
sometimes don't suspend because the sleep transition takes too long
(symptoms like failing to sleep but the backlight stays on).

ok deraadt@


# 1.195 27-Jul-2010 deraadt

remove XXX; EC can now be attached early when needed; ok jordan


# 1.194 27-Jul-2010 deraadt

more brackets feels good


# 1.193 27-Jul-2010 deraadt

XXX annotate a block in acpi_enter_sleep_state() that is pretty worrying


# 1.192 27-Jul-2010 deraadt

horrific indentation that nearly made me puke


# 1.191 27-Jul-2010 mlarkin

Set the sleeping indicator light on machines that support it, in case SMI
doesn't do it for us. Shows the 'moon' LED on older Thinkpads when
sleeping.

Tested on T43p, W500, T510 by me and on X61s by deraadt@.

ok deraadt@


# 1.190 27-Jul-2010 jordan

Early initialization of acpiec if ECDT table exists
Fixes hang when booting thinkpads while docked
ok deraadt


# 1.189 26-Jul-2010 deraadt

remove merge error which was commited


# 1.188 26-Jul-2010 pirofti

Add support for Sony ACPI hotkeys via a new driver: acpisony(4).

Currently it only works for the suspend button and tries to do right for
the brightness events, but I haven't found a Sony laptop that like to do
right so far.

In the future I want to make the brightness keys work on all Sony's and
also add support for the zoom hotkeys and whatever other funky keys I can
find on those things.

Okay deraadt@.


# 1.187 22-Jul-2010 deraadt

pretty large cleanup of SMALL_KERNEL


# 1.186 21-Jul-2010 deraadt

in the acpi GPE handler, late ack level interrupts not edge interrupts.
Duh.


# 1.185 21-Jul-2010 deraadt

Merge the edge and level gpe handlers into one (now possible since the
gpe structure tells us whether the gpe is edge or level)


# 1.184 21-Jul-2010 deraadt

delete #if 0 code we are not going to use; ok marco


# 1.183 20-Jul-2010 deraadt

make the mask code for acpi_enable_onegpe() match other similar code


# 1.182 20-Jul-2010 deraadt

Handle the acpi interrupt controller with proper edge/level handling,
wrapped in spltty to avoid racing against the interrupt controller.
Repair the gpe bit masking code while there, and do operations in the
right order
ok kettenis mlarkin, help from jordan at figuring out the order of
operations.


# 1.181 20-Jul-2010 matthew

When we're preparing to go to sleep, only call bufq_restart() if other
preparations failed.

ok kettenis@, thib@, deraadt@


# 1.180 19-Jul-2010 deraadt

permit compilation with ACPI_DEBUG


# 1.179 19-Jul-2010 deraadt

there is no need for a seperate dsdt_softc pointer; dsdt does not have a
softc but is just a part of the single acpi driver.
ok jordan


# 1.178 19-Jul-2010 deraadt

acpi_delay() is no longer needed; ok jordan


# 1.177 18-Jul-2010 mlarkin

Clear event status on resume, to avoid going back to sleep/reboot/shutdown
immediately after resume. Tested by a bunch of people on hackers@ on
various hardware. Fixes at least Toshiba NB200 and Gateway LT3103u.

ok marco


# 1.176 13-Jul-2010 deraadt

rename some thread related interfaces so they are logical


# 1.175 10-Jul-2010 jordan

Verify _ADR bdf is correct (some systems return 0xffff)
Works on Dell x9xx and Studio 1555


# 1.174 09-Jul-2010 jordan

Clear the noise


# 1.173 08-Jul-2010 jordan

Add mapping for ACPI device to PCI bdf (match autoconf tree)
Simplify resource parsing function to use buffer argument
Convert namespace linked lists to use queue macros
ok marco@, deraadt@


# 1.172 06-Jul-2010 deraadt

Use spltty() to lock downcalls from apm against the information being
modified by the acpi thread.
ok kettenis


# 1.171 06-Jul-2010 deraadt

Don't use a workq to do the suspend, because it races aginst the acpi
thread. Instead, just tell the acpi thread to do the suspend for us.
This makes apmd & zzz work correctly. While here, have acpithinkpad
attempt to post the event to apm before prompting the suspend itself.
ok kettenis marco mlarkin


# 1.170 05-Jul-2010 mlarkin

Reset register SCI_EN on resume. The ACPI spec clearly states we don't have
to do this, but many machines don't, or forget to, which leads to all sorts
of problems on resume (broken battery stats, overheating, failure to resume
more than once, etc).

tested by many, ok marco@, deraadt@


# 1.169 01-Jul-2010 thib

do bufq quiesce again, since vnd/the bufq_done call in
biodone has been fixed.


# 1.168 01-Jul-2010 jordan

Backout recent AML changes, breaks on R210 and others


# 1.167 01-Jul-2010 jordan

Simplified aml_parse_resource call, cleanup code
ok marco


# 1.166 01-Jul-2010 jordan

Fix subordinate bus # for multi-root PCI buses
ok marco, kettenis


# 1.165 30-Jun-2010 thib

Disable/partially backout the bufq quiesce changes as this
is causing havoc with vnds and release must be buildable.


# 1.164 29-Jun-2010 deraadt

Fix ramdisks (new code was placed inside a SMALL_KERNEL chunk)


# 1.163 29-Jun-2010 jordan

Add support for mapping ACPI to PCI devices
ok kettenis, deraadt


# 1.162 29-Jun-2010 kettenis

Call bufq_quiesce() on suspend to stop all I/O and bufq_restart on resume.
Makes suspend/resume work much better while doing disk I/O.

ok mlarkin@


# 1.161 29-Jun-2010 tedu

60 is a more reasonable guess for number of minutes in an hour than 100.
spotted by TeXitoi. ok deraadt


# 1.160 27-Jun-2010 jordan

Add _acpi_matchhids routine, precursor to pci device match


# 1.159 27-Jun-2010 mlarkin

Clear ACPI fixed event status on resume (power buttons/etc) so that some
machines don't immediately resume after suspending to S3.

Tested by kettenis@ and I on 5 or 6 machines, deraadt@ "yeah, looks good"


# 1.158 27-Jun-2010 jordan

Cleaned up acpi_maptable, adds to table list directly


# 1.157 07-Apr-2010 deraadt

If we have done a wsdisplay_suspend(), and ACPI decides it does not want to
suspend, we must roll back by calling wsdisplay_resume() or the virtual
console switching will be locked forever
ok oga


# 1.156 07-Apr-2010 kettenis

Make suspend/resume work on MP machines (running an MP kernel). Joint work
from mlarkin@ and me, with some amd64 fixes thrown in by deraadt@

ok marco@, deraadt@, pirofti@, mlarkin@


# 1.155 31-Mar-2010 kettenis

Bring /dev/apm support in line with apm(4). Make the suspend button send
a suspend request event to apmd(8) instead of suspending immediately. Also
keep track of whether /dev/apm and /dev/apmctl are currently open, such that
we can still suspend immediately if apmd(8) isn't running.

ok deraadt@, marco@, pirofti@, jsing@, oga@


# 1.154 30-Mar-2010 oga

Prevent the apmd/x races for good.

When we hit suspend time, go through all wsdisplays on the system. if
they are in mode MAPPED, but not MODE_DUMBFB then if possible do a full
vt switch to a !mapped vt, and prevent switching back until resume time.
This has to be called from MD code because this involves userland
running so that X can run the vt switch signal handler. This way, any
case where we are using the "poke registers from userland" model, we
will not be on the hardware when we go down, so the kernel can actually
handle thing properly.

Tested on several acpi laptops (by kettenis@ and ian@), x40 (me and
beck@ at LEAST) and zaurus (me). Maybe others, but if so I forgot who at
this time..

Idea from deraadt somewhere over the Faroe Islands (I thought of a
similar thing myself a while ago). Much prompting from him. Ok and
comments miod@


# 1.153 25-Mar-2010 oga

Fix & vs. && in the kqfilter code.

Based on a comparison with the apm code.

ok deraadt@, kettenis@


Revision tags: OPENBSD_4_7_BASE
# 1.152 26-Nov-2009 mlarkin

Visual (and on some machines, audio) feedback while the machine is
resuming via calls to aml _SST method. This also makes the "moon" LED on
thinkpads to blink during resume and cease when resume is completed.

ok pirofti@


# 1.151 26-Nov-2009 deraadt

Toggle into "cold" when we we disable interrupts, because tsleeping
in the AML interpreter and context switching into userland processes
tends to sometimes pop the ACPI trampoline page out of our address
space. Makes lots more machines work.
ok kettenis mlarkin


# 1.150 24-Nov-2009 jsg

in the resume path evaluate _WAK before dealing with device drivers
like the spec says, not after.

ok mlarkin@


# 1.149 24-Nov-2009 deraadt

abort suspends on SMP systems until that gets written; ok mlarkin


# 1.148 23-Nov-2009 mlarkin

pci should return the result of it's children's suspend/resume calls back
to its parent.

handle suspend failure case and unwind if devices have an activate function
that returns failure.

ok deraadt@, kettenis@


# 1.147 23-Nov-2009 mlarkin

acpi* devices need to call their children on suspend/resume.
ok deraadt@


# 1.146 23-Nov-2009 pirofti

Remove ACPI_SLEEP_ENABLED checks.

This enables by default the suspend/resume paths in the kernel.

Okay deraadt@.


# 1.145 23-Nov-2009 deraadt

knf and spacing that are super annoying


# 1.144 23-Nov-2009 mlarkin

Hook up Theo's new children suspend/resume function, as well as more ACPI
suspend/resume glue.

ok deraadt@


# 1.143 26-Oct-2009 deraadt

Do not do strncmp comparisons on dv_xname because longer device names which
look similar could arrive in the future. Instead, compare directly against
dv_cfdata->cf_driver->cd_name
Issue originally spotted by miod


# 1.142 26-Oct-2009 jordan

Verify checksum+address when loading ACPI tables.
Some systems had invalid entries in RSDT/XSDT.
ok marco@


# 1.141 23-Jul-2009 cnst

New aibs(4) driver for ASUSTeK AI Booster (ACPI ATK0110) hardware monitoring,
with sensor state support through limits provided by the ACPI.

Tested on several ASUS motherboards kindly networked by Sam Fourman Jr.

ok deraadt marco jordan


Revision tags: OPENBSD_4_6_BASE
# 1.140 03-Jun-2009 pirofti

Import acpi power resource driver. This is needed for suspend/resume logic.

Okay jordan@ and marco@.


# 1.139 03-Jun-2009 pirofti

Add acpivideo support. This adds brightness support for all laptops
except thinkpads, they will use the acpithinkpad driver. The driver is
also hooked into wsconsole. So brightness can be adjusted via:

$ wsconsctl display.brightness=<percentage>

This is very helpfull on some laptops that have a nasty bios and get two
steps instead of one when pressing the brightness button.

Tested on various dell, fujitsu, acer, samsung and other laptops.

Okay marco@, miod@. Suggestions from kettenis@.
Lots of reviews and help from miod@, thanks!


# 1.138 03-Jun-2009 jordan

Added ejectable bay for IDE cdroms
ok marco@


# 1.137 30-Apr-2009 marco

Oga changed apm ioctls and forgot to grep. Temporary work around by hitting
the sleep button when we get any request. This interface will chnage at
some point.
ok oga


# 1.136 19-Apr-2009 krw

Call acpi_sleep_walk() when powering down just like when entering
any other sleep state. This tells acpi wake devices to not wake up
the box until manual powering up resets their status. Makes my Dell
GX520 stay powered off in response to 'halt -p'.

In snaps for a while.

ok marco@ (several times) deraadt@


# 1.135 17-Apr-2009 pirofti

Add device ranking support for future sleep/wake developments.

okay marco@


# 1.134 11-Mar-2009 jordan

Cleanup.. use aml_evalinteger instead of aml_evalname
ok marco@


# 1.133 10-Mar-2009 jordan

Fix cut/paste error.. call _BFS not _PTS
ok marco@


Revision tags: OPENBSD_4_5_BASE
# 1.132 21-Feb-2009 marco

overzealous cut/paste; found by Vladimir Kirillov <proger@uaoug.org.ua>


# 1.131 20-Feb-2009 marco

Oops didn't mean for Debugger() to remain there.


# 1.130 19-Feb-2009 marco

suspend/resume bits so that we can develop this in tree. This is disabled.
code from mlarkin and me
help from art,toby,jordan and several others
ok jordan, go for it deraadt


# 1.129 10-Feb-2009 jordan

Fix null pointer for shutdown if acpi not enabled
ok marco@


# 1.128 04-Feb-2009 kettenis

Some systems that claim to be ACPI 2.0 apparently don't provide an XSDT
address. Make the code fall back on the RSDT on those systems.

From Joerg Niendorf <p3y80@internode.on.net>. Fixes PR 5986.


# 1.127 17-Jan-2009 krw

A couple of stray malloc/memset -> malloc(M_ZERO), a bit of knf and
lose a couple of unnecessary casts. No functional change.

Feedback from kettenis@

ok kettenis@ marco@


# 1.126 06-Nov-2008 marco

Collapse all sanity checks of hids into one function.

Originaly from jordan


Revision tags: OPENBSD_4_4_BASE
# 1.125 02-Jul-2008 fgsch

add acpivideo. for now almost an empty skel so others can work on
suspend/resume. eventually it will also manage output switching and
brightness where supported. prodded by marco@


# 1.124 11-Jun-2008 marco

Fix $OpenBSD$ for once and for all


# 1.123 08-Jun-2008 deraadt

aaa_dev was being set from an uninitialized variable; ok toby


# 1.122 07-Jun-2008 marco

Kill ACPIVERBOSE. It was never used and is worthless to boot.

comments jmc
ok toby


# 1.121 06-Jun-2008 marco

Shrink acpi some more for floppies and while here re-enable acpi on the
floppies. Fixes crazy sony vaio kettenis has.

tested by deraadt
ok deraadt jordan kettenis


# 1.120 01-Jun-2008 marco

Fix \n nit in SMALL_KERNEL.


# 1.119 01-Jun-2008 marco

Re-enable new parser. Lots of testing by various folks. Fits all media
now too.

ok kettenis deraadt


# 1.118 16-May-2008 dlg

back out jordans new parser.

it didnt have enough oks from the right people, it did break machines
that were previously working, and it bloated the RAMDISKS and install
media beyond the constraints of the media.

requested by deraadt@


# 1.117 14-May-2008 jordan

Adding new ACPI Parser code
Old guts of code still exists, needs to be torn out next
ok marco@
ok brad@


# 1.116 27-Apr-2008 jcs

add acpithinkpad(4), an acpi support driver for ibm/lenovo
thinkpads.

provides functionality that the bios did on older, non-acpi
thinkpads like respond to screen brightness keys and toggle the
bluetooth device on and off.

ok deraadt@ and marco@


# 1.115 24-Apr-2008 jsing

Introduce acpiasus(4), a driver for the ACPI based hotkeys found in many
ASUS laptops (including the ASUS EeePC) - largely based on NetBSD's
asus(4) driver. On the ASUS EeePC this allows us to enable/disable
wireless, change screen brightness and use the volume keys.

ok jsg@, weingart@


# 1.114 12-Apr-2008 kettenis

Get rid of acpi_s5 global variables; simply send SIGUSR2 instead of SUGUSR1
to tell init(8) to power down the machine.

ok krw@


Revision tags: OPENBSD_4_3_BASE
# 1.113 21-Jan-2008 jsg

Check against RESET_REG_SUP in FADT before doing the acpi reset dance
after all, not doing so appears to break more machines than it fixes.

ok marco@


# 1.112 05-Jan-2008 kettenis

Never attach more acpicpu(4) devices than cpu(4) devices. Always attach
the first acpicpu(4) device on non-MULTIPROCESSOR kernels, regardless of its
ACPI processor ID.

ok mikeb@


# 1.111 05-Dec-2007 deraadt

Hang acpi(4) below bios(4) instead of mainbus(4). This lets us move acpi(4)
before pcibios(4), and then.... if acpi(4) attaches, skip pcibios(4) since
it messes the machines up. Fixes claudio's HP dl320 G5, and almost assuredly
others.
ok kettenis


# 1.110 05-Dec-2007 deraadt

repair acpiopen/acpiclose and friends to handle the /dev/apmctl interface,
so that apmd(8) can actually work correctly against acpi(4), and thus,
even do the fancy -C/-A stuff; also tested by jmc


# 1.109 02-Dec-2007 fgsch

fix the trailing space for real now.


# 1.108 02-Dec-2007 fgsch

avoid trailing spaces on output; marco@ ok.


# 1.107 27-Nov-2007 kettenis

Fix off-by-one that made us attach acpicpu0 and acpicpu1 on GENERIC kernels.

ok marco@


# 1.106 25-Nov-2007 jsg

When available and ACPI is enabled use the ACPI reset method
to reboot. This is required for some quirky sis machines
and likely others to reboot.

Tested by many. 'commit early commit often' deraadt@


# 1.105 19-Nov-2007 deraadt

sigh, amd64 still uses that stupid acpi_enabled variable...


# 1.104 19-Nov-2007 deraadt

softc's come initialized to 0, you know...


# 1.103 19-Nov-2007 deraadt

store revision; from jsg


# 1.102 19-Nov-2007 kettenis

Initialize sc_facs earlier.

ok deraadt@, beck@, canacar@


# 1.101 16-Nov-2007 deraadt

fix the bus_space #define nightmare, so that amd64 and i386 are much more
uniform. as a result shared code like acpi needs less #ifdef's
ok marco kettenis


# 1.100 15-Nov-2007 deraadt

remove the #ifdef ACPI_ENABLE. the EC codepath is either safe enough (and
will be improved), or it is unsafe because AML assumes it must be there
ok almost everyone


# 1.99 15-Nov-2007 mikeb

Make acpicpu(4) attach to the existing CPUs only.

Problem reported by form@. Fix idea by kettenis@.
Lots of help from deraadt@. Tested by deraadt@, form@ and me.

ok deraadt marco


# 1.98 14-Nov-2007 deraadt

spacing


# 1.97 12-Nov-2007 deraadt

correct : vs , in attach-time messages


# 1.96 06-Nov-2007 deraadt

all ACPI_ENABLE kernels need acpi_interrupt()


# 1.95 06-Nov-2007 deraadt

move acpi_read_pmreg() and acpi_write_pmreg() out of SMALL_KERNEL so
that acpi ramdisks can compile (obvious solution)


# 1.94 05-Nov-2007 canacar

Establish the acpi interrupt earlier. Fixes problems with some
GENERIC.MP kernels not getting acpi interrupts. This is a
workaround until the real issue with late establishment is fixed.
ok weingart@, beck@, gwk@


# 1.93 03-Nov-2007 jordan

Added support for displaying wakeup devices
ok beck@,weingart@,gwk@


# 1.92 11-Oct-2007 kettenis

When walking the tree yo find _PRT methods, check whether the device is
actually enabled and functioning. Similar to what we do for _INI.

ok weingart@, beck@


# 1.91 08-Oct-2007 krw

More simple memset(,0,) -> M_ZERO changes. In this batch move to
size(*p) as the first malloc() parameter where p is declared locally
and thus easy to check. Add M_ZERO to gpe_table allocation in acpi.c
even though there is no obvious bzero or memset nearby.


# 1.90 13-Sep-2007 mk

Remove unnecessary memset() (see aml_evalnode(9)) and a bit of KNF.

ok toby


# 1.89 13-Sep-2007 weingart

Implement ACPI 6.5.1 spec tree walk for _STA and _INI.
Fixes mk's laptop. No regressions so far (thank you to the
testers).

ok gwk@, mk@, marco@


Revision tags: OPENBSD_4_2_BASE
# 1.88 17-Apr-2007 mk

Now that acpidock no longer hangs machines during boot (at least
according to the reports I've been getting) it should be safe to enable
it again, so remove the #if 0 stuff around it.

ok marco


# 1.87 11-Apr-2007 jordan

Added changes for C-State
ok marco@


# 1.86 26-Mar-2007 marco

Calculate minutes left more accurately.

From giovanni <qgiovanni@gmail.com>


Revision tags: OPENBSD_4_1_BASE
# 1.85 03-Mar-2007 mk

Stop looking for acpidock until we fix that stuff that breaks all pre-60
series ThinkPads with acpidock(4) enabled.


# 1.84 22-Feb-2007 marco

We were getting no interrupts in .MP. There are 2 fixes; one in
acpi_machdep.c and move the interrupt establishment down after ioapic
has been initialized.

ok jordan


# 1.83 21-Feb-2007 kettenis

Make an acpi-enabled GENERIC.MP work on systems that only support PIC mode.

ok marco@


# 1.82 19-Feb-2007 jordan

Added acpi_map_address function for mapping GAS/IO registers
ok marco@


# 1.81 18-Feb-2007 jordan

Added dynamic perfect hash for opcodes
ok marco@


# 1.80 18-Feb-2007 marco

Shave off another K pre compression.
Shuffle SMALL_KERNEL functions around so that we only have a single #ifndef


# 1.79 17-Feb-2007 marco

Add SMALL_KERNEL throught acpi to make it fit on boot media.
ok deraadt


# 1.78 13-Feb-2007 marco

Add smarter _INI code. This helps with some dissapearing devices.

ok kettenis jordan


# 1.77 31-Jan-2007 gwk

Introduce acpi_hasprocfvs thats "ACPI Has Processor Frequency and Voltage
Scalling" a global flag set by acicpu attach to indicate that ACPI is
handling fvs through the _PSS objects. This will fix disappearing
EST/powernow when booting kernels with acpi enabled, as acpicpu isnt
compiled into GENERIC yet. Also in cases where acpicpu is built in and
legacy EST/powernow work but acpi is missing the _PSS object, the legacy
routines will be used.

tested by dim@, and Nick Nauwelaerts <nick AT nauwelaerts.net>
ok marco@, dim@


# 1.76 22-Jan-2007 mk

Actually look for docking stations by looking for _DCK nodes.

ok marco


# 1.75 02-Jan-2007 marco

On some systems EC must be attached as the first device. Move EC from
generic _HID devices to its own routine to force first attach. Bug found
and debugged by joris@

Code mostly written by joris@


# 1.74 26-Dec-2006 marco

Add polling to devices that require it such as acpibat and acpitz.
Use the same tsleep/wakeup mechanism as the interrupt code to keep it non-
concurrent.


# 1.73 21-Dec-2006 deraadt

even more knf love


# 1.72 21-Dec-2006 marco

Remove some leftover acpiec gpe/interrupt code.


# 1.71 20-Dec-2006 deraadt

oops, name already used


# 1.70 20-Dec-2006 deraadt

rename __acpi_enable_gpe(); ok marco


# 1.69 20-Dec-2006 deraadt

acpi_unmap_pmregs() is not used, ok marco


# 1.68 19-Dec-2006 deraadt

simplify some low-level methods substantially (shrinking code); ok marco jordan


# 1.67 06-Dec-2006 mk

malloc(..., M_NOWAIT) with no NULL check is bad. If allocation fails,
complain, clean up, and bail out of attach.

input and ok gwk


# 1.66 28-Nov-2006 jordan

Fixed clearing arg for acpi_init_pic
ok kettenis@


# 1.65 27-Nov-2006 kettenis

Don't spam dmesg.


# 1.64 27-Nov-2006 jordan

Fixes for acpiprt routing
Post-parse fixup for namerefs
ok kettenis@


# 1.63 25-Nov-2006 marco

Don't handle acpiev events inside the gpe handler since this causes deadlocks.

ok jordan@


# 1.62 15-Nov-2006 kettenis

Hook up acpi apic interrupt routing support to generic acpi code.

ok jordan@


# 1.61 02-Nov-2006 marco

Kill acpiec attach function and add its functionality to the generic _HID
device attach

prodded and ok jordan


# 1.60 02-Nov-2006 marco

Fix comments to reflect reality.


# 1.59 19-Oct-2006 marco

Replace ACPI ioctls with APM ioctls. This makes ACPI talk the same protocol
as APM therefore userspace can now retrieve power information in the same
fashion as all other apm arches (zaurus, macppc, i386 etc).

To use force the use of acpi device like so: apmd -f /dev/acpi

ok jordan@


# 1.58 19-Oct-2006 jordan

Fixed GPE debug levels
Fixed ref counter on temporary bufferfields
DSDT parsing fully working on IBM Thinkpad
ok marco@


# 1.57 15-Oct-2006 jordan

Removed old GPE handler code remnants
ok marco@


# 1.56 12-Oct-2006 jordan

Massive ACPI changes
Fixed ACPI leaks
new ACPI API
ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.55 30-Jun-2006 gwk

KNF, No functional change,
"go at it" marco@


# 1.54 30-Jun-2006 jordan

Support for calling Embedded Controller via new GPE interface
ok marco@


# 1.53 30-Jun-2006 jordan

Added new code for GPE handling
Also works with ACPIEC; need to fixup acpiec.c to remove original interrupt handler
ok marco@


# 1.52 30-Jun-2006 jordan

Added offset argument for acpi read/write pmreg routines (new GPE code)
ok marco@


# 1.51 31-May-2006 todd

do not call acpi_powerdown() unless acpi has successfully initialized
ok gwk@ canacar@


# 1.50 29-May-2006 canacar

Add embedded controller support.
ok marco@ deraadt@ testing & ok gwk@


# 1.49 19-May-2006 canacar

Minimal thermal zone driver. Monitors thermal zone temperature,
shuts down the system if the 'critical temperature' is reached.
ok marco@


# 1.48 11-Apr-2006 marco

KNF.


# 1.47 11-Apr-2006 gwk

Add acpi_resume(struct acpi_softc *sc), which calls some AML methods,
detected at attach time, currently unused.

ok marco@


# 1.46 11-Apr-2006 gwk

Discover some ACPI sleep related methods, and call them at the
appropriate time in acpi_enter_sleep_state().

ok marco@


Revision tags: OPENBSD_3_9_BASE
# 1.45 26-Feb-2006 marco

Add acpicpu as a device.

ok jordan@


# 1.44 23-Feb-2006 marco

remove dead function.

ok jordan@


# 1.43 22-Feb-2006 marco

Print out all tables it found during autoconf. We'll need this data in
the future.


# 1.42 22-Feb-2006 marco

Make dmesg less verbose.

ok jordan.


# 1.41 22-Feb-2006 jordan

Added parsing of SSDT tables
ok marco@


# 1.40 22-Feb-2006 jordan

Added new aml_register to notify by device id
Moved powerdown code to button device
ok marco@


# 1.39 21-Feb-2006 marco

Readjust debug levels that crept in too low over the last few days.
KNF


# 1.38 21-Feb-2006 marco

Rig the power button so that the machine shuts down correctly.


# 1.37 20-Feb-2006 marco

Add acpibtn device. Remains disabled for now.


# 1.36 19-Feb-2006 jordan

Added code for aml_notify GPE notification callback
ok marco@


# 1.35 19-Feb-2006 grange

Fix acpi_enter_sleep_state() by replacing current ugly hack
with mostly correct sleeping state transition code.

ok brad@ marco@


# 1.34 19-Feb-2006 marco

jordan:
Enable interrupts.
Added peripheral code to make this work.

marco:
Replace softintr and other types of interrupts with a handler with context.
Fixed interrupt magic for GPEs.
Some KNF

This is work done by jordan and I but has to be a single commit.


# 1.33 17-Feb-2006 marco

Remove earlier introduced delays. We really only need them during autconf.

ok jordan@


# 1.32 17-Feb-2006 marco

Add a generic delay function as per the ACPI spec.

from jordan@


# 1.31 17-Feb-2006 marco

Use tsleep instead of delay when !cold.


# 1.30 17-Feb-2006 marco

Argh, we need delays between reads/writes. The backend reads (i2c most likely)
are so slow that values come back as 0 sometimes. We have to do this
despite the spec saying we shouldnt have to. Guess having a faster
parser than others can hurt sometimes.

Idea from jordan.


# 1.29 17-Feb-2006 jordan

Fixed code for handling _INI
Rearranged debug statements
ok marco@


# 1.28 16-Feb-2006 jordan

Added stub for calling _INI
Fixed aml_dowhile for NULL return
ok marco@


# 1.27 16-Feb-2006 jordan

Fixed parser/evaluator to retrieve battery info+state
Cleanup of parser
Dell laptops return battery status
ok marco@


# 1.26 06-Feb-2006 brad

fix a typo so this will compile on amd64.


# 1.25 04-Feb-2006 marco

When ACPI_ENABLED wasn't set but the devices were it had an unknown variable.

Pointed out by ian@.


# 1.24 03-Feb-2006 jordan

Rewrote aml parser yet again...
Fixed bus_space_map for i386/amd64
Added _BST and _BIF to acpibat to get status/info
ok marco@


# 1.23 20-Jan-2006 jordan

More evaluator changes
Removed old evaluator code
Fixed _HID value
ok marco@


# 1.22 19-Jan-2006 jordan

Added support for read/write of PCI Config space
Help jason@
ok marco@


# 1.21 18-Jan-2006 jordan

Added new evaluation routines
Added helper methods for buffer/field evaluation
Fixed broken indentation on dsdt.c

ok marco@


# 1.20 17-Jan-2006 jordan

Major overhaul of the aml parser. Evaluater temporarily disabled.

ok marco@


# 1.19 06-Jan-2006 grange

Check for ACPI control availability only if ACPI_ENABLE defined.


# 1.18 06-Jan-2006 grange

Informative error messages on attach, prettier dmesg.


# 1.17 06-Jan-2006 grange

printf -> dnprintf conversion for debug messages.


# 1.16 05-Jan-2006 grange

Find SLP_TYPx values and store them for future use in
acpi_enter_sleep_state().


# 1.15 05-Jan-2006 grange

Random KNF and cleanup


# 1.14 05-Jan-2006 grange

Provide symbolic names for the acpi sleeping states and use it.


# 1.13 28-Dec-2005 marco

Moved DEVNAME to a acpidev.h

ok jordan@


# 1.12 18-Dec-2005 sturm

add back comment about wrong code

ok marco


# 1.11 16-Dec-2005 marco

Remove test for kbd_reset. Prompted deraadt@, ok jordan@


# 1.10 16-Dec-2005 jordan

Added hooks for powerdown/power button press


# 1.9 16-Dec-2005 jordan

Cleaned up common power management register i/o
Added enter sleep state ioctl
Hooked power button interrupt
ok marco@


# 1.8 16-Dec-2005 marco

Remove autoconf chatter


# 1.7 16-Dec-2005 jordan

Moved DSDT AML parsing into parent ACPI driver
ok marco@


# 1.6 14-Dec-2005 marco

Add AC device glue.


# 1.5 14-Dec-2005 marco

Adjust debug printing levels to something more sane.


# 1.4 13-Dec-2005 marco

Add glue to attach devices to apci.


# 1.3 07-Dec-2005 marco

KNF


Revision tags: OPENBSD_3_8_BASE
# 1.2 10-Jul-2005 grange

Spacing cleanup.


# 1.1 02-Jun-2005 tholo

Start on a basic ACPI framework -- does not do much more than read out the
ACPI tables into kernel memory and attach ACPI and HPET timers currently.

In order to test this code, enabling the devices in GENERIC as well as
the ACPI_ENABLE option is needed. This code does not do any thermal
control yet, so this should be done with care depending on the platform.

In the tree so more people can contribute to making this more fully
featured.

Ok niklas@ grange@ tedu@


# 1.399 20-Jul-2021 mlarkin

Fix trailing whitespace in a few places.

No code change.


# 1.398 09-May-2021 kettenis

Some x86 machines advertise the "hardware reduced" ACPI feature, advertise
S4 and S5 support, but fail to populate the SLEEP_CONTROL_REG and
SLEEP_STATUS_REG descriptions in the FADT. An example of such a machine
is the ASUS Zenbook 14 UM433DA. Any attempt to powerdown the machine
will result in a kernel crash. It turns out that using the legacy ACPI PM
registers works fine on this machien. So fall back on those registers
if SLEEP_CONTROL_REG and/or SLEEP_STATUS_REG aren't provided.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_9_BASE
# 1.397 15-Mar-2021 patrick

Add acpi_iommu_device_map(), which replaces the DMA tag with one that
is blessed with IOMMU magic, if available. This is mainly for arm64,
since on amd64 and i386 the IOMMU only captures PCIe devices, as far
as I know, which uses the pci_probe_device_hook(). This though is for
non-PCI devices attached through ACPI.

ok kettenis@


# 1.396 07-Mar-2021 patrick

Pass standard DMA tag to acpi(4) table drivers.

ok kettenis@


# 1.395 27-Dec-2020 deraadt

acpi_map_address() cannot be wrapped by SMALL_KERNEL anymore, as
it is used by acpihpet.c


# 1.394 25-Dec-2020 visa

Refactor klist insertion and removal

Rename klist_{insert,remove}() to klist_{insert,remove}_locked().
These functions assume that the caller has locked the klist. The current
state of locking remains intact because the kernel lock is still used
with all klists.

Add new functions klist_insert() and klist_remove() that lock the klist
internally. This allows some code simplification.

OK mpi@


# 1.393 17-Dec-2020 kettenis

Fix some issues with referencing named ACPI nodes from Packages.
These references need to be resolved at runtime rather than when
they're parsed such that they pick up the right values for those nodes
which can be changed when for example _INI methods run.

The current approach is to replace these reference with a string that
names the node in question. The problem with that is that packages
can also contain normal strings. Which means that we need special
code that depends on the context where the package is used.

This diff takes a different approach by retaining a reference when
parsing. Code that uses a package will need to resolve this reference
but there is no ambiguiety anymore.

ok patrick@


# 1.392 05-Dec-2020 kettenis

Add support for the _EVT method to the GPIO support code.

ok patrick@


Revision tags: OPENBSD_6_8_BASE
# 1.391 27-Aug-2020 jmatthew

Reorder the acpi attach process so we look at Processor() CPU nodes
after Device() ones, since we should prefer the newer node type.
If we see any Device() nodes, don't attach acpicpu(4) to any Processor()
nodes if they're also present. This also makes acpitz(4) and
acpipwrres(4) devices attach slightly later.

ok kettenis@, also tested by jmc@


# 1.390 16-Aug-2020 gnezdo

Remove sc_maxgpe from acpi_softc which was never read from

Remove a dead conditional from acpi_init_gpes while there.

ok jcs@


# 1.389 21-Jul-2020 deraadt

acpi can use IPL_BIO (a low interrupt) since it only enqueues operations for
later processing. The use of a high interrupt will predate suspend/resume
efforts, we had to redesign acpi to be non-reentrant obviously
discussed with kettenis, in snaps for more than a week


# 1.388 24-Jun-2020 cheloha

kernel: use gettime(9)/getuptime(9) in lieu of time_second(9)/time_uptime(9)

time_second(9) and time_uptime(9) are widely used in the kernel to
quickly get the system UTC or system uptime as a time_t. However,
time_t is 64-bit everywhere, so it is not generally safe to use them
on 32-bit platforms: you have a split-read problem if your hardware
cannot perform atomic 64-bit reads.

This patch replaces time_second(9) with gettime(9), a safer successor
interface, throughout the kernel. Similarly, time_uptime(9) is replaced
with getuptime(9).

There is a performance cost on 32-bit platforms in exchange for
eliminating the split-read problem: instead of two register reads you
now have a lockless read loop to pull the values from the timehands.
This is really not *too* bad in the grand scheme of things, but
compared to what we were doing before it is several times slower.

There is no performance cost on 64-bit (__LP64__) platforms.

With input from visa@, dlg@, and tedu@.

Several bugs squashed by visa@.

ok kettenis@


# 1.387 10-Jun-2020 jca

Give userland a consistent battery and A/C state after resume

Previously we refreshed the state of acpiac(4) and acpibat(4)/acpisbs(4)
by queueing fake ACPI events to be processed by the acpi thread.
acpibat(4) using ACPIDEV_POLL meant that its status could be refreshed
first, resulting in an APM_POWER_CHANGE event sent to userland before
the state of acpiac(4) was up to date. Because of this, apmd(8) could
see A/C unplugged when it was in fact plugged, possibly leading to
a wrong decision.

Instead of using fake ACPI events, refresh the status of acpiac(4) and
acpibat(4)/acpisbs(4) right away at DVACT_WAKEUP time. To reuse the
existing refresh code of those drivers, make their ACPI *_notify()
handlers responsible for sending APM_POWER_CHANGE events instead. Also
queue an APM_POWER_CHANGE event right after resume to retain existing
behavior.

This fixes the consistency of data shown to userland, and makes those
drivers less special.

Initially discussed last year (!) with mpi@ at Miod's place.
Feedback and ok jcs@, deraadt@ agrees with the use of DVACT_WAKEUP here.


# 1.386 29-May-2020 deraadt

dev/rndvar.h no longer has statistical interfaces (removed during various
conversion steps). it only contains kernel prototypes for 4 interfaces,
all of which legitimately belong in sys/systm.h, which are already included
by all enqueue_randomness() users.


# 1.385 14-May-2020 kettenis

Bring back the acpi(4) changes. This time around only use the ACPI _CRS
information on ACPI 5.0 and later.

ok krw@, patrick@


# 1.384 11-May-2020 jca

More accurate remaining power reporting with disparate batteries

Overall remaining power was computed as the average of the remaining
power of each battery, in percents. This doesn't work with batteries of
different capacity, so use the total capacity and remaining power
instead.

Reported by lidstah, successful tests by lidstah and benno@, ok kettenis@


# 1.383 08-May-2020 kettenis

Move parsing of _CRS into acpi(4) and pass the parsed address and interrupt
information in struct acpi_attach_args.

ok mpi@


Revision tags: OPENBSD_6_7_BASE
# 1.382 14-Apr-2020 kettenis

Implement acpi_getprop(), the ACPI equivalent of OF_getprop().

ok patrick@


# 1.381 12-Apr-2020 kettenis

Add support for the _CCA method. This method indicates whether DMA is
cache-coherent or not. To implement this, acpi(4) gets two bus_dma tags
and passes the appropriate one when attaching devices based on _CCA.
On i386/amd64, where for all practical purpose DMA is always cache-coherent,
the two tags are the same. But on arm64 they are distinct.

ok patrick@


# 1.380 07-Apr-2020 visa

Abstract the head of knote lists. This allows extending the lists,
for example, with locking assertions.

OK mpi@, anton@


# 1.379 03-Apr-2020 mpi

Fix incoherencies of errors returned by various *kqfilter routines.

EOPNOTSUPP is returned when there's no "filterops" for a given fd.
EINVAL when the requested filter isn't supported by the "filterops".
ENXIO means the underlying device is no longer valid/present.

ok visa@


# 1.378 20-Feb-2020 visa

Replace field f_isfd with field f_flags in struct filterops to allow
adding more filter properties without cluttering the struct.

OK mpi@, anton@


# 1.377 31-Dec-2019 visa

Use C99 designated initializers with struct filterops. In addition,
make the structs const so that the data are put in .rodata.

OK mpi@, deraadt@, anton@, bluhm@


# 1.376 31-Dec-2019 mpi

Convert infinite sleeps to {m,t}sleep_nsec(9).

ok kn@


# 1.375 31-Dec-2019 jsg

include sys/sensors.h for sensor use
don't depend on indirect inclusion from acpidev.h


Revision tags: OPENBSD_6_6_BASE
# 1.374 07-Sep-2019 kettenis

Revert acpipci(4) changes. Too many systems are broken. We'll try again
after 6.6 as been released. The acpireg.h change stays behind.


# 1.373 28-Aug-2019 kettenis

Use ACPI information to attach PCI busses like we do on arm64. There are a
few additional quirks though, and attaching the PCI busses is delayed to
replicate the existing code more closely. That may be changed in the
future. Also tweak how we handle MSI support and respect to ACPI flag
that says we shouldn't attempt to use MSIs.

Some fallout is expected.

ok patrick@


# 1.372 27-Aug-2019 deraadt

On amd64 the bootup and resume paths for SP/BP/SP were not performing
msr/pat, clock-startup, firmware upgrade, fpu initialization, and cpu
soft-state initialization in the same order. That kind of chaos is
crazy and fragile. Improve the acpi resume call sequence and unify order
of the operations.


# 1.371 02-Jul-2019 jcs

acpi_set_gpehandler: turn edge arg into flags, add support for
passing GPE_DIRECT which directly executes the supplied callback and
clears a level interrupt rather than going through acpi_taskq which
can be slow when other driver callbacks are in the queue.

This should not be used for GPE handlers that need to do anything
ACPI-related.

ok kettenis


# 1.370 10-Jun-2019 kettenis

Print proper ACPI version number.

ok mlarkin@


# 1.369 08-Jun-2019 kettenis

When establishing the mapping between ACPI device nodes and PCI devices,
skip PCI host bridges and devices that aren't there.

ok patrick@


# 1.368 07-Jun-2019 kettenis

Introduce acpi_getsta() and use it to eliminate some duplicated code.

ok patrick@


# 1.367 12-May-2019 kettenis

Make sure the extended PM register descriptions actually exist before
using them. Fixes machines with ancient ACPI 1.0 (such as recent
hypervisors).

ok deraadt@


# 1.366 12-May-2019 kettenis

Add support for mapping ACPI PM registers using the "extended" gasio
representation from the FADT. Mostly fixes the Lanner NCA-1510.

ok lteo@, mlarkin@, deraadt@


# 1.365 11-May-2019 lteo

Fix incorrect error message.

ok kettenis@


# 1.364 08-May-2019 tedu

free size. the allocation is the header plus the length.


Revision tags: OPENBSD_6_5_BASE
# 1.363 04-Apr-2019 kettenis

Avoid potential null-pointer dereference. Found by Kent R. Spillner.

ok deraadt@, jsg@


# 1.362 20-Jan-2019 jsg

fix SMALL_KERNEL build after changes in rev 1.361
reported by naddy@ ok deraadt@


# 1.361 19-Jan-2019 tedu

Add a pwraction sysctl that controls what the power button does on acpi.
By default, nothing changes -- shutdown is initiated. But allows turning
power button into a sleep button if desired.
(grudging) ok from a few parties


# 1.360 26-Oct-2018 kettenis

Add acpipci(4) on amd64. For now this only calls the PCI-specific _OSC
method to let the ACPI implementation know what features we support.


Revision tags: OPENBSD_6_4_BASE
# 1.359 30-Aug-2018 kettenis

Move softbutton check before the check that skips attaching certain acpi
devices. Fixes regression caused by matching on _CID in addition to
matching on _HID.

ok matthieu@, mlarkin@


# 1.358 25-Aug-2018 kettenis

Match ACPI devices based on _CID if no match for _HID is found.

ok mpi@


# 1.357 19-Aug-2018 kettenis

Add support for multiple PCI segments. Only really implemented for arm64
for now as amd64/i386 firmware still caters for legacy OSes that only
support a single PCI segment.

ok patrick@


# 1.356 03-Aug-2018 kettenis

Let ahci(4) match on _CLS instead of _HID when attaching at acpi(4). Avoids
having to add many more _HID entries to the match table.

ok deraadt@, mlarkin@


# 1.355 10-Jul-2018 kettenis

Make legacy interrupts work in acpipci(4).

ok patrick@


# 1.354 05-Jul-2018 kettenis

Add acpipci(4), a driver that supports generic ECAM-compatible PCI host
bridges based on information provided by ACPI.

ok mlarkin@


# 1.353 04-Jul-2018 kettenis

Properly pass around the PCI "chipset tag" in acpi(4) and refactor
acpimcfg(4) to call an MD initialization functions that sets up a tag for
PCI ECAM.

ok guenther@, mlarkin@, krw@


# 1.352 01-Jul-2018 kettenis

Move acpi(4) attach glue into acpi_machdep.c.

ok guenther@, deraadt@


# 1.351 01-Jul-2018 kettenis

Add support for _DSD Device Properties.

ok mlarkin@


# 1.350 30-Jun-2018 kettenis

Add intr_enable() function, intended for MI use to amd64 and i386 and use
this in the acpi(4) suspend/resume code paths.

ok deraadt@


# 1.349 29-Jun-2018 kettenis

Use <stdint.h> types.

ok mpi@, jung@, krw@, deraadt@


# 1.348 29-Jun-2018 mlarkin

ACPI: Allow (with warning) GPE handler reassignment, instead of returning
a failure code.

ok kettenis


# 1.347 26-Jun-2018 mlarkin

knf


# 1.346 26-Jun-2018 kettenis

Let acpi(4) pass down the bus dma tag.

ok deraadt@


# 1.345 25-Jun-2018 kettenis

Reorganize acpi(4) code a bit in preparation for upcoming arm64 support.

ok deraadt@


# 1.344 20-May-2018 kettenis

Wake up the acpi thread after scheduling a gpio event.

ok mpi@


# 1.343 17-May-2018 kettenis

Sprinkle some #ifndef SMALL_KERNEL such that RAMDISK kernels cuild again.

pointed out by stsp@


# 1.342 17-May-2018 kettenis

Implement GenericSerialBus OpRegion support.

ok mlarkin@


# 1.341 27-Mar-2018 kettenis

Add acpicmos(4), a driver that implements SystemCMOS OperationRegion
access support. This fixes machines where the AML doesn't check whether
support for this OperationRegion type has been registered by the OS.

ok mlarkin@


Revision tags: OPENBSD_6_3_BASE
# 1.340 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.339 10-Feb-2018 deraadt

Syncronize filesystems to disk when suspending. Each mountpoint's vnodes
are pushed to disk. Dangling vnodes (unlinked files still in use) and
vnodes undergoing change by long-running syscalls are identified -- and
such filesystems are marked dirty on-disk while we are suspended (in case
power is lost, a fsck will be required). Filesystems without dangling or
busy vnodes are marked clean, resulting in faster boots following
"battery died" circumstances.
Tested by numerous developers, thanks for the feedback.


# 1.338 08-Feb-2018 deraadt

After flushing disks, try again to release all memory. We don't need to
save dirty memory to the hibernate space.


# 1.337 08-Feb-2018 deraadt

Toss all releaseable memory, because fragmentation can get in the way
of allocating the hibernate playpen.


# 1.336 08-Feb-2018 deraadt

sr_quiesce() is a new approach for ensuring that softraid drains
output to the disks.

This is part of a larger suspend/resume filesystem-safety diff, which
has been worked on for a couple of months already. Tests by job, krw,
beck, benno, and others. Sometimes even by snapshot users...


# 1.335 29-Nov-2017 kettenis

Add support for GPIO-signaled events.

ok mlarkin@


# 1.334 16-Nov-2017 jcs

Add PCI attachment for dwiic(4) needed by Intel 100 Series machines

ok kettenis


# 1.333 14-Nov-2017 kettenis

Add a few more PNP IDs to the skip lists.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.332 17-Aug-2017 stsp

Make dwiic(4) attach its ACPI parent dependencies.
This change makes the Asus E200HA keyboard work.

Original analysis and patch by Cesare Gargano
ok kettenis@


# 1.331 09-Aug-2017 dcoppa

Add "PNP0303" (8042 PS/2 Controller) to acpi_isa_hids[]

ok kettenis@


# 1.330 22-Jul-2017 jcs

acpibat: look for _BIX first before falling back on _BIF

ACPI 4.0 deprecated _BIF for battery status so some newer machines
are exporting _BIX which extends _BIF's package with a few extra
fields. When using _BIX, export the new discharge cycle count as a
sensor.

feedback from kettenis and anton
ok deraadt


# 1.329 20-Jul-2017 kettenis

Start a list of isa(4) device names such that we can avoid printing them
as "not configured".

ok mikeb@, millert@, deraadt@


# 1.328 08-Apr-2017 deraadt

Sensors are run as callbacks inside tasks. During suspend a sensor
could be running inside a driver that will be force-detached, or due
to tsleep end up disrupting the softstate/hardstate contract. At
suspend time, quisce all these callbacks by waiting for completion.
This issue has never been observed for real, but may be implicated in
suspend/resume failures.
ok kettenis guenther mlarkin


# 1.327 08-Apr-2017 deraadt

Two prototypes for acpi_maptable; merge to one place


Revision tags: OPENBSD_6_1_BASE
# 1.326 27-Mar-2017 deraadt

Now that hibernate_alloc() only has clean success/failure, don't
need to call hibernate_free() to clean up a partial mess.
ok mlarkin kettenis


# 1.325 13-Mar-2017 deraadt

allow ramdisks to build; ok jcs


# 1.324 12-Mar-2017 jcs

try to attach acpisbs first and if successful, don't attach acpibat


# 1.323 02-Mar-2017 natano

Add a new sysctl machdep.lidaction. The sysctl works as follows:

machdep.lidaction=0 # do nothing
machdep.lidaction=1 # suspend
machdep.lidaction=2 # hibernate

lidsuspend is just an alias for lidaction, so if you change one, the
other one will have the same value. The plan is to remove
machdep.lidsuspend eventually when people have upgraded their
/ets/sysctl.conf.

discussed with deraadt, who came up with the new MIB name
no objections mlarkin
ok stsp halex jcs


# 1.322 28-Feb-2017 natano

Untangle abstract sleep modes from ACPI sleep states. This paves the way
for further work in the area.

idea and ok deraadt
ok mlarkin


# 1.321 22-Feb-2017 jcs

add acpisbs, an acpi smart battery subsystem driver reading data
over smbus

currently disabled because it conflicts with acpibat

ok deraadt, kettenis


# 1.320 14-Jan-2017 kettenis

Revert aml_rdpciaddr changes; breaks several machines that were working
before.


# 1.319 11-Jan-2017 jsg

Increase the size of the buffers used for _HID and _CID strings to be
able to fit "Hyper_V_Gen_Counter_V1".

ok mlarkin@ deraadt@


# 1.318 08-Jan-2017 kettenis

Make aml_rdpciaddr() work for for devices behind bridges. The code now
uses the mapping between AML nodes and PCI devices that we establish.
Because _INIT methods may end up calling aml_rdpciaddr(), make sure we
create that mapping early. Also handle devices that aren't actually present.
These devices are now included in the mapping and reads will return an
all-ones patterm whereas writes are a no-op..

ok mlarkin@


# 1.317 25-Oct-2016 pirofti

Add the resource index to aml_parse_resource() callback function.

This is needed by an upcoming acpiec commit that handles machines breaking
the current ACPI specifications.

Change suggested by and ok kettenis@, guenther@


# 1.316 18-Sep-2016 guenther

If the APIC table (MADT) isn't present then ci_acpi_proc_id won't be set
when acpicpu tries to attach, leading to a NULL dereference. The ACPI
processor object on the Dells where this was reported is bogus and useless
anyway so apply the same test when deciding whether to attach to processor
objects as acpicpu would when matching them to cpus.

Problem reported by Eivind Eide (xenofil (at) gmail.com) and
Johan Huldtgren (johan+openbsd-bugs (at) huldtgren.com).
Testing by Eivind Eide.
ok kettenis@ mlarkin@


# 1.315 03-Sep-2016 naddy

Write the system time back to the RTC every 30 minutes.
This fixes the problem that long-running machines which were not
shut down properly would reboot with a badly offset system time.

hints and ok kettenis@


# 1.314 31-Aug-2016 mlarkin

Don't put a device in the wake device list if _STA says it's not there.

Fixes at least one machine that woke up from sleep / poweroff immediately.

ok kettenis@, tested by kettenis and martijn@


# 1.313 28-Jul-2016 kettenis

Store the acpi processor ID/UID in struct cpu_info, and use it to attach
acpicpu(4) drivers to the right cpu(4).

ok mlarkin@, guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.312 10-Jun-2016 kettenis

The power management timer has been made optional in ACPI 5.0A, so don't
attempt to attach acpitimer(4) if the timer isn't present.

Pointed out by jcs@

ok jcs@


# 1.311 08-May-2016 kettenis

Make sure devices listed by a device's _DEP method are attached before
we attach the device itself. The _DEP methode, introduced in ACPI 5.0,
is a hint that the device in question depends on OpRegion support from
the devices listed by _DEP, so we have to order them the proper way.
To prevent us from attaching those devices again when we encounter them
later walking down the device tree, keep track of our attempts to
attach a device driver to them using aflag in the aml_node structure.

ok guenther@


# 1.310 25-Apr-2016 pirofti

We attach acpihpet(4) and acpiec(4) without using the HID, so do not
report it in the dmesg.

OK deraadt@, mlarkin@.


# 1.309 02-Apr-2016 kettenis

Reduce dmesg spam by knocking out some acpi hardware IDs that don't need a
driver.

ok mpi@


# 1.308 29-Mar-2016 kettenis

Check if a device is present (using _STA) in acpi_foundhid. This prevents
us from reporting devices as "not configured" that aren't actually present,
and allows us to remove duplicated code from several drivers.

ok deraadt@, ok jsg@


# 1.307 29-Mar-2016 kettenis

Implement a cleaner way to prevent acpivout(4) from attaching if we have
a platform-specific method of controlling the backlight.

ok millert@, mlarkin@, jung@


# 1.306 28-Mar-2016 kettenis

Adding each and every new acpi device driver to acpi_foundhid() is
gettinga bit of the burden. And it really isn't how our autoconf
framework is supposed to work for busses that can be enumerated. All
the drivers already check for a matching _HID in their attach
function. So we can just drop the checks in acpi_foundhid() and let
the drivers sort themselves out.

Print a "not configured" line for devices for which we don't attach adriver.
This may be a bit much as there are devices for which we will not have a
driver. This will be removed if it causes too much dmesg spam.

ok deraadt@, mlarkin@


Revision tags: OPENBSD_5_9_BASE
# 1.305 17-Jan-2016 jsg

Take acpi_foundhid() outside of #ifndef SMALL_KERNEL so the ramdisk
kernel can find and attach dwiic(4) and sdhc(4).


# 1.304 17-Jan-2016 jsg

remove an unused global variable


# 1.303 14-Jan-2016 kettenis

Attach dwiic(4) on the Intel Bay Trail i2c controllers as well.


# 1.302 12-Jan-2016 kettenis

Improve emulation of PM registers on Hardware-redcued ACPI by emulating
SPL_TYPx, SLP_EN and WAK_STS using the new SLEEP_STATUS_REG and
SLEEP_CONTROL_REG registers. Makes the transition into S5 (powerdown) work
on the ASUS X205TA.

ok deraadt@, mlarkin@


# 1.301 12-Jan-2016 jcs

Add dwiic, a driver for the Synopsys DesignWare i2c controller found
on the Samsung ATIV Book 9 laptop. This initial version only
supports ACPI config/attachment.

Add ihidev, a HID-over-i2c driver largely based on uhidev. dwiic
handles attaching ihidev devices found in ACPI.

Add ims, a HID-over-i2c mouse/trackpad driver to get basic cursor
and button functionality from HID-compliant i2c trackpads.

ok kettenis deraadt


# 1.300 11-Jan-2016 kettenis

Make sdhc(4) attach to hardware IDs "80860F14" and "PNP0FFF".


# 1.299 09-Jan-2016 kettenis

Initial support for Hardware-Reduced ACPI. This skips the legacy mode checks
by emulating the PM_CNT register such that SCI_EN is always enabled. It also
skip attaching acpitimer(4) since the relevant registers are no longer there.

ok deraadt@, mlarkin@


# 1.298 06-Jan-2016 kettenis

Follow the spec and check the SCI_EN bit to see if ACPI has already
been enabled and don't bother enabling it again if it is. This should fix
systems that don't implement legacy mode and therefore set ACPI_ENABLE and
ACPI_DISABLE to zero.

ok mlarkin@, yasuoka@


# 1.297 23-Nov-2015 reyk

Back out the CRITICAL change, turn it back from 10% to 25%. Some
people with old/broken batteries didn't like this change, and the
underlying heuristics are wrong.


# 1.296 22-Nov-2015 reyk

The "CRITICAL" APM battery level, that is exported/faked by the ACPI
stack, is currently set to 25%. With 25% battery left, a typical
modern laptop will have at least one hour runtime left. It is an
arbitrary value, and other APM power layers in the kernel set CRITICAL
to something like 5% or 10%, so set it to 10% for now.

OK mlarkin@


# 1.295 28-Sep-2015 deraadt

In low-level suspend routines, set cold=2. In tsleep(), use this to
spit out a ddb trace to console. This should allow us to find suspend
or resume routines which break the rules. It depends on the console
output function being non-sleeping.... but that's another codepath which
should try to be safe when cold is set.
ok kettenis


# 1.294 06-Sep-2015 deraadt

clarify malloc arguments (base + length...)


# 1.293 28-Aug-2015 deraadt

fairly simple sizes for free(); ok tedu


# 1.292 20-Aug-2015 kettenis

Respect the access size when reading or writing to pci config space.
This version also makes sure writes are properly aligned, preventing
the panics seen with the previous incarnation of this change.

Fixes battery status passthrough in vmware, brightness keys on my x220
and who knows what else.

ok mlarkin@, deraadt@


Revision tags: OPENBSD_5_8_BASE
# 1.291 04-Aug-2015 deraadt

If we are setting ACPI_PM1_BM_RLD do it only once, and remember to
repeat this action at resume time.
ok mlarkin guenther


# 1.290 29-Jul-2015 deraadt

Something subtle in previous revision is locking up suspend or booting
on some laptops. Mailing lists threads mention x220 and Asus machines.
Reverting for now.


# 1.289 21-Jul-2015 kettenis

iRespect the access size when reading or writing to pci config space.
Fixes battery status passthrough in vmware and who knows what else.

Tested by many.

ok mlarkin@, deraadt@


# 1.288 17-Jul-2015 mlarkin

fix comments


# 1.287 26-Mar-2015 jsg

Use the 0x10 extended capability to determine if the trackstick
buttons are wired to the trackpad and need to be re-routed to the
trackstick. Without this change the buttons on 2015 thinkpads
get picked up as extended buttons that show up as scroll up/down.

Remove the X1 Carbon 2015 (LEN0048) and X250 (LEN0046) from the
top button area/soft buttons quirks list. Also avoid using the
quirk list entirely if the capability bit is set.

Based on work from jcs@ with feedback from mpi@
ok mpi@ jcs@


# 1.286 20-Mar-2015 kettenis

The ACPI standard says that a PCI function number of FFFF can be used to refer
to all functions on a device. This makes no sense in the context of accessing
PCI config space. Nevertheless stsp@ has a machine with AML that tries to
do config space reads from a device where _ADR encodes function number FFFF.
This is almost certainly buggy AML, but since Linux (and presumably Windows)
boot just fine on this machine we need to work around this. Linux effectively
treats function FFFF as function 7, which isn't present on this machine.
Instead we simulate a read from a non-existent device by returning a bit
pattern of all ones. Writes to function FFFF will still panic.

ok mlarkin@


# 1.285 16-Mar-2015 deraadt

Reduce use of SMALL_KERNEL in acpi, bringing acpiec driver and more gpe
handling into RAMDISK. This is now possible because the install media
has ample room. The goal is to reduce special cases where we may be
skipping (unknown) important operations...
ok mlarkin kettenis


# 1.284 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.283 07-Feb-2015 deraadt

New framework that allows hibernate to pass in entropy from it's fresh
boot.
ok mlarkin


# 1.282 06-Feb-2015 mlarkin

Fix a hibernate crash on some machines due to unmapping a page that
may not have been mapped previously (in the failure to hibernate case).

Also ensure that the lowmem ptp is mapped in all cases (not just MP).

ok kettenis


# 1.281 17-Jan-2015 deraadt

resume_randomness() before spinning up other cpus...


# 1.280 15-Jan-2015 jsg

Define a new wscons mouse type for Synaptics clickpad devices that lack
physical buttons. This will be used if the acpi pnpid for the mouse
matches a list provided by Synaptics found in Linux.

Combined with the xenocara changes this will remove the need for an xorg
config file for the x240/t440/t540 generation of thinkpads.

ok matthieu@ shadchin@ kettenis@


# 1.279 11-Jan-2015 kettenis

Tolerate checksum errors in ACPI tables (but complain about them). Apparently
there are machines out there with broken BIOSen with such checksum errors in
thier RSDT tables. Windows runs fine on these machines, so it probably does
not check. Linux ignores checksum errors as well (and prints a warning).

ok deraadt@, krw@


# 1.278 18-Dec-2014 deraadt

suspend and resume the RNG (at what looks like the right spots)
ok reyk djm


# 1.277 09-Dec-2014 doug

Replace some malloc(n*size,...) calls with mallocarray().

ok tedu@ deraadt@


# 1.276 06-Dec-2014 mpi

If "machdep.lidsuspend" is not set, do not go back to sleep if the lid
is still closed when resuming. This allows docked laptop to be resumed
without opening them.

ok mlarkin@


# 1.275 23-Nov-2014 mlarkin

If a machine has no _LIDs, then none can be open. This diff fixes a problem
where machines without any _LIDs were going back to sleep after hibernate
due to the recent suspend-if-lid-closed-on-resume diff. Noticed on qemu,
which doesn't have any _LIDs.

discussed with deraadt


# 1.274 18-Nov-2014 krw

Nuke yet more obvious #include duplications.

ok miod@


# 1.273 08-Nov-2014 mlarkin

If resuming from sleep (zzz/ZZZ) and the lid is still closed, go back to
sleep. This prevents accidental lid flex or slight opening in a backpack
from waking the machine up and leaving it resumed (powered on).

ok deraadt@


# 1.272 26-Sep-2014 kettenis

Rework piglet and pig allocation. Currently the piglet gets allocated
deep down in the suspend path, where it is really hard to recover from
allocation failure. So allocate the piglet early on in the suspend path.
Also change the piglet and piglet allocation functions to use km_alloc(9)
instead of doing pmemrange magic. This removes a bunch of code which, in the
case of the piglet allocation, is broken since it results in a NULL pointer
dereference. Also switch the piglet allocation to not wait. If we can't
allocate 16MB of phys contig memory on a halfway modern machine we're almost
certainly under a lot of memory pressure and we're better off not trying to
hibernate anyway.

ok mlarkin@


# 1.271 25-Sep-2014 kettenis

Bring back rev 1.249. Now that mp_setperf() has been fixed, it should be
safe to restore perflevel when resuming. Should fix the issues people
see with the processor running at full speed heating up the machine while
the apm command reports that the CPUs are running at a low speed.

ok dcoppa@, deraadt@


# 1.270 23-Sep-2014 kettenis

Release the acpi lock around calls to wsdisplay_suspend() and
wsdisplay_resume(). The X server may adjust the display brightness just
before/after the VT switch when suspending/resuming. In fact, it may
already be blocked adjusting the display brightness for some reason
unrelated to the suspend request.

The VT switch request will eventually time out, but the delay is annoying
and the failed VT switch will lead to problems after resume.

Needed for a future update of the xf86-video-intel driver.

ok mlarkin@


# 1.269 19-Sep-2014 kettenis

Use config_suspend_all(9).


# 1.268 08-Sep-2014 kettenis

Clear the PME Status bit on PCI devices in response to Device Wake events.
Fixes the interrupt storm on the 2009 Mac mini with WOL enabled on its
nfe(4) interface.

ok mlarkin@


Revision tags: OPENBSD_5_6_BASE
# 1.267 20-Jul-2014 mlarkin

Support hibernating to softraid crypto volumes.

much help and ok from deraadt@


# 1.266 16-Jul-2014 mlarkin

Reenable hibernate RLE support and flush+zero all memory after unpack.


# 1.265 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.264 12-Jul-2014 kettenis

Add a function to drop all clean pages on the page daemon queues and call
it when we hibernate.

ok mlarkin@, miod@, deraadt@


# 1.263 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.262 11-Jul-2014 mlarkin

Flush the buffercache to 16MB on hibernate and restore its previous max
size (kern.bufcachepercent) on resume, for better hibernate performance.

ok beck@


# 1.261 10-Jul-2014 blambert

remove unneeded header

ok mlarkin@


# 1.260 01-Jul-2014 kettenis

Adding an additonal delay after each access to SystemMemory or SystemIO
during autoconf makes no sense.

ok deraadt@


# 1.259 23-Jun-2014 kettenis

SystemMemory is used to access memory mapped registers on some machines, so we
must use the bus_space(9) API to access it instead of memcpy(9). Also make
sure we properly align access to fields that need more than byte alignment.

ok guenther@


# 1.258 21-May-2014 mlarkin

Fix invalid / missing format specifiers in several dnprintf statements
(used only when ACPI_DEBUG is enabled)

From a diff on tech@ by Fabian Raetz, with a minor change by me, tested
on i386 and amd64 with ACPI_DEBUG enabled and disabled.


# 1.257 25-Apr-2014 mlarkin

Defer the free of the hibernate structures until later, when it is safe to
do so.

Freeing the hibernate structures too early leads to inadvertent calls into
splx when we aren't ready for them yet. Found when trapping splx calls in
the resume path.

discussed with deraadt@ at m2k14


# 1.256 13-Apr-2014 mlarkin

Fix a badly worded debug message that implied the entire machine was going
to sleep, instead of just the acpi thread going to sleep.


# 1.255 31-Mar-2014 mpi

Change the order of operations during a suspend/resume cycle and
call bufq_quiesce() after executing the DVACT_QUIESCE handlers.

This should be safe since no disk nor controller drivers have such
handler but it will allow us to detach sd(4) devices attached to a
USB bus.

Another benefit pointed out by kettenis@ is that drivers that need
to read a firmware from the disk should be able to do it at resume
time in a DVACT_WAKEUP handler.

ok kettenis@, deraadt@


# 1.254 13-Mar-2014 dlg

get rid of the assumption that the head of the alldevs list is the
"mainbus" device. this breaks when mpath is enabled because it
attaches before mainbus and therefore takes the head position.

have autoconf provide device_mainbus() which looks up mainbus_cd,
and use that instead.

discussed with deraadt who just wants mpath stuff to move forward
despite there being many ways to shine this particular turd.


Revision tags: OPENBSD_5_5_BASE
# 1.253 21-Feb-2014 deraadt

Do the lid control further on in the resume sequence, since this is
AML, and will do WAITOK operations. While cold, that is a bad idea.
We seem to be safe with respect to the spec, I do not see any event
debounce issues here.
ok kettenis, tested by many


# 1.252 20-Jan-2014 claudio

Instead of checking sc->sc_revision <= 1 check that the
fadt->hdr_revision <= 1. This is a better check since it checks
the version of the proper descriptor. This allows the alix APU
to reboot since those boards have currently a wrong sc->sc_revision.
Committing this now so that it can be tested easier.
With and OK jsg@ also fine by derradt@


# 1.251 24-Dec-2013 kettenis

If the FADT has its SMI_CMD set to zero, assume we're only ACPI-only hardware
and don't need to disable SMI ownership of the ACPI hardware registers.

ok mlarkin@


# 1.250 23-Dec-2013 kettenis

Revert previous commit. It makes resume hang on some MP systems running
GENERIC.MP.


# 1.249 16-Dec-2013 kettenis

Restore hw.setperf upon resume like we do for apm(4) on i386. Seems we need
to do this fairly late in the resume to avoid hangs.

ok deraadt@, mlarkin@


# 1.248 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


# 1.247 06-Nov-2013 mpi

Add support for Power Resources for Dx states and the necessary hook
for PCI devices. This hook should be called twice, before and after
changing the power state of a PCI device.

Before setting the device to the new state, the ACPI layer will notify
every power resources linked to the device for that state and make sure
they are turned "_ON". After changing the state of the device, it will
decrement the reference of every power resources linked to that device
for the old state and turn them "_OFF" if they are no longer referenced.

This fixes the no-USB after resume problem seen on various ThinkPad,
problem initialy diagnosed with Alexander Polakov.

ok kettenis@, deraadt@


Revision tags: OPENBSD_5_4_BASE
# 1.246 01-Jun-2013 mlarkin

Stop printing ACPI wakeup devices in dmesg after the 16th wakeup device.

Helps with VMware VMs that report hundreds of wakeup devices.

ok reyk@, deraadt@


# 1.245 31-May-2013 bluhm

Fix the build for a kernel without wd and pciide in its config.
OK deraadt@ tedu@


# 1.244 30-May-2013 deraadt

Enforce ca_activate tree-walks over the entire heirarchy for all events,
cleaning up some shutdown-hook related code on the way.
(A few drivers related to sparc are still skipped at kettenis' request)
ok kettenis mlarkin, tested by many others too


# 1.243 18-Apr-2013 deraadt

pin the acpi thread to the BSP, because AML and SMI cannot be trusted. I
was certain we were doing this already, but it turns out no...


Revision tags: OPENBSD_5_3_BASE
# 1.242 24-Dec-2012 guenther

In acpiioctl(), Return though the splx() path if suser() returns an error

ok deraadt@


# 1.241 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


# 1.240 04-Oct-2012 ehrhardt

Use information provided by ACPI to attach secondary PCI host bridges.

ok kettenis@


# 1.239 07-Sep-2012 kettenis

Add a new function called acpi_pci_min_powerstate() that returns the lowest
power state for a PCI device for the power state (currently S3 or S4) we're
transitioning into. This function is probably too simplistic; there is a lot
of (silly) complexity in the ACPI spec.

ok deraadt@


Revision tags: OPENBSD_5_2_BASE
# 1.238 13-Jul-2012 pirofti

Disable acpivideo(4) on ASUS laptops.

There are some models that panic whenever an event is received.
Some cope, some don't. In order to get this right acpiec(4) requires
major rework. Until then let machines boot.

Discussed with deraadt@.


# 1.237 11-Jul-2012 mlarkin

for now, restrict hibernate to root in the acpi ioctl kernel interface.
normal rules for controlling this interface through apmd still apply.

ok deraadt@


# 1.236 09-Jul-2012 deraadt

tiny little oops in untested hibernate block


# 1.235 09-Jul-2012 deraadt

clean up ioctl() case statements


# 1.234 09-Jul-2012 deraadt

acpi_disable_onegpe() is not used; ok pirofti


# 1.233 24-May-2012 kettenis

If _MIN > _MAX consider a WordBus resource to be invalid. Fixes a panic on
the IBM x3550-M4.

ok mikeb@


# 1.232 29-Mar-2012 mlarkin

Missing #ifdef in previous diff


# 1.231 29-Mar-2012 mlarkin

Prevent hibernation attempts on machines with unsupported disk controllers.
The same check is also performed later in the hibernate sequence, but by
then it's too late to cleanly unwind (presently).

ok deraadt


# 1.230 26-Mar-2012 deraadt

simplify conditionals in acpi_sleep_state()
ok mlarkin


# 1.229 26-Mar-2012 deraadt

hook in the hibernate request code; half of this diff is from mlarkin
ok mlarkin


Revision tags: OPENBSD_5_1_BASE
# 1.228 20-Sep-2011 deraadt

Very early in suspend while the clock is still ticking realtime,
save the clock back to the rtc. The zaurus already did this.
ok phessler miod kettenis


Revision tags: OPENBSD_5_0_BASE
# 1.227 02-Jul-2011 nicm

kqueue attach functions should return an errno or 0, not a plain 1. Fix
the obvious cases to return EINVAL and ENXIO.

ok tedu deraadt


# 1.226 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.225 16-Jun-2011 pirofti

Import acpitoshiba driver.

This is originally written by Hiroyuki Aizu, ported and integrated
in our tree by Javier Vazquez <javama4 AT gmail> with tweaks and
suggestions by me.

Import prodded by deraadt@ and the lack of testers on tech@.


# 1.224 27-Apr-2011 jcs

attach acpithinkpad to newer lenovo models like the x120e

ok deraadt@


# 1.223 22-Apr-2011 jordan

Fix uninitialzied variables and formatting strings (-Wxxx errors)


Revision tags: OPENBSD_4_9_BASE
# 1.222 02-Jan-2011 jordan

Re-add code for ACPI taskqueue, eliminates need for state vars in acpi softc
Serialize processing ACPI tasks for sleep/powerdown/GPE event
ok deraadt@


# 1.221 31-Oct-2010 guenther

Revert last commit: it breaks resume on Thinkpads
"then please back it out" deraadt@


# 1.220 26-Oct-2010 jordan

Add task queue for ACPI gpe and notify handlers
This fixes eject on Dell Latitude dock and an issue on the Dell Mini
battery update.
ok deraadt


# 1.219 07-Oct-2010 jordan

Create separate functions for enabling wake and runtime gpes.
Remove old suspend/resume gpewalk function
ok deraadt, mlarkin


# 1.218 05-Oct-2010 jordan

split acpi_enable_onegpe to separate enable/disable functions
ok deraadt


# 1.217 05-Oct-2010 jordan

Added acpi_disable_allgpes function to clear GPE status
ok deraadt


# 1.216 05-Oct-2010 deraadt

clean up acpi_softc use more


# 1.215 05-Oct-2010 deraadt

delete a bit of #if 0 code


# 1.214 05-Oct-2010 deraadt

hide use of our global acpi_softc pointer a bit better


# 1.213 29-Sep-2010 deraadt

grab the acpi thread lock very early in acpi thread startup; not that it
is likely that anyone will race us... but why be careless.
ok kettenis


# 1.212 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.211 29-Aug-2010 jasper

- add a check for FWRITE here too.

ok miod@


# 1.210 27-Aug-2010 kettenis

Make PCI Power Management optional, and only enable it when acpi(4) attaches.

ok deraadt@


Revision tags: OPENBSD_4_8_BASE
# 1.209 08-Aug-2010 kettenis

Close race between the acpi thread and wscons ioctls running in process
context. We might rip this out post-release and replace it with a solution
that serializes things in a more obvious way.

ok deraadt@, miod@ and "those crazy texans who ok anything"


# 1.208 08-Aug-2010 deraadt

Report the acpi sleep states when on a RAMDISK; ok krw


# 1.207 07-Aug-2010 kettenis

Enforce acpi register access rescrictions.

ok mlarkin@, deraadt@, tested by marco@


# 1.206 07-Aug-2010 deraadt

upon resume, notify all the acpiac and acpibat drivers to update
their status. some laptops do not do this automatically.
ok kettenis canacar


# 1.205 06-Aug-2010 marco

Always call _PSW on _LID devices that support it to enable lid open to
resume the machine. Conversely disable it when the machine wakes up.

Tested by several ok deraadt


# 1.204 06-Aug-2010 deraadt

As part of a revamp of the PM1/GPE code, I write this diff to look for
unmanaged EN & STS bits in the PM1 register at interrupt time and report
them. As a side effect this splits the STS acknowledgement into two writes
(for power, and sleep) instead of one. The printf that is added (to spot
unmanaged STS bits) has not yet been triggered as far as we know.

Before the "write to PM1 registers at the right offset" diff went in, this
was not neccessary. But newer thinkpads do not have a working soft power
button without this diff.

We have no idea why.
ok mlarkin kettenis


# 1.203 05-Aug-2010 deraadt

PM1 power button events were generating 'fake events' to acpibtn PWRB,
but if there is no such device these were being tossed. Instead, arrange
for all power button handlers to flag a softc variable which is processed
by the thread; so that one place will know to request a powerdown (psignal
to init)
ok marco mlarkin


# 1.202 05-Aug-2010 deraadt

rename a variable which is annoyingly poorly named


# 1.201 05-Aug-2010 mlarkin

Write to the correct offset for various ACPI pmreg registers. Writing to
the correct register does properly clear the wake status bits and fixed
event bits, which allows some machines to have more than 1 suspend cycle.

bug spotted by deraadt@

tested by deraadt@ and myself on a half-dozen machines and others on
hackers@

ok deraadt@, marco@


# 1.200 05-Aug-2010 deraadt

Fix the power button. The pm1 register is 16 bits in size.
My mistake, a while back.
tested by krw


# 1.199 04-Aug-2010 kettenis

Print supported sleep states.

ok marco@, phessler@, deraadt@


# 1.198 03-Aug-2010 mlarkin

Use the proper flag for re-enabling certain hardware events (power btn,
sleep btn). The (incorrect) flag prevoiously used coincidentally had the
same value, so this shouldn't cause any different behavior than before.

tested on a variety of machines (i386, amd64, sp, mp) - no behavioral
change seen

ok deraadt@


# 1.197 28-Jul-2010 marco

Make all gasio failures look and behave the same.


# 1.196 28-Jul-2010 mlarkin

Delay a little longer on suspend before giving up. Fixes machines that
sometimes don't suspend because the sleep transition takes too long
(symptoms like failing to sleep but the backlight stays on).

ok deraadt@


# 1.195 27-Jul-2010 deraadt

remove XXX; EC can now be attached early when needed; ok jordan


# 1.194 27-Jul-2010 deraadt

more brackets feels good


# 1.193 27-Jul-2010 deraadt

XXX annotate a block in acpi_enter_sleep_state() that is pretty worrying


# 1.192 27-Jul-2010 deraadt

horrific indentation that nearly made me puke


# 1.191 27-Jul-2010 mlarkin

Set the sleeping indicator light on machines that support it, in case SMI
doesn't do it for us. Shows the 'moon' LED on older Thinkpads when
sleeping.

Tested on T43p, W500, T510 by me and on X61s by deraadt@.

ok deraadt@


# 1.190 27-Jul-2010 jordan

Early initialization of acpiec if ECDT table exists
Fixes hang when booting thinkpads while docked
ok deraadt


# 1.189 26-Jul-2010 deraadt

remove merge error which was commited


# 1.188 26-Jul-2010 pirofti

Add support for Sony ACPI hotkeys via a new driver: acpisony(4).

Currently it only works for the suspend button and tries to do right for
the brightness events, but I haven't found a Sony laptop that like to do
right so far.

In the future I want to make the brightness keys work on all Sony's and
also add support for the zoom hotkeys and whatever other funky keys I can
find on those things.

Okay deraadt@.


# 1.187 22-Jul-2010 deraadt

pretty large cleanup of SMALL_KERNEL


# 1.186 21-Jul-2010 deraadt

in the acpi GPE handler, late ack level interrupts not edge interrupts.
Duh.


# 1.185 21-Jul-2010 deraadt

Merge the edge and level gpe handlers into one (now possible since the
gpe structure tells us whether the gpe is edge or level)


# 1.184 21-Jul-2010 deraadt

delete #if 0 code we are not going to use; ok marco


# 1.183 20-Jul-2010 deraadt

make the mask code for acpi_enable_onegpe() match other similar code


# 1.182 20-Jul-2010 deraadt

Handle the acpi interrupt controller with proper edge/level handling,
wrapped in spltty to avoid racing against the interrupt controller.
Repair the gpe bit masking code while there, and do operations in the
right order
ok kettenis mlarkin, help from jordan at figuring out the order of
operations.


# 1.181 20-Jul-2010 matthew

When we're preparing to go to sleep, only call bufq_restart() if other
preparations failed.

ok kettenis@, thib@, deraadt@


# 1.180 19-Jul-2010 deraadt

permit compilation with ACPI_DEBUG


# 1.179 19-Jul-2010 deraadt

there is no need for a seperate dsdt_softc pointer; dsdt does not have a
softc but is just a part of the single acpi driver.
ok jordan


# 1.178 19-Jul-2010 deraadt

acpi_delay() is no longer needed; ok jordan


# 1.177 18-Jul-2010 mlarkin

Clear event status on resume, to avoid going back to sleep/reboot/shutdown
immediately after resume. Tested by a bunch of people on hackers@ on
various hardware. Fixes at least Toshiba NB200 and Gateway LT3103u.

ok marco


# 1.176 13-Jul-2010 deraadt

rename some thread related interfaces so they are logical


# 1.175 10-Jul-2010 jordan

Verify _ADR bdf is correct (some systems return 0xffff)
Works on Dell x9xx and Studio 1555


# 1.174 09-Jul-2010 jordan

Clear the noise


# 1.173 08-Jul-2010 jordan

Add mapping for ACPI device to PCI bdf (match autoconf tree)
Simplify resource parsing function to use buffer argument
Convert namespace linked lists to use queue macros
ok marco@, deraadt@


# 1.172 06-Jul-2010 deraadt

Use spltty() to lock downcalls from apm against the information being
modified by the acpi thread.
ok kettenis


# 1.171 06-Jul-2010 deraadt

Don't use a workq to do the suspend, because it races aginst the acpi
thread. Instead, just tell the acpi thread to do the suspend for us.
This makes apmd & zzz work correctly. While here, have acpithinkpad
attempt to post the event to apm before prompting the suspend itself.
ok kettenis marco mlarkin


# 1.170 05-Jul-2010 mlarkin

Reset register SCI_EN on resume. The ACPI spec clearly states we don't have
to do this, but many machines don't, or forget to, which leads to all sorts
of problems on resume (broken battery stats, overheating, failure to resume
more than once, etc).

tested by many, ok marco@, deraadt@


# 1.169 01-Jul-2010 thib

do bufq quiesce again, since vnd/the bufq_done call in
biodone has been fixed.


# 1.168 01-Jul-2010 jordan

Backout recent AML changes, breaks on R210 and others


# 1.167 01-Jul-2010 jordan

Simplified aml_parse_resource call, cleanup code
ok marco


# 1.166 01-Jul-2010 jordan

Fix subordinate bus # for multi-root PCI buses
ok marco, kettenis


# 1.165 30-Jun-2010 thib

Disable/partially backout the bufq quiesce changes as this
is causing havoc with vnds and release must be buildable.


# 1.164 29-Jun-2010 deraadt

Fix ramdisks (new code was placed inside a SMALL_KERNEL chunk)


# 1.163 29-Jun-2010 jordan

Add support for mapping ACPI to PCI devices
ok kettenis, deraadt


# 1.162 29-Jun-2010 kettenis

Call bufq_quiesce() on suspend to stop all I/O and bufq_restart on resume.
Makes suspend/resume work much better while doing disk I/O.

ok mlarkin@


# 1.161 29-Jun-2010 tedu

60 is a more reasonable guess for number of minutes in an hour than 100.
spotted by TeXitoi. ok deraadt


# 1.160 27-Jun-2010 jordan

Add _acpi_matchhids routine, precursor to pci device match


# 1.159 27-Jun-2010 mlarkin

Clear ACPI fixed event status on resume (power buttons/etc) so that some
machines don't immediately resume after suspending to S3.

Tested by kettenis@ and I on 5 or 6 machines, deraadt@ "yeah, looks good"


# 1.158 27-Jun-2010 jordan

Cleaned up acpi_maptable, adds to table list directly


# 1.157 07-Apr-2010 deraadt

If we have done a wsdisplay_suspend(), and ACPI decides it does not want to
suspend, we must roll back by calling wsdisplay_resume() or the virtual
console switching will be locked forever
ok oga


# 1.156 07-Apr-2010 kettenis

Make suspend/resume work on MP machines (running an MP kernel). Joint work
from mlarkin@ and me, with some amd64 fixes thrown in by deraadt@

ok marco@, deraadt@, pirofti@, mlarkin@


# 1.155 31-Mar-2010 kettenis

Bring /dev/apm support in line with apm(4). Make the suspend button send
a suspend request event to apmd(8) instead of suspending immediately. Also
keep track of whether /dev/apm and /dev/apmctl are currently open, such that
we can still suspend immediately if apmd(8) isn't running.

ok deraadt@, marco@, pirofti@, jsing@, oga@


# 1.154 30-Mar-2010 oga

Prevent the apmd/x races for good.

When we hit suspend time, go through all wsdisplays on the system. if
they are in mode MAPPED, but not MODE_DUMBFB then if possible do a full
vt switch to a !mapped vt, and prevent switching back until resume time.
This has to be called from MD code because this involves userland
running so that X can run the vt switch signal handler. This way, any
case where we are using the "poke registers from userland" model, we
will not be on the hardware when we go down, so the kernel can actually
handle thing properly.

Tested on several acpi laptops (by kettenis@ and ian@), x40 (me and
beck@ at LEAST) and zaurus (me). Maybe others, but if so I forgot who at
this time..

Idea from deraadt somewhere over the Faroe Islands (I thought of a
similar thing myself a while ago). Much prompting from him. Ok and
comments miod@


# 1.153 25-Mar-2010 oga

Fix & vs. && in the kqfilter code.

Based on a comparison with the apm code.

ok deraadt@, kettenis@


Revision tags: OPENBSD_4_7_BASE
# 1.152 26-Nov-2009 mlarkin

Visual (and on some machines, audio) feedback while the machine is
resuming via calls to aml _SST method. This also makes the "moon" LED on
thinkpads to blink during resume and cease when resume is completed.

ok pirofti@


# 1.151 26-Nov-2009 deraadt

Toggle into "cold" when we we disable interrupts, because tsleeping
in the AML interpreter and context switching into userland processes
tends to sometimes pop the ACPI trampoline page out of our address
space. Makes lots more machines work.
ok kettenis mlarkin


# 1.150 24-Nov-2009 jsg

in the resume path evaluate _WAK before dealing with device drivers
like the spec says, not after.

ok mlarkin@


# 1.149 24-Nov-2009 deraadt

abort suspends on SMP systems until that gets written; ok mlarkin


# 1.148 23-Nov-2009 mlarkin

pci should return the result of it's children's suspend/resume calls back
to its parent.

handle suspend failure case and unwind if devices have an activate function
that returns failure.

ok deraadt@, kettenis@


# 1.147 23-Nov-2009 mlarkin

acpi* devices need to call their children on suspend/resume.
ok deraadt@


# 1.146 23-Nov-2009 pirofti

Remove ACPI_SLEEP_ENABLED checks.

This enables by default the suspend/resume paths in the kernel.

Okay deraadt@.


# 1.145 23-Nov-2009 deraadt

knf and spacing that are super annoying


# 1.144 23-Nov-2009 mlarkin

Hook up Theo's new children suspend/resume function, as well as more ACPI
suspend/resume glue.

ok deraadt@


# 1.143 26-Oct-2009 deraadt

Do not do strncmp comparisons on dv_xname because longer device names which
look similar could arrive in the future. Instead, compare directly against
dv_cfdata->cf_driver->cd_name
Issue originally spotted by miod


# 1.142 26-Oct-2009 jordan

Verify checksum+address when loading ACPI tables.
Some systems had invalid entries in RSDT/XSDT.
ok marco@


# 1.141 23-Jul-2009 cnst

New aibs(4) driver for ASUSTeK AI Booster (ACPI ATK0110) hardware monitoring,
with sensor state support through limits provided by the ACPI.

Tested on several ASUS motherboards kindly networked by Sam Fourman Jr.

ok deraadt marco jordan


Revision tags: OPENBSD_4_6_BASE
# 1.140 03-Jun-2009 pirofti

Import acpi power resource driver. This is needed for suspend/resume logic.

Okay jordan@ and marco@.


# 1.139 03-Jun-2009 pirofti

Add acpivideo support. This adds brightness support for all laptops
except thinkpads, they will use the acpithinkpad driver. The driver is
also hooked into wsconsole. So brightness can be adjusted via:

$ wsconsctl display.brightness=<percentage>

This is very helpfull on some laptops that have a nasty bios and get two
steps instead of one when pressing the brightness button.

Tested on various dell, fujitsu, acer, samsung and other laptops.

Okay marco@, miod@. Suggestions from kettenis@.
Lots of reviews and help from miod@, thanks!


# 1.138 03-Jun-2009 jordan

Added ejectable bay for IDE cdroms
ok marco@


# 1.137 30-Apr-2009 marco

Oga changed apm ioctls and forgot to grep. Temporary work around by hitting
the sleep button when we get any request. This interface will chnage at
some point.
ok oga


# 1.136 19-Apr-2009 krw

Call acpi_sleep_walk() when powering down just like when entering
any other sleep state. This tells acpi wake devices to not wake up
the box until manual powering up resets their status. Makes my Dell
GX520 stay powered off in response to 'halt -p'.

In snaps for a while.

ok marco@ (several times) deraadt@


# 1.135 17-Apr-2009 pirofti

Add device ranking support for future sleep/wake developments.

okay marco@


# 1.134 11-Mar-2009 jordan

Cleanup.. use aml_evalinteger instead of aml_evalname
ok marco@


# 1.133 10-Mar-2009 jordan

Fix cut/paste error.. call _BFS not _PTS
ok marco@


Revision tags: OPENBSD_4_5_BASE
# 1.132 21-Feb-2009 marco

overzealous cut/paste; found by Vladimir Kirillov <proger@uaoug.org.ua>


# 1.131 20-Feb-2009 marco

Oops didn't mean for Debugger() to remain there.


# 1.130 19-Feb-2009 marco

suspend/resume bits so that we can develop this in tree. This is disabled.
code from mlarkin and me
help from art,toby,jordan and several others
ok jordan, go for it deraadt


# 1.129 10-Feb-2009 jordan

Fix null pointer for shutdown if acpi not enabled
ok marco@


# 1.128 04-Feb-2009 kettenis

Some systems that claim to be ACPI 2.0 apparently don't provide an XSDT
address. Make the code fall back on the RSDT on those systems.

From Joerg Niendorf <p3y80@internode.on.net>. Fixes PR 5986.


# 1.127 17-Jan-2009 krw

A couple of stray malloc/memset -> malloc(M_ZERO), a bit of knf and
lose a couple of unnecessary casts. No functional change.

Feedback from kettenis@

ok kettenis@ marco@


# 1.126 06-Nov-2008 marco

Collapse all sanity checks of hids into one function.

Originaly from jordan


Revision tags: OPENBSD_4_4_BASE
# 1.125 02-Jul-2008 fgsch

add acpivideo. for now almost an empty skel so others can work on
suspend/resume. eventually it will also manage output switching and
brightness where supported. prodded by marco@


# 1.124 11-Jun-2008 marco

Fix $OpenBSD$ for once and for all


# 1.123 08-Jun-2008 deraadt

aaa_dev was being set from an uninitialized variable; ok toby


# 1.122 07-Jun-2008 marco

Kill ACPIVERBOSE. It was never used and is worthless to boot.

comments jmc
ok toby


# 1.121 06-Jun-2008 marco

Shrink acpi some more for floppies and while here re-enable acpi on the
floppies. Fixes crazy sony vaio kettenis has.

tested by deraadt
ok deraadt jordan kettenis


# 1.120 01-Jun-2008 marco

Fix \n nit in SMALL_KERNEL.


# 1.119 01-Jun-2008 marco

Re-enable new parser. Lots of testing by various folks. Fits all media
now too.

ok kettenis deraadt


# 1.118 16-May-2008 dlg

back out jordans new parser.

it didnt have enough oks from the right people, it did break machines
that were previously working, and it bloated the RAMDISKS and install
media beyond the constraints of the media.

requested by deraadt@


# 1.117 14-May-2008 jordan

Adding new ACPI Parser code
Old guts of code still exists, needs to be torn out next
ok marco@
ok brad@


# 1.116 27-Apr-2008 jcs

add acpithinkpad(4), an acpi support driver for ibm/lenovo
thinkpads.

provides functionality that the bios did on older, non-acpi
thinkpads like respond to screen brightness keys and toggle the
bluetooth device on and off.

ok deraadt@ and marco@


# 1.115 24-Apr-2008 jsing

Introduce acpiasus(4), a driver for the ACPI based hotkeys found in many
ASUS laptops (including the ASUS EeePC) - largely based on NetBSD's
asus(4) driver. On the ASUS EeePC this allows us to enable/disable
wireless, change screen brightness and use the volume keys.

ok jsg@, weingart@


# 1.114 12-Apr-2008 kettenis

Get rid of acpi_s5 global variables; simply send SIGUSR2 instead of SUGUSR1
to tell init(8) to power down the machine.

ok krw@


Revision tags: OPENBSD_4_3_BASE
# 1.113 21-Jan-2008 jsg

Check against RESET_REG_SUP in FADT before doing the acpi reset dance
after all, not doing so appears to break more machines than it fixes.

ok marco@


# 1.112 05-Jan-2008 kettenis

Never attach more acpicpu(4) devices than cpu(4) devices. Always attach
the first acpicpu(4) device on non-MULTIPROCESSOR kernels, regardless of its
ACPI processor ID.

ok mikeb@


# 1.111 05-Dec-2007 deraadt

Hang acpi(4) below bios(4) instead of mainbus(4). This lets us move acpi(4)
before pcibios(4), and then.... if acpi(4) attaches, skip pcibios(4) since
it messes the machines up. Fixes claudio's HP dl320 G5, and almost assuredly
others.
ok kettenis


# 1.110 05-Dec-2007 deraadt

repair acpiopen/acpiclose and friends to handle the /dev/apmctl interface,
so that apmd(8) can actually work correctly against acpi(4), and thus,
even do the fancy -C/-A stuff; also tested by jmc


# 1.109 02-Dec-2007 fgsch

fix the trailing space for real now.


# 1.108 02-Dec-2007 fgsch

avoid trailing spaces on output; marco@ ok.


# 1.107 27-Nov-2007 kettenis

Fix off-by-one that made us attach acpicpu0 and acpicpu1 on GENERIC kernels.

ok marco@


# 1.106 25-Nov-2007 jsg

When available and ACPI is enabled use the ACPI reset method
to reboot. This is required for some quirky sis machines
and likely others to reboot.

Tested by many. 'commit early commit often' deraadt@


# 1.105 19-Nov-2007 deraadt

sigh, amd64 still uses that stupid acpi_enabled variable...


# 1.104 19-Nov-2007 deraadt

softc's come initialized to 0, you know...


# 1.103 19-Nov-2007 deraadt

store revision; from jsg


# 1.102 19-Nov-2007 kettenis

Initialize sc_facs earlier.

ok deraadt@, beck@, canacar@


# 1.101 16-Nov-2007 deraadt

fix the bus_space #define nightmare, so that amd64 and i386 are much more
uniform. as a result shared code like acpi needs less #ifdef's
ok marco kettenis


# 1.100 15-Nov-2007 deraadt

remove the #ifdef ACPI_ENABLE. the EC codepath is either safe enough (and
will be improved), or it is unsafe because AML assumes it must be there
ok almost everyone


# 1.99 15-Nov-2007 mikeb

Make acpicpu(4) attach to the existing CPUs only.

Problem reported by form@. Fix idea by kettenis@.
Lots of help from deraadt@. Tested by deraadt@, form@ and me.

ok deraadt marco


# 1.98 14-Nov-2007 deraadt

spacing


# 1.97 12-Nov-2007 deraadt

correct : vs , in attach-time messages


# 1.96 06-Nov-2007 deraadt

all ACPI_ENABLE kernels need acpi_interrupt()


# 1.95 06-Nov-2007 deraadt

move acpi_read_pmreg() and acpi_write_pmreg() out of SMALL_KERNEL so
that acpi ramdisks can compile (obvious solution)


# 1.94 05-Nov-2007 canacar

Establish the acpi interrupt earlier. Fixes problems with some
GENERIC.MP kernels not getting acpi interrupts. This is a
workaround until the real issue with late establishment is fixed.
ok weingart@, beck@, gwk@


# 1.93 03-Nov-2007 jordan

Added support for displaying wakeup devices
ok beck@,weingart@,gwk@


# 1.92 11-Oct-2007 kettenis

When walking the tree yo find _PRT methods, check whether the device is
actually enabled and functioning. Similar to what we do for _INI.

ok weingart@, beck@


# 1.91 08-Oct-2007 krw

More simple memset(,0,) -> M_ZERO changes. In this batch move to
size(*p) as the first malloc() parameter where p is declared locally
and thus easy to check. Add M_ZERO to gpe_table allocation in acpi.c
even though there is no obvious bzero or memset nearby.


# 1.90 13-Sep-2007 mk

Remove unnecessary memset() (see aml_evalnode(9)) and a bit of KNF.

ok toby


# 1.89 13-Sep-2007 weingart

Implement ACPI 6.5.1 spec tree walk for _STA and _INI.
Fixes mk's laptop. No regressions so far (thank you to the
testers).

ok gwk@, mk@, marco@


Revision tags: OPENBSD_4_2_BASE
# 1.88 17-Apr-2007 mk

Now that acpidock no longer hangs machines during boot (at least
according to the reports I've been getting) it should be safe to enable
it again, so remove the #if 0 stuff around it.

ok marco


# 1.87 11-Apr-2007 jordan

Added changes for C-State
ok marco@


# 1.86 26-Mar-2007 marco

Calculate minutes left more accurately.

From giovanni <qgiovanni@gmail.com>


Revision tags: OPENBSD_4_1_BASE
# 1.85 03-Mar-2007 mk

Stop looking for acpidock until we fix that stuff that breaks all pre-60
series ThinkPads with acpidock(4) enabled.


# 1.84 22-Feb-2007 marco

We were getting no interrupts in .MP. There are 2 fixes; one in
acpi_machdep.c and move the interrupt establishment down after ioapic
has been initialized.

ok jordan


# 1.83 21-Feb-2007 kettenis

Make an acpi-enabled GENERIC.MP work on systems that only support PIC mode.

ok marco@


# 1.82 19-Feb-2007 jordan

Added acpi_map_address function for mapping GAS/IO registers
ok marco@


# 1.81 18-Feb-2007 jordan

Added dynamic perfect hash for opcodes
ok marco@


# 1.80 18-Feb-2007 marco

Shave off another K pre compression.
Shuffle SMALL_KERNEL functions around so that we only have a single #ifndef


# 1.79 17-Feb-2007 marco

Add SMALL_KERNEL throught acpi to make it fit on boot media.
ok deraadt


# 1.78 13-Feb-2007 marco

Add smarter _INI code. This helps with some dissapearing devices.

ok kettenis jordan


# 1.77 31-Jan-2007 gwk

Introduce acpi_hasprocfvs thats "ACPI Has Processor Frequency and Voltage
Scalling" a global flag set by acicpu attach to indicate that ACPI is
handling fvs through the _PSS objects. This will fix disappearing
EST/powernow when booting kernels with acpi enabled, as acpicpu isnt
compiled into GENERIC yet. Also in cases where acpicpu is built in and
legacy EST/powernow work but acpi is missing the _PSS object, the legacy
routines will be used.

tested by dim@, and Nick Nauwelaerts <nick AT nauwelaerts.net>
ok marco@, dim@


# 1.76 22-Jan-2007 mk

Actually look for docking stations by looking for _DCK nodes.

ok marco


# 1.75 02-Jan-2007 marco

On some systems EC must be attached as the first device. Move EC from
generic _HID devices to its own routine to force first attach. Bug found
and debugged by joris@

Code mostly written by joris@


# 1.74 26-Dec-2006 marco

Add polling to devices that require it such as acpibat and acpitz.
Use the same tsleep/wakeup mechanism as the interrupt code to keep it non-
concurrent.


# 1.73 21-Dec-2006 deraadt

even more knf love


# 1.72 21-Dec-2006 marco

Remove some leftover acpiec gpe/interrupt code.


# 1.71 20-Dec-2006 deraadt

oops, name already used


# 1.70 20-Dec-2006 deraadt

rename __acpi_enable_gpe(); ok marco


# 1.69 20-Dec-2006 deraadt

acpi_unmap_pmregs() is not used, ok marco


# 1.68 19-Dec-2006 deraadt

simplify some low-level methods substantially (shrinking code); ok marco jordan


# 1.67 06-Dec-2006 mk

malloc(..., M_NOWAIT) with no NULL check is bad. If allocation fails,
complain, clean up, and bail out of attach.

input and ok gwk


# 1.66 28-Nov-2006 jordan

Fixed clearing arg for acpi_init_pic
ok kettenis@


# 1.65 27-Nov-2006 kettenis

Don't spam dmesg.


# 1.64 27-Nov-2006 jordan

Fixes for acpiprt routing
Post-parse fixup for namerefs
ok kettenis@


# 1.63 25-Nov-2006 marco

Don't handle acpiev events inside the gpe handler since this causes deadlocks.

ok jordan@


# 1.62 15-Nov-2006 kettenis

Hook up acpi apic interrupt routing support to generic acpi code.

ok jordan@


# 1.61 02-Nov-2006 marco

Kill acpiec attach function and add its functionality to the generic _HID
device attach

prodded and ok jordan


# 1.60 02-Nov-2006 marco

Fix comments to reflect reality.


# 1.59 19-Oct-2006 marco

Replace ACPI ioctls with APM ioctls. This makes ACPI talk the same protocol
as APM therefore userspace can now retrieve power information in the same
fashion as all other apm arches (zaurus, macppc, i386 etc).

To use force the use of acpi device like so: apmd -f /dev/acpi

ok jordan@


# 1.58 19-Oct-2006 jordan

Fixed GPE debug levels
Fixed ref counter on temporary bufferfields
DSDT parsing fully working on IBM Thinkpad
ok marco@


# 1.57 15-Oct-2006 jordan

Removed old GPE handler code remnants
ok marco@


# 1.56 12-Oct-2006 jordan

Massive ACPI changes
Fixed ACPI leaks
new ACPI API
ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.55 30-Jun-2006 gwk

KNF, No functional change,
"go at it" marco@


# 1.54 30-Jun-2006 jordan

Support for calling Embedded Controller via new GPE interface
ok marco@


# 1.53 30-Jun-2006 jordan

Added new code for GPE handling
Also works with ACPIEC; need to fixup acpiec.c to remove original interrupt handler
ok marco@


# 1.52 30-Jun-2006 jordan

Added offset argument for acpi read/write pmreg routines (new GPE code)
ok marco@


# 1.51 31-May-2006 todd

do not call acpi_powerdown() unless acpi has successfully initialized
ok gwk@ canacar@


# 1.50 29-May-2006 canacar

Add embedded controller support.
ok marco@ deraadt@ testing & ok gwk@


# 1.49 19-May-2006 canacar

Minimal thermal zone driver. Monitors thermal zone temperature,
shuts down the system if the 'critical temperature' is reached.
ok marco@


# 1.48 11-Apr-2006 marco

KNF.


# 1.47 11-Apr-2006 gwk

Add acpi_resume(struct acpi_softc *sc), which calls some AML methods,
detected at attach time, currently unused.

ok marco@


# 1.46 11-Apr-2006 gwk

Discover some ACPI sleep related methods, and call them at the
appropriate time in acpi_enter_sleep_state().

ok marco@


Revision tags: OPENBSD_3_9_BASE
# 1.45 26-Feb-2006 marco

Add acpicpu as a device.

ok jordan@


# 1.44 23-Feb-2006 marco

remove dead function.

ok jordan@


# 1.43 22-Feb-2006 marco

Print out all tables it found during autoconf. We'll need this data in
the future.


# 1.42 22-Feb-2006 marco

Make dmesg less verbose.

ok jordan.


# 1.41 22-Feb-2006 jordan

Added parsing of SSDT tables
ok marco@


# 1.40 22-Feb-2006 jordan

Added new aml_register to notify by device id
Moved powerdown code to button device
ok marco@


# 1.39 21-Feb-2006 marco

Readjust debug levels that crept in too low over the last few days.
KNF


# 1.38 21-Feb-2006 marco

Rig the power button so that the machine shuts down correctly.


# 1.37 20-Feb-2006 marco

Add acpibtn device. Remains disabled for now.


# 1.36 19-Feb-2006 jordan

Added code for aml_notify GPE notification callback
ok marco@


# 1.35 19-Feb-2006 grange

Fix acpi_enter_sleep_state() by replacing current ugly hack
with mostly correct sleeping state transition code.

ok brad@ marco@


# 1.34 19-Feb-2006 marco

jordan:
Enable interrupts.
Added peripheral code to make this work.

marco:
Replace softintr and other types of interrupts with a handler with context.
Fixed interrupt magic for GPEs.
Some KNF

This is work done by jordan and I but has to be a single commit.


# 1.33 17-Feb-2006 marco

Remove earlier introduced delays. We really only need them during autconf.

ok jordan@


# 1.32 17-Feb-2006 marco

Add a generic delay function as per the ACPI spec.

from jordan@


# 1.31 17-Feb-2006 marco

Use tsleep instead of delay when !cold.


# 1.30 17-Feb-2006 marco

Argh, we need delays between reads/writes. The backend reads (i2c most likely)
are so slow that values come back as 0 sometimes. We have to do this
despite the spec saying we shouldnt have to. Guess having a faster
parser than others can hurt sometimes.

Idea from jordan.


# 1.29 17-Feb-2006 jordan

Fixed code for handling _INI
Rearranged debug statements
ok marco@


# 1.28 16-Feb-2006 jordan

Added stub for calling _INI
Fixed aml_dowhile for NULL return
ok marco@


# 1.27 16-Feb-2006 jordan

Fixed parser/evaluator to retrieve battery info+state
Cleanup of parser
Dell laptops return battery status
ok marco@


# 1.26 06-Feb-2006 brad

fix a typo so this will compile on amd64.


# 1.25 04-Feb-2006 marco

When ACPI_ENABLED wasn't set but the devices were it had an unknown variable.

Pointed out by ian@.


# 1.24 03-Feb-2006 jordan

Rewrote aml parser yet again...
Fixed bus_space_map for i386/amd64
Added _BST and _BIF to acpibat to get status/info
ok marco@


# 1.23 20-Jan-2006 jordan

More evaluator changes
Removed old evaluator code
Fixed _HID value
ok marco@


# 1.22 19-Jan-2006 jordan

Added support for read/write of PCI Config space
Help jason@
ok marco@


# 1.21 18-Jan-2006 jordan

Added new evaluation routines
Added helper methods for buffer/field evaluation
Fixed broken indentation on dsdt.c

ok marco@


# 1.20 17-Jan-2006 jordan

Major overhaul of the aml parser. Evaluater temporarily disabled.

ok marco@


# 1.19 06-Jan-2006 grange

Check for ACPI control availability only if ACPI_ENABLE defined.


# 1.18 06-Jan-2006 grange

Informative error messages on attach, prettier dmesg.


# 1.17 06-Jan-2006 grange

printf -> dnprintf conversion for debug messages.


# 1.16 05-Jan-2006 grange

Find SLP_TYPx values and store them for future use in
acpi_enter_sleep_state().


# 1.15 05-Jan-2006 grange

Random KNF and cleanup


# 1.14 05-Jan-2006 grange

Provide symbolic names for the acpi sleeping states and use it.


# 1.13 28-Dec-2005 marco

Moved DEVNAME to a acpidev.h

ok jordan@


# 1.12 18-Dec-2005 sturm

add back comment about wrong code

ok marco


# 1.11 16-Dec-2005 marco

Remove test for kbd_reset. Prompted deraadt@, ok jordan@


# 1.10 16-Dec-2005 jordan

Added hooks for powerdown/power button press


# 1.9 16-Dec-2005 jordan

Cleaned up common power management register i/o
Added enter sleep state ioctl
Hooked power button interrupt
ok marco@


# 1.8 16-Dec-2005 marco

Remove autoconf chatter


# 1.7 16-Dec-2005 jordan

Moved DSDT AML parsing into parent ACPI driver
ok marco@


# 1.6 14-Dec-2005 marco

Add AC device glue.


# 1.5 14-Dec-2005 marco

Adjust debug printing levels to something more sane.


# 1.4 13-Dec-2005 marco

Add glue to attach devices to apci.


# 1.3 07-Dec-2005 marco

KNF


Revision tags: OPENBSD_3_8_BASE
# 1.2 10-Jul-2005 grange

Spacing cleanup.


# 1.1 02-Jun-2005 tholo

Start on a basic ACPI framework -- does not do much more than read out the
ACPI tables into kernel memory and attach ACPI and HPET timers currently.

In order to test this code, enabling the devices in GENERIC as well as
the ACPI_ENABLE option is needed. This code does not do any thermal
control yet, so this should be done with care depending on the platform.

In the tree so more people can contribute to making this more fully
featured.

Ok niklas@ grange@ tedu@


# 1.398 09-May-2021 kettenis

Some x86 machines advertise the "hardware reduced" ACPI feature, advertise
S4 and S5 support, but fail to populate the SLEEP_CONTROL_REG and
SLEEP_STATUS_REG descriptions in the FADT. An example of such a machine
is the ASUS Zenbook 14 UM433DA. Any attempt to powerdown the machine
will result in a kernel crash. It turns out that using the legacy ACPI PM
registers works fine on this machien. So fall back on those registers
if SLEEP_CONTROL_REG and/or SLEEP_STATUS_REG aren't provided.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_9_BASE
# 1.397 15-Mar-2021 patrick

Add acpi_iommu_device_map(), which replaces the DMA tag with one that
is blessed with IOMMU magic, if available. This is mainly for arm64,
since on amd64 and i386 the IOMMU only captures PCIe devices, as far
as I know, which uses the pci_probe_device_hook(). This though is for
non-PCI devices attached through ACPI.

ok kettenis@


# 1.396 07-Mar-2021 patrick

Pass standard DMA tag to acpi(4) table drivers.

ok kettenis@


# 1.395 27-Dec-2020 deraadt

acpi_map_address() cannot be wrapped by SMALL_KERNEL anymore, as
it is used by acpihpet.c


# 1.394 25-Dec-2020 visa

Refactor klist insertion and removal

Rename klist_{insert,remove}() to klist_{insert,remove}_locked().
These functions assume that the caller has locked the klist. The current
state of locking remains intact because the kernel lock is still used
with all klists.

Add new functions klist_insert() and klist_remove() that lock the klist
internally. This allows some code simplification.

OK mpi@


# 1.393 17-Dec-2020 kettenis

Fix some issues with referencing named ACPI nodes from Packages.
These references need to be resolved at runtime rather than when
they're parsed such that they pick up the right values for those nodes
which can be changed when for example _INI methods run.

The current approach is to replace these reference with a string that
names the node in question. The problem with that is that packages
can also contain normal strings. Which means that we need special
code that depends on the context where the package is used.

This diff takes a different approach by retaining a reference when
parsing. Code that uses a package will need to resolve this reference
but there is no ambiguiety anymore.

ok patrick@


# 1.392 05-Dec-2020 kettenis

Add support for the _EVT method to the GPIO support code.

ok patrick@


Revision tags: OPENBSD_6_8_BASE
# 1.391 27-Aug-2020 jmatthew

Reorder the acpi attach process so we look at Processor() CPU nodes
after Device() ones, since we should prefer the newer node type.
If we see any Device() nodes, don't attach acpicpu(4) to any Processor()
nodes if they're also present. This also makes acpitz(4) and
acpipwrres(4) devices attach slightly later.

ok kettenis@, also tested by jmc@


# 1.390 16-Aug-2020 gnezdo

Remove sc_maxgpe from acpi_softc which was never read from

Remove a dead conditional from acpi_init_gpes while there.

ok jcs@


# 1.389 21-Jul-2020 deraadt

acpi can use IPL_BIO (a low interrupt) since it only enqueues operations for
later processing. The use of a high interrupt will predate suspend/resume
efforts, we had to redesign acpi to be non-reentrant obviously
discussed with kettenis, in snaps for more than a week


# 1.388 24-Jun-2020 cheloha

kernel: use gettime(9)/getuptime(9) in lieu of time_second(9)/time_uptime(9)

time_second(9) and time_uptime(9) are widely used in the kernel to
quickly get the system UTC or system uptime as a time_t. However,
time_t is 64-bit everywhere, so it is not generally safe to use them
on 32-bit platforms: you have a split-read problem if your hardware
cannot perform atomic 64-bit reads.

This patch replaces time_second(9) with gettime(9), a safer successor
interface, throughout the kernel. Similarly, time_uptime(9) is replaced
with getuptime(9).

There is a performance cost on 32-bit platforms in exchange for
eliminating the split-read problem: instead of two register reads you
now have a lockless read loop to pull the values from the timehands.
This is really not *too* bad in the grand scheme of things, but
compared to what we were doing before it is several times slower.

There is no performance cost on 64-bit (__LP64__) platforms.

With input from visa@, dlg@, and tedu@.

Several bugs squashed by visa@.

ok kettenis@


# 1.387 10-Jun-2020 jca

Give userland a consistent battery and A/C state after resume

Previously we refreshed the state of acpiac(4) and acpibat(4)/acpisbs(4)
by queueing fake ACPI events to be processed by the acpi thread.
acpibat(4) using ACPIDEV_POLL meant that its status could be refreshed
first, resulting in an APM_POWER_CHANGE event sent to userland before
the state of acpiac(4) was up to date. Because of this, apmd(8) could
see A/C unplugged when it was in fact plugged, possibly leading to
a wrong decision.

Instead of using fake ACPI events, refresh the status of acpiac(4) and
acpibat(4)/acpisbs(4) right away at DVACT_WAKEUP time. To reuse the
existing refresh code of those drivers, make their ACPI *_notify()
handlers responsible for sending APM_POWER_CHANGE events instead. Also
queue an APM_POWER_CHANGE event right after resume to retain existing
behavior.

This fixes the consistency of data shown to userland, and makes those
drivers less special.

Initially discussed last year (!) with mpi@ at Miod's place.
Feedback and ok jcs@, deraadt@ agrees with the use of DVACT_WAKEUP here.


# 1.386 29-May-2020 deraadt

dev/rndvar.h no longer has statistical interfaces (removed during various
conversion steps). it only contains kernel prototypes for 4 interfaces,
all of which legitimately belong in sys/systm.h, which are already included
by all enqueue_randomness() users.


# 1.385 14-May-2020 kettenis

Bring back the acpi(4) changes. This time around only use the ACPI _CRS
information on ACPI 5.0 and later.

ok krw@, patrick@


# 1.384 11-May-2020 jca

More accurate remaining power reporting with disparate batteries

Overall remaining power was computed as the average of the remaining
power of each battery, in percents. This doesn't work with batteries of
different capacity, so use the total capacity and remaining power
instead.

Reported by lidstah, successful tests by lidstah and benno@, ok kettenis@


# 1.383 08-May-2020 kettenis

Move parsing of _CRS into acpi(4) and pass the parsed address and interrupt
information in struct acpi_attach_args.

ok mpi@


Revision tags: OPENBSD_6_7_BASE
# 1.382 14-Apr-2020 kettenis

Implement acpi_getprop(), the ACPI equivalent of OF_getprop().

ok patrick@


# 1.381 12-Apr-2020 kettenis

Add support for the _CCA method. This method indicates whether DMA is
cache-coherent or not. To implement this, acpi(4) gets two bus_dma tags
and passes the appropriate one when attaching devices based on _CCA.
On i386/amd64, where for all practical purpose DMA is always cache-coherent,
the two tags are the same. But on arm64 they are distinct.

ok patrick@


# 1.380 07-Apr-2020 visa

Abstract the head of knote lists. This allows extending the lists,
for example, with locking assertions.

OK mpi@, anton@


# 1.379 03-Apr-2020 mpi

Fix incoherencies of errors returned by various *kqfilter routines.

EOPNOTSUPP is returned when there's no "filterops" for a given fd.
EINVAL when the requested filter isn't supported by the "filterops".
ENXIO means the underlying device is no longer valid/present.

ok visa@


# 1.378 20-Feb-2020 visa

Replace field f_isfd with field f_flags in struct filterops to allow
adding more filter properties without cluttering the struct.

OK mpi@, anton@


# 1.377 31-Dec-2019 visa

Use C99 designated initializers with struct filterops. In addition,
make the structs const so that the data are put in .rodata.

OK mpi@, deraadt@, anton@, bluhm@


# 1.376 31-Dec-2019 mpi

Convert infinite sleeps to {m,t}sleep_nsec(9).

ok kn@


# 1.375 31-Dec-2019 jsg

include sys/sensors.h for sensor use
don't depend on indirect inclusion from acpidev.h


Revision tags: OPENBSD_6_6_BASE
# 1.374 07-Sep-2019 kettenis

Revert acpipci(4) changes. Too many systems are broken. We'll try again
after 6.6 as been released. The acpireg.h change stays behind.


# 1.373 28-Aug-2019 kettenis

Use ACPI information to attach PCI busses like we do on arm64. There are a
few additional quirks though, and attaching the PCI busses is delayed to
replicate the existing code more closely. That may be changed in the
future. Also tweak how we handle MSI support and respect to ACPI flag
that says we shouldn't attempt to use MSIs.

Some fallout is expected.

ok patrick@


# 1.372 27-Aug-2019 deraadt

On amd64 the bootup and resume paths for SP/BP/SP were not performing
msr/pat, clock-startup, firmware upgrade, fpu initialization, and cpu
soft-state initialization in the same order. That kind of chaos is
crazy and fragile. Improve the acpi resume call sequence and unify order
of the operations.


# 1.371 02-Jul-2019 jcs

acpi_set_gpehandler: turn edge arg into flags, add support for
passing GPE_DIRECT which directly executes the supplied callback and
clears a level interrupt rather than going through acpi_taskq which
can be slow when other driver callbacks are in the queue.

This should not be used for GPE handlers that need to do anything
ACPI-related.

ok kettenis


# 1.370 10-Jun-2019 kettenis

Print proper ACPI version number.

ok mlarkin@


# 1.369 08-Jun-2019 kettenis

When establishing the mapping between ACPI device nodes and PCI devices,
skip PCI host bridges and devices that aren't there.

ok patrick@


# 1.368 07-Jun-2019 kettenis

Introduce acpi_getsta() and use it to eliminate some duplicated code.

ok patrick@


# 1.367 12-May-2019 kettenis

Make sure the extended PM register descriptions actually exist before
using them. Fixes machines with ancient ACPI 1.0 (such as recent
hypervisors).

ok deraadt@


# 1.366 12-May-2019 kettenis

Add support for mapping ACPI PM registers using the "extended" gasio
representation from the FADT. Mostly fixes the Lanner NCA-1510.

ok lteo@, mlarkin@, deraadt@


# 1.365 11-May-2019 lteo

Fix incorrect error message.

ok kettenis@


# 1.364 08-May-2019 tedu

free size. the allocation is the header plus the length.


Revision tags: OPENBSD_6_5_BASE
# 1.363 04-Apr-2019 kettenis

Avoid potential null-pointer dereference. Found by Kent R. Spillner.

ok deraadt@, jsg@


# 1.362 20-Jan-2019 jsg

fix SMALL_KERNEL build after changes in rev 1.361
reported by naddy@ ok deraadt@


# 1.361 19-Jan-2019 tedu

Add a pwraction sysctl that controls what the power button does on acpi.
By default, nothing changes -- shutdown is initiated. But allows turning
power button into a sleep button if desired.
(grudging) ok from a few parties


# 1.360 26-Oct-2018 kettenis

Add acpipci(4) on amd64. For now this only calls the PCI-specific _OSC
method to let the ACPI implementation know what features we support.


Revision tags: OPENBSD_6_4_BASE
# 1.359 30-Aug-2018 kettenis

Move softbutton check before the check that skips attaching certain acpi
devices. Fixes regression caused by matching on _CID in addition to
matching on _HID.

ok matthieu@, mlarkin@


# 1.358 25-Aug-2018 kettenis

Match ACPI devices based on _CID if no match for _HID is found.

ok mpi@


# 1.357 19-Aug-2018 kettenis

Add support for multiple PCI segments. Only really implemented for arm64
for now as amd64/i386 firmware still caters for legacy OSes that only
support a single PCI segment.

ok patrick@


# 1.356 03-Aug-2018 kettenis

Let ahci(4) match on _CLS instead of _HID when attaching at acpi(4). Avoids
having to add many more _HID entries to the match table.

ok deraadt@, mlarkin@


# 1.355 10-Jul-2018 kettenis

Make legacy interrupts work in acpipci(4).

ok patrick@


# 1.354 05-Jul-2018 kettenis

Add acpipci(4), a driver that supports generic ECAM-compatible PCI host
bridges based on information provided by ACPI.

ok mlarkin@


# 1.353 04-Jul-2018 kettenis

Properly pass around the PCI "chipset tag" in acpi(4) and refactor
acpimcfg(4) to call an MD initialization functions that sets up a tag for
PCI ECAM.

ok guenther@, mlarkin@, krw@


# 1.352 01-Jul-2018 kettenis

Move acpi(4) attach glue into acpi_machdep.c.

ok guenther@, deraadt@


# 1.351 01-Jul-2018 kettenis

Add support for _DSD Device Properties.

ok mlarkin@


# 1.350 30-Jun-2018 kettenis

Add intr_enable() function, intended for MI use to amd64 and i386 and use
this in the acpi(4) suspend/resume code paths.

ok deraadt@


# 1.349 29-Jun-2018 kettenis

Use <stdint.h> types.

ok mpi@, jung@, krw@, deraadt@


# 1.348 29-Jun-2018 mlarkin

ACPI: Allow (with warning) GPE handler reassignment, instead of returning
a failure code.

ok kettenis


# 1.347 26-Jun-2018 mlarkin

knf


# 1.346 26-Jun-2018 kettenis

Let acpi(4) pass down the bus dma tag.

ok deraadt@


# 1.345 25-Jun-2018 kettenis

Reorganize acpi(4) code a bit in preparation for upcoming arm64 support.

ok deraadt@


# 1.344 20-May-2018 kettenis

Wake up the acpi thread after scheduling a gpio event.

ok mpi@


# 1.343 17-May-2018 kettenis

Sprinkle some #ifndef SMALL_KERNEL such that RAMDISK kernels cuild again.

pointed out by stsp@


# 1.342 17-May-2018 kettenis

Implement GenericSerialBus OpRegion support.

ok mlarkin@


# 1.341 27-Mar-2018 kettenis

Add acpicmos(4), a driver that implements SystemCMOS OperationRegion
access support. This fixes machines where the AML doesn't check whether
support for this OperationRegion type has been registered by the OS.

ok mlarkin@


Revision tags: OPENBSD_6_3_BASE
# 1.340 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.339 10-Feb-2018 deraadt

Syncronize filesystems to disk when suspending. Each mountpoint's vnodes
are pushed to disk. Dangling vnodes (unlinked files still in use) and
vnodes undergoing change by long-running syscalls are identified -- and
such filesystems are marked dirty on-disk while we are suspended (in case
power is lost, a fsck will be required). Filesystems without dangling or
busy vnodes are marked clean, resulting in faster boots following
"battery died" circumstances.
Tested by numerous developers, thanks for the feedback.


# 1.338 08-Feb-2018 deraadt

After flushing disks, try again to release all memory. We don't need to
save dirty memory to the hibernate space.


# 1.337 08-Feb-2018 deraadt

Toss all releaseable memory, because fragmentation can get in the way
of allocating the hibernate playpen.


# 1.336 08-Feb-2018 deraadt

sr_quiesce() is a new approach for ensuring that softraid drains
output to the disks.

This is part of a larger suspend/resume filesystem-safety diff, which
has been worked on for a couple of months already. Tests by job, krw,
beck, benno, and others. Sometimes even by snapshot users...


# 1.335 29-Nov-2017 kettenis

Add support for GPIO-signaled events.

ok mlarkin@


# 1.334 16-Nov-2017 jcs

Add PCI attachment for dwiic(4) needed by Intel 100 Series machines

ok kettenis


# 1.333 14-Nov-2017 kettenis

Add a few more PNP IDs to the skip lists.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.332 17-Aug-2017 stsp

Make dwiic(4) attach its ACPI parent dependencies.
This change makes the Asus E200HA keyboard work.

Original analysis and patch by Cesare Gargano
ok kettenis@


# 1.331 09-Aug-2017 dcoppa

Add "PNP0303" (8042 PS/2 Controller) to acpi_isa_hids[]

ok kettenis@


# 1.330 22-Jul-2017 jcs

acpibat: look for _BIX first before falling back on _BIF

ACPI 4.0 deprecated _BIF for battery status so some newer machines
are exporting _BIX which extends _BIF's package with a few extra
fields. When using _BIX, export the new discharge cycle count as a
sensor.

feedback from kettenis and anton
ok deraadt


# 1.329 20-Jul-2017 kettenis

Start a list of isa(4) device names such that we can avoid printing them
as "not configured".

ok mikeb@, millert@, deraadt@


# 1.328 08-Apr-2017 deraadt

Sensors are run as callbacks inside tasks. During suspend a sensor
could be running inside a driver that will be force-detached, or due
to tsleep end up disrupting the softstate/hardstate contract. At
suspend time, quisce all these callbacks by waiting for completion.
This issue has never been observed for real, but may be implicated in
suspend/resume failures.
ok kettenis guenther mlarkin


# 1.327 08-Apr-2017 deraadt

Two prototypes for acpi_maptable; merge to one place


Revision tags: OPENBSD_6_1_BASE
# 1.326 27-Mar-2017 deraadt

Now that hibernate_alloc() only has clean success/failure, don't
need to call hibernate_free() to clean up a partial mess.
ok mlarkin kettenis


# 1.325 13-Mar-2017 deraadt

allow ramdisks to build; ok jcs


# 1.324 12-Mar-2017 jcs

try to attach acpisbs first and if successful, don't attach acpibat


# 1.323 02-Mar-2017 natano

Add a new sysctl machdep.lidaction. The sysctl works as follows:

machdep.lidaction=0 # do nothing
machdep.lidaction=1 # suspend
machdep.lidaction=2 # hibernate

lidsuspend is just an alias for lidaction, so if you change one, the
other one will have the same value. The plan is to remove
machdep.lidsuspend eventually when people have upgraded their
/ets/sysctl.conf.

discussed with deraadt, who came up with the new MIB name
no objections mlarkin
ok stsp halex jcs


# 1.322 28-Feb-2017 natano

Untangle abstract sleep modes from ACPI sleep states. This paves the way
for further work in the area.

idea and ok deraadt
ok mlarkin


# 1.321 22-Feb-2017 jcs

add acpisbs, an acpi smart battery subsystem driver reading data
over smbus

currently disabled because it conflicts with acpibat

ok deraadt, kettenis


# 1.320 14-Jan-2017 kettenis

Revert aml_rdpciaddr changes; breaks several machines that were working
before.


# 1.319 11-Jan-2017 jsg

Increase the size of the buffers used for _HID and _CID strings to be
able to fit "Hyper_V_Gen_Counter_V1".

ok mlarkin@ deraadt@


# 1.318 08-Jan-2017 kettenis

Make aml_rdpciaddr() work for for devices behind bridges. The code now
uses the mapping between AML nodes and PCI devices that we establish.
Because _INIT methods may end up calling aml_rdpciaddr(), make sure we
create that mapping early. Also handle devices that aren't actually present.
These devices are now included in the mapping and reads will return an
all-ones patterm whereas writes are a no-op..

ok mlarkin@


# 1.317 25-Oct-2016 pirofti

Add the resource index to aml_parse_resource() callback function.

This is needed by an upcoming acpiec commit that handles machines breaking
the current ACPI specifications.

Change suggested by and ok kettenis@, guenther@


# 1.316 18-Sep-2016 guenther

If the APIC table (MADT) isn't present then ci_acpi_proc_id won't be set
when acpicpu tries to attach, leading to a NULL dereference. The ACPI
processor object on the Dells where this was reported is bogus and useless
anyway so apply the same test when deciding whether to attach to processor
objects as acpicpu would when matching them to cpus.

Problem reported by Eivind Eide (xenofil (at) gmail.com) and
Johan Huldtgren (johan+openbsd-bugs (at) huldtgren.com).
Testing by Eivind Eide.
ok kettenis@ mlarkin@


# 1.315 03-Sep-2016 naddy

Write the system time back to the RTC every 30 minutes.
This fixes the problem that long-running machines which were not
shut down properly would reboot with a badly offset system time.

hints and ok kettenis@


# 1.314 31-Aug-2016 mlarkin

Don't put a device in the wake device list if _STA says it's not there.

Fixes at least one machine that woke up from sleep / poweroff immediately.

ok kettenis@, tested by kettenis and martijn@


# 1.313 28-Jul-2016 kettenis

Store the acpi processor ID/UID in struct cpu_info, and use it to attach
acpicpu(4) drivers to the right cpu(4).

ok mlarkin@, guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.312 10-Jun-2016 kettenis

The power management timer has been made optional in ACPI 5.0A, so don't
attempt to attach acpitimer(4) if the timer isn't present.

Pointed out by jcs@

ok jcs@


# 1.311 08-May-2016 kettenis

Make sure devices listed by a device's _DEP method are attached before
we attach the device itself. The _DEP methode, introduced in ACPI 5.0,
is a hint that the device in question depends on OpRegion support from
the devices listed by _DEP, so we have to order them the proper way.
To prevent us from attaching those devices again when we encounter them
later walking down the device tree, keep track of our attempts to
attach a device driver to them using aflag in the aml_node structure.

ok guenther@


# 1.310 25-Apr-2016 pirofti

We attach acpihpet(4) and acpiec(4) without using the HID, so do not
report it in the dmesg.

OK deraadt@, mlarkin@.


# 1.309 02-Apr-2016 kettenis

Reduce dmesg spam by knocking out some acpi hardware IDs that don't need a
driver.

ok mpi@


# 1.308 29-Mar-2016 kettenis

Check if a device is present (using _STA) in acpi_foundhid. This prevents
us from reporting devices as "not configured" that aren't actually present,
and allows us to remove duplicated code from several drivers.

ok deraadt@, ok jsg@


# 1.307 29-Mar-2016 kettenis

Implement a cleaner way to prevent acpivout(4) from attaching if we have
a platform-specific method of controlling the backlight.

ok millert@, mlarkin@, jung@


# 1.306 28-Mar-2016 kettenis

Adding each and every new acpi device driver to acpi_foundhid() is
gettinga bit of the burden. And it really isn't how our autoconf
framework is supposed to work for busses that can be enumerated. All
the drivers already check for a matching _HID in their attach
function. So we can just drop the checks in acpi_foundhid() and let
the drivers sort themselves out.

Print a "not configured" line for devices for which we don't attach adriver.
This may be a bit much as there are devices for which we will not have a
driver. This will be removed if it causes too much dmesg spam.

ok deraadt@, mlarkin@


Revision tags: OPENBSD_5_9_BASE
# 1.305 17-Jan-2016 jsg

Take acpi_foundhid() outside of #ifndef SMALL_KERNEL so the ramdisk
kernel can find and attach dwiic(4) and sdhc(4).


# 1.304 17-Jan-2016 jsg

remove an unused global variable


# 1.303 14-Jan-2016 kettenis

Attach dwiic(4) on the Intel Bay Trail i2c controllers as well.


# 1.302 12-Jan-2016 kettenis

Improve emulation of PM registers on Hardware-redcued ACPI by emulating
SPL_TYPx, SLP_EN and WAK_STS using the new SLEEP_STATUS_REG and
SLEEP_CONTROL_REG registers. Makes the transition into S5 (powerdown) work
on the ASUS X205TA.

ok deraadt@, mlarkin@


# 1.301 12-Jan-2016 jcs

Add dwiic, a driver for the Synopsys DesignWare i2c controller found
on the Samsung ATIV Book 9 laptop. This initial version only
supports ACPI config/attachment.

Add ihidev, a HID-over-i2c driver largely based on uhidev. dwiic
handles attaching ihidev devices found in ACPI.

Add ims, a HID-over-i2c mouse/trackpad driver to get basic cursor
and button functionality from HID-compliant i2c trackpads.

ok kettenis deraadt


# 1.300 11-Jan-2016 kettenis

Make sdhc(4) attach to hardware IDs "80860F14" and "PNP0FFF".


# 1.299 09-Jan-2016 kettenis

Initial support for Hardware-Reduced ACPI. This skips the legacy mode checks
by emulating the PM_CNT register such that SCI_EN is always enabled. It also
skip attaching acpitimer(4) since the relevant registers are no longer there.

ok deraadt@, mlarkin@


# 1.298 06-Jan-2016 kettenis

Follow the spec and check the SCI_EN bit to see if ACPI has already
been enabled and don't bother enabling it again if it is. This should fix
systems that don't implement legacy mode and therefore set ACPI_ENABLE and
ACPI_DISABLE to zero.

ok mlarkin@, yasuoka@


# 1.297 23-Nov-2015 reyk

Back out the CRITICAL change, turn it back from 10% to 25%. Some
people with old/broken batteries didn't like this change, and the
underlying heuristics are wrong.


# 1.296 22-Nov-2015 reyk

The "CRITICAL" APM battery level, that is exported/faked by the ACPI
stack, is currently set to 25%. With 25% battery left, a typical
modern laptop will have at least one hour runtime left. It is an
arbitrary value, and other APM power layers in the kernel set CRITICAL
to something like 5% or 10%, so set it to 10% for now.

OK mlarkin@


# 1.295 28-Sep-2015 deraadt

In low-level suspend routines, set cold=2. In tsleep(), use this to
spit out a ddb trace to console. This should allow us to find suspend
or resume routines which break the rules. It depends on the console
output function being non-sleeping.... but that's another codepath which
should try to be safe when cold is set.
ok kettenis


# 1.294 06-Sep-2015 deraadt

clarify malloc arguments (base + length...)


# 1.293 28-Aug-2015 deraadt

fairly simple sizes for free(); ok tedu


# 1.292 20-Aug-2015 kettenis

Respect the access size when reading or writing to pci config space.
This version also makes sure writes are properly aligned, preventing
the panics seen with the previous incarnation of this change.

Fixes battery status passthrough in vmware, brightness keys on my x220
and who knows what else.

ok mlarkin@, deraadt@


Revision tags: OPENBSD_5_8_BASE
# 1.291 04-Aug-2015 deraadt

If we are setting ACPI_PM1_BM_RLD do it only once, and remember to
repeat this action at resume time.
ok mlarkin guenther


# 1.290 29-Jul-2015 deraadt

Something subtle in previous revision is locking up suspend or booting
on some laptops. Mailing lists threads mention x220 and Asus machines.
Reverting for now.


# 1.289 21-Jul-2015 kettenis

iRespect the access size when reading or writing to pci config space.
Fixes battery status passthrough in vmware and who knows what else.

Tested by many.

ok mlarkin@, deraadt@


# 1.288 17-Jul-2015 mlarkin

fix comments


# 1.287 26-Mar-2015 jsg

Use the 0x10 extended capability to determine if the trackstick
buttons are wired to the trackpad and need to be re-routed to the
trackstick. Without this change the buttons on 2015 thinkpads
get picked up as extended buttons that show up as scroll up/down.

Remove the X1 Carbon 2015 (LEN0048) and X250 (LEN0046) from the
top button area/soft buttons quirks list. Also avoid using the
quirk list entirely if the capability bit is set.

Based on work from jcs@ with feedback from mpi@
ok mpi@ jcs@


# 1.286 20-Mar-2015 kettenis

The ACPI standard says that a PCI function number of FFFF can be used to refer
to all functions on a device. This makes no sense in the context of accessing
PCI config space. Nevertheless stsp@ has a machine with AML that tries to
do config space reads from a device where _ADR encodes function number FFFF.
This is almost certainly buggy AML, but since Linux (and presumably Windows)
boot just fine on this machine we need to work around this. Linux effectively
treats function FFFF as function 7, which isn't present on this machine.
Instead we simulate a read from a non-existent device by returning a bit
pattern of all ones. Writes to function FFFF will still panic.

ok mlarkin@


# 1.285 16-Mar-2015 deraadt

Reduce use of SMALL_KERNEL in acpi, bringing acpiec driver and more gpe
handling into RAMDISK. This is now possible because the install media
has ample room. The goal is to reduce special cases where we may be
skipping (unknown) important operations...
ok mlarkin kettenis


# 1.284 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.283 07-Feb-2015 deraadt

New framework that allows hibernate to pass in entropy from it's fresh
boot.
ok mlarkin


# 1.282 06-Feb-2015 mlarkin

Fix a hibernate crash on some machines due to unmapping a page that
may not have been mapped previously (in the failure to hibernate case).

Also ensure that the lowmem ptp is mapped in all cases (not just MP).

ok kettenis


# 1.281 17-Jan-2015 deraadt

resume_randomness() before spinning up other cpus...


# 1.280 15-Jan-2015 jsg

Define a new wscons mouse type for Synaptics clickpad devices that lack
physical buttons. This will be used if the acpi pnpid for the mouse
matches a list provided by Synaptics found in Linux.

Combined with the xenocara changes this will remove the need for an xorg
config file for the x240/t440/t540 generation of thinkpads.

ok matthieu@ shadchin@ kettenis@


# 1.279 11-Jan-2015 kettenis

Tolerate checksum errors in ACPI tables (but complain about them). Apparently
there are machines out there with broken BIOSen with such checksum errors in
thier RSDT tables. Windows runs fine on these machines, so it probably does
not check. Linux ignores checksum errors as well (and prints a warning).

ok deraadt@, krw@


# 1.278 18-Dec-2014 deraadt

suspend and resume the RNG (at what looks like the right spots)
ok reyk djm


# 1.277 09-Dec-2014 doug

Replace some malloc(n*size,...) calls with mallocarray().

ok tedu@ deraadt@


# 1.276 06-Dec-2014 mpi

If "machdep.lidsuspend" is not set, do not go back to sleep if the lid
is still closed when resuming. This allows docked laptop to be resumed
without opening them.

ok mlarkin@


# 1.275 23-Nov-2014 mlarkin

If a machine has no _LIDs, then none can be open. This diff fixes a problem
where machines without any _LIDs were going back to sleep after hibernate
due to the recent suspend-if-lid-closed-on-resume diff. Noticed on qemu,
which doesn't have any _LIDs.

discussed with deraadt


# 1.274 18-Nov-2014 krw

Nuke yet more obvious #include duplications.

ok miod@


# 1.273 08-Nov-2014 mlarkin

If resuming from sleep (zzz/ZZZ) and the lid is still closed, go back to
sleep. This prevents accidental lid flex or slight opening in a backpack
from waking the machine up and leaving it resumed (powered on).

ok deraadt@


# 1.272 26-Sep-2014 kettenis

Rework piglet and pig allocation. Currently the piglet gets allocated
deep down in the suspend path, where it is really hard to recover from
allocation failure. So allocate the piglet early on in the suspend path.
Also change the piglet and piglet allocation functions to use km_alloc(9)
instead of doing pmemrange magic. This removes a bunch of code which, in the
case of the piglet allocation, is broken since it results in a NULL pointer
dereference. Also switch the piglet allocation to not wait. If we can't
allocate 16MB of phys contig memory on a halfway modern machine we're almost
certainly under a lot of memory pressure and we're better off not trying to
hibernate anyway.

ok mlarkin@


# 1.271 25-Sep-2014 kettenis

Bring back rev 1.249. Now that mp_setperf() has been fixed, it should be
safe to restore perflevel when resuming. Should fix the issues people
see with the processor running at full speed heating up the machine while
the apm command reports that the CPUs are running at a low speed.

ok dcoppa@, deraadt@


# 1.270 23-Sep-2014 kettenis

Release the acpi lock around calls to wsdisplay_suspend() and
wsdisplay_resume(). The X server may adjust the display brightness just
before/after the VT switch when suspending/resuming. In fact, it may
already be blocked adjusting the display brightness for some reason
unrelated to the suspend request.

The VT switch request will eventually time out, but the delay is annoying
and the failed VT switch will lead to problems after resume.

Needed for a future update of the xf86-video-intel driver.

ok mlarkin@


# 1.269 19-Sep-2014 kettenis

Use config_suspend_all(9).


# 1.268 08-Sep-2014 kettenis

Clear the PME Status bit on PCI devices in response to Device Wake events.
Fixes the interrupt storm on the 2009 Mac mini with WOL enabled on its
nfe(4) interface.

ok mlarkin@


Revision tags: OPENBSD_5_6_BASE
# 1.267 20-Jul-2014 mlarkin

Support hibernating to softraid crypto volumes.

much help and ok from deraadt@


# 1.266 16-Jul-2014 mlarkin

Reenable hibernate RLE support and flush+zero all memory after unpack.


# 1.265 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.264 12-Jul-2014 kettenis

Add a function to drop all clean pages on the page daemon queues and call
it when we hibernate.

ok mlarkin@, miod@, deraadt@


# 1.263 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.262 11-Jul-2014 mlarkin

Flush the buffercache to 16MB on hibernate and restore its previous max
size (kern.bufcachepercent) on resume, for better hibernate performance.

ok beck@


# 1.261 10-Jul-2014 blambert

remove unneeded header

ok mlarkin@


# 1.260 01-Jul-2014 kettenis

Adding an additonal delay after each access to SystemMemory or SystemIO
during autoconf makes no sense.

ok deraadt@


# 1.259 23-Jun-2014 kettenis

SystemMemory is used to access memory mapped registers on some machines, so we
must use the bus_space(9) API to access it instead of memcpy(9). Also make
sure we properly align access to fields that need more than byte alignment.

ok guenther@


# 1.258 21-May-2014 mlarkin

Fix invalid / missing format specifiers in several dnprintf statements
(used only when ACPI_DEBUG is enabled)

From a diff on tech@ by Fabian Raetz, with a minor change by me, tested
on i386 and amd64 with ACPI_DEBUG enabled and disabled.


# 1.257 25-Apr-2014 mlarkin

Defer the free of the hibernate structures until later, when it is safe to
do so.

Freeing the hibernate structures too early leads to inadvertent calls into
splx when we aren't ready for them yet. Found when trapping splx calls in
the resume path.

discussed with deraadt@ at m2k14


# 1.256 13-Apr-2014 mlarkin

Fix a badly worded debug message that implied the entire machine was going
to sleep, instead of just the acpi thread going to sleep.


# 1.255 31-Mar-2014 mpi

Change the order of operations during a suspend/resume cycle and
call bufq_quiesce() after executing the DVACT_QUIESCE handlers.

This should be safe since no disk nor controller drivers have such
handler but it will allow us to detach sd(4) devices attached to a
USB bus.

Another benefit pointed out by kettenis@ is that drivers that need
to read a firmware from the disk should be able to do it at resume
time in a DVACT_WAKEUP handler.

ok kettenis@, deraadt@


# 1.254 13-Mar-2014 dlg

get rid of the assumption that the head of the alldevs list is the
"mainbus" device. this breaks when mpath is enabled because it
attaches before mainbus and therefore takes the head position.

have autoconf provide device_mainbus() which looks up mainbus_cd,
and use that instead.

discussed with deraadt who just wants mpath stuff to move forward
despite there being many ways to shine this particular turd.


Revision tags: OPENBSD_5_5_BASE
# 1.253 21-Feb-2014 deraadt

Do the lid control further on in the resume sequence, since this is
AML, and will do WAITOK operations. While cold, that is a bad idea.
We seem to be safe with respect to the spec, I do not see any event
debounce issues here.
ok kettenis, tested by many


# 1.252 20-Jan-2014 claudio

Instead of checking sc->sc_revision <= 1 check that the
fadt->hdr_revision <= 1. This is a better check since it checks
the version of the proper descriptor. This allows the alix APU
to reboot since those boards have currently a wrong sc->sc_revision.
Committing this now so that it can be tested easier.
With and OK jsg@ also fine by derradt@


# 1.251 24-Dec-2013 kettenis

If the FADT has its SMI_CMD set to zero, assume we're only ACPI-only hardware
and don't need to disable SMI ownership of the ACPI hardware registers.

ok mlarkin@


# 1.250 23-Dec-2013 kettenis

Revert previous commit. It makes resume hang on some MP systems running
GENERIC.MP.


# 1.249 16-Dec-2013 kettenis

Restore hw.setperf upon resume like we do for apm(4) on i386. Seems we need
to do this fairly late in the resume to avoid hangs.

ok deraadt@, mlarkin@


# 1.248 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


# 1.247 06-Nov-2013 mpi

Add support for Power Resources for Dx states and the necessary hook
for PCI devices. This hook should be called twice, before and after
changing the power state of a PCI device.

Before setting the device to the new state, the ACPI layer will notify
every power resources linked to the device for that state and make sure
they are turned "_ON". After changing the state of the device, it will
decrement the reference of every power resources linked to that device
for the old state and turn them "_OFF" if they are no longer referenced.

This fixes the no-USB after resume problem seen on various ThinkPad,
problem initialy diagnosed with Alexander Polakov.

ok kettenis@, deraadt@


Revision tags: OPENBSD_5_4_BASE
# 1.246 01-Jun-2013 mlarkin

Stop printing ACPI wakeup devices in dmesg after the 16th wakeup device.

Helps with VMware VMs that report hundreds of wakeup devices.

ok reyk@, deraadt@


# 1.245 31-May-2013 bluhm

Fix the build for a kernel without wd and pciide in its config.
OK deraadt@ tedu@


# 1.244 30-May-2013 deraadt

Enforce ca_activate tree-walks over the entire heirarchy for all events,
cleaning up some shutdown-hook related code on the way.
(A few drivers related to sparc are still skipped at kettenis' request)
ok kettenis mlarkin, tested by many others too


# 1.243 18-Apr-2013 deraadt

pin the acpi thread to the BSP, because AML and SMI cannot be trusted. I
was certain we were doing this already, but it turns out no...


Revision tags: OPENBSD_5_3_BASE
# 1.242 24-Dec-2012 guenther

In acpiioctl(), Return though the splx() path if suser() returns an error

ok deraadt@


# 1.241 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


# 1.240 04-Oct-2012 ehrhardt

Use information provided by ACPI to attach secondary PCI host bridges.

ok kettenis@


# 1.239 07-Sep-2012 kettenis

Add a new function called acpi_pci_min_powerstate() that returns the lowest
power state for a PCI device for the power state (currently S3 or S4) we're
transitioning into. This function is probably too simplistic; there is a lot
of (silly) complexity in the ACPI spec.

ok deraadt@


Revision tags: OPENBSD_5_2_BASE
# 1.238 13-Jul-2012 pirofti

Disable acpivideo(4) on ASUS laptops.

There are some models that panic whenever an event is received.
Some cope, some don't. In order to get this right acpiec(4) requires
major rework. Until then let machines boot.

Discussed with deraadt@.


# 1.237 11-Jul-2012 mlarkin

for now, restrict hibernate to root in the acpi ioctl kernel interface.
normal rules for controlling this interface through apmd still apply.

ok deraadt@


# 1.236 09-Jul-2012 deraadt

tiny little oops in untested hibernate block


# 1.235 09-Jul-2012 deraadt

clean up ioctl() case statements


# 1.234 09-Jul-2012 deraadt

acpi_disable_onegpe() is not used; ok pirofti


# 1.233 24-May-2012 kettenis

If _MIN > _MAX consider a WordBus resource to be invalid. Fixes a panic on
the IBM x3550-M4.

ok mikeb@


# 1.232 29-Mar-2012 mlarkin

Missing #ifdef in previous diff


# 1.231 29-Mar-2012 mlarkin

Prevent hibernation attempts on machines with unsupported disk controllers.
The same check is also performed later in the hibernate sequence, but by
then it's too late to cleanly unwind (presently).

ok deraadt


# 1.230 26-Mar-2012 deraadt

simplify conditionals in acpi_sleep_state()
ok mlarkin


# 1.229 26-Mar-2012 deraadt

hook in the hibernate request code; half of this diff is from mlarkin
ok mlarkin


Revision tags: OPENBSD_5_1_BASE
# 1.228 20-Sep-2011 deraadt

Very early in suspend while the clock is still ticking realtime,
save the clock back to the rtc. The zaurus already did this.
ok phessler miod kettenis


Revision tags: OPENBSD_5_0_BASE
# 1.227 02-Jul-2011 nicm

kqueue attach functions should return an errno or 0, not a plain 1. Fix
the obvious cases to return EINVAL and ENXIO.

ok tedu deraadt


# 1.226 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.225 16-Jun-2011 pirofti

Import acpitoshiba driver.

This is originally written by Hiroyuki Aizu, ported and integrated
in our tree by Javier Vazquez <javama4 AT gmail> with tweaks and
suggestions by me.

Import prodded by deraadt@ and the lack of testers on tech@.


# 1.224 27-Apr-2011 jcs

attach acpithinkpad to newer lenovo models like the x120e

ok deraadt@


# 1.223 22-Apr-2011 jordan

Fix uninitialzied variables and formatting strings (-Wxxx errors)


Revision tags: OPENBSD_4_9_BASE
# 1.222 02-Jan-2011 jordan

Re-add code for ACPI taskqueue, eliminates need for state vars in acpi softc
Serialize processing ACPI tasks for sleep/powerdown/GPE event
ok deraadt@


# 1.221 31-Oct-2010 guenther

Revert last commit: it breaks resume on Thinkpads
"then please back it out" deraadt@


# 1.220 26-Oct-2010 jordan

Add task queue for ACPI gpe and notify handlers
This fixes eject on Dell Latitude dock and an issue on the Dell Mini
battery update.
ok deraadt


# 1.219 07-Oct-2010 jordan

Create separate functions for enabling wake and runtime gpes.
Remove old suspend/resume gpewalk function
ok deraadt, mlarkin


# 1.218 05-Oct-2010 jordan

split acpi_enable_onegpe to separate enable/disable functions
ok deraadt


# 1.217 05-Oct-2010 jordan

Added acpi_disable_allgpes function to clear GPE status
ok deraadt


# 1.216 05-Oct-2010 deraadt

clean up acpi_softc use more


# 1.215 05-Oct-2010 deraadt

delete a bit of #if 0 code


# 1.214 05-Oct-2010 deraadt

hide use of our global acpi_softc pointer a bit better


# 1.213 29-Sep-2010 deraadt

grab the acpi thread lock very early in acpi thread startup; not that it
is likely that anyone will race us... but why be careless.
ok kettenis


# 1.212 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.211 29-Aug-2010 jasper

- add a check for FWRITE here too.

ok miod@


# 1.210 27-Aug-2010 kettenis

Make PCI Power Management optional, and only enable it when acpi(4) attaches.

ok deraadt@


Revision tags: OPENBSD_4_8_BASE
# 1.209 08-Aug-2010 kettenis

Close race between the acpi thread and wscons ioctls running in process
context. We might rip this out post-release and replace it with a solution
that serializes things in a more obvious way.

ok deraadt@, miod@ and "those crazy texans who ok anything"


# 1.208 08-Aug-2010 deraadt

Report the acpi sleep states when on a RAMDISK; ok krw


# 1.207 07-Aug-2010 kettenis

Enforce acpi register access rescrictions.

ok mlarkin@, deraadt@, tested by marco@


# 1.206 07-Aug-2010 deraadt

upon resume, notify all the acpiac and acpibat drivers to update
their status. some laptops do not do this automatically.
ok kettenis canacar


# 1.205 06-Aug-2010 marco

Always call _PSW on _LID devices that support it to enable lid open to
resume the machine. Conversely disable it when the machine wakes up.

Tested by several ok deraadt


# 1.204 06-Aug-2010 deraadt

As part of a revamp of the PM1/GPE code, I write this diff to look for
unmanaged EN & STS bits in the PM1 register at interrupt time and report
them. As a side effect this splits the STS acknowledgement into two writes
(for power, and sleep) instead of one. The printf that is added (to spot
unmanaged STS bits) has not yet been triggered as far as we know.

Before the "write to PM1 registers at the right offset" diff went in, this
was not neccessary. But newer thinkpads do not have a working soft power
button without this diff.

We have no idea why.
ok mlarkin kettenis


# 1.203 05-Aug-2010 deraadt

PM1 power button events were generating 'fake events' to acpibtn PWRB,
but if there is no such device these were being tossed. Instead, arrange
for all power button handlers to flag a softc variable which is processed
by the thread; so that one place will know to request a powerdown (psignal
to init)
ok marco mlarkin


# 1.202 05-Aug-2010 deraadt

rename a variable which is annoyingly poorly named


# 1.201 05-Aug-2010 mlarkin

Write to the correct offset for various ACPI pmreg registers. Writing to
the correct register does properly clear the wake status bits and fixed
event bits, which allows some machines to have more than 1 suspend cycle.

bug spotted by deraadt@

tested by deraadt@ and myself on a half-dozen machines and others on
hackers@

ok deraadt@, marco@


# 1.200 05-Aug-2010 deraadt

Fix the power button. The pm1 register is 16 bits in size.
My mistake, a while back.
tested by krw


# 1.199 04-Aug-2010 kettenis

Print supported sleep states.

ok marco@, phessler@, deraadt@


# 1.198 03-Aug-2010 mlarkin

Use the proper flag for re-enabling certain hardware events (power btn,
sleep btn). The (incorrect) flag prevoiously used coincidentally had the
same value, so this shouldn't cause any different behavior than before.

tested on a variety of machines (i386, amd64, sp, mp) - no behavioral
change seen

ok deraadt@


# 1.197 28-Jul-2010 marco

Make all gasio failures look and behave the same.


# 1.196 28-Jul-2010 mlarkin

Delay a little longer on suspend before giving up. Fixes machines that
sometimes don't suspend because the sleep transition takes too long
(symptoms like failing to sleep but the backlight stays on).

ok deraadt@


# 1.195 27-Jul-2010 deraadt

remove XXX; EC can now be attached early when needed; ok jordan


# 1.194 27-Jul-2010 deraadt

more brackets feels good


# 1.193 27-Jul-2010 deraadt

XXX annotate a block in acpi_enter_sleep_state() that is pretty worrying


# 1.192 27-Jul-2010 deraadt

horrific indentation that nearly made me puke


# 1.191 27-Jul-2010 mlarkin

Set the sleeping indicator light on machines that support it, in case SMI
doesn't do it for us. Shows the 'moon' LED on older Thinkpads when
sleeping.

Tested on T43p, W500, T510 by me and on X61s by deraadt@.

ok deraadt@


# 1.190 27-Jul-2010 jordan

Early initialization of acpiec if ECDT table exists
Fixes hang when booting thinkpads while docked
ok deraadt


# 1.189 26-Jul-2010 deraadt

remove merge error which was commited


# 1.188 26-Jul-2010 pirofti

Add support for Sony ACPI hotkeys via a new driver: acpisony(4).

Currently it only works for the suspend button and tries to do right for
the brightness events, but I haven't found a Sony laptop that like to do
right so far.

In the future I want to make the brightness keys work on all Sony's and
also add support for the zoom hotkeys and whatever other funky keys I can
find on those things.

Okay deraadt@.


# 1.187 22-Jul-2010 deraadt

pretty large cleanup of SMALL_KERNEL


# 1.186 21-Jul-2010 deraadt

in the acpi GPE handler, late ack level interrupts not edge interrupts.
Duh.


# 1.185 21-Jul-2010 deraadt

Merge the edge and level gpe handlers into one (now possible since the
gpe structure tells us whether the gpe is edge or level)


# 1.184 21-Jul-2010 deraadt

delete #if 0 code we are not going to use; ok marco


# 1.183 20-Jul-2010 deraadt

make the mask code for acpi_enable_onegpe() match other similar code


# 1.182 20-Jul-2010 deraadt

Handle the acpi interrupt controller with proper edge/level handling,
wrapped in spltty to avoid racing against the interrupt controller.
Repair the gpe bit masking code while there, and do operations in the
right order
ok kettenis mlarkin, help from jordan at figuring out the order of
operations.


# 1.181 20-Jul-2010 matthew

When we're preparing to go to sleep, only call bufq_restart() if other
preparations failed.

ok kettenis@, thib@, deraadt@


# 1.180 19-Jul-2010 deraadt

permit compilation with ACPI_DEBUG


# 1.179 19-Jul-2010 deraadt

there is no need for a seperate dsdt_softc pointer; dsdt does not have a
softc but is just a part of the single acpi driver.
ok jordan


# 1.178 19-Jul-2010 deraadt

acpi_delay() is no longer needed; ok jordan


# 1.177 18-Jul-2010 mlarkin

Clear event status on resume, to avoid going back to sleep/reboot/shutdown
immediately after resume. Tested by a bunch of people on hackers@ on
various hardware. Fixes at least Toshiba NB200 and Gateway LT3103u.

ok marco


# 1.176 13-Jul-2010 deraadt

rename some thread related interfaces so they are logical


# 1.175 10-Jul-2010 jordan

Verify _ADR bdf is correct (some systems return 0xffff)
Works on Dell x9xx and Studio 1555


# 1.174 09-Jul-2010 jordan

Clear the noise


# 1.173 08-Jul-2010 jordan

Add mapping for ACPI device to PCI bdf (match autoconf tree)
Simplify resource parsing function to use buffer argument
Convert namespace linked lists to use queue macros
ok marco@, deraadt@


# 1.172 06-Jul-2010 deraadt

Use spltty() to lock downcalls from apm against the information being
modified by the acpi thread.
ok kettenis


# 1.171 06-Jul-2010 deraadt

Don't use a workq to do the suspend, because it races aginst the acpi
thread. Instead, just tell the acpi thread to do the suspend for us.
This makes apmd & zzz work correctly. While here, have acpithinkpad
attempt to post the event to apm before prompting the suspend itself.
ok kettenis marco mlarkin


# 1.170 05-Jul-2010 mlarkin

Reset register SCI_EN on resume. The ACPI spec clearly states we don't have
to do this, but many machines don't, or forget to, which leads to all sorts
of problems on resume (broken battery stats, overheating, failure to resume
more than once, etc).

tested by many, ok marco@, deraadt@


# 1.169 01-Jul-2010 thib

do bufq quiesce again, since vnd/the bufq_done call in
biodone has been fixed.


# 1.168 01-Jul-2010 jordan

Backout recent AML changes, breaks on R210 and others


# 1.167 01-Jul-2010 jordan

Simplified aml_parse_resource call, cleanup code
ok marco


# 1.166 01-Jul-2010 jordan

Fix subordinate bus # for multi-root PCI buses
ok marco, kettenis


# 1.165 30-Jun-2010 thib

Disable/partially backout the bufq quiesce changes as this
is causing havoc with vnds and release must be buildable.


# 1.164 29-Jun-2010 deraadt

Fix ramdisks (new code was placed inside a SMALL_KERNEL chunk)


# 1.163 29-Jun-2010 jordan

Add support for mapping ACPI to PCI devices
ok kettenis, deraadt


# 1.162 29-Jun-2010 kettenis

Call bufq_quiesce() on suspend to stop all I/O and bufq_restart on resume.
Makes suspend/resume work much better while doing disk I/O.

ok mlarkin@


# 1.161 29-Jun-2010 tedu

60 is a more reasonable guess for number of minutes in an hour than 100.
spotted by TeXitoi. ok deraadt


# 1.160 27-Jun-2010 jordan

Add _acpi_matchhids routine, precursor to pci device match


# 1.159 27-Jun-2010 mlarkin

Clear ACPI fixed event status on resume (power buttons/etc) so that some
machines don't immediately resume after suspending to S3.

Tested by kettenis@ and I on 5 or 6 machines, deraadt@ "yeah, looks good"


# 1.158 27-Jun-2010 jordan

Cleaned up acpi_maptable, adds to table list directly


# 1.157 07-Apr-2010 deraadt

If we have done a wsdisplay_suspend(), and ACPI decides it does not want to
suspend, we must roll back by calling wsdisplay_resume() or the virtual
console switching will be locked forever
ok oga


# 1.156 07-Apr-2010 kettenis

Make suspend/resume work on MP machines (running an MP kernel). Joint work
from mlarkin@ and me, with some amd64 fixes thrown in by deraadt@

ok marco@, deraadt@, pirofti@, mlarkin@


# 1.155 31-Mar-2010 kettenis

Bring /dev/apm support in line with apm(4). Make the suspend button send
a suspend request event to apmd(8) instead of suspending immediately. Also
keep track of whether /dev/apm and /dev/apmctl are currently open, such that
we can still suspend immediately if apmd(8) isn't running.

ok deraadt@, marco@, pirofti@, jsing@, oga@


# 1.154 30-Mar-2010 oga

Prevent the apmd/x races for good.

When we hit suspend time, go through all wsdisplays on the system. if
they are in mode MAPPED, but not MODE_DUMBFB then if possible do a full
vt switch to a !mapped vt, and prevent switching back until resume time.
This has to be called from MD code because this involves userland
running so that X can run the vt switch signal handler. This way, any
case where we are using the "poke registers from userland" model, we
will not be on the hardware when we go down, so the kernel can actually
handle thing properly.

Tested on several acpi laptops (by kettenis@ and ian@), x40 (me and
beck@ at LEAST) and zaurus (me). Maybe others, but if so I forgot who at
this time..

Idea from deraadt somewhere over the Faroe Islands (I thought of a
similar thing myself a while ago). Much prompting from him. Ok and
comments miod@


# 1.153 25-Mar-2010 oga

Fix & vs. && in the kqfilter code.

Based on a comparison with the apm code.

ok deraadt@, kettenis@


Revision tags: OPENBSD_4_7_BASE
# 1.152 26-Nov-2009 mlarkin

Visual (and on some machines, audio) feedback while the machine is
resuming via calls to aml _SST method. This also makes the "moon" LED on
thinkpads to blink during resume and cease when resume is completed.

ok pirofti@


# 1.151 26-Nov-2009 deraadt

Toggle into "cold" when we we disable interrupts, because tsleeping
in the AML interpreter and context switching into userland processes
tends to sometimes pop the ACPI trampoline page out of our address
space. Makes lots more machines work.
ok kettenis mlarkin


# 1.150 24-Nov-2009 jsg

in the resume path evaluate _WAK before dealing with device drivers
like the spec says, not after.

ok mlarkin@


# 1.149 24-Nov-2009 deraadt

abort suspends on SMP systems until that gets written; ok mlarkin


# 1.148 23-Nov-2009 mlarkin

pci should return the result of it's children's suspend/resume calls back
to its parent.

handle suspend failure case and unwind if devices have an activate function
that returns failure.

ok deraadt@, kettenis@


# 1.147 23-Nov-2009 mlarkin

acpi* devices need to call their children on suspend/resume.
ok deraadt@


# 1.146 23-Nov-2009 pirofti

Remove ACPI_SLEEP_ENABLED checks.

This enables by default the suspend/resume paths in the kernel.

Okay deraadt@.


# 1.145 23-Nov-2009 deraadt

knf and spacing that are super annoying


# 1.144 23-Nov-2009 mlarkin

Hook up Theo's new children suspend/resume function, as well as more ACPI
suspend/resume glue.

ok deraadt@


# 1.143 26-Oct-2009 deraadt

Do not do strncmp comparisons on dv_xname because longer device names which
look similar could arrive in the future. Instead, compare directly against
dv_cfdata->cf_driver->cd_name
Issue originally spotted by miod


# 1.142 26-Oct-2009 jordan

Verify checksum+address when loading ACPI tables.
Some systems had invalid entries in RSDT/XSDT.
ok marco@


# 1.141 23-Jul-2009 cnst

New aibs(4) driver for ASUSTeK AI Booster (ACPI ATK0110) hardware monitoring,
with sensor state support through limits provided by the ACPI.

Tested on several ASUS motherboards kindly networked by Sam Fourman Jr.

ok deraadt marco jordan


Revision tags: OPENBSD_4_6_BASE
# 1.140 03-Jun-2009 pirofti

Import acpi power resource driver. This is needed for suspend/resume logic.

Okay jordan@ and marco@.


# 1.139 03-Jun-2009 pirofti

Add acpivideo support. This adds brightness support for all laptops
except thinkpads, they will use the acpithinkpad driver. The driver is
also hooked into wsconsole. So brightness can be adjusted via:

$ wsconsctl display.brightness=<percentage>

This is very helpfull on some laptops that have a nasty bios and get two
steps instead of one when pressing the brightness button.

Tested on various dell, fujitsu, acer, samsung and other laptops.

Okay marco@, miod@. Suggestions from kettenis@.
Lots of reviews and help from miod@, thanks!


# 1.138 03-Jun-2009 jordan

Added ejectable bay for IDE cdroms
ok marco@


# 1.137 30-Apr-2009 marco

Oga changed apm ioctls and forgot to grep. Temporary work around by hitting
the sleep button when we get any request. This interface will chnage at
some point.
ok oga


# 1.136 19-Apr-2009 krw

Call acpi_sleep_walk() when powering down just like when entering
any other sleep state. This tells acpi wake devices to not wake up
the box until manual powering up resets their status. Makes my Dell
GX520 stay powered off in response to 'halt -p'.

In snaps for a while.

ok marco@ (several times) deraadt@


# 1.135 17-Apr-2009 pirofti

Add device ranking support for future sleep/wake developments.

okay marco@


# 1.134 11-Mar-2009 jordan

Cleanup.. use aml_evalinteger instead of aml_evalname
ok marco@


# 1.133 10-Mar-2009 jordan

Fix cut/paste error.. call _BFS not _PTS
ok marco@


Revision tags: OPENBSD_4_5_BASE
# 1.132 21-Feb-2009 marco

overzealous cut/paste; found by Vladimir Kirillov <proger@uaoug.org.ua>


# 1.131 20-Feb-2009 marco

Oops didn't mean for Debugger() to remain there.


# 1.130 19-Feb-2009 marco

suspend/resume bits so that we can develop this in tree. This is disabled.
code from mlarkin and me
help from art,toby,jordan and several others
ok jordan, go for it deraadt


# 1.129 10-Feb-2009 jordan

Fix null pointer for shutdown if acpi not enabled
ok marco@


# 1.128 04-Feb-2009 kettenis

Some systems that claim to be ACPI 2.0 apparently don't provide an XSDT
address. Make the code fall back on the RSDT on those systems.

From Joerg Niendorf <p3y80@internode.on.net>. Fixes PR 5986.


# 1.127 17-Jan-2009 krw

A couple of stray malloc/memset -> malloc(M_ZERO), a bit of knf and
lose a couple of unnecessary casts. No functional change.

Feedback from kettenis@

ok kettenis@ marco@


# 1.126 06-Nov-2008 marco

Collapse all sanity checks of hids into one function.

Originaly from jordan


Revision tags: OPENBSD_4_4_BASE
# 1.125 02-Jul-2008 fgsch

add acpivideo. for now almost an empty skel so others can work on
suspend/resume. eventually it will also manage output switching and
brightness where supported. prodded by marco@


# 1.124 11-Jun-2008 marco

Fix $OpenBSD$ for once and for all


# 1.123 08-Jun-2008 deraadt

aaa_dev was being set from an uninitialized variable; ok toby


# 1.122 07-Jun-2008 marco

Kill ACPIVERBOSE. It was never used and is worthless to boot.

comments jmc
ok toby


# 1.121 06-Jun-2008 marco

Shrink acpi some more for floppies and while here re-enable acpi on the
floppies. Fixes crazy sony vaio kettenis has.

tested by deraadt
ok deraadt jordan kettenis


# 1.120 01-Jun-2008 marco

Fix \n nit in SMALL_KERNEL.


# 1.119 01-Jun-2008 marco

Re-enable new parser. Lots of testing by various folks. Fits all media
now too.

ok kettenis deraadt


# 1.118 16-May-2008 dlg

back out jordans new parser.

it didnt have enough oks from the right people, it did break machines
that were previously working, and it bloated the RAMDISKS and install
media beyond the constraints of the media.

requested by deraadt@


# 1.117 14-May-2008 jordan

Adding new ACPI Parser code
Old guts of code still exists, needs to be torn out next
ok marco@
ok brad@


# 1.116 27-Apr-2008 jcs

add acpithinkpad(4), an acpi support driver for ibm/lenovo
thinkpads.

provides functionality that the bios did on older, non-acpi
thinkpads like respond to screen brightness keys and toggle the
bluetooth device on and off.

ok deraadt@ and marco@


# 1.115 24-Apr-2008 jsing

Introduce acpiasus(4), a driver for the ACPI based hotkeys found in many
ASUS laptops (including the ASUS EeePC) - largely based on NetBSD's
asus(4) driver. On the ASUS EeePC this allows us to enable/disable
wireless, change screen brightness and use the volume keys.

ok jsg@, weingart@


# 1.114 12-Apr-2008 kettenis

Get rid of acpi_s5 global variables; simply send SIGUSR2 instead of SUGUSR1
to tell init(8) to power down the machine.

ok krw@


Revision tags: OPENBSD_4_3_BASE
# 1.113 21-Jan-2008 jsg

Check against RESET_REG_SUP in FADT before doing the acpi reset dance
after all, not doing so appears to break more machines than it fixes.

ok marco@


# 1.112 05-Jan-2008 kettenis

Never attach more acpicpu(4) devices than cpu(4) devices. Always attach
the first acpicpu(4) device on non-MULTIPROCESSOR kernels, regardless of its
ACPI processor ID.

ok mikeb@


# 1.111 05-Dec-2007 deraadt

Hang acpi(4) below bios(4) instead of mainbus(4). This lets us move acpi(4)
before pcibios(4), and then.... if acpi(4) attaches, skip pcibios(4) since
it messes the machines up. Fixes claudio's HP dl320 G5, and almost assuredly
others.
ok kettenis


# 1.110 05-Dec-2007 deraadt

repair acpiopen/acpiclose and friends to handle the /dev/apmctl interface,
so that apmd(8) can actually work correctly against acpi(4), and thus,
even do the fancy -C/-A stuff; also tested by jmc


# 1.109 02-Dec-2007 fgsch

fix the trailing space for real now.


# 1.108 02-Dec-2007 fgsch

avoid trailing spaces on output; marco@ ok.


# 1.107 27-Nov-2007 kettenis

Fix off-by-one that made us attach acpicpu0 and acpicpu1 on GENERIC kernels.

ok marco@


# 1.106 25-Nov-2007 jsg

When available and ACPI is enabled use the ACPI reset method
to reboot. This is required for some quirky sis machines
and likely others to reboot.

Tested by many. 'commit early commit often' deraadt@


# 1.105 19-Nov-2007 deraadt

sigh, amd64 still uses that stupid acpi_enabled variable...


# 1.104 19-Nov-2007 deraadt

softc's come initialized to 0, you know...


# 1.103 19-Nov-2007 deraadt

store revision; from jsg


# 1.102 19-Nov-2007 kettenis

Initialize sc_facs earlier.

ok deraadt@, beck@, canacar@


# 1.101 16-Nov-2007 deraadt

fix the bus_space #define nightmare, so that amd64 and i386 are much more
uniform. as a result shared code like acpi needs less #ifdef's
ok marco kettenis


# 1.100 15-Nov-2007 deraadt

remove the #ifdef ACPI_ENABLE. the EC codepath is either safe enough (and
will be improved), or it is unsafe because AML assumes it must be there
ok almost everyone


# 1.99 15-Nov-2007 mikeb

Make acpicpu(4) attach to the existing CPUs only.

Problem reported by form@. Fix idea by kettenis@.
Lots of help from deraadt@. Tested by deraadt@, form@ and me.

ok deraadt marco


# 1.98 14-Nov-2007 deraadt

spacing


# 1.97 12-Nov-2007 deraadt

correct : vs , in attach-time messages


# 1.96 06-Nov-2007 deraadt

all ACPI_ENABLE kernels need acpi_interrupt()


# 1.95 06-Nov-2007 deraadt

move acpi_read_pmreg() and acpi_write_pmreg() out of SMALL_KERNEL so
that acpi ramdisks can compile (obvious solution)


# 1.94 05-Nov-2007 canacar

Establish the acpi interrupt earlier. Fixes problems with some
GENERIC.MP kernels not getting acpi interrupts. This is a
workaround until the real issue with late establishment is fixed.
ok weingart@, beck@, gwk@


# 1.93 03-Nov-2007 jordan

Added support for displaying wakeup devices
ok beck@,weingart@,gwk@


# 1.92 11-Oct-2007 kettenis

When walking the tree yo find _PRT methods, check whether the device is
actually enabled and functioning. Similar to what we do for _INI.

ok weingart@, beck@


# 1.91 08-Oct-2007 krw

More simple memset(,0,) -> M_ZERO changes. In this batch move to
size(*p) as the first malloc() parameter where p is declared locally
and thus easy to check. Add M_ZERO to gpe_table allocation in acpi.c
even though there is no obvious bzero or memset nearby.


# 1.90 13-Sep-2007 mk

Remove unnecessary memset() (see aml_evalnode(9)) and a bit of KNF.

ok toby


# 1.89 13-Sep-2007 weingart

Implement ACPI 6.5.1 spec tree walk for _STA and _INI.
Fixes mk's laptop. No regressions so far (thank you to the
testers).

ok gwk@, mk@, marco@


Revision tags: OPENBSD_4_2_BASE
# 1.88 17-Apr-2007 mk

Now that acpidock no longer hangs machines during boot (at least
according to the reports I've been getting) it should be safe to enable
it again, so remove the #if 0 stuff around it.

ok marco


# 1.87 11-Apr-2007 jordan

Added changes for C-State
ok marco@


# 1.86 26-Mar-2007 marco

Calculate minutes left more accurately.

From giovanni <qgiovanni@gmail.com>


Revision tags: OPENBSD_4_1_BASE
# 1.85 03-Mar-2007 mk

Stop looking for acpidock until we fix that stuff that breaks all pre-60
series ThinkPads with acpidock(4) enabled.


# 1.84 22-Feb-2007 marco

We were getting no interrupts in .MP. There are 2 fixes; one in
acpi_machdep.c and move the interrupt establishment down after ioapic
has been initialized.

ok jordan


# 1.83 21-Feb-2007 kettenis

Make an acpi-enabled GENERIC.MP work on systems that only support PIC mode.

ok marco@


# 1.82 19-Feb-2007 jordan

Added acpi_map_address function for mapping GAS/IO registers
ok marco@


# 1.81 18-Feb-2007 jordan

Added dynamic perfect hash for opcodes
ok marco@


# 1.80 18-Feb-2007 marco

Shave off another K pre compression.
Shuffle SMALL_KERNEL functions around so that we only have a single #ifndef


# 1.79 17-Feb-2007 marco

Add SMALL_KERNEL throught acpi to make it fit on boot media.
ok deraadt


# 1.78 13-Feb-2007 marco

Add smarter _INI code. This helps with some dissapearing devices.

ok kettenis jordan


# 1.77 31-Jan-2007 gwk

Introduce acpi_hasprocfvs thats "ACPI Has Processor Frequency and Voltage
Scalling" a global flag set by acicpu attach to indicate that ACPI is
handling fvs through the _PSS objects. This will fix disappearing
EST/powernow when booting kernels with acpi enabled, as acpicpu isnt
compiled into GENERIC yet. Also in cases where acpicpu is built in and
legacy EST/powernow work but acpi is missing the _PSS object, the legacy
routines will be used.

tested by dim@, and Nick Nauwelaerts <nick AT nauwelaerts.net>
ok marco@, dim@


# 1.76 22-Jan-2007 mk

Actually look for docking stations by looking for _DCK nodes.

ok marco


# 1.75 02-Jan-2007 marco

On some systems EC must be attached as the first device. Move EC from
generic _HID devices to its own routine to force first attach. Bug found
and debugged by joris@

Code mostly written by joris@


# 1.74 26-Dec-2006 marco

Add polling to devices that require it such as acpibat and acpitz.
Use the same tsleep/wakeup mechanism as the interrupt code to keep it non-
concurrent.


# 1.73 21-Dec-2006 deraadt

even more knf love


# 1.72 21-Dec-2006 marco

Remove some leftover acpiec gpe/interrupt code.


# 1.71 20-Dec-2006 deraadt

oops, name already used


# 1.70 20-Dec-2006 deraadt

rename __acpi_enable_gpe(); ok marco


# 1.69 20-Dec-2006 deraadt

acpi_unmap_pmregs() is not used, ok marco


# 1.68 19-Dec-2006 deraadt

simplify some low-level methods substantially (shrinking code); ok marco jordan


# 1.67 06-Dec-2006 mk

malloc(..., M_NOWAIT) with no NULL check is bad. If allocation fails,
complain, clean up, and bail out of attach.

input and ok gwk


# 1.66 28-Nov-2006 jordan

Fixed clearing arg for acpi_init_pic
ok kettenis@


# 1.65 27-Nov-2006 kettenis

Don't spam dmesg.


# 1.64 27-Nov-2006 jordan

Fixes for acpiprt routing
Post-parse fixup for namerefs
ok kettenis@


# 1.63 25-Nov-2006 marco

Don't handle acpiev events inside the gpe handler since this causes deadlocks.

ok jordan@


# 1.62 15-Nov-2006 kettenis

Hook up acpi apic interrupt routing support to generic acpi code.

ok jordan@


# 1.61 02-Nov-2006 marco

Kill acpiec attach function and add its functionality to the generic _HID
device attach

prodded and ok jordan


# 1.60 02-Nov-2006 marco

Fix comments to reflect reality.


# 1.59 19-Oct-2006 marco

Replace ACPI ioctls with APM ioctls. This makes ACPI talk the same protocol
as APM therefore userspace can now retrieve power information in the same
fashion as all other apm arches (zaurus, macppc, i386 etc).

To use force the use of acpi device like so: apmd -f /dev/acpi

ok jordan@


# 1.58 19-Oct-2006 jordan

Fixed GPE debug levels
Fixed ref counter on temporary bufferfields
DSDT parsing fully working on IBM Thinkpad
ok marco@


# 1.57 15-Oct-2006 jordan

Removed old GPE handler code remnants
ok marco@


# 1.56 12-Oct-2006 jordan

Massive ACPI changes
Fixed ACPI leaks
new ACPI API
ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.55 30-Jun-2006 gwk

KNF, No functional change,
"go at it" marco@


# 1.54 30-Jun-2006 jordan

Support for calling Embedded Controller via new GPE interface
ok marco@


# 1.53 30-Jun-2006 jordan

Added new code for GPE handling
Also works with ACPIEC; need to fixup acpiec.c to remove original interrupt handler
ok marco@


# 1.52 30-Jun-2006 jordan

Added offset argument for acpi read/write pmreg routines (new GPE code)
ok marco@


# 1.51 31-May-2006 todd

do not call acpi_powerdown() unless acpi has successfully initialized
ok gwk@ canacar@


# 1.50 29-May-2006 canacar

Add embedded controller support.
ok marco@ deraadt@ testing & ok gwk@


# 1.49 19-May-2006 canacar

Minimal thermal zone driver. Monitors thermal zone temperature,
shuts down the system if the 'critical temperature' is reached.
ok marco@


# 1.48 11-Apr-2006 marco

KNF.


# 1.47 11-Apr-2006 gwk

Add acpi_resume(struct acpi_softc *sc), which calls some AML methods,
detected at attach time, currently unused.

ok marco@


# 1.46 11-Apr-2006 gwk

Discover some ACPI sleep related methods, and call them at the
appropriate time in acpi_enter_sleep_state().

ok marco@


Revision tags: OPENBSD_3_9_BASE
# 1.45 26-Feb-2006 marco

Add acpicpu as a device.

ok jordan@


# 1.44 23-Feb-2006 marco

remove dead function.

ok jordan@


# 1.43 22-Feb-2006 marco

Print out all tables it found during autoconf. We'll need this data in
the future.


# 1.42 22-Feb-2006 marco

Make dmesg less verbose.

ok jordan.


# 1.41 22-Feb-2006 jordan

Added parsing of SSDT tables
ok marco@


# 1.40 22-Feb-2006 jordan

Added new aml_register to notify by device id
Moved powerdown code to button device
ok marco@


# 1.39 21-Feb-2006 marco

Readjust debug levels that crept in too low over the last few days.
KNF


# 1.38 21-Feb-2006 marco

Rig the power button so that the machine shuts down correctly.


# 1.37 20-Feb-2006 marco

Add acpibtn device. Remains disabled for now.


# 1.36 19-Feb-2006 jordan

Added code for aml_notify GPE notification callback
ok marco@


# 1.35 19-Feb-2006 grange

Fix acpi_enter_sleep_state() by replacing current ugly hack
with mostly correct sleeping state transition code.

ok brad@ marco@


# 1.34 19-Feb-2006 marco

jordan:
Enable interrupts.
Added peripheral code to make this work.

marco:
Replace softintr and other types of interrupts with a handler with context.
Fixed interrupt magic for GPEs.
Some KNF

This is work done by jordan and I but has to be a single commit.


# 1.33 17-Feb-2006 marco

Remove earlier introduced delays. We really only need them during autconf.

ok jordan@


# 1.32 17-Feb-2006 marco

Add a generic delay function as per the ACPI spec.

from jordan@


# 1.31 17-Feb-2006 marco

Use tsleep instead of delay when !cold.


# 1.30 17-Feb-2006 marco

Argh, we need delays between reads/writes. The backend reads (i2c most likely)
are so slow that values come back as 0 sometimes. We have to do this
despite the spec saying we shouldnt have to. Guess having a faster
parser than others can hurt sometimes.

Idea from jordan.


# 1.29 17-Feb-2006 jordan

Fixed code for handling _INI
Rearranged debug statements
ok marco@


# 1.28 16-Feb-2006 jordan

Added stub for calling _INI
Fixed aml_dowhile for NULL return
ok marco@


# 1.27 16-Feb-2006 jordan

Fixed parser/evaluator to retrieve battery info+state
Cleanup of parser
Dell laptops return battery status
ok marco@


# 1.26 06-Feb-2006 brad

fix a typo so this will compile on amd64.


# 1.25 04-Feb-2006 marco

When ACPI_ENABLED wasn't set but the devices were it had an unknown variable.

Pointed out by ian@.


# 1.24 03-Feb-2006 jordan

Rewrote aml parser yet again...
Fixed bus_space_map for i386/amd64
Added _BST and _BIF to acpibat to get status/info
ok marco@


# 1.23 20-Jan-2006 jordan

More evaluator changes
Removed old evaluator code
Fixed _HID value
ok marco@


# 1.22 19-Jan-2006 jordan

Added support for read/write of PCI Config space
Help jason@
ok marco@


# 1.21 18-Jan-2006 jordan

Added new evaluation routines
Added helper methods for buffer/field evaluation
Fixed broken indentation on dsdt.c

ok marco@


# 1.20 17-Jan-2006 jordan

Major overhaul of the aml parser. Evaluater temporarily disabled.

ok marco@


# 1.19 06-Jan-2006 grange

Check for ACPI control availability only if ACPI_ENABLE defined.


# 1.18 06-Jan-2006 grange

Informative error messages on attach, prettier dmesg.


# 1.17 06-Jan-2006 grange

printf -> dnprintf conversion for debug messages.


# 1.16 05-Jan-2006 grange

Find SLP_TYPx values and store them for future use in
acpi_enter_sleep_state().


# 1.15 05-Jan-2006 grange

Random KNF and cleanup


# 1.14 05-Jan-2006 grange

Provide symbolic names for the acpi sleeping states and use it.


# 1.13 28-Dec-2005 marco

Moved DEVNAME to a acpidev.h

ok jordan@


# 1.12 18-Dec-2005 sturm

add back comment about wrong code

ok marco


# 1.11 16-Dec-2005 marco

Remove test for kbd_reset. Prompted deraadt@, ok jordan@


# 1.10 16-Dec-2005 jordan

Added hooks for powerdown/power button press


# 1.9 16-Dec-2005 jordan

Cleaned up common power management register i/o
Added enter sleep state ioctl
Hooked power button interrupt
ok marco@


# 1.8 16-Dec-2005 marco

Remove autoconf chatter


# 1.7 16-Dec-2005 jordan

Moved DSDT AML parsing into parent ACPI driver
ok marco@


# 1.6 14-Dec-2005 marco

Add AC device glue.


# 1.5 14-Dec-2005 marco

Adjust debug printing levels to something more sane.


# 1.4 13-Dec-2005 marco

Add glue to attach devices to apci.


# 1.3 07-Dec-2005 marco

KNF


Revision tags: OPENBSD_3_8_BASE
# 1.2 10-Jul-2005 grange

Spacing cleanup.


# 1.1 02-Jun-2005 tholo

Start on a basic ACPI framework -- does not do much more than read out the
ACPI tables into kernel memory and attach ACPI and HPET timers currently.

In order to test this code, enabling the devices in GENERIC as well as
the ACPI_ENABLE option is needed. This code does not do any thermal
control yet, so this should be done with care depending on the platform.

In the tree so more people can contribute to making this more fully
featured.

Ok niklas@ grange@ tedu@


# 1.397 15-Mar-2021 patrick

Add acpi_iommu_device_map(), which replaces the DMA tag with one that
is blessed with IOMMU magic, if available. This is mainly for arm64,
since on amd64 and i386 the IOMMU only captures PCIe devices, as far
as I know, which uses the pci_probe_device_hook(). This though is for
non-PCI devices attached through ACPI.

ok kettenis@


# 1.396 07-Mar-2021 patrick

Pass standard DMA tag to acpi(4) table drivers.

ok kettenis@


# 1.395 27-Dec-2020 deraadt

acpi_map_address() cannot be wrapped by SMALL_KERNEL anymore, as
it is used by acpihpet.c


# 1.394 25-Dec-2020 visa

Refactor klist insertion and removal

Rename klist_{insert,remove}() to klist_{insert,remove}_locked().
These functions assume that the caller has locked the klist. The current
state of locking remains intact because the kernel lock is still used
with all klists.

Add new functions klist_insert() and klist_remove() that lock the klist
internally. This allows some code simplification.

OK mpi@


# 1.393 17-Dec-2020 kettenis

Fix some issues with referencing named ACPI nodes from Packages.
These references need to be resolved at runtime rather than when
they're parsed such that they pick up the right values for those nodes
which can be changed when for example _INI methods run.

The current approach is to replace these reference with a string that
names the node in question. The problem with that is that packages
can also contain normal strings. Which means that we need special
code that depends on the context where the package is used.

This diff takes a different approach by retaining a reference when
parsing. Code that uses a package will need to resolve this reference
but there is no ambiguiety anymore.

ok patrick@


# 1.392 05-Dec-2020 kettenis

Add support for the _EVT method to the GPIO support code.

ok patrick@


Revision tags: OPENBSD_6_8_BASE
# 1.391 27-Aug-2020 jmatthew

Reorder the acpi attach process so we look at Processor() CPU nodes
after Device() ones, since we should prefer the newer node type.
If we see any Device() nodes, don't attach acpicpu(4) to any Processor()
nodes if they're also present. This also makes acpitz(4) and
acpipwrres(4) devices attach slightly later.

ok kettenis@, also tested by jmc@


# 1.390 16-Aug-2020 gnezdo

Remove sc_maxgpe from acpi_softc which was never read from

Remove a dead conditional from acpi_init_gpes while there.

ok jcs@


# 1.389 21-Jul-2020 deraadt

acpi can use IPL_BIO (a low interrupt) since it only enqueues operations for
later processing. The use of a high interrupt will predate suspend/resume
efforts, we had to redesign acpi to be non-reentrant obviously
discussed with kettenis, in snaps for more than a week


# 1.388 24-Jun-2020 cheloha

kernel: use gettime(9)/getuptime(9) in lieu of time_second(9)/time_uptime(9)

time_second(9) and time_uptime(9) are widely used in the kernel to
quickly get the system UTC or system uptime as a time_t. However,
time_t is 64-bit everywhere, so it is not generally safe to use them
on 32-bit platforms: you have a split-read problem if your hardware
cannot perform atomic 64-bit reads.

This patch replaces time_second(9) with gettime(9), a safer successor
interface, throughout the kernel. Similarly, time_uptime(9) is replaced
with getuptime(9).

There is a performance cost on 32-bit platforms in exchange for
eliminating the split-read problem: instead of two register reads you
now have a lockless read loop to pull the values from the timehands.
This is really not *too* bad in the grand scheme of things, but
compared to what we were doing before it is several times slower.

There is no performance cost on 64-bit (__LP64__) platforms.

With input from visa@, dlg@, and tedu@.

Several bugs squashed by visa@.

ok kettenis@


# 1.387 10-Jun-2020 jca

Give userland a consistent battery and A/C state after resume

Previously we refreshed the state of acpiac(4) and acpibat(4)/acpisbs(4)
by queueing fake ACPI events to be processed by the acpi thread.
acpibat(4) using ACPIDEV_POLL meant that its status could be refreshed
first, resulting in an APM_POWER_CHANGE event sent to userland before
the state of acpiac(4) was up to date. Because of this, apmd(8) could
see A/C unplugged when it was in fact plugged, possibly leading to
a wrong decision.

Instead of using fake ACPI events, refresh the status of acpiac(4) and
acpibat(4)/acpisbs(4) right away at DVACT_WAKEUP time. To reuse the
existing refresh code of those drivers, make their ACPI *_notify()
handlers responsible for sending APM_POWER_CHANGE events instead. Also
queue an APM_POWER_CHANGE event right after resume to retain existing
behavior.

This fixes the consistency of data shown to userland, and makes those
drivers less special.

Initially discussed last year (!) with mpi@ at Miod's place.
Feedback and ok jcs@, deraadt@ agrees with the use of DVACT_WAKEUP here.


# 1.386 29-May-2020 deraadt

dev/rndvar.h no longer has statistical interfaces (removed during various
conversion steps). it only contains kernel prototypes for 4 interfaces,
all of which legitimately belong in sys/systm.h, which are already included
by all enqueue_randomness() users.


# 1.385 14-May-2020 kettenis

Bring back the acpi(4) changes. This time around only use the ACPI _CRS
information on ACPI 5.0 and later.

ok krw@, patrick@


# 1.384 11-May-2020 jca

More accurate remaining power reporting with disparate batteries

Overall remaining power was computed as the average of the remaining
power of each battery, in percents. This doesn't work with batteries of
different capacity, so use the total capacity and remaining power
instead.

Reported by lidstah, successful tests by lidstah and benno@, ok kettenis@


# 1.383 08-May-2020 kettenis

Move parsing of _CRS into acpi(4) and pass the parsed address and interrupt
information in struct acpi_attach_args.

ok mpi@


Revision tags: OPENBSD_6_7_BASE
# 1.382 14-Apr-2020 kettenis

Implement acpi_getprop(), the ACPI equivalent of OF_getprop().

ok patrick@


# 1.381 12-Apr-2020 kettenis

Add support for the _CCA method. This method indicates whether DMA is
cache-coherent or not. To implement this, acpi(4) gets two bus_dma tags
and passes the appropriate one when attaching devices based on _CCA.
On i386/amd64, where for all practical purpose DMA is always cache-coherent,
the two tags are the same. But on arm64 they are distinct.

ok patrick@


# 1.380 07-Apr-2020 visa

Abstract the head of knote lists. This allows extending the lists,
for example, with locking assertions.

OK mpi@, anton@


# 1.379 03-Apr-2020 mpi

Fix incoherencies of errors returned by various *kqfilter routines.

EOPNOTSUPP is returned when there's no "filterops" for a given fd.
EINVAL when the requested filter isn't supported by the "filterops".
ENXIO means the underlying device is no longer valid/present.

ok visa@


# 1.378 20-Feb-2020 visa

Replace field f_isfd with field f_flags in struct filterops to allow
adding more filter properties without cluttering the struct.

OK mpi@, anton@


# 1.377 31-Dec-2019 visa

Use C99 designated initializers with struct filterops. In addition,
make the structs const so that the data are put in .rodata.

OK mpi@, deraadt@, anton@, bluhm@


# 1.376 31-Dec-2019 mpi

Convert infinite sleeps to {m,t}sleep_nsec(9).

ok kn@


# 1.375 31-Dec-2019 jsg

include sys/sensors.h for sensor use
don't depend on indirect inclusion from acpidev.h


Revision tags: OPENBSD_6_6_BASE
# 1.374 07-Sep-2019 kettenis

Revert acpipci(4) changes. Too many systems are broken. We'll try again
after 6.6 as been released. The acpireg.h change stays behind.


# 1.373 28-Aug-2019 kettenis

Use ACPI information to attach PCI busses like we do on arm64. There are a
few additional quirks though, and attaching the PCI busses is delayed to
replicate the existing code more closely. That may be changed in the
future. Also tweak how we handle MSI support and respect to ACPI flag
that says we shouldn't attempt to use MSIs.

Some fallout is expected.

ok patrick@


# 1.372 27-Aug-2019 deraadt

On amd64 the bootup and resume paths for SP/BP/SP were not performing
msr/pat, clock-startup, firmware upgrade, fpu initialization, and cpu
soft-state initialization in the same order. That kind of chaos is
crazy and fragile. Improve the acpi resume call sequence and unify order
of the operations.


# 1.371 02-Jul-2019 jcs

acpi_set_gpehandler: turn edge arg into flags, add support for
passing GPE_DIRECT which directly executes the supplied callback and
clears a level interrupt rather than going through acpi_taskq which
can be slow when other driver callbacks are in the queue.

This should not be used for GPE handlers that need to do anything
ACPI-related.

ok kettenis


# 1.370 10-Jun-2019 kettenis

Print proper ACPI version number.

ok mlarkin@


# 1.369 08-Jun-2019 kettenis

When establishing the mapping between ACPI device nodes and PCI devices,
skip PCI host bridges and devices that aren't there.

ok patrick@


# 1.368 07-Jun-2019 kettenis

Introduce acpi_getsta() and use it to eliminate some duplicated code.

ok patrick@


# 1.367 12-May-2019 kettenis

Make sure the extended PM register descriptions actually exist before
using them. Fixes machines with ancient ACPI 1.0 (such as recent
hypervisors).

ok deraadt@


# 1.366 12-May-2019 kettenis

Add support for mapping ACPI PM registers using the "extended" gasio
representation from the FADT. Mostly fixes the Lanner NCA-1510.

ok lteo@, mlarkin@, deraadt@


# 1.365 11-May-2019 lteo

Fix incorrect error message.

ok kettenis@


# 1.364 08-May-2019 tedu

free size. the allocation is the header plus the length.


Revision tags: OPENBSD_6_5_BASE
# 1.363 04-Apr-2019 kettenis

Avoid potential null-pointer dereference. Found by Kent R. Spillner.

ok deraadt@, jsg@


# 1.362 20-Jan-2019 jsg

fix SMALL_KERNEL build after changes in rev 1.361
reported by naddy@ ok deraadt@


# 1.361 19-Jan-2019 tedu

Add a pwraction sysctl that controls what the power button does on acpi.
By default, nothing changes -- shutdown is initiated. But allows turning
power button into a sleep button if desired.
(grudging) ok from a few parties


# 1.360 26-Oct-2018 kettenis

Add acpipci(4) on amd64. For now this only calls the PCI-specific _OSC
method to let the ACPI implementation know what features we support.


Revision tags: OPENBSD_6_4_BASE
# 1.359 30-Aug-2018 kettenis

Move softbutton check before the check that skips attaching certain acpi
devices. Fixes regression caused by matching on _CID in addition to
matching on _HID.

ok matthieu@, mlarkin@


# 1.358 25-Aug-2018 kettenis

Match ACPI devices based on _CID if no match for _HID is found.

ok mpi@


# 1.357 19-Aug-2018 kettenis

Add support for multiple PCI segments. Only really implemented for arm64
for now as amd64/i386 firmware still caters for legacy OSes that only
support a single PCI segment.

ok patrick@


# 1.356 03-Aug-2018 kettenis

Let ahci(4) match on _CLS instead of _HID when attaching at acpi(4). Avoids
having to add many more _HID entries to the match table.

ok deraadt@, mlarkin@


# 1.355 10-Jul-2018 kettenis

Make legacy interrupts work in acpipci(4).

ok patrick@


# 1.354 05-Jul-2018 kettenis

Add acpipci(4), a driver that supports generic ECAM-compatible PCI host
bridges based on information provided by ACPI.

ok mlarkin@


# 1.353 04-Jul-2018 kettenis

Properly pass around the PCI "chipset tag" in acpi(4) and refactor
acpimcfg(4) to call an MD initialization functions that sets up a tag for
PCI ECAM.

ok guenther@, mlarkin@, krw@


# 1.352 01-Jul-2018 kettenis

Move acpi(4) attach glue into acpi_machdep.c.

ok guenther@, deraadt@


# 1.351 01-Jul-2018 kettenis

Add support for _DSD Device Properties.

ok mlarkin@


# 1.350 30-Jun-2018 kettenis

Add intr_enable() function, intended for MI use to amd64 and i386 and use
this in the acpi(4) suspend/resume code paths.

ok deraadt@


# 1.349 29-Jun-2018 kettenis

Use <stdint.h> types.

ok mpi@, jung@, krw@, deraadt@


# 1.348 29-Jun-2018 mlarkin

ACPI: Allow (with warning) GPE handler reassignment, instead of returning
a failure code.

ok kettenis


# 1.347 26-Jun-2018 mlarkin

knf


# 1.346 26-Jun-2018 kettenis

Let acpi(4) pass down the bus dma tag.

ok deraadt@


# 1.345 25-Jun-2018 kettenis

Reorganize acpi(4) code a bit in preparation for upcoming arm64 support.

ok deraadt@


# 1.344 20-May-2018 kettenis

Wake up the acpi thread after scheduling a gpio event.

ok mpi@


# 1.343 17-May-2018 kettenis

Sprinkle some #ifndef SMALL_KERNEL such that RAMDISK kernels cuild again.

pointed out by stsp@


# 1.342 17-May-2018 kettenis

Implement GenericSerialBus OpRegion support.

ok mlarkin@


# 1.341 27-Mar-2018 kettenis

Add acpicmos(4), a driver that implements SystemCMOS OperationRegion
access support. This fixes machines where the AML doesn't check whether
support for this OperationRegion type has been registered by the OS.

ok mlarkin@


Revision tags: OPENBSD_6_3_BASE
# 1.340 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.339 10-Feb-2018 deraadt

Syncronize filesystems to disk when suspending. Each mountpoint's vnodes
are pushed to disk. Dangling vnodes (unlinked files still in use) and
vnodes undergoing change by long-running syscalls are identified -- and
such filesystems are marked dirty on-disk while we are suspended (in case
power is lost, a fsck will be required). Filesystems without dangling or
busy vnodes are marked clean, resulting in faster boots following
"battery died" circumstances.
Tested by numerous developers, thanks for the feedback.


# 1.338 08-Feb-2018 deraadt

After flushing disks, try again to release all memory. We don't need to
save dirty memory to the hibernate space.


# 1.337 08-Feb-2018 deraadt

Toss all releaseable memory, because fragmentation can get in the way
of allocating the hibernate playpen.


# 1.336 08-Feb-2018 deraadt

sr_quiesce() is a new approach for ensuring that softraid drains
output to the disks.

This is part of a larger suspend/resume filesystem-safety diff, which
has been worked on for a couple of months already. Tests by job, krw,
beck, benno, and others. Sometimes even by snapshot users...


# 1.335 29-Nov-2017 kettenis

Add support for GPIO-signaled events.

ok mlarkin@


# 1.334 16-Nov-2017 jcs

Add PCI attachment for dwiic(4) needed by Intel 100 Series machines

ok kettenis


# 1.333 14-Nov-2017 kettenis

Add a few more PNP IDs to the skip lists.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.332 17-Aug-2017 stsp

Make dwiic(4) attach its ACPI parent dependencies.
This change makes the Asus E200HA keyboard work.

Original analysis and patch by Cesare Gargano
ok kettenis@


# 1.331 09-Aug-2017 dcoppa

Add "PNP0303" (8042 PS/2 Controller) to acpi_isa_hids[]

ok kettenis@


# 1.330 22-Jul-2017 jcs

acpibat: look for _BIX first before falling back on _BIF

ACPI 4.0 deprecated _BIF for battery status so some newer machines
are exporting _BIX which extends _BIF's package with a few extra
fields. When using _BIX, export the new discharge cycle count as a
sensor.

feedback from kettenis and anton
ok deraadt


# 1.329 20-Jul-2017 kettenis

Start a list of isa(4) device names such that we can avoid printing them
as "not configured".

ok mikeb@, millert@, deraadt@


# 1.328 08-Apr-2017 deraadt

Sensors are run as callbacks inside tasks. During suspend a sensor
could be running inside a driver that will be force-detached, or due
to tsleep end up disrupting the softstate/hardstate contract. At
suspend time, quisce all these callbacks by waiting for completion.
This issue has never been observed for real, but may be implicated in
suspend/resume failures.
ok kettenis guenther mlarkin


# 1.327 08-Apr-2017 deraadt

Two prototypes for acpi_maptable; merge to one place


Revision tags: OPENBSD_6_1_BASE
# 1.326 27-Mar-2017 deraadt

Now that hibernate_alloc() only has clean success/failure, don't
need to call hibernate_free() to clean up a partial mess.
ok mlarkin kettenis


# 1.325 13-Mar-2017 deraadt

allow ramdisks to build; ok jcs


# 1.324 12-Mar-2017 jcs

try to attach acpisbs first and if successful, don't attach acpibat


# 1.323 02-Mar-2017 natano

Add a new sysctl machdep.lidaction. The sysctl works as follows:

machdep.lidaction=0 # do nothing
machdep.lidaction=1 # suspend
machdep.lidaction=2 # hibernate

lidsuspend is just an alias for lidaction, so if you change one, the
other one will have the same value. The plan is to remove
machdep.lidsuspend eventually when people have upgraded their
/ets/sysctl.conf.

discussed with deraadt, who came up with the new MIB name
no objections mlarkin
ok stsp halex jcs


# 1.322 28-Feb-2017 natano

Untangle abstract sleep modes from ACPI sleep states. This paves the way
for further work in the area.

idea and ok deraadt
ok mlarkin


# 1.321 22-Feb-2017 jcs

add acpisbs, an acpi smart battery subsystem driver reading data
over smbus

currently disabled because it conflicts with acpibat

ok deraadt, kettenis


# 1.320 14-Jan-2017 kettenis

Revert aml_rdpciaddr changes; breaks several machines that were working
before.


# 1.319 11-Jan-2017 jsg

Increase the size of the buffers used for _HID and _CID strings to be
able to fit "Hyper_V_Gen_Counter_V1".

ok mlarkin@ deraadt@


# 1.318 08-Jan-2017 kettenis

Make aml_rdpciaddr() work for for devices behind bridges. The code now
uses the mapping between AML nodes and PCI devices that we establish.
Because _INIT methods may end up calling aml_rdpciaddr(), make sure we
create that mapping early. Also handle devices that aren't actually present.
These devices are now included in the mapping and reads will return an
all-ones patterm whereas writes are a no-op..

ok mlarkin@


# 1.317 25-Oct-2016 pirofti

Add the resource index to aml_parse_resource() callback function.

This is needed by an upcoming acpiec commit that handles machines breaking
the current ACPI specifications.

Change suggested by and ok kettenis@, guenther@


# 1.316 18-Sep-2016 guenther

If the APIC table (MADT) isn't present then ci_acpi_proc_id won't be set
when acpicpu tries to attach, leading to a NULL dereference. The ACPI
processor object on the Dells where this was reported is bogus and useless
anyway so apply the same test when deciding whether to attach to processor
objects as acpicpu would when matching them to cpus.

Problem reported by Eivind Eide (xenofil (at) gmail.com) and
Johan Huldtgren (johan+openbsd-bugs (at) huldtgren.com).
Testing by Eivind Eide.
ok kettenis@ mlarkin@


# 1.315 03-Sep-2016 naddy

Write the system time back to the RTC every 30 minutes.
This fixes the problem that long-running machines which were not
shut down properly would reboot with a badly offset system time.

hints and ok kettenis@


# 1.314 31-Aug-2016 mlarkin

Don't put a device in the wake device list if _STA says it's not there.

Fixes at least one machine that woke up from sleep / poweroff immediately.

ok kettenis@, tested by kettenis and martijn@


# 1.313 28-Jul-2016 kettenis

Store the acpi processor ID/UID in struct cpu_info, and use it to attach
acpicpu(4) drivers to the right cpu(4).

ok mlarkin@, guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.312 10-Jun-2016 kettenis

The power management timer has been made optional in ACPI 5.0A, so don't
attempt to attach acpitimer(4) if the timer isn't present.

Pointed out by jcs@

ok jcs@


# 1.311 08-May-2016 kettenis

Make sure devices listed by a device's _DEP method are attached before
we attach the device itself. The _DEP methode, introduced in ACPI 5.0,
is a hint that the device in question depends on OpRegion support from
the devices listed by _DEP, so we have to order them the proper way.
To prevent us from attaching those devices again when we encounter them
later walking down the device tree, keep track of our attempts to
attach a device driver to them using aflag in the aml_node structure.

ok guenther@


# 1.310 25-Apr-2016 pirofti

We attach acpihpet(4) and acpiec(4) without using the HID, so do not
report it in the dmesg.

OK deraadt@, mlarkin@.


# 1.309 02-Apr-2016 kettenis

Reduce dmesg spam by knocking out some acpi hardware IDs that don't need a
driver.

ok mpi@


# 1.308 29-Mar-2016 kettenis

Check if a device is present (using _STA) in acpi_foundhid. This prevents
us from reporting devices as "not configured" that aren't actually present,
and allows us to remove duplicated code from several drivers.

ok deraadt@, ok jsg@


# 1.307 29-Mar-2016 kettenis

Implement a cleaner way to prevent acpivout(4) from attaching if we have
a platform-specific method of controlling the backlight.

ok millert@, mlarkin@, jung@


# 1.306 28-Mar-2016 kettenis

Adding each and every new acpi device driver to acpi_foundhid() is
gettinga bit of the burden. And it really isn't how our autoconf
framework is supposed to work for busses that can be enumerated. All
the drivers already check for a matching _HID in their attach
function. So we can just drop the checks in acpi_foundhid() and let
the drivers sort themselves out.

Print a "not configured" line for devices for which we don't attach adriver.
This may be a bit much as there are devices for which we will not have a
driver. This will be removed if it causes too much dmesg spam.

ok deraadt@, mlarkin@


Revision tags: OPENBSD_5_9_BASE
# 1.305 17-Jan-2016 jsg

Take acpi_foundhid() outside of #ifndef SMALL_KERNEL so the ramdisk
kernel can find and attach dwiic(4) and sdhc(4).


# 1.304 17-Jan-2016 jsg

remove an unused global variable


# 1.303 14-Jan-2016 kettenis

Attach dwiic(4) on the Intel Bay Trail i2c controllers as well.


# 1.302 12-Jan-2016 kettenis

Improve emulation of PM registers on Hardware-redcued ACPI by emulating
SPL_TYPx, SLP_EN and WAK_STS using the new SLEEP_STATUS_REG and
SLEEP_CONTROL_REG registers. Makes the transition into S5 (powerdown) work
on the ASUS X205TA.

ok deraadt@, mlarkin@


# 1.301 12-Jan-2016 jcs

Add dwiic, a driver for the Synopsys DesignWare i2c controller found
on the Samsung ATIV Book 9 laptop. This initial version only
supports ACPI config/attachment.

Add ihidev, a HID-over-i2c driver largely based on uhidev. dwiic
handles attaching ihidev devices found in ACPI.

Add ims, a HID-over-i2c mouse/trackpad driver to get basic cursor
and button functionality from HID-compliant i2c trackpads.

ok kettenis deraadt


# 1.300 11-Jan-2016 kettenis

Make sdhc(4) attach to hardware IDs "80860F14" and "PNP0FFF".


# 1.299 09-Jan-2016 kettenis

Initial support for Hardware-Reduced ACPI. This skips the legacy mode checks
by emulating the PM_CNT register such that SCI_EN is always enabled. It also
skip attaching acpitimer(4) since the relevant registers are no longer there.

ok deraadt@, mlarkin@


# 1.298 06-Jan-2016 kettenis

Follow the spec and check the SCI_EN bit to see if ACPI has already
been enabled and don't bother enabling it again if it is. This should fix
systems that don't implement legacy mode and therefore set ACPI_ENABLE and
ACPI_DISABLE to zero.

ok mlarkin@, yasuoka@


# 1.297 23-Nov-2015 reyk

Back out the CRITICAL change, turn it back from 10% to 25%. Some
people with old/broken batteries didn't like this change, and the
underlying heuristics are wrong.


# 1.296 22-Nov-2015 reyk

The "CRITICAL" APM battery level, that is exported/faked by the ACPI
stack, is currently set to 25%. With 25% battery left, a typical
modern laptop will have at least one hour runtime left. It is an
arbitrary value, and other APM power layers in the kernel set CRITICAL
to something like 5% or 10%, so set it to 10% for now.

OK mlarkin@


# 1.295 28-Sep-2015 deraadt

In low-level suspend routines, set cold=2. In tsleep(), use this to
spit out a ddb trace to console. This should allow us to find suspend
or resume routines which break the rules. It depends on the console
output function being non-sleeping.... but that's another codepath which
should try to be safe when cold is set.
ok kettenis


# 1.294 06-Sep-2015 deraadt

clarify malloc arguments (base + length...)


# 1.293 28-Aug-2015 deraadt

fairly simple sizes for free(); ok tedu


# 1.292 20-Aug-2015 kettenis

Respect the access size when reading or writing to pci config space.
This version also makes sure writes are properly aligned, preventing
the panics seen with the previous incarnation of this change.

Fixes battery status passthrough in vmware, brightness keys on my x220
and who knows what else.

ok mlarkin@, deraadt@


Revision tags: OPENBSD_5_8_BASE
# 1.291 04-Aug-2015 deraadt

If we are setting ACPI_PM1_BM_RLD do it only once, and remember to
repeat this action at resume time.
ok mlarkin guenther


# 1.290 29-Jul-2015 deraadt

Something subtle in previous revision is locking up suspend or booting
on some laptops. Mailing lists threads mention x220 and Asus machines.
Reverting for now.


# 1.289 21-Jul-2015 kettenis

iRespect the access size when reading or writing to pci config space.
Fixes battery status passthrough in vmware and who knows what else.

Tested by many.

ok mlarkin@, deraadt@


# 1.288 17-Jul-2015 mlarkin

fix comments


# 1.287 26-Mar-2015 jsg

Use the 0x10 extended capability to determine if the trackstick
buttons are wired to the trackpad and need to be re-routed to the
trackstick. Without this change the buttons on 2015 thinkpads
get picked up as extended buttons that show up as scroll up/down.

Remove the X1 Carbon 2015 (LEN0048) and X250 (LEN0046) from the
top button area/soft buttons quirks list. Also avoid using the
quirk list entirely if the capability bit is set.

Based on work from jcs@ with feedback from mpi@
ok mpi@ jcs@


# 1.286 20-Mar-2015 kettenis

The ACPI standard says that a PCI function number of FFFF can be used to refer
to all functions on a device. This makes no sense in the context of accessing
PCI config space. Nevertheless stsp@ has a machine with AML that tries to
do config space reads from a device where _ADR encodes function number FFFF.
This is almost certainly buggy AML, but since Linux (and presumably Windows)
boot just fine on this machine we need to work around this. Linux effectively
treats function FFFF as function 7, which isn't present on this machine.
Instead we simulate a read from a non-existent device by returning a bit
pattern of all ones. Writes to function FFFF will still panic.

ok mlarkin@


# 1.285 16-Mar-2015 deraadt

Reduce use of SMALL_KERNEL in acpi, bringing acpiec driver and more gpe
handling into RAMDISK. This is now possible because the install media
has ample room. The goal is to reduce special cases where we may be
skipping (unknown) important operations...
ok mlarkin kettenis


# 1.284 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.283 07-Feb-2015 deraadt

New framework that allows hibernate to pass in entropy from it's fresh
boot.
ok mlarkin


# 1.282 06-Feb-2015 mlarkin

Fix a hibernate crash on some machines due to unmapping a page that
may not have been mapped previously (in the failure to hibernate case).

Also ensure that the lowmem ptp is mapped in all cases (not just MP).

ok kettenis


# 1.281 17-Jan-2015 deraadt

resume_randomness() before spinning up other cpus...


# 1.280 15-Jan-2015 jsg

Define a new wscons mouse type for Synaptics clickpad devices that lack
physical buttons. This will be used if the acpi pnpid for the mouse
matches a list provided by Synaptics found in Linux.

Combined with the xenocara changes this will remove the need for an xorg
config file for the x240/t440/t540 generation of thinkpads.

ok matthieu@ shadchin@ kettenis@


# 1.279 11-Jan-2015 kettenis

Tolerate checksum errors in ACPI tables (but complain about them). Apparently
there are machines out there with broken BIOSen with such checksum errors in
thier RSDT tables. Windows runs fine on these machines, so it probably does
not check. Linux ignores checksum errors as well (and prints a warning).

ok deraadt@, krw@


# 1.278 18-Dec-2014 deraadt

suspend and resume the RNG (at what looks like the right spots)
ok reyk djm


# 1.277 09-Dec-2014 doug

Replace some malloc(n*size,...) calls with mallocarray().

ok tedu@ deraadt@


# 1.276 06-Dec-2014 mpi

If "machdep.lidsuspend" is not set, do not go back to sleep if the lid
is still closed when resuming. This allows docked laptop to be resumed
without opening them.

ok mlarkin@


# 1.275 23-Nov-2014 mlarkin

If a machine has no _LIDs, then none can be open. This diff fixes a problem
where machines without any _LIDs were going back to sleep after hibernate
due to the recent suspend-if-lid-closed-on-resume diff. Noticed on qemu,
which doesn't have any _LIDs.

discussed with deraadt


# 1.274 18-Nov-2014 krw

Nuke yet more obvious #include duplications.

ok miod@


# 1.273 08-Nov-2014 mlarkin

If resuming from sleep (zzz/ZZZ) and the lid is still closed, go back to
sleep. This prevents accidental lid flex or slight opening in a backpack
from waking the machine up and leaving it resumed (powered on).

ok deraadt@


# 1.272 26-Sep-2014 kettenis

Rework piglet and pig allocation. Currently the piglet gets allocated
deep down in the suspend path, where it is really hard to recover from
allocation failure. So allocate the piglet early on in the suspend path.
Also change the piglet and piglet allocation functions to use km_alloc(9)
instead of doing pmemrange magic. This removes a bunch of code which, in the
case of the piglet allocation, is broken since it results in a NULL pointer
dereference. Also switch the piglet allocation to not wait. If we can't
allocate 16MB of phys contig memory on a halfway modern machine we're almost
certainly under a lot of memory pressure and we're better off not trying to
hibernate anyway.

ok mlarkin@


# 1.271 25-Sep-2014 kettenis

Bring back rev 1.249. Now that mp_setperf() has been fixed, it should be
safe to restore perflevel when resuming. Should fix the issues people
see with the processor running at full speed heating up the machine while
the apm command reports that the CPUs are running at a low speed.

ok dcoppa@, deraadt@


# 1.270 23-Sep-2014 kettenis

Release the acpi lock around calls to wsdisplay_suspend() and
wsdisplay_resume(). The X server may adjust the display brightness just
before/after the VT switch when suspending/resuming. In fact, it may
already be blocked adjusting the display brightness for some reason
unrelated to the suspend request.

The VT switch request will eventually time out, but the delay is annoying
and the failed VT switch will lead to problems after resume.

Needed for a future update of the xf86-video-intel driver.

ok mlarkin@


# 1.269 19-Sep-2014 kettenis

Use config_suspend_all(9).


# 1.268 08-Sep-2014 kettenis

Clear the PME Status bit on PCI devices in response to Device Wake events.
Fixes the interrupt storm on the 2009 Mac mini with WOL enabled on its
nfe(4) interface.

ok mlarkin@


Revision tags: OPENBSD_5_6_BASE
# 1.267 20-Jul-2014 mlarkin

Support hibernating to softraid crypto volumes.

much help and ok from deraadt@


# 1.266 16-Jul-2014 mlarkin

Reenable hibernate RLE support and flush+zero all memory after unpack.


# 1.265 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.264 12-Jul-2014 kettenis

Add a function to drop all clean pages on the page daemon queues and call
it when we hibernate.

ok mlarkin@, miod@, deraadt@


# 1.263 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.262 11-Jul-2014 mlarkin

Flush the buffercache to 16MB on hibernate and restore its previous max
size (kern.bufcachepercent) on resume, for better hibernate performance.

ok beck@


# 1.261 10-Jul-2014 blambert

remove unneeded header

ok mlarkin@


# 1.260 01-Jul-2014 kettenis

Adding an additonal delay after each access to SystemMemory or SystemIO
during autoconf makes no sense.

ok deraadt@


# 1.259 23-Jun-2014 kettenis

SystemMemory is used to access memory mapped registers on some machines, so we
must use the bus_space(9) API to access it instead of memcpy(9). Also make
sure we properly align access to fields that need more than byte alignment.

ok guenther@


# 1.258 21-May-2014 mlarkin

Fix invalid / missing format specifiers in several dnprintf statements
(used only when ACPI_DEBUG is enabled)

From a diff on tech@ by Fabian Raetz, with a minor change by me, tested
on i386 and amd64 with ACPI_DEBUG enabled and disabled.


# 1.257 25-Apr-2014 mlarkin

Defer the free of the hibernate structures until later, when it is safe to
do so.

Freeing the hibernate structures too early leads to inadvertent calls into
splx when we aren't ready for them yet. Found when trapping splx calls in
the resume path.

discussed with deraadt@ at m2k14


# 1.256 13-Apr-2014 mlarkin

Fix a badly worded debug message that implied the entire machine was going
to sleep, instead of just the acpi thread going to sleep.


# 1.255 31-Mar-2014 mpi

Change the order of operations during a suspend/resume cycle and
call bufq_quiesce() after executing the DVACT_QUIESCE handlers.

This should be safe since no disk nor controller drivers have such
handler but it will allow us to detach sd(4) devices attached to a
USB bus.

Another benefit pointed out by kettenis@ is that drivers that need
to read a firmware from the disk should be able to do it at resume
time in a DVACT_WAKEUP handler.

ok kettenis@, deraadt@


# 1.254 13-Mar-2014 dlg

get rid of the assumption that the head of the alldevs list is the
"mainbus" device. this breaks when mpath is enabled because it
attaches before mainbus and therefore takes the head position.

have autoconf provide device_mainbus() which looks up mainbus_cd,
and use that instead.

discussed with deraadt who just wants mpath stuff to move forward
despite there being many ways to shine this particular turd.


Revision tags: OPENBSD_5_5_BASE
# 1.253 21-Feb-2014 deraadt

Do the lid control further on in the resume sequence, since this is
AML, and will do WAITOK operations. While cold, that is a bad idea.
We seem to be safe with respect to the spec, I do not see any event
debounce issues here.
ok kettenis, tested by many


# 1.252 20-Jan-2014 claudio

Instead of checking sc->sc_revision <= 1 check that the
fadt->hdr_revision <= 1. This is a better check since it checks
the version of the proper descriptor. This allows the alix APU
to reboot since those boards have currently a wrong sc->sc_revision.
Committing this now so that it can be tested easier.
With and OK jsg@ also fine by derradt@


# 1.251 24-Dec-2013 kettenis

If the FADT has its SMI_CMD set to zero, assume we're only ACPI-only hardware
and don't need to disable SMI ownership of the ACPI hardware registers.

ok mlarkin@


# 1.250 23-Dec-2013 kettenis

Revert previous commit. It makes resume hang on some MP systems running
GENERIC.MP.


# 1.249 16-Dec-2013 kettenis

Restore hw.setperf upon resume like we do for apm(4) on i386. Seems we need
to do this fairly late in the resume to avoid hangs.

ok deraadt@, mlarkin@


# 1.248 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


# 1.247 06-Nov-2013 mpi

Add support for Power Resources for Dx states and the necessary hook
for PCI devices. This hook should be called twice, before and after
changing the power state of a PCI device.

Before setting the device to the new state, the ACPI layer will notify
every power resources linked to the device for that state and make sure
they are turned "_ON". After changing the state of the device, it will
decrement the reference of every power resources linked to that device
for the old state and turn them "_OFF" if they are no longer referenced.

This fixes the no-USB after resume problem seen on various ThinkPad,
problem initialy diagnosed with Alexander Polakov.

ok kettenis@, deraadt@


Revision tags: OPENBSD_5_4_BASE
# 1.246 01-Jun-2013 mlarkin

Stop printing ACPI wakeup devices in dmesg after the 16th wakeup device.

Helps with VMware VMs that report hundreds of wakeup devices.

ok reyk@, deraadt@


# 1.245 31-May-2013 bluhm

Fix the build for a kernel without wd and pciide in its config.
OK deraadt@ tedu@


# 1.244 30-May-2013 deraadt

Enforce ca_activate tree-walks over the entire heirarchy for all events,
cleaning up some shutdown-hook related code on the way.
(A few drivers related to sparc are still skipped at kettenis' request)
ok kettenis mlarkin, tested by many others too


# 1.243 18-Apr-2013 deraadt

pin the acpi thread to the BSP, because AML and SMI cannot be trusted. I
was certain we were doing this already, but it turns out no...


Revision tags: OPENBSD_5_3_BASE
# 1.242 24-Dec-2012 guenther

In acpiioctl(), Return though the splx() path if suser() returns an error

ok deraadt@


# 1.241 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


# 1.240 04-Oct-2012 ehrhardt

Use information provided by ACPI to attach secondary PCI host bridges.

ok kettenis@


# 1.239 07-Sep-2012 kettenis

Add a new function called acpi_pci_min_powerstate() that returns the lowest
power state for a PCI device for the power state (currently S3 or S4) we're
transitioning into. This function is probably too simplistic; there is a lot
of (silly) complexity in the ACPI spec.

ok deraadt@


Revision tags: OPENBSD_5_2_BASE
# 1.238 13-Jul-2012 pirofti

Disable acpivideo(4) on ASUS laptops.

There are some models that panic whenever an event is received.
Some cope, some don't. In order to get this right acpiec(4) requires
major rework. Until then let machines boot.

Discussed with deraadt@.


# 1.237 11-Jul-2012 mlarkin

for now, restrict hibernate to root in the acpi ioctl kernel interface.
normal rules for controlling this interface through apmd still apply.

ok deraadt@


# 1.236 09-Jul-2012 deraadt

tiny little oops in untested hibernate block


# 1.235 09-Jul-2012 deraadt

clean up ioctl() case statements


# 1.234 09-Jul-2012 deraadt

acpi_disable_onegpe() is not used; ok pirofti


# 1.233 24-May-2012 kettenis

If _MIN > _MAX consider a WordBus resource to be invalid. Fixes a panic on
the IBM x3550-M4.

ok mikeb@


# 1.232 29-Mar-2012 mlarkin

Missing #ifdef in previous diff


# 1.231 29-Mar-2012 mlarkin

Prevent hibernation attempts on machines with unsupported disk controllers.
The same check is also performed later in the hibernate sequence, but by
then it's too late to cleanly unwind (presently).

ok deraadt


# 1.230 26-Mar-2012 deraadt

simplify conditionals in acpi_sleep_state()
ok mlarkin


# 1.229 26-Mar-2012 deraadt

hook in the hibernate request code; half of this diff is from mlarkin
ok mlarkin


Revision tags: OPENBSD_5_1_BASE
# 1.228 20-Sep-2011 deraadt

Very early in suspend while the clock is still ticking realtime,
save the clock back to the rtc. The zaurus already did this.
ok phessler miod kettenis


Revision tags: OPENBSD_5_0_BASE
# 1.227 02-Jul-2011 nicm

kqueue attach functions should return an errno or 0, not a plain 1. Fix
the obvious cases to return EINVAL and ENXIO.

ok tedu deraadt


# 1.226 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.225 16-Jun-2011 pirofti

Import acpitoshiba driver.

This is originally written by Hiroyuki Aizu, ported and integrated
in our tree by Javier Vazquez <javama4 AT gmail> with tweaks and
suggestions by me.

Import prodded by deraadt@ and the lack of testers on tech@.


# 1.224 27-Apr-2011 jcs

attach acpithinkpad to newer lenovo models like the x120e

ok deraadt@


# 1.223 22-Apr-2011 jordan

Fix uninitialzied variables and formatting strings (-Wxxx errors)


Revision tags: OPENBSD_4_9_BASE
# 1.222 02-Jan-2011 jordan

Re-add code for ACPI taskqueue, eliminates need for state vars in acpi softc
Serialize processing ACPI tasks for sleep/powerdown/GPE event
ok deraadt@


# 1.221 31-Oct-2010 guenther

Revert last commit: it breaks resume on Thinkpads
"then please back it out" deraadt@


# 1.220 26-Oct-2010 jordan

Add task queue for ACPI gpe and notify handlers
This fixes eject on Dell Latitude dock and an issue on the Dell Mini
battery update.
ok deraadt


# 1.219 07-Oct-2010 jordan

Create separate functions for enabling wake and runtime gpes.
Remove old suspend/resume gpewalk function
ok deraadt, mlarkin


# 1.218 05-Oct-2010 jordan

split acpi_enable_onegpe to separate enable/disable functions
ok deraadt


# 1.217 05-Oct-2010 jordan

Added acpi_disable_allgpes function to clear GPE status
ok deraadt


# 1.216 05-Oct-2010 deraadt

clean up acpi_softc use more


# 1.215 05-Oct-2010 deraadt

delete a bit of #if 0 code


# 1.214 05-Oct-2010 deraadt

hide use of our global acpi_softc pointer a bit better


# 1.213 29-Sep-2010 deraadt

grab the acpi thread lock very early in acpi thread startup; not that it
is likely that anyone will race us... but why be careless.
ok kettenis


# 1.212 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.211 29-Aug-2010 jasper

- add a check for FWRITE here too.

ok miod@


# 1.210 27-Aug-2010 kettenis

Make PCI Power Management optional, and only enable it when acpi(4) attaches.

ok deraadt@


Revision tags: OPENBSD_4_8_BASE
# 1.209 08-Aug-2010 kettenis

Close race between the acpi thread and wscons ioctls running in process
context. We might rip this out post-release and replace it with a solution
that serializes things in a more obvious way.

ok deraadt@, miod@ and "those crazy texans who ok anything"


# 1.208 08-Aug-2010 deraadt

Report the acpi sleep states when on a RAMDISK; ok krw


# 1.207 07-Aug-2010 kettenis

Enforce acpi register access rescrictions.

ok mlarkin@, deraadt@, tested by marco@


# 1.206 07-Aug-2010 deraadt

upon resume, notify all the acpiac and acpibat drivers to update
their status. some laptops do not do this automatically.
ok kettenis canacar


# 1.205 06-Aug-2010 marco

Always call _PSW on _LID devices that support it to enable lid open to
resume the machine. Conversely disable it when the machine wakes up.

Tested by several ok deraadt


# 1.204 06-Aug-2010 deraadt

As part of a revamp of the PM1/GPE code, I write this diff to look for
unmanaged EN & STS bits in the PM1 register at interrupt time and report
them. As a side effect this splits the STS acknowledgement into two writes
(for power, and sleep) instead of one. The printf that is added (to spot
unmanaged STS bits) has not yet been triggered as far as we know.

Before the "write to PM1 registers at the right offset" diff went in, this
was not neccessary. But newer thinkpads do not have a working soft power
button without this diff.

We have no idea why.
ok mlarkin kettenis


# 1.203 05-Aug-2010 deraadt

PM1 power button events were generating 'fake events' to acpibtn PWRB,
but if there is no such device these were being tossed. Instead, arrange
for all power button handlers to flag a softc variable which is processed
by the thread; so that one place will know to request a powerdown (psignal
to init)
ok marco mlarkin


# 1.202 05-Aug-2010 deraadt

rename a variable which is annoyingly poorly named


# 1.201 05-Aug-2010 mlarkin

Write to the correct offset for various ACPI pmreg registers. Writing to
the correct register does properly clear the wake status bits and fixed
event bits, which allows some machines to have more than 1 suspend cycle.

bug spotted by deraadt@

tested by deraadt@ and myself on a half-dozen machines and others on
hackers@

ok deraadt@, marco@


# 1.200 05-Aug-2010 deraadt

Fix the power button. The pm1 register is 16 bits in size.
My mistake, a while back.
tested by krw


# 1.199 04-Aug-2010 kettenis

Print supported sleep states.

ok marco@, phessler@, deraadt@


# 1.198 03-Aug-2010 mlarkin

Use the proper flag for re-enabling certain hardware events (power btn,
sleep btn). The (incorrect) flag prevoiously used coincidentally had the
same value, so this shouldn't cause any different behavior than before.

tested on a variety of machines (i386, amd64, sp, mp) - no behavioral
change seen

ok deraadt@


# 1.197 28-Jul-2010 marco

Make all gasio failures look and behave the same.


# 1.196 28-Jul-2010 mlarkin

Delay a little longer on suspend before giving up. Fixes machines that
sometimes don't suspend because the sleep transition takes too long
(symptoms like failing to sleep but the backlight stays on).

ok deraadt@


# 1.195 27-Jul-2010 deraadt

remove XXX; EC can now be attached early when needed; ok jordan


# 1.194 27-Jul-2010 deraadt

more brackets feels good


# 1.193 27-Jul-2010 deraadt

XXX annotate a block in acpi_enter_sleep_state() that is pretty worrying


# 1.192 27-Jul-2010 deraadt

horrific indentation that nearly made me puke


# 1.191 27-Jul-2010 mlarkin

Set the sleeping indicator light on machines that support it, in case SMI
doesn't do it for us. Shows the 'moon' LED on older Thinkpads when
sleeping.

Tested on T43p, W500, T510 by me and on X61s by deraadt@.

ok deraadt@


# 1.190 27-Jul-2010 jordan

Early initialization of acpiec if ECDT table exists
Fixes hang when booting thinkpads while docked
ok deraadt


# 1.189 26-Jul-2010 deraadt

remove merge error which was commited


# 1.188 26-Jul-2010 pirofti

Add support for Sony ACPI hotkeys via a new driver: acpisony(4).

Currently it only works for the suspend button and tries to do right for
the brightness events, but I haven't found a Sony laptop that like to do
right so far.

In the future I want to make the brightness keys work on all Sony's and
also add support for the zoom hotkeys and whatever other funky keys I can
find on those things.

Okay deraadt@.


# 1.187 22-Jul-2010 deraadt

pretty large cleanup of SMALL_KERNEL


# 1.186 21-Jul-2010 deraadt

in the acpi GPE handler, late ack level interrupts not edge interrupts.
Duh.


# 1.185 21-Jul-2010 deraadt

Merge the edge and level gpe handlers into one (now possible since the
gpe structure tells us whether the gpe is edge or level)


# 1.184 21-Jul-2010 deraadt

delete #if 0 code we are not going to use; ok marco


# 1.183 20-Jul-2010 deraadt

make the mask code for acpi_enable_onegpe() match other similar code


# 1.182 20-Jul-2010 deraadt

Handle the acpi interrupt controller with proper edge/level handling,
wrapped in spltty to avoid racing against the interrupt controller.
Repair the gpe bit masking code while there, and do operations in the
right order
ok kettenis mlarkin, help from jordan at figuring out the order of
operations.


# 1.181 20-Jul-2010 matthew

When we're preparing to go to sleep, only call bufq_restart() if other
preparations failed.

ok kettenis@, thib@, deraadt@


# 1.180 19-Jul-2010 deraadt

permit compilation with ACPI_DEBUG


# 1.179 19-Jul-2010 deraadt

there is no need for a seperate dsdt_softc pointer; dsdt does not have a
softc but is just a part of the single acpi driver.
ok jordan


# 1.178 19-Jul-2010 deraadt

acpi_delay() is no longer needed; ok jordan


# 1.177 18-Jul-2010 mlarkin

Clear event status on resume, to avoid going back to sleep/reboot/shutdown
immediately after resume. Tested by a bunch of people on hackers@ on
various hardware. Fixes at least Toshiba NB200 and Gateway LT3103u.

ok marco


# 1.176 13-Jul-2010 deraadt

rename some thread related interfaces so they are logical


# 1.175 10-Jul-2010 jordan

Verify _ADR bdf is correct (some systems return 0xffff)
Works on Dell x9xx and Studio 1555


# 1.174 09-Jul-2010 jordan

Clear the noise


# 1.173 08-Jul-2010 jordan

Add mapping for ACPI device to PCI bdf (match autoconf tree)
Simplify resource parsing function to use buffer argument
Convert namespace linked lists to use queue macros
ok marco@, deraadt@


# 1.172 06-Jul-2010 deraadt

Use spltty() to lock downcalls from apm against the information being
modified by the acpi thread.
ok kettenis


# 1.171 06-Jul-2010 deraadt

Don't use a workq to do the suspend, because it races aginst the acpi
thread. Instead, just tell the acpi thread to do the suspend for us.
This makes apmd & zzz work correctly. While here, have acpithinkpad
attempt to post the event to apm before prompting the suspend itself.
ok kettenis marco mlarkin


# 1.170 05-Jul-2010 mlarkin

Reset register SCI_EN on resume. The ACPI spec clearly states we don't have
to do this, but many machines don't, or forget to, which leads to all sorts
of problems on resume (broken battery stats, overheating, failure to resume
more than once, etc).

tested by many, ok marco@, deraadt@


# 1.169 01-Jul-2010 thib

do bufq quiesce again, since vnd/the bufq_done call in
biodone has been fixed.


# 1.168 01-Jul-2010 jordan

Backout recent AML changes, breaks on R210 and others


# 1.167 01-Jul-2010 jordan

Simplified aml_parse_resource call, cleanup code
ok marco


# 1.166 01-Jul-2010 jordan

Fix subordinate bus # for multi-root PCI buses
ok marco, kettenis


# 1.165 30-Jun-2010 thib

Disable/partially backout the bufq quiesce changes as this
is causing havoc with vnds and release must be buildable.


# 1.164 29-Jun-2010 deraadt

Fix ramdisks (new code was placed inside a SMALL_KERNEL chunk)


# 1.163 29-Jun-2010 jordan

Add support for mapping ACPI to PCI devices
ok kettenis, deraadt


# 1.162 29-Jun-2010 kettenis

Call bufq_quiesce() on suspend to stop all I/O and bufq_restart on resume.
Makes suspend/resume work much better while doing disk I/O.

ok mlarkin@


# 1.161 29-Jun-2010 tedu

60 is a more reasonable guess for number of minutes in an hour than 100.
spotted by TeXitoi. ok deraadt


# 1.160 27-Jun-2010 jordan

Add _acpi_matchhids routine, precursor to pci device match


# 1.159 27-Jun-2010 mlarkin

Clear ACPI fixed event status on resume (power buttons/etc) so that some
machines don't immediately resume after suspending to S3.

Tested by kettenis@ and I on 5 or 6 machines, deraadt@ "yeah, looks good"


# 1.158 27-Jun-2010 jordan

Cleaned up acpi_maptable, adds to table list directly


# 1.157 07-Apr-2010 deraadt

If we have done a wsdisplay_suspend(), and ACPI decides it does not want to
suspend, we must roll back by calling wsdisplay_resume() or the virtual
console switching will be locked forever
ok oga


# 1.156 07-Apr-2010 kettenis

Make suspend/resume work on MP machines (running an MP kernel). Joint work
from mlarkin@ and me, with some amd64 fixes thrown in by deraadt@

ok marco@, deraadt@, pirofti@, mlarkin@


# 1.155 31-Mar-2010 kettenis

Bring /dev/apm support in line with apm(4). Make the suspend button send
a suspend request event to apmd(8) instead of suspending immediately. Also
keep track of whether /dev/apm and /dev/apmctl are currently open, such that
we can still suspend immediately if apmd(8) isn't running.

ok deraadt@, marco@, pirofti@, jsing@, oga@


# 1.154 30-Mar-2010 oga

Prevent the apmd/x races for good.

When we hit suspend time, go through all wsdisplays on the system. if
they are in mode MAPPED, but not MODE_DUMBFB then if possible do a full
vt switch to a !mapped vt, and prevent switching back until resume time.
This has to be called from MD code because this involves userland
running so that X can run the vt switch signal handler. This way, any
case where we are using the "poke registers from userland" model, we
will not be on the hardware when we go down, so the kernel can actually
handle thing properly.

Tested on several acpi laptops (by kettenis@ and ian@), x40 (me and
beck@ at LEAST) and zaurus (me). Maybe others, but if so I forgot who at
this time..

Idea from deraadt somewhere over the Faroe Islands (I thought of a
similar thing myself a while ago). Much prompting from him. Ok and
comments miod@


# 1.153 25-Mar-2010 oga

Fix & vs. && in the kqfilter code.

Based on a comparison with the apm code.

ok deraadt@, kettenis@


Revision tags: OPENBSD_4_7_BASE
# 1.152 26-Nov-2009 mlarkin

Visual (and on some machines, audio) feedback while the machine is
resuming via calls to aml _SST method. This also makes the "moon" LED on
thinkpads to blink during resume and cease when resume is completed.

ok pirofti@


# 1.151 26-Nov-2009 deraadt

Toggle into "cold" when we we disable interrupts, because tsleeping
in the AML interpreter and context switching into userland processes
tends to sometimes pop the ACPI trampoline page out of our address
space. Makes lots more machines work.
ok kettenis mlarkin


# 1.150 24-Nov-2009 jsg

in the resume path evaluate _WAK before dealing with device drivers
like the spec says, not after.

ok mlarkin@


# 1.149 24-Nov-2009 deraadt

abort suspends on SMP systems until that gets written; ok mlarkin


# 1.148 23-Nov-2009 mlarkin

pci should return the result of it's children's suspend/resume calls back
to its parent.

handle suspend failure case and unwind if devices have an activate function
that returns failure.

ok deraadt@, kettenis@


# 1.147 23-Nov-2009 mlarkin

acpi* devices need to call their children on suspend/resume.
ok deraadt@


# 1.146 23-Nov-2009 pirofti

Remove ACPI_SLEEP_ENABLED checks.

This enables by default the suspend/resume paths in the kernel.

Okay deraadt@.


# 1.145 23-Nov-2009 deraadt

knf and spacing that are super annoying


# 1.144 23-Nov-2009 mlarkin

Hook up Theo's new children suspend/resume function, as well as more ACPI
suspend/resume glue.

ok deraadt@


# 1.143 26-Oct-2009 deraadt

Do not do strncmp comparisons on dv_xname because longer device names which
look similar could arrive in the future. Instead, compare directly against
dv_cfdata->cf_driver->cd_name
Issue originally spotted by miod


# 1.142 26-Oct-2009 jordan

Verify checksum+address when loading ACPI tables.
Some systems had invalid entries in RSDT/XSDT.
ok marco@


# 1.141 23-Jul-2009 cnst

New aibs(4) driver for ASUSTeK AI Booster (ACPI ATK0110) hardware monitoring,
with sensor state support through limits provided by the ACPI.

Tested on several ASUS motherboards kindly networked by Sam Fourman Jr.

ok deraadt marco jordan


Revision tags: OPENBSD_4_6_BASE
# 1.140 03-Jun-2009 pirofti

Import acpi power resource driver. This is needed for suspend/resume logic.

Okay jordan@ and marco@.


# 1.139 03-Jun-2009 pirofti

Add acpivideo support. This adds brightness support for all laptops
except thinkpads, they will use the acpithinkpad driver. The driver is
also hooked into wsconsole. So brightness can be adjusted via:

$ wsconsctl display.brightness=<percentage>

This is very helpfull on some laptops that have a nasty bios and get two
steps instead of one when pressing the brightness button.

Tested on various dell, fujitsu, acer, samsung and other laptops.

Okay marco@, miod@. Suggestions from kettenis@.
Lots of reviews and help from miod@, thanks!


# 1.138 03-Jun-2009 jordan

Added ejectable bay for IDE cdroms
ok marco@


# 1.137 30-Apr-2009 marco

Oga changed apm ioctls and forgot to grep. Temporary work around by hitting
the sleep button when we get any request. This interface will chnage at
some point.
ok oga


# 1.136 19-Apr-2009 krw

Call acpi_sleep_walk() when powering down just like when entering
any other sleep state. This tells acpi wake devices to not wake up
the box until manual powering up resets their status. Makes my Dell
GX520 stay powered off in response to 'halt -p'.

In snaps for a while.

ok marco@ (several times) deraadt@


# 1.135 17-Apr-2009 pirofti

Add device ranking support for future sleep/wake developments.

okay marco@


# 1.134 11-Mar-2009 jordan

Cleanup.. use aml_evalinteger instead of aml_evalname
ok marco@


# 1.133 10-Mar-2009 jordan

Fix cut/paste error.. call _BFS not _PTS
ok marco@


Revision tags: OPENBSD_4_5_BASE
# 1.132 21-Feb-2009 marco

overzealous cut/paste; found by Vladimir Kirillov <proger@uaoug.org.ua>


# 1.131 20-Feb-2009 marco

Oops didn't mean for Debugger() to remain there.


# 1.130 19-Feb-2009 marco

suspend/resume bits so that we can develop this in tree. This is disabled.
code from mlarkin and me
help from art,toby,jordan and several others
ok jordan, go for it deraadt


# 1.129 10-Feb-2009 jordan

Fix null pointer for shutdown if acpi not enabled
ok marco@


# 1.128 04-Feb-2009 kettenis

Some systems that claim to be ACPI 2.0 apparently don't provide an XSDT
address. Make the code fall back on the RSDT on those systems.

From Joerg Niendorf <p3y80@internode.on.net>. Fixes PR 5986.


# 1.127 17-Jan-2009 krw

A couple of stray malloc/memset -> malloc(M_ZERO), a bit of knf and
lose a couple of unnecessary casts. No functional change.

Feedback from kettenis@

ok kettenis@ marco@


# 1.126 06-Nov-2008 marco

Collapse all sanity checks of hids into one function.

Originaly from jordan


Revision tags: OPENBSD_4_4_BASE
# 1.125 02-Jul-2008 fgsch

add acpivideo. for now almost an empty skel so others can work on
suspend/resume. eventually it will also manage output switching and
brightness where supported. prodded by marco@


# 1.124 11-Jun-2008 marco

Fix $OpenBSD$ for once and for all


# 1.123 08-Jun-2008 deraadt

aaa_dev was being set from an uninitialized variable; ok toby


# 1.122 07-Jun-2008 marco

Kill ACPIVERBOSE. It was never used and is worthless to boot.

comments jmc
ok toby


# 1.121 06-Jun-2008 marco

Shrink acpi some more for floppies and while here re-enable acpi on the
floppies. Fixes crazy sony vaio kettenis has.

tested by deraadt
ok deraadt jordan kettenis


# 1.120 01-Jun-2008 marco

Fix \n nit in SMALL_KERNEL.


# 1.119 01-Jun-2008 marco

Re-enable new parser. Lots of testing by various folks. Fits all media
now too.

ok kettenis deraadt


# 1.118 16-May-2008 dlg

back out jordans new parser.

it didnt have enough oks from the right people, it did break machines
that were previously working, and it bloated the RAMDISKS and install
media beyond the constraints of the media.

requested by deraadt@


# 1.117 14-May-2008 jordan

Adding new ACPI Parser code
Old guts of code still exists, needs to be torn out next
ok marco@
ok brad@


# 1.116 27-Apr-2008 jcs

add acpithinkpad(4), an acpi support driver for ibm/lenovo
thinkpads.

provides functionality that the bios did on older, non-acpi
thinkpads like respond to screen brightness keys and toggle the
bluetooth device on and off.

ok deraadt@ and marco@


# 1.115 24-Apr-2008 jsing

Introduce acpiasus(4), a driver for the ACPI based hotkeys found in many
ASUS laptops (including the ASUS EeePC) - largely based on NetBSD's
asus(4) driver. On the ASUS EeePC this allows us to enable/disable
wireless, change screen brightness and use the volume keys.

ok jsg@, weingart@


# 1.114 12-Apr-2008 kettenis

Get rid of acpi_s5 global variables; simply send SIGUSR2 instead of SUGUSR1
to tell init(8) to power down the machine.

ok krw@


Revision tags: OPENBSD_4_3_BASE
# 1.113 21-Jan-2008 jsg

Check against RESET_REG_SUP in FADT before doing the acpi reset dance
after all, not doing so appears to break more machines than it fixes.

ok marco@


# 1.112 05-Jan-2008 kettenis

Never attach more acpicpu(4) devices than cpu(4) devices. Always attach
the first acpicpu(4) device on non-MULTIPROCESSOR kernels, regardless of its
ACPI processor ID.

ok mikeb@


# 1.111 05-Dec-2007 deraadt

Hang acpi(4) below bios(4) instead of mainbus(4). This lets us move acpi(4)
before pcibios(4), and then.... if acpi(4) attaches, skip pcibios(4) since
it messes the machines up. Fixes claudio's HP dl320 G5, and almost assuredly
others.
ok kettenis


# 1.110 05-Dec-2007 deraadt

repair acpiopen/acpiclose and friends to handle the /dev/apmctl interface,
so that apmd(8) can actually work correctly against acpi(4), and thus,
even do the fancy -C/-A stuff; also tested by jmc


# 1.109 02-Dec-2007 fgsch

fix the trailing space for real now.


# 1.108 02-Dec-2007 fgsch

avoid trailing spaces on output; marco@ ok.


# 1.107 27-Nov-2007 kettenis

Fix off-by-one that made us attach acpicpu0 and acpicpu1 on GENERIC kernels.

ok marco@


# 1.106 25-Nov-2007 jsg

When available and ACPI is enabled use the ACPI reset method
to reboot. This is required for some quirky sis machines
and likely others to reboot.

Tested by many. 'commit early commit often' deraadt@


# 1.105 19-Nov-2007 deraadt

sigh, amd64 still uses that stupid acpi_enabled variable...


# 1.104 19-Nov-2007 deraadt

softc's come initialized to 0, you know...


# 1.103 19-Nov-2007 deraadt

store revision; from jsg


# 1.102 19-Nov-2007 kettenis

Initialize sc_facs earlier.

ok deraadt@, beck@, canacar@


# 1.101 16-Nov-2007 deraadt

fix the bus_space #define nightmare, so that amd64 and i386 are much more
uniform. as a result shared code like acpi needs less #ifdef's
ok marco kettenis


# 1.100 15-Nov-2007 deraadt

remove the #ifdef ACPI_ENABLE. the EC codepath is either safe enough (and
will be improved), or it is unsafe because AML assumes it must be there
ok almost everyone


# 1.99 15-Nov-2007 mikeb

Make acpicpu(4) attach to the existing CPUs only.

Problem reported by form@. Fix idea by kettenis@.
Lots of help from deraadt@. Tested by deraadt@, form@ and me.

ok deraadt marco


# 1.98 14-Nov-2007 deraadt

spacing


# 1.97 12-Nov-2007 deraadt

correct : vs , in attach-time messages


# 1.96 06-Nov-2007 deraadt

all ACPI_ENABLE kernels need acpi_interrupt()


# 1.95 06-Nov-2007 deraadt

move acpi_read_pmreg() and acpi_write_pmreg() out of SMALL_KERNEL so
that acpi ramdisks can compile (obvious solution)


# 1.94 05-Nov-2007 canacar

Establish the acpi interrupt earlier. Fixes problems with some
GENERIC.MP kernels not getting acpi interrupts. This is a
workaround until the real issue with late establishment is fixed.
ok weingart@, beck@, gwk@


# 1.93 03-Nov-2007 jordan

Added support for displaying wakeup devices
ok beck@,weingart@,gwk@


# 1.92 11-Oct-2007 kettenis

When walking the tree yo find _PRT methods, check whether the device is
actually enabled and functioning. Similar to what we do for _INI.

ok weingart@, beck@


# 1.91 08-Oct-2007 krw

More simple memset(,0,) -> M_ZERO changes. In this batch move to
size(*p) as the first malloc() parameter where p is declared locally
and thus easy to check. Add M_ZERO to gpe_table allocation in acpi.c
even though there is no obvious bzero or memset nearby.


# 1.90 13-Sep-2007 mk

Remove unnecessary memset() (see aml_evalnode(9)) and a bit of KNF.

ok toby


# 1.89 13-Sep-2007 weingart

Implement ACPI 6.5.1 spec tree walk for _STA and _INI.
Fixes mk's laptop. No regressions so far (thank you to the
testers).

ok gwk@, mk@, marco@


Revision tags: OPENBSD_4_2_BASE
# 1.88 17-Apr-2007 mk

Now that acpidock no longer hangs machines during boot (at least
according to the reports I've been getting) it should be safe to enable
it again, so remove the #if 0 stuff around it.

ok marco


# 1.87 11-Apr-2007 jordan

Added changes for C-State
ok marco@


# 1.86 26-Mar-2007 marco

Calculate minutes left more accurately.

From giovanni <qgiovanni@gmail.com>


Revision tags: OPENBSD_4_1_BASE
# 1.85 03-Mar-2007 mk

Stop looking for acpidock until we fix that stuff that breaks all pre-60
series ThinkPads with acpidock(4) enabled.


# 1.84 22-Feb-2007 marco

We were getting no interrupts in .MP. There are 2 fixes; one in
acpi_machdep.c and move the interrupt establishment down after ioapic
has been initialized.

ok jordan


# 1.83 21-Feb-2007 kettenis

Make an acpi-enabled GENERIC.MP work on systems that only support PIC mode.

ok marco@


# 1.82 19-Feb-2007 jordan

Added acpi_map_address function for mapping GAS/IO registers
ok marco@


# 1.81 18-Feb-2007 jordan

Added dynamic perfect hash for opcodes
ok marco@


# 1.80 18-Feb-2007 marco

Shave off another K pre compression.
Shuffle SMALL_KERNEL functions around so that we only have a single #ifndef


# 1.79 17-Feb-2007 marco

Add SMALL_KERNEL throught acpi to make it fit on boot media.
ok deraadt


# 1.78 13-Feb-2007 marco

Add smarter _INI code. This helps with some dissapearing devices.

ok kettenis jordan


# 1.77 31-Jan-2007 gwk

Introduce acpi_hasprocfvs thats "ACPI Has Processor Frequency and Voltage
Scalling" a global flag set by acicpu attach to indicate that ACPI is
handling fvs through the _PSS objects. This will fix disappearing
EST/powernow when booting kernels with acpi enabled, as acpicpu isnt
compiled into GENERIC yet. Also in cases where acpicpu is built in and
legacy EST/powernow work but acpi is missing the _PSS object, the legacy
routines will be used.

tested by dim@, and Nick Nauwelaerts <nick AT nauwelaerts.net>
ok marco@, dim@


# 1.76 22-Jan-2007 mk

Actually look for docking stations by looking for _DCK nodes.

ok marco


# 1.75 02-Jan-2007 marco

On some systems EC must be attached as the first device. Move EC from
generic _HID devices to its own routine to force first attach. Bug found
and debugged by joris@

Code mostly written by joris@


# 1.74 26-Dec-2006 marco

Add polling to devices that require it such as acpibat and acpitz.
Use the same tsleep/wakeup mechanism as the interrupt code to keep it non-
concurrent.


# 1.73 21-Dec-2006 deraadt

even more knf love


# 1.72 21-Dec-2006 marco

Remove some leftover acpiec gpe/interrupt code.


# 1.71 20-Dec-2006 deraadt

oops, name already used


# 1.70 20-Dec-2006 deraadt

rename __acpi_enable_gpe(); ok marco


# 1.69 20-Dec-2006 deraadt

acpi_unmap_pmregs() is not used, ok marco


# 1.68 19-Dec-2006 deraadt

simplify some low-level methods substantially (shrinking code); ok marco jordan


# 1.67 06-Dec-2006 mk

malloc(..., M_NOWAIT) with no NULL check is bad. If allocation fails,
complain, clean up, and bail out of attach.

input and ok gwk


# 1.66 28-Nov-2006 jordan

Fixed clearing arg for acpi_init_pic
ok kettenis@


# 1.65 27-Nov-2006 kettenis

Don't spam dmesg.


# 1.64 27-Nov-2006 jordan

Fixes for acpiprt routing
Post-parse fixup for namerefs
ok kettenis@


# 1.63 25-Nov-2006 marco

Don't handle acpiev events inside the gpe handler since this causes deadlocks.

ok jordan@


# 1.62 15-Nov-2006 kettenis

Hook up acpi apic interrupt routing support to generic acpi code.

ok jordan@


# 1.61 02-Nov-2006 marco

Kill acpiec attach function and add its functionality to the generic _HID
device attach

prodded and ok jordan


# 1.60 02-Nov-2006 marco

Fix comments to reflect reality.


# 1.59 19-Oct-2006 marco

Replace ACPI ioctls with APM ioctls. This makes ACPI talk the same protocol
as APM therefore userspace can now retrieve power information in the same
fashion as all other apm arches (zaurus, macppc, i386 etc).

To use force the use of acpi device like so: apmd -f /dev/acpi

ok jordan@


# 1.58 19-Oct-2006 jordan

Fixed GPE debug levels
Fixed ref counter on temporary bufferfields
DSDT parsing fully working on IBM Thinkpad
ok marco@


# 1.57 15-Oct-2006 jordan

Removed old GPE handler code remnants
ok marco@


# 1.56 12-Oct-2006 jordan

Massive ACPI changes
Fixed ACPI leaks
new ACPI API
ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.55 30-Jun-2006 gwk

KNF, No functional change,
"go at it" marco@


# 1.54 30-Jun-2006 jordan

Support for calling Embedded Controller via new GPE interface
ok marco@


# 1.53 30-Jun-2006 jordan

Added new code for GPE handling
Also works with ACPIEC; need to fixup acpiec.c to remove original interrupt handler
ok marco@


# 1.52 30-Jun-2006 jordan

Added offset argument for acpi read/write pmreg routines (new GPE code)
ok marco@


# 1.51 31-May-2006 todd

do not call acpi_powerdown() unless acpi has successfully initialized
ok gwk@ canacar@


# 1.50 29-May-2006 canacar

Add embedded controller support.
ok marco@ deraadt@ testing & ok gwk@


# 1.49 19-May-2006 canacar

Minimal thermal zone driver. Monitors thermal zone temperature,
shuts down the system if the 'critical temperature' is reached.
ok marco@


# 1.48 11-Apr-2006 marco

KNF.


# 1.47 11-Apr-2006 gwk

Add acpi_resume(struct acpi_softc *sc), which calls some AML methods,
detected at attach time, currently unused.

ok marco@


# 1.46 11-Apr-2006 gwk

Discover some ACPI sleep related methods, and call them at the
appropriate time in acpi_enter_sleep_state().

ok marco@


Revision tags: OPENBSD_3_9_BASE
# 1.45 26-Feb-2006 marco

Add acpicpu as a device.

ok jordan@


# 1.44 23-Feb-2006 marco

remove dead function.

ok jordan@


# 1.43 22-Feb-2006 marco

Print out all tables it found during autoconf. We'll need this data in
the future.


# 1.42 22-Feb-2006 marco

Make dmesg less verbose.

ok jordan.


# 1.41 22-Feb-2006 jordan

Added parsing of SSDT tables
ok marco@


# 1.40 22-Feb-2006 jordan

Added new aml_register to notify by device id
Moved powerdown code to button device
ok marco@


# 1.39 21-Feb-2006 marco

Readjust debug levels that crept in too low over the last few days.
KNF


# 1.38 21-Feb-2006 marco

Rig the power button so that the machine shuts down correctly.


# 1.37 20-Feb-2006 marco

Add acpibtn device. Remains disabled for now.


# 1.36 19-Feb-2006 jordan

Added code for aml_notify GPE notification callback
ok marco@


# 1.35 19-Feb-2006 grange

Fix acpi_enter_sleep_state() by replacing current ugly hack
with mostly correct sleeping state transition code.

ok brad@ marco@


# 1.34 19-Feb-2006 marco

jordan:
Enable interrupts.
Added peripheral code to make this work.

marco:
Replace softintr and other types of interrupts with a handler with context.
Fixed interrupt magic for GPEs.
Some KNF

This is work done by jordan and I but has to be a single commit.


# 1.33 17-Feb-2006 marco

Remove earlier introduced delays. We really only need them during autconf.

ok jordan@


# 1.32 17-Feb-2006 marco

Add a generic delay function as per the ACPI spec.

from jordan@


# 1.31 17-Feb-2006 marco

Use tsleep instead of delay when !cold.


# 1.30 17-Feb-2006 marco

Argh, we need delays between reads/writes. The backend reads (i2c most likely)
are so slow that values come back as 0 sometimes. We have to do this
despite the spec saying we shouldnt have to. Guess having a faster
parser than others can hurt sometimes.

Idea from jordan.


# 1.29 17-Feb-2006 jordan

Fixed code for handling _INI
Rearranged debug statements
ok marco@


# 1.28 16-Feb-2006 jordan

Added stub for calling _INI
Fixed aml_dowhile for NULL return
ok marco@


# 1.27 16-Feb-2006 jordan

Fixed parser/evaluator to retrieve battery info+state
Cleanup of parser
Dell laptops return battery status
ok marco@


# 1.26 06-Feb-2006 brad

fix a typo so this will compile on amd64.


# 1.25 04-Feb-2006 marco

When ACPI_ENABLED wasn't set but the devices were it had an unknown variable.

Pointed out by ian@.


# 1.24 03-Feb-2006 jordan

Rewrote aml parser yet again...
Fixed bus_space_map for i386/amd64
Added _BST and _BIF to acpibat to get status/info
ok marco@


# 1.23 20-Jan-2006 jordan

More evaluator changes
Removed old evaluator code
Fixed _HID value
ok marco@


# 1.22 19-Jan-2006 jordan

Added support for read/write of PCI Config space
Help jason@
ok marco@


# 1.21 18-Jan-2006 jordan

Added new evaluation routines
Added helper methods for buffer/field evaluation
Fixed broken indentation on dsdt.c

ok marco@


# 1.20 17-Jan-2006 jordan

Major overhaul of the aml parser. Evaluater temporarily disabled.

ok marco@


# 1.19 06-Jan-2006 grange

Check for ACPI control availability only if ACPI_ENABLE defined.


# 1.18 06-Jan-2006 grange

Informative error messages on attach, prettier dmesg.


# 1.17 06-Jan-2006 grange

printf -> dnprintf conversion for debug messages.


# 1.16 05-Jan-2006 grange

Find SLP_TYPx values and store them for future use in
acpi_enter_sleep_state().


# 1.15 05-Jan-2006 grange

Random KNF and cleanup


# 1.14 05-Jan-2006 grange

Provide symbolic names for the acpi sleeping states and use it.


# 1.13 28-Dec-2005 marco

Moved DEVNAME to a acpidev.h

ok jordan@


# 1.12 18-Dec-2005 sturm

add back comment about wrong code

ok marco


# 1.11 16-Dec-2005 marco

Remove test for kbd_reset. Prompted deraadt@, ok jordan@


# 1.10 16-Dec-2005 jordan

Added hooks for powerdown/power button press


# 1.9 16-Dec-2005 jordan

Cleaned up common power management register i/o
Added enter sleep state ioctl
Hooked power button interrupt
ok marco@


# 1.8 16-Dec-2005 marco

Remove autoconf chatter


# 1.7 16-Dec-2005 jordan

Moved DSDT AML parsing into parent ACPI driver
ok marco@


# 1.6 14-Dec-2005 marco

Add AC device glue.


# 1.5 14-Dec-2005 marco

Adjust debug printing levels to something more sane.


# 1.4 13-Dec-2005 marco

Add glue to attach devices to apci.


# 1.3 07-Dec-2005 marco

KNF


Revision tags: OPENBSD_3_8_BASE
# 1.2 10-Jul-2005 grange

Spacing cleanup.


# 1.1 02-Jun-2005 tholo

Start on a basic ACPI framework -- does not do much more than read out the
ACPI tables into kernel memory and attach ACPI and HPET timers currently.

In order to test this code, enabling the devices in GENERIC as well as
the ACPI_ENABLE option is needed. This code does not do any thermal
control yet, so this should be done with care depending on the platform.

In the tree so more people can contribute to making this more fully
featured.

Ok niklas@ grange@ tedu@


# 1.396 07-Mar-2021 patrick

Pass standard DMA tag to acpi(4) table drivers.

ok kettenis@


# 1.395 27-Dec-2020 deraadt

acpi_map_address() cannot be wrapped by SMALL_KERNEL anymore, as
it is used by acpihpet.c


# 1.394 25-Dec-2020 visa

Refactor klist insertion and removal

Rename klist_{insert,remove}() to klist_{insert,remove}_locked().
These functions assume that the caller has locked the klist. The current
state of locking remains intact because the kernel lock is still used
with all klists.

Add new functions klist_insert() and klist_remove() that lock the klist
internally. This allows some code simplification.

OK mpi@


# 1.393 17-Dec-2020 kettenis

Fix some issues with referencing named ACPI nodes from Packages.
These references need to be resolved at runtime rather than when
they're parsed such that they pick up the right values for those nodes
which can be changed when for example _INI methods run.

The current approach is to replace these reference with a string that
names the node in question. The problem with that is that packages
can also contain normal strings. Which means that we need special
code that depends on the context where the package is used.

This diff takes a different approach by retaining a reference when
parsing. Code that uses a package will need to resolve this reference
but there is no ambiguiety anymore.

ok patrick@


# 1.392 05-Dec-2020 kettenis

Add support for the _EVT method to the GPIO support code.

ok patrick@


Revision tags: OPENBSD_6_8_BASE
# 1.391 27-Aug-2020 jmatthew

Reorder the acpi attach process so we look at Processor() CPU nodes
after Device() ones, since we should prefer the newer node type.
If we see any Device() nodes, don't attach acpicpu(4) to any Processor()
nodes if they're also present. This also makes acpitz(4) and
acpipwrres(4) devices attach slightly later.

ok kettenis@, also tested by jmc@


# 1.390 16-Aug-2020 gnezdo

Remove sc_maxgpe from acpi_softc which was never read from

Remove a dead conditional from acpi_init_gpes while there.

ok jcs@


# 1.389 21-Jul-2020 deraadt

acpi can use IPL_BIO (a low interrupt) since it only enqueues operations for
later processing. The use of a high interrupt will predate suspend/resume
efforts, we had to redesign acpi to be non-reentrant obviously
discussed with kettenis, in snaps for more than a week


# 1.388 24-Jun-2020 cheloha

kernel: use gettime(9)/getuptime(9) in lieu of time_second(9)/time_uptime(9)

time_second(9) and time_uptime(9) are widely used in the kernel to
quickly get the system UTC or system uptime as a time_t. However,
time_t is 64-bit everywhere, so it is not generally safe to use them
on 32-bit platforms: you have a split-read problem if your hardware
cannot perform atomic 64-bit reads.

This patch replaces time_second(9) with gettime(9), a safer successor
interface, throughout the kernel. Similarly, time_uptime(9) is replaced
with getuptime(9).

There is a performance cost on 32-bit platforms in exchange for
eliminating the split-read problem: instead of two register reads you
now have a lockless read loop to pull the values from the timehands.
This is really not *too* bad in the grand scheme of things, but
compared to what we were doing before it is several times slower.

There is no performance cost on 64-bit (__LP64__) platforms.

With input from visa@, dlg@, and tedu@.

Several bugs squashed by visa@.

ok kettenis@


# 1.387 10-Jun-2020 jca

Give userland a consistent battery and A/C state after resume

Previously we refreshed the state of acpiac(4) and acpibat(4)/acpisbs(4)
by queueing fake ACPI events to be processed by the acpi thread.
acpibat(4) using ACPIDEV_POLL meant that its status could be refreshed
first, resulting in an APM_POWER_CHANGE event sent to userland before
the state of acpiac(4) was up to date. Because of this, apmd(8) could
see A/C unplugged when it was in fact plugged, possibly leading to
a wrong decision.

Instead of using fake ACPI events, refresh the status of acpiac(4) and
acpibat(4)/acpisbs(4) right away at DVACT_WAKEUP time. To reuse the
existing refresh code of those drivers, make their ACPI *_notify()
handlers responsible for sending APM_POWER_CHANGE events instead. Also
queue an APM_POWER_CHANGE event right after resume to retain existing
behavior.

This fixes the consistency of data shown to userland, and makes those
drivers less special.

Initially discussed last year (!) with mpi@ at Miod's place.
Feedback and ok jcs@, deraadt@ agrees with the use of DVACT_WAKEUP here.


# 1.386 29-May-2020 deraadt

dev/rndvar.h no longer has statistical interfaces (removed during various
conversion steps). it only contains kernel prototypes for 4 interfaces,
all of which legitimately belong in sys/systm.h, which are already included
by all enqueue_randomness() users.


# 1.385 14-May-2020 kettenis

Bring back the acpi(4) changes. This time around only use the ACPI _CRS
information on ACPI 5.0 and later.

ok krw@, patrick@


# 1.384 11-May-2020 jca

More accurate remaining power reporting with disparate batteries

Overall remaining power was computed as the average of the remaining
power of each battery, in percents. This doesn't work with batteries of
different capacity, so use the total capacity and remaining power
instead.

Reported by lidstah, successful tests by lidstah and benno@, ok kettenis@


# 1.383 08-May-2020 kettenis

Move parsing of _CRS into acpi(4) and pass the parsed address and interrupt
information in struct acpi_attach_args.

ok mpi@


Revision tags: OPENBSD_6_7_BASE
# 1.382 14-Apr-2020 kettenis

Implement acpi_getprop(), the ACPI equivalent of OF_getprop().

ok patrick@


# 1.381 12-Apr-2020 kettenis

Add support for the _CCA method. This method indicates whether DMA is
cache-coherent or not. To implement this, acpi(4) gets two bus_dma tags
and passes the appropriate one when attaching devices based on _CCA.
On i386/amd64, where for all practical purpose DMA is always cache-coherent,
the two tags are the same. But on arm64 they are distinct.

ok patrick@


# 1.380 07-Apr-2020 visa

Abstract the head of knote lists. This allows extending the lists,
for example, with locking assertions.

OK mpi@, anton@


# 1.379 03-Apr-2020 mpi

Fix incoherencies of errors returned by various *kqfilter routines.

EOPNOTSUPP is returned when there's no "filterops" for a given fd.
EINVAL when the requested filter isn't supported by the "filterops".
ENXIO means the underlying device is no longer valid/present.

ok visa@


# 1.378 20-Feb-2020 visa

Replace field f_isfd with field f_flags in struct filterops to allow
adding more filter properties without cluttering the struct.

OK mpi@, anton@


# 1.377 31-Dec-2019 visa

Use C99 designated initializers with struct filterops. In addition,
make the structs const so that the data are put in .rodata.

OK mpi@, deraadt@, anton@, bluhm@


# 1.376 31-Dec-2019 mpi

Convert infinite sleeps to {m,t}sleep_nsec(9).

ok kn@


# 1.375 31-Dec-2019 jsg

include sys/sensors.h for sensor use
don't depend on indirect inclusion from acpidev.h


Revision tags: OPENBSD_6_6_BASE
# 1.374 07-Sep-2019 kettenis

Revert acpipci(4) changes. Too many systems are broken. We'll try again
after 6.6 as been released. The acpireg.h change stays behind.


# 1.373 28-Aug-2019 kettenis

Use ACPI information to attach PCI busses like we do on arm64. There are a
few additional quirks though, and attaching the PCI busses is delayed to
replicate the existing code more closely. That may be changed in the
future. Also tweak how we handle MSI support and respect to ACPI flag
that says we shouldn't attempt to use MSIs.

Some fallout is expected.

ok patrick@


# 1.372 27-Aug-2019 deraadt

On amd64 the bootup and resume paths for SP/BP/SP were not performing
msr/pat, clock-startup, firmware upgrade, fpu initialization, and cpu
soft-state initialization in the same order. That kind of chaos is
crazy and fragile. Improve the acpi resume call sequence and unify order
of the operations.


# 1.371 02-Jul-2019 jcs

acpi_set_gpehandler: turn edge arg into flags, add support for
passing GPE_DIRECT which directly executes the supplied callback and
clears a level interrupt rather than going through acpi_taskq which
can be slow when other driver callbacks are in the queue.

This should not be used for GPE handlers that need to do anything
ACPI-related.

ok kettenis


# 1.370 10-Jun-2019 kettenis

Print proper ACPI version number.

ok mlarkin@


# 1.369 08-Jun-2019 kettenis

When establishing the mapping between ACPI device nodes and PCI devices,
skip PCI host bridges and devices that aren't there.

ok patrick@


# 1.368 07-Jun-2019 kettenis

Introduce acpi_getsta() and use it to eliminate some duplicated code.

ok patrick@


# 1.367 12-May-2019 kettenis

Make sure the extended PM register descriptions actually exist before
using them. Fixes machines with ancient ACPI 1.0 (such as recent
hypervisors).

ok deraadt@


# 1.366 12-May-2019 kettenis

Add support for mapping ACPI PM registers using the "extended" gasio
representation from the FADT. Mostly fixes the Lanner NCA-1510.

ok lteo@, mlarkin@, deraadt@


# 1.365 11-May-2019 lteo

Fix incorrect error message.

ok kettenis@


# 1.364 08-May-2019 tedu

free size. the allocation is the header plus the length.


Revision tags: OPENBSD_6_5_BASE
# 1.363 04-Apr-2019 kettenis

Avoid potential null-pointer dereference. Found by Kent R. Spillner.

ok deraadt@, jsg@


# 1.362 20-Jan-2019 jsg

fix SMALL_KERNEL build after changes in rev 1.361
reported by naddy@ ok deraadt@


# 1.361 19-Jan-2019 tedu

Add a pwraction sysctl that controls what the power button does on acpi.
By default, nothing changes -- shutdown is initiated. But allows turning
power button into a sleep button if desired.
(grudging) ok from a few parties


# 1.360 26-Oct-2018 kettenis

Add acpipci(4) on amd64. For now this only calls the PCI-specific _OSC
method to let the ACPI implementation know what features we support.


Revision tags: OPENBSD_6_4_BASE
# 1.359 30-Aug-2018 kettenis

Move softbutton check before the check that skips attaching certain acpi
devices. Fixes regression caused by matching on _CID in addition to
matching on _HID.

ok matthieu@, mlarkin@


# 1.358 25-Aug-2018 kettenis

Match ACPI devices based on _CID if no match for _HID is found.

ok mpi@


# 1.357 19-Aug-2018 kettenis

Add support for multiple PCI segments. Only really implemented for arm64
for now as amd64/i386 firmware still caters for legacy OSes that only
support a single PCI segment.

ok patrick@


# 1.356 03-Aug-2018 kettenis

Let ahci(4) match on _CLS instead of _HID when attaching at acpi(4). Avoids
having to add many more _HID entries to the match table.

ok deraadt@, mlarkin@


# 1.355 10-Jul-2018 kettenis

Make legacy interrupts work in acpipci(4).

ok patrick@


# 1.354 05-Jul-2018 kettenis

Add acpipci(4), a driver that supports generic ECAM-compatible PCI host
bridges based on information provided by ACPI.

ok mlarkin@


# 1.353 04-Jul-2018 kettenis

Properly pass around the PCI "chipset tag" in acpi(4) and refactor
acpimcfg(4) to call an MD initialization functions that sets up a tag for
PCI ECAM.

ok guenther@, mlarkin@, krw@


# 1.352 01-Jul-2018 kettenis

Move acpi(4) attach glue into acpi_machdep.c.

ok guenther@, deraadt@


# 1.351 01-Jul-2018 kettenis

Add support for _DSD Device Properties.

ok mlarkin@


# 1.350 30-Jun-2018 kettenis

Add intr_enable() function, intended for MI use to amd64 and i386 and use
this in the acpi(4) suspend/resume code paths.

ok deraadt@


# 1.349 29-Jun-2018 kettenis

Use <stdint.h> types.

ok mpi@, jung@, krw@, deraadt@


# 1.348 29-Jun-2018 mlarkin

ACPI: Allow (with warning) GPE handler reassignment, instead of returning
a failure code.

ok kettenis


# 1.347 26-Jun-2018 mlarkin

knf


# 1.346 26-Jun-2018 kettenis

Let acpi(4) pass down the bus dma tag.

ok deraadt@


# 1.345 25-Jun-2018 kettenis

Reorganize acpi(4) code a bit in preparation for upcoming arm64 support.

ok deraadt@


# 1.344 20-May-2018 kettenis

Wake up the acpi thread after scheduling a gpio event.

ok mpi@


# 1.343 17-May-2018 kettenis

Sprinkle some #ifndef SMALL_KERNEL such that RAMDISK kernels cuild again.

pointed out by stsp@


# 1.342 17-May-2018 kettenis

Implement GenericSerialBus OpRegion support.

ok mlarkin@


# 1.341 27-Mar-2018 kettenis

Add acpicmos(4), a driver that implements SystemCMOS OperationRegion
access support. This fixes machines where the AML doesn't check whether
support for this OperationRegion type has been registered by the OS.

ok mlarkin@


Revision tags: OPENBSD_6_3_BASE
# 1.340 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.339 10-Feb-2018 deraadt

Syncronize filesystems to disk when suspending. Each mountpoint's vnodes
are pushed to disk. Dangling vnodes (unlinked files still in use) and
vnodes undergoing change by long-running syscalls are identified -- and
such filesystems are marked dirty on-disk while we are suspended (in case
power is lost, a fsck will be required). Filesystems without dangling or
busy vnodes are marked clean, resulting in faster boots following
"battery died" circumstances.
Tested by numerous developers, thanks for the feedback.


# 1.338 08-Feb-2018 deraadt

After flushing disks, try again to release all memory. We don't need to
save dirty memory to the hibernate space.


# 1.337 08-Feb-2018 deraadt

Toss all releaseable memory, because fragmentation can get in the way
of allocating the hibernate playpen.


# 1.336 08-Feb-2018 deraadt

sr_quiesce() is a new approach for ensuring that softraid drains
output to the disks.

This is part of a larger suspend/resume filesystem-safety diff, which
has been worked on for a couple of months already. Tests by job, krw,
beck, benno, and others. Sometimes even by snapshot users...


# 1.335 29-Nov-2017 kettenis

Add support for GPIO-signaled events.

ok mlarkin@


# 1.334 16-Nov-2017 jcs

Add PCI attachment for dwiic(4) needed by Intel 100 Series machines

ok kettenis


# 1.333 14-Nov-2017 kettenis

Add a few more PNP IDs to the skip lists.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.332 17-Aug-2017 stsp

Make dwiic(4) attach its ACPI parent dependencies.
This change makes the Asus E200HA keyboard work.

Original analysis and patch by Cesare Gargano
ok kettenis@


# 1.331 09-Aug-2017 dcoppa

Add "PNP0303" (8042 PS/2 Controller) to acpi_isa_hids[]

ok kettenis@


# 1.330 22-Jul-2017 jcs

acpibat: look for _BIX first before falling back on _BIF

ACPI 4.0 deprecated _BIF for battery status so some newer machines
are exporting _BIX which extends _BIF's package with a few extra
fields. When using _BIX, export the new discharge cycle count as a
sensor.

feedback from kettenis and anton
ok deraadt


# 1.329 20-Jul-2017 kettenis

Start a list of isa(4) device names such that we can avoid printing them
as "not configured".

ok mikeb@, millert@, deraadt@


# 1.328 08-Apr-2017 deraadt

Sensors are run as callbacks inside tasks. During suspend a sensor
could be running inside a driver that will be force-detached, or due
to tsleep end up disrupting the softstate/hardstate contract. At
suspend time, quisce all these callbacks by waiting for completion.
This issue has never been observed for real, but may be implicated in
suspend/resume failures.
ok kettenis guenther mlarkin


# 1.327 08-Apr-2017 deraadt

Two prototypes for acpi_maptable; merge to one place


Revision tags: OPENBSD_6_1_BASE
# 1.326 27-Mar-2017 deraadt

Now that hibernate_alloc() only has clean success/failure, don't
need to call hibernate_free() to clean up a partial mess.
ok mlarkin kettenis


# 1.325 13-Mar-2017 deraadt

allow ramdisks to build; ok jcs


# 1.324 12-Mar-2017 jcs

try to attach acpisbs first and if successful, don't attach acpibat


# 1.323 02-Mar-2017 natano

Add a new sysctl machdep.lidaction. The sysctl works as follows:

machdep.lidaction=0 # do nothing
machdep.lidaction=1 # suspend
machdep.lidaction=2 # hibernate

lidsuspend is just an alias for lidaction, so if you change one, the
other one will have the same value. The plan is to remove
machdep.lidsuspend eventually when people have upgraded their
/ets/sysctl.conf.

discussed with deraadt, who came up with the new MIB name
no objections mlarkin
ok stsp halex jcs


# 1.322 28-Feb-2017 natano

Untangle abstract sleep modes from ACPI sleep states. This paves the way
for further work in the area.

idea and ok deraadt
ok mlarkin


# 1.321 22-Feb-2017 jcs

add acpisbs, an acpi smart battery subsystem driver reading data
over smbus

currently disabled because it conflicts with acpibat

ok deraadt, kettenis


# 1.320 14-Jan-2017 kettenis

Revert aml_rdpciaddr changes; breaks several machines that were working
before.


# 1.319 11-Jan-2017 jsg

Increase the size of the buffers used for _HID and _CID strings to be
able to fit "Hyper_V_Gen_Counter_V1".

ok mlarkin@ deraadt@


# 1.318 08-Jan-2017 kettenis

Make aml_rdpciaddr() work for for devices behind bridges. The code now
uses the mapping between AML nodes and PCI devices that we establish.
Because _INIT methods may end up calling aml_rdpciaddr(), make sure we
create that mapping early. Also handle devices that aren't actually present.
These devices are now included in the mapping and reads will return an
all-ones patterm whereas writes are a no-op..

ok mlarkin@


# 1.317 25-Oct-2016 pirofti

Add the resource index to aml_parse_resource() callback function.

This is needed by an upcoming acpiec commit that handles machines breaking
the current ACPI specifications.

Change suggested by and ok kettenis@, guenther@


# 1.316 18-Sep-2016 guenther

If the APIC table (MADT) isn't present then ci_acpi_proc_id won't be set
when acpicpu tries to attach, leading to a NULL dereference. The ACPI
processor object on the Dells where this was reported is bogus and useless
anyway so apply the same test when deciding whether to attach to processor
objects as acpicpu would when matching them to cpus.

Problem reported by Eivind Eide (xenofil (at) gmail.com) and
Johan Huldtgren (johan+openbsd-bugs (at) huldtgren.com).
Testing by Eivind Eide.
ok kettenis@ mlarkin@


# 1.315 03-Sep-2016 naddy

Write the system time back to the RTC every 30 minutes.
This fixes the problem that long-running machines which were not
shut down properly would reboot with a badly offset system time.

hints and ok kettenis@


# 1.314 31-Aug-2016 mlarkin

Don't put a device in the wake device list if _STA says it's not there.

Fixes at least one machine that woke up from sleep / poweroff immediately.

ok kettenis@, tested by kettenis and martijn@


# 1.313 28-Jul-2016 kettenis

Store the acpi processor ID/UID in struct cpu_info, and use it to attach
acpicpu(4) drivers to the right cpu(4).

ok mlarkin@, guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.312 10-Jun-2016 kettenis

The power management timer has been made optional in ACPI 5.0A, so don't
attempt to attach acpitimer(4) if the timer isn't present.

Pointed out by jcs@

ok jcs@


# 1.311 08-May-2016 kettenis

Make sure devices listed by a device's _DEP method are attached before
we attach the device itself. The _DEP methode, introduced in ACPI 5.0,
is a hint that the device in question depends on OpRegion support from
the devices listed by _DEP, so we have to order them the proper way.
To prevent us from attaching those devices again when we encounter them
later walking down the device tree, keep track of our attempts to
attach a device driver to them using aflag in the aml_node structure.

ok guenther@


# 1.310 25-Apr-2016 pirofti

We attach acpihpet(4) and acpiec(4) without using the HID, so do not
report it in the dmesg.

OK deraadt@, mlarkin@.


# 1.309 02-Apr-2016 kettenis

Reduce dmesg spam by knocking out some acpi hardware IDs that don't need a
driver.

ok mpi@


# 1.308 29-Mar-2016 kettenis

Check if a device is present (using _STA) in acpi_foundhid. This prevents
us from reporting devices as "not configured" that aren't actually present,
and allows us to remove duplicated code from several drivers.

ok deraadt@, ok jsg@


# 1.307 29-Mar-2016 kettenis

Implement a cleaner way to prevent acpivout(4) from attaching if we have
a platform-specific method of controlling the backlight.

ok millert@, mlarkin@, jung@


# 1.306 28-Mar-2016 kettenis

Adding each and every new acpi device driver to acpi_foundhid() is
gettinga bit of the burden. And it really isn't how our autoconf
framework is supposed to work for busses that can be enumerated. All
the drivers already check for a matching _HID in their attach
function. So we can just drop the checks in acpi_foundhid() and let
the drivers sort themselves out.

Print a "not configured" line for devices for which we don't attach adriver.
This may be a bit much as there are devices for which we will not have a
driver. This will be removed if it causes too much dmesg spam.

ok deraadt@, mlarkin@


Revision tags: OPENBSD_5_9_BASE
# 1.305 17-Jan-2016 jsg

Take acpi_foundhid() outside of #ifndef SMALL_KERNEL so the ramdisk
kernel can find and attach dwiic(4) and sdhc(4).


# 1.304 17-Jan-2016 jsg

remove an unused global variable


# 1.303 14-Jan-2016 kettenis

Attach dwiic(4) on the Intel Bay Trail i2c controllers as well.


# 1.302 12-Jan-2016 kettenis

Improve emulation of PM registers on Hardware-redcued ACPI by emulating
SPL_TYPx, SLP_EN and WAK_STS using the new SLEEP_STATUS_REG and
SLEEP_CONTROL_REG registers. Makes the transition into S5 (powerdown) work
on the ASUS X205TA.

ok deraadt@, mlarkin@


# 1.301 12-Jan-2016 jcs

Add dwiic, a driver for the Synopsys DesignWare i2c controller found
on the Samsung ATIV Book 9 laptop. This initial version only
supports ACPI config/attachment.

Add ihidev, a HID-over-i2c driver largely based on uhidev. dwiic
handles attaching ihidev devices found in ACPI.

Add ims, a HID-over-i2c mouse/trackpad driver to get basic cursor
and button functionality from HID-compliant i2c trackpads.

ok kettenis deraadt


# 1.300 11-Jan-2016 kettenis

Make sdhc(4) attach to hardware IDs "80860F14" and "PNP0FFF".


# 1.299 09-Jan-2016 kettenis

Initial support for Hardware-Reduced ACPI. This skips the legacy mode checks
by emulating the PM_CNT register such that SCI_EN is always enabled. It also
skip attaching acpitimer(4) since the relevant registers are no longer there.

ok deraadt@, mlarkin@


# 1.298 06-Jan-2016 kettenis

Follow the spec and check the SCI_EN bit to see if ACPI has already
been enabled and don't bother enabling it again if it is. This should fix
systems that don't implement legacy mode and therefore set ACPI_ENABLE and
ACPI_DISABLE to zero.

ok mlarkin@, yasuoka@


# 1.297 23-Nov-2015 reyk

Back out the CRITICAL change, turn it back from 10% to 25%. Some
people with old/broken batteries didn't like this change, and the
underlying heuristics are wrong.


# 1.296 22-Nov-2015 reyk

The "CRITICAL" APM battery level, that is exported/faked by the ACPI
stack, is currently set to 25%. With 25% battery left, a typical
modern laptop will have at least one hour runtime left. It is an
arbitrary value, and other APM power layers in the kernel set CRITICAL
to something like 5% or 10%, so set it to 10% for now.

OK mlarkin@


# 1.295 28-Sep-2015 deraadt

In low-level suspend routines, set cold=2. In tsleep(), use this to
spit out a ddb trace to console. This should allow us to find suspend
or resume routines which break the rules. It depends on the console
output function being non-sleeping.... but that's another codepath which
should try to be safe when cold is set.
ok kettenis


# 1.294 06-Sep-2015 deraadt

clarify malloc arguments (base + length...)


# 1.293 28-Aug-2015 deraadt

fairly simple sizes for free(); ok tedu


# 1.292 20-Aug-2015 kettenis

Respect the access size when reading or writing to pci config space.
This version also makes sure writes are properly aligned, preventing
the panics seen with the previous incarnation of this change.

Fixes battery status passthrough in vmware, brightness keys on my x220
and who knows what else.

ok mlarkin@, deraadt@


Revision tags: OPENBSD_5_8_BASE
# 1.291 04-Aug-2015 deraadt

If we are setting ACPI_PM1_BM_RLD do it only once, and remember to
repeat this action at resume time.
ok mlarkin guenther


# 1.290 29-Jul-2015 deraadt

Something subtle in previous revision is locking up suspend or booting
on some laptops. Mailing lists threads mention x220 and Asus machines.
Reverting for now.


# 1.289 21-Jul-2015 kettenis

iRespect the access size when reading or writing to pci config space.
Fixes battery status passthrough in vmware and who knows what else.

Tested by many.

ok mlarkin@, deraadt@


# 1.288 17-Jul-2015 mlarkin

fix comments


# 1.287 26-Mar-2015 jsg

Use the 0x10 extended capability to determine if the trackstick
buttons are wired to the trackpad and need to be re-routed to the
trackstick. Without this change the buttons on 2015 thinkpads
get picked up as extended buttons that show up as scroll up/down.

Remove the X1 Carbon 2015 (LEN0048) and X250 (LEN0046) from the
top button area/soft buttons quirks list. Also avoid using the
quirk list entirely if the capability bit is set.

Based on work from jcs@ with feedback from mpi@
ok mpi@ jcs@


# 1.286 20-Mar-2015 kettenis

The ACPI standard says that a PCI function number of FFFF can be used to refer
to all functions on a device. This makes no sense in the context of accessing
PCI config space. Nevertheless stsp@ has a machine with AML that tries to
do config space reads from a device where _ADR encodes function number FFFF.
This is almost certainly buggy AML, but since Linux (and presumably Windows)
boot just fine on this machine we need to work around this. Linux effectively
treats function FFFF as function 7, which isn't present on this machine.
Instead we simulate a read from a non-existent device by returning a bit
pattern of all ones. Writes to function FFFF will still panic.

ok mlarkin@


# 1.285 16-Mar-2015 deraadt

Reduce use of SMALL_KERNEL in acpi, bringing acpiec driver and more gpe
handling into RAMDISK. This is now possible because the install media
has ample room. The goal is to reduce special cases where we may be
skipping (unknown) important operations...
ok mlarkin kettenis


# 1.284 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.283 07-Feb-2015 deraadt

New framework that allows hibernate to pass in entropy from it's fresh
boot.
ok mlarkin


# 1.282 06-Feb-2015 mlarkin

Fix a hibernate crash on some machines due to unmapping a page that
may not have been mapped previously (in the failure to hibernate case).

Also ensure that the lowmem ptp is mapped in all cases (not just MP).

ok kettenis


# 1.281 17-Jan-2015 deraadt

resume_randomness() before spinning up other cpus...


# 1.280 15-Jan-2015 jsg

Define a new wscons mouse type for Synaptics clickpad devices that lack
physical buttons. This will be used if the acpi pnpid for the mouse
matches a list provided by Synaptics found in Linux.

Combined with the xenocara changes this will remove the need for an xorg
config file for the x240/t440/t540 generation of thinkpads.

ok matthieu@ shadchin@ kettenis@


# 1.279 11-Jan-2015 kettenis

Tolerate checksum errors in ACPI tables (but complain about them). Apparently
there are machines out there with broken BIOSen with such checksum errors in
thier RSDT tables. Windows runs fine on these machines, so it probably does
not check. Linux ignores checksum errors as well (and prints a warning).

ok deraadt@, krw@


# 1.278 18-Dec-2014 deraadt

suspend and resume the RNG (at what looks like the right spots)
ok reyk djm


# 1.277 09-Dec-2014 doug

Replace some malloc(n*size,...) calls with mallocarray().

ok tedu@ deraadt@


# 1.276 06-Dec-2014 mpi

If "machdep.lidsuspend" is not set, do not go back to sleep if the lid
is still closed when resuming. This allows docked laptop to be resumed
without opening them.

ok mlarkin@


# 1.275 23-Nov-2014 mlarkin

If a machine has no _LIDs, then none can be open. This diff fixes a problem
where machines without any _LIDs were going back to sleep after hibernate
due to the recent suspend-if-lid-closed-on-resume diff. Noticed on qemu,
which doesn't have any _LIDs.

discussed with deraadt


# 1.274 18-Nov-2014 krw

Nuke yet more obvious #include duplications.

ok miod@


# 1.273 08-Nov-2014 mlarkin

If resuming from sleep (zzz/ZZZ) and the lid is still closed, go back to
sleep. This prevents accidental lid flex or slight opening in a backpack
from waking the machine up and leaving it resumed (powered on).

ok deraadt@


# 1.272 26-Sep-2014 kettenis

Rework piglet and pig allocation. Currently the piglet gets allocated
deep down in the suspend path, where it is really hard to recover from
allocation failure. So allocate the piglet early on in the suspend path.
Also change the piglet and piglet allocation functions to use km_alloc(9)
instead of doing pmemrange magic. This removes a bunch of code which, in the
case of the piglet allocation, is broken since it results in a NULL pointer
dereference. Also switch the piglet allocation to not wait. If we can't
allocate 16MB of phys contig memory on a halfway modern machine we're almost
certainly under a lot of memory pressure and we're better off not trying to
hibernate anyway.

ok mlarkin@


# 1.271 25-Sep-2014 kettenis

Bring back rev 1.249. Now that mp_setperf() has been fixed, it should be
safe to restore perflevel when resuming. Should fix the issues people
see with the processor running at full speed heating up the machine while
the apm command reports that the CPUs are running at a low speed.

ok dcoppa@, deraadt@


# 1.270 23-Sep-2014 kettenis

Release the acpi lock around calls to wsdisplay_suspend() and
wsdisplay_resume(). The X server may adjust the display brightness just
before/after the VT switch when suspending/resuming. In fact, it may
already be blocked adjusting the display brightness for some reason
unrelated to the suspend request.

The VT switch request will eventually time out, but the delay is annoying
and the failed VT switch will lead to problems after resume.

Needed for a future update of the xf86-video-intel driver.

ok mlarkin@


# 1.269 19-Sep-2014 kettenis

Use config_suspend_all(9).


# 1.268 08-Sep-2014 kettenis

Clear the PME Status bit on PCI devices in response to Device Wake events.
Fixes the interrupt storm on the 2009 Mac mini with WOL enabled on its
nfe(4) interface.

ok mlarkin@


Revision tags: OPENBSD_5_6_BASE
# 1.267 20-Jul-2014 mlarkin

Support hibernating to softraid crypto volumes.

much help and ok from deraadt@


# 1.266 16-Jul-2014 mlarkin

Reenable hibernate RLE support and flush+zero all memory after unpack.


# 1.265 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.264 12-Jul-2014 kettenis

Add a function to drop all clean pages on the page daemon queues and call
it when we hibernate.

ok mlarkin@, miod@, deraadt@


# 1.263 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.262 11-Jul-2014 mlarkin

Flush the buffercache to 16MB on hibernate and restore its previous max
size (kern.bufcachepercent) on resume, for better hibernate performance.

ok beck@


# 1.261 10-Jul-2014 blambert

remove unneeded header

ok mlarkin@


# 1.260 01-Jul-2014 kettenis

Adding an additonal delay after each access to SystemMemory or SystemIO
during autoconf makes no sense.

ok deraadt@


# 1.259 23-Jun-2014 kettenis

SystemMemory is used to access memory mapped registers on some machines, so we
must use the bus_space(9) API to access it instead of memcpy(9). Also make
sure we properly align access to fields that need more than byte alignment.

ok guenther@


# 1.258 21-May-2014 mlarkin

Fix invalid / missing format specifiers in several dnprintf statements
(used only when ACPI_DEBUG is enabled)

From a diff on tech@ by Fabian Raetz, with a minor change by me, tested
on i386 and amd64 with ACPI_DEBUG enabled and disabled.


# 1.257 25-Apr-2014 mlarkin

Defer the free of the hibernate structures until later, when it is safe to
do so.

Freeing the hibernate structures too early leads to inadvertent calls into
splx when we aren't ready for them yet. Found when trapping splx calls in
the resume path.

discussed with deraadt@ at m2k14


# 1.256 13-Apr-2014 mlarkin

Fix a badly worded debug message that implied the entire machine was going
to sleep, instead of just the acpi thread going to sleep.


# 1.255 31-Mar-2014 mpi

Change the order of operations during a suspend/resume cycle and
call bufq_quiesce() after executing the DVACT_QUIESCE handlers.

This should be safe since no disk nor controller drivers have such
handler but it will allow us to detach sd(4) devices attached to a
USB bus.

Another benefit pointed out by kettenis@ is that drivers that need
to read a firmware from the disk should be able to do it at resume
time in a DVACT_WAKEUP handler.

ok kettenis@, deraadt@


# 1.254 13-Mar-2014 dlg

get rid of the assumption that the head of the alldevs list is the
"mainbus" device. this breaks when mpath is enabled because it
attaches before mainbus and therefore takes the head position.

have autoconf provide device_mainbus() which looks up mainbus_cd,
and use that instead.

discussed with deraadt who just wants mpath stuff to move forward
despite there being many ways to shine this particular turd.


Revision tags: OPENBSD_5_5_BASE
# 1.253 21-Feb-2014 deraadt

Do the lid control further on in the resume sequence, since this is
AML, and will do WAITOK operations. While cold, that is a bad idea.
We seem to be safe with respect to the spec, I do not see any event
debounce issues here.
ok kettenis, tested by many


# 1.252 20-Jan-2014 claudio

Instead of checking sc->sc_revision <= 1 check that the
fadt->hdr_revision <= 1. This is a better check since it checks
the version of the proper descriptor. This allows the alix APU
to reboot since those boards have currently a wrong sc->sc_revision.
Committing this now so that it can be tested easier.
With and OK jsg@ also fine by derradt@


# 1.251 24-Dec-2013 kettenis

If the FADT has its SMI_CMD set to zero, assume we're only ACPI-only hardware
and don't need to disable SMI ownership of the ACPI hardware registers.

ok mlarkin@


# 1.250 23-Dec-2013 kettenis

Revert previous commit. It makes resume hang on some MP systems running
GENERIC.MP.


# 1.249 16-Dec-2013 kettenis

Restore hw.setperf upon resume like we do for apm(4) on i386. Seems we need
to do this fairly late in the resume to avoid hangs.

ok deraadt@, mlarkin@


# 1.248 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


# 1.247 06-Nov-2013 mpi

Add support for Power Resources for Dx states and the necessary hook
for PCI devices. This hook should be called twice, before and after
changing the power state of a PCI device.

Before setting the device to the new state, the ACPI layer will notify
every power resources linked to the device for that state and make sure
they are turned "_ON". After changing the state of the device, it will
decrement the reference of every power resources linked to that device
for the old state and turn them "_OFF" if they are no longer referenced.

This fixes the no-USB after resume problem seen on various ThinkPad,
problem initialy diagnosed with Alexander Polakov.

ok kettenis@, deraadt@


Revision tags: OPENBSD_5_4_BASE
# 1.246 01-Jun-2013 mlarkin

Stop printing ACPI wakeup devices in dmesg after the 16th wakeup device.

Helps with VMware VMs that report hundreds of wakeup devices.

ok reyk@, deraadt@


# 1.245 31-May-2013 bluhm

Fix the build for a kernel without wd and pciide in its config.
OK deraadt@ tedu@


# 1.244 30-May-2013 deraadt

Enforce ca_activate tree-walks over the entire heirarchy for all events,
cleaning up some shutdown-hook related code on the way.
(A few drivers related to sparc are still skipped at kettenis' request)
ok kettenis mlarkin, tested by many others too


# 1.243 18-Apr-2013 deraadt

pin the acpi thread to the BSP, because AML and SMI cannot be trusted. I
was certain we were doing this already, but it turns out no...


Revision tags: OPENBSD_5_3_BASE
# 1.242 24-Dec-2012 guenther

In acpiioctl(), Return though the splx() path if suser() returns an error

ok deraadt@


# 1.241 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


# 1.240 04-Oct-2012 ehrhardt

Use information provided by ACPI to attach secondary PCI host bridges.

ok kettenis@


# 1.239 07-Sep-2012 kettenis

Add a new function called acpi_pci_min_powerstate() that returns the lowest
power state for a PCI device for the power state (currently S3 or S4) we're
transitioning into. This function is probably too simplistic; there is a lot
of (silly) complexity in the ACPI spec.

ok deraadt@


Revision tags: OPENBSD_5_2_BASE
# 1.238 13-Jul-2012 pirofti

Disable acpivideo(4) on ASUS laptops.

There are some models that panic whenever an event is received.
Some cope, some don't. In order to get this right acpiec(4) requires
major rework. Until then let machines boot.

Discussed with deraadt@.


# 1.237 11-Jul-2012 mlarkin

for now, restrict hibernate to root in the acpi ioctl kernel interface.
normal rules for controlling this interface through apmd still apply.

ok deraadt@


# 1.236 09-Jul-2012 deraadt

tiny little oops in untested hibernate block


# 1.235 09-Jul-2012 deraadt

clean up ioctl() case statements


# 1.234 09-Jul-2012 deraadt

acpi_disable_onegpe() is not used; ok pirofti


# 1.233 24-May-2012 kettenis

If _MIN > _MAX consider a WordBus resource to be invalid. Fixes a panic on
the IBM x3550-M4.

ok mikeb@


# 1.232 29-Mar-2012 mlarkin

Missing #ifdef in previous diff


# 1.231 29-Mar-2012 mlarkin

Prevent hibernation attempts on machines with unsupported disk controllers.
The same check is also performed later in the hibernate sequence, but by
then it's too late to cleanly unwind (presently).

ok deraadt


# 1.230 26-Mar-2012 deraadt

simplify conditionals in acpi_sleep_state()
ok mlarkin


# 1.229 26-Mar-2012 deraadt

hook in the hibernate request code; half of this diff is from mlarkin
ok mlarkin


Revision tags: OPENBSD_5_1_BASE
# 1.228 20-Sep-2011 deraadt

Very early in suspend while the clock is still ticking realtime,
save the clock back to the rtc. The zaurus already did this.
ok phessler miod kettenis


Revision tags: OPENBSD_5_0_BASE
# 1.227 02-Jul-2011 nicm

kqueue attach functions should return an errno or 0, not a plain 1. Fix
the obvious cases to return EINVAL and ENXIO.

ok tedu deraadt


# 1.226 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.225 16-Jun-2011 pirofti

Import acpitoshiba driver.

This is originally written by Hiroyuki Aizu, ported and integrated
in our tree by Javier Vazquez <javama4 AT gmail> with tweaks and
suggestions by me.

Import prodded by deraadt@ and the lack of testers on tech@.


# 1.224 27-Apr-2011 jcs

attach acpithinkpad to newer lenovo models like the x120e

ok deraadt@


# 1.223 22-Apr-2011 jordan

Fix uninitialzied variables and formatting strings (-Wxxx errors)


Revision tags: OPENBSD_4_9_BASE
# 1.222 02-Jan-2011 jordan

Re-add code for ACPI taskqueue, eliminates need for state vars in acpi softc
Serialize processing ACPI tasks for sleep/powerdown/GPE event
ok deraadt@


# 1.221 31-Oct-2010 guenther

Revert last commit: it breaks resume on Thinkpads
"then please back it out" deraadt@


# 1.220 26-Oct-2010 jordan

Add task queue for ACPI gpe and notify handlers
This fixes eject on Dell Latitude dock and an issue on the Dell Mini
battery update.
ok deraadt


# 1.219 07-Oct-2010 jordan

Create separate functions for enabling wake and runtime gpes.
Remove old suspend/resume gpewalk function
ok deraadt, mlarkin


# 1.218 05-Oct-2010 jordan

split acpi_enable_onegpe to separate enable/disable functions
ok deraadt


# 1.217 05-Oct-2010 jordan

Added acpi_disable_allgpes function to clear GPE status
ok deraadt


# 1.216 05-Oct-2010 deraadt

clean up acpi_softc use more


# 1.215 05-Oct-2010 deraadt

delete a bit of #if 0 code


# 1.214 05-Oct-2010 deraadt

hide use of our global acpi_softc pointer a bit better


# 1.213 29-Sep-2010 deraadt

grab the acpi thread lock very early in acpi thread startup; not that it
is likely that anyone will race us... but why be careless.
ok kettenis


# 1.212 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.211 29-Aug-2010 jasper

- add a check for FWRITE here too.

ok miod@


# 1.210 27-Aug-2010 kettenis

Make PCI Power Management optional, and only enable it when acpi(4) attaches.

ok deraadt@


Revision tags: OPENBSD_4_8_BASE
# 1.209 08-Aug-2010 kettenis

Close race between the acpi thread and wscons ioctls running in process
context. We might rip this out post-release and replace it with a solution
that serializes things in a more obvious way.

ok deraadt@, miod@ and "those crazy texans who ok anything"


# 1.208 08-Aug-2010 deraadt

Report the acpi sleep states when on a RAMDISK; ok krw


# 1.207 07-Aug-2010 kettenis

Enforce acpi register access rescrictions.

ok mlarkin@, deraadt@, tested by marco@


# 1.206 07-Aug-2010 deraadt

upon resume, notify all the acpiac and acpibat drivers to update
their status. some laptops do not do this automatically.
ok kettenis canacar


# 1.205 06-Aug-2010 marco

Always call _PSW on _LID devices that support it to enable lid open to
resume the machine. Conversely disable it when the machine wakes up.

Tested by several ok deraadt


# 1.204 06-Aug-2010 deraadt

As part of a revamp of the PM1/GPE code, I write this diff to look for
unmanaged EN & STS bits in the PM1 register at interrupt time and report
them. As a side effect this splits the STS acknowledgement into two writes
(for power, and sleep) instead of one. The printf that is added (to spot
unmanaged STS bits) has not yet been triggered as far as we know.

Before the "write to PM1 registers at the right offset" diff went in, this
was not neccessary. But newer thinkpads do not have a working soft power
button without this diff.

We have no idea why.
ok mlarkin kettenis


# 1.203 05-Aug-2010 deraadt

PM1 power button events were generating 'fake events' to acpibtn PWRB,
but if there is no such device these were being tossed. Instead, arrange
for all power button handlers to flag a softc variable which is processed
by the thread; so that one place will know to request a powerdown (psignal
to init)
ok marco mlarkin


# 1.202 05-Aug-2010 deraadt

rename a variable which is annoyingly poorly named


# 1.201 05-Aug-2010 mlarkin

Write to the correct offset for various ACPI pmreg registers. Writing to
the correct register does properly clear the wake status bits and fixed
event bits, which allows some machines to have more than 1 suspend cycle.

bug spotted by deraadt@

tested by deraadt@ and myself on a half-dozen machines and others on
hackers@

ok deraadt@, marco@


# 1.200 05-Aug-2010 deraadt

Fix the power button. The pm1 register is 16 bits in size.
My mistake, a while back.
tested by krw


# 1.199 04-Aug-2010 kettenis

Print supported sleep states.

ok marco@, phessler@, deraadt@


# 1.198 03-Aug-2010 mlarkin

Use the proper flag for re-enabling certain hardware events (power btn,
sleep btn). The (incorrect) flag prevoiously used coincidentally had the
same value, so this shouldn't cause any different behavior than before.

tested on a variety of machines (i386, amd64, sp, mp) - no behavioral
change seen

ok deraadt@


# 1.197 28-Jul-2010 marco

Make all gasio failures look and behave the same.


# 1.196 28-Jul-2010 mlarkin

Delay a little longer on suspend before giving up. Fixes machines that
sometimes don't suspend because the sleep transition takes too long
(symptoms like failing to sleep but the backlight stays on).

ok deraadt@


# 1.195 27-Jul-2010 deraadt

remove XXX; EC can now be attached early when needed; ok jordan


# 1.194 27-Jul-2010 deraadt

more brackets feels good


# 1.193 27-Jul-2010 deraadt

XXX annotate a block in acpi_enter_sleep_state() that is pretty worrying


# 1.192 27-Jul-2010 deraadt

horrific indentation that nearly made me puke


# 1.191 27-Jul-2010 mlarkin

Set the sleeping indicator light on machines that support it, in case SMI
doesn't do it for us. Shows the 'moon' LED on older Thinkpads when
sleeping.

Tested on T43p, W500, T510 by me and on X61s by deraadt@.

ok deraadt@


# 1.190 27-Jul-2010 jordan

Early initialization of acpiec if ECDT table exists
Fixes hang when booting thinkpads while docked
ok deraadt


# 1.189 26-Jul-2010 deraadt

remove merge error which was commited


# 1.188 26-Jul-2010 pirofti

Add support for Sony ACPI hotkeys via a new driver: acpisony(4).

Currently it only works for the suspend button and tries to do right for
the brightness events, but I haven't found a Sony laptop that like to do
right so far.

In the future I want to make the brightness keys work on all Sony's and
also add support for the zoom hotkeys and whatever other funky keys I can
find on those things.

Okay deraadt@.


# 1.187 22-Jul-2010 deraadt

pretty large cleanup of SMALL_KERNEL


# 1.186 21-Jul-2010 deraadt

in the acpi GPE handler, late ack level interrupts not edge interrupts.
Duh.


# 1.185 21-Jul-2010 deraadt

Merge the edge and level gpe handlers into one (now possible since the
gpe structure tells us whether the gpe is edge or level)


# 1.184 21-Jul-2010 deraadt

delete #if 0 code we are not going to use; ok marco


# 1.183 20-Jul-2010 deraadt

make the mask code for acpi_enable_onegpe() match other similar code


# 1.182 20-Jul-2010 deraadt

Handle the acpi interrupt controller with proper edge/level handling,
wrapped in spltty to avoid racing against the interrupt controller.
Repair the gpe bit masking code while there, and do operations in the
right order
ok kettenis mlarkin, help from jordan at figuring out the order of
operations.


# 1.181 20-Jul-2010 matthew

When we're preparing to go to sleep, only call bufq_restart() if other
preparations failed.

ok kettenis@, thib@, deraadt@


# 1.180 19-Jul-2010 deraadt

permit compilation with ACPI_DEBUG


# 1.179 19-Jul-2010 deraadt

there is no need for a seperate dsdt_softc pointer; dsdt does not have a
softc but is just a part of the single acpi driver.
ok jordan


# 1.178 19-Jul-2010 deraadt

acpi_delay() is no longer needed; ok jordan


# 1.177 18-Jul-2010 mlarkin

Clear event status on resume, to avoid going back to sleep/reboot/shutdown
immediately after resume. Tested by a bunch of people on hackers@ on
various hardware. Fixes at least Toshiba NB200 and Gateway LT3103u.

ok marco


# 1.176 13-Jul-2010 deraadt

rename some thread related interfaces so they are logical


# 1.175 10-Jul-2010 jordan

Verify _ADR bdf is correct (some systems return 0xffff)
Works on Dell x9xx and Studio 1555


# 1.174 09-Jul-2010 jordan

Clear the noise


# 1.173 08-Jul-2010 jordan

Add mapping for ACPI device to PCI bdf (match autoconf tree)
Simplify resource parsing function to use buffer argument
Convert namespace linked lists to use queue macros
ok marco@, deraadt@


# 1.172 06-Jul-2010 deraadt

Use spltty() to lock downcalls from apm against the information being
modified by the acpi thread.
ok kettenis


# 1.171 06-Jul-2010 deraadt

Don't use a workq to do the suspend, because it races aginst the acpi
thread. Instead, just tell the acpi thread to do the suspend for us.
This makes apmd & zzz work correctly. While here, have acpithinkpad
attempt to post the event to apm before prompting the suspend itself.
ok kettenis marco mlarkin


# 1.170 05-Jul-2010 mlarkin

Reset register SCI_EN on resume. The ACPI spec clearly states we don't have
to do this, but many machines don't, or forget to, which leads to all sorts
of problems on resume (broken battery stats, overheating, failure to resume
more than once, etc).

tested by many, ok marco@, deraadt@


# 1.169 01-Jul-2010 thib

do bufq quiesce again, since vnd/the bufq_done call in
biodone has been fixed.


# 1.168 01-Jul-2010 jordan

Backout recent AML changes, breaks on R210 and others


# 1.167 01-Jul-2010 jordan

Simplified aml_parse_resource call, cleanup code
ok marco


# 1.166 01-Jul-2010 jordan

Fix subordinate bus # for multi-root PCI buses
ok marco, kettenis


# 1.165 30-Jun-2010 thib

Disable/partially backout the bufq quiesce changes as this
is causing havoc with vnds and release must be buildable.


# 1.164 29-Jun-2010 deraadt

Fix ramdisks (new code was placed inside a SMALL_KERNEL chunk)


# 1.163 29-Jun-2010 jordan

Add support for mapping ACPI to PCI devices
ok kettenis, deraadt


# 1.162 29-Jun-2010 kettenis

Call bufq_quiesce() on suspend to stop all I/O and bufq_restart on resume.
Makes suspend/resume work much better while doing disk I/O.

ok mlarkin@


# 1.161 29-Jun-2010 tedu

60 is a more reasonable guess for number of minutes in an hour than 100.
spotted by TeXitoi. ok deraadt


# 1.160 27-Jun-2010 jordan

Add _acpi_matchhids routine, precursor to pci device match


# 1.159 27-Jun-2010 mlarkin

Clear ACPI fixed event status on resume (power buttons/etc) so that some
machines don't immediately resume after suspending to S3.

Tested by kettenis@ and I on 5 or 6 machines, deraadt@ "yeah, looks good"


# 1.158 27-Jun-2010 jordan

Cleaned up acpi_maptable, adds to table list directly


# 1.157 07-Apr-2010 deraadt

If we have done a wsdisplay_suspend(), and ACPI decides it does not want to
suspend, we must roll back by calling wsdisplay_resume() or the virtual
console switching will be locked forever
ok oga


# 1.156 07-Apr-2010 kettenis

Make suspend/resume work on MP machines (running an MP kernel). Joint work
from mlarkin@ and me, with some amd64 fixes thrown in by deraadt@

ok marco@, deraadt@, pirofti@, mlarkin@


# 1.155 31-Mar-2010 kettenis

Bring /dev/apm support in line with apm(4). Make the suspend button send
a suspend request event to apmd(8) instead of suspending immediately. Also
keep track of whether /dev/apm and /dev/apmctl are currently open, such that
we can still suspend immediately if apmd(8) isn't running.

ok deraadt@, marco@, pirofti@, jsing@, oga@


# 1.154 30-Mar-2010 oga

Prevent the apmd/x races for good.

When we hit suspend time, go through all wsdisplays on the system. if
they are in mode MAPPED, but not MODE_DUMBFB then if possible do a full
vt switch to a !mapped vt, and prevent switching back until resume time.
This has to be called from MD code because this involves userland
running so that X can run the vt switch signal handler. This way, any
case where we are using the "poke registers from userland" model, we
will not be on the hardware when we go down, so the kernel can actually
handle thing properly.

Tested on several acpi laptops (by kettenis@ and ian@), x40 (me and
beck@ at LEAST) and zaurus (me). Maybe others, but if so I forgot who at
this time..

Idea from deraadt somewhere over the Faroe Islands (I thought of a
similar thing myself a while ago). Much prompting from him. Ok and
comments miod@


# 1.153 25-Mar-2010 oga

Fix & vs. && in the kqfilter code.

Based on a comparison with the apm code.

ok deraadt@, kettenis@


Revision tags: OPENBSD_4_7_BASE
# 1.152 26-Nov-2009 mlarkin

Visual (and on some machines, audio) feedback while the machine is
resuming via calls to aml _SST method. This also makes the "moon" LED on
thinkpads to blink during resume and cease when resume is completed.

ok pirofti@


# 1.151 26-Nov-2009 deraadt

Toggle into "cold" when we we disable interrupts, because tsleeping
in the AML interpreter and context switching into userland processes
tends to sometimes pop the ACPI trampoline page out of our address
space. Makes lots more machines work.
ok kettenis mlarkin


# 1.150 24-Nov-2009 jsg

in the resume path evaluate _WAK before dealing with device drivers
like the spec says, not after.

ok mlarkin@


# 1.149 24-Nov-2009 deraadt

abort suspends on SMP systems until that gets written; ok mlarkin


# 1.148 23-Nov-2009 mlarkin

pci should return the result of it's children's suspend/resume calls back
to its parent.

handle suspend failure case and unwind if devices have an activate function
that returns failure.

ok deraadt@, kettenis@


# 1.147 23-Nov-2009 mlarkin

acpi* devices need to call their children on suspend/resume.
ok deraadt@


# 1.146 23-Nov-2009 pirofti

Remove ACPI_SLEEP_ENABLED checks.

This enables by default the suspend/resume paths in the kernel.

Okay deraadt@.


# 1.145 23-Nov-2009 deraadt

knf and spacing that are super annoying


# 1.144 23-Nov-2009 mlarkin

Hook up Theo's new children suspend/resume function, as well as more ACPI
suspend/resume glue.

ok deraadt@


# 1.143 26-Oct-2009 deraadt

Do not do strncmp comparisons on dv_xname because longer device names which
look similar could arrive in the future. Instead, compare directly against
dv_cfdata->cf_driver->cd_name
Issue originally spotted by miod


# 1.142 26-Oct-2009 jordan

Verify checksum+address when loading ACPI tables.
Some systems had invalid entries in RSDT/XSDT.
ok marco@


# 1.141 23-Jul-2009 cnst

New aibs(4) driver for ASUSTeK AI Booster (ACPI ATK0110) hardware monitoring,
with sensor state support through limits provided by the ACPI.

Tested on several ASUS motherboards kindly networked by Sam Fourman Jr.

ok deraadt marco jordan


Revision tags: OPENBSD_4_6_BASE
# 1.140 03-Jun-2009 pirofti

Import acpi power resource driver. This is needed for suspend/resume logic.

Okay jordan@ and marco@.


# 1.139 03-Jun-2009 pirofti

Add acpivideo support. This adds brightness support for all laptops
except thinkpads, they will use the acpithinkpad driver. The driver is
also hooked into wsconsole. So brightness can be adjusted via:

$ wsconsctl display.brightness=<percentage>

This is very helpfull on some laptops that have a nasty bios and get two
steps instead of one when pressing the brightness button.

Tested on various dell, fujitsu, acer, samsung and other laptops.

Okay marco@, miod@. Suggestions from kettenis@.
Lots of reviews and help from miod@, thanks!


# 1.138 03-Jun-2009 jordan

Added ejectable bay for IDE cdroms
ok marco@


# 1.137 30-Apr-2009 marco

Oga changed apm ioctls and forgot to grep. Temporary work around by hitting
the sleep button when we get any request. This interface will chnage at
some point.
ok oga


# 1.136 19-Apr-2009 krw

Call acpi_sleep_walk() when powering down just like when entering
any other sleep state. This tells acpi wake devices to not wake up
the box until manual powering up resets their status. Makes my Dell
GX520 stay powered off in response to 'halt -p'.

In snaps for a while.

ok marco@ (several times) deraadt@


# 1.135 17-Apr-2009 pirofti

Add device ranking support for future sleep/wake developments.

okay marco@


# 1.134 11-Mar-2009 jordan

Cleanup.. use aml_evalinteger instead of aml_evalname
ok marco@


# 1.133 10-Mar-2009 jordan

Fix cut/paste error.. call _BFS not _PTS
ok marco@


Revision tags: OPENBSD_4_5_BASE
# 1.132 21-Feb-2009 marco

overzealous cut/paste; found by Vladimir Kirillov <proger@uaoug.org.ua>


# 1.131 20-Feb-2009 marco

Oops didn't mean for Debugger() to remain there.


# 1.130 19-Feb-2009 marco

suspend/resume bits so that we can develop this in tree. This is disabled.
code from mlarkin and me
help from art,toby,jordan and several others
ok jordan, go for it deraadt


# 1.129 10-Feb-2009 jordan

Fix null pointer for shutdown if acpi not enabled
ok marco@


# 1.128 04-Feb-2009 kettenis

Some systems that claim to be ACPI 2.0 apparently don't provide an XSDT
address. Make the code fall back on the RSDT on those systems.

From Joerg Niendorf <p3y80@internode.on.net>. Fixes PR 5986.


# 1.127 17-Jan-2009 krw

A couple of stray malloc/memset -> malloc(M_ZERO), a bit of knf and
lose a couple of unnecessary casts. No functional change.

Feedback from kettenis@

ok kettenis@ marco@


# 1.126 06-Nov-2008 marco

Collapse all sanity checks of hids into one function.

Originaly from jordan


Revision tags: OPENBSD_4_4_BASE
# 1.125 02-Jul-2008 fgsch

add acpivideo. for now almost an empty skel so others can work on
suspend/resume. eventually it will also manage output switching and
brightness where supported. prodded by marco@


# 1.124 11-Jun-2008 marco

Fix $OpenBSD$ for once and for all


# 1.123 08-Jun-2008 deraadt

aaa_dev was being set from an uninitialized variable; ok toby


# 1.122 07-Jun-2008 marco

Kill ACPIVERBOSE. It was never used and is worthless to boot.

comments jmc
ok toby


# 1.121 06-Jun-2008 marco

Shrink acpi some more for floppies and while here re-enable acpi on the
floppies. Fixes crazy sony vaio kettenis has.

tested by deraadt
ok deraadt jordan kettenis


# 1.120 01-Jun-2008 marco

Fix \n nit in SMALL_KERNEL.


# 1.119 01-Jun-2008 marco

Re-enable new parser. Lots of testing by various folks. Fits all media
now too.

ok kettenis deraadt


# 1.118 16-May-2008 dlg

back out jordans new parser.

it didnt have enough oks from the right people, it did break machines
that were previously working, and it bloated the RAMDISKS and install
media beyond the constraints of the media.

requested by deraadt@


# 1.117 14-May-2008 jordan

Adding new ACPI Parser code
Old guts of code still exists, needs to be torn out next
ok marco@
ok brad@


# 1.116 27-Apr-2008 jcs

add acpithinkpad(4), an acpi support driver for ibm/lenovo
thinkpads.

provides functionality that the bios did on older, non-acpi
thinkpads like respond to screen brightness keys and toggle the
bluetooth device on and off.

ok deraadt@ and marco@


# 1.115 24-Apr-2008 jsing

Introduce acpiasus(4), a driver for the ACPI based hotkeys found in many
ASUS laptops (including the ASUS EeePC) - largely based on NetBSD's
asus(4) driver. On the ASUS EeePC this allows us to enable/disable
wireless, change screen brightness and use the volume keys.

ok jsg@, weingart@


# 1.114 12-Apr-2008 kettenis

Get rid of acpi_s5 global variables; simply send SIGUSR2 instead of SUGUSR1
to tell init(8) to power down the machine.

ok krw@


Revision tags: OPENBSD_4_3_BASE
# 1.113 21-Jan-2008 jsg

Check against RESET_REG_SUP in FADT before doing the acpi reset dance
after all, not doing so appears to break more machines than it fixes.

ok marco@


# 1.112 05-Jan-2008 kettenis

Never attach more acpicpu(4) devices than cpu(4) devices. Always attach
the first acpicpu(4) device on non-MULTIPROCESSOR kernels, regardless of its
ACPI processor ID.

ok mikeb@


# 1.111 05-Dec-2007 deraadt

Hang acpi(4) below bios(4) instead of mainbus(4). This lets us move acpi(4)
before pcibios(4), and then.... if acpi(4) attaches, skip pcibios(4) since
it messes the machines up. Fixes claudio's HP dl320 G5, and almost assuredly
others.
ok kettenis


# 1.110 05-Dec-2007 deraadt

repair acpiopen/acpiclose and friends to handle the /dev/apmctl interface,
so that apmd(8) can actually work correctly against acpi(4), and thus,
even do the fancy -C/-A stuff; also tested by jmc


# 1.109 02-Dec-2007 fgsch

fix the trailing space for real now.


# 1.108 02-Dec-2007 fgsch

avoid trailing spaces on output; marco@ ok.


# 1.107 27-Nov-2007 kettenis

Fix off-by-one that made us attach acpicpu0 and acpicpu1 on GENERIC kernels.

ok marco@


# 1.106 25-Nov-2007 jsg

When available and ACPI is enabled use the ACPI reset method
to reboot. This is required for some quirky sis machines
and likely others to reboot.

Tested by many. 'commit early commit often' deraadt@


# 1.105 19-Nov-2007 deraadt

sigh, amd64 still uses that stupid acpi_enabled variable...


# 1.104 19-Nov-2007 deraadt

softc's come initialized to 0, you know...


# 1.103 19-Nov-2007 deraadt

store revision; from jsg


# 1.102 19-Nov-2007 kettenis

Initialize sc_facs earlier.

ok deraadt@, beck@, canacar@


# 1.101 16-Nov-2007 deraadt

fix the bus_space #define nightmare, so that amd64 and i386 are much more
uniform. as a result shared code like acpi needs less #ifdef's
ok marco kettenis


# 1.100 15-Nov-2007 deraadt

remove the #ifdef ACPI_ENABLE. the EC codepath is either safe enough (and
will be improved), or it is unsafe because AML assumes it must be there
ok almost everyone


# 1.99 15-Nov-2007 mikeb

Make acpicpu(4) attach to the existing CPUs only.

Problem reported by form@. Fix idea by kettenis@.
Lots of help from deraadt@. Tested by deraadt@, form@ and me.

ok deraadt marco


# 1.98 14-Nov-2007 deraadt

spacing


# 1.97 12-Nov-2007 deraadt

correct : vs , in attach-time messages


# 1.96 06-Nov-2007 deraadt

all ACPI_ENABLE kernels need acpi_interrupt()


# 1.95 06-Nov-2007 deraadt

move acpi_read_pmreg() and acpi_write_pmreg() out of SMALL_KERNEL so
that acpi ramdisks can compile (obvious solution)


# 1.94 05-Nov-2007 canacar

Establish the acpi interrupt earlier. Fixes problems with some
GENERIC.MP kernels not getting acpi interrupts. This is a
workaround until the real issue with late establishment is fixed.
ok weingart@, beck@, gwk@


# 1.93 03-Nov-2007 jordan

Added support for displaying wakeup devices
ok beck@,weingart@,gwk@


# 1.92 11-Oct-2007 kettenis

When walking the tree yo find _PRT methods, check whether the device is
actually enabled and functioning. Similar to what we do for _INI.

ok weingart@, beck@


# 1.91 08-Oct-2007 krw

More simple memset(,0,) -> M_ZERO changes. In this batch move to
size(*p) as the first malloc() parameter where p is declared locally
and thus easy to check. Add M_ZERO to gpe_table allocation in acpi.c
even though there is no obvious bzero or memset nearby.


# 1.90 13-Sep-2007 mk

Remove unnecessary memset() (see aml_evalnode(9)) and a bit of KNF.

ok toby


# 1.89 13-Sep-2007 weingart

Implement ACPI 6.5.1 spec tree walk for _STA and _INI.
Fixes mk's laptop. No regressions so far (thank you to the
testers).

ok gwk@, mk@, marco@


Revision tags: OPENBSD_4_2_BASE
# 1.88 17-Apr-2007 mk

Now that acpidock no longer hangs machines during boot (at least
according to the reports I've been getting) it should be safe to enable
it again, so remove the #if 0 stuff around it.

ok marco


# 1.87 11-Apr-2007 jordan

Added changes for C-State
ok marco@


# 1.86 26-Mar-2007 marco

Calculate minutes left more accurately.

From giovanni <qgiovanni@gmail.com>


Revision tags: OPENBSD_4_1_BASE
# 1.85 03-Mar-2007 mk

Stop looking for acpidock until we fix that stuff that breaks all pre-60
series ThinkPads with acpidock(4) enabled.


# 1.84 22-Feb-2007 marco

We were getting no interrupts in .MP. There are 2 fixes; one in
acpi_machdep.c and move the interrupt establishment down after ioapic
has been initialized.

ok jordan


# 1.83 21-Feb-2007 kettenis

Make an acpi-enabled GENERIC.MP work on systems that only support PIC mode.

ok marco@


# 1.82 19-Feb-2007 jordan

Added acpi_map_address function for mapping GAS/IO registers
ok marco@


# 1.81 18-Feb-2007 jordan

Added dynamic perfect hash for opcodes
ok marco@


# 1.80 18-Feb-2007 marco

Shave off another K pre compression.
Shuffle SMALL_KERNEL functions around so that we only have a single #ifndef


# 1.79 17-Feb-2007 marco

Add SMALL_KERNEL throught acpi to make it fit on boot media.
ok deraadt


# 1.78 13-Feb-2007 marco

Add smarter _INI code. This helps with some dissapearing devices.

ok kettenis jordan


# 1.77 31-Jan-2007 gwk

Introduce acpi_hasprocfvs thats "ACPI Has Processor Frequency and Voltage
Scalling" a global flag set by acicpu attach to indicate that ACPI is
handling fvs through the _PSS objects. This will fix disappearing
EST/powernow when booting kernels with acpi enabled, as acpicpu isnt
compiled into GENERIC yet. Also in cases where acpicpu is built in and
legacy EST/powernow work but acpi is missing the _PSS object, the legacy
routines will be used.

tested by dim@, and Nick Nauwelaerts <nick AT nauwelaerts.net>
ok marco@, dim@


# 1.76 22-Jan-2007 mk

Actually look for docking stations by looking for _DCK nodes.

ok marco


# 1.75 02-Jan-2007 marco

On some systems EC must be attached as the first device. Move EC from
generic _HID devices to its own routine to force first attach. Bug found
and debugged by joris@

Code mostly written by joris@


# 1.74 26-Dec-2006 marco

Add polling to devices that require it such as acpibat and acpitz.
Use the same tsleep/wakeup mechanism as the interrupt code to keep it non-
concurrent.


# 1.73 21-Dec-2006 deraadt

even more knf love


# 1.72 21-Dec-2006 marco

Remove some leftover acpiec gpe/interrupt code.


# 1.71 20-Dec-2006 deraadt

oops, name already used


# 1.70 20-Dec-2006 deraadt

rename __acpi_enable_gpe(); ok marco


# 1.69 20-Dec-2006 deraadt

acpi_unmap_pmregs() is not used, ok marco


# 1.68 19-Dec-2006 deraadt

simplify some low-level methods substantially (shrinking code); ok marco jordan


# 1.67 06-Dec-2006 mk

malloc(..., M_NOWAIT) with no NULL check is bad. If allocation fails,
complain, clean up, and bail out of attach.

input and ok gwk


# 1.66 28-Nov-2006 jordan

Fixed clearing arg for acpi_init_pic
ok kettenis@


# 1.65 27-Nov-2006 kettenis

Don't spam dmesg.


# 1.64 27-Nov-2006 jordan

Fixes for acpiprt routing
Post-parse fixup for namerefs
ok kettenis@


# 1.63 25-Nov-2006 marco

Don't handle acpiev events inside the gpe handler since this causes deadlocks.

ok jordan@


# 1.62 15-Nov-2006 kettenis

Hook up acpi apic interrupt routing support to generic acpi code.

ok jordan@


# 1.61 02-Nov-2006 marco

Kill acpiec attach function and add its functionality to the generic _HID
device attach

prodded and ok jordan


# 1.60 02-Nov-2006 marco

Fix comments to reflect reality.


# 1.59 19-Oct-2006 marco

Replace ACPI ioctls with APM ioctls. This makes ACPI talk the same protocol
as APM therefore userspace can now retrieve power information in the same
fashion as all other apm arches (zaurus, macppc, i386 etc).

To use force the use of acpi device like so: apmd -f /dev/acpi

ok jordan@


# 1.58 19-Oct-2006 jordan

Fixed GPE debug levels
Fixed ref counter on temporary bufferfields
DSDT parsing fully working on IBM Thinkpad
ok marco@


# 1.57 15-Oct-2006 jordan

Removed old GPE handler code remnants
ok marco@


# 1.56 12-Oct-2006 jordan

Massive ACPI changes
Fixed ACPI leaks
new ACPI API
ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.55 30-Jun-2006 gwk

KNF, No functional change,
"go at it" marco@


# 1.54 30-Jun-2006 jordan

Support for calling Embedded Controller via new GPE interface
ok marco@


# 1.53 30-Jun-2006 jordan

Added new code for GPE handling
Also works with ACPIEC; need to fixup acpiec.c to remove original interrupt handler
ok marco@


# 1.52 30-Jun-2006 jordan

Added offset argument for acpi read/write pmreg routines (new GPE code)
ok marco@


# 1.51 31-May-2006 todd

do not call acpi_powerdown() unless acpi has successfully initialized
ok gwk@ canacar@


# 1.50 29-May-2006 canacar

Add embedded controller support.
ok marco@ deraadt@ testing & ok gwk@


# 1.49 19-May-2006 canacar

Minimal thermal zone driver. Monitors thermal zone temperature,
shuts down the system if the 'critical temperature' is reached.
ok marco@


# 1.48 11-Apr-2006 marco

KNF.


# 1.47 11-Apr-2006 gwk

Add acpi_resume(struct acpi_softc *sc), which calls some AML methods,
detected at attach time, currently unused.

ok marco@


# 1.46 11-Apr-2006 gwk

Discover some ACPI sleep related methods, and call them at the
appropriate time in acpi_enter_sleep_state().

ok marco@


Revision tags: OPENBSD_3_9_BASE
# 1.45 26-Feb-2006 marco

Add acpicpu as a device.

ok jordan@


# 1.44 23-Feb-2006 marco

remove dead function.

ok jordan@


# 1.43 22-Feb-2006 marco

Print out all tables it found during autoconf. We'll need this data in
the future.


# 1.42 22-Feb-2006 marco

Make dmesg less verbose.

ok jordan.


# 1.41 22-Feb-2006 jordan

Added parsing of SSDT tables
ok marco@


# 1.40 22-Feb-2006 jordan

Added new aml_register to notify by device id
Moved powerdown code to button device
ok marco@


# 1.39 21-Feb-2006 marco

Readjust debug levels that crept in too low over the last few days.
KNF


# 1.38 21-Feb-2006 marco

Rig the power button so that the machine shuts down correctly.


# 1.37 20-Feb-2006 marco

Add acpibtn device. Remains disabled for now.


# 1.36 19-Feb-2006 jordan

Added code for aml_notify GPE notification callback
ok marco@


# 1.35 19-Feb-2006 grange

Fix acpi_enter_sleep_state() by replacing current ugly hack
with mostly correct sleeping state transition code.

ok brad@ marco@


# 1.34 19-Feb-2006 marco

jordan:
Enable interrupts.
Added peripheral code to make this work.

marco:
Replace softintr and other types of interrupts with a handler with context.
Fixed interrupt magic for GPEs.
Some KNF

This is work done by jordan and I but has to be a single commit.


# 1.33 17-Feb-2006 marco

Remove earlier introduced delays. We really only need them during autconf.

ok jordan@


# 1.32 17-Feb-2006 marco

Add a generic delay function as per the ACPI spec.

from jordan@


# 1.31 17-Feb-2006 marco

Use tsleep instead of delay when !cold.


# 1.30 17-Feb-2006 marco

Argh, we need delays between reads/writes. The backend reads (i2c most likely)
are so slow that values come back as 0 sometimes. We have to do this
despite the spec saying we shouldnt have to. Guess having a faster
parser than others can hurt sometimes.

Idea from jordan.


# 1.29 17-Feb-2006 jordan

Fixed code for handling _INI
Rearranged debug statements
ok marco@


# 1.28 16-Feb-2006 jordan

Added stub for calling _INI
Fixed aml_dowhile for NULL return
ok marco@


# 1.27 16-Feb-2006 jordan

Fixed parser/evaluator to retrieve battery info+state
Cleanup of parser
Dell laptops return battery status
ok marco@


# 1.26 06-Feb-2006 brad

fix a typo so this will compile on amd64.


# 1.25 04-Feb-2006 marco

When ACPI_ENABLED wasn't set but the devices were it had an unknown variable.

Pointed out by ian@.


# 1.24 03-Feb-2006 jordan

Rewrote aml parser yet again...
Fixed bus_space_map for i386/amd64
Added _BST and _BIF to acpibat to get status/info
ok marco@


# 1.23 20-Jan-2006 jordan

More evaluator changes
Removed old evaluator code
Fixed _HID value
ok marco@


# 1.22 19-Jan-2006 jordan

Added support for read/write of PCI Config space
Help jason@
ok marco@


# 1.21 18-Jan-2006 jordan

Added new evaluation routines
Added helper methods for buffer/field evaluation
Fixed broken indentation on dsdt.c

ok marco@


# 1.20 17-Jan-2006 jordan

Major overhaul of the aml parser. Evaluater temporarily disabled.

ok marco@


# 1.19 06-Jan-2006 grange

Check for ACPI control availability only if ACPI_ENABLE defined.


# 1.18 06-Jan-2006 grange

Informative error messages on attach, prettier dmesg.


# 1.17 06-Jan-2006 grange

printf -> dnprintf conversion for debug messages.


# 1.16 05-Jan-2006 grange

Find SLP_TYPx values and store them for future use in
acpi_enter_sleep_state().


# 1.15 05-Jan-2006 grange

Random KNF and cleanup


# 1.14 05-Jan-2006 grange

Provide symbolic names for the acpi sleeping states and use it.


# 1.13 28-Dec-2005 marco

Moved DEVNAME to a acpidev.h

ok jordan@


# 1.12 18-Dec-2005 sturm

add back comment about wrong code

ok marco


# 1.11 16-Dec-2005 marco

Remove test for kbd_reset. Prompted deraadt@, ok jordan@


# 1.10 16-Dec-2005 jordan

Added hooks for powerdown/power button press


# 1.9 16-Dec-2005 jordan

Cleaned up common power management register i/o
Added enter sleep state ioctl
Hooked power button interrupt
ok marco@


# 1.8 16-Dec-2005 marco

Remove autoconf chatter


# 1.7 16-Dec-2005 jordan

Moved DSDT AML parsing into parent ACPI driver
ok marco@


# 1.6 14-Dec-2005 marco

Add AC device glue.


# 1.5 14-Dec-2005 marco

Adjust debug printing levels to something more sane.


# 1.4 13-Dec-2005 marco

Add glue to attach devices to apci.


# 1.3 07-Dec-2005 marco

KNF


Revision tags: OPENBSD_3_8_BASE
# 1.2 10-Jul-2005 grange

Spacing cleanup.


# 1.1 02-Jun-2005 tholo

Start on a basic ACPI framework -- does not do much more than read out the
ACPI tables into kernel memory and attach ACPI and HPET timers currently.

In order to test this code, enabling the devices in GENERIC as well as
the ACPI_ENABLE option is needed. This code does not do any thermal
control yet, so this should be done with care depending on the platform.

In the tree so more people can contribute to making this more fully
featured.

Ok niklas@ grange@ tedu@


# 1.395 27-Dec-2020 deraadt

acpi_map_address() cannot be wrapped by SMALL_KERNEL anymore, as
it is used by acpihpet.c


# 1.394 25-Dec-2020 visa

Refactor klist insertion and removal

Rename klist_{insert,remove}() to klist_{insert,remove}_locked().
These functions assume that the caller has locked the klist. The current
state of locking remains intact because the kernel lock is still used
with all klists.

Add new functions klist_insert() and klist_remove() that lock the klist
internally. This allows some code simplification.

OK mpi@


# 1.393 17-Dec-2020 kettenis

Fix some issues with referencing named ACPI nodes from Packages.
These references need to be resolved at runtime rather than when
they're parsed such that they pick up the right values for those nodes
which can be changed when for example _INI methods run.

The current approach is to replace these reference with a string that
names the node in question. The problem with that is that packages
can also contain normal strings. Which means that we need special
code that depends on the context where the package is used.

This diff takes a different approach by retaining a reference when
parsing. Code that uses a package will need to resolve this reference
but there is no ambiguiety anymore.

ok patrick@


# 1.392 05-Dec-2020 kettenis

Add support for the _EVT method to the GPIO support code.

ok patrick@


Revision tags: OPENBSD_6_8_BASE
# 1.391 27-Aug-2020 jmatthew

Reorder the acpi attach process so we look at Processor() CPU nodes
after Device() ones, since we should prefer the newer node type.
If we see any Device() nodes, don't attach acpicpu(4) to any Processor()
nodes if they're also present. This also makes acpitz(4) and
acpipwrres(4) devices attach slightly later.

ok kettenis@, also tested by jmc@


# 1.390 16-Aug-2020 gnezdo

Remove sc_maxgpe from acpi_softc which was never read from

Remove a dead conditional from acpi_init_gpes while there.

ok jcs@


# 1.389 21-Jul-2020 deraadt

acpi can use IPL_BIO (a low interrupt) since it only enqueues operations for
later processing. The use of a high interrupt will predate suspend/resume
efforts, we had to redesign acpi to be non-reentrant obviously
discussed with kettenis, in snaps for more than a week


# 1.388 24-Jun-2020 cheloha

kernel: use gettime(9)/getuptime(9) in lieu of time_second(9)/time_uptime(9)

time_second(9) and time_uptime(9) are widely used in the kernel to
quickly get the system UTC or system uptime as a time_t. However,
time_t is 64-bit everywhere, so it is not generally safe to use them
on 32-bit platforms: you have a split-read problem if your hardware
cannot perform atomic 64-bit reads.

This patch replaces time_second(9) with gettime(9), a safer successor
interface, throughout the kernel. Similarly, time_uptime(9) is replaced
with getuptime(9).

There is a performance cost on 32-bit platforms in exchange for
eliminating the split-read problem: instead of two register reads you
now have a lockless read loop to pull the values from the timehands.
This is really not *too* bad in the grand scheme of things, but
compared to what we were doing before it is several times slower.

There is no performance cost on 64-bit (__LP64__) platforms.

With input from visa@, dlg@, and tedu@.

Several bugs squashed by visa@.

ok kettenis@


# 1.387 10-Jun-2020 jca

Give userland a consistent battery and A/C state after resume

Previously we refreshed the state of acpiac(4) and acpibat(4)/acpisbs(4)
by queueing fake ACPI events to be processed by the acpi thread.
acpibat(4) using ACPIDEV_POLL meant that its status could be refreshed
first, resulting in an APM_POWER_CHANGE event sent to userland before
the state of acpiac(4) was up to date. Because of this, apmd(8) could
see A/C unplugged when it was in fact plugged, possibly leading to
a wrong decision.

Instead of using fake ACPI events, refresh the status of acpiac(4) and
acpibat(4)/acpisbs(4) right away at DVACT_WAKEUP time. To reuse the
existing refresh code of those drivers, make their ACPI *_notify()
handlers responsible for sending APM_POWER_CHANGE events instead. Also
queue an APM_POWER_CHANGE event right after resume to retain existing
behavior.

This fixes the consistency of data shown to userland, and makes those
drivers less special.

Initially discussed last year (!) with mpi@ at Miod's place.
Feedback and ok jcs@, deraadt@ agrees with the use of DVACT_WAKEUP here.


# 1.386 29-May-2020 deraadt

dev/rndvar.h no longer has statistical interfaces (removed during various
conversion steps). it only contains kernel prototypes for 4 interfaces,
all of which legitimately belong in sys/systm.h, which are already included
by all enqueue_randomness() users.


# 1.385 14-May-2020 kettenis

Bring back the acpi(4) changes. This time around only use the ACPI _CRS
information on ACPI 5.0 and later.

ok krw@, patrick@


# 1.384 11-May-2020 jca

More accurate remaining power reporting with disparate batteries

Overall remaining power was computed as the average of the remaining
power of each battery, in percents. This doesn't work with batteries of
different capacity, so use the total capacity and remaining power
instead.

Reported by lidstah, successful tests by lidstah and benno@, ok kettenis@


# 1.383 08-May-2020 kettenis

Move parsing of _CRS into acpi(4) and pass the parsed address and interrupt
information in struct acpi_attach_args.

ok mpi@


Revision tags: OPENBSD_6_7_BASE
# 1.382 14-Apr-2020 kettenis

Implement acpi_getprop(), the ACPI equivalent of OF_getprop().

ok patrick@


# 1.381 12-Apr-2020 kettenis

Add support for the _CCA method. This method indicates whether DMA is
cache-coherent or not. To implement this, acpi(4) gets two bus_dma tags
and passes the appropriate one when attaching devices based on _CCA.
On i386/amd64, where for all practical purpose DMA is always cache-coherent,
the two tags are the same. But on arm64 they are distinct.

ok patrick@


# 1.380 07-Apr-2020 visa

Abstract the head of knote lists. This allows extending the lists,
for example, with locking assertions.

OK mpi@, anton@


# 1.379 03-Apr-2020 mpi

Fix incoherencies of errors returned by various *kqfilter routines.

EOPNOTSUPP is returned when there's no "filterops" for a given fd.
EINVAL when the requested filter isn't supported by the "filterops".
ENXIO means the underlying device is no longer valid/present.

ok visa@


# 1.378 20-Feb-2020 visa

Replace field f_isfd with field f_flags in struct filterops to allow
adding more filter properties without cluttering the struct.

OK mpi@, anton@


# 1.377 31-Dec-2019 visa

Use C99 designated initializers with struct filterops. In addition,
make the structs const so that the data are put in .rodata.

OK mpi@, deraadt@, anton@, bluhm@


# 1.376 31-Dec-2019 mpi

Convert infinite sleeps to {m,t}sleep_nsec(9).

ok kn@


# 1.375 31-Dec-2019 jsg

include sys/sensors.h for sensor use
don't depend on indirect inclusion from acpidev.h


Revision tags: OPENBSD_6_6_BASE
# 1.374 07-Sep-2019 kettenis

Revert acpipci(4) changes. Too many systems are broken. We'll try again
after 6.6 as been released. The acpireg.h change stays behind.


# 1.373 28-Aug-2019 kettenis

Use ACPI information to attach PCI busses like we do on arm64. There are a
few additional quirks though, and attaching the PCI busses is delayed to
replicate the existing code more closely. That may be changed in the
future. Also tweak how we handle MSI support and respect to ACPI flag
that says we shouldn't attempt to use MSIs.

Some fallout is expected.

ok patrick@


# 1.372 27-Aug-2019 deraadt

On amd64 the bootup and resume paths for SP/BP/SP were not performing
msr/pat, clock-startup, firmware upgrade, fpu initialization, and cpu
soft-state initialization in the same order. That kind of chaos is
crazy and fragile. Improve the acpi resume call sequence and unify order
of the operations.


# 1.371 02-Jul-2019 jcs

acpi_set_gpehandler: turn edge arg into flags, add support for
passing GPE_DIRECT which directly executes the supplied callback and
clears a level interrupt rather than going through acpi_taskq which
can be slow when other driver callbacks are in the queue.

This should not be used for GPE handlers that need to do anything
ACPI-related.

ok kettenis


# 1.370 10-Jun-2019 kettenis

Print proper ACPI version number.

ok mlarkin@


# 1.369 08-Jun-2019 kettenis

When establishing the mapping between ACPI device nodes and PCI devices,
skip PCI host bridges and devices that aren't there.

ok patrick@


# 1.368 07-Jun-2019 kettenis

Introduce acpi_getsta() and use it to eliminate some duplicated code.

ok patrick@


# 1.367 12-May-2019 kettenis

Make sure the extended PM register descriptions actually exist before
using them. Fixes machines with ancient ACPI 1.0 (such as recent
hypervisors).

ok deraadt@


# 1.366 12-May-2019 kettenis

Add support for mapping ACPI PM registers using the "extended" gasio
representation from the FADT. Mostly fixes the Lanner NCA-1510.

ok lteo@, mlarkin@, deraadt@


# 1.365 11-May-2019 lteo

Fix incorrect error message.

ok kettenis@


# 1.364 08-May-2019 tedu

free size. the allocation is the header plus the length.


Revision tags: OPENBSD_6_5_BASE
# 1.363 04-Apr-2019 kettenis

Avoid potential null-pointer dereference. Found by Kent R. Spillner.

ok deraadt@, jsg@


# 1.362 20-Jan-2019 jsg

fix SMALL_KERNEL build after changes in rev 1.361
reported by naddy@ ok deraadt@


# 1.361 19-Jan-2019 tedu

Add a pwraction sysctl that controls what the power button does on acpi.
By default, nothing changes -- shutdown is initiated. But allows turning
power button into a sleep button if desired.
(grudging) ok from a few parties


# 1.360 26-Oct-2018 kettenis

Add acpipci(4) on amd64. For now this only calls the PCI-specific _OSC
method to let the ACPI implementation know what features we support.


Revision tags: OPENBSD_6_4_BASE
# 1.359 30-Aug-2018 kettenis

Move softbutton check before the check that skips attaching certain acpi
devices. Fixes regression caused by matching on _CID in addition to
matching on _HID.

ok matthieu@, mlarkin@


# 1.358 25-Aug-2018 kettenis

Match ACPI devices based on _CID if no match for _HID is found.

ok mpi@


# 1.357 19-Aug-2018 kettenis

Add support for multiple PCI segments. Only really implemented for arm64
for now as amd64/i386 firmware still caters for legacy OSes that only
support a single PCI segment.

ok patrick@


# 1.356 03-Aug-2018 kettenis

Let ahci(4) match on _CLS instead of _HID when attaching at acpi(4). Avoids
having to add many more _HID entries to the match table.

ok deraadt@, mlarkin@


# 1.355 10-Jul-2018 kettenis

Make legacy interrupts work in acpipci(4).

ok patrick@


# 1.354 05-Jul-2018 kettenis

Add acpipci(4), a driver that supports generic ECAM-compatible PCI host
bridges based on information provided by ACPI.

ok mlarkin@


# 1.353 04-Jul-2018 kettenis

Properly pass around the PCI "chipset tag" in acpi(4) and refactor
acpimcfg(4) to call an MD initialization functions that sets up a tag for
PCI ECAM.

ok guenther@, mlarkin@, krw@


# 1.352 01-Jul-2018 kettenis

Move acpi(4) attach glue into acpi_machdep.c.

ok guenther@, deraadt@


# 1.351 01-Jul-2018 kettenis

Add support for _DSD Device Properties.

ok mlarkin@


# 1.350 30-Jun-2018 kettenis

Add intr_enable() function, intended for MI use to amd64 and i386 and use
this in the acpi(4) suspend/resume code paths.

ok deraadt@


# 1.349 29-Jun-2018 kettenis

Use <stdint.h> types.

ok mpi@, jung@, krw@, deraadt@


# 1.348 29-Jun-2018 mlarkin

ACPI: Allow (with warning) GPE handler reassignment, instead of returning
a failure code.

ok kettenis


# 1.347 26-Jun-2018 mlarkin

knf


# 1.346 26-Jun-2018 kettenis

Let acpi(4) pass down the bus dma tag.

ok deraadt@


# 1.345 25-Jun-2018 kettenis

Reorganize acpi(4) code a bit in preparation for upcoming arm64 support.

ok deraadt@


# 1.344 20-May-2018 kettenis

Wake up the acpi thread after scheduling a gpio event.

ok mpi@


# 1.343 17-May-2018 kettenis

Sprinkle some #ifndef SMALL_KERNEL such that RAMDISK kernels cuild again.

pointed out by stsp@


# 1.342 17-May-2018 kettenis

Implement GenericSerialBus OpRegion support.

ok mlarkin@


# 1.341 27-Mar-2018 kettenis

Add acpicmos(4), a driver that implements SystemCMOS OperationRegion
access support. This fixes machines where the AML doesn't check whether
support for this OperationRegion type has been registered by the OS.

ok mlarkin@


Revision tags: OPENBSD_6_3_BASE
# 1.340 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.339 10-Feb-2018 deraadt

Syncronize filesystems to disk when suspending. Each mountpoint's vnodes
are pushed to disk. Dangling vnodes (unlinked files still in use) and
vnodes undergoing change by long-running syscalls are identified -- and
such filesystems are marked dirty on-disk while we are suspended (in case
power is lost, a fsck will be required). Filesystems without dangling or
busy vnodes are marked clean, resulting in faster boots following
"battery died" circumstances.
Tested by numerous developers, thanks for the feedback.


# 1.338 08-Feb-2018 deraadt

After flushing disks, try again to release all memory. We don't need to
save dirty memory to the hibernate space.


# 1.337 08-Feb-2018 deraadt

Toss all releaseable memory, because fragmentation can get in the way
of allocating the hibernate playpen.


# 1.336 08-Feb-2018 deraadt

sr_quiesce() is a new approach for ensuring that softraid drains
output to the disks.

This is part of a larger suspend/resume filesystem-safety diff, which
has been worked on for a couple of months already. Tests by job, krw,
beck, benno, and others. Sometimes even by snapshot users...


# 1.335 29-Nov-2017 kettenis

Add support for GPIO-signaled events.

ok mlarkin@


# 1.334 16-Nov-2017 jcs

Add PCI attachment for dwiic(4) needed by Intel 100 Series machines

ok kettenis


# 1.333 14-Nov-2017 kettenis

Add a few more PNP IDs to the skip lists.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.332 17-Aug-2017 stsp

Make dwiic(4) attach its ACPI parent dependencies.
This change makes the Asus E200HA keyboard work.

Original analysis and patch by Cesare Gargano
ok kettenis@


# 1.331 09-Aug-2017 dcoppa

Add "PNP0303" (8042 PS/2 Controller) to acpi_isa_hids[]

ok kettenis@


# 1.330 22-Jul-2017 jcs

acpibat: look for _BIX first before falling back on _BIF

ACPI 4.0 deprecated _BIF for battery status so some newer machines
are exporting _BIX which extends _BIF's package with a few extra
fields. When using _BIX, export the new discharge cycle count as a
sensor.

feedback from kettenis and anton
ok deraadt


# 1.329 20-Jul-2017 kettenis

Start a list of isa(4) device names such that we can avoid printing them
as "not configured".

ok mikeb@, millert@, deraadt@


# 1.328 08-Apr-2017 deraadt

Sensors are run as callbacks inside tasks. During suspend a sensor
could be running inside a driver that will be force-detached, or due
to tsleep end up disrupting the softstate/hardstate contract. At
suspend time, quisce all these callbacks by waiting for completion.
This issue has never been observed for real, but may be implicated in
suspend/resume failures.
ok kettenis guenther mlarkin


# 1.327 08-Apr-2017 deraadt

Two prototypes for acpi_maptable; merge to one place


Revision tags: OPENBSD_6_1_BASE
# 1.326 27-Mar-2017 deraadt

Now that hibernate_alloc() only has clean success/failure, don't
need to call hibernate_free() to clean up a partial mess.
ok mlarkin kettenis


# 1.325 13-Mar-2017 deraadt

allow ramdisks to build; ok jcs


# 1.324 12-Mar-2017 jcs

try to attach acpisbs first and if successful, don't attach acpibat


# 1.323 02-Mar-2017 natano

Add a new sysctl machdep.lidaction. The sysctl works as follows:

machdep.lidaction=0 # do nothing
machdep.lidaction=1 # suspend
machdep.lidaction=2 # hibernate

lidsuspend is just an alias for lidaction, so if you change one, the
other one will have the same value. The plan is to remove
machdep.lidsuspend eventually when people have upgraded their
/ets/sysctl.conf.

discussed with deraadt, who came up with the new MIB name
no objections mlarkin
ok stsp halex jcs


# 1.322 28-Feb-2017 natano

Untangle abstract sleep modes from ACPI sleep states. This paves the way
for further work in the area.

idea and ok deraadt
ok mlarkin


# 1.321 22-Feb-2017 jcs

add acpisbs, an acpi smart battery subsystem driver reading data
over smbus

currently disabled because it conflicts with acpibat

ok deraadt, kettenis


# 1.320 14-Jan-2017 kettenis

Revert aml_rdpciaddr changes; breaks several machines that were working
before.


# 1.319 11-Jan-2017 jsg

Increase the size of the buffers used for _HID and _CID strings to be
able to fit "Hyper_V_Gen_Counter_V1".

ok mlarkin@ deraadt@


# 1.318 08-Jan-2017 kettenis

Make aml_rdpciaddr() work for for devices behind bridges. The code now
uses the mapping between AML nodes and PCI devices that we establish.
Because _INIT methods may end up calling aml_rdpciaddr(), make sure we
create that mapping early. Also handle devices that aren't actually present.
These devices are now included in the mapping and reads will return an
all-ones patterm whereas writes are a no-op..

ok mlarkin@


# 1.317 25-Oct-2016 pirofti

Add the resource index to aml_parse_resource() callback function.

This is needed by an upcoming acpiec commit that handles machines breaking
the current ACPI specifications.

Change suggested by and ok kettenis@, guenther@


# 1.316 18-Sep-2016 guenther

If the APIC table (MADT) isn't present then ci_acpi_proc_id won't be set
when acpicpu tries to attach, leading to a NULL dereference. The ACPI
processor object on the Dells where this was reported is bogus and useless
anyway so apply the same test when deciding whether to attach to processor
objects as acpicpu would when matching them to cpus.

Problem reported by Eivind Eide (xenofil (at) gmail.com) and
Johan Huldtgren (johan+openbsd-bugs (at) huldtgren.com).
Testing by Eivind Eide.
ok kettenis@ mlarkin@


# 1.315 03-Sep-2016 naddy

Write the system time back to the RTC every 30 minutes.
This fixes the problem that long-running machines which were not
shut down properly would reboot with a badly offset system time.

hints and ok kettenis@


# 1.314 31-Aug-2016 mlarkin

Don't put a device in the wake device list if _STA says it's not there.

Fixes at least one machine that woke up from sleep / poweroff immediately.

ok kettenis@, tested by kettenis and martijn@


# 1.313 28-Jul-2016 kettenis

Store the acpi processor ID/UID in struct cpu_info, and use it to attach
acpicpu(4) drivers to the right cpu(4).

ok mlarkin@, guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.312 10-Jun-2016 kettenis

The power management timer has been made optional in ACPI 5.0A, so don't
attempt to attach acpitimer(4) if the timer isn't present.

Pointed out by jcs@

ok jcs@


# 1.311 08-May-2016 kettenis

Make sure devices listed by a device's _DEP method are attached before
we attach the device itself. The _DEP methode, introduced in ACPI 5.0,
is a hint that the device in question depends on OpRegion support from
the devices listed by _DEP, so we have to order them the proper way.
To prevent us from attaching those devices again when we encounter them
later walking down the device tree, keep track of our attempts to
attach a device driver to them using aflag in the aml_node structure.

ok guenther@


# 1.310 25-Apr-2016 pirofti

We attach acpihpet(4) and acpiec(4) without using the HID, so do not
report it in the dmesg.

OK deraadt@, mlarkin@.


# 1.309 02-Apr-2016 kettenis

Reduce dmesg spam by knocking out some acpi hardware IDs that don't need a
driver.

ok mpi@


# 1.308 29-Mar-2016 kettenis

Check if a device is present (using _STA) in acpi_foundhid. This prevents
us from reporting devices as "not configured" that aren't actually present,
and allows us to remove duplicated code from several drivers.

ok deraadt@, ok jsg@


# 1.307 29-Mar-2016 kettenis

Implement a cleaner way to prevent acpivout(4) from attaching if we have
a platform-specific method of controlling the backlight.

ok millert@, mlarkin@, jung@


# 1.306 28-Mar-2016 kettenis

Adding each and every new acpi device driver to acpi_foundhid() is
gettinga bit of the burden. And it really isn't how our autoconf
framework is supposed to work for busses that can be enumerated. All
the drivers already check for a matching _HID in their attach
function. So we can just drop the checks in acpi_foundhid() and let
the drivers sort themselves out.

Print a "not configured" line for devices for which we don't attach adriver.
This may be a bit much as there are devices for which we will not have a
driver. This will be removed if it causes too much dmesg spam.

ok deraadt@, mlarkin@


Revision tags: OPENBSD_5_9_BASE
# 1.305 17-Jan-2016 jsg

Take acpi_foundhid() outside of #ifndef SMALL_KERNEL so the ramdisk
kernel can find and attach dwiic(4) and sdhc(4).


# 1.304 17-Jan-2016 jsg

remove an unused global variable


# 1.303 14-Jan-2016 kettenis

Attach dwiic(4) on the Intel Bay Trail i2c controllers as well.


# 1.302 12-Jan-2016 kettenis

Improve emulation of PM registers on Hardware-redcued ACPI by emulating
SPL_TYPx, SLP_EN and WAK_STS using the new SLEEP_STATUS_REG and
SLEEP_CONTROL_REG registers. Makes the transition into S5 (powerdown) work
on the ASUS X205TA.

ok deraadt@, mlarkin@


# 1.301 12-Jan-2016 jcs

Add dwiic, a driver for the Synopsys DesignWare i2c controller found
on the Samsung ATIV Book 9 laptop. This initial version only
supports ACPI config/attachment.

Add ihidev, a HID-over-i2c driver largely based on uhidev. dwiic
handles attaching ihidev devices found in ACPI.

Add ims, a HID-over-i2c mouse/trackpad driver to get basic cursor
and button functionality from HID-compliant i2c trackpads.

ok kettenis deraadt


# 1.300 11-Jan-2016 kettenis

Make sdhc(4) attach to hardware IDs "80860F14" and "PNP0FFF".


# 1.299 09-Jan-2016 kettenis

Initial support for Hardware-Reduced ACPI. This skips the legacy mode checks
by emulating the PM_CNT register such that SCI_EN is always enabled. It also
skip attaching acpitimer(4) since the relevant registers are no longer there.

ok deraadt@, mlarkin@


# 1.298 06-Jan-2016 kettenis

Follow the spec and check the SCI_EN bit to see if ACPI has already
been enabled and don't bother enabling it again if it is. This should fix
systems that don't implement legacy mode and therefore set ACPI_ENABLE and
ACPI_DISABLE to zero.

ok mlarkin@, yasuoka@


# 1.297 23-Nov-2015 reyk

Back out the CRITICAL change, turn it back from 10% to 25%. Some
people with old/broken batteries didn't like this change, and the
underlying heuristics are wrong.


# 1.296 22-Nov-2015 reyk

The "CRITICAL" APM battery level, that is exported/faked by the ACPI
stack, is currently set to 25%. With 25% battery left, a typical
modern laptop will have at least one hour runtime left. It is an
arbitrary value, and other APM power layers in the kernel set CRITICAL
to something like 5% or 10%, so set it to 10% for now.

OK mlarkin@


# 1.295 28-Sep-2015 deraadt

In low-level suspend routines, set cold=2. In tsleep(), use this to
spit out a ddb trace to console. This should allow us to find suspend
or resume routines which break the rules. It depends on the console
output function being non-sleeping.... but that's another codepath which
should try to be safe when cold is set.
ok kettenis


# 1.294 06-Sep-2015 deraadt

clarify malloc arguments (base + length...)


# 1.293 28-Aug-2015 deraadt

fairly simple sizes for free(); ok tedu


# 1.292 20-Aug-2015 kettenis

Respect the access size when reading or writing to pci config space.
This version also makes sure writes are properly aligned, preventing
the panics seen with the previous incarnation of this change.

Fixes battery status passthrough in vmware, brightness keys on my x220
and who knows what else.

ok mlarkin@, deraadt@


Revision tags: OPENBSD_5_8_BASE
# 1.291 04-Aug-2015 deraadt

If we are setting ACPI_PM1_BM_RLD do it only once, and remember to
repeat this action at resume time.
ok mlarkin guenther


# 1.290 29-Jul-2015 deraadt

Something subtle in previous revision is locking up suspend or booting
on some laptops. Mailing lists threads mention x220 and Asus machines.
Reverting for now.


# 1.289 21-Jul-2015 kettenis

iRespect the access size when reading or writing to pci config space.
Fixes battery status passthrough in vmware and who knows what else.

Tested by many.

ok mlarkin@, deraadt@


# 1.288 17-Jul-2015 mlarkin

fix comments


# 1.287 26-Mar-2015 jsg

Use the 0x10 extended capability to determine if the trackstick
buttons are wired to the trackpad and need to be re-routed to the
trackstick. Without this change the buttons on 2015 thinkpads
get picked up as extended buttons that show up as scroll up/down.

Remove the X1 Carbon 2015 (LEN0048) and X250 (LEN0046) from the
top button area/soft buttons quirks list. Also avoid using the
quirk list entirely if the capability bit is set.

Based on work from jcs@ with feedback from mpi@
ok mpi@ jcs@


# 1.286 20-Mar-2015 kettenis

The ACPI standard says that a PCI function number of FFFF can be used to refer
to all functions on a device. This makes no sense in the context of accessing
PCI config space. Nevertheless stsp@ has a machine with AML that tries to
do config space reads from a device where _ADR encodes function number FFFF.
This is almost certainly buggy AML, but since Linux (and presumably Windows)
boot just fine on this machine we need to work around this. Linux effectively
treats function FFFF as function 7, which isn't present on this machine.
Instead we simulate a read from a non-existent device by returning a bit
pattern of all ones. Writes to function FFFF will still panic.

ok mlarkin@


# 1.285 16-Mar-2015 deraadt

Reduce use of SMALL_KERNEL in acpi, bringing acpiec driver and more gpe
handling into RAMDISK. This is now possible because the install media
has ample room. The goal is to reduce special cases where we may be
skipping (unknown) important operations...
ok mlarkin kettenis


# 1.284 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.283 07-Feb-2015 deraadt

New framework that allows hibernate to pass in entropy from it's fresh
boot.
ok mlarkin


# 1.282 06-Feb-2015 mlarkin

Fix a hibernate crash on some machines due to unmapping a page that
may not have been mapped previously (in the failure to hibernate case).

Also ensure that the lowmem ptp is mapped in all cases (not just MP).

ok kettenis


# 1.281 17-Jan-2015 deraadt

resume_randomness() before spinning up other cpus...


# 1.280 15-Jan-2015 jsg

Define a new wscons mouse type for Synaptics clickpad devices that lack
physical buttons. This will be used if the acpi pnpid for the mouse
matches a list provided by Synaptics found in Linux.

Combined with the xenocara changes this will remove the need for an xorg
config file for the x240/t440/t540 generation of thinkpads.

ok matthieu@ shadchin@ kettenis@


# 1.279 11-Jan-2015 kettenis

Tolerate checksum errors in ACPI tables (but complain about them). Apparently
there are machines out there with broken BIOSen with such checksum errors in
thier RSDT tables. Windows runs fine on these machines, so it probably does
not check. Linux ignores checksum errors as well (and prints a warning).

ok deraadt@, krw@


# 1.278 18-Dec-2014 deraadt

suspend and resume the RNG (at what looks like the right spots)
ok reyk djm


# 1.277 09-Dec-2014 doug

Replace some malloc(n*size,...) calls with mallocarray().

ok tedu@ deraadt@


# 1.276 06-Dec-2014 mpi

If "machdep.lidsuspend" is not set, do not go back to sleep if the lid
is still closed when resuming. This allows docked laptop to be resumed
without opening them.

ok mlarkin@


# 1.275 23-Nov-2014 mlarkin

If a machine has no _LIDs, then none can be open. This diff fixes a problem
where machines without any _LIDs were going back to sleep after hibernate
due to the recent suspend-if-lid-closed-on-resume diff. Noticed on qemu,
which doesn't have any _LIDs.

discussed with deraadt


# 1.274 18-Nov-2014 krw

Nuke yet more obvious #include duplications.

ok miod@


# 1.273 08-Nov-2014 mlarkin

If resuming from sleep (zzz/ZZZ) and the lid is still closed, go back to
sleep. This prevents accidental lid flex or slight opening in a backpack
from waking the machine up and leaving it resumed (powered on).

ok deraadt@


# 1.272 26-Sep-2014 kettenis

Rework piglet and pig allocation. Currently the piglet gets allocated
deep down in the suspend path, where it is really hard to recover from
allocation failure. So allocate the piglet early on in the suspend path.
Also change the piglet and piglet allocation functions to use km_alloc(9)
instead of doing pmemrange magic. This removes a bunch of code which, in the
case of the piglet allocation, is broken since it results in a NULL pointer
dereference. Also switch the piglet allocation to not wait. If we can't
allocate 16MB of phys contig memory on a halfway modern machine we're almost
certainly under a lot of memory pressure and we're better off not trying to
hibernate anyway.

ok mlarkin@


# 1.271 25-Sep-2014 kettenis

Bring back rev 1.249. Now that mp_setperf() has been fixed, it should be
safe to restore perflevel when resuming. Should fix the issues people
see with the processor running at full speed heating up the machine while
the apm command reports that the CPUs are running at a low speed.

ok dcoppa@, deraadt@


# 1.270 23-Sep-2014 kettenis

Release the acpi lock around calls to wsdisplay_suspend() and
wsdisplay_resume(). The X server may adjust the display brightness just
before/after the VT switch when suspending/resuming. In fact, it may
already be blocked adjusting the display brightness for some reason
unrelated to the suspend request.

The VT switch request will eventually time out, but the delay is annoying
and the failed VT switch will lead to problems after resume.

Needed for a future update of the xf86-video-intel driver.

ok mlarkin@


# 1.269 19-Sep-2014 kettenis

Use config_suspend_all(9).


# 1.268 08-Sep-2014 kettenis

Clear the PME Status bit on PCI devices in response to Device Wake events.
Fixes the interrupt storm on the 2009 Mac mini with WOL enabled on its
nfe(4) interface.

ok mlarkin@


Revision tags: OPENBSD_5_6_BASE
# 1.267 20-Jul-2014 mlarkin

Support hibernating to softraid crypto volumes.

much help and ok from deraadt@


# 1.266 16-Jul-2014 mlarkin

Reenable hibernate RLE support and flush+zero all memory after unpack.


# 1.265 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.264 12-Jul-2014 kettenis

Add a function to drop all clean pages on the page daemon queues and call
it when we hibernate.

ok mlarkin@, miod@, deraadt@


# 1.263 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.262 11-Jul-2014 mlarkin

Flush the buffercache to 16MB on hibernate and restore its previous max
size (kern.bufcachepercent) on resume, for better hibernate performance.

ok beck@


# 1.261 10-Jul-2014 blambert

remove unneeded header

ok mlarkin@


# 1.260 01-Jul-2014 kettenis

Adding an additonal delay after each access to SystemMemory or SystemIO
during autoconf makes no sense.

ok deraadt@


# 1.259 23-Jun-2014 kettenis

SystemMemory is used to access memory mapped registers on some machines, so we
must use the bus_space(9) API to access it instead of memcpy(9). Also make
sure we properly align access to fields that need more than byte alignment.

ok guenther@


# 1.258 21-May-2014 mlarkin

Fix invalid / missing format specifiers in several dnprintf statements
(used only when ACPI_DEBUG is enabled)

From a diff on tech@ by Fabian Raetz, with a minor change by me, tested
on i386 and amd64 with ACPI_DEBUG enabled and disabled.


# 1.257 25-Apr-2014 mlarkin

Defer the free of the hibernate structures until later, when it is safe to
do so.

Freeing the hibernate structures too early leads to inadvertent calls into
splx when we aren't ready for them yet. Found when trapping splx calls in
the resume path.

discussed with deraadt@ at m2k14


# 1.256 13-Apr-2014 mlarkin

Fix a badly worded debug message that implied the entire machine was going
to sleep, instead of just the acpi thread going to sleep.


# 1.255 31-Mar-2014 mpi

Change the order of operations during a suspend/resume cycle and
call bufq_quiesce() after executing the DVACT_QUIESCE handlers.

This should be safe since no disk nor controller drivers have such
handler but it will allow us to detach sd(4) devices attached to a
USB bus.

Another benefit pointed out by kettenis@ is that drivers that need
to read a firmware from the disk should be able to do it at resume
time in a DVACT_WAKEUP handler.

ok kettenis@, deraadt@


# 1.254 13-Mar-2014 dlg

get rid of the assumption that the head of the alldevs list is the
"mainbus" device. this breaks when mpath is enabled because it
attaches before mainbus and therefore takes the head position.

have autoconf provide device_mainbus() which looks up mainbus_cd,
and use that instead.

discussed with deraadt who just wants mpath stuff to move forward
despite there being many ways to shine this particular turd.


Revision tags: OPENBSD_5_5_BASE
# 1.253 21-Feb-2014 deraadt

Do the lid control further on in the resume sequence, since this is
AML, and will do WAITOK operations. While cold, that is a bad idea.
We seem to be safe with respect to the spec, I do not see any event
debounce issues here.
ok kettenis, tested by many


# 1.252 20-Jan-2014 claudio

Instead of checking sc->sc_revision <= 1 check that the
fadt->hdr_revision <= 1. This is a better check since it checks
the version of the proper descriptor. This allows the alix APU
to reboot since those boards have currently a wrong sc->sc_revision.
Committing this now so that it can be tested easier.
With and OK jsg@ also fine by derradt@


# 1.251 24-Dec-2013 kettenis

If the FADT has its SMI_CMD set to zero, assume we're only ACPI-only hardware
and don't need to disable SMI ownership of the ACPI hardware registers.

ok mlarkin@


# 1.250 23-Dec-2013 kettenis

Revert previous commit. It makes resume hang on some MP systems running
GENERIC.MP.


# 1.249 16-Dec-2013 kettenis

Restore hw.setperf upon resume like we do for apm(4) on i386. Seems we need
to do this fairly late in the resume to avoid hangs.

ok deraadt@, mlarkin@


# 1.248 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


# 1.247 06-Nov-2013 mpi

Add support for Power Resources for Dx states and the necessary hook
for PCI devices. This hook should be called twice, before and after
changing the power state of a PCI device.

Before setting the device to the new state, the ACPI layer will notify
every power resources linked to the device for that state and make sure
they are turned "_ON". After changing the state of the device, it will
decrement the reference of every power resources linked to that device
for the old state and turn them "_OFF" if they are no longer referenced.

This fixes the no-USB after resume problem seen on various ThinkPad,
problem initialy diagnosed with Alexander Polakov.

ok kettenis@, deraadt@


Revision tags: OPENBSD_5_4_BASE
# 1.246 01-Jun-2013 mlarkin

Stop printing ACPI wakeup devices in dmesg after the 16th wakeup device.

Helps with VMware VMs that report hundreds of wakeup devices.

ok reyk@, deraadt@


# 1.245 31-May-2013 bluhm

Fix the build for a kernel without wd and pciide in its config.
OK deraadt@ tedu@


# 1.244 30-May-2013 deraadt

Enforce ca_activate tree-walks over the entire heirarchy for all events,
cleaning up some shutdown-hook related code on the way.
(A few drivers related to sparc are still skipped at kettenis' request)
ok kettenis mlarkin, tested by many others too


# 1.243 18-Apr-2013 deraadt

pin the acpi thread to the BSP, because AML and SMI cannot be trusted. I
was certain we were doing this already, but it turns out no...


Revision tags: OPENBSD_5_3_BASE
# 1.242 24-Dec-2012 guenther

In acpiioctl(), Return though the splx() path if suser() returns an error

ok deraadt@


# 1.241 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


# 1.240 04-Oct-2012 ehrhardt

Use information provided by ACPI to attach secondary PCI host bridges.

ok kettenis@


# 1.239 07-Sep-2012 kettenis

Add a new function called acpi_pci_min_powerstate() that returns the lowest
power state for a PCI device for the power state (currently S3 or S4) we're
transitioning into. This function is probably too simplistic; there is a lot
of (silly) complexity in the ACPI spec.

ok deraadt@


Revision tags: OPENBSD_5_2_BASE
# 1.238 13-Jul-2012 pirofti

Disable acpivideo(4) on ASUS laptops.

There are some models that panic whenever an event is received.
Some cope, some don't. In order to get this right acpiec(4) requires
major rework. Until then let machines boot.

Discussed with deraadt@.


# 1.237 11-Jul-2012 mlarkin

for now, restrict hibernate to root in the acpi ioctl kernel interface.
normal rules for controlling this interface through apmd still apply.

ok deraadt@


# 1.236 09-Jul-2012 deraadt

tiny little oops in untested hibernate block


# 1.235 09-Jul-2012 deraadt

clean up ioctl() case statements


# 1.234 09-Jul-2012 deraadt

acpi_disable_onegpe() is not used; ok pirofti


# 1.233 24-May-2012 kettenis

If _MIN > _MAX consider a WordBus resource to be invalid. Fixes a panic on
the IBM x3550-M4.

ok mikeb@


# 1.232 29-Mar-2012 mlarkin

Missing #ifdef in previous diff


# 1.231 29-Mar-2012 mlarkin

Prevent hibernation attempts on machines with unsupported disk controllers.
The same check is also performed later in the hibernate sequence, but by
then it's too late to cleanly unwind (presently).

ok deraadt


# 1.230 26-Mar-2012 deraadt

simplify conditionals in acpi_sleep_state()
ok mlarkin


# 1.229 26-Mar-2012 deraadt

hook in the hibernate request code; half of this diff is from mlarkin
ok mlarkin


Revision tags: OPENBSD_5_1_BASE
# 1.228 20-Sep-2011 deraadt

Very early in suspend while the clock is still ticking realtime,
save the clock back to the rtc. The zaurus already did this.
ok phessler miod kettenis


Revision tags: OPENBSD_5_0_BASE
# 1.227 02-Jul-2011 nicm

kqueue attach functions should return an errno or 0, not a plain 1. Fix
the obvious cases to return EINVAL and ENXIO.

ok tedu deraadt


# 1.226 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.225 16-Jun-2011 pirofti

Import acpitoshiba driver.

This is originally written by Hiroyuki Aizu, ported and integrated
in our tree by Javier Vazquez <javama4 AT gmail> with tweaks and
suggestions by me.

Import prodded by deraadt@ and the lack of testers on tech@.


# 1.224 27-Apr-2011 jcs

attach acpithinkpad to newer lenovo models like the x120e

ok deraadt@


# 1.223 22-Apr-2011 jordan

Fix uninitialzied variables and formatting strings (-Wxxx errors)


Revision tags: OPENBSD_4_9_BASE
# 1.222 02-Jan-2011 jordan

Re-add code for ACPI taskqueue, eliminates need for state vars in acpi softc
Serialize processing ACPI tasks for sleep/powerdown/GPE event
ok deraadt@


# 1.221 31-Oct-2010 guenther

Revert last commit: it breaks resume on Thinkpads
"then please back it out" deraadt@


# 1.220 26-Oct-2010 jordan

Add task queue for ACPI gpe and notify handlers
This fixes eject on Dell Latitude dock and an issue on the Dell Mini
battery update.
ok deraadt


# 1.219 07-Oct-2010 jordan

Create separate functions for enabling wake and runtime gpes.
Remove old suspend/resume gpewalk function
ok deraadt, mlarkin


# 1.218 05-Oct-2010 jordan

split acpi_enable_onegpe to separate enable/disable functions
ok deraadt


# 1.217 05-Oct-2010 jordan

Added acpi_disable_allgpes function to clear GPE status
ok deraadt


# 1.216 05-Oct-2010 deraadt

clean up acpi_softc use more


# 1.215 05-Oct-2010 deraadt

delete a bit of #if 0 code


# 1.214 05-Oct-2010 deraadt

hide use of our global acpi_softc pointer a bit better


# 1.213 29-Sep-2010 deraadt

grab the acpi thread lock very early in acpi thread startup; not that it
is likely that anyone will race us... but why be careless.
ok kettenis


# 1.212 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.211 29-Aug-2010 jasper

- add a check for FWRITE here too.

ok miod@


# 1.210 27-Aug-2010 kettenis

Make PCI Power Management optional, and only enable it when acpi(4) attaches.

ok deraadt@


Revision tags: OPENBSD_4_8_BASE
# 1.209 08-Aug-2010 kettenis

Close race between the acpi thread and wscons ioctls running in process
context. We might rip this out post-release and replace it with a solution
that serializes things in a more obvious way.

ok deraadt@, miod@ and "those crazy texans who ok anything"


# 1.208 08-Aug-2010 deraadt

Report the acpi sleep states when on a RAMDISK; ok krw


# 1.207 07-Aug-2010 kettenis

Enforce acpi register access rescrictions.

ok mlarkin@, deraadt@, tested by marco@


# 1.206 07-Aug-2010 deraadt

upon resume, notify all the acpiac and acpibat drivers to update
their status. some laptops do not do this automatically.
ok kettenis canacar


# 1.205 06-Aug-2010 marco

Always call _PSW on _LID devices that support it to enable lid open to
resume the machine. Conversely disable it when the machine wakes up.

Tested by several ok deraadt


# 1.204 06-Aug-2010 deraadt

As part of a revamp of the PM1/GPE code, I write this diff to look for
unmanaged EN & STS bits in the PM1 register at interrupt time and report
them. As a side effect this splits the STS acknowledgement into two writes
(for power, and sleep) instead of one. The printf that is added (to spot
unmanaged STS bits) has not yet been triggered as far as we know.

Before the "write to PM1 registers at the right offset" diff went in, this
was not neccessary. But newer thinkpads do not have a working soft power
button without this diff.

We have no idea why.
ok mlarkin kettenis


# 1.203 05-Aug-2010 deraadt

PM1 power button events were generating 'fake events' to acpibtn PWRB,
but if there is no such device these were being tossed. Instead, arrange
for all power button handlers to flag a softc variable which is processed
by the thread; so that one place will know to request a powerdown (psignal
to init)
ok marco mlarkin


# 1.202 05-Aug-2010 deraadt

rename a variable which is annoyingly poorly named


# 1.201 05-Aug-2010 mlarkin

Write to the correct offset for various ACPI pmreg registers. Writing to
the correct register does properly clear the wake status bits and fixed
event bits, which allows some machines to have more than 1 suspend cycle.

bug spotted by deraadt@

tested by deraadt@ and myself on a half-dozen machines and others on
hackers@

ok deraadt@, marco@


# 1.200 05-Aug-2010 deraadt

Fix the power button. The pm1 register is 16 bits in size.
My mistake, a while back.
tested by krw


# 1.199 04-Aug-2010 kettenis

Print supported sleep states.

ok marco@, phessler@, deraadt@


# 1.198 03-Aug-2010 mlarkin

Use the proper flag for re-enabling certain hardware events (power btn,
sleep btn). The (incorrect) flag prevoiously used coincidentally had the
same value, so this shouldn't cause any different behavior than before.

tested on a variety of machines (i386, amd64, sp, mp) - no behavioral
change seen

ok deraadt@


# 1.197 28-Jul-2010 marco

Make all gasio failures look and behave the same.


# 1.196 28-Jul-2010 mlarkin

Delay a little longer on suspend before giving up. Fixes machines that
sometimes don't suspend because the sleep transition takes too long
(symptoms like failing to sleep but the backlight stays on).

ok deraadt@


# 1.195 27-Jul-2010 deraadt

remove XXX; EC can now be attached early when needed; ok jordan


# 1.194 27-Jul-2010 deraadt

more brackets feels good


# 1.193 27-Jul-2010 deraadt

XXX annotate a block in acpi_enter_sleep_state() that is pretty worrying


# 1.192 27-Jul-2010 deraadt

horrific indentation that nearly made me puke


# 1.191 27-Jul-2010 mlarkin

Set the sleeping indicator light on machines that support it, in case SMI
doesn't do it for us. Shows the 'moon' LED on older Thinkpads when
sleeping.

Tested on T43p, W500, T510 by me and on X61s by deraadt@.

ok deraadt@


# 1.190 27-Jul-2010 jordan

Early initialization of acpiec if ECDT table exists
Fixes hang when booting thinkpads while docked
ok deraadt


# 1.189 26-Jul-2010 deraadt

remove merge error which was commited


# 1.188 26-Jul-2010 pirofti

Add support for Sony ACPI hotkeys via a new driver: acpisony(4).

Currently it only works for the suspend button and tries to do right for
the brightness events, but I haven't found a Sony laptop that like to do
right so far.

In the future I want to make the brightness keys work on all Sony's and
also add support for the zoom hotkeys and whatever other funky keys I can
find on those things.

Okay deraadt@.


# 1.187 22-Jul-2010 deraadt

pretty large cleanup of SMALL_KERNEL


# 1.186 21-Jul-2010 deraadt

in the acpi GPE handler, late ack level interrupts not edge interrupts.
Duh.


# 1.185 21-Jul-2010 deraadt

Merge the edge and level gpe handlers into one (now possible since the
gpe structure tells us whether the gpe is edge or level)


# 1.184 21-Jul-2010 deraadt

delete #if 0 code we are not going to use; ok marco


# 1.183 20-Jul-2010 deraadt

make the mask code for acpi_enable_onegpe() match other similar code


# 1.182 20-Jul-2010 deraadt

Handle the acpi interrupt controller with proper edge/level handling,
wrapped in spltty to avoid racing against the interrupt controller.
Repair the gpe bit masking code while there, and do operations in the
right order
ok kettenis mlarkin, help from jordan at figuring out the order of
operations.


# 1.181 20-Jul-2010 matthew

When we're preparing to go to sleep, only call bufq_restart() if other
preparations failed.

ok kettenis@, thib@, deraadt@


# 1.180 19-Jul-2010 deraadt

permit compilation with ACPI_DEBUG


# 1.179 19-Jul-2010 deraadt

there is no need for a seperate dsdt_softc pointer; dsdt does not have a
softc but is just a part of the single acpi driver.
ok jordan


# 1.178 19-Jul-2010 deraadt

acpi_delay() is no longer needed; ok jordan


# 1.177 18-Jul-2010 mlarkin

Clear event status on resume, to avoid going back to sleep/reboot/shutdown
immediately after resume. Tested by a bunch of people on hackers@ on
various hardware. Fixes at least Toshiba NB200 and Gateway LT3103u.

ok marco


# 1.176 13-Jul-2010 deraadt

rename some thread related interfaces so they are logical


# 1.175 10-Jul-2010 jordan

Verify _ADR bdf is correct (some systems return 0xffff)
Works on Dell x9xx and Studio 1555


# 1.174 09-Jul-2010 jordan

Clear the noise


# 1.173 08-Jul-2010 jordan

Add mapping for ACPI device to PCI bdf (match autoconf tree)
Simplify resource parsing function to use buffer argument
Convert namespace linked lists to use queue macros
ok marco@, deraadt@


# 1.172 06-Jul-2010 deraadt

Use spltty() to lock downcalls from apm against the information being
modified by the acpi thread.
ok kettenis


# 1.171 06-Jul-2010 deraadt

Don't use a workq to do the suspend, because it races aginst the acpi
thread. Instead, just tell the acpi thread to do the suspend for us.
This makes apmd & zzz work correctly. While here, have acpithinkpad
attempt to post the event to apm before prompting the suspend itself.
ok kettenis marco mlarkin


# 1.170 05-Jul-2010 mlarkin

Reset register SCI_EN on resume. The ACPI spec clearly states we don't have
to do this, but many machines don't, or forget to, which leads to all sorts
of problems on resume (broken battery stats, overheating, failure to resume
more than once, etc).

tested by many, ok marco@, deraadt@


# 1.169 01-Jul-2010 thib

do bufq quiesce again, since vnd/the bufq_done call in
biodone has been fixed.


# 1.168 01-Jul-2010 jordan

Backout recent AML changes, breaks on R210 and others


# 1.167 01-Jul-2010 jordan

Simplified aml_parse_resource call, cleanup code
ok marco


# 1.166 01-Jul-2010 jordan

Fix subordinate bus # for multi-root PCI buses
ok marco, kettenis


# 1.165 30-Jun-2010 thib

Disable/partially backout the bufq quiesce changes as this
is causing havoc with vnds and release must be buildable.


# 1.164 29-Jun-2010 deraadt

Fix ramdisks (new code was placed inside a SMALL_KERNEL chunk)


# 1.163 29-Jun-2010 jordan

Add support for mapping ACPI to PCI devices
ok kettenis, deraadt


# 1.162 29-Jun-2010 kettenis

Call bufq_quiesce() on suspend to stop all I/O and bufq_restart on resume.
Makes suspend/resume work much better while doing disk I/O.

ok mlarkin@


# 1.161 29-Jun-2010 tedu

60 is a more reasonable guess for number of minutes in an hour than 100.
spotted by TeXitoi. ok deraadt


# 1.160 27-Jun-2010 jordan

Add _acpi_matchhids routine, precursor to pci device match


# 1.159 27-Jun-2010 mlarkin

Clear ACPI fixed event status on resume (power buttons/etc) so that some
machines don't immediately resume after suspending to S3.

Tested by kettenis@ and I on 5 or 6 machines, deraadt@ "yeah, looks good"


# 1.158 27-Jun-2010 jordan

Cleaned up acpi_maptable, adds to table list directly


# 1.157 07-Apr-2010 deraadt

If we have done a wsdisplay_suspend(), and ACPI decides it does not want to
suspend, we must roll back by calling wsdisplay_resume() or the virtual
console switching will be locked forever
ok oga


# 1.156 07-Apr-2010 kettenis

Make suspend/resume work on MP machines (running an MP kernel). Joint work
from mlarkin@ and me, with some amd64 fixes thrown in by deraadt@

ok marco@, deraadt@, pirofti@, mlarkin@


# 1.155 31-Mar-2010 kettenis

Bring /dev/apm support in line with apm(4). Make the suspend button send
a suspend request event to apmd(8) instead of suspending immediately. Also
keep track of whether /dev/apm and /dev/apmctl are currently open, such that
we can still suspend immediately if apmd(8) isn't running.

ok deraadt@, marco@, pirofti@, jsing@, oga@


# 1.154 30-Mar-2010 oga

Prevent the apmd/x races for good.

When we hit suspend time, go through all wsdisplays on the system. if
they are in mode MAPPED, but not MODE_DUMBFB then if possible do a full
vt switch to a !mapped vt, and prevent switching back until resume time.
This has to be called from MD code because this involves userland
running so that X can run the vt switch signal handler. This way, any
case where we are using the "poke registers from userland" model, we
will not be on the hardware when we go down, so the kernel can actually
handle thing properly.

Tested on several acpi laptops (by kettenis@ and ian@), x40 (me and
beck@ at LEAST) and zaurus (me). Maybe others, but if so I forgot who at
this time..

Idea from deraadt somewhere over the Faroe Islands (I thought of a
similar thing myself a while ago). Much prompting from him. Ok and
comments miod@


# 1.153 25-Mar-2010 oga

Fix & vs. && in the kqfilter code.

Based on a comparison with the apm code.

ok deraadt@, kettenis@


Revision tags: OPENBSD_4_7_BASE
# 1.152 26-Nov-2009 mlarkin

Visual (and on some machines, audio) feedback while the machine is
resuming via calls to aml _SST method. This also makes the "moon" LED on
thinkpads to blink during resume and cease when resume is completed.

ok pirofti@


# 1.151 26-Nov-2009 deraadt

Toggle into "cold" when we we disable interrupts, because tsleeping
in the AML interpreter and context switching into userland processes
tends to sometimes pop the ACPI trampoline page out of our address
space. Makes lots more machines work.
ok kettenis mlarkin


# 1.150 24-Nov-2009 jsg

in the resume path evaluate _WAK before dealing with device drivers
like the spec says, not after.

ok mlarkin@


# 1.149 24-Nov-2009 deraadt

abort suspends on SMP systems until that gets written; ok mlarkin


# 1.148 23-Nov-2009 mlarkin

pci should return the result of it's children's suspend/resume calls back
to its parent.

handle suspend failure case and unwind if devices have an activate function
that returns failure.

ok deraadt@, kettenis@


# 1.147 23-Nov-2009 mlarkin

acpi* devices need to call their children on suspend/resume.
ok deraadt@


# 1.146 23-Nov-2009 pirofti

Remove ACPI_SLEEP_ENABLED checks.

This enables by default the suspend/resume paths in the kernel.

Okay deraadt@.


# 1.145 23-Nov-2009 deraadt

knf and spacing that are super annoying


# 1.144 23-Nov-2009 mlarkin

Hook up Theo's new children suspend/resume function, as well as more ACPI
suspend/resume glue.

ok deraadt@


# 1.143 26-Oct-2009 deraadt

Do not do strncmp comparisons on dv_xname because longer device names which
look similar could arrive in the future. Instead, compare directly against
dv_cfdata->cf_driver->cd_name
Issue originally spotted by miod


# 1.142 26-Oct-2009 jordan

Verify checksum+address when loading ACPI tables.
Some systems had invalid entries in RSDT/XSDT.
ok marco@


# 1.141 23-Jul-2009 cnst

New aibs(4) driver for ASUSTeK AI Booster (ACPI ATK0110) hardware monitoring,
with sensor state support through limits provided by the ACPI.

Tested on several ASUS motherboards kindly networked by Sam Fourman Jr.

ok deraadt marco jordan


Revision tags: OPENBSD_4_6_BASE
# 1.140 03-Jun-2009 pirofti

Import acpi power resource driver. This is needed for suspend/resume logic.

Okay jordan@ and marco@.


# 1.139 03-Jun-2009 pirofti

Add acpivideo support. This adds brightness support for all laptops
except thinkpads, they will use the acpithinkpad driver. The driver is
also hooked into wsconsole. So brightness can be adjusted via:

$ wsconsctl display.brightness=<percentage>

This is very helpfull on some laptops that have a nasty bios and get two
steps instead of one when pressing the brightness button.

Tested on various dell, fujitsu, acer, samsung and other laptops.

Okay marco@, miod@. Suggestions from kettenis@.
Lots of reviews and help from miod@, thanks!


# 1.138 03-Jun-2009 jordan

Added ejectable bay for IDE cdroms
ok marco@


# 1.137 30-Apr-2009 marco

Oga changed apm ioctls and forgot to grep. Temporary work around by hitting
the sleep button when we get any request. This interface will chnage at
some point.
ok oga


# 1.136 19-Apr-2009 krw

Call acpi_sleep_walk() when powering down just like when entering
any other sleep state. This tells acpi wake devices to not wake up
the box until manual powering up resets their status. Makes my Dell
GX520 stay powered off in response to 'halt -p'.

In snaps for a while.

ok marco@ (several times) deraadt@


# 1.135 17-Apr-2009 pirofti

Add device ranking support for future sleep/wake developments.

okay marco@


# 1.134 11-Mar-2009 jordan

Cleanup.. use aml_evalinteger instead of aml_evalname
ok marco@


# 1.133 10-Mar-2009 jordan

Fix cut/paste error.. call _BFS not _PTS
ok marco@


Revision tags: OPENBSD_4_5_BASE
# 1.132 21-Feb-2009 marco

overzealous cut/paste; found by Vladimir Kirillov <proger@uaoug.org.ua>


# 1.131 20-Feb-2009 marco

Oops didn't mean for Debugger() to remain there.


# 1.130 19-Feb-2009 marco

suspend/resume bits so that we can develop this in tree. This is disabled.
code from mlarkin and me
help from art,toby,jordan and several others
ok jordan, go for it deraadt


# 1.129 10-Feb-2009 jordan

Fix null pointer for shutdown if acpi not enabled
ok marco@


# 1.128 04-Feb-2009 kettenis

Some systems that claim to be ACPI 2.0 apparently don't provide an XSDT
address. Make the code fall back on the RSDT on those systems.

From Joerg Niendorf <p3y80@internode.on.net>. Fixes PR 5986.


# 1.127 17-Jan-2009 krw

A couple of stray malloc/memset -> malloc(M_ZERO), a bit of knf and
lose a couple of unnecessary casts. No functional change.

Feedback from kettenis@

ok kettenis@ marco@


# 1.126 06-Nov-2008 marco

Collapse all sanity checks of hids into one function.

Originaly from jordan


Revision tags: OPENBSD_4_4_BASE
# 1.125 02-Jul-2008 fgsch

add acpivideo. for now almost an empty skel so others can work on
suspend/resume. eventually it will also manage output switching and
brightness where supported. prodded by marco@


# 1.124 11-Jun-2008 marco

Fix $OpenBSD$ for once and for all


# 1.123 08-Jun-2008 deraadt

aaa_dev was being set from an uninitialized variable; ok toby


# 1.122 07-Jun-2008 marco

Kill ACPIVERBOSE. It was never used and is worthless to boot.

comments jmc
ok toby


# 1.121 06-Jun-2008 marco

Shrink acpi some more for floppies and while here re-enable acpi on the
floppies. Fixes crazy sony vaio kettenis has.

tested by deraadt
ok deraadt jordan kettenis


# 1.120 01-Jun-2008 marco

Fix \n nit in SMALL_KERNEL.


# 1.119 01-Jun-2008 marco

Re-enable new parser. Lots of testing by various folks. Fits all media
now too.

ok kettenis deraadt


# 1.118 16-May-2008 dlg

back out jordans new parser.

it didnt have enough oks from the right people, it did break machines
that were previously working, and it bloated the RAMDISKS and install
media beyond the constraints of the media.

requested by deraadt@


# 1.117 14-May-2008 jordan

Adding new ACPI Parser code
Old guts of code still exists, needs to be torn out next
ok marco@
ok brad@


# 1.116 27-Apr-2008 jcs

add acpithinkpad(4), an acpi support driver for ibm/lenovo
thinkpads.

provides functionality that the bios did on older, non-acpi
thinkpads like respond to screen brightness keys and toggle the
bluetooth device on and off.

ok deraadt@ and marco@


# 1.115 24-Apr-2008 jsing

Introduce acpiasus(4), a driver for the ACPI based hotkeys found in many
ASUS laptops (including the ASUS EeePC) - largely based on NetBSD's
asus(4) driver. On the ASUS EeePC this allows us to enable/disable
wireless, change screen brightness and use the volume keys.

ok jsg@, weingart@


# 1.114 12-Apr-2008 kettenis

Get rid of acpi_s5 global variables; simply send SIGUSR2 instead of SUGUSR1
to tell init(8) to power down the machine.

ok krw@


Revision tags: OPENBSD_4_3_BASE
# 1.113 21-Jan-2008 jsg

Check against RESET_REG_SUP in FADT before doing the acpi reset dance
after all, not doing so appears to break more machines than it fixes.

ok marco@


# 1.112 05-Jan-2008 kettenis

Never attach more acpicpu(4) devices than cpu(4) devices. Always attach
the first acpicpu(4) device on non-MULTIPROCESSOR kernels, regardless of its
ACPI processor ID.

ok mikeb@


# 1.111 05-Dec-2007 deraadt

Hang acpi(4) below bios(4) instead of mainbus(4). This lets us move acpi(4)
before pcibios(4), and then.... if acpi(4) attaches, skip pcibios(4) since
it messes the machines up. Fixes claudio's HP dl320 G5, and almost assuredly
others.
ok kettenis


# 1.110 05-Dec-2007 deraadt

repair acpiopen/acpiclose and friends to handle the /dev/apmctl interface,
so that apmd(8) can actually work correctly against acpi(4), and thus,
even do the fancy -C/-A stuff; also tested by jmc


# 1.109 02-Dec-2007 fgsch

fix the trailing space for real now.


# 1.108 02-Dec-2007 fgsch

avoid trailing spaces on output; marco@ ok.


# 1.107 27-Nov-2007 kettenis

Fix off-by-one that made us attach acpicpu0 and acpicpu1 on GENERIC kernels.

ok marco@


# 1.106 25-Nov-2007 jsg

When available and ACPI is enabled use the ACPI reset method
to reboot. This is required for some quirky sis machines
and likely others to reboot.

Tested by many. 'commit early commit often' deraadt@


# 1.105 19-Nov-2007 deraadt

sigh, amd64 still uses that stupid acpi_enabled variable...


# 1.104 19-Nov-2007 deraadt

softc's come initialized to 0, you know...


# 1.103 19-Nov-2007 deraadt

store revision; from jsg


# 1.102 19-Nov-2007 kettenis

Initialize sc_facs earlier.

ok deraadt@, beck@, canacar@


# 1.101 16-Nov-2007 deraadt

fix the bus_space #define nightmare, so that amd64 and i386 are much more
uniform. as a result shared code like acpi needs less #ifdef's
ok marco kettenis


# 1.100 15-Nov-2007 deraadt

remove the #ifdef ACPI_ENABLE. the EC codepath is either safe enough (and
will be improved), or it is unsafe because AML assumes it must be there
ok almost everyone


# 1.99 15-Nov-2007 mikeb

Make acpicpu(4) attach to the existing CPUs only.

Problem reported by form@. Fix idea by kettenis@.
Lots of help from deraadt@. Tested by deraadt@, form@ and me.

ok deraadt marco


# 1.98 14-Nov-2007 deraadt

spacing


# 1.97 12-Nov-2007 deraadt

correct : vs , in attach-time messages


# 1.96 06-Nov-2007 deraadt

all ACPI_ENABLE kernels need acpi_interrupt()


# 1.95 06-Nov-2007 deraadt

move acpi_read_pmreg() and acpi_write_pmreg() out of SMALL_KERNEL so
that acpi ramdisks can compile (obvious solution)


# 1.94 05-Nov-2007 canacar

Establish the acpi interrupt earlier. Fixes problems with some
GENERIC.MP kernels not getting acpi interrupts. This is a
workaround until the real issue with late establishment is fixed.
ok weingart@, beck@, gwk@


# 1.93 03-Nov-2007 jordan

Added support for displaying wakeup devices
ok beck@,weingart@,gwk@


# 1.92 11-Oct-2007 kettenis

When walking the tree yo find _PRT methods, check whether the device is
actually enabled and functioning. Similar to what we do for _INI.

ok weingart@, beck@


# 1.91 08-Oct-2007 krw

More simple memset(,0,) -> M_ZERO changes. In this batch move to
size(*p) as the first malloc() parameter where p is declared locally
and thus easy to check. Add M_ZERO to gpe_table allocation in acpi.c
even though there is no obvious bzero or memset nearby.


# 1.90 13-Sep-2007 mk

Remove unnecessary memset() (see aml_evalnode(9)) and a bit of KNF.

ok toby


# 1.89 13-Sep-2007 weingart

Implement ACPI 6.5.1 spec tree walk for _STA and _INI.
Fixes mk's laptop. No regressions so far (thank you to the
testers).

ok gwk@, mk@, marco@


Revision tags: OPENBSD_4_2_BASE
# 1.88 17-Apr-2007 mk

Now that acpidock no longer hangs machines during boot (at least
according to the reports I've been getting) it should be safe to enable
it again, so remove the #if 0 stuff around it.

ok marco


# 1.87 11-Apr-2007 jordan

Added changes for C-State
ok marco@


# 1.86 26-Mar-2007 marco

Calculate minutes left more accurately.

From giovanni <qgiovanni@gmail.com>


Revision tags: OPENBSD_4_1_BASE
# 1.85 03-Mar-2007 mk

Stop looking for acpidock until we fix that stuff that breaks all pre-60
series ThinkPads with acpidock(4) enabled.


# 1.84 22-Feb-2007 marco

We were getting no interrupts in .MP. There are 2 fixes; one in
acpi_machdep.c and move the interrupt establishment down after ioapic
has been initialized.

ok jordan


# 1.83 21-Feb-2007 kettenis

Make an acpi-enabled GENERIC.MP work on systems that only support PIC mode.

ok marco@


# 1.82 19-Feb-2007 jordan

Added acpi_map_address function for mapping GAS/IO registers
ok marco@


# 1.81 18-Feb-2007 jordan

Added dynamic perfect hash for opcodes
ok marco@


# 1.80 18-Feb-2007 marco

Shave off another K pre compression.
Shuffle SMALL_KERNEL functions around so that we only have a single #ifndef


# 1.79 17-Feb-2007 marco

Add SMALL_KERNEL throught acpi to make it fit on boot media.
ok deraadt


# 1.78 13-Feb-2007 marco

Add smarter _INI code. This helps with some dissapearing devices.

ok kettenis jordan


# 1.77 31-Jan-2007 gwk

Introduce acpi_hasprocfvs thats "ACPI Has Processor Frequency and Voltage
Scalling" a global flag set by acicpu attach to indicate that ACPI is
handling fvs through the _PSS objects. This will fix disappearing
EST/powernow when booting kernels with acpi enabled, as acpicpu isnt
compiled into GENERIC yet. Also in cases where acpicpu is built in and
legacy EST/powernow work but acpi is missing the _PSS object, the legacy
routines will be used.

tested by dim@, and Nick Nauwelaerts <nick AT nauwelaerts.net>
ok marco@, dim@


# 1.76 22-Jan-2007 mk

Actually look for docking stations by looking for _DCK nodes.

ok marco


# 1.75 02-Jan-2007 marco

On some systems EC must be attached as the first device. Move EC from
generic _HID devices to its own routine to force first attach. Bug found
and debugged by joris@

Code mostly written by joris@


# 1.74 26-Dec-2006 marco

Add polling to devices that require it such as acpibat and acpitz.
Use the same tsleep/wakeup mechanism as the interrupt code to keep it non-
concurrent.


# 1.73 21-Dec-2006 deraadt

even more knf love


# 1.72 21-Dec-2006 marco

Remove some leftover acpiec gpe/interrupt code.


# 1.71 20-Dec-2006 deraadt

oops, name already used


# 1.70 20-Dec-2006 deraadt

rename __acpi_enable_gpe(); ok marco


# 1.69 20-Dec-2006 deraadt

acpi_unmap_pmregs() is not used, ok marco


# 1.68 19-Dec-2006 deraadt

simplify some low-level methods substantially (shrinking code); ok marco jordan


# 1.67 06-Dec-2006 mk

malloc(..., M_NOWAIT) with no NULL check is bad. If allocation fails,
complain, clean up, and bail out of attach.

input and ok gwk


# 1.66 28-Nov-2006 jordan

Fixed clearing arg for acpi_init_pic
ok kettenis@


# 1.65 27-Nov-2006 kettenis

Don't spam dmesg.


# 1.64 27-Nov-2006 jordan

Fixes for acpiprt routing
Post-parse fixup for namerefs
ok kettenis@


# 1.63 25-Nov-2006 marco

Don't handle acpiev events inside the gpe handler since this causes deadlocks.

ok jordan@


# 1.62 15-Nov-2006 kettenis

Hook up acpi apic interrupt routing support to generic acpi code.

ok jordan@


# 1.61 02-Nov-2006 marco

Kill acpiec attach function and add its functionality to the generic _HID
device attach

prodded and ok jordan


# 1.60 02-Nov-2006 marco

Fix comments to reflect reality.


# 1.59 19-Oct-2006 marco

Replace ACPI ioctls with APM ioctls. This makes ACPI talk the same protocol
as APM therefore userspace can now retrieve power information in the same
fashion as all other apm arches (zaurus, macppc, i386 etc).

To use force the use of acpi device like so: apmd -f /dev/acpi

ok jordan@


# 1.58 19-Oct-2006 jordan

Fixed GPE debug levels
Fixed ref counter on temporary bufferfields
DSDT parsing fully working on IBM Thinkpad
ok marco@


# 1.57 15-Oct-2006 jordan

Removed old GPE handler code remnants
ok marco@


# 1.56 12-Oct-2006 jordan

Massive ACPI changes
Fixed ACPI leaks
new ACPI API
ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.55 30-Jun-2006 gwk

KNF, No functional change,
"go at it" marco@


# 1.54 30-Jun-2006 jordan

Support for calling Embedded Controller via new GPE interface
ok marco@


# 1.53 30-Jun-2006 jordan

Added new code for GPE handling
Also works with ACPIEC; need to fixup acpiec.c to remove original interrupt handler
ok marco@


# 1.52 30-Jun-2006 jordan

Added offset argument for acpi read/write pmreg routines (new GPE code)
ok marco@


# 1.51 31-May-2006 todd

do not call acpi_powerdown() unless acpi has successfully initialized
ok gwk@ canacar@


# 1.50 29-May-2006 canacar

Add embedded controller support.
ok marco@ deraadt@ testing & ok gwk@


# 1.49 19-May-2006 canacar

Minimal thermal zone driver. Monitors thermal zone temperature,
shuts down the system if the 'critical temperature' is reached.
ok marco@


# 1.48 11-Apr-2006 marco

KNF.


# 1.47 11-Apr-2006 gwk

Add acpi_resume(struct acpi_softc *sc), which calls some AML methods,
detected at attach time, currently unused.

ok marco@


# 1.46 11-Apr-2006 gwk

Discover some ACPI sleep related methods, and call them at the
appropriate time in acpi_enter_sleep_state().

ok marco@


Revision tags: OPENBSD_3_9_BASE
# 1.45 26-Feb-2006 marco

Add acpicpu as a device.

ok jordan@


# 1.44 23-Feb-2006 marco

remove dead function.

ok jordan@


# 1.43 22-Feb-2006 marco

Print out all tables it found during autoconf. We'll need this data in
the future.


# 1.42 22-Feb-2006 marco

Make dmesg less verbose.

ok jordan.


# 1.41 22-Feb-2006 jordan

Added parsing of SSDT tables
ok marco@


# 1.40 22-Feb-2006 jordan

Added new aml_register to notify by device id
Moved powerdown code to button device
ok marco@


# 1.39 21-Feb-2006 marco

Readjust debug levels that crept in too low over the last few days.
KNF


# 1.38 21-Feb-2006 marco

Rig the power button so that the machine shuts down correctly.


# 1.37 20-Feb-2006 marco

Add acpibtn device. Remains disabled for now.


# 1.36 19-Feb-2006 jordan

Added code for aml_notify GPE notification callback
ok marco@


# 1.35 19-Feb-2006 grange

Fix acpi_enter_sleep_state() by replacing current ugly hack
with mostly correct sleeping state transition code.

ok brad@ marco@


# 1.34 19-Feb-2006 marco

jordan:
Enable interrupts.
Added peripheral code to make this work.

marco:
Replace softintr and other types of interrupts with a handler with context.
Fixed interrupt magic for GPEs.
Some KNF

This is work done by jordan and I but has to be a single commit.


# 1.33 17-Feb-2006 marco

Remove earlier introduced delays. We really only need them during autconf.

ok jordan@


# 1.32 17-Feb-2006 marco

Add a generic delay function as per the ACPI spec.

from jordan@


# 1.31 17-Feb-2006 marco

Use tsleep instead of delay when !cold.


# 1.30 17-Feb-2006 marco

Argh, we need delays between reads/writes. The backend reads (i2c most likely)
are so slow that values come back as 0 sometimes. We have to do this
despite the spec saying we shouldnt have to. Guess having a faster
parser than others can hurt sometimes.

Idea from jordan.


# 1.29 17-Feb-2006 jordan

Fixed code for handling _INI
Rearranged debug statements
ok marco@


# 1.28 16-Feb-2006 jordan

Added stub for calling _INI
Fixed aml_dowhile for NULL return
ok marco@


# 1.27 16-Feb-2006 jordan

Fixed parser/evaluator to retrieve battery info+state
Cleanup of parser
Dell laptops return battery status
ok marco@


# 1.26 06-Feb-2006 brad

fix a typo so this will compile on amd64.


# 1.25 04-Feb-2006 marco

When ACPI_ENABLED wasn't set but the devices were it had an unknown variable.

Pointed out by ian@.


# 1.24 03-Feb-2006 jordan

Rewrote aml parser yet again...
Fixed bus_space_map for i386/amd64
Added _BST and _BIF to acpibat to get status/info
ok marco@


# 1.23 20-Jan-2006 jordan

More evaluator changes
Removed old evaluator code
Fixed _HID value
ok marco@


# 1.22 19-Jan-2006 jordan

Added support for read/write of PCI Config space
Help jason@
ok marco@


# 1.21 18-Jan-2006 jordan

Added new evaluation routines
Added helper methods for buffer/field evaluation
Fixed broken indentation on dsdt.c

ok marco@


# 1.20 17-Jan-2006 jordan

Major overhaul of the aml parser. Evaluater temporarily disabled.

ok marco@


# 1.19 06-Jan-2006 grange

Check for ACPI control availability only if ACPI_ENABLE defined.


# 1.18 06-Jan-2006 grange

Informative error messages on attach, prettier dmesg.


# 1.17 06-Jan-2006 grange

printf -> dnprintf conversion for debug messages.


# 1.16 05-Jan-2006 grange

Find SLP_TYPx values and store them for future use in
acpi_enter_sleep_state().


# 1.15 05-Jan-2006 grange

Random KNF and cleanup


# 1.14 05-Jan-2006 grange

Provide symbolic names for the acpi sleeping states and use it.


# 1.13 28-Dec-2005 marco

Moved DEVNAME to a acpidev.h

ok jordan@


# 1.12 18-Dec-2005 sturm

add back comment about wrong code

ok marco


# 1.11 16-Dec-2005 marco

Remove test for kbd_reset. Prompted deraadt@, ok jordan@


# 1.10 16-Dec-2005 jordan

Added hooks for powerdown/power button press


# 1.9 16-Dec-2005 jordan

Cleaned up common power management register i/o
Added enter sleep state ioctl
Hooked power button interrupt
ok marco@


# 1.8 16-Dec-2005 marco

Remove autoconf chatter


# 1.7 16-Dec-2005 jordan

Moved DSDT AML parsing into parent ACPI driver
ok marco@


# 1.6 14-Dec-2005 marco

Add AC device glue.


# 1.5 14-Dec-2005 marco

Adjust debug printing levels to something more sane.


# 1.4 13-Dec-2005 marco

Add glue to attach devices to apci.


# 1.3 07-Dec-2005 marco

KNF


Revision tags: OPENBSD_3_8_BASE
# 1.2 10-Jul-2005 grange

Spacing cleanup.


# 1.1 02-Jun-2005 tholo

Start on a basic ACPI framework -- does not do much more than read out the
ACPI tables into kernel memory and attach ACPI and HPET timers currently.

In order to test this code, enabling the devices in GENERIC as well as
the ACPI_ENABLE option is needed. This code does not do any thermal
control yet, so this should be done with care depending on the platform.

In the tree so more people can contribute to making this more fully
featured.

Ok niklas@ grange@ tedu@


# 1.394 25-Dec-2020 visa

Refactor klist insertion and removal

Rename klist_{insert,remove}() to klist_{insert,remove}_locked().
These functions assume that the caller has locked the klist. The current
state of locking remains intact because the kernel lock is still used
with all klists.

Add new functions klist_insert() and klist_remove() that lock the klist
internally. This allows some code simplification.

OK mpi@


# 1.393 17-Dec-2020 kettenis

Fix some issues with referencing named ACPI nodes from Packages.
These references need to be resolved at runtime rather than when
they're parsed such that they pick up the right values for those nodes
which can be changed when for example _INI methods run.

The current approach is to replace these reference with a string that
names the node in question. The problem with that is that packages
can also contain normal strings. Which means that we need special
code that depends on the context where the package is used.

This diff takes a different approach by retaining a reference when
parsing. Code that uses a package will need to resolve this reference
but there is no ambiguiety anymore.

ok patrick@


# 1.392 05-Dec-2020 kettenis

Add support for the _EVT method to the GPIO support code.

ok patrick@


Revision tags: OPENBSD_6_8_BASE
# 1.391 27-Aug-2020 jmatthew

Reorder the acpi attach process so we look at Processor() CPU nodes
after Device() ones, since we should prefer the newer node type.
If we see any Device() nodes, don't attach acpicpu(4) to any Processor()
nodes if they're also present. This also makes acpitz(4) and
acpipwrres(4) devices attach slightly later.

ok kettenis@, also tested by jmc@


# 1.390 16-Aug-2020 gnezdo

Remove sc_maxgpe from acpi_softc which was never read from

Remove a dead conditional from acpi_init_gpes while there.

ok jcs@


# 1.389 21-Jul-2020 deraadt

acpi can use IPL_BIO (a low interrupt) since it only enqueues operations for
later processing. The use of a high interrupt will predate suspend/resume
efforts, we had to redesign acpi to be non-reentrant obviously
discussed with kettenis, in snaps for more than a week


# 1.388 24-Jun-2020 cheloha

kernel: use gettime(9)/getuptime(9) in lieu of time_second(9)/time_uptime(9)

time_second(9) and time_uptime(9) are widely used in the kernel to
quickly get the system UTC or system uptime as a time_t. However,
time_t is 64-bit everywhere, so it is not generally safe to use them
on 32-bit platforms: you have a split-read problem if your hardware
cannot perform atomic 64-bit reads.

This patch replaces time_second(9) with gettime(9), a safer successor
interface, throughout the kernel. Similarly, time_uptime(9) is replaced
with getuptime(9).

There is a performance cost on 32-bit platforms in exchange for
eliminating the split-read problem: instead of two register reads you
now have a lockless read loop to pull the values from the timehands.
This is really not *too* bad in the grand scheme of things, but
compared to what we were doing before it is several times slower.

There is no performance cost on 64-bit (__LP64__) platforms.

With input from visa@, dlg@, and tedu@.

Several bugs squashed by visa@.

ok kettenis@


# 1.387 10-Jun-2020 jca

Give userland a consistent battery and A/C state after resume

Previously we refreshed the state of acpiac(4) and acpibat(4)/acpisbs(4)
by queueing fake ACPI events to be processed by the acpi thread.
acpibat(4) using ACPIDEV_POLL meant that its status could be refreshed
first, resulting in an APM_POWER_CHANGE event sent to userland before
the state of acpiac(4) was up to date. Because of this, apmd(8) could
see A/C unplugged when it was in fact plugged, possibly leading to
a wrong decision.

Instead of using fake ACPI events, refresh the status of acpiac(4) and
acpibat(4)/acpisbs(4) right away at DVACT_WAKEUP time. To reuse the
existing refresh code of those drivers, make their ACPI *_notify()
handlers responsible for sending APM_POWER_CHANGE events instead. Also
queue an APM_POWER_CHANGE event right after resume to retain existing
behavior.

This fixes the consistency of data shown to userland, and makes those
drivers less special.

Initially discussed last year (!) with mpi@ at Miod's place.
Feedback and ok jcs@, deraadt@ agrees with the use of DVACT_WAKEUP here.


# 1.386 29-May-2020 deraadt

dev/rndvar.h no longer has statistical interfaces (removed during various
conversion steps). it only contains kernel prototypes for 4 interfaces,
all of which legitimately belong in sys/systm.h, which are already included
by all enqueue_randomness() users.


# 1.385 14-May-2020 kettenis

Bring back the acpi(4) changes. This time around only use the ACPI _CRS
information on ACPI 5.0 and later.

ok krw@, patrick@


# 1.384 11-May-2020 jca

More accurate remaining power reporting with disparate batteries

Overall remaining power was computed as the average of the remaining
power of each battery, in percents. This doesn't work with batteries of
different capacity, so use the total capacity and remaining power
instead.

Reported by lidstah, successful tests by lidstah and benno@, ok kettenis@


# 1.383 08-May-2020 kettenis

Move parsing of _CRS into acpi(4) and pass the parsed address and interrupt
information in struct acpi_attach_args.

ok mpi@


Revision tags: OPENBSD_6_7_BASE
# 1.382 14-Apr-2020 kettenis

Implement acpi_getprop(), the ACPI equivalent of OF_getprop().

ok patrick@


# 1.381 12-Apr-2020 kettenis

Add support for the _CCA method. This method indicates whether DMA is
cache-coherent or not. To implement this, acpi(4) gets two bus_dma tags
and passes the appropriate one when attaching devices based on _CCA.
On i386/amd64, where for all practical purpose DMA is always cache-coherent,
the two tags are the same. But on arm64 they are distinct.

ok patrick@


# 1.380 07-Apr-2020 visa

Abstract the head of knote lists. This allows extending the lists,
for example, with locking assertions.

OK mpi@, anton@


# 1.379 03-Apr-2020 mpi

Fix incoherencies of errors returned by various *kqfilter routines.

EOPNOTSUPP is returned when there's no "filterops" for a given fd.
EINVAL when the requested filter isn't supported by the "filterops".
ENXIO means the underlying device is no longer valid/present.

ok visa@


# 1.378 20-Feb-2020 visa

Replace field f_isfd with field f_flags in struct filterops to allow
adding more filter properties without cluttering the struct.

OK mpi@, anton@


# 1.377 31-Dec-2019 visa

Use C99 designated initializers with struct filterops. In addition,
make the structs const so that the data are put in .rodata.

OK mpi@, deraadt@, anton@, bluhm@


# 1.376 31-Dec-2019 mpi

Convert infinite sleeps to {m,t}sleep_nsec(9).

ok kn@


# 1.375 31-Dec-2019 jsg

include sys/sensors.h for sensor use
don't depend on indirect inclusion from acpidev.h


Revision tags: OPENBSD_6_6_BASE
# 1.374 07-Sep-2019 kettenis

Revert acpipci(4) changes. Too many systems are broken. We'll try again
after 6.6 as been released. The acpireg.h change stays behind.


# 1.373 28-Aug-2019 kettenis

Use ACPI information to attach PCI busses like we do on arm64. There are a
few additional quirks though, and attaching the PCI busses is delayed to
replicate the existing code more closely. That may be changed in the
future. Also tweak how we handle MSI support and respect to ACPI flag
that says we shouldn't attempt to use MSIs.

Some fallout is expected.

ok patrick@


# 1.372 27-Aug-2019 deraadt

On amd64 the bootup and resume paths for SP/BP/SP were not performing
msr/pat, clock-startup, firmware upgrade, fpu initialization, and cpu
soft-state initialization in the same order. That kind of chaos is
crazy and fragile. Improve the acpi resume call sequence and unify order
of the operations.


# 1.371 02-Jul-2019 jcs

acpi_set_gpehandler: turn edge arg into flags, add support for
passing GPE_DIRECT which directly executes the supplied callback and
clears a level interrupt rather than going through acpi_taskq which
can be slow when other driver callbacks are in the queue.

This should not be used for GPE handlers that need to do anything
ACPI-related.

ok kettenis


# 1.370 10-Jun-2019 kettenis

Print proper ACPI version number.

ok mlarkin@


# 1.369 08-Jun-2019 kettenis

When establishing the mapping between ACPI device nodes and PCI devices,
skip PCI host bridges and devices that aren't there.

ok patrick@


# 1.368 07-Jun-2019 kettenis

Introduce acpi_getsta() and use it to eliminate some duplicated code.

ok patrick@


# 1.367 12-May-2019 kettenis

Make sure the extended PM register descriptions actually exist before
using them. Fixes machines with ancient ACPI 1.0 (such as recent
hypervisors).

ok deraadt@


# 1.366 12-May-2019 kettenis

Add support for mapping ACPI PM registers using the "extended" gasio
representation from the FADT. Mostly fixes the Lanner NCA-1510.

ok lteo@, mlarkin@, deraadt@


# 1.365 11-May-2019 lteo

Fix incorrect error message.

ok kettenis@


# 1.364 08-May-2019 tedu

free size. the allocation is the header plus the length.


Revision tags: OPENBSD_6_5_BASE
# 1.363 04-Apr-2019 kettenis

Avoid potential null-pointer dereference. Found by Kent R. Spillner.

ok deraadt@, jsg@


# 1.362 20-Jan-2019 jsg

fix SMALL_KERNEL build after changes in rev 1.361
reported by naddy@ ok deraadt@


# 1.361 19-Jan-2019 tedu

Add a pwraction sysctl that controls what the power button does on acpi.
By default, nothing changes -- shutdown is initiated. But allows turning
power button into a sleep button if desired.
(grudging) ok from a few parties


# 1.360 26-Oct-2018 kettenis

Add acpipci(4) on amd64. For now this only calls the PCI-specific _OSC
method to let the ACPI implementation know what features we support.


Revision tags: OPENBSD_6_4_BASE
# 1.359 30-Aug-2018 kettenis

Move softbutton check before the check that skips attaching certain acpi
devices. Fixes regression caused by matching on _CID in addition to
matching on _HID.

ok matthieu@, mlarkin@


# 1.358 25-Aug-2018 kettenis

Match ACPI devices based on _CID if no match for _HID is found.

ok mpi@


# 1.357 19-Aug-2018 kettenis

Add support for multiple PCI segments. Only really implemented for arm64
for now as amd64/i386 firmware still caters for legacy OSes that only
support a single PCI segment.

ok patrick@


# 1.356 03-Aug-2018 kettenis

Let ahci(4) match on _CLS instead of _HID when attaching at acpi(4). Avoids
having to add many more _HID entries to the match table.

ok deraadt@, mlarkin@


# 1.355 10-Jul-2018 kettenis

Make legacy interrupts work in acpipci(4).

ok patrick@


# 1.354 05-Jul-2018 kettenis

Add acpipci(4), a driver that supports generic ECAM-compatible PCI host
bridges based on information provided by ACPI.

ok mlarkin@


# 1.353 04-Jul-2018 kettenis

Properly pass around the PCI "chipset tag" in acpi(4) and refactor
acpimcfg(4) to call an MD initialization functions that sets up a tag for
PCI ECAM.

ok guenther@, mlarkin@, krw@


# 1.352 01-Jul-2018 kettenis

Move acpi(4) attach glue into acpi_machdep.c.

ok guenther@, deraadt@


# 1.351 01-Jul-2018 kettenis

Add support for _DSD Device Properties.

ok mlarkin@


# 1.350 30-Jun-2018 kettenis

Add intr_enable() function, intended for MI use to amd64 and i386 and use
this in the acpi(4) suspend/resume code paths.

ok deraadt@


# 1.349 29-Jun-2018 kettenis

Use <stdint.h> types.

ok mpi@, jung@, krw@, deraadt@


# 1.348 29-Jun-2018 mlarkin

ACPI: Allow (with warning) GPE handler reassignment, instead of returning
a failure code.

ok kettenis


# 1.347 26-Jun-2018 mlarkin

knf


# 1.346 26-Jun-2018 kettenis

Let acpi(4) pass down the bus dma tag.

ok deraadt@


# 1.345 25-Jun-2018 kettenis

Reorganize acpi(4) code a bit in preparation for upcoming arm64 support.

ok deraadt@


# 1.344 20-May-2018 kettenis

Wake up the acpi thread after scheduling a gpio event.

ok mpi@


# 1.343 17-May-2018 kettenis

Sprinkle some #ifndef SMALL_KERNEL such that RAMDISK kernels cuild again.

pointed out by stsp@


# 1.342 17-May-2018 kettenis

Implement GenericSerialBus OpRegion support.

ok mlarkin@


# 1.341 27-Mar-2018 kettenis

Add acpicmos(4), a driver that implements SystemCMOS OperationRegion
access support. This fixes machines where the AML doesn't check whether
support for this OperationRegion type has been registered by the OS.

ok mlarkin@


Revision tags: OPENBSD_6_3_BASE
# 1.340 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.339 10-Feb-2018 deraadt

Syncronize filesystems to disk when suspending. Each mountpoint's vnodes
are pushed to disk. Dangling vnodes (unlinked files still in use) and
vnodes undergoing change by long-running syscalls are identified -- and
such filesystems are marked dirty on-disk while we are suspended (in case
power is lost, a fsck will be required). Filesystems without dangling or
busy vnodes are marked clean, resulting in faster boots following
"battery died" circumstances.
Tested by numerous developers, thanks for the feedback.


# 1.338 08-Feb-2018 deraadt

After flushing disks, try again to release all memory. We don't need to
save dirty memory to the hibernate space.


# 1.337 08-Feb-2018 deraadt

Toss all releaseable memory, because fragmentation can get in the way
of allocating the hibernate playpen.


# 1.336 08-Feb-2018 deraadt

sr_quiesce() is a new approach for ensuring that softraid drains
output to the disks.

This is part of a larger suspend/resume filesystem-safety diff, which
has been worked on for a couple of months already. Tests by job, krw,
beck, benno, and others. Sometimes even by snapshot users...


# 1.335 29-Nov-2017 kettenis

Add support for GPIO-signaled events.

ok mlarkin@


# 1.334 16-Nov-2017 jcs

Add PCI attachment for dwiic(4) needed by Intel 100 Series machines

ok kettenis


# 1.333 14-Nov-2017 kettenis

Add a few more PNP IDs to the skip lists.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.332 17-Aug-2017 stsp

Make dwiic(4) attach its ACPI parent dependencies.
This change makes the Asus E200HA keyboard work.

Original analysis and patch by Cesare Gargano
ok kettenis@


# 1.331 09-Aug-2017 dcoppa

Add "PNP0303" (8042 PS/2 Controller) to acpi_isa_hids[]

ok kettenis@


# 1.330 22-Jul-2017 jcs

acpibat: look for _BIX first before falling back on _BIF

ACPI 4.0 deprecated _BIF for battery status so some newer machines
are exporting _BIX which extends _BIF's package with a few extra
fields. When using _BIX, export the new discharge cycle count as a
sensor.

feedback from kettenis and anton
ok deraadt


# 1.329 20-Jul-2017 kettenis

Start a list of isa(4) device names such that we can avoid printing them
as "not configured".

ok mikeb@, millert@, deraadt@


# 1.328 08-Apr-2017 deraadt

Sensors are run as callbacks inside tasks. During suspend a sensor
could be running inside a driver that will be force-detached, or due
to tsleep end up disrupting the softstate/hardstate contract. At
suspend time, quisce all these callbacks by waiting for completion.
This issue has never been observed for real, but may be implicated in
suspend/resume failures.
ok kettenis guenther mlarkin


# 1.327 08-Apr-2017 deraadt

Two prototypes for acpi_maptable; merge to one place


Revision tags: OPENBSD_6_1_BASE
# 1.326 27-Mar-2017 deraadt

Now that hibernate_alloc() only has clean success/failure, don't
need to call hibernate_free() to clean up a partial mess.
ok mlarkin kettenis


# 1.325 13-Mar-2017 deraadt

allow ramdisks to build; ok jcs


# 1.324 12-Mar-2017 jcs

try to attach acpisbs first and if successful, don't attach acpibat


# 1.323 02-Mar-2017 natano

Add a new sysctl machdep.lidaction. The sysctl works as follows:

machdep.lidaction=0 # do nothing
machdep.lidaction=1 # suspend
machdep.lidaction=2 # hibernate

lidsuspend is just an alias for lidaction, so if you change one, the
other one will have the same value. The plan is to remove
machdep.lidsuspend eventually when people have upgraded their
/ets/sysctl.conf.

discussed with deraadt, who came up with the new MIB name
no objections mlarkin
ok stsp halex jcs


# 1.322 28-Feb-2017 natano

Untangle abstract sleep modes from ACPI sleep states. This paves the way
for further work in the area.

idea and ok deraadt
ok mlarkin


# 1.321 22-Feb-2017 jcs

add acpisbs, an acpi smart battery subsystem driver reading data
over smbus

currently disabled because it conflicts with acpibat

ok deraadt, kettenis


# 1.320 14-Jan-2017 kettenis

Revert aml_rdpciaddr changes; breaks several machines that were working
before.


# 1.319 11-Jan-2017 jsg

Increase the size of the buffers used for _HID and _CID strings to be
able to fit "Hyper_V_Gen_Counter_V1".

ok mlarkin@ deraadt@


# 1.318 08-Jan-2017 kettenis

Make aml_rdpciaddr() work for for devices behind bridges. The code now
uses the mapping between AML nodes and PCI devices that we establish.
Because _INIT methods may end up calling aml_rdpciaddr(), make sure we
create that mapping early. Also handle devices that aren't actually present.
These devices are now included in the mapping and reads will return an
all-ones patterm whereas writes are a no-op..

ok mlarkin@


# 1.317 25-Oct-2016 pirofti

Add the resource index to aml_parse_resource() callback function.

This is needed by an upcoming acpiec commit that handles machines breaking
the current ACPI specifications.

Change suggested by and ok kettenis@, guenther@


# 1.316 18-Sep-2016 guenther

If the APIC table (MADT) isn't present then ci_acpi_proc_id won't be set
when acpicpu tries to attach, leading to a NULL dereference. The ACPI
processor object on the Dells where this was reported is bogus and useless
anyway so apply the same test when deciding whether to attach to processor
objects as acpicpu would when matching them to cpus.

Problem reported by Eivind Eide (xenofil (at) gmail.com) and
Johan Huldtgren (johan+openbsd-bugs (at) huldtgren.com).
Testing by Eivind Eide.
ok kettenis@ mlarkin@


# 1.315 03-Sep-2016 naddy

Write the system time back to the RTC every 30 minutes.
This fixes the problem that long-running machines which were not
shut down properly would reboot with a badly offset system time.

hints and ok kettenis@


# 1.314 31-Aug-2016 mlarkin

Don't put a device in the wake device list if _STA says it's not there.

Fixes at least one machine that woke up from sleep / poweroff immediately.

ok kettenis@, tested by kettenis and martijn@


# 1.313 28-Jul-2016 kettenis

Store the acpi processor ID/UID in struct cpu_info, and use it to attach
acpicpu(4) drivers to the right cpu(4).

ok mlarkin@, guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.312 10-Jun-2016 kettenis

The power management timer has been made optional in ACPI 5.0A, so don't
attempt to attach acpitimer(4) if the timer isn't present.

Pointed out by jcs@

ok jcs@


# 1.311 08-May-2016 kettenis

Make sure devices listed by a device's _DEP method are attached before
we attach the device itself. The _DEP methode, introduced in ACPI 5.0,
is a hint that the device in question depends on OpRegion support from
the devices listed by _DEP, so we have to order them the proper way.
To prevent us from attaching those devices again when we encounter them
later walking down the device tree, keep track of our attempts to
attach a device driver to them using aflag in the aml_node structure.

ok guenther@


# 1.310 25-Apr-2016 pirofti

We attach acpihpet(4) and acpiec(4) without using the HID, so do not
report it in the dmesg.

OK deraadt@, mlarkin@.


# 1.309 02-Apr-2016 kettenis

Reduce dmesg spam by knocking out some acpi hardware IDs that don't need a
driver.

ok mpi@


# 1.308 29-Mar-2016 kettenis

Check if a device is present (using _STA) in acpi_foundhid. This prevents
us from reporting devices as "not configured" that aren't actually present,
and allows us to remove duplicated code from several drivers.

ok deraadt@, ok jsg@


# 1.307 29-Mar-2016 kettenis

Implement a cleaner way to prevent acpivout(4) from attaching if we have
a platform-specific method of controlling the backlight.

ok millert@, mlarkin@, jung@


# 1.306 28-Mar-2016 kettenis

Adding each and every new acpi device driver to acpi_foundhid() is
gettinga bit of the burden. And it really isn't how our autoconf
framework is supposed to work for busses that can be enumerated. All
the drivers already check for a matching _HID in their attach
function. So we can just drop the checks in acpi_foundhid() and let
the drivers sort themselves out.

Print a "not configured" line for devices for which we don't attach adriver.
This may be a bit much as there are devices for which we will not have a
driver. This will be removed if it causes too much dmesg spam.

ok deraadt@, mlarkin@


Revision tags: OPENBSD_5_9_BASE
# 1.305 17-Jan-2016 jsg

Take acpi_foundhid() outside of #ifndef SMALL_KERNEL so the ramdisk
kernel can find and attach dwiic(4) and sdhc(4).


# 1.304 17-Jan-2016 jsg

remove an unused global variable


# 1.303 14-Jan-2016 kettenis

Attach dwiic(4) on the Intel Bay Trail i2c controllers as well.


# 1.302 12-Jan-2016 kettenis

Improve emulation of PM registers on Hardware-redcued ACPI by emulating
SPL_TYPx, SLP_EN and WAK_STS using the new SLEEP_STATUS_REG and
SLEEP_CONTROL_REG registers. Makes the transition into S5 (powerdown) work
on the ASUS X205TA.

ok deraadt@, mlarkin@


# 1.301 12-Jan-2016 jcs

Add dwiic, a driver for the Synopsys DesignWare i2c controller found
on the Samsung ATIV Book 9 laptop. This initial version only
supports ACPI config/attachment.

Add ihidev, a HID-over-i2c driver largely based on uhidev. dwiic
handles attaching ihidev devices found in ACPI.

Add ims, a HID-over-i2c mouse/trackpad driver to get basic cursor
and button functionality from HID-compliant i2c trackpads.

ok kettenis deraadt


# 1.300 11-Jan-2016 kettenis

Make sdhc(4) attach to hardware IDs "80860F14" and "PNP0FFF".


# 1.299 09-Jan-2016 kettenis

Initial support for Hardware-Reduced ACPI. This skips the legacy mode checks
by emulating the PM_CNT register such that SCI_EN is always enabled. It also
skip attaching acpitimer(4) since the relevant registers are no longer there.

ok deraadt@, mlarkin@


# 1.298 06-Jan-2016 kettenis

Follow the spec and check the SCI_EN bit to see if ACPI has already
been enabled and don't bother enabling it again if it is. This should fix
systems that don't implement legacy mode and therefore set ACPI_ENABLE and
ACPI_DISABLE to zero.

ok mlarkin@, yasuoka@


# 1.297 23-Nov-2015 reyk

Back out the CRITICAL change, turn it back from 10% to 25%. Some
people with old/broken batteries didn't like this change, and the
underlying heuristics are wrong.


# 1.296 22-Nov-2015 reyk

The "CRITICAL" APM battery level, that is exported/faked by the ACPI
stack, is currently set to 25%. With 25% battery left, a typical
modern laptop will have at least one hour runtime left. It is an
arbitrary value, and other APM power layers in the kernel set CRITICAL
to something like 5% or 10%, so set it to 10% for now.

OK mlarkin@


# 1.295 28-Sep-2015 deraadt

In low-level suspend routines, set cold=2. In tsleep(), use this to
spit out a ddb trace to console. This should allow us to find suspend
or resume routines which break the rules. It depends on the console
output function being non-sleeping.... but that's another codepath which
should try to be safe when cold is set.
ok kettenis


# 1.294 06-Sep-2015 deraadt

clarify malloc arguments (base + length...)


# 1.293 28-Aug-2015 deraadt

fairly simple sizes for free(); ok tedu


# 1.292 20-Aug-2015 kettenis

Respect the access size when reading or writing to pci config space.
This version also makes sure writes are properly aligned, preventing
the panics seen with the previous incarnation of this change.

Fixes battery status passthrough in vmware, brightness keys on my x220
and who knows what else.

ok mlarkin@, deraadt@


Revision tags: OPENBSD_5_8_BASE
# 1.291 04-Aug-2015 deraadt

If we are setting ACPI_PM1_BM_RLD do it only once, and remember to
repeat this action at resume time.
ok mlarkin guenther


# 1.290 29-Jul-2015 deraadt

Something subtle in previous revision is locking up suspend or booting
on some laptops. Mailing lists threads mention x220 and Asus machines.
Reverting for now.


# 1.289 21-Jul-2015 kettenis

iRespect the access size when reading or writing to pci config space.
Fixes battery status passthrough in vmware and who knows what else.

Tested by many.

ok mlarkin@, deraadt@


# 1.288 17-Jul-2015 mlarkin

fix comments


# 1.287 26-Mar-2015 jsg

Use the 0x10 extended capability to determine if the trackstick
buttons are wired to the trackpad and need to be re-routed to the
trackstick. Without this change the buttons on 2015 thinkpads
get picked up as extended buttons that show up as scroll up/down.

Remove the X1 Carbon 2015 (LEN0048) and X250 (LEN0046) from the
top button area/soft buttons quirks list. Also avoid using the
quirk list entirely if the capability bit is set.

Based on work from jcs@ with feedback from mpi@
ok mpi@ jcs@


# 1.286 20-Mar-2015 kettenis

The ACPI standard says that a PCI function number of FFFF can be used to refer
to all functions on a device. This makes no sense in the context of accessing
PCI config space. Nevertheless stsp@ has a machine with AML that tries to
do config space reads from a device where _ADR encodes function number FFFF.
This is almost certainly buggy AML, but since Linux (and presumably Windows)
boot just fine on this machine we need to work around this. Linux effectively
treats function FFFF as function 7, which isn't present on this machine.
Instead we simulate a read from a non-existent device by returning a bit
pattern of all ones. Writes to function FFFF will still panic.

ok mlarkin@


# 1.285 16-Mar-2015 deraadt

Reduce use of SMALL_KERNEL in acpi, bringing acpiec driver and more gpe
handling into RAMDISK. This is now possible because the install media
has ample room. The goal is to reduce special cases where we may be
skipping (unknown) important operations...
ok mlarkin kettenis


# 1.284 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.283 07-Feb-2015 deraadt

New framework that allows hibernate to pass in entropy from it's fresh
boot.
ok mlarkin


# 1.282 06-Feb-2015 mlarkin

Fix a hibernate crash on some machines due to unmapping a page that
may not have been mapped previously (in the failure to hibernate case).

Also ensure that the lowmem ptp is mapped in all cases (not just MP).

ok kettenis


# 1.281 17-Jan-2015 deraadt

resume_randomness() before spinning up other cpus...


# 1.280 15-Jan-2015 jsg

Define a new wscons mouse type for Synaptics clickpad devices that lack
physical buttons. This will be used if the acpi pnpid for the mouse
matches a list provided by Synaptics found in Linux.

Combined with the xenocara changes this will remove the need for an xorg
config file for the x240/t440/t540 generation of thinkpads.

ok matthieu@ shadchin@ kettenis@


# 1.279 11-Jan-2015 kettenis

Tolerate checksum errors in ACPI tables (but complain about them). Apparently
there are machines out there with broken BIOSen with such checksum errors in
thier RSDT tables. Windows runs fine on these machines, so it probably does
not check. Linux ignores checksum errors as well (and prints a warning).

ok deraadt@, krw@


# 1.278 18-Dec-2014 deraadt

suspend and resume the RNG (at what looks like the right spots)
ok reyk djm


# 1.277 09-Dec-2014 doug

Replace some malloc(n*size,...) calls with mallocarray().

ok tedu@ deraadt@


# 1.276 06-Dec-2014 mpi

If "machdep.lidsuspend" is not set, do not go back to sleep if the lid
is still closed when resuming. This allows docked laptop to be resumed
without opening them.

ok mlarkin@


# 1.275 23-Nov-2014 mlarkin

If a machine has no _LIDs, then none can be open. This diff fixes a problem
where machines without any _LIDs were going back to sleep after hibernate
due to the recent suspend-if-lid-closed-on-resume diff. Noticed on qemu,
which doesn't have any _LIDs.

discussed with deraadt


# 1.274 18-Nov-2014 krw

Nuke yet more obvious #include duplications.

ok miod@


# 1.273 08-Nov-2014 mlarkin

If resuming from sleep (zzz/ZZZ) and the lid is still closed, go back to
sleep. This prevents accidental lid flex or slight opening in a backpack
from waking the machine up and leaving it resumed (powered on).

ok deraadt@


# 1.272 26-Sep-2014 kettenis

Rework piglet and pig allocation. Currently the piglet gets allocated
deep down in the suspend path, where it is really hard to recover from
allocation failure. So allocate the piglet early on in the suspend path.
Also change the piglet and piglet allocation functions to use km_alloc(9)
instead of doing pmemrange magic. This removes a bunch of code which, in the
case of the piglet allocation, is broken since it results in a NULL pointer
dereference. Also switch the piglet allocation to not wait. If we can't
allocate 16MB of phys contig memory on a halfway modern machine we're almost
certainly under a lot of memory pressure and we're better off not trying to
hibernate anyway.

ok mlarkin@


# 1.271 25-Sep-2014 kettenis

Bring back rev 1.249. Now that mp_setperf() has been fixed, it should be
safe to restore perflevel when resuming. Should fix the issues people
see with the processor running at full speed heating up the machine while
the apm command reports that the CPUs are running at a low speed.

ok dcoppa@, deraadt@


# 1.270 23-Sep-2014 kettenis

Release the acpi lock around calls to wsdisplay_suspend() and
wsdisplay_resume(). The X server may adjust the display brightness just
before/after the VT switch when suspending/resuming. In fact, it may
already be blocked adjusting the display brightness for some reason
unrelated to the suspend request.

The VT switch request will eventually time out, but the delay is annoying
and the failed VT switch will lead to problems after resume.

Needed for a future update of the xf86-video-intel driver.

ok mlarkin@


# 1.269 19-Sep-2014 kettenis

Use config_suspend_all(9).


# 1.268 08-Sep-2014 kettenis

Clear the PME Status bit on PCI devices in response to Device Wake events.
Fixes the interrupt storm on the 2009 Mac mini with WOL enabled on its
nfe(4) interface.

ok mlarkin@


Revision tags: OPENBSD_5_6_BASE
# 1.267 20-Jul-2014 mlarkin

Support hibernating to softraid crypto volumes.

much help and ok from deraadt@


# 1.266 16-Jul-2014 mlarkin

Reenable hibernate RLE support and flush+zero all memory after unpack.


# 1.265 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.264 12-Jul-2014 kettenis

Add a function to drop all clean pages on the page daemon queues and call
it when we hibernate.

ok mlarkin@, miod@, deraadt@


# 1.263 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.262 11-Jul-2014 mlarkin

Flush the buffercache to 16MB on hibernate and restore its previous max
size (kern.bufcachepercent) on resume, for better hibernate performance.

ok beck@


# 1.261 10-Jul-2014 blambert

remove unneeded header

ok mlarkin@


# 1.260 01-Jul-2014 kettenis

Adding an additonal delay after each access to SystemMemory or SystemIO
during autoconf makes no sense.

ok deraadt@


# 1.259 23-Jun-2014 kettenis

SystemMemory is used to access memory mapped registers on some machines, so we
must use the bus_space(9) API to access it instead of memcpy(9). Also make
sure we properly align access to fields that need more than byte alignment.

ok guenther@


# 1.258 21-May-2014 mlarkin

Fix invalid / missing format specifiers in several dnprintf statements
(used only when ACPI_DEBUG is enabled)

From a diff on tech@ by Fabian Raetz, with a minor change by me, tested
on i386 and amd64 with ACPI_DEBUG enabled and disabled.


# 1.257 25-Apr-2014 mlarkin

Defer the free of the hibernate structures until later, when it is safe to
do so.

Freeing the hibernate structures too early leads to inadvertent calls into
splx when we aren't ready for them yet. Found when trapping splx calls in
the resume path.

discussed with deraadt@ at m2k14


# 1.256 13-Apr-2014 mlarkin

Fix a badly worded debug message that implied the entire machine was going
to sleep, instead of just the acpi thread going to sleep.


# 1.255 31-Mar-2014 mpi

Change the order of operations during a suspend/resume cycle and
call bufq_quiesce() after executing the DVACT_QUIESCE handlers.

This should be safe since no disk nor controller drivers have such
handler but it will allow us to detach sd(4) devices attached to a
USB bus.

Another benefit pointed out by kettenis@ is that drivers that need
to read a firmware from the disk should be able to do it at resume
time in a DVACT_WAKEUP handler.

ok kettenis@, deraadt@


# 1.254 13-Mar-2014 dlg

get rid of the assumption that the head of the alldevs list is the
"mainbus" device. this breaks when mpath is enabled because it
attaches before mainbus and therefore takes the head position.

have autoconf provide device_mainbus() which looks up mainbus_cd,
and use that instead.

discussed with deraadt who just wants mpath stuff to move forward
despite there being many ways to shine this particular turd.


Revision tags: OPENBSD_5_5_BASE
# 1.253 21-Feb-2014 deraadt

Do the lid control further on in the resume sequence, since this is
AML, and will do WAITOK operations. While cold, that is a bad idea.
We seem to be safe with respect to the spec, I do not see any event
debounce issues here.
ok kettenis, tested by many


# 1.252 20-Jan-2014 claudio

Instead of checking sc->sc_revision <= 1 check that the
fadt->hdr_revision <= 1. This is a better check since it checks
the version of the proper descriptor. This allows the alix APU
to reboot since those boards have currently a wrong sc->sc_revision.
Committing this now so that it can be tested easier.
With and OK jsg@ also fine by derradt@


# 1.251 24-Dec-2013 kettenis

If the FADT has its SMI_CMD set to zero, assume we're only ACPI-only hardware
and don't need to disable SMI ownership of the ACPI hardware registers.

ok mlarkin@


# 1.250 23-Dec-2013 kettenis

Revert previous commit. It makes resume hang on some MP systems running
GENERIC.MP.


# 1.249 16-Dec-2013 kettenis

Restore hw.setperf upon resume like we do for apm(4) on i386. Seems we need
to do this fairly late in the resume to avoid hangs.

ok deraadt@, mlarkin@


# 1.248 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


# 1.247 06-Nov-2013 mpi

Add support for Power Resources for Dx states and the necessary hook
for PCI devices. This hook should be called twice, before and after
changing the power state of a PCI device.

Before setting the device to the new state, the ACPI layer will notify
every power resources linked to the device for that state and make sure
they are turned "_ON". After changing the state of the device, it will
decrement the reference of every power resources linked to that device
for the old state and turn them "_OFF" if they are no longer referenced.

This fixes the no-USB after resume problem seen on various ThinkPad,
problem initialy diagnosed with Alexander Polakov.

ok kettenis@, deraadt@


Revision tags: OPENBSD_5_4_BASE
# 1.246 01-Jun-2013 mlarkin

Stop printing ACPI wakeup devices in dmesg after the 16th wakeup device.

Helps with VMware VMs that report hundreds of wakeup devices.

ok reyk@, deraadt@


# 1.245 31-May-2013 bluhm

Fix the build for a kernel without wd and pciide in its config.
OK deraadt@ tedu@


# 1.244 30-May-2013 deraadt

Enforce ca_activate tree-walks over the entire heirarchy for all events,
cleaning up some shutdown-hook related code on the way.
(A few drivers related to sparc are still skipped at kettenis' request)
ok kettenis mlarkin, tested by many others too


# 1.243 18-Apr-2013 deraadt

pin the acpi thread to the BSP, because AML and SMI cannot be trusted. I
was certain we were doing this already, but it turns out no...


Revision tags: OPENBSD_5_3_BASE
# 1.242 24-Dec-2012 guenther

In acpiioctl(), Return though the splx() path if suser() returns an error

ok deraadt@


# 1.241 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


# 1.240 04-Oct-2012 ehrhardt

Use information provided by ACPI to attach secondary PCI host bridges.

ok kettenis@


# 1.239 07-Sep-2012 kettenis

Add a new function called acpi_pci_min_powerstate() that returns the lowest
power state for a PCI device for the power state (currently S3 or S4) we're
transitioning into. This function is probably too simplistic; there is a lot
of (silly) complexity in the ACPI spec.

ok deraadt@


Revision tags: OPENBSD_5_2_BASE
# 1.238 13-Jul-2012 pirofti

Disable acpivideo(4) on ASUS laptops.

There are some models that panic whenever an event is received.
Some cope, some don't. In order to get this right acpiec(4) requires
major rework. Until then let machines boot.

Discussed with deraadt@.


# 1.237 11-Jul-2012 mlarkin

for now, restrict hibernate to root in the acpi ioctl kernel interface.
normal rules for controlling this interface through apmd still apply.

ok deraadt@


# 1.236 09-Jul-2012 deraadt

tiny little oops in untested hibernate block


# 1.235 09-Jul-2012 deraadt

clean up ioctl() case statements


# 1.234 09-Jul-2012 deraadt

acpi_disable_onegpe() is not used; ok pirofti


# 1.233 24-May-2012 kettenis

If _MIN > _MAX consider a WordBus resource to be invalid. Fixes a panic on
the IBM x3550-M4.

ok mikeb@


# 1.232 29-Mar-2012 mlarkin

Missing #ifdef in previous diff


# 1.231 29-Mar-2012 mlarkin

Prevent hibernation attempts on machines with unsupported disk controllers.
The same check is also performed later in the hibernate sequence, but by
then it's too late to cleanly unwind (presently).

ok deraadt


# 1.230 26-Mar-2012 deraadt

simplify conditionals in acpi_sleep_state()
ok mlarkin


# 1.229 26-Mar-2012 deraadt

hook in the hibernate request code; half of this diff is from mlarkin
ok mlarkin


Revision tags: OPENBSD_5_1_BASE
# 1.228 20-Sep-2011 deraadt

Very early in suspend while the clock is still ticking realtime,
save the clock back to the rtc. The zaurus already did this.
ok phessler miod kettenis


Revision tags: OPENBSD_5_0_BASE
# 1.227 02-Jul-2011 nicm

kqueue attach functions should return an errno or 0, not a plain 1. Fix
the obvious cases to return EINVAL and ENXIO.

ok tedu deraadt


# 1.226 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.225 16-Jun-2011 pirofti

Import acpitoshiba driver.

This is originally written by Hiroyuki Aizu, ported and integrated
in our tree by Javier Vazquez <javama4 AT gmail> with tweaks and
suggestions by me.

Import prodded by deraadt@ and the lack of testers on tech@.


# 1.224 27-Apr-2011 jcs

attach acpithinkpad to newer lenovo models like the x120e

ok deraadt@


# 1.223 22-Apr-2011 jordan

Fix uninitialzied variables and formatting strings (-Wxxx errors)


Revision tags: OPENBSD_4_9_BASE
# 1.222 02-Jan-2011 jordan

Re-add code for ACPI taskqueue, eliminates need for state vars in acpi softc
Serialize processing ACPI tasks for sleep/powerdown/GPE event
ok deraadt@


# 1.221 31-Oct-2010 guenther

Revert last commit: it breaks resume on Thinkpads
"then please back it out" deraadt@


# 1.220 26-Oct-2010 jordan

Add task queue for ACPI gpe and notify handlers
This fixes eject on Dell Latitude dock and an issue on the Dell Mini
battery update.
ok deraadt


# 1.219 07-Oct-2010 jordan

Create separate functions for enabling wake and runtime gpes.
Remove old suspend/resume gpewalk function
ok deraadt, mlarkin


# 1.218 05-Oct-2010 jordan

split acpi_enable_onegpe to separate enable/disable functions
ok deraadt


# 1.217 05-Oct-2010 jordan

Added acpi_disable_allgpes function to clear GPE status
ok deraadt


# 1.216 05-Oct-2010 deraadt

clean up acpi_softc use more


# 1.215 05-Oct-2010 deraadt

delete a bit of #if 0 code


# 1.214 05-Oct-2010 deraadt

hide use of our global acpi_softc pointer a bit better


# 1.213 29-Sep-2010 deraadt

grab the acpi thread lock very early in acpi thread startup; not that it
is likely that anyone will race us... but why be careless.
ok kettenis


# 1.212 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.211 29-Aug-2010 jasper

- add a check for FWRITE here too.

ok miod@


# 1.210 27-Aug-2010 kettenis

Make PCI Power Management optional, and only enable it when acpi(4) attaches.

ok deraadt@


Revision tags: OPENBSD_4_8_BASE
# 1.209 08-Aug-2010 kettenis

Close race between the acpi thread and wscons ioctls running in process
context. We might rip this out post-release and replace it with a solution
that serializes things in a more obvious way.

ok deraadt@, miod@ and "those crazy texans who ok anything"


# 1.208 08-Aug-2010 deraadt

Report the acpi sleep states when on a RAMDISK; ok krw


# 1.207 07-Aug-2010 kettenis

Enforce acpi register access rescrictions.

ok mlarkin@, deraadt@, tested by marco@


# 1.206 07-Aug-2010 deraadt

upon resume, notify all the acpiac and acpibat drivers to update
their status. some laptops do not do this automatically.
ok kettenis canacar


# 1.205 06-Aug-2010 marco

Always call _PSW on _LID devices that support it to enable lid open to
resume the machine. Conversely disable it when the machine wakes up.

Tested by several ok deraadt


# 1.204 06-Aug-2010 deraadt

As part of a revamp of the PM1/GPE code, I write this diff to look for
unmanaged EN & STS bits in the PM1 register at interrupt time and report
them. As a side effect this splits the STS acknowledgement into two writes
(for power, and sleep) instead of one. The printf that is added (to spot
unmanaged STS bits) has not yet been triggered as far as we know.

Before the "write to PM1 registers at the right offset" diff went in, this
was not neccessary. But newer thinkpads do not have a working soft power
button without this diff.

We have no idea why.
ok mlarkin kettenis


# 1.203 05-Aug-2010 deraadt

PM1 power button events were generating 'fake events' to acpibtn PWRB,
but if there is no such device these were being tossed. Instead, arrange
for all power button handlers to flag a softc variable which is processed
by the thread; so that one place will know to request a powerdown (psignal
to init)
ok marco mlarkin


# 1.202 05-Aug-2010 deraadt

rename a variable which is annoyingly poorly named


# 1.201 05-Aug-2010 mlarkin

Write to the correct offset for various ACPI pmreg registers. Writing to
the correct register does properly clear the wake status bits and fixed
event bits, which allows some machines to have more than 1 suspend cycle.

bug spotted by deraadt@

tested by deraadt@ and myself on a half-dozen machines and others on
hackers@

ok deraadt@, marco@


# 1.200 05-Aug-2010 deraadt

Fix the power button. The pm1 register is 16 bits in size.
My mistake, a while back.
tested by krw


# 1.199 04-Aug-2010 kettenis

Print supported sleep states.

ok marco@, phessler@, deraadt@


# 1.198 03-Aug-2010 mlarkin

Use the proper flag for re-enabling certain hardware events (power btn,
sleep btn). The (incorrect) flag prevoiously used coincidentally had the
same value, so this shouldn't cause any different behavior than before.

tested on a variety of machines (i386, amd64, sp, mp) - no behavioral
change seen

ok deraadt@


# 1.197 28-Jul-2010 marco

Make all gasio failures look and behave the same.


# 1.196 28-Jul-2010 mlarkin

Delay a little longer on suspend before giving up. Fixes machines that
sometimes don't suspend because the sleep transition takes too long
(symptoms like failing to sleep but the backlight stays on).

ok deraadt@


# 1.195 27-Jul-2010 deraadt

remove XXX; EC can now be attached early when needed; ok jordan


# 1.194 27-Jul-2010 deraadt

more brackets feels good


# 1.193 27-Jul-2010 deraadt

XXX annotate a block in acpi_enter_sleep_state() that is pretty worrying


# 1.192 27-Jul-2010 deraadt

horrific indentation that nearly made me puke


# 1.191 27-Jul-2010 mlarkin

Set the sleeping indicator light on machines that support it, in case SMI
doesn't do it for us. Shows the 'moon' LED on older Thinkpads when
sleeping.

Tested on T43p, W500, T510 by me and on X61s by deraadt@.

ok deraadt@


# 1.190 27-Jul-2010 jordan

Early initialization of acpiec if ECDT table exists
Fixes hang when booting thinkpads while docked
ok deraadt


# 1.189 26-Jul-2010 deraadt

remove merge error which was commited


# 1.188 26-Jul-2010 pirofti

Add support for Sony ACPI hotkeys via a new driver: acpisony(4).

Currently it only works for the suspend button and tries to do right for
the brightness events, but I haven't found a Sony laptop that like to do
right so far.

In the future I want to make the brightness keys work on all Sony's and
also add support for the zoom hotkeys and whatever other funky keys I can
find on those things.

Okay deraadt@.


# 1.187 22-Jul-2010 deraadt

pretty large cleanup of SMALL_KERNEL


# 1.186 21-Jul-2010 deraadt

in the acpi GPE handler, late ack level interrupts not edge interrupts.
Duh.


# 1.185 21-Jul-2010 deraadt

Merge the edge and level gpe handlers into one (now possible since the
gpe structure tells us whether the gpe is edge or level)


# 1.184 21-Jul-2010 deraadt

delete #if 0 code we are not going to use; ok marco


# 1.183 20-Jul-2010 deraadt

make the mask code for acpi_enable_onegpe() match other similar code


# 1.182 20-Jul-2010 deraadt

Handle the acpi interrupt controller with proper edge/level handling,
wrapped in spltty to avoid racing against the interrupt controller.
Repair the gpe bit masking code while there, and do operations in the
right order
ok kettenis mlarkin, help from jordan at figuring out the order of
operations.


# 1.181 20-Jul-2010 matthew

When we're preparing to go to sleep, only call bufq_restart() if other
preparations failed.

ok kettenis@, thib@, deraadt@


# 1.180 19-Jul-2010 deraadt

permit compilation with ACPI_DEBUG


# 1.179 19-Jul-2010 deraadt

there is no need for a seperate dsdt_softc pointer; dsdt does not have a
softc but is just a part of the single acpi driver.
ok jordan


# 1.178 19-Jul-2010 deraadt

acpi_delay() is no longer needed; ok jordan


# 1.177 18-Jul-2010 mlarkin

Clear event status on resume, to avoid going back to sleep/reboot/shutdown
immediately after resume. Tested by a bunch of people on hackers@ on
various hardware. Fixes at least Toshiba NB200 and Gateway LT3103u.

ok marco


# 1.176 13-Jul-2010 deraadt

rename some thread related interfaces so they are logical


# 1.175 10-Jul-2010 jordan

Verify _ADR bdf is correct (some systems return 0xffff)
Works on Dell x9xx and Studio 1555


# 1.174 09-Jul-2010 jordan

Clear the noise


# 1.173 08-Jul-2010 jordan

Add mapping for ACPI device to PCI bdf (match autoconf tree)
Simplify resource parsing function to use buffer argument
Convert namespace linked lists to use queue macros
ok marco@, deraadt@


# 1.172 06-Jul-2010 deraadt

Use spltty() to lock downcalls from apm against the information being
modified by the acpi thread.
ok kettenis


# 1.171 06-Jul-2010 deraadt

Don't use a workq to do the suspend, because it races aginst the acpi
thread. Instead, just tell the acpi thread to do the suspend for us.
This makes apmd & zzz work correctly. While here, have acpithinkpad
attempt to post the event to apm before prompting the suspend itself.
ok kettenis marco mlarkin


# 1.170 05-Jul-2010 mlarkin

Reset register SCI_EN on resume. The ACPI spec clearly states we don't have
to do this, but many machines don't, or forget to, which leads to all sorts
of problems on resume (broken battery stats, overheating, failure to resume
more than once, etc).

tested by many, ok marco@, deraadt@


# 1.169 01-Jul-2010 thib

do bufq quiesce again, since vnd/the bufq_done call in
biodone has been fixed.


# 1.168 01-Jul-2010 jordan

Backout recent AML changes, breaks on R210 and others


# 1.167 01-Jul-2010 jordan

Simplified aml_parse_resource call, cleanup code
ok marco


# 1.166 01-Jul-2010 jordan

Fix subordinate bus # for multi-root PCI buses
ok marco, kettenis


# 1.165 30-Jun-2010 thib

Disable/partially backout the bufq quiesce changes as this
is causing havoc with vnds and release must be buildable.


# 1.164 29-Jun-2010 deraadt

Fix ramdisks (new code was placed inside a SMALL_KERNEL chunk)


# 1.163 29-Jun-2010 jordan

Add support for mapping ACPI to PCI devices
ok kettenis, deraadt


# 1.162 29-Jun-2010 kettenis

Call bufq_quiesce() on suspend to stop all I/O and bufq_restart on resume.
Makes suspend/resume work much better while doing disk I/O.

ok mlarkin@


# 1.161 29-Jun-2010 tedu

60 is a more reasonable guess for number of minutes in an hour than 100.
spotted by TeXitoi. ok deraadt


# 1.160 27-Jun-2010 jordan

Add _acpi_matchhids routine, precursor to pci device match


# 1.159 27-Jun-2010 mlarkin

Clear ACPI fixed event status on resume (power buttons/etc) so that some
machines don't immediately resume after suspending to S3.

Tested by kettenis@ and I on 5 or 6 machines, deraadt@ "yeah, looks good"


# 1.158 27-Jun-2010 jordan

Cleaned up acpi_maptable, adds to table list directly


# 1.157 07-Apr-2010 deraadt

If we have done a wsdisplay_suspend(), and ACPI decides it does not want to
suspend, we must roll back by calling wsdisplay_resume() or the virtual
console switching will be locked forever
ok oga


# 1.156 07-Apr-2010 kettenis

Make suspend/resume work on MP machines (running an MP kernel). Joint work
from mlarkin@ and me, with some amd64 fixes thrown in by deraadt@

ok marco@, deraadt@, pirofti@, mlarkin@


# 1.155 31-Mar-2010 kettenis

Bring /dev/apm support in line with apm(4). Make the suspend button send
a suspend request event to apmd(8) instead of suspending immediately. Also
keep track of whether /dev/apm and /dev/apmctl are currently open, such that
we can still suspend immediately if apmd(8) isn't running.

ok deraadt@, marco@, pirofti@, jsing@, oga@


# 1.154 30-Mar-2010 oga

Prevent the apmd/x races for good.

When we hit suspend time, go through all wsdisplays on the system. if
they are in mode MAPPED, but not MODE_DUMBFB then if possible do a full
vt switch to a !mapped vt, and prevent switching back until resume time.
This has to be called from MD code because this involves userland
running so that X can run the vt switch signal handler. This way, any
case where we are using the "poke registers from userland" model, we
will not be on the hardware when we go down, so the kernel can actually
handle thing properly.

Tested on several acpi laptops (by kettenis@ and ian@), x40 (me and
beck@ at LEAST) and zaurus (me). Maybe others, but if so I forgot who at
this time..

Idea from deraadt somewhere over the Faroe Islands (I thought of a
similar thing myself a while ago). Much prompting from him. Ok and
comments miod@


# 1.153 25-Mar-2010 oga

Fix & vs. && in the kqfilter code.

Based on a comparison with the apm code.

ok deraadt@, kettenis@


Revision tags: OPENBSD_4_7_BASE
# 1.152 26-Nov-2009 mlarkin

Visual (and on some machines, audio) feedback while the machine is
resuming via calls to aml _SST method. This also makes the "moon" LED on
thinkpads to blink during resume and cease when resume is completed.

ok pirofti@


# 1.151 26-Nov-2009 deraadt

Toggle into "cold" when we we disable interrupts, because tsleeping
in the AML interpreter and context switching into userland processes
tends to sometimes pop the ACPI trampoline page out of our address
space. Makes lots more machines work.
ok kettenis mlarkin


# 1.150 24-Nov-2009 jsg

in the resume path evaluate _WAK before dealing with device drivers
like the spec says, not after.

ok mlarkin@


# 1.149 24-Nov-2009 deraadt

abort suspends on SMP systems until that gets written; ok mlarkin


# 1.148 23-Nov-2009 mlarkin

pci should return the result of it's children's suspend/resume calls back
to its parent.

handle suspend failure case and unwind if devices have an activate function
that returns failure.

ok deraadt@, kettenis@


# 1.147 23-Nov-2009 mlarkin

acpi* devices need to call their children on suspend/resume.
ok deraadt@


# 1.146 23-Nov-2009 pirofti

Remove ACPI_SLEEP_ENABLED checks.

This enables by default the suspend/resume paths in the kernel.

Okay deraadt@.


# 1.145 23-Nov-2009 deraadt

knf and spacing that are super annoying


# 1.144 23-Nov-2009 mlarkin

Hook up Theo's new children suspend/resume function, as well as more ACPI
suspend/resume glue.

ok deraadt@


# 1.143 26-Oct-2009 deraadt

Do not do strncmp comparisons on dv_xname because longer device names which
look similar could arrive in the future. Instead, compare directly against
dv_cfdata->cf_driver->cd_name
Issue originally spotted by miod


# 1.142 26-Oct-2009 jordan

Verify checksum+address when loading ACPI tables.
Some systems had invalid entries in RSDT/XSDT.
ok marco@


# 1.141 23-Jul-2009 cnst

New aibs(4) driver for ASUSTeK AI Booster (ACPI ATK0110) hardware monitoring,
with sensor state support through limits provided by the ACPI.

Tested on several ASUS motherboards kindly networked by Sam Fourman Jr.

ok deraadt marco jordan


Revision tags: OPENBSD_4_6_BASE
# 1.140 03-Jun-2009 pirofti

Import acpi power resource driver. This is needed for suspend/resume logic.

Okay jordan@ and marco@.


# 1.139 03-Jun-2009 pirofti

Add acpivideo support. This adds brightness support for all laptops
except thinkpads, they will use the acpithinkpad driver. The driver is
also hooked into wsconsole. So brightness can be adjusted via:

$ wsconsctl display.brightness=<percentage>

This is very helpfull on some laptops that have a nasty bios and get two
steps instead of one when pressing the brightness button.

Tested on various dell, fujitsu, acer, samsung and other laptops.

Okay marco@, miod@. Suggestions from kettenis@.
Lots of reviews and help from miod@, thanks!


# 1.138 03-Jun-2009 jordan

Added ejectable bay for IDE cdroms
ok marco@


# 1.137 30-Apr-2009 marco

Oga changed apm ioctls and forgot to grep. Temporary work around by hitting
the sleep button when we get any request. This interface will chnage at
some point.
ok oga


# 1.136 19-Apr-2009 krw

Call acpi_sleep_walk() when powering down just like when entering
any other sleep state. This tells acpi wake devices to not wake up
the box until manual powering up resets their status. Makes my Dell
GX520 stay powered off in response to 'halt -p'.

In snaps for a while.

ok marco@ (several times) deraadt@


# 1.135 17-Apr-2009 pirofti

Add device ranking support for future sleep/wake developments.

okay marco@


# 1.134 11-Mar-2009 jordan

Cleanup.. use aml_evalinteger instead of aml_evalname
ok marco@


# 1.133 10-Mar-2009 jordan

Fix cut/paste error.. call _BFS not _PTS
ok marco@


Revision tags: OPENBSD_4_5_BASE
# 1.132 21-Feb-2009 marco

overzealous cut/paste; found by Vladimir Kirillov <proger@uaoug.org.ua>


# 1.131 20-Feb-2009 marco

Oops didn't mean for Debugger() to remain there.


# 1.130 19-Feb-2009 marco

suspend/resume bits so that we can develop this in tree. This is disabled.
code from mlarkin and me
help from art,toby,jordan and several others
ok jordan, go for it deraadt


# 1.129 10-Feb-2009 jordan

Fix null pointer for shutdown if acpi not enabled
ok marco@


# 1.128 04-Feb-2009 kettenis

Some systems that claim to be ACPI 2.0 apparently don't provide an XSDT
address. Make the code fall back on the RSDT on those systems.

From Joerg Niendorf <p3y80@internode.on.net>. Fixes PR 5986.


# 1.127 17-Jan-2009 krw

A couple of stray malloc/memset -> malloc(M_ZERO), a bit of knf and
lose a couple of unnecessary casts. No functional change.

Feedback from kettenis@

ok kettenis@ marco@


# 1.126 06-Nov-2008 marco

Collapse all sanity checks of hids into one function.

Originaly from jordan


Revision tags: OPENBSD_4_4_BASE
# 1.125 02-Jul-2008 fgsch

add acpivideo. for now almost an empty skel so others can work on
suspend/resume. eventually it will also manage output switching and
brightness where supported. prodded by marco@


# 1.124 11-Jun-2008 marco

Fix $OpenBSD$ for once and for all


# 1.123 08-Jun-2008 deraadt

aaa_dev was being set from an uninitialized variable; ok toby


# 1.122 07-Jun-2008 marco

Kill ACPIVERBOSE. It was never used and is worthless to boot.

comments jmc
ok toby


# 1.121 06-Jun-2008 marco

Shrink acpi some more for floppies and while here re-enable acpi on the
floppies. Fixes crazy sony vaio kettenis has.

tested by deraadt
ok deraadt jordan kettenis


# 1.120 01-Jun-2008 marco

Fix \n nit in SMALL_KERNEL.


# 1.119 01-Jun-2008 marco

Re-enable new parser. Lots of testing by various folks. Fits all media
now too.

ok kettenis deraadt


# 1.118 16-May-2008 dlg

back out jordans new parser.

it didnt have enough oks from the right people, it did break machines
that were previously working, and it bloated the RAMDISKS and install
media beyond the constraints of the media.

requested by deraadt@


# 1.117 14-May-2008 jordan

Adding new ACPI Parser code
Old guts of code still exists, needs to be torn out next
ok marco@
ok brad@


# 1.116 27-Apr-2008 jcs

add acpithinkpad(4), an acpi support driver for ibm/lenovo
thinkpads.

provides functionality that the bios did on older, non-acpi
thinkpads like respond to screen brightness keys and toggle the
bluetooth device on and off.

ok deraadt@ and marco@


# 1.115 24-Apr-2008 jsing

Introduce acpiasus(4), a driver for the ACPI based hotkeys found in many
ASUS laptops (including the ASUS EeePC) - largely based on NetBSD's
asus(4) driver. On the ASUS EeePC this allows us to enable/disable
wireless, change screen brightness and use the volume keys.

ok jsg@, weingart@


# 1.114 12-Apr-2008 kettenis

Get rid of acpi_s5 global variables; simply send SIGUSR2 instead of SUGUSR1
to tell init(8) to power down the machine.

ok krw@


Revision tags: OPENBSD_4_3_BASE
# 1.113 21-Jan-2008 jsg

Check against RESET_REG_SUP in FADT before doing the acpi reset dance
after all, not doing so appears to break more machines than it fixes.

ok marco@


# 1.112 05-Jan-2008 kettenis

Never attach more acpicpu(4) devices than cpu(4) devices. Always attach
the first acpicpu(4) device on non-MULTIPROCESSOR kernels, regardless of its
ACPI processor ID.

ok mikeb@


# 1.111 05-Dec-2007 deraadt

Hang acpi(4) below bios(4) instead of mainbus(4). This lets us move acpi(4)
before pcibios(4), and then.... if acpi(4) attaches, skip pcibios(4) since
it messes the machines up. Fixes claudio's HP dl320 G5, and almost assuredly
others.
ok kettenis


# 1.110 05-Dec-2007 deraadt

repair acpiopen/acpiclose and friends to handle the /dev/apmctl interface,
so that apmd(8) can actually work correctly against acpi(4), and thus,
even do the fancy -C/-A stuff; also tested by jmc


# 1.109 02-Dec-2007 fgsch

fix the trailing space for real now.


# 1.108 02-Dec-2007 fgsch

avoid trailing spaces on output; marco@ ok.


# 1.107 27-Nov-2007 kettenis

Fix off-by-one that made us attach acpicpu0 and acpicpu1 on GENERIC kernels.

ok marco@


# 1.106 25-Nov-2007 jsg

When available and ACPI is enabled use the ACPI reset method
to reboot. This is required for some quirky sis machines
and likely others to reboot.

Tested by many. 'commit early commit often' deraadt@


# 1.105 19-Nov-2007 deraadt

sigh, amd64 still uses that stupid acpi_enabled variable...


# 1.104 19-Nov-2007 deraadt

softc's come initialized to 0, you know...


# 1.103 19-Nov-2007 deraadt

store revision; from jsg


# 1.102 19-Nov-2007 kettenis

Initialize sc_facs earlier.

ok deraadt@, beck@, canacar@


# 1.101 16-Nov-2007 deraadt

fix the bus_space #define nightmare, so that amd64 and i386 are much more
uniform. as a result shared code like acpi needs less #ifdef's
ok marco kettenis


# 1.100 15-Nov-2007 deraadt

remove the #ifdef ACPI_ENABLE. the EC codepath is either safe enough (and
will be improved), or it is unsafe because AML assumes it must be there
ok almost everyone


# 1.99 15-Nov-2007 mikeb

Make acpicpu(4) attach to the existing CPUs only.

Problem reported by form@. Fix idea by kettenis@.
Lots of help from deraadt@. Tested by deraadt@, form@ and me.

ok deraadt marco


# 1.98 14-Nov-2007 deraadt

spacing


# 1.97 12-Nov-2007 deraadt

correct : vs , in attach-time messages


# 1.96 06-Nov-2007 deraadt

all ACPI_ENABLE kernels need acpi_interrupt()


# 1.95 06-Nov-2007 deraadt

move acpi_read_pmreg() and acpi_write_pmreg() out of SMALL_KERNEL so
that acpi ramdisks can compile (obvious solution)


# 1.94 05-Nov-2007 canacar

Establish the acpi interrupt earlier. Fixes problems with some
GENERIC.MP kernels not getting acpi interrupts. This is a
workaround until the real issue with late establishment is fixed.
ok weingart@, beck@, gwk@


# 1.93 03-Nov-2007 jordan

Added support for displaying wakeup devices
ok beck@,weingart@,gwk@


# 1.92 11-Oct-2007 kettenis

When walking the tree yo find _PRT methods, check whether the device is
actually enabled and functioning. Similar to what we do for _INI.

ok weingart@, beck@


# 1.91 08-Oct-2007 krw

More simple memset(,0,) -> M_ZERO changes. In this batch move to
size(*p) as the first malloc() parameter where p is declared locally
and thus easy to check. Add M_ZERO to gpe_table allocation in acpi.c
even though there is no obvious bzero or memset nearby.


# 1.90 13-Sep-2007 mk

Remove unnecessary memset() (see aml_evalnode(9)) and a bit of KNF.

ok toby


# 1.89 13-Sep-2007 weingart

Implement ACPI 6.5.1 spec tree walk for _STA and _INI.
Fixes mk's laptop. No regressions so far (thank you to the
testers).

ok gwk@, mk@, marco@


Revision tags: OPENBSD_4_2_BASE
# 1.88 17-Apr-2007 mk

Now that acpidock no longer hangs machines during boot (at least
according to the reports I've been getting) it should be safe to enable
it again, so remove the #if 0 stuff around it.

ok marco


# 1.87 11-Apr-2007 jordan

Added changes for C-State
ok marco@


# 1.86 26-Mar-2007 marco

Calculate minutes left more accurately.

From giovanni <qgiovanni@gmail.com>


Revision tags: OPENBSD_4_1_BASE
# 1.85 03-Mar-2007 mk

Stop looking for acpidock until we fix that stuff that breaks all pre-60
series ThinkPads with acpidock(4) enabled.


# 1.84 22-Feb-2007 marco

We were getting no interrupts in .MP. There are 2 fixes; one in
acpi_machdep.c and move the interrupt establishment down after ioapic
has been initialized.

ok jordan


# 1.83 21-Feb-2007 kettenis

Make an acpi-enabled GENERIC.MP work on systems that only support PIC mode.

ok marco@


# 1.82 19-Feb-2007 jordan

Added acpi_map_address function for mapping GAS/IO registers
ok marco@


# 1.81 18-Feb-2007 jordan

Added dynamic perfect hash for opcodes
ok marco@


# 1.80 18-Feb-2007 marco

Shave off another K pre compression.
Shuffle SMALL_KERNEL functions around so that we only have a single #ifndef


# 1.79 17-Feb-2007 marco

Add SMALL_KERNEL throught acpi to make it fit on boot media.
ok deraadt


# 1.78 13-Feb-2007 marco

Add smarter _INI code. This helps with some dissapearing devices.

ok kettenis jordan


# 1.77 31-Jan-2007 gwk

Introduce acpi_hasprocfvs thats "ACPI Has Processor Frequency and Voltage
Scalling" a global flag set by acicpu attach to indicate that ACPI is
handling fvs through the _PSS objects. This will fix disappearing
EST/powernow when booting kernels with acpi enabled, as acpicpu isnt
compiled into GENERIC yet. Also in cases where acpicpu is built in and
legacy EST/powernow work but acpi is missing the _PSS object, the legacy
routines will be used.

tested by dim@, and Nick Nauwelaerts <nick AT nauwelaerts.net>
ok marco@, dim@


# 1.76 22-Jan-2007 mk

Actually look for docking stations by looking for _DCK nodes.

ok marco


# 1.75 02-Jan-2007 marco

On some systems EC must be attached as the first device. Move EC from
generic _HID devices to its own routine to force first attach. Bug found
and debugged by joris@

Code mostly written by joris@


# 1.74 26-Dec-2006 marco

Add polling to devices that require it such as acpibat and acpitz.
Use the same tsleep/wakeup mechanism as the interrupt code to keep it non-
concurrent.


# 1.73 21-Dec-2006 deraadt

even more knf love


# 1.72 21-Dec-2006 marco

Remove some leftover acpiec gpe/interrupt code.


# 1.71 20-Dec-2006 deraadt

oops, name already used


# 1.70 20-Dec-2006 deraadt

rename __acpi_enable_gpe(); ok marco


# 1.69 20-Dec-2006 deraadt

acpi_unmap_pmregs() is not used, ok marco


# 1.68 19-Dec-2006 deraadt

simplify some low-level methods substantially (shrinking code); ok marco jordan


# 1.67 06-Dec-2006 mk

malloc(..., M_NOWAIT) with no NULL check is bad. If allocation fails,
complain, clean up, and bail out of attach.

input and ok gwk


# 1.66 28-Nov-2006 jordan

Fixed clearing arg for acpi_init_pic
ok kettenis@


# 1.65 27-Nov-2006 kettenis

Don't spam dmesg.


# 1.64 27-Nov-2006 jordan

Fixes for acpiprt routing
Post-parse fixup for namerefs
ok kettenis@


# 1.63 25-Nov-2006 marco

Don't handle acpiev events inside the gpe handler since this causes deadlocks.

ok jordan@


# 1.62 15-Nov-2006 kettenis

Hook up acpi apic interrupt routing support to generic acpi code.

ok jordan@


# 1.61 02-Nov-2006 marco

Kill acpiec attach function and add its functionality to the generic _HID
device attach

prodded and ok jordan


# 1.60 02-Nov-2006 marco

Fix comments to reflect reality.


# 1.59 19-Oct-2006 marco

Replace ACPI ioctls with APM ioctls. This makes ACPI talk the same protocol
as APM therefore userspace can now retrieve power information in the same
fashion as all other apm arches (zaurus, macppc, i386 etc).

To use force the use of acpi device like so: apmd -f /dev/acpi

ok jordan@


# 1.58 19-Oct-2006 jordan

Fixed GPE debug levels
Fixed ref counter on temporary bufferfields
DSDT parsing fully working on IBM Thinkpad
ok marco@


# 1.57 15-Oct-2006 jordan

Removed old GPE handler code remnants
ok marco@


# 1.56 12-Oct-2006 jordan

Massive ACPI changes
Fixed ACPI leaks
new ACPI API
ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.55 30-Jun-2006 gwk

KNF, No functional change,
"go at it" marco@


# 1.54 30-Jun-2006 jordan

Support for calling Embedded Controller via new GPE interface
ok marco@


# 1.53 30-Jun-2006 jordan

Added new code for GPE handling
Also works with ACPIEC; need to fixup acpiec.c to remove original interrupt handler
ok marco@


# 1.52 30-Jun-2006 jordan

Added offset argument for acpi read/write pmreg routines (new GPE code)
ok marco@


# 1.51 31-May-2006 todd

do not call acpi_powerdown() unless acpi has successfully initialized
ok gwk@ canacar@


# 1.50 29-May-2006 canacar

Add embedded controller support.
ok marco@ deraadt@ testing & ok gwk@


# 1.49 19-May-2006 canacar

Minimal thermal zone driver. Monitors thermal zone temperature,
shuts down the system if the 'critical temperature' is reached.
ok marco@


# 1.48 11-Apr-2006 marco

KNF.


# 1.47 11-Apr-2006 gwk

Add acpi_resume(struct acpi_softc *sc), which calls some AML methods,
detected at attach time, currently unused.

ok marco@


# 1.46 11-Apr-2006 gwk

Discover some ACPI sleep related methods, and call them at the
appropriate time in acpi_enter_sleep_state().

ok marco@


Revision tags: OPENBSD_3_9_BASE
# 1.45 26-Feb-2006 marco

Add acpicpu as a device.

ok jordan@


# 1.44 23-Feb-2006 marco

remove dead function.

ok jordan@


# 1.43 22-Feb-2006 marco

Print out all tables it found during autoconf. We'll need this data in
the future.


# 1.42 22-Feb-2006 marco

Make dmesg less verbose.

ok jordan.


# 1.41 22-Feb-2006 jordan

Added parsing of SSDT tables
ok marco@


# 1.40 22-Feb-2006 jordan

Added new aml_register to notify by device id
Moved powerdown code to button device
ok marco@


# 1.39 21-Feb-2006 marco

Readjust debug levels that crept in too low over the last few days.
KNF


# 1.38 21-Feb-2006 marco

Rig the power button so that the machine shuts down correctly.


# 1.37 20-Feb-2006 marco

Add acpibtn device. Remains disabled for now.


# 1.36 19-Feb-2006 jordan

Added code for aml_notify GPE notification callback
ok marco@


# 1.35 19-Feb-2006 grange

Fix acpi_enter_sleep_state() by replacing current ugly hack
with mostly correct sleeping state transition code.

ok brad@ marco@


# 1.34 19-Feb-2006 marco

jordan:
Enable interrupts.
Added peripheral code to make this work.

marco:
Replace softintr and other types of interrupts with a handler with context.
Fixed interrupt magic for GPEs.
Some KNF

This is work done by jordan and I but has to be a single commit.


# 1.33 17-Feb-2006 marco

Remove earlier introduced delays. We really only need them during autconf.

ok jordan@


# 1.32 17-Feb-2006 marco

Add a generic delay function as per the ACPI spec.

from jordan@


# 1.31 17-Feb-2006 marco

Use tsleep instead of delay when !cold.


# 1.30 17-Feb-2006 marco

Argh, we need delays between reads/writes. The backend reads (i2c most likely)
are so slow that values come back as 0 sometimes. We have to do this
despite the spec saying we shouldnt have to. Guess having a faster
parser than others can hurt sometimes.

Idea from jordan.


# 1.29 17-Feb-2006 jordan

Fixed code for handling _INI
Rearranged debug statements
ok marco@


# 1.28 16-Feb-2006 jordan

Added stub for calling _INI
Fixed aml_dowhile for NULL return
ok marco@


# 1.27 16-Feb-2006 jordan

Fixed parser/evaluator to retrieve battery info+state
Cleanup of parser
Dell laptops return battery status
ok marco@


# 1.26 06-Feb-2006 brad

fix a typo so this will compile on amd64.


# 1.25 04-Feb-2006 marco

When ACPI_ENABLED wasn't set but the devices were it had an unknown variable.

Pointed out by ian@.


# 1.24 03-Feb-2006 jordan

Rewrote aml parser yet again...
Fixed bus_space_map for i386/amd64
Added _BST and _BIF to acpibat to get status/info
ok marco@


# 1.23 20-Jan-2006 jordan

More evaluator changes
Removed old evaluator code
Fixed _HID value
ok marco@


# 1.22 19-Jan-2006 jordan

Added support for read/write of PCI Config space
Help jason@
ok marco@


# 1.21 18-Jan-2006 jordan

Added new evaluation routines
Added helper methods for buffer/field evaluation
Fixed broken indentation on dsdt.c

ok marco@


# 1.20 17-Jan-2006 jordan

Major overhaul of the aml parser. Evaluater temporarily disabled.

ok marco@


# 1.19 06-Jan-2006 grange

Check for ACPI control availability only if ACPI_ENABLE defined.


# 1.18 06-Jan-2006 grange

Informative error messages on attach, prettier dmesg.


# 1.17 06-Jan-2006 grange

printf -> dnprintf conversion for debug messages.


# 1.16 05-Jan-2006 grange

Find SLP_TYPx values and store them for future use in
acpi_enter_sleep_state().


# 1.15 05-Jan-2006 grange

Random KNF and cleanup


# 1.14 05-Jan-2006 grange

Provide symbolic names for the acpi sleeping states and use it.


# 1.13 28-Dec-2005 marco

Moved DEVNAME to a acpidev.h

ok jordan@


# 1.12 18-Dec-2005 sturm

add back comment about wrong code

ok marco


# 1.11 16-Dec-2005 marco

Remove test for kbd_reset. Prompted deraadt@, ok jordan@


# 1.10 16-Dec-2005 jordan

Added hooks for powerdown/power button press


# 1.9 16-Dec-2005 jordan

Cleaned up common power management register i/o
Added enter sleep state ioctl
Hooked power button interrupt
ok marco@


# 1.8 16-Dec-2005 marco

Remove autoconf chatter


# 1.7 16-Dec-2005 jordan

Moved DSDT AML parsing into parent ACPI driver
ok marco@


# 1.6 14-Dec-2005 marco

Add AC device glue.


# 1.5 14-Dec-2005 marco

Adjust debug printing levels to something more sane.


# 1.4 13-Dec-2005 marco

Add glue to attach devices to apci.


# 1.3 07-Dec-2005 marco

KNF


Revision tags: OPENBSD_3_8_BASE
# 1.2 10-Jul-2005 grange

Spacing cleanup.


# 1.1 02-Jun-2005 tholo

Start on a basic ACPI framework -- does not do much more than read out the
ACPI tables into kernel memory and attach ACPI and HPET timers currently.

In order to test this code, enabling the devices in GENERIC as well as
the ACPI_ENABLE option is needed. This code does not do any thermal
control yet, so this should be done with care depending on the platform.

In the tree so more people can contribute to making this more fully
featured.

Ok niklas@ grange@ tedu@


# 1.393 17-Dec-2020 kettenis

Fix some issues with referencing named ACPI nodes from Packages.
These references need to be resolved at runtime rather than when
they're parsed such that they pick up the right values for those nodes
which can be changed when for example _INI methods run.

The current approach is to replace these reference with a string that
names the node in question. The problem with that is that packages
can also contain normal strings. Which means that we need special
code that depends on the context where the package is used.

This diff takes a different approach by retaining a reference when
parsing. Code that uses a package will need to resolve this reference
but there is no ambiguiety anymore.

ok patrick@


# 1.392 05-Dec-2020 kettenis

Add support for the _EVT method to the GPIO support code.

ok patrick@


Revision tags: OPENBSD_6_8_BASE
# 1.391 27-Aug-2020 jmatthew

Reorder the acpi attach process so we look at Processor() CPU nodes
after Device() ones, since we should prefer the newer node type.
If we see any Device() nodes, don't attach acpicpu(4) to any Processor()
nodes if they're also present. This also makes acpitz(4) and
acpipwrres(4) devices attach slightly later.

ok kettenis@, also tested by jmc@


# 1.390 16-Aug-2020 gnezdo

Remove sc_maxgpe from acpi_softc which was never read from

Remove a dead conditional from acpi_init_gpes while there.

ok jcs@


# 1.389 21-Jul-2020 deraadt

acpi can use IPL_BIO (a low interrupt) since it only enqueues operations for
later processing. The use of a high interrupt will predate suspend/resume
efforts, we had to redesign acpi to be non-reentrant obviously
discussed with kettenis, in snaps for more than a week


# 1.388 24-Jun-2020 cheloha

kernel: use gettime(9)/getuptime(9) in lieu of time_second(9)/time_uptime(9)

time_second(9) and time_uptime(9) are widely used in the kernel to
quickly get the system UTC or system uptime as a time_t. However,
time_t is 64-bit everywhere, so it is not generally safe to use them
on 32-bit platforms: you have a split-read problem if your hardware
cannot perform atomic 64-bit reads.

This patch replaces time_second(9) with gettime(9), a safer successor
interface, throughout the kernel. Similarly, time_uptime(9) is replaced
with getuptime(9).

There is a performance cost on 32-bit platforms in exchange for
eliminating the split-read problem: instead of two register reads you
now have a lockless read loop to pull the values from the timehands.
This is really not *too* bad in the grand scheme of things, but
compared to what we were doing before it is several times slower.

There is no performance cost on 64-bit (__LP64__) platforms.

With input from visa@, dlg@, and tedu@.

Several bugs squashed by visa@.

ok kettenis@


# 1.387 10-Jun-2020 jca

Give userland a consistent battery and A/C state after resume

Previously we refreshed the state of acpiac(4) and acpibat(4)/acpisbs(4)
by queueing fake ACPI events to be processed by the acpi thread.
acpibat(4) using ACPIDEV_POLL meant that its status could be refreshed
first, resulting in an APM_POWER_CHANGE event sent to userland before
the state of acpiac(4) was up to date. Because of this, apmd(8) could
see A/C unplugged when it was in fact plugged, possibly leading to
a wrong decision.

Instead of using fake ACPI events, refresh the status of acpiac(4) and
acpibat(4)/acpisbs(4) right away at DVACT_WAKEUP time. To reuse the
existing refresh code of those drivers, make their ACPI *_notify()
handlers responsible for sending APM_POWER_CHANGE events instead. Also
queue an APM_POWER_CHANGE event right after resume to retain existing
behavior.

This fixes the consistency of data shown to userland, and makes those
drivers less special.

Initially discussed last year (!) with mpi@ at Miod's place.
Feedback and ok jcs@, deraadt@ agrees with the use of DVACT_WAKEUP here.


# 1.386 29-May-2020 deraadt

dev/rndvar.h no longer has statistical interfaces (removed during various
conversion steps). it only contains kernel prototypes for 4 interfaces,
all of which legitimately belong in sys/systm.h, which are already included
by all enqueue_randomness() users.


# 1.385 14-May-2020 kettenis

Bring back the acpi(4) changes. This time around only use the ACPI _CRS
information on ACPI 5.0 and later.

ok krw@, patrick@


# 1.384 11-May-2020 jca

More accurate remaining power reporting with disparate batteries

Overall remaining power was computed as the average of the remaining
power of each battery, in percents. This doesn't work with batteries of
different capacity, so use the total capacity and remaining power
instead.

Reported by lidstah, successful tests by lidstah and benno@, ok kettenis@


# 1.383 08-May-2020 kettenis

Move parsing of _CRS into acpi(4) and pass the parsed address and interrupt
information in struct acpi_attach_args.

ok mpi@


Revision tags: OPENBSD_6_7_BASE
# 1.382 14-Apr-2020 kettenis

Implement acpi_getprop(), the ACPI equivalent of OF_getprop().

ok patrick@


# 1.381 12-Apr-2020 kettenis

Add support for the _CCA method. This method indicates whether DMA is
cache-coherent or not. To implement this, acpi(4) gets two bus_dma tags
and passes the appropriate one when attaching devices based on _CCA.
On i386/amd64, where for all practical purpose DMA is always cache-coherent,
the two tags are the same. But on arm64 they are distinct.

ok patrick@


# 1.380 07-Apr-2020 visa

Abstract the head of knote lists. This allows extending the lists,
for example, with locking assertions.

OK mpi@, anton@


# 1.379 03-Apr-2020 mpi

Fix incoherencies of errors returned by various *kqfilter routines.

EOPNOTSUPP is returned when there's no "filterops" for a given fd.
EINVAL when the requested filter isn't supported by the "filterops".
ENXIO means the underlying device is no longer valid/present.

ok visa@


# 1.378 20-Feb-2020 visa

Replace field f_isfd with field f_flags in struct filterops to allow
adding more filter properties without cluttering the struct.

OK mpi@, anton@


# 1.377 31-Dec-2019 visa

Use C99 designated initializers with struct filterops. In addition,
make the structs const so that the data are put in .rodata.

OK mpi@, deraadt@, anton@, bluhm@


# 1.376 31-Dec-2019 mpi

Convert infinite sleeps to {m,t}sleep_nsec(9).

ok kn@


# 1.375 31-Dec-2019 jsg

include sys/sensors.h for sensor use
don't depend on indirect inclusion from acpidev.h


Revision tags: OPENBSD_6_6_BASE
# 1.374 07-Sep-2019 kettenis

Revert acpipci(4) changes. Too many systems are broken. We'll try again
after 6.6 as been released. The acpireg.h change stays behind.


# 1.373 28-Aug-2019 kettenis

Use ACPI information to attach PCI busses like we do on arm64. There are a
few additional quirks though, and attaching the PCI busses is delayed to
replicate the existing code more closely. That may be changed in the
future. Also tweak how we handle MSI support and respect to ACPI flag
that says we shouldn't attempt to use MSIs.

Some fallout is expected.

ok patrick@


# 1.372 27-Aug-2019 deraadt

On amd64 the bootup and resume paths for SP/BP/SP were not performing
msr/pat, clock-startup, firmware upgrade, fpu initialization, and cpu
soft-state initialization in the same order. That kind of chaos is
crazy and fragile. Improve the acpi resume call sequence and unify order
of the operations.


# 1.371 02-Jul-2019 jcs

acpi_set_gpehandler: turn edge arg into flags, add support for
passing GPE_DIRECT which directly executes the supplied callback and
clears a level interrupt rather than going through acpi_taskq which
can be slow when other driver callbacks are in the queue.

This should not be used for GPE handlers that need to do anything
ACPI-related.

ok kettenis


# 1.370 10-Jun-2019 kettenis

Print proper ACPI version number.

ok mlarkin@


# 1.369 08-Jun-2019 kettenis

When establishing the mapping between ACPI device nodes and PCI devices,
skip PCI host bridges and devices that aren't there.

ok patrick@


# 1.368 07-Jun-2019 kettenis

Introduce acpi_getsta() and use it to eliminate some duplicated code.

ok patrick@


# 1.367 12-May-2019 kettenis

Make sure the extended PM register descriptions actually exist before
using them. Fixes machines with ancient ACPI 1.0 (such as recent
hypervisors).

ok deraadt@


# 1.366 12-May-2019 kettenis

Add support for mapping ACPI PM registers using the "extended" gasio
representation from the FADT. Mostly fixes the Lanner NCA-1510.

ok lteo@, mlarkin@, deraadt@


# 1.365 11-May-2019 lteo

Fix incorrect error message.

ok kettenis@


# 1.364 08-May-2019 tedu

free size. the allocation is the header plus the length.


Revision tags: OPENBSD_6_5_BASE
# 1.363 04-Apr-2019 kettenis

Avoid potential null-pointer dereference. Found by Kent R. Spillner.

ok deraadt@, jsg@


# 1.362 20-Jan-2019 jsg

fix SMALL_KERNEL build after changes in rev 1.361
reported by naddy@ ok deraadt@


# 1.361 19-Jan-2019 tedu

Add a pwraction sysctl that controls what the power button does on acpi.
By default, nothing changes -- shutdown is initiated. But allows turning
power button into a sleep button if desired.
(grudging) ok from a few parties


# 1.360 26-Oct-2018 kettenis

Add acpipci(4) on amd64. For now this only calls the PCI-specific _OSC
method to let the ACPI implementation know what features we support.


Revision tags: OPENBSD_6_4_BASE
# 1.359 30-Aug-2018 kettenis

Move softbutton check before the check that skips attaching certain acpi
devices. Fixes regression caused by matching on _CID in addition to
matching on _HID.

ok matthieu@, mlarkin@


# 1.358 25-Aug-2018 kettenis

Match ACPI devices based on _CID if no match for _HID is found.

ok mpi@


# 1.357 19-Aug-2018 kettenis

Add support for multiple PCI segments. Only really implemented for arm64
for now as amd64/i386 firmware still caters for legacy OSes that only
support a single PCI segment.

ok patrick@


# 1.356 03-Aug-2018 kettenis

Let ahci(4) match on _CLS instead of _HID when attaching at acpi(4). Avoids
having to add many more _HID entries to the match table.

ok deraadt@, mlarkin@


# 1.355 10-Jul-2018 kettenis

Make legacy interrupts work in acpipci(4).

ok patrick@


# 1.354 05-Jul-2018 kettenis

Add acpipci(4), a driver that supports generic ECAM-compatible PCI host
bridges based on information provided by ACPI.

ok mlarkin@


# 1.353 04-Jul-2018 kettenis

Properly pass around the PCI "chipset tag" in acpi(4) and refactor
acpimcfg(4) to call an MD initialization functions that sets up a tag for
PCI ECAM.

ok guenther@, mlarkin@, krw@


# 1.352 01-Jul-2018 kettenis

Move acpi(4) attach glue into acpi_machdep.c.

ok guenther@, deraadt@


# 1.351 01-Jul-2018 kettenis

Add support for _DSD Device Properties.

ok mlarkin@


# 1.350 30-Jun-2018 kettenis

Add intr_enable() function, intended for MI use to amd64 and i386 and use
this in the acpi(4) suspend/resume code paths.

ok deraadt@


# 1.349 29-Jun-2018 kettenis

Use <stdint.h> types.

ok mpi@, jung@, krw@, deraadt@


# 1.348 29-Jun-2018 mlarkin

ACPI: Allow (with warning) GPE handler reassignment, instead of returning
a failure code.

ok kettenis


# 1.347 26-Jun-2018 mlarkin

knf


# 1.346 26-Jun-2018 kettenis

Let acpi(4) pass down the bus dma tag.

ok deraadt@


# 1.345 25-Jun-2018 kettenis

Reorganize acpi(4) code a bit in preparation for upcoming arm64 support.

ok deraadt@


# 1.344 20-May-2018 kettenis

Wake up the acpi thread after scheduling a gpio event.

ok mpi@


# 1.343 17-May-2018 kettenis

Sprinkle some #ifndef SMALL_KERNEL such that RAMDISK kernels cuild again.

pointed out by stsp@


# 1.342 17-May-2018 kettenis

Implement GenericSerialBus OpRegion support.

ok mlarkin@


# 1.341 27-Mar-2018 kettenis

Add acpicmos(4), a driver that implements SystemCMOS OperationRegion
access support. This fixes machines where the AML doesn't check whether
support for this OperationRegion type has been registered by the OS.

ok mlarkin@


Revision tags: OPENBSD_6_3_BASE
# 1.340 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.339 10-Feb-2018 deraadt

Syncronize filesystems to disk when suspending. Each mountpoint's vnodes
are pushed to disk. Dangling vnodes (unlinked files still in use) and
vnodes undergoing change by long-running syscalls are identified -- and
such filesystems are marked dirty on-disk while we are suspended (in case
power is lost, a fsck will be required). Filesystems without dangling or
busy vnodes are marked clean, resulting in faster boots following
"battery died" circumstances.
Tested by numerous developers, thanks for the feedback.


# 1.338 08-Feb-2018 deraadt

After flushing disks, try again to release all memory. We don't need to
save dirty memory to the hibernate space.


# 1.337 08-Feb-2018 deraadt

Toss all releaseable memory, because fragmentation can get in the way
of allocating the hibernate playpen.


# 1.336 08-Feb-2018 deraadt

sr_quiesce() is a new approach for ensuring that softraid drains
output to the disks.

This is part of a larger suspend/resume filesystem-safety diff, which
has been worked on for a couple of months already. Tests by job, krw,
beck, benno, and others. Sometimes even by snapshot users...


# 1.335 29-Nov-2017 kettenis

Add support for GPIO-signaled events.

ok mlarkin@


# 1.334 16-Nov-2017 jcs

Add PCI attachment for dwiic(4) needed by Intel 100 Series machines

ok kettenis


# 1.333 14-Nov-2017 kettenis

Add a few more PNP IDs to the skip lists.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.332 17-Aug-2017 stsp

Make dwiic(4) attach its ACPI parent dependencies.
This change makes the Asus E200HA keyboard work.

Original analysis and patch by Cesare Gargano
ok kettenis@


# 1.331 09-Aug-2017 dcoppa

Add "PNP0303" (8042 PS/2 Controller) to acpi_isa_hids[]

ok kettenis@


# 1.330 22-Jul-2017 jcs

acpibat: look for _BIX first before falling back on _BIF

ACPI 4.0 deprecated _BIF for battery status so some newer machines
are exporting _BIX which extends _BIF's package with a few extra
fields. When using _BIX, export the new discharge cycle count as a
sensor.

feedback from kettenis and anton
ok deraadt


# 1.329 20-Jul-2017 kettenis

Start a list of isa(4) device names such that we can avoid printing them
as "not configured".

ok mikeb@, millert@, deraadt@


# 1.328 08-Apr-2017 deraadt

Sensors are run as callbacks inside tasks. During suspend a sensor
could be running inside a driver that will be force-detached, or due
to tsleep end up disrupting the softstate/hardstate contract. At
suspend time, quisce all these callbacks by waiting for completion.
This issue has never been observed for real, but may be implicated in
suspend/resume failures.
ok kettenis guenther mlarkin


# 1.327 08-Apr-2017 deraadt

Two prototypes for acpi_maptable; merge to one place


Revision tags: OPENBSD_6_1_BASE
# 1.326 27-Mar-2017 deraadt

Now that hibernate_alloc() only has clean success/failure, don't
need to call hibernate_free() to clean up a partial mess.
ok mlarkin kettenis


# 1.325 13-Mar-2017 deraadt

allow ramdisks to build; ok jcs


# 1.324 12-Mar-2017 jcs

try to attach acpisbs first and if successful, don't attach acpibat


# 1.323 02-Mar-2017 natano

Add a new sysctl machdep.lidaction. The sysctl works as follows:

machdep.lidaction=0 # do nothing
machdep.lidaction=1 # suspend
machdep.lidaction=2 # hibernate

lidsuspend is just an alias for lidaction, so if you change one, the
other one will have the same value. The plan is to remove
machdep.lidsuspend eventually when people have upgraded their
/ets/sysctl.conf.

discussed with deraadt, who came up with the new MIB name
no objections mlarkin
ok stsp halex jcs


# 1.322 28-Feb-2017 natano

Untangle abstract sleep modes from ACPI sleep states. This paves the way
for further work in the area.

idea and ok deraadt
ok mlarkin


# 1.321 22-Feb-2017 jcs

add acpisbs, an acpi smart battery subsystem driver reading data
over smbus

currently disabled because it conflicts with acpibat

ok deraadt, kettenis


# 1.320 14-Jan-2017 kettenis

Revert aml_rdpciaddr changes; breaks several machines that were working
before.


# 1.319 11-Jan-2017 jsg

Increase the size of the buffers used for _HID and _CID strings to be
able to fit "Hyper_V_Gen_Counter_V1".

ok mlarkin@ deraadt@


# 1.318 08-Jan-2017 kettenis

Make aml_rdpciaddr() work for for devices behind bridges. The code now
uses the mapping between AML nodes and PCI devices that we establish.
Because _INIT methods may end up calling aml_rdpciaddr(), make sure we
create that mapping early. Also handle devices that aren't actually present.
These devices are now included in the mapping and reads will return an
all-ones patterm whereas writes are a no-op..

ok mlarkin@


# 1.317 25-Oct-2016 pirofti

Add the resource index to aml_parse_resource() callback function.

This is needed by an upcoming acpiec commit that handles machines breaking
the current ACPI specifications.

Change suggested by and ok kettenis@, guenther@


# 1.316 18-Sep-2016 guenther

If the APIC table (MADT) isn't present then ci_acpi_proc_id won't be set
when acpicpu tries to attach, leading to a NULL dereference. The ACPI
processor object on the Dells where this was reported is bogus and useless
anyway so apply the same test when deciding whether to attach to processor
objects as acpicpu would when matching them to cpus.

Problem reported by Eivind Eide (xenofil (at) gmail.com) and
Johan Huldtgren (johan+openbsd-bugs (at) huldtgren.com).
Testing by Eivind Eide.
ok kettenis@ mlarkin@


# 1.315 03-Sep-2016 naddy

Write the system time back to the RTC every 30 minutes.
This fixes the problem that long-running machines which were not
shut down properly would reboot with a badly offset system time.

hints and ok kettenis@


# 1.314 31-Aug-2016 mlarkin

Don't put a device in the wake device list if _STA says it's not there.

Fixes at least one machine that woke up from sleep / poweroff immediately.

ok kettenis@, tested by kettenis and martijn@


# 1.313 28-Jul-2016 kettenis

Store the acpi processor ID/UID in struct cpu_info, and use it to attach
acpicpu(4) drivers to the right cpu(4).

ok mlarkin@, guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.312 10-Jun-2016 kettenis

The power management timer has been made optional in ACPI 5.0A, so don't
attempt to attach acpitimer(4) if the timer isn't present.

Pointed out by jcs@

ok jcs@


# 1.311 08-May-2016 kettenis

Make sure devices listed by a device's _DEP method are attached before
we attach the device itself. The _DEP methode, introduced in ACPI 5.0,
is a hint that the device in question depends on OpRegion support from
the devices listed by _DEP, so we have to order them the proper way.
To prevent us from attaching those devices again when we encounter them
later walking down the device tree, keep track of our attempts to
attach a device driver to them using aflag in the aml_node structure.

ok guenther@


# 1.310 25-Apr-2016 pirofti

We attach acpihpet(4) and acpiec(4) without using the HID, so do not
report it in the dmesg.

OK deraadt@, mlarkin@.


# 1.309 02-Apr-2016 kettenis

Reduce dmesg spam by knocking out some acpi hardware IDs that don't need a
driver.

ok mpi@


# 1.308 29-Mar-2016 kettenis

Check if a device is present (using _STA) in acpi_foundhid. This prevents
us from reporting devices as "not configured" that aren't actually present,
and allows us to remove duplicated code from several drivers.

ok deraadt@, ok jsg@


# 1.307 29-Mar-2016 kettenis

Implement a cleaner way to prevent acpivout(4) from attaching if we have
a platform-specific method of controlling the backlight.

ok millert@, mlarkin@, jung@


# 1.306 28-Mar-2016 kettenis

Adding each and every new acpi device driver to acpi_foundhid() is
gettinga bit of the burden. And it really isn't how our autoconf
framework is supposed to work for busses that can be enumerated. All
the drivers already check for a matching _HID in their attach
function. So we can just drop the checks in acpi_foundhid() and let
the drivers sort themselves out.

Print a "not configured" line for devices for which we don't attach adriver.
This may be a bit much as there are devices for which we will not have a
driver. This will be removed if it causes too much dmesg spam.

ok deraadt@, mlarkin@


Revision tags: OPENBSD_5_9_BASE
# 1.305 17-Jan-2016 jsg

Take acpi_foundhid() outside of #ifndef SMALL_KERNEL so the ramdisk
kernel can find and attach dwiic(4) and sdhc(4).


# 1.304 17-Jan-2016 jsg

remove an unused global variable


# 1.303 14-Jan-2016 kettenis

Attach dwiic(4) on the Intel Bay Trail i2c controllers as well.


# 1.302 12-Jan-2016 kettenis

Improve emulation of PM registers on Hardware-redcued ACPI by emulating
SPL_TYPx, SLP_EN and WAK_STS using the new SLEEP_STATUS_REG and
SLEEP_CONTROL_REG registers. Makes the transition into S5 (powerdown) work
on the ASUS X205TA.

ok deraadt@, mlarkin@


# 1.301 12-Jan-2016 jcs

Add dwiic, a driver for the Synopsys DesignWare i2c controller found
on the Samsung ATIV Book 9 laptop. This initial version only
supports ACPI config/attachment.

Add ihidev, a HID-over-i2c driver largely based on uhidev. dwiic
handles attaching ihidev devices found in ACPI.

Add ims, a HID-over-i2c mouse/trackpad driver to get basic cursor
and button functionality from HID-compliant i2c trackpads.

ok kettenis deraadt


# 1.300 11-Jan-2016 kettenis

Make sdhc(4) attach to hardware IDs "80860F14" and "PNP0FFF".


# 1.299 09-Jan-2016 kettenis

Initial support for Hardware-Reduced ACPI. This skips the legacy mode checks
by emulating the PM_CNT register such that SCI_EN is always enabled. It also
skip attaching acpitimer(4) since the relevant registers are no longer there.

ok deraadt@, mlarkin@


# 1.298 06-Jan-2016 kettenis

Follow the spec and check the SCI_EN bit to see if ACPI has already
been enabled and don't bother enabling it again if it is. This should fix
systems that don't implement legacy mode and therefore set ACPI_ENABLE and
ACPI_DISABLE to zero.

ok mlarkin@, yasuoka@


# 1.297 23-Nov-2015 reyk

Back out the CRITICAL change, turn it back from 10% to 25%. Some
people with old/broken batteries didn't like this change, and the
underlying heuristics are wrong.


# 1.296 22-Nov-2015 reyk

The "CRITICAL" APM battery level, that is exported/faked by the ACPI
stack, is currently set to 25%. With 25% battery left, a typical
modern laptop will have at least one hour runtime left. It is an
arbitrary value, and other APM power layers in the kernel set CRITICAL
to something like 5% or 10%, so set it to 10% for now.

OK mlarkin@


# 1.295 28-Sep-2015 deraadt

In low-level suspend routines, set cold=2. In tsleep(), use this to
spit out a ddb trace to console. This should allow us to find suspend
or resume routines which break the rules. It depends on the console
output function being non-sleeping.... but that's another codepath which
should try to be safe when cold is set.
ok kettenis


# 1.294 06-Sep-2015 deraadt

clarify malloc arguments (base + length...)


# 1.293 28-Aug-2015 deraadt

fairly simple sizes for free(); ok tedu


# 1.292 20-Aug-2015 kettenis

Respect the access size when reading or writing to pci config space.
This version also makes sure writes are properly aligned, preventing
the panics seen with the previous incarnation of this change.

Fixes battery status passthrough in vmware, brightness keys on my x220
and who knows what else.

ok mlarkin@, deraadt@


Revision tags: OPENBSD_5_8_BASE
# 1.291 04-Aug-2015 deraadt

If we are setting ACPI_PM1_BM_RLD do it only once, and remember to
repeat this action at resume time.
ok mlarkin guenther


# 1.290 29-Jul-2015 deraadt

Something subtle in previous revision is locking up suspend or booting
on some laptops. Mailing lists threads mention x220 and Asus machines.
Reverting for now.


# 1.289 21-Jul-2015 kettenis

iRespect the access size when reading or writing to pci config space.
Fixes battery status passthrough in vmware and who knows what else.

Tested by many.

ok mlarkin@, deraadt@


# 1.288 17-Jul-2015 mlarkin

fix comments


# 1.287 26-Mar-2015 jsg

Use the 0x10 extended capability to determine if the trackstick
buttons are wired to the trackpad and need to be re-routed to the
trackstick. Without this change the buttons on 2015 thinkpads
get picked up as extended buttons that show up as scroll up/down.

Remove the X1 Carbon 2015 (LEN0048) and X250 (LEN0046) from the
top button area/soft buttons quirks list. Also avoid using the
quirk list entirely if the capability bit is set.

Based on work from jcs@ with feedback from mpi@
ok mpi@ jcs@


# 1.286 20-Mar-2015 kettenis

The ACPI standard says that a PCI function number of FFFF can be used to refer
to all functions on a device. This makes no sense in the context of accessing
PCI config space. Nevertheless stsp@ has a machine with AML that tries to
do config space reads from a device where _ADR encodes function number FFFF.
This is almost certainly buggy AML, but since Linux (and presumably Windows)
boot just fine on this machine we need to work around this. Linux effectively
treats function FFFF as function 7, which isn't present on this machine.
Instead we simulate a read from a non-existent device by returning a bit
pattern of all ones. Writes to function FFFF will still panic.

ok mlarkin@


# 1.285 16-Mar-2015 deraadt

Reduce use of SMALL_KERNEL in acpi, bringing acpiec driver and more gpe
handling into RAMDISK. This is now possible because the install media
has ample room. The goal is to reduce special cases where we may be
skipping (unknown) important operations...
ok mlarkin kettenis


# 1.284 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.283 07-Feb-2015 deraadt

New framework that allows hibernate to pass in entropy from it's fresh
boot.
ok mlarkin


# 1.282 06-Feb-2015 mlarkin

Fix a hibernate crash on some machines due to unmapping a page that
may not have been mapped previously (in the failure to hibernate case).

Also ensure that the lowmem ptp is mapped in all cases (not just MP).

ok kettenis


# 1.281 17-Jan-2015 deraadt

resume_randomness() before spinning up other cpus...


# 1.280 15-Jan-2015 jsg

Define a new wscons mouse type for Synaptics clickpad devices that lack
physical buttons. This will be used if the acpi pnpid for the mouse
matches a list provided by Synaptics found in Linux.

Combined with the xenocara changes this will remove the need for an xorg
config file for the x240/t440/t540 generation of thinkpads.

ok matthieu@ shadchin@ kettenis@


# 1.279 11-Jan-2015 kettenis

Tolerate checksum errors in ACPI tables (but complain about them). Apparently
there are machines out there with broken BIOSen with such checksum errors in
thier RSDT tables. Windows runs fine on these machines, so it probably does
not check. Linux ignores checksum errors as well (and prints a warning).

ok deraadt@, krw@


# 1.278 18-Dec-2014 deraadt

suspend and resume the RNG (at what looks like the right spots)
ok reyk djm


# 1.277 09-Dec-2014 doug

Replace some malloc(n*size,...) calls with mallocarray().

ok tedu@ deraadt@


# 1.276 06-Dec-2014 mpi

If "machdep.lidsuspend" is not set, do not go back to sleep if the lid
is still closed when resuming. This allows docked laptop to be resumed
without opening them.

ok mlarkin@


# 1.275 23-Nov-2014 mlarkin

If a machine has no _LIDs, then none can be open. This diff fixes a problem
where machines without any _LIDs were going back to sleep after hibernate
due to the recent suspend-if-lid-closed-on-resume diff. Noticed on qemu,
which doesn't have any _LIDs.

discussed with deraadt


# 1.274 18-Nov-2014 krw

Nuke yet more obvious #include duplications.

ok miod@


# 1.273 08-Nov-2014 mlarkin

If resuming from sleep (zzz/ZZZ) and the lid is still closed, go back to
sleep. This prevents accidental lid flex or slight opening in a backpack
from waking the machine up and leaving it resumed (powered on).

ok deraadt@


# 1.272 26-Sep-2014 kettenis

Rework piglet and pig allocation. Currently the piglet gets allocated
deep down in the suspend path, where it is really hard to recover from
allocation failure. So allocate the piglet early on in the suspend path.
Also change the piglet and piglet allocation functions to use km_alloc(9)
instead of doing pmemrange magic. This removes a bunch of code which, in the
case of the piglet allocation, is broken since it results in a NULL pointer
dereference. Also switch the piglet allocation to not wait. If we can't
allocate 16MB of phys contig memory on a halfway modern machine we're almost
certainly under a lot of memory pressure and we're better off not trying to
hibernate anyway.

ok mlarkin@


# 1.271 25-Sep-2014 kettenis

Bring back rev 1.249. Now that mp_setperf() has been fixed, it should be
safe to restore perflevel when resuming. Should fix the issues people
see with the processor running at full speed heating up the machine while
the apm command reports that the CPUs are running at a low speed.

ok dcoppa@, deraadt@


# 1.270 23-Sep-2014 kettenis

Release the acpi lock around calls to wsdisplay_suspend() and
wsdisplay_resume(). The X server may adjust the display brightness just
before/after the VT switch when suspending/resuming. In fact, it may
already be blocked adjusting the display brightness for some reason
unrelated to the suspend request.

The VT switch request will eventually time out, but the delay is annoying
and the failed VT switch will lead to problems after resume.

Needed for a future update of the xf86-video-intel driver.

ok mlarkin@


# 1.269 19-Sep-2014 kettenis

Use config_suspend_all(9).


# 1.268 08-Sep-2014 kettenis

Clear the PME Status bit on PCI devices in response to Device Wake events.
Fixes the interrupt storm on the 2009 Mac mini with WOL enabled on its
nfe(4) interface.

ok mlarkin@


Revision tags: OPENBSD_5_6_BASE
# 1.267 20-Jul-2014 mlarkin

Support hibernating to softraid crypto volumes.

much help and ok from deraadt@


# 1.266 16-Jul-2014 mlarkin

Reenable hibernate RLE support and flush+zero all memory after unpack.


# 1.265 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.264 12-Jul-2014 kettenis

Add a function to drop all clean pages on the page daemon queues and call
it when we hibernate.

ok mlarkin@, miod@, deraadt@


# 1.263 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.262 11-Jul-2014 mlarkin

Flush the buffercache to 16MB on hibernate and restore its previous max
size (kern.bufcachepercent) on resume, for better hibernate performance.

ok beck@


# 1.261 10-Jul-2014 blambert

remove unneeded header

ok mlarkin@


# 1.260 01-Jul-2014 kettenis

Adding an additonal delay after each access to SystemMemory or SystemIO
during autoconf makes no sense.

ok deraadt@


# 1.259 23-Jun-2014 kettenis

SystemMemory is used to access memory mapped registers on some machines, so we
must use the bus_space(9) API to access it instead of memcpy(9). Also make
sure we properly align access to fields that need more than byte alignment.

ok guenther@


# 1.258 21-May-2014 mlarkin

Fix invalid / missing format specifiers in several dnprintf statements
(used only when ACPI_DEBUG is enabled)

From a diff on tech@ by Fabian Raetz, with a minor change by me, tested
on i386 and amd64 with ACPI_DEBUG enabled and disabled.


# 1.257 25-Apr-2014 mlarkin

Defer the free of the hibernate structures until later, when it is safe to
do so.

Freeing the hibernate structures too early leads to inadvertent calls into
splx when we aren't ready for them yet. Found when trapping splx calls in
the resume path.

discussed with deraadt@ at m2k14


# 1.256 13-Apr-2014 mlarkin

Fix a badly worded debug message that implied the entire machine was going
to sleep, instead of just the acpi thread going to sleep.


# 1.255 31-Mar-2014 mpi

Change the order of operations during a suspend/resume cycle and
call bufq_quiesce() after executing the DVACT_QUIESCE handlers.

This should be safe since no disk nor controller drivers have such
handler but it will allow us to detach sd(4) devices attached to a
USB bus.

Another benefit pointed out by kettenis@ is that drivers that need
to read a firmware from the disk should be able to do it at resume
time in a DVACT_WAKEUP handler.

ok kettenis@, deraadt@


# 1.254 13-Mar-2014 dlg

get rid of the assumption that the head of the alldevs list is the
"mainbus" device. this breaks when mpath is enabled because it
attaches before mainbus and therefore takes the head position.

have autoconf provide device_mainbus() which looks up mainbus_cd,
and use that instead.

discussed with deraadt who just wants mpath stuff to move forward
despite there being many ways to shine this particular turd.


Revision tags: OPENBSD_5_5_BASE
# 1.253 21-Feb-2014 deraadt

Do the lid control further on in the resume sequence, since this is
AML, and will do WAITOK operations. While cold, that is a bad idea.
We seem to be safe with respect to the spec, I do not see any event
debounce issues here.
ok kettenis, tested by many


# 1.252 20-Jan-2014 claudio

Instead of checking sc->sc_revision <= 1 check that the
fadt->hdr_revision <= 1. This is a better check since it checks
the version of the proper descriptor. This allows the alix APU
to reboot since those boards have currently a wrong sc->sc_revision.
Committing this now so that it can be tested easier.
With and OK jsg@ also fine by derradt@


# 1.251 24-Dec-2013 kettenis

If the FADT has its SMI_CMD set to zero, assume we're only ACPI-only hardware
and don't need to disable SMI ownership of the ACPI hardware registers.

ok mlarkin@


# 1.250 23-Dec-2013 kettenis

Revert previous commit. It makes resume hang on some MP systems running
GENERIC.MP.


# 1.249 16-Dec-2013 kettenis

Restore hw.setperf upon resume like we do for apm(4) on i386. Seems we need
to do this fairly late in the resume to avoid hangs.

ok deraadt@, mlarkin@


# 1.248 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


# 1.247 06-Nov-2013 mpi

Add support for Power Resources for Dx states and the necessary hook
for PCI devices. This hook should be called twice, before and after
changing the power state of a PCI device.

Before setting the device to the new state, the ACPI layer will notify
every power resources linked to the device for that state and make sure
they are turned "_ON". After changing the state of the device, it will
decrement the reference of every power resources linked to that device
for the old state and turn them "_OFF" if they are no longer referenced.

This fixes the no-USB after resume problem seen on various ThinkPad,
problem initialy diagnosed with Alexander Polakov.

ok kettenis@, deraadt@


Revision tags: OPENBSD_5_4_BASE
# 1.246 01-Jun-2013 mlarkin

Stop printing ACPI wakeup devices in dmesg after the 16th wakeup device.

Helps with VMware VMs that report hundreds of wakeup devices.

ok reyk@, deraadt@


# 1.245 31-May-2013 bluhm

Fix the build for a kernel without wd and pciide in its config.
OK deraadt@ tedu@


# 1.244 30-May-2013 deraadt

Enforce ca_activate tree-walks over the entire heirarchy for all events,
cleaning up some shutdown-hook related code on the way.
(A few drivers related to sparc are still skipped at kettenis' request)
ok kettenis mlarkin, tested by many others too


# 1.243 18-Apr-2013 deraadt

pin the acpi thread to the BSP, because AML and SMI cannot be trusted. I
was certain we were doing this already, but it turns out no...


Revision tags: OPENBSD_5_3_BASE
# 1.242 24-Dec-2012 guenther

In acpiioctl(), Return though the splx() path if suser() returns an error

ok deraadt@


# 1.241 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


# 1.240 04-Oct-2012 ehrhardt

Use information provided by ACPI to attach secondary PCI host bridges.

ok kettenis@


# 1.239 07-Sep-2012 kettenis

Add a new function called acpi_pci_min_powerstate() that returns the lowest
power state for a PCI device for the power state (currently S3 or S4) we're
transitioning into. This function is probably too simplistic; there is a lot
of (silly) complexity in the ACPI spec.

ok deraadt@


Revision tags: OPENBSD_5_2_BASE
# 1.238 13-Jul-2012 pirofti

Disable acpivideo(4) on ASUS laptops.

There are some models that panic whenever an event is received.
Some cope, some don't. In order to get this right acpiec(4) requires
major rework. Until then let machines boot.

Discussed with deraadt@.


# 1.237 11-Jul-2012 mlarkin

for now, restrict hibernate to root in the acpi ioctl kernel interface.
normal rules for controlling this interface through apmd still apply.

ok deraadt@


# 1.236 09-Jul-2012 deraadt

tiny little oops in untested hibernate block


# 1.235 09-Jul-2012 deraadt

clean up ioctl() case statements


# 1.234 09-Jul-2012 deraadt

acpi_disable_onegpe() is not used; ok pirofti


# 1.233 24-May-2012 kettenis

If _MIN > _MAX consider a WordBus resource to be invalid. Fixes a panic on
the IBM x3550-M4.

ok mikeb@


# 1.232 29-Mar-2012 mlarkin

Missing #ifdef in previous diff


# 1.231 29-Mar-2012 mlarkin

Prevent hibernation attempts on machines with unsupported disk controllers.
The same check is also performed later in the hibernate sequence, but by
then it's too late to cleanly unwind (presently).

ok deraadt


# 1.230 26-Mar-2012 deraadt

simplify conditionals in acpi_sleep_state()
ok mlarkin


# 1.229 26-Mar-2012 deraadt

hook in the hibernate request code; half of this diff is from mlarkin
ok mlarkin


Revision tags: OPENBSD_5_1_BASE
# 1.228 20-Sep-2011 deraadt

Very early in suspend while the clock is still ticking realtime,
save the clock back to the rtc. The zaurus already did this.
ok phessler miod kettenis


Revision tags: OPENBSD_5_0_BASE
# 1.227 02-Jul-2011 nicm

kqueue attach functions should return an errno or 0, not a plain 1. Fix
the obvious cases to return EINVAL and ENXIO.

ok tedu deraadt


# 1.226 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.225 16-Jun-2011 pirofti

Import acpitoshiba driver.

This is originally written by Hiroyuki Aizu, ported and integrated
in our tree by Javier Vazquez <javama4 AT gmail> with tweaks and
suggestions by me.

Import prodded by deraadt@ and the lack of testers on tech@.


# 1.224 27-Apr-2011 jcs

attach acpithinkpad to newer lenovo models like the x120e

ok deraadt@


# 1.223 22-Apr-2011 jordan

Fix uninitialzied variables and formatting strings (-Wxxx errors)


Revision tags: OPENBSD_4_9_BASE
# 1.222 02-Jan-2011 jordan

Re-add code for ACPI taskqueue, eliminates need for state vars in acpi softc
Serialize processing ACPI tasks for sleep/powerdown/GPE event
ok deraadt@


# 1.221 31-Oct-2010 guenther

Revert last commit: it breaks resume on Thinkpads
"then please back it out" deraadt@


# 1.220 26-Oct-2010 jordan

Add task queue for ACPI gpe and notify handlers
This fixes eject on Dell Latitude dock and an issue on the Dell Mini
battery update.
ok deraadt


# 1.219 07-Oct-2010 jordan

Create separate functions for enabling wake and runtime gpes.
Remove old suspend/resume gpewalk function
ok deraadt, mlarkin


# 1.218 05-Oct-2010 jordan

split acpi_enable_onegpe to separate enable/disable functions
ok deraadt


# 1.217 05-Oct-2010 jordan

Added acpi_disable_allgpes function to clear GPE status
ok deraadt


# 1.216 05-Oct-2010 deraadt

clean up acpi_softc use more


# 1.215 05-Oct-2010 deraadt

delete a bit of #if 0 code


# 1.214 05-Oct-2010 deraadt

hide use of our global acpi_softc pointer a bit better


# 1.213 29-Sep-2010 deraadt

grab the acpi thread lock very early in acpi thread startup; not that it
is likely that anyone will race us... but why be careless.
ok kettenis


# 1.212 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.211 29-Aug-2010 jasper

- add a check for FWRITE here too.

ok miod@


# 1.210 27-Aug-2010 kettenis

Make PCI Power Management optional, and only enable it when acpi(4) attaches.

ok deraadt@


Revision tags: OPENBSD_4_8_BASE
# 1.209 08-Aug-2010 kettenis

Close race between the acpi thread and wscons ioctls running in process
context. We might rip this out post-release and replace it with a solution
that serializes things in a more obvious way.

ok deraadt@, miod@ and "those crazy texans who ok anything"


# 1.208 08-Aug-2010 deraadt

Report the acpi sleep states when on a RAMDISK; ok krw


# 1.207 07-Aug-2010 kettenis

Enforce acpi register access rescrictions.

ok mlarkin@, deraadt@, tested by marco@


# 1.206 07-Aug-2010 deraadt

upon resume, notify all the acpiac and acpibat drivers to update
their status. some laptops do not do this automatically.
ok kettenis canacar


# 1.205 06-Aug-2010 marco

Always call _PSW on _LID devices that support it to enable lid open to
resume the machine. Conversely disable it when the machine wakes up.

Tested by several ok deraadt


# 1.204 06-Aug-2010 deraadt

As part of a revamp of the PM1/GPE code, I write this diff to look for
unmanaged EN & STS bits in the PM1 register at interrupt time and report
them. As a side effect this splits the STS acknowledgement into two writes
(for power, and sleep) instead of one. The printf that is added (to spot
unmanaged STS bits) has not yet been triggered as far as we know.

Before the "write to PM1 registers at the right offset" diff went in, this
was not neccessary. But newer thinkpads do not have a working soft power
button without this diff.

We have no idea why.
ok mlarkin kettenis


# 1.203 05-Aug-2010 deraadt

PM1 power button events were generating 'fake events' to acpibtn PWRB,
but if there is no such device these were being tossed. Instead, arrange
for all power button handlers to flag a softc variable which is processed
by the thread; so that one place will know to request a powerdown (psignal
to init)
ok marco mlarkin


# 1.202 05-Aug-2010 deraadt

rename a variable which is annoyingly poorly named


# 1.201 05-Aug-2010 mlarkin

Write to the correct offset for various ACPI pmreg registers. Writing to
the correct register does properly clear the wake status bits and fixed
event bits, which allows some machines to have more than 1 suspend cycle.

bug spotted by deraadt@

tested by deraadt@ and myself on a half-dozen machines and others on
hackers@

ok deraadt@, marco@


# 1.200 05-Aug-2010 deraadt

Fix the power button. The pm1 register is 16 bits in size.
My mistake, a while back.
tested by krw


# 1.199 04-Aug-2010 kettenis

Print supported sleep states.

ok marco@, phessler@, deraadt@


# 1.198 03-Aug-2010 mlarkin

Use the proper flag for re-enabling certain hardware events (power btn,
sleep btn). The (incorrect) flag prevoiously used coincidentally had the
same value, so this shouldn't cause any different behavior than before.

tested on a variety of machines (i386, amd64, sp, mp) - no behavioral
change seen

ok deraadt@


# 1.197 28-Jul-2010 marco

Make all gasio failures look and behave the same.


# 1.196 28-Jul-2010 mlarkin

Delay a little longer on suspend before giving up. Fixes machines that
sometimes don't suspend because the sleep transition takes too long
(symptoms like failing to sleep but the backlight stays on).

ok deraadt@


# 1.195 27-Jul-2010 deraadt

remove XXX; EC can now be attached early when needed; ok jordan


# 1.194 27-Jul-2010 deraadt

more brackets feels good


# 1.193 27-Jul-2010 deraadt

XXX annotate a block in acpi_enter_sleep_state() that is pretty worrying


# 1.192 27-Jul-2010 deraadt

horrific indentation that nearly made me puke


# 1.191 27-Jul-2010 mlarkin

Set the sleeping indicator light on machines that support it, in case SMI
doesn't do it for us. Shows the 'moon' LED on older Thinkpads when
sleeping.

Tested on T43p, W500, T510 by me and on X61s by deraadt@.

ok deraadt@


# 1.190 27-Jul-2010 jordan

Early initialization of acpiec if ECDT table exists
Fixes hang when booting thinkpads while docked
ok deraadt


# 1.189 26-Jul-2010 deraadt

remove merge error which was commited


# 1.188 26-Jul-2010 pirofti

Add support for Sony ACPI hotkeys via a new driver: acpisony(4).

Currently it only works for the suspend button and tries to do right for
the brightness events, but I haven't found a Sony laptop that like to do
right so far.

In the future I want to make the brightness keys work on all Sony's and
also add support for the zoom hotkeys and whatever other funky keys I can
find on those things.

Okay deraadt@.


# 1.187 22-Jul-2010 deraadt

pretty large cleanup of SMALL_KERNEL


# 1.186 21-Jul-2010 deraadt

in the acpi GPE handler, late ack level interrupts not edge interrupts.
Duh.


# 1.185 21-Jul-2010 deraadt

Merge the edge and level gpe handlers into one (now possible since the
gpe structure tells us whether the gpe is edge or level)


# 1.184 21-Jul-2010 deraadt

delete #if 0 code we are not going to use; ok marco


# 1.183 20-Jul-2010 deraadt

make the mask code for acpi_enable_onegpe() match other similar code


# 1.182 20-Jul-2010 deraadt

Handle the acpi interrupt controller with proper edge/level handling,
wrapped in spltty to avoid racing against the interrupt controller.
Repair the gpe bit masking code while there, and do operations in the
right order
ok kettenis mlarkin, help from jordan at figuring out the order of
operations.


# 1.181 20-Jul-2010 matthew

When we're preparing to go to sleep, only call bufq_restart() if other
preparations failed.

ok kettenis@, thib@, deraadt@


# 1.180 19-Jul-2010 deraadt

permit compilation with ACPI_DEBUG


# 1.179 19-Jul-2010 deraadt

there is no need for a seperate dsdt_softc pointer; dsdt does not have a
softc but is just a part of the single acpi driver.
ok jordan


# 1.178 19-Jul-2010 deraadt

acpi_delay() is no longer needed; ok jordan


# 1.177 18-Jul-2010 mlarkin

Clear event status on resume, to avoid going back to sleep/reboot/shutdown
immediately after resume. Tested by a bunch of people on hackers@ on
various hardware. Fixes at least Toshiba NB200 and Gateway LT3103u.

ok marco


# 1.176 13-Jul-2010 deraadt

rename some thread related interfaces so they are logical


# 1.175 10-Jul-2010 jordan

Verify _ADR bdf is correct (some systems return 0xffff)
Works on Dell x9xx and Studio 1555


# 1.174 09-Jul-2010 jordan

Clear the noise


# 1.173 08-Jul-2010 jordan

Add mapping for ACPI device to PCI bdf (match autoconf tree)
Simplify resource parsing function to use buffer argument
Convert namespace linked lists to use queue macros
ok marco@, deraadt@


# 1.172 06-Jul-2010 deraadt

Use spltty() to lock downcalls from apm against the information being
modified by the acpi thread.
ok kettenis


# 1.171 06-Jul-2010 deraadt

Don't use a workq to do the suspend, because it races aginst the acpi
thread. Instead, just tell the acpi thread to do the suspend for us.
This makes apmd & zzz work correctly. While here, have acpithinkpad
attempt to post the event to apm before prompting the suspend itself.
ok kettenis marco mlarkin


# 1.170 05-Jul-2010 mlarkin

Reset register SCI_EN on resume. The ACPI spec clearly states we don't have
to do this, but many machines don't, or forget to, which leads to all sorts
of problems on resume (broken battery stats, overheating, failure to resume
more than once, etc).

tested by many, ok marco@, deraadt@


# 1.169 01-Jul-2010 thib

do bufq quiesce again, since vnd/the bufq_done call in
biodone has been fixed.


# 1.168 01-Jul-2010 jordan

Backout recent AML changes, breaks on R210 and others


# 1.167 01-Jul-2010 jordan

Simplified aml_parse_resource call, cleanup code
ok marco


# 1.166 01-Jul-2010 jordan

Fix subordinate bus # for multi-root PCI buses
ok marco, kettenis


# 1.165 30-Jun-2010 thib

Disable/partially backout the bufq quiesce changes as this
is causing havoc with vnds and release must be buildable.


# 1.164 29-Jun-2010 deraadt

Fix ramdisks (new code was placed inside a SMALL_KERNEL chunk)


# 1.163 29-Jun-2010 jordan

Add support for mapping ACPI to PCI devices
ok kettenis, deraadt


# 1.162 29-Jun-2010 kettenis

Call bufq_quiesce() on suspend to stop all I/O and bufq_restart on resume.
Makes suspend/resume work much better while doing disk I/O.

ok mlarkin@


# 1.161 29-Jun-2010 tedu

60 is a more reasonable guess for number of minutes in an hour than 100.
spotted by TeXitoi. ok deraadt


# 1.160 27-Jun-2010 jordan

Add _acpi_matchhids routine, precursor to pci device match


# 1.159 27-Jun-2010 mlarkin

Clear ACPI fixed event status on resume (power buttons/etc) so that some
machines don't immediately resume after suspending to S3.

Tested by kettenis@ and I on 5 or 6 machines, deraadt@ "yeah, looks good"


# 1.158 27-Jun-2010 jordan

Cleaned up acpi_maptable, adds to table list directly


# 1.157 07-Apr-2010 deraadt

If we have done a wsdisplay_suspend(), and ACPI decides it does not want to
suspend, we must roll back by calling wsdisplay_resume() or the virtual
console switching will be locked forever
ok oga


# 1.156 07-Apr-2010 kettenis

Make suspend/resume work on MP machines (running an MP kernel). Joint work
from mlarkin@ and me, with some amd64 fixes thrown in by deraadt@

ok marco@, deraadt@, pirofti@, mlarkin@


# 1.155 31-Mar-2010 kettenis

Bring /dev/apm support in line with apm(4). Make the suspend button send
a suspend request event to apmd(8) instead of suspending immediately. Also
keep track of whether /dev/apm and /dev/apmctl are currently open, such that
we can still suspend immediately if apmd(8) isn't running.

ok deraadt@, marco@, pirofti@, jsing@, oga@


# 1.154 30-Mar-2010 oga

Prevent the apmd/x races for good.

When we hit suspend time, go through all wsdisplays on the system. if
they are in mode MAPPED, but not MODE_DUMBFB then if possible do a full
vt switch to a !mapped vt, and prevent switching back until resume time.
This has to be called from MD code because this involves userland
running so that X can run the vt switch signal handler. This way, any
case where we are using the "poke registers from userland" model, we
will not be on the hardware when we go down, so the kernel can actually
handle thing properly.

Tested on several acpi laptops (by kettenis@ and ian@), x40 (me and
beck@ at LEAST) and zaurus (me). Maybe others, but if so I forgot who at
this time..

Idea from deraadt somewhere over the Faroe Islands (I thought of a
similar thing myself a while ago). Much prompting from him. Ok and
comments miod@


# 1.153 25-Mar-2010 oga

Fix & vs. && in the kqfilter code.

Based on a comparison with the apm code.

ok deraadt@, kettenis@


Revision tags: OPENBSD_4_7_BASE
# 1.152 26-Nov-2009 mlarkin

Visual (and on some machines, audio) feedback while the machine is
resuming via calls to aml _SST method. This also makes the "moon" LED on
thinkpads to blink during resume and cease when resume is completed.

ok pirofti@


# 1.151 26-Nov-2009 deraadt

Toggle into "cold" when we we disable interrupts, because tsleeping
in the AML interpreter and context switching into userland processes
tends to sometimes pop the ACPI trampoline page out of our address
space. Makes lots more machines work.
ok kettenis mlarkin


# 1.150 24-Nov-2009 jsg

in the resume path evaluate _WAK before dealing with device drivers
like the spec says, not after.

ok mlarkin@


# 1.149 24-Nov-2009 deraadt

abort suspends on SMP systems until that gets written; ok mlarkin


# 1.148 23-Nov-2009 mlarkin

pci should return the result of it's children's suspend/resume calls back
to its parent.

handle suspend failure case and unwind if devices have an activate function
that returns failure.

ok deraadt@, kettenis@


# 1.147 23-Nov-2009 mlarkin

acpi* devices need to call their children on suspend/resume.
ok deraadt@


# 1.146 23-Nov-2009 pirofti

Remove ACPI_SLEEP_ENABLED checks.

This enables by default the suspend/resume paths in the kernel.

Okay deraadt@.


# 1.145 23-Nov-2009 deraadt

knf and spacing that are super annoying


# 1.144 23-Nov-2009 mlarkin

Hook up Theo's new children suspend/resume function, as well as more ACPI
suspend/resume glue.

ok deraadt@


# 1.143 26-Oct-2009 deraadt

Do not do strncmp comparisons on dv_xname because longer device names which
look similar could arrive in the future. Instead, compare directly against
dv_cfdata->cf_driver->cd_name
Issue originally spotted by miod


# 1.142 26-Oct-2009 jordan

Verify checksum+address when loading ACPI tables.
Some systems had invalid entries in RSDT/XSDT.
ok marco@


# 1.141 23-Jul-2009 cnst

New aibs(4) driver for ASUSTeK AI Booster (ACPI ATK0110) hardware monitoring,
with sensor state support through limits provided by the ACPI.

Tested on several ASUS motherboards kindly networked by Sam Fourman Jr.

ok deraadt marco jordan


Revision tags: OPENBSD_4_6_BASE
# 1.140 03-Jun-2009 pirofti

Import acpi power resource driver. This is needed for suspend/resume logic.

Okay jordan@ and marco@.


# 1.139 03-Jun-2009 pirofti

Add acpivideo support. This adds brightness support for all laptops
except thinkpads, they will use the acpithinkpad driver. The driver is
also hooked into wsconsole. So brightness can be adjusted via:

$ wsconsctl display.brightness=<percentage>

This is very helpfull on some laptops that have a nasty bios and get two
steps instead of one when pressing the brightness button.

Tested on various dell, fujitsu, acer, samsung and other laptops.

Okay marco@, miod@. Suggestions from kettenis@.
Lots of reviews and help from miod@, thanks!


# 1.138 03-Jun-2009 jordan

Added ejectable bay for IDE cdroms
ok marco@


# 1.137 30-Apr-2009 marco

Oga changed apm ioctls and forgot to grep. Temporary work around by hitting
the sleep button when we get any request. This interface will chnage at
some point.
ok oga


# 1.136 19-Apr-2009 krw

Call acpi_sleep_walk() when powering down just like when entering
any other sleep state. This tells acpi wake devices to not wake up
the box until manual powering up resets their status. Makes my Dell
GX520 stay powered off in response to 'halt -p'.

In snaps for a while.

ok marco@ (several times) deraadt@


# 1.135 17-Apr-2009 pirofti

Add device ranking support for future sleep/wake developments.

okay marco@


# 1.134 11-Mar-2009 jordan

Cleanup.. use aml_evalinteger instead of aml_evalname
ok marco@


# 1.133 10-Mar-2009 jordan

Fix cut/paste error.. call _BFS not _PTS
ok marco@


Revision tags: OPENBSD_4_5_BASE
# 1.132 21-Feb-2009 marco

overzealous cut/paste; found by Vladimir Kirillov <proger@uaoug.org.ua>


# 1.131 20-Feb-2009 marco

Oops didn't mean for Debugger() to remain there.


# 1.130 19-Feb-2009 marco

suspend/resume bits so that we can develop this in tree. This is disabled.
code from mlarkin and me
help from art,toby,jordan and several others
ok jordan, go for it deraadt


# 1.129 10-Feb-2009 jordan

Fix null pointer for shutdown if acpi not enabled
ok marco@


# 1.128 04-Feb-2009 kettenis

Some systems that claim to be ACPI 2.0 apparently don't provide an XSDT
address. Make the code fall back on the RSDT on those systems.

From Joerg Niendorf <p3y80@internode.on.net>. Fixes PR 5986.


# 1.127 17-Jan-2009 krw

A couple of stray malloc/memset -> malloc(M_ZERO), a bit of knf and
lose a couple of unnecessary casts. No functional change.

Feedback from kettenis@

ok kettenis@ marco@


# 1.126 06-Nov-2008 marco

Collapse all sanity checks of hids into one function.

Originaly from jordan


Revision tags: OPENBSD_4_4_BASE
# 1.125 02-Jul-2008 fgsch

add acpivideo. for now almost an empty skel so others can work on
suspend/resume. eventually it will also manage output switching and
brightness where supported. prodded by marco@


# 1.124 11-Jun-2008 marco

Fix $OpenBSD$ for once and for all


# 1.123 08-Jun-2008 deraadt

aaa_dev was being set from an uninitialized variable; ok toby


# 1.122 07-Jun-2008 marco

Kill ACPIVERBOSE. It was never used and is worthless to boot.

comments jmc
ok toby


# 1.121 06-Jun-2008 marco

Shrink acpi some more for floppies and while here re-enable acpi on the
floppies. Fixes crazy sony vaio kettenis has.

tested by deraadt
ok deraadt jordan kettenis


# 1.120 01-Jun-2008 marco

Fix \n nit in SMALL_KERNEL.


# 1.119 01-Jun-2008 marco

Re-enable new parser. Lots of testing by various folks. Fits all media
now too.

ok kettenis deraadt


# 1.118 16-May-2008 dlg

back out jordans new parser.

it didnt have enough oks from the right people, it did break machines
that were previously working, and it bloated the RAMDISKS and install
media beyond the constraints of the media.

requested by deraadt@


# 1.117 14-May-2008 jordan

Adding new ACPI Parser code
Old guts of code still exists, needs to be torn out next
ok marco@
ok brad@


# 1.116 27-Apr-2008 jcs

add acpithinkpad(4), an acpi support driver for ibm/lenovo
thinkpads.

provides functionality that the bios did on older, non-acpi
thinkpads like respond to screen brightness keys and toggle the
bluetooth device on and off.

ok deraadt@ and marco@


# 1.115 24-Apr-2008 jsing

Introduce acpiasus(4), a driver for the ACPI based hotkeys found in many
ASUS laptops (including the ASUS EeePC) - largely based on NetBSD's
asus(4) driver. On the ASUS EeePC this allows us to enable/disable
wireless, change screen brightness and use the volume keys.

ok jsg@, weingart@


# 1.114 12-Apr-2008 kettenis

Get rid of acpi_s5 global variables; simply send SIGUSR2 instead of SUGUSR1
to tell init(8) to power down the machine.

ok krw@


Revision tags: OPENBSD_4_3_BASE
# 1.113 21-Jan-2008 jsg

Check against RESET_REG_SUP in FADT before doing the acpi reset dance
after all, not doing so appears to break more machines than it fixes.

ok marco@


# 1.112 05-Jan-2008 kettenis

Never attach more acpicpu(4) devices than cpu(4) devices. Always attach
the first acpicpu(4) device on non-MULTIPROCESSOR kernels, regardless of its
ACPI processor ID.

ok mikeb@


# 1.111 05-Dec-2007 deraadt

Hang acpi(4) below bios(4) instead of mainbus(4). This lets us move acpi(4)
before pcibios(4), and then.... if acpi(4) attaches, skip pcibios(4) since
it messes the machines up. Fixes claudio's HP dl320 G5, and almost assuredly
others.
ok kettenis


# 1.110 05-Dec-2007 deraadt

repair acpiopen/acpiclose and friends to handle the /dev/apmctl interface,
so that apmd(8) can actually work correctly against acpi(4), and thus,
even do the fancy -C/-A stuff; also tested by jmc


# 1.109 02-Dec-2007 fgsch

fix the trailing space for real now.


# 1.108 02-Dec-2007 fgsch

avoid trailing spaces on output; marco@ ok.


# 1.107 27-Nov-2007 kettenis

Fix off-by-one that made us attach acpicpu0 and acpicpu1 on GENERIC kernels.

ok marco@


# 1.106 25-Nov-2007 jsg

When available and ACPI is enabled use the ACPI reset method
to reboot. This is required for some quirky sis machines
and likely others to reboot.

Tested by many. 'commit early commit often' deraadt@


# 1.105 19-Nov-2007 deraadt

sigh, amd64 still uses that stupid acpi_enabled variable...


# 1.104 19-Nov-2007 deraadt

softc's come initialized to 0, you know...


# 1.103 19-Nov-2007 deraadt

store revision; from jsg


# 1.102 19-Nov-2007 kettenis

Initialize sc_facs earlier.

ok deraadt@, beck@, canacar@


# 1.101 16-Nov-2007 deraadt

fix the bus_space #define nightmare, so that amd64 and i386 are much more
uniform. as a result shared code like acpi needs less #ifdef's
ok marco kettenis


# 1.100 15-Nov-2007 deraadt

remove the #ifdef ACPI_ENABLE. the EC codepath is either safe enough (and
will be improved), or it is unsafe because AML assumes it must be there
ok almost everyone


# 1.99 15-Nov-2007 mikeb

Make acpicpu(4) attach to the existing CPUs only.

Problem reported by form@. Fix idea by kettenis@.
Lots of help from deraadt@. Tested by deraadt@, form@ and me.

ok deraadt marco


# 1.98 14-Nov-2007 deraadt

spacing


# 1.97 12-Nov-2007 deraadt

correct : vs , in attach-time messages


# 1.96 06-Nov-2007 deraadt

all ACPI_ENABLE kernels need acpi_interrupt()


# 1.95 06-Nov-2007 deraadt

move acpi_read_pmreg() and acpi_write_pmreg() out of SMALL_KERNEL so
that acpi ramdisks can compile (obvious solution)


# 1.94 05-Nov-2007 canacar

Establish the acpi interrupt earlier. Fixes problems with some
GENERIC.MP kernels not getting acpi interrupts. This is a
workaround until the real issue with late establishment is fixed.
ok weingart@, beck@, gwk@


# 1.93 03-Nov-2007 jordan

Added support for displaying wakeup devices
ok beck@,weingart@,gwk@


# 1.92 11-Oct-2007 kettenis

When walking the tree yo find _PRT methods, check whether the device is
actually enabled and functioning. Similar to what we do for _INI.

ok weingart@, beck@


# 1.91 08-Oct-2007 krw

More simple memset(,0,) -> M_ZERO changes. In this batch move to
size(*p) as the first malloc() parameter where p is declared locally
and thus easy to check. Add M_ZERO to gpe_table allocation in acpi.c
even though there is no obvious bzero or memset nearby.


# 1.90 13-Sep-2007 mk

Remove unnecessary memset() (see aml_evalnode(9)) and a bit of KNF.

ok toby


# 1.89 13-Sep-2007 weingart

Implement ACPI 6.5.1 spec tree walk for _STA and _INI.
Fixes mk's laptop. No regressions so far (thank you to the
testers).

ok gwk@, mk@, marco@


Revision tags: OPENBSD_4_2_BASE
# 1.88 17-Apr-2007 mk

Now that acpidock no longer hangs machines during boot (at least
according to the reports I've been getting) it should be safe to enable
it again, so remove the #if 0 stuff around it.

ok marco


# 1.87 11-Apr-2007 jordan

Added changes for C-State
ok marco@


# 1.86 26-Mar-2007 marco

Calculate minutes left more accurately.

From giovanni <qgiovanni@gmail.com>


Revision tags: OPENBSD_4_1_BASE
# 1.85 03-Mar-2007 mk

Stop looking for acpidock until we fix that stuff that breaks all pre-60
series ThinkPads with acpidock(4) enabled.


# 1.84 22-Feb-2007 marco

We were getting no interrupts in .MP. There are 2 fixes; one in
acpi_machdep.c and move the interrupt establishment down after ioapic
has been initialized.

ok jordan


# 1.83 21-Feb-2007 kettenis

Make an acpi-enabled GENERIC.MP work on systems that only support PIC mode.

ok marco@


# 1.82 19-Feb-2007 jordan

Added acpi_map_address function for mapping GAS/IO registers
ok marco@


# 1.81 18-Feb-2007 jordan

Added dynamic perfect hash for opcodes
ok marco@


# 1.80 18-Feb-2007 marco

Shave off another K pre compression.
Shuffle SMALL_KERNEL functions around so that we only have a single #ifndef


# 1.79 17-Feb-2007 marco

Add SMALL_KERNEL throught acpi to make it fit on boot media.
ok deraadt


# 1.78 13-Feb-2007 marco

Add smarter _INI code. This helps with some dissapearing devices.

ok kettenis jordan


# 1.77 31-Jan-2007 gwk

Introduce acpi_hasprocfvs thats "ACPI Has Processor Frequency and Voltage
Scalling" a global flag set by acicpu attach to indicate that ACPI is
handling fvs through the _PSS objects. This will fix disappearing
EST/powernow when booting kernels with acpi enabled, as acpicpu isnt
compiled into GENERIC yet. Also in cases where acpicpu is built in and
legacy EST/powernow work but acpi is missing the _PSS object, the legacy
routines will be used.

tested by dim@, and Nick Nauwelaerts <nick AT nauwelaerts.net>
ok marco@, dim@


# 1.76 22-Jan-2007 mk

Actually look for docking stations by looking for _DCK nodes.

ok marco


# 1.75 02-Jan-2007 marco

On some systems EC must be attached as the first device. Move EC from
generic _HID devices to its own routine to force first attach. Bug found
and debugged by joris@

Code mostly written by joris@


# 1.74 26-Dec-2006 marco

Add polling to devices that require it such as acpibat and acpitz.
Use the same tsleep/wakeup mechanism as the interrupt code to keep it non-
concurrent.


# 1.73 21-Dec-2006 deraadt

even more knf love


# 1.72 21-Dec-2006 marco

Remove some leftover acpiec gpe/interrupt code.


# 1.71 20-Dec-2006 deraadt

oops, name already used


# 1.70 20-Dec-2006 deraadt

rename __acpi_enable_gpe(); ok marco


# 1.69 20-Dec-2006 deraadt

acpi_unmap_pmregs() is not used, ok marco


# 1.68 19-Dec-2006 deraadt

simplify some low-level methods substantially (shrinking code); ok marco jordan


# 1.67 06-Dec-2006 mk

malloc(..., M_NOWAIT) with no NULL check is bad. If allocation fails,
complain, clean up, and bail out of attach.

input and ok gwk


# 1.66 28-Nov-2006 jordan

Fixed clearing arg for acpi_init_pic
ok kettenis@


# 1.65 27-Nov-2006 kettenis

Don't spam dmesg.


# 1.64 27-Nov-2006 jordan

Fixes for acpiprt routing
Post-parse fixup for namerefs
ok kettenis@


# 1.63 25-Nov-2006 marco

Don't handle acpiev events inside the gpe handler since this causes deadlocks.

ok jordan@


# 1.62 15-Nov-2006 kettenis

Hook up acpi apic interrupt routing support to generic acpi code.

ok jordan@


# 1.61 02-Nov-2006 marco

Kill acpiec attach function and add its functionality to the generic _HID
device attach

prodded and ok jordan


# 1.60 02-Nov-2006 marco

Fix comments to reflect reality.


# 1.59 19-Oct-2006 marco

Replace ACPI ioctls with APM ioctls. This makes ACPI talk the same protocol
as APM therefore userspace can now retrieve power information in the same
fashion as all other apm arches (zaurus, macppc, i386 etc).

To use force the use of acpi device like so: apmd -f /dev/acpi

ok jordan@


# 1.58 19-Oct-2006 jordan

Fixed GPE debug levels
Fixed ref counter on temporary bufferfields
DSDT parsing fully working on IBM Thinkpad
ok marco@


# 1.57 15-Oct-2006 jordan

Removed old GPE handler code remnants
ok marco@


# 1.56 12-Oct-2006 jordan

Massive ACPI changes
Fixed ACPI leaks
new ACPI API
ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.55 30-Jun-2006 gwk

KNF, No functional change,
"go at it" marco@


# 1.54 30-Jun-2006 jordan

Support for calling Embedded Controller via new GPE interface
ok marco@


# 1.53 30-Jun-2006 jordan

Added new code for GPE handling
Also works with ACPIEC; need to fixup acpiec.c to remove original interrupt handler
ok marco@


# 1.52 30-Jun-2006 jordan

Added offset argument for acpi read/write pmreg routines (new GPE code)
ok marco@


# 1.51 31-May-2006 todd

do not call acpi_powerdown() unless acpi has successfully initialized
ok gwk@ canacar@


# 1.50 29-May-2006 canacar

Add embedded controller support.
ok marco@ deraadt@ testing & ok gwk@


# 1.49 19-May-2006 canacar

Minimal thermal zone driver. Monitors thermal zone temperature,
shuts down the system if the 'critical temperature' is reached.
ok marco@


# 1.48 11-Apr-2006 marco

KNF.


# 1.47 11-Apr-2006 gwk

Add acpi_resume(struct acpi_softc *sc), which calls some AML methods,
detected at attach time, currently unused.

ok marco@


# 1.46 11-Apr-2006 gwk

Discover some ACPI sleep related methods, and call them at the
appropriate time in acpi_enter_sleep_state().

ok marco@


Revision tags: OPENBSD_3_9_BASE
# 1.45 26-Feb-2006 marco

Add acpicpu as a device.

ok jordan@


# 1.44 23-Feb-2006 marco

remove dead function.

ok jordan@


# 1.43 22-Feb-2006 marco

Print out all tables it found during autoconf. We'll need this data in
the future.


# 1.42 22-Feb-2006 marco

Make dmesg less verbose.

ok jordan.


# 1.41 22-Feb-2006 jordan

Added parsing of SSDT tables
ok marco@


# 1.40 22-Feb-2006 jordan

Added new aml_register to notify by device id
Moved powerdown code to button device
ok marco@


# 1.39 21-Feb-2006 marco

Readjust debug levels that crept in too low over the last few days.
KNF


# 1.38 21-Feb-2006 marco

Rig the power button so that the machine shuts down correctly.


# 1.37 20-Feb-2006 marco

Add acpibtn device. Remains disabled for now.


# 1.36 19-Feb-2006 jordan

Added code for aml_notify GPE notification callback
ok marco@


# 1.35 19-Feb-2006 grange

Fix acpi_enter_sleep_state() by replacing current ugly hack
with mostly correct sleeping state transition code.

ok brad@ marco@


# 1.34 19-Feb-2006 marco

jordan:
Enable interrupts.
Added peripheral code to make this work.

marco:
Replace softintr and other types of interrupts with a handler with context.
Fixed interrupt magic for GPEs.
Some KNF

This is work done by jordan and I but has to be a single commit.


# 1.33 17-Feb-2006 marco

Remove earlier introduced delays. We really only need them during autconf.

ok jordan@


# 1.32 17-Feb-2006 marco

Add a generic delay function as per the ACPI spec.

from jordan@


# 1.31 17-Feb-2006 marco

Use tsleep instead of delay when !cold.


# 1.30 17-Feb-2006 marco

Argh, we need delays between reads/writes. The backend reads (i2c most likely)
are so slow that values come back as 0 sometimes. We have to do this
despite the spec saying we shouldnt have to. Guess having a faster
parser than others can hurt sometimes.

Idea from jordan.


# 1.29 17-Feb-2006 jordan

Fixed code for handling _INI
Rearranged debug statements
ok marco@


# 1.28 16-Feb-2006 jordan

Added stub for calling _INI
Fixed aml_dowhile for NULL return
ok marco@


# 1.27 16-Feb-2006 jordan

Fixed parser/evaluator to retrieve battery info+state
Cleanup of parser
Dell laptops return battery status
ok marco@


# 1.26 06-Feb-2006 brad

fix a typo so this will compile on amd64.


# 1.25 04-Feb-2006 marco

When ACPI_ENABLED wasn't set but the devices were it had an unknown variable.

Pointed out by ian@.


# 1.24 03-Feb-2006 jordan

Rewrote aml parser yet again...
Fixed bus_space_map for i386/amd64
Added _BST and _BIF to acpibat to get status/info
ok marco@


# 1.23 20-Jan-2006 jordan

More evaluator changes
Removed old evaluator code
Fixed _HID value
ok marco@


# 1.22 19-Jan-2006 jordan

Added support for read/write of PCI Config space
Help jason@
ok marco@


# 1.21 18-Jan-2006 jordan

Added new evaluation routines
Added helper methods for buffer/field evaluation
Fixed broken indentation on dsdt.c

ok marco@


# 1.20 17-Jan-2006 jordan

Major overhaul of the aml parser. Evaluater temporarily disabled.

ok marco@


# 1.19 06-Jan-2006 grange

Check for ACPI control availability only if ACPI_ENABLE defined.


# 1.18 06-Jan-2006 grange

Informative error messages on attach, prettier dmesg.


# 1.17 06-Jan-2006 grange

printf -> dnprintf conversion for debug messages.


# 1.16 05-Jan-2006 grange

Find SLP_TYPx values and store them for future use in
acpi_enter_sleep_state().


# 1.15 05-Jan-2006 grange

Random KNF and cleanup


# 1.14 05-Jan-2006 grange

Provide symbolic names for the acpi sleeping states and use it.


# 1.13 28-Dec-2005 marco

Moved DEVNAME to a acpidev.h

ok jordan@


# 1.12 18-Dec-2005 sturm

add back comment about wrong code

ok marco


# 1.11 16-Dec-2005 marco

Remove test for kbd_reset. Prompted deraadt@, ok jordan@


# 1.10 16-Dec-2005 jordan

Added hooks for powerdown/power button press


# 1.9 16-Dec-2005 jordan

Cleaned up common power management register i/o
Added enter sleep state ioctl
Hooked power button interrupt
ok marco@


# 1.8 16-Dec-2005 marco

Remove autoconf chatter


# 1.7 16-Dec-2005 jordan

Moved DSDT AML parsing into parent ACPI driver
ok marco@


# 1.6 14-Dec-2005 marco

Add AC device glue.


# 1.5 14-Dec-2005 marco

Adjust debug printing levels to something more sane.


# 1.4 13-Dec-2005 marco

Add glue to attach devices to apci.


# 1.3 07-Dec-2005 marco

KNF


Revision tags: OPENBSD_3_8_BASE
# 1.2 10-Jul-2005 grange

Spacing cleanup.


# 1.1 02-Jun-2005 tholo

Start on a basic ACPI framework -- does not do much more than read out the
ACPI tables into kernel memory and attach ACPI and HPET timers currently.

In order to test this code, enabling the devices in GENERIC as well as
the ACPI_ENABLE option is needed. This code does not do any thermal
control yet, so this should be done with care depending on the platform.

In the tree so more people can contribute to making this more fully
featured.

Ok niklas@ grange@ tedu@


# 1.392 05-Dec-2020 kettenis

Add support for the _EVT method to the GPIO support code.

ok patrick@


Revision tags: OPENBSD_6_8_BASE
# 1.391 27-Aug-2020 jmatthew

Reorder the acpi attach process so we look at Processor() CPU nodes
after Device() ones, since we should prefer the newer node type.
If we see any Device() nodes, don't attach acpicpu(4) to any Processor()
nodes if they're also present. This also makes acpitz(4) and
acpipwrres(4) devices attach slightly later.

ok kettenis@, also tested by jmc@


# 1.390 16-Aug-2020 gnezdo

Remove sc_maxgpe from acpi_softc which was never read from

Remove a dead conditional from acpi_init_gpes while there.

ok jcs@


# 1.389 21-Jul-2020 deraadt

acpi can use IPL_BIO (a low interrupt) since it only enqueues operations for
later processing. The use of a high interrupt will predate suspend/resume
efforts, we had to redesign acpi to be non-reentrant obviously
discussed with kettenis, in snaps for more than a week


# 1.388 24-Jun-2020 cheloha

kernel: use gettime(9)/getuptime(9) in lieu of time_second(9)/time_uptime(9)

time_second(9) and time_uptime(9) are widely used in the kernel to
quickly get the system UTC or system uptime as a time_t. However,
time_t is 64-bit everywhere, so it is not generally safe to use them
on 32-bit platforms: you have a split-read problem if your hardware
cannot perform atomic 64-bit reads.

This patch replaces time_second(9) with gettime(9), a safer successor
interface, throughout the kernel. Similarly, time_uptime(9) is replaced
with getuptime(9).

There is a performance cost on 32-bit platforms in exchange for
eliminating the split-read problem: instead of two register reads you
now have a lockless read loop to pull the values from the timehands.
This is really not *too* bad in the grand scheme of things, but
compared to what we were doing before it is several times slower.

There is no performance cost on 64-bit (__LP64__) platforms.

With input from visa@, dlg@, and tedu@.

Several bugs squashed by visa@.

ok kettenis@


# 1.387 10-Jun-2020 jca

Give userland a consistent battery and A/C state after resume

Previously we refreshed the state of acpiac(4) and acpibat(4)/acpisbs(4)
by queueing fake ACPI events to be processed by the acpi thread.
acpibat(4) using ACPIDEV_POLL meant that its status could be refreshed
first, resulting in an APM_POWER_CHANGE event sent to userland before
the state of acpiac(4) was up to date. Because of this, apmd(8) could
see A/C unplugged when it was in fact plugged, possibly leading to
a wrong decision.

Instead of using fake ACPI events, refresh the status of acpiac(4) and
acpibat(4)/acpisbs(4) right away at DVACT_WAKEUP time. To reuse the
existing refresh code of those drivers, make their ACPI *_notify()
handlers responsible for sending APM_POWER_CHANGE events instead. Also
queue an APM_POWER_CHANGE event right after resume to retain existing
behavior.

This fixes the consistency of data shown to userland, and makes those
drivers less special.

Initially discussed last year (!) with mpi@ at Miod's place.
Feedback and ok jcs@, deraadt@ agrees with the use of DVACT_WAKEUP here.


# 1.386 29-May-2020 deraadt

dev/rndvar.h no longer has statistical interfaces (removed during various
conversion steps). it only contains kernel prototypes for 4 interfaces,
all of which legitimately belong in sys/systm.h, which are already included
by all enqueue_randomness() users.


# 1.385 14-May-2020 kettenis

Bring back the acpi(4) changes. This time around only use the ACPI _CRS
information on ACPI 5.0 and later.

ok krw@, patrick@


# 1.384 11-May-2020 jca

More accurate remaining power reporting with disparate batteries

Overall remaining power was computed as the average of the remaining
power of each battery, in percents. This doesn't work with batteries of
different capacity, so use the total capacity and remaining power
instead.

Reported by lidstah, successful tests by lidstah and benno@, ok kettenis@


# 1.383 08-May-2020 kettenis

Move parsing of _CRS into acpi(4) and pass the parsed address and interrupt
information in struct acpi_attach_args.

ok mpi@


Revision tags: OPENBSD_6_7_BASE
# 1.382 14-Apr-2020 kettenis

Implement acpi_getprop(), the ACPI equivalent of OF_getprop().

ok patrick@


# 1.381 12-Apr-2020 kettenis

Add support for the _CCA method. This method indicates whether DMA is
cache-coherent or not. To implement this, acpi(4) gets two bus_dma tags
and passes the appropriate one when attaching devices based on _CCA.
On i386/amd64, where for all practical purpose DMA is always cache-coherent,
the two tags are the same. But on arm64 they are distinct.

ok patrick@


# 1.380 07-Apr-2020 visa

Abstract the head of knote lists. This allows extending the lists,
for example, with locking assertions.

OK mpi@, anton@


# 1.379 03-Apr-2020 mpi

Fix incoherencies of errors returned by various *kqfilter routines.

EOPNOTSUPP is returned when there's no "filterops" for a given fd.
EINVAL when the requested filter isn't supported by the "filterops".
ENXIO means the underlying device is no longer valid/present.

ok visa@


# 1.378 20-Feb-2020 visa

Replace field f_isfd with field f_flags in struct filterops to allow
adding more filter properties without cluttering the struct.

OK mpi@, anton@


# 1.377 31-Dec-2019 visa

Use C99 designated initializers with struct filterops. In addition,
make the structs const so that the data are put in .rodata.

OK mpi@, deraadt@, anton@, bluhm@


# 1.376 31-Dec-2019 mpi

Convert infinite sleeps to {m,t}sleep_nsec(9).

ok kn@


# 1.375 31-Dec-2019 jsg

include sys/sensors.h for sensor use
don't depend on indirect inclusion from acpidev.h


Revision tags: OPENBSD_6_6_BASE
# 1.374 07-Sep-2019 kettenis

Revert acpipci(4) changes. Too many systems are broken. We'll try again
after 6.6 as been released. The acpireg.h change stays behind.


# 1.373 28-Aug-2019 kettenis

Use ACPI information to attach PCI busses like we do on arm64. There are a
few additional quirks though, and attaching the PCI busses is delayed to
replicate the existing code more closely. That may be changed in the
future. Also tweak how we handle MSI support and respect to ACPI flag
that says we shouldn't attempt to use MSIs.

Some fallout is expected.

ok patrick@


# 1.372 27-Aug-2019 deraadt

On amd64 the bootup and resume paths for SP/BP/SP were not performing
msr/pat, clock-startup, firmware upgrade, fpu initialization, and cpu
soft-state initialization in the same order. That kind of chaos is
crazy and fragile. Improve the acpi resume call sequence and unify order
of the operations.


# 1.371 02-Jul-2019 jcs

acpi_set_gpehandler: turn edge arg into flags, add support for
passing GPE_DIRECT which directly executes the supplied callback and
clears a level interrupt rather than going through acpi_taskq which
can be slow when other driver callbacks are in the queue.

This should not be used for GPE handlers that need to do anything
ACPI-related.

ok kettenis


# 1.370 10-Jun-2019 kettenis

Print proper ACPI version number.

ok mlarkin@


# 1.369 08-Jun-2019 kettenis

When establishing the mapping between ACPI device nodes and PCI devices,
skip PCI host bridges and devices that aren't there.

ok patrick@


# 1.368 07-Jun-2019 kettenis

Introduce acpi_getsta() and use it to eliminate some duplicated code.

ok patrick@


# 1.367 12-May-2019 kettenis

Make sure the extended PM register descriptions actually exist before
using them. Fixes machines with ancient ACPI 1.0 (such as recent
hypervisors).

ok deraadt@


# 1.366 12-May-2019 kettenis

Add support for mapping ACPI PM registers using the "extended" gasio
representation from the FADT. Mostly fixes the Lanner NCA-1510.

ok lteo@, mlarkin@, deraadt@


# 1.365 11-May-2019 lteo

Fix incorrect error message.

ok kettenis@


# 1.364 08-May-2019 tedu

free size. the allocation is the header plus the length.


Revision tags: OPENBSD_6_5_BASE
# 1.363 04-Apr-2019 kettenis

Avoid potential null-pointer dereference. Found by Kent R. Spillner.

ok deraadt@, jsg@


# 1.362 20-Jan-2019 jsg

fix SMALL_KERNEL build after changes in rev 1.361
reported by naddy@ ok deraadt@


# 1.361 19-Jan-2019 tedu

Add a pwraction sysctl that controls what the power button does on acpi.
By default, nothing changes -- shutdown is initiated. But allows turning
power button into a sleep button if desired.
(grudging) ok from a few parties


# 1.360 26-Oct-2018 kettenis

Add acpipci(4) on amd64. For now this only calls the PCI-specific _OSC
method to let the ACPI implementation know what features we support.


Revision tags: OPENBSD_6_4_BASE
# 1.359 30-Aug-2018 kettenis

Move softbutton check before the check that skips attaching certain acpi
devices. Fixes regression caused by matching on _CID in addition to
matching on _HID.

ok matthieu@, mlarkin@


# 1.358 25-Aug-2018 kettenis

Match ACPI devices based on _CID if no match for _HID is found.

ok mpi@


# 1.357 19-Aug-2018 kettenis

Add support for multiple PCI segments. Only really implemented for arm64
for now as amd64/i386 firmware still caters for legacy OSes that only
support a single PCI segment.

ok patrick@


# 1.356 03-Aug-2018 kettenis

Let ahci(4) match on _CLS instead of _HID when attaching at acpi(4). Avoids
having to add many more _HID entries to the match table.

ok deraadt@, mlarkin@


# 1.355 10-Jul-2018 kettenis

Make legacy interrupts work in acpipci(4).

ok patrick@


# 1.354 05-Jul-2018 kettenis

Add acpipci(4), a driver that supports generic ECAM-compatible PCI host
bridges based on information provided by ACPI.

ok mlarkin@


# 1.353 04-Jul-2018 kettenis

Properly pass around the PCI "chipset tag" in acpi(4) and refactor
acpimcfg(4) to call an MD initialization functions that sets up a tag for
PCI ECAM.

ok guenther@, mlarkin@, krw@


# 1.352 01-Jul-2018 kettenis

Move acpi(4) attach glue into acpi_machdep.c.

ok guenther@, deraadt@


# 1.351 01-Jul-2018 kettenis

Add support for _DSD Device Properties.

ok mlarkin@


# 1.350 30-Jun-2018 kettenis

Add intr_enable() function, intended for MI use to amd64 and i386 and use
this in the acpi(4) suspend/resume code paths.

ok deraadt@


# 1.349 29-Jun-2018 kettenis

Use <stdint.h> types.

ok mpi@, jung@, krw@, deraadt@


# 1.348 29-Jun-2018 mlarkin

ACPI: Allow (with warning) GPE handler reassignment, instead of returning
a failure code.

ok kettenis


# 1.347 26-Jun-2018 mlarkin

knf


# 1.346 26-Jun-2018 kettenis

Let acpi(4) pass down the bus dma tag.

ok deraadt@


# 1.345 25-Jun-2018 kettenis

Reorganize acpi(4) code a bit in preparation for upcoming arm64 support.

ok deraadt@


# 1.344 20-May-2018 kettenis

Wake up the acpi thread after scheduling a gpio event.

ok mpi@


# 1.343 17-May-2018 kettenis

Sprinkle some #ifndef SMALL_KERNEL such that RAMDISK kernels cuild again.

pointed out by stsp@


# 1.342 17-May-2018 kettenis

Implement GenericSerialBus OpRegion support.

ok mlarkin@


# 1.341 27-Mar-2018 kettenis

Add acpicmos(4), a driver that implements SystemCMOS OperationRegion
access support. This fixes machines where the AML doesn't check whether
support for this OperationRegion type has been registered by the OS.

ok mlarkin@


Revision tags: OPENBSD_6_3_BASE
# 1.340 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.339 10-Feb-2018 deraadt

Syncronize filesystems to disk when suspending. Each mountpoint's vnodes
are pushed to disk. Dangling vnodes (unlinked files still in use) and
vnodes undergoing change by long-running syscalls are identified -- and
such filesystems are marked dirty on-disk while we are suspended (in case
power is lost, a fsck will be required). Filesystems without dangling or
busy vnodes are marked clean, resulting in faster boots following
"battery died" circumstances.
Tested by numerous developers, thanks for the feedback.


# 1.338 08-Feb-2018 deraadt

After flushing disks, try again to release all memory. We don't need to
save dirty memory to the hibernate space.


# 1.337 08-Feb-2018 deraadt

Toss all releaseable memory, because fragmentation can get in the way
of allocating the hibernate playpen.


# 1.336 08-Feb-2018 deraadt

sr_quiesce() is a new approach for ensuring that softraid drains
output to the disks.

This is part of a larger suspend/resume filesystem-safety diff, which
has been worked on for a couple of months already. Tests by job, krw,
beck, benno, and others. Sometimes even by snapshot users...


# 1.335 29-Nov-2017 kettenis

Add support for GPIO-signaled events.

ok mlarkin@


# 1.334 16-Nov-2017 jcs

Add PCI attachment for dwiic(4) needed by Intel 100 Series machines

ok kettenis


# 1.333 14-Nov-2017 kettenis

Add a few more PNP IDs to the skip lists.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.332 17-Aug-2017 stsp

Make dwiic(4) attach its ACPI parent dependencies.
This change makes the Asus E200HA keyboard work.

Original analysis and patch by Cesare Gargano
ok kettenis@


# 1.331 09-Aug-2017 dcoppa

Add "PNP0303" (8042 PS/2 Controller) to acpi_isa_hids[]

ok kettenis@


# 1.330 22-Jul-2017 jcs

acpibat: look for _BIX first before falling back on _BIF

ACPI 4.0 deprecated _BIF for battery status so some newer machines
are exporting _BIX which extends _BIF's package with a few extra
fields. When using _BIX, export the new discharge cycle count as a
sensor.

feedback from kettenis and anton
ok deraadt


# 1.329 20-Jul-2017 kettenis

Start a list of isa(4) device names such that we can avoid printing them
as "not configured".

ok mikeb@, millert@, deraadt@


# 1.328 08-Apr-2017 deraadt

Sensors are run as callbacks inside tasks. During suspend a sensor
could be running inside a driver that will be force-detached, or due
to tsleep end up disrupting the softstate/hardstate contract. At
suspend time, quisce all these callbacks by waiting for completion.
This issue has never been observed for real, but may be implicated in
suspend/resume failures.
ok kettenis guenther mlarkin


# 1.327 08-Apr-2017 deraadt

Two prototypes for acpi_maptable; merge to one place


Revision tags: OPENBSD_6_1_BASE
# 1.326 27-Mar-2017 deraadt

Now that hibernate_alloc() only has clean success/failure, don't
need to call hibernate_free() to clean up a partial mess.
ok mlarkin kettenis


# 1.325 13-Mar-2017 deraadt

allow ramdisks to build; ok jcs


# 1.324 12-Mar-2017 jcs

try to attach acpisbs first and if successful, don't attach acpibat


# 1.323 02-Mar-2017 natano

Add a new sysctl machdep.lidaction. The sysctl works as follows:

machdep.lidaction=0 # do nothing
machdep.lidaction=1 # suspend
machdep.lidaction=2 # hibernate

lidsuspend is just an alias for lidaction, so if you change one, the
other one will have the same value. The plan is to remove
machdep.lidsuspend eventually when people have upgraded their
/ets/sysctl.conf.

discussed with deraadt, who came up with the new MIB name
no objections mlarkin
ok stsp halex jcs


# 1.322 28-Feb-2017 natano

Untangle abstract sleep modes from ACPI sleep states. This paves the way
for further work in the area.

idea and ok deraadt
ok mlarkin


# 1.321 22-Feb-2017 jcs

add acpisbs, an acpi smart battery subsystem driver reading data
over smbus

currently disabled because it conflicts with acpibat

ok deraadt, kettenis


# 1.320 14-Jan-2017 kettenis

Revert aml_rdpciaddr changes; breaks several machines that were working
before.


# 1.319 11-Jan-2017 jsg

Increase the size of the buffers used for _HID and _CID strings to be
able to fit "Hyper_V_Gen_Counter_V1".

ok mlarkin@ deraadt@


# 1.318 08-Jan-2017 kettenis

Make aml_rdpciaddr() work for for devices behind bridges. The code now
uses the mapping between AML nodes and PCI devices that we establish.
Because _INIT methods may end up calling aml_rdpciaddr(), make sure we
create that mapping early. Also handle devices that aren't actually present.
These devices are now included in the mapping and reads will return an
all-ones patterm whereas writes are a no-op..

ok mlarkin@


# 1.317 25-Oct-2016 pirofti

Add the resource index to aml_parse_resource() callback function.

This is needed by an upcoming acpiec commit that handles machines breaking
the current ACPI specifications.

Change suggested by and ok kettenis@, guenther@


# 1.316 18-Sep-2016 guenther

If the APIC table (MADT) isn't present then ci_acpi_proc_id won't be set
when acpicpu tries to attach, leading to a NULL dereference. The ACPI
processor object on the Dells where this was reported is bogus and useless
anyway so apply the same test when deciding whether to attach to processor
objects as acpicpu would when matching them to cpus.

Problem reported by Eivind Eide (xenofil (at) gmail.com) and
Johan Huldtgren (johan+openbsd-bugs (at) huldtgren.com).
Testing by Eivind Eide.
ok kettenis@ mlarkin@


# 1.315 03-Sep-2016 naddy

Write the system time back to the RTC every 30 minutes.
This fixes the problem that long-running machines which were not
shut down properly would reboot with a badly offset system time.

hints and ok kettenis@


# 1.314 31-Aug-2016 mlarkin

Don't put a device in the wake device list if _STA says it's not there.

Fixes at least one machine that woke up from sleep / poweroff immediately.

ok kettenis@, tested by kettenis and martijn@


# 1.313 28-Jul-2016 kettenis

Store the acpi processor ID/UID in struct cpu_info, and use it to attach
acpicpu(4) drivers to the right cpu(4).

ok mlarkin@, guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.312 10-Jun-2016 kettenis

The power management timer has been made optional in ACPI 5.0A, so don't
attempt to attach acpitimer(4) if the timer isn't present.

Pointed out by jcs@

ok jcs@


# 1.311 08-May-2016 kettenis

Make sure devices listed by a device's _DEP method are attached before
we attach the device itself. The _DEP methode, introduced in ACPI 5.0,
is a hint that the device in question depends on OpRegion support from
the devices listed by _DEP, so we have to order them the proper way.
To prevent us from attaching those devices again when we encounter them
later walking down the device tree, keep track of our attempts to
attach a device driver to them using aflag in the aml_node structure.

ok guenther@


# 1.310 25-Apr-2016 pirofti

We attach acpihpet(4) and acpiec(4) without using the HID, so do not
report it in the dmesg.

OK deraadt@, mlarkin@.


# 1.309 02-Apr-2016 kettenis

Reduce dmesg spam by knocking out some acpi hardware IDs that don't need a
driver.

ok mpi@


# 1.308 29-Mar-2016 kettenis

Check if a device is present (using _STA) in acpi_foundhid. This prevents
us from reporting devices as "not configured" that aren't actually present,
and allows us to remove duplicated code from several drivers.

ok deraadt@, ok jsg@


# 1.307 29-Mar-2016 kettenis

Implement a cleaner way to prevent acpivout(4) from attaching if we have
a platform-specific method of controlling the backlight.

ok millert@, mlarkin@, jung@


# 1.306 28-Mar-2016 kettenis

Adding each and every new acpi device driver to acpi_foundhid() is
gettinga bit of the burden. And it really isn't how our autoconf
framework is supposed to work for busses that can be enumerated. All
the drivers already check for a matching _HID in their attach
function. So we can just drop the checks in acpi_foundhid() and let
the drivers sort themselves out.

Print a "not configured" line for devices for which we don't attach adriver.
This may be a bit much as there are devices for which we will not have a
driver. This will be removed if it causes too much dmesg spam.

ok deraadt@, mlarkin@


Revision tags: OPENBSD_5_9_BASE
# 1.305 17-Jan-2016 jsg

Take acpi_foundhid() outside of #ifndef SMALL_KERNEL so the ramdisk
kernel can find and attach dwiic(4) and sdhc(4).


# 1.304 17-Jan-2016 jsg

remove an unused global variable


# 1.303 14-Jan-2016 kettenis

Attach dwiic(4) on the Intel Bay Trail i2c controllers as well.


# 1.302 12-Jan-2016 kettenis

Improve emulation of PM registers on Hardware-redcued ACPI by emulating
SPL_TYPx, SLP_EN and WAK_STS using the new SLEEP_STATUS_REG and
SLEEP_CONTROL_REG registers. Makes the transition into S5 (powerdown) work
on the ASUS X205TA.

ok deraadt@, mlarkin@


# 1.301 12-Jan-2016 jcs

Add dwiic, a driver for the Synopsys DesignWare i2c controller found
on the Samsung ATIV Book 9 laptop. This initial version only
supports ACPI config/attachment.

Add ihidev, a HID-over-i2c driver largely based on uhidev. dwiic
handles attaching ihidev devices found in ACPI.

Add ims, a HID-over-i2c mouse/trackpad driver to get basic cursor
and button functionality from HID-compliant i2c trackpads.

ok kettenis deraadt


# 1.300 11-Jan-2016 kettenis

Make sdhc(4) attach to hardware IDs "80860F14" and "PNP0FFF".


# 1.299 09-Jan-2016 kettenis

Initial support for Hardware-Reduced ACPI. This skips the legacy mode checks
by emulating the PM_CNT register such that SCI_EN is always enabled. It also
skip attaching acpitimer(4) since the relevant registers are no longer there.

ok deraadt@, mlarkin@


# 1.298 06-Jan-2016 kettenis

Follow the spec and check the SCI_EN bit to see if ACPI has already
been enabled and don't bother enabling it again if it is. This should fix
systems that don't implement legacy mode and therefore set ACPI_ENABLE and
ACPI_DISABLE to zero.

ok mlarkin@, yasuoka@


# 1.297 23-Nov-2015 reyk

Back out the CRITICAL change, turn it back from 10% to 25%. Some
people with old/broken batteries didn't like this change, and the
underlying heuristics are wrong.


# 1.296 22-Nov-2015 reyk

The "CRITICAL" APM battery level, that is exported/faked by the ACPI
stack, is currently set to 25%. With 25% battery left, a typical
modern laptop will have at least one hour runtime left. It is an
arbitrary value, and other APM power layers in the kernel set CRITICAL
to something like 5% or 10%, so set it to 10% for now.

OK mlarkin@


# 1.295 28-Sep-2015 deraadt

In low-level suspend routines, set cold=2. In tsleep(), use this to
spit out a ddb trace to console. This should allow us to find suspend
or resume routines which break the rules. It depends on the console
output function being non-sleeping.... but that's another codepath which
should try to be safe when cold is set.
ok kettenis


# 1.294 06-Sep-2015 deraadt

clarify malloc arguments (base + length...)


# 1.293 28-Aug-2015 deraadt

fairly simple sizes for free(); ok tedu


# 1.292 20-Aug-2015 kettenis

Respect the access size when reading or writing to pci config space.
This version also makes sure writes are properly aligned, preventing
the panics seen with the previous incarnation of this change.

Fixes battery status passthrough in vmware, brightness keys on my x220
and who knows what else.

ok mlarkin@, deraadt@


Revision tags: OPENBSD_5_8_BASE
# 1.291 04-Aug-2015 deraadt

If we are setting ACPI_PM1_BM_RLD do it only once, and remember to
repeat this action at resume time.
ok mlarkin guenther


# 1.290 29-Jul-2015 deraadt

Something subtle in previous revision is locking up suspend or booting
on some laptops. Mailing lists threads mention x220 and Asus machines.
Reverting for now.


# 1.289 21-Jul-2015 kettenis

iRespect the access size when reading or writing to pci config space.
Fixes battery status passthrough in vmware and who knows what else.

Tested by many.

ok mlarkin@, deraadt@


# 1.288 17-Jul-2015 mlarkin

fix comments


# 1.287 26-Mar-2015 jsg

Use the 0x10 extended capability to determine if the trackstick
buttons are wired to the trackpad and need to be re-routed to the
trackstick. Without this change the buttons on 2015 thinkpads
get picked up as extended buttons that show up as scroll up/down.

Remove the X1 Carbon 2015 (LEN0048) and X250 (LEN0046) from the
top button area/soft buttons quirks list. Also avoid using the
quirk list entirely if the capability bit is set.

Based on work from jcs@ with feedback from mpi@
ok mpi@ jcs@


# 1.286 20-Mar-2015 kettenis

The ACPI standard says that a PCI function number of FFFF can be used to refer
to all functions on a device. This makes no sense in the context of accessing
PCI config space. Nevertheless stsp@ has a machine with AML that tries to
do config space reads from a device where _ADR encodes function number FFFF.
This is almost certainly buggy AML, but since Linux (and presumably Windows)
boot just fine on this machine we need to work around this. Linux effectively
treats function FFFF as function 7, which isn't present on this machine.
Instead we simulate a read from a non-existent device by returning a bit
pattern of all ones. Writes to function FFFF will still panic.

ok mlarkin@


# 1.285 16-Mar-2015 deraadt

Reduce use of SMALL_KERNEL in acpi, bringing acpiec driver and more gpe
handling into RAMDISK. This is now possible because the install media
has ample room. The goal is to reduce special cases where we may be
skipping (unknown) important operations...
ok mlarkin kettenis


# 1.284 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.283 07-Feb-2015 deraadt

New framework that allows hibernate to pass in entropy from it's fresh
boot.
ok mlarkin


# 1.282 06-Feb-2015 mlarkin

Fix a hibernate crash on some machines due to unmapping a page that
may not have been mapped previously (in the failure to hibernate case).

Also ensure that the lowmem ptp is mapped in all cases (not just MP).

ok kettenis


# 1.281 17-Jan-2015 deraadt

resume_randomness() before spinning up other cpus...


# 1.280 15-Jan-2015 jsg

Define a new wscons mouse type for Synaptics clickpad devices that lack
physical buttons. This will be used if the acpi pnpid for the mouse
matches a list provided by Synaptics found in Linux.

Combined with the xenocara changes this will remove the need for an xorg
config file for the x240/t440/t540 generation of thinkpads.

ok matthieu@ shadchin@ kettenis@


# 1.279 11-Jan-2015 kettenis

Tolerate checksum errors in ACPI tables (but complain about them). Apparently
there are machines out there with broken BIOSen with such checksum errors in
thier RSDT tables. Windows runs fine on these machines, so it probably does
not check. Linux ignores checksum errors as well (and prints a warning).

ok deraadt@, krw@


# 1.278 18-Dec-2014 deraadt

suspend and resume the RNG (at what looks like the right spots)
ok reyk djm


# 1.277 09-Dec-2014 doug

Replace some malloc(n*size,...) calls with mallocarray().

ok tedu@ deraadt@


# 1.276 06-Dec-2014 mpi

If "machdep.lidsuspend" is not set, do not go back to sleep if the lid
is still closed when resuming. This allows docked laptop to be resumed
without opening them.

ok mlarkin@


# 1.275 23-Nov-2014 mlarkin

If a machine has no _LIDs, then none can be open. This diff fixes a problem
where machines without any _LIDs were going back to sleep after hibernate
due to the recent suspend-if-lid-closed-on-resume diff. Noticed on qemu,
which doesn't have any _LIDs.

discussed with deraadt


# 1.274 18-Nov-2014 krw

Nuke yet more obvious #include duplications.

ok miod@


# 1.273 08-Nov-2014 mlarkin

If resuming from sleep (zzz/ZZZ) and the lid is still closed, go back to
sleep. This prevents accidental lid flex or slight opening in a backpack
from waking the machine up and leaving it resumed (powered on).

ok deraadt@


# 1.272 26-Sep-2014 kettenis

Rework piglet and pig allocation. Currently the piglet gets allocated
deep down in the suspend path, where it is really hard to recover from
allocation failure. So allocate the piglet early on in the suspend path.
Also change the piglet and piglet allocation functions to use km_alloc(9)
instead of doing pmemrange magic. This removes a bunch of code which, in the
case of the piglet allocation, is broken since it results in a NULL pointer
dereference. Also switch the piglet allocation to not wait. If we can't
allocate 16MB of phys contig memory on a halfway modern machine we're almost
certainly under a lot of memory pressure and we're better off not trying to
hibernate anyway.

ok mlarkin@


# 1.271 25-Sep-2014 kettenis

Bring back rev 1.249. Now that mp_setperf() has been fixed, it should be
safe to restore perflevel when resuming. Should fix the issues people
see with the processor running at full speed heating up the machine while
the apm command reports that the CPUs are running at a low speed.

ok dcoppa@, deraadt@


# 1.270 23-Sep-2014 kettenis

Release the acpi lock around calls to wsdisplay_suspend() and
wsdisplay_resume(). The X server may adjust the display brightness just
before/after the VT switch when suspending/resuming. In fact, it may
already be blocked adjusting the display brightness for some reason
unrelated to the suspend request.

The VT switch request will eventually time out, but the delay is annoying
and the failed VT switch will lead to problems after resume.

Needed for a future update of the xf86-video-intel driver.

ok mlarkin@


# 1.269 19-Sep-2014 kettenis

Use config_suspend_all(9).


# 1.268 08-Sep-2014 kettenis

Clear the PME Status bit on PCI devices in response to Device Wake events.
Fixes the interrupt storm on the 2009 Mac mini with WOL enabled on its
nfe(4) interface.

ok mlarkin@


Revision tags: OPENBSD_5_6_BASE
# 1.267 20-Jul-2014 mlarkin

Support hibernating to softraid crypto volumes.

much help and ok from deraadt@


# 1.266 16-Jul-2014 mlarkin

Reenable hibernate RLE support and flush+zero all memory after unpack.


# 1.265 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.264 12-Jul-2014 kettenis

Add a function to drop all clean pages on the page daemon queues and call
it when we hibernate.

ok mlarkin@, miod@, deraadt@


# 1.263 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.262 11-Jul-2014 mlarkin

Flush the buffercache to 16MB on hibernate and restore its previous max
size (kern.bufcachepercent) on resume, for better hibernate performance.

ok beck@


# 1.261 10-Jul-2014 blambert

remove unneeded header

ok mlarkin@


# 1.260 01-Jul-2014 kettenis

Adding an additonal delay after each access to SystemMemory or SystemIO
during autoconf makes no sense.

ok deraadt@


# 1.259 23-Jun-2014 kettenis

SystemMemory is used to access memory mapped registers on some machines, so we
must use the bus_space(9) API to access it instead of memcpy(9). Also make
sure we properly align access to fields that need more than byte alignment.

ok guenther@


# 1.258 21-May-2014 mlarkin

Fix invalid / missing format specifiers in several dnprintf statements
(used only when ACPI_DEBUG is enabled)

From a diff on tech@ by Fabian Raetz, with a minor change by me, tested
on i386 and amd64 with ACPI_DEBUG enabled and disabled.


# 1.257 25-Apr-2014 mlarkin

Defer the free of the hibernate structures until later, when it is safe to
do so.

Freeing the hibernate structures too early leads to inadvertent calls into
splx when we aren't ready for them yet. Found when trapping splx calls in
the resume path.

discussed with deraadt@ at m2k14


# 1.256 13-Apr-2014 mlarkin

Fix a badly worded debug message that implied the entire machine was going
to sleep, instead of just the acpi thread going to sleep.


# 1.255 31-Mar-2014 mpi

Change the order of operations during a suspend/resume cycle and
call bufq_quiesce() after executing the DVACT_QUIESCE handlers.

This should be safe since no disk nor controller drivers have such
handler but it will allow us to detach sd(4) devices attached to a
USB bus.

Another benefit pointed out by kettenis@ is that drivers that need
to read a firmware from the disk should be able to do it at resume
time in a DVACT_WAKEUP handler.

ok kettenis@, deraadt@


# 1.254 13-Mar-2014 dlg

get rid of the assumption that the head of the alldevs list is the
"mainbus" device. this breaks when mpath is enabled because it
attaches before mainbus and therefore takes the head position.

have autoconf provide device_mainbus() which looks up mainbus_cd,
and use that instead.

discussed with deraadt who just wants mpath stuff to move forward
despite there being many ways to shine this particular turd.


Revision tags: OPENBSD_5_5_BASE
# 1.253 21-Feb-2014 deraadt

Do the lid control further on in the resume sequence, since this is
AML, and will do WAITOK operations. While cold, that is a bad idea.
We seem to be safe with respect to the spec, I do not see any event
debounce issues here.
ok kettenis, tested by many


# 1.252 20-Jan-2014 claudio

Instead of checking sc->sc_revision <= 1 check that the
fadt->hdr_revision <= 1. This is a better check since it checks
the version of the proper descriptor. This allows the alix APU
to reboot since those boards have currently a wrong sc->sc_revision.
Committing this now so that it can be tested easier.
With and OK jsg@ also fine by derradt@


# 1.251 24-Dec-2013 kettenis

If the FADT has its SMI_CMD set to zero, assume we're only ACPI-only hardware
and don't need to disable SMI ownership of the ACPI hardware registers.

ok mlarkin@


# 1.250 23-Dec-2013 kettenis

Revert previous commit. It makes resume hang on some MP systems running
GENERIC.MP.


# 1.249 16-Dec-2013 kettenis

Restore hw.setperf upon resume like we do for apm(4) on i386. Seems we need
to do this fairly late in the resume to avoid hangs.

ok deraadt@, mlarkin@


# 1.248 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


# 1.247 06-Nov-2013 mpi

Add support for Power Resources for Dx states and the necessary hook
for PCI devices. This hook should be called twice, before and after
changing the power state of a PCI device.

Before setting the device to the new state, the ACPI layer will notify
every power resources linked to the device for that state and make sure
they are turned "_ON". After changing the state of the device, it will
decrement the reference of every power resources linked to that device
for the old state and turn them "_OFF" if they are no longer referenced.

This fixes the no-USB after resume problem seen on various ThinkPad,
problem initialy diagnosed with Alexander Polakov.

ok kettenis@, deraadt@


Revision tags: OPENBSD_5_4_BASE
# 1.246 01-Jun-2013 mlarkin

Stop printing ACPI wakeup devices in dmesg after the 16th wakeup device.

Helps with VMware VMs that report hundreds of wakeup devices.

ok reyk@, deraadt@


# 1.245 31-May-2013 bluhm

Fix the build for a kernel without wd and pciide in its config.
OK deraadt@ tedu@


# 1.244 30-May-2013 deraadt

Enforce ca_activate tree-walks over the entire heirarchy for all events,
cleaning up some shutdown-hook related code on the way.
(A few drivers related to sparc are still skipped at kettenis' request)
ok kettenis mlarkin, tested by many others too


# 1.243 18-Apr-2013 deraadt

pin the acpi thread to the BSP, because AML and SMI cannot be trusted. I
was certain we were doing this already, but it turns out no...


Revision tags: OPENBSD_5_3_BASE
# 1.242 24-Dec-2012 guenther

In acpiioctl(), Return though the splx() path if suser() returns an error

ok deraadt@


# 1.241 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


# 1.240 04-Oct-2012 ehrhardt

Use information provided by ACPI to attach secondary PCI host bridges.

ok kettenis@


# 1.239 07-Sep-2012 kettenis

Add a new function called acpi_pci_min_powerstate() that returns the lowest
power state for a PCI device for the power state (currently S3 or S4) we're
transitioning into. This function is probably too simplistic; there is a lot
of (silly) complexity in the ACPI spec.

ok deraadt@


Revision tags: OPENBSD_5_2_BASE
# 1.238 13-Jul-2012 pirofti

Disable acpivideo(4) on ASUS laptops.

There are some models that panic whenever an event is received.
Some cope, some don't. In order to get this right acpiec(4) requires
major rework. Until then let machines boot.

Discussed with deraadt@.


# 1.237 11-Jul-2012 mlarkin

for now, restrict hibernate to root in the acpi ioctl kernel interface.
normal rules for controlling this interface through apmd still apply.

ok deraadt@


# 1.236 09-Jul-2012 deraadt

tiny little oops in untested hibernate block


# 1.235 09-Jul-2012 deraadt

clean up ioctl() case statements


# 1.234 09-Jul-2012 deraadt

acpi_disable_onegpe() is not used; ok pirofti


# 1.233 24-May-2012 kettenis

If _MIN > _MAX consider a WordBus resource to be invalid. Fixes a panic on
the IBM x3550-M4.

ok mikeb@


# 1.232 29-Mar-2012 mlarkin

Missing #ifdef in previous diff


# 1.231 29-Mar-2012 mlarkin

Prevent hibernation attempts on machines with unsupported disk controllers.
The same check is also performed later in the hibernate sequence, but by
then it's too late to cleanly unwind (presently).

ok deraadt


# 1.230 26-Mar-2012 deraadt

simplify conditionals in acpi_sleep_state()
ok mlarkin


# 1.229 26-Mar-2012 deraadt

hook in the hibernate request code; half of this diff is from mlarkin
ok mlarkin


Revision tags: OPENBSD_5_1_BASE
# 1.228 20-Sep-2011 deraadt

Very early in suspend while the clock is still ticking realtime,
save the clock back to the rtc. The zaurus already did this.
ok phessler miod kettenis


Revision tags: OPENBSD_5_0_BASE
# 1.227 02-Jul-2011 nicm

kqueue attach functions should return an errno or 0, not a plain 1. Fix
the obvious cases to return EINVAL and ENXIO.

ok tedu deraadt


# 1.226 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.225 16-Jun-2011 pirofti

Import acpitoshiba driver.

This is originally written by Hiroyuki Aizu, ported and integrated
in our tree by Javier Vazquez <javama4 AT gmail> with tweaks and
suggestions by me.

Import prodded by deraadt@ and the lack of testers on tech@.


# 1.224 27-Apr-2011 jcs

attach acpithinkpad to newer lenovo models like the x120e

ok deraadt@


# 1.223 22-Apr-2011 jordan

Fix uninitialzied variables and formatting strings (-Wxxx errors)


Revision tags: OPENBSD_4_9_BASE
# 1.222 02-Jan-2011 jordan

Re-add code for ACPI taskqueue, eliminates need for state vars in acpi softc
Serialize processing ACPI tasks for sleep/powerdown/GPE event
ok deraadt@


# 1.221 31-Oct-2010 guenther

Revert last commit: it breaks resume on Thinkpads
"then please back it out" deraadt@


# 1.220 26-Oct-2010 jordan

Add task queue for ACPI gpe and notify handlers
This fixes eject on Dell Latitude dock and an issue on the Dell Mini
battery update.
ok deraadt


# 1.219 07-Oct-2010 jordan

Create separate functions for enabling wake and runtime gpes.
Remove old suspend/resume gpewalk function
ok deraadt, mlarkin


# 1.218 05-Oct-2010 jordan

split acpi_enable_onegpe to separate enable/disable functions
ok deraadt


# 1.217 05-Oct-2010 jordan

Added acpi_disable_allgpes function to clear GPE status
ok deraadt


# 1.216 05-Oct-2010 deraadt

clean up acpi_softc use more


# 1.215 05-Oct-2010 deraadt

delete a bit of #if 0 code


# 1.214 05-Oct-2010 deraadt

hide use of our global acpi_softc pointer a bit better


# 1.213 29-Sep-2010 deraadt

grab the acpi thread lock very early in acpi thread startup; not that it
is likely that anyone will race us... but why be careless.
ok kettenis


# 1.212 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.211 29-Aug-2010 jasper

- add a check for FWRITE here too.

ok miod@


# 1.210 27-Aug-2010 kettenis

Make PCI Power Management optional, and only enable it when acpi(4) attaches.

ok deraadt@


Revision tags: OPENBSD_4_8_BASE
# 1.209 08-Aug-2010 kettenis

Close race between the acpi thread and wscons ioctls running in process
context. We might rip this out post-release and replace it with a solution
that serializes things in a more obvious way.

ok deraadt@, miod@ and "those crazy texans who ok anything"


# 1.208 08-Aug-2010 deraadt

Report the acpi sleep states when on a RAMDISK; ok krw


# 1.207 07-Aug-2010 kettenis

Enforce acpi register access rescrictions.

ok mlarkin@, deraadt@, tested by marco@


# 1.206 07-Aug-2010 deraadt

upon resume, notify all the acpiac and acpibat drivers to update
their status. some laptops do not do this automatically.
ok kettenis canacar


# 1.205 06-Aug-2010 marco

Always call _PSW on _LID devices that support it to enable lid open to
resume the machine. Conversely disable it when the machine wakes up.

Tested by several ok deraadt


# 1.204 06-Aug-2010 deraadt

As part of a revamp of the PM1/GPE code, I write this diff to look for
unmanaged EN & STS bits in the PM1 register at interrupt time and report
them. As a side effect this splits the STS acknowledgement into two writes
(for power, and sleep) instead of one. The printf that is added (to spot
unmanaged STS bits) has not yet been triggered as far as we know.

Before the "write to PM1 registers at the right offset" diff went in, this
was not neccessary. But newer thinkpads do not have a working soft power
button without this diff.

We have no idea why.
ok mlarkin kettenis


# 1.203 05-Aug-2010 deraadt

PM1 power button events were generating 'fake events' to acpibtn PWRB,
but if there is no such device these were being tossed. Instead, arrange
for all power button handlers to flag a softc variable which is processed
by the thread; so that one place will know to request a powerdown (psignal
to init)
ok marco mlarkin


# 1.202 05-Aug-2010 deraadt

rename a variable which is annoyingly poorly named


# 1.201 05-Aug-2010 mlarkin

Write to the correct offset for various ACPI pmreg registers. Writing to
the correct register does properly clear the wake status bits and fixed
event bits, which allows some machines to have more than 1 suspend cycle.

bug spotted by deraadt@

tested by deraadt@ and myself on a half-dozen machines and others on
hackers@

ok deraadt@, marco@


# 1.200 05-Aug-2010 deraadt

Fix the power button. The pm1 register is 16 bits in size.
My mistake, a while back.
tested by krw


# 1.199 04-Aug-2010 kettenis

Print supported sleep states.

ok marco@, phessler@, deraadt@


# 1.198 03-Aug-2010 mlarkin

Use the proper flag for re-enabling certain hardware events (power btn,
sleep btn). The (incorrect) flag prevoiously used coincidentally had the
same value, so this shouldn't cause any different behavior than before.

tested on a variety of machines (i386, amd64, sp, mp) - no behavioral
change seen

ok deraadt@


# 1.197 28-Jul-2010 marco

Make all gasio failures look and behave the same.


# 1.196 28-Jul-2010 mlarkin

Delay a little longer on suspend before giving up. Fixes machines that
sometimes don't suspend because the sleep transition takes too long
(symptoms like failing to sleep but the backlight stays on).

ok deraadt@


# 1.195 27-Jul-2010 deraadt

remove XXX; EC can now be attached early when needed; ok jordan


# 1.194 27-Jul-2010 deraadt

more brackets feels good


# 1.193 27-Jul-2010 deraadt

XXX annotate a block in acpi_enter_sleep_state() that is pretty worrying


# 1.192 27-Jul-2010 deraadt

horrific indentation that nearly made me puke


# 1.191 27-Jul-2010 mlarkin

Set the sleeping indicator light on machines that support it, in case SMI
doesn't do it for us. Shows the 'moon' LED on older Thinkpads when
sleeping.

Tested on T43p, W500, T510 by me and on X61s by deraadt@.

ok deraadt@


# 1.190 27-Jul-2010 jordan

Early initialization of acpiec if ECDT table exists
Fixes hang when booting thinkpads while docked
ok deraadt


# 1.189 26-Jul-2010 deraadt

remove merge error which was commited


# 1.188 26-Jul-2010 pirofti

Add support for Sony ACPI hotkeys via a new driver: acpisony(4).

Currently it only works for the suspend button and tries to do right for
the brightness events, but I haven't found a Sony laptop that like to do
right so far.

In the future I want to make the brightness keys work on all Sony's and
also add support for the zoom hotkeys and whatever other funky keys I can
find on those things.

Okay deraadt@.


# 1.187 22-Jul-2010 deraadt

pretty large cleanup of SMALL_KERNEL


# 1.186 21-Jul-2010 deraadt

in the acpi GPE handler, late ack level interrupts not edge interrupts.
Duh.


# 1.185 21-Jul-2010 deraadt

Merge the edge and level gpe handlers into one (now possible since the
gpe structure tells us whether the gpe is edge or level)


# 1.184 21-Jul-2010 deraadt

delete #if 0 code we are not going to use; ok marco


# 1.183 20-Jul-2010 deraadt

make the mask code for acpi_enable_onegpe() match other similar code


# 1.182 20-Jul-2010 deraadt

Handle the acpi interrupt controller with proper edge/level handling,
wrapped in spltty to avoid racing against the interrupt controller.
Repair the gpe bit masking code while there, and do operations in the
right order
ok kettenis mlarkin, help from jordan at figuring out the order of
operations.


# 1.181 20-Jul-2010 matthew

When we're preparing to go to sleep, only call bufq_restart() if other
preparations failed.

ok kettenis@, thib@, deraadt@


# 1.180 19-Jul-2010 deraadt

permit compilation with ACPI_DEBUG


# 1.179 19-Jul-2010 deraadt

there is no need for a seperate dsdt_softc pointer; dsdt does not have a
softc but is just a part of the single acpi driver.
ok jordan


# 1.178 19-Jul-2010 deraadt

acpi_delay() is no longer needed; ok jordan


# 1.177 18-Jul-2010 mlarkin

Clear event status on resume, to avoid going back to sleep/reboot/shutdown
immediately after resume. Tested by a bunch of people on hackers@ on
various hardware. Fixes at least Toshiba NB200 and Gateway LT3103u.

ok marco


# 1.176 13-Jul-2010 deraadt

rename some thread related interfaces so they are logical


# 1.175 10-Jul-2010 jordan

Verify _ADR bdf is correct (some systems return 0xffff)
Works on Dell x9xx and Studio 1555


# 1.174 09-Jul-2010 jordan

Clear the noise


# 1.173 08-Jul-2010 jordan

Add mapping for ACPI device to PCI bdf (match autoconf tree)
Simplify resource parsing function to use buffer argument
Convert namespace linked lists to use queue macros
ok marco@, deraadt@


# 1.172 06-Jul-2010 deraadt

Use spltty() to lock downcalls from apm against the information being
modified by the acpi thread.
ok kettenis


# 1.171 06-Jul-2010 deraadt

Don't use a workq to do the suspend, because it races aginst the acpi
thread. Instead, just tell the acpi thread to do the suspend for us.
This makes apmd & zzz work correctly. While here, have acpithinkpad
attempt to post the event to apm before prompting the suspend itself.
ok kettenis marco mlarkin


# 1.170 05-Jul-2010 mlarkin

Reset register SCI_EN on resume. The ACPI spec clearly states we don't have
to do this, but many machines don't, or forget to, which leads to all sorts
of problems on resume (broken battery stats, overheating, failure to resume
more than once, etc).

tested by many, ok marco@, deraadt@


# 1.169 01-Jul-2010 thib

do bufq quiesce again, since vnd/the bufq_done call in
biodone has been fixed.


# 1.168 01-Jul-2010 jordan

Backout recent AML changes, breaks on R210 and others


# 1.167 01-Jul-2010 jordan

Simplified aml_parse_resource call, cleanup code
ok marco


# 1.166 01-Jul-2010 jordan

Fix subordinate bus # for multi-root PCI buses
ok marco, kettenis


# 1.165 30-Jun-2010 thib

Disable/partially backout the bufq quiesce changes as this
is causing havoc with vnds and release must be buildable.


# 1.164 29-Jun-2010 deraadt

Fix ramdisks (new code was placed inside a SMALL_KERNEL chunk)


# 1.163 29-Jun-2010 jordan

Add support for mapping ACPI to PCI devices
ok kettenis, deraadt


# 1.162 29-Jun-2010 kettenis

Call bufq_quiesce() on suspend to stop all I/O and bufq_restart on resume.
Makes suspend/resume work much better while doing disk I/O.

ok mlarkin@


# 1.161 29-Jun-2010 tedu

60 is a more reasonable guess for number of minutes in an hour than 100.
spotted by TeXitoi. ok deraadt


# 1.160 27-Jun-2010 jordan

Add _acpi_matchhids routine, precursor to pci device match


# 1.159 27-Jun-2010 mlarkin

Clear ACPI fixed event status on resume (power buttons/etc) so that some
machines don't immediately resume after suspending to S3.

Tested by kettenis@ and I on 5 or 6 machines, deraadt@ "yeah, looks good"


# 1.158 27-Jun-2010 jordan

Cleaned up acpi_maptable, adds to table list directly


# 1.157 07-Apr-2010 deraadt

If we have done a wsdisplay_suspend(), and ACPI decides it does not want to
suspend, we must roll back by calling wsdisplay_resume() or the virtual
console switching will be locked forever
ok oga


# 1.156 07-Apr-2010 kettenis

Make suspend/resume work on MP machines (running an MP kernel). Joint work
from mlarkin@ and me, with some amd64 fixes thrown in by deraadt@

ok marco@, deraadt@, pirofti@, mlarkin@


# 1.155 31-Mar-2010 kettenis

Bring /dev/apm support in line with apm(4). Make the suspend button send
a suspend request event to apmd(8) instead of suspending immediately. Also
keep track of whether /dev/apm and /dev/apmctl are currently open, such that
we can still suspend immediately if apmd(8) isn't running.

ok deraadt@, marco@, pirofti@, jsing@, oga@


# 1.154 30-Mar-2010 oga

Prevent the apmd/x races for good.

When we hit suspend time, go through all wsdisplays on the system. if
they are in mode MAPPED, but not MODE_DUMBFB then if possible do a full
vt switch to a !mapped vt, and prevent switching back until resume time.
This has to be called from MD code because this involves userland
running so that X can run the vt switch signal handler. This way, any
case where we are using the "poke registers from userland" model, we
will not be on the hardware when we go down, so the kernel can actually
handle thing properly.

Tested on several acpi laptops (by kettenis@ and ian@), x40 (me and
beck@ at LEAST) and zaurus (me). Maybe others, but if so I forgot who at
this time..

Idea from deraadt somewhere over the Faroe Islands (I thought of a
similar thing myself a while ago). Much prompting from him. Ok and
comments miod@


# 1.153 25-Mar-2010 oga

Fix & vs. && in the kqfilter code.

Based on a comparison with the apm code.

ok deraadt@, kettenis@


Revision tags: OPENBSD_4_7_BASE
# 1.152 26-Nov-2009 mlarkin

Visual (and on some machines, audio) feedback while the machine is
resuming via calls to aml _SST method. This also makes the "moon" LED on
thinkpads to blink during resume and cease when resume is completed.

ok pirofti@


# 1.151 26-Nov-2009 deraadt

Toggle into "cold" when we we disable interrupts, because tsleeping
in the AML interpreter and context switching into userland processes
tends to sometimes pop the ACPI trampoline page out of our address
space. Makes lots more machines work.
ok kettenis mlarkin


# 1.150 24-Nov-2009 jsg

in the resume path evaluate _WAK before dealing with device drivers
like the spec says, not after.

ok mlarkin@


# 1.149 24-Nov-2009 deraadt

abort suspends on SMP systems until that gets written; ok mlarkin


# 1.148 23-Nov-2009 mlarkin

pci should return the result of it's children's suspend/resume calls back
to its parent.

handle suspend failure case and unwind if devices have an activate function
that returns failure.

ok deraadt@, kettenis@


# 1.147 23-Nov-2009 mlarkin

acpi* devices need to call their children on suspend/resume.
ok deraadt@


# 1.146 23-Nov-2009 pirofti

Remove ACPI_SLEEP_ENABLED checks.

This enables by default the suspend/resume paths in the kernel.

Okay deraadt@.


# 1.145 23-Nov-2009 deraadt

knf and spacing that are super annoying


# 1.144 23-Nov-2009 mlarkin

Hook up Theo's new children suspend/resume function, as well as more ACPI
suspend/resume glue.

ok deraadt@


# 1.143 26-Oct-2009 deraadt

Do not do strncmp comparisons on dv_xname because longer device names which
look similar could arrive in the future. Instead, compare directly against
dv_cfdata->cf_driver->cd_name
Issue originally spotted by miod


# 1.142 26-Oct-2009 jordan

Verify checksum+address when loading ACPI tables.
Some systems had invalid entries in RSDT/XSDT.
ok marco@


# 1.141 23-Jul-2009 cnst

New aibs(4) driver for ASUSTeK AI Booster (ACPI ATK0110) hardware monitoring,
with sensor state support through limits provided by the ACPI.

Tested on several ASUS motherboards kindly networked by Sam Fourman Jr.

ok deraadt marco jordan


Revision tags: OPENBSD_4_6_BASE
# 1.140 03-Jun-2009 pirofti

Import acpi power resource driver. This is needed for suspend/resume logic.

Okay jordan@ and marco@.


# 1.139 03-Jun-2009 pirofti

Add acpivideo support. This adds brightness support for all laptops
except thinkpads, they will use the acpithinkpad driver. The driver is
also hooked into wsconsole. So brightness can be adjusted via:

$ wsconsctl display.brightness=<percentage>

This is very helpfull on some laptops that have a nasty bios and get two
steps instead of one when pressing the brightness button.

Tested on various dell, fujitsu, acer, samsung and other laptops.

Okay marco@, miod@. Suggestions from kettenis@.
Lots of reviews and help from miod@, thanks!


# 1.138 03-Jun-2009 jordan

Added ejectable bay for IDE cdroms
ok marco@


# 1.137 30-Apr-2009 marco

Oga changed apm ioctls and forgot to grep. Temporary work around by hitting
the sleep button when we get any request. This interface will chnage at
some point.
ok oga


# 1.136 19-Apr-2009 krw

Call acpi_sleep_walk() when powering down just like when entering
any other sleep state. This tells acpi wake devices to not wake up
the box until manual powering up resets their status. Makes my Dell
GX520 stay powered off in response to 'halt -p'.

In snaps for a while.

ok marco@ (several times) deraadt@


# 1.135 17-Apr-2009 pirofti

Add device ranking support for future sleep/wake developments.

okay marco@


# 1.134 11-Mar-2009 jordan

Cleanup.. use aml_evalinteger instead of aml_evalname
ok marco@


# 1.133 10-Mar-2009 jordan

Fix cut/paste error.. call _BFS not _PTS
ok marco@


Revision tags: OPENBSD_4_5_BASE
# 1.132 21-Feb-2009 marco

overzealous cut/paste; found by Vladimir Kirillov <proger@uaoug.org.ua>


# 1.131 20-Feb-2009 marco

Oops didn't mean for Debugger() to remain there.


# 1.130 19-Feb-2009 marco

suspend/resume bits so that we can develop this in tree. This is disabled.
code from mlarkin and me
help from art,toby,jordan and several others
ok jordan, go for it deraadt


# 1.129 10-Feb-2009 jordan

Fix null pointer for shutdown if acpi not enabled
ok marco@


# 1.128 04-Feb-2009 kettenis

Some systems that claim to be ACPI 2.0 apparently don't provide an XSDT
address. Make the code fall back on the RSDT on those systems.

From Joerg Niendorf <p3y80@internode.on.net>. Fixes PR 5986.


# 1.127 17-Jan-2009 krw

A couple of stray malloc/memset -> malloc(M_ZERO), a bit of knf and
lose a couple of unnecessary casts. No functional change.

Feedback from kettenis@

ok kettenis@ marco@


# 1.126 06-Nov-2008 marco

Collapse all sanity checks of hids into one function.

Originaly from jordan


Revision tags: OPENBSD_4_4_BASE
# 1.125 02-Jul-2008 fgsch

add acpivideo. for now almost an empty skel so others can work on
suspend/resume. eventually it will also manage output switching and
brightness where supported. prodded by marco@


# 1.124 11-Jun-2008 marco

Fix $OpenBSD$ for once and for all


# 1.123 08-Jun-2008 deraadt

aaa_dev was being set from an uninitialized variable; ok toby


# 1.122 07-Jun-2008 marco

Kill ACPIVERBOSE. It was never used and is worthless to boot.

comments jmc
ok toby


# 1.121 06-Jun-2008 marco

Shrink acpi some more for floppies and while here re-enable acpi on the
floppies. Fixes crazy sony vaio kettenis has.

tested by deraadt
ok deraadt jordan kettenis


# 1.120 01-Jun-2008 marco

Fix \n nit in SMALL_KERNEL.


# 1.119 01-Jun-2008 marco

Re-enable new parser. Lots of testing by various folks. Fits all media
now too.

ok kettenis deraadt


# 1.118 16-May-2008 dlg

back out jordans new parser.

it didnt have enough oks from the right people, it did break machines
that were previously working, and it bloated the RAMDISKS and install
media beyond the constraints of the media.

requested by deraadt@


# 1.117 14-May-2008 jordan

Adding new ACPI Parser code
Old guts of code still exists, needs to be torn out next
ok marco@
ok brad@


# 1.116 27-Apr-2008 jcs

add acpithinkpad(4), an acpi support driver for ibm/lenovo
thinkpads.

provides functionality that the bios did on older, non-acpi
thinkpads like respond to screen brightness keys and toggle the
bluetooth device on and off.

ok deraadt@ and marco@


# 1.115 24-Apr-2008 jsing

Introduce acpiasus(4), a driver for the ACPI based hotkeys found in many
ASUS laptops (including the ASUS EeePC) - largely based on NetBSD's
asus(4) driver. On the ASUS EeePC this allows us to enable/disable
wireless, change screen brightness and use the volume keys.

ok jsg@, weingart@


# 1.114 12-Apr-2008 kettenis

Get rid of acpi_s5 global variables; simply send SIGUSR2 instead of SUGUSR1
to tell init(8) to power down the machine.

ok krw@


Revision tags: OPENBSD_4_3_BASE
# 1.113 21-Jan-2008 jsg

Check against RESET_REG_SUP in FADT before doing the acpi reset dance
after all, not doing so appears to break more machines than it fixes.

ok marco@


# 1.112 05-Jan-2008 kettenis

Never attach more acpicpu(4) devices than cpu(4) devices. Always attach
the first acpicpu(4) device on non-MULTIPROCESSOR kernels, regardless of its
ACPI processor ID.

ok mikeb@


# 1.111 05-Dec-2007 deraadt

Hang acpi(4) below bios(4) instead of mainbus(4). This lets us move acpi(4)
before pcibios(4), and then.... if acpi(4) attaches, skip pcibios(4) since
it messes the machines up. Fixes claudio's HP dl320 G5, and almost assuredly
others.
ok kettenis


# 1.110 05-Dec-2007 deraadt

repair acpiopen/acpiclose and friends to handle the /dev/apmctl interface,
so that apmd(8) can actually work correctly against acpi(4), and thus,
even do the fancy -C/-A stuff; also tested by jmc


# 1.109 02-Dec-2007 fgsch

fix the trailing space for real now.


# 1.108 02-Dec-2007 fgsch

avoid trailing spaces on output; marco@ ok.


# 1.107 27-Nov-2007 kettenis

Fix off-by-one that made us attach acpicpu0 and acpicpu1 on GENERIC kernels.

ok marco@


# 1.106 25-Nov-2007 jsg

When available and ACPI is enabled use the ACPI reset method
to reboot. This is required for some quirky sis machines
and likely others to reboot.

Tested by many. 'commit early commit often' deraadt@


# 1.105 19-Nov-2007 deraadt

sigh, amd64 still uses that stupid acpi_enabled variable...


# 1.104 19-Nov-2007 deraadt

softc's come initialized to 0, you know...


# 1.103 19-Nov-2007 deraadt

store revision; from jsg


# 1.102 19-Nov-2007 kettenis

Initialize sc_facs earlier.

ok deraadt@, beck@, canacar@


# 1.101 16-Nov-2007 deraadt

fix the bus_space #define nightmare, so that amd64 and i386 are much more
uniform. as a result shared code like acpi needs less #ifdef's
ok marco kettenis


# 1.100 15-Nov-2007 deraadt

remove the #ifdef ACPI_ENABLE. the EC codepath is either safe enough (and
will be improved), or it is unsafe because AML assumes it must be there
ok almost everyone


# 1.99 15-Nov-2007 mikeb

Make acpicpu(4) attach to the existing CPUs only.

Problem reported by form@. Fix idea by kettenis@.
Lots of help from deraadt@. Tested by deraadt@, form@ and me.

ok deraadt marco


# 1.98 14-Nov-2007 deraadt

spacing


# 1.97 12-Nov-2007 deraadt

correct : vs , in attach-time messages


# 1.96 06-Nov-2007 deraadt

all ACPI_ENABLE kernels need acpi_interrupt()


# 1.95 06-Nov-2007 deraadt

move acpi_read_pmreg() and acpi_write_pmreg() out of SMALL_KERNEL so
that acpi ramdisks can compile (obvious solution)


# 1.94 05-Nov-2007 canacar

Establish the acpi interrupt earlier. Fixes problems with some
GENERIC.MP kernels not getting acpi interrupts. This is a
workaround until the real issue with late establishment is fixed.
ok weingart@, beck@, gwk@


# 1.93 03-Nov-2007 jordan

Added support for displaying wakeup devices
ok beck@,weingart@,gwk@


# 1.92 11-Oct-2007 kettenis

When walking the tree yo find _PRT methods, check whether the device is
actually enabled and functioning. Similar to what we do for _INI.

ok weingart@, beck@


# 1.91 08-Oct-2007 krw

More simple memset(,0,) -> M_ZERO changes. In this batch move to
size(*p) as the first malloc() parameter where p is declared locally
and thus easy to check. Add M_ZERO to gpe_table allocation in acpi.c
even though there is no obvious bzero or memset nearby.


# 1.90 13-Sep-2007 mk

Remove unnecessary memset() (see aml_evalnode(9)) and a bit of KNF.

ok toby


# 1.89 13-Sep-2007 weingart

Implement ACPI 6.5.1 spec tree walk for _STA and _INI.
Fixes mk's laptop. No regressions so far (thank you to the
testers).

ok gwk@, mk@, marco@


Revision tags: OPENBSD_4_2_BASE
# 1.88 17-Apr-2007 mk

Now that acpidock no longer hangs machines during boot (at least
according to the reports I've been getting) it should be safe to enable
it again, so remove the #if 0 stuff around it.

ok marco


# 1.87 11-Apr-2007 jordan

Added changes for C-State
ok marco@


# 1.86 26-Mar-2007 marco

Calculate minutes left more accurately.

From giovanni <qgiovanni@gmail.com>


Revision tags: OPENBSD_4_1_BASE
# 1.85 03-Mar-2007 mk

Stop looking for acpidock until we fix that stuff that breaks all pre-60
series ThinkPads with acpidock(4) enabled.


# 1.84 22-Feb-2007 marco

We were getting no interrupts in .MP. There are 2 fixes; one in
acpi_machdep.c and move the interrupt establishment down after ioapic
has been initialized.

ok jordan


# 1.83 21-Feb-2007 kettenis

Make an acpi-enabled GENERIC.MP work on systems that only support PIC mode.

ok marco@


# 1.82 19-Feb-2007 jordan

Added acpi_map_address function for mapping GAS/IO registers
ok marco@


# 1.81 18-Feb-2007 jordan

Added dynamic perfect hash for opcodes
ok marco@


# 1.80 18-Feb-2007 marco

Shave off another K pre compression.
Shuffle SMALL_KERNEL functions around so that we only have a single #ifndef


# 1.79 17-Feb-2007 marco

Add SMALL_KERNEL throught acpi to make it fit on boot media.
ok deraadt


# 1.78 13-Feb-2007 marco

Add smarter _INI code. This helps with some dissapearing devices.

ok kettenis jordan


# 1.77 31-Jan-2007 gwk

Introduce acpi_hasprocfvs thats "ACPI Has Processor Frequency and Voltage
Scalling" a global flag set by acicpu attach to indicate that ACPI is
handling fvs through the _PSS objects. This will fix disappearing
EST/powernow when booting kernels with acpi enabled, as acpicpu isnt
compiled into GENERIC yet. Also in cases where acpicpu is built in and
legacy EST/powernow work but acpi is missing the _PSS object, the legacy
routines will be used.

tested by dim@, and Nick Nauwelaerts <nick AT nauwelaerts.net>
ok marco@, dim@


# 1.76 22-Jan-2007 mk

Actually look for docking stations by looking for _DCK nodes.

ok marco


# 1.75 02-Jan-2007 marco

On some systems EC must be attached as the first device. Move EC from
generic _HID devices to its own routine to force first attach. Bug found
and debugged by joris@

Code mostly written by joris@


# 1.74 26-Dec-2006 marco

Add polling to devices that require it such as acpibat and acpitz.
Use the same tsleep/wakeup mechanism as the interrupt code to keep it non-
concurrent.


# 1.73 21-Dec-2006 deraadt

even more knf love


# 1.72 21-Dec-2006 marco

Remove some leftover acpiec gpe/interrupt code.


# 1.71 20-Dec-2006 deraadt

oops, name already used


# 1.70 20-Dec-2006 deraadt

rename __acpi_enable_gpe(); ok marco


# 1.69 20-Dec-2006 deraadt

acpi_unmap_pmregs() is not used, ok marco


# 1.68 19-Dec-2006 deraadt

simplify some low-level methods substantially (shrinking code); ok marco jordan


# 1.67 06-Dec-2006 mk

malloc(..., M_NOWAIT) with no NULL check is bad. If allocation fails,
complain, clean up, and bail out of attach.

input and ok gwk


# 1.66 28-Nov-2006 jordan

Fixed clearing arg for acpi_init_pic
ok kettenis@


# 1.65 27-Nov-2006 kettenis

Don't spam dmesg.


# 1.64 27-Nov-2006 jordan

Fixes for acpiprt routing
Post-parse fixup for namerefs
ok kettenis@


# 1.63 25-Nov-2006 marco

Don't handle acpiev events inside the gpe handler since this causes deadlocks.

ok jordan@


# 1.62 15-Nov-2006 kettenis

Hook up acpi apic interrupt routing support to generic acpi code.

ok jordan@


# 1.61 02-Nov-2006 marco

Kill acpiec attach function and add its functionality to the generic _HID
device attach

prodded and ok jordan


# 1.60 02-Nov-2006 marco

Fix comments to reflect reality.


# 1.59 19-Oct-2006 marco

Replace ACPI ioctls with APM ioctls. This makes ACPI talk the same protocol
as APM therefore userspace can now retrieve power information in the same
fashion as all other apm arches (zaurus, macppc, i386 etc).

To use force the use of acpi device like so: apmd -f /dev/acpi

ok jordan@


# 1.58 19-Oct-2006 jordan

Fixed GPE debug levels
Fixed ref counter on temporary bufferfields
DSDT parsing fully working on IBM Thinkpad
ok marco@


# 1.57 15-Oct-2006 jordan

Removed old GPE handler code remnants
ok marco@


# 1.56 12-Oct-2006 jordan

Massive ACPI changes
Fixed ACPI leaks
new ACPI API
ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.55 30-Jun-2006 gwk

KNF, No functional change,
"go at it" marco@


# 1.54 30-Jun-2006 jordan

Support for calling Embedded Controller via new GPE interface
ok marco@


# 1.53 30-Jun-2006 jordan

Added new code for GPE handling
Also works with ACPIEC; need to fixup acpiec.c to remove original interrupt handler
ok marco@


# 1.52 30-Jun-2006 jordan

Added offset argument for acpi read/write pmreg routines (new GPE code)
ok marco@


# 1.51 31-May-2006 todd

do not call acpi_powerdown() unless acpi has successfully initialized
ok gwk@ canacar@


# 1.50 29-May-2006 canacar

Add embedded controller support.
ok marco@ deraadt@ testing & ok gwk@


# 1.49 19-May-2006 canacar

Minimal thermal zone driver. Monitors thermal zone temperature,
shuts down the system if the 'critical temperature' is reached.
ok marco@


# 1.48 11-Apr-2006 marco

KNF.


# 1.47 11-Apr-2006 gwk

Add acpi_resume(struct acpi_softc *sc), which calls some AML methods,
detected at attach time, currently unused.

ok marco@


# 1.46 11-Apr-2006 gwk

Discover some ACPI sleep related methods, and call them at the
appropriate time in acpi_enter_sleep_state().

ok marco@


Revision tags: OPENBSD_3_9_BASE
# 1.45 26-Feb-2006 marco

Add acpicpu as a device.

ok jordan@


# 1.44 23-Feb-2006 marco

remove dead function.

ok jordan@


# 1.43 22-Feb-2006 marco

Print out all tables it found during autoconf. We'll need this data in
the future.


# 1.42 22-Feb-2006 marco

Make dmesg less verbose.

ok jordan.


# 1.41 22-Feb-2006 jordan

Added parsing of SSDT tables
ok marco@


# 1.40 22-Feb-2006 jordan

Added new aml_register to notify by device id
Moved powerdown code to button device
ok marco@


# 1.39 21-Feb-2006 marco

Readjust debug levels that crept in too low over the last few days.
KNF


# 1.38 21-Feb-2006 marco

Rig the power button so that the machine shuts down correctly.


# 1.37 20-Feb-2006 marco

Add acpibtn device. Remains disabled for now.


# 1.36 19-Feb-2006 jordan

Added code for aml_notify GPE notification callback
ok marco@


# 1.35 19-Feb-2006 grange

Fix acpi_enter_sleep_state() by replacing current ugly hack
with mostly correct sleeping state transition code.

ok brad@ marco@


# 1.34 19-Feb-2006 marco

jordan:
Enable interrupts.
Added peripheral code to make this work.

marco:
Replace softintr and other types of interrupts with a handler with context.
Fixed interrupt magic for GPEs.
Some KNF

This is work done by jordan and I but has to be a single commit.


# 1.33 17-Feb-2006 marco

Remove earlier introduced delays. We really only need them during autconf.

ok jordan@


# 1.32 17-Feb-2006 marco

Add a generic delay function as per the ACPI spec.

from jordan@


# 1.31 17-Feb-2006 marco

Use tsleep instead of delay when !cold.


# 1.30 17-Feb-2006 marco

Argh, we need delays between reads/writes. The backend reads (i2c most likely)
are so slow that values come back as 0 sometimes. We have to do this
despite the spec saying we shouldnt have to. Guess having a faster
parser than others can hurt sometimes.

Idea from jordan.


# 1.29 17-Feb-2006 jordan

Fixed code for handling _INI
Rearranged debug statements
ok marco@


# 1.28 16-Feb-2006 jordan

Added stub for calling _INI
Fixed aml_dowhile for NULL return
ok marco@


# 1.27 16-Feb-2006 jordan

Fixed parser/evaluator to retrieve battery info+state
Cleanup of parser
Dell laptops return battery status
ok marco@


# 1.26 06-Feb-2006 brad

fix a typo so this will compile on amd64.


# 1.25 04-Feb-2006 marco

When ACPI_ENABLED wasn't set but the devices were it had an unknown variable.

Pointed out by ian@.


# 1.24 03-Feb-2006 jordan

Rewrote aml parser yet again...
Fixed bus_space_map for i386/amd64
Added _BST and _BIF to acpibat to get status/info
ok marco@


# 1.23 20-Jan-2006 jordan

More evaluator changes
Removed old evaluator code
Fixed _HID value
ok marco@


# 1.22 19-Jan-2006 jordan

Added support for read/write of PCI Config space
Help jason@
ok marco@


# 1.21 18-Jan-2006 jordan

Added new evaluation routines
Added helper methods for buffer/field evaluation
Fixed broken indentation on dsdt.c

ok marco@


# 1.20 17-Jan-2006 jordan

Major overhaul of the aml parser. Evaluater temporarily disabled.

ok marco@


# 1.19 06-Jan-2006 grange

Check for ACPI control availability only if ACPI_ENABLE defined.


# 1.18 06-Jan-2006 grange

Informative error messages on attach, prettier dmesg.


# 1.17 06-Jan-2006 grange

printf -> dnprintf conversion for debug messages.


# 1.16 05-Jan-2006 grange

Find SLP_TYPx values and store them for future use in
acpi_enter_sleep_state().


# 1.15 05-Jan-2006 grange

Random KNF and cleanup


# 1.14 05-Jan-2006 grange

Provide symbolic names for the acpi sleeping states and use it.


# 1.13 28-Dec-2005 marco

Moved DEVNAME to a acpidev.h

ok jordan@


# 1.12 18-Dec-2005 sturm

add back comment about wrong code

ok marco


# 1.11 16-Dec-2005 marco

Remove test for kbd_reset. Prompted deraadt@, ok jordan@


# 1.10 16-Dec-2005 jordan

Added hooks for powerdown/power button press


# 1.9 16-Dec-2005 jordan

Cleaned up common power management register i/o
Added enter sleep state ioctl
Hooked power button interrupt
ok marco@


# 1.8 16-Dec-2005 marco

Remove autoconf chatter


# 1.7 16-Dec-2005 jordan

Moved DSDT AML parsing into parent ACPI driver
ok marco@


# 1.6 14-Dec-2005 marco

Add AC device glue.


# 1.5 14-Dec-2005 marco

Adjust debug printing levels to something more sane.


# 1.4 13-Dec-2005 marco

Add glue to attach devices to apci.


# 1.3 07-Dec-2005 marco

KNF


Revision tags: OPENBSD_3_8_BASE
# 1.2 10-Jul-2005 grange

Spacing cleanup.


# 1.1 02-Jun-2005 tholo

Start on a basic ACPI framework -- does not do much more than read out the
ACPI tables into kernel memory and attach ACPI and HPET timers currently.

In order to test this code, enabling the devices in GENERIC as well as
the ACPI_ENABLE option is needed. This code does not do any thermal
control yet, so this should be done with care depending on the platform.

In the tree so more people can contribute to making this more fully
featured.

Ok niklas@ grange@ tedu@


# 1.391 27-Aug-2020 jmatthew

Reorder the acpi attach process so we look at Processor() CPU nodes
after Device() ones, since we should prefer the newer node type.
If we see any Device() nodes, don't attach acpicpu(4) to any Processor()
nodes if they're also present. This also makes acpitz(4) and
acpipwrres(4) devices attach slightly later.

ok kettenis@, also tested by jmc@


# 1.390 16-Aug-2020 gnezdo

Remove sc_maxgpe from acpi_softc which was never read from

Remove a dead conditional from acpi_init_gpes while there.

ok jcs@


# 1.389 21-Jul-2020 deraadt

acpi can use IPL_BIO (a low interrupt) since it only enqueues operations for
later processing. The use of a high interrupt will predate suspend/resume
efforts, we had to redesign acpi to be non-reentrant obviously
discussed with kettenis, in snaps for more than a week


# 1.388 24-Jun-2020 cheloha

kernel: use gettime(9)/getuptime(9) in lieu of time_second(9)/time_uptime(9)

time_second(9) and time_uptime(9) are widely used in the kernel to
quickly get the system UTC or system uptime as a time_t. However,
time_t is 64-bit everywhere, so it is not generally safe to use them
on 32-bit platforms: you have a split-read problem if your hardware
cannot perform atomic 64-bit reads.

This patch replaces time_second(9) with gettime(9), a safer successor
interface, throughout the kernel. Similarly, time_uptime(9) is replaced
with getuptime(9).

There is a performance cost on 32-bit platforms in exchange for
eliminating the split-read problem: instead of two register reads you
now have a lockless read loop to pull the values from the timehands.
This is really not *too* bad in the grand scheme of things, but
compared to what we were doing before it is several times slower.

There is no performance cost on 64-bit (__LP64__) platforms.

With input from visa@, dlg@, and tedu@.

Several bugs squashed by visa@.

ok kettenis@


# 1.387 10-Jun-2020 jca

Give userland a consistent battery and A/C state after resume

Previously we refreshed the state of acpiac(4) and acpibat(4)/acpisbs(4)
by queueing fake ACPI events to be processed by the acpi thread.
acpibat(4) using ACPIDEV_POLL meant that its status could be refreshed
first, resulting in an APM_POWER_CHANGE event sent to userland before
the state of acpiac(4) was up to date. Because of this, apmd(8) could
see A/C unplugged when it was in fact plugged, possibly leading to
a wrong decision.

Instead of using fake ACPI events, refresh the status of acpiac(4) and
acpibat(4)/acpisbs(4) right away at DVACT_WAKEUP time. To reuse the
existing refresh code of those drivers, make their ACPI *_notify()
handlers responsible for sending APM_POWER_CHANGE events instead. Also
queue an APM_POWER_CHANGE event right after resume to retain existing
behavior.

This fixes the consistency of data shown to userland, and makes those
drivers less special.

Initially discussed last year (!) with mpi@ at Miod's place.
Feedback and ok jcs@, deraadt@ agrees with the use of DVACT_WAKEUP here.


# 1.386 29-May-2020 deraadt

dev/rndvar.h no longer has statistical interfaces (removed during various
conversion steps). it only contains kernel prototypes for 4 interfaces,
all of which legitimately belong in sys/systm.h, which are already included
by all enqueue_randomness() users.


# 1.385 14-May-2020 kettenis

Bring back the acpi(4) changes. This time around only use the ACPI _CRS
information on ACPI 5.0 and later.

ok krw@, patrick@


# 1.384 11-May-2020 jca

More accurate remaining power reporting with disparate batteries

Overall remaining power was computed as the average of the remaining
power of each battery, in percents. This doesn't work with batteries of
different capacity, so use the total capacity and remaining power
instead.

Reported by lidstah, successful tests by lidstah and benno@, ok kettenis@


# 1.383 08-May-2020 kettenis

Move parsing of _CRS into acpi(4) and pass the parsed address and interrupt
information in struct acpi_attach_args.

ok mpi@


Revision tags: OPENBSD_6_7_BASE
# 1.382 14-Apr-2020 kettenis

Implement acpi_getprop(), the ACPI equivalent of OF_getprop().

ok patrick@


# 1.381 12-Apr-2020 kettenis

Add support for the _CCA method. This method indicates whether DMA is
cache-coherent or not. To implement this, acpi(4) gets two bus_dma tags
and passes the appropriate one when attaching devices based on _CCA.
On i386/amd64, where for all practical purpose DMA is always cache-coherent,
the two tags are the same. But on arm64 they are distinct.

ok patrick@


# 1.380 07-Apr-2020 visa

Abstract the head of knote lists. This allows extending the lists,
for example, with locking assertions.

OK mpi@, anton@


# 1.379 03-Apr-2020 mpi

Fix incoherencies of errors returned by various *kqfilter routines.

EOPNOTSUPP is returned when there's no "filterops" for a given fd.
EINVAL when the requested filter isn't supported by the "filterops".
ENXIO means the underlying device is no longer valid/present.

ok visa@


# 1.378 20-Feb-2020 visa

Replace field f_isfd with field f_flags in struct filterops to allow
adding more filter properties without cluttering the struct.

OK mpi@, anton@


# 1.377 31-Dec-2019 visa

Use C99 designated initializers with struct filterops. In addition,
make the structs const so that the data are put in .rodata.

OK mpi@, deraadt@, anton@, bluhm@


# 1.376 31-Dec-2019 mpi

Convert infinite sleeps to {m,t}sleep_nsec(9).

ok kn@


# 1.375 31-Dec-2019 jsg

include sys/sensors.h for sensor use
don't depend on indirect inclusion from acpidev.h


Revision tags: OPENBSD_6_6_BASE
# 1.374 07-Sep-2019 kettenis

Revert acpipci(4) changes. Too many systems are broken. We'll try again
after 6.6 as been released. The acpireg.h change stays behind.


# 1.373 28-Aug-2019 kettenis

Use ACPI information to attach PCI busses like we do on arm64. There are a
few additional quirks though, and attaching the PCI busses is delayed to
replicate the existing code more closely. That may be changed in the
future. Also tweak how we handle MSI support and respect to ACPI flag
that says we shouldn't attempt to use MSIs.

Some fallout is expected.

ok patrick@


# 1.372 27-Aug-2019 deraadt

On amd64 the bootup and resume paths for SP/BP/SP were not performing
msr/pat, clock-startup, firmware upgrade, fpu initialization, and cpu
soft-state initialization in the same order. That kind of chaos is
crazy and fragile. Improve the acpi resume call sequence and unify order
of the operations.


# 1.371 02-Jul-2019 jcs

acpi_set_gpehandler: turn edge arg into flags, add support for
passing GPE_DIRECT which directly executes the supplied callback and
clears a level interrupt rather than going through acpi_taskq which
can be slow when other driver callbacks are in the queue.

This should not be used for GPE handlers that need to do anything
ACPI-related.

ok kettenis


# 1.370 10-Jun-2019 kettenis

Print proper ACPI version number.

ok mlarkin@


# 1.369 08-Jun-2019 kettenis

When establishing the mapping between ACPI device nodes and PCI devices,
skip PCI host bridges and devices that aren't there.

ok patrick@


# 1.368 07-Jun-2019 kettenis

Introduce acpi_getsta() and use it to eliminate some duplicated code.

ok patrick@


# 1.367 12-May-2019 kettenis

Make sure the extended PM register descriptions actually exist before
using them. Fixes machines with ancient ACPI 1.0 (such as recent
hypervisors).

ok deraadt@


# 1.366 12-May-2019 kettenis

Add support for mapping ACPI PM registers using the "extended" gasio
representation from the FADT. Mostly fixes the Lanner NCA-1510.

ok lteo@, mlarkin@, deraadt@


# 1.365 11-May-2019 lteo

Fix incorrect error message.

ok kettenis@


# 1.364 08-May-2019 tedu

free size. the allocation is the header plus the length.


Revision tags: OPENBSD_6_5_BASE
# 1.363 04-Apr-2019 kettenis

Avoid potential null-pointer dereference. Found by Kent R. Spillner.

ok deraadt@, jsg@


# 1.362 20-Jan-2019 jsg

fix SMALL_KERNEL build after changes in rev 1.361
reported by naddy@ ok deraadt@


# 1.361 19-Jan-2019 tedu

Add a pwraction sysctl that controls what the power button does on acpi.
By default, nothing changes -- shutdown is initiated. But allows turning
power button into a sleep button if desired.
(grudging) ok from a few parties


# 1.360 26-Oct-2018 kettenis

Add acpipci(4) on amd64. For now this only calls the PCI-specific _OSC
method to let the ACPI implementation know what features we support.


Revision tags: OPENBSD_6_4_BASE
# 1.359 30-Aug-2018 kettenis

Move softbutton check before the check that skips attaching certain acpi
devices. Fixes regression caused by matching on _CID in addition to
matching on _HID.

ok matthieu@, mlarkin@


# 1.358 25-Aug-2018 kettenis

Match ACPI devices based on _CID if no match for _HID is found.

ok mpi@


# 1.357 19-Aug-2018 kettenis

Add support for multiple PCI segments. Only really implemented for arm64
for now as amd64/i386 firmware still caters for legacy OSes that only
support a single PCI segment.

ok patrick@


# 1.356 03-Aug-2018 kettenis

Let ahci(4) match on _CLS instead of _HID when attaching at acpi(4). Avoids
having to add many more _HID entries to the match table.

ok deraadt@, mlarkin@


# 1.355 10-Jul-2018 kettenis

Make legacy interrupts work in acpipci(4).

ok patrick@


# 1.354 05-Jul-2018 kettenis

Add acpipci(4), a driver that supports generic ECAM-compatible PCI host
bridges based on information provided by ACPI.

ok mlarkin@


# 1.353 04-Jul-2018 kettenis

Properly pass around the PCI "chipset tag" in acpi(4) and refactor
acpimcfg(4) to call an MD initialization functions that sets up a tag for
PCI ECAM.

ok guenther@, mlarkin@, krw@


# 1.352 01-Jul-2018 kettenis

Move acpi(4) attach glue into acpi_machdep.c.

ok guenther@, deraadt@


# 1.351 01-Jul-2018 kettenis

Add support for _DSD Device Properties.

ok mlarkin@


# 1.350 30-Jun-2018 kettenis

Add intr_enable() function, intended for MI use to amd64 and i386 and use
this in the acpi(4) suspend/resume code paths.

ok deraadt@


# 1.349 29-Jun-2018 kettenis

Use <stdint.h> types.

ok mpi@, jung@, krw@, deraadt@


# 1.348 29-Jun-2018 mlarkin

ACPI: Allow (with warning) GPE handler reassignment, instead of returning
a failure code.

ok kettenis


# 1.347 26-Jun-2018 mlarkin

knf


# 1.346 26-Jun-2018 kettenis

Let acpi(4) pass down the bus dma tag.

ok deraadt@


# 1.345 25-Jun-2018 kettenis

Reorganize acpi(4) code a bit in preparation for upcoming arm64 support.

ok deraadt@


# 1.344 20-May-2018 kettenis

Wake up the acpi thread after scheduling a gpio event.

ok mpi@


# 1.343 17-May-2018 kettenis

Sprinkle some #ifndef SMALL_KERNEL such that RAMDISK kernels cuild again.

pointed out by stsp@


# 1.342 17-May-2018 kettenis

Implement GenericSerialBus OpRegion support.

ok mlarkin@


# 1.341 27-Mar-2018 kettenis

Add acpicmos(4), a driver that implements SystemCMOS OperationRegion
access support. This fixes machines where the AML doesn't check whether
support for this OperationRegion type has been registered by the OS.

ok mlarkin@


Revision tags: OPENBSD_6_3_BASE
# 1.340 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.339 10-Feb-2018 deraadt

Syncronize filesystems to disk when suspending. Each mountpoint's vnodes
are pushed to disk. Dangling vnodes (unlinked files still in use) and
vnodes undergoing change by long-running syscalls are identified -- and
such filesystems are marked dirty on-disk while we are suspended (in case
power is lost, a fsck will be required). Filesystems without dangling or
busy vnodes are marked clean, resulting in faster boots following
"battery died" circumstances.
Tested by numerous developers, thanks for the feedback.


# 1.338 08-Feb-2018 deraadt

After flushing disks, try again to release all memory. We don't need to
save dirty memory to the hibernate space.


# 1.337 08-Feb-2018 deraadt

Toss all releaseable memory, because fragmentation can get in the way
of allocating the hibernate playpen.


# 1.336 08-Feb-2018 deraadt

sr_quiesce() is a new approach for ensuring that softraid drains
output to the disks.

This is part of a larger suspend/resume filesystem-safety diff, which
has been worked on for a couple of months already. Tests by job, krw,
beck, benno, and others. Sometimes even by snapshot users...


# 1.335 29-Nov-2017 kettenis

Add support for GPIO-signaled events.

ok mlarkin@


# 1.334 16-Nov-2017 jcs

Add PCI attachment for dwiic(4) needed by Intel 100 Series machines

ok kettenis


# 1.333 14-Nov-2017 kettenis

Add a few more PNP IDs to the skip lists.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.332 17-Aug-2017 stsp

Make dwiic(4) attach its ACPI parent dependencies.
This change makes the Asus E200HA keyboard work.

Original analysis and patch by Cesare Gargano
ok kettenis@


# 1.331 09-Aug-2017 dcoppa

Add "PNP0303" (8042 PS/2 Controller) to acpi_isa_hids[]

ok kettenis@


# 1.330 22-Jul-2017 jcs

acpibat: look for _BIX first before falling back on _BIF

ACPI 4.0 deprecated _BIF for battery status so some newer machines
are exporting _BIX which extends _BIF's package with a few extra
fields. When using _BIX, export the new discharge cycle count as a
sensor.

feedback from kettenis and anton
ok deraadt


# 1.329 20-Jul-2017 kettenis

Start a list of isa(4) device names such that we can avoid printing them
as "not configured".

ok mikeb@, millert@, deraadt@


# 1.328 08-Apr-2017 deraadt

Sensors are run as callbacks inside tasks. During suspend a sensor
could be running inside a driver that will be force-detached, or due
to tsleep end up disrupting the softstate/hardstate contract. At
suspend time, quisce all these callbacks by waiting for completion.
This issue has never been observed for real, but may be implicated in
suspend/resume failures.
ok kettenis guenther mlarkin


# 1.327 08-Apr-2017 deraadt

Two prototypes for acpi_maptable; merge to one place


Revision tags: OPENBSD_6_1_BASE
# 1.326 27-Mar-2017 deraadt

Now that hibernate_alloc() only has clean success/failure, don't
need to call hibernate_free() to clean up a partial mess.
ok mlarkin kettenis


# 1.325 13-Mar-2017 deraadt

allow ramdisks to build; ok jcs


# 1.324 12-Mar-2017 jcs

try to attach acpisbs first and if successful, don't attach acpibat


# 1.323 02-Mar-2017 natano

Add a new sysctl machdep.lidaction. The sysctl works as follows:

machdep.lidaction=0 # do nothing
machdep.lidaction=1 # suspend
machdep.lidaction=2 # hibernate

lidsuspend is just an alias for lidaction, so if you change one, the
other one will have the same value. The plan is to remove
machdep.lidsuspend eventually when people have upgraded their
/ets/sysctl.conf.

discussed with deraadt, who came up with the new MIB name
no objections mlarkin
ok stsp halex jcs


# 1.322 28-Feb-2017 natano

Untangle abstract sleep modes from ACPI sleep states. This paves the way
for further work in the area.

idea and ok deraadt
ok mlarkin


# 1.321 22-Feb-2017 jcs

add acpisbs, an acpi smart battery subsystem driver reading data
over smbus

currently disabled because it conflicts with acpibat

ok deraadt, kettenis


# 1.320 14-Jan-2017 kettenis

Revert aml_rdpciaddr changes; breaks several machines that were working
before.


# 1.319 11-Jan-2017 jsg

Increase the size of the buffers used for _HID and _CID strings to be
able to fit "Hyper_V_Gen_Counter_V1".

ok mlarkin@ deraadt@


# 1.318 08-Jan-2017 kettenis

Make aml_rdpciaddr() work for for devices behind bridges. The code now
uses the mapping between AML nodes and PCI devices that we establish.
Because _INIT methods may end up calling aml_rdpciaddr(), make sure we
create that mapping early. Also handle devices that aren't actually present.
These devices are now included in the mapping and reads will return an
all-ones patterm whereas writes are a no-op..

ok mlarkin@


# 1.317 25-Oct-2016 pirofti

Add the resource index to aml_parse_resource() callback function.

This is needed by an upcoming acpiec commit that handles machines breaking
the current ACPI specifications.

Change suggested by and ok kettenis@, guenther@


# 1.316 18-Sep-2016 guenther

If the APIC table (MADT) isn't present then ci_acpi_proc_id won't be set
when acpicpu tries to attach, leading to a NULL dereference. The ACPI
processor object on the Dells where this was reported is bogus and useless
anyway so apply the same test when deciding whether to attach to processor
objects as acpicpu would when matching them to cpus.

Problem reported by Eivind Eide (xenofil (at) gmail.com) and
Johan Huldtgren (johan+openbsd-bugs (at) huldtgren.com).
Testing by Eivind Eide.
ok kettenis@ mlarkin@


# 1.315 03-Sep-2016 naddy

Write the system time back to the RTC every 30 minutes.
This fixes the problem that long-running machines which were not
shut down properly would reboot with a badly offset system time.

hints and ok kettenis@


# 1.314 31-Aug-2016 mlarkin

Don't put a device in the wake device list if _STA says it's not there.

Fixes at least one machine that woke up from sleep / poweroff immediately.

ok kettenis@, tested by kettenis and martijn@


# 1.313 28-Jul-2016 kettenis

Store the acpi processor ID/UID in struct cpu_info, and use it to attach
acpicpu(4) drivers to the right cpu(4).

ok mlarkin@, guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.312 10-Jun-2016 kettenis

The power management timer has been made optional in ACPI 5.0A, so don't
attempt to attach acpitimer(4) if the timer isn't present.

Pointed out by jcs@

ok jcs@


# 1.311 08-May-2016 kettenis

Make sure devices listed by a device's _DEP method are attached before
we attach the device itself. The _DEP methode, introduced in ACPI 5.0,
is a hint that the device in question depends on OpRegion support from
the devices listed by _DEP, so we have to order them the proper way.
To prevent us from attaching those devices again when we encounter them
later walking down the device tree, keep track of our attempts to
attach a device driver to them using aflag in the aml_node structure.

ok guenther@


# 1.310 25-Apr-2016 pirofti

We attach acpihpet(4) and acpiec(4) without using the HID, so do not
report it in the dmesg.

OK deraadt@, mlarkin@.


# 1.309 02-Apr-2016 kettenis

Reduce dmesg spam by knocking out some acpi hardware IDs that don't need a
driver.

ok mpi@


# 1.308 29-Mar-2016 kettenis

Check if a device is present (using _STA) in acpi_foundhid. This prevents
us from reporting devices as "not configured" that aren't actually present,
and allows us to remove duplicated code from several drivers.

ok deraadt@, ok jsg@


# 1.307 29-Mar-2016 kettenis

Implement a cleaner way to prevent acpivout(4) from attaching if we have
a platform-specific method of controlling the backlight.

ok millert@, mlarkin@, jung@


# 1.306 28-Mar-2016 kettenis

Adding each and every new acpi device driver to acpi_foundhid() is
gettinga bit of the burden. And it really isn't how our autoconf
framework is supposed to work for busses that can be enumerated. All
the drivers already check for a matching _HID in their attach
function. So we can just drop the checks in acpi_foundhid() and let
the drivers sort themselves out.

Print a "not configured" line for devices for which we don't attach adriver.
This may be a bit much as there are devices for which we will not have a
driver. This will be removed if it causes too much dmesg spam.

ok deraadt@, mlarkin@


Revision tags: OPENBSD_5_9_BASE
# 1.305 17-Jan-2016 jsg

Take acpi_foundhid() outside of #ifndef SMALL_KERNEL so the ramdisk
kernel can find and attach dwiic(4) and sdhc(4).


# 1.304 17-Jan-2016 jsg

remove an unused global variable


# 1.303 14-Jan-2016 kettenis

Attach dwiic(4) on the Intel Bay Trail i2c controllers as well.


# 1.302 12-Jan-2016 kettenis

Improve emulation of PM registers on Hardware-redcued ACPI by emulating
SPL_TYPx, SLP_EN and WAK_STS using the new SLEEP_STATUS_REG and
SLEEP_CONTROL_REG registers. Makes the transition into S5 (powerdown) work
on the ASUS X205TA.

ok deraadt@, mlarkin@


# 1.301 12-Jan-2016 jcs

Add dwiic, a driver for the Synopsys DesignWare i2c controller found
on the Samsung ATIV Book 9 laptop. This initial version only
supports ACPI config/attachment.

Add ihidev, a HID-over-i2c driver largely based on uhidev. dwiic
handles attaching ihidev devices found in ACPI.

Add ims, a HID-over-i2c mouse/trackpad driver to get basic cursor
and button functionality from HID-compliant i2c trackpads.

ok kettenis deraadt


# 1.300 11-Jan-2016 kettenis

Make sdhc(4) attach to hardware IDs "80860F14" and "PNP0FFF".


# 1.299 09-Jan-2016 kettenis

Initial support for Hardware-Reduced ACPI. This skips the legacy mode checks
by emulating the PM_CNT register such that SCI_EN is always enabled. It also
skip attaching acpitimer(4) since the relevant registers are no longer there.

ok deraadt@, mlarkin@


# 1.298 06-Jan-2016 kettenis

Follow the spec and check the SCI_EN bit to see if ACPI has already
been enabled and don't bother enabling it again if it is. This should fix
systems that don't implement legacy mode and therefore set ACPI_ENABLE and
ACPI_DISABLE to zero.

ok mlarkin@, yasuoka@


# 1.297 23-Nov-2015 reyk

Back out the CRITICAL change, turn it back from 10% to 25%. Some
people with old/broken batteries didn't like this change, and the
underlying heuristics are wrong.


# 1.296 22-Nov-2015 reyk

The "CRITICAL" APM battery level, that is exported/faked by the ACPI
stack, is currently set to 25%. With 25% battery left, a typical
modern laptop will have at least one hour runtime left. It is an
arbitrary value, and other APM power layers in the kernel set CRITICAL
to something like 5% or 10%, so set it to 10% for now.

OK mlarkin@


# 1.295 28-Sep-2015 deraadt

In low-level suspend routines, set cold=2. In tsleep(), use this to
spit out a ddb trace to console. This should allow us to find suspend
or resume routines which break the rules. It depends on the console
output function being non-sleeping.... but that's another codepath which
should try to be safe when cold is set.
ok kettenis


# 1.294 06-Sep-2015 deraadt

clarify malloc arguments (base + length...)


# 1.293 28-Aug-2015 deraadt

fairly simple sizes for free(); ok tedu


# 1.292 20-Aug-2015 kettenis

Respect the access size when reading or writing to pci config space.
This version also makes sure writes are properly aligned, preventing
the panics seen with the previous incarnation of this change.

Fixes battery status passthrough in vmware, brightness keys on my x220
and who knows what else.

ok mlarkin@, deraadt@


Revision tags: OPENBSD_5_8_BASE
# 1.291 04-Aug-2015 deraadt

If we are setting ACPI_PM1_BM_RLD do it only once, and remember to
repeat this action at resume time.
ok mlarkin guenther


# 1.290 29-Jul-2015 deraadt

Something subtle in previous revision is locking up suspend or booting
on some laptops. Mailing lists threads mention x220 and Asus machines.
Reverting for now.


# 1.289 21-Jul-2015 kettenis

iRespect the access size when reading or writing to pci config space.
Fixes battery status passthrough in vmware and who knows what else.

Tested by many.

ok mlarkin@, deraadt@


# 1.288 17-Jul-2015 mlarkin

fix comments


# 1.287 26-Mar-2015 jsg

Use the 0x10 extended capability to determine if the trackstick
buttons are wired to the trackpad and need to be re-routed to the
trackstick. Without this change the buttons on 2015 thinkpads
get picked up as extended buttons that show up as scroll up/down.

Remove the X1 Carbon 2015 (LEN0048) and X250 (LEN0046) from the
top button area/soft buttons quirks list. Also avoid using the
quirk list entirely if the capability bit is set.

Based on work from jcs@ with feedback from mpi@
ok mpi@ jcs@


# 1.286 20-Mar-2015 kettenis

The ACPI standard says that a PCI function number of FFFF can be used to refer
to all functions on a device. This makes no sense in the context of accessing
PCI config space. Nevertheless stsp@ has a machine with AML that tries to
do config space reads from a device where _ADR encodes function number FFFF.
This is almost certainly buggy AML, but since Linux (and presumably Windows)
boot just fine on this machine we need to work around this. Linux effectively
treats function FFFF as function 7, which isn't present on this machine.
Instead we simulate a read from a non-existent device by returning a bit
pattern of all ones. Writes to function FFFF will still panic.

ok mlarkin@


# 1.285 16-Mar-2015 deraadt

Reduce use of SMALL_KERNEL in acpi, bringing acpiec driver and more gpe
handling into RAMDISK. This is now possible because the install media
has ample room. The goal is to reduce special cases where we may be
skipping (unknown) important operations...
ok mlarkin kettenis


# 1.284 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.283 07-Feb-2015 deraadt

New framework that allows hibernate to pass in entropy from it's fresh
boot.
ok mlarkin


# 1.282 06-Feb-2015 mlarkin

Fix a hibernate crash on some machines due to unmapping a page that
may not have been mapped previously (in the failure to hibernate case).

Also ensure that the lowmem ptp is mapped in all cases (not just MP).

ok kettenis


# 1.281 17-Jan-2015 deraadt

resume_randomness() before spinning up other cpus...


# 1.280 15-Jan-2015 jsg

Define a new wscons mouse type for Synaptics clickpad devices that lack
physical buttons. This will be used if the acpi pnpid for the mouse
matches a list provided by Synaptics found in Linux.

Combined with the xenocara changes this will remove the need for an xorg
config file for the x240/t440/t540 generation of thinkpads.

ok matthieu@ shadchin@ kettenis@


# 1.279 11-Jan-2015 kettenis

Tolerate checksum errors in ACPI tables (but complain about them). Apparently
there are machines out there with broken BIOSen with such checksum errors in
thier RSDT tables. Windows runs fine on these machines, so it probably does
not check. Linux ignores checksum errors as well (and prints a warning).

ok deraadt@, krw@


# 1.278 18-Dec-2014 deraadt

suspend and resume the RNG (at what looks like the right spots)
ok reyk djm


# 1.277 09-Dec-2014 doug

Replace some malloc(n*size,...) calls with mallocarray().

ok tedu@ deraadt@


# 1.276 06-Dec-2014 mpi

If "machdep.lidsuspend" is not set, do not go back to sleep if the lid
is still closed when resuming. This allows docked laptop to be resumed
without opening them.

ok mlarkin@


# 1.275 23-Nov-2014 mlarkin

If a machine has no _LIDs, then none can be open. This diff fixes a problem
where machines without any _LIDs were going back to sleep after hibernate
due to the recent suspend-if-lid-closed-on-resume diff. Noticed on qemu,
which doesn't have any _LIDs.

discussed with deraadt


# 1.274 18-Nov-2014 krw

Nuke yet more obvious #include duplications.

ok miod@


# 1.273 08-Nov-2014 mlarkin

If resuming from sleep (zzz/ZZZ) and the lid is still closed, go back to
sleep. This prevents accidental lid flex or slight opening in a backpack
from waking the machine up and leaving it resumed (powered on).

ok deraadt@


# 1.272 26-Sep-2014 kettenis

Rework piglet and pig allocation. Currently the piglet gets allocated
deep down in the suspend path, where it is really hard to recover from
allocation failure. So allocate the piglet early on in the suspend path.
Also change the piglet and piglet allocation functions to use km_alloc(9)
instead of doing pmemrange magic. This removes a bunch of code which, in the
case of the piglet allocation, is broken since it results in a NULL pointer
dereference. Also switch the piglet allocation to not wait. If we can't
allocate 16MB of phys contig memory on a halfway modern machine we're almost
certainly under a lot of memory pressure and we're better off not trying to
hibernate anyway.

ok mlarkin@


# 1.271 25-Sep-2014 kettenis

Bring back rev 1.249. Now that mp_setperf() has been fixed, it should be
safe to restore perflevel when resuming. Should fix the issues people
see with the processor running at full speed heating up the machine while
the apm command reports that the CPUs are running at a low speed.

ok dcoppa@, deraadt@


# 1.270 23-Sep-2014 kettenis

Release the acpi lock around calls to wsdisplay_suspend() and
wsdisplay_resume(). The X server may adjust the display brightness just
before/after the VT switch when suspending/resuming. In fact, it may
already be blocked adjusting the display brightness for some reason
unrelated to the suspend request.

The VT switch request will eventually time out, but the delay is annoying
and the failed VT switch will lead to problems after resume.

Needed for a future update of the xf86-video-intel driver.

ok mlarkin@


# 1.269 19-Sep-2014 kettenis

Use config_suspend_all(9).


# 1.268 08-Sep-2014 kettenis

Clear the PME Status bit on PCI devices in response to Device Wake events.
Fixes the interrupt storm on the 2009 Mac mini with WOL enabled on its
nfe(4) interface.

ok mlarkin@


Revision tags: OPENBSD_5_6_BASE
# 1.267 20-Jul-2014 mlarkin

Support hibernating to softraid crypto volumes.

much help and ok from deraadt@


# 1.266 16-Jul-2014 mlarkin

Reenable hibernate RLE support and flush+zero all memory after unpack.


# 1.265 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.264 12-Jul-2014 kettenis

Add a function to drop all clean pages on the page daemon queues and call
it when we hibernate.

ok mlarkin@, miod@, deraadt@


# 1.263 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.262 11-Jul-2014 mlarkin

Flush the buffercache to 16MB on hibernate and restore its previous max
size (kern.bufcachepercent) on resume, for better hibernate performance.

ok beck@


# 1.261 10-Jul-2014 blambert

remove unneeded header

ok mlarkin@


# 1.260 01-Jul-2014 kettenis

Adding an additonal delay after each access to SystemMemory or SystemIO
during autoconf makes no sense.

ok deraadt@


# 1.259 23-Jun-2014 kettenis

SystemMemory is used to access memory mapped registers on some machines, so we
must use the bus_space(9) API to access it instead of memcpy(9). Also make
sure we properly align access to fields that need more than byte alignment.

ok guenther@


# 1.258 21-May-2014 mlarkin

Fix invalid / missing format specifiers in several dnprintf statements
(used only when ACPI_DEBUG is enabled)

From a diff on tech@ by Fabian Raetz, with a minor change by me, tested
on i386 and amd64 with ACPI_DEBUG enabled and disabled.


# 1.257 25-Apr-2014 mlarkin

Defer the free of the hibernate structures until later, when it is safe to
do so.

Freeing the hibernate structures too early leads to inadvertent calls into
splx when we aren't ready for them yet. Found when trapping splx calls in
the resume path.

discussed with deraadt@ at m2k14


# 1.256 13-Apr-2014 mlarkin

Fix a badly worded debug message that implied the entire machine was going
to sleep, instead of just the acpi thread going to sleep.


# 1.255 31-Mar-2014 mpi

Change the order of operations during a suspend/resume cycle and
call bufq_quiesce() after executing the DVACT_QUIESCE handlers.

This should be safe since no disk nor controller drivers have such
handler but it will allow us to detach sd(4) devices attached to a
USB bus.

Another benefit pointed out by kettenis@ is that drivers that need
to read a firmware from the disk should be able to do it at resume
time in a DVACT_WAKEUP handler.

ok kettenis@, deraadt@


# 1.254 13-Mar-2014 dlg

get rid of the assumption that the head of the alldevs list is the
"mainbus" device. this breaks when mpath is enabled because it
attaches before mainbus and therefore takes the head position.

have autoconf provide device_mainbus() which looks up mainbus_cd,
and use that instead.

discussed with deraadt who just wants mpath stuff to move forward
despite there being many ways to shine this particular turd.


Revision tags: OPENBSD_5_5_BASE
# 1.253 21-Feb-2014 deraadt

Do the lid control further on in the resume sequence, since this is
AML, and will do WAITOK operations. While cold, that is a bad idea.
We seem to be safe with respect to the spec, I do not see any event
debounce issues here.
ok kettenis, tested by many


# 1.252 20-Jan-2014 claudio

Instead of checking sc->sc_revision <= 1 check that the
fadt->hdr_revision <= 1. This is a better check since it checks
the version of the proper descriptor. This allows the alix APU
to reboot since those boards have currently a wrong sc->sc_revision.
Committing this now so that it can be tested easier.
With and OK jsg@ also fine by derradt@


# 1.251 24-Dec-2013 kettenis

If the FADT has its SMI_CMD set to zero, assume we're only ACPI-only hardware
and don't need to disable SMI ownership of the ACPI hardware registers.

ok mlarkin@


# 1.250 23-Dec-2013 kettenis

Revert previous commit. It makes resume hang on some MP systems running
GENERIC.MP.


# 1.249 16-Dec-2013 kettenis

Restore hw.setperf upon resume like we do for apm(4) on i386. Seems we need
to do this fairly late in the resume to avoid hangs.

ok deraadt@, mlarkin@


# 1.248 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


# 1.247 06-Nov-2013 mpi

Add support for Power Resources for Dx states and the necessary hook
for PCI devices. This hook should be called twice, before and after
changing the power state of a PCI device.

Before setting the device to the new state, the ACPI layer will notify
every power resources linked to the device for that state and make sure
they are turned "_ON". After changing the state of the device, it will
decrement the reference of every power resources linked to that device
for the old state and turn them "_OFF" if they are no longer referenced.

This fixes the no-USB after resume problem seen on various ThinkPad,
problem initialy diagnosed with Alexander Polakov.

ok kettenis@, deraadt@


Revision tags: OPENBSD_5_4_BASE
# 1.246 01-Jun-2013 mlarkin

Stop printing ACPI wakeup devices in dmesg after the 16th wakeup device.

Helps with VMware VMs that report hundreds of wakeup devices.

ok reyk@, deraadt@


# 1.245 31-May-2013 bluhm

Fix the build for a kernel without wd and pciide in its config.
OK deraadt@ tedu@


# 1.244 30-May-2013 deraadt

Enforce ca_activate tree-walks over the entire heirarchy for all events,
cleaning up some shutdown-hook related code on the way.
(A few drivers related to sparc are still skipped at kettenis' request)
ok kettenis mlarkin, tested by many others too


# 1.243 18-Apr-2013 deraadt

pin the acpi thread to the BSP, because AML and SMI cannot be trusted. I
was certain we were doing this already, but it turns out no...


Revision tags: OPENBSD_5_3_BASE
# 1.242 24-Dec-2012 guenther

In acpiioctl(), Return though the splx() path if suser() returns an error

ok deraadt@


# 1.241 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


# 1.240 04-Oct-2012 ehrhardt

Use information provided by ACPI to attach secondary PCI host bridges.

ok kettenis@


# 1.239 07-Sep-2012 kettenis

Add a new function called acpi_pci_min_powerstate() that returns the lowest
power state for a PCI device for the power state (currently S3 or S4) we're
transitioning into. This function is probably too simplistic; there is a lot
of (silly) complexity in the ACPI spec.

ok deraadt@


Revision tags: OPENBSD_5_2_BASE
# 1.238 13-Jul-2012 pirofti

Disable acpivideo(4) on ASUS laptops.

There are some models that panic whenever an event is received.
Some cope, some don't. In order to get this right acpiec(4) requires
major rework. Until then let machines boot.

Discussed with deraadt@.


# 1.237 11-Jul-2012 mlarkin

for now, restrict hibernate to root in the acpi ioctl kernel interface.
normal rules for controlling this interface through apmd still apply.

ok deraadt@


# 1.236 09-Jul-2012 deraadt

tiny little oops in untested hibernate block


# 1.235 09-Jul-2012 deraadt

clean up ioctl() case statements


# 1.234 09-Jul-2012 deraadt

acpi_disable_onegpe() is not used; ok pirofti


# 1.233 24-May-2012 kettenis

If _MIN > _MAX consider a WordBus resource to be invalid. Fixes a panic on
the IBM x3550-M4.

ok mikeb@


# 1.232 29-Mar-2012 mlarkin

Missing #ifdef in previous diff


# 1.231 29-Mar-2012 mlarkin

Prevent hibernation attempts on machines with unsupported disk controllers.
The same check is also performed later in the hibernate sequence, but by
then it's too late to cleanly unwind (presently).

ok deraadt


# 1.230 26-Mar-2012 deraadt

simplify conditionals in acpi_sleep_state()
ok mlarkin


# 1.229 26-Mar-2012 deraadt

hook in the hibernate request code; half of this diff is from mlarkin
ok mlarkin


Revision tags: OPENBSD_5_1_BASE
# 1.228 20-Sep-2011 deraadt

Very early in suspend while the clock is still ticking realtime,
save the clock back to the rtc. The zaurus already did this.
ok phessler miod kettenis


Revision tags: OPENBSD_5_0_BASE
# 1.227 02-Jul-2011 nicm

kqueue attach functions should return an errno or 0, not a plain 1. Fix
the obvious cases to return EINVAL and ENXIO.

ok tedu deraadt


# 1.226 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.225 16-Jun-2011 pirofti

Import acpitoshiba driver.

This is originally written by Hiroyuki Aizu, ported and integrated
in our tree by Javier Vazquez <javama4 AT gmail> with tweaks and
suggestions by me.

Import prodded by deraadt@ and the lack of testers on tech@.


# 1.224 27-Apr-2011 jcs

attach acpithinkpad to newer lenovo models like the x120e

ok deraadt@


# 1.223 22-Apr-2011 jordan

Fix uninitialzied variables and formatting strings (-Wxxx errors)


Revision tags: OPENBSD_4_9_BASE
# 1.222 02-Jan-2011 jordan

Re-add code for ACPI taskqueue, eliminates need for state vars in acpi softc
Serialize processing ACPI tasks for sleep/powerdown/GPE event
ok deraadt@


# 1.221 31-Oct-2010 guenther

Revert last commit: it breaks resume on Thinkpads
"then please back it out" deraadt@


# 1.220 26-Oct-2010 jordan

Add task queue for ACPI gpe and notify handlers
This fixes eject on Dell Latitude dock and an issue on the Dell Mini
battery update.
ok deraadt


# 1.219 07-Oct-2010 jordan

Create separate functions for enabling wake and runtime gpes.
Remove old suspend/resume gpewalk function
ok deraadt, mlarkin


# 1.218 05-Oct-2010 jordan

split acpi_enable_onegpe to separate enable/disable functions
ok deraadt


# 1.217 05-Oct-2010 jordan

Added acpi_disable_allgpes function to clear GPE status
ok deraadt


# 1.216 05-Oct-2010 deraadt

clean up acpi_softc use more


# 1.215 05-Oct-2010 deraadt

delete a bit of #if 0 code


# 1.214 05-Oct-2010 deraadt

hide use of our global acpi_softc pointer a bit better


# 1.213 29-Sep-2010 deraadt

grab the acpi thread lock very early in acpi thread startup; not that it
is likely that anyone will race us... but why be careless.
ok kettenis


# 1.212 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.211 29-Aug-2010 jasper

- add a check for FWRITE here too.

ok miod@


# 1.210 27-Aug-2010 kettenis

Make PCI Power Management optional, and only enable it when acpi(4) attaches.

ok deraadt@


Revision tags: OPENBSD_4_8_BASE
# 1.209 08-Aug-2010 kettenis

Close race between the acpi thread and wscons ioctls running in process
context. We might rip this out post-release and replace it with a solution
that serializes things in a more obvious way.

ok deraadt@, miod@ and "those crazy texans who ok anything"


# 1.208 08-Aug-2010 deraadt

Report the acpi sleep states when on a RAMDISK; ok krw


# 1.207 07-Aug-2010 kettenis

Enforce acpi register access rescrictions.

ok mlarkin@, deraadt@, tested by marco@


# 1.206 07-Aug-2010 deraadt

upon resume, notify all the acpiac and acpibat drivers to update
their status. some laptops do not do this automatically.
ok kettenis canacar


# 1.205 06-Aug-2010 marco

Always call _PSW on _LID devices that support it to enable lid open to
resume the machine. Conversely disable it when the machine wakes up.

Tested by several ok deraadt


# 1.204 06-Aug-2010 deraadt

As part of a revamp of the PM1/GPE code, I write this diff to look for
unmanaged EN & STS bits in the PM1 register at interrupt time and report
them. As a side effect this splits the STS acknowledgement into two writes
(for power, and sleep) instead of one. The printf that is added (to spot
unmanaged STS bits) has not yet been triggered as far as we know.

Before the "write to PM1 registers at the right offset" diff went in, this
was not neccessary. But newer thinkpads do not have a working soft power
button without this diff.

We have no idea why.
ok mlarkin kettenis


# 1.203 05-Aug-2010 deraadt

PM1 power button events were generating 'fake events' to acpibtn PWRB,
but if there is no such device these were being tossed. Instead, arrange
for all power button handlers to flag a softc variable which is processed
by the thread; so that one place will know to request a powerdown (psignal
to init)
ok marco mlarkin


# 1.202 05-Aug-2010 deraadt

rename a variable which is annoyingly poorly named


# 1.201 05-Aug-2010 mlarkin

Write to the correct offset for various ACPI pmreg registers. Writing to
the correct register does properly clear the wake status bits and fixed
event bits, which allows some machines to have more than 1 suspend cycle.

bug spotted by deraadt@

tested by deraadt@ and myself on a half-dozen machines and others on
hackers@

ok deraadt@, marco@


# 1.200 05-Aug-2010 deraadt

Fix the power button. The pm1 register is 16 bits in size.
My mistake, a while back.
tested by krw


# 1.199 04-Aug-2010 kettenis

Print supported sleep states.

ok marco@, phessler@, deraadt@


# 1.198 03-Aug-2010 mlarkin

Use the proper flag for re-enabling certain hardware events (power btn,
sleep btn). The (incorrect) flag prevoiously used coincidentally had the
same value, so this shouldn't cause any different behavior than before.

tested on a variety of machines (i386, amd64, sp, mp) - no behavioral
change seen

ok deraadt@


# 1.197 28-Jul-2010 marco

Make all gasio failures look and behave the same.


# 1.196 28-Jul-2010 mlarkin

Delay a little longer on suspend before giving up. Fixes machines that
sometimes don't suspend because the sleep transition takes too long
(symptoms like failing to sleep but the backlight stays on).

ok deraadt@


# 1.195 27-Jul-2010 deraadt

remove XXX; EC can now be attached early when needed; ok jordan


# 1.194 27-Jul-2010 deraadt

more brackets feels good


# 1.193 27-Jul-2010 deraadt

XXX annotate a block in acpi_enter_sleep_state() that is pretty worrying


# 1.192 27-Jul-2010 deraadt

horrific indentation that nearly made me puke


# 1.191 27-Jul-2010 mlarkin

Set the sleeping indicator light on machines that support it, in case SMI
doesn't do it for us. Shows the 'moon' LED on older Thinkpads when
sleeping.

Tested on T43p, W500, T510 by me and on X61s by deraadt@.

ok deraadt@


# 1.190 27-Jul-2010 jordan

Early initialization of acpiec if ECDT table exists
Fixes hang when booting thinkpads while docked
ok deraadt


# 1.189 26-Jul-2010 deraadt

remove merge error which was commited


# 1.188 26-Jul-2010 pirofti

Add support for Sony ACPI hotkeys via a new driver: acpisony(4).

Currently it only works for the suspend button and tries to do right for
the brightness events, but I haven't found a Sony laptop that like to do
right so far.

In the future I want to make the brightness keys work on all Sony's and
also add support for the zoom hotkeys and whatever other funky keys I can
find on those things.

Okay deraadt@.


# 1.187 22-Jul-2010 deraadt

pretty large cleanup of SMALL_KERNEL


# 1.186 21-Jul-2010 deraadt

in the acpi GPE handler, late ack level interrupts not edge interrupts.
Duh.


# 1.185 21-Jul-2010 deraadt

Merge the edge and level gpe handlers into one (now possible since the
gpe structure tells us whether the gpe is edge or level)


# 1.184 21-Jul-2010 deraadt

delete #if 0 code we are not going to use; ok marco


# 1.183 20-Jul-2010 deraadt

make the mask code for acpi_enable_onegpe() match other similar code


# 1.182 20-Jul-2010 deraadt

Handle the acpi interrupt controller with proper edge/level handling,
wrapped in spltty to avoid racing against the interrupt controller.
Repair the gpe bit masking code while there, and do operations in the
right order
ok kettenis mlarkin, help from jordan at figuring out the order of
operations.


# 1.181 20-Jul-2010 matthew

When we're preparing to go to sleep, only call bufq_restart() if other
preparations failed.

ok kettenis@, thib@, deraadt@


# 1.180 19-Jul-2010 deraadt

permit compilation with ACPI_DEBUG


# 1.179 19-Jul-2010 deraadt

there is no need for a seperate dsdt_softc pointer; dsdt does not have a
softc but is just a part of the single acpi driver.
ok jordan


# 1.178 19-Jul-2010 deraadt

acpi_delay() is no longer needed; ok jordan


# 1.177 18-Jul-2010 mlarkin

Clear event status on resume, to avoid going back to sleep/reboot/shutdown
immediately after resume. Tested by a bunch of people on hackers@ on
various hardware. Fixes at least Toshiba NB200 and Gateway LT3103u.

ok marco


# 1.176 13-Jul-2010 deraadt

rename some thread related interfaces so they are logical


# 1.175 10-Jul-2010 jordan

Verify _ADR bdf is correct (some systems return 0xffff)
Works on Dell x9xx and Studio 1555


# 1.174 09-Jul-2010 jordan

Clear the noise


# 1.173 08-Jul-2010 jordan

Add mapping for ACPI device to PCI bdf (match autoconf tree)
Simplify resource parsing function to use buffer argument
Convert namespace linked lists to use queue macros
ok marco@, deraadt@


# 1.172 06-Jul-2010 deraadt

Use spltty() to lock downcalls from apm against the information being
modified by the acpi thread.
ok kettenis


# 1.171 06-Jul-2010 deraadt

Don't use a workq to do the suspend, because it races aginst the acpi
thread. Instead, just tell the acpi thread to do the suspend for us.
This makes apmd & zzz work correctly. While here, have acpithinkpad
attempt to post the event to apm before prompting the suspend itself.
ok kettenis marco mlarkin


# 1.170 05-Jul-2010 mlarkin

Reset register SCI_EN on resume. The ACPI spec clearly states we don't have
to do this, but many machines don't, or forget to, which leads to all sorts
of problems on resume (broken battery stats, overheating, failure to resume
more than once, etc).

tested by many, ok marco@, deraadt@


# 1.169 01-Jul-2010 thib

do bufq quiesce again, since vnd/the bufq_done call in
biodone has been fixed.


# 1.168 01-Jul-2010 jordan

Backout recent AML changes, breaks on R210 and others


# 1.167 01-Jul-2010 jordan

Simplified aml_parse_resource call, cleanup code
ok marco


# 1.166 01-Jul-2010 jordan

Fix subordinate bus # for multi-root PCI buses
ok marco, kettenis


# 1.165 30-Jun-2010 thib

Disable/partially backout the bufq quiesce changes as this
is causing havoc with vnds and release must be buildable.


# 1.164 29-Jun-2010 deraadt

Fix ramdisks (new code was placed inside a SMALL_KERNEL chunk)


# 1.163 29-Jun-2010 jordan

Add support for mapping ACPI to PCI devices
ok kettenis, deraadt


# 1.162 29-Jun-2010 kettenis

Call bufq_quiesce() on suspend to stop all I/O and bufq_restart on resume.
Makes suspend/resume work much better while doing disk I/O.

ok mlarkin@


# 1.161 29-Jun-2010 tedu

60 is a more reasonable guess for number of minutes in an hour than 100.
spotted by TeXitoi. ok deraadt


# 1.160 27-Jun-2010 jordan

Add _acpi_matchhids routine, precursor to pci device match


# 1.159 27-Jun-2010 mlarkin

Clear ACPI fixed event status on resume (power buttons/etc) so that some
machines don't immediately resume after suspending to S3.

Tested by kettenis@ and I on 5 or 6 machines, deraadt@ "yeah, looks good"


# 1.158 27-Jun-2010 jordan

Cleaned up acpi_maptable, adds to table list directly


# 1.157 07-Apr-2010 deraadt

If we have done a wsdisplay_suspend(), and ACPI decides it does not want to
suspend, we must roll back by calling wsdisplay_resume() or the virtual
console switching will be locked forever
ok oga


# 1.156 07-Apr-2010 kettenis

Make suspend/resume work on MP machines (running an MP kernel). Joint work
from mlarkin@ and me, with some amd64 fixes thrown in by deraadt@

ok marco@, deraadt@, pirofti@, mlarkin@


# 1.155 31-Mar-2010 kettenis

Bring /dev/apm support in line with apm(4). Make the suspend button send
a suspend request event to apmd(8) instead of suspending immediately. Also
keep track of whether /dev/apm and /dev/apmctl are currently open, such that
we can still suspend immediately if apmd(8) isn't running.

ok deraadt@, marco@, pirofti@, jsing@, oga@


# 1.154 30-Mar-2010 oga

Prevent the apmd/x races for good.

When we hit suspend time, go through all wsdisplays on the system. if
they are in mode MAPPED, but not MODE_DUMBFB then if possible do a full
vt switch to a !mapped vt, and prevent switching back until resume time.
This has to be called from MD code because this involves userland
running so that X can run the vt switch signal handler. This way, any
case where we are using the "poke registers from userland" model, we
will not be on the hardware when we go down, so the kernel can actually
handle thing properly.

Tested on several acpi laptops (by kettenis@ and ian@), x40 (me and
beck@ at LEAST) and zaurus (me). Maybe others, but if so I forgot who at
this time..

Idea from deraadt somewhere over the Faroe Islands (I thought of a
similar thing myself a while ago). Much prompting from him. Ok and
comments miod@


# 1.153 25-Mar-2010 oga

Fix & vs. && in the kqfilter code.

Based on a comparison with the apm code.

ok deraadt@, kettenis@


Revision tags: OPENBSD_4_7_BASE
# 1.152 26-Nov-2009 mlarkin

Visual (and on some machines, audio) feedback while the machine is
resuming via calls to aml _SST method. This also makes the "moon" LED on
thinkpads to blink during resume and cease when resume is completed.

ok pirofti@


# 1.151 26-Nov-2009 deraadt

Toggle into "cold" when we we disable interrupts, because tsleeping
in the AML interpreter and context switching into userland processes
tends to sometimes pop the ACPI trampoline page out of our address
space. Makes lots more machines work.
ok kettenis mlarkin


# 1.150 24-Nov-2009 jsg

in the resume path evaluate _WAK before dealing with device drivers
like the spec says, not after.

ok mlarkin@


# 1.149 24-Nov-2009 deraadt

abort suspends on SMP systems until that gets written; ok mlarkin


# 1.148 23-Nov-2009 mlarkin

pci should return the result of it's children's suspend/resume calls back
to its parent.

handle suspend failure case and unwind if devices have an activate function
that returns failure.

ok deraadt@, kettenis@


# 1.147 23-Nov-2009 mlarkin

acpi* devices need to call their children on suspend/resume.
ok deraadt@


# 1.146 23-Nov-2009 pirofti

Remove ACPI_SLEEP_ENABLED checks.

This enables by default the suspend/resume paths in the kernel.

Okay deraadt@.


# 1.145 23-Nov-2009 deraadt

knf and spacing that are super annoying


# 1.144 23-Nov-2009 mlarkin

Hook up Theo's new children suspend/resume function, as well as more ACPI
suspend/resume glue.

ok deraadt@


# 1.143 26-Oct-2009 deraadt

Do not do strncmp comparisons on dv_xname because longer device names which
look similar could arrive in the future. Instead, compare directly against
dv_cfdata->cf_driver->cd_name
Issue originally spotted by miod


# 1.142 26-Oct-2009 jordan

Verify checksum+address when loading ACPI tables.
Some systems had invalid entries in RSDT/XSDT.
ok marco@


# 1.141 23-Jul-2009 cnst

New aibs(4) driver for ASUSTeK AI Booster (ACPI ATK0110) hardware monitoring,
with sensor state support through limits provided by the ACPI.

Tested on several ASUS motherboards kindly networked by Sam Fourman Jr.

ok deraadt marco jordan


Revision tags: OPENBSD_4_6_BASE
# 1.140 03-Jun-2009 pirofti

Import acpi power resource driver. This is needed for suspend/resume logic.

Okay jordan@ and marco@.


# 1.139 03-Jun-2009 pirofti

Add acpivideo support. This adds brightness support for all laptops
except thinkpads, they will use the acpithinkpad driver. The driver is
also hooked into wsconsole. So brightness can be adjusted via:

$ wsconsctl display.brightness=<percentage>

This is very helpfull on some laptops that have a nasty bios and get two
steps instead of one when pressing the brightness button.

Tested on various dell, fujitsu, acer, samsung and other laptops.

Okay marco@, miod@. Suggestions from kettenis@.
Lots of reviews and help from miod@, thanks!


# 1.138 03-Jun-2009 jordan

Added ejectable bay for IDE cdroms
ok marco@


# 1.137 30-Apr-2009 marco

Oga changed apm ioctls and forgot to grep. Temporary work around by hitting
the sleep button when we get any request. This interface will chnage at
some point.
ok oga


# 1.136 19-Apr-2009 krw

Call acpi_sleep_walk() when powering down just like when entering
any other sleep state. This tells acpi wake devices to not wake up
the box until manual powering up resets their status. Makes my Dell
GX520 stay powered off in response to 'halt -p'.

In snaps for a while.

ok marco@ (several times) deraadt@


# 1.135 17-Apr-2009 pirofti

Add device ranking support for future sleep/wake developments.

okay marco@


# 1.134 11-Mar-2009 jordan

Cleanup.. use aml_evalinteger instead of aml_evalname
ok marco@


# 1.133 10-Mar-2009 jordan

Fix cut/paste error.. call _BFS not _PTS
ok marco@


Revision tags: OPENBSD_4_5_BASE
# 1.132 21-Feb-2009 marco

overzealous cut/paste; found by Vladimir Kirillov <proger@uaoug.org.ua>


# 1.131 20-Feb-2009 marco

Oops didn't mean for Debugger() to remain there.


# 1.130 19-Feb-2009 marco

suspend/resume bits so that we can develop this in tree. This is disabled.
code from mlarkin and me
help from art,toby,jordan and several others
ok jordan, go for it deraadt


# 1.129 10-Feb-2009 jordan

Fix null pointer for shutdown if acpi not enabled
ok marco@


# 1.128 04-Feb-2009 kettenis

Some systems that claim to be ACPI 2.0 apparently don't provide an XSDT
address. Make the code fall back on the RSDT on those systems.

From Joerg Niendorf <p3y80@internode.on.net>. Fixes PR 5986.


# 1.127 17-Jan-2009 krw

A couple of stray malloc/memset -> malloc(M_ZERO), a bit of knf and
lose a couple of unnecessary casts. No functional change.

Feedback from kettenis@

ok kettenis@ marco@


# 1.126 06-Nov-2008 marco

Collapse all sanity checks of hids into one function.

Originaly from jordan


Revision tags: OPENBSD_4_4_BASE
# 1.125 02-Jul-2008 fgsch

add acpivideo. for now almost an empty skel so others can work on
suspend/resume. eventually it will also manage output switching and
brightness where supported. prodded by marco@


# 1.124 11-Jun-2008 marco

Fix $OpenBSD$ for once and for all


# 1.123 08-Jun-2008 deraadt

aaa_dev was being set from an uninitialized variable; ok toby


# 1.122 07-Jun-2008 marco

Kill ACPIVERBOSE. It was never used and is worthless to boot.

comments jmc
ok toby


# 1.121 06-Jun-2008 marco

Shrink acpi some more for floppies and while here re-enable acpi on the
floppies. Fixes crazy sony vaio kettenis has.

tested by deraadt
ok deraadt jordan kettenis


# 1.120 01-Jun-2008 marco

Fix \n nit in SMALL_KERNEL.


# 1.119 01-Jun-2008 marco

Re-enable new parser. Lots of testing by various folks. Fits all media
now too.

ok kettenis deraadt


# 1.118 16-May-2008 dlg

back out jordans new parser.

it didnt have enough oks from the right people, it did break machines
that were previously working, and it bloated the RAMDISKS and install
media beyond the constraints of the media.

requested by deraadt@


# 1.117 14-May-2008 jordan

Adding new ACPI Parser code
Old guts of code still exists, needs to be torn out next
ok marco@
ok brad@


# 1.116 27-Apr-2008 jcs

add acpithinkpad(4), an acpi support driver for ibm/lenovo
thinkpads.

provides functionality that the bios did on older, non-acpi
thinkpads like respond to screen brightness keys and toggle the
bluetooth device on and off.

ok deraadt@ and marco@


# 1.115 24-Apr-2008 jsing

Introduce acpiasus(4), a driver for the ACPI based hotkeys found in many
ASUS laptops (including the ASUS EeePC) - largely based on NetBSD's
asus(4) driver. On the ASUS EeePC this allows us to enable/disable
wireless, change screen brightness and use the volume keys.

ok jsg@, weingart@


# 1.114 12-Apr-2008 kettenis

Get rid of acpi_s5 global variables; simply send SIGUSR2 instead of SUGUSR1
to tell init(8) to power down the machine.

ok krw@


Revision tags: OPENBSD_4_3_BASE
# 1.113 21-Jan-2008 jsg

Check against RESET_REG_SUP in FADT before doing the acpi reset dance
after all, not doing so appears to break more machines than it fixes.

ok marco@


# 1.112 05-Jan-2008 kettenis

Never attach more acpicpu(4) devices than cpu(4) devices. Always attach
the first acpicpu(4) device on non-MULTIPROCESSOR kernels, regardless of its
ACPI processor ID.

ok mikeb@


# 1.111 05-Dec-2007 deraadt

Hang acpi(4) below bios(4) instead of mainbus(4). This lets us move acpi(4)
before pcibios(4), and then.... if acpi(4) attaches, skip pcibios(4) since
it messes the machines up. Fixes claudio's HP dl320 G5, and almost assuredly
others.
ok kettenis


# 1.110 05-Dec-2007 deraadt

repair acpiopen/acpiclose and friends to handle the /dev/apmctl interface,
so that apmd(8) can actually work correctly against acpi(4), and thus,
even do the fancy -C/-A stuff; also tested by jmc


# 1.109 02-Dec-2007 fgsch

fix the trailing space for real now.


# 1.108 02-Dec-2007 fgsch

avoid trailing spaces on output; marco@ ok.


# 1.107 27-Nov-2007 kettenis

Fix off-by-one that made us attach acpicpu0 and acpicpu1 on GENERIC kernels.

ok marco@


# 1.106 25-Nov-2007 jsg

When available and ACPI is enabled use the ACPI reset method
to reboot. This is required for some quirky sis machines
and likely others to reboot.

Tested by many. 'commit early commit often' deraadt@


# 1.105 19-Nov-2007 deraadt

sigh, amd64 still uses that stupid acpi_enabled variable...


# 1.104 19-Nov-2007 deraadt

softc's come initialized to 0, you know...


# 1.103 19-Nov-2007 deraadt

store revision; from jsg


# 1.102 19-Nov-2007 kettenis

Initialize sc_facs earlier.

ok deraadt@, beck@, canacar@


# 1.101 16-Nov-2007 deraadt

fix the bus_space #define nightmare, so that amd64 and i386 are much more
uniform. as a result shared code like acpi needs less #ifdef's
ok marco kettenis


# 1.100 15-Nov-2007 deraadt

remove the #ifdef ACPI_ENABLE. the EC codepath is either safe enough (and
will be improved), or it is unsafe because AML assumes it must be there
ok almost everyone


# 1.99 15-Nov-2007 mikeb

Make acpicpu(4) attach to the existing CPUs only.

Problem reported by form@. Fix idea by kettenis@.
Lots of help from deraadt@. Tested by deraadt@, form@ and me.

ok deraadt marco


# 1.98 14-Nov-2007 deraadt

spacing


# 1.97 12-Nov-2007 deraadt

correct : vs , in attach-time messages


# 1.96 06-Nov-2007 deraadt

all ACPI_ENABLE kernels need acpi_interrupt()


# 1.95 06-Nov-2007 deraadt

move acpi_read_pmreg() and acpi_write_pmreg() out of SMALL_KERNEL so
that acpi ramdisks can compile (obvious solution)


# 1.94 05-Nov-2007 canacar

Establish the acpi interrupt earlier. Fixes problems with some
GENERIC.MP kernels not getting acpi interrupts. This is a
workaround until the real issue with late establishment is fixed.
ok weingart@, beck@, gwk@


# 1.93 03-Nov-2007 jordan

Added support for displaying wakeup devices
ok beck@,weingart@,gwk@


# 1.92 11-Oct-2007 kettenis

When walking the tree yo find _PRT methods, check whether the device is
actually enabled and functioning. Similar to what we do for _INI.

ok weingart@, beck@


# 1.91 08-Oct-2007 krw

More simple memset(,0,) -> M_ZERO changes. In this batch move to
size(*p) as the first malloc() parameter where p is declared locally
and thus easy to check. Add M_ZERO to gpe_table allocation in acpi.c
even though there is no obvious bzero or memset nearby.


# 1.90 13-Sep-2007 mk

Remove unnecessary memset() (see aml_evalnode(9)) and a bit of KNF.

ok toby


# 1.89 13-Sep-2007 weingart

Implement ACPI 6.5.1 spec tree walk for _STA and _INI.
Fixes mk's laptop. No regressions so far (thank you to the
testers).

ok gwk@, mk@, marco@


Revision tags: OPENBSD_4_2_BASE
# 1.88 17-Apr-2007 mk

Now that acpidock no longer hangs machines during boot (at least
according to the reports I've been getting) it should be safe to enable
it again, so remove the #if 0 stuff around it.

ok marco


# 1.87 11-Apr-2007 jordan

Added changes for C-State
ok marco@


# 1.86 26-Mar-2007 marco

Calculate minutes left more accurately.

From giovanni <qgiovanni@gmail.com>


Revision tags: OPENBSD_4_1_BASE
# 1.85 03-Mar-2007 mk

Stop looking for acpidock until we fix that stuff that breaks all pre-60
series ThinkPads with acpidock(4) enabled.


# 1.84 22-Feb-2007 marco

We were getting no interrupts in .MP. There are 2 fixes; one in
acpi_machdep.c and move the interrupt establishment down after ioapic
has been initialized.

ok jordan


# 1.83 21-Feb-2007 kettenis

Make an acpi-enabled GENERIC.MP work on systems that only support PIC mode.

ok marco@


# 1.82 19-Feb-2007 jordan

Added acpi_map_address function for mapping GAS/IO registers
ok marco@


# 1.81 18-Feb-2007 jordan

Added dynamic perfect hash for opcodes
ok marco@


# 1.80 18-Feb-2007 marco

Shave off another K pre compression.
Shuffle SMALL_KERNEL functions around so that we only have a single #ifndef


# 1.79 17-Feb-2007 marco

Add SMALL_KERNEL throught acpi to make it fit on boot media.
ok deraadt


# 1.78 13-Feb-2007 marco

Add smarter _INI code. This helps with some dissapearing devices.

ok kettenis jordan


# 1.77 31-Jan-2007 gwk

Introduce acpi_hasprocfvs thats "ACPI Has Processor Frequency and Voltage
Scalling" a global flag set by acicpu attach to indicate that ACPI is
handling fvs through the _PSS objects. This will fix disappearing
EST/powernow when booting kernels with acpi enabled, as acpicpu isnt
compiled into GENERIC yet. Also in cases where acpicpu is built in and
legacy EST/powernow work but acpi is missing the _PSS object, the legacy
routines will be used.

tested by dim@, and Nick Nauwelaerts <nick AT nauwelaerts.net>
ok marco@, dim@


# 1.76 22-Jan-2007 mk

Actually look for docking stations by looking for _DCK nodes.

ok marco


# 1.75 02-Jan-2007 marco

On some systems EC must be attached as the first device. Move EC from
generic _HID devices to its own routine to force first attach. Bug found
and debugged by joris@

Code mostly written by joris@


# 1.74 26-Dec-2006 marco

Add polling to devices that require it such as acpibat and acpitz.
Use the same tsleep/wakeup mechanism as the interrupt code to keep it non-
concurrent.


# 1.73 21-Dec-2006 deraadt

even more knf love


# 1.72 21-Dec-2006 marco

Remove some leftover acpiec gpe/interrupt code.


# 1.71 20-Dec-2006 deraadt

oops, name already used


# 1.70 20-Dec-2006 deraadt

rename __acpi_enable_gpe(); ok marco


# 1.69 20-Dec-2006 deraadt

acpi_unmap_pmregs() is not used, ok marco


# 1.68 19-Dec-2006 deraadt

simplify some low-level methods substantially (shrinking code); ok marco jordan


# 1.67 06-Dec-2006 mk

malloc(..., M_NOWAIT) with no NULL check is bad. If allocation fails,
complain, clean up, and bail out of attach.

input and ok gwk


# 1.66 28-Nov-2006 jordan

Fixed clearing arg for acpi_init_pic
ok kettenis@


# 1.65 27-Nov-2006 kettenis

Don't spam dmesg.


# 1.64 27-Nov-2006 jordan

Fixes for acpiprt routing
Post-parse fixup for namerefs
ok kettenis@


# 1.63 25-Nov-2006 marco

Don't handle acpiev events inside the gpe handler since this causes deadlocks.

ok jordan@


# 1.62 15-Nov-2006 kettenis

Hook up acpi apic interrupt routing support to generic acpi code.

ok jordan@


# 1.61 02-Nov-2006 marco

Kill acpiec attach function and add its functionality to the generic _HID
device attach

prodded and ok jordan


# 1.60 02-Nov-2006 marco

Fix comments to reflect reality.


# 1.59 19-Oct-2006 marco

Replace ACPI ioctls with APM ioctls. This makes ACPI talk the same protocol
as APM therefore userspace can now retrieve power information in the same
fashion as all other apm arches (zaurus, macppc, i386 etc).

To use force the use of acpi device like so: apmd -f /dev/acpi

ok jordan@


# 1.58 19-Oct-2006 jordan

Fixed GPE debug levels
Fixed ref counter on temporary bufferfields
DSDT parsing fully working on IBM Thinkpad
ok marco@


# 1.57 15-Oct-2006 jordan

Removed old GPE handler code remnants
ok marco@


# 1.56 12-Oct-2006 jordan

Massive ACPI changes
Fixed ACPI leaks
new ACPI API
ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.55 30-Jun-2006 gwk

KNF, No functional change,
"go at it" marco@


# 1.54 30-Jun-2006 jordan

Support for calling Embedded Controller via new GPE interface
ok marco@


# 1.53 30-Jun-2006 jordan

Added new code for GPE handling
Also works with ACPIEC; need to fixup acpiec.c to remove original interrupt handler
ok marco@


# 1.52 30-Jun-2006 jordan

Added offset argument for acpi read/write pmreg routines (new GPE code)
ok marco@


# 1.51 31-May-2006 todd

do not call acpi_powerdown() unless acpi has successfully initialized
ok gwk@ canacar@


# 1.50 29-May-2006 canacar

Add embedded controller support.
ok marco@ deraadt@ testing & ok gwk@


# 1.49 19-May-2006 canacar

Minimal thermal zone driver. Monitors thermal zone temperature,
shuts down the system if the 'critical temperature' is reached.
ok marco@


# 1.48 11-Apr-2006 marco

KNF.


# 1.47 11-Apr-2006 gwk

Add acpi_resume(struct acpi_softc *sc), which calls some AML methods,
detected at attach time, currently unused.

ok marco@


# 1.46 11-Apr-2006 gwk

Discover some ACPI sleep related methods, and call them at the
appropriate time in acpi_enter_sleep_state().

ok marco@


Revision tags: OPENBSD_3_9_BASE
# 1.45 26-Feb-2006 marco

Add acpicpu as a device.

ok jordan@


# 1.44 23-Feb-2006 marco

remove dead function.

ok jordan@


# 1.43 22-Feb-2006 marco

Print out all tables it found during autoconf. We'll need this data in
the future.


# 1.42 22-Feb-2006 marco

Make dmesg less verbose.

ok jordan.


# 1.41 22-Feb-2006 jordan

Added parsing of SSDT tables
ok marco@


# 1.40 22-Feb-2006 jordan

Added new aml_register to notify by device id
Moved powerdown code to button device
ok marco@


# 1.39 21-Feb-2006 marco

Readjust debug levels that crept in too low over the last few days.
KNF


# 1.38 21-Feb-2006 marco

Rig the power button so that the machine shuts down correctly.


# 1.37 20-Feb-2006 marco

Add acpibtn device. Remains disabled for now.


# 1.36 19-Feb-2006 jordan

Added code for aml_notify GPE notification callback
ok marco@


# 1.35 19-Feb-2006 grange

Fix acpi_enter_sleep_state() by replacing current ugly hack
with mostly correct sleeping state transition code.

ok brad@ marco@


# 1.34 19-Feb-2006 marco

jordan:
Enable interrupts.
Added peripheral code to make this work.

marco:
Replace softintr and other types of interrupts with a handler with context.
Fixed interrupt magic for GPEs.
Some KNF

This is work done by jordan and I but has to be a single commit.


# 1.33 17-Feb-2006 marco

Remove earlier introduced delays. We really only need them during autconf.

ok jordan@


# 1.32 17-Feb-2006 marco

Add a generic delay function as per the ACPI spec.

from jordan@


# 1.31 17-Feb-2006 marco

Use tsleep instead of delay when !cold.


# 1.30 17-Feb-2006 marco

Argh, we need delays between reads/writes. The backend reads (i2c most likely)
are so slow that values come back as 0 sometimes. We have to do this
despite the spec saying we shouldnt have to. Guess having a faster
parser than others can hurt sometimes.

Idea from jordan.


# 1.29 17-Feb-2006 jordan

Fixed code for handling _INI
Rearranged debug statements
ok marco@


# 1.28 16-Feb-2006 jordan

Added stub for calling _INI
Fixed aml_dowhile for NULL return
ok marco@


# 1.27 16-Feb-2006 jordan

Fixed parser/evaluator to retrieve battery info+state
Cleanup of parser
Dell laptops return battery status
ok marco@


# 1.26 06-Feb-2006 brad

fix a typo so this will compile on amd64.


# 1.25 04-Feb-2006 marco

When ACPI_ENABLED wasn't set but the devices were it had an unknown variable.

Pointed out by ian@.


# 1.24 03-Feb-2006 jordan

Rewrote aml parser yet again...
Fixed bus_space_map for i386/amd64
Added _BST and _BIF to acpibat to get status/info
ok marco@


# 1.23 20-Jan-2006 jordan

More evaluator changes
Removed old evaluator code
Fixed _HID value
ok marco@


# 1.22 19-Jan-2006 jordan

Added support for read/write of PCI Config space
Help jason@
ok marco@


# 1.21 18-Jan-2006 jordan

Added new evaluation routines
Added helper methods for buffer/field evaluation
Fixed broken indentation on dsdt.c

ok marco@


# 1.20 17-Jan-2006 jordan

Major overhaul of the aml parser. Evaluater temporarily disabled.

ok marco@


# 1.19 06-Jan-2006 grange

Check for ACPI control availability only if ACPI_ENABLE defined.


# 1.18 06-Jan-2006 grange

Informative error messages on attach, prettier dmesg.


# 1.17 06-Jan-2006 grange

printf -> dnprintf conversion for debug messages.


# 1.16 05-Jan-2006 grange

Find SLP_TYPx values and store them for future use in
acpi_enter_sleep_state().


# 1.15 05-Jan-2006 grange

Random KNF and cleanup


# 1.14 05-Jan-2006 grange

Provide symbolic names for the acpi sleeping states and use it.


# 1.13 28-Dec-2005 marco

Moved DEVNAME to a acpidev.h

ok jordan@


# 1.12 18-Dec-2005 sturm

add back comment about wrong code

ok marco


# 1.11 16-Dec-2005 marco

Remove test for kbd_reset. Prompted deraadt@, ok jordan@


# 1.10 16-Dec-2005 jordan

Added hooks for powerdown/power button press


# 1.9 16-Dec-2005 jordan

Cleaned up common power management register i/o
Added enter sleep state ioctl
Hooked power button interrupt
ok marco@


# 1.8 16-Dec-2005 marco

Remove autoconf chatter


# 1.7 16-Dec-2005 jordan

Moved DSDT AML parsing into parent ACPI driver
ok marco@


# 1.6 14-Dec-2005 marco

Add AC device glue.


# 1.5 14-Dec-2005 marco

Adjust debug printing levels to something more sane.


# 1.4 13-Dec-2005 marco

Add glue to attach devices to apci.


# 1.3 07-Dec-2005 marco

KNF


Revision tags: OPENBSD_3_8_BASE
# 1.2 10-Jul-2005 grange

Spacing cleanup.


# 1.1 02-Jun-2005 tholo

Start on a basic ACPI framework -- does not do much more than read out the
ACPI tables into kernel memory and attach ACPI and HPET timers currently.

In order to test this code, enabling the devices in GENERIC as well as
the ACPI_ENABLE option is needed. This code does not do any thermal
control yet, so this should be done with care depending on the platform.

In the tree so more people can contribute to making this more fully
featured.

Ok niklas@ grange@ tedu@


# 1.390 16-Aug-2020 gnezdo

Remove sc_maxgpe from acpi_softc which was never read from

Remove a dead conditional from acpi_init_gpes while there.

ok jcs@


# 1.389 21-Jul-2020 deraadt

acpi can use IPL_BIO (a low interrupt) since it only enqueues operations for
later processing. The use of a high interrupt will predate suspend/resume
efforts, we had to redesign acpi to be non-reentrant obviously
discussed with kettenis, in snaps for more than a week


# 1.388 24-Jun-2020 cheloha

kernel: use gettime(9)/getuptime(9) in lieu of time_second(9)/time_uptime(9)

time_second(9) and time_uptime(9) are widely used in the kernel to
quickly get the system UTC or system uptime as a time_t. However,
time_t is 64-bit everywhere, so it is not generally safe to use them
on 32-bit platforms: you have a split-read problem if your hardware
cannot perform atomic 64-bit reads.

This patch replaces time_second(9) with gettime(9), a safer successor
interface, throughout the kernel. Similarly, time_uptime(9) is replaced
with getuptime(9).

There is a performance cost on 32-bit platforms in exchange for
eliminating the split-read problem: instead of two register reads you
now have a lockless read loop to pull the values from the timehands.
This is really not *too* bad in the grand scheme of things, but
compared to what we were doing before it is several times slower.

There is no performance cost on 64-bit (__LP64__) platforms.

With input from visa@, dlg@, and tedu@.

Several bugs squashed by visa@.

ok kettenis@


# 1.387 10-Jun-2020 jca

Give userland a consistent battery and A/C state after resume

Previously we refreshed the state of acpiac(4) and acpibat(4)/acpisbs(4)
by queueing fake ACPI events to be processed by the acpi thread.
acpibat(4) using ACPIDEV_POLL meant that its status could be refreshed
first, resulting in an APM_POWER_CHANGE event sent to userland before
the state of acpiac(4) was up to date. Because of this, apmd(8) could
see A/C unplugged when it was in fact plugged, possibly leading to
a wrong decision.

Instead of using fake ACPI events, refresh the status of acpiac(4) and
acpibat(4)/acpisbs(4) right away at DVACT_WAKEUP time. To reuse the
existing refresh code of those drivers, make their ACPI *_notify()
handlers responsible for sending APM_POWER_CHANGE events instead. Also
queue an APM_POWER_CHANGE event right after resume to retain existing
behavior.

This fixes the consistency of data shown to userland, and makes those
drivers less special.

Initially discussed last year (!) with mpi@ at Miod's place.
Feedback and ok jcs@, deraadt@ agrees with the use of DVACT_WAKEUP here.


# 1.386 29-May-2020 deraadt

dev/rndvar.h no longer has statistical interfaces (removed during various
conversion steps). it only contains kernel prototypes for 4 interfaces,
all of which legitimately belong in sys/systm.h, which are already included
by all enqueue_randomness() users.


# 1.385 14-May-2020 kettenis

Bring back the acpi(4) changes. This time around only use the ACPI _CRS
information on ACPI 5.0 and later.

ok krw@, patrick@


# 1.384 11-May-2020 jca

More accurate remaining power reporting with disparate batteries

Overall remaining power was computed as the average of the remaining
power of each battery, in percents. This doesn't work with batteries of
different capacity, so use the total capacity and remaining power
instead.

Reported by lidstah, successful tests by lidstah and benno@, ok kettenis@


# 1.383 08-May-2020 kettenis

Move parsing of _CRS into acpi(4) and pass the parsed address and interrupt
information in struct acpi_attach_args.

ok mpi@


Revision tags: OPENBSD_6_7_BASE
# 1.382 14-Apr-2020 kettenis

Implement acpi_getprop(), the ACPI equivalent of OF_getprop().

ok patrick@


# 1.381 12-Apr-2020 kettenis

Add support for the _CCA method. This method indicates whether DMA is
cache-coherent or not. To implement this, acpi(4) gets two bus_dma tags
and passes the appropriate one when attaching devices based on _CCA.
On i386/amd64, where for all practical purpose DMA is always cache-coherent,
the two tags are the same. But on arm64 they are distinct.

ok patrick@


# 1.380 07-Apr-2020 visa

Abstract the head of knote lists. This allows extending the lists,
for example, with locking assertions.

OK mpi@, anton@


# 1.379 03-Apr-2020 mpi

Fix incoherencies of errors returned by various *kqfilter routines.

EOPNOTSUPP is returned when there's no "filterops" for a given fd.
EINVAL when the requested filter isn't supported by the "filterops".
ENXIO means the underlying device is no longer valid/present.

ok visa@


# 1.378 20-Feb-2020 visa

Replace field f_isfd with field f_flags in struct filterops to allow
adding more filter properties without cluttering the struct.

OK mpi@, anton@


# 1.377 31-Dec-2019 visa

Use C99 designated initializers with struct filterops. In addition,
make the structs const so that the data are put in .rodata.

OK mpi@, deraadt@, anton@, bluhm@


# 1.376 31-Dec-2019 mpi

Convert infinite sleeps to {m,t}sleep_nsec(9).

ok kn@


# 1.375 31-Dec-2019 jsg

include sys/sensors.h for sensor use
don't depend on indirect inclusion from acpidev.h


Revision tags: OPENBSD_6_6_BASE
# 1.374 07-Sep-2019 kettenis

Revert acpipci(4) changes. Too many systems are broken. We'll try again
after 6.6 as been released. The acpireg.h change stays behind.


# 1.373 28-Aug-2019 kettenis

Use ACPI information to attach PCI busses like we do on arm64. There are a
few additional quirks though, and attaching the PCI busses is delayed to
replicate the existing code more closely. That may be changed in the
future. Also tweak how we handle MSI support and respect to ACPI flag
that says we shouldn't attempt to use MSIs.

Some fallout is expected.

ok patrick@


# 1.372 27-Aug-2019 deraadt

On amd64 the bootup and resume paths for SP/BP/SP were not performing
msr/pat, clock-startup, firmware upgrade, fpu initialization, and cpu
soft-state initialization in the same order. That kind of chaos is
crazy and fragile. Improve the acpi resume call sequence and unify order
of the operations.


# 1.371 02-Jul-2019 jcs

acpi_set_gpehandler: turn edge arg into flags, add support for
passing GPE_DIRECT which directly executes the supplied callback and
clears a level interrupt rather than going through acpi_taskq which
can be slow when other driver callbacks are in the queue.

This should not be used for GPE handlers that need to do anything
ACPI-related.

ok kettenis


# 1.370 10-Jun-2019 kettenis

Print proper ACPI version number.

ok mlarkin@


# 1.369 08-Jun-2019 kettenis

When establishing the mapping between ACPI device nodes and PCI devices,
skip PCI host bridges and devices that aren't there.

ok patrick@


# 1.368 07-Jun-2019 kettenis

Introduce acpi_getsta() and use it to eliminate some duplicated code.

ok patrick@


# 1.367 12-May-2019 kettenis

Make sure the extended PM register descriptions actually exist before
using them. Fixes machines with ancient ACPI 1.0 (such as recent
hypervisors).

ok deraadt@


# 1.366 12-May-2019 kettenis

Add support for mapping ACPI PM registers using the "extended" gasio
representation from the FADT. Mostly fixes the Lanner NCA-1510.

ok lteo@, mlarkin@, deraadt@


# 1.365 11-May-2019 lteo

Fix incorrect error message.

ok kettenis@


# 1.364 08-May-2019 tedu

free size. the allocation is the header plus the length.


Revision tags: OPENBSD_6_5_BASE
# 1.363 04-Apr-2019 kettenis

Avoid potential null-pointer dereference. Found by Kent R. Spillner.

ok deraadt@, jsg@


# 1.362 20-Jan-2019 jsg

fix SMALL_KERNEL build after changes in rev 1.361
reported by naddy@ ok deraadt@


# 1.361 19-Jan-2019 tedu

Add a pwraction sysctl that controls what the power button does on acpi.
By default, nothing changes -- shutdown is initiated. But allows turning
power button into a sleep button if desired.
(grudging) ok from a few parties


# 1.360 26-Oct-2018 kettenis

Add acpipci(4) on amd64. For now this only calls the PCI-specific _OSC
method to let the ACPI implementation know what features we support.


Revision tags: OPENBSD_6_4_BASE
# 1.359 30-Aug-2018 kettenis

Move softbutton check before the check that skips attaching certain acpi
devices. Fixes regression caused by matching on _CID in addition to
matching on _HID.

ok matthieu@, mlarkin@


# 1.358 25-Aug-2018 kettenis

Match ACPI devices based on _CID if no match for _HID is found.

ok mpi@


# 1.357 19-Aug-2018 kettenis

Add support for multiple PCI segments. Only really implemented for arm64
for now as amd64/i386 firmware still caters for legacy OSes that only
support a single PCI segment.

ok patrick@


# 1.356 03-Aug-2018 kettenis

Let ahci(4) match on _CLS instead of _HID when attaching at acpi(4). Avoids
having to add many more _HID entries to the match table.

ok deraadt@, mlarkin@


# 1.355 10-Jul-2018 kettenis

Make legacy interrupts work in acpipci(4).

ok patrick@


# 1.354 05-Jul-2018 kettenis

Add acpipci(4), a driver that supports generic ECAM-compatible PCI host
bridges based on information provided by ACPI.

ok mlarkin@


# 1.353 04-Jul-2018 kettenis

Properly pass around the PCI "chipset tag" in acpi(4) and refactor
acpimcfg(4) to call an MD initialization functions that sets up a tag for
PCI ECAM.

ok guenther@, mlarkin@, krw@


# 1.352 01-Jul-2018 kettenis

Move acpi(4) attach glue into acpi_machdep.c.

ok guenther@, deraadt@


# 1.351 01-Jul-2018 kettenis

Add support for _DSD Device Properties.

ok mlarkin@


# 1.350 30-Jun-2018 kettenis

Add intr_enable() function, intended for MI use to amd64 and i386 and use
this in the acpi(4) suspend/resume code paths.

ok deraadt@


# 1.349 29-Jun-2018 kettenis

Use <stdint.h> types.

ok mpi@, jung@, krw@, deraadt@


# 1.348 29-Jun-2018 mlarkin

ACPI: Allow (with warning) GPE handler reassignment, instead of returning
a failure code.

ok kettenis


# 1.347 26-Jun-2018 mlarkin

knf


# 1.346 26-Jun-2018 kettenis

Let acpi(4) pass down the bus dma tag.

ok deraadt@


# 1.345 25-Jun-2018 kettenis

Reorganize acpi(4) code a bit in preparation for upcoming arm64 support.

ok deraadt@


# 1.344 20-May-2018 kettenis

Wake up the acpi thread after scheduling a gpio event.

ok mpi@


# 1.343 17-May-2018 kettenis

Sprinkle some #ifndef SMALL_KERNEL such that RAMDISK kernels cuild again.

pointed out by stsp@


# 1.342 17-May-2018 kettenis

Implement GenericSerialBus OpRegion support.

ok mlarkin@


# 1.341 27-Mar-2018 kettenis

Add acpicmos(4), a driver that implements SystemCMOS OperationRegion
access support. This fixes machines where the AML doesn't check whether
support for this OperationRegion type has been registered by the OS.

ok mlarkin@


Revision tags: OPENBSD_6_3_BASE
# 1.340 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.339 10-Feb-2018 deraadt

Syncronize filesystems to disk when suspending. Each mountpoint's vnodes
are pushed to disk. Dangling vnodes (unlinked files still in use) and
vnodes undergoing change by long-running syscalls are identified -- and
such filesystems are marked dirty on-disk while we are suspended (in case
power is lost, a fsck will be required). Filesystems without dangling or
busy vnodes are marked clean, resulting in faster boots following
"battery died" circumstances.
Tested by numerous developers, thanks for the feedback.


# 1.338 08-Feb-2018 deraadt

After flushing disks, try again to release all memory. We don't need to
save dirty memory to the hibernate space.


# 1.337 08-Feb-2018 deraadt

Toss all releaseable memory, because fragmentation can get in the way
of allocating the hibernate playpen.


# 1.336 08-Feb-2018 deraadt

sr_quiesce() is a new approach for ensuring that softraid drains
output to the disks.

This is part of a larger suspend/resume filesystem-safety diff, which
has been worked on for a couple of months already. Tests by job, krw,
beck, benno, and others. Sometimes even by snapshot users...


# 1.335 29-Nov-2017 kettenis

Add support for GPIO-signaled events.

ok mlarkin@


# 1.334 16-Nov-2017 jcs

Add PCI attachment for dwiic(4) needed by Intel 100 Series machines

ok kettenis


# 1.333 14-Nov-2017 kettenis

Add a few more PNP IDs to the skip lists.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.332 17-Aug-2017 stsp

Make dwiic(4) attach its ACPI parent dependencies.
This change makes the Asus E200HA keyboard work.

Original analysis and patch by Cesare Gargano
ok kettenis@


# 1.331 09-Aug-2017 dcoppa

Add "PNP0303" (8042 PS/2 Controller) to acpi_isa_hids[]

ok kettenis@


# 1.330 22-Jul-2017 jcs

acpibat: look for _BIX first before falling back on _BIF

ACPI 4.0 deprecated _BIF for battery status so some newer machines
are exporting _BIX which extends _BIF's package with a few extra
fields. When using _BIX, export the new discharge cycle count as a
sensor.

feedback from kettenis and anton
ok deraadt


# 1.329 20-Jul-2017 kettenis

Start a list of isa(4) device names such that we can avoid printing them
as "not configured".

ok mikeb@, millert@, deraadt@


# 1.328 08-Apr-2017 deraadt

Sensors are run as callbacks inside tasks. During suspend a sensor
could be running inside a driver that will be force-detached, or due
to tsleep end up disrupting the softstate/hardstate contract. At
suspend time, quisce all these callbacks by waiting for completion.
This issue has never been observed for real, but may be implicated in
suspend/resume failures.
ok kettenis guenther mlarkin


# 1.327 08-Apr-2017 deraadt

Two prototypes for acpi_maptable; merge to one place


Revision tags: OPENBSD_6_1_BASE
# 1.326 27-Mar-2017 deraadt

Now that hibernate_alloc() only has clean success/failure, don't
need to call hibernate_free() to clean up a partial mess.
ok mlarkin kettenis


# 1.325 13-Mar-2017 deraadt

allow ramdisks to build; ok jcs


# 1.324 12-Mar-2017 jcs

try to attach acpisbs first and if successful, don't attach acpibat


# 1.323 02-Mar-2017 natano

Add a new sysctl machdep.lidaction. The sysctl works as follows:

machdep.lidaction=0 # do nothing
machdep.lidaction=1 # suspend
machdep.lidaction=2 # hibernate

lidsuspend is just an alias for lidaction, so if you change one, the
other one will have the same value. The plan is to remove
machdep.lidsuspend eventually when people have upgraded their
/ets/sysctl.conf.

discussed with deraadt, who came up with the new MIB name
no objections mlarkin
ok stsp halex jcs


# 1.322 28-Feb-2017 natano

Untangle abstract sleep modes from ACPI sleep states. This paves the way
for further work in the area.

idea and ok deraadt
ok mlarkin


# 1.321 22-Feb-2017 jcs

add acpisbs, an acpi smart battery subsystem driver reading data
over smbus

currently disabled because it conflicts with acpibat

ok deraadt, kettenis


# 1.320 14-Jan-2017 kettenis

Revert aml_rdpciaddr changes; breaks several machines that were working
before.


# 1.319 11-Jan-2017 jsg

Increase the size of the buffers used for _HID and _CID strings to be
able to fit "Hyper_V_Gen_Counter_V1".

ok mlarkin@ deraadt@


# 1.318 08-Jan-2017 kettenis

Make aml_rdpciaddr() work for for devices behind bridges. The code now
uses the mapping between AML nodes and PCI devices that we establish.
Because _INIT methods may end up calling aml_rdpciaddr(), make sure we
create that mapping early. Also handle devices that aren't actually present.
These devices are now included in the mapping and reads will return an
all-ones patterm whereas writes are a no-op..

ok mlarkin@


# 1.317 25-Oct-2016 pirofti

Add the resource index to aml_parse_resource() callback function.

This is needed by an upcoming acpiec commit that handles machines breaking
the current ACPI specifications.

Change suggested by and ok kettenis@, guenther@


# 1.316 18-Sep-2016 guenther

If the APIC table (MADT) isn't present then ci_acpi_proc_id won't be set
when acpicpu tries to attach, leading to a NULL dereference. The ACPI
processor object on the Dells where this was reported is bogus and useless
anyway so apply the same test when deciding whether to attach to processor
objects as acpicpu would when matching them to cpus.

Problem reported by Eivind Eide (xenofil (at) gmail.com) and
Johan Huldtgren (johan+openbsd-bugs (at) huldtgren.com).
Testing by Eivind Eide.
ok kettenis@ mlarkin@


# 1.315 03-Sep-2016 naddy

Write the system time back to the RTC every 30 minutes.
This fixes the problem that long-running machines which were not
shut down properly would reboot with a badly offset system time.

hints and ok kettenis@


# 1.314 31-Aug-2016 mlarkin

Don't put a device in the wake device list if _STA says it's not there.

Fixes at least one machine that woke up from sleep / poweroff immediately.

ok kettenis@, tested by kettenis and martijn@


# 1.313 28-Jul-2016 kettenis

Store the acpi processor ID/UID in struct cpu_info, and use it to attach
acpicpu(4) drivers to the right cpu(4).

ok mlarkin@, guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.312 10-Jun-2016 kettenis

The power management timer has been made optional in ACPI 5.0A, so don't
attempt to attach acpitimer(4) if the timer isn't present.

Pointed out by jcs@

ok jcs@


# 1.311 08-May-2016 kettenis

Make sure devices listed by a device's _DEP method are attached before
we attach the device itself. The _DEP methode, introduced in ACPI 5.0,
is a hint that the device in question depends on OpRegion support from
the devices listed by _DEP, so we have to order them the proper way.
To prevent us from attaching those devices again when we encounter them
later walking down the device tree, keep track of our attempts to
attach a device driver to them using aflag in the aml_node structure.

ok guenther@


# 1.310 25-Apr-2016 pirofti

We attach acpihpet(4) and acpiec(4) without using the HID, so do not
report it in the dmesg.

OK deraadt@, mlarkin@.


# 1.309 02-Apr-2016 kettenis

Reduce dmesg spam by knocking out some acpi hardware IDs that don't need a
driver.

ok mpi@


# 1.308 29-Mar-2016 kettenis

Check if a device is present (using _STA) in acpi_foundhid. This prevents
us from reporting devices as "not configured" that aren't actually present,
and allows us to remove duplicated code from several drivers.

ok deraadt@, ok jsg@


# 1.307 29-Mar-2016 kettenis

Implement a cleaner way to prevent acpivout(4) from attaching if we have
a platform-specific method of controlling the backlight.

ok millert@, mlarkin@, jung@


# 1.306 28-Mar-2016 kettenis

Adding each and every new acpi device driver to acpi_foundhid() is
gettinga bit of the burden. And it really isn't how our autoconf
framework is supposed to work for busses that can be enumerated. All
the drivers already check for a matching _HID in their attach
function. So we can just drop the checks in acpi_foundhid() and let
the drivers sort themselves out.

Print a "not configured" line for devices for which we don't attach adriver.
This may be a bit much as there are devices for which we will not have a
driver. This will be removed if it causes too much dmesg spam.

ok deraadt@, mlarkin@


Revision tags: OPENBSD_5_9_BASE
# 1.305 17-Jan-2016 jsg

Take acpi_foundhid() outside of #ifndef SMALL_KERNEL so the ramdisk
kernel can find and attach dwiic(4) and sdhc(4).


# 1.304 17-Jan-2016 jsg

remove an unused global variable


# 1.303 14-Jan-2016 kettenis

Attach dwiic(4) on the Intel Bay Trail i2c controllers as well.


# 1.302 12-Jan-2016 kettenis

Improve emulation of PM registers on Hardware-redcued ACPI by emulating
SPL_TYPx, SLP_EN and WAK_STS using the new SLEEP_STATUS_REG and
SLEEP_CONTROL_REG registers. Makes the transition into S5 (powerdown) work
on the ASUS X205TA.

ok deraadt@, mlarkin@


# 1.301 12-Jan-2016 jcs

Add dwiic, a driver for the Synopsys DesignWare i2c controller found
on the Samsung ATIV Book 9 laptop. This initial version only
supports ACPI config/attachment.

Add ihidev, a HID-over-i2c driver largely based on uhidev. dwiic
handles attaching ihidev devices found in ACPI.

Add ims, a HID-over-i2c mouse/trackpad driver to get basic cursor
and button functionality from HID-compliant i2c trackpads.

ok kettenis deraadt


# 1.300 11-Jan-2016 kettenis

Make sdhc(4) attach to hardware IDs "80860F14" and "PNP0FFF".


# 1.299 09-Jan-2016 kettenis

Initial support for Hardware-Reduced ACPI. This skips the legacy mode checks
by emulating the PM_CNT register such that SCI_EN is always enabled. It also
skip attaching acpitimer(4) since the relevant registers are no longer there.

ok deraadt@, mlarkin@


# 1.298 06-Jan-2016 kettenis

Follow the spec and check the SCI_EN bit to see if ACPI has already
been enabled and don't bother enabling it again if it is. This should fix
systems that don't implement legacy mode and therefore set ACPI_ENABLE and
ACPI_DISABLE to zero.

ok mlarkin@, yasuoka@


# 1.297 23-Nov-2015 reyk

Back out the CRITICAL change, turn it back from 10% to 25%. Some
people with old/broken batteries didn't like this change, and the
underlying heuristics are wrong.


# 1.296 22-Nov-2015 reyk

The "CRITICAL" APM battery level, that is exported/faked by the ACPI
stack, is currently set to 25%. With 25% battery left, a typical
modern laptop will have at least one hour runtime left. It is an
arbitrary value, and other APM power layers in the kernel set CRITICAL
to something like 5% or 10%, so set it to 10% for now.

OK mlarkin@


# 1.295 28-Sep-2015 deraadt

In low-level suspend routines, set cold=2. In tsleep(), use this to
spit out a ddb trace to console. This should allow us to find suspend
or resume routines which break the rules. It depends on the console
output function being non-sleeping.... but that's another codepath which
should try to be safe when cold is set.
ok kettenis


# 1.294 06-Sep-2015 deraadt

clarify malloc arguments (base + length...)


# 1.293 28-Aug-2015 deraadt

fairly simple sizes for free(); ok tedu


# 1.292 20-Aug-2015 kettenis

Respect the access size when reading or writing to pci config space.
This version also makes sure writes are properly aligned, preventing
the panics seen with the previous incarnation of this change.

Fixes battery status passthrough in vmware, brightness keys on my x220
and who knows what else.

ok mlarkin@, deraadt@


Revision tags: OPENBSD_5_8_BASE
# 1.291 04-Aug-2015 deraadt

If we are setting ACPI_PM1_BM_RLD do it only once, and remember to
repeat this action at resume time.
ok mlarkin guenther


# 1.290 29-Jul-2015 deraadt

Something subtle in previous revision is locking up suspend or booting
on some laptops. Mailing lists threads mention x220 and Asus machines.
Reverting for now.


# 1.289 21-Jul-2015 kettenis

iRespect the access size when reading or writing to pci config space.
Fixes battery status passthrough in vmware and who knows what else.

Tested by many.

ok mlarkin@, deraadt@


# 1.288 17-Jul-2015 mlarkin

fix comments


# 1.287 26-Mar-2015 jsg

Use the 0x10 extended capability to determine if the trackstick
buttons are wired to the trackpad and need to be re-routed to the
trackstick. Without this change the buttons on 2015 thinkpads
get picked up as extended buttons that show up as scroll up/down.

Remove the X1 Carbon 2015 (LEN0048) and X250 (LEN0046) from the
top button area/soft buttons quirks list. Also avoid using the
quirk list entirely if the capability bit is set.

Based on work from jcs@ with feedback from mpi@
ok mpi@ jcs@


# 1.286 20-Mar-2015 kettenis

The ACPI standard says that a PCI function number of FFFF can be used to refer
to all functions on a device. This makes no sense in the context of accessing
PCI config space. Nevertheless stsp@ has a machine with AML that tries to
do config space reads from a device where _ADR encodes function number FFFF.
This is almost certainly buggy AML, but since Linux (and presumably Windows)
boot just fine on this machine we need to work around this. Linux effectively
treats function FFFF as function 7, which isn't present on this machine.
Instead we simulate a read from a non-existent device by returning a bit
pattern of all ones. Writes to function FFFF will still panic.

ok mlarkin@


# 1.285 16-Mar-2015 deraadt

Reduce use of SMALL_KERNEL in acpi, bringing acpiec driver and more gpe
handling into RAMDISK. This is now possible because the install media
has ample room. The goal is to reduce special cases where we may be
skipping (unknown) important operations...
ok mlarkin kettenis


# 1.284 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.283 07-Feb-2015 deraadt

New framework that allows hibernate to pass in entropy from it's fresh
boot.
ok mlarkin


# 1.282 06-Feb-2015 mlarkin

Fix a hibernate crash on some machines due to unmapping a page that
may not have been mapped previously (in the failure to hibernate case).

Also ensure that the lowmem ptp is mapped in all cases (not just MP).

ok kettenis


# 1.281 17-Jan-2015 deraadt

resume_randomness() before spinning up other cpus...


# 1.280 15-Jan-2015 jsg

Define a new wscons mouse type for Synaptics clickpad devices that lack
physical buttons. This will be used if the acpi pnpid for the mouse
matches a list provided by Synaptics found in Linux.

Combined with the xenocara changes this will remove the need for an xorg
config file for the x240/t440/t540 generation of thinkpads.

ok matthieu@ shadchin@ kettenis@


# 1.279 11-Jan-2015 kettenis

Tolerate checksum errors in ACPI tables (but complain about them). Apparently
there are machines out there with broken BIOSen with such checksum errors in
thier RSDT tables. Windows runs fine on these machines, so it probably does
not check. Linux ignores checksum errors as well (and prints a warning).

ok deraadt@, krw@


# 1.278 18-Dec-2014 deraadt

suspend and resume the RNG (at what looks like the right spots)
ok reyk djm


# 1.277 09-Dec-2014 doug

Replace some malloc(n*size,...) calls with mallocarray().

ok tedu@ deraadt@


# 1.276 06-Dec-2014 mpi

If "machdep.lidsuspend" is not set, do not go back to sleep if the lid
is still closed when resuming. This allows docked laptop to be resumed
without opening them.

ok mlarkin@


# 1.275 23-Nov-2014 mlarkin

If a machine has no _LIDs, then none can be open. This diff fixes a problem
where machines without any _LIDs were going back to sleep after hibernate
due to the recent suspend-if-lid-closed-on-resume diff. Noticed on qemu,
which doesn't have any _LIDs.

discussed with deraadt


# 1.274 18-Nov-2014 krw

Nuke yet more obvious #include duplications.

ok miod@


# 1.273 08-Nov-2014 mlarkin

If resuming from sleep (zzz/ZZZ) and the lid is still closed, go back to
sleep. This prevents accidental lid flex or slight opening in a backpack
from waking the machine up and leaving it resumed (powered on).

ok deraadt@


# 1.272 26-Sep-2014 kettenis

Rework piglet and pig allocation. Currently the piglet gets allocated
deep down in the suspend path, where it is really hard to recover from
allocation failure. So allocate the piglet early on in the suspend path.
Also change the piglet and piglet allocation functions to use km_alloc(9)
instead of doing pmemrange magic. This removes a bunch of code which, in the
case of the piglet allocation, is broken since it results in a NULL pointer
dereference. Also switch the piglet allocation to not wait. If we can't
allocate 16MB of phys contig memory on a halfway modern machine we're almost
certainly under a lot of memory pressure and we're better off not trying to
hibernate anyway.

ok mlarkin@


# 1.271 25-Sep-2014 kettenis

Bring back rev 1.249. Now that mp_setperf() has been fixed, it should be
safe to restore perflevel when resuming. Should fix the issues people
see with the processor running at full speed heating up the machine while
the apm command reports that the CPUs are running at a low speed.

ok dcoppa@, deraadt@


# 1.270 23-Sep-2014 kettenis

Release the acpi lock around calls to wsdisplay_suspend() and
wsdisplay_resume(). The X server may adjust the display brightness just
before/after the VT switch when suspending/resuming. In fact, it may
already be blocked adjusting the display brightness for some reason
unrelated to the suspend request.

The VT switch request will eventually time out, but the delay is annoying
and the failed VT switch will lead to problems after resume.

Needed for a future update of the xf86-video-intel driver.

ok mlarkin@


# 1.269 19-Sep-2014 kettenis

Use config_suspend_all(9).


# 1.268 08-Sep-2014 kettenis

Clear the PME Status bit on PCI devices in response to Device Wake events.
Fixes the interrupt storm on the 2009 Mac mini with WOL enabled on its
nfe(4) interface.

ok mlarkin@


Revision tags: OPENBSD_5_6_BASE
# 1.267 20-Jul-2014 mlarkin

Support hibernating to softraid crypto volumes.

much help and ok from deraadt@


# 1.266 16-Jul-2014 mlarkin

Reenable hibernate RLE support and flush+zero all memory after unpack.


# 1.265 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.264 12-Jul-2014 kettenis

Add a function to drop all clean pages on the page daemon queues and call
it when we hibernate.

ok mlarkin@, miod@, deraadt@


# 1.263 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.262 11-Jul-2014 mlarkin

Flush the buffercache to 16MB on hibernate and restore its previous max
size (kern.bufcachepercent) on resume, for better hibernate performance.

ok beck@


# 1.261 10-Jul-2014 blambert

remove unneeded header

ok mlarkin@


# 1.260 01-Jul-2014 kettenis

Adding an additonal delay after each access to SystemMemory or SystemIO
during autoconf makes no sense.

ok deraadt@


# 1.259 23-Jun-2014 kettenis

SystemMemory is used to access memory mapped registers on some machines, so we
must use the bus_space(9) API to access it instead of memcpy(9). Also make
sure we properly align access to fields that need more than byte alignment.

ok guenther@


# 1.258 21-May-2014 mlarkin

Fix invalid / missing format specifiers in several dnprintf statements
(used only when ACPI_DEBUG is enabled)

From a diff on tech@ by Fabian Raetz, with a minor change by me, tested
on i386 and amd64 with ACPI_DEBUG enabled and disabled.


# 1.257 25-Apr-2014 mlarkin

Defer the free of the hibernate structures until later, when it is safe to
do so.

Freeing the hibernate structures too early leads to inadvertent calls into
splx when we aren't ready for them yet. Found when trapping splx calls in
the resume path.

discussed with deraadt@ at m2k14


# 1.256 13-Apr-2014 mlarkin

Fix a badly worded debug message that implied the entire machine was going
to sleep, instead of just the acpi thread going to sleep.


# 1.255 31-Mar-2014 mpi

Change the order of operations during a suspend/resume cycle and
call bufq_quiesce() after executing the DVACT_QUIESCE handlers.

This should be safe since no disk nor controller drivers have such
handler but it will allow us to detach sd(4) devices attached to a
USB bus.

Another benefit pointed out by kettenis@ is that drivers that need
to read a firmware from the disk should be able to do it at resume
time in a DVACT_WAKEUP handler.

ok kettenis@, deraadt@


# 1.254 13-Mar-2014 dlg

get rid of the assumption that the head of the alldevs list is the
"mainbus" device. this breaks when mpath is enabled because it
attaches before mainbus and therefore takes the head position.

have autoconf provide device_mainbus() which looks up mainbus_cd,
and use that instead.

discussed with deraadt who just wants mpath stuff to move forward
despite there being many ways to shine this particular turd.


Revision tags: OPENBSD_5_5_BASE
# 1.253 21-Feb-2014 deraadt

Do the lid control further on in the resume sequence, since this is
AML, and will do WAITOK operations. While cold, that is a bad idea.
We seem to be safe with respect to the spec, I do not see any event
debounce issues here.
ok kettenis, tested by many


# 1.252 20-Jan-2014 claudio

Instead of checking sc->sc_revision <= 1 check that the
fadt->hdr_revision <= 1. This is a better check since it checks
the version of the proper descriptor. This allows the alix APU
to reboot since those boards have currently a wrong sc->sc_revision.
Committing this now so that it can be tested easier.
With and OK jsg@ also fine by derradt@


# 1.251 24-Dec-2013 kettenis

If the FADT has its SMI_CMD set to zero, assume we're only ACPI-only hardware
and don't need to disable SMI ownership of the ACPI hardware registers.

ok mlarkin@


# 1.250 23-Dec-2013 kettenis

Revert previous commit. It makes resume hang on some MP systems running
GENERIC.MP.


# 1.249 16-Dec-2013 kettenis

Restore hw.setperf upon resume like we do for apm(4) on i386. Seems we need
to do this fairly late in the resume to avoid hangs.

ok deraadt@, mlarkin@


# 1.248 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


# 1.247 06-Nov-2013 mpi

Add support for Power Resources for Dx states and the necessary hook
for PCI devices. This hook should be called twice, before and after
changing the power state of a PCI device.

Before setting the device to the new state, the ACPI layer will notify
every power resources linked to the device for that state and make sure
they are turned "_ON". After changing the state of the device, it will
decrement the reference of every power resources linked to that device
for the old state and turn them "_OFF" if they are no longer referenced.

This fixes the no-USB after resume problem seen on various ThinkPad,
problem initialy diagnosed with Alexander Polakov.

ok kettenis@, deraadt@


Revision tags: OPENBSD_5_4_BASE
# 1.246 01-Jun-2013 mlarkin

Stop printing ACPI wakeup devices in dmesg after the 16th wakeup device.

Helps with VMware VMs that report hundreds of wakeup devices.

ok reyk@, deraadt@


# 1.245 31-May-2013 bluhm

Fix the build for a kernel without wd and pciide in its config.
OK deraadt@ tedu@


# 1.244 30-May-2013 deraadt

Enforce ca_activate tree-walks over the entire heirarchy for all events,
cleaning up some shutdown-hook related code on the way.
(A few drivers related to sparc are still skipped at kettenis' request)
ok kettenis mlarkin, tested by many others too


# 1.243 18-Apr-2013 deraadt

pin the acpi thread to the BSP, because AML and SMI cannot be trusted. I
was certain we were doing this already, but it turns out no...


Revision tags: OPENBSD_5_3_BASE
# 1.242 24-Dec-2012 guenther

In acpiioctl(), Return though the splx() path if suser() returns an error

ok deraadt@


# 1.241 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


# 1.240 04-Oct-2012 ehrhardt

Use information provided by ACPI to attach secondary PCI host bridges.

ok kettenis@


# 1.239 07-Sep-2012 kettenis

Add a new function called acpi_pci_min_powerstate() that returns the lowest
power state for a PCI device for the power state (currently S3 or S4) we're
transitioning into. This function is probably too simplistic; there is a lot
of (silly) complexity in the ACPI spec.

ok deraadt@


Revision tags: OPENBSD_5_2_BASE
# 1.238 13-Jul-2012 pirofti

Disable acpivideo(4) on ASUS laptops.

There are some models that panic whenever an event is received.
Some cope, some don't. In order to get this right acpiec(4) requires
major rework. Until then let machines boot.

Discussed with deraadt@.


# 1.237 11-Jul-2012 mlarkin

for now, restrict hibernate to root in the acpi ioctl kernel interface.
normal rules for controlling this interface through apmd still apply.

ok deraadt@


# 1.236 09-Jul-2012 deraadt

tiny little oops in untested hibernate block


# 1.235 09-Jul-2012 deraadt

clean up ioctl() case statements


# 1.234 09-Jul-2012 deraadt

acpi_disable_onegpe() is not used; ok pirofti


# 1.233 24-May-2012 kettenis

If _MIN > _MAX consider a WordBus resource to be invalid. Fixes a panic on
the IBM x3550-M4.

ok mikeb@


# 1.232 29-Mar-2012 mlarkin

Missing #ifdef in previous diff


# 1.231 29-Mar-2012 mlarkin

Prevent hibernation attempts on machines with unsupported disk controllers.
The same check is also performed later in the hibernate sequence, but by
then it's too late to cleanly unwind (presently).

ok deraadt


# 1.230 26-Mar-2012 deraadt

simplify conditionals in acpi_sleep_state()
ok mlarkin


# 1.229 26-Mar-2012 deraadt

hook in the hibernate request code; half of this diff is from mlarkin
ok mlarkin


Revision tags: OPENBSD_5_1_BASE
# 1.228 20-Sep-2011 deraadt

Very early in suspend while the clock is still ticking realtime,
save the clock back to the rtc. The zaurus already did this.
ok phessler miod kettenis


Revision tags: OPENBSD_5_0_BASE
# 1.227 02-Jul-2011 nicm

kqueue attach functions should return an errno or 0, not a plain 1. Fix
the obvious cases to return EINVAL and ENXIO.

ok tedu deraadt


# 1.226 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.225 16-Jun-2011 pirofti

Import acpitoshiba driver.

This is originally written by Hiroyuki Aizu, ported and integrated
in our tree by Javier Vazquez <javama4 AT gmail> with tweaks and
suggestions by me.

Import prodded by deraadt@ and the lack of testers on tech@.


# 1.224 27-Apr-2011 jcs

attach acpithinkpad to newer lenovo models like the x120e

ok deraadt@


# 1.223 22-Apr-2011 jordan

Fix uninitialzied variables and formatting strings (-Wxxx errors)


Revision tags: OPENBSD_4_9_BASE
# 1.222 02-Jan-2011 jordan

Re-add code for ACPI taskqueue, eliminates need for state vars in acpi softc
Serialize processing ACPI tasks for sleep/powerdown/GPE event
ok deraadt@


# 1.221 31-Oct-2010 guenther

Revert last commit: it breaks resume on Thinkpads
"then please back it out" deraadt@


# 1.220 26-Oct-2010 jordan

Add task queue for ACPI gpe and notify handlers
This fixes eject on Dell Latitude dock and an issue on the Dell Mini
battery update.
ok deraadt


# 1.219 07-Oct-2010 jordan

Create separate functions for enabling wake and runtime gpes.
Remove old suspend/resume gpewalk function
ok deraadt, mlarkin


# 1.218 05-Oct-2010 jordan

split acpi_enable_onegpe to separate enable/disable functions
ok deraadt


# 1.217 05-Oct-2010 jordan

Added acpi_disable_allgpes function to clear GPE status
ok deraadt


# 1.216 05-Oct-2010 deraadt

clean up acpi_softc use more


# 1.215 05-Oct-2010 deraadt

delete a bit of #if 0 code


# 1.214 05-Oct-2010 deraadt

hide use of our global acpi_softc pointer a bit better


# 1.213 29-Sep-2010 deraadt

grab the acpi thread lock very early in acpi thread startup; not that it
is likely that anyone will race us... but why be careless.
ok kettenis


# 1.212 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.211 29-Aug-2010 jasper

- add a check for FWRITE here too.

ok miod@


# 1.210 27-Aug-2010 kettenis

Make PCI Power Management optional, and only enable it when acpi(4) attaches.

ok deraadt@


Revision tags: OPENBSD_4_8_BASE
# 1.209 08-Aug-2010 kettenis

Close race between the acpi thread and wscons ioctls running in process
context. We might rip this out post-release and replace it with a solution
that serializes things in a more obvious way.

ok deraadt@, miod@ and "those crazy texans who ok anything"


# 1.208 08-Aug-2010 deraadt

Report the acpi sleep states when on a RAMDISK; ok krw


# 1.207 07-Aug-2010 kettenis

Enforce acpi register access rescrictions.

ok mlarkin@, deraadt@, tested by marco@


# 1.206 07-Aug-2010 deraadt

upon resume, notify all the acpiac and acpibat drivers to update
their status. some laptops do not do this automatically.
ok kettenis canacar


# 1.205 06-Aug-2010 marco

Always call _PSW on _LID devices that support it to enable lid open to
resume the machine. Conversely disable it when the machine wakes up.

Tested by several ok deraadt


# 1.204 06-Aug-2010 deraadt

As part of a revamp of the PM1/GPE code, I write this diff to look for
unmanaged EN & STS bits in the PM1 register at interrupt time and report
them. As a side effect this splits the STS acknowledgement into two writes
(for power, and sleep) instead of one. The printf that is added (to spot
unmanaged STS bits) has not yet been triggered as far as we know.

Before the "write to PM1 registers at the right offset" diff went in, this
was not neccessary. But newer thinkpads do not have a working soft power
button without this diff.

We have no idea why.
ok mlarkin kettenis


# 1.203 05-Aug-2010 deraadt

PM1 power button events were generating 'fake events' to acpibtn PWRB,
but if there is no such device these were being tossed. Instead, arrange
for all power button handlers to flag a softc variable which is processed
by the thread; so that one place will know to request a powerdown (psignal
to init)
ok marco mlarkin


# 1.202 05-Aug-2010 deraadt

rename a variable which is annoyingly poorly named


# 1.201 05-Aug-2010 mlarkin

Write to the correct offset for various ACPI pmreg registers. Writing to
the correct register does properly clear the wake status bits and fixed
event bits, which allows some machines to have more than 1 suspend cycle.

bug spotted by deraadt@

tested by deraadt@ and myself on a half-dozen machines and others on
hackers@

ok deraadt@, marco@


# 1.200 05-Aug-2010 deraadt

Fix the power button. The pm1 register is 16 bits in size.
My mistake, a while back.
tested by krw


# 1.199 04-Aug-2010 kettenis

Print supported sleep states.

ok marco@, phessler@, deraadt@


# 1.198 03-Aug-2010 mlarkin

Use the proper flag for re-enabling certain hardware events (power btn,
sleep btn). The (incorrect) flag prevoiously used coincidentally had the
same value, so this shouldn't cause any different behavior than before.

tested on a variety of machines (i386, amd64, sp, mp) - no behavioral
change seen

ok deraadt@


# 1.197 28-Jul-2010 marco

Make all gasio failures look and behave the same.


# 1.196 28-Jul-2010 mlarkin

Delay a little longer on suspend before giving up. Fixes machines that
sometimes don't suspend because the sleep transition takes too long
(symptoms like failing to sleep but the backlight stays on).

ok deraadt@


# 1.195 27-Jul-2010 deraadt

remove XXX; EC can now be attached early when needed; ok jordan


# 1.194 27-Jul-2010 deraadt

more brackets feels good


# 1.193 27-Jul-2010 deraadt

XXX annotate a block in acpi_enter_sleep_state() that is pretty worrying


# 1.192 27-Jul-2010 deraadt

horrific indentation that nearly made me puke


# 1.191 27-Jul-2010 mlarkin

Set the sleeping indicator light on machines that support it, in case SMI
doesn't do it for us. Shows the 'moon' LED on older Thinkpads when
sleeping.

Tested on T43p, W500, T510 by me and on X61s by deraadt@.

ok deraadt@


# 1.190 27-Jul-2010 jordan

Early initialization of acpiec if ECDT table exists
Fixes hang when booting thinkpads while docked
ok deraadt


# 1.189 26-Jul-2010 deraadt

remove merge error which was commited


# 1.188 26-Jul-2010 pirofti

Add support for Sony ACPI hotkeys via a new driver: acpisony(4).

Currently it only works for the suspend button and tries to do right for
the brightness events, but I haven't found a Sony laptop that like to do
right so far.

In the future I want to make the brightness keys work on all Sony's and
also add support for the zoom hotkeys and whatever other funky keys I can
find on those things.

Okay deraadt@.


# 1.187 22-Jul-2010 deraadt

pretty large cleanup of SMALL_KERNEL


# 1.186 21-Jul-2010 deraadt

in the acpi GPE handler, late ack level interrupts not edge interrupts.
Duh.


# 1.185 21-Jul-2010 deraadt

Merge the edge and level gpe handlers into one (now possible since the
gpe structure tells us whether the gpe is edge or level)


# 1.184 21-Jul-2010 deraadt

delete #if 0 code we are not going to use; ok marco


# 1.183 20-Jul-2010 deraadt

make the mask code for acpi_enable_onegpe() match other similar code


# 1.182 20-Jul-2010 deraadt

Handle the acpi interrupt controller with proper edge/level handling,
wrapped in spltty to avoid racing against the interrupt controller.
Repair the gpe bit masking code while there, and do operations in the
right order
ok kettenis mlarkin, help from jordan at figuring out the order of
operations.


# 1.181 20-Jul-2010 matthew

When we're preparing to go to sleep, only call bufq_restart() if other
preparations failed.

ok kettenis@, thib@, deraadt@


# 1.180 19-Jul-2010 deraadt

permit compilation with ACPI_DEBUG


# 1.179 19-Jul-2010 deraadt

there is no need for a seperate dsdt_softc pointer; dsdt does not have a
softc but is just a part of the single acpi driver.
ok jordan


# 1.178 19-Jul-2010 deraadt

acpi_delay() is no longer needed; ok jordan


# 1.177 18-Jul-2010 mlarkin

Clear event status on resume, to avoid going back to sleep/reboot/shutdown
immediately after resume. Tested by a bunch of people on hackers@ on
various hardware. Fixes at least Toshiba NB200 and Gateway LT3103u.

ok marco


# 1.176 13-Jul-2010 deraadt

rename some thread related interfaces so they are logical


# 1.175 10-Jul-2010 jordan

Verify _ADR bdf is correct (some systems return 0xffff)
Works on Dell x9xx and Studio 1555


# 1.174 09-Jul-2010 jordan

Clear the noise


# 1.173 08-Jul-2010 jordan

Add mapping for ACPI device to PCI bdf (match autoconf tree)
Simplify resource parsing function to use buffer argument
Convert namespace linked lists to use queue macros
ok marco@, deraadt@


# 1.172 06-Jul-2010 deraadt

Use spltty() to lock downcalls from apm against the information being
modified by the acpi thread.
ok kettenis


# 1.171 06-Jul-2010 deraadt

Don't use a workq to do the suspend, because it races aginst the acpi
thread. Instead, just tell the acpi thread to do the suspend for us.
This makes apmd & zzz work correctly. While here, have acpithinkpad
attempt to post the event to apm before prompting the suspend itself.
ok kettenis marco mlarkin


# 1.170 05-Jul-2010 mlarkin

Reset register SCI_EN on resume. The ACPI spec clearly states we don't have
to do this, but many machines don't, or forget to, which leads to all sorts
of problems on resume (broken battery stats, overheating, failure to resume
more than once, etc).

tested by many, ok marco@, deraadt@


# 1.169 01-Jul-2010 thib

do bufq quiesce again, since vnd/the bufq_done call in
biodone has been fixed.


# 1.168 01-Jul-2010 jordan

Backout recent AML changes, breaks on R210 and others


# 1.167 01-Jul-2010 jordan

Simplified aml_parse_resource call, cleanup code
ok marco


# 1.166 01-Jul-2010 jordan

Fix subordinate bus # for multi-root PCI buses
ok marco, kettenis


# 1.165 30-Jun-2010 thib

Disable/partially backout the bufq quiesce changes as this
is causing havoc with vnds and release must be buildable.


# 1.164 29-Jun-2010 deraadt

Fix ramdisks (new code was placed inside a SMALL_KERNEL chunk)


# 1.163 29-Jun-2010 jordan

Add support for mapping ACPI to PCI devices
ok kettenis, deraadt


# 1.162 29-Jun-2010 kettenis

Call bufq_quiesce() on suspend to stop all I/O and bufq_restart on resume.
Makes suspend/resume work much better while doing disk I/O.

ok mlarkin@


# 1.161 29-Jun-2010 tedu

60 is a more reasonable guess for number of minutes in an hour than 100.
spotted by TeXitoi. ok deraadt


# 1.160 27-Jun-2010 jordan

Add _acpi_matchhids routine, precursor to pci device match


# 1.159 27-Jun-2010 mlarkin

Clear ACPI fixed event status on resume (power buttons/etc) so that some
machines don't immediately resume after suspending to S3.

Tested by kettenis@ and I on 5 or 6 machines, deraadt@ "yeah, looks good"


# 1.158 27-Jun-2010 jordan

Cleaned up acpi_maptable, adds to table list directly


# 1.157 07-Apr-2010 deraadt

If we have done a wsdisplay_suspend(), and ACPI decides it does not want to
suspend, we must roll back by calling wsdisplay_resume() or the virtual
console switching will be locked forever
ok oga


# 1.156 07-Apr-2010 kettenis

Make suspend/resume work on MP machines (running an MP kernel). Joint work
from mlarkin@ and me, with some amd64 fixes thrown in by deraadt@

ok marco@, deraadt@, pirofti@, mlarkin@


# 1.155 31-Mar-2010 kettenis

Bring /dev/apm support in line with apm(4). Make the suspend button send
a suspend request event to apmd(8) instead of suspending immediately. Also
keep track of whether /dev/apm and /dev/apmctl are currently open, such that
we can still suspend immediately if apmd(8) isn't running.

ok deraadt@, marco@, pirofti@, jsing@, oga@


# 1.154 30-Mar-2010 oga

Prevent the apmd/x races for good.

When we hit suspend time, go through all wsdisplays on the system. if
they are in mode MAPPED, but not MODE_DUMBFB then if possible do a full
vt switch to a !mapped vt, and prevent switching back until resume time.
This has to be called from MD code because this involves userland
running so that X can run the vt switch signal handler. This way, any
case where we are using the "poke registers from userland" model, we
will not be on the hardware when we go down, so the kernel can actually
handle thing properly.

Tested on several acpi laptops (by kettenis@ and ian@), x40 (me and
beck@ at LEAST) and zaurus (me). Maybe others, but if so I forgot who at
this time..

Idea from deraadt somewhere over the Faroe Islands (I thought of a
similar thing myself a while ago). Much prompting from him. Ok and
comments miod@


# 1.153 25-Mar-2010 oga

Fix & vs. && in the kqfilter code.

Based on a comparison with the apm code.

ok deraadt@, kettenis@


Revision tags: OPENBSD_4_7_BASE
# 1.152 26-Nov-2009 mlarkin

Visual (and on some machines, audio) feedback while the machine is
resuming via calls to aml _SST method. This also makes the "moon" LED on
thinkpads to blink during resume and cease when resume is completed.

ok pirofti@


# 1.151 26-Nov-2009 deraadt

Toggle into "cold" when we we disable interrupts, because tsleeping
in the AML interpreter and context switching into userland processes
tends to sometimes pop the ACPI trampoline page out of our address
space. Makes lots more machines work.
ok kettenis mlarkin


# 1.150 24-Nov-2009 jsg

in the resume path evaluate _WAK before dealing with device drivers
like the spec says, not after.

ok mlarkin@


# 1.149 24-Nov-2009 deraadt

abort suspends on SMP systems until that gets written; ok mlarkin


# 1.148 23-Nov-2009 mlarkin

pci should return the result of it's children's suspend/resume calls back
to its parent.

handle suspend failure case and unwind if devices have an activate function
that returns failure.

ok deraadt@, kettenis@


# 1.147 23-Nov-2009 mlarkin

acpi* devices need to call their children on suspend/resume.
ok deraadt@


# 1.146 23-Nov-2009 pirofti

Remove ACPI_SLEEP_ENABLED checks.

This enables by default the suspend/resume paths in the kernel.

Okay deraadt@.


# 1.145 23-Nov-2009 deraadt

knf and spacing that are super annoying


# 1.144 23-Nov-2009 mlarkin

Hook up Theo's new children suspend/resume function, as well as more ACPI
suspend/resume glue.

ok deraadt@


# 1.143 26-Oct-2009 deraadt

Do not do strncmp comparisons on dv_xname because longer device names which
look similar could arrive in the future. Instead, compare directly against
dv_cfdata->cf_driver->cd_name
Issue originally spotted by miod


# 1.142 26-Oct-2009 jordan

Verify checksum+address when loading ACPI tables.
Some systems had invalid entries in RSDT/XSDT.
ok marco@


# 1.141 23-Jul-2009 cnst

New aibs(4) driver for ASUSTeK AI Booster (ACPI ATK0110) hardware monitoring,
with sensor state support through limits provided by the ACPI.

Tested on several ASUS motherboards kindly networked by Sam Fourman Jr.

ok deraadt marco jordan


Revision tags: OPENBSD_4_6_BASE
# 1.140 03-Jun-2009 pirofti

Import acpi power resource driver. This is needed for suspend/resume logic.

Okay jordan@ and marco@.


# 1.139 03-Jun-2009 pirofti

Add acpivideo support. This adds brightness support for all laptops
except thinkpads, they will use the acpithinkpad driver. The driver is
also hooked into wsconsole. So brightness can be adjusted via:

$ wsconsctl display.brightness=<percentage>

This is very helpfull on some laptops that have a nasty bios and get two
steps instead of one when pressing the brightness button.

Tested on various dell, fujitsu, acer, samsung and other laptops.

Okay marco@, miod@. Suggestions from kettenis@.
Lots of reviews and help from miod@, thanks!


# 1.138 03-Jun-2009 jordan

Added ejectable bay for IDE cdroms
ok marco@


# 1.137 30-Apr-2009 marco

Oga changed apm ioctls and forgot to grep. Temporary work around by hitting
the sleep button when we get any request. This interface will chnage at
some point.
ok oga


# 1.136 19-Apr-2009 krw

Call acpi_sleep_walk() when powering down just like when entering
any other sleep state. This tells acpi wake devices to not wake up
the box until manual powering up resets their status. Makes my Dell
GX520 stay powered off in response to 'halt -p'.

In snaps for a while.

ok marco@ (several times) deraadt@


# 1.135 17-Apr-2009 pirofti

Add device ranking support for future sleep/wake developments.

okay marco@


# 1.134 11-Mar-2009 jordan

Cleanup.. use aml_evalinteger instead of aml_evalname
ok marco@


# 1.133 10-Mar-2009 jordan

Fix cut/paste error.. call _BFS not _PTS
ok marco@


Revision tags: OPENBSD_4_5_BASE
# 1.132 21-Feb-2009 marco

overzealous cut/paste; found by Vladimir Kirillov <proger@uaoug.org.ua>


# 1.131 20-Feb-2009 marco

Oops didn't mean for Debugger() to remain there.


# 1.130 19-Feb-2009 marco

suspend/resume bits so that we can develop this in tree. This is disabled.
code from mlarkin and me
help from art,toby,jordan and several others
ok jordan, go for it deraadt


# 1.129 10-Feb-2009 jordan

Fix null pointer for shutdown if acpi not enabled
ok marco@


# 1.128 04-Feb-2009 kettenis

Some systems that claim to be ACPI 2.0 apparently don't provide an XSDT
address. Make the code fall back on the RSDT on those systems.

From Joerg Niendorf <p3y80@internode.on.net>. Fixes PR 5986.


# 1.127 17-Jan-2009 krw

A couple of stray malloc/memset -> malloc(M_ZERO), a bit of knf and
lose a couple of unnecessary casts. No functional change.

Feedback from kettenis@

ok kettenis@ marco@


# 1.126 06-Nov-2008 marco

Collapse all sanity checks of hids into one function.

Originaly from jordan


Revision tags: OPENBSD_4_4_BASE
# 1.125 02-Jul-2008 fgsch

add acpivideo. for now almost an empty skel so others can work on
suspend/resume. eventually it will also manage output switching and
brightness where supported. prodded by marco@


# 1.124 11-Jun-2008 marco

Fix $OpenBSD$ for once and for all


# 1.123 08-Jun-2008 deraadt

aaa_dev was being set from an uninitialized variable; ok toby


# 1.122 07-Jun-2008 marco

Kill ACPIVERBOSE. It was never used and is worthless to boot.

comments jmc
ok toby


# 1.121 06-Jun-2008 marco

Shrink acpi some more for floppies and while here re-enable acpi on the
floppies. Fixes crazy sony vaio kettenis has.

tested by deraadt
ok deraadt jordan kettenis


# 1.120 01-Jun-2008 marco

Fix \n nit in SMALL_KERNEL.


# 1.119 01-Jun-2008 marco

Re-enable new parser. Lots of testing by various folks. Fits all media
now too.

ok kettenis deraadt


# 1.118 16-May-2008 dlg

back out jordans new parser.

it didnt have enough oks from the right people, it did break machines
that were previously working, and it bloated the RAMDISKS and install
media beyond the constraints of the media.

requested by deraadt@


# 1.117 14-May-2008 jordan

Adding new ACPI Parser code
Old guts of code still exists, needs to be torn out next
ok marco@
ok brad@


# 1.116 27-Apr-2008 jcs

add acpithinkpad(4), an acpi support driver for ibm/lenovo
thinkpads.

provides functionality that the bios did on older, non-acpi
thinkpads like respond to screen brightness keys and toggle the
bluetooth device on and off.

ok deraadt@ and marco@


# 1.115 24-Apr-2008 jsing

Introduce acpiasus(4), a driver for the ACPI based hotkeys found in many
ASUS laptops (including the ASUS EeePC) - largely based on NetBSD's
asus(4) driver. On the ASUS EeePC this allows us to enable/disable
wireless, change screen brightness and use the volume keys.

ok jsg@, weingart@


# 1.114 12-Apr-2008 kettenis

Get rid of acpi_s5 global variables; simply send SIGUSR2 instead of SUGUSR1
to tell init(8) to power down the machine.

ok krw@


Revision tags: OPENBSD_4_3_BASE
# 1.113 21-Jan-2008 jsg

Check against RESET_REG_SUP in FADT before doing the acpi reset dance
after all, not doing so appears to break more machines than it fixes.

ok marco@


# 1.112 05-Jan-2008 kettenis

Never attach more acpicpu(4) devices than cpu(4) devices. Always attach
the first acpicpu(4) device on non-MULTIPROCESSOR kernels, regardless of its
ACPI processor ID.

ok mikeb@


# 1.111 05-Dec-2007 deraadt

Hang acpi(4) below bios(4) instead of mainbus(4). This lets us move acpi(4)
before pcibios(4), and then.... if acpi(4) attaches, skip pcibios(4) since
it messes the machines up. Fixes claudio's HP dl320 G5, and almost assuredly
others.
ok kettenis


# 1.110 05-Dec-2007 deraadt

repair acpiopen/acpiclose and friends to handle the /dev/apmctl interface,
so that apmd(8) can actually work correctly against acpi(4), and thus,
even do the fancy -C/-A stuff; also tested by jmc


# 1.109 02-Dec-2007 fgsch

fix the trailing space for real now.


# 1.108 02-Dec-2007 fgsch

avoid trailing spaces on output; marco@ ok.


# 1.107 27-Nov-2007 kettenis

Fix off-by-one that made us attach acpicpu0 and acpicpu1 on GENERIC kernels.

ok marco@


# 1.106 25-Nov-2007 jsg

When available and ACPI is enabled use the ACPI reset method
to reboot. This is required for some quirky sis machines
and likely others to reboot.

Tested by many. 'commit early commit often' deraadt@


# 1.105 19-Nov-2007 deraadt

sigh, amd64 still uses that stupid acpi_enabled variable...


# 1.104 19-Nov-2007 deraadt

softc's come initialized to 0, you know...


# 1.103 19-Nov-2007 deraadt

store revision; from jsg


# 1.102 19-Nov-2007 kettenis

Initialize sc_facs earlier.

ok deraadt@, beck@, canacar@


# 1.101 16-Nov-2007 deraadt

fix the bus_space #define nightmare, so that amd64 and i386 are much more
uniform. as a result shared code like acpi needs less #ifdef's
ok marco kettenis


# 1.100 15-Nov-2007 deraadt

remove the #ifdef ACPI_ENABLE. the EC codepath is either safe enough (and
will be improved), or it is unsafe because AML assumes it must be there
ok almost everyone


# 1.99 15-Nov-2007 mikeb

Make acpicpu(4) attach to the existing CPUs only.

Problem reported by form@. Fix idea by kettenis@.
Lots of help from deraadt@. Tested by deraadt@, form@ and me.

ok deraadt marco


# 1.98 14-Nov-2007 deraadt

spacing


# 1.97 12-Nov-2007 deraadt

correct : vs , in attach-time messages


# 1.96 06-Nov-2007 deraadt

all ACPI_ENABLE kernels need acpi_interrupt()


# 1.95 06-Nov-2007 deraadt

move acpi_read_pmreg() and acpi_write_pmreg() out of SMALL_KERNEL so
that acpi ramdisks can compile (obvious solution)


# 1.94 05-Nov-2007 canacar

Establish the acpi interrupt earlier. Fixes problems with some
GENERIC.MP kernels not getting acpi interrupts. This is a
workaround until the real issue with late establishment is fixed.
ok weingart@, beck@, gwk@


# 1.93 03-Nov-2007 jordan

Added support for displaying wakeup devices
ok beck@,weingart@,gwk@


# 1.92 11-Oct-2007 kettenis

When walking the tree yo find _PRT methods, check whether the device is
actually enabled and functioning. Similar to what we do for _INI.

ok weingart@, beck@


# 1.91 08-Oct-2007 krw

More simple memset(,0,) -> M_ZERO changes. In this batch move to
size(*p) as the first malloc() parameter where p is declared locally
and thus easy to check. Add M_ZERO to gpe_table allocation in acpi.c
even though there is no obvious bzero or memset nearby.


# 1.90 13-Sep-2007 mk

Remove unnecessary memset() (see aml_evalnode(9)) and a bit of KNF.

ok toby


# 1.89 13-Sep-2007 weingart

Implement ACPI 6.5.1 spec tree walk for _STA and _INI.
Fixes mk's laptop. No regressions so far (thank you to the
testers).

ok gwk@, mk@, marco@


Revision tags: OPENBSD_4_2_BASE
# 1.88 17-Apr-2007 mk

Now that acpidock no longer hangs machines during boot (at least
according to the reports I've been getting) it should be safe to enable
it again, so remove the #if 0 stuff around it.

ok marco


# 1.87 11-Apr-2007 jordan

Added changes for C-State
ok marco@


# 1.86 26-Mar-2007 marco

Calculate minutes left more accurately.

From giovanni <qgiovanni@gmail.com>


Revision tags: OPENBSD_4_1_BASE
# 1.85 03-Mar-2007 mk

Stop looking for acpidock until we fix that stuff that breaks all pre-60
series ThinkPads with acpidock(4) enabled.


# 1.84 22-Feb-2007 marco

We were getting no interrupts in .MP. There are 2 fixes; one in
acpi_machdep.c and move the interrupt establishment down after ioapic
has been initialized.

ok jordan


# 1.83 21-Feb-2007 kettenis

Make an acpi-enabled GENERIC.MP work on systems that only support PIC mode.

ok marco@


# 1.82 19-Feb-2007 jordan

Added acpi_map_address function for mapping GAS/IO registers
ok marco@


# 1.81 18-Feb-2007 jordan

Added dynamic perfect hash for opcodes
ok marco@


# 1.80 18-Feb-2007 marco

Shave off another K pre compression.
Shuffle SMALL_KERNEL functions around so that we only have a single #ifndef


# 1.79 17-Feb-2007 marco

Add SMALL_KERNEL throught acpi to make it fit on boot media.
ok deraadt


# 1.78 13-Feb-2007 marco

Add smarter _INI code. This helps with some dissapearing devices.

ok kettenis jordan


# 1.77 31-Jan-2007 gwk

Introduce acpi_hasprocfvs thats "ACPI Has Processor Frequency and Voltage
Scalling" a global flag set by acicpu attach to indicate that ACPI is
handling fvs through the _PSS objects. This will fix disappearing
EST/powernow when booting kernels with acpi enabled, as acpicpu isnt
compiled into GENERIC yet. Also in cases where acpicpu is built in and
legacy EST/powernow work but acpi is missing the _PSS object, the legacy
routines will be used.

tested by dim@, and Nick Nauwelaerts <nick AT nauwelaerts.net>
ok marco@, dim@


# 1.76 22-Jan-2007 mk

Actually look for docking stations by looking for _DCK nodes.

ok marco


# 1.75 02-Jan-2007 marco

On some systems EC must be attached as the first device. Move EC from
generic _HID devices to its own routine to force first attach. Bug found
and debugged by joris@

Code mostly written by joris@


# 1.74 26-Dec-2006 marco

Add polling to devices that require it such as acpibat and acpitz.
Use the same tsleep/wakeup mechanism as the interrupt code to keep it non-
concurrent.


# 1.73 21-Dec-2006 deraadt

even more knf love


# 1.72 21-Dec-2006 marco

Remove some leftover acpiec gpe/interrupt code.


# 1.71 20-Dec-2006 deraadt

oops, name already used


# 1.70 20-Dec-2006 deraadt

rename __acpi_enable_gpe(); ok marco


# 1.69 20-Dec-2006 deraadt

acpi_unmap_pmregs() is not used, ok marco


# 1.68 19-Dec-2006 deraadt

simplify some low-level methods substantially (shrinking code); ok marco jordan


# 1.67 06-Dec-2006 mk

malloc(..., M_NOWAIT) with no NULL check is bad. If allocation fails,
complain, clean up, and bail out of attach.

input and ok gwk


# 1.66 28-Nov-2006 jordan

Fixed clearing arg for acpi_init_pic
ok kettenis@


# 1.65 27-Nov-2006 kettenis

Don't spam dmesg.


# 1.64 27-Nov-2006 jordan

Fixes for acpiprt routing
Post-parse fixup for namerefs
ok kettenis@


# 1.63 25-Nov-2006 marco

Don't handle acpiev events inside the gpe handler since this causes deadlocks.

ok jordan@


# 1.62 15-Nov-2006 kettenis

Hook up acpi apic interrupt routing support to generic acpi code.

ok jordan@


# 1.61 02-Nov-2006 marco

Kill acpiec attach function and add its functionality to the generic _HID
device attach

prodded and ok jordan


# 1.60 02-Nov-2006 marco

Fix comments to reflect reality.


# 1.59 19-Oct-2006 marco

Replace ACPI ioctls with APM ioctls. This makes ACPI talk the same protocol
as APM therefore userspace can now retrieve power information in the same
fashion as all other apm arches (zaurus, macppc, i386 etc).

To use force the use of acpi device like so: apmd -f /dev/acpi

ok jordan@


# 1.58 19-Oct-2006 jordan

Fixed GPE debug levels
Fixed ref counter on temporary bufferfields
DSDT parsing fully working on IBM Thinkpad
ok marco@


# 1.57 15-Oct-2006 jordan

Removed old GPE handler code remnants
ok marco@


# 1.56 12-Oct-2006 jordan

Massive ACPI changes
Fixed ACPI leaks
new ACPI API
ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.55 30-Jun-2006 gwk

KNF, No functional change,
"go at it" marco@


# 1.54 30-Jun-2006 jordan

Support for calling Embedded Controller via new GPE interface
ok marco@


# 1.53 30-Jun-2006 jordan

Added new code for GPE handling
Also works with ACPIEC; need to fixup acpiec.c to remove original interrupt handler
ok marco@


# 1.52 30-Jun-2006 jordan

Added offset argument for acpi read/write pmreg routines (new GPE code)
ok marco@


# 1.51 31-May-2006 todd

do not call acpi_powerdown() unless acpi has successfully initialized
ok gwk@ canacar@


# 1.50 29-May-2006 canacar

Add embedded controller support.
ok marco@ deraadt@ testing & ok gwk@


# 1.49 19-May-2006 canacar

Minimal thermal zone driver. Monitors thermal zone temperature,
shuts down the system if the 'critical temperature' is reached.
ok marco@


# 1.48 11-Apr-2006 marco

KNF.


# 1.47 11-Apr-2006 gwk

Add acpi_resume(struct acpi_softc *sc), which calls some AML methods,
detected at attach time, currently unused.

ok marco@


# 1.46 11-Apr-2006 gwk

Discover some ACPI sleep related methods, and call them at the
appropriate time in acpi_enter_sleep_state().

ok marco@


Revision tags: OPENBSD_3_9_BASE
# 1.45 26-Feb-2006 marco

Add acpicpu as a device.

ok jordan@


# 1.44 23-Feb-2006 marco

remove dead function.

ok jordan@


# 1.43 22-Feb-2006 marco

Print out all tables it found during autoconf. We'll need this data in
the future.


# 1.42 22-Feb-2006 marco

Make dmesg less verbose.

ok jordan.


# 1.41 22-Feb-2006 jordan

Added parsing of SSDT tables
ok marco@


# 1.40 22-Feb-2006 jordan

Added new aml_register to notify by device id
Moved powerdown code to button device
ok marco@


# 1.39 21-Feb-2006 marco

Readjust debug levels that crept in too low over the last few days.
KNF


# 1.38 21-Feb-2006 marco

Rig the power button so that the machine shuts down correctly.


# 1.37 20-Feb-2006 marco

Add acpibtn device. Remains disabled for now.


# 1.36 19-Feb-2006 jordan

Added code for aml_notify GPE notification callback
ok marco@


# 1.35 19-Feb-2006 grange

Fix acpi_enter_sleep_state() by replacing current ugly hack
with mostly correct sleeping state transition code.

ok brad@ marco@


# 1.34 19-Feb-2006 marco

jordan:
Enable interrupts.
Added peripheral code to make this work.

marco:
Replace softintr and other types of interrupts with a handler with context.
Fixed interrupt magic for GPEs.
Some KNF

This is work done by jordan and I but has to be a single commit.


# 1.33 17-Feb-2006 marco

Remove earlier introduced delays. We really only need them during autconf.

ok jordan@


# 1.32 17-Feb-2006 marco

Add a generic delay function as per the ACPI spec.

from jordan@


# 1.31 17-Feb-2006 marco

Use tsleep instead of delay when !cold.


# 1.30 17-Feb-2006 marco

Argh, we need delays between reads/writes. The backend reads (i2c most likely)
are so slow that values come back as 0 sometimes. We have to do this
despite the spec saying we shouldnt have to. Guess having a faster
parser than others can hurt sometimes.

Idea from jordan.


# 1.29 17-Feb-2006 jordan

Fixed code for handling _INI
Rearranged debug statements
ok marco@


# 1.28 16-Feb-2006 jordan

Added stub for calling _INI
Fixed aml_dowhile for NULL return
ok marco@


# 1.27 16-Feb-2006 jordan

Fixed parser/evaluator to retrieve battery info+state
Cleanup of parser
Dell laptops return battery status
ok marco@


# 1.26 06-Feb-2006 brad

fix a typo so this will compile on amd64.


# 1.25 04-Feb-2006 marco

When ACPI_ENABLED wasn't set but the devices were it had an unknown variable.

Pointed out by ian@.


# 1.24 03-Feb-2006 jordan

Rewrote aml parser yet again...
Fixed bus_space_map for i386/amd64
Added _BST and _BIF to acpibat to get status/info
ok marco@


# 1.23 20-Jan-2006 jordan

More evaluator changes
Removed old evaluator code
Fixed _HID value
ok marco@


# 1.22 19-Jan-2006 jordan

Added support for read/write of PCI Config space
Help jason@
ok marco@


# 1.21 18-Jan-2006 jordan

Added new evaluation routines
Added helper methods for buffer/field evaluation
Fixed broken indentation on dsdt.c

ok marco@


# 1.20 17-Jan-2006 jordan

Major overhaul of the aml parser. Evaluater temporarily disabled.

ok marco@


# 1.19 06-Jan-2006 grange

Check for ACPI control availability only if ACPI_ENABLE defined.


# 1.18 06-Jan-2006 grange

Informative error messages on attach, prettier dmesg.


# 1.17 06-Jan-2006 grange

printf -> dnprintf conversion for debug messages.


# 1.16 05-Jan-2006 grange

Find SLP_TYPx values and store them for future use in
acpi_enter_sleep_state().


# 1.15 05-Jan-2006 grange

Random KNF and cleanup


# 1.14 05-Jan-2006 grange

Provide symbolic names for the acpi sleeping states and use it.


# 1.13 28-Dec-2005 marco

Moved DEVNAME to a acpidev.h

ok jordan@


# 1.12 18-Dec-2005 sturm

add back comment about wrong code

ok marco


# 1.11 16-Dec-2005 marco

Remove test for kbd_reset. Prompted deraadt@, ok jordan@


# 1.10 16-Dec-2005 jordan

Added hooks for powerdown/power button press


# 1.9 16-Dec-2005 jordan

Cleaned up common power management register i/o
Added enter sleep state ioctl
Hooked power button interrupt
ok marco@


# 1.8 16-Dec-2005 marco

Remove autoconf chatter


# 1.7 16-Dec-2005 jordan

Moved DSDT AML parsing into parent ACPI driver
ok marco@


# 1.6 14-Dec-2005 marco

Add AC device glue.


# 1.5 14-Dec-2005 marco

Adjust debug printing levels to something more sane.


# 1.4 13-Dec-2005 marco

Add glue to attach devices to apci.


# 1.3 07-Dec-2005 marco

KNF


Revision tags: OPENBSD_3_8_BASE
# 1.2 10-Jul-2005 grange

Spacing cleanup.


# 1.1 02-Jun-2005 tholo

Start on a basic ACPI framework -- does not do much more than read out the
ACPI tables into kernel memory and attach ACPI and HPET timers currently.

In order to test this code, enabling the devices in GENERIC as well as
the ACPI_ENABLE option is needed. This code does not do any thermal
control yet, so this should be done with care depending on the platform.

In the tree so more people can contribute to making this more fully
featured.

Ok niklas@ grange@ tedu@


# 1.389 21-Jul-2020 deraadt

acpi can use IPL_BIO (a low interrupt) since it only enqueues operations for
later processing. The use of a high interrupt will predate suspend/resume
efforts, we had to redesign acpi to be non-reentrant obviously
discussed with kettenis, in snaps for more than a week


# 1.388 24-Jun-2020 cheloha

kernel: use gettime(9)/getuptime(9) in lieu of time_second(9)/time_uptime(9)

time_second(9) and time_uptime(9) are widely used in the kernel to
quickly get the system UTC or system uptime as a time_t. However,
time_t is 64-bit everywhere, so it is not generally safe to use them
on 32-bit platforms: you have a split-read problem if your hardware
cannot perform atomic 64-bit reads.

This patch replaces time_second(9) with gettime(9), a safer successor
interface, throughout the kernel. Similarly, time_uptime(9) is replaced
with getuptime(9).

There is a performance cost on 32-bit platforms in exchange for
eliminating the split-read problem: instead of two register reads you
now have a lockless read loop to pull the values from the timehands.
This is really not *too* bad in the grand scheme of things, but
compared to what we were doing before it is several times slower.

There is no performance cost on 64-bit (__LP64__) platforms.

With input from visa@, dlg@, and tedu@.

Several bugs squashed by visa@.

ok kettenis@


# 1.387 10-Jun-2020 jca

Give userland a consistent battery and A/C state after resume

Previously we refreshed the state of acpiac(4) and acpibat(4)/acpisbs(4)
by queueing fake ACPI events to be processed by the acpi thread.
acpibat(4) using ACPIDEV_POLL meant that its status could be refreshed
first, resulting in an APM_POWER_CHANGE event sent to userland before
the state of acpiac(4) was up to date. Because of this, apmd(8) could
see A/C unplugged when it was in fact plugged, possibly leading to
a wrong decision.

Instead of using fake ACPI events, refresh the status of acpiac(4) and
acpibat(4)/acpisbs(4) right away at DVACT_WAKEUP time. To reuse the
existing refresh code of those drivers, make their ACPI *_notify()
handlers responsible for sending APM_POWER_CHANGE events instead. Also
queue an APM_POWER_CHANGE event right after resume to retain existing
behavior.

This fixes the consistency of data shown to userland, and makes those
drivers less special.

Initially discussed last year (!) with mpi@ at Miod's place.
Feedback and ok jcs@, deraadt@ agrees with the use of DVACT_WAKEUP here.


# 1.386 29-May-2020 deraadt

dev/rndvar.h no longer has statistical interfaces (removed during various
conversion steps). it only contains kernel prototypes for 4 interfaces,
all of which legitimately belong in sys/systm.h, which are already included
by all enqueue_randomness() users.


# 1.385 14-May-2020 kettenis

Bring back the acpi(4) changes. This time around only use the ACPI _CRS
information on ACPI 5.0 and later.

ok krw@, patrick@


# 1.384 11-May-2020 jca

More accurate remaining power reporting with disparate batteries

Overall remaining power was computed as the average of the remaining
power of each battery, in percents. This doesn't work with batteries of
different capacity, so use the total capacity and remaining power
instead.

Reported by lidstah, successful tests by lidstah and benno@, ok kettenis@


# 1.383 08-May-2020 kettenis

Move parsing of _CRS into acpi(4) and pass the parsed address and interrupt
information in struct acpi_attach_args.

ok mpi@


Revision tags: OPENBSD_6_7_BASE
# 1.382 14-Apr-2020 kettenis

Implement acpi_getprop(), the ACPI equivalent of OF_getprop().

ok patrick@


# 1.381 12-Apr-2020 kettenis

Add support for the _CCA method. This method indicates whether DMA is
cache-coherent or not. To implement this, acpi(4) gets two bus_dma tags
and passes the appropriate one when attaching devices based on _CCA.
On i386/amd64, where for all practical purpose DMA is always cache-coherent,
the two tags are the same. But on arm64 they are distinct.

ok patrick@


# 1.380 07-Apr-2020 visa

Abstract the head of knote lists. This allows extending the lists,
for example, with locking assertions.

OK mpi@, anton@


# 1.379 03-Apr-2020 mpi

Fix incoherencies of errors returned by various *kqfilter routines.

EOPNOTSUPP is returned when there's no "filterops" for a given fd.
EINVAL when the requested filter isn't supported by the "filterops".
ENXIO means the underlying device is no longer valid/present.

ok visa@


# 1.378 20-Feb-2020 visa

Replace field f_isfd with field f_flags in struct filterops to allow
adding more filter properties without cluttering the struct.

OK mpi@, anton@


# 1.377 31-Dec-2019 visa

Use C99 designated initializers with struct filterops. In addition,
make the structs const so that the data are put in .rodata.

OK mpi@, deraadt@, anton@, bluhm@


# 1.376 31-Dec-2019 mpi

Convert infinite sleeps to {m,t}sleep_nsec(9).

ok kn@


# 1.375 31-Dec-2019 jsg

include sys/sensors.h for sensor use
don't depend on indirect inclusion from acpidev.h


Revision tags: OPENBSD_6_6_BASE
# 1.374 07-Sep-2019 kettenis

Revert acpipci(4) changes. Too many systems are broken. We'll try again
after 6.6 as been released. The acpireg.h change stays behind.


# 1.373 28-Aug-2019 kettenis

Use ACPI information to attach PCI busses like we do on arm64. There are a
few additional quirks though, and attaching the PCI busses is delayed to
replicate the existing code more closely. That may be changed in the
future. Also tweak how we handle MSI support and respect to ACPI flag
that says we shouldn't attempt to use MSIs.

Some fallout is expected.

ok patrick@


# 1.372 27-Aug-2019 deraadt

On amd64 the bootup and resume paths for SP/BP/SP were not performing
msr/pat, clock-startup, firmware upgrade, fpu initialization, and cpu
soft-state initialization in the same order. That kind of chaos is
crazy and fragile. Improve the acpi resume call sequence and unify order
of the operations.


# 1.371 02-Jul-2019 jcs

acpi_set_gpehandler: turn edge arg into flags, add support for
passing GPE_DIRECT which directly executes the supplied callback and
clears a level interrupt rather than going through acpi_taskq which
can be slow when other driver callbacks are in the queue.

This should not be used for GPE handlers that need to do anything
ACPI-related.

ok kettenis


# 1.370 10-Jun-2019 kettenis

Print proper ACPI version number.

ok mlarkin@


# 1.369 08-Jun-2019 kettenis

When establishing the mapping between ACPI device nodes and PCI devices,
skip PCI host bridges and devices that aren't there.

ok patrick@


# 1.368 07-Jun-2019 kettenis

Introduce acpi_getsta() and use it to eliminate some duplicated code.

ok patrick@


# 1.367 12-May-2019 kettenis

Make sure the extended PM register descriptions actually exist before
using them. Fixes machines with ancient ACPI 1.0 (such as recent
hypervisors).

ok deraadt@


# 1.366 12-May-2019 kettenis

Add support for mapping ACPI PM registers using the "extended" gasio
representation from the FADT. Mostly fixes the Lanner NCA-1510.

ok lteo@, mlarkin@, deraadt@


# 1.365 11-May-2019 lteo

Fix incorrect error message.

ok kettenis@


# 1.364 08-May-2019 tedu

free size. the allocation is the header plus the length.


Revision tags: OPENBSD_6_5_BASE
# 1.363 04-Apr-2019 kettenis

Avoid potential null-pointer dereference. Found by Kent R. Spillner.

ok deraadt@, jsg@


# 1.362 20-Jan-2019 jsg

fix SMALL_KERNEL build after changes in rev 1.361
reported by naddy@ ok deraadt@


# 1.361 19-Jan-2019 tedu

Add a pwraction sysctl that controls what the power button does on acpi.
By default, nothing changes -- shutdown is initiated. But allows turning
power button into a sleep button if desired.
(grudging) ok from a few parties


# 1.360 26-Oct-2018 kettenis

Add acpipci(4) on amd64. For now this only calls the PCI-specific _OSC
method to let the ACPI implementation know what features we support.


Revision tags: OPENBSD_6_4_BASE
# 1.359 30-Aug-2018 kettenis

Move softbutton check before the check that skips attaching certain acpi
devices. Fixes regression caused by matching on _CID in addition to
matching on _HID.

ok matthieu@, mlarkin@


# 1.358 25-Aug-2018 kettenis

Match ACPI devices based on _CID if no match for _HID is found.

ok mpi@


# 1.357 19-Aug-2018 kettenis

Add support for multiple PCI segments. Only really implemented for arm64
for now as amd64/i386 firmware still caters for legacy OSes that only
support a single PCI segment.

ok patrick@


# 1.356 03-Aug-2018 kettenis

Let ahci(4) match on _CLS instead of _HID when attaching at acpi(4). Avoids
having to add many more _HID entries to the match table.

ok deraadt@, mlarkin@


# 1.355 10-Jul-2018 kettenis

Make legacy interrupts work in acpipci(4).

ok patrick@


# 1.354 05-Jul-2018 kettenis

Add acpipci(4), a driver that supports generic ECAM-compatible PCI host
bridges based on information provided by ACPI.

ok mlarkin@


# 1.353 04-Jul-2018 kettenis

Properly pass around the PCI "chipset tag" in acpi(4) and refactor
acpimcfg(4) to call an MD initialization functions that sets up a tag for
PCI ECAM.

ok guenther@, mlarkin@, krw@


# 1.352 01-Jul-2018 kettenis

Move acpi(4) attach glue into acpi_machdep.c.

ok guenther@, deraadt@


# 1.351 01-Jul-2018 kettenis

Add support for _DSD Device Properties.

ok mlarkin@


# 1.350 30-Jun-2018 kettenis

Add intr_enable() function, intended for MI use to amd64 and i386 and use
this in the acpi(4) suspend/resume code paths.

ok deraadt@


# 1.349 29-Jun-2018 kettenis

Use <stdint.h> types.

ok mpi@, jung@, krw@, deraadt@


# 1.348 29-Jun-2018 mlarkin

ACPI: Allow (with warning) GPE handler reassignment, instead of returning
a failure code.

ok kettenis


# 1.347 26-Jun-2018 mlarkin

knf


# 1.346 26-Jun-2018 kettenis

Let acpi(4) pass down the bus dma tag.

ok deraadt@


# 1.345 25-Jun-2018 kettenis

Reorganize acpi(4) code a bit in preparation for upcoming arm64 support.

ok deraadt@


# 1.344 20-May-2018 kettenis

Wake up the acpi thread after scheduling a gpio event.

ok mpi@


# 1.343 17-May-2018 kettenis

Sprinkle some #ifndef SMALL_KERNEL such that RAMDISK kernels cuild again.

pointed out by stsp@


# 1.342 17-May-2018 kettenis

Implement GenericSerialBus OpRegion support.

ok mlarkin@


# 1.341 27-Mar-2018 kettenis

Add acpicmos(4), a driver that implements SystemCMOS OperationRegion
access support. This fixes machines where the AML doesn't check whether
support for this OperationRegion type has been registered by the OS.

ok mlarkin@


Revision tags: OPENBSD_6_3_BASE
# 1.340 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.339 10-Feb-2018 deraadt

Syncronize filesystems to disk when suspending. Each mountpoint's vnodes
are pushed to disk. Dangling vnodes (unlinked files still in use) and
vnodes undergoing change by long-running syscalls are identified -- and
such filesystems are marked dirty on-disk while we are suspended (in case
power is lost, a fsck will be required). Filesystems without dangling or
busy vnodes are marked clean, resulting in faster boots following
"battery died" circumstances.
Tested by numerous developers, thanks for the feedback.


# 1.338 08-Feb-2018 deraadt

After flushing disks, try again to release all memory. We don't need to
save dirty memory to the hibernate space.


# 1.337 08-Feb-2018 deraadt

Toss all releaseable memory, because fragmentation can get in the way
of allocating the hibernate playpen.


# 1.336 08-Feb-2018 deraadt

sr_quiesce() is a new approach for ensuring that softraid drains
output to the disks.

This is part of a larger suspend/resume filesystem-safety diff, which
has been worked on for a couple of months already. Tests by job, krw,
beck, benno, and others. Sometimes even by snapshot users...


# 1.335 29-Nov-2017 kettenis

Add support for GPIO-signaled events.

ok mlarkin@


# 1.334 16-Nov-2017 jcs

Add PCI attachment for dwiic(4) needed by Intel 100 Series machines

ok kettenis


# 1.333 14-Nov-2017 kettenis

Add a few more PNP IDs to the skip lists.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.332 17-Aug-2017 stsp

Make dwiic(4) attach its ACPI parent dependencies.
This change makes the Asus E200HA keyboard work.

Original analysis and patch by Cesare Gargano
ok kettenis@


# 1.331 09-Aug-2017 dcoppa

Add "PNP0303" (8042 PS/2 Controller) to acpi_isa_hids[]

ok kettenis@


# 1.330 22-Jul-2017 jcs

acpibat: look for _BIX first before falling back on _BIF

ACPI 4.0 deprecated _BIF for battery status so some newer machines
are exporting _BIX which extends _BIF's package with a few extra
fields. When using _BIX, export the new discharge cycle count as a
sensor.

feedback from kettenis and anton
ok deraadt


# 1.329 20-Jul-2017 kettenis

Start a list of isa(4) device names such that we can avoid printing them
as "not configured".

ok mikeb@, millert@, deraadt@


# 1.328 08-Apr-2017 deraadt

Sensors are run as callbacks inside tasks. During suspend a sensor
could be running inside a driver that will be force-detached, or due
to tsleep end up disrupting the softstate/hardstate contract. At
suspend time, quisce all these callbacks by waiting for completion.
This issue has never been observed for real, but may be implicated in
suspend/resume failures.
ok kettenis guenther mlarkin


# 1.327 08-Apr-2017 deraadt

Two prototypes for acpi_maptable; merge to one place


Revision tags: OPENBSD_6_1_BASE
# 1.326 27-Mar-2017 deraadt

Now that hibernate_alloc() only has clean success/failure, don't
need to call hibernate_free() to clean up a partial mess.
ok mlarkin kettenis


# 1.325 13-Mar-2017 deraadt

allow ramdisks to build; ok jcs


# 1.324 12-Mar-2017 jcs

try to attach acpisbs first and if successful, don't attach acpibat


# 1.323 02-Mar-2017 natano

Add a new sysctl machdep.lidaction. The sysctl works as follows:

machdep.lidaction=0 # do nothing
machdep.lidaction=1 # suspend
machdep.lidaction=2 # hibernate

lidsuspend is just an alias for lidaction, so if you change one, the
other one will have the same value. The plan is to remove
machdep.lidsuspend eventually when people have upgraded their
/ets/sysctl.conf.

discussed with deraadt, who came up with the new MIB name
no objections mlarkin
ok stsp halex jcs


# 1.322 28-Feb-2017 natano

Untangle abstract sleep modes from ACPI sleep states. This paves the way
for further work in the area.

idea and ok deraadt
ok mlarkin


# 1.321 22-Feb-2017 jcs

add acpisbs, an acpi smart battery subsystem driver reading data
over smbus

currently disabled because it conflicts with acpibat

ok deraadt, kettenis


# 1.320 14-Jan-2017 kettenis

Revert aml_rdpciaddr changes; breaks several machines that were working
before.


# 1.319 11-Jan-2017 jsg

Increase the size of the buffers used for _HID and _CID strings to be
able to fit "Hyper_V_Gen_Counter_V1".

ok mlarkin@ deraadt@


# 1.318 08-Jan-2017 kettenis

Make aml_rdpciaddr() work for for devices behind bridges. The code now
uses the mapping between AML nodes and PCI devices that we establish.
Because _INIT methods may end up calling aml_rdpciaddr(), make sure we
create that mapping early. Also handle devices that aren't actually present.
These devices are now included in the mapping and reads will return an
all-ones patterm whereas writes are a no-op..

ok mlarkin@


# 1.317 25-Oct-2016 pirofti

Add the resource index to aml_parse_resource() callback function.

This is needed by an upcoming acpiec commit that handles machines breaking
the current ACPI specifications.

Change suggested by and ok kettenis@, guenther@


# 1.316 18-Sep-2016 guenther

If the APIC table (MADT) isn't present then ci_acpi_proc_id won't be set
when acpicpu tries to attach, leading to a NULL dereference. The ACPI
processor object on the Dells where this was reported is bogus and useless
anyway so apply the same test when deciding whether to attach to processor
objects as acpicpu would when matching them to cpus.

Problem reported by Eivind Eide (xenofil (at) gmail.com) and
Johan Huldtgren (johan+openbsd-bugs (at) huldtgren.com).
Testing by Eivind Eide.
ok kettenis@ mlarkin@


# 1.315 03-Sep-2016 naddy

Write the system time back to the RTC every 30 minutes.
This fixes the problem that long-running machines which were not
shut down properly would reboot with a badly offset system time.

hints and ok kettenis@


# 1.314 31-Aug-2016 mlarkin

Don't put a device in the wake device list if _STA says it's not there.

Fixes at least one machine that woke up from sleep / poweroff immediately.

ok kettenis@, tested by kettenis and martijn@


# 1.313 28-Jul-2016 kettenis

Store the acpi processor ID/UID in struct cpu_info, and use it to attach
acpicpu(4) drivers to the right cpu(4).

ok mlarkin@, guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.312 10-Jun-2016 kettenis

The power management timer has been made optional in ACPI 5.0A, so don't
attempt to attach acpitimer(4) if the timer isn't present.

Pointed out by jcs@

ok jcs@


# 1.311 08-May-2016 kettenis

Make sure devices listed by a device's _DEP method are attached before
we attach the device itself. The _DEP methode, introduced in ACPI 5.0,
is a hint that the device in question depends on OpRegion support from
the devices listed by _DEP, so we have to order them the proper way.
To prevent us from attaching those devices again when we encounter them
later walking down the device tree, keep track of our attempts to
attach a device driver to them using aflag in the aml_node structure.

ok guenther@


# 1.310 25-Apr-2016 pirofti

We attach acpihpet(4) and acpiec(4) without using the HID, so do not
report it in the dmesg.

OK deraadt@, mlarkin@.


# 1.309 02-Apr-2016 kettenis

Reduce dmesg spam by knocking out some acpi hardware IDs that don't need a
driver.

ok mpi@


# 1.308 29-Mar-2016 kettenis

Check if a device is present (using _STA) in acpi_foundhid. This prevents
us from reporting devices as "not configured" that aren't actually present,
and allows us to remove duplicated code from several drivers.

ok deraadt@, ok jsg@


# 1.307 29-Mar-2016 kettenis

Implement a cleaner way to prevent acpivout(4) from attaching if we have
a platform-specific method of controlling the backlight.

ok millert@, mlarkin@, jung@


# 1.306 28-Mar-2016 kettenis

Adding each and every new acpi device driver to acpi_foundhid() is
gettinga bit of the burden. And it really isn't how our autoconf
framework is supposed to work for busses that can be enumerated. All
the drivers already check for a matching _HID in their attach
function. So we can just drop the checks in acpi_foundhid() and let
the drivers sort themselves out.

Print a "not configured" line for devices for which we don't attach adriver.
This may be a bit much as there are devices for which we will not have a
driver. This will be removed if it causes too much dmesg spam.

ok deraadt@, mlarkin@


Revision tags: OPENBSD_5_9_BASE
# 1.305 17-Jan-2016 jsg

Take acpi_foundhid() outside of #ifndef SMALL_KERNEL so the ramdisk
kernel can find and attach dwiic(4) and sdhc(4).


# 1.304 17-Jan-2016 jsg

remove an unused global variable


# 1.303 14-Jan-2016 kettenis

Attach dwiic(4) on the Intel Bay Trail i2c controllers as well.


# 1.302 12-Jan-2016 kettenis

Improve emulation of PM registers on Hardware-redcued ACPI by emulating
SPL_TYPx, SLP_EN and WAK_STS using the new SLEEP_STATUS_REG and
SLEEP_CONTROL_REG registers. Makes the transition into S5 (powerdown) work
on the ASUS X205TA.

ok deraadt@, mlarkin@


# 1.301 12-Jan-2016 jcs

Add dwiic, a driver for the Synopsys DesignWare i2c controller found
on the Samsung ATIV Book 9 laptop. This initial version only
supports ACPI config/attachment.

Add ihidev, a HID-over-i2c driver largely based on uhidev. dwiic
handles attaching ihidev devices found in ACPI.

Add ims, a HID-over-i2c mouse/trackpad driver to get basic cursor
and button functionality from HID-compliant i2c trackpads.

ok kettenis deraadt


# 1.300 11-Jan-2016 kettenis

Make sdhc(4) attach to hardware IDs "80860F14" and "PNP0FFF".


# 1.299 09-Jan-2016 kettenis

Initial support for Hardware-Reduced ACPI. This skips the legacy mode checks
by emulating the PM_CNT register such that SCI_EN is always enabled. It also
skip attaching acpitimer(4) since the relevant registers are no longer there.

ok deraadt@, mlarkin@


# 1.298 06-Jan-2016 kettenis

Follow the spec and check the SCI_EN bit to see if ACPI has already
been enabled and don't bother enabling it again if it is. This should fix
systems that don't implement legacy mode and therefore set ACPI_ENABLE and
ACPI_DISABLE to zero.

ok mlarkin@, yasuoka@


# 1.297 23-Nov-2015 reyk

Back out the CRITICAL change, turn it back from 10% to 25%. Some
people with old/broken batteries didn't like this change, and the
underlying heuristics are wrong.


# 1.296 22-Nov-2015 reyk

The "CRITICAL" APM battery level, that is exported/faked by the ACPI
stack, is currently set to 25%. With 25% battery left, a typical
modern laptop will have at least one hour runtime left. It is an
arbitrary value, and other APM power layers in the kernel set CRITICAL
to something like 5% or 10%, so set it to 10% for now.

OK mlarkin@


# 1.295 28-Sep-2015 deraadt

In low-level suspend routines, set cold=2. In tsleep(), use this to
spit out a ddb trace to console. This should allow us to find suspend
or resume routines which break the rules. It depends on the console
output function being non-sleeping.... but that's another codepath which
should try to be safe when cold is set.
ok kettenis


# 1.294 06-Sep-2015 deraadt

clarify malloc arguments (base + length...)


# 1.293 28-Aug-2015 deraadt

fairly simple sizes for free(); ok tedu


# 1.292 20-Aug-2015 kettenis

Respect the access size when reading or writing to pci config space.
This version also makes sure writes are properly aligned, preventing
the panics seen with the previous incarnation of this change.

Fixes battery status passthrough in vmware, brightness keys on my x220
and who knows what else.

ok mlarkin@, deraadt@


Revision tags: OPENBSD_5_8_BASE
# 1.291 04-Aug-2015 deraadt

If we are setting ACPI_PM1_BM_RLD do it only once, and remember to
repeat this action at resume time.
ok mlarkin guenther


# 1.290 29-Jul-2015 deraadt

Something subtle in previous revision is locking up suspend or booting
on some laptops. Mailing lists threads mention x220 and Asus machines.
Reverting for now.


# 1.289 21-Jul-2015 kettenis

iRespect the access size when reading or writing to pci config space.
Fixes battery status passthrough in vmware and who knows what else.

Tested by many.

ok mlarkin@, deraadt@


# 1.288 17-Jul-2015 mlarkin

fix comments


# 1.287 26-Mar-2015 jsg

Use the 0x10 extended capability to determine if the trackstick
buttons are wired to the trackpad and need to be re-routed to the
trackstick. Without this change the buttons on 2015 thinkpads
get picked up as extended buttons that show up as scroll up/down.

Remove the X1 Carbon 2015 (LEN0048) and X250 (LEN0046) from the
top button area/soft buttons quirks list. Also avoid using the
quirk list entirely if the capability bit is set.

Based on work from jcs@ with feedback from mpi@
ok mpi@ jcs@


# 1.286 20-Mar-2015 kettenis

The ACPI standard says that a PCI function number of FFFF can be used to refer
to all functions on a device. This makes no sense in the context of accessing
PCI config space. Nevertheless stsp@ has a machine with AML that tries to
do config space reads from a device where _ADR encodes function number FFFF.
This is almost certainly buggy AML, but since Linux (and presumably Windows)
boot just fine on this machine we need to work around this. Linux effectively
treats function FFFF as function 7, which isn't present on this machine.
Instead we simulate a read from a non-existent device by returning a bit
pattern of all ones. Writes to function FFFF will still panic.

ok mlarkin@


# 1.285 16-Mar-2015 deraadt

Reduce use of SMALL_KERNEL in acpi, bringing acpiec driver and more gpe
handling into RAMDISK. This is now possible because the install media
has ample room. The goal is to reduce special cases where we may be
skipping (unknown) important operations...
ok mlarkin kettenis


# 1.284 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.283 07-Feb-2015 deraadt

New framework that allows hibernate to pass in entropy from it's fresh
boot.
ok mlarkin


# 1.282 06-Feb-2015 mlarkin

Fix a hibernate crash on some machines due to unmapping a page that
may not have been mapped previously (in the failure to hibernate case).

Also ensure that the lowmem ptp is mapped in all cases (not just MP).

ok kettenis


# 1.281 17-Jan-2015 deraadt

resume_randomness() before spinning up other cpus...


# 1.280 15-Jan-2015 jsg

Define a new wscons mouse type for Synaptics clickpad devices that lack
physical buttons. This will be used if the acpi pnpid for the mouse
matches a list provided by Synaptics found in Linux.

Combined with the xenocara changes this will remove the need for an xorg
config file for the x240/t440/t540 generation of thinkpads.

ok matthieu@ shadchin@ kettenis@


# 1.279 11-Jan-2015 kettenis

Tolerate checksum errors in ACPI tables (but complain about them). Apparently
there are machines out there with broken BIOSen with such checksum errors in
thier RSDT tables. Windows runs fine on these machines, so it probably does
not check. Linux ignores checksum errors as well (and prints a warning).

ok deraadt@, krw@


# 1.278 18-Dec-2014 deraadt

suspend and resume the RNG (at what looks like the right spots)
ok reyk djm


# 1.277 09-Dec-2014 doug

Replace some malloc(n*size,...) calls with mallocarray().

ok tedu@ deraadt@


# 1.276 06-Dec-2014 mpi

If "machdep.lidsuspend" is not set, do not go back to sleep if the lid
is still closed when resuming. This allows docked laptop to be resumed
without opening them.

ok mlarkin@


# 1.275 23-Nov-2014 mlarkin

If a machine has no _LIDs, then none can be open. This diff fixes a problem
where machines without any _LIDs were going back to sleep after hibernate
due to the recent suspend-if-lid-closed-on-resume diff. Noticed on qemu,
which doesn't have any _LIDs.

discussed with deraadt


# 1.274 18-Nov-2014 krw

Nuke yet more obvious #include duplications.

ok miod@


# 1.273 08-Nov-2014 mlarkin

If resuming from sleep (zzz/ZZZ) and the lid is still closed, go back to
sleep. This prevents accidental lid flex or slight opening in a backpack
from waking the machine up and leaving it resumed (powered on).

ok deraadt@


# 1.272 26-Sep-2014 kettenis

Rework piglet and pig allocation. Currently the piglet gets allocated
deep down in the suspend path, where it is really hard to recover from
allocation failure. So allocate the piglet early on in the suspend path.
Also change the piglet and piglet allocation functions to use km_alloc(9)
instead of doing pmemrange magic. This removes a bunch of code which, in the
case of the piglet allocation, is broken since it results in a NULL pointer
dereference. Also switch the piglet allocation to not wait. If we can't
allocate 16MB of phys contig memory on a halfway modern machine we're almost
certainly under a lot of memory pressure and we're better off not trying to
hibernate anyway.

ok mlarkin@


# 1.271 25-Sep-2014 kettenis

Bring back rev 1.249. Now that mp_setperf() has been fixed, it should be
safe to restore perflevel when resuming. Should fix the issues people
see with the processor running at full speed heating up the machine while
the apm command reports that the CPUs are running at a low speed.

ok dcoppa@, deraadt@


# 1.270 23-Sep-2014 kettenis

Release the acpi lock around calls to wsdisplay_suspend() and
wsdisplay_resume(). The X server may adjust the display brightness just
before/after the VT switch when suspending/resuming. In fact, it may
already be blocked adjusting the display brightness for some reason
unrelated to the suspend request.

The VT switch request will eventually time out, but the delay is annoying
and the failed VT switch will lead to problems after resume.

Needed for a future update of the xf86-video-intel driver.

ok mlarkin@


# 1.269 19-Sep-2014 kettenis

Use config_suspend_all(9).


# 1.268 08-Sep-2014 kettenis

Clear the PME Status bit on PCI devices in response to Device Wake events.
Fixes the interrupt storm on the 2009 Mac mini with WOL enabled on its
nfe(4) interface.

ok mlarkin@


Revision tags: OPENBSD_5_6_BASE
# 1.267 20-Jul-2014 mlarkin

Support hibernating to softraid crypto volumes.

much help and ok from deraadt@


# 1.266 16-Jul-2014 mlarkin

Reenable hibernate RLE support and flush+zero all memory after unpack.


# 1.265 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.264 12-Jul-2014 kettenis

Add a function to drop all clean pages on the page daemon queues and call
it when we hibernate.

ok mlarkin@, miod@, deraadt@


# 1.263 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.262 11-Jul-2014 mlarkin

Flush the buffercache to 16MB on hibernate and restore its previous max
size (kern.bufcachepercent) on resume, for better hibernate performance.

ok beck@


# 1.261 10-Jul-2014 blambert

remove unneeded header

ok mlarkin@


# 1.260 01-Jul-2014 kettenis

Adding an additonal delay after each access to SystemMemory or SystemIO
during autoconf makes no sense.

ok deraadt@


# 1.259 23-Jun-2014 kettenis

SystemMemory is used to access memory mapped registers on some machines, so we
must use the bus_space(9) API to access it instead of memcpy(9). Also make
sure we properly align access to fields that need more than byte alignment.

ok guenther@


# 1.258 21-May-2014 mlarkin

Fix invalid / missing format specifiers in several dnprintf statements
(used only when ACPI_DEBUG is enabled)

From a diff on tech@ by Fabian Raetz, with a minor change by me, tested
on i386 and amd64 with ACPI_DEBUG enabled and disabled.


# 1.257 25-Apr-2014 mlarkin

Defer the free of the hibernate structures until later, when it is safe to
do so.

Freeing the hibernate structures too early leads to inadvertent calls into
splx when we aren't ready for them yet. Found when trapping splx calls in
the resume path.

discussed with deraadt@ at m2k14


# 1.256 13-Apr-2014 mlarkin

Fix a badly worded debug message that implied the entire machine was going
to sleep, instead of just the acpi thread going to sleep.


# 1.255 31-Mar-2014 mpi

Change the order of operations during a suspend/resume cycle and
call bufq_quiesce() after executing the DVACT_QUIESCE handlers.

This should be safe since no disk nor controller drivers have such
handler but it will allow us to detach sd(4) devices attached to a
USB bus.

Another benefit pointed out by kettenis@ is that drivers that need
to read a firmware from the disk should be able to do it at resume
time in a DVACT_WAKEUP handler.

ok kettenis@, deraadt@


# 1.254 13-Mar-2014 dlg

get rid of the assumption that the head of the alldevs list is the
"mainbus" device. this breaks when mpath is enabled because it
attaches before mainbus and therefore takes the head position.

have autoconf provide device_mainbus() which looks up mainbus_cd,
and use that instead.

discussed with deraadt who just wants mpath stuff to move forward
despite there being many ways to shine this particular turd.


Revision tags: OPENBSD_5_5_BASE
# 1.253 21-Feb-2014 deraadt

Do the lid control further on in the resume sequence, since this is
AML, and will do WAITOK operations. While cold, that is a bad idea.
We seem to be safe with respect to the spec, I do not see any event
debounce issues here.
ok kettenis, tested by many


# 1.252 20-Jan-2014 claudio

Instead of checking sc->sc_revision <= 1 check that the
fadt->hdr_revision <= 1. This is a better check since it checks
the version of the proper descriptor. This allows the alix APU
to reboot since those boards have currently a wrong sc->sc_revision.
Committing this now so that it can be tested easier.
With and OK jsg@ also fine by derradt@


# 1.251 24-Dec-2013 kettenis

If the FADT has its SMI_CMD set to zero, assume we're only ACPI-only hardware
and don't need to disable SMI ownership of the ACPI hardware registers.

ok mlarkin@


# 1.250 23-Dec-2013 kettenis

Revert previous commit. It makes resume hang on some MP systems running
GENERIC.MP.


# 1.249 16-Dec-2013 kettenis

Restore hw.setperf upon resume like we do for apm(4) on i386. Seems we need
to do this fairly late in the resume to avoid hangs.

ok deraadt@, mlarkin@


# 1.248 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


# 1.247 06-Nov-2013 mpi

Add support for Power Resources for Dx states and the necessary hook
for PCI devices. This hook should be called twice, before and after
changing the power state of a PCI device.

Before setting the device to the new state, the ACPI layer will notify
every power resources linked to the device for that state and make sure
they are turned "_ON". After changing the state of the device, it will
decrement the reference of every power resources linked to that device
for the old state and turn them "_OFF" if they are no longer referenced.

This fixes the no-USB after resume problem seen on various ThinkPad,
problem initialy diagnosed with Alexander Polakov.

ok kettenis@, deraadt@


Revision tags: OPENBSD_5_4_BASE
# 1.246 01-Jun-2013 mlarkin

Stop printing ACPI wakeup devices in dmesg after the 16th wakeup device.

Helps with VMware VMs that report hundreds of wakeup devices.

ok reyk@, deraadt@


# 1.245 31-May-2013 bluhm

Fix the build for a kernel without wd and pciide in its config.
OK deraadt@ tedu@


# 1.244 30-May-2013 deraadt

Enforce ca_activate tree-walks over the entire heirarchy for all events,
cleaning up some shutdown-hook related code on the way.
(A few drivers related to sparc are still skipped at kettenis' request)
ok kettenis mlarkin, tested by many others too


# 1.243 18-Apr-2013 deraadt

pin the acpi thread to the BSP, because AML and SMI cannot be trusted. I
was certain we were doing this already, but it turns out no...


Revision tags: OPENBSD_5_3_BASE
# 1.242 24-Dec-2012 guenther

In acpiioctl(), Return though the splx() path if suser() returns an error

ok deraadt@


# 1.241 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


# 1.240 04-Oct-2012 ehrhardt

Use information provided by ACPI to attach secondary PCI host bridges.

ok kettenis@


# 1.239 07-Sep-2012 kettenis

Add a new function called acpi_pci_min_powerstate() that returns the lowest
power state for a PCI device for the power state (currently S3 or S4) we're
transitioning into. This function is probably too simplistic; there is a lot
of (silly) complexity in the ACPI spec.

ok deraadt@


Revision tags: OPENBSD_5_2_BASE
# 1.238 13-Jul-2012 pirofti

Disable acpivideo(4) on ASUS laptops.

There are some models that panic whenever an event is received.
Some cope, some don't. In order to get this right acpiec(4) requires
major rework. Until then let machines boot.

Discussed with deraadt@.


# 1.237 11-Jul-2012 mlarkin

for now, restrict hibernate to root in the acpi ioctl kernel interface.
normal rules for controlling this interface through apmd still apply.

ok deraadt@


# 1.236 09-Jul-2012 deraadt

tiny little oops in untested hibernate block


# 1.235 09-Jul-2012 deraadt

clean up ioctl() case statements


# 1.234 09-Jul-2012 deraadt

acpi_disable_onegpe() is not used; ok pirofti


# 1.233 24-May-2012 kettenis

If _MIN > _MAX consider a WordBus resource to be invalid. Fixes a panic on
the IBM x3550-M4.

ok mikeb@


# 1.232 29-Mar-2012 mlarkin

Missing #ifdef in previous diff


# 1.231 29-Mar-2012 mlarkin

Prevent hibernation attempts on machines with unsupported disk controllers.
The same check is also performed later in the hibernate sequence, but by
then it's too late to cleanly unwind (presently).

ok deraadt


# 1.230 26-Mar-2012 deraadt

simplify conditionals in acpi_sleep_state()
ok mlarkin


# 1.229 26-Mar-2012 deraadt

hook in the hibernate request code; half of this diff is from mlarkin
ok mlarkin


Revision tags: OPENBSD_5_1_BASE
# 1.228 20-Sep-2011 deraadt

Very early in suspend while the clock is still ticking realtime,
save the clock back to the rtc. The zaurus already did this.
ok phessler miod kettenis


Revision tags: OPENBSD_5_0_BASE
# 1.227 02-Jul-2011 nicm

kqueue attach functions should return an errno or 0, not a plain 1. Fix
the obvious cases to return EINVAL and ENXIO.

ok tedu deraadt


# 1.226 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.225 16-Jun-2011 pirofti

Import acpitoshiba driver.

This is originally written by Hiroyuki Aizu, ported and integrated
in our tree by Javier Vazquez <javama4 AT gmail> with tweaks and
suggestions by me.

Import prodded by deraadt@ and the lack of testers on tech@.


# 1.224 27-Apr-2011 jcs

attach acpithinkpad to newer lenovo models like the x120e

ok deraadt@


# 1.223 22-Apr-2011 jordan

Fix uninitialzied variables and formatting strings (-Wxxx errors)


Revision tags: OPENBSD_4_9_BASE
# 1.222 02-Jan-2011 jordan

Re-add code for ACPI taskqueue, eliminates need for state vars in acpi softc
Serialize processing ACPI tasks for sleep/powerdown/GPE event
ok deraadt@


# 1.221 31-Oct-2010 guenther

Revert last commit: it breaks resume on Thinkpads
"then please back it out" deraadt@


# 1.220 26-Oct-2010 jordan

Add task queue for ACPI gpe and notify handlers
This fixes eject on Dell Latitude dock and an issue on the Dell Mini
battery update.
ok deraadt


# 1.219 07-Oct-2010 jordan

Create separate functions for enabling wake and runtime gpes.
Remove old suspend/resume gpewalk function
ok deraadt, mlarkin


# 1.218 05-Oct-2010 jordan

split acpi_enable_onegpe to separate enable/disable functions
ok deraadt


# 1.217 05-Oct-2010 jordan

Added acpi_disable_allgpes function to clear GPE status
ok deraadt


# 1.216 05-Oct-2010 deraadt

clean up acpi_softc use more


# 1.215 05-Oct-2010 deraadt

delete a bit of #if 0 code


# 1.214 05-Oct-2010 deraadt

hide use of our global acpi_softc pointer a bit better


# 1.213 29-Sep-2010 deraadt

grab the acpi thread lock very early in acpi thread startup; not that it
is likely that anyone will race us... but why be careless.
ok kettenis


# 1.212 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.211 29-Aug-2010 jasper

- add a check for FWRITE here too.

ok miod@


# 1.210 27-Aug-2010 kettenis

Make PCI Power Management optional, and only enable it when acpi(4) attaches.

ok deraadt@


Revision tags: OPENBSD_4_8_BASE
# 1.209 08-Aug-2010 kettenis

Close race between the acpi thread and wscons ioctls running in process
context. We might rip this out post-release and replace it with a solution
that serializes things in a more obvious way.

ok deraadt@, miod@ and "those crazy texans who ok anything"


# 1.208 08-Aug-2010 deraadt

Report the acpi sleep states when on a RAMDISK; ok krw


# 1.207 07-Aug-2010 kettenis

Enforce acpi register access rescrictions.

ok mlarkin@, deraadt@, tested by marco@


# 1.206 07-Aug-2010 deraadt

upon resume, notify all the acpiac and acpibat drivers to update
their status. some laptops do not do this automatically.
ok kettenis canacar


# 1.205 06-Aug-2010 marco

Always call _PSW on _LID devices that support it to enable lid open to
resume the machine. Conversely disable it when the machine wakes up.

Tested by several ok deraadt


# 1.204 06-Aug-2010 deraadt

As part of a revamp of the PM1/GPE code, I write this diff to look for
unmanaged EN & STS bits in the PM1 register at interrupt time and report
them. As a side effect this splits the STS acknowledgement into two writes
(for power, and sleep) instead of one. The printf that is added (to spot
unmanaged STS bits) has not yet been triggered as far as we know.

Before the "write to PM1 registers at the right offset" diff went in, this
was not neccessary. But newer thinkpads do not have a working soft power
button without this diff.

We have no idea why.
ok mlarkin kettenis


# 1.203 05-Aug-2010 deraadt

PM1 power button events were generating 'fake events' to acpibtn PWRB,
but if there is no such device these were being tossed. Instead, arrange
for all power button handlers to flag a softc variable which is processed
by the thread; so that one place will know to request a powerdown (psignal
to init)
ok marco mlarkin


# 1.202 05-Aug-2010 deraadt

rename a variable which is annoyingly poorly named


# 1.201 05-Aug-2010 mlarkin

Write to the correct offset for various ACPI pmreg registers. Writing to
the correct register does properly clear the wake status bits and fixed
event bits, which allows some machines to have more than 1 suspend cycle.

bug spotted by deraadt@

tested by deraadt@ and myself on a half-dozen machines and others on
hackers@

ok deraadt@, marco@


# 1.200 05-Aug-2010 deraadt

Fix the power button. The pm1 register is 16 bits in size.
My mistake, a while back.
tested by krw


# 1.199 04-Aug-2010 kettenis

Print supported sleep states.

ok marco@, phessler@, deraadt@


# 1.198 03-Aug-2010 mlarkin

Use the proper flag for re-enabling certain hardware events (power btn,
sleep btn). The (incorrect) flag prevoiously used coincidentally had the
same value, so this shouldn't cause any different behavior than before.

tested on a variety of machines (i386, amd64, sp, mp) - no behavioral
change seen

ok deraadt@


# 1.197 28-Jul-2010 marco

Make all gasio failures look and behave the same.


# 1.196 28-Jul-2010 mlarkin

Delay a little longer on suspend before giving up. Fixes machines that
sometimes don't suspend because the sleep transition takes too long
(symptoms like failing to sleep but the backlight stays on).

ok deraadt@


# 1.195 27-Jul-2010 deraadt

remove XXX; EC can now be attached early when needed; ok jordan


# 1.194 27-Jul-2010 deraadt

more brackets feels good


# 1.193 27-Jul-2010 deraadt

XXX annotate a block in acpi_enter_sleep_state() that is pretty worrying


# 1.192 27-Jul-2010 deraadt

horrific indentation that nearly made me puke


# 1.191 27-Jul-2010 mlarkin

Set the sleeping indicator light on machines that support it, in case SMI
doesn't do it for us. Shows the 'moon' LED on older Thinkpads when
sleeping.

Tested on T43p, W500, T510 by me and on X61s by deraadt@.

ok deraadt@


# 1.190 27-Jul-2010 jordan

Early initialization of acpiec if ECDT table exists
Fixes hang when booting thinkpads while docked
ok deraadt


# 1.189 26-Jul-2010 deraadt

remove merge error which was commited


# 1.188 26-Jul-2010 pirofti

Add support for Sony ACPI hotkeys via a new driver: acpisony(4).

Currently it only works for the suspend button and tries to do right for
the brightness events, but I haven't found a Sony laptop that like to do
right so far.

In the future I want to make the brightness keys work on all Sony's and
also add support for the zoom hotkeys and whatever other funky keys I can
find on those things.

Okay deraadt@.


# 1.187 22-Jul-2010 deraadt

pretty large cleanup of SMALL_KERNEL


# 1.186 21-Jul-2010 deraadt

in the acpi GPE handler, late ack level interrupts not edge interrupts.
Duh.


# 1.185 21-Jul-2010 deraadt

Merge the edge and level gpe handlers into one (now possible since the
gpe structure tells us whether the gpe is edge or level)


# 1.184 21-Jul-2010 deraadt

delete #if 0 code we are not going to use; ok marco


# 1.183 20-Jul-2010 deraadt

make the mask code for acpi_enable_onegpe() match other similar code


# 1.182 20-Jul-2010 deraadt

Handle the acpi interrupt controller with proper edge/level handling,
wrapped in spltty to avoid racing against the interrupt controller.
Repair the gpe bit masking code while there, and do operations in the
right order
ok kettenis mlarkin, help from jordan at figuring out the order of
operations.


# 1.181 20-Jul-2010 matthew

When we're preparing to go to sleep, only call bufq_restart() if other
preparations failed.

ok kettenis@, thib@, deraadt@


# 1.180 19-Jul-2010 deraadt

permit compilation with ACPI_DEBUG


# 1.179 19-Jul-2010 deraadt

there is no need for a seperate dsdt_softc pointer; dsdt does not have a
softc but is just a part of the single acpi driver.
ok jordan


# 1.178 19-Jul-2010 deraadt

acpi_delay() is no longer needed; ok jordan


# 1.177 18-Jul-2010 mlarkin

Clear event status on resume, to avoid going back to sleep/reboot/shutdown
immediately after resume. Tested by a bunch of people on hackers@ on
various hardware. Fixes at least Toshiba NB200 and Gateway LT3103u.

ok marco


# 1.176 13-Jul-2010 deraadt

rename some thread related interfaces so they are logical


# 1.175 10-Jul-2010 jordan

Verify _ADR bdf is correct (some systems return 0xffff)
Works on Dell x9xx and Studio 1555


# 1.174 09-Jul-2010 jordan

Clear the noise


# 1.173 08-Jul-2010 jordan

Add mapping for ACPI device to PCI bdf (match autoconf tree)
Simplify resource parsing function to use buffer argument
Convert namespace linked lists to use queue macros
ok marco@, deraadt@


# 1.172 06-Jul-2010 deraadt

Use spltty() to lock downcalls from apm against the information being
modified by the acpi thread.
ok kettenis


# 1.171 06-Jul-2010 deraadt

Don't use a workq to do the suspend, because it races aginst the acpi
thread. Instead, just tell the acpi thread to do the suspend for us.
This makes apmd & zzz work correctly. While here, have acpithinkpad
attempt to post the event to apm before prompting the suspend itself.
ok kettenis marco mlarkin


# 1.170 05-Jul-2010 mlarkin

Reset register SCI_EN on resume. The ACPI spec clearly states we don't have
to do this, but many machines don't, or forget to, which leads to all sorts
of problems on resume (broken battery stats, overheating, failure to resume
more than once, etc).

tested by many, ok marco@, deraadt@


# 1.169 01-Jul-2010 thib

do bufq quiesce again, since vnd/the bufq_done call in
biodone has been fixed.


# 1.168 01-Jul-2010 jordan

Backout recent AML changes, breaks on R210 and others


# 1.167 01-Jul-2010 jordan

Simplified aml_parse_resource call, cleanup code
ok marco


# 1.166 01-Jul-2010 jordan

Fix subordinate bus # for multi-root PCI buses
ok marco, kettenis


# 1.165 30-Jun-2010 thib

Disable/partially backout the bufq quiesce changes as this
is causing havoc with vnds and release must be buildable.


# 1.164 29-Jun-2010 deraadt

Fix ramdisks (new code was placed inside a SMALL_KERNEL chunk)


# 1.163 29-Jun-2010 jordan

Add support for mapping ACPI to PCI devices
ok kettenis, deraadt


# 1.162 29-Jun-2010 kettenis

Call bufq_quiesce() on suspend to stop all I/O and bufq_restart on resume.
Makes suspend/resume work much better while doing disk I/O.

ok mlarkin@


# 1.161 29-Jun-2010 tedu

60 is a more reasonable guess for number of minutes in an hour than 100.
spotted by TeXitoi. ok deraadt


# 1.160 27-Jun-2010 jordan

Add _acpi_matchhids routine, precursor to pci device match


# 1.159 27-Jun-2010 mlarkin

Clear ACPI fixed event status on resume (power buttons/etc) so that some
machines don't immediately resume after suspending to S3.

Tested by kettenis@ and I on 5 or 6 machines, deraadt@ "yeah, looks good"


# 1.158 27-Jun-2010 jordan

Cleaned up acpi_maptable, adds to table list directly


# 1.157 07-Apr-2010 deraadt

If we have done a wsdisplay_suspend(), and ACPI decides it does not want to
suspend, we must roll back by calling wsdisplay_resume() or the virtual
console switching will be locked forever
ok oga


# 1.156 07-Apr-2010 kettenis

Make suspend/resume work on MP machines (running an MP kernel). Joint work
from mlarkin@ and me, with some amd64 fixes thrown in by deraadt@

ok marco@, deraadt@, pirofti@, mlarkin@


# 1.155 31-Mar-2010 kettenis

Bring /dev/apm support in line with apm(4). Make the suspend button send
a suspend request event to apmd(8) instead of suspending immediately. Also
keep track of whether /dev/apm and /dev/apmctl are currently open, such that
we can still suspend immediately if apmd(8) isn't running.

ok deraadt@, marco@, pirofti@, jsing@, oga@


# 1.154 30-Mar-2010 oga

Prevent the apmd/x races for good.

When we hit suspend time, go through all wsdisplays on the system. if
they are in mode MAPPED, but not MODE_DUMBFB then if possible do a full
vt switch to a !mapped vt, and prevent switching back until resume time.
This has to be called from MD code because this involves userland
running so that X can run the vt switch signal handler. This way, any
case where we are using the "poke registers from userland" model, we
will not be on the hardware when we go down, so the kernel can actually
handle thing properly.

Tested on several acpi laptops (by kettenis@ and ian@), x40 (me and
beck@ at LEAST) and zaurus (me). Maybe others, but if so I forgot who at
this time..

Idea from deraadt somewhere over the Faroe Islands (I thought of a
similar thing myself a while ago). Much prompting from him. Ok and
comments miod@


# 1.153 25-Mar-2010 oga

Fix & vs. && in the kqfilter code.

Based on a comparison with the apm code.

ok deraadt@, kettenis@


Revision tags: OPENBSD_4_7_BASE
# 1.152 26-Nov-2009 mlarkin

Visual (and on some machines, audio) feedback while the machine is
resuming via calls to aml _SST method. This also makes the "moon" LED on
thinkpads to blink during resume and cease when resume is completed.

ok pirofti@


# 1.151 26-Nov-2009 deraadt

Toggle into "cold" when we we disable interrupts, because tsleeping
in the AML interpreter and context switching into userland processes
tends to sometimes pop the ACPI trampoline page out of our address
space. Makes lots more machines work.
ok kettenis mlarkin


# 1.150 24-Nov-2009 jsg

in the resume path evaluate _WAK before dealing with device drivers
like the spec says, not after.

ok mlarkin@


# 1.149 24-Nov-2009 deraadt

abort suspends on SMP systems until that gets written; ok mlarkin


# 1.148 23-Nov-2009 mlarkin

pci should return the result of it's children's suspend/resume calls back
to its parent.

handle suspend failure case and unwind if devices have an activate function
that returns failure.

ok deraadt@, kettenis@


# 1.147 23-Nov-2009 mlarkin

acpi* devices need to call their children on suspend/resume.
ok deraadt@


# 1.146 23-Nov-2009 pirofti

Remove ACPI_SLEEP_ENABLED checks.

This enables by default the suspend/resume paths in the kernel.

Okay deraadt@.


# 1.145 23-Nov-2009 deraadt

knf and spacing that are super annoying


# 1.144 23-Nov-2009 mlarkin

Hook up Theo's new children suspend/resume function, as well as more ACPI
suspend/resume glue.

ok deraadt@


# 1.143 26-Oct-2009 deraadt

Do not do strncmp comparisons on dv_xname because longer device names which
look similar could arrive in the future. Instead, compare directly against
dv_cfdata->cf_driver->cd_name
Issue originally spotted by miod


# 1.142 26-Oct-2009 jordan

Verify checksum+address when loading ACPI tables.
Some systems had invalid entries in RSDT/XSDT.
ok marco@


# 1.141 23-Jul-2009 cnst

New aibs(4) driver for ASUSTeK AI Booster (ACPI ATK0110) hardware monitoring,
with sensor state support through limits provided by the ACPI.

Tested on several ASUS motherboards kindly networked by Sam Fourman Jr.

ok deraadt marco jordan


Revision tags: OPENBSD_4_6_BASE
# 1.140 03-Jun-2009 pirofti

Import acpi power resource driver. This is needed for suspend/resume logic.

Okay jordan@ and marco@.


# 1.139 03-Jun-2009 pirofti

Add acpivideo support. This adds brightness support for all laptops
except thinkpads, they will use the acpithinkpad driver. The driver is
also hooked into wsconsole. So brightness can be adjusted via:

$ wsconsctl display.brightness=<percentage>

This is very helpfull on some laptops that have a nasty bios and get two
steps instead of one when pressing the brightness button.

Tested on various dell, fujitsu, acer, samsung and other laptops.

Okay marco@, miod@. Suggestions from kettenis@.
Lots of reviews and help from miod@, thanks!


# 1.138 03-Jun-2009 jordan

Added ejectable bay for IDE cdroms
ok marco@


# 1.137 30-Apr-2009 marco

Oga changed apm ioctls and forgot to grep. Temporary work around by hitting
the sleep button when we get any request. This interface will chnage at
some point.
ok oga


# 1.136 19-Apr-2009 krw

Call acpi_sleep_walk() when powering down just like when entering
any other sleep state. This tells acpi wake devices to not wake up
the box until manual powering up resets their status. Makes my Dell
GX520 stay powered off in response to 'halt -p'.

In snaps for a while.

ok marco@ (several times) deraadt@


# 1.135 17-Apr-2009 pirofti

Add device ranking support for future sleep/wake developments.

okay marco@


# 1.134 11-Mar-2009 jordan

Cleanup.. use aml_evalinteger instead of aml_evalname
ok marco@


# 1.133 10-Mar-2009 jordan

Fix cut/paste error.. call _BFS not _PTS
ok marco@


Revision tags: OPENBSD_4_5_BASE
# 1.132 21-Feb-2009 marco

overzealous cut/paste; found by Vladimir Kirillov <proger@uaoug.org.ua>


# 1.131 20-Feb-2009 marco

Oops didn't mean for Debugger() to remain there.


# 1.130 19-Feb-2009 marco

suspend/resume bits so that we can develop this in tree. This is disabled.
code from mlarkin and me
help from art,toby,jordan and several others
ok jordan, go for it deraadt


# 1.129 10-Feb-2009 jordan

Fix null pointer for shutdown if acpi not enabled
ok marco@


# 1.128 04-Feb-2009 kettenis

Some systems that claim to be ACPI 2.0 apparently don't provide an XSDT
address. Make the code fall back on the RSDT on those systems.

From Joerg Niendorf <p3y80@internode.on.net>. Fixes PR 5986.


# 1.127 17-Jan-2009 krw

A couple of stray malloc/memset -> malloc(M_ZERO), a bit of knf and
lose a couple of unnecessary casts. No functional change.

Feedback from kettenis@

ok kettenis@ marco@


# 1.126 06-Nov-2008 marco

Collapse all sanity checks of hids into one function.

Originaly from jordan


Revision tags: OPENBSD_4_4_BASE
# 1.125 02-Jul-2008 fgsch

add acpivideo. for now almost an empty skel so others can work on
suspend/resume. eventually it will also manage output switching and
brightness where supported. prodded by marco@


# 1.124 11-Jun-2008 marco

Fix $OpenBSD$ for once and for all


# 1.123 08-Jun-2008 deraadt

aaa_dev was being set from an uninitialized variable; ok toby


# 1.122 07-Jun-2008 marco

Kill ACPIVERBOSE. It was never used and is worthless to boot.

comments jmc
ok toby


# 1.121 06-Jun-2008 marco

Shrink acpi some more for floppies and while here re-enable acpi on the
floppies. Fixes crazy sony vaio kettenis has.

tested by deraadt
ok deraadt jordan kettenis


# 1.120 01-Jun-2008 marco

Fix \n nit in SMALL_KERNEL.


# 1.119 01-Jun-2008 marco

Re-enable new parser. Lots of testing by various folks. Fits all media
now too.

ok kettenis deraadt


# 1.118 16-May-2008 dlg

back out jordans new parser.

it didnt have enough oks from the right people, it did break machines
that were previously working, and it bloated the RAMDISKS and install
media beyond the constraints of the media.

requested by deraadt@


# 1.117 14-May-2008 jordan

Adding new ACPI Parser code
Old guts of code still exists, needs to be torn out next
ok marco@
ok brad@


# 1.116 27-Apr-2008 jcs

add acpithinkpad(4), an acpi support driver for ibm/lenovo
thinkpads.

provides functionality that the bios did on older, non-acpi
thinkpads like respond to screen brightness keys and toggle the
bluetooth device on and off.

ok deraadt@ and marco@


# 1.115 24-Apr-2008 jsing

Introduce acpiasus(4), a driver for the ACPI based hotkeys found in many
ASUS laptops (including the ASUS EeePC) - largely based on NetBSD's
asus(4) driver. On the ASUS EeePC this allows us to enable/disable
wireless, change screen brightness and use the volume keys.

ok jsg@, weingart@


# 1.114 12-Apr-2008 kettenis

Get rid of acpi_s5 global variables; simply send SIGUSR2 instead of SUGUSR1
to tell init(8) to power down the machine.

ok krw@


Revision tags: OPENBSD_4_3_BASE
# 1.113 21-Jan-2008 jsg

Check against RESET_REG_SUP in FADT before doing the acpi reset dance
after all, not doing so appears to break more machines than it fixes.

ok marco@


# 1.112 05-Jan-2008 kettenis

Never attach more acpicpu(4) devices than cpu(4) devices. Always attach
the first acpicpu(4) device on non-MULTIPROCESSOR kernels, regardless of its
ACPI processor ID.

ok mikeb@


# 1.111 05-Dec-2007 deraadt

Hang acpi(4) below bios(4) instead of mainbus(4). This lets us move acpi(4)
before pcibios(4), and then.... if acpi(4) attaches, skip pcibios(4) since
it messes the machines up. Fixes claudio's HP dl320 G5, and almost assuredly
others.
ok kettenis


# 1.110 05-Dec-2007 deraadt

repair acpiopen/acpiclose and friends to handle the /dev/apmctl interface,
so that apmd(8) can actually work correctly against acpi(4), and thus,
even do the fancy -C/-A stuff; also tested by jmc


# 1.109 02-Dec-2007 fgsch

fix the trailing space for real now.


# 1.108 02-Dec-2007 fgsch

avoid trailing spaces on output; marco@ ok.


# 1.107 27-Nov-2007 kettenis

Fix off-by-one that made us attach acpicpu0 and acpicpu1 on GENERIC kernels.

ok marco@


# 1.106 25-Nov-2007 jsg

When available and ACPI is enabled use the ACPI reset method
to reboot. This is required for some quirky sis machines
and likely others to reboot.

Tested by many. 'commit early commit often' deraadt@


# 1.105 19-Nov-2007 deraadt

sigh, amd64 still uses that stupid acpi_enabled variable...


# 1.104 19-Nov-2007 deraadt

softc's come initialized to 0, you know...


# 1.103 19-Nov-2007 deraadt

store revision; from jsg


# 1.102 19-Nov-2007 kettenis

Initialize sc_facs earlier.

ok deraadt@, beck@, canacar@


# 1.101 16-Nov-2007 deraadt

fix the bus_space #define nightmare, so that amd64 and i386 are much more
uniform. as a result shared code like acpi needs less #ifdef's
ok marco kettenis


# 1.100 15-Nov-2007 deraadt

remove the #ifdef ACPI_ENABLE. the EC codepath is either safe enough (and
will be improved), or it is unsafe because AML assumes it must be there
ok almost everyone


# 1.99 15-Nov-2007 mikeb

Make acpicpu(4) attach to the existing CPUs only.

Problem reported by form@. Fix idea by kettenis@.
Lots of help from deraadt@. Tested by deraadt@, form@ and me.

ok deraadt marco


# 1.98 14-Nov-2007 deraadt

spacing


# 1.97 12-Nov-2007 deraadt

correct : vs , in attach-time messages


# 1.96 06-Nov-2007 deraadt

all ACPI_ENABLE kernels need acpi_interrupt()


# 1.95 06-Nov-2007 deraadt

move acpi_read_pmreg() and acpi_write_pmreg() out of SMALL_KERNEL so
that acpi ramdisks can compile (obvious solution)


# 1.94 05-Nov-2007 canacar

Establish the acpi interrupt earlier. Fixes problems with some
GENERIC.MP kernels not getting acpi interrupts. This is a
workaround until the real issue with late establishment is fixed.
ok weingart@, beck@, gwk@


# 1.93 03-Nov-2007 jordan

Added support for displaying wakeup devices
ok beck@,weingart@,gwk@


# 1.92 11-Oct-2007 kettenis

When walking the tree yo find _PRT methods, check whether the device is
actually enabled and functioning. Similar to what we do for _INI.

ok weingart@, beck@


# 1.91 08-Oct-2007 krw

More simple memset(,0,) -> M_ZERO changes. In this batch move to
size(*p) as the first malloc() parameter where p is declared locally
and thus easy to check. Add M_ZERO to gpe_table allocation in acpi.c
even though there is no obvious bzero or memset nearby.


# 1.90 13-Sep-2007 mk

Remove unnecessary memset() (see aml_evalnode(9)) and a bit of KNF.

ok toby


# 1.89 13-Sep-2007 weingart

Implement ACPI 6.5.1 spec tree walk for _STA and _INI.
Fixes mk's laptop. No regressions so far (thank you to the
testers).

ok gwk@, mk@, marco@


Revision tags: OPENBSD_4_2_BASE
# 1.88 17-Apr-2007 mk

Now that acpidock no longer hangs machines during boot (at least
according to the reports I've been getting) it should be safe to enable
it again, so remove the #if 0 stuff around it.

ok marco


# 1.87 11-Apr-2007 jordan

Added changes for C-State
ok marco@


# 1.86 26-Mar-2007 marco

Calculate minutes left more accurately.

From giovanni <qgiovanni@gmail.com>


Revision tags: OPENBSD_4_1_BASE
# 1.85 03-Mar-2007 mk

Stop looking for acpidock until we fix that stuff that breaks all pre-60
series ThinkPads with acpidock(4) enabled.


# 1.84 22-Feb-2007 marco

We were getting no interrupts in .MP. There are 2 fixes; one in
acpi_machdep.c and move the interrupt establishment down after ioapic
has been initialized.

ok jordan


# 1.83 21-Feb-2007 kettenis

Make an acpi-enabled GENERIC.MP work on systems that only support PIC mode.

ok marco@


# 1.82 19-Feb-2007 jordan

Added acpi_map_address function for mapping GAS/IO registers
ok marco@


# 1.81 18-Feb-2007 jordan

Added dynamic perfect hash for opcodes
ok marco@


# 1.80 18-Feb-2007 marco

Shave off another K pre compression.
Shuffle SMALL_KERNEL functions around so that we only have a single #ifndef


# 1.79 17-Feb-2007 marco

Add SMALL_KERNEL throught acpi to make it fit on boot media.
ok deraadt


# 1.78 13-Feb-2007 marco

Add smarter _INI code. This helps with some dissapearing devices.

ok kettenis jordan


# 1.77 31-Jan-2007 gwk

Introduce acpi_hasprocfvs thats "ACPI Has Processor Frequency and Voltage
Scalling" a global flag set by acicpu attach to indicate that ACPI is
handling fvs through the _PSS objects. This will fix disappearing
EST/powernow when booting kernels with acpi enabled, as acpicpu isnt
compiled into GENERIC yet. Also in cases where acpicpu is built in and
legacy EST/powernow work but acpi is missing the _PSS object, the legacy
routines will be used.

tested by dim@, and Nick Nauwelaerts <nick AT nauwelaerts.net>
ok marco@, dim@


# 1.76 22-Jan-2007 mk

Actually look for docking stations by looking for _DCK nodes.

ok marco


# 1.75 02-Jan-2007 marco

On some systems EC must be attached as the first device. Move EC from
generic _HID devices to its own routine to force first attach. Bug found
and debugged by joris@

Code mostly written by joris@


# 1.74 26-Dec-2006 marco

Add polling to devices that require it such as acpibat and acpitz.
Use the same tsleep/wakeup mechanism as the interrupt code to keep it non-
concurrent.


# 1.73 21-Dec-2006 deraadt

even more knf love


# 1.72 21-Dec-2006 marco

Remove some leftover acpiec gpe/interrupt code.


# 1.71 20-Dec-2006 deraadt

oops, name already used


# 1.70 20-Dec-2006 deraadt

rename __acpi_enable_gpe(); ok marco


# 1.69 20-Dec-2006 deraadt

acpi_unmap_pmregs() is not used, ok marco


# 1.68 19-Dec-2006 deraadt

simplify some low-level methods substantially (shrinking code); ok marco jordan


# 1.67 06-Dec-2006 mk

malloc(..., M_NOWAIT) with no NULL check is bad. If allocation fails,
complain, clean up, and bail out of attach.

input and ok gwk


# 1.66 28-Nov-2006 jordan

Fixed clearing arg for acpi_init_pic
ok kettenis@


# 1.65 27-Nov-2006 kettenis

Don't spam dmesg.


# 1.64 27-Nov-2006 jordan

Fixes for acpiprt routing
Post-parse fixup for namerefs
ok kettenis@


# 1.63 25-Nov-2006 marco

Don't handle acpiev events inside the gpe handler since this causes deadlocks.

ok jordan@


# 1.62 15-Nov-2006 kettenis

Hook up acpi apic interrupt routing support to generic acpi code.

ok jordan@


# 1.61 02-Nov-2006 marco

Kill acpiec attach function and add its functionality to the generic _HID
device attach

prodded and ok jordan


# 1.60 02-Nov-2006 marco

Fix comments to reflect reality.


# 1.59 19-Oct-2006 marco

Replace ACPI ioctls with APM ioctls. This makes ACPI talk the same protocol
as APM therefore userspace can now retrieve power information in the same
fashion as all other apm arches (zaurus, macppc, i386 etc).

To use force the use of acpi device like so: apmd -f /dev/acpi

ok jordan@


# 1.58 19-Oct-2006 jordan

Fixed GPE debug levels
Fixed ref counter on temporary bufferfields
DSDT parsing fully working on IBM Thinkpad
ok marco@


# 1.57 15-Oct-2006 jordan

Removed old GPE handler code remnants
ok marco@


# 1.56 12-Oct-2006 jordan

Massive ACPI changes
Fixed ACPI leaks
new ACPI API
ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.55 30-Jun-2006 gwk

KNF, No functional change,
"go at it" marco@


# 1.54 30-Jun-2006 jordan

Support for calling Embedded Controller via new GPE interface
ok marco@


# 1.53 30-Jun-2006 jordan

Added new code for GPE handling
Also works with ACPIEC; need to fixup acpiec.c to remove original interrupt handler
ok marco@


# 1.52 30-Jun-2006 jordan

Added offset argument for acpi read/write pmreg routines (new GPE code)
ok marco@


# 1.51 31-May-2006 todd

do not call acpi_powerdown() unless acpi has successfully initialized
ok gwk@ canacar@


# 1.50 29-May-2006 canacar

Add embedded controller support.
ok marco@ deraadt@ testing & ok gwk@


# 1.49 19-May-2006 canacar

Minimal thermal zone driver. Monitors thermal zone temperature,
shuts down the system if the 'critical temperature' is reached.
ok marco@


# 1.48 11-Apr-2006 marco

KNF.


# 1.47 11-Apr-2006 gwk

Add acpi_resume(struct acpi_softc *sc), which calls some AML methods,
detected at attach time, currently unused.

ok marco@


# 1.46 11-Apr-2006 gwk

Discover some ACPI sleep related methods, and call them at the
appropriate time in acpi_enter_sleep_state().

ok marco@


Revision tags: OPENBSD_3_9_BASE
# 1.45 26-Feb-2006 marco

Add acpicpu as a device.

ok jordan@


# 1.44 23-Feb-2006 marco

remove dead function.

ok jordan@


# 1.43 22-Feb-2006 marco

Print out all tables it found during autoconf. We'll need this data in
the future.


# 1.42 22-Feb-2006 marco

Make dmesg less verbose.

ok jordan.


# 1.41 22-Feb-2006 jordan

Added parsing of SSDT tables
ok marco@


# 1.40 22-Feb-2006 jordan

Added new aml_register to notify by device id
Moved powerdown code to button device
ok marco@


# 1.39 21-Feb-2006 marco

Readjust debug levels that crept in too low over the last few days.
KNF


# 1.38 21-Feb-2006 marco

Rig the power button so that the machine shuts down correctly.


# 1.37 20-Feb-2006 marco

Add acpibtn device. Remains disabled for now.


# 1.36 19-Feb-2006 jordan

Added code for aml_notify GPE notification callback
ok marco@


# 1.35 19-Feb-2006 grange

Fix acpi_enter_sleep_state() by replacing current ugly hack
with mostly correct sleeping state transition code.

ok brad@ marco@


# 1.34 19-Feb-2006 marco

jordan:
Enable interrupts.
Added peripheral code to make this work.

marco:
Replace softintr and other types of interrupts with a handler with context.
Fixed interrupt magic for GPEs.
Some KNF

This is work done by jordan and I but has to be a single commit.


# 1.33 17-Feb-2006 marco

Remove earlier introduced delays. We really only need them during autconf.

ok jordan@


# 1.32 17-Feb-2006 marco

Add a generic delay function as per the ACPI spec.

from jordan@


# 1.31 17-Feb-2006 marco

Use tsleep instead of delay when !cold.


# 1.30 17-Feb-2006 marco

Argh, we need delays between reads/writes. The backend reads (i2c most likely)
are so slow that values come back as 0 sometimes. We have to do this
despite the spec saying we shouldnt have to. Guess having a faster
parser than others can hurt sometimes.

Idea from jordan.


# 1.29 17-Feb-2006 jordan

Fixed code for handling _INI
Rearranged debug statements
ok marco@


# 1.28 16-Feb-2006 jordan

Added stub for calling _INI
Fixed aml_dowhile for NULL return
ok marco@


# 1.27 16-Feb-2006 jordan

Fixed parser/evaluator to retrieve battery info+state
Cleanup of parser
Dell laptops return battery status
ok marco@


# 1.26 06-Feb-2006 brad

fix a typo so this will compile on amd64.


# 1.25 04-Feb-2006 marco

When ACPI_ENABLED wasn't set but the devices were it had an unknown variable.

Pointed out by ian@.


# 1.24 03-Feb-2006 jordan

Rewrote aml parser yet again...
Fixed bus_space_map for i386/amd64
Added _BST and _BIF to acpibat to get status/info
ok marco@


# 1.23 20-Jan-2006 jordan

More evaluator changes
Removed old evaluator code
Fixed _HID value
ok marco@


# 1.22 19-Jan-2006 jordan

Added support for read/write of PCI Config space
Help jason@
ok marco@


# 1.21 18-Jan-2006 jordan

Added new evaluation routines
Added helper methods for buffer/field evaluation
Fixed broken indentation on dsdt.c

ok marco@


# 1.20 17-Jan-2006 jordan

Major overhaul of the aml parser. Evaluater temporarily disabled.

ok marco@


# 1.19 06-Jan-2006 grange

Check for ACPI control availability only if ACPI_ENABLE defined.


# 1.18 06-Jan-2006 grange

Informative error messages on attach, prettier dmesg.


# 1.17 06-Jan-2006 grange

printf -> dnprintf conversion for debug messages.


# 1.16 05-Jan-2006 grange

Find SLP_TYPx values and store them for future use in
acpi_enter_sleep_state().


# 1.15 05-Jan-2006 grange

Random KNF and cleanup


# 1.14 05-Jan-2006 grange

Provide symbolic names for the acpi sleeping states and use it.


# 1.13 28-Dec-2005 marco

Moved DEVNAME to a acpidev.h

ok jordan@


# 1.12 18-Dec-2005 sturm

add back comment about wrong code

ok marco


# 1.11 16-Dec-2005 marco

Remove test for kbd_reset. Prompted deraadt@, ok jordan@


# 1.10 16-Dec-2005 jordan

Added hooks for powerdown/power button press


# 1.9 16-Dec-2005 jordan

Cleaned up common power management register i/o
Added enter sleep state ioctl
Hooked power button interrupt
ok marco@


# 1.8 16-Dec-2005 marco

Remove autoconf chatter


# 1.7 16-Dec-2005 jordan

Moved DSDT AML parsing into parent ACPI driver
ok marco@


# 1.6 14-Dec-2005 marco

Add AC device glue.


# 1.5 14-Dec-2005 marco

Adjust debug printing levels to something more sane.


# 1.4 13-Dec-2005 marco

Add glue to attach devices to apci.


# 1.3 07-Dec-2005 marco

KNF


Revision tags: OPENBSD_3_8_BASE
# 1.2 10-Jul-2005 grange

Spacing cleanup.


# 1.1 02-Jun-2005 tholo

Start on a basic ACPI framework -- does not do much more than read out the
ACPI tables into kernel memory and attach ACPI and HPET timers currently.

In order to test this code, enabling the devices in GENERIC as well as
the ACPI_ENABLE option is needed. This code does not do any thermal
control yet, so this should be done with care depending on the platform.

In the tree so more people can contribute to making this more fully
featured.

Ok niklas@ grange@ tedu@


# 1.388 24-Jun-2020 cheloha

kernel: use gettime(9)/getuptime(9) in lieu of time_second(9)/time_uptime(9)

time_second(9) and time_uptime(9) are widely used in the kernel to
quickly get the system UTC or system uptime as a time_t. However,
time_t is 64-bit everywhere, so it is not generally safe to use them
on 32-bit platforms: you have a split-read problem if your hardware
cannot perform atomic 64-bit reads.

This patch replaces time_second(9) with gettime(9), a safer successor
interface, throughout the kernel. Similarly, time_uptime(9) is replaced
with getuptime(9).

There is a performance cost on 32-bit platforms in exchange for
eliminating the split-read problem: instead of two register reads you
now have a lockless read loop to pull the values from the timehands.
This is really not *too* bad in the grand scheme of things, but
compared to what we were doing before it is several times slower.

There is no performance cost on 64-bit (__LP64__) platforms.

With input from visa@, dlg@, and tedu@.

Several bugs squashed by visa@.

ok kettenis@


# 1.387 10-Jun-2020 jca

Give userland a consistent battery and A/C state after resume

Previously we refreshed the state of acpiac(4) and acpibat(4)/acpisbs(4)
by queueing fake ACPI events to be processed by the acpi thread.
acpibat(4) using ACPIDEV_POLL meant that its status could be refreshed
first, resulting in an APM_POWER_CHANGE event sent to userland before
the state of acpiac(4) was up to date. Because of this, apmd(8) could
see A/C unplugged when it was in fact plugged, possibly leading to
a wrong decision.

Instead of using fake ACPI events, refresh the status of acpiac(4) and
acpibat(4)/acpisbs(4) right away at DVACT_WAKEUP time. To reuse the
existing refresh code of those drivers, make their ACPI *_notify()
handlers responsible for sending APM_POWER_CHANGE events instead. Also
queue an APM_POWER_CHANGE event right after resume to retain existing
behavior.

This fixes the consistency of data shown to userland, and makes those
drivers less special.

Initially discussed last year (!) with mpi@ at Miod's place.
Feedback and ok jcs@, deraadt@ agrees with the use of DVACT_WAKEUP here.


# 1.386 29-May-2020 deraadt

dev/rndvar.h no longer has statistical interfaces (removed during various
conversion steps). it only contains kernel prototypes for 4 interfaces,
all of which legitimately belong in sys/systm.h, which are already included
by all enqueue_randomness() users.


# 1.385 14-May-2020 kettenis

Bring back the acpi(4) changes. This time around only use the ACPI _CRS
information on ACPI 5.0 and later.

ok krw@, patrick@


# 1.384 11-May-2020 jca

More accurate remaining power reporting with disparate batteries

Overall remaining power was computed as the average of the remaining
power of each battery, in percents. This doesn't work with batteries of
different capacity, so use the total capacity and remaining power
instead.

Reported by lidstah, successful tests by lidstah and benno@, ok kettenis@


# 1.383 08-May-2020 kettenis

Move parsing of _CRS into acpi(4) and pass the parsed address and interrupt
information in struct acpi_attach_args.

ok mpi@


Revision tags: OPENBSD_6_7_BASE
# 1.382 14-Apr-2020 kettenis

Implement acpi_getprop(), the ACPI equivalent of OF_getprop().

ok patrick@


# 1.381 12-Apr-2020 kettenis

Add support for the _CCA method. This method indicates whether DMA is
cache-coherent or not. To implement this, acpi(4) gets two bus_dma tags
and passes the appropriate one when attaching devices based on _CCA.
On i386/amd64, where for all practical purpose DMA is always cache-coherent,
the two tags are the same. But on arm64 they are distinct.

ok patrick@


# 1.380 07-Apr-2020 visa

Abstract the head of knote lists. This allows extending the lists,
for example, with locking assertions.

OK mpi@, anton@


# 1.379 03-Apr-2020 mpi

Fix incoherencies of errors returned by various *kqfilter routines.

EOPNOTSUPP is returned when there's no "filterops" for a given fd.
EINVAL when the requested filter isn't supported by the "filterops".
ENXIO means the underlying device is no longer valid/present.

ok visa@


# 1.378 20-Feb-2020 visa

Replace field f_isfd with field f_flags in struct filterops to allow
adding more filter properties without cluttering the struct.

OK mpi@, anton@


# 1.377 31-Dec-2019 visa

Use C99 designated initializers with struct filterops. In addition,
make the structs const so that the data are put in .rodata.

OK mpi@, deraadt@, anton@, bluhm@


# 1.376 31-Dec-2019 mpi

Convert infinite sleeps to {m,t}sleep_nsec(9).

ok kn@


# 1.375 31-Dec-2019 jsg

include sys/sensors.h for sensor use
don't depend on indirect inclusion from acpidev.h


Revision tags: OPENBSD_6_6_BASE
# 1.374 07-Sep-2019 kettenis

Revert acpipci(4) changes. Too many systems are broken. We'll try again
after 6.6 as been released. The acpireg.h change stays behind.


# 1.373 28-Aug-2019 kettenis

Use ACPI information to attach PCI busses like we do on arm64. There are a
few additional quirks though, and attaching the PCI busses is delayed to
replicate the existing code more closely. That may be changed in the
future. Also tweak how we handle MSI support and respect to ACPI flag
that says we shouldn't attempt to use MSIs.

Some fallout is expected.

ok patrick@


# 1.372 27-Aug-2019 deraadt

On amd64 the bootup and resume paths for SP/BP/SP were not performing
msr/pat, clock-startup, firmware upgrade, fpu initialization, and cpu
soft-state initialization in the same order. That kind of chaos is
crazy and fragile. Improve the acpi resume call sequence and unify order
of the operations.


# 1.371 02-Jul-2019 jcs

acpi_set_gpehandler: turn edge arg into flags, add support for
passing GPE_DIRECT which directly executes the supplied callback and
clears a level interrupt rather than going through acpi_taskq which
can be slow when other driver callbacks are in the queue.

This should not be used for GPE handlers that need to do anything
ACPI-related.

ok kettenis


# 1.370 10-Jun-2019 kettenis

Print proper ACPI version number.

ok mlarkin@


# 1.369 08-Jun-2019 kettenis

When establishing the mapping between ACPI device nodes and PCI devices,
skip PCI host bridges and devices that aren't there.

ok patrick@


# 1.368 07-Jun-2019 kettenis

Introduce acpi_getsta() and use it to eliminate some duplicated code.

ok patrick@


# 1.367 12-May-2019 kettenis

Make sure the extended PM register descriptions actually exist before
using them. Fixes machines with ancient ACPI 1.0 (such as recent
hypervisors).

ok deraadt@


# 1.366 12-May-2019 kettenis

Add support for mapping ACPI PM registers using the "extended" gasio
representation from the FADT. Mostly fixes the Lanner NCA-1510.

ok lteo@, mlarkin@, deraadt@


# 1.365 11-May-2019 lteo

Fix incorrect error message.

ok kettenis@


# 1.364 08-May-2019 tedu

free size. the allocation is the header plus the length.


Revision tags: OPENBSD_6_5_BASE
# 1.363 04-Apr-2019 kettenis

Avoid potential null-pointer dereference. Found by Kent R. Spillner.

ok deraadt@, jsg@


# 1.362 20-Jan-2019 jsg

fix SMALL_KERNEL build after changes in rev 1.361
reported by naddy@ ok deraadt@


# 1.361 19-Jan-2019 tedu

Add a pwraction sysctl that controls what the power button does on acpi.
By default, nothing changes -- shutdown is initiated. But allows turning
power button into a sleep button if desired.
(grudging) ok from a few parties


# 1.360 26-Oct-2018 kettenis

Add acpipci(4) on amd64. For now this only calls the PCI-specific _OSC
method to let the ACPI implementation know what features we support.


Revision tags: OPENBSD_6_4_BASE
# 1.359 30-Aug-2018 kettenis

Move softbutton check before the check that skips attaching certain acpi
devices. Fixes regression caused by matching on _CID in addition to
matching on _HID.

ok matthieu@, mlarkin@


# 1.358 25-Aug-2018 kettenis

Match ACPI devices based on _CID if no match for _HID is found.

ok mpi@


# 1.357 19-Aug-2018 kettenis

Add support for multiple PCI segments. Only really implemented for arm64
for now as amd64/i386 firmware still caters for legacy OSes that only
support a single PCI segment.

ok patrick@


# 1.356 03-Aug-2018 kettenis

Let ahci(4) match on _CLS instead of _HID when attaching at acpi(4). Avoids
having to add many more _HID entries to the match table.

ok deraadt@, mlarkin@


# 1.355 10-Jul-2018 kettenis

Make legacy interrupts work in acpipci(4).

ok patrick@


# 1.354 05-Jul-2018 kettenis

Add acpipci(4), a driver that supports generic ECAM-compatible PCI host
bridges based on information provided by ACPI.

ok mlarkin@


# 1.353 04-Jul-2018 kettenis

Properly pass around the PCI "chipset tag" in acpi(4) and refactor
acpimcfg(4) to call an MD initialization functions that sets up a tag for
PCI ECAM.

ok guenther@, mlarkin@, krw@


# 1.352 01-Jul-2018 kettenis

Move acpi(4) attach glue into acpi_machdep.c.

ok guenther@, deraadt@


# 1.351 01-Jul-2018 kettenis

Add support for _DSD Device Properties.

ok mlarkin@


# 1.350 30-Jun-2018 kettenis

Add intr_enable() function, intended for MI use to amd64 and i386 and use
this in the acpi(4) suspend/resume code paths.

ok deraadt@


# 1.349 29-Jun-2018 kettenis

Use <stdint.h> types.

ok mpi@, jung@, krw@, deraadt@


# 1.348 29-Jun-2018 mlarkin

ACPI: Allow (with warning) GPE handler reassignment, instead of returning
a failure code.

ok kettenis


# 1.347 26-Jun-2018 mlarkin

knf


# 1.346 26-Jun-2018 kettenis

Let acpi(4) pass down the bus dma tag.

ok deraadt@


# 1.345 25-Jun-2018 kettenis

Reorganize acpi(4) code a bit in preparation for upcoming arm64 support.

ok deraadt@


# 1.344 20-May-2018 kettenis

Wake up the acpi thread after scheduling a gpio event.

ok mpi@


# 1.343 17-May-2018 kettenis

Sprinkle some #ifndef SMALL_KERNEL such that RAMDISK kernels cuild again.

pointed out by stsp@


# 1.342 17-May-2018 kettenis

Implement GenericSerialBus OpRegion support.

ok mlarkin@


# 1.341 27-Mar-2018 kettenis

Add acpicmos(4), a driver that implements SystemCMOS OperationRegion
access support. This fixes machines where the AML doesn't check whether
support for this OperationRegion type has been registered by the OS.

ok mlarkin@


Revision tags: OPENBSD_6_3_BASE
# 1.340 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.339 10-Feb-2018 deraadt

Syncronize filesystems to disk when suspending. Each mountpoint's vnodes
are pushed to disk. Dangling vnodes (unlinked files still in use) and
vnodes undergoing change by long-running syscalls are identified -- and
such filesystems are marked dirty on-disk while we are suspended (in case
power is lost, a fsck will be required). Filesystems without dangling or
busy vnodes are marked clean, resulting in faster boots following
"battery died" circumstances.
Tested by numerous developers, thanks for the feedback.


# 1.338 08-Feb-2018 deraadt

After flushing disks, try again to release all memory. We don't need to
save dirty memory to the hibernate space.


# 1.337 08-Feb-2018 deraadt

Toss all releaseable memory, because fragmentation can get in the way
of allocating the hibernate playpen.


# 1.336 08-Feb-2018 deraadt

sr_quiesce() is a new approach for ensuring that softraid drains
output to the disks.

This is part of a larger suspend/resume filesystem-safety diff, which
has been worked on for a couple of months already. Tests by job, krw,
beck, benno, and others. Sometimes even by snapshot users...


# 1.335 29-Nov-2017 kettenis

Add support for GPIO-signaled events.

ok mlarkin@


# 1.334 16-Nov-2017 jcs

Add PCI attachment for dwiic(4) needed by Intel 100 Series machines

ok kettenis


# 1.333 14-Nov-2017 kettenis

Add a few more PNP IDs to the skip lists.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.332 17-Aug-2017 stsp

Make dwiic(4) attach its ACPI parent dependencies.
This change makes the Asus E200HA keyboard work.

Original analysis and patch by Cesare Gargano
ok kettenis@


# 1.331 09-Aug-2017 dcoppa

Add "PNP0303" (8042 PS/2 Controller) to acpi_isa_hids[]

ok kettenis@


# 1.330 22-Jul-2017 jcs

acpibat: look for _BIX first before falling back on _BIF

ACPI 4.0 deprecated _BIF for battery status so some newer machines
are exporting _BIX which extends _BIF's package with a few extra
fields. When using _BIX, export the new discharge cycle count as a
sensor.

feedback from kettenis and anton
ok deraadt


# 1.329 20-Jul-2017 kettenis

Start a list of isa(4) device names such that we can avoid printing them
as "not configured".

ok mikeb@, millert@, deraadt@


# 1.328 08-Apr-2017 deraadt

Sensors are run as callbacks inside tasks. During suspend a sensor
could be running inside a driver that will be force-detached, or due
to tsleep end up disrupting the softstate/hardstate contract. At
suspend time, quisce all these callbacks by waiting for completion.
This issue has never been observed for real, but may be implicated in
suspend/resume failures.
ok kettenis guenther mlarkin


# 1.327 08-Apr-2017 deraadt

Two prototypes for acpi_maptable; merge to one place


Revision tags: OPENBSD_6_1_BASE
# 1.326 27-Mar-2017 deraadt

Now that hibernate_alloc() only has clean success/failure, don't
need to call hibernate_free() to clean up a partial mess.
ok mlarkin kettenis


# 1.325 13-Mar-2017 deraadt

allow ramdisks to build; ok jcs


# 1.324 12-Mar-2017 jcs

try to attach acpisbs first and if successful, don't attach acpibat


# 1.323 02-Mar-2017 natano

Add a new sysctl machdep.lidaction. The sysctl works as follows:

machdep.lidaction=0 # do nothing
machdep.lidaction=1 # suspend
machdep.lidaction=2 # hibernate

lidsuspend is just an alias for lidaction, so if you change one, the
other one will have the same value. The plan is to remove
machdep.lidsuspend eventually when people have upgraded their
/ets/sysctl.conf.

discussed with deraadt, who came up with the new MIB name
no objections mlarkin
ok stsp halex jcs


# 1.322 28-Feb-2017 natano

Untangle abstract sleep modes from ACPI sleep states. This paves the way
for further work in the area.

idea and ok deraadt
ok mlarkin


# 1.321 22-Feb-2017 jcs

add acpisbs, an acpi smart battery subsystem driver reading data
over smbus

currently disabled because it conflicts with acpibat

ok deraadt, kettenis


# 1.320 14-Jan-2017 kettenis

Revert aml_rdpciaddr changes; breaks several machines that were working
before.


# 1.319 11-Jan-2017 jsg

Increase the size of the buffers used for _HID and _CID strings to be
able to fit "Hyper_V_Gen_Counter_V1".

ok mlarkin@ deraadt@


# 1.318 08-Jan-2017 kettenis

Make aml_rdpciaddr() work for for devices behind bridges. The code now
uses the mapping between AML nodes and PCI devices that we establish.
Because _INIT methods may end up calling aml_rdpciaddr(), make sure we
create that mapping early. Also handle devices that aren't actually present.
These devices are now included in the mapping and reads will return an
all-ones patterm whereas writes are a no-op..

ok mlarkin@


# 1.317 25-Oct-2016 pirofti

Add the resource index to aml_parse_resource() callback function.

This is needed by an upcoming acpiec commit that handles machines breaking
the current ACPI specifications.

Change suggested by and ok kettenis@, guenther@


# 1.316 18-Sep-2016 guenther

If the APIC table (MADT) isn't present then ci_acpi_proc_id won't be set
when acpicpu tries to attach, leading to a NULL dereference. The ACPI
processor object on the Dells where this was reported is bogus and useless
anyway so apply the same test when deciding whether to attach to processor
objects as acpicpu would when matching them to cpus.

Problem reported by Eivind Eide (xenofil (at) gmail.com) and
Johan Huldtgren (johan+openbsd-bugs (at) huldtgren.com).
Testing by Eivind Eide.
ok kettenis@ mlarkin@


# 1.315 03-Sep-2016 naddy

Write the system time back to the RTC every 30 minutes.
This fixes the problem that long-running machines which were not
shut down properly would reboot with a badly offset system time.

hints and ok kettenis@


# 1.314 31-Aug-2016 mlarkin

Don't put a device in the wake device list if _STA says it's not there.

Fixes at least one machine that woke up from sleep / poweroff immediately.

ok kettenis@, tested by kettenis and martijn@


# 1.313 28-Jul-2016 kettenis

Store the acpi processor ID/UID in struct cpu_info, and use it to attach
acpicpu(4) drivers to the right cpu(4).

ok mlarkin@, guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.312 10-Jun-2016 kettenis

The power management timer has been made optional in ACPI 5.0A, so don't
attempt to attach acpitimer(4) if the timer isn't present.

Pointed out by jcs@

ok jcs@


# 1.311 08-May-2016 kettenis

Make sure devices listed by a device's _DEP method are attached before
we attach the device itself. The _DEP methode, introduced in ACPI 5.0,
is a hint that the device in question depends on OpRegion support from
the devices listed by _DEP, so we have to order them the proper way.
To prevent us from attaching those devices again when we encounter them
later walking down the device tree, keep track of our attempts to
attach a device driver to them using aflag in the aml_node structure.

ok guenther@


# 1.310 25-Apr-2016 pirofti

We attach acpihpet(4) and acpiec(4) without using the HID, so do not
report it in the dmesg.

OK deraadt@, mlarkin@.


# 1.309 02-Apr-2016 kettenis

Reduce dmesg spam by knocking out some acpi hardware IDs that don't need a
driver.

ok mpi@


# 1.308 29-Mar-2016 kettenis

Check if a device is present (using _STA) in acpi_foundhid. This prevents
us from reporting devices as "not configured" that aren't actually present,
and allows us to remove duplicated code from several drivers.

ok deraadt@, ok jsg@


# 1.307 29-Mar-2016 kettenis

Implement a cleaner way to prevent acpivout(4) from attaching if we have
a platform-specific method of controlling the backlight.

ok millert@, mlarkin@, jung@


# 1.306 28-Mar-2016 kettenis

Adding each and every new acpi device driver to acpi_foundhid() is
gettinga bit of the burden. And it really isn't how our autoconf
framework is supposed to work for busses that can be enumerated. All
the drivers already check for a matching _HID in their attach
function. So we can just drop the checks in acpi_foundhid() and let
the drivers sort themselves out.

Print a "not configured" line for devices for which we don't attach adriver.
This may be a bit much as there are devices for which we will not have a
driver. This will be removed if it causes too much dmesg spam.

ok deraadt@, mlarkin@


Revision tags: OPENBSD_5_9_BASE
# 1.305 17-Jan-2016 jsg

Take acpi_foundhid() outside of #ifndef SMALL_KERNEL so the ramdisk
kernel can find and attach dwiic(4) and sdhc(4).


# 1.304 17-Jan-2016 jsg

remove an unused global variable


# 1.303 14-Jan-2016 kettenis

Attach dwiic(4) on the Intel Bay Trail i2c controllers as well.


# 1.302 12-Jan-2016 kettenis

Improve emulation of PM registers on Hardware-redcued ACPI by emulating
SPL_TYPx, SLP_EN and WAK_STS using the new SLEEP_STATUS_REG and
SLEEP_CONTROL_REG registers. Makes the transition into S5 (powerdown) work
on the ASUS X205TA.

ok deraadt@, mlarkin@


# 1.301 12-Jan-2016 jcs

Add dwiic, a driver for the Synopsys DesignWare i2c controller found
on the Samsung ATIV Book 9 laptop. This initial version only
supports ACPI config/attachment.

Add ihidev, a HID-over-i2c driver largely based on uhidev. dwiic
handles attaching ihidev devices found in ACPI.

Add ims, a HID-over-i2c mouse/trackpad driver to get basic cursor
and button functionality from HID-compliant i2c trackpads.

ok kettenis deraadt


# 1.300 11-Jan-2016 kettenis

Make sdhc(4) attach to hardware IDs "80860F14" and "PNP0FFF".


# 1.299 09-Jan-2016 kettenis

Initial support for Hardware-Reduced ACPI. This skips the legacy mode checks
by emulating the PM_CNT register such that SCI_EN is always enabled. It also
skip attaching acpitimer(4) since the relevant registers are no longer there.

ok deraadt@, mlarkin@


# 1.298 06-Jan-2016 kettenis

Follow the spec and check the SCI_EN bit to see if ACPI has already
been enabled and don't bother enabling it again if it is. This should fix
systems that don't implement legacy mode and therefore set ACPI_ENABLE and
ACPI_DISABLE to zero.

ok mlarkin@, yasuoka@


# 1.297 23-Nov-2015 reyk

Back out the CRITICAL change, turn it back from 10% to 25%. Some
people with old/broken batteries didn't like this change, and the
underlying heuristics are wrong.


# 1.296 22-Nov-2015 reyk

The "CRITICAL" APM battery level, that is exported/faked by the ACPI
stack, is currently set to 25%. With 25% battery left, a typical
modern laptop will have at least one hour runtime left. It is an
arbitrary value, and other APM power layers in the kernel set CRITICAL
to something like 5% or 10%, so set it to 10% for now.

OK mlarkin@


# 1.295 28-Sep-2015 deraadt

In low-level suspend routines, set cold=2. In tsleep(), use this to
spit out a ddb trace to console. This should allow us to find suspend
or resume routines which break the rules. It depends on the console
output function being non-sleeping.... but that's another codepath which
should try to be safe when cold is set.
ok kettenis


# 1.294 06-Sep-2015 deraadt

clarify malloc arguments (base + length...)


# 1.293 28-Aug-2015 deraadt

fairly simple sizes for free(); ok tedu


# 1.292 20-Aug-2015 kettenis

Respect the access size when reading or writing to pci config space.
This version also makes sure writes are properly aligned, preventing
the panics seen with the previous incarnation of this change.

Fixes battery status passthrough in vmware, brightness keys on my x220
and who knows what else.

ok mlarkin@, deraadt@


Revision tags: OPENBSD_5_8_BASE
# 1.291 04-Aug-2015 deraadt

If we are setting ACPI_PM1_BM_RLD do it only once, and remember to
repeat this action at resume time.
ok mlarkin guenther


# 1.290 29-Jul-2015 deraadt

Something subtle in previous revision is locking up suspend or booting
on some laptops. Mailing lists threads mention x220 and Asus machines.
Reverting for now.


# 1.289 21-Jul-2015 kettenis

iRespect the access size when reading or writing to pci config space.
Fixes battery status passthrough in vmware and who knows what else.

Tested by many.

ok mlarkin@, deraadt@


# 1.288 17-Jul-2015 mlarkin

fix comments


# 1.287 26-Mar-2015 jsg

Use the 0x10 extended capability to determine if the trackstick
buttons are wired to the trackpad and need to be re-routed to the
trackstick. Without this change the buttons on 2015 thinkpads
get picked up as extended buttons that show up as scroll up/down.

Remove the X1 Carbon 2015 (LEN0048) and X250 (LEN0046) from the
top button area/soft buttons quirks list. Also avoid using the
quirk list entirely if the capability bit is set.

Based on work from jcs@ with feedback from mpi@
ok mpi@ jcs@


# 1.286 20-Mar-2015 kettenis

The ACPI standard says that a PCI function number of FFFF can be used to refer
to all functions on a device. This makes no sense in the context of accessing
PCI config space. Nevertheless stsp@ has a machine with AML that tries to
do config space reads from a device where _ADR encodes function number FFFF.
This is almost certainly buggy AML, but since Linux (and presumably Windows)
boot just fine on this machine we need to work around this. Linux effectively
treats function FFFF as function 7, which isn't present on this machine.
Instead we simulate a read from a non-existent device by returning a bit
pattern of all ones. Writes to function FFFF will still panic.

ok mlarkin@


# 1.285 16-Mar-2015 deraadt

Reduce use of SMALL_KERNEL in acpi, bringing acpiec driver and more gpe
handling into RAMDISK. This is now possible because the install media
has ample room. The goal is to reduce special cases where we may be
skipping (unknown) important operations...
ok mlarkin kettenis


# 1.284 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.283 07-Feb-2015 deraadt

New framework that allows hibernate to pass in entropy from it's fresh
boot.
ok mlarkin


# 1.282 06-Feb-2015 mlarkin

Fix a hibernate crash on some machines due to unmapping a page that
may not have been mapped previously (in the failure to hibernate case).

Also ensure that the lowmem ptp is mapped in all cases (not just MP).

ok kettenis


# 1.281 17-Jan-2015 deraadt

resume_randomness() before spinning up other cpus...


# 1.280 15-Jan-2015 jsg

Define a new wscons mouse type for Synaptics clickpad devices that lack
physical buttons. This will be used if the acpi pnpid for the mouse
matches a list provided by Synaptics found in Linux.

Combined with the xenocara changes this will remove the need for an xorg
config file for the x240/t440/t540 generation of thinkpads.

ok matthieu@ shadchin@ kettenis@


# 1.279 11-Jan-2015 kettenis

Tolerate checksum errors in ACPI tables (but complain about them). Apparently
there are machines out there with broken BIOSen with such checksum errors in
thier RSDT tables. Windows runs fine on these machines, so it probably does
not check. Linux ignores checksum errors as well (and prints a warning).

ok deraadt@, krw@


# 1.278 18-Dec-2014 deraadt

suspend and resume the RNG (at what looks like the right spots)
ok reyk djm


# 1.277 09-Dec-2014 doug

Replace some malloc(n*size,...) calls with mallocarray().

ok tedu@ deraadt@


# 1.276 06-Dec-2014 mpi

If "machdep.lidsuspend" is not set, do not go back to sleep if the lid
is still closed when resuming. This allows docked laptop to be resumed
without opening them.

ok mlarkin@


# 1.275 23-Nov-2014 mlarkin

If a machine has no _LIDs, then none can be open. This diff fixes a problem
where machines without any _LIDs were going back to sleep after hibernate
due to the recent suspend-if-lid-closed-on-resume diff. Noticed on qemu,
which doesn't have any _LIDs.

discussed with deraadt


# 1.274 18-Nov-2014 krw

Nuke yet more obvious #include duplications.

ok miod@


# 1.273 08-Nov-2014 mlarkin

If resuming from sleep (zzz/ZZZ) and the lid is still closed, go back to
sleep. This prevents accidental lid flex or slight opening in a backpack
from waking the machine up and leaving it resumed (powered on).

ok deraadt@


# 1.272 26-Sep-2014 kettenis

Rework piglet and pig allocation. Currently the piglet gets allocated
deep down in the suspend path, where it is really hard to recover from
allocation failure. So allocate the piglet early on in the suspend path.
Also change the piglet and piglet allocation functions to use km_alloc(9)
instead of doing pmemrange magic. This removes a bunch of code which, in the
case of the piglet allocation, is broken since it results in a NULL pointer
dereference. Also switch the piglet allocation to not wait. If we can't
allocate 16MB of phys contig memory on a halfway modern machine we're almost
certainly under a lot of memory pressure and we're better off not trying to
hibernate anyway.

ok mlarkin@


# 1.271 25-Sep-2014 kettenis

Bring back rev 1.249. Now that mp_setperf() has been fixed, it should be
safe to restore perflevel when resuming. Should fix the issues people
see with the processor running at full speed heating up the machine while
the apm command reports that the CPUs are running at a low speed.

ok dcoppa@, deraadt@


# 1.270 23-Sep-2014 kettenis

Release the acpi lock around calls to wsdisplay_suspend() and
wsdisplay_resume(). The X server may adjust the display brightness just
before/after the VT switch when suspending/resuming. In fact, it may
already be blocked adjusting the display brightness for some reason
unrelated to the suspend request.

The VT switch request will eventually time out, but the delay is annoying
and the failed VT switch will lead to problems after resume.

Needed for a future update of the xf86-video-intel driver.

ok mlarkin@


# 1.269 19-Sep-2014 kettenis

Use config_suspend_all(9).


# 1.268 08-Sep-2014 kettenis

Clear the PME Status bit on PCI devices in response to Device Wake events.
Fixes the interrupt storm on the 2009 Mac mini with WOL enabled on its
nfe(4) interface.

ok mlarkin@


Revision tags: OPENBSD_5_6_BASE
# 1.267 20-Jul-2014 mlarkin

Support hibernating to softraid crypto volumes.

much help and ok from deraadt@


# 1.266 16-Jul-2014 mlarkin

Reenable hibernate RLE support and flush+zero all memory after unpack.


# 1.265 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.264 12-Jul-2014 kettenis

Add a function to drop all clean pages on the page daemon queues and call
it when we hibernate.

ok mlarkin@, miod@, deraadt@


# 1.263 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.262 11-Jul-2014 mlarkin

Flush the buffercache to 16MB on hibernate and restore its previous max
size (kern.bufcachepercent) on resume, for better hibernate performance.

ok beck@


# 1.261 10-Jul-2014 blambert

remove unneeded header

ok mlarkin@


# 1.260 01-Jul-2014 kettenis

Adding an additonal delay after each access to SystemMemory or SystemIO
during autoconf makes no sense.

ok deraadt@


# 1.259 23-Jun-2014 kettenis

SystemMemory is used to access memory mapped registers on some machines, so we
must use the bus_space(9) API to access it instead of memcpy(9). Also make
sure we properly align access to fields that need more than byte alignment.

ok guenther@


# 1.258 21-May-2014 mlarkin

Fix invalid / missing format specifiers in several dnprintf statements
(used only when ACPI_DEBUG is enabled)

From a diff on tech@ by Fabian Raetz, with a minor change by me, tested
on i386 and amd64 with ACPI_DEBUG enabled and disabled.


# 1.257 25-Apr-2014 mlarkin

Defer the free of the hibernate structures until later, when it is safe to
do so.

Freeing the hibernate structures too early leads to inadvertent calls into
splx when we aren't ready for them yet. Found when trapping splx calls in
the resume path.

discussed with deraadt@ at m2k14


# 1.256 13-Apr-2014 mlarkin

Fix a badly worded debug message that implied the entire machine was going
to sleep, instead of just the acpi thread going to sleep.


# 1.255 31-Mar-2014 mpi

Change the order of operations during a suspend/resume cycle and
call bufq_quiesce() after executing the DVACT_QUIESCE handlers.

This should be safe since no disk nor controller drivers have such
handler but it will allow us to detach sd(4) devices attached to a
USB bus.

Another benefit pointed out by kettenis@ is that drivers that need
to read a firmware from the disk should be able to do it at resume
time in a DVACT_WAKEUP handler.

ok kettenis@, deraadt@


# 1.254 13-Mar-2014 dlg

get rid of the assumption that the head of the alldevs list is the
"mainbus" device. this breaks when mpath is enabled because it
attaches before mainbus and therefore takes the head position.

have autoconf provide device_mainbus() which looks up mainbus_cd,
and use that instead.

discussed with deraadt who just wants mpath stuff to move forward
despite there being many ways to shine this particular turd.


Revision tags: OPENBSD_5_5_BASE
# 1.253 21-Feb-2014 deraadt

Do the lid control further on in the resume sequence, since this is
AML, and will do WAITOK operations. While cold, that is a bad idea.
We seem to be safe with respect to the spec, I do not see any event
debounce issues here.
ok kettenis, tested by many


# 1.252 20-Jan-2014 claudio

Instead of checking sc->sc_revision <= 1 check that the
fadt->hdr_revision <= 1. This is a better check since it checks
the version of the proper descriptor. This allows the alix APU
to reboot since those boards have currently a wrong sc->sc_revision.
Committing this now so that it can be tested easier.
With and OK jsg@ also fine by derradt@


# 1.251 24-Dec-2013 kettenis

If the FADT has its SMI_CMD set to zero, assume we're only ACPI-only hardware
and don't need to disable SMI ownership of the ACPI hardware registers.

ok mlarkin@


# 1.250 23-Dec-2013 kettenis

Revert previous commit. It makes resume hang on some MP systems running
GENERIC.MP.


# 1.249 16-Dec-2013 kettenis

Restore hw.setperf upon resume like we do for apm(4) on i386. Seems we need
to do this fairly late in the resume to avoid hangs.

ok deraadt@, mlarkin@


# 1.248 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


# 1.247 06-Nov-2013 mpi

Add support for Power Resources for Dx states and the necessary hook
for PCI devices. This hook should be called twice, before and after
changing the power state of a PCI device.

Before setting the device to the new state, the ACPI layer will notify
every power resources linked to the device for that state and make sure
they are turned "_ON". After changing the state of the device, it will
decrement the reference of every power resources linked to that device
for the old state and turn them "_OFF" if they are no longer referenced.

This fixes the no-USB after resume problem seen on various ThinkPad,
problem initialy diagnosed with Alexander Polakov.

ok kettenis@, deraadt@


Revision tags: OPENBSD_5_4_BASE
# 1.246 01-Jun-2013 mlarkin

Stop printing ACPI wakeup devices in dmesg after the 16th wakeup device.

Helps with VMware VMs that report hundreds of wakeup devices.

ok reyk@, deraadt@


# 1.245 31-May-2013 bluhm

Fix the build for a kernel without wd and pciide in its config.
OK deraadt@ tedu@


# 1.244 30-May-2013 deraadt

Enforce ca_activate tree-walks over the entire heirarchy for all events,
cleaning up some shutdown-hook related code on the way.
(A few drivers related to sparc are still skipped at kettenis' request)
ok kettenis mlarkin, tested by many others too


# 1.243 18-Apr-2013 deraadt

pin the acpi thread to the BSP, because AML and SMI cannot be trusted. I
was certain we were doing this already, but it turns out no...


Revision tags: OPENBSD_5_3_BASE
# 1.242 24-Dec-2012 guenther

In acpiioctl(), Return though the splx() path if suser() returns an error

ok deraadt@


# 1.241 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


# 1.240 04-Oct-2012 ehrhardt

Use information provided by ACPI to attach secondary PCI host bridges.

ok kettenis@


# 1.239 07-Sep-2012 kettenis

Add a new function called acpi_pci_min_powerstate() that returns the lowest
power state for a PCI device for the power state (currently S3 or S4) we're
transitioning into. This function is probably too simplistic; there is a lot
of (silly) complexity in the ACPI spec.

ok deraadt@


Revision tags: OPENBSD_5_2_BASE
# 1.238 13-Jul-2012 pirofti

Disable acpivideo(4) on ASUS laptops.

There are some models that panic whenever an event is received.
Some cope, some don't. In order to get this right acpiec(4) requires
major rework. Until then let machines boot.

Discussed with deraadt@.


# 1.237 11-Jul-2012 mlarkin

for now, restrict hibernate to root in the acpi ioctl kernel interface.
normal rules for controlling this interface through apmd still apply.

ok deraadt@


# 1.236 09-Jul-2012 deraadt

tiny little oops in untested hibernate block


# 1.235 09-Jul-2012 deraadt

clean up ioctl() case statements


# 1.234 09-Jul-2012 deraadt

acpi_disable_onegpe() is not used; ok pirofti


# 1.233 24-May-2012 kettenis

If _MIN > _MAX consider a WordBus resource to be invalid. Fixes a panic on
the IBM x3550-M4.

ok mikeb@


# 1.232 29-Mar-2012 mlarkin

Missing #ifdef in previous diff


# 1.231 29-Mar-2012 mlarkin

Prevent hibernation attempts on machines with unsupported disk controllers.
The same check is also performed later in the hibernate sequence, but by
then it's too late to cleanly unwind (presently).

ok deraadt


# 1.230 26-Mar-2012 deraadt

simplify conditionals in acpi_sleep_state()
ok mlarkin


# 1.229 26-Mar-2012 deraadt

hook in the hibernate request code; half of this diff is from mlarkin
ok mlarkin


Revision tags: OPENBSD_5_1_BASE
# 1.228 20-Sep-2011 deraadt

Very early in suspend while the clock is still ticking realtime,
save the clock back to the rtc. The zaurus already did this.
ok phessler miod kettenis


Revision tags: OPENBSD_5_0_BASE
# 1.227 02-Jul-2011 nicm

kqueue attach functions should return an errno or 0, not a plain 1. Fix
the obvious cases to return EINVAL and ENXIO.

ok tedu deraadt


# 1.226 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.225 16-Jun-2011 pirofti

Import acpitoshiba driver.

This is originally written by Hiroyuki Aizu, ported and integrated
in our tree by Javier Vazquez <javama4 AT gmail> with tweaks and
suggestions by me.

Import prodded by deraadt@ and the lack of testers on tech@.


# 1.224 27-Apr-2011 jcs

attach acpithinkpad to newer lenovo models like the x120e

ok deraadt@


# 1.223 22-Apr-2011 jordan

Fix uninitialzied variables and formatting strings (-Wxxx errors)


Revision tags: OPENBSD_4_9_BASE
# 1.222 02-Jan-2011 jordan

Re-add code for ACPI taskqueue, eliminates need for state vars in acpi softc
Serialize processing ACPI tasks for sleep/powerdown/GPE event
ok deraadt@


# 1.221 31-Oct-2010 guenther

Revert last commit: it breaks resume on Thinkpads
"then please back it out" deraadt@


# 1.220 26-Oct-2010 jordan

Add task queue for ACPI gpe and notify handlers
This fixes eject on Dell Latitude dock and an issue on the Dell Mini
battery update.
ok deraadt


# 1.219 07-Oct-2010 jordan

Create separate functions for enabling wake and runtime gpes.
Remove old suspend/resume gpewalk function
ok deraadt, mlarkin


# 1.218 05-Oct-2010 jordan

split acpi_enable_onegpe to separate enable/disable functions
ok deraadt


# 1.217 05-Oct-2010 jordan

Added acpi_disable_allgpes function to clear GPE status
ok deraadt


# 1.216 05-Oct-2010 deraadt

clean up acpi_softc use more


# 1.215 05-Oct-2010 deraadt

delete a bit of #if 0 code


# 1.214 05-Oct-2010 deraadt

hide use of our global acpi_softc pointer a bit better


# 1.213 29-Sep-2010 deraadt

grab the acpi thread lock very early in acpi thread startup; not that it
is likely that anyone will race us... but why be careless.
ok kettenis


# 1.212 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.211 29-Aug-2010 jasper

- add a check for FWRITE here too.

ok miod@


# 1.210 27-Aug-2010 kettenis

Make PCI Power Management optional, and only enable it when acpi(4) attaches.

ok deraadt@


Revision tags: OPENBSD_4_8_BASE
# 1.209 08-Aug-2010 kettenis

Close race between the acpi thread and wscons ioctls running in process
context. We might rip this out post-release and replace it with a solution
that serializes things in a more obvious way.

ok deraadt@, miod@ and "those crazy texans who ok anything"


# 1.208 08-Aug-2010 deraadt

Report the acpi sleep states when on a RAMDISK; ok krw


# 1.207 07-Aug-2010 kettenis

Enforce acpi register access rescrictions.

ok mlarkin@, deraadt@, tested by marco@


# 1.206 07-Aug-2010 deraadt

upon resume, notify all the acpiac and acpibat drivers to update
their status. some laptops do not do this automatically.
ok kettenis canacar


# 1.205 06-Aug-2010 marco

Always call _PSW on _LID devices that support it to enable lid open to
resume the machine. Conversely disable it when the machine wakes up.

Tested by several ok deraadt


# 1.204 06-Aug-2010 deraadt

As part of a revamp of the PM1/GPE code, I write this diff to look for
unmanaged EN & STS bits in the PM1 register at interrupt time and report
them. As a side effect this splits the STS acknowledgement into two writes
(for power, and sleep) instead of one. The printf that is added (to spot
unmanaged STS bits) has not yet been triggered as far as we know.

Before the "write to PM1 registers at the right offset" diff went in, this
was not neccessary. But newer thinkpads do not have a working soft power
button without this diff.

We have no idea why.
ok mlarkin kettenis


# 1.203 05-Aug-2010 deraadt

PM1 power button events were generating 'fake events' to acpibtn PWRB,
but if there is no such device these were being tossed. Instead, arrange
for all power button handlers to flag a softc variable which is processed
by the thread; so that one place will know to request a powerdown (psignal
to init)
ok marco mlarkin


# 1.202 05-Aug-2010 deraadt

rename a variable which is annoyingly poorly named


# 1.201 05-Aug-2010 mlarkin

Write to the correct offset for various ACPI pmreg registers. Writing to
the correct register does properly clear the wake status bits and fixed
event bits, which allows some machines to have more than 1 suspend cycle.

bug spotted by deraadt@

tested by deraadt@ and myself on a half-dozen machines and others on
hackers@

ok deraadt@, marco@


# 1.200 05-Aug-2010 deraadt

Fix the power button. The pm1 register is 16 bits in size.
My mistake, a while back.
tested by krw


# 1.199 04-Aug-2010 kettenis

Print supported sleep states.

ok marco@, phessler@, deraadt@


# 1.198 03-Aug-2010 mlarkin

Use the proper flag for re-enabling certain hardware events (power btn,
sleep btn). The (incorrect) flag prevoiously used coincidentally had the
same value, so this shouldn't cause any different behavior than before.

tested on a variety of machines (i386, amd64, sp, mp) - no behavioral
change seen

ok deraadt@


# 1.197 28-Jul-2010 marco

Make all gasio failures look and behave the same.


# 1.196 28-Jul-2010 mlarkin

Delay a little longer on suspend before giving up. Fixes machines that
sometimes don't suspend because the sleep transition takes too long
(symptoms like failing to sleep but the backlight stays on).

ok deraadt@


# 1.195 27-Jul-2010 deraadt

remove XXX; EC can now be attached early when needed; ok jordan


# 1.194 27-Jul-2010 deraadt

more brackets feels good


# 1.193 27-Jul-2010 deraadt

XXX annotate a block in acpi_enter_sleep_state() that is pretty worrying


# 1.192 27-Jul-2010 deraadt

horrific indentation that nearly made me puke


# 1.191 27-Jul-2010 mlarkin

Set the sleeping indicator light on machines that support it, in case SMI
doesn't do it for us. Shows the 'moon' LED on older Thinkpads when
sleeping.

Tested on T43p, W500, T510 by me and on X61s by deraadt@.

ok deraadt@


# 1.190 27-Jul-2010 jordan

Early initialization of acpiec if ECDT table exists
Fixes hang when booting thinkpads while docked
ok deraadt


# 1.189 26-Jul-2010 deraadt

remove merge error which was commited


# 1.188 26-Jul-2010 pirofti

Add support for Sony ACPI hotkeys via a new driver: acpisony(4).

Currently it only works for the suspend button and tries to do right for
the brightness events, but I haven't found a Sony laptop that like to do
right so far.

In the future I want to make the brightness keys work on all Sony's and
also add support for the zoom hotkeys and whatever other funky keys I can
find on those things.

Okay deraadt@.


# 1.187 22-Jul-2010 deraadt

pretty large cleanup of SMALL_KERNEL


# 1.186 21-Jul-2010 deraadt

in the acpi GPE handler, late ack level interrupts not edge interrupts.
Duh.


# 1.185 21-Jul-2010 deraadt

Merge the edge and level gpe handlers into one (now possible since the
gpe structure tells us whether the gpe is edge or level)


# 1.184 21-Jul-2010 deraadt

delete #if 0 code we are not going to use; ok marco


# 1.183 20-Jul-2010 deraadt

make the mask code for acpi_enable_onegpe() match other similar code


# 1.182 20-Jul-2010 deraadt

Handle the acpi interrupt controller with proper edge/level handling,
wrapped in spltty to avoid racing against the interrupt controller.
Repair the gpe bit masking code while there, and do operations in the
right order
ok kettenis mlarkin, help from jordan at figuring out the order of
operations.


# 1.181 20-Jul-2010 matthew

When we're preparing to go to sleep, only call bufq_restart() if other
preparations failed.

ok kettenis@, thib@, deraadt@


# 1.180 19-Jul-2010 deraadt

permit compilation with ACPI_DEBUG


# 1.179 19-Jul-2010 deraadt

there is no need for a seperate dsdt_softc pointer; dsdt does not have a
softc but is just a part of the single acpi driver.
ok jordan


# 1.178 19-Jul-2010 deraadt

acpi_delay() is no longer needed; ok jordan


# 1.177 18-Jul-2010 mlarkin

Clear event status on resume, to avoid going back to sleep/reboot/shutdown
immediately after resume. Tested by a bunch of people on hackers@ on
various hardware. Fixes at least Toshiba NB200 and Gateway LT3103u.

ok marco


# 1.176 13-Jul-2010 deraadt

rename some thread related interfaces so they are logical


# 1.175 10-Jul-2010 jordan

Verify _ADR bdf is correct (some systems return 0xffff)
Works on Dell x9xx and Studio 1555


# 1.174 09-Jul-2010 jordan

Clear the noise


# 1.173 08-Jul-2010 jordan

Add mapping for ACPI device to PCI bdf (match autoconf tree)
Simplify resource parsing function to use buffer argument
Convert namespace linked lists to use queue macros
ok marco@, deraadt@


# 1.172 06-Jul-2010 deraadt

Use spltty() to lock downcalls from apm against the information being
modified by the acpi thread.
ok kettenis


# 1.171 06-Jul-2010 deraadt

Don't use a workq to do the suspend, because it races aginst the acpi
thread. Instead, just tell the acpi thread to do the suspend for us.
This makes apmd & zzz work correctly. While here, have acpithinkpad
attempt to post the event to apm before prompting the suspend itself.
ok kettenis marco mlarkin


# 1.170 05-Jul-2010 mlarkin

Reset register SCI_EN on resume. The ACPI spec clearly states we don't have
to do this, but many machines don't, or forget to, which leads to all sorts
of problems on resume (broken battery stats, overheating, failure to resume
more than once, etc).

tested by many, ok marco@, deraadt@


# 1.169 01-Jul-2010 thib

do bufq quiesce again, since vnd/the bufq_done call in
biodone has been fixed.


# 1.168 01-Jul-2010 jordan

Backout recent AML changes, breaks on R210 and others


# 1.167 01-Jul-2010 jordan

Simplified aml_parse_resource call, cleanup code
ok marco


# 1.166 01-Jul-2010 jordan

Fix subordinate bus # for multi-root PCI buses
ok marco, kettenis


# 1.165 30-Jun-2010 thib

Disable/partially backout the bufq quiesce changes as this
is causing havoc with vnds and release must be buildable.


# 1.164 29-Jun-2010 deraadt

Fix ramdisks (new code was placed inside a SMALL_KERNEL chunk)


# 1.163 29-Jun-2010 jordan

Add support for mapping ACPI to PCI devices
ok kettenis, deraadt


# 1.162 29-Jun-2010 kettenis

Call bufq_quiesce() on suspend to stop all I/O and bufq_restart on resume.
Makes suspend/resume work much better while doing disk I/O.

ok mlarkin@


# 1.161 29-Jun-2010 tedu

60 is a more reasonable guess for number of minutes in an hour than 100.
spotted by TeXitoi. ok deraadt


# 1.160 27-Jun-2010 jordan

Add _acpi_matchhids routine, precursor to pci device match


# 1.159 27-Jun-2010 mlarkin

Clear ACPI fixed event status on resume (power buttons/etc) so that some
machines don't immediately resume after suspending to S3.

Tested by kettenis@ and I on 5 or 6 machines, deraadt@ "yeah, looks good"


# 1.158 27-Jun-2010 jordan

Cleaned up acpi_maptable, adds to table list directly


# 1.157 07-Apr-2010 deraadt

If we have done a wsdisplay_suspend(), and ACPI decides it does not want to
suspend, we must roll back by calling wsdisplay_resume() or the virtual
console switching will be locked forever
ok oga


# 1.156 07-Apr-2010 kettenis

Make suspend/resume work on MP machines (running an MP kernel). Joint work
from mlarkin@ and me, with some amd64 fixes thrown in by deraadt@

ok marco@, deraadt@, pirofti@, mlarkin@


# 1.155 31-Mar-2010 kettenis

Bring /dev/apm support in line with apm(4). Make the suspend button send
a suspend request event to apmd(8) instead of suspending immediately. Also
keep track of whether /dev/apm and /dev/apmctl are currently open, such that
we can still suspend immediately if apmd(8) isn't running.

ok deraadt@, marco@, pirofti@, jsing@, oga@


# 1.154 30-Mar-2010 oga

Prevent the apmd/x races for good.

When we hit suspend time, go through all wsdisplays on the system. if
they are in mode MAPPED, but not MODE_DUMBFB then if possible do a full
vt switch to a !mapped vt, and prevent switching back until resume time.
This has to be called from MD code because this involves userland
running so that X can run the vt switch signal handler. This way, any
case where we are using the "poke registers from userland" model, we
will not be on the hardware when we go down, so the kernel can actually
handle thing properly.

Tested on several acpi laptops (by kettenis@ and ian@), x40 (me and
beck@ at LEAST) and zaurus (me). Maybe others, but if so I forgot who at
this time..

Idea from deraadt somewhere over the Faroe Islands (I thought of a
similar thing myself a while ago). Much prompting from him. Ok and
comments miod@


# 1.153 25-Mar-2010 oga

Fix & vs. && in the kqfilter code.

Based on a comparison with the apm code.

ok deraadt@, kettenis@


Revision tags: OPENBSD_4_7_BASE
# 1.152 26-Nov-2009 mlarkin

Visual (and on some machines, audio) feedback while the machine is
resuming via calls to aml _SST method. This also makes the "moon" LED on
thinkpads to blink during resume and cease when resume is completed.

ok pirofti@


# 1.151 26-Nov-2009 deraadt

Toggle into "cold" when we we disable interrupts, because tsleeping
in the AML interpreter and context switching into userland processes
tends to sometimes pop the ACPI trampoline page out of our address
space. Makes lots more machines work.
ok kettenis mlarkin


# 1.150 24-Nov-2009 jsg

in the resume path evaluate _WAK before dealing with device drivers
like the spec says, not after.

ok mlarkin@


# 1.149 24-Nov-2009 deraadt

abort suspends on SMP systems until that gets written; ok mlarkin


# 1.148 23-Nov-2009 mlarkin

pci should return the result of it's children's suspend/resume calls back
to its parent.

handle suspend failure case and unwind if devices have an activate function
that returns failure.

ok deraadt@, kettenis@


# 1.147 23-Nov-2009 mlarkin

acpi* devices need to call their children on suspend/resume.
ok deraadt@


# 1.146 23-Nov-2009 pirofti

Remove ACPI_SLEEP_ENABLED checks.

This enables by default the suspend/resume paths in the kernel.

Okay deraadt@.


# 1.145 23-Nov-2009 deraadt

knf and spacing that are super annoying


# 1.144 23-Nov-2009 mlarkin

Hook up Theo's new children suspend/resume function, as well as more ACPI
suspend/resume glue.

ok deraadt@


# 1.143 26-Oct-2009 deraadt

Do not do strncmp comparisons on dv_xname because longer device names which
look similar could arrive in the future. Instead, compare directly against
dv_cfdata->cf_driver->cd_name
Issue originally spotted by miod


# 1.142 26-Oct-2009 jordan

Verify checksum+address when loading ACPI tables.
Some systems had invalid entries in RSDT/XSDT.
ok marco@


# 1.141 23-Jul-2009 cnst

New aibs(4) driver for ASUSTeK AI Booster (ACPI ATK0110) hardware monitoring,
with sensor state support through limits provided by the ACPI.

Tested on several ASUS motherboards kindly networked by Sam Fourman Jr.

ok deraadt marco jordan


Revision tags: OPENBSD_4_6_BASE
# 1.140 03-Jun-2009 pirofti

Import acpi power resource driver. This is needed for suspend/resume logic.

Okay jordan@ and marco@.


# 1.139 03-Jun-2009 pirofti

Add acpivideo support. This adds brightness support for all laptops
except thinkpads, they will use the acpithinkpad driver. The driver is
also hooked into wsconsole. So brightness can be adjusted via:

$ wsconsctl display.brightness=<percentage>

This is very helpfull on some laptops that have a nasty bios and get two
steps instead of one when pressing the brightness button.

Tested on various dell, fujitsu, acer, samsung and other laptops.

Okay marco@, miod@. Suggestions from kettenis@.
Lots of reviews and help from miod@, thanks!


# 1.138 03-Jun-2009 jordan

Added ejectable bay for IDE cdroms
ok marco@


# 1.137 30-Apr-2009 marco

Oga changed apm ioctls and forgot to grep. Temporary work around by hitting
the sleep button when we get any request. This interface will chnage at
some point.
ok oga


# 1.136 19-Apr-2009 krw

Call acpi_sleep_walk() when powering down just like when entering
any other sleep state. This tells acpi wake devices to not wake up
the box until manual powering up resets their status. Makes my Dell
GX520 stay powered off in response to 'halt -p'.

In snaps for a while.

ok marco@ (several times) deraadt@


# 1.135 17-Apr-2009 pirofti

Add device ranking support for future sleep/wake developments.

okay marco@


# 1.134 11-Mar-2009 jordan

Cleanup.. use aml_evalinteger instead of aml_evalname
ok marco@


# 1.133 10-Mar-2009 jordan

Fix cut/paste error.. call _BFS not _PTS
ok marco@


Revision tags: OPENBSD_4_5_BASE
# 1.132 21-Feb-2009 marco

overzealous cut/paste; found by Vladimir Kirillov <proger@uaoug.org.ua>


# 1.131 20-Feb-2009 marco

Oops didn't mean for Debugger() to remain there.


# 1.130 19-Feb-2009 marco

suspend/resume bits so that we can develop this in tree. This is disabled.
code from mlarkin and me
help from art,toby,jordan and several others
ok jordan, go for it deraadt


# 1.129 10-Feb-2009 jordan

Fix null pointer for shutdown if acpi not enabled
ok marco@


# 1.128 04-Feb-2009 kettenis

Some systems that claim to be ACPI 2.0 apparently don't provide an XSDT
address. Make the code fall back on the RSDT on those systems.

From Joerg Niendorf <p3y80@internode.on.net>. Fixes PR 5986.


# 1.127 17-Jan-2009 krw

A couple of stray malloc/memset -> malloc(M_ZERO), a bit of knf and
lose a couple of unnecessary casts. No functional change.

Feedback from kettenis@

ok kettenis@ marco@


# 1.126 06-Nov-2008 marco

Collapse all sanity checks of hids into one function.

Originaly from jordan


Revision tags: OPENBSD_4_4_BASE
# 1.125 02-Jul-2008 fgsch

add acpivideo. for now almost an empty skel so others can work on
suspend/resume. eventually it will also manage output switching and
brightness where supported. prodded by marco@


# 1.124 11-Jun-2008 marco

Fix $OpenBSD$ for once and for all


# 1.123 08-Jun-2008 deraadt

aaa_dev was being set from an uninitialized variable; ok toby


# 1.122 07-Jun-2008 marco

Kill ACPIVERBOSE. It was never used and is worthless to boot.

comments jmc
ok toby


# 1.121 06-Jun-2008 marco

Shrink acpi some more for floppies and while here re-enable acpi on the
floppies. Fixes crazy sony vaio kettenis has.

tested by deraadt
ok deraadt jordan kettenis


# 1.120 01-Jun-2008 marco

Fix \n nit in SMALL_KERNEL.


# 1.119 01-Jun-2008 marco

Re-enable new parser. Lots of testing by various folks. Fits all media
now too.

ok kettenis deraadt


# 1.118 16-May-2008 dlg

back out jordans new parser.

it didnt have enough oks from the right people, it did break machines
that were previously working, and it bloated the RAMDISKS and install
media beyond the constraints of the media.

requested by deraadt@


# 1.117 14-May-2008 jordan

Adding new ACPI Parser code
Old guts of code still exists, needs to be torn out next
ok marco@
ok brad@


# 1.116 27-Apr-2008 jcs

add acpithinkpad(4), an acpi support driver for ibm/lenovo
thinkpads.

provides functionality that the bios did on older, non-acpi
thinkpads like respond to screen brightness keys and toggle the
bluetooth device on and off.

ok deraadt@ and marco@


# 1.115 24-Apr-2008 jsing

Introduce acpiasus(4), a driver for the ACPI based hotkeys found in many
ASUS laptops (including the ASUS EeePC) - largely based on NetBSD's
asus(4) driver. On the ASUS EeePC this allows us to enable/disable
wireless, change screen brightness and use the volume keys.

ok jsg@, weingart@


# 1.114 12-Apr-2008 kettenis

Get rid of acpi_s5 global variables; simply send SIGUSR2 instead of SUGUSR1
to tell init(8) to power down the machine.

ok krw@


Revision tags: OPENBSD_4_3_BASE
# 1.113 21-Jan-2008 jsg

Check against RESET_REG_SUP in FADT before doing the acpi reset dance
after all, not doing so appears to break more machines than it fixes.

ok marco@


# 1.112 05-Jan-2008 kettenis

Never attach more acpicpu(4) devices than cpu(4) devices. Always attach
the first acpicpu(4) device on non-MULTIPROCESSOR kernels, regardless of its
ACPI processor ID.

ok mikeb@


# 1.111 05-Dec-2007 deraadt

Hang acpi(4) below bios(4) instead of mainbus(4). This lets us move acpi(4)
before pcibios(4), and then.... if acpi(4) attaches, skip pcibios(4) since
it messes the machines up. Fixes claudio's HP dl320 G5, and almost assuredly
others.
ok kettenis


# 1.110 05-Dec-2007 deraadt

repair acpiopen/acpiclose and friends to handle the /dev/apmctl interface,
so that apmd(8) can actually work correctly against acpi(4), and thus,
even do the fancy -C/-A stuff; also tested by jmc


# 1.109 02-Dec-2007 fgsch

fix the trailing space for real now.


# 1.108 02-Dec-2007 fgsch

avoid trailing spaces on output; marco@ ok.


# 1.107 27-Nov-2007 kettenis

Fix off-by-one that made us attach acpicpu0 and acpicpu1 on GENERIC kernels.

ok marco@


# 1.106 25-Nov-2007 jsg

When available and ACPI is enabled use the ACPI reset method
to reboot. This is required for some quirky sis machines
and likely others to reboot.

Tested by many. 'commit early commit often' deraadt@


# 1.105 19-Nov-2007 deraadt

sigh, amd64 still uses that stupid acpi_enabled variable...


# 1.104 19-Nov-2007 deraadt

softc's come initialized to 0, you know...


# 1.103 19-Nov-2007 deraadt

store revision; from jsg


# 1.102 19-Nov-2007 kettenis

Initialize sc_facs earlier.

ok deraadt@, beck@, canacar@


# 1.101 16-Nov-2007 deraadt

fix the bus_space #define nightmare, so that amd64 and i386 are much more
uniform. as a result shared code like acpi needs less #ifdef's
ok marco kettenis


# 1.100 15-Nov-2007 deraadt

remove the #ifdef ACPI_ENABLE. the EC codepath is either safe enough (and
will be improved), or it is unsafe because AML assumes it must be there
ok almost everyone


# 1.99 15-Nov-2007 mikeb

Make acpicpu(4) attach to the existing CPUs only.

Problem reported by form@. Fix idea by kettenis@.
Lots of help from deraadt@. Tested by deraadt@, form@ and me.

ok deraadt marco


# 1.98 14-Nov-2007 deraadt

spacing


# 1.97 12-Nov-2007 deraadt

correct : vs , in attach-time messages


# 1.96 06-Nov-2007 deraadt

all ACPI_ENABLE kernels need acpi_interrupt()


# 1.95 06-Nov-2007 deraadt

move acpi_read_pmreg() and acpi_write_pmreg() out of SMALL_KERNEL so
that acpi ramdisks can compile (obvious solution)


# 1.94 05-Nov-2007 canacar

Establish the acpi interrupt earlier. Fixes problems with some
GENERIC.MP kernels not getting acpi interrupts. This is a
workaround until the real issue with late establishment is fixed.
ok weingart@, beck@, gwk@


# 1.93 03-Nov-2007 jordan

Added support for displaying wakeup devices
ok beck@,weingart@,gwk@


# 1.92 11-Oct-2007 kettenis

When walking the tree yo find _PRT methods, check whether the device is
actually enabled and functioning. Similar to what we do for _INI.

ok weingart@, beck@


# 1.91 08-Oct-2007 krw

More simple memset(,0,) -> M_ZERO changes. In this batch move to
size(*p) as the first malloc() parameter where p is declared locally
and thus easy to check. Add M_ZERO to gpe_table allocation in acpi.c
even though there is no obvious bzero or memset nearby.


# 1.90 13-Sep-2007 mk

Remove unnecessary memset() (see aml_evalnode(9)) and a bit of KNF.

ok toby


# 1.89 13-Sep-2007 weingart

Implement ACPI 6.5.1 spec tree walk for _STA and _INI.
Fixes mk's laptop. No regressions so far (thank you to the
testers).

ok gwk@, mk@, marco@


Revision tags: OPENBSD_4_2_BASE
# 1.88 17-Apr-2007 mk

Now that acpidock no longer hangs machines during boot (at least
according to the reports I've been getting) it should be safe to enable
it again, so remove the #if 0 stuff around it.

ok marco


# 1.87 11-Apr-2007 jordan

Added changes for C-State
ok marco@


# 1.86 26-Mar-2007 marco

Calculate minutes left more accurately.

From giovanni <qgiovanni@gmail.com>


Revision tags: OPENBSD_4_1_BASE
# 1.85 03-Mar-2007 mk

Stop looking for acpidock until we fix that stuff that breaks all pre-60
series ThinkPads with acpidock(4) enabled.


# 1.84 22-Feb-2007 marco

We were getting no interrupts in .MP. There are 2 fixes; one in
acpi_machdep.c and move the interrupt establishment down after ioapic
has been initialized.

ok jordan


# 1.83 21-Feb-2007 kettenis

Make an acpi-enabled GENERIC.MP work on systems that only support PIC mode.

ok marco@


# 1.82 19-Feb-2007 jordan

Added acpi_map_address function for mapping GAS/IO registers
ok marco@


# 1.81 18-Feb-2007 jordan

Added dynamic perfect hash for opcodes
ok marco@


# 1.80 18-Feb-2007 marco

Shave off another K pre compression.
Shuffle SMALL_KERNEL functions around so that we only have a single #ifndef


# 1.79 17-Feb-2007 marco

Add SMALL_KERNEL throught acpi to make it fit on boot media.
ok deraadt


# 1.78 13-Feb-2007 marco

Add smarter _INI code. This helps with some dissapearing devices.

ok kettenis jordan


# 1.77 31-Jan-2007 gwk

Introduce acpi_hasprocfvs thats "ACPI Has Processor Frequency and Voltage
Scalling" a global flag set by acicpu attach to indicate that ACPI is
handling fvs through the _PSS objects. This will fix disappearing
EST/powernow when booting kernels with acpi enabled, as acpicpu isnt
compiled into GENERIC yet. Also in cases where acpicpu is built in and
legacy EST/powernow work but acpi is missing the _PSS object, the legacy
routines will be used.

tested by dim@, and Nick Nauwelaerts <nick AT nauwelaerts.net>
ok marco@, dim@


# 1.76 22-Jan-2007 mk

Actually look for docking stations by looking for _DCK nodes.

ok marco


# 1.75 02-Jan-2007 marco

On some systems EC must be attached as the first device. Move EC from
generic _HID devices to its own routine to force first attach. Bug found
and debugged by joris@

Code mostly written by joris@


# 1.74 26-Dec-2006 marco

Add polling to devices that require it such as acpibat and acpitz.
Use the same tsleep/wakeup mechanism as the interrupt code to keep it non-
concurrent.


# 1.73 21-Dec-2006 deraadt

even more knf love


# 1.72 21-Dec-2006 marco

Remove some leftover acpiec gpe/interrupt code.


# 1.71 20-Dec-2006 deraadt

oops, name already used


# 1.70 20-Dec-2006 deraadt

rename __acpi_enable_gpe(); ok marco


# 1.69 20-Dec-2006 deraadt

acpi_unmap_pmregs() is not used, ok marco


# 1.68 19-Dec-2006 deraadt

simplify some low-level methods substantially (shrinking code); ok marco jordan


# 1.67 06-Dec-2006 mk

malloc(..., M_NOWAIT) with no NULL check is bad. If allocation fails,
complain, clean up, and bail out of attach.

input and ok gwk


# 1.66 28-Nov-2006 jordan

Fixed clearing arg for acpi_init_pic
ok kettenis@


# 1.65 27-Nov-2006 kettenis

Don't spam dmesg.


# 1.64 27-Nov-2006 jordan

Fixes for acpiprt routing
Post-parse fixup for namerefs
ok kettenis@


# 1.63 25-Nov-2006 marco

Don't handle acpiev events inside the gpe handler since this causes deadlocks.

ok jordan@


# 1.62 15-Nov-2006 kettenis

Hook up acpi apic interrupt routing support to generic acpi code.

ok jordan@


# 1.61 02-Nov-2006 marco

Kill acpiec attach function and add its functionality to the generic _HID
device attach

prodded and ok jordan


# 1.60 02-Nov-2006 marco

Fix comments to reflect reality.


# 1.59 19-Oct-2006 marco

Replace ACPI ioctls with APM ioctls. This makes ACPI talk the same protocol
as APM therefore userspace can now retrieve power information in the same
fashion as all other apm arches (zaurus, macppc, i386 etc).

To use force the use of acpi device like so: apmd -f /dev/acpi

ok jordan@


# 1.58 19-Oct-2006 jordan

Fixed GPE debug levels
Fixed ref counter on temporary bufferfields
DSDT parsing fully working on IBM Thinkpad
ok marco@


# 1.57 15-Oct-2006 jordan

Removed old GPE handler code remnants
ok marco@


# 1.56 12-Oct-2006 jordan

Massive ACPI changes
Fixed ACPI leaks
new ACPI API
ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.55 30-Jun-2006 gwk

KNF, No functional change,
"go at it" marco@


# 1.54 30-Jun-2006 jordan

Support for calling Embedded Controller via new GPE interface
ok marco@


# 1.53 30-Jun-2006 jordan

Added new code for GPE handling
Also works with ACPIEC; need to fixup acpiec.c to remove original interrupt handler
ok marco@


# 1.52 30-Jun-2006 jordan

Added offset argument for acpi read/write pmreg routines (new GPE code)
ok marco@


# 1.51 31-May-2006 todd

do not call acpi_powerdown() unless acpi has successfully initialized
ok gwk@ canacar@


# 1.50 29-May-2006 canacar

Add embedded controller support.
ok marco@ deraadt@ testing & ok gwk@


# 1.49 19-May-2006 canacar

Minimal thermal zone driver. Monitors thermal zone temperature,
shuts down the system if the 'critical temperature' is reached.
ok marco@


# 1.48 11-Apr-2006 marco

KNF.


# 1.47 11-Apr-2006 gwk

Add acpi_resume(struct acpi_softc *sc), which calls some AML methods,
detected at attach time, currently unused.

ok marco@


# 1.46 11-Apr-2006 gwk

Discover some ACPI sleep related methods, and call them at the
appropriate time in acpi_enter_sleep_state().

ok marco@


Revision tags: OPENBSD_3_9_BASE
# 1.45 26-Feb-2006 marco

Add acpicpu as a device.

ok jordan@


# 1.44 23-Feb-2006 marco

remove dead function.

ok jordan@


# 1.43 22-Feb-2006 marco

Print out all tables it found during autoconf. We'll need this data in
the future.


# 1.42 22-Feb-2006 marco

Make dmesg less verbose.

ok jordan.


# 1.41 22-Feb-2006 jordan

Added parsing of SSDT tables
ok marco@


# 1.40 22-Feb-2006 jordan

Added new aml_register to notify by device id
Moved powerdown code to button device
ok marco@


# 1.39 21-Feb-2006 marco

Readjust debug levels that crept in too low over the last few days.
KNF


# 1.38 21-Feb-2006 marco

Rig the power button so that the machine shuts down correctly.


# 1.37 20-Feb-2006 marco

Add acpibtn device. Remains disabled for now.


# 1.36 19-Feb-2006 jordan

Added code for aml_notify GPE notification callback
ok marco@


# 1.35 19-Feb-2006 grange

Fix acpi_enter_sleep_state() by replacing current ugly hack
with mostly correct sleeping state transition code.

ok brad@ marco@


# 1.34 19-Feb-2006 marco

jordan:
Enable interrupts.
Added peripheral code to make this work.

marco:
Replace softintr and other types of interrupts with a handler with context.
Fixed interrupt magic for GPEs.
Some KNF

This is work done by jordan and I but has to be a single commit.


# 1.33 17-Feb-2006 marco

Remove earlier introduced delays. We really only need them during autconf.

ok jordan@


# 1.32 17-Feb-2006 marco

Add a generic delay function as per the ACPI spec.

from jordan@


# 1.31 17-Feb-2006 marco

Use tsleep instead of delay when !cold.


# 1.30 17-Feb-2006 marco

Argh, we need delays between reads/writes. The backend reads (i2c most likely)
are so slow that values come back as 0 sometimes. We have to do this
despite the spec saying we shouldnt have to. Guess having a faster
parser than others can hurt sometimes.

Idea from jordan.


# 1.29 17-Feb-2006 jordan

Fixed code for handling _INI
Rearranged debug statements
ok marco@


# 1.28 16-Feb-2006 jordan

Added stub for calling _INI
Fixed aml_dowhile for NULL return
ok marco@


# 1.27 16-Feb-2006 jordan

Fixed parser/evaluator to retrieve battery info+state
Cleanup of parser
Dell laptops return battery status
ok marco@


# 1.26 06-Feb-2006 brad

fix a typo so this will compile on amd64.


# 1.25 04-Feb-2006 marco

When ACPI_ENABLED wasn't set but the devices were it had an unknown variable.

Pointed out by ian@.


# 1.24 03-Feb-2006 jordan

Rewrote aml parser yet again...
Fixed bus_space_map for i386/amd64
Added _BST and _BIF to acpibat to get status/info
ok marco@


# 1.23 20-Jan-2006 jordan

More evaluator changes
Removed old evaluator code
Fixed _HID value
ok marco@


# 1.22 19-Jan-2006 jordan

Added support for read/write of PCI Config space
Help jason@
ok marco@


# 1.21 18-Jan-2006 jordan

Added new evaluation routines
Added helper methods for buffer/field evaluation
Fixed broken indentation on dsdt.c

ok marco@


# 1.20 17-Jan-2006 jordan

Major overhaul of the aml parser. Evaluater temporarily disabled.

ok marco@


# 1.19 06-Jan-2006 grange

Check for ACPI control availability only if ACPI_ENABLE defined.


# 1.18 06-Jan-2006 grange

Informative error messages on attach, prettier dmesg.


# 1.17 06-Jan-2006 grange

printf -> dnprintf conversion for debug messages.


# 1.16 05-Jan-2006 grange

Find SLP_TYPx values and store them for future use in
acpi_enter_sleep_state().


# 1.15 05-Jan-2006 grange

Random KNF and cleanup


# 1.14 05-Jan-2006 grange

Provide symbolic names for the acpi sleeping states and use it.


# 1.13 28-Dec-2005 marco

Moved DEVNAME to a acpidev.h

ok jordan@


# 1.12 18-Dec-2005 sturm

add back comment about wrong code

ok marco


# 1.11 16-Dec-2005 marco

Remove test for kbd_reset. Prompted deraadt@, ok jordan@


# 1.10 16-Dec-2005 jordan

Added hooks for powerdown/power button press


# 1.9 16-Dec-2005 jordan

Cleaned up common power management register i/o
Added enter sleep state ioctl
Hooked power button interrupt
ok marco@


# 1.8 16-Dec-2005 marco

Remove autoconf chatter


# 1.7 16-Dec-2005 jordan

Moved DSDT AML parsing into parent ACPI driver
ok marco@


# 1.6 14-Dec-2005 marco

Add AC device glue.


# 1.5 14-Dec-2005 marco

Adjust debug printing levels to something more sane.


# 1.4 13-Dec-2005 marco

Add glue to attach devices to apci.


# 1.3 07-Dec-2005 marco

KNF


Revision tags: OPENBSD_3_8_BASE
# 1.2 10-Jul-2005 grange

Spacing cleanup.


# 1.1 02-Jun-2005 tholo

Start on a basic ACPI framework -- does not do much more than read out the
ACPI tables into kernel memory and attach ACPI and HPET timers currently.

In order to test this code, enabling the devices in GENERIC as well as
the ACPI_ENABLE option is needed. This code does not do any thermal
control yet, so this should be done with care depending on the platform.

In the tree so more people can contribute to making this more fully
featured.

Ok niklas@ grange@ tedu@


# 1.387 10-Jun-2020 jca

Give userland a consistent battery and A/C state after resume

Previously we refreshed the state of acpiac(4) and acpibat(4)/acpisbs(4)
by queueing fake ACPI events to be processed by the acpi thread.
acpibat(4) using ACPIDEV_POLL meant that its status could be refreshed
first, resulting in an APM_POWER_CHANGE event sent to userland before
the state of acpiac(4) was up to date. Because of this, apmd(8) could
see A/C unplugged when it was in fact plugged, possibly leading to
a wrong decision.

Instead of using fake ACPI events, refresh the status of acpiac(4) and
acpibat(4)/acpisbs(4) right away at DVACT_WAKEUP time. To reuse the
existing refresh code of those drivers, make their ACPI *_notify()
handlers responsible for sending APM_POWER_CHANGE events instead. Also
queue an APM_POWER_CHANGE event right after resume to retain existing
behavior.

This fixes the consistency of data shown to userland, and makes those
drivers less special.

Initially discussed last year (!) with mpi@ at Miod's place.
Feedback and ok jcs@, deraadt@ agrees with the use of DVACT_WAKEUP here.


# 1.386 29-May-2020 deraadt

dev/rndvar.h no longer has statistical interfaces (removed during various
conversion steps). it only contains kernel prototypes for 4 interfaces,
all of which legitimately belong in sys/systm.h, which are already included
by all enqueue_randomness() users.


# 1.385 14-May-2020 kettenis

Bring back the acpi(4) changes. This time around only use the ACPI _CRS
information on ACPI 5.0 and later.

ok krw@, patrick@


# 1.384 11-May-2020 jca

More accurate remaining power reporting with disparate batteries

Overall remaining power was computed as the average of the remaining
power of each battery, in percents. This doesn't work with batteries of
different capacity, so use the total capacity and remaining power
instead.

Reported by lidstah, successful tests by lidstah and benno@, ok kettenis@


# 1.383 08-May-2020 kettenis

Move parsing of _CRS into acpi(4) and pass the parsed address and interrupt
information in struct acpi_attach_args.

ok mpi@


Revision tags: OPENBSD_6_7_BASE
# 1.382 14-Apr-2020 kettenis

Implement acpi_getprop(), the ACPI equivalent of OF_getprop().

ok patrick@


# 1.381 12-Apr-2020 kettenis

Add support for the _CCA method. This method indicates whether DMA is
cache-coherent or not. To implement this, acpi(4) gets two bus_dma tags
and passes the appropriate one when attaching devices based on _CCA.
On i386/amd64, where for all practical purpose DMA is always cache-coherent,
the two tags are the same. But on arm64 they are distinct.

ok patrick@


# 1.380 07-Apr-2020 visa

Abstract the head of knote lists. This allows extending the lists,
for example, with locking assertions.

OK mpi@, anton@


# 1.379 03-Apr-2020 mpi

Fix incoherencies of errors returned by various *kqfilter routines.

EOPNOTSUPP is returned when there's no "filterops" for a given fd.
EINVAL when the requested filter isn't supported by the "filterops".
ENXIO means the underlying device is no longer valid/present.

ok visa@


# 1.378 20-Feb-2020 visa

Replace field f_isfd with field f_flags in struct filterops to allow
adding more filter properties without cluttering the struct.

OK mpi@, anton@


# 1.377 31-Dec-2019 visa

Use C99 designated initializers with struct filterops. In addition,
make the structs const so that the data are put in .rodata.

OK mpi@, deraadt@, anton@, bluhm@


# 1.376 31-Dec-2019 mpi

Convert infinite sleeps to {m,t}sleep_nsec(9).

ok kn@


# 1.375 31-Dec-2019 jsg

include sys/sensors.h for sensor use
don't depend on indirect inclusion from acpidev.h


Revision tags: OPENBSD_6_6_BASE
# 1.374 07-Sep-2019 kettenis

Revert acpipci(4) changes. Too many systems are broken. We'll try again
after 6.6 as been released. The acpireg.h change stays behind.


# 1.373 28-Aug-2019 kettenis

Use ACPI information to attach PCI busses like we do on arm64. There are a
few additional quirks though, and attaching the PCI busses is delayed to
replicate the existing code more closely. That may be changed in the
future. Also tweak how we handle MSI support and respect to ACPI flag
that says we shouldn't attempt to use MSIs.

Some fallout is expected.

ok patrick@


# 1.372 27-Aug-2019 deraadt

On amd64 the bootup and resume paths for SP/BP/SP were not performing
msr/pat, clock-startup, firmware upgrade, fpu initialization, and cpu
soft-state initialization in the same order. That kind of chaos is
crazy and fragile. Improve the acpi resume call sequence and unify order
of the operations.


# 1.371 02-Jul-2019 jcs

acpi_set_gpehandler: turn edge arg into flags, add support for
passing GPE_DIRECT which directly executes the supplied callback and
clears a level interrupt rather than going through acpi_taskq which
can be slow when other driver callbacks are in the queue.

This should not be used for GPE handlers that need to do anything
ACPI-related.

ok kettenis


# 1.370 10-Jun-2019 kettenis

Print proper ACPI version number.

ok mlarkin@


# 1.369 08-Jun-2019 kettenis

When establishing the mapping between ACPI device nodes and PCI devices,
skip PCI host bridges and devices that aren't there.

ok patrick@


# 1.368 07-Jun-2019 kettenis

Introduce acpi_getsta() and use it to eliminate some duplicated code.

ok patrick@


# 1.367 12-May-2019 kettenis

Make sure the extended PM register descriptions actually exist before
using them. Fixes machines with ancient ACPI 1.0 (such as recent
hypervisors).

ok deraadt@


# 1.366 12-May-2019 kettenis

Add support for mapping ACPI PM registers using the "extended" gasio
representation from the FADT. Mostly fixes the Lanner NCA-1510.

ok lteo@, mlarkin@, deraadt@


# 1.365 11-May-2019 lteo

Fix incorrect error message.

ok kettenis@


# 1.364 08-May-2019 tedu

free size. the allocation is the header plus the length.


Revision tags: OPENBSD_6_5_BASE
# 1.363 04-Apr-2019 kettenis

Avoid potential null-pointer dereference. Found by Kent R. Spillner.

ok deraadt@, jsg@


# 1.362 20-Jan-2019 jsg

fix SMALL_KERNEL build after changes in rev 1.361
reported by naddy@ ok deraadt@


# 1.361 19-Jan-2019 tedu

Add a pwraction sysctl that controls what the power button does on acpi.
By default, nothing changes -- shutdown is initiated. But allows turning
power button into a sleep button if desired.
(grudging) ok from a few parties


# 1.360 26-Oct-2018 kettenis

Add acpipci(4) on amd64. For now this only calls the PCI-specific _OSC
method to let the ACPI implementation know what features we support.


Revision tags: OPENBSD_6_4_BASE
# 1.359 30-Aug-2018 kettenis

Move softbutton check before the check that skips attaching certain acpi
devices. Fixes regression caused by matching on _CID in addition to
matching on _HID.

ok matthieu@, mlarkin@


# 1.358 25-Aug-2018 kettenis

Match ACPI devices based on _CID if no match for _HID is found.

ok mpi@


# 1.357 19-Aug-2018 kettenis

Add support for multiple PCI segments. Only really implemented for arm64
for now as amd64/i386 firmware still caters for legacy OSes that only
support a single PCI segment.

ok patrick@


# 1.356 03-Aug-2018 kettenis

Let ahci(4) match on _CLS instead of _HID when attaching at acpi(4). Avoids
having to add many more _HID entries to the match table.

ok deraadt@, mlarkin@


# 1.355 10-Jul-2018 kettenis

Make legacy interrupts work in acpipci(4).

ok patrick@


# 1.354 05-Jul-2018 kettenis

Add acpipci(4), a driver that supports generic ECAM-compatible PCI host
bridges based on information provided by ACPI.

ok mlarkin@


# 1.353 04-Jul-2018 kettenis

Properly pass around the PCI "chipset tag" in acpi(4) and refactor
acpimcfg(4) to call an MD initialization functions that sets up a tag for
PCI ECAM.

ok guenther@, mlarkin@, krw@


# 1.352 01-Jul-2018 kettenis

Move acpi(4) attach glue into acpi_machdep.c.

ok guenther@, deraadt@


# 1.351 01-Jul-2018 kettenis

Add support for _DSD Device Properties.

ok mlarkin@


# 1.350 30-Jun-2018 kettenis

Add intr_enable() function, intended for MI use to amd64 and i386 and use
this in the acpi(4) suspend/resume code paths.

ok deraadt@


# 1.349 29-Jun-2018 kettenis

Use <stdint.h> types.

ok mpi@, jung@, krw@, deraadt@


# 1.348 29-Jun-2018 mlarkin

ACPI: Allow (with warning) GPE handler reassignment, instead of returning
a failure code.

ok kettenis


# 1.347 26-Jun-2018 mlarkin

knf


# 1.346 26-Jun-2018 kettenis

Let acpi(4) pass down the bus dma tag.

ok deraadt@


# 1.345 25-Jun-2018 kettenis

Reorganize acpi(4) code a bit in preparation for upcoming arm64 support.

ok deraadt@


# 1.344 20-May-2018 kettenis

Wake up the acpi thread after scheduling a gpio event.

ok mpi@


# 1.343 17-May-2018 kettenis

Sprinkle some #ifndef SMALL_KERNEL such that RAMDISK kernels cuild again.

pointed out by stsp@


# 1.342 17-May-2018 kettenis

Implement GenericSerialBus OpRegion support.

ok mlarkin@


# 1.341 27-Mar-2018 kettenis

Add acpicmos(4), a driver that implements SystemCMOS OperationRegion
access support. This fixes machines where the AML doesn't check whether
support for this OperationRegion type has been registered by the OS.

ok mlarkin@


Revision tags: OPENBSD_6_3_BASE
# 1.340 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.339 10-Feb-2018 deraadt

Syncronize filesystems to disk when suspending. Each mountpoint's vnodes
are pushed to disk. Dangling vnodes (unlinked files still in use) and
vnodes undergoing change by long-running syscalls are identified -- and
such filesystems are marked dirty on-disk while we are suspended (in case
power is lost, a fsck will be required). Filesystems without dangling or
busy vnodes are marked clean, resulting in faster boots following
"battery died" circumstances.
Tested by numerous developers, thanks for the feedback.


# 1.338 08-Feb-2018 deraadt

After flushing disks, try again to release all memory. We don't need to
save dirty memory to the hibernate space.


# 1.337 08-Feb-2018 deraadt

Toss all releaseable memory, because fragmentation can get in the way
of allocating the hibernate playpen.


# 1.336 08-Feb-2018 deraadt

sr_quiesce() is a new approach for ensuring that softraid drains
output to the disks.

This is part of a larger suspend/resume filesystem-safety diff, which
has been worked on for a couple of months already. Tests by job, krw,
beck, benno, and others. Sometimes even by snapshot users...


# 1.335 29-Nov-2017 kettenis

Add support for GPIO-signaled events.

ok mlarkin@


# 1.334 16-Nov-2017 jcs

Add PCI attachment for dwiic(4) needed by Intel 100 Series machines

ok kettenis


# 1.333 14-Nov-2017 kettenis

Add a few more PNP IDs to the skip lists.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.332 17-Aug-2017 stsp

Make dwiic(4) attach its ACPI parent dependencies.
This change makes the Asus E200HA keyboard work.

Original analysis and patch by Cesare Gargano
ok kettenis@


# 1.331 09-Aug-2017 dcoppa

Add "PNP0303" (8042 PS/2 Controller) to acpi_isa_hids[]

ok kettenis@


# 1.330 22-Jul-2017 jcs

acpibat: look for _BIX first before falling back on _BIF

ACPI 4.0 deprecated _BIF for battery status so some newer machines
are exporting _BIX which extends _BIF's package with a few extra
fields. When using _BIX, export the new discharge cycle count as a
sensor.

feedback from kettenis and anton
ok deraadt


# 1.329 20-Jul-2017 kettenis

Start a list of isa(4) device names such that we can avoid printing them
as "not configured".

ok mikeb@, millert@, deraadt@


# 1.328 08-Apr-2017 deraadt

Sensors are run as callbacks inside tasks. During suspend a sensor
could be running inside a driver that will be force-detached, or due
to tsleep end up disrupting the softstate/hardstate contract. At
suspend time, quisce all these callbacks by waiting for completion.
This issue has never been observed for real, but may be implicated in
suspend/resume failures.
ok kettenis guenther mlarkin


# 1.327 08-Apr-2017 deraadt

Two prototypes for acpi_maptable; merge to one place


Revision tags: OPENBSD_6_1_BASE
# 1.326 27-Mar-2017 deraadt

Now that hibernate_alloc() only has clean success/failure, don't
need to call hibernate_free() to clean up a partial mess.
ok mlarkin kettenis


# 1.325 13-Mar-2017 deraadt

allow ramdisks to build; ok jcs


# 1.324 12-Mar-2017 jcs

try to attach acpisbs first and if successful, don't attach acpibat


# 1.323 02-Mar-2017 natano

Add a new sysctl machdep.lidaction. The sysctl works as follows:

machdep.lidaction=0 # do nothing
machdep.lidaction=1 # suspend
machdep.lidaction=2 # hibernate

lidsuspend is just an alias for lidaction, so if you change one, the
other one will have the same value. The plan is to remove
machdep.lidsuspend eventually when people have upgraded their
/ets/sysctl.conf.

discussed with deraadt, who came up with the new MIB name
no objections mlarkin
ok stsp halex jcs


# 1.322 28-Feb-2017 natano

Untangle abstract sleep modes from ACPI sleep states. This paves the way
for further work in the area.

idea and ok deraadt
ok mlarkin


# 1.321 22-Feb-2017 jcs

add acpisbs, an acpi smart battery subsystem driver reading data
over smbus

currently disabled because it conflicts with acpibat

ok deraadt, kettenis


# 1.320 14-Jan-2017 kettenis

Revert aml_rdpciaddr changes; breaks several machines that were working
before.


# 1.319 11-Jan-2017 jsg

Increase the size of the buffers used for _HID and _CID strings to be
able to fit "Hyper_V_Gen_Counter_V1".

ok mlarkin@ deraadt@


# 1.318 08-Jan-2017 kettenis

Make aml_rdpciaddr() work for for devices behind bridges. The code now
uses the mapping between AML nodes and PCI devices that we establish.
Because _INIT methods may end up calling aml_rdpciaddr(), make sure we
create that mapping early. Also handle devices that aren't actually present.
These devices are now included in the mapping and reads will return an
all-ones patterm whereas writes are a no-op..

ok mlarkin@


# 1.317 25-Oct-2016 pirofti

Add the resource index to aml_parse_resource() callback function.

This is needed by an upcoming acpiec commit that handles machines breaking
the current ACPI specifications.

Change suggested by and ok kettenis@, guenther@


# 1.316 18-Sep-2016 guenther

If the APIC table (MADT) isn't present then ci_acpi_proc_id won't be set
when acpicpu tries to attach, leading to a NULL dereference. The ACPI
processor object on the Dells where this was reported is bogus and useless
anyway so apply the same test when deciding whether to attach to processor
objects as acpicpu would when matching them to cpus.

Problem reported by Eivind Eide (xenofil (at) gmail.com) and
Johan Huldtgren (johan+openbsd-bugs (at) huldtgren.com).
Testing by Eivind Eide.
ok kettenis@ mlarkin@


# 1.315 03-Sep-2016 naddy

Write the system time back to the RTC every 30 minutes.
This fixes the problem that long-running machines which were not
shut down properly would reboot with a badly offset system time.

hints and ok kettenis@


# 1.314 31-Aug-2016 mlarkin

Don't put a device in the wake device list if _STA says it's not there.

Fixes at least one machine that woke up from sleep / poweroff immediately.

ok kettenis@, tested by kettenis and martijn@


# 1.313 28-Jul-2016 kettenis

Store the acpi processor ID/UID in struct cpu_info, and use it to attach
acpicpu(4) drivers to the right cpu(4).

ok mlarkin@, guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.312 10-Jun-2016 kettenis

The power management timer has been made optional in ACPI 5.0A, so don't
attempt to attach acpitimer(4) if the timer isn't present.

Pointed out by jcs@

ok jcs@


# 1.311 08-May-2016 kettenis

Make sure devices listed by a device's _DEP method are attached before
we attach the device itself. The _DEP methode, introduced in ACPI 5.0,
is a hint that the device in question depends on OpRegion support from
the devices listed by _DEP, so we have to order them the proper way.
To prevent us from attaching those devices again when we encounter them
later walking down the device tree, keep track of our attempts to
attach a device driver to them using aflag in the aml_node structure.

ok guenther@


# 1.310 25-Apr-2016 pirofti

We attach acpihpet(4) and acpiec(4) without using the HID, so do not
report it in the dmesg.

OK deraadt@, mlarkin@.


# 1.309 02-Apr-2016 kettenis

Reduce dmesg spam by knocking out some acpi hardware IDs that don't need a
driver.

ok mpi@


# 1.308 29-Mar-2016 kettenis

Check if a device is present (using _STA) in acpi_foundhid. This prevents
us from reporting devices as "not configured" that aren't actually present,
and allows us to remove duplicated code from several drivers.

ok deraadt@, ok jsg@


# 1.307 29-Mar-2016 kettenis

Implement a cleaner way to prevent acpivout(4) from attaching if we have
a platform-specific method of controlling the backlight.

ok millert@, mlarkin@, jung@


# 1.306 28-Mar-2016 kettenis

Adding each and every new acpi device driver to acpi_foundhid() is
gettinga bit of the burden. And it really isn't how our autoconf
framework is supposed to work for busses that can be enumerated. All
the drivers already check for a matching _HID in their attach
function. So we can just drop the checks in acpi_foundhid() and let
the drivers sort themselves out.

Print a "not configured" line for devices for which we don't attach adriver.
This may be a bit much as there are devices for which we will not have a
driver. This will be removed if it causes too much dmesg spam.

ok deraadt@, mlarkin@


Revision tags: OPENBSD_5_9_BASE
# 1.305 17-Jan-2016 jsg

Take acpi_foundhid() outside of #ifndef SMALL_KERNEL so the ramdisk
kernel can find and attach dwiic(4) and sdhc(4).


# 1.304 17-Jan-2016 jsg

remove an unused global variable


# 1.303 14-Jan-2016 kettenis

Attach dwiic(4) on the Intel Bay Trail i2c controllers as well.


# 1.302 12-Jan-2016 kettenis

Improve emulation of PM registers on Hardware-redcued ACPI by emulating
SPL_TYPx, SLP_EN and WAK_STS using the new SLEEP_STATUS_REG and
SLEEP_CONTROL_REG registers. Makes the transition into S5 (powerdown) work
on the ASUS X205TA.

ok deraadt@, mlarkin@


# 1.301 12-Jan-2016 jcs

Add dwiic, a driver for the Synopsys DesignWare i2c controller found
on the Samsung ATIV Book 9 laptop. This initial version only
supports ACPI config/attachment.

Add ihidev, a HID-over-i2c driver largely based on uhidev. dwiic
handles attaching ihidev devices found in ACPI.

Add ims, a HID-over-i2c mouse/trackpad driver to get basic cursor
and button functionality from HID-compliant i2c trackpads.

ok kettenis deraadt


# 1.300 11-Jan-2016 kettenis

Make sdhc(4) attach to hardware IDs "80860F14" and "PNP0FFF".


# 1.299 09-Jan-2016 kettenis

Initial support for Hardware-Reduced ACPI. This skips the legacy mode checks
by emulating the PM_CNT register such that SCI_EN is always enabled. It also
skip attaching acpitimer(4) since the relevant registers are no longer there.

ok deraadt@, mlarkin@


# 1.298 06-Jan-2016 kettenis

Follow the spec and check the SCI_EN bit to see if ACPI has already
been enabled and don't bother enabling it again if it is. This should fix
systems that don't implement legacy mode and therefore set ACPI_ENABLE and
ACPI_DISABLE to zero.

ok mlarkin@, yasuoka@


# 1.297 23-Nov-2015 reyk

Back out the CRITICAL change, turn it back from 10% to 25%. Some
people with old/broken batteries didn't like this change, and the
underlying heuristics are wrong.


# 1.296 22-Nov-2015 reyk

The "CRITICAL" APM battery level, that is exported/faked by the ACPI
stack, is currently set to 25%. With 25% battery left, a typical
modern laptop will have at least one hour runtime left. It is an
arbitrary value, and other APM power layers in the kernel set CRITICAL
to something like 5% or 10%, so set it to 10% for now.

OK mlarkin@


# 1.295 28-Sep-2015 deraadt

In low-level suspend routines, set cold=2. In tsleep(), use this to
spit out a ddb trace to console. This should allow us to find suspend
or resume routines which break the rules. It depends on the console
output function being non-sleeping.... but that's another codepath which
should try to be safe when cold is set.
ok kettenis


# 1.294 06-Sep-2015 deraadt

clarify malloc arguments (base + length...)


# 1.293 28-Aug-2015 deraadt

fairly simple sizes for free(); ok tedu


# 1.292 20-Aug-2015 kettenis

Respect the access size when reading or writing to pci config space.
This version also makes sure writes are properly aligned, preventing
the panics seen with the previous incarnation of this change.

Fixes battery status passthrough in vmware, brightness keys on my x220
and who knows what else.

ok mlarkin@, deraadt@


Revision tags: OPENBSD_5_8_BASE
# 1.291 04-Aug-2015 deraadt

If we are setting ACPI_PM1_BM_RLD do it only once, and remember to
repeat this action at resume time.
ok mlarkin guenther


# 1.290 29-Jul-2015 deraadt

Something subtle in previous revision is locking up suspend or booting
on some laptops. Mailing lists threads mention x220 and Asus machines.
Reverting for now.


# 1.289 21-Jul-2015 kettenis

iRespect the access size when reading or writing to pci config space.
Fixes battery status passthrough in vmware and who knows what else.

Tested by many.

ok mlarkin@, deraadt@


# 1.288 17-Jul-2015 mlarkin

fix comments


# 1.287 26-Mar-2015 jsg

Use the 0x10 extended capability to determine if the trackstick
buttons are wired to the trackpad and need to be re-routed to the
trackstick. Without this change the buttons on 2015 thinkpads
get picked up as extended buttons that show up as scroll up/down.

Remove the X1 Carbon 2015 (LEN0048) and X250 (LEN0046) from the
top button area/soft buttons quirks list. Also avoid using the
quirk list entirely if the capability bit is set.

Based on work from jcs@ with feedback from mpi@
ok mpi@ jcs@


# 1.286 20-Mar-2015 kettenis

The ACPI standard says that a PCI function number of FFFF can be used to refer
to all functions on a device. This makes no sense in the context of accessing
PCI config space. Nevertheless stsp@ has a machine with AML that tries to
do config space reads from a device where _ADR encodes function number FFFF.
This is almost certainly buggy AML, but since Linux (and presumably Windows)
boot just fine on this machine we need to work around this. Linux effectively
treats function FFFF as function 7, which isn't present on this machine.
Instead we simulate a read from a non-existent device by returning a bit
pattern of all ones. Writes to function FFFF will still panic.

ok mlarkin@


# 1.285 16-Mar-2015 deraadt

Reduce use of SMALL_KERNEL in acpi, bringing acpiec driver and more gpe
handling into RAMDISK. This is now possible because the install media
has ample room. The goal is to reduce special cases where we may be
skipping (unknown) important operations...
ok mlarkin kettenis


# 1.284 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.283 07-Feb-2015 deraadt

New framework that allows hibernate to pass in entropy from it's fresh
boot.
ok mlarkin


# 1.282 06-Feb-2015 mlarkin

Fix a hibernate crash on some machines due to unmapping a page that
may not have been mapped previously (in the failure to hibernate case).

Also ensure that the lowmem ptp is mapped in all cases (not just MP).

ok kettenis


# 1.281 17-Jan-2015 deraadt

resume_randomness() before spinning up other cpus...


# 1.280 15-Jan-2015 jsg

Define a new wscons mouse type for Synaptics clickpad devices that lack
physical buttons. This will be used if the acpi pnpid for the mouse
matches a list provided by Synaptics found in Linux.

Combined with the xenocara changes this will remove the need for an xorg
config file for the x240/t440/t540 generation of thinkpads.

ok matthieu@ shadchin@ kettenis@


# 1.279 11-Jan-2015 kettenis

Tolerate checksum errors in ACPI tables (but complain about them). Apparently
there are machines out there with broken BIOSen with such checksum errors in
thier RSDT tables. Windows runs fine on these machines, so it probably does
not check. Linux ignores checksum errors as well (and prints a warning).

ok deraadt@, krw@


# 1.278 18-Dec-2014 deraadt

suspend and resume the RNG (at what looks like the right spots)
ok reyk djm


# 1.277 09-Dec-2014 doug

Replace some malloc(n*size,...) calls with mallocarray().

ok tedu@ deraadt@


# 1.276 06-Dec-2014 mpi

If "machdep.lidsuspend" is not set, do not go back to sleep if the lid
is still closed when resuming. This allows docked laptop to be resumed
without opening them.

ok mlarkin@


# 1.275 23-Nov-2014 mlarkin

If a machine has no _LIDs, then none can be open. This diff fixes a problem
where machines without any _LIDs were going back to sleep after hibernate
due to the recent suspend-if-lid-closed-on-resume diff. Noticed on qemu,
which doesn't have any _LIDs.

discussed with deraadt


# 1.274 18-Nov-2014 krw

Nuke yet more obvious #include duplications.

ok miod@


# 1.273 08-Nov-2014 mlarkin

If resuming from sleep (zzz/ZZZ) and the lid is still closed, go back to
sleep. This prevents accidental lid flex or slight opening in a backpack
from waking the machine up and leaving it resumed (powered on).

ok deraadt@


# 1.272 26-Sep-2014 kettenis

Rework piglet and pig allocation. Currently the piglet gets allocated
deep down in the suspend path, where it is really hard to recover from
allocation failure. So allocate the piglet early on in the suspend path.
Also change the piglet and piglet allocation functions to use km_alloc(9)
instead of doing pmemrange magic. This removes a bunch of code which, in the
case of the piglet allocation, is broken since it results in a NULL pointer
dereference. Also switch the piglet allocation to not wait. If we can't
allocate 16MB of phys contig memory on a halfway modern machine we're almost
certainly under a lot of memory pressure and we're better off not trying to
hibernate anyway.

ok mlarkin@


# 1.271 25-Sep-2014 kettenis

Bring back rev 1.249. Now that mp_setperf() has been fixed, it should be
safe to restore perflevel when resuming. Should fix the issues people
see with the processor running at full speed heating up the machine while
the apm command reports that the CPUs are running at a low speed.

ok dcoppa@, deraadt@


# 1.270 23-Sep-2014 kettenis

Release the acpi lock around calls to wsdisplay_suspend() and
wsdisplay_resume(). The X server may adjust the display brightness just
before/after the VT switch when suspending/resuming. In fact, it may
already be blocked adjusting the display brightness for some reason
unrelated to the suspend request.

The VT switch request will eventually time out, but the delay is annoying
and the failed VT switch will lead to problems after resume.

Needed for a future update of the xf86-video-intel driver.

ok mlarkin@


# 1.269 19-Sep-2014 kettenis

Use config_suspend_all(9).


# 1.268 08-Sep-2014 kettenis

Clear the PME Status bit on PCI devices in response to Device Wake events.
Fixes the interrupt storm on the 2009 Mac mini with WOL enabled on its
nfe(4) interface.

ok mlarkin@


Revision tags: OPENBSD_5_6_BASE
# 1.267 20-Jul-2014 mlarkin

Support hibernating to softraid crypto volumes.

much help and ok from deraadt@


# 1.266 16-Jul-2014 mlarkin

Reenable hibernate RLE support and flush+zero all memory after unpack.


# 1.265 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.264 12-Jul-2014 kettenis

Add a function to drop all clean pages on the page daemon queues and call
it when we hibernate.

ok mlarkin@, miod@, deraadt@


# 1.263 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.262 11-Jul-2014 mlarkin

Flush the buffercache to 16MB on hibernate and restore its previous max
size (kern.bufcachepercent) on resume, for better hibernate performance.

ok beck@


# 1.261 10-Jul-2014 blambert

remove unneeded header

ok mlarkin@


# 1.260 01-Jul-2014 kettenis

Adding an additonal delay after each access to SystemMemory or SystemIO
during autoconf makes no sense.

ok deraadt@


# 1.259 23-Jun-2014 kettenis

SystemMemory is used to access memory mapped registers on some machines, so we
must use the bus_space(9) API to access it instead of memcpy(9). Also make
sure we properly align access to fields that need more than byte alignment.

ok guenther@


# 1.258 21-May-2014 mlarkin

Fix invalid / missing format specifiers in several dnprintf statements
(used only when ACPI_DEBUG is enabled)

From a diff on tech@ by Fabian Raetz, with a minor change by me, tested
on i386 and amd64 with ACPI_DEBUG enabled and disabled.


# 1.257 25-Apr-2014 mlarkin

Defer the free of the hibernate structures until later, when it is safe to
do so.

Freeing the hibernate structures too early leads to inadvertent calls into
splx when we aren't ready for them yet. Found when trapping splx calls in
the resume path.

discussed with deraadt@ at m2k14


# 1.256 13-Apr-2014 mlarkin

Fix a badly worded debug message that implied the entire machine was going
to sleep, instead of just the acpi thread going to sleep.


# 1.255 31-Mar-2014 mpi

Change the order of operations during a suspend/resume cycle and
call bufq_quiesce() after executing the DVACT_QUIESCE handlers.

This should be safe since no disk nor controller drivers have such
handler but it will allow us to detach sd(4) devices attached to a
USB bus.

Another benefit pointed out by kettenis@ is that drivers that need
to read a firmware from the disk should be able to do it at resume
time in a DVACT_WAKEUP handler.

ok kettenis@, deraadt@


# 1.254 13-Mar-2014 dlg

get rid of the assumption that the head of the alldevs list is the
"mainbus" device. this breaks when mpath is enabled because it
attaches before mainbus and therefore takes the head position.

have autoconf provide device_mainbus() which looks up mainbus_cd,
and use that instead.

discussed with deraadt who just wants mpath stuff to move forward
despite there being many ways to shine this particular turd.


Revision tags: OPENBSD_5_5_BASE
# 1.253 21-Feb-2014 deraadt

Do the lid control further on in the resume sequence, since this is
AML, and will do WAITOK operations. While cold, that is a bad idea.
We seem to be safe with respect to the spec, I do not see any event
debounce issues here.
ok kettenis, tested by many


# 1.252 20-Jan-2014 claudio

Instead of checking sc->sc_revision <= 1 check that the
fadt->hdr_revision <= 1. This is a better check since it checks
the version of the proper descriptor. This allows the alix APU
to reboot since those boards have currently a wrong sc->sc_revision.
Committing this now so that it can be tested easier.
With and OK jsg@ also fine by derradt@


# 1.251 24-Dec-2013 kettenis

If the FADT has its SMI_CMD set to zero, assume we're only ACPI-only hardware
and don't need to disable SMI ownership of the ACPI hardware registers.

ok mlarkin@


# 1.250 23-Dec-2013 kettenis

Revert previous commit. It makes resume hang on some MP systems running
GENERIC.MP.


# 1.249 16-Dec-2013 kettenis

Restore hw.setperf upon resume like we do for apm(4) on i386. Seems we need
to do this fairly late in the resume to avoid hangs.

ok deraadt@, mlarkin@


# 1.248 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


# 1.247 06-Nov-2013 mpi

Add support for Power Resources for Dx states and the necessary hook
for PCI devices. This hook should be called twice, before and after
changing the power state of a PCI device.

Before setting the device to the new state, the ACPI layer will notify
every power resources linked to the device for that state and make sure
they are turned "_ON". After changing the state of the device, it will
decrement the reference of every power resources linked to that device
for the old state and turn them "_OFF" if they are no longer referenced.

This fixes the no-USB after resume problem seen on various ThinkPad,
problem initialy diagnosed with Alexander Polakov.

ok kettenis@, deraadt@


Revision tags: OPENBSD_5_4_BASE
# 1.246 01-Jun-2013 mlarkin

Stop printing ACPI wakeup devices in dmesg after the 16th wakeup device.

Helps with VMware VMs that report hundreds of wakeup devices.

ok reyk@, deraadt@


# 1.245 31-May-2013 bluhm

Fix the build for a kernel without wd and pciide in its config.
OK deraadt@ tedu@


# 1.244 30-May-2013 deraadt

Enforce ca_activate tree-walks over the entire heirarchy for all events,
cleaning up some shutdown-hook related code on the way.
(A few drivers related to sparc are still skipped at kettenis' request)
ok kettenis mlarkin, tested by many others too


# 1.243 18-Apr-2013 deraadt

pin the acpi thread to the BSP, because AML and SMI cannot be trusted. I
was certain we were doing this already, but it turns out no...


Revision tags: OPENBSD_5_3_BASE
# 1.242 24-Dec-2012 guenther

In acpiioctl(), Return though the splx() path if suser() returns an error

ok deraadt@


# 1.241 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


# 1.240 04-Oct-2012 ehrhardt

Use information provided by ACPI to attach secondary PCI host bridges.

ok kettenis@


# 1.239 07-Sep-2012 kettenis

Add a new function called acpi_pci_min_powerstate() that returns the lowest
power state for a PCI device for the power state (currently S3 or S4) we're
transitioning into. This function is probably too simplistic; there is a lot
of (silly) complexity in the ACPI spec.

ok deraadt@


Revision tags: OPENBSD_5_2_BASE
# 1.238 13-Jul-2012 pirofti

Disable acpivideo(4) on ASUS laptops.

There are some models that panic whenever an event is received.
Some cope, some don't. In order to get this right acpiec(4) requires
major rework. Until then let machines boot.

Discussed with deraadt@.


# 1.237 11-Jul-2012 mlarkin

for now, restrict hibernate to root in the acpi ioctl kernel interface.
normal rules for controlling this interface through apmd still apply.

ok deraadt@


# 1.236 09-Jul-2012 deraadt

tiny little oops in untested hibernate block


# 1.235 09-Jul-2012 deraadt

clean up ioctl() case statements


# 1.234 09-Jul-2012 deraadt

acpi_disable_onegpe() is not used; ok pirofti


# 1.233 24-May-2012 kettenis

If _MIN > _MAX consider a WordBus resource to be invalid. Fixes a panic on
the IBM x3550-M4.

ok mikeb@


# 1.232 29-Mar-2012 mlarkin

Missing #ifdef in previous diff


# 1.231 29-Mar-2012 mlarkin

Prevent hibernation attempts on machines with unsupported disk controllers.
The same check is also performed later in the hibernate sequence, but by
then it's too late to cleanly unwind (presently).

ok deraadt


# 1.230 26-Mar-2012 deraadt

simplify conditionals in acpi_sleep_state()
ok mlarkin


# 1.229 26-Mar-2012 deraadt

hook in the hibernate request code; half of this diff is from mlarkin
ok mlarkin


Revision tags: OPENBSD_5_1_BASE
# 1.228 20-Sep-2011 deraadt

Very early in suspend while the clock is still ticking realtime,
save the clock back to the rtc. The zaurus already did this.
ok phessler miod kettenis


Revision tags: OPENBSD_5_0_BASE
# 1.227 02-Jul-2011 nicm

kqueue attach functions should return an errno or 0, not a plain 1. Fix
the obvious cases to return EINVAL and ENXIO.

ok tedu deraadt


# 1.226 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.225 16-Jun-2011 pirofti

Import acpitoshiba driver.

This is originally written by Hiroyuki Aizu, ported and integrated
in our tree by Javier Vazquez <javama4 AT gmail> with tweaks and
suggestions by me.

Import prodded by deraadt@ and the lack of testers on tech@.


# 1.224 27-Apr-2011 jcs

attach acpithinkpad to newer lenovo models like the x120e

ok deraadt@


# 1.223 22-Apr-2011 jordan

Fix uninitialzied variables and formatting strings (-Wxxx errors)


Revision tags: OPENBSD_4_9_BASE
# 1.222 02-Jan-2011 jordan

Re-add code for ACPI taskqueue, eliminates need for state vars in acpi softc
Serialize processing ACPI tasks for sleep/powerdown/GPE event
ok deraadt@


# 1.221 31-Oct-2010 guenther

Revert last commit: it breaks resume on Thinkpads
"then please back it out" deraadt@


# 1.220 26-Oct-2010 jordan

Add task queue for ACPI gpe and notify handlers
This fixes eject on Dell Latitude dock and an issue on the Dell Mini
battery update.
ok deraadt


# 1.219 07-Oct-2010 jordan

Create separate functions for enabling wake and runtime gpes.
Remove old suspend/resume gpewalk function
ok deraadt, mlarkin


# 1.218 05-Oct-2010 jordan

split acpi_enable_onegpe to separate enable/disable functions
ok deraadt


# 1.217 05-Oct-2010 jordan

Added acpi_disable_allgpes function to clear GPE status
ok deraadt


# 1.216 05-Oct-2010 deraadt

clean up acpi_softc use more


# 1.215 05-Oct-2010 deraadt

delete a bit of #if 0 code


# 1.214 05-Oct-2010 deraadt

hide use of our global acpi_softc pointer a bit better


# 1.213 29-Sep-2010 deraadt

grab the acpi thread lock very early in acpi thread startup; not that it
is likely that anyone will race us... but why be careless.
ok kettenis


# 1.212 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.211 29-Aug-2010 jasper

- add a check for FWRITE here too.

ok miod@


# 1.210 27-Aug-2010 kettenis

Make PCI Power Management optional, and only enable it when acpi(4) attaches.

ok deraadt@


Revision tags: OPENBSD_4_8_BASE
# 1.209 08-Aug-2010 kettenis

Close race between the acpi thread and wscons ioctls running in process
context. We might rip this out post-release and replace it with a solution
that serializes things in a more obvious way.

ok deraadt@, miod@ and "those crazy texans who ok anything"


# 1.208 08-Aug-2010 deraadt

Report the acpi sleep states when on a RAMDISK; ok krw


# 1.207 07-Aug-2010 kettenis

Enforce acpi register access rescrictions.

ok mlarkin@, deraadt@, tested by marco@


# 1.206 07-Aug-2010 deraadt

upon resume, notify all the acpiac and acpibat drivers to update
their status. some laptops do not do this automatically.
ok kettenis canacar


# 1.205 06-Aug-2010 marco

Always call _PSW on _LID devices that support it to enable lid open to
resume the machine. Conversely disable it when the machine wakes up.

Tested by several ok deraadt


# 1.204 06-Aug-2010 deraadt

As part of a revamp of the PM1/GPE code, I write this diff to look for
unmanaged EN & STS bits in the PM1 register at interrupt time and report
them. As a side effect this splits the STS acknowledgement into two writes
(for power, and sleep) instead of one. The printf that is added (to spot
unmanaged STS bits) has not yet been triggered as far as we know.

Before the "write to PM1 registers at the right offset" diff went in, this
was not neccessary. But newer thinkpads do not have a working soft power
button without this diff.

We have no idea why.
ok mlarkin kettenis


# 1.203 05-Aug-2010 deraadt

PM1 power button events were generating 'fake events' to acpibtn PWRB,
but if there is no such device these were being tossed. Instead, arrange
for all power button handlers to flag a softc variable which is processed
by the thread; so that one place will know to request a powerdown (psignal
to init)
ok marco mlarkin


# 1.202 05-Aug-2010 deraadt

rename a variable which is annoyingly poorly named


# 1.201 05-Aug-2010 mlarkin

Write to the correct offset for various ACPI pmreg registers. Writing to
the correct register does properly clear the wake status bits and fixed
event bits, which allows some machines to have more than 1 suspend cycle.

bug spotted by deraadt@

tested by deraadt@ and myself on a half-dozen machines and others on
hackers@

ok deraadt@, marco@


# 1.200 05-Aug-2010 deraadt

Fix the power button. The pm1 register is 16 bits in size.
My mistake, a while back.
tested by krw


# 1.199 04-Aug-2010 kettenis

Print supported sleep states.

ok marco@, phessler@, deraadt@


# 1.198 03-Aug-2010 mlarkin

Use the proper flag for re-enabling certain hardware events (power btn,
sleep btn). The (incorrect) flag prevoiously used coincidentally had the
same value, so this shouldn't cause any different behavior than before.

tested on a variety of machines (i386, amd64, sp, mp) - no behavioral
change seen

ok deraadt@


# 1.197 28-Jul-2010 marco

Make all gasio failures look and behave the same.


# 1.196 28-Jul-2010 mlarkin

Delay a little longer on suspend before giving up. Fixes machines that
sometimes don't suspend because the sleep transition takes too long
(symptoms like failing to sleep but the backlight stays on).

ok deraadt@


# 1.195 27-Jul-2010 deraadt

remove XXX; EC can now be attached early when needed; ok jordan


# 1.194 27-Jul-2010 deraadt

more brackets feels good


# 1.193 27-Jul-2010 deraadt

XXX annotate a block in acpi_enter_sleep_state() that is pretty worrying


# 1.192 27-Jul-2010 deraadt

horrific indentation that nearly made me puke


# 1.191 27-Jul-2010 mlarkin

Set the sleeping indicator light on machines that support it, in case SMI
doesn't do it for us. Shows the 'moon' LED on older Thinkpads when
sleeping.

Tested on T43p, W500, T510 by me and on X61s by deraadt@.

ok deraadt@


# 1.190 27-Jul-2010 jordan

Early initialization of acpiec if ECDT table exists
Fixes hang when booting thinkpads while docked
ok deraadt


# 1.189 26-Jul-2010 deraadt

remove merge error which was commited


# 1.188 26-Jul-2010 pirofti

Add support for Sony ACPI hotkeys via a new driver: acpisony(4).

Currently it only works for the suspend button and tries to do right for
the brightness events, but I haven't found a Sony laptop that like to do
right so far.

In the future I want to make the brightness keys work on all Sony's and
also add support for the zoom hotkeys and whatever other funky keys I can
find on those things.

Okay deraadt@.


# 1.187 22-Jul-2010 deraadt

pretty large cleanup of SMALL_KERNEL


# 1.186 21-Jul-2010 deraadt

in the acpi GPE handler, late ack level interrupts not edge interrupts.
Duh.


# 1.185 21-Jul-2010 deraadt

Merge the edge and level gpe handlers into one (now possible since the
gpe structure tells us whether the gpe is edge or level)


# 1.184 21-Jul-2010 deraadt

delete #if 0 code we are not going to use; ok marco


# 1.183 20-Jul-2010 deraadt

make the mask code for acpi_enable_onegpe() match other similar code


# 1.182 20-Jul-2010 deraadt

Handle the acpi interrupt controller with proper edge/level handling,
wrapped in spltty to avoid racing against the interrupt controller.
Repair the gpe bit masking code while there, and do operations in the
right order
ok kettenis mlarkin, help from jordan at figuring out the order of
operations.


# 1.181 20-Jul-2010 matthew

When we're preparing to go to sleep, only call bufq_restart() if other
preparations failed.

ok kettenis@, thib@, deraadt@


# 1.180 19-Jul-2010 deraadt

permit compilation with ACPI_DEBUG


# 1.179 19-Jul-2010 deraadt

there is no need for a seperate dsdt_softc pointer; dsdt does not have a
softc but is just a part of the single acpi driver.
ok jordan


# 1.178 19-Jul-2010 deraadt

acpi_delay() is no longer needed; ok jordan


# 1.177 18-Jul-2010 mlarkin

Clear event status on resume, to avoid going back to sleep/reboot/shutdown
immediately after resume. Tested by a bunch of people on hackers@ on
various hardware. Fixes at least Toshiba NB200 and Gateway LT3103u.

ok marco


# 1.176 13-Jul-2010 deraadt

rename some thread related interfaces so they are logical


# 1.175 10-Jul-2010 jordan

Verify _ADR bdf is correct (some systems return 0xffff)
Works on Dell x9xx and Studio 1555


# 1.174 09-Jul-2010 jordan

Clear the noise


# 1.173 08-Jul-2010 jordan

Add mapping for ACPI device to PCI bdf (match autoconf tree)
Simplify resource parsing function to use buffer argument
Convert namespace linked lists to use queue macros
ok marco@, deraadt@


# 1.172 06-Jul-2010 deraadt

Use spltty() to lock downcalls from apm against the information being
modified by the acpi thread.
ok kettenis


# 1.171 06-Jul-2010 deraadt

Don't use a workq to do the suspend, because it races aginst the acpi
thread. Instead, just tell the acpi thread to do the suspend for us.
This makes apmd & zzz work correctly. While here, have acpithinkpad
attempt to post the event to apm before prompting the suspend itself.
ok kettenis marco mlarkin


# 1.170 05-Jul-2010 mlarkin

Reset register SCI_EN on resume. The ACPI spec clearly states we don't have
to do this, but many machines don't, or forget to, which leads to all sorts
of problems on resume (broken battery stats, overheating, failure to resume
more than once, etc).

tested by many, ok marco@, deraadt@


# 1.169 01-Jul-2010 thib

do bufq quiesce again, since vnd/the bufq_done call in
biodone has been fixed.


# 1.168 01-Jul-2010 jordan

Backout recent AML changes, breaks on R210 and others


# 1.167 01-Jul-2010 jordan

Simplified aml_parse_resource call, cleanup code
ok marco


# 1.166 01-Jul-2010 jordan

Fix subordinate bus # for multi-root PCI buses
ok marco, kettenis


# 1.165 30-Jun-2010 thib

Disable/partially backout the bufq quiesce changes as this
is causing havoc with vnds and release must be buildable.


# 1.164 29-Jun-2010 deraadt

Fix ramdisks (new code was placed inside a SMALL_KERNEL chunk)


# 1.163 29-Jun-2010 jordan

Add support for mapping ACPI to PCI devices
ok kettenis, deraadt


# 1.162 29-Jun-2010 kettenis

Call bufq_quiesce() on suspend to stop all I/O and bufq_restart on resume.
Makes suspend/resume work much better while doing disk I/O.

ok mlarkin@


# 1.161 29-Jun-2010 tedu

60 is a more reasonable guess for number of minutes in an hour than 100.
spotted by TeXitoi. ok deraadt


# 1.160 27-Jun-2010 jordan

Add _acpi_matchhids routine, precursor to pci device match


# 1.159 27-Jun-2010 mlarkin

Clear ACPI fixed event status on resume (power buttons/etc) so that some
machines don't immediately resume after suspending to S3.

Tested by kettenis@ and I on 5 or 6 machines, deraadt@ "yeah, looks good"


# 1.158 27-Jun-2010 jordan

Cleaned up acpi_maptable, adds to table list directly


# 1.157 07-Apr-2010 deraadt

If we have done a wsdisplay_suspend(), and ACPI decides it does not want to
suspend, we must roll back by calling wsdisplay_resume() or the virtual
console switching will be locked forever
ok oga


# 1.156 07-Apr-2010 kettenis

Make suspend/resume work on MP machines (running an MP kernel). Joint work
from mlarkin@ and me, with some amd64 fixes thrown in by deraadt@

ok marco@, deraadt@, pirofti@, mlarkin@


# 1.155 31-Mar-2010 kettenis

Bring /dev/apm support in line with apm(4). Make the suspend button send
a suspend request event to apmd(8) instead of suspending immediately. Also
keep track of whether /dev/apm and /dev/apmctl are currently open, such that
we can still suspend immediately if apmd(8) isn't running.

ok deraadt@, marco@, pirofti@, jsing@, oga@


# 1.154 30-Mar-2010 oga

Prevent the apmd/x races for good.

When we hit suspend time, go through all wsdisplays on the system. if
they are in mode MAPPED, but not MODE_DUMBFB then if possible do a full
vt switch to a !mapped vt, and prevent switching back until resume time.
This has to be called from MD code because this involves userland
running so that X can run the vt switch signal handler. This way, any
case where we are using the "poke registers from userland" model, we
will not be on the hardware when we go down, so the kernel can actually
handle thing properly.

Tested on several acpi laptops (by kettenis@ and ian@), x40 (me and
beck@ at LEAST) and zaurus (me). Maybe others, but if so I forgot who at
this time..

Idea from deraadt somewhere over the Faroe Islands (I thought of a
similar thing myself a while ago). Much prompting from him. Ok and
comments miod@


# 1.153 25-Mar-2010 oga

Fix & vs. && in the kqfilter code.

Based on a comparison with the apm code.

ok deraadt@, kettenis@


Revision tags: OPENBSD_4_7_BASE
# 1.152 26-Nov-2009 mlarkin

Visual (and on some machines, audio) feedback while the machine is
resuming via calls to aml _SST method. This also makes the "moon" LED on
thinkpads to blink during resume and cease when resume is completed.

ok pirofti@


# 1.151 26-Nov-2009 deraadt

Toggle into "cold" when we we disable interrupts, because tsleeping
in the AML interpreter and context switching into userland processes
tends to sometimes pop the ACPI trampoline page out of our address
space. Makes lots more machines work.
ok kettenis mlarkin


# 1.150 24-Nov-2009 jsg

in the resume path evaluate _WAK before dealing with device drivers
like the spec says, not after.

ok mlarkin@


# 1.149 24-Nov-2009 deraadt

abort suspends on SMP systems until that gets written; ok mlarkin


# 1.148 23-Nov-2009 mlarkin

pci should return the result of it's children's suspend/resume calls back
to its parent.

handle suspend failure case and unwind if devices have an activate function
that returns failure.

ok deraadt@, kettenis@


# 1.147 23-Nov-2009 mlarkin

acpi* devices need to call their children on suspend/resume.
ok deraadt@


# 1.146 23-Nov-2009 pirofti

Remove ACPI_SLEEP_ENABLED checks.

This enables by default the suspend/resume paths in the kernel.

Okay deraadt@.


# 1.145 23-Nov-2009 deraadt

knf and spacing that are super annoying


# 1.144 23-Nov-2009 mlarkin

Hook up Theo's new children suspend/resume function, as well as more ACPI
suspend/resume glue.

ok deraadt@


# 1.143 26-Oct-2009 deraadt

Do not do strncmp comparisons on dv_xname because longer device names which
look similar could arrive in the future. Instead, compare directly against
dv_cfdata->cf_driver->cd_name
Issue originally spotted by miod


# 1.142 26-Oct-2009 jordan

Verify checksum+address when loading ACPI tables.
Some systems had invalid entries in RSDT/XSDT.
ok marco@


# 1.141 23-Jul-2009 cnst

New aibs(4) driver for ASUSTeK AI Booster (ACPI ATK0110) hardware monitoring,
with sensor state support through limits provided by the ACPI.

Tested on several ASUS motherboards kindly networked by Sam Fourman Jr.

ok deraadt marco jordan


Revision tags: OPENBSD_4_6_BASE
# 1.140 03-Jun-2009 pirofti

Import acpi power resource driver. This is needed for suspend/resume logic.

Okay jordan@ and marco@.


# 1.139 03-Jun-2009 pirofti

Add acpivideo support. This adds brightness support for all laptops
except thinkpads, they will use the acpithinkpad driver. The driver is
also hooked into wsconsole. So brightness can be adjusted via:

$ wsconsctl display.brightness=<percentage>

This is very helpfull on some laptops that have a nasty bios and get two
steps instead of one when pressing the brightness button.

Tested on various dell, fujitsu, acer, samsung and other laptops.

Okay marco@, miod@. Suggestions from kettenis@.
Lots of reviews and help from miod@, thanks!


# 1.138 03-Jun-2009 jordan

Added ejectable bay for IDE cdroms
ok marco@


# 1.137 30-Apr-2009 marco

Oga changed apm ioctls and forgot to grep. Temporary work around by hitting
the sleep button when we get any request. This interface will chnage at
some point.
ok oga


# 1.136 19-Apr-2009 krw

Call acpi_sleep_walk() when powering down just like when entering
any other sleep state. This tells acpi wake devices to not wake up
the box until manual powering up resets their status. Makes my Dell
GX520 stay powered off in response to 'halt -p'.

In snaps for a while.

ok marco@ (several times) deraadt@


# 1.135 17-Apr-2009 pirofti

Add device ranking support for future sleep/wake developments.

okay marco@


# 1.134 11-Mar-2009 jordan

Cleanup.. use aml_evalinteger instead of aml_evalname
ok marco@


# 1.133 10-Mar-2009 jordan

Fix cut/paste error.. call _BFS not _PTS
ok marco@


Revision tags: OPENBSD_4_5_BASE
# 1.132 21-Feb-2009 marco

overzealous cut/paste; found by Vladimir Kirillov <proger@uaoug.org.ua>


# 1.131 20-Feb-2009 marco

Oops didn't mean for Debugger() to remain there.


# 1.130 19-Feb-2009 marco

suspend/resume bits so that we can develop this in tree. This is disabled.
code from mlarkin and me
help from art,toby,jordan and several others
ok jordan, go for it deraadt


# 1.129 10-Feb-2009 jordan

Fix null pointer for shutdown if acpi not enabled
ok marco@


# 1.128 04-Feb-2009 kettenis

Some systems that claim to be ACPI 2.0 apparently don't provide an XSDT
address. Make the code fall back on the RSDT on those systems.

From Joerg Niendorf <p3y80@internode.on.net>. Fixes PR 5986.


# 1.127 17-Jan-2009 krw

A couple of stray malloc/memset -> malloc(M_ZERO), a bit of knf and
lose a couple of unnecessary casts. No functional change.

Feedback from kettenis@

ok kettenis@ marco@


# 1.126 06-Nov-2008 marco

Collapse all sanity checks of hids into one function.

Originaly from jordan


Revision tags: OPENBSD_4_4_BASE
# 1.125 02-Jul-2008 fgsch

add acpivideo. for now almost an empty skel so others can work on
suspend/resume. eventually it will also manage output switching and
brightness where supported. prodded by marco@


# 1.124 11-Jun-2008 marco

Fix $OpenBSD$ for once and for all


# 1.123 08-Jun-2008 deraadt

aaa_dev was being set from an uninitialized variable; ok toby


# 1.122 07-Jun-2008 marco

Kill ACPIVERBOSE. It was never used and is worthless to boot.

comments jmc
ok toby


# 1.121 06-Jun-2008 marco

Shrink acpi some more for floppies and while here re-enable acpi on the
floppies. Fixes crazy sony vaio kettenis has.

tested by deraadt
ok deraadt jordan kettenis


# 1.120 01-Jun-2008 marco

Fix \n nit in SMALL_KERNEL.


# 1.119 01-Jun-2008 marco

Re-enable new parser. Lots of testing by various folks. Fits all media
now too.

ok kettenis deraadt


# 1.118 16-May-2008 dlg

back out jordans new parser.

it didnt have enough oks from the right people, it did break machines
that were previously working, and it bloated the RAMDISKS and install
media beyond the constraints of the media.

requested by deraadt@


# 1.117 14-May-2008 jordan

Adding new ACPI Parser code
Old guts of code still exists, needs to be torn out next
ok marco@
ok brad@


# 1.116 27-Apr-2008 jcs

add acpithinkpad(4), an acpi support driver for ibm/lenovo
thinkpads.

provides functionality that the bios did on older, non-acpi
thinkpads like respond to screen brightness keys and toggle the
bluetooth device on and off.

ok deraadt@ and marco@


# 1.115 24-Apr-2008 jsing

Introduce acpiasus(4), a driver for the ACPI based hotkeys found in many
ASUS laptops (including the ASUS EeePC) - largely based on NetBSD's
asus(4) driver. On the ASUS EeePC this allows us to enable/disable
wireless, change screen brightness and use the volume keys.

ok jsg@, weingart@


# 1.114 12-Apr-2008 kettenis

Get rid of acpi_s5 global variables; simply send SIGUSR2 instead of SUGUSR1
to tell init(8) to power down the machine.

ok krw@


Revision tags: OPENBSD_4_3_BASE
# 1.113 21-Jan-2008 jsg

Check against RESET_REG_SUP in FADT before doing the acpi reset dance
after all, not doing so appears to break more machines than it fixes.

ok marco@


# 1.112 05-Jan-2008 kettenis

Never attach more acpicpu(4) devices than cpu(4) devices. Always attach
the first acpicpu(4) device on non-MULTIPROCESSOR kernels, regardless of its
ACPI processor ID.

ok mikeb@


# 1.111 05-Dec-2007 deraadt

Hang acpi(4) below bios(4) instead of mainbus(4). This lets us move acpi(4)
before pcibios(4), and then.... if acpi(4) attaches, skip pcibios(4) since
it messes the machines up. Fixes claudio's HP dl320 G5, and almost assuredly
others.
ok kettenis


# 1.110 05-Dec-2007 deraadt

repair acpiopen/acpiclose and friends to handle the /dev/apmctl interface,
so that apmd(8) can actually work correctly against acpi(4), and thus,
even do the fancy -C/-A stuff; also tested by jmc


# 1.109 02-Dec-2007 fgsch

fix the trailing space for real now.


# 1.108 02-Dec-2007 fgsch

avoid trailing spaces on output; marco@ ok.


# 1.107 27-Nov-2007 kettenis

Fix off-by-one that made us attach acpicpu0 and acpicpu1 on GENERIC kernels.

ok marco@


# 1.106 25-Nov-2007 jsg

When available and ACPI is enabled use the ACPI reset method
to reboot. This is required for some quirky sis machines
and likely others to reboot.

Tested by many. 'commit early commit often' deraadt@


# 1.105 19-Nov-2007 deraadt

sigh, amd64 still uses that stupid acpi_enabled variable...


# 1.104 19-Nov-2007 deraadt

softc's come initialized to 0, you know...


# 1.103 19-Nov-2007 deraadt

store revision; from jsg


# 1.102 19-Nov-2007 kettenis

Initialize sc_facs earlier.

ok deraadt@, beck@, canacar@


# 1.101 16-Nov-2007 deraadt

fix the bus_space #define nightmare, so that amd64 and i386 are much more
uniform. as a result shared code like acpi needs less #ifdef's
ok marco kettenis


# 1.100 15-Nov-2007 deraadt

remove the #ifdef ACPI_ENABLE. the EC codepath is either safe enough (and
will be improved), or it is unsafe because AML assumes it must be there
ok almost everyone


# 1.99 15-Nov-2007 mikeb

Make acpicpu(4) attach to the existing CPUs only.

Problem reported by form@. Fix idea by kettenis@.
Lots of help from deraadt@. Tested by deraadt@, form@ and me.

ok deraadt marco


# 1.98 14-Nov-2007 deraadt

spacing


# 1.97 12-Nov-2007 deraadt

correct : vs , in attach-time messages


# 1.96 06-Nov-2007 deraadt

all ACPI_ENABLE kernels need acpi_interrupt()


# 1.95 06-Nov-2007 deraadt

move acpi_read_pmreg() and acpi_write_pmreg() out of SMALL_KERNEL so
that acpi ramdisks can compile (obvious solution)


# 1.94 05-Nov-2007 canacar

Establish the acpi interrupt earlier. Fixes problems with some
GENERIC.MP kernels not getting acpi interrupts. This is a
workaround until the real issue with late establishment is fixed.
ok weingart@, beck@, gwk@


# 1.93 03-Nov-2007 jordan

Added support for displaying wakeup devices
ok beck@,weingart@,gwk@


# 1.92 11-Oct-2007 kettenis

When walking the tree yo find _PRT methods, check whether the device is
actually enabled and functioning. Similar to what we do for _INI.

ok weingart@, beck@


# 1.91 08-Oct-2007 krw

More simple memset(,0,) -> M_ZERO changes. In this batch move to
size(*p) as the first malloc() parameter where p is declared locally
and thus easy to check. Add M_ZERO to gpe_table allocation in acpi.c
even though there is no obvious bzero or memset nearby.


# 1.90 13-Sep-2007 mk

Remove unnecessary memset() (see aml_evalnode(9)) and a bit of KNF.

ok toby


# 1.89 13-Sep-2007 weingart

Implement ACPI 6.5.1 spec tree walk for _STA and _INI.
Fixes mk's laptop. No regressions so far (thank you to the
testers).

ok gwk@, mk@, marco@


Revision tags: OPENBSD_4_2_BASE
# 1.88 17-Apr-2007 mk

Now that acpidock no longer hangs machines during boot (at least
according to the reports I've been getting) it should be safe to enable
it again, so remove the #if 0 stuff around it.

ok marco


# 1.87 11-Apr-2007 jordan

Added changes for C-State
ok marco@


# 1.86 26-Mar-2007 marco

Calculate minutes left more accurately.

From giovanni <qgiovanni@gmail.com>


Revision tags: OPENBSD_4_1_BASE
# 1.85 03-Mar-2007 mk

Stop looking for acpidock until we fix that stuff that breaks all pre-60
series ThinkPads with acpidock(4) enabled.


# 1.84 22-Feb-2007 marco

We were getting no interrupts in .MP. There are 2 fixes; one in
acpi_machdep.c and move the interrupt establishment down after ioapic
has been initialized.

ok jordan


# 1.83 21-Feb-2007 kettenis

Make an acpi-enabled GENERIC.MP work on systems that only support PIC mode.

ok marco@


# 1.82 19-Feb-2007 jordan

Added acpi_map_address function for mapping GAS/IO registers
ok marco@


# 1.81 18-Feb-2007 jordan

Added dynamic perfect hash for opcodes
ok marco@


# 1.80 18-Feb-2007 marco

Shave off another K pre compression.
Shuffle SMALL_KERNEL functions around so that we only have a single #ifndef


# 1.79 17-Feb-2007 marco

Add SMALL_KERNEL throught acpi to make it fit on boot media.
ok deraadt


# 1.78 13-Feb-2007 marco

Add smarter _INI code. This helps with some dissapearing devices.

ok kettenis jordan


# 1.77 31-Jan-2007 gwk

Introduce acpi_hasprocfvs thats "ACPI Has Processor Frequency and Voltage
Scalling" a global flag set by acicpu attach to indicate that ACPI is
handling fvs through the _PSS objects. This will fix disappearing
EST/powernow when booting kernels with acpi enabled, as acpicpu isnt
compiled into GENERIC yet. Also in cases where acpicpu is built in and
legacy EST/powernow work but acpi is missing the _PSS object, the legacy
routines will be used.

tested by dim@, and Nick Nauwelaerts <nick AT nauwelaerts.net>
ok marco@, dim@


# 1.76 22-Jan-2007 mk

Actually look for docking stations by looking for _DCK nodes.

ok marco


# 1.75 02-Jan-2007 marco

On some systems EC must be attached as the first device. Move EC from
generic _HID devices to its own routine to force first attach. Bug found
and debugged by joris@

Code mostly written by joris@


# 1.74 26-Dec-2006 marco

Add polling to devices that require it such as acpibat and acpitz.
Use the same tsleep/wakeup mechanism as the interrupt code to keep it non-
concurrent.


# 1.73 21-Dec-2006 deraadt

even more knf love


# 1.72 21-Dec-2006 marco

Remove some leftover acpiec gpe/interrupt code.


# 1.71 20-Dec-2006 deraadt

oops, name already used


# 1.70 20-Dec-2006 deraadt

rename __acpi_enable_gpe(); ok marco


# 1.69 20-Dec-2006 deraadt

acpi_unmap_pmregs() is not used, ok marco


# 1.68 19-Dec-2006 deraadt

simplify some low-level methods substantially (shrinking code); ok marco jordan


# 1.67 06-Dec-2006 mk

malloc(..., M_NOWAIT) with no NULL check is bad. If allocation fails,
complain, clean up, and bail out of attach.

input and ok gwk


# 1.66 28-Nov-2006 jordan

Fixed clearing arg for acpi_init_pic
ok kettenis@


# 1.65 27-Nov-2006 kettenis

Don't spam dmesg.


# 1.64 27-Nov-2006 jordan

Fixes for acpiprt routing
Post-parse fixup for namerefs
ok kettenis@


# 1.63 25-Nov-2006 marco

Don't handle acpiev events inside the gpe handler since this causes deadlocks.

ok jordan@


# 1.62 15-Nov-2006 kettenis

Hook up acpi apic interrupt routing support to generic acpi code.

ok jordan@


# 1.61 02-Nov-2006 marco

Kill acpiec attach function and add its functionality to the generic _HID
device attach

prodded and ok jordan


# 1.60 02-Nov-2006 marco

Fix comments to reflect reality.


# 1.59 19-Oct-2006 marco

Replace ACPI ioctls with APM ioctls. This makes ACPI talk the same protocol
as APM therefore userspace can now retrieve power information in the same
fashion as all other apm arches (zaurus, macppc, i386 etc).

To use force the use of acpi device like so: apmd -f /dev/acpi

ok jordan@


# 1.58 19-Oct-2006 jordan

Fixed GPE debug levels
Fixed ref counter on temporary bufferfields
DSDT parsing fully working on IBM Thinkpad
ok marco@


# 1.57 15-Oct-2006 jordan

Removed old GPE handler code remnants
ok marco@


# 1.56 12-Oct-2006 jordan

Massive ACPI changes
Fixed ACPI leaks
new ACPI API
ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.55 30-Jun-2006 gwk

KNF, No functional change,
"go at it" marco@


# 1.54 30-Jun-2006 jordan

Support for calling Embedded Controller via new GPE interface
ok marco@


# 1.53 30-Jun-2006 jordan

Added new code for GPE handling
Also works with ACPIEC; need to fixup acpiec.c to remove original interrupt handler
ok marco@


# 1.52 30-Jun-2006 jordan

Added offset argument for acpi read/write pmreg routines (new GPE code)
ok marco@


# 1.51 31-May-2006 todd

do not call acpi_powerdown() unless acpi has successfully initialized
ok gwk@ canacar@


# 1.50 29-May-2006 canacar

Add embedded controller support.
ok marco@ deraadt@ testing & ok gwk@


# 1.49 19-May-2006 canacar

Minimal thermal zone driver. Monitors thermal zone temperature,
shuts down the system if the 'critical temperature' is reached.
ok marco@


# 1.48 11-Apr-2006 marco

KNF.


# 1.47 11-Apr-2006 gwk

Add acpi_resume(struct acpi_softc *sc), which calls some AML methods,
detected at attach time, currently unused.

ok marco@


# 1.46 11-Apr-2006 gwk

Discover some ACPI sleep related methods, and call them at the
appropriate time in acpi_enter_sleep_state().

ok marco@


Revision tags: OPENBSD_3_9_BASE
# 1.45 26-Feb-2006 marco

Add acpicpu as a device.

ok jordan@


# 1.44 23-Feb-2006 marco

remove dead function.

ok jordan@


# 1.43 22-Feb-2006 marco

Print out all tables it found during autoconf. We'll need this data in
the future.


# 1.42 22-Feb-2006 marco

Make dmesg less verbose.

ok jordan.


# 1.41 22-Feb-2006 jordan

Added parsing of SSDT tables
ok marco@


# 1.40 22-Feb-2006 jordan

Added new aml_register to notify by device id
Moved powerdown code to button device
ok marco@


# 1.39 21-Feb-2006 marco

Readjust debug levels that crept in too low over the last few days.
KNF


# 1.38 21-Feb-2006 marco

Rig the power button so that the machine shuts down correctly.


# 1.37 20-Feb-2006 marco

Add acpibtn device. Remains disabled for now.


# 1.36 19-Feb-2006 jordan

Added code for aml_notify GPE notification callback
ok marco@


# 1.35 19-Feb-2006 grange

Fix acpi_enter_sleep_state() by replacing current ugly hack
with mostly correct sleeping state transition code.

ok brad@ marco@


# 1.34 19-Feb-2006 marco

jordan:
Enable interrupts.
Added peripheral code to make this work.

marco:
Replace softintr and other types of interrupts with a handler with context.
Fixed interrupt magic for GPEs.
Some KNF

This is work done by jordan and I but has to be a single commit.


# 1.33 17-Feb-2006 marco

Remove earlier introduced delays. We really only need them during autconf.

ok jordan@


# 1.32 17-Feb-2006 marco

Add a generic delay function as per the ACPI spec.

from jordan@


# 1.31 17-Feb-2006 marco

Use tsleep instead of delay when !cold.


# 1.30 17-Feb-2006 marco

Argh, we need delays between reads/writes. The backend reads (i2c most likely)
are so slow that values come back as 0 sometimes. We have to do this
despite the spec saying we shouldnt have to. Guess having a faster
parser than others can hurt sometimes.

Idea from jordan.


# 1.29 17-Feb-2006 jordan

Fixed code for handling _INI
Rearranged debug statements
ok marco@


# 1.28 16-Feb-2006 jordan

Added stub for calling _INI
Fixed aml_dowhile for NULL return
ok marco@


# 1.27 16-Feb-2006 jordan

Fixed parser/evaluator to retrieve battery info+state
Cleanup of parser
Dell laptops return battery status
ok marco@


# 1.26 06-Feb-2006 brad

fix a typo so this will compile on amd64.


# 1.25 04-Feb-2006 marco

When ACPI_ENABLED wasn't set but the devices were it had an unknown variable.

Pointed out by ian@.


# 1.24 03-Feb-2006 jordan

Rewrote aml parser yet again...
Fixed bus_space_map for i386/amd64
Added _BST and _BIF to acpibat to get status/info
ok marco@


# 1.23 20-Jan-2006 jordan

More evaluator changes
Removed old evaluator code
Fixed _HID value
ok marco@


# 1.22 19-Jan-2006 jordan

Added support for read/write of PCI Config space
Help jason@
ok marco@


# 1.21 18-Jan-2006 jordan

Added new evaluation routines
Added helper methods for buffer/field evaluation
Fixed broken indentation on dsdt.c

ok marco@


# 1.20 17-Jan-2006 jordan

Major overhaul of the aml parser. Evaluater temporarily disabled.

ok marco@


# 1.19 06-Jan-2006 grange

Check for ACPI control availability only if ACPI_ENABLE defined.


# 1.18 06-Jan-2006 grange

Informative error messages on attach, prettier dmesg.


# 1.17 06-Jan-2006 grange

printf -> dnprintf conversion for debug messages.


# 1.16 05-Jan-2006 grange

Find SLP_TYPx values and store them for future use in
acpi_enter_sleep_state().


# 1.15 05-Jan-2006 grange

Random KNF and cleanup


# 1.14 05-Jan-2006 grange

Provide symbolic names for the acpi sleeping states and use it.


# 1.13 28-Dec-2005 marco

Moved DEVNAME to a acpidev.h

ok jordan@


# 1.12 18-Dec-2005 sturm

add back comment about wrong code

ok marco


# 1.11 16-Dec-2005 marco

Remove test for kbd_reset. Prompted deraadt@, ok jordan@


# 1.10 16-Dec-2005 jordan

Added hooks for powerdown/power button press


# 1.9 16-Dec-2005 jordan

Cleaned up common power management register i/o
Added enter sleep state ioctl
Hooked power button interrupt
ok marco@


# 1.8 16-Dec-2005 marco

Remove autoconf chatter


# 1.7 16-Dec-2005 jordan

Moved DSDT AML parsing into parent ACPI driver
ok marco@


# 1.6 14-Dec-2005 marco

Add AC device glue.


# 1.5 14-Dec-2005 marco

Adjust debug printing levels to something more sane.


# 1.4 13-Dec-2005 marco

Add glue to attach devices to apci.


# 1.3 07-Dec-2005 marco

KNF


Revision tags: OPENBSD_3_8_BASE
# 1.2 10-Jul-2005 grange

Spacing cleanup.


# 1.1 02-Jun-2005 tholo

Start on a basic ACPI framework -- does not do much more than read out the
ACPI tables into kernel memory and attach ACPI and HPET timers currently.

In order to test this code, enabling the devices in GENERIC as well as
the ACPI_ENABLE option is needed. This code does not do any thermal
control yet, so this should be done with care depending on the platform.

In the tree so more people can contribute to making this more fully
featured.

Ok niklas@ grange@ tedu@


# 1.386 29-May-2020 deraadt

dev/rndvar.h no longer has statistical interfaces (removed during various
conversion steps). it only contains kernel prototypes for 4 interfaces,
all of which legitimately belong in sys/systm.h, which are already included
by all enqueue_randomness() users.


# 1.385 14-May-2020 kettenis

Bring back the acpi(4) changes. This time around only use the ACPI _CRS
information on ACPI 5.0 and later.

ok krw@, patrick@


# 1.384 11-May-2020 jca

More accurate remaining power reporting with disparate batteries

Overall remaining power was computed as the average of the remaining
power of each battery, in percents. This doesn't work with batteries of
different capacity, so use the total capacity and remaining power
instead.

Reported by lidstah, successful tests by lidstah and benno@, ok kettenis@


# 1.383 08-May-2020 kettenis

Move parsing of _CRS into acpi(4) and pass the parsed address and interrupt
information in struct acpi_attach_args.

ok mpi@


Revision tags: OPENBSD_6_7_BASE
# 1.382 14-Apr-2020 kettenis

Implement acpi_getprop(), the ACPI equivalent of OF_getprop().

ok patrick@


# 1.381 12-Apr-2020 kettenis

Add support for the _CCA method. This method indicates whether DMA is
cache-coherent or not. To implement this, acpi(4) gets two bus_dma tags
and passes the appropriate one when attaching devices based on _CCA.
On i386/amd64, where for all practical purpose DMA is always cache-coherent,
the two tags are the same. But on arm64 they are distinct.

ok patrick@


# 1.380 07-Apr-2020 visa

Abstract the head of knote lists. This allows extending the lists,
for example, with locking assertions.

OK mpi@, anton@


# 1.379 03-Apr-2020 mpi

Fix incoherencies of errors returned by various *kqfilter routines.

EOPNOTSUPP is returned when there's no "filterops" for a given fd.
EINVAL when the requested filter isn't supported by the "filterops".
ENXIO means the underlying device is no longer valid/present.

ok visa@


# 1.378 20-Feb-2020 visa

Replace field f_isfd with field f_flags in struct filterops to allow
adding more filter properties without cluttering the struct.

OK mpi@, anton@


# 1.377 31-Dec-2019 visa

Use C99 designated initializers with struct filterops. In addition,
make the structs const so that the data are put in .rodata.

OK mpi@, deraadt@, anton@, bluhm@


# 1.376 31-Dec-2019 mpi

Convert infinite sleeps to {m,t}sleep_nsec(9).

ok kn@


# 1.375 31-Dec-2019 jsg

include sys/sensors.h for sensor use
don't depend on indirect inclusion from acpidev.h


Revision tags: OPENBSD_6_6_BASE
# 1.374 07-Sep-2019 kettenis

Revert acpipci(4) changes. Too many systems are broken. We'll try again
after 6.6 as been released. The acpireg.h change stays behind.


# 1.373 28-Aug-2019 kettenis

Use ACPI information to attach PCI busses like we do on arm64. There are a
few additional quirks though, and attaching the PCI busses is delayed to
replicate the existing code more closely. That may be changed in the
future. Also tweak how we handle MSI support and respect to ACPI flag
that says we shouldn't attempt to use MSIs.

Some fallout is expected.

ok patrick@


# 1.372 27-Aug-2019 deraadt

On amd64 the bootup and resume paths for SP/BP/SP were not performing
msr/pat, clock-startup, firmware upgrade, fpu initialization, and cpu
soft-state initialization in the same order. That kind of chaos is
crazy and fragile. Improve the acpi resume call sequence and unify order
of the operations.


# 1.371 02-Jul-2019 jcs

acpi_set_gpehandler: turn edge arg into flags, add support for
passing GPE_DIRECT which directly executes the supplied callback and
clears a level interrupt rather than going through acpi_taskq which
can be slow when other driver callbacks are in the queue.

This should not be used for GPE handlers that need to do anything
ACPI-related.

ok kettenis


# 1.370 10-Jun-2019 kettenis

Print proper ACPI version number.

ok mlarkin@


# 1.369 08-Jun-2019 kettenis

When establishing the mapping between ACPI device nodes and PCI devices,
skip PCI host bridges and devices that aren't there.

ok patrick@


# 1.368 07-Jun-2019 kettenis

Introduce acpi_getsta() and use it to eliminate some duplicated code.

ok patrick@


# 1.367 12-May-2019 kettenis

Make sure the extended PM register descriptions actually exist before
using them. Fixes machines with ancient ACPI 1.0 (such as recent
hypervisors).

ok deraadt@


# 1.366 12-May-2019 kettenis

Add support for mapping ACPI PM registers using the "extended" gasio
representation from the FADT. Mostly fixes the Lanner NCA-1510.

ok lteo@, mlarkin@, deraadt@


# 1.365 11-May-2019 lteo

Fix incorrect error message.

ok kettenis@


# 1.364 08-May-2019 tedu

free size. the allocation is the header plus the length.


Revision tags: OPENBSD_6_5_BASE
# 1.363 04-Apr-2019 kettenis

Avoid potential null-pointer dereference. Found by Kent R. Spillner.

ok deraadt@, jsg@


# 1.362 20-Jan-2019 jsg

fix SMALL_KERNEL build after changes in rev 1.361
reported by naddy@ ok deraadt@


# 1.361 19-Jan-2019 tedu

Add a pwraction sysctl that controls what the power button does on acpi.
By default, nothing changes -- shutdown is initiated. But allows turning
power button into a sleep button if desired.
(grudging) ok from a few parties


# 1.360 26-Oct-2018 kettenis

Add acpipci(4) on amd64. For now this only calls the PCI-specific _OSC
method to let the ACPI implementation know what features we support.


Revision tags: OPENBSD_6_4_BASE
# 1.359 30-Aug-2018 kettenis

Move softbutton check before the check that skips attaching certain acpi
devices. Fixes regression caused by matching on _CID in addition to
matching on _HID.

ok matthieu@, mlarkin@


# 1.358 25-Aug-2018 kettenis

Match ACPI devices based on _CID if no match for _HID is found.

ok mpi@


# 1.357 19-Aug-2018 kettenis

Add support for multiple PCI segments. Only really implemented for arm64
for now as amd64/i386 firmware still caters for legacy OSes that only
support a single PCI segment.

ok patrick@


# 1.356 03-Aug-2018 kettenis

Let ahci(4) match on _CLS instead of _HID when attaching at acpi(4). Avoids
having to add many more _HID entries to the match table.

ok deraadt@, mlarkin@


# 1.355 10-Jul-2018 kettenis

Make legacy interrupts work in acpipci(4).

ok patrick@


# 1.354 05-Jul-2018 kettenis

Add acpipci(4), a driver that supports generic ECAM-compatible PCI host
bridges based on information provided by ACPI.

ok mlarkin@


# 1.353 04-Jul-2018 kettenis

Properly pass around the PCI "chipset tag" in acpi(4) and refactor
acpimcfg(4) to call an MD initialization functions that sets up a tag for
PCI ECAM.

ok guenther@, mlarkin@, krw@


# 1.352 01-Jul-2018 kettenis

Move acpi(4) attach glue into acpi_machdep.c.

ok guenther@, deraadt@


# 1.351 01-Jul-2018 kettenis

Add support for _DSD Device Properties.

ok mlarkin@


# 1.350 30-Jun-2018 kettenis

Add intr_enable() function, intended for MI use to amd64 and i386 and use
this in the acpi(4) suspend/resume code paths.

ok deraadt@


# 1.349 29-Jun-2018 kettenis

Use <stdint.h> types.

ok mpi@, jung@, krw@, deraadt@


# 1.348 29-Jun-2018 mlarkin

ACPI: Allow (with warning) GPE handler reassignment, instead of returning
a failure code.

ok kettenis


# 1.347 26-Jun-2018 mlarkin

knf


# 1.346 26-Jun-2018 kettenis

Let acpi(4) pass down the bus dma tag.

ok deraadt@


# 1.345 25-Jun-2018 kettenis

Reorganize acpi(4) code a bit in preparation for upcoming arm64 support.

ok deraadt@


# 1.344 20-May-2018 kettenis

Wake up the acpi thread after scheduling a gpio event.

ok mpi@


# 1.343 17-May-2018 kettenis

Sprinkle some #ifndef SMALL_KERNEL such that RAMDISK kernels cuild again.

pointed out by stsp@


# 1.342 17-May-2018 kettenis

Implement GenericSerialBus OpRegion support.

ok mlarkin@


# 1.341 27-Mar-2018 kettenis

Add acpicmos(4), a driver that implements SystemCMOS OperationRegion
access support. This fixes machines where the AML doesn't check whether
support for this OperationRegion type has been registered by the OS.

ok mlarkin@


Revision tags: OPENBSD_6_3_BASE
# 1.340 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.339 10-Feb-2018 deraadt

Syncronize filesystems to disk when suspending. Each mountpoint's vnodes
are pushed to disk. Dangling vnodes (unlinked files still in use) and
vnodes undergoing change by long-running syscalls are identified -- and
such filesystems are marked dirty on-disk while we are suspended (in case
power is lost, a fsck will be required). Filesystems without dangling or
busy vnodes are marked clean, resulting in faster boots following
"battery died" circumstances.
Tested by numerous developers, thanks for the feedback.


# 1.338 08-Feb-2018 deraadt

After flushing disks, try again to release all memory. We don't need to
save dirty memory to the hibernate space.


# 1.337 08-Feb-2018 deraadt

Toss all releaseable memory, because fragmentation can get in the way
of allocating the hibernate playpen.


# 1.336 08-Feb-2018 deraadt

sr_quiesce() is a new approach for ensuring that softraid drains
output to the disks.

This is part of a larger suspend/resume filesystem-safety diff, which
has been worked on for a couple of months already. Tests by job, krw,
beck, benno, and others. Sometimes even by snapshot users...


# 1.335 29-Nov-2017 kettenis

Add support for GPIO-signaled events.

ok mlarkin@


# 1.334 16-Nov-2017 jcs

Add PCI attachment for dwiic(4) needed by Intel 100 Series machines

ok kettenis


# 1.333 14-Nov-2017 kettenis

Add a few more PNP IDs to the skip lists.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.332 17-Aug-2017 stsp

Make dwiic(4) attach its ACPI parent dependencies.
This change makes the Asus E200HA keyboard work.

Original analysis and patch by Cesare Gargano
ok kettenis@


# 1.331 09-Aug-2017 dcoppa

Add "PNP0303" (8042 PS/2 Controller) to acpi_isa_hids[]

ok kettenis@


# 1.330 22-Jul-2017 jcs

acpibat: look for _BIX first before falling back on _BIF

ACPI 4.0 deprecated _BIF for battery status so some newer machines
are exporting _BIX which extends _BIF's package with a few extra
fields. When using _BIX, export the new discharge cycle count as a
sensor.

feedback from kettenis and anton
ok deraadt


# 1.329 20-Jul-2017 kettenis

Start a list of isa(4) device names such that we can avoid printing them
as "not configured".

ok mikeb@, millert@, deraadt@


# 1.328 08-Apr-2017 deraadt

Sensors are run as callbacks inside tasks. During suspend a sensor
could be running inside a driver that will be force-detached, or due
to tsleep end up disrupting the softstate/hardstate contract. At
suspend time, quisce all these callbacks by waiting for completion.
This issue has never been observed for real, but may be implicated in
suspend/resume failures.
ok kettenis guenther mlarkin


# 1.327 08-Apr-2017 deraadt

Two prototypes for acpi_maptable; merge to one place


Revision tags: OPENBSD_6_1_BASE
# 1.326 27-Mar-2017 deraadt

Now that hibernate_alloc() only has clean success/failure, don't
need to call hibernate_free() to clean up a partial mess.
ok mlarkin kettenis


# 1.325 13-Mar-2017 deraadt

allow ramdisks to build; ok jcs


# 1.324 12-Mar-2017 jcs

try to attach acpisbs first and if successful, don't attach acpibat


# 1.323 02-Mar-2017 natano

Add a new sysctl machdep.lidaction. The sysctl works as follows:

machdep.lidaction=0 # do nothing
machdep.lidaction=1 # suspend
machdep.lidaction=2 # hibernate

lidsuspend is just an alias for lidaction, so if you change one, the
other one will have the same value. The plan is to remove
machdep.lidsuspend eventually when people have upgraded their
/ets/sysctl.conf.

discussed with deraadt, who came up with the new MIB name
no objections mlarkin
ok stsp halex jcs


# 1.322 28-Feb-2017 natano

Untangle abstract sleep modes from ACPI sleep states. This paves the way
for further work in the area.

idea and ok deraadt
ok mlarkin


# 1.321 22-Feb-2017 jcs

add acpisbs, an acpi smart battery subsystem driver reading data
over smbus

currently disabled because it conflicts with acpibat

ok deraadt, kettenis


# 1.320 14-Jan-2017 kettenis

Revert aml_rdpciaddr changes; breaks several machines that were working
before.


# 1.319 11-Jan-2017 jsg

Increase the size of the buffers used for _HID and _CID strings to be
able to fit "Hyper_V_Gen_Counter_V1".

ok mlarkin@ deraadt@


# 1.318 08-Jan-2017 kettenis

Make aml_rdpciaddr() work for for devices behind bridges. The code now
uses the mapping between AML nodes and PCI devices that we establish.
Because _INIT methods may end up calling aml_rdpciaddr(), make sure we
create that mapping early. Also handle devices that aren't actually present.
These devices are now included in the mapping and reads will return an
all-ones patterm whereas writes are a no-op..

ok mlarkin@


# 1.317 25-Oct-2016 pirofti

Add the resource index to aml_parse_resource() callback function.

This is needed by an upcoming acpiec commit that handles machines breaking
the current ACPI specifications.

Change suggested by and ok kettenis@, guenther@


# 1.316 18-Sep-2016 guenther

If the APIC table (MADT) isn't present then ci_acpi_proc_id won't be set
when acpicpu tries to attach, leading to a NULL dereference. The ACPI
processor object on the Dells where this was reported is bogus and useless
anyway so apply the same test when deciding whether to attach to processor
objects as acpicpu would when matching them to cpus.

Problem reported by Eivind Eide (xenofil (at) gmail.com) and
Johan Huldtgren (johan+openbsd-bugs (at) huldtgren.com).
Testing by Eivind Eide.
ok kettenis@ mlarkin@


# 1.315 03-Sep-2016 naddy

Write the system time back to the RTC every 30 minutes.
This fixes the problem that long-running machines which were not
shut down properly would reboot with a badly offset system time.

hints and ok kettenis@


# 1.314 31-Aug-2016 mlarkin

Don't put a device in the wake device list if _STA says it's not there.

Fixes at least one machine that woke up from sleep / poweroff immediately.

ok kettenis@, tested by kettenis and martijn@


# 1.313 28-Jul-2016 kettenis

Store the acpi processor ID/UID in struct cpu_info, and use it to attach
acpicpu(4) drivers to the right cpu(4).

ok mlarkin@, guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.312 10-Jun-2016 kettenis

The power management timer has been made optional in ACPI 5.0A, so don't
attempt to attach acpitimer(4) if the timer isn't present.

Pointed out by jcs@

ok jcs@


# 1.311 08-May-2016 kettenis

Make sure devices listed by a device's _DEP method are attached before
we attach the device itself. The _DEP methode, introduced in ACPI 5.0,
is a hint that the device in question depends on OpRegion support from
the devices listed by _DEP, so we have to order them the proper way.
To prevent us from attaching those devices again when we encounter them
later walking down the device tree, keep track of our attempts to
attach a device driver to them using aflag in the aml_node structure.

ok guenther@


# 1.310 25-Apr-2016 pirofti

We attach acpihpet(4) and acpiec(4) without using the HID, so do not
report it in the dmesg.

OK deraadt@, mlarkin@.


# 1.309 02-Apr-2016 kettenis

Reduce dmesg spam by knocking out some acpi hardware IDs that don't need a
driver.

ok mpi@


# 1.308 29-Mar-2016 kettenis

Check if a device is present (using _STA) in acpi_foundhid. This prevents
us from reporting devices as "not configured" that aren't actually present,
and allows us to remove duplicated code from several drivers.

ok deraadt@, ok jsg@


# 1.307 29-Mar-2016 kettenis

Implement a cleaner way to prevent acpivout(4) from attaching if we have
a platform-specific method of controlling the backlight.

ok millert@, mlarkin@, jung@


# 1.306 28-Mar-2016 kettenis

Adding each and every new acpi device driver to acpi_foundhid() is
gettinga bit of the burden. And it really isn't how our autoconf
framework is supposed to work for busses that can be enumerated. All
the drivers already check for a matching _HID in their attach
function. So we can just drop the checks in acpi_foundhid() and let
the drivers sort themselves out.

Print a "not configured" line for devices for which we don't attach adriver.
This may be a bit much as there are devices for which we will not have a
driver. This will be removed if it causes too much dmesg spam.

ok deraadt@, mlarkin@


Revision tags: OPENBSD_5_9_BASE
# 1.305 17-Jan-2016 jsg

Take acpi_foundhid() outside of #ifndef SMALL_KERNEL so the ramdisk
kernel can find and attach dwiic(4) and sdhc(4).


# 1.304 17-Jan-2016 jsg

remove an unused global variable


# 1.303 14-Jan-2016 kettenis

Attach dwiic(4) on the Intel Bay Trail i2c controllers as well.


# 1.302 12-Jan-2016 kettenis

Improve emulation of PM registers on Hardware-redcued ACPI by emulating
SPL_TYPx, SLP_EN and WAK_STS using the new SLEEP_STATUS_REG and
SLEEP_CONTROL_REG registers. Makes the transition into S5 (powerdown) work
on the ASUS X205TA.

ok deraadt@, mlarkin@


# 1.301 12-Jan-2016 jcs

Add dwiic, a driver for the Synopsys DesignWare i2c controller found
on the Samsung ATIV Book 9 laptop. This initial version only
supports ACPI config/attachment.

Add ihidev, a HID-over-i2c driver largely based on uhidev. dwiic
handles attaching ihidev devices found in ACPI.

Add ims, a HID-over-i2c mouse/trackpad driver to get basic cursor
and button functionality from HID-compliant i2c trackpads.

ok kettenis deraadt


# 1.300 11-Jan-2016 kettenis

Make sdhc(4) attach to hardware IDs "80860F14" and "PNP0FFF".


# 1.299 09-Jan-2016 kettenis

Initial support for Hardware-Reduced ACPI. This skips the legacy mode checks
by emulating the PM_CNT register such that SCI_EN is always enabled. It also
skip attaching acpitimer(4) since the relevant registers are no longer there.

ok deraadt@, mlarkin@


# 1.298 06-Jan-2016 kettenis

Follow the spec and check the SCI_EN bit to see if ACPI has already
been enabled and don't bother enabling it again if it is. This should fix
systems that don't implement legacy mode and therefore set ACPI_ENABLE and
ACPI_DISABLE to zero.

ok mlarkin@, yasuoka@


# 1.297 23-Nov-2015 reyk

Back out the CRITICAL change, turn it back from 10% to 25%. Some
people with old/broken batteries didn't like this change, and the
underlying heuristics are wrong.


# 1.296 22-Nov-2015 reyk

The "CRITICAL" APM battery level, that is exported/faked by the ACPI
stack, is currently set to 25%. With 25% battery left, a typical
modern laptop will have at least one hour runtime left. It is an
arbitrary value, and other APM power layers in the kernel set CRITICAL
to something like 5% or 10%, so set it to 10% for now.

OK mlarkin@


# 1.295 28-Sep-2015 deraadt

In low-level suspend routines, set cold=2. In tsleep(), use this to
spit out a ddb trace to console. This should allow us to find suspend
or resume routines which break the rules. It depends on the console
output function being non-sleeping.... but that's another codepath which
should try to be safe when cold is set.
ok kettenis


# 1.294 06-Sep-2015 deraadt

clarify malloc arguments (base + length...)


# 1.293 28-Aug-2015 deraadt

fairly simple sizes for free(); ok tedu


# 1.292 20-Aug-2015 kettenis

Respect the access size when reading or writing to pci config space.
This version also makes sure writes are properly aligned, preventing
the panics seen with the previous incarnation of this change.

Fixes battery status passthrough in vmware, brightness keys on my x220
and who knows what else.

ok mlarkin@, deraadt@


Revision tags: OPENBSD_5_8_BASE
# 1.291 04-Aug-2015 deraadt

If we are setting ACPI_PM1_BM_RLD do it only once, and remember to
repeat this action at resume time.
ok mlarkin guenther


# 1.290 29-Jul-2015 deraadt

Something subtle in previous revision is locking up suspend or booting
on some laptops. Mailing lists threads mention x220 and Asus machines.
Reverting for now.


# 1.289 21-Jul-2015 kettenis

iRespect the access size when reading or writing to pci config space.
Fixes battery status passthrough in vmware and who knows what else.

Tested by many.

ok mlarkin@, deraadt@


# 1.288 17-Jul-2015 mlarkin

fix comments


# 1.287 26-Mar-2015 jsg

Use the 0x10 extended capability to determine if the trackstick
buttons are wired to the trackpad and need to be re-routed to the
trackstick. Without this change the buttons on 2015 thinkpads
get picked up as extended buttons that show up as scroll up/down.

Remove the X1 Carbon 2015 (LEN0048) and X250 (LEN0046) from the
top button area/soft buttons quirks list. Also avoid using the
quirk list entirely if the capability bit is set.

Based on work from jcs@ with feedback from mpi@
ok mpi@ jcs@


# 1.286 20-Mar-2015 kettenis

The ACPI standard says that a PCI function number of FFFF can be used to refer
to all functions on a device. This makes no sense in the context of accessing
PCI config space. Nevertheless stsp@ has a machine with AML that tries to
do config space reads from a device where _ADR encodes function number FFFF.
This is almost certainly buggy AML, but since Linux (and presumably Windows)
boot just fine on this machine we need to work around this. Linux effectively
treats function FFFF as function 7, which isn't present on this machine.
Instead we simulate a read from a non-existent device by returning a bit
pattern of all ones. Writes to function FFFF will still panic.

ok mlarkin@


# 1.285 16-Mar-2015 deraadt

Reduce use of SMALL_KERNEL in acpi, bringing acpiec driver and more gpe
handling into RAMDISK. This is now possible because the install media
has ample room. The goal is to reduce special cases where we may be
skipping (unknown) important operations...
ok mlarkin kettenis


# 1.284 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.283 07-Feb-2015 deraadt

New framework that allows hibernate to pass in entropy from it's fresh
boot.
ok mlarkin


# 1.282 06-Feb-2015 mlarkin

Fix a hibernate crash on some machines due to unmapping a page that
may not have been mapped previously (in the failure to hibernate case).

Also ensure that the lowmem ptp is mapped in all cases (not just MP).

ok kettenis


# 1.281 17-Jan-2015 deraadt

resume_randomness() before spinning up other cpus...


# 1.280 15-Jan-2015 jsg

Define a new wscons mouse type for Synaptics clickpad devices that lack
physical buttons. This will be used if the acpi pnpid for the mouse
matches a list provided by Synaptics found in Linux.

Combined with the xenocara changes this will remove the need for an xorg
config file for the x240/t440/t540 generation of thinkpads.

ok matthieu@ shadchin@ kettenis@


# 1.279 11-Jan-2015 kettenis

Tolerate checksum errors in ACPI tables (but complain about them). Apparently
there are machines out there with broken BIOSen with such checksum errors in
thier RSDT tables. Windows runs fine on these machines, so it probably does
not check. Linux ignores checksum errors as well (and prints a warning).

ok deraadt@, krw@


# 1.278 18-Dec-2014 deraadt

suspend and resume the RNG (at what looks like the right spots)
ok reyk djm


# 1.277 09-Dec-2014 doug

Replace some malloc(n*size,...) calls with mallocarray().

ok tedu@ deraadt@


# 1.276 06-Dec-2014 mpi

If "machdep.lidsuspend" is not set, do not go back to sleep if the lid
is still closed when resuming. This allows docked laptop to be resumed
without opening them.

ok mlarkin@


# 1.275 23-Nov-2014 mlarkin

If a machine has no _LIDs, then none can be open. This diff fixes a problem
where machines without any _LIDs were going back to sleep after hibernate
due to the recent suspend-if-lid-closed-on-resume diff. Noticed on qemu,
which doesn't have any _LIDs.

discussed with deraadt


# 1.274 18-Nov-2014 krw

Nuke yet more obvious #include duplications.

ok miod@


# 1.273 08-Nov-2014 mlarkin

If resuming from sleep (zzz/ZZZ) and the lid is still closed, go back to
sleep. This prevents accidental lid flex or slight opening in a backpack
from waking the machine up and leaving it resumed (powered on).

ok deraadt@


# 1.272 26-Sep-2014 kettenis

Rework piglet and pig allocation. Currently the piglet gets allocated
deep down in the suspend path, where it is really hard to recover from
allocation failure. So allocate the piglet early on in the suspend path.
Also change the piglet and piglet allocation functions to use km_alloc(9)
instead of doing pmemrange magic. This removes a bunch of code which, in the
case of the piglet allocation, is broken since it results in a NULL pointer
dereference. Also switch the piglet allocation to not wait. If we can't
allocate 16MB of phys contig memory on a halfway modern machine we're almost
certainly under a lot of memory pressure and we're better off not trying to
hibernate anyway.

ok mlarkin@


# 1.271 25-Sep-2014 kettenis

Bring back rev 1.249. Now that mp_setperf() has been fixed, it should be
safe to restore perflevel when resuming. Should fix the issues people
see with the processor running at full speed heating up the machine while
the apm command reports that the CPUs are running at a low speed.

ok dcoppa@, deraadt@


# 1.270 23-Sep-2014 kettenis

Release the acpi lock around calls to wsdisplay_suspend() and
wsdisplay_resume(). The X server may adjust the display brightness just
before/after the VT switch when suspending/resuming. In fact, it may
already be blocked adjusting the display brightness for some reason
unrelated to the suspend request.

The VT switch request will eventually time out, but the delay is annoying
and the failed VT switch will lead to problems after resume.

Needed for a future update of the xf86-video-intel driver.

ok mlarkin@


# 1.269 19-Sep-2014 kettenis

Use config_suspend_all(9).


# 1.268 08-Sep-2014 kettenis

Clear the PME Status bit on PCI devices in response to Device Wake events.
Fixes the interrupt storm on the 2009 Mac mini with WOL enabled on its
nfe(4) interface.

ok mlarkin@


Revision tags: OPENBSD_5_6_BASE
# 1.267 20-Jul-2014 mlarkin

Support hibernating to softraid crypto volumes.

much help and ok from deraadt@


# 1.266 16-Jul-2014 mlarkin

Reenable hibernate RLE support and flush+zero all memory after unpack.


# 1.265 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.264 12-Jul-2014 kettenis

Add a function to drop all clean pages on the page daemon queues and call
it when we hibernate.

ok mlarkin@, miod@, deraadt@


# 1.263 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.262 11-Jul-2014 mlarkin

Flush the buffercache to 16MB on hibernate and restore its previous max
size (kern.bufcachepercent) on resume, for better hibernate performance.

ok beck@


# 1.261 10-Jul-2014 blambert

remove unneeded header

ok mlarkin@


# 1.260 01-Jul-2014 kettenis

Adding an additonal delay after each access to SystemMemory or SystemIO
during autoconf makes no sense.

ok deraadt@


# 1.259 23-Jun-2014 kettenis

SystemMemory is used to access memory mapped registers on some machines, so we
must use the bus_space(9) API to access it instead of memcpy(9). Also make
sure we properly align access to fields that need more than byte alignment.

ok guenther@


# 1.258 21-May-2014 mlarkin

Fix invalid / missing format specifiers in several dnprintf statements
(used only when ACPI_DEBUG is enabled)

From a diff on tech@ by Fabian Raetz, with a minor change by me, tested
on i386 and amd64 with ACPI_DEBUG enabled and disabled.


# 1.257 25-Apr-2014 mlarkin

Defer the free of the hibernate structures until later, when it is safe to
do so.

Freeing the hibernate structures too early leads to inadvertent calls into
splx when we aren't ready for them yet. Found when trapping splx calls in
the resume path.

discussed with deraadt@ at m2k14


# 1.256 13-Apr-2014 mlarkin

Fix a badly worded debug message that implied the entire machine was going
to sleep, instead of just the acpi thread going to sleep.


# 1.255 31-Mar-2014 mpi

Change the order of operations during a suspend/resume cycle and
call bufq_quiesce() after executing the DVACT_QUIESCE handlers.

This should be safe since no disk nor controller drivers have such
handler but it will allow us to detach sd(4) devices attached to a
USB bus.

Another benefit pointed out by kettenis@ is that drivers that need
to read a firmware from the disk should be able to do it at resume
time in a DVACT_WAKEUP handler.

ok kettenis@, deraadt@


# 1.254 13-Mar-2014 dlg

get rid of the assumption that the head of the alldevs list is the
"mainbus" device. this breaks when mpath is enabled because it
attaches before mainbus and therefore takes the head position.

have autoconf provide device_mainbus() which looks up mainbus_cd,
and use that instead.

discussed with deraadt who just wants mpath stuff to move forward
despite there being many ways to shine this particular turd.


Revision tags: OPENBSD_5_5_BASE
# 1.253 21-Feb-2014 deraadt

Do the lid control further on in the resume sequence, since this is
AML, and will do WAITOK operations. While cold, that is a bad idea.
We seem to be safe with respect to the spec, I do not see any event
debounce issues here.
ok kettenis, tested by many


# 1.252 20-Jan-2014 claudio

Instead of checking sc->sc_revision <= 1 check that the
fadt->hdr_revision <= 1. This is a better check since it checks
the version of the proper descriptor. This allows the alix APU
to reboot since those boards have currently a wrong sc->sc_revision.
Committing this now so that it can be tested easier.
With and OK jsg@ also fine by derradt@


# 1.251 24-Dec-2013 kettenis

If the FADT has its SMI_CMD set to zero, assume we're only ACPI-only hardware
and don't need to disable SMI ownership of the ACPI hardware registers.

ok mlarkin@


# 1.250 23-Dec-2013 kettenis

Revert previous commit. It makes resume hang on some MP systems running
GENERIC.MP.


# 1.249 16-Dec-2013 kettenis

Restore hw.setperf upon resume like we do for apm(4) on i386. Seems we need
to do this fairly late in the resume to avoid hangs.

ok deraadt@, mlarkin@


# 1.248 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


# 1.247 06-Nov-2013 mpi

Add support for Power Resources for Dx states and the necessary hook
for PCI devices. This hook should be called twice, before and after
changing the power state of a PCI device.

Before setting the device to the new state, the ACPI layer will notify
every power resources linked to the device for that state and make sure
they are turned "_ON". After changing the state of the device, it will
decrement the reference of every power resources linked to that device
for the old state and turn them "_OFF" if they are no longer referenced.

This fixes the no-USB after resume problem seen on various ThinkPad,
problem initialy diagnosed with Alexander Polakov.

ok kettenis@, deraadt@


Revision tags: OPENBSD_5_4_BASE
# 1.246 01-Jun-2013 mlarkin

Stop printing ACPI wakeup devices in dmesg after the 16th wakeup device.

Helps with VMware VMs that report hundreds of wakeup devices.

ok reyk@, deraadt@


# 1.245 31-May-2013 bluhm

Fix the build for a kernel without wd and pciide in its config.
OK deraadt@ tedu@


# 1.244 30-May-2013 deraadt

Enforce ca_activate tree-walks over the entire heirarchy for all events,
cleaning up some shutdown-hook related code on the way.
(A few drivers related to sparc are still skipped at kettenis' request)
ok kettenis mlarkin, tested by many others too


# 1.243 18-Apr-2013 deraadt

pin the acpi thread to the BSP, because AML and SMI cannot be trusted. I
was certain we were doing this already, but it turns out no...


Revision tags: OPENBSD_5_3_BASE
# 1.242 24-Dec-2012 guenther

In acpiioctl(), Return though the splx() path if suser() returns an error

ok deraadt@


# 1.241 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


# 1.240 04-Oct-2012 ehrhardt

Use information provided by ACPI to attach secondary PCI host bridges.

ok kettenis@


# 1.239 07-Sep-2012 kettenis

Add a new function called acpi_pci_min_powerstate() that returns the lowest
power state for a PCI device for the power state (currently S3 or S4) we're
transitioning into. This function is probably too simplistic; there is a lot
of (silly) complexity in the ACPI spec.

ok deraadt@


Revision tags: OPENBSD_5_2_BASE
# 1.238 13-Jul-2012 pirofti

Disable acpivideo(4) on ASUS laptops.

There are some models that panic whenever an event is received.
Some cope, some don't. In order to get this right acpiec(4) requires
major rework. Until then let machines boot.

Discussed with deraadt@.


# 1.237 11-Jul-2012 mlarkin

for now, restrict hibernate to root in the acpi ioctl kernel interface.
normal rules for controlling this interface through apmd still apply.

ok deraadt@


# 1.236 09-Jul-2012 deraadt

tiny little oops in untested hibernate block


# 1.235 09-Jul-2012 deraadt

clean up ioctl() case statements


# 1.234 09-Jul-2012 deraadt

acpi_disable_onegpe() is not used; ok pirofti


# 1.233 24-May-2012 kettenis

If _MIN > _MAX consider a WordBus resource to be invalid. Fixes a panic on
the IBM x3550-M4.

ok mikeb@


# 1.232 29-Mar-2012 mlarkin

Missing #ifdef in previous diff


# 1.231 29-Mar-2012 mlarkin

Prevent hibernation attempts on machines with unsupported disk controllers.
The same check is also performed later in the hibernate sequence, but by
then it's too late to cleanly unwind (presently).

ok deraadt


# 1.230 26-Mar-2012 deraadt

simplify conditionals in acpi_sleep_state()
ok mlarkin


# 1.229 26-Mar-2012 deraadt

hook in the hibernate request code; half of this diff is from mlarkin
ok mlarkin


Revision tags: OPENBSD_5_1_BASE
# 1.228 20-Sep-2011 deraadt

Very early in suspend while the clock is still ticking realtime,
save the clock back to the rtc. The zaurus already did this.
ok phessler miod kettenis


Revision tags: OPENBSD_5_0_BASE
# 1.227 02-Jul-2011 nicm

kqueue attach functions should return an errno or 0, not a plain 1. Fix
the obvious cases to return EINVAL and ENXIO.

ok tedu deraadt


# 1.226 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.225 16-Jun-2011 pirofti

Import acpitoshiba driver.

This is originally written by Hiroyuki Aizu, ported and integrated
in our tree by Javier Vazquez <javama4 AT gmail> with tweaks and
suggestions by me.

Import prodded by deraadt@ and the lack of testers on tech@.


# 1.224 27-Apr-2011 jcs

attach acpithinkpad to newer lenovo models like the x120e

ok deraadt@


# 1.223 22-Apr-2011 jordan

Fix uninitialzied variables and formatting strings (-Wxxx errors)


Revision tags: OPENBSD_4_9_BASE
# 1.222 02-Jan-2011 jordan

Re-add code for ACPI taskqueue, eliminates need for state vars in acpi softc
Serialize processing ACPI tasks for sleep/powerdown/GPE event
ok deraadt@


# 1.221 31-Oct-2010 guenther

Revert last commit: it breaks resume on Thinkpads
"then please back it out" deraadt@


# 1.220 26-Oct-2010 jordan

Add task queue for ACPI gpe and notify handlers
This fixes eject on Dell Latitude dock and an issue on the Dell Mini
battery update.
ok deraadt


# 1.219 07-Oct-2010 jordan

Create separate functions for enabling wake and runtime gpes.
Remove old suspend/resume gpewalk function
ok deraadt, mlarkin


# 1.218 05-Oct-2010 jordan

split acpi_enable_onegpe to separate enable/disable functions
ok deraadt


# 1.217 05-Oct-2010 jordan

Added acpi_disable_allgpes function to clear GPE status
ok deraadt


# 1.216 05-Oct-2010 deraadt

clean up acpi_softc use more


# 1.215 05-Oct-2010 deraadt

delete a bit of #if 0 code


# 1.214 05-Oct-2010 deraadt

hide use of our global acpi_softc pointer a bit better


# 1.213 29-Sep-2010 deraadt

grab the acpi thread lock very early in acpi thread startup; not that it
is likely that anyone will race us... but why be careless.
ok kettenis


# 1.212 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.211 29-Aug-2010 jasper

- add a check for FWRITE here too.

ok miod@


# 1.210 27-Aug-2010 kettenis

Make PCI Power Management optional, and only enable it when acpi(4) attaches.

ok deraadt@


Revision tags: OPENBSD_4_8_BASE
# 1.209 08-Aug-2010 kettenis

Close race between the acpi thread and wscons ioctls running in process
context. We might rip this out post-release and replace it with a solution
that serializes things in a more obvious way.

ok deraadt@, miod@ and "those crazy texans who ok anything"


# 1.208 08-Aug-2010 deraadt

Report the acpi sleep states when on a RAMDISK; ok krw


# 1.207 07-Aug-2010 kettenis

Enforce acpi register access rescrictions.

ok mlarkin@, deraadt@, tested by marco@


# 1.206 07-Aug-2010 deraadt

upon resume, notify all the acpiac and acpibat drivers to update
their status. some laptops do not do this automatically.
ok kettenis canacar


# 1.205 06-Aug-2010 marco

Always call _PSW on _LID devices that support it to enable lid open to
resume the machine. Conversely disable it when the machine wakes up.

Tested by several ok deraadt


# 1.204 06-Aug-2010 deraadt

As part of a revamp of the PM1/GPE code, I write this diff to look for
unmanaged EN & STS bits in the PM1 register at interrupt time and report
them. As a side effect this splits the STS acknowledgement into two writes
(for power, and sleep) instead of one. The printf that is added (to spot
unmanaged STS bits) has not yet been triggered as far as we know.

Before the "write to PM1 registers at the right offset" diff went in, this
was not neccessary. But newer thinkpads do not have a working soft power
button without this diff.

We have no idea why.
ok mlarkin kettenis


# 1.203 05-Aug-2010 deraadt

PM1 power button events were generating 'fake events' to acpibtn PWRB,
but if there is no such device these were being tossed. Instead, arrange
for all power button handlers to flag a softc variable which is processed
by the thread; so that one place will know to request a powerdown (psignal
to init)
ok marco mlarkin


# 1.202 05-Aug-2010 deraadt

rename a variable which is annoyingly poorly named


# 1.201 05-Aug-2010 mlarkin

Write to the correct offset for various ACPI pmreg registers. Writing to
the correct register does properly clear the wake status bits and fixed
event bits, which allows some machines to have more than 1 suspend cycle.

bug spotted by deraadt@

tested by deraadt@ and myself on a half-dozen machines and others on
hackers@

ok deraadt@, marco@


# 1.200 05-Aug-2010 deraadt

Fix the power button. The pm1 register is 16 bits in size.
My mistake, a while back.
tested by krw


# 1.199 04-Aug-2010 kettenis

Print supported sleep states.

ok marco@, phessler@, deraadt@


# 1.198 03-Aug-2010 mlarkin

Use the proper flag for re-enabling certain hardware events (power btn,
sleep btn). The (incorrect) flag prevoiously used coincidentally had the
same value, so this shouldn't cause any different behavior than before.

tested on a variety of machines (i386, amd64, sp, mp) - no behavioral
change seen

ok deraadt@


# 1.197 28-Jul-2010 marco

Make all gasio failures look and behave the same.


# 1.196 28-Jul-2010 mlarkin

Delay a little longer on suspend before giving up. Fixes machines that
sometimes don't suspend because the sleep transition takes too long
(symptoms like failing to sleep but the backlight stays on).

ok deraadt@


# 1.195 27-Jul-2010 deraadt

remove XXX; EC can now be attached early when needed; ok jordan


# 1.194 27-Jul-2010 deraadt

more brackets feels good


# 1.193 27-Jul-2010 deraadt

XXX annotate a block in acpi_enter_sleep_state() that is pretty worrying


# 1.192 27-Jul-2010 deraadt

horrific indentation that nearly made me puke


# 1.191 27-Jul-2010 mlarkin

Set the sleeping indicator light on machines that support it, in case SMI
doesn't do it for us. Shows the 'moon' LED on older Thinkpads when
sleeping.

Tested on T43p, W500, T510 by me and on X61s by deraadt@.

ok deraadt@


# 1.190 27-Jul-2010 jordan

Early initialization of acpiec if ECDT table exists
Fixes hang when booting thinkpads while docked
ok deraadt


# 1.189 26-Jul-2010 deraadt

remove merge error which was commited


# 1.188 26-Jul-2010 pirofti

Add support for Sony ACPI hotkeys via a new driver: acpisony(4).

Currently it only works for the suspend button and tries to do right for
the brightness events, but I haven't found a Sony laptop that like to do
right so far.

In the future I want to make the brightness keys work on all Sony's and
also add support for the zoom hotkeys and whatever other funky keys I can
find on those things.

Okay deraadt@.


# 1.187 22-Jul-2010 deraadt

pretty large cleanup of SMALL_KERNEL


# 1.186 21-Jul-2010 deraadt

in the acpi GPE handler, late ack level interrupts not edge interrupts.
Duh.


# 1.185 21-Jul-2010 deraadt

Merge the edge and level gpe handlers into one (now possible since the
gpe structure tells us whether the gpe is edge or level)


# 1.184 21-Jul-2010 deraadt

delete #if 0 code we are not going to use; ok marco


# 1.183 20-Jul-2010 deraadt

make the mask code for acpi_enable_onegpe() match other similar code


# 1.182 20-Jul-2010 deraadt

Handle the acpi interrupt controller with proper edge/level handling,
wrapped in spltty to avoid racing against the interrupt controller.
Repair the gpe bit masking code while there, and do operations in the
right order
ok kettenis mlarkin, help from jordan at figuring out the order of
operations.


# 1.181 20-Jul-2010 matthew

When we're preparing to go to sleep, only call bufq_restart() if other
preparations failed.

ok kettenis@, thib@, deraadt@


# 1.180 19-Jul-2010 deraadt

permit compilation with ACPI_DEBUG


# 1.179 19-Jul-2010 deraadt

there is no need for a seperate dsdt_softc pointer; dsdt does not have a
softc but is just a part of the single acpi driver.
ok jordan


# 1.178 19-Jul-2010 deraadt

acpi_delay() is no longer needed; ok jordan


# 1.177 18-Jul-2010 mlarkin

Clear event status on resume, to avoid going back to sleep/reboot/shutdown
immediately after resume. Tested by a bunch of people on hackers@ on
various hardware. Fixes at least Toshiba NB200 and Gateway LT3103u.

ok marco


# 1.176 13-Jul-2010 deraadt

rename some thread related interfaces so they are logical


# 1.175 10-Jul-2010 jordan

Verify _ADR bdf is correct (some systems return 0xffff)
Works on Dell x9xx and Studio 1555


# 1.174 09-Jul-2010 jordan

Clear the noise


# 1.173 08-Jul-2010 jordan

Add mapping for ACPI device to PCI bdf (match autoconf tree)
Simplify resource parsing function to use buffer argument
Convert namespace linked lists to use queue macros
ok marco@, deraadt@


# 1.172 06-Jul-2010 deraadt

Use spltty() to lock downcalls from apm against the information being
modified by the acpi thread.
ok kettenis


# 1.171 06-Jul-2010 deraadt

Don't use a workq to do the suspend, because it races aginst the acpi
thread. Instead, just tell the acpi thread to do the suspend for us.
This makes apmd & zzz work correctly. While here, have acpithinkpad
attempt to post the event to apm before prompting the suspend itself.
ok kettenis marco mlarkin


# 1.170 05-Jul-2010 mlarkin

Reset register SCI_EN on resume. The ACPI spec clearly states we don't have
to do this, but many machines don't, or forget to, which leads to all sorts
of problems on resume (broken battery stats, overheating, failure to resume
more than once, etc).

tested by many, ok marco@, deraadt@


# 1.169 01-Jul-2010 thib

do bufq quiesce again, since vnd/the bufq_done call in
biodone has been fixed.


# 1.168 01-Jul-2010 jordan

Backout recent AML changes, breaks on R210 and others


# 1.167 01-Jul-2010 jordan

Simplified aml_parse_resource call, cleanup code
ok marco


# 1.166 01-Jul-2010 jordan

Fix subordinate bus # for multi-root PCI buses
ok marco, kettenis


# 1.165 30-Jun-2010 thib

Disable/partially backout the bufq quiesce changes as this
is causing havoc with vnds and release must be buildable.


# 1.164 29-Jun-2010 deraadt

Fix ramdisks (new code was placed inside a SMALL_KERNEL chunk)


# 1.163 29-Jun-2010 jordan

Add support for mapping ACPI to PCI devices
ok kettenis, deraadt


# 1.162 29-Jun-2010 kettenis

Call bufq_quiesce() on suspend to stop all I/O and bufq_restart on resume.
Makes suspend/resume work much better while doing disk I/O.

ok mlarkin@


# 1.161 29-Jun-2010 tedu

60 is a more reasonable guess for number of minutes in an hour than 100.
spotted by TeXitoi. ok deraadt


# 1.160 27-Jun-2010 jordan

Add _acpi_matchhids routine, precursor to pci device match


# 1.159 27-Jun-2010 mlarkin

Clear ACPI fixed event status on resume (power buttons/etc) so that some
machines don't immediately resume after suspending to S3.

Tested by kettenis@ and I on 5 or 6 machines, deraadt@ "yeah, looks good"


# 1.158 27-Jun-2010 jordan

Cleaned up acpi_maptable, adds to table list directly


# 1.157 07-Apr-2010 deraadt

If we have done a wsdisplay_suspend(), and ACPI decides it does not want to
suspend, we must roll back by calling wsdisplay_resume() or the virtual
console switching will be locked forever
ok oga


# 1.156 07-Apr-2010 kettenis

Make suspend/resume work on MP machines (running an MP kernel). Joint work
from mlarkin@ and me, with some amd64 fixes thrown in by deraadt@

ok marco@, deraadt@, pirofti@, mlarkin@


# 1.155 31-Mar-2010 kettenis

Bring /dev/apm support in line with apm(4). Make the suspend button send
a suspend request event to apmd(8) instead of suspending immediately. Also
keep track of whether /dev/apm and /dev/apmctl are currently open, such that
we can still suspend immediately if apmd(8) isn't running.

ok deraadt@, marco@, pirofti@, jsing@, oga@


# 1.154 30-Mar-2010 oga

Prevent the apmd/x races for good.

When we hit suspend time, go through all wsdisplays on the system. if
they are in mode MAPPED, but not MODE_DUMBFB then if possible do a full
vt switch to a !mapped vt, and prevent switching back until resume time.
This has to be called from MD code because this involves userland
running so that X can run the vt switch signal handler. This way, any
case where we are using the "poke registers from userland" model, we
will not be on the hardware when we go down, so the kernel can actually
handle thing properly.

Tested on several acpi laptops (by kettenis@ and ian@), x40 (me and
beck@ at LEAST) and zaurus (me). Maybe others, but if so I forgot who at
this time..

Idea from deraadt somewhere over the Faroe Islands (I thought of a
similar thing myself a while ago). Much prompting from him. Ok and
comments miod@


# 1.153 25-Mar-2010 oga

Fix & vs. && in the kqfilter code.

Based on a comparison with the apm code.

ok deraadt@, kettenis@


Revision tags: OPENBSD_4_7_BASE
# 1.152 26-Nov-2009 mlarkin

Visual (and on some machines, audio) feedback while the machine is
resuming via calls to aml _SST method. This also makes the "moon" LED on
thinkpads to blink during resume and cease when resume is completed.

ok pirofti@


# 1.151 26-Nov-2009 deraadt

Toggle into "cold" when we we disable interrupts, because tsleeping
in the AML interpreter and context switching into userland processes
tends to sometimes pop the ACPI trampoline page out of our address
space. Makes lots more machines work.
ok kettenis mlarkin


# 1.150 24-Nov-2009 jsg

in the resume path evaluate _WAK before dealing with device drivers
like the spec says, not after.

ok mlarkin@


# 1.149 24-Nov-2009 deraadt

abort suspends on SMP systems until that gets written; ok mlarkin


# 1.148 23-Nov-2009 mlarkin

pci should return the result of it's children's suspend/resume calls back
to its parent.

handle suspend failure case and unwind if devices have an activate function
that returns failure.

ok deraadt@, kettenis@


# 1.147 23-Nov-2009 mlarkin

acpi* devices need to call their children on suspend/resume.
ok deraadt@


# 1.146 23-Nov-2009 pirofti

Remove ACPI_SLEEP_ENABLED checks.

This enables by default the suspend/resume paths in the kernel.

Okay deraadt@.


# 1.145 23-Nov-2009 deraadt

knf and spacing that are super annoying


# 1.144 23-Nov-2009 mlarkin

Hook up Theo's new children suspend/resume function, as well as more ACPI
suspend/resume glue.

ok deraadt@


# 1.143 26-Oct-2009 deraadt

Do not do strncmp comparisons on dv_xname because longer device names which
look similar could arrive in the future. Instead, compare directly against
dv_cfdata->cf_driver->cd_name
Issue originally spotted by miod


# 1.142 26-Oct-2009 jordan

Verify checksum+address when loading ACPI tables.
Some systems had invalid entries in RSDT/XSDT.
ok marco@


# 1.141 23-Jul-2009 cnst

New aibs(4) driver for ASUSTeK AI Booster (ACPI ATK0110) hardware monitoring,
with sensor state support through limits provided by the ACPI.

Tested on several ASUS motherboards kindly networked by Sam Fourman Jr.

ok deraadt marco jordan


Revision tags: OPENBSD_4_6_BASE
# 1.140 03-Jun-2009 pirofti

Import acpi power resource driver. This is needed for suspend/resume logic.

Okay jordan@ and marco@.


# 1.139 03-Jun-2009 pirofti

Add acpivideo support. This adds brightness support for all laptops
except thinkpads, they will use the acpithinkpad driver. The driver is
also hooked into wsconsole. So brightness can be adjusted via:

$ wsconsctl display.brightness=<percentage>

This is very helpfull on some laptops that have a nasty bios and get two
steps instead of one when pressing the brightness button.

Tested on various dell, fujitsu, acer, samsung and other laptops.

Okay marco@, miod@. Suggestions from kettenis@.
Lots of reviews and help from miod@, thanks!


# 1.138 03-Jun-2009 jordan

Added ejectable bay for IDE cdroms
ok marco@


# 1.137 30-Apr-2009 marco

Oga changed apm ioctls and forgot to grep. Temporary work around by hitting
the sleep button when we get any request. This interface will chnage at
some point.
ok oga


# 1.136 19-Apr-2009 krw

Call acpi_sleep_walk() when powering down just like when entering
any other sleep state. This tells acpi wake devices to not wake up
the box until manual powering up resets their status. Makes my Dell
GX520 stay powered off in response to 'halt -p'.

In snaps for a while.

ok marco@ (several times) deraadt@


# 1.135 17-Apr-2009 pirofti

Add device ranking support for future sleep/wake developments.

okay marco@


# 1.134 11-Mar-2009 jordan

Cleanup.. use aml_evalinteger instead of aml_evalname
ok marco@


# 1.133 10-Mar-2009 jordan

Fix cut/paste error.. call _BFS not _PTS
ok marco@


Revision tags: OPENBSD_4_5_BASE
# 1.132 21-Feb-2009 marco

overzealous cut/paste; found by Vladimir Kirillov <proger@uaoug.org.ua>


# 1.131 20-Feb-2009 marco

Oops didn't mean for Debugger() to remain there.


# 1.130 19-Feb-2009 marco

suspend/resume bits so that we can develop this in tree. This is disabled.
code from mlarkin and me
help from art,toby,jordan and several others
ok jordan, go for it deraadt


# 1.129 10-Feb-2009 jordan

Fix null pointer for shutdown if acpi not enabled
ok marco@


# 1.128 04-Feb-2009 kettenis

Some systems that claim to be ACPI 2.0 apparently don't provide an XSDT
address. Make the code fall back on the RSDT on those systems.

From Joerg Niendorf <p3y80@internode.on.net>. Fixes PR 5986.


# 1.127 17-Jan-2009 krw

A couple of stray malloc/memset -> malloc(M_ZERO), a bit of knf and
lose a couple of unnecessary casts. No functional change.

Feedback from kettenis@

ok kettenis@ marco@


# 1.126 06-Nov-2008 marco

Collapse all sanity checks of hids into one function.

Originaly from jordan


Revision tags: OPENBSD_4_4_BASE
# 1.125 02-Jul-2008 fgsch

add acpivideo. for now almost an empty skel so others can work on
suspend/resume. eventually it will also manage output switching and
brightness where supported. prodded by marco@


# 1.124 11-Jun-2008 marco

Fix $OpenBSD$ for once and for all


# 1.123 08-Jun-2008 deraadt

aaa_dev was being set from an uninitialized variable; ok toby


# 1.122 07-Jun-2008 marco

Kill ACPIVERBOSE. It was never used and is worthless to boot.

comments jmc
ok toby


# 1.121 06-Jun-2008 marco

Shrink acpi some more for floppies and while here re-enable acpi on the
floppies. Fixes crazy sony vaio kettenis has.

tested by deraadt
ok deraadt jordan kettenis


# 1.120 01-Jun-2008 marco

Fix \n nit in SMALL_KERNEL.


# 1.119 01-Jun-2008 marco

Re-enable new parser. Lots of testing by various folks. Fits all media
now too.

ok kettenis deraadt


# 1.118 16-May-2008 dlg

back out jordans new parser.

it didnt have enough oks from the right people, it did break machines
that were previously working, and it bloated the RAMDISKS and install
media beyond the constraints of the media.

requested by deraadt@


# 1.117 14-May-2008 jordan

Adding new ACPI Parser code
Old guts of code still exists, needs to be torn out next
ok marco@
ok brad@


# 1.116 27-Apr-2008 jcs

add acpithinkpad(4), an acpi support driver for ibm/lenovo
thinkpads.

provides functionality that the bios did on older, non-acpi
thinkpads like respond to screen brightness keys and toggle the
bluetooth device on and off.

ok deraadt@ and marco@


# 1.115 24-Apr-2008 jsing

Introduce acpiasus(4), a driver for the ACPI based hotkeys found in many
ASUS laptops (including the ASUS EeePC) - largely based on NetBSD's
asus(4) driver. On the ASUS EeePC this allows us to enable/disable
wireless, change screen brightness and use the volume keys.

ok jsg@, weingart@


# 1.114 12-Apr-2008 kettenis

Get rid of acpi_s5 global variables; simply send SIGUSR2 instead of SUGUSR1
to tell init(8) to power down the machine.

ok krw@


Revision tags: OPENBSD_4_3_BASE
# 1.113 21-Jan-2008 jsg

Check against RESET_REG_SUP in FADT before doing the acpi reset dance
after all, not doing so appears to break more machines than it fixes.

ok marco@


# 1.112 05-Jan-2008 kettenis

Never attach more acpicpu(4) devices than cpu(4) devices. Always attach
the first acpicpu(4) device on non-MULTIPROCESSOR kernels, regardless of its
ACPI processor ID.

ok mikeb@


# 1.111 05-Dec-2007 deraadt

Hang acpi(4) below bios(4) instead of mainbus(4). This lets us move acpi(4)
before pcibios(4), and then.... if acpi(4) attaches, skip pcibios(4) since
it messes the machines up. Fixes claudio's HP dl320 G5, and almost assuredly
others.
ok kettenis


# 1.110 05-Dec-2007 deraadt

repair acpiopen/acpiclose and friends to handle the /dev/apmctl interface,
so that apmd(8) can actually work correctly against acpi(4), and thus,
even do the fancy -C/-A stuff; also tested by jmc


# 1.109 02-Dec-2007 fgsch

fix the trailing space for real now.


# 1.108 02-Dec-2007 fgsch

avoid trailing spaces on output; marco@ ok.


# 1.107 27-Nov-2007 kettenis

Fix off-by-one that made us attach acpicpu0 and acpicpu1 on GENERIC kernels.

ok marco@


# 1.106 25-Nov-2007 jsg

When available and ACPI is enabled use the ACPI reset method
to reboot. This is required for some quirky sis machines
and likely others to reboot.

Tested by many. 'commit early commit often' deraadt@


# 1.105 19-Nov-2007 deraadt

sigh, amd64 still uses that stupid acpi_enabled variable...


# 1.104 19-Nov-2007 deraadt

softc's come initialized to 0, you know...


# 1.103 19-Nov-2007 deraadt

store revision; from jsg


# 1.102 19-Nov-2007 kettenis

Initialize sc_facs earlier.

ok deraadt@, beck@, canacar@


# 1.101 16-Nov-2007 deraadt

fix the bus_space #define nightmare, so that amd64 and i386 are much more
uniform. as a result shared code like acpi needs less #ifdef's
ok marco kettenis


# 1.100 15-Nov-2007 deraadt

remove the #ifdef ACPI_ENABLE. the EC codepath is either safe enough (and
will be improved), or it is unsafe because AML assumes it must be there
ok almost everyone


# 1.99 15-Nov-2007 mikeb

Make acpicpu(4) attach to the existing CPUs only.

Problem reported by form@. Fix idea by kettenis@.
Lots of help from deraadt@. Tested by deraadt@, form@ and me.

ok deraadt marco


# 1.98 14-Nov-2007 deraadt

spacing


# 1.97 12-Nov-2007 deraadt

correct : vs , in attach-time messages


# 1.96 06-Nov-2007 deraadt

all ACPI_ENABLE kernels need acpi_interrupt()


# 1.95 06-Nov-2007 deraadt

move acpi_read_pmreg() and acpi_write_pmreg() out of SMALL_KERNEL so
that acpi ramdisks can compile (obvious solution)


# 1.94 05-Nov-2007 canacar

Establish the acpi interrupt earlier. Fixes problems with some
GENERIC.MP kernels not getting acpi interrupts. This is a
workaround until the real issue with late establishment is fixed.
ok weingart@, beck@, gwk@


# 1.93 03-Nov-2007 jordan

Added support for displaying wakeup devices
ok beck@,weingart@,gwk@


# 1.92 11-Oct-2007 kettenis

When walking the tree yo find _PRT methods, check whether the device is
actually enabled and functioning. Similar to what we do for _INI.

ok weingart@, beck@


# 1.91 08-Oct-2007 krw

More simple memset(,0,) -> M_ZERO changes. In this batch move to
size(*p) as the first malloc() parameter where p is declared locally
and thus easy to check. Add M_ZERO to gpe_table allocation in acpi.c
even though there is no obvious bzero or memset nearby.


# 1.90 13-Sep-2007 mk

Remove unnecessary memset() (see aml_evalnode(9)) and a bit of KNF.

ok toby


# 1.89 13-Sep-2007 weingart

Implement ACPI 6.5.1 spec tree walk for _STA and _INI.
Fixes mk's laptop. No regressions so far (thank you to the
testers).

ok gwk@, mk@, marco@


Revision tags: OPENBSD_4_2_BASE
# 1.88 17-Apr-2007 mk

Now that acpidock no longer hangs machines during boot (at least
according to the reports I've been getting) it should be safe to enable
it again, so remove the #if 0 stuff around it.

ok marco


# 1.87 11-Apr-2007 jordan

Added changes for C-State
ok marco@


# 1.86 26-Mar-2007 marco

Calculate minutes left more accurately.

From giovanni <qgiovanni@gmail.com>


Revision tags: OPENBSD_4_1_BASE
# 1.85 03-Mar-2007 mk

Stop looking for acpidock until we fix that stuff that breaks all pre-60
series ThinkPads with acpidock(4) enabled.


# 1.84 22-Feb-2007 marco

We were getting no interrupts in .MP. There are 2 fixes; one in
acpi_machdep.c and move the interrupt establishment down after ioapic
has been initialized.

ok jordan


# 1.83 21-Feb-2007 kettenis

Make an acpi-enabled GENERIC.MP work on systems that only support PIC mode.

ok marco@


# 1.82 19-Feb-2007 jordan

Added acpi_map_address function for mapping GAS/IO registers
ok marco@


# 1.81 18-Feb-2007 jordan

Added dynamic perfect hash for opcodes
ok marco@


# 1.80 18-Feb-2007 marco

Shave off another K pre compression.
Shuffle SMALL_KERNEL functions around so that we only have a single #ifndef


# 1.79 17-Feb-2007 marco

Add SMALL_KERNEL throught acpi to make it fit on boot media.
ok deraadt


# 1.78 13-Feb-2007 marco

Add smarter _INI code. This helps with some dissapearing devices.

ok kettenis jordan


# 1.77 31-Jan-2007 gwk

Introduce acpi_hasprocfvs thats "ACPI Has Processor Frequency and Voltage
Scalling" a global flag set by acicpu attach to indicate that ACPI is
handling fvs through the _PSS objects. This will fix disappearing
EST/powernow when booting kernels with acpi enabled, as acpicpu isnt
compiled into GENERIC yet. Also in cases where acpicpu is built in and
legacy EST/powernow work but acpi is missing the _PSS object, the legacy
routines will be used.

tested by dim@, and Nick Nauwelaerts <nick AT nauwelaerts.net>
ok marco@, dim@


# 1.76 22-Jan-2007 mk

Actually look for docking stations by looking for _DCK nodes.

ok marco


# 1.75 02-Jan-2007 marco

On some systems EC must be attached as the first device. Move EC from
generic _HID devices to its own routine to force first attach. Bug found
and debugged by joris@

Code mostly written by joris@


# 1.74 26-Dec-2006 marco

Add polling to devices that require it such as acpibat and acpitz.
Use the same tsleep/wakeup mechanism as the interrupt code to keep it non-
concurrent.


# 1.73 21-Dec-2006 deraadt

even more knf love


# 1.72 21-Dec-2006 marco

Remove some leftover acpiec gpe/interrupt code.


# 1.71 20-Dec-2006 deraadt

oops, name already used


# 1.70 20-Dec-2006 deraadt

rename __acpi_enable_gpe(); ok marco


# 1.69 20-Dec-2006 deraadt

acpi_unmap_pmregs() is not used, ok marco


# 1.68 19-Dec-2006 deraadt

simplify some low-level methods substantially (shrinking code); ok marco jordan


# 1.67 06-Dec-2006 mk

malloc(..., M_NOWAIT) with no NULL check is bad. If allocation fails,
complain, clean up, and bail out of attach.

input and ok gwk


# 1.66 28-Nov-2006 jordan

Fixed clearing arg for acpi_init_pic
ok kettenis@


# 1.65 27-Nov-2006 kettenis

Don't spam dmesg.


# 1.64 27-Nov-2006 jordan

Fixes for acpiprt routing
Post-parse fixup for namerefs
ok kettenis@


# 1.63 25-Nov-2006 marco

Don't handle acpiev events inside the gpe handler since this causes deadlocks.

ok jordan@


# 1.62 15-Nov-2006 kettenis

Hook up acpi apic interrupt routing support to generic acpi code.

ok jordan@


# 1.61 02-Nov-2006 marco

Kill acpiec attach function and add its functionality to the generic _HID
device attach

prodded and ok jordan


# 1.60 02-Nov-2006 marco

Fix comments to reflect reality.


# 1.59 19-Oct-2006 marco

Replace ACPI ioctls with APM ioctls. This makes ACPI talk the same protocol
as APM therefore userspace can now retrieve power information in the same
fashion as all other apm arches (zaurus, macppc, i386 etc).

To use force the use of acpi device like so: apmd -f /dev/acpi

ok jordan@


# 1.58 19-Oct-2006 jordan

Fixed GPE debug levels
Fixed ref counter on temporary bufferfields
DSDT parsing fully working on IBM Thinkpad
ok marco@


# 1.57 15-Oct-2006 jordan

Removed old GPE handler code remnants
ok marco@


# 1.56 12-Oct-2006 jordan

Massive ACPI changes
Fixed ACPI leaks
new ACPI API
ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.55 30-Jun-2006 gwk

KNF, No functional change,
"go at it" marco@


# 1.54 30-Jun-2006 jordan

Support for calling Embedded Controller via new GPE interface
ok marco@


# 1.53 30-Jun-2006 jordan

Added new code for GPE handling
Also works with ACPIEC; need to fixup acpiec.c to remove original interrupt handler
ok marco@


# 1.52 30-Jun-2006 jordan

Added offset argument for acpi read/write pmreg routines (new GPE code)
ok marco@


# 1.51 31-May-2006 todd

do not call acpi_powerdown() unless acpi has successfully initialized
ok gwk@ canacar@


# 1.50 29-May-2006 canacar

Add embedded controller support.
ok marco@ deraadt@ testing & ok gwk@


# 1.49 19-May-2006 canacar

Minimal thermal zone driver. Monitors thermal zone temperature,
shuts down the system if the 'critical temperature' is reached.
ok marco@


# 1.48 11-Apr-2006 marco

KNF.


# 1.47 11-Apr-2006 gwk

Add acpi_resume(struct acpi_softc *sc), which calls some AML methods,
detected at attach time, currently unused.

ok marco@


# 1.46 11-Apr-2006 gwk

Discover some ACPI sleep related methods, and call them at the
appropriate time in acpi_enter_sleep_state().

ok marco@


Revision tags: OPENBSD_3_9_BASE
# 1.45 26-Feb-2006 marco

Add acpicpu as a device.

ok jordan@


# 1.44 23-Feb-2006 marco

remove dead function.

ok jordan@


# 1.43 22-Feb-2006 marco

Print out all tables it found during autoconf. We'll need this data in
the future.


# 1.42 22-Feb-2006 marco

Make dmesg less verbose.

ok jordan.


# 1.41 22-Feb-2006 jordan

Added parsing of SSDT tables
ok marco@


# 1.40 22-Feb-2006 jordan

Added new aml_register to notify by device id
Moved powerdown code to button device
ok marco@


# 1.39 21-Feb-2006 marco

Readjust debug levels that crept in too low over the last few days.
KNF


# 1.38 21-Feb-2006 marco

Rig the power button so that the machine shuts down correctly.


# 1.37 20-Feb-2006 marco

Add acpibtn device. Remains disabled for now.


# 1.36 19-Feb-2006 jordan

Added code for aml_notify GPE notification callback
ok marco@


# 1.35 19-Feb-2006 grange

Fix acpi_enter_sleep_state() by replacing current ugly hack
with mostly correct sleeping state transition code.

ok brad@ marco@


# 1.34 19-Feb-2006 marco

jordan:
Enable interrupts.
Added peripheral code to make this work.

marco:
Replace softintr and other types of interrupts with a handler with context.
Fixed interrupt magic for GPEs.
Some KNF

This is work done by jordan and I but has to be a single commit.


# 1.33 17-Feb-2006 marco

Remove earlier introduced delays. We really only need them during autconf.

ok jordan@


# 1.32 17-Feb-2006 marco

Add a generic delay function as per the ACPI spec.

from jordan@


# 1.31 17-Feb-2006 marco

Use tsleep instead of delay when !cold.


# 1.30 17-Feb-2006 marco

Argh, we need delays between reads/writes. The backend reads (i2c most likely)
are so slow that values come back as 0 sometimes. We have to do this
despite the spec saying we shouldnt have to. Guess having a faster
parser than others can hurt sometimes.

Idea from jordan.


# 1.29 17-Feb-2006 jordan

Fixed code for handling _INI
Rearranged debug statements
ok marco@


# 1.28 16-Feb-2006 jordan

Added stub for calling _INI
Fixed aml_dowhile for NULL return
ok marco@


# 1.27 16-Feb-2006 jordan

Fixed parser/evaluator to retrieve battery info+state
Cleanup of parser
Dell laptops return battery status
ok marco@


# 1.26 06-Feb-2006 brad

fix a typo so this will compile on amd64.


# 1.25 04-Feb-2006 marco

When ACPI_ENABLED wasn't set but the devices were it had an unknown variable.

Pointed out by ian@.


# 1.24 03-Feb-2006 jordan

Rewrote aml parser yet again...
Fixed bus_space_map for i386/amd64
Added _BST and _BIF to acpibat to get status/info
ok marco@


# 1.23 20-Jan-2006 jordan

More evaluator changes
Removed old evaluator code
Fixed _HID value
ok marco@


# 1.22 19-Jan-2006 jordan

Added support for read/write of PCI Config space
Help jason@
ok marco@


# 1.21 18-Jan-2006 jordan

Added new evaluation routines
Added helper methods for buffer/field evaluation
Fixed broken indentation on dsdt.c

ok marco@


# 1.20 17-Jan-2006 jordan

Major overhaul of the aml parser. Evaluater temporarily disabled.

ok marco@


# 1.19 06-Jan-2006 grange

Check for ACPI control availability only if ACPI_ENABLE defined.


# 1.18 06-Jan-2006 grange

Informative error messages on attach, prettier dmesg.


# 1.17 06-Jan-2006 grange

printf -> dnprintf conversion for debug messages.


# 1.16 05-Jan-2006 grange

Find SLP_TYPx values and store them for future use in
acpi_enter_sleep_state().


# 1.15 05-Jan-2006 grange

Random KNF and cleanup


# 1.14 05-Jan-2006 grange

Provide symbolic names for the acpi sleeping states and use it.


# 1.13 28-Dec-2005 marco

Moved DEVNAME to a acpidev.h

ok jordan@


# 1.12 18-Dec-2005 sturm

add back comment about wrong code

ok marco


# 1.11 16-Dec-2005 marco

Remove test for kbd_reset. Prompted deraadt@, ok jordan@


# 1.10 16-Dec-2005 jordan

Added hooks for powerdown/power button press


# 1.9 16-Dec-2005 jordan

Cleaned up common power management register i/o
Added enter sleep state ioctl
Hooked power button interrupt
ok marco@


# 1.8 16-Dec-2005 marco

Remove autoconf chatter


# 1.7 16-Dec-2005 jordan

Moved DSDT AML parsing into parent ACPI driver
ok marco@


# 1.6 14-Dec-2005 marco

Add AC device glue.


# 1.5 14-Dec-2005 marco

Adjust debug printing levels to something more sane.


# 1.4 13-Dec-2005 marco

Add glue to attach devices to apci.


# 1.3 07-Dec-2005 marco

KNF


Revision tags: OPENBSD_3_8_BASE
# 1.2 10-Jul-2005 grange

Spacing cleanup.


# 1.1 02-Jun-2005 tholo

Start on a basic ACPI framework -- does not do much more than read out the
ACPI tables into kernel memory and attach ACPI and HPET timers currently.

In order to test this code, enabling the devices in GENERIC as well as
the ACPI_ENABLE option is needed. This code does not do any thermal
control yet, so this should be done with care depending on the platform.

In the tree so more people can contribute to making this more fully
featured.

Ok niklas@ grange@ tedu@


# 1.385 14-May-2020 kettenis

Bring back the acpi(4) changes. This time around only use the ACPI _CRS
information on ACPI 5.0 and later.

ok krw@, patrick@


# 1.384 11-May-2020 jca

More accurate remaining power reporting with disparate batteries

Overall remaining power was computed as the average of the remaining
power of each battery, in percents. This doesn't work with batteries of
different capacity, so use the total capacity and remaining power
instead.

Reported by lidstah, successful tests by lidstah and benno@, ok kettenis@


# 1.383 08-May-2020 kettenis

Move parsing of _CRS into acpi(4) and pass the parsed address and interrupt
information in struct acpi_attach_args.

ok mpi@


Revision tags: OPENBSD_6_7_BASE
# 1.382 14-Apr-2020 kettenis

Implement acpi_getprop(), the ACPI equivalent of OF_getprop().

ok patrick@


# 1.381 12-Apr-2020 kettenis

Add support for the _CCA method. This method indicates whether DMA is
cache-coherent or not. To implement this, acpi(4) gets two bus_dma tags
and passes the appropriate one when attaching devices based on _CCA.
On i386/amd64, where for all practical purpose DMA is always cache-coherent,
the two tags are the same. But on arm64 they are distinct.

ok patrick@


# 1.380 07-Apr-2020 visa

Abstract the head of knote lists. This allows extending the lists,
for example, with locking assertions.

OK mpi@, anton@


# 1.379 03-Apr-2020 mpi

Fix incoherencies of errors returned by various *kqfilter routines.

EOPNOTSUPP is returned when there's no "filterops" for a given fd.
EINVAL when the requested filter isn't supported by the "filterops".
ENXIO means the underlying device is no longer valid/present.

ok visa@


# 1.378 20-Feb-2020 visa

Replace field f_isfd with field f_flags in struct filterops to allow
adding more filter properties without cluttering the struct.

OK mpi@, anton@


# 1.377 31-Dec-2019 visa

Use C99 designated initializers with struct filterops. In addition,
make the structs const so that the data are put in .rodata.

OK mpi@, deraadt@, anton@, bluhm@


# 1.376 31-Dec-2019 mpi

Convert infinite sleeps to {m,t}sleep_nsec(9).

ok kn@


# 1.375 31-Dec-2019 jsg

include sys/sensors.h for sensor use
don't depend on indirect inclusion from acpidev.h


Revision tags: OPENBSD_6_6_BASE
# 1.374 07-Sep-2019 kettenis

Revert acpipci(4) changes. Too many systems are broken. We'll try again
after 6.6 as been released. The acpireg.h change stays behind.


# 1.373 28-Aug-2019 kettenis

Use ACPI information to attach PCI busses like we do on arm64. There are a
few additional quirks though, and attaching the PCI busses is delayed to
replicate the existing code more closely. That may be changed in the
future. Also tweak how we handle MSI support and respect to ACPI flag
that says we shouldn't attempt to use MSIs.

Some fallout is expected.

ok patrick@


# 1.372 27-Aug-2019 deraadt

On amd64 the bootup and resume paths for SP/BP/SP were not performing
msr/pat, clock-startup, firmware upgrade, fpu initialization, and cpu
soft-state initialization in the same order. That kind of chaos is
crazy and fragile. Improve the acpi resume call sequence and unify order
of the operations.


# 1.371 02-Jul-2019 jcs

acpi_set_gpehandler: turn edge arg into flags, add support for
passing GPE_DIRECT which directly executes the supplied callback and
clears a level interrupt rather than going through acpi_taskq which
can be slow when other driver callbacks are in the queue.

This should not be used for GPE handlers that need to do anything
ACPI-related.

ok kettenis


# 1.370 10-Jun-2019 kettenis

Print proper ACPI version number.

ok mlarkin@


# 1.369 08-Jun-2019 kettenis

When establishing the mapping between ACPI device nodes and PCI devices,
skip PCI host bridges and devices that aren't there.

ok patrick@


# 1.368 07-Jun-2019 kettenis

Introduce acpi_getsta() and use it to eliminate some duplicated code.

ok patrick@


# 1.367 12-May-2019 kettenis

Make sure the extended PM register descriptions actually exist before
using them. Fixes machines with ancient ACPI 1.0 (such as recent
hypervisors).

ok deraadt@


# 1.366 12-May-2019 kettenis

Add support for mapping ACPI PM registers using the "extended" gasio
representation from the FADT. Mostly fixes the Lanner NCA-1510.

ok lteo@, mlarkin@, deraadt@


# 1.365 11-May-2019 lteo

Fix incorrect error message.

ok kettenis@


# 1.364 08-May-2019 tedu

free size. the allocation is the header plus the length.


Revision tags: OPENBSD_6_5_BASE
# 1.363 04-Apr-2019 kettenis

Avoid potential null-pointer dereference. Found by Kent R. Spillner.

ok deraadt@, jsg@


# 1.362 20-Jan-2019 jsg

fix SMALL_KERNEL build after changes in rev 1.361
reported by naddy@ ok deraadt@


# 1.361 19-Jan-2019 tedu

Add a pwraction sysctl that controls what the power button does on acpi.
By default, nothing changes -- shutdown is initiated. But allows turning
power button into a sleep button if desired.
(grudging) ok from a few parties


# 1.360 26-Oct-2018 kettenis

Add acpipci(4) on amd64. For now this only calls the PCI-specific _OSC
method to let the ACPI implementation know what features we support.


Revision tags: OPENBSD_6_4_BASE
# 1.359 30-Aug-2018 kettenis

Move softbutton check before the check that skips attaching certain acpi
devices. Fixes regression caused by matching on _CID in addition to
matching on _HID.

ok matthieu@, mlarkin@


# 1.358 25-Aug-2018 kettenis

Match ACPI devices based on _CID if no match for _HID is found.

ok mpi@


# 1.357 19-Aug-2018 kettenis

Add support for multiple PCI segments. Only really implemented for arm64
for now as amd64/i386 firmware still caters for legacy OSes that only
support a single PCI segment.

ok patrick@


# 1.356 03-Aug-2018 kettenis

Let ahci(4) match on _CLS instead of _HID when attaching at acpi(4). Avoids
having to add many more _HID entries to the match table.

ok deraadt@, mlarkin@


# 1.355 10-Jul-2018 kettenis

Make legacy interrupts work in acpipci(4).

ok patrick@


# 1.354 05-Jul-2018 kettenis

Add acpipci(4), a driver that supports generic ECAM-compatible PCI host
bridges based on information provided by ACPI.

ok mlarkin@


# 1.353 04-Jul-2018 kettenis

Properly pass around the PCI "chipset tag" in acpi(4) and refactor
acpimcfg(4) to call an MD initialization functions that sets up a tag for
PCI ECAM.

ok guenther@, mlarkin@, krw@


# 1.352 01-Jul-2018 kettenis

Move acpi(4) attach glue into acpi_machdep.c.

ok guenther@, deraadt@


# 1.351 01-Jul-2018 kettenis

Add support for _DSD Device Properties.

ok mlarkin@


# 1.350 30-Jun-2018 kettenis

Add intr_enable() function, intended for MI use to amd64 and i386 and use
this in the acpi(4) suspend/resume code paths.

ok deraadt@


# 1.349 29-Jun-2018 kettenis

Use <stdint.h> types.

ok mpi@, jung@, krw@, deraadt@


# 1.348 29-Jun-2018 mlarkin

ACPI: Allow (with warning) GPE handler reassignment, instead of returning
a failure code.

ok kettenis


# 1.347 26-Jun-2018 mlarkin

knf


# 1.346 26-Jun-2018 kettenis

Let acpi(4) pass down the bus dma tag.

ok deraadt@


# 1.345 25-Jun-2018 kettenis

Reorganize acpi(4) code a bit in preparation for upcoming arm64 support.

ok deraadt@


# 1.344 20-May-2018 kettenis

Wake up the acpi thread after scheduling a gpio event.

ok mpi@


# 1.343 17-May-2018 kettenis

Sprinkle some #ifndef SMALL_KERNEL such that RAMDISK kernels cuild again.

pointed out by stsp@


# 1.342 17-May-2018 kettenis

Implement GenericSerialBus OpRegion support.

ok mlarkin@


# 1.341 27-Mar-2018 kettenis

Add acpicmos(4), a driver that implements SystemCMOS OperationRegion
access support. This fixes machines where the AML doesn't check whether
support for this OperationRegion type has been registered by the OS.

ok mlarkin@


Revision tags: OPENBSD_6_3_BASE
# 1.340 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.339 10-Feb-2018 deraadt

Syncronize filesystems to disk when suspending. Each mountpoint's vnodes
are pushed to disk. Dangling vnodes (unlinked files still in use) and
vnodes undergoing change by long-running syscalls are identified -- and
such filesystems are marked dirty on-disk while we are suspended (in case
power is lost, a fsck will be required). Filesystems without dangling or
busy vnodes are marked clean, resulting in faster boots following
"battery died" circumstances.
Tested by numerous developers, thanks for the feedback.


# 1.338 08-Feb-2018 deraadt

After flushing disks, try again to release all memory. We don't need to
save dirty memory to the hibernate space.


# 1.337 08-Feb-2018 deraadt

Toss all releaseable memory, because fragmentation can get in the way
of allocating the hibernate playpen.


# 1.336 08-Feb-2018 deraadt

sr_quiesce() is a new approach for ensuring that softraid drains
output to the disks.

This is part of a larger suspend/resume filesystem-safety diff, which
has been worked on for a couple of months already. Tests by job, krw,
beck, benno, and others. Sometimes even by snapshot users...


# 1.335 29-Nov-2017 kettenis

Add support for GPIO-signaled events.

ok mlarkin@


# 1.334 16-Nov-2017 jcs

Add PCI attachment for dwiic(4) needed by Intel 100 Series machines

ok kettenis


# 1.333 14-Nov-2017 kettenis

Add a few more PNP IDs to the skip lists.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.332 17-Aug-2017 stsp

Make dwiic(4) attach its ACPI parent dependencies.
This change makes the Asus E200HA keyboard work.

Original analysis and patch by Cesare Gargano
ok kettenis@


# 1.331 09-Aug-2017 dcoppa

Add "PNP0303" (8042 PS/2 Controller) to acpi_isa_hids[]

ok kettenis@


# 1.330 22-Jul-2017 jcs

acpibat: look for _BIX first before falling back on _BIF

ACPI 4.0 deprecated _BIF for battery status so some newer machines
are exporting _BIX which extends _BIF's package with a few extra
fields. When using _BIX, export the new discharge cycle count as a
sensor.

feedback from kettenis and anton
ok deraadt


# 1.329 20-Jul-2017 kettenis

Start a list of isa(4) device names such that we can avoid printing them
as "not configured".

ok mikeb@, millert@, deraadt@


# 1.328 08-Apr-2017 deraadt

Sensors are run as callbacks inside tasks. During suspend a sensor
could be running inside a driver that will be force-detached, or due
to tsleep end up disrupting the softstate/hardstate contract. At
suspend time, quisce all these callbacks by waiting for completion.
This issue has never been observed for real, but may be implicated in
suspend/resume failures.
ok kettenis guenther mlarkin


# 1.327 08-Apr-2017 deraadt

Two prototypes for acpi_maptable; merge to one place


Revision tags: OPENBSD_6_1_BASE
# 1.326 27-Mar-2017 deraadt

Now that hibernate_alloc() only has clean success/failure, don't
need to call hibernate_free() to clean up a partial mess.
ok mlarkin kettenis


# 1.325 13-Mar-2017 deraadt

allow ramdisks to build; ok jcs


# 1.324 12-Mar-2017 jcs

try to attach acpisbs first and if successful, don't attach acpibat


# 1.323 02-Mar-2017 natano

Add a new sysctl machdep.lidaction. The sysctl works as follows:

machdep.lidaction=0 # do nothing
machdep.lidaction=1 # suspend
machdep.lidaction=2 # hibernate

lidsuspend is just an alias for lidaction, so if you change one, the
other one will have the same value. The plan is to remove
machdep.lidsuspend eventually when people have upgraded their
/ets/sysctl.conf.

discussed with deraadt, who came up with the new MIB name
no objections mlarkin
ok stsp halex jcs


# 1.322 28-Feb-2017 natano

Untangle abstract sleep modes from ACPI sleep states. This paves the way
for further work in the area.

idea and ok deraadt
ok mlarkin


# 1.321 22-Feb-2017 jcs

add acpisbs, an acpi smart battery subsystem driver reading data
over smbus

currently disabled because it conflicts with acpibat

ok deraadt, kettenis


# 1.320 14-Jan-2017 kettenis

Revert aml_rdpciaddr changes; breaks several machines that were working
before.


# 1.319 11-Jan-2017 jsg

Increase the size of the buffers used for _HID and _CID strings to be
able to fit "Hyper_V_Gen_Counter_V1".

ok mlarkin@ deraadt@


# 1.318 08-Jan-2017 kettenis

Make aml_rdpciaddr() work for for devices behind bridges. The code now
uses the mapping between AML nodes and PCI devices that we establish.
Because _INIT methods may end up calling aml_rdpciaddr(), make sure we
create that mapping early. Also handle devices that aren't actually present.
These devices are now included in the mapping and reads will return an
all-ones patterm whereas writes are a no-op..

ok mlarkin@


# 1.317 25-Oct-2016 pirofti

Add the resource index to aml_parse_resource() callback function.

This is needed by an upcoming acpiec commit that handles machines breaking
the current ACPI specifications.

Change suggested by and ok kettenis@, guenther@


# 1.316 18-Sep-2016 guenther

If the APIC table (MADT) isn't present then ci_acpi_proc_id won't be set
when acpicpu tries to attach, leading to a NULL dereference. The ACPI
processor object on the Dells where this was reported is bogus and useless
anyway so apply the same test when deciding whether to attach to processor
objects as acpicpu would when matching them to cpus.

Problem reported by Eivind Eide (xenofil (at) gmail.com) and
Johan Huldtgren (johan+openbsd-bugs (at) huldtgren.com).
Testing by Eivind Eide.
ok kettenis@ mlarkin@


# 1.315 03-Sep-2016 naddy

Write the system time back to the RTC every 30 minutes.
This fixes the problem that long-running machines which were not
shut down properly would reboot with a badly offset system time.

hints and ok kettenis@


# 1.314 31-Aug-2016 mlarkin

Don't put a device in the wake device list if _STA says it's not there.

Fixes at least one machine that woke up from sleep / poweroff immediately.

ok kettenis@, tested by kettenis and martijn@


# 1.313 28-Jul-2016 kettenis

Store the acpi processor ID/UID in struct cpu_info, and use it to attach
acpicpu(4) drivers to the right cpu(4).

ok mlarkin@, guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.312 10-Jun-2016 kettenis

The power management timer has been made optional in ACPI 5.0A, so don't
attempt to attach acpitimer(4) if the timer isn't present.

Pointed out by jcs@

ok jcs@


# 1.311 08-May-2016 kettenis

Make sure devices listed by a device's _DEP method are attached before
we attach the device itself. The _DEP methode, introduced in ACPI 5.0,
is a hint that the device in question depends on OpRegion support from
the devices listed by _DEP, so we have to order them the proper way.
To prevent us from attaching those devices again when we encounter them
later walking down the device tree, keep track of our attempts to
attach a device driver to them using aflag in the aml_node structure.

ok guenther@


# 1.310 25-Apr-2016 pirofti

We attach acpihpet(4) and acpiec(4) without using the HID, so do not
report it in the dmesg.

OK deraadt@, mlarkin@.


# 1.309 02-Apr-2016 kettenis

Reduce dmesg spam by knocking out some acpi hardware IDs that don't need a
driver.

ok mpi@


# 1.308 29-Mar-2016 kettenis

Check if a device is present (using _STA) in acpi_foundhid. This prevents
us from reporting devices as "not configured" that aren't actually present,
and allows us to remove duplicated code from several drivers.

ok deraadt@, ok jsg@


# 1.307 29-Mar-2016 kettenis

Implement a cleaner way to prevent acpivout(4) from attaching if we have
a platform-specific method of controlling the backlight.

ok millert@, mlarkin@, jung@


# 1.306 28-Mar-2016 kettenis

Adding each and every new acpi device driver to acpi_foundhid() is
gettinga bit of the burden. And it really isn't how our autoconf
framework is supposed to work for busses that can be enumerated. All
the drivers already check for a matching _HID in their attach
function. So we can just drop the checks in acpi_foundhid() and let
the drivers sort themselves out.

Print a "not configured" line for devices for which we don't attach adriver.
This may be a bit much as there are devices for which we will not have a
driver. This will be removed if it causes too much dmesg spam.

ok deraadt@, mlarkin@


Revision tags: OPENBSD_5_9_BASE
# 1.305 17-Jan-2016 jsg

Take acpi_foundhid() outside of #ifndef SMALL_KERNEL so the ramdisk
kernel can find and attach dwiic(4) and sdhc(4).


# 1.304 17-Jan-2016 jsg

remove an unused global variable


# 1.303 14-Jan-2016 kettenis

Attach dwiic(4) on the Intel Bay Trail i2c controllers as well.


# 1.302 12-Jan-2016 kettenis

Improve emulation of PM registers on Hardware-redcued ACPI by emulating
SPL_TYPx, SLP_EN and WAK_STS using the new SLEEP_STATUS_REG and
SLEEP_CONTROL_REG registers. Makes the transition into S5 (powerdown) work
on the ASUS X205TA.

ok deraadt@, mlarkin@


# 1.301 12-Jan-2016 jcs

Add dwiic, a driver for the Synopsys DesignWare i2c controller found
on the Samsung ATIV Book 9 laptop. This initial version only
supports ACPI config/attachment.

Add ihidev, a HID-over-i2c driver largely based on uhidev. dwiic
handles attaching ihidev devices found in ACPI.

Add ims, a HID-over-i2c mouse/trackpad driver to get basic cursor
and button functionality from HID-compliant i2c trackpads.

ok kettenis deraadt


# 1.300 11-Jan-2016 kettenis

Make sdhc(4) attach to hardware IDs "80860F14" and "PNP0FFF".


# 1.299 09-Jan-2016 kettenis

Initial support for Hardware-Reduced ACPI. This skips the legacy mode checks
by emulating the PM_CNT register such that SCI_EN is always enabled. It also
skip attaching acpitimer(4) since the relevant registers are no longer there.

ok deraadt@, mlarkin@


# 1.298 06-Jan-2016 kettenis

Follow the spec and check the SCI_EN bit to see if ACPI has already
been enabled and don't bother enabling it again if it is. This should fix
systems that don't implement legacy mode and therefore set ACPI_ENABLE and
ACPI_DISABLE to zero.

ok mlarkin@, yasuoka@


# 1.297 23-Nov-2015 reyk

Back out the CRITICAL change, turn it back from 10% to 25%. Some
people with old/broken batteries didn't like this change, and the
underlying heuristics are wrong.


# 1.296 22-Nov-2015 reyk

The "CRITICAL" APM battery level, that is exported/faked by the ACPI
stack, is currently set to 25%. With 25% battery left, a typical
modern laptop will have at least one hour runtime left. It is an
arbitrary value, and other APM power layers in the kernel set CRITICAL
to something like 5% or 10%, so set it to 10% for now.

OK mlarkin@


# 1.295 28-Sep-2015 deraadt

In low-level suspend routines, set cold=2. In tsleep(), use this to
spit out a ddb trace to console. This should allow us to find suspend
or resume routines which break the rules. It depends on the console
output function being non-sleeping.... but that's another codepath which
should try to be safe when cold is set.
ok kettenis


# 1.294 06-Sep-2015 deraadt

clarify malloc arguments (base + length...)


# 1.293 28-Aug-2015 deraadt

fairly simple sizes for free(); ok tedu


# 1.292 20-Aug-2015 kettenis

Respect the access size when reading or writing to pci config space.
This version also makes sure writes are properly aligned, preventing
the panics seen with the previous incarnation of this change.

Fixes battery status passthrough in vmware, brightness keys on my x220
and who knows what else.

ok mlarkin@, deraadt@


Revision tags: OPENBSD_5_8_BASE
# 1.291 04-Aug-2015 deraadt

If we are setting ACPI_PM1_BM_RLD do it only once, and remember to
repeat this action at resume time.
ok mlarkin guenther


# 1.290 29-Jul-2015 deraadt

Something subtle in previous revision is locking up suspend or booting
on some laptops. Mailing lists threads mention x220 and Asus machines.
Reverting for now.


# 1.289 21-Jul-2015 kettenis

iRespect the access size when reading or writing to pci config space.
Fixes battery status passthrough in vmware and who knows what else.

Tested by many.

ok mlarkin@, deraadt@


# 1.288 17-Jul-2015 mlarkin

fix comments


# 1.287 26-Mar-2015 jsg

Use the 0x10 extended capability to determine if the trackstick
buttons are wired to the trackpad and need to be re-routed to the
trackstick. Without this change the buttons on 2015 thinkpads
get picked up as extended buttons that show up as scroll up/down.

Remove the X1 Carbon 2015 (LEN0048) and X250 (LEN0046) from the
top button area/soft buttons quirks list. Also avoid using the
quirk list entirely if the capability bit is set.

Based on work from jcs@ with feedback from mpi@
ok mpi@ jcs@


# 1.286 20-Mar-2015 kettenis

The ACPI standard says that a PCI function number of FFFF can be used to refer
to all functions on a device. This makes no sense in the context of accessing
PCI config space. Nevertheless stsp@ has a machine with AML that tries to
do config space reads from a device where _ADR encodes function number FFFF.
This is almost certainly buggy AML, but since Linux (and presumably Windows)
boot just fine on this machine we need to work around this. Linux effectively
treats function FFFF as function 7, which isn't present on this machine.
Instead we simulate a read from a non-existent device by returning a bit
pattern of all ones. Writes to function FFFF will still panic.

ok mlarkin@


# 1.285 16-Mar-2015 deraadt

Reduce use of SMALL_KERNEL in acpi, bringing acpiec driver and more gpe
handling into RAMDISK. This is now possible because the install media
has ample room. The goal is to reduce special cases where we may be
skipping (unknown) important operations...
ok mlarkin kettenis


# 1.284 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.283 07-Feb-2015 deraadt

New framework that allows hibernate to pass in entropy from it's fresh
boot.
ok mlarkin


# 1.282 06-Feb-2015 mlarkin

Fix a hibernate crash on some machines due to unmapping a page that
may not have been mapped previously (in the failure to hibernate case).

Also ensure that the lowmem ptp is mapped in all cases (not just MP).

ok kettenis


# 1.281 17-Jan-2015 deraadt

resume_randomness() before spinning up other cpus...


# 1.280 15-Jan-2015 jsg

Define a new wscons mouse type for Synaptics clickpad devices that lack
physical buttons. This will be used if the acpi pnpid for the mouse
matches a list provided by Synaptics found in Linux.

Combined with the xenocara changes this will remove the need for an xorg
config file for the x240/t440/t540 generation of thinkpads.

ok matthieu@ shadchin@ kettenis@


# 1.279 11-Jan-2015 kettenis

Tolerate checksum errors in ACPI tables (but complain about them). Apparently
there are machines out there with broken BIOSen with such checksum errors in
thier RSDT tables. Windows runs fine on these machines, so it probably does
not check. Linux ignores checksum errors as well (and prints a warning).

ok deraadt@, krw@


# 1.278 18-Dec-2014 deraadt

suspend and resume the RNG (at what looks like the right spots)
ok reyk djm


# 1.277 09-Dec-2014 doug

Replace some malloc(n*size,...) calls with mallocarray().

ok tedu@ deraadt@


# 1.276 06-Dec-2014 mpi

If "machdep.lidsuspend" is not set, do not go back to sleep if the lid
is still closed when resuming. This allows docked laptop to be resumed
without opening them.

ok mlarkin@


# 1.275 23-Nov-2014 mlarkin

If a machine has no _LIDs, then none can be open. This diff fixes a problem
where machines without any _LIDs were going back to sleep after hibernate
due to the recent suspend-if-lid-closed-on-resume diff. Noticed on qemu,
which doesn't have any _LIDs.

discussed with deraadt


# 1.274 18-Nov-2014 krw

Nuke yet more obvious #include duplications.

ok miod@


# 1.273 08-Nov-2014 mlarkin

If resuming from sleep (zzz/ZZZ) and the lid is still closed, go back to
sleep. This prevents accidental lid flex or slight opening in a backpack
from waking the machine up and leaving it resumed (powered on).

ok deraadt@


# 1.272 26-Sep-2014 kettenis

Rework piglet and pig allocation. Currently the piglet gets allocated
deep down in the suspend path, where it is really hard to recover from
allocation failure. So allocate the piglet early on in the suspend path.
Also change the piglet and piglet allocation functions to use km_alloc(9)
instead of doing pmemrange magic. This removes a bunch of code which, in the
case of the piglet allocation, is broken since it results in a NULL pointer
dereference. Also switch the piglet allocation to not wait. If we can't
allocate 16MB of phys contig memory on a halfway modern machine we're almost
certainly under a lot of memory pressure and we're better off not trying to
hibernate anyway.

ok mlarkin@


# 1.271 25-Sep-2014 kettenis

Bring back rev 1.249. Now that mp_setperf() has been fixed, it should be
safe to restore perflevel when resuming. Should fix the issues people
see with the processor running at full speed heating up the machine while
the apm command reports that the CPUs are running at a low speed.

ok dcoppa@, deraadt@


# 1.270 23-Sep-2014 kettenis

Release the acpi lock around calls to wsdisplay_suspend() and
wsdisplay_resume(). The X server may adjust the display brightness just
before/after the VT switch when suspending/resuming. In fact, it may
already be blocked adjusting the display brightness for some reason
unrelated to the suspend request.

The VT switch request will eventually time out, but the delay is annoying
and the failed VT switch will lead to problems after resume.

Needed for a future update of the xf86-video-intel driver.

ok mlarkin@


# 1.269 19-Sep-2014 kettenis

Use config_suspend_all(9).


# 1.268 08-Sep-2014 kettenis

Clear the PME Status bit on PCI devices in response to Device Wake events.
Fixes the interrupt storm on the 2009 Mac mini with WOL enabled on its
nfe(4) interface.

ok mlarkin@


Revision tags: OPENBSD_5_6_BASE
# 1.267 20-Jul-2014 mlarkin

Support hibernating to softraid crypto volumes.

much help and ok from deraadt@


# 1.266 16-Jul-2014 mlarkin

Reenable hibernate RLE support and flush+zero all memory after unpack.


# 1.265 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.264 12-Jul-2014 kettenis

Add a function to drop all clean pages on the page daemon queues and call
it when we hibernate.

ok mlarkin@, miod@, deraadt@


# 1.263 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.262 11-Jul-2014 mlarkin

Flush the buffercache to 16MB on hibernate and restore its previous max
size (kern.bufcachepercent) on resume, for better hibernate performance.

ok beck@


# 1.261 10-Jul-2014 blambert

remove unneeded header

ok mlarkin@


# 1.260 01-Jul-2014 kettenis

Adding an additonal delay after each access to SystemMemory or SystemIO
during autoconf makes no sense.

ok deraadt@


# 1.259 23-Jun-2014 kettenis

SystemMemory is used to access memory mapped registers on some machines, so we
must use the bus_space(9) API to access it instead of memcpy(9). Also make
sure we properly align access to fields that need more than byte alignment.

ok guenther@


# 1.258 21-May-2014 mlarkin

Fix invalid / missing format specifiers in several dnprintf statements
(used only when ACPI_DEBUG is enabled)

From a diff on tech@ by Fabian Raetz, with a minor change by me, tested
on i386 and amd64 with ACPI_DEBUG enabled and disabled.


# 1.257 25-Apr-2014 mlarkin

Defer the free of the hibernate structures until later, when it is safe to
do so.

Freeing the hibernate structures too early leads to inadvertent calls into
splx when we aren't ready for them yet. Found when trapping splx calls in
the resume path.

discussed with deraadt@ at m2k14


# 1.256 13-Apr-2014 mlarkin

Fix a badly worded debug message that implied the entire machine was going
to sleep, instead of just the acpi thread going to sleep.


# 1.255 31-Mar-2014 mpi

Change the order of operations during a suspend/resume cycle and
call bufq_quiesce() after executing the DVACT_QUIESCE handlers.

This should be safe since no disk nor controller drivers have such
handler but it will allow us to detach sd(4) devices attached to a
USB bus.

Another benefit pointed out by kettenis@ is that drivers that need
to read a firmware from the disk should be able to do it at resume
time in a DVACT_WAKEUP handler.

ok kettenis@, deraadt@


# 1.254 13-Mar-2014 dlg

get rid of the assumption that the head of the alldevs list is the
"mainbus" device. this breaks when mpath is enabled because it
attaches before mainbus and therefore takes the head position.

have autoconf provide device_mainbus() which looks up mainbus_cd,
and use that instead.

discussed with deraadt who just wants mpath stuff to move forward
despite there being many ways to shine this particular turd.


Revision tags: OPENBSD_5_5_BASE
# 1.253 21-Feb-2014 deraadt

Do the lid control further on in the resume sequence, since this is
AML, and will do WAITOK operations. While cold, that is a bad idea.
We seem to be safe with respect to the spec, I do not see any event
debounce issues here.
ok kettenis, tested by many


# 1.252 20-Jan-2014 claudio

Instead of checking sc->sc_revision <= 1 check that the
fadt->hdr_revision <= 1. This is a better check since it checks
the version of the proper descriptor. This allows the alix APU
to reboot since those boards have currently a wrong sc->sc_revision.
Committing this now so that it can be tested easier.
With and OK jsg@ also fine by derradt@


# 1.251 24-Dec-2013 kettenis

If the FADT has its SMI_CMD set to zero, assume we're only ACPI-only hardware
and don't need to disable SMI ownership of the ACPI hardware registers.

ok mlarkin@


# 1.250 23-Dec-2013 kettenis

Revert previous commit. It makes resume hang on some MP systems running
GENERIC.MP.


# 1.249 16-Dec-2013 kettenis

Restore hw.setperf upon resume like we do for apm(4) on i386. Seems we need
to do this fairly late in the resume to avoid hangs.

ok deraadt@, mlarkin@


# 1.248 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


# 1.247 06-Nov-2013 mpi

Add support for Power Resources for Dx states and the necessary hook
for PCI devices. This hook should be called twice, before and after
changing the power state of a PCI device.

Before setting the device to the new state, the ACPI layer will notify
every power resources linked to the device for that state and make sure
they are turned "_ON". After changing the state of the device, it will
decrement the reference of every power resources linked to that device
for the old state and turn them "_OFF" if they are no longer referenced.

This fixes the no-USB after resume problem seen on various ThinkPad,
problem initialy diagnosed with Alexander Polakov.

ok kettenis@, deraadt@


Revision tags: OPENBSD_5_4_BASE
# 1.246 01-Jun-2013 mlarkin

Stop printing ACPI wakeup devices in dmesg after the 16th wakeup device.

Helps with VMware VMs that report hundreds of wakeup devices.

ok reyk@, deraadt@


# 1.245 31-May-2013 bluhm

Fix the build for a kernel without wd and pciide in its config.
OK deraadt@ tedu@


# 1.244 30-May-2013 deraadt

Enforce ca_activate tree-walks over the entire heirarchy for all events,
cleaning up some shutdown-hook related code on the way.
(A few drivers related to sparc are still skipped at kettenis' request)
ok kettenis mlarkin, tested by many others too


# 1.243 18-Apr-2013 deraadt

pin the acpi thread to the BSP, because AML and SMI cannot be trusted. I
was certain we were doing this already, but it turns out no...


Revision tags: OPENBSD_5_3_BASE
# 1.242 24-Dec-2012 guenther

In acpiioctl(), Return though the splx() path if suser() returns an error

ok deraadt@


# 1.241 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


# 1.240 04-Oct-2012 ehrhardt

Use information provided by ACPI to attach secondary PCI host bridges.

ok kettenis@


# 1.239 07-Sep-2012 kettenis

Add a new function called acpi_pci_min_powerstate() that returns the lowest
power state for a PCI device for the power state (currently S3 or S4) we're
transitioning into. This function is probably too simplistic; there is a lot
of (silly) complexity in the ACPI spec.

ok deraadt@


Revision tags: OPENBSD_5_2_BASE
# 1.238 13-Jul-2012 pirofti

Disable acpivideo(4) on ASUS laptops.

There are some models that panic whenever an event is received.
Some cope, some don't. In order to get this right acpiec(4) requires
major rework. Until then let machines boot.

Discussed with deraadt@.


# 1.237 11-Jul-2012 mlarkin

for now, restrict hibernate to root in the acpi ioctl kernel interface.
normal rules for controlling this interface through apmd still apply.

ok deraadt@


# 1.236 09-Jul-2012 deraadt

tiny little oops in untested hibernate block


# 1.235 09-Jul-2012 deraadt

clean up ioctl() case statements


# 1.234 09-Jul-2012 deraadt

acpi_disable_onegpe() is not used; ok pirofti


# 1.233 24-May-2012 kettenis

If _MIN > _MAX consider a WordBus resource to be invalid. Fixes a panic on
the IBM x3550-M4.

ok mikeb@


# 1.232 29-Mar-2012 mlarkin

Missing #ifdef in previous diff


# 1.231 29-Mar-2012 mlarkin

Prevent hibernation attempts on machines with unsupported disk controllers.
The same check is also performed later in the hibernate sequence, but by
then it's too late to cleanly unwind (presently).

ok deraadt


# 1.230 26-Mar-2012 deraadt

simplify conditionals in acpi_sleep_state()
ok mlarkin


# 1.229 26-Mar-2012 deraadt

hook in the hibernate request code; half of this diff is from mlarkin
ok mlarkin


Revision tags: OPENBSD_5_1_BASE
# 1.228 20-Sep-2011 deraadt

Very early in suspend while the clock is still ticking realtime,
save the clock back to the rtc. The zaurus already did this.
ok phessler miod kettenis


Revision tags: OPENBSD_5_0_BASE
# 1.227 02-Jul-2011 nicm

kqueue attach functions should return an errno or 0, not a plain 1. Fix
the obvious cases to return EINVAL and ENXIO.

ok tedu deraadt


# 1.226 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.225 16-Jun-2011 pirofti

Import acpitoshiba driver.

This is originally written by Hiroyuki Aizu, ported and integrated
in our tree by Javier Vazquez <javama4 AT gmail> with tweaks and
suggestions by me.

Import prodded by deraadt@ and the lack of testers on tech@.


# 1.224 27-Apr-2011 jcs

attach acpithinkpad to newer lenovo models like the x120e

ok deraadt@


# 1.223 22-Apr-2011 jordan

Fix uninitialzied variables and formatting strings (-Wxxx errors)


Revision tags: OPENBSD_4_9_BASE
# 1.222 02-Jan-2011 jordan

Re-add code for ACPI taskqueue, eliminates need for state vars in acpi softc
Serialize processing ACPI tasks for sleep/powerdown/GPE event
ok deraadt@


# 1.221 31-Oct-2010 guenther

Revert last commit: it breaks resume on Thinkpads
"then please back it out" deraadt@


# 1.220 26-Oct-2010 jordan

Add task queue for ACPI gpe and notify handlers
This fixes eject on Dell Latitude dock and an issue on the Dell Mini
battery update.
ok deraadt


# 1.219 07-Oct-2010 jordan

Create separate functions for enabling wake and runtime gpes.
Remove old suspend/resume gpewalk function
ok deraadt, mlarkin


# 1.218 05-Oct-2010 jordan

split acpi_enable_onegpe to separate enable/disable functions
ok deraadt


# 1.217 05-Oct-2010 jordan

Added acpi_disable_allgpes function to clear GPE status
ok deraadt


# 1.216 05-Oct-2010 deraadt

clean up acpi_softc use more


# 1.215 05-Oct-2010 deraadt

delete a bit of #if 0 code


# 1.214 05-Oct-2010 deraadt

hide use of our global acpi_softc pointer a bit better


# 1.213 29-Sep-2010 deraadt

grab the acpi thread lock very early in acpi thread startup; not that it
is likely that anyone will race us... but why be careless.
ok kettenis


# 1.212 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.211 29-Aug-2010 jasper

- add a check for FWRITE here too.

ok miod@


# 1.210 27-Aug-2010 kettenis

Make PCI Power Management optional, and only enable it when acpi(4) attaches.

ok deraadt@


Revision tags: OPENBSD_4_8_BASE
# 1.209 08-Aug-2010 kettenis

Close race between the acpi thread and wscons ioctls running in process
context. We might rip this out post-release and replace it with a solution
that serializes things in a more obvious way.

ok deraadt@, miod@ and "those crazy texans who ok anything"


# 1.208 08-Aug-2010 deraadt

Report the acpi sleep states when on a RAMDISK; ok krw


# 1.207 07-Aug-2010 kettenis

Enforce acpi register access rescrictions.

ok mlarkin@, deraadt@, tested by marco@


# 1.206 07-Aug-2010 deraadt

upon resume, notify all the acpiac and acpibat drivers to update
their status. some laptops do not do this automatically.
ok kettenis canacar


# 1.205 06-Aug-2010 marco

Always call _PSW on _LID devices that support it to enable lid open to
resume the machine. Conversely disable it when the machine wakes up.

Tested by several ok deraadt


# 1.204 06-Aug-2010 deraadt

As part of a revamp of the PM1/GPE code, I write this diff to look for
unmanaged EN & STS bits in the PM1 register at interrupt time and report
them. As a side effect this splits the STS acknowledgement into two writes
(for power, and sleep) instead of one. The printf that is added (to spot
unmanaged STS bits) has not yet been triggered as far as we know.

Before the "write to PM1 registers at the right offset" diff went in, this
was not neccessary. But newer thinkpads do not have a working soft power
button without this diff.

We have no idea why.
ok mlarkin kettenis


# 1.203 05-Aug-2010 deraadt

PM1 power button events were generating 'fake events' to acpibtn PWRB,
but if there is no such device these were being tossed. Instead, arrange
for all power button handlers to flag a softc variable which is processed
by the thread; so that one place will know to request a powerdown (psignal
to init)
ok marco mlarkin


# 1.202 05-Aug-2010 deraadt

rename a variable which is annoyingly poorly named


# 1.201 05-Aug-2010 mlarkin

Write to the correct offset for various ACPI pmreg registers. Writing to
the correct register does properly clear the wake status bits and fixed
event bits, which allows some machines to have more than 1 suspend cycle.

bug spotted by deraadt@

tested by deraadt@ and myself on a half-dozen machines and others on
hackers@

ok deraadt@, marco@


# 1.200 05-Aug-2010 deraadt

Fix the power button. The pm1 register is 16 bits in size.
My mistake, a while back.
tested by krw


# 1.199 04-Aug-2010 kettenis

Print supported sleep states.

ok marco@, phessler@, deraadt@


# 1.198 03-Aug-2010 mlarkin

Use the proper flag for re-enabling certain hardware events (power btn,
sleep btn). The (incorrect) flag prevoiously used coincidentally had the
same value, so this shouldn't cause any different behavior than before.

tested on a variety of machines (i386, amd64, sp, mp) - no behavioral
change seen

ok deraadt@


# 1.197 28-Jul-2010 marco

Make all gasio failures look and behave the same.


# 1.196 28-Jul-2010 mlarkin

Delay a little longer on suspend before giving up. Fixes machines that
sometimes don't suspend because the sleep transition takes too long
(symptoms like failing to sleep but the backlight stays on).

ok deraadt@


# 1.195 27-Jul-2010 deraadt

remove XXX; EC can now be attached early when needed; ok jordan


# 1.194 27-Jul-2010 deraadt

more brackets feels good


# 1.193 27-Jul-2010 deraadt

XXX annotate a block in acpi_enter_sleep_state() that is pretty worrying


# 1.192 27-Jul-2010 deraadt

horrific indentation that nearly made me puke


# 1.191 27-Jul-2010 mlarkin

Set the sleeping indicator light on machines that support it, in case SMI
doesn't do it for us. Shows the 'moon' LED on older Thinkpads when
sleeping.

Tested on T43p, W500, T510 by me and on X61s by deraadt@.

ok deraadt@


# 1.190 27-Jul-2010 jordan

Early initialization of acpiec if ECDT table exists
Fixes hang when booting thinkpads while docked
ok deraadt


# 1.189 26-Jul-2010 deraadt

remove merge error which was commited


# 1.188 26-Jul-2010 pirofti

Add support for Sony ACPI hotkeys via a new driver: acpisony(4).

Currently it only works for the suspend button and tries to do right for
the brightness events, but I haven't found a Sony laptop that like to do
right so far.

In the future I want to make the brightness keys work on all Sony's and
also add support for the zoom hotkeys and whatever other funky keys I can
find on those things.

Okay deraadt@.


# 1.187 22-Jul-2010 deraadt

pretty large cleanup of SMALL_KERNEL


# 1.186 21-Jul-2010 deraadt

in the acpi GPE handler, late ack level interrupts not edge interrupts.
Duh.


# 1.185 21-Jul-2010 deraadt

Merge the edge and level gpe handlers into one (now possible since the
gpe structure tells us whether the gpe is edge or level)


# 1.184 21-Jul-2010 deraadt

delete #if 0 code we are not going to use; ok marco


# 1.183 20-Jul-2010 deraadt

make the mask code for acpi_enable_onegpe() match other similar code


# 1.182 20-Jul-2010 deraadt

Handle the acpi interrupt controller with proper edge/level handling,
wrapped in spltty to avoid racing against the interrupt controller.
Repair the gpe bit masking code while there, and do operations in the
right order
ok kettenis mlarkin, help from jordan at figuring out the order of
operations.


# 1.181 20-Jul-2010 matthew

When we're preparing to go to sleep, only call bufq_restart() if other
preparations failed.

ok kettenis@, thib@, deraadt@


# 1.180 19-Jul-2010 deraadt

permit compilation with ACPI_DEBUG


# 1.179 19-Jul-2010 deraadt

there is no need for a seperate dsdt_softc pointer; dsdt does not have a
softc but is just a part of the single acpi driver.
ok jordan


# 1.178 19-Jul-2010 deraadt

acpi_delay() is no longer needed; ok jordan


# 1.177 18-Jul-2010 mlarkin

Clear event status on resume, to avoid going back to sleep/reboot/shutdown
immediately after resume. Tested by a bunch of people on hackers@ on
various hardware. Fixes at least Toshiba NB200 and Gateway LT3103u.

ok marco


# 1.176 13-Jul-2010 deraadt

rename some thread related interfaces so they are logical


# 1.175 10-Jul-2010 jordan

Verify _ADR bdf is correct (some systems return 0xffff)
Works on Dell x9xx and Studio 1555


# 1.174 09-Jul-2010 jordan

Clear the noise


# 1.173 08-Jul-2010 jordan

Add mapping for ACPI device to PCI bdf (match autoconf tree)
Simplify resource parsing function to use buffer argument
Convert namespace linked lists to use queue macros
ok marco@, deraadt@


# 1.172 06-Jul-2010 deraadt

Use spltty() to lock downcalls from apm against the information being
modified by the acpi thread.
ok kettenis


# 1.171 06-Jul-2010 deraadt

Don't use a workq to do the suspend, because it races aginst the acpi
thread. Instead, just tell the acpi thread to do the suspend for us.
This makes apmd & zzz work correctly. While here, have acpithinkpad
attempt to post the event to apm before prompting the suspend itself.
ok kettenis marco mlarkin


# 1.170 05-Jul-2010 mlarkin

Reset register SCI_EN on resume. The ACPI spec clearly states we don't have
to do this, but many machines don't, or forget to, which leads to all sorts
of problems on resume (broken battery stats, overheating, failure to resume
more than once, etc).

tested by many, ok marco@, deraadt@


# 1.169 01-Jul-2010 thib

do bufq quiesce again, since vnd/the bufq_done call in
biodone has been fixed.


# 1.168 01-Jul-2010 jordan

Backout recent AML changes, breaks on R210 and others


# 1.167 01-Jul-2010 jordan

Simplified aml_parse_resource call, cleanup code
ok marco


# 1.166 01-Jul-2010 jordan

Fix subordinate bus # for multi-root PCI buses
ok marco, kettenis


# 1.165 30-Jun-2010 thib

Disable/partially backout the bufq quiesce changes as this
is causing havoc with vnds and release must be buildable.


# 1.164 29-Jun-2010 deraadt

Fix ramdisks (new code was placed inside a SMALL_KERNEL chunk)


# 1.163 29-Jun-2010 jordan

Add support for mapping ACPI to PCI devices
ok kettenis, deraadt


# 1.162 29-Jun-2010 kettenis

Call bufq_quiesce() on suspend to stop all I/O and bufq_restart on resume.
Makes suspend/resume work much better while doing disk I/O.

ok mlarkin@


# 1.161 29-Jun-2010 tedu

60 is a more reasonable guess for number of minutes in an hour than 100.
spotted by TeXitoi. ok deraadt


# 1.160 27-Jun-2010 jordan

Add _acpi_matchhids routine, precursor to pci device match


# 1.159 27-Jun-2010 mlarkin

Clear ACPI fixed event status on resume (power buttons/etc) so that some
machines don't immediately resume after suspending to S3.

Tested by kettenis@ and I on 5 or 6 machines, deraadt@ "yeah, looks good"


# 1.158 27-Jun-2010 jordan

Cleaned up acpi_maptable, adds to table list directly


# 1.157 07-Apr-2010 deraadt

If we have done a wsdisplay_suspend(), and ACPI decides it does not want to
suspend, we must roll back by calling wsdisplay_resume() or the virtual
console switching will be locked forever
ok oga


# 1.156 07-Apr-2010 kettenis

Make suspend/resume work on MP machines (running an MP kernel). Joint work
from mlarkin@ and me, with some amd64 fixes thrown in by deraadt@

ok marco@, deraadt@, pirofti@, mlarkin@


# 1.155 31-Mar-2010 kettenis

Bring /dev/apm support in line with apm(4). Make the suspend button send
a suspend request event to apmd(8) instead of suspending immediately. Also
keep track of whether /dev/apm and /dev/apmctl are currently open, such that
we can still suspend immediately if apmd(8) isn't running.

ok deraadt@, marco@, pirofti@, jsing@, oga@


# 1.154 30-Mar-2010 oga

Prevent the apmd/x races for good.

When we hit suspend time, go through all wsdisplays on the system. if
they are in mode MAPPED, but not MODE_DUMBFB then if possible do a full
vt switch to a !mapped vt, and prevent switching back until resume time.
This has to be called from MD code because this involves userland
running so that X can run the vt switch signal handler. This way, any
case where we are using the "poke registers from userland" model, we
will not be on the hardware when we go down, so the kernel can actually
handle thing properly.

Tested on several acpi laptops (by kettenis@ and ian@), x40 (me and
beck@ at LEAST) and zaurus (me). Maybe others, but if so I forgot who at
this time..

Idea from deraadt somewhere over the Faroe Islands (I thought of a
similar thing myself a while ago). Much prompting from him. Ok and
comments miod@


# 1.153 25-Mar-2010 oga

Fix & vs. && in the kqfilter code.

Based on a comparison with the apm code.

ok deraadt@, kettenis@


Revision tags: OPENBSD_4_7_BASE
# 1.152 26-Nov-2009 mlarkin

Visual (and on some machines, audio) feedback while the machine is
resuming via calls to aml _SST method. This also makes the "moon" LED on
thinkpads to blink during resume and cease when resume is completed.

ok pirofti@


# 1.151 26-Nov-2009 deraadt

Toggle into "cold" when we we disable interrupts, because tsleeping
in the AML interpreter and context switching into userland processes
tends to sometimes pop the ACPI trampoline page out of our address
space. Makes lots more machines work.
ok kettenis mlarkin


# 1.150 24-Nov-2009 jsg

in the resume path evaluate _WAK before dealing with device drivers
like the spec says, not after.

ok mlarkin@


# 1.149 24-Nov-2009 deraadt

abort suspends on SMP systems until that gets written; ok mlarkin


# 1.148 23-Nov-2009 mlarkin

pci should return the result of it's children's suspend/resume calls back
to its parent.

handle suspend failure case and unwind if devices have an activate function
that returns failure.

ok deraadt@, kettenis@


# 1.147 23-Nov-2009 mlarkin

acpi* devices need to call their children on suspend/resume.
ok deraadt@


# 1.146 23-Nov-2009 pirofti

Remove ACPI_SLEEP_ENABLED checks.

This enables by default the suspend/resume paths in the kernel.

Okay deraadt@.


# 1.145 23-Nov-2009 deraadt

knf and spacing that are super annoying


# 1.144 23-Nov-2009 mlarkin

Hook up Theo's new children suspend/resume function, as well as more ACPI
suspend/resume glue.

ok deraadt@


# 1.143 26-Oct-2009 deraadt

Do not do strncmp comparisons on dv_xname because longer device names which
look similar could arrive in the future. Instead, compare directly against
dv_cfdata->cf_driver->cd_name
Issue originally spotted by miod


# 1.142 26-Oct-2009 jordan

Verify checksum+address when loading ACPI tables.
Some systems had invalid entries in RSDT/XSDT.
ok marco@


# 1.141 23-Jul-2009 cnst

New aibs(4) driver for ASUSTeK AI Booster (ACPI ATK0110) hardware monitoring,
with sensor state support through limits provided by the ACPI.

Tested on several ASUS motherboards kindly networked by Sam Fourman Jr.

ok deraadt marco jordan


Revision tags: OPENBSD_4_6_BASE
# 1.140 03-Jun-2009 pirofti

Import acpi power resource driver. This is needed for suspend/resume logic.

Okay jordan@ and marco@.


# 1.139 03-Jun-2009 pirofti

Add acpivideo support. This adds brightness support for all laptops
except thinkpads, they will use the acpithinkpad driver. The driver is
also hooked into wsconsole. So brightness can be adjusted via:

$ wsconsctl display.brightness=<percentage>

This is very helpfull on some laptops that have a nasty bios and get two
steps instead of one when pressing the brightness button.

Tested on various dell, fujitsu, acer, samsung and other laptops.

Okay marco@, miod@. Suggestions from kettenis@.
Lots of reviews and help from miod@, thanks!


# 1.138 03-Jun-2009 jordan

Added ejectable bay for IDE cdroms
ok marco@


# 1.137 30-Apr-2009 marco

Oga changed apm ioctls and forgot to grep. Temporary work around by hitting
the sleep button when we get any request. This interface will chnage at
some point.
ok oga


# 1.136 19-Apr-2009 krw

Call acpi_sleep_walk() when powering down just like when entering
any other sleep state. This tells acpi wake devices to not wake up
the box until manual powering up resets their status. Makes my Dell
GX520 stay powered off in response to 'halt -p'.

In snaps for a while.

ok marco@ (several times) deraadt@


# 1.135 17-Apr-2009 pirofti

Add device ranking support for future sleep/wake developments.

okay marco@


# 1.134 11-Mar-2009 jordan

Cleanup.. use aml_evalinteger instead of aml_evalname
ok marco@


# 1.133 10-Mar-2009 jordan

Fix cut/paste error.. call _BFS not _PTS
ok marco@


Revision tags: OPENBSD_4_5_BASE
# 1.132 21-Feb-2009 marco

overzealous cut/paste; found by Vladimir Kirillov <proger@uaoug.org.ua>


# 1.131 20-Feb-2009 marco

Oops didn't mean for Debugger() to remain there.


# 1.130 19-Feb-2009 marco

suspend/resume bits so that we can develop this in tree. This is disabled.
code from mlarkin and me
help from art,toby,jordan and several others
ok jordan, go for it deraadt


# 1.129 10-Feb-2009 jordan

Fix null pointer for shutdown if acpi not enabled
ok marco@


# 1.128 04-Feb-2009 kettenis

Some systems that claim to be ACPI 2.0 apparently don't provide an XSDT
address. Make the code fall back on the RSDT on those systems.

From Joerg Niendorf <p3y80@internode.on.net>. Fixes PR 5986.


# 1.127 17-Jan-2009 krw

A couple of stray malloc/memset -> malloc(M_ZERO), a bit of knf and
lose a couple of unnecessary casts. No functional change.

Feedback from kettenis@

ok kettenis@ marco@


# 1.126 06-Nov-2008 marco

Collapse all sanity checks of hids into one function.

Originaly from jordan


Revision tags: OPENBSD_4_4_BASE
# 1.125 02-Jul-2008 fgsch

add acpivideo. for now almost an empty skel so others can work on
suspend/resume. eventually it will also manage output switching and
brightness where supported. prodded by marco@


# 1.124 11-Jun-2008 marco

Fix $OpenBSD$ for once and for all


# 1.123 08-Jun-2008 deraadt

aaa_dev was being set from an uninitialized variable; ok toby


# 1.122 07-Jun-2008 marco

Kill ACPIVERBOSE. It was never used and is worthless to boot.

comments jmc
ok toby


# 1.121 06-Jun-2008 marco

Shrink acpi some more for floppies and while here re-enable acpi on the
floppies. Fixes crazy sony vaio kettenis has.

tested by deraadt
ok deraadt jordan kettenis


# 1.120 01-Jun-2008 marco

Fix \n nit in SMALL_KERNEL.


# 1.119 01-Jun-2008 marco

Re-enable new parser. Lots of testing by various folks. Fits all media
now too.

ok kettenis deraadt


# 1.118 16-May-2008 dlg

back out jordans new parser.

it didnt have enough oks from the right people, it did break machines
that were previously working, and it bloated the RAMDISKS and install
media beyond the constraints of the media.

requested by deraadt@


# 1.117 14-May-2008 jordan

Adding new ACPI Parser code
Old guts of code still exists, needs to be torn out next
ok marco@
ok brad@


# 1.116 27-Apr-2008 jcs

add acpithinkpad(4), an acpi support driver for ibm/lenovo
thinkpads.

provides functionality that the bios did on older, non-acpi
thinkpads like respond to screen brightness keys and toggle the
bluetooth device on and off.

ok deraadt@ and marco@


# 1.115 24-Apr-2008 jsing

Introduce acpiasus(4), a driver for the ACPI based hotkeys found in many
ASUS laptops (including the ASUS EeePC) - largely based on NetBSD's
asus(4) driver. On the ASUS EeePC this allows us to enable/disable
wireless, change screen brightness and use the volume keys.

ok jsg@, weingart@


# 1.114 12-Apr-2008 kettenis

Get rid of acpi_s5 global variables; simply send SIGUSR2 instead of SUGUSR1
to tell init(8) to power down the machine.

ok krw@


Revision tags: OPENBSD_4_3_BASE
# 1.113 21-Jan-2008 jsg

Check against RESET_REG_SUP in FADT before doing the acpi reset dance
after all, not doing so appears to break more machines than it fixes.

ok marco@


# 1.112 05-Jan-2008 kettenis

Never attach more acpicpu(4) devices than cpu(4) devices. Always attach
the first acpicpu(4) device on non-MULTIPROCESSOR kernels, regardless of its
ACPI processor ID.

ok mikeb@


# 1.111 05-Dec-2007 deraadt

Hang acpi(4) below bios(4) instead of mainbus(4). This lets us move acpi(4)
before pcibios(4), and then.... if acpi(4) attaches, skip pcibios(4) since
it messes the machines up. Fixes claudio's HP dl320 G5, and almost assuredly
others.
ok kettenis


# 1.110 05-Dec-2007 deraadt

repair acpiopen/acpiclose and friends to handle the /dev/apmctl interface,
so that apmd(8) can actually work correctly against acpi(4), and thus,
even do the fancy -C/-A stuff; also tested by jmc


# 1.109 02-Dec-2007 fgsch

fix the trailing space for real now.


# 1.108 02-Dec-2007 fgsch

avoid trailing spaces on output; marco@ ok.


# 1.107 27-Nov-2007 kettenis

Fix off-by-one that made us attach acpicpu0 and acpicpu1 on GENERIC kernels.

ok marco@


# 1.106 25-Nov-2007 jsg

When available and ACPI is enabled use the ACPI reset method
to reboot. This is required for some quirky sis machines
and likely others to reboot.

Tested by many. 'commit early commit often' deraadt@


# 1.105 19-Nov-2007 deraadt

sigh, amd64 still uses that stupid acpi_enabled variable...


# 1.104 19-Nov-2007 deraadt

softc's come initialized to 0, you know...


# 1.103 19-Nov-2007 deraadt

store revision; from jsg


# 1.102 19-Nov-2007 kettenis

Initialize sc_facs earlier.

ok deraadt@, beck@, canacar@


# 1.101 16-Nov-2007 deraadt

fix the bus_space #define nightmare, so that amd64 and i386 are much more
uniform. as a result shared code like acpi needs less #ifdef's
ok marco kettenis


# 1.100 15-Nov-2007 deraadt

remove the #ifdef ACPI_ENABLE. the EC codepath is either safe enough (and
will be improved), or it is unsafe because AML assumes it must be there
ok almost everyone


# 1.99 15-Nov-2007 mikeb

Make acpicpu(4) attach to the existing CPUs only.

Problem reported by form@. Fix idea by kettenis@.
Lots of help from deraadt@. Tested by deraadt@, form@ and me.

ok deraadt marco


# 1.98 14-Nov-2007 deraadt

spacing


# 1.97 12-Nov-2007 deraadt

correct : vs , in attach-time messages


# 1.96 06-Nov-2007 deraadt

all ACPI_ENABLE kernels need acpi_interrupt()


# 1.95 06-Nov-2007 deraadt

move acpi_read_pmreg() and acpi_write_pmreg() out of SMALL_KERNEL so
that acpi ramdisks can compile (obvious solution)


# 1.94 05-Nov-2007 canacar

Establish the acpi interrupt earlier. Fixes problems with some
GENERIC.MP kernels not getting acpi interrupts. This is a
workaround until the real issue with late establishment is fixed.
ok weingart@, beck@, gwk@


# 1.93 03-Nov-2007 jordan

Added support for displaying wakeup devices
ok beck@,weingart@,gwk@


# 1.92 11-Oct-2007 kettenis

When walking the tree yo find _PRT methods, check whether the device is
actually enabled and functioning. Similar to what we do for _INI.

ok weingart@, beck@


# 1.91 08-Oct-2007 krw

More simple memset(,0,) -> M_ZERO changes. In this batch move to
size(*p) as the first malloc() parameter where p is declared locally
and thus easy to check. Add M_ZERO to gpe_table allocation in acpi.c
even though there is no obvious bzero or memset nearby.


# 1.90 13-Sep-2007 mk

Remove unnecessary memset() (see aml_evalnode(9)) and a bit of KNF.

ok toby


# 1.89 13-Sep-2007 weingart

Implement ACPI 6.5.1 spec tree walk for _STA and _INI.
Fixes mk's laptop. No regressions so far (thank you to the
testers).

ok gwk@, mk@, marco@


Revision tags: OPENBSD_4_2_BASE
# 1.88 17-Apr-2007 mk

Now that acpidock no longer hangs machines during boot (at least
according to the reports I've been getting) it should be safe to enable
it again, so remove the #if 0 stuff around it.

ok marco


# 1.87 11-Apr-2007 jordan

Added changes for C-State
ok marco@


# 1.86 26-Mar-2007 marco

Calculate minutes left more accurately.

From giovanni <qgiovanni@gmail.com>


Revision tags: OPENBSD_4_1_BASE
# 1.85 03-Mar-2007 mk

Stop looking for acpidock until we fix that stuff that breaks all pre-60
series ThinkPads with acpidock(4) enabled.


# 1.84 22-Feb-2007 marco

We were getting no interrupts in .MP. There are 2 fixes; one in
acpi_machdep.c and move the interrupt establishment down after ioapic
has been initialized.

ok jordan


# 1.83 21-Feb-2007 kettenis

Make an acpi-enabled GENERIC.MP work on systems that only support PIC mode.

ok marco@


# 1.82 19-Feb-2007 jordan

Added acpi_map_address function for mapping GAS/IO registers
ok marco@


# 1.81 18-Feb-2007 jordan

Added dynamic perfect hash for opcodes
ok marco@


# 1.80 18-Feb-2007 marco

Shave off another K pre compression.
Shuffle SMALL_KERNEL functions around so that we only have a single #ifndef


# 1.79 17-Feb-2007 marco

Add SMALL_KERNEL throught acpi to make it fit on boot media.
ok deraadt


# 1.78 13-Feb-2007 marco

Add smarter _INI code. This helps with some dissapearing devices.

ok kettenis jordan


# 1.77 31-Jan-2007 gwk

Introduce acpi_hasprocfvs thats "ACPI Has Processor Frequency and Voltage
Scalling" a global flag set by acicpu attach to indicate that ACPI is
handling fvs through the _PSS objects. This will fix disappearing
EST/powernow when booting kernels with acpi enabled, as acpicpu isnt
compiled into GENERIC yet. Also in cases where acpicpu is built in and
legacy EST/powernow work but acpi is missing the _PSS object, the legacy
routines will be used.

tested by dim@, and Nick Nauwelaerts <nick AT nauwelaerts.net>
ok marco@, dim@


# 1.76 22-Jan-2007 mk

Actually look for docking stations by looking for _DCK nodes.

ok marco


# 1.75 02-Jan-2007 marco

On some systems EC must be attached as the first device. Move EC from
generic _HID devices to its own routine to force first attach. Bug found
and debugged by joris@

Code mostly written by joris@


# 1.74 26-Dec-2006 marco

Add polling to devices that require it such as acpibat and acpitz.
Use the same tsleep/wakeup mechanism as the interrupt code to keep it non-
concurrent.


# 1.73 21-Dec-2006 deraadt

even more knf love


# 1.72 21-Dec-2006 marco

Remove some leftover acpiec gpe/interrupt code.


# 1.71 20-Dec-2006 deraadt

oops, name already used


# 1.70 20-Dec-2006 deraadt

rename __acpi_enable_gpe(); ok marco


# 1.69 20-Dec-2006 deraadt

acpi_unmap_pmregs() is not used, ok marco


# 1.68 19-Dec-2006 deraadt

simplify some low-level methods substantially (shrinking code); ok marco jordan


# 1.67 06-Dec-2006 mk

malloc(..., M_NOWAIT) with no NULL check is bad. If allocation fails,
complain, clean up, and bail out of attach.

input and ok gwk


# 1.66 28-Nov-2006 jordan

Fixed clearing arg for acpi_init_pic
ok kettenis@


# 1.65 27-Nov-2006 kettenis

Don't spam dmesg.


# 1.64 27-Nov-2006 jordan

Fixes for acpiprt routing
Post-parse fixup for namerefs
ok kettenis@


# 1.63 25-Nov-2006 marco

Don't handle acpiev events inside the gpe handler since this causes deadlocks.

ok jordan@


# 1.62 15-Nov-2006 kettenis

Hook up acpi apic interrupt routing support to generic acpi code.

ok jordan@


# 1.61 02-Nov-2006 marco

Kill acpiec attach function and add its functionality to the generic _HID
device attach

prodded and ok jordan


# 1.60 02-Nov-2006 marco

Fix comments to reflect reality.


# 1.59 19-Oct-2006 marco

Replace ACPI ioctls with APM ioctls. This makes ACPI talk the same protocol
as APM therefore userspace can now retrieve power information in the same
fashion as all other apm arches (zaurus, macppc, i386 etc).

To use force the use of acpi device like so: apmd -f /dev/acpi

ok jordan@


# 1.58 19-Oct-2006 jordan

Fixed GPE debug levels
Fixed ref counter on temporary bufferfields
DSDT parsing fully working on IBM Thinkpad
ok marco@


# 1.57 15-Oct-2006 jordan

Removed old GPE handler code remnants
ok marco@


# 1.56 12-Oct-2006 jordan

Massive ACPI changes
Fixed ACPI leaks
new ACPI API
ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.55 30-Jun-2006 gwk

KNF, No functional change,
"go at it" marco@


# 1.54 30-Jun-2006 jordan

Support for calling Embedded Controller via new GPE interface
ok marco@


# 1.53 30-Jun-2006 jordan

Added new code for GPE handling
Also works with ACPIEC; need to fixup acpiec.c to remove original interrupt handler
ok marco@


# 1.52 30-Jun-2006 jordan

Added offset argument for acpi read/write pmreg routines (new GPE code)
ok marco@


# 1.51 31-May-2006 todd

do not call acpi_powerdown() unless acpi has successfully initialized
ok gwk@ canacar@


# 1.50 29-May-2006 canacar

Add embedded controller support.
ok marco@ deraadt@ testing & ok gwk@


# 1.49 19-May-2006 canacar

Minimal thermal zone driver. Monitors thermal zone temperature,
shuts down the system if the 'critical temperature' is reached.
ok marco@


# 1.48 11-Apr-2006 marco

KNF.


# 1.47 11-Apr-2006 gwk

Add acpi_resume(struct acpi_softc *sc), which calls some AML methods,
detected at attach time, currently unused.

ok marco@


# 1.46 11-Apr-2006 gwk

Discover some ACPI sleep related methods, and call them at the
appropriate time in acpi_enter_sleep_state().

ok marco@


Revision tags: OPENBSD_3_9_BASE
# 1.45 26-Feb-2006 marco

Add acpicpu as a device.

ok jordan@


# 1.44 23-Feb-2006 marco

remove dead function.

ok jordan@


# 1.43 22-Feb-2006 marco

Print out all tables it found during autoconf. We'll need this data in
the future.


# 1.42 22-Feb-2006 marco

Make dmesg less verbose.

ok jordan.


# 1.41 22-Feb-2006 jordan

Added parsing of SSDT tables
ok marco@


# 1.40 22-Feb-2006 jordan

Added new aml_register to notify by device id
Moved powerdown code to button device
ok marco@


# 1.39 21-Feb-2006 marco

Readjust debug levels that crept in too low over the last few days.
KNF


# 1.38 21-Feb-2006 marco

Rig the power button so that the machine shuts down correctly.


# 1.37 20-Feb-2006 marco

Add acpibtn device. Remains disabled for now.


# 1.36 19-Feb-2006 jordan

Added code for aml_notify GPE notification callback
ok marco@


# 1.35 19-Feb-2006 grange

Fix acpi_enter_sleep_state() by replacing current ugly hack
with mostly correct sleeping state transition code.

ok brad@ marco@


# 1.34 19-Feb-2006 marco

jordan:
Enable interrupts.
Added peripheral code to make this work.

marco:
Replace softintr and other types of interrupts with a handler with context.
Fixed interrupt magic for GPEs.
Some KNF

This is work done by jordan and I but has to be a single commit.


# 1.33 17-Feb-2006 marco

Remove earlier introduced delays. We really only need them during autconf.

ok jordan@


# 1.32 17-Feb-2006 marco

Add a generic delay function as per the ACPI spec.

from jordan@


# 1.31 17-Feb-2006 marco

Use tsleep instead of delay when !cold.


# 1.30 17-Feb-2006 marco

Argh, we need delays between reads/writes. The backend reads (i2c most likely)
are so slow that values come back as 0 sometimes. We have to do this
despite the spec saying we shouldnt have to. Guess having a faster
parser than others can hurt sometimes.

Idea from jordan.


# 1.29 17-Feb-2006 jordan

Fixed code for handling _INI
Rearranged debug statements
ok marco@


# 1.28 16-Feb-2006 jordan

Added stub for calling _INI
Fixed aml_dowhile for NULL return
ok marco@


# 1.27 16-Feb-2006 jordan

Fixed parser/evaluator to retrieve battery info+state
Cleanup of parser
Dell laptops return battery status
ok marco@


# 1.26 06-Feb-2006 brad

fix a typo so this will compile on amd64.


# 1.25 04-Feb-2006 marco

When ACPI_ENABLED wasn't set but the devices were it had an unknown variable.

Pointed out by ian@.


# 1.24 03-Feb-2006 jordan

Rewrote aml parser yet again...
Fixed bus_space_map for i386/amd64
Added _BST and _BIF to acpibat to get status/info
ok marco@


# 1.23 20-Jan-2006 jordan

More evaluator changes
Removed old evaluator code
Fixed _HID value
ok marco@


# 1.22 19-Jan-2006 jordan

Added support for read/write of PCI Config space
Help jason@
ok marco@


# 1.21 18-Jan-2006 jordan

Added new evaluation routines
Added helper methods for buffer/field evaluation
Fixed broken indentation on dsdt.c

ok marco@


# 1.20 17-Jan-2006 jordan

Major overhaul of the aml parser. Evaluater temporarily disabled.

ok marco@


# 1.19 06-Jan-2006 grange

Check for ACPI control availability only if ACPI_ENABLE defined.


# 1.18 06-Jan-2006 grange

Informative error messages on attach, prettier dmesg.


# 1.17 06-Jan-2006 grange

printf -> dnprintf conversion for debug messages.


# 1.16 05-Jan-2006 grange

Find SLP_TYPx values and store them for future use in
acpi_enter_sleep_state().


# 1.15 05-Jan-2006 grange

Random KNF and cleanup


# 1.14 05-Jan-2006 grange

Provide symbolic names for the acpi sleeping states and use it.


# 1.13 28-Dec-2005 marco

Moved DEVNAME to a acpidev.h

ok jordan@


# 1.12 18-Dec-2005 sturm

add back comment about wrong code

ok marco


# 1.11 16-Dec-2005 marco

Remove test for kbd_reset. Prompted deraadt@, ok jordan@


# 1.10 16-Dec-2005 jordan

Added hooks for powerdown/power button press


# 1.9 16-Dec-2005 jordan

Cleaned up common power management register i/o
Added enter sleep state ioctl
Hooked power button interrupt
ok marco@


# 1.8 16-Dec-2005 marco

Remove autoconf chatter


# 1.7 16-Dec-2005 jordan

Moved DSDT AML parsing into parent ACPI driver
ok marco@


# 1.6 14-Dec-2005 marco

Add AC device glue.


# 1.5 14-Dec-2005 marco

Adjust debug printing levels to something more sane.


# 1.4 13-Dec-2005 marco

Add glue to attach devices to apci.


# 1.3 07-Dec-2005 marco

KNF


Revision tags: OPENBSD_3_8_BASE
# 1.2 10-Jul-2005 grange

Spacing cleanup.


# 1.1 02-Jun-2005 tholo

Start on a basic ACPI framework -- does not do much more than read out the
ACPI tables into kernel memory and attach ACPI and HPET timers currently.

In order to test this code, enabling the devices in GENERIC as well as
the ACPI_ENABLE option is needed. This code does not do any thermal
control yet, so this should be done with care depending on the platform.

In the tree so more people can contribute to making this more fully
featured.

Ok niklas@ grange@ tedu@


# 1.384 11-May-2020 jca

More accurate remaining power reporting with disparate batteries

Overall remaining power was computed as the average of the remaining
power of each battery, in percents. This doesn't work with batteries of
different capacity, so use the total capacity and remaining power
instead.

Reported by lidstah, successful tests by lidstah and benno@, ok kettenis@


# 1.383 08-May-2020 kettenis

Move parsing of _CRS into acpi(4) and pass the parsed address and interrupt
information in struct acpi_attach_args.

ok mpi@


Revision tags: OPENBSD_6_7_BASE
# 1.382 14-Apr-2020 kettenis

Implement acpi_getprop(), the ACPI equivalent of OF_getprop().

ok patrick@


# 1.381 12-Apr-2020 kettenis

Add support for the _CCA method. This method indicates whether DMA is
cache-coherent or not. To implement this, acpi(4) gets two bus_dma tags
and passes the appropriate one when attaching devices based on _CCA.
On i386/amd64, where for all practical purpose DMA is always cache-coherent,
the two tags are the same. But on arm64 they are distinct.

ok patrick@


# 1.380 07-Apr-2020 visa

Abstract the head of knote lists. This allows extending the lists,
for example, with locking assertions.

OK mpi@, anton@


# 1.379 03-Apr-2020 mpi

Fix incoherencies of errors returned by various *kqfilter routines.

EOPNOTSUPP is returned when there's no "filterops" for a given fd.
EINVAL when the requested filter isn't supported by the "filterops".
ENXIO means the underlying device is no longer valid/present.

ok visa@


# 1.378 20-Feb-2020 visa

Replace field f_isfd with field f_flags in struct filterops to allow
adding more filter properties without cluttering the struct.

OK mpi@, anton@


# 1.377 31-Dec-2019 visa

Use C99 designated initializers with struct filterops. In addition,
make the structs const so that the data are put in .rodata.

OK mpi@, deraadt@, anton@, bluhm@


# 1.376 31-Dec-2019 mpi

Convert infinite sleeps to {m,t}sleep_nsec(9).

ok kn@


# 1.375 31-Dec-2019 jsg

include sys/sensors.h for sensor use
don't depend on indirect inclusion from acpidev.h


Revision tags: OPENBSD_6_6_BASE
# 1.374 07-Sep-2019 kettenis

Revert acpipci(4) changes. Too many systems are broken. We'll try again
after 6.6 as been released. The acpireg.h change stays behind.


# 1.373 28-Aug-2019 kettenis

Use ACPI information to attach PCI busses like we do on arm64. There are a
few additional quirks though, and attaching the PCI busses is delayed to
replicate the existing code more closely. That may be changed in the
future. Also tweak how we handle MSI support and respect to ACPI flag
that says we shouldn't attempt to use MSIs.

Some fallout is expected.

ok patrick@


# 1.372 27-Aug-2019 deraadt

On amd64 the bootup and resume paths for SP/BP/SP were not performing
msr/pat, clock-startup, firmware upgrade, fpu initialization, and cpu
soft-state initialization in the same order. That kind of chaos is
crazy and fragile. Improve the acpi resume call sequence and unify order
of the operations.


# 1.371 02-Jul-2019 jcs

acpi_set_gpehandler: turn edge arg into flags, add support for
passing GPE_DIRECT which directly executes the supplied callback and
clears a level interrupt rather than going through acpi_taskq which
can be slow when other driver callbacks are in the queue.

This should not be used for GPE handlers that need to do anything
ACPI-related.

ok kettenis


# 1.370 10-Jun-2019 kettenis

Print proper ACPI version number.

ok mlarkin@


# 1.369 08-Jun-2019 kettenis

When establishing the mapping between ACPI device nodes and PCI devices,
skip PCI host bridges and devices that aren't there.

ok patrick@


# 1.368 07-Jun-2019 kettenis

Introduce acpi_getsta() and use it to eliminate some duplicated code.

ok patrick@


# 1.367 12-May-2019 kettenis

Make sure the extended PM register descriptions actually exist before
using them. Fixes machines with ancient ACPI 1.0 (such as recent
hypervisors).

ok deraadt@


# 1.366 12-May-2019 kettenis

Add support for mapping ACPI PM registers using the "extended" gasio
representation from the FADT. Mostly fixes the Lanner NCA-1510.

ok lteo@, mlarkin@, deraadt@


# 1.365 11-May-2019 lteo

Fix incorrect error message.

ok kettenis@


# 1.364 08-May-2019 tedu

free size. the allocation is the header plus the length.


Revision tags: OPENBSD_6_5_BASE
# 1.363 04-Apr-2019 kettenis

Avoid potential null-pointer dereference. Found by Kent R. Spillner.

ok deraadt@, jsg@


# 1.362 20-Jan-2019 jsg

fix SMALL_KERNEL build after changes in rev 1.361
reported by naddy@ ok deraadt@


# 1.361 19-Jan-2019 tedu

Add a pwraction sysctl that controls what the power button does on acpi.
By default, nothing changes -- shutdown is initiated. But allows turning
power button into a sleep button if desired.
(grudging) ok from a few parties


# 1.360 26-Oct-2018 kettenis

Add acpipci(4) on amd64. For now this only calls the PCI-specific _OSC
method to let the ACPI implementation know what features we support.


Revision tags: OPENBSD_6_4_BASE
# 1.359 30-Aug-2018 kettenis

Move softbutton check before the check that skips attaching certain acpi
devices. Fixes regression caused by matching on _CID in addition to
matching on _HID.

ok matthieu@, mlarkin@


# 1.358 25-Aug-2018 kettenis

Match ACPI devices based on _CID if no match for _HID is found.

ok mpi@


# 1.357 19-Aug-2018 kettenis

Add support for multiple PCI segments. Only really implemented for arm64
for now as amd64/i386 firmware still caters for legacy OSes that only
support a single PCI segment.

ok patrick@


# 1.356 03-Aug-2018 kettenis

Let ahci(4) match on _CLS instead of _HID when attaching at acpi(4). Avoids
having to add many more _HID entries to the match table.

ok deraadt@, mlarkin@


# 1.355 10-Jul-2018 kettenis

Make legacy interrupts work in acpipci(4).

ok patrick@


# 1.354 05-Jul-2018 kettenis

Add acpipci(4), a driver that supports generic ECAM-compatible PCI host
bridges based on information provided by ACPI.

ok mlarkin@


# 1.353 04-Jul-2018 kettenis

Properly pass around the PCI "chipset tag" in acpi(4) and refactor
acpimcfg(4) to call an MD initialization functions that sets up a tag for
PCI ECAM.

ok guenther@, mlarkin@, krw@


# 1.352 01-Jul-2018 kettenis

Move acpi(4) attach glue into acpi_machdep.c.

ok guenther@, deraadt@


# 1.351 01-Jul-2018 kettenis

Add support for _DSD Device Properties.

ok mlarkin@


# 1.350 30-Jun-2018 kettenis

Add intr_enable() function, intended for MI use to amd64 and i386 and use
this in the acpi(4) suspend/resume code paths.

ok deraadt@


# 1.349 29-Jun-2018 kettenis

Use <stdint.h> types.

ok mpi@, jung@, krw@, deraadt@


# 1.348 29-Jun-2018 mlarkin

ACPI: Allow (with warning) GPE handler reassignment, instead of returning
a failure code.

ok kettenis


# 1.347 26-Jun-2018 mlarkin

knf


# 1.346 26-Jun-2018 kettenis

Let acpi(4) pass down the bus dma tag.

ok deraadt@


# 1.345 25-Jun-2018 kettenis

Reorganize acpi(4) code a bit in preparation for upcoming arm64 support.

ok deraadt@


# 1.344 20-May-2018 kettenis

Wake up the acpi thread after scheduling a gpio event.

ok mpi@


# 1.343 17-May-2018 kettenis

Sprinkle some #ifndef SMALL_KERNEL such that RAMDISK kernels cuild again.

pointed out by stsp@


# 1.342 17-May-2018 kettenis

Implement GenericSerialBus OpRegion support.

ok mlarkin@


# 1.341 27-Mar-2018 kettenis

Add acpicmos(4), a driver that implements SystemCMOS OperationRegion
access support. This fixes machines where the AML doesn't check whether
support for this OperationRegion type has been registered by the OS.

ok mlarkin@


Revision tags: OPENBSD_6_3_BASE
# 1.340 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.339 10-Feb-2018 deraadt

Syncronize filesystems to disk when suspending. Each mountpoint's vnodes
are pushed to disk. Dangling vnodes (unlinked files still in use) and
vnodes undergoing change by long-running syscalls are identified -- and
such filesystems are marked dirty on-disk while we are suspended (in case
power is lost, a fsck will be required). Filesystems without dangling or
busy vnodes are marked clean, resulting in faster boots following
"battery died" circumstances.
Tested by numerous developers, thanks for the feedback.


# 1.338 08-Feb-2018 deraadt

After flushing disks, try again to release all memory. We don't need to
save dirty memory to the hibernate space.


# 1.337 08-Feb-2018 deraadt

Toss all releaseable memory, because fragmentation can get in the way
of allocating the hibernate playpen.


# 1.336 08-Feb-2018 deraadt

sr_quiesce() is a new approach for ensuring that softraid drains
output to the disks.

This is part of a larger suspend/resume filesystem-safety diff, which
has been worked on for a couple of months already. Tests by job, krw,
beck, benno, and others. Sometimes even by snapshot users...


# 1.335 29-Nov-2017 kettenis

Add support for GPIO-signaled events.

ok mlarkin@


# 1.334 16-Nov-2017 jcs

Add PCI attachment for dwiic(4) needed by Intel 100 Series machines

ok kettenis


# 1.333 14-Nov-2017 kettenis

Add a few more PNP IDs to the skip lists.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.332 17-Aug-2017 stsp

Make dwiic(4) attach its ACPI parent dependencies.
This change makes the Asus E200HA keyboard work.

Original analysis and patch by Cesare Gargano
ok kettenis@


# 1.331 09-Aug-2017 dcoppa

Add "PNP0303" (8042 PS/2 Controller) to acpi_isa_hids[]

ok kettenis@


# 1.330 22-Jul-2017 jcs

acpibat: look for _BIX first before falling back on _BIF

ACPI 4.0 deprecated _BIF for battery status so some newer machines
are exporting _BIX which extends _BIF's package with a few extra
fields. When using _BIX, export the new discharge cycle count as a
sensor.

feedback from kettenis and anton
ok deraadt


# 1.329 20-Jul-2017 kettenis

Start a list of isa(4) device names such that we can avoid printing them
as "not configured".

ok mikeb@, millert@, deraadt@


# 1.328 08-Apr-2017 deraadt

Sensors are run as callbacks inside tasks. During suspend a sensor
could be running inside a driver that will be force-detached, or due
to tsleep end up disrupting the softstate/hardstate contract. At
suspend time, quisce all these callbacks by waiting for completion.
This issue has never been observed for real, but may be implicated in
suspend/resume failures.
ok kettenis guenther mlarkin


# 1.327 08-Apr-2017 deraadt

Two prototypes for acpi_maptable; merge to one place


Revision tags: OPENBSD_6_1_BASE
# 1.326 27-Mar-2017 deraadt

Now that hibernate_alloc() only has clean success/failure, don't
need to call hibernate_free() to clean up a partial mess.
ok mlarkin kettenis


# 1.325 13-Mar-2017 deraadt

allow ramdisks to build; ok jcs


# 1.324 12-Mar-2017 jcs

try to attach acpisbs first and if successful, don't attach acpibat


# 1.323 02-Mar-2017 natano

Add a new sysctl machdep.lidaction. The sysctl works as follows:

machdep.lidaction=0 # do nothing
machdep.lidaction=1 # suspend
machdep.lidaction=2 # hibernate

lidsuspend is just an alias for lidaction, so if you change one, the
other one will have the same value. The plan is to remove
machdep.lidsuspend eventually when people have upgraded their
/ets/sysctl.conf.

discussed with deraadt, who came up with the new MIB name
no objections mlarkin
ok stsp halex jcs


# 1.322 28-Feb-2017 natano

Untangle abstract sleep modes from ACPI sleep states. This paves the way
for further work in the area.

idea and ok deraadt
ok mlarkin


# 1.321 22-Feb-2017 jcs

add acpisbs, an acpi smart battery subsystem driver reading data
over smbus

currently disabled because it conflicts with acpibat

ok deraadt, kettenis


# 1.320 14-Jan-2017 kettenis

Revert aml_rdpciaddr changes; breaks several machines that were working
before.


# 1.319 11-Jan-2017 jsg

Increase the size of the buffers used for _HID and _CID strings to be
able to fit "Hyper_V_Gen_Counter_V1".

ok mlarkin@ deraadt@


# 1.318 08-Jan-2017 kettenis

Make aml_rdpciaddr() work for for devices behind bridges. The code now
uses the mapping between AML nodes and PCI devices that we establish.
Because _INIT methods may end up calling aml_rdpciaddr(), make sure we
create that mapping early. Also handle devices that aren't actually present.
These devices are now included in the mapping and reads will return an
all-ones patterm whereas writes are a no-op..

ok mlarkin@


# 1.317 25-Oct-2016 pirofti

Add the resource index to aml_parse_resource() callback function.

This is needed by an upcoming acpiec commit that handles machines breaking
the current ACPI specifications.

Change suggested by and ok kettenis@, guenther@


# 1.316 18-Sep-2016 guenther

If the APIC table (MADT) isn't present then ci_acpi_proc_id won't be set
when acpicpu tries to attach, leading to a NULL dereference. The ACPI
processor object on the Dells where this was reported is bogus and useless
anyway so apply the same test when deciding whether to attach to processor
objects as acpicpu would when matching them to cpus.

Problem reported by Eivind Eide (xenofil (at) gmail.com) and
Johan Huldtgren (johan+openbsd-bugs (at) huldtgren.com).
Testing by Eivind Eide.
ok kettenis@ mlarkin@


# 1.315 03-Sep-2016 naddy

Write the system time back to the RTC every 30 minutes.
This fixes the problem that long-running machines which were not
shut down properly would reboot with a badly offset system time.

hints and ok kettenis@


# 1.314 31-Aug-2016 mlarkin

Don't put a device in the wake device list if _STA says it's not there.

Fixes at least one machine that woke up from sleep / poweroff immediately.

ok kettenis@, tested by kettenis and martijn@


# 1.313 28-Jul-2016 kettenis

Store the acpi processor ID/UID in struct cpu_info, and use it to attach
acpicpu(4) drivers to the right cpu(4).

ok mlarkin@, guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.312 10-Jun-2016 kettenis

The power management timer has been made optional in ACPI 5.0A, so don't
attempt to attach acpitimer(4) if the timer isn't present.

Pointed out by jcs@

ok jcs@


# 1.311 08-May-2016 kettenis

Make sure devices listed by a device's _DEP method are attached before
we attach the device itself. The _DEP methode, introduced in ACPI 5.0,
is a hint that the device in question depends on OpRegion support from
the devices listed by _DEP, so we have to order them the proper way.
To prevent us from attaching those devices again when we encounter them
later walking down the device tree, keep track of our attempts to
attach a device driver to them using aflag in the aml_node structure.

ok guenther@


# 1.310 25-Apr-2016 pirofti

We attach acpihpet(4) and acpiec(4) without using the HID, so do not
report it in the dmesg.

OK deraadt@, mlarkin@.


# 1.309 02-Apr-2016 kettenis

Reduce dmesg spam by knocking out some acpi hardware IDs that don't need a
driver.

ok mpi@


# 1.308 29-Mar-2016 kettenis

Check if a device is present (using _STA) in acpi_foundhid. This prevents
us from reporting devices as "not configured" that aren't actually present,
and allows us to remove duplicated code from several drivers.

ok deraadt@, ok jsg@


# 1.307 29-Mar-2016 kettenis

Implement a cleaner way to prevent acpivout(4) from attaching if we have
a platform-specific method of controlling the backlight.

ok millert@, mlarkin@, jung@


# 1.306 28-Mar-2016 kettenis

Adding each and every new acpi device driver to acpi_foundhid() is
gettinga bit of the burden. And it really isn't how our autoconf
framework is supposed to work for busses that can be enumerated. All
the drivers already check for a matching _HID in their attach
function. So we can just drop the checks in acpi_foundhid() and let
the drivers sort themselves out.

Print a "not configured" line for devices for which we don't attach adriver.
This may be a bit much as there are devices for which we will not have a
driver. This will be removed if it causes too much dmesg spam.

ok deraadt@, mlarkin@


Revision tags: OPENBSD_5_9_BASE
# 1.305 17-Jan-2016 jsg

Take acpi_foundhid() outside of #ifndef SMALL_KERNEL so the ramdisk
kernel can find and attach dwiic(4) and sdhc(4).


# 1.304 17-Jan-2016 jsg

remove an unused global variable


# 1.303 14-Jan-2016 kettenis

Attach dwiic(4) on the Intel Bay Trail i2c controllers as well.


# 1.302 12-Jan-2016 kettenis

Improve emulation of PM registers on Hardware-redcued ACPI by emulating
SPL_TYPx, SLP_EN and WAK_STS using the new SLEEP_STATUS_REG and
SLEEP_CONTROL_REG registers. Makes the transition into S5 (powerdown) work
on the ASUS X205TA.

ok deraadt@, mlarkin@


# 1.301 12-Jan-2016 jcs

Add dwiic, a driver for the Synopsys DesignWare i2c controller found
on the Samsung ATIV Book 9 laptop. This initial version only
supports ACPI config/attachment.

Add ihidev, a HID-over-i2c driver largely based on uhidev. dwiic
handles attaching ihidev devices found in ACPI.

Add ims, a HID-over-i2c mouse/trackpad driver to get basic cursor
and button functionality from HID-compliant i2c trackpads.

ok kettenis deraadt


# 1.300 11-Jan-2016 kettenis

Make sdhc(4) attach to hardware IDs "80860F14" and "PNP0FFF".


# 1.299 09-Jan-2016 kettenis

Initial support for Hardware-Reduced ACPI. This skips the legacy mode checks
by emulating the PM_CNT register such that SCI_EN is always enabled. It also
skip attaching acpitimer(4) since the relevant registers are no longer there.

ok deraadt@, mlarkin@


# 1.298 06-Jan-2016 kettenis

Follow the spec and check the SCI_EN bit to see if ACPI has already
been enabled and don't bother enabling it again if it is. This should fix
systems that don't implement legacy mode and therefore set ACPI_ENABLE and
ACPI_DISABLE to zero.

ok mlarkin@, yasuoka@


# 1.297 23-Nov-2015 reyk

Back out the CRITICAL change, turn it back from 10% to 25%. Some
people with old/broken batteries didn't like this change, and the
underlying heuristics are wrong.


# 1.296 22-Nov-2015 reyk

The "CRITICAL" APM battery level, that is exported/faked by the ACPI
stack, is currently set to 25%. With 25% battery left, a typical
modern laptop will have at least one hour runtime left. It is an
arbitrary value, and other APM power layers in the kernel set CRITICAL
to something like 5% or 10%, so set it to 10% for now.

OK mlarkin@


# 1.295 28-Sep-2015 deraadt

In low-level suspend routines, set cold=2. In tsleep(), use this to
spit out a ddb trace to console. This should allow us to find suspend
or resume routines which break the rules. It depends on the console
output function being non-sleeping.... but that's another codepath which
should try to be safe when cold is set.
ok kettenis


# 1.294 06-Sep-2015 deraadt

clarify malloc arguments (base + length...)


# 1.293 28-Aug-2015 deraadt

fairly simple sizes for free(); ok tedu


# 1.292 20-Aug-2015 kettenis

Respect the access size when reading or writing to pci config space.
This version also makes sure writes are properly aligned, preventing
the panics seen with the previous incarnation of this change.

Fixes battery status passthrough in vmware, brightness keys on my x220
and who knows what else.

ok mlarkin@, deraadt@


Revision tags: OPENBSD_5_8_BASE
# 1.291 04-Aug-2015 deraadt

If we are setting ACPI_PM1_BM_RLD do it only once, and remember to
repeat this action at resume time.
ok mlarkin guenther


# 1.290 29-Jul-2015 deraadt

Something subtle in previous revision is locking up suspend or booting
on some laptops. Mailing lists threads mention x220 and Asus machines.
Reverting for now.


# 1.289 21-Jul-2015 kettenis

iRespect the access size when reading or writing to pci config space.
Fixes battery status passthrough in vmware and who knows what else.

Tested by many.

ok mlarkin@, deraadt@


# 1.288 17-Jul-2015 mlarkin

fix comments


# 1.287 26-Mar-2015 jsg

Use the 0x10 extended capability to determine if the trackstick
buttons are wired to the trackpad and need to be re-routed to the
trackstick. Without this change the buttons on 2015 thinkpads
get picked up as extended buttons that show up as scroll up/down.

Remove the X1 Carbon 2015 (LEN0048) and X250 (LEN0046) from the
top button area/soft buttons quirks list. Also avoid using the
quirk list entirely if the capability bit is set.

Based on work from jcs@ with feedback from mpi@
ok mpi@ jcs@


# 1.286 20-Mar-2015 kettenis

The ACPI standard says that a PCI function number of FFFF can be used to refer
to all functions on a device. This makes no sense in the context of accessing
PCI config space. Nevertheless stsp@ has a machine with AML that tries to
do config space reads from a device where _ADR encodes function number FFFF.
This is almost certainly buggy AML, but since Linux (and presumably Windows)
boot just fine on this machine we need to work around this. Linux effectively
treats function FFFF as function 7, which isn't present on this machine.
Instead we simulate a read from a non-existent device by returning a bit
pattern of all ones. Writes to function FFFF will still panic.

ok mlarkin@


# 1.285 16-Mar-2015 deraadt

Reduce use of SMALL_KERNEL in acpi, bringing acpiec driver and more gpe
handling into RAMDISK. This is now possible because the install media
has ample room. The goal is to reduce special cases where we may be
skipping (unknown) important operations...
ok mlarkin kettenis


# 1.284 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.283 07-Feb-2015 deraadt

New framework that allows hibernate to pass in entropy from it's fresh
boot.
ok mlarkin


# 1.282 06-Feb-2015 mlarkin

Fix a hibernate crash on some machines due to unmapping a page that
may not have been mapped previously (in the failure to hibernate case).

Also ensure that the lowmem ptp is mapped in all cases (not just MP).

ok kettenis


# 1.281 17-Jan-2015 deraadt

resume_randomness() before spinning up other cpus...


# 1.280 15-Jan-2015 jsg

Define a new wscons mouse type for Synaptics clickpad devices that lack
physical buttons. This will be used if the acpi pnpid for the mouse
matches a list provided by Synaptics found in Linux.

Combined with the xenocara changes this will remove the need for an xorg
config file for the x240/t440/t540 generation of thinkpads.

ok matthieu@ shadchin@ kettenis@


# 1.279 11-Jan-2015 kettenis

Tolerate checksum errors in ACPI tables (but complain about them). Apparently
there are machines out there with broken BIOSen with such checksum errors in
thier RSDT tables. Windows runs fine on these machines, so it probably does
not check. Linux ignores checksum errors as well (and prints a warning).

ok deraadt@, krw@


# 1.278 18-Dec-2014 deraadt

suspend and resume the RNG (at what looks like the right spots)
ok reyk djm


# 1.277 09-Dec-2014 doug

Replace some malloc(n*size,...) calls with mallocarray().

ok tedu@ deraadt@


# 1.276 06-Dec-2014 mpi

If "machdep.lidsuspend" is not set, do not go back to sleep if the lid
is still closed when resuming. This allows docked laptop to be resumed
without opening them.

ok mlarkin@


# 1.275 23-Nov-2014 mlarkin

If a machine has no _LIDs, then none can be open. This diff fixes a problem
where machines without any _LIDs were going back to sleep after hibernate
due to the recent suspend-if-lid-closed-on-resume diff. Noticed on qemu,
which doesn't have any _LIDs.

discussed with deraadt


# 1.274 18-Nov-2014 krw

Nuke yet more obvious #include duplications.

ok miod@


# 1.273 08-Nov-2014 mlarkin

If resuming from sleep (zzz/ZZZ) and the lid is still closed, go back to
sleep. This prevents accidental lid flex or slight opening in a backpack
from waking the machine up and leaving it resumed (powered on).

ok deraadt@


# 1.272 26-Sep-2014 kettenis

Rework piglet and pig allocation. Currently the piglet gets allocated
deep down in the suspend path, where it is really hard to recover from
allocation failure. So allocate the piglet early on in the suspend path.
Also change the piglet and piglet allocation functions to use km_alloc(9)
instead of doing pmemrange magic. This removes a bunch of code which, in the
case of the piglet allocation, is broken since it results in a NULL pointer
dereference. Also switch the piglet allocation to not wait. If we can't
allocate 16MB of phys contig memory on a halfway modern machine we're almost
certainly under a lot of memory pressure and we're better off not trying to
hibernate anyway.

ok mlarkin@


# 1.271 25-Sep-2014 kettenis

Bring back rev 1.249. Now that mp_setperf() has been fixed, it should be
safe to restore perflevel when resuming. Should fix the issues people
see with the processor running at full speed heating up the machine while
the apm command reports that the CPUs are running at a low speed.

ok dcoppa@, deraadt@


# 1.270 23-Sep-2014 kettenis

Release the acpi lock around calls to wsdisplay_suspend() and
wsdisplay_resume(). The X server may adjust the display brightness just
before/after the VT switch when suspending/resuming. In fact, it may
already be blocked adjusting the display brightness for some reason
unrelated to the suspend request.

The VT switch request will eventually time out, but the delay is annoying
and the failed VT switch will lead to problems after resume.

Needed for a future update of the xf86-video-intel driver.

ok mlarkin@


# 1.269 19-Sep-2014 kettenis

Use config_suspend_all(9).


# 1.268 08-Sep-2014 kettenis

Clear the PME Status bit on PCI devices in response to Device Wake events.
Fixes the interrupt storm on the 2009 Mac mini with WOL enabled on its
nfe(4) interface.

ok mlarkin@


Revision tags: OPENBSD_5_6_BASE
# 1.267 20-Jul-2014 mlarkin

Support hibernating to softraid crypto volumes.

much help and ok from deraadt@


# 1.266 16-Jul-2014 mlarkin

Reenable hibernate RLE support and flush+zero all memory after unpack.


# 1.265 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.264 12-Jul-2014 kettenis

Add a function to drop all clean pages on the page daemon queues and call
it when we hibernate.

ok mlarkin@, miod@, deraadt@


# 1.263 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.262 11-Jul-2014 mlarkin

Flush the buffercache to 16MB on hibernate and restore its previous max
size (kern.bufcachepercent) on resume, for better hibernate performance.

ok beck@


# 1.261 10-Jul-2014 blambert

remove unneeded header

ok mlarkin@


# 1.260 01-Jul-2014 kettenis

Adding an additonal delay after each access to SystemMemory or SystemIO
during autoconf makes no sense.

ok deraadt@


# 1.259 23-Jun-2014 kettenis

SystemMemory is used to access memory mapped registers on some machines, so we
must use the bus_space(9) API to access it instead of memcpy(9). Also make
sure we properly align access to fields that need more than byte alignment.

ok guenther@


# 1.258 21-May-2014 mlarkin

Fix invalid / missing format specifiers in several dnprintf statements
(used only when ACPI_DEBUG is enabled)

From a diff on tech@ by Fabian Raetz, with a minor change by me, tested
on i386 and amd64 with ACPI_DEBUG enabled and disabled.


# 1.257 25-Apr-2014 mlarkin

Defer the free of the hibernate structures until later, when it is safe to
do so.

Freeing the hibernate structures too early leads to inadvertent calls into
splx when we aren't ready for them yet. Found when trapping splx calls in
the resume path.

discussed with deraadt@ at m2k14


# 1.256 13-Apr-2014 mlarkin

Fix a badly worded debug message that implied the entire machine was going
to sleep, instead of just the acpi thread going to sleep.


# 1.255 31-Mar-2014 mpi

Change the order of operations during a suspend/resume cycle and
call bufq_quiesce() after executing the DVACT_QUIESCE handlers.

This should be safe since no disk nor controller drivers have such
handler but it will allow us to detach sd(4) devices attached to a
USB bus.

Another benefit pointed out by kettenis@ is that drivers that need
to read a firmware from the disk should be able to do it at resume
time in a DVACT_WAKEUP handler.

ok kettenis@, deraadt@


# 1.254 13-Mar-2014 dlg

get rid of the assumption that the head of the alldevs list is the
"mainbus" device. this breaks when mpath is enabled because it
attaches before mainbus and therefore takes the head position.

have autoconf provide device_mainbus() which looks up mainbus_cd,
and use that instead.

discussed with deraadt who just wants mpath stuff to move forward
despite there being many ways to shine this particular turd.


Revision tags: OPENBSD_5_5_BASE
# 1.253 21-Feb-2014 deraadt

Do the lid control further on in the resume sequence, since this is
AML, and will do WAITOK operations. While cold, that is a bad idea.
We seem to be safe with respect to the spec, I do not see any event
debounce issues here.
ok kettenis, tested by many


# 1.252 20-Jan-2014 claudio

Instead of checking sc->sc_revision <= 1 check that the
fadt->hdr_revision <= 1. This is a better check since it checks
the version of the proper descriptor. This allows the alix APU
to reboot since those boards have currently a wrong sc->sc_revision.
Committing this now so that it can be tested easier.
With and OK jsg@ also fine by derradt@


# 1.251 24-Dec-2013 kettenis

If the FADT has its SMI_CMD set to zero, assume we're only ACPI-only hardware
and don't need to disable SMI ownership of the ACPI hardware registers.

ok mlarkin@


# 1.250 23-Dec-2013 kettenis

Revert previous commit. It makes resume hang on some MP systems running
GENERIC.MP.


# 1.249 16-Dec-2013 kettenis

Restore hw.setperf upon resume like we do for apm(4) on i386. Seems we need
to do this fairly late in the resume to avoid hangs.

ok deraadt@, mlarkin@


# 1.248 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


# 1.247 06-Nov-2013 mpi

Add support for Power Resources for Dx states and the necessary hook
for PCI devices. This hook should be called twice, before and after
changing the power state of a PCI device.

Before setting the device to the new state, the ACPI layer will notify
every power resources linked to the device for that state and make sure
they are turned "_ON". After changing the state of the device, it will
decrement the reference of every power resources linked to that device
for the old state and turn them "_OFF" if they are no longer referenced.

This fixes the no-USB after resume problem seen on various ThinkPad,
problem initialy diagnosed with Alexander Polakov.

ok kettenis@, deraadt@


Revision tags: OPENBSD_5_4_BASE
# 1.246 01-Jun-2013 mlarkin

Stop printing ACPI wakeup devices in dmesg after the 16th wakeup device.

Helps with VMware VMs that report hundreds of wakeup devices.

ok reyk@, deraadt@


# 1.245 31-May-2013 bluhm

Fix the build for a kernel without wd and pciide in its config.
OK deraadt@ tedu@


# 1.244 30-May-2013 deraadt

Enforce ca_activate tree-walks over the entire heirarchy for all events,
cleaning up some shutdown-hook related code on the way.
(A few drivers related to sparc are still skipped at kettenis' request)
ok kettenis mlarkin, tested by many others too


# 1.243 18-Apr-2013 deraadt

pin the acpi thread to the BSP, because AML and SMI cannot be trusted. I
was certain we were doing this already, but it turns out no...


Revision tags: OPENBSD_5_3_BASE
# 1.242 24-Dec-2012 guenther

In acpiioctl(), Return though the splx() path if suser() returns an error

ok deraadt@


# 1.241 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


# 1.240 04-Oct-2012 ehrhardt

Use information provided by ACPI to attach secondary PCI host bridges.

ok kettenis@


# 1.239 07-Sep-2012 kettenis

Add a new function called acpi_pci_min_powerstate() that returns the lowest
power state for a PCI device for the power state (currently S3 or S4) we're
transitioning into. This function is probably too simplistic; there is a lot
of (silly) complexity in the ACPI spec.

ok deraadt@


Revision tags: OPENBSD_5_2_BASE
# 1.238 13-Jul-2012 pirofti

Disable acpivideo(4) on ASUS laptops.

There are some models that panic whenever an event is received.
Some cope, some don't. In order to get this right acpiec(4) requires
major rework. Until then let machines boot.

Discussed with deraadt@.


# 1.237 11-Jul-2012 mlarkin

for now, restrict hibernate to root in the acpi ioctl kernel interface.
normal rules for controlling this interface through apmd still apply.

ok deraadt@


# 1.236 09-Jul-2012 deraadt

tiny little oops in untested hibernate block


# 1.235 09-Jul-2012 deraadt

clean up ioctl() case statements


# 1.234 09-Jul-2012 deraadt

acpi_disable_onegpe() is not used; ok pirofti


# 1.233 24-May-2012 kettenis

If _MIN > _MAX consider a WordBus resource to be invalid. Fixes a panic on
the IBM x3550-M4.

ok mikeb@


# 1.232 29-Mar-2012 mlarkin

Missing #ifdef in previous diff


# 1.231 29-Mar-2012 mlarkin

Prevent hibernation attempts on machines with unsupported disk controllers.
The same check is also performed later in the hibernate sequence, but by
then it's too late to cleanly unwind (presently).

ok deraadt


# 1.230 26-Mar-2012 deraadt

simplify conditionals in acpi_sleep_state()
ok mlarkin


# 1.229 26-Mar-2012 deraadt

hook in the hibernate request code; half of this diff is from mlarkin
ok mlarkin


Revision tags: OPENBSD_5_1_BASE
# 1.228 20-Sep-2011 deraadt

Very early in suspend while the clock is still ticking realtime,
save the clock back to the rtc. The zaurus already did this.
ok phessler miod kettenis


Revision tags: OPENBSD_5_0_BASE
# 1.227 02-Jul-2011 nicm

kqueue attach functions should return an errno or 0, not a plain 1. Fix
the obvious cases to return EINVAL and ENXIO.

ok tedu deraadt


# 1.226 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.225 16-Jun-2011 pirofti

Import acpitoshiba driver.

This is originally written by Hiroyuki Aizu, ported and integrated
in our tree by Javier Vazquez <javama4 AT gmail> with tweaks and
suggestions by me.

Import prodded by deraadt@ and the lack of testers on tech@.


# 1.224 27-Apr-2011 jcs

attach acpithinkpad to newer lenovo models like the x120e

ok deraadt@


# 1.223 22-Apr-2011 jordan

Fix uninitialzied variables and formatting strings (-Wxxx errors)


Revision tags: OPENBSD_4_9_BASE
# 1.222 02-Jan-2011 jordan

Re-add code for ACPI taskqueue, eliminates need for state vars in acpi softc
Serialize processing ACPI tasks for sleep/powerdown/GPE event
ok deraadt@


# 1.221 31-Oct-2010 guenther

Revert last commit: it breaks resume on Thinkpads
"then please back it out" deraadt@


# 1.220 26-Oct-2010 jordan

Add task queue for ACPI gpe and notify handlers
This fixes eject on Dell Latitude dock and an issue on the Dell Mini
battery update.
ok deraadt


# 1.219 07-Oct-2010 jordan

Create separate functions for enabling wake and runtime gpes.
Remove old suspend/resume gpewalk function
ok deraadt, mlarkin


# 1.218 05-Oct-2010 jordan

split acpi_enable_onegpe to separate enable/disable functions
ok deraadt


# 1.217 05-Oct-2010 jordan

Added acpi_disable_allgpes function to clear GPE status
ok deraadt


# 1.216 05-Oct-2010 deraadt

clean up acpi_softc use more


# 1.215 05-Oct-2010 deraadt

delete a bit of #if 0 code


# 1.214 05-Oct-2010 deraadt

hide use of our global acpi_softc pointer a bit better


# 1.213 29-Sep-2010 deraadt

grab the acpi thread lock very early in acpi thread startup; not that it
is likely that anyone will race us... but why be careless.
ok kettenis


# 1.212 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.211 29-Aug-2010 jasper

- add a check for FWRITE here too.

ok miod@


# 1.210 27-Aug-2010 kettenis

Make PCI Power Management optional, and only enable it when acpi(4) attaches.

ok deraadt@


Revision tags: OPENBSD_4_8_BASE
# 1.209 08-Aug-2010 kettenis

Close race between the acpi thread and wscons ioctls running in process
context. We might rip this out post-release and replace it with a solution
that serializes things in a more obvious way.

ok deraadt@, miod@ and "those crazy texans who ok anything"


# 1.208 08-Aug-2010 deraadt

Report the acpi sleep states when on a RAMDISK; ok krw


# 1.207 07-Aug-2010 kettenis

Enforce acpi register access rescrictions.

ok mlarkin@, deraadt@, tested by marco@


# 1.206 07-Aug-2010 deraadt

upon resume, notify all the acpiac and acpibat drivers to update
their status. some laptops do not do this automatically.
ok kettenis canacar


# 1.205 06-Aug-2010 marco

Always call _PSW on _LID devices that support it to enable lid open to
resume the machine. Conversely disable it when the machine wakes up.

Tested by several ok deraadt


# 1.204 06-Aug-2010 deraadt

As part of a revamp of the PM1/GPE code, I write this diff to look for
unmanaged EN & STS bits in the PM1 register at interrupt time and report
them. As a side effect this splits the STS acknowledgement into two writes
(for power, and sleep) instead of one. The printf that is added (to spot
unmanaged STS bits) has not yet been triggered as far as we know.

Before the "write to PM1 registers at the right offset" diff went in, this
was not neccessary. But newer thinkpads do not have a working soft power
button without this diff.

We have no idea why.
ok mlarkin kettenis


# 1.203 05-Aug-2010 deraadt

PM1 power button events were generating 'fake events' to acpibtn PWRB,
but if there is no such device these were being tossed. Instead, arrange
for all power button handlers to flag a softc variable which is processed
by the thread; so that one place will know to request a powerdown (psignal
to init)
ok marco mlarkin


# 1.202 05-Aug-2010 deraadt

rename a variable which is annoyingly poorly named


# 1.201 05-Aug-2010 mlarkin

Write to the correct offset for various ACPI pmreg registers. Writing to
the correct register does properly clear the wake status bits and fixed
event bits, which allows some machines to have more than 1 suspend cycle.

bug spotted by deraadt@

tested by deraadt@ and myself on a half-dozen machines and others on
hackers@

ok deraadt@, marco@


# 1.200 05-Aug-2010 deraadt

Fix the power button. The pm1 register is 16 bits in size.
My mistake, a while back.
tested by krw


# 1.199 04-Aug-2010 kettenis

Print supported sleep states.

ok marco@, phessler@, deraadt@


# 1.198 03-Aug-2010 mlarkin

Use the proper flag for re-enabling certain hardware events (power btn,
sleep btn). The (incorrect) flag prevoiously used coincidentally had the
same value, so this shouldn't cause any different behavior than before.

tested on a variety of machines (i386, amd64, sp, mp) - no behavioral
change seen

ok deraadt@


# 1.197 28-Jul-2010 marco

Make all gasio failures look and behave the same.


# 1.196 28-Jul-2010 mlarkin

Delay a little longer on suspend before giving up. Fixes machines that
sometimes don't suspend because the sleep transition takes too long
(symptoms like failing to sleep but the backlight stays on).

ok deraadt@


# 1.195 27-Jul-2010 deraadt

remove XXX; EC can now be attached early when needed; ok jordan


# 1.194 27-Jul-2010 deraadt

more brackets feels good


# 1.193 27-Jul-2010 deraadt

XXX annotate a block in acpi_enter_sleep_state() that is pretty worrying


# 1.192 27-Jul-2010 deraadt

horrific indentation that nearly made me puke


# 1.191 27-Jul-2010 mlarkin

Set the sleeping indicator light on machines that support it, in case SMI
doesn't do it for us. Shows the 'moon' LED on older Thinkpads when
sleeping.

Tested on T43p, W500, T510 by me and on X61s by deraadt@.

ok deraadt@


# 1.190 27-Jul-2010 jordan

Early initialization of acpiec if ECDT table exists
Fixes hang when booting thinkpads while docked
ok deraadt


# 1.189 26-Jul-2010 deraadt

remove merge error which was commited


# 1.188 26-Jul-2010 pirofti

Add support for Sony ACPI hotkeys via a new driver: acpisony(4).

Currently it only works for the suspend button and tries to do right for
the brightness events, but I haven't found a Sony laptop that like to do
right so far.

In the future I want to make the brightness keys work on all Sony's and
also add support for the zoom hotkeys and whatever other funky keys I can
find on those things.

Okay deraadt@.


# 1.187 22-Jul-2010 deraadt

pretty large cleanup of SMALL_KERNEL


# 1.186 21-Jul-2010 deraadt

in the acpi GPE handler, late ack level interrupts not edge interrupts.
Duh.


# 1.185 21-Jul-2010 deraadt

Merge the edge and level gpe handlers into one (now possible since the
gpe structure tells us whether the gpe is edge or level)


# 1.184 21-Jul-2010 deraadt

delete #if 0 code we are not going to use; ok marco


# 1.183 20-Jul-2010 deraadt

make the mask code for acpi_enable_onegpe() match other similar code


# 1.182 20-Jul-2010 deraadt

Handle the acpi interrupt controller with proper edge/level handling,
wrapped in spltty to avoid racing against the interrupt controller.
Repair the gpe bit masking code while there, and do operations in the
right order
ok kettenis mlarkin, help from jordan at figuring out the order of
operations.


# 1.181 20-Jul-2010 matthew

When we're preparing to go to sleep, only call bufq_restart() if other
preparations failed.

ok kettenis@, thib@, deraadt@


# 1.180 19-Jul-2010 deraadt

permit compilation with ACPI_DEBUG


# 1.179 19-Jul-2010 deraadt

there is no need for a seperate dsdt_softc pointer; dsdt does not have a
softc but is just a part of the single acpi driver.
ok jordan


# 1.178 19-Jul-2010 deraadt

acpi_delay() is no longer needed; ok jordan


# 1.177 18-Jul-2010 mlarkin

Clear event status on resume, to avoid going back to sleep/reboot/shutdown
immediately after resume. Tested by a bunch of people on hackers@ on
various hardware. Fixes at least Toshiba NB200 and Gateway LT3103u.

ok marco


# 1.176 13-Jul-2010 deraadt

rename some thread related interfaces so they are logical


# 1.175 10-Jul-2010 jordan

Verify _ADR bdf is correct (some systems return 0xffff)
Works on Dell x9xx and Studio 1555


# 1.174 09-Jul-2010 jordan

Clear the noise


# 1.173 08-Jul-2010 jordan

Add mapping for ACPI device to PCI bdf (match autoconf tree)
Simplify resource parsing function to use buffer argument
Convert namespace linked lists to use queue macros
ok marco@, deraadt@


# 1.172 06-Jul-2010 deraadt

Use spltty() to lock downcalls from apm against the information being
modified by the acpi thread.
ok kettenis


# 1.171 06-Jul-2010 deraadt

Don't use a workq to do the suspend, because it races aginst the acpi
thread. Instead, just tell the acpi thread to do the suspend for us.
This makes apmd & zzz work correctly. While here, have acpithinkpad
attempt to post the event to apm before prompting the suspend itself.
ok kettenis marco mlarkin


# 1.170 05-Jul-2010 mlarkin

Reset register SCI_EN on resume. The ACPI spec clearly states we don't have
to do this, but many machines don't, or forget to, which leads to all sorts
of problems on resume (broken battery stats, overheating, failure to resume
more than once, etc).

tested by many, ok marco@, deraadt@


# 1.169 01-Jul-2010 thib

do bufq quiesce again, since vnd/the bufq_done call in
biodone has been fixed.


# 1.168 01-Jul-2010 jordan

Backout recent AML changes, breaks on R210 and others


# 1.167 01-Jul-2010 jordan

Simplified aml_parse_resource call, cleanup code
ok marco


# 1.166 01-Jul-2010 jordan

Fix subordinate bus # for multi-root PCI buses
ok marco, kettenis


# 1.165 30-Jun-2010 thib

Disable/partially backout the bufq quiesce changes as this
is causing havoc with vnds and release must be buildable.


# 1.164 29-Jun-2010 deraadt

Fix ramdisks (new code was placed inside a SMALL_KERNEL chunk)


# 1.163 29-Jun-2010 jordan

Add support for mapping ACPI to PCI devices
ok kettenis, deraadt


# 1.162 29-Jun-2010 kettenis

Call bufq_quiesce() on suspend to stop all I/O and bufq_restart on resume.
Makes suspend/resume work much better while doing disk I/O.

ok mlarkin@


# 1.161 29-Jun-2010 tedu

60 is a more reasonable guess for number of minutes in an hour than 100.
spotted by TeXitoi. ok deraadt


# 1.160 27-Jun-2010 jordan

Add _acpi_matchhids routine, precursor to pci device match


# 1.159 27-Jun-2010 mlarkin

Clear ACPI fixed event status on resume (power buttons/etc) so that some
machines don't immediately resume after suspending to S3.

Tested by kettenis@ and I on 5 or 6 machines, deraadt@ "yeah, looks good"


# 1.158 27-Jun-2010 jordan

Cleaned up acpi_maptable, adds to table list directly


# 1.157 07-Apr-2010 deraadt

If we have done a wsdisplay_suspend(), and ACPI decides it does not want to
suspend, we must roll back by calling wsdisplay_resume() or the virtual
console switching will be locked forever
ok oga


# 1.156 07-Apr-2010 kettenis

Make suspend/resume work on MP machines (running an MP kernel). Joint work
from mlarkin@ and me, with some amd64 fixes thrown in by deraadt@

ok marco@, deraadt@, pirofti@, mlarkin@


# 1.155 31-Mar-2010 kettenis

Bring /dev/apm support in line with apm(4). Make the suspend button send
a suspend request event to apmd(8) instead of suspending immediately. Also
keep track of whether /dev/apm and /dev/apmctl are currently open, such that
we can still suspend immediately if apmd(8) isn't running.

ok deraadt@, marco@, pirofti@, jsing@, oga@


# 1.154 30-Mar-2010 oga

Prevent the apmd/x races for good.

When we hit suspend time, go through all wsdisplays on the system. if
they are in mode MAPPED, but not MODE_DUMBFB then if possible do a full
vt switch to a !mapped vt, and prevent switching back until resume time.
This has to be called from MD code because this involves userland
running so that X can run the vt switch signal handler. This way, any
case where we are using the "poke registers from userland" model, we
will not be on the hardware when we go down, so the kernel can actually
handle thing properly.

Tested on several acpi laptops (by kettenis@ and ian@), x40 (me and
beck@ at LEAST) and zaurus (me). Maybe others, but if so I forgot who at
this time..

Idea from deraadt somewhere over the Faroe Islands (I thought of a
similar thing myself a while ago). Much prompting from him. Ok and
comments miod@


# 1.153 25-Mar-2010 oga

Fix & vs. && in the kqfilter code.

Based on a comparison with the apm code.

ok deraadt@, kettenis@


Revision tags: OPENBSD_4_7_BASE
# 1.152 26-Nov-2009 mlarkin

Visual (and on some machines, audio) feedback while the machine is
resuming via calls to aml _SST method. This also makes the "moon" LED on
thinkpads to blink during resume and cease when resume is completed.

ok pirofti@


# 1.151 26-Nov-2009 deraadt

Toggle into "cold" when we we disable interrupts, because tsleeping
in the AML interpreter and context switching into userland processes
tends to sometimes pop the ACPI trampoline page out of our address
space. Makes lots more machines work.
ok kettenis mlarkin


# 1.150 24-Nov-2009 jsg

in the resume path evaluate _WAK before dealing with device drivers
like the spec says, not after.

ok mlarkin@


# 1.149 24-Nov-2009 deraadt

abort suspends on SMP systems until that gets written; ok mlarkin


# 1.148 23-Nov-2009 mlarkin

pci should return the result of it's children's suspend/resume calls back
to its parent.

handle suspend failure case and unwind if devices have an activate function
that returns failure.

ok deraadt@, kettenis@


# 1.147 23-Nov-2009 mlarkin

acpi* devices need to call their children on suspend/resume.
ok deraadt@


# 1.146 23-Nov-2009 pirofti

Remove ACPI_SLEEP_ENABLED checks.

This enables by default the suspend/resume paths in the kernel.

Okay deraadt@.


# 1.145 23-Nov-2009 deraadt

knf and spacing that are super annoying


# 1.144 23-Nov-2009 mlarkin

Hook up Theo's new children suspend/resume function, as well as more ACPI
suspend/resume glue.

ok deraadt@


# 1.143 26-Oct-2009 deraadt

Do not do strncmp comparisons on dv_xname because longer device names which
look similar could arrive in the future. Instead, compare directly against
dv_cfdata->cf_driver->cd_name
Issue originally spotted by miod


# 1.142 26-Oct-2009 jordan

Verify checksum+address when loading ACPI tables.
Some systems had invalid entries in RSDT/XSDT.
ok marco@


# 1.141 23-Jul-2009 cnst

New aibs(4) driver for ASUSTeK AI Booster (ACPI ATK0110) hardware monitoring,
with sensor state support through limits provided by the ACPI.

Tested on several ASUS motherboards kindly networked by Sam Fourman Jr.

ok deraadt marco jordan


Revision tags: OPENBSD_4_6_BASE
# 1.140 03-Jun-2009 pirofti

Import acpi power resource driver. This is needed for suspend/resume logic.

Okay jordan@ and marco@.


# 1.139 03-Jun-2009 pirofti

Add acpivideo support. This adds brightness support for all laptops
except thinkpads, they will use the acpithinkpad driver. The driver is
also hooked into wsconsole. So brightness can be adjusted via:

$ wsconsctl display.brightness=<percentage>

This is very helpfull on some laptops that have a nasty bios and get two
steps instead of one when pressing the brightness button.

Tested on various dell, fujitsu, acer, samsung and other laptops.

Okay marco@, miod@. Suggestions from kettenis@.
Lots of reviews and help from miod@, thanks!


# 1.138 03-Jun-2009 jordan

Added ejectable bay for IDE cdroms
ok marco@


# 1.137 30-Apr-2009 marco

Oga changed apm ioctls and forgot to grep. Temporary work around by hitting
the sleep button when we get any request. This interface will chnage at
some point.
ok oga


# 1.136 19-Apr-2009 krw

Call acpi_sleep_walk() when powering down just like when entering
any other sleep state. This tells acpi wake devices to not wake up
the box until manual powering up resets their status. Makes my Dell
GX520 stay powered off in response to 'halt -p'.

In snaps for a while.

ok marco@ (several times) deraadt@


# 1.135 17-Apr-2009 pirofti

Add device ranking support for future sleep/wake developments.

okay marco@


# 1.134 11-Mar-2009 jordan

Cleanup.. use aml_evalinteger instead of aml_evalname
ok marco@


# 1.133 10-Mar-2009 jordan

Fix cut/paste error.. call _BFS not _PTS
ok marco@


Revision tags: OPENBSD_4_5_BASE
# 1.132 21-Feb-2009 marco

overzealous cut/paste; found by Vladimir Kirillov <proger@uaoug.org.ua>


# 1.131 20-Feb-2009 marco

Oops didn't mean for Debugger() to remain there.


# 1.130 19-Feb-2009 marco

suspend/resume bits so that we can develop this in tree. This is disabled.
code from mlarkin and me
help from art,toby,jordan and several others
ok jordan, go for it deraadt


# 1.129 10-Feb-2009 jordan

Fix null pointer for shutdown if acpi not enabled
ok marco@


# 1.128 04-Feb-2009 kettenis

Some systems that claim to be ACPI 2.0 apparently don't provide an XSDT
address. Make the code fall back on the RSDT on those systems.

From Joerg Niendorf <p3y80@internode.on.net>. Fixes PR 5986.


# 1.127 17-Jan-2009 krw

A couple of stray malloc/memset -> malloc(M_ZERO), a bit of knf and
lose a couple of unnecessary casts. No functional change.

Feedback from kettenis@

ok kettenis@ marco@


# 1.126 06-Nov-2008 marco

Collapse all sanity checks of hids into one function.

Originaly from jordan


Revision tags: OPENBSD_4_4_BASE
# 1.125 02-Jul-2008 fgsch

add acpivideo. for now almost an empty skel so others can work on
suspend/resume. eventually it will also manage output switching and
brightness where supported. prodded by marco@


# 1.124 11-Jun-2008 marco

Fix $OpenBSD$ for once and for all


# 1.123 08-Jun-2008 deraadt

aaa_dev was being set from an uninitialized variable; ok toby


# 1.122 07-Jun-2008 marco

Kill ACPIVERBOSE. It was never used and is worthless to boot.

comments jmc
ok toby


# 1.121 06-Jun-2008 marco

Shrink acpi some more for floppies and while here re-enable acpi on the
floppies. Fixes crazy sony vaio kettenis has.

tested by deraadt
ok deraadt jordan kettenis


# 1.120 01-Jun-2008 marco

Fix \n nit in SMALL_KERNEL.


# 1.119 01-Jun-2008 marco

Re-enable new parser. Lots of testing by various folks. Fits all media
now too.

ok kettenis deraadt


# 1.118 16-May-2008 dlg

back out jordans new parser.

it didnt have enough oks from the right people, it did break machines
that were previously working, and it bloated the RAMDISKS and install
media beyond the constraints of the media.

requested by deraadt@


# 1.117 14-May-2008 jordan

Adding new ACPI Parser code
Old guts of code still exists, needs to be torn out next
ok marco@
ok brad@


# 1.116 27-Apr-2008 jcs

add acpithinkpad(4), an acpi support driver for ibm/lenovo
thinkpads.

provides functionality that the bios did on older, non-acpi
thinkpads like respond to screen brightness keys and toggle the
bluetooth device on and off.

ok deraadt@ and marco@


# 1.115 24-Apr-2008 jsing

Introduce acpiasus(4), a driver for the ACPI based hotkeys found in many
ASUS laptops (including the ASUS EeePC) - largely based on NetBSD's
asus(4) driver. On the ASUS EeePC this allows us to enable/disable
wireless, change screen brightness and use the volume keys.

ok jsg@, weingart@


# 1.114 12-Apr-2008 kettenis

Get rid of acpi_s5 global variables; simply send SIGUSR2 instead of SUGUSR1
to tell init(8) to power down the machine.

ok krw@


Revision tags: OPENBSD_4_3_BASE
# 1.113 21-Jan-2008 jsg

Check against RESET_REG_SUP in FADT before doing the acpi reset dance
after all, not doing so appears to break more machines than it fixes.

ok marco@


# 1.112 05-Jan-2008 kettenis

Never attach more acpicpu(4) devices than cpu(4) devices. Always attach
the first acpicpu(4) device on non-MULTIPROCESSOR kernels, regardless of its
ACPI processor ID.

ok mikeb@


# 1.111 05-Dec-2007 deraadt

Hang acpi(4) below bios(4) instead of mainbus(4). This lets us move acpi(4)
before pcibios(4), and then.... if acpi(4) attaches, skip pcibios(4) since
it messes the machines up. Fixes claudio's HP dl320 G5, and almost assuredly
others.
ok kettenis


# 1.110 05-Dec-2007 deraadt

repair acpiopen/acpiclose and friends to handle the /dev/apmctl interface,
so that apmd(8) can actually work correctly against acpi(4), and thus,
even do the fancy -C/-A stuff; also tested by jmc


# 1.109 02-Dec-2007 fgsch

fix the trailing space for real now.


# 1.108 02-Dec-2007 fgsch

avoid trailing spaces on output; marco@ ok.


# 1.107 27-Nov-2007 kettenis

Fix off-by-one that made us attach acpicpu0 and acpicpu1 on GENERIC kernels.

ok marco@


# 1.106 25-Nov-2007 jsg

When available and ACPI is enabled use the ACPI reset method
to reboot. This is required for some quirky sis machines
and likely others to reboot.

Tested by many. 'commit early commit often' deraadt@


# 1.105 19-Nov-2007 deraadt

sigh, amd64 still uses that stupid acpi_enabled variable...


# 1.104 19-Nov-2007 deraadt

softc's come initialized to 0, you know...


# 1.103 19-Nov-2007 deraadt

store revision; from jsg


# 1.102 19-Nov-2007 kettenis

Initialize sc_facs earlier.

ok deraadt@, beck@, canacar@


# 1.101 16-Nov-2007 deraadt

fix the bus_space #define nightmare, so that amd64 and i386 are much more
uniform. as a result shared code like acpi needs less #ifdef's
ok marco kettenis


# 1.100 15-Nov-2007 deraadt

remove the #ifdef ACPI_ENABLE. the EC codepath is either safe enough (and
will be improved), or it is unsafe because AML assumes it must be there
ok almost everyone


# 1.99 15-Nov-2007 mikeb

Make acpicpu(4) attach to the existing CPUs only.

Problem reported by form@. Fix idea by kettenis@.
Lots of help from deraadt@. Tested by deraadt@, form@ and me.

ok deraadt marco


# 1.98 14-Nov-2007 deraadt

spacing


# 1.97 12-Nov-2007 deraadt

correct : vs , in attach-time messages


# 1.96 06-Nov-2007 deraadt

all ACPI_ENABLE kernels need acpi_interrupt()


# 1.95 06-Nov-2007 deraadt

move acpi_read_pmreg() and acpi_write_pmreg() out of SMALL_KERNEL so
that acpi ramdisks can compile (obvious solution)


# 1.94 05-Nov-2007 canacar

Establish the acpi interrupt earlier. Fixes problems with some
GENERIC.MP kernels not getting acpi interrupts. This is a
workaround until the real issue with late establishment is fixed.
ok weingart@, beck@, gwk@


# 1.93 03-Nov-2007 jordan

Added support for displaying wakeup devices
ok beck@,weingart@,gwk@


# 1.92 11-Oct-2007 kettenis

When walking the tree yo find _PRT methods, check whether the device is
actually enabled and functioning. Similar to what we do for _INI.

ok weingart@, beck@


# 1.91 08-Oct-2007 krw

More simple memset(,0,) -> M_ZERO changes. In this batch move to
size(*p) as the first malloc() parameter where p is declared locally
and thus easy to check. Add M_ZERO to gpe_table allocation in acpi.c
even though there is no obvious bzero or memset nearby.


# 1.90 13-Sep-2007 mk

Remove unnecessary memset() (see aml_evalnode(9)) and a bit of KNF.

ok toby


# 1.89 13-Sep-2007 weingart

Implement ACPI 6.5.1 spec tree walk for _STA and _INI.
Fixes mk's laptop. No regressions so far (thank you to the
testers).

ok gwk@, mk@, marco@


Revision tags: OPENBSD_4_2_BASE
# 1.88 17-Apr-2007 mk

Now that acpidock no longer hangs machines during boot (at least
according to the reports I've been getting) it should be safe to enable
it again, so remove the #if 0 stuff around it.

ok marco


# 1.87 11-Apr-2007 jordan

Added changes for C-State
ok marco@


# 1.86 26-Mar-2007 marco

Calculate minutes left more accurately.

From giovanni <qgiovanni@gmail.com>


Revision tags: OPENBSD_4_1_BASE
# 1.85 03-Mar-2007 mk

Stop looking for acpidock until we fix that stuff that breaks all pre-60
series ThinkPads with acpidock(4) enabled.


# 1.84 22-Feb-2007 marco

We were getting no interrupts in .MP. There are 2 fixes; one in
acpi_machdep.c and move the interrupt establishment down after ioapic
has been initialized.

ok jordan


# 1.83 21-Feb-2007 kettenis

Make an acpi-enabled GENERIC.MP work on systems that only support PIC mode.

ok marco@


# 1.82 19-Feb-2007 jordan

Added acpi_map_address function for mapping GAS/IO registers
ok marco@


# 1.81 18-Feb-2007 jordan

Added dynamic perfect hash for opcodes
ok marco@


# 1.80 18-Feb-2007 marco

Shave off another K pre compression.
Shuffle SMALL_KERNEL functions around so that we only have a single #ifndef


# 1.79 17-Feb-2007 marco

Add SMALL_KERNEL throught acpi to make it fit on boot media.
ok deraadt


# 1.78 13-Feb-2007 marco

Add smarter _INI code. This helps with some dissapearing devices.

ok kettenis jordan


# 1.77 31-Jan-2007 gwk

Introduce acpi_hasprocfvs thats "ACPI Has Processor Frequency and Voltage
Scalling" a global flag set by acicpu attach to indicate that ACPI is
handling fvs through the _PSS objects. This will fix disappearing
EST/powernow when booting kernels with acpi enabled, as acpicpu isnt
compiled into GENERIC yet. Also in cases where acpicpu is built in and
legacy EST/powernow work but acpi is missing the _PSS object, the legacy
routines will be used.

tested by dim@, and Nick Nauwelaerts <nick AT nauwelaerts.net>
ok marco@, dim@


# 1.76 22-Jan-2007 mk

Actually look for docking stations by looking for _DCK nodes.

ok marco


# 1.75 02-Jan-2007 marco

On some systems EC must be attached as the first device. Move EC from
generic _HID devices to its own routine to force first attach. Bug found
and debugged by joris@

Code mostly written by joris@


# 1.74 26-Dec-2006 marco

Add polling to devices that require it such as acpibat and acpitz.
Use the same tsleep/wakeup mechanism as the interrupt code to keep it non-
concurrent.


# 1.73 21-Dec-2006 deraadt

even more knf love


# 1.72 21-Dec-2006 marco

Remove some leftover acpiec gpe/interrupt code.


# 1.71 20-Dec-2006 deraadt

oops, name already used


# 1.70 20-Dec-2006 deraadt

rename __acpi_enable_gpe(); ok marco


# 1.69 20-Dec-2006 deraadt

acpi_unmap_pmregs() is not used, ok marco


# 1.68 19-Dec-2006 deraadt

simplify some low-level methods substantially (shrinking code); ok marco jordan


# 1.67 06-Dec-2006 mk

malloc(..., M_NOWAIT) with no NULL check is bad. If allocation fails,
complain, clean up, and bail out of attach.

input and ok gwk


# 1.66 28-Nov-2006 jordan

Fixed clearing arg for acpi_init_pic
ok kettenis@


# 1.65 27-Nov-2006 kettenis

Don't spam dmesg.


# 1.64 27-Nov-2006 jordan

Fixes for acpiprt routing
Post-parse fixup for namerefs
ok kettenis@


# 1.63 25-Nov-2006 marco

Don't handle acpiev events inside the gpe handler since this causes deadlocks.

ok jordan@


# 1.62 15-Nov-2006 kettenis

Hook up acpi apic interrupt routing support to generic acpi code.

ok jordan@


# 1.61 02-Nov-2006 marco

Kill acpiec attach function and add its functionality to the generic _HID
device attach

prodded and ok jordan


# 1.60 02-Nov-2006 marco

Fix comments to reflect reality.


# 1.59 19-Oct-2006 marco

Replace ACPI ioctls with APM ioctls. This makes ACPI talk the same protocol
as APM therefore userspace can now retrieve power information in the same
fashion as all other apm arches (zaurus, macppc, i386 etc).

To use force the use of acpi device like so: apmd -f /dev/acpi

ok jordan@


# 1.58 19-Oct-2006 jordan

Fixed GPE debug levels
Fixed ref counter on temporary bufferfields
DSDT parsing fully working on IBM Thinkpad
ok marco@


# 1.57 15-Oct-2006 jordan

Removed old GPE handler code remnants
ok marco@


# 1.56 12-Oct-2006 jordan

Massive ACPI changes
Fixed ACPI leaks
new ACPI API
ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.55 30-Jun-2006 gwk

KNF, No functional change,
"go at it" marco@


# 1.54 30-Jun-2006 jordan

Support for calling Embedded Controller via new GPE interface
ok marco@


# 1.53 30-Jun-2006 jordan

Added new code for GPE handling
Also works with ACPIEC; need to fixup acpiec.c to remove original interrupt handler
ok marco@


# 1.52 30-Jun-2006 jordan

Added offset argument for acpi read/write pmreg routines (new GPE code)
ok marco@


# 1.51 31-May-2006 todd

do not call acpi_powerdown() unless acpi has successfully initialized
ok gwk@ canacar@


# 1.50 29-May-2006 canacar

Add embedded controller support.
ok marco@ deraadt@ testing & ok gwk@


# 1.49 19-May-2006 canacar

Minimal thermal zone driver. Monitors thermal zone temperature,
shuts down the system if the 'critical temperature' is reached.
ok marco@


# 1.48 11-Apr-2006 marco

KNF.


# 1.47 11-Apr-2006 gwk

Add acpi_resume(struct acpi_softc *sc), which calls some AML methods,
detected at attach time, currently unused.

ok marco@


# 1.46 11-Apr-2006 gwk

Discover some ACPI sleep related methods, and call them at the
appropriate time in acpi_enter_sleep_state().

ok marco@


Revision tags: OPENBSD_3_9_BASE
# 1.45 26-Feb-2006 marco

Add acpicpu as a device.

ok jordan@


# 1.44 23-Feb-2006 marco

remove dead function.

ok jordan@


# 1.43 22-Feb-2006 marco

Print out all tables it found during autoconf. We'll need this data in
the future.


# 1.42 22-Feb-2006 marco

Make dmesg less verbose.

ok jordan.


# 1.41 22-Feb-2006 jordan

Added parsing of SSDT tables
ok marco@


# 1.40 22-Feb-2006 jordan

Added new aml_register to notify by device id
Moved powerdown code to button device
ok marco@


# 1.39 21-Feb-2006 marco

Readjust debug levels that crept in too low over the last few days.
KNF


# 1.38 21-Feb-2006 marco

Rig the power button so that the machine shuts down correctly.


# 1.37 20-Feb-2006 marco

Add acpibtn device. Remains disabled for now.


# 1.36 19-Feb-2006 jordan

Added code for aml_notify GPE notification callback
ok marco@


# 1.35 19-Feb-2006 grange

Fix acpi_enter_sleep_state() by replacing current ugly hack
with mostly correct sleeping state transition code.

ok brad@ marco@


# 1.34 19-Feb-2006 marco

jordan:
Enable interrupts.
Added peripheral code to make this work.

marco:
Replace softintr and other types of interrupts with a handler with context.
Fixed interrupt magic for GPEs.
Some KNF

This is work done by jordan and I but has to be a single commit.


# 1.33 17-Feb-2006 marco

Remove earlier introduced delays. We really only need them during autconf.

ok jordan@


# 1.32 17-Feb-2006 marco

Add a generic delay function as per the ACPI spec.

from jordan@


# 1.31 17-Feb-2006 marco

Use tsleep instead of delay when !cold.


# 1.30 17-Feb-2006 marco

Argh, we need delays between reads/writes. The backend reads (i2c most likely)
are so slow that values come back as 0 sometimes. We have to do this
despite the spec saying we shouldnt have to. Guess having a faster
parser than others can hurt sometimes.

Idea from jordan.


# 1.29 17-Feb-2006 jordan

Fixed code for handling _INI
Rearranged debug statements
ok marco@


# 1.28 16-Feb-2006 jordan

Added stub for calling _INI
Fixed aml_dowhile for NULL return
ok marco@


# 1.27 16-Feb-2006 jordan

Fixed parser/evaluator to retrieve battery info+state
Cleanup of parser
Dell laptops return battery status
ok marco@


# 1.26 06-Feb-2006 brad

fix a typo so this will compile on amd64.


# 1.25 04-Feb-2006 marco

When ACPI_ENABLED wasn't set but the devices were it had an unknown variable.

Pointed out by ian@.


# 1.24 03-Feb-2006 jordan

Rewrote aml parser yet again...
Fixed bus_space_map for i386/amd64
Added _BST and _BIF to acpibat to get status/info
ok marco@


# 1.23 20-Jan-2006 jordan

More evaluator changes
Removed old evaluator code
Fixed _HID value
ok marco@


# 1.22 19-Jan-2006 jordan

Added support for read/write of PCI Config space
Help jason@
ok marco@


# 1.21 18-Jan-2006 jordan

Added new evaluation routines
Added helper methods for buffer/field evaluation
Fixed broken indentation on dsdt.c

ok marco@


# 1.20 17-Jan-2006 jordan

Major overhaul of the aml parser. Evaluater temporarily disabled.

ok marco@


# 1.19 06-Jan-2006 grange

Check for ACPI control availability only if ACPI_ENABLE defined.


# 1.18 06-Jan-2006 grange

Informative error messages on attach, prettier dmesg.


# 1.17 06-Jan-2006 grange

printf -> dnprintf conversion for debug messages.


# 1.16 05-Jan-2006 grange

Find SLP_TYPx values and store them for future use in
acpi_enter_sleep_state().


# 1.15 05-Jan-2006 grange

Random KNF and cleanup


# 1.14 05-Jan-2006 grange

Provide symbolic names for the acpi sleeping states and use it.


# 1.13 28-Dec-2005 marco

Moved DEVNAME to a acpidev.h

ok jordan@


# 1.12 18-Dec-2005 sturm

add back comment about wrong code

ok marco


# 1.11 16-Dec-2005 marco

Remove test for kbd_reset. Prompted deraadt@, ok jordan@


# 1.10 16-Dec-2005 jordan

Added hooks for powerdown/power button press


# 1.9 16-Dec-2005 jordan

Cleaned up common power management register i/o
Added enter sleep state ioctl
Hooked power button interrupt
ok marco@


# 1.8 16-Dec-2005 marco

Remove autoconf chatter


# 1.7 16-Dec-2005 jordan

Moved DSDT AML parsing into parent ACPI driver
ok marco@


# 1.6 14-Dec-2005 marco

Add AC device glue.


# 1.5 14-Dec-2005 marco

Adjust debug printing levels to something more sane.


# 1.4 13-Dec-2005 marco

Add glue to attach devices to apci.


# 1.3 07-Dec-2005 marco

KNF


Revision tags: OPENBSD_3_8_BASE
# 1.2 10-Jul-2005 grange

Spacing cleanup.


# 1.1 02-Jun-2005 tholo

Start on a basic ACPI framework -- does not do much more than read out the
ACPI tables into kernel memory and attach ACPI and HPET timers currently.

In order to test this code, enabling the devices in GENERIC as well as
the ACPI_ENABLE option is needed. This code does not do any thermal
control yet, so this should be done with care depending on the platform.

In the tree so more people can contribute to making this more fully
featured.

Ok niklas@ grange@ tedu@


# 1.383 08-May-2020 kettenis

Move parsing of _CRS into acpi(4) and pass the parsed address and interrupt
information in struct acpi_attach_args.

ok mpi@


Revision tags: OPENBSD_6_7_BASE
# 1.382 14-Apr-2020 kettenis

Implement acpi_getprop(), the ACPI equivalent of OF_getprop().

ok patrick@


# 1.381 12-Apr-2020 kettenis

Add support for the _CCA method. This method indicates whether DMA is
cache-coherent or not. To implement this, acpi(4) gets two bus_dma tags
and passes the appropriate one when attaching devices based on _CCA.
On i386/amd64, where for all practical purpose DMA is always cache-coherent,
the two tags are the same. But on arm64 they are distinct.

ok patrick@


# 1.380 07-Apr-2020 visa

Abstract the head of knote lists. This allows extending the lists,
for example, with locking assertions.

OK mpi@, anton@


# 1.379 03-Apr-2020 mpi

Fix incoherencies of errors returned by various *kqfilter routines.

EOPNOTSUPP is returned when there's no "filterops" for a given fd.
EINVAL when the requested filter isn't supported by the "filterops".
ENXIO means the underlying device is no longer valid/present.

ok visa@


# 1.378 20-Feb-2020 visa

Replace field f_isfd with field f_flags in struct filterops to allow
adding more filter properties without cluttering the struct.

OK mpi@, anton@


# 1.377 31-Dec-2019 visa

Use C99 designated initializers with struct filterops. In addition,
make the structs const so that the data are put in .rodata.

OK mpi@, deraadt@, anton@, bluhm@


# 1.376 31-Dec-2019 mpi

Convert infinite sleeps to {m,t}sleep_nsec(9).

ok kn@


# 1.375 31-Dec-2019 jsg

include sys/sensors.h for sensor use
don't depend on indirect inclusion from acpidev.h


Revision tags: OPENBSD_6_6_BASE
# 1.374 07-Sep-2019 kettenis

Revert acpipci(4) changes. Too many systems are broken. We'll try again
after 6.6 as been released. The acpireg.h change stays behind.


# 1.373 28-Aug-2019 kettenis

Use ACPI information to attach PCI busses like we do on arm64. There are a
few additional quirks though, and attaching the PCI busses is delayed to
replicate the existing code more closely. That may be changed in the
future. Also tweak how we handle MSI support and respect to ACPI flag
that says we shouldn't attempt to use MSIs.

Some fallout is expected.

ok patrick@


# 1.372 27-Aug-2019 deraadt

On amd64 the bootup and resume paths for SP/BP/SP were not performing
msr/pat, clock-startup, firmware upgrade, fpu initialization, and cpu
soft-state initialization in the same order. That kind of chaos is
crazy and fragile. Improve the acpi resume call sequence and unify order
of the operations.


# 1.371 02-Jul-2019 jcs

acpi_set_gpehandler: turn edge arg into flags, add support for
passing GPE_DIRECT which directly executes the supplied callback and
clears a level interrupt rather than going through acpi_taskq which
can be slow when other driver callbacks are in the queue.

This should not be used for GPE handlers that need to do anything
ACPI-related.

ok kettenis


# 1.370 10-Jun-2019 kettenis

Print proper ACPI version number.

ok mlarkin@


# 1.369 08-Jun-2019 kettenis

When establishing the mapping between ACPI device nodes and PCI devices,
skip PCI host bridges and devices that aren't there.

ok patrick@


# 1.368 07-Jun-2019 kettenis

Introduce acpi_getsta() and use it to eliminate some duplicated code.

ok patrick@


# 1.367 12-May-2019 kettenis

Make sure the extended PM register descriptions actually exist before
using them. Fixes machines with ancient ACPI 1.0 (such as recent
hypervisors).

ok deraadt@


# 1.366 12-May-2019 kettenis

Add support for mapping ACPI PM registers using the "extended" gasio
representation from the FADT. Mostly fixes the Lanner NCA-1510.

ok lteo@, mlarkin@, deraadt@


# 1.365 11-May-2019 lteo

Fix incorrect error message.

ok kettenis@


# 1.364 08-May-2019 tedu

free size. the allocation is the header plus the length.


Revision tags: OPENBSD_6_5_BASE
# 1.363 04-Apr-2019 kettenis

Avoid potential null-pointer dereference. Found by Kent R. Spillner.

ok deraadt@, jsg@


# 1.362 20-Jan-2019 jsg

fix SMALL_KERNEL build after changes in rev 1.361
reported by naddy@ ok deraadt@


# 1.361 19-Jan-2019 tedu

Add a pwraction sysctl that controls what the power button does on acpi.
By default, nothing changes -- shutdown is initiated. But allows turning
power button into a sleep button if desired.
(grudging) ok from a few parties


# 1.360 26-Oct-2018 kettenis

Add acpipci(4) on amd64. For now this only calls the PCI-specific _OSC
method to let the ACPI implementation know what features we support.


Revision tags: OPENBSD_6_4_BASE
# 1.359 30-Aug-2018 kettenis

Move softbutton check before the check that skips attaching certain acpi
devices. Fixes regression caused by matching on _CID in addition to
matching on _HID.

ok matthieu@, mlarkin@


# 1.358 25-Aug-2018 kettenis

Match ACPI devices based on _CID if no match for _HID is found.

ok mpi@


# 1.357 19-Aug-2018 kettenis

Add support for multiple PCI segments. Only really implemented for arm64
for now as amd64/i386 firmware still caters for legacy OSes that only
support a single PCI segment.

ok patrick@


# 1.356 03-Aug-2018 kettenis

Let ahci(4) match on _CLS instead of _HID when attaching at acpi(4). Avoids
having to add many more _HID entries to the match table.

ok deraadt@, mlarkin@


# 1.355 10-Jul-2018 kettenis

Make legacy interrupts work in acpipci(4).

ok patrick@


# 1.354 05-Jul-2018 kettenis

Add acpipci(4), a driver that supports generic ECAM-compatible PCI host
bridges based on information provided by ACPI.

ok mlarkin@


# 1.353 04-Jul-2018 kettenis

Properly pass around the PCI "chipset tag" in acpi(4) and refactor
acpimcfg(4) to call an MD initialization functions that sets up a tag for
PCI ECAM.

ok guenther@, mlarkin@, krw@


# 1.352 01-Jul-2018 kettenis

Move acpi(4) attach glue into acpi_machdep.c.

ok guenther@, deraadt@


# 1.351 01-Jul-2018 kettenis

Add support for _DSD Device Properties.

ok mlarkin@


# 1.350 30-Jun-2018 kettenis

Add intr_enable() function, intended for MI use to amd64 and i386 and use
this in the acpi(4) suspend/resume code paths.

ok deraadt@


# 1.349 29-Jun-2018 kettenis

Use <stdint.h> types.

ok mpi@, jung@, krw@, deraadt@


# 1.348 29-Jun-2018 mlarkin

ACPI: Allow (with warning) GPE handler reassignment, instead of returning
a failure code.

ok kettenis


# 1.347 26-Jun-2018 mlarkin

knf


# 1.346 26-Jun-2018 kettenis

Let acpi(4) pass down the bus dma tag.

ok deraadt@


# 1.345 25-Jun-2018 kettenis

Reorganize acpi(4) code a bit in preparation for upcoming arm64 support.

ok deraadt@


# 1.344 20-May-2018 kettenis

Wake up the acpi thread after scheduling a gpio event.

ok mpi@


# 1.343 17-May-2018 kettenis

Sprinkle some #ifndef SMALL_KERNEL such that RAMDISK kernels cuild again.

pointed out by stsp@


# 1.342 17-May-2018 kettenis

Implement GenericSerialBus OpRegion support.

ok mlarkin@


# 1.341 27-Mar-2018 kettenis

Add acpicmos(4), a driver that implements SystemCMOS OperationRegion
access support. This fixes machines where the AML doesn't check whether
support for this OperationRegion type has been registered by the OS.

ok mlarkin@


Revision tags: OPENBSD_6_3_BASE
# 1.340 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.339 10-Feb-2018 deraadt

Syncronize filesystems to disk when suspending. Each mountpoint's vnodes
are pushed to disk. Dangling vnodes (unlinked files still in use) and
vnodes undergoing change by long-running syscalls are identified -- and
such filesystems are marked dirty on-disk while we are suspended (in case
power is lost, a fsck will be required). Filesystems without dangling or
busy vnodes are marked clean, resulting in faster boots following
"battery died" circumstances.
Tested by numerous developers, thanks for the feedback.


# 1.338 08-Feb-2018 deraadt

After flushing disks, try again to release all memory. We don't need to
save dirty memory to the hibernate space.


# 1.337 08-Feb-2018 deraadt

Toss all releaseable memory, because fragmentation can get in the way
of allocating the hibernate playpen.


# 1.336 08-Feb-2018 deraadt

sr_quiesce() is a new approach for ensuring that softraid drains
output to the disks.

This is part of a larger suspend/resume filesystem-safety diff, which
has been worked on for a couple of months already. Tests by job, krw,
beck, benno, and others. Sometimes even by snapshot users...


# 1.335 29-Nov-2017 kettenis

Add support for GPIO-signaled events.

ok mlarkin@


# 1.334 16-Nov-2017 jcs

Add PCI attachment for dwiic(4) needed by Intel 100 Series machines

ok kettenis


# 1.333 14-Nov-2017 kettenis

Add a few more PNP IDs to the skip lists.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.332 17-Aug-2017 stsp

Make dwiic(4) attach its ACPI parent dependencies.
This change makes the Asus E200HA keyboard work.

Original analysis and patch by Cesare Gargano
ok kettenis@


# 1.331 09-Aug-2017 dcoppa

Add "PNP0303" (8042 PS/2 Controller) to acpi_isa_hids[]

ok kettenis@


# 1.330 22-Jul-2017 jcs

acpibat: look for _BIX first before falling back on _BIF

ACPI 4.0 deprecated _BIF for battery status so some newer machines
are exporting _BIX which extends _BIF's package with a few extra
fields. When using _BIX, export the new discharge cycle count as a
sensor.

feedback from kettenis and anton
ok deraadt


# 1.329 20-Jul-2017 kettenis

Start a list of isa(4) device names such that we can avoid printing them
as "not configured".

ok mikeb@, millert@, deraadt@


# 1.328 08-Apr-2017 deraadt

Sensors are run as callbacks inside tasks. During suspend a sensor
could be running inside a driver that will be force-detached, or due
to tsleep end up disrupting the softstate/hardstate contract. At
suspend time, quisce all these callbacks by waiting for completion.
This issue has never been observed for real, but may be implicated in
suspend/resume failures.
ok kettenis guenther mlarkin


# 1.327 08-Apr-2017 deraadt

Two prototypes for acpi_maptable; merge to one place


Revision tags: OPENBSD_6_1_BASE
# 1.326 27-Mar-2017 deraadt

Now that hibernate_alloc() only has clean success/failure, don't
need to call hibernate_free() to clean up a partial mess.
ok mlarkin kettenis


# 1.325 13-Mar-2017 deraadt

allow ramdisks to build; ok jcs


# 1.324 12-Mar-2017 jcs

try to attach acpisbs first and if successful, don't attach acpibat


# 1.323 02-Mar-2017 natano

Add a new sysctl machdep.lidaction. The sysctl works as follows:

machdep.lidaction=0 # do nothing
machdep.lidaction=1 # suspend
machdep.lidaction=2 # hibernate

lidsuspend is just an alias for lidaction, so if you change one, the
other one will have the same value. The plan is to remove
machdep.lidsuspend eventually when people have upgraded their
/ets/sysctl.conf.

discussed with deraadt, who came up with the new MIB name
no objections mlarkin
ok stsp halex jcs


# 1.322 28-Feb-2017 natano

Untangle abstract sleep modes from ACPI sleep states. This paves the way
for further work in the area.

idea and ok deraadt
ok mlarkin


# 1.321 22-Feb-2017 jcs

add acpisbs, an acpi smart battery subsystem driver reading data
over smbus

currently disabled because it conflicts with acpibat

ok deraadt, kettenis


# 1.320 14-Jan-2017 kettenis

Revert aml_rdpciaddr changes; breaks several machines that were working
before.


# 1.319 11-Jan-2017 jsg

Increase the size of the buffers used for _HID and _CID strings to be
able to fit "Hyper_V_Gen_Counter_V1".

ok mlarkin@ deraadt@


# 1.318 08-Jan-2017 kettenis

Make aml_rdpciaddr() work for for devices behind bridges. The code now
uses the mapping between AML nodes and PCI devices that we establish.
Because _INIT methods may end up calling aml_rdpciaddr(), make sure we
create that mapping early. Also handle devices that aren't actually present.
These devices are now included in the mapping and reads will return an
all-ones patterm whereas writes are a no-op..

ok mlarkin@


# 1.317 25-Oct-2016 pirofti

Add the resource index to aml_parse_resource() callback function.

This is needed by an upcoming acpiec commit that handles machines breaking
the current ACPI specifications.

Change suggested by and ok kettenis@, guenther@


# 1.316 18-Sep-2016 guenther

If the APIC table (MADT) isn't present then ci_acpi_proc_id won't be set
when acpicpu tries to attach, leading to a NULL dereference. The ACPI
processor object on the Dells where this was reported is bogus and useless
anyway so apply the same test when deciding whether to attach to processor
objects as acpicpu would when matching them to cpus.

Problem reported by Eivind Eide (xenofil (at) gmail.com) and
Johan Huldtgren (johan+openbsd-bugs (at) huldtgren.com).
Testing by Eivind Eide.
ok kettenis@ mlarkin@


# 1.315 03-Sep-2016 naddy

Write the system time back to the RTC every 30 minutes.
This fixes the problem that long-running machines which were not
shut down properly would reboot with a badly offset system time.

hints and ok kettenis@


# 1.314 31-Aug-2016 mlarkin

Don't put a device in the wake device list if _STA says it's not there.

Fixes at least one machine that woke up from sleep / poweroff immediately.

ok kettenis@, tested by kettenis and martijn@


# 1.313 28-Jul-2016 kettenis

Store the acpi processor ID/UID in struct cpu_info, and use it to attach
acpicpu(4) drivers to the right cpu(4).

ok mlarkin@, guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.312 10-Jun-2016 kettenis

The power management timer has been made optional in ACPI 5.0A, so don't
attempt to attach acpitimer(4) if the timer isn't present.

Pointed out by jcs@

ok jcs@


# 1.311 08-May-2016 kettenis

Make sure devices listed by a device's _DEP method are attached before
we attach the device itself. The _DEP methode, introduced in ACPI 5.0,
is a hint that the device in question depends on OpRegion support from
the devices listed by _DEP, so we have to order them the proper way.
To prevent us from attaching those devices again when we encounter them
later walking down the device tree, keep track of our attempts to
attach a device driver to them using aflag in the aml_node structure.

ok guenther@


# 1.310 25-Apr-2016 pirofti

We attach acpihpet(4) and acpiec(4) without using the HID, so do not
report it in the dmesg.

OK deraadt@, mlarkin@.


# 1.309 02-Apr-2016 kettenis

Reduce dmesg spam by knocking out some acpi hardware IDs that don't need a
driver.

ok mpi@


# 1.308 29-Mar-2016 kettenis

Check if a device is present (using _STA) in acpi_foundhid. This prevents
us from reporting devices as "not configured" that aren't actually present,
and allows us to remove duplicated code from several drivers.

ok deraadt@, ok jsg@


# 1.307 29-Mar-2016 kettenis

Implement a cleaner way to prevent acpivout(4) from attaching if we have
a platform-specific method of controlling the backlight.

ok millert@, mlarkin@, jung@


# 1.306 28-Mar-2016 kettenis

Adding each and every new acpi device driver to acpi_foundhid() is
gettinga bit of the burden. And it really isn't how our autoconf
framework is supposed to work for busses that can be enumerated. All
the drivers already check for a matching _HID in their attach
function. So we can just drop the checks in acpi_foundhid() and let
the drivers sort themselves out.

Print a "not configured" line for devices for which we don't attach adriver.
This may be a bit much as there are devices for which we will not have a
driver. This will be removed if it causes too much dmesg spam.

ok deraadt@, mlarkin@


Revision tags: OPENBSD_5_9_BASE
# 1.305 17-Jan-2016 jsg

Take acpi_foundhid() outside of #ifndef SMALL_KERNEL so the ramdisk
kernel can find and attach dwiic(4) and sdhc(4).


# 1.304 17-Jan-2016 jsg

remove an unused global variable


# 1.303 14-Jan-2016 kettenis

Attach dwiic(4) on the Intel Bay Trail i2c controllers as well.


# 1.302 12-Jan-2016 kettenis

Improve emulation of PM registers on Hardware-redcued ACPI by emulating
SPL_TYPx, SLP_EN and WAK_STS using the new SLEEP_STATUS_REG and
SLEEP_CONTROL_REG registers. Makes the transition into S5 (powerdown) work
on the ASUS X205TA.

ok deraadt@, mlarkin@


# 1.301 12-Jan-2016 jcs

Add dwiic, a driver for the Synopsys DesignWare i2c controller found
on the Samsung ATIV Book 9 laptop. This initial version only
supports ACPI config/attachment.

Add ihidev, a HID-over-i2c driver largely based on uhidev. dwiic
handles attaching ihidev devices found in ACPI.

Add ims, a HID-over-i2c mouse/trackpad driver to get basic cursor
and button functionality from HID-compliant i2c trackpads.

ok kettenis deraadt


# 1.300 11-Jan-2016 kettenis

Make sdhc(4) attach to hardware IDs "80860F14" and "PNP0FFF".


# 1.299 09-Jan-2016 kettenis

Initial support for Hardware-Reduced ACPI. This skips the legacy mode checks
by emulating the PM_CNT register such that SCI_EN is always enabled. It also
skip attaching acpitimer(4) since the relevant registers are no longer there.

ok deraadt@, mlarkin@


# 1.298 06-Jan-2016 kettenis

Follow the spec and check the SCI_EN bit to see if ACPI has already
been enabled and don't bother enabling it again if it is. This should fix
systems that don't implement legacy mode and therefore set ACPI_ENABLE and
ACPI_DISABLE to zero.

ok mlarkin@, yasuoka@


# 1.297 23-Nov-2015 reyk

Back out the CRITICAL change, turn it back from 10% to 25%. Some
people with old/broken batteries didn't like this change, and the
underlying heuristics are wrong.


# 1.296 22-Nov-2015 reyk

The "CRITICAL" APM battery level, that is exported/faked by the ACPI
stack, is currently set to 25%. With 25% battery left, a typical
modern laptop will have at least one hour runtime left. It is an
arbitrary value, and other APM power layers in the kernel set CRITICAL
to something like 5% or 10%, so set it to 10% for now.

OK mlarkin@


# 1.295 28-Sep-2015 deraadt

In low-level suspend routines, set cold=2. In tsleep(), use this to
spit out a ddb trace to console. This should allow us to find suspend
or resume routines which break the rules. It depends on the console
output function being non-sleeping.... but that's another codepath which
should try to be safe when cold is set.
ok kettenis


# 1.294 06-Sep-2015 deraadt

clarify malloc arguments (base + length...)


# 1.293 28-Aug-2015 deraadt

fairly simple sizes for free(); ok tedu


# 1.292 20-Aug-2015 kettenis

Respect the access size when reading or writing to pci config space.
This version also makes sure writes are properly aligned, preventing
the panics seen with the previous incarnation of this change.

Fixes battery status passthrough in vmware, brightness keys on my x220
and who knows what else.

ok mlarkin@, deraadt@


Revision tags: OPENBSD_5_8_BASE
# 1.291 04-Aug-2015 deraadt

If we are setting ACPI_PM1_BM_RLD do it only once, and remember to
repeat this action at resume time.
ok mlarkin guenther


# 1.290 29-Jul-2015 deraadt

Something subtle in previous revision is locking up suspend or booting
on some laptops. Mailing lists threads mention x220 and Asus machines.
Reverting for now.


# 1.289 21-Jul-2015 kettenis

iRespect the access size when reading or writing to pci config space.
Fixes battery status passthrough in vmware and who knows what else.

Tested by many.

ok mlarkin@, deraadt@


# 1.288 17-Jul-2015 mlarkin

fix comments


# 1.287 26-Mar-2015 jsg

Use the 0x10 extended capability to determine if the trackstick
buttons are wired to the trackpad and need to be re-routed to the
trackstick. Without this change the buttons on 2015 thinkpads
get picked up as extended buttons that show up as scroll up/down.

Remove the X1 Carbon 2015 (LEN0048) and X250 (LEN0046) from the
top button area/soft buttons quirks list. Also avoid using the
quirk list entirely if the capability bit is set.

Based on work from jcs@ with feedback from mpi@
ok mpi@ jcs@


# 1.286 20-Mar-2015 kettenis

The ACPI standard says that a PCI function number of FFFF can be used to refer
to all functions on a device. This makes no sense in the context of accessing
PCI config space. Nevertheless stsp@ has a machine with AML that tries to
do config space reads from a device where _ADR encodes function number FFFF.
This is almost certainly buggy AML, but since Linux (and presumably Windows)
boot just fine on this machine we need to work around this. Linux effectively
treats function FFFF as function 7, which isn't present on this machine.
Instead we simulate a read from a non-existent device by returning a bit
pattern of all ones. Writes to function FFFF will still panic.

ok mlarkin@


# 1.285 16-Mar-2015 deraadt

Reduce use of SMALL_KERNEL in acpi, bringing acpiec driver and more gpe
handling into RAMDISK. This is now possible because the install media
has ample room. The goal is to reduce special cases where we may be
skipping (unknown) important operations...
ok mlarkin kettenis


# 1.284 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.283 07-Feb-2015 deraadt

New framework that allows hibernate to pass in entropy from it's fresh
boot.
ok mlarkin


# 1.282 06-Feb-2015 mlarkin

Fix a hibernate crash on some machines due to unmapping a page that
may not have been mapped previously (in the failure to hibernate case).

Also ensure that the lowmem ptp is mapped in all cases (not just MP).

ok kettenis


# 1.281 17-Jan-2015 deraadt

resume_randomness() before spinning up other cpus...


# 1.280 15-Jan-2015 jsg

Define a new wscons mouse type for Synaptics clickpad devices that lack
physical buttons. This will be used if the acpi pnpid for the mouse
matches a list provided by Synaptics found in Linux.

Combined with the xenocara changes this will remove the need for an xorg
config file for the x240/t440/t540 generation of thinkpads.

ok matthieu@ shadchin@ kettenis@


# 1.279 11-Jan-2015 kettenis

Tolerate checksum errors in ACPI tables (but complain about them). Apparently
there are machines out there with broken BIOSen with such checksum errors in
thier RSDT tables. Windows runs fine on these machines, so it probably does
not check. Linux ignores checksum errors as well (and prints a warning).

ok deraadt@, krw@


# 1.278 18-Dec-2014 deraadt

suspend and resume the RNG (at what looks like the right spots)
ok reyk djm


# 1.277 09-Dec-2014 doug

Replace some malloc(n*size,...) calls with mallocarray().

ok tedu@ deraadt@


# 1.276 06-Dec-2014 mpi

If "machdep.lidsuspend" is not set, do not go back to sleep if the lid
is still closed when resuming. This allows docked laptop to be resumed
without opening them.

ok mlarkin@


# 1.275 23-Nov-2014 mlarkin

If a machine has no _LIDs, then none can be open. This diff fixes a problem
where machines without any _LIDs were going back to sleep after hibernate
due to the recent suspend-if-lid-closed-on-resume diff. Noticed on qemu,
which doesn't have any _LIDs.

discussed with deraadt


# 1.274 18-Nov-2014 krw

Nuke yet more obvious #include duplications.

ok miod@


# 1.273 08-Nov-2014 mlarkin

If resuming from sleep (zzz/ZZZ) and the lid is still closed, go back to
sleep. This prevents accidental lid flex or slight opening in a backpack
from waking the machine up and leaving it resumed (powered on).

ok deraadt@


# 1.272 26-Sep-2014 kettenis

Rework piglet and pig allocation. Currently the piglet gets allocated
deep down in the suspend path, where it is really hard to recover from
allocation failure. So allocate the piglet early on in the suspend path.
Also change the piglet and piglet allocation functions to use km_alloc(9)
instead of doing pmemrange magic. This removes a bunch of code which, in the
case of the piglet allocation, is broken since it results in a NULL pointer
dereference. Also switch the piglet allocation to not wait. If we can't
allocate 16MB of phys contig memory on a halfway modern machine we're almost
certainly under a lot of memory pressure and we're better off not trying to
hibernate anyway.

ok mlarkin@


# 1.271 25-Sep-2014 kettenis

Bring back rev 1.249. Now that mp_setperf() has been fixed, it should be
safe to restore perflevel when resuming. Should fix the issues people
see with the processor running at full speed heating up the machine while
the apm command reports that the CPUs are running at a low speed.

ok dcoppa@, deraadt@


# 1.270 23-Sep-2014 kettenis

Release the acpi lock around calls to wsdisplay_suspend() and
wsdisplay_resume(). The X server may adjust the display brightness just
before/after the VT switch when suspending/resuming. In fact, it may
already be blocked adjusting the display brightness for some reason
unrelated to the suspend request.

The VT switch request will eventually time out, but the delay is annoying
and the failed VT switch will lead to problems after resume.

Needed for a future update of the xf86-video-intel driver.

ok mlarkin@


# 1.269 19-Sep-2014 kettenis

Use config_suspend_all(9).


# 1.268 08-Sep-2014 kettenis

Clear the PME Status bit on PCI devices in response to Device Wake events.
Fixes the interrupt storm on the 2009 Mac mini with WOL enabled on its
nfe(4) interface.

ok mlarkin@


Revision tags: OPENBSD_5_6_BASE
# 1.267 20-Jul-2014 mlarkin

Support hibernating to softraid crypto volumes.

much help and ok from deraadt@


# 1.266 16-Jul-2014 mlarkin

Reenable hibernate RLE support and flush+zero all memory after unpack.


# 1.265 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.264 12-Jul-2014 kettenis

Add a function to drop all clean pages on the page daemon queues and call
it when we hibernate.

ok mlarkin@, miod@, deraadt@


# 1.263 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.262 11-Jul-2014 mlarkin

Flush the buffercache to 16MB on hibernate and restore its previous max
size (kern.bufcachepercent) on resume, for better hibernate performance.

ok beck@


# 1.261 10-Jul-2014 blambert

remove unneeded header

ok mlarkin@


# 1.260 01-Jul-2014 kettenis

Adding an additonal delay after each access to SystemMemory or SystemIO
during autoconf makes no sense.

ok deraadt@


# 1.259 23-Jun-2014 kettenis

SystemMemory is used to access memory mapped registers on some machines, so we
must use the bus_space(9) API to access it instead of memcpy(9). Also make
sure we properly align access to fields that need more than byte alignment.

ok guenther@


# 1.258 21-May-2014 mlarkin

Fix invalid / missing format specifiers in several dnprintf statements
(used only when ACPI_DEBUG is enabled)

From a diff on tech@ by Fabian Raetz, with a minor change by me, tested
on i386 and amd64 with ACPI_DEBUG enabled and disabled.


# 1.257 25-Apr-2014 mlarkin

Defer the free of the hibernate structures until later, when it is safe to
do so.

Freeing the hibernate structures too early leads to inadvertent calls into
splx when we aren't ready for them yet. Found when trapping splx calls in
the resume path.

discussed with deraadt@ at m2k14


# 1.256 13-Apr-2014 mlarkin

Fix a badly worded debug message that implied the entire machine was going
to sleep, instead of just the acpi thread going to sleep.


# 1.255 31-Mar-2014 mpi

Change the order of operations during a suspend/resume cycle and
call bufq_quiesce() after executing the DVACT_QUIESCE handlers.

This should be safe since no disk nor controller drivers have such
handler but it will allow us to detach sd(4) devices attached to a
USB bus.

Another benefit pointed out by kettenis@ is that drivers that need
to read a firmware from the disk should be able to do it at resume
time in a DVACT_WAKEUP handler.

ok kettenis@, deraadt@


# 1.254 13-Mar-2014 dlg

get rid of the assumption that the head of the alldevs list is the
"mainbus" device. this breaks when mpath is enabled because it
attaches before mainbus and therefore takes the head position.

have autoconf provide device_mainbus() which looks up mainbus_cd,
and use that instead.

discussed with deraadt who just wants mpath stuff to move forward
despite there being many ways to shine this particular turd.


Revision tags: OPENBSD_5_5_BASE
# 1.253 21-Feb-2014 deraadt

Do the lid control further on in the resume sequence, since this is
AML, and will do WAITOK operations. While cold, that is a bad idea.
We seem to be safe with respect to the spec, I do not see any event
debounce issues here.
ok kettenis, tested by many


# 1.252 20-Jan-2014 claudio

Instead of checking sc->sc_revision <= 1 check that the
fadt->hdr_revision <= 1. This is a better check since it checks
the version of the proper descriptor. This allows the alix APU
to reboot since those boards have currently a wrong sc->sc_revision.
Committing this now so that it can be tested easier.
With and OK jsg@ also fine by derradt@


# 1.251 24-Dec-2013 kettenis

If the FADT has its SMI_CMD set to zero, assume we're only ACPI-only hardware
and don't need to disable SMI ownership of the ACPI hardware registers.

ok mlarkin@


# 1.250 23-Dec-2013 kettenis

Revert previous commit. It makes resume hang on some MP systems running
GENERIC.MP.


# 1.249 16-Dec-2013 kettenis

Restore hw.setperf upon resume like we do for apm(4) on i386. Seems we need
to do this fairly late in the resume to avoid hangs.

ok deraadt@, mlarkin@


# 1.248 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


# 1.247 06-Nov-2013 mpi

Add support for Power Resources for Dx states and the necessary hook
for PCI devices. This hook should be called twice, before and after
changing the power state of a PCI device.

Before setting the device to the new state, the ACPI layer will notify
every power resources linked to the device for that state and make sure
they are turned "_ON". After changing the state of the device, it will
decrement the reference of every power resources linked to that device
for the old state and turn them "_OFF" if they are no longer referenced.

This fixes the no-USB after resume problem seen on various ThinkPad,
problem initialy diagnosed with Alexander Polakov.

ok kettenis@, deraadt@


Revision tags: OPENBSD_5_4_BASE
# 1.246 01-Jun-2013 mlarkin

Stop printing ACPI wakeup devices in dmesg after the 16th wakeup device.

Helps with VMware VMs that report hundreds of wakeup devices.

ok reyk@, deraadt@


# 1.245 31-May-2013 bluhm

Fix the build for a kernel without wd and pciide in its config.
OK deraadt@ tedu@


# 1.244 30-May-2013 deraadt

Enforce ca_activate tree-walks over the entire heirarchy for all events,
cleaning up some shutdown-hook related code on the way.
(A few drivers related to sparc are still skipped at kettenis' request)
ok kettenis mlarkin, tested by many others too


# 1.243 18-Apr-2013 deraadt

pin the acpi thread to the BSP, because AML and SMI cannot be trusted. I
was certain we were doing this already, but it turns out no...


Revision tags: OPENBSD_5_3_BASE
# 1.242 24-Dec-2012 guenther

In acpiioctl(), Return though the splx() path if suser() returns an error

ok deraadt@


# 1.241 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


# 1.240 04-Oct-2012 ehrhardt

Use information provided by ACPI to attach secondary PCI host bridges.

ok kettenis@


# 1.239 07-Sep-2012 kettenis

Add a new function called acpi_pci_min_powerstate() that returns the lowest
power state for a PCI device for the power state (currently S3 or S4) we're
transitioning into. This function is probably too simplistic; there is a lot
of (silly) complexity in the ACPI spec.

ok deraadt@


Revision tags: OPENBSD_5_2_BASE
# 1.238 13-Jul-2012 pirofti

Disable acpivideo(4) on ASUS laptops.

There are some models that panic whenever an event is received.
Some cope, some don't. In order to get this right acpiec(4) requires
major rework. Until then let machines boot.

Discussed with deraadt@.


# 1.237 11-Jul-2012 mlarkin

for now, restrict hibernate to root in the acpi ioctl kernel interface.
normal rules for controlling this interface through apmd still apply.

ok deraadt@


# 1.236 09-Jul-2012 deraadt

tiny little oops in untested hibernate block


# 1.235 09-Jul-2012 deraadt

clean up ioctl() case statements


# 1.234 09-Jul-2012 deraadt

acpi_disable_onegpe() is not used; ok pirofti


# 1.233 24-May-2012 kettenis

If _MIN > _MAX consider a WordBus resource to be invalid. Fixes a panic on
the IBM x3550-M4.

ok mikeb@


# 1.232 29-Mar-2012 mlarkin

Missing #ifdef in previous diff


# 1.231 29-Mar-2012 mlarkin

Prevent hibernation attempts on machines with unsupported disk controllers.
The same check is also performed later in the hibernate sequence, but by
then it's too late to cleanly unwind (presently).

ok deraadt


# 1.230 26-Mar-2012 deraadt

simplify conditionals in acpi_sleep_state()
ok mlarkin


# 1.229 26-Mar-2012 deraadt

hook in the hibernate request code; half of this diff is from mlarkin
ok mlarkin


Revision tags: OPENBSD_5_1_BASE
# 1.228 20-Sep-2011 deraadt

Very early in suspend while the clock is still ticking realtime,
save the clock back to the rtc. The zaurus already did this.
ok phessler miod kettenis


Revision tags: OPENBSD_5_0_BASE
# 1.227 02-Jul-2011 nicm

kqueue attach functions should return an errno or 0, not a plain 1. Fix
the obvious cases to return EINVAL and ENXIO.

ok tedu deraadt


# 1.226 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.225 16-Jun-2011 pirofti

Import acpitoshiba driver.

This is originally written by Hiroyuki Aizu, ported and integrated
in our tree by Javier Vazquez <javama4 AT gmail> with tweaks and
suggestions by me.

Import prodded by deraadt@ and the lack of testers on tech@.


# 1.224 27-Apr-2011 jcs

attach acpithinkpad to newer lenovo models like the x120e

ok deraadt@


# 1.223 22-Apr-2011 jordan

Fix uninitialzied variables and formatting strings (-Wxxx errors)


Revision tags: OPENBSD_4_9_BASE
# 1.222 02-Jan-2011 jordan

Re-add code for ACPI taskqueue, eliminates need for state vars in acpi softc
Serialize processing ACPI tasks for sleep/powerdown/GPE event
ok deraadt@


# 1.221 31-Oct-2010 guenther

Revert last commit: it breaks resume on Thinkpads
"then please back it out" deraadt@


# 1.220 26-Oct-2010 jordan

Add task queue for ACPI gpe and notify handlers
This fixes eject on Dell Latitude dock and an issue on the Dell Mini
battery update.
ok deraadt


# 1.219 07-Oct-2010 jordan

Create separate functions for enabling wake and runtime gpes.
Remove old suspend/resume gpewalk function
ok deraadt, mlarkin


# 1.218 05-Oct-2010 jordan

split acpi_enable_onegpe to separate enable/disable functions
ok deraadt


# 1.217 05-Oct-2010 jordan

Added acpi_disable_allgpes function to clear GPE status
ok deraadt


# 1.216 05-Oct-2010 deraadt

clean up acpi_softc use more


# 1.215 05-Oct-2010 deraadt

delete a bit of #if 0 code


# 1.214 05-Oct-2010 deraadt

hide use of our global acpi_softc pointer a bit better


# 1.213 29-Sep-2010 deraadt

grab the acpi thread lock very early in acpi thread startup; not that it
is likely that anyone will race us... but why be careless.
ok kettenis


# 1.212 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.211 29-Aug-2010 jasper

- add a check for FWRITE here too.

ok miod@


# 1.210 27-Aug-2010 kettenis

Make PCI Power Management optional, and only enable it when acpi(4) attaches.

ok deraadt@


Revision tags: OPENBSD_4_8_BASE
# 1.209 08-Aug-2010 kettenis

Close race between the acpi thread and wscons ioctls running in process
context. We might rip this out post-release and replace it with a solution
that serializes things in a more obvious way.

ok deraadt@, miod@ and "those crazy texans who ok anything"


# 1.208 08-Aug-2010 deraadt

Report the acpi sleep states when on a RAMDISK; ok krw


# 1.207 07-Aug-2010 kettenis

Enforce acpi register access rescrictions.

ok mlarkin@, deraadt@, tested by marco@


# 1.206 07-Aug-2010 deraadt

upon resume, notify all the acpiac and acpibat drivers to update
their status. some laptops do not do this automatically.
ok kettenis canacar


# 1.205 06-Aug-2010 marco

Always call _PSW on _LID devices that support it to enable lid open to
resume the machine. Conversely disable it when the machine wakes up.

Tested by several ok deraadt


# 1.204 06-Aug-2010 deraadt

As part of a revamp of the PM1/GPE code, I write this diff to look for
unmanaged EN & STS bits in the PM1 register at interrupt time and report
them. As a side effect this splits the STS acknowledgement into two writes
(for power, and sleep) instead of one. The printf that is added (to spot
unmanaged STS bits) has not yet been triggered as far as we know.

Before the "write to PM1 registers at the right offset" diff went in, this
was not neccessary. But newer thinkpads do not have a working soft power
button without this diff.

We have no idea why.
ok mlarkin kettenis


# 1.203 05-Aug-2010 deraadt

PM1 power button events were generating 'fake events' to acpibtn PWRB,
but if there is no such device these were being tossed. Instead, arrange
for all power button handlers to flag a softc variable which is processed
by the thread; so that one place will know to request a powerdown (psignal
to init)
ok marco mlarkin


# 1.202 05-Aug-2010 deraadt

rename a variable which is annoyingly poorly named


# 1.201 05-Aug-2010 mlarkin

Write to the correct offset for various ACPI pmreg registers. Writing to
the correct register does properly clear the wake status bits and fixed
event bits, which allows some machines to have more than 1 suspend cycle.

bug spotted by deraadt@

tested by deraadt@ and myself on a half-dozen machines and others on
hackers@

ok deraadt@, marco@


# 1.200 05-Aug-2010 deraadt

Fix the power button. The pm1 register is 16 bits in size.
My mistake, a while back.
tested by krw


# 1.199 04-Aug-2010 kettenis

Print supported sleep states.

ok marco@, phessler@, deraadt@


# 1.198 03-Aug-2010 mlarkin

Use the proper flag for re-enabling certain hardware events (power btn,
sleep btn). The (incorrect) flag prevoiously used coincidentally had the
same value, so this shouldn't cause any different behavior than before.

tested on a variety of machines (i386, amd64, sp, mp) - no behavioral
change seen

ok deraadt@


# 1.197 28-Jul-2010 marco

Make all gasio failures look and behave the same.


# 1.196 28-Jul-2010 mlarkin

Delay a little longer on suspend before giving up. Fixes machines that
sometimes don't suspend because the sleep transition takes too long
(symptoms like failing to sleep but the backlight stays on).

ok deraadt@


# 1.195 27-Jul-2010 deraadt

remove XXX; EC can now be attached early when needed; ok jordan


# 1.194 27-Jul-2010 deraadt

more brackets feels good


# 1.193 27-Jul-2010 deraadt

XXX annotate a block in acpi_enter_sleep_state() that is pretty worrying


# 1.192 27-Jul-2010 deraadt

horrific indentation that nearly made me puke


# 1.191 27-Jul-2010 mlarkin

Set the sleeping indicator light on machines that support it, in case SMI
doesn't do it for us. Shows the 'moon' LED on older Thinkpads when
sleeping.

Tested on T43p, W500, T510 by me and on X61s by deraadt@.

ok deraadt@


# 1.190 27-Jul-2010 jordan

Early initialization of acpiec if ECDT table exists
Fixes hang when booting thinkpads while docked
ok deraadt


# 1.189 26-Jul-2010 deraadt

remove merge error which was commited


# 1.188 26-Jul-2010 pirofti

Add support for Sony ACPI hotkeys via a new driver: acpisony(4).

Currently it only works for the suspend button and tries to do right for
the brightness events, but I haven't found a Sony laptop that like to do
right so far.

In the future I want to make the brightness keys work on all Sony's and
also add support for the zoom hotkeys and whatever other funky keys I can
find on those things.

Okay deraadt@.


# 1.187 22-Jul-2010 deraadt

pretty large cleanup of SMALL_KERNEL


# 1.186 21-Jul-2010 deraadt

in the acpi GPE handler, late ack level interrupts not edge interrupts.
Duh.


# 1.185 21-Jul-2010 deraadt

Merge the edge and level gpe handlers into one (now possible since the
gpe structure tells us whether the gpe is edge or level)


# 1.184 21-Jul-2010 deraadt

delete #if 0 code we are not going to use; ok marco


# 1.183 20-Jul-2010 deraadt

make the mask code for acpi_enable_onegpe() match other similar code


# 1.182 20-Jul-2010 deraadt

Handle the acpi interrupt controller with proper edge/level handling,
wrapped in spltty to avoid racing against the interrupt controller.
Repair the gpe bit masking code while there, and do operations in the
right order
ok kettenis mlarkin, help from jordan at figuring out the order of
operations.


# 1.181 20-Jul-2010 matthew

When we're preparing to go to sleep, only call bufq_restart() if other
preparations failed.

ok kettenis@, thib@, deraadt@


# 1.180 19-Jul-2010 deraadt

permit compilation with ACPI_DEBUG


# 1.179 19-Jul-2010 deraadt

there is no need for a seperate dsdt_softc pointer; dsdt does not have a
softc but is just a part of the single acpi driver.
ok jordan


# 1.178 19-Jul-2010 deraadt

acpi_delay() is no longer needed; ok jordan


# 1.177 18-Jul-2010 mlarkin

Clear event status on resume, to avoid going back to sleep/reboot/shutdown
immediately after resume. Tested by a bunch of people on hackers@ on
various hardware. Fixes at least Toshiba NB200 and Gateway LT3103u.

ok marco


# 1.176 13-Jul-2010 deraadt

rename some thread related interfaces so they are logical


# 1.175 10-Jul-2010 jordan

Verify _ADR bdf is correct (some systems return 0xffff)
Works on Dell x9xx and Studio 1555


# 1.174 09-Jul-2010 jordan

Clear the noise


# 1.173 08-Jul-2010 jordan

Add mapping for ACPI device to PCI bdf (match autoconf tree)
Simplify resource parsing function to use buffer argument
Convert namespace linked lists to use queue macros
ok marco@, deraadt@


# 1.172 06-Jul-2010 deraadt

Use spltty() to lock downcalls from apm against the information being
modified by the acpi thread.
ok kettenis


# 1.171 06-Jul-2010 deraadt

Don't use a workq to do the suspend, because it races aginst the acpi
thread. Instead, just tell the acpi thread to do the suspend for us.
This makes apmd & zzz work correctly. While here, have acpithinkpad
attempt to post the event to apm before prompting the suspend itself.
ok kettenis marco mlarkin


# 1.170 05-Jul-2010 mlarkin

Reset register SCI_EN on resume. The ACPI spec clearly states we don't have
to do this, but many machines don't, or forget to, which leads to all sorts
of problems on resume (broken battery stats, overheating, failure to resume
more than once, etc).

tested by many, ok marco@, deraadt@


# 1.169 01-Jul-2010 thib

do bufq quiesce again, since vnd/the bufq_done call in
biodone has been fixed.


# 1.168 01-Jul-2010 jordan

Backout recent AML changes, breaks on R210 and others


# 1.167 01-Jul-2010 jordan

Simplified aml_parse_resource call, cleanup code
ok marco


# 1.166 01-Jul-2010 jordan

Fix subordinate bus # for multi-root PCI buses
ok marco, kettenis


# 1.165 30-Jun-2010 thib

Disable/partially backout the bufq quiesce changes as this
is causing havoc with vnds and release must be buildable.


# 1.164 29-Jun-2010 deraadt

Fix ramdisks (new code was placed inside a SMALL_KERNEL chunk)


# 1.163 29-Jun-2010 jordan

Add support for mapping ACPI to PCI devices
ok kettenis, deraadt


# 1.162 29-Jun-2010 kettenis

Call bufq_quiesce() on suspend to stop all I/O and bufq_restart on resume.
Makes suspend/resume work much better while doing disk I/O.

ok mlarkin@


# 1.161 29-Jun-2010 tedu

60 is a more reasonable guess for number of minutes in an hour than 100.
spotted by TeXitoi. ok deraadt


# 1.160 27-Jun-2010 jordan

Add _acpi_matchhids routine, precursor to pci device match


# 1.159 27-Jun-2010 mlarkin

Clear ACPI fixed event status on resume (power buttons/etc) so that some
machines don't immediately resume after suspending to S3.

Tested by kettenis@ and I on 5 or 6 machines, deraadt@ "yeah, looks good"


# 1.158 27-Jun-2010 jordan

Cleaned up acpi_maptable, adds to table list directly


# 1.157 07-Apr-2010 deraadt

If we have done a wsdisplay_suspend(), and ACPI decides it does not want to
suspend, we must roll back by calling wsdisplay_resume() or the virtual
console switching will be locked forever
ok oga


# 1.156 07-Apr-2010 kettenis

Make suspend/resume work on MP machines (running an MP kernel). Joint work
from mlarkin@ and me, with some amd64 fixes thrown in by deraadt@

ok marco@, deraadt@, pirofti@, mlarkin@


# 1.155 31-Mar-2010 kettenis

Bring /dev/apm support in line with apm(4). Make the suspend button send
a suspend request event to apmd(8) instead of suspending immediately. Also
keep track of whether /dev/apm and /dev/apmctl are currently open, such that
we can still suspend immediately if apmd(8) isn't running.

ok deraadt@, marco@, pirofti@, jsing@, oga@


# 1.154 30-Mar-2010 oga

Prevent the apmd/x races for good.

When we hit suspend time, go through all wsdisplays on the system. if
they are in mode MAPPED, but not MODE_DUMBFB then if possible do a full
vt switch to a !mapped vt, and prevent switching back until resume time.
This has to be called from MD code because this involves userland
running so that X can run the vt switch signal handler. This way, any
case where we are using the "poke registers from userland" model, we
will not be on the hardware when we go down, so the kernel can actually
handle thing properly.

Tested on several acpi laptops (by kettenis@ and ian@), x40 (me and
beck@ at LEAST) and zaurus (me). Maybe others, but if so I forgot who at
this time..

Idea from deraadt somewhere over the Faroe Islands (I thought of a
similar thing myself a while ago). Much prompting from him. Ok and
comments miod@


# 1.153 25-Mar-2010 oga

Fix & vs. && in the kqfilter code.

Based on a comparison with the apm code.

ok deraadt@, kettenis@


Revision tags: OPENBSD_4_7_BASE
# 1.152 26-Nov-2009 mlarkin

Visual (and on some machines, audio) feedback while the machine is
resuming via calls to aml _SST method. This also makes the "moon" LED on
thinkpads to blink during resume and cease when resume is completed.

ok pirofti@


# 1.151 26-Nov-2009 deraadt

Toggle into "cold" when we we disable interrupts, because tsleeping
in the AML interpreter and context switching into userland processes
tends to sometimes pop the ACPI trampoline page out of our address
space. Makes lots more machines work.
ok kettenis mlarkin


# 1.150 24-Nov-2009 jsg

in the resume path evaluate _WAK before dealing with device drivers
like the spec says, not after.

ok mlarkin@


# 1.149 24-Nov-2009 deraadt

abort suspends on SMP systems until that gets written; ok mlarkin


# 1.148 23-Nov-2009 mlarkin

pci should return the result of it's children's suspend/resume calls back
to its parent.

handle suspend failure case and unwind if devices have an activate function
that returns failure.

ok deraadt@, kettenis@


# 1.147 23-Nov-2009 mlarkin

acpi* devices need to call their children on suspend/resume.
ok deraadt@


# 1.146 23-Nov-2009 pirofti

Remove ACPI_SLEEP_ENABLED checks.

This enables by default the suspend/resume paths in the kernel.

Okay deraadt@.


# 1.145 23-Nov-2009 deraadt

knf and spacing that are super annoying


# 1.144 23-Nov-2009 mlarkin

Hook up Theo's new children suspend/resume function, as well as more ACPI
suspend/resume glue.

ok deraadt@


# 1.143 26-Oct-2009 deraadt

Do not do strncmp comparisons on dv_xname because longer device names which
look similar could arrive in the future. Instead, compare directly against
dv_cfdata->cf_driver->cd_name
Issue originally spotted by miod


# 1.142 26-Oct-2009 jordan

Verify checksum+address when loading ACPI tables.
Some systems had invalid entries in RSDT/XSDT.
ok marco@


# 1.141 23-Jul-2009 cnst

New aibs(4) driver for ASUSTeK AI Booster (ACPI ATK0110) hardware monitoring,
with sensor state support through limits provided by the ACPI.

Tested on several ASUS motherboards kindly networked by Sam Fourman Jr.

ok deraadt marco jordan


Revision tags: OPENBSD_4_6_BASE
# 1.140 03-Jun-2009 pirofti

Import acpi power resource driver. This is needed for suspend/resume logic.

Okay jordan@ and marco@.


# 1.139 03-Jun-2009 pirofti

Add acpivideo support. This adds brightness support for all laptops
except thinkpads, they will use the acpithinkpad driver. The driver is
also hooked into wsconsole. So brightness can be adjusted via:

$ wsconsctl display.brightness=<percentage>

This is very helpfull on some laptops that have a nasty bios and get two
steps instead of one when pressing the brightness button.

Tested on various dell, fujitsu, acer, samsung and other laptops.

Okay marco@, miod@. Suggestions from kettenis@.
Lots of reviews and help from miod@, thanks!


# 1.138 03-Jun-2009 jordan

Added ejectable bay for IDE cdroms
ok marco@


# 1.137 30-Apr-2009 marco

Oga changed apm ioctls and forgot to grep. Temporary work around by hitting
the sleep button when we get any request. This interface will chnage at
some point.
ok oga


# 1.136 19-Apr-2009 krw

Call acpi_sleep_walk() when powering down just like when entering
any other sleep state. This tells acpi wake devices to not wake up
the box until manual powering up resets their status. Makes my Dell
GX520 stay powered off in response to 'halt -p'.

In snaps for a while.

ok marco@ (several times) deraadt@


# 1.135 17-Apr-2009 pirofti

Add device ranking support for future sleep/wake developments.

okay marco@


# 1.134 11-Mar-2009 jordan

Cleanup.. use aml_evalinteger instead of aml_evalname
ok marco@


# 1.133 10-Mar-2009 jordan

Fix cut/paste error.. call _BFS not _PTS
ok marco@


Revision tags: OPENBSD_4_5_BASE
# 1.132 21-Feb-2009 marco

overzealous cut/paste; found by Vladimir Kirillov <proger@uaoug.org.ua>


# 1.131 20-Feb-2009 marco

Oops didn't mean for Debugger() to remain there.


# 1.130 19-Feb-2009 marco

suspend/resume bits so that we can develop this in tree. This is disabled.
code from mlarkin and me
help from art,toby,jordan and several others
ok jordan, go for it deraadt


# 1.129 10-Feb-2009 jordan

Fix null pointer for shutdown if acpi not enabled
ok marco@


# 1.128 04-Feb-2009 kettenis

Some systems that claim to be ACPI 2.0 apparently don't provide an XSDT
address. Make the code fall back on the RSDT on those systems.

From Joerg Niendorf <p3y80@internode.on.net>. Fixes PR 5986.


# 1.127 17-Jan-2009 krw

A couple of stray malloc/memset -> malloc(M_ZERO), a bit of knf and
lose a couple of unnecessary casts. No functional change.

Feedback from kettenis@

ok kettenis@ marco@


# 1.126 06-Nov-2008 marco

Collapse all sanity checks of hids into one function.

Originaly from jordan


Revision tags: OPENBSD_4_4_BASE
# 1.125 02-Jul-2008 fgsch

add acpivideo. for now almost an empty skel so others can work on
suspend/resume. eventually it will also manage output switching and
brightness where supported. prodded by marco@


# 1.124 11-Jun-2008 marco

Fix $OpenBSD$ for once and for all


# 1.123 08-Jun-2008 deraadt

aaa_dev was being set from an uninitialized variable; ok toby


# 1.122 07-Jun-2008 marco

Kill ACPIVERBOSE. It was never used and is worthless to boot.

comments jmc
ok toby


# 1.121 06-Jun-2008 marco

Shrink acpi some more for floppies and while here re-enable acpi on the
floppies. Fixes crazy sony vaio kettenis has.

tested by deraadt
ok deraadt jordan kettenis


# 1.120 01-Jun-2008 marco

Fix \n nit in SMALL_KERNEL.


# 1.119 01-Jun-2008 marco

Re-enable new parser. Lots of testing by various folks. Fits all media
now too.

ok kettenis deraadt


# 1.118 16-May-2008 dlg

back out jordans new parser.

it didnt have enough oks from the right people, it did break machines
that were previously working, and it bloated the RAMDISKS and install
media beyond the constraints of the media.

requested by deraadt@


# 1.117 14-May-2008 jordan

Adding new ACPI Parser code
Old guts of code still exists, needs to be torn out next
ok marco@
ok brad@


# 1.116 27-Apr-2008 jcs

add acpithinkpad(4), an acpi support driver for ibm/lenovo
thinkpads.

provides functionality that the bios did on older, non-acpi
thinkpads like respond to screen brightness keys and toggle the
bluetooth device on and off.

ok deraadt@ and marco@


# 1.115 24-Apr-2008 jsing

Introduce acpiasus(4), a driver for the ACPI based hotkeys found in many
ASUS laptops (including the ASUS EeePC) - largely based on NetBSD's
asus(4) driver. On the ASUS EeePC this allows us to enable/disable
wireless, change screen brightness and use the volume keys.

ok jsg@, weingart@


# 1.114 12-Apr-2008 kettenis

Get rid of acpi_s5 global variables; simply send SIGUSR2 instead of SUGUSR1
to tell init(8) to power down the machine.

ok krw@


Revision tags: OPENBSD_4_3_BASE
# 1.113 21-Jan-2008 jsg

Check against RESET_REG_SUP in FADT before doing the acpi reset dance
after all, not doing so appears to break more machines than it fixes.

ok marco@


# 1.112 05-Jan-2008 kettenis

Never attach more acpicpu(4) devices than cpu(4) devices. Always attach
the first acpicpu(4) device on non-MULTIPROCESSOR kernels, regardless of its
ACPI processor ID.

ok mikeb@


# 1.111 05-Dec-2007 deraadt

Hang acpi(4) below bios(4) instead of mainbus(4). This lets us move acpi(4)
before pcibios(4), and then.... if acpi(4) attaches, skip pcibios(4) since
it messes the machines up. Fixes claudio's HP dl320 G5, and almost assuredly
others.
ok kettenis


# 1.110 05-Dec-2007 deraadt

repair acpiopen/acpiclose and friends to handle the /dev/apmctl interface,
so that apmd(8) can actually work correctly against acpi(4), and thus,
even do the fancy -C/-A stuff; also tested by jmc


# 1.109 02-Dec-2007 fgsch

fix the trailing space for real now.


# 1.108 02-Dec-2007 fgsch

avoid trailing spaces on output; marco@ ok.


# 1.107 27-Nov-2007 kettenis

Fix off-by-one that made us attach acpicpu0 and acpicpu1 on GENERIC kernels.

ok marco@


# 1.106 25-Nov-2007 jsg

When available and ACPI is enabled use the ACPI reset method
to reboot. This is required for some quirky sis machines
and likely others to reboot.

Tested by many. 'commit early commit often' deraadt@


# 1.105 19-Nov-2007 deraadt

sigh, amd64 still uses that stupid acpi_enabled variable...


# 1.104 19-Nov-2007 deraadt

softc's come initialized to 0, you know...


# 1.103 19-Nov-2007 deraadt

store revision; from jsg


# 1.102 19-Nov-2007 kettenis

Initialize sc_facs earlier.

ok deraadt@, beck@, canacar@


# 1.101 16-Nov-2007 deraadt

fix the bus_space #define nightmare, so that amd64 and i386 are much more
uniform. as a result shared code like acpi needs less #ifdef's
ok marco kettenis


# 1.100 15-Nov-2007 deraadt

remove the #ifdef ACPI_ENABLE. the EC codepath is either safe enough (and
will be improved), or it is unsafe because AML assumes it must be there
ok almost everyone


# 1.99 15-Nov-2007 mikeb

Make acpicpu(4) attach to the existing CPUs only.

Problem reported by form@. Fix idea by kettenis@.
Lots of help from deraadt@. Tested by deraadt@, form@ and me.

ok deraadt marco


# 1.98 14-Nov-2007 deraadt

spacing


# 1.97 12-Nov-2007 deraadt

correct : vs , in attach-time messages


# 1.96 06-Nov-2007 deraadt

all ACPI_ENABLE kernels need acpi_interrupt()


# 1.95 06-Nov-2007 deraadt

move acpi_read_pmreg() and acpi_write_pmreg() out of SMALL_KERNEL so
that acpi ramdisks can compile (obvious solution)


# 1.94 05-Nov-2007 canacar

Establish the acpi interrupt earlier. Fixes problems with some
GENERIC.MP kernels not getting acpi interrupts. This is a
workaround until the real issue with late establishment is fixed.
ok weingart@, beck@, gwk@


# 1.93 03-Nov-2007 jordan

Added support for displaying wakeup devices
ok beck@,weingart@,gwk@


# 1.92 11-Oct-2007 kettenis

When walking the tree yo find _PRT methods, check whether the device is
actually enabled and functioning. Similar to what we do for _INI.

ok weingart@, beck@


# 1.91 08-Oct-2007 krw

More simple memset(,0,) -> M_ZERO changes. In this batch move to
size(*p) as the first malloc() parameter where p is declared locally
and thus easy to check. Add M_ZERO to gpe_table allocation in acpi.c
even though there is no obvious bzero or memset nearby.


# 1.90 13-Sep-2007 mk

Remove unnecessary memset() (see aml_evalnode(9)) and a bit of KNF.

ok toby


# 1.89 13-Sep-2007 weingart

Implement ACPI 6.5.1 spec tree walk for _STA and _INI.
Fixes mk's laptop. No regressions so far (thank you to the
testers).

ok gwk@, mk@, marco@


Revision tags: OPENBSD_4_2_BASE
# 1.88 17-Apr-2007 mk

Now that acpidock no longer hangs machines during boot (at least
according to the reports I've been getting) it should be safe to enable
it again, so remove the #if 0 stuff around it.

ok marco


# 1.87 11-Apr-2007 jordan

Added changes for C-State
ok marco@


# 1.86 26-Mar-2007 marco

Calculate minutes left more accurately.

From giovanni <qgiovanni@gmail.com>


Revision tags: OPENBSD_4_1_BASE
# 1.85 03-Mar-2007 mk

Stop looking for acpidock until we fix that stuff that breaks all pre-60
series ThinkPads with acpidock(4) enabled.


# 1.84 22-Feb-2007 marco

We were getting no interrupts in .MP. There are 2 fixes; one in
acpi_machdep.c and move the interrupt establishment down after ioapic
has been initialized.

ok jordan


# 1.83 21-Feb-2007 kettenis

Make an acpi-enabled GENERIC.MP work on systems that only support PIC mode.

ok marco@


# 1.82 19-Feb-2007 jordan

Added acpi_map_address function for mapping GAS/IO registers
ok marco@


# 1.81 18-Feb-2007 jordan

Added dynamic perfect hash for opcodes
ok marco@


# 1.80 18-Feb-2007 marco

Shave off another K pre compression.
Shuffle SMALL_KERNEL functions around so that we only have a single #ifndef


# 1.79 17-Feb-2007 marco

Add SMALL_KERNEL throught acpi to make it fit on boot media.
ok deraadt


# 1.78 13-Feb-2007 marco

Add smarter _INI code. This helps with some dissapearing devices.

ok kettenis jordan


# 1.77 31-Jan-2007 gwk

Introduce acpi_hasprocfvs thats "ACPI Has Processor Frequency and Voltage
Scalling" a global flag set by acicpu attach to indicate that ACPI is
handling fvs through the _PSS objects. This will fix disappearing
EST/powernow when booting kernels with acpi enabled, as acpicpu isnt
compiled into GENERIC yet. Also in cases where acpicpu is built in and
legacy EST/powernow work but acpi is missing the _PSS object, the legacy
routines will be used.

tested by dim@, and Nick Nauwelaerts <nick AT nauwelaerts.net>
ok marco@, dim@


# 1.76 22-Jan-2007 mk

Actually look for docking stations by looking for _DCK nodes.

ok marco


# 1.75 02-Jan-2007 marco

On some systems EC must be attached as the first device. Move EC from
generic _HID devices to its own routine to force first attach. Bug found
and debugged by joris@

Code mostly written by joris@


# 1.74 26-Dec-2006 marco

Add polling to devices that require it such as acpibat and acpitz.
Use the same tsleep/wakeup mechanism as the interrupt code to keep it non-
concurrent.


# 1.73 21-Dec-2006 deraadt

even more knf love


# 1.72 21-Dec-2006 marco

Remove some leftover acpiec gpe/interrupt code.


# 1.71 20-Dec-2006 deraadt

oops, name already used


# 1.70 20-Dec-2006 deraadt

rename __acpi_enable_gpe(); ok marco


# 1.69 20-Dec-2006 deraadt

acpi_unmap_pmregs() is not used, ok marco


# 1.68 19-Dec-2006 deraadt

simplify some low-level methods substantially (shrinking code); ok marco jordan


# 1.67 06-Dec-2006 mk

malloc(..., M_NOWAIT) with no NULL check is bad. If allocation fails,
complain, clean up, and bail out of attach.

input and ok gwk


# 1.66 28-Nov-2006 jordan

Fixed clearing arg for acpi_init_pic
ok kettenis@


# 1.65 27-Nov-2006 kettenis

Don't spam dmesg.


# 1.64 27-Nov-2006 jordan

Fixes for acpiprt routing
Post-parse fixup for namerefs
ok kettenis@


# 1.63 25-Nov-2006 marco

Don't handle acpiev events inside the gpe handler since this causes deadlocks.

ok jordan@


# 1.62 15-Nov-2006 kettenis

Hook up acpi apic interrupt routing support to generic acpi code.

ok jordan@


# 1.61 02-Nov-2006 marco

Kill acpiec attach function and add its functionality to the generic _HID
device attach

prodded and ok jordan


# 1.60 02-Nov-2006 marco

Fix comments to reflect reality.


# 1.59 19-Oct-2006 marco

Replace ACPI ioctls with APM ioctls. This makes ACPI talk the same protocol
as APM therefore userspace can now retrieve power information in the same
fashion as all other apm arches (zaurus, macppc, i386 etc).

To use force the use of acpi device like so: apmd -f /dev/acpi

ok jordan@


# 1.58 19-Oct-2006 jordan

Fixed GPE debug levels
Fixed ref counter on temporary bufferfields
DSDT parsing fully working on IBM Thinkpad
ok marco@


# 1.57 15-Oct-2006 jordan

Removed old GPE handler code remnants
ok marco@


# 1.56 12-Oct-2006 jordan

Massive ACPI changes
Fixed ACPI leaks
new ACPI API
ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.55 30-Jun-2006 gwk

KNF, No functional change,
"go at it" marco@


# 1.54 30-Jun-2006 jordan

Support for calling Embedded Controller via new GPE interface
ok marco@


# 1.53 30-Jun-2006 jordan

Added new code for GPE handling
Also works with ACPIEC; need to fixup acpiec.c to remove original interrupt handler
ok marco@


# 1.52 30-Jun-2006 jordan

Added offset argument for acpi read/write pmreg routines (new GPE code)
ok marco@


# 1.51 31-May-2006 todd

do not call acpi_powerdown() unless acpi has successfully initialized
ok gwk@ canacar@


# 1.50 29-May-2006 canacar

Add embedded controller support.
ok marco@ deraadt@ testing & ok gwk@


# 1.49 19-May-2006 canacar

Minimal thermal zone driver. Monitors thermal zone temperature,
shuts down the system if the 'critical temperature' is reached.
ok marco@


# 1.48 11-Apr-2006 marco

KNF.


# 1.47 11-Apr-2006 gwk

Add acpi_resume(struct acpi_softc *sc), which calls some AML methods,
detected at attach time, currently unused.

ok marco@


# 1.46 11-Apr-2006 gwk

Discover some ACPI sleep related methods, and call them at the
appropriate time in acpi_enter_sleep_state().

ok marco@


Revision tags: OPENBSD_3_9_BASE
# 1.45 26-Feb-2006 marco

Add acpicpu as a device.

ok jordan@


# 1.44 23-Feb-2006 marco

remove dead function.

ok jordan@


# 1.43 22-Feb-2006 marco

Print out all tables it found during autoconf. We'll need this data in
the future.


# 1.42 22-Feb-2006 marco

Make dmesg less verbose.

ok jordan.


# 1.41 22-Feb-2006 jordan

Added parsing of SSDT tables
ok marco@


# 1.40 22-Feb-2006 jordan

Added new aml_register to notify by device id
Moved powerdown code to button device
ok marco@


# 1.39 21-Feb-2006 marco

Readjust debug levels that crept in too low over the last few days.
KNF


# 1.38 21-Feb-2006 marco

Rig the power button so that the machine shuts down correctly.


# 1.37 20-Feb-2006 marco

Add acpibtn device. Remains disabled for now.


# 1.36 19-Feb-2006 jordan

Added code for aml_notify GPE notification callback
ok marco@


# 1.35 19-Feb-2006 grange

Fix acpi_enter_sleep_state() by replacing current ugly hack
with mostly correct sleeping state transition code.

ok brad@ marco@


# 1.34 19-Feb-2006 marco

jordan:
Enable interrupts.
Added peripheral code to make this work.

marco:
Replace softintr and other types of interrupts with a handler with context.
Fixed interrupt magic for GPEs.
Some KNF

This is work done by jordan and I but has to be a single commit.


# 1.33 17-Feb-2006 marco

Remove earlier introduced delays. We really only need them during autconf.

ok jordan@


# 1.32 17-Feb-2006 marco

Add a generic delay function as per the ACPI spec.

from jordan@


# 1.31 17-Feb-2006 marco

Use tsleep instead of delay when !cold.


# 1.30 17-Feb-2006 marco

Argh, we need delays between reads/writes. The backend reads (i2c most likely)
are so slow that values come back as 0 sometimes. We have to do this
despite the spec saying we shouldnt have to. Guess having a faster
parser than others can hurt sometimes.

Idea from jordan.


# 1.29 17-Feb-2006 jordan

Fixed code for handling _INI
Rearranged debug statements
ok marco@


# 1.28 16-Feb-2006 jordan

Added stub for calling _INI
Fixed aml_dowhile for NULL return
ok marco@


# 1.27 16-Feb-2006 jordan

Fixed parser/evaluator to retrieve battery info+state
Cleanup of parser
Dell laptops return battery status
ok marco@


# 1.26 06-Feb-2006 brad

fix a typo so this will compile on amd64.


# 1.25 04-Feb-2006 marco

When ACPI_ENABLED wasn't set but the devices were it had an unknown variable.

Pointed out by ian@.


# 1.24 03-Feb-2006 jordan

Rewrote aml parser yet again...
Fixed bus_space_map for i386/amd64
Added _BST and _BIF to acpibat to get status/info
ok marco@


# 1.23 20-Jan-2006 jordan

More evaluator changes
Removed old evaluator code
Fixed _HID value
ok marco@


# 1.22 19-Jan-2006 jordan

Added support for read/write of PCI Config space
Help jason@
ok marco@


# 1.21 18-Jan-2006 jordan

Added new evaluation routines
Added helper methods for buffer/field evaluation
Fixed broken indentation on dsdt.c

ok marco@


# 1.20 17-Jan-2006 jordan

Major overhaul of the aml parser. Evaluater temporarily disabled.

ok marco@


# 1.19 06-Jan-2006 grange

Check for ACPI control availability only if ACPI_ENABLE defined.


# 1.18 06-Jan-2006 grange

Informative error messages on attach, prettier dmesg.


# 1.17 06-Jan-2006 grange

printf -> dnprintf conversion for debug messages.


# 1.16 05-Jan-2006 grange

Find SLP_TYPx values and store them for future use in
acpi_enter_sleep_state().


# 1.15 05-Jan-2006 grange

Random KNF and cleanup


# 1.14 05-Jan-2006 grange

Provide symbolic names for the acpi sleeping states and use it.


# 1.13 28-Dec-2005 marco

Moved DEVNAME to a acpidev.h

ok jordan@


# 1.12 18-Dec-2005 sturm

add back comment about wrong code

ok marco


# 1.11 16-Dec-2005 marco

Remove test for kbd_reset. Prompted deraadt@, ok jordan@


# 1.10 16-Dec-2005 jordan

Added hooks for powerdown/power button press


# 1.9 16-Dec-2005 jordan

Cleaned up common power management register i/o
Added enter sleep state ioctl
Hooked power button interrupt
ok marco@


# 1.8 16-Dec-2005 marco

Remove autoconf chatter


# 1.7 16-Dec-2005 jordan

Moved DSDT AML parsing into parent ACPI driver
ok marco@


# 1.6 14-Dec-2005 marco

Add AC device glue.


# 1.5 14-Dec-2005 marco

Adjust debug printing levels to something more sane.


# 1.4 13-Dec-2005 marco

Add glue to attach devices to apci.


# 1.3 07-Dec-2005 marco

KNF


Revision tags: OPENBSD_3_8_BASE
# 1.2 10-Jul-2005 grange

Spacing cleanup.


# 1.1 02-Jun-2005 tholo

Start on a basic ACPI framework -- does not do much more than read out the
ACPI tables into kernel memory and attach ACPI and HPET timers currently.

In order to test this code, enabling the devices in GENERIC as well as
the ACPI_ENABLE option is needed. This code does not do any thermal
control yet, so this should be done with care depending on the platform.

In the tree so more people can contribute to making this more fully
featured.

Ok niklas@ grange@ tedu@


# 1.382 14-Apr-2020 kettenis

Implement acpi_getprop(), the ACPI equivalent of OF_getprop().

ok patrick@


# 1.381 12-Apr-2020 kettenis

Add support for the _CCA method. This method indicates whether DMA is
cache-coherent or not. To implement this, acpi(4) gets two bus_dma tags
and passes the appropriate one when attaching devices based on _CCA.
On i386/amd64, where for all practical purpose DMA is always cache-coherent,
the two tags are the same. But on arm64 they are distinct.

ok patrick@


# 1.380 07-Apr-2020 visa

Abstract the head of knote lists. This allows extending the lists,
for example, with locking assertions.

OK mpi@, anton@


# 1.379 03-Apr-2020 mpi

Fix incoherencies of errors returned by various *kqfilter routines.

EOPNOTSUPP is returned when there's no "filterops" for a given fd.
EINVAL when the requested filter isn't supported by the "filterops".
ENXIO means the underlying device is no longer valid/present.

ok visa@


# 1.378 20-Feb-2020 visa

Replace field f_isfd with field f_flags in struct filterops to allow
adding more filter properties without cluttering the struct.

OK mpi@, anton@


# 1.377 31-Dec-2019 visa

Use C99 designated initializers with struct filterops. In addition,
make the structs const so that the data are put in .rodata.

OK mpi@, deraadt@, anton@, bluhm@


# 1.376 31-Dec-2019 mpi

Convert infinite sleeps to {m,t}sleep_nsec(9).

ok kn@


# 1.375 31-Dec-2019 jsg

include sys/sensors.h for sensor use
don't depend on indirect inclusion from acpidev.h


Revision tags: OPENBSD_6_6_BASE
# 1.374 07-Sep-2019 kettenis

Revert acpipci(4) changes. Too many systems are broken. We'll try again
after 6.6 as been released. The acpireg.h change stays behind.


# 1.373 28-Aug-2019 kettenis

Use ACPI information to attach PCI busses like we do on arm64. There are a
few additional quirks though, and attaching the PCI busses is delayed to
replicate the existing code more closely. That may be changed in the
future. Also tweak how we handle MSI support and respect to ACPI flag
that says we shouldn't attempt to use MSIs.

Some fallout is expected.

ok patrick@


# 1.372 27-Aug-2019 deraadt

On amd64 the bootup and resume paths for SP/BP/SP were not performing
msr/pat, clock-startup, firmware upgrade, fpu initialization, and cpu
soft-state initialization in the same order. That kind of chaos is
crazy and fragile. Improve the acpi resume call sequence and unify order
of the operations.


# 1.371 02-Jul-2019 jcs

acpi_set_gpehandler: turn edge arg into flags, add support for
passing GPE_DIRECT which directly executes the supplied callback and
clears a level interrupt rather than going through acpi_taskq which
can be slow when other driver callbacks are in the queue.

This should not be used for GPE handlers that need to do anything
ACPI-related.

ok kettenis


# 1.370 10-Jun-2019 kettenis

Print proper ACPI version number.

ok mlarkin@


# 1.369 08-Jun-2019 kettenis

When establishing the mapping between ACPI device nodes and PCI devices,
skip PCI host bridges and devices that aren't there.

ok patrick@


# 1.368 07-Jun-2019 kettenis

Introduce acpi_getsta() and use it to eliminate some duplicated code.

ok patrick@


# 1.367 12-May-2019 kettenis

Make sure the extended PM register descriptions actually exist before
using them. Fixes machines with ancient ACPI 1.0 (such as recent
hypervisors).

ok deraadt@


# 1.366 12-May-2019 kettenis

Add support for mapping ACPI PM registers using the "extended" gasio
representation from the FADT. Mostly fixes the Lanner NCA-1510.

ok lteo@, mlarkin@, deraadt@


# 1.365 11-May-2019 lteo

Fix incorrect error message.

ok kettenis@


# 1.364 08-May-2019 tedu

free size. the allocation is the header plus the length.


Revision tags: OPENBSD_6_5_BASE
# 1.363 04-Apr-2019 kettenis

Avoid potential null-pointer dereference. Found by Kent R. Spillner.

ok deraadt@, jsg@


# 1.362 20-Jan-2019 jsg

fix SMALL_KERNEL build after changes in rev 1.361
reported by naddy@ ok deraadt@


# 1.361 19-Jan-2019 tedu

Add a pwraction sysctl that controls what the power button does on acpi.
By default, nothing changes -- shutdown is initiated. But allows turning
power button into a sleep button if desired.
(grudging) ok from a few parties


# 1.360 26-Oct-2018 kettenis

Add acpipci(4) on amd64. For now this only calls the PCI-specific _OSC
method to let the ACPI implementation know what features we support.


Revision tags: OPENBSD_6_4_BASE
# 1.359 30-Aug-2018 kettenis

Move softbutton check before the check that skips attaching certain acpi
devices. Fixes regression caused by matching on _CID in addition to
matching on _HID.

ok matthieu@, mlarkin@


# 1.358 25-Aug-2018 kettenis

Match ACPI devices based on _CID if no match for _HID is found.

ok mpi@


# 1.357 19-Aug-2018 kettenis

Add support for multiple PCI segments. Only really implemented for arm64
for now as amd64/i386 firmware still caters for legacy OSes that only
support a single PCI segment.

ok patrick@


# 1.356 03-Aug-2018 kettenis

Let ahci(4) match on _CLS instead of _HID when attaching at acpi(4). Avoids
having to add many more _HID entries to the match table.

ok deraadt@, mlarkin@


# 1.355 10-Jul-2018 kettenis

Make legacy interrupts work in acpipci(4).

ok patrick@


# 1.354 05-Jul-2018 kettenis

Add acpipci(4), a driver that supports generic ECAM-compatible PCI host
bridges based on information provided by ACPI.

ok mlarkin@


# 1.353 04-Jul-2018 kettenis

Properly pass around the PCI "chipset tag" in acpi(4) and refactor
acpimcfg(4) to call an MD initialization functions that sets up a tag for
PCI ECAM.

ok guenther@, mlarkin@, krw@


# 1.352 01-Jul-2018 kettenis

Move acpi(4) attach glue into acpi_machdep.c.

ok guenther@, deraadt@


# 1.351 01-Jul-2018 kettenis

Add support for _DSD Device Properties.

ok mlarkin@


# 1.350 30-Jun-2018 kettenis

Add intr_enable() function, intended for MI use to amd64 and i386 and use
this in the acpi(4) suspend/resume code paths.

ok deraadt@


# 1.349 29-Jun-2018 kettenis

Use <stdint.h> types.

ok mpi@, jung@, krw@, deraadt@


# 1.348 29-Jun-2018 mlarkin

ACPI: Allow (with warning) GPE handler reassignment, instead of returning
a failure code.

ok kettenis


# 1.347 26-Jun-2018 mlarkin

knf


# 1.346 26-Jun-2018 kettenis

Let acpi(4) pass down the bus dma tag.

ok deraadt@


# 1.345 25-Jun-2018 kettenis

Reorganize acpi(4) code a bit in preparation for upcoming arm64 support.

ok deraadt@


# 1.344 20-May-2018 kettenis

Wake up the acpi thread after scheduling a gpio event.

ok mpi@


# 1.343 17-May-2018 kettenis

Sprinkle some #ifndef SMALL_KERNEL such that RAMDISK kernels cuild again.

pointed out by stsp@


# 1.342 17-May-2018 kettenis

Implement GenericSerialBus OpRegion support.

ok mlarkin@


# 1.341 27-Mar-2018 kettenis

Add acpicmos(4), a driver that implements SystemCMOS OperationRegion
access support. This fixes machines where the AML doesn't check whether
support for this OperationRegion type has been registered by the OS.

ok mlarkin@


Revision tags: OPENBSD_6_3_BASE
# 1.340 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.339 10-Feb-2018 deraadt

Syncronize filesystems to disk when suspending. Each mountpoint's vnodes
are pushed to disk. Dangling vnodes (unlinked files still in use) and
vnodes undergoing change by long-running syscalls are identified -- and
such filesystems are marked dirty on-disk while we are suspended (in case
power is lost, a fsck will be required). Filesystems without dangling or
busy vnodes are marked clean, resulting in faster boots following
"battery died" circumstances.
Tested by numerous developers, thanks for the feedback.


# 1.338 08-Feb-2018 deraadt

After flushing disks, try again to release all memory. We don't need to
save dirty memory to the hibernate space.


# 1.337 08-Feb-2018 deraadt

Toss all releaseable memory, because fragmentation can get in the way
of allocating the hibernate playpen.


# 1.336 08-Feb-2018 deraadt

sr_quiesce() is a new approach for ensuring that softraid drains
output to the disks.

This is part of a larger suspend/resume filesystem-safety diff, which
has been worked on for a couple of months already. Tests by job, krw,
beck, benno, and others. Sometimes even by snapshot users...


# 1.335 29-Nov-2017 kettenis

Add support for GPIO-signaled events.

ok mlarkin@


# 1.334 16-Nov-2017 jcs

Add PCI attachment for dwiic(4) needed by Intel 100 Series machines

ok kettenis


# 1.333 14-Nov-2017 kettenis

Add a few more PNP IDs to the skip lists.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.332 17-Aug-2017 stsp

Make dwiic(4) attach its ACPI parent dependencies.
This change makes the Asus E200HA keyboard work.

Original analysis and patch by Cesare Gargano
ok kettenis@


# 1.331 09-Aug-2017 dcoppa

Add "PNP0303" (8042 PS/2 Controller) to acpi_isa_hids[]

ok kettenis@


# 1.330 22-Jul-2017 jcs

acpibat: look for _BIX first before falling back on _BIF

ACPI 4.0 deprecated _BIF for battery status so some newer machines
are exporting _BIX which extends _BIF's package with a few extra
fields. When using _BIX, export the new discharge cycle count as a
sensor.

feedback from kettenis and anton
ok deraadt


# 1.329 20-Jul-2017 kettenis

Start a list of isa(4) device names such that we can avoid printing them
as "not configured".

ok mikeb@, millert@, deraadt@


# 1.328 08-Apr-2017 deraadt

Sensors are run as callbacks inside tasks. During suspend a sensor
could be running inside a driver that will be force-detached, or due
to tsleep end up disrupting the softstate/hardstate contract. At
suspend time, quisce all these callbacks by waiting for completion.
This issue has never been observed for real, but may be implicated in
suspend/resume failures.
ok kettenis guenther mlarkin


# 1.327 08-Apr-2017 deraadt

Two prototypes for acpi_maptable; merge to one place


Revision tags: OPENBSD_6_1_BASE
# 1.326 27-Mar-2017 deraadt

Now that hibernate_alloc() only has clean success/failure, don't
need to call hibernate_free() to clean up a partial mess.
ok mlarkin kettenis


# 1.325 13-Mar-2017 deraadt

allow ramdisks to build; ok jcs


# 1.324 12-Mar-2017 jcs

try to attach acpisbs first and if successful, don't attach acpibat


# 1.323 02-Mar-2017 natano

Add a new sysctl machdep.lidaction. The sysctl works as follows:

machdep.lidaction=0 # do nothing
machdep.lidaction=1 # suspend
machdep.lidaction=2 # hibernate

lidsuspend is just an alias for lidaction, so if you change one, the
other one will have the same value. The plan is to remove
machdep.lidsuspend eventually when people have upgraded their
/ets/sysctl.conf.

discussed with deraadt, who came up with the new MIB name
no objections mlarkin
ok stsp halex jcs


# 1.322 28-Feb-2017 natano

Untangle abstract sleep modes from ACPI sleep states. This paves the way
for further work in the area.

idea and ok deraadt
ok mlarkin


# 1.321 22-Feb-2017 jcs

add acpisbs, an acpi smart battery subsystem driver reading data
over smbus

currently disabled because it conflicts with acpibat

ok deraadt, kettenis


# 1.320 14-Jan-2017 kettenis

Revert aml_rdpciaddr changes; breaks several machines that were working
before.


# 1.319 11-Jan-2017 jsg

Increase the size of the buffers used for _HID and _CID strings to be
able to fit "Hyper_V_Gen_Counter_V1".

ok mlarkin@ deraadt@


# 1.318 08-Jan-2017 kettenis

Make aml_rdpciaddr() work for for devices behind bridges. The code now
uses the mapping between AML nodes and PCI devices that we establish.
Because _INIT methods may end up calling aml_rdpciaddr(), make sure we
create that mapping early. Also handle devices that aren't actually present.
These devices are now included in the mapping and reads will return an
all-ones patterm whereas writes are a no-op..

ok mlarkin@


# 1.317 25-Oct-2016 pirofti

Add the resource index to aml_parse_resource() callback function.

This is needed by an upcoming acpiec commit that handles machines breaking
the current ACPI specifications.

Change suggested by and ok kettenis@, guenther@


# 1.316 18-Sep-2016 guenther

If the APIC table (MADT) isn't present then ci_acpi_proc_id won't be set
when acpicpu tries to attach, leading to a NULL dereference. The ACPI
processor object on the Dells where this was reported is bogus and useless
anyway so apply the same test when deciding whether to attach to processor
objects as acpicpu would when matching them to cpus.

Problem reported by Eivind Eide (xenofil (at) gmail.com) and
Johan Huldtgren (johan+openbsd-bugs (at) huldtgren.com).
Testing by Eivind Eide.
ok kettenis@ mlarkin@


# 1.315 03-Sep-2016 naddy

Write the system time back to the RTC every 30 minutes.
This fixes the problem that long-running machines which were not
shut down properly would reboot with a badly offset system time.

hints and ok kettenis@


# 1.314 31-Aug-2016 mlarkin

Don't put a device in the wake device list if _STA says it's not there.

Fixes at least one machine that woke up from sleep / poweroff immediately.

ok kettenis@, tested by kettenis and martijn@


# 1.313 28-Jul-2016 kettenis

Store the acpi processor ID/UID in struct cpu_info, and use it to attach
acpicpu(4) drivers to the right cpu(4).

ok mlarkin@, guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.312 10-Jun-2016 kettenis

The power management timer has been made optional in ACPI 5.0A, so don't
attempt to attach acpitimer(4) if the timer isn't present.

Pointed out by jcs@

ok jcs@


# 1.311 08-May-2016 kettenis

Make sure devices listed by a device's _DEP method are attached before
we attach the device itself. The _DEP methode, introduced in ACPI 5.0,
is a hint that the device in question depends on OpRegion support from
the devices listed by _DEP, so we have to order them the proper way.
To prevent us from attaching those devices again when we encounter them
later walking down the device tree, keep track of our attempts to
attach a device driver to them using aflag in the aml_node structure.

ok guenther@


# 1.310 25-Apr-2016 pirofti

We attach acpihpet(4) and acpiec(4) without using the HID, so do not
report it in the dmesg.

OK deraadt@, mlarkin@.


# 1.309 02-Apr-2016 kettenis

Reduce dmesg spam by knocking out some acpi hardware IDs that don't need a
driver.

ok mpi@


# 1.308 29-Mar-2016 kettenis

Check if a device is present (using _STA) in acpi_foundhid. This prevents
us from reporting devices as "not configured" that aren't actually present,
and allows us to remove duplicated code from several drivers.

ok deraadt@, ok jsg@


# 1.307 29-Mar-2016 kettenis

Implement a cleaner way to prevent acpivout(4) from attaching if we have
a platform-specific method of controlling the backlight.

ok millert@, mlarkin@, jung@


# 1.306 28-Mar-2016 kettenis

Adding each and every new acpi device driver to acpi_foundhid() is
gettinga bit of the burden. And it really isn't how our autoconf
framework is supposed to work for busses that can be enumerated. All
the drivers already check for a matching _HID in their attach
function. So we can just drop the checks in acpi_foundhid() and let
the drivers sort themselves out.

Print a "not configured" line for devices for which we don't attach adriver.
This may be a bit much as there are devices for which we will not have a
driver. This will be removed if it causes too much dmesg spam.

ok deraadt@, mlarkin@


Revision tags: OPENBSD_5_9_BASE
# 1.305 17-Jan-2016 jsg

Take acpi_foundhid() outside of #ifndef SMALL_KERNEL so the ramdisk
kernel can find and attach dwiic(4) and sdhc(4).


# 1.304 17-Jan-2016 jsg

remove an unused global variable


# 1.303 14-Jan-2016 kettenis

Attach dwiic(4) on the Intel Bay Trail i2c controllers as well.


# 1.302 12-Jan-2016 kettenis

Improve emulation of PM registers on Hardware-redcued ACPI by emulating
SPL_TYPx, SLP_EN and WAK_STS using the new SLEEP_STATUS_REG and
SLEEP_CONTROL_REG registers. Makes the transition into S5 (powerdown) work
on the ASUS X205TA.

ok deraadt@, mlarkin@


# 1.301 12-Jan-2016 jcs

Add dwiic, a driver for the Synopsys DesignWare i2c controller found
on the Samsung ATIV Book 9 laptop. This initial version only
supports ACPI config/attachment.

Add ihidev, a HID-over-i2c driver largely based on uhidev. dwiic
handles attaching ihidev devices found in ACPI.

Add ims, a HID-over-i2c mouse/trackpad driver to get basic cursor
and button functionality from HID-compliant i2c trackpads.

ok kettenis deraadt


# 1.300 11-Jan-2016 kettenis

Make sdhc(4) attach to hardware IDs "80860F14" and "PNP0FFF".


# 1.299 09-Jan-2016 kettenis

Initial support for Hardware-Reduced ACPI. This skips the legacy mode checks
by emulating the PM_CNT register such that SCI_EN is always enabled. It also
skip attaching acpitimer(4) since the relevant registers are no longer there.

ok deraadt@, mlarkin@


# 1.298 06-Jan-2016 kettenis

Follow the spec and check the SCI_EN bit to see if ACPI has already
been enabled and don't bother enabling it again if it is. This should fix
systems that don't implement legacy mode and therefore set ACPI_ENABLE and
ACPI_DISABLE to zero.

ok mlarkin@, yasuoka@


# 1.297 23-Nov-2015 reyk

Back out the CRITICAL change, turn it back from 10% to 25%. Some
people with old/broken batteries didn't like this change, and the
underlying heuristics are wrong.


# 1.296 22-Nov-2015 reyk

The "CRITICAL" APM battery level, that is exported/faked by the ACPI
stack, is currently set to 25%. With 25% battery left, a typical
modern laptop will have at least one hour runtime left. It is an
arbitrary value, and other APM power layers in the kernel set CRITICAL
to something like 5% or 10%, so set it to 10% for now.

OK mlarkin@


# 1.295 28-Sep-2015 deraadt

In low-level suspend routines, set cold=2. In tsleep(), use this to
spit out a ddb trace to console. This should allow us to find suspend
or resume routines which break the rules. It depends on the console
output function being non-sleeping.... but that's another codepath which
should try to be safe when cold is set.
ok kettenis


# 1.294 06-Sep-2015 deraadt

clarify malloc arguments (base + length...)


# 1.293 28-Aug-2015 deraadt

fairly simple sizes for free(); ok tedu


# 1.292 20-Aug-2015 kettenis

Respect the access size when reading or writing to pci config space.
This version also makes sure writes are properly aligned, preventing
the panics seen with the previous incarnation of this change.

Fixes battery status passthrough in vmware, brightness keys on my x220
and who knows what else.

ok mlarkin@, deraadt@


Revision tags: OPENBSD_5_8_BASE
# 1.291 04-Aug-2015 deraadt

If we are setting ACPI_PM1_BM_RLD do it only once, and remember to
repeat this action at resume time.
ok mlarkin guenther


# 1.290 29-Jul-2015 deraadt

Something subtle in previous revision is locking up suspend or booting
on some laptops. Mailing lists threads mention x220 and Asus machines.
Reverting for now.


# 1.289 21-Jul-2015 kettenis

iRespect the access size when reading or writing to pci config space.
Fixes battery status passthrough in vmware and who knows what else.

Tested by many.

ok mlarkin@, deraadt@


# 1.288 17-Jul-2015 mlarkin

fix comments


# 1.287 26-Mar-2015 jsg

Use the 0x10 extended capability to determine if the trackstick
buttons are wired to the trackpad and need to be re-routed to the
trackstick. Without this change the buttons on 2015 thinkpads
get picked up as extended buttons that show up as scroll up/down.

Remove the X1 Carbon 2015 (LEN0048) and X250 (LEN0046) from the
top button area/soft buttons quirks list. Also avoid using the
quirk list entirely if the capability bit is set.

Based on work from jcs@ with feedback from mpi@
ok mpi@ jcs@


# 1.286 20-Mar-2015 kettenis

The ACPI standard says that a PCI function number of FFFF can be used to refer
to all functions on a device. This makes no sense in the context of accessing
PCI config space. Nevertheless stsp@ has a machine with AML that tries to
do config space reads from a device where _ADR encodes function number FFFF.
This is almost certainly buggy AML, but since Linux (and presumably Windows)
boot just fine on this machine we need to work around this. Linux effectively
treats function FFFF as function 7, which isn't present on this machine.
Instead we simulate a read from a non-existent device by returning a bit
pattern of all ones. Writes to function FFFF will still panic.

ok mlarkin@


# 1.285 16-Mar-2015 deraadt

Reduce use of SMALL_KERNEL in acpi, bringing acpiec driver and more gpe
handling into RAMDISK. This is now possible because the install media
has ample room. The goal is to reduce special cases where we may be
skipping (unknown) important operations...
ok mlarkin kettenis


# 1.284 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.283 07-Feb-2015 deraadt

New framework that allows hibernate to pass in entropy from it's fresh
boot.
ok mlarkin


# 1.282 06-Feb-2015 mlarkin

Fix a hibernate crash on some machines due to unmapping a page that
may not have been mapped previously (in the failure to hibernate case).

Also ensure that the lowmem ptp is mapped in all cases (not just MP).

ok kettenis


# 1.281 17-Jan-2015 deraadt

resume_randomness() before spinning up other cpus...


# 1.280 15-Jan-2015 jsg

Define a new wscons mouse type for Synaptics clickpad devices that lack
physical buttons. This will be used if the acpi pnpid for the mouse
matches a list provided by Synaptics found in Linux.

Combined with the xenocara changes this will remove the need for an xorg
config file for the x240/t440/t540 generation of thinkpads.

ok matthieu@ shadchin@ kettenis@


# 1.279 11-Jan-2015 kettenis

Tolerate checksum errors in ACPI tables (but complain about them). Apparently
there are machines out there with broken BIOSen with such checksum errors in
thier RSDT tables. Windows runs fine on these machines, so it probably does
not check. Linux ignores checksum errors as well (and prints a warning).

ok deraadt@, krw@


# 1.278 18-Dec-2014 deraadt

suspend and resume the RNG (at what looks like the right spots)
ok reyk djm


# 1.277 09-Dec-2014 doug

Replace some malloc(n*size,...) calls with mallocarray().

ok tedu@ deraadt@


# 1.276 06-Dec-2014 mpi

If "machdep.lidsuspend" is not set, do not go back to sleep if the lid
is still closed when resuming. This allows docked laptop to be resumed
without opening them.

ok mlarkin@


# 1.275 23-Nov-2014 mlarkin

If a machine has no _LIDs, then none can be open. This diff fixes a problem
where machines without any _LIDs were going back to sleep after hibernate
due to the recent suspend-if-lid-closed-on-resume diff. Noticed on qemu,
which doesn't have any _LIDs.

discussed with deraadt


# 1.274 18-Nov-2014 krw

Nuke yet more obvious #include duplications.

ok miod@


# 1.273 08-Nov-2014 mlarkin

If resuming from sleep (zzz/ZZZ) and the lid is still closed, go back to
sleep. This prevents accidental lid flex or slight opening in a backpack
from waking the machine up and leaving it resumed (powered on).

ok deraadt@


# 1.272 26-Sep-2014 kettenis

Rework piglet and pig allocation. Currently the piglet gets allocated
deep down in the suspend path, where it is really hard to recover from
allocation failure. So allocate the piglet early on in the suspend path.
Also change the piglet and piglet allocation functions to use km_alloc(9)
instead of doing pmemrange magic. This removes a bunch of code which, in the
case of the piglet allocation, is broken since it results in a NULL pointer
dereference. Also switch the piglet allocation to not wait. If we can't
allocate 16MB of phys contig memory on a halfway modern machine we're almost
certainly under a lot of memory pressure and we're better off not trying to
hibernate anyway.

ok mlarkin@


# 1.271 25-Sep-2014 kettenis

Bring back rev 1.249. Now that mp_setperf() has been fixed, it should be
safe to restore perflevel when resuming. Should fix the issues people
see with the processor running at full speed heating up the machine while
the apm command reports that the CPUs are running at a low speed.

ok dcoppa@, deraadt@


# 1.270 23-Sep-2014 kettenis

Release the acpi lock around calls to wsdisplay_suspend() and
wsdisplay_resume(). The X server may adjust the display brightness just
before/after the VT switch when suspending/resuming. In fact, it may
already be blocked adjusting the display brightness for some reason
unrelated to the suspend request.

The VT switch request will eventually time out, but the delay is annoying
and the failed VT switch will lead to problems after resume.

Needed for a future update of the xf86-video-intel driver.

ok mlarkin@


# 1.269 19-Sep-2014 kettenis

Use config_suspend_all(9).


# 1.268 08-Sep-2014 kettenis

Clear the PME Status bit on PCI devices in response to Device Wake events.
Fixes the interrupt storm on the 2009 Mac mini with WOL enabled on its
nfe(4) interface.

ok mlarkin@


Revision tags: OPENBSD_5_6_BASE
# 1.267 20-Jul-2014 mlarkin

Support hibernating to softraid crypto volumes.

much help and ok from deraadt@


# 1.266 16-Jul-2014 mlarkin

Reenable hibernate RLE support and flush+zero all memory after unpack.


# 1.265 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.264 12-Jul-2014 kettenis

Add a function to drop all clean pages on the page daemon queues and call
it when we hibernate.

ok mlarkin@, miod@, deraadt@


# 1.263 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.262 11-Jul-2014 mlarkin

Flush the buffercache to 16MB on hibernate and restore its previous max
size (kern.bufcachepercent) on resume, for better hibernate performance.

ok beck@


# 1.261 10-Jul-2014 blambert

remove unneeded header

ok mlarkin@


# 1.260 01-Jul-2014 kettenis

Adding an additonal delay after each access to SystemMemory or SystemIO
during autoconf makes no sense.

ok deraadt@


# 1.259 23-Jun-2014 kettenis

SystemMemory is used to access memory mapped registers on some machines, so we
must use the bus_space(9) API to access it instead of memcpy(9). Also make
sure we properly align access to fields that need more than byte alignment.

ok guenther@


# 1.258 21-May-2014 mlarkin

Fix invalid / missing format specifiers in several dnprintf statements
(used only when ACPI_DEBUG is enabled)

From a diff on tech@ by Fabian Raetz, with a minor change by me, tested
on i386 and amd64 with ACPI_DEBUG enabled and disabled.


# 1.257 25-Apr-2014 mlarkin

Defer the free of the hibernate structures until later, when it is safe to
do so.

Freeing the hibernate structures too early leads to inadvertent calls into
splx when we aren't ready for them yet. Found when trapping splx calls in
the resume path.

discussed with deraadt@ at m2k14


# 1.256 13-Apr-2014 mlarkin

Fix a badly worded debug message that implied the entire machine was going
to sleep, instead of just the acpi thread going to sleep.


# 1.255 31-Mar-2014 mpi

Change the order of operations during a suspend/resume cycle and
call bufq_quiesce() after executing the DVACT_QUIESCE handlers.

This should be safe since no disk nor controller drivers have such
handler but it will allow us to detach sd(4) devices attached to a
USB bus.

Another benefit pointed out by kettenis@ is that drivers that need
to read a firmware from the disk should be able to do it at resume
time in a DVACT_WAKEUP handler.

ok kettenis@, deraadt@


# 1.254 13-Mar-2014 dlg

get rid of the assumption that the head of the alldevs list is the
"mainbus" device. this breaks when mpath is enabled because it
attaches before mainbus and therefore takes the head position.

have autoconf provide device_mainbus() which looks up mainbus_cd,
and use that instead.

discussed with deraadt who just wants mpath stuff to move forward
despite there being many ways to shine this particular turd.


Revision tags: OPENBSD_5_5_BASE
# 1.253 21-Feb-2014 deraadt

Do the lid control further on in the resume sequence, since this is
AML, and will do WAITOK operations. While cold, that is a bad idea.
We seem to be safe with respect to the spec, I do not see any event
debounce issues here.
ok kettenis, tested by many


# 1.252 20-Jan-2014 claudio

Instead of checking sc->sc_revision <= 1 check that the
fadt->hdr_revision <= 1. This is a better check since it checks
the version of the proper descriptor. This allows the alix APU
to reboot since those boards have currently a wrong sc->sc_revision.
Committing this now so that it can be tested easier.
With and OK jsg@ also fine by derradt@


# 1.251 24-Dec-2013 kettenis

If the FADT has its SMI_CMD set to zero, assume we're only ACPI-only hardware
and don't need to disable SMI ownership of the ACPI hardware registers.

ok mlarkin@


# 1.250 23-Dec-2013 kettenis

Revert previous commit. It makes resume hang on some MP systems running
GENERIC.MP.


# 1.249 16-Dec-2013 kettenis

Restore hw.setperf upon resume like we do for apm(4) on i386. Seems we need
to do this fairly late in the resume to avoid hangs.

ok deraadt@, mlarkin@


# 1.248 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


# 1.247 06-Nov-2013 mpi

Add support for Power Resources for Dx states and the necessary hook
for PCI devices. This hook should be called twice, before and after
changing the power state of a PCI device.

Before setting the device to the new state, the ACPI layer will notify
every power resources linked to the device for that state and make sure
they are turned "_ON". After changing the state of the device, it will
decrement the reference of every power resources linked to that device
for the old state and turn them "_OFF" if they are no longer referenced.

This fixes the no-USB after resume problem seen on various ThinkPad,
problem initialy diagnosed with Alexander Polakov.

ok kettenis@, deraadt@


Revision tags: OPENBSD_5_4_BASE
# 1.246 01-Jun-2013 mlarkin

Stop printing ACPI wakeup devices in dmesg after the 16th wakeup device.

Helps with VMware VMs that report hundreds of wakeup devices.

ok reyk@, deraadt@


# 1.245 31-May-2013 bluhm

Fix the build for a kernel without wd and pciide in its config.
OK deraadt@ tedu@


# 1.244 30-May-2013 deraadt

Enforce ca_activate tree-walks over the entire heirarchy for all events,
cleaning up some shutdown-hook related code on the way.
(A few drivers related to sparc are still skipped at kettenis' request)
ok kettenis mlarkin, tested by many others too


# 1.243 18-Apr-2013 deraadt

pin the acpi thread to the BSP, because AML and SMI cannot be trusted. I
was certain we were doing this already, but it turns out no...


Revision tags: OPENBSD_5_3_BASE
# 1.242 24-Dec-2012 guenther

In acpiioctl(), Return though the splx() path if suser() returns an error

ok deraadt@


# 1.241 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


# 1.240 04-Oct-2012 ehrhardt

Use information provided by ACPI to attach secondary PCI host bridges.

ok kettenis@


# 1.239 07-Sep-2012 kettenis

Add a new function called acpi_pci_min_powerstate() that returns the lowest
power state for a PCI device for the power state (currently S3 or S4) we're
transitioning into. This function is probably too simplistic; there is a lot
of (silly) complexity in the ACPI spec.

ok deraadt@


Revision tags: OPENBSD_5_2_BASE
# 1.238 13-Jul-2012 pirofti

Disable acpivideo(4) on ASUS laptops.

There are some models that panic whenever an event is received.
Some cope, some don't. In order to get this right acpiec(4) requires
major rework. Until then let machines boot.

Discussed with deraadt@.


# 1.237 11-Jul-2012 mlarkin

for now, restrict hibernate to root in the acpi ioctl kernel interface.
normal rules for controlling this interface through apmd still apply.

ok deraadt@


# 1.236 09-Jul-2012 deraadt

tiny little oops in untested hibernate block


# 1.235 09-Jul-2012 deraadt

clean up ioctl() case statements


# 1.234 09-Jul-2012 deraadt

acpi_disable_onegpe() is not used; ok pirofti


# 1.233 24-May-2012 kettenis

If _MIN > _MAX consider a WordBus resource to be invalid. Fixes a panic on
the IBM x3550-M4.

ok mikeb@


# 1.232 29-Mar-2012 mlarkin

Missing #ifdef in previous diff


# 1.231 29-Mar-2012 mlarkin

Prevent hibernation attempts on machines with unsupported disk controllers.
The same check is also performed later in the hibernate sequence, but by
then it's too late to cleanly unwind (presently).

ok deraadt


# 1.230 26-Mar-2012 deraadt

simplify conditionals in acpi_sleep_state()
ok mlarkin


# 1.229 26-Mar-2012 deraadt

hook in the hibernate request code; half of this diff is from mlarkin
ok mlarkin


Revision tags: OPENBSD_5_1_BASE
# 1.228 20-Sep-2011 deraadt

Very early in suspend while the clock is still ticking realtime,
save the clock back to the rtc. The zaurus already did this.
ok phessler miod kettenis


Revision tags: OPENBSD_5_0_BASE
# 1.227 02-Jul-2011 nicm

kqueue attach functions should return an errno or 0, not a plain 1. Fix
the obvious cases to return EINVAL and ENXIO.

ok tedu deraadt


# 1.226 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.225 16-Jun-2011 pirofti

Import acpitoshiba driver.

This is originally written by Hiroyuki Aizu, ported and integrated
in our tree by Javier Vazquez <javama4 AT gmail> with tweaks and
suggestions by me.

Import prodded by deraadt@ and the lack of testers on tech@.


# 1.224 27-Apr-2011 jcs

attach acpithinkpad to newer lenovo models like the x120e

ok deraadt@


# 1.223 22-Apr-2011 jordan

Fix uninitialzied variables and formatting strings (-Wxxx errors)


Revision tags: OPENBSD_4_9_BASE
# 1.222 02-Jan-2011 jordan

Re-add code for ACPI taskqueue, eliminates need for state vars in acpi softc
Serialize processing ACPI tasks for sleep/powerdown/GPE event
ok deraadt@


# 1.221 31-Oct-2010 guenther

Revert last commit: it breaks resume on Thinkpads
"then please back it out" deraadt@


# 1.220 26-Oct-2010 jordan

Add task queue for ACPI gpe and notify handlers
This fixes eject on Dell Latitude dock and an issue on the Dell Mini
battery update.
ok deraadt


# 1.219 07-Oct-2010 jordan

Create separate functions for enabling wake and runtime gpes.
Remove old suspend/resume gpewalk function
ok deraadt, mlarkin


# 1.218 05-Oct-2010 jordan

split acpi_enable_onegpe to separate enable/disable functions
ok deraadt


# 1.217 05-Oct-2010 jordan

Added acpi_disable_allgpes function to clear GPE status
ok deraadt


# 1.216 05-Oct-2010 deraadt

clean up acpi_softc use more


# 1.215 05-Oct-2010 deraadt

delete a bit of #if 0 code


# 1.214 05-Oct-2010 deraadt

hide use of our global acpi_softc pointer a bit better


# 1.213 29-Sep-2010 deraadt

grab the acpi thread lock very early in acpi thread startup; not that it
is likely that anyone will race us... but why be careless.
ok kettenis


# 1.212 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.211 29-Aug-2010 jasper

- add a check for FWRITE here too.

ok miod@


# 1.210 27-Aug-2010 kettenis

Make PCI Power Management optional, and only enable it when acpi(4) attaches.

ok deraadt@


Revision tags: OPENBSD_4_8_BASE
# 1.209 08-Aug-2010 kettenis

Close race between the acpi thread and wscons ioctls running in process
context. We might rip this out post-release and replace it with a solution
that serializes things in a more obvious way.

ok deraadt@, miod@ and "those crazy texans who ok anything"


# 1.208 08-Aug-2010 deraadt

Report the acpi sleep states when on a RAMDISK; ok krw


# 1.207 07-Aug-2010 kettenis

Enforce acpi register access rescrictions.

ok mlarkin@, deraadt@, tested by marco@


# 1.206 07-Aug-2010 deraadt

upon resume, notify all the acpiac and acpibat drivers to update
their status. some laptops do not do this automatically.
ok kettenis canacar


# 1.205 06-Aug-2010 marco

Always call _PSW on _LID devices that support it to enable lid open to
resume the machine. Conversely disable it when the machine wakes up.

Tested by several ok deraadt


# 1.204 06-Aug-2010 deraadt

As part of a revamp of the PM1/GPE code, I write this diff to look for
unmanaged EN & STS bits in the PM1 register at interrupt time and report
them. As a side effect this splits the STS acknowledgement into two writes
(for power, and sleep) instead of one. The printf that is added (to spot
unmanaged STS bits) has not yet been triggered as far as we know.

Before the "write to PM1 registers at the right offset" diff went in, this
was not neccessary. But newer thinkpads do not have a working soft power
button without this diff.

We have no idea why.
ok mlarkin kettenis


# 1.203 05-Aug-2010 deraadt

PM1 power button events were generating 'fake events' to acpibtn PWRB,
but if there is no such device these were being tossed. Instead, arrange
for all power button handlers to flag a softc variable which is processed
by the thread; so that one place will know to request a powerdown (psignal
to init)
ok marco mlarkin


# 1.202 05-Aug-2010 deraadt

rename a variable which is annoyingly poorly named


# 1.201 05-Aug-2010 mlarkin

Write to the correct offset for various ACPI pmreg registers. Writing to
the correct register does properly clear the wake status bits and fixed
event bits, which allows some machines to have more than 1 suspend cycle.

bug spotted by deraadt@

tested by deraadt@ and myself on a half-dozen machines and others on
hackers@

ok deraadt@, marco@


# 1.200 05-Aug-2010 deraadt

Fix the power button. The pm1 register is 16 bits in size.
My mistake, a while back.
tested by krw


# 1.199 04-Aug-2010 kettenis

Print supported sleep states.

ok marco@, phessler@, deraadt@


# 1.198 03-Aug-2010 mlarkin

Use the proper flag for re-enabling certain hardware events (power btn,
sleep btn). The (incorrect) flag prevoiously used coincidentally had the
same value, so this shouldn't cause any different behavior than before.

tested on a variety of machines (i386, amd64, sp, mp) - no behavioral
change seen

ok deraadt@


# 1.197 28-Jul-2010 marco

Make all gasio failures look and behave the same.


# 1.196 28-Jul-2010 mlarkin

Delay a little longer on suspend before giving up. Fixes machines that
sometimes don't suspend because the sleep transition takes too long
(symptoms like failing to sleep but the backlight stays on).

ok deraadt@


# 1.195 27-Jul-2010 deraadt

remove XXX; EC can now be attached early when needed; ok jordan


# 1.194 27-Jul-2010 deraadt

more brackets feels good


# 1.193 27-Jul-2010 deraadt

XXX annotate a block in acpi_enter_sleep_state() that is pretty worrying


# 1.192 27-Jul-2010 deraadt

horrific indentation that nearly made me puke


# 1.191 27-Jul-2010 mlarkin

Set the sleeping indicator light on machines that support it, in case SMI
doesn't do it for us. Shows the 'moon' LED on older Thinkpads when
sleeping.

Tested on T43p, W500, T510 by me and on X61s by deraadt@.

ok deraadt@


# 1.190 27-Jul-2010 jordan

Early initialization of acpiec if ECDT table exists
Fixes hang when booting thinkpads while docked
ok deraadt


# 1.189 26-Jul-2010 deraadt

remove merge error which was commited


# 1.188 26-Jul-2010 pirofti

Add support for Sony ACPI hotkeys via a new driver: acpisony(4).

Currently it only works for the suspend button and tries to do right for
the brightness events, but I haven't found a Sony laptop that like to do
right so far.

In the future I want to make the brightness keys work on all Sony's and
also add support for the zoom hotkeys and whatever other funky keys I can
find on those things.

Okay deraadt@.


# 1.187 22-Jul-2010 deraadt

pretty large cleanup of SMALL_KERNEL


# 1.186 21-Jul-2010 deraadt

in the acpi GPE handler, late ack level interrupts not edge interrupts.
Duh.


# 1.185 21-Jul-2010 deraadt

Merge the edge and level gpe handlers into one (now possible since the
gpe structure tells us whether the gpe is edge or level)


# 1.184 21-Jul-2010 deraadt

delete #if 0 code we are not going to use; ok marco


# 1.183 20-Jul-2010 deraadt

make the mask code for acpi_enable_onegpe() match other similar code


# 1.182 20-Jul-2010 deraadt

Handle the acpi interrupt controller with proper edge/level handling,
wrapped in spltty to avoid racing against the interrupt controller.
Repair the gpe bit masking code while there, and do operations in the
right order
ok kettenis mlarkin, help from jordan at figuring out the order of
operations.


# 1.181 20-Jul-2010 matthew

When we're preparing to go to sleep, only call bufq_restart() if other
preparations failed.

ok kettenis@, thib@, deraadt@


# 1.180 19-Jul-2010 deraadt

permit compilation with ACPI_DEBUG


# 1.179 19-Jul-2010 deraadt

there is no need for a seperate dsdt_softc pointer; dsdt does not have a
softc but is just a part of the single acpi driver.
ok jordan


# 1.178 19-Jul-2010 deraadt

acpi_delay() is no longer needed; ok jordan


# 1.177 18-Jul-2010 mlarkin

Clear event status on resume, to avoid going back to sleep/reboot/shutdown
immediately after resume. Tested by a bunch of people on hackers@ on
various hardware. Fixes at least Toshiba NB200 and Gateway LT3103u.

ok marco


# 1.176 13-Jul-2010 deraadt

rename some thread related interfaces so they are logical


# 1.175 10-Jul-2010 jordan

Verify _ADR bdf is correct (some systems return 0xffff)
Works on Dell x9xx and Studio 1555


# 1.174 09-Jul-2010 jordan

Clear the noise


# 1.173 08-Jul-2010 jordan

Add mapping for ACPI device to PCI bdf (match autoconf tree)
Simplify resource parsing function to use buffer argument
Convert namespace linked lists to use queue macros
ok marco@, deraadt@


# 1.172 06-Jul-2010 deraadt

Use spltty() to lock downcalls from apm against the information being
modified by the acpi thread.
ok kettenis


# 1.171 06-Jul-2010 deraadt

Don't use a workq to do the suspend, because it races aginst the acpi
thread. Instead, just tell the acpi thread to do the suspend for us.
This makes apmd & zzz work correctly. While here, have acpithinkpad
attempt to post the event to apm before prompting the suspend itself.
ok kettenis marco mlarkin


# 1.170 05-Jul-2010 mlarkin

Reset register SCI_EN on resume. The ACPI spec clearly states we don't have
to do this, but many machines don't, or forget to, which leads to all sorts
of problems on resume (broken battery stats, overheating, failure to resume
more than once, etc).

tested by many, ok marco@, deraadt@


# 1.169 01-Jul-2010 thib

do bufq quiesce again, since vnd/the bufq_done call in
biodone has been fixed.


# 1.168 01-Jul-2010 jordan

Backout recent AML changes, breaks on R210 and others


# 1.167 01-Jul-2010 jordan

Simplified aml_parse_resource call, cleanup code
ok marco


# 1.166 01-Jul-2010 jordan

Fix subordinate bus # for multi-root PCI buses
ok marco, kettenis


# 1.165 30-Jun-2010 thib

Disable/partially backout the bufq quiesce changes as this
is causing havoc with vnds and release must be buildable.


# 1.164 29-Jun-2010 deraadt

Fix ramdisks (new code was placed inside a SMALL_KERNEL chunk)


# 1.163 29-Jun-2010 jordan

Add support for mapping ACPI to PCI devices
ok kettenis, deraadt


# 1.162 29-Jun-2010 kettenis

Call bufq_quiesce() on suspend to stop all I/O and bufq_restart on resume.
Makes suspend/resume work much better while doing disk I/O.

ok mlarkin@


# 1.161 29-Jun-2010 tedu

60 is a more reasonable guess for number of minutes in an hour than 100.
spotted by TeXitoi. ok deraadt


# 1.160 27-Jun-2010 jordan

Add _acpi_matchhids routine, precursor to pci device match


# 1.159 27-Jun-2010 mlarkin

Clear ACPI fixed event status on resume (power buttons/etc) so that some
machines don't immediately resume after suspending to S3.

Tested by kettenis@ and I on 5 or 6 machines, deraadt@ "yeah, looks good"


# 1.158 27-Jun-2010 jordan

Cleaned up acpi_maptable, adds to table list directly


# 1.157 07-Apr-2010 deraadt

If we have done a wsdisplay_suspend(), and ACPI decides it does not want to
suspend, we must roll back by calling wsdisplay_resume() or the virtual
console switching will be locked forever
ok oga


# 1.156 07-Apr-2010 kettenis

Make suspend/resume work on MP machines (running an MP kernel). Joint work
from mlarkin@ and me, with some amd64 fixes thrown in by deraadt@

ok marco@, deraadt@, pirofti@, mlarkin@


# 1.155 31-Mar-2010 kettenis

Bring /dev/apm support in line with apm(4). Make the suspend button send
a suspend request event to apmd(8) instead of suspending immediately. Also
keep track of whether /dev/apm and /dev/apmctl are currently open, such that
we can still suspend immediately if apmd(8) isn't running.

ok deraadt@, marco@, pirofti@, jsing@, oga@


# 1.154 30-Mar-2010 oga

Prevent the apmd/x races for good.

When we hit suspend time, go through all wsdisplays on the system. if
they are in mode MAPPED, but not MODE_DUMBFB then if possible do a full
vt switch to a !mapped vt, and prevent switching back until resume time.
This has to be called from MD code because this involves userland
running so that X can run the vt switch signal handler. This way, any
case where we are using the "poke registers from userland" model, we
will not be on the hardware when we go down, so the kernel can actually
handle thing properly.

Tested on several acpi laptops (by kettenis@ and ian@), x40 (me and
beck@ at LEAST) and zaurus (me). Maybe others, but if so I forgot who at
this time..

Idea from deraadt somewhere over the Faroe Islands (I thought of a
similar thing myself a while ago). Much prompting from him. Ok and
comments miod@


# 1.153 25-Mar-2010 oga

Fix & vs. && in the kqfilter code.

Based on a comparison with the apm code.

ok deraadt@, kettenis@


Revision tags: OPENBSD_4_7_BASE
# 1.152 26-Nov-2009 mlarkin

Visual (and on some machines, audio) feedback while the machine is
resuming via calls to aml _SST method. This also makes the "moon" LED on
thinkpads to blink during resume and cease when resume is completed.

ok pirofti@


# 1.151 26-Nov-2009 deraadt

Toggle into "cold" when we we disable interrupts, because tsleeping
in the AML interpreter and context switching into userland processes
tends to sometimes pop the ACPI trampoline page out of our address
space. Makes lots more machines work.
ok kettenis mlarkin


# 1.150 24-Nov-2009 jsg

in the resume path evaluate _WAK before dealing with device drivers
like the spec says, not after.

ok mlarkin@


# 1.149 24-Nov-2009 deraadt

abort suspends on SMP systems until that gets written; ok mlarkin


# 1.148 23-Nov-2009 mlarkin

pci should return the result of it's children's suspend/resume calls back
to its parent.

handle suspend failure case and unwind if devices have an activate function
that returns failure.

ok deraadt@, kettenis@


# 1.147 23-Nov-2009 mlarkin

acpi* devices need to call their children on suspend/resume.
ok deraadt@


# 1.146 23-Nov-2009 pirofti

Remove ACPI_SLEEP_ENABLED checks.

This enables by default the suspend/resume paths in the kernel.

Okay deraadt@.


# 1.145 23-Nov-2009 deraadt

knf and spacing that are super annoying


# 1.144 23-Nov-2009 mlarkin

Hook up Theo's new children suspend/resume function, as well as more ACPI
suspend/resume glue.

ok deraadt@


# 1.143 26-Oct-2009 deraadt

Do not do strncmp comparisons on dv_xname because longer device names which
look similar could arrive in the future. Instead, compare directly against
dv_cfdata->cf_driver->cd_name
Issue originally spotted by miod


# 1.142 26-Oct-2009 jordan

Verify checksum+address when loading ACPI tables.
Some systems had invalid entries in RSDT/XSDT.
ok marco@


# 1.141 23-Jul-2009 cnst

New aibs(4) driver for ASUSTeK AI Booster (ACPI ATK0110) hardware monitoring,
with sensor state support through limits provided by the ACPI.

Tested on several ASUS motherboards kindly networked by Sam Fourman Jr.

ok deraadt marco jordan


Revision tags: OPENBSD_4_6_BASE
# 1.140 03-Jun-2009 pirofti

Import acpi power resource driver. This is needed for suspend/resume logic.

Okay jordan@ and marco@.


# 1.139 03-Jun-2009 pirofti

Add acpivideo support. This adds brightness support for all laptops
except thinkpads, they will use the acpithinkpad driver. The driver is
also hooked into wsconsole. So brightness can be adjusted via:

$ wsconsctl display.brightness=<percentage>

This is very helpfull on some laptops that have a nasty bios and get two
steps instead of one when pressing the brightness button.

Tested on various dell, fujitsu, acer, samsung and other laptops.

Okay marco@, miod@. Suggestions from kettenis@.
Lots of reviews and help from miod@, thanks!


# 1.138 03-Jun-2009 jordan

Added ejectable bay for IDE cdroms
ok marco@


# 1.137 30-Apr-2009 marco

Oga changed apm ioctls and forgot to grep. Temporary work around by hitting
the sleep button when we get any request. This interface will chnage at
some point.
ok oga


# 1.136 19-Apr-2009 krw

Call acpi_sleep_walk() when powering down just like when entering
any other sleep state. This tells acpi wake devices to not wake up
the box until manual powering up resets their status. Makes my Dell
GX520 stay powered off in response to 'halt -p'.

In snaps for a while.

ok marco@ (several times) deraadt@


# 1.135 17-Apr-2009 pirofti

Add device ranking support for future sleep/wake developments.

okay marco@


# 1.134 11-Mar-2009 jordan

Cleanup.. use aml_evalinteger instead of aml_evalname
ok marco@


# 1.133 10-Mar-2009 jordan

Fix cut/paste error.. call _BFS not _PTS
ok marco@


Revision tags: OPENBSD_4_5_BASE
# 1.132 21-Feb-2009 marco

overzealous cut/paste; found by Vladimir Kirillov <proger@uaoug.org.ua>


# 1.131 20-Feb-2009 marco

Oops didn't mean for Debugger() to remain there.


# 1.130 19-Feb-2009 marco

suspend/resume bits so that we can develop this in tree. This is disabled.
code from mlarkin and me
help from art,toby,jordan and several others
ok jordan, go for it deraadt


# 1.129 10-Feb-2009 jordan

Fix null pointer for shutdown if acpi not enabled
ok marco@


# 1.128 04-Feb-2009 kettenis

Some systems that claim to be ACPI 2.0 apparently don't provide an XSDT
address. Make the code fall back on the RSDT on those systems.

From Joerg Niendorf <p3y80@internode.on.net>. Fixes PR 5986.


# 1.127 17-Jan-2009 krw

A couple of stray malloc/memset -> malloc(M_ZERO), a bit of knf and
lose a couple of unnecessary casts. No functional change.

Feedback from kettenis@

ok kettenis@ marco@


# 1.126 06-Nov-2008 marco

Collapse all sanity checks of hids into one function.

Originaly from jordan


Revision tags: OPENBSD_4_4_BASE
# 1.125 02-Jul-2008 fgsch

add acpivideo. for now almost an empty skel so others can work on
suspend/resume. eventually it will also manage output switching and
brightness where supported. prodded by marco@


# 1.124 11-Jun-2008 marco

Fix $OpenBSD$ for once and for all


# 1.123 08-Jun-2008 deraadt

aaa_dev was being set from an uninitialized variable; ok toby


# 1.122 07-Jun-2008 marco

Kill ACPIVERBOSE. It was never used and is worthless to boot.

comments jmc
ok toby


# 1.121 06-Jun-2008 marco

Shrink acpi some more for floppies and while here re-enable acpi on the
floppies. Fixes crazy sony vaio kettenis has.

tested by deraadt
ok deraadt jordan kettenis


# 1.120 01-Jun-2008 marco

Fix \n nit in SMALL_KERNEL.


# 1.119 01-Jun-2008 marco

Re-enable new parser. Lots of testing by various folks. Fits all media
now too.

ok kettenis deraadt


# 1.118 16-May-2008 dlg

back out jordans new parser.

it didnt have enough oks from the right people, it did break machines
that were previously working, and it bloated the RAMDISKS and install
media beyond the constraints of the media.

requested by deraadt@


# 1.117 14-May-2008 jordan

Adding new ACPI Parser code
Old guts of code still exists, needs to be torn out next
ok marco@
ok brad@


# 1.116 27-Apr-2008 jcs

add acpithinkpad(4), an acpi support driver for ibm/lenovo
thinkpads.

provides functionality that the bios did on older, non-acpi
thinkpads like respond to screen brightness keys and toggle the
bluetooth device on and off.

ok deraadt@ and marco@


# 1.115 24-Apr-2008 jsing

Introduce acpiasus(4), a driver for the ACPI based hotkeys found in many
ASUS laptops (including the ASUS EeePC) - largely based on NetBSD's
asus(4) driver. On the ASUS EeePC this allows us to enable/disable
wireless, change screen brightness and use the volume keys.

ok jsg@, weingart@


# 1.114 12-Apr-2008 kettenis

Get rid of acpi_s5 global variables; simply send SIGUSR2 instead of SUGUSR1
to tell init(8) to power down the machine.

ok krw@


Revision tags: OPENBSD_4_3_BASE
# 1.113 21-Jan-2008 jsg

Check against RESET_REG_SUP in FADT before doing the acpi reset dance
after all, not doing so appears to break more machines than it fixes.

ok marco@


# 1.112 05-Jan-2008 kettenis

Never attach more acpicpu(4) devices than cpu(4) devices. Always attach
the first acpicpu(4) device on non-MULTIPROCESSOR kernels, regardless of its
ACPI processor ID.

ok mikeb@


# 1.111 05-Dec-2007 deraadt

Hang acpi(4) below bios(4) instead of mainbus(4). This lets us move acpi(4)
before pcibios(4), and then.... if acpi(4) attaches, skip pcibios(4) since
it messes the machines up. Fixes claudio's HP dl320 G5, and almost assuredly
others.
ok kettenis


# 1.110 05-Dec-2007 deraadt

repair acpiopen/acpiclose and friends to handle the /dev/apmctl interface,
so that apmd(8) can actually work correctly against acpi(4), and thus,
even do the fancy -C/-A stuff; also tested by jmc


# 1.109 02-Dec-2007 fgsch

fix the trailing space for real now.


# 1.108 02-Dec-2007 fgsch

avoid trailing spaces on output; marco@ ok.


# 1.107 27-Nov-2007 kettenis

Fix off-by-one that made us attach acpicpu0 and acpicpu1 on GENERIC kernels.

ok marco@


# 1.106 25-Nov-2007 jsg

When available and ACPI is enabled use the ACPI reset method
to reboot. This is required for some quirky sis machines
and likely others to reboot.

Tested by many. 'commit early commit often' deraadt@


# 1.105 19-Nov-2007 deraadt

sigh, amd64 still uses that stupid acpi_enabled variable...


# 1.104 19-Nov-2007 deraadt

softc's come initialized to 0, you know...


# 1.103 19-Nov-2007 deraadt

store revision; from jsg


# 1.102 19-Nov-2007 kettenis

Initialize sc_facs earlier.

ok deraadt@, beck@, canacar@


# 1.101 16-Nov-2007 deraadt

fix the bus_space #define nightmare, so that amd64 and i386 are much more
uniform. as a result shared code like acpi needs less #ifdef's
ok marco kettenis


# 1.100 15-Nov-2007 deraadt

remove the #ifdef ACPI_ENABLE. the EC codepath is either safe enough (and
will be improved), or it is unsafe because AML assumes it must be there
ok almost everyone


# 1.99 15-Nov-2007 mikeb

Make acpicpu(4) attach to the existing CPUs only.

Problem reported by form@. Fix idea by kettenis@.
Lots of help from deraadt@. Tested by deraadt@, form@ and me.

ok deraadt marco


# 1.98 14-Nov-2007 deraadt

spacing


# 1.97 12-Nov-2007 deraadt

correct : vs , in attach-time messages


# 1.96 06-Nov-2007 deraadt

all ACPI_ENABLE kernels need acpi_interrupt()


# 1.95 06-Nov-2007 deraadt

move acpi_read_pmreg() and acpi_write_pmreg() out of SMALL_KERNEL so
that acpi ramdisks can compile (obvious solution)


# 1.94 05-Nov-2007 canacar

Establish the acpi interrupt earlier. Fixes problems with some
GENERIC.MP kernels not getting acpi interrupts. This is a
workaround until the real issue with late establishment is fixed.
ok weingart@, beck@, gwk@


# 1.93 03-Nov-2007 jordan

Added support for displaying wakeup devices
ok beck@,weingart@,gwk@


# 1.92 11-Oct-2007 kettenis

When walking the tree yo find _PRT methods, check whether the device is
actually enabled and functioning. Similar to what we do for _INI.

ok weingart@, beck@


# 1.91 08-Oct-2007 krw

More simple memset(,0,) -> M_ZERO changes. In this batch move to
size(*p) as the first malloc() parameter where p is declared locally
and thus easy to check. Add M_ZERO to gpe_table allocation in acpi.c
even though there is no obvious bzero or memset nearby.


# 1.90 13-Sep-2007 mk

Remove unnecessary memset() (see aml_evalnode(9)) and a bit of KNF.

ok toby


# 1.89 13-Sep-2007 weingart

Implement ACPI 6.5.1 spec tree walk for _STA and _INI.
Fixes mk's laptop. No regressions so far (thank you to the
testers).

ok gwk@, mk@, marco@


Revision tags: OPENBSD_4_2_BASE
# 1.88 17-Apr-2007 mk

Now that acpidock no longer hangs machines during boot (at least
according to the reports I've been getting) it should be safe to enable
it again, so remove the #if 0 stuff around it.

ok marco


# 1.87 11-Apr-2007 jordan

Added changes for C-State
ok marco@


# 1.86 26-Mar-2007 marco

Calculate minutes left more accurately.

From giovanni <qgiovanni@gmail.com>


Revision tags: OPENBSD_4_1_BASE
# 1.85 03-Mar-2007 mk

Stop looking for acpidock until we fix that stuff that breaks all pre-60
series ThinkPads with acpidock(4) enabled.


# 1.84 22-Feb-2007 marco

We were getting no interrupts in .MP. There are 2 fixes; one in
acpi_machdep.c and move the interrupt establishment down after ioapic
has been initialized.

ok jordan


# 1.83 21-Feb-2007 kettenis

Make an acpi-enabled GENERIC.MP work on systems that only support PIC mode.

ok marco@


# 1.82 19-Feb-2007 jordan

Added acpi_map_address function for mapping GAS/IO registers
ok marco@


# 1.81 18-Feb-2007 jordan

Added dynamic perfect hash for opcodes
ok marco@


# 1.80 18-Feb-2007 marco

Shave off another K pre compression.
Shuffle SMALL_KERNEL functions around so that we only have a single #ifndef


# 1.79 17-Feb-2007 marco

Add SMALL_KERNEL throught acpi to make it fit on boot media.
ok deraadt


# 1.78 13-Feb-2007 marco

Add smarter _INI code. This helps with some dissapearing devices.

ok kettenis jordan


# 1.77 31-Jan-2007 gwk

Introduce acpi_hasprocfvs thats "ACPI Has Processor Frequency and Voltage
Scalling" a global flag set by acicpu attach to indicate that ACPI is
handling fvs through the _PSS objects. This will fix disappearing
EST/powernow when booting kernels with acpi enabled, as acpicpu isnt
compiled into GENERIC yet. Also in cases where acpicpu is built in and
legacy EST/powernow work but acpi is missing the _PSS object, the legacy
routines will be used.

tested by dim@, and Nick Nauwelaerts <nick AT nauwelaerts.net>
ok marco@, dim@


# 1.76 22-Jan-2007 mk

Actually look for docking stations by looking for _DCK nodes.

ok marco


# 1.75 02-Jan-2007 marco

On some systems EC must be attached as the first device. Move EC from
generic _HID devices to its own routine to force first attach. Bug found
and debugged by joris@

Code mostly written by joris@


# 1.74 26-Dec-2006 marco

Add polling to devices that require it such as acpibat and acpitz.
Use the same tsleep/wakeup mechanism as the interrupt code to keep it non-
concurrent.


# 1.73 21-Dec-2006 deraadt

even more knf love


# 1.72 21-Dec-2006 marco

Remove some leftover acpiec gpe/interrupt code.


# 1.71 20-Dec-2006 deraadt

oops, name already used


# 1.70 20-Dec-2006 deraadt

rename __acpi_enable_gpe(); ok marco


# 1.69 20-Dec-2006 deraadt

acpi_unmap_pmregs() is not used, ok marco


# 1.68 19-Dec-2006 deraadt

simplify some low-level methods substantially (shrinking code); ok marco jordan


# 1.67 06-Dec-2006 mk

malloc(..., M_NOWAIT) with no NULL check is bad. If allocation fails,
complain, clean up, and bail out of attach.

input and ok gwk


# 1.66 28-Nov-2006 jordan

Fixed clearing arg for acpi_init_pic
ok kettenis@


# 1.65 27-Nov-2006 kettenis

Don't spam dmesg.


# 1.64 27-Nov-2006 jordan

Fixes for acpiprt routing
Post-parse fixup for namerefs
ok kettenis@


# 1.63 25-Nov-2006 marco

Don't handle acpiev events inside the gpe handler since this causes deadlocks.

ok jordan@


# 1.62 15-Nov-2006 kettenis

Hook up acpi apic interrupt routing support to generic acpi code.

ok jordan@


# 1.61 02-Nov-2006 marco

Kill acpiec attach function and add its functionality to the generic _HID
device attach

prodded and ok jordan


# 1.60 02-Nov-2006 marco

Fix comments to reflect reality.


# 1.59 19-Oct-2006 marco

Replace ACPI ioctls with APM ioctls. This makes ACPI talk the same protocol
as APM therefore userspace can now retrieve power information in the same
fashion as all other apm arches (zaurus, macppc, i386 etc).

To use force the use of acpi device like so: apmd -f /dev/acpi

ok jordan@


# 1.58 19-Oct-2006 jordan

Fixed GPE debug levels
Fixed ref counter on temporary bufferfields
DSDT parsing fully working on IBM Thinkpad
ok marco@


# 1.57 15-Oct-2006 jordan

Removed old GPE handler code remnants
ok marco@


# 1.56 12-Oct-2006 jordan

Massive ACPI changes
Fixed ACPI leaks
new ACPI API
ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.55 30-Jun-2006 gwk

KNF, No functional change,
"go at it" marco@


# 1.54 30-Jun-2006 jordan

Support for calling Embedded Controller via new GPE interface
ok marco@


# 1.53 30-Jun-2006 jordan

Added new code for GPE handling
Also works with ACPIEC; need to fixup acpiec.c to remove original interrupt handler
ok marco@


# 1.52 30-Jun-2006 jordan

Added offset argument for acpi read/write pmreg routines (new GPE code)
ok marco@


# 1.51 31-May-2006 todd

do not call acpi_powerdown() unless acpi has successfully initialized
ok gwk@ canacar@


# 1.50 29-May-2006 canacar

Add embedded controller support.
ok marco@ deraadt@ testing & ok gwk@


# 1.49 19-May-2006 canacar

Minimal thermal zone driver. Monitors thermal zone temperature,
shuts down the system if the 'critical temperature' is reached.
ok marco@


# 1.48 11-Apr-2006 marco

KNF.


# 1.47 11-Apr-2006 gwk

Add acpi_resume(struct acpi_softc *sc), which calls some AML methods,
detected at attach time, currently unused.

ok marco@


# 1.46 11-Apr-2006 gwk

Discover some ACPI sleep related methods, and call them at the
appropriate time in acpi_enter_sleep_state().

ok marco@


Revision tags: OPENBSD_3_9_BASE
# 1.45 26-Feb-2006 marco

Add acpicpu as a device.

ok jordan@


# 1.44 23-Feb-2006 marco

remove dead function.

ok jordan@


# 1.43 22-Feb-2006 marco

Print out all tables it found during autoconf. We'll need this data in
the future.


# 1.42 22-Feb-2006 marco

Make dmesg less verbose.

ok jordan.


# 1.41 22-Feb-2006 jordan

Added parsing of SSDT tables
ok marco@


# 1.40 22-Feb-2006 jordan

Added new aml_register to notify by device id
Moved powerdown code to button device
ok marco@


# 1.39 21-Feb-2006 marco

Readjust debug levels that crept in too low over the last few days.
KNF


# 1.38 21-Feb-2006 marco

Rig the power button so that the machine shuts down correctly.


# 1.37 20-Feb-2006 marco

Add acpibtn device. Remains disabled for now.


# 1.36 19-Feb-2006 jordan

Added code for aml_notify GPE notification callback
ok marco@


# 1.35 19-Feb-2006 grange

Fix acpi_enter_sleep_state() by replacing current ugly hack
with mostly correct sleeping state transition code.

ok brad@ marco@


# 1.34 19-Feb-2006 marco

jordan:
Enable interrupts.
Added peripheral code to make this work.

marco:
Replace softintr and other types of interrupts with a handler with context.
Fixed interrupt magic for GPEs.
Some KNF

This is work done by jordan and I but has to be a single commit.


# 1.33 17-Feb-2006 marco

Remove earlier introduced delays. We really only need them during autconf.

ok jordan@


# 1.32 17-Feb-2006 marco

Add a generic delay function as per the ACPI spec.

from jordan@


# 1.31 17-Feb-2006 marco

Use tsleep instead of delay when !cold.


# 1.30 17-Feb-2006 marco

Argh, we need delays between reads/writes. The backend reads (i2c most likely)
are so slow that values come back as 0 sometimes. We have to do this
despite the spec saying we shouldnt have to. Guess having a faster
parser than others can hurt sometimes.

Idea from jordan.


# 1.29 17-Feb-2006 jordan

Fixed code for handling _INI
Rearranged debug statements
ok marco@


# 1.28 16-Feb-2006 jordan

Added stub for calling _INI
Fixed aml_dowhile for NULL return
ok marco@


# 1.27 16-Feb-2006 jordan

Fixed parser/evaluator to retrieve battery info+state
Cleanup of parser
Dell laptops return battery status
ok marco@


# 1.26 06-Feb-2006 brad

fix a typo so this will compile on amd64.


# 1.25 04-Feb-2006 marco

When ACPI_ENABLED wasn't set but the devices were it had an unknown variable.

Pointed out by ian@.


# 1.24 03-Feb-2006 jordan

Rewrote aml parser yet again...
Fixed bus_space_map for i386/amd64
Added _BST and _BIF to acpibat to get status/info
ok marco@


# 1.23 20-Jan-2006 jordan

More evaluator changes
Removed old evaluator code
Fixed _HID value
ok marco@


# 1.22 19-Jan-2006 jordan

Added support for read/write of PCI Config space
Help jason@
ok marco@


# 1.21 18-Jan-2006 jordan

Added new evaluation routines
Added helper methods for buffer/field evaluation
Fixed broken indentation on dsdt.c

ok marco@


# 1.20 17-Jan-2006 jordan

Major overhaul of the aml parser. Evaluater temporarily disabled.

ok marco@


# 1.19 06-Jan-2006 grange

Check for ACPI control availability only if ACPI_ENABLE defined.


# 1.18 06-Jan-2006 grange

Informative error messages on attach, prettier dmesg.


# 1.17 06-Jan-2006 grange

printf -> dnprintf conversion for debug messages.


# 1.16 05-Jan-2006 grange

Find SLP_TYPx values and store them for future use in
acpi_enter_sleep_state().


# 1.15 05-Jan-2006 grange

Random KNF and cleanup


# 1.14 05-Jan-2006 grange

Provide symbolic names for the acpi sleeping states and use it.


# 1.13 28-Dec-2005 marco

Moved DEVNAME to a acpidev.h

ok jordan@


# 1.12 18-Dec-2005 sturm

add back comment about wrong code

ok marco


# 1.11 16-Dec-2005 marco

Remove test for kbd_reset. Prompted deraadt@, ok jordan@


# 1.10 16-Dec-2005 jordan

Added hooks for powerdown/power button press


# 1.9 16-Dec-2005 jordan

Cleaned up common power management register i/o
Added enter sleep state ioctl
Hooked power button interrupt
ok marco@


# 1.8 16-Dec-2005 marco

Remove autoconf chatter


# 1.7 16-Dec-2005 jordan

Moved DSDT AML parsing into parent ACPI driver
ok marco@


# 1.6 14-Dec-2005 marco

Add AC device glue.


# 1.5 14-Dec-2005 marco

Adjust debug printing levels to something more sane.


# 1.4 13-Dec-2005 marco

Add glue to attach devices to apci.


# 1.3 07-Dec-2005 marco

KNF


Revision tags: OPENBSD_3_8_BASE
# 1.2 10-Jul-2005 grange

Spacing cleanup.


# 1.1 02-Jun-2005 tholo

Start on a basic ACPI framework -- does not do much more than read out the
ACPI tables into kernel memory and attach ACPI and HPET timers currently.

In order to test this code, enabling the devices in GENERIC as well as
the ACPI_ENABLE option is needed. This code does not do any thermal
control yet, so this should be done with care depending on the platform.

In the tree so more people can contribute to making this more fully
featured.

Ok niklas@ grange@ tedu@


# 1.381 12-Apr-2020 kettenis

Add support for the _CCA method. This method indicates whether DMA is
cache-coherent or not. To implement this, acpi(4) gets two bus_dma tags
and passes the appropriate one when attaching devices based on _CCA.
On i386/amd64, where for all practical purpose DMA is always cache-coherent,
the two tags are the same. But on arm64 they are distinct.

ok patrick@


# 1.380 07-Apr-2020 visa

Abstract the head of knote lists. This allows extending the lists,
for example, with locking assertions.

OK mpi@, anton@


# 1.379 03-Apr-2020 mpi

Fix incoherencies of errors returned by various *kqfilter routines.

EOPNOTSUPP is returned when there's no "filterops" for a given fd.
EINVAL when the requested filter isn't supported by the "filterops".
ENXIO means the underlying device is no longer valid/present.

ok visa@


# 1.378 20-Feb-2020 visa

Replace field f_isfd with field f_flags in struct filterops to allow
adding more filter properties without cluttering the struct.

OK mpi@, anton@


# 1.377 31-Dec-2019 visa

Use C99 designated initializers with struct filterops. In addition,
make the structs const so that the data are put in .rodata.

OK mpi@, deraadt@, anton@, bluhm@


# 1.376 31-Dec-2019 mpi

Convert infinite sleeps to {m,t}sleep_nsec(9).

ok kn@


# 1.375 31-Dec-2019 jsg

include sys/sensors.h for sensor use
don't depend on indirect inclusion from acpidev.h


Revision tags: OPENBSD_6_6_BASE
# 1.374 07-Sep-2019 kettenis

Revert acpipci(4) changes. Too many systems are broken. We'll try again
after 6.6 as been released. The acpireg.h change stays behind.


# 1.373 28-Aug-2019 kettenis

Use ACPI information to attach PCI busses like we do on arm64. There are a
few additional quirks though, and attaching the PCI busses is delayed to
replicate the existing code more closely. That may be changed in the
future. Also tweak how we handle MSI support and respect to ACPI flag
that says we shouldn't attempt to use MSIs.

Some fallout is expected.

ok patrick@


# 1.372 27-Aug-2019 deraadt

On amd64 the bootup and resume paths for SP/BP/SP were not performing
msr/pat, clock-startup, firmware upgrade, fpu initialization, and cpu
soft-state initialization in the same order. That kind of chaos is
crazy and fragile. Improve the acpi resume call sequence and unify order
of the operations.


# 1.371 02-Jul-2019 jcs

acpi_set_gpehandler: turn edge arg into flags, add support for
passing GPE_DIRECT which directly executes the supplied callback and
clears a level interrupt rather than going through acpi_taskq which
can be slow when other driver callbacks are in the queue.

This should not be used for GPE handlers that need to do anything
ACPI-related.

ok kettenis


# 1.370 10-Jun-2019 kettenis

Print proper ACPI version number.

ok mlarkin@


# 1.369 08-Jun-2019 kettenis

When establishing the mapping between ACPI device nodes and PCI devices,
skip PCI host bridges and devices that aren't there.

ok patrick@


# 1.368 07-Jun-2019 kettenis

Introduce acpi_getsta() and use it to eliminate some duplicated code.

ok patrick@


# 1.367 12-May-2019 kettenis

Make sure the extended PM register descriptions actually exist before
using them. Fixes machines with ancient ACPI 1.0 (such as recent
hypervisors).

ok deraadt@


# 1.366 12-May-2019 kettenis

Add support for mapping ACPI PM registers using the "extended" gasio
representation from the FADT. Mostly fixes the Lanner NCA-1510.

ok lteo@, mlarkin@, deraadt@


# 1.365 11-May-2019 lteo

Fix incorrect error message.

ok kettenis@


# 1.364 08-May-2019 tedu

free size. the allocation is the header plus the length.


Revision tags: OPENBSD_6_5_BASE
# 1.363 04-Apr-2019 kettenis

Avoid potential null-pointer dereference. Found by Kent R. Spillner.

ok deraadt@, jsg@


# 1.362 20-Jan-2019 jsg

fix SMALL_KERNEL build after changes in rev 1.361
reported by naddy@ ok deraadt@


# 1.361 19-Jan-2019 tedu

Add a pwraction sysctl that controls what the power button does on acpi.
By default, nothing changes -- shutdown is initiated. But allows turning
power button into a sleep button if desired.
(grudging) ok from a few parties


# 1.360 26-Oct-2018 kettenis

Add acpipci(4) on amd64. For now this only calls the PCI-specific _OSC
method to let the ACPI implementation know what features we support.


Revision tags: OPENBSD_6_4_BASE
# 1.359 30-Aug-2018 kettenis

Move softbutton check before the check that skips attaching certain acpi
devices. Fixes regression caused by matching on _CID in addition to
matching on _HID.

ok matthieu@, mlarkin@


# 1.358 25-Aug-2018 kettenis

Match ACPI devices based on _CID if no match for _HID is found.

ok mpi@


# 1.357 19-Aug-2018 kettenis

Add support for multiple PCI segments. Only really implemented for arm64
for now as amd64/i386 firmware still caters for legacy OSes that only
support a single PCI segment.

ok patrick@


# 1.356 03-Aug-2018 kettenis

Let ahci(4) match on _CLS instead of _HID when attaching at acpi(4). Avoids
having to add many more _HID entries to the match table.

ok deraadt@, mlarkin@


# 1.355 10-Jul-2018 kettenis

Make legacy interrupts work in acpipci(4).

ok patrick@


# 1.354 05-Jul-2018 kettenis

Add acpipci(4), a driver that supports generic ECAM-compatible PCI host
bridges based on information provided by ACPI.

ok mlarkin@


# 1.353 04-Jul-2018 kettenis

Properly pass around the PCI "chipset tag" in acpi(4) and refactor
acpimcfg(4) to call an MD initialization functions that sets up a tag for
PCI ECAM.

ok guenther@, mlarkin@, krw@


# 1.352 01-Jul-2018 kettenis

Move acpi(4) attach glue into acpi_machdep.c.

ok guenther@, deraadt@


# 1.351 01-Jul-2018 kettenis

Add support for _DSD Device Properties.

ok mlarkin@


# 1.350 30-Jun-2018 kettenis

Add intr_enable() function, intended for MI use to amd64 and i386 and use
this in the acpi(4) suspend/resume code paths.

ok deraadt@


# 1.349 29-Jun-2018 kettenis

Use <stdint.h> types.

ok mpi@, jung@, krw@, deraadt@


# 1.348 29-Jun-2018 mlarkin

ACPI: Allow (with warning) GPE handler reassignment, instead of returning
a failure code.

ok kettenis


# 1.347 26-Jun-2018 mlarkin

knf


# 1.346 26-Jun-2018 kettenis

Let acpi(4) pass down the bus dma tag.

ok deraadt@


# 1.345 25-Jun-2018 kettenis

Reorganize acpi(4) code a bit in preparation for upcoming arm64 support.

ok deraadt@


# 1.344 20-May-2018 kettenis

Wake up the acpi thread after scheduling a gpio event.

ok mpi@


# 1.343 17-May-2018 kettenis

Sprinkle some #ifndef SMALL_KERNEL such that RAMDISK kernels cuild again.

pointed out by stsp@


# 1.342 17-May-2018 kettenis

Implement GenericSerialBus OpRegion support.

ok mlarkin@


# 1.341 27-Mar-2018 kettenis

Add acpicmos(4), a driver that implements SystemCMOS OperationRegion
access support. This fixes machines where the AML doesn't check whether
support for this OperationRegion type has been registered by the OS.

ok mlarkin@


Revision tags: OPENBSD_6_3_BASE
# 1.340 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.339 10-Feb-2018 deraadt

Syncronize filesystems to disk when suspending. Each mountpoint's vnodes
are pushed to disk. Dangling vnodes (unlinked files still in use) and
vnodes undergoing change by long-running syscalls are identified -- and
such filesystems are marked dirty on-disk while we are suspended (in case
power is lost, a fsck will be required). Filesystems without dangling or
busy vnodes are marked clean, resulting in faster boots following
"battery died" circumstances.
Tested by numerous developers, thanks for the feedback.


# 1.338 08-Feb-2018 deraadt

After flushing disks, try again to release all memory. We don't need to
save dirty memory to the hibernate space.


# 1.337 08-Feb-2018 deraadt

Toss all releaseable memory, because fragmentation can get in the way
of allocating the hibernate playpen.


# 1.336 08-Feb-2018 deraadt

sr_quiesce() is a new approach for ensuring that softraid drains
output to the disks.

This is part of a larger suspend/resume filesystem-safety diff, which
has been worked on for a couple of months already. Tests by job, krw,
beck, benno, and others. Sometimes even by snapshot users...


# 1.335 29-Nov-2017 kettenis

Add support for GPIO-signaled events.

ok mlarkin@


# 1.334 16-Nov-2017 jcs

Add PCI attachment for dwiic(4) needed by Intel 100 Series machines

ok kettenis


# 1.333 14-Nov-2017 kettenis

Add a few more PNP IDs to the skip lists.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.332 17-Aug-2017 stsp

Make dwiic(4) attach its ACPI parent dependencies.
This change makes the Asus E200HA keyboard work.

Original analysis and patch by Cesare Gargano
ok kettenis@


# 1.331 09-Aug-2017 dcoppa

Add "PNP0303" (8042 PS/2 Controller) to acpi_isa_hids[]

ok kettenis@


# 1.330 22-Jul-2017 jcs

acpibat: look for _BIX first before falling back on _BIF

ACPI 4.0 deprecated _BIF for battery status so some newer machines
are exporting _BIX which extends _BIF's package with a few extra
fields. When using _BIX, export the new discharge cycle count as a
sensor.

feedback from kettenis and anton
ok deraadt


# 1.329 20-Jul-2017 kettenis

Start a list of isa(4) device names such that we can avoid printing them
as "not configured".

ok mikeb@, millert@, deraadt@


# 1.328 08-Apr-2017 deraadt

Sensors are run as callbacks inside tasks. During suspend a sensor
could be running inside a driver that will be force-detached, or due
to tsleep end up disrupting the softstate/hardstate contract. At
suspend time, quisce all these callbacks by waiting for completion.
This issue has never been observed for real, but may be implicated in
suspend/resume failures.
ok kettenis guenther mlarkin


# 1.327 08-Apr-2017 deraadt

Two prototypes for acpi_maptable; merge to one place


Revision tags: OPENBSD_6_1_BASE
# 1.326 27-Mar-2017 deraadt

Now that hibernate_alloc() only has clean success/failure, don't
need to call hibernate_free() to clean up a partial mess.
ok mlarkin kettenis


# 1.325 13-Mar-2017 deraadt

allow ramdisks to build; ok jcs


# 1.324 12-Mar-2017 jcs

try to attach acpisbs first and if successful, don't attach acpibat


# 1.323 02-Mar-2017 natano

Add a new sysctl machdep.lidaction. The sysctl works as follows:

machdep.lidaction=0 # do nothing
machdep.lidaction=1 # suspend
machdep.lidaction=2 # hibernate

lidsuspend is just an alias for lidaction, so if you change one, the
other one will have the same value. The plan is to remove
machdep.lidsuspend eventually when people have upgraded their
/ets/sysctl.conf.

discussed with deraadt, who came up with the new MIB name
no objections mlarkin
ok stsp halex jcs


# 1.322 28-Feb-2017 natano

Untangle abstract sleep modes from ACPI sleep states. This paves the way
for further work in the area.

idea and ok deraadt
ok mlarkin


# 1.321 22-Feb-2017 jcs

add acpisbs, an acpi smart battery subsystem driver reading data
over smbus

currently disabled because it conflicts with acpibat

ok deraadt, kettenis


# 1.320 14-Jan-2017 kettenis

Revert aml_rdpciaddr changes; breaks several machines that were working
before.


# 1.319 11-Jan-2017 jsg

Increase the size of the buffers used for _HID and _CID strings to be
able to fit "Hyper_V_Gen_Counter_V1".

ok mlarkin@ deraadt@


# 1.318 08-Jan-2017 kettenis

Make aml_rdpciaddr() work for for devices behind bridges. The code now
uses the mapping between AML nodes and PCI devices that we establish.
Because _INIT methods may end up calling aml_rdpciaddr(), make sure we
create that mapping early. Also handle devices that aren't actually present.
These devices are now included in the mapping and reads will return an
all-ones patterm whereas writes are a no-op..

ok mlarkin@


# 1.317 25-Oct-2016 pirofti

Add the resource index to aml_parse_resource() callback function.

This is needed by an upcoming acpiec commit that handles machines breaking
the current ACPI specifications.

Change suggested by and ok kettenis@, guenther@


# 1.316 18-Sep-2016 guenther

If the APIC table (MADT) isn't present then ci_acpi_proc_id won't be set
when acpicpu tries to attach, leading to a NULL dereference. The ACPI
processor object on the Dells where this was reported is bogus and useless
anyway so apply the same test when deciding whether to attach to processor
objects as acpicpu would when matching them to cpus.

Problem reported by Eivind Eide (xenofil (at) gmail.com) and
Johan Huldtgren (johan+openbsd-bugs (at) huldtgren.com).
Testing by Eivind Eide.
ok kettenis@ mlarkin@


# 1.315 03-Sep-2016 naddy

Write the system time back to the RTC every 30 minutes.
This fixes the problem that long-running machines which were not
shut down properly would reboot with a badly offset system time.

hints and ok kettenis@


# 1.314 31-Aug-2016 mlarkin

Don't put a device in the wake device list if _STA says it's not there.

Fixes at least one machine that woke up from sleep / poweroff immediately.

ok kettenis@, tested by kettenis and martijn@


# 1.313 28-Jul-2016 kettenis

Store the acpi processor ID/UID in struct cpu_info, and use it to attach
acpicpu(4) drivers to the right cpu(4).

ok mlarkin@, guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.312 10-Jun-2016 kettenis

The power management timer has been made optional in ACPI 5.0A, so don't
attempt to attach acpitimer(4) if the timer isn't present.

Pointed out by jcs@

ok jcs@


# 1.311 08-May-2016 kettenis

Make sure devices listed by a device's _DEP method are attached before
we attach the device itself. The _DEP methode, introduced in ACPI 5.0,
is a hint that the device in question depends on OpRegion support from
the devices listed by _DEP, so we have to order them the proper way.
To prevent us from attaching those devices again when we encounter them
later walking down the device tree, keep track of our attempts to
attach a device driver to them using aflag in the aml_node structure.

ok guenther@


# 1.310 25-Apr-2016 pirofti

We attach acpihpet(4) and acpiec(4) without using the HID, so do not
report it in the dmesg.

OK deraadt@, mlarkin@.


# 1.309 02-Apr-2016 kettenis

Reduce dmesg spam by knocking out some acpi hardware IDs that don't need a
driver.

ok mpi@


# 1.308 29-Mar-2016 kettenis

Check if a device is present (using _STA) in acpi_foundhid. This prevents
us from reporting devices as "not configured" that aren't actually present,
and allows us to remove duplicated code from several drivers.

ok deraadt@, ok jsg@


# 1.307 29-Mar-2016 kettenis

Implement a cleaner way to prevent acpivout(4) from attaching if we have
a platform-specific method of controlling the backlight.

ok millert@, mlarkin@, jung@


# 1.306 28-Mar-2016 kettenis

Adding each and every new acpi device driver to acpi_foundhid() is
gettinga bit of the burden. And it really isn't how our autoconf
framework is supposed to work for busses that can be enumerated. All
the drivers already check for a matching _HID in their attach
function. So we can just drop the checks in acpi_foundhid() and let
the drivers sort themselves out.

Print a "not configured" line for devices for which we don't attach adriver.
This may be a bit much as there are devices for which we will not have a
driver. This will be removed if it causes too much dmesg spam.

ok deraadt@, mlarkin@


Revision tags: OPENBSD_5_9_BASE
# 1.305 17-Jan-2016 jsg

Take acpi_foundhid() outside of #ifndef SMALL_KERNEL so the ramdisk
kernel can find and attach dwiic(4) and sdhc(4).


# 1.304 17-Jan-2016 jsg

remove an unused global variable


# 1.303 14-Jan-2016 kettenis

Attach dwiic(4) on the Intel Bay Trail i2c controllers as well.


# 1.302 12-Jan-2016 kettenis

Improve emulation of PM registers on Hardware-redcued ACPI by emulating
SPL_TYPx, SLP_EN and WAK_STS using the new SLEEP_STATUS_REG and
SLEEP_CONTROL_REG registers. Makes the transition into S5 (powerdown) work
on the ASUS X205TA.

ok deraadt@, mlarkin@


# 1.301 12-Jan-2016 jcs

Add dwiic, a driver for the Synopsys DesignWare i2c controller found
on the Samsung ATIV Book 9 laptop. This initial version only
supports ACPI config/attachment.

Add ihidev, a HID-over-i2c driver largely based on uhidev. dwiic
handles attaching ihidev devices found in ACPI.

Add ims, a HID-over-i2c mouse/trackpad driver to get basic cursor
and button functionality from HID-compliant i2c trackpads.

ok kettenis deraadt


# 1.300 11-Jan-2016 kettenis

Make sdhc(4) attach to hardware IDs "80860F14" and "PNP0FFF".


# 1.299 09-Jan-2016 kettenis

Initial support for Hardware-Reduced ACPI. This skips the legacy mode checks
by emulating the PM_CNT register such that SCI_EN is always enabled. It also
skip attaching acpitimer(4) since the relevant registers are no longer there.

ok deraadt@, mlarkin@


# 1.298 06-Jan-2016 kettenis

Follow the spec and check the SCI_EN bit to see if ACPI has already
been enabled and don't bother enabling it again if it is. This should fix
systems that don't implement legacy mode and therefore set ACPI_ENABLE and
ACPI_DISABLE to zero.

ok mlarkin@, yasuoka@


# 1.297 23-Nov-2015 reyk

Back out the CRITICAL change, turn it back from 10% to 25%. Some
people with old/broken batteries didn't like this change, and the
underlying heuristics are wrong.


# 1.296 22-Nov-2015 reyk

The "CRITICAL" APM battery level, that is exported/faked by the ACPI
stack, is currently set to 25%. With 25% battery left, a typical
modern laptop will have at least one hour runtime left. It is an
arbitrary value, and other APM power layers in the kernel set CRITICAL
to something like 5% or 10%, so set it to 10% for now.

OK mlarkin@


# 1.295 28-Sep-2015 deraadt

In low-level suspend routines, set cold=2. In tsleep(), use this to
spit out a ddb trace to console. This should allow us to find suspend
or resume routines which break the rules. It depends on the console
output function being non-sleeping.... but that's another codepath which
should try to be safe when cold is set.
ok kettenis


# 1.294 06-Sep-2015 deraadt

clarify malloc arguments (base + length...)


# 1.293 28-Aug-2015 deraadt

fairly simple sizes for free(); ok tedu


# 1.292 20-Aug-2015 kettenis

Respect the access size when reading or writing to pci config space.
This version also makes sure writes are properly aligned, preventing
the panics seen with the previous incarnation of this change.

Fixes battery status passthrough in vmware, brightness keys on my x220
and who knows what else.

ok mlarkin@, deraadt@


Revision tags: OPENBSD_5_8_BASE
# 1.291 04-Aug-2015 deraadt

If we are setting ACPI_PM1_BM_RLD do it only once, and remember to
repeat this action at resume time.
ok mlarkin guenther


# 1.290 29-Jul-2015 deraadt

Something subtle in previous revision is locking up suspend or booting
on some laptops. Mailing lists threads mention x220 and Asus machines.
Reverting for now.


# 1.289 21-Jul-2015 kettenis

iRespect the access size when reading or writing to pci config space.
Fixes battery status passthrough in vmware and who knows what else.

Tested by many.

ok mlarkin@, deraadt@


# 1.288 17-Jul-2015 mlarkin

fix comments


# 1.287 26-Mar-2015 jsg

Use the 0x10 extended capability to determine if the trackstick
buttons are wired to the trackpad and need to be re-routed to the
trackstick. Without this change the buttons on 2015 thinkpads
get picked up as extended buttons that show up as scroll up/down.

Remove the X1 Carbon 2015 (LEN0048) and X250 (LEN0046) from the
top button area/soft buttons quirks list. Also avoid using the
quirk list entirely if the capability bit is set.

Based on work from jcs@ with feedback from mpi@
ok mpi@ jcs@


# 1.286 20-Mar-2015 kettenis

The ACPI standard says that a PCI function number of FFFF can be used to refer
to all functions on a device. This makes no sense in the context of accessing
PCI config space. Nevertheless stsp@ has a machine with AML that tries to
do config space reads from a device where _ADR encodes function number FFFF.
This is almost certainly buggy AML, but since Linux (and presumably Windows)
boot just fine on this machine we need to work around this. Linux effectively
treats function FFFF as function 7, which isn't present on this machine.
Instead we simulate a read from a non-existent device by returning a bit
pattern of all ones. Writes to function FFFF will still panic.

ok mlarkin@


# 1.285 16-Mar-2015 deraadt

Reduce use of SMALL_KERNEL in acpi, bringing acpiec driver and more gpe
handling into RAMDISK. This is now possible because the install media
has ample room. The goal is to reduce special cases where we may be
skipping (unknown) important operations...
ok mlarkin kettenis


# 1.284 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.283 07-Feb-2015 deraadt

New framework that allows hibernate to pass in entropy from it's fresh
boot.
ok mlarkin


# 1.282 06-Feb-2015 mlarkin

Fix a hibernate crash on some machines due to unmapping a page that
may not have been mapped previously (in the failure to hibernate case).

Also ensure that the lowmem ptp is mapped in all cases (not just MP).

ok kettenis


# 1.281 17-Jan-2015 deraadt

resume_randomness() before spinning up other cpus...


# 1.280 15-Jan-2015 jsg

Define a new wscons mouse type for Synaptics clickpad devices that lack
physical buttons. This will be used if the acpi pnpid for the mouse
matches a list provided by Synaptics found in Linux.

Combined with the xenocara changes this will remove the need for an xorg
config file for the x240/t440/t540 generation of thinkpads.

ok matthieu@ shadchin@ kettenis@


# 1.279 11-Jan-2015 kettenis

Tolerate checksum errors in ACPI tables (but complain about them). Apparently
there are machines out there with broken BIOSen with such checksum errors in
thier RSDT tables. Windows runs fine on these machines, so it probably does
not check. Linux ignores checksum errors as well (and prints a warning).

ok deraadt@, krw@


# 1.278 18-Dec-2014 deraadt

suspend and resume the RNG (at what looks like the right spots)
ok reyk djm


# 1.277 09-Dec-2014 doug

Replace some malloc(n*size,...) calls with mallocarray().

ok tedu@ deraadt@


# 1.276 06-Dec-2014 mpi

If "machdep.lidsuspend" is not set, do not go back to sleep if the lid
is still closed when resuming. This allows docked laptop to be resumed
without opening them.

ok mlarkin@


# 1.275 23-Nov-2014 mlarkin

If a machine has no _LIDs, then none can be open. This diff fixes a problem
where machines without any _LIDs were going back to sleep after hibernate
due to the recent suspend-if-lid-closed-on-resume diff. Noticed on qemu,
which doesn't have any _LIDs.

discussed with deraadt


# 1.274 18-Nov-2014 krw

Nuke yet more obvious #include duplications.

ok miod@


# 1.273 08-Nov-2014 mlarkin

If resuming from sleep (zzz/ZZZ) and the lid is still closed, go back to
sleep. This prevents accidental lid flex or slight opening in a backpack
from waking the machine up and leaving it resumed (powered on).

ok deraadt@


# 1.272 26-Sep-2014 kettenis

Rework piglet and pig allocation. Currently the piglet gets allocated
deep down in the suspend path, where it is really hard to recover from
allocation failure. So allocate the piglet early on in the suspend path.
Also change the piglet and piglet allocation functions to use km_alloc(9)
instead of doing pmemrange magic. This removes a bunch of code which, in the
case of the piglet allocation, is broken since it results in a NULL pointer
dereference. Also switch the piglet allocation to not wait. If we can't
allocate 16MB of phys contig memory on a halfway modern machine we're almost
certainly under a lot of memory pressure and we're better off not trying to
hibernate anyway.

ok mlarkin@


# 1.271 25-Sep-2014 kettenis

Bring back rev 1.249. Now that mp_setperf() has been fixed, it should be
safe to restore perflevel when resuming. Should fix the issues people
see with the processor running at full speed heating up the machine while
the apm command reports that the CPUs are running at a low speed.

ok dcoppa@, deraadt@


# 1.270 23-Sep-2014 kettenis

Release the acpi lock around calls to wsdisplay_suspend() and
wsdisplay_resume(). The X server may adjust the display brightness just
before/after the VT switch when suspending/resuming. In fact, it may
already be blocked adjusting the display brightness for some reason
unrelated to the suspend request.

The VT switch request will eventually time out, but the delay is annoying
and the failed VT switch will lead to problems after resume.

Needed for a future update of the xf86-video-intel driver.

ok mlarkin@


# 1.269 19-Sep-2014 kettenis

Use config_suspend_all(9).


# 1.268 08-Sep-2014 kettenis

Clear the PME Status bit on PCI devices in response to Device Wake events.
Fixes the interrupt storm on the 2009 Mac mini with WOL enabled on its
nfe(4) interface.

ok mlarkin@


Revision tags: OPENBSD_5_6_BASE
# 1.267 20-Jul-2014 mlarkin

Support hibernating to softraid crypto volumes.

much help and ok from deraadt@


# 1.266 16-Jul-2014 mlarkin

Reenable hibernate RLE support and flush+zero all memory after unpack.


# 1.265 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.264 12-Jul-2014 kettenis

Add a function to drop all clean pages on the page daemon queues and call
it when we hibernate.

ok mlarkin@, miod@, deraadt@


# 1.263 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.262 11-Jul-2014 mlarkin

Flush the buffercache to 16MB on hibernate and restore its previous max
size (kern.bufcachepercent) on resume, for better hibernate performance.

ok beck@


# 1.261 10-Jul-2014 blambert

remove unneeded header

ok mlarkin@


# 1.260 01-Jul-2014 kettenis

Adding an additonal delay after each access to SystemMemory or SystemIO
during autoconf makes no sense.

ok deraadt@


# 1.259 23-Jun-2014 kettenis

SystemMemory is used to access memory mapped registers on some machines, so we
must use the bus_space(9) API to access it instead of memcpy(9). Also make
sure we properly align access to fields that need more than byte alignment.

ok guenther@


# 1.258 21-May-2014 mlarkin

Fix invalid / missing format specifiers in several dnprintf statements
(used only when ACPI_DEBUG is enabled)

From a diff on tech@ by Fabian Raetz, with a minor change by me, tested
on i386 and amd64 with ACPI_DEBUG enabled and disabled.


# 1.257 25-Apr-2014 mlarkin

Defer the free of the hibernate structures until later, when it is safe to
do so.

Freeing the hibernate structures too early leads to inadvertent calls into
splx when we aren't ready for them yet. Found when trapping splx calls in
the resume path.

discussed with deraadt@ at m2k14


# 1.256 13-Apr-2014 mlarkin

Fix a badly worded debug message that implied the entire machine was going
to sleep, instead of just the acpi thread going to sleep.


# 1.255 31-Mar-2014 mpi

Change the order of operations during a suspend/resume cycle and
call bufq_quiesce() after executing the DVACT_QUIESCE handlers.

This should be safe since no disk nor controller drivers have such
handler but it will allow us to detach sd(4) devices attached to a
USB bus.

Another benefit pointed out by kettenis@ is that drivers that need
to read a firmware from the disk should be able to do it at resume
time in a DVACT_WAKEUP handler.

ok kettenis@, deraadt@


# 1.254 13-Mar-2014 dlg

get rid of the assumption that the head of the alldevs list is the
"mainbus" device. this breaks when mpath is enabled because it
attaches before mainbus and therefore takes the head position.

have autoconf provide device_mainbus() which looks up mainbus_cd,
and use that instead.

discussed with deraadt who just wants mpath stuff to move forward
despite there being many ways to shine this particular turd.


Revision tags: OPENBSD_5_5_BASE
# 1.253 21-Feb-2014 deraadt

Do the lid control further on in the resume sequence, since this is
AML, and will do WAITOK operations. While cold, that is a bad idea.
We seem to be safe with respect to the spec, I do not see any event
debounce issues here.
ok kettenis, tested by many


# 1.252 20-Jan-2014 claudio

Instead of checking sc->sc_revision <= 1 check that the
fadt->hdr_revision <= 1. This is a better check since it checks
the version of the proper descriptor. This allows the alix APU
to reboot since those boards have currently a wrong sc->sc_revision.
Committing this now so that it can be tested easier.
With and OK jsg@ also fine by derradt@


# 1.251 24-Dec-2013 kettenis

If the FADT has its SMI_CMD set to zero, assume we're only ACPI-only hardware
and don't need to disable SMI ownership of the ACPI hardware registers.

ok mlarkin@


# 1.250 23-Dec-2013 kettenis

Revert previous commit. It makes resume hang on some MP systems running
GENERIC.MP.


# 1.249 16-Dec-2013 kettenis

Restore hw.setperf upon resume like we do for apm(4) on i386. Seems we need
to do this fairly late in the resume to avoid hangs.

ok deraadt@, mlarkin@


# 1.248 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


# 1.247 06-Nov-2013 mpi

Add support for Power Resources for Dx states and the necessary hook
for PCI devices. This hook should be called twice, before and after
changing the power state of a PCI device.

Before setting the device to the new state, the ACPI layer will notify
every power resources linked to the device for that state and make sure
they are turned "_ON". After changing the state of the device, it will
decrement the reference of every power resources linked to that device
for the old state and turn them "_OFF" if they are no longer referenced.

This fixes the no-USB after resume problem seen on various ThinkPad,
problem initialy diagnosed with Alexander Polakov.

ok kettenis@, deraadt@


Revision tags: OPENBSD_5_4_BASE
# 1.246 01-Jun-2013 mlarkin

Stop printing ACPI wakeup devices in dmesg after the 16th wakeup device.

Helps with VMware VMs that report hundreds of wakeup devices.

ok reyk@, deraadt@


# 1.245 31-May-2013 bluhm

Fix the build for a kernel without wd and pciide in its config.
OK deraadt@ tedu@


# 1.244 30-May-2013 deraadt

Enforce ca_activate tree-walks over the entire heirarchy for all events,
cleaning up some shutdown-hook related code on the way.
(A few drivers related to sparc are still skipped at kettenis' request)
ok kettenis mlarkin, tested by many others too


# 1.243 18-Apr-2013 deraadt

pin the acpi thread to the BSP, because AML and SMI cannot be trusted. I
was certain we were doing this already, but it turns out no...


Revision tags: OPENBSD_5_3_BASE
# 1.242 24-Dec-2012 guenther

In acpiioctl(), Return though the splx() path if suser() returns an error

ok deraadt@


# 1.241 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


# 1.240 04-Oct-2012 ehrhardt

Use information provided by ACPI to attach secondary PCI host bridges.

ok kettenis@


# 1.239 07-Sep-2012 kettenis

Add a new function called acpi_pci_min_powerstate() that returns the lowest
power state for a PCI device for the power state (currently S3 or S4) we're
transitioning into. This function is probably too simplistic; there is a lot
of (silly) complexity in the ACPI spec.

ok deraadt@


Revision tags: OPENBSD_5_2_BASE
# 1.238 13-Jul-2012 pirofti

Disable acpivideo(4) on ASUS laptops.

There are some models that panic whenever an event is received.
Some cope, some don't. In order to get this right acpiec(4) requires
major rework. Until then let machines boot.

Discussed with deraadt@.


# 1.237 11-Jul-2012 mlarkin

for now, restrict hibernate to root in the acpi ioctl kernel interface.
normal rules for controlling this interface through apmd still apply.

ok deraadt@


# 1.236 09-Jul-2012 deraadt

tiny little oops in untested hibernate block


# 1.235 09-Jul-2012 deraadt

clean up ioctl() case statements


# 1.234 09-Jul-2012 deraadt

acpi_disable_onegpe() is not used; ok pirofti


# 1.233 24-May-2012 kettenis

If _MIN > _MAX consider a WordBus resource to be invalid. Fixes a panic on
the IBM x3550-M4.

ok mikeb@


# 1.232 29-Mar-2012 mlarkin

Missing #ifdef in previous diff


# 1.231 29-Mar-2012 mlarkin

Prevent hibernation attempts on machines with unsupported disk controllers.
The same check is also performed later in the hibernate sequence, but by
then it's too late to cleanly unwind (presently).

ok deraadt


# 1.230 26-Mar-2012 deraadt

simplify conditionals in acpi_sleep_state()
ok mlarkin


# 1.229 26-Mar-2012 deraadt

hook in the hibernate request code; half of this diff is from mlarkin
ok mlarkin


Revision tags: OPENBSD_5_1_BASE
# 1.228 20-Sep-2011 deraadt

Very early in suspend while the clock is still ticking realtime,
save the clock back to the rtc. The zaurus already did this.
ok phessler miod kettenis


Revision tags: OPENBSD_5_0_BASE
# 1.227 02-Jul-2011 nicm

kqueue attach functions should return an errno or 0, not a plain 1. Fix
the obvious cases to return EINVAL and ENXIO.

ok tedu deraadt


# 1.226 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.225 16-Jun-2011 pirofti

Import acpitoshiba driver.

This is originally written by Hiroyuki Aizu, ported and integrated
in our tree by Javier Vazquez <javama4 AT gmail> with tweaks and
suggestions by me.

Import prodded by deraadt@ and the lack of testers on tech@.


# 1.224 27-Apr-2011 jcs

attach acpithinkpad to newer lenovo models like the x120e

ok deraadt@


# 1.223 22-Apr-2011 jordan

Fix uninitialzied variables and formatting strings (-Wxxx errors)


Revision tags: OPENBSD_4_9_BASE
# 1.222 02-Jan-2011 jordan

Re-add code for ACPI taskqueue, eliminates need for state vars in acpi softc
Serialize processing ACPI tasks for sleep/powerdown/GPE event
ok deraadt@


# 1.221 31-Oct-2010 guenther

Revert last commit: it breaks resume on Thinkpads
"then please back it out" deraadt@


# 1.220 26-Oct-2010 jordan

Add task queue for ACPI gpe and notify handlers
This fixes eject on Dell Latitude dock and an issue on the Dell Mini
battery update.
ok deraadt


# 1.219 07-Oct-2010 jordan

Create separate functions for enabling wake and runtime gpes.
Remove old suspend/resume gpewalk function
ok deraadt, mlarkin


# 1.218 05-Oct-2010 jordan

split acpi_enable_onegpe to separate enable/disable functions
ok deraadt


# 1.217 05-Oct-2010 jordan

Added acpi_disable_allgpes function to clear GPE status
ok deraadt


# 1.216 05-Oct-2010 deraadt

clean up acpi_softc use more


# 1.215 05-Oct-2010 deraadt

delete a bit of #if 0 code


# 1.214 05-Oct-2010 deraadt

hide use of our global acpi_softc pointer a bit better


# 1.213 29-Sep-2010 deraadt

grab the acpi thread lock very early in acpi thread startup; not that it
is likely that anyone will race us... but why be careless.
ok kettenis


# 1.212 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.211 29-Aug-2010 jasper

- add a check for FWRITE here too.

ok miod@


# 1.210 27-Aug-2010 kettenis

Make PCI Power Management optional, and only enable it when acpi(4) attaches.

ok deraadt@


Revision tags: OPENBSD_4_8_BASE
# 1.209 08-Aug-2010 kettenis

Close race between the acpi thread and wscons ioctls running in process
context. We might rip this out post-release and replace it with a solution
that serializes things in a more obvious way.

ok deraadt@, miod@ and "those crazy texans who ok anything"


# 1.208 08-Aug-2010 deraadt

Report the acpi sleep states when on a RAMDISK; ok krw


# 1.207 07-Aug-2010 kettenis

Enforce acpi register access rescrictions.

ok mlarkin@, deraadt@, tested by marco@


# 1.206 07-Aug-2010 deraadt

upon resume, notify all the acpiac and acpibat drivers to update
their status. some laptops do not do this automatically.
ok kettenis canacar


# 1.205 06-Aug-2010 marco

Always call _PSW on _LID devices that support it to enable lid open to
resume the machine. Conversely disable it when the machine wakes up.

Tested by several ok deraadt


# 1.204 06-Aug-2010 deraadt

As part of a revamp of the PM1/GPE code, I write this diff to look for
unmanaged EN & STS bits in the PM1 register at interrupt time and report
them. As a side effect this splits the STS acknowledgement into two writes
(for power, and sleep) instead of one. The printf that is added (to spot
unmanaged STS bits) has not yet been triggered as far as we know.

Before the "write to PM1 registers at the right offset" diff went in, this
was not neccessary. But newer thinkpads do not have a working soft power
button without this diff.

We have no idea why.
ok mlarkin kettenis


# 1.203 05-Aug-2010 deraadt

PM1 power button events were generating 'fake events' to acpibtn PWRB,
but if there is no such device these were being tossed. Instead, arrange
for all power button handlers to flag a softc variable which is processed
by the thread; so that one place will know to request a powerdown (psignal
to init)
ok marco mlarkin


# 1.202 05-Aug-2010 deraadt

rename a variable which is annoyingly poorly named


# 1.201 05-Aug-2010 mlarkin

Write to the correct offset for various ACPI pmreg registers. Writing to
the correct register does properly clear the wake status bits and fixed
event bits, which allows some machines to have more than 1 suspend cycle.

bug spotted by deraadt@

tested by deraadt@ and myself on a half-dozen machines and others on
hackers@

ok deraadt@, marco@


# 1.200 05-Aug-2010 deraadt

Fix the power button. The pm1 register is 16 bits in size.
My mistake, a while back.
tested by krw


# 1.199 04-Aug-2010 kettenis

Print supported sleep states.

ok marco@, phessler@, deraadt@


# 1.198 03-Aug-2010 mlarkin

Use the proper flag for re-enabling certain hardware events (power btn,
sleep btn). The (incorrect) flag prevoiously used coincidentally had the
same value, so this shouldn't cause any different behavior than before.

tested on a variety of machines (i386, amd64, sp, mp) - no behavioral
change seen

ok deraadt@


# 1.197 28-Jul-2010 marco

Make all gasio failures look and behave the same.


# 1.196 28-Jul-2010 mlarkin

Delay a little longer on suspend before giving up. Fixes machines that
sometimes don't suspend because the sleep transition takes too long
(symptoms like failing to sleep but the backlight stays on).

ok deraadt@


# 1.195 27-Jul-2010 deraadt

remove XXX; EC can now be attached early when needed; ok jordan


# 1.194 27-Jul-2010 deraadt

more brackets feels good


# 1.193 27-Jul-2010 deraadt

XXX annotate a block in acpi_enter_sleep_state() that is pretty worrying


# 1.192 27-Jul-2010 deraadt

horrific indentation that nearly made me puke


# 1.191 27-Jul-2010 mlarkin

Set the sleeping indicator light on machines that support it, in case SMI
doesn't do it for us. Shows the 'moon' LED on older Thinkpads when
sleeping.

Tested on T43p, W500, T510 by me and on X61s by deraadt@.

ok deraadt@


# 1.190 27-Jul-2010 jordan

Early initialization of acpiec if ECDT table exists
Fixes hang when booting thinkpads while docked
ok deraadt


# 1.189 26-Jul-2010 deraadt

remove merge error which was commited


# 1.188 26-Jul-2010 pirofti

Add support for Sony ACPI hotkeys via a new driver: acpisony(4).

Currently it only works for the suspend button and tries to do right for
the brightness events, but I haven't found a Sony laptop that like to do
right so far.

In the future I want to make the brightness keys work on all Sony's and
also add support for the zoom hotkeys and whatever other funky keys I can
find on those things.

Okay deraadt@.


# 1.187 22-Jul-2010 deraadt

pretty large cleanup of SMALL_KERNEL


# 1.186 21-Jul-2010 deraadt

in the acpi GPE handler, late ack level interrupts not edge interrupts.
Duh.


# 1.185 21-Jul-2010 deraadt

Merge the edge and level gpe handlers into one (now possible since the
gpe structure tells us whether the gpe is edge or level)


# 1.184 21-Jul-2010 deraadt

delete #if 0 code we are not going to use; ok marco


# 1.183 20-Jul-2010 deraadt

make the mask code for acpi_enable_onegpe() match other similar code


# 1.182 20-Jul-2010 deraadt

Handle the acpi interrupt controller with proper edge/level handling,
wrapped in spltty to avoid racing against the interrupt controller.
Repair the gpe bit masking code while there, and do operations in the
right order
ok kettenis mlarkin, help from jordan at figuring out the order of
operations.


# 1.181 20-Jul-2010 matthew

When we're preparing to go to sleep, only call bufq_restart() if other
preparations failed.

ok kettenis@, thib@, deraadt@


# 1.180 19-Jul-2010 deraadt

permit compilation with ACPI_DEBUG


# 1.179 19-Jul-2010 deraadt

there is no need for a seperate dsdt_softc pointer; dsdt does not have a
softc but is just a part of the single acpi driver.
ok jordan


# 1.178 19-Jul-2010 deraadt

acpi_delay() is no longer needed; ok jordan


# 1.177 18-Jul-2010 mlarkin

Clear event status on resume, to avoid going back to sleep/reboot/shutdown
immediately after resume. Tested by a bunch of people on hackers@ on
various hardware. Fixes at least Toshiba NB200 and Gateway LT3103u.

ok marco


# 1.176 13-Jul-2010 deraadt

rename some thread related interfaces so they are logical


# 1.175 10-Jul-2010 jordan

Verify _ADR bdf is correct (some systems return 0xffff)
Works on Dell x9xx and Studio 1555


# 1.174 09-Jul-2010 jordan

Clear the noise


# 1.173 08-Jul-2010 jordan

Add mapping for ACPI device to PCI bdf (match autoconf tree)
Simplify resource parsing function to use buffer argument
Convert namespace linked lists to use queue macros
ok marco@, deraadt@


# 1.172 06-Jul-2010 deraadt

Use spltty() to lock downcalls from apm against the information being
modified by the acpi thread.
ok kettenis


# 1.171 06-Jul-2010 deraadt

Don't use a workq to do the suspend, because it races aginst the acpi
thread. Instead, just tell the acpi thread to do the suspend for us.
This makes apmd & zzz work correctly. While here, have acpithinkpad
attempt to post the event to apm before prompting the suspend itself.
ok kettenis marco mlarkin


# 1.170 05-Jul-2010 mlarkin

Reset register SCI_EN on resume. The ACPI spec clearly states we don't have
to do this, but many machines don't, or forget to, which leads to all sorts
of problems on resume (broken battery stats, overheating, failure to resume
more than once, etc).

tested by many, ok marco@, deraadt@


# 1.169 01-Jul-2010 thib

do bufq quiesce again, since vnd/the bufq_done call in
biodone has been fixed.


# 1.168 01-Jul-2010 jordan

Backout recent AML changes, breaks on R210 and others


# 1.167 01-Jul-2010 jordan

Simplified aml_parse_resource call, cleanup code
ok marco


# 1.166 01-Jul-2010 jordan

Fix subordinate bus # for multi-root PCI buses
ok marco, kettenis


# 1.165 30-Jun-2010 thib

Disable/partially backout the bufq quiesce changes as this
is causing havoc with vnds and release must be buildable.


# 1.164 29-Jun-2010 deraadt

Fix ramdisks (new code was placed inside a SMALL_KERNEL chunk)


# 1.163 29-Jun-2010 jordan

Add support for mapping ACPI to PCI devices
ok kettenis, deraadt


# 1.162 29-Jun-2010 kettenis

Call bufq_quiesce() on suspend to stop all I/O and bufq_restart on resume.
Makes suspend/resume work much better while doing disk I/O.

ok mlarkin@


# 1.161 29-Jun-2010 tedu

60 is a more reasonable guess for number of minutes in an hour than 100.
spotted by TeXitoi. ok deraadt


# 1.160 27-Jun-2010 jordan

Add _acpi_matchhids routine, precursor to pci device match


# 1.159 27-Jun-2010 mlarkin

Clear ACPI fixed event status on resume (power buttons/etc) so that some
machines don't immediately resume after suspending to S3.

Tested by kettenis@ and I on 5 or 6 machines, deraadt@ "yeah, looks good"


# 1.158 27-Jun-2010 jordan

Cleaned up acpi_maptable, adds to table list directly


# 1.157 07-Apr-2010 deraadt

If we have done a wsdisplay_suspend(), and ACPI decides it does not want to
suspend, we must roll back by calling wsdisplay_resume() or the virtual
console switching will be locked forever
ok oga


# 1.156 07-Apr-2010 kettenis

Make suspend/resume work on MP machines (running an MP kernel). Joint work
from mlarkin@ and me, with some amd64 fixes thrown in by deraadt@

ok marco@, deraadt@, pirofti@, mlarkin@


# 1.155 31-Mar-2010 kettenis

Bring /dev/apm support in line with apm(4). Make the suspend button send
a suspend request event to apmd(8) instead of suspending immediately. Also
keep track of whether /dev/apm and /dev/apmctl are currently open, such that
we can still suspend immediately if apmd(8) isn't running.

ok deraadt@, marco@, pirofti@, jsing@, oga@


# 1.154 30-Mar-2010 oga

Prevent the apmd/x races for good.

When we hit suspend time, go through all wsdisplays on the system. if
they are in mode MAPPED, but not MODE_DUMBFB then if possible do a full
vt switch to a !mapped vt, and prevent switching back until resume time.
This has to be called from MD code because this involves userland
running so that X can run the vt switch signal handler. This way, any
case where we are using the "poke registers from userland" model, we
will not be on the hardware when we go down, so the kernel can actually
handle thing properly.

Tested on several acpi laptops (by kettenis@ and ian@), x40 (me and
beck@ at LEAST) and zaurus (me). Maybe others, but if so I forgot who at
this time..

Idea from deraadt somewhere over the Faroe Islands (I thought of a
similar thing myself a while ago). Much prompting from him. Ok and
comments miod@


# 1.153 25-Mar-2010 oga

Fix & vs. && in the kqfilter code.

Based on a comparison with the apm code.

ok deraadt@, kettenis@


Revision tags: OPENBSD_4_7_BASE
# 1.152 26-Nov-2009 mlarkin

Visual (and on some machines, audio) feedback while the machine is
resuming via calls to aml _SST method. This also makes the "moon" LED on
thinkpads to blink during resume and cease when resume is completed.

ok pirofti@


# 1.151 26-Nov-2009 deraadt

Toggle into "cold" when we we disable interrupts, because tsleeping
in the AML interpreter and context switching into userland processes
tends to sometimes pop the ACPI trampoline page out of our address
space. Makes lots more machines work.
ok kettenis mlarkin


# 1.150 24-Nov-2009 jsg

in the resume path evaluate _WAK before dealing with device drivers
like the spec says, not after.

ok mlarkin@


# 1.149 24-Nov-2009 deraadt

abort suspends on SMP systems until that gets written; ok mlarkin


# 1.148 23-Nov-2009 mlarkin

pci should return the result of it's children's suspend/resume calls back
to its parent.

handle suspend failure case and unwind if devices have an activate function
that returns failure.

ok deraadt@, kettenis@


# 1.147 23-Nov-2009 mlarkin

acpi* devices need to call their children on suspend/resume.
ok deraadt@


# 1.146 23-Nov-2009 pirofti

Remove ACPI_SLEEP_ENABLED checks.

This enables by default the suspend/resume paths in the kernel.

Okay deraadt@.


# 1.145 23-Nov-2009 deraadt

knf and spacing that are super annoying


# 1.144 23-Nov-2009 mlarkin

Hook up Theo's new children suspend/resume function, as well as more ACPI
suspend/resume glue.

ok deraadt@


# 1.143 26-Oct-2009 deraadt

Do not do strncmp comparisons on dv_xname because longer device names which
look similar could arrive in the future. Instead, compare directly against
dv_cfdata->cf_driver->cd_name
Issue originally spotted by miod


# 1.142 26-Oct-2009 jordan

Verify checksum+address when loading ACPI tables.
Some systems had invalid entries in RSDT/XSDT.
ok marco@


# 1.141 23-Jul-2009 cnst

New aibs(4) driver for ASUSTeK AI Booster (ACPI ATK0110) hardware monitoring,
with sensor state support through limits provided by the ACPI.

Tested on several ASUS motherboards kindly networked by Sam Fourman Jr.

ok deraadt marco jordan


Revision tags: OPENBSD_4_6_BASE
# 1.140 03-Jun-2009 pirofti

Import acpi power resource driver. This is needed for suspend/resume logic.

Okay jordan@ and marco@.


# 1.139 03-Jun-2009 pirofti

Add acpivideo support. This adds brightness support for all laptops
except thinkpads, they will use the acpithinkpad driver. The driver is
also hooked into wsconsole. So brightness can be adjusted via:

$ wsconsctl display.brightness=<percentage>

This is very helpfull on some laptops that have a nasty bios and get two
steps instead of one when pressing the brightness button.

Tested on various dell, fujitsu, acer, samsung and other laptops.

Okay marco@, miod@. Suggestions from kettenis@.
Lots of reviews and help from miod@, thanks!


# 1.138 03-Jun-2009 jordan

Added ejectable bay for IDE cdroms
ok marco@


# 1.137 30-Apr-2009 marco

Oga changed apm ioctls and forgot to grep. Temporary work around by hitting
the sleep button when we get any request. This interface will chnage at
some point.
ok oga


# 1.136 19-Apr-2009 krw

Call acpi_sleep_walk() when powering down just like when entering
any other sleep state. This tells acpi wake devices to not wake up
the box until manual powering up resets their status. Makes my Dell
GX520 stay powered off in response to 'halt -p'.

In snaps for a while.

ok marco@ (several times) deraadt@


# 1.135 17-Apr-2009 pirofti

Add device ranking support for future sleep/wake developments.

okay marco@


# 1.134 11-Mar-2009 jordan

Cleanup.. use aml_evalinteger instead of aml_evalname
ok marco@


# 1.133 10-Mar-2009 jordan

Fix cut/paste error.. call _BFS not _PTS
ok marco@


Revision tags: OPENBSD_4_5_BASE
# 1.132 21-Feb-2009 marco

overzealous cut/paste; found by Vladimir Kirillov <proger@uaoug.org.ua>


# 1.131 20-Feb-2009 marco

Oops didn't mean for Debugger() to remain there.


# 1.130 19-Feb-2009 marco

suspend/resume bits so that we can develop this in tree. This is disabled.
code from mlarkin and me
help from art,toby,jordan and several others
ok jordan, go for it deraadt


# 1.129 10-Feb-2009 jordan

Fix null pointer for shutdown if acpi not enabled
ok marco@


# 1.128 04-Feb-2009 kettenis

Some systems that claim to be ACPI 2.0 apparently don't provide an XSDT
address. Make the code fall back on the RSDT on those systems.

From Joerg Niendorf <p3y80@internode.on.net>. Fixes PR 5986.


# 1.127 17-Jan-2009 krw

A couple of stray malloc/memset -> malloc(M_ZERO), a bit of knf and
lose a couple of unnecessary casts. No functional change.

Feedback from kettenis@

ok kettenis@ marco@


# 1.126 06-Nov-2008 marco

Collapse all sanity checks of hids into one function.

Originaly from jordan


Revision tags: OPENBSD_4_4_BASE
# 1.125 02-Jul-2008 fgsch

add acpivideo. for now almost an empty skel so others can work on
suspend/resume. eventually it will also manage output switching and
brightness where supported. prodded by marco@


# 1.124 11-Jun-2008 marco

Fix $OpenBSD$ for once and for all


# 1.123 08-Jun-2008 deraadt

aaa_dev was being set from an uninitialized variable; ok toby


# 1.122 07-Jun-2008 marco

Kill ACPIVERBOSE. It was never used and is worthless to boot.

comments jmc
ok toby


# 1.121 06-Jun-2008 marco

Shrink acpi some more for floppies and while here re-enable acpi on the
floppies. Fixes crazy sony vaio kettenis has.

tested by deraadt
ok deraadt jordan kettenis


# 1.120 01-Jun-2008 marco

Fix \n nit in SMALL_KERNEL.


# 1.119 01-Jun-2008 marco

Re-enable new parser. Lots of testing by various folks. Fits all media
now too.

ok kettenis deraadt


# 1.118 16-May-2008 dlg

back out jordans new parser.

it didnt have enough oks from the right people, it did break machines
that were previously working, and it bloated the RAMDISKS and install
media beyond the constraints of the media.

requested by deraadt@


# 1.117 14-May-2008 jordan

Adding new ACPI Parser code
Old guts of code still exists, needs to be torn out next
ok marco@
ok brad@


# 1.116 27-Apr-2008 jcs

add acpithinkpad(4), an acpi support driver for ibm/lenovo
thinkpads.

provides functionality that the bios did on older, non-acpi
thinkpads like respond to screen brightness keys and toggle the
bluetooth device on and off.

ok deraadt@ and marco@


# 1.115 24-Apr-2008 jsing

Introduce acpiasus(4), a driver for the ACPI based hotkeys found in many
ASUS laptops (including the ASUS EeePC) - largely based on NetBSD's
asus(4) driver. On the ASUS EeePC this allows us to enable/disable
wireless, change screen brightness and use the volume keys.

ok jsg@, weingart@


# 1.114 12-Apr-2008 kettenis

Get rid of acpi_s5 global variables; simply send SIGUSR2 instead of SUGUSR1
to tell init(8) to power down the machine.

ok krw@


Revision tags: OPENBSD_4_3_BASE
# 1.113 21-Jan-2008 jsg

Check against RESET_REG_SUP in FADT before doing the acpi reset dance
after all, not doing so appears to break more machines than it fixes.

ok marco@


# 1.112 05-Jan-2008 kettenis

Never attach more acpicpu(4) devices than cpu(4) devices. Always attach
the first acpicpu(4) device on non-MULTIPROCESSOR kernels, regardless of its
ACPI processor ID.

ok mikeb@


# 1.111 05-Dec-2007 deraadt

Hang acpi(4) below bios(4) instead of mainbus(4). This lets us move acpi(4)
before pcibios(4), and then.... if acpi(4) attaches, skip pcibios(4) since
it messes the machines up. Fixes claudio's HP dl320 G5, and almost assuredly
others.
ok kettenis


# 1.110 05-Dec-2007 deraadt

repair acpiopen/acpiclose and friends to handle the /dev/apmctl interface,
so that apmd(8) can actually work correctly against acpi(4), and thus,
even do the fancy -C/-A stuff; also tested by jmc


# 1.109 02-Dec-2007 fgsch

fix the trailing space for real now.


# 1.108 02-Dec-2007 fgsch

avoid trailing spaces on output; marco@ ok.


# 1.107 27-Nov-2007 kettenis

Fix off-by-one that made us attach acpicpu0 and acpicpu1 on GENERIC kernels.

ok marco@


# 1.106 25-Nov-2007 jsg

When available and ACPI is enabled use the ACPI reset method
to reboot. This is required for some quirky sis machines
and likely others to reboot.

Tested by many. 'commit early commit often' deraadt@


# 1.105 19-Nov-2007 deraadt

sigh, amd64 still uses that stupid acpi_enabled variable...


# 1.104 19-Nov-2007 deraadt

softc's come initialized to 0, you know...


# 1.103 19-Nov-2007 deraadt

store revision; from jsg


# 1.102 19-Nov-2007 kettenis

Initialize sc_facs earlier.

ok deraadt@, beck@, canacar@


# 1.101 16-Nov-2007 deraadt

fix the bus_space #define nightmare, so that amd64 and i386 are much more
uniform. as a result shared code like acpi needs less #ifdef's
ok marco kettenis


# 1.100 15-Nov-2007 deraadt

remove the #ifdef ACPI_ENABLE. the EC codepath is either safe enough (and
will be improved), or it is unsafe because AML assumes it must be there
ok almost everyone


# 1.99 15-Nov-2007 mikeb

Make acpicpu(4) attach to the existing CPUs only.

Problem reported by form@. Fix idea by kettenis@.
Lots of help from deraadt@. Tested by deraadt@, form@ and me.

ok deraadt marco


# 1.98 14-Nov-2007 deraadt

spacing


# 1.97 12-Nov-2007 deraadt

correct : vs , in attach-time messages


# 1.96 06-Nov-2007 deraadt

all ACPI_ENABLE kernels need acpi_interrupt()


# 1.95 06-Nov-2007 deraadt

move acpi_read_pmreg() and acpi_write_pmreg() out of SMALL_KERNEL so
that acpi ramdisks can compile (obvious solution)


# 1.94 05-Nov-2007 canacar

Establish the acpi interrupt earlier. Fixes problems with some
GENERIC.MP kernels not getting acpi interrupts. This is a
workaround until the real issue with late establishment is fixed.
ok weingart@, beck@, gwk@


# 1.93 03-Nov-2007 jordan

Added support for displaying wakeup devices
ok beck@,weingart@,gwk@


# 1.92 11-Oct-2007 kettenis

When walking the tree yo find _PRT methods, check whether the device is
actually enabled and functioning. Similar to what we do for _INI.

ok weingart@, beck@


# 1.91 08-Oct-2007 krw

More simple memset(,0,) -> M_ZERO changes. In this batch move to
size(*p) as the first malloc() parameter where p is declared locally
and thus easy to check. Add M_ZERO to gpe_table allocation in acpi.c
even though there is no obvious bzero or memset nearby.


# 1.90 13-Sep-2007 mk

Remove unnecessary memset() (see aml_evalnode(9)) and a bit of KNF.

ok toby


# 1.89 13-Sep-2007 weingart

Implement ACPI 6.5.1 spec tree walk for _STA and _INI.
Fixes mk's laptop. No regressions so far (thank you to the
testers).

ok gwk@, mk@, marco@


Revision tags: OPENBSD_4_2_BASE
# 1.88 17-Apr-2007 mk

Now that acpidock no longer hangs machines during boot (at least
according to the reports I've been getting) it should be safe to enable
it again, so remove the #if 0 stuff around it.

ok marco


# 1.87 11-Apr-2007 jordan

Added changes for C-State
ok marco@


# 1.86 26-Mar-2007 marco

Calculate minutes left more accurately.

From giovanni <qgiovanni@gmail.com>


Revision tags: OPENBSD_4_1_BASE
# 1.85 03-Mar-2007 mk

Stop looking for acpidock until we fix that stuff that breaks all pre-60
series ThinkPads with acpidock(4) enabled.


# 1.84 22-Feb-2007 marco

We were getting no interrupts in .MP. There are 2 fixes; one in
acpi_machdep.c and move the interrupt establishment down after ioapic
has been initialized.

ok jordan


# 1.83 21-Feb-2007 kettenis

Make an acpi-enabled GENERIC.MP work on systems that only support PIC mode.

ok marco@


# 1.82 19-Feb-2007 jordan

Added acpi_map_address function for mapping GAS/IO registers
ok marco@


# 1.81 18-Feb-2007 jordan

Added dynamic perfect hash for opcodes
ok marco@


# 1.80 18-Feb-2007 marco

Shave off another K pre compression.
Shuffle SMALL_KERNEL functions around so that we only have a single #ifndef


# 1.79 17-Feb-2007 marco

Add SMALL_KERNEL throught acpi to make it fit on boot media.
ok deraadt


# 1.78 13-Feb-2007 marco

Add smarter _INI code. This helps with some dissapearing devices.

ok kettenis jordan


# 1.77 31-Jan-2007 gwk

Introduce acpi_hasprocfvs thats "ACPI Has Processor Frequency and Voltage
Scalling" a global flag set by acicpu attach to indicate that ACPI is
handling fvs through the _PSS objects. This will fix disappearing
EST/powernow when booting kernels with acpi enabled, as acpicpu isnt
compiled into GENERIC yet. Also in cases where acpicpu is built in and
legacy EST/powernow work but acpi is missing the _PSS object, the legacy
routines will be used.

tested by dim@, and Nick Nauwelaerts <nick AT nauwelaerts.net>
ok marco@, dim@


# 1.76 22-Jan-2007 mk

Actually look for docking stations by looking for _DCK nodes.

ok marco


# 1.75 02-Jan-2007 marco

On some systems EC must be attached as the first device. Move EC from
generic _HID devices to its own routine to force first attach. Bug found
and debugged by joris@

Code mostly written by joris@


# 1.74 26-Dec-2006 marco

Add polling to devices that require it such as acpibat and acpitz.
Use the same tsleep/wakeup mechanism as the interrupt code to keep it non-
concurrent.


# 1.73 21-Dec-2006 deraadt

even more knf love


# 1.72 21-Dec-2006 marco

Remove some leftover acpiec gpe/interrupt code.


# 1.71 20-Dec-2006 deraadt

oops, name already used


# 1.70 20-Dec-2006 deraadt

rename __acpi_enable_gpe(); ok marco


# 1.69 20-Dec-2006 deraadt

acpi_unmap_pmregs() is not used, ok marco


# 1.68 19-Dec-2006 deraadt

simplify some low-level methods substantially (shrinking code); ok marco jordan


# 1.67 06-Dec-2006 mk

malloc(..., M_NOWAIT) with no NULL check is bad. If allocation fails,
complain, clean up, and bail out of attach.

input and ok gwk


# 1.66 28-Nov-2006 jordan

Fixed clearing arg for acpi_init_pic
ok kettenis@


# 1.65 27-Nov-2006 kettenis

Don't spam dmesg.


# 1.64 27-Nov-2006 jordan

Fixes for acpiprt routing
Post-parse fixup for namerefs
ok kettenis@


# 1.63 25-Nov-2006 marco

Don't handle acpiev events inside the gpe handler since this causes deadlocks.

ok jordan@


# 1.62 15-Nov-2006 kettenis

Hook up acpi apic interrupt routing support to generic acpi code.

ok jordan@


# 1.61 02-Nov-2006 marco

Kill acpiec attach function and add its functionality to the generic _HID
device attach

prodded and ok jordan


# 1.60 02-Nov-2006 marco

Fix comments to reflect reality.


# 1.59 19-Oct-2006 marco

Replace ACPI ioctls with APM ioctls. This makes ACPI talk the same protocol
as APM therefore userspace can now retrieve power information in the same
fashion as all other apm arches (zaurus, macppc, i386 etc).

To use force the use of acpi device like so: apmd -f /dev/acpi

ok jordan@


# 1.58 19-Oct-2006 jordan

Fixed GPE debug levels
Fixed ref counter on temporary bufferfields
DSDT parsing fully working on IBM Thinkpad
ok marco@


# 1.57 15-Oct-2006 jordan

Removed old GPE handler code remnants
ok marco@


# 1.56 12-Oct-2006 jordan

Massive ACPI changes
Fixed ACPI leaks
new ACPI API
ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.55 30-Jun-2006 gwk

KNF, No functional change,
"go at it" marco@


# 1.54 30-Jun-2006 jordan

Support for calling Embedded Controller via new GPE interface
ok marco@


# 1.53 30-Jun-2006 jordan

Added new code for GPE handling
Also works with ACPIEC; need to fixup acpiec.c to remove original interrupt handler
ok marco@


# 1.52 30-Jun-2006 jordan

Added offset argument for acpi read/write pmreg routines (new GPE code)
ok marco@


# 1.51 31-May-2006 todd

do not call acpi_powerdown() unless acpi has successfully initialized
ok gwk@ canacar@


# 1.50 29-May-2006 canacar

Add embedded controller support.
ok marco@ deraadt@ testing & ok gwk@


# 1.49 19-May-2006 canacar

Minimal thermal zone driver. Monitors thermal zone temperature,
shuts down the system if the 'critical temperature' is reached.
ok marco@


# 1.48 11-Apr-2006 marco

KNF.


# 1.47 11-Apr-2006 gwk

Add acpi_resume(struct acpi_softc *sc), which calls some AML methods,
detected at attach time, currently unused.

ok marco@


# 1.46 11-Apr-2006 gwk

Discover some ACPI sleep related methods, and call them at the
appropriate time in acpi_enter_sleep_state().

ok marco@


Revision tags: OPENBSD_3_9_BASE
# 1.45 26-Feb-2006 marco

Add acpicpu as a device.

ok jordan@


# 1.44 23-Feb-2006 marco

remove dead function.

ok jordan@


# 1.43 22-Feb-2006 marco

Print out all tables it found during autoconf. We'll need this data in
the future.


# 1.42 22-Feb-2006 marco

Make dmesg less verbose.

ok jordan.


# 1.41 22-Feb-2006 jordan

Added parsing of SSDT tables
ok marco@


# 1.40 22-Feb-2006 jordan

Added new aml_register to notify by device id
Moved powerdown code to button device
ok marco@


# 1.39 21-Feb-2006 marco

Readjust debug levels that crept in too low over the last few days.
KNF


# 1.38 21-Feb-2006 marco

Rig the power button so that the machine shuts down correctly.


# 1.37 20-Feb-2006 marco

Add acpibtn device. Remains disabled for now.


# 1.36 19-Feb-2006 jordan

Added code for aml_notify GPE notification callback
ok marco@


# 1.35 19-Feb-2006 grange

Fix acpi_enter_sleep_state() by replacing current ugly hack
with mostly correct sleeping state transition code.

ok brad@ marco@


# 1.34 19-Feb-2006 marco

jordan:
Enable interrupts.
Added peripheral code to make this work.

marco:
Replace softintr and other types of interrupts with a handler with context.
Fixed interrupt magic for GPEs.
Some KNF

This is work done by jordan and I but has to be a single commit.


# 1.33 17-Feb-2006 marco

Remove earlier introduced delays. We really only need them during autconf.

ok jordan@


# 1.32 17-Feb-2006 marco

Add a generic delay function as per the ACPI spec.

from jordan@


# 1.31 17-Feb-2006 marco

Use tsleep instead of delay when !cold.


# 1.30 17-Feb-2006 marco

Argh, we need delays between reads/writes. The backend reads (i2c most likely)
are so slow that values come back as 0 sometimes. We have to do this
despite the spec saying we shouldnt have to. Guess having a faster
parser than others can hurt sometimes.

Idea from jordan.


# 1.29 17-Feb-2006 jordan

Fixed code for handling _INI
Rearranged debug statements
ok marco@


# 1.28 16-Feb-2006 jordan

Added stub for calling _INI
Fixed aml_dowhile for NULL return
ok marco@


# 1.27 16-Feb-2006 jordan

Fixed parser/evaluator to retrieve battery info+state
Cleanup of parser
Dell laptops return battery status
ok marco@


# 1.26 06-Feb-2006 brad

fix a typo so this will compile on amd64.


# 1.25 04-Feb-2006 marco

When ACPI_ENABLED wasn't set but the devices were it had an unknown variable.

Pointed out by ian@.


# 1.24 03-Feb-2006 jordan

Rewrote aml parser yet again...
Fixed bus_space_map for i386/amd64
Added _BST and _BIF to acpibat to get status/info
ok marco@


# 1.23 20-Jan-2006 jordan

More evaluator changes
Removed old evaluator code
Fixed _HID value
ok marco@


# 1.22 19-Jan-2006 jordan

Added support for read/write of PCI Config space
Help jason@
ok marco@


# 1.21 18-Jan-2006 jordan

Added new evaluation routines
Added helper methods for buffer/field evaluation
Fixed broken indentation on dsdt.c

ok marco@


# 1.20 17-Jan-2006 jordan

Major overhaul of the aml parser. Evaluater temporarily disabled.

ok marco@


# 1.19 06-Jan-2006 grange

Check for ACPI control availability only if ACPI_ENABLE defined.


# 1.18 06-Jan-2006 grange

Informative error messages on attach, prettier dmesg.


# 1.17 06-Jan-2006 grange

printf -> dnprintf conversion for debug messages.


# 1.16 05-Jan-2006 grange

Find SLP_TYPx values and store them for future use in
acpi_enter_sleep_state().


# 1.15 05-Jan-2006 grange

Random KNF and cleanup


# 1.14 05-Jan-2006 grange

Provide symbolic names for the acpi sleeping states and use it.


# 1.13 28-Dec-2005 marco

Moved DEVNAME to a acpidev.h

ok jordan@


# 1.12 18-Dec-2005 sturm

add back comment about wrong code

ok marco


# 1.11 16-Dec-2005 marco

Remove test for kbd_reset. Prompted deraadt@, ok jordan@


# 1.10 16-Dec-2005 jordan

Added hooks for powerdown/power button press


# 1.9 16-Dec-2005 jordan

Cleaned up common power management register i/o
Added enter sleep state ioctl
Hooked power button interrupt
ok marco@


# 1.8 16-Dec-2005 marco

Remove autoconf chatter


# 1.7 16-Dec-2005 jordan

Moved DSDT AML parsing into parent ACPI driver
ok marco@


# 1.6 14-Dec-2005 marco

Add AC device glue.


# 1.5 14-Dec-2005 marco

Adjust debug printing levels to something more sane.


# 1.4 13-Dec-2005 marco

Add glue to attach devices to apci.


# 1.3 07-Dec-2005 marco

KNF


Revision tags: OPENBSD_3_8_BASE
# 1.2 10-Jul-2005 grange

Spacing cleanup.


# 1.1 02-Jun-2005 tholo

Start on a basic ACPI framework -- does not do much more than read out the
ACPI tables into kernel memory and attach ACPI and HPET timers currently.

In order to test this code, enabling the devices in GENERIC as well as
the ACPI_ENABLE option is needed. This code does not do any thermal
control yet, so this should be done with care depending on the platform.

In the tree so more people can contribute to making this more fully
featured.

Ok niklas@ grange@ tedu@


# 1.380 07-Apr-2020 visa

Abstract the head of knote lists. This allows extending the lists,
for example, with locking assertions.

OK mpi@, anton@


# 1.379 03-Apr-2020 mpi

Fix incoherencies of errors returned by various *kqfilter routines.

EOPNOTSUPP is returned when there's no "filterops" for a given fd.
EINVAL when the requested filter isn't supported by the "filterops".
ENXIO means the underlying device is no longer valid/present.

ok visa@


# 1.378 20-Feb-2020 visa

Replace field f_isfd with field f_flags in struct filterops to allow
adding more filter properties without cluttering the struct.

OK mpi@, anton@


# 1.377 31-Dec-2019 visa

Use C99 designated initializers with struct filterops. In addition,
make the structs const so that the data are put in .rodata.

OK mpi@, deraadt@, anton@, bluhm@


# 1.376 31-Dec-2019 mpi

Convert infinite sleeps to {m,t}sleep_nsec(9).

ok kn@


# 1.375 31-Dec-2019 jsg

include sys/sensors.h for sensor use
don't depend on indirect inclusion from acpidev.h


Revision tags: OPENBSD_6_6_BASE
# 1.374 07-Sep-2019 kettenis

Revert acpipci(4) changes. Too many systems are broken. We'll try again
after 6.6 as been released. The acpireg.h change stays behind.


# 1.373 28-Aug-2019 kettenis

Use ACPI information to attach PCI busses like we do on arm64. There are a
few additional quirks though, and attaching the PCI busses is delayed to
replicate the existing code more closely. That may be changed in the
future. Also tweak how we handle MSI support and respect to ACPI flag
that says we shouldn't attempt to use MSIs.

Some fallout is expected.

ok patrick@


# 1.372 27-Aug-2019 deraadt

On amd64 the bootup and resume paths for SP/BP/SP were not performing
msr/pat, clock-startup, firmware upgrade, fpu initialization, and cpu
soft-state initialization in the same order. That kind of chaos is
crazy and fragile. Improve the acpi resume call sequence and unify order
of the operations.


# 1.371 02-Jul-2019 jcs

acpi_set_gpehandler: turn edge arg into flags, add support for
passing GPE_DIRECT which directly executes the supplied callback and
clears a level interrupt rather than going through acpi_taskq which
can be slow when other driver callbacks are in the queue.

This should not be used for GPE handlers that need to do anything
ACPI-related.

ok kettenis


# 1.370 10-Jun-2019 kettenis

Print proper ACPI version number.

ok mlarkin@


# 1.369 08-Jun-2019 kettenis

When establishing the mapping between ACPI device nodes and PCI devices,
skip PCI host bridges and devices that aren't there.

ok patrick@


# 1.368 07-Jun-2019 kettenis

Introduce acpi_getsta() and use it to eliminate some duplicated code.

ok patrick@


# 1.367 12-May-2019 kettenis

Make sure the extended PM register descriptions actually exist before
using them. Fixes machines with ancient ACPI 1.0 (such as recent
hypervisors).

ok deraadt@


# 1.366 12-May-2019 kettenis

Add support for mapping ACPI PM registers using the "extended" gasio
representation from the FADT. Mostly fixes the Lanner NCA-1510.

ok lteo@, mlarkin@, deraadt@


# 1.365 11-May-2019 lteo

Fix incorrect error message.

ok kettenis@


# 1.364 08-May-2019 tedu

free size. the allocation is the header plus the length.


Revision tags: OPENBSD_6_5_BASE
# 1.363 04-Apr-2019 kettenis

Avoid potential null-pointer dereference. Found by Kent R. Spillner.

ok deraadt@, jsg@


# 1.362 20-Jan-2019 jsg

fix SMALL_KERNEL build after changes in rev 1.361
reported by naddy@ ok deraadt@


# 1.361 19-Jan-2019 tedu

Add a pwraction sysctl that controls what the power button does on acpi.
By default, nothing changes -- shutdown is initiated. But allows turning
power button into a sleep button if desired.
(grudging) ok from a few parties


# 1.360 26-Oct-2018 kettenis

Add acpipci(4) on amd64. For now this only calls the PCI-specific _OSC
method to let the ACPI implementation know what features we support.


Revision tags: OPENBSD_6_4_BASE
# 1.359 30-Aug-2018 kettenis

Move softbutton check before the check that skips attaching certain acpi
devices. Fixes regression caused by matching on _CID in addition to
matching on _HID.

ok matthieu@, mlarkin@


# 1.358 25-Aug-2018 kettenis

Match ACPI devices based on _CID if no match for _HID is found.

ok mpi@


# 1.357 19-Aug-2018 kettenis

Add support for multiple PCI segments. Only really implemented for arm64
for now as amd64/i386 firmware still caters for legacy OSes that only
support a single PCI segment.

ok patrick@


# 1.356 03-Aug-2018 kettenis

Let ahci(4) match on _CLS instead of _HID when attaching at acpi(4). Avoids
having to add many more _HID entries to the match table.

ok deraadt@, mlarkin@


# 1.355 10-Jul-2018 kettenis

Make legacy interrupts work in acpipci(4).

ok patrick@


# 1.354 05-Jul-2018 kettenis

Add acpipci(4), a driver that supports generic ECAM-compatible PCI host
bridges based on information provided by ACPI.

ok mlarkin@


# 1.353 04-Jul-2018 kettenis

Properly pass around the PCI "chipset tag" in acpi(4) and refactor
acpimcfg(4) to call an MD initialization functions that sets up a tag for
PCI ECAM.

ok guenther@, mlarkin@, krw@


# 1.352 01-Jul-2018 kettenis

Move acpi(4) attach glue into acpi_machdep.c.

ok guenther@, deraadt@


# 1.351 01-Jul-2018 kettenis

Add support for _DSD Device Properties.

ok mlarkin@


# 1.350 30-Jun-2018 kettenis

Add intr_enable() function, intended for MI use to amd64 and i386 and use
this in the acpi(4) suspend/resume code paths.

ok deraadt@


# 1.349 29-Jun-2018 kettenis

Use <stdint.h> types.

ok mpi@, jung@, krw@, deraadt@


# 1.348 29-Jun-2018 mlarkin

ACPI: Allow (with warning) GPE handler reassignment, instead of returning
a failure code.

ok kettenis


# 1.347 26-Jun-2018 mlarkin

knf


# 1.346 26-Jun-2018 kettenis

Let acpi(4) pass down the bus dma tag.

ok deraadt@


# 1.345 25-Jun-2018 kettenis

Reorganize acpi(4) code a bit in preparation for upcoming arm64 support.

ok deraadt@


# 1.344 20-May-2018 kettenis

Wake up the acpi thread after scheduling a gpio event.

ok mpi@


# 1.343 17-May-2018 kettenis

Sprinkle some #ifndef SMALL_KERNEL such that RAMDISK kernels cuild again.

pointed out by stsp@


# 1.342 17-May-2018 kettenis

Implement GenericSerialBus OpRegion support.

ok mlarkin@


# 1.341 27-Mar-2018 kettenis

Add acpicmos(4), a driver that implements SystemCMOS OperationRegion
access support. This fixes machines where the AML doesn't check whether
support for this OperationRegion type has been registered by the OS.

ok mlarkin@


Revision tags: OPENBSD_6_3_BASE
# 1.340 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.339 10-Feb-2018 deraadt

Syncronize filesystems to disk when suspending. Each mountpoint's vnodes
are pushed to disk. Dangling vnodes (unlinked files still in use) and
vnodes undergoing change by long-running syscalls are identified -- and
such filesystems are marked dirty on-disk while we are suspended (in case
power is lost, a fsck will be required). Filesystems without dangling or
busy vnodes are marked clean, resulting in faster boots following
"battery died" circumstances.
Tested by numerous developers, thanks for the feedback.


# 1.338 08-Feb-2018 deraadt

After flushing disks, try again to release all memory. We don't need to
save dirty memory to the hibernate space.


# 1.337 08-Feb-2018 deraadt

Toss all releaseable memory, because fragmentation can get in the way
of allocating the hibernate playpen.


# 1.336 08-Feb-2018 deraadt

sr_quiesce() is a new approach for ensuring that softraid drains
output to the disks.

This is part of a larger suspend/resume filesystem-safety diff, which
has been worked on for a couple of months already. Tests by job, krw,
beck, benno, and others. Sometimes even by snapshot users...


# 1.335 29-Nov-2017 kettenis

Add support for GPIO-signaled events.

ok mlarkin@


# 1.334 16-Nov-2017 jcs

Add PCI attachment for dwiic(4) needed by Intel 100 Series machines

ok kettenis


# 1.333 14-Nov-2017 kettenis

Add a few more PNP IDs to the skip lists.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.332 17-Aug-2017 stsp

Make dwiic(4) attach its ACPI parent dependencies.
This change makes the Asus E200HA keyboard work.

Original analysis and patch by Cesare Gargano
ok kettenis@


# 1.331 09-Aug-2017 dcoppa

Add "PNP0303" (8042 PS/2 Controller) to acpi_isa_hids[]

ok kettenis@


# 1.330 22-Jul-2017 jcs

acpibat: look for _BIX first before falling back on _BIF

ACPI 4.0 deprecated _BIF for battery status so some newer machines
are exporting _BIX which extends _BIF's package with a few extra
fields. When using _BIX, export the new discharge cycle count as a
sensor.

feedback from kettenis and anton
ok deraadt


# 1.329 20-Jul-2017 kettenis

Start a list of isa(4) device names such that we can avoid printing them
as "not configured".

ok mikeb@, millert@, deraadt@


# 1.328 08-Apr-2017 deraadt

Sensors are run as callbacks inside tasks. During suspend a sensor
could be running inside a driver that will be force-detached, or due
to tsleep end up disrupting the softstate/hardstate contract. At
suspend time, quisce all these callbacks by waiting for completion.
This issue has never been observed for real, but may be implicated in
suspend/resume failures.
ok kettenis guenther mlarkin


# 1.327 08-Apr-2017 deraadt

Two prototypes for acpi_maptable; merge to one place


Revision tags: OPENBSD_6_1_BASE
# 1.326 27-Mar-2017 deraadt

Now that hibernate_alloc() only has clean success/failure, don't
need to call hibernate_free() to clean up a partial mess.
ok mlarkin kettenis


# 1.325 13-Mar-2017 deraadt

allow ramdisks to build; ok jcs


# 1.324 12-Mar-2017 jcs

try to attach acpisbs first and if successful, don't attach acpibat


# 1.323 02-Mar-2017 natano

Add a new sysctl machdep.lidaction. The sysctl works as follows:

machdep.lidaction=0 # do nothing
machdep.lidaction=1 # suspend
machdep.lidaction=2 # hibernate

lidsuspend is just an alias for lidaction, so if you change one, the
other one will have the same value. The plan is to remove
machdep.lidsuspend eventually when people have upgraded their
/ets/sysctl.conf.

discussed with deraadt, who came up with the new MIB name
no objections mlarkin
ok stsp halex jcs


# 1.322 28-Feb-2017 natano

Untangle abstract sleep modes from ACPI sleep states. This paves the way
for further work in the area.

idea and ok deraadt
ok mlarkin


# 1.321 22-Feb-2017 jcs

add acpisbs, an acpi smart battery subsystem driver reading data
over smbus

currently disabled because it conflicts with acpibat

ok deraadt, kettenis


# 1.320 14-Jan-2017 kettenis

Revert aml_rdpciaddr changes; breaks several machines that were working
before.


# 1.319 11-Jan-2017 jsg

Increase the size of the buffers used for _HID and _CID strings to be
able to fit "Hyper_V_Gen_Counter_V1".

ok mlarkin@ deraadt@


# 1.318 08-Jan-2017 kettenis

Make aml_rdpciaddr() work for for devices behind bridges. The code now
uses the mapping between AML nodes and PCI devices that we establish.
Because _INIT methods may end up calling aml_rdpciaddr(), make sure we
create that mapping early. Also handle devices that aren't actually present.
These devices are now included in the mapping and reads will return an
all-ones patterm whereas writes are a no-op..

ok mlarkin@


# 1.317 25-Oct-2016 pirofti

Add the resource index to aml_parse_resource() callback function.

This is needed by an upcoming acpiec commit that handles machines breaking
the current ACPI specifications.

Change suggested by and ok kettenis@, guenther@


# 1.316 18-Sep-2016 guenther

If the APIC table (MADT) isn't present then ci_acpi_proc_id won't be set
when acpicpu tries to attach, leading to a NULL dereference. The ACPI
processor object on the Dells where this was reported is bogus and useless
anyway so apply the same test when deciding whether to attach to processor
objects as acpicpu would when matching them to cpus.

Problem reported by Eivind Eide (xenofil (at) gmail.com) and
Johan Huldtgren (johan+openbsd-bugs (at) huldtgren.com).
Testing by Eivind Eide.
ok kettenis@ mlarkin@


# 1.315 03-Sep-2016 naddy

Write the system time back to the RTC every 30 minutes.
This fixes the problem that long-running machines which were not
shut down properly would reboot with a badly offset system time.

hints and ok kettenis@


# 1.314 31-Aug-2016 mlarkin

Don't put a device in the wake device list if _STA says it's not there.

Fixes at least one machine that woke up from sleep / poweroff immediately.

ok kettenis@, tested by kettenis and martijn@


# 1.313 28-Jul-2016 kettenis

Store the acpi processor ID/UID in struct cpu_info, and use it to attach
acpicpu(4) drivers to the right cpu(4).

ok mlarkin@, guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.312 10-Jun-2016 kettenis

The power management timer has been made optional in ACPI 5.0A, so don't
attempt to attach acpitimer(4) if the timer isn't present.

Pointed out by jcs@

ok jcs@


# 1.311 08-May-2016 kettenis

Make sure devices listed by a device's _DEP method are attached before
we attach the device itself. The _DEP methode, introduced in ACPI 5.0,
is a hint that the device in question depends on OpRegion support from
the devices listed by _DEP, so we have to order them the proper way.
To prevent us from attaching those devices again when we encounter them
later walking down the device tree, keep track of our attempts to
attach a device driver to them using aflag in the aml_node structure.

ok guenther@


# 1.310 25-Apr-2016 pirofti

We attach acpihpet(4) and acpiec(4) without using the HID, so do not
report it in the dmesg.

OK deraadt@, mlarkin@.


# 1.309 02-Apr-2016 kettenis

Reduce dmesg spam by knocking out some acpi hardware IDs that don't need a
driver.

ok mpi@


# 1.308 29-Mar-2016 kettenis

Check if a device is present (using _STA) in acpi_foundhid. This prevents
us from reporting devices as "not configured" that aren't actually present,
and allows us to remove duplicated code from several drivers.

ok deraadt@, ok jsg@


# 1.307 29-Mar-2016 kettenis

Implement a cleaner way to prevent acpivout(4) from attaching if we have
a platform-specific method of controlling the backlight.

ok millert@, mlarkin@, jung@


# 1.306 28-Mar-2016 kettenis

Adding each and every new acpi device driver to acpi_foundhid() is
gettinga bit of the burden. And it really isn't how our autoconf
framework is supposed to work for busses that can be enumerated. All
the drivers already check for a matching _HID in their attach
function. So we can just drop the checks in acpi_foundhid() and let
the drivers sort themselves out.

Print a "not configured" line for devices for which we don't attach adriver.
This may be a bit much as there are devices for which we will not have a
driver. This will be removed if it causes too much dmesg spam.

ok deraadt@, mlarkin@


Revision tags: OPENBSD_5_9_BASE
# 1.305 17-Jan-2016 jsg

Take acpi_foundhid() outside of #ifndef SMALL_KERNEL so the ramdisk
kernel can find and attach dwiic(4) and sdhc(4).


# 1.304 17-Jan-2016 jsg

remove an unused global variable


# 1.303 14-Jan-2016 kettenis

Attach dwiic(4) on the Intel Bay Trail i2c controllers as well.


# 1.302 12-Jan-2016 kettenis

Improve emulation of PM registers on Hardware-redcued ACPI by emulating
SPL_TYPx, SLP_EN and WAK_STS using the new SLEEP_STATUS_REG and
SLEEP_CONTROL_REG registers. Makes the transition into S5 (powerdown) work
on the ASUS X205TA.

ok deraadt@, mlarkin@


# 1.301 12-Jan-2016 jcs

Add dwiic, a driver for the Synopsys DesignWare i2c controller found
on the Samsung ATIV Book 9 laptop. This initial version only
supports ACPI config/attachment.

Add ihidev, a HID-over-i2c driver largely based on uhidev. dwiic
handles attaching ihidev devices found in ACPI.

Add ims, a HID-over-i2c mouse/trackpad driver to get basic cursor
and button functionality from HID-compliant i2c trackpads.

ok kettenis deraadt


# 1.300 11-Jan-2016 kettenis

Make sdhc(4) attach to hardware IDs "80860F14" and "PNP0FFF".


# 1.299 09-Jan-2016 kettenis

Initial support for Hardware-Reduced ACPI. This skips the legacy mode checks
by emulating the PM_CNT register such that SCI_EN is always enabled. It also
skip attaching acpitimer(4) since the relevant registers are no longer there.

ok deraadt@, mlarkin@


# 1.298 06-Jan-2016 kettenis

Follow the spec and check the SCI_EN bit to see if ACPI has already
been enabled and don't bother enabling it again if it is. This should fix
systems that don't implement legacy mode and therefore set ACPI_ENABLE and
ACPI_DISABLE to zero.

ok mlarkin@, yasuoka@


# 1.297 23-Nov-2015 reyk

Back out the CRITICAL change, turn it back from 10% to 25%. Some
people with old/broken batteries didn't like this change, and the
underlying heuristics are wrong.


# 1.296 22-Nov-2015 reyk

The "CRITICAL" APM battery level, that is exported/faked by the ACPI
stack, is currently set to 25%. With 25% battery left, a typical
modern laptop will have at least one hour runtime left. It is an
arbitrary value, and other APM power layers in the kernel set CRITICAL
to something like 5% or 10%, so set it to 10% for now.

OK mlarkin@


# 1.295 28-Sep-2015 deraadt

In low-level suspend routines, set cold=2. In tsleep(), use this to
spit out a ddb trace to console. This should allow us to find suspend
or resume routines which break the rules. It depends on the console
output function being non-sleeping.... but that's another codepath which
should try to be safe when cold is set.
ok kettenis


# 1.294 06-Sep-2015 deraadt

clarify malloc arguments (base + length...)


# 1.293 28-Aug-2015 deraadt

fairly simple sizes for free(); ok tedu


# 1.292 20-Aug-2015 kettenis

Respect the access size when reading or writing to pci config space.
This version also makes sure writes are properly aligned, preventing
the panics seen with the previous incarnation of this change.

Fixes battery status passthrough in vmware, brightness keys on my x220
and who knows what else.

ok mlarkin@, deraadt@


Revision tags: OPENBSD_5_8_BASE
# 1.291 04-Aug-2015 deraadt

If we are setting ACPI_PM1_BM_RLD do it only once, and remember to
repeat this action at resume time.
ok mlarkin guenther


# 1.290 29-Jul-2015 deraadt

Something subtle in previous revision is locking up suspend or booting
on some laptops. Mailing lists threads mention x220 and Asus machines.
Reverting for now.


# 1.289 21-Jul-2015 kettenis

iRespect the access size when reading or writing to pci config space.
Fixes battery status passthrough in vmware and who knows what else.

Tested by many.

ok mlarkin@, deraadt@


# 1.288 17-Jul-2015 mlarkin

fix comments


# 1.287 26-Mar-2015 jsg

Use the 0x10 extended capability to determine if the trackstick
buttons are wired to the trackpad and need to be re-routed to the
trackstick. Without this change the buttons on 2015 thinkpads
get picked up as extended buttons that show up as scroll up/down.

Remove the X1 Carbon 2015 (LEN0048) and X250 (LEN0046) from the
top button area/soft buttons quirks list. Also avoid using the
quirk list entirely if the capability bit is set.

Based on work from jcs@ with feedback from mpi@
ok mpi@ jcs@


# 1.286 20-Mar-2015 kettenis

The ACPI standard says that a PCI function number of FFFF can be used to refer
to all functions on a device. This makes no sense in the context of accessing
PCI config space. Nevertheless stsp@ has a machine with AML that tries to
do config space reads from a device where _ADR encodes function number FFFF.
This is almost certainly buggy AML, but since Linux (and presumably Windows)
boot just fine on this machine we need to work around this. Linux effectively
treats function FFFF as function 7, which isn't present on this machine.
Instead we simulate a read from a non-existent device by returning a bit
pattern of all ones. Writes to function FFFF will still panic.

ok mlarkin@


# 1.285 16-Mar-2015 deraadt

Reduce use of SMALL_KERNEL in acpi, bringing acpiec driver and more gpe
handling into RAMDISK. This is now possible because the install media
has ample room. The goal is to reduce special cases where we may be
skipping (unknown) important operations...
ok mlarkin kettenis


# 1.284 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.283 07-Feb-2015 deraadt

New framework that allows hibernate to pass in entropy from it's fresh
boot.
ok mlarkin


# 1.282 06-Feb-2015 mlarkin

Fix a hibernate crash on some machines due to unmapping a page that
may not have been mapped previously (in the failure to hibernate case).

Also ensure that the lowmem ptp is mapped in all cases (not just MP).

ok kettenis


# 1.281 17-Jan-2015 deraadt

resume_randomness() before spinning up other cpus...


# 1.280 15-Jan-2015 jsg

Define a new wscons mouse type for Synaptics clickpad devices that lack
physical buttons. This will be used if the acpi pnpid for the mouse
matches a list provided by Synaptics found in Linux.

Combined with the xenocara changes this will remove the need for an xorg
config file for the x240/t440/t540 generation of thinkpads.

ok matthieu@ shadchin@ kettenis@


# 1.279 11-Jan-2015 kettenis

Tolerate checksum errors in ACPI tables (but complain about them). Apparently
there are machines out there with broken BIOSen with such checksum errors in
thier RSDT tables. Windows runs fine on these machines, so it probably does
not check. Linux ignores checksum errors as well (and prints a warning).

ok deraadt@, krw@


# 1.278 18-Dec-2014 deraadt

suspend and resume the RNG (at what looks like the right spots)
ok reyk djm


# 1.277 09-Dec-2014 doug

Replace some malloc(n*size,...) calls with mallocarray().

ok tedu@ deraadt@


# 1.276 06-Dec-2014 mpi

If "machdep.lidsuspend" is not set, do not go back to sleep if the lid
is still closed when resuming. This allows docked laptop to be resumed
without opening them.

ok mlarkin@


# 1.275 23-Nov-2014 mlarkin

If a machine has no _LIDs, then none can be open. This diff fixes a problem
where machines without any _LIDs were going back to sleep after hibernate
due to the recent suspend-if-lid-closed-on-resume diff. Noticed on qemu,
which doesn't have any _LIDs.

discussed with deraadt


# 1.274 18-Nov-2014 krw

Nuke yet more obvious #include duplications.

ok miod@


# 1.273 08-Nov-2014 mlarkin

If resuming from sleep (zzz/ZZZ) and the lid is still closed, go back to
sleep. This prevents accidental lid flex or slight opening in a backpack
from waking the machine up and leaving it resumed (powered on).

ok deraadt@


# 1.272 26-Sep-2014 kettenis

Rework piglet and pig allocation. Currently the piglet gets allocated
deep down in the suspend path, where it is really hard to recover from
allocation failure. So allocate the piglet early on in the suspend path.
Also change the piglet and piglet allocation functions to use km_alloc(9)
instead of doing pmemrange magic. This removes a bunch of code which, in the
case of the piglet allocation, is broken since it results in a NULL pointer
dereference. Also switch the piglet allocation to not wait. If we can't
allocate 16MB of phys contig memory on a halfway modern machine we're almost
certainly under a lot of memory pressure and we're better off not trying to
hibernate anyway.

ok mlarkin@


# 1.271 25-Sep-2014 kettenis

Bring back rev 1.249. Now that mp_setperf() has been fixed, it should be
safe to restore perflevel when resuming. Should fix the issues people
see with the processor running at full speed heating up the machine while
the apm command reports that the CPUs are running at a low speed.

ok dcoppa@, deraadt@


# 1.270 23-Sep-2014 kettenis

Release the acpi lock around calls to wsdisplay_suspend() and
wsdisplay_resume(). The X server may adjust the display brightness just
before/after the VT switch when suspending/resuming. In fact, it may
already be blocked adjusting the display brightness for some reason
unrelated to the suspend request.

The VT switch request will eventually time out, but the delay is annoying
and the failed VT switch will lead to problems after resume.

Needed for a future update of the xf86-video-intel driver.

ok mlarkin@


# 1.269 19-Sep-2014 kettenis

Use config_suspend_all(9).


# 1.268 08-Sep-2014 kettenis

Clear the PME Status bit on PCI devices in response to Device Wake events.
Fixes the interrupt storm on the 2009 Mac mini with WOL enabled on its
nfe(4) interface.

ok mlarkin@


Revision tags: OPENBSD_5_6_BASE
# 1.267 20-Jul-2014 mlarkin

Support hibernating to softraid crypto volumes.

much help and ok from deraadt@


# 1.266 16-Jul-2014 mlarkin

Reenable hibernate RLE support and flush+zero all memory after unpack.


# 1.265 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.264 12-Jul-2014 kettenis

Add a function to drop all clean pages on the page daemon queues and call
it when we hibernate.

ok mlarkin@, miod@, deraadt@


# 1.263 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.262 11-Jul-2014 mlarkin

Flush the buffercache to 16MB on hibernate and restore its previous max
size (kern.bufcachepercent) on resume, for better hibernate performance.

ok beck@


# 1.261 10-Jul-2014 blambert

remove unneeded header

ok mlarkin@


# 1.260 01-Jul-2014 kettenis

Adding an additonal delay after each access to SystemMemory or SystemIO
during autoconf makes no sense.

ok deraadt@


# 1.259 23-Jun-2014 kettenis

SystemMemory is used to access memory mapped registers on some machines, so we
must use the bus_space(9) API to access it instead of memcpy(9). Also make
sure we properly align access to fields that need more than byte alignment.

ok guenther@


# 1.258 21-May-2014 mlarkin

Fix invalid / missing format specifiers in several dnprintf statements
(used only when ACPI_DEBUG is enabled)

From a diff on tech@ by Fabian Raetz, with a minor change by me, tested
on i386 and amd64 with ACPI_DEBUG enabled and disabled.


# 1.257 25-Apr-2014 mlarkin

Defer the free of the hibernate structures until later, when it is safe to
do so.

Freeing the hibernate structures too early leads to inadvertent calls into
splx when we aren't ready for them yet. Found when trapping splx calls in
the resume path.

discussed with deraadt@ at m2k14


# 1.256 13-Apr-2014 mlarkin

Fix a badly worded debug message that implied the entire machine was going
to sleep, instead of just the acpi thread going to sleep.


# 1.255 31-Mar-2014 mpi

Change the order of operations during a suspend/resume cycle and
call bufq_quiesce() after executing the DVACT_QUIESCE handlers.

This should be safe since no disk nor controller drivers have such
handler but it will allow us to detach sd(4) devices attached to a
USB bus.

Another benefit pointed out by kettenis@ is that drivers that need
to read a firmware from the disk should be able to do it at resume
time in a DVACT_WAKEUP handler.

ok kettenis@, deraadt@


# 1.254 13-Mar-2014 dlg

get rid of the assumption that the head of the alldevs list is the
"mainbus" device. this breaks when mpath is enabled because it
attaches before mainbus and therefore takes the head position.

have autoconf provide device_mainbus() which looks up mainbus_cd,
and use that instead.

discussed with deraadt who just wants mpath stuff to move forward
despite there being many ways to shine this particular turd.


Revision tags: OPENBSD_5_5_BASE
# 1.253 21-Feb-2014 deraadt

Do the lid control further on in the resume sequence, since this is
AML, and will do WAITOK operations. While cold, that is a bad idea.
We seem to be safe with respect to the spec, I do not see any event
debounce issues here.
ok kettenis, tested by many


# 1.252 20-Jan-2014 claudio

Instead of checking sc->sc_revision <= 1 check that the
fadt->hdr_revision <= 1. This is a better check since it checks
the version of the proper descriptor. This allows the alix APU
to reboot since those boards have currently a wrong sc->sc_revision.
Committing this now so that it can be tested easier.
With and OK jsg@ also fine by derradt@


# 1.251 24-Dec-2013 kettenis

If the FADT has its SMI_CMD set to zero, assume we're only ACPI-only hardware
and don't need to disable SMI ownership of the ACPI hardware registers.

ok mlarkin@


# 1.250 23-Dec-2013 kettenis

Revert previous commit. It makes resume hang on some MP systems running
GENERIC.MP.


# 1.249 16-Dec-2013 kettenis

Restore hw.setperf upon resume like we do for apm(4) on i386. Seems we need
to do this fairly late in the resume to avoid hangs.

ok deraadt@, mlarkin@


# 1.248 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


# 1.247 06-Nov-2013 mpi

Add support for Power Resources for Dx states and the necessary hook
for PCI devices. This hook should be called twice, before and after
changing the power state of a PCI device.

Before setting the device to the new state, the ACPI layer will notify
every power resources linked to the device for that state and make sure
they are turned "_ON". After changing the state of the device, it will
decrement the reference of every power resources linked to that device
for the old state and turn them "_OFF" if they are no longer referenced.

This fixes the no-USB after resume problem seen on various ThinkPad,
problem initialy diagnosed with Alexander Polakov.

ok kettenis@, deraadt@


Revision tags: OPENBSD_5_4_BASE
# 1.246 01-Jun-2013 mlarkin

Stop printing ACPI wakeup devices in dmesg after the 16th wakeup device.

Helps with VMware VMs that report hundreds of wakeup devices.

ok reyk@, deraadt@


# 1.245 31-May-2013 bluhm

Fix the build for a kernel without wd and pciide in its config.
OK deraadt@ tedu@


# 1.244 30-May-2013 deraadt

Enforce ca_activate tree-walks over the entire heirarchy for all events,
cleaning up some shutdown-hook related code on the way.
(A few drivers related to sparc are still skipped at kettenis' request)
ok kettenis mlarkin, tested by many others too


# 1.243 18-Apr-2013 deraadt

pin the acpi thread to the BSP, because AML and SMI cannot be trusted. I
was certain we were doing this already, but it turns out no...


Revision tags: OPENBSD_5_3_BASE
# 1.242 24-Dec-2012 guenther

In acpiioctl(), Return though the splx() path if suser() returns an error

ok deraadt@


# 1.241 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


# 1.240 04-Oct-2012 ehrhardt

Use information provided by ACPI to attach secondary PCI host bridges.

ok kettenis@


# 1.239 07-Sep-2012 kettenis

Add a new function called acpi_pci_min_powerstate() that returns the lowest
power state for a PCI device for the power state (currently S3 or S4) we're
transitioning into. This function is probably too simplistic; there is a lot
of (silly) complexity in the ACPI spec.

ok deraadt@


Revision tags: OPENBSD_5_2_BASE
# 1.238 13-Jul-2012 pirofti

Disable acpivideo(4) on ASUS laptops.

There are some models that panic whenever an event is received.
Some cope, some don't. In order to get this right acpiec(4) requires
major rework. Until then let machines boot.

Discussed with deraadt@.


# 1.237 11-Jul-2012 mlarkin

for now, restrict hibernate to root in the acpi ioctl kernel interface.
normal rules for controlling this interface through apmd still apply.

ok deraadt@


# 1.236 09-Jul-2012 deraadt

tiny little oops in untested hibernate block


# 1.235 09-Jul-2012 deraadt

clean up ioctl() case statements


# 1.234 09-Jul-2012 deraadt

acpi_disable_onegpe() is not used; ok pirofti


# 1.233 24-May-2012 kettenis

If _MIN > _MAX consider a WordBus resource to be invalid. Fixes a panic on
the IBM x3550-M4.

ok mikeb@


# 1.232 29-Mar-2012 mlarkin

Missing #ifdef in previous diff


# 1.231 29-Mar-2012 mlarkin

Prevent hibernation attempts on machines with unsupported disk controllers.
The same check is also performed later in the hibernate sequence, but by
then it's too late to cleanly unwind (presently).

ok deraadt


# 1.230 26-Mar-2012 deraadt

simplify conditionals in acpi_sleep_state()
ok mlarkin


# 1.229 26-Mar-2012 deraadt

hook in the hibernate request code; half of this diff is from mlarkin
ok mlarkin


Revision tags: OPENBSD_5_1_BASE
# 1.228 20-Sep-2011 deraadt

Very early in suspend while the clock is still ticking realtime,
save the clock back to the rtc. The zaurus already did this.
ok phessler miod kettenis


Revision tags: OPENBSD_5_0_BASE
# 1.227 02-Jul-2011 nicm

kqueue attach functions should return an errno or 0, not a plain 1. Fix
the obvious cases to return EINVAL and ENXIO.

ok tedu deraadt


# 1.226 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.225 16-Jun-2011 pirofti

Import acpitoshiba driver.

This is originally written by Hiroyuki Aizu, ported and integrated
in our tree by Javier Vazquez <javama4 AT gmail> with tweaks and
suggestions by me.

Import prodded by deraadt@ and the lack of testers on tech@.


# 1.224 27-Apr-2011 jcs

attach acpithinkpad to newer lenovo models like the x120e

ok deraadt@


# 1.223 22-Apr-2011 jordan

Fix uninitialzied variables and formatting strings (-Wxxx errors)


Revision tags: OPENBSD_4_9_BASE
# 1.222 02-Jan-2011 jordan

Re-add code for ACPI taskqueue, eliminates need for state vars in acpi softc
Serialize processing ACPI tasks for sleep/powerdown/GPE event
ok deraadt@


# 1.221 31-Oct-2010 guenther

Revert last commit: it breaks resume on Thinkpads
"then please back it out" deraadt@


# 1.220 26-Oct-2010 jordan

Add task queue for ACPI gpe and notify handlers
This fixes eject on Dell Latitude dock and an issue on the Dell Mini
battery update.
ok deraadt


# 1.219 07-Oct-2010 jordan

Create separate functions for enabling wake and runtime gpes.
Remove old suspend/resume gpewalk function
ok deraadt, mlarkin


# 1.218 05-Oct-2010 jordan

split acpi_enable_onegpe to separate enable/disable functions
ok deraadt


# 1.217 05-Oct-2010 jordan

Added acpi_disable_allgpes function to clear GPE status
ok deraadt


# 1.216 05-Oct-2010 deraadt

clean up acpi_softc use more


# 1.215 05-Oct-2010 deraadt

delete a bit of #if 0 code


# 1.214 05-Oct-2010 deraadt

hide use of our global acpi_softc pointer a bit better


# 1.213 29-Sep-2010 deraadt

grab the acpi thread lock very early in acpi thread startup; not that it
is likely that anyone will race us... but why be careless.
ok kettenis


# 1.212 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.211 29-Aug-2010 jasper

- add a check for FWRITE here too.

ok miod@


# 1.210 27-Aug-2010 kettenis

Make PCI Power Management optional, and only enable it when acpi(4) attaches.

ok deraadt@


Revision tags: OPENBSD_4_8_BASE
# 1.209 08-Aug-2010 kettenis

Close race between the acpi thread and wscons ioctls running in process
context. We might rip this out post-release and replace it with a solution
that serializes things in a more obvious way.

ok deraadt@, miod@ and "those crazy texans who ok anything"


# 1.208 08-Aug-2010 deraadt

Report the acpi sleep states when on a RAMDISK; ok krw


# 1.207 07-Aug-2010 kettenis

Enforce acpi register access rescrictions.

ok mlarkin@, deraadt@, tested by marco@


# 1.206 07-Aug-2010 deraadt

upon resume, notify all the acpiac and acpibat drivers to update
their status. some laptops do not do this automatically.
ok kettenis canacar


# 1.205 06-Aug-2010 marco

Always call _PSW on _LID devices that support it to enable lid open to
resume the machine. Conversely disable it when the machine wakes up.

Tested by several ok deraadt


# 1.204 06-Aug-2010 deraadt

As part of a revamp of the PM1/GPE code, I write this diff to look for
unmanaged EN & STS bits in the PM1 register at interrupt time and report
them. As a side effect this splits the STS acknowledgement into two writes
(for power, and sleep) instead of one. The printf that is added (to spot
unmanaged STS bits) has not yet been triggered as far as we know.

Before the "write to PM1 registers at the right offset" diff went in, this
was not neccessary. But newer thinkpads do not have a working soft power
button without this diff.

We have no idea why.
ok mlarkin kettenis


# 1.203 05-Aug-2010 deraadt

PM1 power button events were generating 'fake events' to acpibtn PWRB,
but if there is no such device these were being tossed. Instead, arrange
for all power button handlers to flag a softc variable which is processed
by the thread; so that one place will know to request a powerdown (psignal
to init)
ok marco mlarkin


# 1.202 05-Aug-2010 deraadt

rename a variable which is annoyingly poorly named


# 1.201 05-Aug-2010 mlarkin

Write to the correct offset for various ACPI pmreg registers. Writing to
the correct register does properly clear the wake status bits and fixed
event bits, which allows some machines to have more than 1 suspend cycle.

bug spotted by deraadt@

tested by deraadt@ and myself on a half-dozen machines and others on
hackers@

ok deraadt@, marco@


# 1.200 05-Aug-2010 deraadt

Fix the power button. The pm1 register is 16 bits in size.
My mistake, a while back.
tested by krw


# 1.199 04-Aug-2010 kettenis

Print supported sleep states.

ok marco@, phessler@, deraadt@


# 1.198 03-Aug-2010 mlarkin

Use the proper flag for re-enabling certain hardware events (power btn,
sleep btn). The (incorrect) flag prevoiously used coincidentally had the
same value, so this shouldn't cause any different behavior than before.

tested on a variety of machines (i386, amd64, sp, mp) - no behavioral
change seen

ok deraadt@


# 1.197 28-Jul-2010 marco

Make all gasio failures look and behave the same.


# 1.196 28-Jul-2010 mlarkin

Delay a little longer on suspend before giving up. Fixes machines that
sometimes don't suspend because the sleep transition takes too long
(symptoms like failing to sleep but the backlight stays on).

ok deraadt@


# 1.195 27-Jul-2010 deraadt

remove XXX; EC can now be attached early when needed; ok jordan


# 1.194 27-Jul-2010 deraadt

more brackets feels good


# 1.193 27-Jul-2010 deraadt

XXX annotate a block in acpi_enter_sleep_state() that is pretty worrying


# 1.192 27-Jul-2010 deraadt

horrific indentation that nearly made me puke


# 1.191 27-Jul-2010 mlarkin

Set the sleeping indicator light on machines that support it, in case SMI
doesn't do it for us. Shows the 'moon' LED on older Thinkpads when
sleeping.

Tested on T43p, W500, T510 by me and on X61s by deraadt@.

ok deraadt@


# 1.190 27-Jul-2010 jordan

Early initialization of acpiec if ECDT table exists
Fixes hang when booting thinkpads while docked
ok deraadt


# 1.189 26-Jul-2010 deraadt

remove merge error which was commited


# 1.188 26-Jul-2010 pirofti

Add support for Sony ACPI hotkeys via a new driver: acpisony(4).

Currently it only works for the suspend button and tries to do right for
the brightness events, but I haven't found a Sony laptop that like to do
right so far.

In the future I want to make the brightness keys work on all Sony's and
also add support for the zoom hotkeys and whatever other funky keys I can
find on those things.

Okay deraadt@.


# 1.187 22-Jul-2010 deraadt

pretty large cleanup of SMALL_KERNEL


# 1.186 21-Jul-2010 deraadt

in the acpi GPE handler, late ack level interrupts not edge interrupts.
Duh.


# 1.185 21-Jul-2010 deraadt

Merge the edge and level gpe handlers into one (now possible since the
gpe structure tells us whether the gpe is edge or level)


# 1.184 21-Jul-2010 deraadt

delete #if 0 code we are not going to use; ok marco


# 1.183 20-Jul-2010 deraadt

make the mask code for acpi_enable_onegpe() match other similar code


# 1.182 20-Jul-2010 deraadt

Handle the acpi interrupt controller with proper edge/level handling,
wrapped in spltty to avoid racing against the interrupt controller.
Repair the gpe bit masking code while there, and do operations in the
right order
ok kettenis mlarkin, help from jordan at figuring out the order of
operations.


# 1.181 20-Jul-2010 matthew

When we're preparing to go to sleep, only call bufq_restart() if other
preparations failed.

ok kettenis@, thib@, deraadt@


# 1.180 19-Jul-2010 deraadt

permit compilation with ACPI_DEBUG


# 1.179 19-Jul-2010 deraadt

there is no need for a seperate dsdt_softc pointer; dsdt does not have a
softc but is just a part of the single acpi driver.
ok jordan


# 1.178 19-Jul-2010 deraadt

acpi_delay() is no longer needed; ok jordan


# 1.177 18-Jul-2010 mlarkin

Clear event status on resume, to avoid going back to sleep/reboot/shutdown
immediately after resume. Tested by a bunch of people on hackers@ on
various hardware. Fixes at least Toshiba NB200 and Gateway LT3103u.

ok marco


# 1.176 13-Jul-2010 deraadt

rename some thread related interfaces so they are logical


# 1.175 10-Jul-2010 jordan

Verify _ADR bdf is correct (some systems return 0xffff)
Works on Dell x9xx and Studio 1555


# 1.174 09-Jul-2010 jordan

Clear the noise


# 1.173 08-Jul-2010 jordan

Add mapping for ACPI device to PCI bdf (match autoconf tree)
Simplify resource parsing function to use buffer argument
Convert namespace linked lists to use queue macros
ok marco@, deraadt@


# 1.172 06-Jul-2010 deraadt

Use spltty() to lock downcalls from apm against the information being
modified by the acpi thread.
ok kettenis


# 1.171 06-Jul-2010 deraadt

Don't use a workq to do the suspend, because it races aginst the acpi
thread. Instead, just tell the acpi thread to do the suspend for us.
This makes apmd & zzz work correctly. While here, have acpithinkpad
attempt to post the event to apm before prompting the suspend itself.
ok kettenis marco mlarkin


# 1.170 05-Jul-2010 mlarkin

Reset register SCI_EN on resume. The ACPI spec clearly states we don't have
to do this, but many machines don't, or forget to, which leads to all sorts
of problems on resume (broken battery stats, overheating, failure to resume
more than once, etc).

tested by many, ok marco@, deraadt@


# 1.169 01-Jul-2010 thib

do bufq quiesce again, since vnd/the bufq_done call in
biodone has been fixed.


# 1.168 01-Jul-2010 jordan

Backout recent AML changes, breaks on R210 and others


# 1.167 01-Jul-2010 jordan

Simplified aml_parse_resource call, cleanup code
ok marco


# 1.166 01-Jul-2010 jordan

Fix subordinate bus # for multi-root PCI buses
ok marco, kettenis


# 1.165 30-Jun-2010 thib

Disable/partially backout the bufq quiesce changes as this
is causing havoc with vnds and release must be buildable.


# 1.164 29-Jun-2010 deraadt

Fix ramdisks (new code was placed inside a SMALL_KERNEL chunk)


# 1.163 29-Jun-2010 jordan

Add support for mapping ACPI to PCI devices
ok kettenis, deraadt


# 1.162 29-Jun-2010 kettenis

Call bufq_quiesce() on suspend to stop all I/O and bufq_restart on resume.
Makes suspend/resume work much better while doing disk I/O.

ok mlarkin@


# 1.161 29-Jun-2010 tedu

60 is a more reasonable guess for number of minutes in an hour than 100.
spotted by TeXitoi. ok deraadt


# 1.160 27-Jun-2010 jordan

Add _acpi_matchhids routine, precursor to pci device match


# 1.159 27-Jun-2010 mlarkin

Clear ACPI fixed event status on resume (power buttons/etc) so that some
machines don't immediately resume after suspending to S3.

Tested by kettenis@ and I on 5 or 6 machines, deraadt@ "yeah, looks good"


# 1.158 27-Jun-2010 jordan

Cleaned up acpi_maptable, adds to table list directly


# 1.157 07-Apr-2010 deraadt

If we have done a wsdisplay_suspend(), and ACPI decides it does not want to
suspend, we must roll back by calling wsdisplay_resume() or the virtual
console switching will be locked forever
ok oga


# 1.156 07-Apr-2010 kettenis

Make suspend/resume work on MP machines (running an MP kernel). Joint work
from mlarkin@ and me, with some amd64 fixes thrown in by deraadt@

ok marco@, deraadt@, pirofti@, mlarkin@


# 1.155 31-Mar-2010 kettenis

Bring /dev/apm support in line with apm(4). Make the suspend button send
a suspend request event to apmd(8) instead of suspending immediately. Also
keep track of whether /dev/apm and /dev/apmctl are currently open, such that
we can still suspend immediately if apmd(8) isn't running.

ok deraadt@, marco@, pirofti@, jsing@, oga@


# 1.154 30-Mar-2010 oga

Prevent the apmd/x races for good.

When we hit suspend time, go through all wsdisplays on the system. if
they are in mode MAPPED, but not MODE_DUMBFB then if possible do a full
vt switch to a !mapped vt, and prevent switching back until resume time.
This has to be called from MD code because this involves userland
running so that X can run the vt switch signal handler. This way, any
case where we are using the "poke registers from userland" model, we
will not be on the hardware when we go down, so the kernel can actually
handle thing properly.

Tested on several acpi laptops (by kettenis@ and ian@), x40 (me and
beck@ at LEAST) and zaurus (me). Maybe others, but if so I forgot who at
this time..

Idea from deraadt somewhere over the Faroe Islands (I thought of a
similar thing myself a while ago). Much prompting from him. Ok and
comments miod@


# 1.153 25-Mar-2010 oga

Fix & vs. && in the kqfilter code.

Based on a comparison with the apm code.

ok deraadt@, kettenis@


Revision tags: OPENBSD_4_7_BASE
# 1.152 26-Nov-2009 mlarkin

Visual (and on some machines, audio) feedback while the machine is
resuming via calls to aml _SST method. This also makes the "moon" LED on
thinkpads to blink during resume and cease when resume is completed.

ok pirofti@


# 1.151 26-Nov-2009 deraadt

Toggle into "cold" when we we disable interrupts, because tsleeping
in the AML interpreter and context switching into userland processes
tends to sometimes pop the ACPI trampoline page out of our address
space. Makes lots more machines work.
ok kettenis mlarkin


# 1.150 24-Nov-2009 jsg

in the resume path evaluate _WAK before dealing with device drivers
like the spec says, not after.

ok mlarkin@


# 1.149 24-Nov-2009 deraadt

abort suspends on SMP systems until that gets written; ok mlarkin


# 1.148 23-Nov-2009 mlarkin

pci should return the result of it's children's suspend/resume calls back
to its parent.

handle suspend failure case and unwind if devices have an activate function
that returns failure.

ok deraadt@, kettenis@


# 1.147 23-Nov-2009 mlarkin

acpi* devices need to call their children on suspend/resume.
ok deraadt@


# 1.146 23-Nov-2009 pirofti

Remove ACPI_SLEEP_ENABLED checks.

This enables by default the suspend/resume paths in the kernel.

Okay deraadt@.


# 1.145 23-Nov-2009 deraadt

knf and spacing that are super annoying


# 1.144 23-Nov-2009 mlarkin

Hook up Theo's new children suspend/resume function, as well as more ACPI
suspend/resume glue.

ok deraadt@


# 1.143 26-Oct-2009 deraadt

Do not do strncmp comparisons on dv_xname because longer device names which
look similar could arrive in the future. Instead, compare directly against
dv_cfdata->cf_driver->cd_name
Issue originally spotted by miod


# 1.142 26-Oct-2009 jordan

Verify checksum+address when loading ACPI tables.
Some systems had invalid entries in RSDT/XSDT.
ok marco@


# 1.141 23-Jul-2009 cnst

New aibs(4) driver for ASUSTeK AI Booster (ACPI ATK0110) hardware monitoring,
with sensor state support through limits provided by the ACPI.

Tested on several ASUS motherboards kindly networked by Sam Fourman Jr.

ok deraadt marco jordan


Revision tags: OPENBSD_4_6_BASE
# 1.140 03-Jun-2009 pirofti

Import acpi power resource driver. This is needed for suspend/resume logic.

Okay jordan@ and marco@.


# 1.139 03-Jun-2009 pirofti

Add acpivideo support. This adds brightness support for all laptops
except thinkpads, they will use the acpithinkpad driver. The driver is
also hooked into wsconsole. So brightness can be adjusted via:

$ wsconsctl display.brightness=<percentage>

This is very helpfull on some laptops that have a nasty bios and get two
steps instead of one when pressing the brightness button.

Tested on various dell, fujitsu, acer, samsung and other laptops.

Okay marco@, miod@. Suggestions from kettenis@.
Lots of reviews and help from miod@, thanks!


# 1.138 03-Jun-2009 jordan

Added ejectable bay for IDE cdroms
ok marco@


# 1.137 30-Apr-2009 marco

Oga changed apm ioctls and forgot to grep. Temporary work around by hitting
the sleep button when we get any request. This interface will chnage at
some point.
ok oga


# 1.136 19-Apr-2009 krw

Call acpi_sleep_walk() when powering down just like when entering
any other sleep state. This tells acpi wake devices to not wake up
the box until manual powering up resets their status. Makes my Dell
GX520 stay powered off in response to 'halt -p'.

In snaps for a while.

ok marco@ (several times) deraadt@


# 1.135 17-Apr-2009 pirofti

Add device ranking support for future sleep/wake developments.

okay marco@


# 1.134 11-Mar-2009 jordan

Cleanup.. use aml_evalinteger instead of aml_evalname
ok marco@


# 1.133 10-Mar-2009 jordan

Fix cut/paste error.. call _BFS not _PTS
ok marco@


Revision tags: OPENBSD_4_5_BASE
# 1.132 21-Feb-2009 marco

overzealous cut/paste; found by Vladimir Kirillov <proger@uaoug.org.ua>


# 1.131 20-Feb-2009 marco

Oops didn't mean for Debugger() to remain there.


# 1.130 19-Feb-2009 marco

suspend/resume bits so that we can develop this in tree. This is disabled.
code from mlarkin and me
help from art,toby,jordan and several others
ok jordan, go for it deraadt


# 1.129 10-Feb-2009 jordan

Fix null pointer for shutdown if acpi not enabled
ok marco@


# 1.128 04-Feb-2009 kettenis

Some systems that claim to be ACPI 2.0 apparently don't provide an XSDT
address. Make the code fall back on the RSDT on those systems.

From Joerg Niendorf <p3y80@internode.on.net>. Fixes PR 5986.


# 1.127 17-Jan-2009 krw

A couple of stray malloc/memset -> malloc(M_ZERO), a bit of knf and
lose a couple of unnecessary casts. No functional change.

Feedback from kettenis@

ok kettenis@ marco@


# 1.126 06-Nov-2008 marco

Collapse all sanity checks of hids into one function.

Originaly from jordan


Revision tags: OPENBSD_4_4_BASE
# 1.125 02-Jul-2008 fgsch

add acpivideo. for now almost an empty skel so others can work on
suspend/resume. eventually it will also manage output switching and
brightness where supported. prodded by marco@


# 1.124 11-Jun-2008 marco

Fix $OpenBSD$ for once and for all


# 1.123 08-Jun-2008 deraadt

aaa_dev was being set from an uninitialized variable; ok toby


# 1.122 07-Jun-2008 marco

Kill ACPIVERBOSE. It was never used and is worthless to boot.

comments jmc
ok toby


# 1.121 06-Jun-2008 marco

Shrink acpi some more for floppies and while here re-enable acpi on the
floppies. Fixes crazy sony vaio kettenis has.

tested by deraadt
ok deraadt jordan kettenis


# 1.120 01-Jun-2008 marco

Fix \n nit in SMALL_KERNEL.


# 1.119 01-Jun-2008 marco

Re-enable new parser. Lots of testing by various folks. Fits all media
now too.

ok kettenis deraadt


# 1.118 16-May-2008 dlg

back out jordans new parser.

it didnt have enough oks from the right people, it did break machines
that were previously working, and it bloated the RAMDISKS and install
media beyond the constraints of the media.

requested by deraadt@


# 1.117 14-May-2008 jordan

Adding new ACPI Parser code
Old guts of code still exists, needs to be torn out next
ok marco@
ok brad@


# 1.116 27-Apr-2008 jcs

add acpithinkpad(4), an acpi support driver for ibm/lenovo
thinkpads.

provides functionality that the bios did on older, non-acpi
thinkpads like respond to screen brightness keys and toggle the
bluetooth device on and off.

ok deraadt@ and marco@


# 1.115 24-Apr-2008 jsing

Introduce acpiasus(4), a driver for the ACPI based hotkeys found in many
ASUS laptops (including the ASUS EeePC) - largely based on NetBSD's
asus(4) driver. On the ASUS EeePC this allows us to enable/disable
wireless, change screen brightness and use the volume keys.

ok jsg@, weingart@


# 1.114 12-Apr-2008 kettenis

Get rid of acpi_s5 global variables; simply send SIGUSR2 instead of SUGUSR1
to tell init(8) to power down the machine.

ok krw@


Revision tags: OPENBSD_4_3_BASE
# 1.113 21-Jan-2008 jsg

Check against RESET_REG_SUP in FADT before doing the acpi reset dance
after all, not doing so appears to break more machines than it fixes.

ok marco@


# 1.112 05-Jan-2008 kettenis

Never attach more acpicpu(4) devices than cpu(4) devices. Always attach
the first acpicpu(4) device on non-MULTIPROCESSOR kernels, regardless of its
ACPI processor ID.

ok mikeb@


# 1.111 05-Dec-2007 deraadt

Hang acpi(4) below bios(4) instead of mainbus(4). This lets us move acpi(4)
before pcibios(4), and then.... if acpi(4) attaches, skip pcibios(4) since
it messes the machines up. Fixes claudio's HP dl320 G5, and almost assuredly
others.
ok kettenis


# 1.110 05-Dec-2007 deraadt

repair acpiopen/acpiclose and friends to handle the /dev/apmctl interface,
so that apmd(8) can actually work correctly against acpi(4), and thus,
even do the fancy -C/-A stuff; also tested by jmc


# 1.109 02-Dec-2007 fgsch

fix the trailing space for real now.


# 1.108 02-Dec-2007 fgsch

avoid trailing spaces on output; marco@ ok.


# 1.107 27-Nov-2007 kettenis

Fix off-by-one that made us attach acpicpu0 and acpicpu1 on GENERIC kernels.

ok marco@


# 1.106 25-Nov-2007 jsg

When available and ACPI is enabled use the ACPI reset method
to reboot. This is required for some quirky sis machines
and likely others to reboot.

Tested by many. 'commit early commit often' deraadt@


# 1.105 19-Nov-2007 deraadt

sigh, amd64 still uses that stupid acpi_enabled variable...


# 1.104 19-Nov-2007 deraadt

softc's come initialized to 0, you know...


# 1.103 19-Nov-2007 deraadt

store revision; from jsg


# 1.102 19-Nov-2007 kettenis

Initialize sc_facs earlier.

ok deraadt@, beck@, canacar@


# 1.101 16-Nov-2007 deraadt

fix the bus_space #define nightmare, so that amd64 and i386 are much more
uniform. as a result shared code like acpi needs less #ifdef's
ok marco kettenis


# 1.100 15-Nov-2007 deraadt

remove the #ifdef ACPI_ENABLE. the EC codepath is either safe enough (and
will be improved), or it is unsafe because AML assumes it must be there
ok almost everyone


# 1.99 15-Nov-2007 mikeb

Make acpicpu(4) attach to the existing CPUs only.

Problem reported by form@. Fix idea by kettenis@.
Lots of help from deraadt@. Tested by deraadt@, form@ and me.

ok deraadt marco


# 1.98 14-Nov-2007 deraadt

spacing


# 1.97 12-Nov-2007 deraadt

correct : vs , in attach-time messages


# 1.96 06-Nov-2007 deraadt

all ACPI_ENABLE kernels need acpi_interrupt()


# 1.95 06-Nov-2007 deraadt

move acpi_read_pmreg() and acpi_write_pmreg() out of SMALL_KERNEL so
that acpi ramdisks can compile (obvious solution)


# 1.94 05-Nov-2007 canacar

Establish the acpi interrupt earlier. Fixes problems with some
GENERIC.MP kernels not getting acpi interrupts. This is a
workaround until the real issue with late establishment is fixed.
ok weingart@, beck@, gwk@


# 1.93 03-Nov-2007 jordan

Added support for displaying wakeup devices
ok beck@,weingart@,gwk@


# 1.92 11-Oct-2007 kettenis

When walking the tree yo find _PRT methods, check whether the device is
actually enabled and functioning. Similar to what we do for _INI.

ok weingart@, beck@


# 1.91 08-Oct-2007 krw

More simple memset(,0,) -> M_ZERO changes. In this batch move to
size(*p) as the first malloc() parameter where p is declared locally
and thus easy to check. Add M_ZERO to gpe_table allocation in acpi.c
even though there is no obvious bzero or memset nearby.


# 1.90 13-Sep-2007 mk

Remove unnecessary memset() (see aml_evalnode(9)) and a bit of KNF.

ok toby


# 1.89 13-Sep-2007 weingart

Implement ACPI 6.5.1 spec tree walk for _STA and _INI.
Fixes mk's laptop. No regressions so far (thank you to the
testers).

ok gwk@, mk@, marco@


Revision tags: OPENBSD_4_2_BASE
# 1.88 17-Apr-2007 mk

Now that acpidock no longer hangs machines during boot (at least
according to the reports I've been getting) it should be safe to enable
it again, so remove the #if 0 stuff around it.

ok marco


# 1.87 11-Apr-2007 jordan

Added changes for C-State
ok marco@


# 1.86 26-Mar-2007 marco

Calculate minutes left more accurately.

From giovanni <qgiovanni@gmail.com>


Revision tags: OPENBSD_4_1_BASE
# 1.85 03-Mar-2007 mk

Stop looking for acpidock until we fix that stuff that breaks all pre-60
series ThinkPads with acpidock(4) enabled.


# 1.84 22-Feb-2007 marco

We were getting no interrupts in .MP. There are 2 fixes; one in
acpi_machdep.c and move the interrupt establishment down after ioapic
has been initialized.

ok jordan


# 1.83 21-Feb-2007 kettenis

Make an acpi-enabled GENERIC.MP work on systems that only support PIC mode.

ok marco@


# 1.82 19-Feb-2007 jordan

Added acpi_map_address function for mapping GAS/IO registers
ok marco@


# 1.81 18-Feb-2007 jordan

Added dynamic perfect hash for opcodes
ok marco@


# 1.80 18-Feb-2007 marco

Shave off another K pre compression.
Shuffle SMALL_KERNEL functions around so that we only have a single #ifndef


# 1.79 17-Feb-2007 marco

Add SMALL_KERNEL throught acpi to make it fit on boot media.
ok deraadt


# 1.78 13-Feb-2007 marco

Add smarter _INI code. This helps with some dissapearing devices.

ok kettenis jordan


# 1.77 31-Jan-2007 gwk

Introduce acpi_hasprocfvs thats "ACPI Has Processor Frequency and Voltage
Scalling" a global flag set by acicpu attach to indicate that ACPI is
handling fvs through the _PSS objects. This will fix disappearing
EST/powernow when booting kernels with acpi enabled, as acpicpu isnt
compiled into GENERIC yet. Also in cases where acpicpu is built in and
legacy EST/powernow work but acpi is missing the _PSS object, the legacy
routines will be used.

tested by dim@, and Nick Nauwelaerts <nick AT nauwelaerts.net>
ok marco@, dim@


# 1.76 22-Jan-2007 mk

Actually look for docking stations by looking for _DCK nodes.

ok marco


# 1.75 02-Jan-2007 marco

On some systems EC must be attached as the first device. Move EC from
generic _HID devices to its own routine to force first attach. Bug found
and debugged by joris@

Code mostly written by joris@


# 1.74 26-Dec-2006 marco

Add polling to devices that require it such as acpibat and acpitz.
Use the same tsleep/wakeup mechanism as the interrupt code to keep it non-
concurrent.


# 1.73 21-Dec-2006 deraadt

even more knf love


# 1.72 21-Dec-2006 marco

Remove some leftover acpiec gpe/interrupt code.


# 1.71 20-Dec-2006 deraadt

oops, name already used


# 1.70 20-Dec-2006 deraadt

rename __acpi_enable_gpe(); ok marco


# 1.69 20-Dec-2006 deraadt

acpi_unmap_pmregs() is not used, ok marco


# 1.68 19-Dec-2006 deraadt

simplify some low-level methods substantially (shrinking code); ok marco jordan


# 1.67 06-Dec-2006 mk

malloc(..., M_NOWAIT) with no NULL check is bad. If allocation fails,
complain, clean up, and bail out of attach.

input and ok gwk


# 1.66 28-Nov-2006 jordan

Fixed clearing arg for acpi_init_pic
ok kettenis@


# 1.65 27-Nov-2006 kettenis

Don't spam dmesg.


# 1.64 27-Nov-2006 jordan

Fixes for acpiprt routing
Post-parse fixup for namerefs
ok kettenis@


# 1.63 25-Nov-2006 marco

Don't handle acpiev events inside the gpe handler since this causes deadlocks.

ok jordan@


# 1.62 15-Nov-2006 kettenis

Hook up acpi apic interrupt routing support to generic acpi code.

ok jordan@


# 1.61 02-Nov-2006 marco

Kill acpiec attach function and add its functionality to the generic _HID
device attach

prodded and ok jordan


# 1.60 02-Nov-2006 marco

Fix comments to reflect reality.


# 1.59 19-Oct-2006 marco

Replace ACPI ioctls with APM ioctls. This makes ACPI talk the same protocol
as APM therefore userspace can now retrieve power information in the same
fashion as all other apm arches (zaurus, macppc, i386 etc).

To use force the use of acpi device like so: apmd -f /dev/acpi

ok jordan@


# 1.58 19-Oct-2006 jordan

Fixed GPE debug levels
Fixed ref counter on temporary bufferfields
DSDT parsing fully working on IBM Thinkpad
ok marco@


# 1.57 15-Oct-2006 jordan

Removed old GPE handler code remnants
ok marco@


# 1.56 12-Oct-2006 jordan

Massive ACPI changes
Fixed ACPI leaks
new ACPI API
ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.55 30-Jun-2006 gwk

KNF, No functional change,
"go at it" marco@


# 1.54 30-Jun-2006 jordan

Support for calling Embedded Controller via new GPE interface
ok marco@


# 1.53 30-Jun-2006 jordan

Added new code for GPE handling
Also works with ACPIEC; need to fixup acpiec.c to remove original interrupt handler
ok marco@


# 1.52 30-Jun-2006 jordan

Added offset argument for acpi read/write pmreg routines (new GPE code)
ok marco@


# 1.51 31-May-2006 todd

do not call acpi_powerdown() unless acpi has successfully initialized
ok gwk@ canacar@


# 1.50 29-May-2006 canacar

Add embedded controller support.
ok marco@ deraadt@ testing & ok gwk@


# 1.49 19-May-2006 canacar

Minimal thermal zone driver. Monitors thermal zone temperature,
shuts down the system if the 'critical temperature' is reached.
ok marco@


# 1.48 11-Apr-2006 marco

KNF.


# 1.47 11-Apr-2006 gwk

Add acpi_resume(struct acpi_softc *sc), which calls some AML methods,
detected at attach time, currently unused.

ok marco@


# 1.46 11-Apr-2006 gwk

Discover some ACPI sleep related methods, and call them at the
appropriate time in acpi_enter_sleep_state().

ok marco@


Revision tags: OPENBSD_3_9_BASE
# 1.45 26-Feb-2006 marco

Add acpicpu as a device.

ok jordan@


# 1.44 23-Feb-2006 marco

remove dead function.

ok jordan@


# 1.43 22-Feb-2006 marco

Print out all tables it found during autoconf. We'll need this data in
the future.


# 1.42 22-Feb-2006 marco

Make dmesg less verbose.

ok jordan.


# 1.41 22-Feb-2006 jordan

Added parsing of SSDT tables
ok marco@


# 1.40 22-Feb-2006 jordan

Added new aml_register to notify by device id
Moved powerdown code to button device
ok marco@


# 1.39 21-Feb-2006 marco

Readjust debug levels that crept in too low over the last few days.
KNF


# 1.38 21-Feb-2006 marco

Rig the power button so that the machine shuts down correctly.


# 1.37 20-Feb-2006 marco

Add acpibtn device. Remains disabled for now.


# 1.36 19-Feb-2006 jordan

Added code for aml_notify GPE notification callback
ok marco@


# 1.35 19-Feb-2006 grange

Fix acpi_enter_sleep_state() by replacing current ugly hack
with mostly correct sleeping state transition code.

ok brad@ marco@


# 1.34 19-Feb-2006 marco

jordan:
Enable interrupts.
Added peripheral code to make this work.

marco:
Replace softintr and other types of interrupts with a handler with context.
Fixed interrupt magic for GPEs.
Some KNF

This is work done by jordan and I but has to be a single commit.


# 1.33 17-Feb-2006 marco

Remove earlier introduced delays. We really only need them during autconf.

ok jordan@


# 1.32 17-Feb-2006 marco

Add a generic delay function as per the ACPI spec.

from jordan@


# 1.31 17-Feb-2006 marco

Use tsleep instead of delay when !cold.


# 1.30 17-Feb-2006 marco

Argh, we need delays between reads/writes. The backend reads (i2c most likely)
are so slow that values come back as 0 sometimes. We have to do this
despite the spec saying we shouldnt have to. Guess having a faster
parser than others can hurt sometimes.

Idea from jordan.


# 1.29 17-Feb-2006 jordan

Fixed code for handling _INI
Rearranged debug statements
ok marco@


# 1.28 16-Feb-2006 jordan

Added stub for calling _INI
Fixed aml_dowhile for NULL return
ok marco@


# 1.27 16-Feb-2006 jordan

Fixed parser/evaluator to retrieve battery info+state
Cleanup of parser
Dell laptops return battery status
ok marco@


# 1.26 06-Feb-2006 brad

fix a typo so this will compile on amd64.


# 1.25 04-Feb-2006 marco

When ACPI_ENABLED wasn't set but the devices were it had an unknown variable.

Pointed out by ian@.


# 1.24 03-Feb-2006 jordan

Rewrote aml parser yet again...
Fixed bus_space_map for i386/amd64
Added _BST and _BIF to acpibat to get status/info
ok marco@


# 1.23 20-Jan-2006 jordan

More evaluator changes
Removed old evaluator code
Fixed _HID value
ok marco@


# 1.22 19-Jan-2006 jordan

Added support for read/write of PCI Config space
Help jason@
ok marco@


# 1.21 18-Jan-2006 jordan

Added new evaluation routines
Added helper methods for buffer/field evaluation
Fixed broken indentation on dsdt.c

ok marco@


# 1.20 17-Jan-2006 jordan

Major overhaul of the aml parser. Evaluater temporarily disabled.

ok marco@


# 1.19 06-Jan-2006 grange

Check for ACPI control availability only if ACPI_ENABLE defined.


# 1.18 06-Jan-2006 grange

Informative error messages on attach, prettier dmesg.


# 1.17 06-Jan-2006 grange

printf -> dnprintf conversion for debug messages.


# 1.16 05-Jan-2006 grange

Find SLP_TYPx values and store them for future use in
acpi_enter_sleep_state().


# 1.15 05-Jan-2006 grange

Random KNF and cleanup


# 1.14 05-Jan-2006 grange

Provide symbolic names for the acpi sleeping states and use it.


# 1.13 28-Dec-2005 marco

Moved DEVNAME to a acpidev.h

ok jordan@


# 1.12 18-Dec-2005 sturm

add back comment about wrong code

ok marco


# 1.11 16-Dec-2005 marco

Remove test for kbd_reset. Prompted deraadt@, ok jordan@


# 1.10 16-Dec-2005 jordan

Added hooks for powerdown/power button press


# 1.9 16-Dec-2005 jordan

Cleaned up common power management register i/o
Added enter sleep state ioctl
Hooked power button interrupt
ok marco@


# 1.8 16-Dec-2005 marco

Remove autoconf chatter


# 1.7 16-Dec-2005 jordan

Moved DSDT AML parsing into parent ACPI driver
ok marco@


# 1.6 14-Dec-2005 marco

Add AC device glue.


# 1.5 14-Dec-2005 marco

Adjust debug printing levels to something more sane.


# 1.4 13-Dec-2005 marco

Add glue to attach devices to apci.


# 1.3 07-Dec-2005 marco

KNF


Revision tags: OPENBSD_3_8_BASE
# 1.2 10-Jul-2005 grange

Spacing cleanup.


# 1.1 02-Jun-2005 tholo

Start on a basic ACPI framework -- does not do much more than read out the
ACPI tables into kernel memory and attach ACPI and HPET timers currently.

In order to test this code, enabling the devices in GENERIC as well as
the ACPI_ENABLE option is needed. This code does not do any thermal
control yet, so this should be done with care depending on the platform.

In the tree so more people can contribute to making this more fully
featured.

Ok niklas@ grange@ tedu@


# 1.379 03-Apr-2020 mpi

Fix incoherencies of errors returned by various *kqfilter routines.

EOPNOTSUPP is returned when there's no "filterops" for a given fd.
EINVAL when the requested filter isn't supported by the "filterops".
ENXIO means the underlying device is no longer valid/present.

ok visa@


# 1.378 20-Feb-2020 visa

Replace field f_isfd with field f_flags in struct filterops to allow
adding more filter properties without cluttering the struct.

OK mpi@, anton@


# 1.377 31-Dec-2019 visa

Use C99 designated initializers with struct filterops. In addition,
make the structs const so that the data are put in .rodata.

OK mpi@, deraadt@, anton@, bluhm@


# 1.376 31-Dec-2019 mpi

Convert infinite sleeps to {m,t}sleep_nsec(9).

ok kn@


# 1.375 31-Dec-2019 jsg

include sys/sensors.h for sensor use
don't depend on indirect inclusion from acpidev.h


Revision tags: OPENBSD_6_6_BASE
# 1.374 07-Sep-2019 kettenis

Revert acpipci(4) changes. Too many systems are broken. We'll try again
after 6.6 as been released. The acpireg.h change stays behind.


# 1.373 28-Aug-2019 kettenis

Use ACPI information to attach PCI busses like we do on arm64. There are a
few additional quirks though, and attaching the PCI busses is delayed to
replicate the existing code more closely. That may be changed in the
future. Also tweak how we handle MSI support and respect to ACPI flag
that says we shouldn't attempt to use MSIs.

Some fallout is expected.

ok patrick@


# 1.372 27-Aug-2019 deraadt

On amd64 the bootup and resume paths for SP/BP/SP were not performing
msr/pat, clock-startup, firmware upgrade, fpu initialization, and cpu
soft-state initialization in the same order. That kind of chaos is
crazy and fragile. Improve the acpi resume call sequence and unify order
of the operations.


# 1.371 02-Jul-2019 jcs

acpi_set_gpehandler: turn edge arg into flags, add support for
passing GPE_DIRECT which directly executes the supplied callback and
clears a level interrupt rather than going through acpi_taskq which
can be slow when other driver callbacks are in the queue.

This should not be used for GPE handlers that need to do anything
ACPI-related.

ok kettenis


# 1.370 10-Jun-2019 kettenis

Print proper ACPI version number.

ok mlarkin@


# 1.369 08-Jun-2019 kettenis

When establishing the mapping between ACPI device nodes and PCI devices,
skip PCI host bridges and devices that aren't there.

ok patrick@


# 1.368 07-Jun-2019 kettenis

Introduce acpi_getsta() and use it to eliminate some duplicated code.

ok patrick@


# 1.367 12-May-2019 kettenis

Make sure the extended PM register descriptions actually exist before
using them. Fixes machines with ancient ACPI 1.0 (such as recent
hypervisors).

ok deraadt@


# 1.366 12-May-2019 kettenis

Add support for mapping ACPI PM registers using the "extended" gasio
representation from the FADT. Mostly fixes the Lanner NCA-1510.

ok lteo@, mlarkin@, deraadt@


# 1.365 11-May-2019 lteo

Fix incorrect error message.

ok kettenis@


# 1.364 08-May-2019 tedu

free size. the allocation is the header plus the length.


Revision tags: OPENBSD_6_5_BASE
# 1.363 04-Apr-2019 kettenis

Avoid potential null-pointer dereference. Found by Kent R. Spillner.

ok deraadt@, jsg@


# 1.362 20-Jan-2019 jsg

fix SMALL_KERNEL build after changes in rev 1.361
reported by naddy@ ok deraadt@


# 1.361 19-Jan-2019 tedu

Add a pwraction sysctl that controls what the power button does on acpi.
By default, nothing changes -- shutdown is initiated. But allows turning
power button into a sleep button if desired.
(grudging) ok from a few parties


# 1.360 26-Oct-2018 kettenis

Add acpipci(4) on amd64. For now this only calls the PCI-specific _OSC
method to let the ACPI implementation know what features we support.


Revision tags: OPENBSD_6_4_BASE
# 1.359 30-Aug-2018 kettenis

Move softbutton check before the check that skips attaching certain acpi
devices. Fixes regression caused by matching on _CID in addition to
matching on _HID.

ok matthieu@, mlarkin@


# 1.358 25-Aug-2018 kettenis

Match ACPI devices based on _CID if no match for _HID is found.

ok mpi@


# 1.357 19-Aug-2018 kettenis

Add support for multiple PCI segments. Only really implemented for arm64
for now as amd64/i386 firmware still caters for legacy OSes that only
support a single PCI segment.

ok patrick@


# 1.356 03-Aug-2018 kettenis

Let ahci(4) match on _CLS instead of _HID when attaching at acpi(4). Avoids
having to add many more _HID entries to the match table.

ok deraadt@, mlarkin@


# 1.355 10-Jul-2018 kettenis

Make legacy interrupts work in acpipci(4).

ok patrick@


# 1.354 05-Jul-2018 kettenis

Add acpipci(4), a driver that supports generic ECAM-compatible PCI host
bridges based on information provided by ACPI.

ok mlarkin@


# 1.353 04-Jul-2018 kettenis

Properly pass around the PCI "chipset tag" in acpi(4) and refactor
acpimcfg(4) to call an MD initialization functions that sets up a tag for
PCI ECAM.

ok guenther@, mlarkin@, krw@


# 1.352 01-Jul-2018 kettenis

Move acpi(4) attach glue into acpi_machdep.c.

ok guenther@, deraadt@


# 1.351 01-Jul-2018 kettenis

Add support for _DSD Device Properties.

ok mlarkin@


# 1.350 30-Jun-2018 kettenis

Add intr_enable() function, intended for MI use to amd64 and i386 and use
this in the acpi(4) suspend/resume code paths.

ok deraadt@


# 1.349 29-Jun-2018 kettenis

Use <stdint.h> types.

ok mpi@, jung@, krw@, deraadt@


# 1.348 29-Jun-2018 mlarkin

ACPI: Allow (with warning) GPE handler reassignment, instead of returning
a failure code.

ok kettenis


# 1.347 26-Jun-2018 mlarkin

knf


# 1.346 26-Jun-2018 kettenis

Let acpi(4) pass down the bus dma tag.

ok deraadt@


# 1.345 25-Jun-2018 kettenis

Reorganize acpi(4) code a bit in preparation for upcoming arm64 support.

ok deraadt@


# 1.344 20-May-2018 kettenis

Wake up the acpi thread after scheduling a gpio event.

ok mpi@


# 1.343 17-May-2018 kettenis

Sprinkle some #ifndef SMALL_KERNEL such that RAMDISK kernels cuild again.

pointed out by stsp@


# 1.342 17-May-2018 kettenis

Implement GenericSerialBus OpRegion support.

ok mlarkin@


# 1.341 27-Mar-2018 kettenis

Add acpicmos(4), a driver that implements SystemCMOS OperationRegion
access support. This fixes machines where the AML doesn't check whether
support for this OperationRegion type has been registered by the OS.

ok mlarkin@


Revision tags: OPENBSD_6_3_BASE
# 1.340 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.339 10-Feb-2018 deraadt

Syncronize filesystems to disk when suspending. Each mountpoint's vnodes
are pushed to disk. Dangling vnodes (unlinked files still in use) and
vnodes undergoing change by long-running syscalls are identified -- and
such filesystems are marked dirty on-disk while we are suspended (in case
power is lost, a fsck will be required). Filesystems without dangling or
busy vnodes are marked clean, resulting in faster boots following
"battery died" circumstances.
Tested by numerous developers, thanks for the feedback.


# 1.338 08-Feb-2018 deraadt

After flushing disks, try again to release all memory. We don't need to
save dirty memory to the hibernate space.


# 1.337 08-Feb-2018 deraadt

Toss all releaseable memory, because fragmentation can get in the way
of allocating the hibernate playpen.


# 1.336 08-Feb-2018 deraadt

sr_quiesce() is a new approach for ensuring that softraid drains
output to the disks.

This is part of a larger suspend/resume filesystem-safety diff, which
has been worked on for a couple of months already. Tests by job, krw,
beck, benno, and others. Sometimes even by snapshot users...


# 1.335 29-Nov-2017 kettenis

Add support for GPIO-signaled events.

ok mlarkin@


# 1.334 16-Nov-2017 jcs

Add PCI attachment for dwiic(4) needed by Intel 100 Series machines

ok kettenis


# 1.333 14-Nov-2017 kettenis

Add a few more PNP IDs to the skip lists.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.332 17-Aug-2017 stsp

Make dwiic(4) attach its ACPI parent dependencies.
This change makes the Asus E200HA keyboard work.

Original analysis and patch by Cesare Gargano
ok kettenis@


# 1.331 09-Aug-2017 dcoppa

Add "PNP0303" (8042 PS/2 Controller) to acpi_isa_hids[]

ok kettenis@


# 1.330 22-Jul-2017 jcs

acpibat: look for _BIX first before falling back on _BIF

ACPI 4.0 deprecated _BIF for battery status so some newer machines
are exporting _BIX which extends _BIF's package with a few extra
fields. When using _BIX, export the new discharge cycle count as a
sensor.

feedback from kettenis and anton
ok deraadt


# 1.329 20-Jul-2017 kettenis

Start a list of isa(4) device names such that we can avoid printing them
as "not configured".

ok mikeb@, millert@, deraadt@


# 1.328 08-Apr-2017 deraadt

Sensors are run as callbacks inside tasks. During suspend a sensor
could be running inside a driver that will be force-detached, or due
to tsleep end up disrupting the softstate/hardstate contract. At
suspend time, quisce all these callbacks by waiting for completion.
This issue has never been observed for real, but may be implicated in
suspend/resume failures.
ok kettenis guenther mlarkin


# 1.327 08-Apr-2017 deraadt

Two prototypes for acpi_maptable; merge to one place


Revision tags: OPENBSD_6_1_BASE
# 1.326 27-Mar-2017 deraadt

Now that hibernate_alloc() only has clean success/failure, don't
need to call hibernate_free() to clean up a partial mess.
ok mlarkin kettenis


# 1.325 13-Mar-2017 deraadt

allow ramdisks to build; ok jcs


# 1.324 12-Mar-2017 jcs

try to attach acpisbs first and if successful, don't attach acpibat


# 1.323 02-Mar-2017 natano

Add a new sysctl machdep.lidaction. The sysctl works as follows:

machdep.lidaction=0 # do nothing
machdep.lidaction=1 # suspend
machdep.lidaction=2 # hibernate

lidsuspend is just an alias for lidaction, so if you change one, the
other one will have the same value. The plan is to remove
machdep.lidsuspend eventually when people have upgraded their
/ets/sysctl.conf.

discussed with deraadt, who came up with the new MIB name
no objections mlarkin
ok stsp halex jcs


# 1.322 28-Feb-2017 natano

Untangle abstract sleep modes from ACPI sleep states. This paves the way
for further work in the area.

idea and ok deraadt
ok mlarkin


# 1.321 22-Feb-2017 jcs

add acpisbs, an acpi smart battery subsystem driver reading data
over smbus

currently disabled because it conflicts with acpibat

ok deraadt, kettenis


# 1.320 14-Jan-2017 kettenis

Revert aml_rdpciaddr changes; breaks several machines that were working
before.


# 1.319 11-Jan-2017 jsg

Increase the size of the buffers used for _HID and _CID strings to be
able to fit "Hyper_V_Gen_Counter_V1".

ok mlarkin@ deraadt@


# 1.318 08-Jan-2017 kettenis

Make aml_rdpciaddr() work for for devices behind bridges. The code now
uses the mapping between AML nodes and PCI devices that we establish.
Because _INIT methods may end up calling aml_rdpciaddr(), make sure we
create that mapping early. Also handle devices that aren't actually present.
These devices are now included in the mapping and reads will return an
all-ones patterm whereas writes are a no-op..

ok mlarkin@


# 1.317 25-Oct-2016 pirofti

Add the resource index to aml_parse_resource() callback function.

This is needed by an upcoming acpiec commit that handles machines breaking
the current ACPI specifications.

Change suggested by and ok kettenis@, guenther@


# 1.316 18-Sep-2016 guenther

If the APIC table (MADT) isn't present then ci_acpi_proc_id won't be set
when acpicpu tries to attach, leading to a NULL dereference. The ACPI
processor object on the Dells where this was reported is bogus and useless
anyway so apply the same test when deciding whether to attach to processor
objects as acpicpu would when matching them to cpus.

Problem reported by Eivind Eide (xenofil (at) gmail.com) and
Johan Huldtgren (johan+openbsd-bugs (at) huldtgren.com).
Testing by Eivind Eide.
ok kettenis@ mlarkin@


# 1.315 03-Sep-2016 naddy

Write the system time back to the RTC every 30 minutes.
This fixes the problem that long-running machines which were not
shut down properly would reboot with a badly offset system time.

hints and ok kettenis@


# 1.314 31-Aug-2016 mlarkin

Don't put a device in the wake device list if _STA says it's not there.

Fixes at least one machine that woke up from sleep / poweroff immediately.

ok kettenis@, tested by kettenis and martijn@


# 1.313 28-Jul-2016 kettenis

Store the acpi processor ID/UID in struct cpu_info, and use it to attach
acpicpu(4) drivers to the right cpu(4).

ok mlarkin@, guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.312 10-Jun-2016 kettenis

The power management timer has been made optional in ACPI 5.0A, so don't
attempt to attach acpitimer(4) if the timer isn't present.

Pointed out by jcs@

ok jcs@


# 1.311 08-May-2016 kettenis

Make sure devices listed by a device's _DEP method are attached before
we attach the device itself. The _DEP methode, introduced in ACPI 5.0,
is a hint that the device in question depends on OpRegion support from
the devices listed by _DEP, so we have to order them the proper way.
To prevent us from attaching those devices again when we encounter them
later walking down the device tree, keep track of our attempts to
attach a device driver to them using aflag in the aml_node structure.

ok guenther@


# 1.310 25-Apr-2016 pirofti

We attach acpihpet(4) and acpiec(4) without using the HID, so do not
report it in the dmesg.

OK deraadt@, mlarkin@.


# 1.309 02-Apr-2016 kettenis

Reduce dmesg spam by knocking out some acpi hardware IDs that don't need a
driver.

ok mpi@


# 1.308 29-Mar-2016 kettenis

Check if a device is present (using _STA) in acpi_foundhid. This prevents
us from reporting devices as "not configured" that aren't actually present,
and allows us to remove duplicated code from several drivers.

ok deraadt@, ok jsg@


# 1.307 29-Mar-2016 kettenis

Implement a cleaner way to prevent acpivout(4) from attaching if we have
a platform-specific method of controlling the backlight.

ok millert@, mlarkin@, jung@


# 1.306 28-Mar-2016 kettenis

Adding each and every new acpi device driver to acpi_foundhid() is
gettinga bit of the burden. And it really isn't how our autoconf
framework is supposed to work for busses that can be enumerated. All
the drivers already check for a matching _HID in their attach
function. So we can just drop the checks in acpi_foundhid() and let
the drivers sort themselves out.

Print a "not configured" line for devices for which we don't attach adriver.
This may be a bit much as there are devices for which we will not have a
driver. This will be removed if it causes too much dmesg spam.

ok deraadt@, mlarkin@


Revision tags: OPENBSD_5_9_BASE
# 1.305 17-Jan-2016 jsg

Take acpi_foundhid() outside of #ifndef SMALL_KERNEL so the ramdisk
kernel can find and attach dwiic(4) and sdhc(4).


# 1.304 17-Jan-2016 jsg

remove an unused global variable


# 1.303 14-Jan-2016 kettenis

Attach dwiic(4) on the Intel Bay Trail i2c controllers as well.


# 1.302 12-Jan-2016 kettenis

Improve emulation of PM registers on Hardware-redcued ACPI by emulating
SPL_TYPx, SLP_EN and WAK_STS using the new SLEEP_STATUS_REG and
SLEEP_CONTROL_REG registers. Makes the transition into S5 (powerdown) work
on the ASUS X205TA.

ok deraadt@, mlarkin@


# 1.301 12-Jan-2016 jcs

Add dwiic, a driver for the Synopsys DesignWare i2c controller found
on the Samsung ATIV Book 9 laptop. This initial version only
supports ACPI config/attachment.

Add ihidev, a HID-over-i2c driver largely based on uhidev. dwiic
handles attaching ihidev devices found in ACPI.

Add ims, a HID-over-i2c mouse/trackpad driver to get basic cursor
and button functionality from HID-compliant i2c trackpads.

ok kettenis deraadt


# 1.300 11-Jan-2016 kettenis

Make sdhc(4) attach to hardware IDs "80860F14" and "PNP0FFF".


# 1.299 09-Jan-2016 kettenis

Initial support for Hardware-Reduced ACPI. This skips the legacy mode checks
by emulating the PM_CNT register such that SCI_EN is always enabled. It also
skip attaching acpitimer(4) since the relevant registers are no longer there.

ok deraadt@, mlarkin@


# 1.298 06-Jan-2016 kettenis

Follow the spec and check the SCI_EN bit to see if ACPI has already
been enabled and don't bother enabling it again if it is. This should fix
systems that don't implement legacy mode and therefore set ACPI_ENABLE and
ACPI_DISABLE to zero.

ok mlarkin@, yasuoka@


# 1.297 23-Nov-2015 reyk

Back out the CRITICAL change, turn it back from 10% to 25%. Some
people with old/broken batteries didn't like this change, and the
underlying heuristics are wrong.


# 1.296 22-Nov-2015 reyk

The "CRITICAL" APM battery level, that is exported/faked by the ACPI
stack, is currently set to 25%. With 25% battery left, a typical
modern laptop will have at least one hour runtime left. It is an
arbitrary value, and other APM power layers in the kernel set CRITICAL
to something like 5% or 10%, so set it to 10% for now.

OK mlarkin@


# 1.295 28-Sep-2015 deraadt

In low-level suspend routines, set cold=2. In tsleep(), use this to
spit out a ddb trace to console. This should allow us to find suspend
or resume routines which break the rules. It depends on the console
output function being non-sleeping.... but that's another codepath which
should try to be safe when cold is set.
ok kettenis


# 1.294 06-Sep-2015 deraadt

clarify malloc arguments (base + length...)


# 1.293 28-Aug-2015 deraadt

fairly simple sizes for free(); ok tedu


# 1.292 20-Aug-2015 kettenis

Respect the access size when reading or writing to pci config space.
This version also makes sure writes are properly aligned, preventing
the panics seen with the previous incarnation of this change.

Fixes battery status passthrough in vmware, brightness keys on my x220
and who knows what else.

ok mlarkin@, deraadt@


Revision tags: OPENBSD_5_8_BASE
# 1.291 04-Aug-2015 deraadt

If we are setting ACPI_PM1_BM_RLD do it only once, and remember to
repeat this action at resume time.
ok mlarkin guenther


# 1.290 29-Jul-2015 deraadt

Something subtle in previous revision is locking up suspend or booting
on some laptops. Mailing lists threads mention x220 and Asus machines.
Reverting for now.


# 1.289 21-Jul-2015 kettenis

iRespect the access size when reading or writing to pci config space.
Fixes battery status passthrough in vmware and who knows what else.

Tested by many.

ok mlarkin@, deraadt@


# 1.288 17-Jul-2015 mlarkin

fix comments


# 1.287 26-Mar-2015 jsg

Use the 0x10 extended capability to determine if the trackstick
buttons are wired to the trackpad and need to be re-routed to the
trackstick. Without this change the buttons on 2015 thinkpads
get picked up as extended buttons that show up as scroll up/down.

Remove the X1 Carbon 2015 (LEN0048) and X250 (LEN0046) from the
top button area/soft buttons quirks list. Also avoid using the
quirk list entirely if the capability bit is set.

Based on work from jcs@ with feedback from mpi@
ok mpi@ jcs@


# 1.286 20-Mar-2015 kettenis

The ACPI standard says that a PCI function number of FFFF can be used to refer
to all functions on a device. This makes no sense in the context of accessing
PCI config space. Nevertheless stsp@ has a machine with AML that tries to
do config space reads from a device where _ADR encodes function number FFFF.
This is almost certainly buggy AML, but since Linux (and presumably Windows)
boot just fine on this machine we need to work around this. Linux effectively
treats function FFFF as function 7, which isn't present on this machine.
Instead we simulate a read from a non-existent device by returning a bit
pattern of all ones. Writes to function FFFF will still panic.

ok mlarkin@


# 1.285 16-Mar-2015 deraadt

Reduce use of SMALL_KERNEL in acpi, bringing acpiec driver and more gpe
handling into RAMDISK. This is now possible because the install media
has ample room. The goal is to reduce special cases where we may be
skipping (unknown) important operations...
ok mlarkin kettenis


# 1.284 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.283 07-Feb-2015 deraadt

New framework that allows hibernate to pass in entropy from it's fresh
boot.
ok mlarkin


# 1.282 06-Feb-2015 mlarkin

Fix a hibernate crash on some machines due to unmapping a page that
may not have been mapped previously (in the failure to hibernate case).

Also ensure that the lowmem ptp is mapped in all cases (not just MP).

ok kettenis


# 1.281 17-Jan-2015 deraadt

resume_randomness() before spinning up other cpus...


# 1.280 15-Jan-2015 jsg

Define a new wscons mouse type for Synaptics clickpad devices that lack
physical buttons. This will be used if the acpi pnpid for the mouse
matches a list provided by Synaptics found in Linux.

Combined with the xenocara changes this will remove the need for an xorg
config file for the x240/t440/t540 generation of thinkpads.

ok matthieu@ shadchin@ kettenis@


# 1.279 11-Jan-2015 kettenis

Tolerate checksum errors in ACPI tables (but complain about them). Apparently
there are machines out there with broken BIOSen with such checksum errors in
thier RSDT tables. Windows runs fine on these machines, so it probably does
not check. Linux ignores checksum errors as well (and prints a warning).

ok deraadt@, krw@


# 1.278 18-Dec-2014 deraadt

suspend and resume the RNG (at what looks like the right spots)
ok reyk djm


# 1.277 09-Dec-2014 doug

Replace some malloc(n*size,...) calls with mallocarray().

ok tedu@ deraadt@


# 1.276 06-Dec-2014 mpi

If "machdep.lidsuspend" is not set, do not go back to sleep if the lid
is still closed when resuming. This allows docked laptop to be resumed
without opening them.

ok mlarkin@


# 1.275 23-Nov-2014 mlarkin

If a machine has no _LIDs, then none can be open. This diff fixes a problem
where machines without any _LIDs were going back to sleep after hibernate
due to the recent suspend-if-lid-closed-on-resume diff. Noticed on qemu,
which doesn't have any _LIDs.

discussed with deraadt


# 1.274 18-Nov-2014 krw

Nuke yet more obvious #include duplications.

ok miod@


# 1.273 08-Nov-2014 mlarkin

If resuming from sleep (zzz/ZZZ) and the lid is still closed, go back to
sleep. This prevents accidental lid flex or slight opening in a backpack
from waking the machine up and leaving it resumed (powered on).

ok deraadt@


# 1.272 26-Sep-2014 kettenis

Rework piglet and pig allocation. Currently the piglet gets allocated
deep down in the suspend path, where it is really hard to recover from
allocation failure. So allocate the piglet early on in the suspend path.
Also change the piglet and piglet allocation functions to use km_alloc(9)
instead of doing pmemrange magic. This removes a bunch of code which, in the
case of the piglet allocation, is broken since it results in a NULL pointer
dereference. Also switch the piglet allocation to not wait. If we can't
allocate 16MB of phys contig memory on a halfway modern machine we're almost
certainly under a lot of memory pressure and we're better off not trying to
hibernate anyway.

ok mlarkin@


# 1.271 25-Sep-2014 kettenis

Bring back rev 1.249. Now that mp_setperf() has been fixed, it should be
safe to restore perflevel when resuming. Should fix the issues people
see with the processor running at full speed heating up the machine while
the apm command reports that the CPUs are running at a low speed.

ok dcoppa@, deraadt@


# 1.270 23-Sep-2014 kettenis

Release the acpi lock around calls to wsdisplay_suspend() and
wsdisplay_resume(). The X server may adjust the display brightness just
before/after the VT switch when suspending/resuming. In fact, it may
already be blocked adjusting the display brightness for some reason
unrelated to the suspend request.

The VT switch request will eventually time out, but the delay is annoying
and the failed VT switch will lead to problems after resume.

Needed for a future update of the xf86-video-intel driver.

ok mlarkin@


# 1.269 19-Sep-2014 kettenis

Use config_suspend_all(9).


# 1.268 08-Sep-2014 kettenis

Clear the PME Status bit on PCI devices in response to Device Wake events.
Fixes the interrupt storm on the 2009 Mac mini with WOL enabled on its
nfe(4) interface.

ok mlarkin@


Revision tags: OPENBSD_5_6_BASE
# 1.267 20-Jul-2014 mlarkin

Support hibernating to softraid crypto volumes.

much help and ok from deraadt@


# 1.266 16-Jul-2014 mlarkin

Reenable hibernate RLE support and flush+zero all memory after unpack.


# 1.265 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.264 12-Jul-2014 kettenis

Add a function to drop all clean pages on the page daemon queues and call
it when we hibernate.

ok mlarkin@, miod@, deraadt@


# 1.263 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.262 11-Jul-2014 mlarkin

Flush the buffercache to 16MB on hibernate and restore its previous max
size (kern.bufcachepercent) on resume, for better hibernate performance.

ok beck@


# 1.261 10-Jul-2014 blambert

remove unneeded header

ok mlarkin@


# 1.260 01-Jul-2014 kettenis

Adding an additonal delay after each access to SystemMemory or SystemIO
during autoconf makes no sense.

ok deraadt@


# 1.259 23-Jun-2014 kettenis

SystemMemory is used to access memory mapped registers on some machines, so we
must use the bus_space(9) API to access it instead of memcpy(9). Also make
sure we properly align access to fields that need more than byte alignment.

ok guenther@


# 1.258 21-May-2014 mlarkin

Fix invalid / missing format specifiers in several dnprintf statements
(used only when ACPI_DEBUG is enabled)

From a diff on tech@ by Fabian Raetz, with a minor change by me, tested
on i386 and amd64 with ACPI_DEBUG enabled and disabled.


# 1.257 25-Apr-2014 mlarkin

Defer the free of the hibernate structures until later, when it is safe to
do so.

Freeing the hibernate structures too early leads to inadvertent calls into
splx when we aren't ready for them yet. Found when trapping splx calls in
the resume path.

discussed with deraadt@ at m2k14


# 1.256 13-Apr-2014 mlarkin

Fix a badly worded debug message that implied the entire machine was going
to sleep, instead of just the acpi thread going to sleep.


# 1.255 31-Mar-2014 mpi

Change the order of operations during a suspend/resume cycle and
call bufq_quiesce() after executing the DVACT_QUIESCE handlers.

This should be safe since no disk nor controller drivers have such
handler but it will allow us to detach sd(4) devices attached to a
USB bus.

Another benefit pointed out by kettenis@ is that drivers that need
to read a firmware from the disk should be able to do it at resume
time in a DVACT_WAKEUP handler.

ok kettenis@, deraadt@


# 1.254 13-Mar-2014 dlg

get rid of the assumption that the head of the alldevs list is the
"mainbus" device. this breaks when mpath is enabled because it
attaches before mainbus and therefore takes the head position.

have autoconf provide device_mainbus() which looks up mainbus_cd,
and use that instead.

discussed with deraadt who just wants mpath stuff to move forward
despite there being many ways to shine this particular turd.


Revision tags: OPENBSD_5_5_BASE
# 1.253 21-Feb-2014 deraadt

Do the lid control further on in the resume sequence, since this is
AML, and will do WAITOK operations. While cold, that is a bad idea.
We seem to be safe with respect to the spec, I do not see any event
debounce issues here.
ok kettenis, tested by many


# 1.252 20-Jan-2014 claudio

Instead of checking sc->sc_revision <= 1 check that the
fadt->hdr_revision <= 1. This is a better check since it checks
the version of the proper descriptor. This allows the alix APU
to reboot since those boards have currently a wrong sc->sc_revision.
Committing this now so that it can be tested easier.
With and OK jsg@ also fine by derradt@


# 1.251 24-Dec-2013 kettenis

If the FADT has its SMI_CMD set to zero, assume we're only ACPI-only hardware
and don't need to disable SMI ownership of the ACPI hardware registers.

ok mlarkin@


# 1.250 23-Dec-2013 kettenis

Revert previous commit. It makes resume hang on some MP systems running
GENERIC.MP.


# 1.249 16-Dec-2013 kettenis

Restore hw.setperf upon resume like we do for apm(4) on i386. Seems we need
to do this fairly late in the resume to avoid hangs.

ok deraadt@, mlarkin@


# 1.248 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


# 1.247 06-Nov-2013 mpi

Add support for Power Resources for Dx states and the necessary hook
for PCI devices. This hook should be called twice, before and after
changing the power state of a PCI device.

Before setting the device to the new state, the ACPI layer will notify
every power resources linked to the device for that state and make sure
they are turned "_ON". After changing the state of the device, it will
decrement the reference of every power resources linked to that device
for the old state and turn them "_OFF" if they are no longer referenced.

This fixes the no-USB after resume problem seen on various ThinkPad,
problem initialy diagnosed with Alexander Polakov.

ok kettenis@, deraadt@


Revision tags: OPENBSD_5_4_BASE
# 1.246 01-Jun-2013 mlarkin

Stop printing ACPI wakeup devices in dmesg after the 16th wakeup device.

Helps with VMware VMs that report hundreds of wakeup devices.

ok reyk@, deraadt@


# 1.245 31-May-2013 bluhm

Fix the build for a kernel without wd and pciide in its config.
OK deraadt@ tedu@


# 1.244 30-May-2013 deraadt

Enforce ca_activate tree-walks over the entire heirarchy for all events,
cleaning up some shutdown-hook related code on the way.
(A few drivers related to sparc are still skipped at kettenis' request)
ok kettenis mlarkin, tested by many others too


# 1.243 18-Apr-2013 deraadt

pin the acpi thread to the BSP, because AML and SMI cannot be trusted. I
was certain we were doing this already, but it turns out no...


Revision tags: OPENBSD_5_3_BASE
# 1.242 24-Dec-2012 guenther

In acpiioctl(), Return though the splx() path if suser() returns an error

ok deraadt@


# 1.241 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


# 1.240 04-Oct-2012 ehrhardt

Use information provided by ACPI to attach secondary PCI host bridges.

ok kettenis@


# 1.239 07-Sep-2012 kettenis

Add a new function called acpi_pci_min_powerstate() that returns the lowest
power state for a PCI device for the power state (currently S3 or S4) we're
transitioning into. This function is probably too simplistic; there is a lot
of (silly) complexity in the ACPI spec.

ok deraadt@


Revision tags: OPENBSD_5_2_BASE
# 1.238 13-Jul-2012 pirofti

Disable acpivideo(4) on ASUS laptops.

There are some models that panic whenever an event is received.
Some cope, some don't. In order to get this right acpiec(4) requires
major rework. Until then let machines boot.

Discussed with deraadt@.


# 1.237 11-Jul-2012 mlarkin

for now, restrict hibernate to root in the acpi ioctl kernel interface.
normal rules for controlling this interface through apmd still apply.

ok deraadt@


# 1.236 09-Jul-2012 deraadt

tiny little oops in untested hibernate block


# 1.235 09-Jul-2012 deraadt

clean up ioctl() case statements


# 1.234 09-Jul-2012 deraadt

acpi_disable_onegpe() is not used; ok pirofti


# 1.233 24-May-2012 kettenis

If _MIN > _MAX consider a WordBus resource to be invalid. Fixes a panic on
the IBM x3550-M4.

ok mikeb@


# 1.232 29-Mar-2012 mlarkin

Missing #ifdef in previous diff


# 1.231 29-Mar-2012 mlarkin

Prevent hibernation attempts on machines with unsupported disk controllers.
The same check is also performed later in the hibernate sequence, but by
then it's too late to cleanly unwind (presently).

ok deraadt


# 1.230 26-Mar-2012 deraadt

simplify conditionals in acpi_sleep_state()
ok mlarkin


# 1.229 26-Mar-2012 deraadt

hook in the hibernate request code; half of this diff is from mlarkin
ok mlarkin


Revision tags: OPENBSD_5_1_BASE
# 1.228 20-Sep-2011 deraadt

Very early in suspend while the clock is still ticking realtime,
save the clock back to the rtc. The zaurus already did this.
ok phessler miod kettenis


Revision tags: OPENBSD_5_0_BASE
# 1.227 02-Jul-2011 nicm

kqueue attach functions should return an errno or 0, not a plain 1. Fix
the obvious cases to return EINVAL and ENXIO.

ok tedu deraadt


# 1.226 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.225 16-Jun-2011 pirofti

Import acpitoshiba driver.

This is originally written by Hiroyuki Aizu, ported and integrated
in our tree by Javier Vazquez <javama4 AT gmail> with tweaks and
suggestions by me.

Import prodded by deraadt@ and the lack of testers on tech@.


# 1.224 27-Apr-2011 jcs

attach acpithinkpad to newer lenovo models like the x120e

ok deraadt@


# 1.223 22-Apr-2011 jordan

Fix uninitialzied variables and formatting strings (-Wxxx errors)


Revision tags: OPENBSD_4_9_BASE
# 1.222 02-Jan-2011 jordan

Re-add code for ACPI taskqueue, eliminates need for state vars in acpi softc
Serialize processing ACPI tasks for sleep/powerdown/GPE event
ok deraadt@


# 1.221 31-Oct-2010 guenther

Revert last commit: it breaks resume on Thinkpads
"then please back it out" deraadt@


# 1.220 26-Oct-2010 jordan

Add task queue for ACPI gpe and notify handlers
This fixes eject on Dell Latitude dock and an issue on the Dell Mini
battery update.
ok deraadt


# 1.219 07-Oct-2010 jordan

Create separate functions for enabling wake and runtime gpes.
Remove old suspend/resume gpewalk function
ok deraadt, mlarkin


# 1.218 05-Oct-2010 jordan

split acpi_enable_onegpe to separate enable/disable functions
ok deraadt


# 1.217 05-Oct-2010 jordan

Added acpi_disable_allgpes function to clear GPE status
ok deraadt


# 1.216 05-Oct-2010 deraadt

clean up acpi_softc use more


# 1.215 05-Oct-2010 deraadt

delete a bit of #if 0 code


# 1.214 05-Oct-2010 deraadt

hide use of our global acpi_softc pointer a bit better


# 1.213 29-Sep-2010 deraadt

grab the acpi thread lock very early in acpi thread startup; not that it
is likely that anyone will race us... but why be careless.
ok kettenis


# 1.212 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.211 29-Aug-2010 jasper

- add a check for FWRITE here too.

ok miod@


# 1.210 27-Aug-2010 kettenis

Make PCI Power Management optional, and only enable it when acpi(4) attaches.

ok deraadt@


Revision tags: OPENBSD_4_8_BASE
# 1.209 08-Aug-2010 kettenis

Close race between the acpi thread and wscons ioctls running in process
context. We might rip this out post-release and replace it with a solution
that serializes things in a more obvious way.

ok deraadt@, miod@ and "those crazy texans who ok anything"


# 1.208 08-Aug-2010 deraadt

Report the acpi sleep states when on a RAMDISK; ok krw


# 1.207 07-Aug-2010 kettenis

Enforce acpi register access rescrictions.

ok mlarkin@, deraadt@, tested by marco@


# 1.206 07-Aug-2010 deraadt

upon resume, notify all the acpiac and acpibat drivers to update
their status. some laptops do not do this automatically.
ok kettenis canacar


# 1.205 06-Aug-2010 marco

Always call _PSW on _LID devices that support it to enable lid open to
resume the machine. Conversely disable it when the machine wakes up.

Tested by several ok deraadt


# 1.204 06-Aug-2010 deraadt

As part of a revamp of the PM1/GPE code, I write this diff to look for
unmanaged EN & STS bits in the PM1 register at interrupt time and report
them. As a side effect this splits the STS acknowledgement into two writes
(for power, and sleep) instead of one. The printf that is added (to spot
unmanaged STS bits) has not yet been triggered as far as we know.

Before the "write to PM1 registers at the right offset" diff went in, this
was not neccessary. But newer thinkpads do not have a working soft power
button without this diff.

We have no idea why.
ok mlarkin kettenis


# 1.203 05-Aug-2010 deraadt

PM1 power button events were generating 'fake events' to acpibtn PWRB,
but if there is no such device these were being tossed. Instead, arrange
for all power button handlers to flag a softc variable which is processed
by the thread; so that one place will know to request a powerdown (psignal
to init)
ok marco mlarkin


# 1.202 05-Aug-2010 deraadt

rename a variable which is annoyingly poorly named


# 1.201 05-Aug-2010 mlarkin

Write to the correct offset for various ACPI pmreg registers. Writing to
the correct register does properly clear the wake status bits and fixed
event bits, which allows some machines to have more than 1 suspend cycle.

bug spotted by deraadt@

tested by deraadt@ and myself on a half-dozen machines and others on
hackers@

ok deraadt@, marco@


# 1.200 05-Aug-2010 deraadt

Fix the power button. The pm1 register is 16 bits in size.
My mistake, a while back.
tested by krw


# 1.199 04-Aug-2010 kettenis

Print supported sleep states.

ok marco@, phessler@, deraadt@


# 1.198 03-Aug-2010 mlarkin

Use the proper flag for re-enabling certain hardware events (power btn,
sleep btn). The (incorrect) flag prevoiously used coincidentally had the
same value, so this shouldn't cause any different behavior than before.

tested on a variety of machines (i386, amd64, sp, mp) - no behavioral
change seen

ok deraadt@


# 1.197 28-Jul-2010 marco

Make all gasio failures look and behave the same.


# 1.196 28-Jul-2010 mlarkin

Delay a little longer on suspend before giving up. Fixes machines that
sometimes don't suspend because the sleep transition takes too long
(symptoms like failing to sleep but the backlight stays on).

ok deraadt@


# 1.195 27-Jul-2010 deraadt

remove XXX; EC can now be attached early when needed; ok jordan


# 1.194 27-Jul-2010 deraadt

more brackets feels good


# 1.193 27-Jul-2010 deraadt

XXX annotate a block in acpi_enter_sleep_state() that is pretty worrying


# 1.192 27-Jul-2010 deraadt

horrific indentation that nearly made me puke


# 1.191 27-Jul-2010 mlarkin

Set the sleeping indicator light on machines that support it, in case SMI
doesn't do it for us. Shows the 'moon' LED on older Thinkpads when
sleeping.

Tested on T43p, W500, T510 by me and on X61s by deraadt@.

ok deraadt@


# 1.190 27-Jul-2010 jordan

Early initialization of acpiec if ECDT table exists
Fixes hang when booting thinkpads while docked
ok deraadt


# 1.189 26-Jul-2010 deraadt

remove merge error which was commited


# 1.188 26-Jul-2010 pirofti

Add support for Sony ACPI hotkeys via a new driver: acpisony(4).

Currently it only works for the suspend button and tries to do right for
the brightness events, but I haven't found a Sony laptop that like to do
right so far.

In the future I want to make the brightness keys work on all Sony's and
also add support for the zoom hotkeys and whatever other funky keys I can
find on those things.

Okay deraadt@.


# 1.187 22-Jul-2010 deraadt

pretty large cleanup of SMALL_KERNEL


# 1.186 21-Jul-2010 deraadt

in the acpi GPE handler, late ack level interrupts not edge interrupts.
Duh.


# 1.185 21-Jul-2010 deraadt

Merge the edge and level gpe handlers into one (now possible since the
gpe structure tells us whether the gpe is edge or level)


# 1.184 21-Jul-2010 deraadt

delete #if 0 code we are not going to use; ok marco


# 1.183 20-Jul-2010 deraadt

make the mask code for acpi_enable_onegpe() match other similar code


# 1.182 20-Jul-2010 deraadt

Handle the acpi interrupt controller with proper edge/level handling,
wrapped in spltty to avoid racing against the interrupt controller.
Repair the gpe bit masking code while there, and do operations in the
right order
ok kettenis mlarkin, help from jordan at figuring out the order of
operations.


# 1.181 20-Jul-2010 matthew

When we're preparing to go to sleep, only call bufq_restart() if other
preparations failed.

ok kettenis@, thib@, deraadt@


# 1.180 19-Jul-2010 deraadt

permit compilation with ACPI_DEBUG


# 1.179 19-Jul-2010 deraadt

there is no need for a seperate dsdt_softc pointer; dsdt does not have a
softc but is just a part of the single acpi driver.
ok jordan


# 1.178 19-Jul-2010 deraadt

acpi_delay() is no longer needed; ok jordan


# 1.177 18-Jul-2010 mlarkin

Clear event status on resume, to avoid going back to sleep/reboot/shutdown
immediately after resume. Tested by a bunch of people on hackers@ on
various hardware. Fixes at least Toshiba NB200 and Gateway LT3103u.

ok marco


# 1.176 13-Jul-2010 deraadt

rename some thread related interfaces so they are logical


# 1.175 10-Jul-2010 jordan

Verify _ADR bdf is correct (some systems return 0xffff)
Works on Dell x9xx and Studio 1555


# 1.174 09-Jul-2010 jordan

Clear the noise


# 1.173 08-Jul-2010 jordan

Add mapping for ACPI device to PCI bdf (match autoconf tree)
Simplify resource parsing function to use buffer argument
Convert namespace linked lists to use queue macros
ok marco@, deraadt@


# 1.172 06-Jul-2010 deraadt

Use spltty() to lock downcalls from apm against the information being
modified by the acpi thread.
ok kettenis


# 1.171 06-Jul-2010 deraadt

Don't use a workq to do the suspend, because it races aginst the acpi
thread. Instead, just tell the acpi thread to do the suspend for us.
This makes apmd & zzz work correctly. While here, have acpithinkpad
attempt to post the event to apm before prompting the suspend itself.
ok kettenis marco mlarkin


# 1.170 05-Jul-2010 mlarkin

Reset register SCI_EN on resume. The ACPI spec clearly states we don't have
to do this, but many machines don't, or forget to, which leads to all sorts
of problems on resume (broken battery stats, overheating, failure to resume
more than once, etc).

tested by many, ok marco@, deraadt@


# 1.169 01-Jul-2010 thib

do bufq quiesce again, since vnd/the bufq_done call in
biodone has been fixed.


# 1.168 01-Jul-2010 jordan

Backout recent AML changes, breaks on R210 and others


# 1.167 01-Jul-2010 jordan

Simplified aml_parse_resource call, cleanup code
ok marco


# 1.166 01-Jul-2010 jordan

Fix subordinate bus # for multi-root PCI buses
ok marco, kettenis


# 1.165 30-Jun-2010 thib

Disable/partially backout the bufq quiesce changes as this
is causing havoc with vnds and release must be buildable.


# 1.164 29-Jun-2010 deraadt

Fix ramdisks (new code was placed inside a SMALL_KERNEL chunk)


# 1.163 29-Jun-2010 jordan

Add support for mapping ACPI to PCI devices
ok kettenis, deraadt


# 1.162 29-Jun-2010 kettenis

Call bufq_quiesce() on suspend to stop all I/O and bufq_restart on resume.
Makes suspend/resume work much better while doing disk I/O.

ok mlarkin@


# 1.161 29-Jun-2010 tedu

60 is a more reasonable guess for number of minutes in an hour than 100.
spotted by TeXitoi. ok deraadt


# 1.160 27-Jun-2010 jordan

Add _acpi_matchhids routine, precursor to pci device match


# 1.159 27-Jun-2010 mlarkin

Clear ACPI fixed event status on resume (power buttons/etc) so that some
machines don't immediately resume after suspending to S3.

Tested by kettenis@ and I on 5 or 6 machines, deraadt@ "yeah, looks good"


# 1.158 27-Jun-2010 jordan

Cleaned up acpi_maptable, adds to table list directly


# 1.157 07-Apr-2010 deraadt

If we have done a wsdisplay_suspend(), and ACPI decides it does not want to
suspend, we must roll back by calling wsdisplay_resume() or the virtual
console switching will be locked forever
ok oga


# 1.156 07-Apr-2010 kettenis

Make suspend/resume work on MP machines (running an MP kernel). Joint work
from mlarkin@ and me, with some amd64 fixes thrown in by deraadt@

ok marco@, deraadt@, pirofti@, mlarkin@


# 1.155 31-Mar-2010 kettenis

Bring /dev/apm support in line with apm(4). Make the suspend button send
a suspend request event to apmd(8) instead of suspending immediately. Also
keep track of whether /dev/apm and /dev/apmctl are currently open, such that
we can still suspend immediately if apmd(8) isn't running.

ok deraadt@, marco@, pirofti@, jsing@, oga@


# 1.154 30-Mar-2010 oga

Prevent the apmd/x races for good.

When we hit suspend time, go through all wsdisplays on the system. if
they are in mode MAPPED, but not MODE_DUMBFB then if possible do a full
vt switch to a !mapped vt, and prevent switching back until resume time.
This has to be called from MD code because this involves userland
running so that X can run the vt switch signal handler. This way, any
case where we are using the "poke registers from userland" model, we
will not be on the hardware when we go down, so the kernel can actually
handle thing properly.

Tested on several acpi laptops (by kettenis@ and ian@), x40 (me and
beck@ at LEAST) and zaurus (me). Maybe others, but if so I forgot who at
this time..

Idea from deraadt somewhere over the Faroe Islands (I thought of a
similar thing myself a while ago). Much prompting from him. Ok and
comments miod@


# 1.153 25-Mar-2010 oga

Fix & vs. && in the kqfilter code.

Based on a comparison with the apm code.

ok deraadt@, kettenis@


Revision tags: OPENBSD_4_7_BASE
# 1.152 26-Nov-2009 mlarkin

Visual (and on some machines, audio) feedback while the machine is
resuming via calls to aml _SST method. This also makes the "moon" LED on
thinkpads to blink during resume and cease when resume is completed.

ok pirofti@


# 1.151 26-Nov-2009 deraadt

Toggle into "cold" when we we disable interrupts, because tsleeping
in the AML interpreter and context switching into userland processes
tends to sometimes pop the ACPI trampoline page out of our address
space. Makes lots more machines work.
ok kettenis mlarkin


# 1.150 24-Nov-2009 jsg

in the resume path evaluate _WAK before dealing with device drivers
like the spec says, not after.

ok mlarkin@


# 1.149 24-Nov-2009 deraadt

abort suspends on SMP systems until that gets written; ok mlarkin


# 1.148 23-Nov-2009 mlarkin

pci should return the result of it's children's suspend/resume calls back
to its parent.

handle suspend failure case and unwind if devices have an activate function
that returns failure.

ok deraadt@, kettenis@


# 1.147 23-Nov-2009 mlarkin

acpi* devices need to call their children on suspend/resume.
ok deraadt@


# 1.146 23-Nov-2009 pirofti

Remove ACPI_SLEEP_ENABLED checks.

This enables by default the suspend/resume paths in the kernel.

Okay deraadt@.


# 1.145 23-Nov-2009 deraadt

knf and spacing that are super annoying


# 1.144 23-Nov-2009 mlarkin

Hook up Theo's new children suspend/resume function, as well as more ACPI
suspend/resume glue.

ok deraadt@


# 1.143 26-Oct-2009 deraadt

Do not do strncmp comparisons on dv_xname because longer device names which
look similar could arrive in the future. Instead, compare directly against
dv_cfdata->cf_driver->cd_name
Issue originally spotted by miod


# 1.142 26-Oct-2009 jordan

Verify checksum+address when loading ACPI tables.
Some systems had invalid entries in RSDT/XSDT.
ok marco@


# 1.141 23-Jul-2009 cnst

New aibs(4) driver for ASUSTeK AI Booster (ACPI ATK0110) hardware monitoring,
with sensor state support through limits provided by the ACPI.

Tested on several ASUS motherboards kindly networked by Sam Fourman Jr.

ok deraadt marco jordan


Revision tags: OPENBSD_4_6_BASE
# 1.140 03-Jun-2009 pirofti

Import acpi power resource driver. This is needed for suspend/resume logic.

Okay jordan@ and marco@.


# 1.139 03-Jun-2009 pirofti

Add acpivideo support. This adds brightness support for all laptops
except thinkpads, they will use the acpithinkpad driver. The driver is
also hooked into wsconsole. So brightness can be adjusted via:

$ wsconsctl display.brightness=<percentage>

This is very helpfull on some laptops that have a nasty bios and get two
steps instead of one when pressing the brightness button.

Tested on various dell, fujitsu, acer, samsung and other laptops.

Okay marco@, miod@. Suggestions from kettenis@.
Lots of reviews and help from miod@, thanks!


# 1.138 03-Jun-2009 jordan

Added ejectable bay for IDE cdroms
ok marco@


# 1.137 30-Apr-2009 marco

Oga changed apm ioctls and forgot to grep. Temporary work around by hitting
the sleep button when we get any request. This interface will chnage at
some point.
ok oga


# 1.136 19-Apr-2009 krw

Call acpi_sleep_walk() when powering down just like when entering
any other sleep state. This tells acpi wake devices to not wake up
the box until manual powering up resets their status. Makes my Dell
GX520 stay powered off in response to 'halt -p'.

In snaps for a while.

ok marco@ (several times) deraadt@


# 1.135 17-Apr-2009 pirofti

Add device ranking support for future sleep/wake developments.

okay marco@


# 1.134 11-Mar-2009 jordan

Cleanup.. use aml_evalinteger instead of aml_evalname
ok marco@


# 1.133 10-Mar-2009 jordan

Fix cut/paste error.. call _BFS not _PTS
ok marco@


Revision tags: OPENBSD_4_5_BASE
# 1.132 21-Feb-2009 marco

overzealous cut/paste; found by Vladimir Kirillov <proger@uaoug.org.ua>


# 1.131 20-Feb-2009 marco

Oops didn't mean for Debugger() to remain there.


# 1.130 19-Feb-2009 marco

suspend/resume bits so that we can develop this in tree. This is disabled.
code from mlarkin and me
help from art,toby,jordan and several others
ok jordan, go for it deraadt


# 1.129 10-Feb-2009 jordan

Fix null pointer for shutdown if acpi not enabled
ok marco@


# 1.128 04-Feb-2009 kettenis

Some systems that claim to be ACPI 2.0 apparently don't provide an XSDT
address. Make the code fall back on the RSDT on those systems.

From Joerg Niendorf <p3y80@internode.on.net>. Fixes PR 5986.


# 1.127 17-Jan-2009 krw

A couple of stray malloc/memset -> malloc(M_ZERO), a bit of knf and
lose a couple of unnecessary casts. No functional change.

Feedback from kettenis@

ok kettenis@ marco@


# 1.126 06-Nov-2008 marco

Collapse all sanity checks of hids into one function.

Originaly from jordan


Revision tags: OPENBSD_4_4_BASE
# 1.125 02-Jul-2008 fgsch

add acpivideo. for now almost an empty skel so others can work on
suspend/resume. eventually it will also manage output switching and
brightness where supported. prodded by marco@


# 1.124 11-Jun-2008 marco

Fix $OpenBSD$ for once and for all


# 1.123 08-Jun-2008 deraadt

aaa_dev was being set from an uninitialized variable; ok toby


# 1.122 07-Jun-2008 marco

Kill ACPIVERBOSE. It was never used and is worthless to boot.

comments jmc
ok toby


# 1.121 06-Jun-2008 marco

Shrink acpi some more for floppies and while here re-enable acpi on the
floppies. Fixes crazy sony vaio kettenis has.

tested by deraadt
ok deraadt jordan kettenis


# 1.120 01-Jun-2008 marco

Fix \n nit in SMALL_KERNEL.


# 1.119 01-Jun-2008 marco

Re-enable new parser. Lots of testing by various folks. Fits all media
now too.

ok kettenis deraadt


# 1.118 16-May-2008 dlg

back out jordans new parser.

it didnt have enough oks from the right people, it did break machines
that were previously working, and it bloated the RAMDISKS and install
media beyond the constraints of the media.

requested by deraadt@


# 1.117 14-May-2008 jordan

Adding new ACPI Parser code
Old guts of code still exists, needs to be torn out next
ok marco@
ok brad@


# 1.116 27-Apr-2008 jcs

add acpithinkpad(4), an acpi support driver for ibm/lenovo
thinkpads.

provides functionality that the bios did on older, non-acpi
thinkpads like respond to screen brightness keys and toggle the
bluetooth device on and off.

ok deraadt@ and marco@


# 1.115 24-Apr-2008 jsing

Introduce acpiasus(4), a driver for the ACPI based hotkeys found in many
ASUS laptops (including the ASUS EeePC) - largely based on NetBSD's
asus(4) driver. On the ASUS EeePC this allows us to enable/disable
wireless, change screen brightness and use the volume keys.

ok jsg@, weingart@


# 1.114 12-Apr-2008 kettenis

Get rid of acpi_s5 global variables; simply send SIGUSR2 instead of SUGUSR1
to tell init(8) to power down the machine.

ok krw@


Revision tags: OPENBSD_4_3_BASE
# 1.113 21-Jan-2008 jsg

Check against RESET_REG_SUP in FADT before doing the acpi reset dance
after all, not doing so appears to break more machines than it fixes.

ok marco@


# 1.112 05-Jan-2008 kettenis

Never attach more acpicpu(4) devices than cpu(4) devices. Always attach
the first acpicpu(4) device on non-MULTIPROCESSOR kernels, regardless of its
ACPI processor ID.

ok mikeb@


# 1.111 05-Dec-2007 deraadt

Hang acpi(4) below bios(4) instead of mainbus(4). This lets us move acpi(4)
before pcibios(4), and then.... if acpi(4) attaches, skip pcibios(4) since
it messes the machines up. Fixes claudio's HP dl320 G5, and almost assuredly
others.
ok kettenis


# 1.110 05-Dec-2007 deraadt

repair acpiopen/acpiclose and friends to handle the /dev/apmctl interface,
so that apmd(8) can actually work correctly against acpi(4), and thus,
even do the fancy -C/-A stuff; also tested by jmc


# 1.109 02-Dec-2007 fgsch

fix the trailing space for real now.


# 1.108 02-Dec-2007 fgsch

avoid trailing spaces on output; marco@ ok.


# 1.107 27-Nov-2007 kettenis

Fix off-by-one that made us attach acpicpu0 and acpicpu1 on GENERIC kernels.

ok marco@


# 1.106 25-Nov-2007 jsg

When available and ACPI is enabled use the ACPI reset method
to reboot. This is required for some quirky sis machines
and likely others to reboot.

Tested by many. 'commit early commit often' deraadt@


# 1.105 19-Nov-2007 deraadt

sigh, amd64 still uses that stupid acpi_enabled variable...


# 1.104 19-Nov-2007 deraadt

softc's come initialized to 0, you know...


# 1.103 19-Nov-2007 deraadt

store revision; from jsg


# 1.102 19-Nov-2007 kettenis

Initialize sc_facs earlier.

ok deraadt@, beck@, canacar@


# 1.101 16-Nov-2007 deraadt

fix the bus_space #define nightmare, so that amd64 and i386 are much more
uniform. as a result shared code like acpi needs less #ifdef's
ok marco kettenis


# 1.100 15-Nov-2007 deraadt

remove the #ifdef ACPI_ENABLE. the EC codepath is either safe enough (and
will be improved), or it is unsafe because AML assumes it must be there
ok almost everyone


# 1.99 15-Nov-2007 mikeb

Make acpicpu(4) attach to the existing CPUs only.

Problem reported by form@. Fix idea by kettenis@.
Lots of help from deraadt@. Tested by deraadt@, form@ and me.

ok deraadt marco


# 1.98 14-Nov-2007 deraadt

spacing


# 1.97 12-Nov-2007 deraadt

correct : vs , in attach-time messages


# 1.96 06-Nov-2007 deraadt

all ACPI_ENABLE kernels need acpi_interrupt()


# 1.95 06-Nov-2007 deraadt

move acpi_read_pmreg() and acpi_write_pmreg() out of SMALL_KERNEL so
that acpi ramdisks can compile (obvious solution)


# 1.94 05-Nov-2007 canacar

Establish the acpi interrupt earlier. Fixes problems with some
GENERIC.MP kernels not getting acpi interrupts. This is a
workaround until the real issue with late establishment is fixed.
ok weingart@, beck@, gwk@


# 1.93 03-Nov-2007 jordan

Added support for displaying wakeup devices
ok beck@,weingart@,gwk@


# 1.92 11-Oct-2007 kettenis

When walking the tree yo find _PRT methods, check whether the device is
actually enabled and functioning. Similar to what we do for _INI.

ok weingart@, beck@


# 1.91 08-Oct-2007 krw

More simple memset(,0,) -> M_ZERO changes. In this batch move to
size(*p) as the first malloc() parameter where p is declared locally
and thus easy to check. Add M_ZERO to gpe_table allocation in acpi.c
even though there is no obvious bzero or memset nearby.


# 1.90 13-Sep-2007 mk

Remove unnecessary memset() (see aml_evalnode(9)) and a bit of KNF.

ok toby


# 1.89 13-Sep-2007 weingart

Implement ACPI 6.5.1 spec tree walk for _STA and _INI.
Fixes mk's laptop. No regressions so far (thank you to the
testers).

ok gwk@, mk@, marco@


Revision tags: OPENBSD_4_2_BASE
# 1.88 17-Apr-2007 mk

Now that acpidock no longer hangs machines during boot (at least
according to the reports I've been getting) it should be safe to enable
it again, so remove the #if 0 stuff around it.

ok marco


# 1.87 11-Apr-2007 jordan

Added changes for C-State
ok marco@


# 1.86 26-Mar-2007 marco

Calculate minutes left more accurately.

From giovanni <qgiovanni@gmail.com>


Revision tags: OPENBSD_4_1_BASE
# 1.85 03-Mar-2007 mk

Stop looking for acpidock until we fix that stuff that breaks all pre-60
series ThinkPads with acpidock(4) enabled.


# 1.84 22-Feb-2007 marco

We were getting no interrupts in .MP. There are 2 fixes; one in
acpi_machdep.c and move the interrupt establishment down after ioapic
has been initialized.

ok jordan


# 1.83 21-Feb-2007 kettenis

Make an acpi-enabled GENERIC.MP work on systems that only support PIC mode.

ok marco@


# 1.82 19-Feb-2007 jordan

Added acpi_map_address function for mapping GAS/IO registers
ok marco@


# 1.81 18-Feb-2007 jordan

Added dynamic perfect hash for opcodes
ok marco@


# 1.80 18-Feb-2007 marco

Shave off another K pre compression.
Shuffle SMALL_KERNEL functions around so that we only have a single #ifndef


# 1.79 17-Feb-2007 marco

Add SMALL_KERNEL throught acpi to make it fit on boot media.
ok deraadt


# 1.78 13-Feb-2007 marco

Add smarter _INI code. This helps with some dissapearing devices.

ok kettenis jordan


# 1.77 31-Jan-2007 gwk

Introduce acpi_hasprocfvs thats "ACPI Has Processor Frequency and Voltage
Scalling" a global flag set by acicpu attach to indicate that ACPI is
handling fvs through the _PSS objects. This will fix disappearing
EST/powernow when booting kernels with acpi enabled, as acpicpu isnt
compiled into GENERIC yet. Also in cases where acpicpu is built in and
legacy EST/powernow work but acpi is missing the _PSS object, the legacy
routines will be used.

tested by dim@, and Nick Nauwelaerts <nick AT nauwelaerts.net>
ok marco@, dim@


# 1.76 22-Jan-2007 mk

Actually look for docking stations by looking for _DCK nodes.

ok marco


# 1.75 02-Jan-2007 marco

On some systems EC must be attached as the first device. Move EC from
generic _HID devices to its own routine to force first attach. Bug found
and debugged by joris@

Code mostly written by joris@


# 1.74 26-Dec-2006 marco

Add polling to devices that require it such as acpibat and acpitz.
Use the same tsleep/wakeup mechanism as the interrupt code to keep it non-
concurrent.


# 1.73 21-Dec-2006 deraadt

even more knf love


# 1.72 21-Dec-2006 marco

Remove some leftover acpiec gpe/interrupt code.


# 1.71 20-Dec-2006 deraadt

oops, name already used


# 1.70 20-Dec-2006 deraadt

rename __acpi_enable_gpe(); ok marco


# 1.69 20-Dec-2006 deraadt

acpi_unmap_pmregs() is not used, ok marco


# 1.68 19-Dec-2006 deraadt

simplify some low-level methods substantially (shrinking code); ok marco jordan


# 1.67 06-Dec-2006 mk

malloc(..., M_NOWAIT) with no NULL check is bad. If allocation fails,
complain, clean up, and bail out of attach.

input and ok gwk


# 1.66 28-Nov-2006 jordan

Fixed clearing arg for acpi_init_pic
ok kettenis@


# 1.65 27-Nov-2006 kettenis

Don't spam dmesg.


# 1.64 27-Nov-2006 jordan

Fixes for acpiprt routing
Post-parse fixup for namerefs
ok kettenis@


# 1.63 25-Nov-2006 marco

Don't handle acpiev events inside the gpe handler since this causes deadlocks.

ok jordan@


# 1.62 15-Nov-2006 kettenis

Hook up acpi apic interrupt routing support to generic acpi code.

ok jordan@


# 1.61 02-Nov-2006 marco

Kill acpiec attach function and add its functionality to the generic _HID
device attach

prodded and ok jordan


# 1.60 02-Nov-2006 marco

Fix comments to reflect reality.


# 1.59 19-Oct-2006 marco

Replace ACPI ioctls with APM ioctls. This makes ACPI talk the same protocol
as APM therefore userspace can now retrieve power information in the same
fashion as all other apm arches (zaurus, macppc, i386 etc).

To use force the use of acpi device like so: apmd -f /dev/acpi

ok jordan@


# 1.58 19-Oct-2006 jordan

Fixed GPE debug levels
Fixed ref counter on temporary bufferfields
DSDT parsing fully working on IBM Thinkpad
ok marco@


# 1.57 15-Oct-2006 jordan

Removed old GPE handler code remnants
ok marco@


# 1.56 12-Oct-2006 jordan

Massive ACPI changes
Fixed ACPI leaks
new ACPI API
ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.55 30-Jun-2006 gwk

KNF, No functional change,
"go at it" marco@


# 1.54 30-Jun-2006 jordan

Support for calling Embedded Controller via new GPE interface
ok marco@


# 1.53 30-Jun-2006 jordan

Added new code for GPE handling
Also works with ACPIEC; need to fixup acpiec.c to remove original interrupt handler
ok marco@


# 1.52 30-Jun-2006 jordan

Added offset argument for acpi read/write pmreg routines (new GPE code)
ok marco@


# 1.51 31-May-2006 todd

do not call acpi_powerdown() unless acpi has successfully initialized
ok gwk@ canacar@


# 1.50 29-May-2006 canacar

Add embedded controller support.
ok marco@ deraadt@ testing & ok gwk@


# 1.49 19-May-2006 canacar

Minimal thermal zone driver. Monitors thermal zone temperature,
shuts down the system if the 'critical temperature' is reached.
ok marco@


# 1.48 11-Apr-2006 marco

KNF.


# 1.47 11-Apr-2006 gwk

Add acpi_resume(struct acpi_softc *sc), which calls some AML methods,
detected at attach time, currently unused.

ok marco@


# 1.46 11-Apr-2006 gwk

Discover some ACPI sleep related methods, and call them at the
appropriate time in acpi_enter_sleep_state().

ok marco@


Revision tags: OPENBSD_3_9_BASE
# 1.45 26-Feb-2006 marco

Add acpicpu as a device.

ok jordan@


# 1.44 23-Feb-2006 marco

remove dead function.

ok jordan@


# 1.43 22-Feb-2006 marco

Print out all tables it found during autoconf. We'll need this data in
the future.


# 1.42 22-Feb-2006 marco

Make dmesg less verbose.

ok jordan.


# 1.41 22-Feb-2006 jordan

Added parsing of SSDT tables
ok marco@


# 1.40 22-Feb-2006 jordan

Added new aml_register to notify by device id
Moved powerdown code to button device
ok marco@


# 1.39 21-Feb-2006 marco

Readjust debug levels that crept in too low over the last few days.
KNF


# 1.38 21-Feb-2006 marco

Rig the power button so that the machine shuts down correctly.


# 1.37 20-Feb-2006 marco

Add acpibtn device. Remains disabled for now.


# 1.36 19-Feb-2006 jordan

Added code for aml_notify GPE notification callback
ok marco@


# 1.35 19-Feb-2006 grange

Fix acpi_enter_sleep_state() by replacing current ugly hack
with mostly correct sleeping state transition code.

ok brad@ marco@


# 1.34 19-Feb-2006 marco

jordan:
Enable interrupts.
Added peripheral code to make this work.

marco:
Replace softintr and other types of interrupts with a handler with context.
Fixed interrupt magic for GPEs.
Some KNF

This is work done by jordan and I but has to be a single commit.


# 1.33 17-Feb-2006 marco

Remove earlier introduced delays. We really only need them during autconf.

ok jordan@


# 1.32 17-Feb-2006 marco

Add a generic delay function as per the ACPI spec.

from jordan@


# 1.31 17-Feb-2006 marco

Use tsleep instead of delay when !cold.


# 1.30 17-Feb-2006 marco

Argh, we need delays between reads/writes. The backend reads (i2c most likely)
are so slow that values come back as 0 sometimes. We have to do this
despite the spec saying we shouldnt have to. Guess having a faster
parser than others can hurt sometimes.

Idea from jordan.


# 1.29 17-Feb-2006 jordan

Fixed code for handling _INI
Rearranged debug statements
ok marco@


# 1.28 16-Feb-2006 jordan

Added stub for calling _INI
Fixed aml_dowhile for NULL return
ok marco@


# 1.27 16-Feb-2006 jordan

Fixed parser/evaluator to retrieve battery info+state
Cleanup of parser
Dell laptops return battery status
ok marco@


# 1.26 06-Feb-2006 brad

fix a typo so this will compile on amd64.


# 1.25 04-Feb-2006 marco

When ACPI_ENABLED wasn't set but the devices were it had an unknown variable.

Pointed out by ian@.


# 1.24 03-Feb-2006 jordan

Rewrote aml parser yet again...
Fixed bus_space_map for i386/amd64
Added _BST and _BIF to acpibat to get status/info
ok marco@


# 1.23 20-Jan-2006 jordan

More evaluator changes
Removed old evaluator code
Fixed _HID value
ok marco@


# 1.22 19-Jan-2006 jordan

Added support for read/write of PCI Config space
Help jason@
ok marco@


# 1.21 18-Jan-2006 jordan

Added new evaluation routines
Added helper methods for buffer/field evaluation
Fixed broken indentation on dsdt.c

ok marco@


# 1.20 17-Jan-2006 jordan

Major overhaul of the aml parser. Evaluater temporarily disabled.

ok marco@


# 1.19 06-Jan-2006 grange

Check for ACPI control availability only if ACPI_ENABLE defined.


# 1.18 06-Jan-2006 grange

Informative error messages on attach, prettier dmesg.


# 1.17 06-Jan-2006 grange

printf -> dnprintf conversion for debug messages.


# 1.16 05-Jan-2006 grange

Find SLP_TYPx values and store them for future use in
acpi_enter_sleep_state().


# 1.15 05-Jan-2006 grange

Random KNF and cleanup


# 1.14 05-Jan-2006 grange

Provide symbolic names for the acpi sleeping states and use it.


# 1.13 28-Dec-2005 marco

Moved DEVNAME to a acpidev.h

ok jordan@


# 1.12 18-Dec-2005 sturm

add back comment about wrong code

ok marco


# 1.11 16-Dec-2005 marco

Remove test for kbd_reset. Prompted deraadt@, ok jordan@


# 1.10 16-Dec-2005 jordan

Added hooks for powerdown/power button press


# 1.9 16-Dec-2005 jordan

Cleaned up common power management register i/o
Added enter sleep state ioctl
Hooked power button interrupt
ok marco@


# 1.8 16-Dec-2005 marco

Remove autoconf chatter


# 1.7 16-Dec-2005 jordan

Moved DSDT AML parsing into parent ACPI driver
ok marco@


# 1.6 14-Dec-2005 marco

Add AC device glue.


# 1.5 14-Dec-2005 marco

Adjust debug printing levels to something more sane.


# 1.4 13-Dec-2005 marco

Add glue to attach devices to apci.


# 1.3 07-Dec-2005 marco

KNF


Revision tags: OPENBSD_3_8_BASE
# 1.2 10-Jul-2005 grange

Spacing cleanup.


# 1.1 02-Jun-2005 tholo

Start on a basic ACPI framework -- does not do much more than read out the
ACPI tables into kernel memory and attach ACPI and HPET timers currently.

In order to test this code, enabling the devices in GENERIC as well as
the ACPI_ENABLE option is needed. This code does not do any thermal
control yet, so this should be done with care depending on the platform.

In the tree so more people can contribute to making this more fully
featured.

Ok niklas@ grange@ tedu@


# 1.378 20-Feb-2020 visa

Replace field f_isfd with field f_flags in struct filterops to allow
adding more filter properties without cluttering the struct.

OK mpi@, anton@


# 1.377 31-Dec-2019 visa

Use C99 designated initializers with struct filterops. In addition,
make the structs const so that the data are put in .rodata.

OK mpi@, deraadt@, anton@, bluhm@


# 1.376 31-Dec-2019 mpi

Convert infinite sleeps to {m,t}sleep_nsec(9).

ok kn@


# 1.375 31-Dec-2019 jsg

include sys/sensors.h for sensor use
don't depend on indirect inclusion from acpidev.h


Revision tags: OPENBSD_6_6_BASE
# 1.374 07-Sep-2019 kettenis

Revert acpipci(4) changes. Too many systems are broken. We'll try again
after 6.6 as been released. The acpireg.h change stays behind.


# 1.373 28-Aug-2019 kettenis

Use ACPI information to attach PCI busses like we do on arm64. There are a
few additional quirks though, and attaching the PCI busses is delayed to
replicate the existing code more closely. That may be changed in the
future. Also tweak how we handle MSI support and respect to ACPI flag
that says we shouldn't attempt to use MSIs.

Some fallout is expected.

ok patrick@


# 1.372 27-Aug-2019 deraadt

On amd64 the bootup and resume paths for SP/BP/SP were not performing
msr/pat, clock-startup, firmware upgrade, fpu initialization, and cpu
soft-state initialization in the same order. That kind of chaos is
crazy and fragile. Improve the acpi resume call sequence and unify order
of the operations.


# 1.371 02-Jul-2019 jcs

acpi_set_gpehandler: turn edge arg into flags, add support for
passing GPE_DIRECT which directly executes the supplied callback and
clears a level interrupt rather than going through acpi_taskq which
can be slow when other driver callbacks are in the queue.

This should not be used for GPE handlers that need to do anything
ACPI-related.

ok kettenis


# 1.370 10-Jun-2019 kettenis

Print proper ACPI version number.

ok mlarkin@


# 1.369 08-Jun-2019 kettenis

When establishing the mapping between ACPI device nodes and PCI devices,
skip PCI host bridges and devices that aren't there.

ok patrick@


# 1.368 07-Jun-2019 kettenis

Introduce acpi_getsta() and use it to eliminate some duplicated code.

ok patrick@


# 1.367 12-May-2019 kettenis

Make sure the extended PM register descriptions actually exist before
using them. Fixes machines with ancient ACPI 1.0 (such as recent
hypervisors).

ok deraadt@


# 1.366 12-May-2019 kettenis

Add support for mapping ACPI PM registers using the "extended" gasio
representation from the FADT. Mostly fixes the Lanner NCA-1510.

ok lteo@, mlarkin@, deraadt@


# 1.365 11-May-2019 lteo

Fix incorrect error message.

ok kettenis@


# 1.364 08-May-2019 tedu

free size. the allocation is the header plus the length.


Revision tags: OPENBSD_6_5_BASE
# 1.363 04-Apr-2019 kettenis

Avoid potential null-pointer dereference. Found by Kent R. Spillner.

ok deraadt@, jsg@


# 1.362 20-Jan-2019 jsg

fix SMALL_KERNEL build after changes in rev 1.361
reported by naddy@ ok deraadt@


# 1.361 19-Jan-2019 tedu

Add a pwraction sysctl that controls what the power button does on acpi.
By default, nothing changes -- shutdown is initiated. But allows turning
power button into a sleep button if desired.
(grudging) ok from a few parties


# 1.360 26-Oct-2018 kettenis

Add acpipci(4) on amd64. For now this only calls the PCI-specific _OSC
method to let the ACPI implementation know what features we support.


Revision tags: OPENBSD_6_4_BASE
# 1.359 30-Aug-2018 kettenis

Move softbutton check before the check that skips attaching certain acpi
devices. Fixes regression caused by matching on _CID in addition to
matching on _HID.

ok matthieu@, mlarkin@


# 1.358 25-Aug-2018 kettenis

Match ACPI devices based on _CID if no match for _HID is found.

ok mpi@


# 1.357 19-Aug-2018 kettenis

Add support for multiple PCI segments. Only really implemented for arm64
for now as amd64/i386 firmware still caters for legacy OSes that only
support a single PCI segment.

ok patrick@


# 1.356 03-Aug-2018 kettenis

Let ahci(4) match on _CLS instead of _HID when attaching at acpi(4). Avoids
having to add many more _HID entries to the match table.

ok deraadt@, mlarkin@


# 1.355 10-Jul-2018 kettenis

Make legacy interrupts work in acpipci(4).

ok patrick@


# 1.354 05-Jul-2018 kettenis

Add acpipci(4), a driver that supports generic ECAM-compatible PCI host
bridges based on information provided by ACPI.

ok mlarkin@


# 1.353 04-Jul-2018 kettenis

Properly pass around the PCI "chipset tag" in acpi(4) and refactor
acpimcfg(4) to call an MD initialization functions that sets up a tag for
PCI ECAM.

ok guenther@, mlarkin@, krw@


# 1.352 01-Jul-2018 kettenis

Move acpi(4) attach glue into acpi_machdep.c.

ok guenther@, deraadt@


# 1.351 01-Jul-2018 kettenis

Add support for _DSD Device Properties.

ok mlarkin@


# 1.350 30-Jun-2018 kettenis

Add intr_enable() function, intended for MI use to amd64 and i386 and use
this in the acpi(4) suspend/resume code paths.

ok deraadt@


# 1.349 29-Jun-2018 kettenis

Use <stdint.h> types.

ok mpi@, jung@, krw@, deraadt@


# 1.348 29-Jun-2018 mlarkin

ACPI: Allow (with warning) GPE handler reassignment, instead of returning
a failure code.

ok kettenis


# 1.347 26-Jun-2018 mlarkin

knf


# 1.346 26-Jun-2018 kettenis

Let acpi(4) pass down the bus dma tag.

ok deraadt@


# 1.345 25-Jun-2018 kettenis

Reorganize acpi(4) code a bit in preparation for upcoming arm64 support.

ok deraadt@


# 1.344 20-May-2018 kettenis

Wake up the acpi thread after scheduling a gpio event.

ok mpi@


# 1.343 17-May-2018 kettenis

Sprinkle some #ifndef SMALL_KERNEL such that RAMDISK kernels cuild again.

pointed out by stsp@


# 1.342 17-May-2018 kettenis

Implement GenericSerialBus OpRegion support.

ok mlarkin@


# 1.341 27-Mar-2018 kettenis

Add acpicmos(4), a driver that implements SystemCMOS OperationRegion
access support. This fixes machines where the AML doesn't check whether
support for this OperationRegion type has been registered by the OS.

ok mlarkin@


Revision tags: OPENBSD_6_3_BASE
# 1.340 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.339 10-Feb-2018 deraadt

Syncronize filesystems to disk when suspending. Each mountpoint's vnodes
are pushed to disk. Dangling vnodes (unlinked files still in use) and
vnodes undergoing change by long-running syscalls are identified -- and
such filesystems are marked dirty on-disk while we are suspended (in case
power is lost, a fsck will be required). Filesystems without dangling or
busy vnodes are marked clean, resulting in faster boots following
"battery died" circumstances.
Tested by numerous developers, thanks for the feedback.


# 1.338 08-Feb-2018 deraadt

After flushing disks, try again to release all memory. We don't need to
save dirty memory to the hibernate space.


# 1.337 08-Feb-2018 deraadt

Toss all releaseable memory, because fragmentation can get in the way
of allocating the hibernate playpen.


# 1.336 08-Feb-2018 deraadt

sr_quiesce() is a new approach for ensuring that softraid drains
output to the disks.

This is part of a larger suspend/resume filesystem-safety diff, which
has been worked on for a couple of months already. Tests by job, krw,
beck, benno, and others. Sometimes even by snapshot users...


# 1.335 29-Nov-2017 kettenis

Add support for GPIO-signaled events.

ok mlarkin@


# 1.334 16-Nov-2017 jcs

Add PCI attachment for dwiic(4) needed by Intel 100 Series machines

ok kettenis


# 1.333 14-Nov-2017 kettenis

Add a few more PNP IDs to the skip lists.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.332 17-Aug-2017 stsp

Make dwiic(4) attach its ACPI parent dependencies.
This change makes the Asus E200HA keyboard work.

Original analysis and patch by Cesare Gargano
ok kettenis@


# 1.331 09-Aug-2017 dcoppa

Add "PNP0303" (8042 PS/2 Controller) to acpi_isa_hids[]

ok kettenis@


# 1.330 22-Jul-2017 jcs

acpibat: look for _BIX first before falling back on _BIF

ACPI 4.0 deprecated _BIF for battery status so some newer machines
are exporting _BIX which extends _BIF's package with a few extra
fields. When using _BIX, export the new discharge cycle count as a
sensor.

feedback from kettenis and anton
ok deraadt


# 1.329 20-Jul-2017 kettenis

Start a list of isa(4) device names such that we can avoid printing them
as "not configured".

ok mikeb@, millert@, deraadt@


# 1.328 08-Apr-2017 deraadt

Sensors are run as callbacks inside tasks. During suspend a sensor
could be running inside a driver that will be force-detached, or due
to tsleep end up disrupting the softstate/hardstate contract. At
suspend time, quisce all these callbacks by waiting for completion.
This issue has never been observed for real, but may be implicated in
suspend/resume failures.
ok kettenis guenther mlarkin


# 1.327 08-Apr-2017 deraadt

Two prototypes for acpi_maptable; merge to one place


Revision tags: OPENBSD_6_1_BASE
# 1.326 27-Mar-2017 deraadt

Now that hibernate_alloc() only has clean success/failure, don't
need to call hibernate_free() to clean up a partial mess.
ok mlarkin kettenis


# 1.325 13-Mar-2017 deraadt

allow ramdisks to build; ok jcs


# 1.324 12-Mar-2017 jcs

try to attach acpisbs first and if successful, don't attach acpibat


# 1.323 02-Mar-2017 natano

Add a new sysctl machdep.lidaction. The sysctl works as follows:

machdep.lidaction=0 # do nothing
machdep.lidaction=1 # suspend
machdep.lidaction=2 # hibernate

lidsuspend is just an alias for lidaction, so if you change one, the
other one will have the same value. The plan is to remove
machdep.lidsuspend eventually when people have upgraded their
/ets/sysctl.conf.

discussed with deraadt, who came up with the new MIB name
no objections mlarkin
ok stsp halex jcs


# 1.322 28-Feb-2017 natano

Untangle abstract sleep modes from ACPI sleep states. This paves the way
for further work in the area.

idea and ok deraadt
ok mlarkin


# 1.321 22-Feb-2017 jcs

add acpisbs, an acpi smart battery subsystem driver reading data
over smbus

currently disabled because it conflicts with acpibat

ok deraadt, kettenis


# 1.320 14-Jan-2017 kettenis

Revert aml_rdpciaddr changes; breaks several machines that were working
before.


# 1.319 11-Jan-2017 jsg

Increase the size of the buffers used for _HID and _CID strings to be
able to fit "Hyper_V_Gen_Counter_V1".

ok mlarkin@ deraadt@


# 1.318 08-Jan-2017 kettenis

Make aml_rdpciaddr() work for for devices behind bridges. The code now
uses the mapping between AML nodes and PCI devices that we establish.
Because _INIT methods may end up calling aml_rdpciaddr(), make sure we
create that mapping early. Also handle devices that aren't actually present.
These devices are now included in the mapping and reads will return an
all-ones patterm whereas writes are a no-op..

ok mlarkin@


# 1.317 25-Oct-2016 pirofti

Add the resource index to aml_parse_resource() callback function.

This is needed by an upcoming acpiec commit that handles machines breaking
the current ACPI specifications.

Change suggested by and ok kettenis@, guenther@


# 1.316 18-Sep-2016 guenther

If the APIC table (MADT) isn't present then ci_acpi_proc_id won't be set
when acpicpu tries to attach, leading to a NULL dereference. The ACPI
processor object on the Dells where this was reported is bogus and useless
anyway so apply the same test when deciding whether to attach to processor
objects as acpicpu would when matching them to cpus.

Problem reported by Eivind Eide (xenofil (at) gmail.com) and
Johan Huldtgren (johan+openbsd-bugs (at) huldtgren.com).
Testing by Eivind Eide.
ok kettenis@ mlarkin@


# 1.315 03-Sep-2016 naddy

Write the system time back to the RTC every 30 minutes.
This fixes the problem that long-running machines which were not
shut down properly would reboot with a badly offset system time.

hints and ok kettenis@


# 1.314 31-Aug-2016 mlarkin

Don't put a device in the wake device list if _STA says it's not there.

Fixes at least one machine that woke up from sleep / poweroff immediately.

ok kettenis@, tested by kettenis and martijn@


# 1.313 28-Jul-2016 kettenis

Store the acpi processor ID/UID in struct cpu_info, and use it to attach
acpicpu(4) drivers to the right cpu(4).

ok mlarkin@, guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.312 10-Jun-2016 kettenis

The power management timer has been made optional in ACPI 5.0A, so don't
attempt to attach acpitimer(4) if the timer isn't present.

Pointed out by jcs@

ok jcs@


# 1.311 08-May-2016 kettenis

Make sure devices listed by a device's _DEP method are attached before
we attach the device itself. The _DEP methode, introduced in ACPI 5.0,
is a hint that the device in question depends on OpRegion support from
the devices listed by _DEP, so we have to order them the proper way.
To prevent us from attaching those devices again when we encounter them
later walking down the device tree, keep track of our attempts to
attach a device driver to them using aflag in the aml_node structure.

ok guenther@


# 1.310 25-Apr-2016 pirofti

We attach acpihpet(4) and acpiec(4) without using the HID, so do not
report it in the dmesg.

OK deraadt@, mlarkin@.


# 1.309 02-Apr-2016 kettenis

Reduce dmesg spam by knocking out some acpi hardware IDs that don't need a
driver.

ok mpi@


# 1.308 29-Mar-2016 kettenis

Check if a device is present (using _STA) in acpi_foundhid. This prevents
us from reporting devices as "not configured" that aren't actually present,
and allows us to remove duplicated code from several drivers.

ok deraadt@, ok jsg@


# 1.307 29-Mar-2016 kettenis

Implement a cleaner way to prevent acpivout(4) from attaching if we have
a platform-specific method of controlling the backlight.

ok millert@, mlarkin@, jung@


# 1.306 28-Mar-2016 kettenis

Adding each and every new acpi device driver to acpi_foundhid() is
gettinga bit of the burden. And it really isn't how our autoconf
framework is supposed to work for busses that can be enumerated. All
the drivers already check for a matching _HID in their attach
function. So we can just drop the checks in acpi_foundhid() and let
the drivers sort themselves out.

Print a "not configured" line for devices for which we don't attach adriver.
This may be a bit much as there are devices for which we will not have a
driver. This will be removed if it causes too much dmesg spam.

ok deraadt@, mlarkin@


Revision tags: OPENBSD_5_9_BASE
# 1.305 17-Jan-2016 jsg

Take acpi_foundhid() outside of #ifndef SMALL_KERNEL so the ramdisk
kernel can find and attach dwiic(4) and sdhc(4).


# 1.304 17-Jan-2016 jsg

remove an unused global variable


# 1.303 14-Jan-2016 kettenis

Attach dwiic(4) on the Intel Bay Trail i2c controllers as well.


# 1.302 12-Jan-2016 kettenis

Improve emulation of PM registers on Hardware-redcued ACPI by emulating
SPL_TYPx, SLP_EN and WAK_STS using the new SLEEP_STATUS_REG and
SLEEP_CONTROL_REG registers. Makes the transition into S5 (powerdown) work
on the ASUS X205TA.

ok deraadt@, mlarkin@


# 1.301 12-Jan-2016 jcs

Add dwiic, a driver for the Synopsys DesignWare i2c controller found
on the Samsung ATIV Book 9 laptop. This initial version only
supports ACPI config/attachment.

Add ihidev, a HID-over-i2c driver largely based on uhidev. dwiic
handles attaching ihidev devices found in ACPI.

Add ims, a HID-over-i2c mouse/trackpad driver to get basic cursor
and button functionality from HID-compliant i2c trackpads.

ok kettenis deraadt


# 1.300 11-Jan-2016 kettenis

Make sdhc(4) attach to hardware IDs "80860F14" and "PNP0FFF".


# 1.299 09-Jan-2016 kettenis

Initial support for Hardware-Reduced ACPI. This skips the legacy mode checks
by emulating the PM_CNT register such that SCI_EN is always enabled. It also
skip attaching acpitimer(4) since the relevant registers are no longer there.

ok deraadt@, mlarkin@


# 1.298 06-Jan-2016 kettenis

Follow the spec and check the SCI_EN bit to see if ACPI has already
been enabled and don't bother enabling it again if it is. This should fix
systems that don't implement legacy mode and therefore set ACPI_ENABLE and
ACPI_DISABLE to zero.

ok mlarkin@, yasuoka@


# 1.297 23-Nov-2015 reyk

Back out the CRITICAL change, turn it back from 10% to 25%. Some
people with old/broken batteries didn't like this change, and the
underlying heuristics are wrong.


# 1.296 22-Nov-2015 reyk

The "CRITICAL" APM battery level, that is exported/faked by the ACPI
stack, is currently set to 25%. With 25% battery left, a typical
modern laptop will have at least one hour runtime left. It is an
arbitrary value, and other APM power layers in the kernel set CRITICAL
to something like 5% or 10%, so set it to 10% for now.

OK mlarkin@


# 1.295 28-Sep-2015 deraadt

In low-level suspend routines, set cold=2. In tsleep(), use this to
spit out a ddb trace to console. This should allow us to find suspend
or resume routines which break the rules. It depends on the console
output function being non-sleeping.... but that's another codepath which
should try to be safe when cold is set.
ok kettenis


# 1.294 06-Sep-2015 deraadt

clarify malloc arguments (base + length...)


# 1.293 28-Aug-2015 deraadt

fairly simple sizes for free(); ok tedu


# 1.292 20-Aug-2015 kettenis

Respect the access size when reading or writing to pci config space.
This version also makes sure writes are properly aligned, preventing
the panics seen with the previous incarnation of this change.

Fixes battery status passthrough in vmware, brightness keys on my x220
and who knows what else.

ok mlarkin@, deraadt@


Revision tags: OPENBSD_5_8_BASE
# 1.291 04-Aug-2015 deraadt

If we are setting ACPI_PM1_BM_RLD do it only once, and remember to
repeat this action at resume time.
ok mlarkin guenther


# 1.290 29-Jul-2015 deraadt

Something subtle in previous revision is locking up suspend or booting
on some laptops. Mailing lists threads mention x220 and Asus machines.
Reverting for now.


# 1.289 21-Jul-2015 kettenis

iRespect the access size when reading or writing to pci config space.
Fixes battery status passthrough in vmware and who knows what else.

Tested by many.

ok mlarkin@, deraadt@


# 1.288 17-Jul-2015 mlarkin

fix comments


# 1.287 26-Mar-2015 jsg

Use the 0x10 extended capability to determine if the trackstick
buttons are wired to the trackpad and need to be re-routed to the
trackstick. Without this change the buttons on 2015 thinkpads
get picked up as extended buttons that show up as scroll up/down.

Remove the X1 Carbon 2015 (LEN0048) and X250 (LEN0046) from the
top button area/soft buttons quirks list. Also avoid using the
quirk list entirely if the capability bit is set.

Based on work from jcs@ with feedback from mpi@
ok mpi@ jcs@


# 1.286 20-Mar-2015 kettenis

The ACPI standard says that a PCI function number of FFFF can be used to refer
to all functions on a device. This makes no sense in the context of accessing
PCI config space. Nevertheless stsp@ has a machine with AML that tries to
do config space reads from a device where _ADR encodes function number FFFF.
This is almost certainly buggy AML, but since Linux (and presumably Windows)
boot just fine on this machine we need to work around this. Linux effectively
treats function FFFF as function 7, which isn't present on this machine.
Instead we simulate a read from a non-existent device by returning a bit
pattern of all ones. Writes to function FFFF will still panic.

ok mlarkin@


# 1.285 16-Mar-2015 deraadt

Reduce use of SMALL_KERNEL in acpi, bringing acpiec driver and more gpe
handling into RAMDISK. This is now possible because the install media
has ample room. The goal is to reduce special cases where we may be
skipping (unknown) important operations...
ok mlarkin kettenis


# 1.284 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.283 07-Feb-2015 deraadt

New framework that allows hibernate to pass in entropy from it's fresh
boot.
ok mlarkin


# 1.282 06-Feb-2015 mlarkin

Fix a hibernate crash on some machines due to unmapping a page that
may not have been mapped previously (in the failure to hibernate case).

Also ensure that the lowmem ptp is mapped in all cases (not just MP).

ok kettenis


# 1.281 17-Jan-2015 deraadt

resume_randomness() before spinning up other cpus...


# 1.280 15-Jan-2015 jsg

Define a new wscons mouse type for Synaptics clickpad devices that lack
physical buttons. This will be used if the acpi pnpid for the mouse
matches a list provided by Synaptics found in Linux.

Combined with the xenocara changes this will remove the need for an xorg
config file for the x240/t440/t540 generation of thinkpads.

ok matthieu@ shadchin@ kettenis@


# 1.279 11-Jan-2015 kettenis

Tolerate checksum errors in ACPI tables (but complain about them). Apparently
there are machines out there with broken BIOSen with such checksum errors in
thier RSDT tables. Windows runs fine on these machines, so it probably does
not check. Linux ignores checksum errors as well (and prints a warning).

ok deraadt@, krw@


# 1.278 18-Dec-2014 deraadt

suspend and resume the RNG (at what looks like the right spots)
ok reyk djm


# 1.277 09-Dec-2014 doug

Replace some malloc(n*size,...) calls with mallocarray().

ok tedu@ deraadt@


# 1.276 06-Dec-2014 mpi

If "machdep.lidsuspend" is not set, do not go back to sleep if the lid
is still closed when resuming. This allows docked laptop to be resumed
without opening them.

ok mlarkin@


# 1.275 23-Nov-2014 mlarkin

If a machine has no _LIDs, then none can be open. This diff fixes a problem
where machines without any _LIDs were going back to sleep after hibernate
due to the recent suspend-if-lid-closed-on-resume diff. Noticed on qemu,
which doesn't have any _LIDs.

discussed with deraadt


# 1.274 18-Nov-2014 krw

Nuke yet more obvious #include duplications.

ok miod@


# 1.273 08-Nov-2014 mlarkin

If resuming from sleep (zzz/ZZZ) and the lid is still closed, go back to
sleep. This prevents accidental lid flex or slight opening in a backpack
from waking the machine up and leaving it resumed (powered on).

ok deraadt@


# 1.272 26-Sep-2014 kettenis

Rework piglet and pig allocation. Currently the piglet gets allocated
deep down in the suspend path, where it is really hard to recover from
allocation failure. So allocate the piglet early on in the suspend path.
Also change the piglet and piglet allocation functions to use km_alloc(9)
instead of doing pmemrange magic. This removes a bunch of code which, in the
case of the piglet allocation, is broken since it results in a NULL pointer
dereference. Also switch the piglet allocation to not wait. If we can't
allocate 16MB of phys contig memory on a halfway modern machine we're almost
certainly under a lot of memory pressure and we're better off not trying to
hibernate anyway.

ok mlarkin@


# 1.271 25-Sep-2014 kettenis

Bring back rev 1.249. Now that mp_setperf() has been fixed, it should be
safe to restore perflevel when resuming. Should fix the issues people
see with the processor running at full speed heating up the machine while
the apm command reports that the CPUs are running at a low speed.

ok dcoppa@, deraadt@


# 1.270 23-Sep-2014 kettenis

Release the acpi lock around calls to wsdisplay_suspend() and
wsdisplay_resume(). The X server may adjust the display brightness just
before/after the VT switch when suspending/resuming. In fact, it may
already be blocked adjusting the display brightness for some reason
unrelated to the suspend request.

The VT switch request will eventually time out, but the delay is annoying
and the failed VT switch will lead to problems after resume.

Needed for a future update of the xf86-video-intel driver.

ok mlarkin@


# 1.269 19-Sep-2014 kettenis

Use config_suspend_all(9).


# 1.268 08-Sep-2014 kettenis

Clear the PME Status bit on PCI devices in response to Device Wake events.
Fixes the interrupt storm on the 2009 Mac mini with WOL enabled on its
nfe(4) interface.

ok mlarkin@


Revision tags: OPENBSD_5_6_BASE
# 1.267 20-Jul-2014 mlarkin

Support hibernating to softraid crypto volumes.

much help and ok from deraadt@


# 1.266 16-Jul-2014 mlarkin

Reenable hibernate RLE support and flush+zero all memory after unpack.


# 1.265 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.264 12-Jul-2014 kettenis

Add a function to drop all clean pages on the page daemon queues and call
it when we hibernate.

ok mlarkin@, miod@, deraadt@


# 1.263 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.262 11-Jul-2014 mlarkin

Flush the buffercache to 16MB on hibernate and restore its previous max
size (kern.bufcachepercent) on resume, for better hibernate performance.

ok beck@


# 1.261 10-Jul-2014 blambert

remove unneeded header

ok mlarkin@


# 1.260 01-Jul-2014 kettenis

Adding an additonal delay after each access to SystemMemory or SystemIO
during autoconf makes no sense.

ok deraadt@


# 1.259 23-Jun-2014 kettenis

SystemMemory is used to access memory mapped registers on some machines, so we
must use the bus_space(9) API to access it instead of memcpy(9). Also make
sure we properly align access to fields that need more than byte alignment.

ok guenther@


# 1.258 21-May-2014 mlarkin

Fix invalid / missing format specifiers in several dnprintf statements
(used only when ACPI_DEBUG is enabled)

From a diff on tech@ by Fabian Raetz, with a minor change by me, tested
on i386 and amd64 with ACPI_DEBUG enabled and disabled.


# 1.257 25-Apr-2014 mlarkin

Defer the free of the hibernate structures until later, when it is safe to
do so.

Freeing the hibernate structures too early leads to inadvertent calls into
splx when we aren't ready for them yet. Found when trapping splx calls in
the resume path.

discussed with deraadt@ at m2k14


# 1.256 13-Apr-2014 mlarkin

Fix a badly worded debug message that implied the entire machine was going
to sleep, instead of just the acpi thread going to sleep.


# 1.255 31-Mar-2014 mpi

Change the order of operations during a suspend/resume cycle and
call bufq_quiesce() after executing the DVACT_QUIESCE handlers.

This should be safe since no disk nor controller drivers have such
handler but it will allow us to detach sd(4) devices attached to a
USB bus.

Another benefit pointed out by kettenis@ is that drivers that need
to read a firmware from the disk should be able to do it at resume
time in a DVACT_WAKEUP handler.

ok kettenis@, deraadt@


# 1.254 13-Mar-2014 dlg

get rid of the assumption that the head of the alldevs list is the
"mainbus" device. this breaks when mpath is enabled because it
attaches before mainbus and therefore takes the head position.

have autoconf provide device_mainbus() which looks up mainbus_cd,
and use that instead.

discussed with deraadt who just wants mpath stuff to move forward
despite there being many ways to shine this particular turd.


Revision tags: OPENBSD_5_5_BASE
# 1.253 21-Feb-2014 deraadt

Do the lid control further on in the resume sequence, since this is
AML, and will do WAITOK operations. While cold, that is a bad idea.
We seem to be safe with respect to the spec, I do not see any event
debounce issues here.
ok kettenis, tested by many


# 1.252 20-Jan-2014 claudio

Instead of checking sc->sc_revision <= 1 check that the
fadt->hdr_revision <= 1. This is a better check since it checks
the version of the proper descriptor. This allows the alix APU
to reboot since those boards have currently a wrong sc->sc_revision.
Committing this now so that it can be tested easier.
With and OK jsg@ also fine by derradt@


# 1.251 24-Dec-2013 kettenis

If the FADT has its SMI_CMD set to zero, assume we're only ACPI-only hardware
and don't need to disable SMI ownership of the ACPI hardware registers.

ok mlarkin@


# 1.250 23-Dec-2013 kettenis

Revert previous commit. It makes resume hang on some MP systems running
GENERIC.MP.


# 1.249 16-Dec-2013 kettenis

Restore hw.setperf upon resume like we do for apm(4) on i386. Seems we need
to do this fairly late in the resume to avoid hangs.

ok deraadt@, mlarkin@


# 1.248 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


# 1.247 06-Nov-2013 mpi

Add support for Power Resources for Dx states and the necessary hook
for PCI devices. This hook should be called twice, before and after
changing the power state of a PCI device.

Before setting the device to the new state, the ACPI layer will notify
every power resources linked to the device for that state and make sure
they are turned "_ON". After changing the state of the device, it will
decrement the reference of every power resources linked to that device
for the old state and turn them "_OFF" if they are no longer referenced.

This fixes the no-USB after resume problem seen on various ThinkPad,
problem initialy diagnosed with Alexander Polakov.

ok kettenis@, deraadt@


Revision tags: OPENBSD_5_4_BASE
# 1.246 01-Jun-2013 mlarkin

Stop printing ACPI wakeup devices in dmesg after the 16th wakeup device.

Helps with VMware VMs that report hundreds of wakeup devices.

ok reyk@, deraadt@


# 1.245 31-May-2013 bluhm

Fix the build for a kernel without wd and pciide in its config.
OK deraadt@ tedu@


# 1.244 30-May-2013 deraadt

Enforce ca_activate tree-walks over the entire heirarchy for all events,
cleaning up some shutdown-hook related code on the way.
(A few drivers related to sparc are still skipped at kettenis' request)
ok kettenis mlarkin, tested by many others too


# 1.243 18-Apr-2013 deraadt

pin the acpi thread to the BSP, because AML and SMI cannot be trusted. I
was certain we were doing this already, but it turns out no...


Revision tags: OPENBSD_5_3_BASE
# 1.242 24-Dec-2012 guenther

In acpiioctl(), Return though the splx() path if suser() returns an error

ok deraadt@


# 1.241 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


# 1.240 04-Oct-2012 ehrhardt

Use information provided by ACPI to attach secondary PCI host bridges.

ok kettenis@


# 1.239 07-Sep-2012 kettenis

Add a new function called acpi_pci_min_powerstate() that returns the lowest
power state for a PCI device for the power state (currently S3 or S4) we're
transitioning into. This function is probably too simplistic; there is a lot
of (silly) complexity in the ACPI spec.

ok deraadt@


Revision tags: OPENBSD_5_2_BASE
# 1.238 13-Jul-2012 pirofti

Disable acpivideo(4) on ASUS laptops.

There are some models that panic whenever an event is received.
Some cope, some don't. In order to get this right acpiec(4) requires
major rework. Until then let machines boot.

Discussed with deraadt@.


# 1.237 11-Jul-2012 mlarkin

for now, restrict hibernate to root in the acpi ioctl kernel interface.
normal rules for controlling this interface through apmd still apply.

ok deraadt@


# 1.236 09-Jul-2012 deraadt

tiny little oops in untested hibernate block


# 1.235 09-Jul-2012 deraadt

clean up ioctl() case statements


# 1.234 09-Jul-2012 deraadt

acpi_disable_onegpe() is not used; ok pirofti


# 1.233 24-May-2012 kettenis

If _MIN > _MAX consider a WordBus resource to be invalid. Fixes a panic on
the IBM x3550-M4.

ok mikeb@


# 1.232 29-Mar-2012 mlarkin

Missing #ifdef in previous diff


# 1.231 29-Mar-2012 mlarkin

Prevent hibernation attempts on machines with unsupported disk controllers.
The same check is also performed later in the hibernate sequence, but by
then it's too late to cleanly unwind (presently).

ok deraadt


# 1.230 26-Mar-2012 deraadt

simplify conditionals in acpi_sleep_state()
ok mlarkin


# 1.229 26-Mar-2012 deraadt

hook in the hibernate request code; half of this diff is from mlarkin
ok mlarkin


Revision tags: OPENBSD_5_1_BASE
# 1.228 20-Sep-2011 deraadt

Very early in suspend while the clock is still ticking realtime,
save the clock back to the rtc. The zaurus already did this.
ok phessler miod kettenis


Revision tags: OPENBSD_5_0_BASE
# 1.227 02-Jul-2011 nicm

kqueue attach functions should return an errno or 0, not a plain 1. Fix
the obvious cases to return EINVAL and ENXIO.

ok tedu deraadt


# 1.226 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.225 16-Jun-2011 pirofti

Import acpitoshiba driver.

This is originally written by Hiroyuki Aizu, ported and integrated
in our tree by Javier Vazquez <javama4 AT gmail> with tweaks and
suggestions by me.

Import prodded by deraadt@ and the lack of testers on tech@.


# 1.224 27-Apr-2011 jcs

attach acpithinkpad to newer lenovo models like the x120e

ok deraadt@


# 1.223 22-Apr-2011 jordan

Fix uninitialzied variables and formatting strings (-Wxxx errors)


Revision tags: OPENBSD_4_9_BASE
# 1.222 02-Jan-2011 jordan

Re-add code for ACPI taskqueue, eliminates need for state vars in acpi softc
Serialize processing ACPI tasks for sleep/powerdown/GPE event
ok deraadt@


# 1.221 31-Oct-2010 guenther

Revert last commit: it breaks resume on Thinkpads
"then please back it out" deraadt@


# 1.220 26-Oct-2010 jordan

Add task queue for ACPI gpe and notify handlers
This fixes eject on Dell Latitude dock and an issue on the Dell Mini
battery update.
ok deraadt


# 1.219 07-Oct-2010 jordan

Create separate functions for enabling wake and runtime gpes.
Remove old suspend/resume gpewalk function
ok deraadt, mlarkin


# 1.218 05-Oct-2010 jordan

split acpi_enable_onegpe to separate enable/disable functions
ok deraadt


# 1.217 05-Oct-2010 jordan

Added acpi_disable_allgpes function to clear GPE status
ok deraadt


# 1.216 05-Oct-2010 deraadt

clean up acpi_softc use more


# 1.215 05-Oct-2010 deraadt

delete a bit of #if 0 code


# 1.214 05-Oct-2010 deraadt

hide use of our global acpi_softc pointer a bit better


# 1.213 29-Sep-2010 deraadt

grab the acpi thread lock very early in acpi thread startup; not that it
is likely that anyone will race us... but why be careless.
ok kettenis


# 1.212 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.211 29-Aug-2010 jasper

- add a check for FWRITE here too.

ok miod@


# 1.210 27-Aug-2010 kettenis

Make PCI Power Management optional, and only enable it when acpi(4) attaches.

ok deraadt@


Revision tags: OPENBSD_4_8_BASE
# 1.209 08-Aug-2010 kettenis

Close race between the acpi thread and wscons ioctls running in process
context. We might rip this out post-release and replace it with a solution
that serializes things in a more obvious way.

ok deraadt@, miod@ and "those crazy texans who ok anything"


# 1.208 08-Aug-2010 deraadt

Report the acpi sleep states when on a RAMDISK; ok krw


# 1.207 07-Aug-2010 kettenis

Enforce acpi register access rescrictions.

ok mlarkin@, deraadt@, tested by marco@


# 1.206 07-Aug-2010 deraadt

upon resume, notify all the acpiac and acpibat drivers to update
their status. some laptops do not do this automatically.
ok kettenis canacar


# 1.205 06-Aug-2010 marco

Always call _PSW on _LID devices that support it to enable lid open to
resume the machine. Conversely disable it when the machine wakes up.

Tested by several ok deraadt


# 1.204 06-Aug-2010 deraadt

As part of a revamp of the PM1/GPE code, I write this diff to look for
unmanaged EN & STS bits in the PM1 register at interrupt time and report
them. As a side effect this splits the STS acknowledgement into two writes
(for power, and sleep) instead of one. The printf that is added (to spot
unmanaged STS bits) has not yet been triggered as far as we know.

Before the "write to PM1 registers at the right offset" diff went in, this
was not neccessary. But newer thinkpads do not have a working soft power
button without this diff.

We have no idea why.
ok mlarkin kettenis


# 1.203 05-Aug-2010 deraadt

PM1 power button events were generating 'fake events' to acpibtn PWRB,
but if there is no such device these were being tossed. Instead, arrange
for all power button handlers to flag a softc variable which is processed
by the thread; so that one place will know to request a powerdown (psignal
to init)
ok marco mlarkin


# 1.202 05-Aug-2010 deraadt

rename a variable which is annoyingly poorly named


# 1.201 05-Aug-2010 mlarkin

Write to the correct offset for various ACPI pmreg registers. Writing to
the correct register does properly clear the wake status bits and fixed
event bits, which allows some machines to have more than 1 suspend cycle.

bug spotted by deraadt@

tested by deraadt@ and myself on a half-dozen machines and others on
hackers@

ok deraadt@, marco@


# 1.200 05-Aug-2010 deraadt

Fix the power button. The pm1 register is 16 bits in size.
My mistake, a while back.
tested by krw


# 1.199 04-Aug-2010 kettenis

Print supported sleep states.

ok marco@, phessler@, deraadt@


# 1.198 03-Aug-2010 mlarkin

Use the proper flag for re-enabling certain hardware events (power btn,
sleep btn). The (incorrect) flag prevoiously used coincidentally had the
same value, so this shouldn't cause any different behavior than before.

tested on a variety of machines (i386, amd64, sp, mp) - no behavioral
change seen

ok deraadt@


# 1.197 28-Jul-2010 marco

Make all gasio failures look and behave the same.


# 1.196 28-Jul-2010 mlarkin

Delay a little longer on suspend before giving up. Fixes machines that
sometimes don't suspend because the sleep transition takes too long
(symptoms like failing to sleep but the backlight stays on).

ok deraadt@


# 1.195 27-Jul-2010 deraadt

remove XXX; EC can now be attached early when needed; ok jordan


# 1.194 27-Jul-2010 deraadt

more brackets feels good


# 1.193 27-Jul-2010 deraadt

XXX annotate a block in acpi_enter_sleep_state() that is pretty worrying


# 1.192 27-Jul-2010 deraadt

horrific indentation that nearly made me puke


# 1.191 27-Jul-2010 mlarkin

Set the sleeping indicator light on machines that support it, in case SMI
doesn't do it for us. Shows the 'moon' LED on older Thinkpads when
sleeping.

Tested on T43p, W500, T510 by me and on X61s by deraadt@.

ok deraadt@


# 1.190 27-Jul-2010 jordan

Early initialization of acpiec if ECDT table exists
Fixes hang when booting thinkpads while docked
ok deraadt


# 1.189 26-Jul-2010 deraadt

remove merge error which was commited


# 1.188 26-Jul-2010 pirofti

Add support for Sony ACPI hotkeys via a new driver: acpisony(4).

Currently it only works for the suspend button and tries to do right for
the brightness events, but I haven't found a Sony laptop that like to do
right so far.

In the future I want to make the brightness keys work on all Sony's and
also add support for the zoom hotkeys and whatever other funky keys I can
find on those things.

Okay deraadt@.


# 1.187 22-Jul-2010 deraadt

pretty large cleanup of SMALL_KERNEL


# 1.186 21-Jul-2010 deraadt

in the acpi GPE handler, late ack level interrupts not edge interrupts.
Duh.


# 1.185 21-Jul-2010 deraadt

Merge the edge and level gpe handlers into one (now possible since the
gpe structure tells us whether the gpe is edge or level)


# 1.184 21-Jul-2010 deraadt

delete #if 0 code we are not going to use; ok marco


# 1.183 20-Jul-2010 deraadt

make the mask code for acpi_enable_onegpe() match other similar code


# 1.182 20-Jul-2010 deraadt

Handle the acpi interrupt controller with proper edge/level handling,
wrapped in spltty to avoid racing against the interrupt controller.
Repair the gpe bit masking code while there, and do operations in the
right order
ok kettenis mlarkin, help from jordan at figuring out the order of
operations.


# 1.181 20-Jul-2010 matthew

When we're preparing to go to sleep, only call bufq_restart() if other
preparations failed.

ok kettenis@, thib@, deraadt@


# 1.180 19-Jul-2010 deraadt

permit compilation with ACPI_DEBUG


# 1.179 19-Jul-2010 deraadt

there is no need for a seperate dsdt_softc pointer; dsdt does not have a
softc but is just a part of the single acpi driver.
ok jordan


# 1.178 19-Jul-2010 deraadt

acpi_delay() is no longer needed; ok jordan


# 1.177 18-Jul-2010 mlarkin

Clear event status on resume, to avoid going back to sleep/reboot/shutdown
immediately after resume. Tested by a bunch of people on hackers@ on
various hardware. Fixes at least Toshiba NB200 and Gateway LT3103u.

ok marco


# 1.176 13-Jul-2010 deraadt

rename some thread related interfaces so they are logical


# 1.175 10-Jul-2010 jordan

Verify _ADR bdf is correct (some systems return 0xffff)
Works on Dell x9xx and Studio 1555


# 1.174 09-Jul-2010 jordan

Clear the noise


# 1.173 08-Jul-2010 jordan

Add mapping for ACPI device to PCI bdf (match autoconf tree)
Simplify resource parsing function to use buffer argument
Convert namespace linked lists to use queue macros
ok marco@, deraadt@


# 1.172 06-Jul-2010 deraadt

Use spltty() to lock downcalls from apm against the information being
modified by the acpi thread.
ok kettenis


# 1.171 06-Jul-2010 deraadt

Don't use a workq to do the suspend, because it races aginst the acpi
thread. Instead, just tell the acpi thread to do the suspend for us.
This makes apmd & zzz work correctly. While here, have acpithinkpad
attempt to post the event to apm before prompting the suspend itself.
ok kettenis marco mlarkin


# 1.170 05-Jul-2010 mlarkin

Reset register SCI_EN on resume. The ACPI spec clearly states we don't have
to do this, but many machines don't, or forget to, which leads to all sorts
of problems on resume (broken battery stats, overheating, failure to resume
more than once, etc).

tested by many, ok marco@, deraadt@


# 1.169 01-Jul-2010 thib

do bufq quiesce again, since vnd/the bufq_done call in
biodone has been fixed.


# 1.168 01-Jul-2010 jordan

Backout recent AML changes, breaks on R210 and others


# 1.167 01-Jul-2010 jordan

Simplified aml_parse_resource call, cleanup code
ok marco


# 1.166 01-Jul-2010 jordan

Fix subordinate bus # for multi-root PCI buses
ok marco, kettenis


# 1.165 30-Jun-2010 thib

Disable/partially backout the bufq quiesce changes as this
is causing havoc with vnds and release must be buildable.


# 1.164 29-Jun-2010 deraadt

Fix ramdisks (new code was placed inside a SMALL_KERNEL chunk)


# 1.163 29-Jun-2010 jordan

Add support for mapping ACPI to PCI devices
ok kettenis, deraadt


# 1.162 29-Jun-2010 kettenis

Call bufq_quiesce() on suspend to stop all I/O and bufq_restart on resume.
Makes suspend/resume work much better while doing disk I/O.

ok mlarkin@


# 1.161 29-Jun-2010 tedu

60 is a more reasonable guess for number of minutes in an hour than 100.
spotted by TeXitoi. ok deraadt


# 1.160 27-Jun-2010 jordan

Add _acpi_matchhids routine, precursor to pci device match


# 1.159 27-Jun-2010 mlarkin

Clear ACPI fixed event status on resume (power buttons/etc) so that some
machines don't immediately resume after suspending to S3.

Tested by kettenis@ and I on 5 or 6 machines, deraadt@ "yeah, looks good"


# 1.158 27-Jun-2010 jordan

Cleaned up acpi_maptable, adds to table list directly


# 1.157 07-Apr-2010 deraadt

If we have done a wsdisplay_suspend(), and ACPI decides it does not want to
suspend, we must roll back by calling wsdisplay_resume() or the virtual
console switching will be locked forever
ok oga


# 1.156 07-Apr-2010 kettenis

Make suspend/resume work on MP machines (running an MP kernel). Joint work
from mlarkin@ and me, with some amd64 fixes thrown in by deraadt@

ok marco@, deraadt@, pirofti@, mlarkin@


# 1.155 31-Mar-2010 kettenis

Bring /dev/apm support in line with apm(4). Make the suspend button send
a suspend request event to apmd(8) instead of suspending immediately. Also
keep track of whether /dev/apm and /dev/apmctl are currently open, such that
we can still suspend immediately if apmd(8) isn't running.

ok deraadt@, marco@, pirofti@, jsing@, oga@


# 1.154 30-Mar-2010 oga

Prevent the apmd/x races for good.

When we hit suspend time, go through all wsdisplays on the system. if
they are in mode MAPPED, but not MODE_DUMBFB then if possible do a full
vt switch to a !mapped vt, and prevent switching back until resume time.
This has to be called from MD code because this involves userland
running so that X can run the vt switch signal handler. This way, any
case where we are using the "poke registers from userland" model, we
will not be on the hardware when we go down, so the kernel can actually
handle thing properly.

Tested on several acpi laptops (by kettenis@ and ian@), x40 (me and
beck@ at LEAST) and zaurus (me). Maybe others, but if so I forgot who at
this time..

Idea from deraadt somewhere over the Faroe Islands (I thought of a
similar thing myself a while ago). Much prompting from him. Ok and
comments miod@


# 1.153 25-Mar-2010 oga

Fix & vs. && in the kqfilter code.

Based on a comparison with the apm code.

ok deraadt@, kettenis@


Revision tags: OPENBSD_4_7_BASE
# 1.152 26-Nov-2009 mlarkin

Visual (and on some machines, audio) feedback while the machine is
resuming via calls to aml _SST method. This also makes the "moon" LED on
thinkpads to blink during resume and cease when resume is completed.

ok pirofti@


# 1.151 26-Nov-2009 deraadt

Toggle into "cold" when we we disable interrupts, because tsleeping
in the AML interpreter and context switching into userland processes
tends to sometimes pop the ACPI trampoline page out of our address
space. Makes lots more machines work.
ok kettenis mlarkin


# 1.150 24-Nov-2009 jsg

in the resume path evaluate _WAK before dealing with device drivers
like the spec says, not after.

ok mlarkin@


# 1.149 24-Nov-2009 deraadt

abort suspends on SMP systems until that gets written; ok mlarkin


# 1.148 23-Nov-2009 mlarkin

pci should return the result of it's children's suspend/resume calls back
to its parent.

handle suspend failure case and unwind if devices have an activate function
that returns failure.

ok deraadt@, kettenis@


# 1.147 23-Nov-2009 mlarkin

acpi* devices need to call their children on suspend/resume.
ok deraadt@


# 1.146 23-Nov-2009 pirofti

Remove ACPI_SLEEP_ENABLED checks.

This enables by default the suspend/resume paths in the kernel.

Okay deraadt@.


# 1.145 23-Nov-2009 deraadt

knf and spacing that are super annoying


# 1.144 23-Nov-2009 mlarkin

Hook up Theo's new children suspend/resume function, as well as more ACPI
suspend/resume glue.

ok deraadt@


# 1.143 26-Oct-2009 deraadt

Do not do strncmp comparisons on dv_xname because longer device names which
look similar could arrive in the future. Instead, compare directly against
dv_cfdata->cf_driver->cd_name
Issue originally spotted by miod


# 1.142 26-Oct-2009 jordan

Verify checksum+address when loading ACPI tables.
Some systems had invalid entries in RSDT/XSDT.
ok marco@


# 1.141 23-Jul-2009 cnst

New aibs(4) driver for ASUSTeK AI Booster (ACPI ATK0110) hardware monitoring,
with sensor state support through limits provided by the ACPI.

Tested on several ASUS motherboards kindly networked by Sam Fourman Jr.

ok deraadt marco jordan


Revision tags: OPENBSD_4_6_BASE
# 1.140 03-Jun-2009 pirofti

Import acpi power resource driver. This is needed for suspend/resume logic.

Okay jordan@ and marco@.


# 1.139 03-Jun-2009 pirofti

Add acpivideo support. This adds brightness support for all laptops
except thinkpads, they will use the acpithinkpad driver. The driver is
also hooked into wsconsole. So brightness can be adjusted via:

$ wsconsctl display.brightness=<percentage>

This is very helpfull on some laptops that have a nasty bios and get two
steps instead of one when pressing the brightness button.

Tested on various dell, fujitsu, acer, samsung and other laptops.

Okay marco@, miod@. Suggestions from kettenis@.
Lots of reviews and help from miod@, thanks!


# 1.138 03-Jun-2009 jordan

Added ejectable bay for IDE cdroms
ok marco@


# 1.137 30-Apr-2009 marco

Oga changed apm ioctls and forgot to grep. Temporary work around by hitting
the sleep button when we get any request. This interface will chnage at
some point.
ok oga


# 1.136 19-Apr-2009 krw

Call acpi_sleep_walk() when powering down just like when entering
any other sleep state. This tells acpi wake devices to not wake up
the box until manual powering up resets their status. Makes my Dell
GX520 stay powered off in response to 'halt -p'.

In snaps for a while.

ok marco@ (several times) deraadt@


# 1.135 17-Apr-2009 pirofti

Add device ranking support for future sleep/wake developments.

okay marco@


# 1.134 11-Mar-2009 jordan

Cleanup.. use aml_evalinteger instead of aml_evalname
ok marco@


# 1.133 10-Mar-2009 jordan

Fix cut/paste error.. call _BFS not _PTS
ok marco@


Revision tags: OPENBSD_4_5_BASE
# 1.132 21-Feb-2009 marco

overzealous cut/paste; found by Vladimir Kirillov <proger@uaoug.org.ua>


# 1.131 20-Feb-2009 marco

Oops didn't mean for Debugger() to remain there.


# 1.130 19-Feb-2009 marco

suspend/resume bits so that we can develop this in tree. This is disabled.
code from mlarkin and me
help from art,toby,jordan and several others
ok jordan, go for it deraadt


# 1.129 10-Feb-2009 jordan

Fix null pointer for shutdown if acpi not enabled
ok marco@


# 1.128 04-Feb-2009 kettenis

Some systems that claim to be ACPI 2.0 apparently don't provide an XSDT
address. Make the code fall back on the RSDT on those systems.

From Joerg Niendorf <p3y80@internode.on.net>. Fixes PR 5986.


# 1.127 17-Jan-2009 krw

A couple of stray malloc/memset -> malloc(M_ZERO), a bit of knf and
lose a couple of unnecessary casts. No functional change.

Feedback from kettenis@

ok kettenis@ marco@


# 1.126 06-Nov-2008 marco

Collapse all sanity checks of hids into one function.

Originaly from jordan


Revision tags: OPENBSD_4_4_BASE
# 1.125 02-Jul-2008 fgsch

add acpivideo. for now almost an empty skel so others can work on
suspend/resume. eventually it will also manage output switching and
brightness where supported. prodded by marco@


# 1.124 11-Jun-2008 marco

Fix $OpenBSD$ for once and for all


# 1.123 08-Jun-2008 deraadt

aaa_dev was being set from an uninitialized variable; ok toby


# 1.122 07-Jun-2008 marco

Kill ACPIVERBOSE. It was never used and is worthless to boot.

comments jmc
ok toby


# 1.121 06-Jun-2008 marco

Shrink acpi some more for floppies and while here re-enable acpi on the
floppies. Fixes crazy sony vaio kettenis has.

tested by deraadt
ok deraadt jordan kettenis


# 1.120 01-Jun-2008 marco

Fix \n nit in SMALL_KERNEL.


# 1.119 01-Jun-2008 marco

Re-enable new parser. Lots of testing by various folks. Fits all media
now too.

ok kettenis deraadt


# 1.118 16-May-2008 dlg

back out jordans new parser.

it didnt have enough oks from the right people, it did break machines
that were previously working, and it bloated the RAMDISKS and install
media beyond the constraints of the media.

requested by deraadt@


# 1.117 14-May-2008 jordan

Adding new ACPI Parser code
Old guts of code still exists, needs to be torn out next
ok marco@
ok brad@


# 1.116 27-Apr-2008 jcs

add acpithinkpad(4), an acpi support driver for ibm/lenovo
thinkpads.

provides functionality that the bios did on older, non-acpi
thinkpads like respond to screen brightness keys and toggle the
bluetooth device on and off.

ok deraadt@ and marco@


# 1.115 24-Apr-2008 jsing

Introduce acpiasus(4), a driver for the ACPI based hotkeys found in many
ASUS laptops (including the ASUS EeePC) - largely based on NetBSD's
asus(4) driver. On the ASUS EeePC this allows us to enable/disable
wireless, change screen brightness and use the volume keys.

ok jsg@, weingart@


# 1.114 12-Apr-2008 kettenis

Get rid of acpi_s5 global variables; simply send SIGUSR2 instead of SUGUSR1
to tell init(8) to power down the machine.

ok krw@


Revision tags: OPENBSD_4_3_BASE
# 1.113 21-Jan-2008 jsg

Check against RESET_REG_SUP in FADT before doing the acpi reset dance
after all, not doing so appears to break more machines than it fixes.

ok marco@


# 1.112 05-Jan-2008 kettenis

Never attach more acpicpu(4) devices than cpu(4) devices. Always attach
the first acpicpu(4) device on non-MULTIPROCESSOR kernels, regardless of its
ACPI processor ID.

ok mikeb@


# 1.111 05-Dec-2007 deraadt

Hang acpi(4) below bios(4) instead of mainbus(4). This lets us move acpi(4)
before pcibios(4), and then.... if acpi(4) attaches, skip pcibios(4) since
it messes the machines up. Fixes claudio's HP dl320 G5, and almost assuredly
others.
ok kettenis


# 1.110 05-Dec-2007 deraadt

repair acpiopen/acpiclose and friends to handle the /dev/apmctl interface,
so that apmd(8) can actually work correctly against acpi(4), and thus,
even do the fancy -C/-A stuff; also tested by jmc


# 1.109 02-Dec-2007 fgsch

fix the trailing space for real now.


# 1.108 02-Dec-2007 fgsch

avoid trailing spaces on output; marco@ ok.


# 1.107 27-Nov-2007 kettenis

Fix off-by-one that made us attach acpicpu0 and acpicpu1 on GENERIC kernels.

ok marco@


# 1.106 25-Nov-2007 jsg

When available and ACPI is enabled use the ACPI reset method
to reboot. This is required for some quirky sis machines
and likely others to reboot.

Tested by many. 'commit early commit often' deraadt@


# 1.105 19-Nov-2007 deraadt

sigh, amd64 still uses that stupid acpi_enabled variable...


# 1.104 19-Nov-2007 deraadt

softc's come initialized to 0, you know...


# 1.103 19-Nov-2007 deraadt

store revision; from jsg


# 1.102 19-Nov-2007 kettenis

Initialize sc_facs earlier.

ok deraadt@, beck@, canacar@


# 1.101 16-Nov-2007 deraadt

fix the bus_space #define nightmare, so that amd64 and i386 are much more
uniform. as a result shared code like acpi needs less #ifdef's
ok marco kettenis


# 1.100 15-Nov-2007 deraadt

remove the #ifdef ACPI_ENABLE. the EC codepath is either safe enough (and
will be improved), or it is unsafe because AML assumes it must be there
ok almost everyone


# 1.99 15-Nov-2007 mikeb

Make acpicpu(4) attach to the existing CPUs only.

Problem reported by form@. Fix idea by kettenis@.
Lots of help from deraadt@. Tested by deraadt@, form@ and me.

ok deraadt marco


# 1.98 14-Nov-2007 deraadt

spacing


# 1.97 12-Nov-2007 deraadt

correct : vs , in attach-time messages


# 1.96 06-Nov-2007 deraadt

all ACPI_ENABLE kernels need acpi_interrupt()


# 1.95 06-Nov-2007 deraadt

move acpi_read_pmreg() and acpi_write_pmreg() out of SMALL_KERNEL so
that acpi ramdisks can compile (obvious solution)


# 1.94 05-Nov-2007 canacar

Establish the acpi interrupt earlier. Fixes problems with some
GENERIC.MP kernels not getting acpi interrupts. This is a
workaround until the real issue with late establishment is fixed.
ok weingart@, beck@, gwk@


# 1.93 03-Nov-2007 jordan

Added support for displaying wakeup devices
ok beck@,weingart@,gwk@


# 1.92 11-Oct-2007 kettenis

When walking the tree yo find _PRT methods, check whether the device is
actually enabled and functioning. Similar to what we do for _INI.

ok weingart@, beck@


# 1.91 08-Oct-2007 krw

More simple memset(,0,) -> M_ZERO changes. In this batch move to
size(*p) as the first malloc() parameter where p is declared locally
and thus easy to check. Add M_ZERO to gpe_table allocation in acpi.c
even though there is no obvious bzero or memset nearby.


# 1.90 13-Sep-2007 mk

Remove unnecessary memset() (see aml_evalnode(9)) and a bit of KNF.

ok toby


# 1.89 13-Sep-2007 weingart

Implement ACPI 6.5.1 spec tree walk for _STA and _INI.
Fixes mk's laptop. No regressions so far (thank you to the
testers).

ok gwk@, mk@, marco@


Revision tags: OPENBSD_4_2_BASE
# 1.88 17-Apr-2007 mk

Now that acpidock no longer hangs machines during boot (at least
according to the reports I've been getting) it should be safe to enable
it again, so remove the #if 0 stuff around it.

ok marco


# 1.87 11-Apr-2007 jordan

Added changes for C-State
ok marco@


# 1.86 26-Mar-2007 marco

Calculate minutes left more accurately.

From giovanni <qgiovanni@gmail.com>


Revision tags: OPENBSD_4_1_BASE
# 1.85 03-Mar-2007 mk

Stop looking for acpidock until we fix that stuff that breaks all pre-60
series ThinkPads with acpidock(4) enabled.


# 1.84 22-Feb-2007 marco

We were getting no interrupts in .MP. There are 2 fixes; one in
acpi_machdep.c and move the interrupt establishment down after ioapic
has been initialized.

ok jordan


# 1.83 21-Feb-2007 kettenis

Make an acpi-enabled GENERIC.MP work on systems that only support PIC mode.

ok marco@


# 1.82 19-Feb-2007 jordan

Added acpi_map_address function for mapping GAS/IO registers
ok marco@


# 1.81 18-Feb-2007 jordan

Added dynamic perfect hash for opcodes
ok marco@


# 1.80 18-Feb-2007 marco

Shave off another K pre compression.
Shuffle SMALL_KERNEL functions around so that we only have a single #ifndef


# 1.79 17-Feb-2007 marco

Add SMALL_KERNEL throught acpi to make it fit on boot media.
ok deraadt


# 1.78 13-Feb-2007 marco

Add smarter _INI code. This helps with some dissapearing devices.

ok kettenis jordan


# 1.77 31-Jan-2007 gwk

Introduce acpi_hasprocfvs thats "ACPI Has Processor Frequency and Voltage
Scalling" a global flag set by acicpu attach to indicate that ACPI is
handling fvs through the _PSS objects. This will fix disappearing
EST/powernow when booting kernels with acpi enabled, as acpicpu isnt
compiled into GENERIC yet. Also in cases where acpicpu is built in and
legacy EST/powernow work but acpi is missing the _PSS object, the legacy
routines will be used.

tested by dim@, and Nick Nauwelaerts <nick AT nauwelaerts.net>
ok marco@, dim@


# 1.76 22-Jan-2007 mk

Actually look for docking stations by looking for _DCK nodes.

ok marco


# 1.75 02-Jan-2007 marco

On some systems EC must be attached as the first device. Move EC from
generic _HID devices to its own routine to force first attach. Bug found
and debugged by joris@

Code mostly written by joris@


# 1.74 26-Dec-2006 marco

Add polling to devices that require it such as acpibat and acpitz.
Use the same tsleep/wakeup mechanism as the interrupt code to keep it non-
concurrent.


# 1.73 21-Dec-2006 deraadt

even more knf love


# 1.72 21-Dec-2006 marco

Remove some leftover acpiec gpe/interrupt code.


# 1.71 20-Dec-2006 deraadt

oops, name already used


# 1.70 20-Dec-2006 deraadt

rename __acpi_enable_gpe(); ok marco


# 1.69 20-Dec-2006 deraadt

acpi_unmap_pmregs() is not used, ok marco


# 1.68 19-Dec-2006 deraadt

simplify some low-level methods substantially (shrinking code); ok marco jordan


# 1.67 06-Dec-2006 mk

malloc(..., M_NOWAIT) with no NULL check is bad. If allocation fails,
complain, clean up, and bail out of attach.

input and ok gwk


# 1.66 28-Nov-2006 jordan

Fixed clearing arg for acpi_init_pic
ok kettenis@


# 1.65 27-Nov-2006 kettenis

Don't spam dmesg.


# 1.64 27-Nov-2006 jordan

Fixes for acpiprt routing
Post-parse fixup for namerefs
ok kettenis@


# 1.63 25-Nov-2006 marco

Don't handle acpiev events inside the gpe handler since this causes deadlocks.

ok jordan@


# 1.62 15-Nov-2006 kettenis

Hook up acpi apic interrupt routing support to generic acpi code.

ok jordan@


# 1.61 02-Nov-2006 marco

Kill acpiec attach function and add its functionality to the generic _HID
device attach

prodded and ok jordan


# 1.60 02-Nov-2006 marco

Fix comments to reflect reality.


# 1.59 19-Oct-2006 marco

Replace ACPI ioctls with APM ioctls. This makes ACPI talk the same protocol
as APM therefore userspace can now retrieve power information in the same
fashion as all other apm arches (zaurus, macppc, i386 etc).

To use force the use of acpi device like so: apmd -f /dev/acpi

ok jordan@


# 1.58 19-Oct-2006 jordan

Fixed GPE debug levels
Fixed ref counter on temporary bufferfields
DSDT parsing fully working on IBM Thinkpad
ok marco@


# 1.57 15-Oct-2006 jordan

Removed old GPE handler code remnants
ok marco@


# 1.56 12-Oct-2006 jordan

Massive ACPI changes
Fixed ACPI leaks
new ACPI API
ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.55 30-Jun-2006 gwk

KNF, No functional change,
"go at it" marco@


# 1.54 30-Jun-2006 jordan

Support for calling Embedded Controller via new GPE interface
ok marco@


# 1.53 30-Jun-2006 jordan

Added new code for GPE handling
Also works with ACPIEC; need to fixup acpiec.c to remove original interrupt handler
ok marco@


# 1.52 30-Jun-2006 jordan

Added offset argument for acpi read/write pmreg routines (new GPE code)
ok marco@


# 1.51 31-May-2006 todd

do not call acpi_powerdown() unless acpi has successfully initialized
ok gwk@ canacar@


# 1.50 29-May-2006 canacar

Add embedded controller support.
ok marco@ deraadt@ testing & ok gwk@


# 1.49 19-May-2006 canacar

Minimal thermal zone driver. Monitors thermal zone temperature,
shuts down the system if the 'critical temperature' is reached.
ok marco@


# 1.48 11-Apr-2006 marco

KNF.


# 1.47 11-Apr-2006 gwk

Add acpi_resume(struct acpi_softc *sc), which calls some AML methods,
detected at attach time, currently unused.

ok marco@


# 1.46 11-Apr-2006 gwk

Discover some ACPI sleep related methods, and call them at the
appropriate time in acpi_enter_sleep_state().

ok marco@


Revision tags: OPENBSD_3_9_BASE
# 1.45 26-Feb-2006 marco

Add acpicpu as a device.

ok jordan@


# 1.44 23-Feb-2006 marco

remove dead function.

ok jordan@


# 1.43 22-Feb-2006 marco

Print out all tables it found during autoconf. We'll need this data in
the future.


# 1.42 22-Feb-2006 marco

Make dmesg less verbose.

ok jordan.


# 1.41 22-Feb-2006 jordan

Added parsing of SSDT tables
ok marco@


# 1.40 22-Feb-2006 jordan

Added new aml_register to notify by device id
Moved powerdown code to button device
ok marco@


# 1.39 21-Feb-2006 marco

Readjust debug levels that crept in too low over the last few days.
KNF


# 1.38 21-Feb-2006 marco

Rig the power button so that the machine shuts down correctly.


# 1.37 20-Feb-2006 marco

Add acpibtn device. Remains disabled for now.


# 1.36 19-Feb-2006 jordan

Added code for aml_notify GPE notification callback
ok marco@


# 1.35 19-Feb-2006 grange

Fix acpi_enter_sleep_state() by replacing current ugly hack
with mostly correct sleeping state transition code.

ok brad@ marco@


# 1.34 19-Feb-2006 marco

jordan:
Enable interrupts.
Added peripheral code to make this work.

marco:
Replace softintr and other types of interrupts with a handler with context.
Fixed interrupt magic for GPEs.
Some KNF

This is work done by jordan and I but has to be a single commit.


# 1.33 17-Feb-2006 marco

Remove earlier introduced delays. We really only need them during autconf.

ok jordan@


# 1.32 17-Feb-2006 marco

Add a generic delay function as per the ACPI spec.

from jordan@


# 1.31 17-Feb-2006 marco

Use tsleep instead of delay when !cold.


# 1.30 17-Feb-2006 marco

Argh, we need delays between reads/writes. The backend reads (i2c most likely)
are so slow that values come back as 0 sometimes. We have to do this
despite the spec saying we shouldnt have to. Guess having a faster
parser than others can hurt sometimes.

Idea from jordan.


# 1.29 17-Feb-2006 jordan

Fixed code for handling _INI
Rearranged debug statements
ok marco@


# 1.28 16-Feb-2006 jordan

Added stub for calling _INI
Fixed aml_dowhile for NULL return
ok marco@


# 1.27 16-Feb-2006 jordan

Fixed parser/evaluator to retrieve battery info+state
Cleanup of parser
Dell laptops return battery status
ok marco@


# 1.26 06-Feb-2006 brad

fix a typo so this will compile on amd64.


# 1.25 04-Feb-2006 marco

When ACPI_ENABLED wasn't set but the devices were it had an unknown variable.

Pointed out by ian@.


# 1.24 03-Feb-2006 jordan

Rewrote aml parser yet again...
Fixed bus_space_map for i386/amd64
Added _BST and _BIF to acpibat to get status/info
ok marco@


# 1.23 20-Jan-2006 jordan

More evaluator changes
Removed old evaluator code
Fixed _HID value
ok marco@


# 1.22 19-Jan-2006 jordan

Added support for read/write of PCI Config space
Help jason@
ok marco@


# 1.21 18-Jan-2006 jordan

Added new evaluation routines
Added helper methods for buffer/field evaluation
Fixed broken indentation on dsdt.c

ok marco@


# 1.20 17-Jan-2006 jordan

Major overhaul of the aml parser. Evaluater temporarily disabled.

ok marco@


# 1.19 06-Jan-2006 grange

Check for ACPI control availability only if ACPI_ENABLE defined.


# 1.18 06-Jan-2006 grange

Informative error messages on attach, prettier dmesg.


# 1.17 06-Jan-2006 grange

printf -> dnprintf conversion for debug messages.


# 1.16 05-Jan-2006 grange

Find SLP_TYPx values and store them for future use in
acpi_enter_sleep_state().


# 1.15 05-Jan-2006 grange

Random KNF and cleanup


# 1.14 05-Jan-2006 grange

Provide symbolic names for the acpi sleeping states and use it.


# 1.13 28-Dec-2005 marco

Moved DEVNAME to a acpidev.h

ok jordan@


# 1.12 18-Dec-2005 sturm

add back comment about wrong code

ok marco


# 1.11 16-Dec-2005 marco

Remove test for kbd_reset. Prompted deraadt@, ok jordan@


# 1.10 16-Dec-2005 jordan

Added hooks for powerdown/power button press


# 1.9 16-Dec-2005 jordan

Cleaned up common power management register i/o
Added enter sleep state ioctl
Hooked power button interrupt
ok marco@


# 1.8 16-Dec-2005 marco

Remove autoconf chatter


# 1.7 16-Dec-2005 jordan

Moved DSDT AML parsing into parent ACPI driver
ok marco@


# 1.6 14-Dec-2005 marco

Add AC device glue.


# 1.5 14-Dec-2005 marco

Adjust debug printing levels to something more sane.


# 1.4 13-Dec-2005 marco

Add glue to attach devices to apci.


# 1.3 07-Dec-2005 marco

KNF


Revision tags: OPENBSD_3_8_BASE
# 1.2 10-Jul-2005 grange

Spacing cleanup.


# 1.1 02-Jun-2005 tholo

Start on a basic ACPI framework -- does not do much more than read out the
ACPI tables into kernel memory and attach ACPI and HPET timers currently.

In order to test this code, enabling the devices in GENERIC as well as
the ACPI_ENABLE option is needed. This code does not do any thermal
control yet, so this should be done with care depending on the platform.

In the tree so more people can contribute to making this more fully
featured.

Ok niklas@ grange@ tedu@


# 1.377 31-Dec-2019 visa

Use C99 designated initializers with struct filterops. In addition,
make the structs const so that the data are put in .rodata.

OK mpi@, deraadt@, anton@, bluhm@


# 1.376 31-Dec-2019 mpi

Convert infinite sleeps to {m,t}sleep_nsec(9).

ok kn@


# 1.375 31-Dec-2019 jsg

include sys/sensors.h for sensor use
don't depend on indirect inclusion from acpidev.h


Revision tags: OPENBSD_6_6_BASE
# 1.374 07-Sep-2019 kettenis

Revert acpipci(4) changes. Too many systems are broken. We'll try again
after 6.6 as been released. The acpireg.h change stays behind.


# 1.373 28-Aug-2019 kettenis

Use ACPI information to attach PCI busses like we do on arm64. There are a
few additional quirks though, and attaching the PCI busses is delayed to
replicate the existing code more closely. That may be changed in the
future. Also tweak how we handle MSI support and respect to ACPI flag
that says we shouldn't attempt to use MSIs.

Some fallout is expected.

ok patrick@


# 1.372 27-Aug-2019 deraadt

On amd64 the bootup and resume paths for SP/BP/SP were not performing
msr/pat, clock-startup, firmware upgrade, fpu initialization, and cpu
soft-state initialization in the same order. That kind of chaos is
crazy and fragile. Improve the acpi resume call sequence and unify order
of the operations.


# 1.371 02-Jul-2019 jcs

acpi_set_gpehandler: turn edge arg into flags, add support for
passing GPE_DIRECT which directly executes the supplied callback and
clears a level interrupt rather than going through acpi_taskq which
can be slow when other driver callbacks are in the queue.

This should not be used for GPE handlers that need to do anything
ACPI-related.

ok kettenis


# 1.370 10-Jun-2019 kettenis

Print proper ACPI version number.

ok mlarkin@


# 1.369 08-Jun-2019 kettenis

When establishing the mapping between ACPI device nodes and PCI devices,
skip PCI host bridges and devices that aren't there.

ok patrick@


# 1.368 07-Jun-2019 kettenis

Introduce acpi_getsta() and use it to eliminate some duplicated code.

ok patrick@


# 1.367 12-May-2019 kettenis

Make sure the extended PM register descriptions actually exist before
using them. Fixes machines with ancient ACPI 1.0 (such as recent
hypervisors).

ok deraadt@


# 1.366 12-May-2019 kettenis

Add support for mapping ACPI PM registers using the "extended" gasio
representation from the FADT. Mostly fixes the Lanner NCA-1510.

ok lteo@, mlarkin@, deraadt@


# 1.365 11-May-2019 lteo

Fix incorrect error message.

ok kettenis@


# 1.364 08-May-2019 tedu

free size. the allocation is the header plus the length.


Revision tags: OPENBSD_6_5_BASE
# 1.363 04-Apr-2019 kettenis

Avoid potential null-pointer dereference. Found by Kent R. Spillner.

ok deraadt@, jsg@


# 1.362 20-Jan-2019 jsg

fix SMALL_KERNEL build after changes in rev 1.361
reported by naddy@ ok deraadt@


# 1.361 19-Jan-2019 tedu

Add a pwraction sysctl that controls what the power button does on acpi.
By default, nothing changes -- shutdown is initiated. But allows turning
power button into a sleep button if desired.
(grudging) ok from a few parties


# 1.360 26-Oct-2018 kettenis

Add acpipci(4) on amd64. For now this only calls the PCI-specific _OSC
method to let the ACPI implementation know what features we support.


Revision tags: OPENBSD_6_4_BASE
# 1.359 30-Aug-2018 kettenis

Move softbutton check before the check that skips attaching certain acpi
devices. Fixes regression caused by matching on _CID in addition to
matching on _HID.

ok matthieu@, mlarkin@


# 1.358 25-Aug-2018 kettenis

Match ACPI devices based on _CID if no match for _HID is found.

ok mpi@


# 1.357 19-Aug-2018 kettenis

Add support for multiple PCI segments. Only really implemented for arm64
for now as amd64/i386 firmware still caters for legacy OSes that only
support a single PCI segment.

ok patrick@


# 1.356 03-Aug-2018 kettenis

Let ahci(4) match on _CLS instead of _HID when attaching at acpi(4). Avoids
having to add many more _HID entries to the match table.

ok deraadt@, mlarkin@


# 1.355 10-Jul-2018 kettenis

Make legacy interrupts work in acpipci(4).

ok patrick@


# 1.354 05-Jul-2018 kettenis

Add acpipci(4), a driver that supports generic ECAM-compatible PCI host
bridges based on information provided by ACPI.

ok mlarkin@


# 1.353 04-Jul-2018 kettenis

Properly pass around the PCI "chipset tag" in acpi(4) and refactor
acpimcfg(4) to call an MD initialization functions that sets up a tag for
PCI ECAM.

ok guenther@, mlarkin@, krw@


# 1.352 01-Jul-2018 kettenis

Move acpi(4) attach glue into acpi_machdep.c.

ok guenther@, deraadt@


# 1.351 01-Jul-2018 kettenis

Add support for _DSD Device Properties.

ok mlarkin@


# 1.350 30-Jun-2018 kettenis

Add intr_enable() function, intended for MI use to amd64 and i386 and use
this in the acpi(4) suspend/resume code paths.

ok deraadt@


# 1.349 29-Jun-2018 kettenis

Use <stdint.h> types.

ok mpi@, jung@, krw@, deraadt@


# 1.348 29-Jun-2018 mlarkin

ACPI: Allow (with warning) GPE handler reassignment, instead of returning
a failure code.

ok kettenis


# 1.347 26-Jun-2018 mlarkin

knf


# 1.346 26-Jun-2018 kettenis

Let acpi(4) pass down the bus dma tag.

ok deraadt@


# 1.345 25-Jun-2018 kettenis

Reorganize acpi(4) code a bit in preparation for upcoming arm64 support.

ok deraadt@


# 1.344 20-May-2018 kettenis

Wake up the acpi thread after scheduling a gpio event.

ok mpi@


# 1.343 17-May-2018 kettenis

Sprinkle some #ifndef SMALL_KERNEL such that RAMDISK kernels cuild again.

pointed out by stsp@


# 1.342 17-May-2018 kettenis

Implement GenericSerialBus OpRegion support.

ok mlarkin@


# 1.341 27-Mar-2018 kettenis

Add acpicmos(4), a driver that implements SystemCMOS OperationRegion
access support. This fixes machines where the AML doesn't check whether
support for this OperationRegion type has been registered by the OS.

ok mlarkin@


Revision tags: OPENBSD_6_3_BASE
# 1.340 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.339 10-Feb-2018 deraadt

Syncronize filesystems to disk when suspending. Each mountpoint's vnodes
are pushed to disk. Dangling vnodes (unlinked files still in use) and
vnodes undergoing change by long-running syscalls are identified -- and
such filesystems are marked dirty on-disk while we are suspended (in case
power is lost, a fsck will be required). Filesystems without dangling or
busy vnodes are marked clean, resulting in faster boots following
"battery died" circumstances.
Tested by numerous developers, thanks for the feedback.


# 1.338 08-Feb-2018 deraadt

After flushing disks, try again to release all memory. We don't need to
save dirty memory to the hibernate space.


# 1.337 08-Feb-2018 deraadt

Toss all releaseable memory, because fragmentation can get in the way
of allocating the hibernate playpen.


# 1.336 08-Feb-2018 deraadt

sr_quiesce() is a new approach for ensuring that softraid drains
output to the disks.

This is part of a larger suspend/resume filesystem-safety diff, which
has been worked on for a couple of months already. Tests by job, krw,
beck, benno, and others. Sometimes even by snapshot users...


# 1.335 29-Nov-2017 kettenis

Add support for GPIO-signaled events.

ok mlarkin@


# 1.334 16-Nov-2017 jcs

Add PCI attachment for dwiic(4) needed by Intel 100 Series machines

ok kettenis


# 1.333 14-Nov-2017 kettenis

Add a few more PNP IDs to the skip lists.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.332 17-Aug-2017 stsp

Make dwiic(4) attach its ACPI parent dependencies.
This change makes the Asus E200HA keyboard work.

Original analysis and patch by Cesare Gargano
ok kettenis@


# 1.331 09-Aug-2017 dcoppa

Add "PNP0303" (8042 PS/2 Controller) to acpi_isa_hids[]

ok kettenis@


# 1.330 22-Jul-2017 jcs

acpibat: look for _BIX first before falling back on _BIF

ACPI 4.0 deprecated _BIF for battery status so some newer machines
are exporting _BIX which extends _BIF's package with a few extra
fields. When using _BIX, export the new discharge cycle count as a
sensor.

feedback from kettenis and anton
ok deraadt


# 1.329 20-Jul-2017 kettenis

Start a list of isa(4) device names such that we can avoid printing them
as "not configured".

ok mikeb@, millert@, deraadt@


# 1.328 08-Apr-2017 deraadt

Sensors are run as callbacks inside tasks. During suspend a sensor
could be running inside a driver that will be force-detached, or due
to tsleep end up disrupting the softstate/hardstate contract. At
suspend time, quisce all these callbacks by waiting for completion.
This issue has never been observed for real, but may be implicated in
suspend/resume failures.
ok kettenis guenther mlarkin


# 1.327 08-Apr-2017 deraadt

Two prototypes for acpi_maptable; merge to one place


Revision tags: OPENBSD_6_1_BASE
# 1.326 27-Mar-2017 deraadt

Now that hibernate_alloc() only has clean success/failure, don't
need to call hibernate_free() to clean up a partial mess.
ok mlarkin kettenis


# 1.325 13-Mar-2017 deraadt

allow ramdisks to build; ok jcs


# 1.324 12-Mar-2017 jcs

try to attach acpisbs first and if successful, don't attach acpibat


# 1.323 02-Mar-2017 natano

Add a new sysctl machdep.lidaction. The sysctl works as follows:

machdep.lidaction=0 # do nothing
machdep.lidaction=1 # suspend
machdep.lidaction=2 # hibernate

lidsuspend is just an alias for lidaction, so if you change one, the
other one will have the same value. The plan is to remove
machdep.lidsuspend eventually when people have upgraded their
/ets/sysctl.conf.

discussed with deraadt, who came up with the new MIB name
no objections mlarkin
ok stsp halex jcs


# 1.322 28-Feb-2017 natano

Untangle abstract sleep modes from ACPI sleep states. This paves the way
for further work in the area.

idea and ok deraadt
ok mlarkin


# 1.321 22-Feb-2017 jcs

add acpisbs, an acpi smart battery subsystem driver reading data
over smbus

currently disabled because it conflicts with acpibat

ok deraadt, kettenis


# 1.320 14-Jan-2017 kettenis

Revert aml_rdpciaddr changes; breaks several machines that were working
before.


# 1.319 11-Jan-2017 jsg

Increase the size of the buffers used for _HID and _CID strings to be
able to fit "Hyper_V_Gen_Counter_V1".

ok mlarkin@ deraadt@


# 1.318 08-Jan-2017 kettenis

Make aml_rdpciaddr() work for for devices behind bridges. The code now
uses the mapping between AML nodes and PCI devices that we establish.
Because _INIT methods may end up calling aml_rdpciaddr(), make sure we
create that mapping early. Also handle devices that aren't actually present.
These devices are now included in the mapping and reads will return an
all-ones patterm whereas writes are a no-op..

ok mlarkin@


# 1.317 25-Oct-2016 pirofti

Add the resource index to aml_parse_resource() callback function.

This is needed by an upcoming acpiec commit that handles machines breaking
the current ACPI specifications.

Change suggested by and ok kettenis@, guenther@


# 1.316 18-Sep-2016 guenther

If the APIC table (MADT) isn't present then ci_acpi_proc_id won't be set
when acpicpu tries to attach, leading to a NULL dereference. The ACPI
processor object on the Dells where this was reported is bogus and useless
anyway so apply the same test when deciding whether to attach to processor
objects as acpicpu would when matching them to cpus.

Problem reported by Eivind Eide (xenofil (at) gmail.com) and
Johan Huldtgren (johan+openbsd-bugs (at) huldtgren.com).
Testing by Eivind Eide.
ok kettenis@ mlarkin@


# 1.315 03-Sep-2016 naddy

Write the system time back to the RTC every 30 minutes.
This fixes the problem that long-running machines which were not
shut down properly would reboot with a badly offset system time.

hints and ok kettenis@


# 1.314 31-Aug-2016 mlarkin

Don't put a device in the wake device list if _STA says it's not there.

Fixes at least one machine that woke up from sleep / poweroff immediately.

ok kettenis@, tested by kettenis and martijn@


# 1.313 28-Jul-2016 kettenis

Store the acpi processor ID/UID in struct cpu_info, and use it to attach
acpicpu(4) drivers to the right cpu(4).

ok mlarkin@, guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.312 10-Jun-2016 kettenis

The power management timer has been made optional in ACPI 5.0A, so don't
attempt to attach acpitimer(4) if the timer isn't present.

Pointed out by jcs@

ok jcs@


# 1.311 08-May-2016 kettenis

Make sure devices listed by a device's _DEP method are attached before
we attach the device itself. The _DEP methode, introduced in ACPI 5.0,
is a hint that the device in question depends on OpRegion support from
the devices listed by _DEP, so we have to order them the proper way.
To prevent us from attaching those devices again when we encounter them
later walking down the device tree, keep track of our attempts to
attach a device driver to them using aflag in the aml_node structure.

ok guenther@


# 1.310 25-Apr-2016 pirofti

We attach acpihpet(4) and acpiec(4) without using the HID, so do not
report it in the dmesg.

OK deraadt@, mlarkin@.


# 1.309 02-Apr-2016 kettenis

Reduce dmesg spam by knocking out some acpi hardware IDs that don't need a
driver.

ok mpi@


# 1.308 29-Mar-2016 kettenis

Check if a device is present (using _STA) in acpi_foundhid. This prevents
us from reporting devices as "not configured" that aren't actually present,
and allows us to remove duplicated code from several drivers.

ok deraadt@, ok jsg@


# 1.307 29-Mar-2016 kettenis

Implement a cleaner way to prevent acpivout(4) from attaching if we have
a platform-specific method of controlling the backlight.

ok millert@, mlarkin@, jung@


# 1.306 28-Mar-2016 kettenis

Adding each and every new acpi device driver to acpi_foundhid() is
gettinga bit of the burden. And it really isn't how our autoconf
framework is supposed to work for busses that can be enumerated. All
the drivers already check for a matching _HID in their attach
function. So we can just drop the checks in acpi_foundhid() and let
the drivers sort themselves out.

Print a "not configured" line for devices for which we don't attach adriver.
This may be a bit much as there are devices for which we will not have a
driver. This will be removed if it causes too much dmesg spam.

ok deraadt@, mlarkin@


Revision tags: OPENBSD_5_9_BASE
# 1.305 17-Jan-2016 jsg

Take acpi_foundhid() outside of #ifndef SMALL_KERNEL so the ramdisk
kernel can find and attach dwiic(4) and sdhc(4).


# 1.304 17-Jan-2016 jsg

remove an unused global variable


# 1.303 14-Jan-2016 kettenis

Attach dwiic(4) on the Intel Bay Trail i2c controllers as well.


# 1.302 12-Jan-2016 kettenis

Improve emulation of PM registers on Hardware-redcued ACPI by emulating
SPL_TYPx, SLP_EN and WAK_STS using the new SLEEP_STATUS_REG and
SLEEP_CONTROL_REG registers. Makes the transition into S5 (powerdown) work
on the ASUS X205TA.

ok deraadt@, mlarkin@


# 1.301 12-Jan-2016 jcs

Add dwiic, a driver for the Synopsys DesignWare i2c controller found
on the Samsung ATIV Book 9 laptop. This initial version only
supports ACPI config/attachment.

Add ihidev, a HID-over-i2c driver largely based on uhidev. dwiic
handles attaching ihidev devices found in ACPI.

Add ims, a HID-over-i2c mouse/trackpad driver to get basic cursor
and button functionality from HID-compliant i2c trackpads.

ok kettenis deraadt


# 1.300 11-Jan-2016 kettenis

Make sdhc(4) attach to hardware IDs "80860F14" and "PNP0FFF".


# 1.299 09-Jan-2016 kettenis

Initial support for Hardware-Reduced ACPI. This skips the legacy mode checks
by emulating the PM_CNT register such that SCI_EN is always enabled. It also
skip attaching acpitimer(4) since the relevant registers are no longer there.

ok deraadt@, mlarkin@


# 1.298 06-Jan-2016 kettenis

Follow the spec and check the SCI_EN bit to see if ACPI has already
been enabled and don't bother enabling it again if it is. This should fix
systems that don't implement legacy mode and therefore set ACPI_ENABLE and
ACPI_DISABLE to zero.

ok mlarkin@, yasuoka@


# 1.297 23-Nov-2015 reyk

Back out the CRITICAL change, turn it back from 10% to 25%. Some
people with old/broken batteries didn't like this change, and the
underlying heuristics are wrong.


# 1.296 22-Nov-2015 reyk

The "CRITICAL" APM battery level, that is exported/faked by the ACPI
stack, is currently set to 25%. With 25% battery left, a typical
modern laptop will have at least one hour runtime left. It is an
arbitrary value, and other APM power layers in the kernel set CRITICAL
to something like 5% or 10%, so set it to 10% for now.

OK mlarkin@


# 1.295 28-Sep-2015 deraadt

In low-level suspend routines, set cold=2. In tsleep(), use this to
spit out a ddb trace to console. This should allow us to find suspend
or resume routines which break the rules. It depends on the console
output function being non-sleeping.... but that's another codepath which
should try to be safe when cold is set.
ok kettenis


# 1.294 06-Sep-2015 deraadt

clarify malloc arguments (base + length...)


# 1.293 28-Aug-2015 deraadt

fairly simple sizes for free(); ok tedu


# 1.292 20-Aug-2015 kettenis

Respect the access size when reading or writing to pci config space.
This version also makes sure writes are properly aligned, preventing
the panics seen with the previous incarnation of this change.

Fixes battery status passthrough in vmware, brightness keys on my x220
and who knows what else.

ok mlarkin@, deraadt@


Revision tags: OPENBSD_5_8_BASE
# 1.291 04-Aug-2015 deraadt

If we are setting ACPI_PM1_BM_RLD do it only once, and remember to
repeat this action at resume time.
ok mlarkin guenther


# 1.290 29-Jul-2015 deraadt

Something subtle in previous revision is locking up suspend or booting
on some laptops. Mailing lists threads mention x220 and Asus machines.
Reverting for now.


# 1.289 21-Jul-2015 kettenis

iRespect the access size when reading or writing to pci config space.
Fixes battery status passthrough in vmware and who knows what else.

Tested by many.

ok mlarkin@, deraadt@


# 1.288 17-Jul-2015 mlarkin

fix comments


# 1.287 26-Mar-2015 jsg

Use the 0x10 extended capability to determine if the trackstick
buttons are wired to the trackpad and need to be re-routed to the
trackstick. Without this change the buttons on 2015 thinkpads
get picked up as extended buttons that show up as scroll up/down.

Remove the X1 Carbon 2015 (LEN0048) and X250 (LEN0046) from the
top button area/soft buttons quirks list. Also avoid using the
quirk list entirely if the capability bit is set.

Based on work from jcs@ with feedback from mpi@
ok mpi@ jcs@


# 1.286 20-Mar-2015 kettenis

The ACPI standard says that a PCI function number of FFFF can be used to refer
to all functions on a device. This makes no sense in the context of accessing
PCI config space. Nevertheless stsp@ has a machine with AML that tries to
do config space reads from a device where _ADR encodes function number FFFF.
This is almost certainly buggy AML, but since Linux (and presumably Windows)
boot just fine on this machine we need to work around this. Linux effectively
treats function FFFF as function 7, which isn't present on this machine.
Instead we simulate a read from a non-existent device by returning a bit
pattern of all ones. Writes to function FFFF will still panic.

ok mlarkin@


# 1.285 16-Mar-2015 deraadt

Reduce use of SMALL_KERNEL in acpi, bringing acpiec driver and more gpe
handling into RAMDISK. This is now possible because the install media
has ample room. The goal is to reduce special cases where we may be
skipping (unknown) important operations...
ok mlarkin kettenis


# 1.284 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.283 07-Feb-2015 deraadt

New framework that allows hibernate to pass in entropy from it's fresh
boot.
ok mlarkin


# 1.282 06-Feb-2015 mlarkin

Fix a hibernate crash on some machines due to unmapping a page that
may not have been mapped previously (in the failure to hibernate case).

Also ensure that the lowmem ptp is mapped in all cases (not just MP).

ok kettenis


# 1.281 17-Jan-2015 deraadt

resume_randomness() before spinning up other cpus...


# 1.280 15-Jan-2015 jsg

Define a new wscons mouse type for Synaptics clickpad devices that lack
physical buttons. This will be used if the acpi pnpid for the mouse
matches a list provided by Synaptics found in Linux.

Combined with the xenocara changes this will remove the need for an xorg
config file for the x240/t440/t540 generation of thinkpads.

ok matthieu@ shadchin@ kettenis@


# 1.279 11-Jan-2015 kettenis

Tolerate checksum errors in ACPI tables (but complain about them). Apparently
there are machines out there with broken BIOSen with such checksum errors in
thier RSDT tables. Windows runs fine on these machines, so it probably does
not check. Linux ignores checksum errors as well (and prints a warning).

ok deraadt@, krw@


# 1.278 18-Dec-2014 deraadt

suspend and resume the RNG (at what looks like the right spots)
ok reyk djm


# 1.277 09-Dec-2014 doug

Replace some malloc(n*size,...) calls with mallocarray().

ok tedu@ deraadt@


# 1.276 06-Dec-2014 mpi

If "machdep.lidsuspend" is not set, do not go back to sleep if the lid
is still closed when resuming. This allows docked laptop to be resumed
without opening them.

ok mlarkin@


# 1.275 23-Nov-2014 mlarkin

If a machine has no _LIDs, then none can be open. This diff fixes a problem
where machines without any _LIDs were going back to sleep after hibernate
due to the recent suspend-if-lid-closed-on-resume diff. Noticed on qemu,
which doesn't have any _LIDs.

discussed with deraadt


# 1.274 18-Nov-2014 krw

Nuke yet more obvious #include duplications.

ok miod@


# 1.273 08-Nov-2014 mlarkin

If resuming from sleep (zzz/ZZZ) and the lid is still closed, go back to
sleep. This prevents accidental lid flex or slight opening in a backpack
from waking the machine up and leaving it resumed (powered on).

ok deraadt@


# 1.272 26-Sep-2014 kettenis

Rework piglet and pig allocation. Currently the piglet gets allocated
deep down in the suspend path, where it is really hard to recover from
allocation failure. So allocate the piglet early on in the suspend path.
Also change the piglet and piglet allocation functions to use km_alloc(9)
instead of doing pmemrange magic. This removes a bunch of code which, in the
case of the piglet allocation, is broken since it results in a NULL pointer
dereference. Also switch the piglet allocation to not wait. If we can't
allocate 16MB of phys contig memory on a halfway modern machine we're almost
certainly under a lot of memory pressure and we're better off not trying to
hibernate anyway.

ok mlarkin@


# 1.271 25-Sep-2014 kettenis

Bring back rev 1.249. Now that mp_setperf() has been fixed, it should be
safe to restore perflevel when resuming. Should fix the issues people
see with the processor running at full speed heating up the machine while
the apm command reports that the CPUs are running at a low speed.

ok dcoppa@, deraadt@


# 1.270 23-Sep-2014 kettenis

Release the acpi lock around calls to wsdisplay_suspend() and
wsdisplay_resume(). The X server may adjust the display brightness just
before/after the VT switch when suspending/resuming. In fact, it may
already be blocked adjusting the display brightness for some reason
unrelated to the suspend request.

The VT switch request will eventually time out, but the delay is annoying
and the failed VT switch will lead to problems after resume.

Needed for a future update of the xf86-video-intel driver.

ok mlarkin@


# 1.269 19-Sep-2014 kettenis

Use config_suspend_all(9).


# 1.268 08-Sep-2014 kettenis

Clear the PME Status bit on PCI devices in response to Device Wake events.
Fixes the interrupt storm on the 2009 Mac mini with WOL enabled on its
nfe(4) interface.

ok mlarkin@


Revision tags: OPENBSD_5_6_BASE
# 1.267 20-Jul-2014 mlarkin

Support hibernating to softraid crypto volumes.

much help and ok from deraadt@


# 1.266 16-Jul-2014 mlarkin

Reenable hibernate RLE support and flush+zero all memory after unpack.


# 1.265 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.264 12-Jul-2014 kettenis

Add a function to drop all clean pages on the page daemon queues and call
it when we hibernate.

ok mlarkin@, miod@, deraadt@


# 1.263 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.262 11-Jul-2014 mlarkin

Flush the buffercache to 16MB on hibernate and restore its previous max
size (kern.bufcachepercent) on resume, for better hibernate performance.

ok beck@


# 1.261 10-Jul-2014 blambert

remove unneeded header

ok mlarkin@


# 1.260 01-Jul-2014 kettenis

Adding an additonal delay after each access to SystemMemory or SystemIO
during autoconf makes no sense.

ok deraadt@


# 1.259 23-Jun-2014 kettenis

SystemMemory is used to access memory mapped registers on some machines, so we
must use the bus_space(9) API to access it instead of memcpy(9). Also make
sure we properly align access to fields that need more than byte alignment.

ok guenther@


# 1.258 21-May-2014 mlarkin

Fix invalid / missing format specifiers in several dnprintf statements
(used only when ACPI_DEBUG is enabled)

From a diff on tech@ by Fabian Raetz, with a minor change by me, tested
on i386 and amd64 with ACPI_DEBUG enabled and disabled.


# 1.257 25-Apr-2014 mlarkin

Defer the free of the hibernate structures until later, when it is safe to
do so.

Freeing the hibernate structures too early leads to inadvertent calls into
splx when we aren't ready for them yet. Found when trapping splx calls in
the resume path.

discussed with deraadt@ at m2k14


# 1.256 13-Apr-2014 mlarkin

Fix a badly worded debug message that implied the entire machine was going
to sleep, instead of just the acpi thread going to sleep.


# 1.255 31-Mar-2014 mpi

Change the order of operations during a suspend/resume cycle and
call bufq_quiesce() after executing the DVACT_QUIESCE handlers.

This should be safe since no disk nor controller drivers have such
handler but it will allow us to detach sd(4) devices attached to a
USB bus.

Another benefit pointed out by kettenis@ is that drivers that need
to read a firmware from the disk should be able to do it at resume
time in a DVACT_WAKEUP handler.

ok kettenis@, deraadt@


# 1.254 13-Mar-2014 dlg

get rid of the assumption that the head of the alldevs list is the
"mainbus" device. this breaks when mpath is enabled because it
attaches before mainbus and therefore takes the head position.

have autoconf provide device_mainbus() which looks up mainbus_cd,
and use that instead.

discussed with deraadt who just wants mpath stuff to move forward
despite there being many ways to shine this particular turd.


Revision tags: OPENBSD_5_5_BASE
# 1.253 21-Feb-2014 deraadt

Do the lid control further on in the resume sequence, since this is
AML, and will do WAITOK operations. While cold, that is a bad idea.
We seem to be safe with respect to the spec, I do not see any event
debounce issues here.
ok kettenis, tested by many


# 1.252 20-Jan-2014 claudio

Instead of checking sc->sc_revision <= 1 check that the
fadt->hdr_revision <= 1. This is a better check since it checks
the version of the proper descriptor. This allows the alix APU
to reboot since those boards have currently a wrong sc->sc_revision.
Committing this now so that it can be tested easier.
With and OK jsg@ also fine by derradt@


# 1.251 24-Dec-2013 kettenis

If the FADT has its SMI_CMD set to zero, assume we're only ACPI-only hardware
and don't need to disable SMI ownership of the ACPI hardware registers.

ok mlarkin@


# 1.250 23-Dec-2013 kettenis

Revert previous commit. It makes resume hang on some MP systems running
GENERIC.MP.


# 1.249 16-Dec-2013 kettenis

Restore hw.setperf upon resume like we do for apm(4) on i386. Seems we need
to do this fairly late in the resume to avoid hangs.

ok deraadt@, mlarkin@


# 1.248 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


# 1.247 06-Nov-2013 mpi

Add support for Power Resources for Dx states and the necessary hook
for PCI devices. This hook should be called twice, before and after
changing the power state of a PCI device.

Before setting the device to the new state, the ACPI layer will notify
every power resources linked to the device for that state and make sure
they are turned "_ON". After changing the state of the device, it will
decrement the reference of every power resources linked to that device
for the old state and turn them "_OFF" if they are no longer referenced.

This fixes the no-USB after resume problem seen on various ThinkPad,
problem initialy diagnosed with Alexander Polakov.

ok kettenis@, deraadt@


Revision tags: OPENBSD_5_4_BASE
# 1.246 01-Jun-2013 mlarkin

Stop printing ACPI wakeup devices in dmesg after the 16th wakeup device.

Helps with VMware VMs that report hundreds of wakeup devices.

ok reyk@, deraadt@


# 1.245 31-May-2013 bluhm

Fix the build for a kernel without wd and pciide in its config.
OK deraadt@ tedu@


# 1.244 30-May-2013 deraadt

Enforce ca_activate tree-walks over the entire heirarchy for all events,
cleaning up some shutdown-hook related code on the way.
(A few drivers related to sparc are still skipped at kettenis' request)
ok kettenis mlarkin, tested by many others too


# 1.243 18-Apr-2013 deraadt

pin the acpi thread to the BSP, because AML and SMI cannot be trusted. I
was certain we were doing this already, but it turns out no...


Revision tags: OPENBSD_5_3_BASE
# 1.242 24-Dec-2012 guenther

In acpiioctl(), Return though the splx() path if suser() returns an error

ok deraadt@


# 1.241 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


# 1.240 04-Oct-2012 ehrhardt

Use information provided by ACPI to attach secondary PCI host bridges.

ok kettenis@


# 1.239 07-Sep-2012 kettenis

Add a new function called acpi_pci_min_powerstate() that returns the lowest
power state for a PCI device for the power state (currently S3 or S4) we're
transitioning into. This function is probably too simplistic; there is a lot
of (silly) complexity in the ACPI spec.

ok deraadt@


Revision tags: OPENBSD_5_2_BASE
# 1.238 13-Jul-2012 pirofti

Disable acpivideo(4) on ASUS laptops.

There are some models that panic whenever an event is received.
Some cope, some don't. In order to get this right acpiec(4) requires
major rework. Until then let machines boot.

Discussed with deraadt@.


# 1.237 11-Jul-2012 mlarkin

for now, restrict hibernate to root in the acpi ioctl kernel interface.
normal rules for controlling this interface through apmd still apply.

ok deraadt@


# 1.236 09-Jul-2012 deraadt

tiny little oops in untested hibernate block


# 1.235 09-Jul-2012 deraadt

clean up ioctl() case statements


# 1.234 09-Jul-2012 deraadt

acpi_disable_onegpe() is not used; ok pirofti


# 1.233 24-May-2012 kettenis

If _MIN > _MAX consider a WordBus resource to be invalid. Fixes a panic on
the IBM x3550-M4.

ok mikeb@


# 1.232 29-Mar-2012 mlarkin

Missing #ifdef in previous diff


# 1.231 29-Mar-2012 mlarkin

Prevent hibernation attempts on machines with unsupported disk controllers.
The same check is also performed later in the hibernate sequence, but by
then it's too late to cleanly unwind (presently).

ok deraadt


# 1.230 26-Mar-2012 deraadt

simplify conditionals in acpi_sleep_state()
ok mlarkin


# 1.229 26-Mar-2012 deraadt

hook in the hibernate request code; half of this diff is from mlarkin
ok mlarkin


Revision tags: OPENBSD_5_1_BASE
# 1.228 20-Sep-2011 deraadt

Very early in suspend while the clock is still ticking realtime,
save the clock back to the rtc. The zaurus already did this.
ok phessler miod kettenis


Revision tags: OPENBSD_5_0_BASE
# 1.227 02-Jul-2011 nicm

kqueue attach functions should return an errno or 0, not a plain 1. Fix
the obvious cases to return EINVAL and ENXIO.

ok tedu deraadt


# 1.226 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.225 16-Jun-2011 pirofti

Import acpitoshiba driver.

This is originally written by Hiroyuki Aizu, ported and integrated
in our tree by Javier Vazquez <javama4 AT gmail> with tweaks and
suggestions by me.

Import prodded by deraadt@ and the lack of testers on tech@.


# 1.224 27-Apr-2011 jcs

attach acpithinkpad to newer lenovo models like the x120e

ok deraadt@


# 1.223 22-Apr-2011 jordan

Fix uninitialzied variables and formatting strings (-Wxxx errors)


Revision tags: OPENBSD_4_9_BASE
# 1.222 02-Jan-2011 jordan

Re-add code for ACPI taskqueue, eliminates need for state vars in acpi softc
Serialize processing ACPI tasks for sleep/powerdown/GPE event
ok deraadt@


# 1.221 31-Oct-2010 guenther

Revert last commit: it breaks resume on Thinkpads
"then please back it out" deraadt@


# 1.220 26-Oct-2010 jordan

Add task queue for ACPI gpe and notify handlers
This fixes eject on Dell Latitude dock and an issue on the Dell Mini
battery update.
ok deraadt


# 1.219 07-Oct-2010 jordan

Create separate functions for enabling wake and runtime gpes.
Remove old suspend/resume gpewalk function
ok deraadt, mlarkin


# 1.218 05-Oct-2010 jordan

split acpi_enable_onegpe to separate enable/disable functions
ok deraadt


# 1.217 05-Oct-2010 jordan

Added acpi_disable_allgpes function to clear GPE status
ok deraadt


# 1.216 05-Oct-2010 deraadt

clean up acpi_softc use more


# 1.215 05-Oct-2010 deraadt

delete a bit of #if 0 code


# 1.214 05-Oct-2010 deraadt

hide use of our global acpi_softc pointer a bit better


# 1.213 29-Sep-2010 deraadt

grab the acpi thread lock very early in acpi thread startup; not that it
is likely that anyone will race us... but why be careless.
ok kettenis


# 1.212 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.211 29-Aug-2010 jasper

- add a check for FWRITE here too.

ok miod@


# 1.210 27-Aug-2010 kettenis

Make PCI Power Management optional, and only enable it when acpi(4) attaches.

ok deraadt@


Revision tags: OPENBSD_4_8_BASE
# 1.209 08-Aug-2010 kettenis

Close race between the acpi thread and wscons ioctls running in process
context. We might rip this out post-release and replace it with a solution
that serializes things in a more obvious way.

ok deraadt@, miod@ and "those crazy texans who ok anything"


# 1.208 08-Aug-2010 deraadt

Report the acpi sleep states when on a RAMDISK; ok krw


# 1.207 07-Aug-2010 kettenis

Enforce acpi register access rescrictions.

ok mlarkin@, deraadt@, tested by marco@


# 1.206 07-Aug-2010 deraadt

upon resume, notify all the acpiac and acpibat drivers to update
their status. some laptops do not do this automatically.
ok kettenis canacar


# 1.205 06-Aug-2010 marco

Always call _PSW on _LID devices that support it to enable lid open to
resume the machine. Conversely disable it when the machine wakes up.

Tested by several ok deraadt


# 1.204 06-Aug-2010 deraadt

As part of a revamp of the PM1/GPE code, I write this diff to look for
unmanaged EN & STS bits in the PM1 register at interrupt time and report
them. As a side effect this splits the STS acknowledgement into two writes
(for power, and sleep) instead of one. The printf that is added (to spot
unmanaged STS bits) has not yet been triggered as far as we know.

Before the "write to PM1 registers at the right offset" diff went in, this
was not neccessary. But newer thinkpads do not have a working soft power
button without this diff.

We have no idea why.
ok mlarkin kettenis


# 1.203 05-Aug-2010 deraadt

PM1 power button events were generating 'fake events' to acpibtn PWRB,
but if there is no such device these were being tossed. Instead, arrange
for all power button handlers to flag a softc variable which is processed
by the thread; so that one place will know to request a powerdown (psignal
to init)
ok marco mlarkin


# 1.202 05-Aug-2010 deraadt

rename a variable which is annoyingly poorly named


# 1.201 05-Aug-2010 mlarkin

Write to the correct offset for various ACPI pmreg registers. Writing to
the correct register does properly clear the wake status bits and fixed
event bits, which allows some machines to have more than 1 suspend cycle.

bug spotted by deraadt@

tested by deraadt@ and myself on a half-dozen machines and others on
hackers@

ok deraadt@, marco@


# 1.200 05-Aug-2010 deraadt

Fix the power button. The pm1 register is 16 bits in size.
My mistake, a while back.
tested by krw


# 1.199 04-Aug-2010 kettenis

Print supported sleep states.

ok marco@, phessler@, deraadt@


# 1.198 03-Aug-2010 mlarkin

Use the proper flag for re-enabling certain hardware events (power btn,
sleep btn). The (incorrect) flag prevoiously used coincidentally had the
same value, so this shouldn't cause any different behavior than before.

tested on a variety of machines (i386, amd64, sp, mp) - no behavioral
change seen

ok deraadt@


# 1.197 28-Jul-2010 marco

Make all gasio failures look and behave the same.


# 1.196 28-Jul-2010 mlarkin

Delay a little longer on suspend before giving up. Fixes machines that
sometimes don't suspend because the sleep transition takes too long
(symptoms like failing to sleep but the backlight stays on).

ok deraadt@


# 1.195 27-Jul-2010 deraadt

remove XXX; EC can now be attached early when needed; ok jordan


# 1.194 27-Jul-2010 deraadt

more brackets feels good


# 1.193 27-Jul-2010 deraadt

XXX annotate a block in acpi_enter_sleep_state() that is pretty worrying


# 1.192 27-Jul-2010 deraadt

horrific indentation that nearly made me puke


# 1.191 27-Jul-2010 mlarkin

Set the sleeping indicator light on machines that support it, in case SMI
doesn't do it for us. Shows the 'moon' LED on older Thinkpads when
sleeping.

Tested on T43p, W500, T510 by me and on X61s by deraadt@.

ok deraadt@


# 1.190 27-Jul-2010 jordan

Early initialization of acpiec if ECDT table exists
Fixes hang when booting thinkpads while docked
ok deraadt


# 1.189 26-Jul-2010 deraadt

remove merge error which was commited


# 1.188 26-Jul-2010 pirofti

Add support for Sony ACPI hotkeys via a new driver: acpisony(4).

Currently it only works for the suspend button and tries to do right for
the brightness events, but I haven't found a Sony laptop that like to do
right so far.

In the future I want to make the brightness keys work on all Sony's and
also add support for the zoom hotkeys and whatever other funky keys I can
find on those things.

Okay deraadt@.


# 1.187 22-Jul-2010 deraadt

pretty large cleanup of SMALL_KERNEL


# 1.186 21-Jul-2010 deraadt

in the acpi GPE handler, late ack level interrupts not edge interrupts.
Duh.


# 1.185 21-Jul-2010 deraadt

Merge the edge and level gpe handlers into one (now possible since the
gpe structure tells us whether the gpe is edge or level)


# 1.184 21-Jul-2010 deraadt

delete #if 0 code we are not going to use; ok marco


# 1.183 20-Jul-2010 deraadt

make the mask code for acpi_enable_onegpe() match other similar code


# 1.182 20-Jul-2010 deraadt

Handle the acpi interrupt controller with proper edge/level handling,
wrapped in spltty to avoid racing against the interrupt controller.
Repair the gpe bit masking code while there, and do operations in the
right order
ok kettenis mlarkin, help from jordan at figuring out the order of
operations.


# 1.181 20-Jul-2010 matthew

When we're preparing to go to sleep, only call bufq_restart() if other
preparations failed.

ok kettenis@, thib@, deraadt@


# 1.180 19-Jul-2010 deraadt

permit compilation with ACPI_DEBUG


# 1.179 19-Jul-2010 deraadt

there is no need for a seperate dsdt_softc pointer; dsdt does not have a
softc but is just a part of the single acpi driver.
ok jordan


# 1.178 19-Jul-2010 deraadt

acpi_delay() is no longer needed; ok jordan


# 1.177 18-Jul-2010 mlarkin

Clear event status on resume, to avoid going back to sleep/reboot/shutdown
immediately after resume. Tested by a bunch of people on hackers@ on
various hardware. Fixes at least Toshiba NB200 and Gateway LT3103u.

ok marco


# 1.176 13-Jul-2010 deraadt

rename some thread related interfaces so they are logical


# 1.175 10-Jul-2010 jordan

Verify _ADR bdf is correct (some systems return 0xffff)
Works on Dell x9xx and Studio 1555


# 1.174 09-Jul-2010 jordan

Clear the noise


# 1.173 08-Jul-2010 jordan

Add mapping for ACPI device to PCI bdf (match autoconf tree)
Simplify resource parsing function to use buffer argument
Convert namespace linked lists to use queue macros
ok marco@, deraadt@


# 1.172 06-Jul-2010 deraadt

Use spltty() to lock downcalls from apm against the information being
modified by the acpi thread.
ok kettenis


# 1.171 06-Jul-2010 deraadt

Don't use a workq to do the suspend, because it races aginst the acpi
thread. Instead, just tell the acpi thread to do the suspend for us.
This makes apmd & zzz work correctly. While here, have acpithinkpad
attempt to post the event to apm before prompting the suspend itself.
ok kettenis marco mlarkin


# 1.170 05-Jul-2010 mlarkin

Reset register SCI_EN on resume. The ACPI spec clearly states we don't have
to do this, but many machines don't, or forget to, which leads to all sorts
of problems on resume (broken battery stats, overheating, failure to resume
more than once, etc).

tested by many, ok marco@, deraadt@


# 1.169 01-Jul-2010 thib

do bufq quiesce again, since vnd/the bufq_done call in
biodone has been fixed.


# 1.168 01-Jul-2010 jordan

Backout recent AML changes, breaks on R210 and others


# 1.167 01-Jul-2010 jordan

Simplified aml_parse_resource call, cleanup code
ok marco


# 1.166 01-Jul-2010 jordan

Fix subordinate bus # for multi-root PCI buses
ok marco, kettenis


# 1.165 30-Jun-2010 thib

Disable/partially backout the bufq quiesce changes as this
is causing havoc with vnds and release must be buildable.


# 1.164 29-Jun-2010 deraadt

Fix ramdisks (new code was placed inside a SMALL_KERNEL chunk)


# 1.163 29-Jun-2010 jordan

Add support for mapping ACPI to PCI devices
ok kettenis, deraadt


# 1.162 29-Jun-2010 kettenis

Call bufq_quiesce() on suspend to stop all I/O and bufq_restart on resume.
Makes suspend/resume work much better while doing disk I/O.

ok mlarkin@


# 1.161 29-Jun-2010 tedu

60 is a more reasonable guess for number of minutes in an hour than 100.
spotted by TeXitoi. ok deraadt


# 1.160 27-Jun-2010 jordan

Add _acpi_matchhids routine, precursor to pci device match


# 1.159 27-Jun-2010 mlarkin

Clear ACPI fixed event status on resume (power buttons/etc) so that some
machines don't immediately resume after suspending to S3.

Tested by kettenis@ and I on 5 or 6 machines, deraadt@ "yeah, looks good"


# 1.158 27-Jun-2010 jordan

Cleaned up acpi_maptable, adds to table list directly


# 1.157 07-Apr-2010 deraadt

If we have done a wsdisplay_suspend(), and ACPI decides it does not want to
suspend, we must roll back by calling wsdisplay_resume() or the virtual
console switching will be locked forever
ok oga


# 1.156 07-Apr-2010 kettenis

Make suspend/resume work on MP machines (running an MP kernel). Joint work
from mlarkin@ and me, with some amd64 fixes thrown in by deraadt@

ok marco@, deraadt@, pirofti@, mlarkin@


# 1.155 31-Mar-2010 kettenis

Bring /dev/apm support in line with apm(4). Make the suspend button send
a suspend request event to apmd(8) instead of suspending immediately. Also
keep track of whether /dev/apm and /dev/apmctl are currently open, such that
we can still suspend immediately if apmd(8) isn't running.

ok deraadt@, marco@, pirofti@, jsing@, oga@


# 1.154 30-Mar-2010 oga

Prevent the apmd/x races for good.

When we hit suspend time, go through all wsdisplays on the system. if
they are in mode MAPPED, but not MODE_DUMBFB then if possible do a full
vt switch to a !mapped vt, and prevent switching back until resume time.
This has to be called from MD code because this involves userland
running so that X can run the vt switch signal handler. This way, any
case where we are using the "poke registers from userland" model, we
will not be on the hardware when we go down, so the kernel can actually
handle thing properly.

Tested on several acpi laptops (by kettenis@ and ian@), x40 (me and
beck@ at LEAST) and zaurus (me). Maybe others, but if so I forgot who at
this time..

Idea from deraadt somewhere over the Faroe Islands (I thought of a
similar thing myself a while ago). Much prompting from him. Ok and
comments miod@


# 1.153 25-Mar-2010 oga

Fix & vs. && in the kqfilter code.

Based on a comparison with the apm code.

ok deraadt@, kettenis@


Revision tags: OPENBSD_4_7_BASE
# 1.152 26-Nov-2009 mlarkin

Visual (and on some machines, audio) feedback while the machine is
resuming via calls to aml _SST method. This also makes the "moon" LED on
thinkpads to blink during resume and cease when resume is completed.

ok pirofti@


# 1.151 26-Nov-2009 deraadt

Toggle into "cold" when we we disable interrupts, because tsleeping
in the AML interpreter and context switching into userland processes
tends to sometimes pop the ACPI trampoline page out of our address
space. Makes lots more machines work.
ok kettenis mlarkin


# 1.150 24-Nov-2009 jsg

in the resume path evaluate _WAK before dealing with device drivers
like the spec says, not after.

ok mlarkin@


# 1.149 24-Nov-2009 deraadt

abort suspends on SMP systems until that gets written; ok mlarkin


# 1.148 23-Nov-2009 mlarkin

pci should return the result of it's children's suspend/resume calls back
to its parent.

handle suspend failure case and unwind if devices have an activate function
that returns failure.

ok deraadt@, kettenis@


# 1.147 23-Nov-2009 mlarkin

acpi* devices need to call their children on suspend/resume.
ok deraadt@


# 1.146 23-Nov-2009 pirofti

Remove ACPI_SLEEP_ENABLED checks.

This enables by default the suspend/resume paths in the kernel.

Okay deraadt@.


# 1.145 23-Nov-2009 deraadt

knf and spacing that are super annoying


# 1.144 23-Nov-2009 mlarkin

Hook up Theo's new children suspend/resume function, as well as more ACPI
suspend/resume glue.

ok deraadt@


# 1.143 26-Oct-2009 deraadt

Do not do strncmp comparisons on dv_xname because longer device names which
look similar could arrive in the future. Instead, compare directly against
dv_cfdata->cf_driver->cd_name
Issue originally spotted by miod


# 1.142 26-Oct-2009 jordan

Verify checksum+address when loading ACPI tables.
Some systems had invalid entries in RSDT/XSDT.
ok marco@


# 1.141 23-Jul-2009 cnst

New aibs(4) driver for ASUSTeK AI Booster (ACPI ATK0110) hardware monitoring,
with sensor state support through limits provided by the ACPI.

Tested on several ASUS motherboards kindly networked by Sam Fourman Jr.

ok deraadt marco jordan


Revision tags: OPENBSD_4_6_BASE
# 1.140 03-Jun-2009 pirofti

Import acpi power resource driver. This is needed for suspend/resume logic.

Okay jordan@ and marco@.


# 1.139 03-Jun-2009 pirofti

Add acpivideo support. This adds brightness support for all laptops
except thinkpads, they will use the acpithinkpad driver. The driver is
also hooked into wsconsole. So brightness can be adjusted via:

$ wsconsctl display.brightness=<percentage>

This is very helpfull on some laptops that have a nasty bios and get two
steps instead of one when pressing the brightness button.

Tested on various dell, fujitsu, acer, samsung and other laptops.

Okay marco@, miod@. Suggestions from kettenis@.
Lots of reviews and help from miod@, thanks!


# 1.138 03-Jun-2009 jordan

Added ejectable bay for IDE cdroms
ok marco@


# 1.137 30-Apr-2009 marco

Oga changed apm ioctls and forgot to grep. Temporary work around by hitting
the sleep button when we get any request. This interface will chnage at
some point.
ok oga


# 1.136 19-Apr-2009 krw

Call acpi_sleep_walk() when powering down just like when entering
any other sleep state. This tells acpi wake devices to not wake up
the box until manual powering up resets their status. Makes my Dell
GX520 stay powered off in response to 'halt -p'.

In snaps for a while.

ok marco@ (several times) deraadt@


# 1.135 17-Apr-2009 pirofti

Add device ranking support for future sleep/wake developments.

okay marco@


# 1.134 11-Mar-2009 jordan

Cleanup.. use aml_evalinteger instead of aml_evalname
ok marco@


# 1.133 10-Mar-2009 jordan

Fix cut/paste error.. call _BFS not _PTS
ok marco@


Revision tags: OPENBSD_4_5_BASE
# 1.132 21-Feb-2009 marco

overzealous cut/paste; found by Vladimir Kirillov <proger@uaoug.org.ua>


# 1.131 20-Feb-2009 marco

Oops didn't mean for Debugger() to remain there.


# 1.130 19-Feb-2009 marco

suspend/resume bits so that we can develop this in tree. This is disabled.
code from mlarkin and me
help from art,toby,jordan and several others
ok jordan, go for it deraadt


# 1.129 10-Feb-2009 jordan

Fix null pointer for shutdown if acpi not enabled
ok marco@


# 1.128 04-Feb-2009 kettenis

Some systems that claim to be ACPI 2.0 apparently don't provide an XSDT
address. Make the code fall back on the RSDT on those systems.

From Joerg Niendorf <p3y80@internode.on.net>. Fixes PR 5986.


# 1.127 17-Jan-2009 krw

A couple of stray malloc/memset -> malloc(M_ZERO), a bit of knf and
lose a couple of unnecessary casts. No functional change.

Feedback from kettenis@

ok kettenis@ marco@


# 1.126 06-Nov-2008 marco

Collapse all sanity checks of hids into one function.

Originaly from jordan


Revision tags: OPENBSD_4_4_BASE
# 1.125 02-Jul-2008 fgsch

add acpivideo. for now almost an empty skel so others can work on
suspend/resume. eventually it will also manage output switching and
brightness where supported. prodded by marco@


# 1.124 11-Jun-2008 marco

Fix $OpenBSD$ for once and for all


# 1.123 08-Jun-2008 deraadt

aaa_dev was being set from an uninitialized variable; ok toby


# 1.122 07-Jun-2008 marco

Kill ACPIVERBOSE. It was never used and is worthless to boot.

comments jmc
ok toby


# 1.121 06-Jun-2008 marco

Shrink acpi some more for floppies and while here re-enable acpi on the
floppies. Fixes crazy sony vaio kettenis has.

tested by deraadt
ok deraadt jordan kettenis


# 1.120 01-Jun-2008 marco

Fix \n nit in SMALL_KERNEL.


# 1.119 01-Jun-2008 marco

Re-enable new parser. Lots of testing by various folks. Fits all media
now too.

ok kettenis deraadt


# 1.118 16-May-2008 dlg

back out jordans new parser.

it didnt have enough oks from the right people, it did break machines
that were previously working, and it bloated the RAMDISKS and install
media beyond the constraints of the media.

requested by deraadt@


# 1.117 14-May-2008 jordan

Adding new ACPI Parser code
Old guts of code still exists, needs to be torn out next
ok marco@
ok brad@


# 1.116 27-Apr-2008 jcs

add acpithinkpad(4), an acpi support driver for ibm/lenovo
thinkpads.

provides functionality that the bios did on older, non-acpi
thinkpads like respond to screen brightness keys and toggle the
bluetooth device on and off.

ok deraadt@ and marco@


# 1.115 24-Apr-2008 jsing

Introduce acpiasus(4), a driver for the ACPI based hotkeys found in many
ASUS laptops (including the ASUS EeePC) - largely based on NetBSD's
asus(4) driver. On the ASUS EeePC this allows us to enable/disable
wireless, change screen brightness and use the volume keys.

ok jsg@, weingart@


# 1.114 12-Apr-2008 kettenis

Get rid of acpi_s5 global variables; simply send SIGUSR2 instead of SUGUSR1
to tell init(8) to power down the machine.

ok krw@


Revision tags: OPENBSD_4_3_BASE
# 1.113 21-Jan-2008 jsg

Check against RESET_REG_SUP in FADT before doing the acpi reset dance
after all, not doing so appears to break more machines than it fixes.

ok marco@


# 1.112 05-Jan-2008 kettenis

Never attach more acpicpu(4) devices than cpu(4) devices. Always attach
the first acpicpu(4) device on non-MULTIPROCESSOR kernels, regardless of its
ACPI processor ID.

ok mikeb@


# 1.111 05-Dec-2007 deraadt

Hang acpi(4) below bios(4) instead of mainbus(4). This lets us move acpi(4)
before pcibios(4), and then.... if acpi(4) attaches, skip pcibios(4) since
it messes the machines up. Fixes claudio's HP dl320 G5, and almost assuredly
others.
ok kettenis


# 1.110 05-Dec-2007 deraadt

repair acpiopen/acpiclose and friends to handle the /dev/apmctl interface,
so that apmd(8) can actually work correctly against acpi(4), and thus,
even do the fancy -C/-A stuff; also tested by jmc


# 1.109 02-Dec-2007 fgsch

fix the trailing space for real now.


# 1.108 02-Dec-2007 fgsch

avoid trailing spaces on output; marco@ ok.


# 1.107 27-Nov-2007 kettenis

Fix off-by-one that made us attach acpicpu0 and acpicpu1 on GENERIC kernels.

ok marco@


# 1.106 25-Nov-2007 jsg

When available and ACPI is enabled use the ACPI reset method
to reboot. This is required for some quirky sis machines
and likely others to reboot.

Tested by many. 'commit early commit often' deraadt@


# 1.105 19-Nov-2007 deraadt

sigh, amd64 still uses that stupid acpi_enabled variable...


# 1.104 19-Nov-2007 deraadt

softc's come initialized to 0, you know...


# 1.103 19-Nov-2007 deraadt

store revision; from jsg


# 1.102 19-Nov-2007 kettenis

Initialize sc_facs earlier.

ok deraadt@, beck@, canacar@


# 1.101 16-Nov-2007 deraadt

fix the bus_space #define nightmare, so that amd64 and i386 are much more
uniform. as a result shared code like acpi needs less #ifdef's
ok marco kettenis


# 1.100 15-Nov-2007 deraadt

remove the #ifdef ACPI_ENABLE. the EC codepath is either safe enough (and
will be improved), or it is unsafe because AML assumes it must be there
ok almost everyone


# 1.99 15-Nov-2007 mikeb

Make acpicpu(4) attach to the existing CPUs only.

Problem reported by form@. Fix idea by kettenis@.
Lots of help from deraadt@. Tested by deraadt@, form@ and me.

ok deraadt marco


# 1.98 14-Nov-2007 deraadt

spacing


# 1.97 12-Nov-2007 deraadt

correct : vs , in attach-time messages


# 1.96 06-Nov-2007 deraadt

all ACPI_ENABLE kernels need acpi_interrupt()


# 1.95 06-Nov-2007 deraadt

move acpi_read_pmreg() and acpi_write_pmreg() out of SMALL_KERNEL so
that acpi ramdisks can compile (obvious solution)


# 1.94 05-Nov-2007 canacar

Establish the acpi interrupt earlier. Fixes problems with some
GENERIC.MP kernels not getting acpi interrupts. This is a
workaround until the real issue with late establishment is fixed.
ok weingart@, beck@, gwk@


# 1.93 03-Nov-2007 jordan

Added support for displaying wakeup devices
ok beck@,weingart@,gwk@


# 1.92 11-Oct-2007 kettenis

When walking the tree yo find _PRT methods, check whether the device is
actually enabled and functioning. Similar to what we do for _INI.

ok weingart@, beck@


# 1.91 08-Oct-2007 krw

More simple memset(,0,) -> M_ZERO changes. In this batch move to
size(*p) as the first malloc() parameter where p is declared locally
and thus easy to check. Add M_ZERO to gpe_table allocation in acpi.c
even though there is no obvious bzero or memset nearby.


# 1.90 13-Sep-2007 mk

Remove unnecessary memset() (see aml_evalnode(9)) and a bit of KNF.

ok toby


# 1.89 13-Sep-2007 weingart

Implement ACPI 6.5.1 spec tree walk for _STA and _INI.
Fixes mk's laptop. No regressions so far (thank you to the
testers).

ok gwk@, mk@, marco@


Revision tags: OPENBSD_4_2_BASE
# 1.88 17-Apr-2007 mk

Now that acpidock no longer hangs machines during boot (at least
according to the reports I've been getting) it should be safe to enable
it again, so remove the #if 0 stuff around it.

ok marco


# 1.87 11-Apr-2007 jordan

Added changes for C-State
ok marco@


# 1.86 26-Mar-2007 marco

Calculate minutes left more accurately.

From giovanni <qgiovanni@gmail.com>


Revision tags: OPENBSD_4_1_BASE
# 1.85 03-Mar-2007 mk

Stop looking for acpidock until we fix that stuff that breaks all pre-60
series ThinkPads with acpidock(4) enabled.


# 1.84 22-Feb-2007 marco

We were getting no interrupts in .MP. There are 2 fixes; one in
acpi_machdep.c and move the interrupt establishment down after ioapic
has been initialized.

ok jordan


# 1.83 21-Feb-2007 kettenis

Make an acpi-enabled GENERIC.MP work on systems that only support PIC mode.

ok marco@


# 1.82 19-Feb-2007 jordan

Added acpi_map_address function for mapping GAS/IO registers
ok marco@


# 1.81 18-Feb-2007 jordan

Added dynamic perfect hash for opcodes
ok marco@


# 1.80 18-Feb-2007 marco

Shave off another K pre compression.
Shuffle SMALL_KERNEL functions around so that we only have a single #ifndef


# 1.79 17-Feb-2007 marco

Add SMALL_KERNEL throught acpi to make it fit on boot media.
ok deraadt


# 1.78 13-Feb-2007 marco

Add smarter _INI code. This helps with some dissapearing devices.

ok kettenis jordan


# 1.77 31-Jan-2007 gwk

Introduce acpi_hasprocfvs thats "ACPI Has Processor Frequency and Voltage
Scalling" a global flag set by acicpu attach to indicate that ACPI is
handling fvs through the _PSS objects. This will fix disappearing
EST/powernow when booting kernels with acpi enabled, as acpicpu isnt
compiled into GENERIC yet. Also in cases where acpicpu is built in and
legacy EST/powernow work but acpi is missing the _PSS object, the legacy
routines will be used.

tested by dim@, and Nick Nauwelaerts <nick AT nauwelaerts.net>
ok marco@, dim@


# 1.76 22-Jan-2007 mk

Actually look for docking stations by looking for _DCK nodes.

ok marco


# 1.75 02-Jan-2007 marco

On some systems EC must be attached as the first device. Move EC from
generic _HID devices to its own routine to force first attach. Bug found
and debugged by joris@

Code mostly written by joris@


# 1.74 26-Dec-2006 marco

Add polling to devices that require it such as acpibat and acpitz.
Use the same tsleep/wakeup mechanism as the interrupt code to keep it non-
concurrent.


# 1.73 21-Dec-2006 deraadt

even more knf love


# 1.72 21-Dec-2006 marco

Remove some leftover acpiec gpe/interrupt code.


# 1.71 20-Dec-2006 deraadt

oops, name already used


# 1.70 20-Dec-2006 deraadt

rename __acpi_enable_gpe(); ok marco


# 1.69 20-Dec-2006 deraadt

acpi_unmap_pmregs() is not used, ok marco


# 1.68 19-Dec-2006 deraadt

simplify some low-level methods substantially (shrinking code); ok marco jordan


# 1.67 06-Dec-2006 mk

malloc(..., M_NOWAIT) with no NULL check is bad. If allocation fails,
complain, clean up, and bail out of attach.

input and ok gwk


# 1.66 28-Nov-2006 jordan

Fixed clearing arg for acpi_init_pic
ok kettenis@


# 1.65 27-Nov-2006 kettenis

Don't spam dmesg.


# 1.64 27-Nov-2006 jordan

Fixes for acpiprt routing
Post-parse fixup for namerefs
ok kettenis@


# 1.63 25-Nov-2006 marco

Don't handle acpiev events inside the gpe handler since this causes deadlocks.

ok jordan@


# 1.62 15-Nov-2006 kettenis

Hook up acpi apic interrupt routing support to generic acpi code.

ok jordan@


# 1.61 02-Nov-2006 marco

Kill acpiec attach function and add its functionality to the generic _HID
device attach

prodded and ok jordan


# 1.60 02-Nov-2006 marco

Fix comments to reflect reality.


# 1.59 19-Oct-2006 marco

Replace ACPI ioctls with APM ioctls. This makes ACPI talk the same protocol
as APM therefore userspace can now retrieve power information in the same
fashion as all other apm arches (zaurus, macppc, i386 etc).

To use force the use of acpi device like so: apmd -f /dev/acpi

ok jordan@


# 1.58 19-Oct-2006 jordan

Fixed GPE debug levels
Fixed ref counter on temporary bufferfields
DSDT parsing fully working on IBM Thinkpad
ok marco@


# 1.57 15-Oct-2006 jordan

Removed old GPE handler code remnants
ok marco@


# 1.56 12-Oct-2006 jordan

Massive ACPI changes
Fixed ACPI leaks
new ACPI API
ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.55 30-Jun-2006 gwk

KNF, No functional change,
"go at it" marco@


# 1.54 30-Jun-2006 jordan

Support for calling Embedded Controller via new GPE interface
ok marco@


# 1.53 30-Jun-2006 jordan

Added new code for GPE handling
Also works with ACPIEC; need to fixup acpiec.c to remove original interrupt handler
ok marco@


# 1.52 30-Jun-2006 jordan

Added offset argument for acpi read/write pmreg routines (new GPE code)
ok marco@


# 1.51 31-May-2006 todd

do not call acpi_powerdown() unless acpi has successfully initialized
ok gwk@ canacar@


# 1.50 29-May-2006 canacar

Add embedded controller support.
ok marco@ deraadt@ testing & ok gwk@


# 1.49 19-May-2006 canacar

Minimal thermal zone driver. Monitors thermal zone temperature,
shuts down the system if the 'critical temperature' is reached.
ok marco@


# 1.48 11-Apr-2006 marco

KNF.


# 1.47 11-Apr-2006 gwk

Add acpi_resume(struct acpi_softc *sc), which calls some AML methods,
detected at attach time, currently unused.

ok marco@


# 1.46 11-Apr-2006 gwk

Discover some ACPI sleep related methods, and call them at the
appropriate time in acpi_enter_sleep_state().

ok marco@


Revision tags: OPENBSD_3_9_BASE
# 1.45 26-Feb-2006 marco

Add acpicpu as a device.

ok jordan@


# 1.44 23-Feb-2006 marco

remove dead function.

ok jordan@


# 1.43 22-Feb-2006 marco

Print out all tables it found during autoconf. We'll need this data in
the future.


# 1.42 22-Feb-2006 marco

Make dmesg less verbose.

ok jordan.


# 1.41 22-Feb-2006 jordan

Added parsing of SSDT tables
ok marco@


# 1.40 22-Feb-2006 jordan

Added new aml_register to notify by device id
Moved powerdown code to button device
ok marco@


# 1.39 21-Feb-2006 marco

Readjust debug levels that crept in too low over the last few days.
KNF


# 1.38 21-Feb-2006 marco

Rig the power button so that the machine shuts down correctly.


# 1.37 20-Feb-2006 marco

Add acpibtn device. Remains disabled for now.


# 1.36 19-Feb-2006 jordan

Added code for aml_notify GPE notification callback
ok marco@


# 1.35 19-Feb-2006 grange

Fix acpi_enter_sleep_state() by replacing current ugly hack
with mostly correct sleeping state transition code.

ok brad@ marco@


# 1.34 19-Feb-2006 marco

jordan:
Enable interrupts.
Added peripheral code to make this work.

marco:
Replace softintr and other types of interrupts with a handler with context.
Fixed interrupt magic for GPEs.
Some KNF

This is work done by jordan and I but has to be a single commit.


# 1.33 17-Feb-2006 marco

Remove earlier introduced delays. We really only need them during autconf.

ok jordan@


# 1.32 17-Feb-2006 marco

Add a generic delay function as per the ACPI spec.

from jordan@


# 1.31 17-Feb-2006 marco

Use tsleep instead of delay when !cold.


# 1.30 17-Feb-2006 marco

Argh, we need delays between reads/writes. The backend reads (i2c most likely)
are so slow that values come back as 0 sometimes. We have to do this
despite the spec saying we shouldnt have to. Guess having a faster
parser than others can hurt sometimes.

Idea from jordan.


# 1.29 17-Feb-2006 jordan

Fixed code for handling _INI
Rearranged debug statements
ok marco@


# 1.28 16-Feb-2006 jordan

Added stub for calling _INI
Fixed aml_dowhile for NULL return
ok marco@


# 1.27 16-Feb-2006 jordan

Fixed parser/evaluator to retrieve battery info+state
Cleanup of parser
Dell laptops return battery status
ok marco@


# 1.26 06-Feb-2006 brad

fix a typo so this will compile on amd64.


# 1.25 04-Feb-2006 marco

When ACPI_ENABLED wasn't set but the devices were it had an unknown variable.

Pointed out by ian@.


# 1.24 03-Feb-2006 jordan

Rewrote aml parser yet again...
Fixed bus_space_map for i386/amd64
Added _BST and _BIF to acpibat to get status/info
ok marco@


# 1.23 20-Jan-2006 jordan

More evaluator changes
Removed old evaluator code
Fixed _HID value
ok marco@


# 1.22 19-Jan-2006 jordan

Added support for read/write of PCI Config space
Help jason@
ok marco@


# 1.21 18-Jan-2006 jordan

Added new evaluation routines
Added helper methods for buffer/field evaluation
Fixed broken indentation on dsdt.c

ok marco@


# 1.20 17-Jan-2006 jordan

Major overhaul of the aml parser. Evaluater temporarily disabled.

ok marco@


# 1.19 06-Jan-2006 grange

Check for ACPI control availability only if ACPI_ENABLE defined.


# 1.18 06-Jan-2006 grange

Informative error messages on attach, prettier dmesg.


# 1.17 06-Jan-2006 grange

printf -> dnprintf conversion for debug messages.


# 1.16 05-Jan-2006 grange

Find SLP_TYPx values and store them for future use in
acpi_enter_sleep_state().


# 1.15 05-Jan-2006 grange

Random KNF and cleanup


# 1.14 05-Jan-2006 grange

Provide symbolic names for the acpi sleeping states and use it.


# 1.13 28-Dec-2005 marco

Moved DEVNAME to a acpidev.h

ok jordan@


# 1.12 18-Dec-2005 sturm

add back comment about wrong code

ok marco


# 1.11 16-Dec-2005 marco

Remove test for kbd_reset. Prompted deraadt@, ok jordan@


# 1.10 16-Dec-2005 jordan

Added hooks for powerdown/power button press


# 1.9 16-Dec-2005 jordan

Cleaned up common power management register i/o
Added enter sleep state ioctl
Hooked power button interrupt
ok marco@


# 1.8 16-Dec-2005 marco

Remove autoconf chatter


# 1.7 16-Dec-2005 jordan

Moved DSDT AML parsing into parent ACPI driver
ok marco@


# 1.6 14-Dec-2005 marco

Add AC device glue.


# 1.5 14-Dec-2005 marco

Adjust debug printing levels to something more sane.


# 1.4 13-Dec-2005 marco

Add glue to attach devices to apci.


# 1.3 07-Dec-2005 marco

KNF


Revision tags: OPENBSD_3_8_BASE
# 1.2 10-Jul-2005 grange

Spacing cleanup.


# 1.1 02-Jun-2005 tholo

Start on a basic ACPI framework -- does not do much more than read out the
ACPI tables into kernel memory and attach ACPI and HPET timers currently.

In order to test this code, enabling the devices in GENERIC as well as
the ACPI_ENABLE option is needed. This code does not do any thermal
control yet, so this should be done with care depending on the platform.

In the tree so more people can contribute to making this more fully
featured.

Ok niklas@ grange@ tedu@


# 1.376 31-Dec-2019 mpi

Convert infinite sleeps to {m,t}sleep_nsec(9).

ok kn@


# 1.375 31-Dec-2019 jsg

include sys/sensors.h for sensor use
don't depend on indirect inclusion from acpidev.h


Revision tags: OPENBSD_6_6_BASE
# 1.374 07-Sep-2019 kettenis

Revert acpipci(4) changes. Too many systems are broken. We'll try again
after 6.6 as been released. The acpireg.h change stays behind.


# 1.373 28-Aug-2019 kettenis

Use ACPI information to attach PCI busses like we do on arm64. There are a
few additional quirks though, and attaching the PCI busses is delayed to
replicate the existing code more closely. That may be changed in the
future. Also tweak how we handle MSI support and respect to ACPI flag
that says we shouldn't attempt to use MSIs.

Some fallout is expected.

ok patrick@


# 1.372 27-Aug-2019 deraadt

On amd64 the bootup and resume paths for SP/BP/SP were not performing
msr/pat, clock-startup, firmware upgrade, fpu initialization, and cpu
soft-state initialization in the same order. That kind of chaos is
crazy and fragile. Improve the acpi resume call sequence and unify order
of the operations.


# 1.371 02-Jul-2019 jcs

acpi_set_gpehandler: turn edge arg into flags, add support for
passing GPE_DIRECT which directly executes the supplied callback and
clears a level interrupt rather than going through acpi_taskq which
can be slow when other driver callbacks are in the queue.

This should not be used for GPE handlers that need to do anything
ACPI-related.

ok kettenis


# 1.370 10-Jun-2019 kettenis

Print proper ACPI version number.

ok mlarkin@


# 1.369 08-Jun-2019 kettenis

When establishing the mapping between ACPI device nodes and PCI devices,
skip PCI host bridges and devices that aren't there.

ok patrick@


# 1.368 07-Jun-2019 kettenis

Introduce acpi_getsta() and use it to eliminate some duplicated code.

ok patrick@


# 1.367 12-May-2019 kettenis

Make sure the extended PM register descriptions actually exist before
using them. Fixes machines with ancient ACPI 1.0 (such as recent
hypervisors).

ok deraadt@


# 1.366 12-May-2019 kettenis

Add support for mapping ACPI PM registers using the "extended" gasio
representation from the FADT. Mostly fixes the Lanner NCA-1510.

ok lteo@, mlarkin@, deraadt@


# 1.365 11-May-2019 lteo

Fix incorrect error message.

ok kettenis@


# 1.364 08-May-2019 tedu

free size. the allocation is the header plus the length.


Revision tags: OPENBSD_6_5_BASE
# 1.363 04-Apr-2019 kettenis

Avoid potential null-pointer dereference. Found by Kent R. Spillner.

ok deraadt@, jsg@


# 1.362 20-Jan-2019 jsg

fix SMALL_KERNEL build after changes in rev 1.361
reported by naddy@ ok deraadt@


# 1.361 19-Jan-2019 tedu

Add a pwraction sysctl that controls what the power button does on acpi.
By default, nothing changes -- shutdown is initiated. But allows turning
power button into a sleep button if desired.
(grudging) ok from a few parties


# 1.360 26-Oct-2018 kettenis

Add acpipci(4) on amd64. For now this only calls the PCI-specific _OSC
method to let the ACPI implementation know what features we support.


Revision tags: OPENBSD_6_4_BASE
# 1.359 30-Aug-2018 kettenis

Move softbutton check before the check that skips attaching certain acpi
devices. Fixes regression caused by matching on _CID in addition to
matching on _HID.

ok matthieu@, mlarkin@


# 1.358 25-Aug-2018 kettenis

Match ACPI devices based on _CID if no match for _HID is found.

ok mpi@


# 1.357 19-Aug-2018 kettenis

Add support for multiple PCI segments. Only really implemented for arm64
for now as amd64/i386 firmware still caters for legacy OSes that only
support a single PCI segment.

ok patrick@


# 1.356 03-Aug-2018 kettenis

Let ahci(4) match on _CLS instead of _HID when attaching at acpi(4). Avoids
having to add many more _HID entries to the match table.

ok deraadt@, mlarkin@


# 1.355 10-Jul-2018 kettenis

Make legacy interrupts work in acpipci(4).

ok patrick@


# 1.354 05-Jul-2018 kettenis

Add acpipci(4), a driver that supports generic ECAM-compatible PCI host
bridges based on information provided by ACPI.

ok mlarkin@


# 1.353 04-Jul-2018 kettenis

Properly pass around the PCI "chipset tag" in acpi(4) and refactor
acpimcfg(4) to call an MD initialization functions that sets up a tag for
PCI ECAM.

ok guenther@, mlarkin@, krw@


# 1.352 01-Jul-2018 kettenis

Move acpi(4) attach glue into acpi_machdep.c.

ok guenther@, deraadt@


# 1.351 01-Jul-2018 kettenis

Add support for _DSD Device Properties.

ok mlarkin@


# 1.350 30-Jun-2018 kettenis

Add intr_enable() function, intended for MI use to amd64 and i386 and use
this in the acpi(4) suspend/resume code paths.

ok deraadt@


# 1.349 29-Jun-2018 kettenis

Use <stdint.h> types.

ok mpi@, jung@, krw@, deraadt@


# 1.348 29-Jun-2018 mlarkin

ACPI: Allow (with warning) GPE handler reassignment, instead of returning
a failure code.

ok kettenis


# 1.347 26-Jun-2018 mlarkin

knf


# 1.346 26-Jun-2018 kettenis

Let acpi(4) pass down the bus dma tag.

ok deraadt@


# 1.345 25-Jun-2018 kettenis

Reorganize acpi(4) code a bit in preparation for upcoming arm64 support.

ok deraadt@


# 1.344 20-May-2018 kettenis

Wake up the acpi thread after scheduling a gpio event.

ok mpi@


# 1.343 17-May-2018 kettenis

Sprinkle some #ifndef SMALL_KERNEL such that RAMDISK kernels cuild again.

pointed out by stsp@


# 1.342 17-May-2018 kettenis

Implement GenericSerialBus OpRegion support.

ok mlarkin@


# 1.341 27-Mar-2018 kettenis

Add acpicmos(4), a driver that implements SystemCMOS OperationRegion
access support. This fixes machines where the AML doesn't check whether
support for this OperationRegion type has been registered by the OS.

ok mlarkin@


Revision tags: OPENBSD_6_3_BASE
# 1.340 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.339 10-Feb-2018 deraadt

Syncronize filesystems to disk when suspending. Each mountpoint's vnodes
are pushed to disk. Dangling vnodes (unlinked files still in use) and
vnodes undergoing change by long-running syscalls are identified -- and
such filesystems are marked dirty on-disk while we are suspended (in case
power is lost, a fsck will be required). Filesystems without dangling or
busy vnodes are marked clean, resulting in faster boots following
"battery died" circumstances.
Tested by numerous developers, thanks for the feedback.


# 1.338 08-Feb-2018 deraadt

After flushing disks, try again to release all memory. We don't need to
save dirty memory to the hibernate space.


# 1.337 08-Feb-2018 deraadt

Toss all releaseable memory, because fragmentation can get in the way
of allocating the hibernate playpen.


# 1.336 08-Feb-2018 deraadt

sr_quiesce() is a new approach for ensuring that softraid drains
output to the disks.

This is part of a larger suspend/resume filesystem-safety diff, which
has been worked on for a couple of months already. Tests by job, krw,
beck, benno, and others. Sometimes even by snapshot users...


# 1.335 29-Nov-2017 kettenis

Add support for GPIO-signaled events.

ok mlarkin@


# 1.334 16-Nov-2017 jcs

Add PCI attachment for dwiic(4) needed by Intel 100 Series machines

ok kettenis


# 1.333 14-Nov-2017 kettenis

Add a few more PNP IDs to the skip lists.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.332 17-Aug-2017 stsp

Make dwiic(4) attach its ACPI parent dependencies.
This change makes the Asus E200HA keyboard work.

Original analysis and patch by Cesare Gargano
ok kettenis@


# 1.331 09-Aug-2017 dcoppa

Add "PNP0303" (8042 PS/2 Controller) to acpi_isa_hids[]

ok kettenis@


# 1.330 22-Jul-2017 jcs

acpibat: look for _BIX first before falling back on _BIF

ACPI 4.0 deprecated _BIF for battery status so some newer machines
are exporting _BIX which extends _BIF's package with a few extra
fields. When using _BIX, export the new discharge cycle count as a
sensor.

feedback from kettenis and anton
ok deraadt


# 1.329 20-Jul-2017 kettenis

Start a list of isa(4) device names such that we can avoid printing them
as "not configured".

ok mikeb@, millert@, deraadt@


# 1.328 08-Apr-2017 deraadt

Sensors are run as callbacks inside tasks. During suspend a sensor
could be running inside a driver that will be force-detached, or due
to tsleep end up disrupting the softstate/hardstate contract. At
suspend time, quisce all these callbacks by waiting for completion.
This issue has never been observed for real, but may be implicated in
suspend/resume failures.
ok kettenis guenther mlarkin


# 1.327 08-Apr-2017 deraadt

Two prototypes for acpi_maptable; merge to one place


Revision tags: OPENBSD_6_1_BASE
# 1.326 27-Mar-2017 deraadt

Now that hibernate_alloc() only has clean success/failure, don't
need to call hibernate_free() to clean up a partial mess.
ok mlarkin kettenis


# 1.325 13-Mar-2017 deraadt

allow ramdisks to build; ok jcs


# 1.324 12-Mar-2017 jcs

try to attach acpisbs first and if successful, don't attach acpibat


# 1.323 02-Mar-2017 natano

Add a new sysctl machdep.lidaction. The sysctl works as follows:

machdep.lidaction=0 # do nothing
machdep.lidaction=1 # suspend
machdep.lidaction=2 # hibernate

lidsuspend is just an alias for lidaction, so if you change one, the
other one will have the same value. The plan is to remove
machdep.lidsuspend eventually when people have upgraded their
/ets/sysctl.conf.

discussed with deraadt, who came up with the new MIB name
no objections mlarkin
ok stsp halex jcs


# 1.322 28-Feb-2017 natano

Untangle abstract sleep modes from ACPI sleep states. This paves the way
for further work in the area.

idea and ok deraadt
ok mlarkin


# 1.321 22-Feb-2017 jcs

add acpisbs, an acpi smart battery subsystem driver reading data
over smbus

currently disabled because it conflicts with acpibat

ok deraadt, kettenis


# 1.320 14-Jan-2017 kettenis

Revert aml_rdpciaddr changes; breaks several machines that were working
before.


# 1.319 11-Jan-2017 jsg

Increase the size of the buffers used for _HID and _CID strings to be
able to fit "Hyper_V_Gen_Counter_V1".

ok mlarkin@ deraadt@


# 1.318 08-Jan-2017 kettenis

Make aml_rdpciaddr() work for for devices behind bridges. The code now
uses the mapping between AML nodes and PCI devices that we establish.
Because _INIT methods may end up calling aml_rdpciaddr(), make sure we
create that mapping early. Also handle devices that aren't actually present.
These devices are now included in the mapping and reads will return an
all-ones patterm whereas writes are a no-op..

ok mlarkin@


# 1.317 25-Oct-2016 pirofti

Add the resource index to aml_parse_resource() callback function.

This is needed by an upcoming acpiec commit that handles machines breaking
the current ACPI specifications.

Change suggested by and ok kettenis@, guenther@


# 1.316 18-Sep-2016 guenther

If the APIC table (MADT) isn't present then ci_acpi_proc_id won't be set
when acpicpu tries to attach, leading to a NULL dereference. The ACPI
processor object on the Dells where this was reported is bogus and useless
anyway so apply the same test when deciding whether to attach to processor
objects as acpicpu would when matching them to cpus.

Problem reported by Eivind Eide (xenofil (at) gmail.com) and
Johan Huldtgren (johan+openbsd-bugs (at) huldtgren.com).
Testing by Eivind Eide.
ok kettenis@ mlarkin@


# 1.315 03-Sep-2016 naddy

Write the system time back to the RTC every 30 minutes.
This fixes the problem that long-running machines which were not
shut down properly would reboot with a badly offset system time.

hints and ok kettenis@


# 1.314 31-Aug-2016 mlarkin

Don't put a device in the wake device list if _STA says it's not there.

Fixes at least one machine that woke up from sleep / poweroff immediately.

ok kettenis@, tested by kettenis and martijn@


# 1.313 28-Jul-2016 kettenis

Store the acpi processor ID/UID in struct cpu_info, and use it to attach
acpicpu(4) drivers to the right cpu(4).

ok mlarkin@, guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.312 10-Jun-2016 kettenis

The power management timer has been made optional in ACPI 5.0A, so don't
attempt to attach acpitimer(4) if the timer isn't present.

Pointed out by jcs@

ok jcs@


# 1.311 08-May-2016 kettenis

Make sure devices listed by a device's _DEP method are attached before
we attach the device itself. The _DEP methode, introduced in ACPI 5.0,
is a hint that the device in question depends on OpRegion support from
the devices listed by _DEP, so we have to order them the proper way.
To prevent us from attaching those devices again when we encounter them
later walking down the device tree, keep track of our attempts to
attach a device driver to them using aflag in the aml_node structure.

ok guenther@


# 1.310 25-Apr-2016 pirofti

We attach acpihpet(4) and acpiec(4) without using the HID, so do not
report it in the dmesg.

OK deraadt@, mlarkin@.


# 1.309 02-Apr-2016 kettenis

Reduce dmesg spam by knocking out some acpi hardware IDs that don't need a
driver.

ok mpi@


# 1.308 29-Mar-2016 kettenis

Check if a device is present (using _STA) in acpi_foundhid. This prevents
us from reporting devices as "not configured" that aren't actually present,
and allows us to remove duplicated code from several drivers.

ok deraadt@, ok jsg@


# 1.307 29-Mar-2016 kettenis

Implement a cleaner way to prevent acpivout(4) from attaching if we have
a platform-specific method of controlling the backlight.

ok millert@, mlarkin@, jung@


# 1.306 28-Mar-2016 kettenis

Adding each and every new acpi device driver to acpi_foundhid() is
gettinga bit of the burden. And it really isn't how our autoconf
framework is supposed to work for busses that can be enumerated. All
the drivers already check for a matching _HID in their attach
function. So we can just drop the checks in acpi_foundhid() and let
the drivers sort themselves out.

Print a "not configured" line for devices for which we don't attach adriver.
This may be a bit much as there are devices for which we will not have a
driver. This will be removed if it causes too much dmesg spam.

ok deraadt@, mlarkin@


Revision tags: OPENBSD_5_9_BASE
# 1.305 17-Jan-2016 jsg

Take acpi_foundhid() outside of #ifndef SMALL_KERNEL so the ramdisk
kernel can find and attach dwiic(4) and sdhc(4).


# 1.304 17-Jan-2016 jsg

remove an unused global variable


# 1.303 14-Jan-2016 kettenis

Attach dwiic(4) on the Intel Bay Trail i2c controllers as well.


# 1.302 12-Jan-2016 kettenis

Improve emulation of PM registers on Hardware-redcued ACPI by emulating
SPL_TYPx, SLP_EN and WAK_STS using the new SLEEP_STATUS_REG and
SLEEP_CONTROL_REG registers. Makes the transition into S5 (powerdown) work
on the ASUS X205TA.

ok deraadt@, mlarkin@


# 1.301 12-Jan-2016 jcs

Add dwiic, a driver for the Synopsys DesignWare i2c controller found
on the Samsung ATIV Book 9 laptop. This initial version only
supports ACPI config/attachment.

Add ihidev, a HID-over-i2c driver largely based on uhidev. dwiic
handles attaching ihidev devices found in ACPI.

Add ims, a HID-over-i2c mouse/trackpad driver to get basic cursor
and button functionality from HID-compliant i2c trackpads.

ok kettenis deraadt


# 1.300 11-Jan-2016 kettenis

Make sdhc(4) attach to hardware IDs "80860F14" and "PNP0FFF".


# 1.299 09-Jan-2016 kettenis

Initial support for Hardware-Reduced ACPI. This skips the legacy mode checks
by emulating the PM_CNT register such that SCI_EN is always enabled. It also
skip attaching acpitimer(4) since the relevant registers are no longer there.

ok deraadt@, mlarkin@


# 1.298 06-Jan-2016 kettenis

Follow the spec and check the SCI_EN bit to see if ACPI has already
been enabled and don't bother enabling it again if it is. This should fix
systems that don't implement legacy mode and therefore set ACPI_ENABLE and
ACPI_DISABLE to zero.

ok mlarkin@, yasuoka@


# 1.297 23-Nov-2015 reyk

Back out the CRITICAL change, turn it back from 10% to 25%. Some
people with old/broken batteries didn't like this change, and the
underlying heuristics are wrong.


# 1.296 22-Nov-2015 reyk

The "CRITICAL" APM battery level, that is exported/faked by the ACPI
stack, is currently set to 25%. With 25% battery left, a typical
modern laptop will have at least one hour runtime left. It is an
arbitrary value, and other APM power layers in the kernel set CRITICAL
to something like 5% or 10%, so set it to 10% for now.

OK mlarkin@


# 1.295 28-Sep-2015 deraadt

In low-level suspend routines, set cold=2. In tsleep(), use this to
spit out a ddb trace to console. This should allow us to find suspend
or resume routines which break the rules. It depends on the console
output function being non-sleeping.... but that's another codepath which
should try to be safe when cold is set.
ok kettenis


# 1.294 06-Sep-2015 deraadt

clarify malloc arguments (base + length...)


# 1.293 28-Aug-2015 deraadt

fairly simple sizes for free(); ok tedu


# 1.292 20-Aug-2015 kettenis

Respect the access size when reading or writing to pci config space.
This version also makes sure writes are properly aligned, preventing
the panics seen with the previous incarnation of this change.

Fixes battery status passthrough in vmware, brightness keys on my x220
and who knows what else.

ok mlarkin@, deraadt@


Revision tags: OPENBSD_5_8_BASE
# 1.291 04-Aug-2015 deraadt

If we are setting ACPI_PM1_BM_RLD do it only once, and remember to
repeat this action at resume time.
ok mlarkin guenther


# 1.290 29-Jul-2015 deraadt

Something subtle in previous revision is locking up suspend or booting
on some laptops. Mailing lists threads mention x220 and Asus machines.
Reverting for now.


# 1.289 21-Jul-2015 kettenis

iRespect the access size when reading or writing to pci config space.
Fixes battery status passthrough in vmware and who knows what else.

Tested by many.

ok mlarkin@, deraadt@


# 1.288 17-Jul-2015 mlarkin

fix comments


# 1.287 26-Mar-2015 jsg

Use the 0x10 extended capability to determine if the trackstick
buttons are wired to the trackpad and need to be re-routed to the
trackstick. Without this change the buttons on 2015 thinkpads
get picked up as extended buttons that show up as scroll up/down.

Remove the X1 Carbon 2015 (LEN0048) and X250 (LEN0046) from the
top button area/soft buttons quirks list. Also avoid using the
quirk list entirely if the capability bit is set.

Based on work from jcs@ with feedback from mpi@
ok mpi@ jcs@


# 1.286 20-Mar-2015 kettenis

The ACPI standard says that a PCI function number of FFFF can be used to refer
to all functions on a device. This makes no sense in the context of accessing
PCI config space. Nevertheless stsp@ has a machine with AML that tries to
do config space reads from a device where _ADR encodes function number FFFF.
This is almost certainly buggy AML, but since Linux (and presumably Windows)
boot just fine on this machine we need to work around this. Linux effectively
treats function FFFF as function 7, which isn't present on this machine.
Instead we simulate a read from a non-existent device by returning a bit
pattern of all ones. Writes to function FFFF will still panic.

ok mlarkin@


# 1.285 16-Mar-2015 deraadt

Reduce use of SMALL_KERNEL in acpi, bringing acpiec driver and more gpe
handling into RAMDISK. This is now possible because the install media
has ample room. The goal is to reduce special cases where we may be
skipping (unknown) important operations...
ok mlarkin kettenis


# 1.284 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.283 07-Feb-2015 deraadt

New framework that allows hibernate to pass in entropy from it's fresh
boot.
ok mlarkin


# 1.282 06-Feb-2015 mlarkin

Fix a hibernate crash on some machines due to unmapping a page that
may not have been mapped previously (in the failure to hibernate case).

Also ensure that the lowmem ptp is mapped in all cases (not just MP).

ok kettenis


# 1.281 17-Jan-2015 deraadt

resume_randomness() before spinning up other cpus...


# 1.280 15-Jan-2015 jsg

Define a new wscons mouse type for Synaptics clickpad devices that lack
physical buttons. This will be used if the acpi pnpid for the mouse
matches a list provided by Synaptics found in Linux.

Combined with the xenocara changes this will remove the need for an xorg
config file for the x240/t440/t540 generation of thinkpads.

ok matthieu@ shadchin@ kettenis@


# 1.279 11-Jan-2015 kettenis

Tolerate checksum errors in ACPI tables (but complain about them). Apparently
there are machines out there with broken BIOSen with such checksum errors in
thier RSDT tables. Windows runs fine on these machines, so it probably does
not check. Linux ignores checksum errors as well (and prints a warning).

ok deraadt@, krw@


# 1.278 18-Dec-2014 deraadt

suspend and resume the RNG (at what looks like the right spots)
ok reyk djm


# 1.277 09-Dec-2014 doug

Replace some malloc(n*size,...) calls with mallocarray().

ok tedu@ deraadt@


# 1.276 06-Dec-2014 mpi

If "machdep.lidsuspend" is not set, do not go back to sleep if the lid
is still closed when resuming. This allows docked laptop to be resumed
without opening them.

ok mlarkin@


# 1.275 23-Nov-2014 mlarkin

If a machine has no _LIDs, then none can be open. This diff fixes a problem
where machines without any _LIDs were going back to sleep after hibernate
due to the recent suspend-if-lid-closed-on-resume diff. Noticed on qemu,
which doesn't have any _LIDs.

discussed with deraadt


# 1.274 18-Nov-2014 krw

Nuke yet more obvious #include duplications.

ok miod@


# 1.273 08-Nov-2014 mlarkin

If resuming from sleep (zzz/ZZZ) and the lid is still closed, go back to
sleep. This prevents accidental lid flex or slight opening in a backpack
from waking the machine up and leaving it resumed (powered on).

ok deraadt@


# 1.272 26-Sep-2014 kettenis

Rework piglet and pig allocation. Currently the piglet gets allocated
deep down in the suspend path, where it is really hard to recover from
allocation failure. So allocate the piglet early on in the suspend path.
Also change the piglet and piglet allocation functions to use km_alloc(9)
instead of doing pmemrange magic. This removes a bunch of code which, in the
case of the piglet allocation, is broken since it results in a NULL pointer
dereference. Also switch the piglet allocation to not wait. If we can't
allocate 16MB of phys contig memory on a halfway modern machine we're almost
certainly under a lot of memory pressure and we're better off not trying to
hibernate anyway.

ok mlarkin@


# 1.271 25-Sep-2014 kettenis

Bring back rev 1.249. Now that mp_setperf() has been fixed, it should be
safe to restore perflevel when resuming. Should fix the issues people
see with the processor running at full speed heating up the machine while
the apm command reports that the CPUs are running at a low speed.

ok dcoppa@, deraadt@


# 1.270 23-Sep-2014 kettenis

Release the acpi lock around calls to wsdisplay_suspend() and
wsdisplay_resume(). The X server may adjust the display brightness just
before/after the VT switch when suspending/resuming. In fact, it may
already be blocked adjusting the display brightness for some reason
unrelated to the suspend request.

The VT switch request will eventually time out, but the delay is annoying
and the failed VT switch will lead to problems after resume.

Needed for a future update of the xf86-video-intel driver.

ok mlarkin@


# 1.269 19-Sep-2014 kettenis

Use config_suspend_all(9).


# 1.268 08-Sep-2014 kettenis

Clear the PME Status bit on PCI devices in response to Device Wake events.
Fixes the interrupt storm on the 2009 Mac mini with WOL enabled on its
nfe(4) interface.

ok mlarkin@


Revision tags: OPENBSD_5_6_BASE
# 1.267 20-Jul-2014 mlarkin

Support hibernating to softraid crypto volumes.

much help and ok from deraadt@


# 1.266 16-Jul-2014 mlarkin

Reenable hibernate RLE support and flush+zero all memory after unpack.


# 1.265 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.264 12-Jul-2014 kettenis

Add a function to drop all clean pages on the page daemon queues and call
it when we hibernate.

ok mlarkin@, miod@, deraadt@


# 1.263 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.262 11-Jul-2014 mlarkin

Flush the buffercache to 16MB on hibernate and restore its previous max
size (kern.bufcachepercent) on resume, for better hibernate performance.

ok beck@


# 1.261 10-Jul-2014 blambert

remove unneeded header

ok mlarkin@


# 1.260 01-Jul-2014 kettenis

Adding an additonal delay after each access to SystemMemory or SystemIO
during autoconf makes no sense.

ok deraadt@


# 1.259 23-Jun-2014 kettenis

SystemMemory is used to access memory mapped registers on some machines, so we
must use the bus_space(9) API to access it instead of memcpy(9). Also make
sure we properly align access to fields that need more than byte alignment.

ok guenther@


# 1.258 21-May-2014 mlarkin

Fix invalid / missing format specifiers in several dnprintf statements
(used only when ACPI_DEBUG is enabled)

From a diff on tech@ by Fabian Raetz, with a minor change by me, tested
on i386 and amd64 with ACPI_DEBUG enabled and disabled.


# 1.257 25-Apr-2014 mlarkin

Defer the free of the hibernate structures until later, when it is safe to
do so.

Freeing the hibernate structures too early leads to inadvertent calls into
splx when we aren't ready for them yet. Found when trapping splx calls in
the resume path.

discussed with deraadt@ at m2k14


# 1.256 13-Apr-2014 mlarkin

Fix a badly worded debug message that implied the entire machine was going
to sleep, instead of just the acpi thread going to sleep.


# 1.255 31-Mar-2014 mpi

Change the order of operations during a suspend/resume cycle and
call bufq_quiesce() after executing the DVACT_QUIESCE handlers.

This should be safe since no disk nor controller drivers have such
handler but it will allow us to detach sd(4) devices attached to a
USB bus.

Another benefit pointed out by kettenis@ is that drivers that need
to read a firmware from the disk should be able to do it at resume
time in a DVACT_WAKEUP handler.

ok kettenis@, deraadt@


# 1.254 13-Mar-2014 dlg

get rid of the assumption that the head of the alldevs list is the
"mainbus" device. this breaks when mpath is enabled because it
attaches before mainbus and therefore takes the head position.

have autoconf provide device_mainbus() which looks up mainbus_cd,
and use that instead.

discussed with deraadt who just wants mpath stuff to move forward
despite there being many ways to shine this particular turd.


Revision tags: OPENBSD_5_5_BASE
# 1.253 21-Feb-2014 deraadt

Do the lid control further on in the resume sequence, since this is
AML, and will do WAITOK operations. While cold, that is a bad idea.
We seem to be safe with respect to the spec, I do not see any event
debounce issues here.
ok kettenis, tested by many


# 1.252 20-Jan-2014 claudio

Instead of checking sc->sc_revision <= 1 check that the
fadt->hdr_revision <= 1. This is a better check since it checks
the version of the proper descriptor. This allows the alix APU
to reboot since those boards have currently a wrong sc->sc_revision.
Committing this now so that it can be tested easier.
With and OK jsg@ also fine by derradt@


# 1.251 24-Dec-2013 kettenis

If the FADT has its SMI_CMD set to zero, assume we're only ACPI-only hardware
and don't need to disable SMI ownership of the ACPI hardware registers.

ok mlarkin@


# 1.250 23-Dec-2013 kettenis

Revert previous commit. It makes resume hang on some MP systems running
GENERIC.MP.


# 1.249 16-Dec-2013 kettenis

Restore hw.setperf upon resume like we do for apm(4) on i386. Seems we need
to do this fairly late in the resume to avoid hangs.

ok deraadt@, mlarkin@


# 1.248 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


# 1.247 06-Nov-2013 mpi

Add support for Power Resources for Dx states and the necessary hook
for PCI devices. This hook should be called twice, before and after
changing the power state of a PCI device.

Before setting the device to the new state, the ACPI layer will notify
every power resources linked to the device for that state and make sure
they are turned "_ON". After changing the state of the device, it will
decrement the reference of every power resources linked to that device
for the old state and turn them "_OFF" if they are no longer referenced.

This fixes the no-USB after resume problem seen on various ThinkPad,
problem initialy diagnosed with Alexander Polakov.

ok kettenis@, deraadt@


Revision tags: OPENBSD_5_4_BASE
# 1.246 01-Jun-2013 mlarkin

Stop printing ACPI wakeup devices in dmesg after the 16th wakeup device.

Helps with VMware VMs that report hundreds of wakeup devices.

ok reyk@, deraadt@


# 1.245 31-May-2013 bluhm

Fix the build for a kernel without wd and pciide in its config.
OK deraadt@ tedu@


# 1.244 30-May-2013 deraadt

Enforce ca_activate tree-walks over the entire heirarchy for all events,
cleaning up some shutdown-hook related code on the way.
(A few drivers related to sparc are still skipped at kettenis' request)
ok kettenis mlarkin, tested by many others too


# 1.243 18-Apr-2013 deraadt

pin the acpi thread to the BSP, because AML and SMI cannot be trusted. I
was certain we were doing this already, but it turns out no...


Revision tags: OPENBSD_5_3_BASE
# 1.242 24-Dec-2012 guenther

In acpiioctl(), Return though the splx() path if suser() returns an error

ok deraadt@


# 1.241 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


# 1.240 04-Oct-2012 ehrhardt

Use information provided by ACPI to attach secondary PCI host bridges.

ok kettenis@


# 1.239 07-Sep-2012 kettenis

Add a new function called acpi_pci_min_powerstate() that returns the lowest
power state for a PCI device for the power state (currently S3 or S4) we're
transitioning into. This function is probably too simplistic; there is a lot
of (silly) complexity in the ACPI spec.

ok deraadt@


Revision tags: OPENBSD_5_2_BASE
# 1.238 13-Jul-2012 pirofti

Disable acpivideo(4) on ASUS laptops.

There are some models that panic whenever an event is received.
Some cope, some don't. In order to get this right acpiec(4) requires
major rework. Until then let machines boot.

Discussed with deraadt@.


# 1.237 11-Jul-2012 mlarkin

for now, restrict hibernate to root in the acpi ioctl kernel interface.
normal rules for controlling this interface through apmd still apply.

ok deraadt@


# 1.236 09-Jul-2012 deraadt

tiny little oops in untested hibernate block


# 1.235 09-Jul-2012 deraadt

clean up ioctl() case statements


# 1.234 09-Jul-2012 deraadt

acpi_disable_onegpe() is not used; ok pirofti


# 1.233 24-May-2012 kettenis

If _MIN > _MAX consider a WordBus resource to be invalid. Fixes a panic on
the IBM x3550-M4.

ok mikeb@


# 1.232 29-Mar-2012 mlarkin

Missing #ifdef in previous diff


# 1.231 29-Mar-2012 mlarkin

Prevent hibernation attempts on machines with unsupported disk controllers.
The same check is also performed later in the hibernate sequence, but by
then it's too late to cleanly unwind (presently).

ok deraadt


# 1.230 26-Mar-2012 deraadt

simplify conditionals in acpi_sleep_state()
ok mlarkin


# 1.229 26-Mar-2012 deraadt

hook in the hibernate request code; half of this diff is from mlarkin
ok mlarkin


Revision tags: OPENBSD_5_1_BASE
# 1.228 20-Sep-2011 deraadt

Very early in suspend while the clock is still ticking realtime,
save the clock back to the rtc. The zaurus already did this.
ok phessler miod kettenis


Revision tags: OPENBSD_5_0_BASE
# 1.227 02-Jul-2011 nicm

kqueue attach functions should return an errno or 0, not a plain 1. Fix
the obvious cases to return EINVAL and ENXIO.

ok tedu deraadt


# 1.226 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.225 16-Jun-2011 pirofti

Import acpitoshiba driver.

This is originally written by Hiroyuki Aizu, ported and integrated
in our tree by Javier Vazquez <javama4 AT gmail> with tweaks and
suggestions by me.

Import prodded by deraadt@ and the lack of testers on tech@.


# 1.224 27-Apr-2011 jcs

attach acpithinkpad to newer lenovo models like the x120e

ok deraadt@


# 1.223 22-Apr-2011 jordan

Fix uninitialzied variables and formatting strings (-Wxxx errors)


Revision tags: OPENBSD_4_9_BASE
# 1.222 02-Jan-2011 jordan

Re-add code for ACPI taskqueue, eliminates need for state vars in acpi softc
Serialize processing ACPI tasks for sleep/powerdown/GPE event
ok deraadt@


# 1.221 31-Oct-2010 guenther

Revert last commit: it breaks resume on Thinkpads
"then please back it out" deraadt@


# 1.220 26-Oct-2010 jordan

Add task queue for ACPI gpe and notify handlers
This fixes eject on Dell Latitude dock and an issue on the Dell Mini
battery update.
ok deraadt


# 1.219 07-Oct-2010 jordan

Create separate functions for enabling wake and runtime gpes.
Remove old suspend/resume gpewalk function
ok deraadt, mlarkin


# 1.218 05-Oct-2010 jordan

split acpi_enable_onegpe to separate enable/disable functions
ok deraadt


# 1.217 05-Oct-2010 jordan

Added acpi_disable_allgpes function to clear GPE status
ok deraadt


# 1.216 05-Oct-2010 deraadt

clean up acpi_softc use more


# 1.215 05-Oct-2010 deraadt

delete a bit of #if 0 code


# 1.214 05-Oct-2010 deraadt

hide use of our global acpi_softc pointer a bit better


# 1.213 29-Sep-2010 deraadt

grab the acpi thread lock very early in acpi thread startup; not that it
is likely that anyone will race us... but why be careless.
ok kettenis


# 1.212 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.211 29-Aug-2010 jasper

- add a check for FWRITE here too.

ok miod@


# 1.210 27-Aug-2010 kettenis

Make PCI Power Management optional, and only enable it when acpi(4) attaches.

ok deraadt@


Revision tags: OPENBSD_4_8_BASE
# 1.209 08-Aug-2010 kettenis

Close race between the acpi thread and wscons ioctls running in process
context. We might rip this out post-release and replace it with a solution
that serializes things in a more obvious way.

ok deraadt@, miod@ and "those crazy texans who ok anything"


# 1.208 08-Aug-2010 deraadt

Report the acpi sleep states when on a RAMDISK; ok krw


# 1.207 07-Aug-2010 kettenis

Enforce acpi register access rescrictions.

ok mlarkin@, deraadt@, tested by marco@


# 1.206 07-Aug-2010 deraadt

upon resume, notify all the acpiac and acpibat drivers to update
their status. some laptops do not do this automatically.
ok kettenis canacar


# 1.205 06-Aug-2010 marco

Always call _PSW on _LID devices that support it to enable lid open to
resume the machine. Conversely disable it when the machine wakes up.

Tested by several ok deraadt


# 1.204 06-Aug-2010 deraadt

As part of a revamp of the PM1/GPE code, I write this diff to look for
unmanaged EN & STS bits in the PM1 register at interrupt time and report
them. As a side effect this splits the STS acknowledgement into two writes
(for power, and sleep) instead of one. The printf that is added (to spot
unmanaged STS bits) has not yet been triggered as far as we know.

Before the "write to PM1 registers at the right offset" diff went in, this
was not neccessary. But newer thinkpads do not have a working soft power
button without this diff.

We have no idea why.
ok mlarkin kettenis


# 1.203 05-Aug-2010 deraadt

PM1 power button events were generating 'fake events' to acpibtn PWRB,
but if there is no such device these were being tossed. Instead, arrange
for all power button handlers to flag a softc variable which is processed
by the thread; so that one place will know to request a powerdown (psignal
to init)
ok marco mlarkin


# 1.202 05-Aug-2010 deraadt

rename a variable which is annoyingly poorly named


# 1.201 05-Aug-2010 mlarkin

Write to the correct offset for various ACPI pmreg registers. Writing to
the correct register does properly clear the wake status bits and fixed
event bits, which allows some machines to have more than 1 suspend cycle.

bug spotted by deraadt@

tested by deraadt@ and myself on a half-dozen machines and others on
hackers@

ok deraadt@, marco@


# 1.200 05-Aug-2010 deraadt

Fix the power button. The pm1 register is 16 bits in size.
My mistake, a while back.
tested by krw


# 1.199 04-Aug-2010 kettenis

Print supported sleep states.

ok marco@, phessler@, deraadt@


# 1.198 03-Aug-2010 mlarkin

Use the proper flag for re-enabling certain hardware events (power btn,
sleep btn). The (incorrect) flag prevoiously used coincidentally had the
same value, so this shouldn't cause any different behavior than before.

tested on a variety of machines (i386, amd64, sp, mp) - no behavioral
change seen

ok deraadt@


# 1.197 28-Jul-2010 marco

Make all gasio failures look and behave the same.


# 1.196 28-Jul-2010 mlarkin

Delay a little longer on suspend before giving up. Fixes machines that
sometimes don't suspend because the sleep transition takes too long
(symptoms like failing to sleep but the backlight stays on).

ok deraadt@


# 1.195 27-Jul-2010 deraadt

remove XXX; EC can now be attached early when needed; ok jordan


# 1.194 27-Jul-2010 deraadt

more brackets feels good


# 1.193 27-Jul-2010 deraadt

XXX annotate a block in acpi_enter_sleep_state() that is pretty worrying


# 1.192 27-Jul-2010 deraadt

horrific indentation that nearly made me puke


# 1.191 27-Jul-2010 mlarkin

Set the sleeping indicator light on machines that support it, in case SMI
doesn't do it for us. Shows the 'moon' LED on older Thinkpads when
sleeping.

Tested on T43p, W500, T510 by me and on X61s by deraadt@.

ok deraadt@


# 1.190 27-Jul-2010 jordan

Early initialization of acpiec if ECDT table exists
Fixes hang when booting thinkpads while docked
ok deraadt


# 1.189 26-Jul-2010 deraadt

remove merge error which was commited


# 1.188 26-Jul-2010 pirofti

Add support for Sony ACPI hotkeys via a new driver: acpisony(4).

Currently it only works for the suspend button and tries to do right for
the brightness events, but I haven't found a Sony laptop that like to do
right so far.

In the future I want to make the brightness keys work on all Sony's and
also add support for the zoom hotkeys and whatever other funky keys I can
find on those things.

Okay deraadt@.


# 1.187 22-Jul-2010 deraadt

pretty large cleanup of SMALL_KERNEL


# 1.186 21-Jul-2010 deraadt

in the acpi GPE handler, late ack level interrupts not edge interrupts.
Duh.


# 1.185 21-Jul-2010 deraadt

Merge the edge and level gpe handlers into one (now possible since the
gpe structure tells us whether the gpe is edge or level)


# 1.184 21-Jul-2010 deraadt

delete #if 0 code we are not going to use; ok marco


# 1.183 20-Jul-2010 deraadt

make the mask code for acpi_enable_onegpe() match other similar code


# 1.182 20-Jul-2010 deraadt

Handle the acpi interrupt controller with proper edge/level handling,
wrapped in spltty to avoid racing against the interrupt controller.
Repair the gpe bit masking code while there, and do operations in the
right order
ok kettenis mlarkin, help from jordan at figuring out the order of
operations.


# 1.181 20-Jul-2010 matthew

When we're preparing to go to sleep, only call bufq_restart() if other
preparations failed.

ok kettenis@, thib@, deraadt@


# 1.180 19-Jul-2010 deraadt

permit compilation with ACPI_DEBUG


# 1.179 19-Jul-2010 deraadt

there is no need for a seperate dsdt_softc pointer; dsdt does not have a
softc but is just a part of the single acpi driver.
ok jordan


# 1.178 19-Jul-2010 deraadt

acpi_delay() is no longer needed; ok jordan


# 1.177 18-Jul-2010 mlarkin

Clear event status on resume, to avoid going back to sleep/reboot/shutdown
immediately after resume. Tested by a bunch of people on hackers@ on
various hardware. Fixes at least Toshiba NB200 and Gateway LT3103u.

ok marco


# 1.176 13-Jul-2010 deraadt

rename some thread related interfaces so they are logical


# 1.175 10-Jul-2010 jordan

Verify _ADR bdf is correct (some systems return 0xffff)
Works on Dell x9xx and Studio 1555


# 1.174 09-Jul-2010 jordan

Clear the noise


# 1.173 08-Jul-2010 jordan

Add mapping for ACPI device to PCI bdf (match autoconf tree)
Simplify resource parsing function to use buffer argument
Convert namespace linked lists to use queue macros
ok marco@, deraadt@


# 1.172 06-Jul-2010 deraadt

Use spltty() to lock downcalls from apm against the information being
modified by the acpi thread.
ok kettenis


# 1.171 06-Jul-2010 deraadt

Don't use a workq to do the suspend, because it races aginst the acpi
thread. Instead, just tell the acpi thread to do the suspend for us.
This makes apmd & zzz work correctly. While here, have acpithinkpad
attempt to post the event to apm before prompting the suspend itself.
ok kettenis marco mlarkin


# 1.170 05-Jul-2010 mlarkin

Reset register SCI_EN on resume. The ACPI spec clearly states we don't have
to do this, but many machines don't, or forget to, which leads to all sorts
of problems on resume (broken battery stats, overheating, failure to resume
more than once, etc).

tested by many, ok marco@, deraadt@


# 1.169 01-Jul-2010 thib

do bufq quiesce again, since vnd/the bufq_done call in
biodone has been fixed.


# 1.168 01-Jul-2010 jordan

Backout recent AML changes, breaks on R210 and others


# 1.167 01-Jul-2010 jordan

Simplified aml_parse_resource call, cleanup code
ok marco


# 1.166 01-Jul-2010 jordan

Fix subordinate bus # for multi-root PCI buses
ok marco, kettenis


# 1.165 30-Jun-2010 thib

Disable/partially backout the bufq quiesce changes as this
is causing havoc with vnds and release must be buildable.


# 1.164 29-Jun-2010 deraadt

Fix ramdisks (new code was placed inside a SMALL_KERNEL chunk)


# 1.163 29-Jun-2010 jordan

Add support for mapping ACPI to PCI devices
ok kettenis, deraadt


# 1.162 29-Jun-2010 kettenis

Call bufq_quiesce() on suspend to stop all I/O and bufq_restart on resume.
Makes suspend/resume work much better while doing disk I/O.

ok mlarkin@


# 1.161 29-Jun-2010 tedu

60 is a more reasonable guess for number of minutes in an hour than 100.
spotted by TeXitoi. ok deraadt


# 1.160 27-Jun-2010 jordan

Add _acpi_matchhids routine, precursor to pci device match


# 1.159 27-Jun-2010 mlarkin

Clear ACPI fixed event status on resume (power buttons/etc) so that some
machines don't immediately resume after suspending to S3.

Tested by kettenis@ and I on 5 or 6 machines, deraadt@ "yeah, looks good"


# 1.158 27-Jun-2010 jordan

Cleaned up acpi_maptable, adds to table list directly


# 1.157 07-Apr-2010 deraadt

If we have done a wsdisplay_suspend(), and ACPI decides it does not want to
suspend, we must roll back by calling wsdisplay_resume() or the virtual
console switching will be locked forever
ok oga


# 1.156 07-Apr-2010 kettenis

Make suspend/resume work on MP machines (running an MP kernel). Joint work
from mlarkin@ and me, with some amd64 fixes thrown in by deraadt@

ok marco@, deraadt@, pirofti@, mlarkin@


# 1.155 31-Mar-2010 kettenis

Bring /dev/apm support in line with apm(4). Make the suspend button send
a suspend request event to apmd(8) instead of suspending immediately. Also
keep track of whether /dev/apm and /dev/apmctl are currently open, such that
we can still suspend immediately if apmd(8) isn't running.

ok deraadt@, marco@, pirofti@, jsing@, oga@


# 1.154 30-Mar-2010 oga

Prevent the apmd/x races for good.

When we hit suspend time, go through all wsdisplays on the system. if
they are in mode MAPPED, but not MODE_DUMBFB then if possible do a full
vt switch to a !mapped vt, and prevent switching back until resume time.
This has to be called from MD code because this involves userland
running so that X can run the vt switch signal handler. This way, any
case where we are using the "poke registers from userland" model, we
will not be on the hardware when we go down, so the kernel can actually
handle thing properly.

Tested on several acpi laptops (by kettenis@ and ian@), x40 (me and
beck@ at LEAST) and zaurus (me). Maybe others, but if so I forgot who at
this time..

Idea from deraadt somewhere over the Faroe Islands (I thought of a
similar thing myself a while ago). Much prompting from him. Ok and
comments miod@


# 1.153 25-Mar-2010 oga

Fix & vs. && in the kqfilter code.

Based on a comparison with the apm code.

ok deraadt@, kettenis@


Revision tags: OPENBSD_4_7_BASE
# 1.152 26-Nov-2009 mlarkin

Visual (and on some machines, audio) feedback while the machine is
resuming via calls to aml _SST method. This also makes the "moon" LED on
thinkpads to blink during resume and cease when resume is completed.

ok pirofti@


# 1.151 26-Nov-2009 deraadt

Toggle into "cold" when we we disable interrupts, because tsleeping
in the AML interpreter and context switching into userland processes
tends to sometimes pop the ACPI trampoline page out of our address
space. Makes lots more machines work.
ok kettenis mlarkin


# 1.150 24-Nov-2009 jsg

in the resume path evaluate _WAK before dealing with device drivers
like the spec says, not after.

ok mlarkin@


# 1.149 24-Nov-2009 deraadt

abort suspends on SMP systems until that gets written; ok mlarkin


# 1.148 23-Nov-2009 mlarkin

pci should return the result of it's children's suspend/resume calls back
to its parent.

handle suspend failure case and unwind if devices have an activate function
that returns failure.

ok deraadt@, kettenis@


# 1.147 23-Nov-2009 mlarkin

acpi* devices need to call their children on suspend/resume.
ok deraadt@


# 1.146 23-Nov-2009 pirofti

Remove ACPI_SLEEP_ENABLED checks.

This enables by default the suspend/resume paths in the kernel.

Okay deraadt@.


# 1.145 23-Nov-2009 deraadt

knf and spacing that are super annoying


# 1.144 23-Nov-2009 mlarkin

Hook up Theo's new children suspend/resume function, as well as more ACPI
suspend/resume glue.

ok deraadt@


# 1.143 26-Oct-2009 deraadt

Do not do strncmp comparisons on dv_xname because longer device names which
look similar could arrive in the future. Instead, compare directly against
dv_cfdata->cf_driver->cd_name
Issue originally spotted by miod


# 1.142 26-Oct-2009 jordan

Verify checksum+address when loading ACPI tables.
Some systems had invalid entries in RSDT/XSDT.
ok marco@


# 1.141 23-Jul-2009 cnst

New aibs(4) driver for ASUSTeK AI Booster (ACPI ATK0110) hardware monitoring,
with sensor state support through limits provided by the ACPI.

Tested on several ASUS motherboards kindly networked by Sam Fourman Jr.

ok deraadt marco jordan


Revision tags: OPENBSD_4_6_BASE
# 1.140 03-Jun-2009 pirofti

Import acpi power resource driver. This is needed for suspend/resume logic.

Okay jordan@ and marco@.


# 1.139 03-Jun-2009 pirofti

Add acpivideo support. This adds brightness support for all laptops
except thinkpads, they will use the acpithinkpad driver. The driver is
also hooked into wsconsole. So brightness can be adjusted via:

$ wsconsctl display.brightness=<percentage>

This is very helpfull on some laptops that have a nasty bios and get two
steps instead of one when pressing the brightness button.

Tested on various dell, fujitsu, acer, samsung and other laptops.

Okay marco@, miod@. Suggestions from kettenis@.
Lots of reviews and help from miod@, thanks!


# 1.138 03-Jun-2009 jordan

Added ejectable bay for IDE cdroms
ok marco@


# 1.137 30-Apr-2009 marco

Oga changed apm ioctls and forgot to grep. Temporary work around by hitting
the sleep button when we get any request. This interface will chnage at
some point.
ok oga


# 1.136 19-Apr-2009 krw

Call acpi_sleep_walk() when powering down just like when entering
any other sleep state. This tells acpi wake devices to not wake up
the box until manual powering up resets their status. Makes my Dell
GX520 stay powered off in response to 'halt -p'.

In snaps for a while.

ok marco@ (several times) deraadt@


# 1.135 17-Apr-2009 pirofti

Add device ranking support for future sleep/wake developments.

okay marco@


# 1.134 11-Mar-2009 jordan

Cleanup.. use aml_evalinteger instead of aml_evalname
ok marco@


# 1.133 10-Mar-2009 jordan

Fix cut/paste error.. call _BFS not _PTS
ok marco@


Revision tags: OPENBSD_4_5_BASE
# 1.132 21-Feb-2009 marco

overzealous cut/paste; found by Vladimir Kirillov <proger@uaoug.org.ua>


# 1.131 20-Feb-2009 marco

Oops didn't mean for Debugger() to remain there.


# 1.130 19-Feb-2009 marco

suspend/resume bits so that we can develop this in tree. This is disabled.
code from mlarkin and me
help from art,toby,jordan and several others
ok jordan, go for it deraadt


# 1.129 10-Feb-2009 jordan

Fix null pointer for shutdown if acpi not enabled
ok marco@


# 1.128 04-Feb-2009 kettenis

Some systems that claim to be ACPI 2.0 apparently don't provide an XSDT
address. Make the code fall back on the RSDT on those systems.

From Joerg Niendorf <p3y80@internode.on.net>. Fixes PR 5986.


# 1.127 17-Jan-2009 krw

A couple of stray malloc/memset -> malloc(M_ZERO), a bit of knf and
lose a couple of unnecessary casts. No functional change.

Feedback from kettenis@

ok kettenis@ marco@


# 1.126 06-Nov-2008 marco

Collapse all sanity checks of hids into one function.

Originaly from jordan


Revision tags: OPENBSD_4_4_BASE
# 1.125 02-Jul-2008 fgsch

add acpivideo. for now almost an empty skel so others can work on
suspend/resume. eventually it will also manage output switching and
brightness where supported. prodded by marco@


# 1.124 11-Jun-2008 marco

Fix $OpenBSD$ for once and for all


# 1.123 08-Jun-2008 deraadt

aaa_dev was being set from an uninitialized variable; ok toby


# 1.122 07-Jun-2008 marco

Kill ACPIVERBOSE. It was never used and is worthless to boot.

comments jmc
ok toby


# 1.121 06-Jun-2008 marco

Shrink acpi some more for floppies and while here re-enable acpi on the
floppies. Fixes crazy sony vaio kettenis has.

tested by deraadt
ok deraadt jordan kettenis


# 1.120 01-Jun-2008 marco

Fix \n nit in SMALL_KERNEL.


# 1.119 01-Jun-2008 marco

Re-enable new parser. Lots of testing by various folks. Fits all media
now too.

ok kettenis deraadt


# 1.118 16-May-2008 dlg

back out jordans new parser.

it didnt have enough oks from the right people, it did break machines
that were previously working, and it bloated the RAMDISKS and install
media beyond the constraints of the media.

requested by deraadt@


# 1.117 14-May-2008 jordan

Adding new ACPI Parser code
Old guts of code still exists, needs to be torn out next
ok marco@
ok brad@


# 1.116 27-Apr-2008 jcs

add acpithinkpad(4), an acpi support driver for ibm/lenovo
thinkpads.

provides functionality that the bios did on older, non-acpi
thinkpads like respond to screen brightness keys and toggle the
bluetooth device on and off.

ok deraadt@ and marco@


# 1.115 24-Apr-2008 jsing

Introduce acpiasus(4), a driver for the ACPI based hotkeys found in many
ASUS laptops (including the ASUS EeePC) - largely based on NetBSD's
asus(4) driver. On the ASUS EeePC this allows us to enable/disable
wireless, change screen brightness and use the volume keys.

ok jsg@, weingart@


# 1.114 12-Apr-2008 kettenis

Get rid of acpi_s5 global variables; simply send SIGUSR2 instead of SUGUSR1
to tell init(8) to power down the machine.

ok krw@


Revision tags: OPENBSD_4_3_BASE
# 1.113 21-Jan-2008 jsg

Check against RESET_REG_SUP in FADT before doing the acpi reset dance
after all, not doing so appears to break more machines than it fixes.

ok marco@


# 1.112 05-Jan-2008 kettenis

Never attach more acpicpu(4) devices than cpu(4) devices. Always attach
the first acpicpu(4) device on non-MULTIPROCESSOR kernels, regardless of its
ACPI processor ID.

ok mikeb@


# 1.111 05-Dec-2007 deraadt

Hang acpi(4) below bios(4) instead of mainbus(4). This lets us move acpi(4)
before pcibios(4), and then.... if acpi(4) attaches, skip pcibios(4) since
it messes the machines up. Fixes claudio's HP dl320 G5, and almost assuredly
others.
ok kettenis


# 1.110 05-Dec-2007 deraadt

repair acpiopen/acpiclose and friends to handle the /dev/apmctl interface,
so that apmd(8) can actually work correctly against acpi(4), and thus,
even do the fancy -C/-A stuff; also tested by jmc


# 1.109 02-Dec-2007 fgsch

fix the trailing space for real now.


# 1.108 02-Dec-2007 fgsch

avoid trailing spaces on output; marco@ ok.


# 1.107 27-Nov-2007 kettenis

Fix off-by-one that made us attach acpicpu0 and acpicpu1 on GENERIC kernels.

ok marco@


# 1.106 25-Nov-2007 jsg

When available and ACPI is enabled use the ACPI reset method
to reboot. This is required for some quirky sis machines
and likely others to reboot.

Tested by many. 'commit early commit often' deraadt@


# 1.105 19-Nov-2007 deraadt

sigh, amd64 still uses that stupid acpi_enabled variable...


# 1.104 19-Nov-2007 deraadt

softc's come initialized to 0, you know...


# 1.103 19-Nov-2007 deraadt

store revision; from jsg


# 1.102 19-Nov-2007 kettenis

Initialize sc_facs earlier.

ok deraadt@, beck@, canacar@


# 1.101 16-Nov-2007 deraadt

fix the bus_space #define nightmare, so that amd64 and i386 are much more
uniform. as a result shared code like acpi needs less #ifdef's
ok marco kettenis


# 1.100 15-Nov-2007 deraadt

remove the #ifdef ACPI_ENABLE. the EC codepath is either safe enough (and
will be improved), or it is unsafe because AML assumes it must be there
ok almost everyone


# 1.99 15-Nov-2007 mikeb

Make acpicpu(4) attach to the existing CPUs only.

Problem reported by form@. Fix idea by kettenis@.
Lots of help from deraadt@. Tested by deraadt@, form@ and me.

ok deraadt marco


# 1.98 14-Nov-2007 deraadt

spacing


# 1.97 12-Nov-2007 deraadt

correct : vs , in attach-time messages


# 1.96 06-Nov-2007 deraadt

all ACPI_ENABLE kernels need acpi_interrupt()


# 1.95 06-Nov-2007 deraadt

move acpi_read_pmreg() and acpi_write_pmreg() out of SMALL_KERNEL so
that acpi ramdisks can compile (obvious solution)


# 1.94 05-Nov-2007 canacar

Establish the acpi interrupt earlier. Fixes problems with some
GENERIC.MP kernels not getting acpi interrupts. This is a
workaround until the real issue with late establishment is fixed.
ok weingart@, beck@, gwk@


# 1.93 03-Nov-2007 jordan

Added support for displaying wakeup devices
ok beck@,weingart@,gwk@


# 1.92 11-Oct-2007 kettenis

When walking the tree yo find _PRT methods, check whether the device is
actually enabled and functioning. Similar to what we do for _INI.

ok weingart@, beck@


# 1.91 08-Oct-2007 krw

More simple memset(,0,) -> M_ZERO changes. In this batch move to
size(*p) as the first malloc() parameter where p is declared locally
and thus easy to check. Add M_ZERO to gpe_table allocation in acpi.c
even though there is no obvious bzero or memset nearby.


# 1.90 13-Sep-2007 mk

Remove unnecessary memset() (see aml_evalnode(9)) and a bit of KNF.

ok toby


# 1.89 13-Sep-2007 weingart

Implement ACPI 6.5.1 spec tree walk for _STA and _INI.
Fixes mk's laptop. No regressions so far (thank you to the
testers).

ok gwk@, mk@, marco@


Revision tags: OPENBSD_4_2_BASE
# 1.88 17-Apr-2007 mk

Now that acpidock no longer hangs machines during boot (at least
according to the reports I've been getting) it should be safe to enable
it again, so remove the #if 0 stuff around it.

ok marco


# 1.87 11-Apr-2007 jordan

Added changes for C-State
ok marco@


# 1.86 26-Mar-2007 marco

Calculate minutes left more accurately.

From giovanni <qgiovanni@gmail.com>


Revision tags: OPENBSD_4_1_BASE
# 1.85 03-Mar-2007 mk

Stop looking for acpidock until we fix that stuff that breaks all pre-60
series ThinkPads with acpidock(4) enabled.


# 1.84 22-Feb-2007 marco

We were getting no interrupts in .MP. There are 2 fixes; one in
acpi_machdep.c and move the interrupt establishment down after ioapic
has been initialized.

ok jordan


# 1.83 21-Feb-2007 kettenis

Make an acpi-enabled GENERIC.MP work on systems that only support PIC mode.

ok marco@


# 1.82 19-Feb-2007 jordan

Added acpi_map_address function for mapping GAS/IO registers
ok marco@


# 1.81 18-Feb-2007 jordan

Added dynamic perfect hash for opcodes
ok marco@


# 1.80 18-Feb-2007 marco

Shave off another K pre compression.
Shuffle SMALL_KERNEL functions around so that we only have a single #ifndef


# 1.79 17-Feb-2007 marco

Add SMALL_KERNEL throught acpi to make it fit on boot media.
ok deraadt


# 1.78 13-Feb-2007 marco

Add smarter _INI code. This helps with some dissapearing devices.

ok kettenis jordan


# 1.77 31-Jan-2007 gwk

Introduce acpi_hasprocfvs thats "ACPI Has Processor Frequency and Voltage
Scalling" a global flag set by acicpu attach to indicate that ACPI is
handling fvs through the _PSS objects. This will fix disappearing
EST/powernow when booting kernels with acpi enabled, as acpicpu isnt
compiled into GENERIC yet. Also in cases where acpicpu is built in and
legacy EST/powernow work but acpi is missing the _PSS object, the legacy
routines will be used.

tested by dim@, and Nick Nauwelaerts <nick AT nauwelaerts.net>
ok marco@, dim@


# 1.76 22-Jan-2007 mk

Actually look for docking stations by looking for _DCK nodes.

ok marco


# 1.75 02-Jan-2007 marco

On some systems EC must be attached as the first device. Move EC from
generic _HID devices to its own routine to force first attach. Bug found
and debugged by joris@

Code mostly written by joris@


# 1.74 26-Dec-2006 marco

Add polling to devices that require it such as acpibat and acpitz.
Use the same tsleep/wakeup mechanism as the interrupt code to keep it non-
concurrent.


# 1.73 21-Dec-2006 deraadt

even more knf love


# 1.72 21-Dec-2006 marco

Remove some leftover acpiec gpe/interrupt code.


# 1.71 20-Dec-2006 deraadt

oops, name already used


# 1.70 20-Dec-2006 deraadt

rename __acpi_enable_gpe(); ok marco


# 1.69 20-Dec-2006 deraadt

acpi_unmap_pmregs() is not used, ok marco


# 1.68 19-Dec-2006 deraadt

simplify some low-level methods substantially (shrinking code); ok marco jordan


# 1.67 06-Dec-2006 mk

malloc(..., M_NOWAIT) with no NULL check is bad. If allocation fails,
complain, clean up, and bail out of attach.

input and ok gwk


# 1.66 28-Nov-2006 jordan

Fixed clearing arg for acpi_init_pic
ok kettenis@


# 1.65 27-Nov-2006 kettenis

Don't spam dmesg.


# 1.64 27-Nov-2006 jordan

Fixes for acpiprt routing
Post-parse fixup for namerefs
ok kettenis@


# 1.63 25-Nov-2006 marco

Don't handle acpiev events inside the gpe handler since this causes deadlocks.

ok jordan@


# 1.62 15-Nov-2006 kettenis

Hook up acpi apic interrupt routing support to generic acpi code.

ok jordan@


# 1.61 02-Nov-2006 marco

Kill acpiec attach function and add its functionality to the generic _HID
device attach

prodded and ok jordan


# 1.60 02-Nov-2006 marco

Fix comments to reflect reality.


# 1.59 19-Oct-2006 marco

Replace ACPI ioctls with APM ioctls. This makes ACPI talk the same protocol
as APM therefore userspace can now retrieve power information in the same
fashion as all other apm arches (zaurus, macppc, i386 etc).

To use force the use of acpi device like so: apmd -f /dev/acpi

ok jordan@


# 1.58 19-Oct-2006 jordan

Fixed GPE debug levels
Fixed ref counter on temporary bufferfields
DSDT parsing fully working on IBM Thinkpad
ok marco@


# 1.57 15-Oct-2006 jordan

Removed old GPE handler code remnants
ok marco@


# 1.56 12-Oct-2006 jordan

Massive ACPI changes
Fixed ACPI leaks
new ACPI API
ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.55 30-Jun-2006 gwk

KNF, No functional change,
"go at it" marco@


# 1.54 30-Jun-2006 jordan

Support for calling Embedded Controller via new GPE interface
ok marco@


# 1.53 30-Jun-2006 jordan

Added new code for GPE handling
Also works with ACPIEC; need to fixup acpiec.c to remove original interrupt handler
ok marco@


# 1.52 30-Jun-2006 jordan

Added offset argument for acpi read/write pmreg routines (new GPE code)
ok marco@


# 1.51 31-May-2006 todd

do not call acpi_powerdown() unless acpi has successfully initialized
ok gwk@ canacar@


# 1.50 29-May-2006 canacar

Add embedded controller support.
ok marco@ deraadt@ testing & ok gwk@


# 1.49 19-May-2006 canacar

Minimal thermal zone driver. Monitors thermal zone temperature,
shuts down the system if the 'critical temperature' is reached.
ok marco@


# 1.48 11-Apr-2006 marco

KNF.


# 1.47 11-Apr-2006 gwk

Add acpi_resume(struct acpi_softc *sc), which calls some AML methods,
detected at attach time, currently unused.

ok marco@


# 1.46 11-Apr-2006 gwk

Discover some ACPI sleep related methods, and call them at the
appropriate time in acpi_enter_sleep_state().

ok marco@


Revision tags: OPENBSD_3_9_BASE
# 1.45 26-Feb-2006 marco

Add acpicpu as a device.

ok jordan@


# 1.44 23-Feb-2006 marco

remove dead function.

ok jordan@


# 1.43 22-Feb-2006 marco

Print out all tables it found during autoconf. We'll need this data in
the future.


# 1.42 22-Feb-2006 marco

Make dmesg less verbose.

ok jordan.


# 1.41 22-Feb-2006 jordan

Added parsing of SSDT tables
ok marco@


# 1.40 22-Feb-2006 jordan

Added new aml_register to notify by device id
Moved powerdown code to button device
ok marco@


# 1.39 21-Feb-2006 marco

Readjust debug levels that crept in too low over the last few days.
KNF


# 1.38 21-Feb-2006 marco

Rig the power button so that the machine shuts down correctly.


# 1.37 20-Feb-2006 marco

Add acpibtn device. Remains disabled for now.


# 1.36 19-Feb-2006 jordan

Added code for aml_notify GPE notification callback
ok marco@


# 1.35 19-Feb-2006 grange

Fix acpi_enter_sleep_state() by replacing current ugly hack
with mostly correct sleeping state transition code.

ok brad@ marco@


# 1.34 19-Feb-2006 marco

jordan:
Enable interrupts.
Added peripheral code to make this work.

marco:
Replace softintr and other types of interrupts with a handler with context.
Fixed interrupt magic for GPEs.
Some KNF

This is work done by jordan and I but has to be a single commit.


# 1.33 17-Feb-2006 marco

Remove earlier introduced delays. We really only need them during autconf.

ok jordan@


# 1.32 17-Feb-2006 marco

Add a generic delay function as per the ACPI spec.

from jordan@


# 1.31 17-Feb-2006 marco

Use tsleep instead of delay when !cold.


# 1.30 17-Feb-2006 marco

Argh, we need delays between reads/writes. The backend reads (i2c most likely)
are so slow that values come back as 0 sometimes. We have to do this
despite the spec saying we shouldnt have to. Guess having a faster
parser than others can hurt sometimes.

Idea from jordan.


# 1.29 17-Feb-2006 jordan

Fixed code for handling _INI
Rearranged debug statements
ok marco@


# 1.28 16-Feb-2006 jordan

Added stub for calling _INI
Fixed aml_dowhile for NULL return
ok marco@


# 1.27 16-Feb-2006 jordan

Fixed parser/evaluator to retrieve battery info+state
Cleanup of parser
Dell laptops return battery status
ok marco@


# 1.26 06-Feb-2006 brad

fix a typo so this will compile on amd64.


# 1.25 04-Feb-2006 marco

When ACPI_ENABLED wasn't set but the devices were it had an unknown variable.

Pointed out by ian@.


# 1.24 03-Feb-2006 jordan

Rewrote aml parser yet again...
Fixed bus_space_map for i386/amd64
Added _BST and _BIF to acpibat to get status/info
ok marco@


# 1.23 20-Jan-2006 jordan

More evaluator changes
Removed old evaluator code
Fixed _HID value
ok marco@


# 1.22 19-Jan-2006 jordan

Added support for read/write of PCI Config space
Help jason@
ok marco@


# 1.21 18-Jan-2006 jordan

Added new evaluation routines
Added helper methods for buffer/field evaluation
Fixed broken indentation on dsdt.c

ok marco@


# 1.20 17-Jan-2006 jordan

Major overhaul of the aml parser. Evaluater temporarily disabled.

ok marco@


# 1.19 06-Jan-2006 grange

Check for ACPI control availability only if ACPI_ENABLE defined.


# 1.18 06-Jan-2006 grange

Informative error messages on attach, prettier dmesg.


# 1.17 06-Jan-2006 grange

printf -> dnprintf conversion for debug messages.


# 1.16 05-Jan-2006 grange

Find SLP_TYPx values and store them for future use in
acpi_enter_sleep_state().


# 1.15 05-Jan-2006 grange

Random KNF and cleanup


# 1.14 05-Jan-2006 grange

Provide symbolic names for the acpi sleeping states and use it.


# 1.13 28-Dec-2005 marco

Moved DEVNAME to a acpidev.h

ok jordan@


# 1.12 18-Dec-2005 sturm

add back comment about wrong code

ok marco


# 1.11 16-Dec-2005 marco

Remove test for kbd_reset. Prompted deraadt@, ok jordan@


# 1.10 16-Dec-2005 jordan

Added hooks for powerdown/power button press


# 1.9 16-Dec-2005 jordan

Cleaned up common power management register i/o
Added enter sleep state ioctl
Hooked power button interrupt
ok marco@


# 1.8 16-Dec-2005 marco

Remove autoconf chatter


# 1.7 16-Dec-2005 jordan

Moved DSDT AML parsing into parent ACPI driver
ok marco@


# 1.6 14-Dec-2005 marco

Add AC device glue.


# 1.5 14-Dec-2005 marco

Adjust debug printing levels to something more sane.


# 1.4 13-Dec-2005 marco

Add glue to attach devices to apci.


# 1.3 07-Dec-2005 marco

KNF


Revision tags: OPENBSD_3_8_BASE
# 1.2 10-Jul-2005 grange

Spacing cleanup.


# 1.1 02-Jun-2005 tholo

Start on a basic ACPI framework -- does not do much more than read out the
ACPI tables into kernel memory and attach ACPI and HPET timers currently.

In order to test this code, enabling the devices in GENERIC as well as
the ACPI_ENABLE option is needed. This code does not do any thermal
control yet, so this should be done with care depending on the platform.

In the tree so more people can contribute to making this more fully
featured.

Ok niklas@ grange@ tedu@


# 1.374 07-Sep-2019 kettenis

Revert acpipci(4) changes. Too many systems are broken. We'll try again
after 6.6 as been released. The acpireg.h change stays behind.


# 1.373 28-Aug-2019 kettenis

Use ACPI information to attach PCI busses like we do on arm64. There are a
few additional quirks though, and attaching the PCI busses is delayed to
replicate the existing code more closely. That may be changed in the
future. Also tweak how we handle MSI support and respect to ACPI flag
that says we shouldn't attempt to use MSIs.

Some fallout is expected.

ok patrick@


# 1.372 27-Aug-2019 deraadt

On amd64 the bootup and resume paths for SP/BP/SP were not performing
msr/pat, clock-startup, firmware upgrade, fpu initialization, and cpu
soft-state initialization in the same order. That kind of chaos is
crazy and fragile. Improve the acpi resume call sequence and unify order
of the operations.


# 1.371 02-Jul-2019 jcs

acpi_set_gpehandler: turn edge arg into flags, add support for
passing GPE_DIRECT which directly executes the supplied callback and
clears a level interrupt rather than going through acpi_taskq which
can be slow when other driver callbacks are in the queue.

This should not be used for GPE handlers that need to do anything
ACPI-related.

ok kettenis


# 1.370 10-Jun-2019 kettenis

Print proper ACPI version number.

ok mlarkin@


# 1.369 08-Jun-2019 kettenis

When establishing the mapping between ACPI device nodes and PCI devices,
skip PCI host bridges and devices that aren't there.

ok patrick@


# 1.368 07-Jun-2019 kettenis

Introduce acpi_getsta() and use it to eliminate some duplicated code.

ok patrick@


# 1.367 12-May-2019 kettenis

Make sure the extended PM register descriptions actually exist before
using them. Fixes machines with ancient ACPI 1.0 (such as recent
hypervisors).

ok deraadt@


# 1.366 12-May-2019 kettenis

Add support for mapping ACPI PM registers using the "extended" gasio
representation from the FADT. Mostly fixes the Lanner NCA-1510.

ok lteo@, mlarkin@, deraadt@


# 1.365 11-May-2019 lteo

Fix incorrect error message.

ok kettenis@


# 1.364 08-May-2019 tedu

free size. the allocation is the header plus the length.


Revision tags: OPENBSD_6_5_BASE
# 1.363 04-Apr-2019 kettenis

Avoid potential null-pointer dereference. Found by Kent R. Spillner.

ok deraadt@, jsg@


# 1.362 20-Jan-2019 jsg

fix SMALL_KERNEL build after changes in rev 1.361
reported by naddy@ ok deraadt@


# 1.361 19-Jan-2019 tedu

Add a pwraction sysctl that controls what the power button does on acpi.
By default, nothing changes -- shutdown is initiated. But allows turning
power button into a sleep button if desired.
(grudging) ok from a few parties


# 1.360 26-Oct-2018 kettenis

Add acpipci(4) on amd64. For now this only calls the PCI-specific _OSC
method to let the ACPI implementation know what features we support.


Revision tags: OPENBSD_6_4_BASE
# 1.359 30-Aug-2018 kettenis

Move softbutton check before the check that skips attaching certain acpi
devices. Fixes regression caused by matching on _CID in addition to
matching on _HID.

ok matthieu@, mlarkin@


# 1.358 25-Aug-2018 kettenis

Match ACPI devices based on _CID if no match for _HID is found.

ok mpi@


# 1.357 19-Aug-2018 kettenis

Add support for multiple PCI segments. Only really implemented for arm64
for now as amd64/i386 firmware still caters for legacy OSes that only
support a single PCI segment.

ok patrick@


# 1.356 03-Aug-2018 kettenis

Let ahci(4) match on _CLS instead of _HID when attaching at acpi(4). Avoids
having to add many more _HID entries to the match table.

ok deraadt@, mlarkin@


# 1.355 10-Jul-2018 kettenis

Make legacy interrupts work in acpipci(4).

ok patrick@


# 1.354 05-Jul-2018 kettenis

Add acpipci(4), a driver that supports generic ECAM-compatible PCI host
bridges based on information provided by ACPI.

ok mlarkin@


# 1.353 04-Jul-2018 kettenis

Properly pass around the PCI "chipset tag" in acpi(4) and refactor
acpimcfg(4) to call an MD initialization functions that sets up a tag for
PCI ECAM.

ok guenther@, mlarkin@, krw@


# 1.352 01-Jul-2018 kettenis

Move acpi(4) attach glue into acpi_machdep.c.

ok guenther@, deraadt@


# 1.351 01-Jul-2018 kettenis

Add support for _DSD Device Properties.

ok mlarkin@


# 1.350 30-Jun-2018 kettenis

Add intr_enable() function, intended for MI use to amd64 and i386 and use
this in the acpi(4) suspend/resume code paths.

ok deraadt@


# 1.349 29-Jun-2018 kettenis

Use <stdint.h> types.

ok mpi@, jung@, krw@, deraadt@


# 1.348 29-Jun-2018 mlarkin

ACPI: Allow (with warning) GPE handler reassignment, instead of returning
a failure code.

ok kettenis


# 1.347 26-Jun-2018 mlarkin

knf


# 1.346 26-Jun-2018 kettenis

Let acpi(4) pass down the bus dma tag.

ok deraadt@


# 1.345 25-Jun-2018 kettenis

Reorganize acpi(4) code a bit in preparation for upcoming arm64 support.

ok deraadt@


# 1.344 20-May-2018 kettenis

Wake up the acpi thread after scheduling a gpio event.

ok mpi@


# 1.343 17-May-2018 kettenis

Sprinkle some #ifndef SMALL_KERNEL such that RAMDISK kernels cuild again.

pointed out by stsp@


# 1.342 17-May-2018 kettenis

Implement GenericSerialBus OpRegion support.

ok mlarkin@


# 1.341 27-Mar-2018 kettenis

Add acpicmos(4), a driver that implements SystemCMOS OperationRegion
access support. This fixes machines where the AML doesn't check whether
support for this OperationRegion type has been registered by the OS.

ok mlarkin@


Revision tags: OPENBSD_6_3_BASE
# 1.340 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.339 10-Feb-2018 deraadt

Syncronize filesystems to disk when suspending. Each mountpoint's vnodes
are pushed to disk. Dangling vnodes (unlinked files still in use) and
vnodes undergoing change by long-running syscalls are identified -- and
such filesystems are marked dirty on-disk while we are suspended (in case
power is lost, a fsck will be required). Filesystems without dangling or
busy vnodes are marked clean, resulting in faster boots following
"battery died" circumstances.
Tested by numerous developers, thanks for the feedback.


# 1.338 08-Feb-2018 deraadt

After flushing disks, try again to release all memory. We don't need to
save dirty memory to the hibernate space.


# 1.337 08-Feb-2018 deraadt

Toss all releaseable memory, because fragmentation can get in the way
of allocating the hibernate playpen.


# 1.336 08-Feb-2018 deraadt

sr_quiesce() is a new approach for ensuring that softraid drains
output to the disks.

This is part of a larger suspend/resume filesystem-safety diff, which
has been worked on for a couple of months already. Tests by job, krw,
beck, benno, and others. Sometimes even by snapshot users...


# 1.335 29-Nov-2017 kettenis

Add support for GPIO-signaled events.

ok mlarkin@


# 1.334 16-Nov-2017 jcs

Add PCI attachment for dwiic(4) needed by Intel 100 Series machines

ok kettenis


# 1.333 14-Nov-2017 kettenis

Add a few more PNP IDs to the skip lists.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.332 17-Aug-2017 stsp

Make dwiic(4) attach its ACPI parent dependencies.
This change makes the Asus E200HA keyboard work.

Original analysis and patch by Cesare Gargano
ok kettenis@


# 1.331 09-Aug-2017 dcoppa

Add "PNP0303" (8042 PS/2 Controller) to acpi_isa_hids[]

ok kettenis@


# 1.330 22-Jul-2017 jcs

acpibat: look for _BIX first before falling back on _BIF

ACPI 4.0 deprecated _BIF for battery status so some newer machines
are exporting _BIX which extends _BIF's package with a few extra
fields. When using _BIX, export the new discharge cycle count as a
sensor.

feedback from kettenis and anton
ok deraadt


# 1.329 20-Jul-2017 kettenis

Start a list of isa(4) device names such that we can avoid printing them
as "not configured".

ok mikeb@, millert@, deraadt@


# 1.328 08-Apr-2017 deraadt

Sensors are run as callbacks inside tasks. During suspend a sensor
could be running inside a driver that will be force-detached, or due
to tsleep end up disrupting the softstate/hardstate contract. At
suspend time, quisce all these callbacks by waiting for completion.
This issue has never been observed for real, but may be implicated in
suspend/resume failures.
ok kettenis guenther mlarkin


# 1.327 08-Apr-2017 deraadt

Two prototypes for acpi_maptable; merge to one place


Revision tags: OPENBSD_6_1_BASE
# 1.326 27-Mar-2017 deraadt

Now that hibernate_alloc() only has clean success/failure, don't
need to call hibernate_free() to clean up a partial mess.
ok mlarkin kettenis


# 1.325 13-Mar-2017 deraadt

allow ramdisks to build; ok jcs


# 1.324 12-Mar-2017 jcs

try to attach acpisbs first and if successful, don't attach acpibat


# 1.323 02-Mar-2017 natano

Add a new sysctl machdep.lidaction. The sysctl works as follows:

machdep.lidaction=0 # do nothing
machdep.lidaction=1 # suspend
machdep.lidaction=2 # hibernate

lidsuspend is just an alias for lidaction, so if you change one, the
other one will have the same value. The plan is to remove
machdep.lidsuspend eventually when people have upgraded their
/ets/sysctl.conf.

discussed with deraadt, who came up with the new MIB name
no objections mlarkin
ok stsp halex jcs


# 1.322 28-Feb-2017 natano

Untangle abstract sleep modes from ACPI sleep states. This paves the way
for further work in the area.

idea and ok deraadt
ok mlarkin


# 1.321 22-Feb-2017 jcs

add acpisbs, an acpi smart battery subsystem driver reading data
over smbus

currently disabled because it conflicts with acpibat

ok deraadt, kettenis


# 1.320 14-Jan-2017 kettenis

Revert aml_rdpciaddr changes; breaks several machines that were working
before.


# 1.319 11-Jan-2017 jsg

Increase the size of the buffers used for _HID and _CID strings to be
able to fit "Hyper_V_Gen_Counter_V1".

ok mlarkin@ deraadt@


# 1.318 08-Jan-2017 kettenis

Make aml_rdpciaddr() work for for devices behind bridges. The code now
uses the mapping between AML nodes and PCI devices that we establish.
Because _INIT methods may end up calling aml_rdpciaddr(), make sure we
create that mapping early. Also handle devices that aren't actually present.
These devices are now included in the mapping and reads will return an
all-ones patterm whereas writes are a no-op..

ok mlarkin@


# 1.317 25-Oct-2016 pirofti

Add the resource index to aml_parse_resource() callback function.

This is needed by an upcoming acpiec commit that handles machines breaking
the current ACPI specifications.

Change suggested by and ok kettenis@, guenther@


# 1.316 18-Sep-2016 guenther

If the APIC table (MADT) isn't present then ci_acpi_proc_id won't be set
when acpicpu tries to attach, leading to a NULL dereference. The ACPI
processor object on the Dells where this was reported is bogus and useless
anyway so apply the same test when deciding whether to attach to processor
objects as acpicpu would when matching them to cpus.

Problem reported by Eivind Eide (xenofil (at) gmail.com) and
Johan Huldtgren (johan+openbsd-bugs (at) huldtgren.com).
Testing by Eivind Eide.
ok kettenis@ mlarkin@


# 1.315 03-Sep-2016 naddy

Write the system time back to the RTC every 30 minutes.
This fixes the problem that long-running machines which were not
shut down properly would reboot with a badly offset system time.

hints and ok kettenis@


# 1.314 31-Aug-2016 mlarkin

Don't put a device in the wake device list if _STA says it's not there.

Fixes at least one machine that woke up from sleep / poweroff immediately.

ok kettenis@, tested by kettenis and martijn@


# 1.313 28-Jul-2016 kettenis

Store the acpi processor ID/UID in struct cpu_info, and use it to attach
acpicpu(4) drivers to the right cpu(4).

ok mlarkin@, guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.312 10-Jun-2016 kettenis

The power management timer has been made optional in ACPI 5.0A, so don't
attempt to attach acpitimer(4) if the timer isn't present.

Pointed out by jcs@

ok jcs@


# 1.311 08-May-2016 kettenis

Make sure devices listed by a device's _DEP method are attached before
we attach the device itself. The _DEP methode, introduced in ACPI 5.0,
is a hint that the device in question depends on OpRegion support from
the devices listed by _DEP, so we have to order them the proper way.
To prevent us from attaching those devices again when we encounter them
later walking down the device tree, keep track of our attempts to
attach a device driver to them using aflag in the aml_node structure.

ok guenther@


# 1.310 25-Apr-2016 pirofti

We attach acpihpet(4) and acpiec(4) without using the HID, so do not
report it in the dmesg.

OK deraadt@, mlarkin@.


# 1.309 02-Apr-2016 kettenis

Reduce dmesg spam by knocking out some acpi hardware IDs that don't need a
driver.

ok mpi@


# 1.308 29-Mar-2016 kettenis

Check if a device is present (using _STA) in acpi_foundhid. This prevents
us from reporting devices as "not configured" that aren't actually present,
and allows us to remove duplicated code from several drivers.

ok deraadt@, ok jsg@


# 1.307 29-Mar-2016 kettenis

Implement a cleaner way to prevent acpivout(4) from attaching if we have
a platform-specific method of controlling the backlight.

ok millert@, mlarkin@, jung@


# 1.306 28-Mar-2016 kettenis

Adding each and every new acpi device driver to acpi_foundhid() is
gettinga bit of the burden. And it really isn't how our autoconf
framework is supposed to work for busses that can be enumerated. All
the drivers already check for a matching _HID in their attach
function. So we can just drop the checks in acpi_foundhid() and let
the drivers sort themselves out.

Print a "not configured" line for devices for which we don't attach adriver.
This may be a bit much as there are devices for which we will not have a
driver. This will be removed if it causes too much dmesg spam.

ok deraadt@, mlarkin@


Revision tags: OPENBSD_5_9_BASE
# 1.305 17-Jan-2016 jsg

Take acpi_foundhid() outside of #ifndef SMALL_KERNEL so the ramdisk
kernel can find and attach dwiic(4) and sdhc(4).


# 1.304 17-Jan-2016 jsg

remove an unused global variable


# 1.303 14-Jan-2016 kettenis

Attach dwiic(4) on the Intel Bay Trail i2c controllers as well.


# 1.302 12-Jan-2016 kettenis

Improve emulation of PM registers on Hardware-redcued ACPI by emulating
SPL_TYPx, SLP_EN and WAK_STS using the new SLEEP_STATUS_REG and
SLEEP_CONTROL_REG registers. Makes the transition into S5 (powerdown) work
on the ASUS X205TA.

ok deraadt@, mlarkin@


# 1.301 12-Jan-2016 jcs

Add dwiic, a driver for the Synopsys DesignWare i2c controller found
on the Samsung ATIV Book 9 laptop. This initial version only
supports ACPI config/attachment.

Add ihidev, a HID-over-i2c driver largely based on uhidev. dwiic
handles attaching ihidev devices found in ACPI.

Add ims, a HID-over-i2c mouse/trackpad driver to get basic cursor
and button functionality from HID-compliant i2c trackpads.

ok kettenis deraadt


# 1.300 11-Jan-2016 kettenis

Make sdhc(4) attach to hardware IDs "80860F14" and "PNP0FFF".


# 1.299 09-Jan-2016 kettenis

Initial support for Hardware-Reduced ACPI. This skips the legacy mode checks
by emulating the PM_CNT register such that SCI_EN is always enabled. It also
skip attaching acpitimer(4) since the relevant registers are no longer there.

ok deraadt@, mlarkin@


# 1.298 06-Jan-2016 kettenis

Follow the spec and check the SCI_EN bit to see if ACPI has already
been enabled and don't bother enabling it again if it is. This should fix
systems that don't implement legacy mode and therefore set ACPI_ENABLE and
ACPI_DISABLE to zero.

ok mlarkin@, yasuoka@


# 1.297 23-Nov-2015 reyk

Back out the CRITICAL change, turn it back from 10% to 25%. Some
people with old/broken batteries didn't like this change, and the
underlying heuristics are wrong.


# 1.296 22-Nov-2015 reyk

The "CRITICAL" APM battery level, that is exported/faked by the ACPI
stack, is currently set to 25%. With 25% battery left, a typical
modern laptop will have at least one hour runtime left. It is an
arbitrary value, and other APM power layers in the kernel set CRITICAL
to something like 5% or 10%, so set it to 10% for now.

OK mlarkin@


# 1.295 28-Sep-2015 deraadt

In low-level suspend routines, set cold=2. In tsleep(), use this to
spit out a ddb trace to console. This should allow us to find suspend
or resume routines which break the rules. It depends on the console
output function being non-sleeping.... but that's another codepath which
should try to be safe when cold is set.
ok kettenis


# 1.294 06-Sep-2015 deraadt

clarify malloc arguments (base + length...)


# 1.293 28-Aug-2015 deraadt

fairly simple sizes for free(); ok tedu


# 1.292 20-Aug-2015 kettenis

Respect the access size when reading or writing to pci config space.
This version also makes sure writes are properly aligned, preventing
the panics seen with the previous incarnation of this change.

Fixes battery status passthrough in vmware, brightness keys on my x220
and who knows what else.

ok mlarkin@, deraadt@


Revision tags: OPENBSD_5_8_BASE
# 1.291 04-Aug-2015 deraadt

If we are setting ACPI_PM1_BM_RLD do it only once, and remember to
repeat this action at resume time.
ok mlarkin guenther


# 1.290 29-Jul-2015 deraadt

Something subtle in previous revision is locking up suspend or booting
on some laptops. Mailing lists threads mention x220 and Asus machines.
Reverting for now.


# 1.289 21-Jul-2015 kettenis

iRespect the access size when reading or writing to pci config space.
Fixes battery status passthrough in vmware and who knows what else.

Tested by many.

ok mlarkin@, deraadt@


# 1.288 17-Jul-2015 mlarkin

fix comments


# 1.287 26-Mar-2015 jsg

Use the 0x10 extended capability to determine if the trackstick
buttons are wired to the trackpad and need to be re-routed to the
trackstick. Without this change the buttons on 2015 thinkpads
get picked up as extended buttons that show up as scroll up/down.

Remove the X1 Carbon 2015 (LEN0048) and X250 (LEN0046) from the
top button area/soft buttons quirks list. Also avoid using the
quirk list entirely if the capability bit is set.

Based on work from jcs@ with feedback from mpi@
ok mpi@ jcs@


# 1.286 20-Mar-2015 kettenis

The ACPI standard says that a PCI function number of FFFF can be used to refer
to all functions on a device. This makes no sense in the context of accessing
PCI config space. Nevertheless stsp@ has a machine with AML that tries to
do config space reads from a device where _ADR encodes function number FFFF.
This is almost certainly buggy AML, but since Linux (and presumably Windows)
boot just fine on this machine we need to work around this. Linux effectively
treats function FFFF as function 7, which isn't present on this machine.
Instead we simulate a read from a non-existent device by returning a bit
pattern of all ones. Writes to function FFFF will still panic.

ok mlarkin@


# 1.285 16-Mar-2015 deraadt

Reduce use of SMALL_KERNEL in acpi, bringing acpiec driver and more gpe
handling into RAMDISK. This is now possible because the install media
has ample room. The goal is to reduce special cases where we may be
skipping (unknown) important operations...
ok mlarkin kettenis


# 1.284 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.283 07-Feb-2015 deraadt

New framework that allows hibernate to pass in entropy from it's fresh
boot.
ok mlarkin


# 1.282 06-Feb-2015 mlarkin

Fix a hibernate crash on some machines due to unmapping a page that
may not have been mapped previously (in the failure to hibernate case).

Also ensure that the lowmem ptp is mapped in all cases (not just MP).

ok kettenis


# 1.281 17-Jan-2015 deraadt

resume_randomness() before spinning up other cpus...


# 1.280 15-Jan-2015 jsg

Define a new wscons mouse type for Synaptics clickpad devices that lack
physical buttons. This will be used if the acpi pnpid for the mouse
matches a list provided by Synaptics found in Linux.

Combined with the xenocara changes this will remove the need for an xorg
config file for the x240/t440/t540 generation of thinkpads.

ok matthieu@ shadchin@ kettenis@


# 1.279 11-Jan-2015 kettenis

Tolerate checksum errors in ACPI tables (but complain about them). Apparently
there are machines out there with broken BIOSen with such checksum errors in
thier RSDT tables. Windows runs fine on these machines, so it probably does
not check. Linux ignores checksum errors as well (and prints a warning).

ok deraadt@, krw@


# 1.278 18-Dec-2014 deraadt

suspend and resume the RNG (at what looks like the right spots)
ok reyk djm


# 1.277 09-Dec-2014 doug

Replace some malloc(n*size,...) calls with mallocarray().

ok tedu@ deraadt@


# 1.276 06-Dec-2014 mpi

If "machdep.lidsuspend" is not set, do not go back to sleep if the lid
is still closed when resuming. This allows docked laptop to be resumed
without opening them.

ok mlarkin@


# 1.275 23-Nov-2014 mlarkin

If a machine has no _LIDs, then none can be open. This diff fixes a problem
where machines without any _LIDs were going back to sleep after hibernate
due to the recent suspend-if-lid-closed-on-resume diff. Noticed on qemu,
which doesn't have any _LIDs.

discussed with deraadt


# 1.274 18-Nov-2014 krw

Nuke yet more obvious #include duplications.

ok miod@


# 1.273 08-Nov-2014 mlarkin

If resuming from sleep (zzz/ZZZ) and the lid is still closed, go back to
sleep. This prevents accidental lid flex or slight opening in a backpack
from waking the machine up and leaving it resumed (powered on).

ok deraadt@


# 1.272 26-Sep-2014 kettenis

Rework piglet and pig allocation. Currently the piglet gets allocated
deep down in the suspend path, where it is really hard to recover from
allocation failure. So allocate the piglet early on in the suspend path.
Also change the piglet and piglet allocation functions to use km_alloc(9)
instead of doing pmemrange magic. This removes a bunch of code which, in the
case of the piglet allocation, is broken since it results in a NULL pointer
dereference. Also switch the piglet allocation to not wait. If we can't
allocate 16MB of phys contig memory on a halfway modern machine we're almost
certainly under a lot of memory pressure and we're better off not trying to
hibernate anyway.

ok mlarkin@


# 1.271 25-Sep-2014 kettenis

Bring back rev 1.249. Now that mp_setperf() has been fixed, it should be
safe to restore perflevel when resuming. Should fix the issues people
see with the processor running at full speed heating up the machine while
the apm command reports that the CPUs are running at a low speed.

ok dcoppa@, deraadt@


# 1.270 23-Sep-2014 kettenis

Release the acpi lock around calls to wsdisplay_suspend() and
wsdisplay_resume(). The X server may adjust the display brightness just
before/after the VT switch when suspending/resuming. In fact, it may
already be blocked adjusting the display brightness for some reason
unrelated to the suspend request.

The VT switch request will eventually time out, but the delay is annoying
and the failed VT switch will lead to problems after resume.

Needed for a future update of the xf86-video-intel driver.

ok mlarkin@


# 1.269 19-Sep-2014 kettenis

Use config_suspend_all(9).


# 1.268 08-Sep-2014 kettenis

Clear the PME Status bit on PCI devices in response to Device Wake events.
Fixes the interrupt storm on the 2009 Mac mini with WOL enabled on its
nfe(4) interface.

ok mlarkin@


Revision tags: OPENBSD_5_6_BASE
# 1.267 20-Jul-2014 mlarkin

Support hibernating to softraid crypto volumes.

much help and ok from deraadt@


# 1.266 16-Jul-2014 mlarkin

Reenable hibernate RLE support and flush+zero all memory after unpack.


# 1.265 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.264 12-Jul-2014 kettenis

Add a function to drop all clean pages on the page daemon queues and call
it when we hibernate.

ok mlarkin@, miod@, deraadt@


# 1.263 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.262 11-Jul-2014 mlarkin

Flush the buffercache to 16MB on hibernate and restore its previous max
size (kern.bufcachepercent) on resume, for better hibernate performance.

ok beck@


# 1.261 10-Jul-2014 blambert

remove unneeded header

ok mlarkin@


# 1.260 01-Jul-2014 kettenis

Adding an additonal delay after each access to SystemMemory or SystemIO
during autoconf makes no sense.

ok deraadt@


# 1.259 23-Jun-2014 kettenis

SystemMemory is used to access memory mapped registers on some machines, so we
must use the bus_space(9) API to access it instead of memcpy(9). Also make
sure we properly align access to fields that need more than byte alignment.

ok guenther@


# 1.258 21-May-2014 mlarkin

Fix invalid / missing format specifiers in several dnprintf statements
(used only when ACPI_DEBUG is enabled)

From a diff on tech@ by Fabian Raetz, with a minor change by me, tested
on i386 and amd64 with ACPI_DEBUG enabled and disabled.


# 1.257 25-Apr-2014 mlarkin

Defer the free of the hibernate structures until later, when it is safe to
do so.

Freeing the hibernate structures too early leads to inadvertent calls into
splx when we aren't ready for them yet. Found when trapping splx calls in
the resume path.

discussed with deraadt@ at m2k14


# 1.256 13-Apr-2014 mlarkin

Fix a badly worded debug message that implied the entire machine was going
to sleep, instead of just the acpi thread going to sleep.


# 1.255 31-Mar-2014 mpi

Change the order of operations during a suspend/resume cycle and
call bufq_quiesce() after executing the DVACT_QUIESCE handlers.

This should be safe since no disk nor controller drivers have such
handler but it will allow us to detach sd(4) devices attached to a
USB bus.

Another benefit pointed out by kettenis@ is that drivers that need
to read a firmware from the disk should be able to do it at resume
time in a DVACT_WAKEUP handler.

ok kettenis@, deraadt@


# 1.254 13-Mar-2014 dlg

get rid of the assumption that the head of the alldevs list is the
"mainbus" device. this breaks when mpath is enabled because it
attaches before mainbus and therefore takes the head position.

have autoconf provide device_mainbus() which looks up mainbus_cd,
and use that instead.

discussed with deraadt who just wants mpath stuff to move forward
despite there being many ways to shine this particular turd.


Revision tags: OPENBSD_5_5_BASE
# 1.253 21-Feb-2014 deraadt

Do the lid control further on in the resume sequence, since this is
AML, and will do WAITOK operations. While cold, that is a bad idea.
We seem to be safe with respect to the spec, I do not see any event
debounce issues here.
ok kettenis, tested by many


# 1.252 20-Jan-2014 claudio

Instead of checking sc->sc_revision <= 1 check that the
fadt->hdr_revision <= 1. This is a better check since it checks
the version of the proper descriptor. This allows the alix APU
to reboot since those boards have currently a wrong sc->sc_revision.
Committing this now so that it can be tested easier.
With and OK jsg@ also fine by derradt@


# 1.251 24-Dec-2013 kettenis

If the FADT has its SMI_CMD set to zero, assume we're only ACPI-only hardware
and don't need to disable SMI ownership of the ACPI hardware registers.

ok mlarkin@


# 1.250 23-Dec-2013 kettenis

Revert previous commit. It makes resume hang on some MP systems running
GENERIC.MP.


# 1.249 16-Dec-2013 kettenis

Restore hw.setperf upon resume like we do for apm(4) on i386. Seems we need
to do this fairly late in the resume to avoid hangs.

ok deraadt@, mlarkin@


# 1.248 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


# 1.247 06-Nov-2013 mpi

Add support for Power Resources for Dx states and the necessary hook
for PCI devices. This hook should be called twice, before and after
changing the power state of a PCI device.

Before setting the device to the new state, the ACPI layer will notify
every power resources linked to the device for that state and make sure
they are turned "_ON". After changing the state of the device, it will
decrement the reference of every power resources linked to that device
for the old state and turn them "_OFF" if they are no longer referenced.

This fixes the no-USB after resume problem seen on various ThinkPad,
problem initialy diagnosed with Alexander Polakov.

ok kettenis@, deraadt@


Revision tags: OPENBSD_5_4_BASE
# 1.246 01-Jun-2013 mlarkin

Stop printing ACPI wakeup devices in dmesg after the 16th wakeup device.

Helps with VMware VMs that report hundreds of wakeup devices.

ok reyk@, deraadt@


# 1.245 31-May-2013 bluhm

Fix the build for a kernel without wd and pciide in its config.
OK deraadt@ tedu@


# 1.244 30-May-2013 deraadt

Enforce ca_activate tree-walks over the entire heirarchy for all events,
cleaning up some shutdown-hook related code on the way.
(A few drivers related to sparc are still skipped at kettenis' request)
ok kettenis mlarkin, tested by many others too


# 1.243 18-Apr-2013 deraadt

pin the acpi thread to the BSP, because AML and SMI cannot be trusted. I
was certain we were doing this already, but it turns out no...


Revision tags: OPENBSD_5_3_BASE
# 1.242 24-Dec-2012 guenther

In acpiioctl(), Return though the splx() path if suser() returns an error

ok deraadt@


# 1.241 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


# 1.240 04-Oct-2012 ehrhardt

Use information provided by ACPI to attach secondary PCI host bridges.

ok kettenis@


# 1.239 07-Sep-2012 kettenis

Add a new function called acpi_pci_min_powerstate() that returns the lowest
power state for a PCI device for the power state (currently S3 or S4) we're
transitioning into. This function is probably too simplistic; there is a lot
of (silly) complexity in the ACPI spec.

ok deraadt@


Revision tags: OPENBSD_5_2_BASE
# 1.238 13-Jul-2012 pirofti

Disable acpivideo(4) on ASUS laptops.

There are some models that panic whenever an event is received.
Some cope, some don't. In order to get this right acpiec(4) requires
major rework. Until then let machines boot.

Discussed with deraadt@.


# 1.237 11-Jul-2012 mlarkin

for now, restrict hibernate to root in the acpi ioctl kernel interface.
normal rules for controlling this interface through apmd still apply.

ok deraadt@


# 1.236 09-Jul-2012 deraadt

tiny little oops in untested hibernate block


# 1.235 09-Jul-2012 deraadt

clean up ioctl() case statements


# 1.234 09-Jul-2012 deraadt

acpi_disable_onegpe() is not used; ok pirofti


# 1.233 24-May-2012 kettenis

If _MIN > _MAX consider a WordBus resource to be invalid. Fixes a panic on
the IBM x3550-M4.

ok mikeb@


# 1.232 29-Mar-2012 mlarkin

Missing #ifdef in previous diff


# 1.231 29-Mar-2012 mlarkin

Prevent hibernation attempts on machines with unsupported disk controllers.
The same check is also performed later in the hibernate sequence, but by
then it's too late to cleanly unwind (presently).

ok deraadt


# 1.230 26-Mar-2012 deraadt

simplify conditionals in acpi_sleep_state()
ok mlarkin


# 1.229 26-Mar-2012 deraadt

hook in the hibernate request code; half of this diff is from mlarkin
ok mlarkin


Revision tags: OPENBSD_5_1_BASE
# 1.228 20-Sep-2011 deraadt

Very early in suspend while the clock is still ticking realtime,
save the clock back to the rtc. The zaurus already did this.
ok phessler miod kettenis


Revision tags: OPENBSD_5_0_BASE
# 1.227 02-Jul-2011 nicm

kqueue attach functions should return an errno or 0, not a plain 1. Fix
the obvious cases to return EINVAL and ENXIO.

ok tedu deraadt


# 1.226 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.225 16-Jun-2011 pirofti

Import acpitoshiba driver.

This is originally written by Hiroyuki Aizu, ported and integrated
in our tree by Javier Vazquez <javama4 AT gmail> with tweaks and
suggestions by me.

Import prodded by deraadt@ and the lack of testers on tech@.


# 1.224 27-Apr-2011 jcs

attach acpithinkpad to newer lenovo models like the x120e

ok deraadt@


# 1.223 22-Apr-2011 jordan

Fix uninitialzied variables and formatting strings (-Wxxx errors)


Revision tags: OPENBSD_4_9_BASE
# 1.222 02-Jan-2011 jordan

Re-add code for ACPI taskqueue, eliminates need for state vars in acpi softc
Serialize processing ACPI tasks for sleep/powerdown/GPE event
ok deraadt@


# 1.221 31-Oct-2010 guenther

Revert last commit: it breaks resume on Thinkpads
"then please back it out" deraadt@


# 1.220 26-Oct-2010 jordan

Add task queue for ACPI gpe and notify handlers
This fixes eject on Dell Latitude dock and an issue on the Dell Mini
battery update.
ok deraadt


# 1.219 07-Oct-2010 jordan

Create separate functions for enabling wake and runtime gpes.
Remove old suspend/resume gpewalk function
ok deraadt, mlarkin


# 1.218 05-Oct-2010 jordan

split acpi_enable_onegpe to separate enable/disable functions
ok deraadt


# 1.217 05-Oct-2010 jordan

Added acpi_disable_allgpes function to clear GPE status
ok deraadt


# 1.216 05-Oct-2010 deraadt

clean up acpi_softc use more


# 1.215 05-Oct-2010 deraadt

delete a bit of #if 0 code


# 1.214 05-Oct-2010 deraadt

hide use of our global acpi_softc pointer a bit better


# 1.213 29-Sep-2010 deraadt

grab the acpi thread lock very early in acpi thread startup; not that it
is likely that anyone will race us... but why be careless.
ok kettenis


# 1.212 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.211 29-Aug-2010 jasper

- add a check for FWRITE here too.

ok miod@


# 1.210 27-Aug-2010 kettenis

Make PCI Power Management optional, and only enable it when acpi(4) attaches.

ok deraadt@


Revision tags: OPENBSD_4_8_BASE
# 1.209 08-Aug-2010 kettenis

Close race between the acpi thread and wscons ioctls running in process
context. We might rip this out post-release and replace it with a solution
that serializes things in a more obvious way.

ok deraadt@, miod@ and "those crazy texans who ok anything"


# 1.208 08-Aug-2010 deraadt

Report the acpi sleep states when on a RAMDISK; ok krw


# 1.207 07-Aug-2010 kettenis

Enforce acpi register access rescrictions.

ok mlarkin@, deraadt@, tested by marco@


# 1.206 07-Aug-2010 deraadt

upon resume, notify all the acpiac and acpibat drivers to update
their status. some laptops do not do this automatically.
ok kettenis canacar


# 1.205 06-Aug-2010 marco

Always call _PSW on _LID devices that support it to enable lid open to
resume the machine. Conversely disable it when the machine wakes up.

Tested by several ok deraadt


# 1.204 06-Aug-2010 deraadt

As part of a revamp of the PM1/GPE code, I write this diff to look for
unmanaged EN & STS bits in the PM1 register at interrupt time and report
them. As a side effect this splits the STS acknowledgement into two writes
(for power, and sleep) instead of one. The printf that is added (to spot
unmanaged STS bits) has not yet been triggered as far as we know.

Before the "write to PM1 registers at the right offset" diff went in, this
was not neccessary. But newer thinkpads do not have a working soft power
button without this diff.

We have no idea why.
ok mlarkin kettenis


# 1.203 05-Aug-2010 deraadt

PM1 power button events were generating 'fake events' to acpibtn PWRB,
but if there is no such device these were being tossed. Instead, arrange
for all power button handlers to flag a softc variable which is processed
by the thread; so that one place will know to request a powerdown (psignal
to init)
ok marco mlarkin


# 1.202 05-Aug-2010 deraadt

rename a variable which is annoyingly poorly named


# 1.201 05-Aug-2010 mlarkin

Write to the correct offset for various ACPI pmreg registers. Writing to
the correct register does properly clear the wake status bits and fixed
event bits, which allows some machines to have more than 1 suspend cycle.

bug spotted by deraadt@

tested by deraadt@ and myself on a half-dozen machines and others on
hackers@

ok deraadt@, marco@


# 1.200 05-Aug-2010 deraadt

Fix the power button. The pm1 register is 16 bits in size.
My mistake, a while back.
tested by krw


# 1.199 04-Aug-2010 kettenis

Print supported sleep states.

ok marco@, phessler@, deraadt@


# 1.198 03-Aug-2010 mlarkin

Use the proper flag for re-enabling certain hardware events (power btn,
sleep btn). The (incorrect) flag prevoiously used coincidentally had the
same value, so this shouldn't cause any different behavior than before.

tested on a variety of machines (i386, amd64, sp, mp) - no behavioral
change seen

ok deraadt@


# 1.197 28-Jul-2010 marco

Make all gasio failures look and behave the same.


# 1.196 28-Jul-2010 mlarkin

Delay a little longer on suspend before giving up. Fixes machines that
sometimes don't suspend because the sleep transition takes too long
(symptoms like failing to sleep but the backlight stays on).

ok deraadt@


# 1.195 27-Jul-2010 deraadt

remove XXX; EC can now be attached early when needed; ok jordan


# 1.194 27-Jul-2010 deraadt

more brackets feels good


# 1.193 27-Jul-2010 deraadt

XXX annotate a block in acpi_enter_sleep_state() that is pretty worrying


# 1.192 27-Jul-2010 deraadt

horrific indentation that nearly made me puke


# 1.191 27-Jul-2010 mlarkin

Set the sleeping indicator light on machines that support it, in case SMI
doesn't do it for us. Shows the 'moon' LED on older Thinkpads when
sleeping.

Tested on T43p, W500, T510 by me and on X61s by deraadt@.

ok deraadt@


# 1.190 27-Jul-2010 jordan

Early initialization of acpiec if ECDT table exists
Fixes hang when booting thinkpads while docked
ok deraadt


# 1.189 26-Jul-2010 deraadt

remove merge error which was commited


# 1.188 26-Jul-2010 pirofti

Add support for Sony ACPI hotkeys via a new driver: acpisony(4).

Currently it only works for the suspend button and tries to do right for
the brightness events, but I haven't found a Sony laptop that like to do
right so far.

In the future I want to make the brightness keys work on all Sony's and
also add support for the zoom hotkeys and whatever other funky keys I can
find on those things.

Okay deraadt@.


# 1.187 22-Jul-2010 deraadt

pretty large cleanup of SMALL_KERNEL


# 1.186 21-Jul-2010 deraadt

in the acpi GPE handler, late ack level interrupts not edge interrupts.
Duh.


# 1.185 21-Jul-2010 deraadt

Merge the edge and level gpe handlers into one (now possible since the
gpe structure tells us whether the gpe is edge or level)


# 1.184 21-Jul-2010 deraadt

delete #if 0 code we are not going to use; ok marco


# 1.183 20-Jul-2010 deraadt

make the mask code for acpi_enable_onegpe() match other similar code


# 1.182 20-Jul-2010 deraadt

Handle the acpi interrupt controller with proper edge/level handling,
wrapped in spltty to avoid racing against the interrupt controller.
Repair the gpe bit masking code while there, and do operations in the
right order
ok kettenis mlarkin, help from jordan at figuring out the order of
operations.


# 1.181 20-Jul-2010 matthew

When we're preparing to go to sleep, only call bufq_restart() if other
preparations failed.

ok kettenis@, thib@, deraadt@


# 1.180 19-Jul-2010 deraadt

permit compilation with ACPI_DEBUG


# 1.179 19-Jul-2010 deraadt

there is no need for a seperate dsdt_softc pointer; dsdt does not have a
softc but is just a part of the single acpi driver.
ok jordan


# 1.178 19-Jul-2010 deraadt

acpi_delay() is no longer needed; ok jordan


# 1.177 18-Jul-2010 mlarkin

Clear event status on resume, to avoid going back to sleep/reboot/shutdown
immediately after resume. Tested by a bunch of people on hackers@ on
various hardware. Fixes at least Toshiba NB200 and Gateway LT3103u.

ok marco


# 1.176 13-Jul-2010 deraadt

rename some thread related interfaces so they are logical


# 1.175 10-Jul-2010 jordan

Verify _ADR bdf is correct (some systems return 0xffff)
Works on Dell x9xx and Studio 1555


# 1.174 09-Jul-2010 jordan

Clear the noise


# 1.173 08-Jul-2010 jordan

Add mapping for ACPI device to PCI bdf (match autoconf tree)
Simplify resource parsing function to use buffer argument
Convert namespace linked lists to use queue macros
ok marco@, deraadt@


# 1.172 06-Jul-2010 deraadt

Use spltty() to lock downcalls from apm against the information being
modified by the acpi thread.
ok kettenis


# 1.171 06-Jul-2010 deraadt

Don't use a workq to do the suspend, because it races aginst the acpi
thread. Instead, just tell the acpi thread to do the suspend for us.
This makes apmd & zzz work correctly. While here, have acpithinkpad
attempt to post the event to apm before prompting the suspend itself.
ok kettenis marco mlarkin


# 1.170 05-Jul-2010 mlarkin

Reset register SCI_EN on resume. The ACPI spec clearly states we don't have
to do this, but many machines don't, or forget to, which leads to all sorts
of problems on resume (broken battery stats, overheating, failure to resume
more than once, etc).

tested by many, ok marco@, deraadt@


# 1.169 01-Jul-2010 thib

do bufq quiesce again, since vnd/the bufq_done call in
biodone has been fixed.


# 1.168 01-Jul-2010 jordan

Backout recent AML changes, breaks on R210 and others


# 1.167 01-Jul-2010 jordan

Simplified aml_parse_resource call, cleanup code
ok marco


# 1.166 01-Jul-2010 jordan

Fix subordinate bus # for multi-root PCI buses
ok marco, kettenis


# 1.165 30-Jun-2010 thib

Disable/partially backout the bufq quiesce changes as this
is causing havoc with vnds and release must be buildable.


# 1.164 29-Jun-2010 deraadt

Fix ramdisks (new code was placed inside a SMALL_KERNEL chunk)


# 1.163 29-Jun-2010 jordan

Add support for mapping ACPI to PCI devices
ok kettenis, deraadt


# 1.162 29-Jun-2010 kettenis

Call bufq_quiesce() on suspend to stop all I/O and bufq_restart on resume.
Makes suspend/resume work much better while doing disk I/O.

ok mlarkin@


# 1.161 29-Jun-2010 tedu

60 is a more reasonable guess for number of minutes in an hour than 100.
spotted by TeXitoi. ok deraadt


# 1.160 27-Jun-2010 jordan

Add _acpi_matchhids routine, precursor to pci device match


# 1.159 27-Jun-2010 mlarkin

Clear ACPI fixed event status on resume (power buttons/etc) so that some
machines don't immediately resume after suspending to S3.

Tested by kettenis@ and I on 5 or 6 machines, deraadt@ "yeah, looks good"


# 1.158 27-Jun-2010 jordan

Cleaned up acpi_maptable, adds to table list directly


# 1.157 07-Apr-2010 deraadt

If we have done a wsdisplay_suspend(), and ACPI decides it does not want to
suspend, we must roll back by calling wsdisplay_resume() or the virtual
console switching will be locked forever
ok oga


# 1.156 07-Apr-2010 kettenis

Make suspend/resume work on MP machines (running an MP kernel). Joint work
from mlarkin@ and me, with some amd64 fixes thrown in by deraadt@

ok marco@, deraadt@, pirofti@, mlarkin@


# 1.155 31-Mar-2010 kettenis

Bring /dev/apm support in line with apm(4). Make the suspend button send
a suspend request event to apmd(8) instead of suspending immediately. Also
keep track of whether /dev/apm and /dev/apmctl are currently open, such that
we can still suspend immediately if apmd(8) isn't running.

ok deraadt@, marco@, pirofti@, jsing@, oga@


# 1.154 30-Mar-2010 oga

Prevent the apmd/x races for good.

When we hit suspend time, go through all wsdisplays on the system. if
they are in mode MAPPED, but not MODE_DUMBFB then if possible do a full
vt switch to a !mapped vt, and prevent switching back until resume time.
This has to be called from MD code because this involves userland
running so that X can run the vt switch signal handler. This way, any
case where we are using the "poke registers from userland" model, we
will not be on the hardware when we go down, so the kernel can actually
handle thing properly.

Tested on several acpi laptops (by kettenis@ and ian@), x40 (me and
beck@ at LEAST) and zaurus (me). Maybe others, but if so I forgot who at
this time..

Idea from deraadt somewhere over the Faroe Islands (I thought of a
similar thing myself a while ago). Much prompting from him. Ok and
comments miod@


# 1.153 25-Mar-2010 oga

Fix & vs. && in the kqfilter code.

Based on a comparison with the apm code.

ok deraadt@, kettenis@


Revision tags: OPENBSD_4_7_BASE
# 1.152 26-Nov-2009 mlarkin

Visual (and on some machines, audio) feedback while the machine is
resuming via calls to aml _SST method. This also makes the "moon" LED on
thinkpads to blink during resume and cease when resume is completed.

ok pirofti@


# 1.151 26-Nov-2009 deraadt

Toggle into "cold" when we we disable interrupts, because tsleeping
in the AML interpreter and context switching into userland processes
tends to sometimes pop the ACPI trampoline page out of our address
space. Makes lots more machines work.
ok kettenis mlarkin


# 1.150 24-Nov-2009 jsg

in the resume path evaluate _WAK before dealing with device drivers
like the spec says, not after.

ok mlarkin@


# 1.149 24-Nov-2009 deraadt

abort suspends on SMP systems until that gets written; ok mlarkin


# 1.148 23-Nov-2009 mlarkin

pci should return the result of it's children's suspend/resume calls back
to its parent.

handle suspend failure case and unwind if devices have an activate function
that returns failure.

ok deraadt@, kettenis@


# 1.147 23-Nov-2009 mlarkin

acpi* devices need to call their children on suspend/resume.
ok deraadt@


# 1.146 23-Nov-2009 pirofti

Remove ACPI_SLEEP_ENABLED checks.

This enables by default the suspend/resume paths in the kernel.

Okay deraadt@.


# 1.145 23-Nov-2009 deraadt

knf and spacing that are super annoying


# 1.144 23-Nov-2009 mlarkin

Hook up Theo's new children suspend/resume function, as well as more ACPI
suspend/resume glue.

ok deraadt@


# 1.143 26-Oct-2009 deraadt

Do not do strncmp comparisons on dv_xname because longer device names which
look similar could arrive in the future. Instead, compare directly against
dv_cfdata->cf_driver->cd_name
Issue originally spotted by miod


# 1.142 26-Oct-2009 jordan

Verify checksum+address when loading ACPI tables.
Some systems had invalid entries in RSDT/XSDT.
ok marco@


# 1.141 23-Jul-2009 cnst

New aibs(4) driver for ASUSTeK AI Booster (ACPI ATK0110) hardware monitoring,
with sensor state support through limits provided by the ACPI.

Tested on several ASUS motherboards kindly networked by Sam Fourman Jr.

ok deraadt marco jordan


Revision tags: OPENBSD_4_6_BASE
# 1.140 03-Jun-2009 pirofti

Import acpi power resource driver. This is needed for suspend/resume logic.

Okay jordan@ and marco@.


# 1.139 03-Jun-2009 pirofti

Add acpivideo support. This adds brightness support for all laptops
except thinkpads, they will use the acpithinkpad driver. The driver is
also hooked into wsconsole. So brightness can be adjusted via:

$ wsconsctl display.brightness=<percentage>

This is very helpfull on some laptops that have a nasty bios and get two
steps instead of one when pressing the brightness button.

Tested on various dell, fujitsu, acer, samsung and other laptops.

Okay marco@, miod@. Suggestions from kettenis@.
Lots of reviews and help from miod@, thanks!


# 1.138 03-Jun-2009 jordan

Added ejectable bay for IDE cdroms
ok marco@


# 1.137 30-Apr-2009 marco

Oga changed apm ioctls and forgot to grep. Temporary work around by hitting
the sleep button when we get any request. This interface will chnage at
some point.
ok oga


# 1.136 19-Apr-2009 krw

Call acpi_sleep_walk() when powering down just like when entering
any other sleep state. This tells acpi wake devices to not wake up
the box until manual powering up resets their status. Makes my Dell
GX520 stay powered off in response to 'halt -p'.

In snaps for a while.

ok marco@ (several times) deraadt@


# 1.135 17-Apr-2009 pirofti

Add device ranking support for future sleep/wake developments.

okay marco@


# 1.134 11-Mar-2009 jordan

Cleanup.. use aml_evalinteger instead of aml_evalname
ok marco@


# 1.133 10-Mar-2009 jordan

Fix cut/paste error.. call _BFS not _PTS
ok marco@


Revision tags: OPENBSD_4_5_BASE
# 1.132 21-Feb-2009 marco

overzealous cut/paste; found by Vladimir Kirillov <proger@uaoug.org.ua>


# 1.131 20-Feb-2009 marco

Oops didn't mean for Debugger() to remain there.


# 1.130 19-Feb-2009 marco

suspend/resume bits so that we can develop this in tree. This is disabled.
code from mlarkin and me
help from art,toby,jordan and several others
ok jordan, go for it deraadt


# 1.129 10-Feb-2009 jordan

Fix null pointer for shutdown if acpi not enabled
ok marco@


# 1.128 04-Feb-2009 kettenis

Some systems that claim to be ACPI 2.0 apparently don't provide an XSDT
address. Make the code fall back on the RSDT on those systems.

From Joerg Niendorf <p3y80@internode.on.net>. Fixes PR 5986.


# 1.127 17-Jan-2009 krw

A couple of stray malloc/memset -> malloc(M_ZERO), a bit of knf and
lose a couple of unnecessary casts. No functional change.

Feedback from kettenis@

ok kettenis@ marco@


# 1.126 06-Nov-2008 marco

Collapse all sanity checks of hids into one function.

Originaly from jordan


Revision tags: OPENBSD_4_4_BASE
# 1.125 02-Jul-2008 fgsch

add acpivideo. for now almost an empty skel so others can work on
suspend/resume. eventually it will also manage output switching and
brightness where supported. prodded by marco@


# 1.124 11-Jun-2008 marco

Fix $OpenBSD$ for once and for all


# 1.123 08-Jun-2008 deraadt

aaa_dev was being set from an uninitialized variable; ok toby


# 1.122 07-Jun-2008 marco

Kill ACPIVERBOSE. It was never used and is worthless to boot.

comments jmc
ok toby


# 1.121 06-Jun-2008 marco

Shrink acpi some more for floppies and while here re-enable acpi on the
floppies. Fixes crazy sony vaio kettenis has.

tested by deraadt
ok deraadt jordan kettenis


# 1.120 01-Jun-2008 marco

Fix \n nit in SMALL_KERNEL.


# 1.119 01-Jun-2008 marco

Re-enable new parser. Lots of testing by various folks. Fits all media
now too.

ok kettenis deraadt


# 1.118 16-May-2008 dlg

back out jordans new parser.

it didnt have enough oks from the right people, it did break machines
that were previously working, and it bloated the RAMDISKS and install
media beyond the constraints of the media.

requested by deraadt@


# 1.117 14-May-2008 jordan

Adding new ACPI Parser code
Old guts of code still exists, needs to be torn out next
ok marco@
ok brad@


# 1.116 27-Apr-2008 jcs

add acpithinkpad(4), an acpi support driver for ibm/lenovo
thinkpads.

provides functionality that the bios did on older, non-acpi
thinkpads like respond to screen brightness keys and toggle the
bluetooth device on and off.

ok deraadt@ and marco@


# 1.115 24-Apr-2008 jsing

Introduce acpiasus(4), a driver for the ACPI based hotkeys found in many
ASUS laptops (including the ASUS EeePC) - largely based on NetBSD's
asus(4) driver. On the ASUS EeePC this allows us to enable/disable
wireless, change screen brightness and use the volume keys.

ok jsg@, weingart@


# 1.114 12-Apr-2008 kettenis

Get rid of acpi_s5 global variables; simply send SIGUSR2 instead of SUGUSR1
to tell init(8) to power down the machine.

ok krw@


Revision tags: OPENBSD_4_3_BASE
# 1.113 21-Jan-2008 jsg

Check against RESET_REG_SUP in FADT before doing the acpi reset dance
after all, not doing so appears to break more machines than it fixes.

ok marco@


# 1.112 05-Jan-2008 kettenis

Never attach more acpicpu(4) devices than cpu(4) devices. Always attach
the first acpicpu(4) device on non-MULTIPROCESSOR kernels, regardless of its
ACPI processor ID.

ok mikeb@


# 1.111 05-Dec-2007 deraadt

Hang acpi(4) below bios(4) instead of mainbus(4). This lets us move acpi(4)
before pcibios(4), and then.... if acpi(4) attaches, skip pcibios(4) since
it messes the machines up. Fixes claudio's HP dl320 G5, and almost assuredly
others.
ok kettenis


# 1.110 05-Dec-2007 deraadt

repair acpiopen/acpiclose and friends to handle the /dev/apmctl interface,
so that apmd(8) can actually work correctly against acpi(4), and thus,
even do the fancy -C/-A stuff; also tested by jmc


# 1.109 02-Dec-2007 fgsch

fix the trailing space for real now.


# 1.108 02-Dec-2007 fgsch

avoid trailing spaces on output; marco@ ok.


# 1.107 27-Nov-2007 kettenis

Fix off-by-one that made us attach acpicpu0 and acpicpu1 on GENERIC kernels.

ok marco@


# 1.106 25-Nov-2007 jsg

When available and ACPI is enabled use the ACPI reset method
to reboot. This is required for some quirky sis machines
and likely others to reboot.

Tested by many. 'commit early commit often' deraadt@


# 1.105 19-Nov-2007 deraadt

sigh, amd64 still uses that stupid acpi_enabled variable...


# 1.104 19-Nov-2007 deraadt

softc's come initialized to 0, you know...


# 1.103 19-Nov-2007 deraadt

store revision; from jsg


# 1.102 19-Nov-2007 kettenis

Initialize sc_facs earlier.

ok deraadt@, beck@, canacar@


# 1.101 16-Nov-2007 deraadt

fix the bus_space #define nightmare, so that amd64 and i386 are much more
uniform. as a result shared code like acpi needs less #ifdef's
ok marco kettenis


# 1.100 15-Nov-2007 deraadt

remove the #ifdef ACPI_ENABLE. the EC codepath is either safe enough (and
will be improved), or it is unsafe because AML assumes it must be there
ok almost everyone


# 1.99 15-Nov-2007 mikeb

Make acpicpu(4) attach to the existing CPUs only.

Problem reported by form@. Fix idea by kettenis@.
Lots of help from deraadt@. Tested by deraadt@, form@ and me.

ok deraadt marco


# 1.98 14-Nov-2007 deraadt

spacing


# 1.97 12-Nov-2007 deraadt

correct : vs , in attach-time messages


# 1.96 06-Nov-2007 deraadt

all ACPI_ENABLE kernels need acpi_interrupt()


# 1.95 06-Nov-2007 deraadt

move acpi_read_pmreg() and acpi_write_pmreg() out of SMALL_KERNEL so
that acpi ramdisks can compile (obvious solution)


# 1.94 05-Nov-2007 canacar

Establish the acpi interrupt earlier. Fixes problems with some
GENERIC.MP kernels not getting acpi interrupts. This is a
workaround until the real issue with late establishment is fixed.
ok weingart@, beck@, gwk@


# 1.93 03-Nov-2007 jordan

Added support for displaying wakeup devices
ok beck@,weingart@,gwk@


# 1.92 11-Oct-2007 kettenis

When walking the tree yo find _PRT methods, check whether the device is
actually enabled and functioning. Similar to what we do for _INI.

ok weingart@, beck@


# 1.91 08-Oct-2007 krw

More simple memset(,0,) -> M_ZERO changes. In this batch move to
size(*p) as the first malloc() parameter where p is declared locally
and thus easy to check. Add M_ZERO to gpe_table allocation in acpi.c
even though there is no obvious bzero or memset nearby.


# 1.90 13-Sep-2007 mk

Remove unnecessary memset() (see aml_evalnode(9)) and a bit of KNF.

ok toby


# 1.89 13-Sep-2007 weingart

Implement ACPI 6.5.1 spec tree walk for _STA and _INI.
Fixes mk's laptop. No regressions so far (thank you to the
testers).

ok gwk@, mk@, marco@


Revision tags: OPENBSD_4_2_BASE
# 1.88 17-Apr-2007 mk

Now that acpidock no longer hangs machines during boot (at least
according to the reports I've been getting) it should be safe to enable
it again, so remove the #if 0 stuff around it.

ok marco


# 1.87 11-Apr-2007 jordan

Added changes for C-State
ok marco@


# 1.86 26-Mar-2007 marco

Calculate minutes left more accurately.

From giovanni <qgiovanni@gmail.com>


Revision tags: OPENBSD_4_1_BASE
# 1.85 03-Mar-2007 mk

Stop looking for acpidock until we fix that stuff that breaks all pre-60
series ThinkPads with acpidock(4) enabled.


# 1.84 22-Feb-2007 marco

We were getting no interrupts in .MP. There are 2 fixes; one in
acpi_machdep.c and move the interrupt establishment down after ioapic
has been initialized.

ok jordan


# 1.83 21-Feb-2007 kettenis

Make an acpi-enabled GENERIC.MP work on systems that only support PIC mode.

ok marco@


# 1.82 19-Feb-2007 jordan

Added acpi_map_address function for mapping GAS/IO registers
ok marco@


# 1.81 18-Feb-2007 jordan

Added dynamic perfect hash for opcodes
ok marco@


# 1.80 18-Feb-2007 marco

Shave off another K pre compression.
Shuffle SMALL_KERNEL functions around so that we only have a single #ifndef


# 1.79 17-Feb-2007 marco

Add SMALL_KERNEL throught acpi to make it fit on boot media.
ok deraadt


# 1.78 13-Feb-2007 marco

Add smarter _INI code. This helps with some dissapearing devices.

ok kettenis jordan


# 1.77 31-Jan-2007 gwk

Introduce acpi_hasprocfvs thats "ACPI Has Processor Frequency and Voltage
Scalling" a global flag set by acicpu attach to indicate that ACPI is
handling fvs through the _PSS objects. This will fix disappearing
EST/powernow when booting kernels with acpi enabled, as acpicpu isnt
compiled into GENERIC yet. Also in cases where acpicpu is built in and
legacy EST/powernow work but acpi is missing the _PSS object, the legacy
routines will be used.

tested by dim@, and Nick Nauwelaerts <nick AT nauwelaerts.net>
ok marco@, dim@


# 1.76 22-Jan-2007 mk

Actually look for docking stations by looking for _DCK nodes.

ok marco


# 1.75 02-Jan-2007 marco

On some systems EC must be attached as the first device. Move EC from
generic _HID devices to its own routine to force first attach. Bug found
and debugged by joris@

Code mostly written by joris@


# 1.74 26-Dec-2006 marco

Add polling to devices that require it such as acpibat and acpitz.
Use the same tsleep/wakeup mechanism as the interrupt code to keep it non-
concurrent.


# 1.73 21-Dec-2006 deraadt

even more knf love


# 1.72 21-Dec-2006 marco

Remove some leftover acpiec gpe/interrupt code.


# 1.71 20-Dec-2006 deraadt

oops, name already used


# 1.70 20-Dec-2006 deraadt

rename __acpi_enable_gpe(); ok marco


# 1.69 20-Dec-2006 deraadt

acpi_unmap_pmregs() is not used, ok marco


# 1.68 19-Dec-2006 deraadt

simplify some low-level methods substantially (shrinking code); ok marco jordan


# 1.67 06-Dec-2006 mk

malloc(..., M_NOWAIT) with no NULL check is bad. If allocation fails,
complain, clean up, and bail out of attach.

input and ok gwk


# 1.66 28-Nov-2006 jordan

Fixed clearing arg for acpi_init_pic
ok kettenis@


# 1.65 27-Nov-2006 kettenis

Don't spam dmesg.


# 1.64 27-Nov-2006 jordan

Fixes for acpiprt routing
Post-parse fixup for namerefs
ok kettenis@


# 1.63 25-Nov-2006 marco

Don't handle acpiev events inside the gpe handler since this causes deadlocks.

ok jordan@


# 1.62 15-Nov-2006 kettenis

Hook up acpi apic interrupt routing support to generic acpi code.

ok jordan@


# 1.61 02-Nov-2006 marco

Kill acpiec attach function and add its functionality to the generic _HID
device attach

prodded and ok jordan


# 1.60 02-Nov-2006 marco

Fix comments to reflect reality.


# 1.59 19-Oct-2006 marco

Replace ACPI ioctls with APM ioctls. This makes ACPI talk the same protocol
as APM therefore userspace can now retrieve power information in the same
fashion as all other apm arches (zaurus, macppc, i386 etc).

To use force the use of acpi device like so: apmd -f /dev/acpi

ok jordan@


# 1.58 19-Oct-2006 jordan

Fixed GPE debug levels
Fixed ref counter on temporary bufferfields
DSDT parsing fully working on IBM Thinkpad
ok marco@


# 1.57 15-Oct-2006 jordan

Removed old GPE handler code remnants
ok marco@


# 1.56 12-Oct-2006 jordan

Massive ACPI changes
Fixed ACPI leaks
new ACPI API
ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.55 30-Jun-2006 gwk

KNF, No functional change,
"go at it" marco@


# 1.54 30-Jun-2006 jordan

Support for calling Embedded Controller via new GPE interface
ok marco@


# 1.53 30-Jun-2006 jordan

Added new code for GPE handling
Also works with ACPIEC; need to fixup acpiec.c to remove original interrupt handler
ok marco@


# 1.52 30-Jun-2006 jordan

Added offset argument for acpi read/write pmreg routines (new GPE code)
ok marco@


# 1.51 31-May-2006 todd

do not call acpi_powerdown() unless acpi has successfully initialized
ok gwk@ canacar@


# 1.50 29-May-2006 canacar

Add embedded controller support.
ok marco@ deraadt@ testing & ok gwk@


# 1.49 19-May-2006 canacar

Minimal thermal zone driver. Monitors thermal zone temperature,
shuts down the system if the 'critical temperature' is reached.
ok marco@


# 1.48 11-Apr-2006 marco

KNF.


# 1.47 11-Apr-2006 gwk

Add acpi_resume(struct acpi_softc *sc), which calls some AML methods,
detected at attach time, currently unused.

ok marco@


# 1.46 11-Apr-2006 gwk

Discover some ACPI sleep related methods, and call them at the
appropriate time in acpi_enter_sleep_state().

ok marco@


Revision tags: OPENBSD_3_9_BASE
# 1.45 26-Feb-2006 marco

Add acpicpu as a device.

ok jordan@


# 1.44 23-Feb-2006 marco

remove dead function.

ok jordan@


# 1.43 22-Feb-2006 marco

Print out all tables it found during autoconf. We'll need this data in
the future.


# 1.42 22-Feb-2006 marco

Make dmesg less verbose.

ok jordan.


# 1.41 22-Feb-2006 jordan

Added parsing of SSDT tables
ok marco@


# 1.40 22-Feb-2006 jordan

Added new aml_register to notify by device id
Moved powerdown code to button device
ok marco@


# 1.39 21-Feb-2006 marco

Readjust debug levels that crept in too low over the last few days.
KNF


# 1.38 21-Feb-2006 marco

Rig the power button so that the machine shuts down correctly.


# 1.37 20-Feb-2006 marco

Add acpibtn device. Remains disabled for now.


# 1.36 19-Feb-2006 jordan

Added code for aml_notify GPE notification callback
ok marco@


# 1.35 19-Feb-2006 grange

Fix acpi_enter_sleep_state() by replacing current ugly hack
with mostly correct sleeping state transition code.

ok brad@ marco@


# 1.34 19-Feb-2006 marco

jordan:
Enable interrupts.
Added peripheral code to make this work.

marco:
Replace softintr and other types of interrupts with a handler with context.
Fixed interrupt magic for GPEs.
Some KNF

This is work done by jordan and I but has to be a single commit.


# 1.33 17-Feb-2006 marco

Remove earlier introduced delays. We really only need them during autconf.

ok jordan@


# 1.32 17-Feb-2006 marco

Add a generic delay function as per the ACPI spec.

from jordan@


# 1.31 17-Feb-2006 marco

Use tsleep instead of delay when !cold.


# 1.30 17-Feb-2006 marco

Argh, we need delays between reads/writes. The backend reads (i2c most likely)
are so slow that values come back as 0 sometimes. We have to do this
despite the spec saying we shouldnt have to. Guess having a faster
parser than others can hurt sometimes.

Idea from jordan.


# 1.29 17-Feb-2006 jordan

Fixed code for handling _INI
Rearranged debug statements
ok marco@


# 1.28 16-Feb-2006 jordan

Added stub for calling _INI
Fixed aml_dowhile for NULL return
ok marco@


# 1.27 16-Feb-2006 jordan

Fixed parser/evaluator to retrieve battery info+state
Cleanup of parser
Dell laptops return battery status
ok marco@


# 1.26 06-Feb-2006 brad

fix a typo so this will compile on amd64.


# 1.25 04-Feb-2006 marco

When ACPI_ENABLED wasn't set but the devices were it had an unknown variable.

Pointed out by ian@.


# 1.24 03-Feb-2006 jordan

Rewrote aml parser yet again...
Fixed bus_space_map for i386/amd64
Added _BST and _BIF to acpibat to get status/info
ok marco@


# 1.23 20-Jan-2006 jordan

More evaluator changes
Removed old evaluator code
Fixed _HID value
ok marco@


# 1.22 19-Jan-2006 jordan

Added support for read/write of PCI Config space
Help jason@
ok marco@


# 1.21 18-Jan-2006 jordan

Added new evaluation routines
Added helper methods for buffer/field evaluation
Fixed broken indentation on dsdt.c

ok marco@


# 1.20 17-Jan-2006 jordan

Major overhaul of the aml parser. Evaluater temporarily disabled.

ok marco@


# 1.19 06-Jan-2006 grange

Check for ACPI control availability only if ACPI_ENABLE defined.


# 1.18 06-Jan-2006 grange

Informative error messages on attach, prettier dmesg.


# 1.17 06-Jan-2006 grange

printf -> dnprintf conversion for debug messages.


# 1.16 05-Jan-2006 grange

Find SLP_TYPx values and store them for future use in
acpi_enter_sleep_state().


# 1.15 05-Jan-2006 grange

Random KNF and cleanup


# 1.14 05-Jan-2006 grange

Provide symbolic names for the acpi sleeping states and use it.


# 1.13 28-Dec-2005 marco

Moved DEVNAME to a acpidev.h

ok jordan@


# 1.12 18-Dec-2005 sturm

add back comment about wrong code

ok marco


# 1.11 16-Dec-2005 marco

Remove test for kbd_reset. Prompted deraadt@, ok jordan@


# 1.10 16-Dec-2005 jordan

Added hooks for powerdown/power button press


# 1.9 16-Dec-2005 jordan

Cleaned up common power management register i/o
Added enter sleep state ioctl
Hooked power button interrupt
ok marco@


# 1.8 16-Dec-2005 marco

Remove autoconf chatter


# 1.7 16-Dec-2005 jordan

Moved DSDT AML parsing into parent ACPI driver
ok marco@


# 1.6 14-Dec-2005 marco

Add AC device glue.


# 1.5 14-Dec-2005 marco

Adjust debug printing levels to something more sane.


# 1.4 13-Dec-2005 marco

Add glue to attach devices to apci.


# 1.3 07-Dec-2005 marco

KNF


Revision tags: OPENBSD_3_8_BASE
# 1.2 10-Jul-2005 grange

Spacing cleanup.


# 1.1 02-Jun-2005 tholo

Start on a basic ACPI framework -- does not do much more than read out the
ACPI tables into kernel memory and attach ACPI and HPET timers currently.

In order to test this code, enabling the devices in GENERIC as well as
the ACPI_ENABLE option is needed. This code does not do any thermal
control yet, so this should be done with care depending on the platform.

In the tree so more people can contribute to making this more fully
featured.

Ok niklas@ grange@ tedu@


# 1.373 28-Aug-2019 kettenis

Use ACPI information to attach PCI busses like we do on arm64. There are a
few additional quirks though, and attaching the PCI busses is delayed to
replicate the existing code more closely. That may be changed in the
future. Also tweak how we handle MSI support and respect to ACPI flag
that says we shouldn't attempt to use MSIs.

Some fallout is expected.

ok patrick@


# 1.372 27-Aug-2019 deraadt

On amd64 the bootup and resume paths for SP/BP/SP were not performing
msr/pat, clock-startup, firmware upgrade, fpu initialization, and cpu
soft-state initialization in the same order. That kind of chaos is
crazy and fragile. Improve the acpi resume call sequence and unify order
of the operations.


# 1.371 02-Jul-2019 jcs

acpi_set_gpehandler: turn edge arg into flags, add support for
passing GPE_DIRECT which directly executes the supplied callback and
clears a level interrupt rather than going through acpi_taskq which
can be slow when other driver callbacks are in the queue.

This should not be used for GPE handlers that need to do anything
ACPI-related.

ok kettenis


# 1.370 10-Jun-2019 kettenis

Print proper ACPI version number.

ok mlarkin@


# 1.369 08-Jun-2019 kettenis

When establishing the mapping between ACPI device nodes and PCI devices,
skip PCI host bridges and devices that aren't there.

ok patrick@


# 1.368 07-Jun-2019 kettenis

Introduce acpi_getsta() and use it to eliminate some duplicated code.

ok patrick@


# 1.367 12-May-2019 kettenis

Make sure the extended PM register descriptions actually exist before
using them. Fixes machines with ancient ACPI 1.0 (such as recent
hypervisors).

ok deraadt@


# 1.366 12-May-2019 kettenis

Add support for mapping ACPI PM registers using the "extended" gasio
representation from the FADT. Mostly fixes the Lanner NCA-1510.

ok lteo@, mlarkin@, deraadt@


# 1.365 11-May-2019 lteo

Fix incorrect error message.

ok kettenis@


# 1.364 08-May-2019 tedu

free size. the allocation is the header plus the length.


Revision tags: OPENBSD_6_5_BASE
# 1.363 04-Apr-2019 kettenis

Avoid potential null-pointer dereference. Found by Kent R. Spillner.

ok deraadt@, jsg@


# 1.362 20-Jan-2019 jsg

fix SMALL_KERNEL build after changes in rev 1.361
reported by naddy@ ok deraadt@


# 1.361 19-Jan-2019 tedu

Add a pwraction sysctl that controls what the power button does on acpi.
By default, nothing changes -- shutdown is initiated. But allows turning
power button into a sleep button if desired.
(grudging) ok from a few parties


# 1.360 26-Oct-2018 kettenis

Add acpipci(4) on amd64. For now this only calls the PCI-specific _OSC
method to let the ACPI implementation know what features we support.


Revision tags: OPENBSD_6_4_BASE
# 1.359 30-Aug-2018 kettenis

Move softbutton check before the check that skips attaching certain acpi
devices. Fixes regression caused by matching on _CID in addition to
matching on _HID.

ok matthieu@, mlarkin@


# 1.358 25-Aug-2018 kettenis

Match ACPI devices based on _CID if no match for _HID is found.

ok mpi@


# 1.357 19-Aug-2018 kettenis

Add support for multiple PCI segments. Only really implemented for arm64
for now as amd64/i386 firmware still caters for legacy OSes that only
support a single PCI segment.

ok patrick@


# 1.356 03-Aug-2018 kettenis

Let ahci(4) match on _CLS instead of _HID when attaching at acpi(4). Avoids
having to add many more _HID entries to the match table.

ok deraadt@, mlarkin@


# 1.355 10-Jul-2018 kettenis

Make legacy interrupts work in acpipci(4).

ok patrick@


# 1.354 05-Jul-2018 kettenis

Add acpipci(4), a driver that supports generic ECAM-compatible PCI host
bridges based on information provided by ACPI.

ok mlarkin@


# 1.353 04-Jul-2018 kettenis

Properly pass around the PCI "chipset tag" in acpi(4) and refactor
acpimcfg(4) to call an MD initialization functions that sets up a tag for
PCI ECAM.

ok guenther@, mlarkin@, krw@


# 1.352 01-Jul-2018 kettenis

Move acpi(4) attach glue into acpi_machdep.c.

ok guenther@, deraadt@


# 1.351 01-Jul-2018 kettenis

Add support for _DSD Device Properties.

ok mlarkin@


# 1.350 30-Jun-2018 kettenis

Add intr_enable() function, intended for MI use to amd64 and i386 and use
this in the acpi(4) suspend/resume code paths.

ok deraadt@


# 1.349 29-Jun-2018 kettenis

Use <stdint.h> types.

ok mpi@, jung@, krw@, deraadt@


# 1.348 29-Jun-2018 mlarkin

ACPI: Allow (with warning) GPE handler reassignment, instead of returning
a failure code.

ok kettenis


# 1.347 26-Jun-2018 mlarkin

knf


# 1.346 26-Jun-2018 kettenis

Let acpi(4) pass down the bus dma tag.

ok deraadt@


# 1.345 25-Jun-2018 kettenis

Reorganize acpi(4) code a bit in preparation for upcoming arm64 support.

ok deraadt@


# 1.344 20-May-2018 kettenis

Wake up the acpi thread after scheduling a gpio event.

ok mpi@


# 1.343 17-May-2018 kettenis

Sprinkle some #ifndef SMALL_KERNEL such that RAMDISK kernels cuild again.

pointed out by stsp@


# 1.342 17-May-2018 kettenis

Implement GenericSerialBus OpRegion support.

ok mlarkin@


# 1.341 27-Mar-2018 kettenis

Add acpicmos(4), a driver that implements SystemCMOS OperationRegion
access support. This fixes machines where the AML doesn't check whether
support for this OperationRegion type has been registered by the OS.

ok mlarkin@


Revision tags: OPENBSD_6_3_BASE
# 1.340 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.339 10-Feb-2018 deraadt

Syncronize filesystems to disk when suspending. Each mountpoint's vnodes
are pushed to disk. Dangling vnodes (unlinked files still in use) and
vnodes undergoing change by long-running syscalls are identified -- and
such filesystems are marked dirty on-disk while we are suspended (in case
power is lost, a fsck will be required). Filesystems without dangling or
busy vnodes are marked clean, resulting in faster boots following
"battery died" circumstances.
Tested by numerous developers, thanks for the feedback.


# 1.338 08-Feb-2018 deraadt

After flushing disks, try again to release all memory. We don't need to
save dirty memory to the hibernate space.


# 1.337 08-Feb-2018 deraadt

Toss all releaseable memory, because fragmentation can get in the way
of allocating the hibernate playpen.


# 1.336 08-Feb-2018 deraadt

sr_quiesce() is a new approach for ensuring that softraid drains
output to the disks.

This is part of a larger suspend/resume filesystem-safety diff, which
has been worked on for a couple of months already. Tests by job, krw,
beck, benno, and others. Sometimes even by snapshot users...


# 1.335 29-Nov-2017 kettenis

Add support for GPIO-signaled events.

ok mlarkin@


# 1.334 16-Nov-2017 jcs

Add PCI attachment for dwiic(4) needed by Intel 100 Series machines

ok kettenis


# 1.333 14-Nov-2017 kettenis

Add a few more PNP IDs to the skip lists.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.332 17-Aug-2017 stsp

Make dwiic(4) attach its ACPI parent dependencies.
This change makes the Asus E200HA keyboard work.

Original analysis and patch by Cesare Gargano
ok kettenis@


# 1.331 09-Aug-2017 dcoppa

Add "PNP0303" (8042 PS/2 Controller) to acpi_isa_hids[]

ok kettenis@


# 1.330 22-Jul-2017 jcs

acpibat: look for _BIX first before falling back on _BIF

ACPI 4.0 deprecated _BIF for battery status so some newer machines
are exporting _BIX which extends _BIF's package with a few extra
fields. When using _BIX, export the new discharge cycle count as a
sensor.

feedback from kettenis and anton
ok deraadt


# 1.329 20-Jul-2017 kettenis

Start a list of isa(4) device names such that we can avoid printing them
as "not configured".

ok mikeb@, millert@, deraadt@


# 1.328 08-Apr-2017 deraadt

Sensors are run as callbacks inside tasks. During suspend a sensor
could be running inside a driver that will be force-detached, or due
to tsleep end up disrupting the softstate/hardstate contract. At
suspend time, quisce all these callbacks by waiting for completion.
This issue has never been observed for real, but may be implicated in
suspend/resume failures.
ok kettenis guenther mlarkin


# 1.327 08-Apr-2017 deraadt

Two prototypes for acpi_maptable; merge to one place


Revision tags: OPENBSD_6_1_BASE
# 1.326 27-Mar-2017 deraadt

Now that hibernate_alloc() only has clean success/failure, don't
need to call hibernate_free() to clean up a partial mess.
ok mlarkin kettenis


# 1.325 13-Mar-2017 deraadt

allow ramdisks to build; ok jcs


# 1.324 12-Mar-2017 jcs

try to attach acpisbs first and if successful, don't attach acpibat


# 1.323 02-Mar-2017 natano

Add a new sysctl machdep.lidaction. The sysctl works as follows:

machdep.lidaction=0 # do nothing
machdep.lidaction=1 # suspend
machdep.lidaction=2 # hibernate

lidsuspend is just an alias for lidaction, so if you change one, the
other one will have the same value. The plan is to remove
machdep.lidsuspend eventually when people have upgraded their
/ets/sysctl.conf.

discussed with deraadt, who came up with the new MIB name
no objections mlarkin
ok stsp halex jcs


# 1.322 28-Feb-2017 natano

Untangle abstract sleep modes from ACPI sleep states. This paves the way
for further work in the area.

idea and ok deraadt
ok mlarkin


# 1.321 22-Feb-2017 jcs

add acpisbs, an acpi smart battery subsystem driver reading data
over smbus

currently disabled because it conflicts with acpibat

ok deraadt, kettenis


# 1.320 14-Jan-2017 kettenis

Revert aml_rdpciaddr changes; breaks several machines that were working
before.


# 1.319 11-Jan-2017 jsg

Increase the size of the buffers used for _HID and _CID strings to be
able to fit "Hyper_V_Gen_Counter_V1".

ok mlarkin@ deraadt@


# 1.318 08-Jan-2017 kettenis

Make aml_rdpciaddr() work for for devices behind bridges. The code now
uses the mapping between AML nodes and PCI devices that we establish.
Because _INIT methods may end up calling aml_rdpciaddr(), make sure we
create that mapping early. Also handle devices that aren't actually present.
These devices are now included in the mapping and reads will return an
all-ones patterm whereas writes are a no-op..

ok mlarkin@


# 1.317 25-Oct-2016 pirofti

Add the resource index to aml_parse_resource() callback function.

This is needed by an upcoming acpiec commit that handles machines breaking
the current ACPI specifications.

Change suggested by and ok kettenis@, guenther@


# 1.316 18-Sep-2016 guenther

If the APIC table (MADT) isn't present then ci_acpi_proc_id won't be set
when acpicpu tries to attach, leading to a NULL dereference. The ACPI
processor object on the Dells where this was reported is bogus and useless
anyway so apply the same test when deciding whether to attach to processor
objects as acpicpu would when matching them to cpus.

Problem reported by Eivind Eide (xenofil (at) gmail.com) and
Johan Huldtgren (johan+openbsd-bugs (at) huldtgren.com).
Testing by Eivind Eide.
ok kettenis@ mlarkin@


# 1.315 03-Sep-2016 naddy

Write the system time back to the RTC every 30 minutes.
This fixes the problem that long-running machines which were not
shut down properly would reboot with a badly offset system time.

hints and ok kettenis@


# 1.314 31-Aug-2016 mlarkin

Don't put a device in the wake device list if _STA says it's not there.

Fixes at least one machine that woke up from sleep / poweroff immediately.

ok kettenis@, tested by kettenis and martijn@


# 1.313 28-Jul-2016 kettenis

Store the acpi processor ID/UID in struct cpu_info, and use it to attach
acpicpu(4) drivers to the right cpu(4).

ok mlarkin@, guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.312 10-Jun-2016 kettenis

The power management timer has been made optional in ACPI 5.0A, so don't
attempt to attach acpitimer(4) if the timer isn't present.

Pointed out by jcs@

ok jcs@


# 1.311 08-May-2016 kettenis

Make sure devices listed by a device's _DEP method are attached before
we attach the device itself. The _DEP methode, introduced in ACPI 5.0,
is a hint that the device in question depends on OpRegion support from
the devices listed by _DEP, so we have to order them the proper way.
To prevent us from attaching those devices again when we encounter them
later walking down the device tree, keep track of our attempts to
attach a device driver to them using aflag in the aml_node structure.

ok guenther@


# 1.310 25-Apr-2016 pirofti

We attach acpihpet(4) and acpiec(4) without using the HID, so do not
report it in the dmesg.

OK deraadt@, mlarkin@.


# 1.309 02-Apr-2016 kettenis

Reduce dmesg spam by knocking out some acpi hardware IDs that don't need a
driver.

ok mpi@


# 1.308 29-Mar-2016 kettenis

Check if a device is present (using _STA) in acpi_foundhid. This prevents
us from reporting devices as "not configured" that aren't actually present,
and allows us to remove duplicated code from several drivers.

ok deraadt@, ok jsg@


# 1.307 29-Mar-2016 kettenis

Implement a cleaner way to prevent acpivout(4) from attaching if we have
a platform-specific method of controlling the backlight.

ok millert@, mlarkin@, jung@


# 1.306 28-Mar-2016 kettenis

Adding each and every new acpi device driver to acpi_foundhid() is
gettinga bit of the burden. And it really isn't how our autoconf
framework is supposed to work for busses that can be enumerated. All
the drivers already check for a matching _HID in their attach
function. So we can just drop the checks in acpi_foundhid() and let
the drivers sort themselves out.

Print a "not configured" line for devices for which we don't attach adriver.
This may be a bit much as there are devices for which we will not have a
driver. This will be removed if it causes too much dmesg spam.

ok deraadt@, mlarkin@


Revision tags: OPENBSD_5_9_BASE
# 1.305 17-Jan-2016 jsg

Take acpi_foundhid() outside of #ifndef SMALL_KERNEL so the ramdisk
kernel can find and attach dwiic(4) and sdhc(4).


# 1.304 17-Jan-2016 jsg

remove an unused global variable


# 1.303 14-Jan-2016 kettenis

Attach dwiic(4) on the Intel Bay Trail i2c controllers as well.


# 1.302 12-Jan-2016 kettenis

Improve emulation of PM registers on Hardware-redcued ACPI by emulating
SPL_TYPx, SLP_EN and WAK_STS using the new SLEEP_STATUS_REG and
SLEEP_CONTROL_REG registers. Makes the transition into S5 (powerdown) work
on the ASUS X205TA.

ok deraadt@, mlarkin@


# 1.301 12-Jan-2016 jcs

Add dwiic, a driver for the Synopsys DesignWare i2c controller found
on the Samsung ATIV Book 9 laptop. This initial version only
supports ACPI config/attachment.

Add ihidev, a HID-over-i2c driver largely based on uhidev. dwiic
handles attaching ihidev devices found in ACPI.

Add ims, a HID-over-i2c mouse/trackpad driver to get basic cursor
and button functionality from HID-compliant i2c trackpads.

ok kettenis deraadt


# 1.300 11-Jan-2016 kettenis

Make sdhc(4) attach to hardware IDs "80860F14" and "PNP0FFF".


# 1.299 09-Jan-2016 kettenis

Initial support for Hardware-Reduced ACPI. This skips the legacy mode checks
by emulating the PM_CNT register such that SCI_EN is always enabled. It also
skip attaching acpitimer(4) since the relevant registers are no longer there.

ok deraadt@, mlarkin@


# 1.298 06-Jan-2016 kettenis

Follow the spec and check the SCI_EN bit to see if ACPI has already
been enabled and don't bother enabling it again if it is. This should fix
systems that don't implement legacy mode and therefore set ACPI_ENABLE and
ACPI_DISABLE to zero.

ok mlarkin@, yasuoka@


# 1.297 23-Nov-2015 reyk

Back out the CRITICAL change, turn it back from 10% to 25%. Some
people with old/broken batteries didn't like this change, and the
underlying heuristics are wrong.


# 1.296 22-Nov-2015 reyk

The "CRITICAL" APM battery level, that is exported/faked by the ACPI
stack, is currently set to 25%. With 25% battery left, a typical
modern laptop will have at least one hour runtime left. It is an
arbitrary value, and other APM power layers in the kernel set CRITICAL
to something like 5% or 10%, so set it to 10% for now.

OK mlarkin@


# 1.295 28-Sep-2015 deraadt

In low-level suspend routines, set cold=2. In tsleep(), use this to
spit out a ddb trace to console. This should allow us to find suspend
or resume routines which break the rules. It depends on the console
output function being non-sleeping.... but that's another codepath which
should try to be safe when cold is set.
ok kettenis


# 1.294 06-Sep-2015 deraadt

clarify malloc arguments (base + length...)


# 1.293 28-Aug-2015 deraadt

fairly simple sizes for free(); ok tedu


# 1.292 20-Aug-2015 kettenis

Respect the access size when reading or writing to pci config space.
This version also makes sure writes are properly aligned, preventing
the panics seen with the previous incarnation of this change.

Fixes battery status passthrough in vmware, brightness keys on my x220
and who knows what else.

ok mlarkin@, deraadt@


Revision tags: OPENBSD_5_8_BASE
# 1.291 04-Aug-2015 deraadt

If we are setting ACPI_PM1_BM_RLD do it only once, and remember to
repeat this action at resume time.
ok mlarkin guenther


# 1.290 29-Jul-2015 deraadt

Something subtle in previous revision is locking up suspend or booting
on some laptops. Mailing lists threads mention x220 and Asus machines.
Reverting for now.


# 1.289 21-Jul-2015 kettenis

iRespect the access size when reading or writing to pci config space.
Fixes battery status passthrough in vmware and who knows what else.

Tested by many.

ok mlarkin@, deraadt@


# 1.288 17-Jul-2015 mlarkin

fix comments


# 1.287 26-Mar-2015 jsg

Use the 0x10 extended capability to determine if the trackstick
buttons are wired to the trackpad and need to be re-routed to the
trackstick. Without this change the buttons on 2015 thinkpads
get picked up as extended buttons that show up as scroll up/down.

Remove the X1 Carbon 2015 (LEN0048) and X250 (LEN0046) from the
top button area/soft buttons quirks list. Also avoid using the
quirk list entirely if the capability bit is set.

Based on work from jcs@ with feedback from mpi@
ok mpi@ jcs@


# 1.286 20-Mar-2015 kettenis

The ACPI standard says that a PCI function number of FFFF can be used to refer
to all functions on a device. This makes no sense in the context of accessing
PCI config space. Nevertheless stsp@ has a machine with AML that tries to
do config space reads from a device where _ADR encodes function number FFFF.
This is almost certainly buggy AML, but since Linux (and presumably Windows)
boot just fine on this machine we need to work around this. Linux effectively
treats function FFFF as function 7, which isn't present on this machine.
Instead we simulate a read from a non-existent device by returning a bit
pattern of all ones. Writes to function FFFF will still panic.

ok mlarkin@


# 1.285 16-Mar-2015 deraadt

Reduce use of SMALL_KERNEL in acpi, bringing acpiec driver and more gpe
handling into RAMDISK. This is now possible because the install media
has ample room. The goal is to reduce special cases where we may be
skipping (unknown) important operations...
ok mlarkin kettenis


# 1.284 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.283 07-Feb-2015 deraadt

New framework that allows hibernate to pass in entropy from it's fresh
boot.
ok mlarkin


# 1.282 06-Feb-2015 mlarkin

Fix a hibernate crash on some machines due to unmapping a page that
may not have been mapped previously (in the failure to hibernate case).

Also ensure that the lowmem ptp is mapped in all cases (not just MP).

ok kettenis


# 1.281 17-Jan-2015 deraadt

resume_randomness() before spinning up other cpus...


# 1.280 15-Jan-2015 jsg

Define a new wscons mouse type for Synaptics clickpad devices that lack
physical buttons. This will be used if the acpi pnpid for the mouse
matches a list provided by Synaptics found in Linux.

Combined with the xenocara changes this will remove the need for an xorg
config file for the x240/t440/t540 generation of thinkpads.

ok matthieu@ shadchin@ kettenis@


# 1.279 11-Jan-2015 kettenis

Tolerate checksum errors in ACPI tables (but complain about them). Apparently
there are machines out there with broken BIOSen with such checksum errors in
thier RSDT tables. Windows runs fine on these machines, so it probably does
not check. Linux ignores checksum errors as well (and prints a warning).

ok deraadt@, krw@


# 1.278 18-Dec-2014 deraadt

suspend and resume the RNG (at what looks like the right spots)
ok reyk djm


# 1.277 09-Dec-2014 doug

Replace some malloc(n*size,...) calls with mallocarray().

ok tedu@ deraadt@


# 1.276 06-Dec-2014 mpi

If "machdep.lidsuspend" is not set, do not go back to sleep if the lid
is still closed when resuming. This allows docked laptop to be resumed
without opening them.

ok mlarkin@


# 1.275 23-Nov-2014 mlarkin

If a machine has no _LIDs, then none can be open. This diff fixes a problem
where machines without any _LIDs were going back to sleep after hibernate
due to the recent suspend-if-lid-closed-on-resume diff. Noticed on qemu,
which doesn't have any _LIDs.

discussed with deraadt


# 1.274 18-Nov-2014 krw

Nuke yet more obvious #include duplications.

ok miod@


# 1.273 08-Nov-2014 mlarkin

If resuming from sleep (zzz/ZZZ) and the lid is still closed, go back to
sleep. This prevents accidental lid flex or slight opening in a backpack
from waking the machine up and leaving it resumed (powered on).

ok deraadt@


# 1.272 26-Sep-2014 kettenis

Rework piglet and pig allocation. Currently the piglet gets allocated
deep down in the suspend path, where it is really hard to recover from
allocation failure. So allocate the piglet early on in the suspend path.
Also change the piglet and piglet allocation functions to use km_alloc(9)
instead of doing pmemrange magic. This removes a bunch of code which, in the
case of the piglet allocation, is broken since it results in a NULL pointer
dereference. Also switch the piglet allocation to not wait. If we can't
allocate 16MB of phys contig memory on a halfway modern machine we're almost
certainly under a lot of memory pressure and we're better off not trying to
hibernate anyway.

ok mlarkin@


# 1.271 25-Sep-2014 kettenis

Bring back rev 1.249. Now that mp_setperf() has been fixed, it should be
safe to restore perflevel when resuming. Should fix the issues people
see with the processor running at full speed heating up the machine while
the apm command reports that the CPUs are running at a low speed.

ok dcoppa@, deraadt@


# 1.270 23-Sep-2014 kettenis

Release the acpi lock around calls to wsdisplay_suspend() and
wsdisplay_resume(). The X server may adjust the display brightness just
before/after the VT switch when suspending/resuming. In fact, it may
already be blocked adjusting the display brightness for some reason
unrelated to the suspend request.

The VT switch request will eventually time out, but the delay is annoying
and the failed VT switch will lead to problems after resume.

Needed for a future update of the xf86-video-intel driver.

ok mlarkin@


# 1.269 19-Sep-2014 kettenis

Use config_suspend_all(9).


# 1.268 08-Sep-2014 kettenis

Clear the PME Status bit on PCI devices in response to Device Wake events.
Fixes the interrupt storm on the 2009 Mac mini with WOL enabled on its
nfe(4) interface.

ok mlarkin@


Revision tags: OPENBSD_5_6_BASE
# 1.267 20-Jul-2014 mlarkin

Support hibernating to softraid crypto volumes.

much help and ok from deraadt@


# 1.266 16-Jul-2014 mlarkin

Reenable hibernate RLE support and flush+zero all memory after unpack.


# 1.265 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.264 12-Jul-2014 kettenis

Add a function to drop all clean pages on the page daemon queues and call
it when we hibernate.

ok mlarkin@, miod@, deraadt@


# 1.263 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.262 11-Jul-2014 mlarkin

Flush the buffercache to 16MB on hibernate and restore its previous max
size (kern.bufcachepercent) on resume, for better hibernate performance.

ok beck@


# 1.261 10-Jul-2014 blambert

remove unneeded header

ok mlarkin@


# 1.260 01-Jul-2014 kettenis

Adding an additonal delay after each access to SystemMemory or SystemIO
during autoconf makes no sense.

ok deraadt@


# 1.259 23-Jun-2014 kettenis

SystemMemory is used to access memory mapped registers on some machines, so we
must use the bus_space(9) API to access it instead of memcpy(9). Also make
sure we properly align access to fields that need more than byte alignment.

ok guenther@


# 1.258 21-May-2014 mlarkin

Fix invalid / missing format specifiers in several dnprintf statements
(used only when ACPI_DEBUG is enabled)

From a diff on tech@ by Fabian Raetz, with a minor change by me, tested
on i386 and amd64 with ACPI_DEBUG enabled and disabled.


# 1.257 25-Apr-2014 mlarkin

Defer the free of the hibernate structures until later, when it is safe to
do so.

Freeing the hibernate structures too early leads to inadvertent calls into
splx when we aren't ready for them yet. Found when trapping splx calls in
the resume path.

discussed with deraadt@ at m2k14


# 1.256 13-Apr-2014 mlarkin

Fix a badly worded debug message that implied the entire machine was going
to sleep, instead of just the acpi thread going to sleep.


# 1.255 31-Mar-2014 mpi

Change the order of operations during a suspend/resume cycle and
call bufq_quiesce() after executing the DVACT_QUIESCE handlers.

This should be safe since no disk nor controller drivers have such
handler but it will allow us to detach sd(4) devices attached to a
USB bus.

Another benefit pointed out by kettenis@ is that drivers that need
to read a firmware from the disk should be able to do it at resume
time in a DVACT_WAKEUP handler.

ok kettenis@, deraadt@


# 1.254 13-Mar-2014 dlg

get rid of the assumption that the head of the alldevs list is the
"mainbus" device. this breaks when mpath is enabled because it
attaches before mainbus and therefore takes the head position.

have autoconf provide device_mainbus() which looks up mainbus_cd,
and use that instead.

discussed with deraadt who just wants mpath stuff to move forward
despite there being many ways to shine this particular turd.


Revision tags: OPENBSD_5_5_BASE
# 1.253 21-Feb-2014 deraadt

Do the lid control further on in the resume sequence, since this is
AML, and will do WAITOK operations. While cold, that is a bad idea.
We seem to be safe with respect to the spec, I do not see any event
debounce issues here.
ok kettenis, tested by many


# 1.252 20-Jan-2014 claudio

Instead of checking sc->sc_revision <= 1 check that the
fadt->hdr_revision <= 1. This is a better check since it checks
the version of the proper descriptor. This allows the alix APU
to reboot since those boards have currently a wrong sc->sc_revision.
Committing this now so that it can be tested easier.
With and OK jsg@ also fine by derradt@


# 1.251 24-Dec-2013 kettenis

If the FADT has its SMI_CMD set to zero, assume we're only ACPI-only hardware
and don't need to disable SMI ownership of the ACPI hardware registers.

ok mlarkin@


# 1.250 23-Dec-2013 kettenis

Revert previous commit. It makes resume hang on some MP systems running
GENERIC.MP.


# 1.249 16-Dec-2013 kettenis

Restore hw.setperf upon resume like we do for apm(4) on i386. Seems we need
to do this fairly late in the resume to avoid hangs.

ok deraadt@, mlarkin@


# 1.248 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


# 1.247 06-Nov-2013 mpi

Add support for Power Resources for Dx states and the necessary hook
for PCI devices. This hook should be called twice, before and after
changing the power state of a PCI device.

Before setting the device to the new state, the ACPI layer will notify
every power resources linked to the device for that state and make sure
they are turned "_ON". After changing the state of the device, it will
decrement the reference of every power resources linked to that device
for the old state and turn them "_OFF" if they are no longer referenced.

This fixes the no-USB after resume problem seen on various ThinkPad,
problem initialy diagnosed with Alexander Polakov.

ok kettenis@, deraadt@


Revision tags: OPENBSD_5_4_BASE
# 1.246 01-Jun-2013 mlarkin

Stop printing ACPI wakeup devices in dmesg after the 16th wakeup device.

Helps with VMware VMs that report hundreds of wakeup devices.

ok reyk@, deraadt@


# 1.245 31-May-2013 bluhm

Fix the build for a kernel without wd and pciide in its config.
OK deraadt@ tedu@


# 1.244 30-May-2013 deraadt

Enforce ca_activate tree-walks over the entire heirarchy for all events,
cleaning up some shutdown-hook related code on the way.
(A few drivers related to sparc are still skipped at kettenis' request)
ok kettenis mlarkin, tested by many others too


# 1.243 18-Apr-2013 deraadt

pin the acpi thread to the BSP, because AML and SMI cannot be trusted. I
was certain we were doing this already, but it turns out no...


Revision tags: OPENBSD_5_3_BASE
# 1.242 24-Dec-2012 guenther

In acpiioctl(), Return though the splx() path if suser() returns an error

ok deraadt@


# 1.241 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


# 1.240 04-Oct-2012 ehrhardt

Use information provided by ACPI to attach secondary PCI host bridges.

ok kettenis@


# 1.239 07-Sep-2012 kettenis

Add a new function called acpi_pci_min_powerstate() that returns the lowest
power state for a PCI device for the power state (currently S3 or S4) we're
transitioning into. This function is probably too simplistic; there is a lot
of (silly) complexity in the ACPI spec.

ok deraadt@


Revision tags: OPENBSD_5_2_BASE
# 1.238 13-Jul-2012 pirofti

Disable acpivideo(4) on ASUS laptops.

There are some models that panic whenever an event is received.
Some cope, some don't. In order to get this right acpiec(4) requires
major rework. Until then let machines boot.

Discussed with deraadt@.


# 1.237 11-Jul-2012 mlarkin

for now, restrict hibernate to root in the acpi ioctl kernel interface.
normal rules for controlling this interface through apmd still apply.

ok deraadt@


# 1.236 09-Jul-2012 deraadt

tiny little oops in untested hibernate block


# 1.235 09-Jul-2012 deraadt

clean up ioctl() case statements


# 1.234 09-Jul-2012 deraadt

acpi_disable_onegpe() is not used; ok pirofti


# 1.233 24-May-2012 kettenis

If _MIN > _MAX consider a WordBus resource to be invalid. Fixes a panic on
the IBM x3550-M4.

ok mikeb@


# 1.232 29-Mar-2012 mlarkin

Missing #ifdef in previous diff


# 1.231 29-Mar-2012 mlarkin

Prevent hibernation attempts on machines with unsupported disk controllers.
The same check is also performed later in the hibernate sequence, but by
then it's too late to cleanly unwind (presently).

ok deraadt


# 1.230 26-Mar-2012 deraadt

simplify conditionals in acpi_sleep_state()
ok mlarkin


# 1.229 26-Mar-2012 deraadt

hook in the hibernate request code; half of this diff is from mlarkin
ok mlarkin


Revision tags: OPENBSD_5_1_BASE
# 1.228 20-Sep-2011 deraadt

Very early in suspend while the clock is still ticking realtime,
save the clock back to the rtc. The zaurus already did this.
ok phessler miod kettenis


Revision tags: OPENBSD_5_0_BASE
# 1.227 02-Jul-2011 nicm

kqueue attach functions should return an errno or 0, not a plain 1. Fix
the obvious cases to return EINVAL and ENXIO.

ok tedu deraadt


# 1.226 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.225 16-Jun-2011 pirofti

Import acpitoshiba driver.

This is originally written by Hiroyuki Aizu, ported and integrated
in our tree by Javier Vazquez <javama4 AT gmail> with tweaks and
suggestions by me.

Import prodded by deraadt@ and the lack of testers on tech@.


# 1.224 27-Apr-2011 jcs

attach acpithinkpad to newer lenovo models like the x120e

ok deraadt@


# 1.223 22-Apr-2011 jordan

Fix uninitialzied variables and formatting strings (-Wxxx errors)


Revision tags: OPENBSD_4_9_BASE
# 1.222 02-Jan-2011 jordan

Re-add code for ACPI taskqueue, eliminates need for state vars in acpi softc
Serialize processing ACPI tasks for sleep/powerdown/GPE event
ok deraadt@


# 1.221 31-Oct-2010 guenther

Revert last commit: it breaks resume on Thinkpads
"then please back it out" deraadt@


# 1.220 26-Oct-2010 jordan

Add task queue for ACPI gpe and notify handlers
This fixes eject on Dell Latitude dock and an issue on the Dell Mini
battery update.
ok deraadt


# 1.219 07-Oct-2010 jordan

Create separate functions for enabling wake and runtime gpes.
Remove old suspend/resume gpewalk function
ok deraadt, mlarkin


# 1.218 05-Oct-2010 jordan

split acpi_enable_onegpe to separate enable/disable functions
ok deraadt


# 1.217 05-Oct-2010 jordan

Added acpi_disable_allgpes function to clear GPE status
ok deraadt


# 1.216 05-Oct-2010 deraadt

clean up acpi_softc use more


# 1.215 05-Oct-2010 deraadt

delete a bit of #if 0 code


# 1.214 05-Oct-2010 deraadt

hide use of our global acpi_softc pointer a bit better


# 1.213 29-Sep-2010 deraadt

grab the acpi thread lock very early in acpi thread startup; not that it
is likely that anyone will race us... but why be careless.
ok kettenis


# 1.212 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.211 29-Aug-2010 jasper

- add a check for FWRITE here too.

ok miod@


# 1.210 27-Aug-2010 kettenis

Make PCI Power Management optional, and only enable it when acpi(4) attaches.

ok deraadt@


Revision tags: OPENBSD_4_8_BASE
# 1.209 08-Aug-2010 kettenis

Close race between the acpi thread and wscons ioctls running in process
context. We might rip this out post-release and replace it with a solution
that serializes things in a more obvious way.

ok deraadt@, miod@ and "those crazy texans who ok anything"


# 1.208 08-Aug-2010 deraadt

Report the acpi sleep states when on a RAMDISK; ok krw


# 1.207 07-Aug-2010 kettenis

Enforce acpi register access rescrictions.

ok mlarkin@, deraadt@, tested by marco@


# 1.206 07-Aug-2010 deraadt

upon resume, notify all the acpiac and acpibat drivers to update
their status. some laptops do not do this automatically.
ok kettenis canacar


# 1.205 06-Aug-2010 marco

Always call _PSW on _LID devices that support it to enable lid open to
resume the machine. Conversely disable it when the machine wakes up.

Tested by several ok deraadt


# 1.204 06-Aug-2010 deraadt

As part of a revamp of the PM1/GPE code, I write this diff to look for
unmanaged EN & STS bits in the PM1 register at interrupt time and report
them. As a side effect this splits the STS acknowledgement into two writes
(for power, and sleep) instead of one. The printf that is added (to spot
unmanaged STS bits) has not yet been triggered as far as we know.

Before the "write to PM1 registers at the right offset" diff went in, this
was not neccessary. But newer thinkpads do not have a working soft power
button without this diff.

We have no idea why.
ok mlarkin kettenis


# 1.203 05-Aug-2010 deraadt

PM1 power button events were generating 'fake events' to acpibtn PWRB,
but if there is no such device these were being tossed. Instead, arrange
for all power button handlers to flag a softc variable which is processed
by the thread; so that one place will know to request a powerdown (psignal
to init)
ok marco mlarkin


# 1.202 05-Aug-2010 deraadt

rename a variable which is annoyingly poorly named


# 1.201 05-Aug-2010 mlarkin

Write to the correct offset for various ACPI pmreg registers. Writing to
the correct register does properly clear the wake status bits and fixed
event bits, which allows some machines to have more than 1 suspend cycle.

bug spotted by deraadt@

tested by deraadt@ and myself on a half-dozen machines and others on
hackers@

ok deraadt@, marco@


# 1.200 05-Aug-2010 deraadt

Fix the power button. The pm1 register is 16 bits in size.
My mistake, a while back.
tested by krw


# 1.199 04-Aug-2010 kettenis

Print supported sleep states.

ok marco@, phessler@, deraadt@


# 1.198 03-Aug-2010 mlarkin

Use the proper flag for re-enabling certain hardware events (power btn,
sleep btn). The (incorrect) flag prevoiously used coincidentally had the
same value, so this shouldn't cause any different behavior than before.

tested on a variety of machines (i386, amd64, sp, mp) - no behavioral
change seen

ok deraadt@


# 1.197 28-Jul-2010 marco

Make all gasio failures look and behave the same.


# 1.196 28-Jul-2010 mlarkin

Delay a little longer on suspend before giving up. Fixes machines that
sometimes don't suspend because the sleep transition takes too long
(symptoms like failing to sleep but the backlight stays on).

ok deraadt@


# 1.195 27-Jul-2010 deraadt

remove XXX; EC can now be attached early when needed; ok jordan


# 1.194 27-Jul-2010 deraadt

more brackets feels good


# 1.193 27-Jul-2010 deraadt

XXX annotate a block in acpi_enter_sleep_state() that is pretty worrying


# 1.192 27-Jul-2010 deraadt

horrific indentation that nearly made me puke


# 1.191 27-Jul-2010 mlarkin

Set the sleeping indicator light on machines that support it, in case SMI
doesn't do it for us. Shows the 'moon' LED on older Thinkpads when
sleeping.

Tested on T43p, W500, T510 by me and on X61s by deraadt@.

ok deraadt@


# 1.190 27-Jul-2010 jordan

Early initialization of acpiec if ECDT table exists
Fixes hang when booting thinkpads while docked
ok deraadt


# 1.189 26-Jul-2010 deraadt

remove merge error which was commited


# 1.188 26-Jul-2010 pirofti

Add support for Sony ACPI hotkeys via a new driver: acpisony(4).

Currently it only works for the suspend button and tries to do right for
the brightness events, but I haven't found a Sony laptop that like to do
right so far.

In the future I want to make the brightness keys work on all Sony's and
also add support for the zoom hotkeys and whatever other funky keys I can
find on those things.

Okay deraadt@.


# 1.187 22-Jul-2010 deraadt

pretty large cleanup of SMALL_KERNEL


# 1.186 21-Jul-2010 deraadt

in the acpi GPE handler, late ack level interrupts not edge interrupts.
Duh.


# 1.185 21-Jul-2010 deraadt

Merge the edge and level gpe handlers into one (now possible since the
gpe structure tells us whether the gpe is edge or level)


# 1.184 21-Jul-2010 deraadt

delete #if 0 code we are not going to use; ok marco


# 1.183 20-Jul-2010 deraadt

make the mask code for acpi_enable_onegpe() match other similar code


# 1.182 20-Jul-2010 deraadt

Handle the acpi interrupt controller with proper edge/level handling,
wrapped in spltty to avoid racing against the interrupt controller.
Repair the gpe bit masking code while there, and do operations in the
right order
ok kettenis mlarkin, help from jordan at figuring out the order of
operations.


# 1.181 20-Jul-2010 matthew

When we're preparing to go to sleep, only call bufq_restart() if other
preparations failed.

ok kettenis@, thib@, deraadt@


# 1.180 19-Jul-2010 deraadt

permit compilation with ACPI_DEBUG


# 1.179 19-Jul-2010 deraadt

there is no need for a seperate dsdt_softc pointer; dsdt does not have a
softc but is just a part of the single acpi driver.
ok jordan


# 1.178 19-Jul-2010 deraadt

acpi_delay() is no longer needed; ok jordan


# 1.177 18-Jul-2010 mlarkin

Clear event status on resume, to avoid going back to sleep/reboot/shutdown
immediately after resume. Tested by a bunch of people on hackers@ on
various hardware. Fixes at least Toshiba NB200 and Gateway LT3103u.

ok marco


# 1.176 13-Jul-2010 deraadt

rename some thread related interfaces so they are logical


# 1.175 10-Jul-2010 jordan

Verify _ADR bdf is correct (some systems return 0xffff)
Works on Dell x9xx and Studio 1555


# 1.174 09-Jul-2010 jordan

Clear the noise


# 1.173 08-Jul-2010 jordan

Add mapping for ACPI device to PCI bdf (match autoconf tree)
Simplify resource parsing function to use buffer argument
Convert namespace linked lists to use queue macros
ok marco@, deraadt@


# 1.172 06-Jul-2010 deraadt

Use spltty() to lock downcalls from apm against the information being
modified by the acpi thread.
ok kettenis


# 1.171 06-Jul-2010 deraadt

Don't use a workq to do the suspend, because it races aginst the acpi
thread. Instead, just tell the acpi thread to do the suspend for us.
This makes apmd & zzz work correctly. While here, have acpithinkpad
attempt to post the event to apm before prompting the suspend itself.
ok kettenis marco mlarkin


# 1.170 05-Jul-2010 mlarkin

Reset register SCI_EN on resume. The ACPI spec clearly states we don't have
to do this, but many machines don't, or forget to, which leads to all sorts
of problems on resume (broken battery stats, overheating, failure to resume
more than once, etc).

tested by many, ok marco@, deraadt@


# 1.169 01-Jul-2010 thib

do bufq quiesce again, since vnd/the bufq_done call in
biodone has been fixed.


# 1.168 01-Jul-2010 jordan

Backout recent AML changes, breaks on R210 and others


# 1.167 01-Jul-2010 jordan

Simplified aml_parse_resource call, cleanup code
ok marco


# 1.166 01-Jul-2010 jordan

Fix subordinate bus # for multi-root PCI buses
ok marco, kettenis


# 1.165 30-Jun-2010 thib

Disable/partially backout the bufq quiesce changes as this
is causing havoc with vnds and release must be buildable.


# 1.164 29-Jun-2010 deraadt

Fix ramdisks (new code was placed inside a SMALL_KERNEL chunk)


# 1.163 29-Jun-2010 jordan

Add support for mapping ACPI to PCI devices
ok kettenis, deraadt


# 1.162 29-Jun-2010 kettenis

Call bufq_quiesce() on suspend to stop all I/O and bufq_restart on resume.
Makes suspend/resume work much better while doing disk I/O.

ok mlarkin@


# 1.161 29-Jun-2010 tedu

60 is a more reasonable guess for number of minutes in an hour than 100.
spotted by TeXitoi. ok deraadt


# 1.160 27-Jun-2010 jordan

Add _acpi_matchhids routine, precursor to pci device match


# 1.159 27-Jun-2010 mlarkin

Clear ACPI fixed event status on resume (power buttons/etc) so that some
machines don't immediately resume after suspending to S3.

Tested by kettenis@ and I on 5 or 6 machines, deraadt@ "yeah, looks good"


# 1.158 27-Jun-2010 jordan

Cleaned up acpi_maptable, adds to table list directly


# 1.157 07-Apr-2010 deraadt

If we have done a wsdisplay_suspend(), and ACPI decides it does not want to
suspend, we must roll back by calling wsdisplay_resume() or the virtual
console switching will be locked forever
ok oga


# 1.156 07-Apr-2010 kettenis

Make suspend/resume work on MP machines (running an MP kernel). Joint work
from mlarkin@ and me, with some amd64 fixes thrown in by deraadt@

ok marco@, deraadt@, pirofti@, mlarkin@


# 1.155 31-Mar-2010 kettenis

Bring /dev/apm support in line with apm(4). Make the suspend button send
a suspend request event to apmd(8) instead of suspending immediately. Also
keep track of whether /dev/apm and /dev/apmctl are currently open, such that
we can still suspend immediately if apmd(8) isn't running.

ok deraadt@, marco@, pirofti@, jsing@, oga@


# 1.154 30-Mar-2010 oga

Prevent the apmd/x races for good.

When we hit suspend time, go through all wsdisplays on the system. if
they are in mode MAPPED, but not MODE_DUMBFB then if possible do a full
vt switch to a !mapped vt, and prevent switching back until resume time.
This has to be called from MD code because this involves userland
running so that X can run the vt switch signal handler. This way, any
case where we are using the "poke registers from userland" model, we
will not be on the hardware when we go down, so the kernel can actually
handle thing properly.

Tested on several acpi laptops (by kettenis@ and ian@), x40 (me and
beck@ at LEAST) and zaurus (me). Maybe others, but if so I forgot who at
this time..

Idea from deraadt somewhere over the Faroe Islands (I thought of a
similar thing myself a while ago). Much prompting from him. Ok and
comments miod@


# 1.153 25-Mar-2010 oga

Fix & vs. && in the kqfilter code.

Based on a comparison with the apm code.

ok deraadt@, kettenis@


Revision tags: OPENBSD_4_7_BASE
# 1.152 26-Nov-2009 mlarkin

Visual (and on some machines, audio) feedback while the machine is
resuming via calls to aml _SST method. This also makes the "moon" LED on
thinkpads to blink during resume and cease when resume is completed.

ok pirofti@


# 1.151 26-Nov-2009 deraadt

Toggle into "cold" when we we disable interrupts, because tsleeping
in the AML interpreter and context switching into userland processes
tends to sometimes pop the ACPI trampoline page out of our address
space. Makes lots more machines work.
ok kettenis mlarkin


# 1.150 24-Nov-2009 jsg

in the resume path evaluate _WAK before dealing with device drivers
like the spec says, not after.

ok mlarkin@


# 1.149 24-Nov-2009 deraadt

abort suspends on SMP systems until that gets written; ok mlarkin


# 1.148 23-Nov-2009 mlarkin

pci should return the result of it's children's suspend/resume calls back
to its parent.

handle suspend failure case and unwind if devices have an activate function
that returns failure.

ok deraadt@, kettenis@


# 1.147 23-Nov-2009 mlarkin

acpi* devices need to call their children on suspend/resume.
ok deraadt@


# 1.146 23-Nov-2009 pirofti

Remove ACPI_SLEEP_ENABLED checks.

This enables by default the suspend/resume paths in the kernel.

Okay deraadt@.


# 1.145 23-Nov-2009 deraadt

knf and spacing that are super annoying


# 1.144 23-Nov-2009 mlarkin

Hook up Theo's new children suspend/resume function, as well as more ACPI
suspend/resume glue.

ok deraadt@


# 1.143 26-Oct-2009 deraadt

Do not do strncmp comparisons on dv_xname because longer device names which
look similar could arrive in the future. Instead, compare directly against
dv_cfdata->cf_driver->cd_name
Issue originally spotted by miod


# 1.142 26-Oct-2009 jordan

Verify checksum+address when loading ACPI tables.
Some systems had invalid entries in RSDT/XSDT.
ok marco@


# 1.141 23-Jul-2009 cnst

New aibs(4) driver for ASUSTeK AI Booster (ACPI ATK0110) hardware monitoring,
with sensor state support through limits provided by the ACPI.

Tested on several ASUS motherboards kindly networked by Sam Fourman Jr.

ok deraadt marco jordan


Revision tags: OPENBSD_4_6_BASE
# 1.140 03-Jun-2009 pirofti

Import acpi power resource driver. This is needed for suspend/resume logic.

Okay jordan@ and marco@.


# 1.139 03-Jun-2009 pirofti

Add acpivideo support. This adds brightness support for all laptops
except thinkpads, they will use the acpithinkpad driver. The driver is
also hooked into wsconsole. So brightness can be adjusted via:

$ wsconsctl display.brightness=<percentage>

This is very helpfull on some laptops that have a nasty bios and get two
steps instead of one when pressing the brightness button.

Tested on various dell, fujitsu, acer, samsung and other laptops.

Okay marco@, miod@. Suggestions from kettenis@.
Lots of reviews and help from miod@, thanks!


# 1.138 03-Jun-2009 jordan

Added ejectable bay for IDE cdroms
ok marco@


# 1.137 30-Apr-2009 marco

Oga changed apm ioctls and forgot to grep. Temporary work around by hitting
the sleep button when we get any request. This interface will chnage at
some point.
ok oga


# 1.136 19-Apr-2009 krw

Call acpi_sleep_walk() when powering down just like when entering
any other sleep state. This tells acpi wake devices to not wake up
the box until manual powering up resets their status. Makes my Dell
GX520 stay powered off in response to 'halt -p'.

In snaps for a while.

ok marco@ (several times) deraadt@


# 1.135 17-Apr-2009 pirofti

Add device ranking support for future sleep/wake developments.

okay marco@


# 1.134 11-Mar-2009 jordan

Cleanup.. use aml_evalinteger instead of aml_evalname
ok marco@


# 1.133 10-Mar-2009 jordan

Fix cut/paste error.. call _BFS not _PTS
ok marco@


Revision tags: OPENBSD_4_5_BASE
# 1.132 21-Feb-2009 marco

overzealous cut/paste; found by Vladimir Kirillov <proger@uaoug.org.ua>


# 1.131 20-Feb-2009 marco

Oops didn't mean for Debugger() to remain there.


# 1.130 19-Feb-2009 marco

suspend/resume bits so that we can develop this in tree. This is disabled.
code from mlarkin and me
help from art,toby,jordan and several others
ok jordan, go for it deraadt


# 1.129 10-Feb-2009 jordan

Fix null pointer for shutdown if acpi not enabled
ok marco@


# 1.128 04-Feb-2009 kettenis

Some systems that claim to be ACPI 2.0 apparently don't provide an XSDT
address. Make the code fall back on the RSDT on those systems.

From Joerg Niendorf <p3y80@internode.on.net>. Fixes PR 5986.


# 1.127 17-Jan-2009 krw

A couple of stray malloc/memset -> malloc(M_ZERO), a bit of knf and
lose a couple of unnecessary casts. No functional change.

Feedback from kettenis@

ok kettenis@ marco@


# 1.126 06-Nov-2008 marco

Collapse all sanity checks of hids into one function.

Originaly from jordan


Revision tags: OPENBSD_4_4_BASE
# 1.125 02-Jul-2008 fgsch

add acpivideo. for now almost an empty skel so others can work on
suspend/resume. eventually it will also manage output switching and
brightness where supported. prodded by marco@


# 1.124 11-Jun-2008 marco

Fix $OpenBSD$ for once and for all


# 1.123 08-Jun-2008 deraadt

aaa_dev was being set from an uninitialized variable; ok toby


# 1.122 07-Jun-2008 marco

Kill ACPIVERBOSE. It was never used and is worthless to boot.

comments jmc
ok toby


# 1.121 06-Jun-2008 marco

Shrink acpi some more for floppies and while here re-enable acpi on the
floppies. Fixes crazy sony vaio kettenis has.

tested by deraadt
ok deraadt jordan kettenis


# 1.120 01-Jun-2008 marco

Fix \n nit in SMALL_KERNEL.


# 1.119 01-Jun-2008 marco

Re-enable new parser. Lots of testing by various folks. Fits all media
now too.

ok kettenis deraadt


# 1.118 16-May-2008 dlg

back out jordans new parser.

it didnt have enough oks from the right people, it did break machines
that were previously working, and it bloated the RAMDISKS and install
media beyond the constraints of the media.

requested by deraadt@


# 1.117 14-May-2008 jordan

Adding new ACPI Parser code
Old guts of code still exists, needs to be torn out next
ok marco@
ok brad@


# 1.116 27-Apr-2008 jcs

add acpithinkpad(4), an acpi support driver for ibm/lenovo
thinkpads.

provides functionality that the bios did on older, non-acpi
thinkpads like respond to screen brightness keys and toggle the
bluetooth device on and off.

ok deraadt@ and marco@


# 1.115 24-Apr-2008 jsing

Introduce acpiasus(4), a driver for the ACPI based hotkeys found in many
ASUS laptops (including the ASUS EeePC) - largely based on NetBSD's
asus(4) driver. On the ASUS EeePC this allows us to enable/disable
wireless, change screen brightness and use the volume keys.

ok jsg@, weingart@


# 1.114 12-Apr-2008 kettenis

Get rid of acpi_s5 global variables; simply send SIGUSR2 instead of SUGUSR1
to tell init(8) to power down the machine.

ok krw@


Revision tags: OPENBSD_4_3_BASE
# 1.113 21-Jan-2008 jsg

Check against RESET_REG_SUP in FADT before doing the acpi reset dance
after all, not doing so appears to break more machines than it fixes.

ok marco@


# 1.112 05-Jan-2008 kettenis

Never attach more acpicpu(4) devices than cpu(4) devices. Always attach
the first acpicpu(4) device on non-MULTIPROCESSOR kernels, regardless of its
ACPI processor ID.

ok mikeb@


# 1.111 05-Dec-2007 deraadt

Hang acpi(4) below bios(4) instead of mainbus(4). This lets us move acpi(4)
before pcibios(4), and then.... if acpi(4) attaches, skip pcibios(4) since
it messes the machines up. Fixes claudio's HP dl320 G5, and almost assuredly
others.
ok kettenis


# 1.110 05-Dec-2007 deraadt

repair acpiopen/acpiclose and friends to handle the /dev/apmctl interface,
so that apmd(8) can actually work correctly against acpi(4), and thus,
even do the fancy -C/-A stuff; also tested by jmc


# 1.109 02-Dec-2007 fgsch

fix the trailing space for real now.


# 1.108 02-Dec-2007 fgsch

avoid trailing spaces on output; marco@ ok.


# 1.107 27-Nov-2007 kettenis

Fix off-by-one that made us attach acpicpu0 and acpicpu1 on GENERIC kernels.

ok marco@


# 1.106 25-Nov-2007 jsg

When available and ACPI is enabled use the ACPI reset method
to reboot. This is required for some quirky sis machines
and likely others to reboot.

Tested by many. 'commit early commit often' deraadt@


# 1.105 19-Nov-2007 deraadt

sigh, amd64 still uses that stupid acpi_enabled variable...


# 1.104 19-Nov-2007 deraadt

softc's come initialized to 0, you know...


# 1.103 19-Nov-2007 deraadt

store revision; from jsg


# 1.102 19-Nov-2007 kettenis

Initialize sc_facs earlier.

ok deraadt@, beck@, canacar@


# 1.101 16-Nov-2007 deraadt

fix the bus_space #define nightmare, so that amd64 and i386 are much more
uniform. as a result shared code like acpi needs less #ifdef's
ok marco kettenis


# 1.100 15-Nov-2007 deraadt

remove the #ifdef ACPI_ENABLE. the EC codepath is either safe enough (and
will be improved), or it is unsafe because AML assumes it must be there
ok almost everyone


# 1.99 15-Nov-2007 mikeb

Make acpicpu(4) attach to the existing CPUs only.

Problem reported by form@. Fix idea by kettenis@.
Lots of help from deraadt@. Tested by deraadt@, form@ and me.

ok deraadt marco


# 1.98 14-Nov-2007 deraadt

spacing


# 1.97 12-Nov-2007 deraadt

correct : vs , in attach-time messages


# 1.96 06-Nov-2007 deraadt

all ACPI_ENABLE kernels need acpi_interrupt()


# 1.95 06-Nov-2007 deraadt

move acpi_read_pmreg() and acpi_write_pmreg() out of SMALL_KERNEL so
that acpi ramdisks can compile (obvious solution)


# 1.94 05-Nov-2007 canacar

Establish the acpi interrupt earlier. Fixes problems with some
GENERIC.MP kernels not getting acpi interrupts. This is a
workaround until the real issue with late establishment is fixed.
ok weingart@, beck@, gwk@


# 1.93 03-Nov-2007 jordan

Added support for displaying wakeup devices
ok beck@,weingart@,gwk@


# 1.92 11-Oct-2007 kettenis

When walking the tree yo find _PRT methods, check whether the device is
actually enabled and functioning. Similar to what we do for _INI.

ok weingart@, beck@


# 1.91 08-Oct-2007 krw

More simple memset(,0,) -> M_ZERO changes. In this batch move to
size(*p) as the first malloc() parameter where p is declared locally
and thus easy to check. Add M_ZERO to gpe_table allocation in acpi.c
even though there is no obvious bzero or memset nearby.


# 1.90 13-Sep-2007 mk

Remove unnecessary memset() (see aml_evalnode(9)) and a bit of KNF.

ok toby


# 1.89 13-Sep-2007 weingart

Implement ACPI 6.5.1 spec tree walk for _STA and _INI.
Fixes mk's laptop. No regressions so far (thank you to the
testers).

ok gwk@, mk@, marco@


Revision tags: OPENBSD_4_2_BASE
# 1.88 17-Apr-2007 mk

Now that acpidock no longer hangs machines during boot (at least
according to the reports I've been getting) it should be safe to enable
it again, so remove the #if 0 stuff around it.

ok marco


# 1.87 11-Apr-2007 jordan

Added changes for C-State
ok marco@


# 1.86 26-Mar-2007 marco

Calculate minutes left more accurately.

From giovanni <qgiovanni@gmail.com>


Revision tags: OPENBSD_4_1_BASE
# 1.85 03-Mar-2007 mk

Stop looking for acpidock until we fix that stuff that breaks all pre-60
series ThinkPads with acpidock(4) enabled.


# 1.84 22-Feb-2007 marco

We were getting no interrupts in .MP. There are 2 fixes; one in
acpi_machdep.c and move the interrupt establishment down after ioapic
has been initialized.

ok jordan


# 1.83 21-Feb-2007 kettenis

Make an acpi-enabled GENERIC.MP work on systems that only support PIC mode.

ok marco@


# 1.82 19-Feb-2007 jordan

Added acpi_map_address function for mapping GAS/IO registers
ok marco@


# 1.81 18-Feb-2007 jordan

Added dynamic perfect hash for opcodes
ok marco@


# 1.80 18-Feb-2007 marco

Shave off another K pre compression.
Shuffle SMALL_KERNEL functions around so that we only have a single #ifndef


# 1.79 17-Feb-2007 marco

Add SMALL_KERNEL throught acpi to make it fit on boot media.
ok deraadt


# 1.78 13-Feb-2007 marco

Add smarter _INI code. This helps with some dissapearing devices.

ok kettenis jordan


# 1.77 31-Jan-2007 gwk

Introduce acpi_hasprocfvs thats "ACPI Has Processor Frequency and Voltage
Scalling" a global flag set by acicpu attach to indicate that ACPI is
handling fvs through the _PSS objects. This will fix disappearing
EST/powernow when booting kernels with acpi enabled, as acpicpu isnt
compiled into GENERIC yet. Also in cases where acpicpu is built in and
legacy EST/powernow work but acpi is missing the _PSS object, the legacy
routines will be used.

tested by dim@, and Nick Nauwelaerts <nick AT nauwelaerts.net>
ok marco@, dim@


# 1.76 22-Jan-2007 mk

Actually look for docking stations by looking for _DCK nodes.

ok marco


# 1.75 02-Jan-2007 marco

On some systems EC must be attached as the first device. Move EC from
generic _HID devices to its own routine to force first attach. Bug found
and debugged by joris@

Code mostly written by joris@


# 1.74 26-Dec-2006 marco

Add polling to devices that require it such as acpibat and acpitz.
Use the same tsleep/wakeup mechanism as the interrupt code to keep it non-
concurrent.


# 1.73 21-Dec-2006 deraadt

even more knf love


# 1.72 21-Dec-2006 marco

Remove some leftover acpiec gpe/interrupt code.


# 1.71 20-Dec-2006 deraadt

oops, name already used


# 1.70 20-Dec-2006 deraadt

rename __acpi_enable_gpe(); ok marco


# 1.69 20-Dec-2006 deraadt

acpi_unmap_pmregs() is not used, ok marco


# 1.68 19-Dec-2006 deraadt

simplify some low-level methods substantially (shrinking code); ok marco jordan


# 1.67 06-Dec-2006 mk

malloc(..., M_NOWAIT) with no NULL check is bad. If allocation fails,
complain, clean up, and bail out of attach.

input and ok gwk


# 1.66 28-Nov-2006 jordan

Fixed clearing arg for acpi_init_pic
ok kettenis@


# 1.65 27-Nov-2006 kettenis

Don't spam dmesg.


# 1.64 27-Nov-2006 jordan

Fixes for acpiprt routing
Post-parse fixup for namerefs
ok kettenis@


# 1.63 25-Nov-2006 marco

Don't handle acpiev events inside the gpe handler since this causes deadlocks.

ok jordan@


# 1.62 15-Nov-2006 kettenis

Hook up acpi apic interrupt routing support to generic acpi code.

ok jordan@


# 1.61 02-Nov-2006 marco

Kill acpiec attach function and add its functionality to the generic _HID
device attach

prodded and ok jordan


# 1.60 02-Nov-2006 marco

Fix comments to reflect reality.


# 1.59 19-Oct-2006 marco

Replace ACPI ioctls with APM ioctls. This makes ACPI talk the same protocol
as APM therefore userspace can now retrieve power information in the same
fashion as all other apm arches (zaurus, macppc, i386 etc).

To use force the use of acpi device like so: apmd -f /dev/acpi

ok jordan@


# 1.58 19-Oct-2006 jordan

Fixed GPE debug levels
Fixed ref counter on temporary bufferfields
DSDT parsing fully working on IBM Thinkpad
ok marco@


# 1.57 15-Oct-2006 jordan

Removed old GPE handler code remnants
ok marco@


# 1.56 12-Oct-2006 jordan

Massive ACPI changes
Fixed ACPI leaks
new ACPI API
ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.55 30-Jun-2006 gwk

KNF, No functional change,
"go at it" marco@


# 1.54 30-Jun-2006 jordan

Support for calling Embedded Controller via new GPE interface
ok marco@


# 1.53 30-Jun-2006 jordan

Added new code for GPE handling
Also works with ACPIEC; need to fixup acpiec.c to remove original interrupt handler
ok marco@


# 1.52 30-Jun-2006 jordan

Added offset argument for acpi read/write pmreg routines (new GPE code)
ok marco@


# 1.51 31-May-2006 todd

do not call acpi_powerdown() unless acpi has successfully initialized
ok gwk@ canacar@


# 1.50 29-May-2006 canacar

Add embedded controller support.
ok marco@ deraadt@ testing & ok gwk@


# 1.49 19-May-2006 canacar

Minimal thermal zone driver. Monitors thermal zone temperature,
shuts down the system if the 'critical temperature' is reached.
ok marco@


# 1.48 11-Apr-2006 marco

KNF.


# 1.47 11-Apr-2006 gwk

Add acpi_resume(struct acpi_softc *sc), which calls some AML methods,
detected at attach time, currently unused.

ok marco@


# 1.46 11-Apr-2006 gwk

Discover some ACPI sleep related methods, and call them at the
appropriate time in acpi_enter_sleep_state().

ok marco@


Revision tags: OPENBSD_3_9_BASE
# 1.45 26-Feb-2006 marco

Add acpicpu as a device.

ok jordan@


# 1.44 23-Feb-2006 marco

remove dead function.

ok jordan@


# 1.43 22-Feb-2006 marco

Print out all tables it found during autoconf. We'll need this data in
the future.


# 1.42 22-Feb-2006 marco

Make dmesg less verbose.

ok jordan.


# 1.41 22-Feb-2006 jordan

Added parsing of SSDT tables
ok marco@


# 1.40 22-Feb-2006 jordan

Added new aml_register to notify by device id
Moved powerdown code to button device
ok marco@


# 1.39 21-Feb-2006 marco

Readjust debug levels that crept in too low over the last few days.
KNF


# 1.38 21-Feb-2006 marco

Rig the power button so that the machine shuts down correctly.


# 1.37 20-Feb-2006 marco

Add acpibtn device. Remains disabled for now.


# 1.36 19-Feb-2006 jordan

Added code for aml_notify GPE notification callback
ok marco@


# 1.35 19-Feb-2006 grange

Fix acpi_enter_sleep_state() by replacing current ugly hack
with mostly correct sleeping state transition code.

ok brad@ marco@


# 1.34 19-Feb-2006 marco

jordan:
Enable interrupts.
Added peripheral code to make this work.

marco:
Replace softintr and other types of interrupts with a handler with context.
Fixed interrupt magic for GPEs.
Some KNF

This is work done by jordan and I but has to be a single commit.


# 1.33 17-Feb-2006 marco

Remove earlier introduced delays. We really only need them during autconf.

ok jordan@


# 1.32 17-Feb-2006 marco

Add a generic delay function as per the ACPI spec.

from jordan@


# 1.31 17-Feb-2006 marco

Use tsleep instead of delay when !cold.


# 1.30 17-Feb-2006 marco

Argh, we need delays between reads/writes. The backend reads (i2c most likely)
are so slow that values come back as 0 sometimes. We have to do this
despite the spec saying we shouldnt have to. Guess having a faster
parser than others can hurt sometimes.

Idea from jordan.


# 1.29 17-Feb-2006 jordan

Fixed code for handling _INI
Rearranged debug statements
ok marco@


# 1.28 16-Feb-2006 jordan

Added stub for calling _INI
Fixed aml_dowhile for NULL return
ok marco@


# 1.27 16-Feb-2006 jordan

Fixed parser/evaluator to retrieve battery info+state
Cleanup of parser
Dell laptops return battery status
ok marco@


# 1.26 06-Feb-2006 brad

fix a typo so this will compile on amd64.


# 1.25 04-Feb-2006 marco

When ACPI_ENABLED wasn't set but the devices were it had an unknown variable.

Pointed out by ian@.


# 1.24 03-Feb-2006 jordan

Rewrote aml parser yet again...
Fixed bus_space_map for i386/amd64
Added _BST and _BIF to acpibat to get status/info
ok marco@


# 1.23 20-Jan-2006 jordan

More evaluator changes
Removed old evaluator code
Fixed _HID value
ok marco@


# 1.22 19-Jan-2006 jordan

Added support for read/write of PCI Config space
Help jason@
ok marco@


# 1.21 18-Jan-2006 jordan

Added new evaluation routines
Added helper methods for buffer/field evaluation
Fixed broken indentation on dsdt.c

ok marco@


# 1.20 17-Jan-2006 jordan

Major overhaul of the aml parser. Evaluater temporarily disabled.

ok marco@


# 1.19 06-Jan-2006 grange

Check for ACPI control availability only if ACPI_ENABLE defined.


# 1.18 06-Jan-2006 grange

Informative error messages on attach, prettier dmesg.


# 1.17 06-Jan-2006 grange

printf -> dnprintf conversion for debug messages.


# 1.16 05-Jan-2006 grange

Find SLP_TYPx values and store them for future use in
acpi_enter_sleep_state().


# 1.15 05-Jan-2006 grange

Random KNF and cleanup


# 1.14 05-Jan-2006 grange

Provide symbolic names for the acpi sleeping states and use it.


# 1.13 28-Dec-2005 marco

Moved DEVNAME to a acpidev.h

ok jordan@


# 1.12 18-Dec-2005 sturm

add back comment about wrong code

ok marco


# 1.11 16-Dec-2005 marco

Remove test for kbd_reset. Prompted deraadt@, ok jordan@


# 1.10 16-Dec-2005 jordan

Added hooks for powerdown/power button press


# 1.9 16-Dec-2005 jordan

Cleaned up common power management register i/o
Added enter sleep state ioctl
Hooked power button interrupt
ok marco@


# 1.8 16-Dec-2005 marco

Remove autoconf chatter


# 1.7 16-Dec-2005 jordan

Moved DSDT AML parsing into parent ACPI driver
ok marco@


# 1.6 14-Dec-2005 marco

Add AC device glue.


# 1.5 14-Dec-2005 marco

Adjust debug printing levels to something more sane.


# 1.4 13-Dec-2005 marco

Add glue to attach devices to apci.


# 1.3 07-Dec-2005 marco

KNF


Revision tags: OPENBSD_3_8_BASE
# 1.2 10-Jul-2005 grange

Spacing cleanup.


# 1.1 02-Jun-2005 tholo

Start on a basic ACPI framework -- does not do much more than read out the
ACPI tables into kernel memory and attach ACPI and HPET timers currently.

In order to test this code, enabling the devices in GENERIC as well as
the ACPI_ENABLE option is needed. This code does not do any thermal
control yet, so this should be done with care depending on the platform.

In the tree so more people can contribute to making this more fully
featured.

Ok niklas@ grange@ tedu@


# 1.372 27-Aug-2019 deraadt

On amd64 the bootup and resume paths for SP/BP/SP were not performing
msr/pat, clock-startup, firmware upgrade, fpu initialization, and cpu
soft-state initialization in the same order. That kind of chaos is
crazy and fragile. Improve the acpi resume call sequence and unify order
of the operations.


# 1.371 02-Jul-2019 jcs

acpi_set_gpehandler: turn edge arg into flags, add support for
passing GPE_DIRECT which directly executes the supplied callback and
clears a level interrupt rather than going through acpi_taskq which
can be slow when other driver callbacks are in the queue.

This should not be used for GPE handlers that need to do anything
ACPI-related.

ok kettenis


# 1.370 10-Jun-2019 kettenis

Print proper ACPI version number.

ok mlarkin@


# 1.369 08-Jun-2019 kettenis

When establishing the mapping between ACPI device nodes and PCI devices,
skip PCI host bridges and devices that aren't there.

ok patrick@


# 1.368 07-Jun-2019 kettenis

Introduce acpi_getsta() and use it to eliminate some duplicated code.

ok patrick@


# 1.367 12-May-2019 kettenis

Make sure the extended PM register descriptions actually exist before
using them. Fixes machines with ancient ACPI 1.0 (such as recent
hypervisors).

ok deraadt@


# 1.366 12-May-2019 kettenis

Add support for mapping ACPI PM registers using the "extended" gasio
representation from the FADT. Mostly fixes the Lanner NCA-1510.

ok lteo@, mlarkin@, deraadt@


# 1.365 11-May-2019 lteo

Fix incorrect error message.

ok kettenis@


# 1.364 08-May-2019 tedu

free size. the allocation is the header plus the length.


Revision tags: OPENBSD_6_5_BASE
# 1.363 04-Apr-2019 kettenis

Avoid potential null-pointer dereference. Found by Kent R. Spillner.

ok deraadt@, jsg@


# 1.362 20-Jan-2019 jsg

fix SMALL_KERNEL build after changes in rev 1.361
reported by naddy@ ok deraadt@


# 1.361 19-Jan-2019 tedu

Add a pwraction sysctl that controls what the power button does on acpi.
By default, nothing changes -- shutdown is initiated. But allows turning
power button into a sleep button if desired.
(grudging) ok from a few parties


# 1.360 26-Oct-2018 kettenis

Add acpipci(4) on amd64. For now this only calls the PCI-specific _OSC
method to let the ACPI implementation know what features we support.


Revision tags: OPENBSD_6_4_BASE
# 1.359 30-Aug-2018 kettenis

Move softbutton check before the check that skips attaching certain acpi
devices. Fixes regression caused by matching on _CID in addition to
matching on _HID.

ok matthieu@, mlarkin@


# 1.358 25-Aug-2018 kettenis

Match ACPI devices based on _CID if no match for _HID is found.

ok mpi@


# 1.357 19-Aug-2018 kettenis

Add support for multiple PCI segments. Only really implemented for arm64
for now as amd64/i386 firmware still caters for legacy OSes that only
support a single PCI segment.

ok patrick@


# 1.356 03-Aug-2018 kettenis

Let ahci(4) match on _CLS instead of _HID when attaching at acpi(4). Avoids
having to add many more _HID entries to the match table.

ok deraadt@, mlarkin@


# 1.355 10-Jul-2018 kettenis

Make legacy interrupts work in acpipci(4).

ok patrick@


# 1.354 05-Jul-2018 kettenis

Add acpipci(4), a driver that supports generic ECAM-compatible PCI host
bridges based on information provided by ACPI.

ok mlarkin@


# 1.353 04-Jul-2018 kettenis

Properly pass around the PCI "chipset tag" in acpi(4) and refactor
acpimcfg(4) to call an MD initialization functions that sets up a tag for
PCI ECAM.

ok guenther@, mlarkin@, krw@


# 1.352 01-Jul-2018 kettenis

Move acpi(4) attach glue into acpi_machdep.c.

ok guenther@, deraadt@


# 1.351 01-Jul-2018 kettenis

Add support for _DSD Device Properties.

ok mlarkin@


# 1.350 30-Jun-2018 kettenis

Add intr_enable() function, intended for MI use to amd64 and i386 and use
this in the acpi(4) suspend/resume code paths.

ok deraadt@


# 1.349 29-Jun-2018 kettenis

Use <stdint.h> types.

ok mpi@, jung@, krw@, deraadt@


# 1.348 29-Jun-2018 mlarkin

ACPI: Allow (with warning) GPE handler reassignment, instead of returning
a failure code.

ok kettenis


# 1.347 26-Jun-2018 mlarkin

knf


# 1.346 26-Jun-2018 kettenis

Let acpi(4) pass down the bus dma tag.

ok deraadt@


# 1.345 25-Jun-2018 kettenis

Reorganize acpi(4) code a bit in preparation for upcoming arm64 support.

ok deraadt@


# 1.344 20-May-2018 kettenis

Wake up the acpi thread after scheduling a gpio event.

ok mpi@


# 1.343 17-May-2018 kettenis

Sprinkle some #ifndef SMALL_KERNEL such that RAMDISK kernels cuild again.

pointed out by stsp@


# 1.342 17-May-2018 kettenis

Implement GenericSerialBus OpRegion support.

ok mlarkin@


# 1.341 27-Mar-2018 kettenis

Add acpicmos(4), a driver that implements SystemCMOS OperationRegion
access support. This fixes machines where the AML doesn't check whether
support for this OperationRegion type has been registered by the OS.

ok mlarkin@


Revision tags: OPENBSD_6_3_BASE
# 1.340 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.339 10-Feb-2018 deraadt

Syncronize filesystems to disk when suspending. Each mountpoint's vnodes
are pushed to disk. Dangling vnodes (unlinked files still in use) and
vnodes undergoing change by long-running syscalls are identified -- and
such filesystems are marked dirty on-disk while we are suspended (in case
power is lost, a fsck will be required). Filesystems without dangling or
busy vnodes are marked clean, resulting in faster boots following
"battery died" circumstances.
Tested by numerous developers, thanks for the feedback.


# 1.338 08-Feb-2018 deraadt

After flushing disks, try again to release all memory. We don't need to
save dirty memory to the hibernate space.


# 1.337 08-Feb-2018 deraadt

Toss all releaseable memory, because fragmentation can get in the way
of allocating the hibernate playpen.


# 1.336 08-Feb-2018 deraadt

sr_quiesce() is a new approach for ensuring that softraid drains
output to the disks.

This is part of a larger suspend/resume filesystem-safety diff, which
has been worked on for a couple of months already. Tests by job, krw,
beck, benno, and others. Sometimes even by snapshot users...


# 1.335 29-Nov-2017 kettenis

Add support for GPIO-signaled events.

ok mlarkin@


# 1.334 16-Nov-2017 jcs

Add PCI attachment for dwiic(4) needed by Intel 100 Series machines

ok kettenis


# 1.333 14-Nov-2017 kettenis

Add a few more PNP IDs to the skip lists.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.332 17-Aug-2017 stsp

Make dwiic(4) attach its ACPI parent dependencies.
This change makes the Asus E200HA keyboard work.

Original analysis and patch by Cesare Gargano
ok kettenis@


# 1.331 09-Aug-2017 dcoppa

Add "PNP0303" (8042 PS/2 Controller) to acpi_isa_hids[]

ok kettenis@


# 1.330 22-Jul-2017 jcs

acpibat: look for _BIX first before falling back on _BIF

ACPI 4.0 deprecated _BIF for battery status so some newer machines
are exporting _BIX which extends _BIF's package with a few extra
fields. When using _BIX, export the new discharge cycle count as a
sensor.

feedback from kettenis and anton
ok deraadt


# 1.329 20-Jul-2017 kettenis

Start a list of isa(4) device names such that we can avoid printing them
as "not configured".

ok mikeb@, millert@, deraadt@


# 1.328 08-Apr-2017 deraadt

Sensors are run as callbacks inside tasks. During suspend a sensor
could be running inside a driver that will be force-detached, or due
to tsleep end up disrupting the softstate/hardstate contract. At
suspend time, quisce all these callbacks by waiting for completion.
This issue has never been observed for real, but may be implicated in
suspend/resume failures.
ok kettenis guenther mlarkin


# 1.327 08-Apr-2017 deraadt

Two prototypes for acpi_maptable; merge to one place


Revision tags: OPENBSD_6_1_BASE
# 1.326 27-Mar-2017 deraadt

Now that hibernate_alloc() only has clean success/failure, don't
need to call hibernate_free() to clean up a partial mess.
ok mlarkin kettenis


# 1.325 13-Mar-2017 deraadt

allow ramdisks to build; ok jcs


# 1.324 12-Mar-2017 jcs

try to attach acpisbs first and if successful, don't attach acpibat


# 1.323 02-Mar-2017 natano

Add a new sysctl machdep.lidaction. The sysctl works as follows:

machdep.lidaction=0 # do nothing
machdep.lidaction=1 # suspend
machdep.lidaction=2 # hibernate

lidsuspend is just an alias for lidaction, so if you change one, the
other one will have the same value. The plan is to remove
machdep.lidsuspend eventually when people have upgraded their
/ets/sysctl.conf.

discussed with deraadt, who came up with the new MIB name
no objections mlarkin
ok stsp halex jcs


# 1.322 28-Feb-2017 natano

Untangle abstract sleep modes from ACPI sleep states. This paves the way
for further work in the area.

idea and ok deraadt
ok mlarkin


# 1.321 22-Feb-2017 jcs

add acpisbs, an acpi smart battery subsystem driver reading data
over smbus

currently disabled because it conflicts with acpibat

ok deraadt, kettenis


# 1.320 14-Jan-2017 kettenis

Revert aml_rdpciaddr changes; breaks several machines that were working
before.


# 1.319 11-Jan-2017 jsg

Increase the size of the buffers used for _HID and _CID strings to be
able to fit "Hyper_V_Gen_Counter_V1".

ok mlarkin@ deraadt@


# 1.318 08-Jan-2017 kettenis

Make aml_rdpciaddr() work for for devices behind bridges. The code now
uses the mapping between AML nodes and PCI devices that we establish.
Because _INIT methods may end up calling aml_rdpciaddr(), make sure we
create that mapping early. Also handle devices that aren't actually present.
These devices are now included in the mapping and reads will return an
all-ones patterm whereas writes are a no-op..

ok mlarkin@


# 1.317 25-Oct-2016 pirofti

Add the resource index to aml_parse_resource() callback function.

This is needed by an upcoming acpiec commit that handles machines breaking
the current ACPI specifications.

Change suggested by and ok kettenis@, guenther@


# 1.316 18-Sep-2016 guenther

If the APIC table (MADT) isn't present then ci_acpi_proc_id won't be set
when acpicpu tries to attach, leading to a NULL dereference. The ACPI
processor object on the Dells where this was reported is bogus and useless
anyway so apply the same test when deciding whether to attach to processor
objects as acpicpu would when matching them to cpus.

Problem reported by Eivind Eide (xenofil (at) gmail.com) and
Johan Huldtgren (johan+openbsd-bugs (at) huldtgren.com).
Testing by Eivind Eide.
ok kettenis@ mlarkin@


# 1.315 03-Sep-2016 naddy

Write the system time back to the RTC every 30 minutes.
This fixes the problem that long-running machines which were not
shut down properly would reboot with a badly offset system time.

hints and ok kettenis@


# 1.314 31-Aug-2016 mlarkin

Don't put a device in the wake device list if _STA says it's not there.

Fixes at least one machine that woke up from sleep / poweroff immediately.

ok kettenis@, tested by kettenis and martijn@


# 1.313 28-Jul-2016 kettenis

Store the acpi processor ID/UID in struct cpu_info, and use it to attach
acpicpu(4) drivers to the right cpu(4).

ok mlarkin@, guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.312 10-Jun-2016 kettenis

The power management timer has been made optional in ACPI 5.0A, so don't
attempt to attach acpitimer(4) if the timer isn't present.

Pointed out by jcs@

ok jcs@


# 1.311 08-May-2016 kettenis

Make sure devices listed by a device's _DEP method are attached before
we attach the device itself. The _DEP methode, introduced in ACPI 5.0,
is a hint that the device in question depends on OpRegion support from
the devices listed by _DEP, so we have to order them the proper way.
To prevent us from attaching those devices again when we encounter them
later walking down the device tree, keep track of our attempts to
attach a device driver to them using aflag in the aml_node structure.

ok guenther@


# 1.310 25-Apr-2016 pirofti

We attach acpihpet(4) and acpiec(4) without using the HID, so do not
report it in the dmesg.

OK deraadt@, mlarkin@.


# 1.309 02-Apr-2016 kettenis

Reduce dmesg spam by knocking out some acpi hardware IDs that don't need a
driver.

ok mpi@


# 1.308 29-Mar-2016 kettenis

Check if a device is present (using _STA) in acpi_foundhid. This prevents
us from reporting devices as "not configured" that aren't actually present,
and allows us to remove duplicated code from several drivers.

ok deraadt@, ok jsg@


# 1.307 29-Mar-2016 kettenis

Implement a cleaner way to prevent acpivout(4) from attaching if we have
a platform-specific method of controlling the backlight.

ok millert@, mlarkin@, jung@


# 1.306 28-Mar-2016 kettenis

Adding each and every new acpi device driver to acpi_foundhid() is
gettinga bit of the burden. And it really isn't how our autoconf
framework is supposed to work for busses that can be enumerated. All
the drivers already check for a matching _HID in their attach
function. So we can just drop the checks in acpi_foundhid() and let
the drivers sort themselves out.

Print a "not configured" line for devices for which we don't attach adriver.
This may be a bit much as there are devices for which we will not have a
driver. This will be removed if it causes too much dmesg spam.

ok deraadt@, mlarkin@


Revision tags: OPENBSD_5_9_BASE
# 1.305 17-Jan-2016 jsg

Take acpi_foundhid() outside of #ifndef SMALL_KERNEL so the ramdisk
kernel can find and attach dwiic(4) and sdhc(4).


# 1.304 17-Jan-2016 jsg

remove an unused global variable


# 1.303 14-Jan-2016 kettenis

Attach dwiic(4) on the Intel Bay Trail i2c controllers as well.


# 1.302 12-Jan-2016 kettenis

Improve emulation of PM registers on Hardware-redcued ACPI by emulating
SPL_TYPx, SLP_EN and WAK_STS using the new SLEEP_STATUS_REG and
SLEEP_CONTROL_REG registers. Makes the transition into S5 (powerdown) work
on the ASUS X205TA.

ok deraadt@, mlarkin@


# 1.301 12-Jan-2016 jcs

Add dwiic, a driver for the Synopsys DesignWare i2c controller found
on the Samsung ATIV Book 9 laptop. This initial version only
supports ACPI config/attachment.

Add ihidev, a HID-over-i2c driver largely based on uhidev. dwiic
handles attaching ihidev devices found in ACPI.

Add ims, a HID-over-i2c mouse/trackpad driver to get basic cursor
and button functionality from HID-compliant i2c trackpads.

ok kettenis deraadt


# 1.300 11-Jan-2016 kettenis

Make sdhc(4) attach to hardware IDs "80860F14" and "PNP0FFF".


# 1.299 09-Jan-2016 kettenis

Initial support for Hardware-Reduced ACPI. This skips the legacy mode checks
by emulating the PM_CNT register such that SCI_EN is always enabled. It also
skip attaching acpitimer(4) since the relevant registers are no longer there.

ok deraadt@, mlarkin@


# 1.298 06-Jan-2016 kettenis

Follow the spec and check the SCI_EN bit to see if ACPI has already
been enabled and don't bother enabling it again if it is. This should fix
systems that don't implement legacy mode and therefore set ACPI_ENABLE and
ACPI_DISABLE to zero.

ok mlarkin@, yasuoka@


# 1.297 23-Nov-2015 reyk

Back out the CRITICAL change, turn it back from 10% to 25%. Some
people with old/broken batteries didn't like this change, and the
underlying heuristics are wrong.


# 1.296 22-Nov-2015 reyk

The "CRITICAL" APM battery level, that is exported/faked by the ACPI
stack, is currently set to 25%. With 25% battery left, a typical
modern laptop will have at least one hour runtime left. It is an
arbitrary value, and other APM power layers in the kernel set CRITICAL
to something like 5% or 10%, so set it to 10% for now.

OK mlarkin@


# 1.295 28-Sep-2015 deraadt

In low-level suspend routines, set cold=2. In tsleep(), use this to
spit out a ddb trace to console. This should allow us to find suspend
or resume routines which break the rules. It depends on the console
output function being non-sleeping.... but that's another codepath which
should try to be safe when cold is set.
ok kettenis


# 1.294 06-Sep-2015 deraadt

clarify malloc arguments (base + length...)


# 1.293 28-Aug-2015 deraadt

fairly simple sizes for free(); ok tedu


# 1.292 20-Aug-2015 kettenis

Respect the access size when reading or writing to pci config space.
This version also makes sure writes are properly aligned, preventing
the panics seen with the previous incarnation of this change.

Fixes battery status passthrough in vmware, brightness keys on my x220
and who knows what else.

ok mlarkin@, deraadt@


Revision tags: OPENBSD_5_8_BASE
# 1.291 04-Aug-2015 deraadt

If we are setting ACPI_PM1_BM_RLD do it only once, and remember to
repeat this action at resume time.
ok mlarkin guenther


# 1.290 29-Jul-2015 deraadt

Something subtle in previous revision is locking up suspend or booting
on some laptops. Mailing lists threads mention x220 and Asus machines.
Reverting for now.


# 1.289 21-Jul-2015 kettenis

iRespect the access size when reading or writing to pci config space.
Fixes battery status passthrough in vmware and who knows what else.

Tested by many.

ok mlarkin@, deraadt@


# 1.288 17-Jul-2015 mlarkin

fix comments


# 1.287 26-Mar-2015 jsg

Use the 0x10 extended capability to determine if the trackstick
buttons are wired to the trackpad and need to be re-routed to the
trackstick. Without this change the buttons on 2015 thinkpads
get picked up as extended buttons that show up as scroll up/down.

Remove the X1 Carbon 2015 (LEN0048) and X250 (LEN0046) from the
top button area/soft buttons quirks list. Also avoid using the
quirk list entirely if the capability bit is set.

Based on work from jcs@ with feedback from mpi@
ok mpi@ jcs@


# 1.286 20-Mar-2015 kettenis

The ACPI standard says that a PCI function number of FFFF can be used to refer
to all functions on a device. This makes no sense in the context of accessing
PCI config space. Nevertheless stsp@ has a machine with AML that tries to
do config space reads from a device where _ADR encodes function number FFFF.
This is almost certainly buggy AML, but since Linux (and presumably Windows)
boot just fine on this machine we need to work around this. Linux effectively
treats function FFFF as function 7, which isn't present on this machine.
Instead we simulate a read from a non-existent device by returning a bit
pattern of all ones. Writes to function FFFF will still panic.

ok mlarkin@


# 1.285 16-Mar-2015 deraadt

Reduce use of SMALL_KERNEL in acpi, bringing acpiec driver and more gpe
handling into RAMDISK. This is now possible because the install media
has ample room. The goal is to reduce special cases where we may be
skipping (unknown) important operations...
ok mlarkin kettenis


# 1.284 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.283 07-Feb-2015 deraadt

New framework that allows hibernate to pass in entropy from it's fresh
boot.
ok mlarkin


# 1.282 06-Feb-2015 mlarkin

Fix a hibernate crash on some machines due to unmapping a page that
may not have been mapped previously (in the failure to hibernate case).

Also ensure that the lowmem ptp is mapped in all cases (not just MP).

ok kettenis


# 1.281 17-Jan-2015 deraadt

resume_randomness() before spinning up other cpus...


# 1.280 15-Jan-2015 jsg

Define a new wscons mouse type for Synaptics clickpad devices that lack
physical buttons. This will be used if the acpi pnpid for the mouse
matches a list provided by Synaptics found in Linux.

Combined with the xenocara changes this will remove the need for an xorg
config file for the x240/t440/t540 generation of thinkpads.

ok matthieu@ shadchin@ kettenis@


# 1.279 11-Jan-2015 kettenis

Tolerate checksum errors in ACPI tables (but complain about them). Apparently
there are machines out there with broken BIOSen with such checksum errors in
thier RSDT tables. Windows runs fine on these machines, so it probably does
not check. Linux ignores checksum errors as well (and prints a warning).

ok deraadt@, krw@


# 1.278 18-Dec-2014 deraadt

suspend and resume the RNG (at what looks like the right spots)
ok reyk djm


# 1.277 09-Dec-2014 doug

Replace some malloc(n*size,...) calls with mallocarray().

ok tedu@ deraadt@


# 1.276 06-Dec-2014 mpi

If "machdep.lidsuspend" is not set, do not go back to sleep if the lid
is still closed when resuming. This allows docked laptop to be resumed
without opening them.

ok mlarkin@


# 1.275 23-Nov-2014 mlarkin

If a machine has no _LIDs, then none can be open. This diff fixes a problem
where machines without any _LIDs were going back to sleep after hibernate
due to the recent suspend-if-lid-closed-on-resume diff. Noticed on qemu,
which doesn't have any _LIDs.

discussed with deraadt


# 1.274 18-Nov-2014 krw

Nuke yet more obvious #include duplications.

ok miod@


# 1.273 08-Nov-2014 mlarkin

If resuming from sleep (zzz/ZZZ) and the lid is still closed, go back to
sleep. This prevents accidental lid flex or slight opening in a backpack
from waking the machine up and leaving it resumed (powered on).

ok deraadt@


# 1.272 26-Sep-2014 kettenis

Rework piglet and pig allocation. Currently the piglet gets allocated
deep down in the suspend path, where it is really hard to recover from
allocation failure. So allocate the piglet early on in the suspend path.
Also change the piglet and piglet allocation functions to use km_alloc(9)
instead of doing pmemrange magic. This removes a bunch of code which, in the
case of the piglet allocation, is broken since it results in a NULL pointer
dereference. Also switch the piglet allocation to not wait. If we can't
allocate 16MB of phys contig memory on a halfway modern machine we're almost
certainly under a lot of memory pressure and we're better off not trying to
hibernate anyway.

ok mlarkin@


# 1.271 25-Sep-2014 kettenis

Bring back rev 1.249. Now that mp_setperf() has been fixed, it should be
safe to restore perflevel when resuming. Should fix the issues people
see with the processor running at full speed heating up the machine while
the apm command reports that the CPUs are running at a low speed.

ok dcoppa@, deraadt@


# 1.270 23-Sep-2014 kettenis

Release the acpi lock around calls to wsdisplay_suspend() and
wsdisplay_resume(). The X server may adjust the display brightness just
before/after the VT switch when suspending/resuming. In fact, it may
already be blocked adjusting the display brightness for some reason
unrelated to the suspend request.

The VT switch request will eventually time out, but the delay is annoying
and the failed VT switch will lead to problems after resume.

Needed for a future update of the xf86-video-intel driver.

ok mlarkin@


# 1.269 19-Sep-2014 kettenis

Use config_suspend_all(9).


# 1.268 08-Sep-2014 kettenis

Clear the PME Status bit on PCI devices in response to Device Wake events.
Fixes the interrupt storm on the 2009 Mac mini with WOL enabled on its
nfe(4) interface.

ok mlarkin@


Revision tags: OPENBSD_5_6_BASE
# 1.267 20-Jul-2014 mlarkin

Support hibernating to softraid crypto volumes.

much help and ok from deraadt@


# 1.266 16-Jul-2014 mlarkin

Reenable hibernate RLE support and flush+zero all memory after unpack.


# 1.265 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.264 12-Jul-2014 kettenis

Add a function to drop all clean pages on the page daemon queues and call
it when we hibernate.

ok mlarkin@, miod@, deraadt@


# 1.263 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.262 11-Jul-2014 mlarkin

Flush the buffercache to 16MB on hibernate and restore its previous max
size (kern.bufcachepercent) on resume, for better hibernate performance.

ok beck@


# 1.261 10-Jul-2014 blambert

remove unneeded header

ok mlarkin@


# 1.260 01-Jul-2014 kettenis

Adding an additonal delay after each access to SystemMemory or SystemIO
during autoconf makes no sense.

ok deraadt@


# 1.259 23-Jun-2014 kettenis

SystemMemory is used to access memory mapped registers on some machines, so we
must use the bus_space(9) API to access it instead of memcpy(9). Also make
sure we properly align access to fields that need more than byte alignment.

ok guenther@


# 1.258 21-May-2014 mlarkin

Fix invalid / missing format specifiers in several dnprintf statements
(used only when ACPI_DEBUG is enabled)

From a diff on tech@ by Fabian Raetz, with a minor change by me, tested
on i386 and amd64 with ACPI_DEBUG enabled and disabled.


# 1.257 25-Apr-2014 mlarkin

Defer the free of the hibernate structures until later, when it is safe to
do so.

Freeing the hibernate structures too early leads to inadvertent calls into
splx when we aren't ready for them yet. Found when trapping splx calls in
the resume path.

discussed with deraadt@ at m2k14


# 1.256 13-Apr-2014 mlarkin

Fix a badly worded debug message that implied the entire machine was going
to sleep, instead of just the acpi thread going to sleep.


# 1.255 31-Mar-2014 mpi

Change the order of operations during a suspend/resume cycle and
call bufq_quiesce() after executing the DVACT_QUIESCE handlers.

This should be safe since no disk nor controller drivers have such
handler but it will allow us to detach sd(4) devices attached to a
USB bus.

Another benefit pointed out by kettenis@ is that drivers that need
to read a firmware from the disk should be able to do it at resume
time in a DVACT_WAKEUP handler.

ok kettenis@, deraadt@


# 1.254 13-Mar-2014 dlg

get rid of the assumption that the head of the alldevs list is the
"mainbus" device. this breaks when mpath is enabled because it
attaches before mainbus and therefore takes the head position.

have autoconf provide device_mainbus() which looks up mainbus_cd,
and use that instead.

discussed with deraadt who just wants mpath stuff to move forward
despite there being many ways to shine this particular turd.


Revision tags: OPENBSD_5_5_BASE
# 1.253 21-Feb-2014 deraadt

Do the lid control further on in the resume sequence, since this is
AML, and will do WAITOK operations. While cold, that is a bad idea.
We seem to be safe with respect to the spec, I do not see any event
debounce issues here.
ok kettenis, tested by many


# 1.252 20-Jan-2014 claudio

Instead of checking sc->sc_revision <= 1 check that the
fadt->hdr_revision <= 1. This is a better check since it checks
the version of the proper descriptor. This allows the alix APU
to reboot since those boards have currently a wrong sc->sc_revision.
Committing this now so that it can be tested easier.
With and OK jsg@ also fine by derradt@


# 1.251 24-Dec-2013 kettenis

If the FADT has its SMI_CMD set to zero, assume we're only ACPI-only hardware
and don't need to disable SMI ownership of the ACPI hardware registers.

ok mlarkin@


# 1.250 23-Dec-2013 kettenis

Revert previous commit. It makes resume hang on some MP systems running
GENERIC.MP.


# 1.249 16-Dec-2013 kettenis

Restore hw.setperf upon resume like we do for apm(4) on i386. Seems we need
to do this fairly late in the resume to avoid hangs.

ok deraadt@, mlarkin@


# 1.248 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


# 1.247 06-Nov-2013 mpi

Add support for Power Resources for Dx states and the necessary hook
for PCI devices. This hook should be called twice, before and after
changing the power state of a PCI device.

Before setting the device to the new state, the ACPI layer will notify
every power resources linked to the device for that state and make sure
they are turned "_ON". After changing the state of the device, it will
decrement the reference of every power resources linked to that device
for the old state and turn them "_OFF" if they are no longer referenced.

This fixes the no-USB after resume problem seen on various ThinkPad,
problem initialy diagnosed with Alexander Polakov.

ok kettenis@, deraadt@


Revision tags: OPENBSD_5_4_BASE
# 1.246 01-Jun-2013 mlarkin

Stop printing ACPI wakeup devices in dmesg after the 16th wakeup device.

Helps with VMware VMs that report hundreds of wakeup devices.

ok reyk@, deraadt@


# 1.245 31-May-2013 bluhm

Fix the build for a kernel without wd and pciide in its config.
OK deraadt@ tedu@


# 1.244 30-May-2013 deraadt

Enforce ca_activate tree-walks over the entire heirarchy for all events,
cleaning up some shutdown-hook related code on the way.
(A few drivers related to sparc are still skipped at kettenis' request)
ok kettenis mlarkin, tested by many others too


# 1.243 18-Apr-2013 deraadt

pin the acpi thread to the BSP, because AML and SMI cannot be trusted. I
was certain we were doing this already, but it turns out no...


Revision tags: OPENBSD_5_3_BASE
# 1.242 24-Dec-2012 guenther

In acpiioctl(), Return though the splx() path if suser() returns an error

ok deraadt@


# 1.241 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


# 1.240 04-Oct-2012 ehrhardt

Use information provided by ACPI to attach secondary PCI host bridges.

ok kettenis@


# 1.239 07-Sep-2012 kettenis

Add a new function called acpi_pci_min_powerstate() that returns the lowest
power state for a PCI device for the power state (currently S3 or S4) we're
transitioning into. This function is probably too simplistic; there is a lot
of (silly) complexity in the ACPI spec.

ok deraadt@


Revision tags: OPENBSD_5_2_BASE
# 1.238 13-Jul-2012 pirofti

Disable acpivideo(4) on ASUS laptops.

There are some models that panic whenever an event is received.
Some cope, some don't. In order to get this right acpiec(4) requires
major rework. Until then let machines boot.

Discussed with deraadt@.


# 1.237 11-Jul-2012 mlarkin

for now, restrict hibernate to root in the acpi ioctl kernel interface.
normal rules for controlling this interface through apmd still apply.

ok deraadt@


# 1.236 09-Jul-2012 deraadt

tiny little oops in untested hibernate block


# 1.235 09-Jul-2012 deraadt

clean up ioctl() case statements


# 1.234 09-Jul-2012 deraadt

acpi_disable_onegpe() is not used; ok pirofti


# 1.233 24-May-2012 kettenis

If _MIN > _MAX consider a WordBus resource to be invalid. Fixes a panic on
the IBM x3550-M4.

ok mikeb@


# 1.232 29-Mar-2012 mlarkin

Missing #ifdef in previous diff


# 1.231 29-Mar-2012 mlarkin

Prevent hibernation attempts on machines with unsupported disk controllers.
The same check is also performed later in the hibernate sequence, but by
then it's too late to cleanly unwind (presently).

ok deraadt


# 1.230 26-Mar-2012 deraadt

simplify conditionals in acpi_sleep_state()
ok mlarkin


# 1.229 26-Mar-2012 deraadt

hook in the hibernate request code; half of this diff is from mlarkin
ok mlarkin


Revision tags: OPENBSD_5_1_BASE
# 1.228 20-Sep-2011 deraadt

Very early in suspend while the clock is still ticking realtime,
save the clock back to the rtc. The zaurus already did this.
ok phessler miod kettenis


Revision tags: OPENBSD_5_0_BASE
# 1.227 02-Jul-2011 nicm

kqueue attach functions should return an errno or 0, not a plain 1. Fix
the obvious cases to return EINVAL and ENXIO.

ok tedu deraadt


# 1.226 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.225 16-Jun-2011 pirofti

Import acpitoshiba driver.

This is originally written by Hiroyuki Aizu, ported and integrated
in our tree by Javier Vazquez <javama4 AT gmail> with tweaks and
suggestions by me.

Import prodded by deraadt@ and the lack of testers on tech@.


# 1.224 27-Apr-2011 jcs

attach acpithinkpad to newer lenovo models like the x120e

ok deraadt@


# 1.223 22-Apr-2011 jordan

Fix uninitialzied variables and formatting strings (-Wxxx errors)


Revision tags: OPENBSD_4_9_BASE
# 1.222 02-Jan-2011 jordan

Re-add code for ACPI taskqueue, eliminates need for state vars in acpi softc
Serialize processing ACPI tasks for sleep/powerdown/GPE event
ok deraadt@


# 1.221 31-Oct-2010 guenther

Revert last commit: it breaks resume on Thinkpads
"then please back it out" deraadt@


# 1.220 26-Oct-2010 jordan

Add task queue for ACPI gpe and notify handlers
This fixes eject on Dell Latitude dock and an issue on the Dell Mini
battery update.
ok deraadt


# 1.219 07-Oct-2010 jordan

Create separate functions for enabling wake and runtime gpes.
Remove old suspend/resume gpewalk function
ok deraadt, mlarkin


# 1.218 05-Oct-2010 jordan

split acpi_enable_onegpe to separate enable/disable functions
ok deraadt


# 1.217 05-Oct-2010 jordan

Added acpi_disable_allgpes function to clear GPE status
ok deraadt


# 1.216 05-Oct-2010 deraadt

clean up acpi_softc use more


# 1.215 05-Oct-2010 deraadt

delete a bit of #if 0 code


# 1.214 05-Oct-2010 deraadt

hide use of our global acpi_softc pointer a bit better


# 1.213 29-Sep-2010 deraadt

grab the acpi thread lock very early in acpi thread startup; not that it
is likely that anyone will race us... but why be careless.
ok kettenis


# 1.212 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.211 29-Aug-2010 jasper

- add a check for FWRITE here too.

ok miod@


# 1.210 27-Aug-2010 kettenis

Make PCI Power Management optional, and only enable it when acpi(4) attaches.

ok deraadt@


Revision tags: OPENBSD_4_8_BASE
# 1.209 08-Aug-2010 kettenis

Close race between the acpi thread and wscons ioctls running in process
context. We might rip this out post-release and replace it with a solution
that serializes things in a more obvious way.

ok deraadt@, miod@ and "those crazy texans who ok anything"


# 1.208 08-Aug-2010 deraadt

Report the acpi sleep states when on a RAMDISK; ok krw


# 1.207 07-Aug-2010 kettenis

Enforce acpi register access rescrictions.

ok mlarkin@, deraadt@, tested by marco@


# 1.206 07-Aug-2010 deraadt

upon resume, notify all the acpiac and acpibat drivers to update
their status. some laptops do not do this automatically.
ok kettenis canacar


# 1.205 06-Aug-2010 marco

Always call _PSW on _LID devices that support it to enable lid open to
resume the machine. Conversely disable it when the machine wakes up.

Tested by several ok deraadt


# 1.204 06-Aug-2010 deraadt

As part of a revamp of the PM1/GPE code, I write this diff to look for
unmanaged EN & STS bits in the PM1 register at interrupt time and report
them. As a side effect this splits the STS acknowledgement into two writes
(for power, and sleep) instead of one. The printf that is added (to spot
unmanaged STS bits) has not yet been triggered as far as we know.

Before the "write to PM1 registers at the right offset" diff went in, this
was not neccessary. But newer thinkpads do not have a working soft power
button without this diff.

We have no idea why.
ok mlarkin kettenis


# 1.203 05-Aug-2010 deraadt

PM1 power button events were generating 'fake events' to acpibtn PWRB,
but if there is no such device these were being tossed. Instead, arrange
for all power button handlers to flag a softc variable which is processed
by the thread; so that one place will know to request a powerdown (psignal
to init)
ok marco mlarkin


# 1.202 05-Aug-2010 deraadt

rename a variable which is annoyingly poorly named


# 1.201 05-Aug-2010 mlarkin

Write to the correct offset for various ACPI pmreg registers. Writing to
the correct register does properly clear the wake status bits and fixed
event bits, which allows some machines to have more than 1 suspend cycle.

bug spotted by deraadt@

tested by deraadt@ and myself on a half-dozen machines and others on
hackers@

ok deraadt@, marco@


# 1.200 05-Aug-2010 deraadt

Fix the power button. The pm1 register is 16 bits in size.
My mistake, a while back.
tested by krw


# 1.199 04-Aug-2010 kettenis

Print supported sleep states.

ok marco@, phessler@, deraadt@


# 1.198 03-Aug-2010 mlarkin

Use the proper flag for re-enabling certain hardware events (power btn,
sleep btn). The (incorrect) flag prevoiously used coincidentally had the
same value, so this shouldn't cause any different behavior than before.

tested on a variety of machines (i386, amd64, sp, mp) - no behavioral
change seen

ok deraadt@


# 1.197 28-Jul-2010 marco

Make all gasio failures look and behave the same.


# 1.196 28-Jul-2010 mlarkin

Delay a little longer on suspend before giving up. Fixes machines that
sometimes don't suspend because the sleep transition takes too long
(symptoms like failing to sleep but the backlight stays on).

ok deraadt@


# 1.195 27-Jul-2010 deraadt

remove XXX; EC can now be attached early when needed; ok jordan


# 1.194 27-Jul-2010 deraadt

more brackets feels good


# 1.193 27-Jul-2010 deraadt

XXX annotate a block in acpi_enter_sleep_state() that is pretty worrying


# 1.192 27-Jul-2010 deraadt

horrific indentation that nearly made me puke


# 1.191 27-Jul-2010 mlarkin

Set the sleeping indicator light on machines that support it, in case SMI
doesn't do it for us. Shows the 'moon' LED on older Thinkpads when
sleeping.

Tested on T43p, W500, T510 by me and on X61s by deraadt@.

ok deraadt@


# 1.190 27-Jul-2010 jordan

Early initialization of acpiec if ECDT table exists
Fixes hang when booting thinkpads while docked
ok deraadt


# 1.189 26-Jul-2010 deraadt

remove merge error which was commited


# 1.188 26-Jul-2010 pirofti

Add support for Sony ACPI hotkeys via a new driver: acpisony(4).

Currently it only works for the suspend button and tries to do right for
the brightness events, but I haven't found a Sony laptop that like to do
right so far.

In the future I want to make the brightness keys work on all Sony's and
also add support for the zoom hotkeys and whatever other funky keys I can
find on those things.

Okay deraadt@.


# 1.187 22-Jul-2010 deraadt

pretty large cleanup of SMALL_KERNEL


# 1.186 21-Jul-2010 deraadt

in the acpi GPE handler, late ack level interrupts not edge interrupts.
Duh.


# 1.185 21-Jul-2010 deraadt

Merge the edge and level gpe handlers into one (now possible since the
gpe structure tells us whether the gpe is edge or level)


# 1.184 21-Jul-2010 deraadt

delete #if 0 code we are not going to use; ok marco


# 1.183 20-Jul-2010 deraadt

make the mask code for acpi_enable_onegpe() match other similar code


# 1.182 20-Jul-2010 deraadt

Handle the acpi interrupt controller with proper edge/level handling,
wrapped in spltty to avoid racing against the interrupt controller.
Repair the gpe bit masking code while there, and do operations in the
right order
ok kettenis mlarkin, help from jordan at figuring out the order of
operations.


# 1.181 20-Jul-2010 matthew

When we're preparing to go to sleep, only call bufq_restart() if other
preparations failed.

ok kettenis@, thib@, deraadt@


# 1.180 19-Jul-2010 deraadt

permit compilation with ACPI_DEBUG


# 1.179 19-Jul-2010 deraadt

there is no need for a seperate dsdt_softc pointer; dsdt does not have a
softc but is just a part of the single acpi driver.
ok jordan


# 1.178 19-Jul-2010 deraadt

acpi_delay() is no longer needed; ok jordan


# 1.177 18-Jul-2010 mlarkin

Clear event status on resume, to avoid going back to sleep/reboot/shutdown
immediately after resume. Tested by a bunch of people on hackers@ on
various hardware. Fixes at least Toshiba NB200 and Gateway LT3103u.

ok marco


# 1.176 13-Jul-2010 deraadt

rename some thread related interfaces so they are logical


# 1.175 10-Jul-2010 jordan

Verify _ADR bdf is correct (some systems return 0xffff)
Works on Dell x9xx and Studio 1555


# 1.174 09-Jul-2010 jordan

Clear the noise


# 1.173 08-Jul-2010 jordan

Add mapping for ACPI device to PCI bdf (match autoconf tree)
Simplify resource parsing function to use buffer argument
Convert namespace linked lists to use queue macros
ok marco@, deraadt@


# 1.172 06-Jul-2010 deraadt

Use spltty() to lock downcalls from apm against the information being
modified by the acpi thread.
ok kettenis


# 1.171 06-Jul-2010 deraadt

Don't use a workq to do the suspend, because it races aginst the acpi
thread. Instead, just tell the acpi thread to do the suspend for us.
This makes apmd & zzz work correctly. While here, have acpithinkpad
attempt to post the event to apm before prompting the suspend itself.
ok kettenis marco mlarkin


# 1.170 05-Jul-2010 mlarkin

Reset register SCI_EN on resume. The ACPI spec clearly states we don't have
to do this, but many machines don't, or forget to, which leads to all sorts
of problems on resume (broken battery stats, overheating, failure to resume
more than once, etc).

tested by many, ok marco@, deraadt@


# 1.169 01-Jul-2010 thib

do bufq quiesce again, since vnd/the bufq_done call in
biodone has been fixed.


# 1.168 01-Jul-2010 jordan

Backout recent AML changes, breaks on R210 and others


# 1.167 01-Jul-2010 jordan

Simplified aml_parse_resource call, cleanup code
ok marco


# 1.166 01-Jul-2010 jordan

Fix subordinate bus # for multi-root PCI buses
ok marco, kettenis


# 1.165 30-Jun-2010 thib

Disable/partially backout the bufq quiesce changes as this
is causing havoc with vnds and release must be buildable.


# 1.164 29-Jun-2010 deraadt

Fix ramdisks (new code was placed inside a SMALL_KERNEL chunk)


# 1.163 29-Jun-2010 jordan

Add support for mapping ACPI to PCI devices
ok kettenis, deraadt


# 1.162 29-Jun-2010 kettenis

Call bufq_quiesce() on suspend to stop all I/O and bufq_restart on resume.
Makes suspend/resume work much better while doing disk I/O.

ok mlarkin@


# 1.161 29-Jun-2010 tedu

60 is a more reasonable guess for number of minutes in an hour than 100.
spotted by TeXitoi. ok deraadt


# 1.160 27-Jun-2010 jordan

Add _acpi_matchhids routine, precursor to pci device match


# 1.159 27-Jun-2010 mlarkin

Clear ACPI fixed event status on resume (power buttons/etc) so that some
machines don't immediately resume after suspending to S3.

Tested by kettenis@ and I on 5 or 6 machines, deraadt@ "yeah, looks good"


# 1.158 27-Jun-2010 jordan

Cleaned up acpi_maptable, adds to table list directly


# 1.157 07-Apr-2010 deraadt

If we have done a wsdisplay_suspend(), and ACPI decides it does not want to
suspend, we must roll back by calling wsdisplay_resume() or the virtual
console switching will be locked forever
ok oga


# 1.156 07-Apr-2010 kettenis

Make suspend/resume work on MP machines (running an MP kernel). Joint work
from mlarkin@ and me, with some amd64 fixes thrown in by deraadt@

ok marco@, deraadt@, pirofti@, mlarkin@


# 1.155 31-Mar-2010 kettenis

Bring /dev/apm support in line with apm(4). Make the suspend button send
a suspend request event to apmd(8) instead of suspending immediately. Also
keep track of whether /dev/apm and /dev/apmctl are currently open, such that
we can still suspend immediately if apmd(8) isn't running.

ok deraadt@, marco@, pirofti@, jsing@, oga@


# 1.154 30-Mar-2010 oga

Prevent the apmd/x races for good.

When we hit suspend time, go through all wsdisplays on the system. if
they are in mode MAPPED, but not MODE_DUMBFB then if possible do a full
vt switch to a !mapped vt, and prevent switching back until resume time.
This has to be called from MD code because this involves userland
running so that X can run the vt switch signal handler. This way, any
case where we are using the "poke registers from userland" model, we
will not be on the hardware when we go down, so the kernel can actually
handle thing properly.

Tested on several acpi laptops (by kettenis@ and ian@), x40 (me and
beck@ at LEAST) and zaurus (me). Maybe others, but if so I forgot who at
this time..

Idea from deraadt somewhere over the Faroe Islands (I thought of a
similar thing myself a while ago). Much prompting from him. Ok and
comments miod@


# 1.153 25-Mar-2010 oga

Fix & vs. && in the kqfilter code.

Based on a comparison with the apm code.

ok deraadt@, kettenis@


Revision tags: OPENBSD_4_7_BASE
# 1.152 26-Nov-2009 mlarkin

Visual (and on some machines, audio) feedback while the machine is
resuming via calls to aml _SST method. This also makes the "moon" LED on
thinkpads to blink during resume and cease when resume is completed.

ok pirofti@


# 1.151 26-Nov-2009 deraadt

Toggle into "cold" when we we disable interrupts, because tsleeping
in the AML interpreter and context switching into userland processes
tends to sometimes pop the ACPI trampoline page out of our address
space. Makes lots more machines work.
ok kettenis mlarkin


# 1.150 24-Nov-2009 jsg

in the resume path evaluate _WAK before dealing with device drivers
like the spec says, not after.

ok mlarkin@


# 1.149 24-Nov-2009 deraadt

abort suspends on SMP systems until that gets written; ok mlarkin


# 1.148 23-Nov-2009 mlarkin

pci should return the result of it's children's suspend/resume calls back
to its parent.

handle suspend failure case and unwind if devices have an activate function
that returns failure.

ok deraadt@, kettenis@


# 1.147 23-Nov-2009 mlarkin

acpi* devices need to call their children on suspend/resume.
ok deraadt@


# 1.146 23-Nov-2009 pirofti

Remove ACPI_SLEEP_ENABLED checks.

This enables by default the suspend/resume paths in the kernel.

Okay deraadt@.


# 1.145 23-Nov-2009 deraadt

knf and spacing that are super annoying


# 1.144 23-Nov-2009 mlarkin

Hook up Theo's new children suspend/resume function, as well as more ACPI
suspend/resume glue.

ok deraadt@


# 1.143 26-Oct-2009 deraadt

Do not do strncmp comparisons on dv_xname because longer device names which
look similar could arrive in the future. Instead, compare directly against
dv_cfdata->cf_driver->cd_name
Issue originally spotted by miod


# 1.142 26-Oct-2009 jordan

Verify checksum+address when loading ACPI tables.
Some systems had invalid entries in RSDT/XSDT.
ok marco@


# 1.141 23-Jul-2009 cnst

New aibs(4) driver for ASUSTeK AI Booster (ACPI ATK0110) hardware monitoring,
with sensor state support through limits provided by the ACPI.

Tested on several ASUS motherboards kindly networked by Sam Fourman Jr.

ok deraadt marco jordan


Revision tags: OPENBSD_4_6_BASE
# 1.140 03-Jun-2009 pirofti

Import acpi power resource driver. This is needed for suspend/resume logic.

Okay jordan@ and marco@.


# 1.139 03-Jun-2009 pirofti

Add acpivideo support. This adds brightness support for all laptops
except thinkpads, they will use the acpithinkpad driver. The driver is
also hooked into wsconsole. So brightness can be adjusted via:

$ wsconsctl display.brightness=<percentage>

This is very helpfull on some laptops that have a nasty bios and get two
steps instead of one when pressing the brightness button.

Tested on various dell, fujitsu, acer, samsung and other laptops.

Okay marco@, miod@. Suggestions from kettenis@.
Lots of reviews and help from miod@, thanks!


# 1.138 03-Jun-2009 jordan

Added ejectable bay for IDE cdroms
ok marco@


# 1.137 30-Apr-2009 marco

Oga changed apm ioctls and forgot to grep. Temporary work around by hitting
the sleep button when we get any request. This interface will chnage at
some point.
ok oga


# 1.136 19-Apr-2009 krw

Call acpi_sleep_walk() when powering down just like when entering
any other sleep state. This tells acpi wake devices to not wake up
the box until manual powering up resets their status. Makes my Dell
GX520 stay powered off in response to 'halt -p'.

In snaps for a while.

ok marco@ (several times) deraadt@


# 1.135 17-Apr-2009 pirofti

Add device ranking support for future sleep/wake developments.

okay marco@


# 1.134 11-Mar-2009 jordan

Cleanup.. use aml_evalinteger instead of aml_evalname
ok marco@


# 1.133 10-Mar-2009 jordan

Fix cut/paste error.. call _BFS not _PTS
ok marco@


Revision tags: OPENBSD_4_5_BASE
# 1.132 21-Feb-2009 marco

overzealous cut/paste; found by Vladimir Kirillov <proger@uaoug.org.ua>


# 1.131 20-Feb-2009 marco

Oops didn't mean for Debugger() to remain there.


# 1.130 19-Feb-2009 marco

suspend/resume bits so that we can develop this in tree. This is disabled.
code from mlarkin and me
help from art,toby,jordan and several others
ok jordan, go for it deraadt


# 1.129 10-Feb-2009 jordan

Fix null pointer for shutdown if acpi not enabled
ok marco@


# 1.128 04-Feb-2009 kettenis

Some systems that claim to be ACPI 2.0 apparently don't provide an XSDT
address. Make the code fall back on the RSDT on those systems.

From Joerg Niendorf <p3y80@internode.on.net>. Fixes PR 5986.


# 1.127 17-Jan-2009 krw

A couple of stray malloc/memset -> malloc(M_ZERO), a bit of knf and
lose a couple of unnecessary casts. No functional change.

Feedback from kettenis@

ok kettenis@ marco@


# 1.126 06-Nov-2008 marco

Collapse all sanity checks of hids into one function.

Originaly from jordan


Revision tags: OPENBSD_4_4_BASE
# 1.125 02-Jul-2008 fgsch

add acpivideo. for now almost an empty skel so others can work on
suspend/resume. eventually it will also manage output switching and
brightness where supported. prodded by marco@


# 1.124 11-Jun-2008 marco

Fix $OpenBSD$ for once and for all


# 1.123 08-Jun-2008 deraadt

aaa_dev was being set from an uninitialized variable; ok toby


# 1.122 07-Jun-2008 marco

Kill ACPIVERBOSE. It was never used and is worthless to boot.

comments jmc
ok toby


# 1.121 06-Jun-2008 marco

Shrink acpi some more for floppies and while here re-enable acpi on the
floppies. Fixes crazy sony vaio kettenis has.

tested by deraadt
ok deraadt jordan kettenis


# 1.120 01-Jun-2008 marco

Fix \n nit in SMALL_KERNEL.


# 1.119 01-Jun-2008 marco

Re-enable new parser. Lots of testing by various folks. Fits all media
now too.

ok kettenis deraadt


# 1.118 16-May-2008 dlg

back out jordans new parser.

it didnt have enough oks from the right people, it did break machines
that were previously working, and it bloated the RAMDISKS and install
media beyond the constraints of the media.

requested by deraadt@


# 1.117 14-May-2008 jordan

Adding new ACPI Parser code
Old guts of code still exists, needs to be torn out next
ok marco@
ok brad@


# 1.116 27-Apr-2008 jcs

add acpithinkpad(4), an acpi support driver for ibm/lenovo
thinkpads.

provides functionality that the bios did on older, non-acpi
thinkpads like respond to screen brightness keys and toggle the
bluetooth device on and off.

ok deraadt@ and marco@


# 1.115 24-Apr-2008 jsing

Introduce acpiasus(4), a driver for the ACPI based hotkeys found in many
ASUS laptops (including the ASUS EeePC) - largely based on NetBSD's
asus(4) driver. On the ASUS EeePC this allows us to enable/disable
wireless, change screen brightness and use the volume keys.

ok jsg@, weingart@


# 1.114 12-Apr-2008 kettenis

Get rid of acpi_s5 global variables; simply send SIGUSR2 instead of SUGUSR1
to tell init(8) to power down the machine.

ok krw@


Revision tags: OPENBSD_4_3_BASE
# 1.113 21-Jan-2008 jsg

Check against RESET_REG_SUP in FADT before doing the acpi reset dance
after all, not doing so appears to break more machines than it fixes.

ok marco@


# 1.112 05-Jan-2008 kettenis

Never attach more acpicpu(4) devices than cpu(4) devices. Always attach
the first acpicpu(4) device on non-MULTIPROCESSOR kernels, regardless of its
ACPI processor ID.

ok mikeb@


# 1.111 05-Dec-2007 deraadt

Hang acpi(4) below bios(4) instead of mainbus(4). This lets us move acpi(4)
before pcibios(4), and then.... if acpi(4) attaches, skip pcibios(4) since
it messes the machines up. Fixes claudio's HP dl320 G5, and almost assuredly
others.
ok kettenis


# 1.110 05-Dec-2007 deraadt

repair acpiopen/acpiclose and friends to handle the /dev/apmctl interface,
so that apmd(8) can actually work correctly against acpi(4), and thus,
even do the fancy -C/-A stuff; also tested by jmc


# 1.109 02-Dec-2007 fgsch

fix the trailing space for real now.


# 1.108 02-Dec-2007 fgsch

avoid trailing spaces on output; marco@ ok.


# 1.107 27-Nov-2007 kettenis

Fix off-by-one that made us attach acpicpu0 and acpicpu1 on GENERIC kernels.

ok marco@


# 1.106 25-Nov-2007 jsg

When available and ACPI is enabled use the ACPI reset method
to reboot. This is required for some quirky sis machines
and likely others to reboot.

Tested by many. 'commit early commit often' deraadt@


# 1.105 19-Nov-2007 deraadt

sigh, amd64 still uses that stupid acpi_enabled variable...


# 1.104 19-Nov-2007 deraadt

softc's come initialized to 0, you know...


# 1.103 19-Nov-2007 deraadt

store revision; from jsg


# 1.102 19-Nov-2007 kettenis

Initialize sc_facs earlier.

ok deraadt@, beck@, canacar@


# 1.101 16-Nov-2007 deraadt

fix the bus_space #define nightmare, so that amd64 and i386 are much more
uniform. as a result shared code like acpi needs less #ifdef's
ok marco kettenis


# 1.100 15-Nov-2007 deraadt

remove the #ifdef ACPI_ENABLE. the EC codepath is either safe enough (and
will be improved), or it is unsafe because AML assumes it must be there
ok almost everyone


# 1.99 15-Nov-2007 mikeb

Make acpicpu(4) attach to the existing CPUs only.

Problem reported by form@. Fix idea by kettenis@.
Lots of help from deraadt@. Tested by deraadt@, form@ and me.

ok deraadt marco


# 1.98 14-Nov-2007 deraadt

spacing


# 1.97 12-Nov-2007 deraadt

correct : vs , in attach-time messages


# 1.96 06-Nov-2007 deraadt

all ACPI_ENABLE kernels need acpi_interrupt()


# 1.95 06-Nov-2007 deraadt

move acpi_read_pmreg() and acpi_write_pmreg() out of SMALL_KERNEL so
that acpi ramdisks can compile (obvious solution)


# 1.94 05-Nov-2007 canacar

Establish the acpi interrupt earlier. Fixes problems with some
GENERIC.MP kernels not getting acpi interrupts. This is a
workaround until the real issue with late establishment is fixed.
ok weingart@, beck@, gwk@


# 1.93 03-Nov-2007 jordan

Added support for displaying wakeup devices
ok beck@,weingart@,gwk@


# 1.92 11-Oct-2007 kettenis

When walking the tree yo find _PRT methods, check whether the device is
actually enabled and functioning. Similar to what we do for _INI.

ok weingart@, beck@


# 1.91 08-Oct-2007 krw

More simple memset(,0,) -> M_ZERO changes. In this batch move to
size(*p) as the first malloc() parameter where p is declared locally
and thus easy to check. Add M_ZERO to gpe_table allocation in acpi.c
even though there is no obvious bzero or memset nearby.


# 1.90 13-Sep-2007 mk

Remove unnecessary memset() (see aml_evalnode(9)) and a bit of KNF.

ok toby


# 1.89 13-Sep-2007 weingart

Implement ACPI 6.5.1 spec tree walk for _STA and _INI.
Fixes mk's laptop. No regressions so far (thank you to the
testers).

ok gwk@, mk@, marco@


Revision tags: OPENBSD_4_2_BASE
# 1.88 17-Apr-2007 mk

Now that acpidock no longer hangs machines during boot (at least
according to the reports I've been getting) it should be safe to enable
it again, so remove the #if 0 stuff around it.

ok marco


# 1.87 11-Apr-2007 jordan

Added changes for C-State
ok marco@


# 1.86 26-Mar-2007 marco

Calculate minutes left more accurately.

From giovanni <qgiovanni@gmail.com>


Revision tags: OPENBSD_4_1_BASE
# 1.85 03-Mar-2007 mk

Stop looking for acpidock until we fix that stuff that breaks all pre-60
series ThinkPads with acpidock(4) enabled.


# 1.84 22-Feb-2007 marco

We were getting no interrupts in .MP. There are 2 fixes; one in
acpi_machdep.c and move the interrupt establishment down after ioapic
has been initialized.

ok jordan


# 1.83 21-Feb-2007 kettenis

Make an acpi-enabled GENERIC.MP work on systems that only support PIC mode.

ok marco@


# 1.82 19-Feb-2007 jordan

Added acpi_map_address function for mapping GAS/IO registers
ok marco@


# 1.81 18-Feb-2007 jordan

Added dynamic perfect hash for opcodes
ok marco@


# 1.80 18-Feb-2007 marco

Shave off another K pre compression.
Shuffle SMALL_KERNEL functions around so that we only have a single #ifndef


# 1.79 17-Feb-2007 marco

Add SMALL_KERNEL throught acpi to make it fit on boot media.
ok deraadt


# 1.78 13-Feb-2007 marco

Add smarter _INI code. This helps with some dissapearing devices.

ok kettenis jordan


# 1.77 31-Jan-2007 gwk

Introduce acpi_hasprocfvs thats "ACPI Has Processor Frequency and Voltage
Scalling" a global flag set by acicpu attach to indicate that ACPI is
handling fvs through the _PSS objects. This will fix disappearing
EST/powernow when booting kernels with acpi enabled, as acpicpu isnt
compiled into GENERIC yet. Also in cases where acpicpu is built in and
legacy EST/powernow work but acpi is missing the _PSS object, the legacy
routines will be used.

tested by dim@, and Nick Nauwelaerts <nick AT nauwelaerts.net>
ok marco@, dim@


# 1.76 22-Jan-2007 mk

Actually look for docking stations by looking for _DCK nodes.

ok marco


# 1.75 02-Jan-2007 marco

On some systems EC must be attached as the first device. Move EC from
generic _HID devices to its own routine to force first attach. Bug found
and debugged by joris@

Code mostly written by joris@


# 1.74 26-Dec-2006 marco

Add polling to devices that require it such as acpibat and acpitz.
Use the same tsleep/wakeup mechanism as the interrupt code to keep it non-
concurrent.


# 1.73 21-Dec-2006 deraadt

even more knf love


# 1.72 21-Dec-2006 marco

Remove some leftover acpiec gpe/interrupt code.


# 1.71 20-Dec-2006 deraadt

oops, name already used


# 1.70 20-Dec-2006 deraadt

rename __acpi_enable_gpe(); ok marco


# 1.69 20-Dec-2006 deraadt

acpi_unmap_pmregs() is not used, ok marco


# 1.68 19-Dec-2006 deraadt

simplify some low-level methods substantially (shrinking code); ok marco jordan


# 1.67 06-Dec-2006 mk

malloc(..., M_NOWAIT) with no NULL check is bad. If allocation fails,
complain, clean up, and bail out of attach.

input and ok gwk


# 1.66 28-Nov-2006 jordan

Fixed clearing arg for acpi_init_pic
ok kettenis@


# 1.65 27-Nov-2006 kettenis

Don't spam dmesg.


# 1.64 27-Nov-2006 jordan

Fixes for acpiprt routing
Post-parse fixup for namerefs
ok kettenis@


# 1.63 25-Nov-2006 marco

Don't handle acpiev events inside the gpe handler since this causes deadlocks.

ok jordan@


# 1.62 15-Nov-2006 kettenis

Hook up acpi apic interrupt routing support to generic acpi code.

ok jordan@


# 1.61 02-Nov-2006 marco

Kill acpiec attach function and add its functionality to the generic _HID
device attach

prodded and ok jordan


# 1.60 02-Nov-2006 marco

Fix comments to reflect reality.


# 1.59 19-Oct-2006 marco

Replace ACPI ioctls with APM ioctls. This makes ACPI talk the same protocol
as APM therefore userspace can now retrieve power information in the same
fashion as all other apm arches (zaurus, macppc, i386 etc).

To use force the use of acpi device like so: apmd -f /dev/acpi

ok jordan@


# 1.58 19-Oct-2006 jordan

Fixed GPE debug levels
Fixed ref counter on temporary bufferfields
DSDT parsing fully working on IBM Thinkpad
ok marco@


# 1.57 15-Oct-2006 jordan

Removed old GPE handler code remnants
ok marco@


# 1.56 12-Oct-2006 jordan

Massive ACPI changes
Fixed ACPI leaks
new ACPI API
ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.55 30-Jun-2006 gwk

KNF, No functional change,
"go at it" marco@


# 1.54 30-Jun-2006 jordan

Support for calling Embedded Controller via new GPE interface
ok marco@


# 1.53 30-Jun-2006 jordan

Added new code for GPE handling
Also works with ACPIEC; need to fixup acpiec.c to remove original interrupt handler
ok marco@


# 1.52 30-Jun-2006 jordan

Added offset argument for acpi read/write pmreg routines (new GPE code)
ok marco@


# 1.51 31-May-2006 todd

do not call acpi_powerdown() unless acpi has successfully initialized
ok gwk@ canacar@


# 1.50 29-May-2006 canacar

Add embedded controller support.
ok marco@ deraadt@ testing & ok gwk@


# 1.49 19-May-2006 canacar

Minimal thermal zone driver. Monitors thermal zone temperature,
shuts down the system if the 'critical temperature' is reached.
ok marco@


# 1.48 11-Apr-2006 marco

KNF.


# 1.47 11-Apr-2006 gwk

Add acpi_resume(struct acpi_softc *sc), which calls some AML methods,
detected at attach time, currently unused.

ok marco@


# 1.46 11-Apr-2006 gwk

Discover some ACPI sleep related methods, and call them at the
appropriate time in acpi_enter_sleep_state().

ok marco@


Revision tags: OPENBSD_3_9_BASE
# 1.45 26-Feb-2006 marco

Add acpicpu as a device.

ok jordan@


# 1.44 23-Feb-2006 marco

remove dead function.

ok jordan@


# 1.43 22-Feb-2006 marco

Print out all tables it found during autoconf. We'll need this data in
the future.


# 1.42 22-Feb-2006 marco

Make dmesg less verbose.

ok jordan.


# 1.41 22-Feb-2006 jordan

Added parsing of SSDT tables
ok marco@


# 1.40 22-Feb-2006 jordan

Added new aml_register to notify by device id
Moved powerdown code to button device
ok marco@


# 1.39 21-Feb-2006 marco

Readjust debug levels that crept in too low over the last few days.
KNF


# 1.38 21-Feb-2006 marco

Rig the power button so that the machine shuts down correctly.


# 1.37 20-Feb-2006 marco

Add acpibtn device. Remains disabled for now.


# 1.36 19-Feb-2006 jordan

Added code for aml_notify GPE notification callback
ok marco@


# 1.35 19-Feb-2006 grange

Fix acpi_enter_sleep_state() by replacing current ugly hack
with mostly correct sleeping state transition code.

ok brad@ marco@


# 1.34 19-Feb-2006 marco

jordan:
Enable interrupts.
Added peripheral code to make this work.

marco:
Replace softintr and other types of interrupts with a handler with context.
Fixed interrupt magic for GPEs.
Some KNF

This is work done by jordan and I but has to be a single commit.


# 1.33 17-Feb-2006 marco

Remove earlier introduced delays. We really only need them during autconf.

ok jordan@


# 1.32 17-Feb-2006 marco

Add a generic delay function as per the ACPI spec.

from jordan@


# 1.31 17-Feb-2006 marco

Use tsleep instead of delay when !cold.


# 1.30 17-Feb-2006 marco

Argh, we need delays between reads/writes. The backend reads (i2c most likely)
are so slow that values come back as 0 sometimes. We have to do this
despite the spec saying we shouldnt have to. Guess having a faster
parser than others can hurt sometimes.

Idea from jordan.


# 1.29 17-Feb-2006 jordan

Fixed code for handling _INI
Rearranged debug statements
ok marco@


# 1.28 16-Feb-2006 jordan

Added stub for calling _INI
Fixed aml_dowhile for NULL return
ok marco@


# 1.27 16-Feb-2006 jordan

Fixed parser/evaluator to retrieve battery info+state
Cleanup of parser
Dell laptops return battery status
ok marco@


# 1.26 06-Feb-2006 brad

fix a typo so this will compile on amd64.


# 1.25 04-Feb-2006 marco

When ACPI_ENABLED wasn't set but the devices were it had an unknown variable.

Pointed out by ian@.


# 1.24 03-Feb-2006 jordan

Rewrote aml parser yet again...
Fixed bus_space_map for i386/amd64
Added _BST and _BIF to acpibat to get status/info
ok marco@


# 1.23 20-Jan-2006 jordan

More evaluator changes
Removed old evaluator code
Fixed _HID value
ok marco@


# 1.22 19-Jan-2006 jordan

Added support for read/write of PCI Config space
Help jason@
ok marco@


# 1.21 18-Jan-2006 jordan

Added new evaluation routines
Added helper methods for buffer/field evaluation
Fixed broken indentation on dsdt.c

ok marco@


# 1.20 17-Jan-2006 jordan

Major overhaul of the aml parser. Evaluater temporarily disabled.

ok marco@


# 1.19 06-Jan-2006 grange

Check for ACPI control availability only if ACPI_ENABLE defined.


# 1.18 06-Jan-2006 grange

Informative error messages on attach, prettier dmesg.


# 1.17 06-Jan-2006 grange

printf -> dnprintf conversion for debug messages.


# 1.16 05-Jan-2006 grange

Find SLP_TYPx values and store them for future use in
acpi_enter_sleep_state().


# 1.15 05-Jan-2006 grange

Random KNF and cleanup


# 1.14 05-Jan-2006 grange

Provide symbolic names for the acpi sleeping states and use it.


# 1.13 28-Dec-2005 marco

Moved DEVNAME to a acpidev.h

ok jordan@


# 1.12 18-Dec-2005 sturm

add back comment about wrong code

ok marco


# 1.11 16-Dec-2005 marco

Remove test for kbd_reset. Prompted deraadt@, ok jordan@


# 1.10 16-Dec-2005 jordan

Added hooks for powerdown/power button press


# 1.9 16-Dec-2005 jordan

Cleaned up common power management register i/o
Added enter sleep state ioctl
Hooked power button interrupt
ok marco@


# 1.8 16-Dec-2005 marco

Remove autoconf chatter


# 1.7 16-Dec-2005 jordan

Moved DSDT AML parsing into parent ACPI driver
ok marco@


# 1.6 14-Dec-2005 marco

Add AC device glue.


# 1.5 14-Dec-2005 marco

Adjust debug printing levels to something more sane.


# 1.4 13-Dec-2005 marco

Add glue to attach devices to apci.


# 1.3 07-Dec-2005 marco

KNF


Revision tags: OPENBSD_3_8_BASE
# 1.2 10-Jul-2005 grange

Spacing cleanup.


# 1.1 02-Jun-2005 tholo

Start on a basic ACPI framework -- does not do much more than read out the
ACPI tables into kernel memory and attach ACPI and HPET timers currently.

In order to test this code, enabling the devices in GENERIC as well as
the ACPI_ENABLE option is needed. This code does not do any thermal
control yet, so this should be done with care depending on the platform.

In the tree so more people can contribute to making this more fully
featured.

Ok niklas@ grange@ tedu@


# 1.371 02-Jul-2019 jcs

acpi_set_gpehandler: turn edge arg into flags, add support for
passing GPE_DIRECT which directly executes the supplied callback and
clears a level interrupt rather than going through acpi_taskq which
can be slow when other driver callbacks are in the queue.

This should not be used for GPE handlers that need to do anything
ACPI-related.

ok kettenis


# 1.370 10-Jun-2019 kettenis

Print proper ACPI version number.

ok mlarkin@


# 1.369 08-Jun-2019 kettenis

When establishing the mapping between ACPI device nodes and PCI devices,
skip PCI host bridges and devices that aren't there.

ok patrick@


# 1.368 07-Jun-2019 kettenis

Introduce acpi_getsta() and use it to eliminate some duplicated code.

ok patrick@


# 1.367 12-May-2019 kettenis

Make sure the extended PM register descriptions actually exist before
using them. Fixes machines with ancient ACPI 1.0 (such as recent
hypervisors).

ok deraadt@


# 1.366 12-May-2019 kettenis

Add support for mapping ACPI PM registers using the "extended" gasio
representation from the FADT. Mostly fixes the Lanner NCA-1510.

ok lteo@, mlarkin@, deraadt@


# 1.365 11-May-2019 lteo

Fix incorrect error message.

ok kettenis@


# 1.364 08-May-2019 tedu

free size. the allocation is the header plus the length.


Revision tags: OPENBSD_6_5_BASE
# 1.363 04-Apr-2019 kettenis

Avoid potential null-pointer dereference. Found by Kent R. Spillner.

ok deraadt@, jsg@


# 1.362 20-Jan-2019 jsg

fix SMALL_KERNEL build after changes in rev 1.361
reported by naddy@ ok deraadt@


# 1.361 19-Jan-2019 tedu

Add a pwraction sysctl that controls what the power button does on acpi.
By default, nothing changes -- shutdown is initiated. But allows turning
power button into a sleep button if desired.
(grudging) ok from a few parties


# 1.360 26-Oct-2018 kettenis

Add acpipci(4) on amd64. For now this only calls the PCI-specific _OSC
method to let the ACPI implementation know what features we support.


Revision tags: OPENBSD_6_4_BASE
# 1.359 30-Aug-2018 kettenis

Move softbutton check before the check that skips attaching certain acpi
devices. Fixes regression caused by matching on _CID in addition to
matching on _HID.

ok matthieu@, mlarkin@


# 1.358 25-Aug-2018 kettenis

Match ACPI devices based on _CID if no match for _HID is found.

ok mpi@


# 1.357 19-Aug-2018 kettenis

Add support for multiple PCI segments. Only really implemented for arm64
for now as amd64/i386 firmware still caters for legacy OSes that only
support a single PCI segment.

ok patrick@


# 1.356 03-Aug-2018 kettenis

Let ahci(4) match on _CLS instead of _HID when attaching at acpi(4). Avoids
having to add many more _HID entries to the match table.

ok deraadt@, mlarkin@


# 1.355 10-Jul-2018 kettenis

Make legacy interrupts work in acpipci(4).

ok patrick@


# 1.354 05-Jul-2018 kettenis

Add acpipci(4), a driver that supports generic ECAM-compatible PCI host
bridges based on information provided by ACPI.

ok mlarkin@


# 1.353 04-Jul-2018 kettenis

Properly pass around the PCI "chipset tag" in acpi(4) and refactor
acpimcfg(4) to call an MD initialization functions that sets up a tag for
PCI ECAM.

ok guenther@, mlarkin@, krw@


# 1.352 01-Jul-2018 kettenis

Move acpi(4) attach glue into acpi_machdep.c.

ok guenther@, deraadt@


# 1.351 01-Jul-2018 kettenis

Add support for _DSD Device Properties.

ok mlarkin@


# 1.350 30-Jun-2018 kettenis

Add intr_enable() function, intended for MI use to amd64 and i386 and use
this in the acpi(4) suspend/resume code paths.

ok deraadt@


# 1.349 29-Jun-2018 kettenis

Use <stdint.h> types.

ok mpi@, jung@, krw@, deraadt@


# 1.348 29-Jun-2018 mlarkin

ACPI: Allow (with warning) GPE handler reassignment, instead of returning
a failure code.

ok kettenis


# 1.347 26-Jun-2018 mlarkin

knf


# 1.346 26-Jun-2018 kettenis

Let acpi(4) pass down the bus dma tag.

ok deraadt@


# 1.345 25-Jun-2018 kettenis

Reorganize acpi(4) code a bit in preparation for upcoming arm64 support.

ok deraadt@


# 1.344 20-May-2018 kettenis

Wake up the acpi thread after scheduling a gpio event.

ok mpi@


# 1.343 17-May-2018 kettenis

Sprinkle some #ifndef SMALL_KERNEL such that RAMDISK kernels cuild again.

pointed out by stsp@


# 1.342 17-May-2018 kettenis

Implement GenericSerialBus OpRegion support.

ok mlarkin@


# 1.341 27-Mar-2018 kettenis

Add acpicmos(4), a driver that implements SystemCMOS OperationRegion
access support. This fixes machines where the AML doesn't check whether
support for this OperationRegion type has been registered by the OS.

ok mlarkin@


Revision tags: OPENBSD_6_3_BASE
# 1.340 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.339 10-Feb-2018 deraadt

Syncronize filesystems to disk when suspending. Each mountpoint's vnodes
are pushed to disk. Dangling vnodes (unlinked files still in use) and
vnodes undergoing change by long-running syscalls are identified -- and
such filesystems are marked dirty on-disk while we are suspended (in case
power is lost, a fsck will be required). Filesystems without dangling or
busy vnodes are marked clean, resulting in faster boots following
"battery died" circumstances.
Tested by numerous developers, thanks for the feedback.


# 1.338 08-Feb-2018 deraadt

After flushing disks, try again to release all memory. We don't need to
save dirty memory to the hibernate space.


# 1.337 08-Feb-2018 deraadt

Toss all releaseable memory, because fragmentation can get in the way
of allocating the hibernate playpen.


# 1.336 08-Feb-2018 deraadt

sr_quiesce() is a new approach for ensuring that softraid drains
output to the disks.

This is part of a larger suspend/resume filesystem-safety diff, which
has been worked on for a couple of months already. Tests by job, krw,
beck, benno, and others. Sometimes even by snapshot users...


# 1.335 29-Nov-2017 kettenis

Add support for GPIO-signaled events.

ok mlarkin@


# 1.334 16-Nov-2017 jcs

Add PCI attachment for dwiic(4) needed by Intel 100 Series machines

ok kettenis


# 1.333 14-Nov-2017 kettenis

Add a few more PNP IDs to the skip lists.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.332 17-Aug-2017 stsp

Make dwiic(4) attach its ACPI parent dependencies.
This change makes the Asus E200HA keyboard work.

Original analysis and patch by Cesare Gargano
ok kettenis@


# 1.331 09-Aug-2017 dcoppa

Add "PNP0303" (8042 PS/2 Controller) to acpi_isa_hids[]

ok kettenis@


# 1.330 22-Jul-2017 jcs

acpibat: look for _BIX first before falling back on _BIF

ACPI 4.0 deprecated _BIF for battery status so some newer machines
are exporting _BIX which extends _BIF's package with a few extra
fields. When using _BIX, export the new discharge cycle count as a
sensor.

feedback from kettenis and anton
ok deraadt


# 1.329 20-Jul-2017 kettenis

Start a list of isa(4) device names such that we can avoid printing them
as "not configured".

ok mikeb@, millert@, deraadt@


# 1.328 08-Apr-2017 deraadt

Sensors are run as callbacks inside tasks. During suspend a sensor
could be running inside a driver that will be force-detached, or due
to tsleep end up disrupting the softstate/hardstate contract. At
suspend time, quisce all these callbacks by waiting for completion.
This issue has never been observed for real, but may be implicated in
suspend/resume failures.
ok kettenis guenther mlarkin


# 1.327 08-Apr-2017 deraadt

Two prototypes for acpi_maptable; merge to one place


Revision tags: OPENBSD_6_1_BASE
# 1.326 27-Mar-2017 deraadt

Now that hibernate_alloc() only has clean success/failure, don't
need to call hibernate_free() to clean up a partial mess.
ok mlarkin kettenis


# 1.325 13-Mar-2017 deraadt

allow ramdisks to build; ok jcs


# 1.324 12-Mar-2017 jcs

try to attach acpisbs first and if successful, don't attach acpibat


# 1.323 02-Mar-2017 natano

Add a new sysctl machdep.lidaction. The sysctl works as follows:

machdep.lidaction=0 # do nothing
machdep.lidaction=1 # suspend
machdep.lidaction=2 # hibernate

lidsuspend is just an alias for lidaction, so if you change one, the
other one will have the same value. The plan is to remove
machdep.lidsuspend eventually when people have upgraded their
/ets/sysctl.conf.

discussed with deraadt, who came up with the new MIB name
no objections mlarkin
ok stsp halex jcs


# 1.322 28-Feb-2017 natano

Untangle abstract sleep modes from ACPI sleep states. This paves the way
for further work in the area.

idea and ok deraadt
ok mlarkin


# 1.321 22-Feb-2017 jcs

add acpisbs, an acpi smart battery subsystem driver reading data
over smbus

currently disabled because it conflicts with acpibat

ok deraadt, kettenis


# 1.320 14-Jan-2017 kettenis

Revert aml_rdpciaddr changes; breaks several machines that were working
before.


# 1.319 11-Jan-2017 jsg

Increase the size of the buffers used for _HID and _CID strings to be
able to fit "Hyper_V_Gen_Counter_V1".

ok mlarkin@ deraadt@


# 1.318 08-Jan-2017 kettenis

Make aml_rdpciaddr() work for for devices behind bridges. The code now
uses the mapping between AML nodes and PCI devices that we establish.
Because _INIT methods may end up calling aml_rdpciaddr(), make sure we
create that mapping early. Also handle devices that aren't actually present.
These devices are now included in the mapping and reads will return an
all-ones patterm whereas writes are a no-op..

ok mlarkin@


# 1.317 25-Oct-2016 pirofti

Add the resource index to aml_parse_resource() callback function.

This is needed by an upcoming acpiec commit that handles machines breaking
the current ACPI specifications.

Change suggested by and ok kettenis@, guenther@


# 1.316 18-Sep-2016 guenther

If the APIC table (MADT) isn't present then ci_acpi_proc_id won't be set
when acpicpu tries to attach, leading to a NULL dereference. The ACPI
processor object on the Dells where this was reported is bogus and useless
anyway so apply the same test when deciding whether to attach to processor
objects as acpicpu would when matching them to cpus.

Problem reported by Eivind Eide (xenofil (at) gmail.com) and
Johan Huldtgren (johan+openbsd-bugs (at) huldtgren.com).
Testing by Eivind Eide.
ok kettenis@ mlarkin@


# 1.315 03-Sep-2016 naddy

Write the system time back to the RTC every 30 minutes.
This fixes the problem that long-running machines which were not
shut down properly would reboot with a badly offset system time.

hints and ok kettenis@


# 1.314 31-Aug-2016 mlarkin

Don't put a device in the wake device list if _STA says it's not there.

Fixes at least one machine that woke up from sleep / poweroff immediately.

ok kettenis@, tested by kettenis and martijn@


# 1.313 28-Jul-2016 kettenis

Store the acpi processor ID/UID in struct cpu_info, and use it to attach
acpicpu(4) drivers to the right cpu(4).

ok mlarkin@, guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.312 10-Jun-2016 kettenis

The power management timer has been made optional in ACPI 5.0A, so don't
attempt to attach acpitimer(4) if the timer isn't present.

Pointed out by jcs@

ok jcs@


# 1.311 08-May-2016 kettenis

Make sure devices listed by a device's _DEP method are attached before
we attach the device itself. The _DEP methode, introduced in ACPI 5.0,
is a hint that the device in question depends on OpRegion support from
the devices listed by _DEP, so we have to order them the proper way.
To prevent us from attaching those devices again when we encounter them
later walking down the device tree, keep track of our attempts to
attach a device driver to them using aflag in the aml_node structure.

ok guenther@


# 1.310 25-Apr-2016 pirofti

We attach acpihpet(4) and acpiec(4) without using the HID, so do not
report it in the dmesg.

OK deraadt@, mlarkin@.


# 1.309 02-Apr-2016 kettenis

Reduce dmesg spam by knocking out some acpi hardware IDs that don't need a
driver.

ok mpi@


# 1.308 29-Mar-2016 kettenis

Check if a device is present (using _STA) in acpi_foundhid. This prevents
us from reporting devices as "not configured" that aren't actually present,
and allows us to remove duplicated code from several drivers.

ok deraadt@, ok jsg@


# 1.307 29-Mar-2016 kettenis

Implement a cleaner way to prevent acpivout(4) from attaching if we have
a platform-specific method of controlling the backlight.

ok millert@, mlarkin@, jung@


# 1.306 28-Mar-2016 kettenis

Adding each and every new acpi device driver to acpi_foundhid() is
gettinga bit of the burden. And it really isn't how our autoconf
framework is supposed to work for busses that can be enumerated. All
the drivers already check for a matching _HID in their attach
function. So we can just drop the checks in acpi_foundhid() and let
the drivers sort themselves out.

Print a "not configured" line for devices for which we don't attach adriver.
This may be a bit much as there are devices for which we will not have a
driver. This will be removed if it causes too much dmesg spam.

ok deraadt@, mlarkin@


Revision tags: OPENBSD_5_9_BASE
# 1.305 17-Jan-2016 jsg

Take acpi_foundhid() outside of #ifndef SMALL_KERNEL so the ramdisk
kernel can find and attach dwiic(4) and sdhc(4).


# 1.304 17-Jan-2016 jsg

remove an unused global variable


# 1.303 14-Jan-2016 kettenis

Attach dwiic(4) on the Intel Bay Trail i2c controllers as well.


# 1.302 12-Jan-2016 kettenis

Improve emulation of PM registers on Hardware-redcued ACPI by emulating
SPL_TYPx, SLP_EN and WAK_STS using the new SLEEP_STATUS_REG and
SLEEP_CONTROL_REG registers. Makes the transition into S5 (powerdown) work
on the ASUS X205TA.

ok deraadt@, mlarkin@


# 1.301 12-Jan-2016 jcs

Add dwiic, a driver for the Synopsys DesignWare i2c controller found
on the Samsung ATIV Book 9 laptop. This initial version only
supports ACPI config/attachment.

Add ihidev, a HID-over-i2c driver largely based on uhidev. dwiic
handles attaching ihidev devices found in ACPI.

Add ims, a HID-over-i2c mouse/trackpad driver to get basic cursor
and button functionality from HID-compliant i2c trackpads.

ok kettenis deraadt


# 1.300 11-Jan-2016 kettenis

Make sdhc(4) attach to hardware IDs "80860F14" and "PNP0FFF".


# 1.299 09-Jan-2016 kettenis

Initial support for Hardware-Reduced ACPI. This skips the legacy mode checks
by emulating the PM_CNT register such that SCI_EN is always enabled. It also
skip attaching acpitimer(4) since the relevant registers are no longer there.

ok deraadt@, mlarkin@


# 1.298 06-Jan-2016 kettenis

Follow the spec and check the SCI_EN bit to see if ACPI has already
been enabled and don't bother enabling it again if it is. This should fix
systems that don't implement legacy mode and therefore set ACPI_ENABLE and
ACPI_DISABLE to zero.

ok mlarkin@, yasuoka@


# 1.297 23-Nov-2015 reyk

Back out the CRITICAL change, turn it back from 10% to 25%. Some
people with old/broken batteries didn't like this change, and the
underlying heuristics are wrong.


# 1.296 22-Nov-2015 reyk

The "CRITICAL" APM battery level, that is exported/faked by the ACPI
stack, is currently set to 25%. With 25% battery left, a typical
modern laptop will have at least one hour runtime left. It is an
arbitrary value, and other APM power layers in the kernel set CRITICAL
to something like 5% or 10%, so set it to 10% for now.

OK mlarkin@


# 1.295 28-Sep-2015 deraadt

In low-level suspend routines, set cold=2. In tsleep(), use this to
spit out a ddb trace to console. This should allow us to find suspend
or resume routines which break the rules. It depends on the console
output function being non-sleeping.... but that's another codepath which
should try to be safe when cold is set.
ok kettenis


# 1.294 06-Sep-2015 deraadt

clarify malloc arguments (base + length...)


# 1.293 28-Aug-2015 deraadt

fairly simple sizes for free(); ok tedu


# 1.292 20-Aug-2015 kettenis

Respect the access size when reading or writing to pci config space.
This version also makes sure writes are properly aligned, preventing
the panics seen with the previous incarnation of this change.

Fixes battery status passthrough in vmware, brightness keys on my x220
and who knows what else.

ok mlarkin@, deraadt@


Revision tags: OPENBSD_5_8_BASE
# 1.291 04-Aug-2015 deraadt

If we are setting ACPI_PM1_BM_RLD do it only once, and remember to
repeat this action at resume time.
ok mlarkin guenther


# 1.290 29-Jul-2015 deraadt

Something subtle in previous revision is locking up suspend or booting
on some laptops. Mailing lists threads mention x220 and Asus machines.
Reverting for now.


# 1.289 21-Jul-2015 kettenis

iRespect the access size when reading or writing to pci config space.
Fixes battery status passthrough in vmware and who knows what else.

Tested by many.

ok mlarkin@, deraadt@


# 1.288 17-Jul-2015 mlarkin

fix comments


# 1.287 26-Mar-2015 jsg

Use the 0x10 extended capability to determine if the trackstick
buttons are wired to the trackpad and need to be re-routed to the
trackstick. Without this change the buttons on 2015 thinkpads
get picked up as extended buttons that show up as scroll up/down.

Remove the X1 Carbon 2015 (LEN0048) and X250 (LEN0046) from the
top button area/soft buttons quirks list. Also avoid using the
quirk list entirely if the capability bit is set.

Based on work from jcs@ with feedback from mpi@
ok mpi@ jcs@


# 1.286 20-Mar-2015 kettenis

The ACPI standard says that a PCI function number of FFFF can be used to refer
to all functions on a device. This makes no sense in the context of accessing
PCI config space. Nevertheless stsp@ has a machine with AML that tries to
do config space reads from a device where _ADR encodes function number FFFF.
This is almost certainly buggy AML, but since Linux (and presumably Windows)
boot just fine on this machine we need to work around this. Linux effectively
treats function FFFF as function 7, which isn't present on this machine.
Instead we simulate a read from a non-existent device by returning a bit
pattern of all ones. Writes to function FFFF will still panic.

ok mlarkin@


# 1.285 16-Mar-2015 deraadt

Reduce use of SMALL_KERNEL in acpi, bringing acpiec driver and more gpe
handling into RAMDISK. This is now possible because the install media
has ample room. The goal is to reduce special cases where we may be
skipping (unknown) important operations...
ok mlarkin kettenis


# 1.284 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.283 07-Feb-2015 deraadt

New framework that allows hibernate to pass in entropy from it's fresh
boot.
ok mlarkin


# 1.282 06-Feb-2015 mlarkin

Fix a hibernate crash on some machines due to unmapping a page that
may not have been mapped previously (in the failure to hibernate case).

Also ensure that the lowmem ptp is mapped in all cases (not just MP).

ok kettenis


# 1.281 17-Jan-2015 deraadt

resume_randomness() before spinning up other cpus...


# 1.280 15-Jan-2015 jsg

Define a new wscons mouse type for Synaptics clickpad devices that lack
physical buttons. This will be used if the acpi pnpid for the mouse
matches a list provided by Synaptics found in Linux.

Combined with the xenocara changes this will remove the need for an xorg
config file for the x240/t440/t540 generation of thinkpads.

ok matthieu@ shadchin@ kettenis@


# 1.279 11-Jan-2015 kettenis

Tolerate checksum errors in ACPI tables (but complain about them). Apparently
there are machines out there with broken BIOSen with such checksum errors in
thier RSDT tables. Windows runs fine on these machines, so it probably does
not check. Linux ignores checksum errors as well (and prints a warning).

ok deraadt@, krw@


# 1.278 18-Dec-2014 deraadt

suspend and resume the RNG (at what looks like the right spots)
ok reyk djm


# 1.277 09-Dec-2014 doug

Replace some malloc(n*size,...) calls with mallocarray().

ok tedu@ deraadt@


# 1.276 06-Dec-2014 mpi

If "machdep.lidsuspend" is not set, do not go back to sleep if the lid
is still closed when resuming. This allows docked laptop to be resumed
without opening them.

ok mlarkin@


# 1.275 23-Nov-2014 mlarkin

If a machine has no _LIDs, then none can be open. This diff fixes a problem
where machines without any _LIDs were going back to sleep after hibernate
due to the recent suspend-if-lid-closed-on-resume diff. Noticed on qemu,
which doesn't have any _LIDs.

discussed with deraadt


# 1.274 18-Nov-2014 krw

Nuke yet more obvious #include duplications.

ok miod@


# 1.273 08-Nov-2014 mlarkin

If resuming from sleep (zzz/ZZZ) and the lid is still closed, go back to
sleep. This prevents accidental lid flex or slight opening in a backpack
from waking the machine up and leaving it resumed (powered on).

ok deraadt@


# 1.272 26-Sep-2014 kettenis

Rework piglet and pig allocation. Currently the piglet gets allocated
deep down in the suspend path, where it is really hard to recover from
allocation failure. So allocate the piglet early on in the suspend path.
Also change the piglet and piglet allocation functions to use km_alloc(9)
instead of doing pmemrange magic. This removes a bunch of code which, in the
case of the piglet allocation, is broken since it results in a NULL pointer
dereference. Also switch the piglet allocation to not wait. If we can't
allocate 16MB of phys contig memory on a halfway modern machine we're almost
certainly under a lot of memory pressure and we're better off not trying to
hibernate anyway.

ok mlarkin@


# 1.271 25-Sep-2014 kettenis

Bring back rev 1.249. Now that mp_setperf() has been fixed, it should be
safe to restore perflevel when resuming. Should fix the issues people
see with the processor running at full speed heating up the machine while
the apm command reports that the CPUs are running at a low speed.

ok dcoppa@, deraadt@


# 1.270 23-Sep-2014 kettenis

Release the acpi lock around calls to wsdisplay_suspend() and
wsdisplay_resume(). The X server may adjust the display brightness just
before/after the VT switch when suspending/resuming. In fact, it may
already be blocked adjusting the display brightness for some reason
unrelated to the suspend request.

The VT switch request will eventually time out, but the delay is annoying
and the failed VT switch will lead to problems after resume.

Needed for a future update of the xf86-video-intel driver.

ok mlarkin@


# 1.269 19-Sep-2014 kettenis

Use config_suspend_all(9).


# 1.268 08-Sep-2014 kettenis

Clear the PME Status bit on PCI devices in response to Device Wake events.
Fixes the interrupt storm on the 2009 Mac mini with WOL enabled on its
nfe(4) interface.

ok mlarkin@


Revision tags: OPENBSD_5_6_BASE
# 1.267 20-Jul-2014 mlarkin

Support hibernating to softraid crypto volumes.

much help and ok from deraadt@


# 1.266 16-Jul-2014 mlarkin

Reenable hibernate RLE support and flush+zero all memory after unpack.


# 1.265 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.264 12-Jul-2014 kettenis

Add a function to drop all clean pages on the page daemon queues and call
it when we hibernate.

ok mlarkin@, miod@, deraadt@


# 1.263 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.262 11-Jul-2014 mlarkin

Flush the buffercache to 16MB on hibernate and restore its previous max
size (kern.bufcachepercent) on resume, for better hibernate performance.

ok beck@


# 1.261 10-Jul-2014 blambert

remove unneeded header

ok mlarkin@


# 1.260 01-Jul-2014 kettenis

Adding an additonal delay after each access to SystemMemory or SystemIO
during autoconf makes no sense.

ok deraadt@


# 1.259 23-Jun-2014 kettenis

SystemMemory is used to access memory mapped registers on some machines, so we
must use the bus_space(9) API to access it instead of memcpy(9). Also make
sure we properly align access to fields that need more than byte alignment.

ok guenther@


# 1.258 21-May-2014 mlarkin

Fix invalid / missing format specifiers in several dnprintf statements
(used only when ACPI_DEBUG is enabled)

From a diff on tech@ by Fabian Raetz, with a minor change by me, tested
on i386 and amd64 with ACPI_DEBUG enabled and disabled.


# 1.257 25-Apr-2014 mlarkin

Defer the free of the hibernate structures until later, when it is safe to
do so.

Freeing the hibernate structures too early leads to inadvertent calls into
splx when we aren't ready for them yet. Found when trapping splx calls in
the resume path.

discussed with deraadt@ at m2k14


# 1.256 13-Apr-2014 mlarkin

Fix a badly worded debug message that implied the entire machine was going
to sleep, instead of just the acpi thread going to sleep.


# 1.255 31-Mar-2014 mpi

Change the order of operations during a suspend/resume cycle and
call bufq_quiesce() after executing the DVACT_QUIESCE handlers.

This should be safe since no disk nor controller drivers have such
handler but it will allow us to detach sd(4) devices attached to a
USB bus.

Another benefit pointed out by kettenis@ is that drivers that need
to read a firmware from the disk should be able to do it at resume
time in a DVACT_WAKEUP handler.

ok kettenis@, deraadt@


# 1.254 13-Mar-2014 dlg

get rid of the assumption that the head of the alldevs list is the
"mainbus" device. this breaks when mpath is enabled because it
attaches before mainbus and therefore takes the head position.

have autoconf provide device_mainbus() which looks up mainbus_cd,
and use that instead.

discussed with deraadt who just wants mpath stuff to move forward
despite there being many ways to shine this particular turd.


Revision tags: OPENBSD_5_5_BASE
# 1.253 21-Feb-2014 deraadt

Do the lid control further on in the resume sequence, since this is
AML, and will do WAITOK operations. While cold, that is a bad idea.
We seem to be safe with respect to the spec, I do not see any event
debounce issues here.
ok kettenis, tested by many


# 1.252 20-Jan-2014 claudio

Instead of checking sc->sc_revision <= 1 check that the
fadt->hdr_revision <= 1. This is a better check since it checks
the version of the proper descriptor. This allows the alix APU
to reboot since those boards have currently a wrong sc->sc_revision.
Committing this now so that it can be tested easier.
With and OK jsg@ also fine by derradt@


# 1.251 24-Dec-2013 kettenis

If the FADT has its SMI_CMD set to zero, assume we're only ACPI-only hardware
and don't need to disable SMI ownership of the ACPI hardware registers.

ok mlarkin@


# 1.250 23-Dec-2013 kettenis

Revert previous commit. It makes resume hang on some MP systems running
GENERIC.MP.


# 1.249 16-Dec-2013 kettenis

Restore hw.setperf upon resume like we do for apm(4) on i386. Seems we need
to do this fairly late in the resume to avoid hangs.

ok deraadt@, mlarkin@


# 1.248 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


# 1.247 06-Nov-2013 mpi

Add support for Power Resources for Dx states and the necessary hook
for PCI devices. This hook should be called twice, before and after
changing the power state of a PCI device.

Before setting the device to the new state, the ACPI layer will notify
every power resources linked to the device for that state and make sure
they are turned "_ON". After changing the state of the device, it will
decrement the reference of every power resources linked to that device
for the old state and turn them "_OFF" if they are no longer referenced.

This fixes the no-USB after resume problem seen on various ThinkPad,
problem initialy diagnosed with Alexander Polakov.

ok kettenis@, deraadt@


Revision tags: OPENBSD_5_4_BASE
# 1.246 01-Jun-2013 mlarkin

Stop printing ACPI wakeup devices in dmesg after the 16th wakeup device.

Helps with VMware VMs that report hundreds of wakeup devices.

ok reyk@, deraadt@


# 1.245 31-May-2013 bluhm

Fix the build for a kernel without wd and pciide in its config.
OK deraadt@ tedu@


# 1.244 30-May-2013 deraadt

Enforce ca_activate tree-walks over the entire heirarchy for all events,
cleaning up some shutdown-hook related code on the way.
(A few drivers related to sparc are still skipped at kettenis' request)
ok kettenis mlarkin, tested by many others too


# 1.243 18-Apr-2013 deraadt

pin the acpi thread to the BSP, because AML and SMI cannot be trusted. I
was certain we were doing this already, but it turns out no...


Revision tags: OPENBSD_5_3_BASE
# 1.242 24-Dec-2012 guenther

In acpiioctl(), Return though the splx() path if suser() returns an error

ok deraadt@


# 1.241 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


# 1.240 04-Oct-2012 ehrhardt

Use information provided by ACPI to attach secondary PCI host bridges.

ok kettenis@


# 1.239 07-Sep-2012 kettenis

Add a new function called acpi_pci_min_powerstate() that returns the lowest
power state for a PCI device for the power state (currently S3 or S4) we're
transitioning into. This function is probably too simplistic; there is a lot
of (silly) complexity in the ACPI spec.

ok deraadt@


Revision tags: OPENBSD_5_2_BASE
# 1.238 13-Jul-2012 pirofti

Disable acpivideo(4) on ASUS laptops.

There are some models that panic whenever an event is received.
Some cope, some don't. In order to get this right acpiec(4) requires
major rework. Until then let machines boot.

Discussed with deraadt@.


# 1.237 11-Jul-2012 mlarkin

for now, restrict hibernate to root in the acpi ioctl kernel interface.
normal rules for controlling this interface through apmd still apply.

ok deraadt@


# 1.236 09-Jul-2012 deraadt

tiny little oops in untested hibernate block


# 1.235 09-Jul-2012 deraadt

clean up ioctl() case statements


# 1.234 09-Jul-2012 deraadt

acpi_disable_onegpe() is not used; ok pirofti


# 1.233 24-May-2012 kettenis

If _MIN > _MAX consider a WordBus resource to be invalid. Fixes a panic on
the IBM x3550-M4.

ok mikeb@


# 1.232 29-Mar-2012 mlarkin

Missing #ifdef in previous diff


# 1.231 29-Mar-2012 mlarkin

Prevent hibernation attempts on machines with unsupported disk controllers.
The same check is also performed later in the hibernate sequence, but by
then it's too late to cleanly unwind (presently).

ok deraadt


# 1.230 26-Mar-2012 deraadt

simplify conditionals in acpi_sleep_state()
ok mlarkin


# 1.229 26-Mar-2012 deraadt

hook in the hibernate request code; half of this diff is from mlarkin
ok mlarkin


Revision tags: OPENBSD_5_1_BASE
# 1.228 20-Sep-2011 deraadt

Very early in suspend while the clock is still ticking realtime,
save the clock back to the rtc. The zaurus already did this.
ok phessler miod kettenis


Revision tags: OPENBSD_5_0_BASE
# 1.227 02-Jul-2011 nicm

kqueue attach functions should return an errno or 0, not a plain 1. Fix
the obvious cases to return EINVAL and ENXIO.

ok tedu deraadt


# 1.226 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.225 16-Jun-2011 pirofti

Import acpitoshiba driver.

This is originally written by Hiroyuki Aizu, ported and integrated
in our tree by Javier Vazquez <javama4 AT gmail> with tweaks and
suggestions by me.

Import prodded by deraadt@ and the lack of testers on tech@.


# 1.224 27-Apr-2011 jcs

attach acpithinkpad to newer lenovo models like the x120e

ok deraadt@


# 1.223 22-Apr-2011 jordan

Fix uninitialzied variables and formatting strings (-Wxxx errors)


Revision tags: OPENBSD_4_9_BASE
# 1.222 02-Jan-2011 jordan

Re-add code for ACPI taskqueue, eliminates need for state vars in acpi softc
Serialize processing ACPI tasks for sleep/powerdown/GPE event
ok deraadt@


# 1.221 31-Oct-2010 guenther

Revert last commit: it breaks resume on Thinkpads
"then please back it out" deraadt@


# 1.220 26-Oct-2010 jordan

Add task queue for ACPI gpe and notify handlers
This fixes eject on Dell Latitude dock and an issue on the Dell Mini
battery update.
ok deraadt


# 1.219 07-Oct-2010 jordan

Create separate functions for enabling wake and runtime gpes.
Remove old suspend/resume gpewalk function
ok deraadt, mlarkin


# 1.218 05-Oct-2010 jordan

split acpi_enable_onegpe to separate enable/disable functions
ok deraadt


# 1.217 05-Oct-2010 jordan

Added acpi_disable_allgpes function to clear GPE status
ok deraadt


# 1.216 05-Oct-2010 deraadt

clean up acpi_softc use more


# 1.215 05-Oct-2010 deraadt

delete a bit of #if 0 code


# 1.214 05-Oct-2010 deraadt

hide use of our global acpi_softc pointer a bit better


# 1.213 29-Sep-2010 deraadt

grab the acpi thread lock very early in acpi thread startup; not that it
is likely that anyone will race us... but why be careless.
ok kettenis


# 1.212 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.211 29-Aug-2010 jasper

- add a check for FWRITE here too.

ok miod@


# 1.210 27-Aug-2010 kettenis

Make PCI Power Management optional, and only enable it when acpi(4) attaches.

ok deraadt@


Revision tags: OPENBSD_4_8_BASE
# 1.209 08-Aug-2010 kettenis

Close race between the acpi thread and wscons ioctls running in process
context. We might rip this out post-release and replace it with a solution
that serializes things in a more obvious way.

ok deraadt@, miod@ and "those crazy texans who ok anything"


# 1.208 08-Aug-2010 deraadt

Report the acpi sleep states when on a RAMDISK; ok krw


# 1.207 07-Aug-2010 kettenis

Enforce acpi register access rescrictions.

ok mlarkin@, deraadt@, tested by marco@


# 1.206 07-Aug-2010 deraadt

upon resume, notify all the acpiac and acpibat drivers to update
their status. some laptops do not do this automatically.
ok kettenis canacar


# 1.205 06-Aug-2010 marco

Always call _PSW on _LID devices that support it to enable lid open to
resume the machine. Conversely disable it when the machine wakes up.

Tested by several ok deraadt


# 1.204 06-Aug-2010 deraadt

As part of a revamp of the PM1/GPE code, I write this diff to look for
unmanaged EN & STS bits in the PM1 register at interrupt time and report
them. As a side effect this splits the STS acknowledgement into two writes
(for power, and sleep) instead of one. The printf that is added (to spot
unmanaged STS bits) has not yet been triggered as far as we know.

Before the "write to PM1 registers at the right offset" diff went in, this
was not neccessary. But newer thinkpads do not have a working soft power
button without this diff.

We have no idea why.
ok mlarkin kettenis


# 1.203 05-Aug-2010 deraadt

PM1 power button events were generating 'fake events' to acpibtn PWRB,
but if there is no such device these were being tossed. Instead, arrange
for all power button handlers to flag a softc variable which is processed
by the thread; so that one place will know to request a powerdown (psignal
to init)
ok marco mlarkin


# 1.202 05-Aug-2010 deraadt

rename a variable which is annoyingly poorly named


# 1.201 05-Aug-2010 mlarkin

Write to the correct offset for various ACPI pmreg registers. Writing to
the correct register does properly clear the wake status bits and fixed
event bits, which allows some machines to have more than 1 suspend cycle.

bug spotted by deraadt@

tested by deraadt@ and myself on a half-dozen machines and others on
hackers@

ok deraadt@, marco@


# 1.200 05-Aug-2010 deraadt

Fix the power button. The pm1 register is 16 bits in size.
My mistake, a while back.
tested by krw


# 1.199 04-Aug-2010 kettenis

Print supported sleep states.

ok marco@, phessler@, deraadt@


# 1.198 03-Aug-2010 mlarkin

Use the proper flag for re-enabling certain hardware events (power btn,
sleep btn). The (incorrect) flag prevoiously used coincidentally had the
same value, so this shouldn't cause any different behavior than before.

tested on a variety of machines (i386, amd64, sp, mp) - no behavioral
change seen

ok deraadt@


# 1.197 28-Jul-2010 marco

Make all gasio failures look and behave the same.


# 1.196 28-Jul-2010 mlarkin

Delay a little longer on suspend before giving up. Fixes machines that
sometimes don't suspend because the sleep transition takes too long
(symptoms like failing to sleep but the backlight stays on).

ok deraadt@


# 1.195 27-Jul-2010 deraadt

remove XXX; EC can now be attached early when needed; ok jordan


# 1.194 27-Jul-2010 deraadt

more brackets feels good


# 1.193 27-Jul-2010 deraadt

XXX annotate a block in acpi_enter_sleep_state() that is pretty worrying


# 1.192 27-Jul-2010 deraadt

horrific indentation that nearly made me puke


# 1.191 27-Jul-2010 mlarkin

Set the sleeping indicator light on machines that support it, in case SMI
doesn't do it for us. Shows the 'moon' LED on older Thinkpads when
sleeping.

Tested on T43p, W500, T510 by me and on X61s by deraadt@.

ok deraadt@


# 1.190 27-Jul-2010 jordan

Early initialization of acpiec if ECDT table exists
Fixes hang when booting thinkpads while docked
ok deraadt


# 1.189 26-Jul-2010 deraadt

remove merge error which was commited


# 1.188 26-Jul-2010 pirofti

Add support for Sony ACPI hotkeys via a new driver: acpisony(4).

Currently it only works for the suspend button and tries to do right for
the brightness events, but I haven't found a Sony laptop that like to do
right so far.

In the future I want to make the brightness keys work on all Sony's and
also add support for the zoom hotkeys and whatever other funky keys I can
find on those things.

Okay deraadt@.


# 1.187 22-Jul-2010 deraadt

pretty large cleanup of SMALL_KERNEL


# 1.186 21-Jul-2010 deraadt

in the acpi GPE handler, late ack level interrupts not edge interrupts.
Duh.


# 1.185 21-Jul-2010 deraadt

Merge the edge and level gpe handlers into one (now possible since the
gpe structure tells us whether the gpe is edge or level)


# 1.184 21-Jul-2010 deraadt

delete #if 0 code we are not going to use; ok marco


# 1.183 20-Jul-2010 deraadt

make the mask code for acpi_enable_onegpe() match other similar code


# 1.182 20-Jul-2010 deraadt

Handle the acpi interrupt controller with proper edge/level handling,
wrapped in spltty to avoid racing against the interrupt controller.
Repair the gpe bit masking code while there, and do operations in the
right order
ok kettenis mlarkin, help from jordan at figuring out the order of
operations.


# 1.181 20-Jul-2010 matthew

When we're preparing to go to sleep, only call bufq_restart() if other
preparations failed.

ok kettenis@, thib@, deraadt@


# 1.180 19-Jul-2010 deraadt

permit compilation with ACPI_DEBUG


# 1.179 19-Jul-2010 deraadt

there is no need for a seperate dsdt_softc pointer; dsdt does not have a
softc but is just a part of the single acpi driver.
ok jordan


# 1.178 19-Jul-2010 deraadt

acpi_delay() is no longer needed; ok jordan


# 1.177 18-Jul-2010 mlarkin

Clear event status on resume, to avoid going back to sleep/reboot/shutdown
immediately after resume. Tested by a bunch of people on hackers@ on
various hardware. Fixes at least Toshiba NB200 and Gateway LT3103u.

ok marco


# 1.176 13-Jul-2010 deraadt

rename some thread related interfaces so they are logical


# 1.175 10-Jul-2010 jordan

Verify _ADR bdf is correct (some systems return 0xffff)
Works on Dell x9xx and Studio 1555


# 1.174 09-Jul-2010 jordan

Clear the noise


# 1.173 08-Jul-2010 jordan

Add mapping for ACPI device to PCI bdf (match autoconf tree)
Simplify resource parsing function to use buffer argument
Convert namespace linked lists to use queue macros
ok marco@, deraadt@


# 1.172 06-Jul-2010 deraadt

Use spltty() to lock downcalls from apm against the information being
modified by the acpi thread.
ok kettenis


# 1.171 06-Jul-2010 deraadt

Don't use a workq to do the suspend, because it races aginst the acpi
thread. Instead, just tell the acpi thread to do the suspend for us.
This makes apmd & zzz work correctly. While here, have acpithinkpad
attempt to post the event to apm before prompting the suspend itself.
ok kettenis marco mlarkin


# 1.170 05-Jul-2010 mlarkin

Reset register SCI_EN on resume. The ACPI spec clearly states we don't have
to do this, but many machines don't, or forget to, which leads to all sorts
of problems on resume (broken battery stats, overheating, failure to resume
more than once, etc).

tested by many, ok marco@, deraadt@


# 1.169 01-Jul-2010 thib

do bufq quiesce again, since vnd/the bufq_done call in
biodone has been fixed.


# 1.168 01-Jul-2010 jordan

Backout recent AML changes, breaks on R210 and others


# 1.167 01-Jul-2010 jordan

Simplified aml_parse_resource call, cleanup code
ok marco


# 1.166 01-Jul-2010 jordan

Fix subordinate bus # for multi-root PCI buses
ok marco, kettenis


# 1.165 30-Jun-2010 thib

Disable/partially backout the bufq quiesce changes as this
is causing havoc with vnds and release must be buildable.


# 1.164 29-Jun-2010 deraadt

Fix ramdisks (new code was placed inside a SMALL_KERNEL chunk)


# 1.163 29-Jun-2010 jordan

Add support for mapping ACPI to PCI devices
ok kettenis, deraadt


# 1.162 29-Jun-2010 kettenis

Call bufq_quiesce() on suspend to stop all I/O and bufq_restart on resume.
Makes suspend/resume work much better while doing disk I/O.

ok mlarkin@


# 1.161 29-Jun-2010 tedu

60 is a more reasonable guess for number of minutes in an hour than 100.
spotted by TeXitoi. ok deraadt


# 1.160 27-Jun-2010 jordan

Add _acpi_matchhids routine, precursor to pci device match


# 1.159 27-Jun-2010 mlarkin

Clear ACPI fixed event status on resume (power buttons/etc) so that some
machines don't immediately resume after suspending to S3.

Tested by kettenis@ and I on 5 or 6 machines, deraadt@ "yeah, looks good"


# 1.158 27-Jun-2010 jordan

Cleaned up acpi_maptable, adds to table list directly


# 1.157 07-Apr-2010 deraadt

If we have done a wsdisplay_suspend(), and ACPI decides it does not want to
suspend, we must roll back by calling wsdisplay_resume() or the virtual
console switching will be locked forever
ok oga


# 1.156 07-Apr-2010 kettenis

Make suspend/resume work on MP machines (running an MP kernel). Joint work
from mlarkin@ and me, with some amd64 fixes thrown in by deraadt@

ok marco@, deraadt@, pirofti@, mlarkin@


# 1.155 31-Mar-2010 kettenis

Bring /dev/apm support in line with apm(4). Make the suspend button send
a suspend request event to apmd(8) instead of suspending immediately. Also
keep track of whether /dev/apm and /dev/apmctl are currently open, such that
we can still suspend immediately if apmd(8) isn't running.

ok deraadt@, marco@, pirofti@, jsing@, oga@


# 1.154 30-Mar-2010 oga

Prevent the apmd/x races for good.

When we hit suspend time, go through all wsdisplays on the system. if
they are in mode MAPPED, but not MODE_DUMBFB then if possible do a full
vt switch to a !mapped vt, and prevent switching back until resume time.
This has to be called from MD code because this involves userland
running so that X can run the vt switch signal handler. This way, any
case where we are using the "poke registers from userland" model, we
will not be on the hardware when we go down, so the kernel can actually
handle thing properly.

Tested on several acpi laptops (by kettenis@ and ian@), x40 (me and
beck@ at LEAST) and zaurus (me). Maybe others, but if so I forgot who at
this time..

Idea from deraadt somewhere over the Faroe Islands (I thought of a
similar thing myself a while ago). Much prompting from him. Ok and
comments miod@


# 1.153 25-Mar-2010 oga

Fix & vs. && in the kqfilter code.

Based on a comparison with the apm code.

ok deraadt@, kettenis@


Revision tags: OPENBSD_4_7_BASE
# 1.152 26-Nov-2009 mlarkin

Visual (and on some machines, audio) feedback while the machine is
resuming via calls to aml _SST method. This also makes the "moon" LED on
thinkpads to blink during resume and cease when resume is completed.

ok pirofti@


# 1.151 26-Nov-2009 deraadt

Toggle into "cold" when we we disable interrupts, because tsleeping
in the AML interpreter and context switching into userland processes
tends to sometimes pop the ACPI trampoline page out of our address
space. Makes lots more machines work.
ok kettenis mlarkin


# 1.150 24-Nov-2009 jsg

in the resume path evaluate _WAK before dealing with device drivers
like the spec says, not after.

ok mlarkin@


# 1.149 24-Nov-2009 deraadt

abort suspends on SMP systems until that gets written; ok mlarkin


# 1.148 23-Nov-2009 mlarkin

pci should return the result of it's children's suspend/resume calls back
to its parent.

handle suspend failure case and unwind if devices have an activate function
that returns failure.

ok deraadt@, kettenis@


# 1.147 23-Nov-2009 mlarkin

acpi* devices need to call their children on suspend/resume.
ok deraadt@


# 1.146 23-Nov-2009 pirofti

Remove ACPI_SLEEP_ENABLED checks.

This enables by default the suspend/resume paths in the kernel.

Okay deraadt@.


# 1.145 23-Nov-2009 deraadt

knf and spacing that are super annoying


# 1.144 23-Nov-2009 mlarkin

Hook up Theo's new children suspend/resume function, as well as more ACPI
suspend/resume glue.

ok deraadt@


# 1.143 26-Oct-2009 deraadt

Do not do strncmp comparisons on dv_xname because longer device names which
look similar could arrive in the future. Instead, compare directly against
dv_cfdata->cf_driver->cd_name
Issue originally spotted by miod


# 1.142 26-Oct-2009 jordan

Verify checksum+address when loading ACPI tables.
Some systems had invalid entries in RSDT/XSDT.
ok marco@


# 1.141 23-Jul-2009 cnst

New aibs(4) driver for ASUSTeK AI Booster (ACPI ATK0110) hardware monitoring,
with sensor state support through limits provided by the ACPI.

Tested on several ASUS motherboards kindly networked by Sam Fourman Jr.

ok deraadt marco jordan


Revision tags: OPENBSD_4_6_BASE
# 1.140 03-Jun-2009 pirofti

Import acpi power resource driver. This is needed for suspend/resume logic.

Okay jordan@ and marco@.


# 1.139 03-Jun-2009 pirofti

Add acpivideo support. This adds brightness support for all laptops
except thinkpads, they will use the acpithinkpad driver. The driver is
also hooked into wsconsole. So brightness can be adjusted via:

$ wsconsctl display.brightness=<percentage>

This is very helpfull on some laptops that have a nasty bios and get two
steps instead of one when pressing the brightness button.

Tested on various dell, fujitsu, acer, samsung and other laptops.

Okay marco@, miod@. Suggestions from kettenis@.
Lots of reviews and help from miod@, thanks!


# 1.138 03-Jun-2009 jordan

Added ejectable bay for IDE cdroms
ok marco@


# 1.137 30-Apr-2009 marco

Oga changed apm ioctls and forgot to grep. Temporary work around by hitting
the sleep button when we get any request. This interface will chnage at
some point.
ok oga


# 1.136 19-Apr-2009 krw

Call acpi_sleep_walk() when powering down just like when entering
any other sleep state. This tells acpi wake devices to not wake up
the box until manual powering up resets their status. Makes my Dell
GX520 stay powered off in response to 'halt -p'.

In snaps for a while.

ok marco@ (several times) deraadt@


# 1.135 17-Apr-2009 pirofti

Add device ranking support for future sleep/wake developments.

okay marco@


# 1.134 11-Mar-2009 jordan

Cleanup.. use aml_evalinteger instead of aml_evalname
ok marco@


# 1.133 10-Mar-2009 jordan

Fix cut/paste error.. call _BFS not _PTS
ok marco@


Revision tags: OPENBSD_4_5_BASE
# 1.132 21-Feb-2009 marco

overzealous cut/paste; found by Vladimir Kirillov <proger@uaoug.org.ua>


# 1.131 20-Feb-2009 marco

Oops didn't mean for Debugger() to remain there.


# 1.130 19-Feb-2009 marco

suspend/resume bits so that we can develop this in tree. This is disabled.
code from mlarkin and me
help from art,toby,jordan and several others
ok jordan, go for it deraadt


# 1.129 10-Feb-2009 jordan

Fix null pointer for shutdown if acpi not enabled
ok marco@


# 1.128 04-Feb-2009 kettenis

Some systems that claim to be ACPI 2.0 apparently don't provide an XSDT
address. Make the code fall back on the RSDT on those systems.

From Joerg Niendorf <p3y80@internode.on.net>. Fixes PR 5986.


# 1.127 17-Jan-2009 krw

A couple of stray malloc/memset -> malloc(M_ZERO), a bit of knf and
lose a couple of unnecessary casts. No functional change.

Feedback from kettenis@

ok kettenis@ marco@


# 1.126 06-Nov-2008 marco

Collapse all sanity checks of hids into one function.

Originaly from jordan


Revision tags: OPENBSD_4_4_BASE
# 1.125 02-Jul-2008 fgsch

add acpivideo. for now almost an empty skel so others can work on
suspend/resume. eventually it will also manage output switching and
brightness where supported. prodded by marco@


# 1.124 11-Jun-2008 marco

Fix $OpenBSD$ for once and for all


# 1.123 08-Jun-2008 deraadt

aaa_dev was being set from an uninitialized variable; ok toby


# 1.122 07-Jun-2008 marco

Kill ACPIVERBOSE. It was never used and is worthless to boot.

comments jmc
ok toby


# 1.121 06-Jun-2008 marco

Shrink acpi some more for floppies and while here re-enable acpi on the
floppies. Fixes crazy sony vaio kettenis has.

tested by deraadt
ok deraadt jordan kettenis


# 1.120 01-Jun-2008 marco

Fix \n nit in SMALL_KERNEL.


# 1.119 01-Jun-2008 marco

Re-enable new parser. Lots of testing by various folks. Fits all media
now too.

ok kettenis deraadt


# 1.118 16-May-2008 dlg

back out jordans new parser.

it didnt have enough oks from the right people, it did break machines
that were previously working, and it bloated the RAMDISKS and install
media beyond the constraints of the media.

requested by deraadt@


# 1.117 14-May-2008 jordan

Adding new ACPI Parser code
Old guts of code still exists, needs to be torn out next
ok marco@
ok brad@


# 1.116 27-Apr-2008 jcs

add acpithinkpad(4), an acpi support driver for ibm/lenovo
thinkpads.

provides functionality that the bios did on older, non-acpi
thinkpads like respond to screen brightness keys and toggle the
bluetooth device on and off.

ok deraadt@ and marco@


# 1.115 24-Apr-2008 jsing

Introduce acpiasus(4), a driver for the ACPI based hotkeys found in many
ASUS laptops (including the ASUS EeePC) - largely based on NetBSD's
asus(4) driver. On the ASUS EeePC this allows us to enable/disable
wireless, change screen brightness and use the volume keys.

ok jsg@, weingart@


# 1.114 12-Apr-2008 kettenis

Get rid of acpi_s5 global variables; simply send SIGUSR2 instead of SUGUSR1
to tell init(8) to power down the machine.

ok krw@


Revision tags: OPENBSD_4_3_BASE
# 1.113 21-Jan-2008 jsg

Check against RESET_REG_SUP in FADT before doing the acpi reset dance
after all, not doing so appears to break more machines than it fixes.

ok marco@


# 1.112 05-Jan-2008 kettenis

Never attach more acpicpu(4) devices than cpu(4) devices. Always attach
the first acpicpu(4) device on non-MULTIPROCESSOR kernels, regardless of its
ACPI processor ID.

ok mikeb@


# 1.111 05-Dec-2007 deraadt

Hang acpi(4) below bios(4) instead of mainbus(4). This lets us move acpi(4)
before pcibios(4), and then.... if acpi(4) attaches, skip pcibios(4) since
it messes the machines up. Fixes claudio's HP dl320 G5, and almost assuredly
others.
ok kettenis


# 1.110 05-Dec-2007 deraadt

repair acpiopen/acpiclose and friends to handle the /dev/apmctl interface,
so that apmd(8) can actually work correctly against acpi(4), and thus,
even do the fancy -C/-A stuff; also tested by jmc


# 1.109 02-Dec-2007 fgsch

fix the trailing space for real now.


# 1.108 02-Dec-2007 fgsch

avoid trailing spaces on output; marco@ ok.


# 1.107 27-Nov-2007 kettenis

Fix off-by-one that made us attach acpicpu0 and acpicpu1 on GENERIC kernels.

ok marco@


# 1.106 25-Nov-2007 jsg

When available and ACPI is enabled use the ACPI reset method
to reboot. This is required for some quirky sis machines
and likely others to reboot.

Tested by many. 'commit early commit often' deraadt@


# 1.105 19-Nov-2007 deraadt

sigh, amd64 still uses that stupid acpi_enabled variable...


# 1.104 19-Nov-2007 deraadt

softc's come initialized to 0, you know...


# 1.103 19-Nov-2007 deraadt

store revision; from jsg


# 1.102 19-Nov-2007 kettenis

Initialize sc_facs earlier.

ok deraadt@, beck@, canacar@


# 1.101 16-Nov-2007 deraadt

fix the bus_space #define nightmare, so that amd64 and i386 are much more
uniform. as a result shared code like acpi needs less #ifdef's
ok marco kettenis


# 1.100 15-Nov-2007 deraadt

remove the #ifdef ACPI_ENABLE. the EC codepath is either safe enough (and
will be improved), or it is unsafe because AML assumes it must be there
ok almost everyone


# 1.99 15-Nov-2007 mikeb

Make acpicpu(4) attach to the existing CPUs only.

Problem reported by form@. Fix idea by kettenis@.
Lots of help from deraadt@. Tested by deraadt@, form@ and me.

ok deraadt marco


# 1.98 14-Nov-2007 deraadt

spacing


# 1.97 12-Nov-2007 deraadt

correct : vs , in attach-time messages


# 1.96 06-Nov-2007 deraadt

all ACPI_ENABLE kernels need acpi_interrupt()


# 1.95 06-Nov-2007 deraadt

move acpi_read_pmreg() and acpi_write_pmreg() out of SMALL_KERNEL so
that acpi ramdisks can compile (obvious solution)


# 1.94 05-Nov-2007 canacar

Establish the acpi interrupt earlier. Fixes problems with some
GENERIC.MP kernels not getting acpi interrupts. This is a
workaround until the real issue with late establishment is fixed.
ok weingart@, beck@, gwk@


# 1.93 03-Nov-2007 jordan

Added support for displaying wakeup devices
ok beck@,weingart@,gwk@


# 1.92 11-Oct-2007 kettenis

When walking the tree yo find _PRT methods, check whether the device is
actually enabled and functioning. Similar to what we do for _INI.

ok weingart@, beck@


# 1.91 08-Oct-2007 krw

More simple memset(,0,) -> M_ZERO changes. In this batch move to
size(*p) as the first malloc() parameter where p is declared locally
and thus easy to check. Add M_ZERO to gpe_table allocation in acpi.c
even though there is no obvious bzero or memset nearby.


# 1.90 13-Sep-2007 mk

Remove unnecessary memset() (see aml_evalnode(9)) and a bit of KNF.

ok toby


# 1.89 13-Sep-2007 weingart

Implement ACPI 6.5.1 spec tree walk for _STA and _INI.
Fixes mk's laptop. No regressions so far (thank you to the
testers).

ok gwk@, mk@, marco@


Revision tags: OPENBSD_4_2_BASE
# 1.88 17-Apr-2007 mk

Now that acpidock no longer hangs machines during boot (at least
according to the reports I've been getting) it should be safe to enable
it again, so remove the #if 0 stuff around it.

ok marco


# 1.87 11-Apr-2007 jordan

Added changes for C-State
ok marco@


# 1.86 26-Mar-2007 marco

Calculate minutes left more accurately.

From giovanni <qgiovanni@gmail.com>


Revision tags: OPENBSD_4_1_BASE
# 1.85 03-Mar-2007 mk

Stop looking for acpidock until we fix that stuff that breaks all pre-60
series ThinkPads with acpidock(4) enabled.


# 1.84 22-Feb-2007 marco

We were getting no interrupts in .MP. There are 2 fixes; one in
acpi_machdep.c and move the interrupt establishment down after ioapic
has been initialized.

ok jordan


# 1.83 21-Feb-2007 kettenis

Make an acpi-enabled GENERIC.MP work on systems that only support PIC mode.

ok marco@


# 1.82 19-Feb-2007 jordan

Added acpi_map_address function for mapping GAS/IO registers
ok marco@


# 1.81 18-Feb-2007 jordan

Added dynamic perfect hash for opcodes
ok marco@


# 1.80 18-Feb-2007 marco

Shave off another K pre compression.
Shuffle SMALL_KERNEL functions around so that we only have a single #ifndef


# 1.79 17-Feb-2007 marco

Add SMALL_KERNEL throught acpi to make it fit on boot media.
ok deraadt


# 1.78 13-Feb-2007 marco

Add smarter _INI code. This helps with some dissapearing devices.

ok kettenis jordan


# 1.77 31-Jan-2007 gwk

Introduce acpi_hasprocfvs thats "ACPI Has Processor Frequency and Voltage
Scalling" a global flag set by acicpu attach to indicate that ACPI is
handling fvs through the _PSS objects. This will fix disappearing
EST/powernow when booting kernels with acpi enabled, as acpicpu isnt
compiled into GENERIC yet. Also in cases where acpicpu is built in and
legacy EST/powernow work but acpi is missing the _PSS object, the legacy
routines will be used.

tested by dim@, and Nick Nauwelaerts <nick AT nauwelaerts.net>
ok marco@, dim@


# 1.76 22-Jan-2007 mk

Actually look for docking stations by looking for _DCK nodes.

ok marco


# 1.75 02-Jan-2007 marco

On some systems EC must be attached as the first device. Move EC from
generic _HID devices to its own routine to force first attach. Bug found
and debugged by joris@

Code mostly written by joris@


# 1.74 26-Dec-2006 marco

Add polling to devices that require it such as acpibat and acpitz.
Use the same tsleep/wakeup mechanism as the interrupt code to keep it non-
concurrent.


# 1.73 21-Dec-2006 deraadt

even more knf love


# 1.72 21-Dec-2006 marco

Remove some leftover acpiec gpe/interrupt code.


# 1.71 20-Dec-2006 deraadt

oops, name already used


# 1.70 20-Dec-2006 deraadt

rename __acpi_enable_gpe(); ok marco


# 1.69 20-Dec-2006 deraadt

acpi_unmap_pmregs() is not used, ok marco


# 1.68 19-Dec-2006 deraadt

simplify some low-level methods substantially (shrinking code); ok marco jordan


# 1.67 06-Dec-2006 mk

malloc(..., M_NOWAIT) with no NULL check is bad. If allocation fails,
complain, clean up, and bail out of attach.

input and ok gwk


# 1.66 28-Nov-2006 jordan

Fixed clearing arg for acpi_init_pic
ok kettenis@


# 1.65 27-Nov-2006 kettenis

Don't spam dmesg.


# 1.64 27-Nov-2006 jordan

Fixes for acpiprt routing
Post-parse fixup for namerefs
ok kettenis@


# 1.63 25-Nov-2006 marco

Don't handle acpiev events inside the gpe handler since this causes deadlocks.

ok jordan@


# 1.62 15-Nov-2006 kettenis

Hook up acpi apic interrupt routing support to generic acpi code.

ok jordan@


# 1.61 02-Nov-2006 marco

Kill acpiec attach function and add its functionality to the generic _HID
device attach

prodded and ok jordan


# 1.60 02-Nov-2006 marco

Fix comments to reflect reality.


# 1.59 19-Oct-2006 marco

Replace ACPI ioctls with APM ioctls. This makes ACPI talk the same protocol
as APM therefore userspace can now retrieve power information in the same
fashion as all other apm arches (zaurus, macppc, i386 etc).

To use force the use of acpi device like so: apmd -f /dev/acpi

ok jordan@


# 1.58 19-Oct-2006 jordan

Fixed GPE debug levels
Fixed ref counter on temporary bufferfields
DSDT parsing fully working on IBM Thinkpad
ok marco@


# 1.57 15-Oct-2006 jordan

Removed old GPE handler code remnants
ok marco@


# 1.56 12-Oct-2006 jordan

Massive ACPI changes
Fixed ACPI leaks
new ACPI API
ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.55 30-Jun-2006 gwk

KNF, No functional change,
"go at it" marco@


# 1.54 30-Jun-2006 jordan

Support for calling Embedded Controller via new GPE interface
ok marco@


# 1.53 30-Jun-2006 jordan

Added new code for GPE handling
Also works with ACPIEC; need to fixup acpiec.c to remove original interrupt handler
ok marco@


# 1.52 30-Jun-2006 jordan

Added offset argument for acpi read/write pmreg routines (new GPE code)
ok marco@


# 1.51 31-May-2006 todd

do not call acpi_powerdown() unless acpi has successfully initialized
ok gwk@ canacar@


# 1.50 29-May-2006 canacar

Add embedded controller support.
ok marco@ deraadt@ testing & ok gwk@


# 1.49 19-May-2006 canacar

Minimal thermal zone driver. Monitors thermal zone temperature,
shuts down the system if the 'critical temperature' is reached.
ok marco@


# 1.48 11-Apr-2006 marco

KNF.


# 1.47 11-Apr-2006 gwk

Add acpi_resume(struct acpi_softc *sc), which calls some AML methods,
detected at attach time, currently unused.

ok marco@


# 1.46 11-Apr-2006 gwk

Discover some ACPI sleep related methods, and call them at the
appropriate time in acpi_enter_sleep_state().

ok marco@


Revision tags: OPENBSD_3_9_BASE
# 1.45 26-Feb-2006 marco

Add acpicpu as a device.

ok jordan@


# 1.44 23-Feb-2006 marco

remove dead function.

ok jordan@


# 1.43 22-Feb-2006 marco

Print out all tables it found during autoconf. We'll need this data in
the future.


# 1.42 22-Feb-2006 marco

Make dmesg less verbose.

ok jordan.


# 1.41 22-Feb-2006 jordan

Added parsing of SSDT tables
ok marco@


# 1.40 22-Feb-2006 jordan

Added new aml_register to notify by device id
Moved powerdown code to button device
ok marco@


# 1.39 21-Feb-2006 marco

Readjust debug levels that crept in too low over the last few days.
KNF


# 1.38 21-Feb-2006 marco

Rig the power button so that the machine shuts down correctly.


# 1.37 20-Feb-2006 marco

Add acpibtn device. Remains disabled for now.


# 1.36 19-Feb-2006 jordan

Added code for aml_notify GPE notification callback
ok marco@


# 1.35 19-Feb-2006 grange

Fix acpi_enter_sleep_state() by replacing current ugly hack
with mostly correct sleeping state transition code.

ok brad@ marco@


# 1.34 19-Feb-2006 marco

jordan:
Enable interrupts.
Added peripheral code to make this work.

marco:
Replace softintr and other types of interrupts with a handler with context.
Fixed interrupt magic for GPEs.
Some KNF

This is work done by jordan and I but has to be a single commit.


# 1.33 17-Feb-2006 marco

Remove earlier introduced delays. We really only need them during autconf.

ok jordan@


# 1.32 17-Feb-2006 marco

Add a generic delay function as per the ACPI spec.

from jordan@


# 1.31 17-Feb-2006 marco

Use tsleep instead of delay when !cold.


# 1.30 17-Feb-2006 marco

Argh, we need delays between reads/writes. The backend reads (i2c most likely)
are so slow that values come back as 0 sometimes. We have to do this
despite the spec saying we shouldnt have to. Guess having a faster
parser than others can hurt sometimes.

Idea from jordan.


# 1.29 17-Feb-2006 jordan

Fixed code for handling _INI
Rearranged debug statements
ok marco@


# 1.28 16-Feb-2006 jordan

Added stub for calling _INI
Fixed aml_dowhile for NULL return
ok marco@


# 1.27 16-Feb-2006 jordan

Fixed parser/evaluator to retrieve battery info+state
Cleanup of parser
Dell laptops return battery status
ok marco@


# 1.26 06-Feb-2006 brad

fix a typo so this will compile on amd64.


# 1.25 04-Feb-2006 marco

When ACPI_ENABLED wasn't set but the devices were it had an unknown variable.

Pointed out by ian@.


# 1.24 03-Feb-2006 jordan

Rewrote aml parser yet again...
Fixed bus_space_map for i386/amd64
Added _BST and _BIF to acpibat to get status/info
ok marco@


# 1.23 20-Jan-2006 jordan

More evaluator changes
Removed old evaluator code
Fixed _HID value
ok marco@


# 1.22 19-Jan-2006 jordan

Added support for read/write of PCI Config space
Help jason@
ok marco@


# 1.21 18-Jan-2006 jordan

Added new evaluation routines
Added helper methods for buffer/field evaluation
Fixed broken indentation on dsdt.c

ok marco@


# 1.20 17-Jan-2006 jordan

Major overhaul of the aml parser. Evaluater temporarily disabled.

ok marco@


# 1.19 06-Jan-2006 grange

Check for ACPI control availability only if ACPI_ENABLE defined.


# 1.18 06-Jan-2006 grange

Informative error messages on attach, prettier dmesg.


# 1.17 06-Jan-2006 grange

printf -> dnprintf conversion for debug messages.


# 1.16 05-Jan-2006 grange

Find SLP_TYPx values and store them for future use in
acpi_enter_sleep_state().


# 1.15 05-Jan-2006 grange

Random KNF and cleanup


# 1.14 05-Jan-2006 grange

Provide symbolic names for the acpi sleeping states and use it.


# 1.13 28-Dec-2005 marco

Moved DEVNAME to a acpidev.h

ok jordan@


# 1.12 18-Dec-2005 sturm

add back comment about wrong code

ok marco


# 1.11 16-Dec-2005 marco

Remove test for kbd_reset. Prompted deraadt@, ok jordan@


# 1.10 16-Dec-2005 jordan

Added hooks for powerdown/power button press


# 1.9 16-Dec-2005 jordan

Cleaned up common power management register i/o
Added enter sleep state ioctl
Hooked power button interrupt
ok marco@


# 1.8 16-Dec-2005 marco

Remove autoconf chatter


# 1.7 16-Dec-2005 jordan

Moved DSDT AML parsing into parent ACPI driver
ok marco@


# 1.6 14-Dec-2005 marco

Add AC device glue.


# 1.5 14-Dec-2005 marco

Adjust debug printing levels to something more sane.


# 1.4 13-Dec-2005 marco

Add glue to attach devices to apci.


# 1.3 07-Dec-2005 marco

KNF


Revision tags: OPENBSD_3_8_BASE
# 1.2 10-Jul-2005 grange

Spacing cleanup.


# 1.1 02-Jun-2005 tholo

Start on a basic ACPI framework -- does not do much more than read out the
ACPI tables into kernel memory and attach ACPI and HPET timers currently.

In order to test this code, enabling the devices in GENERIC as well as
the ACPI_ENABLE option is needed. This code does not do any thermal
control yet, so this should be done with care depending on the platform.

In the tree so more people can contribute to making this more fully
featured.

Ok niklas@ grange@ tedu@


# 1.370 10-Jun-2019 kettenis

Print proper ACPI version number.

ok mlarkin@


# 1.369 08-Jun-2019 kettenis

When establishing the mapping between ACPI device nodes and PCI devices,
skip PCI host bridges and devices that aren't there.

ok patrick@


# 1.368 07-Jun-2019 kettenis

Introduce acpi_getsta() and use it to eliminate some duplicated code.

ok patrick@


# 1.367 12-May-2019 kettenis

Make sure the extended PM register descriptions actually exist before
using them. Fixes machines with ancient ACPI 1.0 (such as recent
hypervisors).

ok deraadt@


# 1.366 12-May-2019 kettenis

Add support for mapping ACPI PM registers using the "extended" gasio
representation from the FADT. Mostly fixes the Lanner NCA-1510.

ok lteo@, mlarkin@, deraadt@


# 1.365 11-May-2019 lteo

Fix incorrect error message.

ok kettenis@


# 1.364 08-May-2019 tedu

free size. the allocation is the header plus the length.


Revision tags: OPENBSD_6_5_BASE
# 1.363 04-Apr-2019 kettenis

Avoid potential null-pointer dereference. Found by Kent R. Spillner.

ok deraadt@, jsg@


# 1.362 20-Jan-2019 jsg

fix SMALL_KERNEL build after changes in rev 1.361
reported by naddy@ ok deraadt@


# 1.361 19-Jan-2019 tedu

Add a pwraction sysctl that controls what the power button does on acpi.
By default, nothing changes -- shutdown is initiated. But allows turning
power button into a sleep button if desired.
(grudging) ok from a few parties


# 1.360 26-Oct-2018 kettenis

Add acpipci(4) on amd64. For now this only calls the PCI-specific _OSC
method to let the ACPI implementation know what features we support.


Revision tags: OPENBSD_6_4_BASE
# 1.359 30-Aug-2018 kettenis

Move softbutton check before the check that skips attaching certain acpi
devices. Fixes regression caused by matching on _CID in addition to
matching on _HID.

ok matthieu@, mlarkin@


# 1.358 25-Aug-2018 kettenis

Match ACPI devices based on _CID if no match for _HID is found.

ok mpi@


# 1.357 19-Aug-2018 kettenis

Add support for multiple PCI segments. Only really implemented for arm64
for now as amd64/i386 firmware still caters for legacy OSes that only
support a single PCI segment.

ok patrick@


# 1.356 03-Aug-2018 kettenis

Let ahci(4) match on _CLS instead of _HID when attaching at acpi(4). Avoids
having to add many more _HID entries to the match table.

ok deraadt@, mlarkin@


# 1.355 10-Jul-2018 kettenis

Make legacy interrupts work in acpipci(4).

ok patrick@


# 1.354 05-Jul-2018 kettenis

Add acpipci(4), a driver that supports generic ECAM-compatible PCI host
bridges based on information provided by ACPI.

ok mlarkin@


# 1.353 04-Jul-2018 kettenis

Properly pass around the PCI "chipset tag" in acpi(4) and refactor
acpimcfg(4) to call an MD initialization functions that sets up a tag for
PCI ECAM.

ok guenther@, mlarkin@, krw@


# 1.352 01-Jul-2018 kettenis

Move acpi(4) attach glue into acpi_machdep.c.

ok guenther@, deraadt@


# 1.351 01-Jul-2018 kettenis

Add support for _DSD Device Properties.

ok mlarkin@


# 1.350 30-Jun-2018 kettenis

Add intr_enable() function, intended for MI use to amd64 and i386 and use
this in the acpi(4) suspend/resume code paths.

ok deraadt@


# 1.349 29-Jun-2018 kettenis

Use <stdint.h> types.

ok mpi@, jung@, krw@, deraadt@


# 1.348 29-Jun-2018 mlarkin

ACPI: Allow (with warning) GPE handler reassignment, instead of returning
a failure code.

ok kettenis


# 1.347 26-Jun-2018 mlarkin

knf


# 1.346 26-Jun-2018 kettenis

Let acpi(4) pass down the bus dma tag.

ok deraadt@


# 1.345 25-Jun-2018 kettenis

Reorganize acpi(4) code a bit in preparation for upcoming arm64 support.

ok deraadt@


# 1.344 20-May-2018 kettenis

Wake up the acpi thread after scheduling a gpio event.

ok mpi@


# 1.343 17-May-2018 kettenis

Sprinkle some #ifndef SMALL_KERNEL such that RAMDISK kernels cuild again.

pointed out by stsp@


# 1.342 17-May-2018 kettenis

Implement GenericSerialBus OpRegion support.

ok mlarkin@


# 1.341 27-Mar-2018 kettenis

Add acpicmos(4), a driver that implements SystemCMOS OperationRegion
access support. This fixes machines where the AML doesn't check whether
support for this OperationRegion type has been registered by the OS.

ok mlarkin@


Revision tags: OPENBSD_6_3_BASE
# 1.340 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.339 10-Feb-2018 deraadt

Syncronize filesystems to disk when suspending. Each mountpoint's vnodes
are pushed to disk. Dangling vnodes (unlinked files still in use) and
vnodes undergoing change by long-running syscalls are identified -- and
such filesystems are marked dirty on-disk while we are suspended (in case
power is lost, a fsck will be required). Filesystems without dangling or
busy vnodes are marked clean, resulting in faster boots following
"battery died" circumstances.
Tested by numerous developers, thanks for the feedback.


# 1.338 08-Feb-2018 deraadt

After flushing disks, try again to release all memory. We don't need to
save dirty memory to the hibernate space.


# 1.337 08-Feb-2018 deraadt

Toss all releaseable memory, because fragmentation can get in the way
of allocating the hibernate playpen.


# 1.336 08-Feb-2018 deraadt

sr_quiesce() is a new approach for ensuring that softraid drains
output to the disks.

This is part of a larger suspend/resume filesystem-safety diff, which
has been worked on for a couple of months already. Tests by job, krw,
beck, benno, and others. Sometimes even by snapshot users...


# 1.335 29-Nov-2017 kettenis

Add support for GPIO-signaled events.

ok mlarkin@


# 1.334 16-Nov-2017 jcs

Add PCI attachment for dwiic(4) needed by Intel 100 Series machines

ok kettenis


# 1.333 14-Nov-2017 kettenis

Add a few more PNP IDs to the skip lists.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.332 17-Aug-2017 stsp

Make dwiic(4) attach its ACPI parent dependencies.
This change makes the Asus E200HA keyboard work.

Original analysis and patch by Cesare Gargano
ok kettenis@


# 1.331 09-Aug-2017 dcoppa

Add "PNP0303" (8042 PS/2 Controller) to acpi_isa_hids[]

ok kettenis@


# 1.330 22-Jul-2017 jcs

acpibat: look for _BIX first before falling back on _BIF

ACPI 4.0 deprecated _BIF for battery status so some newer machines
are exporting _BIX which extends _BIF's package with a few extra
fields. When using _BIX, export the new discharge cycle count as a
sensor.

feedback from kettenis and anton
ok deraadt


# 1.329 20-Jul-2017 kettenis

Start a list of isa(4) device names such that we can avoid printing them
as "not configured".

ok mikeb@, millert@, deraadt@


# 1.328 08-Apr-2017 deraadt

Sensors are run as callbacks inside tasks. During suspend a sensor
could be running inside a driver that will be force-detached, or due
to tsleep end up disrupting the softstate/hardstate contract. At
suspend time, quisce all these callbacks by waiting for completion.
This issue has never been observed for real, but may be implicated in
suspend/resume failures.
ok kettenis guenther mlarkin


# 1.327 08-Apr-2017 deraadt

Two prototypes for acpi_maptable; merge to one place


Revision tags: OPENBSD_6_1_BASE
# 1.326 27-Mar-2017 deraadt

Now that hibernate_alloc() only has clean success/failure, don't
need to call hibernate_free() to clean up a partial mess.
ok mlarkin kettenis


# 1.325 13-Mar-2017 deraadt

allow ramdisks to build; ok jcs


# 1.324 12-Mar-2017 jcs

try to attach acpisbs first and if successful, don't attach acpibat


# 1.323 02-Mar-2017 natano

Add a new sysctl machdep.lidaction. The sysctl works as follows:

machdep.lidaction=0 # do nothing
machdep.lidaction=1 # suspend
machdep.lidaction=2 # hibernate

lidsuspend is just an alias for lidaction, so if you change one, the
other one will have the same value. The plan is to remove
machdep.lidsuspend eventually when people have upgraded their
/ets/sysctl.conf.

discussed with deraadt, who came up with the new MIB name
no objections mlarkin
ok stsp halex jcs


# 1.322 28-Feb-2017 natano

Untangle abstract sleep modes from ACPI sleep states. This paves the way
for further work in the area.

idea and ok deraadt
ok mlarkin


# 1.321 22-Feb-2017 jcs

add acpisbs, an acpi smart battery subsystem driver reading data
over smbus

currently disabled because it conflicts with acpibat

ok deraadt, kettenis


# 1.320 14-Jan-2017 kettenis

Revert aml_rdpciaddr changes; breaks several machines that were working
before.


# 1.319 11-Jan-2017 jsg

Increase the size of the buffers used for _HID and _CID strings to be
able to fit "Hyper_V_Gen_Counter_V1".

ok mlarkin@ deraadt@


# 1.318 08-Jan-2017 kettenis

Make aml_rdpciaddr() work for for devices behind bridges. The code now
uses the mapping between AML nodes and PCI devices that we establish.
Because _INIT methods may end up calling aml_rdpciaddr(), make sure we
create that mapping early. Also handle devices that aren't actually present.
These devices are now included in the mapping and reads will return an
all-ones patterm whereas writes are a no-op..

ok mlarkin@


# 1.317 25-Oct-2016 pirofti

Add the resource index to aml_parse_resource() callback function.

This is needed by an upcoming acpiec commit that handles machines breaking
the current ACPI specifications.

Change suggested by and ok kettenis@, guenther@


# 1.316 18-Sep-2016 guenther

If the APIC table (MADT) isn't present then ci_acpi_proc_id won't be set
when acpicpu tries to attach, leading to a NULL dereference. The ACPI
processor object on the Dells where this was reported is bogus and useless
anyway so apply the same test when deciding whether to attach to processor
objects as acpicpu would when matching them to cpus.

Problem reported by Eivind Eide (xenofil (at) gmail.com) and
Johan Huldtgren (johan+openbsd-bugs (at) huldtgren.com).
Testing by Eivind Eide.
ok kettenis@ mlarkin@


# 1.315 03-Sep-2016 naddy

Write the system time back to the RTC every 30 minutes.
This fixes the problem that long-running machines which were not
shut down properly would reboot with a badly offset system time.

hints and ok kettenis@


# 1.314 31-Aug-2016 mlarkin

Don't put a device in the wake device list if _STA says it's not there.

Fixes at least one machine that woke up from sleep / poweroff immediately.

ok kettenis@, tested by kettenis and martijn@


# 1.313 28-Jul-2016 kettenis

Store the acpi processor ID/UID in struct cpu_info, and use it to attach
acpicpu(4) drivers to the right cpu(4).

ok mlarkin@, guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.312 10-Jun-2016 kettenis

The power management timer has been made optional in ACPI 5.0A, so don't
attempt to attach acpitimer(4) if the timer isn't present.

Pointed out by jcs@

ok jcs@


# 1.311 08-May-2016 kettenis

Make sure devices listed by a device's _DEP method are attached before
we attach the device itself. The _DEP methode, introduced in ACPI 5.0,
is a hint that the device in question depends on OpRegion support from
the devices listed by _DEP, so we have to order them the proper way.
To prevent us from attaching those devices again when we encounter them
later walking down the device tree, keep track of our attempts to
attach a device driver to them using aflag in the aml_node structure.

ok guenther@


# 1.310 25-Apr-2016 pirofti

We attach acpihpet(4) and acpiec(4) without using the HID, so do not
report it in the dmesg.

OK deraadt@, mlarkin@.


# 1.309 02-Apr-2016 kettenis

Reduce dmesg spam by knocking out some acpi hardware IDs that don't need a
driver.

ok mpi@


# 1.308 29-Mar-2016 kettenis

Check if a device is present (using _STA) in acpi_foundhid. This prevents
us from reporting devices as "not configured" that aren't actually present,
and allows us to remove duplicated code from several drivers.

ok deraadt@, ok jsg@


# 1.307 29-Mar-2016 kettenis

Implement a cleaner way to prevent acpivout(4) from attaching if we have
a platform-specific method of controlling the backlight.

ok millert@, mlarkin@, jung@


# 1.306 28-Mar-2016 kettenis

Adding each and every new acpi device driver to acpi_foundhid() is
gettinga bit of the burden. And it really isn't how our autoconf
framework is supposed to work for busses that can be enumerated. All
the drivers already check for a matching _HID in their attach
function. So we can just drop the checks in acpi_foundhid() and let
the drivers sort themselves out.

Print a "not configured" line for devices for which we don't attach adriver.
This may be a bit much as there are devices for which we will not have a
driver. This will be removed if it causes too much dmesg spam.

ok deraadt@, mlarkin@


Revision tags: OPENBSD_5_9_BASE
# 1.305 17-Jan-2016 jsg

Take acpi_foundhid() outside of #ifndef SMALL_KERNEL so the ramdisk
kernel can find and attach dwiic(4) and sdhc(4).


# 1.304 17-Jan-2016 jsg

remove an unused global variable


# 1.303 14-Jan-2016 kettenis

Attach dwiic(4) on the Intel Bay Trail i2c controllers as well.


# 1.302 12-Jan-2016 kettenis

Improve emulation of PM registers on Hardware-redcued ACPI by emulating
SPL_TYPx, SLP_EN and WAK_STS using the new SLEEP_STATUS_REG and
SLEEP_CONTROL_REG registers. Makes the transition into S5 (powerdown) work
on the ASUS X205TA.

ok deraadt@, mlarkin@


# 1.301 12-Jan-2016 jcs

Add dwiic, a driver for the Synopsys DesignWare i2c controller found
on the Samsung ATIV Book 9 laptop. This initial version only
supports ACPI config/attachment.

Add ihidev, a HID-over-i2c driver largely based on uhidev. dwiic
handles attaching ihidev devices found in ACPI.

Add ims, a HID-over-i2c mouse/trackpad driver to get basic cursor
and button functionality from HID-compliant i2c trackpads.

ok kettenis deraadt


# 1.300 11-Jan-2016 kettenis

Make sdhc(4) attach to hardware IDs "80860F14" and "PNP0FFF".


# 1.299 09-Jan-2016 kettenis

Initial support for Hardware-Reduced ACPI. This skips the legacy mode checks
by emulating the PM_CNT register such that SCI_EN is always enabled. It also
skip attaching acpitimer(4) since the relevant registers are no longer there.

ok deraadt@, mlarkin@


# 1.298 06-Jan-2016 kettenis

Follow the spec and check the SCI_EN bit to see if ACPI has already
been enabled and don't bother enabling it again if it is. This should fix
systems that don't implement legacy mode and therefore set ACPI_ENABLE and
ACPI_DISABLE to zero.

ok mlarkin@, yasuoka@


# 1.297 23-Nov-2015 reyk

Back out the CRITICAL change, turn it back from 10% to 25%. Some
people with old/broken batteries didn't like this change, and the
underlying heuristics are wrong.


# 1.296 22-Nov-2015 reyk

The "CRITICAL" APM battery level, that is exported/faked by the ACPI
stack, is currently set to 25%. With 25% battery left, a typical
modern laptop will have at least one hour runtime left. It is an
arbitrary value, and other APM power layers in the kernel set CRITICAL
to something like 5% or 10%, so set it to 10% for now.

OK mlarkin@


# 1.295 28-Sep-2015 deraadt

In low-level suspend routines, set cold=2. In tsleep(), use this to
spit out a ddb trace to console. This should allow us to find suspend
or resume routines which break the rules. It depends on the console
output function being non-sleeping.... but that's another codepath which
should try to be safe when cold is set.
ok kettenis


# 1.294 06-Sep-2015 deraadt

clarify malloc arguments (base + length...)


# 1.293 28-Aug-2015 deraadt

fairly simple sizes for free(); ok tedu


# 1.292 20-Aug-2015 kettenis

Respect the access size when reading or writing to pci config space.
This version also makes sure writes are properly aligned, preventing
the panics seen with the previous incarnation of this change.

Fixes battery status passthrough in vmware, brightness keys on my x220
and who knows what else.

ok mlarkin@, deraadt@


Revision tags: OPENBSD_5_8_BASE
# 1.291 04-Aug-2015 deraadt

If we are setting ACPI_PM1_BM_RLD do it only once, and remember to
repeat this action at resume time.
ok mlarkin guenther


# 1.290 29-Jul-2015 deraadt

Something subtle in previous revision is locking up suspend or booting
on some laptops. Mailing lists threads mention x220 and Asus machines.
Reverting for now.


# 1.289 21-Jul-2015 kettenis

iRespect the access size when reading or writing to pci config space.
Fixes battery status passthrough in vmware and who knows what else.

Tested by many.

ok mlarkin@, deraadt@


# 1.288 17-Jul-2015 mlarkin

fix comments


# 1.287 26-Mar-2015 jsg

Use the 0x10 extended capability to determine if the trackstick
buttons are wired to the trackpad and need to be re-routed to the
trackstick. Without this change the buttons on 2015 thinkpads
get picked up as extended buttons that show up as scroll up/down.

Remove the X1 Carbon 2015 (LEN0048) and X250 (LEN0046) from the
top button area/soft buttons quirks list. Also avoid using the
quirk list entirely if the capability bit is set.

Based on work from jcs@ with feedback from mpi@
ok mpi@ jcs@


# 1.286 20-Mar-2015 kettenis

The ACPI standard says that a PCI function number of FFFF can be used to refer
to all functions on a device. This makes no sense in the context of accessing
PCI config space. Nevertheless stsp@ has a machine with AML that tries to
do config space reads from a device where _ADR encodes function number FFFF.
This is almost certainly buggy AML, but since Linux (and presumably Windows)
boot just fine on this machine we need to work around this. Linux effectively
treats function FFFF as function 7, which isn't present on this machine.
Instead we simulate a read from a non-existent device by returning a bit
pattern of all ones. Writes to function FFFF will still panic.

ok mlarkin@


# 1.285 16-Mar-2015 deraadt

Reduce use of SMALL_KERNEL in acpi, bringing acpiec driver and more gpe
handling into RAMDISK. This is now possible because the install media
has ample room. The goal is to reduce special cases where we may be
skipping (unknown) important operations...
ok mlarkin kettenis


# 1.284 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.283 07-Feb-2015 deraadt

New framework that allows hibernate to pass in entropy from it's fresh
boot.
ok mlarkin


# 1.282 06-Feb-2015 mlarkin

Fix a hibernate crash on some machines due to unmapping a page that
may not have been mapped previously (in the failure to hibernate case).

Also ensure that the lowmem ptp is mapped in all cases (not just MP).

ok kettenis


# 1.281 17-Jan-2015 deraadt

resume_randomness() before spinning up other cpus...


# 1.280 15-Jan-2015 jsg

Define a new wscons mouse type for Synaptics clickpad devices that lack
physical buttons. This will be used if the acpi pnpid for the mouse
matches a list provided by Synaptics found in Linux.

Combined with the xenocara changes this will remove the need for an xorg
config file for the x240/t440/t540 generation of thinkpads.

ok matthieu@ shadchin@ kettenis@


# 1.279 11-Jan-2015 kettenis

Tolerate checksum errors in ACPI tables (but complain about them). Apparently
there are machines out there with broken BIOSen with such checksum errors in
thier RSDT tables. Windows runs fine on these machines, so it probably does
not check. Linux ignores checksum errors as well (and prints a warning).

ok deraadt@, krw@


# 1.278 18-Dec-2014 deraadt

suspend and resume the RNG (at what looks like the right spots)
ok reyk djm


# 1.277 09-Dec-2014 doug

Replace some malloc(n*size,...) calls with mallocarray().

ok tedu@ deraadt@


# 1.276 06-Dec-2014 mpi

If "machdep.lidsuspend" is not set, do not go back to sleep if the lid
is still closed when resuming. This allows docked laptop to be resumed
without opening them.

ok mlarkin@


# 1.275 23-Nov-2014 mlarkin

If a machine has no _LIDs, then none can be open. This diff fixes a problem
where machines without any _LIDs were going back to sleep after hibernate
due to the recent suspend-if-lid-closed-on-resume diff. Noticed on qemu,
which doesn't have any _LIDs.

discussed with deraadt


# 1.274 18-Nov-2014 krw

Nuke yet more obvious #include duplications.

ok miod@


# 1.273 08-Nov-2014 mlarkin

If resuming from sleep (zzz/ZZZ) and the lid is still closed, go back to
sleep. This prevents accidental lid flex or slight opening in a backpack
from waking the machine up and leaving it resumed (powered on).

ok deraadt@


# 1.272 26-Sep-2014 kettenis

Rework piglet and pig allocation. Currently the piglet gets allocated
deep down in the suspend path, where it is really hard to recover from
allocation failure. So allocate the piglet early on in the suspend path.
Also change the piglet and piglet allocation functions to use km_alloc(9)
instead of doing pmemrange magic. This removes a bunch of code which, in the
case of the piglet allocation, is broken since it results in a NULL pointer
dereference. Also switch the piglet allocation to not wait. If we can't
allocate 16MB of phys contig memory on a halfway modern machine we're almost
certainly under a lot of memory pressure and we're better off not trying to
hibernate anyway.

ok mlarkin@


# 1.271 25-Sep-2014 kettenis

Bring back rev 1.249. Now that mp_setperf() has been fixed, it should be
safe to restore perflevel when resuming. Should fix the issues people
see with the processor running at full speed heating up the machine while
the apm command reports that the CPUs are running at a low speed.

ok dcoppa@, deraadt@


# 1.270 23-Sep-2014 kettenis

Release the acpi lock around calls to wsdisplay_suspend() and
wsdisplay_resume(). The X server may adjust the display brightness just
before/after the VT switch when suspending/resuming. In fact, it may
already be blocked adjusting the display brightness for some reason
unrelated to the suspend request.

The VT switch request will eventually time out, but the delay is annoying
and the failed VT switch will lead to problems after resume.

Needed for a future update of the xf86-video-intel driver.

ok mlarkin@


# 1.269 19-Sep-2014 kettenis

Use config_suspend_all(9).


# 1.268 08-Sep-2014 kettenis

Clear the PME Status bit on PCI devices in response to Device Wake events.
Fixes the interrupt storm on the 2009 Mac mini with WOL enabled on its
nfe(4) interface.

ok mlarkin@


Revision tags: OPENBSD_5_6_BASE
# 1.267 20-Jul-2014 mlarkin

Support hibernating to softraid crypto volumes.

much help and ok from deraadt@


# 1.266 16-Jul-2014 mlarkin

Reenable hibernate RLE support and flush+zero all memory after unpack.


# 1.265 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.264 12-Jul-2014 kettenis

Add a function to drop all clean pages on the page daemon queues and call
it when we hibernate.

ok mlarkin@, miod@, deraadt@


# 1.263 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.262 11-Jul-2014 mlarkin

Flush the buffercache to 16MB on hibernate and restore its previous max
size (kern.bufcachepercent) on resume, for better hibernate performance.

ok beck@


# 1.261 10-Jul-2014 blambert

remove unneeded header

ok mlarkin@


# 1.260 01-Jul-2014 kettenis

Adding an additonal delay after each access to SystemMemory or SystemIO
during autoconf makes no sense.

ok deraadt@


# 1.259 23-Jun-2014 kettenis

SystemMemory is used to access memory mapped registers on some machines, so we
must use the bus_space(9) API to access it instead of memcpy(9). Also make
sure we properly align access to fields that need more than byte alignment.

ok guenther@


# 1.258 21-May-2014 mlarkin

Fix invalid / missing format specifiers in several dnprintf statements
(used only when ACPI_DEBUG is enabled)

From a diff on tech@ by Fabian Raetz, with a minor change by me, tested
on i386 and amd64 with ACPI_DEBUG enabled and disabled.


# 1.257 25-Apr-2014 mlarkin

Defer the free of the hibernate structures until later, when it is safe to
do so.

Freeing the hibernate structures too early leads to inadvertent calls into
splx when we aren't ready for them yet. Found when trapping splx calls in
the resume path.

discussed with deraadt@ at m2k14


# 1.256 13-Apr-2014 mlarkin

Fix a badly worded debug message that implied the entire machine was going
to sleep, instead of just the acpi thread going to sleep.


# 1.255 31-Mar-2014 mpi

Change the order of operations during a suspend/resume cycle and
call bufq_quiesce() after executing the DVACT_QUIESCE handlers.

This should be safe since no disk nor controller drivers have such
handler but it will allow us to detach sd(4) devices attached to a
USB bus.

Another benefit pointed out by kettenis@ is that drivers that need
to read a firmware from the disk should be able to do it at resume
time in a DVACT_WAKEUP handler.

ok kettenis@, deraadt@


# 1.254 13-Mar-2014 dlg

get rid of the assumption that the head of the alldevs list is the
"mainbus" device. this breaks when mpath is enabled because it
attaches before mainbus and therefore takes the head position.

have autoconf provide device_mainbus() which looks up mainbus_cd,
and use that instead.

discussed with deraadt who just wants mpath stuff to move forward
despite there being many ways to shine this particular turd.


Revision tags: OPENBSD_5_5_BASE
# 1.253 21-Feb-2014 deraadt

Do the lid control further on in the resume sequence, since this is
AML, and will do WAITOK operations. While cold, that is a bad idea.
We seem to be safe with respect to the spec, I do not see any event
debounce issues here.
ok kettenis, tested by many


# 1.252 20-Jan-2014 claudio

Instead of checking sc->sc_revision <= 1 check that the
fadt->hdr_revision <= 1. This is a better check since it checks
the version of the proper descriptor. This allows the alix APU
to reboot since those boards have currently a wrong sc->sc_revision.
Committing this now so that it can be tested easier.
With and OK jsg@ also fine by derradt@


# 1.251 24-Dec-2013 kettenis

If the FADT has its SMI_CMD set to zero, assume we're only ACPI-only hardware
and don't need to disable SMI ownership of the ACPI hardware registers.

ok mlarkin@


# 1.250 23-Dec-2013 kettenis

Revert previous commit. It makes resume hang on some MP systems running
GENERIC.MP.


# 1.249 16-Dec-2013 kettenis

Restore hw.setperf upon resume like we do for apm(4) on i386. Seems we need
to do this fairly late in the resume to avoid hangs.

ok deraadt@, mlarkin@


# 1.248 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


# 1.247 06-Nov-2013 mpi

Add support for Power Resources for Dx states and the necessary hook
for PCI devices. This hook should be called twice, before and after
changing the power state of a PCI device.

Before setting the device to the new state, the ACPI layer will notify
every power resources linked to the device for that state and make sure
they are turned "_ON". After changing the state of the device, it will
decrement the reference of every power resources linked to that device
for the old state and turn them "_OFF" if they are no longer referenced.

This fixes the no-USB after resume problem seen on various ThinkPad,
problem initialy diagnosed with Alexander Polakov.

ok kettenis@, deraadt@


Revision tags: OPENBSD_5_4_BASE
# 1.246 01-Jun-2013 mlarkin

Stop printing ACPI wakeup devices in dmesg after the 16th wakeup device.

Helps with VMware VMs that report hundreds of wakeup devices.

ok reyk@, deraadt@


# 1.245 31-May-2013 bluhm

Fix the build for a kernel without wd and pciide in its config.
OK deraadt@ tedu@


# 1.244 30-May-2013 deraadt

Enforce ca_activate tree-walks over the entire heirarchy for all events,
cleaning up some shutdown-hook related code on the way.
(A few drivers related to sparc are still skipped at kettenis' request)
ok kettenis mlarkin, tested by many others too


# 1.243 18-Apr-2013 deraadt

pin the acpi thread to the BSP, because AML and SMI cannot be trusted. I
was certain we were doing this already, but it turns out no...


Revision tags: OPENBSD_5_3_BASE
# 1.242 24-Dec-2012 guenther

In acpiioctl(), Return though the splx() path if suser() returns an error

ok deraadt@


# 1.241 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


# 1.240 04-Oct-2012 ehrhardt

Use information provided by ACPI to attach secondary PCI host bridges.

ok kettenis@


# 1.239 07-Sep-2012 kettenis

Add a new function called acpi_pci_min_powerstate() that returns the lowest
power state for a PCI device for the power state (currently S3 or S4) we're
transitioning into. This function is probably too simplistic; there is a lot
of (silly) complexity in the ACPI spec.

ok deraadt@


Revision tags: OPENBSD_5_2_BASE
# 1.238 13-Jul-2012 pirofti

Disable acpivideo(4) on ASUS laptops.

There are some models that panic whenever an event is received.
Some cope, some don't. In order to get this right acpiec(4) requires
major rework. Until then let machines boot.

Discussed with deraadt@.


# 1.237 11-Jul-2012 mlarkin

for now, restrict hibernate to root in the acpi ioctl kernel interface.
normal rules for controlling this interface through apmd still apply.

ok deraadt@


# 1.236 09-Jul-2012 deraadt

tiny little oops in untested hibernate block


# 1.235 09-Jul-2012 deraadt

clean up ioctl() case statements


# 1.234 09-Jul-2012 deraadt

acpi_disable_onegpe() is not used; ok pirofti


# 1.233 24-May-2012 kettenis

If _MIN > _MAX consider a WordBus resource to be invalid. Fixes a panic on
the IBM x3550-M4.

ok mikeb@


# 1.232 29-Mar-2012 mlarkin

Missing #ifdef in previous diff


# 1.231 29-Mar-2012 mlarkin

Prevent hibernation attempts on machines with unsupported disk controllers.
The same check is also performed later in the hibernate sequence, but by
then it's too late to cleanly unwind (presently).

ok deraadt


# 1.230 26-Mar-2012 deraadt

simplify conditionals in acpi_sleep_state()
ok mlarkin


# 1.229 26-Mar-2012 deraadt

hook in the hibernate request code; half of this diff is from mlarkin
ok mlarkin


Revision tags: OPENBSD_5_1_BASE
# 1.228 20-Sep-2011 deraadt

Very early in suspend while the clock is still ticking realtime,
save the clock back to the rtc. The zaurus already did this.
ok phessler miod kettenis


Revision tags: OPENBSD_5_0_BASE
# 1.227 02-Jul-2011 nicm

kqueue attach functions should return an errno or 0, not a plain 1. Fix
the obvious cases to return EINVAL and ENXIO.

ok tedu deraadt


# 1.226 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.225 16-Jun-2011 pirofti

Import acpitoshiba driver.

This is originally written by Hiroyuki Aizu, ported and integrated
in our tree by Javier Vazquez <javama4 AT gmail> with tweaks and
suggestions by me.

Import prodded by deraadt@ and the lack of testers on tech@.


# 1.224 27-Apr-2011 jcs

attach acpithinkpad to newer lenovo models like the x120e

ok deraadt@


# 1.223 22-Apr-2011 jordan

Fix uninitialzied variables and formatting strings (-Wxxx errors)


Revision tags: OPENBSD_4_9_BASE
# 1.222 02-Jan-2011 jordan

Re-add code for ACPI taskqueue, eliminates need for state vars in acpi softc
Serialize processing ACPI tasks for sleep/powerdown/GPE event
ok deraadt@


# 1.221 31-Oct-2010 guenther

Revert last commit: it breaks resume on Thinkpads
"then please back it out" deraadt@


# 1.220 26-Oct-2010 jordan

Add task queue for ACPI gpe and notify handlers
This fixes eject on Dell Latitude dock and an issue on the Dell Mini
battery update.
ok deraadt


# 1.219 07-Oct-2010 jordan

Create separate functions for enabling wake and runtime gpes.
Remove old suspend/resume gpewalk function
ok deraadt, mlarkin


# 1.218 05-Oct-2010 jordan

split acpi_enable_onegpe to separate enable/disable functions
ok deraadt


# 1.217 05-Oct-2010 jordan

Added acpi_disable_allgpes function to clear GPE status
ok deraadt


# 1.216 05-Oct-2010 deraadt

clean up acpi_softc use more


# 1.215 05-Oct-2010 deraadt

delete a bit of #if 0 code


# 1.214 05-Oct-2010 deraadt

hide use of our global acpi_softc pointer a bit better


# 1.213 29-Sep-2010 deraadt

grab the acpi thread lock very early in acpi thread startup; not that it
is likely that anyone will race us... but why be careless.
ok kettenis


# 1.212 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.211 29-Aug-2010 jasper

- add a check for FWRITE here too.

ok miod@


# 1.210 27-Aug-2010 kettenis

Make PCI Power Management optional, and only enable it when acpi(4) attaches.

ok deraadt@


Revision tags: OPENBSD_4_8_BASE
# 1.209 08-Aug-2010 kettenis

Close race between the acpi thread and wscons ioctls running in process
context. We might rip this out post-release and replace it with a solution
that serializes things in a more obvious way.

ok deraadt@, miod@ and "those crazy texans who ok anything"


# 1.208 08-Aug-2010 deraadt

Report the acpi sleep states when on a RAMDISK; ok krw


# 1.207 07-Aug-2010 kettenis

Enforce acpi register access rescrictions.

ok mlarkin@, deraadt@, tested by marco@


# 1.206 07-Aug-2010 deraadt

upon resume, notify all the acpiac and acpibat drivers to update
their status. some laptops do not do this automatically.
ok kettenis canacar


# 1.205 06-Aug-2010 marco

Always call _PSW on _LID devices that support it to enable lid open to
resume the machine. Conversely disable it when the machine wakes up.

Tested by several ok deraadt


# 1.204 06-Aug-2010 deraadt

As part of a revamp of the PM1/GPE code, I write this diff to look for
unmanaged EN & STS bits in the PM1 register at interrupt time and report
them. As a side effect this splits the STS acknowledgement into two writes
(for power, and sleep) instead of one. The printf that is added (to spot
unmanaged STS bits) has not yet been triggered as far as we know.

Before the "write to PM1 registers at the right offset" diff went in, this
was not neccessary. But newer thinkpads do not have a working soft power
button without this diff.

We have no idea why.
ok mlarkin kettenis


# 1.203 05-Aug-2010 deraadt

PM1 power button events were generating 'fake events' to acpibtn PWRB,
but if there is no such device these were being tossed. Instead, arrange
for all power button handlers to flag a softc variable which is processed
by the thread; so that one place will know to request a powerdown (psignal
to init)
ok marco mlarkin


# 1.202 05-Aug-2010 deraadt

rename a variable which is annoyingly poorly named


# 1.201 05-Aug-2010 mlarkin

Write to the correct offset for various ACPI pmreg registers. Writing to
the correct register does properly clear the wake status bits and fixed
event bits, which allows some machines to have more than 1 suspend cycle.

bug spotted by deraadt@

tested by deraadt@ and myself on a half-dozen machines and others on
hackers@

ok deraadt@, marco@


# 1.200 05-Aug-2010 deraadt

Fix the power button. The pm1 register is 16 bits in size.
My mistake, a while back.
tested by krw


# 1.199 04-Aug-2010 kettenis

Print supported sleep states.

ok marco@, phessler@, deraadt@


# 1.198 03-Aug-2010 mlarkin

Use the proper flag for re-enabling certain hardware events (power btn,
sleep btn). The (incorrect) flag prevoiously used coincidentally had the
same value, so this shouldn't cause any different behavior than before.

tested on a variety of machines (i386, amd64, sp, mp) - no behavioral
change seen

ok deraadt@


# 1.197 28-Jul-2010 marco

Make all gasio failures look and behave the same.


# 1.196 28-Jul-2010 mlarkin

Delay a little longer on suspend before giving up. Fixes machines that
sometimes don't suspend because the sleep transition takes too long
(symptoms like failing to sleep but the backlight stays on).

ok deraadt@


# 1.195 27-Jul-2010 deraadt

remove XXX; EC can now be attached early when needed; ok jordan


# 1.194 27-Jul-2010 deraadt

more brackets feels good


# 1.193 27-Jul-2010 deraadt

XXX annotate a block in acpi_enter_sleep_state() that is pretty worrying


# 1.192 27-Jul-2010 deraadt

horrific indentation that nearly made me puke


# 1.191 27-Jul-2010 mlarkin

Set the sleeping indicator light on machines that support it, in case SMI
doesn't do it for us. Shows the 'moon' LED on older Thinkpads when
sleeping.

Tested on T43p, W500, T510 by me and on X61s by deraadt@.

ok deraadt@


# 1.190 27-Jul-2010 jordan

Early initialization of acpiec if ECDT table exists
Fixes hang when booting thinkpads while docked
ok deraadt


# 1.189 26-Jul-2010 deraadt

remove merge error which was commited


# 1.188 26-Jul-2010 pirofti

Add support for Sony ACPI hotkeys via a new driver: acpisony(4).

Currently it only works for the suspend button and tries to do right for
the brightness events, but I haven't found a Sony laptop that like to do
right so far.

In the future I want to make the brightness keys work on all Sony's and
also add support for the zoom hotkeys and whatever other funky keys I can
find on those things.

Okay deraadt@.


# 1.187 22-Jul-2010 deraadt

pretty large cleanup of SMALL_KERNEL


# 1.186 21-Jul-2010 deraadt

in the acpi GPE handler, late ack level interrupts not edge interrupts.
Duh.


# 1.185 21-Jul-2010 deraadt

Merge the edge and level gpe handlers into one (now possible since the
gpe structure tells us whether the gpe is edge or level)


# 1.184 21-Jul-2010 deraadt

delete #if 0 code we are not going to use; ok marco


# 1.183 20-Jul-2010 deraadt

make the mask code for acpi_enable_onegpe() match other similar code


# 1.182 20-Jul-2010 deraadt

Handle the acpi interrupt controller with proper edge/level handling,
wrapped in spltty to avoid racing against the interrupt controller.
Repair the gpe bit masking code while there, and do operations in the
right order
ok kettenis mlarkin, help from jordan at figuring out the order of
operations.


# 1.181 20-Jul-2010 matthew

When we're preparing to go to sleep, only call bufq_restart() if other
preparations failed.

ok kettenis@, thib@, deraadt@


# 1.180 19-Jul-2010 deraadt

permit compilation with ACPI_DEBUG


# 1.179 19-Jul-2010 deraadt

there is no need for a seperate dsdt_softc pointer; dsdt does not have a
softc but is just a part of the single acpi driver.
ok jordan


# 1.178 19-Jul-2010 deraadt

acpi_delay() is no longer needed; ok jordan


# 1.177 18-Jul-2010 mlarkin

Clear event status on resume, to avoid going back to sleep/reboot/shutdown
immediately after resume. Tested by a bunch of people on hackers@ on
various hardware. Fixes at least Toshiba NB200 and Gateway LT3103u.

ok marco


# 1.176 13-Jul-2010 deraadt

rename some thread related interfaces so they are logical


# 1.175 10-Jul-2010 jordan

Verify _ADR bdf is correct (some systems return 0xffff)
Works on Dell x9xx and Studio 1555


# 1.174 09-Jul-2010 jordan

Clear the noise


# 1.173 08-Jul-2010 jordan

Add mapping for ACPI device to PCI bdf (match autoconf tree)
Simplify resource parsing function to use buffer argument
Convert namespace linked lists to use queue macros
ok marco@, deraadt@


# 1.172 06-Jul-2010 deraadt

Use spltty() to lock downcalls from apm against the information being
modified by the acpi thread.
ok kettenis


# 1.171 06-Jul-2010 deraadt

Don't use a workq to do the suspend, because it races aginst the acpi
thread. Instead, just tell the acpi thread to do the suspend for us.
This makes apmd & zzz work correctly. While here, have acpithinkpad
attempt to post the event to apm before prompting the suspend itself.
ok kettenis marco mlarkin


# 1.170 05-Jul-2010 mlarkin

Reset register SCI_EN on resume. The ACPI spec clearly states we don't have
to do this, but many machines don't, or forget to, which leads to all sorts
of problems on resume (broken battery stats, overheating, failure to resume
more than once, etc).

tested by many, ok marco@, deraadt@


# 1.169 01-Jul-2010 thib

do bufq quiesce again, since vnd/the bufq_done call in
biodone has been fixed.


# 1.168 01-Jul-2010 jordan

Backout recent AML changes, breaks on R210 and others


# 1.167 01-Jul-2010 jordan

Simplified aml_parse_resource call, cleanup code
ok marco


# 1.166 01-Jul-2010 jordan

Fix subordinate bus # for multi-root PCI buses
ok marco, kettenis


# 1.165 30-Jun-2010 thib

Disable/partially backout the bufq quiesce changes as this
is causing havoc with vnds and release must be buildable.


# 1.164 29-Jun-2010 deraadt

Fix ramdisks (new code was placed inside a SMALL_KERNEL chunk)


# 1.163 29-Jun-2010 jordan

Add support for mapping ACPI to PCI devices
ok kettenis, deraadt


# 1.162 29-Jun-2010 kettenis

Call bufq_quiesce() on suspend to stop all I/O and bufq_restart on resume.
Makes suspend/resume work much better while doing disk I/O.

ok mlarkin@


# 1.161 29-Jun-2010 tedu

60 is a more reasonable guess for number of minutes in an hour than 100.
spotted by TeXitoi. ok deraadt


# 1.160 27-Jun-2010 jordan

Add _acpi_matchhids routine, precursor to pci device match


# 1.159 27-Jun-2010 mlarkin

Clear ACPI fixed event status on resume (power buttons/etc) so that some
machines don't immediately resume after suspending to S3.

Tested by kettenis@ and I on 5 or 6 machines, deraadt@ "yeah, looks good"


# 1.158 27-Jun-2010 jordan

Cleaned up acpi_maptable, adds to table list directly


# 1.157 07-Apr-2010 deraadt

If we have done a wsdisplay_suspend(), and ACPI decides it does not want to
suspend, we must roll back by calling wsdisplay_resume() or the virtual
console switching will be locked forever
ok oga


# 1.156 07-Apr-2010 kettenis

Make suspend/resume work on MP machines (running an MP kernel). Joint work
from mlarkin@ and me, with some amd64 fixes thrown in by deraadt@

ok marco@, deraadt@, pirofti@, mlarkin@


# 1.155 31-Mar-2010 kettenis

Bring /dev/apm support in line with apm(4). Make the suspend button send
a suspend request event to apmd(8) instead of suspending immediately. Also
keep track of whether /dev/apm and /dev/apmctl are currently open, such that
we can still suspend immediately if apmd(8) isn't running.

ok deraadt@, marco@, pirofti@, jsing@, oga@


# 1.154 30-Mar-2010 oga

Prevent the apmd/x races for good.

When we hit suspend time, go through all wsdisplays on the system. if
they are in mode MAPPED, but not MODE_DUMBFB then if possible do a full
vt switch to a !mapped vt, and prevent switching back until resume time.
This has to be called from MD code because this involves userland
running so that X can run the vt switch signal handler. This way, any
case where we are using the "poke registers from userland" model, we
will not be on the hardware when we go down, so the kernel can actually
handle thing properly.

Tested on several acpi laptops (by kettenis@ and ian@), x40 (me and
beck@ at LEAST) and zaurus (me). Maybe others, but if so I forgot who at
this time..

Idea from deraadt somewhere over the Faroe Islands (I thought of a
similar thing myself a while ago). Much prompting from him. Ok and
comments miod@


# 1.153 25-Mar-2010 oga

Fix & vs. && in the kqfilter code.

Based on a comparison with the apm code.

ok deraadt@, kettenis@


Revision tags: OPENBSD_4_7_BASE
# 1.152 26-Nov-2009 mlarkin

Visual (and on some machines, audio) feedback while the machine is
resuming via calls to aml _SST method. This also makes the "moon" LED on
thinkpads to blink during resume and cease when resume is completed.

ok pirofti@


# 1.151 26-Nov-2009 deraadt

Toggle into "cold" when we we disable interrupts, because tsleeping
in the AML interpreter and context switching into userland processes
tends to sometimes pop the ACPI trampoline page out of our address
space. Makes lots more machines work.
ok kettenis mlarkin


# 1.150 24-Nov-2009 jsg

in the resume path evaluate _WAK before dealing with device drivers
like the spec says, not after.

ok mlarkin@


# 1.149 24-Nov-2009 deraadt

abort suspends on SMP systems until that gets written; ok mlarkin


# 1.148 23-Nov-2009 mlarkin

pci should return the result of it's children's suspend/resume calls back
to its parent.

handle suspend failure case and unwind if devices have an activate function
that returns failure.

ok deraadt@, kettenis@


# 1.147 23-Nov-2009 mlarkin

acpi* devices need to call their children on suspend/resume.
ok deraadt@


# 1.146 23-Nov-2009 pirofti

Remove ACPI_SLEEP_ENABLED checks.

This enables by default the suspend/resume paths in the kernel.

Okay deraadt@.


# 1.145 23-Nov-2009 deraadt

knf and spacing that are super annoying


# 1.144 23-Nov-2009 mlarkin

Hook up Theo's new children suspend/resume function, as well as more ACPI
suspend/resume glue.

ok deraadt@


# 1.143 26-Oct-2009 deraadt

Do not do strncmp comparisons on dv_xname because longer device names which
look similar could arrive in the future. Instead, compare directly against
dv_cfdata->cf_driver->cd_name
Issue originally spotted by miod


# 1.142 26-Oct-2009 jordan

Verify checksum+address when loading ACPI tables.
Some systems had invalid entries in RSDT/XSDT.
ok marco@


# 1.141 23-Jul-2009 cnst

New aibs(4) driver for ASUSTeK AI Booster (ACPI ATK0110) hardware monitoring,
with sensor state support through limits provided by the ACPI.

Tested on several ASUS motherboards kindly networked by Sam Fourman Jr.

ok deraadt marco jordan


Revision tags: OPENBSD_4_6_BASE
# 1.140 03-Jun-2009 pirofti

Import acpi power resource driver. This is needed for suspend/resume logic.

Okay jordan@ and marco@.


# 1.139 03-Jun-2009 pirofti

Add acpivideo support. This adds brightness support for all laptops
except thinkpads, they will use the acpithinkpad driver. The driver is
also hooked into wsconsole. So brightness can be adjusted via:

$ wsconsctl display.brightness=<percentage>

This is very helpfull on some laptops that have a nasty bios and get two
steps instead of one when pressing the brightness button.

Tested on various dell, fujitsu, acer, samsung and other laptops.

Okay marco@, miod@. Suggestions from kettenis@.
Lots of reviews and help from miod@, thanks!


# 1.138 03-Jun-2009 jordan

Added ejectable bay for IDE cdroms
ok marco@


# 1.137 30-Apr-2009 marco

Oga changed apm ioctls and forgot to grep. Temporary work around by hitting
the sleep button when we get any request. This interface will chnage at
some point.
ok oga


# 1.136 19-Apr-2009 krw

Call acpi_sleep_walk() when powering down just like when entering
any other sleep state. This tells acpi wake devices to not wake up
the box until manual powering up resets their status. Makes my Dell
GX520 stay powered off in response to 'halt -p'.

In snaps for a while.

ok marco@ (several times) deraadt@


# 1.135 17-Apr-2009 pirofti

Add device ranking support for future sleep/wake developments.

okay marco@


# 1.134 11-Mar-2009 jordan

Cleanup.. use aml_evalinteger instead of aml_evalname
ok marco@


# 1.133 10-Mar-2009 jordan

Fix cut/paste error.. call _BFS not _PTS
ok marco@


Revision tags: OPENBSD_4_5_BASE
# 1.132 21-Feb-2009 marco

overzealous cut/paste; found by Vladimir Kirillov <proger@uaoug.org.ua>


# 1.131 20-Feb-2009 marco

Oops didn't mean for Debugger() to remain there.


# 1.130 19-Feb-2009 marco

suspend/resume bits so that we can develop this in tree. This is disabled.
code from mlarkin and me
help from art,toby,jordan and several others
ok jordan, go for it deraadt


# 1.129 10-Feb-2009 jordan

Fix null pointer for shutdown if acpi not enabled
ok marco@


# 1.128 04-Feb-2009 kettenis

Some systems that claim to be ACPI 2.0 apparently don't provide an XSDT
address. Make the code fall back on the RSDT on those systems.

From Joerg Niendorf <p3y80@internode.on.net>. Fixes PR 5986.


# 1.127 17-Jan-2009 krw

A couple of stray malloc/memset -> malloc(M_ZERO), a bit of knf and
lose a couple of unnecessary casts. No functional change.

Feedback from kettenis@

ok kettenis@ marco@


# 1.126 06-Nov-2008 marco

Collapse all sanity checks of hids into one function.

Originaly from jordan


Revision tags: OPENBSD_4_4_BASE
# 1.125 02-Jul-2008 fgsch

add acpivideo. for now almost an empty skel so others can work on
suspend/resume. eventually it will also manage output switching and
brightness where supported. prodded by marco@


# 1.124 11-Jun-2008 marco

Fix $OpenBSD$ for once and for all


# 1.123 08-Jun-2008 deraadt

aaa_dev was being set from an uninitialized variable; ok toby


# 1.122 07-Jun-2008 marco

Kill ACPIVERBOSE. It was never used and is worthless to boot.

comments jmc
ok toby


# 1.121 06-Jun-2008 marco

Shrink acpi some more for floppies and while here re-enable acpi on the
floppies. Fixes crazy sony vaio kettenis has.

tested by deraadt
ok deraadt jordan kettenis


# 1.120 01-Jun-2008 marco

Fix \n nit in SMALL_KERNEL.


# 1.119 01-Jun-2008 marco

Re-enable new parser. Lots of testing by various folks. Fits all media
now too.

ok kettenis deraadt


# 1.118 16-May-2008 dlg

back out jordans new parser.

it didnt have enough oks from the right people, it did break machines
that were previously working, and it bloated the RAMDISKS and install
media beyond the constraints of the media.

requested by deraadt@


# 1.117 14-May-2008 jordan

Adding new ACPI Parser code
Old guts of code still exists, needs to be torn out next
ok marco@
ok brad@


# 1.116 27-Apr-2008 jcs

add acpithinkpad(4), an acpi support driver for ibm/lenovo
thinkpads.

provides functionality that the bios did on older, non-acpi
thinkpads like respond to screen brightness keys and toggle the
bluetooth device on and off.

ok deraadt@ and marco@


# 1.115 24-Apr-2008 jsing

Introduce acpiasus(4), a driver for the ACPI based hotkeys found in many
ASUS laptops (including the ASUS EeePC) - largely based on NetBSD's
asus(4) driver. On the ASUS EeePC this allows us to enable/disable
wireless, change screen brightness and use the volume keys.

ok jsg@, weingart@


# 1.114 12-Apr-2008 kettenis

Get rid of acpi_s5 global variables; simply send SIGUSR2 instead of SUGUSR1
to tell init(8) to power down the machine.

ok krw@


Revision tags: OPENBSD_4_3_BASE
# 1.113 21-Jan-2008 jsg

Check against RESET_REG_SUP in FADT before doing the acpi reset dance
after all, not doing so appears to break more machines than it fixes.

ok marco@


# 1.112 05-Jan-2008 kettenis

Never attach more acpicpu(4) devices than cpu(4) devices. Always attach
the first acpicpu(4) device on non-MULTIPROCESSOR kernels, regardless of its
ACPI processor ID.

ok mikeb@


# 1.111 05-Dec-2007 deraadt

Hang acpi(4) below bios(4) instead of mainbus(4). This lets us move acpi(4)
before pcibios(4), and then.... if acpi(4) attaches, skip pcibios(4) since
it messes the machines up. Fixes claudio's HP dl320 G5, and almost assuredly
others.
ok kettenis


# 1.110 05-Dec-2007 deraadt

repair acpiopen/acpiclose and friends to handle the /dev/apmctl interface,
so that apmd(8) can actually work correctly against acpi(4), and thus,
even do the fancy -C/-A stuff; also tested by jmc


# 1.109 02-Dec-2007 fgsch

fix the trailing space for real now.


# 1.108 02-Dec-2007 fgsch

avoid trailing spaces on output; marco@ ok.


# 1.107 27-Nov-2007 kettenis

Fix off-by-one that made us attach acpicpu0 and acpicpu1 on GENERIC kernels.

ok marco@


# 1.106 25-Nov-2007 jsg

When available and ACPI is enabled use the ACPI reset method
to reboot. This is required for some quirky sis machines
and likely others to reboot.

Tested by many. 'commit early commit often' deraadt@


# 1.105 19-Nov-2007 deraadt

sigh, amd64 still uses that stupid acpi_enabled variable...


# 1.104 19-Nov-2007 deraadt

softc's come initialized to 0, you know...


# 1.103 19-Nov-2007 deraadt

store revision; from jsg


# 1.102 19-Nov-2007 kettenis

Initialize sc_facs earlier.

ok deraadt@, beck@, canacar@


# 1.101 16-Nov-2007 deraadt

fix the bus_space #define nightmare, so that amd64 and i386 are much more
uniform. as a result shared code like acpi needs less #ifdef's
ok marco kettenis


# 1.100 15-Nov-2007 deraadt

remove the #ifdef ACPI_ENABLE. the EC codepath is either safe enough (and
will be improved), or it is unsafe because AML assumes it must be there
ok almost everyone


# 1.99 15-Nov-2007 mikeb

Make acpicpu(4) attach to the existing CPUs only.

Problem reported by form@. Fix idea by kettenis@.
Lots of help from deraadt@. Tested by deraadt@, form@ and me.

ok deraadt marco


# 1.98 14-Nov-2007 deraadt

spacing


# 1.97 12-Nov-2007 deraadt

correct : vs , in attach-time messages


# 1.96 06-Nov-2007 deraadt

all ACPI_ENABLE kernels need acpi_interrupt()


# 1.95 06-Nov-2007 deraadt

move acpi_read_pmreg() and acpi_write_pmreg() out of SMALL_KERNEL so
that acpi ramdisks can compile (obvious solution)


# 1.94 05-Nov-2007 canacar

Establish the acpi interrupt earlier. Fixes problems with some
GENERIC.MP kernels not getting acpi interrupts. This is a
workaround until the real issue with late establishment is fixed.
ok weingart@, beck@, gwk@


# 1.93 03-Nov-2007 jordan

Added support for displaying wakeup devices
ok beck@,weingart@,gwk@


# 1.92 11-Oct-2007 kettenis

When walking the tree yo find _PRT methods, check whether the device is
actually enabled and functioning. Similar to what we do for _INI.

ok weingart@, beck@


# 1.91 08-Oct-2007 krw

More simple memset(,0,) -> M_ZERO changes. In this batch move to
size(*p) as the first malloc() parameter where p is declared locally
and thus easy to check. Add M_ZERO to gpe_table allocation in acpi.c
even though there is no obvious bzero or memset nearby.


# 1.90 13-Sep-2007 mk

Remove unnecessary memset() (see aml_evalnode(9)) and a bit of KNF.

ok toby


# 1.89 13-Sep-2007 weingart

Implement ACPI 6.5.1 spec tree walk for _STA and _INI.
Fixes mk's laptop. No regressions so far (thank you to the
testers).

ok gwk@, mk@, marco@


Revision tags: OPENBSD_4_2_BASE
# 1.88 17-Apr-2007 mk

Now that acpidock no longer hangs machines during boot (at least
according to the reports I've been getting) it should be safe to enable
it again, so remove the #if 0 stuff around it.

ok marco


# 1.87 11-Apr-2007 jordan

Added changes for C-State
ok marco@


# 1.86 26-Mar-2007 marco

Calculate minutes left more accurately.

From giovanni <qgiovanni@gmail.com>


Revision tags: OPENBSD_4_1_BASE
# 1.85 03-Mar-2007 mk

Stop looking for acpidock until we fix that stuff that breaks all pre-60
series ThinkPads with acpidock(4) enabled.


# 1.84 22-Feb-2007 marco

We were getting no interrupts in .MP. There are 2 fixes; one in
acpi_machdep.c and move the interrupt establishment down after ioapic
has been initialized.

ok jordan


# 1.83 21-Feb-2007 kettenis

Make an acpi-enabled GENERIC.MP work on systems that only support PIC mode.

ok marco@


# 1.82 19-Feb-2007 jordan

Added acpi_map_address function for mapping GAS/IO registers
ok marco@


# 1.81 18-Feb-2007 jordan

Added dynamic perfect hash for opcodes
ok marco@


# 1.80 18-Feb-2007 marco

Shave off another K pre compression.
Shuffle SMALL_KERNEL functions around so that we only have a single #ifndef


# 1.79 17-Feb-2007 marco

Add SMALL_KERNEL throught acpi to make it fit on boot media.
ok deraadt


# 1.78 13-Feb-2007 marco

Add smarter _INI code. This helps with some dissapearing devices.

ok kettenis jordan


# 1.77 31-Jan-2007 gwk

Introduce acpi_hasprocfvs thats "ACPI Has Processor Frequency and Voltage
Scalling" a global flag set by acicpu attach to indicate that ACPI is
handling fvs through the _PSS objects. This will fix disappearing
EST/powernow when booting kernels with acpi enabled, as acpicpu isnt
compiled into GENERIC yet. Also in cases where acpicpu is built in and
legacy EST/powernow work but acpi is missing the _PSS object, the legacy
routines will be used.

tested by dim@, and Nick Nauwelaerts <nick AT nauwelaerts.net>
ok marco@, dim@


# 1.76 22-Jan-2007 mk

Actually look for docking stations by looking for _DCK nodes.

ok marco


# 1.75 02-Jan-2007 marco

On some systems EC must be attached as the first device. Move EC from
generic _HID devices to its own routine to force first attach. Bug found
and debugged by joris@

Code mostly written by joris@


# 1.74 26-Dec-2006 marco

Add polling to devices that require it such as acpibat and acpitz.
Use the same tsleep/wakeup mechanism as the interrupt code to keep it non-
concurrent.


# 1.73 21-Dec-2006 deraadt

even more knf love


# 1.72 21-Dec-2006 marco

Remove some leftover acpiec gpe/interrupt code.


# 1.71 20-Dec-2006 deraadt

oops, name already used


# 1.70 20-Dec-2006 deraadt

rename __acpi_enable_gpe(); ok marco


# 1.69 20-Dec-2006 deraadt

acpi_unmap_pmregs() is not used, ok marco


# 1.68 19-Dec-2006 deraadt

simplify some low-level methods substantially (shrinking code); ok marco jordan


# 1.67 06-Dec-2006 mk

malloc(..., M_NOWAIT) with no NULL check is bad. If allocation fails,
complain, clean up, and bail out of attach.

input and ok gwk


# 1.66 28-Nov-2006 jordan

Fixed clearing arg for acpi_init_pic
ok kettenis@


# 1.65 27-Nov-2006 kettenis

Don't spam dmesg.


# 1.64 27-Nov-2006 jordan

Fixes for acpiprt routing
Post-parse fixup for namerefs
ok kettenis@


# 1.63 25-Nov-2006 marco

Don't handle acpiev events inside the gpe handler since this causes deadlocks.

ok jordan@


# 1.62 15-Nov-2006 kettenis

Hook up acpi apic interrupt routing support to generic acpi code.

ok jordan@


# 1.61 02-Nov-2006 marco

Kill acpiec attach function and add its functionality to the generic _HID
device attach

prodded and ok jordan


# 1.60 02-Nov-2006 marco

Fix comments to reflect reality.


# 1.59 19-Oct-2006 marco

Replace ACPI ioctls with APM ioctls. This makes ACPI talk the same protocol
as APM therefore userspace can now retrieve power information in the same
fashion as all other apm arches (zaurus, macppc, i386 etc).

To use force the use of acpi device like so: apmd -f /dev/acpi

ok jordan@


# 1.58 19-Oct-2006 jordan

Fixed GPE debug levels
Fixed ref counter on temporary bufferfields
DSDT parsing fully working on IBM Thinkpad
ok marco@


# 1.57 15-Oct-2006 jordan

Removed old GPE handler code remnants
ok marco@


# 1.56 12-Oct-2006 jordan

Massive ACPI changes
Fixed ACPI leaks
new ACPI API
ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.55 30-Jun-2006 gwk

KNF, No functional change,
"go at it" marco@


# 1.54 30-Jun-2006 jordan

Support for calling Embedded Controller via new GPE interface
ok marco@


# 1.53 30-Jun-2006 jordan

Added new code for GPE handling
Also works with ACPIEC; need to fixup acpiec.c to remove original interrupt handler
ok marco@


# 1.52 30-Jun-2006 jordan

Added offset argument for acpi read/write pmreg routines (new GPE code)
ok marco@


# 1.51 31-May-2006 todd

do not call acpi_powerdown() unless acpi has successfully initialized
ok gwk@ canacar@


# 1.50 29-May-2006 canacar

Add embedded controller support.
ok marco@ deraadt@ testing & ok gwk@


# 1.49 19-May-2006 canacar

Minimal thermal zone driver. Monitors thermal zone temperature,
shuts down the system if the 'critical temperature' is reached.
ok marco@


# 1.48 11-Apr-2006 marco

KNF.


# 1.47 11-Apr-2006 gwk

Add acpi_resume(struct acpi_softc *sc), which calls some AML methods,
detected at attach time, currently unused.

ok marco@


# 1.46 11-Apr-2006 gwk

Discover some ACPI sleep related methods, and call them at the
appropriate time in acpi_enter_sleep_state().

ok marco@


Revision tags: OPENBSD_3_9_BASE
# 1.45 26-Feb-2006 marco

Add acpicpu as a device.

ok jordan@


# 1.44 23-Feb-2006 marco

remove dead function.

ok jordan@


# 1.43 22-Feb-2006 marco

Print out all tables it found during autoconf. We'll need this data in
the future.


# 1.42 22-Feb-2006 marco

Make dmesg less verbose.

ok jordan.


# 1.41 22-Feb-2006 jordan

Added parsing of SSDT tables
ok marco@


# 1.40 22-Feb-2006 jordan

Added new aml_register to notify by device id
Moved powerdown code to button device
ok marco@


# 1.39 21-Feb-2006 marco

Readjust debug levels that crept in too low over the last few days.
KNF


# 1.38 21-Feb-2006 marco

Rig the power button so that the machine shuts down correctly.


# 1.37 20-Feb-2006 marco

Add acpibtn device. Remains disabled for now.


# 1.36 19-Feb-2006 jordan

Added code for aml_notify GPE notification callback
ok marco@


# 1.35 19-Feb-2006 grange

Fix acpi_enter_sleep_state() by replacing current ugly hack
with mostly correct sleeping state transition code.

ok brad@ marco@


# 1.34 19-Feb-2006 marco

jordan:
Enable interrupts.
Added peripheral code to make this work.

marco:
Replace softintr and other types of interrupts with a handler with context.
Fixed interrupt magic for GPEs.
Some KNF

This is work done by jordan and I but has to be a single commit.


# 1.33 17-Feb-2006 marco

Remove earlier introduced delays. We really only need them during autconf.

ok jordan@


# 1.32 17-Feb-2006 marco

Add a generic delay function as per the ACPI spec.

from jordan@


# 1.31 17-Feb-2006 marco

Use tsleep instead of delay when !cold.


# 1.30 17-Feb-2006 marco

Argh, we need delays between reads/writes. The backend reads (i2c most likely)
are so slow that values come back as 0 sometimes. We have to do this
despite the spec saying we shouldnt have to. Guess having a faster
parser than others can hurt sometimes.

Idea from jordan.


# 1.29 17-Feb-2006 jordan

Fixed code for handling _INI
Rearranged debug statements
ok marco@


# 1.28 16-Feb-2006 jordan

Added stub for calling _INI
Fixed aml_dowhile for NULL return
ok marco@


# 1.27 16-Feb-2006 jordan

Fixed parser/evaluator to retrieve battery info+state
Cleanup of parser
Dell laptops return battery status
ok marco@


# 1.26 06-Feb-2006 brad

fix a typo so this will compile on amd64.


# 1.25 04-Feb-2006 marco

When ACPI_ENABLED wasn't set but the devices were it had an unknown variable.

Pointed out by ian@.


# 1.24 03-Feb-2006 jordan

Rewrote aml parser yet again...
Fixed bus_space_map for i386/amd64
Added _BST and _BIF to acpibat to get status/info
ok marco@


# 1.23 20-Jan-2006 jordan

More evaluator changes
Removed old evaluator code
Fixed _HID value
ok marco@


# 1.22 19-Jan-2006 jordan

Added support for read/write of PCI Config space
Help jason@
ok marco@


# 1.21 18-Jan-2006 jordan

Added new evaluation routines
Added helper methods for buffer/field evaluation
Fixed broken indentation on dsdt.c

ok marco@


# 1.20 17-Jan-2006 jordan

Major overhaul of the aml parser. Evaluater temporarily disabled.

ok marco@


# 1.19 06-Jan-2006 grange

Check for ACPI control availability only if ACPI_ENABLE defined.


# 1.18 06-Jan-2006 grange

Informative error messages on attach, prettier dmesg.


# 1.17 06-Jan-2006 grange

printf -> dnprintf conversion for debug messages.


# 1.16 05-Jan-2006 grange

Find SLP_TYPx values and store them for future use in
acpi_enter_sleep_state().


# 1.15 05-Jan-2006 grange

Random KNF and cleanup


# 1.14 05-Jan-2006 grange

Provide symbolic names for the acpi sleeping states and use it.


# 1.13 28-Dec-2005 marco

Moved DEVNAME to a acpidev.h

ok jordan@


# 1.12 18-Dec-2005 sturm

add back comment about wrong code

ok marco


# 1.11 16-Dec-2005 marco

Remove test for kbd_reset. Prompted deraadt@, ok jordan@


# 1.10 16-Dec-2005 jordan

Added hooks for powerdown/power button press


# 1.9 16-Dec-2005 jordan

Cleaned up common power management register i/o
Added enter sleep state ioctl
Hooked power button interrupt
ok marco@


# 1.8 16-Dec-2005 marco

Remove autoconf chatter


# 1.7 16-Dec-2005 jordan

Moved DSDT AML parsing into parent ACPI driver
ok marco@


# 1.6 14-Dec-2005 marco

Add AC device glue.


# 1.5 14-Dec-2005 marco

Adjust debug printing levels to something more sane.


# 1.4 13-Dec-2005 marco

Add glue to attach devices to apci.


# 1.3 07-Dec-2005 marco

KNF


Revision tags: OPENBSD_3_8_BASE
# 1.2 10-Jul-2005 grange

Spacing cleanup.


# 1.1 02-Jun-2005 tholo

Start on a basic ACPI framework -- does not do much more than read out the
ACPI tables into kernel memory and attach ACPI and HPET timers currently.

In order to test this code, enabling the devices in GENERIC as well as
the ACPI_ENABLE option is needed. This code does not do any thermal
control yet, so this should be done with care depending on the platform.

In the tree so more people can contribute to making this more fully
featured.

Ok niklas@ grange@ tedu@


# 1.369 08-Jun-2019 kettenis

When establishing the mapping between ACPI device nodes and PCI devices,
skip PCI host bridges and devices that aren't there.

ok patrick@


# 1.368 07-Jun-2019 kettenis

Introduce acpi_getsta() and use it to eliminate some duplicated code.

ok patrick@


# 1.367 12-May-2019 kettenis

Make sure the extended PM register descriptions actually exist before
using them. Fixes machines with ancient ACPI 1.0 (such as recent
hypervisors).

ok deraadt@


# 1.366 12-May-2019 kettenis

Add support for mapping ACPI PM registers using the "extended" gasio
representation from the FADT. Mostly fixes the Lanner NCA-1510.

ok lteo@, mlarkin@, deraadt@


# 1.365 11-May-2019 lteo

Fix incorrect error message.

ok kettenis@


# 1.364 08-May-2019 tedu

free size. the allocation is the header plus the length.


Revision tags: OPENBSD_6_5_BASE
# 1.363 04-Apr-2019 kettenis

Avoid potential null-pointer dereference. Found by Kent R. Spillner.

ok deraadt@, jsg@


# 1.362 20-Jan-2019 jsg

fix SMALL_KERNEL build after changes in rev 1.361
reported by naddy@ ok deraadt@


# 1.361 19-Jan-2019 tedu

Add a pwraction sysctl that controls what the power button does on acpi.
By default, nothing changes -- shutdown is initiated. But allows turning
power button into a sleep button if desired.
(grudging) ok from a few parties


# 1.360 26-Oct-2018 kettenis

Add acpipci(4) on amd64. For now this only calls the PCI-specific _OSC
method to let the ACPI implementation know what features we support.


Revision tags: OPENBSD_6_4_BASE
# 1.359 30-Aug-2018 kettenis

Move softbutton check before the check that skips attaching certain acpi
devices. Fixes regression caused by matching on _CID in addition to
matching on _HID.

ok matthieu@, mlarkin@


# 1.358 25-Aug-2018 kettenis

Match ACPI devices based on _CID if no match for _HID is found.

ok mpi@


# 1.357 19-Aug-2018 kettenis

Add support for multiple PCI segments. Only really implemented for arm64
for now as amd64/i386 firmware still caters for legacy OSes that only
support a single PCI segment.

ok patrick@


# 1.356 03-Aug-2018 kettenis

Let ahci(4) match on _CLS instead of _HID when attaching at acpi(4). Avoids
having to add many more _HID entries to the match table.

ok deraadt@, mlarkin@


# 1.355 10-Jul-2018 kettenis

Make legacy interrupts work in acpipci(4).

ok patrick@


# 1.354 05-Jul-2018 kettenis

Add acpipci(4), a driver that supports generic ECAM-compatible PCI host
bridges based on information provided by ACPI.

ok mlarkin@


# 1.353 04-Jul-2018 kettenis

Properly pass around the PCI "chipset tag" in acpi(4) and refactor
acpimcfg(4) to call an MD initialization functions that sets up a tag for
PCI ECAM.

ok guenther@, mlarkin@, krw@


# 1.352 01-Jul-2018 kettenis

Move acpi(4) attach glue into acpi_machdep.c.

ok guenther@, deraadt@


# 1.351 01-Jul-2018 kettenis

Add support for _DSD Device Properties.

ok mlarkin@


# 1.350 30-Jun-2018 kettenis

Add intr_enable() function, intended for MI use to amd64 and i386 and use
this in the acpi(4) suspend/resume code paths.

ok deraadt@


# 1.349 29-Jun-2018 kettenis

Use <stdint.h> types.

ok mpi@, jung@, krw@, deraadt@


# 1.348 29-Jun-2018 mlarkin

ACPI: Allow (with warning) GPE handler reassignment, instead of returning
a failure code.

ok kettenis


# 1.347 26-Jun-2018 mlarkin

knf


# 1.346 26-Jun-2018 kettenis

Let acpi(4) pass down the bus dma tag.

ok deraadt@


# 1.345 25-Jun-2018 kettenis

Reorganize acpi(4) code a bit in preparation for upcoming arm64 support.

ok deraadt@


# 1.344 20-May-2018 kettenis

Wake up the acpi thread after scheduling a gpio event.

ok mpi@


# 1.343 17-May-2018 kettenis

Sprinkle some #ifndef SMALL_KERNEL such that RAMDISK kernels cuild again.

pointed out by stsp@


# 1.342 17-May-2018 kettenis

Implement GenericSerialBus OpRegion support.

ok mlarkin@


# 1.341 27-Mar-2018 kettenis

Add acpicmos(4), a driver that implements SystemCMOS OperationRegion
access support. This fixes machines where the AML doesn't check whether
support for this OperationRegion type has been registered by the OS.

ok mlarkin@


Revision tags: OPENBSD_6_3_BASE
# 1.340 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.339 10-Feb-2018 deraadt

Syncronize filesystems to disk when suspending. Each mountpoint's vnodes
are pushed to disk. Dangling vnodes (unlinked files still in use) and
vnodes undergoing change by long-running syscalls are identified -- and
such filesystems are marked dirty on-disk while we are suspended (in case
power is lost, a fsck will be required). Filesystems without dangling or
busy vnodes are marked clean, resulting in faster boots following
"battery died" circumstances.
Tested by numerous developers, thanks for the feedback.


# 1.338 08-Feb-2018 deraadt

After flushing disks, try again to release all memory. We don't need to
save dirty memory to the hibernate space.


# 1.337 08-Feb-2018 deraadt

Toss all releaseable memory, because fragmentation can get in the way
of allocating the hibernate playpen.


# 1.336 08-Feb-2018 deraadt

sr_quiesce() is a new approach for ensuring that softraid drains
output to the disks.

This is part of a larger suspend/resume filesystem-safety diff, which
has been worked on for a couple of months already. Tests by job, krw,
beck, benno, and others. Sometimes even by snapshot users...


# 1.335 29-Nov-2017 kettenis

Add support for GPIO-signaled events.

ok mlarkin@


# 1.334 16-Nov-2017 jcs

Add PCI attachment for dwiic(4) needed by Intel 100 Series machines

ok kettenis


# 1.333 14-Nov-2017 kettenis

Add a few more PNP IDs to the skip lists.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.332 17-Aug-2017 stsp

Make dwiic(4) attach its ACPI parent dependencies.
This change makes the Asus E200HA keyboard work.

Original analysis and patch by Cesare Gargano
ok kettenis@


# 1.331 09-Aug-2017 dcoppa

Add "PNP0303" (8042 PS/2 Controller) to acpi_isa_hids[]

ok kettenis@


# 1.330 22-Jul-2017 jcs

acpibat: look for _BIX first before falling back on _BIF

ACPI 4.0 deprecated _BIF for battery status so some newer machines
are exporting _BIX which extends _BIF's package with a few extra
fields. When using _BIX, export the new discharge cycle count as a
sensor.

feedback from kettenis and anton
ok deraadt


# 1.329 20-Jul-2017 kettenis

Start a list of isa(4) device names such that we can avoid printing them
as "not configured".

ok mikeb@, millert@, deraadt@


# 1.328 08-Apr-2017 deraadt

Sensors are run as callbacks inside tasks. During suspend a sensor
could be running inside a driver that will be force-detached, or due
to tsleep end up disrupting the softstate/hardstate contract. At
suspend time, quisce all these callbacks by waiting for completion.
This issue has never been observed for real, but may be implicated in
suspend/resume failures.
ok kettenis guenther mlarkin


# 1.327 08-Apr-2017 deraadt

Two prototypes for acpi_maptable; merge to one place


Revision tags: OPENBSD_6_1_BASE
# 1.326 27-Mar-2017 deraadt

Now that hibernate_alloc() only has clean success/failure, don't
need to call hibernate_free() to clean up a partial mess.
ok mlarkin kettenis


# 1.325 13-Mar-2017 deraadt

allow ramdisks to build; ok jcs


# 1.324 12-Mar-2017 jcs

try to attach acpisbs first and if successful, don't attach acpibat


# 1.323 02-Mar-2017 natano

Add a new sysctl machdep.lidaction. The sysctl works as follows:

machdep.lidaction=0 # do nothing
machdep.lidaction=1 # suspend
machdep.lidaction=2 # hibernate

lidsuspend is just an alias for lidaction, so if you change one, the
other one will have the same value. The plan is to remove
machdep.lidsuspend eventually when people have upgraded their
/ets/sysctl.conf.

discussed with deraadt, who came up with the new MIB name
no objections mlarkin
ok stsp halex jcs


# 1.322 28-Feb-2017 natano

Untangle abstract sleep modes from ACPI sleep states. This paves the way
for further work in the area.

idea and ok deraadt
ok mlarkin


# 1.321 22-Feb-2017 jcs

add acpisbs, an acpi smart battery subsystem driver reading data
over smbus

currently disabled because it conflicts with acpibat

ok deraadt, kettenis


# 1.320 14-Jan-2017 kettenis

Revert aml_rdpciaddr changes; breaks several machines that were working
before.


# 1.319 11-Jan-2017 jsg

Increase the size of the buffers used for _HID and _CID strings to be
able to fit "Hyper_V_Gen_Counter_V1".

ok mlarkin@ deraadt@


# 1.318 08-Jan-2017 kettenis

Make aml_rdpciaddr() work for for devices behind bridges. The code now
uses the mapping between AML nodes and PCI devices that we establish.
Because _INIT methods may end up calling aml_rdpciaddr(), make sure we
create that mapping early. Also handle devices that aren't actually present.
These devices are now included in the mapping and reads will return an
all-ones patterm whereas writes are a no-op..

ok mlarkin@


# 1.317 25-Oct-2016 pirofti

Add the resource index to aml_parse_resource() callback function.

This is needed by an upcoming acpiec commit that handles machines breaking
the current ACPI specifications.

Change suggested by and ok kettenis@, guenther@


# 1.316 18-Sep-2016 guenther

If the APIC table (MADT) isn't present then ci_acpi_proc_id won't be set
when acpicpu tries to attach, leading to a NULL dereference. The ACPI
processor object on the Dells where this was reported is bogus and useless
anyway so apply the same test when deciding whether to attach to processor
objects as acpicpu would when matching them to cpus.

Problem reported by Eivind Eide (xenofil (at) gmail.com) and
Johan Huldtgren (johan+openbsd-bugs (at) huldtgren.com).
Testing by Eivind Eide.
ok kettenis@ mlarkin@


# 1.315 03-Sep-2016 naddy

Write the system time back to the RTC every 30 minutes.
This fixes the problem that long-running machines which were not
shut down properly would reboot with a badly offset system time.

hints and ok kettenis@


# 1.314 31-Aug-2016 mlarkin

Don't put a device in the wake device list if _STA says it's not there.

Fixes at least one machine that woke up from sleep / poweroff immediately.

ok kettenis@, tested by kettenis and martijn@


# 1.313 28-Jul-2016 kettenis

Store the acpi processor ID/UID in struct cpu_info, and use it to attach
acpicpu(4) drivers to the right cpu(4).

ok mlarkin@, guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.312 10-Jun-2016 kettenis

The power management timer has been made optional in ACPI 5.0A, so don't
attempt to attach acpitimer(4) if the timer isn't present.

Pointed out by jcs@

ok jcs@


# 1.311 08-May-2016 kettenis

Make sure devices listed by a device's _DEP method are attached before
we attach the device itself. The _DEP methode, introduced in ACPI 5.0,
is a hint that the device in question depends on OpRegion support from
the devices listed by _DEP, so we have to order them the proper way.
To prevent us from attaching those devices again when we encounter them
later walking down the device tree, keep track of our attempts to
attach a device driver to them using aflag in the aml_node structure.

ok guenther@


# 1.310 25-Apr-2016 pirofti

We attach acpihpet(4) and acpiec(4) without using the HID, so do not
report it in the dmesg.

OK deraadt@, mlarkin@.


# 1.309 02-Apr-2016 kettenis

Reduce dmesg spam by knocking out some acpi hardware IDs that don't need a
driver.

ok mpi@


# 1.308 29-Mar-2016 kettenis

Check if a device is present (using _STA) in acpi_foundhid. This prevents
us from reporting devices as "not configured" that aren't actually present,
and allows us to remove duplicated code from several drivers.

ok deraadt@, ok jsg@


# 1.307 29-Mar-2016 kettenis

Implement a cleaner way to prevent acpivout(4) from attaching if we have
a platform-specific method of controlling the backlight.

ok millert@, mlarkin@, jung@


# 1.306 28-Mar-2016 kettenis

Adding each and every new acpi device driver to acpi_foundhid() is
gettinga bit of the burden. And it really isn't how our autoconf
framework is supposed to work for busses that can be enumerated. All
the drivers already check for a matching _HID in their attach
function. So we can just drop the checks in acpi_foundhid() and let
the drivers sort themselves out.

Print a "not configured" line for devices for which we don't attach adriver.
This may be a bit much as there are devices for which we will not have a
driver. This will be removed if it causes too much dmesg spam.

ok deraadt@, mlarkin@


Revision tags: OPENBSD_5_9_BASE
# 1.305 17-Jan-2016 jsg

Take acpi_foundhid() outside of #ifndef SMALL_KERNEL so the ramdisk
kernel can find and attach dwiic(4) and sdhc(4).


# 1.304 17-Jan-2016 jsg

remove an unused global variable


# 1.303 14-Jan-2016 kettenis

Attach dwiic(4) on the Intel Bay Trail i2c controllers as well.


# 1.302 12-Jan-2016 kettenis

Improve emulation of PM registers on Hardware-redcued ACPI by emulating
SPL_TYPx, SLP_EN and WAK_STS using the new SLEEP_STATUS_REG and
SLEEP_CONTROL_REG registers. Makes the transition into S5 (powerdown) work
on the ASUS X205TA.

ok deraadt@, mlarkin@


# 1.301 12-Jan-2016 jcs

Add dwiic, a driver for the Synopsys DesignWare i2c controller found
on the Samsung ATIV Book 9 laptop. This initial version only
supports ACPI config/attachment.

Add ihidev, a HID-over-i2c driver largely based on uhidev. dwiic
handles attaching ihidev devices found in ACPI.

Add ims, a HID-over-i2c mouse/trackpad driver to get basic cursor
and button functionality from HID-compliant i2c trackpads.

ok kettenis deraadt


# 1.300 11-Jan-2016 kettenis

Make sdhc(4) attach to hardware IDs "80860F14" and "PNP0FFF".


# 1.299 09-Jan-2016 kettenis

Initial support for Hardware-Reduced ACPI. This skips the legacy mode checks
by emulating the PM_CNT register such that SCI_EN is always enabled. It also
skip attaching acpitimer(4) since the relevant registers are no longer there.

ok deraadt@, mlarkin@


# 1.298 06-Jan-2016 kettenis

Follow the spec and check the SCI_EN bit to see if ACPI has already
been enabled and don't bother enabling it again if it is. This should fix
systems that don't implement legacy mode and therefore set ACPI_ENABLE and
ACPI_DISABLE to zero.

ok mlarkin@, yasuoka@


# 1.297 23-Nov-2015 reyk

Back out the CRITICAL change, turn it back from 10% to 25%. Some
people with old/broken batteries didn't like this change, and the
underlying heuristics are wrong.


# 1.296 22-Nov-2015 reyk

The "CRITICAL" APM battery level, that is exported/faked by the ACPI
stack, is currently set to 25%. With 25% battery left, a typical
modern laptop will have at least one hour runtime left. It is an
arbitrary value, and other APM power layers in the kernel set CRITICAL
to something like 5% or 10%, so set it to 10% for now.

OK mlarkin@


# 1.295 28-Sep-2015 deraadt

In low-level suspend routines, set cold=2. In tsleep(), use this to
spit out a ddb trace to console. This should allow us to find suspend
or resume routines which break the rules. It depends on the console
output function being non-sleeping.... but that's another codepath which
should try to be safe when cold is set.
ok kettenis


# 1.294 06-Sep-2015 deraadt

clarify malloc arguments (base + length...)


# 1.293 28-Aug-2015 deraadt

fairly simple sizes for free(); ok tedu


# 1.292 20-Aug-2015 kettenis

Respect the access size when reading or writing to pci config space.
This version also makes sure writes are properly aligned, preventing
the panics seen with the previous incarnation of this change.

Fixes battery status passthrough in vmware, brightness keys on my x220
and who knows what else.

ok mlarkin@, deraadt@


Revision tags: OPENBSD_5_8_BASE
# 1.291 04-Aug-2015 deraadt

If we are setting ACPI_PM1_BM_RLD do it only once, and remember to
repeat this action at resume time.
ok mlarkin guenther


# 1.290 29-Jul-2015 deraadt

Something subtle in previous revision is locking up suspend or booting
on some laptops. Mailing lists threads mention x220 and Asus machines.
Reverting for now.


# 1.289 21-Jul-2015 kettenis

iRespect the access size when reading or writing to pci config space.
Fixes battery status passthrough in vmware and who knows what else.

Tested by many.

ok mlarkin@, deraadt@


# 1.288 17-Jul-2015 mlarkin

fix comments


# 1.287 26-Mar-2015 jsg

Use the 0x10 extended capability to determine if the trackstick
buttons are wired to the trackpad and need to be re-routed to the
trackstick. Without this change the buttons on 2015 thinkpads
get picked up as extended buttons that show up as scroll up/down.

Remove the X1 Carbon 2015 (LEN0048) and X250 (LEN0046) from the
top button area/soft buttons quirks list. Also avoid using the
quirk list entirely if the capability bit is set.

Based on work from jcs@ with feedback from mpi@
ok mpi@ jcs@


# 1.286 20-Mar-2015 kettenis

The ACPI standard says that a PCI function number of FFFF can be used to refer
to all functions on a device. This makes no sense in the context of accessing
PCI config space. Nevertheless stsp@ has a machine with AML that tries to
do config space reads from a device where _ADR encodes function number FFFF.
This is almost certainly buggy AML, but since Linux (and presumably Windows)
boot just fine on this machine we need to work around this. Linux effectively
treats function FFFF as function 7, which isn't present on this machine.
Instead we simulate a read from a non-existent device by returning a bit
pattern of all ones. Writes to function FFFF will still panic.

ok mlarkin@


# 1.285 16-Mar-2015 deraadt

Reduce use of SMALL_KERNEL in acpi, bringing acpiec driver and more gpe
handling into RAMDISK. This is now possible because the install media
has ample room. The goal is to reduce special cases where we may be
skipping (unknown) important operations...
ok mlarkin kettenis


# 1.284 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.283 07-Feb-2015 deraadt

New framework that allows hibernate to pass in entropy from it's fresh
boot.
ok mlarkin


# 1.282 06-Feb-2015 mlarkin

Fix a hibernate crash on some machines due to unmapping a page that
may not have been mapped previously (in the failure to hibernate case).

Also ensure that the lowmem ptp is mapped in all cases (not just MP).

ok kettenis


# 1.281 17-Jan-2015 deraadt

resume_randomness() before spinning up other cpus...


# 1.280 15-Jan-2015 jsg

Define a new wscons mouse type for Synaptics clickpad devices that lack
physical buttons. This will be used if the acpi pnpid for the mouse
matches a list provided by Synaptics found in Linux.

Combined with the xenocara changes this will remove the need for an xorg
config file for the x240/t440/t540 generation of thinkpads.

ok matthieu@ shadchin@ kettenis@


# 1.279 11-Jan-2015 kettenis

Tolerate checksum errors in ACPI tables (but complain about them). Apparently
there are machines out there with broken BIOSen with such checksum errors in
thier RSDT tables. Windows runs fine on these machines, so it probably does
not check. Linux ignores checksum errors as well (and prints a warning).

ok deraadt@, krw@


# 1.278 18-Dec-2014 deraadt

suspend and resume the RNG (at what looks like the right spots)
ok reyk djm


# 1.277 09-Dec-2014 doug

Replace some malloc(n*size,...) calls with mallocarray().

ok tedu@ deraadt@


# 1.276 06-Dec-2014 mpi

If "machdep.lidsuspend" is not set, do not go back to sleep if the lid
is still closed when resuming. This allows docked laptop to be resumed
without opening them.

ok mlarkin@


# 1.275 23-Nov-2014 mlarkin

If a machine has no _LIDs, then none can be open. This diff fixes a problem
where machines without any _LIDs were going back to sleep after hibernate
due to the recent suspend-if-lid-closed-on-resume diff. Noticed on qemu,
which doesn't have any _LIDs.

discussed with deraadt


# 1.274 18-Nov-2014 krw

Nuke yet more obvious #include duplications.

ok miod@


# 1.273 08-Nov-2014 mlarkin

If resuming from sleep (zzz/ZZZ) and the lid is still closed, go back to
sleep. This prevents accidental lid flex or slight opening in a backpack
from waking the machine up and leaving it resumed (powered on).

ok deraadt@


# 1.272 26-Sep-2014 kettenis

Rework piglet and pig allocation. Currently the piglet gets allocated
deep down in the suspend path, where it is really hard to recover from
allocation failure. So allocate the piglet early on in the suspend path.
Also change the piglet and piglet allocation functions to use km_alloc(9)
instead of doing pmemrange magic. This removes a bunch of code which, in the
case of the piglet allocation, is broken since it results in a NULL pointer
dereference. Also switch the piglet allocation to not wait. If we can't
allocate 16MB of phys contig memory on a halfway modern machine we're almost
certainly under a lot of memory pressure and we're better off not trying to
hibernate anyway.

ok mlarkin@


# 1.271 25-Sep-2014 kettenis

Bring back rev 1.249. Now that mp_setperf() has been fixed, it should be
safe to restore perflevel when resuming. Should fix the issues people
see with the processor running at full speed heating up the machine while
the apm command reports that the CPUs are running at a low speed.

ok dcoppa@, deraadt@


# 1.270 23-Sep-2014 kettenis

Release the acpi lock around calls to wsdisplay_suspend() and
wsdisplay_resume(). The X server may adjust the display brightness just
before/after the VT switch when suspending/resuming. In fact, it may
already be blocked adjusting the display brightness for some reason
unrelated to the suspend request.

The VT switch request will eventually time out, but the delay is annoying
and the failed VT switch will lead to problems after resume.

Needed for a future update of the xf86-video-intel driver.

ok mlarkin@


# 1.269 19-Sep-2014 kettenis

Use config_suspend_all(9).


# 1.268 08-Sep-2014 kettenis

Clear the PME Status bit on PCI devices in response to Device Wake events.
Fixes the interrupt storm on the 2009 Mac mini with WOL enabled on its
nfe(4) interface.

ok mlarkin@


Revision tags: OPENBSD_5_6_BASE
# 1.267 20-Jul-2014 mlarkin

Support hibernating to softraid crypto volumes.

much help and ok from deraadt@


# 1.266 16-Jul-2014 mlarkin

Reenable hibernate RLE support and flush+zero all memory after unpack.


# 1.265 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.264 12-Jul-2014 kettenis

Add a function to drop all clean pages on the page daemon queues and call
it when we hibernate.

ok mlarkin@, miod@, deraadt@


# 1.263 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.262 11-Jul-2014 mlarkin

Flush the buffercache to 16MB on hibernate and restore its previous max
size (kern.bufcachepercent) on resume, for better hibernate performance.

ok beck@


# 1.261 10-Jul-2014 blambert

remove unneeded header

ok mlarkin@


# 1.260 01-Jul-2014 kettenis

Adding an additonal delay after each access to SystemMemory or SystemIO
during autoconf makes no sense.

ok deraadt@


# 1.259 23-Jun-2014 kettenis

SystemMemory is used to access memory mapped registers on some machines, so we
must use the bus_space(9) API to access it instead of memcpy(9). Also make
sure we properly align access to fields that need more than byte alignment.

ok guenther@


# 1.258 21-May-2014 mlarkin

Fix invalid / missing format specifiers in several dnprintf statements
(used only when ACPI_DEBUG is enabled)

From a diff on tech@ by Fabian Raetz, with a minor change by me, tested
on i386 and amd64 with ACPI_DEBUG enabled and disabled.


# 1.257 25-Apr-2014 mlarkin

Defer the free of the hibernate structures until later, when it is safe to
do so.

Freeing the hibernate structures too early leads to inadvertent calls into
splx when we aren't ready for them yet. Found when trapping splx calls in
the resume path.

discussed with deraadt@ at m2k14


# 1.256 13-Apr-2014 mlarkin

Fix a badly worded debug message that implied the entire machine was going
to sleep, instead of just the acpi thread going to sleep.


# 1.255 31-Mar-2014 mpi

Change the order of operations during a suspend/resume cycle and
call bufq_quiesce() after executing the DVACT_QUIESCE handlers.

This should be safe since no disk nor controller drivers have such
handler but it will allow us to detach sd(4) devices attached to a
USB bus.

Another benefit pointed out by kettenis@ is that drivers that need
to read a firmware from the disk should be able to do it at resume
time in a DVACT_WAKEUP handler.

ok kettenis@, deraadt@


# 1.254 13-Mar-2014 dlg

get rid of the assumption that the head of the alldevs list is the
"mainbus" device. this breaks when mpath is enabled because it
attaches before mainbus and therefore takes the head position.

have autoconf provide device_mainbus() which looks up mainbus_cd,
and use that instead.

discussed with deraadt who just wants mpath stuff to move forward
despite there being many ways to shine this particular turd.


Revision tags: OPENBSD_5_5_BASE
# 1.253 21-Feb-2014 deraadt

Do the lid control further on in the resume sequence, since this is
AML, and will do WAITOK operations. While cold, that is a bad idea.
We seem to be safe with respect to the spec, I do not see any event
debounce issues here.
ok kettenis, tested by many


# 1.252 20-Jan-2014 claudio

Instead of checking sc->sc_revision <= 1 check that the
fadt->hdr_revision <= 1. This is a better check since it checks
the version of the proper descriptor. This allows the alix APU
to reboot since those boards have currently a wrong sc->sc_revision.
Committing this now so that it can be tested easier.
With and OK jsg@ also fine by derradt@


# 1.251 24-Dec-2013 kettenis

If the FADT has its SMI_CMD set to zero, assume we're only ACPI-only hardware
and don't need to disable SMI ownership of the ACPI hardware registers.

ok mlarkin@


# 1.250 23-Dec-2013 kettenis

Revert previous commit. It makes resume hang on some MP systems running
GENERIC.MP.


# 1.249 16-Dec-2013 kettenis

Restore hw.setperf upon resume like we do for apm(4) on i386. Seems we need
to do this fairly late in the resume to avoid hangs.

ok deraadt@, mlarkin@


# 1.248 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


# 1.247 06-Nov-2013 mpi

Add support for Power Resources for Dx states and the necessary hook
for PCI devices. This hook should be called twice, before and after
changing the power state of a PCI device.

Before setting the device to the new state, the ACPI layer will notify
every power resources linked to the device for that state and make sure
they are turned "_ON". After changing the state of the device, it will
decrement the reference of every power resources linked to that device
for the old state and turn them "_OFF" if they are no longer referenced.

This fixes the no-USB after resume problem seen on various ThinkPad,
problem initialy diagnosed with Alexander Polakov.

ok kettenis@, deraadt@


Revision tags: OPENBSD_5_4_BASE
# 1.246 01-Jun-2013 mlarkin

Stop printing ACPI wakeup devices in dmesg after the 16th wakeup device.

Helps with VMware VMs that report hundreds of wakeup devices.

ok reyk@, deraadt@


# 1.245 31-May-2013 bluhm

Fix the build for a kernel without wd and pciide in its config.
OK deraadt@ tedu@


# 1.244 30-May-2013 deraadt

Enforce ca_activate tree-walks over the entire heirarchy for all events,
cleaning up some shutdown-hook related code on the way.
(A few drivers related to sparc are still skipped at kettenis' request)
ok kettenis mlarkin, tested by many others too


# 1.243 18-Apr-2013 deraadt

pin the acpi thread to the BSP, because AML and SMI cannot be trusted. I
was certain we were doing this already, but it turns out no...


Revision tags: OPENBSD_5_3_BASE
# 1.242 24-Dec-2012 guenther

In acpiioctl(), Return though the splx() path if suser() returns an error

ok deraadt@


# 1.241 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


# 1.240 04-Oct-2012 ehrhardt

Use information provided by ACPI to attach secondary PCI host bridges.

ok kettenis@


# 1.239 07-Sep-2012 kettenis

Add a new function called acpi_pci_min_powerstate() that returns the lowest
power state for a PCI device for the power state (currently S3 or S4) we're
transitioning into. This function is probably too simplistic; there is a lot
of (silly) complexity in the ACPI spec.

ok deraadt@


Revision tags: OPENBSD_5_2_BASE
# 1.238 13-Jul-2012 pirofti

Disable acpivideo(4) on ASUS laptops.

There are some models that panic whenever an event is received.
Some cope, some don't. In order to get this right acpiec(4) requires
major rework. Until then let machines boot.

Discussed with deraadt@.


# 1.237 11-Jul-2012 mlarkin

for now, restrict hibernate to root in the acpi ioctl kernel interface.
normal rules for controlling this interface through apmd still apply.

ok deraadt@


# 1.236 09-Jul-2012 deraadt

tiny little oops in untested hibernate block


# 1.235 09-Jul-2012 deraadt

clean up ioctl() case statements


# 1.234 09-Jul-2012 deraadt

acpi_disable_onegpe() is not used; ok pirofti


# 1.233 24-May-2012 kettenis

If _MIN > _MAX consider a WordBus resource to be invalid. Fixes a panic on
the IBM x3550-M4.

ok mikeb@


# 1.232 29-Mar-2012 mlarkin

Missing #ifdef in previous diff


# 1.231 29-Mar-2012 mlarkin

Prevent hibernation attempts on machines with unsupported disk controllers.
The same check is also performed later in the hibernate sequence, but by
then it's too late to cleanly unwind (presently).

ok deraadt


# 1.230 26-Mar-2012 deraadt

simplify conditionals in acpi_sleep_state()
ok mlarkin


# 1.229 26-Mar-2012 deraadt

hook in the hibernate request code; half of this diff is from mlarkin
ok mlarkin


Revision tags: OPENBSD_5_1_BASE
# 1.228 20-Sep-2011 deraadt

Very early in suspend while the clock is still ticking realtime,
save the clock back to the rtc. The zaurus already did this.
ok phessler miod kettenis


Revision tags: OPENBSD_5_0_BASE
# 1.227 02-Jul-2011 nicm

kqueue attach functions should return an errno or 0, not a plain 1. Fix
the obvious cases to return EINVAL and ENXIO.

ok tedu deraadt


# 1.226 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.225 16-Jun-2011 pirofti

Import acpitoshiba driver.

This is originally written by Hiroyuki Aizu, ported and integrated
in our tree by Javier Vazquez <javama4 AT gmail> with tweaks and
suggestions by me.

Import prodded by deraadt@ and the lack of testers on tech@.


# 1.224 27-Apr-2011 jcs

attach acpithinkpad to newer lenovo models like the x120e

ok deraadt@


# 1.223 22-Apr-2011 jordan

Fix uninitialzied variables and formatting strings (-Wxxx errors)


Revision tags: OPENBSD_4_9_BASE
# 1.222 02-Jan-2011 jordan

Re-add code for ACPI taskqueue, eliminates need for state vars in acpi softc
Serialize processing ACPI tasks for sleep/powerdown/GPE event
ok deraadt@


# 1.221 31-Oct-2010 guenther

Revert last commit: it breaks resume on Thinkpads
"then please back it out" deraadt@


# 1.220 26-Oct-2010 jordan

Add task queue for ACPI gpe and notify handlers
This fixes eject on Dell Latitude dock and an issue on the Dell Mini
battery update.
ok deraadt


# 1.219 07-Oct-2010 jordan

Create separate functions for enabling wake and runtime gpes.
Remove old suspend/resume gpewalk function
ok deraadt, mlarkin


# 1.218 05-Oct-2010 jordan

split acpi_enable_onegpe to separate enable/disable functions
ok deraadt


# 1.217 05-Oct-2010 jordan

Added acpi_disable_allgpes function to clear GPE status
ok deraadt


# 1.216 05-Oct-2010 deraadt

clean up acpi_softc use more


# 1.215 05-Oct-2010 deraadt

delete a bit of #if 0 code


# 1.214 05-Oct-2010 deraadt

hide use of our global acpi_softc pointer a bit better


# 1.213 29-Sep-2010 deraadt

grab the acpi thread lock very early in acpi thread startup; not that it
is likely that anyone will race us... but why be careless.
ok kettenis


# 1.212 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.211 29-Aug-2010 jasper

- add a check for FWRITE here too.

ok miod@


# 1.210 27-Aug-2010 kettenis

Make PCI Power Management optional, and only enable it when acpi(4) attaches.

ok deraadt@


Revision tags: OPENBSD_4_8_BASE
# 1.209 08-Aug-2010 kettenis

Close race between the acpi thread and wscons ioctls running in process
context. We might rip this out post-release and replace it with a solution
that serializes things in a more obvious way.

ok deraadt@, miod@ and "those crazy texans who ok anything"


# 1.208 08-Aug-2010 deraadt

Report the acpi sleep states when on a RAMDISK; ok krw


# 1.207 07-Aug-2010 kettenis

Enforce acpi register access rescrictions.

ok mlarkin@, deraadt@, tested by marco@


# 1.206 07-Aug-2010 deraadt

upon resume, notify all the acpiac and acpibat drivers to update
their status. some laptops do not do this automatically.
ok kettenis canacar


# 1.205 06-Aug-2010 marco

Always call _PSW on _LID devices that support it to enable lid open to
resume the machine. Conversely disable it when the machine wakes up.

Tested by several ok deraadt


# 1.204 06-Aug-2010 deraadt

As part of a revamp of the PM1/GPE code, I write this diff to look for
unmanaged EN & STS bits in the PM1 register at interrupt time and report
them. As a side effect this splits the STS acknowledgement into two writes
(for power, and sleep) instead of one. The printf that is added (to spot
unmanaged STS bits) has not yet been triggered as far as we know.

Before the "write to PM1 registers at the right offset" diff went in, this
was not neccessary. But newer thinkpads do not have a working soft power
button without this diff.

We have no idea why.
ok mlarkin kettenis


# 1.203 05-Aug-2010 deraadt

PM1 power button events were generating 'fake events' to acpibtn PWRB,
but if there is no such device these were being tossed. Instead, arrange
for all power button handlers to flag a softc variable which is processed
by the thread; so that one place will know to request a powerdown (psignal
to init)
ok marco mlarkin


# 1.202 05-Aug-2010 deraadt

rename a variable which is annoyingly poorly named


# 1.201 05-Aug-2010 mlarkin

Write to the correct offset for various ACPI pmreg registers. Writing to
the correct register does properly clear the wake status bits and fixed
event bits, which allows some machines to have more than 1 suspend cycle.

bug spotted by deraadt@

tested by deraadt@ and myself on a half-dozen machines and others on
hackers@

ok deraadt@, marco@


# 1.200 05-Aug-2010 deraadt

Fix the power button. The pm1 register is 16 bits in size.
My mistake, a while back.
tested by krw


# 1.199 04-Aug-2010 kettenis

Print supported sleep states.

ok marco@, phessler@, deraadt@


# 1.198 03-Aug-2010 mlarkin

Use the proper flag for re-enabling certain hardware events (power btn,
sleep btn). The (incorrect) flag prevoiously used coincidentally had the
same value, so this shouldn't cause any different behavior than before.

tested on a variety of machines (i386, amd64, sp, mp) - no behavioral
change seen

ok deraadt@


# 1.197 28-Jul-2010 marco

Make all gasio failures look and behave the same.


# 1.196 28-Jul-2010 mlarkin

Delay a little longer on suspend before giving up. Fixes machines that
sometimes don't suspend because the sleep transition takes too long
(symptoms like failing to sleep but the backlight stays on).

ok deraadt@


# 1.195 27-Jul-2010 deraadt

remove XXX; EC can now be attached early when needed; ok jordan


# 1.194 27-Jul-2010 deraadt

more brackets feels good


# 1.193 27-Jul-2010 deraadt

XXX annotate a block in acpi_enter_sleep_state() that is pretty worrying


# 1.192 27-Jul-2010 deraadt

horrific indentation that nearly made me puke


# 1.191 27-Jul-2010 mlarkin

Set the sleeping indicator light on machines that support it, in case SMI
doesn't do it for us. Shows the 'moon' LED on older Thinkpads when
sleeping.

Tested on T43p, W500, T510 by me and on X61s by deraadt@.

ok deraadt@


# 1.190 27-Jul-2010 jordan

Early initialization of acpiec if ECDT table exists
Fixes hang when booting thinkpads while docked
ok deraadt


# 1.189 26-Jul-2010 deraadt

remove merge error which was commited


# 1.188 26-Jul-2010 pirofti

Add support for Sony ACPI hotkeys via a new driver: acpisony(4).

Currently it only works for the suspend button and tries to do right for
the brightness events, but I haven't found a Sony laptop that like to do
right so far.

In the future I want to make the brightness keys work on all Sony's and
also add support for the zoom hotkeys and whatever other funky keys I can
find on those things.

Okay deraadt@.


# 1.187 22-Jul-2010 deraadt

pretty large cleanup of SMALL_KERNEL


# 1.186 21-Jul-2010 deraadt

in the acpi GPE handler, late ack level interrupts not edge interrupts.
Duh.


# 1.185 21-Jul-2010 deraadt

Merge the edge and level gpe handlers into one (now possible since the
gpe structure tells us whether the gpe is edge or level)


# 1.184 21-Jul-2010 deraadt

delete #if 0 code we are not going to use; ok marco


# 1.183 20-Jul-2010 deraadt

make the mask code for acpi_enable_onegpe() match other similar code


# 1.182 20-Jul-2010 deraadt

Handle the acpi interrupt controller with proper edge/level handling,
wrapped in spltty to avoid racing against the interrupt controller.
Repair the gpe bit masking code while there, and do operations in the
right order
ok kettenis mlarkin, help from jordan at figuring out the order of
operations.


# 1.181 20-Jul-2010 matthew

When we're preparing to go to sleep, only call bufq_restart() if other
preparations failed.

ok kettenis@, thib@, deraadt@


# 1.180 19-Jul-2010 deraadt

permit compilation with ACPI_DEBUG


# 1.179 19-Jul-2010 deraadt

there is no need for a seperate dsdt_softc pointer; dsdt does not have a
softc but is just a part of the single acpi driver.
ok jordan


# 1.178 19-Jul-2010 deraadt

acpi_delay() is no longer needed; ok jordan


# 1.177 18-Jul-2010 mlarkin

Clear event status on resume, to avoid going back to sleep/reboot/shutdown
immediately after resume. Tested by a bunch of people on hackers@ on
various hardware. Fixes at least Toshiba NB200 and Gateway LT3103u.

ok marco


# 1.176 13-Jul-2010 deraadt

rename some thread related interfaces so they are logical


# 1.175 10-Jul-2010 jordan

Verify _ADR bdf is correct (some systems return 0xffff)
Works on Dell x9xx and Studio 1555


# 1.174 09-Jul-2010 jordan

Clear the noise


# 1.173 08-Jul-2010 jordan

Add mapping for ACPI device to PCI bdf (match autoconf tree)
Simplify resource parsing function to use buffer argument
Convert namespace linked lists to use queue macros
ok marco@, deraadt@


# 1.172 06-Jul-2010 deraadt

Use spltty() to lock downcalls from apm against the information being
modified by the acpi thread.
ok kettenis


# 1.171 06-Jul-2010 deraadt

Don't use a workq to do the suspend, because it races aginst the acpi
thread. Instead, just tell the acpi thread to do the suspend for us.
This makes apmd & zzz work correctly. While here, have acpithinkpad
attempt to post the event to apm before prompting the suspend itself.
ok kettenis marco mlarkin


# 1.170 05-Jul-2010 mlarkin

Reset register SCI_EN on resume. The ACPI spec clearly states we don't have
to do this, but many machines don't, or forget to, which leads to all sorts
of problems on resume (broken battery stats, overheating, failure to resume
more than once, etc).

tested by many, ok marco@, deraadt@


# 1.169 01-Jul-2010 thib

do bufq quiesce again, since vnd/the bufq_done call in
biodone has been fixed.


# 1.168 01-Jul-2010 jordan

Backout recent AML changes, breaks on R210 and others


# 1.167 01-Jul-2010 jordan

Simplified aml_parse_resource call, cleanup code
ok marco


# 1.166 01-Jul-2010 jordan

Fix subordinate bus # for multi-root PCI buses
ok marco, kettenis


# 1.165 30-Jun-2010 thib

Disable/partially backout the bufq quiesce changes as this
is causing havoc with vnds and release must be buildable.


# 1.164 29-Jun-2010 deraadt

Fix ramdisks (new code was placed inside a SMALL_KERNEL chunk)


# 1.163 29-Jun-2010 jordan

Add support for mapping ACPI to PCI devices
ok kettenis, deraadt


# 1.162 29-Jun-2010 kettenis

Call bufq_quiesce() on suspend to stop all I/O and bufq_restart on resume.
Makes suspend/resume work much better while doing disk I/O.

ok mlarkin@


# 1.161 29-Jun-2010 tedu

60 is a more reasonable guess for number of minutes in an hour than 100.
spotted by TeXitoi. ok deraadt


# 1.160 27-Jun-2010 jordan

Add _acpi_matchhids routine, precursor to pci device match


# 1.159 27-Jun-2010 mlarkin

Clear ACPI fixed event status on resume (power buttons/etc) so that some
machines don't immediately resume after suspending to S3.

Tested by kettenis@ and I on 5 or 6 machines, deraadt@ "yeah, looks good"


# 1.158 27-Jun-2010 jordan

Cleaned up acpi_maptable, adds to table list directly


# 1.157 07-Apr-2010 deraadt

If we have done a wsdisplay_suspend(), and ACPI decides it does not want to
suspend, we must roll back by calling wsdisplay_resume() or the virtual
console switching will be locked forever
ok oga


# 1.156 07-Apr-2010 kettenis

Make suspend/resume work on MP machines (running an MP kernel). Joint work
from mlarkin@ and me, with some amd64 fixes thrown in by deraadt@

ok marco@, deraadt@, pirofti@, mlarkin@


# 1.155 31-Mar-2010 kettenis

Bring /dev/apm support in line with apm(4). Make the suspend button send
a suspend request event to apmd(8) instead of suspending immediately. Also
keep track of whether /dev/apm and /dev/apmctl are currently open, such that
we can still suspend immediately if apmd(8) isn't running.

ok deraadt@, marco@, pirofti@, jsing@, oga@


# 1.154 30-Mar-2010 oga

Prevent the apmd/x races for good.

When we hit suspend time, go through all wsdisplays on the system. if
they are in mode MAPPED, but not MODE_DUMBFB then if possible do a full
vt switch to a !mapped vt, and prevent switching back until resume time.
This has to be called from MD code because this involves userland
running so that X can run the vt switch signal handler. This way, any
case where we are using the "poke registers from userland" model, we
will not be on the hardware when we go down, so the kernel can actually
handle thing properly.

Tested on several acpi laptops (by kettenis@ and ian@), x40 (me and
beck@ at LEAST) and zaurus (me). Maybe others, but if so I forgot who at
this time..

Idea from deraadt somewhere over the Faroe Islands (I thought of a
similar thing myself a while ago). Much prompting from him. Ok and
comments miod@


# 1.153 25-Mar-2010 oga

Fix & vs. && in the kqfilter code.

Based on a comparison with the apm code.

ok deraadt@, kettenis@


Revision tags: OPENBSD_4_7_BASE
# 1.152 26-Nov-2009 mlarkin

Visual (and on some machines, audio) feedback while the machine is
resuming via calls to aml _SST method. This also makes the "moon" LED on
thinkpads to blink during resume and cease when resume is completed.

ok pirofti@


# 1.151 26-Nov-2009 deraadt

Toggle into "cold" when we we disable interrupts, because tsleeping
in the AML interpreter and context switching into userland processes
tends to sometimes pop the ACPI trampoline page out of our address
space. Makes lots more machines work.
ok kettenis mlarkin


# 1.150 24-Nov-2009 jsg

in the resume path evaluate _WAK before dealing with device drivers
like the spec says, not after.

ok mlarkin@


# 1.149 24-Nov-2009 deraadt

abort suspends on SMP systems until that gets written; ok mlarkin


# 1.148 23-Nov-2009 mlarkin

pci should return the result of it's children's suspend/resume calls back
to its parent.

handle suspend failure case and unwind if devices have an activate function
that returns failure.

ok deraadt@, kettenis@


# 1.147 23-Nov-2009 mlarkin

acpi* devices need to call their children on suspend/resume.
ok deraadt@


# 1.146 23-Nov-2009 pirofti

Remove ACPI_SLEEP_ENABLED checks.

This enables by default the suspend/resume paths in the kernel.

Okay deraadt@.


# 1.145 23-Nov-2009 deraadt

knf and spacing that are super annoying


# 1.144 23-Nov-2009 mlarkin

Hook up Theo's new children suspend/resume function, as well as more ACPI
suspend/resume glue.

ok deraadt@


# 1.143 26-Oct-2009 deraadt

Do not do strncmp comparisons on dv_xname because longer device names which
look similar could arrive in the future. Instead, compare directly against
dv_cfdata->cf_driver->cd_name
Issue originally spotted by miod


# 1.142 26-Oct-2009 jordan

Verify checksum+address when loading ACPI tables.
Some systems had invalid entries in RSDT/XSDT.
ok marco@


# 1.141 23-Jul-2009 cnst

New aibs(4) driver for ASUSTeK AI Booster (ACPI ATK0110) hardware monitoring,
with sensor state support through limits provided by the ACPI.

Tested on several ASUS motherboards kindly networked by Sam Fourman Jr.

ok deraadt marco jordan


Revision tags: OPENBSD_4_6_BASE
# 1.140 03-Jun-2009 pirofti

Import acpi power resource driver. This is needed for suspend/resume logic.

Okay jordan@ and marco@.


# 1.139 03-Jun-2009 pirofti

Add acpivideo support. This adds brightness support for all laptops
except thinkpads, they will use the acpithinkpad driver. The driver is
also hooked into wsconsole. So brightness can be adjusted via:

$ wsconsctl display.brightness=<percentage>

This is very helpfull on some laptops that have a nasty bios and get two
steps instead of one when pressing the brightness button.

Tested on various dell, fujitsu, acer, samsung and other laptops.

Okay marco@, miod@. Suggestions from kettenis@.
Lots of reviews and help from miod@, thanks!


# 1.138 03-Jun-2009 jordan

Added ejectable bay for IDE cdroms
ok marco@


# 1.137 30-Apr-2009 marco

Oga changed apm ioctls and forgot to grep. Temporary work around by hitting
the sleep button when we get any request. This interface will chnage at
some point.
ok oga


# 1.136 19-Apr-2009 krw

Call acpi_sleep_walk() when powering down just like when entering
any other sleep state. This tells acpi wake devices to not wake up
the box until manual powering up resets their status. Makes my Dell
GX520 stay powered off in response to 'halt -p'.

In snaps for a while.

ok marco@ (several times) deraadt@


# 1.135 17-Apr-2009 pirofti

Add device ranking support for future sleep/wake developments.

okay marco@


# 1.134 11-Mar-2009 jordan

Cleanup.. use aml_evalinteger instead of aml_evalname
ok marco@


# 1.133 10-Mar-2009 jordan

Fix cut/paste error.. call _BFS not _PTS
ok marco@


Revision tags: OPENBSD_4_5_BASE
# 1.132 21-Feb-2009 marco

overzealous cut/paste; found by Vladimir Kirillov <proger@uaoug.org.ua>


# 1.131 20-Feb-2009 marco

Oops didn't mean for Debugger() to remain there.


# 1.130 19-Feb-2009 marco

suspend/resume bits so that we can develop this in tree. This is disabled.
code from mlarkin and me
help from art,toby,jordan and several others
ok jordan, go for it deraadt


# 1.129 10-Feb-2009 jordan

Fix null pointer for shutdown if acpi not enabled
ok marco@


# 1.128 04-Feb-2009 kettenis

Some systems that claim to be ACPI 2.0 apparently don't provide an XSDT
address. Make the code fall back on the RSDT on those systems.

From Joerg Niendorf <p3y80@internode.on.net>. Fixes PR 5986.


# 1.127 17-Jan-2009 krw

A couple of stray malloc/memset -> malloc(M_ZERO), a bit of knf and
lose a couple of unnecessary casts. No functional change.

Feedback from kettenis@

ok kettenis@ marco@


# 1.126 06-Nov-2008 marco

Collapse all sanity checks of hids into one function.

Originaly from jordan


Revision tags: OPENBSD_4_4_BASE
# 1.125 02-Jul-2008 fgsch

add acpivideo. for now almost an empty skel so others can work on
suspend/resume. eventually it will also manage output switching and
brightness where supported. prodded by marco@


# 1.124 11-Jun-2008 marco

Fix $OpenBSD$ for once and for all


# 1.123 08-Jun-2008 deraadt

aaa_dev was being set from an uninitialized variable; ok toby


# 1.122 07-Jun-2008 marco

Kill ACPIVERBOSE. It was never used and is worthless to boot.

comments jmc
ok toby


# 1.121 06-Jun-2008 marco

Shrink acpi some more for floppies and while here re-enable acpi on the
floppies. Fixes crazy sony vaio kettenis has.

tested by deraadt
ok deraadt jordan kettenis


# 1.120 01-Jun-2008 marco

Fix \n nit in SMALL_KERNEL.


# 1.119 01-Jun-2008 marco

Re-enable new parser. Lots of testing by various folks. Fits all media
now too.

ok kettenis deraadt


# 1.118 16-May-2008 dlg

back out jordans new parser.

it didnt have enough oks from the right people, it did break machines
that were previously working, and it bloated the RAMDISKS and install
media beyond the constraints of the media.

requested by deraadt@


# 1.117 14-May-2008 jordan

Adding new ACPI Parser code
Old guts of code still exists, needs to be torn out next
ok marco@
ok brad@


# 1.116 27-Apr-2008 jcs

add acpithinkpad(4), an acpi support driver for ibm/lenovo
thinkpads.

provides functionality that the bios did on older, non-acpi
thinkpads like respond to screen brightness keys and toggle the
bluetooth device on and off.

ok deraadt@ and marco@


# 1.115 24-Apr-2008 jsing

Introduce acpiasus(4), a driver for the ACPI based hotkeys found in many
ASUS laptops (including the ASUS EeePC) - largely based on NetBSD's
asus(4) driver. On the ASUS EeePC this allows us to enable/disable
wireless, change screen brightness and use the volume keys.

ok jsg@, weingart@


# 1.114 12-Apr-2008 kettenis

Get rid of acpi_s5 global variables; simply send SIGUSR2 instead of SUGUSR1
to tell init(8) to power down the machine.

ok krw@


Revision tags: OPENBSD_4_3_BASE
# 1.113 21-Jan-2008 jsg

Check against RESET_REG_SUP in FADT before doing the acpi reset dance
after all, not doing so appears to break more machines than it fixes.

ok marco@


# 1.112 05-Jan-2008 kettenis

Never attach more acpicpu(4) devices than cpu(4) devices. Always attach
the first acpicpu(4) device on non-MULTIPROCESSOR kernels, regardless of its
ACPI processor ID.

ok mikeb@


# 1.111 05-Dec-2007 deraadt

Hang acpi(4) below bios(4) instead of mainbus(4). This lets us move acpi(4)
before pcibios(4), and then.... if acpi(4) attaches, skip pcibios(4) since
it messes the machines up. Fixes claudio's HP dl320 G5, and almost assuredly
others.
ok kettenis


# 1.110 05-Dec-2007 deraadt

repair acpiopen/acpiclose and friends to handle the /dev/apmctl interface,
so that apmd(8) can actually work correctly against acpi(4), and thus,
even do the fancy -C/-A stuff; also tested by jmc


# 1.109 02-Dec-2007 fgsch

fix the trailing space for real now.


# 1.108 02-Dec-2007 fgsch

avoid trailing spaces on output; marco@ ok.


# 1.107 27-Nov-2007 kettenis

Fix off-by-one that made us attach acpicpu0 and acpicpu1 on GENERIC kernels.

ok marco@


# 1.106 25-Nov-2007 jsg

When available and ACPI is enabled use the ACPI reset method
to reboot. This is required for some quirky sis machines
and likely others to reboot.

Tested by many. 'commit early commit often' deraadt@


# 1.105 19-Nov-2007 deraadt

sigh, amd64 still uses that stupid acpi_enabled variable...


# 1.104 19-Nov-2007 deraadt

softc's come initialized to 0, you know...


# 1.103 19-Nov-2007 deraadt

store revision; from jsg


# 1.102 19-Nov-2007 kettenis

Initialize sc_facs earlier.

ok deraadt@, beck@, canacar@


# 1.101 16-Nov-2007 deraadt

fix the bus_space #define nightmare, so that amd64 and i386 are much more
uniform. as a result shared code like acpi needs less #ifdef's
ok marco kettenis


# 1.100 15-Nov-2007 deraadt

remove the #ifdef ACPI_ENABLE. the EC codepath is either safe enough (and
will be improved), or it is unsafe because AML assumes it must be there
ok almost everyone


# 1.99 15-Nov-2007 mikeb

Make acpicpu(4) attach to the existing CPUs only.

Problem reported by form@. Fix idea by kettenis@.
Lots of help from deraadt@. Tested by deraadt@, form@ and me.

ok deraadt marco


# 1.98 14-Nov-2007 deraadt

spacing


# 1.97 12-Nov-2007 deraadt

correct : vs , in attach-time messages


# 1.96 06-Nov-2007 deraadt

all ACPI_ENABLE kernels need acpi_interrupt()


# 1.95 06-Nov-2007 deraadt

move acpi_read_pmreg() and acpi_write_pmreg() out of SMALL_KERNEL so
that acpi ramdisks can compile (obvious solution)


# 1.94 05-Nov-2007 canacar

Establish the acpi interrupt earlier. Fixes problems with some
GENERIC.MP kernels not getting acpi interrupts. This is a
workaround until the real issue with late establishment is fixed.
ok weingart@, beck@, gwk@


# 1.93 03-Nov-2007 jordan

Added support for displaying wakeup devices
ok beck@,weingart@,gwk@


# 1.92 11-Oct-2007 kettenis

When walking the tree yo find _PRT methods, check whether the device is
actually enabled and functioning. Similar to what we do for _INI.

ok weingart@, beck@


# 1.91 08-Oct-2007 krw

More simple memset(,0,) -> M_ZERO changes. In this batch move to
size(*p) as the first malloc() parameter where p is declared locally
and thus easy to check. Add M_ZERO to gpe_table allocation in acpi.c
even though there is no obvious bzero or memset nearby.


# 1.90 13-Sep-2007 mk

Remove unnecessary memset() (see aml_evalnode(9)) and a bit of KNF.

ok toby


# 1.89 13-Sep-2007 weingart

Implement ACPI 6.5.1 spec tree walk for _STA and _INI.
Fixes mk's laptop. No regressions so far (thank you to the
testers).

ok gwk@, mk@, marco@


Revision tags: OPENBSD_4_2_BASE
# 1.88 17-Apr-2007 mk

Now that acpidock no longer hangs machines during boot (at least
according to the reports I've been getting) it should be safe to enable
it again, so remove the #if 0 stuff around it.

ok marco


# 1.87 11-Apr-2007 jordan

Added changes for C-State
ok marco@


# 1.86 26-Mar-2007 marco

Calculate minutes left more accurately.

From giovanni <qgiovanni@gmail.com>


Revision tags: OPENBSD_4_1_BASE
# 1.85 03-Mar-2007 mk

Stop looking for acpidock until we fix that stuff that breaks all pre-60
series ThinkPads with acpidock(4) enabled.


# 1.84 22-Feb-2007 marco

We were getting no interrupts in .MP. There are 2 fixes; one in
acpi_machdep.c and move the interrupt establishment down after ioapic
has been initialized.

ok jordan


# 1.83 21-Feb-2007 kettenis

Make an acpi-enabled GENERIC.MP work on systems that only support PIC mode.

ok marco@


# 1.82 19-Feb-2007 jordan

Added acpi_map_address function for mapping GAS/IO registers
ok marco@


# 1.81 18-Feb-2007 jordan

Added dynamic perfect hash for opcodes
ok marco@


# 1.80 18-Feb-2007 marco

Shave off another K pre compression.
Shuffle SMALL_KERNEL functions around so that we only have a single #ifndef


# 1.79 17-Feb-2007 marco

Add SMALL_KERNEL throught acpi to make it fit on boot media.
ok deraadt


# 1.78 13-Feb-2007 marco

Add smarter _INI code. This helps with some dissapearing devices.

ok kettenis jordan


# 1.77 31-Jan-2007 gwk

Introduce acpi_hasprocfvs thats "ACPI Has Processor Frequency and Voltage
Scalling" a global flag set by acicpu attach to indicate that ACPI is
handling fvs through the _PSS objects. This will fix disappearing
EST/powernow when booting kernels with acpi enabled, as acpicpu isnt
compiled into GENERIC yet. Also in cases where acpicpu is built in and
legacy EST/powernow work but acpi is missing the _PSS object, the legacy
routines will be used.

tested by dim@, and Nick Nauwelaerts <nick AT nauwelaerts.net>
ok marco@, dim@


# 1.76 22-Jan-2007 mk

Actually look for docking stations by looking for _DCK nodes.

ok marco


# 1.75 02-Jan-2007 marco

On some systems EC must be attached as the first device. Move EC from
generic _HID devices to its own routine to force first attach. Bug found
and debugged by joris@

Code mostly written by joris@


# 1.74 26-Dec-2006 marco

Add polling to devices that require it such as acpibat and acpitz.
Use the same tsleep/wakeup mechanism as the interrupt code to keep it non-
concurrent.


# 1.73 21-Dec-2006 deraadt

even more knf love


# 1.72 21-Dec-2006 marco

Remove some leftover acpiec gpe/interrupt code.


# 1.71 20-Dec-2006 deraadt

oops, name already used


# 1.70 20-Dec-2006 deraadt

rename __acpi_enable_gpe(); ok marco


# 1.69 20-Dec-2006 deraadt

acpi_unmap_pmregs() is not used, ok marco


# 1.68 19-Dec-2006 deraadt

simplify some low-level methods substantially (shrinking code); ok marco jordan


# 1.67 06-Dec-2006 mk

malloc(..., M_NOWAIT) with no NULL check is bad. If allocation fails,
complain, clean up, and bail out of attach.

input and ok gwk


# 1.66 28-Nov-2006 jordan

Fixed clearing arg for acpi_init_pic
ok kettenis@


# 1.65 27-Nov-2006 kettenis

Don't spam dmesg.


# 1.64 27-Nov-2006 jordan

Fixes for acpiprt routing
Post-parse fixup for namerefs
ok kettenis@


# 1.63 25-Nov-2006 marco

Don't handle acpiev events inside the gpe handler since this causes deadlocks.

ok jordan@


# 1.62 15-Nov-2006 kettenis

Hook up acpi apic interrupt routing support to generic acpi code.

ok jordan@


# 1.61 02-Nov-2006 marco

Kill acpiec attach function and add its functionality to the generic _HID
device attach

prodded and ok jordan


# 1.60 02-Nov-2006 marco

Fix comments to reflect reality.


# 1.59 19-Oct-2006 marco

Replace ACPI ioctls with APM ioctls. This makes ACPI talk the same protocol
as APM therefore userspace can now retrieve power information in the same
fashion as all other apm arches (zaurus, macppc, i386 etc).

To use force the use of acpi device like so: apmd -f /dev/acpi

ok jordan@


# 1.58 19-Oct-2006 jordan

Fixed GPE debug levels
Fixed ref counter on temporary bufferfields
DSDT parsing fully working on IBM Thinkpad
ok marco@


# 1.57 15-Oct-2006 jordan

Removed old GPE handler code remnants
ok marco@


# 1.56 12-Oct-2006 jordan

Massive ACPI changes
Fixed ACPI leaks
new ACPI API
ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.55 30-Jun-2006 gwk

KNF, No functional change,
"go at it" marco@


# 1.54 30-Jun-2006 jordan

Support for calling Embedded Controller via new GPE interface
ok marco@


# 1.53 30-Jun-2006 jordan

Added new code for GPE handling
Also works with ACPIEC; need to fixup acpiec.c to remove original interrupt handler
ok marco@


# 1.52 30-Jun-2006 jordan

Added offset argument for acpi read/write pmreg routines (new GPE code)
ok marco@


# 1.51 31-May-2006 todd

do not call acpi_powerdown() unless acpi has successfully initialized
ok gwk@ canacar@


# 1.50 29-May-2006 canacar

Add embedded controller support.
ok marco@ deraadt@ testing & ok gwk@


# 1.49 19-May-2006 canacar

Minimal thermal zone driver. Monitors thermal zone temperature,
shuts down the system if the 'critical temperature' is reached.
ok marco@


# 1.48 11-Apr-2006 marco

KNF.


# 1.47 11-Apr-2006 gwk

Add acpi_resume(struct acpi_softc *sc), which calls some AML methods,
detected at attach time, currently unused.

ok marco@


# 1.46 11-Apr-2006 gwk

Discover some ACPI sleep related methods, and call them at the
appropriate time in acpi_enter_sleep_state().

ok marco@


Revision tags: OPENBSD_3_9_BASE
# 1.45 26-Feb-2006 marco

Add acpicpu as a device.

ok jordan@


# 1.44 23-Feb-2006 marco

remove dead function.

ok jordan@


# 1.43 22-Feb-2006 marco

Print out all tables it found during autoconf. We'll need this data in
the future.


# 1.42 22-Feb-2006 marco

Make dmesg less verbose.

ok jordan.


# 1.41 22-Feb-2006 jordan

Added parsing of SSDT tables
ok marco@


# 1.40 22-Feb-2006 jordan

Added new aml_register to notify by device id
Moved powerdown code to button device
ok marco@


# 1.39 21-Feb-2006 marco

Readjust debug levels that crept in too low over the last few days.
KNF


# 1.38 21-Feb-2006 marco

Rig the power button so that the machine shuts down correctly.


# 1.37 20-Feb-2006 marco

Add acpibtn device. Remains disabled for now.


# 1.36 19-Feb-2006 jordan

Added code for aml_notify GPE notification callback
ok marco@


# 1.35 19-Feb-2006 grange

Fix acpi_enter_sleep_state() by replacing current ugly hack
with mostly correct sleeping state transition code.

ok brad@ marco@


# 1.34 19-Feb-2006 marco

jordan:
Enable interrupts.
Added peripheral code to make this work.

marco:
Replace softintr and other types of interrupts with a handler with context.
Fixed interrupt magic for GPEs.
Some KNF

This is work done by jordan and I but has to be a single commit.


# 1.33 17-Feb-2006 marco

Remove earlier introduced delays. We really only need them during autconf.

ok jordan@


# 1.32 17-Feb-2006 marco

Add a generic delay function as per the ACPI spec.

from jordan@


# 1.31 17-Feb-2006 marco

Use tsleep instead of delay when !cold.


# 1.30 17-Feb-2006 marco

Argh, we need delays between reads/writes. The backend reads (i2c most likely)
are so slow that values come back as 0 sometimes. We have to do this
despite the spec saying we shouldnt have to. Guess having a faster
parser than others can hurt sometimes.

Idea from jordan.


# 1.29 17-Feb-2006 jordan

Fixed code for handling _INI
Rearranged debug statements
ok marco@


# 1.28 16-Feb-2006 jordan

Added stub for calling _INI
Fixed aml_dowhile for NULL return
ok marco@


# 1.27 16-Feb-2006 jordan

Fixed parser/evaluator to retrieve battery info+state
Cleanup of parser
Dell laptops return battery status
ok marco@


# 1.26 06-Feb-2006 brad

fix a typo so this will compile on amd64.


# 1.25 04-Feb-2006 marco

When ACPI_ENABLED wasn't set but the devices were it had an unknown variable.

Pointed out by ian@.


# 1.24 03-Feb-2006 jordan

Rewrote aml parser yet again...
Fixed bus_space_map for i386/amd64
Added _BST and _BIF to acpibat to get status/info
ok marco@


# 1.23 20-Jan-2006 jordan

More evaluator changes
Removed old evaluator code
Fixed _HID value
ok marco@


# 1.22 19-Jan-2006 jordan

Added support for read/write of PCI Config space
Help jason@
ok marco@


# 1.21 18-Jan-2006 jordan

Added new evaluation routines
Added helper methods for buffer/field evaluation
Fixed broken indentation on dsdt.c

ok marco@


# 1.20 17-Jan-2006 jordan

Major overhaul of the aml parser. Evaluater temporarily disabled.

ok marco@


# 1.19 06-Jan-2006 grange

Check for ACPI control availability only if ACPI_ENABLE defined.


# 1.18 06-Jan-2006 grange

Informative error messages on attach, prettier dmesg.


# 1.17 06-Jan-2006 grange

printf -> dnprintf conversion for debug messages.


# 1.16 05-Jan-2006 grange

Find SLP_TYPx values and store them for future use in
acpi_enter_sleep_state().


# 1.15 05-Jan-2006 grange

Random KNF and cleanup


# 1.14 05-Jan-2006 grange

Provide symbolic names for the acpi sleeping states and use it.


# 1.13 28-Dec-2005 marco

Moved DEVNAME to a acpidev.h

ok jordan@


# 1.12 18-Dec-2005 sturm

add back comment about wrong code

ok marco


# 1.11 16-Dec-2005 marco

Remove test for kbd_reset. Prompted deraadt@, ok jordan@


# 1.10 16-Dec-2005 jordan

Added hooks for powerdown/power button press


# 1.9 16-Dec-2005 jordan

Cleaned up common power management register i/o
Added enter sleep state ioctl
Hooked power button interrupt
ok marco@


# 1.8 16-Dec-2005 marco

Remove autoconf chatter


# 1.7 16-Dec-2005 jordan

Moved DSDT AML parsing into parent ACPI driver
ok marco@


# 1.6 14-Dec-2005 marco

Add AC device glue.


# 1.5 14-Dec-2005 marco

Adjust debug printing levels to something more sane.


# 1.4 13-Dec-2005 marco

Add glue to attach devices to apci.


# 1.3 07-Dec-2005 marco

KNF


Revision tags: OPENBSD_3_8_BASE
# 1.2 10-Jul-2005 grange

Spacing cleanup.


# 1.1 02-Jun-2005 tholo

Start on a basic ACPI framework -- does not do much more than read out the
ACPI tables into kernel memory and attach ACPI and HPET timers currently.

In order to test this code, enabling the devices in GENERIC as well as
the ACPI_ENABLE option is needed. This code does not do any thermal
control yet, so this should be done with care depending on the platform.

In the tree so more people can contribute to making this more fully
featured.

Ok niklas@ grange@ tedu@


# 1.368 07-Jun-2019 kettenis

Introduce acpi_getsta() and use it to eliminate some duplicated code.

ok patrick@


# 1.367 12-May-2019 kettenis

Make sure the extended PM register descriptions actually exist before
using them. Fixes machines with ancient ACPI 1.0 (such as recent
hypervisors).

ok deraadt@


# 1.366 12-May-2019 kettenis

Add support for mapping ACPI PM registers using the "extended" gasio
representation from the FADT. Mostly fixes the Lanner NCA-1510.

ok lteo@, mlarkin@, deraadt@


# 1.365 11-May-2019 lteo

Fix incorrect error message.

ok kettenis@


# 1.364 08-May-2019 tedu

free size. the allocation is the header plus the length.


Revision tags: OPENBSD_6_5_BASE
# 1.363 04-Apr-2019 kettenis

Avoid potential null-pointer dereference. Found by Kent R. Spillner.

ok deraadt@, jsg@


# 1.362 20-Jan-2019 jsg

fix SMALL_KERNEL build after changes in rev 1.361
reported by naddy@ ok deraadt@


# 1.361 19-Jan-2019 tedu

Add a pwraction sysctl that controls what the power button does on acpi.
By default, nothing changes -- shutdown is initiated. But allows turning
power button into a sleep button if desired.
(grudging) ok from a few parties


# 1.360 26-Oct-2018 kettenis

Add acpipci(4) on amd64. For now this only calls the PCI-specific _OSC
method to let the ACPI implementation know what features we support.


Revision tags: OPENBSD_6_4_BASE
# 1.359 30-Aug-2018 kettenis

Move softbutton check before the check that skips attaching certain acpi
devices. Fixes regression caused by matching on _CID in addition to
matching on _HID.

ok matthieu@, mlarkin@


# 1.358 25-Aug-2018 kettenis

Match ACPI devices based on _CID if no match for _HID is found.

ok mpi@


# 1.357 19-Aug-2018 kettenis

Add support for multiple PCI segments. Only really implemented for arm64
for now as amd64/i386 firmware still caters for legacy OSes that only
support a single PCI segment.

ok patrick@


# 1.356 03-Aug-2018 kettenis

Let ahci(4) match on _CLS instead of _HID when attaching at acpi(4). Avoids
having to add many more _HID entries to the match table.

ok deraadt@, mlarkin@


# 1.355 10-Jul-2018 kettenis

Make legacy interrupts work in acpipci(4).

ok patrick@


# 1.354 05-Jul-2018 kettenis

Add acpipci(4), a driver that supports generic ECAM-compatible PCI host
bridges based on information provided by ACPI.

ok mlarkin@


# 1.353 04-Jul-2018 kettenis

Properly pass around the PCI "chipset tag" in acpi(4) and refactor
acpimcfg(4) to call an MD initialization functions that sets up a tag for
PCI ECAM.

ok guenther@, mlarkin@, krw@


# 1.352 01-Jul-2018 kettenis

Move acpi(4) attach glue into acpi_machdep.c.

ok guenther@, deraadt@


# 1.351 01-Jul-2018 kettenis

Add support for _DSD Device Properties.

ok mlarkin@


# 1.350 30-Jun-2018 kettenis

Add intr_enable() function, intended for MI use to amd64 and i386 and use
this in the acpi(4) suspend/resume code paths.

ok deraadt@


# 1.349 29-Jun-2018 kettenis

Use <stdint.h> types.

ok mpi@, jung@, krw@, deraadt@


# 1.348 29-Jun-2018 mlarkin

ACPI: Allow (with warning) GPE handler reassignment, instead of returning
a failure code.

ok kettenis


# 1.347 26-Jun-2018 mlarkin

knf


# 1.346 26-Jun-2018 kettenis

Let acpi(4) pass down the bus dma tag.

ok deraadt@


# 1.345 25-Jun-2018 kettenis

Reorganize acpi(4) code a bit in preparation for upcoming arm64 support.

ok deraadt@


# 1.344 20-May-2018 kettenis

Wake up the acpi thread after scheduling a gpio event.

ok mpi@


# 1.343 17-May-2018 kettenis

Sprinkle some #ifndef SMALL_KERNEL such that RAMDISK kernels cuild again.

pointed out by stsp@


# 1.342 17-May-2018 kettenis

Implement GenericSerialBus OpRegion support.

ok mlarkin@


# 1.341 27-Mar-2018 kettenis

Add acpicmos(4), a driver that implements SystemCMOS OperationRegion
access support. This fixes machines where the AML doesn't check whether
support for this OperationRegion type has been registered by the OS.

ok mlarkin@


Revision tags: OPENBSD_6_3_BASE
# 1.340 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.339 10-Feb-2018 deraadt

Syncronize filesystems to disk when suspending. Each mountpoint's vnodes
are pushed to disk. Dangling vnodes (unlinked files still in use) and
vnodes undergoing change by long-running syscalls are identified -- and
such filesystems are marked dirty on-disk while we are suspended (in case
power is lost, a fsck will be required). Filesystems without dangling or
busy vnodes are marked clean, resulting in faster boots following
"battery died" circumstances.
Tested by numerous developers, thanks for the feedback.


# 1.338 08-Feb-2018 deraadt

After flushing disks, try again to release all memory. We don't need to
save dirty memory to the hibernate space.


# 1.337 08-Feb-2018 deraadt

Toss all releaseable memory, because fragmentation can get in the way
of allocating the hibernate playpen.


# 1.336 08-Feb-2018 deraadt

sr_quiesce() is a new approach for ensuring that softraid drains
output to the disks.

This is part of a larger suspend/resume filesystem-safety diff, which
has been worked on for a couple of months already. Tests by job, krw,
beck, benno, and others. Sometimes even by snapshot users...


# 1.335 29-Nov-2017 kettenis

Add support for GPIO-signaled events.

ok mlarkin@


# 1.334 16-Nov-2017 jcs

Add PCI attachment for dwiic(4) needed by Intel 100 Series machines

ok kettenis


# 1.333 14-Nov-2017 kettenis

Add a few more PNP IDs to the skip lists.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.332 17-Aug-2017 stsp

Make dwiic(4) attach its ACPI parent dependencies.
This change makes the Asus E200HA keyboard work.

Original analysis and patch by Cesare Gargano
ok kettenis@


# 1.331 09-Aug-2017 dcoppa

Add "PNP0303" (8042 PS/2 Controller) to acpi_isa_hids[]

ok kettenis@


# 1.330 22-Jul-2017 jcs

acpibat: look for _BIX first before falling back on _BIF

ACPI 4.0 deprecated _BIF for battery status so some newer machines
are exporting _BIX which extends _BIF's package with a few extra
fields. When using _BIX, export the new discharge cycle count as a
sensor.

feedback from kettenis and anton
ok deraadt


# 1.329 20-Jul-2017 kettenis

Start a list of isa(4) device names such that we can avoid printing them
as "not configured".

ok mikeb@, millert@, deraadt@


# 1.328 08-Apr-2017 deraadt

Sensors are run as callbacks inside tasks. During suspend a sensor
could be running inside a driver that will be force-detached, or due
to tsleep end up disrupting the softstate/hardstate contract. At
suspend time, quisce all these callbacks by waiting for completion.
This issue has never been observed for real, but may be implicated in
suspend/resume failures.
ok kettenis guenther mlarkin


# 1.327 08-Apr-2017 deraadt

Two prototypes for acpi_maptable; merge to one place


Revision tags: OPENBSD_6_1_BASE
# 1.326 27-Mar-2017 deraadt

Now that hibernate_alloc() only has clean success/failure, don't
need to call hibernate_free() to clean up a partial mess.
ok mlarkin kettenis


# 1.325 13-Mar-2017 deraadt

allow ramdisks to build; ok jcs


# 1.324 12-Mar-2017 jcs

try to attach acpisbs first and if successful, don't attach acpibat


# 1.323 02-Mar-2017 natano

Add a new sysctl machdep.lidaction. The sysctl works as follows:

machdep.lidaction=0 # do nothing
machdep.lidaction=1 # suspend
machdep.lidaction=2 # hibernate

lidsuspend is just an alias for lidaction, so if you change one, the
other one will have the same value. The plan is to remove
machdep.lidsuspend eventually when people have upgraded their
/ets/sysctl.conf.

discussed with deraadt, who came up with the new MIB name
no objections mlarkin
ok stsp halex jcs


# 1.322 28-Feb-2017 natano

Untangle abstract sleep modes from ACPI sleep states. This paves the way
for further work in the area.

idea and ok deraadt
ok mlarkin


# 1.321 22-Feb-2017 jcs

add acpisbs, an acpi smart battery subsystem driver reading data
over smbus

currently disabled because it conflicts with acpibat

ok deraadt, kettenis


# 1.320 14-Jan-2017 kettenis

Revert aml_rdpciaddr changes; breaks several machines that were working
before.


# 1.319 11-Jan-2017 jsg

Increase the size of the buffers used for _HID and _CID strings to be
able to fit "Hyper_V_Gen_Counter_V1".

ok mlarkin@ deraadt@


# 1.318 08-Jan-2017 kettenis

Make aml_rdpciaddr() work for for devices behind bridges. The code now
uses the mapping between AML nodes and PCI devices that we establish.
Because _INIT methods may end up calling aml_rdpciaddr(), make sure we
create that mapping early. Also handle devices that aren't actually present.
These devices are now included in the mapping and reads will return an
all-ones patterm whereas writes are a no-op..

ok mlarkin@


# 1.317 25-Oct-2016 pirofti

Add the resource index to aml_parse_resource() callback function.

This is needed by an upcoming acpiec commit that handles machines breaking
the current ACPI specifications.

Change suggested by and ok kettenis@, guenther@


# 1.316 18-Sep-2016 guenther

If the APIC table (MADT) isn't present then ci_acpi_proc_id won't be set
when acpicpu tries to attach, leading to a NULL dereference. The ACPI
processor object on the Dells where this was reported is bogus and useless
anyway so apply the same test when deciding whether to attach to processor
objects as acpicpu would when matching them to cpus.

Problem reported by Eivind Eide (xenofil (at) gmail.com) and
Johan Huldtgren (johan+openbsd-bugs (at) huldtgren.com).
Testing by Eivind Eide.
ok kettenis@ mlarkin@


# 1.315 03-Sep-2016 naddy

Write the system time back to the RTC every 30 minutes.
This fixes the problem that long-running machines which were not
shut down properly would reboot with a badly offset system time.

hints and ok kettenis@


# 1.314 31-Aug-2016 mlarkin

Don't put a device in the wake device list if _STA says it's not there.

Fixes at least one machine that woke up from sleep / poweroff immediately.

ok kettenis@, tested by kettenis and martijn@


# 1.313 28-Jul-2016 kettenis

Store the acpi processor ID/UID in struct cpu_info, and use it to attach
acpicpu(4) drivers to the right cpu(4).

ok mlarkin@, guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.312 10-Jun-2016 kettenis

The power management timer has been made optional in ACPI 5.0A, so don't
attempt to attach acpitimer(4) if the timer isn't present.

Pointed out by jcs@

ok jcs@


# 1.311 08-May-2016 kettenis

Make sure devices listed by a device's _DEP method are attached before
we attach the device itself. The _DEP methode, introduced in ACPI 5.0,
is a hint that the device in question depends on OpRegion support from
the devices listed by _DEP, so we have to order them the proper way.
To prevent us from attaching those devices again when we encounter them
later walking down the device tree, keep track of our attempts to
attach a device driver to them using aflag in the aml_node structure.

ok guenther@


# 1.310 25-Apr-2016 pirofti

We attach acpihpet(4) and acpiec(4) without using the HID, so do not
report it in the dmesg.

OK deraadt@, mlarkin@.


# 1.309 02-Apr-2016 kettenis

Reduce dmesg spam by knocking out some acpi hardware IDs that don't need a
driver.

ok mpi@


# 1.308 29-Mar-2016 kettenis

Check if a device is present (using _STA) in acpi_foundhid. This prevents
us from reporting devices as "not configured" that aren't actually present,
and allows us to remove duplicated code from several drivers.

ok deraadt@, ok jsg@


# 1.307 29-Mar-2016 kettenis

Implement a cleaner way to prevent acpivout(4) from attaching if we have
a platform-specific method of controlling the backlight.

ok millert@, mlarkin@, jung@


# 1.306 28-Mar-2016 kettenis

Adding each and every new acpi device driver to acpi_foundhid() is
gettinga bit of the burden. And it really isn't how our autoconf
framework is supposed to work for busses that can be enumerated. All
the drivers already check for a matching _HID in their attach
function. So we can just drop the checks in acpi_foundhid() and let
the drivers sort themselves out.

Print a "not configured" line for devices for which we don't attach adriver.
This may be a bit much as there are devices for which we will not have a
driver. This will be removed if it causes too much dmesg spam.

ok deraadt@, mlarkin@


Revision tags: OPENBSD_5_9_BASE
# 1.305 17-Jan-2016 jsg

Take acpi_foundhid() outside of #ifndef SMALL_KERNEL so the ramdisk
kernel can find and attach dwiic(4) and sdhc(4).


# 1.304 17-Jan-2016 jsg

remove an unused global variable


# 1.303 14-Jan-2016 kettenis

Attach dwiic(4) on the Intel Bay Trail i2c controllers as well.


# 1.302 12-Jan-2016 kettenis

Improve emulation of PM registers on Hardware-redcued ACPI by emulating
SPL_TYPx, SLP_EN and WAK_STS using the new SLEEP_STATUS_REG and
SLEEP_CONTROL_REG registers. Makes the transition into S5 (powerdown) work
on the ASUS X205TA.

ok deraadt@, mlarkin@


# 1.301 12-Jan-2016 jcs

Add dwiic, a driver for the Synopsys DesignWare i2c controller found
on the Samsung ATIV Book 9 laptop. This initial version only
supports ACPI config/attachment.

Add ihidev, a HID-over-i2c driver largely based on uhidev. dwiic
handles attaching ihidev devices found in ACPI.

Add ims, a HID-over-i2c mouse/trackpad driver to get basic cursor
and button functionality from HID-compliant i2c trackpads.

ok kettenis deraadt


# 1.300 11-Jan-2016 kettenis

Make sdhc(4) attach to hardware IDs "80860F14" and "PNP0FFF".


# 1.299 09-Jan-2016 kettenis

Initial support for Hardware-Reduced ACPI. This skips the legacy mode checks
by emulating the PM_CNT register such that SCI_EN is always enabled. It also
skip attaching acpitimer(4) since the relevant registers are no longer there.

ok deraadt@, mlarkin@


# 1.298 06-Jan-2016 kettenis

Follow the spec and check the SCI_EN bit to see if ACPI has already
been enabled and don't bother enabling it again if it is. This should fix
systems that don't implement legacy mode and therefore set ACPI_ENABLE and
ACPI_DISABLE to zero.

ok mlarkin@, yasuoka@


# 1.297 23-Nov-2015 reyk

Back out the CRITICAL change, turn it back from 10% to 25%. Some
people with old/broken batteries didn't like this change, and the
underlying heuristics are wrong.


# 1.296 22-Nov-2015 reyk

The "CRITICAL" APM battery level, that is exported/faked by the ACPI
stack, is currently set to 25%. With 25% battery left, a typical
modern laptop will have at least one hour runtime left. It is an
arbitrary value, and other APM power layers in the kernel set CRITICAL
to something like 5% or 10%, so set it to 10% for now.

OK mlarkin@


# 1.295 28-Sep-2015 deraadt

In low-level suspend routines, set cold=2. In tsleep(), use this to
spit out a ddb trace to console. This should allow us to find suspend
or resume routines which break the rules. It depends on the console
output function being non-sleeping.... but that's another codepath which
should try to be safe when cold is set.
ok kettenis


# 1.294 06-Sep-2015 deraadt

clarify malloc arguments (base + length...)


# 1.293 28-Aug-2015 deraadt

fairly simple sizes for free(); ok tedu


# 1.292 20-Aug-2015 kettenis

Respect the access size when reading or writing to pci config space.
This version also makes sure writes are properly aligned, preventing
the panics seen with the previous incarnation of this change.

Fixes battery status passthrough in vmware, brightness keys on my x220
and who knows what else.

ok mlarkin@, deraadt@


Revision tags: OPENBSD_5_8_BASE
# 1.291 04-Aug-2015 deraadt

If we are setting ACPI_PM1_BM_RLD do it only once, and remember to
repeat this action at resume time.
ok mlarkin guenther


# 1.290 29-Jul-2015 deraadt

Something subtle in previous revision is locking up suspend or booting
on some laptops. Mailing lists threads mention x220 and Asus machines.
Reverting for now.


# 1.289 21-Jul-2015 kettenis

iRespect the access size when reading or writing to pci config space.
Fixes battery status passthrough in vmware and who knows what else.

Tested by many.

ok mlarkin@, deraadt@


# 1.288 17-Jul-2015 mlarkin

fix comments


# 1.287 26-Mar-2015 jsg

Use the 0x10 extended capability to determine if the trackstick
buttons are wired to the trackpad and need to be re-routed to the
trackstick. Without this change the buttons on 2015 thinkpads
get picked up as extended buttons that show up as scroll up/down.

Remove the X1 Carbon 2015 (LEN0048) and X250 (LEN0046) from the
top button area/soft buttons quirks list. Also avoid using the
quirk list entirely if the capability bit is set.

Based on work from jcs@ with feedback from mpi@
ok mpi@ jcs@


# 1.286 20-Mar-2015 kettenis

The ACPI standard says that a PCI function number of FFFF can be used to refer
to all functions on a device. This makes no sense in the context of accessing
PCI config space. Nevertheless stsp@ has a machine with AML that tries to
do config space reads from a device where _ADR encodes function number FFFF.
This is almost certainly buggy AML, but since Linux (and presumably Windows)
boot just fine on this machine we need to work around this. Linux effectively
treats function FFFF as function 7, which isn't present on this machine.
Instead we simulate a read from a non-existent device by returning a bit
pattern of all ones. Writes to function FFFF will still panic.

ok mlarkin@


# 1.285 16-Mar-2015 deraadt

Reduce use of SMALL_KERNEL in acpi, bringing acpiec driver and more gpe
handling into RAMDISK. This is now possible because the install media
has ample room. The goal is to reduce special cases where we may be
skipping (unknown) important operations...
ok mlarkin kettenis


# 1.284 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.283 07-Feb-2015 deraadt

New framework that allows hibernate to pass in entropy from it's fresh
boot.
ok mlarkin


# 1.282 06-Feb-2015 mlarkin

Fix a hibernate crash on some machines due to unmapping a page that
may not have been mapped previously (in the failure to hibernate case).

Also ensure that the lowmem ptp is mapped in all cases (not just MP).

ok kettenis


# 1.281 17-Jan-2015 deraadt

resume_randomness() before spinning up other cpus...


# 1.280 15-Jan-2015 jsg

Define a new wscons mouse type for Synaptics clickpad devices that lack
physical buttons. This will be used if the acpi pnpid for the mouse
matches a list provided by Synaptics found in Linux.

Combined with the xenocara changes this will remove the need for an xorg
config file for the x240/t440/t540 generation of thinkpads.

ok matthieu@ shadchin@ kettenis@


# 1.279 11-Jan-2015 kettenis

Tolerate checksum errors in ACPI tables (but complain about them). Apparently
there are machines out there with broken BIOSen with such checksum errors in
thier RSDT tables. Windows runs fine on these machines, so it probably does
not check. Linux ignores checksum errors as well (and prints a warning).

ok deraadt@, krw@


# 1.278 18-Dec-2014 deraadt

suspend and resume the RNG (at what looks like the right spots)
ok reyk djm


# 1.277 09-Dec-2014 doug

Replace some malloc(n*size,...) calls with mallocarray().

ok tedu@ deraadt@


# 1.276 06-Dec-2014 mpi

If "machdep.lidsuspend" is not set, do not go back to sleep if the lid
is still closed when resuming. This allows docked laptop to be resumed
without opening them.

ok mlarkin@


# 1.275 23-Nov-2014 mlarkin

If a machine has no _LIDs, then none can be open. This diff fixes a problem
where machines without any _LIDs were going back to sleep after hibernate
due to the recent suspend-if-lid-closed-on-resume diff. Noticed on qemu,
which doesn't have any _LIDs.

discussed with deraadt


# 1.274 18-Nov-2014 krw

Nuke yet more obvious #include duplications.

ok miod@


# 1.273 08-Nov-2014 mlarkin

If resuming from sleep (zzz/ZZZ) and the lid is still closed, go back to
sleep. This prevents accidental lid flex or slight opening in a backpack
from waking the machine up and leaving it resumed (powered on).

ok deraadt@


# 1.272 26-Sep-2014 kettenis

Rework piglet and pig allocation. Currently the piglet gets allocated
deep down in the suspend path, where it is really hard to recover from
allocation failure. So allocate the piglet early on in the suspend path.
Also change the piglet and piglet allocation functions to use km_alloc(9)
instead of doing pmemrange magic. This removes a bunch of code which, in the
case of the piglet allocation, is broken since it results in a NULL pointer
dereference. Also switch the piglet allocation to not wait. If we can't
allocate 16MB of phys contig memory on a halfway modern machine we're almost
certainly under a lot of memory pressure and we're better off not trying to
hibernate anyway.

ok mlarkin@


# 1.271 25-Sep-2014 kettenis

Bring back rev 1.249. Now that mp_setperf() has been fixed, it should be
safe to restore perflevel when resuming. Should fix the issues people
see with the processor running at full speed heating up the machine while
the apm command reports that the CPUs are running at a low speed.

ok dcoppa@, deraadt@


# 1.270 23-Sep-2014 kettenis

Release the acpi lock around calls to wsdisplay_suspend() and
wsdisplay_resume(). The X server may adjust the display brightness just
before/after the VT switch when suspending/resuming. In fact, it may
already be blocked adjusting the display brightness for some reason
unrelated to the suspend request.

The VT switch request will eventually time out, but the delay is annoying
and the failed VT switch will lead to problems after resume.

Needed for a future update of the xf86-video-intel driver.

ok mlarkin@


# 1.269 19-Sep-2014 kettenis

Use config_suspend_all(9).


# 1.268 08-Sep-2014 kettenis

Clear the PME Status bit on PCI devices in response to Device Wake events.
Fixes the interrupt storm on the 2009 Mac mini with WOL enabled on its
nfe(4) interface.

ok mlarkin@


Revision tags: OPENBSD_5_6_BASE
# 1.267 20-Jul-2014 mlarkin

Support hibernating to softraid crypto volumes.

much help and ok from deraadt@


# 1.266 16-Jul-2014 mlarkin

Reenable hibernate RLE support and flush+zero all memory after unpack.


# 1.265 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.264 12-Jul-2014 kettenis

Add a function to drop all clean pages on the page daemon queues and call
it when we hibernate.

ok mlarkin@, miod@, deraadt@


# 1.263 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.262 11-Jul-2014 mlarkin

Flush the buffercache to 16MB on hibernate and restore its previous max
size (kern.bufcachepercent) on resume, for better hibernate performance.

ok beck@


# 1.261 10-Jul-2014 blambert

remove unneeded header

ok mlarkin@


# 1.260 01-Jul-2014 kettenis

Adding an additonal delay after each access to SystemMemory or SystemIO
during autoconf makes no sense.

ok deraadt@


# 1.259 23-Jun-2014 kettenis

SystemMemory is used to access memory mapped registers on some machines, so we
must use the bus_space(9) API to access it instead of memcpy(9). Also make
sure we properly align access to fields that need more than byte alignment.

ok guenther@


# 1.258 21-May-2014 mlarkin

Fix invalid / missing format specifiers in several dnprintf statements
(used only when ACPI_DEBUG is enabled)

From a diff on tech@ by Fabian Raetz, with a minor change by me, tested
on i386 and amd64 with ACPI_DEBUG enabled and disabled.


# 1.257 25-Apr-2014 mlarkin

Defer the free of the hibernate structures until later, when it is safe to
do so.

Freeing the hibernate structures too early leads to inadvertent calls into
splx when we aren't ready for them yet. Found when trapping splx calls in
the resume path.

discussed with deraadt@ at m2k14


# 1.256 13-Apr-2014 mlarkin

Fix a badly worded debug message that implied the entire machine was going
to sleep, instead of just the acpi thread going to sleep.


# 1.255 31-Mar-2014 mpi

Change the order of operations during a suspend/resume cycle and
call bufq_quiesce() after executing the DVACT_QUIESCE handlers.

This should be safe since no disk nor controller drivers have such
handler but it will allow us to detach sd(4) devices attached to a
USB bus.

Another benefit pointed out by kettenis@ is that drivers that need
to read a firmware from the disk should be able to do it at resume
time in a DVACT_WAKEUP handler.

ok kettenis@, deraadt@


# 1.254 13-Mar-2014 dlg

get rid of the assumption that the head of the alldevs list is the
"mainbus" device. this breaks when mpath is enabled because it
attaches before mainbus and therefore takes the head position.

have autoconf provide device_mainbus() which looks up mainbus_cd,
and use that instead.

discussed with deraadt who just wants mpath stuff to move forward
despite there being many ways to shine this particular turd.


Revision tags: OPENBSD_5_5_BASE
# 1.253 21-Feb-2014 deraadt

Do the lid control further on in the resume sequence, since this is
AML, and will do WAITOK operations. While cold, that is a bad idea.
We seem to be safe with respect to the spec, I do not see any event
debounce issues here.
ok kettenis, tested by many


# 1.252 20-Jan-2014 claudio

Instead of checking sc->sc_revision <= 1 check that the
fadt->hdr_revision <= 1. This is a better check since it checks
the version of the proper descriptor. This allows the alix APU
to reboot since those boards have currently a wrong sc->sc_revision.
Committing this now so that it can be tested easier.
With and OK jsg@ also fine by derradt@


# 1.251 24-Dec-2013 kettenis

If the FADT has its SMI_CMD set to zero, assume we're only ACPI-only hardware
and don't need to disable SMI ownership of the ACPI hardware registers.

ok mlarkin@


# 1.250 23-Dec-2013 kettenis

Revert previous commit. It makes resume hang on some MP systems running
GENERIC.MP.


# 1.249 16-Dec-2013 kettenis

Restore hw.setperf upon resume like we do for apm(4) on i386. Seems we need
to do this fairly late in the resume to avoid hangs.

ok deraadt@, mlarkin@


# 1.248 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


# 1.247 06-Nov-2013 mpi

Add support for Power Resources for Dx states and the necessary hook
for PCI devices. This hook should be called twice, before and after
changing the power state of a PCI device.

Before setting the device to the new state, the ACPI layer will notify
every power resources linked to the device for that state and make sure
they are turned "_ON". After changing the state of the device, it will
decrement the reference of every power resources linked to that device
for the old state and turn them "_OFF" if they are no longer referenced.

This fixes the no-USB after resume problem seen on various ThinkPad,
problem initialy diagnosed with Alexander Polakov.

ok kettenis@, deraadt@


Revision tags: OPENBSD_5_4_BASE
# 1.246 01-Jun-2013 mlarkin

Stop printing ACPI wakeup devices in dmesg after the 16th wakeup device.

Helps with VMware VMs that report hundreds of wakeup devices.

ok reyk@, deraadt@


# 1.245 31-May-2013 bluhm

Fix the build for a kernel without wd and pciide in its config.
OK deraadt@ tedu@


# 1.244 30-May-2013 deraadt

Enforce ca_activate tree-walks over the entire heirarchy for all events,
cleaning up some shutdown-hook related code on the way.
(A few drivers related to sparc are still skipped at kettenis' request)
ok kettenis mlarkin, tested by many others too


# 1.243 18-Apr-2013 deraadt

pin the acpi thread to the BSP, because AML and SMI cannot be trusted. I
was certain we were doing this already, but it turns out no...


Revision tags: OPENBSD_5_3_BASE
# 1.242 24-Dec-2012 guenther

In acpiioctl(), Return though the splx() path if suser() returns an error

ok deraadt@


# 1.241 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


# 1.240 04-Oct-2012 ehrhardt

Use information provided by ACPI to attach secondary PCI host bridges.

ok kettenis@


# 1.239 07-Sep-2012 kettenis

Add a new function called acpi_pci_min_powerstate() that returns the lowest
power state for a PCI device for the power state (currently S3 or S4) we're
transitioning into. This function is probably too simplistic; there is a lot
of (silly) complexity in the ACPI spec.

ok deraadt@


Revision tags: OPENBSD_5_2_BASE
# 1.238 13-Jul-2012 pirofti

Disable acpivideo(4) on ASUS laptops.

There are some models that panic whenever an event is received.
Some cope, some don't. In order to get this right acpiec(4) requires
major rework. Until then let machines boot.

Discussed with deraadt@.


# 1.237 11-Jul-2012 mlarkin

for now, restrict hibernate to root in the acpi ioctl kernel interface.
normal rules for controlling this interface through apmd still apply.

ok deraadt@


# 1.236 09-Jul-2012 deraadt

tiny little oops in untested hibernate block


# 1.235 09-Jul-2012 deraadt

clean up ioctl() case statements


# 1.234 09-Jul-2012 deraadt

acpi_disable_onegpe() is not used; ok pirofti


# 1.233 24-May-2012 kettenis

If _MIN > _MAX consider a WordBus resource to be invalid. Fixes a panic on
the IBM x3550-M4.

ok mikeb@


# 1.232 29-Mar-2012 mlarkin

Missing #ifdef in previous diff


# 1.231 29-Mar-2012 mlarkin

Prevent hibernation attempts on machines with unsupported disk controllers.
The same check is also performed later in the hibernate sequence, but by
then it's too late to cleanly unwind (presently).

ok deraadt


# 1.230 26-Mar-2012 deraadt

simplify conditionals in acpi_sleep_state()
ok mlarkin


# 1.229 26-Mar-2012 deraadt

hook in the hibernate request code; half of this diff is from mlarkin
ok mlarkin


Revision tags: OPENBSD_5_1_BASE
# 1.228 20-Sep-2011 deraadt

Very early in suspend while the clock is still ticking realtime,
save the clock back to the rtc. The zaurus already did this.
ok phessler miod kettenis


Revision tags: OPENBSD_5_0_BASE
# 1.227 02-Jul-2011 nicm

kqueue attach functions should return an errno or 0, not a plain 1. Fix
the obvious cases to return EINVAL and ENXIO.

ok tedu deraadt


# 1.226 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.225 16-Jun-2011 pirofti

Import acpitoshiba driver.

This is originally written by Hiroyuki Aizu, ported and integrated
in our tree by Javier Vazquez <javama4 AT gmail> with tweaks and
suggestions by me.

Import prodded by deraadt@ and the lack of testers on tech@.


# 1.224 27-Apr-2011 jcs

attach acpithinkpad to newer lenovo models like the x120e

ok deraadt@


# 1.223 22-Apr-2011 jordan

Fix uninitialzied variables and formatting strings (-Wxxx errors)


Revision tags: OPENBSD_4_9_BASE
# 1.222 02-Jan-2011 jordan

Re-add code for ACPI taskqueue, eliminates need for state vars in acpi softc
Serialize processing ACPI tasks for sleep/powerdown/GPE event
ok deraadt@


# 1.221 31-Oct-2010 guenther

Revert last commit: it breaks resume on Thinkpads
"then please back it out" deraadt@


# 1.220 26-Oct-2010 jordan

Add task queue for ACPI gpe and notify handlers
This fixes eject on Dell Latitude dock and an issue on the Dell Mini
battery update.
ok deraadt


# 1.219 07-Oct-2010 jordan

Create separate functions for enabling wake and runtime gpes.
Remove old suspend/resume gpewalk function
ok deraadt, mlarkin


# 1.218 05-Oct-2010 jordan

split acpi_enable_onegpe to separate enable/disable functions
ok deraadt


# 1.217 05-Oct-2010 jordan

Added acpi_disable_allgpes function to clear GPE status
ok deraadt


# 1.216 05-Oct-2010 deraadt

clean up acpi_softc use more


# 1.215 05-Oct-2010 deraadt

delete a bit of #if 0 code


# 1.214 05-Oct-2010 deraadt

hide use of our global acpi_softc pointer a bit better


# 1.213 29-Sep-2010 deraadt

grab the acpi thread lock very early in acpi thread startup; not that it
is likely that anyone will race us... but why be careless.
ok kettenis


# 1.212 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.211 29-Aug-2010 jasper

- add a check for FWRITE here too.

ok miod@


# 1.210 27-Aug-2010 kettenis

Make PCI Power Management optional, and only enable it when acpi(4) attaches.

ok deraadt@


Revision tags: OPENBSD_4_8_BASE
# 1.209 08-Aug-2010 kettenis

Close race between the acpi thread and wscons ioctls running in process
context. We might rip this out post-release and replace it with a solution
that serializes things in a more obvious way.

ok deraadt@, miod@ and "those crazy texans who ok anything"


# 1.208 08-Aug-2010 deraadt

Report the acpi sleep states when on a RAMDISK; ok krw


# 1.207 07-Aug-2010 kettenis

Enforce acpi register access rescrictions.

ok mlarkin@, deraadt@, tested by marco@


# 1.206 07-Aug-2010 deraadt

upon resume, notify all the acpiac and acpibat drivers to update
their status. some laptops do not do this automatically.
ok kettenis canacar


# 1.205 06-Aug-2010 marco

Always call _PSW on _LID devices that support it to enable lid open to
resume the machine. Conversely disable it when the machine wakes up.

Tested by several ok deraadt


# 1.204 06-Aug-2010 deraadt

As part of a revamp of the PM1/GPE code, I write this diff to look for
unmanaged EN & STS bits in the PM1 register at interrupt time and report
them. As a side effect this splits the STS acknowledgement into two writes
(for power, and sleep) instead of one. The printf that is added (to spot
unmanaged STS bits) has not yet been triggered as far as we know.

Before the "write to PM1 registers at the right offset" diff went in, this
was not neccessary. But newer thinkpads do not have a working soft power
button without this diff.

We have no idea why.
ok mlarkin kettenis


# 1.203 05-Aug-2010 deraadt

PM1 power button events were generating 'fake events' to acpibtn PWRB,
but if there is no such device these were being tossed. Instead, arrange
for all power button handlers to flag a softc variable which is processed
by the thread; so that one place will know to request a powerdown (psignal
to init)
ok marco mlarkin


# 1.202 05-Aug-2010 deraadt

rename a variable which is annoyingly poorly named


# 1.201 05-Aug-2010 mlarkin

Write to the correct offset for various ACPI pmreg registers. Writing to
the correct register does properly clear the wake status bits and fixed
event bits, which allows some machines to have more than 1 suspend cycle.

bug spotted by deraadt@

tested by deraadt@ and myself on a half-dozen machines and others on
hackers@

ok deraadt@, marco@


# 1.200 05-Aug-2010 deraadt

Fix the power button. The pm1 register is 16 bits in size.
My mistake, a while back.
tested by krw


# 1.199 04-Aug-2010 kettenis

Print supported sleep states.

ok marco@, phessler@, deraadt@


# 1.198 03-Aug-2010 mlarkin

Use the proper flag for re-enabling certain hardware events (power btn,
sleep btn). The (incorrect) flag prevoiously used coincidentally had the
same value, so this shouldn't cause any different behavior than before.

tested on a variety of machines (i386, amd64, sp, mp) - no behavioral
change seen

ok deraadt@


# 1.197 28-Jul-2010 marco

Make all gasio failures look and behave the same.


# 1.196 28-Jul-2010 mlarkin

Delay a little longer on suspend before giving up. Fixes machines that
sometimes don't suspend because the sleep transition takes too long
(symptoms like failing to sleep but the backlight stays on).

ok deraadt@


# 1.195 27-Jul-2010 deraadt

remove XXX; EC can now be attached early when needed; ok jordan


# 1.194 27-Jul-2010 deraadt

more brackets feels good


# 1.193 27-Jul-2010 deraadt

XXX annotate a block in acpi_enter_sleep_state() that is pretty worrying


# 1.192 27-Jul-2010 deraadt

horrific indentation that nearly made me puke


# 1.191 27-Jul-2010 mlarkin

Set the sleeping indicator light on machines that support it, in case SMI
doesn't do it for us. Shows the 'moon' LED on older Thinkpads when
sleeping.

Tested on T43p, W500, T510 by me and on X61s by deraadt@.

ok deraadt@


# 1.190 27-Jul-2010 jordan

Early initialization of acpiec if ECDT table exists
Fixes hang when booting thinkpads while docked
ok deraadt


# 1.189 26-Jul-2010 deraadt

remove merge error which was commited


# 1.188 26-Jul-2010 pirofti

Add support for Sony ACPI hotkeys via a new driver: acpisony(4).

Currently it only works for the suspend button and tries to do right for
the brightness events, but I haven't found a Sony laptop that like to do
right so far.

In the future I want to make the brightness keys work on all Sony's and
also add support for the zoom hotkeys and whatever other funky keys I can
find on those things.

Okay deraadt@.


# 1.187 22-Jul-2010 deraadt

pretty large cleanup of SMALL_KERNEL


# 1.186 21-Jul-2010 deraadt

in the acpi GPE handler, late ack level interrupts not edge interrupts.
Duh.


# 1.185 21-Jul-2010 deraadt

Merge the edge and level gpe handlers into one (now possible since the
gpe structure tells us whether the gpe is edge or level)


# 1.184 21-Jul-2010 deraadt

delete #if 0 code we are not going to use; ok marco


# 1.183 20-Jul-2010 deraadt

make the mask code for acpi_enable_onegpe() match other similar code


# 1.182 20-Jul-2010 deraadt

Handle the acpi interrupt controller with proper edge/level handling,
wrapped in spltty to avoid racing against the interrupt controller.
Repair the gpe bit masking code while there, and do operations in the
right order
ok kettenis mlarkin, help from jordan at figuring out the order of
operations.


# 1.181 20-Jul-2010 matthew

When we're preparing to go to sleep, only call bufq_restart() if other
preparations failed.

ok kettenis@, thib@, deraadt@


# 1.180 19-Jul-2010 deraadt

permit compilation with ACPI_DEBUG


# 1.179 19-Jul-2010 deraadt

there is no need for a seperate dsdt_softc pointer; dsdt does not have a
softc but is just a part of the single acpi driver.
ok jordan


# 1.178 19-Jul-2010 deraadt

acpi_delay() is no longer needed; ok jordan


# 1.177 18-Jul-2010 mlarkin

Clear event status on resume, to avoid going back to sleep/reboot/shutdown
immediately after resume. Tested by a bunch of people on hackers@ on
various hardware. Fixes at least Toshiba NB200 and Gateway LT3103u.

ok marco


# 1.176 13-Jul-2010 deraadt

rename some thread related interfaces so they are logical


# 1.175 10-Jul-2010 jordan

Verify _ADR bdf is correct (some systems return 0xffff)
Works on Dell x9xx and Studio 1555


# 1.174 09-Jul-2010 jordan

Clear the noise


# 1.173 08-Jul-2010 jordan

Add mapping for ACPI device to PCI bdf (match autoconf tree)
Simplify resource parsing function to use buffer argument
Convert namespace linked lists to use queue macros
ok marco@, deraadt@


# 1.172 06-Jul-2010 deraadt

Use spltty() to lock downcalls from apm against the information being
modified by the acpi thread.
ok kettenis


# 1.171 06-Jul-2010 deraadt

Don't use a workq to do the suspend, because it races aginst the acpi
thread. Instead, just tell the acpi thread to do the suspend for us.
This makes apmd & zzz work correctly. While here, have acpithinkpad
attempt to post the event to apm before prompting the suspend itself.
ok kettenis marco mlarkin


# 1.170 05-Jul-2010 mlarkin

Reset register SCI_EN on resume. The ACPI spec clearly states we don't have
to do this, but many machines don't, or forget to, which leads to all sorts
of problems on resume (broken battery stats, overheating, failure to resume
more than once, etc).

tested by many, ok marco@, deraadt@


# 1.169 01-Jul-2010 thib

do bufq quiesce again, since vnd/the bufq_done call in
biodone has been fixed.


# 1.168 01-Jul-2010 jordan

Backout recent AML changes, breaks on R210 and others


# 1.167 01-Jul-2010 jordan

Simplified aml_parse_resource call, cleanup code
ok marco


# 1.166 01-Jul-2010 jordan

Fix subordinate bus # for multi-root PCI buses
ok marco, kettenis


# 1.165 30-Jun-2010 thib

Disable/partially backout the bufq quiesce changes as this
is causing havoc with vnds and release must be buildable.


# 1.164 29-Jun-2010 deraadt

Fix ramdisks (new code was placed inside a SMALL_KERNEL chunk)


# 1.163 29-Jun-2010 jordan

Add support for mapping ACPI to PCI devices
ok kettenis, deraadt


# 1.162 29-Jun-2010 kettenis

Call bufq_quiesce() on suspend to stop all I/O and bufq_restart on resume.
Makes suspend/resume work much better while doing disk I/O.

ok mlarkin@


# 1.161 29-Jun-2010 tedu

60 is a more reasonable guess for number of minutes in an hour than 100.
spotted by TeXitoi. ok deraadt


# 1.160 27-Jun-2010 jordan

Add _acpi_matchhids routine, precursor to pci device match


# 1.159 27-Jun-2010 mlarkin

Clear ACPI fixed event status on resume (power buttons/etc) so that some
machines don't immediately resume after suspending to S3.

Tested by kettenis@ and I on 5 or 6 machines, deraadt@ "yeah, looks good"


# 1.158 27-Jun-2010 jordan

Cleaned up acpi_maptable, adds to table list directly


# 1.157 07-Apr-2010 deraadt

If we have done a wsdisplay_suspend(), and ACPI decides it does not want to
suspend, we must roll back by calling wsdisplay_resume() or the virtual
console switching will be locked forever
ok oga


# 1.156 07-Apr-2010 kettenis

Make suspend/resume work on MP machines (running an MP kernel). Joint work
from mlarkin@ and me, with some amd64 fixes thrown in by deraadt@

ok marco@, deraadt@, pirofti@, mlarkin@


# 1.155 31-Mar-2010 kettenis

Bring /dev/apm support in line with apm(4). Make the suspend button send
a suspend request event to apmd(8) instead of suspending immediately. Also
keep track of whether /dev/apm and /dev/apmctl are currently open, such that
we can still suspend immediately if apmd(8) isn't running.

ok deraadt@, marco@, pirofti@, jsing@, oga@


# 1.154 30-Mar-2010 oga

Prevent the apmd/x races for good.

When we hit suspend time, go through all wsdisplays on the system. if
they are in mode MAPPED, but not MODE_DUMBFB then if possible do a full
vt switch to a !mapped vt, and prevent switching back until resume time.
This has to be called from MD code because this involves userland
running so that X can run the vt switch signal handler. This way, any
case where we are using the "poke registers from userland" model, we
will not be on the hardware when we go down, so the kernel can actually
handle thing properly.

Tested on several acpi laptops (by kettenis@ and ian@), x40 (me and
beck@ at LEAST) and zaurus (me). Maybe others, but if so I forgot who at
this time..

Idea from deraadt somewhere over the Faroe Islands (I thought of a
similar thing myself a while ago). Much prompting from him. Ok and
comments miod@


# 1.153 25-Mar-2010 oga

Fix & vs. && in the kqfilter code.

Based on a comparison with the apm code.

ok deraadt@, kettenis@


Revision tags: OPENBSD_4_7_BASE
# 1.152 26-Nov-2009 mlarkin

Visual (and on some machines, audio) feedback while the machine is
resuming via calls to aml _SST method. This also makes the "moon" LED on
thinkpads to blink during resume and cease when resume is completed.

ok pirofti@


# 1.151 26-Nov-2009 deraadt

Toggle into "cold" when we we disable interrupts, because tsleeping
in the AML interpreter and context switching into userland processes
tends to sometimes pop the ACPI trampoline page out of our address
space. Makes lots more machines work.
ok kettenis mlarkin


# 1.150 24-Nov-2009 jsg

in the resume path evaluate _WAK before dealing with device drivers
like the spec says, not after.

ok mlarkin@


# 1.149 24-Nov-2009 deraadt

abort suspends on SMP systems until that gets written; ok mlarkin


# 1.148 23-Nov-2009 mlarkin

pci should return the result of it's children's suspend/resume calls back
to its parent.

handle suspend failure case and unwind if devices have an activate function
that returns failure.

ok deraadt@, kettenis@


# 1.147 23-Nov-2009 mlarkin

acpi* devices need to call their children on suspend/resume.
ok deraadt@


# 1.146 23-Nov-2009 pirofti

Remove ACPI_SLEEP_ENABLED checks.

This enables by default the suspend/resume paths in the kernel.

Okay deraadt@.


# 1.145 23-Nov-2009 deraadt

knf and spacing that are super annoying


# 1.144 23-Nov-2009 mlarkin

Hook up Theo's new children suspend/resume function, as well as more ACPI
suspend/resume glue.

ok deraadt@


# 1.143 26-Oct-2009 deraadt

Do not do strncmp comparisons on dv_xname because longer device names which
look similar could arrive in the future. Instead, compare directly against
dv_cfdata->cf_driver->cd_name
Issue originally spotted by miod


# 1.142 26-Oct-2009 jordan

Verify checksum+address when loading ACPI tables.
Some systems had invalid entries in RSDT/XSDT.
ok marco@


# 1.141 23-Jul-2009 cnst

New aibs(4) driver for ASUSTeK AI Booster (ACPI ATK0110) hardware monitoring,
with sensor state support through limits provided by the ACPI.

Tested on several ASUS motherboards kindly networked by Sam Fourman Jr.

ok deraadt marco jordan


Revision tags: OPENBSD_4_6_BASE
# 1.140 03-Jun-2009 pirofti

Import acpi power resource driver. This is needed for suspend/resume logic.

Okay jordan@ and marco@.


# 1.139 03-Jun-2009 pirofti

Add acpivideo support. This adds brightness support for all laptops
except thinkpads, they will use the acpithinkpad driver. The driver is
also hooked into wsconsole. So brightness can be adjusted via:

$ wsconsctl display.brightness=<percentage>

This is very helpfull on some laptops that have a nasty bios and get two
steps instead of one when pressing the brightness button.

Tested on various dell, fujitsu, acer, samsung and other laptops.

Okay marco@, miod@. Suggestions from kettenis@.
Lots of reviews and help from miod@, thanks!


# 1.138 03-Jun-2009 jordan

Added ejectable bay for IDE cdroms
ok marco@


# 1.137 30-Apr-2009 marco

Oga changed apm ioctls and forgot to grep. Temporary work around by hitting
the sleep button when we get any request. This interface will chnage at
some point.
ok oga


# 1.136 19-Apr-2009 krw

Call acpi_sleep_walk() when powering down just like when entering
any other sleep state. This tells acpi wake devices to not wake up
the box until manual powering up resets their status. Makes my Dell
GX520 stay powered off in response to 'halt -p'.

In snaps for a while.

ok marco@ (several times) deraadt@


# 1.135 17-Apr-2009 pirofti

Add device ranking support for future sleep/wake developments.

okay marco@


# 1.134 11-Mar-2009 jordan

Cleanup.. use aml_evalinteger instead of aml_evalname
ok marco@


# 1.133 10-Mar-2009 jordan

Fix cut/paste error.. call _BFS not _PTS
ok marco@


Revision tags: OPENBSD_4_5_BASE
# 1.132 21-Feb-2009 marco

overzealous cut/paste; found by Vladimir Kirillov <proger@uaoug.org.ua>


# 1.131 20-Feb-2009 marco

Oops didn't mean for Debugger() to remain there.


# 1.130 19-Feb-2009 marco

suspend/resume bits so that we can develop this in tree. This is disabled.
code from mlarkin and me
help from art,toby,jordan and several others
ok jordan, go for it deraadt


# 1.129 10-Feb-2009 jordan

Fix null pointer for shutdown if acpi not enabled
ok marco@


# 1.128 04-Feb-2009 kettenis

Some systems that claim to be ACPI 2.0 apparently don't provide an XSDT
address. Make the code fall back on the RSDT on those systems.

From Joerg Niendorf <p3y80@internode.on.net>. Fixes PR 5986.


# 1.127 17-Jan-2009 krw

A couple of stray malloc/memset -> malloc(M_ZERO), a bit of knf and
lose a couple of unnecessary casts. No functional change.

Feedback from kettenis@

ok kettenis@ marco@


# 1.126 06-Nov-2008 marco

Collapse all sanity checks of hids into one function.

Originaly from jordan


Revision tags: OPENBSD_4_4_BASE
# 1.125 02-Jul-2008 fgsch

add acpivideo. for now almost an empty skel so others can work on
suspend/resume. eventually it will also manage output switching and
brightness where supported. prodded by marco@


# 1.124 11-Jun-2008 marco

Fix $OpenBSD$ for once and for all


# 1.123 08-Jun-2008 deraadt

aaa_dev was being set from an uninitialized variable; ok toby


# 1.122 07-Jun-2008 marco

Kill ACPIVERBOSE. It was never used and is worthless to boot.

comments jmc
ok toby


# 1.121 06-Jun-2008 marco

Shrink acpi some more for floppies and while here re-enable acpi on the
floppies. Fixes crazy sony vaio kettenis has.

tested by deraadt
ok deraadt jordan kettenis


# 1.120 01-Jun-2008 marco

Fix \n nit in SMALL_KERNEL.


# 1.119 01-Jun-2008 marco

Re-enable new parser. Lots of testing by various folks. Fits all media
now too.

ok kettenis deraadt


# 1.118 16-May-2008 dlg

back out jordans new parser.

it didnt have enough oks from the right people, it did break machines
that were previously working, and it bloated the RAMDISKS and install
media beyond the constraints of the media.

requested by deraadt@


# 1.117 14-May-2008 jordan

Adding new ACPI Parser code
Old guts of code still exists, needs to be torn out next
ok marco@
ok brad@


# 1.116 27-Apr-2008 jcs

add acpithinkpad(4), an acpi support driver for ibm/lenovo
thinkpads.

provides functionality that the bios did on older, non-acpi
thinkpads like respond to screen brightness keys and toggle the
bluetooth device on and off.

ok deraadt@ and marco@


# 1.115 24-Apr-2008 jsing

Introduce acpiasus(4), a driver for the ACPI based hotkeys found in many
ASUS laptops (including the ASUS EeePC) - largely based on NetBSD's
asus(4) driver. On the ASUS EeePC this allows us to enable/disable
wireless, change screen brightness and use the volume keys.

ok jsg@, weingart@


# 1.114 12-Apr-2008 kettenis

Get rid of acpi_s5 global variables; simply send SIGUSR2 instead of SUGUSR1
to tell init(8) to power down the machine.

ok krw@


Revision tags: OPENBSD_4_3_BASE
# 1.113 21-Jan-2008 jsg

Check against RESET_REG_SUP in FADT before doing the acpi reset dance
after all, not doing so appears to break more machines than it fixes.

ok marco@


# 1.112 05-Jan-2008 kettenis

Never attach more acpicpu(4) devices than cpu(4) devices. Always attach
the first acpicpu(4) device on non-MULTIPROCESSOR kernels, regardless of its
ACPI processor ID.

ok mikeb@


# 1.111 05-Dec-2007 deraadt

Hang acpi(4) below bios(4) instead of mainbus(4). This lets us move acpi(4)
before pcibios(4), and then.... if acpi(4) attaches, skip pcibios(4) since
it messes the machines up. Fixes claudio's HP dl320 G5, and almost assuredly
others.
ok kettenis


# 1.110 05-Dec-2007 deraadt

repair acpiopen/acpiclose and friends to handle the /dev/apmctl interface,
so that apmd(8) can actually work correctly against acpi(4), and thus,
even do the fancy -C/-A stuff; also tested by jmc


# 1.109 02-Dec-2007 fgsch

fix the trailing space for real now.


# 1.108 02-Dec-2007 fgsch

avoid trailing spaces on output; marco@ ok.


# 1.107 27-Nov-2007 kettenis

Fix off-by-one that made us attach acpicpu0 and acpicpu1 on GENERIC kernels.

ok marco@


# 1.106 25-Nov-2007 jsg

When available and ACPI is enabled use the ACPI reset method
to reboot. This is required for some quirky sis machines
and likely others to reboot.

Tested by many. 'commit early commit often' deraadt@


# 1.105 19-Nov-2007 deraadt

sigh, amd64 still uses that stupid acpi_enabled variable...


# 1.104 19-Nov-2007 deraadt

softc's come initialized to 0, you know...


# 1.103 19-Nov-2007 deraadt

store revision; from jsg


# 1.102 19-Nov-2007 kettenis

Initialize sc_facs earlier.

ok deraadt@, beck@, canacar@


# 1.101 16-Nov-2007 deraadt

fix the bus_space #define nightmare, so that amd64 and i386 are much more
uniform. as a result shared code like acpi needs less #ifdef's
ok marco kettenis


# 1.100 15-Nov-2007 deraadt

remove the #ifdef ACPI_ENABLE. the EC codepath is either safe enough (and
will be improved), or it is unsafe because AML assumes it must be there
ok almost everyone


# 1.99 15-Nov-2007 mikeb

Make acpicpu(4) attach to the existing CPUs only.

Problem reported by form@. Fix idea by kettenis@.
Lots of help from deraadt@. Tested by deraadt@, form@ and me.

ok deraadt marco


# 1.98 14-Nov-2007 deraadt

spacing


# 1.97 12-Nov-2007 deraadt

correct : vs , in attach-time messages


# 1.96 06-Nov-2007 deraadt

all ACPI_ENABLE kernels need acpi_interrupt()


# 1.95 06-Nov-2007 deraadt

move acpi_read_pmreg() and acpi_write_pmreg() out of SMALL_KERNEL so
that acpi ramdisks can compile (obvious solution)


# 1.94 05-Nov-2007 canacar

Establish the acpi interrupt earlier. Fixes problems with some
GENERIC.MP kernels not getting acpi interrupts. This is a
workaround until the real issue with late establishment is fixed.
ok weingart@, beck@, gwk@


# 1.93 03-Nov-2007 jordan

Added support for displaying wakeup devices
ok beck@,weingart@,gwk@


# 1.92 11-Oct-2007 kettenis

When walking the tree yo find _PRT methods, check whether the device is
actually enabled and functioning. Similar to what we do for _INI.

ok weingart@, beck@


# 1.91 08-Oct-2007 krw

More simple memset(,0,) -> M_ZERO changes. In this batch move to
size(*p) as the first malloc() parameter where p is declared locally
and thus easy to check. Add M_ZERO to gpe_table allocation in acpi.c
even though there is no obvious bzero or memset nearby.


# 1.90 13-Sep-2007 mk

Remove unnecessary memset() (see aml_evalnode(9)) and a bit of KNF.

ok toby


# 1.89 13-Sep-2007 weingart

Implement ACPI 6.5.1 spec tree walk for _STA and _INI.
Fixes mk's laptop. No regressions so far (thank you to the
testers).

ok gwk@, mk@, marco@


Revision tags: OPENBSD_4_2_BASE
# 1.88 17-Apr-2007 mk

Now that acpidock no longer hangs machines during boot (at least
according to the reports I've been getting) it should be safe to enable
it again, so remove the #if 0 stuff around it.

ok marco


# 1.87 11-Apr-2007 jordan

Added changes for C-State
ok marco@


# 1.86 26-Mar-2007 marco

Calculate minutes left more accurately.

From giovanni <qgiovanni@gmail.com>


Revision tags: OPENBSD_4_1_BASE
# 1.85 03-Mar-2007 mk

Stop looking for acpidock until we fix that stuff that breaks all pre-60
series ThinkPads with acpidock(4) enabled.


# 1.84 22-Feb-2007 marco

We were getting no interrupts in .MP. There are 2 fixes; one in
acpi_machdep.c and move the interrupt establishment down after ioapic
has been initialized.

ok jordan


# 1.83 21-Feb-2007 kettenis

Make an acpi-enabled GENERIC.MP work on systems that only support PIC mode.

ok marco@


# 1.82 19-Feb-2007 jordan

Added acpi_map_address function for mapping GAS/IO registers
ok marco@


# 1.81 18-Feb-2007 jordan

Added dynamic perfect hash for opcodes
ok marco@


# 1.80 18-Feb-2007 marco

Shave off another K pre compression.
Shuffle SMALL_KERNEL functions around so that we only have a single #ifndef


# 1.79 17-Feb-2007 marco

Add SMALL_KERNEL throught acpi to make it fit on boot media.
ok deraadt


# 1.78 13-Feb-2007 marco

Add smarter _INI code. This helps with some dissapearing devices.

ok kettenis jordan


# 1.77 31-Jan-2007 gwk

Introduce acpi_hasprocfvs thats "ACPI Has Processor Frequency and Voltage
Scalling" a global flag set by acicpu attach to indicate that ACPI is
handling fvs through the _PSS objects. This will fix disappearing
EST/powernow when booting kernels with acpi enabled, as acpicpu isnt
compiled into GENERIC yet. Also in cases where acpicpu is built in and
legacy EST/powernow work but acpi is missing the _PSS object, the legacy
routines will be used.

tested by dim@, and Nick Nauwelaerts <nick AT nauwelaerts.net>
ok marco@, dim@


# 1.76 22-Jan-2007 mk

Actually look for docking stations by looking for _DCK nodes.

ok marco


# 1.75 02-Jan-2007 marco

On some systems EC must be attached as the first device. Move EC from
generic _HID devices to its own routine to force first attach. Bug found
and debugged by joris@

Code mostly written by joris@


# 1.74 26-Dec-2006 marco

Add polling to devices that require it such as acpibat and acpitz.
Use the same tsleep/wakeup mechanism as the interrupt code to keep it non-
concurrent.


# 1.73 21-Dec-2006 deraadt

even more knf love


# 1.72 21-Dec-2006 marco

Remove some leftover acpiec gpe/interrupt code.


# 1.71 20-Dec-2006 deraadt

oops, name already used


# 1.70 20-Dec-2006 deraadt

rename __acpi_enable_gpe(); ok marco


# 1.69 20-Dec-2006 deraadt

acpi_unmap_pmregs() is not used, ok marco


# 1.68 19-Dec-2006 deraadt

simplify some low-level methods substantially (shrinking code); ok marco jordan


# 1.67 06-Dec-2006 mk

malloc(..., M_NOWAIT) with no NULL check is bad. If allocation fails,
complain, clean up, and bail out of attach.

input and ok gwk


# 1.66 28-Nov-2006 jordan

Fixed clearing arg for acpi_init_pic
ok kettenis@


# 1.65 27-Nov-2006 kettenis

Don't spam dmesg.


# 1.64 27-Nov-2006 jordan

Fixes for acpiprt routing
Post-parse fixup for namerefs
ok kettenis@


# 1.63 25-Nov-2006 marco

Don't handle acpiev events inside the gpe handler since this causes deadlocks.

ok jordan@


# 1.62 15-Nov-2006 kettenis

Hook up acpi apic interrupt routing support to generic acpi code.

ok jordan@


# 1.61 02-Nov-2006 marco

Kill acpiec attach function and add its functionality to the generic _HID
device attach

prodded and ok jordan


# 1.60 02-Nov-2006 marco

Fix comments to reflect reality.


# 1.59 19-Oct-2006 marco

Replace ACPI ioctls with APM ioctls. This makes ACPI talk the same protocol
as APM therefore userspace can now retrieve power information in the same
fashion as all other apm arches (zaurus, macppc, i386 etc).

To use force the use of acpi device like so: apmd -f /dev/acpi

ok jordan@


# 1.58 19-Oct-2006 jordan

Fixed GPE debug levels
Fixed ref counter on temporary bufferfields
DSDT parsing fully working on IBM Thinkpad
ok marco@


# 1.57 15-Oct-2006 jordan

Removed old GPE handler code remnants
ok marco@


# 1.56 12-Oct-2006 jordan

Massive ACPI changes
Fixed ACPI leaks
new ACPI API
ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.55 30-Jun-2006 gwk

KNF, No functional change,
"go at it" marco@


# 1.54 30-Jun-2006 jordan

Support for calling Embedded Controller via new GPE interface
ok marco@


# 1.53 30-Jun-2006 jordan

Added new code for GPE handling
Also works with ACPIEC; need to fixup acpiec.c to remove original interrupt handler
ok marco@


# 1.52 30-Jun-2006 jordan

Added offset argument for acpi read/write pmreg routines (new GPE code)
ok marco@


# 1.51 31-May-2006 todd

do not call acpi_powerdown() unless acpi has successfully initialized
ok gwk@ canacar@


# 1.50 29-May-2006 canacar

Add embedded controller support.
ok marco@ deraadt@ testing & ok gwk@


# 1.49 19-May-2006 canacar

Minimal thermal zone driver. Monitors thermal zone temperature,
shuts down the system if the 'critical temperature' is reached.
ok marco@


# 1.48 11-Apr-2006 marco

KNF.


# 1.47 11-Apr-2006 gwk

Add acpi_resume(struct acpi_softc *sc), which calls some AML methods,
detected at attach time, currently unused.

ok marco@


# 1.46 11-Apr-2006 gwk

Discover some ACPI sleep related methods, and call them at the
appropriate time in acpi_enter_sleep_state().

ok marco@


Revision tags: OPENBSD_3_9_BASE
# 1.45 26-Feb-2006 marco

Add acpicpu as a device.

ok jordan@


# 1.44 23-Feb-2006 marco

remove dead function.

ok jordan@


# 1.43 22-Feb-2006 marco

Print out all tables it found during autoconf. We'll need this data in
the future.


# 1.42 22-Feb-2006 marco

Make dmesg less verbose.

ok jordan.


# 1.41 22-Feb-2006 jordan

Added parsing of SSDT tables
ok marco@


# 1.40 22-Feb-2006 jordan

Added new aml_register to notify by device id
Moved powerdown code to button device
ok marco@


# 1.39 21-Feb-2006 marco

Readjust debug levels that crept in too low over the last few days.
KNF


# 1.38 21-Feb-2006 marco

Rig the power button so that the machine shuts down correctly.


# 1.37 20-Feb-2006 marco

Add acpibtn device. Remains disabled for now.


# 1.36 19-Feb-2006 jordan

Added code for aml_notify GPE notification callback
ok marco@


# 1.35 19-Feb-2006 grange

Fix acpi_enter_sleep_state() by replacing current ugly hack
with mostly correct sleeping state transition code.

ok brad@ marco@


# 1.34 19-Feb-2006 marco

jordan:
Enable interrupts.
Added peripheral code to make this work.

marco:
Replace softintr and other types of interrupts with a handler with context.
Fixed interrupt magic for GPEs.
Some KNF

This is work done by jordan and I but has to be a single commit.


# 1.33 17-Feb-2006 marco

Remove earlier introduced delays. We really only need them during autconf.

ok jordan@


# 1.32 17-Feb-2006 marco

Add a generic delay function as per the ACPI spec.

from jordan@


# 1.31 17-Feb-2006 marco

Use tsleep instead of delay when !cold.


# 1.30 17-Feb-2006 marco

Argh, we need delays between reads/writes. The backend reads (i2c most likely)
are so slow that values come back as 0 sometimes. We have to do this
despite the spec saying we shouldnt have to. Guess having a faster
parser than others can hurt sometimes.

Idea from jordan.


# 1.29 17-Feb-2006 jordan

Fixed code for handling _INI
Rearranged debug statements
ok marco@


# 1.28 16-Feb-2006 jordan

Added stub for calling _INI
Fixed aml_dowhile for NULL return
ok marco@


# 1.27 16-Feb-2006 jordan

Fixed parser/evaluator to retrieve battery info+state
Cleanup of parser
Dell laptops return battery status
ok marco@


# 1.26 06-Feb-2006 brad

fix a typo so this will compile on amd64.


# 1.25 04-Feb-2006 marco

When ACPI_ENABLED wasn't set but the devices were it had an unknown variable.

Pointed out by ian@.


# 1.24 03-Feb-2006 jordan

Rewrote aml parser yet again...
Fixed bus_space_map for i386/amd64
Added _BST and _BIF to acpibat to get status/info
ok marco@


# 1.23 20-Jan-2006 jordan

More evaluator changes
Removed old evaluator code
Fixed _HID value
ok marco@


# 1.22 19-Jan-2006 jordan

Added support for read/write of PCI Config space
Help jason@
ok marco@


# 1.21 18-Jan-2006 jordan

Added new evaluation routines
Added helper methods for buffer/field evaluation
Fixed broken indentation on dsdt.c

ok marco@


# 1.20 17-Jan-2006 jordan

Major overhaul of the aml parser. Evaluater temporarily disabled.

ok marco@


# 1.19 06-Jan-2006 grange

Check for ACPI control availability only if ACPI_ENABLE defined.


# 1.18 06-Jan-2006 grange

Informative error messages on attach, prettier dmesg.


# 1.17 06-Jan-2006 grange

printf -> dnprintf conversion for debug messages.


# 1.16 05-Jan-2006 grange

Find SLP_TYPx values and store them for future use in
acpi_enter_sleep_state().


# 1.15 05-Jan-2006 grange

Random KNF and cleanup


# 1.14 05-Jan-2006 grange

Provide symbolic names for the acpi sleeping states and use it.


# 1.13 28-Dec-2005 marco

Moved DEVNAME to a acpidev.h

ok jordan@


# 1.12 18-Dec-2005 sturm

add back comment about wrong code

ok marco


# 1.11 16-Dec-2005 marco

Remove test for kbd_reset. Prompted deraadt@, ok jordan@


# 1.10 16-Dec-2005 jordan

Added hooks for powerdown/power button press


# 1.9 16-Dec-2005 jordan

Cleaned up common power management register i/o
Added enter sleep state ioctl
Hooked power button interrupt
ok marco@


# 1.8 16-Dec-2005 marco

Remove autoconf chatter


# 1.7 16-Dec-2005 jordan

Moved DSDT AML parsing into parent ACPI driver
ok marco@


# 1.6 14-Dec-2005 marco

Add AC device glue.


# 1.5 14-Dec-2005 marco

Adjust debug printing levels to something more sane.


# 1.4 13-Dec-2005 marco

Add glue to attach devices to apci.


# 1.3 07-Dec-2005 marco

KNF


Revision tags: OPENBSD_3_8_BASE
# 1.2 10-Jul-2005 grange

Spacing cleanup.


# 1.1 02-Jun-2005 tholo

Start on a basic ACPI framework -- does not do much more than read out the
ACPI tables into kernel memory and attach ACPI and HPET timers currently.

In order to test this code, enabling the devices in GENERIC as well as
the ACPI_ENABLE option is needed. This code does not do any thermal
control yet, so this should be done with care depending on the platform.

In the tree so more people can contribute to making this more fully
featured.

Ok niklas@ grange@ tedu@


# 1.367 12-May-2019 kettenis

Make sure the extended PM register descriptions actually exist before
using them. Fixes machines with ancient ACPI 1.0 (such as recent
hypervisors).

ok deraadt@


# 1.366 12-May-2019 kettenis

Add support for mapping ACPI PM registers using the "extended" gasio
representation from the FADT. Mostly fixes the Lanner NCA-1510.

ok lteo@, mlarkin@, deraadt@


# 1.365 11-May-2019 lteo

Fix incorrect error message.

ok kettenis@


# 1.364 08-May-2019 tedu

free size. the allocation is the header plus the length.


Revision tags: OPENBSD_6_5_BASE
# 1.363 04-Apr-2019 kettenis

Avoid potential null-pointer dereference. Found by Kent R. Spillner.

ok deraadt@, jsg@


# 1.362 20-Jan-2019 jsg

fix SMALL_KERNEL build after changes in rev 1.361
reported by naddy@ ok deraadt@


# 1.361 19-Jan-2019 tedu

Add a pwraction sysctl that controls what the power button does on acpi.
By default, nothing changes -- shutdown is initiated. But allows turning
power button into a sleep button if desired.
(grudging) ok from a few parties


# 1.360 26-Oct-2018 kettenis

Add acpipci(4) on amd64. For now this only calls the PCI-specific _OSC
method to let the ACPI implementation know what features we support.


Revision tags: OPENBSD_6_4_BASE
# 1.359 30-Aug-2018 kettenis

Move softbutton check before the check that skips attaching certain acpi
devices. Fixes regression caused by matching on _CID in addition to
matching on _HID.

ok matthieu@, mlarkin@


# 1.358 25-Aug-2018 kettenis

Match ACPI devices based on _CID if no match for _HID is found.

ok mpi@


# 1.357 19-Aug-2018 kettenis

Add support for multiple PCI segments. Only really implemented for arm64
for now as amd64/i386 firmware still caters for legacy OSes that only
support a single PCI segment.

ok patrick@


# 1.356 03-Aug-2018 kettenis

Let ahci(4) match on _CLS instead of _HID when attaching at acpi(4). Avoids
having to add many more _HID entries to the match table.

ok deraadt@, mlarkin@


# 1.355 10-Jul-2018 kettenis

Make legacy interrupts work in acpipci(4).

ok patrick@


# 1.354 05-Jul-2018 kettenis

Add acpipci(4), a driver that supports generic ECAM-compatible PCI host
bridges based on information provided by ACPI.

ok mlarkin@


# 1.353 04-Jul-2018 kettenis

Properly pass around the PCI "chipset tag" in acpi(4) and refactor
acpimcfg(4) to call an MD initialization functions that sets up a tag for
PCI ECAM.

ok guenther@, mlarkin@, krw@


# 1.352 01-Jul-2018 kettenis

Move acpi(4) attach glue into acpi_machdep.c.

ok guenther@, deraadt@


# 1.351 01-Jul-2018 kettenis

Add support for _DSD Device Properties.

ok mlarkin@


# 1.350 30-Jun-2018 kettenis

Add intr_enable() function, intended for MI use to amd64 and i386 and use
this in the acpi(4) suspend/resume code paths.

ok deraadt@


# 1.349 29-Jun-2018 kettenis

Use <stdint.h> types.

ok mpi@, jung@, krw@, deraadt@


# 1.348 29-Jun-2018 mlarkin

ACPI: Allow (with warning) GPE handler reassignment, instead of returning
a failure code.

ok kettenis


# 1.347 26-Jun-2018 mlarkin

knf


# 1.346 26-Jun-2018 kettenis

Let acpi(4) pass down the bus dma tag.

ok deraadt@


# 1.345 25-Jun-2018 kettenis

Reorganize acpi(4) code a bit in preparation for upcoming arm64 support.

ok deraadt@


# 1.344 20-May-2018 kettenis

Wake up the acpi thread after scheduling a gpio event.

ok mpi@


# 1.343 17-May-2018 kettenis

Sprinkle some #ifndef SMALL_KERNEL such that RAMDISK kernels cuild again.

pointed out by stsp@


# 1.342 17-May-2018 kettenis

Implement GenericSerialBus OpRegion support.

ok mlarkin@


# 1.341 27-Mar-2018 kettenis

Add acpicmos(4), a driver that implements SystemCMOS OperationRegion
access support. This fixes machines where the AML doesn't check whether
support for this OperationRegion type has been registered by the OS.

ok mlarkin@


Revision tags: OPENBSD_6_3_BASE
# 1.340 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.339 10-Feb-2018 deraadt

Syncronize filesystems to disk when suspending. Each mountpoint's vnodes
are pushed to disk. Dangling vnodes (unlinked files still in use) and
vnodes undergoing change by long-running syscalls are identified -- and
such filesystems are marked dirty on-disk while we are suspended (in case
power is lost, a fsck will be required). Filesystems without dangling or
busy vnodes are marked clean, resulting in faster boots following
"battery died" circumstances.
Tested by numerous developers, thanks for the feedback.


# 1.338 08-Feb-2018 deraadt

After flushing disks, try again to release all memory. We don't need to
save dirty memory to the hibernate space.


# 1.337 08-Feb-2018 deraadt

Toss all releaseable memory, because fragmentation can get in the way
of allocating the hibernate playpen.


# 1.336 08-Feb-2018 deraadt

sr_quiesce() is a new approach for ensuring that softraid drains
output to the disks.

This is part of a larger suspend/resume filesystem-safety diff, which
has been worked on for a couple of months already. Tests by job, krw,
beck, benno, and others. Sometimes even by snapshot users...


# 1.335 29-Nov-2017 kettenis

Add support for GPIO-signaled events.

ok mlarkin@


# 1.334 16-Nov-2017 jcs

Add PCI attachment for dwiic(4) needed by Intel 100 Series machines

ok kettenis


# 1.333 14-Nov-2017 kettenis

Add a few more PNP IDs to the skip lists.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.332 17-Aug-2017 stsp

Make dwiic(4) attach its ACPI parent dependencies.
This change makes the Asus E200HA keyboard work.

Original analysis and patch by Cesare Gargano
ok kettenis@


# 1.331 09-Aug-2017 dcoppa

Add "PNP0303" (8042 PS/2 Controller) to acpi_isa_hids[]

ok kettenis@


# 1.330 22-Jul-2017 jcs

acpibat: look for _BIX first before falling back on _BIF

ACPI 4.0 deprecated _BIF for battery status so some newer machines
are exporting _BIX which extends _BIF's package with a few extra
fields. When using _BIX, export the new discharge cycle count as a
sensor.

feedback from kettenis and anton
ok deraadt


# 1.329 20-Jul-2017 kettenis

Start a list of isa(4) device names such that we can avoid printing them
as "not configured".

ok mikeb@, millert@, deraadt@


# 1.328 08-Apr-2017 deraadt

Sensors are run as callbacks inside tasks. During suspend a sensor
could be running inside a driver that will be force-detached, or due
to tsleep end up disrupting the softstate/hardstate contract. At
suspend time, quisce all these callbacks by waiting for completion.
This issue has never been observed for real, but may be implicated in
suspend/resume failures.
ok kettenis guenther mlarkin


# 1.327 08-Apr-2017 deraadt

Two prototypes for acpi_maptable; merge to one place


Revision tags: OPENBSD_6_1_BASE
# 1.326 27-Mar-2017 deraadt

Now that hibernate_alloc() only has clean success/failure, don't
need to call hibernate_free() to clean up a partial mess.
ok mlarkin kettenis


# 1.325 13-Mar-2017 deraadt

allow ramdisks to build; ok jcs


# 1.324 12-Mar-2017 jcs

try to attach acpisbs first and if successful, don't attach acpibat


# 1.323 02-Mar-2017 natano

Add a new sysctl machdep.lidaction. The sysctl works as follows:

machdep.lidaction=0 # do nothing
machdep.lidaction=1 # suspend
machdep.lidaction=2 # hibernate

lidsuspend is just an alias for lidaction, so if you change one, the
other one will have the same value. The plan is to remove
machdep.lidsuspend eventually when people have upgraded their
/ets/sysctl.conf.

discussed with deraadt, who came up with the new MIB name
no objections mlarkin
ok stsp halex jcs


# 1.322 28-Feb-2017 natano

Untangle abstract sleep modes from ACPI sleep states. This paves the way
for further work in the area.

idea and ok deraadt
ok mlarkin


# 1.321 22-Feb-2017 jcs

add acpisbs, an acpi smart battery subsystem driver reading data
over smbus

currently disabled because it conflicts with acpibat

ok deraadt, kettenis


# 1.320 14-Jan-2017 kettenis

Revert aml_rdpciaddr changes; breaks several machines that were working
before.


# 1.319 11-Jan-2017 jsg

Increase the size of the buffers used for _HID and _CID strings to be
able to fit "Hyper_V_Gen_Counter_V1".

ok mlarkin@ deraadt@


# 1.318 08-Jan-2017 kettenis

Make aml_rdpciaddr() work for for devices behind bridges. The code now
uses the mapping between AML nodes and PCI devices that we establish.
Because _INIT methods may end up calling aml_rdpciaddr(), make sure we
create that mapping early. Also handle devices that aren't actually present.
These devices are now included in the mapping and reads will return an
all-ones patterm whereas writes are a no-op..

ok mlarkin@


# 1.317 25-Oct-2016 pirofti

Add the resource index to aml_parse_resource() callback function.

This is needed by an upcoming acpiec commit that handles machines breaking
the current ACPI specifications.

Change suggested by and ok kettenis@, guenther@


# 1.316 18-Sep-2016 guenther

If the APIC table (MADT) isn't present then ci_acpi_proc_id won't be set
when acpicpu tries to attach, leading to a NULL dereference. The ACPI
processor object on the Dells where this was reported is bogus and useless
anyway so apply the same test when deciding whether to attach to processor
objects as acpicpu would when matching them to cpus.

Problem reported by Eivind Eide (xenofil (at) gmail.com) and
Johan Huldtgren (johan+openbsd-bugs (at) huldtgren.com).
Testing by Eivind Eide.
ok kettenis@ mlarkin@


# 1.315 03-Sep-2016 naddy

Write the system time back to the RTC every 30 minutes.
This fixes the problem that long-running machines which were not
shut down properly would reboot with a badly offset system time.

hints and ok kettenis@


# 1.314 31-Aug-2016 mlarkin

Don't put a device in the wake device list if _STA says it's not there.

Fixes at least one machine that woke up from sleep / poweroff immediately.

ok kettenis@, tested by kettenis and martijn@


# 1.313 28-Jul-2016 kettenis

Store the acpi processor ID/UID in struct cpu_info, and use it to attach
acpicpu(4) drivers to the right cpu(4).

ok mlarkin@, guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.312 10-Jun-2016 kettenis

The power management timer has been made optional in ACPI 5.0A, so don't
attempt to attach acpitimer(4) if the timer isn't present.

Pointed out by jcs@

ok jcs@


# 1.311 08-May-2016 kettenis

Make sure devices listed by a device's _DEP method are attached before
we attach the device itself. The _DEP methode, introduced in ACPI 5.0,
is a hint that the device in question depends on OpRegion support from
the devices listed by _DEP, so we have to order them the proper way.
To prevent us from attaching those devices again when we encounter them
later walking down the device tree, keep track of our attempts to
attach a device driver to them using aflag in the aml_node structure.

ok guenther@


# 1.310 25-Apr-2016 pirofti

We attach acpihpet(4) and acpiec(4) without using the HID, so do not
report it in the dmesg.

OK deraadt@, mlarkin@.


# 1.309 02-Apr-2016 kettenis

Reduce dmesg spam by knocking out some acpi hardware IDs that don't need a
driver.

ok mpi@


# 1.308 29-Mar-2016 kettenis

Check if a device is present (using _STA) in acpi_foundhid. This prevents
us from reporting devices as "not configured" that aren't actually present,
and allows us to remove duplicated code from several drivers.

ok deraadt@, ok jsg@


# 1.307 29-Mar-2016 kettenis

Implement a cleaner way to prevent acpivout(4) from attaching if we have
a platform-specific method of controlling the backlight.

ok millert@, mlarkin@, jung@


# 1.306 28-Mar-2016 kettenis

Adding each and every new acpi device driver to acpi_foundhid() is
gettinga bit of the burden. And it really isn't how our autoconf
framework is supposed to work for busses that can be enumerated. All
the drivers already check for a matching _HID in their attach
function. So we can just drop the checks in acpi_foundhid() and let
the drivers sort themselves out.

Print a "not configured" line for devices for which we don't attach adriver.
This may be a bit much as there are devices for which we will not have a
driver. This will be removed if it causes too much dmesg spam.

ok deraadt@, mlarkin@


Revision tags: OPENBSD_5_9_BASE
# 1.305 17-Jan-2016 jsg

Take acpi_foundhid() outside of #ifndef SMALL_KERNEL so the ramdisk
kernel can find and attach dwiic(4) and sdhc(4).


# 1.304 17-Jan-2016 jsg

remove an unused global variable


# 1.303 14-Jan-2016 kettenis

Attach dwiic(4) on the Intel Bay Trail i2c controllers as well.


# 1.302 12-Jan-2016 kettenis

Improve emulation of PM registers on Hardware-redcued ACPI by emulating
SPL_TYPx, SLP_EN and WAK_STS using the new SLEEP_STATUS_REG and
SLEEP_CONTROL_REG registers. Makes the transition into S5 (powerdown) work
on the ASUS X205TA.

ok deraadt@, mlarkin@


# 1.301 12-Jan-2016 jcs

Add dwiic, a driver for the Synopsys DesignWare i2c controller found
on the Samsung ATIV Book 9 laptop. This initial version only
supports ACPI config/attachment.

Add ihidev, a HID-over-i2c driver largely based on uhidev. dwiic
handles attaching ihidev devices found in ACPI.

Add ims, a HID-over-i2c mouse/trackpad driver to get basic cursor
and button functionality from HID-compliant i2c trackpads.

ok kettenis deraadt


# 1.300 11-Jan-2016 kettenis

Make sdhc(4) attach to hardware IDs "80860F14" and "PNP0FFF".


# 1.299 09-Jan-2016 kettenis

Initial support for Hardware-Reduced ACPI. This skips the legacy mode checks
by emulating the PM_CNT register such that SCI_EN is always enabled. It also
skip attaching acpitimer(4) since the relevant registers are no longer there.

ok deraadt@, mlarkin@


# 1.298 06-Jan-2016 kettenis

Follow the spec and check the SCI_EN bit to see if ACPI has already
been enabled and don't bother enabling it again if it is. This should fix
systems that don't implement legacy mode and therefore set ACPI_ENABLE and
ACPI_DISABLE to zero.

ok mlarkin@, yasuoka@


# 1.297 23-Nov-2015 reyk

Back out the CRITICAL change, turn it back from 10% to 25%. Some
people with old/broken batteries didn't like this change, and the
underlying heuristics are wrong.


# 1.296 22-Nov-2015 reyk

The "CRITICAL" APM battery level, that is exported/faked by the ACPI
stack, is currently set to 25%. With 25% battery left, a typical
modern laptop will have at least one hour runtime left. It is an
arbitrary value, and other APM power layers in the kernel set CRITICAL
to something like 5% or 10%, so set it to 10% for now.

OK mlarkin@


# 1.295 28-Sep-2015 deraadt

In low-level suspend routines, set cold=2. In tsleep(), use this to
spit out a ddb trace to console. This should allow us to find suspend
or resume routines which break the rules. It depends on the console
output function being non-sleeping.... but that's another codepath which
should try to be safe when cold is set.
ok kettenis


# 1.294 06-Sep-2015 deraadt

clarify malloc arguments (base + length...)


# 1.293 28-Aug-2015 deraadt

fairly simple sizes for free(); ok tedu


# 1.292 20-Aug-2015 kettenis

Respect the access size when reading or writing to pci config space.
This version also makes sure writes are properly aligned, preventing
the panics seen with the previous incarnation of this change.

Fixes battery status passthrough in vmware, brightness keys on my x220
and who knows what else.

ok mlarkin@, deraadt@


Revision tags: OPENBSD_5_8_BASE
# 1.291 04-Aug-2015 deraadt

If we are setting ACPI_PM1_BM_RLD do it only once, and remember to
repeat this action at resume time.
ok mlarkin guenther


# 1.290 29-Jul-2015 deraadt

Something subtle in previous revision is locking up suspend or booting
on some laptops. Mailing lists threads mention x220 and Asus machines.
Reverting for now.


# 1.289 21-Jul-2015 kettenis

iRespect the access size when reading or writing to pci config space.
Fixes battery status passthrough in vmware and who knows what else.

Tested by many.

ok mlarkin@, deraadt@


# 1.288 17-Jul-2015 mlarkin

fix comments


# 1.287 26-Mar-2015 jsg

Use the 0x10 extended capability to determine if the trackstick
buttons are wired to the trackpad and need to be re-routed to the
trackstick. Without this change the buttons on 2015 thinkpads
get picked up as extended buttons that show up as scroll up/down.

Remove the X1 Carbon 2015 (LEN0048) and X250 (LEN0046) from the
top button area/soft buttons quirks list. Also avoid using the
quirk list entirely if the capability bit is set.

Based on work from jcs@ with feedback from mpi@
ok mpi@ jcs@


# 1.286 20-Mar-2015 kettenis

The ACPI standard says that a PCI function number of FFFF can be used to refer
to all functions on a device. This makes no sense in the context of accessing
PCI config space. Nevertheless stsp@ has a machine with AML that tries to
do config space reads from a device where _ADR encodes function number FFFF.
This is almost certainly buggy AML, but since Linux (and presumably Windows)
boot just fine on this machine we need to work around this. Linux effectively
treats function FFFF as function 7, which isn't present on this machine.
Instead we simulate a read from a non-existent device by returning a bit
pattern of all ones. Writes to function FFFF will still panic.

ok mlarkin@


# 1.285 16-Mar-2015 deraadt

Reduce use of SMALL_KERNEL in acpi, bringing acpiec driver and more gpe
handling into RAMDISK. This is now possible because the install media
has ample room. The goal is to reduce special cases where we may be
skipping (unknown) important operations...
ok mlarkin kettenis


# 1.284 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.283 07-Feb-2015 deraadt

New framework that allows hibernate to pass in entropy from it's fresh
boot.
ok mlarkin


# 1.282 06-Feb-2015 mlarkin

Fix a hibernate crash on some machines due to unmapping a page that
may not have been mapped previously (in the failure to hibernate case).

Also ensure that the lowmem ptp is mapped in all cases (not just MP).

ok kettenis


# 1.281 17-Jan-2015 deraadt

resume_randomness() before spinning up other cpus...


# 1.280 15-Jan-2015 jsg

Define a new wscons mouse type for Synaptics clickpad devices that lack
physical buttons. This will be used if the acpi pnpid for the mouse
matches a list provided by Synaptics found in Linux.

Combined with the xenocara changes this will remove the need for an xorg
config file for the x240/t440/t540 generation of thinkpads.

ok matthieu@ shadchin@ kettenis@


# 1.279 11-Jan-2015 kettenis

Tolerate checksum errors in ACPI tables (but complain about them). Apparently
there are machines out there with broken BIOSen with such checksum errors in
thier RSDT tables. Windows runs fine on these machines, so it probably does
not check. Linux ignores checksum errors as well (and prints a warning).

ok deraadt@, krw@


# 1.278 18-Dec-2014 deraadt

suspend and resume the RNG (at what looks like the right spots)
ok reyk djm


# 1.277 09-Dec-2014 doug

Replace some malloc(n*size,...) calls with mallocarray().

ok tedu@ deraadt@


# 1.276 06-Dec-2014 mpi

If "machdep.lidsuspend" is not set, do not go back to sleep if the lid
is still closed when resuming. This allows docked laptop to be resumed
without opening them.

ok mlarkin@


# 1.275 23-Nov-2014 mlarkin

If a machine has no _LIDs, then none can be open. This diff fixes a problem
where machines without any _LIDs were going back to sleep after hibernate
due to the recent suspend-if-lid-closed-on-resume diff. Noticed on qemu,
which doesn't have any _LIDs.

discussed with deraadt


# 1.274 18-Nov-2014 krw

Nuke yet more obvious #include duplications.

ok miod@


# 1.273 08-Nov-2014 mlarkin

If resuming from sleep (zzz/ZZZ) and the lid is still closed, go back to
sleep. This prevents accidental lid flex or slight opening in a backpack
from waking the machine up and leaving it resumed (powered on).

ok deraadt@


# 1.272 26-Sep-2014 kettenis

Rework piglet and pig allocation. Currently the piglet gets allocated
deep down in the suspend path, where it is really hard to recover from
allocation failure. So allocate the piglet early on in the suspend path.
Also change the piglet and piglet allocation functions to use km_alloc(9)
instead of doing pmemrange magic. This removes a bunch of code which, in the
case of the piglet allocation, is broken since it results in a NULL pointer
dereference. Also switch the piglet allocation to not wait. If we can't
allocate 16MB of phys contig memory on a halfway modern machine we're almost
certainly under a lot of memory pressure and we're better off not trying to
hibernate anyway.

ok mlarkin@


# 1.271 25-Sep-2014 kettenis

Bring back rev 1.249. Now that mp_setperf() has been fixed, it should be
safe to restore perflevel when resuming. Should fix the issues people
see with the processor running at full speed heating up the machine while
the apm command reports that the CPUs are running at a low speed.

ok dcoppa@, deraadt@


# 1.270 23-Sep-2014 kettenis

Release the acpi lock around calls to wsdisplay_suspend() and
wsdisplay_resume(). The X server may adjust the display brightness just
before/after the VT switch when suspending/resuming. In fact, it may
already be blocked adjusting the display brightness for some reason
unrelated to the suspend request.

The VT switch request will eventually time out, but the delay is annoying
and the failed VT switch will lead to problems after resume.

Needed for a future update of the xf86-video-intel driver.

ok mlarkin@


# 1.269 19-Sep-2014 kettenis

Use config_suspend_all(9).


# 1.268 08-Sep-2014 kettenis

Clear the PME Status bit on PCI devices in response to Device Wake events.
Fixes the interrupt storm on the 2009 Mac mini with WOL enabled on its
nfe(4) interface.

ok mlarkin@


Revision tags: OPENBSD_5_6_BASE
# 1.267 20-Jul-2014 mlarkin

Support hibernating to softraid crypto volumes.

much help and ok from deraadt@


# 1.266 16-Jul-2014 mlarkin

Reenable hibernate RLE support and flush+zero all memory after unpack.


# 1.265 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.264 12-Jul-2014 kettenis

Add a function to drop all clean pages on the page daemon queues and call
it when we hibernate.

ok mlarkin@, miod@, deraadt@


# 1.263 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.262 11-Jul-2014 mlarkin

Flush the buffercache to 16MB on hibernate and restore its previous max
size (kern.bufcachepercent) on resume, for better hibernate performance.

ok beck@


# 1.261 10-Jul-2014 blambert

remove unneeded header

ok mlarkin@


# 1.260 01-Jul-2014 kettenis

Adding an additonal delay after each access to SystemMemory or SystemIO
during autoconf makes no sense.

ok deraadt@


# 1.259 23-Jun-2014 kettenis

SystemMemory is used to access memory mapped registers on some machines, so we
must use the bus_space(9) API to access it instead of memcpy(9). Also make
sure we properly align access to fields that need more than byte alignment.

ok guenther@


# 1.258 21-May-2014 mlarkin

Fix invalid / missing format specifiers in several dnprintf statements
(used only when ACPI_DEBUG is enabled)

From a diff on tech@ by Fabian Raetz, with a minor change by me, tested
on i386 and amd64 with ACPI_DEBUG enabled and disabled.


# 1.257 25-Apr-2014 mlarkin

Defer the free of the hibernate structures until later, when it is safe to
do so.

Freeing the hibernate structures too early leads to inadvertent calls into
splx when we aren't ready for them yet. Found when trapping splx calls in
the resume path.

discussed with deraadt@ at m2k14


# 1.256 13-Apr-2014 mlarkin

Fix a badly worded debug message that implied the entire machine was going
to sleep, instead of just the acpi thread going to sleep.


# 1.255 31-Mar-2014 mpi

Change the order of operations during a suspend/resume cycle and
call bufq_quiesce() after executing the DVACT_QUIESCE handlers.

This should be safe since no disk nor controller drivers have such
handler but it will allow us to detach sd(4) devices attached to a
USB bus.

Another benefit pointed out by kettenis@ is that drivers that need
to read a firmware from the disk should be able to do it at resume
time in a DVACT_WAKEUP handler.

ok kettenis@, deraadt@


# 1.254 13-Mar-2014 dlg

get rid of the assumption that the head of the alldevs list is the
"mainbus" device. this breaks when mpath is enabled because it
attaches before mainbus and therefore takes the head position.

have autoconf provide device_mainbus() which looks up mainbus_cd,
and use that instead.

discussed with deraadt who just wants mpath stuff to move forward
despite there being many ways to shine this particular turd.


Revision tags: OPENBSD_5_5_BASE
# 1.253 21-Feb-2014 deraadt

Do the lid control further on in the resume sequence, since this is
AML, and will do WAITOK operations. While cold, that is a bad idea.
We seem to be safe with respect to the spec, I do not see any event
debounce issues here.
ok kettenis, tested by many


# 1.252 20-Jan-2014 claudio

Instead of checking sc->sc_revision <= 1 check that the
fadt->hdr_revision <= 1. This is a better check since it checks
the version of the proper descriptor. This allows the alix APU
to reboot since those boards have currently a wrong sc->sc_revision.
Committing this now so that it can be tested easier.
With and OK jsg@ also fine by derradt@


# 1.251 24-Dec-2013 kettenis

If the FADT has its SMI_CMD set to zero, assume we're only ACPI-only hardware
and don't need to disable SMI ownership of the ACPI hardware registers.

ok mlarkin@


# 1.250 23-Dec-2013 kettenis

Revert previous commit. It makes resume hang on some MP systems running
GENERIC.MP.


# 1.249 16-Dec-2013 kettenis

Restore hw.setperf upon resume like we do for apm(4) on i386. Seems we need
to do this fairly late in the resume to avoid hangs.

ok deraadt@, mlarkin@


# 1.248 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


# 1.247 06-Nov-2013 mpi

Add support for Power Resources for Dx states and the necessary hook
for PCI devices. This hook should be called twice, before and after
changing the power state of a PCI device.

Before setting the device to the new state, the ACPI layer will notify
every power resources linked to the device for that state and make sure
they are turned "_ON". After changing the state of the device, it will
decrement the reference of every power resources linked to that device
for the old state and turn them "_OFF" if they are no longer referenced.

This fixes the no-USB after resume problem seen on various ThinkPad,
problem initialy diagnosed with Alexander Polakov.

ok kettenis@, deraadt@


Revision tags: OPENBSD_5_4_BASE
# 1.246 01-Jun-2013 mlarkin

Stop printing ACPI wakeup devices in dmesg after the 16th wakeup device.

Helps with VMware VMs that report hundreds of wakeup devices.

ok reyk@, deraadt@


# 1.245 31-May-2013 bluhm

Fix the build for a kernel without wd and pciide in its config.
OK deraadt@ tedu@


# 1.244 30-May-2013 deraadt

Enforce ca_activate tree-walks over the entire heirarchy for all events,
cleaning up some shutdown-hook related code on the way.
(A few drivers related to sparc are still skipped at kettenis' request)
ok kettenis mlarkin, tested by many others too


# 1.243 18-Apr-2013 deraadt

pin the acpi thread to the BSP, because AML and SMI cannot be trusted. I
was certain we were doing this already, but it turns out no...


Revision tags: OPENBSD_5_3_BASE
# 1.242 24-Dec-2012 guenther

In acpiioctl(), Return though the splx() path if suser() returns an error

ok deraadt@


# 1.241 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


# 1.240 04-Oct-2012 ehrhardt

Use information provided by ACPI to attach secondary PCI host bridges.

ok kettenis@


# 1.239 07-Sep-2012 kettenis

Add a new function called acpi_pci_min_powerstate() that returns the lowest
power state for a PCI device for the power state (currently S3 or S4) we're
transitioning into. This function is probably too simplistic; there is a lot
of (silly) complexity in the ACPI spec.

ok deraadt@


Revision tags: OPENBSD_5_2_BASE
# 1.238 13-Jul-2012 pirofti

Disable acpivideo(4) on ASUS laptops.

There are some models that panic whenever an event is received.
Some cope, some don't. In order to get this right acpiec(4) requires
major rework. Until then let machines boot.

Discussed with deraadt@.


# 1.237 11-Jul-2012 mlarkin

for now, restrict hibernate to root in the acpi ioctl kernel interface.
normal rules for controlling this interface through apmd still apply.

ok deraadt@


# 1.236 09-Jul-2012 deraadt

tiny little oops in untested hibernate block


# 1.235 09-Jul-2012 deraadt

clean up ioctl() case statements


# 1.234 09-Jul-2012 deraadt

acpi_disable_onegpe() is not used; ok pirofti


# 1.233 24-May-2012 kettenis

If _MIN > _MAX consider a WordBus resource to be invalid. Fixes a panic on
the IBM x3550-M4.

ok mikeb@


# 1.232 29-Mar-2012 mlarkin

Missing #ifdef in previous diff


# 1.231 29-Mar-2012 mlarkin

Prevent hibernation attempts on machines with unsupported disk controllers.
The same check is also performed later in the hibernate sequence, but by
then it's too late to cleanly unwind (presently).

ok deraadt


# 1.230 26-Mar-2012 deraadt

simplify conditionals in acpi_sleep_state()
ok mlarkin


# 1.229 26-Mar-2012 deraadt

hook in the hibernate request code; half of this diff is from mlarkin
ok mlarkin


Revision tags: OPENBSD_5_1_BASE
# 1.228 20-Sep-2011 deraadt

Very early in suspend while the clock is still ticking realtime,
save the clock back to the rtc. The zaurus already did this.
ok phessler miod kettenis


Revision tags: OPENBSD_5_0_BASE
# 1.227 02-Jul-2011 nicm

kqueue attach functions should return an errno or 0, not a plain 1. Fix
the obvious cases to return EINVAL and ENXIO.

ok tedu deraadt


# 1.226 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.225 16-Jun-2011 pirofti

Import acpitoshiba driver.

This is originally written by Hiroyuki Aizu, ported and integrated
in our tree by Javier Vazquez <javama4 AT gmail> with tweaks and
suggestions by me.

Import prodded by deraadt@ and the lack of testers on tech@.


# 1.224 27-Apr-2011 jcs

attach acpithinkpad to newer lenovo models like the x120e

ok deraadt@


# 1.223 22-Apr-2011 jordan

Fix uninitialzied variables and formatting strings (-Wxxx errors)


Revision tags: OPENBSD_4_9_BASE
# 1.222 02-Jan-2011 jordan

Re-add code for ACPI taskqueue, eliminates need for state vars in acpi softc
Serialize processing ACPI tasks for sleep/powerdown/GPE event
ok deraadt@


# 1.221 31-Oct-2010 guenther

Revert last commit: it breaks resume on Thinkpads
"then please back it out" deraadt@


# 1.220 26-Oct-2010 jordan

Add task queue for ACPI gpe and notify handlers
This fixes eject on Dell Latitude dock and an issue on the Dell Mini
battery update.
ok deraadt


# 1.219 07-Oct-2010 jordan

Create separate functions for enabling wake and runtime gpes.
Remove old suspend/resume gpewalk function
ok deraadt, mlarkin


# 1.218 05-Oct-2010 jordan

split acpi_enable_onegpe to separate enable/disable functions
ok deraadt


# 1.217 05-Oct-2010 jordan

Added acpi_disable_allgpes function to clear GPE status
ok deraadt


# 1.216 05-Oct-2010 deraadt

clean up acpi_softc use more


# 1.215 05-Oct-2010 deraadt

delete a bit of #if 0 code


# 1.214 05-Oct-2010 deraadt

hide use of our global acpi_softc pointer a bit better


# 1.213 29-Sep-2010 deraadt

grab the acpi thread lock very early in acpi thread startup; not that it
is likely that anyone will race us... but why be careless.
ok kettenis


# 1.212 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.211 29-Aug-2010 jasper

- add a check for FWRITE here too.

ok miod@


# 1.210 27-Aug-2010 kettenis

Make PCI Power Management optional, and only enable it when acpi(4) attaches.

ok deraadt@


Revision tags: OPENBSD_4_8_BASE
# 1.209 08-Aug-2010 kettenis

Close race between the acpi thread and wscons ioctls running in process
context. We might rip this out post-release and replace it with a solution
that serializes things in a more obvious way.

ok deraadt@, miod@ and "those crazy texans who ok anything"


# 1.208 08-Aug-2010 deraadt

Report the acpi sleep states when on a RAMDISK; ok krw


# 1.207 07-Aug-2010 kettenis

Enforce acpi register access rescrictions.

ok mlarkin@, deraadt@, tested by marco@


# 1.206 07-Aug-2010 deraadt

upon resume, notify all the acpiac and acpibat drivers to update
their status. some laptops do not do this automatically.
ok kettenis canacar


# 1.205 06-Aug-2010 marco

Always call _PSW on _LID devices that support it to enable lid open to
resume the machine. Conversely disable it when the machine wakes up.

Tested by several ok deraadt


# 1.204 06-Aug-2010 deraadt

As part of a revamp of the PM1/GPE code, I write this diff to look for
unmanaged EN & STS bits in the PM1 register at interrupt time and report
them. As a side effect this splits the STS acknowledgement into two writes
(for power, and sleep) instead of one. The printf that is added (to spot
unmanaged STS bits) has not yet been triggered as far as we know.

Before the "write to PM1 registers at the right offset" diff went in, this
was not neccessary. But newer thinkpads do not have a working soft power
button without this diff.

We have no idea why.
ok mlarkin kettenis


# 1.203 05-Aug-2010 deraadt

PM1 power button events were generating 'fake events' to acpibtn PWRB,
but if there is no such device these were being tossed. Instead, arrange
for all power button handlers to flag a softc variable which is processed
by the thread; so that one place will know to request a powerdown (psignal
to init)
ok marco mlarkin


# 1.202 05-Aug-2010 deraadt

rename a variable which is annoyingly poorly named


# 1.201 05-Aug-2010 mlarkin

Write to the correct offset for various ACPI pmreg registers. Writing to
the correct register does properly clear the wake status bits and fixed
event bits, which allows some machines to have more than 1 suspend cycle.

bug spotted by deraadt@

tested by deraadt@ and myself on a half-dozen machines and others on
hackers@

ok deraadt@, marco@


# 1.200 05-Aug-2010 deraadt

Fix the power button. The pm1 register is 16 bits in size.
My mistake, a while back.
tested by krw


# 1.199 04-Aug-2010 kettenis

Print supported sleep states.

ok marco@, phessler@, deraadt@


# 1.198 03-Aug-2010 mlarkin

Use the proper flag for re-enabling certain hardware events (power btn,
sleep btn). The (incorrect) flag prevoiously used coincidentally had the
same value, so this shouldn't cause any different behavior than before.

tested on a variety of machines (i386, amd64, sp, mp) - no behavioral
change seen

ok deraadt@


# 1.197 28-Jul-2010 marco

Make all gasio failures look and behave the same.


# 1.196 28-Jul-2010 mlarkin

Delay a little longer on suspend before giving up. Fixes machines that
sometimes don't suspend because the sleep transition takes too long
(symptoms like failing to sleep but the backlight stays on).

ok deraadt@


# 1.195 27-Jul-2010 deraadt

remove XXX; EC can now be attached early when needed; ok jordan


# 1.194 27-Jul-2010 deraadt

more brackets feels good


# 1.193 27-Jul-2010 deraadt

XXX annotate a block in acpi_enter_sleep_state() that is pretty worrying


# 1.192 27-Jul-2010 deraadt

horrific indentation that nearly made me puke


# 1.191 27-Jul-2010 mlarkin

Set the sleeping indicator light on machines that support it, in case SMI
doesn't do it for us. Shows the 'moon' LED on older Thinkpads when
sleeping.

Tested on T43p, W500, T510 by me and on X61s by deraadt@.

ok deraadt@


# 1.190 27-Jul-2010 jordan

Early initialization of acpiec if ECDT table exists
Fixes hang when booting thinkpads while docked
ok deraadt


# 1.189 26-Jul-2010 deraadt

remove merge error which was commited


# 1.188 26-Jul-2010 pirofti

Add support for Sony ACPI hotkeys via a new driver: acpisony(4).

Currently it only works for the suspend button and tries to do right for
the brightness events, but I haven't found a Sony laptop that like to do
right so far.

In the future I want to make the brightness keys work on all Sony's and
also add support for the zoom hotkeys and whatever other funky keys I can
find on those things.

Okay deraadt@.


# 1.187 22-Jul-2010 deraadt

pretty large cleanup of SMALL_KERNEL


# 1.186 21-Jul-2010 deraadt

in the acpi GPE handler, late ack level interrupts not edge interrupts.
Duh.


# 1.185 21-Jul-2010 deraadt

Merge the edge and level gpe handlers into one (now possible since the
gpe structure tells us whether the gpe is edge or level)


# 1.184 21-Jul-2010 deraadt

delete #if 0 code we are not going to use; ok marco


# 1.183 20-Jul-2010 deraadt

make the mask code for acpi_enable_onegpe() match other similar code


# 1.182 20-Jul-2010 deraadt

Handle the acpi interrupt controller with proper edge/level handling,
wrapped in spltty to avoid racing against the interrupt controller.
Repair the gpe bit masking code while there, and do operations in the
right order
ok kettenis mlarkin, help from jordan at figuring out the order of
operations.


# 1.181 20-Jul-2010 matthew

When we're preparing to go to sleep, only call bufq_restart() if other
preparations failed.

ok kettenis@, thib@, deraadt@


# 1.180 19-Jul-2010 deraadt

permit compilation with ACPI_DEBUG


# 1.179 19-Jul-2010 deraadt

there is no need for a seperate dsdt_softc pointer; dsdt does not have a
softc but is just a part of the single acpi driver.
ok jordan


# 1.178 19-Jul-2010 deraadt

acpi_delay() is no longer needed; ok jordan


# 1.177 18-Jul-2010 mlarkin

Clear event status on resume, to avoid going back to sleep/reboot/shutdown
immediately after resume. Tested by a bunch of people on hackers@ on
various hardware. Fixes at least Toshiba NB200 and Gateway LT3103u.

ok marco


# 1.176 13-Jul-2010 deraadt

rename some thread related interfaces so they are logical


# 1.175 10-Jul-2010 jordan

Verify _ADR bdf is correct (some systems return 0xffff)
Works on Dell x9xx and Studio 1555


# 1.174 09-Jul-2010 jordan

Clear the noise


# 1.173 08-Jul-2010 jordan

Add mapping for ACPI device to PCI bdf (match autoconf tree)
Simplify resource parsing function to use buffer argument
Convert namespace linked lists to use queue macros
ok marco@, deraadt@


# 1.172 06-Jul-2010 deraadt

Use spltty() to lock downcalls from apm against the information being
modified by the acpi thread.
ok kettenis


# 1.171 06-Jul-2010 deraadt

Don't use a workq to do the suspend, because it races aginst the acpi
thread. Instead, just tell the acpi thread to do the suspend for us.
This makes apmd & zzz work correctly. While here, have acpithinkpad
attempt to post the event to apm before prompting the suspend itself.
ok kettenis marco mlarkin


# 1.170 05-Jul-2010 mlarkin

Reset register SCI_EN on resume. The ACPI spec clearly states we don't have
to do this, but many machines don't, or forget to, which leads to all sorts
of problems on resume (broken battery stats, overheating, failure to resume
more than once, etc).

tested by many, ok marco@, deraadt@


# 1.169 01-Jul-2010 thib

do bufq quiesce again, since vnd/the bufq_done call in
biodone has been fixed.


# 1.168 01-Jul-2010 jordan

Backout recent AML changes, breaks on R210 and others


# 1.167 01-Jul-2010 jordan

Simplified aml_parse_resource call, cleanup code
ok marco


# 1.166 01-Jul-2010 jordan

Fix subordinate bus # for multi-root PCI buses
ok marco, kettenis


# 1.165 30-Jun-2010 thib

Disable/partially backout the bufq quiesce changes as this
is causing havoc with vnds and release must be buildable.


# 1.164 29-Jun-2010 deraadt

Fix ramdisks (new code was placed inside a SMALL_KERNEL chunk)


# 1.163 29-Jun-2010 jordan

Add support for mapping ACPI to PCI devices
ok kettenis, deraadt


# 1.162 29-Jun-2010 kettenis

Call bufq_quiesce() on suspend to stop all I/O and bufq_restart on resume.
Makes suspend/resume work much better while doing disk I/O.

ok mlarkin@


# 1.161 29-Jun-2010 tedu

60 is a more reasonable guess for number of minutes in an hour than 100.
spotted by TeXitoi. ok deraadt


# 1.160 27-Jun-2010 jordan

Add _acpi_matchhids routine, precursor to pci device match


# 1.159 27-Jun-2010 mlarkin

Clear ACPI fixed event status on resume (power buttons/etc) so that some
machines don't immediately resume after suspending to S3.

Tested by kettenis@ and I on 5 or 6 machines, deraadt@ "yeah, looks good"


# 1.158 27-Jun-2010 jordan

Cleaned up acpi_maptable, adds to table list directly


# 1.157 07-Apr-2010 deraadt

If we have done a wsdisplay_suspend(), and ACPI decides it does not want to
suspend, we must roll back by calling wsdisplay_resume() or the virtual
console switching will be locked forever
ok oga


# 1.156 07-Apr-2010 kettenis

Make suspend/resume work on MP machines (running an MP kernel). Joint work
from mlarkin@ and me, with some amd64 fixes thrown in by deraadt@

ok marco@, deraadt@, pirofti@, mlarkin@


# 1.155 31-Mar-2010 kettenis

Bring /dev/apm support in line with apm(4). Make the suspend button send
a suspend request event to apmd(8) instead of suspending immediately. Also
keep track of whether /dev/apm and /dev/apmctl are currently open, such that
we can still suspend immediately if apmd(8) isn't running.

ok deraadt@, marco@, pirofti@, jsing@, oga@


# 1.154 30-Mar-2010 oga

Prevent the apmd/x races for good.

When we hit suspend time, go through all wsdisplays on the system. if
they are in mode MAPPED, but not MODE_DUMBFB then if possible do a full
vt switch to a !mapped vt, and prevent switching back until resume time.
This has to be called from MD code because this involves userland
running so that X can run the vt switch signal handler. This way, any
case where we are using the "poke registers from userland" model, we
will not be on the hardware when we go down, so the kernel can actually
handle thing properly.

Tested on several acpi laptops (by kettenis@ and ian@), x40 (me and
beck@ at LEAST) and zaurus (me). Maybe others, but if so I forgot who at
this time..

Idea from deraadt somewhere over the Faroe Islands (I thought of a
similar thing myself a while ago). Much prompting from him. Ok and
comments miod@


# 1.153 25-Mar-2010 oga

Fix & vs. && in the kqfilter code.

Based on a comparison with the apm code.

ok deraadt@, kettenis@


Revision tags: OPENBSD_4_7_BASE
# 1.152 26-Nov-2009 mlarkin

Visual (and on some machines, audio) feedback while the machine is
resuming via calls to aml _SST method. This also makes the "moon" LED on
thinkpads to blink during resume and cease when resume is completed.

ok pirofti@


# 1.151 26-Nov-2009 deraadt

Toggle into "cold" when we we disable interrupts, because tsleeping
in the AML interpreter and context switching into userland processes
tends to sometimes pop the ACPI trampoline page out of our address
space. Makes lots more machines work.
ok kettenis mlarkin


# 1.150 24-Nov-2009 jsg

in the resume path evaluate _WAK before dealing with device drivers
like the spec says, not after.

ok mlarkin@


# 1.149 24-Nov-2009 deraadt

abort suspends on SMP systems until that gets written; ok mlarkin


# 1.148 23-Nov-2009 mlarkin

pci should return the result of it's children's suspend/resume calls back
to its parent.

handle suspend failure case and unwind if devices have an activate function
that returns failure.

ok deraadt@, kettenis@


# 1.147 23-Nov-2009 mlarkin

acpi* devices need to call their children on suspend/resume.
ok deraadt@


# 1.146 23-Nov-2009 pirofti

Remove ACPI_SLEEP_ENABLED checks.

This enables by default the suspend/resume paths in the kernel.

Okay deraadt@.


# 1.145 23-Nov-2009 deraadt

knf and spacing that are super annoying


# 1.144 23-Nov-2009 mlarkin

Hook up Theo's new children suspend/resume function, as well as more ACPI
suspend/resume glue.

ok deraadt@


# 1.143 26-Oct-2009 deraadt

Do not do strncmp comparisons on dv_xname because longer device names which
look similar could arrive in the future. Instead, compare directly against
dv_cfdata->cf_driver->cd_name
Issue originally spotted by miod


# 1.142 26-Oct-2009 jordan

Verify checksum+address when loading ACPI tables.
Some systems had invalid entries in RSDT/XSDT.
ok marco@


# 1.141 23-Jul-2009 cnst

New aibs(4) driver for ASUSTeK AI Booster (ACPI ATK0110) hardware monitoring,
with sensor state support through limits provided by the ACPI.

Tested on several ASUS motherboards kindly networked by Sam Fourman Jr.

ok deraadt marco jordan


Revision tags: OPENBSD_4_6_BASE
# 1.140 03-Jun-2009 pirofti

Import acpi power resource driver. This is needed for suspend/resume logic.

Okay jordan@ and marco@.


# 1.139 03-Jun-2009 pirofti

Add acpivideo support. This adds brightness support for all laptops
except thinkpads, they will use the acpithinkpad driver. The driver is
also hooked into wsconsole. So brightness can be adjusted via:

$ wsconsctl display.brightness=<percentage>

This is very helpfull on some laptops that have a nasty bios and get two
steps instead of one when pressing the brightness button.

Tested on various dell, fujitsu, acer, samsung and other laptops.

Okay marco@, miod@. Suggestions from kettenis@.
Lots of reviews and help from miod@, thanks!


# 1.138 03-Jun-2009 jordan

Added ejectable bay for IDE cdroms
ok marco@


# 1.137 30-Apr-2009 marco

Oga changed apm ioctls and forgot to grep. Temporary work around by hitting
the sleep button when we get any request. This interface will chnage at
some point.
ok oga


# 1.136 19-Apr-2009 krw

Call acpi_sleep_walk() when powering down just like when entering
any other sleep state. This tells acpi wake devices to not wake up
the box until manual powering up resets their status. Makes my Dell
GX520 stay powered off in response to 'halt -p'.

In snaps for a while.

ok marco@ (several times) deraadt@


# 1.135 17-Apr-2009 pirofti

Add device ranking support for future sleep/wake developments.

okay marco@


# 1.134 11-Mar-2009 jordan

Cleanup.. use aml_evalinteger instead of aml_evalname
ok marco@


# 1.133 10-Mar-2009 jordan

Fix cut/paste error.. call _BFS not _PTS
ok marco@


Revision tags: OPENBSD_4_5_BASE
# 1.132 21-Feb-2009 marco

overzealous cut/paste; found by Vladimir Kirillov <proger@uaoug.org.ua>


# 1.131 20-Feb-2009 marco

Oops didn't mean for Debugger() to remain there.


# 1.130 19-Feb-2009 marco

suspend/resume bits so that we can develop this in tree. This is disabled.
code from mlarkin and me
help from art,toby,jordan and several others
ok jordan, go for it deraadt


# 1.129 10-Feb-2009 jordan

Fix null pointer for shutdown if acpi not enabled
ok marco@


# 1.128 04-Feb-2009 kettenis

Some systems that claim to be ACPI 2.0 apparently don't provide an XSDT
address. Make the code fall back on the RSDT on those systems.

From Joerg Niendorf <p3y80@internode.on.net>. Fixes PR 5986.


# 1.127 17-Jan-2009 krw

A couple of stray malloc/memset -> malloc(M_ZERO), a bit of knf and
lose a couple of unnecessary casts. No functional change.

Feedback from kettenis@

ok kettenis@ marco@


# 1.126 06-Nov-2008 marco

Collapse all sanity checks of hids into one function.

Originaly from jordan


Revision tags: OPENBSD_4_4_BASE
# 1.125 02-Jul-2008 fgsch

add acpivideo. for now almost an empty skel so others can work on
suspend/resume. eventually it will also manage output switching and
brightness where supported. prodded by marco@


# 1.124 11-Jun-2008 marco

Fix $OpenBSD$ for once and for all


# 1.123 08-Jun-2008 deraadt

aaa_dev was being set from an uninitialized variable; ok toby


# 1.122 07-Jun-2008 marco

Kill ACPIVERBOSE. It was never used and is worthless to boot.

comments jmc
ok toby


# 1.121 06-Jun-2008 marco

Shrink acpi some more for floppies and while here re-enable acpi on the
floppies. Fixes crazy sony vaio kettenis has.

tested by deraadt
ok deraadt jordan kettenis


# 1.120 01-Jun-2008 marco

Fix \n nit in SMALL_KERNEL.


# 1.119 01-Jun-2008 marco

Re-enable new parser. Lots of testing by various folks. Fits all media
now too.

ok kettenis deraadt


# 1.118 16-May-2008 dlg

back out jordans new parser.

it didnt have enough oks from the right people, it did break machines
that were previously working, and it bloated the RAMDISKS and install
media beyond the constraints of the media.

requested by deraadt@


# 1.117 14-May-2008 jordan

Adding new ACPI Parser code
Old guts of code still exists, needs to be torn out next
ok marco@
ok brad@


# 1.116 27-Apr-2008 jcs

add acpithinkpad(4), an acpi support driver for ibm/lenovo
thinkpads.

provides functionality that the bios did on older, non-acpi
thinkpads like respond to screen brightness keys and toggle the
bluetooth device on and off.

ok deraadt@ and marco@


# 1.115 24-Apr-2008 jsing

Introduce acpiasus(4), a driver for the ACPI based hotkeys found in many
ASUS laptops (including the ASUS EeePC) - largely based on NetBSD's
asus(4) driver. On the ASUS EeePC this allows us to enable/disable
wireless, change screen brightness and use the volume keys.

ok jsg@, weingart@


# 1.114 12-Apr-2008 kettenis

Get rid of acpi_s5 global variables; simply send SIGUSR2 instead of SUGUSR1
to tell init(8) to power down the machine.

ok krw@


Revision tags: OPENBSD_4_3_BASE
# 1.113 21-Jan-2008 jsg

Check against RESET_REG_SUP in FADT before doing the acpi reset dance
after all, not doing so appears to break more machines than it fixes.

ok marco@


# 1.112 05-Jan-2008 kettenis

Never attach more acpicpu(4) devices than cpu(4) devices. Always attach
the first acpicpu(4) device on non-MULTIPROCESSOR kernels, regardless of its
ACPI processor ID.

ok mikeb@


# 1.111 05-Dec-2007 deraadt

Hang acpi(4) below bios(4) instead of mainbus(4). This lets us move acpi(4)
before pcibios(4), and then.... if acpi(4) attaches, skip pcibios(4) since
it messes the machines up. Fixes claudio's HP dl320 G5, and almost assuredly
others.
ok kettenis


# 1.110 05-Dec-2007 deraadt

repair acpiopen/acpiclose and friends to handle the /dev/apmctl interface,
so that apmd(8) can actually work correctly against acpi(4), and thus,
even do the fancy -C/-A stuff; also tested by jmc


# 1.109 02-Dec-2007 fgsch

fix the trailing space for real now.


# 1.108 02-Dec-2007 fgsch

avoid trailing spaces on output; marco@ ok.


# 1.107 27-Nov-2007 kettenis

Fix off-by-one that made us attach acpicpu0 and acpicpu1 on GENERIC kernels.

ok marco@


# 1.106 25-Nov-2007 jsg

When available and ACPI is enabled use the ACPI reset method
to reboot. This is required for some quirky sis machines
and likely others to reboot.

Tested by many. 'commit early commit often' deraadt@


# 1.105 19-Nov-2007 deraadt

sigh, amd64 still uses that stupid acpi_enabled variable...


# 1.104 19-Nov-2007 deraadt

softc's come initialized to 0, you know...


# 1.103 19-Nov-2007 deraadt

store revision; from jsg


# 1.102 19-Nov-2007 kettenis

Initialize sc_facs earlier.

ok deraadt@, beck@, canacar@


# 1.101 16-Nov-2007 deraadt

fix the bus_space #define nightmare, so that amd64 and i386 are much more
uniform. as a result shared code like acpi needs less #ifdef's
ok marco kettenis


# 1.100 15-Nov-2007 deraadt

remove the #ifdef ACPI_ENABLE. the EC codepath is either safe enough (and
will be improved), or it is unsafe because AML assumes it must be there
ok almost everyone


# 1.99 15-Nov-2007 mikeb

Make acpicpu(4) attach to the existing CPUs only.

Problem reported by form@. Fix idea by kettenis@.
Lots of help from deraadt@. Tested by deraadt@, form@ and me.

ok deraadt marco


# 1.98 14-Nov-2007 deraadt

spacing


# 1.97 12-Nov-2007 deraadt

correct : vs , in attach-time messages


# 1.96 06-Nov-2007 deraadt

all ACPI_ENABLE kernels need acpi_interrupt()


# 1.95 06-Nov-2007 deraadt

move acpi_read_pmreg() and acpi_write_pmreg() out of SMALL_KERNEL so
that acpi ramdisks can compile (obvious solution)


# 1.94 05-Nov-2007 canacar

Establish the acpi interrupt earlier. Fixes problems with some
GENERIC.MP kernels not getting acpi interrupts. This is a
workaround until the real issue with late establishment is fixed.
ok weingart@, beck@, gwk@


# 1.93 03-Nov-2007 jordan

Added support for displaying wakeup devices
ok beck@,weingart@,gwk@


# 1.92 11-Oct-2007 kettenis

When walking the tree yo find _PRT methods, check whether the device is
actually enabled and functioning. Similar to what we do for _INI.

ok weingart@, beck@


# 1.91 08-Oct-2007 krw

More simple memset(,0,) -> M_ZERO changes. In this batch move to
size(*p) as the first malloc() parameter where p is declared locally
and thus easy to check. Add M_ZERO to gpe_table allocation in acpi.c
even though there is no obvious bzero or memset nearby.


# 1.90 13-Sep-2007 mk

Remove unnecessary memset() (see aml_evalnode(9)) and a bit of KNF.

ok toby


# 1.89 13-Sep-2007 weingart

Implement ACPI 6.5.1 spec tree walk for _STA and _INI.
Fixes mk's laptop. No regressions so far (thank you to the
testers).

ok gwk@, mk@, marco@


Revision tags: OPENBSD_4_2_BASE
# 1.88 17-Apr-2007 mk

Now that acpidock no longer hangs machines during boot (at least
according to the reports I've been getting) it should be safe to enable
it again, so remove the #if 0 stuff around it.

ok marco


# 1.87 11-Apr-2007 jordan

Added changes for C-State
ok marco@


# 1.86 26-Mar-2007 marco

Calculate minutes left more accurately.

From giovanni <qgiovanni@gmail.com>


Revision tags: OPENBSD_4_1_BASE
# 1.85 03-Mar-2007 mk

Stop looking for acpidock until we fix that stuff that breaks all pre-60
series ThinkPads with acpidock(4) enabled.


# 1.84 22-Feb-2007 marco

We were getting no interrupts in .MP. There are 2 fixes; one in
acpi_machdep.c and move the interrupt establishment down after ioapic
has been initialized.

ok jordan


# 1.83 21-Feb-2007 kettenis

Make an acpi-enabled GENERIC.MP work on systems that only support PIC mode.

ok marco@


# 1.82 19-Feb-2007 jordan

Added acpi_map_address function for mapping GAS/IO registers
ok marco@


# 1.81 18-Feb-2007 jordan

Added dynamic perfect hash for opcodes
ok marco@


# 1.80 18-Feb-2007 marco

Shave off another K pre compression.
Shuffle SMALL_KERNEL functions around so that we only have a single #ifndef


# 1.79 17-Feb-2007 marco

Add SMALL_KERNEL throught acpi to make it fit on boot media.
ok deraadt


# 1.78 13-Feb-2007 marco

Add smarter _INI code. This helps with some dissapearing devices.

ok kettenis jordan


# 1.77 31-Jan-2007 gwk

Introduce acpi_hasprocfvs thats "ACPI Has Processor Frequency and Voltage
Scalling" a global flag set by acicpu attach to indicate that ACPI is
handling fvs through the _PSS objects. This will fix disappearing
EST/powernow when booting kernels with acpi enabled, as acpicpu isnt
compiled into GENERIC yet. Also in cases where acpicpu is built in and
legacy EST/powernow work but acpi is missing the _PSS object, the legacy
routines will be used.

tested by dim@, and Nick Nauwelaerts <nick AT nauwelaerts.net>
ok marco@, dim@


# 1.76 22-Jan-2007 mk

Actually look for docking stations by looking for _DCK nodes.

ok marco


# 1.75 02-Jan-2007 marco

On some systems EC must be attached as the first device. Move EC from
generic _HID devices to its own routine to force first attach. Bug found
and debugged by joris@

Code mostly written by joris@


# 1.74 26-Dec-2006 marco

Add polling to devices that require it such as acpibat and acpitz.
Use the same tsleep/wakeup mechanism as the interrupt code to keep it non-
concurrent.


# 1.73 21-Dec-2006 deraadt

even more knf love


# 1.72 21-Dec-2006 marco

Remove some leftover acpiec gpe/interrupt code.


# 1.71 20-Dec-2006 deraadt

oops, name already used


# 1.70 20-Dec-2006 deraadt

rename __acpi_enable_gpe(); ok marco


# 1.69 20-Dec-2006 deraadt

acpi_unmap_pmregs() is not used, ok marco


# 1.68 19-Dec-2006 deraadt

simplify some low-level methods substantially (shrinking code); ok marco jordan


# 1.67 06-Dec-2006 mk

malloc(..., M_NOWAIT) with no NULL check is bad. If allocation fails,
complain, clean up, and bail out of attach.

input and ok gwk


# 1.66 28-Nov-2006 jordan

Fixed clearing arg for acpi_init_pic
ok kettenis@


# 1.65 27-Nov-2006 kettenis

Don't spam dmesg.


# 1.64 27-Nov-2006 jordan

Fixes for acpiprt routing
Post-parse fixup for namerefs
ok kettenis@


# 1.63 25-Nov-2006 marco

Don't handle acpiev events inside the gpe handler since this causes deadlocks.

ok jordan@


# 1.62 15-Nov-2006 kettenis

Hook up acpi apic interrupt routing support to generic acpi code.

ok jordan@


# 1.61 02-Nov-2006 marco

Kill acpiec attach function and add its functionality to the generic _HID
device attach

prodded and ok jordan


# 1.60 02-Nov-2006 marco

Fix comments to reflect reality.


# 1.59 19-Oct-2006 marco

Replace ACPI ioctls with APM ioctls. This makes ACPI talk the same protocol
as APM therefore userspace can now retrieve power information in the same
fashion as all other apm arches (zaurus, macppc, i386 etc).

To use force the use of acpi device like so: apmd -f /dev/acpi

ok jordan@


# 1.58 19-Oct-2006 jordan

Fixed GPE debug levels
Fixed ref counter on temporary bufferfields
DSDT parsing fully working on IBM Thinkpad
ok marco@


# 1.57 15-Oct-2006 jordan

Removed old GPE handler code remnants
ok marco@


# 1.56 12-Oct-2006 jordan

Massive ACPI changes
Fixed ACPI leaks
new ACPI API
ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.55 30-Jun-2006 gwk

KNF, No functional change,
"go at it" marco@


# 1.54 30-Jun-2006 jordan

Support for calling Embedded Controller via new GPE interface
ok marco@


# 1.53 30-Jun-2006 jordan

Added new code for GPE handling
Also works with ACPIEC; need to fixup acpiec.c to remove original interrupt handler
ok marco@


# 1.52 30-Jun-2006 jordan

Added offset argument for acpi read/write pmreg routines (new GPE code)
ok marco@


# 1.51 31-May-2006 todd

do not call acpi_powerdown() unless acpi has successfully initialized
ok gwk@ canacar@


# 1.50 29-May-2006 canacar

Add embedded controller support.
ok marco@ deraadt@ testing & ok gwk@


# 1.49 19-May-2006 canacar

Minimal thermal zone driver. Monitors thermal zone temperature,
shuts down the system if the 'critical temperature' is reached.
ok marco@


# 1.48 11-Apr-2006 marco

KNF.


# 1.47 11-Apr-2006 gwk

Add acpi_resume(struct acpi_softc *sc), which calls some AML methods,
detected at attach time, currently unused.

ok marco@


# 1.46 11-Apr-2006 gwk

Discover some ACPI sleep related methods, and call them at the
appropriate time in acpi_enter_sleep_state().

ok marco@


Revision tags: OPENBSD_3_9_BASE
# 1.45 26-Feb-2006 marco

Add acpicpu as a device.

ok jordan@


# 1.44 23-Feb-2006 marco

remove dead function.

ok jordan@


# 1.43 22-Feb-2006 marco

Print out all tables it found during autoconf. We'll need this data in
the future.


# 1.42 22-Feb-2006 marco

Make dmesg less verbose.

ok jordan.


# 1.41 22-Feb-2006 jordan

Added parsing of SSDT tables
ok marco@


# 1.40 22-Feb-2006 jordan

Added new aml_register to notify by device id
Moved powerdown code to button device
ok marco@


# 1.39 21-Feb-2006 marco

Readjust debug levels that crept in too low over the last few days.
KNF


# 1.38 21-Feb-2006 marco

Rig the power button so that the machine shuts down correctly.


# 1.37 20-Feb-2006 marco

Add acpibtn device. Remains disabled for now.


# 1.36 19-Feb-2006 jordan

Added code for aml_notify GPE notification callback
ok marco@


# 1.35 19-Feb-2006 grange

Fix acpi_enter_sleep_state() by replacing current ugly hack
with mostly correct sleeping state transition code.

ok brad@ marco@


# 1.34 19-Feb-2006 marco

jordan:
Enable interrupts.
Added peripheral code to make this work.

marco:
Replace softintr and other types of interrupts with a handler with context.
Fixed interrupt magic for GPEs.
Some KNF

This is work done by jordan and I but has to be a single commit.


# 1.33 17-Feb-2006 marco

Remove earlier introduced delays. We really only need them during autconf.

ok jordan@


# 1.32 17-Feb-2006 marco

Add a generic delay function as per the ACPI spec.

from jordan@


# 1.31 17-Feb-2006 marco

Use tsleep instead of delay when !cold.


# 1.30 17-Feb-2006 marco

Argh, we need delays between reads/writes. The backend reads (i2c most likely)
are so slow that values come back as 0 sometimes. We have to do this
despite the spec saying we shouldnt have to. Guess having a faster
parser than others can hurt sometimes.

Idea from jordan.


# 1.29 17-Feb-2006 jordan

Fixed code for handling _INI
Rearranged debug statements
ok marco@


# 1.28 16-Feb-2006 jordan

Added stub for calling _INI
Fixed aml_dowhile for NULL return
ok marco@


# 1.27 16-Feb-2006 jordan

Fixed parser/evaluator to retrieve battery info+state
Cleanup of parser
Dell laptops return battery status
ok marco@


# 1.26 06-Feb-2006 brad

fix a typo so this will compile on amd64.


# 1.25 04-Feb-2006 marco

When ACPI_ENABLED wasn't set but the devices were it had an unknown variable.

Pointed out by ian@.


# 1.24 03-Feb-2006 jordan

Rewrote aml parser yet again...
Fixed bus_space_map for i386/amd64
Added _BST and _BIF to acpibat to get status/info
ok marco@


# 1.23 20-Jan-2006 jordan

More evaluator changes
Removed old evaluator code
Fixed _HID value
ok marco@


# 1.22 19-Jan-2006 jordan

Added support for read/write of PCI Config space
Help jason@
ok marco@


# 1.21 18-Jan-2006 jordan

Added new evaluation routines
Added helper methods for buffer/field evaluation
Fixed broken indentation on dsdt.c

ok marco@


# 1.20 17-Jan-2006 jordan

Major overhaul of the aml parser. Evaluater temporarily disabled.

ok marco@


# 1.19 06-Jan-2006 grange

Check for ACPI control availability only if ACPI_ENABLE defined.


# 1.18 06-Jan-2006 grange

Informative error messages on attach, prettier dmesg.


# 1.17 06-Jan-2006 grange

printf -> dnprintf conversion for debug messages.


# 1.16 05-Jan-2006 grange

Find SLP_TYPx values and store them for future use in
acpi_enter_sleep_state().


# 1.15 05-Jan-2006 grange

Random KNF and cleanup


# 1.14 05-Jan-2006 grange

Provide symbolic names for the acpi sleeping states and use it.


# 1.13 28-Dec-2005 marco

Moved DEVNAME to a acpidev.h

ok jordan@


# 1.12 18-Dec-2005 sturm

add back comment about wrong code

ok marco


# 1.11 16-Dec-2005 marco

Remove test for kbd_reset. Prompted deraadt@, ok jordan@


# 1.10 16-Dec-2005 jordan

Added hooks for powerdown/power button press


# 1.9 16-Dec-2005 jordan

Cleaned up common power management register i/o
Added enter sleep state ioctl
Hooked power button interrupt
ok marco@


# 1.8 16-Dec-2005 marco

Remove autoconf chatter


# 1.7 16-Dec-2005 jordan

Moved DSDT AML parsing into parent ACPI driver
ok marco@


# 1.6 14-Dec-2005 marco

Add AC device glue.


# 1.5 14-Dec-2005 marco

Adjust debug printing levels to something more sane.


# 1.4 13-Dec-2005 marco

Add glue to attach devices to apci.


# 1.3 07-Dec-2005 marco

KNF


Revision tags: OPENBSD_3_8_BASE
# 1.2 10-Jul-2005 grange

Spacing cleanup.


# 1.1 02-Jun-2005 tholo

Start on a basic ACPI framework -- does not do much more than read out the
ACPI tables into kernel memory and attach ACPI and HPET timers currently.

In order to test this code, enabling the devices in GENERIC as well as
the ACPI_ENABLE option is needed. This code does not do any thermal
control yet, so this should be done with care depending on the platform.

In the tree so more people can contribute to making this more fully
featured.

Ok niklas@ grange@ tedu@


# 1.366 12-May-2019 kettenis

Add support for mapping ACPI PM registers using the "extended" gasio
representation from the FADT. Mostly fixes the Lanner NCA-1510.

ok lteo@, mlarkin@, deraadt@


# 1.365 11-May-2019 lteo

Fix incorrect error message.

ok kettenis@


# 1.364 08-May-2019 tedu

free size. the allocation is the header plus the length.


Revision tags: OPENBSD_6_5_BASE
# 1.363 04-Apr-2019 kettenis

Avoid potential null-pointer dereference. Found by Kent R. Spillner.

ok deraadt@, jsg@


# 1.362 20-Jan-2019 jsg

fix SMALL_KERNEL build after changes in rev 1.361
reported by naddy@ ok deraadt@


# 1.361 19-Jan-2019 tedu

Add a pwraction sysctl that controls what the power button does on acpi.
By default, nothing changes -- shutdown is initiated. But allows turning
power button into a sleep button if desired.
(grudging) ok from a few parties


# 1.360 26-Oct-2018 kettenis

Add acpipci(4) on amd64. For now this only calls the PCI-specific _OSC
method to let the ACPI implementation know what features we support.


Revision tags: OPENBSD_6_4_BASE
# 1.359 30-Aug-2018 kettenis

Move softbutton check before the check that skips attaching certain acpi
devices. Fixes regression caused by matching on _CID in addition to
matching on _HID.

ok matthieu@, mlarkin@


# 1.358 25-Aug-2018 kettenis

Match ACPI devices based on _CID if no match for _HID is found.

ok mpi@


# 1.357 19-Aug-2018 kettenis

Add support for multiple PCI segments. Only really implemented for arm64
for now as amd64/i386 firmware still caters for legacy OSes that only
support a single PCI segment.

ok patrick@


# 1.356 03-Aug-2018 kettenis

Let ahci(4) match on _CLS instead of _HID when attaching at acpi(4). Avoids
having to add many more _HID entries to the match table.

ok deraadt@, mlarkin@


# 1.355 10-Jul-2018 kettenis

Make legacy interrupts work in acpipci(4).

ok patrick@


# 1.354 05-Jul-2018 kettenis

Add acpipci(4), a driver that supports generic ECAM-compatible PCI host
bridges based on information provided by ACPI.

ok mlarkin@


# 1.353 04-Jul-2018 kettenis

Properly pass around the PCI "chipset tag" in acpi(4) and refactor
acpimcfg(4) to call an MD initialization functions that sets up a tag for
PCI ECAM.

ok guenther@, mlarkin@, krw@


# 1.352 01-Jul-2018 kettenis

Move acpi(4) attach glue into acpi_machdep.c.

ok guenther@, deraadt@


# 1.351 01-Jul-2018 kettenis

Add support for _DSD Device Properties.

ok mlarkin@


# 1.350 30-Jun-2018 kettenis

Add intr_enable() function, intended for MI use to amd64 and i386 and use
this in the acpi(4) suspend/resume code paths.

ok deraadt@


# 1.349 29-Jun-2018 kettenis

Use <stdint.h> types.

ok mpi@, jung@, krw@, deraadt@


# 1.348 29-Jun-2018 mlarkin

ACPI: Allow (with warning) GPE handler reassignment, instead of returning
a failure code.

ok kettenis


# 1.347 26-Jun-2018 mlarkin

knf


# 1.346 26-Jun-2018 kettenis

Let acpi(4) pass down the bus dma tag.

ok deraadt@


# 1.345 25-Jun-2018 kettenis

Reorganize acpi(4) code a bit in preparation for upcoming arm64 support.

ok deraadt@


# 1.344 20-May-2018 kettenis

Wake up the acpi thread after scheduling a gpio event.

ok mpi@


# 1.343 17-May-2018 kettenis

Sprinkle some #ifndef SMALL_KERNEL such that RAMDISK kernels cuild again.

pointed out by stsp@


# 1.342 17-May-2018 kettenis

Implement GenericSerialBus OpRegion support.

ok mlarkin@


# 1.341 27-Mar-2018 kettenis

Add acpicmos(4), a driver that implements SystemCMOS OperationRegion
access support. This fixes machines where the AML doesn't check whether
support for this OperationRegion type has been registered by the OS.

ok mlarkin@


Revision tags: OPENBSD_6_3_BASE
# 1.340 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.339 10-Feb-2018 deraadt

Syncronize filesystems to disk when suspending. Each mountpoint's vnodes
are pushed to disk. Dangling vnodes (unlinked files still in use) and
vnodes undergoing change by long-running syscalls are identified -- and
such filesystems are marked dirty on-disk while we are suspended (in case
power is lost, a fsck will be required). Filesystems without dangling or
busy vnodes are marked clean, resulting in faster boots following
"battery died" circumstances.
Tested by numerous developers, thanks for the feedback.


# 1.338 08-Feb-2018 deraadt

After flushing disks, try again to release all memory. We don't need to
save dirty memory to the hibernate space.


# 1.337 08-Feb-2018 deraadt

Toss all releaseable memory, because fragmentation can get in the way
of allocating the hibernate playpen.


# 1.336 08-Feb-2018 deraadt

sr_quiesce() is a new approach for ensuring that softraid drains
output to the disks.

This is part of a larger suspend/resume filesystem-safety diff, which
has been worked on for a couple of months already. Tests by job, krw,
beck, benno, and others. Sometimes even by snapshot users...


# 1.335 29-Nov-2017 kettenis

Add support for GPIO-signaled events.

ok mlarkin@


# 1.334 16-Nov-2017 jcs

Add PCI attachment for dwiic(4) needed by Intel 100 Series machines

ok kettenis


# 1.333 14-Nov-2017 kettenis

Add a few more PNP IDs to the skip lists.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.332 17-Aug-2017 stsp

Make dwiic(4) attach its ACPI parent dependencies.
This change makes the Asus E200HA keyboard work.

Original analysis and patch by Cesare Gargano
ok kettenis@


# 1.331 09-Aug-2017 dcoppa

Add "PNP0303" (8042 PS/2 Controller) to acpi_isa_hids[]

ok kettenis@


# 1.330 22-Jul-2017 jcs

acpibat: look for _BIX first before falling back on _BIF

ACPI 4.0 deprecated _BIF for battery status so some newer machines
are exporting _BIX which extends _BIF's package with a few extra
fields. When using _BIX, export the new discharge cycle count as a
sensor.

feedback from kettenis and anton
ok deraadt


# 1.329 20-Jul-2017 kettenis

Start a list of isa(4) device names such that we can avoid printing them
as "not configured".

ok mikeb@, millert@, deraadt@


# 1.328 08-Apr-2017 deraadt

Sensors are run as callbacks inside tasks. During suspend a sensor
could be running inside a driver that will be force-detached, or due
to tsleep end up disrupting the softstate/hardstate contract. At
suspend time, quisce all these callbacks by waiting for completion.
This issue has never been observed for real, but may be implicated in
suspend/resume failures.
ok kettenis guenther mlarkin


# 1.327 08-Apr-2017 deraadt

Two prototypes for acpi_maptable; merge to one place


Revision tags: OPENBSD_6_1_BASE
# 1.326 27-Mar-2017 deraadt

Now that hibernate_alloc() only has clean success/failure, don't
need to call hibernate_free() to clean up a partial mess.
ok mlarkin kettenis


# 1.325 13-Mar-2017 deraadt

allow ramdisks to build; ok jcs


# 1.324 12-Mar-2017 jcs

try to attach acpisbs first and if successful, don't attach acpibat


# 1.323 02-Mar-2017 natano

Add a new sysctl machdep.lidaction. The sysctl works as follows:

machdep.lidaction=0 # do nothing
machdep.lidaction=1 # suspend
machdep.lidaction=2 # hibernate

lidsuspend is just an alias for lidaction, so if you change one, the
other one will have the same value. The plan is to remove
machdep.lidsuspend eventually when people have upgraded their
/ets/sysctl.conf.

discussed with deraadt, who came up with the new MIB name
no objections mlarkin
ok stsp halex jcs


# 1.322 28-Feb-2017 natano

Untangle abstract sleep modes from ACPI sleep states. This paves the way
for further work in the area.

idea and ok deraadt
ok mlarkin


# 1.321 22-Feb-2017 jcs

add acpisbs, an acpi smart battery subsystem driver reading data
over smbus

currently disabled because it conflicts with acpibat

ok deraadt, kettenis


# 1.320 14-Jan-2017 kettenis

Revert aml_rdpciaddr changes; breaks several machines that were working
before.


# 1.319 11-Jan-2017 jsg

Increase the size of the buffers used for _HID and _CID strings to be
able to fit "Hyper_V_Gen_Counter_V1".

ok mlarkin@ deraadt@


# 1.318 08-Jan-2017 kettenis

Make aml_rdpciaddr() work for for devices behind bridges. The code now
uses the mapping between AML nodes and PCI devices that we establish.
Because _INIT methods may end up calling aml_rdpciaddr(), make sure we
create that mapping early. Also handle devices that aren't actually present.
These devices are now included in the mapping and reads will return an
all-ones patterm whereas writes are a no-op..

ok mlarkin@


# 1.317 25-Oct-2016 pirofti

Add the resource index to aml_parse_resource() callback function.

This is needed by an upcoming acpiec commit that handles machines breaking
the current ACPI specifications.

Change suggested by and ok kettenis@, guenther@


# 1.316 18-Sep-2016 guenther

If the APIC table (MADT) isn't present then ci_acpi_proc_id won't be set
when acpicpu tries to attach, leading to a NULL dereference. The ACPI
processor object on the Dells where this was reported is bogus and useless
anyway so apply the same test when deciding whether to attach to processor
objects as acpicpu would when matching them to cpus.

Problem reported by Eivind Eide (xenofil (at) gmail.com) and
Johan Huldtgren (johan+openbsd-bugs (at) huldtgren.com).
Testing by Eivind Eide.
ok kettenis@ mlarkin@


# 1.315 03-Sep-2016 naddy

Write the system time back to the RTC every 30 minutes.
This fixes the problem that long-running machines which were not
shut down properly would reboot with a badly offset system time.

hints and ok kettenis@


# 1.314 31-Aug-2016 mlarkin

Don't put a device in the wake device list if _STA says it's not there.

Fixes at least one machine that woke up from sleep / poweroff immediately.

ok kettenis@, tested by kettenis and martijn@


# 1.313 28-Jul-2016 kettenis

Store the acpi processor ID/UID in struct cpu_info, and use it to attach
acpicpu(4) drivers to the right cpu(4).

ok mlarkin@, guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.312 10-Jun-2016 kettenis

The power management timer has been made optional in ACPI 5.0A, so don't
attempt to attach acpitimer(4) if the timer isn't present.

Pointed out by jcs@

ok jcs@


# 1.311 08-May-2016 kettenis

Make sure devices listed by a device's _DEP method are attached before
we attach the device itself. The _DEP methode, introduced in ACPI 5.0,
is a hint that the device in question depends on OpRegion support from
the devices listed by _DEP, so we have to order them the proper way.
To prevent us from attaching those devices again when we encounter them
later walking down the device tree, keep track of our attempts to
attach a device driver to them using aflag in the aml_node structure.

ok guenther@


# 1.310 25-Apr-2016 pirofti

We attach acpihpet(4) and acpiec(4) without using the HID, so do not
report it in the dmesg.

OK deraadt@, mlarkin@.


# 1.309 02-Apr-2016 kettenis

Reduce dmesg spam by knocking out some acpi hardware IDs that don't need a
driver.

ok mpi@


# 1.308 29-Mar-2016 kettenis

Check if a device is present (using _STA) in acpi_foundhid. This prevents
us from reporting devices as "not configured" that aren't actually present,
and allows us to remove duplicated code from several drivers.

ok deraadt@, ok jsg@


# 1.307 29-Mar-2016 kettenis

Implement a cleaner way to prevent acpivout(4) from attaching if we have
a platform-specific method of controlling the backlight.

ok millert@, mlarkin@, jung@


# 1.306 28-Mar-2016 kettenis

Adding each and every new acpi device driver to acpi_foundhid() is
gettinga bit of the burden. And it really isn't how our autoconf
framework is supposed to work for busses that can be enumerated. All
the drivers already check for a matching _HID in their attach
function. So we can just drop the checks in acpi_foundhid() and let
the drivers sort themselves out.

Print a "not configured" line for devices for which we don't attach adriver.
This may be a bit much as there are devices for which we will not have a
driver. This will be removed if it causes too much dmesg spam.

ok deraadt@, mlarkin@


Revision tags: OPENBSD_5_9_BASE
# 1.305 17-Jan-2016 jsg

Take acpi_foundhid() outside of #ifndef SMALL_KERNEL so the ramdisk
kernel can find and attach dwiic(4) and sdhc(4).


# 1.304 17-Jan-2016 jsg

remove an unused global variable


# 1.303 14-Jan-2016 kettenis

Attach dwiic(4) on the Intel Bay Trail i2c controllers as well.


# 1.302 12-Jan-2016 kettenis

Improve emulation of PM registers on Hardware-redcued ACPI by emulating
SPL_TYPx, SLP_EN and WAK_STS using the new SLEEP_STATUS_REG and
SLEEP_CONTROL_REG registers. Makes the transition into S5 (powerdown) work
on the ASUS X205TA.

ok deraadt@, mlarkin@


# 1.301 12-Jan-2016 jcs

Add dwiic, a driver for the Synopsys DesignWare i2c controller found
on the Samsung ATIV Book 9 laptop. This initial version only
supports ACPI config/attachment.

Add ihidev, a HID-over-i2c driver largely based on uhidev. dwiic
handles attaching ihidev devices found in ACPI.

Add ims, a HID-over-i2c mouse/trackpad driver to get basic cursor
and button functionality from HID-compliant i2c trackpads.

ok kettenis deraadt


# 1.300 11-Jan-2016 kettenis

Make sdhc(4) attach to hardware IDs "80860F14" and "PNP0FFF".


# 1.299 09-Jan-2016 kettenis

Initial support for Hardware-Reduced ACPI. This skips the legacy mode checks
by emulating the PM_CNT register such that SCI_EN is always enabled. It also
skip attaching acpitimer(4) since the relevant registers are no longer there.

ok deraadt@, mlarkin@


# 1.298 06-Jan-2016 kettenis

Follow the spec and check the SCI_EN bit to see if ACPI has already
been enabled and don't bother enabling it again if it is. This should fix
systems that don't implement legacy mode and therefore set ACPI_ENABLE and
ACPI_DISABLE to zero.

ok mlarkin@, yasuoka@


# 1.297 23-Nov-2015 reyk

Back out the CRITICAL change, turn it back from 10% to 25%. Some
people with old/broken batteries didn't like this change, and the
underlying heuristics are wrong.


# 1.296 22-Nov-2015 reyk

The "CRITICAL" APM battery level, that is exported/faked by the ACPI
stack, is currently set to 25%. With 25% battery left, a typical
modern laptop will have at least one hour runtime left. It is an
arbitrary value, and other APM power layers in the kernel set CRITICAL
to something like 5% or 10%, so set it to 10% for now.

OK mlarkin@


# 1.295 28-Sep-2015 deraadt

In low-level suspend routines, set cold=2. In tsleep(), use this to
spit out a ddb trace to console. This should allow us to find suspend
or resume routines which break the rules. It depends on the console
output function being non-sleeping.... but that's another codepath which
should try to be safe when cold is set.
ok kettenis


# 1.294 06-Sep-2015 deraadt

clarify malloc arguments (base + length...)


# 1.293 28-Aug-2015 deraadt

fairly simple sizes for free(); ok tedu


# 1.292 20-Aug-2015 kettenis

Respect the access size when reading or writing to pci config space.
This version also makes sure writes are properly aligned, preventing
the panics seen with the previous incarnation of this change.

Fixes battery status passthrough in vmware, brightness keys on my x220
and who knows what else.

ok mlarkin@, deraadt@


Revision tags: OPENBSD_5_8_BASE
# 1.291 04-Aug-2015 deraadt

If we are setting ACPI_PM1_BM_RLD do it only once, and remember to
repeat this action at resume time.
ok mlarkin guenther


# 1.290 29-Jul-2015 deraadt

Something subtle in previous revision is locking up suspend or booting
on some laptops. Mailing lists threads mention x220 and Asus machines.
Reverting for now.


# 1.289 21-Jul-2015 kettenis

iRespect the access size when reading or writing to pci config space.
Fixes battery status passthrough in vmware and who knows what else.

Tested by many.

ok mlarkin@, deraadt@


# 1.288 17-Jul-2015 mlarkin

fix comments


# 1.287 26-Mar-2015 jsg

Use the 0x10 extended capability to determine if the trackstick
buttons are wired to the trackpad and need to be re-routed to the
trackstick. Without this change the buttons on 2015 thinkpads
get picked up as extended buttons that show up as scroll up/down.

Remove the X1 Carbon 2015 (LEN0048) and X250 (LEN0046) from the
top button area/soft buttons quirks list. Also avoid using the
quirk list entirely if the capability bit is set.

Based on work from jcs@ with feedback from mpi@
ok mpi@ jcs@


# 1.286 20-Mar-2015 kettenis

The ACPI standard says that a PCI function number of FFFF can be used to refer
to all functions on a device. This makes no sense in the context of accessing
PCI config space. Nevertheless stsp@ has a machine with AML that tries to
do config space reads from a device where _ADR encodes function number FFFF.
This is almost certainly buggy AML, but since Linux (and presumably Windows)
boot just fine on this machine we need to work around this. Linux effectively
treats function FFFF as function 7, which isn't present on this machine.
Instead we simulate a read from a non-existent device by returning a bit
pattern of all ones. Writes to function FFFF will still panic.

ok mlarkin@


# 1.285 16-Mar-2015 deraadt

Reduce use of SMALL_KERNEL in acpi, bringing acpiec driver and more gpe
handling into RAMDISK. This is now possible because the install media
has ample room. The goal is to reduce special cases where we may be
skipping (unknown) important operations...
ok mlarkin kettenis


# 1.284 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.283 07-Feb-2015 deraadt

New framework that allows hibernate to pass in entropy from it's fresh
boot.
ok mlarkin


# 1.282 06-Feb-2015 mlarkin

Fix a hibernate crash on some machines due to unmapping a page that
may not have been mapped previously (in the failure to hibernate case).

Also ensure that the lowmem ptp is mapped in all cases (not just MP).

ok kettenis


# 1.281 17-Jan-2015 deraadt

resume_randomness() before spinning up other cpus...


# 1.280 15-Jan-2015 jsg

Define a new wscons mouse type for Synaptics clickpad devices that lack
physical buttons. This will be used if the acpi pnpid for the mouse
matches a list provided by Synaptics found in Linux.

Combined with the xenocara changes this will remove the need for an xorg
config file for the x240/t440/t540 generation of thinkpads.

ok matthieu@ shadchin@ kettenis@


# 1.279 11-Jan-2015 kettenis

Tolerate checksum errors in ACPI tables (but complain about them). Apparently
there are machines out there with broken BIOSen with such checksum errors in
thier RSDT tables. Windows runs fine on these machines, so it probably does
not check. Linux ignores checksum errors as well (and prints a warning).

ok deraadt@, krw@


# 1.278 18-Dec-2014 deraadt

suspend and resume the RNG (at what looks like the right spots)
ok reyk djm


# 1.277 09-Dec-2014 doug

Replace some malloc(n*size,...) calls with mallocarray().

ok tedu@ deraadt@


# 1.276 06-Dec-2014 mpi

If "machdep.lidsuspend" is not set, do not go back to sleep if the lid
is still closed when resuming. This allows docked laptop to be resumed
without opening them.

ok mlarkin@


# 1.275 23-Nov-2014 mlarkin

If a machine has no _LIDs, then none can be open. This diff fixes a problem
where machines without any _LIDs were going back to sleep after hibernate
due to the recent suspend-if-lid-closed-on-resume diff. Noticed on qemu,
which doesn't have any _LIDs.

discussed with deraadt


# 1.274 18-Nov-2014 krw

Nuke yet more obvious #include duplications.

ok miod@


# 1.273 08-Nov-2014 mlarkin

If resuming from sleep (zzz/ZZZ) and the lid is still closed, go back to
sleep. This prevents accidental lid flex or slight opening in a backpack
from waking the machine up and leaving it resumed (powered on).

ok deraadt@


# 1.272 26-Sep-2014 kettenis

Rework piglet and pig allocation. Currently the piglet gets allocated
deep down in the suspend path, where it is really hard to recover from
allocation failure. So allocate the piglet early on in the suspend path.
Also change the piglet and piglet allocation functions to use km_alloc(9)
instead of doing pmemrange magic. This removes a bunch of code which, in the
case of the piglet allocation, is broken since it results in a NULL pointer
dereference. Also switch the piglet allocation to not wait. If we can't
allocate 16MB of phys contig memory on a halfway modern machine we're almost
certainly under a lot of memory pressure and we're better off not trying to
hibernate anyway.

ok mlarkin@


# 1.271 25-Sep-2014 kettenis

Bring back rev 1.249. Now that mp_setperf() has been fixed, it should be
safe to restore perflevel when resuming. Should fix the issues people
see with the processor running at full speed heating up the machine while
the apm command reports that the CPUs are running at a low speed.

ok dcoppa@, deraadt@


# 1.270 23-Sep-2014 kettenis

Release the acpi lock around calls to wsdisplay_suspend() and
wsdisplay_resume(). The X server may adjust the display brightness just
before/after the VT switch when suspending/resuming. In fact, it may
already be blocked adjusting the display brightness for some reason
unrelated to the suspend request.

The VT switch request will eventually time out, but the delay is annoying
and the failed VT switch will lead to problems after resume.

Needed for a future update of the xf86-video-intel driver.

ok mlarkin@


# 1.269 19-Sep-2014 kettenis

Use config_suspend_all(9).


# 1.268 08-Sep-2014 kettenis

Clear the PME Status bit on PCI devices in response to Device Wake events.
Fixes the interrupt storm on the 2009 Mac mini with WOL enabled on its
nfe(4) interface.

ok mlarkin@


Revision tags: OPENBSD_5_6_BASE
# 1.267 20-Jul-2014 mlarkin

Support hibernating to softraid crypto volumes.

much help and ok from deraadt@


# 1.266 16-Jul-2014 mlarkin

Reenable hibernate RLE support and flush+zero all memory after unpack.


# 1.265 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.264 12-Jul-2014 kettenis

Add a function to drop all clean pages on the page daemon queues and call
it when we hibernate.

ok mlarkin@, miod@, deraadt@


# 1.263 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.262 11-Jul-2014 mlarkin

Flush the buffercache to 16MB on hibernate and restore its previous max
size (kern.bufcachepercent) on resume, for better hibernate performance.

ok beck@


# 1.261 10-Jul-2014 blambert

remove unneeded header

ok mlarkin@


# 1.260 01-Jul-2014 kettenis

Adding an additonal delay after each access to SystemMemory or SystemIO
during autoconf makes no sense.

ok deraadt@


# 1.259 23-Jun-2014 kettenis

SystemMemory is used to access memory mapped registers on some machines, so we
must use the bus_space(9) API to access it instead of memcpy(9). Also make
sure we properly align access to fields that need more than byte alignment.

ok guenther@


# 1.258 21-May-2014 mlarkin

Fix invalid / missing format specifiers in several dnprintf statements
(used only when ACPI_DEBUG is enabled)

From a diff on tech@ by Fabian Raetz, with a minor change by me, tested
on i386 and amd64 with ACPI_DEBUG enabled and disabled.


# 1.257 25-Apr-2014 mlarkin

Defer the free of the hibernate structures until later, when it is safe to
do so.

Freeing the hibernate structures too early leads to inadvertent calls into
splx when we aren't ready for them yet. Found when trapping splx calls in
the resume path.

discussed with deraadt@ at m2k14


# 1.256 13-Apr-2014 mlarkin

Fix a badly worded debug message that implied the entire machine was going
to sleep, instead of just the acpi thread going to sleep.


# 1.255 31-Mar-2014 mpi

Change the order of operations during a suspend/resume cycle and
call bufq_quiesce() after executing the DVACT_QUIESCE handlers.

This should be safe since no disk nor controller drivers have such
handler but it will allow us to detach sd(4) devices attached to a
USB bus.

Another benefit pointed out by kettenis@ is that drivers that need
to read a firmware from the disk should be able to do it at resume
time in a DVACT_WAKEUP handler.

ok kettenis@, deraadt@


# 1.254 13-Mar-2014 dlg

get rid of the assumption that the head of the alldevs list is the
"mainbus" device. this breaks when mpath is enabled because it
attaches before mainbus and therefore takes the head position.

have autoconf provide device_mainbus() which looks up mainbus_cd,
and use that instead.

discussed with deraadt who just wants mpath stuff to move forward
despite there being many ways to shine this particular turd.


Revision tags: OPENBSD_5_5_BASE
# 1.253 21-Feb-2014 deraadt

Do the lid control further on in the resume sequence, since this is
AML, and will do WAITOK operations. While cold, that is a bad idea.
We seem to be safe with respect to the spec, I do not see any event
debounce issues here.
ok kettenis, tested by many


# 1.252 20-Jan-2014 claudio

Instead of checking sc->sc_revision <= 1 check that the
fadt->hdr_revision <= 1. This is a better check since it checks
the version of the proper descriptor. This allows the alix APU
to reboot since those boards have currently a wrong sc->sc_revision.
Committing this now so that it can be tested easier.
With and OK jsg@ also fine by derradt@


# 1.251 24-Dec-2013 kettenis

If the FADT has its SMI_CMD set to zero, assume we're only ACPI-only hardware
and don't need to disable SMI ownership of the ACPI hardware registers.

ok mlarkin@


# 1.250 23-Dec-2013 kettenis

Revert previous commit. It makes resume hang on some MP systems running
GENERIC.MP.


# 1.249 16-Dec-2013 kettenis

Restore hw.setperf upon resume like we do for apm(4) on i386. Seems we need
to do this fairly late in the resume to avoid hangs.

ok deraadt@, mlarkin@


# 1.248 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


# 1.247 06-Nov-2013 mpi

Add support for Power Resources for Dx states and the necessary hook
for PCI devices. This hook should be called twice, before and after
changing the power state of a PCI device.

Before setting the device to the new state, the ACPI layer will notify
every power resources linked to the device for that state and make sure
they are turned "_ON". After changing the state of the device, it will
decrement the reference of every power resources linked to that device
for the old state and turn them "_OFF" if they are no longer referenced.

This fixes the no-USB after resume problem seen on various ThinkPad,
problem initialy diagnosed with Alexander Polakov.

ok kettenis@, deraadt@


Revision tags: OPENBSD_5_4_BASE
# 1.246 01-Jun-2013 mlarkin

Stop printing ACPI wakeup devices in dmesg after the 16th wakeup device.

Helps with VMware VMs that report hundreds of wakeup devices.

ok reyk@, deraadt@


# 1.245 31-May-2013 bluhm

Fix the build for a kernel without wd and pciide in its config.
OK deraadt@ tedu@


# 1.244 30-May-2013 deraadt

Enforce ca_activate tree-walks over the entire heirarchy for all events,
cleaning up some shutdown-hook related code on the way.
(A few drivers related to sparc are still skipped at kettenis' request)
ok kettenis mlarkin, tested by many others too


# 1.243 18-Apr-2013 deraadt

pin the acpi thread to the BSP, because AML and SMI cannot be trusted. I
was certain we were doing this already, but it turns out no...


Revision tags: OPENBSD_5_3_BASE
# 1.242 24-Dec-2012 guenther

In acpiioctl(), Return though the splx() path if suser() returns an error

ok deraadt@


# 1.241 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


# 1.240 04-Oct-2012 ehrhardt

Use information provided by ACPI to attach secondary PCI host bridges.

ok kettenis@


# 1.239 07-Sep-2012 kettenis

Add a new function called acpi_pci_min_powerstate() that returns the lowest
power state for a PCI device for the power state (currently S3 or S4) we're
transitioning into. This function is probably too simplistic; there is a lot
of (silly) complexity in the ACPI spec.

ok deraadt@


Revision tags: OPENBSD_5_2_BASE
# 1.238 13-Jul-2012 pirofti

Disable acpivideo(4) on ASUS laptops.

There are some models that panic whenever an event is received.
Some cope, some don't. In order to get this right acpiec(4) requires
major rework. Until then let machines boot.

Discussed with deraadt@.


# 1.237 11-Jul-2012 mlarkin

for now, restrict hibernate to root in the acpi ioctl kernel interface.
normal rules for controlling this interface through apmd still apply.

ok deraadt@


# 1.236 09-Jul-2012 deraadt

tiny little oops in untested hibernate block


# 1.235 09-Jul-2012 deraadt

clean up ioctl() case statements


# 1.234 09-Jul-2012 deraadt

acpi_disable_onegpe() is not used; ok pirofti


# 1.233 24-May-2012 kettenis

If _MIN > _MAX consider a WordBus resource to be invalid. Fixes a panic on
the IBM x3550-M4.

ok mikeb@


# 1.232 29-Mar-2012 mlarkin

Missing #ifdef in previous diff


# 1.231 29-Mar-2012 mlarkin

Prevent hibernation attempts on machines with unsupported disk controllers.
The same check is also performed later in the hibernate sequence, but by
then it's too late to cleanly unwind (presently).

ok deraadt


# 1.230 26-Mar-2012 deraadt

simplify conditionals in acpi_sleep_state()
ok mlarkin


# 1.229 26-Mar-2012 deraadt

hook in the hibernate request code; half of this diff is from mlarkin
ok mlarkin


Revision tags: OPENBSD_5_1_BASE
# 1.228 20-Sep-2011 deraadt

Very early in suspend while the clock is still ticking realtime,
save the clock back to the rtc. The zaurus already did this.
ok phessler miod kettenis


Revision tags: OPENBSD_5_0_BASE
# 1.227 02-Jul-2011 nicm

kqueue attach functions should return an errno or 0, not a plain 1. Fix
the obvious cases to return EINVAL and ENXIO.

ok tedu deraadt


# 1.226 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.225 16-Jun-2011 pirofti

Import acpitoshiba driver.

This is originally written by Hiroyuki Aizu, ported and integrated
in our tree by Javier Vazquez <javama4 AT gmail> with tweaks and
suggestions by me.

Import prodded by deraadt@ and the lack of testers on tech@.


# 1.224 27-Apr-2011 jcs

attach acpithinkpad to newer lenovo models like the x120e

ok deraadt@


# 1.223 22-Apr-2011 jordan

Fix uninitialzied variables and formatting strings (-Wxxx errors)


Revision tags: OPENBSD_4_9_BASE
# 1.222 02-Jan-2011 jordan

Re-add code for ACPI taskqueue, eliminates need for state vars in acpi softc
Serialize processing ACPI tasks for sleep/powerdown/GPE event
ok deraadt@


# 1.221 31-Oct-2010 guenther

Revert last commit: it breaks resume on Thinkpads
"then please back it out" deraadt@


# 1.220 26-Oct-2010 jordan

Add task queue for ACPI gpe and notify handlers
This fixes eject on Dell Latitude dock and an issue on the Dell Mini
battery update.
ok deraadt


# 1.219 07-Oct-2010 jordan

Create separate functions for enabling wake and runtime gpes.
Remove old suspend/resume gpewalk function
ok deraadt, mlarkin


# 1.218 05-Oct-2010 jordan

split acpi_enable_onegpe to separate enable/disable functions
ok deraadt


# 1.217 05-Oct-2010 jordan

Added acpi_disable_allgpes function to clear GPE status
ok deraadt


# 1.216 05-Oct-2010 deraadt

clean up acpi_softc use more


# 1.215 05-Oct-2010 deraadt

delete a bit of #if 0 code


# 1.214 05-Oct-2010 deraadt

hide use of our global acpi_softc pointer a bit better


# 1.213 29-Sep-2010 deraadt

grab the acpi thread lock very early in acpi thread startup; not that it
is likely that anyone will race us... but why be careless.
ok kettenis


# 1.212 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.211 29-Aug-2010 jasper

- add a check for FWRITE here too.

ok miod@


# 1.210 27-Aug-2010 kettenis

Make PCI Power Management optional, and only enable it when acpi(4) attaches.

ok deraadt@


Revision tags: OPENBSD_4_8_BASE
# 1.209 08-Aug-2010 kettenis

Close race between the acpi thread and wscons ioctls running in process
context. We might rip this out post-release and replace it with a solution
that serializes things in a more obvious way.

ok deraadt@, miod@ and "those crazy texans who ok anything"


# 1.208 08-Aug-2010 deraadt

Report the acpi sleep states when on a RAMDISK; ok krw


# 1.207 07-Aug-2010 kettenis

Enforce acpi register access rescrictions.

ok mlarkin@, deraadt@, tested by marco@


# 1.206 07-Aug-2010 deraadt

upon resume, notify all the acpiac and acpibat drivers to update
their status. some laptops do not do this automatically.
ok kettenis canacar


# 1.205 06-Aug-2010 marco

Always call _PSW on _LID devices that support it to enable lid open to
resume the machine. Conversely disable it when the machine wakes up.

Tested by several ok deraadt


# 1.204 06-Aug-2010 deraadt

As part of a revamp of the PM1/GPE code, I write this diff to look for
unmanaged EN & STS bits in the PM1 register at interrupt time and report
them. As a side effect this splits the STS acknowledgement into two writes
(for power, and sleep) instead of one. The printf that is added (to spot
unmanaged STS bits) has not yet been triggered as far as we know.

Before the "write to PM1 registers at the right offset" diff went in, this
was not neccessary. But newer thinkpads do not have a working soft power
button without this diff.

We have no idea why.
ok mlarkin kettenis


# 1.203 05-Aug-2010 deraadt

PM1 power button events were generating 'fake events' to acpibtn PWRB,
but if there is no such device these were being tossed. Instead, arrange
for all power button handlers to flag a softc variable which is processed
by the thread; so that one place will know to request a powerdown (psignal
to init)
ok marco mlarkin


# 1.202 05-Aug-2010 deraadt

rename a variable which is annoyingly poorly named


# 1.201 05-Aug-2010 mlarkin

Write to the correct offset for various ACPI pmreg registers. Writing to
the correct register does properly clear the wake status bits and fixed
event bits, which allows some machines to have more than 1 suspend cycle.

bug spotted by deraadt@

tested by deraadt@ and myself on a half-dozen machines and others on
hackers@

ok deraadt@, marco@


# 1.200 05-Aug-2010 deraadt

Fix the power button. The pm1 register is 16 bits in size.
My mistake, a while back.
tested by krw


# 1.199 04-Aug-2010 kettenis

Print supported sleep states.

ok marco@, phessler@, deraadt@


# 1.198 03-Aug-2010 mlarkin

Use the proper flag for re-enabling certain hardware events (power btn,
sleep btn). The (incorrect) flag prevoiously used coincidentally had the
same value, so this shouldn't cause any different behavior than before.

tested on a variety of machines (i386, amd64, sp, mp) - no behavioral
change seen

ok deraadt@


# 1.197 28-Jul-2010 marco

Make all gasio failures look and behave the same.


# 1.196 28-Jul-2010 mlarkin

Delay a little longer on suspend before giving up. Fixes machines that
sometimes don't suspend because the sleep transition takes too long
(symptoms like failing to sleep but the backlight stays on).

ok deraadt@


# 1.195 27-Jul-2010 deraadt

remove XXX; EC can now be attached early when needed; ok jordan


# 1.194 27-Jul-2010 deraadt

more brackets feels good


# 1.193 27-Jul-2010 deraadt

XXX annotate a block in acpi_enter_sleep_state() that is pretty worrying


# 1.192 27-Jul-2010 deraadt

horrific indentation that nearly made me puke


# 1.191 27-Jul-2010 mlarkin

Set the sleeping indicator light on machines that support it, in case SMI
doesn't do it for us. Shows the 'moon' LED on older Thinkpads when
sleeping.

Tested on T43p, W500, T510 by me and on X61s by deraadt@.

ok deraadt@


# 1.190 27-Jul-2010 jordan

Early initialization of acpiec if ECDT table exists
Fixes hang when booting thinkpads while docked
ok deraadt


# 1.189 26-Jul-2010 deraadt

remove merge error which was commited


# 1.188 26-Jul-2010 pirofti

Add support for Sony ACPI hotkeys via a new driver: acpisony(4).

Currently it only works for the suspend button and tries to do right for
the brightness events, but I haven't found a Sony laptop that like to do
right so far.

In the future I want to make the brightness keys work on all Sony's and
also add support for the zoom hotkeys and whatever other funky keys I can
find on those things.

Okay deraadt@.


# 1.187 22-Jul-2010 deraadt

pretty large cleanup of SMALL_KERNEL


# 1.186 21-Jul-2010 deraadt

in the acpi GPE handler, late ack level interrupts not edge interrupts.
Duh.


# 1.185 21-Jul-2010 deraadt

Merge the edge and level gpe handlers into one (now possible since the
gpe structure tells us whether the gpe is edge or level)


# 1.184 21-Jul-2010 deraadt

delete #if 0 code we are not going to use; ok marco


# 1.183 20-Jul-2010 deraadt

make the mask code for acpi_enable_onegpe() match other similar code


# 1.182 20-Jul-2010 deraadt

Handle the acpi interrupt controller with proper edge/level handling,
wrapped in spltty to avoid racing against the interrupt controller.
Repair the gpe bit masking code while there, and do operations in the
right order
ok kettenis mlarkin, help from jordan at figuring out the order of
operations.


# 1.181 20-Jul-2010 matthew

When we're preparing to go to sleep, only call bufq_restart() if other
preparations failed.

ok kettenis@, thib@, deraadt@


# 1.180 19-Jul-2010 deraadt

permit compilation with ACPI_DEBUG


# 1.179 19-Jul-2010 deraadt

there is no need for a seperate dsdt_softc pointer; dsdt does not have a
softc but is just a part of the single acpi driver.
ok jordan


# 1.178 19-Jul-2010 deraadt

acpi_delay() is no longer needed; ok jordan


# 1.177 18-Jul-2010 mlarkin

Clear event status on resume, to avoid going back to sleep/reboot/shutdown
immediately after resume. Tested by a bunch of people on hackers@ on
various hardware. Fixes at least Toshiba NB200 and Gateway LT3103u.

ok marco


# 1.176 13-Jul-2010 deraadt

rename some thread related interfaces so they are logical


# 1.175 10-Jul-2010 jordan

Verify _ADR bdf is correct (some systems return 0xffff)
Works on Dell x9xx and Studio 1555


# 1.174 09-Jul-2010 jordan

Clear the noise


# 1.173 08-Jul-2010 jordan

Add mapping for ACPI device to PCI bdf (match autoconf tree)
Simplify resource parsing function to use buffer argument
Convert namespace linked lists to use queue macros
ok marco@, deraadt@


# 1.172 06-Jul-2010 deraadt

Use spltty() to lock downcalls from apm against the information being
modified by the acpi thread.
ok kettenis


# 1.171 06-Jul-2010 deraadt

Don't use a workq to do the suspend, because it races aginst the acpi
thread. Instead, just tell the acpi thread to do the suspend for us.
This makes apmd & zzz work correctly. While here, have acpithinkpad
attempt to post the event to apm before prompting the suspend itself.
ok kettenis marco mlarkin


# 1.170 05-Jul-2010 mlarkin

Reset register SCI_EN on resume. The ACPI spec clearly states we don't have
to do this, but many machines don't, or forget to, which leads to all sorts
of problems on resume (broken battery stats, overheating, failure to resume
more than once, etc).

tested by many, ok marco@, deraadt@


# 1.169 01-Jul-2010 thib

do bufq quiesce again, since vnd/the bufq_done call in
biodone has been fixed.


# 1.168 01-Jul-2010 jordan

Backout recent AML changes, breaks on R210 and others


# 1.167 01-Jul-2010 jordan

Simplified aml_parse_resource call, cleanup code
ok marco


# 1.166 01-Jul-2010 jordan

Fix subordinate bus # for multi-root PCI buses
ok marco, kettenis


# 1.165 30-Jun-2010 thib

Disable/partially backout the bufq quiesce changes as this
is causing havoc with vnds and release must be buildable.


# 1.164 29-Jun-2010 deraadt

Fix ramdisks (new code was placed inside a SMALL_KERNEL chunk)


# 1.163 29-Jun-2010 jordan

Add support for mapping ACPI to PCI devices
ok kettenis, deraadt


# 1.162 29-Jun-2010 kettenis

Call bufq_quiesce() on suspend to stop all I/O and bufq_restart on resume.
Makes suspend/resume work much better while doing disk I/O.

ok mlarkin@


# 1.161 29-Jun-2010 tedu

60 is a more reasonable guess for number of minutes in an hour than 100.
spotted by TeXitoi. ok deraadt


# 1.160 27-Jun-2010 jordan

Add _acpi_matchhids routine, precursor to pci device match


# 1.159 27-Jun-2010 mlarkin

Clear ACPI fixed event status on resume (power buttons/etc) so that some
machines don't immediately resume after suspending to S3.

Tested by kettenis@ and I on 5 or 6 machines, deraadt@ "yeah, looks good"


# 1.158 27-Jun-2010 jordan

Cleaned up acpi_maptable, adds to table list directly


# 1.157 07-Apr-2010 deraadt

If we have done a wsdisplay_suspend(), and ACPI decides it does not want to
suspend, we must roll back by calling wsdisplay_resume() or the virtual
console switching will be locked forever
ok oga


# 1.156 07-Apr-2010 kettenis

Make suspend/resume work on MP machines (running an MP kernel). Joint work
from mlarkin@ and me, with some amd64 fixes thrown in by deraadt@

ok marco@, deraadt@, pirofti@, mlarkin@


# 1.155 31-Mar-2010 kettenis

Bring /dev/apm support in line with apm(4). Make the suspend button send
a suspend request event to apmd(8) instead of suspending immediately. Also
keep track of whether /dev/apm and /dev/apmctl are currently open, such that
we can still suspend immediately if apmd(8) isn't running.

ok deraadt@, marco@, pirofti@, jsing@, oga@


# 1.154 30-Mar-2010 oga

Prevent the apmd/x races for good.

When we hit suspend time, go through all wsdisplays on the system. if
they are in mode MAPPED, but not MODE_DUMBFB then if possible do a full
vt switch to a !mapped vt, and prevent switching back until resume time.
This has to be called from MD code because this involves userland
running so that X can run the vt switch signal handler. This way, any
case where we are using the "poke registers from userland" model, we
will not be on the hardware when we go down, so the kernel can actually
handle thing properly.

Tested on several acpi laptops (by kettenis@ and ian@), x40 (me and
beck@ at LEAST) and zaurus (me). Maybe others, but if so I forgot who at
this time..

Idea from deraadt somewhere over the Faroe Islands (I thought of a
similar thing myself a while ago). Much prompting from him. Ok and
comments miod@


# 1.153 25-Mar-2010 oga

Fix & vs. && in the kqfilter code.

Based on a comparison with the apm code.

ok deraadt@, kettenis@


Revision tags: OPENBSD_4_7_BASE
# 1.152 26-Nov-2009 mlarkin

Visual (and on some machines, audio) feedback while the machine is
resuming via calls to aml _SST method. This also makes the "moon" LED on
thinkpads to blink during resume and cease when resume is completed.

ok pirofti@


# 1.151 26-Nov-2009 deraadt

Toggle into "cold" when we we disable interrupts, because tsleeping
in the AML interpreter and context switching into userland processes
tends to sometimes pop the ACPI trampoline page out of our address
space. Makes lots more machines work.
ok kettenis mlarkin


# 1.150 24-Nov-2009 jsg

in the resume path evaluate _WAK before dealing with device drivers
like the spec says, not after.

ok mlarkin@


# 1.149 24-Nov-2009 deraadt

abort suspends on SMP systems until that gets written; ok mlarkin


# 1.148 23-Nov-2009 mlarkin

pci should return the result of it's children's suspend/resume calls back
to its parent.

handle suspend failure case and unwind if devices have an activate function
that returns failure.

ok deraadt@, kettenis@


# 1.147 23-Nov-2009 mlarkin

acpi* devices need to call their children on suspend/resume.
ok deraadt@


# 1.146 23-Nov-2009 pirofti

Remove ACPI_SLEEP_ENABLED checks.

This enables by default the suspend/resume paths in the kernel.

Okay deraadt@.


# 1.145 23-Nov-2009 deraadt

knf and spacing that are super annoying


# 1.144 23-Nov-2009 mlarkin

Hook up Theo's new children suspend/resume function, as well as more ACPI
suspend/resume glue.

ok deraadt@


# 1.143 26-Oct-2009 deraadt

Do not do strncmp comparisons on dv_xname because longer device names which
look similar could arrive in the future. Instead, compare directly against
dv_cfdata->cf_driver->cd_name
Issue originally spotted by miod


# 1.142 26-Oct-2009 jordan

Verify checksum+address when loading ACPI tables.
Some systems had invalid entries in RSDT/XSDT.
ok marco@


# 1.141 23-Jul-2009 cnst

New aibs(4) driver for ASUSTeK AI Booster (ACPI ATK0110) hardware monitoring,
with sensor state support through limits provided by the ACPI.

Tested on several ASUS motherboards kindly networked by Sam Fourman Jr.

ok deraadt marco jordan


Revision tags: OPENBSD_4_6_BASE
# 1.140 03-Jun-2009 pirofti

Import acpi power resource driver. This is needed for suspend/resume logic.

Okay jordan@ and marco@.


# 1.139 03-Jun-2009 pirofti

Add acpivideo support. This adds brightness support for all laptops
except thinkpads, they will use the acpithinkpad driver. The driver is
also hooked into wsconsole. So brightness can be adjusted via:

$ wsconsctl display.brightness=<percentage>

This is very helpfull on some laptops that have a nasty bios and get two
steps instead of one when pressing the brightness button.

Tested on various dell, fujitsu, acer, samsung and other laptops.

Okay marco@, miod@. Suggestions from kettenis@.
Lots of reviews and help from miod@, thanks!


# 1.138 03-Jun-2009 jordan

Added ejectable bay for IDE cdroms
ok marco@


# 1.137 30-Apr-2009 marco

Oga changed apm ioctls and forgot to grep. Temporary work around by hitting
the sleep button when we get any request. This interface will chnage at
some point.
ok oga


# 1.136 19-Apr-2009 krw

Call acpi_sleep_walk() when powering down just like when entering
any other sleep state. This tells acpi wake devices to not wake up
the box until manual powering up resets their status. Makes my Dell
GX520 stay powered off in response to 'halt -p'.

In snaps for a while.

ok marco@ (several times) deraadt@


# 1.135 17-Apr-2009 pirofti

Add device ranking support for future sleep/wake developments.

okay marco@


# 1.134 11-Mar-2009 jordan

Cleanup.. use aml_evalinteger instead of aml_evalname
ok marco@


# 1.133 10-Mar-2009 jordan

Fix cut/paste error.. call _BFS not _PTS
ok marco@


Revision tags: OPENBSD_4_5_BASE
# 1.132 21-Feb-2009 marco

overzealous cut/paste; found by Vladimir Kirillov <proger@uaoug.org.ua>


# 1.131 20-Feb-2009 marco

Oops didn't mean for Debugger() to remain there.


# 1.130 19-Feb-2009 marco

suspend/resume bits so that we can develop this in tree. This is disabled.
code from mlarkin and me
help from art,toby,jordan and several others
ok jordan, go for it deraadt


# 1.129 10-Feb-2009 jordan

Fix null pointer for shutdown if acpi not enabled
ok marco@


# 1.128 04-Feb-2009 kettenis

Some systems that claim to be ACPI 2.0 apparently don't provide an XSDT
address. Make the code fall back on the RSDT on those systems.

From Joerg Niendorf <p3y80@internode.on.net>. Fixes PR 5986.


# 1.127 17-Jan-2009 krw

A couple of stray malloc/memset -> malloc(M_ZERO), a bit of knf and
lose a couple of unnecessary casts. No functional change.

Feedback from kettenis@

ok kettenis@ marco@


# 1.126 06-Nov-2008 marco

Collapse all sanity checks of hids into one function.

Originaly from jordan


Revision tags: OPENBSD_4_4_BASE
# 1.125 02-Jul-2008 fgsch

add acpivideo. for now almost an empty skel so others can work on
suspend/resume. eventually it will also manage output switching and
brightness where supported. prodded by marco@


# 1.124 11-Jun-2008 marco

Fix $OpenBSD$ for once and for all


# 1.123 08-Jun-2008 deraadt

aaa_dev was being set from an uninitialized variable; ok toby


# 1.122 07-Jun-2008 marco

Kill ACPIVERBOSE. It was never used and is worthless to boot.

comments jmc
ok toby


# 1.121 06-Jun-2008 marco

Shrink acpi some more for floppies and while here re-enable acpi on the
floppies. Fixes crazy sony vaio kettenis has.

tested by deraadt
ok deraadt jordan kettenis


# 1.120 01-Jun-2008 marco

Fix \n nit in SMALL_KERNEL.


# 1.119 01-Jun-2008 marco

Re-enable new parser. Lots of testing by various folks. Fits all media
now too.

ok kettenis deraadt


# 1.118 16-May-2008 dlg

back out jordans new parser.

it didnt have enough oks from the right people, it did break machines
that were previously working, and it bloated the RAMDISKS and install
media beyond the constraints of the media.

requested by deraadt@


# 1.117 14-May-2008 jordan

Adding new ACPI Parser code
Old guts of code still exists, needs to be torn out next
ok marco@
ok brad@


# 1.116 27-Apr-2008 jcs

add acpithinkpad(4), an acpi support driver for ibm/lenovo
thinkpads.

provides functionality that the bios did on older, non-acpi
thinkpads like respond to screen brightness keys and toggle the
bluetooth device on and off.

ok deraadt@ and marco@


# 1.115 24-Apr-2008 jsing

Introduce acpiasus(4), a driver for the ACPI based hotkeys found in many
ASUS laptops (including the ASUS EeePC) - largely based on NetBSD's
asus(4) driver. On the ASUS EeePC this allows us to enable/disable
wireless, change screen brightness and use the volume keys.

ok jsg@, weingart@


# 1.114 12-Apr-2008 kettenis

Get rid of acpi_s5 global variables; simply send SIGUSR2 instead of SUGUSR1
to tell init(8) to power down the machine.

ok krw@


Revision tags: OPENBSD_4_3_BASE
# 1.113 21-Jan-2008 jsg

Check against RESET_REG_SUP in FADT before doing the acpi reset dance
after all, not doing so appears to break more machines than it fixes.

ok marco@


# 1.112 05-Jan-2008 kettenis

Never attach more acpicpu(4) devices than cpu(4) devices. Always attach
the first acpicpu(4) device on non-MULTIPROCESSOR kernels, regardless of its
ACPI processor ID.

ok mikeb@


# 1.111 05-Dec-2007 deraadt

Hang acpi(4) below bios(4) instead of mainbus(4). This lets us move acpi(4)
before pcibios(4), and then.... if acpi(4) attaches, skip pcibios(4) since
it messes the machines up. Fixes claudio's HP dl320 G5, and almost assuredly
others.
ok kettenis


# 1.110 05-Dec-2007 deraadt

repair acpiopen/acpiclose and friends to handle the /dev/apmctl interface,
so that apmd(8) can actually work correctly against acpi(4), and thus,
even do the fancy -C/-A stuff; also tested by jmc


# 1.109 02-Dec-2007 fgsch

fix the trailing space for real now.


# 1.108 02-Dec-2007 fgsch

avoid trailing spaces on output; marco@ ok.


# 1.107 27-Nov-2007 kettenis

Fix off-by-one that made us attach acpicpu0 and acpicpu1 on GENERIC kernels.

ok marco@


# 1.106 25-Nov-2007 jsg

When available and ACPI is enabled use the ACPI reset method
to reboot. This is required for some quirky sis machines
and likely others to reboot.

Tested by many. 'commit early commit often' deraadt@


# 1.105 19-Nov-2007 deraadt

sigh, amd64 still uses that stupid acpi_enabled variable...


# 1.104 19-Nov-2007 deraadt

softc's come initialized to 0, you know...


# 1.103 19-Nov-2007 deraadt

store revision; from jsg


# 1.102 19-Nov-2007 kettenis

Initialize sc_facs earlier.

ok deraadt@, beck@, canacar@


# 1.101 16-Nov-2007 deraadt

fix the bus_space #define nightmare, so that amd64 and i386 are much more
uniform. as a result shared code like acpi needs less #ifdef's
ok marco kettenis


# 1.100 15-Nov-2007 deraadt

remove the #ifdef ACPI_ENABLE. the EC codepath is either safe enough (and
will be improved), or it is unsafe because AML assumes it must be there
ok almost everyone


# 1.99 15-Nov-2007 mikeb

Make acpicpu(4) attach to the existing CPUs only.

Problem reported by form@. Fix idea by kettenis@.
Lots of help from deraadt@. Tested by deraadt@, form@ and me.

ok deraadt marco


# 1.98 14-Nov-2007 deraadt

spacing


# 1.97 12-Nov-2007 deraadt

correct : vs , in attach-time messages


# 1.96 06-Nov-2007 deraadt

all ACPI_ENABLE kernels need acpi_interrupt()


# 1.95 06-Nov-2007 deraadt

move acpi_read_pmreg() and acpi_write_pmreg() out of SMALL_KERNEL so
that acpi ramdisks can compile (obvious solution)


# 1.94 05-Nov-2007 canacar

Establish the acpi interrupt earlier. Fixes problems with some
GENERIC.MP kernels not getting acpi interrupts. This is a
workaround until the real issue with late establishment is fixed.
ok weingart@, beck@, gwk@


# 1.93 03-Nov-2007 jordan

Added support for displaying wakeup devices
ok beck@,weingart@,gwk@


# 1.92 11-Oct-2007 kettenis

When walking the tree yo find _PRT methods, check whether the device is
actually enabled and functioning. Similar to what we do for _INI.

ok weingart@, beck@


# 1.91 08-Oct-2007 krw

More simple memset(,0,) -> M_ZERO changes. In this batch move to
size(*p) as the first malloc() parameter where p is declared locally
and thus easy to check. Add M_ZERO to gpe_table allocation in acpi.c
even though there is no obvious bzero or memset nearby.


# 1.90 13-Sep-2007 mk

Remove unnecessary memset() (see aml_evalnode(9)) and a bit of KNF.

ok toby


# 1.89 13-Sep-2007 weingart

Implement ACPI 6.5.1 spec tree walk for _STA and _INI.
Fixes mk's laptop. No regressions so far (thank you to the
testers).

ok gwk@, mk@, marco@


Revision tags: OPENBSD_4_2_BASE
# 1.88 17-Apr-2007 mk

Now that acpidock no longer hangs machines during boot (at least
according to the reports I've been getting) it should be safe to enable
it again, so remove the #if 0 stuff around it.

ok marco


# 1.87 11-Apr-2007 jordan

Added changes for C-State
ok marco@


# 1.86 26-Mar-2007 marco

Calculate minutes left more accurately.

From giovanni <qgiovanni@gmail.com>


Revision tags: OPENBSD_4_1_BASE
# 1.85 03-Mar-2007 mk

Stop looking for acpidock until we fix that stuff that breaks all pre-60
series ThinkPads with acpidock(4) enabled.


# 1.84 22-Feb-2007 marco

We were getting no interrupts in .MP. There are 2 fixes; one in
acpi_machdep.c and move the interrupt establishment down after ioapic
has been initialized.

ok jordan


# 1.83 21-Feb-2007 kettenis

Make an acpi-enabled GENERIC.MP work on systems that only support PIC mode.

ok marco@


# 1.82 19-Feb-2007 jordan

Added acpi_map_address function for mapping GAS/IO registers
ok marco@


# 1.81 18-Feb-2007 jordan

Added dynamic perfect hash for opcodes
ok marco@


# 1.80 18-Feb-2007 marco

Shave off another K pre compression.
Shuffle SMALL_KERNEL functions around so that we only have a single #ifndef


# 1.79 17-Feb-2007 marco

Add SMALL_KERNEL throught acpi to make it fit on boot media.
ok deraadt


# 1.78 13-Feb-2007 marco

Add smarter _INI code. This helps with some dissapearing devices.

ok kettenis jordan


# 1.77 31-Jan-2007 gwk

Introduce acpi_hasprocfvs thats "ACPI Has Processor Frequency and Voltage
Scalling" a global flag set by acicpu attach to indicate that ACPI is
handling fvs through the _PSS objects. This will fix disappearing
EST/powernow when booting kernels with acpi enabled, as acpicpu isnt
compiled into GENERIC yet. Also in cases where acpicpu is built in and
legacy EST/powernow work but acpi is missing the _PSS object, the legacy
routines will be used.

tested by dim@, and Nick Nauwelaerts <nick AT nauwelaerts.net>
ok marco@, dim@


# 1.76 22-Jan-2007 mk

Actually look for docking stations by looking for _DCK nodes.

ok marco


# 1.75 02-Jan-2007 marco

On some systems EC must be attached as the first device. Move EC from
generic _HID devices to its own routine to force first attach. Bug found
and debugged by joris@

Code mostly written by joris@


# 1.74 26-Dec-2006 marco

Add polling to devices that require it such as acpibat and acpitz.
Use the same tsleep/wakeup mechanism as the interrupt code to keep it non-
concurrent.


# 1.73 21-Dec-2006 deraadt

even more knf love


# 1.72 21-Dec-2006 marco

Remove some leftover acpiec gpe/interrupt code.


# 1.71 20-Dec-2006 deraadt

oops, name already used


# 1.70 20-Dec-2006 deraadt

rename __acpi_enable_gpe(); ok marco


# 1.69 20-Dec-2006 deraadt

acpi_unmap_pmregs() is not used, ok marco


# 1.68 19-Dec-2006 deraadt

simplify some low-level methods substantially (shrinking code); ok marco jordan


# 1.67 06-Dec-2006 mk

malloc(..., M_NOWAIT) with no NULL check is bad. If allocation fails,
complain, clean up, and bail out of attach.

input and ok gwk


# 1.66 28-Nov-2006 jordan

Fixed clearing arg for acpi_init_pic
ok kettenis@


# 1.65 27-Nov-2006 kettenis

Don't spam dmesg.


# 1.64 27-Nov-2006 jordan

Fixes for acpiprt routing
Post-parse fixup for namerefs
ok kettenis@


# 1.63 25-Nov-2006 marco

Don't handle acpiev events inside the gpe handler since this causes deadlocks.

ok jordan@


# 1.62 15-Nov-2006 kettenis

Hook up acpi apic interrupt routing support to generic acpi code.

ok jordan@


# 1.61 02-Nov-2006 marco

Kill acpiec attach function and add its functionality to the generic _HID
device attach

prodded and ok jordan


# 1.60 02-Nov-2006 marco

Fix comments to reflect reality.


# 1.59 19-Oct-2006 marco

Replace ACPI ioctls with APM ioctls. This makes ACPI talk the same protocol
as APM therefore userspace can now retrieve power information in the same
fashion as all other apm arches (zaurus, macppc, i386 etc).

To use force the use of acpi device like so: apmd -f /dev/acpi

ok jordan@


# 1.58 19-Oct-2006 jordan

Fixed GPE debug levels
Fixed ref counter on temporary bufferfields
DSDT parsing fully working on IBM Thinkpad
ok marco@


# 1.57 15-Oct-2006 jordan

Removed old GPE handler code remnants
ok marco@


# 1.56 12-Oct-2006 jordan

Massive ACPI changes
Fixed ACPI leaks
new ACPI API
ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.55 30-Jun-2006 gwk

KNF, No functional change,
"go at it" marco@


# 1.54 30-Jun-2006 jordan

Support for calling Embedded Controller via new GPE interface
ok marco@


# 1.53 30-Jun-2006 jordan

Added new code for GPE handling
Also works with ACPIEC; need to fixup acpiec.c to remove original interrupt handler
ok marco@


# 1.52 30-Jun-2006 jordan

Added offset argument for acpi read/write pmreg routines (new GPE code)
ok marco@


# 1.51 31-May-2006 todd

do not call acpi_powerdown() unless acpi has successfully initialized
ok gwk@ canacar@


# 1.50 29-May-2006 canacar

Add embedded controller support.
ok marco@ deraadt@ testing & ok gwk@


# 1.49 19-May-2006 canacar

Minimal thermal zone driver. Monitors thermal zone temperature,
shuts down the system if the 'critical temperature' is reached.
ok marco@


# 1.48 11-Apr-2006 marco

KNF.


# 1.47 11-Apr-2006 gwk

Add acpi_resume(struct acpi_softc *sc), which calls some AML methods,
detected at attach time, currently unused.

ok marco@


# 1.46 11-Apr-2006 gwk

Discover some ACPI sleep related methods, and call them at the
appropriate time in acpi_enter_sleep_state().

ok marco@


Revision tags: OPENBSD_3_9_BASE
# 1.45 26-Feb-2006 marco

Add acpicpu as a device.

ok jordan@


# 1.44 23-Feb-2006 marco

remove dead function.

ok jordan@


# 1.43 22-Feb-2006 marco

Print out all tables it found during autoconf. We'll need this data in
the future.


# 1.42 22-Feb-2006 marco

Make dmesg less verbose.

ok jordan.


# 1.41 22-Feb-2006 jordan

Added parsing of SSDT tables
ok marco@


# 1.40 22-Feb-2006 jordan

Added new aml_register to notify by device id
Moved powerdown code to button device
ok marco@


# 1.39 21-Feb-2006 marco

Readjust debug levels that crept in too low over the last few days.
KNF


# 1.38 21-Feb-2006 marco

Rig the power button so that the machine shuts down correctly.


# 1.37 20-Feb-2006 marco

Add acpibtn device. Remains disabled for now.


# 1.36 19-Feb-2006 jordan

Added code for aml_notify GPE notification callback
ok marco@


# 1.35 19-Feb-2006 grange

Fix acpi_enter_sleep_state() by replacing current ugly hack
with mostly correct sleeping state transition code.

ok brad@ marco@


# 1.34 19-Feb-2006 marco

jordan:
Enable interrupts.
Added peripheral code to make this work.

marco:
Replace softintr and other types of interrupts with a handler with context.
Fixed interrupt magic for GPEs.
Some KNF

This is work done by jordan and I but has to be a single commit.


# 1.33 17-Feb-2006 marco

Remove earlier introduced delays. We really only need them during autconf.

ok jordan@


# 1.32 17-Feb-2006 marco

Add a generic delay function as per the ACPI spec.

from jordan@


# 1.31 17-Feb-2006 marco

Use tsleep instead of delay when !cold.


# 1.30 17-Feb-2006 marco

Argh, we need delays between reads/writes. The backend reads (i2c most likely)
are so slow that values come back as 0 sometimes. We have to do this
despite the spec saying we shouldnt have to. Guess having a faster
parser than others can hurt sometimes.

Idea from jordan.


# 1.29 17-Feb-2006 jordan

Fixed code for handling _INI
Rearranged debug statements
ok marco@


# 1.28 16-Feb-2006 jordan

Added stub for calling _INI
Fixed aml_dowhile for NULL return
ok marco@


# 1.27 16-Feb-2006 jordan

Fixed parser/evaluator to retrieve battery info+state
Cleanup of parser
Dell laptops return battery status
ok marco@


# 1.26 06-Feb-2006 brad

fix a typo so this will compile on amd64.


# 1.25 04-Feb-2006 marco

When ACPI_ENABLED wasn't set but the devices were it had an unknown variable.

Pointed out by ian@.


# 1.24 03-Feb-2006 jordan

Rewrote aml parser yet again...
Fixed bus_space_map for i386/amd64
Added _BST and _BIF to acpibat to get status/info
ok marco@


# 1.23 20-Jan-2006 jordan

More evaluator changes
Removed old evaluator code
Fixed _HID value
ok marco@


# 1.22 19-Jan-2006 jordan

Added support for read/write of PCI Config space
Help jason@
ok marco@


# 1.21 18-Jan-2006 jordan

Added new evaluation routines
Added helper methods for buffer/field evaluation
Fixed broken indentation on dsdt.c

ok marco@


# 1.20 17-Jan-2006 jordan

Major overhaul of the aml parser. Evaluater temporarily disabled.

ok marco@


# 1.19 06-Jan-2006 grange

Check for ACPI control availability only if ACPI_ENABLE defined.


# 1.18 06-Jan-2006 grange

Informative error messages on attach, prettier dmesg.


# 1.17 06-Jan-2006 grange

printf -> dnprintf conversion for debug messages.


# 1.16 05-Jan-2006 grange

Find SLP_TYPx values and store them for future use in
acpi_enter_sleep_state().


# 1.15 05-Jan-2006 grange

Random KNF and cleanup


# 1.14 05-Jan-2006 grange

Provide symbolic names for the acpi sleeping states and use it.


# 1.13 28-Dec-2005 marco

Moved DEVNAME to a acpidev.h

ok jordan@


# 1.12 18-Dec-2005 sturm

add back comment about wrong code

ok marco


# 1.11 16-Dec-2005 marco

Remove test for kbd_reset. Prompted deraadt@, ok jordan@


# 1.10 16-Dec-2005 jordan

Added hooks for powerdown/power button press


# 1.9 16-Dec-2005 jordan

Cleaned up common power management register i/o
Added enter sleep state ioctl
Hooked power button interrupt
ok marco@


# 1.8 16-Dec-2005 marco

Remove autoconf chatter


# 1.7 16-Dec-2005 jordan

Moved DSDT AML parsing into parent ACPI driver
ok marco@


# 1.6 14-Dec-2005 marco

Add AC device glue.


# 1.5 14-Dec-2005 marco

Adjust debug printing levels to something more sane.


# 1.4 13-Dec-2005 marco

Add glue to attach devices to apci.


# 1.3 07-Dec-2005 marco

KNF


Revision tags: OPENBSD_3_8_BASE
# 1.2 10-Jul-2005 grange

Spacing cleanup.


# 1.1 02-Jun-2005 tholo

Start on a basic ACPI framework -- does not do much more than read out the
ACPI tables into kernel memory and attach ACPI and HPET timers currently.

In order to test this code, enabling the devices in GENERIC as well as
the ACPI_ENABLE option is needed. This code does not do any thermal
control yet, so this should be done with care depending on the platform.

In the tree so more people can contribute to making this more fully
featured.

Ok niklas@ grange@ tedu@


# 1.364 08-May-2019 tedu

free size. the allocation is the header plus the length.


Revision tags: OPENBSD_6_5_BASE
# 1.363 04-Apr-2019 kettenis

Avoid potential null-pointer dereference. Found by Kent R. Spillner.

ok deraadt@, jsg@


# 1.362 20-Jan-2019 jsg

fix SMALL_KERNEL build after changes in rev 1.361
reported by naddy@ ok deraadt@


# 1.361 19-Jan-2019 tedu

Add a pwraction sysctl that controls what the power button does on acpi.
By default, nothing changes -- shutdown is initiated. But allows turning
power button into a sleep button if desired.
(grudging) ok from a few parties


# 1.360 26-Oct-2018 kettenis

Add acpipci(4) on amd64. For now this only calls the PCI-specific _OSC
method to let the ACPI implementation know what features we support.


Revision tags: OPENBSD_6_4_BASE
# 1.359 30-Aug-2018 kettenis

Move softbutton check before the check that skips attaching certain acpi
devices. Fixes regression caused by matching on _CID in addition to
matching on _HID.

ok matthieu@, mlarkin@


# 1.358 25-Aug-2018 kettenis

Match ACPI devices based on _CID if no match for _HID is found.

ok mpi@


# 1.357 19-Aug-2018 kettenis

Add support for multiple PCI segments. Only really implemented for arm64
for now as amd64/i386 firmware still caters for legacy OSes that only
support a single PCI segment.

ok patrick@


# 1.356 03-Aug-2018 kettenis

Let ahci(4) match on _CLS instead of _HID when attaching at acpi(4). Avoids
having to add many more _HID entries to the match table.

ok deraadt@, mlarkin@


# 1.355 10-Jul-2018 kettenis

Make legacy interrupts work in acpipci(4).

ok patrick@


# 1.354 05-Jul-2018 kettenis

Add acpipci(4), a driver that supports generic ECAM-compatible PCI host
bridges based on information provided by ACPI.

ok mlarkin@


# 1.353 04-Jul-2018 kettenis

Properly pass around the PCI "chipset tag" in acpi(4) and refactor
acpimcfg(4) to call an MD initialization functions that sets up a tag for
PCI ECAM.

ok guenther@, mlarkin@, krw@


# 1.352 01-Jul-2018 kettenis

Move acpi(4) attach glue into acpi_machdep.c.

ok guenther@, deraadt@


# 1.351 01-Jul-2018 kettenis

Add support for _DSD Device Properties.

ok mlarkin@


# 1.350 30-Jun-2018 kettenis

Add intr_enable() function, intended for MI use to amd64 and i386 and use
this in the acpi(4) suspend/resume code paths.

ok deraadt@


# 1.349 29-Jun-2018 kettenis

Use <stdint.h> types.

ok mpi@, jung@, krw@, deraadt@


# 1.348 29-Jun-2018 mlarkin

ACPI: Allow (with warning) GPE handler reassignment, instead of returning
a failure code.

ok kettenis


# 1.347 26-Jun-2018 mlarkin

knf


# 1.346 26-Jun-2018 kettenis

Let acpi(4) pass down the bus dma tag.

ok deraadt@


# 1.345 25-Jun-2018 kettenis

Reorganize acpi(4) code a bit in preparation for upcoming arm64 support.

ok deraadt@


# 1.344 20-May-2018 kettenis

Wake up the acpi thread after scheduling a gpio event.

ok mpi@


# 1.343 17-May-2018 kettenis

Sprinkle some #ifndef SMALL_KERNEL such that RAMDISK kernels cuild again.

pointed out by stsp@


# 1.342 17-May-2018 kettenis

Implement GenericSerialBus OpRegion support.

ok mlarkin@


# 1.341 27-Mar-2018 kettenis

Add acpicmos(4), a driver that implements SystemCMOS OperationRegion
access support. This fixes machines where the AML doesn't check whether
support for this OperationRegion type has been registered by the OS.

ok mlarkin@


Revision tags: OPENBSD_6_3_BASE
# 1.340 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.339 10-Feb-2018 deraadt

Syncronize filesystems to disk when suspending. Each mountpoint's vnodes
are pushed to disk. Dangling vnodes (unlinked files still in use) and
vnodes undergoing change by long-running syscalls are identified -- and
such filesystems are marked dirty on-disk while we are suspended (in case
power is lost, a fsck will be required). Filesystems without dangling or
busy vnodes are marked clean, resulting in faster boots following
"battery died" circumstances.
Tested by numerous developers, thanks for the feedback.


# 1.338 08-Feb-2018 deraadt

After flushing disks, try again to release all memory. We don't need to
save dirty memory to the hibernate space.


# 1.337 08-Feb-2018 deraadt

Toss all releaseable memory, because fragmentation can get in the way
of allocating the hibernate playpen.


# 1.336 08-Feb-2018 deraadt

sr_quiesce() is a new approach for ensuring that softraid drains
output to the disks.

This is part of a larger suspend/resume filesystem-safety diff, which
has been worked on for a couple of months already. Tests by job, krw,
beck, benno, and others. Sometimes even by snapshot users...


# 1.335 29-Nov-2017 kettenis

Add support for GPIO-signaled events.

ok mlarkin@


# 1.334 16-Nov-2017 jcs

Add PCI attachment for dwiic(4) needed by Intel 100 Series machines

ok kettenis


# 1.333 14-Nov-2017 kettenis

Add a few more PNP IDs to the skip lists.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.332 17-Aug-2017 stsp

Make dwiic(4) attach its ACPI parent dependencies.
This change makes the Asus E200HA keyboard work.

Original analysis and patch by Cesare Gargano
ok kettenis@


# 1.331 09-Aug-2017 dcoppa

Add "PNP0303" (8042 PS/2 Controller) to acpi_isa_hids[]

ok kettenis@


# 1.330 22-Jul-2017 jcs

acpibat: look for _BIX first before falling back on _BIF

ACPI 4.0 deprecated _BIF for battery status so some newer machines
are exporting _BIX which extends _BIF's package with a few extra
fields. When using _BIX, export the new discharge cycle count as a
sensor.

feedback from kettenis and anton
ok deraadt


# 1.329 20-Jul-2017 kettenis

Start a list of isa(4) device names such that we can avoid printing them
as "not configured".

ok mikeb@, millert@, deraadt@


# 1.328 08-Apr-2017 deraadt

Sensors are run as callbacks inside tasks. During suspend a sensor
could be running inside a driver that will be force-detached, or due
to tsleep end up disrupting the softstate/hardstate contract. At
suspend time, quisce all these callbacks by waiting for completion.
This issue has never been observed for real, but may be implicated in
suspend/resume failures.
ok kettenis guenther mlarkin


# 1.327 08-Apr-2017 deraadt

Two prototypes for acpi_maptable; merge to one place


Revision tags: OPENBSD_6_1_BASE
# 1.326 27-Mar-2017 deraadt

Now that hibernate_alloc() only has clean success/failure, don't
need to call hibernate_free() to clean up a partial mess.
ok mlarkin kettenis


# 1.325 13-Mar-2017 deraadt

allow ramdisks to build; ok jcs


# 1.324 12-Mar-2017 jcs

try to attach acpisbs first and if successful, don't attach acpibat


# 1.323 02-Mar-2017 natano

Add a new sysctl machdep.lidaction. The sysctl works as follows:

machdep.lidaction=0 # do nothing
machdep.lidaction=1 # suspend
machdep.lidaction=2 # hibernate

lidsuspend is just an alias for lidaction, so if you change one, the
other one will have the same value. The plan is to remove
machdep.lidsuspend eventually when people have upgraded their
/ets/sysctl.conf.

discussed with deraadt, who came up with the new MIB name
no objections mlarkin
ok stsp halex jcs


# 1.322 28-Feb-2017 natano

Untangle abstract sleep modes from ACPI sleep states. This paves the way
for further work in the area.

idea and ok deraadt
ok mlarkin


# 1.321 22-Feb-2017 jcs

add acpisbs, an acpi smart battery subsystem driver reading data
over smbus

currently disabled because it conflicts with acpibat

ok deraadt, kettenis


# 1.320 14-Jan-2017 kettenis

Revert aml_rdpciaddr changes; breaks several machines that were working
before.


# 1.319 11-Jan-2017 jsg

Increase the size of the buffers used for _HID and _CID strings to be
able to fit "Hyper_V_Gen_Counter_V1".

ok mlarkin@ deraadt@


# 1.318 08-Jan-2017 kettenis

Make aml_rdpciaddr() work for for devices behind bridges. The code now
uses the mapping between AML nodes and PCI devices that we establish.
Because _INIT methods may end up calling aml_rdpciaddr(), make sure we
create that mapping early. Also handle devices that aren't actually present.
These devices are now included in the mapping and reads will return an
all-ones patterm whereas writes are a no-op..

ok mlarkin@


# 1.317 25-Oct-2016 pirofti

Add the resource index to aml_parse_resource() callback function.

This is needed by an upcoming acpiec commit that handles machines breaking
the current ACPI specifications.

Change suggested by and ok kettenis@, guenther@


# 1.316 18-Sep-2016 guenther

If the APIC table (MADT) isn't present then ci_acpi_proc_id won't be set
when acpicpu tries to attach, leading to a NULL dereference. The ACPI
processor object on the Dells where this was reported is bogus and useless
anyway so apply the same test when deciding whether to attach to processor
objects as acpicpu would when matching them to cpus.

Problem reported by Eivind Eide (xenofil (at) gmail.com) and
Johan Huldtgren (johan+openbsd-bugs (at) huldtgren.com).
Testing by Eivind Eide.
ok kettenis@ mlarkin@


# 1.315 03-Sep-2016 naddy

Write the system time back to the RTC every 30 minutes.
This fixes the problem that long-running machines which were not
shut down properly would reboot with a badly offset system time.

hints and ok kettenis@


# 1.314 31-Aug-2016 mlarkin

Don't put a device in the wake device list if _STA says it's not there.

Fixes at least one machine that woke up from sleep / poweroff immediately.

ok kettenis@, tested by kettenis and martijn@


# 1.313 28-Jul-2016 kettenis

Store the acpi processor ID/UID in struct cpu_info, and use it to attach
acpicpu(4) drivers to the right cpu(4).

ok mlarkin@, guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.312 10-Jun-2016 kettenis

The power management timer has been made optional in ACPI 5.0A, so don't
attempt to attach acpitimer(4) if the timer isn't present.

Pointed out by jcs@

ok jcs@


# 1.311 08-May-2016 kettenis

Make sure devices listed by a device's _DEP method are attached before
we attach the device itself. The _DEP methode, introduced in ACPI 5.0,
is a hint that the device in question depends on OpRegion support from
the devices listed by _DEP, so we have to order them the proper way.
To prevent us from attaching those devices again when we encounter them
later walking down the device tree, keep track of our attempts to
attach a device driver to them using aflag in the aml_node structure.

ok guenther@


# 1.310 25-Apr-2016 pirofti

We attach acpihpet(4) and acpiec(4) without using the HID, so do not
report it in the dmesg.

OK deraadt@, mlarkin@.


# 1.309 02-Apr-2016 kettenis

Reduce dmesg spam by knocking out some acpi hardware IDs that don't need a
driver.

ok mpi@


# 1.308 29-Mar-2016 kettenis

Check if a device is present (using _STA) in acpi_foundhid. This prevents
us from reporting devices as "not configured" that aren't actually present,
and allows us to remove duplicated code from several drivers.

ok deraadt@, ok jsg@


# 1.307 29-Mar-2016 kettenis

Implement a cleaner way to prevent acpivout(4) from attaching if we have
a platform-specific method of controlling the backlight.

ok millert@, mlarkin@, jung@


# 1.306 28-Mar-2016 kettenis

Adding each and every new acpi device driver to acpi_foundhid() is
gettinga bit of the burden. And it really isn't how our autoconf
framework is supposed to work for busses that can be enumerated. All
the drivers already check for a matching _HID in their attach
function. So we can just drop the checks in acpi_foundhid() and let
the drivers sort themselves out.

Print a "not configured" line for devices for which we don't attach adriver.
This may be a bit much as there are devices for which we will not have a
driver. This will be removed if it causes too much dmesg spam.

ok deraadt@, mlarkin@


Revision tags: OPENBSD_5_9_BASE
# 1.305 17-Jan-2016 jsg

Take acpi_foundhid() outside of #ifndef SMALL_KERNEL so the ramdisk
kernel can find and attach dwiic(4) and sdhc(4).


# 1.304 17-Jan-2016 jsg

remove an unused global variable


# 1.303 14-Jan-2016 kettenis

Attach dwiic(4) on the Intel Bay Trail i2c controllers as well.


# 1.302 12-Jan-2016 kettenis

Improve emulation of PM registers on Hardware-redcued ACPI by emulating
SPL_TYPx, SLP_EN and WAK_STS using the new SLEEP_STATUS_REG and
SLEEP_CONTROL_REG registers. Makes the transition into S5 (powerdown) work
on the ASUS X205TA.

ok deraadt@, mlarkin@


# 1.301 12-Jan-2016 jcs

Add dwiic, a driver for the Synopsys DesignWare i2c controller found
on the Samsung ATIV Book 9 laptop. This initial version only
supports ACPI config/attachment.

Add ihidev, a HID-over-i2c driver largely based on uhidev. dwiic
handles attaching ihidev devices found in ACPI.

Add ims, a HID-over-i2c mouse/trackpad driver to get basic cursor
and button functionality from HID-compliant i2c trackpads.

ok kettenis deraadt


# 1.300 11-Jan-2016 kettenis

Make sdhc(4) attach to hardware IDs "80860F14" and "PNP0FFF".


# 1.299 09-Jan-2016 kettenis

Initial support for Hardware-Reduced ACPI. This skips the legacy mode checks
by emulating the PM_CNT register such that SCI_EN is always enabled. It also
skip attaching acpitimer(4) since the relevant registers are no longer there.

ok deraadt@, mlarkin@


# 1.298 06-Jan-2016 kettenis

Follow the spec and check the SCI_EN bit to see if ACPI has already
been enabled and don't bother enabling it again if it is. This should fix
systems that don't implement legacy mode and therefore set ACPI_ENABLE and
ACPI_DISABLE to zero.

ok mlarkin@, yasuoka@


# 1.297 23-Nov-2015 reyk

Back out the CRITICAL change, turn it back from 10% to 25%. Some
people with old/broken batteries didn't like this change, and the
underlying heuristics are wrong.


# 1.296 22-Nov-2015 reyk

The "CRITICAL" APM battery level, that is exported/faked by the ACPI
stack, is currently set to 25%. With 25% battery left, a typical
modern laptop will have at least one hour runtime left. It is an
arbitrary value, and other APM power layers in the kernel set CRITICAL
to something like 5% or 10%, so set it to 10% for now.

OK mlarkin@


# 1.295 28-Sep-2015 deraadt

In low-level suspend routines, set cold=2. In tsleep(), use this to
spit out a ddb trace to console. This should allow us to find suspend
or resume routines which break the rules. It depends on the console
output function being non-sleeping.... but that's another codepath which
should try to be safe when cold is set.
ok kettenis


# 1.294 06-Sep-2015 deraadt

clarify malloc arguments (base + length...)


# 1.293 28-Aug-2015 deraadt

fairly simple sizes for free(); ok tedu


# 1.292 20-Aug-2015 kettenis

Respect the access size when reading or writing to pci config space.
This version also makes sure writes are properly aligned, preventing
the panics seen with the previous incarnation of this change.

Fixes battery status passthrough in vmware, brightness keys on my x220
and who knows what else.

ok mlarkin@, deraadt@


Revision tags: OPENBSD_5_8_BASE
# 1.291 04-Aug-2015 deraadt

If we are setting ACPI_PM1_BM_RLD do it only once, and remember to
repeat this action at resume time.
ok mlarkin guenther


# 1.290 29-Jul-2015 deraadt

Something subtle in previous revision is locking up suspend or booting
on some laptops. Mailing lists threads mention x220 and Asus machines.
Reverting for now.


# 1.289 21-Jul-2015 kettenis

iRespect the access size when reading or writing to pci config space.
Fixes battery status passthrough in vmware and who knows what else.

Tested by many.

ok mlarkin@, deraadt@


# 1.288 17-Jul-2015 mlarkin

fix comments


# 1.287 26-Mar-2015 jsg

Use the 0x10 extended capability to determine if the trackstick
buttons are wired to the trackpad and need to be re-routed to the
trackstick. Without this change the buttons on 2015 thinkpads
get picked up as extended buttons that show up as scroll up/down.

Remove the X1 Carbon 2015 (LEN0048) and X250 (LEN0046) from the
top button area/soft buttons quirks list. Also avoid using the
quirk list entirely if the capability bit is set.

Based on work from jcs@ with feedback from mpi@
ok mpi@ jcs@


# 1.286 20-Mar-2015 kettenis

The ACPI standard says that a PCI function number of FFFF can be used to refer
to all functions on a device. This makes no sense in the context of accessing
PCI config space. Nevertheless stsp@ has a machine with AML that tries to
do config space reads from a device where _ADR encodes function number FFFF.
This is almost certainly buggy AML, but since Linux (and presumably Windows)
boot just fine on this machine we need to work around this. Linux effectively
treats function FFFF as function 7, which isn't present on this machine.
Instead we simulate a read from a non-existent device by returning a bit
pattern of all ones. Writes to function FFFF will still panic.

ok mlarkin@


# 1.285 16-Mar-2015 deraadt

Reduce use of SMALL_KERNEL in acpi, bringing acpiec driver and more gpe
handling into RAMDISK. This is now possible because the install media
has ample room. The goal is to reduce special cases where we may be
skipping (unknown) important operations...
ok mlarkin kettenis


# 1.284 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.283 07-Feb-2015 deraadt

New framework that allows hibernate to pass in entropy from it's fresh
boot.
ok mlarkin


# 1.282 06-Feb-2015 mlarkin

Fix a hibernate crash on some machines due to unmapping a page that
may not have been mapped previously (in the failure to hibernate case).

Also ensure that the lowmem ptp is mapped in all cases (not just MP).

ok kettenis


# 1.281 17-Jan-2015 deraadt

resume_randomness() before spinning up other cpus...


# 1.280 15-Jan-2015 jsg

Define a new wscons mouse type for Synaptics clickpad devices that lack
physical buttons. This will be used if the acpi pnpid for the mouse
matches a list provided by Synaptics found in Linux.

Combined with the xenocara changes this will remove the need for an xorg
config file for the x240/t440/t540 generation of thinkpads.

ok matthieu@ shadchin@ kettenis@


# 1.279 11-Jan-2015 kettenis

Tolerate checksum errors in ACPI tables (but complain about them). Apparently
there are machines out there with broken BIOSen with such checksum errors in
thier RSDT tables. Windows runs fine on these machines, so it probably does
not check. Linux ignores checksum errors as well (and prints a warning).

ok deraadt@, krw@


# 1.278 18-Dec-2014 deraadt

suspend and resume the RNG (at what looks like the right spots)
ok reyk djm


# 1.277 09-Dec-2014 doug

Replace some malloc(n*size,...) calls with mallocarray().

ok tedu@ deraadt@


# 1.276 06-Dec-2014 mpi

If "machdep.lidsuspend" is not set, do not go back to sleep if the lid
is still closed when resuming. This allows docked laptop to be resumed
without opening them.

ok mlarkin@


# 1.275 23-Nov-2014 mlarkin

If a machine has no _LIDs, then none can be open. This diff fixes a problem
where machines without any _LIDs were going back to sleep after hibernate
due to the recent suspend-if-lid-closed-on-resume diff. Noticed on qemu,
which doesn't have any _LIDs.

discussed with deraadt


# 1.274 18-Nov-2014 krw

Nuke yet more obvious #include duplications.

ok miod@


# 1.273 08-Nov-2014 mlarkin

If resuming from sleep (zzz/ZZZ) and the lid is still closed, go back to
sleep. This prevents accidental lid flex or slight opening in a backpack
from waking the machine up and leaving it resumed (powered on).

ok deraadt@


# 1.272 26-Sep-2014 kettenis

Rework piglet and pig allocation. Currently the piglet gets allocated
deep down in the suspend path, where it is really hard to recover from
allocation failure. So allocate the piglet early on in the suspend path.
Also change the piglet and piglet allocation functions to use km_alloc(9)
instead of doing pmemrange magic. This removes a bunch of code which, in the
case of the piglet allocation, is broken since it results in a NULL pointer
dereference. Also switch the piglet allocation to not wait. If we can't
allocate 16MB of phys contig memory on a halfway modern machine we're almost
certainly under a lot of memory pressure and we're better off not trying to
hibernate anyway.

ok mlarkin@


# 1.271 25-Sep-2014 kettenis

Bring back rev 1.249. Now that mp_setperf() has been fixed, it should be
safe to restore perflevel when resuming. Should fix the issues people
see with the processor running at full speed heating up the machine while
the apm command reports that the CPUs are running at a low speed.

ok dcoppa@, deraadt@


# 1.270 23-Sep-2014 kettenis

Release the acpi lock around calls to wsdisplay_suspend() and
wsdisplay_resume(). The X server may adjust the display brightness just
before/after the VT switch when suspending/resuming. In fact, it may
already be blocked adjusting the display brightness for some reason
unrelated to the suspend request.

The VT switch request will eventually time out, but the delay is annoying
and the failed VT switch will lead to problems after resume.

Needed for a future update of the xf86-video-intel driver.

ok mlarkin@


# 1.269 19-Sep-2014 kettenis

Use config_suspend_all(9).


# 1.268 08-Sep-2014 kettenis

Clear the PME Status bit on PCI devices in response to Device Wake events.
Fixes the interrupt storm on the 2009 Mac mini with WOL enabled on its
nfe(4) interface.

ok mlarkin@


Revision tags: OPENBSD_5_6_BASE
# 1.267 20-Jul-2014 mlarkin

Support hibernating to softraid crypto volumes.

much help and ok from deraadt@


# 1.266 16-Jul-2014 mlarkin

Reenable hibernate RLE support and flush+zero all memory after unpack.


# 1.265 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.264 12-Jul-2014 kettenis

Add a function to drop all clean pages on the page daemon queues and call
it when we hibernate.

ok mlarkin@, miod@, deraadt@


# 1.263 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.262 11-Jul-2014 mlarkin

Flush the buffercache to 16MB on hibernate and restore its previous max
size (kern.bufcachepercent) on resume, for better hibernate performance.

ok beck@


# 1.261 10-Jul-2014 blambert

remove unneeded header

ok mlarkin@


# 1.260 01-Jul-2014 kettenis

Adding an additonal delay after each access to SystemMemory or SystemIO
during autoconf makes no sense.

ok deraadt@


# 1.259 23-Jun-2014 kettenis

SystemMemory is used to access memory mapped registers on some machines, so we
must use the bus_space(9) API to access it instead of memcpy(9). Also make
sure we properly align access to fields that need more than byte alignment.

ok guenther@


# 1.258 21-May-2014 mlarkin

Fix invalid / missing format specifiers in several dnprintf statements
(used only when ACPI_DEBUG is enabled)

From a diff on tech@ by Fabian Raetz, with a minor change by me, tested
on i386 and amd64 with ACPI_DEBUG enabled and disabled.


# 1.257 25-Apr-2014 mlarkin

Defer the free of the hibernate structures until later, when it is safe to
do so.

Freeing the hibernate structures too early leads to inadvertent calls into
splx when we aren't ready for them yet. Found when trapping splx calls in
the resume path.

discussed with deraadt@ at m2k14


# 1.256 13-Apr-2014 mlarkin

Fix a badly worded debug message that implied the entire machine was going
to sleep, instead of just the acpi thread going to sleep.


# 1.255 31-Mar-2014 mpi

Change the order of operations during a suspend/resume cycle and
call bufq_quiesce() after executing the DVACT_QUIESCE handlers.

This should be safe since no disk nor controller drivers have such
handler but it will allow us to detach sd(4) devices attached to a
USB bus.

Another benefit pointed out by kettenis@ is that drivers that need
to read a firmware from the disk should be able to do it at resume
time in a DVACT_WAKEUP handler.

ok kettenis@, deraadt@


# 1.254 13-Mar-2014 dlg

get rid of the assumption that the head of the alldevs list is the
"mainbus" device. this breaks when mpath is enabled because it
attaches before mainbus and therefore takes the head position.

have autoconf provide device_mainbus() which looks up mainbus_cd,
and use that instead.

discussed with deraadt who just wants mpath stuff to move forward
despite there being many ways to shine this particular turd.


Revision tags: OPENBSD_5_5_BASE
# 1.253 21-Feb-2014 deraadt

Do the lid control further on in the resume sequence, since this is
AML, and will do WAITOK operations. While cold, that is a bad idea.
We seem to be safe with respect to the spec, I do not see any event
debounce issues here.
ok kettenis, tested by many


# 1.252 20-Jan-2014 claudio

Instead of checking sc->sc_revision <= 1 check that the
fadt->hdr_revision <= 1. This is a better check since it checks
the version of the proper descriptor. This allows the alix APU
to reboot since those boards have currently a wrong sc->sc_revision.
Committing this now so that it can be tested easier.
With and OK jsg@ also fine by derradt@


# 1.251 24-Dec-2013 kettenis

If the FADT has its SMI_CMD set to zero, assume we're only ACPI-only hardware
and don't need to disable SMI ownership of the ACPI hardware registers.

ok mlarkin@


# 1.250 23-Dec-2013 kettenis

Revert previous commit. It makes resume hang on some MP systems running
GENERIC.MP.


# 1.249 16-Dec-2013 kettenis

Restore hw.setperf upon resume like we do for apm(4) on i386. Seems we need
to do this fairly late in the resume to avoid hangs.

ok deraadt@, mlarkin@


# 1.248 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


# 1.247 06-Nov-2013 mpi

Add support for Power Resources for Dx states and the necessary hook
for PCI devices. This hook should be called twice, before and after
changing the power state of a PCI device.

Before setting the device to the new state, the ACPI layer will notify
every power resources linked to the device for that state and make sure
they are turned "_ON". After changing the state of the device, it will
decrement the reference of every power resources linked to that device
for the old state and turn them "_OFF" if they are no longer referenced.

This fixes the no-USB after resume problem seen on various ThinkPad,
problem initialy diagnosed with Alexander Polakov.

ok kettenis@, deraadt@


Revision tags: OPENBSD_5_4_BASE
# 1.246 01-Jun-2013 mlarkin

Stop printing ACPI wakeup devices in dmesg after the 16th wakeup device.

Helps with VMware VMs that report hundreds of wakeup devices.

ok reyk@, deraadt@


# 1.245 31-May-2013 bluhm

Fix the build for a kernel without wd and pciide in its config.
OK deraadt@ tedu@


# 1.244 30-May-2013 deraadt

Enforce ca_activate tree-walks over the entire heirarchy for all events,
cleaning up some shutdown-hook related code on the way.
(A few drivers related to sparc are still skipped at kettenis' request)
ok kettenis mlarkin, tested by many others too


# 1.243 18-Apr-2013 deraadt

pin the acpi thread to the BSP, because AML and SMI cannot be trusted. I
was certain we were doing this already, but it turns out no...


Revision tags: OPENBSD_5_3_BASE
# 1.242 24-Dec-2012 guenther

In acpiioctl(), Return though the splx() path if suser() returns an error

ok deraadt@


# 1.241 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


# 1.240 04-Oct-2012 ehrhardt

Use information provided by ACPI to attach secondary PCI host bridges.

ok kettenis@


# 1.239 07-Sep-2012 kettenis

Add a new function called acpi_pci_min_powerstate() that returns the lowest
power state for a PCI device for the power state (currently S3 or S4) we're
transitioning into. This function is probably too simplistic; there is a lot
of (silly) complexity in the ACPI spec.

ok deraadt@


Revision tags: OPENBSD_5_2_BASE
# 1.238 13-Jul-2012 pirofti

Disable acpivideo(4) on ASUS laptops.

There are some models that panic whenever an event is received.
Some cope, some don't. In order to get this right acpiec(4) requires
major rework. Until then let machines boot.

Discussed with deraadt@.


# 1.237 11-Jul-2012 mlarkin

for now, restrict hibernate to root in the acpi ioctl kernel interface.
normal rules for controlling this interface through apmd still apply.

ok deraadt@


# 1.236 09-Jul-2012 deraadt

tiny little oops in untested hibernate block


# 1.235 09-Jul-2012 deraadt

clean up ioctl() case statements


# 1.234 09-Jul-2012 deraadt

acpi_disable_onegpe() is not used; ok pirofti


# 1.233 24-May-2012 kettenis

If _MIN > _MAX consider a WordBus resource to be invalid. Fixes a panic on
the IBM x3550-M4.

ok mikeb@


# 1.232 29-Mar-2012 mlarkin

Missing #ifdef in previous diff


# 1.231 29-Mar-2012 mlarkin

Prevent hibernation attempts on machines with unsupported disk controllers.
The same check is also performed later in the hibernate sequence, but by
then it's too late to cleanly unwind (presently).

ok deraadt


# 1.230 26-Mar-2012 deraadt

simplify conditionals in acpi_sleep_state()
ok mlarkin


# 1.229 26-Mar-2012 deraadt

hook in the hibernate request code; half of this diff is from mlarkin
ok mlarkin


Revision tags: OPENBSD_5_1_BASE
# 1.228 20-Sep-2011 deraadt

Very early in suspend while the clock is still ticking realtime,
save the clock back to the rtc. The zaurus already did this.
ok phessler miod kettenis


Revision tags: OPENBSD_5_0_BASE
# 1.227 02-Jul-2011 nicm

kqueue attach functions should return an errno or 0, not a plain 1. Fix
the obvious cases to return EINVAL and ENXIO.

ok tedu deraadt


# 1.226 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.225 16-Jun-2011 pirofti

Import acpitoshiba driver.

This is originally written by Hiroyuki Aizu, ported and integrated
in our tree by Javier Vazquez <javama4 AT gmail> with tweaks and
suggestions by me.

Import prodded by deraadt@ and the lack of testers on tech@.


# 1.224 27-Apr-2011 jcs

attach acpithinkpad to newer lenovo models like the x120e

ok deraadt@


# 1.223 22-Apr-2011 jordan

Fix uninitialzied variables and formatting strings (-Wxxx errors)


Revision tags: OPENBSD_4_9_BASE
# 1.222 02-Jan-2011 jordan

Re-add code for ACPI taskqueue, eliminates need for state vars in acpi softc
Serialize processing ACPI tasks for sleep/powerdown/GPE event
ok deraadt@


# 1.221 31-Oct-2010 guenther

Revert last commit: it breaks resume on Thinkpads
"then please back it out" deraadt@


# 1.220 26-Oct-2010 jordan

Add task queue for ACPI gpe and notify handlers
This fixes eject on Dell Latitude dock and an issue on the Dell Mini
battery update.
ok deraadt


# 1.219 07-Oct-2010 jordan

Create separate functions for enabling wake and runtime gpes.
Remove old suspend/resume gpewalk function
ok deraadt, mlarkin


# 1.218 05-Oct-2010 jordan

split acpi_enable_onegpe to separate enable/disable functions
ok deraadt


# 1.217 05-Oct-2010 jordan

Added acpi_disable_allgpes function to clear GPE status
ok deraadt


# 1.216 05-Oct-2010 deraadt

clean up acpi_softc use more


# 1.215 05-Oct-2010 deraadt

delete a bit of #if 0 code


# 1.214 05-Oct-2010 deraadt

hide use of our global acpi_softc pointer a bit better


# 1.213 29-Sep-2010 deraadt

grab the acpi thread lock very early in acpi thread startup; not that it
is likely that anyone will race us... but why be careless.
ok kettenis


# 1.212 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.211 29-Aug-2010 jasper

- add a check for FWRITE here too.

ok miod@


# 1.210 27-Aug-2010 kettenis

Make PCI Power Management optional, and only enable it when acpi(4) attaches.

ok deraadt@


Revision tags: OPENBSD_4_8_BASE
# 1.209 08-Aug-2010 kettenis

Close race between the acpi thread and wscons ioctls running in process
context. We might rip this out post-release and replace it with a solution
that serializes things in a more obvious way.

ok deraadt@, miod@ and "those crazy texans who ok anything"


# 1.208 08-Aug-2010 deraadt

Report the acpi sleep states when on a RAMDISK; ok krw


# 1.207 07-Aug-2010 kettenis

Enforce acpi register access rescrictions.

ok mlarkin@, deraadt@, tested by marco@


# 1.206 07-Aug-2010 deraadt

upon resume, notify all the acpiac and acpibat drivers to update
their status. some laptops do not do this automatically.
ok kettenis canacar


# 1.205 06-Aug-2010 marco

Always call _PSW on _LID devices that support it to enable lid open to
resume the machine. Conversely disable it when the machine wakes up.

Tested by several ok deraadt


# 1.204 06-Aug-2010 deraadt

As part of a revamp of the PM1/GPE code, I write this diff to look for
unmanaged EN & STS bits in the PM1 register at interrupt time and report
them. As a side effect this splits the STS acknowledgement into two writes
(for power, and sleep) instead of one. The printf that is added (to spot
unmanaged STS bits) has not yet been triggered as far as we know.

Before the "write to PM1 registers at the right offset" diff went in, this
was not neccessary. But newer thinkpads do not have a working soft power
button without this diff.

We have no idea why.
ok mlarkin kettenis


# 1.203 05-Aug-2010 deraadt

PM1 power button events were generating 'fake events' to acpibtn PWRB,
but if there is no such device these were being tossed. Instead, arrange
for all power button handlers to flag a softc variable which is processed
by the thread; so that one place will know to request a powerdown (psignal
to init)
ok marco mlarkin


# 1.202 05-Aug-2010 deraadt

rename a variable which is annoyingly poorly named


# 1.201 05-Aug-2010 mlarkin

Write to the correct offset for various ACPI pmreg registers. Writing to
the correct register does properly clear the wake status bits and fixed
event bits, which allows some machines to have more than 1 suspend cycle.

bug spotted by deraadt@

tested by deraadt@ and myself on a half-dozen machines and others on
hackers@

ok deraadt@, marco@


# 1.200 05-Aug-2010 deraadt

Fix the power button. The pm1 register is 16 bits in size.
My mistake, a while back.
tested by krw


# 1.199 04-Aug-2010 kettenis

Print supported sleep states.

ok marco@, phessler@, deraadt@


# 1.198 03-Aug-2010 mlarkin

Use the proper flag for re-enabling certain hardware events (power btn,
sleep btn). The (incorrect) flag prevoiously used coincidentally had the
same value, so this shouldn't cause any different behavior than before.

tested on a variety of machines (i386, amd64, sp, mp) - no behavioral
change seen

ok deraadt@


# 1.197 28-Jul-2010 marco

Make all gasio failures look and behave the same.


# 1.196 28-Jul-2010 mlarkin

Delay a little longer on suspend before giving up. Fixes machines that
sometimes don't suspend because the sleep transition takes too long
(symptoms like failing to sleep but the backlight stays on).

ok deraadt@


# 1.195 27-Jul-2010 deraadt

remove XXX; EC can now be attached early when needed; ok jordan


# 1.194 27-Jul-2010 deraadt

more brackets feels good


# 1.193 27-Jul-2010 deraadt

XXX annotate a block in acpi_enter_sleep_state() that is pretty worrying


# 1.192 27-Jul-2010 deraadt

horrific indentation that nearly made me puke


# 1.191 27-Jul-2010 mlarkin

Set the sleeping indicator light on machines that support it, in case SMI
doesn't do it for us. Shows the 'moon' LED on older Thinkpads when
sleeping.

Tested on T43p, W500, T510 by me and on X61s by deraadt@.

ok deraadt@


# 1.190 27-Jul-2010 jordan

Early initialization of acpiec if ECDT table exists
Fixes hang when booting thinkpads while docked
ok deraadt


# 1.189 26-Jul-2010 deraadt

remove merge error which was commited


# 1.188 26-Jul-2010 pirofti

Add support for Sony ACPI hotkeys via a new driver: acpisony(4).

Currently it only works for the suspend button and tries to do right for
the brightness events, but I haven't found a Sony laptop that like to do
right so far.

In the future I want to make the brightness keys work on all Sony's and
also add support for the zoom hotkeys and whatever other funky keys I can
find on those things.

Okay deraadt@.


# 1.187 22-Jul-2010 deraadt

pretty large cleanup of SMALL_KERNEL


# 1.186 21-Jul-2010 deraadt

in the acpi GPE handler, late ack level interrupts not edge interrupts.
Duh.


# 1.185 21-Jul-2010 deraadt

Merge the edge and level gpe handlers into one (now possible since the
gpe structure tells us whether the gpe is edge or level)


# 1.184 21-Jul-2010 deraadt

delete #if 0 code we are not going to use; ok marco


# 1.183 20-Jul-2010 deraadt

make the mask code for acpi_enable_onegpe() match other similar code


# 1.182 20-Jul-2010 deraadt

Handle the acpi interrupt controller with proper edge/level handling,
wrapped in spltty to avoid racing against the interrupt controller.
Repair the gpe bit masking code while there, and do operations in the
right order
ok kettenis mlarkin, help from jordan at figuring out the order of
operations.


# 1.181 20-Jul-2010 matthew

When we're preparing to go to sleep, only call bufq_restart() if other
preparations failed.

ok kettenis@, thib@, deraadt@


# 1.180 19-Jul-2010 deraadt

permit compilation with ACPI_DEBUG


# 1.179 19-Jul-2010 deraadt

there is no need for a seperate dsdt_softc pointer; dsdt does not have a
softc but is just a part of the single acpi driver.
ok jordan


# 1.178 19-Jul-2010 deraadt

acpi_delay() is no longer needed; ok jordan


# 1.177 18-Jul-2010 mlarkin

Clear event status on resume, to avoid going back to sleep/reboot/shutdown
immediately after resume. Tested by a bunch of people on hackers@ on
various hardware. Fixes at least Toshiba NB200 and Gateway LT3103u.

ok marco


# 1.176 13-Jul-2010 deraadt

rename some thread related interfaces so they are logical


# 1.175 10-Jul-2010 jordan

Verify _ADR bdf is correct (some systems return 0xffff)
Works on Dell x9xx and Studio 1555


# 1.174 09-Jul-2010 jordan

Clear the noise


# 1.173 08-Jul-2010 jordan

Add mapping for ACPI device to PCI bdf (match autoconf tree)
Simplify resource parsing function to use buffer argument
Convert namespace linked lists to use queue macros
ok marco@, deraadt@


# 1.172 06-Jul-2010 deraadt

Use spltty() to lock downcalls from apm against the information being
modified by the acpi thread.
ok kettenis


# 1.171 06-Jul-2010 deraadt

Don't use a workq to do the suspend, because it races aginst the acpi
thread. Instead, just tell the acpi thread to do the suspend for us.
This makes apmd & zzz work correctly. While here, have acpithinkpad
attempt to post the event to apm before prompting the suspend itself.
ok kettenis marco mlarkin


# 1.170 05-Jul-2010 mlarkin

Reset register SCI_EN on resume. The ACPI spec clearly states we don't have
to do this, but many machines don't, or forget to, which leads to all sorts
of problems on resume (broken battery stats, overheating, failure to resume
more than once, etc).

tested by many, ok marco@, deraadt@


# 1.169 01-Jul-2010 thib

do bufq quiesce again, since vnd/the bufq_done call in
biodone has been fixed.


# 1.168 01-Jul-2010 jordan

Backout recent AML changes, breaks on R210 and others


# 1.167 01-Jul-2010 jordan

Simplified aml_parse_resource call, cleanup code
ok marco


# 1.166 01-Jul-2010 jordan

Fix subordinate bus # for multi-root PCI buses
ok marco, kettenis


# 1.165 30-Jun-2010 thib

Disable/partially backout the bufq quiesce changes as this
is causing havoc with vnds and release must be buildable.


# 1.164 29-Jun-2010 deraadt

Fix ramdisks (new code was placed inside a SMALL_KERNEL chunk)


# 1.163 29-Jun-2010 jordan

Add support for mapping ACPI to PCI devices
ok kettenis, deraadt


# 1.162 29-Jun-2010 kettenis

Call bufq_quiesce() on suspend to stop all I/O and bufq_restart on resume.
Makes suspend/resume work much better while doing disk I/O.

ok mlarkin@


# 1.161 29-Jun-2010 tedu

60 is a more reasonable guess for number of minutes in an hour than 100.
spotted by TeXitoi. ok deraadt


# 1.160 27-Jun-2010 jordan

Add _acpi_matchhids routine, precursor to pci device match


# 1.159 27-Jun-2010 mlarkin

Clear ACPI fixed event status on resume (power buttons/etc) so that some
machines don't immediately resume after suspending to S3.

Tested by kettenis@ and I on 5 or 6 machines, deraadt@ "yeah, looks good"


# 1.158 27-Jun-2010 jordan

Cleaned up acpi_maptable, adds to table list directly


# 1.157 07-Apr-2010 deraadt

If we have done a wsdisplay_suspend(), and ACPI decides it does not want to
suspend, we must roll back by calling wsdisplay_resume() or the virtual
console switching will be locked forever
ok oga


# 1.156 07-Apr-2010 kettenis

Make suspend/resume work on MP machines (running an MP kernel). Joint work
from mlarkin@ and me, with some amd64 fixes thrown in by deraadt@

ok marco@, deraadt@, pirofti@, mlarkin@


# 1.155 31-Mar-2010 kettenis

Bring /dev/apm support in line with apm(4). Make the suspend button send
a suspend request event to apmd(8) instead of suspending immediately. Also
keep track of whether /dev/apm and /dev/apmctl are currently open, such that
we can still suspend immediately if apmd(8) isn't running.

ok deraadt@, marco@, pirofti@, jsing@, oga@


# 1.154 30-Mar-2010 oga

Prevent the apmd/x races for good.

When we hit suspend time, go through all wsdisplays on the system. if
they are in mode MAPPED, but not MODE_DUMBFB then if possible do a full
vt switch to a !mapped vt, and prevent switching back until resume time.
This has to be called from MD code because this involves userland
running so that X can run the vt switch signal handler. This way, any
case where we are using the "poke registers from userland" model, we
will not be on the hardware when we go down, so the kernel can actually
handle thing properly.

Tested on several acpi laptops (by kettenis@ and ian@), x40 (me and
beck@ at LEAST) and zaurus (me). Maybe others, but if so I forgot who at
this time..

Idea from deraadt somewhere over the Faroe Islands (I thought of a
similar thing myself a while ago). Much prompting from him. Ok and
comments miod@


# 1.153 25-Mar-2010 oga

Fix & vs. && in the kqfilter code.

Based on a comparison with the apm code.

ok deraadt@, kettenis@


Revision tags: OPENBSD_4_7_BASE
# 1.152 26-Nov-2009 mlarkin

Visual (and on some machines, audio) feedback while the machine is
resuming via calls to aml _SST method. This also makes the "moon" LED on
thinkpads to blink during resume and cease when resume is completed.

ok pirofti@


# 1.151 26-Nov-2009 deraadt

Toggle into "cold" when we we disable interrupts, because tsleeping
in the AML interpreter and context switching into userland processes
tends to sometimes pop the ACPI trampoline page out of our address
space. Makes lots more machines work.
ok kettenis mlarkin


# 1.150 24-Nov-2009 jsg

in the resume path evaluate _WAK before dealing with device drivers
like the spec says, not after.

ok mlarkin@


# 1.149 24-Nov-2009 deraadt

abort suspends on SMP systems until that gets written; ok mlarkin


# 1.148 23-Nov-2009 mlarkin

pci should return the result of it's children's suspend/resume calls back
to its parent.

handle suspend failure case and unwind if devices have an activate function
that returns failure.

ok deraadt@, kettenis@


# 1.147 23-Nov-2009 mlarkin

acpi* devices need to call their children on suspend/resume.
ok deraadt@


# 1.146 23-Nov-2009 pirofti

Remove ACPI_SLEEP_ENABLED checks.

This enables by default the suspend/resume paths in the kernel.

Okay deraadt@.


# 1.145 23-Nov-2009 deraadt

knf and spacing that are super annoying


# 1.144 23-Nov-2009 mlarkin

Hook up Theo's new children suspend/resume function, as well as more ACPI
suspend/resume glue.

ok deraadt@


# 1.143 26-Oct-2009 deraadt

Do not do strncmp comparisons on dv_xname because longer device names which
look similar could arrive in the future. Instead, compare directly against
dv_cfdata->cf_driver->cd_name
Issue originally spotted by miod


# 1.142 26-Oct-2009 jordan

Verify checksum+address when loading ACPI tables.
Some systems had invalid entries in RSDT/XSDT.
ok marco@


# 1.141 23-Jul-2009 cnst

New aibs(4) driver for ASUSTeK AI Booster (ACPI ATK0110) hardware monitoring,
with sensor state support through limits provided by the ACPI.

Tested on several ASUS motherboards kindly networked by Sam Fourman Jr.

ok deraadt marco jordan


Revision tags: OPENBSD_4_6_BASE
# 1.140 03-Jun-2009 pirofti

Import acpi power resource driver. This is needed for suspend/resume logic.

Okay jordan@ and marco@.


# 1.139 03-Jun-2009 pirofti

Add acpivideo support. This adds brightness support for all laptops
except thinkpads, they will use the acpithinkpad driver. The driver is
also hooked into wsconsole. So brightness can be adjusted via:

$ wsconsctl display.brightness=<percentage>

This is very helpfull on some laptops that have a nasty bios and get two
steps instead of one when pressing the brightness button.

Tested on various dell, fujitsu, acer, samsung and other laptops.

Okay marco@, miod@. Suggestions from kettenis@.
Lots of reviews and help from miod@, thanks!


# 1.138 03-Jun-2009 jordan

Added ejectable bay for IDE cdroms
ok marco@


# 1.137 30-Apr-2009 marco

Oga changed apm ioctls and forgot to grep. Temporary work around by hitting
the sleep button when we get any request. This interface will chnage at
some point.
ok oga


# 1.136 19-Apr-2009 krw

Call acpi_sleep_walk() when powering down just like when entering
any other sleep state. This tells acpi wake devices to not wake up
the box until manual powering up resets their status. Makes my Dell
GX520 stay powered off in response to 'halt -p'.

In snaps for a while.

ok marco@ (several times) deraadt@


# 1.135 17-Apr-2009 pirofti

Add device ranking support for future sleep/wake developments.

okay marco@


# 1.134 11-Mar-2009 jordan

Cleanup.. use aml_evalinteger instead of aml_evalname
ok marco@


# 1.133 10-Mar-2009 jordan

Fix cut/paste error.. call _BFS not _PTS
ok marco@


Revision tags: OPENBSD_4_5_BASE
# 1.132 21-Feb-2009 marco

overzealous cut/paste; found by Vladimir Kirillov <proger@uaoug.org.ua>


# 1.131 20-Feb-2009 marco

Oops didn't mean for Debugger() to remain there.


# 1.130 19-Feb-2009 marco

suspend/resume bits so that we can develop this in tree. This is disabled.
code from mlarkin and me
help from art,toby,jordan and several others
ok jordan, go for it deraadt


# 1.129 10-Feb-2009 jordan

Fix null pointer for shutdown if acpi not enabled
ok marco@


# 1.128 04-Feb-2009 kettenis

Some systems that claim to be ACPI 2.0 apparently don't provide an XSDT
address. Make the code fall back on the RSDT on those systems.

From Joerg Niendorf <p3y80@internode.on.net>. Fixes PR 5986.


# 1.127 17-Jan-2009 krw

A couple of stray malloc/memset -> malloc(M_ZERO), a bit of knf and
lose a couple of unnecessary casts. No functional change.

Feedback from kettenis@

ok kettenis@ marco@


# 1.126 06-Nov-2008 marco

Collapse all sanity checks of hids into one function.

Originaly from jordan


Revision tags: OPENBSD_4_4_BASE
# 1.125 02-Jul-2008 fgsch

add acpivideo. for now almost an empty skel so others can work on
suspend/resume. eventually it will also manage output switching and
brightness where supported. prodded by marco@


# 1.124 11-Jun-2008 marco

Fix $OpenBSD$ for once and for all


# 1.123 08-Jun-2008 deraadt

aaa_dev was being set from an uninitialized variable; ok toby


# 1.122 07-Jun-2008 marco

Kill ACPIVERBOSE. It was never used and is worthless to boot.

comments jmc
ok toby


# 1.121 06-Jun-2008 marco

Shrink acpi some more for floppies and while here re-enable acpi on the
floppies. Fixes crazy sony vaio kettenis has.

tested by deraadt
ok deraadt jordan kettenis


# 1.120 01-Jun-2008 marco

Fix \n nit in SMALL_KERNEL.


# 1.119 01-Jun-2008 marco

Re-enable new parser. Lots of testing by various folks. Fits all media
now too.

ok kettenis deraadt


# 1.118 16-May-2008 dlg

back out jordans new parser.

it didnt have enough oks from the right people, it did break machines
that were previously working, and it bloated the RAMDISKS and install
media beyond the constraints of the media.

requested by deraadt@


# 1.117 14-May-2008 jordan

Adding new ACPI Parser code
Old guts of code still exists, needs to be torn out next
ok marco@
ok brad@


# 1.116 27-Apr-2008 jcs

add acpithinkpad(4), an acpi support driver for ibm/lenovo
thinkpads.

provides functionality that the bios did on older, non-acpi
thinkpads like respond to screen brightness keys and toggle the
bluetooth device on and off.

ok deraadt@ and marco@


# 1.115 24-Apr-2008 jsing

Introduce acpiasus(4), a driver for the ACPI based hotkeys found in many
ASUS laptops (including the ASUS EeePC) - largely based on NetBSD's
asus(4) driver. On the ASUS EeePC this allows us to enable/disable
wireless, change screen brightness and use the volume keys.

ok jsg@, weingart@


# 1.114 12-Apr-2008 kettenis

Get rid of acpi_s5 global variables; simply send SIGUSR2 instead of SUGUSR1
to tell init(8) to power down the machine.

ok krw@


Revision tags: OPENBSD_4_3_BASE
# 1.113 21-Jan-2008 jsg

Check against RESET_REG_SUP in FADT before doing the acpi reset dance
after all, not doing so appears to break more machines than it fixes.

ok marco@


# 1.112 05-Jan-2008 kettenis

Never attach more acpicpu(4) devices than cpu(4) devices. Always attach
the first acpicpu(4) device on non-MULTIPROCESSOR kernels, regardless of its
ACPI processor ID.

ok mikeb@


# 1.111 05-Dec-2007 deraadt

Hang acpi(4) below bios(4) instead of mainbus(4). This lets us move acpi(4)
before pcibios(4), and then.... if acpi(4) attaches, skip pcibios(4) since
it messes the machines up. Fixes claudio's HP dl320 G5, and almost assuredly
others.
ok kettenis


# 1.110 05-Dec-2007 deraadt

repair acpiopen/acpiclose and friends to handle the /dev/apmctl interface,
so that apmd(8) can actually work correctly against acpi(4), and thus,
even do the fancy -C/-A stuff; also tested by jmc


# 1.109 02-Dec-2007 fgsch

fix the trailing space for real now.


# 1.108 02-Dec-2007 fgsch

avoid trailing spaces on output; marco@ ok.


# 1.107 27-Nov-2007 kettenis

Fix off-by-one that made us attach acpicpu0 and acpicpu1 on GENERIC kernels.

ok marco@


# 1.106 25-Nov-2007 jsg

When available and ACPI is enabled use the ACPI reset method
to reboot. This is required for some quirky sis machines
and likely others to reboot.

Tested by many. 'commit early commit often' deraadt@


# 1.105 19-Nov-2007 deraadt

sigh, amd64 still uses that stupid acpi_enabled variable...


# 1.104 19-Nov-2007 deraadt

softc's come initialized to 0, you know...


# 1.103 19-Nov-2007 deraadt

store revision; from jsg


# 1.102 19-Nov-2007 kettenis

Initialize sc_facs earlier.

ok deraadt@, beck@, canacar@


# 1.101 16-Nov-2007 deraadt

fix the bus_space #define nightmare, so that amd64 and i386 are much more
uniform. as a result shared code like acpi needs less #ifdef's
ok marco kettenis


# 1.100 15-Nov-2007 deraadt

remove the #ifdef ACPI_ENABLE. the EC codepath is either safe enough (and
will be improved), or it is unsafe because AML assumes it must be there
ok almost everyone


# 1.99 15-Nov-2007 mikeb

Make acpicpu(4) attach to the existing CPUs only.

Problem reported by form@. Fix idea by kettenis@.
Lots of help from deraadt@. Tested by deraadt@, form@ and me.

ok deraadt marco


# 1.98 14-Nov-2007 deraadt

spacing


# 1.97 12-Nov-2007 deraadt

correct : vs , in attach-time messages


# 1.96 06-Nov-2007 deraadt

all ACPI_ENABLE kernels need acpi_interrupt()


# 1.95 06-Nov-2007 deraadt

move acpi_read_pmreg() and acpi_write_pmreg() out of SMALL_KERNEL so
that acpi ramdisks can compile (obvious solution)


# 1.94 05-Nov-2007 canacar

Establish the acpi interrupt earlier. Fixes problems with some
GENERIC.MP kernels not getting acpi interrupts. This is a
workaround until the real issue with late establishment is fixed.
ok weingart@, beck@, gwk@


# 1.93 03-Nov-2007 jordan

Added support for displaying wakeup devices
ok beck@,weingart@,gwk@


# 1.92 11-Oct-2007 kettenis

When walking the tree yo find _PRT methods, check whether the device is
actually enabled and functioning. Similar to what we do for _INI.

ok weingart@, beck@


# 1.91 08-Oct-2007 krw

More simple memset(,0,) -> M_ZERO changes. In this batch move to
size(*p) as the first malloc() parameter where p is declared locally
and thus easy to check. Add M_ZERO to gpe_table allocation in acpi.c
even though there is no obvious bzero or memset nearby.


# 1.90 13-Sep-2007 mk

Remove unnecessary memset() (see aml_evalnode(9)) and a bit of KNF.

ok toby


# 1.89 13-Sep-2007 weingart

Implement ACPI 6.5.1 spec tree walk for _STA and _INI.
Fixes mk's laptop. No regressions so far (thank you to the
testers).

ok gwk@, mk@, marco@


Revision tags: OPENBSD_4_2_BASE
# 1.88 17-Apr-2007 mk

Now that acpidock no longer hangs machines during boot (at least
according to the reports I've been getting) it should be safe to enable
it again, so remove the #if 0 stuff around it.

ok marco


# 1.87 11-Apr-2007 jordan

Added changes for C-State
ok marco@


# 1.86 26-Mar-2007 marco

Calculate minutes left more accurately.

From giovanni <qgiovanni@gmail.com>


Revision tags: OPENBSD_4_1_BASE
# 1.85 03-Mar-2007 mk

Stop looking for acpidock until we fix that stuff that breaks all pre-60
series ThinkPads with acpidock(4) enabled.


# 1.84 22-Feb-2007 marco

We were getting no interrupts in .MP. There are 2 fixes; one in
acpi_machdep.c and move the interrupt establishment down after ioapic
has been initialized.

ok jordan


# 1.83 21-Feb-2007 kettenis

Make an acpi-enabled GENERIC.MP work on systems that only support PIC mode.

ok marco@


# 1.82 19-Feb-2007 jordan

Added acpi_map_address function for mapping GAS/IO registers
ok marco@


# 1.81 18-Feb-2007 jordan

Added dynamic perfect hash for opcodes
ok marco@


# 1.80 18-Feb-2007 marco

Shave off another K pre compression.
Shuffle SMALL_KERNEL functions around so that we only have a single #ifndef


# 1.79 17-Feb-2007 marco

Add SMALL_KERNEL throught acpi to make it fit on boot media.
ok deraadt


# 1.78 13-Feb-2007 marco

Add smarter _INI code. This helps with some dissapearing devices.

ok kettenis jordan


# 1.77 31-Jan-2007 gwk

Introduce acpi_hasprocfvs thats "ACPI Has Processor Frequency and Voltage
Scalling" a global flag set by acicpu attach to indicate that ACPI is
handling fvs through the _PSS objects. This will fix disappearing
EST/powernow when booting kernels with acpi enabled, as acpicpu isnt
compiled into GENERIC yet. Also in cases where acpicpu is built in and
legacy EST/powernow work but acpi is missing the _PSS object, the legacy
routines will be used.

tested by dim@, and Nick Nauwelaerts <nick AT nauwelaerts.net>
ok marco@, dim@


# 1.76 22-Jan-2007 mk

Actually look for docking stations by looking for _DCK nodes.

ok marco


# 1.75 02-Jan-2007 marco

On some systems EC must be attached as the first device. Move EC from
generic _HID devices to its own routine to force first attach. Bug found
and debugged by joris@

Code mostly written by joris@


# 1.74 26-Dec-2006 marco

Add polling to devices that require it such as acpibat and acpitz.
Use the same tsleep/wakeup mechanism as the interrupt code to keep it non-
concurrent.


# 1.73 21-Dec-2006 deraadt

even more knf love


# 1.72 21-Dec-2006 marco

Remove some leftover acpiec gpe/interrupt code.


# 1.71 20-Dec-2006 deraadt

oops, name already used


# 1.70 20-Dec-2006 deraadt

rename __acpi_enable_gpe(); ok marco


# 1.69 20-Dec-2006 deraadt

acpi_unmap_pmregs() is not used, ok marco


# 1.68 19-Dec-2006 deraadt

simplify some low-level methods substantially (shrinking code); ok marco jordan


# 1.67 06-Dec-2006 mk

malloc(..., M_NOWAIT) with no NULL check is bad. If allocation fails,
complain, clean up, and bail out of attach.

input and ok gwk


# 1.66 28-Nov-2006 jordan

Fixed clearing arg for acpi_init_pic
ok kettenis@


# 1.65 27-Nov-2006 kettenis

Don't spam dmesg.


# 1.64 27-Nov-2006 jordan

Fixes for acpiprt routing
Post-parse fixup for namerefs
ok kettenis@


# 1.63 25-Nov-2006 marco

Don't handle acpiev events inside the gpe handler since this causes deadlocks.

ok jordan@


# 1.62 15-Nov-2006 kettenis

Hook up acpi apic interrupt routing support to generic acpi code.

ok jordan@


# 1.61 02-Nov-2006 marco

Kill acpiec attach function and add its functionality to the generic _HID
device attach

prodded and ok jordan


# 1.60 02-Nov-2006 marco

Fix comments to reflect reality.


# 1.59 19-Oct-2006 marco

Replace ACPI ioctls with APM ioctls. This makes ACPI talk the same protocol
as APM therefore userspace can now retrieve power information in the same
fashion as all other apm arches (zaurus, macppc, i386 etc).

To use force the use of acpi device like so: apmd -f /dev/acpi

ok jordan@


# 1.58 19-Oct-2006 jordan

Fixed GPE debug levels
Fixed ref counter on temporary bufferfields
DSDT parsing fully working on IBM Thinkpad
ok marco@


# 1.57 15-Oct-2006 jordan

Removed old GPE handler code remnants
ok marco@


# 1.56 12-Oct-2006 jordan

Massive ACPI changes
Fixed ACPI leaks
new ACPI API
ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.55 30-Jun-2006 gwk

KNF, No functional change,
"go at it" marco@


# 1.54 30-Jun-2006 jordan

Support for calling Embedded Controller via new GPE interface
ok marco@


# 1.53 30-Jun-2006 jordan

Added new code for GPE handling
Also works with ACPIEC; need to fixup acpiec.c to remove original interrupt handler
ok marco@


# 1.52 30-Jun-2006 jordan

Added offset argument for acpi read/write pmreg routines (new GPE code)
ok marco@


# 1.51 31-May-2006 todd

do not call acpi_powerdown() unless acpi has successfully initialized
ok gwk@ canacar@


# 1.50 29-May-2006 canacar

Add embedded controller support.
ok marco@ deraadt@ testing & ok gwk@


# 1.49 19-May-2006 canacar

Minimal thermal zone driver. Monitors thermal zone temperature,
shuts down the system if the 'critical temperature' is reached.
ok marco@


# 1.48 11-Apr-2006 marco

KNF.


# 1.47 11-Apr-2006 gwk

Add acpi_resume(struct acpi_softc *sc), which calls some AML methods,
detected at attach time, currently unused.

ok marco@


# 1.46 11-Apr-2006 gwk

Discover some ACPI sleep related methods, and call them at the
appropriate time in acpi_enter_sleep_state().

ok marco@


Revision tags: OPENBSD_3_9_BASE
# 1.45 26-Feb-2006 marco

Add acpicpu as a device.

ok jordan@


# 1.44 23-Feb-2006 marco

remove dead function.

ok jordan@


# 1.43 22-Feb-2006 marco

Print out all tables it found during autoconf. We'll need this data in
the future.


# 1.42 22-Feb-2006 marco

Make dmesg less verbose.

ok jordan.


# 1.41 22-Feb-2006 jordan

Added parsing of SSDT tables
ok marco@


# 1.40 22-Feb-2006 jordan

Added new aml_register to notify by device id
Moved powerdown code to button device
ok marco@


# 1.39 21-Feb-2006 marco

Readjust debug levels that crept in too low over the last few days.
KNF


# 1.38 21-Feb-2006 marco

Rig the power button so that the machine shuts down correctly.


# 1.37 20-Feb-2006 marco

Add acpibtn device. Remains disabled for now.


# 1.36 19-Feb-2006 jordan

Added code for aml_notify GPE notification callback
ok marco@


# 1.35 19-Feb-2006 grange

Fix acpi_enter_sleep_state() by replacing current ugly hack
with mostly correct sleeping state transition code.

ok brad@ marco@


# 1.34 19-Feb-2006 marco

jordan:
Enable interrupts.
Added peripheral code to make this work.

marco:
Replace softintr and other types of interrupts with a handler with context.
Fixed interrupt magic for GPEs.
Some KNF

This is work done by jordan and I but has to be a single commit.


# 1.33 17-Feb-2006 marco

Remove earlier introduced delays. We really only need them during autconf.

ok jordan@


# 1.32 17-Feb-2006 marco

Add a generic delay function as per the ACPI spec.

from jordan@


# 1.31 17-Feb-2006 marco

Use tsleep instead of delay when !cold.


# 1.30 17-Feb-2006 marco

Argh, we need delays between reads/writes. The backend reads (i2c most likely)
are so slow that values come back as 0 sometimes. We have to do this
despite the spec saying we shouldnt have to. Guess having a faster
parser than others can hurt sometimes.

Idea from jordan.


# 1.29 17-Feb-2006 jordan

Fixed code for handling _INI
Rearranged debug statements
ok marco@


# 1.28 16-Feb-2006 jordan

Added stub for calling _INI
Fixed aml_dowhile for NULL return
ok marco@


# 1.27 16-Feb-2006 jordan

Fixed parser/evaluator to retrieve battery info+state
Cleanup of parser
Dell laptops return battery status
ok marco@


# 1.26 06-Feb-2006 brad

fix a typo so this will compile on amd64.


# 1.25 04-Feb-2006 marco

When ACPI_ENABLED wasn't set but the devices were it had an unknown variable.

Pointed out by ian@.


# 1.24 03-Feb-2006 jordan

Rewrote aml parser yet again...
Fixed bus_space_map for i386/amd64
Added _BST and _BIF to acpibat to get status/info
ok marco@


# 1.23 20-Jan-2006 jordan

More evaluator changes
Removed old evaluator code
Fixed _HID value
ok marco@


# 1.22 19-Jan-2006 jordan

Added support for read/write of PCI Config space
Help jason@
ok marco@


# 1.21 18-Jan-2006 jordan

Added new evaluation routines
Added helper methods for buffer/field evaluation
Fixed broken indentation on dsdt.c

ok marco@


# 1.20 17-Jan-2006 jordan

Major overhaul of the aml parser. Evaluater temporarily disabled.

ok marco@


# 1.19 06-Jan-2006 grange

Check for ACPI control availability only if ACPI_ENABLE defined.


# 1.18 06-Jan-2006 grange

Informative error messages on attach, prettier dmesg.


# 1.17 06-Jan-2006 grange

printf -> dnprintf conversion for debug messages.


# 1.16 05-Jan-2006 grange

Find SLP_TYPx values and store them for future use in
acpi_enter_sleep_state().


# 1.15 05-Jan-2006 grange

Random KNF and cleanup


# 1.14 05-Jan-2006 grange

Provide symbolic names for the acpi sleeping states and use it.


# 1.13 28-Dec-2005 marco

Moved DEVNAME to a acpidev.h

ok jordan@


# 1.12 18-Dec-2005 sturm

add back comment about wrong code

ok marco


# 1.11 16-Dec-2005 marco

Remove test for kbd_reset. Prompted deraadt@, ok jordan@


# 1.10 16-Dec-2005 jordan

Added hooks for powerdown/power button press


# 1.9 16-Dec-2005 jordan

Cleaned up common power management register i/o
Added enter sleep state ioctl
Hooked power button interrupt
ok marco@


# 1.8 16-Dec-2005 marco

Remove autoconf chatter


# 1.7 16-Dec-2005 jordan

Moved DSDT AML parsing into parent ACPI driver
ok marco@


# 1.6 14-Dec-2005 marco

Add AC device glue.


# 1.5 14-Dec-2005 marco

Adjust debug printing levels to something more sane.


# 1.4 13-Dec-2005 marco

Add glue to attach devices to apci.


# 1.3 07-Dec-2005 marco

KNF


Revision tags: OPENBSD_3_8_BASE
# 1.2 10-Jul-2005 grange

Spacing cleanup.


# 1.1 02-Jun-2005 tholo

Start on a basic ACPI framework -- does not do much more than read out the
ACPI tables into kernel memory and attach ACPI and HPET timers currently.

In order to test this code, enabling the devices in GENERIC as well as
the ACPI_ENABLE option is needed. This code does not do any thermal
control yet, so this should be done with care depending on the platform.

In the tree so more people can contribute to making this more fully
featured.

Ok niklas@ grange@ tedu@


Revision tags: OPENBSD_6_5_BASE
# 1.363 04-Apr-2019 kettenis

Avoid potential null-pointer dereference. Found by Kent R. Spillner.

ok deraadt@, jsg@


# 1.362 20-Jan-2019 jsg

fix SMALL_KERNEL build after changes in rev 1.361
reported by naddy@ ok deraadt@


# 1.361 19-Jan-2019 tedu

Add a pwraction sysctl that controls what the power button does on acpi.
By default, nothing changes -- shutdown is initiated. But allows turning
power button into a sleep button if desired.
(grudging) ok from a few parties


# 1.360 26-Oct-2018 kettenis

Add acpipci(4) on amd64. For now this only calls the PCI-specific _OSC
method to let the ACPI implementation know what features we support.


Revision tags: OPENBSD_6_4_BASE
# 1.359 30-Aug-2018 kettenis

Move softbutton check before the check that skips attaching certain acpi
devices. Fixes regression caused by matching on _CID in addition to
matching on _HID.

ok matthieu@, mlarkin@


# 1.358 25-Aug-2018 kettenis

Match ACPI devices based on _CID if no match for _HID is found.

ok mpi@


# 1.357 19-Aug-2018 kettenis

Add support for multiple PCI segments. Only really implemented for arm64
for now as amd64/i386 firmware still caters for legacy OSes that only
support a single PCI segment.

ok patrick@


# 1.356 03-Aug-2018 kettenis

Let ahci(4) match on _CLS instead of _HID when attaching at acpi(4). Avoids
having to add many more _HID entries to the match table.

ok deraadt@, mlarkin@


# 1.355 10-Jul-2018 kettenis

Make legacy interrupts work in acpipci(4).

ok patrick@


# 1.354 05-Jul-2018 kettenis

Add acpipci(4), a driver that supports generic ECAM-compatible PCI host
bridges based on information provided by ACPI.

ok mlarkin@


# 1.353 04-Jul-2018 kettenis

Properly pass around the PCI "chipset tag" in acpi(4) and refactor
acpimcfg(4) to call an MD initialization functions that sets up a tag for
PCI ECAM.

ok guenther@, mlarkin@, krw@


# 1.352 01-Jul-2018 kettenis

Move acpi(4) attach glue into acpi_machdep.c.

ok guenther@, deraadt@


# 1.351 01-Jul-2018 kettenis

Add support for _DSD Device Properties.

ok mlarkin@


# 1.350 30-Jun-2018 kettenis

Add intr_enable() function, intended for MI use to amd64 and i386 and use
this in the acpi(4) suspend/resume code paths.

ok deraadt@


# 1.349 29-Jun-2018 kettenis

Use <stdint.h> types.

ok mpi@, jung@, krw@, deraadt@


# 1.348 29-Jun-2018 mlarkin

ACPI: Allow (with warning) GPE handler reassignment, instead of returning
a failure code.

ok kettenis


# 1.347 26-Jun-2018 mlarkin

knf


# 1.346 26-Jun-2018 kettenis

Let acpi(4) pass down the bus dma tag.

ok deraadt@


# 1.345 25-Jun-2018 kettenis

Reorganize acpi(4) code a bit in preparation for upcoming arm64 support.

ok deraadt@


# 1.344 20-May-2018 kettenis

Wake up the acpi thread after scheduling a gpio event.

ok mpi@


# 1.343 17-May-2018 kettenis

Sprinkle some #ifndef SMALL_KERNEL such that RAMDISK kernels cuild again.

pointed out by stsp@


# 1.342 17-May-2018 kettenis

Implement GenericSerialBus OpRegion support.

ok mlarkin@


# 1.341 27-Mar-2018 kettenis

Add acpicmos(4), a driver that implements SystemCMOS OperationRegion
access support. This fixes machines where the AML doesn't check whether
support for this OperationRegion type has been registered by the OS.

ok mlarkin@


Revision tags: OPENBSD_6_3_BASE
# 1.340 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.339 10-Feb-2018 deraadt

Syncronize filesystems to disk when suspending. Each mountpoint's vnodes
are pushed to disk. Dangling vnodes (unlinked files still in use) and
vnodes undergoing change by long-running syscalls are identified -- and
such filesystems are marked dirty on-disk while we are suspended (in case
power is lost, a fsck will be required). Filesystems without dangling or
busy vnodes are marked clean, resulting in faster boots following
"battery died" circumstances.
Tested by numerous developers, thanks for the feedback.


# 1.338 08-Feb-2018 deraadt

After flushing disks, try again to release all memory. We don't need to
save dirty memory to the hibernate space.


# 1.337 08-Feb-2018 deraadt

Toss all releaseable memory, because fragmentation can get in the way
of allocating the hibernate playpen.


# 1.336 08-Feb-2018 deraadt

sr_quiesce() is a new approach for ensuring that softraid drains
output to the disks.

This is part of a larger suspend/resume filesystem-safety diff, which
has been worked on for a couple of months already. Tests by job, krw,
beck, benno, and others. Sometimes even by snapshot users...


# 1.335 29-Nov-2017 kettenis

Add support for GPIO-signaled events.

ok mlarkin@


# 1.334 16-Nov-2017 jcs

Add PCI attachment for dwiic(4) needed by Intel 100 Series machines

ok kettenis


# 1.333 14-Nov-2017 kettenis

Add a few more PNP IDs to the skip lists.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.332 17-Aug-2017 stsp

Make dwiic(4) attach its ACPI parent dependencies.
This change makes the Asus E200HA keyboard work.

Original analysis and patch by Cesare Gargano
ok kettenis@


# 1.331 09-Aug-2017 dcoppa

Add "PNP0303" (8042 PS/2 Controller) to acpi_isa_hids[]

ok kettenis@


# 1.330 22-Jul-2017 jcs

acpibat: look for _BIX first before falling back on _BIF

ACPI 4.0 deprecated _BIF for battery status so some newer machines
are exporting _BIX which extends _BIF's package with a few extra
fields. When using _BIX, export the new discharge cycle count as a
sensor.

feedback from kettenis and anton
ok deraadt


# 1.329 20-Jul-2017 kettenis

Start a list of isa(4) device names such that we can avoid printing them
as "not configured".

ok mikeb@, millert@, deraadt@


# 1.328 08-Apr-2017 deraadt

Sensors are run as callbacks inside tasks. During suspend a sensor
could be running inside a driver that will be force-detached, or due
to tsleep end up disrupting the softstate/hardstate contract. At
suspend time, quisce all these callbacks by waiting for completion.
This issue has never been observed for real, but may be implicated in
suspend/resume failures.
ok kettenis guenther mlarkin


# 1.327 08-Apr-2017 deraadt

Two prototypes for acpi_maptable; merge to one place


Revision tags: OPENBSD_6_1_BASE
# 1.326 27-Mar-2017 deraadt

Now that hibernate_alloc() only has clean success/failure, don't
need to call hibernate_free() to clean up a partial mess.
ok mlarkin kettenis


# 1.325 13-Mar-2017 deraadt

allow ramdisks to build; ok jcs


# 1.324 12-Mar-2017 jcs

try to attach acpisbs first and if successful, don't attach acpibat


# 1.323 02-Mar-2017 natano

Add a new sysctl machdep.lidaction. The sysctl works as follows:

machdep.lidaction=0 # do nothing
machdep.lidaction=1 # suspend
machdep.lidaction=2 # hibernate

lidsuspend is just an alias for lidaction, so if you change one, the
other one will have the same value. The plan is to remove
machdep.lidsuspend eventually when people have upgraded their
/ets/sysctl.conf.

discussed with deraadt, who came up with the new MIB name
no objections mlarkin
ok stsp halex jcs


# 1.322 28-Feb-2017 natano

Untangle abstract sleep modes from ACPI sleep states. This paves the way
for further work in the area.

idea and ok deraadt
ok mlarkin


# 1.321 22-Feb-2017 jcs

add acpisbs, an acpi smart battery subsystem driver reading data
over smbus

currently disabled because it conflicts with acpibat

ok deraadt, kettenis


# 1.320 14-Jan-2017 kettenis

Revert aml_rdpciaddr changes; breaks several machines that were working
before.


# 1.319 11-Jan-2017 jsg

Increase the size of the buffers used for _HID and _CID strings to be
able to fit "Hyper_V_Gen_Counter_V1".

ok mlarkin@ deraadt@


# 1.318 08-Jan-2017 kettenis

Make aml_rdpciaddr() work for for devices behind bridges. The code now
uses the mapping between AML nodes and PCI devices that we establish.
Because _INIT methods may end up calling aml_rdpciaddr(), make sure we
create that mapping early. Also handle devices that aren't actually present.
These devices are now included in the mapping and reads will return an
all-ones patterm whereas writes are a no-op..

ok mlarkin@


# 1.317 25-Oct-2016 pirofti

Add the resource index to aml_parse_resource() callback function.

This is needed by an upcoming acpiec commit that handles machines breaking
the current ACPI specifications.

Change suggested by and ok kettenis@, guenther@


# 1.316 18-Sep-2016 guenther

If the APIC table (MADT) isn't present then ci_acpi_proc_id won't be set
when acpicpu tries to attach, leading to a NULL dereference. The ACPI
processor object on the Dells where this was reported is bogus and useless
anyway so apply the same test when deciding whether to attach to processor
objects as acpicpu would when matching them to cpus.

Problem reported by Eivind Eide (xenofil (at) gmail.com) and
Johan Huldtgren (johan+openbsd-bugs (at) huldtgren.com).
Testing by Eivind Eide.
ok kettenis@ mlarkin@


# 1.315 03-Sep-2016 naddy

Write the system time back to the RTC every 30 minutes.
This fixes the problem that long-running machines which were not
shut down properly would reboot with a badly offset system time.

hints and ok kettenis@


# 1.314 31-Aug-2016 mlarkin

Don't put a device in the wake device list if _STA says it's not there.

Fixes at least one machine that woke up from sleep / poweroff immediately.

ok kettenis@, tested by kettenis and martijn@


# 1.313 28-Jul-2016 kettenis

Store the acpi processor ID/UID in struct cpu_info, and use it to attach
acpicpu(4) drivers to the right cpu(4).

ok mlarkin@, guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.312 10-Jun-2016 kettenis

The power management timer has been made optional in ACPI 5.0A, so don't
attempt to attach acpitimer(4) if the timer isn't present.

Pointed out by jcs@

ok jcs@


# 1.311 08-May-2016 kettenis

Make sure devices listed by a device's _DEP method are attached before
we attach the device itself. The _DEP methode, introduced in ACPI 5.0,
is a hint that the device in question depends on OpRegion support from
the devices listed by _DEP, so we have to order them the proper way.
To prevent us from attaching those devices again when we encounter them
later walking down the device tree, keep track of our attempts to
attach a device driver to them using aflag in the aml_node structure.

ok guenther@


# 1.310 25-Apr-2016 pirofti

We attach acpihpet(4) and acpiec(4) without using the HID, so do not
report it in the dmesg.

OK deraadt@, mlarkin@.


# 1.309 02-Apr-2016 kettenis

Reduce dmesg spam by knocking out some acpi hardware IDs that don't need a
driver.

ok mpi@


# 1.308 29-Mar-2016 kettenis

Check if a device is present (using _STA) in acpi_foundhid. This prevents
us from reporting devices as "not configured" that aren't actually present,
and allows us to remove duplicated code from several drivers.

ok deraadt@, ok jsg@


# 1.307 29-Mar-2016 kettenis

Implement a cleaner way to prevent acpivout(4) from attaching if we have
a platform-specific method of controlling the backlight.

ok millert@, mlarkin@, jung@


# 1.306 28-Mar-2016 kettenis

Adding each and every new acpi device driver to acpi_foundhid() is
gettinga bit of the burden. And it really isn't how our autoconf
framework is supposed to work for busses that can be enumerated. All
the drivers already check for a matching _HID in their attach
function. So we can just drop the checks in acpi_foundhid() and let
the drivers sort themselves out.

Print a "not configured" line for devices for which we don't attach adriver.
This may be a bit much as there are devices for which we will not have a
driver. This will be removed if it causes too much dmesg spam.

ok deraadt@, mlarkin@


Revision tags: OPENBSD_5_9_BASE
# 1.305 17-Jan-2016 jsg

Take acpi_foundhid() outside of #ifndef SMALL_KERNEL so the ramdisk
kernel can find and attach dwiic(4) and sdhc(4).


# 1.304 17-Jan-2016 jsg

remove an unused global variable


# 1.303 14-Jan-2016 kettenis

Attach dwiic(4) on the Intel Bay Trail i2c controllers as well.


# 1.302 12-Jan-2016 kettenis

Improve emulation of PM registers on Hardware-redcued ACPI by emulating
SPL_TYPx, SLP_EN and WAK_STS using the new SLEEP_STATUS_REG and
SLEEP_CONTROL_REG registers. Makes the transition into S5 (powerdown) work
on the ASUS X205TA.

ok deraadt@, mlarkin@


# 1.301 12-Jan-2016 jcs

Add dwiic, a driver for the Synopsys DesignWare i2c controller found
on the Samsung ATIV Book 9 laptop. This initial version only
supports ACPI config/attachment.

Add ihidev, a HID-over-i2c driver largely based on uhidev. dwiic
handles attaching ihidev devices found in ACPI.

Add ims, a HID-over-i2c mouse/trackpad driver to get basic cursor
and button functionality from HID-compliant i2c trackpads.

ok kettenis deraadt


# 1.300 11-Jan-2016 kettenis

Make sdhc(4) attach to hardware IDs "80860F14" and "PNP0FFF".


# 1.299 09-Jan-2016 kettenis

Initial support for Hardware-Reduced ACPI. This skips the legacy mode checks
by emulating the PM_CNT register such that SCI_EN is always enabled. It also
skip attaching acpitimer(4) since the relevant registers are no longer there.

ok deraadt@, mlarkin@


# 1.298 06-Jan-2016 kettenis

Follow the spec and check the SCI_EN bit to see if ACPI has already
been enabled and don't bother enabling it again if it is. This should fix
systems that don't implement legacy mode and therefore set ACPI_ENABLE and
ACPI_DISABLE to zero.

ok mlarkin@, yasuoka@


# 1.297 23-Nov-2015 reyk

Back out the CRITICAL change, turn it back from 10% to 25%. Some
people with old/broken batteries didn't like this change, and the
underlying heuristics are wrong.


# 1.296 22-Nov-2015 reyk

The "CRITICAL" APM battery level, that is exported/faked by the ACPI
stack, is currently set to 25%. With 25% battery left, a typical
modern laptop will have at least one hour runtime left. It is an
arbitrary value, and other APM power layers in the kernel set CRITICAL
to something like 5% or 10%, so set it to 10% for now.

OK mlarkin@


# 1.295 28-Sep-2015 deraadt

In low-level suspend routines, set cold=2. In tsleep(), use this to
spit out a ddb trace to console. This should allow us to find suspend
or resume routines which break the rules. It depends on the console
output function being non-sleeping.... but that's another codepath which
should try to be safe when cold is set.
ok kettenis


# 1.294 06-Sep-2015 deraadt

clarify malloc arguments (base + length...)


# 1.293 28-Aug-2015 deraadt

fairly simple sizes for free(); ok tedu


# 1.292 20-Aug-2015 kettenis

Respect the access size when reading or writing to pci config space.
This version also makes sure writes are properly aligned, preventing
the panics seen with the previous incarnation of this change.

Fixes battery status passthrough in vmware, brightness keys on my x220
and who knows what else.

ok mlarkin@, deraadt@


Revision tags: OPENBSD_5_8_BASE
# 1.291 04-Aug-2015 deraadt

If we are setting ACPI_PM1_BM_RLD do it only once, and remember to
repeat this action at resume time.
ok mlarkin guenther


# 1.290 29-Jul-2015 deraadt

Something subtle in previous revision is locking up suspend or booting
on some laptops. Mailing lists threads mention x220 and Asus machines.
Reverting for now.


# 1.289 21-Jul-2015 kettenis

iRespect the access size when reading or writing to pci config space.
Fixes battery status passthrough in vmware and who knows what else.

Tested by many.

ok mlarkin@, deraadt@


# 1.288 17-Jul-2015 mlarkin

fix comments


# 1.287 26-Mar-2015 jsg

Use the 0x10 extended capability to determine if the trackstick
buttons are wired to the trackpad and need to be re-routed to the
trackstick. Without this change the buttons on 2015 thinkpads
get picked up as extended buttons that show up as scroll up/down.

Remove the X1 Carbon 2015 (LEN0048) and X250 (LEN0046) from the
top button area/soft buttons quirks list. Also avoid using the
quirk list entirely if the capability bit is set.

Based on work from jcs@ with feedback from mpi@
ok mpi@ jcs@


# 1.286 20-Mar-2015 kettenis

The ACPI standard says that a PCI function number of FFFF can be used to refer
to all functions on a device. This makes no sense in the context of accessing
PCI config space. Nevertheless stsp@ has a machine with AML that tries to
do config space reads from a device where _ADR encodes function number FFFF.
This is almost certainly buggy AML, but since Linux (and presumably Windows)
boot just fine on this machine we need to work around this. Linux effectively
treats function FFFF as function 7, which isn't present on this machine.
Instead we simulate a read from a non-existent device by returning a bit
pattern of all ones. Writes to function FFFF will still panic.

ok mlarkin@


# 1.285 16-Mar-2015 deraadt

Reduce use of SMALL_KERNEL in acpi, bringing acpiec driver and more gpe
handling into RAMDISK. This is now possible because the install media
has ample room. The goal is to reduce special cases where we may be
skipping (unknown) important operations...
ok mlarkin kettenis


# 1.284 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.283 07-Feb-2015 deraadt

New framework that allows hibernate to pass in entropy from it's fresh
boot.
ok mlarkin


# 1.282 06-Feb-2015 mlarkin

Fix a hibernate crash on some machines due to unmapping a page that
may not have been mapped previously (in the failure to hibernate case).

Also ensure that the lowmem ptp is mapped in all cases (not just MP).

ok kettenis


# 1.281 17-Jan-2015 deraadt

resume_randomness() before spinning up other cpus...


# 1.280 15-Jan-2015 jsg

Define a new wscons mouse type for Synaptics clickpad devices that lack
physical buttons. This will be used if the acpi pnpid for the mouse
matches a list provided by Synaptics found in Linux.

Combined with the xenocara changes this will remove the need for an xorg
config file for the x240/t440/t540 generation of thinkpads.

ok matthieu@ shadchin@ kettenis@


# 1.279 11-Jan-2015 kettenis

Tolerate checksum errors in ACPI tables (but complain about them). Apparently
there are machines out there with broken BIOSen with such checksum errors in
thier RSDT tables. Windows runs fine on these machines, so it probably does
not check. Linux ignores checksum errors as well (and prints a warning).

ok deraadt@, krw@


# 1.278 18-Dec-2014 deraadt

suspend and resume the RNG (at what looks like the right spots)
ok reyk djm


# 1.277 09-Dec-2014 doug

Replace some malloc(n*size,...) calls with mallocarray().

ok tedu@ deraadt@


# 1.276 06-Dec-2014 mpi

If "machdep.lidsuspend" is not set, do not go back to sleep if the lid
is still closed when resuming. This allows docked laptop to be resumed
without opening them.

ok mlarkin@


# 1.275 23-Nov-2014 mlarkin

If a machine has no _LIDs, then none can be open. This diff fixes a problem
where machines without any _LIDs were going back to sleep after hibernate
due to the recent suspend-if-lid-closed-on-resume diff. Noticed on qemu,
which doesn't have any _LIDs.

discussed with deraadt


# 1.274 18-Nov-2014 krw

Nuke yet more obvious #include duplications.

ok miod@


# 1.273 08-Nov-2014 mlarkin

If resuming from sleep (zzz/ZZZ) and the lid is still closed, go back to
sleep. This prevents accidental lid flex or slight opening in a backpack
from waking the machine up and leaving it resumed (powered on).

ok deraadt@


# 1.272 26-Sep-2014 kettenis

Rework piglet and pig allocation. Currently the piglet gets allocated
deep down in the suspend path, where it is really hard to recover from
allocation failure. So allocate the piglet early on in the suspend path.
Also change the piglet and piglet allocation functions to use km_alloc(9)
instead of doing pmemrange magic. This removes a bunch of code which, in the
case of the piglet allocation, is broken since it results in a NULL pointer
dereference. Also switch the piglet allocation to not wait. If we can't
allocate 16MB of phys contig memory on a halfway modern machine we're almost
certainly under a lot of memory pressure and we're better off not trying to
hibernate anyway.

ok mlarkin@


# 1.271 25-Sep-2014 kettenis

Bring back rev 1.249. Now that mp_setperf() has been fixed, it should be
safe to restore perflevel when resuming. Should fix the issues people
see with the processor running at full speed heating up the machine while
the apm command reports that the CPUs are running at a low speed.

ok dcoppa@, deraadt@


# 1.270 23-Sep-2014 kettenis

Release the acpi lock around calls to wsdisplay_suspend() and
wsdisplay_resume(). The X server may adjust the display brightness just
before/after the VT switch when suspending/resuming. In fact, it may
already be blocked adjusting the display brightness for some reason
unrelated to the suspend request.

The VT switch request will eventually time out, but the delay is annoying
and the failed VT switch will lead to problems after resume.

Needed for a future update of the xf86-video-intel driver.

ok mlarkin@


# 1.269 19-Sep-2014 kettenis

Use config_suspend_all(9).


# 1.268 08-Sep-2014 kettenis

Clear the PME Status bit on PCI devices in response to Device Wake events.
Fixes the interrupt storm on the 2009 Mac mini with WOL enabled on its
nfe(4) interface.

ok mlarkin@


Revision tags: OPENBSD_5_6_BASE
# 1.267 20-Jul-2014 mlarkin

Support hibernating to softraid crypto volumes.

much help and ok from deraadt@


# 1.266 16-Jul-2014 mlarkin

Reenable hibernate RLE support and flush+zero all memory after unpack.


# 1.265 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.264 12-Jul-2014 kettenis

Add a function to drop all clean pages on the page daemon queues and call
it when we hibernate.

ok mlarkin@, miod@, deraadt@


# 1.263 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.262 11-Jul-2014 mlarkin

Flush the buffercache to 16MB on hibernate and restore its previous max
size (kern.bufcachepercent) on resume, for better hibernate performance.

ok beck@


# 1.261 10-Jul-2014 blambert

remove unneeded header

ok mlarkin@


# 1.260 01-Jul-2014 kettenis

Adding an additonal delay after each access to SystemMemory or SystemIO
during autoconf makes no sense.

ok deraadt@


# 1.259 23-Jun-2014 kettenis

SystemMemory is used to access memory mapped registers on some machines, so we
must use the bus_space(9) API to access it instead of memcpy(9). Also make
sure we properly align access to fields that need more than byte alignment.

ok guenther@


# 1.258 21-May-2014 mlarkin

Fix invalid / missing format specifiers in several dnprintf statements
(used only when ACPI_DEBUG is enabled)

From a diff on tech@ by Fabian Raetz, with a minor change by me, tested
on i386 and amd64 with ACPI_DEBUG enabled and disabled.


# 1.257 25-Apr-2014 mlarkin

Defer the free of the hibernate structures until later, when it is safe to
do so.

Freeing the hibernate structures too early leads to inadvertent calls into
splx when we aren't ready for them yet. Found when trapping splx calls in
the resume path.

discussed with deraadt@ at m2k14


# 1.256 13-Apr-2014 mlarkin

Fix a badly worded debug message that implied the entire machine was going
to sleep, instead of just the acpi thread going to sleep.


# 1.255 31-Mar-2014 mpi

Change the order of operations during a suspend/resume cycle and
call bufq_quiesce() after executing the DVACT_QUIESCE handlers.

This should be safe since no disk nor controller drivers have such
handler but it will allow us to detach sd(4) devices attached to a
USB bus.

Another benefit pointed out by kettenis@ is that drivers that need
to read a firmware from the disk should be able to do it at resume
time in a DVACT_WAKEUP handler.

ok kettenis@, deraadt@


# 1.254 13-Mar-2014 dlg

get rid of the assumption that the head of the alldevs list is the
"mainbus" device. this breaks when mpath is enabled because it
attaches before mainbus and therefore takes the head position.

have autoconf provide device_mainbus() which looks up mainbus_cd,
and use that instead.

discussed with deraadt who just wants mpath stuff to move forward
despite there being many ways to shine this particular turd.


Revision tags: OPENBSD_5_5_BASE
# 1.253 21-Feb-2014 deraadt

Do the lid control further on in the resume sequence, since this is
AML, and will do WAITOK operations. While cold, that is a bad idea.
We seem to be safe with respect to the spec, I do not see any event
debounce issues here.
ok kettenis, tested by many


# 1.252 20-Jan-2014 claudio

Instead of checking sc->sc_revision <= 1 check that the
fadt->hdr_revision <= 1. This is a better check since it checks
the version of the proper descriptor. This allows the alix APU
to reboot since those boards have currently a wrong sc->sc_revision.
Committing this now so that it can be tested easier.
With and OK jsg@ also fine by derradt@


# 1.251 24-Dec-2013 kettenis

If the FADT has its SMI_CMD set to zero, assume we're only ACPI-only hardware
and don't need to disable SMI ownership of the ACPI hardware registers.

ok mlarkin@


# 1.250 23-Dec-2013 kettenis

Revert previous commit. It makes resume hang on some MP systems running
GENERIC.MP.


# 1.249 16-Dec-2013 kettenis

Restore hw.setperf upon resume like we do for apm(4) on i386. Seems we need
to do this fairly late in the resume to avoid hangs.

ok deraadt@, mlarkin@


# 1.248 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


# 1.247 06-Nov-2013 mpi

Add support for Power Resources for Dx states and the necessary hook
for PCI devices. This hook should be called twice, before and after
changing the power state of a PCI device.

Before setting the device to the new state, the ACPI layer will notify
every power resources linked to the device for that state and make sure
they are turned "_ON". After changing the state of the device, it will
decrement the reference of every power resources linked to that device
for the old state and turn them "_OFF" if they are no longer referenced.

This fixes the no-USB after resume problem seen on various ThinkPad,
problem initialy diagnosed with Alexander Polakov.

ok kettenis@, deraadt@


Revision tags: OPENBSD_5_4_BASE
# 1.246 01-Jun-2013 mlarkin

Stop printing ACPI wakeup devices in dmesg after the 16th wakeup device.

Helps with VMware VMs that report hundreds of wakeup devices.

ok reyk@, deraadt@


# 1.245 31-May-2013 bluhm

Fix the build for a kernel without wd and pciide in its config.
OK deraadt@ tedu@


# 1.244 30-May-2013 deraadt

Enforce ca_activate tree-walks over the entire heirarchy for all events,
cleaning up some shutdown-hook related code on the way.
(A few drivers related to sparc are still skipped at kettenis' request)
ok kettenis mlarkin, tested by many others too


# 1.243 18-Apr-2013 deraadt

pin the acpi thread to the BSP, because AML and SMI cannot be trusted. I
was certain we were doing this already, but it turns out no...


Revision tags: OPENBSD_5_3_BASE
# 1.242 24-Dec-2012 guenther

In acpiioctl(), Return though the splx() path if suser() returns an error

ok deraadt@


# 1.241 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


# 1.240 04-Oct-2012 ehrhardt

Use information provided by ACPI to attach secondary PCI host bridges.

ok kettenis@


# 1.239 07-Sep-2012 kettenis

Add a new function called acpi_pci_min_powerstate() that returns the lowest
power state for a PCI device for the power state (currently S3 or S4) we're
transitioning into. This function is probably too simplistic; there is a lot
of (silly) complexity in the ACPI spec.

ok deraadt@


Revision tags: OPENBSD_5_2_BASE
# 1.238 13-Jul-2012 pirofti

Disable acpivideo(4) on ASUS laptops.

There are some models that panic whenever an event is received.
Some cope, some don't. In order to get this right acpiec(4) requires
major rework. Until then let machines boot.

Discussed with deraadt@.


# 1.237 11-Jul-2012 mlarkin

for now, restrict hibernate to root in the acpi ioctl kernel interface.
normal rules for controlling this interface through apmd still apply.

ok deraadt@


# 1.236 09-Jul-2012 deraadt

tiny little oops in untested hibernate block


# 1.235 09-Jul-2012 deraadt

clean up ioctl() case statements


# 1.234 09-Jul-2012 deraadt

acpi_disable_onegpe() is not used; ok pirofti


# 1.233 24-May-2012 kettenis

If _MIN > _MAX consider a WordBus resource to be invalid. Fixes a panic on
the IBM x3550-M4.

ok mikeb@


# 1.232 29-Mar-2012 mlarkin

Missing #ifdef in previous diff


# 1.231 29-Mar-2012 mlarkin

Prevent hibernation attempts on machines with unsupported disk controllers.
The same check is also performed later in the hibernate sequence, but by
then it's too late to cleanly unwind (presently).

ok deraadt


# 1.230 26-Mar-2012 deraadt

simplify conditionals in acpi_sleep_state()
ok mlarkin


# 1.229 26-Mar-2012 deraadt

hook in the hibernate request code; half of this diff is from mlarkin
ok mlarkin


Revision tags: OPENBSD_5_1_BASE
# 1.228 20-Sep-2011 deraadt

Very early in suspend while the clock is still ticking realtime,
save the clock back to the rtc. The zaurus already did this.
ok phessler miod kettenis


Revision tags: OPENBSD_5_0_BASE
# 1.227 02-Jul-2011 nicm

kqueue attach functions should return an errno or 0, not a plain 1. Fix
the obvious cases to return EINVAL and ENXIO.

ok tedu deraadt


# 1.226 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.225 16-Jun-2011 pirofti

Import acpitoshiba driver.

This is originally written by Hiroyuki Aizu, ported and integrated
in our tree by Javier Vazquez <javama4 AT gmail> with tweaks and
suggestions by me.

Import prodded by deraadt@ and the lack of testers on tech@.


# 1.224 27-Apr-2011 jcs

attach acpithinkpad to newer lenovo models like the x120e

ok deraadt@


# 1.223 22-Apr-2011 jordan

Fix uninitialzied variables and formatting strings (-Wxxx errors)


Revision tags: OPENBSD_4_9_BASE
# 1.222 02-Jan-2011 jordan

Re-add code for ACPI taskqueue, eliminates need for state vars in acpi softc
Serialize processing ACPI tasks for sleep/powerdown/GPE event
ok deraadt@


# 1.221 31-Oct-2010 guenther

Revert last commit: it breaks resume on Thinkpads
"then please back it out" deraadt@


# 1.220 26-Oct-2010 jordan

Add task queue for ACPI gpe and notify handlers
This fixes eject on Dell Latitude dock and an issue on the Dell Mini
battery update.
ok deraadt


# 1.219 07-Oct-2010 jordan

Create separate functions for enabling wake and runtime gpes.
Remove old suspend/resume gpewalk function
ok deraadt, mlarkin


# 1.218 05-Oct-2010 jordan

split acpi_enable_onegpe to separate enable/disable functions
ok deraadt


# 1.217 05-Oct-2010 jordan

Added acpi_disable_allgpes function to clear GPE status
ok deraadt


# 1.216 05-Oct-2010 deraadt

clean up acpi_softc use more


# 1.215 05-Oct-2010 deraadt

delete a bit of #if 0 code


# 1.214 05-Oct-2010 deraadt

hide use of our global acpi_softc pointer a bit better


# 1.213 29-Sep-2010 deraadt

grab the acpi thread lock very early in acpi thread startup; not that it
is likely that anyone will race us... but why be careless.
ok kettenis


# 1.212 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.211 29-Aug-2010 jasper

- add a check for FWRITE here too.

ok miod@


# 1.210 27-Aug-2010 kettenis

Make PCI Power Management optional, and only enable it when acpi(4) attaches.

ok deraadt@


Revision tags: OPENBSD_4_8_BASE
# 1.209 08-Aug-2010 kettenis

Close race between the acpi thread and wscons ioctls running in process
context. We might rip this out post-release and replace it with a solution
that serializes things in a more obvious way.

ok deraadt@, miod@ and "those crazy texans who ok anything"


# 1.208 08-Aug-2010 deraadt

Report the acpi sleep states when on a RAMDISK; ok krw


# 1.207 07-Aug-2010 kettenis

Enforce acpi register access rescrictions.

ok mlarkin@, deraadt@, tested by marco@


# 1.206 07-Aug-2010 deraadt

upon resume, notify all the acpiac and acpibat drivers to update
their status. some laptops do not do this automatically.
ok kettenis canacar


# 1.205 06-Aug-2010 marco

Always call _PSW on _LID devices that support it to enable lid open to
resume the machine. Conversely disable it when the machine wakes up.

Tested by several ok deraadt


# 1.204 06-Aug-2010 deraadt

As part of a revamp of the PM1/GPE code, I write this diff to look for
unmanaged EN & STS bits in the PM1 register at interrupt time and report
them. As a side effect this splits the STS acknowledgement into two writes
(for power, and sleep) instead of one. The printf that is added (to spot
unmanaged STS bits) has not yet been triggered as far as we know.

Before the "write to PM1 registers at the right offset" diff went in, this
was not neccessary. But newer thinkpads do not have a working soft power
button without this diff.

We have no idea why.
ok mlarkin kettenis


# 1.203 05-Aug-2010 deraadt

PM1 power button events were generating 'fake events' to acpibtn PWRB,
but if there is no such device these were being tossed. Instead, arrange
for all power button handlers to flag a softc variable which is processed
by the thread; so that one place will know to request a powerdown (psignal
to init)
ok marco mlarkin


# 1.202 05-Aug-2010 deraadt

rename a variable which is annoyingly poorly named


# 1.201 05-Aug-2010 mlarkin

Write to the correct offset for various ACPI pmreg registers. Writing to
the correct register does properly clear the wake status bits and fixed
event bits, which allows some machines to have more than 1 suspend cycle.

bug spotted by deraadt@

tested by deraadt@ and myself on a half-dozen machines and others on
hackers@

ok deraadt@, marco@


# 1.200 05-Aug-2010 deraadt

Fix the power button. The pm1 register is 16 bits in size.
My mistake, a while back.
tested by krw


# 1.199 04-Aug-2010 kettenis

Print supported sleep states.

ok marco@, phessler@, deraadt@


# 1.198 03-Aug-2010 mlarkin

Use the proper flag for re-enabling certain hardware events (power btn,
sleep btn). The (incorrect) flag prevoiously used coincidentally had the
same value, so this shouldn't cause any different behavior than before.

tested on a variety of machines (i386, amd64, sp, mp) - no behavioral
change seen

ok deraadt@


# 1.197 28-Jul-2010 marco

Make all gasio failures look and behave the same.


# 1.196 28-Jul-2010 mlarkin

Delay a little longer on suspend before giving up. Fixes machines that
sometimes don't suspend because the sleep transition takes too long
(symptoms like failing to sleep but the backlight stays on).

ok deraadt@


# 1.195 27-Jul-2010 deraadt

remove XXX; EC can now be attached early when needed; ok jordan


# 1.194 27-Jul-2010 deraadt

more brackets feels good


# 1.193 27-Jul-2010 deraadt

XXX annotate a block in acpi_enter_sleep_state() that is pretty worrying


# 1.192 27-Jul-2010 deraadt

horrific indentation that nearly made me puke


# 1.191 27-Jul-2010 mlarkin

Set the sleeping indicator light on machines that support it, in case SMI
doesn't do it for us. Shows the 'moon' LED on older Thinkpads when
sleeping.

Tested on T43p, W500, T510 by me and on X61s by deraadt@.

ok deraadt@


# 1.190 27-Jul-2010 jordan

Early initialization of acpiec if ECDT table exists
Fixes hang when booting thinkpads while docked
ok deraadt


# 1.189 26-Jul-2010 deraadt

remove merge error which was commited


# 1.188 26-Jul-2010 pirofti

Add support for Sony ACPI hotkeys via a new driver: acpisony(4).

Currently it only works for the suspend button and tries to do right for
the brightness events, but I haven't found a Sony laptop that like to do
right so far.

In the future I want to make the brightness keys work on all Sony's and
also add support for the zoom hotkeys and whatever other funky keys I can
find on those things.

Okay deraadt@.


# 1.187 22-Jul-2010 deraadt

pretty large cleanup of SMALL_KERNEL


# 1.186 21-Jul-2010 deraadt

in the acpi GPE handler, late ack level interrupts not edge interrupts.
Duh.


# 1.185 21-Jul-2010 deraadt

Merge the edge and level gpe handlers into one (now possible since the
gpe structure tells us whether the gpe is edge or level)


# 1.184 21-Jul-2010 deraadt

delete #if 0 code we are not going to use; ok marco


# 1.183 20-Jul-2010 deraadt

make the mask code for acpi_enable_onegpe() match other similar code


# 1.182 20-Jul-2010 deraadt

Handle the acpi interrupt controller with proper edge/level handling,
wrapped in spltty to avoid racing against the interrupt controller.
Repair the gpe bit masking code while there, and do operations in the
right order
ok kettenis mlarkin, help from jordan at figuring out the order of
operations.


# 1.181 20-Jul-2010 matthew

When we're preparing to go to sleep, only call bufq_restart() if other
preparations failed.

ok kettenis@, thib@, deraadt@


# 1.180 19-Jul-2010 deraadt

permit compilation with ACPI_DEBUG


# 1.179 19-Jul-2010 deraadt

there is no need for a seperate dsdt_softc pointer; dsdt does not have a
softc but is just a part of the single acpi driver.
ok jordan


# 1.178 19-Jul-2010 deraadt

acpi_delay() is no longer needed; ok jordan


# 1.177 18-Jul-2010 mlarkin

Clear event status on resume, to avoid going back to sleep/reboot/shutdown
immediately after resume. Tested by a bunch of people on hackers@ on
various hardware. Fixes at least Toshiba NB200 and Gateway LT3103u.

ok marco


# 1.176 13-Jul-2010 deraadt

rename some thread related interfaces so they are logical


# 1.175 10-Jul-2010 jordan

Verify _ADR bdf is correct (some systems return 0xffff)
Works on Dell x9xx and Studio 1555


# 1.174 09-Jul-2010 jordan

Clear the noise


# 1.173 08-Jul-2010 jordan

Add mapping for ACPI device to PCI bdf (match autoconf tree)
Simplify resource parsing function to use buffer argument
Convert namespace linked lists to use queue macros
ok marco@, deraadt@


# 1.172 06-Jul-2010 deraadt

Use spltty() to lock downcalls from apm against the information being
modified by the acpi thread.
ok kettenis


# 1.171 06-Jul-2010 deraadt

Don't use a workq to do the suspend, because it races aginst the acpi
thread. Instead, just tell the acpi thread to do the suspend for us.
This makes apmd & zzz work correctly. While here, have acpithinkpad
attempt to post the event to apm before prompting the suspend itself.
ok kettenis marco mlarkin


# 1.170 05-Jul-2010 mlarkin

Reset register SCI_EN on resume. The ACPI spec clearly states we don't have
to do this, but many machines don't, or forget to, which leads to all sorts
of problems on resume (broken battery stats, overheating, failure to resume
more than once, etc).

tested by many, ok marco@, deraadt@


# 1.169 01-Jul-2010 thib

do bufq quiesce again, since vnd/the bufq_done call in
biodone has been fixed.


# 1.168 01-Jul-2010 jordan

Backout recent AML changes, breaks on R210 and others


# 1.167 01-Jul-2010 jordan

Simplified aml_parse_resource call, cleanup code
ok marco


# 1.166 01-Jul-2010 jordan

Fix subordinate bus # for multi-root PCI buses
ok marco, kettenis


# 1.165 30-Jun-2010 thib

Disable/partially backout the bufq quiesce changes as this
is causing havoc with vnds and release must be buildable.


# 1.164 29-Jun-2010 deraadt

Fix ramdisks (new code was placed inside a SMALL_KERNEL chunk)


# 1.163 29-Jun-2010 jordan

Add support for mapping ACPI to PCI devices
ok kettenis, deraadt


# 1.162 29-Jun-2010 kettenis

Call bufq_quiesce() on suspend to stop all I/O and bufq_restart on resume.
Makes suspend/resume work much better while doing disk I/O.

ok mlarkin@


# 1.161 29-Jun-2010 tedu

60 is a more reasonable guess for number of minutes in an hour than 100.
spotted by TeXitoi. ok deraadt


# 1.160 27-Jun-2010 jordan

Add _acpi_matchhids routine, precursor to pci device match


# 1.159 27-Jun-2010 mlarkin

Clear ACPI fixed event status on resume (power buttons/etc) so that some
machines don't immediately resume after suspending to S3.

Tested by kettenis@ and I on 5 or 6 machines, deraadt@ "yeah, looks good"


# 1.158 27-Jun-2010 jordan

Cleaned up acpi_maptable, adds to table list directly


# 1.157 07-Apr-2010 deraadt

If we have done a wsdisplay_suspend(), and ACPI decides it does not want to
suspend, we must roll back by calling wsdisplay_resume() or the virtual
console switching will be locked forever
ok oga


# 1.156 07-Apr-2010 kettenis

Make suspend/resume work on MP machines (running an MP kernel). Joint work
from mlarkin@ and me, with some amd64 fixes thrown in by deraadt@

ok marco@, deraadt@, pirofti@, mlarkin@


# 1.155 31-Mar-2010 kettenis

Bring /dev/apm support in line with apm(4). Make the suspend button send
a suspend request event to apmd(8) instead of suspending immediately. Also
keep track of whether /dev/apm and /dev/apmctl are currently open, such that
we can still suspend immediately if apmd(8) isn't running.

ok deraadt@, marco@, pirofti@, jsing@, oga@


# 1.154 30-Mar-2010 oga

Prevent the apmd/x races for good.

When we hit suspend time, go through all wsdisplays on the system. if
they are in mode MAPPED, but not MODE_DUMBFB then if possible do a full
vt switch to a !mapped vt, and prevent switching back until resume time.
This has to be called from MD code because this involves userland
running so that X can run the vt switch signal handler. This way, any
case where we are using the "poke registers from userland" model, we
will not be on the hardware when we go down, so the kernel can actually
handle thing properly.

Tested on several acpi laptops (by kettenis@ and ian@), x40 (me and
beck@ at LEAST) and zaurus (me). Maybe others, but if so I forgot who at
this time..

Idea from deraadt somewhere over the Faroe Islands (I thought of a
similar thing myself a while ago). Much prompting from him. Ok and
comments miod@


# 1.153 25-Mar-2010 oga

Fix & vs. && in the kqfilter code.

Based on a comparison with the apm code.

ok deraadt@, kettenis@


Revision tags: OPENBSD_4_7_BASE
# 1.152 26-Nov-2009 mlarkin

Visual (and on some machines, audio) feedback while the machine is
resuming via calls to aml _SST method. This also makes the "moon" LED on
thinkpads to blink during resume and cease when resume is completed.

ok pirofti@


# 1.151 26-Nov-2009 deraadt

Toggle into "cold" when we we disable interrupts, because tsleeping
in the AML interpreter and context switching into userland processes
tends to sometimes pop the ACPI trampoline page out of our address
space. Makes lots more machines work.
ok kettenis mlarkin


# 1.150 24-Nov-2009 jsg

in the resume path evaluate _WAK before dealing with device drivers
like the spec says, not after.

ok mlarkin@


# 1.149 24-Nov-2009 deraadt

abort suspends on SMP systems until that gets written; ok mlarkin


# 1.148 23-Nov-2009 mlarkin

pci should return the result of it's children's suspend/resume calls back
to its parent.

handle suspend failure case and unwind if devices have an activate function
that returns failure.

ok deraadt@, kettenis@


# 1.147 23-Nov-2009 mlarkin

acpi* devices need to call their children on suspend/resume.
ok deraadt@


# 1.146 23-Nov-2009 pirofti

Remove ACPI_SLEEP_ENABLED checks.

This enables by default the suspend/resume paths in the kernel.

Okay deraadt@.


# 1.145 23-Nov-2009 deraadt

knf and spacing that are super annoying


# 1.144 23-Nov-2009 mlarkin

Hook up Theo's new children suspend/resume function, as well as more ACPI
suspend/resume glue.

ok deraadt@


# 1.143 26-Oct-2009 deraadt

Do not do strncmp comparisons on dv_xname because longer device names which
look similar could arrive in the future. Instead, compare directly against
dv_cfdata->cf_driver->cd_name
Issue originally spotted by miod


# 1.142 26-Oct-2009 jordan

Verify checksum+address when loading ACPI tables.
Some systems had invalid entries in RSDT/XSDT.
ok marco@


# 1.141 23-Jul-2009 cnst

New aibs(4) driver for ASUSTeK AI Booster (ACPI ATK0110) hardware monitoring,
with sensor state support through limits provided by the ACPI.

Tested on several ASUS motherboards kindly networked by Sam Fourman Jr.

ok deraadt marco jordan


Revision tags: OPENBSD_4_6_BASE
# 1.140 03-Jun-2009 pirofti

Import acpi power resource driver. This is needed for suspend/resume logic.

Okay jordan@ and marco@.


# 1.139 03-Jun-2009 pirofti

Add acpivideo support. This adds brightness support for all laptops
except thinkpads, they will use the acpithinkpad driver. The driver is
also hooked into wsconsole. So brightness can be adjusted via:

$ wsconsctl display.brightness=<percentage>

This is very helpfull on some laptops that have a nasty bios and get two
steps instead of one when pressing the brightness button.

Tested on various dell, fujitsu, acer, samsung and other laptops.

Okay marco@, miod@. Suggestions from kettenis@.
Lots of reviews and help from miod@, thanks!


# 1.138 03-Jun-2009 jordan

Added ejectable bay for IDE cdroms
ok marco@


# 1.137 30-Apr-2009 marco

Oga changed apm ioctls and forgot to grep. Temporary work around by hitting
the sleep button when we get any request. This interface will chnage at
some point.
ok oga


# 1.136 19-Apr-2009 krw

Call acpi_sleep_walk() when powering down just like when entering
any other sleep state. This tells acpi wake devices to not wake up
the box until manual powering up resets their status. Makes my Dell
GX520 stay powered off in response to 'halt -p'.

In snaps for a while.

ok marco@ (several times) deraadt@


# 1.135 17-Apr-2009 pirofti

Add device ranking support for future sleep/wake developments.

okay marco@


# 1.134 11-Mar-2009 jordan

Cleanup.. use aml_evalinteger instead of aml_evalname
ok marco@


# 1.133 10-Mar-2009 jordan

Fix cut/paste error.. call _BFS not _PTS
ok marco@


Revision tags: OPENBSD_4_5_BASE
# 1.132 21-Feb-2009 marco

overzealous cut/paste; found by Vladimir Kirillov <proger@uaoug.org.ua>


# 1.131 20-Feb-2009 marco

Oops didn't mean for Debugger() to remain there.


# 1.130 19-Feb-2009 marco

suspend/resume bits so that we can develop this in tree. This is disabled.
code from mlarkin and me
help from art,toby,jordan and several others
ok jordan, go for it deraadt


# 1.129 10-Feb-2009 jordan

Fix null pointer for shutdown if acpi not enabled
ok marco@


# 1.128 04-Feb-2009 kettenis

Some systems that claim to be ACPI 2.0 apparently don't provide an XSDT
address. Make the code fall back on the RSDT on those systems.

From Joerg Niendorf <p3y80@internode.on.net>. Fixes PR 5986.


# 1.127 17-Jan-2009 krw

A couple of stray malloc/memset -> malloc(M_ZERO), a bit of knf and
lose a couple of unnecessary casts. No functional change.

Feedback from kettenis@

ok kettenis@ marco@


# 1.126 06-Nov-2008 marco

Collapse all sanity checks of hids into one function.

Originaly from jordan


Revision tags: OPENBSD_4_4_BASE
# 1.125 02-Jul-2008 fgsch

add acpivideo. for now almost an empty skel so others can work on
suspend/resume. eventually it will also manage output switching and
brightness where supported. prodded by marco@


# 1.124 11-Jun-2008 marco

Fix $OpenBSD$ for once and for all


# 1.123 08-Jun-2008 deraadt

aaa_dev was being set from an uninitialized variable; ok toby


# 1.122 07-Jun-2008 marco

Kill ACPIVERBOSE. It was never used and is worthless to boot.

comments jmc
ok toby


# 1.121 06-Jun-2008 marco

Shrink acpi some more for floppies and while here re-enable acpi on the
floppies. Fixes crazy sony vaio kettenis has.

tested by deraadt
ok deraadt jordan kettenis


# 1.120 01-Jun-2008 marco

Fix \n nit in SMALL_KERNEL.


# 1.119 01-Jun-2008 marco

Re-enable new parser. Lots of testing by various folks. Fits all media
now too.

ok kettenis deraadt


# 1.118 16-May-2008 dlg

back out jordans new parser.

it didnt have enough oks from the right people, it did break machines
that were previously working, and it bloated the RAMDISKS and install
media beyond the constraints of the media.

requested by deraadt@


# 1.117 14-May-2008 jordan

Adding new ACPI Parser code
Old guts of code still exists, needs to be torn out next
ok marco@
ok brad@


# 1.116 27-Apr-2008 jcs

add acpithinkpad(4), an acpi support driver for ibm/lenovo
thinkpads.

provides functionality that the bios did on older, non-acpi
thinkpads like respond to screen brightness keys and toggle the
bluetooth device on and off.

ok deraadt@ and marco@


# 1.115 24-Apr-2008 jsing

Introduce acpiasus(4), a driver for the ACPI based hotkeys found in many
ASUS laptops (including the ASUS EeePC) - largely based on NetBSD's
asus(4) driver. On the ASUS EeePC this allows us to enable/disable
wireless, change screen brightness and use the volume keys.

ok jsg@, weingart@


# 1.114 12-Apr-2008 kettenis

Get rid of acpi_s5 global variables; simply send SIGUSR2 instead of SUGUSR1
to tell init(8) to power down the machine.

ok krw@


Revision tags: OPENBSD_4_3_BASE
# 1.113 21-Jan-2008 jsg

Check against RESET_REG_SUP in FADT before doing the acpi reset dance
after all, not doing so appears to break more machines than it fixes.

ok marco@


# 1.112 05-Jan-2008 kettenis

Never attach more acpicpu(4) devices than cpu(4) devices. Always attach
the first acpicpu(4) device on non-MULTIPROCESSOR kernels, regardless of its
ACPI processor ID.

ok mikeb@


# 1.111 05-Dec-2007 deraadt

Hang acpi(4) below bios(4) instead of mainbus(4). This lets us move acpi(4)
before pcibios(4), and then.... if acpi(4) attaches, skip pcibios(4) since
it messes the machines up. Fixes claudio's HP dl320 G5, and almost assuredly
others.
ok kettenis


# 1.110 05-Dec-2007 deraadt

repair acpiopen/acpiclose and friends to handle the /dev/apmctl interface,
so that apmd(8) can actually work correctly against acpi(4), and thus,
even do the fancy -C/-A stuff; also tested by jmc


# 1.109 02-Dec-2007 fgsch

fix the trailing space for real now.


# 1.108 02-Dec-2007 fgsch

avoid trailing spaces on output; marco@ ok.


# 1.107 27-Nov-2007 kettenis

Fix off-by-one that made us attach acpicpu0 and acpicpu1 on GENERIC kernels.

ok marco@


# 1.106 25-Nov-2007 jsg

When available and ACPI is enabled use the ACPI reset method
to reboot. This is required for some quirky sis machines
and likely others to reboot.

Tested by many. 'commit early commit often' deraadt@


# 1.105 19-Nov-2007 deraadt

sigh, amd64 still uses that stupid acpi_enabled variable...


# 1.104 19-Nov-2007 deraadt

softc's come initialized to 0, you know...


# 1.103 19-Nov-2007 deraadt

store revision; from jsg


# 1.102 19-Nov-2007 kettenis

Initialize sc_facs earlier.

ok deraadt@, beck@, canacar@


# 1.101 16-Nov-2007 deraadt

fix the bus_space #define nightmare, so that amd64 and i386 are much more
uniform. as a result shared code like acpi needs less #ifdef's
ok marco kettenis


# 1.100 15-Nov-2007 deraadt

remove the #ifdef ACPI_ENABLE. the EC codepath is either safe enough (and
will be improved), or it is unsafe because AML assumes it must be there
ok almost everyone


# 1.99 15-Nov-2007 mikeb

Make acpicpu(4) attach to the existing CPUs only.

Problem reported by form@. Fix idea by kettenis@.
Lots of help from deraadt@. Tested by deraadt@, form@ and me.

ok deraadt marco


# 1.98 14-Nov-2007 deraadt

spacing


# 1.97 12-Nov-2007 deraadt

correct : vs , in attach-time messages


# 1.96 06-Nov-2007 deraadt

all ACPI_ENABLE kernels need acpi_interrupt()


# 1.95 06-Nov-2007 deraadt

move acpi_read_pmreg() and acpi_write_pmreg() out of SMALL_KERNEL so
that acpi ramdisks can compile (obvious solution)


# 1.94 05-Nov-2007 canacar

Establish the acpi interrupt earlier. Fixes problems with some
GENERIC.MP kernels not getting acpi interrupts. This is a
workaround until the real issue with late establishment is fixed.
ok weingart@, beck@, gwk@


# 1.93 03-Nov-2007 jordan

Added support for displaying wakeup devices
ok beck@,weingart@,gwk@


# 1.92 11-Oct-2007 kettenis

When walking the tree yo find _PRT methods, check whether the device is
actually enabled and functioning. Similar to what we do for _INI.

ok weingart@, beck@


# 1.91 08-Oct-2007 krw

More simple memset(,0,) -> M_ZERO changes. In this batch move to
size(*p) as the first malloc() parameter where p is declared locally
and thus easy to check. Add M_ZERO to gpe_table allocation in acpi.c
even though there is no obvious bzero or memset nearby.


# 1.90 13-Sep-2007 mk

Remove unnecessary memset() (see aml_evalnode(9)) and a bit of KNF.

ok toby


# 1.89 13-Sep-2007 weingart

Implement ACPI 6.5.1 spec tree walk for _STA and _INI.
Fixes mk's laptop. No regressions so far (thank you to the
testers).

ok gwk@, mk@, marco@


Revision tags: OPENBSD_4_2_BASE
# 1.88 17-Apr-2007 mk

Now that acpidock no longer hangs machines during boot (at least
according to the reports I've been getting) it should be safe to enable
it again, so remove the #if 0 stuff around it.

ok marco


# 1.87 11-Apr-2007 jordan

Added changes for C-State
ok marco@


# 1.86 26-Mar-2007 marco

Calculate minutes left more accurately.

From giovanni <qgiovanni@gmail.com>


Revision tags: OPENBSD_4_1_BASE
# 1.85 03-Mar-2007 mk

Stop looking for acpidock until we fix that stuff that breaks all pre-60
series ThinkPads with acpidock(4) enabled.


# 1.84 22-Feb-2007 marco

We were getting no interrupts in .MP. There are 2 fixes; one in
acpi_machdep.c and move the interrupt establishment down after ioapic
has been initialized.

ok jordan


# 1.83 21-Feb-2007 kettenis

Make an acpi-enabled GENERIC.MP work on systems that only support PIC mode.

ok marco@


# 1.82 19-Feb-2007 jordan

Added acpi_map_address function for mapping GAS/IO registers
ok marco@


# 1.81 18-Feb-2007 jordan

Added dynamic perfect hash for opcodes
ok marco@


# 1.80 18-Feb-2007 marco

Shave off another K pre compression.
Shuffle SMALL_KERNEL functions around so that we only have a single #ifndef


# 1.79 17-Feb-2007 marco

Add SMALL_KERNEL throught acpi to make it fit on boot media.
ok deraadt


# 1.78 13-Feb-2007 marco

Add smarter _INI code. This helps with some dissapearing devices.

ok kettenis jordan


# 1.77 31-Jan-2007 gwk

Introduce acpi_hasprocfvs thats "ACPI Has Processor Frequency and Voltage
Scalling" a global flag set by acicpu attach to indicate that ACPI is
handling fvs through the _PSS objects. This will fix disappearing
EST/powernow when booting kernels with acpi enabled, as acpicpu isnt
compiled into GENERIC yet. Also in cases where acpicpu is built in and
legacy EST/powernow work but acpi is missing the _PSS object, the legacy
routines will be used.

tested by dim@, and Nick Nauwelaerts <nick AT nauwelaerts.net>
ok marco@, dim@


# 1.76 22-Jan-2007 mk

Actually look for docking stations by looking for _DCK nodes.

ok marco


# 1.75 02-Jan-2007 marco

On some systems EC must be attached as the first device. Move EC from
generic _HID devices to its own routine to force first attach. Bug found
and debugged by joris@

Code mostly written by joris@


# 1.74 26-Dec-2006 marco

Add polling to devices that require it such as acpibat and acpitz.
Use the same tsleep/wakeup mechanism as the interrupt code to keep it non-
concurrent.


# 1.73 21-Dec-2006 deraadt

even more knf love


# 1.72 21-Dec-2006 marco

Remove some leftover acpiec gpe/interrupt code.


# 1.71 20-Dec-2006 deraadt

oops, name already used


# 1.70 20-Dec-2006 deraadt

rename __acpi_enable_gpe(); ok marco


# 1.69 20-Dec-2006 deraadt

acpi_unmap_pmregs() is not used, ok marco


# 1.68 19-Dec-2006 deraadt

simplify some low-level methods substantially (shrinking code); ok marco jordan


# 1.67 06-Dec-2006 mk

malloc(..., M_NOWAIT) with no NULL check is bad. If allocation fails,
complain, clean up, and bail out of attach.

input and ok gwk


# 1.66 28-Nov-2006 jordan

Fixed clearing arg for acpi_init_pic
ok kettenis@


# 1.65 27-Nov-2006 kettenis

Don't spam dmesg.


# 1.64 27-Nov-2006 jordan

Fixes for acpiprt routing
Post-parse fixup for namerefs
ok kettenis@


# 1.63 25-Nov-2006 marco

Don't handle acpiev events inside the gpe handler since this causes deadlocks.

ok jordan@


# 1.62 15-Nov-2006 kettenis

Hook up acpi apic interrupt routing support to generic acpi code.

ok jordan@


# 1.61 02-Nov-2006 marco

Kill acpiec attach function and add its functionality to the generic _HID
device attach

prodded and ok jordan


# 1.60 02-Nov-2006 marco

Fix comments to reflect reality.


# 1.59 19-Oct-2006 marco

Replace ACPI ioctls with APM ioctls. This makes ACPI talk the same protocol
as APM therefore userspace can now retrieve power information in the same
fashion as all other apm arches (zaurus, macppc, i386 etc).

To use force the use of acpi device like so: apmd -f /dev/acpi

ok jordan@


# 1.58 19-Oct-2006 jordan

Fixed GPE debug levels
Fixed ref counter on temporary bufferfields
DSDT parsing fully working on IBM Thinkpad
ok marco@


# 1.57 15-Oct-2006 jordan

Removed old GPE handler code remnants
ok marco@


# 1.56 12-Oct-2006 jordan

Massive ACPI changes
Fixed ACPI leaks
new ACPI API
ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.55 30-Jun-2006 gwk

KNF, No functional change,
"go at it" marco@


# 1.54 30-Jun-2006 jordan

Support for calling Embedded Controller via new GPE interface
ok marco@


# 1.53 30-Jun-2006 jordan

Added new code for GPE handling
Also works with ACPIEC; need to fixup acpiec.c to remove original interrupt handler
ok marco@


# 1.52 30-Jun-2006 jordan

Added offset argument for acpi read/write pmreg routines (new GPE code)
ok marco@


# 1.51 31-May-2006 todd

do not call acpi_powerdown() unless acpi has successfully initialized
ok gwk@ canacar@


# 1.50 29-May-2006 canacar

Add embedded controller support.
ok marco@ deraadt@ testing & ok gwk@


# 1.49 19-May-2006 canacar

Minimal thermal zone driver. Monitors thermal zone temperature,
shuts down the system if the 'critical temperature' is reached.
ok marco@


# 1.48 11-Apr-2006 marco

KNF.


# 1.47 11-Apr-2006 gwk

Add acpi_resume(struct acpi_softc *sc), which calls some AML methods,
detected at attach time, currently unused.

ok marco@


# 1.46 11-Apr-2006 gwk

Discover some ACPI sleep related methods, and call them at the
appropriate time in acpi_enter_sleep_state().

ok marco@


Revision tags: OPENBSD_3_9_BASE
# 1.45 26-Feb-2006 marco

Add acpicpu as a device.

ok jordan@


# 1.44 23-Feb-2006 marco

remove dead function.

ok jordan@


# 1.43 22-Feb-2006 marco

Print out all tables it found during autoconf. We'll need this data in
the future.


# 1.42 22-Feb-2006 marco

Make dmesg less verbose.

ok jordan.


# 1.41 22-Feb-2006 jordan

Added parsing of SSDT tables
ok marco@


# 1.40 22-Feb-2006 jordan

Added new aml_register to notify by device id
Moved powerdown code to button device
ok marco@


# 1.39 21-Feb-2006 marco

Readjust debug levels that crept in too low over the last few days.
KNF


# 1.38 21-Feb-2006 marco

Rig the power button so that the machine shuts down correctly.


# 1.37 20-Feb-2006 marco

Add acpibtn device. Remains disabled for now.


# 1.36 19-Feb-2006 jordan

Added code for aml_notify GPE notification callback
ok marco@


# 1.35 19-Feb-2006 grange

Fix acpi_enter_sleep_state() by replacing current ugly hack
with mostly correct sleeping state transition code.

ok brad@ marco@


# 1.34 19-Feb-2006 marco

jordan:
Enable interrupts.
Added peripheral code to make this work.

marco:
Replace softintr and other types of interrupts with a handler with context.
Fixed interrupt magic for GPEs.
Some KNF

This is work done by jordan and I but has to be a single commit.


# 1.33 17-Feb-2006 marco

Remove earlier introduced delays. We really only need them during autconf.

ok jordan@


# 1.32 17-Feb-2006 marco

Add a generic delay function as per the ACPI spec.

from jordan@


# 1.31 17-Feb-2006 marco

Use tsleep instead of delay when !cold.


# 1.30 17-Feb-2006 marco

Argh, we need delays between reads/writes. The backend reads (i2c most likely)
are so slow that values come back as 0 sometimes. We have to do this
despite the spec saying we shouldnt have to. Guess having a faster
parser than others can hurt sometimes.

Idea from jordan.


# 1.29 17-Feb-2006 jordan

Fixed code for handling _INI
Rearranged debug statements
ok marco@


# 1.28 16-Feb-2006 jordan

Added stub for calling _INI
Fixed aml_dowhile for NULL return
ok marco@


# 1.27 16-Feb-2006 jordan

Fixed parser/evaluator to retrieve battery info+state
Cleanup of parser
Dell laptops return battery status
ok marco@


# 1.26 06-Feb-2006 brad

fix a typo so this will compile on amd64.


# 1.25 04-Feb-2006 marco

When ACPI_ENABLED wasn't set but the devices were it had an unknown variable.

Pointed out by ian@.


# 1.24 03-Feb-2006 jordan

Rewrote aml parser yet again...
Fixed bus_space_map for i386/amd64
Added _BST and _BIF to acpibat to get status/info
ok marco@


# 1.23 20-Jan-2006 jordan

More evaluator changes
Removed old evaluator code
Fixed _HID value
ok marco@


# 1.22 19-Jan-2006 jordan

Added support for read/write of PCI Config space
Help jason@
ok marco@


# 1.21 18-Jan-2006 jordan

Added new evaluation routines
Added helper methods for buffer/field evaluation
Fixed broken indentation on dsdt.c

ok marco@


# 1.20 17-Jan-2006 jordan

Major overhaul of the aml parser. Evaluater temporarily disabled.

ok marco@


# 1.19 06-Jan-2006 grange

Check for ACPI control availability only if ACPI_ENABLE defined.


# 1.18 06-Jan-2006 grange

Informative error messages on attach, prettier dmesg.


# 1.17 06-Jan-2006 grange

printf -> dnprintf conversion for debug messages.


# 1.16 05-Jan-2006 grange

Find SLP_TYPx values and store them for future use in
acpi_enter_sleep_state().


# 1.15 05-Jan-2006 grange

Random KNF and cleanup


# 1.14 05-Jan-2006 grange

Provide symbolic names for the acpi sleeping states and use it.


# 1.13 28-Dec-2005 marco

Moved DEVNAME to a acpidev.h

ok jordan@


# 1.12 18-Dec-2005 sturm

add back comment about wrong code

ok marco


# 1.11 16-Dec-2005 marco

Remove test for kbd_reset. Prompted deraadt@, ok jordan@


# 1.10 16-Dec-2005 jordan

Added hooks for powerdown/power button press


# 1.9 16-Dec-2005 jordan

Cleaned up common power management register i/o
Added enter sleep state ioctl
Hooked power button interrupt
ok marco@


# 1.8 16-Dec-2005 marco

Remove autoconf chatter


# 1.7 16-Dec-2005 jordan

Moved DSDT AML parsing into parent ACPI driver
ok marco@


# 1.6 14-Dec-2005 marco

Add AC device glue.


# 1.5 14-Dec-2005 marco

Adjust debug printing levels to something more sane.


# 1.4 13-Dec-2005 marco

Add glue to attach devices to apci.


# 1.3 07-Dec-2005 marco

KNF


Revision tags: OPENBSD_3_8_BASE
# 1.2 10-Jul-2005 grange

Spacing cleanup.


# 1.1 02-Jun-2005 tholo

Start on a basic ACPI framework -- does not do much more than read out the
ACPI tables into kernel memory and attach ACPI and HPET timers currently.

In order to test this code, enabling the devices in GENERIC as well as
the ACPI_ENABLE option is needed. This code does not do any thermal
control yet, so this should be done with care depending on the platform.

In the tree so more people can contribute to making this more fully
featured.

Ok niklas@ grange@ tedu@


# 1.362 20-Jan-2019 jsg

fix SMALL_KERNEL build after changes in rev 1.361
reported by naddy@ ok deraadt@


# 1.361 19-Jan-2019 tedu

Add a pwraction sysctl that controls what the power button does on acpi.
By default, nothing changes -- shutdown is initiated. But allows turning
power button into a sleep button if desired.
(grudging) ok from a few parties


# 1.360 26-Oct-2018 kettenis

Add acpipci(4) on amd64. For now this only calls the PCI-specific _OSC
method to let the ACPI implementation know what features we support.


Revision tags: OPENBSD_6_4_BASE
# 1.359 30-Aug-2018 kettenis

Move softbutton check before the check that skips attaching certain acpi
devices. Fixes regression caused by matching on _CID in addition to
matching on _HID.

ok matthieu@, mlarkin@


# 1.358 25-Aug-2018 kettenis

Match ACPI devices based on _CID if no match for _HID is found.

ok mpi@


# 1.357 19-Aug-2018 kettenis

Add support for multiple PCI segments. Only really implemented for arm64
for now as amd64/i386 firmware still caters for legacy OSes that only
support a single PCI segment.

ok patrick@


# 1.356 03-Aug-2018 kettenis

Let ahci(4) match on _CLS instead of _HID when attaching at acpi(4). Avoids
having to add many more _HID entries to the match table.

ok deraadt@, mlarkin@


# 1.355 10-Jul-2018 kettenis

Make legacy interrupts work in acpipci(4).

ok patrick@


# 1.354 05-Jul-2018 kettenis

Add acpipci(4), a driver that supports generic ECAM-compatible PCI host
bridges based on information provided by ACPI.

ok mlarkin@


# 1.353 04-Jul-2018 kettenis

Properly pass around the PCI "chipset tag" in acpi(4) and refactor
acpimcfg(4) to call an MD initialization functions that sets up a tag for
PCI ECAM.

ok guenther@, mlarkin@, krw@


# 1.352 01-Jul-2018 kettenis

Move acpi(4) attach glue into acpi_machdep.c.

ok guenther@, deraadt@


# 1.351 01-Jul-2018 kettenis

Add support for _DSD Device Properties.

ok mlarkin@


# 1.350 30-Jun-2018 kettenis

Add intr_enable() function, intended for MI use to amd64 and i386 and use
this in the acpi(4) suspend/resume code paths.

ok deraadt@


# 1.349 29-Jun-2018 kettenis

Use <stdint.h> types.

ok mpi@, jung@, krw@, deraadt@


# 1.348 29-Jun-2018 mlarkin

ACPI: Allow (with warning) GPE handler reassignment, instead of returning
a failure code.

ok kettenis


# 1.347 26-Jun-2018 mlarkin

knf


# 1.346 26-Jun-2018 kettenis

Let acpi(4) pass down the bus dma tag.

ok deraadt@


# 1.345 25-Jun-2018 kettenis

Reorganize acpi(4) code a bit in preparation for upcoming arm64 support.

ok deraadt@


# 1.344 20-May-2018 kettenis

Wake up the acpi thread after scheduling a gpio event.

ok mpi@


# 1.343 17-May-2018 kettenis

Sprinkle some #ifndef SMALL_KERNEL such that RAMDISK kernels cuild again.

pointed out by stsp@


# 1.342 17-May-2018 kettenis

Implement GenericSerialBus OpRegion support.

ok mlarkin@


# 1.341 27-Mar-2018 kettenis

Add acpicmos(4), a driver that implements SystemCMOS OperationRegion
access support. This fixes machines where the AML doesn't check whether
support for this OperationRegion type has been registered by the OS.

ok mlarkin@


Revision tags: OPENBSD_6_3_BASE
# 1.340 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.339 10-Feb-2018 deraadt

Syncronize filesystems to disk when suspending. Each mountpoint's vnodes
are pushed to disk. Dangling vnodes (unlinked files still in use) and
vnodes undergoing change by long-running syscalls are identified -- and
such filesystems are marked dirty on-disk while we are suspended (in case
power is lost, a fsck will be required). Filesystems without dangling or
busy vnodes are marked clean, resulting in faster boots following
"battery died" circumstances.
Tested by numerous developers, thanks for the feedback.


# 1.338 08-Feb-2018 deraadt

After flushing disks, try again to release all memory. We don't need to
save dirty memory to the hibernate space.


# 1.337 08-Feb-2018 deraadt

Toss all releaseable memory, because fragmentation can get in the way
of allocating the hibernate playpen.


# 1.336 08-Feb-2018 deraadt

sr_quiesce() is a new approach for ensuring that softraid drains
output to the disks.

This is part of a larger suspend/resume filesystem-safety diff, which
has been worked on for a couple of months already. Tests by job, krw,
beck, benno, and others. Sometimes even by snapshot users...


# 1.335 29-Nov-2017 kettenis

Add support for GPIO-signaled events.

ok mlarkin@


# 1.334 16-Nov-2017 jcs

Add PCI attachment for dwiic(4) needed by Intel 100 Series machines

ok kettenis


# 1.333 14-Nov-2017 kettenis

Add a few more PNP IDs to the skip lists.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.332 17-Aug-2017 stsp

Make dwiic(4) attach its ACPI parent dependencies.
This change makes the Asus E200HA keyboard work.

Original analysis and patch by Cesare Gargano
ok kettenis@


# 1.331 09-Aug-2017 dcoppa

Add "PNP0303" (8042 PS/2 Controller) to acpi_isa_hids[]

ok kettenis@


# 1.330 22-Jul-2017 jcs

acpibat: look for _BIX first before falling back on _BIF

ACPI 4.0 deprecated _BIF for battery status so some newer machines
are exporting _BIX which extends _BIF's package with a few extra
fields. When using _BIX, export the new discharge cycle count as a
sensor.

feedback from kettenis and anton
ok deraadt


# 1.329 20-Jul-2017 kettenis

Start a list of isa(4) device names such that we can avoid printing them
as "not configured".

ok mikeb@, millert@, deraadt@


# 1.328 08-Apr-2017 deraadt

Sensors are run as callbacks inside tasks. During suspend a sensor
could be running inside a driver that will be force-detached, or due
to tsleep end up disrupting the softstate/hardstate contract. At
suspend time, quisce all these callbacks by waiting for completion.
This issue has never been observed for real, but may be implicated in
suspend/resume failures.
ok kettenis guenther mlarkin


# 1.327 08-Apr-2017 deraadt

Two prototypes for acpi_maptable; merge to one place


Revision tags: OPENBSD_6_1_BASE
# 1.326 27-Mar-2017 deraadt

Now that hibernate_alloc() only has clean success/failure, don't
need to call hibernate_free() to clean up a partial mess.
ok mlarkin kettenis


# 1.325 13-Mar-2017 deraadt

allow ramdisks to build; ok jcs


# 1.324 12-Mar-2017 jcs

try to attach acpisbs first and if successful, don't attach acpibat


# 1.323 02-Mar-2017 natano

Add a new sysctl machdep.lidaction. The sysctl works as follows:

machdep.lidaction=0 # do nothing
machdep.lidaction=1 # suspend
machdep.lidaction=2 # hibernate

lidsuspend is just an alias for lidaction, so if you change one, the
other one will have the same value. The plan is to remove
machdep.lidsuspend eventually when people have upgraded their
/ets/sysctl.conf.

discussed with deraadt, who came up with the new MIB name
no objections mlarkin
ok stsp halex jcs


# 1.322 28-Feb-2017 natano

Untangle abstract sleep modes from ACPI sleep states. This paves the way
for further work in the area.

idea and ok deraadt
ok mlarkin


# 1.321 22-Feb-2017 jcs

add acpisbs, an acpi smart battery subsystem driver reading data
over smbus

currently disabled because it conflicts with acpibat

ok deraadt, kettenis


# 1.320 14-Jan-2017 kettenis

Revert aml_rdpciaddr changes; breaks several machines that were working
before.


# 1.319 11-Jan-2017 jsg

Increase the size of the buffers used for _HID and _CID strings to be
able to fit "Hyper_V_Gen_Counter_V1".

ok mlarkin@ deraadt@


# 1.318 08-Jan-2017 kettenis

Make aml_rdpciaddr() work for for devices behind bridges. The code now
uses the mapping between AML nodes and PCI devices that we establish.
Because _INIT methods may end up calling aml_rdpciaddr(), make sure we
create that mapping early. Also handle devices that aren't actually present.
These devices are now included in the mapping and reads will return an
all-ones patterm whereas writes are a no-op..

ok mlarkin@


# 1.317 25-Oct-2016 pirofti

Add the resource index to aml_parse_resource() callback function.

This is needed by an upcoming acpiec commit that handles machines breaking
the current ACPI specifications.

Change suggested by and ok kettenis@, guenther@


# 1.316 18-Sep-2016 guenther

If the APIC table (MADT) isn't present then ci_acpi_proc_id won't be set
when acpicpu tries to attach, leading to a NULL dereference. The ACPI
processor object on the Dells where this was reported is bogus and useless
anyway so apply the same test when deciding whether to attach to processor
objects as acpicpu would when matching them to cpus.

Problem reported by Eivind Eide (xenofil (at) gmail.com) and
Johan Huldtgren (johan+openbsd-bugs (at) huldtgren.com).
Testing by Eivind Eide.
ok kettenis@ mlarkin@


# 1.315 03-Sep-2016 naddy

Write the system time back to the RTC every 30 minutes.
This fixes the problem that long-running machines which were not
shut down properly would reboot with a badly offset system time.

hints and ok kettenis@


# 1.314 31-Aug-2016 mlarkin

Don't put a device in the wake device list if _STA says it's not there.

Fixes at least one machine that woke up from sleep / poweroff immediately.

ok kettenis@, tested by kettenis and martijn@


# 1.313 28-Jul-2016 kettenis

Store the acpi processor ID/UID in struct cpu_info, and use it to attach
acpicpu(4) drivers to the right cpu(4).

ok mlarkin@, guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.312 10-Jun-2016 kettenis

The power management timer has been made optional in ACPI 5.0A, so don't
attempt to attach acpitimer(4) if the timer isn't present.

Pointed out by jcs@

ok jcs@


# 1.311 08-May-2016 kettenis

Make sure devices listed by a device's _DEP method are attached before
we attach the device itself. The _DEP methode, introduced in ACPI 5.0,
is a hint that the device in question depends on OpRegion support from
the devices listed by _DEP, so we have to order them the proper way.
To prevent us from attaching those devices again when we encounter them
later walking down the device tree, keep track of our attempts to
attach a device driver to them using aflag in the aml_node structure.

ok guenther@


# 1.310 25-Apr-2016 pirofti

We attach acpihpet(4) and acpiec(4) without using the HID, so do not
report it in the dmesg.

OK deraadt@, mlarkin@.


# 1.309 02-Apr-2016 kettenis

Reduce dmesg spam by knocking out some acpi hardware IDs that don't need a
driver.

ok mpi@


# 1.308 29-Mar-2016 kettenis

Check if a device is present (using _STA) in acpi_foundhid. This prevents
us from reporting devices as "not configured" that aren't actually present,
and allows us to remove duplicated code from several drivers.

ok deraadt@, ok jsg@


# 1.307 29-Mar-2016 kettenis

Implement a cleaner way to prevent acpivout(4) from attaching if we have
a platform-specific method of controlling the backlight.

ok millert@, mlarkin@, jung@


# 1.306 28-Mar-2016 kettenis

Adding each and every new acpi device driver to acpi_foundhid() is
gettinga bit of the burden. And it really isn't how our autoconf
framework is supposed to work for busses that can be enumerated. All
the drivers already check for a matching _HID in their attach
function. So we can just drop the checks in acpi_foundhid() and let
the drivers sort themselves out.

Print a "not configured" line for devices for which we don't attach adriver.
This may be a bit much as there are devices for which we will not have a
driver. This will be removed if it causes too much dmesg spam.

ok deraadt@, mlarkin@


Revision tags: OPENBSD_5_9_BASE
# 1.305 17-Jan-2016 jsg

Take acpi_foundhid() outside of #ifndef SMALL_KERNEL so the ramdisk
kernel can find and attach dwiic(4) and sdhc(4).


# 1.304 17-Jan-2016 jsg

remove an unused global variable


# 1.303 14-Jan-2016 kettenis

Attach dwiic(4) on the Intel Bay Trail i2c controllers as well.


# 1.302 12-Jan-2016 kettenis

Improve emulation of PM registers on Hardware-redcued ACPI by emulating
SPL_TYPx, SLP_EN and WAK_STS using the new SLEEP_STATUS_REG and
SLEEP_CONTROL_REG registers. Makes the transition into S5 (powerdown) work
on the ASUS X205TA.

ok deraadt@, mlarkin@


# 1.301 12-Jan-2016 jcs

Add dwiic, a driver for the Synopsys DesignWare i2c controller found
on the Samsung ATIV Book 9 laptop. This initial version only
supports ACPI config/attachment.

Add ihidev, a HID-over-i2c driver largely based on uhidev. dwiic
handles attaching ihidev devices found in ACPI.

Add ims, a HID-over-i2c mouse/trackpad driver to get basic cursor
and button functionality from HID-compliant i2c trackpads.

ok kettenis deraadt


# 1.300 11-Jan-2016 kettenis

Make sdhc(4) attach to hardware IDs "80860F14" and "PNP0FFF".


# 1.299 09-Jan-2016 kettenis

Initial support for Hardware-Reduced ACPI. This skips the legacy mode checks
by emulating the PM_CNT register such that SCI_EN is always enabled. It also
skip attaching acpitimer(4) since the relevant registers are no longer there.

ok deraadt@, mlarkin@


# 1.298 06-Jan-2016 kettenis

Follow the spec and check the SCI_EN bit to see if ACPI has already
been enabled and don't bother enabling it again if it is. This should fix
systems that don't implement legacy mode and therefore set ACPI_ENABLE and
ACPI_DISABLE to zero.

ok mlarkin@, yasuoka@


# 1.297 23-Nov-2015 reyk

Back out the CRITICAL change, turn it back from 10% to 25%. Some
people with old/broken batteries didn't like this change, and the
underlying heuristics are wrong.


# 1.296 22-Nov-2015 reyk

The "CRITICAL" APM battery level, that is exported/faked by the ACPI
stack, is currently set to 25%. With 25% battery left, a typical
modern laptop will have at least one hour runtime left. It is an
arbitrary value, and other APM power layers in the kernel set CRITICAL
to something like 5% or 10%, so set it to 10% for now.

OK mlarkin@


# 1.295 28-Sep-2015 deraadt

In low-level suspend routines, set cold=2. In tsleep(), use this to
spit out a ddb trace to console. This should allow us to find suspend
or resume routines which break the rules. It depends on the console
output function being non-sleeping.... but that's another codepath which
should try to be safe when cold is set.
ok kettenis


# 1.294 06-Sep-2015 deraadt

clarify malloc arguments (base + length...)


# 1.293 28-Aug-2015 deraadt

fairly simple sizes for free(); ok tedu


# 1.292 20-Aug-2015 kettenis

Respect the access size when reading or writing to pci config space.
This version also makes sure writes are properly aligned, preventing
the panics seen with the previous incarnation of this change.

Fixes battery status passthrough in vmware, brightness keys on my x220
and who knows what else.

ok mlarkin@, deraadt@


Revision tags: OPENBSD_5_8_BASE
# 1.291 04-Aug-2015 deraadt

If we are setting ACPI_PM1_BM_RLD do it only once, and remember to
repeat this action at resume time.
ok mlarkin guenther


# 1.290 29-Jul-2015 deraadt

Something subtle in previous revision is locking up suspend or booting
on some laptops. Mailing lists threads mention x220 and Asus machines.
Reverting for now.


# 1.289 21-Jul-2015 kettenis

iRespect the access size when reading or writing to pci config space.
Fixes battery status passthrough in vmware and who knows what else.

Tested by many.

ok mlarkin@, deraadt@


# 1.288 17-Jul-2015 mlarkin

fix comments


# 1.287 26-Mar-2015 jsg

Use the 0x10 extended capability to determine if the trackstick
buttons are wired to the trackpad and need to be re-routed to the
trackstick. Without this change the buttons on 2015 thinkpads
get picked up as extended buttons that show up as scroll up/down.

Remove the X1 Carbon 2015 (LEN0048) and X250 (LEN0046) from the
top button area/soft buttons quirks list. Also avoid using the
quirk list entirely if the capability bit is set.

Based on work from jcs@ with feedback from mpi@
ok mpi@ jcs@


# 1.286 20-Mar-2015 kettenis

The ACPI standard says that a PCI function number of FFFF can be used to refer
to all functions on a device. This makes no sense in the context of accessing
PCI config space. Nevertheless stsp@ has a machine with AML that tries to
do config space reads from a device where _ADR encodes function number FFFF.
This is almost certainly buggy AML, but since Linux (and presumably Windows)
boot just fine on this machine we need to work around this. Linux effectively
treats function FFFF as function 7, which isn't present on this machine.
Instead we simulate a read from a non-existent device by returning a bit
pattern of all ones. Writes to function FFFF will still panic.

ok mlarkin@


# 1.285 16-Mar-2015 deraadt

Reduce use of SMALL_KERNEL in acpi, bringing acpiec driver and more gpe
handling into RAMDISK. This is now possible because the install media
has ample room. The goal is to reduce special cases where we may be
skipping (unknown) important operations...
ok mlarkin kettenis


# 1.284 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.283 07-Feb-2015 deraadt

New framework that allows hibernate to pass in entropy from it's fresh
boot.
ok mlarkin


# 1.282 06-Feb-2015 mlarkin

Fix a hibernate crash on some machines due to unmapping a page that
may not have been mapped previously (in the failure to hibernate case).

Also ensure that the lowmem ptp is mapped in all cases (not just MP).

ok kettenis


# 1.281 17-Jan-2015 deraadt

resume_randomness() before spinning up other cpus...


# 1.280 15-Jan-2015 jsg

Define a new wscons mouse type for Synaptics clickpad devices that lack
physical buttons. This will be used if the acpi pnpid for the mouse
matches a list provided by Synaptics found in Linux.

Combined with the xenocara changes this will remove the need for an xorg
config file for the x240/t440/t540 generation of thinkpads.

ok matthieu@ shadchin@ kettenis@


# 1.279 11-Jan-2015 kettenis

Tolerate checksum errors in ACPI tables (but complain about them). Apparently
there are machines out there with broken BIOSen with such checksum errors in
thier RSDT tables. Windows runs fine on these machines, so it probably does
not check. Linux ignores checksum errors as well (and prints a warning).

ok deraadt@, krw@


# 1.278 18-Dec-2014 deraadt

suspend and resume the RNG (at what looks like the right spots)
ok reyk djm


# 1.277 09-Dec-2014 doug

Replace some malloc(n*size,...) calls with mallocarray().

ok tedu@ deraadt@


# 1.276 06-Dec-2014 mpi

If "machdep.lidsuspend" is not set, do not go back to sleep if the lid
is still closed when resuming. This allows docked laptop to be resumed
without opening them.

ok mlarkin@


# 1.275 23-Nov-2014 mlarkin

If a machine has no _LIDs, then none can be open. This diff fixes a problem
where machines without any _LIDs were going back to sleep after hibernate
due to the recent suspend-if-lid-closed-on-resume diff. Noticed on qemu,
which doesn't have any _LIDs.

discussed with deraadt


# 1.274 18-Nov-2014 krw

Nuke yet more obvious #include duplications.

ok miod@


# 1.273 08-Nov-2014 mlarkin

If resuming from sleep (zzz/ZZZ) and the lid is still closed, go back to
sleep. This prevents accidental lid flex or slight opening in a backpack
from waking the machine up and leaving it resumed (powered on).

ok deraadt@


# 1.272 26-Sep-2014 kettenis

Rework piglet and pig allocation. Currently the piglet gets allocated
deep down in the suspend path, where it is really hard to recover from
allocation failure. So allocate the piglet early on in the suspend path.
Also change the piglet and piglet allocation functions to use km_alloc(9)
instead of doing pmemrange magic. This removes a bunch of code which, in the
case of the piglet allocation, is broken since it results in a NULL pointer
dereference. Also switch the piglet allocation to not wait. If we can't
allocate 16MB of phys contig memory on a halfway modern machine we're almost
certainly under a lot of memory pressure and we're better off not trying to
hibernate anyway.

ok mlarkin@


# 1.271 25-Sep-2014 kettenis

Bring back rev 1.249. Now that mp_setperf() has been fixed, it should be
safe to restore perflevel when resuming. Should fix the issues people
see with the processor running at full speed heating up the machine while
the apm command reports that the CPUs are running at a low speed.

ok dcoppa@, deraadt@


# 1.270 23-Sep-2014 kettenis

Release the acpi lock around calls to wsdisplay_suspend() and
wsdisplay_resume(). The X server may adjust the display brightness just
before/after the VT switch when suspending/resuming. In fact, it may
already be blocked adjusting the display brightness for some reason
unrelated to the suspend request.

The VT switch request will eventually time out, but the delay is annoying
and the failed VT switch will lead to problems after resume.

Needed for a future update of the xf86-video-intel driver.

ok mlarkin@


# 1.269 19-Sep-2014 kettenis

Use config_suspend_all(9).


# 1.268 08-Sep-2014 kettenis

Clear the PME Status bit on PCI devices in response to Device Wake events.
Fixes the interrupt storm on the 2009 Mac mini with WOL enabled on its
nfe(4) interface.

ok mlarkin@


Revision tags: OPENBSD_5_6_BASE
# 1.267 20-Jul-2014 mlarkin

Support hibernating to softraid crypto volumes.

much help and ok from deraadt@


# 1.266 16-Jul-2014 mlarkin

Reenable hibernate RLE support and flush+zero all memory after unpack.


# 1.265 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.264 12-Jul-2014 kettenis

Add a function to drop all clean pages on the page daemon queues and call
it when we hibernate.

ok mlarkin@, miod@, deraadt@


# 1.263 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.262 11-Jul-2014 mlarkin

Flush the buffercache to 16MB on hibernate and restore its previous max
size (kern.bufcachepercent) on resume, for better hibernate performance.

ok beck@


# 1.261 10-Jul-2014 blambert

remove unneeded header

ok mlarkin@


# 1.260 01-Jul-2014 kettenis

Adding an additonal delay after each access to SystemMemory or SystemIO
during autoconf makes no sense.

ok deraadt@


# 1.259 23-Jun-2014 kettenis

SystemMemory is used to access memory mapped registers on some machines, so we
must use the bus_space(9) API to access it instead of memcpy(9). Also make
sure we properly align access to fields that need more than byte alignment.

ok guenther@


# 1.258 21-May-2014 mlarkin

Fix invalid / missing format specifiers in several dnprintf statements
(used only when ACPI_DEBUG is enabled)

From a diff on tech@ by Fabian Raetz, with a minor change by me, tested
on i386 and amd64 with ACPI_DEBUG enabled and disabled.


# 1.257 25-Apr-2014 mlarkin

Defer the free of the hibernate structures until later, when it is safe to
do so.

Freeing the hibernate structures too early leads to inadvertent calls into
splx when we aren't ready for them yet. Found when trapping splx calls in
the resume path.

discussed with deraadt@ at m2k14


# 1.256 13-Apr-2014 mlarkin

Fix a badly worded debug message that implied the entire machine was going
to sleep, instead of just the acpi thread going to sleep.


# 1.255 31-Mar-2014 mpi

Change the order of operations during a suspend/resume cycle and
call bufq_quiesce() after executing the DVACT_QUIESCE handlers.

This should be safe since no disk nor controller drivers have such
handler but it will allow us to detach sd(4) devices attached to a
USB bus.

Another benefit pointed out by kettenis@ is that drivers that need
to read a firmware from the disk should be able to do it at resume
time in a DVACT_WAKEUP handler.

ok kettenis@, deraadt@


# 1.254 13-Mar-2014 dlg

get rid of the assumption that the head of the alldevs list is the
"mainbus" device. this breaks when mpath is enabled because it
attaches before mainbus and therefore takes the head position.

have autoconf provide device_mainbus() which looks up mainbus_cd,
and use that instead.

discussed with deraadt who just wants mpath stuff to move forward
despite there being many ways to shine this particular turd.


Revision tags: OPENBSD_5_5_BASE
# 1.253 21-Feb-2014 deraadt

Do the lid control further on in the resume sequence, since this is
AML, and will do WAITOK operations. While cold, that is a bad idea.
We seem to be safe with respect to the spec, I do not see any event
debounce issues here.
ok kettenis, tested by many


# 1.252 20-Jan-2014 claudio

Instead of checking sc->sc_revision <= 1 check that the
fadt->hdr_revision <= 1. This is a better check since it checks
the version of the proper descriptor. This allows the alix APU
to reboot since those boards have currently a wrong sc->sc_revision.
Committing this now so that it can be tested easier.
With and OK jsg@ also fine by derradt@


# 1.251 24-Dec-2013 kettenis

If the FADT has its SMI_CMD set to zero, assume we're only ACPI-only hardware
and don't need to disable SMI ownership of the ACPI hardware registers.

ok mlarkin@


# 1.250 23-Dec-2013 kettenis

Revert previous commit. It makes resume hang on some MP systems running
GENERIC.MP.


# 1.249 16-Dec-2013 kettenis

Restore hw.setperf upon resume like we do for apm(4) on i386. Seems we need
to do this fairly late in the resume to avoid hangs.

ok deraadt@, mlarkin@


# 1.248 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


# 1.247 06-Nov-2013 mpi

Add support for Power Resources for Dx states and the necessary hook
for PCI devices. This hook should be called twice, before and after
changing the power state of a PCI device.

Before setting the device to the new state, the ACPI layer will notify
every power resources linked to the device for that state and make sure
they are turned "_ON". After changing the state of the device, it will
decrement the reference of every power resources linked to that device
for the old state and turn them "_OFF" if they are no longer referenced.

This fixes the no-USB after resume problem seen on various ThinkPad,
problem initialy diagnosed with Alexander Polakov.

ok kettenis@, deraadt@


Revision tags: OPENBSD_5_4_BASE
# 1.246 01-Jun-2013 mlarkin

Stop printing ACPI wakeup devices in dmesg after the 16th wakeup device.

Helps with VMware VMs that report hundreds of wakeup devices.

ok reyk@, deraadt@


# 1.245 31-May-2013 bluhm

Fix the build for a kernel without wd and pciide in its config.
OK deraadt@ tedu@


# 1.244 30-May-2013 deraadt

Enforce ca_activate tree-walks over the entire heirarchy for all events,
cleaning up some shutdown-hook related code on the way.
(A few drivers related to sparc are still skipped at kettenis' request)
ok kettenis mlarkin, tested by many others too


# 1.243 18-Apr-2013 deraadt

pin the acpi thread to the BSP, because AML and SMI cannot be trusted. I
was certain we were doing this already, but it turns out no...


Revision tags: OPENBSD_5_3_BASE
# 1.242 24-Dec-2012 guenther

In acpiioctl(), Return though the splx() path if suser() returns an error

ok deraadt@


# 1.241 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


# 1.240 04-Oct-2012 ehrhardt

Use information provided by ACPI to attach secondary PCI host bridges.

ok kettenis@


# 1.239 07-Sep-2012 kettenis

Add a new function called acpi_pci_min_powerstate() that returns the lowest
power state for a PCI device for the power state (currently S3 or S4) we're
transitioning into. This function is probably too simplistic; there is a lot
of (silly) complexity in the ACPI spec.

ok deraadt@


Revision tags: OPENBSD_5_2_BASE
# 1.238 13-Jul-2012 pirofti

Disable acpivideo(4) on ASUS laptops.

There are some models that panic whenever an event is received.
Some cope, some don't. In order to get this right acpiec(4) requires
major rework. Until then let machines boot.

Discussed with deraadt@.


# 1.237 11-Jul-2012 mlarkin

for now, restrict hibernate to root in the acpi ioctl kernel interface.
normal rules for controlling this interface through apmd still apply.

ok deraadt@


# 1.236 09-Jul-2012 deraadt

tiny little oops in untested hibernate block


# 1.235 09-Jul-2012 deraadt

clean up ioctl() case statements


# 1.234 09-Jul-2012 deraadt

acpi_disable_onegpe() is not used; ok pirofti


# 1.233 24-May-2012 kettenis

If _MIN > _MAX consider a WordBus resource to be invalid. Fixes a panic on
the IBM x3550-M4.

ok mikeb@


# 1.232 29-Mar-2012 mlarkin

Missing #ifdef in previous diff


# 1.231 29-Mar-2012 mlarkin

Prevent hibernation attempts on machines with unsupported disk controllers.
The same check is also performed later in the hibernate sequence, but by
then it's too late to cleanly unwind (presently).

ok deraadt


# 1.230 26-Mar-2012 deraadt

simplify conditionals in acpi_sleep_state()
ok mlarkin


# 1.229 26-Mar-2012 deraadt

hook in the hibernate request code; half of this diff is from mlarkin
ok mlarkin


Revision tags: OPENBSD_5_1_BASE
# 1.228 20-Sep-2011 deraadt

Very early in suspend while the clock is still ticking realtime,
save the clock back to the rtc. The zaurus already did this.
ok phessler miod kettenis


Revision tags: OPENBSD_5_0_BASE
# 1.227 02-Jul-2011 nicm

kqueue attach functions should return an errno or 0, not a plain 1. Fix
the obvious cases to return EINVAL and ENXIO.

ok tedu deraadt


# 1.226 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.225 16-Jun-2011 pirofti

Import acpitoshiba driver.

This is originally written by Hiroyuki Aizu, ported and integrated
in our tree by Javier Vazquez <javama4 AT gmail> with tweaks and
suggestions by me.

Import prodded by deraadt@ and the lack of testers on tech@.


# 1.224 27-Apr-2011 jcs

attach acpithinkpad to newer lenovo models like the x120e

ok deraadt@


# 1.223 22-Apr-2011 jordan

Fix uninitialzied variables and formatting strings (-Wxxx errors)


Revision tags: OPENBSD_4_9_BASE
# 1.222 02-Jan-2011 jordan

Re-add code for ACPI taskqueue, eliminates need for state vars in acpi softc
Serialize processing ACPI tasks for sleep/powerdown/GPE event
ok deraadt@


# 1.221 31-Oct-2010 guenther

Revert last commit: it breaks resume on Thinkpads
"then please back it out" deraadt@


# 1.220 26-Oct-2010 jordan

Add task queue for ACPI gpe and notify handlers
This fixes eject on Dell Latitude dock and an issue on the Dell Mini
battery update.
ok deraadt


# 1.219 07-Oct-2010 jordan

Create separate functions for enabling wake and runtime gpes.
Remove old suspend/resume gpewalk function
ok deraadt, mlarkin


# 1.218 05-Oct-2010 jordan

split acpi_enable_onegpe to separate enable/disable functions
ok deraadt


# 1.217 05-Oct-2010 jordan

Added acpi_disable_allgpes function to clear GPE status
ok deraadt


# 1.216 05-Oct-2010 deraadt

clean up acpi_softc use more


# 1.215 05-Oct-2010 deraadt

delete a bit of #if 0 code


# 1.214 05-Oct-2010 deraadt

hide use of our global acpi_softc pointer a bit better


# 1.213 29-Sep-2010 deraadt

grab the acpi thread lock very early in acpi thread startup; not that it
is likely that anyone will race us... but why be careless.
ok kettenis


# 1.212 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.211 29-Aug-2010 jasper

- add a check for FWRITE here too.

ok miod@


# 1.210 27-Aug-2010 kettenis

Make PCI Power Management optional, and only enable it when acpi(4) attaches.

ok deraadt@


Revision tags: OPENBSD_4_8_BASE
# 1.209 08-Aug-2010 kettenis

Close race between the acpi thread and wscons ioctls running in process
context. We might rip this out post-release and replace it with a solution
that serializes things in a more obvious way.

ok deraadt@, miod@ and "those crazy texans who ok anything"


# 1.208 08-Aug-2010 deraadt

Report the acpi sleep states when on a RAMDISK; ok krw


# 1.207 07-Aug-2010 kettenis

Enforce acpi register access rescrictions.

ok mlarkin@, deraadt@, tested by marco@


# 1.206 07-Aug-2010 deraadt

upon resume, notify all the acpiac and acpibat drivers to update
their status. some laptops do not do this automatically.
ok kettenis canacar


# 1.205 06-Aug-2010 marco

Always call _PSW on _LID devices that support it to enable lid open to
resume the machine. Conversely disable it when the machine wakes up.

Tested by several ok deraadt


# 1.204 06-Aug-2010 deraadt

As part of a revamp of the PM1/GPE code, I write this diff to look for
unmanaged EN & STS bits in the PM1 register at interrupt time and report
them. As a side effect this splits the STS acknowledgement into two writes
(for power, and sleep) instead of one. The printf that is added (to spot
unmanaged STS bits) has not yet been triggered as far as we know.

Before the "write to PM1 registers at the right offset" diff went in, this
was not neccessary. But newer thinkpads do not have a working soft power
button without this diff.

We have no idea why.
ok mlarkin kettenis


# 1.203 05-Aug-2010 deraadt

PM1 power button events were generating 'fake events' to acpibtn PWRB,
but if there is no such device these were being tossed. Instead, arrange
for all power button handlers to flag a softc variable which is processed
by the thread; so that one place will know to request a powerdown (psignal
to init)
ok marco mlarkin


# 1.202 05-Aug-2010 deraadt

rename a variable which is annoyingly poorly named


# 1.201 05-Aug-2010 mlarkin

Write to the correct offset for various ACPI pmreg registers. Writing to
the correct register does properly clear the wake status bits and fixed
event bits, which allows some machines to have more than 1 suspend cycle.

bug spotted by deraadt@

tested by deraadt@ and myself on a half-dozen machines and others on
hackers@

ok deraadt@, marco@


# 1.200 05-Aug-2010 deraadt

Fix the power button. The pm1 register is 16 bits in size.
My mistake, a while back.
tested by krw


# 1.199 04-Aug-2010 kettenis

Print supported sleep states.

ok marco@, phessler@, deraadt@


# 1.198 03-Aug-2010 mlarkin

Use the proper flag for re-enabling certain hardware events (power btn,
sleep btn). The (incorrect) flag prevoiously used coincidentally had the
same value, so this shouldn't cause any different behavior than before.

tested on a variety of machines (i386, amd64, sp, mp) - no behavioral
change seen

ok deraadt@


# 1.197 28-Jul-2010 marco

Make all gasio failures look and behave the same.


# 1.196 28-Jul-2010 mlarkin

Delay a little longer on suspend before giving up. Fixes machines that
sometimes don't suspend because the sleep transition takes too long
(symptoms like failing to sleep but the backlight stays on).

ok deraadt@


# 1.195 27-Jul-2010 deraadt

remove XXX; EC can now be attached early when needed; ok jordan


# 1.194 27-Jul-2010 deraadt

more brackets feels good


# 1.193 27-Jul-2010 deraadt

XXX annotate a block in acpi_enter_sleep_state() that is pretty worrying


# 1.192 27-Jul-2010 deraadt

horrific indentation that nearly made me puke


# 1.191 27-Jul-2010 mlarkin

Set the sleeping indicator light on machines that support it, in case SMI
doesn't do it for us. Shows the 'moon' LED on older Thinkpads when
sleeping.

Tested on T43p, W500, T510 by me and on X61s by deraadt@.

ok deraadt@


# 1.190 27-Jul-2010 jordan

Early initialization of acpiec if ECDT table exists
Fixes hang when booting thinkpads while docked
ok deraadt


# 1.189 26-Jul-2010 deraadt

remove merge error which was commited


# 1.188 26-Jul-2010 pirofti

Add support for Sony ACPI hotkeys via a new driver: acpisony(4).

Currently it only works for the suspend button and tries to do right for
the brightness events, but I haven't found a Sony laptop that like to do
right so far.

In the future I want to make the brightness keys work on all Sony's and
also add support for the zoom hotkeys and whatever other funky keys I can
find on those things.

Okay deraadt@.


# 1.187 22-Jul-2010 deraadt

pretty large cleanup of SMALL_KERNEL


# 1.186 21-Jul-2010 deraadt

in the acpi GPE handler, late ack level interrupts not edge interrupts.
Duh.


# 1.185 21-Jul-2010 deraadt

Merge the edge and level gpe handlers into one (now possible since the
gpe structure tells us whether the gpe is edge or level)


# 1.184 21-Jul-2010 deraadt

delete #if 0 code we are not going to use; ok marco


# 1.183 20-Jul-2010 deraadt

make the mask code for acpi_enable_onegpe() match other similar code


# 1.182 20-Jul-2010 deraadt

Handle the acpi interrupt controller with proper edge/level handling,
wrapped in spltty to avoid racing against the interrupt controller.
Repair the gpe bit masking code while there, and do operations in the
right order
ok kettenis mlarkin, help from jordan at figuring out the order of
operations.


# 1.181 20-Jul-2010 matthew

When we're preparing to go to sleep, only call bufq_restart() if other
preparations failed.

ok kettenis@, thib@, deraadt@


# 1.180 19-Jul-2010 deraadt

permit compilation with ACPI_DEBUG


# 1.179 19-Jul-2010 deraadt

there is no need for a seperate dsdt_softc pointer; dsdt does not have a
softc but is just a part of the single acpi driver.
ok jordan


# 1.178 19-Jul-2010 deraadt

acpi_delay() is no longer needed; ok jordan


# 1.177 18-Jul-2010 mlarkin

Clear event status on resume, to avoid going back to sleep/reboot/shutdown
immediately after resume. Tested by a bunch of people on hackers@ on
various hardware. Fixes at least Toshiba NB200 and Gateway LT3103u.

ok marco


# 1.176 13-Jul-2010 deraadt

rename some thread related interfaces so they are logical


# 1.175 10-Jul-2010 jordan

Verify _ADR bdf is correct (some systems return 0xffff)
Works on Dell x9xx and Studio 1555


# 1.174 09-Jul-2010 jordan

Clear the noise


# 1.173 08-Jul-2010 jordan

Add mapping for ACPI device to PCI bdf (match autoconf tree)
Simplify resource parsing function to use buffer argument
Convert namespace linked lists to use queue macros
ok marco@, deraadt@


# 1.172 06-Jul-2010 deraadt

Use spltty() to lock downcalls from apm against the information being
modified by the acpi thread.
ok kettenis


# 1.171 06-Jul-2010 deraadt

Don't use a workq to do the suspend, because it races aginst the acpi
thread. Instead, just tell the acpi thread to do the suspend for us.
This makes apmd & zzz work correctly. While here, have acpithinkpad
attempt to post the event to apm before prompting the suspend itself.
ok kettenis marco mlarkin


# 1.170 05-Jul-2010 mlarkin

Reset register SCI_EN on resume. The ACPI spec clearly states we don't have
to do this, but many machines don't, or forget to, which leads to all sorts
of problems on resume (broken battery stats, overheating, failure to resume
more than once, etc).

tested by many, ok marco@, deraadt@


# 1.169 01-Jul-2010 thib

do bufq quiesce again, since vnd/the bufq_done call in
biodone has been fixed.


# 1.168 01-Jul-2010 jordan

Backout recent AML changes, breaks on R210 and others


# 1.167 01-Jul-2010 jordan

Simplified aml_parse_resource call, cleanup code
ok marco


# 1.166 01-Jul-2010 jordan

Fix subordinate bus # for multi-root PCI buses
ok marco, kettenis


# 1.165 30-Jun-2010 thib

Disable/partially backout the bufq quiesce changes as this
is causing havoc with vnds and release must be buildable.


# 1.164 29-Jun-2010 deraadt

Fix ramdisks (new code was placed inside a SMALL_KERNEL chunk)


# 1.163 29-Jun-2010 jordan

Add support for mapping ACPI to PCI devices
ok kettenis, deraadt


# 1.162 29-Jun-2010 kettenis

Call bufq_quiesce() on suspend to stop all I/O and bufq_restart on resume.
Makes suspend/resume work much better while doing disk I/O.

ok mlarkin@


# 1.161 29-Jun-2010 tedu

60 is a more reasonable guess for number of minutes in an hour than 100.
spotted by TeXitoi. ok deraadt


# 1.160 27-Jun-2010 jordan

Add _acpi_matchhids routine, precursor to pci device match


# 1.159 27-Jun-2010 mlarkin

Clear ACPI fixed event status on resume (power buttons/etc) so that some
machines don't immediately resume after suspending to S3.

Tested by kettenis@ and I on 5 or 6 machines, deraadt@ "yeah, looks good"


# 1.158 27-Jun-2010 jordan

Cleaned up acpi_maptable, adds to table list directly


# 1.157 07-Apr-2010 deraadt

If we have done a wsdisplay_suspend(), and ACPI decides it does not want to
suspend, we must roll back by calling wsdisplay_resume() or the virtual
console switching will be locked forever
ok oga


# 1.156 07-Apr-2010 kettenis

Make suspend/resume work on MP machines (running an MP kernel). Joint work
from mlarkin@ and me, with some amd64 fixes thrown in by deraadt@

ok marco@, deraadt@, pirofti@, mlarkin@


# 1.155 31-Mar-2010 kettenis

Bring /dev/apm support in line with apm(4). Make the suspend button send
a suspend request event to apmd(8) instead of suspending immediately. Also
keep track of whether /dev/apm and /dev/apmctl are currently open, such that
we can still suspend immediately if apmd(8) isn't running.

ok deraadt@, marco@, pirofti@, jsing@, oga@


# 1.154 30-Mar-2010 oga

Prevent the apmd/x races for good.

When we hit suspend time, go through all wsdisplays on the system. if
they are in mode MAPPED, but not MODE_DUMBFB then if possible do a full
vt switch to a !mapped vt, and prevent switching back until resume time.
This has to be called from MD code because this involves userland
running so that X can run the vt switch signal handler. This way, any
case where we are using the "poke registers from userland" model, we
will not be on the hardware when we go down, so the kernel can actually
handle thing properly.

Tested on several acpi laptops (by kettenis@ and ian@), x40 (me and
beck@ at LEAST) and zaurus (me). Maybe others, but if so I forgot who at
this time..

Idea from deraadt somewhere over the Faroe Islands (I thought of a
similar thing myself a while ago). Much prompting from him. Ok and
comments miod@


# 1.153 25-Mar-2010 oga

Fix & vs. && in the kqfilter code.

Based on a comparison with the apm code.

ok deraadt@, kettenis@


Revision tags: OPENBSD_4_7_BASE
# 1.152 26-Nov-2009 mlarkin

Visual (and on some machines, audio) feedback while the machine is
resuming via calls to aml _SST method. This also makes the "moon" LED on
thinkpads to blink during resume and cease when resume is completed.

ok pirofti@


# 1.151 26-Nov-2009 deraadt

Toggle into "cold" when we we disable interrupts, because tsleeping
in the AML interpreter and context switching into userland processes
tends to sometimes pop the ACPI trampoline page out of our address
space. Makes lots more machines work.
ok kettenis mlarkin


# 1.150 24-Nov-2009 jsg

in the resume path evaluate _WAK before dealing with device drivers
like the spec says, not after.

ok mlarkin@


# 1.149 24-Nov-2009 deraadt

abort suspends on SMP systems until that gets written; ok mlarkin


# 1.148 23-Nov-2009 mlarkin

pci should return the result of it's children's suspend/resume calls back
to its parent.

handle suspend failure case and unwind if devices have an activate function
that returns failure.

ok deraadt@, kettenis@


# 1.147 23-Nov-2009 mlarkin

acpi* devices need to call their children on suspend/resume.
ok deraadt@


# 1.146 23-Nov-2009 pirofti

Remove ACPI_SLEEP_ENABLED checks.

This enables by default the suspend/resume paths in the kernel.

Okay deraadt@.


# 1.145 23-Nov-2009 deraadt

knf and spacing that are super annoying


# 1.144 23-Nov-2009 mlarkin

Hook up Theo's new children suspend/resume function, as well as more ACPI
suspend/resume glue.

ok deraadt@


# 1.143 26-Oct-2009 deraadt

Do not do strncmp comparisons on dv_xname because longer device names which
look similar could arrive in the future. Instead, compare directly against
dv_cfdata->cf_driver->cd_name
Issue originally spotted by miod


# 1.142 26-Oct-2009 jordan

Verify checksum+address when loading ACPI tables.
Some systems had invalid entries in RSDT/XSDT.
ok marco@


# 1.141 23-Jul-2009 cnst

New aibs(4) driver for ASUSTeK AI Booster (ACPI ATK0110) hardware monitoring,
with sensor state support through limits provided by the ACPI.

Tested on several ASUS motherboards kindly networked by Sam Fourman Jr.

ok deraadt marco jordan


Revision tags: OPENBSD_4_6_BASE
# 1.140 03-Jun-2009 pirofti

Import acpi power resource driver. This is needed for suspend/resume logic.

Okay jordan@ and marco@.


# 1.139 03-Jun-2009 pirofti

Add acpivideo support. This adds brightness support for all laptops
except thinkpads, they will use the acpithinkpad driver. The driver is
also hooked into wsconsole. So brightness can be adjusted via:

$ wsconsctl display.brightness=<percentage>

This is very helpfull on some laptops that have a nasty bios and get two
steps instead of one when pressing the brightness button.

Tested on various dell, fujitsu, acer, samsung and other laptops.

Okay marco@, miod@. Suggestions from kettenis@.
Lots of reviews and help from miod@, thanks!


# 1.138 03-Jun-2009 jordan

Added ejectable bay for IDE cdroms
ok marco@


# 1.137 30-Apr-2009 marco

Oga changed apm ioctls and forgot to grep. Temporary work around by hitting
the sleep button when we get any request. This interface will chnage at
some point.
ok oga


# 1.136 19-Apr-2009 krw

Call acpi_sleep_walk() when powering down just like when entering
any other sleep state. This tells acpi wake devices to not wake up
the box until manual powering up resets their status. Makes my Dell
GX520 stay powered off in response to 'halt -p'.

In snaps for a while.

ok marco@ (several times) deraadt@


# 1.135 17-Apr-2009 pirofti

Add device ranking support for future sleep/wake developments.

okay marco@


# 1.134 11-Mar-2009 jordan

Cleanup.. use aml_evalinteger instead of aml_evalname
ok marco@


# 1.133 10-Mar-2009 jordan

Fix cut/paste error.. call _BFS not _PTS
ok marco@


Revision tags: OPENBSD_4_5_BASE
# 1.132 21-Feb-2009 marco

overzealous cut/paste; found by Vladimir Kirillov <proger@uaoug.org.ua>


# 1.131 20-Feb-2009 marco

Oops didn't mean for Debugger() to remain there.


# 1.130 19-Feb-2009 marco

suspend/resume bits so that we can develop this in tree. This is disabled.
code from mlarkin and me
help from art,toby,jordan and several others
ok jordan, go for it deraadt


# 1.129 10-Feb-2009 jordan

Fix null pointer for shutdown if acpi not enabled
ok marco@


# 1.128 04-Feb-2009 kettenis

Some systems that claim to be ACPI 2.0 apparently don't provide an XSDT
address. Make the code fall back on the RSDT on those systems.

From Joerg Niendorf <p3y80@internode.on.net>. Fixes PR 5986.


# 1.127 17-Jan-2009 krw

A couple of stray malloc/memset -> malloc(M_ZERO), a bit of knf and
lose a couple of unnecessary casts. No functional change.

Feedback from kettenis@

ok kettenis@ marco@


# 1.126 06-Nov-2008 marco

Collapse all sanity checks of hids into one function.

Originaly from jordan


Revision tags: OPENBSD_4_4_BASE
# 1.125 02-Jul-2008 fgsch

add acpivideo. for now almost an empty skel so others can work on
suspend/resume. eventually it will also manage output switching and
brightness where supported. prodded by marco@


# 1.124 11-Jun-2008 marco

Fix $OpenBSD$ for once and for all


# 1.123 08-Jun-2008 deraadt

aaa_dev was being set from an uninitialized variable; ok toby


# 1.122 07-Jun-2008 marco

Kill ACPIVERBOSE. It was never used and is worthless to boot.

comments jmc
ok toby


# 1.121 06-Jun-2008 marco

Shrink acpi some more for floppies and while here re-enable acpi on the
floppies. Fixes crazy sony vaio kettenis has.

tested by deraadt
ok deraadt jordan kettenis


# 1.120 01-Jun-2008 marco

Fix \n nit in SMALL_KERNEL.


# 1.119 01-Jun-2008 marco

Re-enable new parser. Lots of testing by various folks. Fits all media
now too.

ok kettenis deraadt


# 1.118 16-May-2008 dlg

back out jordans new parser.

it didnt have enough oks from the right people, it did break machines
that were previously working, and it bloated the RAMDISKS and install
media beyond the constraints of the media.

requested by deraadt@


# 1.117 14-May-2008 jordan

Adding new ACPI Parser code
Old guts of code still exists, needs to be torn out next
ok marco@
ok brad@


# 1.116 27-Apr-2008 jcs

add acpithinkpad(4), an acpi support driver for ibm/lenovo
thinkpads.

provides functionality that the bios did on older, non-acpi
thinkpads like respond to screen brightness keys and toggle the
bluetooth device on and off.

ok deraadt@ and marco@


# 1.115 24-Apr-2008 jsing

Introduce acpiasus(4), a driver for the ACPI based hotkeys found in many
ASUS laptops (including the ASUS EeePC) - largely based on NetBSD's
asus(4) driver. On the ASUS EeePC this allows us to enable/disable
wireless, change screen brightness and use the volume keys.

ok jsg@, weingart@


# 1.114 12-Apr-2008 kettenis

Get rid of acpi_s5 global variables; simply send SIGUSR2 instead of SUGUSR1
to tell init(8) to power down the machine.

ok krw@


Revision tags: OPENBSD_4_3_BASE
# 1.113 21-Jan-2008 jsg

Check against RESET_REG_SUP in FADT before doing the acpi reset dance
after all, not doing so appears to break more machines than it fixes.

ok marco@


# 1.112 05-Jan-2008 kettenis

Never attach more acpicpu(4) devices than cpu(4) devices. Always attach
the first acpicpu(4) device on non-MULTIPROCESSOR kernels, regardless of its
ACPI processor ID.

ok mikeb@


# 1.111 05-Dec-2007 deraadt

Hang acpi(4) below bios(4) instead of mainbus(4). This lets us move acpi(4)
before pcibios(4), and then.... if acpi(4) attaches, skip pcibios(4) since
it messes the machines up. Fixes claudio's HP dl320 G5, and almost assuredly
others.
ok kettenis


# 1.110 05-Dec-2007 deraadt

repair acpiopen/acpiclose and friends to handle the /dev/apmctl interface,
so that apmd(8) can actually work correctly against acpi(4), and thus,
even do the fancy -C/-A stuff; also tested by jmc


# 1.109 02-Dec-2007 fgsch

fix the trailing space for real now.


# 1.108 02-Dec-2007 fgsch

avoid trailing spaces on output; marco@ ok.


# 1.107 27-Nov-2007 kettenis

Fix off-by-one that made us attach acpicpu0 and acpicpu1 on GENERIC kernels.

ok marco@


# 1.106 25-Nov-2007 jsg

When available and ACPI is enabled use the ACPI reset method
to reboot. This is required for some quirky sis machines
and likely others to reboot.

Tested by many. 'commit early commit often' deraadt@


# 1.105 19-Nov-2007 deraadt

sigh, amd64 still uses that stupid acpi_enabled variable...


# 1.104 19-Nov-2007 deraadt

softc's come initialized to 0, you know...


# 1.103 19-Nov-2007 deraadt

store revision; from jsg


# 1.102 19-Nov-2007 kettenis

Initialize sc_facs earlier.

ok deraadt@, beck@, canacar@


# 1.101 16-Nov-2007 deraadt

fix the bus_space #define nightmare, so that amd64 and i386 are much more
uniform. as a result shared code like acpi needs less #ifdef's
ok marco kettenis


# 1.100 15-Nov-2007 deraadt

remove the #ifdef ACPI_ENABLE. the EC codepath is either safe enough (and
will be improved), or it is unsafe because AML assumes it must be there
ok almost everyone


# 1.99 15-Nov-2007 mikeb

Make acpicpu(4) attach to the existing CPUs only.

Problem reported by form@. Fix idea by kettenis@.
Lots of help from deraadt@. Tested by deraadt@, form@ and me.

ok deraadt marco


# 1.98 14-Nov-2007 deraadt

spacing


# 1.97 12-Nov-2007 deraadt

correct : vs , in attach-time messages


# 1.96 06-Nov-2007 deraadt

all ACPI_ENABLE kernels need acpi_interrupt()


# 1.95 06-Nov-2007 deraadt

move acpi_read_pmreg() and acpi_write_pmreg() out of SMALL_KERNEL so
that acpi ramdisks can compile (obvious solution)


# 1.94 05-Nov-2007 canacar

Establish the acpi interrupt earlier. Fixes problems with some
GENERIC.MP kernels not getting acpi interrupts. This is a
workaround until the real issue with late establishment is fixed.
ok weingart@, beck@, gwk@


# 1.93 03-Nov-2007 jordan

Added support for displaying wakeup devices
ok beck@,weingart@,gwk@


# 1.92 11-Oct-2007 kettenis

When walking the tree yo find _PRT methods, check whether the device is
actually enabled and functioning. Similar to what we do for _INI.

ok weingart@, beck@


# 1.91 08-Oct-2007 krw

More simple memset(,0,) -> M_ZERO changes. In this batch move to
size(*p) as the first malloc() parameter where p is declared locally
and thus easy to check. Add M_ZERO to gpe_table allocation in acpi.c
even though there is no obvious bzero or memset nearby.


# 1.90 13-Sep-2007 mk

Remove unnecessary memset() (see aml_evalnode(9)) and a bit of KNF.

ok toby


# 1.89 13-Sep-2007 weingart

Implement ACPI 6.5.1 spec tree walk for _STA and _INI.
Fixes mk's laptop. No regressions so far (thank you to the
testers).

ok gwk@, mk@, marco@


Revision tags: OPENBSD_4_2_BASE
# 1.88 17-Apr-2007 mk

Now that acpidock no longer hangs machines during boot (at least
according to the reports I've been getting) it should be safe to enable
it again, so remove the #if 0 stuff around it.

ok marco


# 1.87 11-Apr-2007 jordan

Added changes for C-State
ok marco@


# 1.86 26-Mar-2007 marco

Calculate minutes left more accurately.

From giovanni <qgiovanni@gmail.com>


Revision tags: OPENBSD_4_1_BASE
# 1.85 03-Mar-2007 mk

Stop looking for acpidock until we fix that stuff that breaks all pre-60
series ThinkPads with acpidock(4) enabled.


# 1.84 22-Feb-2007 marco

We were getting no interrupts in .MP. There are 2 fixes; one in
acpi_machdep.c and move the interrupt establishment down after ioapic
has been initialized.

ok jordan


# 1.83 21-Feb-2007 kettenis

Make an acpi-enabled GENERIC.MP work on systems that only support PIC mode.

ok marco@


# 1.82 19-Feb-2007 jordan

Added acpi_map_address function for mapping GAS/IO registers
ok marco@


# 1.81 18-Feb-2007 jordan

Added dynamic perfect hash for opcodes
ok marco@


# 1.80 18-Feb-2007 marco

Shave off another K pre compression.
Shuffle SMALL_KERNEL functions around so that we only have a single #ifndef


# 1.79 17-Feb-2007 marco

Add SMALL_KERNEL throught acpi to make it fit on boot media.
ok deraadt


# 1.78 13-Feb-2007 marco

Add smarter _INI code. This helps with some dissapearing devices.

ok kettenis jordan


# 1.77 31-Jan-2007 gwk

Introduce acpi_hasprocfvs thats "ACPI Has Processor Frequency and Voltage
Scalling" a global flag set by acicpu attach to indicate that ACPI is
handling fvs through the _PSS objects. This will fix disappearing
EST/powernow when booting kernels with acpi enabled, as acpicpu isnt
compiled into GENERIC yet. Also in cases where acpicpu is built in and
legacy EST/powernow work but acpi is missing the _PSS object, the legacy
routines will be used.

tested by dim@, and Nick Nauwelaerts <nick AT nauwelaerts.net>
ok marco@, dim@


# 1.76 22-Jan-2007 mk

Actually look for docking stations by looking for _DCK nodes.

ok marco


# 1.75 02-Jan-2007 marco

On some systems EC must be attached as the first device. Move EC from
generic _HID devices to its own routine to force first attach. Bug found
and debugged by joris@

Code mostly written by joris@


# 1.74 26-Dec-2006 marco

Add polling to devices that require it such as acpibat and acpitz.
Use the same tsleep/wakeup mechanism as the interrupt code to keep it non-
concurrent.


# 1.73 21-Dec-2006 deraadt

even more knf love


# 1.72 21-Dec-2006 marco

Remove some leftover acpiec gpe/interrupt code.


# 1.71 20-Dec-2006 deraadt

oops, name already used


# 1.70 20-Dec-2006 deraadt

rename __acpi_enable_gpe(); ok marco


# 1.69 20-Dec-2006 deraadt

acpi_unmap_pmregs() is not used, ok marco


# 1.68 19-Dec-2006 deraadt

simplify some low-level methods substantially (shrinking code); ok marco jordan


# 1.67 06-Dec-2006 mk

malloc(..., M_NOWAIT) with no NULL check is bad. If allocation fails,
complain, clean up, and bail out of attach.

input and ok gwk


# 1.66 28-Nov-2006 jordan

Fixed clearing arg for acpi_init_pic
ok kettenis@


# 1.65 27-Nov-2006 kettenis

Don't spam dmesg.


# 1.64 27-Nov-2006 jordan

Fixes for acpiprt routing
Post-parse fixup for namerefs
ok kettenis@


# 1.63 25-Nov-2006 marco

Don't handle acpiev events inside the gpe handler since this causes deadlocks.

ok jordan@


# 1.62 15-Nov-2006 kettenis

Hook up acpi apic interrupt routing support to generic acpi code.

ok jordan@


# 1.61 02-Nov-2006 marco

Kill acpiec attach function and add its functionality to the generic _HID
device attach

prodded and ok jordan


# 1.60 02-Nov-2006 marco

Fix comments to reflect reality.


# 1.59 19-Oct-2006 marco

Replace ACPI ioctls with APM ioctls. This makes ACPI talk the same protocol
as APM therefore userspace can now retrieve power information in the same
fashion as all other apm arches (zaurus, macppc, i386 etc).

To use force the use of acpi device like so: apmd -f /dev/acpi

ok jordan@


# 1.58 19-Oct-2006 jordan

Fixed GPE debug levels
Fixed ref counter on temporary bufferfields
DSDT parsing fully working on IBM Thinkpad
ok marco@


# 1.57 15-Oct-2006 jordan

Removed old GPE handler code remnants
ok marco@


# 1.56 12-Oct-2006 jordan

Massive ACPI changes
Fixed ACPI leaks
new ACPI API
ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.55 30-Jun-2006 gwk

KNF, No functional change,
"go at it" marco@


# 1.54 30-Jun-2006 jordan

Support for calling Embedded Controller via new GPE interface
ok marco@


# 1.53 30-Jun-2006 jordan

Added new code for GPE handling
Also works with ACPIEC; need to fixup acpiec.c to remove original interrupt handler
ok marco@


# 1.52 30-Jun-2006 jordan

Added offset argument for acpi read/write pmreg routines (new GPE code)
ok marco@


# 1.51 31-May-2006 todd

do not call acpi_powerdown() unless acpi has successfully initialized
ok gwk@ canacar@


# 1.50 29-May-2006 canacar

Add embedded controller support.
ok marco@ deraadt@ testing & ok gwk@


# 1.49 19-May-2006 canacar

Minimal thermal zone driver. Monitors thermal zone temperature,
shuts down the system if the 'critical temperature' is reached.
ok marco@


# 1.48 11-Apr-2006 marco

KNF.


# 1.47 11-Apr-2006 gwk

Add acpi_resume(struct acpi_softc *sc), which calls some AML methods,
detected at attach time, currently unused.

ok marco@


# 1.46 11-Apr-2006 gwk

Discover some ACPI sleep related methods, and call them at the
appropriate time in acpi_enter_sleep_state().

ok marco@


Revision tags: OPENBSD_3_9_BASE
# 1.45 26-Feb-2006 marco

Add acpicpu as a device.

ok jordan@


# 1.44 23-Feb-2006 marco

remove dead function.

ok jordan@


# 1.43 22-Feb-2006 marco

Print out all tables it found during autoconf. We'll need this data in
the future.


# 1.42 22-Feb-2006 marco

Make dmesg less verbose.

ok jordan.


# 1.41 22-Feb-2006 jordan

Added parsing of SSDT tables
ok marco@


# 1.40 22-Feb-2006 jordan

Added new aml_register to notify by device id
Moved powerdown code to button device
ok marco@


# 1.39 21-Feb-2006 marco

Readjust debug levels that crept in too low over the last few days.
KNF


# 1.38 21-Feb-2006 marco

Rig the power button so that the machine shuts down correctly.


# 1.37 20-Feb-2006 marco

Add acpibtn device. Remains disabled for now.


# 1.36 19-Feb-2006 jordan

Added code for aml_notify GPE notification callback
ok marco@


# 1.35 19-Feb-2006 grange

Fix acpi_enter_sleep_state() by replacing current ugly hack
with mostly correct sleeping state transition code.

ok brad@ marco@


# 1.34 19-Feb-2006 marco

jordan:
Enable interrupts.
Added peripheral code to make this work.

marco:
Replace softintr and other types of interrupts with a handler with context.
Fixed interrupt magic for GPEs.
Some KNF

This is work done by jordan and I but has to be a single commit.


# 1.33 17-Feb-2006 marco

Remove earlier introduced delays. We really only need them during autconf.

ok jordan@


# 1.32 17-Feb-2006 marco

Add a generic delay function as per the ACPI spec.

from jordan@


# 1.31 17-Feb-2006 marco

Use tsleep instead of delay when !cold.


# 1.30 17-Feb-2006 marco

Argh, we need delays between reads/writes. The backend reads (i2c most likely)
are so slow that values come back as 0 sometimes. We have to do this
despite the spec saying we shouldnt have to. Guess having a faster
parser than others can hurt sometimes.

Idea from jordan.


# 1.29 17-Feb-2006 jordan

Fixed code for handling _INI
Rearranged debug statements
ok marco@


# 1.28 16-Feb-2006 jordan

Added stub for calling _INI
Fixed aml_dowhile for NULL return
ok marco@


# 1.27 16-Feb-2006 jordan

Fixed parser/evaluator to retrieve battery info+state
Cleanup of parser
Dell laptops return battery status
ok marco@


# 1.26 06-Feb-2006 brad

fix a typo so this will compile on amd64.


# 1.25 04-Feb-2006 marco

When ACPI_ENABLED wasn't set but the devices were it had an unknown variable.

Pointed out by ian@.


# 1.24 03-Feb-2006 jordan

Rewrote aml parser yet again...
Fixed bus_space_map for i386/amd64
Added _BST and _BIF to acpibat to get status/info
ok marco@


# 1.23 20-Jan-2006 jordan

More evaluator changes
Removed old evaluator code
Fixed _HID value
ok marco@


# 1.22 19-Jan-2006 jordan

Added support for read/write of PCI Config space
Help jason@
ok marco@


# 1.21 18-Jan-2006 jordan

Added new evaluation routines
Added helper methods for buffer/field evaluation
Fixed broken indentation on dsdt.c

ok marco@


# 1.20 17-Jan-2006 jordan

Major overhaul of the aml parser. Evaluater temporarily disabled.

ok marco@


# 1.19 06-Jan-2006 grange

Check for ACPI control availability only if ACPI_ENABLE defined.


# 1.18 06-Jan-2006 grange

Informative error messages on attach, prettier dmesg.


# 1.17 06-Jan-2006 grange

printf -> dnprintf conversion for debug messages.


# 1.16 05-Jan-2006 grange

Find SLP_TYPx values and store them for future use in
acpi_enter_sleep_state().


# 1.15 05-Jan-2006 grange

Random KNF and cleanup


# 1.14 05-Jan-2006 grange

Provide symbolic names for the acpi sleeping states and use it.


# 1.13 28-Dec-2005 marco

Moved DEVNAME to a acpidev.h

ok jordan@


# 1.12 18-Dec-2005 sturm

add back comment about wrong code

ok marco


# 1.11 16-Dec-2005 marco

Remove test for kbd_reset. Prompted deraadt@, ok jordan@


# 1.10 16-Dec-2005 jordan

Added hooks for powerdown/power button press


# 1.9 16-Dec-2005 jordan

Cleaned up common power management register i/o
Added enter sleep state ioctl
Hooked power button interrupt
ok marco@


# 1.8 16-Dec-2005 marco

Remove autoconf chatter


# 1.7 16-Dec-2005 jordan

Moved DSDT AML parsing into parent ACPI driver
ok marco@


# 1.6 14-Dec-2005 marco

Add AC device glue.


# 1.5 14-Dec-2005 marco

Adjust debug printing levels to something more sane.


# 1.4 13-Dec-2005 marco

Add glue to attach devices to apci.


# 1.3 07-Dec-2005 marco

KNF


Revision tags: OPENBSD_3_8_BASE
# 1.2 10-Jul-2005 grange

Spacing cleanup.


# 1.1 02-Jun-2005 tholo

Start on a basic ACPI framework -- does not do much more than read out the
ACPI tables into kernel memory and attach ACPI and HPET timers currently.

In order to test this code, enabling the devices in GENERIC as well as
the ACPI_ENABLE option is needed. This code does not do any thermal
control yet, so this should be done with care depending on the platform.

In the tree so more people can contribute to making this more fully
featured.

Ok niklas@ grange@ tedu@


# 1.360 26-Oct-2018 kettenis

Add acpipci(4) on amd64. For now this only calls the PCI-specific _OSC
method to let the ACPI implementation know what features we support.


Revision tags: OPENBSD_6_4_BASE
# 1.359 30-Aug-2018 kettenis

Move softbutton check before the check that skips attaching certain acpi
devices. Fixes regression caused by matching on _CID in addition to
matching on _HID.

ok matthieu@, mlarkin@


# 1.358 25-Aug-2018 kettenis

Match ACPI devices based on _CID if no match for _HID is found.

ok mpi@


# 1.357 19-Aug-2018 kettenis

Add support for multiple PCI segments. Only really implemented for arm64
for now as amd64/i386 firmware still caters for legacy OSes that only
support a single PCI segment.

ok patrick@


# 1.356 03-Aug-2018 kettenis

Let ahci(4) match on _CLS instead of _HID when attaching at acpi(4). Avoids
having to add many more _HID entries to the match table.

ok deraadt@, mlarkin@


# 1.355 10-Jul-2018 kettenis

Make legacy interrupts work in acpipci(4).

ok patrick@


# 1.354 05-Jul-2018 kettenis

Add acpipci(4), a driver that supports generic ECAM-compatible PCI host
bridges based on information provided by ACPI.

ok mlarkin@


# 1.353 04-Jul-2018 kettenis

Properly pass around the PCI "chipset tag" in acpi(4) and refactor
acpimcfg(4) to call an MD initialization functions that sets up a tag for
PCI ECAM.

ok guenther@, mlarkin@, krw@


# 1.352 01-Jul-2018 kettenis

Move acpi(4) attach glue into acpi_machdep.c.

ok guenther@, deraadt@


# 1.351 01-Jul-2018 kettenis

Add support for _DSD Device Properties.

ok mlarkin@


# 1.350 30-Jun-2018 kettenis

Add intr_enable() function, intended for MI use to amd64 and i386 and use
this in the acpi(4) suspend/resume code paths.

ok deraadt@


# 1.349 29-Jun-2018 kettenis

Use <stdint.h> types.

ok mpi@, jung@, krw@, deraadt@


# 1.348 29-Jun-2018 mlarkin

ACPI: Allow (with warning) GPE handler reassignment, instead of returning
a failure code.

ok kettenis


# 1.347 26-Jun-2018 mlarkin

knf


# 1.346 26-Jun-2018 kettenis

Let acpi(4) pass down the bus dma tag.

ok deraadt@


# 1.345 25-Jun-2018 kettenis

Reorganize acpi(4) code a bit in preparation for upcoming arm64 support.

ok deraadt@


# 1.344 20-May-2018 kettenis

Wake up the acpi thread after scheduling a gpio event.

ok mpi@


# 1.343 17-May-2018 kettenis

Sprinkle some #ifndef SMALL_KERNEL such that RAMDISK kernels cuild again.

pointed out by stsp@


# 1.342 17-May-2018 kettenis

Implement GenericSerialBus OpRegion support.

ok mlarkin@


# 1.341 27-Mar-2018 kettenis

Add acpicmos(4), a driver that implements SystemCMOS OperationRegion
access support. This fixes machines where the AML doesn't check whether
support for this OperationRegion type has been registered by the OS.

ok mlarkin@


Revision tags: OPENBSD_6_3_BASE
# 1.340 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.339 10-Feb-2018 deraadt

Syncronize filesystems to disk when suspending. Each mountpoint's vnodes
are pushed to disk. Dangling vnodes (unlinked files still in use) and
vnodes undergoing change by long-running syscalls are identified -- and
such filesystems are marked dirty on-disk while we are suspended (in case
power is lost, a fsck will be required). Filesystems without dangling or
busy vnodes are marked clean, resulting in faster boots following
"battery died" circumstances.
Tested by numerous developers, thanks for the feedback.


# 1.338 08-Feb-2018 deraadt

After flushing disks, try again to release all memory. We don't need to
save dirty memory to the hibernate space.


# 1.337 08-Feb-2018 deraadt

Toss all releaseable memory, because fragmentation can get in the way
of allocating the hibernate playpen.


# 1.336 08-Feb-2018 deraadt

sr_quiesce() is a new approach for ensuring that softraid drains
output to the disks.

This is part of a larger suspend/resume filesystem-safety diff, which
has been worked on for a couple of months already. Tests by job, krw,
beck, benno, and others. Sometimes even by snapshot users...


# 1.335 29-Nov-2017 kettenis

Add support for GPIO-signaled events.

ok mlarkin@


# 1.334 16-Nov-2017 jcs

Add PCI attachment for dwiic(4) needed by Intel 100 Series machines

ok kettenis


# 1.333 14-Nov-2017 kettenis

Add a few more PNP IDs to the skip lists.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.332 17-Aug-2017 stsp

Make dwiic(4) attach its ACPI parent dependencies.
This change makes the Asus E200HA keyboard work.

Original analysis and patch by Cesare Gargano
ok kettenis@


# 1.331 09-Aug-2017 dcoppa

Add "PNP0303" (8042 PS/2 Controller) to acpi_isa_hids[]

ok kettenis@


# 1.330 22-Jul-2017 jcs

acpibat: look for _BIX first before falling back on _BIF

ACPI 4.0 deprecated _BIF for battery status so some newer machines
are exporting _BIX which extends _BIF's package with a few extra
fields. When using _BIX, export the new discharge cycle count as a
sensor.

feedback from kettenis and anton
ok deraadt


# 1.329 20-Jul-2017 kettenis

Start a list of isa(4) device names such that we can avoid printing them
as "not configured".

ok mikeb@, millert@, deraadt@


# 1.328 08-Apr-2017 deraadt

Sensors are run as callbacks inside tasks. During suspend a sensor
could be running inside a driver that will be force-detached, or due
to tsleep end up disrupting the softstate/hardstate contract. At
suspend time, quisce all these callbacks by waiting for completion.
This issue has never been observed for real, but may be implicated in
suspend/resume failures.
ok kettenis guenther mlarkin


# 1.327 08-Apr-2017 deraadt

Two prototypes for acpi_maptable; merge to one place


Revision tags: OPENBSD_6_1_BASE
# 1.326 27-Mar-2017 deraadt

Now that hibernate_alloc() only has clean success/failure, don't
need to call hibernate_free() to clean up a partial mess.
ok mlarkin kettenis


# 1.325 13-Mar-2017 deraadt

allow ramdisks to build; ok jcs


# 1.324 12-Mar-2017 jcs

try to attach acpisbs first and if successful, don't attach acpibat


# 1.323 02-Mar-2017 natano

Add a new sysctl machdep.lidaction. The sysctl works as follows:

machdep.lidaction=0 # do nothing
machdep.lidaction=1 # suspend
machdep.lidaction=2 # hibernate

lidsuspend is just an alias for lidaction, so if you change one, the
other one will have the same value. The plan is to remove
machdep.lidsuspend eventually when people have upgraded their
/ets/sysctl.conf.

discussed with deraadt, who came up with the new MIB name
no objections mlarkin
ok stsp halex jcs


# 1.322 28-Feb-2017 natano

Untangle abstract sleep modes from ACPI sleep states. This paves the way
for further work in the area.

idea and ok deraadt
ok mlarkin


# 1.321 22-Feb-2017 jcs

add acpisbs, an acpi smart battery subsystem driver reading data
over smbus

currently disabled because it conflicts with acpibat

ok deraadt, kettenis


# 1.320 14-Jan-2017 kettenis

Revert aml_rdpciaddr changes; breaks several machines that were working
before.


# 1.319 11-Jan-2017 jsg

Increase the size of the buffers used for _HID and _CID strings to be
able to fit "Hyper_V_Gen_Counter_V1".

ok mlarkin@ deraadt@


# 1.318 08-Jan-2017 kettenis

Make aml_rdpciaddr() work for for devices behind bridges. The code now
uses the mapping between AML nodes and PCI devices that we establish.
Because _INIT methods may end up calling aml_rdpciaddr(), make sure we
create that mapping early. Also handle devices that aren't actually present.
These devices are now included in the mapping and reads will return an
all-ones patterm whereas writes are a no-op..

ok mlarkin@


# 1.317 25-Oct-2016 pirofti

Add the resource index to aml_parse_resource() callback function.

This is needed by an upcoming acpiec commit that handles machines breaking
the current ACPI specifications.

Change suggested by and ok kettenis@, guenther@


# 1.316 18-Sep-2016 guenther

If the APIC table (MADT) isn't present then ci_acpi_proc_id won't be set
when acpicpu tries to attach, leading to a NULL dereference. The ACPI
processor object on the Dells where this was reported is bogus and useless
anyway so apply the same test when deciding whether to attach to processor
objects as acpicpu would when matching them to cpus.

Problem reported by Eivind Eide (xenofil (at) gmail.com) and
Johan Huldtgren (johan+openbsd-bugs (at) huldtgren.com).
Testing by Eivind Eide.
ok kettenis@ mlarkin@


# 1.315 03-Sep-2016 naddy

Write the system time back to the RTC every 30 minutes.
This fixes the problem that long-running machines which were not
shut down properly would reboot with a badly offset system time.

hints and ok kettenis@


# 1.314 31-Aug-2016 mlarkin

Don't put a device in the wake device list if _STA says it's not there.

Fixes at least one machine that woke up from sleep / poweroff immediately.

ok kettenis@, tested by kettenis and martijn@


# 1.313 28-Jul-2016 kettenis

Store the acpi processor ID/UID in struct cpu_info, and use it to attach
acpicpu(4) drivers to the right cpu(4).

ok mlarkin@, guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.312 10-Jun-2016 kettenis

The power management timer has been made optional in ACPI 5.0A, so don't
attempt to attach acpitimer(4) if the timer isn't present.

Pointed out by jcs@

ok jcs@


# 1.311 08-May-2016 kettenis

Make sure devices listed by a device's _DEP method are attached before
we attach the device itself. The _DEP methode, introduced in ACPI 5.0,
is a hint that the device in question depends on OpRegion support from
the devices listed by _DEP, so we have to order them the proper way.
To prevent us from attaching those devices again when we encounter them
later walking down the device tree, keep track of our attempts to
attach a device driver to them using aflag in the aml_node structure.

ok guenther@


# 1.310 25-Apr-2016 pirofti

We attach acpihpet(4) and acpiec(4) without using the HID, so do not
report it in the dmesg.

OK deraadt@, mlarkin@.


# 1.309 02-Apr-2016 kettenis

Reduce dmesg spam by knocking out some acpi hardware IDs that don't need a
driver.

ok mpi@


# 1.308 29-Mar-2016 kettenis

Check if a device is present (using _STA) in acpi_foundhid. This prevents
us from reporting devices as "not configured" that aren't actually present,
and allows us to remove duplicated code from several drivers.

ok deraadt@, ok jsg@


# 1.307 29-Mar-2016 kettenis

Implement a cleaner way to prevent acpivout(4) from attaching if we have
a platform-specific method of controlling the backlight.

ok millert@, mlarkin@, jung@


# 1.306 28-Mar-2016 kettenis

Adding each and every new acpi device driver to acpi_foundhid() is
gettinga bit of the burden. And it really isn't how our autoconf
framework is supposed to work for busses that can be enumerated. All
the drivers already check for a matching _HID in their attach
function. So we can just drop the checks in acpi_foundhid() and let
the drivers sort themselves out.

Print a "not configured" line for devices for which we don't attach adriver.
This may be a bit much as there are devices for which we will not have a
driver. This will be removed if it causes too much dmesg spam.

ok deraadt@, mlarkin@


Revision tags: OPENBSD_5_9_BASE
# 1.305 17-Jan-2016 jsg

Take acpi_foundhid() outside of #ifndef SMALL_KERNEL so the ramdisk
kernel can find and attach dwiic(4) and sdhc(4).


# 1.304 17-Jan-2016 jsg

remove an unused global variable


# 1.303 14-Jan-2016 kettenis

Attach dwiic(4) on the Intel Bay Trail i2c controllers as well.


# 1.302 12-Jan-2016 kettenis

Improve emulation of PM registers on Hardware-redcued ACPI by emulating
SPL_TYPx, SLP_EN and WAK_STS using the new SLEEP_STATUS_REG and
SLEEP_CONTROL_REG registers. Makes the transition into S5 (powerdown) work
on the ASUS X205TA.

ok deraadt@, mlarkin@


# 1.301 12-Jan-2016 jcs

Add dwiic, a driver for the Synopsys DesignWare i2c controller found
on the Samsung ATIV Book 9 laptop. This initial version only
supports ACPI config/attachment.

Add ihidev, a HID-over-i2c driver largely based on uhidev. dwiic
handles attaching ihidev devices found in ACPI.

Add ims, a HID-over-i2c mouse/trackpad driver to get basic cursor
and button functionality from HID-compliant i2c trackpads.

ok kettenis deraadt


# 1.300 11-Jan-2016 kettenis

Make sdhc(4) attach to hardware IDs "80860F14" and "PNP0FFF".


# 1.299 09-Jan-2016 kettenis

Initial support for Hardware-Reduced ACPI. This skips the legacy mode checks
by emulating the PM_CNT register such that SCI_EN is always enabled. It also
skip attaching acpitimer(4) since the relevant registers are no longer there.

ok deraadt@, mlarkin@


# 1.298 06-Jan-2016 kettenis

Follow the spec and check the SCI_EN bit to see if ACPI has already
been enabled and don't bother enabling it again if it is. This should fix
systems that don't implement legacy mode and therefore set ACPI_ENABLE and
ACPI_DISABLE to zero.

ok mlarkin@, yasuoka@


# 1.297 23-Nov-2015 reyk

Back out the CRITICAL change, turn it back from 10% to 25%. Some
people with old/broken batteries didn't like this change, and the
underlying heuristics are wrong.


# 1.296 22-Nov-2015 reyk

The "CRITICAL" APM battery level, that is exported/faked by the ACPI
stack, is currently set to 25%. With 25% battery left, a typical
modern laptop will have at least one hour runtime left. It is an
arbitrary value, and other APM power layers in the kernel set CRITICAL
to something like 5% or 10%, so set it to 10% for now.

OK mlarkin@


# 1.295 28-Sep-2015 deraadt

In low-level suspend routines, set cold=2. In tsleep(), use this to
spit out a ddb trace to console. This should allow us to find suspend
or resume routines which break the rules. It depends on the console
output function being non-sleeping.... but that's another codepath which
should try to be safe when cold is set.
ok kettenis


# 1.294 06-Sep-2015 deraadt

clarify malloc arguments (base + length...)


# 1.293 28-Aug-2015 deraadt

fairly simple sizes for free(); ok tedu


# 1.292 20-Aug-2015 kettenis

Respect the access size when reading or writing to pci config space.
This version also makes sure writes are properly aligned, preventing
the panics seen with the previous incarnation of this change.

Fixes battery status passthrough in vmware, brightness keys on my x220
and who knows what else.

ok mlarkin@, deraadt@


Revision tags: OPENBSD_5_8_BASE
# 1.291 04-Aug-2015 deraadt

If we are setting ACPI_PM1_BM_RLD do it only once, and remember to
repeat this action at resume time.
ok mlarkin guenther


# 1.290 29-Jul-2015 deraadt

Something subtle in previous revision is locking up suspend or booting
on some laptops. Mailing lists threads mention x220 and Asus machines.
Reverting for now.


# 1.289 21-Jul-2015 kettenis

iRespect the access size when reading or writing to pci config space.
Fixes battery status passthrough in vmware and who knows what else.

Tested by many.

ok mlarkin@, deraadt@


# 1.288 17-Jul-2015 mlarkin

fix comments


# 1.287 26-Mar-2015 jsg

Use the 0x10 extended capability to determine if the trackstick
buttons are wired to the trackpad and need to be re-routed to the
trackstick. Without this change the buttons on 2015 thinkpads
get picked up as extended buttons that show up as scroll up/down.

Remove the X1 Carbon 2015 (LEN0048) and X250 (LEN0046) from the
top button area/soft buttons quirks list. Also avoid using the
quirk list entirely if the capability bit is set.

Based on work from jcs@ with feedback from mpi@
ok mpi@ jcs@


# 1.286 20-Mar-2015 kettenis

The ACPI standard says that a PCI function number of FFFF can be used to refer
to all functions on a device. This makes no sense in the context of accessing
PCI config space. Nevertheless stsp@ has a machine with AML that tries to
do config space reads from a device where _ADR encodes function number FFFF.
This is almost certainly buggy AML, but since Linux (and presumably Windows)
boot just fine on this machine we need to work around this. Linux effectively
treats function FFFF as function 7, which isn't present on this machine.
Instead we simulate a read from a non-existent device by returning a bit
pattern of all ones. Writes to function FFFF will still panic.

ok mlarkin@


# 1.285 16-Mar-2015 deraadt

Reduce use of SMALL_KERNEL in acpi, bringing acpiec driver and more gpe
handling into RAMDISK. This is now possible because the install media
has ample room. The goal is to reduce special cases where we may be
skipping (unknown) important operations...
ok mlarkin kettenis


# 1.284 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.283 07-Feb-2015 deraadt

New framework that allows hibernate to pass in entropy from it's fresh
boot.
ok mlarkin


# 1.282 06-Feb-2015 mlarkin

Fix a hibernate crash on some machines due to unmapping a page that
may not have been mapped previously (in the failure to hibernate case).

Also ensure that the lowmem ptp is mapped in all cases (not just MP).

ok kettenis


# 1.281 17-Jan-2015 deraadt

resume_randomness() before spinning up other cpus...


# 1.280 15-Jan-2015 jsg

Define a new wscons mouse type for Synaptics clickpad devices that lack
physical buttons. This will be used if the acpi pnpid for the mouse
matches a list provided by Synaptics found in Linux.

Combined with the xenocara changes this will remove the need for an xorg
config file for the x240/t440/t540 generation of thinkpads.

ok matthieu@ shadchin@ kettenis@


# 1.279 11-Jan-2015 kettenis

Tolerate checksum errors in ACPI tables (but complain about them). Apparently
there are machines out there with broken BIOSen with such checksum errors in
thier RSDT tables. Windows runs fine on these machines, so it probably does
not check. Linux ignores checksum errors as well (and prints a warning).

ok deraadt@, krw@


# 1.278 18-Dec-2014 deraadt

suspend and resume the RNG (at what looks like the right spots)
ok reyk djm


# 1.277 09-Dec-2014 doug

Replace some malloc(n*size,...) calls with mallocarray().

ok tedu@ deraadt@


# 1.276 06-Dec-2014 mpi

If "machdep.lidsuspend" is not set, do not go back to sleep if the lid
is still closed when resuming. This allows docked laptop to be resumed
without opening them.

ok mlarkin@


# 1.275 23-Nov-2014 mlarkin

If a machine has no _LIDs, then none can be open. This diff fixes a problem
where machines without any _LIDs were going back to sleep after hibernate
due to the recent suspend-if-lid-closed-on-resume diff. Noticed on qemu,
which doesn't have any _LIDs.

discussed with deraadt


# 1.274 18-Nov-2014 krw

Nuke yet more obvious #include duplications.

ok miod@


# 1.273 08-Nov-2014 mlarkin

If resuming from sleep (zzz/ZZZ) and the lid is still closed, go back to
sleep. This prevents accidental lid flex or slight opening in a backpack
from waking the machine up and leaving it resumed (powered on).

ok deraadt@


# 1.272 26-Sep-2014 kettenis

Rework piglet and pig allocation. Currently the piglet gets allocated
deep down in the suspend path, where it is really hard to recover from
allocation failure. So allocate the piglet early on in the suspend path.
Also change the piglet and piglet allocation functions to use km_alloc(9)
instead of doing pmemrange magic. This removes a bunch of code which, in the
case of the piglet allocation, is broken since it results in a NULL pointer
dereference. Also switch the piglet allocation to not wait. If we can't
allocate 16MB of phys contig memory on a halfway modern machine we're almost
certainly under a lot of memory pressure and we're better off not trying to
hibernate anyway.

ok mlarkin@


# 1.271 25-Sep-2014 kettenis

Bring back rev 1.249. Now that mp_setperf() has been fixed, it should be
safe to restore perflevel when resuming. Should fix the issues people
see with the processor running at full speed heating up the machine while
the apm command reports that the CPUs are running at a low speed.

ok dcoppa@, deraadt@


# 1.270 23-Sep-2014 kettenis

Release the acpi lock around calls to wsdisplay_suspend() and
wsdisplay_resume(). The X server may adjust the display brightness just
before/after the VT switch when suspending/resuming. In fact, it may
already be blocked adjusting the display brightness for some reason
unrelated to the suspend request.

The VT switch request will eventually time out, but the delay is annoying
and the failed VT switch will lead to problems after resume.

Needed for a future update of the xf86-video-intel driver.

ok mlarkin@


# 1.269 19-Sep-2014 kettenis

Use config_suspend_all(9).


# 1.268 08-Sep-2014 kettenis

Clear the PME Status bit on PCI devices in response to Device Wake events.
Fixes the interrupt storm on the 2009 Mac mini with WOL enabled on its
nfe(4) interface.

ok mlarkin@


Revision tags: OPENBSD_5_6_BASE
# 1.267 20-Jul-2014 mlarkin

Support hibernating to softraid crypto volumes.

much help and ok from deraadt@


# 1.266 16-Jul-2014 mlarkin

Reenable hibernate RLE support and flush+zero all memory after unpack.


# 1.265 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.264 12-Jul-2014 kettenis

Add a function to drop all clean pages on the page daemon queues and call
it when we hibernate.

ok mlarkin@, miod@, deraadt@


# 1.263 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.262 11-Jul-2014 mlarkin

Flush the buffercache to 16MB on hibernate and restore its previous max
size (kern.bufcachepercent) on resume, for better hibernate performance.

ok beck@


# 1.261 10-Jul-2014 blambert

remove unneeded header

ok mlarkin@


# 1.260 01-Jul-2014 kettenis

Adding an additonal delay after each access to SystemMemory or SystemIO
during autoconf makes no sense.

ok deraadt@


# 1.259 23-Jun-2014 kettenis

SystemMemory is used to access memory mapped registers on some machines, so we
must use the bus_space(9) API to access it instead of memcpy(9). Also make
sure we properly align access to fields that need more than byte alignment.

ok guenther@


# 1.258 21-May-2014 mlarkin

Fix invalid / missing format specifiers in several dnprintf statements
(used only when ACPI_DEBUG is enabled)

From a diff on tech@ by Fabian Raetz, with a minor change by me, tested
on i386 and amd64 with ACPI_DEBUG enabled and disabled.


# 1.257 25-Apr-2014 mlarkin

Defer the free of the hibernate structures until later, when it is safe to
do so.

Freeing the hibernate structures too early leads to inadvertent calls into
splx when we aren't ready for them yet. Found when trapping splx calls in
the resume path.

discussed with deraadt@ at m2k14


# 1.256 13-Apr-2014 mlarkin

Fix a badly worded debug message that implied the entire machine was going
to sleep, instead of just the acpi thread going to sleep.


# 1.255 31-Mar-2014 mpi

Change the order of operations during a suspend/resume cycle and
call bufq_quiesce() after executing the DVACT_QUIESCE handlers.

This should be safe since no disk nor controller drivers have such
handler but it will allow us to detach sd(4) devices attached to a
USB bus.

Another benefit pointed out by kettenis@ is that drivers that need
to read a firmware from the disk should be able to do it at resume
time in a DVACT_WAKEUP handler.

ok kettenis@, deraadt@


# 1.254 13-Mar-2014 dlg

get rid of the assumption that the head of the alldevs list is the
"mainbus" device. this breaks when mpath is enabled because it
attaches before mainbus and therefore takes the head position.

have autoconf provide device_mainbus() which looks up mainbus_cd,
and use that instead.

discussed with deraadt who just wants mpath stuff to move forward
despite there being many ways to shine this particular turd.


Revision tags: OPENBSD_5_5_BASE
# 1.253 21-Feb-2014 deraadt

Do the lid control further on in the resume sequence, since this is
AML, and will do WAITOK operations. While cold, that is a bad idea.
We seem to be safe with respect to the spec, I do not see any event
debounce issues here.
ok kettenis, tested by many


# 1.252 20-Jan-2014 claudio

Instead of checking sc->sc_revision <= 1 check that the
fadt->hdr_revision <= 1. This is a better check since it checks
the version of the proper descriptor. This allows the alix APU
to reboot since those boards have currently a wrong sc->sc_revision.
Committing this now so that it can be tested easier.
With and OK jsg@ also fine by derradt@


# 1.251 24-Dec-2013 kettenis

If the FADT has its SMI_CMD set to zero, assume we're only ACPI-only hardware
and don't need to disable SMI ownership of the ACPI hardware registers.

ok mlarkin@


# 1.250 23-Dec-2013 kettenis

Revert previous commit. It makes resume hang on some MP systems running
GENERIC.MP.


# 1.249 16-Dec-2013 kettenis

Restore hw.setperf upon resume like we do for apm(4) on i386. Seems we need
to do this fairly late in the resume to avoid hangs.

ok deraadt@, mlarkin@


# 1.248 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


# 1.247 06-Nov-2013 mpi

Add support for Power Resources for Dx states and the necessary hook
for PCI devices. This hook should be called twice, before and after
changing the power state of a PCI device.

Before setting the device to the new state, the ACPI layer will notify
every power resources linked to the device for that state and make sure
they are turned "_ON". After changing the state of the device, it will
decrement the reference of every power resources linked to that device
for the old state and turn them "_OFF" if they are no longer referenced.

This fixes the no-USB after resume problem seen on various ThinkPad,
problem initialy diagnosed with Alexander Polakov.

ok kettenis@, deraadt@


Revision tags: OPENBSD_5_4_BASE
# 1.246 01-Jun-2013 mlarkin

Stop printing ACPI wakeup devices in dmesg after the 16th wakeup device.

Helps with VMware VMs that report hundreds of wakeup devices.

ok reyk@, deraadt@


# 1.245 31-May-2013 bluhm

Fix the build for a kernel without wd and pciide in its config.
OK deraadt@ tedu@


# 1.244 30-May-2013 deraadt

Enforce ca_activate tree-walks over the entire heirarchy for all events,
cleaning up some shutdown-hook related code on the way.
(A few drivers related to sparc are still skipped at kettenis' request)
ok kettenis mlarkin, tested by many others too


# 1.243 18-Apr-2013 deraadt

pin the acpi thread to the BSP, because AML and SMI cannot be trusted. I
was certain we were doing this already, but it turns out no...


Revision tags: OPENBSD_5_3_BASE
# 1.242 24-Dec-2012 guenther

In acpiioctl(), Return though the splx() path if suser() returns an error

ok deraadt@


# 1.241 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


# 1.240 04-Oct-2012 ehrhardt

Use information provided by ACPI to attach secondary PCI host bridges.

ok kettenis@


# 1.239 07-Sep-2012 kettenis

Add a new function called acpi_pci_min_powerstate() that returns the lowest
power state for a PCI device for the power state (currently S3 or S4) we're
transitioning into. This function is probably too simplistic; there is a lot
of (silly) complexity in the ACPI spec.

ok deraadt@


Revision tags: OPENBSD_5_2_BASE
# 1.238 13-Jul-2012 pirofti

Disable acpivideo(4) on ASUS laptops.

There are some models that panic whenever an event is received.
Some cope, some don't. In order to get this right acpiec(4) requires
major rework. Until then let machines boot.

Discussed with deraadt@.


# 1.237 11-Jul-2012 mlarkin

for now, restrict hibernate to root in the acpi ioctl kernel interface.
normal rules for controlling this interface through apmd still apply.

ok deraadt@


# 1.236 09-Jul-2012 deraadt

tiny little oops in untested hibernate block


# 1.235 09-Jul-2012 deraadt

clean up ioctl() case statements


# 1.234 09-Jul-2012 deraadt

acpi_disable_onegpe() is not used; ok pirofti


# 1.233 24-May-2012 kettenis

If _MIN > _MAX consider a WordBus resource to be invalid. Fixes a panic on
the IBM x3550-M4.

ok mikeb@


# 1.232 29-Mar-2012 mlarkin

Missing #ifdef in previous diff


# 1.231 29-Mar-2012 mlarkin

Prevent hibernation attempts on machines with unsupported disk controllers.
The same check is also performed later in the hibernate sequence, but by
then it's too late to cleanly unwind (presently).

ok deraadt


# 1.230 26-Mar-2012 deraadt

simplify conditionals in acpi_sleep_state()
ok mlarkin


# 1.229 26-Mar-2012 deraadt

hook in the hibernate request code; half of this diff is from mlarkin
ok mlarkin


Revision tags: OPENBSD_5_1_BASE
# 1.228 20-Sep-2011 deraadt

Very early in suspend while the clock is still ticking realtime,
save the clock back to the rtc. The zaurus already did this.
ok phessler miod kettenis


Revision tags: OPENBSD_5_0_BASE
# 1.227 02-Jul-2011 nicm

kqueue attach functions should return an errno or 0, not a plain 1. Fix
the obvious cases to return EINVAL and ENXIO.

ok tedu deraadt


# 1.226 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.225 16-Jun-2011 pirofti

Import acpitoshiba driver.

This is originally written by Hiroyuki Aizu, ported and integrated
in our tree by Javier Vazquez <javama4 AT gmail> with tweaks and
suggestions by me.

Import prodded by deraadt@ and the lack of testers on tech@.


# 1.224 27-Apr-2011 jcs

attach acpithinkpad to newer lenovo models like the x120e

ok deraadt@


# 1.223 22-Apr-2011 jordan

Fix uninitialzied variables and formatting strings (-Wxxx errors)


Revision tags: OPENBSD_4_9_BASE
# 1.222 02-Jan-2011 jordan

Re-add code for ACPI taskqueue, eliminates need for state vars in acpi softc
Serialize processing ACPI tasks for sleep/powerdown/GPE event
ok deraadt@


# 1.221 31-Oct-2010 guenther

Revert last commit: it breaks resume on Thinkpads
"then please back it out" deraadt@


# 1.220 26-Oct-2010 jordan

Add task queue for ACPI gpe and notify handlers
This fixes eject on Dell Latitude dock and an issue on the Dell Mini
battery update.
ok deraadt


# 1.219 07-Oct-2010 jordan

Create separate functions for enabling wake and runtime gpes.
Remove old suspend/resume gpewalk function
ok deraadt, mlarkin


# 1.218 05-Oct-2010 jordan

split acpi_enable_onegpe to separate enable/disable functions
ok deraadt


# 1.217 05-Oct-2010 jordan

Added acpi_disable_allgpes function to clear GPE status
ok deraadt


# 1.216 05-Oct-2010 deraadt

clean up acpi_softc use more


# 1.215 05-Oct-2010 deraadt

delete a bit of #if 0 code


# 1.214 05-Oct-2010 deraadt

hide use of our global acpi_softc pointer a bit better


# 1.213 29-Sep-2010 deraadt

grab the acpi thread lock very early in acpi thread startup; not that it
is likely that anyone will race us... but why be careless.
ok kettenis


# 1.212 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.211 29-Aug-2010 jasper

- add a check for FWRITE here too.

ok miod@


# 1.210 27-Aug-2010 kettenis

Make PCI Power Management optional, and only enable it when acpi(4) attaches.

ok deraadt@


Revision tags: OPENBSD_4_8_BASE
# 1.209 08-Aug-2010 kettenis

Close race between the acpi thread and wscons ioctls running in process
context. We might rip this out post-release and replace it with a solution
that serializes things in a more obvious way.

ok deraadt@, miod@ and "those crazy texans who ok anything"


# 1.208 08-Aug-2010 deraadt

Report the acpi sleep states when on a RAMDISK; ok krw


# 1.207 07-Aug-2010 kettenis

Enforce acpi register access rescrictions.

ok mlarkin@, deraadt@, tested by marco@


# 1.206 07-Aug-2010 deraadt

upon resume, notify all the acpiac and acpibat drivers to update
their status. some laptops do not do this automatically.
ok kettenis canacar


# 1.205 06-Aug-2010 marco

Always call _PSW on _LID devices that support it to enable lid open to
resume the machine. Conversely disable it when the machine wakes up.

Tested by several ok deraadt


# 1.204 06-Aug-2010 deraadt

As part of a revamp of the PM1/GPE code, I write this diff to look for
unmanaged EN & STS bits in the PM1 register at interrupt time and report
them. As a side effect this splits the STS acknowledgement into two writes
(for power, and sleep) instead of one. The printf that is added (to spot
unmanaged STS bits) has not yet been triggered as far as we know.

Before the "write to PM1 registers at the right offset" diff went in, this
was not neccessary. But newer thinkpads do not have a working soft power
button without this diff.

We have no idea why.
ok mlarkin kettenis


# 1.203 05-Aug-2010 deraadt

PM1 power button events were generating 'fake events' to acpibtn PWRB,
but if there is no such device these were being tossed. Instead, arrange
for all power button handlers to flag a softc variable which is processed
by the thread; so that one place will know to request a powerdown (psignal
to init)
ok marco mlarkin


# 1.202 05-Aug-2010 deraadt

rename a variable which is annoyingly poorly named


# 1.201 05-Aug-2010 mlarkin

Write to the correct offset for various ACPI pmreg registers. Writing to
the correct register does properly clear the wake status bits and fixed
event bits, which allows some machines to have more than 1 suspend cycle.

bug spotted by deraadt@

tested by deraadt@ and myself on a half-dozen machines and others on
hackers@

ok deraadt@, marco@


# 1.200 05-Aug-2010 deraadt

Fix the power button. The pm1 register is 16 bits in size.
My mistake, a while back.
tested by krw


# 1.199 04-Aug-2010 kettenis

Print supported sleep states.

ok marco@, phessler@, deraadt@


# 1.198 03-Aug-2010 mlarkin

Use the proper flag for re-enabling certain hardware events (power btn,
sleep btn). The (incorrect) flag prevoiously used coincidentally had the
same value, so this shouldn't cause any different behavior than before.

tested on a variety of machines (i386, amd64, sp, mp) - no behavioral
change seen

ok deraadt@


# 1.197 28-Jul-2010 marco

Make all gasio failures look and behave the same.


# 1.196 28-Jul-2010 mlarkin

Delay a little longer on suspend before giving up. Fixes machines that
sometimes don't suspend because the sleep transition takes too long
(symptoms like failing to sleep but the backlight stays on).

ok deraadt@


# 1.195 27-Jul-2010 deraadt

remove XXX; EC can now be attached early when needed; ok jordan


# 1.194 27-Jul-2010 deraadt

more brackets feels good


# 1.193 27-Jul-2010 deraadt

XXX annotate a block in acpi_enter_sleep_state() that is pretty worrying


# 1.192 27-Jul-2010 deraadt

horrific indentation that nearly made me puke


# 1.191 27-Jul-2010 mlarkin

Set the sleeping indicator light on machines that support it, in case SMI
doesn't do it for us. Shows the 'moon' LED on older Thinkpads when
sleeping.

Tested on T43p, W500, T510 by me and on X61s by deraadt@.

ok deraadt@


# 1.190 27-Jul-2010 jordan

Early initialization of acpiec if ECDT table exists
Fixes hang when booting thinkpads while docked
ok deraadt


# 1.189 26-Jul-2010 deraadt

remove merge error which was commited


# 1.188 26-Jul-2010 pirofti

Add support for Sony ACPI hotkeys via a new driver: acpisony(4).

Currently it only works for the suspend button and tries to do right for
the brightness events, but I haven't found a Sony laptop that like to do
right so far.

In the future I want to make the brightness keys work on all Sony's and
also add support for the zoom hotkeys and whatever other funky keys I can
find on those things.

Okay deraadt@.


# 1.187 22-Jul-2010 deraadt

pretty large cleanup of SMALL_KERNEL


# 1.186 21-Jul-2010 deraadt

in the acpi GPE handler, late ack level interrupts not edge interrupts.
Duh.


# 1.185 21-Jul-2010 deraadt

Merge the edge and level gpe handlers into one (now possible since the
gpe structure tells us whether the gpe is edge or level)


# 1.184 21-Jul-2010 deraadt

delete #if 0 code we are not going to use; ok marco


# 1.183 20-Jul-2010 deraadt

make the mask code for acpi_enable_onegpe() match other similar code


# 1.182 20-Jul-2010 deraadt

Handle the acpi interrupt controller with proper edge/level handling,
wrapped in spltty to avoid racing against the interrupt controller.
Repair the gpe bit masking code while there, and do operations in the
right order
ok kettenis mlarkin, help from jordan at figuring out the order of
operations.


# 1.181 20-Jul-2010 matthew

When we're preparing to go to sleep, only call bufq_restart() if other
preparations failed.

ok kettenis@, thib@, deraadt@


# 1.180 19-Jul-2010 deraadt

permit compilation with ACPI_DEBUG


# 1.179 19-Jul-2010 deraadt

there is no need for a seperate dsdt_softc pointer; dsdt does not have a
softc but is just a part of the single acpi driver.
ok jordan


# 1.178 19-Jul-2010 deraadt

acpi_delay() is no longer needed; ok jordan


# 1.177 18-Jul-2010 mlarkin

Clear event status on resume, to avoid going back to sleep/reboot/shutdown
immediately after resume. Tested by a bunch of people on hackers@ on
various hardware. Fixes at least Toshiba NB200 and Gateway LT3103u.

ok marco


# 1.176 13-Jul-2010 deraadt

rename some thread related interfaces so they are logical


# 1.175 10-Jul-2010 jordan

Verify _ADR bdf is correct (some systems return 0xffff)
Works on Dell x9xx and Studio 1555


# 1.174 09-Jul-2010 jordan

Clear the noise


# 1.173 08-Jul-2010 jordan

Add mapping for ACPI device to PCI bdf (match autoconf tree)
Simplify resource parsing function to use buffer argument
Convert namespace linked lists to use queue macros
ok marco@, deraadt@


# 1.172 06-Jul-2010 deraadt

Use spltty() to lock downcalls from apm against the information being
modified by the acpi thread.
ok kettenis


# 1.171 06-Jul-2010 deraadt

Don't use a workq to do the suspend, because it races aginst the acpi
thread. Instead, just tell the acpi thread to do the suspend for us.
This makes apmd & zzz work correctly. While here, have acpithinkpad
attempt to post the event to apm before prompting the suspend itself.
ok kettenis marco mlarkin


# 1.170 05-Jul-2010 mlarkin

Reset register SCI_EN on resume. The ACPI spec clearly states we don't have
to do this, but many machines don't, or forget to, which leads to all sorts
of problems on resume (broken battery stats, overheating, failure to resume
more than once, etc).

tested by many, ok marco@, deraadt@


# 1.169 01-Jul-2010 thib

do bufq quiesce again, since vnd/the bufq_done call in
biodone has been fixed.


# 1.168 01-Jul-2010 jordan

Backout recent AML changes, breaks on R210 and others


# 1.167 01-Jul-2010 jordan

Simplified aml_parse_resource call, cleanup code
ok marco


# 1.166 01-Jul-2010 jordan

Fix subordinate bus # for multi-root PCI buses
ok marco, kettenis


# 1.165 30-Jun-2010 thib

Disable/partially backout the bufq quiesce changes as this
is causing havoc with vnds and release must be buildable.


# 1.164 29-Jun-2010 deraadt

Fix ramdisks (new code was placed inside a SMALL_KERNEL chunk)


# 1.163 29-Jun-2010 jordan

Add support for mapping ACPI to PCI devices
ok kettenis, deraadt


# 1.162 29-Jun-2010 kettenis

Call bufq_quiesce() on suspend to stop all I/O and bufq_restart on resume.
Makes suspend/resume work much better while doing disk I/O.

ok mlarkin@


# 1.161 29-Jun-2010 tedu

60 is a more reasonable guess for number of minutes in an hour than 100.
spotted by TeXitoi. ok deraadt


# 1.160 27-Jun-2010 jordan

Add _acpi_matchhids routine, precursor to pci device match


# 1.159 27-Jun-2010 mlarkin

Clear ACPI fixed event status on resume (power buttons/etc) so that some
machines don't immediately resume after suspending to S3.

Tested by kettenis@ and I on 5 or 6 machines, deraadt@ "yeah, looks good"


# 1.158 27-Jun-2010 jordan

Cleaned up acpi_maptable, adds to table list directly


# 1.157 07-Apr-2010 deraadt

If we have done a wsdisplay_suspend(), and ACPI decides it does not want to
suspend, we must roll back by calling wsdisplay_resume() or the virtual
console switching will be locked forever
ok oga


# 1.156 07-Apr-2010 kettenis

Make suspend/resume work on MP machines (running an MP kernel). Joint work
from mlarkin@ and me, with some amd64 fixes thrown in by deraadt@

ok marco@, deraadt@, pirofti@, mlarkin@


# 1.155 31-Mar-2010 kettenis

Bring /dev/apm support in line with apm(4). Make the suspend button send
a suspend request event to apmd(8) instead of suspending immediately. Also
keep track of whether /dev/apm and /dev/apmctl are currently open, such that
we can still suspend immediately if apmd(8) isn't running.

ok deraadt@, marco@, pirofti@, jsing@, oga@


# 1.154 30-Mar-2010 oga

Prevent the apmd/x races for good.

When we hit suspend time, go through all wsdisplays on the system. if
they are in mode MAPPED, but not MODE_DUMBFB then if possible do a full
vt switch to a !mapped vt, and prevent switching back until resume time.
This has to be called from MD code because this involves userland
running so that X can run the vt switch signal handler. This way, any
case where we are using the "poke registers from userland" model, we
will not be on the hardware when we go down, so the kernel can actually
handle thing properly.

Tested on several acpi laptops (by kettenis@ and ian@), x40 (me and
beck@ at LEAST) and zaurus (me). Maybe others, but if so I forgot who at
this time..

Idea from deraadt somewhere over the Faroe Islands (I thought of a
similar thing myself a while ago). Much prompting from him. Ok and
comments miod@


# 1.153 25-Mar-2010 oga

Fix & vs. && in the kqfilter code.

Based on a comparison with the apm code.

ok deraadt@, kettenis@


Revision tags: OPENBSD_4_7_BASE
# 1.152 26-Nov-2009 mlarkin

Visual (and on some machines, audio) feedback while the machine is
resuming via calls to aml _SST method. This also makes the "moon" LED on
thinkpads to blink during resume and cease when resume is completed.

ok pirofti@


# 1.151 26-Nov-2009 deraadt

Toggle into "cold" when we we disable interrupts, because tsleeping
in the AML interpreter and context switching into userland processes
tends to sometimes pop the ACPI trampoline page out of our address
space. Makes lots more machines work.
ok kettenis mlarkin


# 1.150 24-Nov-2009 jsg

in the resume path evaluate _WAK before dealing with device drivers
like the spec says, not after.

ok mlarkin@


# 1.149 24-Nov-2009 deraadt

abort suspends on SMP systems until that gets written; ok mlarkin


# 1.148 23-Nov-2009 mlarkin

pci should return the result of it's children's suspend/resume calls back
to its parent.

handle suspend failure case and unwind if devices have an activate function
that returns failure.

ok deraadt@, kettenis@


# 1.147 23-Nov-2009 mlarkin

acpi* devices need to call their children on suspend/resume.
ok deraadt@


# 1.146 23-Nov-2009 pirofti

Remove ACPI_SLEEP_ENABLED checks.

This enables by default the suspend/resume paths in the kernel.

Okay deraadt@.


# 1.145 23-Nov-2009 deraadt

knf and spacing that are super annoying


# 1.144 23-Nov-2009 mlarkin

Hook up Theo's new children suspend/resume function, as well as more ACPI
suspend/resume glue.

ok deraadt@


# 1.143 26-Oct-2009 deraadt

Do not do strncmp comparisons on dv_xname because longer device names which
look similar could arrive in the future. Instead, compare directly against
dv_cfdata->cf_driver->cd_name
Issue originally spotted by miod


# 1.142 26-Oct-2009 jordan

Verify checksum+address when loading ACPI tables.
Some systems had invalid entries in RSDT/XSDT.
ok marco@


# 1.141 23-Jul-2009 cnst

New aibs(4) driver for ASUSTeK AI Booster (ACPI ATK0110) hardware monitoring,
with sensor state support through limits provided by the ACPI.

Tested on several ASUS motherboards kindly networked by Sam Fourman Jr.

ok deraadt marco jordan


Revision tags: OPENBSD_4_6_BASE
# 1.140 03-Jun-2009 pirofti

Import acpi power resource driver. This is needed for suspend/resume logic.

Okay jordan@ and marco@.


# 1.139 03-Jun-2009 pirofti

Add acpivideo support. This adds brightness support for all laptops
except thinkpads, they will use the acpithinkpad driver. The driver is
also hooked into wsconsole. So brightness can be adjusted via:

$ wsconsctl display.brightness=<percentage>

This is very helpfull on some laptops that have a nasty bios and get two
steps instead of one when pressing the brightness button.

Tested on various dell, fujitsu, acer, samsung and other laptops.

Okay marco@, miod@. Suggestions from kettenis@.
Lots of reviews and help from miod@, thanks!


# 1.138 03-Jun-2009 jordan

Added ejectable bay for IDE cdroms
ok marco@


# 1.137 30-Apr-2009 marco

Oga changed apm ioctls and forgot to grep. Temporary work around by hitting
the sleep button when we get any request. This interface will chnage at
some point.
ok oga


# 1.136 19-Apr-2009 krw

Call acpi_sleep_walk() when powering down just like when entering
any other sleep state. This tells acpi wake devices to not wake up
the box until manual powering up resets their status. Makes my Dell
GX520 stay powered off in response to 'halt -p'.

In snaps for a while.

ok marco@ (several times) deraadt@


# 1.135 17-Apr-2009 pirofti

Add device ranking support for future sleep/wake developments.

okay marco@


# 1.134 11-Mar-2009 jordan

Cleanup.. use aml_evalinteger instead of aml_evalname
ok marco@


# 1.133 10-Mar-2009 jordan

Fix cut/paste error.. call _BFS not _PTS
ok marco@


Revision tags: OPENBSD_4_5_BASE
# 1.132 21-Feb-2009 marco

overzealous cut/paste; found by Vladimir Kirillov <proger@uaoug.org.ua>


# 1.131 20-Feb-2009 marco

Oops didn't mean for Debugger() to remain there.


# 1.130 19-Feb-2009 marco

suspend/resume bits so that we can develop this in tree. This is disabled.
code from mlarkin and me
help from art,toby,jordan and several others
ok jordan, go for it deraadt


# 1.129 10-Feb-2009 jordan

Fix null pointer for shutdown if acpi not enabled
ok marco@


# 1.128 04-Feb-2009 kettenis

Some systems that claim to be ACPI 2.0 apparently don't provide an XSDT
address. Make the code fall back on the RSDT on those systems.

From Joerg Niendorf <p3y80@internode.on.net>. Fixes PR 5986.


# 1.127 17-Jan-2009 krw

A couple of stray malloc/memset -> malloc(M_ZERO), a bit of knf and
lose a couple of unnecessary casts. No functional change.

Feedback from kettenis@

ok kettenis@ marco@


# 1.126 06-Nov-2008 marco

Collapse all sanity checks of hids into one function.

Originaly from jordan


Revision tags: OPENBSD_4_4_BASE
# 1.125 02-Jul-2008 fgsch

add acpivideo. for now almost an empty skel so others can work on
suspend/resume. eventually it will also manage output switching and
brightness where supported. prodded by marco@


# 1.124 11-Jun-2008 marco

Fix $OpenBSD$ for once and for all


# 1.123 08-Jun-2008 deraadt

aaa_dev was being set from an uninitialized variable; ok toby


# 1.122 07-Jun-2008 marco

Kill ACPIVERBOSE. It was never used and is worthless to boot.

comments jmc
ok toby


# 1.121 06-Jun-2008 marco

Shrink acpi some more for floppies and while here re-enable acpi on the
floppies. Fixes crazy sony vaio kettenis has.

tested by deraadt
ok deraadt jordan kettenis


# 1.120 01-Jun-2008 marco

Fix \n nit in SMALL_KERNEL.


# 1.119 01-Jun-2008 marco

Re-enable new parser. Lots of testing by various folks. Fits all media
now too.

ok kettenis deraadt


# 1.118 16-May-2008 dlg

back out jordans new parser.

it didnt have enough oks from the right people, it did break machines
that were previously working, and it bloated the RAMDISKS and install
media beyond the constraints of the media.

requested by deraadt@


# 1.117 14-May-2008 jordan

Adding new ACPI Parser code
Old guts of code still exists, needs to be torn out next
ok marco@
ok brad@


# 1.116 27-Apr-2008 jcs

add acpithinkpad(4), an acpi support driver for ibm/lenovo
thinkpads.

provides functionality that the bios did on older, non-acpi
thinkpads like respond to screen brightness keys and toggle the
bluetooth device on and off.

ok deraadt@ and marco@


# 1.115 24-Apr-2008 jsing

Introduce acpiasus(4), a driver for the ACPI based hotkeys found in many
ASUS laptops (including the ASUS EeePC) - largely based on NetBSD's
asus(4) driver. On the ASUS EeePC this allows us to enable/disable
wireless, change screen brightness and use the volume keys.

ok jsg@, weingart@


# 1.114 12-Apr-2008 kettenis

Get rid of acpi_s5 global variables; simply send SIGUSR2 instead of SUGUSR1
to tell init(8) to power down the machine.

ok krw@


Revision tags: OPENBSD_4_3_BASE
# 1.113 21-Jan-2008 jsg

Check against RESET_REG_SUP in FADT before doing the acpi reset dance
after all, not doing so appears to break more machines than it fixes.

ok marco@


# 1.112 05-Jan-2008 kettenis

Never attach more acpicpu(4) devices than cpu(4) devices. Always attach
the first acpicpu(4) device on non-MULTIPROCESSOR kernels, regardless of its
ACPI processor ID.

ok mikeb@


# 1.111 05-Dec-2007 deraadt

Hang acpi(4) below bios(4) instead of mainbus(4). This lets us move acpi(4)
before pcibios(4), and then.... if acpi(4) attaches, skip pcibios(4) since
it messes the machines up. Fixes claudio's HP dl320 G5, and almost assuredly
others.
ok kettenis


# 1.110 05-Dec-2007 deraadt

repair acpiopen/acpiclose and friends to handle the /dev/apmctl interface,
so that apmd(8) can actually work correctly against acpi(4), and thus,
even do the fancy -C/-A stuff; also tested by jmc


# 1.109 02-Dec-2007 fgsch

fix the trailing space for real now.


# 1.108 02-Dec-2007 fgsch

avoid trailing spaces on output; marco@ ok.


# 1.107 27-Nov-2007 kettenis

Fix off-by-one that made us attach acpicpu0 and acpicpu1 on GENERIC kernels.

ok marco@


# 1.106 25-Nov-2007 jsg

When available and ACPI is enabled use the ACPI reset method
to reboot. This is required for some quirky sis machines
and likely others to reboot.

Tested by many. 'commit early commit often' deraadt@


# 1.105 19-Nov-2007 deraadt

sigh, amd64 still uses that stupid acpi_enabled variable...


# 1.104 19-Nov-2007 deraadt

softc's come initialized to 0, you know...


# 1.103 19-Nov-2007 deraadt

store revision; from jsg


# 1.102 19-Nov-2007 kettenis

Initialize sc_facs earlier.

ok deraadt@, beck@, canacar@


# 1.101 16-Nov-2007 deraadt

fix the bus_space #define nightmare, so that amd64 and i386 are much more
uniform. as a result shared code like acpi needs less #ifdef's
ok marco kettenis


# 1.100 15-Nov-2007 deraadt

remove the #ifdef ACPI_ENABLE. the EC codepath is either safe enough (and
will be improved), or it is unsafe because AML assumes it must be there
ok almost everyone


# 1.99 15-Nov-2007 mikeb

Make acpicpu(4) attach to the existing CPUs only.

Problem reported by form@. Fix idea by kettenis@.
Lots of help from deraadt@. Tested by deraadt@, form@ and me.

ok deraadt marco


# 1.98 14-Nov-2007 deraadt

spacing


# 1.97 12-Nov-2007 deraadt

correct : vs , in attach-time messages


# 1.96 06-Nov-2007 deraadt

all ACPI_ENABLE kernels need acpi_interrupt()


# 1.95 06-Nov-2007 deraadt

move acpi_read_pmreg() and acpi_write_pmreg() out of SMALL_KERNEL so
that acpi ramdisks can compile (obvious solution)


# 1.94 05-Nov-2007 canacar

Establish the acpi interrupt earlier. Fixes problems with some
GENERIC.MP kernels not getting acpi interrupts. This is a
workaround until the real issue with late establishment is fixed.
ok weingart@, beck@, gwk@


# 1.93 03-Nov-2007 jordan

Added support for displaying wakeup devices
ok beck@,weingart@,gwk@


# 1.92 11-Oct-2007 kettenis

When walking the tree yo find _PRT methods, check whether the device is
actually enabled and functioning. Similar to what we do for _INI.

ok weingart@, beck@


# 1.91 08-Oct-2007 krw

More simple memset(,0,) -> M_ZERO changes. In this batch move to
size(*p) as the first malloc() parameter where p is declared locally
and thus easy to check. Add M_ZERO to gpe_table allocation in acpi.c
even though there is no obvious bzero or memset nearby.


# 1.90 13-Sep-2007 mk

Remove unnecessary memset() (see aml_evalnode(9)) and a bit of KNF.

ok toby


# 1.89 13-Sep-2007 weingart

Implement ACPI 6.5.1 spec tree walk for _STA and _INI.
Fixes mk's laptop. No regressions so far (thank you to the
testers).

ok gwk@, mk@, marco@


Revision tags: OPENBSD_4_2_BASE
# 1.88 17-Apr-2007 mk

Now that acpidock no longer hangs machines during boot (at least
according to the reports I've been getting) it should be safe to enable
it again, so remove the #if 0 stuff around it.

ok marco


# 1.87 11-Apr-2007 jordan

Added changes for C-State
ok marco@


# 1.86 26-Mar-2007 marco

Calculate minutes left more accurately.

From giovanni <qgiovanni@gmail.com>


Revision tags: OPENBSD_4_1_BASE
# 1.85 03-Mar-2007 mk

Stop looking for acpidock until we fix that stuff that breaks all pre-60
series ThinkPads with acpidock(4) enabled.


# 1.84 22-Feb-2007 marco

We were getting no interrupts in .MP. There are 2 fixes; one in
acpi_machdep.c and move the interrupt establishment down after ioapic
has been initialized.

ok jordan


# 1.83 21-Feb-2007 kettenis

Make an acpi-enabled GENERIC.MP work on systems that only support PIC mode.

ok marco@


# 1.82 19-Feb-2007 jordan

Added acpi_map_address function for mapping GAS/IO registers
ok marco@


# 1.81 18-Feb-2007 jordan

Added dynamic perfect hash for opcodes
ok marco@


# 1.80 18-Feb-2007 marco

Shave off another K pre compression.
Shuffle SMALL_KERNEL functions around so that we only have a single #ifndef


# 1.79 17-Feb-2007 marco

Add SMALL_KERNEL throught acpi to make it fit on boot media.
ok deraadt


# 1.78 13-Feb-2007 marco

Add smarter _INI code. This helps with some dissapearing devices.

ok kettenis jordan


# 1.77 31-Jan-2007 gwk

Introduce acpi_hasprocfvs thats "ACPI Has Processor Frequency and Voltage
Scalling" a global flag set by acicpu attach to indicate that ACPI is
handling fvs through the _PSS objects. This will fix disappearing
EST/powernow when booting kernels with acpi enabled, as acpicpu isnt
compiled into GENERIC yet. Also in cases where acpicpu is built in and
legacy EST/powernow work but acpi is missing the _PSS object, the legacy
routines will be used.

tested by dim@, and Nick Nauwelaerts <nick AT nauwelaerts.net>
ok marco@, dim@


# 1.76 22-Jan-2007 mk

Actually look for docking stations by looking for _DCK nodes.

ok marco


# 1.75 02-Jan-2007 marco

On some systems EC must be attached as the first device. Move EC from
generic _HID devices to its own routine to force first attach. Bug found
and debugged by joris@

Code mostly written by joris@


# 1.74 26-Dec-2006 marco

Add polling to devices that require it such as acpibat and acpitz.
Use the same tsleep/wakeup mechanism as the interrupt code to keep it non-
concurrent.


# 1.73 21-Dec-2006 deraadt

even more knf love


# 1.72 21-Dec-2006 marco

Remove some leftover acpiec gpe/interrupt code.


# 1.71 20-Dec-2006 deraadt

oops, name already used


# 1.70 20-Dec-2006 deraadt

rename __acpi_enable_gpe(); ok marco


# 1.69 20-Dec-2006 deraadt

acpi_unmap_pmregs() is not used, ok marco


# 1.68 19-Dec-2006 deraadt

simplify some low-level methods substantially (shrinking code); ok marco jordan


# 1.67 06-Dec-2006 mk

malloc(..., M_NOWAIT) with no NULL check is bad. If allocation fails,
complain, clean up, and bail out of attach.

input and ok gwk


# 1.66 28-Nov-2006 jordan

Fixed clearing arg for acpi_init_pic
ok kettenis@


# 1.65 27-Nov-2006 kettenis

Don't spam dmesg.


# 1.64 27-Nov-2006 jordan

Fixes for acpiprt routing
Post-parse fixup for namerefs
ok kettenis@


# 1.63 25-Nov-2006 marco

Don't handle acpiev events inside the gpe handler since this causes deadlocks.

ok jordan@


# 1.62 15-Nov-2006 kettenis

Hook up acpi apic interrupt routing support to generic acpi code.

ok jordan@


# 1.61 02-Nov-2006 marco

Kill acpiec attach function and add its functionality to the generic _HID
device attach

prodded and ok jordan


# 1.60 02-Nov-2006 marco

Fix comments to reflect reality.


# 1.59 19-Oct-2006 marco

Replace ACPI ioctls with APM ioctls. This makes ACPI talk the same protocol
as APM therefore userspace can now retrieve power information in the same
fashion as all other apm arches (zaurus, macppc, i386 etc).

To use force the use of acpi device like so: apmd -f /dev/acpi

ok jordan@


# 1.58 19-Oct-2006 jordan

Fixed GPE debug levels
Fixed ref counter on temporary bufferfields
DSDT parsing fully working on IBM Thinkpad
ok marco@


# 1.57 15-Oct-2006 jordan

Removed old GPE handler code remnants
ok marco@


# 1.56 12-Oct-2006 jordan

Massive ACPI changes
Fixed ACPI leaks
new ACPI API
ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.55 30-Jun-2006 gwk

KNF, No functional change,
"go at it" marco@


# 1.54 30-Jun-2006 jordan

Support for calling Embedded Controller via new GPE interface
ok marco@


# 1.53 30-Jun-2006 jordan

Added new code for GPE handling
Also works with ACPIEC; need to fixup acpiec.c to remove original interrupt handler
ok marco@


# 1.52 30-Jun-2006 jordan

Added offset argument for acpi read/write pmreg routines (new GPE code)
ok marco@


# 1.51 31-May-2006 todd

do not call acpi_powerdown() unless acpi has successfully initialized
ok gwk@ canacar@


# 1.50 29-May-2006 canacar

Add embedded controller support.
ok marco@ deraadt@ testing & ok gwk@


# 1.49 19-May-2006 canacar

Minimal thermal zone driver. Monitors thermal zone temperature,
shuts down the system if the 'critical temperature' is reached.
ok marco@


# 1.48 11-Apr-2006 marco

KNF.


# 1.47 11-Apr-2006 gwk

Add acpi_resume(struct acpi_softc *sc), which calls some AML methods,
detected at attach time, currently unused.

ok marco@


# 1.46 11-Apr-2006 gwk

Discover some ACPI sleep related methods, and call them at the
appropriate time in acpi_enter_sleep_state().

ok marco@


Revision tags: OPENBSD_3_9_BASE
# 1.45 26-Feb-2006 marco

Add acpicpu as a device.

ok jordan@


# 1.44 23-Feb-2006 marco

remove dead function.

ok jordan@


# 1.43 22-Feb-2006 marco

Print out all tables it found during autoconf. We'll need this data in
the future.


# 1.42 22-Feb-2006 marco

Make dmesg less verbose.

ok jordan.


# 1.41 22-Feb-2006 jordan

Added parsing of SSDT tables
ok marco@


# 1.40 22-Feb-2006 jordan

Added new aml_register to notify by device id
Moved powerdown code to button device
ok marco@


# 1.39 21-Feb-2006 marco

Readjust debug levels that crept in too low over the last few days.
KNF


# 1.38 21-Feb-2006 marco

Rig the power button so that the machine shuts down correctly.


# 1.37 20-Feb-2006 marco

Add acpibtn device. Remains disabled for now.


# 1.36 19-Feb-2006 jordan

Added code for aml_notify GPE notification callback
ok marco@


# 1.35 19-Feb-2006 grange

Fix acpi_enter_sleep_state() by replacing current ugly hack
with mostly correct sleeping state transition code.

ok brad@ marco@


# 1.34 19-Feb-2006 marco

jordan:
Enable interrupts.
Added peripheral code to make this work.

marco:
Replace softintr and other types of interrupts with a handler with context.
Fixed interrupt magic for GPEs.
Some KNF

This is work done by jordan and I but has to be a single commit.


# 1.33 17-Feb-2006 marco

Remove earlier introduced delays. We really only need them during autconf.

ok jordan@


# 1.32 17-Feb-2006 marco

Add a generic delay function as per the ACPI spec.

from jordan@


# 1.31 17-Feb-2006 marco

Use tsleep instead of delay when !cold.


# 1.30 17-Feb-2006 marco

Argh, we need delays between reads/writes. The backend reads (i2c most likely)
are so slow that values come back as 0 sometimes. We have to do this
despite the spec saying we shouldnt have to. Guess having a faster
parser than others can hurt sometimes.

Idea from jordan.


# 1.29 17-Feb-2006 jordan

Fixed code for handling _INI
Rearranged debug statements
ok marco@


# 1.28 16-Feb-2006 jordan

Added stub for calling _INI
Fixed aml_dowhile for NULL return
ok marco@


# 1.27 16-Feb-2006 jordan

Fixed parser/evaluator to retrieve battery info+state
Cleanup of parser
Dell laptops return battery status
ok marco@


# 1.26 06-Feb-2006 brad

fix a typo so this will compile on amd64.


# 1.25 04-Feb-2006 marco

When ACPI_ENABLED wasn't set but the devices were it had an unknown variable.

Pointed out by ian@.


# 1.24 03-Feb-2006 jordan

Rewrote aml parser yet again...
Fixed bus_space_map for i386/amd64
Added _BST and _BIF to acpibat to get status/info
ok marco@


# 1.23 20-Jan-2006 jordan

More evaluator changes
Removed old evaluator code
Fixed _HID value
ok marco@


# 1.22 19-Jan-2006 jordan

Added support for read/write of PCI Config space
Help jason@
ok marco@


# 1.21 18-Jan-2006 jordan

Added new evaluation routines
Added helper methods for buffer/field evaluation
Fixed broken indentation on dsdt.c

ok marco@


# 1.20 17-Jan-2006 jordan

Major overhaul of the aml parser. Evaluater temporarily disabled.

ok marco@


# 1.19 06-Jan-2006 grange

Check for ACPI control availability only if ACPI_ENABLE defined.


# 1.18 06-Jan-2006 grange

Informative error messages on attach, prettier dmesg.


# 1.17 06-Jan-2006 grange

printf -> dnprintf conversion for debug messages.


# 1.16 05-Jan-2006 grange

Find SLP_TYPx values and store them for future use in
acpi_enter_sleep_state().


# 1.15 05-Jan-2006 grange

Random KNF and cleanup


# 1.14 05-Jan-2006 grange

Provide symbolic names for the acpi sleeping states and use it.


# 1.13 28-Dec-2005 marco

Moved DEVNAME to a acpidev.h

ok jordan@


# 1.12 18-Dec-2005 sturm

add back comment about wrong code

ok marco


# 1.11 16-Dec-2005 marco

Remove test for kbd_reset. Prompted deraadt@, ok jordan@


# 1.10 16-Dec-2005 jordan

Added hooks for powerdown/power button press


# 1.9 16-Dec-2005 jordan

Cleaned up common power management register i/o
Added enter sleep state ioctl
Hooked power button interrupt
ok marco@


# 1.8 16-Dec-2005 marco

Remove autoconf chatter


# 1.7 16-Dec-2005 jordan

Moved DSDT AML parsing into parent ACPI driver
ok marco@


# 1.6 14-Dec-2005 marco

Add AC device glue.


# 1.5 14-Dec-2005 marco

Adjust debug printing levels to something more sane.


# 1.4 13-Dec-2005 marco

Add glue to attach devices to apci.


# 1.3 07-Dec-2005 marco

KNF


Revision tags: OPENBSD_3_8_BASE
# 1.2 10-Jul-2005 grange

Spacing cleanup.


# 1.1 02-Jun-2005 tholo

Start on a basic ACPI framework -- does not do much more than read out the
ACPI tables into kernel memory and attach ACPI and HPET timers currently.

In order to test this code, enabling the devices in GENERIC as well as
the ACPI_ENABLE option is needed. This code does not do any thermal
control yet, so this should be done with care depending on the platform.

In the tree so more people can contribute to making this more fully
featured.

Ok niklas@ grange@ tedu@


# 1.359 30-Aug-2018 kettenis

Move softbutton check before the check that skips attaching certain acpi
devices. Fixes regression caused by matching on _CID in addition to
matching on _HID.

ok matthieu@, mlarkin@


# 1.358 25-Aug-2018 kettenis

Match ACPI devices based on _CID if no match for _HID is found.

ok mpi@


# 1.357 19-Aug-2018 kettenis

Add support for multiple PCI segments. Only really implemented for arm64
for now as amd64/i386 firmware still caters for legacy OSes that only
support a single PCI segment.

ok patrick@


# 1.356 03-Aug-2018 kettenis

Let ahci(4) match on _CLS instead of _HID when attaching at acpi(4). Avoids
having to add many more _HID entries to the match table.

ok deraadt@, mlarkin@


# 1.355 10-Jul-2018 kettenis

Make legacy interrupts work in acpipci(4).

ok patrick@


# 1.354 05-Jul-2018 kettenis

Add acpipci(4), a driver that supports generic ECAM-compatible PCI host
bridges based on information provided by ACPI.

ok mlarkin@


# 1.353 04-Jul-2018 kettenis

Properly pass around the PCI "chipset tag" in acpi(4) and refactor
acpimcfg(4) to call an MD initialization functions that sets up a tag for
PCI ECAM.

ok guenther@, mlarkin@, krw@


# 1.352 01-Jul-2018 kettenis

Move acpi(4) attach glue into acpi_machdep.c.

ok guenther@, deraadt@


# 1.351 01-Jul-2018 kettenis

Add support for _DSD Device Properties.

ok mlarkin@


# 1.350 30-Jun-2018 kettenis

Add intr_enable() function, intended for MI use to amd64 and i386 and use
this in the acpi(4) suspend/resume code paths.

ok deraadt@


# 1.349 29-Jun-2018 kettenis

Use <stdint.h> types.

ok mpi@, jung@, krw@, deraadt@


# 1.348 29-Jun-2018 mlarkin

ACPI: Allow (with warning) GPE handler reassignment, instead of returning
a failure code.

ok kettenis


# 1.347 26-Jun-2018 mlarkin

knf


# 1.346 26-Jun-2018 kettenis

Let acpi(4) pass down the bus dma tag.

ok deraadt@


# 1.345 25-Jun-2018 kettenis

Reorganize acpi(4) code a bit in preparation for upcoming arm64 support.

ok deraadt@


# 1.344 20-May-2018 kettenis

Wake up the acpi thread after scheduling a gpio event.

ok mpi@


# 1.343 17-May-2018 kettenis

Sprinkle some #ifndef SMALL_KERNEL such that RAMDISK kernels cuild again.

pointed out by stsp@


# 1.342 17-May-2018 kettenis

Implement GenericSerialBus OpRegion support.

ok mlarkin@


# 1.341 27-Mar-2018 kettenis

Add acpicmos(4), a driver that implements SystemCMOS OperationRegion
access support. This fixes machines where the AML doesn't check whether
support for this OperationRegion type has been registered by the OS.

ok mlarkin@


Revision tags: OPENBSD_6_3_BASE
# 1.340 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.339 10-Feb-2018 deraadt

Syncronize filesystems to disk when suspending. Each mountpoint's vnodes
are pushed to disk. Dangling vnodes (unlinked files still in use) and
vnodes undergoing change by long-running syscalls are identified -- and
such filesystems are marked dirty on-disk while we are suspended (in case
power is lost, a fsck will be required). Filesystems without dangling or
busy vnodes are marked clean, resulting in faster boots following
"battery died" circumstances.
Tested by numerous developers, thanks for the feedback.


# 1.338 08-Feb-2018 deraadt

After flushing disks, try again to release all memory. We don't need to
save dirty memory to the hibernate space.


# 1.337 08-Feb-2018 deraadt

Toss all releaseable memory, because fragmentation can get in the way
of allocating the hibernate playpen.


# 1.336 08-Feb-2018 deraadt

sr_quiesce() is a new approach for ensuring that softraid drains
output to the disks.

This is part of a larger suspend/resume filesystem-safety diff, which
has been worked on for a couple of months already. Tests by job, krw,
beck, benno, and others. Sometimes even by snapshot users...


# 1.335 29-Nov-2017 kettenis

Add support for GPIO-signaled events.

ok mlarkin@


# 1.334 16-Nov-2017 jcs

Add PCI attachment for dwiic(4) needed by Intel 100 Series machines

ok kettenis


# 1.333 14-Nov-2017 kettenis

Add a few more PNP IDs to the skip lists.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.332 17-Aug-2017 stsp

Make dwiic(4) attach its ACPI parent dependencies.
This change makes the Asus E200HA keyboard work.

Original analysis and patch by Cesare Gargano
ok kettenis@


# 1.331 09-Aug-2017 dcoppa

Add "PNP0303" (8042 PS/2 Controller) to acpi_isa_hids[]

ok kettenis@


# 1.330 22-Jul-2017 jcs

acpibat: look for _BIX first before falling back on _BIF

ACPI 4.0 deprecated _BIF for battery status so some newer machines
are exporting _BIX which extends _BIF's package with a few extra
fields. When using _BIX, export the new discharge cycle count as a
sensor.

feedback from kettenis and anton
ok deraadt


# 1.329 20-Jul-2017 kettenis

Start a list of isa(4) device names such that we can avoid printing them
as "not configured".

ok mikeb@, millert@, deraadt@


# 1.328 08-Apr-2017 deraadt

Sensors are run as callbacks inside tasks. During suspend a sensor
could be running inside a driver that will be force-detached, or due
to tsleep end up disrupting the softstate/hardstate contract. At
suspend time, quisce all these callbacks by waiting for completion.
This issue has never been observed for real, but may be implicated in
suspend/resume failures.
ok kettenis guenther mlarkin


# 1.327 08-Apr-2017 deraadt

Two prototypes for acpi_maptable; merge to one place


Revision tags: OPENBSD_6_1_BASE
# 1.326 27-Mar-2017 deraadt

Now that hibernate_alloc() only has clean success/failure, don't
need to call hibernate_free() to clean up a partial mess.
ok mlarkin kettenis


# 1.325 13-Mar-2017 deraadt

allow ramdisks to build; ok jcs


# 1.324 12-Mar-2017 jcs

try to attach acpisbs first and if successful, don't attach acpibat


# 1.323 02-Mar-2017 natano

Add a new sysctl machdep.lidaction. The sysctl works as follows:

machdep.lidaction=0 # do nothing
machdep.lidaction=1 # suspend
machdep.lidaction=2 # hibernate

lidsuspend is just an alias for lidaction, so if you change one, the
other one will have the same value. The plan is to remove
machdep.lidsuspend eventually when people have upgraded their
/ets/sysctl.conf.

discussed with deraadt, who came up with the new MIB name
no objections mlarkin
ok stsp halex jcs


# 1.322 28-Feb-2017 natano

Untangle abstract sleep modes from ACPI sleep states. This paves the way
for further work in the area.

idea and ok deraadt
ok mlarkin


# 1.321 22-Feb-2017 jcs

add acpisbs, an acpi smart battery subsystem driver reading data
over smbus

currently disabled because it conflicts with acpibat

ok deraadt, kettenis


# 1.320 14-Jan-2017 kettenis

Revert aml_rdpciaddr changes; breaks several machines that were working
before.


# 1.319 11-Jan-2017 jsg

Increase the size of the buffers used for _HID and _CID strings to be
able to fit "Hyper_V_Gen_Counter_V1".

ok mlarkin@ deraadt@


# 1.318 08-Jan-2017 kettenis

Make aml_rdpciaddr() work for for devices behind bridges. The code now
uses the mapping between AML nodes and PCI devices that we establish.
Because _INIT methods may end up calling aml_rdpciaddr(), make sure we
create that mapping early. Also handle devices that aren't actually present.
These devices are now included in the mapping and reads will return an
all-ones patterm whereas writes are a no-op..

ok mlarkin@


# 1.317 25-Oct-2016 pirofti

Add the resource index to aml_parse_resource() callback function.

This is needed by an upcoming acpiec commit that handles machines breaking
the current ACPI specifications.

Change suggested by and ok kettenis@, guenther@


# 1.316 18-Sep-2016 guenther

If the APIC table (MADT) isn't present then ci_acpi_proc_id won't be set
when acpicpu tries to attach, leading to a NULL dereference. The ACPI
processor object on the Dells where this was reported is bogus and useless
anyway so apply the same test when deciding whether to attach to processor
objects as acpicpu would when matching them to cpus.

Problem reported by Eivind Eide (xenofil (at) gmail.com) and
Johan Huldtgren (johan+openbsd-bugs (at) huldtgren.com).
Testing by Eivind Eide.
ok kettenis@ mlarkin@


# 1.315 03-Sep-2016 naddy

Write the system time back to the RTC every 30 minutes.
This fixes the problem that long-running machines which were not
shut down properly would reboot with a badly offset system time.

hints and ok kettenis@


# 1.314 31-Aug-2016 mlarkin

Don't put a device in the wake device list if _STA says it's not there.

Fixes at least one machine that woke up from sleep / poweroff immediately.

ok kettenis@, tested by kettenis and martijn@


# 1.313 28-Jul-2016 kettenis

Store the acpi processor ID/UID in struct cpu_info, and use it to attach
acpicpu(4) drivers to the right cpu(4).

ok mlarkin@, guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.312 10-Jun-2016 kettenis

The power management timer has been made optional in ACPI 5.0A, so don't
attempt to attach acpitimer(4) if the timer isn't present.

Pointed out by jcs@

ok jcs@


# 1.311 08-May-2016 kettenis

Make sure devices listed by a device's _DEP method are attached before
we attach the device itself. The _DEP methode, introduced in ACPI 5.0,
is a hint that the device in question depends on OpRegion support from
the devices listed by _DEP, so we have to order them the proper way.
To prevent us from attaching those devices again when we encounter them
later walking down the device tree, keep track of our attempts to
attach a device driver to them using aflag in the aml_node structure.

ok guenther@


# 1.310 25-Apr-2016 pirofti

We attach acpihpet(4) and acpiec(4) without using the HID, so do not
report it in the dmesg.

OK deraadt@, mlarkin@.


# 1.309 02-Apr-2016 kettenis

Reduce dmesg spam by knocking out some acpi hardware IDs that don't need a
driver.

ok mpi@


# 1.308 29-Mar-2016 kettenis

Check if a device is present (using _STA) in acpi_foundhid. This prevents
us from reporting devices as "not configured" that aren't actually present,
and allows us to remove duplicated code from several drivers.

ok deraadt@, ok jsg@


# 1.307 29-Mar-2016 kettenis

Implement a cleaner way to prevent acpivout(4) from attaching if we have
a platform-specific method of controlling the backlight.

ok millert@, mlarkin@, jung@


# 1.306 28-Mar-2016 kettenis

Adding each and every new acpi device driver to acpi_foundhid() is
gettinga bit of the burden. And it really isn't how our autoconf
framework is supposed to work for busses that can be enumerated. All
the drivers already check for a matching _HID in their attach
function. So we can just drop the checks in acpi_foundhid() and let
the drivers sort themselves out.

Print a "not configured" line for devices for which we don't attach adriver.
This may be a bit much as there are devices for which we will not have a
driver. This will be removed if it causes too much dmesg spam.

ok deraadt@, mlarkin@


Revision tags: OPENBSD_5_9_BASE
# 1.305 17-Jan-2016 jsg

Take acpi_foundhid() outside of #ifndef SMALL_KERNEL so the ramdisk
kernel can find and attach dwiic(4) and sdhc(4).


# 1.304 17-Jan-2016 jsg

remove an unused global variable


# 1.303 14-Jan-2016 kettenis

Attach dwiic(4) on the Intel Bay Trail i2c controllers as well.


# 1.302 12-Jan-2016 kettenis

Improve emulation of PM registers on Hardware-redcued ACPI by emulating
SPL_TYPx, SLP_EN and WAK_STS using the new SLEEP_STATUS_REG and
SLEEP_CONTROL_REG registers. Makes the transition into S5 (powerdown) work
on the ASUS X205TA.

ok deraadt@, mlarkin@


# 1.301 12-Jan-2016 jcs

Add dwiic, a driver for the Synopsys DesignWare i2c controller found
on the Samsung ATIV Book 9 laptop. This initial version only
supports ACPI config/attachment.

Add ihidev, a HID-over-i2c driver largely based on uhidev. dwiic
handles attaching ihidev devices found in ACPI.

Add ims, a HID-over-i2c mouse/trackpad driver to get basic cursor
and button functionality from HID-compliant i2c trackpads.

ok kettenis deraadt


# 1.300 11-Jan-2016 kettenis

Make sdhc(4) attach to hardware IDs "80860F14" and "PNP0FFF".


# 1.299 09-Jan-2016 kettenis

Initial support for Hardware-Reduced ACPI. This skips the legacy mode checks
by emulating the PM_CNT register such that SCI_EN is always enabled. It also
skip attaching acpitimer(4) since the relevant registers are no longer there.

ok deraadt@, mlarkin@


# 1.298 06-Jan-2016 kettenis

Follow the spec and check the SCI_EN bit to see if ACPI has already
been enabled and don't bother enabling it again if it is. This should fix
systems that don't implement legacy mode and therefore set ACPI_ENABLE and
ACPI_DISABLE to zero.

ok mlarkin@, yasuoka@


# 1.297 23-Nov-2015 reyk

Back out the CRITICAL change, turn it back from 10% to 25%. Some
people with old/broken batteries didn't like this change, and the
underlying heuristics are wrong.


# 1.296 22-Nov-2015 reyk

The "CRITICAL" APM battery level, that is exported/faked by the ACPI
stack, is currently set to 25%. With 25% battery left, a typical
modern laptop will have at least one hour runtime left. It is an
arbitrary value, and other APM power layers in the kernel set CRITICAL
to something like 5% or 10%, so set it to 10% for now.

OK mlarkin@


# 1.295 28-Sep-2015 deraadt

In low-level suspend routines, set cold=2. In tsleep(), use this to
spit out a ddb trace to console. This should allow us to find suspend
or resume routines which break the rules. It depends on the console
output function being non-sleeping.... but that's another codepath which
should try to be safe when cold is set.
ok kettenis


# 1.294 06-Sep-2015 deraadt

clarify malloc arguments (base + length...)


# 1.293 28-Aug-2015 deraadt

fairly simple sizes for free(); ok tedu


# 1.292 20-Aug-2015 kettenis

Respect the access size when reading or writing to pci config space.
This version also makes sure writes are properly aligned, preventing
the panics seen with the previous incarnation of this change.

Fixes battery status passthrough in vmware, brightness keys on my x220
and who knows what else.

ok mlarkin@, deraadt@


Revision tags: OPENBSD_5_8_BASE
# 1.291 04-Aug-2015 deraadt

If we are setting ACPI_PM1_BM_RLD do it only once, and remember to
repeat this action at resume time.
ok mlarkin guenther


# 1.290 29-Jul-2015 deraadt

Something subtle in previous revision is locking up suspend or booting
on some laptops. Mailing lists threads mention x220 and Asus machines.
Reverting for now.


# 1.289 21-Jul-2015 kettenis

iRespect the access size when reading or writing to pci config space.
Fixes battery status passthrough in vmware and who knows what else.

Tested by many.

ok mlarkin@, deraadt@


# 1.288 17-Jul-2015 mlarkin

fix comments


# 1.287 26-Mar-2015 jsg

Use the 0x10 extended capability to determine if the trackstick
buttons are wired to the trackpad and need to be re-routed to the
trackstick. Without this change the buttons on 2015 thinkpads
get picked up as extended buttons that show up as scroll up/down.

Remove the X1 Carbon 2015 (LEN0048) and X250 (LEN0046) from the
top button area/soft buttons quirks list. Also avoid using the
quirk list entirely if the capability bit is set.

Based on work from jcs@ with feedback from mpi@
ok mpi@ jcs@


# 1.286 20-Mar-2015 kettenis

The ACPI standard says that a PCI function number of FFFF can be used to refer
to all functions on a device. This makes no sense in the context of accessing
PCI config space. Nevertheless stsp@ has a machine with AML that tries to
do config space reads from a device where _ADR encodes function number FFFF.
This is almost certainly buggy AML, but since Linux (and presumably Windows)
boot just fine on this machine we need to work around this. Linux effectively
treats function FFFF as function 7, which isn't present on this machine.
Instead we simulate a read from a non-existent device by returning a bit
pattern of all ones. Writes to function FFFF will still panic.

ok mlarkin@


# 1.285 16-Mar-2015 deraadt

Reduce use of SMALL_KERNEL in acpi, bringing acpiec driver and more gpe
handling into RAMDISK. This is now possible because the install media
has ample room. The goal is to reduce special cases where we may be
skipping (unknown) important operations...
ok mlarkin kettenis


# 1.284 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.283 07-Feb-2015 deraadt

New framework that allows hibernate to pass in entropy from it's fresh
boot.
ok mlarkin


# 1.282 06-Feb-2015 mlarkin

Fix a hibernate crash on some machines due to unmapping a page that
may not have been mapped previously (in the failure to hibernate case).

Also ensure that the lowmem ptp is mapped in all cases (not just MP).

ok kettenis


# 1.281 17-Jan-2015 deraadt

resume_randomness() before spinning up other cpus...


# 1.280 15-Jan-2015 jsg

Define a new wscons mouse type for Synaptics clickpad devices that lack
physical buttons. This will be used if the acpi pnpid for the mouse
matches a list provided by Synaptics found in Linux.

Combined with the xenocara changes this will remove the need for an xorg
config file for the x240/t440/t540 generation of thinkpads.

ok matthieu@ shadchin@ kettenis@


# 1.279 11-Jan-2015 kettenis

Tolerate checksum errors in ACPI tables (but complain about them). Apparently
there are machines out there with broken BIOSen with such checksum errors in
thier RSDT tables. Windows runs fine on these machines, so it probably does
not check. Linux ignores checksum errors as well (and prints a warning).

ok deraadt@, krw@


# 1.278 18-Dec-2014 deraadt

suspend and resume the RNG (at what looks like the right spots)
ok reyk djm


# 1.277 09-Dec-2014 doug

Replace some malloc(n*size,...) calls with mallocarray().

ok tedu@ deraadt@


# 1.276 06-Dec-2014 mpi

If "machdep.lidsuspend" is not set, do not go back to sleep if the lid
is still closed when resuming. This allows docked laptop to be resumed
without opening them.

ok mlarkin@


# 1.275 23-Nov-2014 mlarkin

If a machine has no _LIDs, then none can be open. This diff fixes a problem
where machines without any _LIDs were going back to sleep after hibernate
due to the recent suspend-if-lid-closed-on-resume diff. Noticed on qemu,
which doesn't have any _LIDs.

discussed with deraadt


# 1.274 18-Nov-2014 krw

Nuke yet more obvious #include duplications.

ok miod@


# 1.273 08-Nov-2014 mlarkin

If resuming from sleep (zzz/ZZZ) and the lid is still closed, go back to
sleep. This prevents accidental lid flex or slight opening in a backpack
from waking the machine up and leaving it resumed (powered on).

ok deraadt@


# 1.272 26-Sep-2014 kettenis

Rework piglet and pig allocation. Currently the piglet gets allocated
deep down in the suspend path, where it is really hard to recover from
allocation failure. So allocate the piglet early on in the suspend path.
Also change the piglet and piglet allocation functions to use km_alloc(9)
instead of doing pmemrange magic. This removes a bunch of code which, in the
case of the piglet allocation, is broken since it results in a NULL pointer
dereference. Also switch the piglet allocation to not wait. If we can't
allocate 16MB of phys contig memory on a halfway modern machine we're almost
certainly under a lot of memory pressure and we're better off not trying to
hibernate anyway.

ok mlarkin@


# 1.271 25-Sep-2014 kettenis

Bring back rev 1.249. Now that mp_setperf() has been fixed, it should be
safe to restore perflevel when resuming. Should fix the issues people
see with the processor running at full speed heating up the machine while
the apm command reports that the CPUs are running at a low speed.

ok dcoppa@, deraadt@


# 1.270 23-Sep-2014 kettenis

Release the acpi lock around calls to wsdisplay_suspend() and
wsdisplay_resume(). The X server may adjust the display brightness just
before/after the VT switch when suspending/resuming. In fact, it may
already be blocked adjusting the display brightness for some reason
unrelated to the suspend request.

The VT switch request will eventually time out, but the delay is annoying
and the failed VT switch will lead to problems after resume.

Needed for a future update of the xf86-video-intel driver.

ok mlarkin@


# 1.269 19-Sep-2014 kettenis

Use config_suspend_all(9).


# 1.268 08-Sep-2014 kettenis

Clear the PME Status bit on PCI devices in response to Device Wake events.
Fixes the interrupt storm on the 2009 Mac mini with WOL enabled on its
nfe(4) interface.

ok mlarkin@


Revision tags: OPENBSD_5_6_BASE
# 1.267 20-Jul-2014 mlarkin

Support hibernating to softraid crypto volumes.

much help and ok from deraadt@


# 1.266 16-Jul-2014 mlarkin

Reenable hibernate RLE support and flush+zero all memory after unpack.


# 1.265 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.264 12-Jul-2014 kettenis

Add a function to drop all clean pages on the page daemon queues and call
it when we hibernate.

ok mlarkin@, miod@, deraadt@


# 1.263 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.262 11-Jul-2014 mlarkin

Flush the buffercache to 16MB on hibernate and restore its previous max
size (kern.bufcachepercent) on resume, for better hibernate performance.

ok beck@


# 1.261 10-Jul-2014 blambert

remove unneeded header

ok mlarkin@


# 1.260 01-Jul-2014 kettenis

Adding an additonal delay after each access to SystemMemory or SystemIO
during autoconf makes no sense.

ok deraadt@


# 1.259 23-Jun-2014 kettenis

SystemMemory is used to access memory mapped registers on some machines, so we
must use the bus_space(9) API to access it instead of memcpy(9). Also make
sure we properly align access to fields that need more than byte alignment.

ok guenther@


# 1.258 21-May-2014 mlarkin

Fix invalid / missing format specifiers in several dnprintf statements
(used only when ACPI_DEBUG is enabled)

From a diff on tech@ by Fabian Raetz, with a minor change by me, tested
on i386 and amd64 with ACPI_DEBUG enabled and disabled.


# 1.257 25-Apr-2014 mlarkin

Defer the free of the hibernate structures until later, when it is safe to
do so.

Freeing the hibernate structures too early leads to inadvertent calls into
splx when we aren't ready for them yet. Found when trapping splx calls in
the resume path.

discussed with deraadt@ at m2k14


# 1.256 13-Apr-2014 mlarkin

Fix a badly worded debug message that implied the entire machine was going
to sleep, instead of just the acpi thread going to sleep.


# 1.255 31-Mar-2014 mpi

Change the order of operations during a suspend/resume cycle and
call bufq_quiesce() after executing the DVACT_QUIESCE handlers.

This should be safe since no disk nor controller drivers have such
handler but it will allow us to detach sd(4) devices attached to a
USB bus.

Another benefit pointed out by kettenis@ is that drivers that need
to read a firmware from the disk should be able to do it at resume
time in a DVACT_WAKEUP handler.

ok kettenis@, deraadt@


# 1.254 13-Mar-2014 dlg

get rid of the assumption that the head of the alldevs list is the
"mainbus" device. this breaks when mpath is enabled because it
attaches before mainbus and therefore takes the head position.

have autoconf provide device_mainbus() which looks up mainbus_cd,
and use that instead.

discussed with deraadt who just wants mpath stuff to move forward
despite there being many ways to shine this particular turd.


Revision tags: OPENBSD_5_5_BASE
# 1.253 21-Feb-2014 deraadt

Do the lid control further on in the resume sequence, since this is
AML, and will do WAITOK operations. While cold, that is a bad idea.
We seem to be safe with respect to the spec, I do not see any event
debounce issues here.
ok kettenis, tested by many


# 1.252 20-Jan-2014 claudio

Instead of checking sc->sc_revision <= 1 check that the
fadt->hdr_revision <= 1. This is a better check since it checks
the version of the proper descriptor. This allows the alix APU
to reboot since those boards have currently a wrong sc->sc_revision.
Committing this now so that it can be tested easier.
With and OK jsg@ also fine by derradt@


# 1.251 24-Dec-2013 kettenis

If the FADT has its SMI_CMD set to zero, assume we're only ACPI-only hardware
and don't need to disable SMI ownership of the ACPI hardware registers.

ok mlarkin@


# 1.250 23-Dec-2013 kettenis

Revert previous commit. It makes resume hang on some MP systems running
GENERIC.MP.


# 1.249 16-Dec-2013 kettenis

Restore hw.setperf upon resume like we do for apm(4) on i386. Seems we need
to do this fairly late in the resume to avoid hangs.

ok deraadt@, mlarkin@


# 1.248 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


# 1.247 06-Nov-2013 mpi

Add support for Power Resources for Dx states and the necessary hook
for PCI devices. This hook should be called twice, before and after
changing the power state of a PCI device.

Before setting the device to the new state, the ACPI layer will notify
every power resources linked to the device for that state and make sure
they are turned "_ON". After changing the state of the device, it will
decrement the reference of every power resources linked to that device
for the old state and turn them "_OFF" if they are no longer referenced.

This fixes the no-USB after resume problem seen on various ThinkPad,
problem initialy diagnosed with Alexander Polakov.

ok kettenis@, deraadt@


Revision tags: OPENBSD_5_4_BASE
# 1.246 01-Jun-2013 mlarkin

Stop printing ACPI wakeup devices in dmesg after the 16th wakeup device.

Helps with VMware VMs that report hundreds of wakeup devices.

ok reyk@, deraadt@


# 1.245 31-May-2013 bluhm

Fix the build for a kernel without wd and pciide in its config.
OK deraadt@ tedu@


# 1.244 30-May-2013 deraadt

Enforce ca_activate tree-walks over the entire heirarchy for all events,
cleaning up some shutdown-hook related code on the way.
(A few drivers related to sparc are still skipped at kettenis' request)
ok kettenis mlarkin, tested by many others too


# 1.243 18-Apr-2013 deraadt

pin the acpi thread to the BSP, because AML and SMI cannot be trusted. I
was certain we were doing this already, but it turns out no...


Revision tags: OPENBSD_5_3_BASE
# 1.242 24-Dec-2012 guenther

In acpiioctl(), Return though the splx() path if suser() returns an error

ok deraadt@


# 1.241 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


# 1.240 04-Oct-2012 ehrhardt

Use information provided by ACPI to attach secondary PCI host bridges.

ok kettenis@


# 1.239 07-Sep-2012 kettenis

Add a new function called acpi_pci_min_powerstate() that returns the lowest
power state for a PCI device for the power state (currently S3 or S4) we're
transitioning into. This function is probably too simplistic; there is a lot
of (silly) complexity in the ACPI spec.

ok deraadt@


Revision tags: OPENBSD_5_2_BASE
# 1.238 13-Jul-2012 pirofti

Disable acpivideo(4) on ASUS laptops.

There are some models that panic whenever an event is received.
Some cope, some don't. In order to get this right acpiec(4) requires
major rework. Until then let machines boot.

Discussed with deraadt@.


# 1.237 11-Jul-2012 mlarkin

for now, restrict hibernate to root in the acpi ioctl kernel interface.
normal rules for controlling this interface through apmd still apply.

ok deraadt@


# 1.236 09-Jul-2012 deraadt

tiny little oops in untested hibernate block


# 1.235 09-Jul-2012 deraadt

clean up ioctl() case statements


# 1.234 09-Jul-2012 deraadt

acpi_disable_onegpe() is not used; ok pirofti


# 1.233 24-May-2012 kettenis

If _MIN > _MAX consider a WordBus resource to be invalid. Fixes a panic on
the IBM x3550-M4.

ok mikeb@


# 1.232 29-Mar-2012 mlarkin

Missing #ifdef in previous diff


# 1.231 29-Mar-2012 mlarkin

Prevent hibernation attempts on machines with unsupported disk controllers.
The same check is also performed later in the hibernate sequence, but by
then it's too late to cleanly unwind (presently).

ok deraadt


# 1.230 26-Mar-2012 deraadt

simplify conditionals in acpi_sleep_state()
ok mlarkin


# 1.229 26-Mar-2012 deraadt

hook in the hibernate request code; half of this diff is from mlarkin
ok mlarkin


Revision tags: OPENBSD_5_1_BASE
# 1.228 20-Sep-2011 deraadt

Very early in suspend while the clock is still ticking realtime,
save the clock back to the rtc. The zaurus already did this.
ok phessler miod kettenis


Revision tags: OPENBSD_5_0_BASE
# 1.227 02-Jul-2011 nicm

kqueue attach functions should return an errno or 0, not a plain 1. Fix
the obvious cases to return EINVAL and ENXIO.

ok tedu deraadt


# 1.226 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.225 16-Jun-2011 pirofti

Import acpitoshiba driver.

This is originally written by Hiroyuki Aizu, ported and integrated
in our tree by Javier Vazquez <javama4 AT gmail> with tweaks and
suggestions by me.

Import prodded by deraadt@ and the lack of testers on tech@.


# 1.224 27-Apr-2011 jcs

attach acpithinkpad to newer lenovo models like the x120e

ok deraadt@


# 1.223 22-Apr-2011 jordan

Fix uninitialzied variables and formatting strings (-Wxxx errors)


Revision tags: OPENBSD_4_9_BASE
# 1.222 02-Jan-2011 jordan

Re-add code for ACPI taskqueue, eliminates need for state vars in acpi softc
Serialize processing ACPI tasks for sleep/powerdown/GPE event
ok deraadt@


# 1.221 31-Oct-2010 guenther

Revert last commit: it breaks resume on Thinkpads
"then please back it out" deraadt@


# 1.220 26-Oct-2010 jordan

Add task queue for ACPI gpe and notify handlers
This fixes eject on Dell Latitude dock and an issue on the Dell Mini
battery update.
ok deraadt


# 1.219 07-Oct-2010 jordan

Create separate functions for enabling wake and runtime gpes.
Remove old suspend/resume gpewalk function
ok deraadt, mlarkin


# 1.218 05-Oct-2010 jordan

split acpi_enable_onegpe to separate enable/disable functions
ok deraadt


# 1.217 05-Oct-2010 jordan

Added acpi_disable_allgpes function to clear GPE status
ok deraadt


# 1.216 05-Oct-2010 deraadt

clean up acpi_softc use more


# 1.215 05-Oct-2010 deraadt

delete a bit of #if 0 code


# 1.214 05-Oct-2010 deraadt

hide use of our global acpi_softc pointer a bit better


# 1.213 29-Sep-2010 deraadt

grab the acpi thread lock very early in acpi thread startup; not that it
is likely that anyone will race us... but why be careless.
ok kettenis


# 1.212 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.211 29-Aug-2010 jasper

- add a check for FWRITE here too.

ok miod@


# 1.210 27-Aug-2010 kettenis

Make PCI Power Management optional, and only enable it when acpi(4) attaches.

ok deraadt@


Revision tags: OPENBSD_4_8_BASE
# 1.209 08-Aug-2010 kettenis

Close race between the acpi thread and wscons ioctls running in process
context. We might rip this out post-release and replace it with a solution
that serializes things in a more obvious way.

ok deraadt@, miod@ and "those crazy texans who ok anything"


# 1.208 08-Aug-2010 deraadt

Report the acpi sleep states when on a RAMDISK; ok krw


# 1.207 07-Aug-2010 kettenis

Enforce acpi register access rescrictions.

ok mlarkin@, deraadt@, tested by marco@


# 1.206 07-Aug-2010 deraadt

upon resume, notify all the acpiac and acpibat drivers to update
their status. some laptops do not do this automatically.
ok kettenis canacar


# 1.205 06-Aug-2010 marco

Always call _PSW on _LID devices that support it to enable lid open to
resume the machine. Conversely disable it when the machine wakes up.

Tested by several ok deraadt


# 1.204 06-Aug-2010 deraadt

As part of a revamp of the PM1/GPE code, I write this diff to look for
unmanaged EN & STS bits in the PM1 register at interrupt time and report
them. As a side effect this splits the STS acknowledgement into two writes
(for power, and sleep) instead of one. The printf that is added (to spot
unmanaged STS bits) has not yet been triggered as far as we know.

Before the "write to PM1 registers at the right offset" diff went in, this
was not neccessary. But newer thinkpads do not have a working soft power
button without this diff.

We have no idea why.
ok mlarkin kettenis


# 1.203 05-Aug-2010 deraadt

PM1 power button events were generating 'fake events' to acpibtn PWRB,
but if there is no such device these were being tossed. Instead, arrange
for all power button handlers to flag a softc variable which is processed
by the thread; so that one place will know to request a powerdown (psignal
to init)
ok marco mlarkin


# 1.202 05-Aug-2010 deraadt

rename a variable which is annoyingly poorly named


# 1.201 05-Aug-2010 mlarkin

Write to the correct offset for various ACPI pmreg registers. Writing to
the correct register does properly clear the wake status bits and fixed
event bits, which allows some machines to have more than 1 suspend cycle.

bug spotted by deraadt@

tested by deraadt@ and myself on a half-dozen machines and others on
hackers@

ok deraadt@, marco@


# 1.200 05-Aug-2010 deraadt

Fix the power button. The pm1 register is 16 bits in size.
My mistake, a while back.
tested by krw


# 1.199 04-Aug-2010 kettenis

Print supported sleep states.

ok marco@, phessler@, deraadt@


# 1.198 03-Aug-2010 mlarkin

Use the proper flag for re-enabling certain hardware events (power btn,
sleep btn). The (incorrect) flag prevoiously used coincidentally had the
same value, so this shouldn't cause any different behavior than before.

tested on a variety of machines (i386, amd64, sp, mp) - no behavioral
change seen

ok deraadt@


# 1.197 28-Jul-2010 marco

Make all gasio failures look and behave the same.


# 1.196 28-Jul-2010 mlarkin

Delay a little longer on suspend before giving up. Fixes machines that
sometimes don't suspend because the sleep transition takes too long
(symptoms like failing to sleep but the backlight stays on).

ok deraadt@


# 1.195 27-Jul-2010 deraadt

remove XXX; EC can now be attached early when needed; ok jordan


# 1.194 27-Jul-2010 deraadt

more brackets feels good


# 1.193 27-Jul-2010 deraadt

XXX annotate a block in acpi_enter_sleep_state() that is pretty worrying


# 1.192 27-Jul-2010 deraadt

horrific indentation that nearly made me puke


# 1.191 27-Jul-2010 mlarkin

Set the sleeping indicator light on machines that support it, in case SMI
doesn't do it for us. Shows the 'moon' LED on older Thinkpads when
sleeping.

Tested on T43p, W500, T510 by me and on X61s by deraadt@.

ok deraadt@


# 1.190 27-Jul-2010 jordan

Early initialization of acpiec if ECDT table exists
Fixes hang when booting thinkpads while docked
ok deraadt


# 1.189 26-Jul-2010 deraadt

remove merge error which was commited


# 1.188 26-Jul-2010 pirofti

Add support for Sony ACPI hotkeys via a new driver: acpisony(4).

Currently it only works for the suspend button and tries to do right for
the brightness events, but I haven't found a Sony laptop that like to do
right so far.

In the future I want to make the brightness keys work on all Sony's and
also add support for the zoom hotkeys and whatever other funky keys I can
find on those things.

Okay deraadt@.


# 1.187 22-Jul-2010 deraadt

pretty large cleanup of SMALL_KERNEL


# 1.186 21-Jul-2010 deraadt

in the acpi GPE handler, late ack level interrupts not edge interrupts.
Duh.


# 1.185 21-Jul-2010 deraadt

Merge the edge and level gpe handlers into one (now possible since the
gpe structure tells us whether the gpe is edge or level)


# 1.184 21-Jul-2010 deraadt

delete #if 0 code we are not going to use; ok marco


# 1.183 20-Jul-2010 deraadt

make the mask code for acpi_enable_onegpe() match other similar code


# 1.182 20-Jul-2010 deraadt

Handle the acpi interrupt controller with proper edge/level handling,
wrapped in spltty to avoid racing against the interrupt controller.
Repair the gpe bit masking code while there, and do operations in the
right order
ok kettenis mlarkin, help from jordan at figuring out the order of
operations.


# 1.181 20-Jul-2010 matthew

When we're preparing to go to sleep, only call bufq_restart() if other
preparations failed.

ok kettenis@, thib@, deraadt@


# 1.180 19-Jul-2010 deraadt

permit compilation with ACPI_DEBUG


# 1.179 19-Jul-2010 deraadt

there is no need for a seperate dsdt_softc pointer; dsdt does not have a
softc but is just a part of the single acpi driver.
ok jordan


# 1.178 19-Jul-2010 deraadt

acpi_delay() is no longer needed; ok jordan


# 1.177 18-Jul-2010 mlarkin

Clear event status on resume, to avoid going back to sleep/reboot/shutdown
immediately after resume. Tested by a bunch of people on hackers@ on
various hardware. Fixes at least Toshiba NB200 and Gateway LT3103u.

ok marco


# 1.176 13-Jul-2010 deraadt

rename some thread related interfaces so they are logical


# 1.175 10-Jul-2010 jordan

Verify _ADR bdf is correct (some systems return 0xffff)
Works on Dell x9xx and Studio 1555


# 1.174 09-Jul-2010 jordan

Clear the noise


# 1.173 08-Jul-2010 jordan

Add mapping for ACPI device to PCI bdf (match autoconf tree)
Simplify resource parsing function to use buffer argument
Convert namespace linked lists to use queue macros
ok marco@, deraadt@


# 1.172 06-Jul-2010 deraadt

Use spltty() to lock downcalls from apm against the information being
modified by the acpi thread.
ok kettenis


# 1.171 06-Jul-2010 deraadt

Don't use a workq to do the suspend, because it races aginst the acpi
thread. Instead, just tell the acpi thread to do the suspend for us.
This makes apmd & zzz work correctly. While here, have acpithinkpad
attempt to post the event to apm before prompting the suspend itself.
ok kettenis marco mlarkin


# 1.170 05-Jul-2010 mlarkin

Reset register SCI_EN on resume. The ACPI spec clearly states we don't have
to do this, but many machines don't, or forget to, which leads to all sorts
of problems on resume (broken battery stats, overheating, failure to resume
more than once, etc).

tested by many, ok marco@, deraadt@


# 1.169 01-Jul-2010 thib

do bufq quiesce again, since vnd/the bufq_done call in
biodone has been fixed.


# 1.168 01-Jul-2010 jordan

Backout recent AML changes, breaks on R210 and others


# 1.167 01-Jul-2010 jordan

Simplified aml_parse_resource call, cleanup code
ok marco


# 1.166 01-Jul-2010 jordan

Fix subordinate bus # for multi-root PCI buses
ok marco, kettenis


# 1.165 30-Jun-2010 thib

Disable/partially backout the bufq quiesce changes as this
is causing havoc with vnds and release must be buildable.


# 1.164 29-Jun-2010 deraadt

Fix ramdisks (new code was placed inside a SMALL_KERNEL chunk)


# 1.163 29-Jun-2010 jordan

Add support for mapping ACPI to PCI devices
ok kettenis, deraadt


# 1.162 29-Jun-2010 kettenis

Call bufq_quiesce() on suspend to stop all I/O and bufq_restart on resume.
Makes suspend/resume work much better while doing disk I/O.

ok mlarkin@


# 1.161 29-Jun-2010 tedu

60 is a more reasonable guess for number of minutes in an hour than 100.
spotted by TeXitoi. ok deraadt


# 1.160 27-Jun-2010 jordan

Add _acpi_matchhids routine, precursor to pci device match


# 1.159 27-Jun-2010 mlarkin

Clear ACPI fixed event status on resume (power buttons/etc) so that some
machines don't immediately resume after suspending to S3.

Tested by kettenis@ and I on 5 or 6 machines, deraadt@ "yeah, looks good"


# 1.158 27-Jun-2010 jordan

Cleaned up acpi_maptable, adds to table list directly


# 1.157 07-Apr-2010 deraadt

If we have done a wsdisplay_suspend(), and ACPI decides it does not want to
suspend, we must roll back by calling wsdisplay_resume() or the virtual
console switching will be locked forever
ok oga


# 1.156 07-Apr-2010 kettenis

Make suspend/resume work on MP machines (running an MP kernel). Joint work
from mlarkin@ and me, with some amd64 fixes thrown in by deraadt@

ok marco@, deraadt@, pirofti@, mlarkin@


# 1.155 31-Mar-2010 kettenis

Bring /dev/apm support in line with apm(4). Make the suspend button send
a suspend request event to apmd(8) instead of suspending immediately. Also
keep track of whether /dev/apm and /dev/apmctl are currently open, such that
we can still suspend immediately if apmd(8) isn't running.

ok deraadt@, marco@, pirofti@, jsing@, oga@


# 1.154 30-Mar-2010 oga

Prevent the apmd/x races for good.

When we hit suspend time, go through all wsdisplays on the system. if
they are in mode MAPPED, but not MODE_DUMBFB then if possible do a full
vt switch to a !mapped vt, and prevent switching back until resume time.
This has to be called from MD code because this involves userland
running so that X can run the vt switch signal handler. This way, any
case where we are using the "poke registers from userland" model, we
will not be on the hardware when we go down, so the kernel can actually
handle thing properly.

Tested on several acpi laptops (by kettenis@ and ian@), x40 (me and
beck@ at LEAST) and zaurus (me). Maybe others, but if so I forgot who at
this time..

Idea from deraadt somewhere over the Faroe Islands (I thought of a
similar thing myself a while ago). Much prompting from him. Ok and
comments miod@


# 1.153 25-Mar-2010 oga

Fix & vs. && in the kqfilter code.

Based on a comparison with the apm code.

ok deraadt@, kettenis@


Revision tags: OPENBSD_4_7_BASE
# 1.152 26-Nov-2009 mlarkin

Visual (and on some machines, audio) feedback while the machine is
resuming via calls to aml _SST method. This also makes the "moon" LED on
thinkpads to blink during resume and cease when resume is completed.

ok pirofti@


# 1.151 26-Nov-2009 deraadt

Toggle into "cold" when we we disable interrupts, because tsleeping
in the AML interpreter and context switching into userland processes
tends to sometimes pop the ACPI trampoline page out of our address
space. Makes lots more machines work.
ok kettenis mlarkin


# 1.150 24-Nov-2009 jsg

in the resume path evaluate _WAK before dealing with device drivers
like the spec says, not after.

ok mlarkin@


# 1.149 24-Nov-2009 deraadt

abort suspends on SMP systems until that gets written; ok mlarkin


# 1.148 23-Nov-2009 mlarkin

pci should return the result of it's children's suspend/resume calls back
to its parent.

handle suspend failure case and unwind if devices have an activate function
that returns failure.

ok deraadt@, kettenis@


# 1.147 23-Nov-2009 mlarkin

acpi* devices need to call their children on suspend/resume.
ok deraadt@


# 1.146 23-Nov-2009 pirofti

Remove ACPI_SLEEP_ENABLED checks.

This enables by default the suspend/resume paths in the kernel.

Okay deraadt@.


# 1.145 23-Nov-2009 deraadt

knf and spacing that are super annoying


# 1.144 23-Nov-2009 mlarkin

Hook up Theo's new children suspend/resume function, as well as more ACPI
suspend/resume glue.

ok deraadt@


# 1.143 26-Oct-2009 deraadt

Do not do strncmp comparisons on dv_xname because longer device names which
look similar could arrive in the future. Instead, compare directly against
dv_cfdata->cf_driver->cd_name
Issue originally spotted by miod


# 1.142 26-Oct-2009 jordan

Verify checksum+address when loading ACPI tables.
Some systems had invalid entries in RSDT/XSDT.
ok marco@


# 1.141 23-Jul-2009 cnst

New aibs(4) driver for ASUSTeK AI Booster (ACPI ATK0110) hardware monitoring,
with sensor state support through limits provided by the ACPI.

Tested on several ASUS motherboards kindly networked by Sam Fourman Jr.

ok deraadt marco jordan


Revision tags: OPENBSD_4_6_BASE
# 1.140 03-Jun-2009 pirofti

Import acpi power resource driver. This is needed for suspend/resume logic.

Okay jordan@ and marco@.


# 1.139 03-Jun-2009 pirofti

Add acpivideo support. This adds brightness support for all laptops
except thinkpads, they will use the acpithinkpad driver. The driver is
also hooked into wsconsole. So brightness can be adjusted via:

$ wsconsctl display.brightness=<percentage>

This is very helpfull on some laptops that have a nasty bios and get two
steps instead of one when pressing the brightness button.

Tested on various dell, fujitsu, acer, samsung and other laptops.

Okay marco@, miod@. Suggestions from kettenis@.
Lots of reviews and help from miod@, thanks!


# 1.138 03-Jun-2009 jordan

Added ejectable bay for IDE cdroms
ok marco@


# 1.137 30-Apr-2009 marco

Oga changed apm ioctls and forgot to grep. Temporary work around by hitting
the sleep button when we get any request. This interface will chnage at
some point.
ok oga


# 1.136 19-Apr-2009 krw

Call acpi_sleep_walk() when powering down just like when entering
any other sleep state. This tells acpi wake devices to not wake up
the box until manual powering up resets their status. Makes my Dell
GX520 stay powered off in response to 'halt -p'.

In snaps for a while.

ok marco@ (several times) deraadt@


# 1.135 17-Apr-2009 pirofti

Add device ranking support for future sleep/wake developments.

okay marco@


# 1.134 11-Mar-2009 jordan

Cleanup.. use aml_evalinteger instead of aml_evalname
ok marco@


# 1.133 10-Mar-2009 jordan

Fix cut/paste error.. call _BFS not _PTS
ok marco@


Revision tags: OPENBSD_4_5_BASE
# 1.132 21-Feb-2009 marco

overzealous cut/paste; found by Vladimir Kirillov <proger@uaoug.org.ua>


# 1.131 20-Feb-2009 marco

Oops didn't mean for Debugger() to remain there.


# 1.130 19-Feb-2009 marco

suspend/resume bits so that we can develop this in tree. This is disabled.
code from mlarkin and me
help from art,toby,jordan and several others
ok jordan, go for it deraadt


# 1.129 10-Feb-2009 jordan

Fix null pointer for shutdown if acpi not enabled
ok marco@


# 1.128 04-Feb-2009 kettenis

Some systems that claim to be ACPI 2.0 apparently don't provide an XSDT
address. Make the code fall back on the RSDT on those systems.

From Joerg Niendorf <p3y80@internode.on.net>. Fixes PR 5986.


# 1.127 17-Jan-2009 krw

A couple of stray malloc/memset -> malloc(M_ZERO), a bit of knf and
lose a couple of unnecessary casts. No functional change.

Feedback from kettenis@

ok kettenis@ marco@


# 1.126 06-Nov-2008 marco

Collapse all sanity checks of hids into one function.

Originaly from jordan


Revision tags: OPENBSD_4_4_BASE
# 1.125 02-Jul-2008 fgsch

add acpivideo. for now almost an empty skel so others can work on
suspend/resume. eventually it will also manage output switching and
brightness where supported. prodded by marco@


# 1.124 11-Jun-2008 marco

Fix $OpenBSD$ for once and for all


# 1.123 08-Jun-2008 deraadt

aaa_dev was being set from an uninitialized variable; ok toby


# 1.122 07-Jun-2008 marco

Kill ACPIVERBOSE. It was never used and is worthless to boot.

comments jmc
ok toby


# 1.121 06-Jun-2008 marco

Shrink acpi some more for floppies and while here re-enable acpi on the
floppies. Fixes crazy sony vaio kettenis has.

tested by deraadt
ok deraadt jordan kettenis


# 1.120 01-Jun-2008 marco

Fix \n nit in SMALL_KERNEL.


# 1.119 01-Jun-2008 marco

Re-enable new parser. Lots of testing by various folks. Fits all media
now too.

ok kettenis deraadt


# 1.118 16-May-2008 dlg

back out jordans new parser.

it didnt have enough oks from the right people, it did break machines
that were previously working, and it bloated the RAMDISKS and install
media beyond the constraints of the media.

requested by deraadt@


# 1.117 14-May-2008 jordan

Adding new ACPI Parser code
Old guts of code still exists, needs to be torn out next
ok marco@
ok brad@


# 1.116 27-Apr-2008 jcs

add acpithinkpad(4), an acpi support driver for ibm/lenovo
thinkpads.

provides functionality that the bios did on older, non-acpi
thinkpads like respond to screen brightness keys and toggle the
bluetooth device on and off.

ok deraadt@ and marco@


# 1.115 24-Apr-2008 jsing

Introduce acpiasus(4), a driver for the ACPI based hotkeys found in many
ASUS laptops (including the ASUS EeePC) - largely based on NetBSD's
asus(4) driver. On the ASUS EeePC this allows us to enable/disable
wireless, change screen brightness and use the volume keys.

ok jsg@, weingart@


# 1.114 12-Apr-2008 kettenis

Get rid of acpi_s5 global variables; simply send SIGUSR2 instead of SUGUSR1
to tell init(8) to power down the machine.

ok krw@


Revision tags: OPENBSD_4_3_BASE
# 1.113 21-Jan-2008 jsg

Check against RESET_REG_SUP in FADT before doing the acpi reset dance
after all, not doing so appears to break more machines than it fixes.

ok marco@


# 1.112 05-Jan-2008 kettenis

Never attach more acpicpu(4) devices than cpu(4) devices. Always attach
the first acpicpu(4) device on non-MULTIPROCESSOR kernels, regardless of its
ACPI processor ID.

ok mikeb@


# 1.111 05-Dec-2007 deraadt

Hang acpi(4) below bios(4) instead of mainbus(4). This lets us move acpi(4)
before pcibios(4), and then.... if acpi(4) attaches, skip pcibios(4) since
it messes the machines up. Fixes claudio's HP dl320 G5, and almost assuredly
others.
ok kettenis


# 1.110 05-Dec-2007 deraadt

repair acpiopen/acpiclose and friends to handle the /dev/apmctl interface,
so that apmd(8) can actually work correctly against acpi(4), and thus,
even do the fancy -C/-A stuff; also tested by jmc


# 1.109 02-Dec-2007 fgsch

fix the trailing space for real now.


# 1.108 02-Dec-2007 fgsch

avoid trailing spaces on output; marco@ ok.


# 1.107 27-Nov-2007 kettenis

Fix off-by-one that made us attach acpicpu0 and acpicpu1 on GENERIC kernels.

ok marco@


# 1.106 25-Nov-2007 jsg

When available and ACPI is enabled use the ACPI reset method
to reboot. This is required for some quirky sis machines
and likely others to reboot.

Tested by many. 'commit early commit often' deraadt@


# 1.105 19-Nov-2007 deraadt

sigh, amd64 still uses that stupid acpi_enabled variable...


# 1.104 19-Nov-2007 deraadt

softc's come initialized to 0, you know...


# 1.103 19-Nov-2007 deraadt

store revision; from jsg


# 1.102 19-Nov-2007 kettenis

Initialize sc_facs earlier.

ok deraadt@, beck@, canacar@


# 1.101 16-Nov-2007 deraadt

fix the bus_space #define nightmare, so that amd64 and i386 are much more
uniform. as a result shared code like acpi needs less #ifdef's
ok marco kettenis


# 1.100 15-Nov-2007 deraadt

remove the #ifdef ACPI_ENABLE. the EC codepath is either safe enough (and
will be improved), or it is unsafe because AML assumes it must be there
ok almost everyone


# 1.99 15-Nov-2007 mikeb

Make acpicpu(4) attach to the existing CPUs only.

Problem reported by form@. Fix idea by kettenis@.
Lots of help from deraadt@. Tested by deraadt@, form@ and me.

ok deraadt marco


# 1.98 14-Nov-2007 deraadt

spacing


# 1.97 12-Nov-2007 deraadt

correct : vs , in attach-time messages


# 1.96 06-Nov-2007 deraadt

all ACPI_ENABLE kernels need acpi_interrupt()


# 1.95 06-Nov-2007 deraadt

move acpi_read_pmreg() and acpi_write_pmreg() out of SMALL_KERNEL so
that acpi ramdisks can compile (obvious solution)


# 1.94 05-Nov-2007 canacar

Establish the acpi interrupt earlier. Fixes problems with some
GENERIC.MP kernels not getting acpi interrupts. This is a
workaround until the real issue with late establishment is fixed.
ok weingart@, beck@, gwk@


# 1.93 03-Nov-2007 jordan

Added support for displaying wakeup devices
ok beck@,weingart@,gwk@


# 1.92 11-Oct-2007 kettenis

When walking the tree yo find _PRT methods, check whether the device is
actually enabled and functioning. Similar to what we do for _INI.

ok weingart@, beck@


# 1.91 08-Oct-2007 krw

More simple memset(,0,) -> M_ZERO changes. In this batch move to
size(*p) as the first malloc() parameter where p is declared locally
and thus easy to check. Add M_ZERO to gpe_table allocation in acpi.c
even though there is no obvious bzero or memset nearby.


# 1.90 13-Sep-2007 mk

Remove unnecessary memset() (see aml_evalnode(9)) and a bit of KNF.

ok toby


# 1.89 13-Sep-2007 weingart

Implement ACPI 6.5.1 spec tree walk for _STA and _INI.
Fixes mk's laptop. No regressions so far (thank you to the
testers).

ok gwk@, mk@, marco@


Revision tags: OPENBSD_4_2_BASE
# 1.88 17-Apr-2007 mk

Now that acpidock no longer hangs machines during boot (at least
according to the reports I've been getting) it should be safe to enable
it again, so remove the #if 0 stuff around it.

ok marco


# 1.87 11-Apr-2007 jordan

Added changes for C-State
ok marco@


# 1.86 26-Mar-2007 marco

Calculate minutes left more accurately.

From giovanni <qgiovanni@gmail.com>


Revision tags: OPENBSD_4_1_BASE
# 1.85 03-Mar-2007 mk

Stop looking for acpidock until we fix that stuff that breaks all pre-60
series ThinkPads with acpidock(4) enabled.


# 1.84 22-Feb-2007 marco

We were getting no interrupts in .MP. There are 2 fixes; one in
acpi_machdep.c and move the interrupt establishment down after ioapic
has been initialized.

ok jordan


# 1.83 21-Feb-2007 kettenis

Make an acpi-enabled GENERIC.MP work on systems that only support PIC mode.

ok marco@


# 1.82 19-Feb-2007 jordan

Added acpi_map_address function for mapping GAS/IO registers
ok marco@


# 1.81 18-Feb-2007 jordan

Added dynamic perfect hash for opcodes
ok marco@


# 1.80 18-Feb-2007 marco

Shave off another K pre compression.
Shuffle SMALL_KERNEL functions around so that we only have a single #ifndef


# 1.79 17-Feb-2007 marco

Add SMALL_KERNEL throught acpi to make it fit on boot media.
ok deraadt


# 1.78 13-Feb-2007 marco

Add smarter _INI code. This helps with some dissapearing devices.

ok kettenis jordan


# 1.77 31-Jan-2007 gwk

Introduce acpi_hasprocfvs thats "ACPI Has Processor Frequency and Voltage
Scalling" a global flag set by acicpu attach to indicate that ACPI is
handling fvs through the _PSS objects. This will fix disappearing
EST/powernow when booting kernels with acpi enabled, as acpicpu isnt
compiled into GENERIC yet. Also in cases where acpicpu is built in and
legacy EST/powernow work but acpi is missing the _PSS object, the legacy
routines will be used.

tested by dim@, and Nick Nauwelaerts <nick AT nauwelaerts.net>
ok marco@, dim@


# 1.76 22-Jan-2007 mk

Actually look for docking stations by looking for _DCK nodes.

ok marco


# 1.75 02-Jan-2007 marco

On some systems EC must be attached as the first device. Move EC from
generic _HID devices to its own routine to force first attach. Bug found
and debugged by joris@

Code mostly written by joris@


# 1.74 26-Dec-2006 marco

Add polling to devices that require it such as acpibat and acpitz.
Use the same tsleep/wakeup mechanism as the interrupt code to keep it non-
concurrent.


# 1.73 21-Dec-2006 deraadt

even more knf love


# 1.72 21-Dec-2006 marco

Remove some leftover acpiec gpe/interrupt code.


# 1.71 20-Dec-2006 deraadt

oops, name already used


# 1.70 20-Dec-2006 deraadt

rename __acpi_enable_gpe(); ok marco


# 1.69 20-Dec-2006 deraadt

acpi_unmap_pmregs() is not used, ok marco


# 1.68 19-Dec-2006 deraadt

simplify some low-level methods substantially (shrinking code); ok marco jordan


# 1.67 06-Dec-2006 mk

malloc(..., M_NOWAIT) with no NULL check is bad. If allocation fails,
complain, clean up, and bail out of attach.

input and ok gwk


# 1.66 28-Nov-2006 jordan

Fixed clearing arg for acpi_init_pic
ok kettenis@


# 1.65 27-Nov-2006 kettenis

Don't spam dmesg.


# 1.64 27-Nov-2006 jordan

Fixes for acpiprt routing
Post-parse fixup for namerefs
ok kettenis@


# 1.63 25-Nov-2006 marco

Don't handle acpiev events inside the gpe handler since this causes deadlocks.

ok jordan@


# 1.62 15-Nov-2006 kettenis

Hook up acpi apic interrupt routing support to generic acpi code.

ok jordan@


# 1.61 02-Nov-2006 marco

Kill acpiec attach function and add its functionality to the generic _HID
device attach

prodded and ok jordan


# 1.60 02-Nov-2006 marco

Fix comments to reflect reality.


# 1.59 19-Oct-2006 marco

Replace ACPI ioctls with APM ioctls. This makes ACPI talk the same protocol
as APM therefore userspace can now retrieve power information in the same
fashion as all other apm arches (zaurus, macppc, i386 etc).

To use force the use of acpi device like so: apmd -f /dev/acpi

ok jordan@


# 1.58 19-Oct-2006 jordan

Fixed GPE debug levels
Fixed ref counter on temporary bufferfields
DSDT parsing fully working on IBM Thinkpad
ok marco@


# 1.57 15-Oct-2006 jordan

Removed old GPE handler code remnants
ok marco@


# 1.56 12-Oct-2006 jordan

Massive ACPI changes
Fixed ACPI leaks
new ACPI API
ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.55 30-Jun-2006 gwk

KNF, No functional change,
"go at it" marco@


# 1.54 30-Jun-2006 jordan

Support for calling Embedded Controller via new GPE interface
ok marco@


# 1.53 30-Jun-2006 jordan

Added new code for GPE handling
Also works with ACPIEC; need to fixup acpiec.c to remove original interrupt handler
ok marco@


# 1.52 30-Jun-2006 jordan

Added offset argument for acpi read/write pmreg routines (new GPE code)
ok marco@


# 1.51 31-May-2006 todd

do not call acpi_powerdown() unless acpi has successfully initialized
ok gwk@ canacar@


# 1.50 29-May-2006 canacar

Add embedded controller support.
ok marco@ deraadt@ testing & ok gwk@


# 1.49 19-May-2006 canacar

Minimal thermal zone driver. Monitors thermal zone temperature,
shuts down the system if the 'critical temperature' is reached.
ok marco@


# 1.48 11-Apr-2006 marco

KNF.


# 1.47 11-Apr-2006 gwk

Add acpi_resume(struct acpi_softc *sc), which calls some AML methods,
detected at attach time, currently unused.

ok marco@


# 1.46 11-Apr-2006 gwk

Discover some ACPI sleep related methods, and call them at the
appropriate time in acpi_enter_sleep_state().

ok marco@


Revision tags: OPENBSD_3_9_BASE
# 1.45 26-Feb-2006 marco

Add acpicpu as a device.

ok jordan@


# 1.44 23-Feb-2006 marco

remove dead function.

ok jordan@


# 1.43 22-Feb-2006 marco

Print out all tables it found during autoconf. We'll need this data in
the future.


# 1.42 22-Feb-2006 marco

Make dmesg less verbose.

ok jordan.


# 1.41 22-Feb-2006 jordan

Added parsing of SSDT tables
ok marco@


# 1.40 22-Feb-2006 jordan

Added new aml_register to notify by device id
Moved powerdown code to button device
ok marco@


# 1.39 21-Feb-2006 marco

Readjust debug levels that crept in too low over the last few days.
KNF


# 1.38 21-Feb-2006 marco

Rig the power button so that the machine shuts down correctly.


# 1.37 20-Feb-2006 marco

Add acpibtn device. Remains disabled for now.


# 1.36 19-Feb-2006 jordan

Added code for aml_notify GPE notification callback
ok marco@


# 1.35 19-Feb-2006 grange

Fix acpi_enter_sleep_state() by replacing current ugly hack
with mostly correct sleeping state transition code.

ok brad@ marco@


# 1.34 19-Feb-2006 marco

jordan:
Enable interrupts.
Added peripheral code to make this work.

marco:
Replace softintr and other types of interrupts with a handler with context.
Fixed interrupt magic for GPEs.
Some KNF

This is work done by jordan and I but has to be a single commit.


# 1.33 17-Feb-2006 marco

Remove earlier introduced delays. We really only need them during autconf.

ok jordan@


# 1.32 17-Feb-2006 marco

Add a generic delay function as per the ACPI spec.

from jordan@


# 1.31 17-Feb-2006 marco

Use tsleep instead of delay when !cold.


# 1.30 17-Feb-2006 marco

Argh, we need delays between reads/writes. The backend reads (i2c most likely)
are so slow that values come back as 0 sometimes. We have to do this
despite the spec saying we shouldnt have to. Guess having a faster
parser than others can hurt sometimes.

Idea from jordan.


# 1.29 17-Feb-2006 jordan

Fixed code for handling _INI
Rearranged debug statements
ok marco@


# 1.28 16-Feb-2006 jordan

Added stub for calling _INI
Fixed aml_dowhile for NULL return
ok marco@


# 1.27 16-Feb-2006 jordan

Fixed parser/evaluator to retrieve battery info+state
Cleanup of parser
Dell laptops return battery status
ok marco@


# 1.26 06-Feb-2006 brad

fix a typo so this will compile on amd64.


# 1.25 04-Feb-2006 marco

When ACPI_ENABLED wasn't set but the devices were it had an unknown variable.

Pointed out by ian@.


# 1.24 03-Feb-2006 jordan

Rewrote aml parser yet again...
Fixed bus_space_map for i386/amd64
Added _BST and _BIF to acpibat to get status/info
ok marco@


# 1.23 20-Jan-2006 jordan

More evaluator changes
Removed old evaluator code
Fixed _HID value
ok marco@


# 1.22 19-Jan-2006 jordan

Added support for read/write of PCI Config space
Help jason@
ok marco@


# 1.21 18-Jan-2006 jordan

Added new evaluation routines
Added helper methods for buffer/field evaluation
Fixed broken indentation on dsdt.c

ok marco@


# 1.20 17-Jan-2006 jordan

Major overhaul of the aml parser. Evaluater temporarily disabled.

ok marco@


# 1.19 06-Jan-2006 grange

Check for ACPI control availability only if ACPI_ENABLE defined.


# 1.18 06-Jan-2006 grange

Informative error messages on attach, prettier dmesg.


# 1.17 06-Jan-2006 grange

printf -> dnprintf conversion for debug messages.


# 1.16 05-Jan-2006 grange

Find SLP_TYPx values and store them for future use in
acpi_enter_sleep_state().


# 1.15 05-Jan-2006 grange

Random KNF and cleanup


# 1.14 05-Jan-2006 grange

Provide symbolic names for the acpi sleeping states and use it.


# 1.13 28-Dec-2005 marco

Moved DEVNAME to a acpidev.h

ok jordan@


# 1.12 18-Dec-2005 sturm

add back comment about wrong code

ok marco


# 1.11 16-Dec-2005 marco

Remove test for kbd_reset. Prompted deraadt@, ok jordan@


# 1.10 16-Dec-2005 jordan

Added hooks for powerdown/power button press


# 1.9 16-Dec-2005 jordan

Cleaned up common power management register i/o
Added enter sleep state ioctl
Hooked power button interrupt
ok marco@


# 1.8 16-Dec-2005 marco

Remove autoconf chatter


# 1.7 16-Dec-2005 jordan

Moved DSDT AML parsing into parent ACPI driver
ok marco@


# 1.6 14-Dec-2005 marco

Add AC device glue.


# 1.5 14-Dec-2005 marco

Adjust debug printing levels to something more sane.


# 1.4 13-Dec-2005 marco

Add glue to attach devices to apci.


# 1.3 07-Dec-2005 marco

KNF


Revision tags: OPENBSD_3_8_BASE
# 1.2 10-Jul-2005 grange

Spacing cleanup.


# 1.1 02-Jun-2005 tholo

Start on a basic ACPI framework -- does not do much more than read out the
ACPI tables into kernel memory and attach ACPI and HPET timers currently.

In order to test this code, enabling the devices in GENERIC as well as
the ACPI_ENABLE option is needed. This code does not do any thermal
control yet, so this should be done with care depending on the platform.

In the tree so more people can contribute to making this more fully
featured.

Ok niklas@ grange@ tedu@


# 1.355 10-Jul-2018 kettenis

Make legacy interrupts work in acpipci(4).

ok patrick@


# 1.354 05-Jul-2018 kettenis

Add acpipci(4), a driver that supports generic ECAM-compatible PCI host
bridges based on information provided by ACPI.

ok mlarkin@


# 1.353 04-Jul-2018 kettenis

Properly pass around the PCI "chipset tag" in acpi(4) and refactor
acpimcfg(4) to call an MD initialization functions that sets up a tag for
PCI ECAM.

ok guenther@, mlarkin@, krw@


# 1.352 01-Jul-2018 kettenis

Move acpi(4) attach glue into acpi_machdep.c.

ok guenther@, deraadt@


# 1.351 01-Jul-2018 kettenis

Add support for _DSD Device Properties.

ok mlarkin@


# 1.350 30-Jun-2018 kettenis

Add intr_enable() function, intended for MI use to amd64 and i386 and use
this in the acpi(4) suspend/resume code paths.

ok deraadt@


# 1.349 29-Jun-2018 kettenis

Use <stdint.h> types.

ok mpi@, jung@, krw@, deraadt@


# 1.348 29-Jun-2018 mlarkin

ACPI: Allow (with warning) GPE handler reassignment, instead of returning
a failure code.

ok kettenis


# 1.347 26-Jun-2018 mlarkin

knf


# 1.346 26-Jun-2018 kettenis

Let acpi(4) pass down the bus dma tag.

ok deraadt@


# 1.345 25-Jun-2018 kettenis

Reorganize acpi(4) code a bit in preparation for upcoming arm64 support.

ok deraadt@


# 1.344 20-May-2018 kettenis

Wake up the acpi thread after scheduling a gpio event.

ok mpi@


# 1.343 17-May-2018 kettenis

Sprinkle some #ifndef SMALL_KERNEL such that RAMDISK kernels cuild again.

pointed out by stsp@


# 1.342 17-May-2018 kettenis

Implement GenericSerialBus OpRegion support.

ok mlarkin@


# 1.341 27-Mar-2018 kettenis

Add acpicmos(4), a driver that implements SystemCMOS OperationRegion
access support. This fixes machines where the AML doesn't check whether
support for this OperationRegion type has been registered by the OS.

ok mlarkin@


Revision tags: OPENBSD_6_3_BASE
# 1.340 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.339 10-Feb-2018 deraadt

Syncronize filesystems to disk when suspending. Each mountpoint's vnodes
are pushed to disk. Dangling vnodes (unlinked files still in use) and
vnodes undergoing change by long-running syscalls are identified -- and
such filesystems are marked dirty on-disk while we are suspended (in case
power is lost, a fsck will be required). Filesystems without dangling or
busy vnodes are marked clean, resulting in faster boots following
"battery died" circumstances.
Tested by numerous developers, thanks for the feedback.


# 1.338 08-Feb-2018 deraadt

After flushing disks, try again to release all memory. We don't need to
save dirty memory to the hibernate space.


# 1.337 08-Feb-2018 deraadt

Toss all releaseable memory, because fragmentation can get in the way
of allocating the hibernate playpen.


# 1.336 08-Feb-2018 deraadt

sr_quiesce() is a new approach for ensuring that softraid drains
output to the disks.

This is part of a larger suspend/resume filesystem-safety diff, which
has been worked on for a couple of months already. Tests by job, krw,
beck, benno, and others. Sometimes even by snapshot users...


# 1.335 29-Nov-2017 kettenis

Add support for GPIO-signaled events.

ok mlarkin@


# 1.334 16-Nov-2017 jcs

Add PCI attachment for dwiic(4) needed by Intel 100 Series machines

ok kettenis


# 1.333 14-Nov-2017 kettenis

Add a few more PNP IDs to the skip lists.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.332 17-Aug-2017 stsp

Make dwiic(4) attach its ACPI parent dependencies.
This change makes the Asus E200HA keyboard work.

Original analysis and patch by Cesare Gargano
ok kettenis@


# 1.331 09-Aug-2017 dcoppa

Add "PNP0303" (8042 PS/2 Controller) to acpi_isa_hids[]

ok kettenis@


# 1.330 22-Jul-2017 jcs

acpibat: look for _BIX first before falling back on _BIF

ACPI 4.0 deprecated _BIF for battery status so some newer machines
are exporting _BIX which extends _BIF's package with a few extra
fields. When using _BIX, export the new discharge cycle count as a
sensor.

feedback from kettenis and anton
ok deraadt


# 1.329 20-Jul-2017 kettenis

Start a list of isa(4) device names such that we can avoid printing them
as "not configured".

ok mikeb@, millert@, deraadt@


# 1.328 08-Apr-2017 deraadt

Sensors are run as callbacks inside tasks. During suspend a sensor
could be running inside a driver that will be force-detached, or due
to tsleep end up disrupting the softstate/hardstate contract. At
suspend time, quisce all these callbacks by waiting for completion.
This issue has never been observed for real, but may be implicated in
suspend/resume failures.
ok kettenis guenther mlarkin


# 1.327 08-Apr-2017 deraadt

Two prototypes for acpi_maptable; merge to one place


Revision tags: OPENBSD_6_1_BASE
# 1.326 27-Mar-2017 deraadt

Now that hibernate_alloc() only has clean success/failure, don't
need to call hibernate_free() to clean up a partial mess.
ok mlarkin kettenis


# 1.325 13-Mar-2017 deraadt

allow ramdisks to build; ok jcs


# 1.324 12-Mar-2017 jcs

try to attach acpisbs first and if successful, don't attach acpibat


# 1.323 02-Mar-2017 natano

Add a new sysctl machdep.lidaction. The sysctl works as follows:

machdep.lidaction=0 # do nothing
machdep.lidaction=1 # suspend
machdep.lidaction=2 # hibernate

lidsuspend is just an alias for lidaction, so if you change one, the
other one will have the same value. The plan is to remove
machdep.lidsuspend eventually when people have upgraded their
/ets/sysctl.conf.

discussed with deraadt, who came up with the new MIB name
no objections mlarkin
ok stsp halex jcs


# 1.322 28-Feb-2017 natano

Untangle abstract sleep modes from ACPI sleep states. This paves the way
for further work in the area.

idea and ok deraadt
ok mlarkin


# 1.321 22-Feb-2017 jcs

add acpisbs, an acpi smart battery subsystem driver reading data
over smbus

currently disabled because it conflicts with acpibat

ok deraadt, kettenis


# 1.320 14-Jan-2017 kettenis

Revert aml_rdpciaddr changes; breaks several machines that were working
before.


# 1.319 11-Jan-2017 jsg

Increase the size of the buffers used for _HID and _CID strings to be
able to fit "Hyper_V_Gen_Counter_V1".

ok mlarkin@ deraadt@


# 1.318 08-Jan-2017 kettenis

Make aml_rdpciaddr() work for for devices behind bridges. The code now
uses the mapping between AML nodes and PCI devices that we establish.
Because _INIT methods may end up calling aml_rdpciaddr(), make sure we
create that mapping early. Also handle devices that aren't actually present.
These devices are now included in the mapping and reads will return an
all-ones patterm whereas writes are a no-op..

ok mlarkin@


# 1.317 25-Oct-2016 pirofti

Add the resource index to aml_parse_resource() callback function.

This is needed by an upcoming acpiec commit that handles machines breaking
the current ACPI specifications.

Change suggested by and ok kettenis@, guenther@


# 1.316 18-Sep-2016 guenther

If the APIC table (MADT) isn't present then ci_acpi_proc_id won't be set
when acpicpu tries to attach, leading to a NULL dereference. The ACPI
processor object on the Dells where this was reported is bogus and useless
anyway so apply the same test when deciding whether to attach to processor
objects as acpicpu would when matching them to cpus.

Problem reported by Eivind Eide (xenofil (at) gmail.com) and
Johan Huldtgren (johan+openbsd-bugs (at) huldtgren.com).
Testing by Eivind Eide.
ok kettenis@ mlarkin@


# 1.315 03-Sep-2016 naddy

Write the system time back to the RTC every 30 minutes.
This fixes the problem that long-running machines which were not
shut down properly would reboot with a badly offset system time.

hints and ok kettenis@


# 1.314 31-Aug-2016 mlarkin

Don't put a device in the wake device list if _STA says it's not there.

Fixes at least one machine that woke up from sleep / poweroff immediately.

ok kettenis@, tested by kettenis and martijn@


# 1.313 28-Jul-2016 kettenis

Store the acpi processor ID/UID in struct cpu_info, and use it to attach
acpicpu(4) drivers to the right cpu(4).

ok mlarkin@, guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.312 10-Jun-2016 kettenis

The power management timer has been made optional in ACPI 5.0A, so don't
attempt to attach acpitimer(4) if the timer isn't present.

Pointed out by jcs@

ok jcs@


# 1.311 08-May-2016 kettenis

Make sure devices listed by a device's _DEP method are attached before
we attach the device itself. The _DEP methode, introduced in ACPI 5.0,
is a hint that the device in question depends on OpRegion support from
the devices listed by _DEP, so we have to order them the proper way.
To prevent us from attaching those devices again when we encounter them
later walking down the device tree, keep track of our attempts to
attach a device driver to them using aflag in the aml_node structure.

ok guenther@


# 1.310 25-Apr-2016 pirofti

We attach acpihpet(4) and acpiec(4) without using the HID, so do not
report it in the dmesg.

OK deraadt@, mlarkin@.


# 1.309 02-Apr-2016 kettenis

Reduce dmesg spam by knocking out some acpi hardware IDs that don't need a
driver.

ok mpi@


# 1.308 29-Mar-2016 kettenis

Check if a device is present (using _STA) in acpi_foundhid. This prevents
us from reporting devices as "not configured" that aren't actually present,
and allows us to remove duplicated code from several drivers.

ok deraadt@, ok jsg@


# 1.307 29-Mar-2016 kettenis

Implement a cleaner way to prevent acpivout(4) from attaching if we have
a platform-specific method of controlling the backlight.

ok millert@, mlarkin@, jung@


# 1.306 28-Mar-2016 kettenis

Adding each and every new acpi device driver to acpi_foundhid() is
gettinga bit of the burden. And it really isn't how our autoconf
framework is supposed to work for busses that can be enumerated. All
the drivers already check for a matching _HID in their attach
function. So we can just drop the checks in acpi_foundhid() and let
the drivers sort themselves out.

Print a "not configured" line for devices for which we don't attach adriver.
This may be a bit much as there are devices for which we will not have a
driver. This will be removed if it causes too much dmesg spam.

ok deraadt@, mlarkin@


Revision tags: OPENBSD_5_9_BASE
# 1.305 17-Jan-2016 jsg

Take acpi_foundhid() outside of #ifndef SMALL_KERNEL so the ramdisk
kernel can find and attach dwiic(4) and sdhc(4).


# 1.304 17-Jan-2016 jsg

remove an unused global variable


# 1.303 14-Jan-2016 kettenis

Attach dwiic(4) on the Intel Bay Trail i2c controllers as well.


# 1.302 12-Jan-2016 kettenis

Improve emulation of PM registers on Hardware-redcued ACPI by emulating
SPL_TYPx, SLP_EN and WAK_STS using the new SLEEP_STATUS_REG and
SLEEP_CONTROL_REG registers. Makes the transition into S5 (powerdown) work
on the ASUS X205TA.

ok deraadt@, mlarkin@


# 1.301 12-Jan-2016 jcs

Add dwiic, a driver for the Synopsys DesignWare i2c controller found
on the Samsung ATIV Book 9 laptop. This initial version only
supports ACPI config/attachment.

Add ihidev, a HID-over-i2c driver largely based on uhidev. dwiic
handles attaching ihidev devices found in ACPI.

Add ims, a HID-over-i2c mouse/trackpad driver to get basic cursor
and button functionality from HID-compliant i2c trackpads.

ok kettenis deraadt


# 1.300 11-Jan-2016 kettenis

Make sdhc(4) attach to hardware IDs "80860F14" and "PNP0FFF".


# 1.299 09-Jan-2016 kettenis

Initial support for Hardware-Reduced ACPI. This skips the legacy mode checks
by emulating the PM_CNT register such that SCI_EN is always enabled. It also
skip attaching acpitimer(4) since the relevant registers are no longer there.

ok deraadt@, mlarkin@


# 1.298 06-Jan-2016 kettenis

Follow the spec and check the SCI_EN bit to see if ACPI has already
been enabled and don't bother enabling it again if it is. This should fix
systems that don't implement legacy mode and therefore set ACPI_ENABLE and
ACPI_DISABLE to zero.

ok mlarkin@, yasuoka@


# 1.297 23-Nov-2015 reyk

Back out the CRITICAL change, turn it back from 10% to 25%. Some
people with old/broken batteries didn't like this change, and the
underlying heuristics are wrong.


# 1.296 22-Nov-2015 reyk

The "CRITICAL" APM battery level, that is exported/faked by the ACPI
stack, is currently set to 25%. With 25% battery left, a typical
modern laptop will have at least one hour runtime left. It is an
arbitrary value, and other APM power layers in the kernel set CRITICAL
to something like 5% or 10%, so set it to 10% for now.

OK mlarkin@


# 1.295 28-Sep-2015 deraadt

In low-level suspend routines, set cold=2. In tsleep(), use this to
spit out a ddb trace to console. This should allow us to find suspend
or resume routines which break the rules. It depends on the console
output function being non-sleeping.... but that's another codepath which
should try to be safe when cold is set.
ok kettenis


# 1.294 06-Sep-2015 deraadt

clarify malloc arguments (base + length...)


# 1.293 28-Aug-2015 deraadt

fairly simple sizes for free(); ok tedu


# 1.292 20-Aug-2015 kettenis

Respect the access size when reading or writing to pci config space.
This version also makes sure writes are properly aligned, preventing
the panics seen with the previous incarnation of this change.

Fixes battery status passthrough in vmware, brightness keys on my x220
and who knows what else.

ok mlarkin@, deraadt@


Revision tags: OPENBSD_5_8_BASE
# 1.291 04-Aug-2015 deraadt

If we are setting ACPI_PM1_BM_RLD do it only once, and remember to
repeat this action at resume time.
ok mlarkin guenther


# 1.290 29-Jul-2015 deraadt

Something subtle in previous revision is locking up suspend or booting
on some laptops. Mailing lists threads mention x220 and Asus machines.
Reverting for now.


# 1.289 21-Jul-2015 kettenis

iRespect the access size when reading or writing to pci config space.
Fixes battery status passthrough in vmware and who knows what else.

Tested by many.

ok mlarkin@, deraadt@


# 1.288 17-Jul-2015 mlarkin

fix comments


# 1.287 26-Mar-2015 jsg

Use the 0x10 extended capability to determine if the trackstick
buttons are wired to the trackpad and need to be re-routed to the
trackstick. Without this change the buttons on 2015 thinkpads
get picked up as extended buttons that show up as scroll up/down.

Remove the X1 Carbon 2015 (LEN0048) and X250 (LEN0046) from the
top button area/soft buttons quirks list. Also avoid using the
quirk list entirely if the capability bit is set.

Based on work from jcs@ with feedback from mpi@
ok mpi@ jcs@


# 1.286 20-Mar-2015 kettenis

The ACPI standard says that a PCI function number of FFFF can be used to refer
to all functions on a device. This makes no sense in the context of accessing
PCI config space. Nevertheless stsp@ has a machine with AML that tries to
do config space reads from a device where _ADR encodes function number FFFF.
This is almost certainly buggy AML, but since Linux (and presumably Windows)
boot just fine on this machine we need to work around this. Linux effectively
treats function FFFF as function 7, which isn't present on this machine.
Instead we simulate a read from a non-existent device by returning a bit
pattern of all ones. Writes to function FFFF will still panic.

ok mlarkin@


# 1.285 16-Mar-2015 deraadt

Reduce use of SMALL_KERNEL in acpi, bringing acpiec driver and more gpe
handling into RAMDISK. This is now possible because the install media
has ample room. The goal is to reduce special cases where we may be
skipping (unknown) important operations...
ok mlarkin kettenis


# 1.284 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.283 07-Feb-2015 deraadt

New framework that allows hibernate to pass in entropy from it's fresh
boot.
ok mlarkin


# 1.282 06-Feb-2015 mlarkin

Fix a hibernate crash on some machines due to unmapping a page that
may not have been mapped previously (in the failure to hibernate case).

Also ensure that the lowmem ptp is mapped in all cases (not just MP).

ok kettenis


# 1.281 17-Jan-2015 deraadt

resume_randomness() before spinning up other cpus...


# 1.280 15-Jan-2015 jsg

Define a new wscons mouse type for Synaptics clickpad devices that lack
physical buttons. This will be used if the acpi pnpid for the mouse
matches a list provided by Synaptics found in Linux.

Combined with the xenocara changes this will remove the need for an xorg
config file for the x240/t440/t540 generation of thinkpads.

ok matthieu@ shadchin@ kettenis@


# 1.279 11-Jan-2015 kettenis

Tolerate checksum errors in ACPI tables (but complain about them). Apparently
there are machines out there with broken BIOSen with such checksum errors in
thier RSDT tables. Windows runs fine on these machines, so it probably does
not check. Linux ignores checksum errors as well (and prints a warning).

ok deraadt@, krw@


# 1.278 18-Dec-2014 deraadt

suspend and resume the RNG (at what looks like the right spots)
ok reyk djm


# 1.277 09-Dec-2014 doug

Replace some malloc(n*size,...) calls with mallocarray().

ok tedu@ deraadt@


# 1.276 06-Dec-2014 mpi

If "machdep.lidsuspend" is not set, do not go back to sleep if the lid
is still closed when resuming. This allows docked laptop to be resumed
without opening them.

ok mlarkin@


# 1.275 23-Nov-2014 mlarkin

If a machine has no _LIDs, then none can be open. This diff fixes a problem
where machines without any _LIDs were going back to sleep after hibernate
due to the recent suspend-if-lid-closed-on-resume diff. Noticed on qemu,
which doesn't have any _LIDs.

discussed with deraadt


# 1.274 18-Nov-2014 krw

Nuke yet more obvious #include duplications.

ok miod@


# 1.273 08-Nov-2014 mlarkin

If resuming from sleep (zzz/ZZZ) and the lid is still closed, go back to
sleep. This prevents accidental lid flex or slight opening in a backpack
from waking the machine up and leaving it resumed (powered on).

ok deraadt@


# 1.272 26-Sep-2014 kettenis

Rework piglet and pig allocation. Currently the piglet gets allocated
deep down in the suspend path, where it is really hard to recover from
allocation failure. So allocate the piglet early on in the suspend path.
Also change the piglet and piglet allocation functions to use km_alloc(9)
instead of doing pmemrange magic. This removes a bunch of code which, in the
case of the piglet allocation, is broken since it results in a NULL pointer
dereference. Also switch the piglet allocation to not wait. If we can't
allocate 16MB of phys contig memory on a halfway modern machine we're almost
certainly under a lot of memory pressure and we're better off not trying to
hibernate anyway.

ok mlarkin@


# 1.271 25-Sep-2014 kettenis

Bring back rev 1.249. Now that mp_setperf() has been fixed, it should be
safe to restore perflevel when resuming. Should fix the issues people
see with the processor running at full speed heating up the machine while
the apm command reports that the CPUs are running at a low speed.

ok dcoppa@, deraadt@


# 1.270 23-Sep-2014 kettenis

Release the acpi lock around calls to wsdisplay_suspend() and
wsdisplay_resume(). The X server may adjust the display brightness just
before/after the VT switch when suspending/resuming. In fact, it may
already be blocked adjusting the display brightness for some reason
unrelated to the suspend request.

The VT switch request will eventually time out, but the delay is annoying
and the failed VT switch will lead to problems after resume.

Needed for a future update of the xf86-video-intel driver.

ok mlarkin@


# 1.269 19-Sep-2014 kettenis

Use config_suspend_all(9).


# 1.268 08-Sep-2014 kettenis

Clear the PME Status bit on PCI devices in response to Device Wake events.
Fixes the interrupt storm on the 2009 Mac mini with WOL enabled on its
nfe(4) interface.

ok mlarkin@


Revision tags: OPENBSD_5_6_BASE
# 1.267 20-Jul-2014 mlarkin

Support hibernating to softraid crypto volumes.

much help and ok from deraadt@


# 1.266 16-Jul-2014 mlarkin

Reenable hibernate RLE support and flush+zero all memory after unpack.


# 1.265 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.264 12-Jul-2014 kettenis

Add a function to drop all clean pages on the page daemon queues and call
it when we hibernate.

ok mlarkin@, miod@, deraadt@


# 1.263 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.262 11-Jul-2014 mlarkin

Flush the buffercache to 16MB on hibernate and restore its previous max
size (kern.bufcachepercent) on resume, for better hibernate performance.

ok beck@


# 1.261 10-Jul-2014 blambert

remove unneeded header

ok mlarkin@


# 1.260 01-Jul-2014 kettenis

Adding an additonal delay after each access to SystemMemory or SystemIO
during autoconf makes no sense.

ok deraadt@


# 1.259 23-Jun-2014 kettenis

SystemMemory is used to access memory mapped registers on some machines, so we
must use the bus_space(9) API to access it instead of memcpy(9). Also make
sure we properly align access to fields that need more than byte alignment.

ok guenther@


# 1.258 21-May-2014 mlarkin

Fix invalid / missing format specifiers in several dnprintf statements
(used only when ACPI_DEBUG is enabled)

From a diff on tech@ by Fabian Raetz, with a minor change by me, tested
on i386 and amd64 with ACPI_DEBUG enabled and disabled.


# 1.257 25-Apr-2014 mlarkin

Defer the free of the hibernate structures until later, when it is safe to
do so.

Freeing the hibernate structures too early leads to inadvertent calls into
splx when we aren't ready for them yet. Found when trapping splx calls in
the resume path.

discussed with deraadt@ at m2k14


# 1.256 13-Apr-2014 mlarkin

Fix a badly worded debug message that implied the entire machine was going
to sleep, instead of just the acpi thread going to sleep.


# 1.255 31-Mar-2014 mpi

Change the order of operations during a suspend/resume cycle and
call bufq_quiesce() after executing the DVACT_QUIESCE handlers.

This should be safe since no disk nor controller drivers have such
handler but it will allow us to detach sd(4) devices attached to a
USB bus.

Another benefit pointed out by kettenis@ is that drivers that need
to read a firmware from the disk should be able to do it at resume
time in a DVACT_WAKEUP handler.

ok kettenis@, deraadt@


# 1.254 13-Mar-2014 dlg

get rid of the assumption that the head of the alldevs list is the
"mainbus" device. this breaks when mpath is enabled because it
attaches before mainbus and therefore takes the head position.

have autoconf provide device_mainbus() which looks up mainbus_cd,
and use that instead.

discussed with deraadt who just wants mpath stuff to move forward
despite there being many ways to shine this particular turd.


Revision tags: OPENBSD_5_5_BASE
# 1.253 21-Feb-2014 deraadt

Do the lid control further on in the resume sequence, since this is
AML, and will do WAITOK operations. While cold, that is a bad idea.
We seem to be safe with respect to the spec, I do not see any event
debounce issues here.
ok kettenis, tested by many


# 1.252 20-Jan-2014 claudio

Instead of checking sc->sc_revision <= 1 check that the
fadt->hdr_revision <= 1. This is a better check since it checks
the version of the proper descriptor. This allows the alix APU
to reboot since those boards have currently a wrong sc->sc_revision.
Committing this now so that it can be tested easier.
With and OK jsg@ also fine by derradt@


# 1.251 24-Dec-2013 kettenis

If the FADT has its SMI_CMD set to zero, assume we're only ACPI-only hardware
and don't need to disable SMI ownership of the ACPI hardware registers.

ok mlarkin@


# 1.250 23-Dec-2013 kettenis

Revert previous commit. It makes resume hang on some MP systems running
GENERIC.MP.


# 1.249 16-Dec-2013 kettenis

Restore hw.setperf upon resume like we do for apm(4) on i386. Seems we need
to do this fairly late in the resume to avoid hangs.

ok deraadt@, mlarkin@


# 1.248 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


# 1.247 06-Nov-2013 mpi

Add support for Power Resources for Dx states and the necessary hook
for PCI devices. This hook should be called twice, before and after
changing the power state of a PCI device.

Before setting the device to the new state, the ACPI layer will notify
every power resources linked to the device for that state and make sure
they are turned "_ON". After changing the state of the device, it will
decrement the reference of every power resources linked to that device
for the old state and turn them "_OFF" if they are no longer referenced.

This fixes the no-USB after resume problem seen on various ThinkPad,
problem initialy diagnosed with Alexander Polakov.

ok kettenis@, deraadt@


Revision tags: OPENBSD_5_4_BASE
# 1.246 01-Jun-2013 mlarkin

Stop printing ACPI wakeup devices in dmesg after the 16th wakeup device.

Helps with VMware VMs that report hundreds of wakeup devices.

ok reyk@, deraadt@


# 1.245 31-May-2013 bluhm

Fix the build for a kernel without wd and pciide in its config.
OK deraadt@ tedu@


# 1.244 30-May-2013 deraadt

Enforce ca_activate tree-walks over the entire heirarchy for all events,
cleaning up some shutdown-hook related code on the way.
(A few drivers related to sparc are still skipped at kettenis' request)
ok kettenis mlarkin, tested by many others too


# 1.243 18-Apr-2013 deraadt

pin the acpi thread to the BSP, because AML and SMI cannot be trusted. I
was certain we were doing this already, but it turns out no...


Revision tags: OPENBSD_5_3_BASE
# 1.242 24-Dec-2012 guenther

In acpiioctl(), Return though the splx() path if suser() returns an error

ok deraadt@


# 1.241 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


# 1.240 04-Oct-2012 ehrhardt

Use information provided by ACPI to attach secondary PCI host bridges.

ok kettenis@


# 1.239 07-Sep-2012 kettenis

Add a new function called acpi_pci_min_powerstate() that returns the lowest
power state for a PCI device for the power state (currently S3 or S4) we're
transitioning into. This function is probably too simplistic; there is a lot
of (silly) complexity in the ACPI spec.

ok deraadt@


Revision tags: OPENBSD_5_2_BASE
# 1.238 13-Jul-2012 pirofti

Disable acpivideo(4) on ASUS laptops.

There are some models that panic whenever an event is received.
Some cope, some don't. In order to get this right acpiec(4) requires
major rework. Until then let machines boot.

Discussed with deraadt@.


# 1.237 11-Jul-2012 mlarkin

for now, restrict hibernate to root in the acpi ioctl kernel interface.
normal rules for controlling this interface through apmd still apply.

ok deraadt@


# 1.236 09-Jul-2012 deraadt

tiny little oops in untested hibernate block


# 1.235 09-Jul-2012 deraadt

clean up ioctl() case statements


# 1.234 09-Jul-2012 deraadt

acpi_disable_onegpe() is not used; ok pirofti


# 1.233 24-May-2012 kettenis

If _MIN > _MAX consider a WordBus resource to be invalid. Fixes a panic on
the IBM x3550-M4.

ok mikeb@


# 1.232 29-Mar-2012 mlarkin

Missing #ifdef in previous diff


# 1.231 29-Mar-2012 mlarkin

Prevent hibernation attempts on machines with unsupported disk controllers.
The same check is also performed later in the hibernate sequence, but by
then it's too late to cleanly unwind (presently).

ok deraadt


# 1.230 26-Mar-2012 deraadt

simplify conditionals in acpi_sleep_state()
ok mlarkin


# 1.229 26-Mar-2012 deraadt

hook in the hibernate request code; half of this diff is from mlarkin
ok mlarkin


Revision tags: OPENBSD_5_1_BASE
# 1.228 20-Sep-2011 deraadt

Very early in suspend while the clock is still ticking realtime,
save the clock back to the rtc. The zaurus already did this.
ok phessler miod kettenis


Revision tags: OPENBSD_5_0_BASE
# 1.227 02-Jul-2011 nicm

kqueue attach functions should return an errno or 0, not a plain 1. Fix
the obvious cases to return EINVAL and ENXIO.

ok tedu deraadt


# 1.226 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.225 16-Jun-2011 pirofti

Import acpitoshiba driver.

This is originally written by Hiroyuki Aizu, ported and integrated
in our tree by Javier Vazquez <javama4 AT gmail> with tweaks and
suggestions by me.

Import prodded by deraadt@ and the lack of testers on tech@.


# 1.224 27-Apr-2011 jcs

attach acpithinkpad to newer lenovo models like the x120e

ok deraadt@


# 1.223 22-Apr-2011 jordan

Fix uninitialzied variables and formatting strings (-Wxxx errors)


Revision tags: OPENBSD_4_9_BASE
# 1.222 02-Jan-2011 jordan

Re-add code for ACPI taskqueue, eliminates need for state vars in acpi softc
Serialize processing ACPI tasks for sleep/powerdown/GPE event
ok deraadt@


# 1.221 31-Oct-2010 guenther

Revert last commit: it breaks resume on Thinkpads
"then please back it out" deraadt@


# 1.220 26-Oct-2010 jordan

Add task queue for ACPI gpe and notify handlers
This fixes eject on Dell Latitude dock and an issue on the Dell Mini
battery update.
ok deraadt


# 1.219 07-Oct-2010 jordan

Create separate functions for enabling wake and runtime gpes.
Remove old suspend/resume gpewalk function
ok deraadt, mlarkin


# 1.218 05-Oct-2010 jordan

split acpi_enable_onegpe to separate enable/disable functions
ok deraadt


# 1.217 05-Oct-2010 jordan

Added acpi_disable_allgpes function to clear GPE status
ok deraadt


# 1.216 05-Oct-2010 deraadt

clean up acpi_softc use more


# 1.215 05-Oct-2010 deraadt

delete a bit of #if 0 code


# 1.214 05-Oct-2010 deraadt

hide use of our global acpi_softc pointer a bit better


# 1.213 29-Sep-2010 deraadt

grab the acpi thread lock very early in acpi thread startup; not that it
is likely that anyone will race us... but why be careless.
ok kettenis


# 1.212 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.211 29-Aug-2010 jasper

- add a check for FWRITE here too.

ok miod@


# 1.210 27-Aug-2010 kettenis

Make PCI Power Management optional, and only enable it when acpi(4) attaches.

ok deraadt@


Revision tags: OPENBSD_4_8_BASE
# 1.209 08-Aug-2010 kettenis

Close race between the acpi thread and wscons ioctls running in process
context. We might rip this out post-release and replace it with a solution
that serializes things in a more obvious way.

ok deraadt@, miod@ and "those crazy texans who ok anything"


# 1.208 08-Aug-2010 deraadt

Report the acpi sleep states when on a RAMDISK; ok krw


# 1.207 07-Aug-2010 kettenis

Enforce acpi register access rescrictions.

ok mlarkin@, deraadt@, tested by marco@


# 1.206 07-Aug-2010 deraadt

upon resume, notify all the acpiac and acpibat drivers to update
their status. some laptops do not do this automatically.
ok kettenis canacar


# 1.205 06-Aug-2010 marco

Always call _PSW on _LID devices that support it to enable lid open to
resume the machine. Conversely disable it when the machine wakes up.

Tested by several ok deraadt


# 1.204 06-Aug-2010 deraadt

As part of a revamp of the PM1/GPE code, I write this diff to look for
unmanaged EN & STS bits in the PM1 register at interrupt time and report
them. As a side effect this splits the STS acknowledgement into two writes
(for power, and sleep) instead of one. The printf that is added (to spot
unmanaged STS bits) has not yet been triggered as far as we know.

Before the "write to PM1 registers at the right offset" diff went in, this
was not neccessary. But newer thinkpads do not have a working soft power
button without this diff.

We have no idea why.
ok mlarkin kettenis


# 1.203 05-Aug-2010 deraadt

PM1 power button events were generating 'fake events' to acpibtn PWRB,
but if there is no such device these were being tossed. Instead, arrange
for all power button handlers to flag a softc variable which is processed
by the thread; so that one place will know to request a powerdown (psignal
to init)
ok marco mlarkin


# 1.202 05-Aug-2010 deraadt

rename a variable which is annoyingly poorly named


# 1.201 05-Aug-2010 mlarkin

Write to the correct offset for various ACPI pmreg registers. Writing to
the correct register does properly clear the wake status bits and fixed
event bits, which allows some machines to have more than 1 suspend cycle.

bug spotted by deraadt@

tested by deraadt@ and myself on a half-dozen machines and others on
hackers@

ok deraadt@, marco@


# 1.200 05-Aug-2010 deraadt

Fix the power button. The pm1 register is 16 bits in size.
My mistake, a while back.
tested by krw


# 1.199 04-Aug-2010 kettenis

Print supported sleep states.

ok marco@, phessler@, deraadt@


# 1.198 03-Aug-2010 mlarkin

Use the proper flag for re-enabling certain hardware events (power btn,
sleep btn). The (incorrect) flag prevoiously used coincidentally had the
same value, so this shouldn't cause any different behavior than before.

tested on a variety of machines (i386, amd64, sp, mp) - no behavioral
change seen

ok deraadt@


# 1.197 28-Jul-2010 marco

Make all gasio failures look and behave the same.


# 1.196 28-Jul-2010 mlarkin

Delay a little longer on suspend before giving up. Fixes machines that
sometimes don't suspend because the sleep transition takes too long
(symptoms like failing to sleep but the backlight stays on).

ok deraadt@


# 1.195 27-Jul-2010 deraadt

remove XXX; EC can now be attached early when needed; ok jordan


# 1.194 27-Jul-2010 deraadt

more brackets feels good


# 1.193 27-Jul-2010 deraadt

XXX annotate a block in acpi_enter_sleep_state() that is pretty worrying


# 1.192 27-Jul-2010 deraadt

horrific indentation that nearly made me puke


# 1.191 27-Jul-2010 mlarkin

Set the sleeping indicator light on machines that support it, in case SMI
doesn't do it for us. Shows the 'moon' LED on older Thinkpads when
sleeping.

Tested on T43p, W500, T510 by me and on X61s by deraadt@.

ok deraadt@


# 1.190 27-Jul-2010 jordan

Early initialization of acpiec if ECDT table exists
Fixes hang when booting thinkpads while docked
ok deraadt


# 1.189 26-Jul-2010 deraadt

remove merge error which was commited


# 1.188 26-Jul-2010 pirofti

Add support for Sony ACPI hotkeys via a new driver: acpisony(4).

Currently it only works for the suspend button and tries to do right for
the brightness events, but I haven't found a Sony laptop that like to do
right so far.

In the future I want to make the brightness keys work on all Sony's and
also add support for the zoom hotkeys and whatever other funky keys I can
find on those things.

Okay deraadt@.


# 1.187 22-Jul-2010 deraadt

pretty large cleanup of SMALL_KERNEL


# 1.186 21-Jul-2010 deraadt

in the acpi GPE handler, late ack level interrupts not edge interrupts.
Duh.


# 1.185 21-Jul-2010 deraadt

Merge the edge and level gpe handlers into one (now possible since the
gpe structure tells us whether the gpe is edge or level)


# 1.184 21-Jul-2010 deraadt

delete #if 0 code we are not going to use; ok marco


# 1.183 20-Jul-2010 deraadt

make the mask code for acpi_enable_onegpe() match other similar code


# 1.182 20-Jul-2010 deraadt

Handle the acpi interrupt controller with proper edge/level handling,
wrapped in spltty to avoid racing against the interrupt controller.
Repair the gpe bit masking code while there, and do operations in the
right order
ok kettenis mlarkin, help from jordan at figuring out the order of
operations.


# 1.181 20-Jul-2010 matthew

When we're preparing to go to sleep, only call bufq_restart() if other
preparations failed.

ok kettenis@, thib@, deraadt@


# 1.180 19-Jul-2010 deraadt

permit compilation with ACPI_DEBUG


# 1.179 19-Jul-2010 deraadt

there is no need for a seperate dsdt_softc pointer; dsdt does not have a
softc but is just a part of the single acpi driver.
ok jordan


# 1.178 19-Jul-2010 deraadt

acpi_delay() is no longer needed; ok jordan


# 1.177 18-Jul-2010 mlarkin

Clear event status on resume, to avoid going back to sleep/reboot/shutdown
immediately after resume. Tested by a bunch of people on hackers@ on
various hardware. Fixes at least Toshiba NB200 and Gateway LT3103u.

ok marco


# 1.176 13-Jul-2010 deraadt

rename some thread related interfaces so they are logical


# 1.175 10-Jul-2010 jordan

Verify _ADR bdf is correct (some systems return 0xffff)
Works on Dell x9xx and Studio 1555


# 1.174 09-Jul-2010 jordan

Clear the noise


# 1.173 08-Jul-2010 jordan

Add mapping for ACPI device to PCI bdf (match autoconf tree)
Simplify resource parsing function to use buffer argument
Convert namespace linked lists to use queue macros
ok marco@, deraadt@


# 1.172 06-Jul-2010 deraadt

Use spltty() to lock downcalls from apm against the information being
modified by the acpi thread.
ok kettenis


# 1.171 06-Jul-2010 deraadt

Don't use a workq to do the suspend, because it races aginst the acpi
thread. Instead, just tell the acpi thread to do the suspend for us.
This makes apmd & zzz work correctly. While here, have acpithinkpad
attempt to post the event to apm before prompting the suspend itself.
ok kettenis marco mlarkin


# 1.170 05-Jul-2010 mlarkin

Reset register SCI_EN on resume. The ACPI spec clearly states we don't have
to do this, but many machines don't, or forget to, which leads to all sorts
of problems on resume (broken battery stats, overheating, failure to resume
more than once, etc).

tested by many, ok marco@, deraadt@


# 1.169 01-Jul-2010 thib

do bufq quiesce again, since vnd/the bufq_done call in
biodone has been fixed.


# 1.168 01-Jul-2010 jordan

Backout recent AML changes, breaks on R210 and others


# 1.167 01-Jul-2010 jordan

Simplified aml_parse_resource call, cleanup code
ok marco


# 1.166 01-Jul-2010 jordan

Fix subordinate bus # for multi-root PCI buses
ok marco, kettenis


# 1.165 30-Jun-2010 thib

Disable/partially backout the bufq quiesce changes as this
is causing havoc with vnds and release must be buildable.


# 1.164 29-Jun-2010 deraadt

Fix ramdisks (new code was placed inside a SMALL_KERNEL chunk)


# 1.163 29-Jun-2010 jordan

Add support for mapping ACPI to PCI devices
ok kettenis, deraadt


# 1.162 29-Jun-2010 kettenis

Call bufq_quiesce() on suspend to stop all I/O and bufq_restart on resume.
Makes suspend/resume work much better while doing disk I/O.

ok mlarkin@


# 1.161 29-Jun-2010 tedu

60 is a more reasonable guess for number of minutes in an hour than 100.
spotted by TeXitoi. ok deraadt


# 1.160 27-Jun-2010 jordan

Add _acpi_matchhids routine, precursor to pci device match


# 1.159 27-Jun-2010 mlarkin

Clear ACPI fixed event status on resume (power buttons/etc) so that some
machines don't immediately resume after suspending to S3.

Tested by kettenis@ and I on 5 or 6 machines, deraadt@ "yeah, looks good"


# 1.158 27-Jun-2010 jordan

Cleaned up acpi_maptable, adds to table list directly


# 1.157 07-Apr-2010 deraadt

If we have done a wsdisplay_suspend(), and ACPI decides it does not want to
suspend, we must roll back by calling wsdisplay_resume() or the virtual
console switching will be locked forever
ok oga


# 1.156 07-Apr-2010 kettenis

Make suspend/resume work on MP machines (running an MP kernel). Joint work
from mlarkin@ and me, with some amd64 fixes thrown in by deraadt@

ok marco@, deraadt@, pirofti@, mlarkin@


# 1.155 31-Mar-2010 kettenis

Bring /dev/apm support in line with apm(4). Make the suspend button send
a suspend request event to apmd(8) instead of suspending immediately. Also
keep track of whether /dev/apm and /dev/apmctl are currently open, such that
we can still suspend immediately if apmd(8) isn't running.

ok deraadt@, marco@, pirofti@, jsing@, oga@


# 1.154 30-Mar-2010 oga

Prevent the apmd/x races for good.

When we hit suspend time, go through all wsdisplays on the system. if
they are in mode MAPPED, but not MODE_DUMBFB then if possible do a full
vt switch to a !mapped vt, and prevent switching back until resume time.
This has to be called from MD code because this involves userland
running so that X can run the vt switch signal handler. This way, any
case where we are using the "poke registers from userland" model, we
will not be on the hardware when we go down, so the kernel can actually
handle thing properly.

Tested on several acpi laptops (by kettenis@ and ian@), x40 (me and
beck@ at LEAST) and zaurus (me). Maybe others, but if so I forgot who at
this time..

Idea from deraadt somewhere over the Faroe Islands (I thought of a
similar thing myself a while ago). Much prompting from him. Ok and
comments miod@


# 1.153 25-Mar-2010 oga

Fix & vs. && in the kqfilter code.

Based on a comparison with the apm code.

ok deraadt@, kettenis@


Revision tags: OPENBSD_4_7_BASE
# 1.152 26-Nov-2009 mlarkin

Visual (and on some machines, audio) feedback while the machine is
resuming via calls to aml _SST method. This also makes the "moon" LED on
thinkpads to blink during resume and cease when resume is completed.

ok pirofti@


# 1.151 26-Nov-2009 deraadt

Toggle into "cold" when we we disable interrupts, because tsleeping
in the AML interpreter and context switching into userland processes
tends to sometimes pop the ACPI trampoline page out of our address
space. Makes lots more machines work.
ok kettenis mlarkin


# 1.150 24-Nov-2009 jsg

in the resume path evaluate _WAK before dealing with device drivers
like the spec says, not after.

ok mlarkin@


# 1.149 24-Nov-2009 deraadt

abort suspends on SMP systems until that gets written; ok mlarkin


# 1.148 23-Nov-2009 mlarkin

pci should return the result of it's children's suspend/resume calls back
to its parent.

handle suspend failure case and unwind if devices have an activate function
that returns failure.

ok deraadt@, kettenis@


# 1.147 23-Nov-2009 mlarkin

acpi* devices need to call their children on suspend/resume.
ok deraadt@


# 1.146 23-Nov-2009 pirofti

Remove ACPI_SLEEP_ENABLED checks.

This enables by default the suspend/resume paths in the kernel.

Okay deraadt@.


# 1.145 23-Nov-2009 deraadt

knf and spacing that are super annoying


# 1.144 23-Nov-2009 mlarkin

Hook up Theo's new children suspend/resume function, as well as more ACPI
suspend/resume glue.

ok deraadt@


# 1.143 26-Oct-2009 deraadt

Do not do strncmp comparisons on dv_xname because longer device names which
look similar could arrive in the future. Instead, compare directly against
dv_cfdata->cf_driver->cd_name
Issue originally spotted by miod


# 1.142 26-Oct-2009 jordan

Verify checksum+address when loading ACPI tables.
Some systems had invalid entries in RSDT/XSDT.
ok marco@


# 1.141 23-Jul-2009 cnst

New aibs(4) driver for ASUSTeK AI Booster (ACPI ATK0110) hardware monitoring,
with sensor state support through limits provided by the ACPI.

Tested on several ASUS motherboards kindly networked by Sam Fourman Jr.

ok deraadt marco jordan


Revision tags: OPENBSD_4_6_BASE
# 1.140 03-Jun-2009 pirofti

Import acpi power resource driver. This is needed for suspend/resume logic.

Okay jordan@ and marco@.


# 1.139 03-Jun-2009 pirofti

Add acpivideo support. This adds brightness support for all laptops
except thinkpads, they will use the acpithinkpad driver. The driver is
also hooked into wsconsole. So brightness can be adjusted via:

$ wsconsctl display.brightness=<percentage>

This is very helpfull on some laptops that have a nasty bios and get two
steps instead of one when pressing the brightness button.

Tested on various dell, fujitsu, acer, samsung and other laptops.

Okay marco@, miod@. Suggestions from kettenis@.
Lots of reviews and help from miod@, thanks!


# 1.138 03-Jun-2009 jordan

Added ejectable bay for IDE cdroms
ok marco@


# 1.137 30-Apr-2009 marco

Oga changed apm ioctls and forgot to grep. Temporary work around by hitting
the sleep button when we get any request. This interface will chnage at
some point.
ok oga


# 1.136 19-Apr-2009 krw

Call acpi_sleep_walk() when powering down just like when entering
any other sleep state. This tells acpi wake devices to not wake up
the box until manual powering up resets their status. Makes my Dell
GX520 stay powered off in response to 'halt -p'.

In snaps for a while.

ok marco@ (several times) deraadt@


# 1.135 17-Apr-2009 pirofti

Add device ranking support for future sleep/wake developments.

okay marco@


# 1.134 11-Mar-2009 jordan

Cleanup.. use aml_evalinteger instead of aml_evalname
ok marco@


# 1.133 10-Mar-2009 jordan

Fix cut/paste error.. call _BFS not _PTS
ok marco@


Revision tags: OPENBSD_4_5_BASE
# 1.132 21-Feb-2009 marco

overzealous cut/paste; found by Vladimir Kirillov <proger@uaoug.org.ua>


# 1.131 20-Feb-2009 marco

Oops didn't mean for Debugger() to remain there.


# 1.130 19-Feb-2009 marco

suspend/resume bits so that we can develop this in tree. This is disabled.
code from mlarkin and me
help from art,toby,jordan and several others
ok jordan, go for it deraadt


# 1.129 10-Feb-2009 jordan

Fix null pointer for shutdown if acpi not enabled
ok marco@


# 1.128 04-Feb-2009 kettenis

Some systems that claim to be ACPI 2.0 apparently don't provide an XSDT
address. Make the code fall back on the RSDT on those systems.

From Joerg Niendorf <p3y80@internode.on.net>. Fixes PR 5986.


# 1.127 17-Jan-2009 krw

A couple of stray malloc/memset -> malloc(M_ZERO), a bit of knf and
lose a couple of unnecessary casts. No functional change.

Feedback from kettenis@

ok kettenis@ marco@


# 1.126 06-Nov-2008 marco

Collapse all sanity checks of hids into one function.

Originaly from jordan


Revision tags: OPENBSD_4_4_BASE
# 1.125 02-Jul-2008 fgsch

add acpivideo. for now almost an empty skel so others can work on
suspend/resume. eventually it will also manage output switching and
brightness where supported. prodded by marco@


# 1.124 11-Jun-2008 marco

Fix $OpenBSD$ for once and for all


# 1.123 08-Jun-2008 deraadt

aaa_dev was being set from an uninitialized variable; ok toby


# 1.122 07-Jun-2008 marco

Kill ACPIVERBOSE. It was never used and is worthless to boot.

comments jmc
ok toby


# 1.121 06-Jun-2008 marco

Shrink acpi some more for floppies and while here re-enable acpi on the
floppies. Fixes crazy sony vaio kettenis has.

tested by deraadt
ok deraadt jordan kettenis


# 1.120 01-Jun-2008 marco

Fix \n nit in SMALL_KERNEL.


# 1.119 01-Jun-2008 marco

Re-enable new parser. Lots of testing by various folks. Fits all media
now too.

ok kettenis deraadt


# 1.118 16-May-2008 dlg

back out jordans new parser.

it didnt have enough oks from the right people, it did break machines
that were previously working, and it bloated the RAMDISKS and install
media beyond the constraints of the media.

requested by deraadt@


# 1.117 14-May-2008 jordan

Adding new ACPI Parser code
Old guts of code still exists, needs to be torn out next
ok marco@
ok brad@


# 1.116 27-Apr-2008 jcs

add acpithinkpad(4), an acpi support driver for ibm/lenovo
thinkpads.

provides functionality that the bios did on older, non-acpi
thinkpads like respond to screen brightness keys and toggle the
bluetooth device on and off.

ok deraadt@ and marco@


# 1.115 24-Apr-2008 jsing

Introduce acpiasus(4), a driver for the ACPI based hotkeys found in many
ASUS laptops (including the ASUS EeePC) - largely based on NetBSD's
asus(4) driver. On the ASUS EeePC this allows us to enable/disable
wireless, change screen brightness and use the volume keys.

ok jsg@, weingart@


# 1.114 12-Apr-2008 kettenis

Get rid of acpi_s5 global variables; simply send SIGUSR2 instead of SUGUSR1
to tell init(8) to power down the machine.

ok krw@


Revision tags: OPENBSD_4_3_BASE
# 1.113 21-Jan-2008 jsg

Check against RESET_REG_SUP in FADT before doing the acpi reset dance
after all, not doing so appears to break more machines than it fixes.

ok marco@


# 1.112 05-Jan-2008 kettenis

Never attach more acpicpu(4) devices than cpu(4) devices. Always attach
the first acpicpu(4) device on non-MULTIPROCESSOR kernels, regardless of its
ACPI processor ID.

ok mikeb@


# 1.111 05-Dec-2007 deraadt

Hang acpi(4) below bios(4) instead of mainbus(4). This lets us move acpi(4)
before pcibios(4), and then.... if acpi(4) attaches, skip pcibios(4) since
it messes the machines up. Fixes claudio's HP dl320 G5, and almost assuredly
others.
ok kettenis


# 1.110 05-Dec-2007 deraadt

repair acpiopen/acpiclose and friends to handle the /dev/apmctl interface,
so that apmd(8) can actually work correctly against acpi(4), and thus,
even do the fancy -C/-A stuff; also tested by jmc


# 1.109 02-Dec-2007 fgsch

fix the trailing space for real now.


# 1.108 02-Dec-2007 fgsch

avoid trailing spaces on output; marco@ ok.


# 1.107 27-Nov-2007 kettenis

Fix off-by-one that made us attach acpicpu0 and acpicpu1 on GENERIC kernels.

ok marco@


# 1.106 25-Nov-2007 jsg

When available and ACPI is enabled use the ACPI reset method
to reboot. This is required for some quirky sis machines
and likely others to reboot.

Tested by many. 'commit early commit often' deraadt@


# 1.105 19-Nov-2007 deraadt

sigh, amd64 still uses that stupid acpi_enabled variable...


# 1.104 19-Nov-2007 deraadt

softc's come initialized to 0, you know...


# 1.103 19-Nov-2007 deraadt

store revision; from jsg


# 1.102 19-Nov-2007 kettenis

Initialize sc_facs earlier.

ok deraadt@, beck@, canacar@


# 1.101 16-Nov-2007 deraadt

fix the bus_space #define nightmare, so that amd64 and i386 are much more
uniform. as a result shared code like acpi needs less #ifdef's
ok marco kettenis


# 1.100 15-Nov-2007 deraadt

remove the #ifdef ACPI_ENABLE. the EC codepath is either safe enough (and
will be improved), or it is unsafe because AML assumes it must be there
ok almost everyone


# 1.99 15-Nov-2007 mikeb

Make acpicpu(4) attach to the existing CPUs only.

Problem reported by form@. Fix idea by kettenis@.
Lots of help from deraadt@. Tested by deraadt@, form@ and me.

ok deraadt marco


# 1.98 14-Nov-2007 deraadt

spacing


# 1.97 12-Nov-2007 deraadt

correct : vs , in attach-time messages


# 1.96 06-Nov-2007 deraadt

all ACPI_ENABLE kernels need acpi_interrupt()


# 1.95 06-Nov-2007 deraadt

move acpi_read_pmreg() and acpi_write_pmreg() out of SMALL_KERNEL so
that acpi ramdisks can compile (obvious solution)


# 1.94 05-Nov-2007 canacar

Establish the acpi interrupt earlier. Fixes problems with some
GENERIC.MP kernels not getting acpi interrupts. This is a
workaround until the real issue with late establishment is fixed.
ok weingart@, beck@, gwk@


# 1.93 03-Nov-2007 jordan

Added support for displaying wakeup devices
ok beck@,weingart@,gwk@


# 1.92 11-Oct-2007 kettenis

When walking the tree yo find _PRT methods, check whether the device is
actually enabled and functioning. Similar to what we do for _INI.

ok weingart@, beck@


# 1.91 08-Oct-2007 krw

More simple memset(,0,) -> M_ZERO changes. In this batch move to
size(*p) as the first malloc() parameter where p is declared locally
and thus easy to check. Add M_ZERO to gpe_table allocation in acpi.c
even though there is no obvious bzero or memset nearby.


# 1.90 13-Sep-2007 mk

Remove unnecessary memset() (see aml_evalnode(9)) and a bit of KNF.

ok toby


# 1.89 13-Sep-2007 weingart

Implement ACPI 6.5.1 spec tree walk for _STA and _INI.
Fixes mk's laptop. No regressions so far (thank you to the
testers).

ok gwk@, mk@, marco@


Revision tags: OPENBSD_4_2_BASE
# 1.88 17-Apr-2007 mk

Now that acpidock no longer hangs machines during boot (at least
according to the reports I've been getting) it should be safe to enable
it again, so remove the #if 0 stuff around it.

ok marco


# 1.87 11-Apr-2007 jordan

Added changes for C-State
ok marco@


# 1.86 26-Mar-2007 marco

Calculate minutes left more accurately.

From giovanni <qgiovanni@gmail.com>


Revision tags: OPENBSD_4_1_BASE
# 1.85 03-Mar-2007 mk

Stop looking for acpidock until we fix that stuff that breaks all pre-60
series ThinkPads with acpidock(4) enabled.


# 1.84 22-Feb-2007 marco

We were getting no interrupts in .MP. There are 2 fixes; one in
acpi_machdep.c and move the interrupt establishment down after ioapic
has been initialized.

ok jordan


# 1.83 21-Feb-2007 kettenis

Make an acpi-enabled GENERIC.MP work on systems that only support PIC mode.

ok marco@


# 1.82 19-Feb-2007 jordan

Added acpi_map_address function for mapping GAS/IO registers
ok marco@


# 1.81 18-Feb-2007 jordan

Added dynamic perfect hash for opcodes
ok marco@


# 1.80 18-Feb-2007 marco

Shave off another K pre compression.
Shuffle SMALL_KERNEL functions around so that we only have a single #ifndef


# 1.79 17-Feb-2007 marco

Add SMALL_KERNEL throught acpi to make it fit on boot media.
ok deraadt


# 1.78 13-Feb-2007 marco

Add smarter _INI code. This helps with some dissapearing devices.

ok kettenis jordan


# 1.77 31-Jan-2007 gwk

Introduce acpi_hasprocfvs thats "ACPI Has Processor Frequency and Voltage
Scalling" a global flag set by acicpu attach to indicate that ACPI is
handling fvs through the _PSS objects. This will fix disappearing
EST/powernow when booting kernels with acpi enabled, as acpicpu isnt
compiled into GENERIC yet. Also in cases where acpicpu is built in and
legacy EST/powernow work but acpi is missing the _PSS object, the legacy
routines will be used.

tested by dim@, and Nick Nauwelaerts <nick AT nauwelaerts.net>
ok marco@, dim@


# 1.76 22-Jan-2007 mk

Actually look for docking stations by looking for _DCK nodes.

ok marco


# 1.75 02-Jan-2007 marco

On some systems EC must be attached as the first device. Move EC from
generic _HID devices to its own routine to force first attach. Bug found
and debugged by joris@

Code mostly written by joris@


# 1.74 26-Dec-2006 marco

Add polling to devices that require it such as acpibat and acpitz.
Use the same tsleep/wakeup mechanism as the interrupt code to keep it non-
concurrent.


# 1.73 21-Dec-2006 deraadt

even more knf love


# 1.72 21-Dec-2006 marco

Remove some leftover acpiec gpe/interrupt code.


# 1.71 20-Dec-2006 deraadt

oops, name already used


# 1.70 20-Dec-2006 deraadt

rename __acpi_enable_gpe(); ok marco


# 1.69 20-Dec-2006 deraadt

acpi_unmap_pmregs() is not used, ok marco


# 1.68 19-Dec-2006 deraadt

simplify some low-level methods substantially (shrinking code); ok marco jordan


# 1.67 06-Dec-2006 mk

malloc(..., M_NOWAIT) with no NULL check is bad. If allocation fails,
complain, clean up, and bail out of attach.

input and ok gwk


# 1.66 28-Nov-2006 jordan

Fixed clearing arg for acpi_init_pic
ok kettenis@


# 1.65 27-Nov-2006 kettenis

Don't spam dmesg.


# 1.64 27-Nov-2006 jordan

Fixes for acpiprt routing
Post-parse fixup for namerefs
ok kettenis@


# 1.63 25-Nov-2006 marco

Don't handle acpiev events inside the gpe handler since this causes deadlocks.

ok jordan@


# 1.62 15-Nov-2006 kettenis

Hook up acpi apic interrupt routing support to generic acpi code.

ok jordan@


# 1.61 02-Nov-2006 marco

Kill acpiec attach function and add its functionality to the generic _HID
device attach

prodded and ok jordan


# 1.60 02-Nov-2006 marco

Fix comments to reflect reality.


# 1.59 19-Oct-2006 marco

Replace ACPI ioctls with APM ioctls. This makes ACPI talk the same protocol
as APM therefore userspace can now retrieve power information in the same
fashion as all other apm arches (zaurus, macppc, i386 etc).

To use force the use of acpi device like so: apmd -f /dev/acpi

ok jordan@


# 1.58 19-Oct-2006 jordan

Fixed GPE debug levels
Fixed ref counter on temporary bufferfields
DSDT parsing fully working on IBM Thinkpad
ok marco@


# 1.57 15-Oct-2006 jordan

Removed old GPE handler code remnants
ok marco@


# 1.56 12-Oct-2006 jordan

Massive ACPI changes
Fixed ACPI leaks
new ACPI API
ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.55 30-Jun-2006 gwk

KNF, No functional change,
"go at it" marco@


# 1.54 30-Jun-2006 jordan

Support for calling Embedded Controller via new GPE interface
ok marco@


# 1.53 30-Jun-2006 jordan

Added new code for GPE handling
Also works with ACPIEC; need to fixup acpiec.c to remove original interrupt handler
ok marco@


# 1.52 30-Jun-2006 jordan

Added offset argument for acpi read/write pmreg routines (new GPE code)
ok marco@


# 1.51 31-May-2006 todd

do not call acpi_powerdown() unless acpi has successfully initialized
ok gwk@ canacar@


# 1.50 29-May-2006 canacar

Add embedded controller support.
ok marco@ deraadt@ testing & ok gwk@


# 1.49 19-May-2006 canacar

Minimal thermal zone driver. Monitors thermal zone temperature,
shuts down the system if the 'critical temperature' is reached.
ok marco@


# 1.48 11-Apr-2006 marco

KNF.


# 1.47 11-Apr-2006 gwk

Add acpi_resume(struct acpi_softc *sc), which calls some AML methods,
detected at attach time, currently unused.

ok marco@


# 1.46 11-Apr-2006 gwk

Discover some ACPI sleep related methods, and call them at the
appropriate time in acpi_enter_sleep_state().

ok marco@


Revision tags: OPENBSD_3_9_BASE
# 1.45 26-Feb-2006 marco

Add acpicpu as a device.

ok jordan@


# 1.44 23-Feb-2006 marco

remove dead function.

ok jordan@


# 1.43 22-Feb-2006 marco

Print out all tables it found during autoconf. We'll need this data in
the future.


# 1.42 22-Feb-2006 marco

Make dmesg less verbose.

ok jordan.


# 1.41 22-Feb-2006 jordan

Added parsing of SSDT tables
ok marco@


# 1.40 22-Feb-2006 jordan

Added new aml_register to notify by device id
Moved powerdown code to button device
ok marco@


# 1.39 21-Feb-2006 marco

Readjust debug levels that crept in too low over the last few days.
KNF


# 1.38 21-Feb-2006 marco

Rig the power button so that the machine shuts down correctly.


# 1.37 20-Feb-2006 marco

Add acpibtn device. Remains disabled for now.


# 1.36 19-Feb-2006 jordan

Added code for aml_notify GPE notification callback
ok marco@


# 1.35 19-Feb-2006 grange

Fix acpi_enter_sleep_state() by replacing current ugly hack
with mostly correct sleeping state transition code.

ok brad@ marco@


# 1.34 19-Feb-2006 marco

jordan:
Enable interrupts.
Added peripheral code to make this work.

marco:
Replace softintr and other types of interrupts with a handler with context.
Fixed interrupt magic for GPEs.
Some KNF

This is work done by jordan and I but has to be a single commit.


# 1.33 17-Feb-2006 marco

Remove earlier introduced delays. We really only need them during autconf.

ok jordan@


# 1.32 17-Feb-2006 marco

Add a generic delay function as per the ACPI spec.

from jordan@


# 1.31 17-Feb-2006 marco

Use tsleep instead of delay when !cold.


# 1.30 17-Feb-2006 marco

Argh, we need delays between reads/writes. The backend reads (i2c most likely)
are so slow that values come back as 0 sometimes. We have to do this
despite the spec saying we shouldnt have to. Guess having a faster
parser than others can hurt sometimes.

Idea from jordan.


# 1.29 17-Feb-2006 jordan

Fixed code for handling _INI
Rearranged debug statements
ok marco@


# 1.28 16-Feb-2006 jordan

Added stub for calling _INI
Fixed aml_dowhile for NULL return
ok marco@


# 1.27 16-Feb-2006 jordan

Fixed parser/evaluator to retrieve battery info+state
Cleanup of parser
Dell laptops return battery status
ok marco@


# 1.26 06-Feb-2006 brad

fix a typo so this will compile on amd64.


# 1.25 04-Feb-2006 marco

When ACPI_ENABLED wasn't set but the devices were it had an unknown variable.

Pointed out by ian@.


# 1.24 03-Feb-2006 jordan

Rewrote aml parser yet again...
Fixed bus_space_map for i386/amd64
Added _BST and _BIF to acpibat to get status/info
ok marco@


# 1.23 20-Jan-2006 jordan

More evaluator changes
Removed old evaluator code
Fixed _HID value
ok marco@


# 1.22 19-Jan-2006 jordan

Added support for read/write of PCI Config space
Help jason@
ok marco@


# 1.21 18-Jan-2006 jordan

Added new evaluation routines
Added helper methods for buffer/field evaluation
Fixed broken indentation on dsdt.c

ok marco@


# 1.20 17-Jan-2006 jordan

Major overhaul of the aml parser. Evaluater temporarily disabled.

ok marco@


# 1.19 06-Jan-2006 grange

Check for ACPI control availability only if ACPI_ENABLE defined.


# 1.18 06-Jan-2006 grange

Informative error messages on attach, prettier dmesg.


# 1.17 06-Jan-2006 grange

printf -> dnprintf conversion for debug messages.


# 1.16 05-Jan-2006 grange

Find SLP_TYPx values and store them for future use in
acpi_enter_sleep_state().


# 1.15 05-Jan-2006 grange

Random KNF and cleanup


# 1.14 05-Jan-2006 grange

Provide symbolic names for the acpi sleeping states and use it.


# 1.13 28-Dec-2005 marco

Moved DEVNAME to a acpidev.h

ok jordan@


# 1.12 18-Dec-2005 sturm

add back comment about wrong code

ok marco


# 1.11 16-Dec-2005 marco

Remove test for kbd_reset. Prompted deraadt@, ok jordan@


# 1.10 16-Dec-2005 jordan

Added hooks for powerdown/power button press


# 1.9 16-Dec-2005 jordan

Cleaned up common power management register i/o
Added enter sleep state ioctl
Hooked power button interrupt
ok marco@


# 1.8 16-Dec-2005 marco

Remove autoconf chatter


# 1.7 16-Dec-2005 jordan

Moved DSDT AML parsing into parent ACPI driver
ok marco@


# 1.6 14-Dec-2005 marco

Add AC device glue.


# 1.5 14-Dec-2005 marco

Adjust debug printing levels to something more sane.


# 1.4 13-Dec-2005 marco

Add glue to attach devices to apci.


# 1.3 07-Dec-2005 marco

KNF


Revision tags: OPENBSD_3_8_BASE
# 1.2 10-Jul-2005 grange

Spacing cleanup.


# 1.1 02-Jun-2005 tholo

Start on a basic ACPI framework -- does not do much more than read out the
ACPI tables into kernel memory and attach ACPI and HPET timers currently.

In order to test this code, enabling the devices in GENERIC as well as
the ACPI_ENABLE option is needed. This code does not do any thermal
control yet, so this should be done with care depending on the platform.

In the tree so more people can contribute to making this more fully
featured.

Ok niklas@ grange@ tedu@


# 1.350 30-Jun-2018 kettenis

Add intr_enable() function, intended for MI use to amd64 and i386 and use
this in the acpi(4) suspend/resume code paths.

ok deraadt@


# 1.349 29-Jun-2018 kettenis

Use <stdint.h> types.

ok mpi@, jung@, krw@, deraadt@


# 1.348 29-Jun-2018 mlarkin

ACPI: Allow (with warning) GPE handler reassignment, instead of returning
a failure code.

ok kettenis


# 1.347 26-Jun-2018 mlarkin

knf


# 1.346 26-Jun-2018 kettenis

Let acpi(4) pass down the bus dma tag.

ok deraadt@


# 1.345 25-Jun-2018 kettenis

Reorganize acpi(4) code a bit in preparation for upcoming arm64 support.

ok deraadt@


# 1.344 20-May-2018 kettenis

Wake up the acpi thread after scheduling a gpio event.

ok mpi@


# 1.343 17-May-2018 kettenis

Sprinkle some #ifndef SMALL_KERNEL such that RAMDISK kernels cuild again.

pointed out by stsp@


# 1.342 17-May-2018 kettenis

Implement GenericSerialBus OpRegion support.

ok mlarkin@


# 1.341 27-Mar-2018 kettenis

Add acpicmos(4), a driver that implements SystemCMOS OperationRegion
access support. This fixes machines where the AML doesn't check whether
support for this OperationRegion type has been registered by the OS.

ok mlarkin@


Revision tags: OPENBSD_6_3_BASE
# 1.340 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.339 10-Feb-2018 deraadt

Syncronize filesystems to disk when suspending. Each mountpoint's vnodes
are pushed to disk. Dangling vnodes (unlinked files still in use) and
vnodes undergoing change by long-running syscalls are identified -- and
such filesystems are marked dirty on-disk while we are suspended (in case
power is lost, a fsck will be required). Filesystems without dangling or
busy vnodes are marked clean, resulting in faster boots following
"battery died" circumstances.
Tested by numerous developers, thanks for the feedback.


# 1.338 08-Feb-2018 deraadt

After flushing disks, try again to release all memory. We don't need to
save dirty memory to the hibernate space.


# 1.337 08-Feb-2018 deraadt

Toss all releaseable memory, because fragmentation can get in the way
of allocating the hibernate playpen.


# 1.336 08-Feb-2018 deraadt

sr_quiesce() is a new approach for ensuring that softraid drains
output to the disks.

This is part of a larger suspend/resume filesystem-safety diff, which
has been worked on for a couple of months already. Tests by job, krw,
beck, benno, and others. Sometimes even by snapshot users...


# 1.335 29-Nov-2017 kettenis

Add support for GPIO-signaled events.

ok mlarkin@


# 1.334 16-Nov-2017 jcs

Add PCI attachment for dwiic(4) needed by Intel 100 Series machines

ok kettenis


# 1.333 14-Nov-2017 kettenis

Add a few more PNP IDs to the skip lists.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.332 17-Aug-2017 stsp

Make dwiic(4) attach its ACPI parent dependencies.
This change makes the Asus E200HA keyboard work.

Original analysis and patch by Cesare Gargano
ok kettenis@


# 1.331 09-Aug-2017 dcoppa

Add "PNP0303" (8042 PS/2 Controller) to acpi_isa_hids[]

ok kettenis@


# 1.330 22-Jul-2017 jcs

acpibat: look for _BIX first before falling back on _BIF

ACPI 4.0 deprecated _BIF for battery status so some newer machines
are exporting _BIX which extends _BIF's package with a few extra
fields. When using _BIX, export the new discharge cycle count as a
sensor.

feedback from kettenis and anton
ok deraadt


# 1.329 20-Jul-2017 kettenis

Start a list of isa(4) device names such that we can avoid printing them
as "not configured".

ok mikeb@, millert@, deraadt@


# 1.328 08-Apr-2017 deraadt

Sensors are run as callbacks inside tasks. During suspend a sensor
could be running inside a driver that will be force-detached, or due
to tsleep end up disrupting the softstate/hardstate contract. At
suspend time, quisce all these callbacks by waiting for completion.
This issue has never been observed for real, but may be implicated in
suspend/resume failures.
ok kettenis guenther mlarkin


# 1.327 08-Apr-2017 deraadt

Two prototypes for acpi_maptable; merge to one place


Revision tags: OPENBSD_6_1_BASE
# 1.326 27-Mar-2017 deraadt

Now that hibernate_alloc() only has clean success/failure, don't
need to call hibernate_free() to clean up a partial mess.
ok mlarkin kettenis


# 1.325 13-Mar-2017 deraadt

allow ramdisks to build; ok jcs


# 1.324 12-Mar-2017 jcs

try to attach acpisbs first and if successful, don't attach acpibat


# 1.323 02-Mar-2017 natano

Add a new sysctl machdep.lidaction. The sysctl works as follows:

machdep.lidaction=0 # do nothing
machdep.lidaction=1 # suspend
machdep.lidaction=2 # hibernate

lidsuspend is just an alias for lidaction, so if you change one, the
other one will have the same value. The plan is to remove
machdep.lidsuspend eventually when people have upgraded their
/ets/sysctl.conf.

discussed with deraadt, who came up with the new MIB name
no objections mlarkin
ok stsp halex jcs


# 1.322 28-Feb-2017 natano

Untangle abstract sleep modes from ACPI sleep states. This paves the way
for further work in the area.

idea and ok deraadt
ok mlarkin


# 1.321 22-Feb-2017 jcs

add acpisbs, an acpi smart battery subsystem driver reading data
over smbus

currently disabled because it conflicts with acpibat

ok deraadt, kettenis


# 1.320 14-Jan-2017 kettenis

Revert aml_rdpciaddr changes; breaks several machines that were working
before.


# 1.319 11-Jan-2017 jsg

Increase the size of the buffers used for _HID and _CID strings to be
able to fit "Hyper_V_Gen_Counter_V1".

ok mlarkin@ deraadt@


# 1.318 08-Jan-2017 kettenis

Make aml_rdpciaddr() work for for devices behind bridges. The code now
uses the mapping between AML nodes and PCI devices that we establish.
Because _INIT methods may end up calling aml_rdpciaddr(), make sure we
create that mapping early. Also handle devices that aren't actually present.
These devices are now included in the mapping and reads will return an
all-ones patterm whereas writes are a no-op..

ok mlarkin@


# 1.317 25-Oct-2016 pirofti

Add the resource index to aml_parse_resource() callback function.

This is needed by an upcoming acpiec commit that handles machines breaking
the current ACPI specifications.

Change suggested by and ok kettenis@, guenther@


# 1.316 18-Sep-2016 guenther

If the APIC table (MADT) isn't present then ci_acpi_proc_id won't be set
when acpicpu tries to attach, leading to a NULL dereference. The ACPI
processor object on the Dells where this was reported is bogus and useless
anyway so apply the same test when deciding whether to attach to processor
objects as acpicpu would when matching them to cpus.

Problem reported by Eivind Eide (xenofil (at) gmail.com) and
Johan Huldtgren (johan+openbsd-bugs (at) huldtgren.com).
Testing by Eivind Eide.
ok kettenis@ mlarkin@


# 1.315 03-Sep-2016 naddy

Write the system time back to the RTC every 30 minutes.
This fixes the problem that long-running machines which were not
shut down properly would reboot with a badly offset system time.

hints and ok kettenis@


# 1.314 31-Aug-2016 mlarkin

Don't put a device in the wake device list if _STA says it's not there.

Fixes at least one machine that woke up from sleep / poweroff immediately.

ok kettenis@, tested by kettenis and martijn@


# 1.313 28-Jul-2016 kettenis

Store the acpi processor ID/UID in struct cpu_info, and use it to attach
acpicpu(4) drivers to the right cpu(4).

ok mlarkin@, guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.312 10-Jun-2016 kettenis

The power management timer has been made optional in ACPI 5.0A, so don't
attempt to attach acpitimer(4) if the timer isn't present.

Pointed out by jcs@

ok jcs@


# 1.311 08-May-2016 kettenis

Make sure devices listed by a device's _DEP method are attached before
we attach the device itself. The _DEP methode, introduced in ACPI 5.0,
is a hint that the device in question depends on OpRegion support from
the devices listed by _DEP, so we have to order them the proper way.
To prevent us from attaching those devices again when we encounter them
later walking down the device tree, keep track of our attempts to
attach a device driver to them using aflag in the aml_node structure.

ok guenther@


# 1.310 25-Apr-2016 pirofti

We attach acpihpet(4) and acpiec(4) without using the HID, so do not
report it in the dmesg.

OK deraadt@, mlarkin@.


# 1.309 02-Apr-2016 kettenis

Reduce dmesg spam by knocking out some acpi hardware IDs that don't need a
driver.

ok mpi@


# 1.308 29-Mar-2016 kettenis

Check if a device is present (using _STA) in acpi_foundhid. This prevents
us from reporting devices as "not configured" that aren't actually present,
and allows us to remove duplicated code from several drivers.

ok deraadt@, ok jsg@


# 1.307 29-Mar-2016 kettenis

Implement a cleaner way to prevent acpivout(4) from attaching if we have
a platform-specific method of controlling the backlight.

ok millert@, mlarkin@, jung@


# 1.306 28-Mar-2016 kettenis

Adding each and every new acpi device driver to acpi_foundhid() is
gettinga bit of the burden. And it really isn't how our autoconf
framework is supposed to work for busses that can be enumerated. All
the drivers already check for a matching _HID in their attach
function. So we can just drop the checks in acpi_foundhid() and let
the drivers sort themselves out.

Print a "not configured" line for devices for which we don't attach adriver.
This may be a bit much as there are devices for which we will not have a
driver. This will be removed if it causes too much dmesg spam.

ok deraadt@, mlarkin@


Revision tags: OPENBSD_5_9_BASE
# 1.305 17-Jan-2016 jsg

Take acpi_foundhid() outside of #ifndef SMALL_KERNEL so the ramdisk
kernel can find and attach dwiic(4) and sdhc(4).


# 1.304 17-Jan-2016 jsg

remove an unused global variable


# 1.303 14-Jan-2016 kettenis

Attach dwiic(4) on the Intel Bay Trail i2c controllers as well.


# 1.302 12-Jan-2016 kettenis

Improve emulation of PM registers on Hardware-redcued ACPI by emulating
SPL_TYPx, SLP_EN and WAK_STS using the new SLEEP_STATUS_REG and
SLEEP_CONTROL_REG registers. Makes the transition into S5 (powerdown) work
on the ASUS X205TA.

ok deraadt@, mlarkin@


# 1.301 12-Jan-2016 jcs

Add dwiic, a driver for the Synopsys DesignWare i2c controller found
on the Samsung ATIV Book 9 laptop. This initial version only
supports ACPI config/attachment.

Add ihidev, a HID-over-i2c driver largely based on uhidev. dwiic
handles attaching ihidev devices found in ACPI.

Add ims, a HID-over-i2c mouse/trackpad driver to get basic cursor
and button functionality from HID-compliant i2c trackpads.

ok kettenis deraadt


# 1.300 11-Jan-2016 kettenis

Make sdhc(4) attach to hardware IDs "80860F14" and "PNP0FFF".


# 1.299 09-Jan-2016 kettenis

Initial support for Hardware-Reduced ACPI. This skips the legacy mode checks
by emulating the PM_CNT register such that SCI_EN is always enabled. It also
skip attaching acpitimer(4) since the relevant registers are no longer there.

ok deraadt@, mlarkin@


# 1.298 06-Jan-2016 kettenis

Follow the spec and check the SCI_EN bit to see if ACPI has already
been enabled and don't bother enabling it again if it is. This should fix
systems that don't implement legacy mode and therefore set ACPI_ENABLE and
ACPI_DISABLE to zero.

ok mlarkin@, yasuoka@


# 1.297 23-Nov-2015 reyk

Back out the CRITICAL change, turn it back from 10% to 25%. Some
people with old/broken batteries didn't like this change, and the
underlying heuristics are wrong.


# 1.296 22-Nov-2015 reyk

The "CRITICAL" APM battery level, that is exported/faked by the ACPI
stack, is currently set to 25%. With 25% battery left, a typical
modern laptop will have at least one hour runtime left. It is an
arbitrary value, and other APM power layers in the kernel set CRITICAL
to something like 5% or 10%, so set it to 10% for now.

OK mlarkin@


# 1.295 28-Sep-2015 deraadt

In low-level suspend routines, set cold=2. In tsleep(), use this to
spit out a ddb trace to console. This should allow us to find suspend
or resume routines which break the rules. It depends on the console
output function being non-sleeping.... but that's another codepath which
should try to be safe when cold is set.
ok kettenis


# 1.294 06-Sep-2015 deraadt

clarify malloc arguments (base + length...)


# 1.293 28-Aug-2015 deraadt

fairly simple sizes for free(); ok tedu


# 1.292 20-Aug-2015 kettenis

Respect the access size when reading or writing to pci config space.
This version also makes sure writes are properly aligned, preventing
the panics seen with the previous incarnation of this change.

Fixes battery status passthrough in vmware, brightness keys on my x220
and who knows what else.

ok mlarkin@, deraadt@


Revision tags: OPENBSD_5_8_BASE
# 1.291 04-Aug-2015 deraadt

If we are setting ACPI_PM1_BM_RLD do it only once, and remember to
repeat this action at resume time.
ok mlarkin guenther


# 1.290 29-Jul-2015 deraadt

Something subtle in previous revision is locking up suspend or booting
on some laptops. Mailing lists threads mention x220 and Asus machines.
Reverting for now.


# 1.289 21-Jul-2015 kettenis

iRespect the access size when reading or writing to pci config space.
Fixes battery status passthrough in vmware and who knows what else.

Tested by many.

ok mlarkin@, deraadt@


# 1.288 17-Jul-2015 mlarkin

fix comments


# 1.287 26-Mar-2015 jsg

Use the 0x10 extended capability to determine if the trackstick
buttons are wired to the trackpad and need to be re-routed to the
trackstick. Without this change the buttons on 2015 thinkpads
get picked up as extended buttons that show up as scroll up/down.

Remove the X1 Carbon 2015 (LEN0048) and X250 (LEN0046) from the
top button area/soft buttons quirks list. Also avoid using the
quirk list entirely if the capability bit is set.

Based on work from jcs@ with feedback from mpi@
ok mpi@ jcs@


# 1.286 20-Mar-2015 kettenis

The ACPI standard says that a PCI function number of FFFF can be used to refer
to all functions on a device. This makes no sense in the context of accessing
PCI config space. Nevertheless stsp@ has a machine with AML that tries to
do config space reads from a device where _ADR encodes function number FFFF.
This is almost certainly buggy AML, but since Linux (and presumably Windows)
boot just fine on this machine we need to work around this. Linux effectively
treats function FFFF as function 7, which isn't present on this machine.
Instead we simulate a read from a non-existent device by returning a bit
pattern of all ones. Writes to function FFFF will still panic.

ok mlarkin@


# 1.285 16-Mar-2015 deraadt

Reduce use of SMALL_KERNEL in acpi, bringing acpiec driver and more gpe
handling into RAMDISK. This is now possible because the install media
has ample room. The goal is to reduce special cases where we may be
skipping (unknown) important operations...
ok mlarkin kettenis


# 1.284 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.283 07-Feb-2015 deraadt

New framework that allows hibernate to pass in entropy from it's fresh
boot.
ok mlarkin


# 1.282 06-Feb-2015 mlarkin

Fix a hibernate crash on some machines due to unmapping a page that
may not have been mapped previously (in the failure to hibernate case).

Also ensure that the lowmem ptp is mapped in all cases (not just MP).

ok kettenis


# 1.281 17-Jan-2015 deraadt

resume_randomness() before spinning up other cpus...


# 1.280 15-Jan-2015 jsg

Define a new wscons mouse type for Synaptics clickpad devices that lack
physical buttons. This will be used if the acpi pnpid for the mouse
matches a list provided by Synaptics found in Linux.

Combined with the xenocara changes this will remove the need for an xorg
config file for the x240/t440/t540 generation of thinkpads.

ok matthieu@ shadchin@ kettenis@


# 1.279 11-Jan-2015 kettenis

Tolerate checksum errors in ACPI tables (but complain about them). Apparently
there are machines out there with broken BIOSen with such checksum errors in
thier RSDT tables. Windows runs fine on these machines, so it probably does
not check. Linux ignores checksum errors as well (and prints a warning).

ok deraadt@, krw@


# 1.278 18-Dec-2014 deraadt

suspend and resume the RNG (at what looks like the right spots)
ok reyk djm


# 1.277 09-Dec-2014 doug

Replace some malloc(n*size,...) calls with mallocarray().

ok tedu@ deraadt@


# 1.276 06-Dec-2014 mpi

If "machdep.lidsuspend" is not set, do not go back to sleep if the lid
is still closed when resuming. This allows docked laptop to be resumed
without opening them.

ok mlarkin@


# 1.275 23-Nov-2014 mlarkin

If a machine has no _LIDs, then none can be open. This diff fixes a problem
where machines without any _LIDs were going back to sleep after hibernate
due to the recent suspend-if-lid-closed-on-resume diff. Noticed on qemu,
which doesn't have any _LIDs.

discussed with deraadt


# 1.274 18-Nov-2014 krw

Nuke yet more obvious #include duplications.

ok miod@


# 1.273 08-Nov-2014 mlarkin

If resuming from sleep (zzz/ZZZ) and the lid is still closed, go back to
sleep. This prevents accidental lid flex or slight opening in a backpack
from waking the machine up and leaving it resumed (powered on).

ok deraadt@


# 1.272 26-Sep-2014 kettenis

Rework piglet and pig allocation. Currently the piglet gets allocated
deep down in the suspend path, where it is really hard to recover from
allocation failure. So allocate the piglet early on in the suspend path.
Also change the piglet and piglet allocation functions to use km_alloc(9)
instead of doing pmemrange magic. This removes a bunch of code which, in the
case of the piglet allocation, is broken since it results in a NULL pointer
dereference. Also switch the piglet allocation to not wait. If we can't
allocate 16MB of phys contig memory on a halfway modern machine we're almost
certainly under a lot of memory pressure and we're better off not trying to
hibernate anyway.

ok mlarkin@


# 1.271 25-Sep-2014 kettenis

Bring back rev 1.249. Now that mp_setperf() has been fixed, it should be
safe to restore perflevel when resuming. Should fix the issues people
see with the processor running at full speed heating up the machine while
the apm command reports that the CPUs are running at a low speed.

ok dcoppa@, deraadt@


# 1.270 23-Sep-2014 kettenis

Release the acpi lock around calls to wsdisplay_suspend() and
wsdisplay_resume(). The X server may adjust the display brightness just
before/after the VT switch when suspending/resuming. In fact, it may
already be blocked adjusting the display brightness for some reason
unrelated to the suspend request.

The VT switch request will eventually time out, but the delay is annoying
and the failed VT switch will lead to problems after resume.

Needed for a future update of the xf86-video-intel driver.

ok mlarkin@


# 1.269 19-Sep-2014 kettenis

Use config_suspend_all(9).


# 1.268 08-Sep-2014 kettenis

Clear the PME Status bit on PCI devices in response to Device Wake events.
Fixes the interrupt storm on the 2009 Mac mini with WOL enabled on its
nfe(4) interface.

ok mlarkin@


Revision tags: OPENBSD_5_6_BASE
# 1.267 20-Jul-2014 mlarkin

Support hibernating to softraid crypto volumes.

much help and ok from deraadt@


# 1.266 16-Jul-2014 mlarkin

Reenable hibernate RLE support and flush+zero all memory after unpack.


# 1.265 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.264 12-Jul-2014 kettenis

Add a function to drop all clean pages on the page daemon queues and call
it when we hibernate.

ok mlarkin@, miod@, deraadt@


# 1.263 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.262 11-Jul-2014 mlarkin

Flush the buffercache to 16MB on hibernate and restore its previous max
size (kern.bufcachepercent) on resume, for better hibernate performance.

ok beck@


# 1.261 10-Jul-2014 blambert

remove unneeded header

ok mlarkin@


# 1.260 01-Jul-2014 kettenis

Adding an additonal delay after each access to SystemMemory or SystemIO
during autoconf makes no sense.

ok deraadt@


# 1.259 23-Jun-2014 kettenis

SystemMemory is used to access memory mapped registers on some machines, so we
must use the bus_space(9) API to access it instead of memcpy(9). Also make
sure we properly align access to fields that need more than byte alignment.

ok guenther@


# 1.258 21-May-2014 mlarkin

Fix invalid / missing format specifiers in several dnprintf statements
(used only when ACPI_DEBUG is enabled)

From a diff on tech@ by Fabian Raetz, with a minor change by me, tested
on i386 and amd64 with ACPI_DEBUG enabled and disabled.


# 1.257 25-Apr-2014 mlarkin

Defer the free of the hibernate structures until later, when it is safe to
do so.

Freeing the hibernate structures too early leads to inadvertent calls into
splx when we aren't ready for them yet. Found when trapping splx calls in
the resume path.

discussed with deraadt@ at m2k14


# 1.256 13-Apr-2014 mlarkin

Fix a badly worded debug message that implied the entire machine was going
to sleep, instead of just the acpi thread going to sleep.


# 1.255 31-Mar-2014 mpi

Change the order of operations during a suspend/resume cycle and
call bufq_quiesce() after executing the DVACT_QUIESCE handlers.

This should be safe since no disk nor controller drivers have such
handler but it will allow us to detach sd(4) devices attached to a
USB bus.

Another benefit pointed out by kettenis@ is that drivers that need
to read a firmware from the disk should be able to do it at resume
time in a DVACT_WAKEUP handler.

ok kettenis@, deraadt@


# 1.254 13-Mar-2014 dlg

get rid of the assumption that the head of the alldevs list is the
"mainbus" device. this breaks when mpath is enabled because it
attaches before mainbus and therefore takes the head position.

have autoconf provide device_mainbus() which looks up mainbus_cd,
and use that instead.

discussed with deraadt who just wants mpath stuff to move forward
despite there being many ways to shine this particular turd.


Revision tags: OPENBSD_5_5_BASE
# 1.253 21-Feb-2014 deraadt

Do the lid control further on in the resume sequence, since this is
AML, and will do WAITOK operations. While cold, that is a bad idea.
We seem to be safe with respect to the spec, I do not see any event
debounce issues here.
ok kettenis, tested by many


# 1.252 20-Jan-2014 claudio

Instead of checking sc->sc_revision <= 1 check that the
fadt->hdr_revision <= 1. This is a better check since it checks
the version of the proper descriptor. This allows the alix APU
to reboot since those boards have currently a wrong sc->sc_revision.
Committing this now so that it can be tested easier.
With and OK jsg@ also fine by derradt@


# 1.251 24-Dec-2013 kettenis

If the FADT has its SMI_CMD set to zero, assume we're only ACPI-only hardware
and don't need to disable SMI ownership of the ACPI hardware registers.

ok mlarkin@


# 1.250 23-Dec-2013 kettenis

Revert previous commit. It makes resume hang on some MP systems running
GENERIC.MP.


# 1.249 16-Dec-2013 kettenis

Restore hw.setperf upon resume like we do for apm(4) on i386. Seems we need
to do this fairly late in the resume to avoid hangs.

ok deraadt@, mlarkin@


# 1.248 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


# 1.247 06-Nov-2013 mpi

Add support for Power Resources for Dx states and the necessary hook
for PCI devices. This hook should be called twice, before and after
changing the power state of a PCI device.

Before setting the device to the new state, the ACPI layer will notify
every power resources linked to the device for that state and make sure
they are turned "_ON". After changing the state of the device, it will
decrement the reference of every power resources linked to that device
for the old state and turn them "_OFF" if they are no longer referenced.

This fixes the no-USB after resume problem seen on various ThinkPad,
problem initialy diagnosed with Alexander Polakov.

ok kettenis@, deraadt@


Revision tags: OPENBSD_5_4_BASE
# 1.246 01-Jun-2013 mlarkin

Stop printing ACPI wakeup devices in dmesg after the 16th wakeup device.

Helps with VMware VMs that report hundreds of wakeup devices.

ok reyk@, deraadt@


# 1.245 31-May-2013 bluhm

Fix the build for a kernel without wd and pciide in its config.
OK deraadt@ tedu@


# 1.244 30-May-2013 deraadt

Enforce ca_activate tree-walks over the entire heirarchy for all events,
cleaning up some shutdown-hook related code on the way.
(A few drivers related to sparc are still skipped at kettenis' request)
ok kettenis mlarkin, tested by many others too


# 1.243 18-Apr-2013 deraadt

pin the acpi thread to the BSP, because AML and SMI cannot be trusted. I
was certain we were doing this already, but it turns out no...


Revision tags: OPENBSD_5_3_BASE
# 1.242 24-Dec-2012 guenther

In acpiioctl(), Return though the splx() path if suser() returns an error

ok deraadt@


# 1.241 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


# 1.240 04-Oct-2012 ehrhardt

Use information provided by ACPI to attach secondary PCI host bridges.

ok kettenis@


# 1.239 07-Sep-2012 kettenis

Add a new function called acpi_pci_min_powerstate() that returns the lowest
power state for a PCI device for the power state (currently S3 or S4) we're
transitioning into. This function is probably too simplistic; there is a lot
of (silly) complexity in the ACPI spec.

ok deraadt@


Revision tags: OPENBSD_5_2_BASE
# 1.238 13-Jul-2012 pirofti

Disable acpivideo(4) on ASUS laptops.

There are some models that panic whenever an event is received.
Some cope, some don't. In order to get this right acpiec(4) requires
major rework. Until then let machines boot.

Discussed with deraadt@.


# 1.237 11-Jul-2012 mlarkin

for now, restrict hibernate to root in the acpi ioctl kernel interface.
normal rules for controlling this interface through apmd still apply.

ok deraadt@


# 1.236 09-Jul-2012 deraadt

tiny little oops in untested hibernate block


# 1.235 09-Jul-2012 deraadt

clean up ioctl() case statements


# 1.234 09-Jul-2012 deraadt

acpi_disable_onegpe() is not used; ok pirofti


# 1.233 24-May-2012 kettenis

If _MIN > _MAX consider a WordBus resource to be invalid. Fixes a panic on
the IBM x3550-M4.

ok mikeb@


# 1.232 29-Mar-2012 mlarkin

Missing #ifdef in previous diff


# 1.231 29-Mar-2012 mlarkin

Prevent hibernation attempts on machines with unsupported disk controllers.
The same check is also performed later in the hibernate sequence, but by
then it's too late to cleanly unwind (presently).

ok deraadt


# 1.230 26-Mar-2012 deraadt

simplify conditionals in acpi_sleep_state()
ok mlarkin


# 1.229 26-Mar-2012 deraadt

hook in the hibernate request code; half of this diff is from mlarkin
ok mlarkin


Revision tags: OPENBSD_5_1_BASE
# 1.228 20-Sep-2011 deraadt

Very early in suspend while the clock is still ticking realtime,
save the clock back to the rtc. The zaurus already did this.
ok phessler miod kettenis


Revision tags: OPENBSD_5_0_BASE
# 1.227 02-Jul-2011 nicm

kqueue attach functions should return an errno or 0, not a plain 1. Fix
the obvious cases to return EINVAL and ENXIO.

ok tedu deraadt


# 1.226 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.225 16-Jun-2011 pirofti

Import acpitoshiba driver.

This is originally written by Hiroyuki Aizu, ported and integrated
in our tree by Javier Vazquez <javama4 AT gmail> with tweaks and
suggestions by me.

Import prodded by deraadt@ and the lack of testers on tech@.


# 1.224 27-Apr-2011 jcs

attach acpithinkpad to newer lenovo models like the x120e

ok deraadt@


# 1.223 22-Apr-2011 jordan

Fix uninitialzied variables and formatting strings (-Wxxx errors)


Revision tags: OPENBSD_4_9_BASE
# 1.222 02-Jan-2011 jordan

Re-add code for ACPI taskqueue, eliminates need for state vars in acpi softc
Serialize processing ACPI tasks for sleep/powerdown/GPE event
ok deraadt@


# 1.221 31-Oct-2010 guenther

Revert last commit: it breaks resume on Thinkpads
"then please back it out" deraadt@


# 1.220 26-Oct-2010 jordan

Add task queue for ACPI gpe and notify handlers
This fixes eject on Dell Latitude dock and an issue on the Dell Mini
battery update.
ok deraadt


# 1.219 07-Oct-2010 jordan

Create separate functions for enabling wake and runtime gpes.
Remove old suspend/resume gpewalk function
ok deraadt, mlarkin


# 1.218 05-Oct-2010 jordan

split acpi_enable_onegpe to separate enable/disable functions
ok deraadt


# 1.217 05-Oct-2010 jordan

Added acpi_disable_allgpes function to clear GPE status
ok deraadt


# 1.216 05-Oct-2010 deraadt

clean up acpi_softc use more


# 1.215 05-Oct-2010 deraadt

delete a bit of #if 0 code


# 1.214 05-Oct-2010 deraadt

hide use of our global acpi_softc pointer a bit better


# 1.213 29-Sep-2010 deraadt

grab the acpi thread lock very early in acpi thread startup; not that it
is likely that anyone will race us... but why be careless.
ok kettenis


# 1.212 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.211 29-Aug-2010 jasper

- add a check for FWRITE here too.

ok miod@


# 1.210 27-Aug-2010 kettenis

Make PCI Power Management optional, and only enable it when acpi(4) attaches.

ok deraadt@


Revision tags: OPENBSD_4_8_BASE
# 1.209 08-Aug-2010 kettenis

Close race between the acpi thread and wscons ioctls running in process
context. We might rip this out post-release and replace it with a solution
that serializes things in a more obvious way.

ok deraadt@, miod@ and "those crazy texans who ok anything"


# 1.208 08-Aug-2010 deraadt

Report the acpi sleep states when on a RAMDISK; ok krw


# 1.207 07-Aug-2010 kettenis

Enforce acpi register access rescrictions.

ok mlarkin@, deraadt@, tested by marco@


# 1.206 07-Aug-2010 deraadt

upon resume, notify all the acpiac and acpibat drivers to update
their status. some laptops do not do this automatically.
ok kettenis canacar


# 1.205 06-Aug-2010 marco

Always call _PSW on _LID devices that support it to enable lid open to
resume the machine. Conversely disable it when the machine wakes up.

Tested by several ok deraadt


# 1.204 06-Aug-2010 deraadt

As part of a revamp of the PM1/GPE code, I write this diff to look for
unmanaged EN & STS bits in the PM1 register at interrupt time and report
them. As a side effect this splits the STS acknowledgement into two writes
(for power, and sleep) instead of one. The printf that is added (to spot
unmanaged STS bits) has not yet been triggered as far as we know.

Before the "write to PM1 registers at the right offset" diff went in, this
was not neccessary. But newer thinkpads do not have a working soft power
button without this diff.

We have no idea why.
ok mlarkin kettenis


# 1.203 05-Aug-2010 deraadt

PM1 power button events were generating 'fake events' to acpibtn PWRB,
but if there is no such device these were being tossed. Instead, arrange
for all power button handlers to flag a softc variable which is processed
by the thread; so that one place will know to request a powerdown (psignal
to init)
ok marco mlarkin


# 1.202 05-Aug-2010 deraadt

rename a variable which is annoyingly poorly named


# 1.201 05-Aug-2010 mlarkin

Write to the correct offset for various ACPI pmreg registers. Writing to
the correct register does properly clear the wake status bits and fixed
event bits, which allows some machines to have more than 1 suspend cycle.

bug spotted by deraadt@

tested by deraadt@ and myself on a half-dozen machines and others on
hackers@

ok deraadt@, marco@


# 1.200 05-Aug-2010 deraadt

Fix the power button. The pm1 register is 16 bits in size.
My mistake, a while back.
tested by krw


# 1.199 04-Aug-2010 kettenis

Print supported sleep states.

ok marco@, phessler@, deraadt@


# 1.198 03-Aug-2010 mlarkin

Use the proper flag for re-enabling certain hardware events (power btn,
sleep btn). The (incorrect) flag prevoiously used coincidentally had the
same value, so this shouldn't cause any different behavior than before.

tested on a variety of machines (i386, amd64, sp, mp) - no behavioral
change seen

ok deraadt@


# 1.197 28-Jul-2010 marco

Make all gasio failures look and behave the same.


# 1.196 28-Jul-2010 mlarkin

Delay a little longer on suspend before giving up. Fixes machines that
sometimes don't suspend because the sleep transition takes too long
(symptoms like failing to sleep but the backlight stays on).

ok deraadt@


# 1.195 27-Jul-2010 deraadt

remove XXX; EC can now be attached early when needed; ok jordan


# 1.194 27-Jul-2010 deraadt

more brackets feels good


# 1.193 27-Jul-2010 deraadt

XXX annotate a block in acpi_enter_sleep_state() that is pretty worrying


# 1.192 27-Jul-2010 deraadt

horrific indentation that nearly made me puke


# 1.191 27-Jul-2010 mlarkin

Set the sleeping indicator light on machines that support it, in case SMI
doesn't do it for us. Shows the 'moon' LED on older Thinkpads when
sleeping.

Tested on T43p, W500, T510 by me and on X61s by deraadt@.

ok deraadt@


# 1.190 27-Jul-2010 jordan

Early initialization of acpiec if ECDT table exists
Fixes hang when booting thinkpads while docked
ok deraadt


# 1.189 26-Jul-2010 deraadt

remove merge error which was commited


# 1.188 26-Jul-2010 pirofti

Add support for Sony ACPI hotkeys via a new driver: acpisony(4).

Currently it only works for the suspend button and tries to do right for
the brightness events, but I haven't found a Sony laptop that like to do
right so far.

In the future I want to make the brightness keys work on all Sony's and
also add support for the zoom hotkeys and whatever other funky keys I can
find on those things.

Okay deraadt@.


# 1.187 22-Jul-2010 deraadt

pretty large cleanup of SMALL_KERNEL


# 1.186 21-Jul-2010 deraadt

in the acpi GPE handler, late ack level interrupts not edge interrupts.
Duh.


# 1.185 21-Jul-2010 deraadt

Merge the edge and level gpe handlers into one (now possible since the
gpe structure tells us whether the gpe is edge or level)


# 1.184 21-Jul-2010 deraadt

delete #if 0 code we are not going to use; ok marco


# 1.183 20-Jul-2010 deraadt

make the mask code for acpi_enable_onegpe() match other similar code


# 1.182 20-Jul-2010 deraadt

Handle the acpi interrupt controller with proper edge/level handling,
wrapped in spltty to avoid racing against the interrupt controller.
Repair the gpe bit masking code while there, and do operations in the
right order
ok kettenis mlarkin, help from jordan at figuring out the order of
operations.


# 1.181 20-Jul-2010 matthew

When we're preparing to go to sleep, only call bufq_restart() if other
preparations failed.

ok kettenis@, thib@, deraadt@


# 1.180 19-Jul-2010 deraadt

permit compilation with ACPI_DEBUG


# 1.179 19-Jul-2010 deraadt

there is no need for a seperate dsdt_softc pointer; dsdt does not have a
softc but is just a part of the single acpi driver.
ok jordan


# 1.178 19-Jul-2010 deraadt

acpi_delay() is no longer needed; ok jordan


# 1.177 18-Jul-2010 mlarkin

Clear event status on resume, to avoid going back to sleep/reboot/shutdown
immediately after resume. Tested by a bunch of people on hackers@ on
various hardware. Fixes at least Toshiba NB200 and Gateway LT3103u.

ok marco


# 1.176 13-Jul-2010 deraadt

rename some thread related interfaces so they are logical


# 1.175 10-Jul-2010 jordan

Verify _ADR bdf is correct (some systems return 0xffff)
Works on Dell x9xx and Studio 1555


# 1.174 09-Jul-2010 jordan

Clear the noise


# 1.173 08-Jul-2010 jordan

Add mapping for ACPI device to PCI bdf (match autoconf tree)
Simplify resource parsing function to use buffer argument
Convert namespace linked lists to use queue macros
ok marco@, deraadt@


# 1.172 06-Jul-2010 deraadt

Use spltty() to lock downcalls from apm against the information being
modified by the acpi thread.
ok kettenis


# 1.171 06-Jul-2010 deraadt

Don't use a workq to do the suspend, because it races aginst the acpi
thread. Instead, just tell the acpi thread to do the suspend for us.
This makes apmd & zzz work correctly. While here, have acpithinkpad
attempt to post the event to apm before prompting the suspend itself.
ok kettenis marco mlarkin


# 1.170 05-Jul-2010 mlarkin

Reset register SCI_EN on resume. The ACPI spec clearly states we don't have
to do this, but many machines don't, or forget to, which leads to all sorts
of problems on resume (broken battery stats, overheating, failure to resume
more than once, etc).

tested by many, ok marco@, deraadt@


# 1.169 01-Jul-2010 thib

do bufq quiesce again, since vnd/the bufq_done call in
biodone has been fixed.


# 1.168 01-Jul-2010 jordan

Backout recent AML changes, breaks on R210 and others


# 1.167 01-Jul-2010 jordan

Simplified aml_parse_resource call, cleanup code
ok marco


# 1.166 01-Jul-2010 jordan

Fix subordinate bus # for multi-root PCI buses
ok marco, kettenis


# 1.165 30-Jun-2010 thib

Disable/partially backout the bufq quiesce changes as this
is causing havoc with vnds and release must be buildable.


# 1.164 29-Jun-2010 deraadt

Fix ramdisks (new code was placed inside a SMALL_KERNEL chunk)


# 1.163 29-Jun-2010 jordan

Add support for mapping ACPI to PCI devices
ok kettenis, deraadt


# 1.162 29-Jun-2010 kettenis

Call bufq_quiesce() on suspend to stop all I/O and bufq_restart on resume.
Makes suspend/resume work much better while doing disk I/O.

ok mlarkin@


# 1.161 29-Jun-2010 tedu

60 is a more reasonable guess for number of minutes in an hour than 100.
spotted by TeXitoi. ok deraadt


# 1.160 27-Jun-2010 jordan

Add _acpi_matchhids routine, precursor to pci device match


# 1.159 27-Jun-2010 mlarkin

Clear ACPI fixed event status on resume (power buttons/etc) so that some
machines don't immediately resume after suspending to S3.

Tested by kettenis@ and I on 5 or 6 machines, deraadt@ "yeah, looks good"


# 1.158 27-Jun-2010 jordan

Cleaned up acpi_maptable, adds to table list directly


# 1.157 07-Apr-2010 deraadt

If we have done a wsdisplay_suspend(), and ACPI decides it does not want to
suspend, we must roll back by calling wsdisplay_resume() or the virtual
console switching will be locked forever
ok oga


# 1.156 07-Apr-2010 kettenis

Make suspend/resume work on MP machines (running an MP kernel). Joint work
from mlarkin@ and me, with some amd64 fixes thrown in by deraadt@

ok marco@, deraadt@, pirofti@, mlarkin@


# 1.155 31-Mar-2010 kettenis

Bring /dev/apm support in line with apm(4). Make the suspend button send
a suspend request event to apmd(8) instead of suspending immediately. Also
keep track of whether /dev/apm and /dev/apmctl are currently open, such that
we can still suspend immediately if apmd(8) isn't running.

ok deraadt@, marco@, pirofti@, jsing@, oga@


# 1.154 30-Mar-2010 oga

Prevent the apmd/x races for good.

When we hit suspend time, go through all wsdisplays on the system. if
they are in mode MAPPED, but not MODE_DUMBFB then if possible do a full
vt switch to a !mapped vt, and prevent switching back until resume time.
This has to be called from MD code because this involves userland
running so that X can run the vt switch signal handler. This way, any
case where we are using the "poke registers from userland" model, we
will not be on the hardware when we go down, so the kernel can actually
handle thing properly.

Tested on several acpi laptops (by kettenis@ and ian@), x40 (me and
beck@ at LEAST) and zaurus (me). Maybe others, but if so I forgot who at
this time..

Idea from deraadt somewhere over the Faroe Islands (I thought of a
similar thing myself a while ago). Much prompting from him. Ok and
comments miod@


# 1.153 25-Mar-2010 oga

Fix & vs. && in the kqfilter code.

Based on a comparison with the apm code.

ok deraadt@, kettenis@


Revision tags: OPENBSD_4_7_BASE
# 1.152 26-Nov-2009 mlarkin

Visual (and on some machines, audio) feedback while the machine is
resuming via calls to aml _SST method. This also makes the "moon" LED on
thinkpads to blink during resume and cease when resume is completed.

ok pirofti@


# 1.151 26-Nov-2009 deraadt

Toggle into "cold" when we we disable interrupts, because tsleeping
in the AML interpreter and context switching into userland processes
tends to sometimes pop the ACPI trampoline page out of our address
space. Makes lots more machines work.
ok kettenis mlarkin


# 1.150 24-Nov-2009 jsg

in the resume path evaluate _WAK before dealing with device drivers
like the spec says, not after.

ok mlarkin@


# 1.149 24-Nov-2009 deraadt

abort suspends on SMP systems until that gets written; ok mlarkin


# 1.148 23-Nov-2009 mlarkin

pci should return the result of it's children's suspend/resume calls back
to its parent.

handle suspend failure case and unwind if devices have an activate function
that returns failure.

ok deraadt@, kettenis@


# 1.147 23-Nov-2009 mlarkin

acpi* devices need to call their children on suspend/resume.
ok deraadt@


# 1.146 23-Nov-2009 pirofti

Remove ACPI_SLEEP_ENABLED checks.

This enables by default the suspend/resume paths in the kernel.

Okay deraadt@.


# 1.145 23-Nov-2009 deraadt

knf and spacing that are super annoying


# 1.144 23-Nov-2009 mlarkin

Hook up Theo's new children suspend/resume function, as well as more ACPI
suspend/resume glue.

ok deraadt@


# 1.143 26-Oct-2009 deraadt

Do not do strncmp comparisons on dv_xname because longer device names which
look similar could arrive in the future. Instead, compare directly against
dv_cfdata->cf_driver->cd_name
Issue originally spotted by miod


# 1.142 26-Oct-2009 jordan

Verify checksum+address when loading ACPI tables.
Some systems had invalid entries in RSDT/XSDT.
ok marco@


# 1.141 23-Jul-2009 cnst

New aibs(4) driver for ASUSTeK AI Booster (ACPI ATK0110) hardware monitoring,
with sensor state support through limits provided by the ACPI.

Tested on several ASUS motherboards kindly networked by Sam Fourman Jr.

ok deraadt marco jordan


Revision tags: OPENBSD_4_6_BASE
# 1.140 03-Jun-2009 pirofti

Import acpi power resource driver. This is needed for suspend/resume logic.

Okay jordan@ and marco@.


# 1.139 03-Jun-2009 pirofti

Add acpivideo support. This adds brightness support for all laptops
except thinkpads, they will use the acpithinkpad driver. The driver is
also hooked into wsconsole. So brightness can be adjusted via:

$ wsconsctl display.brightness=<percentage>

This is very helpfull on some laptops that have a nasty bios and get two
steps instead of one when pressing the brightness button.

Tested on various dell, fujitsu, acer, samsung and other laptops.

Okay marco@, miod@. Suggestions from kettenis@.
Lots of reviews and help from miod@, thanks!


# 1.138 03-Jun-2009 jordan

Added ejectable bay for IDE cdroms
ok marco@


# 1.137 30-Apr-2009 marco

Oga changed apm ioctls and forgot to grep. Temporary work around by hitting
the sleep button when we get any request. This interface will chnage at
some point.
ok oga


# 1.136 19-Apr-2009 krw

Call acpi_sleep_walk() when powering down just like when entering
any other sleep state. This tells acpi wake devices to not wake up
the box until manual powering up resets their status. Makes my Dell
GX520 stay powered off in response to 'halt -p'.

In snaps for a while.

ok marco@ (several times) deraadt@


# 1.135 17-Apr-2009 pirofti

Add device ranking support for future sleep/wake developments.

okay marco@


# 1.134 11-Mar-2009 jordan

Cleanup.. use aml_evalinteger instead of aml_evalname
ok marco@


# 1.133 10-Mar-2009 jordan

Fix cut/paste error.. call _BFS not _PTS
ok marco@


Revision tags: OPENBSD_4_5_BASE
# 1.132 21-Feb-2009 marco

overzealous cut/paste; found by Vladimir Kirillov <proger@uaoug.org.ua>


# 1.131 20-Feb-2009 marco

Oops didn't mean for Debugger() to remain there.


# 1.130 19-Feb-2009 marco

suspend/resume bits so that we can develop this in tree. This is disabled.
code from mlarkin and me
help from art,toby,jordan and several others
ok jordan, go for it deraadt


# 1.129 10-Feb-2009 jordan

Fix null pointer for shutdown if acpi not enabled
ok marco@


# 1.128 04-Feb-2009 kettenis

Some systems that claim to be ACPI 2.0 apparently don't provide an XSDT
address. Make the code fall back on the RSDT on those systems.

From Joerg Niendorf <p3y80@internode.on.net>. Fixes PR 5986.


# 1.127 17-Jan-2009 krw

A couple of stray malloc/memset -> malloc(M_ZERO), a bit of knf and
lose a couple of unnecessary casts. No functional change.

Feedback from kettenis@

ok kettenis@ marco@


# 1.126 06-Nov-2008 marco

Collapse all sanity checks of hids into one function.

Originaly from jordan


Revision tags: OPENBSD_4_4_BASE
# 1.125 02-Jul-2008 fgsch

add acpivideo. for now almost an empty skel so others can work on
suspend/resume. eventually it will also manage output switching and
brightness where supported. prodded by marco@


# 1.124 11-Jun-2008 marco

Fix $OpenBSD$ for once and for all


# 1.123 08-Jun-2008 deraadt

aaa_dev was being set from an uninitialized variable; ok toby


# 1.122 07-Jun-2008 marco

Kill ACPIVERBOSE. It was never used and is worthless to boot.

comments jmc
ok toby


# 1.121 06-Jun-2008 marco

Shrink acpi some more for floppies and while here re-enable acpi on the
floppies. Fixes crazy sony vaio kettenis has.

tested by deraadt
ok deraadt jordan kettenis


# 1.120 01-Jun-2008 marco

Fix \n nit in SMALL_KERNEL.


# 1.119 01-Jun-2008 marco

Re-enable new parser. Lots of testing by various folks. Fits all media
now too.

ok kettenis deraadt


# 1.118 16-May-2008 dlg

back out jordans new parser.

it didnt have enough oks from the right people, it did break machines
that were previously working, and it bloated the RAMDISKS and install
media beyond the constraints of the media.

requested by deraadt@


# 1.117 14-May-2008 jordan

Adding new ACPI Parser code
Old guts of code still exists, needs to be torn out next
ok marco@
ok brad@


# 1.116 27-Apr-2008 jcs

add acpithinkpad(4), an acpi support driver for ibm/lenovo
thinkpads.

provides functionality that the bios did on older, non-acpi
thinkpads like respond to screen brightness keys and toggle the
bluetooth device on and off.

ok deraadt@ and marco@


# 1.115 24-Apr-2008 jsing

Introduce acpiasus(4), a driver for the ACPI based hotkeys found in many
ASUS laptops (including the ASUS EeePC) - largely based on NetBSD's
asus(4) driver. On the ASUS EeePC this allows us to enable/disable
wireless, change screen brightness and use the volume keys.

ok jsg@, weingart@


# 1.114 12-Apr-2008 kettenis

Get rid of acpi_s5 global variables; simply send SIGUSR2 instead of SUGUSR1
to tell init(8) to power down the machine.

ok krw@


Revision tags: OPENBSD_4_3_BASE
# 1.113 21-Jan-2008 jsg

Check against RESET_REG_SUP in FADT before doing the acpi reset dance
after all, not doing so appears to break more machines than it fixes.

ok marco@


# 1.112 05-Jan-2008 kettenis

Never attach more acpicpu(4) devices than cpu(4) devices. Always attach
the first acpicpu(4) device on non-MULTIPROCESSOR kernels, regardless of its
ACPI processor ID.

ok mikeb@


# 1.111 05-Dec-2007 deraadt

Hang acpi(4) below bios(4) instead of mainbus(4). This lets us move acpi(4)
before pcibios(4), and then.... if acpi(4) attaches, skip pcibios(4) since
it messes the machines up. Fixes claudio's HP dl320 G5, and almost assuredly
others.
ok kettenis


# 1.110 05-Dec-2007 deraadt

repair acpiopen/acpiclose and friends to handle the /dev/apmctl interface,
so that apmd(8) can actually work correctly against acpi(4), and thus,
even do the fancy -C/-A stuff; also tested by jmc


# 1.109 02-Dec-2007 fgsch

fix the trailing space for real now.


# 1.108 02-Dec-2007 fgsch

avoid trailing spaces on output; marco@ ok.


# 1.107 27-Nov-2007 kettenis

Fix off-by-one that made us attach acpicpu0 and acpicpu1 on GENERIC kernels.

ok marco@


# 1.106 25-Nov-2007 jsg

When available and ACPI is enabled use the ACPI reset method
to reboot. This is required for some quirky sis machines
and likely others to reboot.

Tested by many. 'commit early commit often' deraadt@


# 1.105 19-Nov-2007 deraadt

sigh, amd64 still uses that stupid acpi_enabled variable...


# 1.104 19-Nov-2007 deraadt

softc's come initialized to 0, you know...


# 1.103 19-Nov-2007 deraadt

store revision; from jsg


# 1.102 19-Nov-2007 kettenis

Initialize sc_facs earlier.

ok deraadt@, beck@, canacar@


# 1.101 16-Nov-2007 deraadt

fix the bus_space #define nightmare, so that amd64 and i386 are much more
uniform. as a result shared code like acpi needs less #ifdef's
ok marco kettenis


# 1.100 15-Nov-2007 deraadt

remove the #ifdef ACPI_ENABLE. the EC codepath is either safe enough (and
will be improved), or it is unsafe because AML assumes it must be there
ok almost everyone


# 1.99 15-Nov-2007 mikeb

Make acpicpu(4) attach to the existing CPUs only.

Problem reported by form@. Fix idea by kettenis@.
Lots of help from deraadt@. Tested by deraadt@, form@ and me.

ok deraadt marco


# 1.98 14-Nov-2007 deraadt

spacing


# 1.97 12-Nov-2007 deraadt

correct : vs , in attach-time messages


# 1.96 06-Nov-2007 deraadt

all ACPI_ENABLE kernels need acpi_interrupt()


# 1.95 06-Nov-2007 deraadt

move acpi_read_pmreg() and acpi_write_pmreg() out of SMALL_KERNEL so
that acpi ramdisks can compile (obvious solution)


# 1.94 05-Nov-2007 canacar

Establish the acpi interrupt earlier. Fixes problems with some
GENERIC.MP kernels not getting acpi interrupts. This is a
workaround until the real issue with late establishment is fixed.
ok weingart@, beck@, gwk@


# 1.93 03-Nov-2007 jordan

Added support for displaying wakeup devices
ok beck@,weingart@,gwk@


# 1.92 11-Oct-2007 kettenis

When walking the tree yo find _PRT methods, check whether the device is
actually enabled and functioning. Similar to what we do for _INI.

ok weingart@, beck@


# 1.91 08-Oct-2007 krw

More simple memset(,0,) -> M_ZERO changes. In this batch move to
size(*p) as the first malloc() parameter where p is declared locally
and thus easy to check. Add M_ZERO to gpe_table allocation in acpi.c
even though there is no obvious bzero or memset nearby.


# 1.90 13-Sep-2007 mk

Remove unnecessary memset() (see aml_evalnode(9)) and a bit of KNF.

ok toby


# 1.89 13-Sep-2007 weingart

Implement ACPI 6.5.1 spec tree walk for _STA and _INI.
Fixes mk's laptop. No regressions so far (thank you to the
testers).

ok gwk@, mk@, marco@


Revision tags: OPENBSD_4_2_BASE
# 1.88 17-Apr-2007 mk

Now that acpidock no longer hangs machines during boot (at least
according to the reports I've been getting) it should be safe to enable
it again, so remove the #if 0 stuff around it.

ok marco


# 1.87 11-Apr-2007 jordan

Added changes for C-State
ok marco@


# 1.86 26-Mar-2007 marco

Calculate minutes left more accurately.

From giovanni <qgiovanni@gmail.com>


Revision tags: OPENBSD_4_1_BASE
# 1.85 03-Mar-2007 mk

Stop looking for acpidock until we fix that stuff that breaks all pre-60
series ThinkPads with acpidock(4) enabled.


# 1.84 22-Feb-2007 marco

We were getting no interrupts in .MP. There are 2 fixes; one in
acpi_machdep.c and move the interrupt establishment down after ioapic
has been initialized.

ok jordan


# 1.83 21-Feb-2007 kettenis

Make an acpi-enabled GENERIC.MP work on systems that only support PIC mode.

ok marco@


# 1.82 19-Feb-2007 jordan

Added acpi_map_address function for mapping GAS/IO registers
ok marco@


# 1.81 18-Feb-2007 jordan

Added dynamic perfect hash for opcodes
ok marco@


# 1.80 18-Feb-2007 marco

Shave off another K pre compression.
Shuffle SMALL_KERNEL functions around so that we only have a single #ifndef


# 1.79 17-Feb-2007 marco

Add SMALL_KERNEL throught acpi to make it fit on boot media.
ok deraadt


# 1.78 13-Feb-2007 marco

Add smarter _INI code. This helps with some dissapearing devices.

ok kettenis jordan


# 1.77 31-Jan-2007 gwk

Introduce acpi_hasprocfvs thats "ACPI Has Processor Frequency and Voltage
Scalling" a global flag set by acicpu attach to indicate that ACPI is
handling fvs through the _PSS objects. This will fix disappearing
EST/powernow when booting kernels with acpi enabled, as acpicpu isnt
compiled into GENERIC yet. Also in cases where acpicpu is built in and
legacy EST/powernow work but acpi is missing the _PSS object, the legacy
routines will be used.

tested by dim@, and Nick Nauwelaerts <nick AT nauwelaerts.net>
ok marco@, dim@


# 1.76 22-Jan-2007 mk

Actually look for docking stations by looking for _DCK nodes.

ok marco


# 1.75 02-Jan-2007 marco

On some systems EC must be attached as the first device. Move EC from
generic _HID devices to its own routine to force first attach. Bug found
and debugged by joris@

Code mostly written by joris@


# 1.74 26-Dec-2006 marco

Add polling to devices that require it such as acpibat and acpitz.
Use the same tsleep/wakeup mechanism as the interrupt code to keep it non-
concurrent.


# 1.73 21-Dec-2006 deraadt

even more knf love


# 1.72 21-Dec-2006 marco

Remove some leftover acpiec gpe/interrupt code.


# 1.71 20-Dec-2006 deraadt

oops, name already used


# 1.70 20-Dec-2006 deraadt

rename __acpi_enable_gpe(); ok marco


# 1.69 20-Dec-2006 deraadt

acpi_unmap_pmregs() is not used, ok marco


# 1.68 19-Dec-2006 deraadt

simplify some low-level methods substantially (shrinking code); ok marco jordan


# 1.67 06-Dec-2006 mk

malloc(..., M_NOWAIT) with no NULL check is bad. If allocation fails,
complain, clean up, and bail out of attach.

input and ok gwk


# 1.66 28-Nov-2006 jordan

Fixed clearing arg for acpi_init_pic
ok kettenis@


# 1.65 27-Nov-2006 kettenis

Don't spam dmesg.


# 1.64 27-Nov-2006 jordan

Fixes for acpiprt routing
Post-parse fixup for namerefs
ok kettenis@


# 1.63 25-Nov-2006 marco

Don't handle acpiev events inside the gpe handler since this causes deadlocks.

ok jordan@


# 1.62 15-Nov-2006 kettenis

Hook up acpi apic interrupt routing support to generic acpi code.

ok jordan@


# 1.61 02-Nov-2006 marco

Kill acpiec attach function and add its functionality to the generic _HID
device attach

prodded and ok jordan


# 1.60 02-Nov-2006 marco

Fix comments to reflect reality.


# 1.59 19-Oct-2006 marco

Replace ACPI ioctls with APM ioctls. This makes ACPI talk the same protocol
as APM therefore userspace can now retrieve power information in the same
fashion as all other apm arches (zaurus, macppc, i386 etc).

To use force the use of acpi device like so: apmd -f /dev/acpi

ok jordan@


# 1.58 19-Oct-2006 jordan

Fixed GPE debug levels
Fixed ref counter on temporary bufferfields
DSDT parsing fully working on IBM Thinkpad
ok marco@


# 1.57 15-Oct-2006 jordan

Removed old GPE handler code remnants
ok marco@


# 1.56 12-Oct-2006 jordan

Massive ACPI changes
Fixed ACPI leaks
new ACPI API
ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.55 30-Jun-2006 gwk

KNF, No functional change,
"go at it" marco@


# 1.54 30-Jun-2006 jordan

Support for calling Embedded Controller via new GPE interface
ok marco@


# 1.53 30-Jun-2006 jordan

Added new code for GPE handling
Also works with ACPIEC; need to fixup acpiec.c to remove original interrupt handler
ok marco@


# 1.52 30-Jun-2006 jordan

Added offset argument for acpi read/write pmreg routines (new GPE code)
ok marco@


# 1.51 31-May-2006 todd

do not call acpi_powerdown() unless acpi has successfully initialized
ok gwk@ canacar@


# 1.50 29-May-2006 canacar

Add embedded controller support.
ok marco@ deraadt@ testing & ok gwk@


# 1.49 19-May-2006 canacar

Minimal thermal zone driver. Monitors thermal zone temperature,
shuts down the system if the 'critical temperature' is reached.
ok marco@


# 1.48 11-Apr-2006 marco

KNF.


# 1.47 11-Apr-2006 gwk

Add acpi_resume(struct acpi_softc *sc), which calls some AML methods,
detected at attach time, currently unused.

ok marco@


# 1.46 11-Apr-2006 gwk

Discover some ACPI sleep related methods, and call them at the
appropriate time in acpi_enter_sleep_state().

ok marco@


Revision tags: OPENBSD_3_9_BASE
# 1.45 26-Feb-2006 marco

Add acpicpu as a device.

ok jordan@


# 1.44 23-Feb-2006 marco

remove dead function.

ok jordan@


# 1.43 22-Feb-2006 marco

Print out all tables it found during autoconf. We'll need this data in
the future.


# 1.42 22-Feb-2006 marco

Make dmesg less verbose.

ok jordan.


# 1.41 22-Feb-2006 jordan

Added parsing of SSDT tables
ok marco@


# 1.40 22-Feb-2006 jordan

Added new aml_register to notify by device id
Moved powerdown code to button device
ok marco@


# 1.39 21-Feb-2006 marco

Readjust debug levels that crept in too low over the last few days.
KNF


# 1.38 21-Feb-2006 marco

Rig the power button so that the machine shuts down correctly.


# 1.37 20-Feb-2006 marco

Add acpibtn device. Remains disabled for now.


# 1.36 19-Feb-2006 jordan

Added code for aml_notify GPE notification callback
ok marco@


# 1.35 19-Feb-2006 grange

Fix acpi_enter_sleep_state() by replacing current ugly hack
with mostly correct sleeping state transition code.

ok brad@ marco@


# 1.34 19-Feb-2006 marco

jordan:
Enable interrupts.
Added peripheral code to make this work.

marco:
Replace softintr and other types of interrupts with a handler with context.
Fixed interrupt magic for GPEs.
Some KNF

This is work done by jordan and I but has to be a single commit.


# 1.33 17-Feb-2006 marco

Remove earlier introduced delays. We really only need them during autconf.

ok jordan@


# 1.32 17-Feb-2006 marco

Add a generic delay function as per the ACPI spec.

from jordan@


# 1.31 17-Feb-2006 marco

Use tsleep instead of delay when !cold.


# 1.30 17-Feb-2006 marco

Argh, we need delays between reads/writes. The backend reads (i2c most likely)
are so slow that values come back as 0 sometimes. We have to do this
despite the spec saying we shouldnt have to. Guess having a faster
parser than others can hurt sometimes.

Idea from jordan.


# 1.29 17-Feb-2006 jordan

Fixed code for handling _INI
Rearranged debug statements
ok marco@


# 1.28 16-Feb-2006 jordan

Added stub for calling _INI
Fixed aml_dowhile for NULL return
ok marco@


# 1.27 16-Feb-2006 jordan

Fixed parser/evaluator to retrieve battery info+state
Cleanup of parser
Dell laptops return battery status
ok marco@


# 1.26 06-Feb-2006 brad

fix a typo so this will compile on amd64.


# 1.25 04-Feb-2006 marco

When ACPI_ENABLED wasn't set but the devices were it had an unknown variable.

Pointed out by ian@.


# 1.24 03-Feb-2006 jordan

Rewrote aml parser yet again...
Fixed bus_space_map for i386/amd64
Added _BST and _BIF to acpibat to get status/info
ok marco@


# 1.23 20-Jan-2006 jordan

More evaluator changes
Removed old evaluator code
Fixed _HID value
ok marco@


# 1.22 19-Jan-2006 jordan

Added support for read/write of PCI Config space
Help jason@
ok marco@


# 1.21 18-Jan-2006 jordan

Added new evaluation routines
Added helper methods for buffer/field evaluation
Fixed broken indentation on dsdt.c

ok marco@


# 1.20 17-Jan-2006 jordan

Major overhaul of the aml parser. Evaluater temporarily disabled.

ok marco@


# 1.19 06-Jan-2006 grange

Check for ACPI control availability only if ACPI_ENABLE defined.


# 1.18 06-Jan-2006 grange

Informative error messages on attach, prettier dmesg.


# 1.17 06-Jan-2006 grange

printf -> dnprintf conversion for debug messages.


# 1.16 05-Jan-2006 grange

Find SLP_TYPx values and store them for future use in
acpi_enter_sleep_state().


# 1.15 05-Jan-2006 grange

Random KNF and cleanup


# 1.14 05-Jan-2006 grange

Provide symbolic names for the acpi sleeping states and use it.


# 1.13 28-Dec-2005 marco

Moved DEVNAME to a acpidev.h

ok jordan@


# 1.12 18-Dec-2005 sturm

add back comment about wrong code

ok marco


# 1.11 16-Dec-2005 marco

Remove test for kbd_reset. Prompted deraadt@, ok jordan@


# 1.10 16-Dec-2005 jordan

Added hooks for powerdown/power button press


# 1.9 16-Dec-2005 jordan

Cleaned up common power management register i/o
Added enter sleep state ioctl
Hooked power button interrupt
ok marco@


# 1.8 16-Dec-2005 marco

Remove autoconf chatter


# 1.7 16-Dec-2005 jordan

Moved DSDT AML parsing into parent ACPI driver
ok marco@


# 1.6 14-Dec-2005 marco

Add AC device glue.


# 1.5 14-Dec-2005 marco

Adjust debug printing levels to something more sane.


# 1.4 13-Dec-2005 marco

Add glue to attach devices to apci.


# 1.3 07-Dec-2005 marco

KNF


Revision tags: OPENBSD_3_8_BASE
# 1.2 10-Jul-2005 grange

Spacing cleanup.


# 1.1 02-Jun-2005 tholo

Start on a basic ACPI framework -- does not do much more than read out the
ACPI tables into kernel memory and attach ACPI and HPET timers currently.

In order to test this code, enabling the devices in GENERIC as well as
the ACPI_ENABLE option is needed. This code does not do any thermal
control yet, so this should be done with care depending on the platform.

In the tree so more people can contribute to making this more fully
featured.

Ok niklas@ grange@ tedu@


# 1.341 27-Mar-2018 kettenis

Add acpicmos(4), a driver that implements SystemCMOS OperationRegion
access support. This fixes machines where the AML doesn't check whether
support for this OperationRegion type has been registered by the OS.

ok mlarkin@


Revision tags: OPENBSD_6_3_BASE
# 1.340 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.339 10-Feb-2018 deraadt

Syncronize filesystems to disk when suspending. Each mountpoint's vnodes
are pushed to disk. Dangling vnodes (unlinked files still in use) and
vnodes undergoing change by long-running syscalls are identified -- and
such filesystems are marked dirty on-disk while we are suspended (in case
power is lost, a fsck will be required). Filesystems without dangling or
busy vnodes are marked clean, resulting in faster boots following
"battery died" circumstances.
Tested by numerous developers, thanks for the feedback.


# 1.338 08-Feb-2018 deraadt

After flushing disks, try again to release all memory. We don't need to
save dirty memory to the hibernate space.


# 1.337 08-Feb-2018 deraadt

Toss all releaseable memory, because fragmentation can get in the way
of allocating the hibernate playpen.


# 1.336 08-Feb-2018 deraadt

sr_quiesce() is a new approach for ensuring that softraid drains
output to the disks.

This is part of a larger suspend/resume filesystem-safety diff, which
has been worked on for a couple of months already. Tests by job, krw,
beck, benno, and others. Sometimes even by snapshot users...


# 1.335 29-Nov-2017 kettenis

Add support for GPIO-signaled events.

ok mlarkin@


# 1.334 16-Nov-2017 jcs

Add PCI attachment for dwiic(4) needed by Intel 100 Series machines

ok kettenis


# 1.333 14-Nov-2017 kettenis

Add a few more PNP IDs to the skip lists.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.332 17-Aug-2017 stsp

Make dwiic(4) attach its ACPI parent dependencies.
This change makes the Asus E200HA keyboard work.

Original analysis and patch by Cesare Gargano
ok kettenis@


# 1.331 09-Aug-2017 dcoppa

Add "PNP0303" (8042 PS/2 Controller) to acpi_isa_hids[]

ok kettenis@


# 1.330 22-Jul-2017 jcs

acpibat: look for _BIX first before falling back on _BIF

ACPI 4.0 deprecated _BIF for battery status so some newer machines
are exporting _BIX which extends _BIF's package with a few extra
fields. When using _BIX, export the new discharge cycle count as a
sensor.

feedback from kettenis and anton
ok deraadt


# 1.329 20-Jul-2017 kettenis

Start a list of isa(4) device names such that we can avoid printing them
as "not configured".

ok mikeb@, millert@, deraadt@


# 1.328 08-Apr-2017 deraadt

Sensors are run as callbacks inside tasks. During suspend a sensor
could be running inside a driver that will be force-detached, or due
to tsleep end up disrupting the softstate/hardstate contract. At
suspend time, quisce all these callbacks by waiting for completion.
This issue has never been observed for real, but may be implicated in
suspend/resume failures.
ok kettenis guenther mlarkin


# 1.327 08-Apr-2017 deraadt

Two prototypes for acpi_maptable; merge to one place


Revision tags: OPENBSD_6_1_BASE
# 1.326 27-Mar-2017 deraadt

Now that hibernate_alloc() only has clean success/failure, don't
need to call hibernate_free() to clean up a partial mess.
ok mlarkin kettenis


# 1.325 13-Mar-2017 deraadt

allow ramdisks to build; ok jcs


# 1.324 12-Mar-2017 jcs

try to attach acpisbs first and if successful, don't attach acpibat


# 1.323 02-Mar-2017 natano

Add a new sysctl machdep.lidaction. The sysctl works as follows:

machdep.lidaction=0 # do nothing
machdep.lidaction=1 # suspend
machdep.lidaction=2 # hibernate

lidsuspend is just an alias for lidaction, so if you change one, the
other one will have the same value. The plan is to remove
machdep.lidsuspend eventually when people have upgraded their
/ets/sysctl.conf.

discussed with deraadt, who came up with the new MIB name
no objections mlarkin
ok stsp halex jcs


# 1.322 28-Feb-2017 natano

Untangle abstract sleep modes from ACPI sleep states. This paves the way
for further work in the area.

idea and ok deraadt
ok mlarkin


# 1.321 22-Feb-2017 jcs

add acpisbs, an acpi smart battery subsystem driver reading data
over smbus

currently disabled because it conflicts with acpibat

ok deraadt, kettenis


# 1.320 14-Jan-2017 kettenis

Revert aml_rdpciaddr changes; breaks several machines that were working
before.


# 1.319 11-Jan-2017 jsg

Increase the size of the buffers used for _HID and _CID strings to be
able to fit "Hyper_V_Gen_Counter_V1".

ok mlarkin@ deraadt@


# 1.318 08-Jan-2017 kettenis

Make aml_rdpciaddr() work for for devices behind bridges. The code now
uses the mapping between AML nodes and PCI devices that we establish.
Because _INIT methods may end up calling aml_rdpciaddr(), make sure we
create that mapping early. Also handle devices that aren't actually present.
These devices are now included in the mapping and reads will return an
all-ones patterm whereas writes are a no-op..

ok mlarkin@


# 1.317 25-Oct-2016 pirofti

Add the resource index to aml_parse_resource() callback function.

This is needed by an upcoming acpiec commit that handles machines breaking
the current ACPI specifications.

Change suggested by and ok kettenis@, guenther@


# 1.316 18-Sep-2016 guenther

If the APIC table (MADT) isn't present then ci_acpi_proc_id won't be set
when acpicpu tries to attach, leading to a NULL dereference. The ACPI
processor object on the Dells where this was reported is bogus and useless
anyway so apply the same test when deciding whether to attach to processor
objects as acpicpu would when matching them to cpus.

Problem reported by Eivind Eide (xenofil (at) gmail.com) and
Johan Huldtgren (johan+openbsd-bugs (at) huldtgren.com).
Testing by Eivind Eide.
ok kettenis@ mlarkin@


# 1.315 03-Sep-2016 naddy

Write the system time back to the RTC every 30 minutes.
This fixes the problem that long-running machines which were not
shut down properly would reboot with a badly offset system time.

hints and ok kettenis@


# 1.314 31-Aug-2016 mlarkin

Don't put a device in the wake device list if _STA says it's not there.

Fixes at least one machine that woke up from sleep / poweroff immediately.

ok kettenis@, tested by kettenis and martijn@


# 1.313 28-Jul-2016 kettenis

Store the acpi processor ID/UID in struct cpu_info, and use it to attach
acpicpu(4) drivers to the right cpu(4).

ok mlarkin@, guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.312 10-Jun-2016 kettenis

The power management timer has been made optional in ACPI 5.0A, so don't
attempt to attach acpitimer(4) if the timer isn't present.

Pointed out by jcs@

ok jcs@


# 1.311 08-May-2016 kettenis

Make sure devices listed by a device's _DEP method are attached before
we attach the device itself. The _DEP methode, introduced in ACPI 5.0,
is a hint that the device in question depends on OpRegion support from
the devices listed by _DEP, so we have to order them the proper way.
To prevent us from attaching those devices again when we encounter them
later walking down the device tree, keep track of our attempts to
attach a device driver to them using aflag in the aml_node structure.

ok guenther@


# 1.310 25-Apr-2016 pirofti

We attach acpihpet(4) and acpiec(4) without using the HID, so do not
report it in the dmesg.

OK deraadt@, mlarkin@.


# 1.309 02-Apr-2016 kettenis

Reduce dmesg spam by knocking out some acpi hardware IDs that don't need a
driver.

ok mpi@


# 1.308 29-Mar-2016 kettenis

Check if a device is present (using _STA) in acpi_foundhid. This prevents
us from reporting devices as "not configured" that aren't actually present,
and allows us to remove duplicated code from several drivers.

ok deraadt@, ok jsg@


# 1.307 29-Mar-2016 kettenis

Implement a cleaner way to prevent acpivout(4) from attaching if we have
a platform-specific method of controlling the backlight.

ok millert@, mlarkin@, jung@


# 1.306 28-Mar-2016 kettenis

Adding each and every new acpi device driver to acpi_foundhid() is
gettinga bit of the burden. And it really isn't how our autoconf
framework is supposed to work for busses that can be enumerated. All
the drivers already check for a matching _HID in their attach
function. So we can just drop the checks in acpi_foundhid() and let
the drivers sort themselves out.

Print a "not configured" line for devices for which we don't attach adriver.
This may be a bit much as there are devices for which we will not have a
driver. This will be removed if it causes too much dmesg spam.

ok deraadt@, mlarkin@


Revision tags: OPENBSD_5_9_BASE
# 1.305 17-Jan-2016 jsg

Take acpi_foundhid() outside of #ifndef SMALL_KERNEL so the ramdisk
kernel can find and attach dwiic(4) and sdhc(4).


# 1.304 17-Jan-2016 jsg

remove an unused global variable


# 1.303 14-Jan-2016 kettenis

Attach dwiic(4) on the Intel Bay Trail i2c controllers as well.


# 1.302 12-Jan-2016 kettenis

Improve emulation of PM registers on Hardware-redcued ACPI by emulating
SPL_TYPx, SLP_EN and WAK_STS using the new SLEEP_STATUS_REG and
SLEEP_CONTROL_REG registers. Makes the transition into S5 (powerdown) work
on the ASUS X205TA.

ok deraadt@, mlarkin@


# 1.301 12-Jan-2016 jcs

Add dwiic, a driver for the Synopsys DesignWare i2c controller found
on the Samsung ATIV Book 9 laptop. This initial version only
supports ACPI config/attachment.

Add ihidev, a HID-over-i2c driver largely based on uhidev. dwiic
handles attaching ihidev devices found in ACPI.

Add ims, a HID-over-i2c mouse/trackpad driver to get basic cursor
and button functionality from HID-compliant i2c trackpads.

ok kettenis deraadt


# 1.300 11-Jan-2016 kettenis

Make sdhc(4) attach to hardware IDs "80860F14" and "PNP0FFF".


# 1.299 09-Jan-2016 kettenis

Initial support for Hardware-Reduced ACPI. This skips the legacy mode checks
by emulating the PM_CNT register such that SCI_EN is always enabled. It also
skip attaching acpitimer(4) since the relevant registers are no longer there.

ok deraadt@, mlarkin@


# 1.298 06-Jan-2016 kettenis

Follow the spec and check the SCI_EN bit to see if ACPI has already
been enabled and don't bother enabling it again if it is. This should fix
systems that don't implement legacy mode and therefore set ACPI_ENABLE and
ACPI_DISABLE to zero.

ok mlarkin@, yasuoka@


# 1.297 23-Nov-2015 reyk

Back out the CRITICAL change, turn it back from 10% to 25%. Some
people with old/broken batteries didn't like this change, and the
underlying heuristics are wrong.


# 1.296 22-Nov-2015 reyk

The "CRITICAL" APM battery level, that is exported/faked by the ACPI
stack, is currently set to 25%. With 25% battery left, a typical
modern laptop will have at least one hour runtime left. It is an
arbitrary value, and other APM power layers in the kernel set CRITICAL
to something like 5% or 10%, so set it to 10% for now.

OK mlarkin@


# 1.295 28-Sep-2015 deraadt

In low-level suspend routines, set cold=2. In tsleep(), use this to
spit out a ddb trace to console. This should allow us to find suspend
or resume routines which break the rules. It depends on the console
output function being non-sleeping.... but that's another codepath which
should try to be safe when cold is set.
ok kettenis


# 1.294 06-Sep-2015 deraadt

clarify malloc arguments (base + length...)


# 1.293 28-Aug-2015 deraadt

fairly simple sizes for free(); ok tedu


# 1.292 20-Aug-2015 kettenis

Respect the access size when reading or writing to pci config space.
This version also makes sure writes are properly aligned, preventing
the panics seen with the previous incarnation of this change.

Fixes battery status passthrough in vmware, brightness keys on my x220
and who knows what else.

ok mlarkin@, deraadt@


Revision tags: OPENBSD_5_8_BASE
# 1.291 04-Aug-2015 deraadt

If we are setting ACPI_PM1_BM_RLD do it only once, and remember to
repeat this action at resume time.
ok mlarkin guenther


# 1.290 29-Jul-2015 deraadt

Something subtle in previous revision is locking up suspend or booting
on some laptops. Mailing lists threads mention x220 and Asus machines.
Reverting for now.


# 1.289 21-Jul-2015 kettenis

iRespect the access size when reading or writing to pci config space.
Fixes battery status passthrough in vmware and who knows what else.

Tested by many.

ok mlarkin@, deraadt@


# 1.288 17-Jul-2015 mlarkin

fix comments


# 1.287 26-Mar-2015 jsg

Use the 0x10 extended capability to determine if the trackstick
buttons are wired to the trackpad and need to be re-routed to the
trackstick. Without this change the buttons on 2015 thinkpads
get picked up as extended buttons that show up as scroll up/down.

Remove the X1 Carbon 2015 (LEN0048) and X250 (LEN0046) from the
top button area/soft buttons quirks list. Also avoid using the
quirk list entirely if the capability bit is set.

Based on work from jcs@ with feedback from mpi@
ok mpi@ jcs@


# 1.286 20-Mar-2015 kettenis

The ACPI standard says that a PCI function number of FFFF can be used to refer
to all functions on a device. This makes no sense in the context of accessing
PCI config space. Nevertheless stsp@ has a machine with AML that tries to
do config space reads from a device where _ADR encodes function number FFFF.
This is almost certainly buggy AML, but since Linux (and presumably Windows)
boot just fine on this machine we need to work around this. Linux effectively
treats function FFFF as function 7, which isn't present on this machine.
Instead we simulate a read from a non-existent device by returning a bit
pattern of all ones. Writes to function FFFF will still panic.

ok mlarkin@


# 1.285 16-Mar-2015 deraadt

Reduce use of SMALL_KERNEL in acpi, bringing acpiec driver and more gpe
handling into RAMDISK. This is now possible because the install media
has ample room. The goal is to reduce special cases where we may be
skipping (unknown) important operations...
ok mlarkin kettenis


# 1.284 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.283 07-Feb-2015 deraadt

New framework that allows hibernate to pass in entropy from it's fresh
boot.
ok mlarkin


# 1.282 06-Feb-2015 mlarkin

Fix a hibernate crash on some machines due to unmapping a page that
may not have been mapped previously (in the failure to hibernate case).

Also ensure that the lowmem ptp is mapped in all cases (not just MP).

ok kettenis


# 1.281 17-Jan-2015 deraadt

resume_randomness() before spinning up other cpus...


# 1.280 15-Jan-2015 jsg

Define a new wscons mouse type for Synaptics clickpad devices that lack
physical buttons. This will be used if the acpi pnpid for the mouse
matches a list provided by Synaptics found in Linux.

Combined with the xenocara changes this will remove the need for an xorg
config file for the x240/t440/t540 generation of thinkpads.

ok matthieu@ shadchin@ kettenis@


# 1.279 11-Jan-2015 kettenis

Tolerate checksum errors in ACPI tables (but complain about them). Apparently
there are machines out there with broken BIOSen with such checksum errors in
thier RSDT tables. Windows runs fine on these machines, so it probably does
not check. Linux ignores checksum errors as well (and prints a warning).

ok deraadt@, krw@


# 1.278 18-Dec-2014 deraadt

suspend and resume the RNG (at what looks like the right spots)
ok reyk djm


# 1.277 09-Dec-2014 doug

Replace some malloc(n*size,...) calls with mallocarray().

ok tedu@ deraadt@


# 1.276 06-Dec-2014 mpi

If "machdep.lidsuspend" is not set, do not go back to sleep if the lid
is still closed when resuming. This allows docked laptop to be resumed
without opening them.

ok mlarkin@


# 1.275 23-Nov-2014 mlarkin

If a machine has no _LIDs, then none can be open. This diff fixes a problem
where machines without any _LIDs were going back to sleep after hibernate
due to the recent suspend-if-lid-closed-on-resume diff. Noticed on qemu,
which doesn't have any _LIDs.

discussed with deraadt


# 1.274 18-Nov-2014 krw

Nuke yet more obvious #include duplications.

ok miod@


# 1.273 08-Nov-2014 mlarkin

If resuming from sleep (zzz/ZZZ) and the lid is still closed, go back to
sleep. This prevents accidental lid flex or slight opening in a backpack
from waking the machine up and leaving it resumed (powered on).

ok deraadt@


# 1.272 26-Sep-2014 kettenis

Rework piglet and pig allocation. Currently the piglet gets allocated
deep down in the suspend path, where it is really hard to recover from
allocation failure. So allocate the piglet early on in the suspend path.
Also change the piglet and piglet allocation functions to use km_alloc(9)
instead of doing pmemrange magic. This removes a bunch of code which, in the
case of the piglet allocation, is broken since it results in a NULL pointer
dereference. Also switch the piglet allocation to not wait. If we can't
allocate 16MB of phys contig memory on a halfway modern machine we're almost
certainly under a lot of memory pressure and we're better off not trying to
hibernate anyway.

ok mlarkin@


# 1.271 25-Sep-2014 kettenis

Bring back rev 1.249. Now that mp_setperf() has been fixed, it should be
safe to restore perflevel when resuming. Should fix the issues people
see with the processor running at full speed heating up the machine while
the apm command reports that the CPUs are running at a low speed.

ok dcoppa@, deraadt@


# 1.270 23-Sep-2014 kettenis

Release the acpi lock around calls to wsdisplay_suspend() and
wsdisplay_resume(). The X server may adjust the display brightness just
before/after the VT switch when suspending/resuming. In fact, it may
already be blocked adjusting the display brightness for some reason
unrelated to the suspend request.

The VT switch request will eventually time out, but the delay is annoying
and the failed VT switch will lead to problems after resume.

Needed for a future update of the xf86-video-intel driver.

ok mlarkin@


# 1.269 19-Sep-2014 kettenis

Use config_suspend_all(9).


# 1.268 08-Sep-2014 kettenis

Clear the PME Status bit on PCI devices in response to Device Wake events.
Fixes the interrupt storm on the 2009 Mac mini with WOL enabled on its
nfe(4) interface.

ok mlarkin@


Revision tags: OPENBSD_5_6_BASE
# 1.267 20-Jul-2014 mlarkin

Support hibernating to softraid crypto volumes.

much help and ok from deraadt@


# 1.266 16-Jul-2014 mlarkin

Reenable hibernate RLE support and flush+zero all memory after unpack.


# 1.265 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.264 12-Jul-2014 kettenis

Add a function to drop all clean pages on the page daemon queues and call
it when we hibernate.

ok mlarkin@, miod@, deraadt@


# 1.263 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.262 11-Jul-2014 mlarkin

Flush the buffercache to 16MB on hibernate and restore its previous max
size (kern.bufcachepercent) on resume, for better hibernate performance.

ok beck@


# 1.261 10-Jul-2014 blambert

remove unneeded header

ok mlarkin@


# 1.260 01-Jul-2014 kettenis

Adding an additonal delay after each access to SystemMemory or SystemIO
during autoconf makes no sense.

ok deraadt@


# 1.259 23-Jun-2014 kettenis

SystemMemory is used to access memory mapped registers on some machines, so we
must use the bus_space(9) API to access it instead of memcpy(9). Also make
sure we properly align access to fields that need more than byte alignment.

ok guenther@


# 1.258 21-May-2014 mlarkin

Fix invalid / missing format specifiers in several dnprintf statements
(used only when ACPI_DEBUG is enabled)

From a diff on tech@ by Fabian Raetz, with a minor change by me, tested
on i386 and amd64 with ACPI_DEBUG enabled and disabled.


# 1.257 25-Apr-2014 mlarkin

Defer the free of the hibernate structures until later, when it is safe to
do so.

Freeing the hibernate structures too early leads to inadvertent calls into
splx when we aren't ready for them yet. Found when trapping splx calls in
the resume path.

discussed with deraadt@ at m2k14


# 1.256 13-Apr-2014 mlarkin

Fix a badly worded debug message that implied the entire machine was going
to sleep, instead of just the acpi thread going to sleep.


# 1.255 31-Mar-2014 mpi

Change the order of operations during a suspend/resume cycle and
call bufq_quiesce() after executing the DVACT_QUIESCE handlers.

This should be safe since no disk nor controller drivers have such
handler but it will allow us to detach sd(4) devices attached to a
USB bus.

Another benefit pointed out by kettenis@ is that drivers that need
to read a firmware from the disk should be able to do it at resume
time in a DVACT_WAKEUP handler.

ok kettenis@, deraadt@


# 1.254 13-Mar-2014 dlg

get rid of the assumption that the head of the alldevs list is the
"mainbus" device. this breaks when mpath is enabled because it
attaches before mainbus and therefore takes the head position.

have autoconf provide device_mainbus() which looks up mainbus_cd,
and use that instead.

discussed with deraadt who just wants mpath stuff to move forward
despite there being many ways to shine this particular turd.


Revision tags: OPENBSD_5_5_BASE
# 1.253 21-Feb-2014 deraadt

Do the lid control further on in the resume sequence, since this is
AML, and will do WAITOK operations. While cold, that is a bad idea.
We seem to be safe with respect to the spec, I do not see any event
debounce issues here.
ok kettenis, tested by many


# 1.252 20-Jan-2014 claudio

Instead of checking sc->sc_revision <= 1 check that the
fadt->hdr_revision <= 1. This is a better check since it checks
the version of the proper descriptor. This allows the alix APU
to reboot since those boards have currently a wrong sc->sc_revision.
Committing this now so that it can be tested easier.
With and OK jsg@ also fine by derradt@


# 1.251 24-Dec-2013 kettenis

If the FADT has its SMI_CMD set to zero, assume we're only ACPI-only hardware
and don't need to disable SMI ownership of the ACPI hardware registers.

ok mlarkin@


# 1.250 23-Dec-2013 kettenis

Revert previous commit. It makes resume hang on some MP systems running
GENERIC.MP.


# 1.249 16-Dec-2013 kettenis

Restore hw.setperf upon resume like we do for apm(4) on i386. Seems we need
to do this fairly late in the resume to avoid hangs.

ok deraadt@, mlarkin@


# 1.248 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


# 1.247 06-Nov-2013 mpi

Add support for Power Resources for Dx states and the necessary hook
for PCI devices. This hook should be called twice, before and after
changing the power state of a PCI device.

Before setting the device to the new state, the ACPI layer will notify
every power resources linked to the device for that state and make sure
they are turned "_ON". After changing the state of the device, it will
decrement the reference of every power resources linked to that device
for the old state and turn them "_OFF" if they are no longer referenced.

This fixes the no-USB after resume problem seen on various ThinkPad,
problem initialy diagnosed with Alexander Polakov.

ok kettenis@, deraadt@


Revision tags: OPENBSD_5_4_BASE
# 1.246 01-Jun-2013 mlarkin

Stop printing ACPI wakeup devices in dmesg after the 16th wakeup device.

Helps with VMware VMs that report hundreds of wakeup devices.

ok reyk@, deraadt@


# 1.245 31-May-2013 bluhm

Fix the build for a kernel without wd and pciide in its config.
OK deraadt@ tedu@


# 1.244 30-May-2013 deraadt

Enforce ca_activate tree-walks over the entire heirarchy for all events,
cleaning up some shutdown-hook related code on the way.
(A few drivers related to sparc are still skipped at kettenis' request)
ok kettenis mlarkin, tested by many others too


# 1.243 18-Apr-2013 deraadt

pin the acpi thread to the BSP, because AML and SMI cannot be trusted. I
was certain we were doing this already, but it turns out no...


Revision tags: OPENBSD_5_3_BASE
# 1.242 24-Dec-2012 guenther

In acpiioctl(), Return though the splx() path if suser() returns an error

ok deraadt@


# 1.241 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


# 1.240 04-Oct-2012 ehrhardt

Use information provided by ACPI to attach secondary PCI host bridges.

ok kettenis@


# 1.239 07-Sep-2012 kettenis

Add a new function called acpi_pci_min_powerstate() that returns the lowest
power state for a PCI device for the power state (currently S3 or S4) we're
transitioning into. This function is probably too simplistic; there is a lot
of (silly) complexity in the ACPI spec.

ok deraadt@


Revision tags: OPENBSD_5_2_BASE
# 1.238 13-Jul-2012 pirofti

Disable acpivideo(4) on ASUS laptops.

There are some models that panic whenever an event is received.
Some cope, some don't. In order to get this right acpiec(4) requires
major rework. Until then let machines boot.

Discussed with deraadt@.


# 1.237 11-Jul-2012 mlarkin

for now, restrict hibernate to root in the acpi ioctl kernel interface.
normal rules for controlling this interface through apmd still apply.

ok deraadt@


# 1.236 09-Jul-2012 deraadt

tiny little oops in untested hibernate block


# 1.235 09-Jul-2012 deraadt

clean up ioctl() case statements


# 1.234 09-Jul-2012 deraadt

acpi_disable_onegpe() is not used; ok pirofti


# 1.233 24-May-2012 kettenis

If _MIN > _MAX consider a WordBus resource to be invalid. Fixes a panic on
the IBM x3550-M4.

ok mikeb@


# 1.232 29-Mar-2012 mlarkin

Missing #ifdef in previous diff


# 1.231 29-Mar-2012 mlarkin

Prevent hibernation attempts on machines with unsupported disk controllers.
The same check is also performed later in the hibernate sequence, but by
then it's too late to cleanly unwind (presently).

ok deraadt


# 1.230 26-Mar-2012 deraadt

simplify conditionals in acpi_sleep_state()
ok mlarkin


# 1.229 26-Mar-2012 deraadt

hook in the hibernate request code; half of this diff is from mlarkin
ok mlarkin


Revision tags: OPENBSD_5_1_BASE
# 1.228 20-Sep-2011 deraadt

Very early in suspend while the clock is still ticking realtime,
save the clock back to the rtc. The zaurus already did this.
ok phessler miod kettenis


Revision tags: OPENBSD_5_0_BASE
# 1.227 02-Jul-2011 nicm

kqueue attach functions should return an errno or 0, not a plain 1. Fix
the obvious cases to return EINVAL and ENXIO.

ok tedu deraadt


# 1.226 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.225 16-Jun-2011 pirofti

Import acpitoshiba driver.

This is originally written by Hiroyuki Aizu, ported and integrated
in our tree by Javier Vazquez <javama4 AT gmail> with tweaks and
suggestions by me.

Import prodded by deraadt@ and the lack of testers on tech@.


# 1.224 27-Apr-2011 jcs

attach acpithinkpad to newer lenovo models like the x120e

ok deraadt@


# 1.223 22-Apr-2011 jordan

Fix uninitialzied variables and formatting strings (-Wxxx errors)


Revision tags: OPENBSD_4_9_BASE
# 1.222 02-Jan-2011 jordan

Re-add code for ACPI taskqueue, eliminates need for state vars in acpi softc
Serialize processing ACPI tasks for sleep/powerdown/GPE event
ok deraadt@


# 1.221 31-Oct-2010 guenther

Revert last commit: it breaks resume on Thinkpads
"then please back it out" deraadt@


# 1.220 26-Oct-2010 jordan

Add task queue for ACPI gpe and notify handlers
This fixes eject on Dell Latitude dock and an issue on the Dell Mini
battery update.
ok deraadt


# 1.219 07-Oct-2010 jordan

Create separate functions for enabling wake and runtime gpes.
Remove old suspend/resume gpewalk function
ok deraadt, mlarkin


# 1.218 05-Oct-2010 jordan

split acpi_enable_onegpe to separate enable/disable functions
ok deraadt


# 1.217 05-Oct-2010 jordan

Added acpi_disable_allgpes function to clear GPE status
ok deraadt


# 1.216 05-Oct-2010 deraadt

clean up acpi_softc use more


# 1.215 05-Oct-2010 deraadt

delete a bit of #if 0 code


# 1.214 05-Oct-2010 deraadt

hide use of our global acpi_softc pointer a bit better


# 1.213 29-Sep-2010 deraadt

grab the acpi thread lock very early in acpi thread startup; not that it
is likely that anyone will race us... but why be careless.
ok kettenis


# 1.212 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.211 29-Aug-2010 jasper

- add a check for FWRITE here too.

ok miod@


# 1.210 27-Aug-2010 kettenis

Make PCI Power Management optional, and only enable it when acpi(4) attaches.

ok deraadt@


Revision tags: OPENBSD_4_8_BASE
# 1.209 08-Aug-2010 kettenis

Close race between the acpi thread and wscons ioctls running in process
context. We might rip this out post-release and replace it with a solution
that serializes things in a more obvious way.

ok deraadt@, miod@ and "those crazy texans who ok anything"


# 1.208 08-Aug-2010 deraadt

Report the acpi sleep states when on a RAMDISK; ok krw


# 1.207 07-Aug-2010 kettenis

Enforce acpi register access rescrictions.

ok mlarkin@, deraadt@, tested by marco@


# 1.206 07-Aug-2010 deraadt

upon resume, notify all the acpiac and acpibat drivers to update
their status. some laptops do not do this automatically.
ok kettenis canacar


# 1.205 06-Aug-2010 marco

Always call _PSW on _LID devices that support it to enable lid open to
resume the machine. Conversely disable it when the machine wakes up.

Tested by several ok deraadt


# 1.204 06-Aug-2010 deraadt

As part of a revamp of the PM1/GPE code, I write this diff to look for
unmanaged EN & STS bits in the PM1 register at interrupt time and report
them. As a side effect this splits the STS acknowledgement into two writes
(for power, and sleep) instead of one. The printf that is added (to spot
unmanaged STS bits) has not yet been triggered as far as we know.

Before the "write to PM1 registers at the right offset" diff went in, this
was not neccessary. But newer thinkpads do not have a working soft power
button without this diff.

We have no idea why.
ok mlarkin kettenis


# 1.203 05-Aug-2010 deraadt

PM1 power button events were generating 'fake events' to acpibtn PWRB,
but if there is no such device these were being tossed. Instead, arrange
for all power button handlers to flag a softc variable which is processed
by the thread; so that one place will know to request a powerdown (psignal
to init)
ok marco mlarkin


# 1.202 05-Aug-2010 deraadt

rename a variable which is annoyingly poorly named


# 1.201 05-Aug-2010 mlarkin

Write to the correct offset for various ACPI pmreg registers. Writing to
the correct register does properly clear the wake status bits and fixed
event bits, which allows some machines to have more than 1 suspend cycle.

bug spotted by deraadt@

tested by deraadt@ and myself on a half-dozen machines and others on
hackers@

ok deraadt@, marco@


# 1.200 05-Aug-2010 deraadt

Fix the power button. The pm1 register is 16 bits in size.
My mistake, a while back.
tested by krw


# 1.199 04-Aug-2010 kettenis

Print supported sleep states.

ok marco@, phessler@, deraadt@


# 1.198 03-Aug-2010 mlarkin

Use the proper flag for re-enabling certain hardware events (power btn,
sleep btn). The (incorrect) flag prevoiously used coincidentally had the
same value, so this shouldn't cause any different behavior than before.

tested on a variety of machines (i386, amd64, sp, mp) - no behavioral
change seen

ok deraadt@


# 1.197 28-Jul-2010 marco

Make all gasio failures look and behave the same.


# 1.196 28-Jul-2010 mlarkin

Delay a little longer on suspend before giving up. Fixes machines that
sometimes don't suspend because the sleep transition takes too long
(symptoms like failing to sleep but the backlight stays on).

ok deraadt@


# 1.195 27-Jul-2010 deraadt

remove XXX; EC can now be attached early when needed; ok jordan


# 1.194 27-Jul-2010 deraadt

more brackets feels good


# 1.193 27-Jul-2010 deraadt

XXX annotate a block in acpi_enter_sleep_state() that is pretty worrying


# 1.192 27-Jul-2010 deraadt

horrific indentation that nearly made me puke


# 1.191 27-Jul-2010 mlarkin

Set the sleeping indicator light on machines that support it, in case SMI
doesn't do it for us. Shows the 'moon' LED on older Thinkpads when
sleeping.

Tested on T43p, W500, T510 by me and on X61s by deraadt@.

ok deraadt@


# 1.190 27-Jul-2010 jordan

Early initialization of acpiec if ECDT table exists
Fixes hang when booting thinkpads while docked
ok deraadt


# 1.189 26-Jul-2010 deraadt

remove merge error which was commited


# 1.188 26-Jul-2010 pirofti

Add support for Sony ACPI hotkeys via a new driver: acpisony(4).

Currently it only works for the suspend button and tries to do right for
the brightness events, but I haven't found a Sony laptop that like to do
right so far.

In the future I want to make the brightness keys work on all Sony's and
also add support for the zoom hotkeys and whatever other funky keys I can
find on those things.

Okay deraadt@.


# 1.187 22-Jul-2010 deraadt

pretty large cleanup of SMALL_KERNEL


# 1.186 21-Jul-2010 deraadt

in the acpi GPE handler, late ack level interrupts not edge interrupts.
Duh.


# 1.185 21-Jul-2010 deraadt

Merge the edge and level gpe handlers into one (now possible since the
gpe structure tells us whether the gpe is edge or level)


# 1.184 21-Jul-2010 deraadt

delete #if 0 code we are not going to use; ok marco


# 1.183 20-Jul-2010 deraadt

make the mask code for acpi_enable_onegpe() match other similar code


# 1.182 20-Jul-2010 deraadt

Handle the acpi interrupt controller with proper edge/level handling,
wrapped in spltty to avoid racing against the interrupt controller.
Repair the gpe bit masking code while there, and do operations in the
right order
ok kettenis mlarkin, help from jordan at figuring out the order of
operations.


# 1.181 20-Jul-2010 matthew

When we're preparing to go to sleep, only call bufq_restart() if other
preparations failed.

ok kettenis@, thib@, deraadt@


# 1.180 19-Jul-2010 deraadt

permit compilation with ACPI_DEBUG


# 1.179 19-Jul-2010 deraadt

there is no need for a seperate dsdt_softc pointer; dsdt does not have a
softc but is just a part of the single acpi driver.
ok jordan


# 1.178 19-Jul-2010 deraadt

acpi_delay() is no longer needed; ok jordan


# 1.177 18-Jul-2010 mlarkin

Clear event status on resume, to avoid going back to sleep/reboot/shutdown
immediately after resume. Tested by a bunch of people on hackers@ on
various hardware. Fixes at least Toshiba NB200 and Gateway LT3103u.

ok marco


# 1.176 13-Jul-2010 deraadt

rename some thread related interfaces so they are logical


# 1.175 10-Jul-2010 jordan

Verify _ADR bdf is correct (some systems return 0xffff)
Works on Dell x9xx and Studio 1555


# 1.174 09-Jul-2010 jordan

Clear the noise


# 1.173 08-Jul-2010 jordan

Add mapping for ACPI device to PCI bdf (match autoconf tree)
Simplify resource parsing function to use buffer argument
Convert namespace linked lists to use queue macros
ok marco@, deraadt@


# 1.172 06-Jul-2010 deraadt

Use spltty() to lock downcalls from apm against the information being
modified by the acpi thread.
ok kettenis


# 1.171 06-Jul-2010 deraadt

Don't use a workq to do the suspend, because it races aginst the acpi
thread. Instead, just tell the acpi thread to do the suspend for us.
This makes apmd & zzz work correctly. While here, have acpithinkpad
attempt to post the event to apm before prompting the suspend itself.
ok kettenis marco mlarkin


# 1.170 05-Jul-2010 mlarkin

Reset register SCI_EN on resume. The ACPI spec clearly states we don't have
to do this, but many machines don't, or forget to, which leads to all sorts
of problems on resume (broken battery stats, overheating, failure to resume
more than once, etc).

tested by many, ok marco@, deraadt@


# 1.169 01-Jul-2010 thib

do bufq quiesce again, since vnd/the bufq_done call in
biodone has been fixed.


# 1.168 01-Jul-2010 jordan

Backout recent AML changes, breaks on R210 and others


# 1.167 01-Jul-2010 jordan

Simplified aml_parse_resource call, cleanup code
ok marco


# 1.166 01-Jul-2010 jordan

Fix subordinate bus # for multi-root PCI buses
ok marco, kettenis


# 1.165 30-Jun-2010 thib

Disable/partially backout the bufq quiesce changes as this
is causing havoc with vnds and release must be buildable.


# 1.164 29-Jun-2010 deraadt

Fix ramdisks (new code was placed inside a SMALL_KERNEL chunk)


# 1.163 29-Jun-2010 jordan

Add support for mapping ACPI to PCI devices
ok kettenis, deraadt


# 1.162 29-Jun-2010 kettenis

Call bufq_quiesce() on suspend to stop all I/O and bufq_restart on resume.
Makes suspend/resume work much better while doing disk I/O.

ok mlarkin@


# 1.161 29-Jun-2010 tedu

60 is a more reasonable guess for number of minutes in an hour than 100.
spotted by TeXitoi. ok deraadt


# 1.160 27-Jun-2010 jordan

Add _acpi_matchhids routine, precursor to pci device match


# 1.159 27-Jun-2010 mlarkin

Clear ACPI fixed event status on resume (power buttons/etc) so that some
machines don't immediately resume after suspending to S3.

Tested by kettenis@ and I on 5 or 6 machines, deraadt@ "yeah, looks good"


# 1.158 27-Jun-2010 jordan

Cleaned up acpi_maptable, adds to table list directly


# 1.157 07-Apr-2010 deraadt

If we have done a wsdisplay_suspend(), and ACPI decides it does not want to
suspend, we must roll back by calling wsdisplay_resume() or the virtual
console switching will be locked forever
ok oga


# 1.156 07-Apr-2010 kettenis

Make suspend/resume work on MP machines (running an MP kernel). Joint work
from mlarkin@ and me, with some amd64 fixes thrown in by deraadt@

ok marco@, deraadt@, pirofti@, mlarkin@


# 1.155 31-Mar-2010 kettenis

Bring /dev/apm support in line with apm(4). Make the suspend button send
a suspend request event to apmd(8) instead of suspending immediately. Also
keep track of whether /dev/apm and /dev/apmctl are currently open, such that
we can still suspend immediately if apmd(8) isn't running.

ok deraadt@, marco@, pirofti@, jsing@, oga@


# 1.154 30-Mar-2010 oga

Prevent the apmd/x races for good.

When we hit suspend time, go through all wsdisplays on the system. if
they are in mode MAPPED, but not MODE_DUMBFB then if possible do a full
vt switch to a !mapped vt, and prevent switching back until resume time.
This has to be called from MD code because this involves userland
running so that X can run the vt switch signal handler. This way, any
case where we are using the "poke registers from userland" model, we
will not be on the hardware when we go down, so the kernel can actually
handle thing properly.

Tested on several acpi laptops (by kettenis@ and ian@), x40 (me and
beck@ at LEAST) and zaurus (me). Maybe others, but if so I forgot who at
this time..

Idea from deraadt somewhere over the Faroe Islands (I thought of a
similar thing myself a while ago). Much prompting from him. Ok and
comments miod@


# 1.153 25-Mar-2010 oga

Fix & vs. && in the kqfilter code.

Based on a comparison with the apm code.

ok deraadt@, kettenis@


Revision tags: OPENBSD_4_7_BASE
# 1.152 26-Nov-2009 mlarkin

Visual (and on some machines, audio) feedback while the machine is
resuming via calls to aml _SST method. This also makes the "moon" LED on
thinkpads to blink during resume and cease when resume is completed.

ok pirofti@


# 1.151 26-Nov-2009 deraadt

Toggle into "cold" when we we disable interrupts, because tsleeping
in the AML interpreter and context switching into userland processes
tends to sometimes pop the ACPI trampoline page out of our address
space. Makes lots more machines work.
ok kettenis mlarkin


# 1.150 24-Nov-2009 jsg

in the resume path evaluate _WAK before dealing with device drivers
like the spec says, not after.

ok mlarkin@


# 1.149 24-Nov-2009 deraadt

abort suspends on SMP systems until that gets written; ok mlarkin


# 1.148 23-Nov-2009 mlarkin

pci should return the result of it's children's suspend/resume calls back
to its parent.

handle suspend failure case and unwind if devices have an activate function
that returns failure.

ok deraadt@, kettenis@


# 1.147 23-Nov-2009 mlarkin

acpi* devices need to call their children on suspend/resume.
ok deraadt@


# 1.146 23-Nov-2009 pirofti

Remove ACPI_SLEEP_ENABLED checks.

This enables by default the suspend/resume paths in the kernel.

Okay deraadt@.


# 1.145 23-Nov-2009 deraadt

knf and spacing that are super annoying


# 1.144 23-Nov-2009 mlarkin

Hook up Theo's new children suspend/resume function, as well as more ACPI
suspend/resume glue.

ok deraadt@


# 1.143 26-Oct-2009 deraadt

Do not do strncmp comparisons on dv_xname because longer device names which
look similar could arrive in the future. Instead, compare directly against
dv_cfdata->cf_driver->cd_name
Issue originally spotted by miod


# 1.142 26-Oct-2009 jordan

Verify checksum+address when loading ACPI tables.
Some systems had invalid entries in RSDT/XSDT.
ok marco@


# 1.141 23-Jul-2009 cnst

New aibs(4) driver for ASUSTeK AI Booster (ACPI ATK0110) hardware monitoring,
with sensor state support through limits provided by the ACPI.

Tested on several ASUS motherboards kindly networked by Sam Fourman Jr.

ok deraadt marco jordan


Revision tags: OPENBSD_4_6_BASE
# 1.140 03-Jun-2009 pirofti

Import acpi power resource driver. This is needed for suspend/resume logic.

Okay jordan@ and marco@.


# 1.139 03-Jun-2009 pirofti

Add acpivideo support. This adds brightness support for all laptops
except thinkpads, they will use the acpithinkpad driver. The driver is
also hooked into wsconsole. So brightness can be adjusted via:

$ wsconsctl display.brightness=<percentage>

This is very helpfull on some laptops that have a nasty bios and get two
steps instead of one when pressing the brightness button.

Tested on various dell, fujitsu, acer, samsung and other laptops.

Okay marco@, miod@. Suggestions from kettenis@.
Lots of reviews and help from miod@, thanks!


# 1.138 03-Jun-2009 jordan

Added ejectable bay for IDE cdroms
ok marco@


# 1.137 30-Apr-2009 marco

Oga changed apm ioctls and forgot to grep. Temporary work around by hitting
the sleep button when we get any request. This interface will chnage at
some point.
ok oga


# 1.136 19-Apr-2009 krw

Call acpi_sleep_walk() when powering down just like when entering
any other sleep state. This tells acpi wake devices to not wake up
the box until manual powering up resets their status. Makes my Dell
GX520 stay powered off in response to 'halt -p'.

In snaps for a while.

ok marco@ (several times) deraadt@


# 1.135 17-Apr-2009 pirofti

Add device ranking support for future sleep/wake developments.

okay marco@


# 1.134 11-Mar-2009 jordan

Cleanup.. use aml_evalinteger instead of aml_evalname
ok marco@


# 1.133 10-Mar-2009 jordan

Fix cut/paste error.. call _BFS not _PTS
ok marco@


Revision tags: OPENBSD_4_5_BASE
# 1.132 21-Feb-2009 marco

overzealous cut/paste; found by Vladimir Kirillov <proger@uaoug.org.ua>


# 1.131 20-Feb-2009 marco

Oops didn't mean for Debugger() to remain there.


# 1.130 19-Feb-2009 marco

suspend/resume bits so that we can develop this in tree. This is disabled.
code from mlarkin and me
help from art,toby,jordan and several others
ok jordan, go for it deraadt


# 1.129 10-Feb-2009 jordan

Fix null pointer for shutdown if acpi not enabled
ok marco@


# 1.128 04-Feb-2009 kettenis

Some systems that claim to be ACPI 2.0 apparently don't provide an XSDT
address. Make the code fall back on the RSDT on those systems.

From Joerg Niendorf <p3y80@internode.on.net>. Fixes PR 5986.


# 1.127 17-Jan-2009 krw

A couple of stray malloc/memset -> malloc(M_ZERO), a bit of knf and
lose a couple of unnecessary casts. No functional change.

Feedback from kettenis@

ok kettenis@ marco@


# 1.126 06-Nov-2008 marco

Collapse all sanity checks of hids into one function.

Originaly from jordan


Revision tags: OPENBSD_4_4_BASE
# 1.125 02-Jul-2008 fgsch

add acpivideo. for now almost an empty skel so others can work on
suspend/resume. eventually it will also manage output switching and
brightness where supported. prodded by marco@


# 1.124 11-Jun-2008 marco

Fix $OpenBSD$ for once and for all


# 1.123 08-Jun-2008 deraadt

aaa_dev was being set from an uninitialized variable; ok toby


# 1.122 07-Jun-2008 marco

Kill ACPIVERBOSE. It was never used and is worthless to boot.

comments jmc
ok toby


# 1.121 06-Jun-2008 marco

Shrink acpi some more for floppies and while here re-enable acpi on the
floppies. Fixes crazy sony vaio kettenis has.

tested by deraadt
ok deraadt jordan kettenis


# 1.120 01-Jun-2008 marco

Fix \n nit in SMALL_KERNEL.


# 1.119 01-Jun-2008 marco

Re-enable new parser. Lots of testing by various folks. Fits all media
now too.

ok kettenis deraadt


# 1.118 16-May-2008 dlg

back out jordans new parser.

it didnt have enough oks from the right people, it did break machines
that were previously working, and it bloated the RAMDISKS and install
media beyond the constraints of the media.

requested by deraadt@


# 1.117 14-May-2008 jordan

Adding new ACPI Parser code
Old guts of code still exists, needs to be torn out next
ok marco@
ok brad@


# 1.116 27-Apr-2008 jcs

add acpithinkpad(4), an acpi support driver for ibm/lenovo
thinkpads.

provides functionality that the bios did on older, non-acpi
thinkpads like respond to screen brightness keys and toggle the
bluetooth device on and off.

ok deraadt@ and marco@


# 1.115 24-Apr-2008 jsing

Introduce acpiasus(4), a driver for the ACPI based hotkeys found in many
ASUS laptops (including the ASUS EeePC) - largely based on NetBSD's
asus(4) driver. On the ASUS EeePC this allows us to enable/disable
wireless, change screen brightness and use the volume keys.

ok jsg@, weingart@


# 1.114 12-Apr-2008 kettenis

Get rid of acpi_s5 global variables; simply send SIGUSR2 instead of SUGUSR1
to tell init(8) to power down the machine.

ok krw@


Revision tags: OPENBSD_4_3_BASE
# 1.113 21-Jan-2008 jsg

Check against RESET_REG_SUP in FADT before doing the acpi reset dance
after all, not doing so appears to break more machines than it fixes.

ok marco@


# 1.112 05-Jan-2008 kettenis

Never attach more acpicpu(4) devices than cpu(4) devices. Always attach
the first acpicpu(4) device on non-MULTIPROCESSOR kernels, regardless of its
ACPI processor ID.

ok mikeb@


# 1.111 05-Dec-2007 deraadt

Hang acpi(4) below bios(4) instead of mainbus(4). This lets us move acpi(4)
before pcibios(4), and then.... if acpi(4) attaches, skip pcibios(4) since
it messes the machines up. Fixes claudio's HP dl320 G5, and almost assuredly
others.
ok kettenis


# 1.110 05-Dec-2007 deraadt

repair acpiopen/acpiclose and friends to handle the /dev/apmctl interface,
so that apmd(8) can actually work correctly against acpi(4), and thus,
even do the fancy -C/-A stuff; also tested by jmc


# 1.109 02-Dec-2007 fgsch

fix the trailing space for real now.


# 1.108 02-Dec-2007 fgsch

avoid trailing spaces on output; marco@ ok.


# 1.107 27-Nov-2007 kettenis

Fix off-by-one that made us attach acpicpu0 and acpicpu1 on GENERIC kernels.

ok marco@


# 1.106 25-Nov-2007 jsg

When available and ACPI is enabled use the ACPI reset method
to reboot. This is required for some quirky sis machines
and likely others to reboot.

Tested by many. 'commit early commit often' deraadt@


# 1.105 19-Nov-2007 deraadt

sigh, amd64 still uses that stupid acpi_enabled variable...


# 1.104 19-Nov-2007 deraadt

softc's come initialized to 0, you know...


# 1.103 19-Nov-2007 deraadt

store revision; from jsg


# 1.102 19-Nov-2007 kettenis

Initialize sc_facs earlier.

ok deraadt@, beck@, canacar@


# 1.101 16-Nov-2007 deraadt

fix the bus_space #define nightmare, so that amd64 and i386 are much more
uniform. as a result shared code like acpi needs less #ifdef's
ok marco kettenis


# 1.100 15-Nov-2007 deraadt

remove the #ifdef ACPI_ENABLE. the EC codepath is either safe enough (and
will be improved), or it is unsafe because AML assumes it must be there
ok almost everyone


# 1.99 15-Nov-2007 mikeb

Make acpicpu(4) attach to the existing CPUs only.

Problem reported by form@. Fix idea by kettenis@.
Lots of help from deraadt@. Tested by deraadt@, form@ and me.

ok deraadt marco


# 1.98 14-Nov-2007 deraadt

spacing


# 1.97 12-Nov-2007 deraadt

correct : vs , in attach-time messages


# 1.96 06-Nov-2007 deraadt

all ACPI_ENABLE kernels need acpi_interrupt()


# 1.95 06-Nov-2007 deraadt

move acpi_read_pmreg() and acpi_write_pmreg() out of SMALL_KERNEL so
that acpi ramdisks can compile (obvious solution)


# 1.94 05-Nov-2007 canacar

Establish the acpi interrupt earlier. Fixes problems with some
GENERIC.MP kernels not getting acpi interrupts. This is a
workaround until the real issue with late establishment is fixed.
ok weingart@, beck@, gwk@


# 1.93 03-Nov-2007 jordan

Added support for displaying wakeup devices
ok beck@,weingart@,gwk@


# 1.92 11-Oct-2007 kettenis

When walking the tree yo find _PRT methods, check whether the device is
actually enabled and functioning. Similar to what we do for _INI.

ok weingart@, beck@


# 1.91 08-Oct-2007 krw

More simple memset(,0,) -> M_ZERO changes. In this batch move to
size(*p) as the first malloc() parameter where p is declared locally
and thus easy to check. Add M_ZERO to gpe_table allocation in acpi.c
even though there is no obvious bzero or memset nearby.


# 1.90 13-Sep-2007 mk

Remove unnecessary memset() (see aml_evalnode(9)) and a bit of KNF.

ok toby


# 1.89 13-Sep-2007 weingart

Implement ACPI 6.5.1 spec tree walk for _STA and _INI.
Fixes mk's laptop. No regressions so far (thank you to the
testers).

ok gwk@, mk@, marco@


Revision tags: OPENBSD_4_2_BASE
# 1.88 17-Apr-2007 mk

Now that acpidock no longer hangs machines during boot (at least
according to the reports I've been getting) it should be safe to enable
it again, so remove the #if 0 stuff around it.

ok marco


# 1.87 11-Apr-2007 jordan

Added changes for C-State
ok marco@


# 1.86 26-Mar-2007 marco

Calculate minutes left more accurately.

From giovanni <qgiovanni@gmail.com>


Revision tags: OPENBSD_4_1_BASE
# 1.85 03-Mar-2007 mk

Stop looking for acpidock until we fix that stuff that breaks all pre-60
series ThinkPads with acpidock(4) enabled.


# 1.84 22-Feb-2007 marco

We were getting no interrupts in .MP. There are 2 fixes; one in
acpi_machdep.c and move the interrupt establishment down after ioapic
has been initialized.

ok jordan


# 1.83 21-Feb-2007 kettenis

Make an acpi-enabled GENERIC.MP work on systems that only support PIC mode.

ok marco@


# 1.82 19-Feb-2007 jordan

Added acpi_map_address function for mapping GAS/IO registers
ok marco@


# 1.81 18-Feb-2007 jordan

Added dynamic perfect hash for opcodes
ok marco@


# 1.80 18-Feb-2007 marco

Shave off another K pre compression.
Shuffle SMALL_KERNEL functions around so that we only have a single #ifndef


# 1.79 17-Feb-2007 marco

Add SMALL_KERNEL throught acpi to make it fit on boot media.
ok deraadt


# 1.78 13-Feb-2007 marco

Add smarter _INI code. This helps with some dissapearing devices.

ok kettenis jordan


# 1.77 31-Jan-2007 gwk

Introduce acpi_hasprocfvs thats "ACPI Has Processor Frequency and Voltage
Scalling" a global flag set by acicpu attach to indicate that ACPI is
handling fvs through the _PSS objects. This will fix disappearing
EST/powernow when booting kernels with acpi enabled, as acpicpu isnt
compiled into GENERIC yet. Also in cases where acpicpu is built in and
legacy EST/powernow work but acpi is missing the _PSS object, the legacy
routines will be used.

tested by dim@, and Nick Nauwelaerts <nick AT nauwelaerts.net>
ok marco@, dim@


# 1.76 22-Jan-2007 mk

Actually look for docking stations by looking for _DCK nodes.

ok marco


# 1.75 02-Jan-2007 marco

On some systems EC must be attached as the first device. Move EC from
generic _HID devices to its own routine to force first attach. Bug found
and debugged by joris@

Code mostly written by joris@


# 1.74 26-Dec-2006 marco

Add polling to devices that require it such as acpibat and acpitz.
Use the same tsleep/wakeup mechanism as the interrupt code to keep it non-
concurrent.


# 1.73 21-Dec-2006 deraadt

even more knf love


# 1.72 21-Dec-2006 marco

Remove some leftover acpiec gpe/interrupt code.


# 1.71 20-Dec-2006 deraadt

oops, name already used


# 1.70 20-Dec-2006 deraadt

rename __acpi_enable_gpe(); ok marco


# 1.69 20-Dec-2006 deraadt

acpi_unmap_pmregs() is not used, ok marco


# 1.68 19-Dec-2006 deraadt

simplify some low-level methods substantially (shrinking code); ok marco jordan


# 1.67 06-Dec-2006 mk

malloc(..., M_NOWAIT) with no NULL check is bad. If allocation fails,
complain, clean up, and bail out of attach.

input and ok gwk


# 1.66 28-Nov-2006 jordan

Fixed clearing arg for acpi_init_pic
ok kettenis@


# 1.65 27-Nov-2006 kettenis

Don't spam dmesg.


# 1.64 27-Nov-2006 jordan

Fixes for acpiprt routing
Post-parse fixup for namerefs
ok kettenis@


# 1.63 25-Nov-2006 marco

Don't handle acpiev events inside the gpe handler since this causes deadlocks.

ok jordan@


# 1.62 15-Nov-2006 kettenis

Hook up acpi apic interrupt routing support to generic acpi code.

ok jordan@


# 1.61 02-Nov-2006 marco

Kill acpiec attach function and add its functionality to the generic _HID
device attach

prodded and ok jordan


# 1.60 02-Nov-2006 marco

Fix comments to reflect reality.


# 1.59 19-Oct-2006 marco

Replace ACPI ioctls with APM ioctls. This makes ACPI talk the same protocol
as APM therefore userspace can now retrieve power information in the same
fashion as all other apm arches (zaurus, macppc, i386 etc).

To use force the use of acpi device like so: apmd -f /dev/acpi

ok jordan@


# 1.58 19-Oct-2006 jordan

Fixed GPE debug levels
Fixed ref counter on temporary bufferfields
DSDT parsing fully working on IBM Thinkpad
ok marco@


# 1.57 15-Oct-2006 jordan

Removed old GPE handler code remnants
ok marco@


# 1.56 12-Oct-2006 jordan

Massive ACPI changes
Fixed ACPI leaks
new ACPI API
ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.55 30-Jun-2006 gwk

KNF, No functional change,
"go at it" marco@


# 1.54 30-Jun-2006 jordan

Support for calling Embedded Controller via new GPE interface
ok marco@


# 1.53 30-Jun-2006 jordan

Added new code for GPE handling
Also works with ACPIEC; need to fixup acpiec.c to remove original interrupt handler
ok marco@


# 1.52 30-Jun-2006 jordan

Added offset argument for acpi read/write pmreg routines (new GPE code)
ok marco@


# 1.51 31-May-2006 todd

do not call acpi_powerdown() unless acpi has successfully initialized
ok gwk@ canacar@


# 1.50 29-May-2006 canacar

Add embedded controller support.
ok marco@ deraadt@ testing & ok gwk@


# 1.49 19-May-2006 canacar

Minimal thermal zone driver. Monitors thermal zone temperature,
shuts down the system if the 'critical temperature' is reached.
ok marco@


# 1.48 11-Apr-2006 marco

KNF.


# 1.47 11-Apr-2006 gwk

Add acpi_resume(struct acpi_softc *sc), which calls some AML methods,
detected at attach time, currently unused.

ok marco@


# 1.46 11-Apr-2006 gwk

Discover some ACPI sleep related methods, and call them at the
appropriate time in acpi_enter_sleep_state().

ok marco@


Revision tags: OPENBSD_3_9_BASE
# 1.45 26-Feb-2006 marco

Add acpicpu as a device.

ok jordan@


# 1.44 23-Feb-2006 marco

remove dead function.

ok jordan@


# 1.43 22-Feb-2006 marco

Print out all tables it found during autoconf. We'll need this data in
the future.


# 1.42 22-Feb-2006 marco

Make dmesg less verbose.

ok jordan.


# 1.41 22-Feb-2006 jordan

Added parsing of SSDT tables
ok marco@


# 1.40 22-Feb-2006 jordan

Added new aml_register to notify by device id
Moved powerdown code to button device
ok marco@


# 1.39 21-Feb-2006 marco

Readjust debug levels that crept in too low over the last few days.
KNF


# 1.38 21-Feb-2006 marco

Rig the power button so that the machine shuts down correctly.


# 1.37 20-Feb-2006 marco

Add acpibtn device. Remains disabled for now.


# 1.36 19-Feb-2006 jordan

Added code for aml_notify GPE notification callback
ok marco@


# 1.35 19-Feb-2006 grange

Fix acpi_enter_sleep_state() by replacing current ugly hack
with mostly correct sleeping state transition code.

ok brad@ marco@


# 1.34 19-Feb-2006 marco

jordan:
Enable interrupts.
Added peripheral code to make this work.

marco:
Replace softintr and other types of interrupts with a handler with context.
Fixed interrupt magic for GPEs.
Some KNF

This is work done by jordan and I but has to be a single commit.


# 1.33 17-Feb-2006 marco

Remove earlier introduced delays. We really only need them during autconf.

ok jordan@


# 1.32 17-Feb-2006 marco

Add a generic delay function as per the ACPI spec.

from jordan@


# 1.31 17-Feb-2006 marco

Use tsleep instead of delay when !cold.


# 1.30 17-Feb-2006 marco

Argh, we need delays between reads/writes. The backend reads (i2c most likely)
are so slow that values come back as 0 sometimes. We have to do this
despite the spec saying we shouldnt have to. Guess having a faster
parser than others can hurt sometimes.

Idea from jordan.


# 1.29 17-Feb-2006 jordan

Fixed code for handling _INI
Rearranged debug statements
ok marco@


# 1.28 16-Feb-2006 jordan

Added stub for calling _INI
Fixed aml_dowhile for NULL return
ok marco@


# 1.27 16-Feb-2006 jordan

Fixed parser/evaluator to retrieve battery info+state
Cleanup of parser
Dell laptops return battery status
ok marco@


# 1.26 06-Feb-2006 brad

fix a typo so this will compile on amd64.


# 1.25 04-Feb-2006 marco

When ACPI_ENABLED wasn't set but the devices were it had an unknown variable.

Pointed out by ian@.


# 1.24 03-Feb-2006 jordan

Rewrote aml parser yet again...
Fixed bus_space_map for i386/amd64
Added _BST and _BIF to acpibat to get status/info
ok marco@


# 1.23 20-Jan-2006 jordan

More evaluator changes
Removed old evaluator code
Fixed _HID value
ok marco@


# 1.22 19-Jan-2006 jordan

Added support for read/write of PCI Config space
Help jason@
ok marco@


# 1.21 18-Jan-2006 jordan

Added new evaluation routines
Added helper methods for buffer/field evaluation
Fixed broken indentation on dsdt.c

ok marco@


# 1.20 17-Jan-2006 jordan

Major overhaul of the aml parser. Evaluater temporarily disabled.

ok marco@


# 1.19 06-Jan-2006 grange

Check for ACPI control availability only if ACPI_ENABLE defined.


# 1.18 06-Jan-2006 grange

Informative error messages on attach, prettier dmesg.


# 1.17 06-Jan-2006 grange

printf -> dnprintf conversion for debug messages.


# 1.16 05-Jan-2006 grange

Find SLP_TYPx values and store them for future use in
acpi_enter_sleep_state().


# 1.15 05-Jan-2006 grange

Random KNF and cleanup


# 1.14 05-Jan-2006 grange

Provide symbolic names for the acpi sleeping states and use it.


# 1.13 28-Dec-2005 marco

Moved DEVNAME to a acpidev.h

ok jordan@


# 1.12 18-Dec-2005 sturm

add back comment about wrong code

ok marco


# 1.11 16-Dec-2005 marco

Remove test for kbd_reset. Prompted deraadt@, ok jordan@


# 1.10 16-Dec-2005 jordan

Added hooks for powerdown/power button press


# 1.9 16-Dec-2005 jordan

Cleaned up common power management register i/o
Added enter sleep state ioctl
Hooked power button interrupt
ok marco@


# 1.8 16-Dec-2005 marco

Remove autoconf chatter


# 1.7 16-Dec-2005 jordan

Moved DSDT AML parsing into parent ACPI driver
ok marco@


# 1.6 14-Dec-2005 marco

Add AC device glue.


# 1.5 14-Dec-2005 marco

Adjust debug printing levels to something more sane.


# 1.4 13-Dec-2005 marco

Add glue to attach devices to apci.


# 1.3 07-Dec-2005 marco

KNF


Revision tags: OPENBSD_3_8_BASE
# 1.2 10-Jul-2005 grange

Spacing cleanup.


# 1.1 02-Jun-2005 tholo

Start on a basic ACPI framework -- does not do much more than read out the
ACPI tables into kernel memory and attach ACPI and HPET timers currently.

In order to test this code, enabling the devices in GENERIC as well as
the ACPI_ENABLE option is needed. This code does not do any thermal
control yet, so this should be done with care depending on the platform.

In the tree so more people can contribute to making this more fully
featured.

Ok niklas@ grange@ tedu@


# 1.340 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.339 10-Feb-2018 deraadt

Syncronize filesystems to disk when suspending. Each mountpoint's vnodes
are pushed to disk. Dangling vnodes (unlinked files still in use) and
vnodes undergoing change by long-running syscalls are identified -- and
such filesystems are marked dirty on-disk while we are suspended (in case
power is lost, a fsck will be required). Filesystems without dangling or
busy vnodes are marked clean, resulting in faster boots following
"battery died" circumstances.
Tested by numerous developers, thanks for the feedback.


# 1.338 08-Feb-2018 deraadt

After flushing disks, try again to release all memory. We don't need to
save dirty memory to the hibernate space.


# 1.337 08-Feb-2018 deraadt

Toss all releaseable memory, because fragmentation can get in the way
of allocating the hibernate playpen.


# 1.336 08-Feb-2018 deraadt

sr_quiesce() is a new approach for ensuring that softraid drains
output to the disks.

This is part of a larger suspend/resume filesystem-safety diff, which
has been worked on for a couple of months already. Tests by job, krw,
beck, benno, and others. Sometimes even by snapshot users...


# 1.335 29-Nov-2017 kettenis

Add support for GPIO-signaled events.

ok mlarkin@


# 1.334 16-Nov-2017 jcs

Add PCI attachment for dwiic(4) needed by Intel 100 Series machines

ok kettenis


# 1.333 14-Nov-2017 kettenis

Add a few more PNP IDs to the skip lists.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.332 17-Aug-2017 stsp

Make dwiic(4) attach its ACPI parent dependencies.
This change makes the Asus E200HA keyboard work.

Original analysis and patch by Cesare Gargano
ok kettenis@


# 1.331 09-Aug-2017 dcoppa

Add "PNP0303" (8042 PS/2 Controller) to acpi_isa_hids[]

ok kettenis@


# 1.330 22-Jul-2017 jcs

acpibat: look for _BIX first before falling back on _BIF

ACPI 4.0 deprecated _BIF for battery status so some newer machines
are exporting _BIX which extends _BIF's package with a few extra
fields. When using _BIX, export the new discharge cycle count as a
sensor.

feedback from kettenis and anton
ok deraadt


# 1.329 20-Jul-2017 kettenis

Start a list of isa(4) device names such that we can avoid printing them
as "not configured".

ok mikeb@, millert@, deraadt@


# 1.328 08-Apr-2017 deraadt

Sensors are run as callbacks inside tasks. During suspend a sensor
could be running inside a driver that will be force-detached, or due
to tsleep end up disrupting the softstate/hardstate contract. At
suspend time, quisce all these callbacks by waiting for completion.
This issue has never been observed for real, but may be implicated in
suspend/resume failures.
ok kettenis guenther mlarkin


# 1.327 08-Apr-2017 deraadt

Two prototypes for acpi_maptable; merge to one place


Revision tags: OPENBSD_6_1_BASE
# 1.326 27-Mar-2017 deraadt

Now that hibernate_alloc() only has clean success/failure, don't
need to call hibernate_free() to clean up a partial mess.
ok mlarkin kettenis


# 1.325 13-Mar-2017 deraadt

allow ramdisks to build; ok jcs


# 1.324 12-Mar-2017 jcs

try to attach acpisbs first and if successful, don't attach acpibat


# 1.323 02-Mar-2017 natano

Add a new sysctl machdep.lidaction. The sysctl works as follows:

machdep.lidaction=0 # do nothing
machdep.lidaction=1 # suspend
machdep.lidaction=2 # hibernate

lidsuspend is just an alias for lidaction, so if you change one, the
other one will have the same value. The plan is to remove
machdep.lidsuspend eventually when people have upgraded their
/ets/sysctl.conf.

discussed with deraadt, who came up with the new MIB name
no objections mlarkin
ok stsp halex jcs


# 1.322 28-Feb-2017 natano

Untangle abstract sleep modes from ACPI sleep states. This paves the way
for further work in the area.

idea and ok deraadt
ok mlarkin


# 1.321 22-Feb-2017 jcs

add acpisbs, an acpi smart battery subsystem driver reading data
over smbus

currently disabled because it conflicts with acpibat

ok deraadt, kettenis


# 1.320 14-Jan-2017 kettenis

Revert aml_rdpciaddr changes; breaks several machines that were working
before.


# 1.319 11-Jan-2017 jsg

Increase the size of the buffers used for _HID and _CID strings to be
able to fit "Hyper_V_Gen_Counter_V1".

ok mlarkin@ deraadt@


# 1.318 08-Jan-2017 kettenis

Make aml_rdpciaddr() work for for devices behind bridges. The code now
uses the mapping between AML nodes and PCI devices that we establish.
Because _INIT methods may end up calling aml_rdpciaddr(), make sure we
create that mapping early. Also handle devices that aren't actually present.
These devices are now included in the mapping and reads will return an
all-ones patterm whereas writes are a no-op..

ok mlarkin@


# 1.317 25-Oct-2016 pirofti

Add the resource index to aml_parse_resource() callback function.

This is needed by an upcoming acpiec commit that handles machines breaking
the current ACPI specifications.

Change suggested by and ok kettenis@, guenther@


# 1.316 18-Sep-2016 guenther

If the APIC table (MADT) isn't present then ci_acpi_proc_id won't be set
when acpicpu tries to attach, leading to a NULL dereference. The ACPI
processor object on the Dells where this was reported is bogus and useless
anyway so apply the same test when deciding whether to attach to processor
objects as acpicpu would when matching them to cpus.

Problem reported by Eivind Eide (xenofil (at) gmail.com) and
Johan Huldtgren (johan+openbsd-bugs (at) huldtgren.com).
Testing by Eivind Eide.
ok kettenis@ mlarkin@


# 1.315 03-Sep-2016 naddy

Write the system time back to the RTC every 30 minutes.
This fixes the problem that long-running machines which were not
shut down properly would reboot with a badly offset system time.

hints and ok kettenis@


# 1.314 31-Aug-2016 mlarkin

Don't put a device in the wake device list if _STA says it's not there.

Fixes at least one machine that woke up from sleep / poweroff immediately.

ok kettenis@, tested by kettenis and martijn@


# 1.313 28-Jul-2016 kettenis

Store the acpi processor ID/UID in struct cpu_info, and use it to attach
acpicpu(4) drivers to the right cpu(4).

ok mlarkin@, guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.312 10-Jun-2016 kettenis

The power management timer has been made optional in ACPI 5.0A, so don't
attempt to attach acpitimer(4) if the timer isn't present.

Pointed out by jcs@

ok jcs@


# 1.311 08-May-2016 kettenis

Make sure devices listed by a device's _DEP method are attached before
we attach the device itself. The _DEP methode, introduced in ACPI 5.0,
is a hint that the device in question depends on OpRegion support from
the devices listed by _DEP, so we have to order them the proper way.
To prevent us from attaching those devices again when we encounter them
later walking down the device tree, keep track of our attempts to
attach a device driver to them using aflag in the aml_node structure.

ok guenther@


# 1.310 25-Apr-2016 pirofti

We attach acpihpet(4) and acpiec(4) without using the HID, so do not
report it in the dmesg.

OK deraadt@, mlarkin@.


# 1.309 02-Apr-2016 kettenis

Reduce dmesg spam by knocking out some acpi hardware IDs that don't need a
driver.

ok mpi@


# 1.308 29-Mar-2016 kettenis

Check if a device is present (using _STA) in acpi_foundhid. This prevents
us from reporting devices as "not configured" that aren't actually present,
and allows us to remove duplicated code from several drivers.

ok deraadt@, ok jsg@


# 1.307 29-Mar-2016 kettenis

Implement a cleaner way to prevent acpivout(4) from attaching if we have
a platform-specific method of controlling the backlight.

ok millert@, mlarkin@, jung@


# 1.306 28-Mar-2016 kettenis

Adding each and every new acpi device driver to acpi_foundhid() is
gettinga bit of the burden. And it really isn't how our autoconf
framework is supposed to work for busses that can be enumerated. All
the drivers already check for a matching _HID in their attach
function. So we can just drop the checks in acpi_foundhid() and let
the drivers sort themselves out.

Print a "not configured" line for devices for which we don't attach adriver.
This may be a bit much as there are devices for which we will not have a
driver. This will be removed if it causes too much dmesg spam.

ok deraadt@, mlarkin@


Revision tags: OPENBSD_5_9_BASE
# 1.305 17-Jan-2016 jsg

Take acpi_foundhid() outside of #ifndef SMALL_KERNEL so the ramdisk
kernel can find and attach dwiic(4) and sdhc(4).


# 1.304 17-Jan-2016 jsg

remove an unused global variable


# 1.303 14-Jan-2016 kettenis

Attach dwiic(4) on the Intel Bay Trail i2c controllers as well.


# 1.302 12-Jan-2016 kettenis

Improve emulation of PM registers on Hardware-redcued ACPI by emulating
SPL_TYPx, SLP_EN and WAK_STS using the new SLEEP_STATUS_REG and
SLEEP_CONTROL_REG registers. Makes the transition into S5 (powerdown) work
on the ASUS X205TA.

ok deraadt@, mlarkin@


# 1.301 12-Jan-2016 jcs

Add dwiic, a driver for the Synopsys DesignWare i2c controller found
on the Samsung ATIV Book 9 laptop. This initial version only
supports ACPI config/attachment.

Add ihidev, a HID-over-i2c driver largely based on uhidev. dwiic
handles attaching ihidev devices found in ACPI.

Add ims, a HID-over-i2c mouse/trackpad driver to get basic cursor
and button functionality from HID-compliant i2c trackpads.

ok kettenis deraadt


# 1.300 11-Jan-2016 kettenis

Make sdhc(4) attach to hardware IDs "80860F14" and "PNP0FFF".


# 1.299 09-Jan-2016 kettenis

Initial support for Hardware-Reduced ACPI. This skips the legacy mode checks
by emulating the PM_CNT register such that SCI_EN is always enabled. It also
skip attaching acpitimer(4) since the relevant registers are no longer there.

ok deraadt@, mlarkin@


# 1.298 06-Jan-2016 kettenis

Follow the spec and check the SCI_EN bit to see if ACPI has already
been enabled and don't bother enabling it again if it is. This should fix
systems that don't implement legacy mode and therefore set ACPI_ENABLE and
ACPI_DISABLE to zero.

ok mlarkin@, yasuoka@


# 1.297 23-Nov-2015 reyk

Back out the CRITICAL change, turn it back from 10% to 25%. Some
people with old/broken batteries didn't like this change, and the
underlying heuristics are wrong.


# 1.296 22-Nov-2015 reyk

The "CRITICAL" APM battery level, that is exported/faked by the ACPI
stack, is currently set to 25%. With 25% battery left, a typical
modern laptop will have at least one hour runtime left. It is an
arbitrary value, and other APM power layers in the kernel set CRITICAL
to something like 5% or 10%, so set it to 10% for now.

OK mlarkin@


# 1.295 28-Sep-2015 deraadt

In low-level suspend routines, set cold=2. In tsleep(), use this to
spit out a ddb trace to console. This should allow us to find suspend
or resume routines which break the rules. It depends on the console
output function being non-sleeping.... but that's another codepath which
should try to be safe when cold is set.
ok kettenis


# 1.294 06-Sep-2015 deraadt

clarify malloc arguments (base + length...)


# 1.293 28-Aug-2015 deraadt

fairly simple sizes for free(); ok tedu


# 1.292 20-Aug-2015 kettenis

Respect the access size when reading or writing to pci config space.
This version also makes sure writes are properly aligned, preventing
the panics seen with the previous incarnation of this change.

Fixes battery status passthrough in vmware, brightness keys on my x220
and who knows what else.

ok mlarkin@, deraadt@


Revision tags: OPENBSD_5_8_BASE
# 1.291 04-Aug-2015 deraadt

If we are setting ACPI_PM1_BM_RLD do it only once, and remember to
repeat this action at resume time.
ok mlarkin guenther


# 1.290 29-Jul-2015 deraadt

Something subtle in previous revision is locking up suspend or booting
on some laptops. Mailing lists threads mention x220 and Asus machines.
Reverting for now.


# 1.289 21-Jul-2015 kettenis

iRespect the access size when reading or writing to pci config space.
Fixes battery status passthrough in vmware and who knows what else.

Tested by many.

ok mlarkin@, deraadt@


# 1.288 17-Jul-2015 mlarkin

fix comments


# 1.287 26-Mar-2015 jsg

Use the 0x10 extended capability to determine if the trackstick
buttons are wired to the trackpad and need to be re-routed to the
trackstick. Without this change the buttons on 2015 thinkpads
get picked up as extended buttons that show up as scroll up/down.

Remove the X1 Carbon 2015 (LEN0048) and X250 (LEN0046) from the
top button area/soft buttons quirks list. Also avoid using the
quirk list entirely if the capability bit is set.

Based on work from jcs@ with feedback from mpi@
ok mpi@ jcs@


# 1.286 20-Mar-2015 kettenis

The ACPI standard says that a PCI function number of FFFF can be used to refer
to all functions on a device. This makes no sense in the context of accessing
PCI config space. Nevertheless stsp@ has a machine with AML that tries to
do config space reads from a device where _ADR encodes function number FFFF.
This is almost certainly buggy AML, but since Linux (and presumably Windows)
boot just fine on this machine we need to work around this. Linux effectively
treats function FFFF as function 7, which isn't present on this machine.
Instead we simulate a read from a non-existent device by returning a bit
pattern of all ones. Writes to function FFFF will still panic.

ok mlarkin@


# 1.285 16-Mar-2015 deraadt

Reduce use of SMALL_KERNEL in acpi, bringing acpiec driver and more gpe
handling into RAMDISK. This is now possible because the install media
has ample room. The goal is to reduce special cases where we may be
skipping (unknown) important operations...
ok mlarkin kettenis


# 1.284 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.283 07-Feb-2015 deraadt

New framework that allows hibernate to pass in entropy from it's fresh
boot.
ok mlarkin


# 1.282 06-Feb-2015 mlarkin

Fix a hibernate crash on some machines due to unmapping a page that
may not have been mapped previously (in the failure to hibernate case).

Also ensure that the lowmem ptp is mapped in all cases (not just MP).

ok kettenis


# 1.281 17-Jan-2015 deraadt

resume_randomness() before spinning up other cpus...


# 1.280 15-Jan-2015 jsg

Define a new wscons mouse type for Synaptics clickpad devices that lack
physical buttons. This will be used if the acpi pnpid for the mouse
matches a list provided by Synaptics found in Linux.

Combined with the xenocara changes this will remove the need for an xorg
config file for the x240/t440/t540 generation of thinkpads.

ok matthieu@ shadchin@ kettenis@


# 1.279 11-Jan-2015 kettenis

Tolerate checksum errors in ACPI tables (but complain about them). Apparently
there are machines out there with broken BIOSen with such checksum errors in
thier RSDT tables. Windows runs fine on these machines, so it probably does
not check. Linux ignores checksum errors as well (and prints a warning).

ok deraadt@, krw@


# 1.278 18-Dec-2014 deraadt

suspend and resume the RNG (at what looks like the right spots)
ok reyk djm


# 1.277 09-Dec-2014 doug

Replace some malloc(n*size,...) calls with mallocarray().

ok tedu@ deraadt@


# 1.276 06-Dec-2014 mpi

If "machdep.lidsuspend" is not set, do not go back to sleep if the lid
is still closed when resuming. This allows docked laptop to be resumed
without opening them.

ok mlarkin@


# 1.275 23-Nov-2014 mlarkin

If a machine has no _LIDs, then none can be open. This diff fixes a problem
where machines without any _LIDs were going back to sleep after hibernate
due to the recent suspend-if-lid-closed-on-resume diff. Noticed on qemu,
which doesn't have any _LIDs.

discussed with deraadt


# 1.274 18-Nov-2014 krw

Nuke yet more obvious #include duplications.

ok miod@


# 1.273 08-Nov-2014 mlarkin

If resuming from sleep (zzz/ZZZ) and the lid is still closed, go back to
sleep. This prevents accidental lid flex or slight opening in a backpack
from waking the machine up and leaving it resumed (powered on).

ok deraadt@


# 1.272 26-Sep-2014 kettenis

Rework piglet and pig allocation. Currently the piglet gets allocated
deep down in the suspend path, where it is really hard to recover from
allocation failure. So allocate the piglet early on in the suspend path.
Also change the piglet and piglet allocation functions to use km_alloc(9)
instead of doing pmemrange magic. This removes a bunch of code which, in the
case of the piglet allocation, is broken since it results in a NULL pointer
dereference. Also switch the piglet allocation to not wait. If we can't
allocate 16MB of phys contig memory on a halfway modern machine we're almost
certainly under a lot of memory pressure and we're better off not trying to
hibernate anyway.

ok mlarkin@


# 1.271 25-Sep-2014 kettenis

Bring back rev 1.249. Now that mp_setperf() has been fixed, it should be
safe to restore perflevel when resuming. Should fix the issues people
see with the processor running at full speed heating up the machine while
the apm command reports that the CPUs are running at a low speed.

ok dcoppa@, deraadt@


# 1.270 23-Sep-2014 kettenis

Release the acpi lock around calls to wsdisplay_suspend() and
wsdisplay_resume(). The X server may adjust the display brightness just
before/after the VT switch when suspending/resuming. In fact, it may
already be blocked adjusting the display brightness for some reason
unrelated to the suspend request.

The VT switch request will eventually time out, but the delay is annoying
and the failed VT switch will lead to problems after resume.

Needed for a future update of the xf86-video-intel driver.

ok mlarkin@


# 1.269 19-Sep-2014 kettenis

Use config_suspend_all(9).


# 1.268 08-Sep-2014 kettenis

Clear the PME Status bit on PCI devices in response to Device Wake events.
Fixes the interrupt storm on the 2009 Mac mini with WOL enabled on its
nfe(4) interface.

ok mlarkin@


Revision tags: OPENBSD_5_6_BASE
# 1.267 20-Jul-2014 mlarkin

Support hibernating to softraid crypto volumes.

much help and ok from deraadt@


# 1.266 16-Jul-2014 mlarkin

Reenable hibernate RLE support and flush+zero all memory after unpack.


# 1.265 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.264 12-Jul-2014 kettenis

Add a function to drop all clean pages on the page daemon queues and call
it when we hibernate.

ok mlarkin@, miod@, deraadt@


# 1.263 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.262 11-Jul-2014 mlarkin

Flush the buffercache to 16MB on hibernate and restore its previous max
size (kern.bufcachepercent) on resume, for better hibernate performance.

ok beck@


# 1.261 10-Jul-2014 blambert

remove unneeded header

ok mlarkin@


# 1.260 01-Jul-2014 kettenis

Adding an additonal delay after each access to SystemMemory or SystemIO
during autoconf makes no sense.

ok deraadt@


# 1.259 23-Jun-2014 kettenis

SystemMemory is used to access memory mapped registers on some machines, so we
must use the bus_space(9) API to access it instead of memcpy(9). Also make
sure we properly align access to fields that need more than byte alignment.

ok guenther@


# 1.258 21-May-2014 mlarkin

Fix invalid / missing format specifiers in several dnprintf statements
(used only when ACPI_DEBUG is enabled)

From a diff on tech@ by Fabian Raetz, with a minor change by me, tested
on i386 and amd64 with ACPI_DEBUG enabled and disabled.


# 1.257 25-Apr-2014 mlarkin

Defer the free of the hibernate structures until later, when it is safe to
do so.

Freeing the hibernate structures too early leads to inadvertent calls into
splx when we aren't ready for them yet. Found when trapping splx calls in
the resume path.

discussed with deraadt@ at m2k14


# 1.256 13-Apr-2014 mlarkin

Fix a badly worded debug message that implied the entire machine was going
to sleep, instead of just the acpi thread going to sleep.


# 1.255 31-Mar-2014 mpi

Change the order of operations during a suspend/resume cycle and
call bufq_quiesce() after executing the DVACT_QUIESCE handlers.

This should be safe since no disk nor controller drivers have such
handler but it will allow us to detach sd(4) devices attached to a
USB bus.

Another benefit pointed out by kettenis@ is that drivers that need
to read a firmware from the disk should be able to do it at resume
time in a DVACT_WAKEUP handler.

ok kettenis@, deraadt@


# 1.254 13-Mar-2014 dlg

get rid of the assumption that the head of the alldevs list is the
"mainbus" device. this breaks when mpath is enabled because it
attaches before mainbus and therefore takes the head position.

have autoconf provide device_mainbus() which looks up mainbus_cd,
and use that instead.

discussed with deraadt who just wants mpath stuff to move forward
despite there being many ways to shine this particular turd.


Revision tags: OPENBSD_5_5_BASE
# 1.253 21-Feb-2014 deraadt

Do the lid control further on in the resume sequence, since this is
AML, and will do WAITOK operations. While cold, that is a bad idea.
We seem to be safe with respect to the spec, I do not see any event
debounce issues here.
ok kettenis, tested by many


# 1.252 20-Jan-2014 claudio

Instead of checking sc->sc_revision <= 1 check that the
fadt->hdr_revision <= 1. This is a better check since it checks
the version of the proper descriptor. This allows the alix APU
to reboot since those boards have currently a wrong sc->sc_revision.
Committing this now so that it can be tested easier.
With and OK jsg@ also fine by derradt@


# 1.251 24-Dec-2013 kettenis

If the FADT has its SMI_CMD set to zero, assume we're only ACPI-only hardware
and don't need to disable SMI ownership of the ACPI hardware registers.

ok mlarkin@


# 1.250 23-Dec-2013 kettenis

Revert previous commit. It makes resume hang on some MP systems running
GENERIC.MP.


# 1.249 16-Dec-2013 kettenis

Restore hw.setperf upon resume like we do for apm(4) on i386. Seems we need
to do this fairly late in the resume to avoid hangs.

ok deraadt@, mlarkin@


# 1.248 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


# 1.247 06-Nov-2013 mpi

Add support for Power Resources for Dx states and the necessary hook
for PCI devices. This hook should be called twice, before and after
changing the power state of a PCI device.

Before setting the device to the new state, the ACPI layer will notify
every power resources linked to the device for that state and make sure
they are turned "_ON". After changing the state of the device, it will
decrement the reference of every power resources linked to that device
for the old state and turn them "_OFF" if they are no longer referenced.

This fixes the no-USB after resume problem seen on various ThinkPad,
problem initialy diagnosed with Alexander Polakov.

ok kettenis@, deraadt@


Revision tags: OPENBSD_5_4_BASE
# 1.246 01-Jun-2013 mlarkin

Stop printing ACPI wakeup devices in dmesg after the 16th wakeup device.

Helps with VMware VMs that report hundreds of wakeup devices.

ok reyk@, deraadt@


# 1.245 31-May-2013 bluhm

Fix the build for a kernel without wd and pciide in its config.
OK deraadt@ tedu@


# 1.244 30-May-2013 deraadt

Enforce ca_activate tree-walks over the entire heirarchy for all events,
cleaning up some shutdown-hook related code on the way.
(A few drivers related to sparc are still skipped at kettenis' request)
ok kettenis mlarkin, tested by many others too


# 1.243 18-Apr-2013 deraadt

pin the acpi thread to the BSP, because AML and SMI cannot be trusted. I
was certain we were doing this already, but it turns out no...


Revision tags: OPENBSD_5_3_BASE
# 1.242 24-Dec-2012 guenther

In acpiioctl(), Return though the splx() path if suser() returns an error

ok deraadt@


# 1.241 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


# 1.240 04-Oct-2012 ehrhardt

Use information provided by ACPI to attach secondary PCI host bridges.

ok kettenis@


# 1.239 07-Sep-2012 kettenis

Add a new function called acpi_pci_min_powerstate() that returns the lowest
power state for a PCI device for the power state (currently S3 or S4) we're
transitioning into. This function is probably too simplistic; there is a lot
of (silly) complexity in the ACPI spec.

ok deraadt@


Revision tags: OPENBSD_5_2_BASE
# 1.238 13-Jul-2012 pirofti

Disable acpivideo(4) on ASUS laptops.

There are some models that panic whenever an event is received.
Some cope, some don't. In order to get this right acpiec(4) requires
major rework. Until then let machines boot.

Discussed with deraadt@.


# 1.237 11-Jul-2012 mlarkin

for now, restrict hibernate to root in the acpi ioctl kernel interface.
normal rules for controlling this interface through apmd still apply.

ok deraadt@


# 1.236 09-Jul-2012 deraadt

tiny little oops in untested hibernate block


# 1.235 09-Jul-2012 deraadt

clean up ioctl() case statements


# 1.234 09-Jul-2012 deraadt

acpi_disable_onegpe() is not used; ok pirofti


# 1.233 24-May-2012 kettenis

If _MIN > _MAX consider a WordBus resource to be invalid. Fixes a panic on
the IBM x3550-M4.

ok mikeb@


# 1.232 29-Mar-2012 mlarkin

Missing #ifdef in previous diff


# 1.231 29-Mar-2012 mlarkin

Prevent hibernation attempts on machines with unsupported disk controllers.
The same check is also performed later in the hibernate sequence, but by
then it's too late to cleanly unwind (presently).

ok deraadt


# 1.230 26-Mar-2012 deraadt

simplify conditionals in acpi_sleep_state()
ok mlarkin


# 1.229 26-Mar-2012 deraadt

hook in the hibernate request code; half of this diff is from mlarkin
ok mlarkin


Revision tags: OPENBSD_5_1_BASE
# 1.228 20-Sep-2011 deraadt

Very early in suspend while the clock is still ticking realtime,
save the clock back to the rtc. The zaurus already did this.
ok phessler miod kettenis


Revision tags: OPENBSD_5_0_BASE
# 1.227 02-Jul-2011 nicm

kqueue attach functions should return an errno or 0, not a plain 1. Fix
the obvious cases to return EINVAL and ENXIO.

ok tedu deraadt


# 1.226 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.225 16-Jun-2011 pirofti

Import acpitoshiba driver.

This is originally written by Hiroyuki Aizu, ported and integrated
in our tree by Javier Vazquez <javama4 AT gmail> with tweaks and
suggestions by me.

Import prodded by deraadt@ and the lack of testers on tech@.


# 1.224 27-Apr-2011 jcs

attach acpithinkpad to newer lenovo models like the x120e

ok deraadt@


# 1.223 22-Apr-2011 jordan

Fix uninitialzied variables and formatting strings (-Wxxx errors)


Revision tags: OPENBSD_4_9_BASE
# 1.222 02-Jan-2011 jordan

Re-add code for ACPI taskqueue, eliminates need for state vars in acpi softc
Serialize processing ACPI tasks for sleep/powerdown/GPE event
ok deraadt@


# 1.221 31-Oct-2010 guenther

Revert last commit: it breaks resume on Thinkpads
"then please back it out" deraadt@


# 1.220 26-Oct-2010 jordan

Add task queue for ACPI gpe and notify handlers
This fixes eject on Dell Latitude dock and an issue on the Dell Mini
battery update.
ok deraadt


# 1.219 07-Oct-2010 jordan

Create separate functions for enabling wake and runtime gpes.
Remove old suspend/resume gpewalk function
ok deraadt, mlarkin


# 1.218 05-Oct-2010 jordan

split acpi_enable_onegpe to separate enable/disable functions
ok deraadt


# 1.217 05-Oct-2010 jordan

Added acpi_disable_allgpes function to clear GPE status
ok deraadt


# 1.216 05-Oct-2010 deraadt

clean up acpi_softc use more


# 1.215 05-Oct-2010 deraadt

delete a bit of #if 0 code


# 1.214 05-Oct-2010 deraadt

hide use of our global acpi_softc pointer a bit better


# 1.213 29-Sep-2010 deraadt

grab the acpi thread lock very early in acpi thread startup; not that it
is likely that anyone will race us... but why be careless.
ok kettenis


# 1.212 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.211 29-Aug-2010 jasper

- add a check for FWRITE here too.

ok miod@


# 1.210 27-Aug-2010 kettenis

Make PCI Power Management optional, and only enable it when acpi(4) attaches.

ok deraadt@


Revision tags: OPENBSD_4_8_BASE
# 1.209 08-Aug-2010 kettenis

Close race between the acpi thread and wscons ioctls running in process
context. We might rip this out post-release and replace it with a solution
that serializes things in a more obvious way.

ok deraadt@, miod@ and "those crazy texans who ok anything"


# 1.208 08-Aug-2010 deraadt

Report the acpi sleep states when on a RAMDISK; ok krw


# 1.207 07-Aug-2010 kettenis

Enforce acpi register access rescrictions.

ok mlarkin@, deraadt@, tested by marco@


# 1.206 07-Aug-2010 deraadt

upon resume, notify all the acpiac and acpibat drivers to update
their status. some laptops do not do this automatically.
ok kettenis canacar


# 1.205 06-Aug-2010 marco

Always call _PSW on _LID devices that support it to enable lid open to
resume the machine. Conversely disable it when the machine wakes up.

Tested by several ok deraadt


# 1.204 06-Aug-2010 deraadt

As part of a revamp of the PM1/GPE code, I write this diff to look for
unmanaged EN & STS bits in the PM1 register at interrupt time and report
them. As a side effect this splits the STS acknowledgement into two writes
(for power, and sleep) instead of one. The printf that is added (to spot
unmanaged STS bits) has not yet been triggered as far as we know.

Before the "write to PM1 registers at the right offset" diff went in, this
was not neccessary. But newer thinkpads do not have a working soft power
button without this diff.

We have no idea why.
ok mlarkin kettenis


# 1.203 05-Aug-2010 deraadt

PM1 power button events were generating 'fake events' to acpibtn PWRB,
but if there is no such device these were being tossed. Instead, arrange
for all power button handlers to flag a softc variable which is processed
by the thread; so that one place will know to request a powerdown (psignal
to init)
ok marco mlarkin


# 1.202 05-Aug-2010 deraadt

rename a variable which is annoyingly poorly named


# 1.201 05-Aug-2010 mlarkin

Write to the correct offset for various ACPI pmreg registers. Writing to
the correct register does properly clear the wake status bits and fixed
event bits, which allows some machines to have more than 1 suspend cycle.

bug spotted by deraadt@

tested by deraadt@ and myself on a half-dozen machines and others on
hackers@

ok deraadt@, marco@


# 1.200 05-Aug-2010 deraadt

Fix the power button. The pm1 register is 16 bits in size.
My mistake, a while back.
tested by krw


# 1.199 04-Aug-2010 kettenis

Print supported sleep states.

ok marco@, phessler@, deraadt@


# 1.198 03-Aug-2010 mlarkin

Use the proper flag for re-enabling certain hardware events (power btn,
sleep btn). The (incorrect) flag prevoiously used coincidentally had the
same value, so this shouldn't cause any different behavior than before.

tested on a variety of machines (i386, amd64, sp, mp) - no behavioral
change seen

ok deraadt@


# 1.197 28-Jul-2010 marco

Make all gasio failures look and behave the same.


# 1.196 28-Jul-2010 mlarkin

Delay a little longer on suspend before giving up. Fixes machines that
sometimes don't suspend because the sleep transition takes too long
(symptoms like failing to sleep but the backlight stays on).

ok deraadt@


# 1.195 27-Jul-2010 deraadt

remove XXX; EC can now be attached early when needed; ok jordan


# 1.194 27-Jul-2010 deraadt

more brackets feels good


# 1.193 27-Jul-2010 deraadt

XXX annotate a block in acpi_enter_sleep_state() that is pretty worrying


# 1.192 27-Jul-2010 deraadt

horrific indentation that nearly made me puke


# 1.191 27-Jul-2010 mlarkin

Set the sleeping indicator light on machines that support it, in case SMI
doesn't do it for us. Shows the 'moon' LED on older Thinkpads when
sleeping.

Tested on T43p, W500, T510 by me and on X61s by deraadt@.

ok deraadt@


# 1.190 27-Jul-2010 jordan

Early initialization of acpiec if ECDT table exists
Fixes hang when booting thinkpads while docked
ok deraadt


# 1.189 26-Jul-2010 deraadt

remove merge error which was commited


# 1.188 26-Jul-2010 pirofti

Add support for Sony ACPI hotkeys via a new driver: acpisony(4).

Currently it only works for the suspend button and tries to do right for
the brightness events, but I haven't found a Sony laptop that like to do
right so far.

In the future I want to make the brightness keys work on all Sony's and
also add support for the zoom hotkeys and whatever other funky keys I can
find on those things.

Okay deraadt@.


# 1.187 22-Jul-2010 deraadt

pretty large cleanup of SMALL_KERNEL


# 1.186 21-Jul-2010 deraadt

in the acpi GPE handler, late ack level interrupts not edge interrupts.
Duh.


# 1.185 21-Jul-2010 deraadt

Merge the edge and level gpe handlers into one (now possible since the
gpe structure tells us whether the gpe is edge or level)


# 1.184 21-Jul-2010 deraadt

delete #if 0 code we are not going to use; ok marco


# 1.183 20-Jul-2010 deraadt

make the mask code for acpi_enable_onegpe() match other similar code


# 1.182 20-Jul-2010 deraadt

Handle the acpi interrupt controller with proper edge/level handling,
wrapped in spltty to avoid racing against the interrupt controller.
Repair the gpe bit masking code while there, and do operations in the
right order
ok kettenis mlarkin, help from jordan at figuring out the order of
operations.


# 1.181 20-Jul-2010 matthew

When we're preparing to go to sleep, only call bufq_restart() if other
preparations failed.

ok kettenis@, thib@, deraadt@


# 1.180 19-Jul-2010 deraadt

permit compilation with ACPI_DEBUG


# 1.179 19-Jul-2010 deraadt

there is no need for a seperate dsdt_softc pointer; dsdt does not have a
softc but is just a part of the single acpi driver.
ok jordan


# 1.178 19-Jul-2010 deraadt

acpi_delay() is no longer needed; ok jordan


# 1.177 18-Jul-2010 mlarkin

Clear event status on resume, to avoid going back to sleep/reboot/shutdown
immediately after resume. Tested by a bunch of people on hackers@ on
various hardware. Fixes at least Toshiba NB200 and Gateway LT3103u.

ok marco


# 1.176 13-Jul-2010 deraadt

rename some thread related interfaces so they are logical


# 1.175 10-Jul-2010 jordan

Verify _ADR bdf is correct (some systems return 0xffff)
Works on Dell x9xx and Studio 1555


# 1.174 09-Jul-2010 jordan

Clear the noise


# 1.173 08-Jul-2010 jordan

Add mapping for ACPI device to PCI bdf (match autoconf tree)
Simplify resource parsing function to use buffer argument
Convert namespace linked lists to use queue macros
ok marco@, deraadt@


# 1.172 06-Jul-2010 deraadt

Use spltty() to lock downcalls from apm against the information being
modified by the acpi thread.
ok kettenis


# 1.171 06-Jul-2010 deraadt

Don't use a workq to do the suspend, because it races aginst the acpi
thread. Instead, just tell the acpi thread to do the suspend for us.
This makes apmd & zzz work correctly. While here, have acpithinkpad
attempt to post the event to apm before prompting the suspend itself.
ok kettenis marco mlarkin


# 1.170 05-Jul-2010 mlarkin

Reset register SCI_EN on resume. The ACPI spec clearly states we don't have
to do this, but many machines don't, or forget to, which leads to all sorts
of problems on resume (broken battery stats, overheating, failure to resume
more than once, etc).

tested by many, ok marco@, deraadt@


# 1.169 01-Jul-2010 thib

do bufq quiesce again, since vnd/the bufq_done call in
biodone has been fixed.


# 1.168 01-Jul-2010 jordan

Backout recent AML changes, breaks on R210 and others


# 1.167 01-Jul-2010 jordan

Simplified aml_parse_resource call, cleanup code
ok marco


# 1.166 01-Jul-2010 jordan

Fix subordinate bus # for multi-root PCI buses
ok marco, kettenis


# 1.165 30-Jun-2010 thib

Disable/partially backout the bufq quiesce changes as this
is causing havoc with vnds and release must be buildable.


# 1.164 29-Jun-2010 deraadt

Fix ramdisks (new code was placed inside a SMALL_KERNEL chunk)


# 1.163 29-Jun-2010 jordan

Add support for mapping ACPI to PCI devices
ok kettenis, deraadt


# 1.162 29-Jun-2010 kettenis

Call bufq_quiesce() on suspend to stop all I/O and bufq_restart on resume.
Makes suspend/resume work much better while doing disk I/O.

ok mlarkin@


# 1.161 29-Jun-2010 tedu

60 is a more reasonable guess for number of minutes in an hour than 100.
spotted by TeXitoi. ok deraadt


# 1.160 27-Jun-2010 jordan

Add _acpi_matchhids routine, precursor to pci device match


# 1.159 27-Jun-2010 mlarkin

Clear ACPI fixed event status on resume (power buttons/etc) so that some
machines don't immediately resume after suspending to S3.

Tested by kettenis@ and I on 5 or 6 machines, deraadt@ "yeah, looks good"


# 1.158 27-Jun-2010 jordan

Cleaned up acpi_maptable, adds to table list directly


# 1.157 07-Apr-2010 deraadt

If we have done a wsdisplay_suspend(), and ACPI decides it does not want to
suspend, we must roll back by calling wsdisplay_resume() or the virtual
console switching will be locked forever
ok oga


# 1.156 07-Apr-2010 kettenis

Make suspend/resume work on MP machines (running an MP kernel). Joint work
from mlarkin@ and me, with some amd64 fixes thrown in by deraadt@

ok marco@, deraadt@, pirofti@, mlarkin@


# 1.155 31-Mar-2010 kettenis

Bring /dev/apm support in line with apm(4). Make the suspend button send
a suspend request event to apmd(8) instead of suspending immediately. Also
keep track of whether /dev/apm and /dev/apmctl are currently open, such that
we can still suspend immediately if apmd(8) isn't running.

ok deraadt@, marco@, pirofti@, jsing@, oga@


# 1.154 30-Mar-2010 oga

Prevent the apmd/x races for good.

When we hit suspend time, go through all wsdisplays on the system. if
they are in mode MAPPED, but not MODE_DUMBFB then if possible do a full
vt switch to a !mapped vt, and prevent switching back until resume time.
This has to be called from MD code because this involves userland
running so that X can run the vt switch signal handler. This way, any
case where we are using the "poke registers from userland" model, we
will not be on the hardware when we go down, so the kernel can actually
handle thing properly.

Tested on several acpi laptops (by kettenis@ and ian@), x40 (me and
beck@ at LEAST) and zaurus (me). Maybe others, but if so I forgot who at
this time..

Idea from deraadt somewhere over the Faroe Islands (I thought of a
similar thing myself a while ago). Much prompting from him. Ok and
comments miod@


# 1.153 25-Mar-2010 oga

Fix & vs. && in the kqfilter code.

Based on a comparison with the apm code.

ok deraadt@, kettenis@


Revision tags: OPENBSD_4_7_BASE
# 1.152 26-Nov-2009 mlarkin

Visual (and on some machines, audio) feedback while the machine is
resuming via calls to aml _SST method. This also makes the "moon" LED on
thinkpads to blink during resume and cease when resume is completed.

ok pirofti@


# 1.151 26-Nov-2009 deraadt

Toggle into "cold" when we we disable interrupts, because tsleeping
in the AML interpreter and context switching into userland processes
tends to sometimes pop the ACPI trampoline page out of our address
space. Makes lots more machines work.
ok kettenis mlarkin


# 1.150 24-Nov-2009 jsg

in the resume path evaluate _WAK before dealing with device drivers
like the spec says, not after.

ok mlarkin@


# 1.149 24-Nov-2009 deraadt

abort suspends on SMP systems until that gets written; ok mlarkin


# 1.148 23-Nov-2009 mlarkin

pci should return the result of it's children's suspend/resume calls back
to its parent.

handle suspend failure case and unwind if devices have an activate function
that returns failure.

ok deraadt@, kettenis@


# 1.147 23-Nov-2009 mlarkin

acpi* devices need to call their children on suspend/resume.
ok deraadt@


# 1.146 23-Nov-2009 pirofti

Remove ACPI_SLEEP_ENABLED checks.

This enables by default the suspend/resume paths in the kernel.

Okay deraadt@.


# 1.145 23-Nov-2009 deraadt

knf and spacing that are super annoying


# 1.144 23-Nov-2009 mlarkin

Hook up Theo's new children suspend/resume function, as well as more ACPI
suspend/resume glue.

ok deraadt@


# 1.143 26-Oct-2009 deraadt

Do not do strncmp comparisons on dv_xname because longer device names which
look similar could arrive in the future. Instead, compare directly against
dv_cfdata->cf_driver->cd_name
Issue originally spotted by miod


# 1.142 26-Oct-2009 jordan

Verify checksum+address when loading ACPI tables.
Some systems had invalid entries in RSDT/XSDT.
ok marco@


# 1.141 23-Jul-2009 cnst

New aibs(4) driver for ASUSTeK AI Booster (ACPI ATK0110) hardware monitoring,
with sensor state support through limits provided by the ACPI.

Tested on several ASUS motherboards kindly networked by Sam Fourman Jr.

ok deraadt marco jordan


Revision tags: OPENBSD_4_6_BASE
# 1.140 03-Jun-2009 pirofti

Import acpi power resource driver. This is needed for suspend/resume logic.

Okay jordan@ and marco@.


# 1.139 03-Jun-2009 pirofti

Add acpivideo support. This adds brightness support for all laptops
except thinkpads, they will use the acpithinkpad driver. The driver is
also hooked into wsconsole. So brightness can be adjusted via:

$ wsconsctl display.brightness=<percentage>

This is very helpfull on some laptops that have a nasty bios and get two
steps instead of one when pressing the brightness button.

Tested on various dell, fujitsu, acer, samsung and other laptops.

Okay marco@, miod@. Suggestions from kettenis@.
Lots of reviews and help from miod@, thanks!


# 1.138 03-Jun-2009 jordan

Added ejectable bay for IDE cdroms
ok marco@


# 1.137 30-Apr-2009 marco

Oga changed apm ioctls and forgot to grep. Temporary work around by hitting
the sleep button when we get any request. This interface will chnage at
some point.
ok oga


# 1.136 19-Apr-2009 krw

Call acpi_sleep_walk() when powering down just like when entering
any other sleep state. This tells acpi wake devices to not wake up
the box until manual powering up resets their status. Makes my Dell
GX520 stay powered off in response to 'halt -p'.

In snaps for a while.

ok marco@ (several times) deraadt@


# 1.135 17-Apr-2009 pirofti

Add device ranking support for future sleep/wake developments.

okay marco@


# 1.134 11-Mar-2009 jordan

Cleanup.. use aml_evalinteger instead of aml_evalname
ok marco@


# 1.133 10-Mar-2009 jordan

Fix cut/paste error.. call _BFS not _PTS
ok marco@


Revision tags: OPENBSD_4_5_BASE
# 1.132 21-Feb-2009 marco

overzealous cut/paste; found by Vladimir Kirillov <proger@uaoug.org.ua>


# 1.131 20-Feb-2009 marco

Oops didn't mean for Debugger() to remain there.


# 1.130 19-Feb-2009 marco

suspend/resume bits so that we can develop this in tree. This is disabled.
code from mlarkin and me
help from art,toby,jordan and several others
ok jordan, go for it deraadt


# 1.129 10-Feb-2009 jordan

Fix null pointer for shutdown if acpi not enabled
ok marco@


# 1.128 04-Feb-2009 kettenis

Some systems that claim to be ACPI 2.0 apparently don't provide an XSDT
address. Make the code fall back on the RSDT on those systems.

From Joerg Niendorf <p3y80@internode.on.net>. Fixes PR 5986.


# 1.127 17-Jan-2009 krw

A couple of stray malloc/memset -> malloc(M_ZERO), a bit of knf and
lose a couple of unnecessary casts. No functional change.

Feedback from kettenis@

ok kettenis@ marco@


# 1.126 06-Nov-2008 marco

Collapse all sanity checks of hids into one function.

Originaly from jordan


Revision tags: OPENBSD_4_4_BASE
# 1.125 02-Jul-2008 fgsch

add acpivideo. for now almost an empty skel so others can work on
suspend/resume. eventually it will also manage output switching and
brightness where supported. prodded by marco@


# 1.124 11-Jun-2008 marco

Fix $OpenBSD$ for once and for all


# 1.123 08-Jun-2008 deraadt

aaa_dev was being set from an uninitialized variable; ok toby


# 1.122 07-Jun-2008 marco

Kill ACPIVERBOSE. It was never used and is worthless to boot.

comments jmc
ok toby


# 1.121 06-Jun-2008 marco

Shrink acpi some more for floppies and while here re-enable acpi on the
floppies. Fixes crazy sony vaio kettenis has.

tested by deraadt
ok deraadt jordan kettenis


# 1.120 01-Jun-2008 marco

Fix \n nit in SMALL_KERNEL.


# 1.119 01-Jun-2008 marco

Re-enable new parser. Lots of testing by various folks. Fits all media
now too.

ok kettenis deraadt


# 1.118 16-May-2008 dlg

back out jordans new parser.

it didnt have enough oks from the right people, it did break machines
that were previously working, and it bloated the RAMDISKS and install
media beyond the constraints of the media.

requested by deraadt@


# 1.117 14-May-2008 jordan

Adding new ACPI Parser code
Old guts of code still exists, needs to be torn out next
ok marco@
ok brad@


# 1.116 27-Apr-2008 jcs

add acpithinkpad(4), an acpi support driver for ibm/lenovo
thinkpads.

provides functionality that the bios did on older, non-acpi
thinkpads like respond to screen brightness keys and toggle the
bluetooth device on and off.

ok deraadt@ and marco@


# 1.115 24-Apr-2008 jsing

Introduce acpiasus(4), a driver for the ACPI based hotkeys found in many
ASUS laptops (including the ASUS EeePC) - largely based on NetBSD's
asus(4) driver. On the ASUS EeePC this allows us to enable/disable
wireless, change screen brightness and use the volume keys.

ok jsg@, weingart@


# 1.114 12-Apr-2008 kettenis

Get rid of acpi_s5 global variables; simply send SIGUSR2 instead of SUGUSR1
to tell init(8) to power down the machine.

ok krw@


Revision tags: OPENBSD_4_3_BASE
# 1.113 21-Jan-2008 jsg

Check against RESET_REG_SUP in FADT before doing the acpi reset dance
after all, not doing so appears to break more machines than it fixes.

ok marco@


# 1.112 05-Jan-2008 kettenis

Never attach more acpicpu(4) devices than cpu(4) devices. Always attach
the first acpicpu(4) device on non-MULTIPROCESSOR kernels, regardless of its
ACPI processor ID.

ok mikeb@


# 1.111 05-Dec-2007 deraadt

Hang acpi(4) below bios(4) instead of mainbus(4). This lets us move acpi(4)
before pcibios(4), and then.... if acpi(4) attaches, skip pcibios(4) since
it messes the machines up. Fixes claudio's HP dl320 G5, and almost assuredly
others.
ok kettenis


# 1.110 05-Dec-2007 deraadt

repair acpiopen/acpiclose and friends to handle the /dev/apmctl interface,
so that apmd(8) can actually work correctly against acpi(4), and thus,
even do the fancy -C/-A stuff; also tested by jmc


# 1.109 02-Dec-2007 fgsch

fix the trailing space for real now.


# 1.108 02-Dec-2007 fgsch

avoid trailing spaces on output; marco@ ok.


# 1.107 27-Nov-2007 kettenis

Fix off-by-one that made us attach acpicpu0 and acpicpu1 on GENERIC kernels.

ok marco@


# 1.106 25-Nov-2007 jsg

When available and ACPI is enabled use the ACPI reset method
to reboot. This is required for some quirky sis machines
and likely others to reboot.

Tested by many. 'commit early commit often' deraadt@


# 1.105 19-Nov-2007 deraadt

sigh, amd64 still uses that stupid acpi_enabled variable...


# 1.104 19-Nov-2007 deraadt

softc's come initialized to 0, you know...


# 1.103 19-Nov-2007 deraadt

store revision; from jsg


# 1.102 19-Nov-2007 kettenis

Initialize sc_facs earlier.

ok deraadt@, beck@, canacar@


# 1.101 16-Nov-2007 deraadt

fix the bus_space #define nightmare, so that amd64 and i386 are much more
uniform. as a result shared code like acpi needs less #ifdef's
ok marco kettenis


# 1.100 15-Nov-2007 deraadt

remove the #ifdef ACPI_ENABLE. the EC codepath is either safe enough (and
will be improved), or it is unsafe because AML assumes it must be there
ok almost everyone


# 1.99 15-Nov-2007 mikeb

Make acpicpu(4) attach to the existing CPUs only.

Problem reported by form@. Fix idea by kettenis@.
Lots of help from deraadt@. Tested by deraadt@, form@ and me.

ok deraadt marco


# 1.98 14-Nov-2007 deraadt

spacing


# 1.97 12-Nov-2007 deraadt

correct : vs , in attach-time messages


# 1.96 06-Nov-2007 deraadt

all ACPI_ENABLE kernels need acpi_interrupt()


# 1.95 06-Nov-2007 deraadt

move acpi_read_pmreg() and acpi_write_pmreg() out of SMALL_KERNEL so
that acpi ramdisks can compile (obvious solution)


# 1.94 05-Nov-2007 canacar

Establish the acpi interrupt earlier. Fixes problems with some
GENERIC.MP kernels not getting acpi interrupts. This is a
workaround until the real issue with late establishment is fixed.
ok weingart@, beck@, gwk@


# 1.93 03-Nov-2007 jordan

Added support for displaying wakeup devices
ok beck@,weingart@,gwk@


# 1.92 11-Oct-2007 kettenis

When walking the tree yo find _PRT methods, check whether the device is
actually enabled and functioning. Similar to what we do for _INI.

ok weingart@, beck@


# 1.91 08-Oct-2007 krw

More simple memset(,0,) -> M_ZERO changes. In this batch move to
size(*p) as the first malloc() parameter where p is declared locally
and thus easy to check. Add M_ZERO to gpe_table allocation in acpi.c
even though there is no obvious bzero or memset nearby.


# 1.90 13-Sep-2007 mk

Remove unnecessary memset() (see aml_evalnode(9)) and a bit of KNF.

ok toby


# 1.89 13-Sep-2007 weingart

Implement ACPI 6.5.1 spec tree walk for _STA and _INI.
Fixes mk's laptop. No regressions so far (thank you to the
testers).

ok gwk@, mk@, marco@


Revision tags: OPENBSD_4_2_BASE
# 1.88 17-Apr-2007 mk

Now that acpidock no longer hangs machines during boot (at least
according to the reports I've been getting) it should be safe to enable
it again, so remove the #if 0 stuff around it.

ok marco


# 1.87 11-Apr-2007 jordan

Added changes for C-State
ok marco@


# 1.86 26-Mar-2007 marco

Calculate minutes left more accurately.

From giovanni <qgiovanni@gmail.com>


Revision tags: OPENBSD_4_1_BASE
# 1.85 03-Mar-2007 mk

Stop looking for acpidock until we fix that stuff that breaks all pre-60
series ThinkPads with acpidock(4) enabled.


# 1.84 22-Feb-2007 marco

We were getting no interrupts in .MP. There are 2 fixes; one in
acpi_machdep.c and move the interrupt establishment down after ioapic
has been initialized.

ok jordan


# 1.83 21-Feb-2007 kettenis

Make an acpi-enabled GENERIC.MP work on systems that only support PIC mode.

ok marco@


# 1.82 19-Feb-2007 jordan

Added acpi_map_address function for mapping GAS/IO registers
ok marco@


# 1.81 18-Feb-2007 jordan

Added dynamic perfect hash for opcodes
ok marco@


# 1.80 18-Feb-2007 marco

Shave off another K pre compression.
Shuffle SMALL_KERNEL functions around so that we only have a single #ifndef


# 1.79 17-Feb-2007 marco

Add SMALL_KERNEL throught acpi to make it fit on boot media.
ok deraadt


# 1.78 13-Feb-2007 marco

Add smarter _INI code. This helps with some dissapearing devices.

ok kettenis jordan


# 1.77 31-Jan-2007 gwk

Introduce acpi_hasprocfvs thats "ACPI Has Processor Frequency and Voltage
Scalling" a global flag set by acicpu attach to indicate that ACPI is
handling fvs through the _PSS objects. This will fix disappearing
EST/powernow when booting kernels with acpi enabled, as acpicpu isnt
compiled into GENERIC yet. Also in cases where acpicpu is built in and
legacy EST/powernow work but acpi is missing the _PSS object, the legacy
routines will be used.

tested by dim@, and Nick Nauwelaerts <nick AT nauwelaerts.net>
ok marco@, dim@


# 1.76 22-Jan-2007 mk

Actually look for docking stations by looking for _DCK nodes.

ok marco


# 1.75 02-Jan-2007 marco

On some systems EC must be attached as the first device. Move EC from
generic _HID devices to its own routine to force first attach. Bug found
and debugged by joris@

Code mostly written by joris@


# 1.74 26-Dec-2006 marco

Add polling to devices that require it such as acpibat and acpitz.
Use the same tsleep/wakeup mechanism as the interrupt code to keep it non-
concurrent.


# 1.73 21-Dec-2006 deraadt

even more knf love


# 1.72 21-Dec-2006 marco

Remove some leftover acpiec gpe/interrupt code.


# 1.71 20-Dec-2006 deraadt

oops, name already used


# 1.70 20-Dec-2006 deraadt

rename __acpi_enable_gpe(); ok marco


# 1.69 20-Dec-2006 deraadt

acpi_unmap_pmregs() is not used, ok marco


# 1.68 19-Dec-2006 deraadt

simplify some low-level methods substantially (shrinking code); ok marco jordan


# 1.67 06-Dec-2006 mk

malloc(..., M_NOWAIT) with no NULL check is bad. If allocation fails,
complain, clean up, and bail out of attach.

input and ok gwk


# 1.66 28-Nov-2006 jordan

Fixed clearing arg for acpi_init_pic
ok kettenis@


# 1.65 27-Nov-2006 kettenis

Don't spam dmesg.


# 1.64 27-Nov-2006 jordan

Fixes for acpiprt routing
Post-parse fixup for namerefs
ok kettenis@


# 1.63 25-Nov-2006 marco

Don't handle acpiev events inside the gpe handler since this causes deadlocks.

ok jordan@


# 1.62 15-Nov-2006 kettenis

Hook up acpi apic interrupt routing support to generic acpi code.

ok jordan@


# 1.61 02-Nov-2006 marco

Kill acpiec attach function and add its functionality to the generic _HID
device attach

prodded and ok jordan


# 1.60 02-Nov-2006 marco

Fix comments to reflect reality.


# 1.59 19-Oct-2006 marco

Replace ACPI ioctls with APM ioctls. This makes ACPI talk the same protocol
as APM therefore userspace can now retrieve power information in the same
fashion as all other apm arches (zaurus, macppc, i386 etc).

To use force the use of acpi device like so: apmd -f /dev/acpi

ok jordan@


# 1.58 19-Oct-2006 jordan

Fixed GPE debug levels
Fixed ref counter on temporary bufferfields
DSDT parsing fully working on IBM Thinkpad
ok marco@


# 1.57 15-Oct-2006 jordan

Removed old GPE handler code remnants
ok marco@


# 1.56 12-Oct-2006 jordan

Massive ACPI changes
Fixed ACPI leaks
new ACPI API
ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.55 30-Jun-2006 gwk

KNF, No functional change,
"go at it" marco@


# 1.54 30-Jun-2006 jordan

Support for calling Embedded Controller via new GPE interface
ok marco@


# 1.53 30-Jun-2006 jordan

Added new code for GPE handling
Also works with ACPIEC; need to fixup acpiec.c to remove original interrupt handler
ok marco@


# 1.52 30-Jun-2006 jordan

Added offset argument for acpi read/write pmreg routines (new GPE code)
ok marco@


# 1.51 31-May-2006 todd

do not call acpi_powerdown() unless acpi has successfully initialized
ok gwk@ canacar@


# 1.50 29-May-2006 canacar

Add embedded controller support.
ok marco@ deraadt@ testing & ok gwk@


# 1.49 19-May-2006 canacar

Minimal thermal zone driver. Monitors thermal zone temperature,
shuts down the system if the 'critical temperature' is reached.
ok marco@


# 1.48 11-Apr-2006 marco

KNF.


# 1.47 11-Apr-2006 gwk

Add acpi_resume(struct acpi_softc *sc), which calls some AML methods,
detected at attach time, currently unused.

ok marco@


# 1.46 11-Apr-2006 gwk

Discover some ACPI sleep related methods, and call them at the
appropriate time in acpi_enter_sleep_state().

ok marco@


Revision tags: OPENBSD_3_9_BASE
# 1.45 26-Feb-2006 marco

Add acpicpu as a device.

ok jordan@


# 1.44 23-Feb-2006 marco

remove dead function.

ok jordan@


# 1.43 22-Feb-2006 marco

Print out all tables it found during autoconf. We'll need this data in
the future.


# 1.42 22-Feb-2006 marco

Make dmesg less verbose.

ok jordan.


# 1.41 22-Feb-2006 jordan

Added parsing of SSDT tables
ok marco@


# 1.40 22-Feb-2006 jordan

Added new aml_register to notify by device id
Moved powerdown code to button device
ok marco@


# 1.39 21-Feb-2006 marco

Readjust debug levels that crept in too low over the last few days.
KNF


# 1.38 21-Feb-2006 marco

Rig the power button so that the machine shuts down correctly.


# 1.37 20-Feb-2006 marco

Add acpibtn device. Remains disabled for now.


# 1.36 19-Feb-2006 jordan

Added code for aml_notify GPE notification callback
ok marco@


# 1.35 19-Feb-2006 grange

Fix acpi_enter_sleep_state() by replacing current ugly hack
with mostly correct sleeping state transition code.

ok brad@ marco@


# 1.34 19-Feb-2006 marco

jordan:
Enable interrupts.
Added peripheral code to make this work.

marco:
Replace softintr and other types of interrupts with a handler with context.
Fixed interrupt magic for GPEs.
Some KNF

This is work done by jordan and I but has to be a single commit.


# 1.33 17-Feb-2006 marco

Remove earlier introduced delays. We really only need them during autconf.

ok jordan@


# 1.32 17-Feb-2006 marco

Add a generic delay function as per the ACPI spec.

from jordan@


# 1.31 17-Feb-2006 marco

Use tsleep instead of delay when !cold.


# 1.30 17-Feb-2006 marco

Argh, we need delays between reads/writes. The backend reads (i2c most likely)
are so slow that values come back as 0 sometimes. We have to do this
despite the spec saying we shouldnt have to. Guess having a faster
parser than others can hurt sometimes.

Idea from jordan.


# 1.29 17-Feb-2006 jordan

Fixed code for handling _INI
Rearranged debug statements
ok marco@


# 1.28 16-Feb-2006 jordan

Added stub for calling _INI
Fixed aml_dowhile for NULL return
ok marco@


# 1.27 16-Feb-2006 jordan

Fixed parser/evaluator to retrieve battery info+state
Cleanup of parser
Dell laptops return battery status
ok marco@


# 1.26 06-Feb-2006 brad

fix a typo so this will compile on amd64.


# 1.25 04-Feb-2006 marco

When ACPI_ENABLED wasn't set but the devices were it had an unknown variable.

Pointed out by ian@.


# 1.24 03-Feb-2006 jordan

Rewrote aml parser yet again...
Fixed bus_space_map for i386/amd64
Added _BST and _BIF to acpibat to get status/info
ok marco@


# 1.23 20-Jan-2006 jordan

More evaluator changes
Removed old evaluator code
Fixed _HID value
ok marco@


# 1.22 19-Jan-2006 jordan

Added support for read/write of PCI Config space
Help jason@
ok marco@


# 1.21 18-Jan-2006 jordan

Added new evaluation routines
Added helper methods for buffer/field evaluation
Fixed broken indentation on dsdt.c

ok marco@


# 1.20 17-Jan-2006 jordan

Major overhaul of the aml parser. Evaluater temporarily disabled.

ok marco@


# 1.19 06-Jan-2006 grange

Check for ACPI control availability only if ACPI_ENABLE defined.


# 1.18 06-Jan-2006 grange

Informative error messages on attach, prettier dmesg.


# 1.17 06-Jan-2006 grange

printf -> dnprintf conversion for debug messages.


# 1.16 05-Jan-2006 grange

Find SLP_TYPx values and store them for future use in
acpi_enter_sleep_state().


# 1.15 05-Jan-2006 grange

Random KNF and cleanup


# 1.14 05-Jan-2006 grange

Provide symbolic names for the acpi sleeping states and use it.


# 1.13 28-Dec-2005 marco

Moved DEVNAME to a acpidev.h

ok jordan@


# 1.12 18-Dec-2005 sturm

add back comment about wrong code

ok marco


# 1.11 16-Dec-2005 marco

Remove test for kbd_reset. Prompted deraadt@, ok jordan@


# 1.10 16-Dec-2005 jordan

Added hooks for powerdown/power button press


# 1.9 16-Dec-2005 jordan

Cleaned up common power management register i/o
Added enter sleep state ioctl
Hooked power button interrupt
ok marco@


# 1.8 16-Dec-2005 marco

Remove autoconf chatter


# 1.7 16-Dec-2005 jordan

Moved DSDT AML parsing into parent ACPI driver
ok marco@


# 1.6 14-Dec-2005 marco

Add AC device glue.


# 1.5 14-Dec-2005 marco

Adjust debug printing levels to something more sane.


# 1.4 13-Dec-2005 marco

Add glue to attach devices to apci.


# 1.3 07-Dec-2005 marco

KNF


Revision tags: OPENBSD_3_8_BASE
# 1.2 10-Jul-2005 grange

Spacing cleanup.


# 1.1 02-Jun-2005 tholo

Start on a basic ACPI framework -- does not do much more than read out the
ACPI tables into kernel memory and attach ACPI and HPET timers currently.

In order to test this code, enabling the devices in GENERIC as well as
the ACPI_ENABLE option is needed. This code does not do any thermal
control yet, so this should be done with care depending on the platform.

In the tree so more people can contribute to making this more fully
featured.

Ok niklas@ grange@ tedu@


# 1.339 10-Feb-2018 deraadt

Syncronize filesystems to disk when suspending. Each mountpoint's vnodes
are pushed to disk. Dangling vnodes (unlinked files still in use) and
vnodes undergoing change by long-running syscalls are identified -- and
such filesystems are marked dirty on-disk while we are suspended (in case
power is lost, a fsck will be required). Filesystems without dangling or
busy vnodes are marked clean, resulting in faster boots following
"battery died" circumstances.
Tested by numerous developers, thanks for the feedback.


# 1.338 08-Feb-2018 deraadt

After flushing disks, try again to release all memory. We don't need to
save dirty memory to the hibernate space.


# 1.337 08-Feb-2018 deraadt

Toss all releaseable memory, because fragmentation can get in the way
of allocating the hibernate playpen.


# 1.336 08-Feb-2018 deraadt

sr_quiesce() is a new approach for ensuring that softraid drains
output to the disks.

This is part of a larger suspend/resume filesystem-safety diff, which
has been worked on for a couple of months already. Tests by job, krw,
beck, benno, and others. Sometimes even by snapshot users...


# 1.335 29-Nov-2017 kettenis

Add support for GPIO-signaled events.

ok mlarkin@


# 1.334 16-Nov-2017 jcs

Add PCI attachment for dwiic(4) needed by Intel 100 Series machines

ok kettenis


# 1.333 14-Nov-2017 kettenis

Add a few more PNP IDs to the skip lists.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.332 17-Aug-2017 stsp

Make dwiic(4) attach its ACPI parent dependencies.
This change makes the Asus E200HA keyboard work.

Original analysis and patch by Cesare Gargano
ok kettenis@


# 1.331 09-Aug-2017 dcoppa

Add "PNP0303" (8042 PS/2 Controller) to acpi_isa_hids[]

ok kettenis@


# 1.330 22-Jul-2017 jcs

acpibat: look for _BIX first before falling back on _BIF

ACPI 4.0 deprecated _BIF for battery status so some newer machines
are exporting _BIX which extends _BIF's package with a few extra
fields. When using _BIX, export the new discharge cycle count as a
sensor.

feedback from kettenis and anton
ok deraadt


# 1.329 20-Jul-2017 kettenis

Start a list of isa(4) device names such that we can avoid printing them
as "not configured".

ok mikeb@, millert@, deraadt@


# 1.328 08-Apr-2017 deraadt

Sensors are run as callbacks inside tasks. During suspend a sensor
could be running inside a driver that will be force-detached, or due
to tsleep end up disrupting the softstate/hardstate contract. At
suspend time, quisce all these callbacks by waiting for completion.
This issue has never been observed for real, but may be implicated in
suspend/resume failures.
ok kettenis guenther mlarkin


# 1.327 08-Apr-2017 deraadt

Two prototypes for acpi_maptable; merge to one place


Revision tags: OPENBSD_6_1_BASE
# 1.326 27-Mar-2017 deraadt

Now that hibernate_alloc() only has clean success/failure, don't
need to call hibernate_free() to clean up a partial mess.
ok mlarkin kettenis


# 1.325 13-Mar-2017 deraadt

allow ramdisks to build; ok jcs


# 1.324 12-Mar-2017 jcs

try to attach acpisbs first and if successful, don't attach acpibat


# 1.323 02-Mar-2017 natano

Add a new sysctl machdep.lidaction. The sysctl works as follows:

machdep.lidaction=0 # do nothing
machdep.lidaction=1 # suspend
machdep.lidaction=2 # hibernate

lidsuspend is just an alias for lidaction, so if you change one, the
other one will have the same value. The plan is to remove
machdep.lidsuspend eventually when people have upgraded their
/ets/sysctl.conf.

discussed with deraadt, who came up with the new MIB name
no objections mlarkin
ok stsp halex jcs


# 1.322 28-Feb-2017 natano

Untangle abstract sleep modes from ACPI sleep states. This paves the way
for further work in the area.

idea and ok deraadt
ok mlarkin


# 1.321 22-Feb-2017 jcs

add acpisbs, an acpi smart battery subsystem driver reading data
over smbus

currently disabled because it conflicts with acpibat

ok deraadt, kettenis


# 1.320 14-Jan-2017 kettenis

Revert aml_rdpciaddr changes; breaks several machines that were working
before.


# 1.319 11-Jan-2017 jsg

Increase the size of the buffers used for _HID and _CID strings to be
able to fit "Hyper_V_Gen_Counter_V1".

ok mlarkin@ deraadt@


# 1.318 08-Jan-2017 kettenis

Make aml_rdpciaddr() work for for devices behind bridges. The code now
uses the mapping between AML nodes and PCI devices that we establish.
Because _INIT methods may end up calling aml_rdpciaddr(), make sure we
create that mapping early. Also handle devices that aren't actually present.
These devices are now included in the mapping and reads will return an
all-ones patterm whereas writes are a no-op..

ok mlarkin@


# 1.317 25-Oct-2016 pirofti

Add the resource index to aml_parse_resource() callback function.

This is needed by an upcoming acpiec commit that handles machines breaking
the current ACPI specifications.

Change suggested by and ok kettenis@, guenther@


# 1.316 18-Sep-2016 guenther

If the APIC table (MADT) isn't present then ci_acpi_proc_id won't be set
when acpicpu tries to attach, leading to a NULL dereference. The ACPI
processor object on the Dells where this was reported is bogus and useless
anyway so apply the same test when deciding whether to attach to processor
objects as acpicpu would when matching them to cpus.

Problem reported by Eivind Eide (xenofil (at) gmail.com) and
Johan Huldtgren (johan+openbsd-bugs (at) huldtgren.com).
Testing by Eivind Eide.
ok kettenis@ mlarkin@


# 1.315 03-Sep-2016 naddy

Write the system time back to the RTC every 30 minutes.
This fixes the problem that long-running machines which were not
shut down properly would reboot with a badly offset system time.

hints and ok kettenis@


# 1.314 31-Aug-2016 mlarkin

Don't put a device in the wake device list if _STA says it's not there.

Fixes at least one machine that woke up from sleep / poweroff immediately.

ok kettenis@, tested by kettenis and martijn@


# 1.313 28-Jul-2016 kettenis

Store the acpi processor ID/UID in struct cpu_info, and use it to attach
acpicpu(4) drivers to the right cpu(4).

ok mlarkin@, guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.312 10-Jun-2016 kettenis

The power management timer has been made optional in ACPI 5.0A, so don't
attempt to attach acpitimer(4) if the timer isn't present.

Pointed out by jcs@

ok jcs@


# 1.311 08-May-2016 kettenis

Make sure devices listed by a device's _DEP method are attached before
we attach the device itself. The _DEP methode, introduced in ACPI 5.0,
is a hint that the device in question depends on OpRegion support from
the devices listed by _DEP, so we have to order them the proper way.
To prevent us from attaching those devices again when we encounter them
later walking down the device tree, keep track of our attempts to
attach a device driver to them using aflag in the aml_node structure.

ok guenther@


# 1.310 25-Apr-2016 pirofti

We attach acpihpet(4) and acpiec(4) without using the HID, so do not
report it in the dmesg.

OK deraadt@, mlarkin@.


# 1.309 02-Apr-2016 kettenis

Reduce dmesg spam by knocking out some acpi hardware IDs that don't need a
driver.

ok mpi@


# 1.308 29-Mar-2016 kettenis

Check if a device is present (using _STA) in acpi_foundhid. This prevents
us from reporting devices as "not configured" that aren't actually present,
and allows us to remove duplicated code from several drivers.

ok deraadt@, ok jsg@


# 1.307 29-Mar-2016 kettenis

Implement a cleaner way to prevent acpivout(4) from attaching if we have
a platform-specific method of controlling the backlight.

ok millert@, mlarkin@, jung@


# 1.306 28-Mar-2016 kettenis

Adding each and every new acpi device driver to acpi_foundhid() is
gettinga bit of the burden. And it really isn't how our autoconf
framework is supposed to work for busses that can be enumerated. All
the drivers already check for a matching _HID in their attach
function. So we can just drop the checks in acpi_foundhid() and let
the drivers sort themselves out.

Print a "not configured" line for devices for which we don't attach adriver.
This may be a bit much as there are devices for which we will not have a
driver. This will be removed if it causes too much dmesg spam.

ok deraadt@, mlarkin@


Revision tags: OPENBSD_5_9_BASE
# 1.305 17-Jan-2016 jsg

Take acpi_foundhid() outside of #ifndef SMALL_KERNEL so the ramdisk
kernel can find and attach dwiic(4) and sdhc(4).


# 1.304 17-Jan-2016 jsg

remove an unused global variable


# 1.303 14-Jan-2016 kettenis

Attach dwiic(4) on the Intel Bay Trail i2c controllers as well.


# 1.302 12-Jan-2016 kettenis

Improve emulation of PM registers on Hardware-redcued ACPI by emulating
SPL_TYPx, SLP_EN and WAK_STS using the new SLEEP_STATUS_REG and
SLEEP_CONTROL_REG registers. Makes the transition into S5 (powerdown) work
on the ASUS X205TA.

ok deraadt@, mlarkin@


# 1.301 12-Jan-2016 jcs

Add dwiic, a driver for the Synopsys DesignWare i2c controller found
on the Samsung ATIV Book 9 laptop. This initial version only
supports ACPI config/attachment.

Add ihidev, a HID-over-i2c driver largely based on uhidev. dwiic
handles attaching ihidev devices found in ACPI.

Add ims, a HID-over-i2c mouse/trackpad driver to get basic cursor
and button functionality from HID-compliant i2c trackpads.

ok kettenis deraadt


# 1.300 11-Jan-2016 kettenis

Make sdhc(4) attach to hardware IDs "80860F14" and "PNP0FFF".


# 1.299 09-Jan-2016 kettenis

Initial support for Hardware-Reduced ACPI. This skips the legacy mode checks
by emulating the PM_CNT register such that SCI_EN is always enabled. It also
skip attaching acpitimer(4) since the relevant registers are no longer there.

ok deraadt@, mlarkin@


# 1.298 06-Jan-2016 kettenis

Follow the spec and check the SCI_EN bit to see if ACPI has already
been enabled and don't bother enabling it again if it is. This should fix
systems that don't implement legacy mode and therefore set ACPI_ENABLE and
ACPI_DISABLE to zero.

ok mlarkin@, yasuoka@


# 1.297 23-Nov-2015 reyk

Back out the CRITICAL change, turn it back from 10% to 25%. Some
people with old/broken batteries didn't like this change, and the
underlying heuristics are wrong.


# 1.296 22-Nov-2015 reyk

The "CRITICAL" APM battery level, that is exported/faked by the ACPI
stack, is currently set to 25%. With 25% battery left, a typical
modern laptop will have at least one hour runtime left. It is an
arbitrary value, and other APM power layers in the kernel set CRITICAL
to something like 5% or 10%, so set it to 10% for now.

OK mlarkin@


# 1.295 28-Sep-2015 deraadt

In low-level suspend routines, set cold=2. In tsleep(), use this to
spit out a ddb trace to console. This should allow us to find suspend
or resume routines which break the rules. It depends on the console
output function being non-sleeping.... but that's another codepath which
should try to be safe when cold is set.
ok kettenis


# 1.294 06-Sep-2015 deraadt

clarify malloc arguments (base + length...)


# 1.293 28-Aug-2015 deraadt

fairly simple sizes for free(); ok tedu


# 1.292 20-Aug-2015 kettenis

Respect the access size when reading or writing to pci config space.
This version also makes sure writes are properly aligned, preventing
the panics seen with the previous incarnation of this change.

Fixes battery status passthrough in vmware, brightness keys on my x220
and who knows what else.

ok mlarkin@, deraadt@


Revision tags: OPENBSD_5_8_BASE
# 1.291 04-Aug-2015 deraadt

If we are setting ACPI_PM1_BM_RLD do it only once, and remember to
repeat this action at resume time.
ok mlarkin guenther


# 1.290 29-Jul-2015 deraadt

Something subtle in previous revision is locking up suspend or booting
on some laptops. Mailing lists threads mention x220 and Asus machines.
Reverting for now.


# 1.289 21-Jul-2015 kettenis

iRespect the access size when reading or writing to pci config space.
Fixes battery status passthrough in vmware and who knows what else.

Tested by many.

ok mlarkin@, deraadt@


# 1.288 17-Jul-2015 mlarkin

fix comments


# 1.287 26-Mar-2015 jsg

Use the 0x10 extended capability to determine if the trackstick
buttons are wired to the trackpad and need to be re-routed to the
trackstick. Without this change the buttons on 2015 thinkpads
get picked up as extended buttons that show up as scroll up/down.

Remove the X1 Carbon 2015 (LEN0048) and X250 (LEN0046) from the
top button area/soft buttons quirks list. Also avoid using the
quirk list entirely if the capability bit is set.

Based on work from jcs@ with feedback from mpi@
ok mpi@ jcs@


# 1.286 20-Mar-2015 kettenis

The ACPI standard says that a PCI function number of FFFF can be used to refer
to all functions on a device. This makes no sense in the context of accessing
PCI config space. Nevertheless stsp@ has a machine with AML that tries to
do config space reads from a device where _ADR encodes function number FFFF.
This is almost certainly buggy AML, but since Linux (and presumably Windows)
boot just fine on this machine we need to work around this. Linux effectively
treats function FFFF as function 7, which isn't present on this machine.
Instead we simulate a read from a non-existent device by returning a bit
pattern of all ones. Writes to function FFFF will still panic.

ok mlarkin@


# 1.285 16-Mar-2015 deraadt

Reduce use of SMALL_KERNEL in acpi, bringing acpiec driver and more gpe
handling into RAMDISK. This is now possible because the install media
has ample room. The goal is to reduce special cases where we may be
skipping (unknown) important operations...
ok mlarkin kettenis


# 1.284 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.283 07-Feb-2015 deraadt

New framework that allows hibernate to pass in entropy from it's fresh
boot.
ok mlarkin


# 1.282 06-Feb-2015 mlarkin

Fix a hibernate crash on some machines due to unmapping a page that
may not have been mapped previously (in the failure to hibernate case).

Also ensure that the lowmem ptp is mapped in all cases (not just MP).

ok kettenis


# 1.281 17-Jan-2015 deraadt

resume_randomness() before spinning up other cpus...


# 1.280 15-Jan-2015 jsg

Define a new wscons mouse type for Synaptics clickpad devices that lack
physical buttons. This will be used if the acpi pnpid for the mouse
matches a list provided by Synaptics found in Linux.

Combined with the xenocara changes this will remove the need for an xorg
config file for the x240/t440/t540 generation of thinkpads.

ok matthieu@ shadchin@ kettenis@


# 1.279 11-Jan-2015 kettenis

Tolerate checksum errors in ACPI tables (but complain about them). Apparently
there are machines out there with broken BIOSen with such checksum errors in
thier RSDT tables. Windows runs fine on these machines, so it probably does
not check. Linux ignores checksum errors as well (and prints a warning).

ok deraadt@, krw@


# 1.278 18-Dec-2014 deraadt

suspend and resume the RNG (at what looks like the right spots)
ok reyk djm


# 1.277 09-Dec-2014 doug

Replace some malloc(n*size,...) calls with mallocarray().

ok tedu@ deraadt@


# 1.276 06-Dec-2014 mpi

If "machdep.lidsuspend" is not set, do not go back to sleep if the lid
is still closed when resuming. This allows docked laptop to be resumed
without opening them.

ok mlarkin@


# 1.275 23-Nov-2014 mlarkin

If a machine has no _LIDs, then none can be open. This diff fixes a problem
where machines without any _LIDs were going back to sleep after hibernate
due to the recent suspend-if-lid-closed-on-resume diff. Noticed on qemu,
which doesn't have any _LIDs.

discussed with deraadt


# 1.274 18-Nov-2014 krw

Nuke yet more obvious #include duplications.

ok miod@


# 1.273 08-Nov-2014 mlarkin

If resuming from sleep (zzz/ZZZ) and the lid is still closed, go back to
sleep. This prevents accidental lid flex or slight opening in a backpack
from waking the machine up and leaving it resumed (powered on).

ok deraadt@


# 1.272 26-Sep-2014 kettenis

Rework piglet and pig allocation. Currently the piglet gets allocated
deep down in the suspend path, where it is really hard to recover from
allocation failure. So allocate the piglet early on in the suspend path.
Also change the piglet and piglet allocation functions to use km_alloc(9)
instead of doing pmemrange magic. This removes a bunch of code which, in the
case of the piglet allocation, is broken since it results in a NULL pointer
dereference. Also switch the piglet allocation to not wait. If we can't
allocate 16MB of phys contig memory on a halfway modern machine we're almost
certainly under a lot of memory pressure and we're better off not trying to
hibernate anyway.

ok mlarkin@


# 1.271 25-Sep-2014 kettenis

Bring back rev 1.249. Now that mp_setperf() has been fixed, it should be
safe to restore perflevel when resuming. Should fix the issues people
see with the processor running at full speed heating up the machine while
the apm command reports that the CPUs are running at a low speed.

ok dcoppa@, deraadt@


# 1.270 23-Sep-2014 kettenis

Release the acpi lock around calls to wsdisplay_suspend() and
wsdisplay_resume(). The X server may adjust the display brightness just
before/after the VT switch when suspending/resuming. In fact, it may
already be blocked adjusting the display brightness for some reason
unrelated to the suspend request.

The VT switch request will eventually time out, but the delay is annoying
and the failed VT switch will lead to problems after resume.

Needed for a future update of the xf86-video-intel driver.

ok mlarkin@


# 1.269 19-Sep-2014 kettenis

Use config_suspend_all(9).


# 1.268 08-Sep-2014 kettenis

Clear the PME Status bit on PCI devices in response to Device Wake events.
Fixes the interrupt storm on the 2009 Mac mini with WOL enabled on its
nfe(4) interface.

ok mlarkin@


Revision tags: OPENBSD_5_6_BASE
# 1.267 20-Jul-2014 mlarkin

Support hibernating to softraid crypto volumes.

much help and ok from deraadt@


# 1.266 16-Jul-2014 mlarkin

Reenable hibernate RLE support and flush+zero all memory after unpack.


# 1.265 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.264 12-Jul-2014 kettenis

Add a function to drop all clean pages on the page daemon queues and call
it when we hibernate.

ok mlarkin@, miod@, deraadt@


# 1.263 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.262 11-Jul-2014 mlarkin

Flush the buffercache to 16MB on hibernate and restore its previous max
size (kern.bufcachepercent) on resume, for better hibernate performance.

ok beck@


# 1.261 10-Jul-2014 blambert

remove unneeded header

ok mlarkin@


# 1.260 01-Jul-2014 kettenis

Adding an additonal delay after each access to SystemMemory or SystemIO
during autoconf makes no sense.

ok deraadt@


# 1.259 23-Jun-2014 kettenis

SystemMemory is used to access memory mapped registers on some machines, so we
must use the bus_space(9) API to access it instead of memcpy(9). Also make
sure we properly align access to fields that need more than byte alignment.

ok guenther@


# 1.258 21-May-2014 mlarkin

Fix invalid / missing format specifiers in several dnprintf statements
(used only when ACPI_DEBUG is enabled)

From a diff on tech@ by Fabian Raetz, with a minor change by me, tested
on i386 and amd64 with ACPI_DEBUG enabled and disabled.


# 1.257 25-Apr-2014 mlarkin

Defer the free of the hibernate structures until later, when it is safe to
do so.

Freeing the hibernate structures too early leads to inadvertent calls into
splx when we aren't ready for them yet. Found when trapping splx calls in
the resume path.

discussed with deraadt@ at m2k14


# 1.256 13-Apr-2014 mlarkin

Fix a badly worded debug message that implied the entire machine was going
to sleep, instead of just the acpi thread going to sleep.


# 1.255 31-Mar-2014 mpi

Change the order of operations during a suspend/resume cycle and
call bufq_quiesce() after executing the DVACT_QUIESCE handlers.

This should be safe since no disk nor controller drivers have such
handler but it will allow us to detach sd(4) devices attached to a
USB bus.

Another benefit pointed out by kettenis@ is that drivers that need
to read a firmware from the disk should be able to do it at resume
time in a DVACT_WAKEUP handler.

ok kettenis@, deraadt@


# 1.254 13-Mar-2014 dlg

get rid of the assumption that the head of the alldevs list is the
"mainbus" device. this breaks when mpath is enabled because it
attaches before mainbus and therefore takes the head position.

have autoconf provide device_mainbus() which looks up mainbus_cd,
and use that instead.

discussed with deraadt who just wants mpath stuff to move forward
despite there being many ways to shine this particular turd.


Revision tags: OPENBSD_5_5_BASE
# 1.253 21-Feb-2014 deraadt

Do the lid control further on in the resume sequence, since this is
AML, and will do WAITOK operations. While cold, that is a bad idea.
We seem to be safe with respect to the spec, I do not see any event
debounce issues here.
ok kettenis, tested by many


# 1.252 20-Jan-2014 claudio

Instead of checking sc->sc_revision <= 1 check that the
fadt->hdr_revision <= 1. This is a better check since it checks
the version of the proper descriptor. This allows the alix APU
to reboot since those boards have currently a wrong sc->sc_revision.
Committing this now so that it can be tested easier.
With and OK jsg@ also fine by derradt@


# 1.251 24-Dec-2013 kettenis

If the FADT has its SMI_CMD set to zero, assume we're only ACPI-only hardware
and don't need to disable SMI ownership of the ACPI hardware registers.

ok mlarkin@


# 1.250 23-Dec-2013 kettenis

Revert previous commit. It makes resume hang on some MP systems running
GENERIC.MP.


# 1.249 16-Dec-2013 kettenis

Restore hw.setperf upon resume like we do for apm(4) on i386. Seems we need
to do this fairly late in the resume to avoid hangs.

ok deraadt@, mlarkin@


# 1.248 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


# 1.247 06-Nov-2013 mpi

Add support for Power Resources for Dx states and the necessary hook
for PCI devices. This hook should be called twice, before and after
changing the power state of a PCI device.

Before setting the device to the new state, the ACPI layer will notify
every power resources linked to the device for that state and make sure
they are turned "_ON". After changing the state of the device, it will
decrement the reference of every power resources linked to that device
for the old state and turn them "_OFF" if they are no longer referenced.

This fixes the no-USB after resume problem seen on various ThinkPad,
problem initialy diagnosed with Alexander Polakov.

ok kettenis@, deraadt@


Revision tags: OPENBSD_5_4_BASE
# 1.246 01-Jun-2013 mlarkin

Stop printing ACPI wakeup devices in dmesg after the 16th wakeup device.

Helps with VMware VMs that report hundreds of wakeup devices.

ok reyk@, deraadt@


# 1.245 31-May-2013 bluhm

Fix the build for a kernel without wd and pciide in its config.
OK deraadt@ tedu@


# 1.244 30-May-2013 deraadt

Enforce ca_activate tree-walks over the entire heirarchy for all events,
cleaning up some shutdown-hook related code on the way.
(A few drivers related to sparc are still skipped at kettenis' request)
ok kettenis mlarkin, tested by many others too


# 1.243 18-Apr-2013 deraadt

pin the acpi thread to the BSP, because AML and SMI cannot be trusted. I
was certain we were doing this already, but it turns out no...


Revision tags: OPENBSD_5_3_BASE
# 1.242 24-Dec-2012 guenther

In acpiioctl(), Return though the splx() path if suser() returns an error

ok deraadt@


# 1.241 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


# 1.240 04-Oct-2012 ehrhardt

Use information provided by ACPI to attach secondary PCI host bridges.

ok kettenis@


# 1.239 07-Sep-2012 kettenis

Add a new function called acpi_pci_min_powerstate() that returns the lowest
power state for a PCI device for the power state (currently S3 or S4) we're
transitioning into. This function is probably too simplistic; there is a lot
of (silly) complexity in the ACPI spec.

ok deraadt@


Revision tags: OPENBSD_5_2_BASE
# 1.238 13-Jul-2012 pirofti

Disable acpivideo(4) on ASUS laptops.

There are some models that panic whenever an event is received.
Some cope, some don't. In order to get this right acpiec(4) requires
major rework. Until then let machines boot.

Discussed with deraadt@.


# 1.237 11-Jul-2012 mlarkin

for now, restrict hibernate to root in the acpi ioctl kernel interface.
normal rules for controlling this interface through apmd still apply.

ok deraadt@


# 1.236 09-Jul-2012 deraadt

tiny little oops in untested hibernate block


# 1.235 09-Jul-2012 deraadt

clean up ioctl() case statements


# 1.234 09-Jul-2012 deraadt

acpi_disable_onegpe() is not used; ok pirofti


# 1.233 24-May-2012 kettenis

If _MIN > _MAX consider a WordBus resource to be invalid. Fixes a panic on
the IBM x3550-M4.

ok mikeb@


# 1.232 29-Mar-2012 mlarkin

Missing #ifdef in previous diff


# 1.231 29-Mar-2012 mlarkin

Prevent hibernation attempts on machines with unsupported disk controllers.
The same check is also performed later in the hibernate sequence, but by
then it's too late to cleanly unwind (presently).

ok deraadt


# 1.230 26-Mar-2012 deraadt

simplify conditionals in acpi_sleep_state()
ok mlarkin


# 1.229 26-Mar-2012 deraadt

hook in the hibernate request code; half of this diff is from mlarkin
ok mlarkin


Revision tags: OPENBSD_5_1_BASE
# 1.228 20-Sep-2011 deraadt

Very early in suspend while the clock is still ticking realtime,
save the clock back to the rtc. The zaurus already did this.
ok phessler miod kettenis


Revision tags: OPENBSD_5_0_BASE
# 1.227 02-Jul-2011 nicm

kqueue attach functions should return an errno or 0, not a plain 1. Fix
the obvious cases to return EINVAL and ENXIO.

ok tedu deraadt


# 1.226 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.225 16-Jun-2011 pirofti

Import acpitoshiba driver.

This is originally written by Hiroyuki Aizu, ported and integrated
in our tree by Javier Vazquez <javama4 AT gmail> with tweaks and
suggestions by me.

Import prodded by deraadt@ and the lack of testers on tech@.


# 1.224 27-Apr-2011 jcs

attach acpithinkpad to newer lenovo models like the x120e

ok deraadt@


# 1.223 22-Apr-2011 jordan

Fix uninitialzied variables and formatting strings (-Wxxx errors)


Revision tags: OPENBSD_4_9_BASE
# 1.222 02-Jan-2011 jordan

Re-add code for ACPI taskqueue, eliminates need for state vars in acpi softc
Serialize processing ACPI tasks for sleep/powerdown/GPE event
ok deraadt@


# 1.221 31-Oct-2010 guenther

Revert last commit: it breaks resume on Thinkpads
"then please back it out" deraadt@


# 1.220 26-Oct-2010 jordan

Add task queue for ACPI gpe and notify handlers
This fixes eject on Dell Latitude dock and an issue on the Dell Mini
battery update.
ok deraadt


# 1.219 07-Oct-2010 jordan

Create separate functions for enabling wake and runtime gpes.
Remove old suspend/resume gpewalk function
ok deraadt, mlarkin


# 1.218 05-Oct-2010 jordan

split acpi_enable_onegpe to separate enable/disable functions
ok deraadt


# 1.217 05-Oct-2010 jordan

Added acpi_disable_allgpes function to clear GPE status
ok deraadt


# 1.216 05-Oct-2010 deraadt

clean up acpi_softc use more


# 1.215 05-Oct-2010 deraadt

delete a bit of #if 0 code


# 1.214 05-Oct-2010 deraadt

hide use of our global acpi_softc pointer a bit better


# 1.213 29-Sep-2010 deraadt

grab the acpi thread lock very early in acpi thread startup; not that it
is likely that anyone will race us... but why be careless.
ok kettenis


# 1.212 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.211 29-Aug-2010 jasper

- add a check for FWRITE here too.

ok miod@


# 1.210 27-Aug-2010 kettenis

Make PCI Power Management optional, and only enable it when acpi(4) attaches.

ok deraadt@


Revision tags: OPENBSD_4_8_BASE
# 1.209 08-Aug-2010 kettenis

Close race between the acpi thread and wscons ioctls running in process
context. We might rip this out post-release and replace it with a solution
that serializes things in a more obvious way.

ok deraadt@, miod@ and "those crazy texans who ok anything"


# 1.208 08-Aug-2010 deraadt

Report the acpi sleep states when on a RAMDISK; ok krw


# 1.207 07-Aug-2010 kettenis

Enforce acpi register access rescrictions.

ok mlarkin@, deraadt@, tested by marco@


# 1.206 07-Aug-2010 deraadt

upon resume, notify all the acpiac and acpibat drivers to update
their status. some laptops do not do this automatically.
ok kettenis canacar


# 1.205 06-Aug-2010 marco

Always call _PSW on _LID devices that support it to enable lid open to
resume the machine. Conversely disable it when the machine wakes up.

Tested by several ok deraadt


# 1.204 06-Aug-2010 deraadt

As part of a revamp of the PM1/GPE code, I write this diff to look for
unmanaged EN & STS bits in the PM1 register at interrupt time and report
them. As a side effect this splits the STS acknowledgement into two writes
(for power, and sleep) instead of one. The printf that is added (to spot
unmanaged STS bits) has not yet been triggered as far as we know.

Before the "write to PM1 registers at the right offset" diff went in, this
was not neccessary. But newer thinkpads do not have a working soft power
button without this diff.

We have no idea why.
ok mlarkin kettenis


# 1.203 05-Aug-2010 deraadt

PM1 power button events were generating 'fake events' to acpibtn PWRB,
but if there is no such device these were being tossed. Instead, arrange
for all power button handlers to flag a softc variable which is processed
by the thread; so that one place will know to request a powerdown (psignal
to init)
ok marco mlarkin


# 1.202 05-Aug-2010 deraadt

rename a variable which is annoyingly poorly named


# 1.201 05-Aug-2010 mlarkin

Write to the correct offset for various ACPI pmreg registers. Writing to
the correct register does properly clear the wake status bits and fixed
event bits, which allows some machines to have more than 1 suspend cycle.

bug spotted by deraadt@

tested by deraadt@ and myself on a half-dozen machines and others on
hackers@

ok deraadt@, marco@


# 1.200 05-Aug-2010 deraadt

Fix the power button. The pm1 register is 16 bits in size.
My mistake, a while back.
tested by krw


# 1.199 04-Aug-2010 kettenis

Print supported sleep states.

ok marco@, phessler@, deraadt@


# 1.198 03-Aug-2010 mlarkin

Use the proper flag for re-enabling certain hardware events (power btn,
sleep btn). The (incorrect) flag prevoiously used coincidentally had the
same value, so this shouldn't cause any different behavior than before.

tested on a variety of machines (i386, amd64, sp, mp) - no behavioral
change seen

ok deraadt@


# 1.197 28-Jul-2010 marco

Make all gasio failures look and behave the same.


# 1.196 28-Jul-2010 mlarkin

Delay a little longer on suspend before giving up. Fixes machines that
sometimes don't suspend because the sleep transition takes too long
(symptoms like failing to sleep but the backlight stays on).

ok deraadt@


# 1.195 27-Jul-2010 deraadt

remove XXX; EC can now be attached early when needed; ok jordan


# 1.194 27-Jul-2010 deraadt

more brackets feels good


# 1.193 27-Jul-2010 deraadt

XXX annotate a block in acpi_enter_sleep_state() that is pretty worrying


# 1.192 27-Jul-2010 deraadt

horrific indentation that nearly made me puke


# 1.191 27-Jul-2010 mlarkin

Set the sleeping indicator light on machines that support it, in case SMI
doesn't do it for us. Shows the 'moon' LED on older Thinkpads when
sleeping.

Tested on T43p, W500, T510 by me and on X61s by deraadt@.

ok deraadt@


# 1.190 27-Jul-2010 jordan

Early initialization of acpiec if ECDT table exists
Fixes hang when booting thinkpads while docked
ok deraadt


# 1.189 26-Jul-2010 deraadt

remove merge error which was commited


# 1.188 26-Jul-2010 pirofti

Add support for Sony ACPI hotkeys via a new driver: acpisony(4).

Currently it only works for the suspend button and tries to do right for
the brightness events, but I haven't found a Sony laptop that like to do
right so far.

In the future I want to make the brightness keys work on all Sony's and
also add support for the zoom hotkeys and whatever other funky keys I can
find on those things.

Okay deraadt@.


# 1.187 22-Jul-2010 deraadt

pretty large cleanup of SMALL_KERNEL


# 1.186 21-Jul-2010 deraadt

in the acpi GPE handler, late ack level interrupts not edge interrupts.
Duh.


# 1.185 21-Jul-2010 deraadt

Merge the edge and level gpe handlers into one (now possible since the
gpe structure tells us whether the gpe is edge or level)


# 1.184 21-Jul-2010 deraadt

delete #if 0 code we are not going to use; ok marco


# 1.183 20-Jul-2010 deraadt

make the mask code for acpi_enable_onegpe() match other similar code


# 1.182 20-Jul-2010 deraadt

Handle the acpi interrupt controller with proper edge/level handling,
wrapped in spltty to avoid racing against the interrupt controller.
Repair the gpe bit masking code while there, and do operations in the
right order
ok kettenis mlarkin, help from jordan at figuring out the order of
operations.


# 1.181 20-Jul-2010 matthew

When we're preparing to go to sleep, only call bufq_restart() if other
preparations failed.

ok kettenis@, thib@, deraadt@


# 1.180 19-Jul-2010 deraadt

permit compilation with ACPI_DEBUG


# 1.179 19-Jul-2010 deraadt

there is no need for a seperate dsdt_softc pointer; dsdt does not have a
softc but is just a part of the single acpi driver.
ok jordan


# 1.178 19-Jul-2010 deraadt

acpi_delay() is no longer needed; ok jordan


# 1.177 18-Jul-2010 mlarkin

Clear event status on resume, to avoid going back to sleep/reboot/shutdown
immediately after resume. Tested by a bunch of people on hackers@ on
various hardware. Fixes at least Toshiba NB200 and Gateway LT3103u.

ok marco


# 1.176 13-Jul-2010 deraadt

rename some thread related interfaces so they are logical


# 1.175 10-Jul-2010 jordan

Verify _ADR bdf is correct (some systems return 0xffff)
Works on Dell x9xx and Studio 1555


# 1.174 09-Jul-2010 jordan

Clear the noise


# 1.173 08-Jul-2010 jordan

Add mapping for ACPI device to PCI bdf (match autoconf tree)
Simplify resource parsing function to use buffer argument
Convert namespace linked lists to use queue macros
ok marco@, deraadt@


# 1.172 06-Jul-2010 deraadt

Use spltty() to lock downcalls from apm against the information being
modified by the acpi thread.
ok kettenis


# 1.171 06-Jul-2010 deraadt

Don't use a workq to do the suspend, because it races aginst the acpi
thread. Instead, just tell the acpi thread to do the suspend for us.
This makes apmd & zzz work correctly. While here, have acpithinkpad
attempt to post the event to apm before prompting the suspend itself.
ok kettenis marco mlarkin


# 1.170 05-Jul-2010 mlarkin

Reset register SCI_EN on resume. The ACPI spec clearly states we don't have
to do this, but many machines don't, or forget to, which leads to all sorts
of problems on resume (broken battery stats, overheating, failure to resume
more than once, etc).

tested by many, ok marco@, deraadt@


# 1.169 01-Jul-2010 thib

do bufq quiesce again, since vnd/the bufq_done call in
biodone has been fixed.


# 1.168 01-Jul-2010 jordan

Backout recent AML changes, breaks on R210 and others


# 1.167 01-Jul-2010 jordan

Simplified aml_parse_resource call, cleanup code
ok marco


# 1.166 01-Jul-2010 jordan

Fix subordinate bus # for multi-root PCI buses
ok marco, kettenis


# 1.165 30-Jun-2010 thib

Disable/partially backout the bufq quiesce changes as this
is causing havoc with vnds and release must be buildable.


# 1.164 29-Jun-2010 deraadt

Fix ramdisks (new code was placed inside a SMALL_KERNEL chunk)


# 1.163 29-Jun-2010 jordan

Add support for mapping ACPI to PCI devices
ok kettenis, deraadt


# 1.162 29-Jun-2010 kettenis

Call bufq_quiesce() on suspend to stop all I/O and bufq_restart on resume.
Makes suspend/resume work much better while doing disk I/O.

ok mlarkin@


# 1.161 29-Jun-2010 tedu

60 is a more reasonable guess for number of minutes in an hour than 100.
spotted by TeXitoi. ok deraadt


# 1.160 27-Jun-2010 jordan

Add _acpi_matchhids routine, precursor to pci device match


# 1.159 27-Jun-2010 mlarkin

Clear ACPI fixed event status on resume (power buttons/etc) so that some
machines don't immediately resume after suspending to S3.

Tested by kettenis@ and I on 5 or 6 machines, deraadt@ "yeah, looks good"


# 1.158 27-Jun-2010 jordan

Cleaned up acpi_maptable, adds to table list directly


# 1.157 07-Apr-2010 deraadt

If we have done a wsdisplay_suspend(), and ACPI decides it does not want to
suspend, we must roll back by calling wsdisplay_resume() or the virtual
console switching will be locked forever
ok oga


# 1.156 07-Apr-2010 kettenis

Make suspend/resume work on MP machines (running an MP kernel). Joint work
from mlarkin@ and me, with some amd64 fixes thrown in by deraadt@

ok marco@, deraadt@, pirofti@, mlarkin@


# 1.155 31-Mar-2010 kettenis

Bring /dev/apm support in line with apm(4). Make the suspend button send
a suspend request event to apmd(8) instead of suspending immediately. Also
keep track of whether /dev/apm and /dev/apmctl are currently open, such that
we can still suspend immediately if apmd(8) isn't running.

ok deraadt@, marco@, pirofti@, jsing@, oga@


# 1.154 30-Mar-2010 oga

Prevent the apmd/x races for good.

When we hit suspend time, go through all wsdisplays on the system. if
they are in mode MAPPED, but not MODE_DUMBFB then if possible do a full
vt switch to a !mapped vt, and prevent switching back until resume time.
This has to be called from MD code because this involves userland
running so that X can run the vt switch signal handler. This way, any
case where we are using the "poke registers from userland" model, we
will not be on the hardware when we go down, so the kernel can actually
handle thing properly.

Tested on several acpi laptops (by kettenis@ and ian@), x40 (me and
beck@ at LEAST) and zaurus (me). Maybe others, but if so I forgot who at
this time..

Idea from deraadt somewhere over the Faroe Islands (I thought of a
similar thing myself a while ago). Much prompting from him. Ok and
comments miod@


# 1.153 25-Mar-2010 oga

Fix & vs. && in the kqfilter code.

Based on a comparison with the apm code.

ok deraadt@, kettenis@


Revision tags: OPENBSD_4_7_BASE
# 1.152 26-Nov-2009 mlarkin

Visual (and on some machines, audio) feedback while the machine is
resuming via calls to aml _SST method. This also makes the "moon" LED on
thinkpads to blink during resume and cease when resume is completed.

ok pirofti@


# 1.151 26-Nov-2009 deraadt

Toggle into "cold" when we we disable interrupts, because tsleeping
in the AML interpreter and context switching into userland processes
tends to sometimes pop the ACPI trampoline page out of our address
space. Makes lots more machines work.
ok kettenis mlarkin


# 1.150 24-Nov-2009 jsg

in the resume path evaluate _WAK before dealing with device drivers
like the spec says, not after.

ok mlarkin@


# 1.149 24-Nov-2009 deraadt

abort suspends on SMP systems until that gets written; ok mlarkin


# 1.148 23-Nov-2009 mlarkin

pci should return the result of it's children's suspend/resume calls back
to its parent.

handle suspend failure case and unwind if devices have an activate function
that returns failure.

ok deraadt@, kettenis@


# 1.147 23-Nov-2009 mlarkin

acpi* devices need to call their children on suspend/resume.
ok deraadt@


# 1.146 23-Nov-2009 pirofti

Remove ACPI_SLEEP_ENABLED checks.

This enables by default the suspend/resume paths in the kernel.

Okay deraadt@.


# 1.145 23-Nov-2009 deraadt

knf and spacing that are super annoying


# 1.144 23-Nov-2009 mlarkin

Hook up Theo's new children suspend/resume function, as well as more ACPI
suspend/resume glue.

ok deraadt@


# 1.143 26-Oct-2009 deraadt

Do not do strncmp comparisons on dv_xname because longer device names which
look similar could arrive in the future. Instead, compare directly against
dv_cfdata->cf_driver->cd_name
Issue originally spotted by miod


# 1.142 26-Oct-2009 jordan

Verify checksum+address when loading ACPI tables.
Some systems had invalid entries in RSDT/XSDT.
ok marco@


# 1.141 23-Jul-2009 cnst

New aibs(4) driver for ASUSTeK AI Booster (ACPI ATK0110) hardware monitoring,
with sensor state support through limits provided by the ACPI.

Tested on several ASUS motherboards kindly networked by Sam Fourman Jr.

ok deraadt marco jordan


Revision tags: OPENBSD_4_6_BASE
# 1.140 03-Jun-2009 pirofti

Import acpi power resource driver. This is needed for suspend/resume logic.

Okay jordan@ and marco@.


# 1.139 03-Jun-2009 pirofti

Add acpivideo support. This adds brightness support for all laptops
except thinkpads, they will use the acpithinkpad driver. The driver is
also hooked into wsconsole. So brightness can be adjusted via:

$ wsconsctl display.brightness=<percentage>

This is very helpfull on some laptops that have a nasty bios and get two
steps instead of one when pressing the brightness button.

Tested on various dell, fujitsu, acer, samsung and other laptops.

Okay marco@, miod@. Suggestions from kettenis@.
Lots of reviews and help from miod@, thanks!


# 1.138 03-Jun-2009 jordan

Added ejectable bay for IDE cdroms
ok marco@


# 1.137 30-Apr-2009 marco

Oga changed apm ioctls and forgot to grep. Temporary work around by hitting
the sleep button when we get any request. This interface will chnage at
some point.
ok oga


# 1.136 19-Apr-2009 krw

Call acpi_sleep_walk() when powering down just like when entering
any other sleep state. This tells acpi wake devices to not wake up
the box until manual powering up resets their status. Makes my Dell
GX520 stay powered off in response to 'halt -p'.

In snaps for a while.

ok marco@ (several times) deraadt@


# 1.135 17-Apr-2009 pirofti

Add device ranking support for future sleep/wake developments.

okay marco@


# 1.134 11-Mar-2009 jordan

Cleanup.. use aml_evalinteger instead of aml_evalname
ok marco@


# 1.133 10-Mar-2009 jordan

Fix cut/paste error.. call _BFS not _PTS
ok marco@


Revision tags: OPENBSD_4_5_BASE
# 1.132 21-Feb-2009 marco

overzealous cut/paste; found by Vladimir Kirillov <proger@uaoug.org.ua>


# 1.131 20-Feb-2009 marco

Oops didn't mean for Debugger() to remain there.


# 1.130 19-Feb-2009 marco

suspend/resume bits so that we can develop this in tree. This is disabled.
code from mlarkin and me
help from art,toby,jordan and several others
ok jordan, go for it deraadt


# 1.129 10-Feb-2009 jordan

Fix null pointer for shutdown if acpi not enabled
ok marco@


# 1.128 04-Feb-2009 kettenis

Some systems that claim to be ACPI 2.0 apparently don't provide an XSDT
address. Make the code fall back on the RSDT on those systems.

From Joerg Niendorf <p3y80@internode.on.net>. Fixes PR 5986.


# 1.127 17-Jan-2009 krw

A couple of stray malloc/memset -> malloc(M_ZERO), a bit of knf and
lose a couple of unnecessary casts. No functional change.

Feedback from kettenis@

ok kettenis@ marco@


# 1.126 06-Nov-2008 marco

Collapse all sanity checks of hids into one function.

Originaly from jordan


Revision tags: OPENBSD_4_4_BASE
# 1.125 02-Jul-2008 fgsch

add acpivideo. for now almost an empty skel so others can work on
suspend/resume. eventually it will also manage output switching and
brightness where supported. prodded by marco@


# 1.124 11-Jun-2008 marco

Fix $OpenBSD$ for once and for all


# 1.123 08-Jun-2008 deraadt

aaa_dev was being set from an uninitialized variable; ok toby


# 1.122 07-Jun-2008 marco

Kill ACPIVERBOSE. It was never used and is worthless to boot.

comments jmc
ok toby


# 1.121 06-Jun-2008 marco

Shrink acpi some more for floppies and while here re-enable acpi on the
floppies. Fixes crazy sony vaio kettenis has.

tested by deraadt
ok deraadt jordan kettenis


# 1.120 01-Jun-2008 marco

Fix \n nit in SMALL_KERNEL.


# 1.119 01-Jun-2008 marco

Re-enable new parser. Lots of testing by various folks. Fits all media
now too.

ok kettenis deraadt


# 1.118 16-May-2008 dlg

back out jordans new parser.

it didnt have enough oks from the right people, it did break machines
that were previously working, and it bloated the RAMDISKS and install
media beyond the constraints of the media.

requested by deraadt@


# 1.117 14-May-2008 jordan

Adding new ACPI Parser code
Old guts of code still exists, needs to be torn out next
ok marco@
ok brad@


# 1.116 27-Apr-2008 jcs

add acpithinkpad(4), an acpi support driver for ibm/lenovo
thinkpads.

provides functionality that the bios did on older, non-acpi
thinkpads like respond to screen brightness keys and toggle the
bluetooth device on and off.

ok deraadt@ and marco@


# 1.115 24-Apr-2008 jsing

Introduce acpiasus(4), a driver for the ACPI based hotkeys found in many
ASUS laptops (including the ASUS EeePC) - largely based on NetBSD's
asus(4) driver. On the ASUS EeePC this allows us to enable/disable
wireless, change screen brightness and use the volume keys.

ok jsg@, weingart@


# 1.114 12-Apr-2008 kettenis

Get rid of acpi_s5 global variables; simply send SIGUSR2 instead of SUGUSR1
to tell init(8) to power down the machine.

ok krw@


Revision tags: OPENBSD_4_3_BASE
# 1.113 21-Jan-2008 jsg

Check against RESET_REG_SUP in FADT before doing the acpi reset dance
after all, not doing so appears to break more machines than it fixes.

ok marco@


# 1.112 05-Jan-2008 kettenis

Never attach more acpicpu(4) devices than cpu(4) devices. Always attach
the first acpicpu(4) device on non-MULTIPROCESSOR kernels, regardless of its
ACPI processor ID.

ok mikeb@


# 1.111 05-Dec-2007 deraadt

Hang acpi(4) below bios(4) instead of mainbus(4). This lets us move acpi(4)
before pcibios(4), and then.... if acpi(4) attaches, skip pcibios(4) since
it messes the machines up. Fixes claudio's HP dl320 G5, and almost assuredly
others.
ok kettenis


# 1.110 05-Dec-2007 deraadt

repair acpiopen/acpiclose and friends to handle the /dev/apmctl interface,
so that apmd(8) can actually work correctly against acpi(4), and thus,
even do the fancy -C/-A stuff; also tested by jmc


# 1.109 02-Dec-2007 fgsch

fix the trailing space for real now.


# 1.108 02-Dec-2007 fgsch

avoid trailing spaces on output; marco@ ok.


# 1.107 27-Nov-2007 kettenis

Fix off-by-one that made us attach acpicpu0 and acpicpu1 on GENERIC kernels.

ok marco@


# 1.106 25-Nov-2007 jsg

When available and ACPI is enabled use the ACPI reset method
to reboot. This is required for some quirky sis machines
and likely others to reboot.

Tested by many. 'commit early commit often' deraadt@


# 1.105 19-Nov-2007 deraadt

sigh, amd64 still uses that stupid acpi_enabled variable...


# 1.104 19-Nov-2007 deraadt

softc's come initialized to 0, you know...


# 1.103 19-Nov-2007 deraadt

store revision; from jsg


# 1.102 19-Nov-2007 kettenis

Initialize sc_facs earlier.

ok deraadt@, beck@, canacar@


# 1.101 16-Nov-2007 deraadt

fix the bus_space #define nightmare, so that amd64 and i386 are much more
uniform. as a result shared code like acpi needs less #ifdef's
ok marco kettenis


# 1.100 15-Nov-2007 deraadt

remove the #ifdef ACPI_ENABLE. the EC codepath is either safe enough (and
will be improved), or it is unsafe because AML assumes it must be there
ok almost everyone


# 1.99 15-Nov-2007 mikeb

Make acpicpu(4) attach to the existing CPUs only.

Problem reported by form@. Fix idea by kettenis@.
Lots of help from deraadt@. Tested by deraadt@, form@ and me.

ok deraadt marco


# 1.98 14-Nov-2007 deraadt

spacing


# 1.97 12-Nov-2007 deraadt

correct : vs , in attach-time messages


# 1.96 06-Nov-2007 deraadt

all ACPI_ENABLE kernels need acpi_interrupt()


# 1.95 06-Nov-2007 deraadt

move acpi_read_pmreg() and acpi_write_pmreg() out of SMALL_KERNEL so
that acpi ramdisks can compile (obvious solution)


# 1.94 05-Nov-2007 canacar

Establish the acpi interrupt earlier. Fixes problems with some
GENERIC.MP kernels not getting acpi interrupts. This is a
workaround until the real issue with late establishment is fixed.
ok weingart@, beck@, gwk@


# 1.93 03-Nov-2007 jordan

Added support for displaying wakeup devices
ok beck@,weingart@,gwk@


# 1.92 11-Oct-2007 kettenis

When walking the tree yo find _PRT methods, check whether the device is
actually enabled and functioning. Similar to what we do for _INI.

ok weingart@, beck@


# 1.91 08-Oct-2007 krw

More simple memset(,0,) -> M_ZERO changes. In this batch move to
size(*p) as the first malloc() parameter where p is declared locally
and thus easy to check. Add M_ZERO to gpe_table allocation in acpi.c
even though there is no obvious bzero or memset nearby.


# 1.90 13-Sep-2007 mk

Remove unnecessary memset() (see aml_evalnode(9)) and a bit of KNF.

ok toby


# 1.89 13-Sep-2007 weingart

Implement ACPI 6.5.1 spec tree walk for _STA and _INI.
Fixes mk's laptop. No regressions so far (thank you to the
testers).

ok gwk@, mk@, marco@


Revision tags: OPENBSD_4_2_BASE
# 1.88 17-Apr-2007 mk

Now that acpidock no longer hangs machines during boot (at least
according to the reports I've been getting) it should be safe to enable
it again, so remove the #if 0 stuff around it.

ok marco


# 1.87 11-Apr-2007 jordan

Added changes for C-State
ok marco@


# 1.86 26-Mar-2007 marco

Calculate minutes left more accurately.

From giovanni <qgiovanni@gmail.com>


Revision tags: OPENBSD_4_1_BASE
# 1.85 03-Mar-2007 mk

Stop looking for acpidock until we fix that stuff that breaks all pre-60
series ThinkPads with acpidock(4) enabled.


# 1.84 22-Feb-2007 marco

We were getting no interrupts in .MP. There are 2 fixes; one in
acpi_machdep.c and move the interrupt establishment down after ioapic
has been initialized.

ok jordan


# 1.83 21-Feb-2007 kettenis

Make an acpi-enabled GENERIC.MP work on systems that only support PIC mode.

ok marco@


# 1.82 19-Feb-2007 jordan

Added acpi_map_address function for mapping GAS/IO registers
ok marco@


# 1.81 18-Feb-2007 jordan

Added dynamic perfect hash for opcodes
ok marco@


# 1.80 18-Feb-2007 marco

Shave off another K pre compression.
Shuffle SMALL_KERNEL functions around so that we only have a single #ifndef


# 1.79 17-Feb-2007 marco

Add SMALL_KERNEL throught acpi to make it fit on boot media.
ok deraadt


# 1.78 13-Feb-2007 marco

Add smarter _INI code. This helps with some dissapearing devices.

ok kettenis jordan


# 1.77 31-Jan-2007 gwk

Introduce acpi_hasprocfvs thats "ACPI Has Processor Frequency and Voltage
Scalling" a global flag set by acicpu attach to indicate that ACPI is
handling fvs through the _PSS objects. This will fix disappearing
EST/powernow when booting kernels with acpi enabled, as acpicpu isnt
compiled into GENERIC yet. Also in cases where acpicpu is built in and
legacy EST/powernow work but acpi is missing the _PSS object, the legacy
routines will be used.

tested by dim@, and Nick Nauwelaerts <nick AT nauwelaerts.net>
ok marco@, dim@


# 1.76 22-Jan-2007 mk

Actually look for docking stations by looking for _DCK nodes.

ok marco


# 1.75 02-Jan-2007 marco

On some systems EC must be attached as the first device. Move EC from
generic _HID devices to its own routine to force first attach. Bug found
and debugged by joris@

Code mostly written by joris@


# 1.74 26-Dec-2006 marco

Add polling to devices that require it such as acpibat and acpitz.
Use the same tsleep/wakeup mechanism as the interrupt code to keep it non-
concurrent.


# 1.73 21-Dec-2006 deraadt

even more knf love


# 1.72 21-Dec-2006 marco

Remove some leftover acpiec gpe/interrupt code.


# 1.71 20-Dec-2006 deraadt

oops, name already used


# 1.70 20-Dec-2006 deraadt

rename __acpi_enable_gpe(); ok marco


# 1.69 20-Dec-2006 deraadt

acpi_unmap_pmregs() is not used, ok marco


# 1.68 19-Dec-2006 deraadt

simplify some low-level methods substantially (shrinking code); ok marco jordan


# 1.67 06-Dec-2006 mk

malloc(..., M_NOWAIT) with no NULL check is bad. If allocation fails,
complain, clean up, and bail out of attach.

input and ok gwk


# 1.66 28-Nov-2006 jordan

Fixed clearing arg for acpi_init_pic
ok kettenis@


# 1.65 27-Nov-2006 kettenis

Don't spam dmesg.


# 1.64 27-Nov-2006 jordan

Fixes for acpiprt routing
Post-parse fixup for namerefs
ok kettenis@


# 1.63 25-Nov-2006 marco

Don't handle acpiev events inside the gpe handler since this causes deadlocks.

ok jordan@


# 1.62 15-Nov-2006 kettenis

Hook up acpi apic interrupt routing support to generic acpi code.

ok jordan@


# 1.61 02-Nov-2006 marco

Kill acpiec attach function and add its functionality to the generic _HID
device attach

prodded and ok jordan


# 1.60 02-Nov-2006 marco

Fix comments to reflect reality.


# 1.59 19-Oct-2006 marco

Replace ACPI ioctls with APM ioctls. This makes ACPI talk the same protocol
as APM therefore userspace can now retrieve power information in the same
fashion as all other apm arches (zaurus, macppc, i386 etc).

To use force the use of acpi device like so: apmd -f /dev/acpi

ok jordan@


# 1.58 19-Oct-2006 jordan

Fixed GPE debug levels
Fixed ref counter on temporary bufferfields
DSDT parsing fully working on IBM Thinkpad
ok marco@


# 1.57 15-Oct-2006 jordan

Removed old GPE handler code remnants
ok marco@


# 1.56 12-Oct-2006 jordan

Massive ACPI changes
Fixed ACPI leaks
new ACPI API
ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.55 30-Jun-2006 gwk

KNF, No functional change,
"go at it" marco@


# 1.54 30-Jun-2006 jordan

Support for calling Embedded Controller via new GPE interface
ok marco@


# 1.53 30-Jun-2006 jordan

Added new code for GPE handling
Also works with ACPIEC; need to fixup acpiec.c to remove original interrupt handler
ok marco@


# 1.52 30-Jun-2006 jordan

Added offset argument for acpi read/write pmreg routines (new GPE code)
ok marco@


# 1.51 31-May-2006 todd

do not call acpi_powerdown() unless acpi has successfully initialized
ok gwk@ canacar@


# 1.50 29-May-2006 canacar

Add embedded controller support.
ok marco@ deraadt@ testing & ok gwk@


# 1.49 19-May-2006 canacar

Minimal thermal zone driver. Monitors thermal zone temperature,
shuts down the system if the 'critical temperature' is reached.
ok marco@


# 1.48 11-Apr-2006 marco

KNF.


# 1.47 11-Apr-2006 gwk

Add acpi_resume(struct acpi_softc *sc), which calls some AML methods,
detected at attach time, currently unused.

ok marco@


# 1.46 11-Apr-2006 gwk

Discover some ACPI sleep related methods, and call them at the
appropriate time in acpi_enter_sleep_state().

ok marco@


Revision tags: OPENBSD_3_9_BASE
# 1.45 26-Feb-2006 marco

Add acpicpu as a device.

ok jordan@


# 1.44 23-Feb-2006 marco

remove dead function.

ok jordan@


# 1.43 22-Feb-2006 marco

Print out all tables it found during autoconf. We'll need this data in
the future.


# 1.42 22-Feb-2006 marco

Make dmesg less verbose.

ok jordan.


# 1.41 22-Feb-2006 jordan

Added parsing of SSDT tables
ok marco@


# 1.40 22-Feb-2006 jordan

Added new aml_register to notify by device id
Moved powerdown code to button device
ok marco@


# 1.39 21-Feb-2006 marco

Readjust debug levels that crept in too low over the last few days.
KNF


# 1.38 21-Feb-2006 marco

Rig the power button so that the machine shuts down correctly.


# 1.37 20-Feb-2006 marco

Add acpibtn device. Remains disabled for now.


# 1.36 19-Feb-2006 jordan

Added code for aml_notify GPE notification callback
ok marco@


# 1.35 19-Feb-2006 grange

Fix acpi_enter_sleep_state() by replacing current ugly hack
with mostly correct sleeping state transition code.

ok brad@ marco@


# 1.34 19-Feb-2006 marco

jordan:
Enable interrupts.
Added peripheral code to make this work.

marco:
Replace softintr and other types of interrupts with a handler with context.
Fixed interrupt magic for GPEs.
Some KNF

This is work done by jordan and I but has to be a single commit.


# 1.33 17-Feb-2006 marco

Remove earlier introduced delays. We really only need them during autconf.

ok jordan@


# 1.32 17-Feb-2006 marco

Add a generic delay function as per the ACPI spec.

from jordan@


# 1.31 17-Feb-2006 marco

Use tsleep instead of delay when !cold.


# 1.30 17-Feb-2006 marco

Argh, we need delays between reads/writes. The backend reads (i2c most likely)
are so slow that values come back as 0 sometimes. We have to do this
despite the spec saying we shouldnt have to. Guess having a faster
parser than others can hurt sometimes.

Idea from jordan.


# 1.29 17-Feb-2006 jordan

Fixed code for handling _INI
Rearranged debug statements
ok marco@


# 1.28 16-Feb-2006 jordan

Added stub for calling _INI
Fixed aml_dowhile for NULL return
ok marco@


# 1.27 16-Feb-2006 jordan

Fixed parser/evaluator to retrieve battery info+state
Cleanup of parser
Dell laptops return battery status
ok marco@


# 1.26 06-Feb-2006 brad

fix a typo so this will compile on amd64.


# 1.25 04-Feb-2006 marco

When ACPI_ENABLED wasn't set but the devices were it had an unknown variable.

Pointed out by ian@.


# 1.24 03-Feb-2006 jordan

Rewrote aml parser yet again...
Fixed bus_space_map for i386/amd64
Added _BST and _BIF to acpibat to get status/info
ok marco@


# 1.23 20-Jan-2006 jordan

More evaluator changes
Removed old evaluator code
Fixed _HID value
ok marco@


# 1.22 19-Jan-2006 jordan

Added support for read/write of PCI Config space
Help jason@
ok marco@


# 1.21 18-Jan-2006 jordan

Added new evaluation routines
Added helper methods for buffer/field evaluation
Fixed broken indentation on dsdt.c

ok marco@


# 1.20 17-Jan-2006 jordan

Major overhaul of the aml parser. Evaluater temporarily disabled.

ok marco@


# 1.19 06-Jan-2006 grange

Check for ACPI control availability only if ACPI_ENABLE defined.


# 1.18 06-Jan-2006 grange

Informative error messages on attach, prettier dmesg.


# 1.17 06-Jan-2006 grange

printf -> dnprintf conversion for debug messages.


# 1.16 05-Jan-2006 grange

Find SLP_TYPx values and store them for future use in
acpi_enter_sleep_state().


# 1.15 05-Jan-2006 grange

Random KNF and cleanup


# 1.14 05-Jan-2006 grange

Provide symbolic names for the acpi sleeping states and use it.


# 1.13 28-Dec-2005 marco

Moved DEVNAME to a acpidev.h

ok jordan@


# 1.12 18-Dec-2005 sturm

add back comment about wrong code

ok marco


# 1.11 16-Dec-2005 marco

Remove test for kbd_reset. Prompted deraadt@, ok jordan@


# 1.10 16-Dec-2005 jordan

Added hooks for powerdown/power button press


# 1.9 16-Dec-2005 jordan

Cleaned up common power management register i/o
Added enter sleep state ioctl
Hooked power button interrupt
ok marco@


# 1.8 16-Dec-2005 marco

Remove autoconf chatter


# 1.7 16-Dec-2005 jordan

Moved DSDT AML parsing into parent ACPI driver
ok marco@


# 1.6 14-Dec-2005 marco

Add AC device glue.


# 1.5 14-Dec-2005 marco

Adjust debug printing levels to something more sane.


# 1.4 13-Dec-2005 marco

Add glue to attach devices to apci.


# 1.3 07-Dec-2005 marco

KNF


Revision tags: OPENBSD_3_8_BASE
# 1.2 10-Jul-2005 grange

Spacing cleanup.


# 1.1 02-Jun-2005 tholo

Start on a basic ACPI framework -- does not do much more than read out the
ACPI tables into kernel memory and attach ACPI and HPET timers currently.

In order to test this code, enabling the devices in GENERIC as well as
the ACPI_ENABLE option is needed. This code does not do any thermal
control yet, so this should be done with care depending on the platform.

In the tree so more people can contribute to making this more fully
featured.

Ok niklas@ grange@ tedu@


# 1.338 08-Feb-2018 deraadt

After flushing disks, try again to release all memory. We don't need to
save dirty memory to the hibernate space.


# 1.337 08-Feb-2018 deraadt

Toss all releaseable memory, because fragmentation can get in the way
of allocating the hibernate playpen.


# 1.336 08-Feb-2018 deraadt

sr_quiesce() is a new approach for ensuring that softraid drains
output to the disks.

This is part of a larger suspend/resume filesystem-safety diff, which
has been worked on for a couple of months already. Tests by job, krw,
beck, benno, and others. Sometimes even by snapshot users...


# 1.335 29-Nov-2017 kettenis

Add support for GPIO-signaled events.

ok mlarkin@


# 1.334 16-Nov-2017 jcs

Add PCI attachment for dwiic(4) needed by Intel 100 Series machines

ok kettenis


# 1.333 14-Nov-2017 kettenis

Add a few more PNP IDs to the skip lists.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.332 17-Aug-2017 stsp

Make dwiic(4) attach its ACPI parent dependencies.
This change makes the Asus E200HA keyboard work.

Original analysis and patch by Cesare Gargano
ok kettenis@


# 1.331 09-Aug-2017 dcoppa

Add "PNP0303" (8042 PS/2 Controller) to acpi_isa_hids[]

ok kettenis@


# 1.330 22-Jul-2017 jcs

acpibat: look for _BIX first before falling back on _BIF

ACPI 4.0 deprecated _BIF for battery status so some newer machines
are exporting _BIX which extends _BIF's package with a few extra
fields. When using _BIX, export the new discharge cycle count as a
sensor.

feedback from kettenis and anton
ok deraadt


# 1.329 20-Jul-2017 kettenis

Start a list of isa(4) device names such that we can avoid printing them
as "not configured".

ok mikeb@, millert@, deraadt@


# 1.328 08-Apr-2017 deraadt

Sensors are run as callbacks inside tasks. During suspend a sensor
could be running inside a driver that will be force-detached, or due
to tsleep end up disrupting the softstate/hardstate contract. At
suspend time, quisce all these callbacks by waiting for completion.
This issue has never been observed for real, but may be implicated in
suspend/resume failures.
ok kettenis guenther mlarkin


# 1.327 08-Apr-2017 deraadt

Two prototypes for acpi_maptable; merge to one place


Revision tags: OPENBSD_6_1_BASE
# 1.326 27-Mar-2017 deraadt

Now that hibernate_alloc() only has clean success/failure, don't
need to call hibernate_free() to clean up a partial mess.
ok mlarkin kettenis


# 1.325 13-Mar-2017 deraadt

allow ramdisks to build; ok jcs


# 1.324 12-Mar-2017 jcs

try to attach acpisbs first and if successful, don't attach acpibat


# 1.323 02-Mar-2017 natano

Add a new sysctl machdep.lidaction. The sysctl works as follows:

machdep.lidaction=0 # do nothing
machdep.lidaction=1 # suspend
machdep.lidaction=2 # hibernate

lidsuspend is just an alias for lidaction, so if you change one, the
other one will have the same value. The plan is to remove
machdep.lidsuspend eventually when people have upgraded their
/ets/sysctl.conf.

discussed with deraadt, who came up with the new MIB name
no objections mlarkin
ok stsp halex jcs


# 1.322 28-Feb-2017 natano

Untangle abstract sleep modes from ACPI sleep states. This paves the way
for further work in the area.

idea and ok deraadt
ok mlarkin


# 1.321 22-Feb-2017 jcs

add acpisbs, an acpi smart battery subsystem driver reading data
over smbus

currently disabled because it conflicts with acpibat

ok deraadt, kettenis


# 1.320 14-Jan-2017 kettenis

Revert aml_rdpciaddr changes; breaks several machines that were working
before.


# 1.319 11-Jan-2017 jsg

Increase the size of the buffers used for _HID and _CID strings to be
able to fit "Hyper_V_Gen_Counter_V1".

ok mlarkin@ deraadt@


# 1.318 08-Jan-2017 kettenis

Make aml_rdpciaddr() work for for devices behind bridges. The code now
uses the mapping between AML nodes and PCI devices that we establish.
Because _INIT methods may end up calling aml_rdpciaddr(), make sure we
create that mapping early. Also handle devices that aren't actually present.
These devices are now included in the mapping and reads will return an
all-ones patterm whereas writes are a no-op..

ok mlarkin@


# 1.317 25-Oct-2016 pirofti

Add the resource index to aml_parse_resource() callback function.

This is needed by an upcoming acpiec commit that handles machines breaking
the current ACPI specifications.

Change suggested by and ok kettenis@, guenther@


# 1.316 18-Sep-2016 guenther

If the APIC table (MADT) isn't present then ci_acpi_proc_id won't be set
when acpicpu tries to attach, leading to a NULL dereference. The ACPI
processor object on the Dells where this was reported is bogus and useless
anyway so apply the same test when deciding whether to attach to processor
objects as acpicpu would when matching them to cpus.

Problem reported by Eivind Eide (xenofil (at) gmail.com) and
Johan Huldtgren (johan+openbsd-bugs (at) huldtgren.com).
Testing by Eivind Eide.
ok kettenis@ mlarkin@


# 1.315 03-Sep-2016 naddy

Write the system time back to the RTC every 30 minutes.
This fixes the problem that long-running machines which were not
shut down properly would reboot with a badly offset system time.

hints and ok kettenis@


# 1.314 31-Aug-2016 mlarkin

Don't put a device in the wake device list if _STA says it's not there.

Fixes at least one machine that woke up from sleep / poweroff immediately.

ok kettenis@, tested by kettenis and martijn@


# 1.313 28-Jul-2016 kettenis

Store the acpi processor ID/UID in struct cpu_info, and use it to attach
acpicpu(4) drivers to the right cpu(4).

ok mlarkin@, guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.312 10-Jun-2016 kettenis

The power management timer has been made optional in ACPI 5.0A, so don't
attempt to attach acpitimer(4) if the timer isn't present.

Pointed out by jcs@

ok jcs@


# 1.311 08-May-2016 kettenis

Make sure devices listed by a device's _DEP method are attached before
we attach the device itself. The _DEP methode, introduced in ACPI 5.0,
is a hint that the device in question depends on OpRegion support from
the devices listed by _DEP, so we have to order them the proper way.
To prevent us from attaching those devices again when we encounter them
later walking down the device tree, keep track of our attempts to
attach a device driver to them using aflag in the aml_node structure.

ok guenther@


# 1.310 25-Apr-2016 pirofti

We attach acpihpet(4) and acpiec(4) without using the HID, so do not
report it in the dmesg.

OK deraadt@, mlarkin@.


# 1.309 02-Apr-2016 kettenis

Reduce dmesg spam by knocking out some acpi hardware IDs that don't need a
driver.

ok mpi@


# 1.308 29-Mar-2016 kettenis

Check if a device is present (using _STA) in acpi_foundhid. This prevents
us from reporting devices as "not configured" that aren't actually present,
and allows us to remove duplicated code from several drivers.

ok deraadt@, ok jsg@


# 1.307 29-Mar-2016 kettenis

Implement a cleaner way to prevent acpivout(4) from attaching if we have
a platform-specific method of controlling the backlight.

ok millert@, mlarkin@, jung@


# 1.306 28-Mar-2016 kettenis

Adding each and every new acpi device driver to acpi_foundhid() is
gettinga bit of the burden. And it really isn't how our autoconf
framework is supposed to work for busses that can be enumerated. All
the drivers already check for a matching _HID in their attach
function. So we can just drop the checks in acpi_foundhid() and let
the drivers sort themselves out.

Print a "not configured" line for devices for which we don't attach adriver.
This may be a bit much as there are devices for which we will not have a
driver. This will be removed if it causes too much dmesg spam.

ok deraadt@, mlarkin@


Revision tags: OPENBSD_5_9_BASE
# 1.305 17-Jan-2016 jsg

Take acpi_foundhid() outside of #ifndef SMALL_KERNEL so the ramdisk
kernel can find and attach dwiic(4) and sdhc(4).


# 1.304 17-Jan-2016 jsg

remove an unused global variable


# 1.303 14-Jan-2016 kettenis

Attach dwiic(4) on the Intel Bay Trail i2c controllers as well.


# 1.302 12-Jan-2016 kettenis

Improve emulation of PM registers on Hardware-redcued ACPI by emulating
SPL_TYPx, SLP_EN and WAK_STS using the new SLEEP_STATUS_REG and
SLEEP_CONTROL_REG registers. Makes the transition into S5 (powerdown) work
on the ASUS X205TA.

ok deraadt@, mlarkin@


# 1.301 12-Jan-2016 jcs

Add dwiic, a driver for the Synopsys DesignWare i2c controller found
on the Samsung ATIV Book 9 laptop. This initial version only
supports ACPI config/attachment.

Add ihidev, a HID-over-i2c driver largely based on uhidev. dwiic
handles attaching ihidev devices found in ACPI.

Add ims, a HID-over-i2c mouse/trackpad driver to get basic cursor
and button functionality from HID-compliant i2c trackpads.

ok kettenis deraadt


# 1.300 11-Jan-2016 kettenis

Make sdhc(4) attach to hardware IDs "80860F14" and "PNP0FFF".


# 1.299 09-Jan-2016 kettenis

Initial support for Hardware-Reduced ACPI. This skips the legacy mode checks
by emulating the PM_CNT register such that SCI_EN is always enabled. It also
skip attaching acpitimer(4) since the relevant registers are no longer there.

ok deraadt@, mlarkin@


# 1.298 06-Jan-2016 kettenis

Follow the spec and check the SCI_EN bit to see if ACPI has already
been enabled and don't bother enabling it again if it is. This should fix
systems that don't implement legacy mode and therefore set ACPI_ENABLE and
ACPI_DISABLE to zero.

ok mlarkin@, yasuoka@


# 1.297 23-Nov-2015 reyk

Back out the CRITICAL change, turn it back from 10% to 25%. Some
people with old/broken batteries didn't like this change, and the
underlying heuristics are wrong.


# 1.296 22-Nov-2015 reyk

The "CRITICAL" APM battery level, that is exported/faked by the ACPI
stack, is currently set to 25%. With 25% battery left, a typical
modern laptop will have at least one hour runtime left. It is an
arbitrary value, and other APM power layers in the kernel set CRITICAL
to something like 5% or 10%, so set it to 10% for now.

OK mlarkin@


# 1.295 28-Sep-2015 deraadt

In low-level suspend routines, set cold=2. In tsleep(), use this to
spit out a ddb trace to console. This should allow us to find suspend
or resume routines which break the rules. It depends on the console
output function being non-sleeping.... but that's another codepath which
should try to be safe when cold is set.
ok kettenis


# 1.294 06-Sep-2015 deraadt

clarify malloc arguments (base + length...)


# 1.293 28-Aug-2015 deraadt

fairly simple sizes for free(); ok tedu


# 1.292 20-Aug-2015 kettenis

Respect the access size when reading or writing to pci config space.
This version also makes sure writes are properly aligned, preventing
the panics seen with the previous incarnation of this change.

Fixes battery status passthrough in vmware, brightness keys on my x220
and who knows what else.

ok mlarkin@, deraadt@


Revision tags: OPENBSD_5_8_BASE
# 1.291 04-Aug-2015 deraadt

If we are setting ACPI_PM1_BM_RLD do it only once, and remember to
repeat this action at resume time.
ok mlarkin guenther


# 1.290 29-Jul-2015 deraadt

Something subtle in previous revision is locking up suspend or booting
on some laptops. Mailing lists threads mention x220 and Asus machines.
Reverting for now.


# 1.289 21-Jul-2015 kettenis

iRespect the access size when reading or writing to pci config space.
Fixes battery status passthrough in vmware and who knows what else.

Tested by many.

ok mlarkin@, deraadt@


# 1.288 17-Jul-2015 mlarkin

fix comments


# 1.287 26-Mar-2015 jsg

Use the 0x10 extended capability to determine if the trackstick
buttons are wired to the trackpad and need to be re-routed to the
trackstick. Without this change the buttons on 2015 thinkpads
get picked up as extended buttons that show up as scroll up/down.

Remove the X1 Carbon 2015 (LEN0048) and X250 (LEN0046) from the
top button area/soft buttons quirks list. Also avoid using the
quirk list entirely if the capability bit is set.

Based on work from jcs@ with feedback from mpi@
ok mpi@ jcs@


# 1.286 20-Mar-2015 kettenis

The ACPI standard says that a PCI function number of FFFF can be used to refer
to all functions on a device. This makes no sense in the context of accessing
PCI config space. Nevertheless stsp@ has a machine with AML that tries to
do config space reads from a device where _ADR encodes function number FFFF.
This is almost certainly buggy AML, but since Linux (and presumably Windows)
boot just fine on this machine we need to work around this. Linux effectively
treats function FFFF as function 7, which isn't present on this machine.
Instead we simulate a read from a non-existent device by returning a bit
pattern of all ones. Writes to function FFFF will still panic.

ok mlarkin@


# 1.285 16-Mar-2015 deraadt

Reduce use of SMALL_KERNEL in acpi, bringing acpiec driver and more gpe
handling into RAMDISK. This is now possible because the install media
has ample room. The goal is to reduce special cases where we may be
skipping (unknown) important operations...
ok mlarkin kettenis


# 1.284 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.283 07-Feb-2015 deraadt

New framework that allows hibernate to pass in entropy from it's fresh
boot.
ok mlarkin


# 1.282 06-Feb-2015 mlarkin

Fix a hibernate crash on some machines due to unmapping a page that
may not have been mapped previously (in the failure to hibernate case).

Also ensure that the lowmem ptp is mapped in all cases (not just MP).

ok kettenis


# 1.281 17-Jan-2015 deraadt

resume_randomness() before spinning up other cpus...


# 1.280 15-Jan-2015 jsg

Define a new wscons mouse type for Synaptics clickpad devices that lack
physical buttons. This will be used if the acpi pnpid for the mouse
matches a list provided by Synaptics found in Linux.

Combined with the xenocara changes this will remove the need for an xorg
config file for the x240/t440/t540 generation of thinkpads.

ok matthieu@ shadchin@ kettenis@


# 1.279 11-Jan-2015 kettenis

Tolerate checksum errors in ACPI tables (but complain about them). Apparently
there are machines out there with broken BIOSen with such checksum errors in
thier RSDT tables. Windows runs fine on these machines, so it probably does
not check. Linux ignores checksum errors as well (and prints a warning).

ok deraadt@, krw@


# 1.278 18-Dec-2014 deraadt

suspend and resume the RNG (at what looks like the right spots)
ok reyk djm


# 1.277 09-Dec-2014 doug

Replace some malloc(n*size,...) calls with mallocarray().

ok tedu@ deraadt@


# 1.276 06-Dec-2014 mpi

If "machdep.lidsuspend" is not set, do not go back to sleep if the lid
is still closed when resuming. This allows docked laptop to be resumed
without opening them.

ok mlarkin@


# 1.275 23-Nov-2014 mlarkin

If a machine has no _LIDs, then none can be open. This diff fixes a problem
where machines without any _LIDs were going back to sleep after hibernate
due to the recent suspend-if-lid-closed-on-resume diff. Noticed on qemu,
which doesn't have any _LIDs.

discussed with deraadt


# 1.274 18-Nov-2014 krw

Nuke yet more obvious #include duplications.

ok miod@


# 1.273 08-Nov-2014 mlarkin

If resuming from sleep (zzz/ZZZ) and the lid is still closed, go back to
sleep. This prevents accidental lid flex or slight opening in a backpack
from waking the machine up and leaving it resumed (powered on).

ok deraadt@


# 1.272 26-Sep-2014 kettenis

Rework piglet and pig allocation. Currently the piglet gets allocated
deep down in the suspend path, where it is really hard to recover from
allocation failure. So allocate the piglet early on in the suspend path.
Also change the piglet and piglet allocation functions to use km_alloc(9)
instead of doing pmemrange magic. This removes a bunch of code which, in the
case of the piglet allocation, is broken since it results in a NULL pointer
dereference. Also switch the piglet allocation to not wait. If we can't
allocate 16MB of phys contig memory on a halfway modern machine we're almost
certainly under a lot of memory pressure and we're better off not trying to
hibernate anyway.

ok mlarkin@


# 1.271 25-Sep-2014 kettenis

Bring back rev 1.249. Now that mp_setperf() has been fixed, it should be
safe to restore perflevel when resuming. Should fix the issues people
see with the processor running at full speed heating up the machine while
the apm command reports that the CPUs are running at a low speed.

ok dcoppa@, deraadt@


# 1.270 23-Sep-2014 kettenis

Release the acpi lock around calls to wsdisplay_suspend() and
wsdisplay_resume(). The X server may adjust the display brightness just
before/after the VT switch when suspending/resuming. In fact, it may
already be blocked adjusting the display brightness for some reason
unrelated to the suspend request.

The VT switch request will eventually time out, but the delay is annoying
and the failed VT switch will lead to problems after resume.

Needed for a future update of the xf86-video-intel driver.

ok mlarkin@


# 1.269 19-Sep-2014 kettenis

Use config_suspend_all(9).


# 1.268 08-Sep-2014 kettenis

Clear the PME Status bit on PCI devices in response to Device Wake events.
Fixes the interrupt storm on the 2009 Mac mini with WOL enabled on its
nfe(4) interface.

ok mlarkin@


Revision tags: OPENBSD_5_6_BASE
# 1.267 20-Jul-2014 mlarkin

Support hibernating to softraid crypto volumes.

much help and ok from deraadt@


# 1.266 16-Jul-2014 mlarkin

Reenable hibernate RLE support and flush+zero all memory after unpack.


# 1.265 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.264 12-Jul-2014 kettenis

Add a function to drop all clean pages on the page daemon queues and call
it when we hibernate.

ok mlarkin@, miod@, deraadt@


# 1.263 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.262 11-Jul-2014 mlarkin

Flush the buffercache to 16MB on hibernate and restore its previous max
size (kern.bufcachepercent) on resume, for better hibernate performance.

ok beck@


# 1.261 10-Jul-2014 blambert

remove unneeded header

ok mlarkin@


# 1.260 01-Jul-2014 kettenis

Adding an additonal delay after each access to SystemMemory or SystemIO
during autoconf makes no sense.

ok deraadt@


# 1.259 23-Jun-2014 kettenis

SystemMemory is used to access memory mapped registers on some machines, so we
must use the bus_space(9) API to access it instead of memcpy(9). Also make
sure we properly align access to fields that need more than byte alignment.

ok guenther@


# 1.258 21-May-2014 mlarkin

Fix invalid / missing format specifiers in several dnprintf statements
(used only when ACPI_DEBUG is enabled)

From a diff on tech@ by Fabian Raetz, with a minor change by me, tested
on i386 and amd64 with ACPI_DEBUG enabled and disabled.


# 1.257 25-Apr-2014 mlarkin

Defer the free of the hibernate structures until later, when it is safe to
do so.

Freeing the hibernate structures too early leads to inadvertent calls into
splx when we aren't ready for them yet. Found when trapping splx calls in
the resume path.

discussed with deraadt@ at m2k14


# 1.256 13-Apr-2014 mlarkin

Fix a badly worded debug message that implied the entire machine was going
to sleep, instead of just the acpi thread going to sleep.


# 1.255 31-Mar-2014 mpi

Change the order of operations during a suspend/resume cycle and
call bufq_quiesce() after executing the DVACT_QUIESCE handlers.

This should be safe since no disk nor controller drivers have such
handler but it will allow us to detach sd(4) devices attached to a
USB bus.

Another benefit pointed out by kettenis@ is that drivers that need
to read a firmware from the disk should be able to do it at resume
time in a DVACT_WAKEUP handler.

ok kettenis@, deraadt@


# 1.254 13-Mar-2014 dlg

get rid of the assumption that the head of the alldevs list is the
"mainbus" device. this breaks when mpath is enabled because it
attaches before mainbus and therefore takes the head position.

have autoconf provide device_mainbus() which looks up mainbus_cd,
and use that instead.

discussed with deraadt who just wants mpath stuff to move forward
despite there being many ways to shine this particular turd.


Revision tags: OPENBSD_5_5_BASE
# 1.253 21-Feb-2014 deraadt

Do the lid control further on in the resume sequence, since this is
AML, and will do WAITOK operations. While cold, that is a bad idea.
We seem to be safe with respect to the spec, I do not see any event
debounce issues here.
ok kettenis, tested by many


# 1.252 20-Jan-2014 claudio

Instead of checking sc->sc_revision <= 1 check that the
fadt->hdr_revision <= 1. This is a better check since it checks
the version of the proper descriptor. This allows the alix APU
to reboot since those boards have currently a wrong sc->sc_revision.
Committing this now so that it can be tested easier.
With and OK jsg@ also fine by derradt@


# 1.251 24-Dec-2013 kettenis

If the FADT has its SMI_CMD set to zero, assume we're only ACPI-only hardware
and don't need to disable SMI ownership of the ACPI hardware registers.

ok mlarkin@


# 1.250 23-Dec-2013 kettenis

Revert previous commit. It makes resume hang on some MP systems running
GENERIC.MP.


# 1.249 16-Dec-2013 kettenis

Restore hw.setperf upon resume like we do for apm(4) on i386. Seems we need
to do this fairly late in the resume to avoid hangs.

ok deraadt@, mlarkin@


# 1.248 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


# 1.247 06-Nov-2013 mpi

Add support for Power Resources for Dx states and the necessary hook
for PCI devices. This hook should be called twice, before and after
changing the power state of a PCI device.

Before setting the device to the new state, the ACPI layer will notify
every power resources linked to the device for that state and make sure
they are turned "_ON". After changing the state of the device, it will
decrement the reference of every power resources linked to that device
for the old state and turn them "_OFF" if they are no longer referenced.

This fixes the no-USB after resume problem seen on various ThinkPad,
problem initialy diagnosed with Alexander Polakov.

ok kettenis@, deraadt@


Revision tags: OPENBSD_5_4_BASE
# 1.246 01-Jun-2013 mlarkin

Stop printing ACPI wakeup devices in dmesg after the 16th wakeup device.

Helps with VMware VMs that report hundreds of wakeup devices.

ok reyk@, deraadt@


# 1.245 31-May-2013 bluhm

Fix the build for a kernel without wd and pciide in its config.
OK deraadt@ tedu@


# 1.244 30-May-2013 deraadt

Enforce ca_activate tree-walks over the entire heirarchy for all events,
cleaning up some shutdown-hook related code on the way.
(A few drivers related to sparc are still skipped at kettenis' request)
ok kettenis mlarkin, tested by many others too


# 1.243 18-Apr-2013 deraadt

pin the acpi thread to the BSP, because AML and SMI cannot be trusted. I
was certain we were doing this already, but it turns out no...


Revision tags: OPENBSD_5_3_BASE
# 1.242 24-Dec-2012 guenther

In acpiioctl(), Return though the splx() path if suser() returns an error

ok deraadt@


# 1.241 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


# 1.240 04-Oct-2012 ehrhardt

Use information provided by ACPI to attach secondary PCI host bridges.

ok kettenis@


# 1.239 07-Sep-2012 kettenis

Add a new function called acpi_pci_min_powerstate() that returns the lowest
power state for a PCI device for the power state (currently S3 or S4) we're
transitioning into. This function is probably too simplistic; there is a lot
of (silly) complexity in the ACPI spec.

ok deraadt@


Revision tags: OPENBSD_5_2_BASE
# 1.238 13-Jul-2012 pirofti

Disable acpivideo(4) on ASUS laptops.

There are some models that panic whenever an event is received.
Some cope, some don't. In order to get this right acpiec(4) requires
major rework. Until then let machines boot.

Discussed with deraadt@.


# 1.237 11-Jul-2012 mlarkin

for now, restrict hibernate to root in the acpi ioctl kernel interface.
normal rules for controlling this interface through apmd still apply.

ok deraadt@


# 1.236 09-Jul-2012 deraadt

tiny little oops in untested hibernate block


# 1.235 09-Jul-2012 deraadt

clean up ioctl() case statements


# 1.234 09-Jul-2012 deraadt

acpi_disable_onegpe() is not used; ok pirofti


# 1.233 24-May-2012 kettenis

If _MIN > _MAX consider a WordBus resource to be invalid. Fixes a panic on
the IBM x3550-M4.

ok mikeb@


# 1.232 29-Mar-2012 mlarkin

Missing #ifdef in previous diff


# 1.231 29-Mar-2012 mlarkin

Prevent hibernation attempts on machines with unsupported disk controllers.
The same check is also performed later in the hibernate sequence, but by
then it's too late to cleanly unwind (presently).

ok deraadt


# 1.230 26-Mar-2012 deraadt

simplify conditionals in acpi_sleep_state()
ok mlarkin


# 1.229 26-Mar-2012 deraadt

hook in the hibernate request code; half of this diff is from mlarkin
ok mlarkin


Revision tags: OPENBSD_5_1_BASE
# 1.228 20-Sep-2011 deraadt

Very early in suspend while the clock is still ticking realtime,
save the clock back to the rtc. The zaurus already did this.
ok phessler miod kettenis


Revision tags: OPENBSD_5_0_BASE
# 1.227 02-Jul-2011 nicm

kqueue attach functions should return an errno or 0, not a plain 1. Fix
the obvious cases to return EINVAL and ENXIO.

ok tedu deraadt


# 1.226 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.225 16-Jun-2011 pirofti

Import acpitoshiba driver.

This is originally written by Hiroyuki Aizu, ported and integrated
in our tree by Javier Vazquez <javama4 AT gmail> with tweaks and
suggestions by me.

Import prodded by deraadt@ and the lack of testers on tech@.


# 1.224 27-Apr-2011 jcs

attach acpithinkpad to newer lenovo models like the x120e

ok deraadt@


# 1.223 22-Apr-2011 jordan

Fix uninitialzied variables and formatting strings (-Wxxx errors)


Revision tags: OPENBSD_4_9_BASE
# 1.222 02-Jan-2011 jordan

Re-add code for ACPI taskqueue, eliminates need for state vars in acpi softc
Serialize processing ACPI tasks for sleep/powerdown/GPE event
ok deraadt@


# 1.221 31-Oct-2010 guenther

Revert last commit: it breaks resume on Thinkpads
"then please back it out" deraadt@


# 1.220 26-Oct-2010 jordan

Add task queue for ACPI gpe and notify handlers
This fixes eject on Dell Latitude dock and an issue on the Dell Mini
battery update.
ok deraadt


# 1.219 07-Oct-2010 jordan

Create separate functions for enabling wake and runtime gpes.
Remove old suspend/resume gpewalk function
ok deraadt, mlarkin


# 1.218 05-Oct-2010 jordan

split acpi_enable_onegpe to separate enable/disable functions
ok deraadt


# 1.217 05-Oct-2010 jordan

Added acpi_disable_allgpes function to clear GPE status
ok deraadt


# 1.216 05-Oct-2010 deraadt

clean up acpi_softc use more


# 1.215 05-Oct-2010 deraadt

delete a bit of #if 0 code


# 1.214 05-Oct-2010 deraadt

hide use of our global acpi_softc pointer a bit better


# 1.213 29-Sep-2010 deraadt

grab the acpi thread lock very early in acpi thread startup; not that it
is likely that anyone will race us... but why be careless.
ok kettenis


# 1.212 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.211 29-Aug-2010 jasper

- add a check for FWRITE here too.

ok miod@


# 1.210 27-Aug-2010 kettenis

Make PCI Power Management optional, and only enable it when acpi(4) attaches.

ok deraadt@


Revision tags: OPENBSD_4_8_BASE
# 1.209 08-Aug-2010 kettenis

Close race between the acpi thread and wscons ioctls running in process
context. We might rip this out post-release and replace it with a solution
that serializes things in a more obvious way.

ok deraadt@, miod@ and "those crazy texans who ok anything"


# 1.208 08-Aug-2010 deraadt

Report the acpi sleep states when on a RAMDISK; ok krw


# 1.207 07-Aug-2010 kettenis

Enforce acpi register access rescrictions.

ok mlarkin@, deraadt@, tested by marco@


# 1.206 07-Aug-2010 deraadt

upon resume, notify all the acpiac and acpibat drivers to update
their status. some laptops do not do this automatically.
ok kettenis canacar


# 1.205 06-Aug-2010 marco

Always call _PSW on _LID devices that support it to enable lid open to
resume the machine. Conversely disable it when the machine wakes up.

Tested by several ok deraadt


# 1.204 06-Aug-2010 deraadt

As part of a revamp of the PM1/GPE code, I write this diff to look for
unmanaged EN & STS bits in the PM1 register at interrupt time and report
them. As a side effect this splits the STS acknowledgement into two writes
(for power, and sleep) instead of one. The printf that is added (to spot
unmanaged STS bits) has not yet been triggered as far as we know.

Before the "write to PM1 registers at the right offset" diff went in, this
was not neccessary. But newer thinkpads do not have a working soft power
button without this diff.

We have no idea why.
ok mlarkin kettenis


# 1.203 05-Aug-2010 deraadt

PM1 power button events were generating 'fake events' to acpibtn PWRB,
but if there is no such device these were being tossed. Instead, arrange
for all power button handlers to flag a softc variable which is processed
by the thread; so that one place will know to request a powerdown (psignal
to init)
ok marco mlarkin


# 1.202 05-Aug-2010 deraadt

rename a variable which is annoyingly poorly named


# 1.201 05-Aug-2010 mlarkin

Write to the correct offset for various ACPI pmreg registers. Writing to
the correct register does properly clear the wake status bits and fixed
event bits, which allows some machines to have more than 1 suspend cycle.

bug spotted by deraadt@

tested by deraadt@ and myself on a half-dozen machines and others on
hackers@

ok deraadt@, marco@


# 1.200 05-Aug-2010 deraadt

Fix the power button. The pm1 register is 16 bits in size.
My mistake, a while back.
tested by krw


# 1.199 04-Aug-2010 kettenis

Print supported sleep states.

ok marco@, phessler@, deraadt@


# 1.198 03-Aug-2010 mlarkin

Use the proper flag for re-enabling certain hardware events (power btn,
sleep btn). The (incorrect) flag prevoiously used coincidentally had the
same value, so this shouldn't cause any different behavior than before.

tested on a variety of machines (i386, amd64, sp, mp) - no behavioral
change seen

ok deraadt@


# 1.197 28-Jul-2010 marco

Make all gasio failures look and behave the same.


# 1.196 28-Jul-2010 mlarkin

Delay a little longer on suspend before giving up. Fixes machines that
sometimes don't suspend because the sleep transition takes too long
(symptoms like failing to sleep but the backlight stays on).

ok deraadt@


# 1.195 27-Jul-2010 deraadt

remove XXX; EC can now be attached early when needed; ok jordan


# 1.194 27-Jul-2010 deraadt

more brackets feels good


# 1.193 27-Jul-2010 deraadt

XXX annotate a block in acpi_enter_sleep_state() that is pretty worrying


# 1.192 27-Jul-2010 deraadt

horrific indentation that nearly made me puke


# 1.191 27-Jul-2010 mlarkin

Set the sleeping indicator light on machines that support it, in case SMI
doesn't do it for us. Shows the 'moon' LED on older Thinkpads when
sleeping.

Tested on T43p, W500, T510 by me and on X61s by deraadt@.

ok deraadt@


# 1.190 27-Jul-2010 jordan

Early initialization of acpiec if ECDT table exists
Fixes hang when booting thinkpads while docked
ok deraadt


# 1.189 26-Jul-2010 deraadt

remove merge error which was commited


# 1.188 26-Jul-2010 pirofti

Add support for Sony ACPI hotkeys via a new driver: acpisony(4).

Currently it only works for the suspend button and tries to do right for
the brightness events, but I haven't found a Sony laptop that like to do
right so far.

In the future I want to make the brightness keys work on all Sony's and
also add support for the zoom hotkeys and whatever other funky keys I can
find on those things.

Okay deraadt@.


# 1.187 22-Jul-2010 deraadt

pretty large cleanup of SMALL_KERNEL


# 1.186 21-Jul-2010 deraadt

in the acpi GPE handler, late ack level interrupts not edge interrupts.
Duh.


# 1.185 21-Jul-2010 deraadt

Merge the edge and level gpe handlers into one (now possible since the
gpe structure tells us whether the gpe is edge or level)


# 1.184 21-Jul-2010 deraadt

delete #if 0 code we are not going to use; ok marco


# 1.183 20-Jul-2010 deraadt

make the mask code for acpi_enable_onegpe() match other similar code


# 1.182 20-Jul-2010 deraadt

Handle the acpi interrupt controller with proper edge/level handling,
wrapped in spltty to avoid racing against the interrupt controller.
Repair the gpe bit masking code while there, and do operations in the
right order
ok kettenis mlarkin, help from jordan at figuring out the order of
operations.


# 1.181 20-Jul-2010 matthew

When we're preparing to go to sleep, only call bufq_restart() if other
preparations failed.

ok kettenis@, thib@, deraadt@


# 1.180 19-Jul-2010 deraadt

permit compilation with ACPI_DEBUG


# 1.179 19-Jul-2010 deraadt

there is no need for a seperate dsdt_softc pointer; dsdt does not have a
softc but is just a part of the single acpi driver.
ok jordan


# 1.178 19-Jul-2010 deraadt

acpi_delay() is no longer needed; ok jordan


# 1.177 18-Jul-2010 mlarkin

Clear event status on resume, to avoid going back to sleep/reboot/shutdown
immediately after resume. Tested by a bunch of people on hackers@ on
various hardware. Fixes at least Toshiba NB200 and Gateway LT3103u.

ok marco


# 1.176 13-Jul-2010 deraadt

rename some thread related interfaces so they are logical


# 1.175 10-Jul-2010 jordan

Verify _ADR bdf is correct (some systems return 0xffff)
Works on Dell x9xx and Studio 1555


# 1.174 09-Jul-2010 jordan

Clear the noise


# 1.173 08-Jul-2010 jordan

Add mapping for ACPI device to PCI bdf (match autoconf tree)
Simplify resource parsing function to use buffer argument
Convert namespace linked lists to use queue macros
ok marco@, deraadt@


# 1.172 06-Jul-2010 deraadt

Use spltty() to lock downcalls from apm against the information being
modified by the acpi thread.
ok kettenis


# 1.171 06-Jul-2010 deraadt

Don't use a workq to do the suspend, because it races aginst the acpi
thread. Instead, just tell the acpi thread to do the suspend for us.
This makes apmd & zzz work correctly. While here, have acpithinkpad
attempt to post the event to apm before prompting the suspend itself.
ok kettenis marco mlarkin


# 1.170 05-Jul-2010 mlarkin

Reset register SCI_EN on resume. The ACPI spec clearly states we don't have
to do this, but many machines don't, or forget to, which leads to all sorts
of problems on resume (broken battery stats, overheating, failure to resume
more than once, etc).

tested by many, ok marco@, deraadt@


# 1.169 01-Jul-2010 thib

do bufq quiesce again, since vnd/the bufq_done call in
biodone has been fixed.


# 1.168 01-Jul-2010 jordan

Backout recent AML changes, breaks on R210 and others


# 1.167 01-Jul-2010 jordan

Simplified aml_parse_resource call, cleanup code
ok marco


# 1.166 01-Jul-2010 jordan

Fix subordinate bus # for multi-root PCI buses
ok marco, kettenis


# 1.165 30-Jun-2010 thib

Disable/partially backout the bufq quiesce changes as this
is causing havoc with vnds and release must be buildable.


# 1.164 29-Jun-2010 deraadt

Fix ramdisks (new code was placed inside a SMALL_KERNEL chunk)


# 1.163 29-Jun-2010 jordan

Add support for mapping ACPI to PCI devices
ok kettenis, deraadt


# 1.162 29-Jun-2010 kettenis

Call bufq_quiesce() on suspend to stop all I/O and bufq_restart on resume.
Makes suspend/resume work much better while doing disk I/O.

ok mlarkin@


# 1.161 29-Jun-2010 tedu

60 is a more reasonable guess for number of minutes in an hour than 100.
spotted by TeXitoi. ok deraadt


# 1.160 27-Jun-2010 jordan

Add _acpi_matchhids routine, precursor to pci device match


# 1.159 27-Jun-2010 mlarkin

Clear ACPI fixed event status on resume (power buttons/etc) so that some
machines don't immediately resume after suspending to S3.

Tested by kettenis@ and I on 5 or 6 machines, deraadt@ "yeah, looks good"


# 1.158 27-Jun-2010 jordan

Cleaned up acpi_maptable, adds to table list directly


# 1.157 07-Apr-2010 deraadt

If we have done a wsdisplay_suspend(), and ACPI decides it does not want to
suspend, we must roll back by calling wsdisplay_resume() or the virtual
console switching will be locked forever
ok oga


# 1.156 07-Apr-2010 kettenis

Make suspend/resume work on MP machines (running an MP kernel). Joint work
from mlarkin@ and me, with some amd64 fixes thrown in by deraadt@

ok marco@, deraadt@, pirofti@, mlarkin@


# 1.155 31-Mar-2010 kettenis

Bring /dev/apm support in line with apm(4). Make the suspend button send
a suspend request event to apmd(8) instead of suspending immediately. Also
keep track of whether /dev/apm and /dev/apmctl are currently open, such that
we can still suspend immediately if apmd(8) isn't running.

ok deraadt@, marco@, pirofti@, jsing@, oga@


# 1.154 30-Mar-2010 oga

Prevent the apmd/x races for good.

When we hit suspend time, go through all wsdisplays on the system. if
they are in mode MAPPED, but not MODE_DUMBFB then if possible do a full
vt switch to a !mapped vt, and prevent switching back until resume time.
This has to be called from MD code because this involves userland
running so that X can run the vt switch signal handler. This way, any
case where we are using the "poke registers from userland" model, we
will not be on the hardware when we go down, so the kernel can actually
handle thing properly.

Tested on several acpi laptops (by kettenis@ and ian@), x40 (me and
beck@ at LEAST) and zaurus (me). Maybe others, but if so I forgot who at
this time..

Idea from deraadt somewhere over the Faroe Islands (I thought of a
similar thing myself a while ago). Much prompting from him. Ok and
comments miod@


# 1.153 25-Mar-2010 oga

Fix & vs. && in the kqfilter code.

Based on a comparison with the apm code.

ok deraadt@, kettenis@


Revision tags: OPENBSD_4_7_BASE
# 1.152 26-Nov-2009 mlarkin

Visual (and on some machines, audio) feedback while the machine is
resuming via calls to aml _SST method. This also makes the "moon" LED on
thinkpads to blink during resume and cease when resume is completed.

ok pirofti@


# 1.151 26-Nov-2009 deraadt

Toggle into "cold" when we we disable interrupts, because tsleeping
in the AML interpreter and context switching into userland processes
tends to sometimes pop the ACPI trampoline page out of our address
space. Makes lots more machines work.
ok kettenis mlarkin


# 1.150 24-Nov-2009 jsg

in the resume path evaluate _WAK before dealing with device drivers
like the spec says, not after.

ok mlarkin@


# 1.149 24-Nov-2009 deraadt

abort suspends on SMP systems until that gets written; ok mlarkin


# 1.148 23-Nov-2009 mlarkin

pci should return the result of it's children's suspend/resume calls back
to its parent.

handle suspend failure case and unwind if devices have an activate function
that returns failure.

ok deraadt@, kettenis@


# 1.147 23-Nov-2009 mlarkin

acpi* devices need to call their children on suspend/resume.
ok deraadt@


# 1.146 23-Nov-2009 pirofti

Remove ACPI_SLEEP_ENABLED checks.

This enables by default the suspend/resume paths in the kernel.

Okay deraadt@.


# 1.145 23-Nov-2009 deraadt

knf and spacing that are super annoying


# 1.144 23-Nov-2009 mlarkin

Hook up Theo's new children suspend/resume function, as well as more ACPI
suspend/resume glue.

ok deraadt@


# 1.143 26-Oct-2009 deraadt

Do not do strncmp comparisons on dv_xname because longer device names which
look similar could arrive in the future. Instead, compare directly against
dv_cfdata->cf_driver->cd_name
Issue originally spotted by miod


# 1.142 26-Oct-2009 jordan

Verify checksum+address when loading ACPI tables.
Some systems had invalid entries in RSDT/XSDT.
ok marco@


# 1.141 23-Jul-2009 cnst

New aibs(4) driver for ASUSTeK AI Booster (ACPI ATK0110) hardware monitoring,
with sensor state support through limits provided by the ACPI.

Tested on several ASUS motherboards kindly networked by Sam Fourman Jr.

ok deraadt marco jordan


Revision tags: OPENBSD_4_6_BASE
# 1.140 03-Jun-2009 pirofti

Import acpi power resource driver. This is needed for suspend/resume logic.

Okay jordan@ and marco@.


# 1.139 03-Jun-2009 pirofti

Add acpivideo support. This adds brightness support for all laptops
except thinkpads, they will use the acpithinkpad driver. The driver is
also hooked into wsconsole. So brightness can be adjusted via:

$ wsconsctl display.brightness=<percentage>

This is very helpfull on some laptops that have a nasty bios and get two
steps instead of one when pressing the brightness button.

Tested on various dell, fujitsu, acer, samsung and other laptops.

Okay marco@, miod@. Suggestions from kettenis@.
Lots of reviews and help from miod@, thanks!


# 1.138 03-Jun-2009 jordan

Added ejectable bay for IDE cdroms
ok marco@


# 1.137 30-Apr-2009 marco

Oga changed apm ioctls and forgot to grep. Temporary work around by hitting
the sleep button when we get any request. This interface will chnage at
some point.
ok oga


# 1.136 19-Apr-2009 krw

Call acpi_sleep_walk() when powering down just like when entering
any other sleep state. This tells acpi wake devices to not wake up
the box until manual powering up resets their status. Makes my Dell
GX520 stay powered off in response to 'halt -p'.

In snaps for a while.

ok marco@ (several times) deraadt@


# 1.135 17-Apr-2009 pirofti

Add device ranking support for future sleep/wake developments.

okay marco@


# 1.134 11-Mar-2009 jordan

Cleanup.. use aml_evalinteger instead of aml_evalname
ok marco@


# 1.133 10-Mar-2009 jordan

Fix cut/paste error.. call _BFS not _PTS
ok marco@


Revision tags: OPENBSD_4_5_BASE
# 1.132 21-Feb-2009 marco

overzealous cut/paste; found by Vladimir Kirillov <proger@uaoug.org.ua>


# 1.131 20-Feb-2009 marco

Oops didn't mean for Debugger() to remain there.


# 1.130 19-Feb-2009 marco

suspend/resume bits so that we can develop this in tree. This is disabled.
code from mlarkin and me
help from art,toby,jordan and several others
ok jordan, go for it deraadt


# 1.129 10-Feb-2009 jordan

Fix null pointer for shutdown if acpi not enabled
ok marco@


# 1.128 04-Feb-2009 kettenis

Some systems that claim to be ACPI 2.0 apparently don't provide an XSDT
address. Make the code fall back on the RSDT on those systems.

From Joerg Niendorf <p3y80@internode.on.net>. Fixes PR 5986.


# 1.127 17-Jan-2009 krw

A couple of stray malloc/memset -> malloc(M_ZERO), a bit of knf and
lose a couple of unnecessary casts. No functional change.

Feedback from kettenis@

ok kettenis@ marco@


# 1.126 06-Nov-2008 marco

Collapse all sanity checks of hids into one function.

Originaly from jordan


Revision tags: OPENBSD_4_4_BASE
# 1.125 02-Jul-2008 fgsch

add acpivideo. for now almost an empty skel so others can work on
suspend/resume. eventually it will also manage output switching and
brightness where supported. prodded by marco@


# 1.124 11-Jun-2008 marco

Fix $OpenBSD$ for once and for all


# 1.123 08-Jun-2008 deraadt

aaa_dev was being set from an uninitialized variable; ok toby


# 1.122 07-Jun-2008 marco

Kill ACPIVERBOSE. It was never used and is worthless to boot.

comments jmc
ok toby


# 1.121 06-Jun-2008 marco

Shrink acpi some more for floppies and while here re-enable acpi on the
floppies. Fixes crazy sony vaio kettenis has.

tested by deraadt
ok deraadt jordan kettenis


# 1.120 01-Jun-2008 marco

Fix \n nit in SMALL_KERNEL.


# 1.119 01-Jun-2008 marco

Re-enable new parser. Lots of testing by various folks. Fits all media
now too.

ok kettenis deraadt


# 1.118 16-May-2008 dlg

back out jordans new parser.

it didnt have enough oks from the right people, it did break machines
that were previously working, and it bloated the RAMDISKS and install
media beyond the constraints of the media.

requested by deraadt@


# 1.117 14-May-2008 jordan

Adding new ACPI Parser code
Old guts of code still exists, needs to be torn out next
ok marco@
ok brad@


# 1.116 27-Apr-2008 jcs

add acpithinkpad(4), an acpi support driver for ibm/lenovo
thinkpads.

provides functionality that the bios did on older, non-acpi
thinkpads like respond to screen brightness keys and toggle the
bluetooth device on and off.

ok deraadt@ and marco@


# 1.115 24-Apr-2008 jsing

Introduce acpiasus(4), a driver for the ACPI based hotkeys found in many
ASUS laptops (including the ASUS EeePC) - largely based on NetBSD's
asus(4) driver. On the ASUS EeePC this allows us to enable/disable
wireless, change screen brightness and use the volume keys.

ok jsg@, weingart@


# 1.114 12-Apr-2008 kettenis

Get rid of acpi_s5 global variables; simply send SIGUSR2 instead of SUGUSR1
to tell init(8) to power down the machine.

ok krw@


Revision tags: OPENBSD_4_3_BASE
# 1.113 21-Jan-2008 jsg

Check against RESET_REG_SUP in FADT before doing the acpi reset dance
after all, not doing so appears to break more machines than it fixes.

ok marco@


# 1.112 05-Jan-2008 kettenis

Never attach more acpicpu(4) devices than cpu(4) devices. Always attach
the first acpicpu(4) device on non-MULTIPROCESSOR kernels, regardless of its
ACPI processor ID.

ok mikeb@


# 1.111 05-Dec-2007 deraadt

Hang acpi(4) below bios(4) instead of mainbus(4). This lets us move acpi(4)
before pcibios(4), and then.... if acpi(4) attaches, skip pcibios(4) since
it messes the machines up. Fixes claudio's HP dl320 G5, and almost assuredly
others.
ok kettenis


# 1.110 05-Dec-2007 deraadt

repair acpiopen/acpiclose and friends to handle the /dev/apmctl interface,
so that apmd(8) can actually work correctly against acpi(4), and thus,
even do the fancy -C/-A stuff; also tested by jmc


# 1.109 02-Dec-2007 fgsch

fix the trailing space for real now.


# 1.108 02-Dec-2007 fgsch

avoid trailing spaces on output; marco@ ok.


# 1.107 27-Nov-2007 kettenis

Fix off-by-one that made us attach acpicpu0 and acpicpu1 on GENERIC kernels.

ok marco@


# 1.106 25-Nov-2007 jsg

When available and ACPI is enabled use the ACPI reset method
to reboot. This is required for some quirky sis machines
and likely others to reboot.

Tested by many. 'commit early commit often' deraadt@


# 1.105 19-Nov-2007 deraadt

sigh, amd64 still uses that stupid acpi_enabled variable...


# 1.104 19-Nov-2007 deraadt

softc's come initialized to 0, you know...


# 1.103 19-Nov-2007 deraadt

store revision; from jsg


# 1.102 19-Nov-2007 kettenis

Initialize sc_facs earlier.

ok deraadt@, beck@, canacar@


# 1.101 16-Nov-2007 deraadt

fix the bus_space #define nightmare, so that amd64 and i386 are much more
uniform. as a result shared code like acpi needs less #ifdef's
ok marco kettenis


# 1.100 15-Nov-2007 deraadt

remove the #ifdef ACPI_ENABLE. the EC codepath is either safe enough (and
will be improved), or it is unsafe because AML assumes it must be there
ok almost everyone


# 1.99 15-Nov-2007 mikeb

Make acpicpu(4) attach to the existing CPUs only.

Problem reported by form@. Fix idea by kettenis@.
Lots of help from deraadt@. Tested by deraadt@, form@ and me.

ok deraadt marco


# 1.98 14-Nov-2007 deraadt

spacing


# 1.97 12-Nov-2007 deraadt

correct : vs , in attach-time messages


# 1.96 06-Nov-2007 deraadt

all ACPI_ENABLE kernels need acpi_interrupt()


# 1.95 06-Nov-2007 deraadt

move acpi_read_pmreg() and acpi_write_pmreg() out of SMALL_KERNEL so
that acpi ramdisks can compile (obvious solution)


# 1.94 05-Nov-2007 canacar

Establish the acpi interrupt earlier. Fixes problems with some
GENERIC.MP kernels not getting acpi interrupts. This is a
workaround until the real issue with late establishment is fixed.
ok weingart@, beck@, gwk@


# 1.93 03-Nov-2007 jordan

Added support for displaying wakeup devices
ok beck@,weingart@,gwk@


# 1.92 11-Oct-2007 kettenis

When walking the tree yo find _PRT methods, check whether the device is
actually enabled and functioning. Similar to what we do for _INI.

ok weingart@, beck@


# 1.91 08-Oct-2007 krw

More simple memset(,0,) -> M_ZERO changes. In this batch move to
size(*p) as the first malloc() parameter where p is declared locally
and thus easy to check. Add M_ZERO to gpe_table allocation in acpi.c
even though there is no obvious bzero or memset nearby.


# 1.90 13-Sep-2007 mk

Remove unnecessary memset() (see aml_evalnode(9)) and a bit of KNF.

ok toby


# 1.89 13-Sep-2007 weingart

Implement ACPI 6.5.1 spec tree walk for _STA and _INI.
Fixes mk's laptop. No regressions so far (thank you to the
testers).

ok gwk@, mk@, marco@


Revision tags: OPENBSD_4_2_BASE
# 1.88 17-Apr-2007 mk

Now that acpidock no longer hangs machines during boot (at least
according to the reports I've been getting) it should be safe to enable
it again, so remove the #if 0 stuff around it.

ok marco


# 1.87 11-Apr-2007 jordan

Added changes for C-State
ok marco@


# 1.86 26-Mar-2007 marco

Calculate minutes left more accurately.

From giovanni <qgiovanni@gmail.com>


Revision tags: OPENBSD_4_1_BASE
# 1.85 03-Mar-2007 mk

Stop looking for acpidock until we fix that stuff that breaks all pre-60
series ThinkPads with acpidock(4) enabled.


# 1.84 22-Feb-2007 marco

We were getting no interrupts in .MP. There are 2 fixes; one in
acpi_machdep.c and move the interrupt establishment down after ioapic
has been initialized.

ok jordan


# 1.83 21-Feb-2007 kettenis

Make an acpi-enabled GENERIC.MP work on systems that only support PIC mode.

ok marco@


# 1.82 19-Feb-2007 jordan

Added acpi_map_address function for mapping GAS/IO registers
ok marco@


# 1.81 18-Feb-2007 jordan

Added dynamic perfect hash for opcodes
ok marco@


# 1.80 18-Feb-2007 marco

Shave off another K pre compression.
Shuffle SMALL_KERNEL functions around so that we only have a single #ifndef


# 1.79 17-Feb-2007 marco

Add SMALL_KERNEL throught acpi to make it fit on boot media.
ok deraadt


# 1.78 13-Feb-2007 marco

Add smarter _INI code. This helps with some dissapearing devices.

ok kettenis jordan


# 1.77 31-Jan-2007 gwk

Introduce acpi_hasprocfvs thats "ACPI Has Processor Frequency and Voltage
Scalling" a global flag set by acicpu attach to indicate that ACPI is
handling fvs through the _PSS objects. This will fix disappearing
EST/powernow when booting kernels with acpi enabled, as acpicpu isnt
compiled into GENERIC yet. Also in cases where acpicpu is built in and
legacy EST/powernow work but acpi is missing the _PSS object, the legacy
routines will be used.

tested by dim@, and Nick Nauwelaerts <nick AT nauwelaerts.net>
ok marco@, dim@


# 1.76 22-Jan-2007 mk

Actually look for docking stations by looking for _DCK nodes.

ok marco


# 1.75 02-Jan-2007 marco

On some systems EC must be attached as the first device. Move EC from
generic _HID devices to its own routine to force first attach. Bug found
and debugged by joris@

Code mostly written by joris@


# 1.74 26-Dec-2006 marco

Add polling to devices that require it such as acpibat and acpitz.
Use the same tsleep/wakeup mechanism as the interrupt code to keep it non-
concurrent.


# 1.73 21-Dec-2006 deraadt

even more knf love


# 1.72 21-Dec-2006 marco

Remove some leftover acpiec gpe/interrupt code.


# 1.71 20-Dec-2006 deraadt

oops, name already used


# 1.70 20-Dec-2006 deraadt

rename __acpi_enable_gpe(); ok marco


# 1.69 20-Dec-2006 deraadt

acpi_unmap_pmregs() is not used, ok marco


# 1.68 19-Dec-2006 deraadt

simplify some low-level methods substantially (shrinking code); ok marco jordan


# 1.67 06-Dec-2006 mk

malloc(..., M_NOWAIT) with no NULL check is bad. If allocation fails,
complain, clean up, and bail out of attach.

input and ok gwk


# 1.66 28-Nov-2006 jordan

Fixed clearing arg for acpi_init_pic
ok kettenis@


# 1.65 27-Nov-2006 kettenis

Don't spam dmesg.


# 1.64 27-Nov-2006 jordan

Fixes for acpiprt routing
Post-parse fixup for namerefs
ok kettenis@


# 1.63 25-Nov-2006 marco

Don't handle acpiev events inside the gpe handler since this causes deadlocks.

ok jordan@


# 1.62 15-Nov-2006 kettenis

Hook up acpi apic interrupt routing support to generic acpi code.

ok jordan@


# 1.61 02-Nov-2006 marco

Kill acpiec attach function and add its functionality to the generic _HID
device attach

prodded and ok jordan


# 1.60 02-Nov-2006 marco

Fix comments to reflect reality.


# 1.59 19-Oct-2006 marco

Replace ACPI ioctls with APM ioctls. This makes ACPI talk the same protocol
as APM therefore userspace can now retrieve power information in the same
fashion as all other apm arches (zaurus, macppc, i386 etc).

To use force the use of acpi device like so: apmd -f /dev/acpi

ok jordan@


# 1.58 19-Oct-2006 jordan

Fixed GPE debug levels
Fixed ref counter on temporary bufferfields
DSDT parsing fully working on IBM Thinkpad
ok marco@


# 1.57 15-Oct-2006 jordan

Removed old GPE handler code remnants
ok marco@


# 1.56 12-Oct-2006 jordan

Massive ACPI changes
Fixed ACPI leaks
new ACPI API
ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.55 30-Jun-2006 gwk

KNF, No functional change,
"go at it" marco@


# 1.54 30-Jun-2006 jordan

Support for calling Embedded Controller via new GPE interface
ok marco@


# 1.53 30-Jun-2006 jordan

Added new code for GPE handling
Also works with ACPIEC; need to fixup acpiec.c to remove original interrupt handler
ok marco@


# 1.52 30-Jun-2006 jordan

Added offset argument for acpi read/write pmreg routines (new GPE code)
ok marco@


# 1.51 31-May-2006 todd

do not call acpi_powerdown() unless acpi has successfully initialized
ok gwk@ canacar@


# 1.50 29-May-2006 canacar

Add embedded controller support.
ok marco@ deraadt@ testing & ok gwk@


# 1.49 19-May-2006 canacar

Minimal thermal zone driver. Monitors thermal zone temperature,
shuts down the system if the 'critical temperature' is reached.
ok marco@


# 1.48 11-Apr-2006 marco

KNF.


# 1.47 11-Apr-2006 gwk

Add acpi_resume(struct acpi_softc *sc), which calls some AML methods,
detected at attach time, currently unused.

ok marco@


# 1.46 11-Apr-2006 gwk

Discover some ACPI sleep related methods, and call them at the
appropriate time in acpi_enter_sleep_state().

ok marco@


Revision tags: OPENBSD_3_9_BASE
# 1.45 26-Feb-2006 marco

Add acpicpu as a device.

ok jordan@


# 1.44 23-Feb-2006 marco

remove dead function.

ok jordan@


# 1.43 22-Feb-2006 marco

Print out all tables it found during autoconf. We'll need this data in
the future.


# 1.42 22-Feb-2006 marco

Make dmesg less verbose.

ok jordan.


# 1.41 22-Feb-2006 jordan

Added parsing of SSDT tables
ok marco@


# 1.40 22-Feb-2006 jordan

Added new aml_register to notify by device id
Moved powerdown code to button device
ok marco@


# 1.39 21-Feb-2006 marco

Readjust debug levels that crept in too low over the last few days.
KNF


# 1.38 21-Feb-2006 marco

Rig the power button so that the machine shuts down correctly.


# 1.37 20-Feb-2006 marco

Add acpibtn device. Remains disabled for now.


# 1.36 19-Feb-2006 jordan

Added code for aml_notify GPE notification callback
ok marco@


# 1.35 19-Feb-2006 grange

Fix acpi_enter_sleep_state() by replacing current ugly hack
with mostly correct sleeping state transition code.

ok brad@ marco@


# 1.34 19-Feb-2006 marco

jordan:
Enable interrupts.
Added peripheral code to make this work.

marco:
Replace softintr and other types of interrupts with a handler with context.
Fixed interrupt magic for GPEs.
Some KNF

This is work done by jordan and I but has to be a single commit.


# 1.33 17-Feb-2006 marco

Remove earlier introduced delays. We really only need them during autconf.

ok jordan@


# 1.32 17-Feb-2006 marco

Add a generic delay function as per the ACPI spec.

from jordan@


# 1.31 17-Feb-2006 marco

Use tsleep instead of delay when !cold.


# 1.30 17-Feb-2006 marco

Argh, we need delays between reads/writes. The backend reads (i2c most likely)
are so slow that values come back as 0 sometimes. We have to do this
despite the spec saying we shouldnt have to. Guess having a faster
parser than others can hurt sometimes.

Idea from jordan.


# 1.29 17-Feb-2006 jordan

Fixed code for handling _INI
Rearranged debug statements
ok marco@


# 1.28 16-Feb-2006 jordan

Added stub for calling _INI
Fixed aml_dowhile for NULL return
ok marco@


# 1.27 16-Feb-2006 jordan

Fixed parser/evaluator to retrieve battery info+state
Cleanup of parser
Dell laptops return battery status
ok marco@


# 1.26 06-Feb-2006 brad

fix a typo so this will compile on amd64.


# 1.25 04-Feb-2006 marco

When ACPI_ENABLED wasn't set but the devices were it had an unknown variable.

Pointed out by ian@.


# 1.24 03-Feb-2006 jordan

Rewrote aml parser yet again...
Fixed bus_space_map for i386/amd64
Added _BST and _BIF to acpibat to get status/info
ok marco@


# 1.23 20-Jan-2006 jordan

More evaluator changes
Removed old evaluator code
Fixed _HID value
ok marco@


# 1.22 19-Jan-2006 jordan

Added support for read/write of PCI Config space
Help jason@
ok marco@


# 1.21 18-Jan-2006 jordan

Added new evaluation routines
Added helper methods for buffer/field evaluation
Fixed broken indentation on dsdt.c

ok marco@


# 1.20 17-Jan-2006 jordan

Major overhaul of the aml parser. Evaluater temporarily disabled.

ok marco@


# 1.19 06-Jan-2006 grange

Check for ACPI control availability only if ACPI_ENABLE defined.


# 1.18 06-Jan-2006 grange

Informative error messages on attach, prettier dmesg.


# 1.17 06-Jan-2006 grange

printf -> dnprintf conversion for debug messages.


# 1.16 05-Jan-2006 grange

Find SLP_TYPx values and store them for future use in
acpi_enter_sleep_state().


# 1.15 05-Jan-2006 grange

Random KNF and cleanup


# 1.14 05-Jan-2006 grange

Provide symbolic names for the acpi sleeping states and use it.


# 1.13 28-Dec-2005 marco

Moved DEVNAME to a acpidev.h

ok jordan@


# 1.12 18-Dec-2005 sturm

add back comment about wrong code

ok marco


# 1.11 16-Dec-2005 marco

Remove test for kbd_reset. Prompted deraadt@, ok jordan@


# 1.10 16-Dec-2005 jordan

Added hooks for powerdown/power button press


# 1.9 16-Dec-2005 jordan

Cleaned up common power management register i/o
Added enter sleep state ioctl
Hooked power button interrupt
ok marco@


# 1.8 16-Dec-2005 marco

Remove autoconf chatter


# 1.7 16-Dec-2005 jordan

Moved DSDT AML parsing into parent ACPI driver
ok marco@


# 1.6 14-Dec-2005 marco

Add AC device glue.


# 1.5 14-Dec-2005 marco

Adjust debug printing levels to something more sane.


# 1.4 13-Dec-2005 marco

Add glue to attach devices to apci.


# 1.3 07-Dec-2005 marco

KNF


Revision tags: OPENBSD_3_8_BASE
# 1.2 10-Jul-2005 grange

Spacing cleanup.


# 1.1 02-Jun-2005 tholo

Start on a basic ACPI framework -- does not do much more than read out the
ACPI tables into kernel memory and attach ACPI and HPET timers currently.

In order to test this code, enabling the devices in GENERIC as well as
the ACPI_ENABLE option is needed. This code does not do any thermal
control yet, so this should be done with care depending on the platform.

In the tree so more people can contribute to making this more fully
featured.

Ok niklas@ grange@ tedu@


# 1.335 29-Nov-2017 kettenis

Add support for GPIO-signaled events.

ok mlarkin@


# 1.334 16-Nov-2017 jcs

Add PCI attachment for dwiic(4) needed by Intel 100 Series machines

ok kettenis


# 1.333 14-Nov-2017 kettenis

Add a few more PNP IDs to the skip lists.

ok deraadt@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.332 17-Aug-2017 stsp

Make dwiic(4) attach its ACPI parent dependencies.
This change makes the Asus E200HA keyboard work.

Original analysis and patch by Cesare Gargano
ok kettenis@


# 1.331 09-Aug-2017 dcoppa

Add "PNP0303" (8042 PS/2 Controller) to acpi_isa_hids[]

ok kettenis@


# 1.330 22-Jul-2017 jcs

acpibat: look for _BIX first before falling back on _BIF

ACPI 4.0 deprecated _BIF for battery status so some newer machines
are exporting _BIX which extends _BIF's package with a few extra
fields. When using _BIX, export the new discharge cycle count as a
sensor.

feedback from kettenis and anton
ok deraadt


# 1.329 20-Jul-2017 kettenis

Start a list of isa(4) device names such that we can avoid printing them
as "not configured".

ok mikeb@, millert@, deraadt@


# 1.328 08-Apr-2017 deraadt

Sensors are run as callbacks inside tasks. During suspend a sensor
could be running inside a driver that will be force-detached, or due
to tsleep end up disrupting the softstate/hardstate contract. At
suspend time, quisce all these callbacks by waiting for completion.
This issue has never been observed for real, but may be implicated in
suspend/resume failures.
ok kettenis guenther mlarkin


# 1.327 08-Apr-2017 deraadt

Two prototypes for acpi_maptable; merge to one place


Revision tags: OPENBSD_6_1_BASE
# 1.326 27-Mar-2017 deraadt

Now that hibernate_alloc() only has clean success/failure, don't
need to call hibernate_free() to clean up a partial mess.
ok mlarkin kettenis


# 1.325 13-Mar-2017 deraadt

allow ramdisks to build; ok jcs


# 1.324 12-Mar-2017 jcs

try to attach acpisbs first and if successful, don't attach acpibat


# 1.323 02-Mar-2017 natano

Add a new sysctl machdep.lidaction. The sysctl works as follows:

machdep.lidaction=0 # do nothing
machdep.lidaction=1 # suspend
machdep.lidaction=2 # hibernate

lidsuspend is just an alias for lidaction, so if you change one, the
other one will have the same value. The plan is to remove
machdep.lidsuspend eventually when people have upgraded their
/ets/sysctl.conf.

discussed with deraadt, who came up with the new MIB name
no objections mlarkin
ok stsp halex jcs


# 1.322 28-Feb-2017 natano

Untangle abstract sleep modes from ACPI sleep states. This paves the way
for further work in the area.

idea and ok deraadt
ok mlarkin


# 1.321 22-Feb-2017 jcs

add acpisbs, an acpi smart battery subsystem driver reading data
over smbus

currently disabled because it conflicts with acpibat

ok deraadt, kettenis


# 1.320 14-Jan-2017 kettenis

Revert aml_rdpciaddr changes; breaks several machines that were working
before.


# 1.319 11-Jan-2017 jsg

Increase the size of the buffers used for _HID and _CID strings to be
able to fit "Hyper_V_Gen_Counter_V1".

ok mlarkin@ deraadt@


# 1.318 08-Jan-2017 kettenis

Make aml_rdpciaddr() work for for devices behind bridges. The code now
uses the mapping between AML nodes and PCI devices that we establish.
Because _INIT methods may end up calling aml_rdpciaddr(), make sure we
create that mapping early. Also handle devices that aren't actually present.
These devices are now included in the mapping and reads will return an
all-ones patterm whereas writes are a no-op..

ok mlarkin@


# 1.317 25-Oct-2016 pirofti

Add the resource index to aml_parse_resource() callback function.

This is needed by an upcoming acpiec commit that handles machines breaking
the current ACPI specifications.

Change suggested by and ok kettenis@, guenther@


# 1.316 18-Sep-2016 guenther

If the APIC table (MADT) isn't present then ci_acpi_proc_id won't be set
when acpicpu tries to attach, leading to a NULL dereference. The ACPI
processor object on the Dells where this was reported is bogus and useless
anyway so apply the same test when deciding whether to attach to processor
objects as acpicpu would when matching them to cpus.

Problem reported by Eivind Eide (xenofil (at) gmail.com) and
Johan Huldtgren (johan+openbsd-bugs (at) huldtgren.com).
Testing by Eivind Eide.
ok kettenis@ mlarkin@


# 1.315 03-Sep-2016 naddy

Write the system time back to the RTC every 30 minutes.
This fixes the problem that long-running machines which were not
shut down properly would reboot with a badly offset system time.

hints and ok kettenis@


# 1.314 31-Aug-2016 mlarkin

Don't put a device in the wake device list if _STA says it's not there.

Fixes at least one machine that woke up from sleep / poweroff immediately.

ok kettenis@, tested by kettenis and martijn@


# 1.313 28-Jul-2016 kettenis

Store the acpi processor ID/UID in struct cpu_info, and use it to attach
acpicpu(4) drivers to the right cpu(4).

ok mlarkin@, guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.312 10-Jun-2016 kettenis

The power management timer has been made optional in ACPI 5.0A, so don't
attempt to attach acpitimer(4) if the timer isn't present.

Pointed out by jcs@

ok jcs@


# 1.311 08-May-2016 kettenis

Make sure devices listed by a device's _DEP method are attached before
we attach the device itself. The _DEP methode, introduced in ACPI 5.0,
is a hint that the device in question depends on OpRegion support from
the devices listed by _DEP, so we have to order them the proper way.
To prevent us from attaching those devices again when we encounter them
later walking down the device tree, keep track of our attempts to
attach a device driver to them using aflag in the aml_node structure.

ok guenther@


# 1.310 25-Apr-2016 pirofti

We attach acpihpet(4) and acpiec(4) without using the HID, so do not
report it in the dmesg.

OK deraadt@, mlarkin@.


# 1.309 02-Apr-2016 kettenis

Reduce dmesg spam by knocking out some acpi hardware IDs that don't need a
driver.

ok mpi@


# 1.308 29-Mar-2016 kettenis

Check if a device is present (using _STA) in acpi_foundhid. This prevents
us from reporting devices as "not configured" that aren't actually present,
and allows us to remove duplicated code from several drivers.

ok deraadt@, ok jsg@


# 1.307 29-Mar-2016 kettenis

Implement a cleaner way to prevent acpivout(4) from attaching if we have
a platform-specific method of controlling the backlight.

ok millert@, mlarkin@, jung@


# 1.306 28-Mar-2016 kettenis

Adding each and every new acpi device driver to acpi_foundhid() is
gettinga bit of the burden. And it really isn't how our autoconf
framework is supposed to work for busses that can be enumerated. All
the drivers already check for a matching _HID in their attach
function. So we can just drop the checks in acpi_foundhid() and let
the drivers sort themselves out.

Print a "not configured" line for devices for which we don't attach adriver.
This may be a bit much as there are devices for which we will not have a
driver. This will be removed if it causes too much dmesg spam.

ok deraadt@, mlarkin@


Revision tags: OPENBSD_5_9_BASE
# 1.305 17-Jan-2016 jsg

Take acpi_foundhid() outside of #ifndef SMALL_KERNEL so the ramdisk
kernel can find and attach dwiic(4) and sdhc(4).


# 1.304 17-Jan-2016 jsg

remove an unused global variable


# 1.303 14-Jan-2016 kettenis

Attach dwiic(4) on the Intel Bay Trail i2c controllers as well.


# 1.302 12-Jan-2016 kettenis

Improve emulation of PM registers on Hardware-redcued ACPI by emulating
SPL_TYPx, SLP_EN and WAK_STS using the new SLEEP_STATUS_REG and
SLEEP_CONTROL_REG registers. Makes the transition into S5 (powerdown) work
on the ASUS X205TA.

ok deraadt@, mlarkin@


# 1.301 12-Jan-2016 jcs

Add dwiic, a driver for the Synopsys DesignWare i2c controller found
on the Samsung ATIV Book 9 laptop. This initial version only
supports ACPI config/attachment.

Add ihidev, a HID-over-i2c driver largely based on uhidev. dwiic
handles attaching ihidev devices found in ACPI.

Add ims, a HID-over-i2c mouse/trackpad driver to get basic cursor
and button functionality from HID-compliant i2c trackpads.

ok kettenis deraadt


# 1.300 11-Jan-2016 kettenis

Make sdhc(4) attach to hardware IDs "80860F14" and "PNP0FFF".


# 1.299 09-Jan-2016 kettenis

Initial support for Hardware-Reduced ACPI. This skips the legacy mode checks
by emulating the PM_CNT register such that SCI_EN is always enabled. It also
skip attaching acpitimer(4) since the relevant registers are no longer there.

ok deraadt@, mlarkin@


# 1.298 06-Jan-2016 kettenis

Follow the spec and check the SCI_EN bit to see if ACPI has already
been enabled and don't bother enabling it again if it is. This should fix
systems that don't implement legacy mode and therefore set ACPI_ENABLE and
ACPI_DISABLE to zero.

ok mlarkin@, yasuoka@


# 1.297 23-Nov-2015 reyk

Back out the CRITICAL change, turn it back from 10% to 25%. Some
people with old/broken batteries didn't like this change, and the
underlying heuristics are wrong.


# 1.296 22-Nov-2015 reyk

The "CRITICAL" APM battery level, that is exported/faked by the ACPI
stack, is currently set to 25%. With 25% battery left, a typical
modern laptop will have at least one hour runtime left. It is an
arbitrary value, and other APM power layers in the kernel set CRITICAL
to something like 5% or 10%, so set it to 10% for now.

OK mlarkin@


# 1.295 28-Sep-2015 deraadt

In low-level suspend routines, set cold=2. In tsleep(), use this to
spit out a ddb trace to console. This should allow us to find suspend
or resume routines which break the rules. It depends on the console
output function being non-sleeping.... but that's another codepath which
should try to be safe when cold is set.
ok kettenis


# 1.294 06-Sep-2015 deraadt

clarify malloc arguments (base + length...)


# 1.293 28-Aug-2015 deraadt

fairly simple sizes for free(); ok tedu


# 1.292 20-Aug-2015 kettenis

Respect the access size when reading or writing to pci config space.
This version also makes sure writes are properly aligned, preventing
the panics seen with the previous incarnation of this change.

Fixes battery status passthrough in vmware, brightness keys on my x220
and who knows what else.

ok mlarkin@, deraadt@


Revision tags: OPENBSD_5_8_BASE
# 1.291 04-Aug-2015 deraadt

If we are setting ACPI_PM1_BM_RLD do it only once, and remember to
repeat this action at resume time.
ok mlarkin guenther


# 1.290 29-Jul-2015 deraadt

Something subtle in previous revision is locking up suspend or booting
on some laptops. Mailing lists threads mention x220 and Asus machines.
Reverting for now.


# 1.289 21-Jul-2015 kettenis

iRespect the access size when reading or writing to pci config space.
Fixes battery status passthrough in vmware and who knows what else.

Tested by many.

ok mlarkin@, deraadt@


# 1.288 17-Jul-2015 mlarkin

fix comments


# 1.287 26-Mar-2015 jsg

Use the 0x10 extended capability to determine if the trackstick
buttons are wired to the trackpad and need to be re-routed to the
trackstick. Without this change the buttons on 2015 thinkpads
get picked up as extended buttons that show up as scroll up/down.

Remove the X1 Carbon 2015 (LEN0048) and X250 (LEN0046) from the
top button area/soft buttons quirks list. Also avoid using the
quirk list entirely if the capability bit is set.

Based on work from jcs@ with feedback from mpi@
ok mpi@ jcs@


# 1.286 20-Mar-2015 kettenis

The ACPI standard says that a PCI function number of FFFF can be used to refer
to all functions on a device. This makes no sense in the context of accessing
PCI config space. Nevertheless stsp@ has a machine with AML that tries to
do config space reads from a device where _ADR encodes function number FFFF.
This is almost certainly buggy AML, but since Linux (and presumably Windows)
boot just fine on this machine we need to work around this. Linux effectively
treats function FFFF as function 7, which isn't present on this machine.
Instead we simulate a read from a non-existent device by returning a bit
pattern of all ones. Writes to function FFFF will still panic.

ok mlarkin@


# 1.285 16-Mar-2015 deraadt

Reduce use of SMALL_KERNEL in acpi, bringing acpiec driver and more gpe
handling into RAMDISK. This is now possible because the install media
has ample room. The goal is to reduce special cases where we may be
skipping (unknown) important operations...
ok mlarkin kettenis


# 1.284 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.283 07-Feb-2015 deraadt

New framework that allows hibernate to pass in entropy from it's fresh
boot.
ok mlarkin


# 1.282 06-Feb-2015 mlarkin

Fix a hibernate crash on some machines due to unmapping a page that
may not have been mapped previously (in the failure to hibernate case).

Also ensure that the lowmem ptp is mapped in all cases (not just MP).

ok kettenis


# 1.281 17-Jan-2015 deraadt

resume_randomness() before spinning up other cpus...


# 1.280 15-Jan-2015 jsg

Define a new wscons mouse type for Synaptics clickpad devices that lack
physical buttons. This will be used if the acpi pnpid for the mouse
matches a list provided by Synaptics found in Linux.

Combined with the xenocara changes this will remove the need for an xorg
config file for the x240/t440/t540 generation of thinkpads.

ok matthieu@ shadchin@ kettenis@


# 1.279 11-Jan-2015 kettenis

Tolerate checksum errors in ACPI tables (but complain about them). Apparently
there are machines out there with broken BIOSen with such checksum errors in
thier RSDT tables. Windows runs fine on these machines, so it probably does
not check. Linux ignores checksum errors as well (and prints a warning).

ok deraadt@, krw@


# 1.278 18-Dec-2014 deraadt

suspend and resume the RNG (at what looks like the right spots)
ok reyk djm


# 1.277 09-Dec-2014 doug

Replace some malloc(n*size,...) calls with mallocarray().

ok tedu@ deraadt@


# 1.276 06-Dec-2014 mpi

If "machdep.lidsuspend" is not set, do not go back to sleep if the lid
is still closed when resuming. This allows docked laptop to be resumed
without opening them.

ok mlarkin@


# 1.275 23-Nov-2014 mlarkin

If a machine has no _LIDs, then none can be open. This diff fixes a problem
where machines without any _LIDs were going back to sleep after hibernate
due to the recent suspend-if-lid-closed-on-resume diff. Noticed on qemu,
which doesn't have any _LIDs.

discussed with deraadt


# 1.274 18-Nov-2014 krw

Nuke yet more obvious #include duplications.

ok miod@


# 1.273 08-Nov-2014 mlarkin

If resuming from sleep (zzz/ZZZ) and the lid is still closed, go back to
sleep. This prevents accidental lid flex or slight opening in a backpack
from waking the machine up and leaving it resumed (powered on).

ok deraadt@


# 1.272 26-Sep-2014 kettenis

Rework piglet and pig allocation. Currently the piglet gets allocated
deep down in the suspend path, where it is really hard to recover from
allocation failure. So allocate the piglet early on in the suspend path.
Also change the piglet and piglet allocation functions to use km_alloc(9)
instead of doing pmemrange magic. This removes a bunch of code which, in the
case of the piglet allocation, is broken since it results in a NULL pointer
dereference. Also switch the piglet allocation to not wait. If we can't
allocate 16MB of phys contig memory on a halfway modern machine we're almost
certainly under a lot of memory pressure and we're better off not trying to
hibernate anyway.

ok mlarkin@


# 1.271 25-Sep-2014 kettenis

Bring back rev 1.249. Now that mp_setperf() has been fixed, it should be
safe to restore perflevel when resuming. Should fix the issues people
see with the processor running at full speed heating up the machine while
the apm command reports that the CPUs are running at a low speed.

ok dcoppa@, deraadt@


# 1.270 23-Sep-2014 kettenis

Release the acpi lock around calls to wsdisplay_suspend() and
wsdisplay_resume(). The X server may adjust the display brightness just
before/after the VT switch when suspending/resuming. In fact, it may
already be blocked adjusting the display brightness for some reason
unrelated to the suspend request.

The VT switch request will eventually time out, but the delay is annoying
and the failed VT switch will lead to problems after resume.

Needed for a future update of the xf86-video-intel driver.

ok mlarkin@


# 1.269 19-Sep-2014 kettenis

Use config_suspend_all(9).


# 1.268 08-Sep-2014 kettenis

Clear the PME Status bit on PCI devices in response to Device Wake events.
Fixes the interrupt storm on the 2009 Mac mini with WOL enabled on its
nfe(4) interface.

ok mlarkin@


Revision tags: OPENBSD_5_6_BASE
# 1.267 20-Jul-2014 mlarkin

Support hibernating to softraid crypto volumes.

much help and ok from deraadt@


# 1.266 16-Jul-2014 mlarkin

Reenable hibernate RLE support and flush+zero all memory after unpack.


# 1.265 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.264 12-Jul-2014 kettenis

Add a function to drop all clean pages on the page daemon queues and call
it when we hibernate.

ok mlarkin@, miod@, deraadt@


# 1.263 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.262 11-Jul-2014 mlarkin

Flush the buffercache to 16MB on hibernate and restore its previous max
size (kern.bufcachepercent) on resume, for better hibernate performance.

ok beck@


# 1.261 10-Jul-2014 blambert

remove unneeded header

ok mlarkin@


# 1.260 01-Jul-2014 kettenis

Adding an additonal delay after each access to SystemMemory or SystemIO
during autoconf makes no sense.

ok deraadt@


# 1.259 23-Jun-2014 kettenis

SystemMemory is used to access memory mapped registers on some machines, so we
must use the bus_space(9) API to access it instead of memcpy(9). Also make
sure we properly align access to fields that need more than byte alignment.

ok guenther@


# 1.258 21-May-2014 mlarkin

Fix invalid / missing format specifiers in several dnprintf statements
(used only when ACPI_DEBUG is enabled)

From a diff on tech@ by Fabian Raetz, with a minor change by me, tested
on i386 and amd64 with ACPI_DEBUG enabled and disabled.


# 1.257 25-Apr-2014 mlarkin

Defer the free of the hibernate structures until later, when it is safe to
do so.

Freeing the hibernate structures too early leads to inadvertent calls into
splx when we aren't ready for them yet. Found when trapping splx calls in
the resume path.

discussed with deraadt@ at m2k14


# 1.256 13-Apr-2014 mlarkin

Fix a badly worded debug message that implied the entire machine was going
to sleep, instead of just the acpi thread going to sleep.


# 1.255 31-Mar-2014 mpi

Change the order of operations during a suspend/resume cycle and
call bufq_quiesce() after executing the DVACT_QUIESCE handlers.

This should be safe since no disk nor controller drivers have such
handler but it will allow us to detach sd(4) devices attached to a
USB bus.

Another benefit pointed out by kettenis@ is that drivers that need
to read a firmware from the disk should be able to do it at resume
time in a DVACT_WAKEUP handler.

ok kettenis@, deraadt@


# 1.254 13-Mar-2014 dlg

get rid of the assumption that the head of the alldevs list is the
"mainbus" device. this breaks when mpath is enabled because it
attaches before mainbus and therefore takes the head position.

have autoconf provide device_mainbus() which looks up mainbus_cd,
and use that instead.

discussed with deraadt who just wants mpath stuff to move forward
despite there being many ways to shine this particular turd.


Revision tags: OPENBSD_5_5_BASE
# 1.253 21-Feb-2014 deraadt

Do the lid control further on in the resume sequence, since this is
AML, and will do WAITOK operations. While cold, that is a bad idea.
We seem to be safe with respect to the spec, I do not see any event
debounce issues here.
ok kettenis, tested by many


# 1.252 20-Jan-2014 claudio

Instead of checking sc->sc_revision <= 1 check that the
fadt->hdr_revision <= 1. This is a better check since it checks
the version of the proper descriptor. This allows the alix APU
to reboot since those boards have currently a wrong sc->sc_revision.
Committing this now so that it can be tested easier.
With and OK jsg@ also fine by derradt@


# 1.251 24-Dec-2013 kettenis

If the FADT has its SMI_CMD set to zero, assume we're only ACPI-only hardware
and don't need to disable SMI ownership of the ACPI hardware registers.

ok mlarkin@


# 1.250 23-Dec-2013 kettenis

Revert previous commit. It makes resume hang on some MP systems running
GENERIC.MP.


# 1.249 16-Dec-2013 kettenis

Restore hw.setperf upon resume like we do for apm(4) on i386. Seems we need
to do this fairly late in the resume to avoid hangs.

ok deraadt@, mlarkin@


# 1.248 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


# 1.247 06-Nov-2013 mpi

Add support for Power Resources for Dx states and the necessary hook
for PCI devices. This hook should be called twice, before and after
changing the power state of a PCI device.

Before setting the device to the new state, the ACPI layer will notify
every power resources linked to the device for that state and make sure
they are turned "_ON". After changing the state of the device, it will
decrement the reference of every power resources linked to that device
for the old state and turn them "_OFF" if they are no longer referenced.

This fixes the no-USB after resume problem seen on various ThinkPad,
problem initialy diagnosed with Alexander Polakov.

ok kettenis@, deraadt@


Revision tags: OPENBSD_5_4_BASE
# 1.246 01-Jun-2013 mlarkin

Stop printing ACPI wakeup devices in dmesg after the 16th wakeup device.

Helps with VMware VMs that report hundreds of wakeup devices.

ok reyk@, deraadt@


# 1.245 31-May-2013 bluhm

Fix the build for a kernel without wd and pciide in its config.
OK deraadt@ tedu@


# 1.244 30-May-2013 deraadt

Enforce ca_activate tree-walks over the entire heirarchy for all events,
cleaning up some shutdown-hook related code on the way.
(A few drivers related to sparc are still skipped at kettenis' request)
ok kettenis mlarkin, tested by many others too


# 1.243 18-Apr-2013 deraadt

pin the acpi thread to the BSP, because AML and SMI cannot be trusted. I
was certain we were doing this already, but it turns out no...


Revision tags: OPENBSD_5_3_BASE
# 1.242 24-Dec-2012 guenther

In acpiioctl(), Return though the splx() path if suser() returns an error

ok deraadt@


# 1.241 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


# 1.240 04-Oct-2012 ehrhardt

Use information provided by ACPI to attach secondary PCI host bridges.

ok kettenis@


# 1.239 07-Sep-2012 kettenis

Add a new function called acpi_pci_min_powerstate() that returns the lowest
power state for a PCI device for the power state (currently S3 or S4) we're
transitioning into. This function is probably too simplistic; there is a lot
of (silly) complexity in the ACPI spec.

ok deraadt@


Revision tags: OPENBSD_5_2_BASE
# 1.238 13-Jul-2012 pirofti

Disable acpivideo(4) on ASUS laptops.

There are some models that panic whenever an event is received.
Some cope, some don't. In order to get this right acpiec(4) requires
major rework. Until then let machines boot.

Discussed with deraadt@.


# 1.237 11-Jul-2012 mlarkin

for now, restrict hibernate to root in the acpi ioctl kernel interface.
normal rules for controlling this interface through apmd still apply.

ok deraadt@


# 1.236 09-Jul-2012 deraadt

tiny little oops in untested hibernate block


# 1.235 09-Jul-2012 deraadt

clean up ioctl() case statements


# 1.234 09-Jul-2012 deraadt

acpi_disable_onegpe() is not used; ok pirofti


# 1.233 24-May-2012 kettenis

If _MIN > _MAX consider a WordBus resource to be invalid. Fixes a panic on
the IBM x3550-M4.

ok mikeb@


# 1.232 29-Mar-2012 mlarkin

Missing #ifdef in previous diff


# 1.231 29-Mar-2012 mlarkin

Prevent hibernation attempts on machines with unsupported disk controllers.
The same check is also performed later in the hibernate sequence, but by
then it's too late to cleanly unwind (presently).

ok deraadt


# 1.230 26-Mar-2012 deraadt

simplify conditionals in acpi_sleep_state()
ok mlarkin


# 1.229 26-Mar-2012 deraadt

hook in the hibernate request code; half of this diff is from mlarkin
ok mlarkin


Revision tags: OPENBSD_5_1_BASE
# 1.228 20-Sep-2011 deraadt

Very early in suspend while the clock is still ticking realtime,
save the clock back to the rtc. The zaurus already did this.
ok phessler miod kettenis


Revision tags: OPENBSD_5_0_BASE
# 1.227 02-Jul-2011 nicm

kqueue attach functions should return an errno or 0, not a plain 1. Fix
the obvious cases to return EINVAL and ENXIO.

ok tedu deraadt


# 1.226 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.225 16-Jun-2011 pirofti

Import acpitoshiba driver.

This is originally written by Hiroyuki Aizu, ported and integrated
in our tree by Javier Vazquez <javama4 AT gmail> with tweaks and
suggestions by me.

Import prodded by deraadt@ and the lack of testers on tech@.


# 1.224 27-Apr-2011 jcs

attach acpithinkpad to newer lenovo models like the x120e

ok deraadt@


# 1.223 22-Apr-2011 jordan

Fix uninitialzied variables and formatting strings (-Wxxx errors)


Revision tags: OPENBSD_4_9_BASE
# 1.222 02-Jan-2011 jordan

Re-add code for ACPI taskqueue, eliminates need for state vars in acpi softc
Serialize processing ACPI tasks for sleep/powerdown/GPE event
ok deraadt@


# 1.221 31-Oct-2010 guenther

Revert last commit: it breaks resume on Thinkpads
"then please back it out" deraadt@


# 1.220 26-Oct-2010 jordan

Add task queue for ACPI gpe and notify handlers
This fixes eject on Dell Latitude dock and an issue on the Dell Mini
battery update.
ok deraadt


# 1.219 07-Oct-2010 jordan

Create separate functions for enabling wake and runtime gpes.
Remove old suspend/resume gpewalk function
ok deraadt, mlarkin


# 1.218 05-Oct-2010 jordan

split acpi_enable_onegpe to separate enable/disable functions
ok deraadt


# 1.217 05-Oct-2010 jordan

Added acpi_disable_allgpes function to clear GPE status
ok deraadt


# 1.216 05-Oct-2010 deraadt

clean up acpi_softc use more


# 1.215 05-Oct-2010 deraadt

delete a bit of #if 0 code


# 1.214 05-Oct-2010 deraadt

hide use of our global acpi_softc pointer a bit better


# 1.213 29-Sep-2010 deraadt

grab the acpi thread lock very early in acpi thread startup; not that it
is likely that anyone will race us... but why be careless.
ok kettenis


# 1.212 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.211 29-Aug-2010 jasper

- add a check for FWRITE here too.

ok miod@


# 1.210 27-Aug-2010 kettenis

Make PCI Power Management optional, and only enable it when acpi(4) attaches.

ok deraadt@


Revision tags: OPENBSD_4_8_BASE
# 1.209 08-Aug-2010 kettenis

Close race between the acpi thread and wscons ioctls running in process
context. We might rip this out post-release and replace it with a solution
that serializes things in a more obvious way.

ok deraadt@, miod@ and "those crazy texans who ok anything"


# 1.208 08-Aug-2010 deraadt

Report the acpi sleep states when on a RAMDISK; ok krw


# 1.207 07-Aug-2010 kettenis

Enforce acpi register access rescrictions.

ok mlarkin@, deraadt@, tested by marco@


# 1.206 07-Aug-2010 deraadt

upon resume, notify all the acpiac and acpibat drivers to update
their status. some laptops do not do this automatically.
ok kettenis canacar


# 1.205 06-Aug-2010 marco

Always call _PSW on _LID devices that support it to enable lid open to
resume the machine. Conversely disable it when the machine wakes up.

Tested by several ok deraadt


# 1.204 06-Aug-2010 deraadt

As part of a revamp of the PM1/GPE code, I write this diff to look for
unmanaged EN & STS bits in the PM1 register at interrupt time and report
them. As a side effect this splits the STS acknowledgement into two writes
(for power, and sleep) instead of one. The printf that is added (to spot
unmanaged STS bits) has not yet been triggered as far as we know.

Before the "write to PM1 registers at the right offset" diff went in, this
was not neccessary. But newer thinkpads do not have a working soft power
button without this diff.

We have no idea why.
ok mlarkin kettenis


# 1.203 05-Aug-2010 deraadt

PM1 power button events were generating 'fake events' to acpibtn PWRB,
but if there is no such device these were being tossed. Instead, arrange
for all power button handlers to flag a softc variable which is processed
by the thread; so that one place will know to request a powerdown (psignal
to init)
ok marco mlarkin


# 1.202 05-Aug-2010 deraadt

rename a variable which is annoyingly poorly named


# 1.201 05-Aug-2010 mlarkin

Write to the correct offset for various ACPI pmreg registers. Writing to
the correct register does properly clear the wake status bits and fixed
event bits, which allows some machines to have more than 1 suspend cycle.

bug spotted by deraadt@

tested by deraadt@ and myself on a half-dozen machines and others on
hackers@

ok deraadt@, marco@


# 1.200 05-Aug-2010 deraadt

Fix the power button. The pm1 register is 16 bits in size.
My mistake, a while back.
tested by krw


# 1.199 04-Aug-2010 kettenis

Print supported sleep states.

ok marco@, phessler@, deraadt@


# 1.198 03-Aug-2010 mlarkin

Use the proper flag for re-enabling certain hardware events (power btn,
sleep btn). The (incorrect) flag prevoiously used coincidentally had the
same value, so this shouldn't cause any different behavior than before.

tested on a variety of machines (i386, amd64, sp, mp) - no behavioral
change seen

ok deraadt@


# 1.197 28-Jul-2010 marco

Make all gasio failures look and behave the same.


# 1.196 28-Jul-2010 mlarkin

Delay a little longer on suspend before giving up. Fixes machines that
sometimes don't suspend because the sleep transition takes too long
(symptoms like failing to sleep but the backlight stays on).

ok deraadt@


# 1.195 27-Jul-2010 deraadt

remove XXX; EC can now be attached early when needed; ok jordan


# 1.194 27-Jul-2010 deraadt

more brackets feels good


# 1.193 27-Jul-2010 deraadt

XXX annotate a block in acpi_enter_sleep_state() that is pretty worrying


# 1.192 27-Jul-2010 deraadt

horrific indentation that nearly made me puke


# 1.191 27-Jul-2010 mlarkin

Set the sleeping indicator light on machines that support it, in case SMI
doesn't do it for us. Shows the 'moon' LED on older Thinkpads when
sleeping.

Tested on T43p, W500, T510 by me and on X61s by deraadt@.

ok deraadt@


# 1.190 27-Jul-2010 jordan

Early initialization of acpiec if ECDT table exists
Fixes hang when booting thinkpads while docked
ok deraadt


# 1.189 26-Jul-2010 deraadt

remove merge error which was commited


# 1.188 26-Jul-2010 pirofti

Add support for Sony ACPI hotkeys via a new driver: acpisony(4).

Currently it only works for the suspend button and tries to do right for
the brightness events, but I haven't found a Sony laptop that like to do
right so far.

In the future I want to make the brightness keys work on all Sony's and
also add support for the zoom hotkeys and whatever other funky keys I can
find on those things.

Okay deraadt@.


# 1.187 22-Jul-2010 deraadt

pretty large cleanup of SMALL_KERNEL


# 1.186 21-Jul-2010 deraadt

in the acpi GPE handler, late ack level interrupts not edge interrupts.
Duh.


# 1.185 21-Jul-2010 deraadt

Merge the edge and level gpe handlers into one (now possible since the
gpe structure tells us whether the gpe is edge or level)


# 1.184 21-Jul-2010 deraadt

delete #if 0 code we are not going to use; ok marco


# 1.183 20-Jul-2010 deraadt

make the mask code for acpi_enable_onegpe() match other similar code


# 1.182 20-Jul-2010 deraadt

Handle the acpi interrupt controller with proper edge/level handling,
wrapped in spltty to avoid racing against the interrupt controller.
Repair the gpe bit masking code while there, and do operations in the
right order
ok kettenis mlarkin, help from jordan at figuring out the order of
operations.


# 1.181 20-Jul-2010 matthew

When we're preparing to go to sleep, only call bufq_restart() if other
preparations failed.

ok kettenis@, thib@, deraadt@


# 1.180 19-Jul-2010 deraadt

permit compilation with ACPI_DEBUG


# 1.179 19-Jul-2010 deraadt

there is no need for a seperate dsdt_softc pointer; dsdt does not have a
softc but is just a part of the single acpi driver.
ok jordan


# 1.178 19-Jul-2010 deraadt

acpi_delay() is no longer needed; ok jordan


# 1.177 18-Jul-2010 mlarkin

Clear event status on resume, to avoid going back to sleep/reboot/shutdown
immediately after resume. Tested by a bunch of people on hackers@ on
various hardware. Fixes at least Toshiba NB200 and Gateway LT3103u.

ok marco


# 1.176 13-Jul-2010 deraadt

rename some thread related interfaces so they are logical


# 1.175 10-Jul-2010 jordan

Verify _ADR bdf is correct (some systems return 0xffff)
Works on Dell x9xx and Studio 1555


# 1.174 09-Jul-2010 jordan

Clear the noise


# 1.173 08-Jul-2010 jordan

Add mapping for ACPI device to PCI bdf (match autoconf tree)
Simplify resource parsing function to use buffer argument
Convert namespace linked lists to use queue macros
ok marco@, deraadt@


# 1.172 06-Jul-2010 deraadt

Use spltty() to lock downcalls from apm against the information being
modified by the acpi thread.
ok kettenis


# 1.171 06-Jul-2010 deraadt

Don't use a workq to do the suspend, because it races aginst the acpi
thread. Instead, just tell the acpi thread to do the suspend for us.
This makes apmd & zzz work correctly. While here, have acpithinkpad
attempt to post the event to apm before prompting the suspend itself.
ok kettenis marco mlarkin


# 1.170 05-Jul-2010 mlarkin

Reset register SCI_EN on resume. The ACPI spec clearly states we don't have
to do this, but many machines don't, or forget to, which leads to all sorts
of problems on resume (broken battery stats, overheating, failure to resume
more than once, etc).

tested by many, ok marco@, deraadt@


# 1.169 01-Jul-2010 thib

do bufq quiesce again, since vnd/the bufq_done call in
biodone has been fixed.


# 1.168 01-Jul-2010 jordan

Backout recent AML changes, breaks on R210 and others


# 1.167 01-Jul-2010 jordan

Simplified aml_parse_resource call, cleanup code
ok marco


# 1.166 01-Jul-2010 jordan

Fix subordinate bus # for multi-root PCI buses
ok marco, kettenis


# 1.165 30-Jun-2010 thib

Disable/partially backout the bufq quiesce changes as this
is causing havoc with vnds and release must be buildable.


# 1.164 29-Jun-2010 deraadt

Fix ramdisks (new code was placed inside a SMALL_KERNEL chunk)


# 1.163 29-Jun-2010 jordan

Add support for mapping ACPI to PCI devices
ok kettenis, deraadt


# 1.162 29-Jun-2010 kettenis

Call bufq_quiesce() on suspend to stop all I/O and bufq_restart on resume.
Makes suspend/resume work much better while doing disk I/O.

ok mlarkin@


# 1.161 29-Jun-2010 tedu

60 is a more reasonable guess for number of minutes in an hour than 100.
spotted by TeXitoi. ok deraadt


# 1.160 27-Jun-2010 jordan

Add _acpi_matchhids routine, precursor to pci device match


# 1.159 27-Jun-2010 mlarkin

Clear ACPI fixed event status on resume (power buttons/etc) so that some
machines don't immediately resume after suspending to S3.

Tested by kettenis@ and I on 5 or 6 machines, deraadt@ "yeah, looks good"


# 1.158 27-Jun-2010 jordan

Cleaned up acpi_maptable, adds to table list directly


# 1.157 07-Apr-2010 deraadt

If we have done a wsdisplay_suspend(), and ACPI decides it does not want to
suspend, we must roll back by calling wsdisplay_resume() or the virtual
console switching will be locked forever
ok oga


# 1.156 07-Apr-2010 kettenis

Make suspend/resume work on MP machines (running an MP kernel). Joint work
from mlarkin@ and me, with some amd64 fixes thrown in by deraadt@

ok marco@, deraadt@, pirofti@, mlarkin@


# 1.155 31-Mar-2010 kettenis

Bring /dev/apm support in line with apm(4). Make the suspend button send
a suspend request event to apmd(8) instead of suspending immediately. Also
keep track of whether /dev/apm and /dev/apmctl are currently open, such that
we can still suspend immediately if apmd(8) isn't running.

ok deraadt@, marco@, pirofti@, jsing@, oga@


# 1.154 30-Mar-2010 oga

Prevent the apmd/x races for good.

When we hit suspend time, go through all wsdisplays on the system. if
they are in mode MAPPED, but not MODE_DUMBFB then if possible do a full
vt switch to a !mapped vt, and prevent switching back until resume time.
This has to be called from MD code because this involves userland
running so that X can run the vt switch signal handler. This way, any
case where we are using the "poke registers from userland" model, we
will not be on the hardware when we go down, so the kernel can actually
handle thing properly.

Tested on several acpi laptops (by kettenis@ and ian@), x40 (me and
beck@ at LEAST) and zaurus (me). Maybe others, but if so I forgot who at
this time..

Idea from deraadt somewhere over the Faroe Islands (I thought of a
similar thing myself a while ago). Much prompting from him. Ok and
comments miod@


# 1.153 25-Mar-2010 oga

Fix & vs. && in the kqfilter code.

Based on a comparison with the apm code.

ok deraadt@, kettenis@


Revision tags: OPENBSD_4_7_BASE
# 1.152 26-Nov-2009 mlarkin

Visual (and on some machines, audio) feedback while the machine is
resuming via calls to aml _SST method. This also makes the "moon" LED on
thinkpads to blink during resume and cease when resume is completed.

ok pirofti@


# 1.151 26-Nov-2009 deraadt

Toggle into "cold" when we we disable interrupts, because tsleeping
in the AML interpreter and context switching into userland processes
tends to sometimes pop the ACPI trampoline page out of our address
space. Makes lots more machines work.
ok kettenis mlarkin


# 1.150 24-Nov-2009 jsg

in the resume path evaluate _WAK before dealing with device drivers
like the spec says, not after.

ok mlarkin@


# 1.149 24-Nov-2009 deraadt

abort suspends on SMP systems until that gets written; ok mlarkin


# 1.148 23-Nov-2009 mlarkin

pci should return the result of it's children's suspend/resume calls back
to its parent.

handle suspend failure case and unwind if devices have an activate function
that returns failure.

ok deraadt@, kettenis@


# 1.147 23-Nov-2009 mlarkin

acpi* devices need to call their children on suspend/resume.
ok deraadt@


# 1.146 23-Nov-2009 pirofti

Remove ACPI_SLEEP_ENABLED checks.

This enables by default the suspend/resume paths in the kernel.

Okay deraadt@.


# 1.145 23-Nov-2009 deraadt

knf and spacing that are super annoying


# 1.144 23-Nov-2009 mlarkin

Hook up Theo's new children suspend/resume function, as well as more ACPI
suspend/resume glue.

ok deraadt@


# 1.143 26-Oct-2009 deraadt

Do not do strncmp comparisons on dv_xname because longer device names which
look similar could arrive in the future. Instead, compare directly against
dv_cfdata->cf_driver->cd_name
Issue originally spotted by miod


# 1.142 26-Oct-2009 jordan

Verify checksum+address when loading ACPI tables.
Some systems had invalid entries in RSDT/XSDT.
ok marco@


# 1.141 23-Jul-2009 cnst

New aibs(4) driver for ASUSTeK AI Booster (ACPI ATK0110) hardware monitoring,
with sensor state support through limits provided by the ACPI.

Tested on several ASUS motherboards kindly networked by Sam Fourman Jr.

ok deraadt marco jordan


Revision tags: OPENBSD_4_6_BASE
# 1.140 03-Jun-2009 pirofti

Import acpi power resource driver. This is needed for suspend/resume logic.

Okay jordan@ and marco@.


# 1.139 03-Jun-2009 pirofti

Add acpivideo support. This adds brightness support for all laptops
except thinkpads, they will use the acpithinkpad driver. The driver is
also hooked into wsconsole. So brightness can be adjusted via:

$ wsconsctl display.brightness=<percentage>

This is very helpfull on some laptops that have a nasty bios and get two
steps instead of one when pressing the brightness button.

Tested on various dell, fujitsu, acer, samsung and other laptops.

Okay marco@, miod@. Suggestions from kettenis@.
Lots of reviews and help from miod@, thanks!


# 1.138 03-Jun-2009 jordan

Added ejectable bay for IDE cdroms
ok marco@


# 1.137 30-Apr-2009 marco

Oga changed apm ioctls and forgot to grep. Temporary work around by hitting
the sleep button when we get any request. This interface will chnage at
some point.
ok oga


# 1.136 19-Apr-2009 krw

Call acpi_sleep_walk() when powering down just like when entering
any other sleep state. This tells acpi wake devices to not wake up
the box until manual powering up resets their status. Makes my Dell
GX520 stay powered off in response to 'halt -p'.

In snaps for a while.

ok marco@ (several times) deraadt@


# 1.135 17-Apr-2009 pirofti

Add device ranking support for future sleep/wake developments.

okay marco@


# 1.134 11-Mar-2009 jordan

Cleanup.. use aml_evalinteger instead of aml_evalname
ok marco@


# 1.133 10-Mar-2009 jordan

Fix cut/paste error.. call _BFS not _PTS
ok marco@


Revision tags: OPENBSD_4_5_BASE
# 1.132 21-Feb-2009 marco

overzealous cut/paste; found by Vladimir Kirillov <proger@uaoug.org.ua>


# 1.131 20-Feb-2009 marco

Oops didn't mean for Debugger() to remain there.


# 1.130 19-Feb-2009 marco

suspend/resume bits so that we can develop this in tree. This is disabled.
code from mlarkin and me
help from art,toby,jordan and several others
ok jordan, go for it deraadt


# 1.129 10-Feb-2009 jordan

Fix null pointer for shutdown if acpi not enabled
ok marco@


# 1.128 04-Feb-2009 kettenis

Some systems that claim to be ACPI 2.0 apparently don't provide an XSDT
address. Make the code fall back on the RSDT on those systems.

From Joerg Niendorf <p3y80@internode.on.net>. Fixes PR 5986.


# 1.127 17-Jan-2009 krw

A couple of stray malloc/memset -> malloc(M_ZERO), a bit of knf and
lose a couple of unnecessary casts. No functional change.

Feedback from kettenis@

ok kettenis@ marco@


# 1.126 06-Nov-2008 marco

Collapse all sanity checks of hids into one function.

Originaly from jordan


Revision tags: OPENBSD_4_4_BASE
# 1.125 02-Jul-2008 fgsch

add acpivideo. for now almost an empty skel so others can work on
suspend/resume. eventually it will also manage output switching and
brightness where supported. prodded by marco@


# 1.124 11-Jun-2008 marco

Fix $OpenBSD$ for once and for all


# 1.123 08-Jun-2008 deraadt

aaa_dev was being set from an uninitialized variable; ok toby


# 1.122 07-Jun-2008 marco

Kill ACPIVERBOSE. It was never used and is worthless to boot.

comments jmc
ok toby


# 1.121 06-Jun-2008 marco

Shrink acpi some more for floppies and while here re-enable acpi on the
floppies. Fixes crazy sony vaio kettenis has.

tested by deraadt
ok deraadt jordan kettenis


# 1.120 01-Jun-2008 marco

Fix \n nit in SMALL_KERNEL.


# 1.119 01-Jun-2008 marco

Re-enable new parser. Lots of testing by various folks. Fits all media
now too.

ok kettenis deraadt


# 1.118 16-May-2008 dlg

back out jordans new parser.

it didnt have enough oks from the right people, it did break machines
that were previously working, and it bloated the RAMDISKS and install
media beyond the constraints of the media.

requested by deraadt@


# 1.117 14-May-2008 jordan

Adding new ACPI Parser code
Old guts of code still exists, needs to be torn out next
ok marco@
ok brad@


# 1.116 27-Apr-2008 jcs

add acpithinkpad(4), an acpi support driver for ibm/lenovo
thinkpads.

provides functionality that the bios did on older, non-acpi
thinkpads like respond to screen brightness keys and toggle the
bluetooth device on and off.

ok deraadt@ and marco@


# 1.115 24-Apr-2008 jsing

Introduce acpiasus(4), a driver for the ACPI based hotkeys found in many
ASUS laptops (including the ASUS EeePC) - largely based on NetBSD's
asus(4) driver. On the ASUS EeePC this allows us to enable/disable
wireless, change screen brightness and use the volume keys.

ok jsg@, weingart@


# 1.114 12-Apr-2008 kettenis

Get rid of acpi_s5 global variables; simply send SIGUSR2 instead of SUGUSR1
to tell init(8) to power down the machine.

ok krw@


Revision tags: OPENBSD_4_3_BASE
# 1.113 21-Jan-2008 jsg

Check against RESET_REG_SUP in FADT before doing the acpi reset dance
after all, not doing so appears to break more machines than it fixes.

ok marco@


# 1.112 05-Jan-2008 kettenis

Never attach more acpicpu(4) devices than cpu(4) devices. Always attach
the first acpicpu(4) device on non-MULTIPROCESSOR kernels, regardless of its
ACPI processor ID.

ok mikeb@


# 1.111 05-Dec-2007 deraadt

Hang acpi(4) below bios(4) instead of mainbus(4). This lets us move acpi(4)
before pcibios(4), and then.... if acpi(4) attaches, skip pcibios(4) since
it messes the machines up. Fixes claudio's HP dl320 G5, and almost assuredly
others.
ok kettenis


# 1.110 05-Dec-2007 deraadt

repair acpiopen/acpiclose and friends to handle the /dev/apmctl interface,
so that apmd(8) can actually work correctly against acpi(4), and thus,
even do the fancy -C/-A stuff; also tested by jmc


# 1.109 02-Dec-2007 fgsch

fix the trailing space for real now.


# 1.108 02-Dec-2007 fgsch

avoid trailing spaces on output; marco@ ok.


# 1.107 27-Nov-2007 kettenis

Fix off-by-one that made us attach acpicpu0 and acpicpu1 on GENERIC kernels.

ok marco@


# 1.106 25-Nov-2007 jsg

When available and ACPI is enabled use the ACPI reset method
to reboot. This is required for some quirky sis machines
and likely others to reboot.

Tested by many. 'commit early commit often' deraadt@


# 1.105 19-Nov-2007 deraadt

sigh, amd64 still uses that stupid acpi_enabled variable...


# 1.104 19-Nov-2007 deraadt

softc's come initialized to 0, you know...


# 1.103 19-Nov-2007 deraadt

store revision; from jsg


# 1.102 19-Nov-2007 kettenis

Initialize sc_facs earlier.

ok deraadt@, beck@, canacar@


# 1.101 16-Nov-2007 deraadt

fix the bus_space #define nightmare, so that amd64 and i386 are much more
uniform. as a result shared code like acpi needs less #ifdef's
ok marco kettenis


# 1.100 15-Nov-2007 deraadt

remove the #ifdef ACPI_ENABLE. the EC codepath is either safe enough (and
will be improved), or it is unsafe because AML assumes it must be there
ok almost everyone


# 1.99 15-Nov-2007 mikeb

Make acpicpu(4) attach to the existing CPUs only.

Problem reported by form@. Fix idea by kettenis@.
Lots of help from deraadt@. Tested by deraadt@, form@ and me.

ok deraadt marco


# 1.98 14-Nov-2007 deraadt

spacing


# 1.97 12-Nov-2007 deraadt

correct : vs , in attach-time messages


# 1.96 06-Nov-2007 deraadt

all ACPI_ENABLE kernels need acpi_interrupt()


# 1.95 06-Nov-2007 deraadt

move acpi_read_pmreg() and acpi_write_pmreg() out of SMALL_KERNEL so
that acpi ramdisks can compile (obvious solution)


# 1.94 05-Nov-2007 canacar

Establish the acpi interrupt earlier. Fixes problems with some
GENERIC.MP kernels not getting acpi interrupts. This is a
workaround until the real issue with late establishment is fixed.
ok weingart@, beck@, gwk@


# 1.93 03-Nov-2007 jordan

Added support for displaying wakeup devices
ok beck@,weingart@,gwk@


# 1.92 11-Oct-2007 kettenis

When walking the tree yo find _PRT methods, check whether the device is
actually enabled and functioning. Similar to what we do for _INI.

ok weingart@, beck@


# 1.91 08-Oct-2007 krw

More simple memset(,0,) -> M_ZERO changes. In this batch move to
size(*p) as the first malloc() parameter where p is declared locally
and thus easy to check. Add M_ZERO to gpe_table allocation in acpi.c
even though there is no obvious bzero or memset nearby.


# 1.90 13-Sep-2007 mk

Remove unnecessary memset() (see aml_evalnode(9)) and a bit of KNF.

ok toby


# 1.89 13-Sep-2007 weingart

Implement ACPI 6.5.1 spec tree walk for _STA and _INI.
Fixes mk's laptop. No regressions so far (thank you to the
testers).

ok gwk@, mk@, marco@


Revision tags: OPENBSD_4_2_BASE
# 1.88 17-Apr-2007 mk

Now that acpidock no longer hangs machines during boot (at least
according to the reports I've been getting) it should be safe to enable
it again, so remove the #if 0 stuff around it.

ok marco


# 1.87 11-Apr-2007 jordan

Added changes for C-State
ok marco@


# 1.86 26-Mar-2007 marco

Calculate minutes left more accurately.

From giovanni <qgiovanni@gmail.com>


Revision tags: OPENBSD_4_1_BASE
# 1.85 03-Mar-2007 mk

Stop looking for acpidock until we fix that stuff that breaks all pre-60
series ThinkPads with acpidock(4) enabled.


# 1.84 22-Feb-2007 marco

We were getting no interrupts in .MP. There are 2 fixes; one in
acpi_machdep.c and move the interrupt establishment down after ioapic
has been initialized.

ok jordan


# 1.83 21-Feb-2007 kettenis

Make an acpi-enabled GENERIC.MP work on systems that only support PIC mode.

ok marco@


# 1.82 19-Feb-2007 jordan

Added acpi_map_address function for mapping GAS/IO registers
ok marco@


# 1.81 18-Feb-2007 jordan

Added dynamic perfect hash for opcodes
ok marco@


# 1.80 18-Feb-2007 marco

Shave off another K pre compression.
Shuffle SMALL_KERNEL functions around so that we only have a single #ifndef


# 1.79 17-Feb-2007 marco

Add SMALL_KERNEL throught acpi to make it fit on boot media.
ok deraadt


# 1.78 13-Feb-2007 marco

Add smarter _INI code. This helps with some dissapearing devices.

ok kettenis jordan


# 1.77 31-Jan-2007 gwk

Introduce acpi_hasprocfvs thats "ACPI Has Processor Frequency and Voltage
Scalling" a global flag set by acicpu attach to indicate that ACPI is
handling fvs through the _PSS objects. This will fix disappearing
EST/powernow when booting kernels with acpi enabled, as acpicpu isnt
compiled into GENERIC yet. Also in cases where acpicpu is built in and
legacy EST/powernow work but acpi is missing the _PSS object, the legacy
routines will be used.

tested by dim@, and Nick Nauwelaerts <nick AT nauwelaerts.net>
ok marco@, dim@


# 1.76 22-Jan-2007 mk

Actually look for docking stations by looking for _DCK nodes.

ok marco


# 1.75 02-Jan-2007 marco

On some systems EC must be attached as the first device. Move EC from
generic _HID devices to its own routine to force first attach. Bug found
and debugged by joris@

Code mostly written by joris@


# 1.74 26-Dec-2006 marco

Add polling to devices that require it such as acpibat and acpitz.
Use the same tsleep/wakeup mechanism as the interrupt code to keep it non-
concurrent.


# 1.73 21-Dec-2006 deraadt

even more knf love


# 1.72 21-Dec-2006 marco

Remove some leftover acpiec gpe/interrupt code.


# 1.71 20-Dec-2006 deraadt

oops, name already used


# 1.70 20-Dec-2006 deraadt

rename __acpi_enable_gpe(); ok marco


# 1.69 20-Dec-2006 deraadt

acpi_unmap_pmregs() is not used, ok marco


# 1.68 19-Dec-2006 deraadt

simplify some low-level methods substantially (shrinking code); ok marco jordan


# 1.67 06-Dec-2006 mk

malloc(..., M_NOWAIT) with no NULL check is bad. If allocation fails,
complain, clean up, and bail out of attach.

input and ok gwk


# 1.66 28-Nov-2006 jordan

Fixed clearing arg for acpi_init_pic
ok kettenis@


# 1.65 27-Nov-2006 kettenis

Don't spam dmesg.


# 1.64 27-Nov-2006 jordan

Fixes for acpiprt routing
Post-parse fixup for namerefs
ok kettenis@


# 1.63 25-Nov-2006 marco

Don't handle acpiev events inside the gpe handler since this causes deadlocks.

ok jordan@


# 1.62 15-Nov-2006 kettenis

Hook up acpi apic interrupt routing support to generic acpi code.

ok jordan@


# 1.61 02-Nov-2006 marco

Kill acpiec attach function and add its functionality to the generic _HID
device attach

prodded and ok jordan


# 1.60 02-Nov-2006 marco

Fix comments to reflect reality.


# 1.59 19-Oct-2006 marco

Replace ACPI ioctls with APM ioctls. This makes ACPI talk the same protocol
as APM therefore userspace can now retrieve power information in the same
fashion as all other apm arches (zaurus, macppc, i386 etc).

To use force the use of acpi device like so: apmd -f /dev/acpi

ok jordan@


# 1.58 19-Oct-2006 jordan

Fixed GPE debug levels
Fixed ref counter on temporary bufferfields
DSDT parsing fully working on IBM Thinkpad
ok marco@


# 1.57 15-Oct-2006 jordan

Removed old GPE handler code remnants
ok marco@


# 1.56 12-Oct-2006 jordan

Massive ACPI changes
Fixed ACPI leaks
new ACPI API
ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.55 30-Jun-2006 gwk

KNF, No functional change,
"go at it" marco@


# 1.54 30-Jun-2006 jordan

Support for calling Embedded Controller via new GPE interface
ok marco@


# 1.53 30-Jun-2006 jordan

Added new code for GPE handling
Also works with ACPIEC; need to fixup acpiec.c to remove original interrupt handler
ok marco@


# 1.52 30-Jun-2006 jordan

Added offset argument for acpi read/write pmreg routines (new GPE code)
ok marco@


# 1.51 31-May-2006 todd

do not call acpi_powerdown() unless acpi has successfully initialized
ok gwk@ canacar@


# 1.50 29-May-2006 canacar

Add embedded controller support.
ok marco@ deraadt@ testing & ok gwk@


# 1.49 19-May-2006 canacar

Minimal thermal zone driver. Monitors thermal zone temperature,
shuts down the system if the 'critical temperature' is reached.
ok marco@


# 1.48 11-Apr-2006 marco

KNF.


# 1.47 11-Apr-2006 gwk

Add acpi_resume(struct acpi_softc *sc), which calls some AML methods,
detected at attach time, currently unused.

ok marco@


# 1.46 11-Apr-2006 gwk

Discover some ACPI sleep related methods, and call them at the
appropriate time in acpi_enter_sleep_state().

ok marco@


Revision tags: OPENBSD_3_9_BASE
# 1.45 26-Feb-2006 marco

Add acpicpu as a device.

ok jordan@


# 1.44 23-Feb-2006 marco

remove dead function.

ok jordan@


# 1.43 22-Feb-2006 marco

Print out all tables it found during autoconf. We'll need this data in
the future.


# 1.42 22-Feb-2006 marco

Make dmesg less verbose.

ok jordan.


# 1.41 22-Feb-2006 jordan

Added parsing of SSDT tables
ok marco@


# 1.40 22-Feb-2006 jordan

Added new aml_register to notify by device id
Moved powerdown code to button device
ok marco@


# 1.39 21-Feb-2006 marco

Readjust debug levels that crept in too low over the last few days.
KNF


# 1.38 21-Feb-2006 marco

Rig the power button so that the machine shuts down correctly.


# 1.37 20-Feb-2006 marco

Add acpibtn device. Remains disabled for now.


# 1.36 19-Feb-2006 jordan

Added code for aml_notify GPE notification callback
ok marco@


# 1.35 19-Feb-2006 grange

Fix acpi_enter_sleep_state() by replacing current ugly hack
with mostly correct sleeping state transition code.

ok brad@ marco@


# 1.34 19-Feb-2006 marco

jordan:
Enable interrupts.
Added peripheral code to make this work.

marco:
Replace softintr and other types of interrupts with a handler with context.
Fixed interrupt magic for GPEs.
Some KNF

This is work done by jordan and I but has to be a single commit.


# 1.33 17-Feb-2006 marco

Remove earlier introduced delays. We really only need them during autconf.

ok jordan@


# 1.32 17-Feb-2006 marco

Add a generic delay function as per the ACPI spec.

from jordan@


# 1.31 17-Feb-2006 marco

Use tsleep instead of delay when !cold.


# 1.30 17-Feb-2006 marco

Argh, we need delays between reads/writes. The backend reads (i2c most likely)
are so slow that values come back as 0 sometimes. We have to do this
despite the spec saying we shouldnt have to. Guess having a faster
parser than others can hurt sometimes.

Idea from jordan.


# 1.29 17-Feb-2006 jordan

Fixed code for handling _INI
Rearranged debug statements
ok marco@


# 1.28 16-Feb-2006 jordan

Added stub for calling _INI
Fixed aml_dowhile for NULL return
ok marco@


# 1.27 16-Feb-2006 jordan

Fixed parser/evaluator to retrieve battery info+state
Cleanup of parser
Dell laptops return battery status
ok marco@


# 1.26 06-Feb-2006 brad

fix a typo so this will compile on amd64.


# 1.25 04-Feb-2006 marco

When ACPI_ENABLED wasn't set but the devices were it had an unknown variable.

Pointed out by ian@.


# 1.24 03-Feb-2006 jordan

Rewrote aml parser yet again...
Fixed bus_space_map for i386/amd64
Added _BST and _BIF to acpibat to get status/info
ok marco@


# 1.23 20-Jan-2006 jordan

More evaluator changes
Removed old evaluator code
Fixed _HID value
ok marco@


# 1.22 19-Jan-2006 jordan

Added support for read/write of PCI Config space
Help jason@
ok marco@


# 1.21 18-Jan-2006 jordan

Added new evaluation routines
Added helper methods for buffer/field evaluation
Fixed broken indentation on dsdt.c

ok marco@


# 1.20 17-Jan-2006 jordan

Major overhaul of the aml parser. Evaluater temporarily disabled.

ok marco@


# 1.19 06-Jan-2006 grange

Check for ACPI control availability only if ACPI_ENABLE defined.


# 1.18 06-Jan-2006 grange

Informative error messages on attach, prettier dmesg.


# 1.17 06-Jan-2006 grange

printf -> dnprintf conversion for debug messages.


# 1.16 05-Jan-2006 grange

Find SLP_TYPx values and store them for future use in
acpi_enter_sleep_state().


# 1.15 05-Jan-2006 grange

Random KNF and cleanup


# 1.14 05-Jan-2006 grange

Provide symbolic names for the acpi sleeping states and use it.


# 1.13 28-Dec-2005 marco

Moved DEVNAME to a acpidev.h

ok jordan@


# 1.12 18-Dec-2005 sturm

add back comment about wrong code

ok marco


# 1.11 16-Dec-2005 marco

Remove test for kbd_reset. Prompted deraadt@, ok jordan@


# 1.10 16-Dec-2005 jordan

Added hooks for powerdown/power button press


# 1.9 16-Dec-2005 jordan

Cleaned up common power management register i/o
Added enter sleep state ioctl
Hooked power button interrupt
ok marco@


# 1.8 16-Dec-2005 marco

Remove autoconf chatter


# 1.7 16-Dec-2005 jordan

Moved DSDT AML parsing into parent ACPI driver
ok marco@


# 1.6 14-Dec-2005 marco

Add AC device glue.


# 1.5 14-Dec-2005 marco

Adjust debug printing levels to something more sane.


# 1.4 13-Dec-2005 marco

Add glue to attach devices to apci.


# 1.3 07-Dec-2005 marco

KNF


Revision tags: OPENBSD_3_8_BASE
# 1.2 10-Jul-2005 grange

Spacing cleanup.


# 1.1 02-Jun-2005 tholo

Start on a basic ACPI framework -- does not do much more than read out the
ACPI tables into kernel memory and attach ACPI and HPET timers currently.

In order to test this code, enabling the devices in GENERIC as well as
the ACPI_ENABLE option is needed. This code does not do any thermal
control yet, so this should be done with care depending on the platform.

In the tree so more people can contribute to making this more fully
featured.

Ok niklas@ grange@ tedu@