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