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