You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

acpipwr.c 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * Copyright (C) 2016 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. *
  19. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. #include <unistd.h>
  25. #include <errno.h>
  26. #include <byteswap.h>
  27. #include <ipxe/io.h>
  28. #include <ipxe/acpi.h>
  29. #include <ipxe/acpipwr.h>
  30. /** @file
  31. *
  32. * ACPI power off
  33. *
  34. */
  35. /** Colour for debug messages */
  36. #define colour FADT_SIGNATURE
  37. /** _S5_ signature */
  38. #define S5_SIGNATURE ACPI_SIGNATURE ( '_', 'S', '5', '_' )
  39. /**
  40. * Power off the computer using ACPI
  41. *
  42. * @ret rc Return status code
  43. */
  44. int acpi_poweroff ( void ) {
  45. struct acpi_fadt fadtab;
  46. userptr_t fadt;
  47. unsigned int pm1a_cnt_blk;
  48. unsigned int pm1b_cnt_blk;
  49. unsigned int pm1a_cnt;
  50. unsigned int pm1b_cnt;
  51. unsigned int slp_typa;
  52. unsigned int slp_typb;
  53. int s5;
  54. int rc;
  55. /* Locate FADT */
  56. fadt = acpi_find ( FADT_SIGNATURE, 0 );
  57. if ( ! fadt ) {
  58. DBGC ( colour, "ACPI could not find FADT\n" );
  59. return -ENOENT;
  60. }
  61. /* Read FADT */
  62. copy_from_user ( &fadtab, fadt, 0, sizeof ( fadtab ) );
  63. pm1a_cnt_blk = le32_to_cpu ( fadtab.pm1a_cnt_blk );
  64. pm1b_cnt_blk = le32_to_cpu ( fadtab.pm1b_cnt_blk );
  65. pm1a_cnt = ( pm1a_cnt_blk + ACPI_PM1_CNT );
  66. pm1b_cnt = ( pm1b_cnt_blk + ACPI_PM1_CNT );
  67. /* Extract \_S5 from DSDT or any SSDT */
  68. s5 = acpi_sx ( S5_SIGNATURE );
  69. if ( s5 < 0 ) {
  70. rc = s5;
  71. DBGC ( colour, "ACPI could not extract \\_S5: %s\n",
  72. strerror ( rc ) );
  73. return rc;
  74. }
  75. /* Power off system */
  76. if ( pm1a_cnt_blk ) {
  77. slp_typa = ( ( s5 >> 0 ) & 0xff );
  78. DBGC ( colour, "ACPI PM1a sleep type %#x => %04x\n",
  79. slp_typa, pm1a_cnt );
  80. outw ( ( ACPI_PM1_CNT_SLP_TYP ( slp_typa ) |
  81. ACPI_PM1_CNT_SLP_EN ), pm1a_cnt );
  82. }
  83. if ( pm1b_cnt_blk ) {
  84. slp_typb = ( ( s5 >> 8 ) & 0xff );
  85. DBGC ( colour, "ACPI PM1b sleep type %#x => %04x\n",
  86. slp_typb, pm1b_cnt );
  87. outw ( ( ACPI_PM1_CNT_SLP_TYP ( slp_typb ) |
  88. ACPI_PM1_CNT_SLP_EN ), pm1b_cnt );
  89. }
  90. /* On some systems, execution will continue briefly. Delay to
  91. * avoid potentially confusing log messages.
  92. */
  93. mdelay ( 1000 );
  94. DBGC ( colour, "ACPI power off failed\n" );
  95. return -EPROTO;
  96. }