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