blob: b76357e6757b5eea0dc1fb0e0b34cfafb5798a38 [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 */
Duncan Laurie84fac412020-06-03 12:36:51 -070079 if (dev->ops && dev->ops->acpi_name)
Duncan Lauried9af3ce2016-05-08 18:15:25 -070080 return dev->ops->acpi_name(dev);
81
Duncan Laurie47029142018-05-07 14:28:53 -070082 /* Walk up the tree to find if any parent can identify this device */
83 while (pdev->bus) {
84 pdev = pdev->bus->dev;
85 if (!pdev)
86 break;
87 if (pdev->path.type == DEVICE_PATH_ROOT)
88 break;
89 if (pdev->ops && pdev->ops->acpi_name)
90 name = pdev->ops->acpi_name(dev);
91 if (name)
92 return name;
93 }
Duncan Lauried9af3ce2016-05-08 18:15:25 -070094
95 return NULL;
96}
97
Patrick Rudolpheeb8e742019-08-20 08:20:01 +020098/* Locate and return the ACPI _HID (Hardware ID) for this device */
99const char *acpi_device_hid(const struct device *dev)
100{
101 if (!dev)
102 return NULL;
103
104 /* Check for device specific handler */
105 if (dev->ops->acpi_hid)
106 return dev->ops->acpi_hid(dev);
107
108 /*
109 * Don't walk up the tree to find any parent that can identify this device, as
110 * PNP devices are hard to identify.
111 */
112
113 return NULL;
114}
115
Patrick Rudolphc83bab62019-12-13 12:16:06 +0100116/*
117 * Generate unique ID based on the ACPI path.
118 * Collisions on the same _HID are possible but very unlikely.
119 */
Furquan Shaikhd14d03a2020-04-24 21:27:29 -0700120uint32_t acpi_device_uid(const struct device *dev)
Patrick Rudolphc83bab62019-12-13 12:16:06 +0100121{
122 const char *path = acpi_device_path(dev);
123 if (!path)
124 return 0;
125
126 return CRC(path, strlen(path), crc32_byte);
127}
128
Duncan Lauried9af3ce2016-05-08 18:15:25 -0700129/* Recursive function to find the root device and print a path from there */
Aamir Bohrae825d3f2019-07-30 10:11:41 +0530130static ssize_t acpi_device_path_fill(const struct device *dev, char *buf,
Duncan Laurie47029142018-05-07 14:28:53 -0700131 size_t buf_len, size_t cur)
Duncan Lauried9af3ce2016-05-08 18:15:25 -0700132{
133 const char *name = acpi_device_name(dev);
Duncan Laurie47029142018-05-07 14:28:53 -0700134 ssize_t next = 0;
135
136 if (!name)
137 return -1;
Duncan Lauried9af3ce2016-05-08 18:15:25 -0700138
139 /*
140 * Make sure this name segment will fit, including the path segment
141 * separator and possible NUL terminator if this is the last segment.
142 */
Duncan Laurie47029142018-05-07 14:28:53 -0700143 if (!dev || (cur + strlen(name) + 2) > buf_len)
Duncan Lauried9af3ce2016-05-08 18:15:25 -0700144 return cur;
145
146 /* Walk up the tree to the root device */
147 if (dev->path.type != DEVICE_PATH_ROOT && dev->bus && dev->bus->dev)
148 next = acpi_device_path_fill(dev->bus->dev, buf, buf_len, cur);
Duncan Laurie47029142018-05-07 14:28:53 -0700149 if (next < 0)
150 return next;
Duncan Lauried9af3ce2016-05-08 18:15:25 -0700151
152 /* Fill in the path from the root device */
153 next += snprintf(buf + next, buf_len - next, "%s%s",
Timothy Pearsonaeb61012017-04-13 17:06:16 -0500154 (dev->path.type == DEVICE_PATH_ROOT
155 || (strlen(name) == 0)) ?
156 "" : ".", name);
Duncan Lauried9af3ce2016-05-08 18:15:25 -0700157
158 return next;
159}
160
161/*
162 * Warning: just as with dev_path() this uses a static buffer
Martin Roth0949e732021-10-01 14:28:22 -0600163 * so should not be called multiple times in one statement
Duncan Lauried9af3ce2016-05-08 18:15:25 -0700164 */
Aamir Bohrae825d3f2019-07-30 10:11:41 +0530165const char *acpi_device_path(const struct device *dev)
Duncan Lauried9af3ce2016-05-08 18:15:25 -0700166{
167 static char buf[DEVICE_PATH_MAX] = {};
168
169 if (!dev)
170 return NULL;
171
172 if (acpi_device_path_fill(dev, buf, sizeof(buf), 0) <= 0)
173 return NULL;
174
175 return buf;
176}
177
178/* Return the path of the parent device as the ACPI Scope for this device */
Aamir Bohrae825d3f2019-07-30 10:11:41 +0530179const char *acpi_device_scope(const struct device *dev)
Duncan Lauried9af3ce2016-05-08 18:15:25 -0700180{
Duncan Lauried02685b2016-06-30 14:37:37 -0700181 static char buf[DEVICE_PATH_MAX] = {};
182
Duncan Lauried9af3ce2016-05-08 18:15:25 -0700183 if (!dev || !dev->bus || !dev->bus->dev)
184 return NULL;
185
Duncan Lauried02685b2016-06-30 14:37:37 -0700186 if (acpi_device_path_fill(dev->bus->dev, buf, sizeof(buf), 0) <= 0)
187 return NULL;
188
189 return buf;
Duncan Lauried9af3ce2016-05-08 18:15:25 -0700190}
191
Elyes HAOUAS6716bab2020-01-09 21:29:25 +0100192/* Concatenate the device path and provided name suffix */
Aamir Bohrae825d3f2019-07-30 10:11:41 +0530193const char *acpi_device_path_join(const struct device *dev, const char *name)
Duncan Lauried9af3ce2016-05-08 18:15:25 -0700194{
195 static char buf[DEVICE_PATH_MAX] = {};
Jacob Garberf2ba2d92019-07-02 10:50:10 -0600196 ssize_t len;
Duncan Lauried9af3ce2016-05-08 18:15:25 -0700197
198 if (!dev)
199 return NULL;
200
201 /* Build the path of this device */
202 len = acpi_device_path_fill(dev, buf, sizeof(buf), 0);
203 if (len <= 0)
204 return NULL;
205
206 /* Ensure there is room for the added name, separator, and NUL */
207 if ((len + strlen(name) + 2) > sizeof(buf))
208 return NULL;
209 snprintf(buf + len, sizeof(buf) - len, ".%s", name);
210
211 return buf;
212}
Duncan Laurie6b7c1f62016-05-09 15:38:36 -0700213
Hung-Te Linb4be50c2018-09-10 10:55:49 +0800214int acpi_device_status(const struct device *dev)
215{
216 if (!dev->enabled)
217 return ACPI_STATUS_DEVICE_ALL_OFF;
218 if (dev->hidden)
219 return ACPI_STATUS_DEVICE_HIDDEN_ON;
220 return ACPI_STATUS_DEVICE_ALL_ON;
221}
222
Patrick Rudolphc83bab62019-12-13 12:16:06 +0100223/* Write the unique _UID based on ACPI device path. */
Furquan Shaikhd14d03a2020-04-24 21:27:29 -0700224void acpi_device_write_uid(const struct device *dev)
Patrick Rudolphc83bab62019-12-13 12:16:06 +0100225{
226 acpigen_write_name_integer("_UID", acpi_device_uid(dev));
227}
228
Duncan Laurie6b7c1f62016-05-09 15:38:36 -0700229/* ACPI 6.1 section 6.4.3.6: Extended Interrupt Descriptor */
230void acpi_device_write_interrupt(const struct acpi_irq *irq)
231{
232 void *desc_length;
233 uint8_t flags;
234
235 if (!irq || !irq->pin)
236 return;
237
238 /* This is supported by GpioInt() but not Interrupt() */
Furquan Shaikh5b9b5932017-02-21 13:16:30 -0800239 if (irq->polarity == ACPI_IRQ_ACTIVE_BOTH)
Duncan Laurie6b7c1f62016-05-09 15:38:36 -0700240 return;
241
242 /* Byte 0: Descriptor Type */
243 acpigen_emit_byte(ACPI_DESCRIPTOR_INTERRUPT);
244
245 /* Byte 1-2: Length (filled in later) */
246 desc_length = acpi_device_write_zero_len();
247
248 /*
249 * Byte 3: Flags
250 * [7:5]: Reserved
251 * [4]: Wake (0=NO_WAKE 1=WAKE)
252 * [3]: Sharing (0=EXCLUSIVE 1=SHARED)
253 * [2]: Polarity (0=HIGH 1=LOW)
254 * [1]: Mode (0=LEVEL 1=EDGE)
255 * [0]: Resource (0=PRODUCER 1=CONSUMER)
256 */
257 flags = 1 << 0; /* ResourceConsumer */
Furquan Shaikh5b9b5932017-02-21 13:16:30 -0800258 if (irq->mode == ACPI_IRQ_EDGE_TRIGGERED)
Duncan Laurie6b7c1f62016-05-09 15:38:36 -0700259 flags |= 1 << 1;
Furquan Shaikh5b9b5932017-02-21 13:16:30 -0800260 if (irq->polarity == ACPI_IRQ_ACTIVE_LOW)
Duncan Laurie6b7c1f62016-05-09 15:38:36 -0700261 flags |= 1 << 2;
Furquan Shaikh5b9b5932017-02-21 13:16:30 -0800262 if (irq->shared == ACPI_IRQ_SHARED)
Duncan Laurie6b7c1f62016-05-09 15:38:36 -0700263 flags |= 1 << 3;
Furquan Shaikh5b9b5932017-02-21 13:16:30 -0800264 if (irq->wake == ACPI_IRQ_WAKE)
Duncan Laurie6b7c1f62016-05-09 15:38:36 -0700265 flags |= 1 << 4;
266 acpigen_emit_byte(flags);
267
268 /* Byte 4: Interrupt Table Entry Count */
269 acpigen_emit_byte(1);
270
271 /* Byte 5-8: Interrupt Number */
272 acpigen_emit_dword(irq->pin);
273
274 /* Fill in Descriptor Length (account for len word) */
275 acpi_device_fill_len(desc_length);
276}
Duncan Lauriecfb6ea72016-05-09 17:08:38 -0700277
278/* ACPI 6.1 section 6.4.3.8.1 - GPIO Interrupt or I/O */
279void acpi_device_write_gpio(const struct acpi_gpio *gpio)
280{
281 void *start, *desc_length;
282 void *pin_table_offset, *vendor_data_offset, *resource_offset;
283 uint16_t flags = 0;
284 int pin;
285
286 if (!gpio || gpio->type > ACPI_GPIO_TYPE_IO)
287 return;
288
289 start = acpigen_get_current();
290
291 /* Byte 0: Descriptor Type */
292 acpigen_emit_byte(ACPI_DESCRIPTOR_GPIO);
293
294 /* Byte 1-2: Length (fill in later) */
295 desc_length = acpi_device_write_zero_len();
296
297 /* Byte 3: Revision ID */
298 acpigen_emit_byte(ACPI_GPIO_REVISION_ID);
299
300 /* Byte 4: GpioIo or GpioInt */
301 acpigen_emit_byte(gpio->type);
302
303 /*
304 * Byte 5-6: General Flags
305 * [15:1]: 0 => Reserved
306 * [0]: 1 => ResourceConsumer
307 */
308 acpigen_emit_word(1 << 0);
309
310 switch (gpio->type) {
311 case ACPI_GPIO_TYPE_INTERRUPT:
312 /*
313 * Byte 7-8: GPIO Interrupt Flags
314 * [15:5]: 0 => Reserved
315 * [4]: Wake (0=NO_WAKE 1=WAKE)
316 * [3]: Sharing (0=EXCLUSIVE 1=SHARED)
317 * [2:1]: Polarity (0=HIGH 1=LOW 2=BOTH)
318 * [0]: Mode (0=LEVEL 1=EDGE)
319 */
Furquan Shaikh5b9b5932017-02-21 13:16:30 -0800320 if (gpio->irq.mode == ACPI_IRQ_EDGE_TRIGGERED)
Duncan Lauriecfb6ea72016-05-09 17:08:38 -0700321 flags |= 1 << 0;
Furquan Shaikh5b9b5932017-02-21 13:16:30 -0800322 if (gpio->irq.shared == ACPI_IRQ_SHARED)
Duncan Lauriecfb6ea72016-05-09 17:08:38 -0700323 flags |= 1 << 3;
Furquan Shaikh5b9b5932017-02-21 13:16:30 -0800324 if (gpio->irq.wake == ACPI_IRQ_WAKE)
Duncan Lauriecfb6ea72016-05-09 17:08:38 -0700325 flags |= 1 << 4;
326
327 switch (gpio->irq.polarity) {
Furquan Shaikh5b9b5932017-02-21 13:16:30 -0800328 case ACPI_IRQ_ACTIVE_HIGH:
Duncan Lauriecfb6ea72016-05-09 17:08:38 -0700329 flags |= 0 << 1;
330 break;
Furquan Shaikh5b9b5932017-02-21 13:16:30 -0800331 case ACPI_IRQ_ACTIVE_LOW:
Duncan Lauriecfb6ea72016-05-09 17:08:38 -0700332 flags |= 1 << 1;
333 break;
Furquan Shaikh5b9b5932017-02-21 13:16:30 -0800334 case ACPI_IRQ_ACTIVE_BOTH:
Duncan Lauriecfb6ea72016-05-09 17:08:38 -0700335 flags |= 2 << 1;
336 break;
337 }
338 break;
339
340 case ACPI_GPIO_TYPE_IO:
341 /*
342 * Byte 7-8: GPIO IO Flags
343 * [15:4]: 0 => Reserved
344 * [3]: Sharing (0=EXCLUSIVE 1=SHARED)
345 * [2]: 0 => Reserved
346 * [1:0]: IO Restriction
347 * 0 => IoRestrictionNone
348 * 1 => IoRestrictionInputOnly
349 * 2 => IoRestrictionOutputOnly
350 * 3 => IoRestrictionNoneAndPreserve
351 */
352 flags |= gpio->io_restrict & 3;
353 if (gpio->io_shared)
354 flags |= 1 << 3;
355 break;
356 }
357 acpigen_emit_word(flags);
358
359 /*
360 * Byte 9: Pin Configuration
361 * 0x01 => Default (no configuration applied)
362 * 0x02 => Pull-up
363 * 0x03 => Pull-down
364 * 0x04-0x7F => Reserved
365 * 0x80-0xff => Vendor defined
366 */
367 acpigen_emit_byte(gpio->pull);
368
369 /* Byte 10-11: Output Drive Strength in 1/100 mA */
370 acpigen_emit_word(gpio->output_drive_strength);
371
372 /* Byte 12-13: Debounce Timeout in 1/100 ms */
373 acpigen_emit_word(gpio->interrupt_debounce_timeout);
374
375 /* Byte 14-15: Pin Table Offset, relative to start */
376 pin_table_offset = acpi_device_write_zero_len();
377
378 /* Byte 16: Reserved */
379 acpigen_emit_byte(0);
380
381 /* Byte 17-18: Resource Source Name Offset, relative to start */
382 resource_offset = acpi_device_write_zero_len();
383
384 /* Byte 19-20: Vendor Data Offset, relative to start */
385 vendor_data_offset = acpi_device_write_zero_len();
386
387 /* Byte 21-22: Vendor Data Length */
388 acpigen_emit_word(0);
389
390 /* Fill in Pin Table Offset */
391 acpi_device_fill_from_len(pin_table_offset, start);
392
393 /* Pin Table, one word for each pin */
Duncan Laurie5b6c28c2016-06-29 10:47:22 -0700394 for (pin = 0; pin < gpio->pin_count; pin++) {
395 uint16_t acpi_pin = gpio->pins[pin];
Julius Wernercd49cce2019-03-05 16:53:33 -0800396#if CONFIG(GENERIC_GPIO_LIB)
Duncan Laurie5b6c28c2016-06-29 10:47:22 -0700397 acpi_pin = gpio_acpi_pin(acpi_pin);
398#endif
399 acpigen_emit_word(acpi_pin);
400 }
Duncan Lauriecfb6ea72016-05-09 17:08:38 -0700401
402 /* Fill in Resource Source Name Offset */
403 acpi_device_fill_from_len(resource_offset, start);
404
405 /* Resource Source Name String */
Julius Wernercd49cce2019-03-05 16:53:33 -0800406#if CONFIG(GENERIC_GPIO_LIB)
Duncan Lauriecfb6ea72016-05-09 17:08:38 -0700407 acpigen_emit_string(gpio->resource ? : gpio_acpi_path(gpio->pins[0]));
408#else
409 acpigen_emit_string(gpio->resource);
410#endif
411
412 /* Fill in Vendor Data Offset */
413 acpi_device_fill_from_len(vendor_data_offset, start);
414
415 /* Fill in GPIO Descriptor Length (account for len word) */
416 acpi_device_fill_len(desc_length);
417}
Duncan Laurie1010b4a2016-05-09 20:10:47 -0700418
419/* ACPI 6.1 section 6.4.3.8.2.1 - I2cSerialBus() */
420void acpi_device_write_i2c(const struct acpi_i2c *i2c)
421{
422 void *desc_length, *type_length;
423
424 /* Byte 0: Descriptor Type */
425 acpigen_emit_byte(ACPI_DESCRIPTOR_SERIAL_BUS);
426
427 /* Byte 1+2: Length (filled in later) */
428 desc_length = acpi_device_write_zero_len();
429
430 /* Byte 3: Revision ID */
Elyes HAOUAS34564ed2019-04-16 08:12:10 +0200431 acpigen_emit_byte(ACPI_I2C_SERIAL_BUS_REVISION_ID);
Duncan Laurie1010b4a2016-05-09 20:10:47 -0700432
433 /* Byte 4: Resource Source Index is Reserved */
434 acpigen_emit_byte(0);
435
436 /* Byte 5: Serial Bus Type is I2C */
437 acpigen_emit_byte(ACPI_SERIAL_BUS_TYPE_I2C);
438
439 /*
440 * Byte 6: Flags
441 * [7:2]: 0 => Reserved
442 * [1]: 1 => ResourceConsumer
443 * [0]: 0 => ControllerInitiated
444 */
445 acpigen_emit_byte(1 << 1);
446
447 /*
448 * Byte 7-8: Type Specific Flags
449 * [15:1]: 0 => Reserved
450 * [0]: 0 => 7bit, 1 => 10bit
451 */
452 acpigen_emit_word(i2c->mode_10bit);
453
454 /* Byte 9: Type Specific Revision ID */
Elyes HAOUAS34564ed2019-04-16 08:12:10 +0200455 acpigen_emit_byte(ACPI_I2C_TYPE_SPECIFIC_REVISION_ID);
Duncan Laurie1010b4a2016-05-09 20:10:47 -0700456
457 /* Byte 10-11: I2C Type Data Length */
458 type_length = acpi_device_write_zero_len();
459
460 /* Byte 12-15: I2C Bus Speed */
461 acpigen_emit_dword(i2c->speed);
462
463 /* Byte 16-17: I2C Slave Address */
464 acpigen_emit_word(i2c->address);
465
466 /* Fill in Type Data Length */
467 acpi_device_fill_len(type_length);
468
469 /* Byte 18+: ResourceSource */
470 acpigen_emit_string(i2c->resource);
471
472 /* Fill in I2C Descriptor Length */
473 acpi_device_fill_len(desc_length);
474}
Duncan Laurie70c86d92016-05-10 07:26:34 -0700475
476/* ACPI 6.1 section 6.4.3.8.2.2 - SpiSerialBus() */
477void acpi_device_write_spi(const struct acpi_spi *spi)
478{
479 void *desc_length, *type_length;
480 uint16_t flags = 0;
481
482 /* Byte 0: Descriptor Type */
483 acpigen_emit_byte(ACPI_DESCRIPTOR_SERIAL_BUS);
484
485 /* Byte 1+2: Length (filled in later) */
486 desc_length = acpi_device_write_zero_len();
487
488 /* Byte 3: Revision ID */
Elyes HAOUAS34564ed2019-04-16 08:12:10 +0200489 acpigen_emit_byte(ACPI_SPI_SERIAL_BUS_REVISION_ID);
Duncan Laurie70c86d92016-05-10 07:26:34 -0700490
491 /* Byte 4: Resource Source Index is Reserved */
492 acpigen_emit_byte(0);
493
494 /* Byte 5: Serial Bus Type is SPI */
495 acpigen_emit_byte(ACPI_SERIAL_BUS_TYPE_SPI);
496
497 /*
498 * Byte 6: Flags
499 * [7:2]: 0 => Reserved
500 * [1]: 1 => ResourceConsumer
501 * [0]: 0 => ControllerInitiated
502 */
503 acpigen_emit_byte(1 << 1);
504
505 /*
506 * Byte 7-8: Type Specific Flags
507 * [15:2]: 0 => Reserved
508 * [1]: 0 => ActiveLow, 1 => ActiveHigh
509 * [0]: 0 => FourWire, 1 => ThreeWire
510 */
511 if (spi->wire_mode == SPI_3_WIRE_MODE)
512 flags |= 1 << 0;
513 if (spi->device_select_polarity == SPI_POLARITY_HIGH)
514 flags |= 1 << 1;
515 acpigen_emit_word(flags);
516
517 /* Byte 9: Type Specific Revision ID */
Elyes HAOUAS34564ed2019-04-16 08:12:10 +0200518 acpigen_emit_byte(ACPI_SPI_TYPE_SPECIFIC_REVISION_ID);
Duncan Laurie70c86d92016-05-10 07:26:34 -0700519
520 /* Byte 10-11: SPI Type Data Length */
521 type_length = acpi_device_write_zero_len();
522
523 /* Byte 12-15: Connection Speed */
524 acpigen_emit_dword(spi->speed);
525
526 /* Byte 16: Data Bit Length */
527 acpigen_emit_byte(spi->data_bit_length);
528
529 /* Byte 17: Clock Phase */
530 acpigen_emit_byte(spi->clock_phase);
531
532 /* Byte 18: Clock Polarity */
533 acpigen_emit_byte(spi->clock_polarity);
534
535 /* Byte 19-20: Device Selection */
536 acpigen_emit_word(spi->device_select);
537
538 /* Fill in Type Data Length */
539 acpi_device_fill_len(type_length);
540
541 /* Byte 21+: ResourceSource String */
542 acpigen_emit_string(spi->resource);
543
544 /* Fill in SPI Descriptor Length */
545 acpi_device_fill_len(desc_length);
546}
Duncan Laurie559e9472016-05-10 13:18:17 -0700547
Duncan Lauriedccef0d2020-05-27 12:29:51 -0700548/* UART Serial Bus - UARTSerialBusV2() */
549void acpi_device_write_uart(const struct acpi_uart *uart)
550{
551 void *desc_length, *type_length;
552 uint16_t flags;
553
554 /* Byte 0: Descriptor Type */
555 acpigen_emit_byte(ACPI_DESCRIPTOR_SERIAL_BUS);
556
557 /* Byte 1+2: Length (filled in later) */
558 desc_length = acpi_device_write_zero_len();
559
560 /* Byte 3: Revision ID */
561 acpigen_emit_byte(ACPI_UART_SERIAL_BUS_REVISION_ID);
562
563 /* Byte 4: Resource Source Index is Reserved */
564 acpigen_emit_byte(0);
565
566 /* Byte 5: Serial Bus Type is UART */
567 acpigen_emit_byte(ACPI_SERIAL_BUS_TYPE_UART);
568
569 /*
570 * Byte 6: Flags
571 * [7:2]: 0 => Reserved
572 * [1]: 1 => ResourceConsumer
573 * [0]: 0 => ControllerInitiated
574 */
575 acpigen_emit_byte(BIT(1));
576
577 /*
578 * Byte 7-8: Type Specific Flags
579 * [15:8]: 0 => Reserved
580 * [7]: 0 => Little Endian, 1 => Big Endian
581 * [6:4]: Data bits
582 * [3:2]: Stop bits
583 * [1:0]: Flow control
584 */
585 flags = uart->flow_control & 3;
586 flags |= (uart->stop_bits & 3) << 2;
587 flags |= (uart->data_bits & 7) << 4;
588 flags |= (uart->endian & 1) << 7;
589 acpigen_emit_word(flags);
590
591 /* Byte 9: Type Specific Revision ID */
592 acpigen_emit_byte(ACPI_UART_TYPE_SPECIFIC_REVISION_ID);
593
594 /* Byte 10-11: Type Data Length */
595 type_length = acpi_device_write_zero_len();
596
597 /* Byte 12-15: Initial Baud Rate */
598 acpigen_emit_dword(uart->initial_baud_rate);
599
600 /* Byte 16-17: RX FIFO size */
601 acpigen_emit_word(uart->rx_fifo_bytes);
602
603 /* Byte 18-19: TX FIFO size */
604 acpigen_emit_word(uart->tx_fifo_bytes);
605
606 /* Byte 20: Parity */
607 acpigen_emit_byte(uart->parity);
608
609 /* Byte 21: Lines Enabled */
610 acpigen_emit_byte(uart->lines_in_use);
611
612 /* Fill in Type Data Length */
613 acpi_device_fill_len(type_length);
614
615 /* Byte 22+: ResourceSource */
616 acpigen_emit_string(uart->resource);
617
618 /* Fill in Descriptor Length */
619 acpi_device_fill_len(desc_length);
620}
621
Raul E Rangel6d2bc002021-05-27 15:04:21 -0600622#define ACPI_POWER_RESOURCE_STATUS_ON_OP ONE_OP
623#define ACPI_POWER_RESOURCE_STATUS_OFF_OP ZERO_OP
624
625/**
626 * Writes an ACPI fragment that will check the GPIO and return 0 if the GPIO
627 * state does not match the active parameter.
628 */
629static void acpigen_write_gpio_STA(const struct acpi_gpio *gpio, bool active)
630{
631 if (!gpio || !gpio->pin_count)
632 return;
633
634 /* Read current GPIO status into Local0. */
635 acpigen_get_tx_gpio(gpio);
636
637 /*
638 * If (!Local0)
639 * {
640 * Return (Zero)
641 * }
642 */
643 acpigen_write_if();
644 if (active)
645 acpigen_emit_byte(LNOT_OP);
646 acpigen_emit_byte(LOCAL0_OP);
647 acpigen_write_return_op(ACPI_POWER_RESOURCE_STATUS_OFF_OP);
648 acpigen_write_if_end();
649}
650
651static void acpigen_write_power_res_STA(const struct acpi_power_res_params *params)
652{
653 acpigen_write_method_serialized("_STA", 0);
654
655 /* Verify all the GPIOs are in the ON state, otherwise return 0 */
656 acpigen_write_gpio_STA(params->enable_gpio, true);
657 acpigen_write_gpio_STA(params->reset_gpio, false);
658 acpigen_write_gpio_STA(params->stop_gpio, false);
659
660 /* All GPIOs are in the ON state */
661 acpigen_write_return_op(ACPI_POWER_RESOURCE_STATUS_ON_OP);
662
663 acpigen_pop_len(); /* Method */
664}
665
Duncan Lauriebd73dbb2017-02-17 17:05:03 -0800666/* PowerResource() with Enable and/or Reset control */
Shelley Chena0603392018-04-26 13:52:30 -0700667void acpi_device_add_power_res(const struct acpi_power_res_params *params)
Duncan Lauriebd73dbb2017-02-17 17:05:03 -0800668{
Furquan Shaikhd47946e2021-09-14 15:54:41 -0700669 static uint8_t id;
Furquan Shaikhdc782752020-04-30 22:49:39 -0700670 static const char * const power_res_dev_states[] = { "_PR0", "_PR3" };
Edward O'Callaghan7e262552020-01-23 10:32:33 +1100671 unsigned int reset_gpio = params->reset_gpio ? params->reset_gpio->pins[0] : 0;
672 unsigned int enable_gpio = params->enable_gpio ? params->enable_gpio->pins[0] : 0;
673 unsigned int stop_gpio = params->stop_gpio ? params->stop_gpio->pins[0] : 0;
Furquan Shaikhd47946e2021-09-14 15:54:41 -0700674 char pr_name[ACPI_NAME_BUFFER_SIZE];
Duncan Lauriebd73dbb2017-02-17 17:05:03 -0800675
Furquan Shaikhedf459f2017-08-28 17:20:49 -0700676 if (!reset_gpio && !enable_gpio && !stop_gpio)
Duncan Lauriebd73dbb2017-02-17 17:05:03 -0800677 return;
678
Furquan Shaikhd47946e2021-09-14 15:54:41 -0700679 snprintf(pr_name, sizeof(pr_name), "PR%02X", id++);
680
681 /* PowerResource (PR##, 0, 0) */
682 acpigen_write_power_res(pr_name, 0, 0, power_res_dev_states,
Duncan Lauriebd73dbb2017-02-17 17:05:03 -0800683 ARRAY_SIZE(power_res_dev_states));
684
Raul E Rangel6d2bc002021-05-27 15:04:21 -0600685 if (params->use_gpio_for_status) {
686 acpigen_write_power_res_STA(params);
687 } else {
688 /* Method (_STA, 0, NotSerialized) { Return (0x1) } */
689 acpigen_write_STA(ACPI_POWER_RESOURCE_STATUS_ON_OP);
690 }
Duncan Lauriebd73dbb2017-02-17 17:05:03 -0800691
692 /* Method (_ON, 0, Serialized) */
693 acpigen_write_method_serialized("_ON", 0);
Tim Van Patten3d4665c2022-04-13 11:53:20 -0600694 /* Call _STA and early return if the device is already enabled, since the Linux
695 kernel doesn't check the device status before calling _ON. This avoids
696 unnecessary delays while booting. */
697 if (params->use_gpio_for_status) {
698 /* Local0 = _STA () */
699 acpigen_write_store();
700 acpigen_emit_namestring("_STA");
701 acpigen_emit_byte(LOCAL0_OP);
702 /* If (( Local0 == ACPI_POWER_RESOURCE_STATUS_ON_OP)) */
703 acpigen_write_if_lequal_op_op(LOCAL0_OP, ACPI_POWER_RESOURCE_STATUS_ON_OP);
704 acpigen_write_return_op(ZERO_OP);
705 acpigen_write_if_end();
706 }
Duncan Lauriebd73dbb2017-02-17 17:05:03 -0800707 if (reset_gpio)
Shelley Chena0603392018-04-26 13:52:30 -0700708 acpigen_enable_tx_gpio(params->reset_gpio);
Duncan Lauriebd73dbb2017-02-17 17:05:03 -0800709 if (enable_gpio) {
Shelley Chena0603392018-04-26 13:52:30 -0700710 acpigen_enable_tx_gpio(params->enable_gpio);
711 if (params->enable_delay_ms)
712 acpigen_write_sleep(params->enable_delay_ms);
Duncan Lauriebd73dbb2017-02-17 17:05:03 -0800713 }
714 if (reset_gpio) {
Shelley Chena0603392018-04-26 13:52:30 -0700715 acpigen_disable_tx_gpio(params->reset_gpio);
716 if (params->reset_delay_ms)
717 acpigen_write_sleep(params->reset_delay_ms);
Duncan Lauriebd73dbb2017-02-17 17:05:03 -0800718 }
Furquan Shaikhedf459f2017-08-28 17:20:49 -0700719 if (stop_gpio) {
Shelley Chena0603392018-04-26 13:52:30 -0700720 acpigen_disable_tx_gpio(params->stop_gpio);
721 if (params->stop_delay_ms)
722 acpigen_write_sleep(params->stop_delay_ms);
Furquan Shaikhedf459f2017-08-28 17:20:49 -0700723 }
Duncan Lauriebd73dbb2017-02-17 17:05:03 -0800724 acpigen_pop_len(); /* _ON method */
725
726 /* Method (_OFF, 0, Serialized) */
727 acpigen_write_method_serialized("_OFF", 0);
Shelley Chena0603392018-04-26 13:52:30 -0700728 if (stop_gpio) {
729 acpigen_enable_tx_gpio(params->stop_gpio);
730 if (params->stop_off_delay_ms)
731 acpigen_write_sleep(params->stop_off_delay_ms);
732 }
733 if (reset_gpio) {
734 acpigen_enable_tx_gpio(params->reset_gpio);
735 if (params->reset_off_delay_ms)
736 acpigen_write_sleep(params->reset_off_delay_ms);
737 }
738 if (enable_gpio) {
739 acpigen_disable_tx_gpio(params->enable_gpio);
740 if (params->enable_off_delay_ms)
741 acpigen_write_sleep(params->enable_off_delay_ms);
742 }
Duncan Lauriebd73dbb2017-02-17 17:05:03 -0800743 acpigen_pop_len(); /* _OFF method */
744
Furquan Shaikhd47946e2021-09-14 15:54:41 -0700745 acpigen_pop_len(); /* PowerResource PR## */
Duncan Lauriebd73dbb2017-02-17 17:05:03 -0800746}
747
Duncan Laurieffc99902016-07-02 19:56:06 -0700748static void acpi_dp_write_array(const struct acpi_dp *array);
749static void acpi_dp_write_value(const struct acpi_dp *prop)
Duncan Laurie559e9472016-05-10 13:18:17 -0700750{
751 switch (prop->type) {
752 case ACPI_DP_TYPE_INTEGER:
753 acpigen_write_integer(prop->integer);
754 break;
755 case ACPI_DP_TYPE_STRING:
Harsha Priya3a96ac42016-07-15 17:31:43 -0700756 case ACPI_DP_TYPE_CHILD:
Duncan Laurie559e9472016-05-10 13:18:17 -0700757 acpigen_write_string(prop->string);
758 break;
759 case ACPI_DP_TYPE_REFERENCE:
760 acpigen_emit_namestring(prop->string);
761 break;
Duncan Laurieffc99902016-07-02 19:56:06 -0700762 case ACPI_DP_TYPE_ARRAY:
763 acpi_dp_write_array(prop->array);
764 break;
765 default:
766 break;
Duncan Laurie559e9472016-05-10 13:18:17 -0700767 }
768}
769
Duncan Laurieffc99902016-07-02 19:56:06 -0700770/* Package (2) { "prop->name", VALUE } */
771static void acpi_dp_write_property(const struct acpi_dp *prop)
Duncan Laurie559e9472016-05-10 13:18:17 -0700772{
773 acpigen_write_package(2);
Duncan Laurieffc99902016-07-02 19:56:06 -0700774 acpigen_write_string(prop->name);
Duncan Laurie559e9472016-05-10 13:18:17 -0700775 acpi_dp_write_value(prop);
776 acpigen_pop_len();
Duncan Laurie559e9472016-05-10 13:18:17 -0700777}
778
779/* Write array of Device Properties */
Duncan Laurieffc99902016-07-02 19:56:06 -0700780static void acpi_dp_write_array(const struct acpi_dp *array)
Duncan Laurie559e9472016-05-10 13:18:17 -0700781{
Duncan Laurieffc99902016-07-02 19:56:06 -0700782 const struct acpi_dp *dp;
783 char *pkg_count;
784
785 /* Package element count determined as it is populated */
786 pkg_count = acpigen_write_package(0);
787
Furquan Shaikh35c01bc2016-10-03 23:30:14 -0700788 /*
789 * Only acpi_dp of type DP_TYPE_TABLE is allowed to be an array.
790 * DP_TYPE_TABLE does not have a value to be written. Thus, start
791 * the loop from next type in the array.
792 */
793 for (dp = array->next; dp; dp = dp->next) {
Duncan Laurieffc99902016-07-02 19:56:06 -0700794 acpi_dp_write_value(dp);
795 (*pkg_count)++;
796 }
797
Duncan Laurie559e9472016-05-10 13:18:17 -0700798 acpigen_pop_len();
Duncan Laurie559e9472016-05-10 13:18:17 -0700799}
800
Duncan Laurieffc99902016-07-02 19:56:06 -0700801static void acpi_dp_free(struct acpi_dp *dp)
Duncan Laurie559e9472016-05-10 13:18:17 -0700802{
Duncan Laurieffc99902016-07-02 19:56:06 -0700803 while (dp) {
804 struct acpi_dp *p = dp->next;
805
806 switch (dp->type) {
807 case ACPI_DP_TYPE_CHILD:
808 acpi_dp_free(dp->child);
809 break;
810 case ACPI_DP_TYPE_ARRAY:
811 acpi_dp_free(dp->array);
812 break;
813 default:
814 break;
815 }
816
817 free(dp);
818 dp = p;
819 }
Duncan Laurie559e9472016-05-10 13:18:17 -0700820}
821
Duncan Laurie84fac412020-06-03 12:36:51 -0700822static bool acpi_dp_write_properties(struct acpi_dp *prop, const char *uuid)
823{
824 struct acpi_dp *dp;
825 char *prop_count = NULL;
826
827 /* Print base properties */
828 for (dp = prop; dp; dp = dp->next) {
829 if (dp->type == ACPI_DP_TYPE_TABLE ||
830 dp->type == ACPI_DP_TYPE_CHILD ||
831 dp->type == ACPI_DP_TYPE_PACKAGE)
832 continue;
833
834 /*
835 * The UUID and package is only added when
836 * we come across the first property. This
837 * is to avoid creating a zero-length package
838 * in situations where there are only children.
839 */
840 if (!prop_count) {
841 /* ToUUID (dp->uuid) */
842 acpigen_write_uuid(uuid);
843 /*
844 * Package (PROP), element count determined as
845 * it is populated
846 */
847 prop_count = acpigen_write_package(0);
848 }
849 (*prop_count)++;
850 acpi_dp_write_property(dp);
851 }
852 if (prop_count) {
853 /* Package (PROP) length, if a package was written */
854 acpigen_pop_len();
855 return true;
856 }
857 return false;
858}
859
Simon Glass8bfa51e2020-06-29 16:18:37 -0600860static void acpi_dp_write_(struct acpi_dp *table)
Duncan Laurie559e9472016-05-10 13:18:17 -0700861{
Duncan Laurieffc99902016-07-02 19:56:06 -0700862 struct acpi_dp *dp, *prop;
Duncan Laurie84fac412020-06-03 12:36:51 -0700863 char *dp_count;
Duncan Laurieffc99902016-07-02 19:56:06 -0700864 int child_count = 0;
865
Duncan Lauriec1adeb62020-04-29 00:04:14 -0700866 if (!table || table->type != ACPI_DP_TYPE_TABLE || !table->next)
Duncan Laurieffc99902016-07-02 19:56:06 -0700867 return;
868
869 /* Name (name) */
870 acpigen_write_name(table->name);
871
872 /* Device Property list starts with the next entry */
873 prop = table->next;
874
Matt Delco08258882019-01-30 11:16:08 -0800875 /* Package (DP), default to assuming no properties or children */
876 dp_count = acpigen_write_package(0);
Duncan Laurieffc99902016-07-02 19:56:06 -0700877
878 /* Print base properties */
Duncan Laurie84fac412020-06-03 12:36:51 -0700879 if (acpi_dp_write_properties(prop, table->uuid))
880 *dp_count += 2;
Duncan Laurieffc99902016-07-02 19:56:06 -0700881
Duncan Laurie84fac412020-06-03 12:36:51 -0700882 /* Count child properties */
883 for (dp = prop; dp; dp = dp->next)
884 if (dp->type == ACPI_DP_TYPE_CHILD)
885 child_count++;
886
887 /* Add child properties to the base table */
Duncan Laurieffc99902016-07-02 19:56:06 -0700888 if (child_count) {
Duncan Laurie84fac412020-06-03 12:36:51 -0700889 /* Update DP package count */
Matt Delco08258882019-01-30 11:16:08 -0800890 *dp_count += 2;
Duncan Laurieffc99902016-07-02 19:56:06 -0700891 /* ToUUID (ACPI_DP_CHILD_UUID) */
892 acpigen_write_uuid(ACPI_DP_CHILD_UUID);
893
894 /* Print child pointer properties */
895 acpigen_write_package(child_count);
896
897 for (dp = prop; dp; dp = dp->next)
898 if (dp->type == ACPI_DP_TYPE_CHILD)
899 acpi_dp_write_property(dp);
Matt Delco08258882019-01-30 11:16:08 -0800900 /* Package (CHILD) length */
Duncan Laurieffc99902016-07-02 19:56:06 -0700901 acpigen_pop_len();
902 }
903
Duncan Laurie84fac412020-06-03 12:36:51 -0700904 /* Write packages of properties with unique UUID */
905 for (dp = prop; dp; dp = dp->next)
906 if (dp->type == ACPI_DP_TYPE_PACKAGE)
907 if (acpi_dp_write_properties(dp->child, dp->uuid))
908 *dp_count += 2;
909
Duncan Laurieffc99902016-07-02 19:56:06 -0700910 /* Package (DP) length */
911 acpigen_pop_len();
912
913 /* Recursively parse children into separate tables */
914 for (dp = prop; dp; dp = dp->next)
915 if (dp->type == ACPI_DP_TYPE_CHILD)
Simon Glass8bfa51e2020-06-29 16:18:37 -0600916 acpi_dp_write_(dp->child);
917}
918
919void acpi_dp_write(struct acpi_dp *table)
920{
921 acpi_dp_write_(table);
Duncan Laurieffc99902016-07-02 19:56:06 -0700922
923 /* Clean up */
924 acpi_dp_free(table);
925}
926
927static struct acpi_dp *acpi_dp_new(struct acpi_dp *dp, enum acpi_dp_type type,
928 const char *name)
929{
930 struct acpi_dp *new;
931
932 new = malloc(sizeof(struct acpi_dp));
933 if (!new)
934 return NULL;
935
936 memset(new, 0, sizeof(*new));
937 new->type = type;
938 new->name = name;
Duncan Laurie84fac412020-06-03 12:36:51 -0700939 new->uuid = ACPI_DP_UUID;
Duncan Laurieffc99902016-07-02 19:56:06 -0700940
941 if (dp) {
942 /* Add to end of property list */
943 while (dp->next)
944 dp = dp->next;
945 dp->next = new;
946 }
947
948 return new;
949}
950
951struct acpi_dp *acpi_dp_new_table(const char *name)
952{
953 return acpi_dp_new(NULL, ACPI_DP_TYPE_TABLE, name);
954}
955
Duncan Laurieb3023b62017-08-29 08:26:50 -0700956size_t acpi_dp_add_property_list(struct acpi_dp *dp,
957 const struct acpi_dp *property_list,
958 size_t property_count)
959{
960 const struct acpi_dp *prop;
961 size_t i, properties_added = 0;
962
Jacob Garberc30e5902019-05-23 14:34:58 -0600963 if (!dp || !property_list)
964 return 0;
965
Duncan Laurieb3023b62017-08-29 08:26:50 -0700966 for (i = 0; i < property_count; i++) {
967 prop = &property_list[i];
968
969 if (prop->type == ACPI_DP_TYPE_UNKNOWN || !prop->name)
970 continue;
971
972 switch (prop->type) {
973 case ACPI_DP_TYPE_INTEGER:
974 acpi_dp_add_integer(dp, prop->name, prop->integer);
975 break;
976 case ACPI_DP_TYPE_STRING:
977 acpi_dp_add_string(dp, prop->name, prop->string);
978 break;
979 case ACPI_DP_TYPE_REFERENCE:
980 acpi_dp_add_reference(dp, prop->name, prop->string);
981 break;
982 case ACPI_DP_TYPE_ARRAY:
983 acpi_dp_add_array(dp, prop->array);
984 break;
985 case ACPI_DP_TYPE_CHILD:
986 acpi_dp_add_child(dp, prop->name, prop->child);
987 break;
988 default:
989 continue;
990 }
991
992 ++properties_added;
993 }
994
995 return properties_added;
996}
997
Duncan Laurieffc99902016-07-02 19:56:06 -0700998struct acpi_dp *acpi_dp_add_integer(struct acpi_dp *dp, const char *name,
999 uint64_t value)
1000{
Jacob Garberc30e5902019-05-23 14:34:58 -06001001 if (!dp)
1002 return NULL;
1003
Duncan Laurieffc99902016-07-02 19:56:06 -07001004 struct acpi_dp *new = acpi_dp_new(dp, ACPI_DP_TYPE_INTEGER, name);
1005
1006 if (new)
1007 new->integer = value;
1008
1009 return new;
1010}
1011
1012struct acpi_dp *acpi_dp_add_string(struct acpi_dp *dp, const char *name,
1013 const char *string)
1014{
Jacob Garberc30e5902019-05-23 14:34:58 -06001015 if (!dp)
1016 return NULL;
1017
Duncan Laurieffc99902016-07-02 19:56:06 -07001018 struct acpi_dp *new = acpi_dp_new(dp, ACPI_DP_TYPE_STRING, name);
1019
1020 if (new)
1021 new->string = string;
1022
1023 return new;
1024}
1025
1026struct acpi_dp *acpi_dp_add_reference(struct acpi_dp *dp, const char *name,
1027 const char *reference)
1028{
Jacob Garberc30e5902019-05-23 14:34:58 -06001029 if (!dp)
1030 return NULL;
1031
Duncan Laurieffc99902016-07-02 19:56:06 -07001032 struct acpi_dp *new = acpi_dp_new(dp, ACPI_DP_TYPE_REFERENCE, name);
1033
1034 if (new)
1035 new->string = reference;
1036
1037 return new;
1038}
1039
1040struct acpi_dp *acpi_dp_add_child(struct acpi_dp *dp, const char *name,
1041 struct acpi_dp *child)
1042{
1043 struct acpi_dp *new;
1044
Jacob Garberc30e5902019-05-23 14:34:58 -06001045 if (!dp || !child || child->type != ACPI_DP_TYPE_TABLE)
Duncan Laurieffc99902016-07-02 19:56:06 -07001046 return NULL;
1047
1048 new = acpi_dp_new(dp, ACPI_DP_TYPE_CHILD, name);
1049 if (new) {
1050 new->child = child;
1051 new->string = child->name;
1052 }
1053
1054 return new;
1055}
1056
Duncan Laurie84fac412020-06-03 12:36:51 -07001057struct acpi_dp *acpi_dp_add_package(struct acpi_dp *dp, struct acpi_dp *package)
1058{
1059 struct acpi_dp *new;
1060
1061 if (!dp || !package || package->type != ACPI_DP_TYPE_TABLE)
1062 return NULL;
1063
1064 new = acpi_dp_new(dp, ACPI_DP_TYPE_PACKAGE, NULL);
1065 if (new) {
1066 new->uuid = package->name;
1067 new->child = package;
1068 }
1069
1070 return new;
1071}
1072
Duncan Laurieffc99902016-07-02 19:56:06 -07001073struct acpi_dp *acpi_dp_add_array(struct acpi_dp *dp, struct acpi_dp *array)
1074{
1075 struct acpi_dp *new;
1076
Jacob Garberc30e5902019-05-23 14:34:58 -06001077 if (!dp || !array || array->type != ACPI_DP_TYPE_TABLE)
Duncan Laurieffc99902016-07-02 19:56:06 -07001078 return NULL;
1079
1080 new = acpi_dp_new(dp, ACPI_DP_TYPE_ARRAY, array->name);
1081 if (new)
1082 new->array = array;
1083
1084 return new;
1085}
1086
1087struct acpi_dp *acpi_dp_add_integer_array(struct acpi_dp *dp, const char *name,
Duncan Laurieed6eb272020-04-29 11:39:08 -07001088 const uint64_t *array, int len)
Duncan Laurieffc99902016-07-02 19:56:06 -07001089{
1090 struct acpi_dp *dp_array;
1091 int i;
1092
Jacob Garberc30e5902019-05-23 14:34:58 -06001093 if (!dp || len <= 0)
Duncan Laurieffc99902016-07-02 19:56:06 -07001094 return NULL;
1095
1096 dp_array = acpi_dp_new_table(name);
1097 if (!dp_array)
1098 return NULL;
1099
1100 for (i = 0; i < len; i++)
1101 if (!acpi_dp_add_integer(dp_array, NULL, array[i]))
1102 break;
1103
1104 acpi_dp_add_array(dp, dp_array);
1105
1106 return dp_array;
1107}
1108
Karthikeyan Ramasubramanian685dbe12020-10-05 10:44:50 -06001109struct acpi_dp *acpi_dp_add_gpio_array(struct acpi_dp *dp, const char *name,
1110 const struct acpi_gpio_res_params *params,
1111 size_t param_count)
1112{
1113 struct acpi_dp *gpio;
1114 uint32_t i;
1115
1116 if (!dp || !param_count)
1117 return NULL;
1118
1119 gpio = acpi_dp_new_table(name);
1120 if (!gpio)
1121 return NULL;
1122
1123 /*
1124 * Generate ACPI identifiers as follows:
1125 * Package () {
1126 * name, // e.g. cs-gpios
1127 * Package() {
1128 * ref, index, pin, active_low, // GPIO-0 (params[0])
1129 * ref, index, pin, active_low, // GPIO-1 (params[1])
1130 * ...
1131 * }
1132 * }
1133 */
1134 for (i = 0; i < param_count; i++, params++) {
1135 /*
1136 * If refs is NULL, leave a hole in the gpio array. This can be used in
1137 * conditions where some controllers use both GPIOs and native signals.
1138 */
1139 if (!params->ref) {
1140 acpi_dp_add_integer(gpio, NULL, 0);
1141 continue;
1142 }
1143
1144 /* The device that has _CRS containing GpioIO()/GpioInt() */
1145 acpi_dp_add_reference(gpio, NULL, params->ref);
1146
1147 /* Index of the GPIO resource in _CRS starting from zero */
1148 acpi_dp_add_integer(gpio, NULL, params->index);
1149
1150 /* Pin in the GPIO resource, typically zero */
1151 acpi_dp_add_integer(gpio, NULL, params->pin);
1152
1153 /* Set if pin is active low */
1154 acpi_dp_add_integer(gpio, NULL, params->active_low);
1155 }
1156 acpi_dp_add_array(dp, gpio);
1157
1158 return gpio;
1159
1160}
1161
1162
Duncan Laurieffc99902016-07-02 19:56:06 -07001163struct acpi_dp *acpi_dp_add_gpio(struct acpi_dp *dp, const char *name,
1164 const char *ref, int index, int pin,
1165 int active_low)
1166{
Karthikeyan Ramasubramanian685dbe12020-10-05 10:44:50 -06001167 struct acpi_gpio_res_params param = {
1168 .ref = ref,
1169 .index = index,
1170 .pin = pin,
1171 .active_low = active_low,
1172 };
Jacob Garberc30e5902019-05-23 14:34:58 -06001173
Karthikeyan Ramasubramanian685dbe12020-10-05 10:44:50 -06001174 return acpi_dp_add_gpio_array(dp, name, &param, 1);
Duncan Laurie559e9472016-05-10 13:18:17 -07001175}
Furquan Shaikhd1130af2020-04-23 12:51:42 -07001176
1177/*
1178 * This function writes a PCI device with _ADR object:
1179 * Example:
1180 * Scope (\_SB.PCI0)
1181 * {
1182 * Device (IGFX)
1183 * {
1184 * Name (_ADR, 0x0000000000000000)
1185 * Method (_STA, 0, NotSerialized) { Return (status) }
1186 * }
1187 * }
1188 */
Furquan Shaikh7536a392020-04-24 21:59:21 -07001189void acpi_device_write_pci_dev(const struct device *dev)
Furquan Shaikhd1130af2020-04-23 12:51:42 -07001190{
1191 const char *scope = acpi_device_scope(dev);
1192 const char *name = acpi_device_name(dev);
1193
1194 assert(dev->path.type == DEVICE_PATH_PCI);
1195 assert(name);
1196 assert(scope);
1197
1198 acpigen_write_scope(scope);
1199 acpigen_write_device(name);
1200
1201 acpigen_write_ADR_pci_device(dev);
1202 acpigen_write_STA(acpi_device_status(dev));
1203
1204 acpigen_pop_len(); /* Device */
1205 acpigen_pop_len(); /* Scope */
1206}
Kapil Porwalddc52a62022-11-26 19:10:57 +05301207
Kapil Porwal75436272022-11-28 17:25:48 +05301208/*
1209 * Helper function to add given integer property with an UUID to _DSD in the current scope.
1210 *
1211 * dsd - Pointer to a _DSD object.
1212 * Append to existing _DSD object if not NULL.
1213 * Create new _DSD object and flush it if NULL.
1214 * uuid - Pointer to the UUID string.
1215 * name - Pointer to the property name string.
1216 * value - Value of the integer property.
1217 */
1218static void acpi_device_add_integer_property_with_uuid(struct acpi_dp *dsd,
1219 const char *uuid,
1220 const char *name,
1221 uint64_t value)
Kapil Porwalddc52a62022-11-26 19:10:57 +05301222{
1223 struct acpi_dp *prev_dsd = dsd, *pkg;
1224 if (prev_dsd == NULL)
1225 dsd = acpi_dp_new_table("_DSD");
Kapil Porwal75436272022-11-28 17:25:48 +05301226 pkg = acpi_dp_new_table(uuid);
1227 acpi_dp_add_integer(pkg, name, value);
Kapil Porwalddc52a62022-11-26 19:10:57 +05301228 acpi_dp_add_package(dsd, pkg);
1229 if (prev_dsd == NULL)
1230 acpi_dp_write(dsd);
1231}
Kapil Porwal75436272022-11-28 17:25:48 +05301232
1233/* _DSD with ExternalFacingPort */
1234void acpi_device_add_external_facing_port(struct acpi_dp *dsd)
1235{
1236 acpi_device_add_integer_property_with_uuid(dsd,
1237 ACPI_DSD_EXTERNAL_FACING_PORT_UUID,
1238 ACPI_DSD_EXTERNAL_FACING_PORT_NAME,
1239 1);
1240}
1241
1242/* _DSD with HotPlugSupportInD3 */
1243void acpi_device_add_hotplug_support_in_d3(struct acpi_dp *dsd)
1244{
1245 acpi_device_add_integer_property_with_uuid(dsd,
1246 ACPI_DSD_HOTPLUG_IN_D3_UUID,
1247 ACPI_DSD_HOTPLUG_IN_D3_NAME,
1248 1);
1249}
1250
1251/* _DSD with DmaProperty */
1252void acpi_device_add_dma_property(struct acpi_dp *dsd)
1253{
1254 acpi_device_add_integer_property_with_uuid(dsd,
1255 ACPI_DSD_DMA_PROPERTY_UUID,
1256 ACPI_DSD_DMA_PROPERTY_NAME,
1257 1);
1258}
1259
1260/* _DSD with StorageD3Enable */
1261void acpi_device_add_storage_d3_enable(struct acpi_dp *dsd)
1262{
1263 acpi_device_add_integer_property_with_uuid(dsd,
1264 ACPI_DSD_STORAGE_D3_UUID,
1265 ACPI_DSD_STORAGE_D3_NAME,
1266 1);
1267}