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 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 <realmode.h>
  28. #include <bios.h>
  29. #include <ipxe/io.h>
  30. #include <ipxe/acpi.h>
  31. #include <ipxe/acpipwr.h>
  32. /** @file
  33. *
  34. * ACPI power off
  35. *
  36. */
  37. /** Colour for debug messages */
  38. #define colour FADT_SIGNATURE
  39. /** _S5_ signature */
  40. #define S5_SIGNATURE ACPI_SIGNATURE ( '_', 'S', '5', '_' )
  41. /**
  42. * Power off the computer using ACPI
  43. *
  44. * @ret rc Return status code
  45. */
  46. int acpi_poweroff ( void ) {
  47. struct acpi_fadt fadtab;
  48. uint16_t ebda;
  49. userptr_t rsdt;
  50. userptr_t fadt;
  51. unsigned int pm1a_cnt_blk;
  52. unsigned int pm1b_cnt_blk;
  53. unsigned int pm1a_cnt;
  54. unsigned int pm1b_cnt;
  55. unsigned int slp_typa;
  56. unsigned int slp_typb;
  57. int s5;
  58. int rc;
  59. /* Locate EBDA */
  60. get_real ( ebda, BDA_SEG, BDA_EBDA );
  61. /* Locate RSDT */
  62. rsdt = acpi_find_rsdt ( real_to_user ( ebda, 0 ) );
  63. if ( ! rsdt ) {
  64. DBGC ( colour, "ACPI could not find RSDT (EBDA %04x)\n", ebda );
  65. return -ENOENT;
  66. }
  67. /* Locate FADT */
  68. fadt = acpi_find ( rsdt, FADT_SIGNATURE, 0 );
  69. if ( ! fadt ) {
  70. DBGC ( colour, "ACPI could not find FADT\n" );
  71. return -ENOENT;
  72. }
  73. /* Read FADT */
  74. copy_from_user ( &fadtab, fadt, 0, sizeof ( fadtab ) );
  75. pm1a_cnt_blk = le32_to_cpu ( fadtab.pm1a_cnt_blk );
  76. pm1b_cnt_blk = le32_to_cpu ( fadtab.pm1b_cnt_blk );
  77. pm1a_cnt = ( pm1a_cnt_blk + ACPI_PM1_CNT );
  78. pm1b_cnt = ( pm1b_cnt_blk + ACPI_PM1_CNT );
  79. /* Extract \_S5 from DSDT or any SSDT */
  80. s5 = acpi_sx ( rsdt, S5_SIGNATURE );
  81. if ( s5 < 0 ) {
  82. rc = s5;
  83. DBGC ( colour, "ACPI could not extract \\_S5: %s\n",
  84. strerror ( rc ) );
  85. return rc;
  86. }
  87. /* Power off system */
  88. if ( pm1a_cnt_blk ) {
  89. slp_typa = ( ( s5 >> 0 ) & 0xff );
  90. DBGC ( colour, "ACPI PM1a sleep type %#x => %04x\n",
  91. slp_typa, pm1a_cnt );
  92. outw ( ( ACPI_PM1_CNT_SLP_TYP ( slp_typa ) |
  93. ACPI_PM1_CNT_SLP_EN ), pm1a_cnt );
  94. }
  95. if ( pm1b_cnt_blk ) {
  96. slp_typb = ( ( s5 >> 8 ) & 0xff );
  97. DBGC ( colour, "ACPI PM1b sleep type %#x => %04x\n",
  98. slp_typb, pm1b_cnt );
  99. outw ( ( ACPI_PM1_CNT_SLP_TYP ( slp_typb ) |
  100. ACPI_PM1_CNT_SLP_EN ), pm1b_cnt );
  101. }
  102. /* On some systems, execution will continue briefly. Delay to
  103. * avoid potentially confusing log messages.
  104. */
  105. mdelay ( 1000 );
  106. DBGC ( colour, "ACPI power off failed\n" );
  107. return -EPROTO;
  108. }