Patrick Georgi | 11f0079 | 2020-03-04 15:10:45 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Rudolf Marek | 293b5f5 | 2009-02-01 18:35:15 +0000 | [diff] [blame] | 2 | |
Paul Menzel | 0f4c0e2 | 2013-02-22 12:33:08 +0100 | [diff] [blame] | 3 | /* How much nesting do we support? */ |
Rudolf Marek | 293b5f5 | 2009-02-01 18:35:15 +0000 | [diff] [blame] | 4 | #define ACPIGEN_LENSTACK_SIZE 10 |
| 5 | |
Felix Held | 575ee13 | 2023-11-13 14:50:35 +0100 | [diff] [blame] | 6 | /* If you need to change this, change acpigen_pop_len too */ |
| 7 | #define ACPIGEN_RSVD_PKGLEN_BYTES 3 |
Rudolf Marek | 293b5f5 | 2009-02-01 18:35:15 +0000 | [diff] [blame] | 8 | |
Duncan Laurie | 3829f23 | 2016-05-09 11:08:46 -0700 | [diff] [blame] | 9 | #include <lib.h> |
Rudolf Marek | 293b5f5 | 2009-02-01 18:35:15 +0000 | [diff] [blame] | 10 | #include <string.h> |
Furquan Shaikh | 76cedd2 | 2020-05-02 10:24:23 -0700 | [diff] [blame] | 11 | #include <acpi/acpigen.h> |
Elyes HAOUAS | cd4fe0f | 2019-03-29 17:12:15 +0100 | [diff] [blame] | 12 | #include <assert.h> |
Tim Wawrzynczak | d96f3a2 | 2021-07-14 15:56:57 -0600 | [diff] [blame] | 13 | #include <commonlib/helpers.h> |
Rudolf Marek | 293b5f5 | 2009-02-01 18:35:15 +0000 | [diff] [blame] | 14 | #include <console/console.h> |
Tobias Diedrich | 0fe6e9a | 2010-11-17 16:27:06 +0000 | [diff] [blame] | 15 | #include <device/device.h> |
Duncan Laurie | 08a942f | 2020-04-29 12:13:14 -0700 | [diff] [blame] | 16 | #include <device/soundwire.h> |
Elyes HAOUAS | cdd2f63 | 2021-02-11 19:37:11 +0100 | [diff] [blame] | 17 | #include <types.h> |
Rudolf Marek | 293b5f5 | 2009-02-01 18:35:15 +0000 | [diff] [blame] | 18 | |
| 19 | static char *gencurrent; |
| 20 | |
| 21 | char *len_stack[ACPIGEN_LENSTACK_SIZE]; |
| 22 | int ltop = 0; |
| 23 | |
Vladimir Serbinenko | 9bb5c5c | 2014-11-09 03:51:32 +0100 | [diff] [blame] | 24 | void acpigen_write_len_f(void) |
Rudolf Marek | 293b5f5 | 2009-02-01 18:35:15 +0000 | [diff] [blame] | 25 | { |
| 26 | ASSERT(ltop < (ACPIGEN_LENSTACK_SIZE - 1)) |
Paul Menzel | 0f4c0e2 | 2013-02-22 12:33:08 +0100 | [diff] [blame] | 27 | len_stack[ltop++] = gencurrent; |
Felix Held | 575ee13 | 2023-11-13 14:50:35 +0100 | [diff] [blame] | 28 | /* Reserve ACPIGEN_RSVD_PKGLEN_BYTES bytes for PkgLength. The actual byte values will |
| 29 | be written later in the corresponding acpigen_pop_len call. */ |
| 30 | for (size_t i = 0; i < ACPIGEN_RSVD_PKGLEN_BYTES; i++) |
| 31 | acpigen_emit_byte(0); |
Rudolf Marek | 293b5f5 | 2009-02-01 18:35:15 +0000 | [diff] [blame] | 32 | } |
| 33 | |
Vladimir Serbinenko | 430363a | 2014-11-02 21:51:22 +0100 | [diff] [blame] | 34 | void acpigen_pop_len(void) |
| 35 | { |
Felix Held | a6f7459 | 2023-11-10 19:57:27 +0100 | [diff] [blame] | 36 | size_t len; |
Vladimir Serbinenko | 430363a | 2014-11-02 21:51:22 +0100 | [diff] [blame] | 37 | ASSERT(ltop > 0) |
| 38 | char *p = len_stack[--ltop]; |
| 39 | len = gencurrent - p; |
Felix Held | 575ee13 | 2023-11-13 14:50:35 +0100 | [diff] [blame] | 40 | const size_t payload_len = len - ACPIGEN_RSVD_PKGLEN_BYTES; |
Vladimir Serbinenko | 430363a | 2014-11-02 21:51:22 +0100 | [diff] [blame] | 41 | |
Felix Held | a6f7459 | 2023-11-10 19:57:27 +0100 | [diff] [blame] | 42 | if (len <= 0x3f + 2) { |
| 43 | /* PkgLength of up to 0x3f can be encoded in one PkgLength byte instead of the |
| 44 | reserved 3 bytes. Since only 1 PkgLength byte will be written, the payload |
| 45 | data needs to be moved by 2 bytes */ |
Felix Held | 575ee13 | 2023-11-13 14:50:35 +0100 | [diff] [blame] | 46 | memmove(&p[ACPIGEN_RSVD_PKGLEN_BYTES - 2], |
| 47 | &p[ACPIGEN_RSVD_PKGLEN_BYTES], payload_len); |
Felix Held | a6f7459 | 2023-11-10 19:57:27 +0100 | [diff] [blame] | 48 | /* Adjust the PkgLength to take into account that we only use 1 of the 3 |
| 49 | reserved bytes */ |
| 50 | len -= 2; |
| 51 | /* The two most significant bits of PkgLength get the value of 0 to indicate |
| 52 | there are no additional PkgLength bytes. In this case the single PkgLength |
| 53 | byte encodes the length in its lower 6 bits */ |
| 54 | p[0] = len; |
| 55 | /* Adjust pointer for next ACPI bytecode byte */ |
| 56 | acpigen_set_current(p + len); |
| 57 | } else if (len <= 0xfff + 1) { |
| 58 | /* PkgLength of up to 0xfff can be encoded in 2 PkgLength bytes instead of the |
| 59 | reserved 3 bytes. Since only 2 PkgLength bytes will be written, the payload |
| 60 | data needs to be moved by 1 byte */ |
Felix Held | 575ee13 | 2023-11-13 14:50:35 +0100 | [diff] [blame] | 61 | memmove(&p[ACPIGEN_RSVD_PKGLEN_BYTES - 1], |
| 62 | &p[ACPIGEN_RSVD_PKGLEN_BYTES], payload_len); |
Felix Held | a6f7459 | 2023-11-10 19:57:27 +0100 | [diff] [blame] | 63 | /* Adjust the PkgLength to take into account that we only use 2 of the 3 |
| 64 | reserved bytes */ |
| 65 | len -= 1; |
| 66 | /* The two most significant bits of PkgLength get the value of 1 to indicate |
| 67 | there's a second PkgLength byte. The lower 4 bits of the first PkgLength |
| 68 | byte and the second PkgLength byte encode the length */ |
| 69 | p[0] = (0x1 << 6 | (len & 0xf)); |
| 70 | p[1] = (len >> 4 & 0xff); |
| 71 | /* Adjust pointer for next ACPI bytecode byte */ |
| 72 | acpigen_set_current(p + len); |
| 73 | } else if (len <= 0xfffff) { |
| 74 | /* PkgLength of up to 0xfffff can be encoded in 3 PkgLength bytes. Since this |
| 75 | is the amount of reserved bytes, no need to move the payload in this case */ |
| 76 | /* The two most significant bits of PkgLength get the value of 2 to indicate |
| 77 | there are two more PkgLength bytes following the first one. The lower 4 bits |
| 78 | of the first PkgLength byte and the two following PkgLength bytes encode the |
| 79 | length */ |
| 80 | p[0] = (0x2 << 6 | (len & 0xf)); |
| 81 | p[1] = (len >> 4 & 0xff); |
| 82 | p[2] = (len >> 12 & 0xff); |
| 83 | /* No need to adjust pointer for next ACPI bytecode byte */ |
| 84 | } else { |
| 85 | /* The case of PkgLength up to 0xfffffff isn't supported at the moment */ |
| 86 | printk(BIOS_ERR, "%s: package length exceeds maximum of 0xfffff.\n", __func__); |
| 87 | } |
Vladimir Serbinenko | 430363a | 2014-11-02 21:51:22 +0100 | [diff] [blame] | 88 | } |
| 89 | |
Stefan Reinauer | f96c2d9 | 2009-04-22 16:23:47 +0000 | [diff] [blame] | 90 | void acpigen_set_current(char *curr) |
| 91 | { |
| 92 | gencurrent = curr; |
Rudolf Marek | 293b5f5 | 2009-02-01 18:35:15 +0000 | [diff] [blame] | 93 | } |
| 94 | |
Stefan Reinauer | f96c2d9 | 2009-04-22 16:23:47 +0000 | [diff] [blame] | 95 | char *acpigen_get_current(void) |
| 96 | { |
| 97 | return gencurrent; |
Rudolf Marek | 293b5f5 | 2009-02-01 18:35:15 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Vladimir Serbinenko | 9bb5c5c | 2014-11-09 03:51:32 +0100 | [diff] [blame] | 100 | void acpigen_emit_byte(unsigned char b) |
Rudolf Marek | 293b5f5 | 2009-02-01 18:35:15 +0000 | [diff] [blame] | 101 | { |
| 102 | (*gencurrent++) = b; |
Rudolf Marek | 293b5f5 | 2009-02-01 18:35:15 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Furquan Shaikh | e4e9b94 | 2016-10-20 23:09:05 -0700 | [diff] [blame] | 105 | void acpigen_emit_ext_op(uint8_t op) |
| 106 | { |
| 107 | acpigen_emit_byte(EXT_OP_PREFIX); |
| 108 | acpigen_emit_byte(op); |
| 109 | } |
| 110 | |
Duncan Laurie | 9ccae75 | 2016-05-09 07:43:19 -0700 | [diff] [blame] | 111 | void acpigen_emit_word(unsigned int data) |
| 112 | { |
| 113 | acpigen_emit_byte(data & 0xff); |
| 114 | acpigen_emit_byte((data >> 8) & 0xff); |
| 115 | } |
| 116 | |
| 117 | void acpigen_emit_dword(unsigned int data) |
| 118 | { |
| 119 | acpigen_emit_byte(data & 0xff); |
| 120 | acpigen_emit_byte((data >> 8) & 0xff); |
| 121 | acpigen_emit_byte((data >> 16) & 0xff); |
| 122 | acpigen_emit_byte((data >> 24) & 0xff); |
| 123 | } |
| 124 | |
Duncan Laurie | 85d8027 | 2016-07-02 19:53:54 -0700 | [diff] [blame] | 125 | char *acpigen_write_package(int nr_el) |
Rudolf Marek | 293b5f5 | 2009-02-01 18:35:15 +0000 | [diff] [blame] | 126 | { |
Duncan Laurie | 85d8027 | 2016-07-02 19:53:54 -0700 | [diff] [blame] | 127 | char *p; |
Furquan Shaikh | e4e9b94 | 2016-10-20 23:09:05 -0700 | [diff] [blame] | 128 | acpigen_emit_byte(PACKAGE_OP); |
Vladimir Serbinenko | 9bb5c5c | 2014-11-09 03:51:32 +0100 | [diff] [blame] | 129 | acpigen_write_len_f(); |
Duncan Laurie | 85d8027 | 2016-07-02 19:53:54 -0700 | [diff] [blame] | 130 | p = acpigen_get_current(); |
Rudolf Marek | 293b5f5 | 2009-02-01 18:35:15 +0000 | [diff] [blame] | 131 | acpigen_emit_byte(nr_el); |
Duncan Laurie | 85d8027 | 2016-07-02 19:53:54 -0700 | [diff] [blame] | 132 | return p; |
Rudolf Marek | 293b5f5 | 2009-02-01 18:35:15 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Vladimir Serbinenko | 9bb5c5c | 2014-11-09 03:51:32 +0100 | [diff] [blame] | 135 | void acpigen_write_byte(unsigned int data) |
Rudolf Marek | 293b5f5 | 2009-02-01 18:35:15 +0000 | [diff] [blame] | 136 | { |
Furquan Shaikh | e4e9b94 | 2016-10-20 23:09:05 -0700 | [diff] [blame] | 137 | acpigen_emit_byte(BYTE_PREFIX); |
Rudolf Marek | 293b5f5 | 2009-02-01 18:35:15 +0000 | [diff] [blame] | 138 | acpigen_emit_byte(data & 0xff); |
Rudolf Marek | 293b5f5 | 2009-02-01 18:35:15 +0000 | [diff] [blame] | 139 | } |
| 140 | |
Duncan Laurie | 9ccae75 | 2016-05-09 07:43:19 -0700 | [diff] [blame] | 141 | void acpigen_write_word(unsigned int data) |
| 142 | { |
Furquan Shaikh | e4e9b94 | 2016-10-20 23:09:05 -0700 | [diff] [blame] | 143 | acpigen_emit_byte(WORD_PREFIX); |
Duncan Laurie | 9ccae75 | 2016-05-09 07:43:19 -0700 | [diff] [blame] | 144 | acpigen_emit_word(data); |
| 145 | } |
| 146 | |
Vladimir Serbinenko | 9bb5c5c | 2014-11-09 03:51:32 +0100 | [diff] [blame] | 147 | void acpigen_write_dword(unsigned int data) |
Rudolf Marek | 293b5f5 | 2009-02-01 18:35:15 +0000 | [diff] [blame] | 148 | { |
Furquan Shaikh | e4e9b94 | 2016-10-20 23:09:05 -0700 | [diff] [blame] | 149 | acpigen_emit_byte(DWORD_PREFIX); |
Duncan Laurie | 9ccae75 | 2016-05-09 07:43:19 -0700 | [diff] [blame] | 150 | acpigen_emit_dword(data); |
Rudolf Marek | 293b5f5 | 2009-02-01 18:35:15 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Vladimir Serbinenko | 9bb5c5c | 2014-11-09 03:51:32 +0100 | [diff] [blame] | 153 | void acpigen_write_qword(uint64_t data) |
Carl-Daniel Hailfinger | d58671c | 2009-02-17 21:38:51 +0000 | [diff] [blame] | 154 | { |
Furquan Shaikh | e4e9b94 | 2016-10-20 23:09:05 -0700 | [diff] [blame] | 155 | acpigen_emit_byte(QWORD_PREFIX); |
Duncan Laurie | 9ccae75 | 2016-05-09 07:43:19 -0700 | [diff] [blame] | 156 | acpigen_emit_dword(data & 0xffffffff); |
| 157 | acpigen_emit_dword((data >> 32) & 0xffffffff); |
Carl-Daniel Hailfinger | d58671c | 2009-02-17 21:38:51 +0000 | [diff] [blame] | 158 | } |
| 159 | |
Duncan Laurie | f7c3876 | 2016-05-09 08:20:38 -0700 | [diff] [blame] | 160 | void acpigen_write_zero(void) |
| 161 | { |
Furquan Shaikh | e4e9b94 | 2016-10-20 23:09:05 -0700 | [diff] [blame] | 162 | acpigen_emit_byte(ZERO_OP); |
Duncan Laurie | f7c3876 | 2016-05-09 08:20:38 -0700 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | void acpigen_write_one(void) |
| 166 | { |
Furquan Shaikh | e4e9b94 | 2016-10-20 23:09:05 -0700 | [diff] [blame] | 167 | acpigen_emit_byte(ONE_OP); |
Duncan Laurie | f7c3876 | 2016-05-09 08:20:38 -0700 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | void acpigen_write_ones(void) |
| 171 | { |
Furquan Shaikh | e4e9b94 | 2016-10-20 23:09:05 -0700 | [diff] [blame] | 172 | acpigen_emit_byte(ONES_OP); |
Duncan Laurie | f7c3876 | 2016-05-09 08:20:38 -0700 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | void acpigen_write_integer(uint64_t data) |
| 176 | { |
| 177 | if (data == 0) |
| 178 | acpigen_write_zero(); |
| 179 | else if (data == 1) |
| 180 | acpigen_write_one(); |
| 181 | else if (data <= 0xff) |
| 182 | acpigen_write_byte((unsigned char)data); |
| 183 | else if (data <= 0xffff) |
| 184 | acpigen_write_word((unsigned int)data); |
| 185 | else if (data <= 0xffffffff) |
| 186 | acpigen_write_dword((unsigned int)data); |
| 187 | else |
| 188 | acpigen_write_qword(data); |
| 189 | } |
| 190 | |
Vladimir Serbinenko | 9bb5c5c | 2014-11-09 03:51:32 +0100 | [diff] [blame] | 191 | void acpigen_write_name_byte(const char *name, uint8_t val) |
Stefan Reinauer | f96c2d9 | 2009-04-22 16:23:47 +0000 | [diff] [blame] | 192 | { |
Vladimir Serbinenko | 9bb5c5c | 2014-11-09 03:51:32 +0100 | [diff] [blame] | 193 | acpigen_write_name(name); |
| 194 | acpigen_write_byte(val); |
Rudolf Marek | 293b5f5 | 2009-02-01 18:35:15 +0000 | [diff] [blame] | 195 | } |
| 196 | |
Vladimir Serbinenko | 9bb5c5c | 2014-11-09 03:51:32 +0100 | [diff] [blame] | 197 | void acpigen_write_name_dword(const char *name, uint32_t val) |
Stefan Reinauer | f96c2d9 | 2009-04-22 16:23:47 +0000 | [diff] [blame] | 198 | { |
Vladimir Serbinenko | 9bb5c5c | 2014-11-09 03:51:32 +0100 | [diff] [blame] | 199 | acpigen_write_name(name); |
| 200 | acpigen_write_dword(val); |
Rudolf Marek | 293b5f5 | 2009-02-01 18:35:15 +0000 | [diff] [blame] | 201 | } |
| 202 | |
Vladimir Serbinenko | 9bb5c5c | 2014-11-09 03:51:32 +0100 | [diff] [blame] | 203 | void acpigen_write_name_qword(const char *name, uint64_t val) |
Stefan Reinauer | f96c2d9 | 2009-04-22 16:23:47 +0000 | [diff] [blame] | 204 | { |
Vladimir Serbinenko | 9bb5c5c | 2014-11-09 03:51:32 +0100 | [diff] [blame] | 205 | acpigen_write_name(name); |
| 206 | acpigen_write_qword(val); |
Carl-Daniel Hailfinger | d58671c | 2009-02-17 21:38:51 +0000 | [diff] [blame] | 207 | } |
| 208 | |
Duncan Laurie | f7c3876 | 2016-05-09 08:20:38 -0700 | [diff] [blame] | 209 | void acpigen_write_name_integer(const char *name, uint64_t val) |
| 210 | { |
| 211 | acpigen_write_name(name); |
| 212 | acpigen_write_integer(val); |
| 213 | } |
| 214 | |
Duncan Laurie | 56b69aa | 2016-05-09 08:17:02 -0700 | [diff] [blame] | 215 | void acpigen_write_name_string(const char *name, const char *string) |
| 216 | { |
| 217 | acpigen_write_name(name); |
| 218 | acpigen_write_string(string); |
| 219 | } |
| 220 | |
Patrick Rudolph | 389c827 | 2019-12-17 13:54:41 +0100 | [diff] [blame] | 221 | void acpigen_write_name_unicode(const char *name, const char *string) |
| 222 | { |
| 223 | const size_t len = strlen(string) + 1; |
| 224 | acpigen_write_name(name); |
| 225 | acpigen_emit_byte(BUFFER_OP); |
| 226 | acpigen_write_len_f(); |
Matt DeVillier | d4d40c6 | 2023-11-09 15:04:58 -0600 | [diff] [blame] | 227 | acpigen_write_integer(2 * len); |
Patrick Rudolph | 389c827 | 2019-12-17 13:54:41 +0100 | [diff] [blame] | 228 | for (size_t i = 0; i < len; i++) { |
Arthur Heymans | 62ea7a8 | 2023-06-22 20:39:31 +0200 | [diff] [blame] | 229 | const signed char c = string[i]; |
Patrick Rudolph | 389c827 | 2019-12-17 13:54:41 +0100 | [diff] [blame] | 230 | /* Simple ASCII to UTF-16 conversion, replace non ASCII characters */ |
| 231 | acpigen_emit_word(c >= 0 ? c : '?'); |
| 232 | } |
| 233 | acpigen_pop_len(); |
| 234 | } |
| 235 | |
Vladimir Serbinenko | 9bb5c5c | 2014-11-09 03:51:32 +0100 | [diff] [blame] | 236 | void acpigen_emit_stream(const char *data, int size) |
Stefan Reinauer | f96c2d9 | 2009-04-22 16:23:47 +0000 | [diff] [blame] | 237 | { |
Rudolf Marek | 293b5f5 | 2009-02-01 18:35:15 +0000 | [diff] [blame] | 238 | int i; |
Lee Leahy | 9c7c6f7 | 2017-03-16 11:24:09 -0700 | [diff] [blame] | 239 | for (i = 0; i < size; i++) |
Rudolf Marek | 293b5f5 | 2009-02-01 18:35:15 +0000 | [diff] [blame] | 240 | acpigen_emit_byte(data[i]); |
Rudolf Marek | 293b5f5 | 2009-02-01 18:35:15 +0000 | [diff] [blame] | 241 | } |
| 242 | |
Duncan Laurie | 56b69aa | 2016-05-09 08:17:02 -0700 | [diff] [blame] | 243 | void acpigen_emit_string(const char *string) |
| 244 | { |
Jonathan Neuschäfer | 0ba307f | 2016-05-17 17:40:09 +0200 | [diff] [blame] | 245 | acpigen_emit_stream(string, string ? strlen(string) : 0); |
Duncan Laurie | 56b69aa | 2016-05-09 08:17:02 -0700 | [diff] [blame] | 246 | acpigen_emit_byte('\0'); /* NUL */ |
| 247 | } |
| 248 | |
| 249 | void acpigen_write_string(const char *string) |
| 250 | { |
Furquan Shaikh | e4e9b94 | 2016-10-20 23:09:05 -0700 | [diff] [blame] | 251 | acpigen_emit_byte(STRING_PREFIX); |
Duncan Laurie | 56b69aa | 2016-05-09 08:17:02 -0700 | [diff] [blame] | 252 | acpigen_emit_string(string); |
| 253 | } |
| 254 | |
Duncan Laurie | 3731903 | 2016-05-26 12:47:05 -0700 | [diff] [blame] | 255 | void acpigen_write_coreboot_hid(enum coreboot_acpi_ids id) |
| 256 | { |
Joel Kitching | 3ab36b84 | 2018-03-08 12:09:32 +0800 | [diff] [blame] | 257 | char hid[9]; /* BOOTxxxx */ |
Duncan Laurie | 3731903 | 2016-05-26 12:47:05 -0700 | [diff] [blame] | 258 | |
Duncan Laurie | 02fdb3e | 2016-09-22 14:08:33 -0700 | [diff] [blame] | 259 | snprintf(hid, sizeof(hid), "%.4s%04X", COREBOOT_ACPI_ID, id); |
Duncan Laurie | 3731903 | 2016-05-26 12:47:05 -0700 | [diff] [blame] | 260 | acpigen_write_name_string("_HID", hid); |
| 261 | } |
| 262 | |
Paul Menzel | 0f4c0e2 | 2013-02-22 12:33:08 +0100 | [diff] [blame] | 263 | /* |
| 264 | * The naming conventions for ACPI namespace names are a bit tricky as |
Martin Roth | 0cd338e | 2016-07-29 14:07:30 -0600 | [diff] [blame] | 265 | * each element has to be 4 chars wide ("All names are a fixed 32 bits.") |
| 266 | * and "By convention, when an ASL compiler pads a name shorter than 4 |
| 267 | * characters, it is done so with trailing underscores ('_')". |
Paul Menzel | 0f4c0e2 | 2013-02-22 12:33:08 +0100 | [diff] [blame] | 268 | * |
| 269 | * Check sections 5.3, 18.2.2 and 18.4 of ACPI spec 3.0 for details. |
| 270 | */ |
Rudolf Marek | 3310d75 | 2009-06-21 20:26:13 +0000 | [diff] [blame] | 271 | |
Lee Leahy | 9c7c6f7 | 2017-03-16 11:24:09 -0700 | [diff] [blame] | 272 | static void acpigen_emit_simple_namestring(const char *name) |
| 273 | { |
Vladimir Serbinenko | 9bb5c5c | 2014-11-09 03:51:32 +0100 | [diff] [blame] | 274 | int i; |
Rudolf Marek | 3310d75 | 2009-06-21 20:26:13 +0000 | [diff] [blame] | 275 | char ud[] = "____"; |
| 276 | for (i = 0; i < 4; i++) { |
| 277 | if ((name[i] == '\0') || (name[i] == '.')) { |
Vladimir Serbinenko | 9bb5c5c | 2014-11-09 03:51:32 +0100 | [diff] [blame] | 278 | acpigen_emit_stream(ud, 4 - i); |
Rudolf Marek | 3310d75 | 2009-06-21 20:26:13 +0000 | [diff] [blame] | 279 | break; |
Rudolf Marek | 3310d75 | 2009-06-21 20:26:13 +0000 | [diff] [blame] | 280 | } |
Lee Leahy | 0b5678f | 2017-03-16 16:01:40 -0700 | [diff] [blame] | 281 | acpigen_emit_byte(name[i]); |
Rudolf Marek | 3310d75 | 2009-06-21 20:26:13 +0000 | [diff] [blame] | 282 | } |
Rudolf Marek | 3310d75 | 2009-06-21 20:26:13 +0000 | [diff] [blame] | 283 | } |
| 284 | |
Lee Leahy | 9c7c6f7 | 2017-03-16 11:24:09 -0700 | [diff] [blame] | 285 | static void acpigen_emit_double_namestring(const char *name, int dotpos) |
| 286 | { |
Furquan Shaikh | e4e9b94 | 2016-10-20 23:09:05 -0700 | [diff] [blame] | 287 | acpigen_emit_byte(DUAL_NAME_PREFIX); |
Vladimir Serbinenko | 9bb5c5c | 2014-11-09 03:51:32 +0100 | [diff] [blame] | 288 | acpigen_emit_simple_namestring(name); |
| 289 | acpigen_emit_simple_namestring(&name[dotpos + 1]); |
Rudolf Marek | 3310d75 | 2009-06-21 20:26:13 +0000 | [diff] [blame] | 290 | } |
| 291 | |
Lee Leahy | 9c7c6f7 | 2017-03-16 11:24:09 -0700 | [diff] [blame] | 292 | static void acpigen_emit_multi_namestring(const char *name) |
| 293 | { |
Vladimir Serbinenko | 9bb5c5c | 2014-11-09 03:51:32 +0100 | [diff] [blame] | 294 | int count = 0; |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 295 | unsigned char *pathlen; |
Furquan Shaikh | e4e9b94 | 2016-10-20 23:09:05 -0700 | [diff] [blame] | 296 | acpigen_emit_byte(MULTI_NAME_PREFIX); |
| 297 | acpigen_emit_byte(ZERO_OP); |
Elyes Haouas | 3141fbad | 2022-11-18 15:22:32 +0100 | [diff] [blame] | 298 | pathlen = ((unsigned char *)acpigen_get_current()) - 1; |
Rudolf Marek | 3310d75 | 2009-06-21 20:26:13 +0000 | [diff] [blame] | 299 | |
| 300 | while (name[0] != '\0') { |
Vladimir Serbinenko | 9bb5c5c | 2014-11-09 03:51:32 +0100 | [diff] [blame] | 301 | acpigen_emit_simple_namestring(name); |
Rudolf Marek | 3310d75 | 2009-06-21 20:26:13 +0000 | [diff] [blame] | 302 | /* find end or next entity */ |
| 303 | while ((name[0] != '.') && (name[0] != '\0')) |
| 304 | name++; |
| 305 | /* forward to next */ |
| 306 | if (name[0] == '.') |
| 307 | name++; |
| 308 | count++; |
| 309 | } |
| 310 | |
| 311 | pathlen[0] = count; |
Rudolf Marek | 3310d75 | 2009-06-21 20:26:13 +0000 | [diff] [blame] | 312 | } |
| 313 | |
Lee Leahy | 9c7c6f7 | 2017-03-16 11:24:09 -0700 | [diff] [blame] | 314 | void acpigen_emit_namestring(const char *namepath) |
| 315 | { |
Rudolf Marek | 3310d75 | 2009-06-21 20:26:13 +0000 | [diff] [blame] | 316 | int dotcount = 0, i; |
Myles Watson | 3fe6b70 | 2009-10-09 20:13:43 +0000 | [diff] [blame] | 317 | int dotpos = 0; |
Rudolf Marek | 3310d75 | 2009-06-21 20:26:13 +0000 | [diff] [blame] | 318 | |
Ronak Kanabar | 7c0f007 | 2020-11-30 15:40:51 +0530 | [diff] [blame] | 319 | /* Check for NULL pointer */ |
| 320 | if (!namepath) |
| 321 | return; |
| 322 | |
Ronald G. Minnich | be738eb | 2013-03-07 11:05:28 -0600 | [diff] [blame] | 323 | /* We can start with a '\'. */ |
Rudolf Marek | 3310d75 | 2009-06-21 20:26:13 +0000 | [diff] [blame] | 324 | if (namepath[0] == '\\') { |
Vladimir Serbinenko | 9bb5c5c | 2014-11-09 03:51:32 +0100 | [diff] [blame] | 325 | acpigen_emit_byte('\\'); |
Rudolf Marek | 3310d75 | 2009-06-21 20:26:13 +0000 | [diff] [blame] | 326 | namepath++; |
| 327 | } |
| 328 | |
Ronald G. Minnich | be738eb | 2013-03-07 11:05:28 -0600 | [diff] [blame] | 329 | /* And there can be any number of '^' */ |
Rudolf Marek | 3310d75 | 2009-06-21 20:26:13 +0000 | [diff] [blame] | 330 | while (namepath[0] == '^') { |
Vladimir Serbinenko | 9bb5c5c | 2014-11-09 03:51:32 +0100 | [diff] [blame] | 331 | acpigen_emit_byte('^'); |
Rudolf Marek | 3310d75 | 2009-06-21 20:26:13 +0000 | [diff] [blame] | 332 | namepath++; |
| 333 | } |
| 334 | |
Vladimir Serbinenko | d942ed9 | 2014-08-30 20:44:37 +0200 | [diff] [blame] | 335 | /* If we have only \\ or only ^...^. Then we need to put a null |
| 336 | name (0x00). */ |
Elyes HAOUAS | dbf3067 | 2016-08-21 17:37:15 +0200 | [diff] [blame] | 337 | if (namepath[0] == '\0') { |
Furquan Shaikh | e4e9b94 | 2016-10-20 23:09:05 -0700 | [diff] [blame] | 338 | acpigen_emit_byte(ZERO_OP); |
Vladimir Serbinenko | 9bb5c5c | 2014-11-09 03:51:32 +0100 | [diff] [blame] | 339 | return; |
Vladimir Serbinenko | d942ed9 | 2014-08-30 20:44:37 +0200 | [diff] [blame] | 340 | } |
Rudolf Marek | 3310d75 | 2009-06-21 20:26:13 +0000 | [diff] [blame] | 341 | |
| 342 | i = 0; |
| 343 | while (namepath[i] != '\0') { |
| 344 | if (namepath[i] == '.') { |
| 345 | dotcount++; |
| 346 | dotpos = i; |
| 347 | } |
| 348 | i++; |
| 349 | } |
| 350 | |
Lee Leahy | 9c7c6f7 | 2017-03-16 11:24:09 -0700 | [diff] [blame] | 351 | if (dotcount == 0) |
Vladimir Serbinenko | 9bb5c5c | 2014-11-09 03:51:32 +0100 | [diff] [blame] | 352 | acpigen_emit_simple_namestring(namepath); |
Lee Leahy | 9c7c6f7 | 2017-03-16 11:24:09 -0700 | [diff] [blame] | 353 | else if (dotcount == 1) |
Vladimir Serbinenko | 9bb5c5c | 2014-11-09 03:51:32 +0100 | [diff] [blame] | 354 | acpigen_emit_double_namestring(namepath, dotpos); |
Lee Leahy | 9c7c6f7 | 2017-03-16 11:24:09 -0700 | [diff] [blame] | 355 | else |
Vladimir Serbinenko | 9bb5c5c | 2014-11-09 03:51:32 +0100 | [diff] [blame] | 356 | acpigen_emit_multi_namestring(namepath); |
Rudolf Marek | 3310d75 | 2009-06-21 20:26:13 +0000 | [diff] [blame] | 357 | } |
| 358 | |
Vladimir Serbinenko | 9bb5c5c | 2014-11-09 03:51:32 +0100 | [diff] [blame] | 359 | void acpigen_write_name(const char *name) |
Rudolf Marek | 293b5f5 | 2009-02-01 18:35:15 +0000 | [diff] [blame] | 360 | { |
Furquan Shaikh | e4e9b94 | 2016-10-20 23:09:05 -0700 | [diff] [blame] | 361 | acpigen_emit_byte(NAME_OP); |
Vladimir Serbinenko | 9bb5c5c | 2014-11-09 03:51:32 +0100 | [diff] [blame] | 362 | acpigen_emit_namestring(name); |
Rudolf Marek | 293b5f5 | 2009-02-01 18:35:15 +0000 | [diff] [blame] | 363 | } |
| 364 | |
Vladimir Serbinenko | 9bb5c5c | 2014-11-09 03:51:32 +0100 | [diff] [blame] | 365 | void acpigen_write_scope(const char *name) |
Rudolf Marek | 293b5f5 | 2009-02-01 18:35:15 +0000 | [diff] [blame] | 366 | { |
Furquan Shaikh | e4e9b94 | 2016-10-20 23:09:05 -0700 | [diff] [blame] | 367 | acpigen_emit_byte(SCOPE_OP); |
Vladimir Serbinenko | 9bb5c5c | 2014-11-09 03:51:32 +0100 | [diff] [blame] | 368 | acpigen_write_len_f(); |
| 369 | acpigen_emit_namestring(name); |
Rudolf Marek | 293b5f5 | 2009-02-01 18:35:15 +0000 | [diff] [blame] | 370 | } |
Rudolf Marek | f997b55 | 2009-02-14 15:40:23 +0000 | [diff] [blame] | 371 | |
Duncan Laurie | 2fe7471 | 2020-06-01 17:36:56 -0700 | [diff] [blame] | 372 | void acpigen_get_package_op_element(uint8_t package_op, unsigned int element, uint8_t dest_op) |
| 373 | { |
Duncan Laurie | ec2e3e4 | 2020-11-03 15:19:08 -0800 | [diff] [blame] | 374 | /* <dest_op> = DeRefOf (<package_op>[<element>]) */ |
Duncan Laurie | 2fe7471 | 2020-06-01 17:36:56 -0700 | [diff] [blame] | 375 | acpigen_write_store(); |
| 376 | acpigen_emit_byte(DEREF_OP); |
| 377 | acpigen_emit_byte(INDEX_OP); |
| 378 | acpigen_emit_byte(package_op); |
| 379 | acpigen_write_integer(element); |
| 380 | acpigen_emit_byte(ZERO_OP); /* Ignore Index() Destination */ |
| 381 | acpigen_emit_byte(dest_op); |
| 382 | } |
| 383 | |
Duncan Laurie | ec2e3e4 | 2020-11-03 15:19:08 -0800 | [diff] [blame] | 384 | void acpigen_set_package_op_element_int(uint8_t package_op, unsigned int element, uint64_t src) |
| 385 | { |
| 386 | /* DeRefOf (<package>[<element>]) = <src> */ |
| 387 | acpigen_write_store(); |
| 388 | acpigen_write_integer(src); |
| 389 | acpigen_emit_byte(DEREF_OP); |
| 390 | acpigen_emit_byte(INDEX_OP); |
| 391 | acpigen_emit_byte(package_op); |
| 392 | acpigen_write_integer(element); |
| 393 | acpigen_emit_byte(ZERO_OP); /* Ignore Index() Destination */ |
| 394 | } |
| 395 | |
| 396 | void acpigen_get_package_element(const char *package, unsigned int element, uint8_t dest_op) |
| 397 | { |
| 398 | /* <dest_op> = <package>[<element>] */ |
| 399 | acpigen_write_store(); |
| 400 | acpigen_emit_byte(INDEX_OP); |
| 401 | acpigen_emit_namestring(package); |
| 402 | acpigen_write_integer(element); |
| 403 | acpigen_emit_byte(ZERO_OP); /* Ignore Index() Destination */ |
| 404 | acpigen_emit_byte(dest_op); |
| 405 | } |
| 406 | |
| 407 | void acpigen_set_package_element_int(const char *package, unsigned int element, uint64_t src) |
| 408 | { |
| 409 | /* <package>[<element>] = <src> */ |
| 410 | acpigen_write_store(); |
| 411 | acpigen_write_integer(src); |
| 412 | acpigen_emit_byte(INDEX_OP); |
| 413 | acpigen_emit_namestring(package); |
| 414 | acpigen_write_integer(element); |
| 415 | acpigen_emit_byte(ZERO_OP); /* Ignore Index() Destination */ |
| 416 | } |
| 417 | |
| 418 | void acpigen_set_package_element_namestr(const char *package, unsigned int element, |
| 419 | const char *src) |
| 420 | { |
| 421 | /* <package>[<element>] = <src> */ |
| 422 | acpigen_write_store(); |
| 423 | acpigen_emit_namestring(src); |
| 424 | acpigen_emit_byte(INDEX_OP); |
| 425 | acpigen_emit_namestring(package); |
| 426 | acpigen_write_integer(element); |
| 427 | acpigen_emit_byte(ZERO_OP); /* Ignore Index() Destination */ |
| 428 | } |
| 429 | |
Felix Held | b57b12f | 2023-01-24 19:31:00 +0100 | [diff] [blame] | 430 | void acpigen_write_processor_namestring(unsigned int cpu_index) |
| 431 | { |
| 432 | char buffer[16]; |
Felix Held | f0a8b04 | 2023-05-12 15:55:06 +0200 | [diff] [blame] | 433 | snprintf(buffer, sizeof(buffer), "\\_SB." CONFIG_ACPI_CPU_STRING, cpu_index); |
Felix Held | b57b12f | 2023-01-24 19:31:00 +0100 | [diff] [blame] | 434 | acpigen_emit_namestring(buffer); |
| 435 | } |
| 436 | |
Elyes Haouas | 9c82491 | 2023-01-28 09:13:17 +0100 | [diff] [blame] | 437 | /* Processor() operator is deprecated as of ACPI 6.0, use Device() instead. */ |
Vladimir Serbinenko | 9bb5c5c | 2014-11-09 03:51:32 +0100 | [diff] [blame] | 438 | void acpigen_write_processor(u8 cpuindex, u32 pblock_addr, u8 pblock_len) |
Rudolf Marek | f997b55 | 2009-02-14 15:40:23 +0000 | [diff] [blame] | 439 | { |
| 440 | /* |
Christian Walter | be3979c | 2019-12-18 15:07:59 +0100 | [diff] [blame] | 441 | Processor (\_SB.CPcpuindex, cpuindex, pblock_addr, pblock_len) |
Elyes HAOUAS | 6b72787 | 2016-09-03 08:28:48 +0200 | [diff] [blame] | 442 | { |
Rudolf Marek | f997b55 | 2009-02-14 15:40:23 +0000 | [diff] [blame] | 443 | */ |
Furquan Shaikh | e4e9b94 | 2016-10-20 23:09:05 -0700 | [diff] [blame] | 444 | acpigen_emit_ext_op(PROCESSOR_OP); |
Vladimir Serbinenko | 9bb5c5c | 2014-11-09 03:51:32 +0100 | [diff] [blame] | 445 | acpigen_write_len_f(); |
Felix Held | b57b12f | 2023-01-24 19:31:00 +0100 | [diff] [blame] | 446 | acpigen_write_processor_namestring(cpuindex); |
Rudolf Marek | f997b55 | 2009-02-14 15:40:23 +0000 | [diff] [blame] | 447 | acpigen_emit_byte(cpuindex); |
Duncan Laurie | 9ccae75 | 2016-05-09 07:43:19 -0700 | [diff] [blame] | 448 | acpigen_emit_dword(pblock_addr); |
Rudolf Marek | f997b55 | 2009-02-14 15:40:23 +0000 | [diff] [blame] | 449 | acpigen_emit_byte(pblock_len); |
Rudolf Marek | f997b55 | 2009-02-14 15:40:23 +0000 | [diff] [blame] | 450 | } |
| 451 | |
Felix Held | 32bba18 | 2023-01-28 03:37:21 +0100 | [diff] [blame] | 452 | void acpigen_write_processor_device(unsigned int cpu_index) |
| 453 | { |
| 454 | acpigen_emit_ext_op(DEVICE_OP); |
| 455 | acpigen_write_len_f(); |
| 456 | acpigen_write_processor_namestring(cpu_index); |
| 457 | acpigen_write_name_string("_HID", "ACPI0007"); |
| 458 | acpigen_write_name_integer("_UID", cpu_index); |
| 459 | } |
| 460 | |
Elyes Haouas | 1f5e1b4 | 2022-02-17 17:44:07 +0100 | [diff] [blame] | 461 | void acpigen_write_processor_package(const char *const name, const unsigned int first_core, |
Nico Huber | 4d211ac | 2017-09-01 21:14:36 +0200 | [diff] [blame] | 462 | const unsigned int core_count) |
| 463 | { |
| 464 | unsigned int i; |
Nico Huber | 4d211ac | 2017-09-01 21:14:36 +0200 | [diff] [blame] | 465 | |
| 466 | acpigen_write_name(name); |
| 467 | acpigen_write_package(core_count); |
Felix Held | b57b12f | 2023-01-24 19:31:00 +0100 | [diff] [blame] | 468 | |
| 469 | for (i = first_core; i < first_core + core_count; ++i) |
| 470 | acpigen_write_processor_namestring(i); |
| 471 | |
Nico Huber | 4d211ac | 2017-09-01 21:14:36 +0200 | [diff] [blame] | 472 | acpigen_pop_len(); |
| 473 | } |
| 474 | |
Arthur Heymans | 8f05527 | 2018-11-28 13:18:57 +0100 | [diff] [blame] | 475 | /* Method to notify all CPU cores */ |
| 476 | void acpigen_write_processor_cnot(const unsigned int number_of_cores) |
| 477 | { |
| 478 | int core_id; |
| 479 | |
Christian Walter | be3979c | 2019-12-18 15:07:59 +0100 | [diff] [blame] | 480 | acpigen_write_method("\\_SB.CNOT", 1); |
Arthur Heymans | 8f05527 | 2018-11-28 13:18:57 +0100 | [diff] [blame] | 481 | for (core_id = 0; core_id < number_of_cores; core_id++) { |
Arthur Heymans | 8f05527 | 2018-11-28 13:18:57 +0100 | [diff] [blame] | 482 | acpigen_emit_byte(NOTIFY_OP); |
Felix Held | b57b12f | 2023-01-24 19:31:00 +0100 | [diff] [blame] | 483 | acpigen_write_processor_namestring(core_id); |
Arthur Heymans | 8f05527 | 2018-11-28 13:18:57 +0100 | [diff] [blame] | 484 | acpigen_emit_byte(ARG0_OP); |
| 485 | } |
| 486 | acpigen_pop_len(); |
| 487 | } |
| 488 | |
Naresh G Solanki | 8535c66 | 2016-10-25 00:51:24 +0530 | [diff] [blame] | 489 | /* |
| 490 | * Generate ACPI AML code for OperationRegion |
| 491 | * Arg0: Pointer to struct opregion opreg = OPREGION(rname, space, offset, len) |
| 492 | * where rname is region name, space is region space, offset is region offset & |
| 493 | * len is region length. |
| 494 | * OperationRegion(regionname, regionspace, regionoffset, regionlength) |
| 495 | */ |
Duncan Laurie | 3502960 | 2020-10-09 17:50:25 +0000 | [diff] [blame] | 496 | void acpigen_write_opregion(const struct opregion *opreg) |
Naresh G Solanki | 8535c66 | 2016-10-25 00:51:24 +0530 | [diff] [blame] | 497 | { |
| 498 | /* OpregionOp */ |
| 499 | acpigen_emit_ext_op(OPREGION_OP); |
| 500 | /* NameString 4 chars only */ |
| 501 | acpigen_emit_simple_namestring(opreg->name); |
| 502 | /* RegionSpace */ |
| 503 | acpigen_emit_byte(opreg->regionspace); |
| 504 | /* RegionOffset & RegionLen, it can be byte word or double word */ |
| 505 | acpigen_write_integer(opreg->regionoffset); |
| 506 | acpigen_write_integer(opreg->regionlen); |
| 507 | } |
| 508 | |
Patrick Rudolph | 32bae49 | 2019-08-08 12:47:30 +0200 | [diff] [blame] | 509 | /* |
| 510 | * Generate ACPI AML code for Mutex |
| 511 | * Arg0: Pointer to name of mutex |
| 512 | * Arg1: Initial value of mutex |
| 513 | */ |
| 514 | void acpigen_write_mutex(const char *name, const uint8_t flags) |
| 515 | { |
| 516 | /* MutexOp */ |
| 517 | acpigen_emit_ext_op(MUTEX_OP); |
Paweł Anikiel | ed3b688 | 2023-10-13 11:24:19 +0000 | [diff] [blame] | 518 | acpigen_emit_namestring(name); |
Patrick Rudolph | 32bae49 | 2019-08-08 12:47:30 +0200 | [diff] [blame] | 519 | acpigen_emit_byte(flags); |
| 520 | } |
| 521 | |
| 522 | void acpigen_write_acquire(const char *name, const uint16_t val) |
| 523 | { |
| 524 | /* AcquireOp */ |
| 525 | acpigen_emit_ext_op(ACQUIRE_OP); |
Paweł Anikiel | ed3b688 | 2023-10-13 11:24:19 +0000 | [diff] [blame] | 526 | acpigen_emit_namestring(name); |
Patrick Rudolph | 32bae49 | 2019-08-08 12:47:30 +0200 | [diff] [blame] | 527 | acpigen_emit_word(val); |
| 528 | } |
| 529 | |
| 530 | void acpigen_write_release(const char *name) |
| 531 | { |
| 532 | /* ReleaseOp */ |
| 533 | acpigen_emit_ext_op(RELEASE_OP); |
Paweł Anikiel | ed3b688 | 2023-10-13 11:24:19 +0000 | [diff] [blame] | 534 | acpigen_emit_namestring(name); |
Patrick Rudolph | 32bae49 | 2019-08-08 12:47:30 +0200 | [diff] [blame] | 535 | } |
| 536 | |
Patrick Rudolph | 894977b | 2017-07-01 16:15:05 +0200 | [diff] [blame] | 537 | static void acpigen_write_field_length(uint32_t len) |
| 538 | { |
| 539 | uint8_t i, j; |
| 540 | uint8_t emit[4]; |
| 541 | |
| 542 | i = 1; |
| 543 | if (len < 0x40) { |
| 544 | emit[0] = len & 0x3F; |
| 545 | } else { |
| 546 | emit[0] = len & 0xF; |
| 547 | len >>= 4; |
| 548 | while (len) { |
| 549 | emit[i] = len & 0xFF; |
| 550 | i++; |
| 551 | len >>= 8; |
| 552 | } |
| 553 | } |
| 554 | /* Update bit 7:6 : Number of bytes followed by emit[0] */ |
| 555 | emit[0] |= (i - 1) << 6; |
| 556 | |
| 557 | for (j = 0; j < i; j++) |
| 558 | acpigen_emit_byte(emit[j]); |
| 559 | } |
| 560 | |
Elyes Haouas | 1f5e1b4 | 2022-02-17 17:44:07 +0100 | [diff] [blame] | 561 | static void acpigen_write_field_offset(uint32_t offset, uint32_t current_bit_pos) |
Naresh G Solanki | 8535c66 | 2016-10-25 00:51:24 +0530 | [diff] [blame] | 562 | { |
| 563 | uint32_t diff_bits; |
Naresh G Solanki | 8535c66 | 2016-10-25 00:51:24 +0530 | [diff] [blame] | 564 | |
| 565 | if (offset < current_bit_pos) { |
Elyes Haouas | 1f5e1b4 | 2022-02-17 17:44:07 +0100 | [diff] [blame] | 566 | printk(BIOS_WARNING, "%s: Cannot move offset backward", __func__); |
Naresh G Solanki | 8535c66 | 2016-10-25 00:51:24 +0530 | [diff] [blame] | 567 | return; |
| 568 | } |
| 569 | |
| 570 | diff_bits = offset - current_bit_pos; |
| 571 | /* Upper limit */ |
| 572 | if (diff_bits > 0xFFFFFFF) { |
Elyes Haouas | 1f5e1b4 | 2022-02-17 17:44:07 +0100 | [diff] [blame] | 573 | printk(BIOS_WARNING, "%s: Offset very large to encode", __func__); |
Naresh G Solanki | 8535c66 | 2016-10-25 00:51:24 +0530 | [diff] [blame] | 574 | return; |
| 575 | } |
| 576 | |
Naresh G Solanki | 8535c66 | 2016-10-25 00:51:24 +0530 | [diff] [blame] | 577 | acpigen_emit_byte(0); |
Patrick Rudolph | 894977b | 2017-07-01 16:15:05 +0200 | [diff] [blame] | 578 | acpigen_write_field_length(diff_bits); |
| 579 | } |
| 580 | |
Michael Niewöhner | 060dc7b | 2022-05-13 00:04:19 +0200 | [diff] [blame] | 581 | void acpigen_write_field_name(const char *name, uint32_t size) |
Patrick Rudolph | 894977b | 2017-07-01 16:15:05 +0200 | [diff] [blame] | 582 | { |
| 583 | acpigen_emit_simple_namestring(name); |
| 584 | acpigen_write_field_length(size); |
Naresh G Solanki | 8535c66 | 2016-10-25 00:51:24 +0530 | [diff] [blame] | 585 | } |
| 586 | |
Duncan Laurie | 095bbf9 | 2020-09-30 23:09:29 +0000 | [diff] [blame] | 587 | static void acpigen_write_field_reserved(uint32_t size) |
| 588 | { |
| 589 | acpigen_emit_byte(0); |
| 590 | acpigen_write_field_length(size); |
| 591 | } |
| 592 | |
Naresh G Solanki | 8535c66 | 2016-10-25 00:51:24 +0530 | [diff] [blame] | 593 | /* |
| 594 | * Generate ACPI AML code for Field |
| 595 | * Arg0: region name |
| 596 | * Arg1: Pointer to struct fieldlist. |
| 597 | * Arg2: no. of entries in Arg1 |
| 598 | * Arg3: flags which indicate filed access type, lock rule & update rule. |
| 599 | * Example with fieldlist |
| 600 | * struct fieldlist l[] = { |
| 601 | * FIELDLIST_OFFSET(0x84), |
| 602 | * FIELDLIST_NAMESTR("PMCS", 2), |
Duncan Laurie | 095bbf9 | 2020-09-30 23:09:29 +0000 | [diff] [blame] | 603 | * FIELDLIST_RESERVED(6), |
Naresh G Solanki | 8535c66 | 2016-10-25 00:51:24 +0530 | [diff] [blame] | 604 | * }; |
Elyes HAOUAS | a342f39 | 2018-10-17 10:56:26 +0200 | [diff] [blame] | 605 | * acpigen_write_field("UART", l, ARRAY_SIZE(l), FIELD_ANYACC | FIELD_NOLOCK | |
Naresh G Solanki | 8535c66 | 2016-10-25 00:51:24 +0530 | [diff] [blame] | 606 | * FIELD_PRESERVE); |
| 607 | * Output: |
| 608 | * Field (UART, AnyAcc, NoLock, Preserve) |
| 609 | * { |
| 610 | * Offset (0x84), |
Duncan Laurie | 095bbf9 | 2020-09-30 23:09:29 +0000 | [diff] [blame] | 611 | * PMCS, 2, |
| 612 | * , 6, |
Naresh G Solanki | 8535c66 | 2016-10-25 00:51:24 +0530 | [diff] [blame] | 613 | * } |
| 614 | */ |
Furquan Shaikh | f939299 | 2020-04-27 23:18:12 -0700 | [diff] [blame] | 615 | void acpigen_write_field(const char *name, const struct fieldlist *l, size_t count, |
Naresh G Solanki | 8535c66 | 2016-10-25 00:51:24 +0530 | [diff] [blame] | 616 | uint8_t flags) |
| 617 | { |
| 618 | uint16_t i; |
| 619 | uint32_t current_bit_pos = 0; |
| 620 | |
| 621 | /* FieldOp */ |
| 622 | acpigen_emit_ext_op(FIELD_OP); |
| 623 | /* Package Length */ |
| 624 | acpigen_write_len_f(); |
| 625 | /* NameString 4 chars only */ |
| 626 | acpigen_emit_simple_namestring(name); |
| 627 | /* Field Flag */ |
| 628 | acpigen_emit_byte(flags); |
| 629 | |
| 630 | for (i = 0; i < count; i++) { |
| 631 | switch (l[i].type) { |
| 632 | case NAME_STRING: |
Patrick Rudolph | 894977b | 2017-07-01 16:15:05 +0200 | [diff] [blame] | 633 | acpigen_write_field_name(l[i].name, l[i].bits); |
Naresh G Solanki | 8535c66 | 2016-10-25 00:51:24 +0530 | [diff] [blame] | 634 | current_bit_pos += l[i].bits; |
| 635 | break; |
Duncan Laurie | 095bbf9 | 2020-09-30 23:09:29 +0000 | [diff] [blame] | 636 | case RESERVED: |
| 637 | acpigen_write_field_reserved(l[i].bits); |
| 638 | current_bit_pos += l[i].bits; |
| 639 | break; |
Naresh G Solanki | 8535c66 | 2016-10-25 00:51:24 +0530 | [diff] [blame] | 640 | case OFFSET: |
| 641 | acpigen_write_field_offset(l[i].bits, current_bit_pos); |
| 642 | current_bit_pos = l[i].bits; |
| 643 | break; |
| 644 | default: |
Elyes Haouas | 1f5e1b4 | 2022-02-17 17:44:07 +0100 | [diff] [blame] | 645 | printk(BIOS_ERR, "%s: Invalid field type 0x%X\n", __func__, l[i].type); |
Naresh G Solanki | 8535c66 | 2016-10-25 00:51:24 +0530 | [diff] [blame] | 646 | break; |
| 647 | } |
| 648 | } |
| 649 | acpigen_pop_len(); |
| 650 | } |
| 651 | |
Patrick Rudolph | 34846ad | 2019-05-22 11:59:23 +0200 | [diff] [blame] | 652 | /* |
| 653 | * Generate ACPI AML code for IndexField |
| 654 | * Arg0: region name |
| 655 | * Arg1: Pointer to struct fieldlist. |
| 656 | * Arg2: no. of entries in Arg1 |
| 657 | * Arg3: flags which indicate filed access type, lock rule & update rule. |
| 658 | * Example with fieldlist |
| 659 | * struct fieldlist l[] = { |
| 660 | * FIELDLIST_OFFSET(0x84), |
| 661 | * FIELDLIST_NAMESTR("PMCS", 2), |
| 662 | * }; |
| 663 | * acpigen_write_field("IDX", "DATA" l, ARRAY_SIZE(l), FIELD_ANYACC | |
| 664 | * FIELD_NOLOCK | |
| 665 | * FIELD_PRESERVE); |
| 666 | * Output: |
| 667 | * IndexField (IDX, DATA, AnyAcc, NoLock, Preserve) |
| 668 | * { |
| 669 | * Offset (0x84), |
| 670 | * PMCS, 2 |
| 671 | * } |
| 672 | */ |
Elyes Haouas | 1f5e1b4 | 2022-02-17 17:44:07 +0100 | [diff] [blame] | 673 | void acpigen_write_indexfield(const char *idx, const char *data, struct fieldlist *l, |
| 674 | size_t count, uint8_t flags) |
Patrick Rudolph | 34846ad | 2019-05-22 11:59:23 +0200 | [diff] [blame] | 675 | { |
| 676 | uint16_t i; |
| 677 | uint32_t current_bit_pos = 0; |
| 678 | |
| 679 | /* FieldOp */ |
| 680 | acpigen_emit_ext_op(INDEX_FIELD_OP); |
| 681 | /* Package Length */ |
| 682 | acpigen_write_len_f(); |
| 683 | /* NameString 4 chars only */ |
| 684 | acpigen_emit_simple_namestring(idx); |
| 685 | /* NameString 4 chars only */ |
| 686 | acpigen_emit_simple_namestring(data); |
| 687 | /* Field Flag */ |
| 688 | acpigen_emit_byte(flags); |
| 689 | |
| 690 | for (i = 0; i < count; i++) { |
| 691 | switch (l[i].type) { |
| 692 | case NAME_STRING: |
| 693 | acpigen_write_field_name(l[i].name, l[i].bits); |
| 694 | current_bit_pos += l[i].bits; |
| 695 | break; |
| 696 | case OFFSET: |
| 697 | acpigen_write_field_offset(l[i].bits, current_bit_pos); |
| 698 | current_bit_pos = l[i].bits; |
| 699 | break; |
| 700 | default: |
Elyes Haouas | 1f5e1b4 | 2022-02-17 17:44:07 +0100 | [diff] [blame] | 701 | printk(BIOS_ERR, "%s: Invalid field type 0x%X\n", __func__, l[i].type); |
Patrick Rudolph | 34846ad | 2019-05-22 11:59:23 +0200 | [diff] [blame] | 702 | break; |
| 703 | } |
| 704 | } |
| 705 | acpigen_pop_len(); |
| 706 | } |
| 707 | |
Vladimir Serbinenko | 9bb5c5c | 2014-11-09 03:51:32 +0100 | [diff] [blame] | 708 | void acpigen_write_empty_PCT(void) |
Rudolf Marek | f997b55 | 2009-02-14 15:40:23 +0000 | [diff] [blame] | 709 | { |
| 710 | /* |
Elyes HAOUAS | 6b72787 | 2016-09-03 08:28:48 +0200 | [diff] [blame] | 711 | Name (_PCT, Package (0x02) |
| 712 | { |
| 713 | ResourceTemplate () |
| 714 | { |
| 715 | Register (FFixedHW, |
| 716 | 0x00, // Bit Width |
| 717 | 0x00, // Bit Offset |
| 718 | 0x0000000000000000, // Address |
| 719 | ,) |
| 720 | }, |
Rudolf Marek | f997b55 | 2009-02-14 15:40:23 +0000 | [diff] [blame] | 721 | |
Elyes HAOUAS | 6b72787 | 2016-09-03 08:28:48 +0200 | [diff] [blame] | 722 | ResourceTemplate () |
| 723 | { |
| 724 | Register (FFixedHW, |
| 725 | 0x00, // Bit Width |
| 726 | 0x00, // Bit Offset |
| 727 | 0x0000000000000000, // Address |
| 728 | ,) |
| 729 | } |
| 730 | }) |
Rudolf Marek | f997b55 | 2009-02-14 15:40:23 +0000 | [diff] [blame] | 731 | */ |
| 732 | static char stream[] = { |
Lee Leahy | 6f80ccc | 2017-03-16 15:18:22 -0700 | [diff] [blame] | 733 | /* 00000030 "0._PCT.," */ |
| 734 | 0x08, 0x5F, 0x50, 0x43, 0x54, 0x12, 0x2C, |
| 735 | /* 00000038 "........" */ |
| 736 | 0x02, 0x11, 0x14, 0x0A, 0x11, 0x82, 0x0C, 0x00, |
| 737 | /* 00000040 "........" */ |
| 738 | 0x7F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 739 | /* 00000048 "....y..." */ |
| 740 | 0x00, 0x00, 0x00, 0x00, 0x79, 0x00, 0x11, 0x14, |
| 741 | /* 00000050 "........" */ |
| 742 | 0x0A, 0x11, 0x82, 0x0C, 0x00, 0x7F, 0x00, 0x00, |
| 743 | /* 00000058 "........" */ |
| 744 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
Rudolf Marek | f997b55 | 2009-02-14 15:40:23 +0000 | [diff] [blame] | 745 | 0x00, 0x79, 0x00 |
| 746 | }; |
Vladimir Serbinenko | 9bb5c5c | 2014-11-09 03:51:32 +0100 | [diff] [blame] | 747 | acpigen_emit_stream(stream, ARRAY_SIZE(stream)); |
Rudolf Marek | f997b55 | 2009-02-14 15:40:23 +0000 | [diff] [blame] | 748 | } |
| 749 | |
Kyösti Mälkki | ddc37d6 | 2023-04-17 12:11:03 +0300 | [diff] [blame] | 750 | void acpigen_write_PTC(uint8_t duty_width, uint8_t duty_offset, uint16_t p_cnt) |
Stefan Reinauer | 39205c6 | 2012-04-27 21:49:28 +0200 | [diff] [blame] | 751 | { |
| 752 | /* |
Elyes HAOUAS | 6b72787 | 2016-09-03 08:28:48 +0200 | [diff] [blame] | 753 | Name (_PTC, Package (0x02) |
| 754 | { |
| 755 | ResourceTemplate () |
| 756 | { |
| 757 | Register (FFixedHW, |
Kyösti Mälkki | ddc37d6 | 2023-04-17 12:11:03 +0300 | [diff] [blame] | 758 | 0x00, // Duty Width |
| 759 | 0x00, // Duty Offset |
| 760 | 0x0000000000000000, // P_CNT IO Address |
Elyes HAOUAS | 6b72787 | 2016-09-03 08:28:48 +0200 | [diff] [blame] | 761 | ,) |
| 762 | }, |
Stefan Reinauer | 39205c6 | 2012-04-27 21:49:28 +0200 | [diff] [blame] | 763 | |
Elyes HAOUAS | 6b72787 | 2016-09-03 08:28:48 +0200 | [diff] [blame] | 764 | ResourceTemplate () |
| 765 | { |
| 766 | Register (FFixedHW, |
Kyösti Mälkki | ddc37d6 | 2023-04-17 12:11:03 +0300 | [diff] [blame] | 767 | 0x00, // Duty Width |
| 768 | 0x00, // Duty Offset |
| 769 | 0x0000000000000000, // P_CNT IO Address |
Elyes HAOUAS | 6b72787 | 2016-09-03 08:28:48 +0200 | [diff] [blame] | 770 | ,) |
| 771 | } |
| 772 | }) |
Stefan Reinauer | 39205c6 | 2012-04-27 21:49:28 +0200 | [diff] [blame] | 773 | */ |
Stefan Reinauer | 39205c6 | 2012-04-27 21:49:28 +0200 | [diff] [blame] | 774 | acpi_addr_t addr = { |
Kyösti Mälkki | ddc37d6 | 2023-04-17 12:11:03 +0300 | [diff] [blame] | 775 | .bit_width = duty_width, |
| 776 | .bit_offset = duty_offset, |
Elyes HAOUAS | 721cb8a | 2020-06-23 09:13:42 +0200 | [diff] [blame] | 777 | .access_size = ACPI_ACCESS_SIZE_UNDEFINED, |
Kyösti Mälkki | ddc37d6 | 2023-04-17 12:11:03 +0300 | [diff] [blame] | 778 | .addrl = p_cnt, |
Elyes HAOUAS | 8ee161d | 2019-03-03 12:49:56 +0100 | [diff] [blame] | 779 | .addrh = 0, |
Stefan Reinauer | 39205c6 | 2012-04-27 21:49:28 +0200 | [diff] [blame] | 780 | }; |
| 781 | |
Kyösti Mälkki | ddc37d6 | 2023-04-17 12:11:03 +0300 | [diff] [blame] | 782 | if (addr.addrl != 0) |
| 783 | addr.space_id = ACPI_ADDRESS_SPACE_IO; |
| 784 | else |
| 785 | addr.space_id = ACPI_ADDRESS_SPACE_FIXED; |
| 786 | |
Vladimir Serbinenko | 9bb5c5c | 2014-11-09 03:51:32 +0100 | [diff] [blame] | 787 | acpigen_write_name("_PTC"); |
| 788 | acpigen_write_package(2); |
Stefan Reinauer | 39205c6 | 2012-04-27 21:49:28 +0200 | [diff] [blame] | 789 | |
| 790 | /* ControlRegister */ |
Matt Delco | c3f9d7a | 2018-07-27 14:01:05 -0700 | [diff] [blame] | 791 | acpigen_write_register_resource(&addr); |
Stefan Reinauer | 39205c6 | 2012-04-27 21:49:28 +0200 | [diff] [blame] | 792 | |
| 793 | /* StatusRegister */ |
Matt Delco | c3f9d7a | 2018-07-27 14:01:05 -0700 | [diff] [blame] | 794 | acpigen_write_register_resource(&addr); |
Stefan Reinauer | 39205c6 | 2012-04-27 21:49:28 +0200 | [diff] [blame] | 795 | |
Vladimir Serbinenko | a09f4db | 2014-11-09 03:32:58 +0100 | [diff] [blame] | 796 | acpigen_pop_len(); |
Stefan Reinauer | 39205c6 | 2012-04-27 21:49:28 +0200 | [diff] [blame] | 797 | } |
| 798 | |
Kyösti Mälkki | ddc37d6 | 2023-04-17 12:11:03 +0300 | [diff] [blame] | 799 | void acpigen_write_empty_PTC(void) |
| 800 | { |
| 801 | acpigen_write_PTC(0, 0, 0); |
| 802 | } |
| 803 | |
Furquan Shaikh | fcc7a04 | 2016-10-20 23:12:38 -0700 | [diff] [blame] | 804 | static void __acpigen_write_method(const char *name, uint8_t flags) |
Vladimir Serbinenko | 80fb8ed | 2014-11-05 10:28:28 +0100 | [diff] [blame] | 805 | { |
Furquan Shaikh | e4e9b94 | 2016-10-20 23:09:05 -0700 | [diff] [blame] | 806 | acpigen_emit_byte(METHOD_OP); |
Vladimir Serbinenko | 9bb5c5c | 2014-11-09 03:51:32 +0100 | [diff] [blame] | 807 | acpigen_write_len_f(); |
| 808 | acpigen_emit_namestring(name); |
Furquan Shaikh | fcc7a04 | 2016-10-20 23:12:38 -0700 | [diff] [blame] | 809 | acpigen_emit_byte(flags); |
| 810 | } |
| 811 | |
| 812 | /* Method (name, nargs, NotSerialized) */ |
| 813 | void acpigen_write_method(const char *name, int nargs) |
| 814 | { |
| 815 | __acpigen_write_method(name, (nargs & 7)); |
| 816 | } |
| 817 | |
| 818 | /* Method (name, nargs, Serialized) */ |
| 819 | void acpigen_write_method_serialized(const char *name, int nargs) |
| 820 | { |
| 821 | __acpigen_write_method(name, (nargs & 7) | (1 << 3)); |
Vladimir Serbinenko | 80fb8ed | 2014-11-05 10:28:28 +0100 | [diff] [blame] | 822 | } |
| 823 | |
Vladimir Serbinenko | 9bb5c5c | 2014-11-09 03:51:32 +0100 | [diff] [blame] | 824 | void acpigen_write_device(const char *name) |
Vladimir Serbinenko | 663be6e | 2014-11-05 21:29:45 +0100 | [diff] [blame] | 825 | { |
Furquan Shaikh | e4e9b94 | 2016-10-20 23:09:05 -0700 | [diff] [blame] | 826 | acpigen_emit_ext_op(DEVICE_OP); |
Vladimir Serbinenko | 9bb5c5c | 2014-11-09 03:51:32 +0100 | [diff] [blame] | 827 | acpigen_write_len_f(); |
| 828 | acpigen_emit_namestring(name); |
Vladimir Serbinenko | 663be6e | 2014-11-05 21:29:45 +0100 | [diff] [blame] | 829 | } |
| 830 | |
Raul E Rangel | 052c963 | 2021-05-12 17:01:30 -0600 | [diff] [blame] | 831 | void acpigen_write_thermal_zone(const char *name) |
| 832 | { |
| 833 | acpigen_emit_ext_op(THERMAL_ZONE_OP); |
| 834 | acpigen_write_len_f(); |
| 835 | acpigen_emit_namestring(name); |
| 836 | } |
| 837 | |
Duncan Laurie | abe2de8 | 2016-05-09 11:08:46 -0700 | [diff] [blame] | 838 | void acpigen_write_STA(uint8_t status) |
| 839 | { |
| 840 | /* |
| 841 | * Method (_STA, 0, NotSerialized) { Return (status) } |
| 842 | */ |
| 843 | acpigen_write_method("_STA", 0); |
Furquan Shaikh | e4e9b94 | 2016-10-20 23:09:05 -0700 | [diff] [blame] | 844 | acpigen_emit_byte(RETURN_OP); |
Duncan Laurie | abe2de8 | 2016-05-09 11:08:46 -0700 | [diff] [blame] | 845 | acpigen_write_byte(status); |
| 846 | acpigen_pop_len(); |
| 847 | } |
| 848 | |
Sugnan Prabhu S | 4f77f61 | 2020-06-10 09:51:02 +0530 | [diff] [blame] | 849 | void acpigen_write_STA_ext(const char *namestring) |
| 850 | { |
| 851 | /* |
| 852 | * Method (_STA, 0, NotSerialized) { Return (ext_val) } |
| 853 | */ |
| 854 | acpigen_write_method("_STA", 0); |
| 855 | acpigen_emit_byte(RETURN_OP); |
| 856 | acpigen_emit_namestring(namestring); |
| 857 | acpigen_pop_len(); |
| 858 | } |
| 859 | |
Felix Held | 2d4112f | 2023-05-04 23:00:06 +0200 | [diff] [blame] | 860 | void acpigen_write_BBN(uint8_t base_bus_number) |
| 861 | { |
| 862 | /* |
| 863 | * Method (_BBN, 0, NotSerialized) { Return (status) } |
| 864 | */ |
| 865 | acpigen_write_method("_BBN", 0); |
| 866 | acpigen_emit_byte(RETURN_OP); |
| 867 | acpigen_write_byte(base_bus_number); |
| 868 | acpigen_pop_len(); |
| 869 | } |
| 870 | |
Felix Held | 75c4d44 | 2024-01-11 23:57:38 +0100 | [diff] [blame] | 871 | void acpigen_write_SEG(uint8_t segment_group_number) |
| 872 | { |
| 873 | /* |
| 874 | * Method (_SEG, 0, NotSerialized) { Return (status) } |
| 875 | */ |
| 876 | acpigen_write_method("_SEG", 0); |
| 877 | acpigen_emit_byte(RETURN_OP); |
| 878 | acpigen_write_byte(segment_group_number); |
| 879 | acpigen_pop_len(); |
| 880 | } |
| 881 | |
Raul E Rangel | c704832 | 2021-04-19 15:58:25 -0600 | [diff] [blame] | 882 | void acpigen_write_LPI_package(u64 level, const struct acpi_lpi_state *states, u16 nentries) |
| 883 | { |
| 884 | /* |
| 885 | * Name (_LPI, Package (0x06) // _LPI: Low Power Idle States |
| 886 | * { |
| 887 | * 0x0000, |
| 888 | * 0x0000000000000000, |
| 889 | * 0x0003, |
| 890 | * Package (0x0A) |
| 891 | * { |
| 892 | * 0x00000002, |
| 893 | * 0x00000001, |
| 894 | * 0x00000001, |
| 895 | * 0x00000000, |
| 896 | * 0x00000000, |
| 897 | * 0x00000000, |
| 898 | * ResourceTemplate () |
| 899 | * { |
| 900 | * Register (FFixedHW, |
| 901 | * 0x02, // Bit Width |
| 902 | * 0x02, // Bit Offset |
| 903 | * 0x0000000000000000, // Address |
| 904 | * ,) |
| 905 | * }, |
| 906 | * |
| 907 | * ResourceTemplate () |
| 908 | * { |
| 909 | * Register (SystemMemory, |
| 910 | * 0x00, // Bit Width |
| 911 | * 0x00, // Bit Offset |
| 912 | * 0x0000000000000000, // Address |
| 913 | * ,) |
| 914 | * }, |
| 915 | * |
| 916 | * ResourceTemplate () |
| 917 | * { |
| 918 | * Register (SystemMemory, |
| 919 | * 0x00, // Bit Width |
| 920 | * 0x00, // Bit Offset |
| 921 | * 0x0000000000000000, // Address |
| 922 | * ,) |
| 923 | * }, |
| 924 | * |
| 925 | * "C1" |
| 926 | * }, |
| 927 | * ... |
| 928 | * } |
| 929 | */ |
| 930 | |
| 931 | acpigen_write_name("_LPI"); |
| 932 | acpigen_write_package(3 + nentries); |
| 933 | acpigen_write_word(0); /* Revision */ |
| 934 | acpigen_write_qword(level); |
| 935 | acpigen_write_word(nentries); |
| 936 | |
| 937 | for (size_t i = 0; i < nentries; i++, states++) { |
| 938 | acpigen_write_package(0xA); |
| 939 | acpigen_write_dword(states->min_residency_us); |
| 940 | acpigen_write_dword(states->worst_case_wakeup_latency_us); |
| 941 | acpigen_write_dword(states->flags); |
| 942 | acpigen_write_dword(states->arch_context_lost_flags); |
| 943 | acpigen_write_dword(states->residency_counter_frequency_hz); |
| 944 | acpigen_write_dword(states->enabled_parent_state); |
| 945 | acpigen_write_register_resource(&states->entry_method); |
| 946 | acpigen_write_register_resource(&states->residency_counter_register); |
| 947 | acpigen_write_register_resource(&states->usage_counter_register); |
| 948 | acpigen_write_string(states->state_name); |
| 949 | acpigen_pop_len(); |
| 950 | } |
| 951 | acpigen_pop_len(); |
| 952 | } |
| 953 | |
Paul Menzel | 0f4c0e2 | 2013-02-22 12:33:08 +0100 | [diff] [blame] | 954 | /* |
| 955 | * Generates a func with max supported P-states. |
| 956 | */ |
Vladimir Serbinenko | 9bb5c5c | 2014-11-09 03:51:32 +0100 | [diff] [blame] | 957 | void acpigen_write_PPC(u8 nr) |
Rudolf Marek | f997b55 | 2009-02-14 15:40:23 +0000 | [diff] [blame] | 958 | { |
| 959 | /* |
Elyes HAOUAS | 6b72787 | 2016-09-03 08:28:48 +0200 | [diff] [blame] | 960 | Method (_PPC, 0, NotSerialized) |
| 961 | { |
| 962 | Return (nr) |
| 963 | } |
Rudolf Marek | f997b55 | 2009-02-14 15:40:23 +0000 | [diff] [blame] | 964 | */ |
Vladimir Serbinenko | 9bb5c5c | 2014-11-09 03:51:32 +0100 | [diff] [blame] | 965 | acpigen_write_method("_PPC", 0); |
Furquan Shaikh | e4e9b94 | 2016-10-20 23:09:05 -0700 | [diff] [blame] | 966 | acpigen_emit_byte(RETURN_OP); |
Rudolf Marek | f997b55 | 2009-02-14 15:40:23 +0000 | [diff] [blame] | 967 | /* arg */ |
Vladimir Serbinenko | 9bb5c5c | 2014-11-09 03:51:32 +0100 | [diff] [blame] | 968 | acpigen_write_byte(nr); |
Vladimir Serbinenko | a09f4db | 2014-11-09 03:32:58 +0100 | [diff] [blame] | 969 | acpigen_pop_len(); |
Rudolf Marek | f997b55 | 2009-02-14 15:40:23 +0000 | [diff] [blame] | 970 | } |
| 971 | |
Paul Menzel | 0f4c0e2 | 2013-02-22 12:33:08 +0100 | [diff] [blame] | 972 | /* |
| 973 | * Generates a func with max supported P-states saved |
| 974 | * in the variable PPCM. |
| 975 | */ |
Vladimir Serbinenko | 9bb5c5c | 2014-11-09 03:51:32 +0100 | [diff] [blame] | 976 | void acpigen_write_PPC_NVS(void) |
Duncan Laurie | 0eefa00 | 2012-07-16 12:11:53 -0700 | [diff] [blame] | 977 | { |
| 978 | /* |
Elyes HAOUAS | 6b72787 | 2016-09-03 08:28:48 +0200 | [diff] [blame] | 979 | Method (_PPC, 0, NotSerialized) |
| 980 | { |
| 981 | Return (PPCM) |
| 982 | } |
Duncan Laurie | 0eefa00 | 2012-07-16 12:11:53 -0700 | [diff] [blame] | 983 | */ |
Vladimir Serbinenko | 9bb5c5c | 2014-11-09 03:51:32 +0100 | [diff] [blame] | 984 | acpigen_write_method("_PPC", 0); |
Furquan Shaikh | e4e9b94 | 2016-10-20 23:09:05 -0700 | [diff] [blame] | 985 | acpigen_emit_byte(RETURN_OP); |
Duncan Laurie | 0eefa00 | 2012-07-16 12:11:53 -0700 | [diff] [blame] | 986 | /* arg */ |
Vladimir Serbinenko | 9bb5c5c | 2014-11-09 03:51:32 +0100 | [diff] [blame] | 987 | acpigen_emit_namestring("PPCM"); |
Vladimir Serbinenko | a09f4db | 2014-11-09 03:32:58 +0100 | [diff] [blame] | 988 | acpigen_pop_len(); |
Duncan Laurie | 0eefa00 | 2012-07-16 12:11:53 -0700 | [diff] [blame] | 989 | } |
| 990 | |
Vladimir Serbinenko | 9bb5c5c | 2014-11-09 03:51:32 +0100 | [diff] [blame] | 991 | void acpigen_write_TPC(const char *gnvs_tpc_limit) |
Stefan Reinauer | 39205c6 | 2012-04-27 21:49:28 +0200 | [diff] [blame] | 992 | { |
| 993 | /* |
Elyes HAOUAS | 6b72787 | 2016-09-03 08:28:48 +0200 | [diff] [blame] | 994 | // Sample _TPC method |
| 995 | Method (_TPC, 0, NotSerialized) |
| 996 | { |
| 997 | Return (\TLVL) |
| 998 | } |
| 999 | */ |
Vladimir Serbinenko | 9bb5c5c | 2014-11-09 03:51:32 +0100 | [diff] [blame] | 1000 | acpigen_write_method("_TPC", 0); |
Furquan Shaikh | e4e9b94 | 2016-10-20 23:09:05 -0700 | [diff] [blame] | 1001 | acpigen_emit_byte(RETURN_OP); |
Vladimir Serbinenko | 9bb5c5c | 2014-11-09 03:51:32 +0100 | [diff] [blame] | 1002 | acpigen_emit_namestring(gnvs_tpc_limit); |
Vladimir Serbinenko | a09f4db | 2014-11-09 03:32:58 +0100 | [diff] [blame] | 1003 | acpigen_pop_len(); |
Stefan Reinauer | 39205c6 | 2012-04-27 21:49:28 +0200 | [diff] [blame] | 1004 | } |
| 1005 | |
Duncan Laurie | abe2de8 | 2016-05-09 11:08:46 -0700 | [diff] [blame] | 1006 | void acpigen_write_PRW(u32 wake, u32 level) |
| 1007 | { |
| 1008 | /* |
| 1009 | * Name (_PRW, Package () { wake, level } |
| 1010 | */ |
| 1011 | acpigen_write_name("_PRW"); |
| 1012 | acpigen_write_package(2); |
| 1013 | acpigen_write_integer(wake); |
| 1014 | acpigen_write_integer(level); |
| 1015 | acpigen_pop_len(); |
| 1016 | } |
| 1017 | |
Elyes Haouas | 1f5e1b4 | 2022-02-17 17:44:07 +0100 | [diff] [blame] | 1018 | void acpigen_write_PSS_package(u32 coreFreq, u32 power, u32 transLat, u32 busmLat, u32 control, |
| 1019 | u32 status) |
Rudolf Marek | f997b55 | 2009-02-14 15:40:23 +0000 | [diff] [blame] | 1020 | { |
Vladimir Serbinenko | 9bb5c5c | 2014-11-09 03:51:32 +0100 | [diff] [blame] | 1021 | acpigen_write_package(6); |
| 1022 | acpigen_write_dword(coreFreq); |
| 1023 | acpigen_write_dword(power); |
| 1024 | acpigen_write_dword(transLat); |
| 1025 | acpigen_write_dword(busmLat); |
| 1026 | acpigen_write_dword(control); |
| 1027 | acpigen_write_dword(status); |
Vladimir Serbinenko | a09f4db | 2014-11-09 03:32:58 +0100 | [diff] [blame] | 1028 | acpigen_pop_len(); |
Stefan Reinauer | d4bacf9 | 2012-05-02 16:38:47 -0700 | [diff] [blame] | 1029 | |
Elyes Haouas | 1f5e1b4 | 2022-02-17 17:44:07 +0100 | [diff] [blame] | 1030 | printk(BIOS_DEBUG, "PSS: %uMHz power %u control 0x%x status 0x%x\n", coreFreq, power, |
| 1031 | control, status); |
Rudolf Marek | f997b55 | 2009-02-14 15:40:23 +0000 | [diff] [blame] | 1032 | } |
Patrick Georgi | df444bf | 2009-04-21 20:34:36 +0000 | [diff] [blame] | 1033 | |
Jason Glenesk | ca36aed | 2020-09-15 21:01:57 -0700 | [diff] [blame] | 1034 | void acpigen_write_pss_object(const struct acpi_sw_pstate *pstate_values, size_t nentries) |
| 1035 | { |
| 1036 | size_t pstate; |
| 1037 | |
| 1038 | acpigen_write_name("_PSS"); |
| 1039 | acpigen_write_package(nentries); |
| 1040 | for (pstate = 0; pstate < nentries; pstate++) { |
| 1041 | acpigen_write_PSS_package( |
| 1042 | pstate_values->core_freq, pstate_values->power, |
| 1043 | pstate_values->transition_latency, pstate_values->bus_master_latency, |
| 1044 | pstate_values->control_value, pstate_values->status_value); |
| 1045 | pstate_values++; |
| 1046 | } |
| 1047 | |
| 1048 | acpigen_pop_len(); |
| 1049 | } |
| 1050 | |
Vladimir Serbinenko | 9bb5c5c | 2014-11-09 03:51:32 +0100 | [diff] [blame] | 1051 | void acpigen_write_PSD_package(u32 domain, u32 numprocs, PSD_coord coordtype) |
Patrick Georgi | df444bf | 2009-04-21 20:34:36 +0000 | [diff] [blame] | 1052 | { |
Vladimir Serbinenko | 9bb5c5c | 2014-11-09 03:51:32 +0100 | [diff] [blame] | 1053 | acpigen_write_name("_PSD"); |
| 1054 | acpigen_write_package(1); |
| 1055 | acpigen_write_package(5); |
| 1056 | acpigen_write_byte(5); // 5 values |
| 1057 | acpigen_write_byte(0); // revision 0 |
| 1058 | acpigen_write_dword(domain); |
| 1059 | acpigen_write_dword(coordtype); |
| 1060 | acpigen_write_dword(numprocs); |
Vladimir Serbinenko | a09f4db | 2014-11-09 03:32:58 +0100 | [diff] [blame] | 1061 | acpigen_pop_len(); |
Vladimir Serbinenko | a09f4db | 2014-11-09 03:32:58 +0100 | [diff] [blame] | 1062 | acpigen_pop_len(); |
Patrick Georgi | df444bf | 2009-04-21 20:34:36 +0000 | [diff] [blame] | 1063 | } |
Tobias Diedrich | 0fe6e9a | 2010-11-17 16:27:06 +0000 | [diff] [blame] | 1064 | |
Angel Pons | d2794ce | 2021-10-17 12:59:43 +0200 | [diff] [blame] | 1065 | void acpigen_write_CST_package_entry(const acpi_cstate_t *cstate) |
Sven Schnelle | 0b86c76 | 2011-10-21 21:46:47 +0200 | [diff] [blame] | 1066 | { |
Vladimir Serbinenko | 9bb5c5c | 2014-11-09 03:51:32 +0100 | [diff] [blame] | 1067 | acpigen_write_package(4); |
Matt Delco | c3f9d7a | 2018-07-27 14:01:05 -0700 | [diff] [blame] | 1068 | acpigen_write_register_resource(&cstate->resource); |
Jason Glenesk | 201acca | 2020-09-11 12:36:15 -0700 | [diff] [blame] | 1069 | acpigen_write_byte(cstate->ctype); |
| 1070 | acpigen_write_word(cstate->latency); |
Vladimir Serbinenko | 9bb5c5c | 2014-11-09 03:51:32 +0100 | [diff] [blame] | 1071 | acpigen_write_dword(cstate->power); |
Vladimir Serbinenko | a09f4db | 2014-11-09 03:32:58 +0100 | [diff] [blame] | 1072 | acpigen_pop_len(); |
Sven Schnelle | 0b86c76 | 2011-10-21 21:46:47 +0200 | [diff] [blame] | 1073 | } |
| 1074 | |
Angel Pons | d2794ce | 2021-10-17 12:59:43 +0200 | [diff] [blame] | 1075 | void acpigen_write_CST_package(const acpi_cstate_t *cstate, int nentries) |
Sven Schnelle | 0b86c76 | 2011-10-21 21:46:47 +0200 | [diff] [blame] | 1076 | { |
Vladimir Serbinenko | 9bb5c5c | 2014-11-09 03:51:32 +0100 | [diff] [blame] | 1077 | int i; |
| 1078 | acpigen_write_name("_CST"); |
| 1079 | acpigen_write_package(nentries+1); |
Jason Glenesk | 201acca | 2020-09-11 12:36:15 -0700 | [diff] [blame] | 1080 | acpigen_write_integer(nentries); |
Sven Schnelle | 0b86c76 | 2011-10-21 21:46:47 +0200 | [diff] [blame] | 1081 | |
| 1082 | for (i = 0; i < nentries; i++) |
Vladimir Serbinenko | 9bb5c5c | 2014-11-09 03:51:32 +0100 | [diff] [blame] | 1083 | acpigen_write_CST_package_entry(cstate + i); |
Sven Schnelle | 0b86c76 | 2011-10-21 21:46:47 +0200 | [diff] [blame] | 1084 | |
Vladimir Serbinenko | a09f4db | 2014-11-09 03:32:58 +0100 | [diff] [blame] | 1085 | acpigen_pop_len(); |
Sven Schnelle | 0b86c76 | 2011-10-21 21:46:47 +0200 | [diff] [blame] | 1086 | } |
| 1087 | |
Lee Leahy | 6f80ccc | 2017-03-16 15:18:22 -0700 | [diff] [blame] | 1088 | void acpigen_write_CSD_package(u32 domain, u32 numprocs, CSD_coord coordtype, |
| 1089 | u32 index) |
Timothy Pearson | 83abd81 | 2015-06-08 19:35:06 -0500 | [diff] [blame] | 1090 | { |
| 1091 | acpigen_write_name("_CSD"); |
| 1092 | acpigen_write_package(1); |
| 1093 | acpigen_write_package(6); |
Jason Glenesk | 201acca | 2020-09-11 12:36:15 -0700 | [diff] [blame] | 1094 | acpigen_write_integer(6); // 6 values |
Timothy Pearson | 83abd81 | 2015-06-08 19:35:06 -0500 | [diff] [blame] | 1095 | acpigen_write_byte(0); // revision 0 |
| 1096 | acpigen_write_dword(domain); |
| 1097 | acpigen_write_dword(coordtype); |
| 1098 | acpigen_write_dword(numprocs); |
| 1099 | acpigen_write_dword(index); |
| 1100 | acpigen_pop_len(); |
| 1101 | acpigen_pop_len(); |
| 1102 | } |
| 1103 | |
Vladimir Serbinenko | 9bb5c5c | 2014-11-09 03:51:32 +0100 | [diff] [blame] | 1104 | void acpigen_write_TSS_package(int entries, acpi_tstate_t *tstate_list) |
Stefan Reinauer | 39205c6 | 2012-04-27 21:49:28 +0200 | [diff] [blame] | 1105 | { |
| 1106 | /* |
Elyes HAOUAS | 6b72787 | 2016-09-03 08:28:48 +0200 | [diff] [blame] | 1107 | Sample _TSS package with 100% and 50% duty cycles |
| 1108 | Name (_TSS, Package (0x02) |
| 1109 | { |
| 1110 | Package(){100, 1000, 0, 0x00, 0) |
| 1111 | Package(){50, 520, 0, 0x18, 0) |
| 1112 | }) |
| 1113 | */ |
Vladimir Serbinenko | 9bb5c5c | 2014-11-09 03:51:32 +0100 | [diff] [blame] | 1114 | int i; |
Stefan Reinauer | 39205c6 | 2012-04-27 21:49:28 +0200 | [diff] [blame] | 1115 | acpi_tstate_t *tstate = tstate_list; |
| 1116 | |
Vladimir Serbinenko | 9bb5c5c | 2014-11-09 03:51:32 +0100 | [diff] [blame] | 1117 | acpigen_write_name("_TSS"); |
| 1118 | acpigen_write_package(entries); |
Stefan Reinauer | 39205c6 | 2012-04-27 21:49:28 +0200 | [diff] [blame] | 1119 | |
| 1120 | for (i = 0; i < entries; i++) { |
Vladimir Serbinenko | 9bb5c5c | 2014-11-09 03:51:32 +0100 | [diff] [blame] | 1121 | acpigen_write_package(5); |
| 1122 | acpigen_write_dword(tstate->percent); |
| 1123 | acpigen_write_dword(tstate->power); |
| 1124 | acpigen_write_dword(tstate->latency); |
| 1125 | acpigen_write_dword(tstate->control); |
| 1126 | acpigen_write_dword(tstate->status); |
Vladimir Serbinenko | a09f4db | 2014-11-09 03:32:58 +0100 | [diff] [blame] | 1127 | acpigen_pop_len(); |
Stefan Reinauer | 39205c6 | 2012-04-27 21:49:28 +0200 | [diff] [blame] | 1128 | tstate++; |
Stefan Reinauer | 39205c6 | 2012-04-27 21:49:28 +0200 | [diff] [blame] | 1129 | } |
| 1130 | |
Vladimir Serbinenko | a09f4db | 2014-11-09 03:32:58 +0100 | [diff] [blame] | 1131 | acpigen_pop_len(); |
Stefan Reinauer | 39205c6 | 2012-04-27 21:49:28 +0200 | [diff] [blame] | 1132 | } |
| 1133 | |
Vladimir Serbinenko | 9bb5c5c | 2014-11-09 03:51:32 +0100 | [diff] [blame] | 1134 | void acpigen_write_TSD_package(u32 domain, u32 numprocs, PSD_coord coordtype) |
Stefan Reinauer | 39205c6 | 2012-04-27 21:49:28 +0200 | [diff] [blame] | 1135 | { |
Vladimir Serbinenko | 9bb5c5c | 2014-11-09 03:51:32 +0100 | [diff] [blame] | 1136 | acpigen_write_name("_TSD"); |
| 1137 | acpigen_write_package(1); |
| 1138 | acpigen_write_package(5); |
| 1139 | acpigen_write_byte(5); // 5 values |
| 1140 | acpigen_write_byte(0); // revision 0 |
| 1141 | acpigen_write_dword(domain); |
| 1142 | acpigen_write_dword(coordtype); |
| 1143 | acpigen_write_dword(numprocs); |
Vladimir Serbinenko | a09f4db | 2014-11-09 03:32:58 +0100 | [diff] [blame] | 1144 | acpigen_pop_len(); |
Vladimir Serbinenko | a09f4db | 2014-11-09 03:32:58 +0100 | [diff] [blame] | 1145 | acpigen_pop_len(); |
Stefan Reinauer | 39205c6 | 2012-04-27 21:49:28 +0200 | [diff] [blame] | 1146 | } |
| 1147 | |
Vladimir Serbinenko | 9bb5c5c | 2014-11-09 03:51:32 +0100 | [diff] [blame] | 1148 | void acpigen_write_mem32fixed(int readwrite, u32 base, u32 size) |
Tobias Diedrich | 0fe6e9a | 2010-11-17 16:27:06 +0000 | [diff] [blame] | 1149 | { |
| 1150 | /* |
Elyes HAOUAS | 2078e75 | 2016-08-21 10:41:44 +0200 | [diff] [blame] | 1151 | * ACPI 4.0 section 6.4.3.4: 32-Bit Fixed Memory Range Descriptor |
Tobias Diedrich | 0fe6e9a | 2010-11-17 16:27:06 +0000 | [diff] [blame] | 1152 | * Byte 0: |
| 1153 | * Bit7 : 1 => big item |
| 1154 | * Bit6-0: 0000110 (0x6) => 32-bit fixed memory |
| 1155 | */ |
| 1156 | acpigen_emit_byte(0x86); |
| 1157 | /* Byte 1+2: length (0x0009) */ |
| 1158 | acpigen_emit_byte(0x09); |
| 1159 | acpigen_emit_byte(0x00); |
| 1160 | /* bit1-7 are ignored */ |
| 1161 | acpigen_emit_byte(readwrite ? 0x01 : 0x00); |
Duncan Laurie | 9ccae75 | 2016-05-09 07:43:19 -0700 | [diff] [blame] | 1162 | acpigen_emit_dword(base); |
| 1163 | acpigen_emit_dword(size); |
Tobias Diedrich | 0fe6e9a | 2010-11-17 16:27:06 +0000 | [diff] [blame] | 1164 | } |
| 1165 | |
Matt Delco | c3f9d7a | 2018-07-27 14:01:05 -0700 | [diff] [blame] | 1166 | static void acpigen_write_register(const acpi_addr_t *addr) |
Sven Schnelle | 0b86c76 | 2011-10-21 21:46:47 +0200 | [diff] [blame] | 1167 | { |
Stefan Reinauer | 4cc8c70 | 2012-04-27 21:34:16 +0200 | [diff] [blame] | 1168 | acpigen_emit_byte(0x82); /* Register Descriptor */ |
| 1169 | acpigen_emit_byte(0x0c); /* Register Length 7:0 */ |
| 1170 | acpigen_emit_byte(0x00); /* Register Length 15:8 */ |
| 1171 | acpigen_emit_byte(addr->space_id); /* Address Space ID */ |
| 1172 | acpigen_emit_byte(addr->bit_width); /* Register Bit Width */ |
| 1173 | acpigen_emit_byte(addr->bit_offset); /* Register Bit Offset */ |
Elyes HAOUAS | 8ee161d | 2019-03-03 12:49:56 +0100 | [diff] [blame] | 1174 | acpigen_emit_byte(addr->access_size); /* Register Access Size */ |
Duncan Laurie | 9ccae75 | 2016-05-09 07:43:19 -0700 | [diff] [blame] | 1175 | acpigen_emit_dword(addr->addrl); /* Register Address Low */ |
| 1176 | acpigen_emit_dword(addr->addrh); /* Register Address High */ |
Sven Schnelle | 0b86c76 | 2011-10-21 21:46:47 +0200 | [diff] [blame] | 1177 | } |
| 1178 | |
Matt Delco | c3f9d7a | 2018-07-27 14:01:05 -0700 | [diff] [blame] | 1179 | void acpigen_write_register_resource(const acpi_addr_t *addr) |
| 1180 | { |
| 1181 | acpigen_write_resourcetemplate_header(); |
| 1182 | acpigen_write_register(addr); |
| 1183 | acpigen_write_resourcetemplate_footer(); |
| 1184 | } |
| 1185 | |
Vladimir Serbinenko | 9bb5c5c | 2014-11-09 03:51:32 +0100 | [diff] [blame] | 1186 | void acpigen_write_irq(u16 mask) |
Vladimir Serbinenko | 20ea040 | 2014-02-15 18:57:17 +0100 | [diff] [blame] | 1187 | { |
| 1188 | /* |
Elyes HAOUAS | 2078e75 | 2016-08-21 10:41:44 +0200 | [diff] [blame] | 1189 | * ACPI 3.0b section 6.4.2.1: IRQ Descriptor |
Vladimir Serbinenko | 20ea040 | 2014-02-15 18:57:17 +0100 | [diff] [blame] | 1190 | * Byte 0: |
| 1191 | * Bit7 : 0 => small item |
| 1192 | * Bit6-3: 0100 (0x4) => IRQ port descriptor |
| 1193 | * Bit2-0: 010 (0x2) => 2 Bytes long |
| 1194 | */ |
| 1195 | acpigen_emit_byte(0x22); |
| 1196 | acpigen_emit_byte(mask & 0xff); |
| 1197 | acpigen_emit_byte((mask >> 8) & 0xff); |
Vladimir Serbinenko | 20ea040 | 2014-02-15 18:57:17 +0100 | [diff] [blame] | 1198 | } |
| 1199 | |
Vladimir Serbinenko | 9bb5c5c | 2014-11-09 03:51:32 +0100 | [diff] [blame] | 1200 | void acpigen_write_io16(u16 min, u16 max, u8 align, u8 len, u8 decode16) |
Tobias Diedrich | 0fe6e9a | 2010-11-17 16:27:06 +0000 | [diff] [blame] | 1201 | { |
| 1202 | /* |
Elyes HAOUAS | 2078e75 | 2016-08-21 10:41:44 +0200 | [diff] [blame] | 1203 | * ACPI 4.0 section 6.4.2.6: I/O Port Descriptor |
Tobias Diedrich | 0fe6e9a | 2010-11-17 16:27:06 +0000 | [diff] [blame] | 1204 | * Byte 0: |
| 1205 | * Bit7 : 0 => small item |
| 1206 | * Bit6-3: 1000 (0x8) => I/O port descriptor |
| 1207 | * Bit2-0: 111 (0x7) => 7 Bytes long |
| 1208 | */ |
| 1209 | acpigen_emit_byte(0x47); |
Paul Menzel | 0f4c0e2 | 2013-02-22 12:33:08 +0100 | [diff] [blame] | 1210 | /* Does the device decode all 16 or just 10 bits? */ |
Tobias Diedrich | 0fe6e9a | 2010-11-17 16:27:06 +0000 | [diff] [blame] | 1211 | /* bit1-7 are ignored */ |
| 1212 | acpigen_emit_byte(decode16 ? 0x01 : 0x00); |
| 1213 | /* minimum base address the device may be configured for */ |
| 1214 | acpigen_emit_byte(min & 0xff); |
| 1215 | acpigen_emit_byte((min >> 8) & 0xff); |
| 1216 | /* maximum base address the device may be configured for */ |
| 1217 | acpigen_emit_byte(max & 0xff); |
| 1218 | acpigen_emit_byte((max >> 8) & 0xff); |
| 1219 | /* alignment for min base */ |
| 1220 | acpigen_emit_byte(align & 0xff); |
| 1221 | acpigen_emit_byte(len & 0xff); |
Tobias Diedrich | 0fe6e9a | 2010-11-17 16:27:06 +0000 | [diff] [blame] | 1222 | } |
| 1223 | |
Vladimir Serbinenko | 9bb5c5c | 2014-11-09 03:51:32 +0100 | [diff] [blame] | 1224 | void acpigen_write_resourcetemplate_header(void) |
Tobias Diedrich | 0fe6e9a | 2010-11-17 16:27:06 +0000 | [diff] [blame] | 1225 | { |
Tobias Diedrich | 0fe6e9a | 2010-11-17 16:27:06 +0000 | [diff] [blame] | 1226 | /* |
| 1227 | * A ResourceTemplate() is a Buffer() with a |
| 1228 | * (Byte|Word|DWord) containing the length, followed by one or more |
Paul Menzel | 0f4c0e2 | 2013-02-22 12:33:08 +0100 | [diff] [blame] | 1229 | * resource items, terminated by the end tag. |
Tobias Diedrich | 0fe6e9a | 2010-11-17 16:27:06 +0000 | [diff] [blame] | 1230 | * (small item 0xf, len 1) |
| 1231 | */ |
Furquan Shaikh | e4e9b94 | 2016-10-20 23:09:05 -0700 | [diff] [blame] | 1232 | acpigen_emit_byte(BUFFER_OP); |
Vladimir Serbinenko | 9bb5c5c | 2014-11-09 03:51:32 +0100 | [diff] [blame] | 1233 | acpigen_write_len_f(); |
Furquan Shaikh | e4e9b94 | 2016-10-20 23:09:05 -0700 | [diff] [blame] | 1234 | acpigen_emit_byte(WORD_PREFIX); |
Tobias Diedrich | 0fe6e9a | 2010-11-17 16:27:06 +0000 | [diff] [blame] | 1235 | len_stack[ltop++] = acpigen_get_current(); |
Nico Huber | 7d89ce3 | 2017-04-14 00:08:18 +0200 | [diff] [blame] | 1236 | /* Add 2 dummy bytes for the ACPI word (keep aligned with |
Elyes HAOUAS | 6716bab | 2020-01-09 21:29:25 +0100 | [diff] [blame] | 1237 | the calculation in acpigen_write_resourcetemplate() below). */ |
Vladimir Serbinenko | 9bb5c5c | 2014-11-09 03:51:32 +0100 | [diff] [blame] | 1238 | acpigen_emit_byte(0x00); |
| 1239 | acpigen_emit_byte(0x00); |
Tobias Diedrich | 0fe6e9a | 2010-11-17 16:27:06 +0000 | [diff] [blame] | 1240 | } |
| 1241 | |
Vladimir Serbinenko | 9bb5c5c | 2014-11-09 03:51:32 +0100 | [diff] [blame] | 1242 | void acpigen_write_resourcetemplate_footer(void) |
Tobias Diedrich | 0fe6e9a | 2010-11-17 16:27:06 +0000 | [diff] [blame] | 1243 | { |
| 1244 | char *p = len_stack[--ltop]; |
Vladimir Serbinenko | 9acc1e8 | 2014-11-09 03:37:28 +0100 | [diff] [blame] | 1245 | int len; |
Tobias Diedrich | 0fe6e9a | 2010-11-17 16:27:06 +0000 | [diff] [blame] | 1246 | /* |
| 1247 | * end tag (acpi 4.0 Section 6.4.2.8) |
| 1248 | * 0x79 <checksum> |
| 1249 | * 0x00 is treated as a good checksum according to the spec |
| 1250 | * and is what iasl generates. |
| 1251 | */ |
Vladimir Serbinenko | 9acc1e8 | 2014-11-09 03:37:28 +0100 | [diff] [blame] | 1252 | acpigen_emit_byte(0x79); |
| 1253 | acpigen_emit_byte(0x00); |
| 1254 | |
Nico Huber | 7d89ce3 | 2017-04-14 00:08:18 +0200 | [diff] [blame] | 1255 | /* Start counting past the 2-bytes length added in |
| 1256 | acpigen_write_resourcetemplate() above. */ |
| 1257 | len = acpigen_get_current() - (p + 2); |
Vladimir Serbinenko | 9acc1e8 | 2014-11-09 03:37:28 +0100 | [diff] [blame] | 1258 | |
Tobias Diedrich | 0fe6e9a | 2010-11-17 16:27:06 +0000 | [diff] [blame] | 1259 | /* patch len word */ |
Vladimir Serbinenko | 9acc1e8 | 2014-11-09 03:37:28 +0100 | [diff] [blame] | 1260 | p[0] = len & 0xff; |
| 1261 | p[1] = (len >> 8) & 0xff; |
Tobias Diedrich | 0fe6e9a | 2010-11-17 16:27:06 +0000 | [diff] [blame] | 1262 | /* patch len field */ |
Vladimir Serbinenko | a09f4db | 2014-11-09 03:32:58 +0100 | [diff] [blame] | 1263 | acpigen_pop_len(); |
Tobias Diedrich | 0fe6e9a | 2010-11-17 16:27:06 +0000 | [diff] [blame] | 1264 | } |
| 1265 | |
Elyes Haouas | 1f5e1b4 | 2022-02-17 17:44:07 +0100 | [diff] [blame] | 1266 | static void acpigen_add_mainboard_rsvd_mem32(void *gp, struct device *dev, struct resource *res) |
Tobias Diedrich | 0fe6e9a | 2010-11-17 16:27:06 +0000 | [diff] [blame] | 1267 | { |
| 1268 | acpigen_write_mem32fixed(0, res->base, res->size); |
| 1269 | } |
| 1270 | |
Elyes Haouas | 1f5e1b4 | 2022-02-17 17:44:07 +0100 | [diff] [blame] | 1271 | static void acpigen_add_mainboard_rsvd_io(void *gp, struct device *dev, struct resource *res) |
Tobias Diedrich | 0fe6e9a | 2010-11-17 16:27:06 +0000 | [diff] [blame] | 1272 | { |
| 1273 | resource_t base = res->base; |
| 1274 | resource_t size = res->size; |
| 1275 | while (size > 0) { |
| 1276 | resource_t sz = size > 255 ? 255 : size; |
| 1277 | acpigen_write_io16(base, base, 0, sz, 1); |
| 1278 | size -= sz; |
| 1279 | base += sz; |
| 1280 | } |
| 1281 | } |
| 1282 | |
Vladimir Serbinenko | 9bb5c5c | 2014-11-09 03:51:32 +0100 | [diff] [blame] | 1283 | void acpigen_write_mainboard_resource_template(void) |
Tobias Diedrich | 0fe6e9a | 2010-11-17 16:27:06 +0000 | [diff] [blame] | 1284 | { |
Vladimir Serbinenko | 9bb5c5c | 2014-11-09 03:51:32 +0100 | [diff] [blame] | 1285 | acpigen_write_resourcetemplate_header(); |
Tobias Diedrich | 0fe6e9a | 2010-11-17 16:27:06 +0000 | [diff] [blame] | 1286 | |
Paul Menzel | 0f4c0e2 | 2013-02-22 12:33:08 +0100 | [diff] [blame] | 1287 | /* Add reserved memory ranges. */ |
Tobias Diedrich | 0fe6e9a | 2010-11-17 16:27:06 +0000 | [diff] [blame] | 1288 | search_global_resources( |
| 1289 | IORESOURCE_MEM | IORESOURCE_RESERVE, |
Elyes Haouas | 1f5e1b4 | 2022-02-17 17:44:07 +0100 | [diff] [blame] | 1290 | IORESOURCE_MEM | IORESOURCE_RESERVE, |
Tobias Diedrich | 0fe6e9a | 2010-11-17 16:27:06 +0000 | [diff] [blame] | 1291 | acpigen_add_mainboard_rsvd_mem32, 0); |
| 1292 | |
Paul Menzel | 0f4c0e2 | 2013-02-22 12:33:08 +0100 | [diff] [blame] | 1293 | /* Add reserved io ranges. */ |
Tobias Diedrich | 0fe6e9a | 2010-11-17 16:27:06 +0000 | [diff] [blame] | 1294 | search_global_resources( |
| 1295 | IORESOURCE_IO | IORESOURCE_RESERVE, |
Elyes Haouas | 1f5e1b4 | 2022-02-17 17:44:07 +0100 | [diff] [blame] | 1296 | IORESOURCE_IO | IORESOURCE_RESERVE, |
Tobias Diedrich | 0fe6e9a | 2010-11-17 16:27:06 +0000 | [diff] [blame] | 1297 | acpigen_add_mainboard_rsvd_io, 0); |
| 1298 | |
Vladimir Serbinenko | 9bb5c5c | 2014-11-09 03:51:32 +0100 | [diff] [blame] | 1299 | acpigen_write_resourcetemplate_footer(); |
Tobias Diedrich | 0fe6e9a | 2010-11-17 16:27:06 +0000 | [diff] [blame] | 1300 | } |
| 1301 | |
Vladimir Serbinenko | 9bb5c5c | 2014-11-09 03:51:32 +0100 | [diff] [blame] | 1302 | void acpigen_write_mainboard_resources(const char *scope, const char *name) |
Tobias Diedrich | 0fe6e9a | 2010-11-17 16:27:06 +0000 | [diff] [blame] | 1303 | { |
Vladimir Serbinenko | 9bb5c5c | 2014-11-09 03:51:32 +0100 | [diff] [blame] | 1304 | acpigen_write_scope(scope); |
| 1305 | acpigen_write_name(name); |
| 1306 | acpigen_write_mainboard_resource_template(); |
Vladimir Serbinenko | a09f4db | 2014-11-09 03:32:58 +0100 | [diff] [blame] | 1307 | acpigen_pop_len(); |
Tobias Diedrich | 0fe6e9a | 2010-11-17 16:27:06 +0000 | [diff] [blame] | 1308 | } |
Vladimir Serbinenko | 8ecdc9e | 2014-02-15 18:59:40 +0100 | [diff] [blame] | 1309 | |
| 1310 | static int hex2bin(const char c) |
| 1311 | { |
| 1312 | if (c >= 'A' && c <= 'F') |
| 1313 | return c - 'A' + 10; |
| 1314 | if (c >= 'a' && c <= 'f') |
| 1315 | return c - 'a' + 10; |
| 1316 | return c - '0'; |
| 1317 | } |
| 1318 | |
Vladimir Serbinenko | 9bb5c5c | 2014-11-09 03:51:32 +0100 | [diff] [blame] | 1319 | void acpigen_emit_eisaid(const char *eisaid) |
Vladimir Serbinenko | 8ecdc9e | 2014-02-15 18:59:40 +0100 | [diff] [blame] | 1320 | { |
| 1321 | u32 compact = 0; |
| 1322 | |
| 1323 | /* Clamping individual values would be better but |
| 1324 | there is a disagreement over what is a valid |
| 1325 | EISA id, so accept anything and don't clamp, |
| 1326 | parent code should create a valid EISAid. |
| 1327 | */ |
| 1328 | compact |= (eisaid[0] - 'A' + 1) << 26; |
| 1329 | compact |= (eisaid[1] - 'A' + 1) << 21; |
| 1330 | compact |= (eisaid[2] - 'A' + 1) << 16; |
| 1331 | compact |= hex2bin(eisaid[3]) << 12; |
| 1332 | compact |= hex2bin(eisaid[4]) << 8; |
| 1333 | compact |= hex2bin(eisaid[5]) << 4; |
| 1334 | compact |= hex2bin(eisaid[6]); |
| 1335 | |
| 1336 | acpigen_emit_byte(0xc); |
| 1337 | acpigen_emit_byte((compact >> 24) & 0xff); |
| 1338 | acpigen_emit_byte((compact >> 16) & 0xff); |
| 1339 | acpigen_emit_byte((compact >> 8) & 0xff); |
| 1340 | acpigen_emit_byte(compact & 0xff); |
Vladimir Serbinenko | 8ecdc9e | 2014-02-15 18:59:40 +0100 | [diff] [blame] | 1341 | } |
Duncan Laurie | 3829f23 | 2016-05-09 11:08:46 -0700 | [diff] [blame] | 1342 | |
| 1343 | /* |
| 1344 | * ToUUID(uuid) |
| 1345 | * |
| 1346 | * ACPI 6.1 Section 19.6.136 table 19-385 defines a special output |
| 1347 | * order for the bytes that make up a UUID Buffer object. |
| 1348 | * UUID byte order for input: |
| 1349 | * aabbccdd-eeff-gghh-iijj-kkllmmnnoopp |
| 1350 | * UUID byte order for output: |
| 1351 | * ddccbbaa-ffee-hhgg-iijj-kkllmmnnoopp |
| 1352 | */ |
| 1353 | #define UUID_LEN 16 |
| 1354 | void acpigen_write_uuid(const char *uuid) |
| 1355 | { |
| 1356 | uint8_t buf[UUID_LEN]; |
| 1357 | size_t i, order[UUID_LEN] = { 3, 2, 1, 0, 5, 4, 7, 6, |
| 1358 | 8, 9, 10, 11, 12, 13, 14, 15 }; |
| 1359 | |
| 1360 | /* Parse UUID string into bytes */ |
| 1361 | if (hexstrtobin(uuid, buf, UUID_LEN) < UUID_LEN) |
| 1362 | return; |
| 1363 | |
| 1364 | /* BufferOp */ |
Furquan Shaikh | e4e9b94 | 2016-10-20 23:09:05 -0700 | [diff] [blame] | 1365 | acpigen_emit_byte(BUFFER_OP); |
Duncan Laurie | 3829f23 | 2016-05-09 11:08:46 -0700 | [diff] [blame] | 1366 | acpigen_write_len_f(); |
| 1367 | |
| 1368 | /* Buffer length in bytes */ |
| 1369 | acpigen_write_word(UUID_LEN); |
| 1370 | |
| 1371 | /* Output UUID in expected order */ |
| 1372 | for (i = 0; i < UUID_LEN; i++) |
| 1373 | acpigen_emit_byte(buf[order[i]]); |
| 1374 | |
| 1375 | acpigen_pop_len(); |
| 1376 | } |
Furquan Shaikh | fcc7a04 | 2016-10-20 23:12:38 -0700 | [diff] [blame] | 1377 | |
| 1378 | /* |
| 1379 | * Name (_PRx, Package(One) { name }) |
| 1380 | * ... |
| 1381 | * PowerResource (name, level, order) |
| 1382 | */ |
| 1383 | void acpigen_write_power_res(const char *name, uint8_t level, uint16_t order, |
Furquan Shaikh | dc78275 | 2020-04-30 22:49:39 -0700 | [diff] [blame] | 1384 | const char * const dev_states[], size_t dev_states_count) |
Furquan Shaikh | fcc7a04 | 2016-10-20 23:12:38 -0700 | [diff] [blame] | 1385 | { |
Elyes HAOUAS | cca6e00 | 2019-06-26 12:12:00 +0200 | [diff] [blame] | 1386 | size_t i; |
Furquan Shaikh | fcc7a04 | 2016-10-20 23:12:38 -0700 | [diff] [blame] | 1387 | for (i = 0; i < dev_states_count; i++) { |
| 1388 | acpigen_write_name(dev_states[i]); |
| 1389 | acpigen_write_package(1); |
| 1390 | acpigen_emit_simple_namestring(name); |
| 1391 | acpigen_pop_len(); /* Package */ |
| 1392 | } |
| 1393 | |
| 1394 | acpigen_emit_ext_op(POWER_RES_OP); |
| 1395 | |
| 1396 | acpigen_write_len_f(); |
| 1397 | |
| 1398 | acpigen_emit_simple_namestring(name); |
| 1399 | acpigen_emit_byte(level); |
| 1400 | acpigen_emit_word(order); |
| 1401 | } |
| 1402 | |
| 1403 | /* Sleep (ms) */ |
| 1404 | void acpigen_write_sleep(uint64_t sleep_ms) |
| 1405 | { |
| 1406 | acpigen_emit_ext_op(SLEEP_OP); |
| 1407 | acpigen_write_integer(sleep_ms); |
| 1408 | } |
| 1409 | |
| 1410 | void acpigen_write_store(void) |
| 1411 | { |
| 1412 | acpigen_emit_byte(STORE_OP); |
| 1413 | } |
| 1414 | |
| 1415 | /* Store (src, dst) */ |
| 1416 | void acpigen_write_store_ops(uint8_t src, uint8_t dst) |
| 1417 | { |
| 1418 | acpigen_write_store(); |
| 1419 | acpigen_emit_byte(src); |
| 1420 | acpigen_emit_byte(dst); |
| 1421 | } |
| 1422 | |
Furquan Shaikh | 2c213d3 | 2020-04-27 23:06:30 -0700 | [diff] [blame] | 1423 | /* Store (src, "namestr") */ |
| 1424 | void acpigen_write_store_op_to_namestr(uint8_t src, const char *dst) |
| 1425 | { |
| 1426 | acpigen_write_store(); |
| 1427 | acpigen_emit_byte(src); |
| 1428 | acpigen_emit_namestring(dst); |
| 1429 | } |
| 1430 | |
Duncan Laurie | 8e391d3 | 2020-09-30 23:17:41 +0000 | [diff] [blame] | 1431 | /* Store (src, "namestr") */ |
| 1432 | void acpigen_write_store_int_to_namestr(uint64_t src, const char *dst) |
| 1433 | { |
| 1434 | acpigen_write_store(); |
| 1435 | acpigen_write_integer(src); |
| 1436 | acpigen_emit_namestring(dst); |
| 1437 | } |
| 1438 | |
Cliff Huang | 24f3dc8 | 2023-02-04 21:11:51 -0800 | [diff] [blame] | 1439 | /* Store ("namestr", dst) */ |
| 1440 | void acpigen_write_store_namestr_to_op(const char *src, uint8_t dst) |
| 1441 | { |
| 1442 | acpigen_write_store(); |
| 1443 | acpigen_emit_namestring(src); |
| 1444 | acpigen_emit_byte(dst); |
| 1445 | } |
| 1446 | |
Duncan Laurie | 8e391d3 | 2020-09-30 23:17:41 +0000 | [diff] [blame] | 1447 | /* Store (src, dst) */ |
| 1448 | void acpigen_write_store_int_to_op(uint64_t src, uint8_t dst) |
| 1449 | { |
| 1450 | acpigen_write_store(); |
| 1451 | acpigen_write_integer(src); |
| 1452 | acpigen_emit_byte(dst); |
| 1453 | } |
| 1454 | |
Felix Held | 178cf35 | 2023-02-09 16:29:46 +0100 | [diff] [blame] | 1455 | /* Store ("namestr", "namestr") */ |
| 1456 | void acpigen_write_store_namestr_to_namestr(const char *src, const char *dst) |
| 1457 | { |
| 1458 | acpigen_write_store(); |
| 1459 | acpigen_emit_namestring(src); |
| 1460 | acpigen_emit_namestring(dst); |
| 1461 | } |
| 1462 | |
Furquan Shaikh | fcc7a04 | 2016-10-20 23:12:38 -0700 | [diff] [blame] | 1463 | /* Or (arg1, arg2, res) */ |
| 1464 | void acpigen_write_or(uint8_t arg1, uint8_t arg2, uint8_t res) |
| 1465 | { |
| 1466 | acpigen_emit_byte(OR_OP); |
| 1467 | acpigen_emit_byte(arg1); |
| 1468 | acpigen_emit_byte(arg2); |
| 1469 | acpigen_emit_byte(res); |
| 1470 | } |
| 1471 | |
Rajat Jain | 310623b | 2020-02-26 11:02:53 -0800 | [diff] [blame] | 1472 | /* Xor (arg1, arg2, res) */ |
| 1473 | void acpigen_write_xor(uint8_t arg1, uint8_t arg2, uint8_t res) |
| 1474 | { |
| 1475 | acpigen_emit_byte(XOR_OP); |
| 1476 | acpigen_emit_byte(arg1); |
| 1477 | acpigen_emit_byte(arg2); |
| 1478 | acpigen_emit_byte(res); |
| 1479 | } |
| 1480 | |
Furquan Shaikh | fcc7a04 | 2016-10-20 23:12:38 -0700 | [diff] [blame] | 1481 | /* And (arg1, arg2, res) */ |
| 1482 | void acpigen_write_and(uint8_t arg1, uint8_t arg2, uint8_t res) |
| 1483 | { |
| 1484 | acpigen_emit_byte(AND_OP); |
| 1485 | acpigen_emit_byte(arg1); |
| 1486 | acpigen_emit_byte(arg2); |
| 1487 | acpigen_emit_byte(res); |
| 1488 | } |
| 1489 | |
| 1490 | /* Not (arg, res) */ |
| 1491 | void acpigen_write_not(uint8_t arg, uint8_t res) |
| 1492 | { |
| 1493 | acpigen_emit_byte(NOT_OP); |
| 1494 | acpigen_emit_byte(arg); |
| 1495 | acpigen_emit_byte(res); |
| 1496 | } |
| 1497 | |
Cliff Huang | e608308 | 2023-01-19 21:18:23 -0800 | [diff] [blame] | 1498 | /* Concatenate (str, src_res, dest_res) */ |
| 1499 | void acpigen_concatenate_string_op(const char *str, uint8_t src_res, uint8_t dest_res) |
| 1500 | { |
| 1501 | acpigen_emit_byte(CONCATENATE_OP); |
| 1502 | acpigen_write_string(str); |
| 1503 | acpigen_emit_byte(src_res); |
| 1504 | acpigen_emit_byte(dest_res); |
| 1505 | } |
| 1506 | |
Furquan Shaikh | fcc7a04 | 2016-10-20 23:12:38 -0700 | [diff] [blame] | 1507 | /* Store (str, DEBUG) */ |
| 1508 | void acpigen_write_debug_string(const char *str) |
| 1509 | { |
| 1510 | acpigen_write_store(); |
| 1511 | acpigen_write_string(str); |
| 1512 | acpigen_emit_ext_op(DEBUG_OP); |
| 1513 | } |
| 1514 | |
| 1515 | /* Store (val, DEBUG) */ |
| 1516 | void acpigen_write_debug_integer(uint64_t val) |
| 1517 | { |
| 1518 | acpigen_write_store(); |
| 1519 | acpigen_write_integer(val); |
| 1520 | acpigen_emit_ext_op(DEBUG_OP); |
| 1521 | } |
| 1522 | |
| 1523 | /* Store (op, DEBUG) */ |
| 1524 | void acpigen_write_debug_op(uint8_t op) |
| 1525 | { |
| 1526 | acpigen_write_store(); |
| 1527 | acpigen_emit_byte(op); |
| 1528 | acpigen_emit_ext_op(DEBUG_OP); |
| 1529 | } |
| 1530 | |
Duncan Laurie | ec2e3e4 | 2020-11-03 15:19:08 -0800 | [diff] [blame] | 1531 | /* Store (str, DEBUG) */ |
| 1532 | void acpigen_write_debug_namestr(const char *str) |
| 1533 | { |
| 1534 | acpigen_write_store(); |
| 1535 | acpigen_emit_namestring(str); |
| 1536 | acpigen_emit_ext_op(DEBUG_OP); |
| 1537 | } |
| 1538 | |
Cliff Huang | e608308 | 2023-01-19 21:18:23 -0800 | [diff] [blame] | 1539 | /* Concatenate (str1, res, tmp_res) |
| 1540 | Store(tmp_res, DEBUG) */ |
| 1541 | void acpigen_write_debug_concatenate_string_op(const char *str, uint8_t res, |
| 1542 | uint8_t tmp_res) |
| 1543 | { |
| 1544 | acpigen_concatenate_string_op(str, res, tmp_res); |
| 1545 | acpigen_write_debug_op(tmp_res); |
| 1546 | } |
| 1547 | |
Cliff Huang | 063a1b8 | 2023-02-18 00:17:26 -0800 | [diff] [blame] | 1548 | static void acpigen_tx_byte(unsigned char byte, void *data) |
| 1549 | { |
| 1550 | acpigen_emit_byte(byte); |
| 1551 | } |
| 1552 | |
| 1553 | /* Store("formatted string", DEBUG) */ |
| 1554 | void acpigen_write_debug_sprintf(const char *fmt, ...) |
| 1555 | { |
| 1556 | va_list args; |
| 1557 | |
| 1558 | acpigen_write_store(); |
| 1559 | |
| 1560 | acpigen_emit_byte(STRING_PREFIX); |
| 1561 | va_start(args, fmt); |
| 1562 | vtxprintf(acpigen_tx_byte, fmt, args, NULL); |
| 1563 | va_end(args); |
| 1564 | acpigen_emit_byte('\0'); |
| 1565 | |
| 1566 | acpigen_emit_ext_op(DEBUG_OP); |
| 1567 | } |
| 1568 | |
Furquan Shaikh | fcc7a04 | 2016-10-20 23:12:38 -0700 | [diff] [blame] | 1569 | void acpigen_write_if(void) |
| 1570 | { |
| 1571 | acpigen_emit_byte(IF_OP); |
| 1572 | acpigen_write_len_f(); |
| 1573 | } |
| 1574 | |
| 1575 | /* If (And (arg1, arg2)) */ |
| 1576 | void acpigen_write_if_and(uint8_t arg1, uint8_t arg2) |
| 1577 | { |
| 1578 | acpigen_write_if(); |
| 1579 | acpigen_emit_byte(AND_OP); |
| 1580 | acpigen_emit_byte(arg1); |
| 1581 | acpigen_emit_byte(arg2); |
| 1582 | } |
| 1583 | |
Furquan Shaikh | 1fc6bb9 | 2016-11-14 14:16:26 -0800 | [diff] [blame] | 1584 | /* |
Duncan Laurie | 2fe7471 | 2020-06-01 17:36:56 -0700 | [diff] [blame] | 1585 | * Generates ACPI code for checking if operand1 and operand2 are equal. |
| 1586 | * Both operand1 and operand2 are ACPI ops. |
| 1587 | * |
| 1588 | * If (Lequal (op,1 op2)) |
| 1589 | */ |
| 1590 | void acpigen_write_if_lequal_op_op(uint8_t op1, uint8_t op2) |
| 1591 | { |
| 1592 | acpigen_write_if(); |
| 1593 | acpigen_emit_byte(LEQUAL_OP); |
| 1594 | acpigen_emit_byte(op1); |
| 1595 | acpigen_emit_byte(op2); |
| 1596 | } |
| 1597 | |
| 1598 | /* |
Cliff Huang | 24f3dc8 | 2023-02-04 21:11:51 -0800 | [diff] [blame] | 1599 | * Generates ACPI code for checking if operand1 is greater than operand2. |
| 1600 | * Both operand1 and operand2 are ACPI ops. |
| 1601 | * |
| 1602 | * If (Lgreater (op1 op2)) |
| 1603 | */ |
| 1604 | void acpigen_write_if_lgreater_op_op(uint8_t op1, uint8_t op2) |
| 1605 | { |
| 1606 | acpigen_write_if(); |
| 1607 | acpigen_emit_byte(LGREATER_OP); |
| 1608 | acpigen_emit_byte(op1); |
| 1609 | acpigen_emit_byte(op2); |
| 1610 | } |
| 1611 | |
| 1612 | /* |
Furquan Shaikh | 1fc6bb9 | 2016-11-14 14:16:26 -0800 | [diff] [blame] | 1613 | * Generates ACPI code for checking if operand1 and operand2 are equal, where, |
| 1614 | * operand1 is ACPI op and operand2 is an integer. |
| 1615 | * |
| 1616 | * If (Lequal (op, val)) |
| 1617 | */ |
| 1618 | void acpigen_write_if_lequal_op_int(uint8_t op, uint64_t val) |
Furquan Shaikh | 9b39adb | 2016-10-21 16:21:07 -0700 | [diff] [blame] | 1619 | { |
| 1620 | acpigen_write_if(); |
| 1621 | acpigen_emit_byte(LEQUAL_OP); |
Furquan Shaikh | 1fc6bb9 | 2016-11-14 14:16:26 -0800 | [diff] [blame] | 1622 | acpigen_emit_byte(op); |
| 1623 | acpigen_write_integer(val); |
Furquan Shaikh | 9b39adb | 2016-10-21 16:21:07 -0700 | [diff] [blame] | 1624 | } |
| 1625 | |
Furquan Shaikh | 2c213d3 | 2020-04-27 23:06:30 -0700 | [diff] [blame] | 1626 | /* |
Cliff Huang | 24f3dc8 | 2023-02-04 21:11:51 -0800 | [diff] [blame] | 1627 | * Generates ACPI code for checking if operand is greater than the value, where, |
| 1628 | * operand is ACPI op and val is an integer. |
| 1629 | * |
| 1630 | * If (Lgreater (op, val)) |
| 1631 | */ |
| 1632 | void acpigen_write_if_lgreater_op_int(uint8_t op, uint64_t val) |
| 1633 | { |
| 1634 | acpigen_write_if(); |
| 1635 | acpigen_emit_byte(LGREATER_OP); |
| 1636 | acpigen_emit_byte(op); |
| 1637 | acpigen_write_integer(val); |
| 1638 | } |
| 1639 | |
| 1640 | /* |
Furquan Shaikh | 2c213d3 | 2020-04-27 23:06:30 -0700 | [diff] [blame] | 1641 | * Generates ACPI code for checking if operand1 and operand2 are equal, where, |
| 1642 | * operand1 is namestring and operand2 is an integer. |
| 1643 | * |
| 1644 | * If (Lequal ("namestr", val)) |
| 1645 | */ |
| 1646 | void acpigen_write_if_lequal_namestr_int(const char *namestr, uint64_t val) |
| 1647 | { |
| 1648 | acpigen_write_if(); |
| 1649 | acpigen_emit_byte(LEQUAL_OP); |
| 1650 | acpigen_emit_namestring(namestr); |
| 1651 | acpigen_write_integer(val); |
| 1652 | } |
| 1653 | |
Tim Wawrzynczak | c4ca2f6 | 2021-07-01 11:18:50 -0600 | [diff] [blame] | 1654 | /* |
Cliff Huang | 24f3dc8 | 2023-02-04 21:11:51 -0800 | [diff] [blame] | 1655 | * Generates ACPI code for checking if operand1 and operand2 are equal, where, |
| 1656 | * operand1 is namestring and operand2 is an integer. |
| 1657 | * |
| 1658 | * If (Lgreater ("namestr", val)) |
| 1659 | */ |
| 1660 | void acpigen_write_if_lgreater_namestr_int(const char *namestr, uint64_t val) |
| 1661 | { |
| 1662 | acpigen_write_if(); |
| 1663 | acpigen_emit_byte(LGREATER_OP); |
| 1664 | acpigen_emit_namestring(namestr); |
| 1665 | acpigen_write_integer(val); |
| 1666 | } |
| 1667 | |
| 1668 | /* |
Tim Wawrzynczak | c4ca2f6 | 2021-07-01 11:18:50 -0600 | [diff] [blame] | 1669 | * Generates ACPI code to check at runtime if an object named `namestring` |
| 1670 | * exists, and leaves the If scope open to continue execute code when this |
| 1671 | * is true. NOTE: Requires matching acpigen_write_if_end(). |
| 1672 | * |
| 1673 | * If (CondRefOf (NAME)) |
| 1674 | */ |
| 1675 | void acpigen_write_if_cond_ref_of(const char *namestring) |
| 1676 | { |
| 1677 | acpigen_write_if(); |
| 1678 | acpigen_emit_ext_op(COND_REFOF_OP); |
| 1679 | acpigen_emit_namestring(namestring); |
| 1680 | acpigen_emit_byte(ZERO_OP); /* ignore COND_REFOF_OP destination */ |
| 1681 | } |
| 1682 | |
Jakub Czapiga | 61fcb7e | 2021-02-19 11:44:22 +0100 | [diff] [blame] | 1683 | /* Closes previously opened if statement and generates ACPI code for else statement. */ |
Furquan Shaikh | fcc7a04 | 2016-10-20 23:12:38 -0700 | [diff] [blame] | 1684 | void acpigen_write_else(void) |
| 1685 | { |
Jakub Czapiga | 61fcb7e | 2021-02-19 11:44:22 +0100 | [diff] [blame] | 1686 | acpigen_pop_len(); |
Furquan Shaikh | fcc7a04 | 2016-10-20 23:12:38 -0700 | [diff] [blame] | 1687 | acpigen_emit_byte(ELSE_OP); |
| 1688 | acpigen_write_len_f(); |
| 1689 | } |
Furquan Shaikh | 0a48aee | 2016-10-20 07:12:49 -0700 | [diff] [blame] | 1690 | |
Duncan Laurie | 3685820 | 2020-10-06 21:01:51 +0000 | [diff] [blame] | 1691 | void acpigen_write_shiftleft_op_int(uint8_t src_result, uint64_t count) |
| 1692 | { |
| 1693 | acpigen_emit_byte(SHIFT_LEFT_OP); |
| 1694 | acpigen_emit_byte(src_result); |
| 1695 | acpigen_write_integer(count); |
| 1696 | acpigen_emit_byte(ZERO_OP); |
| 1697 | } |
| 1698 | |
Furquan Shaikh | 9b39adb | 2016-10-21 16:21:07 -0700 | [diff] [blame] | 1699 | void acpigen_write_to_buffer(uint8_t src, uint8_t dst) |
| 1700 | { |
| 1701 | acpigen_emit_byte(TO_BUFFER_OP); |
| 1702 | acpigen_emit_byte(src); |
| 1703 | acpigen_emit_byte(dst); |
| 1704 | } |
| 1705 | |
| 1706 | void acpigen_write_to_integer(uint8_t src, uint8_t dst) |
| 1707 | { |
| 1708 | acpigen_emit_byte(TO_INTEGER_OP); |
| 1709 | acpigen_emit_byte(src); |
| 1710 | acpigen_emit_byte(dst); |
| 1711 | } |
| 1712 | |
Aaron Durbin | 80bc091 | 2020-08-19 23:17:42 -0600 | [diff] [blame] | 1713 | void acpigen_write_to_integer_from_namestring(const char *source, uint8_t dst_op) |
| 1714 | { |
| 1715 | acpigen_emit_byte(TO_INTEGER_OP); |
| 1716 | acpigen_emit_namestring(source); |
| 1717 | acpigen_emit_byte(dst_op); |
| 1718 | } |
| 1719 | |
Rizwan Qureshi | aca4c94 | 2017-03-06 21:50:26 +0530 | [diff] [blame] | 1720 | void acpigen_write_byte_buffer(uint8_t *arr, size_t size) |
Furquan Shaikh | 9b39adb | 2016-10-21 16:21:07 -0700 | [diff] [blame] | 1721 | { |
Rizwan Qureshi | aca4c94 | 2017-03-06 21:50:26 +0530 | [diff] [blame] | 1722 | size_t i; |
Furquan Shaikh | 9b39adb | 2016-10-21 16:21:07 -0700 | [diff] [blame] | 1723 | |
| 1724 | acpigen_emit_byte(BUFFER_OP); |
| 1725 | acpigen_write_len_f(); |
Rizwan Qureshi | aca4c94 | 2017-03-06 21:50:26 +0530 | [diff] [blame] | 1726 | acpigen_write_integer(size); |
Furquan Shaikh | 9b39adb | 2016-10-21 16:21:07 -0700 | [diff] [blame] | 1727 | |
| 1728 | for (i = 0; i < size; i++) |
| 1729 | acpigen_emit_byte(arr[i]); |
| 1730 | |
| 1731 | acpigen_pop_len(); |
| 1732 | } |
| 1733 | |
Rizwan Qureshi | aca4c94 | 2017-03-06 21:50:26 +0530 | [diff] [blame] | 1734 | void acpigen_write_return_byte_buffer(uint8_t *arr, size_t size) |
Furquan Shaikh | 9b39adb | 2016-10-21 16:21:07 -0700 | [diff] [blame] | 1735 | { |
| 1736 | acpigen_emit_byte(RETURN_OP); |
| 1737 | acpigen_write_byte_buffer(arr, size); |
| 1738 | } |
| 1739 | |
| 1740 | void acpigen_write_return_singleton_buffer(uint8_t arg) |
| 1741 | { |
| 1742 | acpigen_write_return_byte_buffer(&arg, 1); |
| 1743 | } |
| 1744 | |
Duncan Laurie | 2fe7471 | 2020-06-01 17:36:56 -0700 | [diff] [blame] | 1745 | void acpigen_write_return_op(uint8_t arg) |
| 1746 | { |
| 1747 | acpigen_emit_byte(RETURN_OP); |
| 1748 | acpigen_emit_byte(arg); |
| 1749 | } |
| 1750 | |
Furquan Shaikh | 9b39adb | 2016-10-21 16:21:07 -0700 | [diff] [blame] | 1751 | void acpigen_write_return_byte(uint8_t arg) |
| 1752 | { |
| 1753 | acpigen_emit_byte(RETURN_OP); |
| 1754 | acpigen_write_byte(arg); |
| 1755 | } |
| 1756 | |
Naresh G Solanki | 05abe43 | 2016-11-17 00:16:29 +0530 | [diff] [blame] | 1757 | void acpigen_write_return_integer(uint64_t arg) |
| 1758 | { |
| 1759 | acpigen_emit_byte(RETURN_OP); |
| 1760 | acpigen_write_integer(arg); |
| 1761 | } |
| 1762 | |
Duncan Laurie | ec2e3e4 | 2020-11-03 15:19:08 -0800 | [diff] [blame] | 1763 | void acpigen_write_return_namestr(const char *arg) |
| 1764 | { |
| 1765 | acpigen_emit_byte(RETURN_OP); |
| 1766 | acpigen_emit_namestring(arg); |
| 1767 | } |
| 1768 | |
Naresh G Solanki | 05abe43 | 2016-11-17 00:16:29 +0530 | [diff] [blame] | 1769 | void acpigen_write_return_string(const char *arg) |
| 1770 | { |
| 1771 | acpigen_emit_byte(RETURN_OP); |
| 1772 | acpigen_write_string(arg); |
| 1773 | } |
| 1774 | |
Duncan Laurie | beb2af4 | 2018-05-07 14:26:59 -0700 | [diff] [blame] | 1775 | void acpigen_write_upc(enum acpi_upc_type type) |
| 1776 | { |
| 1777 | acpigen_write_name("_UPC"); |
| 1778 | acpigen_write_package(4); |
| 1779 | /* Connectable */ |
| 1780 | acpigen_write_byte(type == UPC_TYPE_UNUSED ? 0 : 0xff); |
| 1781 | /* Type */ |
| 1782 | acpigen_write_byte(type); |
| 1783 | /* Reserved0 */ |
| 1784 | acpigen_write_zero(); |
| 1785 | /* Reserved1 */ |
| 1786 | acpigen_write_zero(); |
| 1787 | acpigen_pop_len(); |
| 1788 | } |
| 1789 | |
Duncan Laurie | 3e7197a | 2018-05-07 14:18:13 -0700 | [diff] [blame] | 1790 | void acpigen_write_pld(const struct acpi_pld *pld) |
| 1791 | { |
| 1792 | uint8_t buf[20]; |
| 1793 | |
| 1794 | if (acpi_pld_to_buffer(pld, buf, ARRAY_SIZE(buf)) < 0) |
| 1795 | return; |
| 1796 | |
| 1797 | acpigen_write_name("_PLD"); |
Matt Delco | 4929f43 | 2019-01-30 11:40:44 -0800 | [diff] [blame] | 1798 | acpigen_write_package(1); |
Duncan Laurie | 3e7197a | 2018-05-07 14:18:13 -0700 | [diff] [blame] | 1799 | acpigen_write_byte_buffer(buf, ARRAY_SIZE(buf)); |
Matt Delco | 4929f43 | 2019-01-30 11:40:44 -0800 | [diff] [blame] | 1800 | acpigen_pop_len(); |
Duncan Laurie | 3e7197a | 2018-05-07 14:18:13 -0700 | [diff] [blame] | 1801 | } |
| 1802 | |
Elyes Haouas | 1f5e1b4 | 2022-02-17 17:44:07 +0100 | [diff] [blame] | 1803 | void acpigen_write_dsm(const char *uuid, void (**callbacks)(void *), size_t count, void *arg) |
Naresh G Solanki | ab77cd4 | 2016-11-15 10:13:15 +0530 | [diff] [blame] | 1804 | { |
| 1805 | struct dsm_uuid id = DSM_UUID(uuid, callbacks, count, arg); |
| 1806 | acpigen_write_dsm_uuid_arr(&id, 1); |
| 1807 | } |
| 1808 | |
Tim Wawrzynczak | d96f3a2 | 2021-07-14 15:56:57 -0600 | [diff] [blame] | 1809 | /* |
| 1810 | * Create a supported functions bitmask |
| 1811 | * bit 0: other functions than 0 are supported |
| 1812 | * bits 1-x: function x supported |
| 1813 | */ |
Arthur Heymans | 9c1f78d | 2023-06-22 21:04:06 +0200 | [diff] [blame] | 1814 | /* On GCC aarch64 the compiler is worried about alloca() having unbounded stack usage. */ |
| 1815 | #if defined(__GNUC__) && !defined(__clang__) |
| 1816 | #pragma GCC diagnostic ignored "-Wstack-usage=" |
| 1817 | #endif |
Tim Wawrzynczak | d96f3a2 | 2021-07-14 15:56:57 -0600 | [diff] [blame] | 1818 | static void acpigen_dsm_uuid_enum_functions(const struct dsm_uuid *id) |
| 1819 | { |
| 1820 | const size_t bytes = DIV_ROUND_UP(id->count, BITS_PER_BYTE); |
| 1821 | uint8_t *buffer = alloca(bytes); |
| 1822 | bool set = false; |
| 1823 | size_t cb_idx = 0; |
| 1824 | |
| 1825 | memset(buffer, 0, bytes); |
| 1826 | |
| 1827 | for (size_t i = 0; i < bytes; i++) { |
| 1828 | for (size_t j = 0; j < BITS_PER_BYTE; j++) { |
| 1829 | if (cb_idx >= id->count) |
| 1830 | break; |
| 1831 | |
| 1832 | if (id->callbacks[cb_idx++]) { |
| 1833 | set = true; |
| 1834 | buffer[i] |= BIT(j); |
| 1835 | } |
| 1836 | } |
| 1837 | } |
| 1838 | |
| 1839 | if (set) |
| 1840 | buffer[0] |= BIT(0); |
| 1841 | |
| 1842 | acpigen_write_return_byte_buffer(buffer, bytes); |
| 1843 | } |
| 1844 | |
Naresh G Solanki | ab77cd4 | 2016-11-15 10:13:15 +0530 | [diff] [blame] | 1845 | static void acpigen_write_dsm_uuid(struct dsm_uuid *id) |
| 1846 | { |
| 1847 | size_t i; |
| 1848 | |
| 1849 | /* If (LEqual (Local0, ToUUID(uuid))) */ |
| 1850 | acpigen_write_if(); |
| 1851 | acpigen_emit_byte(LEQUAL_OP); |
| 1852 | acpigen_emit_byte(LOCAL0_OP); |
| 1853 | acpigen_write_uuid(id->uuid); |
| 1854 | |
| 1855 | /* ToInteger (Arg2, Local1) */ |
| 1856 | acpigen_write_to_integer(ARG2_OP, LOCAL1_OP); |
| 1857 | |
Tim Wawrzynczak | d96f3a2 | 2021-07-14 15:56:57 -0600 | [diff] [blame] | 1858 | /* If (LEqual(Local1, 0)) */ |
| 1859 | { |
| 1860 | acpigen_write_if_lequal_op_int(LOCAL1_OP, 0); |
| 1861 | if (id->callbacks[0]) |
| 1862 | id->callbacks[0](id->arg); |
| 1863 | else if (id->count) |
| 1864 | acpigen_dsm_uuid_enum_functions(id); |
| 1865 | acpigen_write_if_end(); |
| 1866 | } |
| 1867 | |
| 1868 | for (i = 1; i < id->count; i++) { |
Naresh G Solanki | ab77cd4 | 2016-11-15 10:13:15 +0530 | [diff] [blame] | 1869 | /* If (LEqual (Local1, i)) */ |
| 1870 | acpigen_write_if_lequal_op_int(LOCAL1_OP, i); |
| 1871 | |
| 1872 | /* Callback to write if handler. */ |
| 1873 | if (id->callbacks[i]) |
| 1874 | id->callbacks[i](id->arg); |
| 1875 | |
Tim Wawrzynczak | d96f3a2 | 2021-07-14 15:56:57 -0600 | [diff] [blame] | 1876 | acpigen_write_if_end(); /* If */ |
Naresh G Solanki | ab77cd4 | 2016-11-15 10:13:15 +0530 | [diff] [blame] | 1877 | } |
| 1878 | |
| 1879 | /* Default case: Return (Buffer (One) { 0x0 }) */ |
| 1880 | acpigen_write_return_singleton_buffer(0x0); |
| 1881 | |
Tim Wawrzynczak | d96f3a2 | 2021-07-14 15:56:57 -0600 | [diff] [blame] | 1882 | acpigen_write_if_end(); /* If (LEqual (Local0, ToUUID(uuid))) */ |
Naresh G Solanki | ab77cd4 | 2016-11-15 10:13:15 +0530 | [diff] [blame] | 1883 | |
| 1884 | } |
| 1885 | |
Furquan Shaikh | c00bd18 | 2016-10-21 16:37:41 -0700 | [diff] [blame] | 1886 | /* |
| 1887 | * Generate ACPI AML code for _DSM method. |
Naresh G Solanki | ab77cd4 | 2016-11-15 10:13:15 +0530 | [diff] [blame] | 1888 | * This function takes as input array of uuid for the device, set of callbacks |
| 1889 | * and argument to pass into the callbacks. Callbacks should ensure that Local0 |
| 1890 | * and Local1 are left untouched. Use of Local2-Local7 is permitted in |
| 1891 | * callbacks. |
Furquan Shaikh | c00bd18 | 2016-10-21 16:37:41 -0700 | [diff] [blame] | 1892 | * |
| 1893 | * Arguments passed into _DSM method: |
| 1894 | * Arg0 = UUID |
| 1895 | * Arg1 = Revision |
| 1896 | * Arg2 = Function index |
| 1897 | * Arg3 = Function specific arguments |
| 1898 | * |
| 1899 | * AML code generated would look like: |
| 1900 | * Method (_DSM, 4, Serialized) { |
Naresh G Solanki | ab77cd4 | 2016-11-15 10:13:15 +0530 | [diff] [blame] | 1901 | * ToBuffer (Arg0, Local0) |
| 1902 | * If (LEqual (Local0, ToUUID(uuid))) { |
| 1903 | * ToInteger (Arg2, Local1) |
| 1904 | * If (LEqual (Local1, 0)) { |
| 1905 | * <acpigen by callback[0]> |
| 1906 | * } |
| 1907 | * ... |
| 1908 | * If (LEqual (Local1, n)) { |
| 1909 | * <acpigen by callback[n]> |
| 1910 | * } |
| 1911 | * Return (Buffer (One) { 0x0 }) |
| 1912 | * } |
| 1913 | * ... |
| 1914 | * If (LEqual (Local0, ToUUID(uuidn))) { |
| 1915 | * ... |
| 1916 | * } |
| 1917 | * Return (Buffer (One) { 0x0 }) |
Furquan Shaikh | c00bd18 | 2016-10-21 16:37:41 -0700 | [diff] [blame] | 1918 | * } |
| 1919 | */ |
Naresh G Solanki | ab77cd4 | 2016-11-15 10:13:15 +0530 | [diff] [blame] | 1920 | void acpigen_write_dsm_uuid_arr(struct dsm_uuid *ids, size_t count) |
Furquan Shaikh | c00bd18 | 2016-10-21 16:37:41 -0700 | [diff] [blame] | 1921 | { |
| 1922 | size_t i; |
| 1923 | |
| 1924 | /* Method (_DSM, 4, Serialized) */ |
| 1925 | acpigen_write_method_serialized("_DSM", 0x4); |
| 1926 | |
| 1927 | /* ToBuffer (Arg0, Local0) */ |
| 1928 | acpigen_write_to_buffer(ARG0_OP, LOCAL0_OP); |
| 1929 | |
Furquan Shaikh | c00bd18 | 2016-10-21 16:37:41 -0700 | [diff] [blame] | 1930 | for (i = 0; i < count; i++) |
Naresh G Solanki | ab77cd4 | 2016-11-15 10:13:15 +0530 | [diff] [blame] | 1931 | acpigen_write_dsm_uuid(&ids[i]); |
Furquan Shaikh | c00bd18 | 2016-10-21 16:37:41 -0700 | [diff] [blame] | 1932 | |
Naresh G Solanki | ab77cd4 | 2016-11-15 10:13:15 +0530 | [diff] [blame] | 1933 | /* Return (Buffer (One) { 0x0 }) */ |
Furquan Shaikh | c00bd18 | 2016-10-21 16:37:41 -0700 | [diff] [blame] | 1934 | acpigen_write_return_singleton_buffer(0x0); |
| 1935 | |
Furquan Shaikh | c00bd18 | 2016-10-21 16:37:41 -0700 | [diff] [blame] | 1936 | acpigen_pop_len(); /* Method _DSM */ |
| 1937 | } |
| 1938 | |
Matt Delco | b425bc8 | 2018-08-13 13:36:27 -0700 | [diff] [blame] | 1939 | void acpigen_write_CPPC_package(const struct cppc_config *config) |
| 1940 | { |
| 1941 | u32 i; |
| 1942 | u32 max; |
| 1943 | switch (config->version) { |
| 1944 | case 1: |
| 1945 | max = CPPC_MAX_FIELDS_VER_1; |
| 1946 | break; |
| 1947 | case 2: |
| 1948 | max = CPPC_MAX_FIELDS_VER_2; |
| 1949 | break; |
| 1950 | case 3: |
| 1951 | max = CPPC_MAX_FIELDS_VER_3; |
| 1952 | break; |
| 1953 | default: |
Elyes Haouas | 1f5e1b4 | 2022-02-17 17:44:07 +0100 | [diff] [blame] | 1954 | printk(BIOS_ERR, "CPPC version %u is not implemented\n", config->version); |
Matt Delco | b425bc8 | 2018-08-13 13:36:27 -0700 | [diff] [blame] | 1955 | return; |
| 1956 | } |
| 1957 | acpigen_write_name(CPPC_PACKAGE_NAME); |
| 1958 | |
| 1959 | /* Adding 2 to account for length and version fields */ |
| 1960 | acpigen_write_package(max + 2); |
| 1961 | acpigen_write_dword(max + 2); |
| 1962 | |
| 1963 | acpigen_write_byte(config->version); |
| 1964 | |
| 1965 | for (i = 0; i < max; ++i) { |
Michael Niewöhner | 38107fa | 2021-10-05 22:22:21 +0200 | [diff] [blame] | 1966 | const cppc_entry_t *entry = &config->entries[i]; |
| 1967 | if (entry->type == CPPC_TYPE_DWORD) |
| 1968 | acpigen_write_dword(entry->dword); |
| 1969 | else |
| 1970 | acpigen_write_register_resource(&entry->reg); |
Matt Delco | b425bc8 | 2018-08-13 13:36:27 -0700 | [diff] [blame] | 1971 | } |
| 1972 | acpigen_pop_len(); |
| 1973 | } |
| 1974 | |
| 1975 | void acpigen_write_CPPC_method(void) |
| 1976 | { |
Michael Niewöhner | b20aac0 | 2020-10-14 19:30:46 +0200 | [diff] [blame] | 1977 | char pscope[16]; |
Felix Held | f0a8b04 | 2023-05-12 15:55:06 +0200 | [diff] [blame] | 1978 | snprintf(pscope, sizeof(pscope), |
| 1979 | "\\_SB." CONFIG_ACPI_CPU_STRING "." CPPC_PACKAGE_NAME, 0); |
Michael Niewöhner | b20aac0 | 2020-10-14 19:30:46 +0200 | [diff] [blame] | 1980 | |
Matt Delco | b425bc8 | 2018-08-13 13:36:27 -0700 | [diff] [blame] | 1981 | acpigen_write_method("_CPC", 0); |
| 1982 | acpigen_emit_byte(RETURN_OP); |
Michael Niewöhner | b20aac0 | 2020-10-14 19:30:46 +0200 | [diff] [blame] | 1983 | acpigen_emit_namestring(pscope); |
Matt Delco | b425bc8 | 2018-08-13 13:36:27 -0700 | [diff] [blame] | 1984 | acpigen_pop_len(); |
| 1985 | } |
| 1986 | |
Patrick Rudolph | 6be6df0 | 2017-04-28 16:34:26 +0200 | [diff] [blame] | 1987 | /* |
| 1988 | * Generate ACPI AML code for _ROM method. |
| 1989 | * This function takes as input ROM data and ROM length. |
| 1990 | * |
Patrick Rudolph | 65cbbe7 | 2018-05-02 19:07:57 +0200 | [diff] [blame] | 1991 | * The ACPI spec isn't clear about what should happen at the end of the |
| 1992 | * ROM. Tests showed that it shouldn't truncate, but fill the remaining |
| 1993 | * bytes in the returned buffer with zeros. |
| 1994 | * |
Patrick Rudolph | 6be6df0 | 2017-04-28 16:34:26 +0200 | [diff] [blame] | 1995 | * Arguments passed into _DSM method: |
| 1996 | * Arg0 = Offset in Bytes |
| 1997 | * Arg1 = Bytes to return |
| 1998 | * |
| 1999 | * Example: |
| 2000 | * acpigen_write_rom(0xdeadbeef, 0x10000) |
| 2001 | * |
| 2002 | * AML code generated would look like: |
| 2003 | * Method (_ROM, 2, NotSerialized) { |
| 2004 | * |
| 2005 | * OperationRegion("ROMS", SYSTEMMEMORY, 0xdeadbeef, 0x10000) |
| 2006 | * Field (ROMS, AnyAcc, NoLock, Preserve) |
| 2007 | * { |
| 2008 | * Offset (0), |
| 2009 | * RBF0, 0x80000 |
| 2010 | * } |
| 2011 | * |
| 2012 | * Store (Arg0, Local0) |
| 2013 | * Store (Arg1, Local1) |
| 2014 | * |
| 2015 | * If (LGreater (Local1, 0x1000)) |
| 2016 | * { |
| 2017 | * Store (0x1000, Local1) |
| 2018 | * } |
| 2019 | * |
Patrick Rudolph | 65cbbe7 | 2018-05-02 19:07:57 +0200 | [diff] [blame] | 2020 | * Store (Local1, Local3) |
| 2021 | * |
Patrick Rudolph | 6be6df0 | 2017-04-28 16:34:26 +0200 | [diff] [blame] | 2022 | * If (LGreater (Local0, 0x10000)) |
| 2023 | * { |
| 2024 | * Return(Buffer(Local1){0}) |
| 2025 | * } |
| 2026 | * |
| 2027 | * If (LGreater (Local0, 0x0f000)) |
| 2028 | * { |
| 2029 | * Subtract (0x10000, Local0, Local2) |
| 2030 | * If (LGreater (Local1, Local2)) |
| 2031 | * { |
| 2032 | * Store (Local2, Local1) |
| 2033 | * } |
| 2034 | * } |
| 2035 | * |
Patrick Rudolph | 65cbbe7 | 2018-05-02 19:07:57 +0200 | [diff] [blame] | 2036 | * Name (ROM1, Buffer (Local3) {0}) |
Patrick Rudolph | 6be6df0 | 2017-04-28 16:34:26 +0200 | [diff] [blame] | 2037 | * |
| 2038 | * Multiply (Local0, 0x08, Local0) |
| 2039 | * Multiply (Local1, 0x08, Local1) |
| 2040 | * |
| 2041 | * CreateField (RBF0, Local0, Local1, TMPB) |
| 2042 | * Store (TMPB, ROM1) |
| 2043 | * Return (ROM1) |
| 2044 | * } |
| 2045 | */ |
| 2046 | |
| 2047 | void acpigen_write_rom(void *bios, const size_t length) |
| 2048 | { |
| 2049 | ASSERT(bios) |
| 2050 | ASSERT(length) |
| 2051 | |
Jonathan Neuschäfer | 6b0102d | 2018-09-12 12:26:45 +0200 | [diff] [blame] | 2052 | /* Method (_ROM, 2, Serialized) */ |
Marc Jones | 24462e6 | 2018-08-15 23:57:28 -0600 | [diff] [blame] | 2053 | acpigen_write_method_serialized("_ROM", 2); |
Patrick Rudolph | 6be6df0 | 2017-04-28 16:34:26 +0200 | [diff] [blame] | 2054 | |
| 2055 | /* OperationRegion("ROMS", SYSTEMMEMORY, current, length) */ |
Elyes Haouas | 1f5e1b4 | 2022-02-17 17:44:07 +0100 | [diff] [blame] | 2056 | struct opregion opreg = OPREGION("ROMS", SYSTEMMEMORY, (uintptr_t)bios, length); |
Patrick Rudolph | 6be6df0 | 2017-04-28 16:34:26 +0200 | [diff] [blame] | 2057 | acpigen_write_opregion(&opreg); |
| 2058 | |
| 2059 | struct fieldlist l[] = { |
| 2060 | FIELDLIST_OFFSET(0), |
| 2061 | FIELDLIST_NAMESTR("RBF0", 8 * length), |
| 2062 | }; |
| 2063 | |
| 2064 | /* Field (ROMS, AnyAcc, NoLock, Preserve) |
| 2065 | * { |
| 2066 | * Offset (0), |
| 2067 | * RBF0, 0x80000 |
| 2068 | * } */ |
Elyes Haouas | 1f5e1b4 | 2022-02-17 17:44:07 +0100 | [diff] [blame] | 2069 | acpigen_write_field(opreg.name, l, 2, FIELD_ANYACC | FIELD_NOLOCK | FIELD_PRESERVE); |
Patrick Rudolph | 6be6df0 | 2017-04-28 16:34:26 +0200 | [diff] [blame] | 2070 | |
| 2071 | /* Store (Arg0, Local0) */ |
Felix Held | f28f27b | 2023-02-09 16:26:26 +0100 | [diff] [blame] | 2072 | acpigen_write_store_ops(ARG0_OP, LOCAL0_OP); |
Patrick Rudolph | 6be6df0 | 2017-04-28 16:34:26 +0200 | [diff] [blame] | 2073 | |
| 2074 | /* Store (Arg1, Local1) */ |
Felix Held | f28f27b | 2023-02-09 16:26:26 +0100 | [diff] [blame] | 2075 | acpigen_write_store_ops(ARG1_OP, LOCAL1_OP); |
Patrick Rudolph | 6be6df0 | 2017-04-28 16:34:26 +0200 | [diff] [blame] | 2076 | |
| 2077 | /* ACPI SPEC requires to return at maximum 4KiB */ |
| 2078 | /* If (LGreater (Local1, 0x1000)) */ |
Felix Held | 383a06e | 2023-02-09 16:25:38 +0100 | [diff] [blame] | 2079 | acpigen_write_if_lgreater_op_int(LOCAL1_OP, 0x1000); |
Patrick Rudolph | 6be6df0 | 2017-04-28 16:34:26 +0200 | [diff] [blame] | 2080 | |
| 2081 | /* Store (0x1000, Local1) */ |
Felix Held | f28f27b | 2023-02-09 16:26:26 +0100 | [diff] [blame] | 2082 | acpigen_write_store_int_to_op(0x1000, LOCAL1_OP); |
Patrick Rudolph | 6be6df0 | 2017-04-28 16:34:26 +0200 | [diff] [blame] | 2083 | |
| 2084 | /* Pop if */ |
| 2085 | acpigen_pop_len(); |
| 2086 | |
Patrick Rudolph | 65cbbe7 | 2018-05-02 19:07:57 +0200 | [diff] [blame] | 2087 | /* Store (Local1, Local3) */ |
Felix Held | f28f27b | 2023-02-09 16:26:26 +0100 | [diff] [blame] | 2088 | acpigen_write_store_ops(LOCAL1_OP, LOCAL3_OP); |
Patrick Rudolph | 65cbbe7 | 2018-05-02 19:07:57 +0200 | [diff] [blame] | 2089 | |
Patrick Rudolph | 6be6df0 | 2017-04-28 16:34:26 +0200 | [diff] [blame] | 2090 | /* If (LGreater (Local0, length)) */ |
Felix Held | 383a06e | 2023-02-09 16:25:38 +0100 | [diff] [blame] | 2091 | acpigen_write_if_lgreater_op_int(LOCAL0_OP, length); |
Patrick Rudolph | 6be6df0 | 2017-04-28 16:34:26 +0200 | [diff] [blame] | 2092 | |
| 2093 | /* Return(Buffer(Local1){0}) */ |
| 2094 | acpigen_emit_byte(RETURN_OP); |
| 2095 | acpigen_emit_byte(BUFFER_OP); |
| 2096 | acpigen_write_len_f(); |
| 2097 | acpigen_emit_byte(LOCAL1_OP); |
| 2098 | acpigen_emit_byte(0); |
| 2099 | acpigen_pop_len(); |
| 2100 | |
| 2101 | /* Pop if */ |
| 2102 | acpigen_pop_len(); |
| 2103 | |
| 2104 | /* If (LGreater (Local0, length - 4096)) */ |
Felix Held | 383a06e | 2023-02-09 16:25:38 +0100 | [diff] [blame] | 2105 | acpigen_write_if_lgreater_op_int(LOCAL0_OP, length - 4096); |
Patrick Rudolph | 6be6df0 | 2017-04-28 16:34:26 +0200 | [diff] [blame] | 2106 | |
| 2107 | /* Subtract (length, Local0, Local2) */ |
| 2108 | acpigen_emit_byte(SUBTRACT_OP); |
| 2109 | acpigen_write_integer(length); |
| 2110 | acpigen_emit_byte(LOCAL0_OP); |
| 2111 | acpigen_emit_byte(LOCAL2_OP); |
| 2112 | |
| 2113 | /* If (LGreater (Local1, Local2)) */ |
Felix Held | 383a06e | 2023-02-09 16:25:38 +0100 | [diff] [blame] | 2114 | acpigen_write_if_lgreater_op_op(LOCAL1_OP, LOCAL2_OP); |
Patrick Rudolph | 6be6df0 | 2017-04-28 16:34:26 +0200 | [diff] [blame] | 2115 | |
| 2116 | /* Store (Local2, Local1) */ |
Felix Held | f28f27b | 2023-02-09 16:26:26 +0100 | [diff] [blame] | 2117 | acpigen_write_store_ops(LOCAL2_OP, LOCAL1_OP); |
Patrick Rudolph | 6be6df0 | 2017-04-28 16:34:26 +0200 | [diff] [blame] | 2118 | |
| 2119 | /* Pop if */ |
| 2120 | acpigen_pop_len(); |
| 2121 | |
| 2122 | /* Pop if */ |
| 2123 | acpigen_pop_len(); |
| 2124 | |
Patrick Rudolph | 65cbbe7 | 2018-05-02 19:07:57 +0200 | [diff] [blame] | 2125 | /* Name (ROM1, Buffer (Local3) {0}) */ |
Patrick Rudolph | 6be6df0 | 2017-04-28 16:34:26 +0200 | [diff] [blame] | 2126 | acpigen_write_name("ROM1"); |
| 2127 | acpigen_emit_byte(BUFFER_OP); |
| 2128 | acpigen_write_len_f(); |
Patrick Rudolph | 65cbbe7 | 2018-05-02 19:07:57 +0200 | [diff] [blame] | 2129 | acpigen_emit_byte(LOCAL3_OP); |
Patrick Rudolph | 6be6df0 | 2017-04-28 16:34:26 +0200 | [diff] [blame] | 2130 | acpigen_emit_byte(0); |
| 2131 | acpigen_pop_len(); |
| 2132 | |
| 2133 | /* Multiply (Local1, 0x08, Local1) */ |
| 2134 | acpigen_emit_byte(MULTIPLY_OP); |
| 2135 | acpigen_emit_byte(LOCAL1_OP); |
| 2136 | acpigen_write_integer(0x08); |
| 2137 | acpigen_emit_byte(LOCAL1_OP); |
| 2138 | |
| 2139 | /* Multiply (Local0, 0x08, Local0) */ |
| 2140 | acpigen_emit_byte(MULTIPLY_OP); |
| 2141 | acpigen_emit_byte(LOCAL0_OP); |
| 2142 | acpigen_write_integer(0x08); |
| 2143 | acpigen_emit_byte(LOCAL0_OP); |
| 2144 | |
| 2145 | /* CreateField (RBF0, Local0, Local1, TMPB) */ |
| 2146 | acpigen_emit_ext_op(CREATEFIELD_OP); |
| 2147 | acpigen_emit_namestring("RBF0"); |
| 2148 | acpigen_emit_byte(LOCAL0_OP); |
| 2149 | acpigen_emit_byte(LOCAL1_OP); |
| 2150 | acpigen_emit_namestring("TMPB"); |
| 2151 | |
| 2152 | /* Store (TMPB, ROM1) */ |
Felix Held | f28f27b | 2023-02-09 16:26:26 +0100 | [diff] [blame] | 2153 | acpigen_write_store_namestr_to_namestr("TMPB", "ROM1"); |
Patrick Rudolph | 6be6df0 | 2017-04-28 16:34:26 +0200 | [diff] [blame] | 2154 | |
| 2155 | /* Return (ROM1) */ |
| 2156 | acpigen_emit_byte(RETURN_OP); |
| 2157 | acpigen_emit_namestring("ROM1"); |
| 2158 | |
| 2159 | /* Pop method */ |
| 2160 | acpigen_pop_len(); |
| 2161 | } |
| 2162 | |
Furquan Shaikh | bf4845d | 2017-02-20 22:56:25 -0800 | [diff] [blame] | 2163 | /* |
| 2164 | * Helper functions for enabling/disabling Tx GPIOs based on the GPIO |
| 2165 | * polarity. These functions end up calling acpigen_soc_{set,clear}_tx_gpio to |
| 2166 | * make callbacks into SoC acpigen code. |
| 2167 | * |
| 2168 | * Returns 0 on success and -1 on error. |
| 2169 | */ |
Duncan Laurie | 30c3f91 | 2020-10-09 04:48:53 +0000 | [diff] [blame] | 2170 | int acpigen_enable_tx_gpio(const struct acpi_gpio *gpio) |
Furquan Shaikh | bf4845d | 2017-02-20 22:56:25 -0800 | [diff] [blame] | 2171 | { |
Furquan Shaikh | d2d5e44 | 2020-07-01 01:37:13 -0700 | [diff] [blame] | 2172 | if (gpio->active_low) |
Furquan Shaikh | bf4845d | 2017-02-20 22:56:25 -0800 | [diff] [blame] | 2173 | return acpigen_soc_clear_tx_gpio(gpio->pins[0]); |
Furquan Shaikh | d2d5e44 | 2020-07-01 01:37:13 -0700 | [diff] [blame] | 2174 | else |
| 2175 | return acpigen_soc_set_tx_gpio(gpio->pins[0]); |
Furquan Shaikh | bf4845d | 2017-02-20 22:56:25 -0800 | [diff] [blame] | 2176 | } |
| 2177 | |
Duncan Laurie | 30c3f91 | 2020-10-09 04:48:53 +0000 | [diff] [blame] | 2178 | int acpigen_disable_tx_gpio(const struct acpi_gpio *gpio) |
Furquan Shaikh | bf4845d | 2017-02-20 22:56:25 -0800 | [diff] [blame] | 2179 | { |
Furquan Shaikh | d2d5e44 | 2020-07-01 01:37:13 -0700 | [diff] [blame] | 2180 | if (gpio->active_low) |
Furquan Shaikh | bf4845d | 2017-02-20 22:56:25 -0800 | [diff] [blame] | 2181 | return acpigen_soc_set_tx_gpio(gpio->pins[0]); |
| 2182 | else |
| 2183 | return acpigen_soc_clear_tx_gpio(gpio->pins[0]); |
| 2184 | } |
Jonathan Zhang | 71299c2 | 2020-01-27 11:38:57 -0800 | [diff] [blame] | 2185 | |
Duncan Laurie | 30c3f91 | 2020-10-09 04:48:53 +0000 | [diff] [blame] | 2186 | void acpigen_get_rx_gpio(const struct acpi_gpio *gpio) |
Rajat Jain | 310623b | 2020-02-26 11:02:53 -0800 | [diff] [blame] | 2187 | { |
| 2188 | acpigen_soc_read_rx_gpio(gpio->pins[0]); |
| 2189 | |
Furquan Shaikh | d2d5e44 | 2020-07-01 01:37:13 -0700 | [diff] [blame] | 2190 | if (gpio->active_low) |
Rajat Jain | 310623b | 2020-02-26 11:02:53 -0800 | [diff] [blame] | 2191 | acpigen_write_xor(LOCAL0_OP, 1, LOCAL0_OP); |
| 2192 | } |
| 2193 | |
Duncan Laurie | 30c3f91 | 2020-10-09 04:48:53 +0000 | [diff] [blame] | 2194 | void acpigen_get_tx_gpio(const struct acpi_gpio *gpio) |
Duncan Laurie | 2fe7471 | 2020-06-01 17:36:56 -0700 | [diff] [blame] | 2195 | { |
| 2196 | acpigen_soc_get_tx_gpio(gpio->pins[0]); |
| 2197 | |
Furquan Shaikh | d2d5e44 | 2020-07-01 01:37:13 -0700 | [diff] [blame] | 2198 | if (gpio->active_low) |
Duncan Laurie | 2fe7471 | 2020-06-01 17:36:56 -0700 | [diff] [blame] | 2199 | acpigen_write_xor(LOCAL0_OP, 1, LOCAL0_OP); |
| 2200 | } |
| 2201 | |
Jonathan Zhang | 71299c2 | 2020-01-27 11:38:57 -0800 | [diff] [blame] | 2202 | /* refer to ACPI 6.4.3.5.3 Word Address Space Descriptor section for details */ |
Elyes Haouas | 1f5e1b4 | 2022-02-17 17:44:07 +0100 | [diff] [blame] | 2203 | void acpigen_resource_word(u16 res_type, u16 gen_flags, u16 type_flags, u16 gran, u16 range_min, |
| 2204 | u16 range_max, u16 translation, u16 length) |
Jonathan Zhang | 71299c2 | 2020-01-27 11:38:57 -0800 | [diff] [blame] | 2205 | { |
Felix Held | b2f2b53 | 2023-05-11 22:22:06 +0200 | [diff] [blame] | 2206 | /* Byte 0: Type 1, Large Item Value 0x8: Word Address Space Descriptor */ |
Jonathan Zhang | 71299c2 | 2020-01-27 11:38:57 -0800 | [diff] [blame] | 2207 | acpigen_emit_byte(0x88); |
| 2208 | /* Byte 1+2: length (0x000d) */ |
| 2209 | acpigen_emit_byte(0x0d); |
| 2210 | acpigen_emit_byte(0x00); |
| 2211 | /* resource type */ |
| 2212 | acpigen_emit_byte(res_type); // 0 - mem, 1 - io, 2 - bus |
| 2213 | /* general flags */ |
| 2214 | acpigen_emit_byte(gen_flags); |
| 2215 | /* type flags */ |
| 2216 | // refer to ACPI Table 6-234 (Memory), 6-235 (IO), 6-236 (Bus) for details |
| 2217 | acpigen_emit_byte(type_flags); |
| 2218 | /* granularity, min, max, translation, length */ |
| 2219 | acpigen_emit_word(gran); |
| 2220 | acpigen_emit_word(range_min); |
| 2221 | acpigen_emit_word(range_max); |
| 2222 | acpigen_emit_word(translation); |
| 2223 | acpigen_emit_word(length); |
| 2224 | } |
| 2225 | |
| 2226 | /* refer to ACPI 6.4.3.5.2 DWord Address Space Descriptor section for details */ |
Elyes Haouas | 1f5e1b4 | 2022-02-17 17:44:07 +0100 | [diff] [blame] | 2227 | void acpigen_resource_dword(u16 res_type, u16 gen_flags, u16 type_flags, u32 gran, |
| 2228 | u32 range_min, u32 range_max, u32 translation, u32 length) |
Jonathan Zhang | 71299c2 | 2020-01-27 11:38:57 -0800 | [diff] [blame] | 2229 | { |
Felix Held | b2f2b53 | 2023-05-11 22:22:06 +0200 | [diff] [blame] | 2230 | /* Byte 0: Type 1, Large Item Value 0x7: DWord Address Space Descriptor */ |
Jonathan Zhang | 71299c2 | 2020-01-27 11:38:57 -0800 | [diff] [blame] | 2231 | acpigen_emit_byte(0x87); |
| 2232 | /* Byte 1+2: length (0023) */ |
| 2233 | acpigen_emit_byte(23); |
| 2234 | acpigen_emit_byte(0x00); |
| 2235 | /* resource type */ |
| 2236 | acpigen_emit_byte(res_type); // 0 - mem, 1 - io, 2 - bus |
| 2237 | /* general flags */ |
| 2238 | acpigen_emit_byte(gen_flags); |
| 2239 | /* type flags */ |
| 2240 | // refer to ACPI Table 6-234 (Memory), 6-235 (IO), 6-236 (Bus) for details |
| 2241 | acpigen_emit_byte(type_flags); |
| 2242 | /* granularity, min, max, translation, length */ |
| 2243 | acpigen_emit_dword(gran); |
| 2244 | acpigen_emit_dword(range_min); |
| 2245 | acpigen_emit_dword(range_max); |
| 2246 | acpigen_emit_dword(translation); |
| 2247 | acpigen_emit_dword(length); |
| 2248 | } |
| 2249 | |
| 2250 | static void acpigen_emit_qword(u64 data) |
| 2251 | { |
| 2252 | acpigen_emit_dword(data & 0xffffffff); |
| 2253 | acpigen_emit_dword((data >> 32) & 0xffffffff); |
| 2254 | } |
| 2255 | |
| 2256 | /* refer to ACPI 6.4.3.5.1 QWord Address Space Descriptor section for details */ |
Elyes Haouas | 1f5e1b4 | 2022-02-17 17:44:07 +0100 | [diff] [blame] | 2257 | void acpigen_resource_qword(u16 res_type, u16 gen_flags, u16 type_flags, u64 gran, |
| 2258 | u64 range_min, u64 range_max, u64 translation, u64 length) |
Jonathan Zhang | 71299c2 | 2020-01-27 11:38:57 -0800 | [diff] [blame] | 2259 | { |
Felix Held | b2f2b53 | 2023-05-11 22:22:06 +0200 | [diff] [blame] | 2260 | /* Byte 0: Type 1, Large Item Value 0xa: QWord Address Space Descriptor */ |
Jonathan Zhang | 71299c2 | 2020-01-27 11:38:57 -0800 | [diff] [blame] | 2261 | acpigen_emit_byte(0x8a); |
| 2262 | /* Byte 1+2: length (0x002b) */ |
| 2263 | acpigen_emit_byte(0x2b); |
| 2264 | acpigen_emit_byte(0x00); |
| 2265 | /* resource type */ |
| 2266 | acpigen_emit_byte(res_type); // 0 - mem, 1 - io, 2 - bus |
| 2267 | /* general flags */ |
| 2268 | acpigen_emit_byte(gen_flags); |
| 2269 | /* type flags */ |
| 2270 | // refer to ACPI Table 6-234 (Memory), 6-235 (IO), 6-236 (Bus) for details |
| 2271 | acpigen_emit_byte(type_flags); |
| 2272 | /* granularity, min, max, translation, length */ |
| 2273 | acpigen_emit_qword(gran); |
| 2274 | acpigen_emit_qword(range_min); |
| 2275 | acpigen_emit_qword(range_max); |
| 2276 | acpigen_emit_qword(translation); |
| 2277 | acpigen_emit_qword(length); |
| 2278 | } |
Furquan Shaikh | 1a82923 | 2020-04-23 11:11:05 -0700 | [diff] [blame] | 2279 | |
Felix Held | 0fdede0 | 2023-05-27 01:02:37 +0200 | [diff] [blame] | 2280 | void acpigen_resource_producer_bus_number(u16 bus_base, u16 bus_limit) |
Felix Held | 5a82f0d | 2023-05-09 16:10:35 +0200 | [diff] [blame] | 2281 | { |
| 2282 | acpigen_resource_word(RSRC_TYPE_BUS, /* res_type */ |
| 2283 | ADDR_SPACE_GENERAL_FLAG_MAX_FIXED |
| 2284 | | ADDR_SPACE_GENERAL_FLAG_MIN_FIXED |
Felix Held | 0fdede0 | 2023-05-27 01:02:37 +0200 | [diff] [blame] | 2285 | | ADDR_SPACE_GENERAL_FLAG_DEC_POS |
| 2286 | | ADDR_SPACE_GENERAL_FLAG_PRODUCER, /* gen_flags */ |
Felix Held | 5a82f0d | 2023-05-09 16:10:35 +0200 | [diff] [blame] | 2287 | BUS_NUM_RANGE_RESOURCE_FLAG, /* type_flags */ |
| 2288 | 0, /* gran */ |
| 2289 | bus_base, /* range_min */ |
| 2290 | bus_limit, /* range_max */ |
| 2291 | 0x0, /* translation */ |
| 2292 | bus_limit - bus_base + 1); /* length */ |
| 2293 | } |
| 2294 | |
Felix Held | 0fdede0 | 2023-05-27 01:02:37 +0200 | [diff] [blame] | 2295 | void acpigen_resource_producer_io(u16 io_base, u16 io_limit) |
Felix Held | 6695be6 | 2023-05-09 16:18:51 +0200 | [diff] [blame] | 2296 | { |
Felix Held | e3c9a04 | 2023-06-05 19:59:25 +0200 | [diff] [blame] | 2297 | acpigen_resource_dword(RSRC_TYPE_IO, /* res_type */ |
Felix Held | 6695be6 | 2023-05-09 16:18:51 +0200 | [diff] [blame] | 2298 | ADDR_SPACE_GENERAL_FLAG_MAX_FIXED |
| 2299 | | ADDR_SPACE_GENERAL_FLAG_MIN_FIXED |
Felix Held | 0fdede0 | 2023-05-27 01:02:37 +0200 | [diff] [blame] | 2300 | | ADDR_SPACE_GENERAL_FLAG_DEC_POS |
| 2301 | | ADDR_SPACE_GENERAL_FLAG_PRODUCER, /* gen_flags */ |
Felix Held | 6695be6 | 2023-05-09 16:18:51 +0200 | [diff] [blame] | 2302 | IO_RSRC_FLAG_ENTIRE_RANGE, /* type_flags */ |
| 2303 | 0, /* gran */ |
| 2304 | io_base, /* range_min */ |
| 2305 | io_limit, /* range_max */ |
| 2306 | 0x0, /* translation */ |
| 2307 | io_limit - io_base + 1); /* length */ |
| 2308 | } |
| 2309 | |
Arthur Heymans | 62f788e | 2023-11-21 18:49:33 +0100 | [diff] [blame] | 2310 | static void acpigen_resource_mmio32(u32 mmio_base, u32 mmio_limit, u16 gen_flags, |
| 2311 | u16 type_flags) |
Felix Held | 21594fd1 | 2023-05-09 16:39:57 +0200 | [diff] [blame] | 2312 | { |
| 2313 | acpigen_resource_dword(RSRC_TYPE_MEM, /* res_type */ |
Arthur Heymans | 62f788e | 2023-11-21 18:49:33 +0100 | [diff] [blame] | 2314 | gen_flags, /* gen_flags */ |
| 2315 | type_flags, /* type_flags */ |
| 2316 | 0, /* gran */ |
| 2317 | mmio_base, /* range_min */ |
| 2318 | mmio_limit, /* range_max */ |
| 2319 | 0x0, /* translation */ |
| 2320 | mmio_limit - mmio_base + 1); /* length */ |
Felix Held | 21594fd1 | 2023-05-09 16:39:57 +0200 | [diff] [blame] | 2321 | } |
| 2322 | |
Arthur Heymans | 62f788e | 2023-11-21 18:49:33 +0100 | [diff] [blame] | 2323 | static void acpigen_resource_mmio64(u64 mmio_base, u64 mmio_limit, u16 gen_flags, |
| 2324 | u16 type_flags) |
Felix Held | 21594fd1 | 2023-05-09 16:39:57 +0200 | [diff] [blame] | 2325 | { |
| 2326 | acpigen_resource_qword(RSRC_TYPE_MEM, /* res_type */ |
Arthur Heymans | 62f788e | 2023-11-21 18:49:33 +0100 | [diff] [blame] | 2327 | gen_flags, /* gen_flags */ |
| 2328 | type_flags, /* type_flags */ |
| 2329 | 0, /* gran */ |
| 2330 | mmio_base, /* range_min */ |
| 2331 | mmio_limit, /* range_max */ |
| 2332 | 0x0, /* translation */ |
| 2333 | mmio_limit - mmio_base + 1); /* length */ |
| 2334 | } |
| 2335 | |
| 2336 | static void acpigen_resource_mmio(u64 mmio_base, u64 mmio_limit, bool is_producer, u16 type_flags) |
| 2337 | { |
| 2338 | const u16 gen_flags = ADDR_SPACE_GENERAL_FLAG_MAX_FIXED |
| 2339 | | ADDR_SPACE_GENERAL_FLAG_MIN_FIXED |
| 2340 | | ADDR_SPACE_GENERAL_FLAG_DEC_POS |
| 2341 | | (is_producer ? ADDR_SPACE_GENERAL_FLAG_PRODUCER |
| 2342 | : ADDR_SPACE_GENERAL_FLAG_CONSUMER); |
| 2343 | |
| 2344 | if (mmio_base < 4ULL * GiB && mmio_limit < 4ULL * GiB) |
| 2345 | acpigen_resource_mmio32(mmio_base, mmio_limit, gen_flags, type_flags); |
| 2346 | else |
| 2347 | acpigen_resource_mmio64(mmio_base, mmio_limit, gen_flags, type_flags); |
Felix Held | 21594fd1 | 2023-05-09 16:39:57 +0200 | [diff] [blame] | 2348 | } |
| 2349 | |
Felix Held | 0fdede0 | 2023-05-27 01:02:37 +0200 | [diff] [blame] | 2350 | void acpigen_resource_producer_mmio(u64 mmio_base, u64 mmio_limit, u16 type_flags) |
Felix Held | 21594fd1 | 2023-05-09 16:39:57 +0200 | [diff] [blame] | 2351 | { |
Arthur Heymans | 62f788e | 2023-11-21 18:49:33 +0100 | [diff] [blame] | 2352 | acpigen_resource_mmio(mmio_base, mmio_limit, true, type_flags); |
| 2353 | } |
| 2354 | |
| 2355 | void acpigen_resource_consumer_mmio(u64 mmio_base, u64 mmio_limit, u16 type_flags) |
| 2356 | { |
| 2357 | acpigen_resource_mmio(mmio_base, mmio_limit, false, type_flags); |
Felix Held | 21594fd1 | 2023-05-09 16:39:57 +0200 | [diff] [blame] | 2358 | } |
| 2359 | |
Furquan Shaikh | 1a82923 | 2020-04-23 11:11:05 -0700 | [diff] [blame] | 2360 | void acpigen_write_ADR(uint64_t adr) |
| 2361 | { |
| 2362 | acpigen_write_name_qword("_ADR", adr); |
| 2363 | } |
| 2364 | |
Duncan Laurie | 08a942f | 2020-04-29 12:13:14 -0700 | [diff] [blame] | 2365 | /** |
| 2366 | * acpigen_write_ADR_soundwire_device() - SoundWire ACPI Device Address Encoding. |
| 2367 | * @address: SoundWire device address properties. |
| 2368 | * |
| 2369 | * From SoundWire Discovery and Configuration Specification Version 1.0 Table 3. |
| 2370 | * |
| 2371 | * 63..52 - Reserved (0) |
| 2372 | * 51..48 - Zero-based SoundWire Link ID, relative to the immediate parent. |
| 2373 | * Used when a Controller has multiple master devices, each producing a |
| 2374 | * separate SoundWire Link. Set to 0 for single-link controllers. |
| 2375 | * 47..0 - SoundWire Device ID Encoding from specification version 1.2 table 88 |
| 2376 | * 47..44 - SoundWire specification version that this device supports |
| 2377 | * 43..40 - Unique ID for multiple devices |
| 2378 | * 39..24 - MIPI standard manufacturer code |
| 2379 | * 23..08 - Vendor defined part ID |
| 2380 | * 07..00 - MIPI class encoding |
| 2381 | */ |
| 2382 | void acpigen_write_ADR_soundwire_device(const struct soundwire_address *address) |
| 2383 | { |
| 2384 | acpigen_write_ADR((((uint64_t)address->link_id & 0xf) << 48) | |
| 2385 | (((uint64_t)address->version & 0xf) << 44) | |
| 2386 | (((uint64_t)address->unique_id & 0xf) << 40) | |
| 2387 | (((uint64_t)address->manufacturer_id & 0xffff) << 24) | |
| 2388 | (((uint64_t)address->part_id & 0xffff) << 8) | |
| 2389 | (((uint64_t)address->class & 0xff))); |
| 2390 | } |
Tim Wawrzynczak | f694502 | 2020-06-04 15:18:47 -0600 | [diff] [blame] | 2391 | |
| 2392 | void acpigen_notify(const char *namestr, int value) |
| 2393 | { |
| 2394 | acpigen_emit_byte(NOTIFY_OP); |
| 2395 | acpigen_emit_namestring(namestr); |
| 2396 | acpigen_write_integer(value); |
| 2397 | } |
Aaron Durbin | 80bc091 | 2020-08-19 23:17:42 -0600 | [diff] [blame] | 2398 | |
| 2399 | static void _create_field(uint8_t aml_op, uint8_t srcop, size_t byte_offset, const char *name) |
| 2400 | { |
| 2401 | acpigen_emit_byte(aml_op); |
| 2402 | acpigen_emit_byte(srcop); |
| 2403 | acpigen_write_integer(byte_offset); |
| 2404 | acpigen_emit_namestring(name); |
| 2405 | } |
| 2406 | |
| 2407 | void acpigen_write_create_byte_field(uint8_t op, size_t byte_offset, const char *name) |
| 2408 | { |
| 2409 | _create_field(CREATE_BYTE_OP, op, byte_offset, name); |
| 2410 | } |
| 2411 | |
| 2412 | void acpigen_write_create_word_field(uint8_t op, size_t byte_offset, const char *name) |
| 2413 | { |
| 2414 | _create_field(CREATE_WORD_OP, op, byte_offset, name); |
| 2415 | } |
| 2416 | |
| 2417 | void acpigen_write_create_dword_field(uint8_t op, size_t byte_offset, const char *name) |
| 2418 | { |
| 2419 | _create_field(CREATE_DWORD_OP, op, byte_offset, name); |
| 2420 | } |
| 2421 | |
| 2422 | void acpigen_write_create_qword_field(uint8_t op, size_t byte_offset, const char *name) |
| 2423 | { |
| 2424 | _create_field(CREATE_QWORD_OP, op, byte_offset, name); |
| 2425 | } |
Jason Glenesk | ca36aed | 2020-09-15 21:01:57 -0700 | [diff] [blame] | 2426 | |
| 2427 | void acpigen_write_pct_package(const acpi_addr_t *perf_ctrl, const acpi_addr_t *perf_sts) |
| 2428 | { |
| 2429 | acpigen_write_name("_PCT"); |
| 2430 | acpigen_write_package(0x02); |
| 2431 | acpigen_write_register_resource(perf_ctrl); |
| 2432 | acpigen_write_register_resource(perf_sts); |
| 2433 | |
| 2434 | acpigen_pop_len(); |
| 2435 | } |
| 2436 | |
| 2437 | void acpigen_write_xpss_package(const struct acpi_xpss_sw_pstate *pstate_value) |
| 2438 | { |
| 2439 | acpigen_write_package(0x08); |
| 2440 | acpigen_write_dword(pstate_value->core_freq); |
| 2441 | acpigen_write_dword(pstate_value->power); |
| 2442 | acpigen_write_dword(pstate_value->transition_latency); |
| 2443 | acpigen_write_dword(pstate_value->bus_master_latency); |
| 2444 | |
| 2445 | acpigen_write_byte_buffer((uint8_t *)&pstate_value->control_value, sizeof(uint64_t)); |
| 2446 | acpigen_write_byte_buffer((uint8_t *)&pstate_value->status_value, sizeof(uint64_t)); |
| 2447 | acpigen_write_byte_buffer((uint8_t *)&pstate_value->control_mask, sizeof(uint64_t)); |
| 2448 | acpigen_write_byte_buffer((uint8_t *)&pstate_value->status_mask, sizeof(uint64_t)); |
| 2449 | |
| 2450 | acpigen_pop_len(); |
| 2451 | } |
| 2452 | |
| 2453 | void acpigen_write_xpss_object(const struct acpi_xpss_sw_pstate *pstate_values, size_t nentries) |
| 2454 | { |
| 2455 | size_t pstate; |
| 2456 | |
| 2457 | acpigen_write_name("XPSS"); |
| 2458 | acpigen_write_package(nentries); |
| 2459 | for (pstate = 0; pstate < nentries; pstate++) { |
| 2460 | acpigen_write_xpss_package(pstate_values); |
| 2461 | pstate_values++; |
| 2462 | } |
| 2463 | |
| 2464 | acpigen_pop_len(); |
| 2465 | } |
Duncan Laurie | ec2e3e4 | 2020-11-03 15:19:08 -0800 | [diff] [blame] | 2466 | |
| 2467 | /* Delay up to wait_ms until provided namestr matches expected value. */ |
| 2468 | void acpigen_write_delay_until_namestr_int(uint32_t wait_ms, const char *name, uint64_t value) |
| 2469 | { |
| 2470 | uint32_t wait_ms_segment = 1; |
| 2471 | uint32_t segments = wait_ms; |
| 2472 | |
Sukumar Ghorai | 9b3c5af | 2023-12-07 18:33:43 -0800 | [diff] [blame] | 2473 | /* Sleep in 2ms segments if delay is more than 2ms. */ |
| 2474 | if (wait_ms > 2) { |
| 2475 | wait_ms_segment = 2; |
| 2476 | segments = wait_ms / wait_ms_segment; |
Duncan Laurie | ec2e3e4 | 2020-11-03 15:19:08 -0800 | [diff] [blame] | 2477 | } |
| 2478 | |
| 2479 | acpigen_write_store_int_to_op(segments, LOCAL7_OP); |
| 2480 | acpigen_emit_byte(WHILE_OP); |
| 2481 | acpigen_write_len_f(); |
| 2482 | acpigen_emit_byte(LGREATER_OP); |
| 2483 | acpigen_emit_byte(LOCAL7_OP); |
| 2484 | acpigen_emit_byte(ZERO_OP); |
| 2485 | |
| 2486 | /* If name is not provided then just delay in a loop. */ |
| 2487 | if (name) { |
| 2488 | acpigen_write_if_lequal_namestr_int(name, value); |
| 2489 | acpigen_emit_byte(BREAK_OP); |
| 2490 | acpigen_pop_len(); /* If */ |
| 2491 | } |
| 2492 | |
| 2493 | acpigen_write_sleep(wait_ms_segment); |
| 2494 | acpigen_emit_byte(DECREMENT_OP); |
| 2495 | acpigen_emit_byte(LOCAL7_OP); |
| 2496 | acpigen_pop_len(); /* While */ |
Cliff Huang | f97598f | 2023-03-01 14:30:41 -0800 | [diff] [blame] | 2497 | |
| 2498 | if (name) { |
| 2499 | acpigen_write_if_lequal_op_op(LOCAL7_OP, ZERO_OP); |
| 2500 | acpigen_write_debug_sprintf("WARN: Wait loop timeout for variable %s", |
| 2501 | name); |
| 2502 | acpigen_pop_len(); /* If */ |
| 2503 | } |
Duncan Laurie | ec2e3e4 | 2020-11-03 15:19:08 -0800 | [diff] [blame] | 2504 | } |
Arthur Heymans | 0eb5974 | 2023-04-25 16:23:37 +0200 | [diff] [blame] | 2505 | |
| 2506 | void acpigen_ssdt_override_sleep_states(bool enable_s1, bool enable_s2, bool enable_s3, |
| 2507 | bool enable_s4) |
| 2508 | { |
| 2509 | assert(!(enable_s1 && CONFIG(ACPI_S1_NOT_SUPPORTED))); |
| 2510 | assert(!(enable_s3 && !CONFIG(HAVE_ACPI_RESUME))); |
| 2511 | assert(!(enable_s4 && CONFIG(DISABLE_ACPI_HIBERNATE))); |
| 2512 | |
| 2513 | acpigen_write_scope("\\"); |
| 2514 | uint32_t sleep_enable = (enable_s1 << 0) | (enable_s2 << 1) |
| 2515 | | (enable_s3 << 2) | (enable_s4 << 3); |
| 2516 | acpigen_write_name_dword("OSFG", sleep_enable); |
| 2517 | acpigen_pop_len(); |
| 2518 | } |