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