blob: 368b86c576f8c0cd112cfd31e226e271784f2d6f [file] [log] [blame]
Patrick Georgi11f00792020-03-04 15:10:45 +01001/* SPDX-License-Identifier: GPL-2.0-only */
Duncan Lauried9af3ce2016-05-08 18:15:25 -07002
Furquan Shaikhd1130af2020-04-23 12:51:42 -07003#include <assert.h>
Duncan Lauried9af3ce2016-05-08 18:15:25 -07004#include <string.h>
Furquan Shaikh76cedd22020-05-02 10:24:23 -07005#include <acpi/acpi.h>
6#include <acpi/acpi_device.h>
7#include <acpi/acpigen.h>
Tim Wawrzynczakd40a4c22021-02-25 13:14:49 -07008#include <acpi/acpigen_pci.h>
Duncan Lauried9af3ce2016-05-08 18:15:25 -07009#include <device/device.h>
10#include <device/path.h>
Elyes HAOUAS70a03dd2019-12-02 20:47:50 +010011#include <stdlib.h>
Elyes HAOUASa83a7db2020-07-22 11:42:53 +020012#include <types.h>
Patrick Rudolphc83bab62019-12-13 12:16:06 +010013#include <crc_byte.h>
14
Julius Wernercd49cce2019-03-05 16:53:33 -080015#if CONFIG(GENERIC_GPIO_LIB)
Duncan Lauriecfb6ea72016-05-09 17:08:38 -070016#include <gpio.h>
17#endif
Duncan Lauried9af3ce2016-05-08 18:15:25 -070018
Duncan Laurieffc99902016-07-02 19:56:06 -070019#define ACPI_DP_UUID "daffd814-6eba-4d8c-8a91-bc9bbf4aa301"
20#define ACPI_DP_CHILD_UUID "dbb8e3e6-5886-4ba6-8795-1319f52a966b"
Duncan Laurie559e9472016-05-10 13:18:17 -070021
Kapil Porwal75436272022-11-28 17:25:48 +053022/*
23 * Below properties are defined at
24 * https://docs.microsoft.com/en-us/windows-hardware/drivers/pci/dsd-for-pcie-root-ports
25 */
26#define ACPI_DSD_EXTERNAL_FACING_PORT_UUID "EFCC06CC-73AC-4BC3-BFF0-76143807C389"
27#define ACPI_DSD_EXTERNAL_FACING_PORT_NAME "ExternalFacingPort"
28
29#define ACPI_DSD_HOTPLUG_IN_D3_UUID "6211E2C0-58A3-4AF3-90E1-927A4E0C55A4"
30#define ACPI_DSD_HOTPLUG_IN_D3_NAME "HotPlugSupportInD3"
31
32/* ID for the DmaProperty _DSD */
33#define ACPI_DSD_DMA_PROPERTY_UUID "70D24161-6DD5-4C9E-8070-705531292865"
34#define ACPI_DSD_DMA_PROPERTY_NAME "DmaProperty"
35
36/*
37 * Below properties are defined at
38 * https://docs.microsoft.com/en-us/windows-hardware/design/component-guidelines/power-management-for-storage-hardware-devices-intro
39 */
40#define ACPI_DSD_STORAGE_D3_UUID "5025030F-842F-4AB4-A561-99A5189762D0"
41#define ACPI_DSD_STORAGE_D3_NAME "StorageD3Enable"
42
Duncan Laurie6b7c1f62016-05-09 15:38:36 -070043/* Write empty word value and return pointer to it */
44static void *acpi_device_write_zero_len(void)
45{
46 char *p = acpigen_get_current();
47 acpigen_emit_word(0);
48 return p;
49}
50
51/* Fill in length value from start to current at specified location */
52static void acpi_device_fill_from_len(char *ptr, char *start)
53{
54 uint16_t len = acpigen_get_current() - start;
55 ptr[0] = len & 0xff;
56 ptr[1] = (len >> 8) & 0xff;
57}
58
59/*
60 * Fill in the length field with the value calculated from after
61 * the 16bit field to acpigen current as this length value does
62 * not include the length field itself.
63 */
64static void acpi_device_fill_len(void *ptr)
65{
66 acpi_device_fill_from_len(ptr, ptr + sizeof(uint16_t));
67}
68
Duncan Lauried9af3ce2016-05-08 18:15:25 -070069/* Locate and return the ACPI name for this device */
Aamir Bohrae825d3f2019-07-30 10:11:41 +053070const char *acpi_device_name(const struct device *dev)
Duncan Lauried9af3ce2016-05-08 18:15:25 -070071{
Aamir Bohrae825d3f2019-07-30 10:11:41 +053072 const struct device *pdev = dev;
Duncan Laurie47029142018-05-07 14:28:53 -070073 const char *name = NULL;
74
Duncan Lauried9af3ce2016-05-08 18:15:25 -070075 if (!dev)
76 return NULL;
77
78 /* Check for device specific handler */
CoolStar42be8992023-08-24 02:00:50 -070079 if (dev->ops && dev->ops->acpi_name) {
80 name = dev->ops->acpi_name(dev);
81 if (name)
82 return name;
83 }
Duncan Lauried9af3ce2016-05-08 18:15:25 -070084
Duncan Laurie47029142018-05-07 14:28:53 -070085 /* Walk up the tree to find if any parent can identify this device */
Arthur Heymans7fcd4d52023-08-24 15:12:19 +020086 while (pdev->upstream) {
87 pdev = pdev->upstream->dev;
Duncan Laurie47029142018-05-07 14:28:53 -070088 if (!pdev)
89 break;
Patrick Rudolphf95dbce2024-01-22 15:39:46 +010090 if (is_root_device(pdev))
Duncan Laurie47029142018-05-07 14:28:53 -070091 break;
92 if (pdev->ops && pdev->ops->acpi_name)
93 name = pdev->ops->acpi_name(dev);
94 if (name)
95 return name;
96 }
Duncan Lauried9af3ce2016-05-08 18:15:25 -070097
98 return NULL;
99}
100
Patrick Rudolpheeb8e742019-08-20 08:20:01 +0200101/* Locate and return the ACPI _HID (Hardware ID) for this device */
102const char *acpi_device_hid(const struct device *dev)
103{
104 if (!dev)
105 return NULL;
106
107 /* Check for device specific handler */
108 if (dev->ops->acpi_hid)
109 return dev->ops->acpi_hid(dev);
110
111 /*
112 * Don't walk up the tree to find any parent that can identify this device, as
113 * PNP devices are hard to identify.
114 */
115
116 return NULL;
117}
118
Patrick Rudolphc83bab62019-12-13 12:16:06 +0100119/*
120 * Generate unique ID based on the ACPI path.
121 * Collisions on the same _HID are possible but very unlikely.
122 */
Furquan Shaikhd14d03a2020-04-24 21:27:29 -0700123uint32_t acpi_device_uid(const struct device *dev)
Patrick Rudolphc83bab62019-12-13 12:16:06 +0100124{
125 const char *path = acpi_device_path(dev);
126 if (!path)
127 return 0;
128
129 return CRC(path, strlen(path), crc32_byte);
130}
131
Duncan Lauried9af3ce2016-05-08 18:15:25 -0700132/* Recursive function to find the root device and print a path from there */
Aamir Bohrae825d3f2019-07-30 10:11:41 +0530133static ssize_t acpi_device_path_fill(const struct device *dev, char *buf,
Duncan Laurie47029142018-05-07 14:28:53 -0700134 size_t buf_len, size_t cur)
Duncan Lauried9af3ce2016-05-08 18:15:25 -0700135{
136 const char *name = acpi_device_name(dev);
Duncan Laurie47029142018-05-07 14:28:53 -0700137 ssize_t next = 0;
138
139 if (!name)
140 return -1;
Duncan Lauried9af3ce2016-05-08 18:15:25 -0700141
142 /*
143 * Make sure this name segment will fit, including the path segment
144 * separator and possible NUL terminator if this is the last segment.
145 */
Duncan Laurie47029142018-05-07 14:28:53 -0700146 if (!dev || (cur + strlen(name) + 2) > buf_len)
Duncan Lauried9af3ce2016-05-08 18:15:25 -0700147 return cur;
148
149 /* Walk up the tree to the root device */
Arthur Heymans7fcd4d52023-08-24 15:12:19 +0200150 if (!is_root_device(dev) && dev->upstream && dev->upstream->dev)
151 next = acpi_device_path_fill(dev->upstream->dev, buf, buf_len, cur);
Duncan Laurie47029142018-05-07 14:28:53 -0700152 if (next < 0)
153 return next;
Duncan Lauried9af3ce2016-05-08 18:15:25 -0700154
155 /* Fill in the path from the root device */
156 next += snprintf(buf + next, buf_len - next, "%s%s",
Patrick Rudolphf95dbce2024-01-22 15:39:46 +0100157 (is_root_device(dev) || (strlen(name) == 0)) ?
158 "" : ".", name);
Duncan Lauried9af3ce2016-05-08 18:15:25 -0700159
160 return next;
161}
162
163/*
164 * Warning: just as with dev_path() this uses a static buffer
Martin Roth0949e732021-10-01 14:28:22 -0600165 * so should not be called multiple times in one statement
Duncan Lauried9af3ce2016-05-08 18:15:25 -0700166 */
Aamir Bohrae825d3f2019-07-30 10:11:41 +0530167const char *acpi_device_path(const struct device *dev)
Duncan Lauried9af3ce2016-05-08 18:15:25 -0700168{
169 static char buf[DEVICE_PATH_MAX] = {};
170
171 if (!dev)
172 return NULL;
173
174 if (acpi_device_path_fill(dev, buf, sizeof(buf), 0) <= 0)
175 return NULL;
176
177 return buf;
178}
179
180/* Return the path of the parent device as the ACPI Scope for this device */
Aamir Bohrae825d3f2019-07-30 10:11:41 +0530181const char *acpi_device_scope(const struct device *dev)
Duncan Lauried9af3ce2016-05-08 18:15:25 -0700182{
Duncan Lauried02685b2016-06-30 14:37:37 -0700183 static char buf[DEVICE_PATH_MAX] = {};
184
Arthur Heymans7fcd4d52023-08-24 15:12:19 +0200185 if (!dev || !dev->upstream || !dev->upstream->dev)
Duncan Lauried9af3ce2016-05-08 18:15:25 -0700186 return NULL;
187
Arthur Heymans7fcd4d52023-08-24 15:12:19 +0200188 if (acpi_device_path_fill(dev->upstream->dev, buf, sizeof(buf), 0) <= 0)
Duncan Lauried02685b2016-06-30 14:37:37 -0700189 return NULL;
190
191 return buf;
Duncan Lauried9af3ce2016-05-08 18:15:25 -0700192}
193
Elyes HAOUAS6716bab2020-01-09 21:29:25 +0100194/* Concatenate the device path and provided name suffix */
Aamir Bohrae825d3f2019-07-30 10:11:41 +0530195const char *acpi_device_path_join(const struct device *dev, const char *name)
Duncan Lauried9af3ce2016-05-08 18:15:25 -0700196{
197 static char buf[DEVICE_PATH_MAX] = {};
Jacob Garberf2ba2d92019-07-02 10:50:10 -0600198 ssize_t len;
Duncan Lauried9af3ce2016-05-08 18:15:25 -0700199
200 if (!dev)
201 return NULL;
202
203 /* Build the path of this device */
204 len = acpi_device_path_fill(dev, buf, sizeof(buf), 0);
205 if (len <= 0)
206 return NULL;
207
208 /* Ensure there is room for the added name, separator, and NUL */
209 if ((len + strlen(name) + 2) > sizeof(buf))
210 return NULL;
211 snprintf(buf + len, sizeof(buf) - len, ".%s", name);
212
213 return buf;
214}
Duncan Laurie6b7c1f62016-05-09 15:38:36 -0700215
Hung-Te Linb4be50c2018-09-10 10:55:49 +0800216int acpi_device_status(const struct device *dev)
217{
218 if (!dev->enabled)
219 return ACPI_STATUS_DEVICE_ALL_OFF;
220 if (dev->hidden)
221 return ACPI_STATUS_DEVICE_HIDDEN_ON;
222 return ACPI_STATUS_DEVICE_ALL_ON;
223}
224
Patrick Rudolphc83bab62019-12-13 12:16:06 +0100225/* Write the unique _UID based on ACPI device path. */
Furquan Shaikhd14d03a2020-04-24 21:27:29 -0700226void acpi_device_write_uid(const struct device *dev)
Patrick Rudolphc83bab62019-12-13 12:16:06 +0100227{
228 acpigen_write_name_integer("_UID", acpi_device_uid(dev));
229}
230
Duncan Laurie6b7c1f62016-05-09 15:38:36 -0700231/* ACPI 6.1 section 6.4.3.6: Extended Interrupt Descriptor */
232void acpi_device_write_interrupt(const struct acpi_irq *irq)
233{
234 void *desc_length;
235 uint8_t flags;
236
237 if (!irq || !irq->pin)
238 return;
239
240 /* This is supported by GpioInt() but not Interrupt() */
Furquan Shaikh5b9b5932017-02-21 13:16:30 -0800241 if (irq->polarity == ACPI_IRQ_ACTIVE_BOTH)
Duncan Laurie6b7c1f62016-05-09 15:38:36 -0700242 return;
243
244 /* Byte 0: Descriptor Type */
245 acpigen_emit_byte(ACPI_DESCRIPTOR_INTERRUPT);
246
247 /* Byte 1-2: Length (filled in later) */
248 desc_length = acpi_device_write_zero_len();
249
250 /*
251 * Byte 3: Flags
252 * [7:5]: Reserved
253 * [4]: Wake (0=NO_WAKE 1=WAKE)
254 * [3]: Sharing (0=EXCLUSIVE 1=SHARED)
255 * [2]: Polarity (0=HIGH 1=LOW)
256 * [1]: Mode (0=LEVEL 1=EDGE)
257 * [0]: Resource (0=PRODUCER 1=CONSUMER)
258 */
259 flags = 1 << 0; /* ResourceConsumer */
Furquan Shaikh5b9b5932017-02-21 13:16:30 -0800260 if (irq->mode == ACPI_IRQ_EDGE_TRIGGERED)
Duncan Laurie6b7c1f62016-05-09 15:38:36 -0700261 flags |= 1 << 1;
Furquan Shaikh5b9b5932017-02-21 13:16:30 -0800262 if (irq->polarity == ACPI_IRQ_ACTIVE_LOW)
Duncan Laurie6b7c1f62016-05-09 15:38:36 -0700263 flags |= 1 << 2;
Furquan Shaikh5b9b5932017-02-21 13:16:30 -0800264 if (irq->shared == ACPI_IRQ_SHARED)
Duncan Laurie6b7c1f62016-05-09 15:38:36 -0700265 flags |= 1 << 3;
Furquan Shaikh5b9b5932017-02-21 13:16:30 -0800266 if (irq->wake == ACPI_IRQ_WAKE)
Duncan Laurie6b7c1f62016-05-09 15:38:36 -0700267 flags |= 1 << 4;
268 acpigen_emit_byte(flags);
269
270 /* Byte 4: Interrupt Table Entry Count */
271 acpigen_emit_byte(1);
272
273 /* Byte 5-8: Interrupt Number */
274 acpigen_emit_dword(irq->pin);
275
276 /* Fill in Descriptor Length (account for len word) */
277 acpi_device_fill_len(desc_length);
278}
Duncan Lauriecfb6ea72016-05-09 17:08:38 -0700279
280/* ACPI 6.1 section 6.4.3.8.1 - GPIO Interrupt or I/O */
281void acpi_device_write_gpio(const struct acpi_gpio *gpio)
282{
283 void *start, *desc_length;
284 void *pin_table_offset, *vendor_data_offset, *resource_offset;
285 uint16_t flags = 0;
286 int pin;
287
288 if (!gpio || gpio->type > ACPI_GPIO_TYPE_IO)
289 return;
290
291 start = acpigen_get_current();
292
293 /* Byte 0: Descriptor Type */
294 acpigen_emit_byte(ACPI_DESCRIPTOR_GPIO);
295
296 /* Byte 1-2: Length (fill in later) */
297 desc_length = acpi_device_write_zero_len();
298
299 /* Byte 3: Revision ID */
300 acpigen_emit_byte(ACPI_GPIO_REVISION_ID);
301
302 /* Byte 4: GpioIo or GpioInt */
303 acpigen_emit_byte(gpio->type);
304
305 /*
306 * Byte 5-6: General Flags
307 * [15:1]: 0 => Reserved
308 * [0]: 1 => ResourceConsumer
309 */
310 acpigen_emit_word(1 << 0);
311
312 switch (gpio->type) {
313 case ACPI_GPIO_TYPE_INTERRUPT:
314 /*
315 * Byte 7-8: GPIO Interrupt Flags
316 * [15:5]: 0 => Reserved
317 * [4]: Wake (0=NO_WAKE 1=WAKE)
318 * [3]: Sharing (0=EXCLUSIVE 1=SHARED)
319 * [2:1]: Polarity (0=HIGH 1=LOW 2=BOTH)
320 * [0]: Mode (0=LEVEL 1=EDGE)
321 */
Furquan Shaikh5b9b5932017-02-21 13:16:30 -0800322 if (gpio->irq.mode == ACPI_IRQ_EDGE_TRIGGERED)
Duncan Lauriecfb6ea72016-05-09 17:08:38 -0700323 flags |= 1 << 0;
Furquan Shaikh5b9b5932017-02-21 13:16:30 -0800324 if (gpio->irq.shared == ACPI_IRQ_SHARED)
Duncan Lauriecfb6ea72016-05-09 17:08:38 -0700325 flags |= 1 << 3;
Furquan Shaikh5b9b5932017-02-21 13:16:30 -0800326 if (gpio->irq.wake == ACPI_IRQ_WAKE)
Duncan Lauriecfb6ea72016-05-09 17:08:38 -0700327 flags |= 1 << 4;
328
329 switch (gpio->irq.polarity) {
Furquan Shaikh5b9b5932017-02-21 13:16:30 -0800330 case ACPI_IRQ_ACTIVE_HIGH:
Duncan Lauriecfb6ea72016-05-09 17:08:38 -0700331 flags |= 0 << 1;
332 break;
Furquan Shaikh5b9b5932017-02-21 13:16:30 -0800333 case ACPI_IRQ_ACTIVE_LOW:
Duncan Lauriecfb6ea72016-05-09 17:08:38 -0700334 flags |= 1 << 1;
335 break;
Furquan Shaikh5b9b5932017-02-21 13:16:30 -0800336 case ACPI_IRQ_ACTIVE_BOTH:
Duncan Lauriecfb6ea72016-05-09 17:08:38 -0700337 flags |= 2 << 1;
338 break;
339 }
340 break;
341
342 case ACPI_GPIO_TYPE_IO:
343 /*
344 * Byte 7-8: GPIO IO Flags
345 * [15:4]: 0 => Reserved
346 * [3]: Sharing (0=EXCLUSIVE 1=SHARED)
347 * [2]: 0 => Reserved
348 * [1:0]: IO Restriction
349 * 0 => IoRestrictionNone
350 * 1 => IoRestrictionInputOnly
351 * 2 => IoRestrictionOutputOnly
352 * 3 => IoRestrictionNoneAndPreserve
353 */
354 flags |= gpio->io_restrict & 3;
355 if (gpio->io_shared)
356 flags |= 1 << 3;
357 break;
358 }
359 acpigen_emit_word(flags);
360
361 /*
362 * Byte 9: Pin Configuration
363 * 0x01 => Default (no configuration applied)
364 * 0x02 => Pull-up
365 * 0x03 => Pull-down
366 * 0x04-0x7F => Reserved
367 * 0x80-0xff => Vendor defined
368 */
369 acpigen_emit_byte(gpio->pull);
370
371 /* Byte 10-11: Output Drive Strength in 1/100 mA */
372 acpigen_emit_word(gpio->output_drive_strength);
373
374 /* Byte 12-13: Debounce Timeout in 1/100 ms */
375 acpigen_emit_word(gpio->interrupt_debounce_timeout);
376
377 /* Byte 14-15: Pin Table Offset, relative to start */
378 pin_table_offset = acpi_device_write_zero_len();
379
380 /* Byte 16: Reserved */
381 acpigen_emit_byte(0);
382
383 /* Byte 17-18: Resource Source Name Offset, relative to start */
384 resource_offset = acpi_device_write_zero_len();
385
386 /* Byte 19-20: Vendor Data Offset, relative to start */
387 vendor_data_offset = acpi_device_write_zero_len();
388
389 /* Byte 21-22: Vendor Data Length */
390 acpigen_emit_word(0);
391
392 /* Fill in Pin Table Offset */
393 acpi_device_fill_from_len(pin_table_offset, start);
394
395 /* Pin Table, one word for each pin */
Duncan Laurie5b6c28c2016-06-29 10:47:22 -0700396 for (pin = 0; pin < gpio->pin_count; pin++) {
397 uint16_t acpi_pin = gpio->pins[pin];
Julius Wernercd49cce2019-03-05 16:53:33 -0800398#if CONFIG(GENERIC_GPIO_LIB)
Duncan Laurie5b6c28c2016-06-29 10:47:22 -0700399 acpi_pin = gpio_acpi_pin(acpi_pin);
400#endif
401 acpigen_emit_word(acpi_pin);
402 }
Duncan Lauriecfb6ea72016-05-09 17:08:38 -0700403
404 /* Fill in Resource Source Name Offset */
405 acpi_device_fill_from_len(resource_offset, start);
406
407 /* Resource Source Name String */
Julius Wernercd49cce2019-03-05 16:53:33 -0800408#if CONFIG(GENERIC_GPIO_LIB)
Duncan Lauriecfb6ea72016-05-09 17:08:38 -0700409 acpigen_emit_string(gpio->resource ? : gpio_acpi_path(gpio->pins[0]));
410#else
411 acpigen_emit_string(gpio->resource);
412#endif
413
414 /* Fill in Vendor Data Offset */
415 acpi_device_fill_from_len(vendor_data_offset, start);
416
417 /* Fill in GPIO Descriptor Length (account for len word) */
418 acpi_device_fill_len(desc_length);
419}
Duncan Laurie1010b4a2016-05-09 20:10:47 -0700420
421/* ACPI 6.1 section 6.4.3.8.2.1 - I2cSerialBus() */
422void acpi_device_write_i2c(const struct acpi_i2c *i2c)
423{
424 void *desc_length, *type_length;
425
426 /* Byte 0: Descriptor Type */
427 acpigen_emit_byte(ACPI_DESCRIPTOR_SERIAL_BUS);
428
429 /* Byte 1+2: Length (filled in later) */
430 desc_length = acpi_device_write_zero_len();
431
432 /* Byte 3: Revision ID */
Elyes HAOUAS34564ed2019-04-16 08:12:10 +0200433 acpigen_emit_byte(ACPI_I2C_SERIAL_BUS_REVISION_ID);
Duncan Laurie1010b4a2016-05-09 20:10:47 -0700434
435 /* Byte 4: Resource Source Index is Reserved */
436 acpigen_emit_byte(0);
437
438 /* Byte 5: Serial Bus Type is I2C */
439 acpigen_emit_byte(ACPI_SERIAL_BUS_TYPE_I2C);
440
441 /*
442 * Byte 6: Flags
443 * [7:2]: 0 => Reserved
444 * [1]: 1 => ResourceConsumer
445 * [0]: 0 => ControllerInitiated
446 */
447 acpigen_emit_byte(1 << 1);
448
449 /*
450 * Byte 7-8: Type Specific Flags
451 * [15:1]: 0 => Reserved
452 * [0]: 0 => 7bit, 1 => 10bit
453 */
454 acpigen_emit_word(i2c->mode_10bit);
455
456 /* Byte 9: Type Specific Revision ID */
Elyes HAOUAS34564ed2019-04-16 08:12:10 +0200457 acpigen_emit_byte(ACPI_I2C_TYPE_SPECIFIC_REVISION_ID);
Duncan Laurie1010b4a2016-05-09 20:10:47 -0700458
459 /* Byte 10-11: I2C Type Data Length */
460 type_length = acpi_device_write_zero_len();
461
462 /* Byte 12-15: I2C Bus Speed */
463 acpigen_emit_dword(i2c->speed);
464
465 /* Byte 16-17: I2C Slave Address */
466 acpigen_emit_word(i2c->address);
467
468 /* Fill in Type Data Length */
469 acpi_device_fill_len(type_length);
470
471 /* Byte 18+: ResourceSource */
472 acpigen_emit_string(i2c->resource);
473
474 /* Fill in I2C Descriptor Length */
475 acpi_device_fill_len(desc_length);
476}
Duncan Laurie70c86d92016-05-10 07:26:34 -0700477
478/* ACPI 6.1 section 6.4.3.8.2.2 - SpiSerialBus() */
479void acpi_device_write_spi(const struct acpi_spi *spi)
480{
481 void *desc_length, *type_length;
482 uint16_t flags = 0;
483
484 /* Byte 0: Descriptor Type */
485 acpigen_emit_byte(ACPI_DESCRIPTOR_SERIAL_BUS);
486
487 /* Byte 1+2: Length (filled in later) */
488 desc_length = acpi_device_write_zero_len();
489
490 /* Byte 3: Revision ID */
Elyes HAOUAS34564ed2019-04-16 08:12:10 +0200491 acpigen_emit_byte(ACPI_SPI_SERIAL_BUS_REVISION_ID);
Duncan Laurie70c86d92016-05-10 07:26:34 -0700492
493 /* Byte 4: Resource Source Index is Reserved */
494 acpigen_emit_byte(0);
495
496 /* Byte 5: Serial Bus Type is SPI */
497 acpigen_emit_byte(ACPI_SERIAL_BUS_TYPE_SPI);
498
499 /*
500 * Byte 6: Flags
501 * [7:2]: 0 => Reserved
502 * [1]: 1 => ResourceConsumer
503 * [0]: 0 => ControllerInitiated
504 */
505 acpigen_emit_byte(1 << 1);
506
507 /*
508 * Byte 7-8: Type Specific Flags
509 * [15:2]: 0 => Reserved
510 * [1]: 0 => ActiveLow, 1 => ActiveHigh
511 * [0]: 0 => FourWire, 1 => ThreeWire
512 */
513 if (spi->wire_mode == SPI_3_WIRE_MODE)
514 flags |= 1 << 0;
515 if (spi->device_select_polarity == SPI_POLARITY_HIGH)
516 flags |= 1 << 1;
517 acpigen_emit_word(flags);
518
519 /* Byte 9: Type Specific Revision ID */
Elyes HAOUAS34564ed2019-04-16 08:12:10 +0200520 acpigen_emit_byte(ACPI_SPI_TYPE_SPECIFIC_REVISION_ID);
Duncan Laurie70c86d92016-05-10 07:26:34 -0700521
522 /* Byte 10-11: SPI Type Data Length */
523 type_length = acpi_device_write_zero_len();
524
525 /* Byte 12-15: Connection Speed */
526 acpigen_emit_dword(spi->speed);
527
528 /* Byte 16: Data Bit Length */
529 acpigen_emit_byte(spi->data_bit_length);
530
531 /* Byte 17: Clock Phase */
532 acpigen_emit_byte(spi->clock_phase);
533
534 /* Byte 18: Clock Polarity */
535 acpigen_emit_byte(spi->clock_polarity);
536
537 /* Byte 19-20: Device Selection */
538 acpigen_emit_word(spi->device_select);
539
540 /* Fill in Type Data Length */
541 acpi_device_fill_len(type_length);
542
543 /* Byte 21+: ResourceSource String */
544 acpigen_emit_string(spi->resource);
545
546 /* Fill in SPI Descriptor Length */
547 acpi_device_fill_len(desc_length);
548}
Duncan Laurie559e9472016-05-10 13:18:17 -0700549
Duncan Lauriedccef0d2020-05-27 12:29:51 -0700550/* UART Serial Bus - UARTSerialBusV2() */
551void acpi_device_write_uart(const struct acpi_uart *uart)
552{
553 void *desc_length, *type_length;
554 uint16_t flags;
555
556 /* Byte 0: Descriptor Type */
557 acpigen_emit_byte(ACPI_DESCRIPTOR_SERIAL_BUS);
558
559 /* Byte 1+2: Length (filled in later) */
560 desc_length = acpi_device_write_zero_len();
561
562 /* Byte 3: Revision ID */
563 acpigen_emit_byte(ACPI_UART_SERIAL_BUS_REVISION_ID);
564
565 /* Byte 4: Resource Source Index is Reserved */
566 acpigen_emit_byte(0);
567
568 /* Byte 5: Serial Bus Type is UART */
569 acpigen_emit_byte(ACPI_SERIAL_BUS_TYPE_UART);
570
571 /*
572 * Byte 6: Flags
573 * [7:2]: 0 => Reserved
574 * [1]: 1 => ResourceConsumer
575 * [0]: 0 => ControllerInitiated
576 */
577 acpigen_emit_byte(BIT(1));
578
579 /*
580 * Byte 7-8: Type Specific Flags
581 * [15:8]: 0 => Reserved
582 * [7]: 0 => Little Endian, 1 => Big Endian
583 * [6:4]: Data bits
584 * [3:2]: Stop bits
585 * [1:0]: Flow control
586 */
587 flags = uart->flow_control & 3;
588 flags |= (uart->stop_bits & 3) << 2;
589 flags |= (uart->data_bits & 7) << 4;
590 flags |= (uart->endian & 1) << 7;
591 acpigen_emit_word(flags);
592
593 /* Byte 9: Type Specific Revision ID */
594 acpigen_emit_byte(ACPI_UART_TYPE_SPECIFIC_REVISION_ID);
595
596 /* Byte 10-11: Type Data Length */
597 type_length = acpi_device_write_zero_len();
598
599 /* Byte 12-15: Initial Baud Rate */
600 acpigen_emit_dword(uart->initial_baud_rate);
601
602 /* Byte 16-17: RX FIFO size */
603 acpigen_emit_word(uart->rx_fifo_bytes);
604
605 /* Byte 18-19: TX FIFO size */
606 acpigen_emit_word(uart->tx_fifo_bytes);
607
608 /* Byte 20: Parity */
609 acpigen_emit_byte(uart->parity);
610
611 /* Byte 21: Lines Enabled */
612 acpigen_emit_byte(uart->lines_in_use);
613
614 /* Fill in Type Data Length */
615 acpi_device_fill_len(type_length);
616
617 /* Byte 22+: ResourceSource */
618 acpigen_emit_string(uart->resource);
619
620 /* Fill in Descriptor Length */
621 acpi_device_fill_len(desc_length);
622}
623
Raul E Rangel6d2bc002021-05-27 15:04:21 -0600624#define ACPI_POWER_RESOURCE_STATUS_ON_OP ONE_OP
625#define ACPI_POWER_RESOURCE_STATUS_OFF_OP ZERO_OP
626
627/**
628 * Writes an ACPI fragment that will check the GPIO and return 0 if the GPIO
629 * state does not match the active parameter.
630 */
631static void acpigen_write_gpio_STA(const struct acpi_gpio *gpio, bool active)
632{
633 if (!gpio || !gpio->pin_count)
634 return;
635
636 /* Read current GPIO status into Local0. */
637 acpigen_get_tx_gpio(gpio);
638
639 /*
640 * If (!Local0)
641 * {
642 * Return (Zero)
643 * }
644 */
645 acpigen_write_if();
646 if (active)
647 acpigen_emit_byte(LNOT_OP);
648 acpigen_emit_byte(LOCAL0_OP);
649 acpigen_write_return_op(ACPI_POWER_RESOURCE_STATUS_OFF_OP);
650 acpigen_write_if_end();
651}
652
653static void acpigen_write_power_res_STA(const struct acpi_power_res_params *params)
654{
655 acpigen_write_method_serialized("_STA", 0);
656
657 /* Verify all the GPIOs are in the ON state, otherwise return 0 */
658 acpigen_write_gpio_STA(params->enable_gpio, true);
659 acpigen_write_gpio_STA(params->reset_gpio, false);
660 acpigen_write_gpio_STA(params->stop_gpio, false);
661
662 /* All GPIOs are in the ON state */
663 acpigen_write_return_op(ACPI_POWER_RESOURCE_STATUS_ON_OP);
664
665 acpigen_pop_len(); /* Method */
666}
667
Duncan Lauriebd73dbb2017-02-17 17:05:03 -0800668/* PowerResource() with Enable and/or Reset control */
Shelley Chena0603392018-04-26 13:52:30 -0700669void acpi_device_add_power_res(const struct acpi_power_res_params *params)
Duncan Lauriebd73dbb2017-02-17 17:05:03 -0800670{
Furquan Shaikhd47946e2021-09-14 15:54:41 -0700671 static uint8_t id;
Furquan Shaikhdc782752020-04-30 22:49:39 -0700672 static const char * const power_res_dev_states[] = { "_PR0", "_PR3" };
Edward O'Callaghan7e262552020-01-23 10:32:33 +1100673 unsigned int reset_gpio = params->reset_gpio ? params->reset_gpio->pins[0] : 0;
674 unsigned int enable_gpio = params->enable_gpio ? params->enable_gpio->pins[0] : 0;
675 unsigned int stop_gpio = params->stop_gpio ? params->stop_gpio->pins[0] : 0;
Furquan Shaikhd47946e2021-09-14 15:54:41 -0700676 char pr_name[ACPI_NAME_BUFFER_SIZE];
Duncan Lauriebd73dbb2017-02-17 17:05:03 -0800677
Furquan Shaikhedf459f2017-08-28 17:20:49 -0700678 if (!reset_gpio && !enable_gpio && !stop_gpio)
Duncan Lauriebd73dbb2017-02-17 17:05:03 -0800679 return;
680
Furquan Shaikhd47946e2021-09-14 15:54:41 -0700681 snprintf(pr_name, sizeof(pr_name), "PR%02X", id++);
682
683 /* PowerResource (PR##, 0, 0) */
684 acpigen_write_power_res(pr_name, 0, 0, power_res_dev_states,
Duncan Lauriebd73dbb2017-02-17 17:05:03 -0800685 ARRAY_SIZE(power_res_dev_states));
686
Raul E Rangel6d2bc002021-05-27 15:04:21 -0600687 if (params->use_gpio_for_status) {
688 acpigen_write_power_res_STA(params);
689 } else {
690 /* Method (_STA, 0, NotSerialized) { Return (0x1) } */
691 acpigen_write_STA(ACPI_POWER_RESOURCE_STATUS_ON_OP);
692 }
Duncan Lauriebd73dbb2017-02-17 17:05:03 -0800693
694 /* Method (_ON, 0, Serialized) */
695 acpigen_write_method_serialized("_ON", 0);
Tim Van Patten3d4665c2022-04-13 11:53:20 -0600696 /* Call _STA and early return if the device is already enabled, since the Linux
697 kernel doesn't check the device status before calling _ON. This avoids
698 unnecessary delays while booting. */
699 if (params->use_gpio_for_status) {
700 /* Local0 = _STA () */
701 acpigen_write_store();
702 acpigen_emit_namestring("_STA");
703 acpigen_emit_byte(LOCAL0_OP);
704 /* If (( Local0 == ACPI_POWER_RESOURCE_STATUS_ON_OP)) */
705 acpigen_write_if_lequal_op_op(LOCAL0_OP, ACPI_POWER_RESOURCE_STATUS_ON_OP);
706 acpigen_write_return_op(ZERO_OP);
707 acpigen_write_if_end();
708 }
Duncan Lauriebd73dbb2017-02-17 17:05:03 -0800709 if (reset_gpio)
Shelley Chena0603392018-04-26 13:52:30 -0700710 acpigen_enable_tx_gpio(params->reset_gpio);
Duncan Lauriebd73dbb2017-02-17 17:05:03 -0800711 if (enable_gpio) {
Shelley Chena0603392018-04-26 13:52:30 -0700712 acpigen_enable_tx_gpio(params->enable_gpio);
713 if (params->enable_delay_ms)
714 acpigen_write_sleep(params->enable_delay_ms);
Duncan Lauriebd73dbb2017-02-17 17:05:03 -0800715 }
716 if (reset_gpio) {
Shelley Chena0603392018-04-26 13:52:30 -0700717 acpigen_disable_tx_gpio(params->reset_gpio);
718 if (params->reset_delay_ms)
719 acpigen_write_sleep(params->reset_delay_ms);
Duncan Lauriebd73dbb2017-02-17 17:05:03 -0800720 }
Furquan Shaikhedf459f2017-08-28 17:20:49 -0700721 if (stop_gpio) {
Shelley Chena0603392018-04-26 13:52:30 -0700722 acpigen_disable_tx_gpio(params->stop_gpio);
723 if (params->stop_delay_ms)
724 acpigen_write_sleep(params->stop_delay_ms);
Furquan Shaikhedf459f2017-08-28 17:20:49 -0700725 }
Duncan Lauriebd73dbb2017-02-17 17:05:03 -0800726 acpigen_pop_len(); /* _ON method */
727
728 /* Method (_OFF, 0, Serialized) */
729 acpigen_write_method_serialized("_OFF", 0);
Shelley Chena0603392018-04-26 13:52:30 -0700730 if (stop_gpio) {
731 acpigen_enable_tx_gpio(params->stop_gpio);
732 if (params->stop_off_delay_ms)
733 acpigen_write_sleep(params->stop_off_delay_ms);
734 }
735 if (reset_gpio) {
736 acpigen_enable_tx_gpio(params->reset_gpio);
737 if (params->reset_off_delay_ms)
738 acpigen_write_sleep(params->reset_off_delay_ms);
739 }
740 if (enable_gpio) {
741 acpigen_disable_tx_gpio(params->enable_gpio);
742 if (params->enable_off_delay_ms)
743 acpigen_write_sleep(params->enable_off_delay_ms);
744 }
Duncan Lauriebd73dbb2017-02-17 17:05:03 -0800745 acpigen_pop_len(); /* _OFF method */
746
Furquan Shaikhd47946e2021-09-14 15:54:41 -0700747 acpigen_pop_len(); /* PowerResource PR## */
Duncan Lauriebd73dbb2017-02-17 17:05:03 -0800748}
749
Duncan Laurieffc99902016-07-02 19:56:06 -0700750static void acpi_dp_write_array(const struct acpi_dp *array);
751static void acpi_dp_write_value(const struct acpi_dp *prop)
Duncan Laurie559e9472016-05-10 13:18:17 -0700752{
753 switch (prop->type) {
754 case ACPI_DP_TYPE_INTEGER:
755 acpigen_write_integer(prop->integer);
756 break;
757 case ACPI_DP_TYPE_STRING:
Harsha Priya3a96ac42016-07-15 17:31:43 -0700758 case ACPI_DP_TYPE_CHILD:
Duncan Laurie559e9472016-05-10 13:18:17 -0700759 acpigen_write_string(prop->string);
760 break;
761 case ACPI_DP_TYPE_REFERENCE:
762 acpigen_emit_namestring(prop->string);
763 break;
Duncan Laurieffc99902016-07-02 19:56:06 -0700764 case ACPI_DP_TYPE_ARRAY:
765 acpi_dp_write_array(prop->array);
766 break;
767 default:
768 break;
Duncan Laurie559e9472016-05-10 13:18:17 -0700769 }
770}
771
Duncan Laurieffc99902016-07-02 19:56:06 -0700772/* Package (2) { "prop->name", VALUE } */
773static void acpi_dp_write_property(const struct acpi_dp *prop)
Duncan Laurie559e9472016-05-10 13:18:17 -0700774{
775 acpigen_write_package(2);
Duncan Laurieffc99902016-07-02 19:56:06 -0700776 acpigen_write_string(prop->name);
Duncan Laurie559e9472016-05-10 13:18:17 -0700777 acpi_dp_write_value(prop);
778 acpigen_pop_len();
Duncan Laurie559e9472016-05-10 13:18:17 -0700779}
780
781/* Write array of Device Properties */
Duncan Laurieffc99902016-07-02 19:56:06 -0700782static void acpi_dp_write_array(const struct acpi_dp *array)
Duncan Laurie559e9472016-05-10 13:18:17 -0700783{
Duncan Laurieffc99902016-07-02 19:56:06 -0700784 const struct acpi_dp *dp;
785 char *pkg_count;
786
787 /* Package element count determined as it is populated */
788 pkg_count = acpigen_write_package(0);
789
Furquan Shaikh35c01bc2016-10-03 23:30:14 -0700790 /*
791 * Only acpi_dp of type DP_TYPE_TABLE is allowed to be an array.
792 * DP_TYPE_TABLE does not have a value to be written. Thus, start
793 * the loop from next type in the array.
794 */
795 for (dp = array->next; dp; dp = dp->next) {
Duncan Laurieffc99902016-07-02 19:56:06 -0700796 acpi_dp_write_value(dp);
797 (*pkg_count)++;
798 }
799
Duncan Laurie559e9472016-05-10 13:18:17 -0700800 acpigen_pop_len();
Duncan Laurie559e9472016-05-10 13:18:17 -0700801}
802
Duncan Laurieffc99902016-07-02 19:56:06 -0700803static void acpi_dp_free(struct acpi_dp *dp)
Duncan Laurie559e9472016-05-10 13:18:17 -0700804{
Duncan Laurieffc99902016-07-02 19:56:06 -0700805 while (dp) {
806 struct acpi_dp *p = dp->next;
807
808 switch (dp->type) {
809 case ACPI_DP_TYPE_CHILD:
810 acpi_dp_free(dp->child);
811 break;
812 case ACPI_DP_TYPE_ARRAY:
813 acpi_dp_free(dp->array);
814 break;
815 default:
816 break;
817 }
818
819 free(dp);
820 dp = p;
821 }
Duncan Laurie559e9472016-05-10 13:18:17 -0700822}
823
Duncan Laurie84fac412020-06-03 12:36:51 -0700824static bool acpi_dp_write_properties(struct acpi_dp *prop, const char *uuid)
825{
826 struct acpi_dp *dp;
827 char *prop_count = NULL;
828
829 /* Print base properties */
830 for (dp = prop; dp; dp = dp->next) {
831 if (dp->type == ACPI_DP_TYPE_TABLE ||
832 dp->type == ACPI_DP_TYPE_CHILD ||
833 dp->type == ACPI_DP_TYPE_PACKAGE)
834 continue;
835
836 /*
837 * The UUID and package is only added when
838 * we come across the first property. This
839 * is to avoid creating a zero-length package
840 * in situations where there are only children.
841 */
842 if (!prop_count) {
843 /* ToUUID (dp->uuid) */
844 acpigen_write_uuid(uuid);
845 /*
846 * Package (PROP), element count determined as
847 * it is populated
848 */
849 prop_count = acpigen_write_package(0);
850 }
851 (*prop_count)++;
852 acpi_dp_write_property(dp);
853 }
854 if (prop_count) {
855 /* Package (PROP) length, if a package was written */
856 acpigen_pop_len();
857 return true;
858 }
859 return false;
860}
861
Simon Glass8bfa51e2020-06-29 16:18:37 -0600862static void acpi_dp_write_(struct acpi_dp *table)
Duncan Laurie559e9472016-05-10 13:18:17 -0700863{
Duncan Laurieffc99902016-07-02 19:56:06 -0700864 struct acpi_dp *dp, *prop;
Duncan Laurie84fac412020-06-03 12:36:51 -0700865 char *dp_count;
Duncan Laurieffc99902016-07-02 19:56:06 -0700866 int child_count = 0;
867
Duncan Lauriec1adeb62020-04-29 00:04:14 -0700868 if (!table || table->type != ACPI_DP_TYPE_TABLE || !table->next)
Duncan Laurieffc99902016-07-02 19:56:06 -0700869 return;
870
871 /* Name (name) */
872 acpigen_write_name(table->name);
873
874 /* Device Property list starts with the next entry */
875 prop = table->next;
876
Matt Delco08258882019-01-30 11:16:08 -0800877 /* Package (DP), default to assuming no properties or children */
878 dp_count = acpigen_write_package(0);
Duncan Laurieffc99902016-07-02 19:56:06 -0700879
880 /* Print base properties */
Duncan Laurie84fac412020-06-03 12:36:51 -0700881 if (acpi_dp_write_properties(prop, table->uuid))
882 *dp_count += 2;
Duncan Laurieffc99902016-07-02 19:56:06 -0700883
Duncan Laurie84fac412020-06-03 12:36:51 -0700884 /* Count child properties */
885 for (dp = prop; dp; dp = dp->next)
886 if (dp->type == ACPI_DP_TYPE_CHILD)
887 child_count++;
888
889 /* Add child properties to the base table */
Duncan Laurieffc99902016-07-02 19:56:06 -0700890 if (child_count) {
Duncan Laurie84fac412020-06-03 12:36:51 -0700891 /* Update DP package count */
Matt Delco08258882019-01-30 11:16:08 -0800892 *dp_count += 2;
Duncan Laurieffc99902016-07-02 19:56:06 -0700893 /* ToUUID (ACPI_DP_CHILD_UUID) */
894 acpigen_write_uuid(ACPI_DP_CHILD_UUID);
895
896 /* Print child pointer properties */
897 acpigen_write_package(child_count);
898
899 for (dp = prop; dp; dp = dp->next)
900 if (dp->type == ACPI_DP_TYPE_CHILD)
901 acpi_dp_write_property(dp);
Matt Delco08258882019-01-30 11:16:08 -0800902 /* Package (CHILD) length */
Duncan Laurieffc99902016-07-02 19:56:06 -0700903 acpigen_pop_len();
904 }
905
Duncan Laurie84fac412020-06-03 12:36:51 -0700906 /* Write packages of properties with unique UUID */
907 for (dp = prop; dp; dp = dp->next)
908 if (dp->type == ACPI_DP_TYPE_PACKAGE)
909 if (acpi_dp_write_properties(dp->child, dp->uuid))
910 *dp_count += 2;
911
Duncan Laurieffc99902016-07-02 19:56:06 -0700912 /* Package (DP) length */
913 acpigen_pop_len();
914
915 /* Recursively parse children into separate tables */
916 for (dp = prop; dp; dp = dp->next)
917 if (dp->type == ACPI_DP_TYPE_CHILD)
Simon Glass8bfa51e2020-06-29 16:18:37 -0600918 acpi_dp_write_(dp->child);
919}
920
921void acpi_dp_write(struct acpi_dp *table)
922{
923 acpi_dp_write_(table);
Duncan Laurieffc99902016-07-02 19:56:06 -0700924
925 /* Clean up */
926 acpi_dp_free(table);
927}
928
929static struct acpi_dp *acpi_dp_new(struct acpi_dp *dp, enum acpi_dp_type type,
930 const char *name)
931{
932 struct acpi_dp *new;
933
934 new = malloc(sizeof(struct acpi_dp));
935 if (!new)
936 return NULL;
937
938 memset(new, 0, sizeof(*new));
939 new->type = type;
940 new->name = name;
Duncan Laurie84fac412020-06-03 12:36:51 -0700941 new->uuid = ACPI_DP_UUID;
Duncan Laurieffc99902016-07-02 19:56:06 -0700942
943 if (dp) {
944 /* Add to end of property list */
945 while (dp->next)
946 dp = dp->next;
947 dp->next = new;
948 }
949
950 return new;
951}
952
953struct acpi_dp *acpi_dp_new_table(const char *name)
954{
955 return acpi_dp_new(NULL, ACPI_DP_TYPE_TABLE, name);
956}
957
Duncan Laurieb3023b62017-08-29 08:26:50 -0700958size_t acpi_dp_add_property_list(struct acpi_dp *dp,
959 const struct acpi_dp *property_list,
960 size_t property_count)
961{
962 const struct acpi_dp *prop;
963 size_t i, properties_added = 0;
964
Jacob Garberc30e5902019-05-23 14:34:58 -0600965 if (!dp || !property_list)
966 return 0;
967
Duncan Laurieb3023b62017-08-29 08:26:50 -0700968 for (i = 0; i < property_count; i++) {
969 prop = &property_list[i];
970
971 if (prop->type == ACPI_DP_TYPE_UNKNOWN || !prop->name)
972 continue;
973
974 switch (prop->type) {
975 case ACPI_DP_TYPE_INTEGER:
976 acpi_dp_add_integer(dp, prop->name, prop->integer);
977 break;
978 case ACPI_DP_TYPE_STRING:
979 acpi_dp_add_string(dp, prop->name, prop->string);
980 break;
981 case ACPI_DP_TYPE_REFERENCE:
982 acpi_dp_add_reference(dp, prop->name, prop->string);
983 break;
984 case ACPI_DP_TYPE_ARRAY:
985 acpi_dp_add_array(dp, prop->array);
986 break;
987 case ACPI_DP_TYPE_CHILD:
988 acpi_dp_add_child(dp, prop->name, prop->child);
989 break;
990 default:
991 continue;
992 }
993
994 ++properties_added;
995 }
996
997 return properties_added;
998}
999
Duncan Laurieffc99902016-07-02 19:56:06 -07001000struct acpi_dp *acpi_dp_add_integer(struct acpi_dp *dp, const char *name,
1001 uint64_t value)
1002{
Jacob Garberc30e5902019-05-23 14:34:58 -06001003 if (!dp)
1004 return NULL;
1005
Duncan Laurieffc99902016-07-02 19:56:06 -07001006 struct acpi_dp *new = acpi_dp_new(dp, ACPI_DP_TYPE_INTEGER, name);
1007
1008 if (new)
1009 new->integer = value;
1010
1011 return new;
1012}
1013
1014struct acpi_dp *acpi_dp_add_string(struct acpi_dp *dp, const char *name,
1015 const char *string)
1016{
Jacob Garberc30e5902019-05-23 14:34:58 -06001017 if (!dp)
1018 return NULL;
1019
Duncan Laurieffc99902016-07-02 19:56:06 -07001020 struct acpi_dp *new = acpi_dp_new(dp, ACPI_DP_TYPE_STRING, name);
1021
1022 if (new)
1023 new->string = string;
1024
1025 return new;
1026}
1027
1028struct acpi_dp *acpi_dp_add_reference(struct acpi_dp *dp, const char *name,
1029 const char *reference)
1030{
Jacob Garberc30e5902019-05-23 14:34:58 -06001031 if (!dp)
1032 return NULL;
1033
Duncan Laurieffc99902016-07-02 19:56:06 -07001034 struct acpi_dp *new = acpi_dp_new(dp, ACPI_DP_TYPE_REFERENCE, name);
1035
1036 if (new)
1037 new->string = reference;
1038
1039 return new;
1040}
1041
1042struct acpi_dp *acpi_dp_add_child(struct acpi_dp *dp, const char *name,
1043 struct acpi_dp *child)
1044{
1045 struct acpi_dp *new;
1046
Jacob Garberc30e5902019-05-23 14:34:58 -06001047 if (!dp || !child || child->type != ACPI_DP_TYPE_TABLE)
Duncan Laurieffc99902016-07-02 19:56:06 -07001048 return NULL;
1049
1050 new = acpi_dp_new(dp, ACPI_DP_TYPE_CHILD, name);
1051 if (new) {
1052 new->child = child;
1053 new->string = child->name;
1054 }
1055
1056 return new;
1057}
1058
Duncan Laurie84fac412020-06-03 12:36:51 -07001059struct acpi_dp *acpi_dp_add_package(struct acpi_dp *dp, struct acpi_dp *package)
1060{
1061 struct acpi_dp *new;
1062
1063 if (!dp || !package || package->type != ACPI_DP_TYPE_TABLE)
1064 return NULL;
1065
1066 new = acpi_dp_new(dp, ACPI_DP_TYPE_PACKAGE, NULL);
1067 if (new) {
1068 new->uuid = package->name;
1069 new->child = package;
1070 }
1071
1072 return new;
1073}
1074
Duncan Laurieffc99902016-07-02 19:56:06 -07001075struct acpi_dp *acpi_dp_add_array(struct acpi_dp *dp, struct acpi_dp *array)
1076{
1077 struct acpi_dp *new;
1078
Jacob Garberc30e5902019-05-23 14:34:58 -06001079 if (!dp || !array || array->type != ACPI_DP_TYPE_TABLE)
Duncan Laurieffc99902016-07-02 19:56:06 -07001080 return NULL;
1081
1082 new = acpi_dp_new(dp, ACPI_DP_TYPE_ARRAY, array->name);
1083 if (new)
1084 new->array = array;
1085
1086 return new;
1087}
1088
1089struct acpi_dp *acpi_dp_add_integer_array(struct acpi_dp *dp, const char *name,
Duncan Laurieed6eb272020-04-29 11:39:08 -07001090 const uint64_t *array, int len)
Duncan Laurieffc99902016-07-02 19:56:06 -07001091{
1092 struct acpi_dp *dp_array;
1093 int i;
1094
Jacob Garberc30e5902019-05-23 14:34:58 -06001095 if (!dp || len <= 0)
Duncan Laurieffc99902016-07-02 19:56:06 -07001096 return NULL;
1097
1098 dp_array = acpi_dp_new_table(name);
1099 if (!dp_array)
1100 return NULL;
1101
1102 for (i = 0; i < len; i++)
1103 if (!acpi_dp_add_integer(dp_array, NULL, array[i]))
1104 break;
1105
1106 acpi_dp_add_array(dp, dp_array);
1107
1108 return dp_array;
1109}
1110
Karthikeyan Ramasubramanian685dbe12020-10-05 10:44:50 -06001111struct acpi_dp *acpi_dp_add_gpio_array(struct acpi_dp *dp, const char *name,
1112 const struct acpi_gpio_res_params *params,
1113 size_t param_count)
1114{
1115 struct acpi_dp *gpio;
1116 uint32_t i;
1117
1118 if (!dp || !param_count)
1119 return NULL;
1120
1121 gpio = acpi_dp_new_table(name);
1122 if (!gpio)
1123 return NULL;
1124
1125 /*
1126 * Generate ACPI identifiers as follows:
1127 * Package () {
1128 * name, // e.g. cs-gpios
1129 * Package() {
1130 * ref, index, pin, active_low, // GPIO-0 (params[0])
1131 * ref, index, pin, active_low, // GPIO-1 (params[1])
1132 * ...
1133 * }
1134 * }
1135 */
1136 for (i = 0; i < param_count; i++, params++) {
1137 /*
1138 * If refs is NULL, leave a hole in the gpio array. This can be used in
1139 * conditions where some controllers use both GPIOs and native signals.
1140 */
1141 if (!params->ref) {
1142 acpi_dp_add_integer(gpio, NULL, 0);
1143 continue;
1144 }
1145
1146 /* The device that has _CRS containing GpioIO()/GpioInt() */
1147 acpi_dp_add_reference(gpio, NULL, params->ref);
1148
1149 /* Index of the GPIO resource in _CRS starting from zero */
1150 acpi_dp_add_integer(gpio, NULL, params->index);
1151
1152 /* Pin in the GPIO resource, typically zero */
1153 acpi_dp_add_integer(gpio, NULL, params->pin);
1154
1155 /* Set if pin is active low */
1156 acpi_dp_add_integer(gpio, NULL, params->active_low);
1157 }
1158 acpi_dp_add_array(dp, gpio);
1159
1160 return gpio;
1161
1162}
1163
1164
Duncan Laurieffc99902016-07-02 19:56:06 -07001165struct acpi_dp *acpi_dp_add_gpio(struct acpi_dp *dp, const char *name,
1166 const char *ref, int index, int pin,
1167 int active_low)
1168{
Karthikeyan Ramasubramanian685dbe12020-10-05 10:44:50 -06001169 struct acpi_gpio_res_params param = {
1170 .ref = ref,
1171 .index = index,
1172 .pin = pin,
1173 .active_low = active_low,
1174 };
Jacob Garberc30e5902019-05-23 14:34:58 -06001175
Karthikeyan Ramasubramanian685dbe12020-10-05 10:44:50 -06001176 return acpi_dp_add_gpio_array(dp, name, &param, 1);
Duncan Laurie559e9472016-05-10 13:18:17 -07001177}
Furquan Shaikhd1130af2020-04-23 12:51:42 -07001178
1179/*
1180 * This function writes a PCI device with _ADR object:
1181 * Example:
1182 * Scope (\_SB.PCI0)
1183 * {
1184 * Device (IGFX)
1185 * {
1186 * Name (_ADR, 0x0000000000000000)
1187 * Method (_STA, 0, NotSerialized) { Return (status) }
1188 * }
1189 * }
1190 */
Furquan Shaikh7536a392020-04-24 21:59:21 -07001191void acpi_device_write_pci_dev(const struct device *dev)
Furquan Shaikhd1130af2020-04-23 12:51:42 -07001192{
1193 const char *scope = acpi_device_scope(dev);
1194 const char *name = acpi_device_name(dev);
1195
1196 assert(dev->path.type == DEVICE_PATH_PCI);
1197 assert(name);
1198 assert(scope);
1199
1200 acpigen_write_scope(scope);
1201 acpigen_write_device(name);
1202
1203 acpigen_write_ADR_pci_device(dev);
1204 acpigen_write_STA(acpi_device_status(dev));
1205
1206 acpigen_pop_len(); /* Device */
1207 acpigen_pop_len(); /* Scope */
1208}
Kapil Porwalddc52a62022-11-26 19:10:57 +05301209
Kapil Porwal75436272022-11-28 17:25:48 +05301210/*
1211 * Helper function to add given integer property with an UUID to _DSD in the current scope.
1212 *
1213 * dsd - Pointer to a _DSD object.
1214 * Append to existing _DSD object if not NULL.
1215 * Create new _DSD object and flush it if NULL.
1216 * uuid - Pointer to the UUID string.
1217 * name - Pointer to the property name string.
1218 * value - Value of the integer property.
1219 */
1220static void acpi_device_add_integer_property_with_uuid(struct acpi_dp *dsd,
1221 const char *uuid,
1222 const char *name,
1223 uint64_t value)
Kapil Porwalddc52a62022-11-26 19:10:57 +05301224{
1225 struct acpi_dp *prev_dsd = dsd, *pkg;
1226 if (prev_dsd == NULL)
1227 dsd = acpi_dp_new_table("_DSD");
Kapil Porwal75436272022-11-28 17:25:48 +05301228 pkg = acpi_dp_new_table(uuid);
1229 acpi_dp_add_integer(pkg, name, value);
Kapil Porwalddc52a62022-11-26 19:10:57 +05301230 acpi_dp_add_package(dsd, pkg);
1231 if (prev_dsd == NULL)
1232 acpi_dp_write(dsd);
1233}
Kapil Porwal75436272022-11-28 17:25:48 +05301234
1235/* _DSD with ExternalFacingPort */
1236void acpi_device_add_external_facing_port(struct acpi_dp *dsd)
1237{
1238 acpi_device_add_integer_property_with_uuid(dsd,
1239 ACPI_DSD_EXTERNAL_FACING_PORT_UUID,
1240 ACPI_DSD_EXTERNAL_FACING_PORT_NAME,
1241 1);
1242}
1243
1244/* _DSD with HotPlugSupportInD3 */
1245void acpi_device_add_hotplug_support_in_d3(struct acpi_dp *dsd)
1246{
1247 acpi_device_add_integer_property_with_uuid(dsd,
1248 ACPI_DSD_HOTPLUG_IN_D3_UUID,
1249 ACPI_DSD_HOTPLUG_IN_D3_NAME,
1250 1);
1251}
1252
1253/* _DSD with DmaProperty */
1254void acpi_device_add_dma_property(struct acpi_dp *dsd)
1255{
1256 acpi_device_add_integer_property_with_uuid(dsd,
1257 ACPI_DSD_DMA_PROPERTY_UUID,
1258 ACPI_DSD_DMA_PROPERTY_NAME,
1259 1);
1260}
1261
1262/* _DSD with StorageD3Enable */
1263void acpi_device_add_storage_d3_enable(struct acpi_dp *dsd)
1264{
1265 acpi_device_add_integer_property_with_uuid(dsd,
1266 ACPI_DSD_STORAGE_D3_UUID,
1267 ACPI_DSD_STORAGE_D3_NAME,
1268 1);
1269}