18c2ecf20Sopenharmony_ci.. SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci
38c2ecf20Sopenharmony_ci=======================================
48c2ecf20Sopenharmony_ciLinux ACPI Custom Control Method How To
58c2ecf20Sopenharmony_ci=======================================
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci:Author: Zhang Rui <rui.zhang@intel.com>
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ciLinux supports customizing ACPI control methods at runtime.
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ciUsers can use this to:
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci1. override an existing method which may not work correctly,
158c2ecf20Sopenharmony_ci   or just for debugging purposes.
168c2ecf20Sopenharmony_ci2. insert a completely new method in order to create a missing
178c2ecf20Sopenharmony_ci   method such as _OFF, _ON, _STA, _INI, etc.
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ciFor these cases, it is far simpler to dynamically install a single
208c2ecf20Sopenharmony_cicontrol method rather than override the entire DSDT, because kernel
218c2ecf20Sopenharmony_cirebuild/reboot is not needed and test result can be got in minutes.
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci.. note::
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci  - Only ACPI METHOD can be overridden, any other object types like
268c2ecf20Sopenharmony_ci    "Device", "OperationRegion", are not recognized. Methods
278c2ecf20Sopenharmony_ci    declared inside scope operators are also not supported.
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci  - The same ACPI control method can be overridden for many times,
308c2ecf20Sopenharmony_ci    and it's always the latest one that used by Linux/kernel.
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci  - To get the ACPI debug object output (Store (AAAA, Debug)),
338c2ecf20Sopenharmony_ci    please run::
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci      echo 1 > /sys/module/acpi/parameters/aml_debug_output
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci1. override an existing method
398c2ecf20Sopenharmony_ci==============================
408c2ecf20Sopenharmony_cia) get the ACPI table via ACPI sysfs I/F. e.g. to get the DSDT,
418c2ecf20Sopenharmony_ci   just run "cat /sys/firmware/acpi/tables/DSDT > /tmp/dsdt.dat"
428c2ecf20Sopenharmony_cib) disassemble the table by running "iasl -d dsdt.dat".
438c2ecf20Sopenharmony_cic) rewrite the ASL code of the method and save it in a new file,
448c2ecf20Sopenharmony_cid) package the new file (psr.asl) to an ACPI table format.
458c2ecf20Sopenharmony_ci   Here is an example of a customized \_SB._AC._PSR method::
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci      DefinitionBlock ("", "SSDT", 1, "", "", 0x20080715)
488c2ecf20Sopenharmony_ci      {
498c2ecf20Sopenharmony_ci         Method (\_SB_.AC._PSR, 0, NotSerialized)
508c2ecf20Sopenharmony_ci         {
518c2ecf20Sopenharmony_ci            Store ("In AC _PSR", Debug)
528c2ecf20Sopenharmony_ci            Return (ACON)
538c2ecf20Sopenharmony_ci         }
548c2ecf20Sopenharmony_ci      }
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci   Note that the full pathname of the method in ACPI namespace
578c2ecf20Sopenharmony_ci   should be used.
588c2ecf20Sopenharmony_cie) assemble the file to generate the AML code of the method.
598c2ecf20Sopenharmony_ci   e.g. "iasl -vw 6084 psr.asl" (psr.aml is generated as a result)
608c2ecf20Sopenharmony_ci   If parameter "-vw 6084" is not supported by your iASL compiler,
618c2ecf20Sopenharmony_ci   please try a newer version.
628c2ecf20Sopenharmony_cif) mount debugfs by "mount -t debugfs none /sys/kernel/debug"
638c2ecf20Sopenharmony_cig) override the old method via the debugfs by running
648c2ecf20Sopenharmony_ci   "cat /tmp/psr.aml > /sys/kernel/debug/acpi/custom_method"
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci2. insert a new method
678c2ecf20Sopenharmony_ci======================
688c2ecf20Sopenharmony_ciThis is easier than overriding an existing method.
698c2ecf20Sopenharmony_ciWe just need to create the ASL code of the method we want to
708c2ecf20Sopenharmony_ciinsert and then follow the step c) ~ g) in section 1.
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci3. undo your changes
738c2ecf20Sopenharmony_ci====================
748c2ecf20Sopenharmony_ciThe "undo" operation is not supported for a new inserted method
758c2ecf20Sopenharmony_ciright now, i.e. we can not remove a method currently.
768c2ecf20Sopenharmony_ciFor an overridden method, in order to undo your changes, please
778c2ecf20Sopenharmony_cisave a copy of the method original ASL code in step c) section 1,
788c2ecf20Sopenharmony_ciand redo step c) ~ g) to override the method with the original one.
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci.. note:: We can use a kernel with multiple custom ACPI method running,
828c2ecf20Sopenharmony_ci   But each individual write to debugfs can implement a SINGLE
838c2ecf20Sopenharmony_ci   method override. i.e. if we want to insert/override multiple
848c2ecf20Sopenharmony_ci   ACPI methods, we need to redo step c) ~ g) for multiple times.
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci.. note:: Be aware that root can mis-use this driver to modify arbitrary
878c2ecf20Sopenharmony_ci   memory and gain additional rights, if root's privileges got
888c2ecf20Sopenharmony_ci   restricted (for example if root is not allowed to load additional
898c2ecf20Sopenharmony_ci   modules after boot).
90