blob: 92000a3d8b212c5a0e52ed9df0b667bdae47c9f2 [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 */
86 while (pdev->bus) {
87 pdev = pdev->bus->dev;
88 if (!pdev)
89 break;
90 if (pdev->path.type == DEVICE_PATH_ROOT)
91 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 */
150 if (dev->path.type != DEVICE_PATH_ROOT && dev->bus && dev->bus->dev)
151 next = acpi_device_path_fill(dev->bus->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",
Timothy Pearsonaeb61012017-04-13 17:06:16 -0500157 (dev->path.type == DEVICE_PATH_ROOT
158 || (strlen(name) == 0)) ?
159 "" : ".", name);
Duncan Lauried9af3ce2016-05-08 18:15:25 -0700160
161 return next;
162}
163
164/*
165 * Warning: just as with dev_path() this uses a static buffer
Martin Roth0949e732021-10-01 14:28:22 -0600166 * so should not be called multiple times in one statement
Duncan Lauried9af3ce2016-05-08 18:15:25 -0700167 */
Aamir Bohrae825d3f2019-07-30 10:11:41 +0530168const char *acpi_device_path(const struct device *dev)
Duncan Lauried9af3ce2016-05-08 18:15:25 -0700169{
170 static char buf[DEVICE_PATH_MAX] = {};
171
172 if (!dev)
173 return NULL;
174
175 if (acpi_device_path_fill(dev, buf, sizeof(buf), 0) <= 0)
176 return NULL;
177
178 return buf;
179}
180
181/* Return the path of the parent device as the ACPI Scope for this device */
Aamir Bohrae825d3f2019-07-30 10:11:41 +0530182const char *acpi_device_scope(const struct device *dev)
Duncan Lauried9af3ce2016-05-08 18:15:25 -0700183{
Duncan Lauried02685b2016-06-30 14:37:37 -0700184 static char buf[DEVICE_PATH_MAX] = {};
185
Duncan Lauried9af3ce2016-05-08 18:15:25 -0700186 if (!dev || !dev->bus || !dev->bus->dev)
187 return NULL;
188
Duncan Lauried02685b2016-06-30 14:37:37 -0700189 if (acpi_device_path_fill(dev->bus->dev, buf, sizeof(buf), 0) <= 0)
190 return NULL;
191
192 return buf;
Duncan Lauried9af3ce2016-05-08 18:15:25 -0700193}
194
Elyes HAOUAS6716bab2020-01-09 21:29:25 +0100195/* Concatenate the device path and provided name suffix */
Aamir Bohrae825d3f2019-07-30 10:11:41 +0530196const char *acpi_device_path_join(const struct device *dev, const char *name)
Duncan Lauried9af3ce2016-05-08 18:15:25 -0700197{
198 static char buf[DEVICE_PATH_MAX] = {};
Jacob Garberf2ba2d92019-07-02 10:50:10 -0600199 ssize_t len;
Duncan Lauried9af3ce2016-05-08 18:15:25 -0700200
201 if (!dev)
202 return NULL;
203
204 /* Build the path of this device */
205 len = acpi_device_path_fill(dev, buf, sizeof(buf), 0);
206 if (len <= 0)
207 return NULL;
208
209 /* Ensure there is room for the added name, separator, and NUL */
210 if ((len + strlen(name) + 2) > sizeof(buf))
211 return NULL;
212 snprintf(buf + len, sizeof(buf) - len, ".%s", name);
213
214 return buf;
215}
Duncan Laurie6b7c1f62016-05-09 15:38:36 -0700216
Hung-Te Linb4be50c2018-09-10 10:55:49 +0800217int acpi_device_status(const struct device *dev)
218{
219 if (!dev->enabled)
220 return ACPI_STATUS_DEVICE_ALL_OFF;
221 if (dev->hidden)
222 return ACPI_STATUS_DEVICE_HIDDEN_ON;
223 return ACPI_STATUS_DEVICE_ALL_ON;
224}
225
Patrick Rudolphc83bab62019-12-13 12:16:06 +0100226/* Write the unique _UID based on ACPI device path. */
Furquan Shaikhd14d03a2020-04-24 21:27:29 -0700227void acpi_device_write_uid(const struct device *dev)
Patrick Rudolphc83bab62019-12-13 12:16:06 +0100228{
229 acpigen_write_name_integer("_UID", acpi_device_uid(dev));
230}
231
Duncan Laurie6b7c1f62016-05-09 15:38:36 -0700232/* ACPI 6.1 section 6.4.3.6: Extended Interrupt Descriptor */
233void acpi_device_write_interrupt(const struct acpi_irq *irq)
234{
235 void *desc_length;
236 uint8_t flags;
237
238 if (!irq || !irq->pin)
239 return;
240
241 /* This is supported by GpioInt() but not Interrupt() */
Furquan Shaikh5b9b5932017-02-21 13:16:30 -0800242 if (irq->polarity == ACPI_IRQ_ACTIVE_BOTH)
Duncan Laurie6b7c1f62016-05-09 15:38:36 -0700243 return;
244
245 /* Byte 0: Descriptor Type */
246 acpigen_emit_byte(ACPI_DESCRIPTOR_INTERRUPT);
247
248 /* Byte 1-2: Length (filled in later) */
249 desc_length = acpi_device_write_zero_len();
250
251 /*
252 * Byte 3: Flags
253 * [7:5]: Reserved
254 * [4]: Wake (0=NO_WAKE 1=WAKE)
255 * [3]: Sharing (0=EXCLUSIVE 1=SHARED)
256 * [2]: Polarity (0=HIGH 1=LOW)
257 * [1]: Mode (0=LEVEL 1=EDGE)
258 * [0]: Resource (0=PRODUCER 1=CONSUMER)
259 */
260 flags = 1 << 0; /* ResourceConsumer */
Furquan Shaikh5b9b5932017-02-21 13:16:30 -0800261 if (irq->mode == ACPI_IRQ_EDGE_TRIGGERED)
Duncan Laurie6b7c1f62016-05-09 15:38:36 -0700262 flags |= 1 << 1;
Furquan Shaikh5b9b5932017-02-21 13:16:30 -0800263 if (irq->polarity == ACPI_IRQ_ACTIVE_LOW)
Duncan Laurie6b7c1f62016-05-09 15:38:36 -0700264 flags |= 1 << 2;
Furquan Shaikh5b9b5932017-02-21 13:16:30 -0800265 if (irq->shared == ACPI_IRQ_SHARED)
Duncan Laurie6b7c1f62016-05-09 15:38:36 -0700266 flags |= 1 << 3;
Furquan Shaikh5b9b5932017-02-21 13:16:30 -0800267 if (irq->wake == ACPI_IRQ_WAKE)
Duncan Laurie6b7c1f62016-05-09 15:38:36 -0700268 flags |= 1 << 4;
269 acpigen_emit_byte(flags);
270
271 /* Byte 4: Interrupt Table Entry Count */
272 acpigen_emit_byte(1);
273
274 /* Byte 5-8: Interrupt Number */
275 acpigen_emit_dword(irq->pin);
276
277 /* Fill in Descriptor Length (account for len word) */
278 acpi_device_fill_len(desc_length);
279}
Duncan Lauriecfb6ea72016-05-09 17:08:38 -0700280
281/* ACPI 6.1 section 6.4.3.8.1 - GPIO Interrupt or I/O */
282void acpi_device_write_gpio(const struct acpi_gpio *gpio)
283{
284 void *start, *desc_length;
285 void *pin_table_offset, *vendor_data_offset, *resource_offset;
286 uint16_t flags = 0;
287 int pin;
288
289 if (!gpio || gpio->type > ACPI_GPIO_TYPE_IO)
290 return;
291
292 start = acpigen_get_current();
293
294 /* Byte 0: Descriptor Type */
295 acpigen_emit_byte(ACPI_DESCRIPTOR_GPIO);
296
297 /* Byte 1-2: Length (fill in later) */
298 desc_length = acpi_device_write_zero_len();
299
300 /* Byte 3: Revision ID */
301 acpigen_emit_byte(ACPI_GPIO_REVISION_ID);
302
303 /* Byte 4: GpioIo or GpioInt */
304 acpigen_emit_byte(gpio->type);
305
306 /*
307 * Byte 5-6: General Flags
308 * [15:1]: 0 => Reserved
309 * [0]: 1 => ResourceConsumer
310 */
311 acpigen_emit_word(1 << 0);
312
313 switch (gpio->type) {
314 case ACPI_GPIO_TYPE_INTERRUPT:
315 /*
316 * Byte 7-8: GPIO Interrupt Flags
317 * [15:5]: 0 => Reserved
318 * [4]: Wake (0=NO_WAKE 1=WAKE)
319 * [3]: Sharing (0=EXCLUSIVE 1=SHARED)
320 * [2:1]: Polarity (0=HIGH 1=LOW 2=BOTH)
321 * [0]: Mode (0=LEVEL 1=EDGE)
322 */
Furquan Shaikh5b9b5932017-02-21 13:16:30 -0800323 if (gpio->irq.mode == ACPI_IRQ_EDGE_TRIGGERED)
Duncan Lauriecfb6ea72016-05-09 17:08:38 -0700324 flags |= 1 << 0;
Furquan Shaikh5b9b5932017-02-21 13:16:30 -0800325 if (gpio->irq.shared == ACPI_IRQ_SHARED)
Duncan Lauriecfb6ea72016-05-09 17:08:38 -0700326 flags |= 1 << 3;
Furquan Shaikh5b9b5932017-02-21 13:16:30 -0800327 if (gpio->irq.wake == ACPI_IRQ_WAKE)
Duncan Lauriecfb6ea72016-05-09 17:08:38 -0700328 flags |= 1 << 4;
329
330 switch (gpio->irq.polarity) {
Furquan Shaikh5b9b5932017-02-21 13:16:30 -0800331 case ACPI_IRQ_ACTIVE_HIGH:
Duncan Lauriecfb6ea72016-05-09 17:08:38 -0700332 flags |= 0 << 1;
333 break;
Furquan Shaikh5b9b5932017-02-21 13:16:30 -0800334 case ACPI_IRQ_ACTIVE_LOW:
Duncan Lauriecfb6ea72016-05-09 17:08:38 -0700335 flags |= 1 << 1;
336 break;
Furquan Shaikh5b9b5932017-02-21 13:16:30 -0800337 case ACPI_IRQ_ACTIVE_BOTH:
Duncan Lauriecfb6ea72016-05-09 17:08:38 -0700338 flags |= 2 << 1;
339 break;
340 }
341 break;
342
343 case ACPI_GPIO_TYPE_IO:
344 /*
345 * Byte 7-8: GPIO IO Flags
346 * [15:4]: 0 => Reserved
347 * [3]: Sharing (0=EXCLUSIVE 1=SHARED)
348 * [2]: 0 => Reserved
349 * [1:0]: IO Restriction
350 * 0 => IoRestrictionNone
351 * 1 => IoRestrictionInputOnly
352 * 2 => IoRestrictionOutputOnly
353 * 3 => IoRestrictionNoneAndPreserve
354 */
355 flags |= gpio->io_restrict & 3;
356 if (gpio->io_shared)
357 flags |= 1 << 3;
358 break;
359 }
360 acpigen_emit_word(flags);
361
362 /*
363 * Byte 9: Pin Configuration
364 * 0x01 => Default (no configuration applied)
365 * 0x02 => Pull-up
366 * 0x03 => Pull-down
367 * 0x04-0x7F => Reserved
368 * 0x80-0xff => Vendor defined
369 */
370 acpigen_emit_byte(gpio->pull);
371
372 /* Byte 10-11: Output Drive Strength in 1/100 mA */
373 acpigen_emit_word(gpio->output_drive_strength);
374
375 /* Byte 12-13: Debounce Timeout in 1/100 ms */
376 acpigen_emit_word(gpio->interrupt_debounce_timeout);
377
378 /* Byte 14-15: Pin Table Offset, relative to start */
379 pin_table_offset = acpi_device_write_zero_len();
380
381 /* Byte 16: Reserved */
382 acpigen_emit_byte(0);
383
384 /* Byte 17-18: Resource Source Name Offset, relative to start */
385 resource_offset = acpi_device_write_zero_len();
386
387 /* Byte 19-20: Vendor Data Offset, relative to start */
388 vendor_data_offset = acpi_device_write_zero_len();
389
390 /* Byte 21-22: Vendor Data Length */
391 acpigen_emit_word(0);
392
393 /* Fill in Pin Table Offset */
394 acpi_device_fill_from_len(pin_table_offset, start);
395
396 /* Pin Table, one word for each pin */
Duncan Laurie5b6c28c2016-06-29 10:47:22 -0700397 for (pin = 0; pin < gpio->pin_count; pin++) {
398 uint16_t acpi_pin = gpio->pins[pin];
Julius Wernercd49cce2019-03-05 16:53:33 -0800399#if CONFIG(GENERIC_GPIO_LIB)
Duncan Laurie5b6c28c2016-06-29 10:47:22 -0700400 acpi_pin = gpio_acpi_pin(acpi_pin);
401#endif
402 acpigen_emit_word(acpi_pin);
403 }
Duncan Lauriecfb6ea72016-05-09 17:08:38 -0700404
405 /* Fill in Resource Source Name Offset */
406 acpi_device_fill_from_len(resource_offset, start);
407
408 /* Resource Source Name String */
Julius Wernercd49cce2019-03-05 16:53:33 -0800409#if CONFIG(GENERIC_GPIO_LIB)
Duncan Lauriecfb6ea72016-05-09 17:08:38 -0700410 acpigen_emit_string(gpio->resource ? : gpio_acpi_path(gpio->pins[0]));
411#else
412 acpigen_emit_string(gpio->resource);
413#endif
414
415 /* Fill in Vendor Data Offset */
416 acpi_device_fill_from_len(vendor_data_offset, start);
417
418 /* Fill in GPIO Descriptor Length (account for len word) */
419 acpi_device_fill_len(desc_length);
420}
Duncan Laurie1010b4a2016-05-09 20:10:47 -0700421
422/* ACPI 6.1 section 6.4.3.8.2.1 - I2cSerialBus() */
423void acpi_device_write_i2c(const struct acpi_i2c *i2c)
424{
425 void *desc_length, *type_length;
426
427 /* Byte 0: Descriptor Type */
428 acpigen_emit_byte(ACPI_DESCRIPTOR_SERIAL_BUS);
429
430 /* Byte 1+2: Length (filled in later) */
431 desc_length = acpi_device_write_zero_len();
432
433 /* Byte 3: Revision ID */
Elyes HAOUAS34564ed2019-04-16 08:12:10 +0200434 acpigen_emit_byte(ACPI_I2C_SERIAL_BUS_REVISION_ID);
Duncan Laurie1010b4a2016-05-09 20:10:47 -0700435
436 /* Byte 4: Resource Source Index is Reserved */
437 acpigen_emit_byte(0);
438
439 /* Byte 5: Serial Bus Type is I2C */
440 acpigen_emit_byte(ACPI_SERIAL_BUS_TYPE_I2C);
441
442 /*
443 * Byte 6: Flags
444 * [7:2]: 0 => Reserved
445 * [1]: 1 => ResourceConsumer
446 * [0]: 0 => ControllerInitiated
447 */
448 acpigen_emit_byte(1 << 1);
449
450 /*
451 * Byte 7-8: Type Specific Flags
452 * [15:1]: 0 => Reserved
453 * [0]: 0 => 7bit, 1 => 10bit
454 */
455 acpigen_emit_word(i2c->mode_10bit);
456
457 /* Byte 9: Type Specific Revision ID */
Elyes HAOUAS34564ed2019-04-16 08:12:10 +0200458 acpigen_emit_byte(ACPI_I2C_TYPE_SPECIFIC_REVISION_ID);
Duncan Laurie1010b4a2016-05-09 20:10:47 -0700459
460 /* Byte 10-11: I2C Type Data Length */
461 type_length = acpi_device_write_zero_len();
462
463 /* Byte 12-15: I2C Bus Speed */
464 acpigen_emit_dword(i2c->speed);
465
466 /* Byte 16-17: I2C Slave Address */
467 acpigen_emit_word(i2c->address);
468
469 /* Fill in Type Data Length */
470 acpi_device_fill_len(type_length);
471
472 /* Byte 18+: ResourceSource */
473 acpigen_emit_string(i2c->resource);
474
475 /* Fill in I2C Descriptor Length */
476 acpi_device_fill_len(desc_length);
477}
Duncan Laurie70c86d92016-05-10 07:26:34 -0700478
479/* ACPI 6.1 section 6.4.3.8.2.2 - SpiSerialBus() */
480void acpi_device_write_spi(const struct acpi_spi *spi)
481{
482 void *desc_length, *type_length;
483 uint16_t flags = 0;
484
485 /* Byte 0: Descriptor Type */
486 acpigen_emit_byte(ACPI_DESCRIPTOR_SERIAL_BUS);
487
488 /* Byte 1+2: Length (filled in later) */
489 desc_length = acpi_device_write_zero_len();
490
491 /* Byte 3: Revision ID */
Elyes HAOUAS34564ed2019-04-16 08:12:10 +0200492 acpigen_emit_byte(ACPI_SPI_SERIAL_BUS_REVISION_ID);
Duncan Laurie70c86d92016-05-10 07:26:34 -0700493
494 /* Byte 4: Resource Source Index is Reserved */
495 acpigen_emit_byte(0);
496
497 /* Byte 5: Serial Bus Type is SPI */
498 acpigen_emit_byte(ACPI_SERIAL_BUS_TYPE_SPI);
499
500 /*
501 * Byte 6: Flags
502 * [7:2]: 0 => Reserved
503 * [1]: 1 => ResourceConsumer
504 * [0]: 0 => ControllerInitiated
505 */
506 acpigen_emit_byte(1 << 1);
507
508 /*
509 * Byte 7-8: Type Specific Flags
510 * [15:2]: 0 => Reserved
511 * [1]: 0 => ActiveLow, 1 => ActiveHigh
512 * [0]: 0 => FourWire, 1 => ThreeWire
513 */
514 if (spi->wire_mode == SPI_3_WIRE_MODE)
515 flags |= 1 << 0;
516 if (spi->device_select_polarity == SPI_POLARITY_HIGH)
517 flags |= 1 << 1;
518 acpigen_emit_word(flags);
519
520 /* Byte 9: Type Specific Revision ID */
Elyes HAOUAS34564ed2019-04-16 08:12:10 +0200521 acpigen_emit_byte(ACPI_SPI_TYPE_SPECIFIC_REVISION_ID);
Duncan Laurie70c86d92016-05-10 07:26:34 -0700522
523 /* Byte 10-11: SPI Type Data Length */
524 type_length = acpi_device_write_zero_len();
525
526 /* Byte 12-15: Connection Speed */
527 acpigen_emit_dword(spi->speed);
528
529 /* Byte 16: Data Bit Length */
530 acpigen_emit_byte(spi->data_bit_length);
531
532 /* Byte 17: Clock Phase */
533 acpigen_emit_byte(spi->clock_phase);
534
535 /* Byte 18: Clock Polarity */
536 acpigen_emit_byte(spi->clock_polarity);
537
538 /* Byte 19-20: Device Selection */
539 acpigen_emit_word(spi->device_select);
540
541 /* Fill in Type Data Length */
542 acpi_device_fill_len(type_length);
543
544 /* Byte 21+: ResourceSource String */
545 acpigen_emit_string(spi->resource);
546
547 /* Fill in SPI Descriptor Length */
548 acpi_device_fill_len(desc_length);
549}
Duncan Laurie559e9472016-05-10 13:18:17 -0700550
Duncan Lauriedccef0d2020-05-27 12:29:51 -0700551/* UART Serial Bus - UARTSerialBusV2() */
552void acpi_device_write_uart(const struct acpi_uart *uart)
553{
554 void *desc_length, *type_length;
555 uint16_t flags;
556
557 /* Byte 0: Descriptor Type */
558 acpigen_emit_byte(ACPI_DESCRIPTOR_SERIAL_BUS);
559
560 /* Byte 1+2: Length (filled in later) */
561 desc_length = acpi_device_write_zero_len();
562
563 /* Byte 3: Revision ID */
564 acpigen_emit_byte(ACPI_UART_SERIAL_BUS_REVISION_ID);
565
566 /* Byte 4: Resource Source Index is Reserved */
567 acpigen_emit_byte(0);
568
569 /* Byte 5: Serial Bus Type is UART */
570 acpigen_emit_byte(ACPI_SERIAL_BUS_TYPE_UART);
571
572 /*
573 * Byte 6: Flags
574 * [7:2]: 0 => Reserved
575 * [1]: 1 => ResourceConsumer
576 * [0]: 0 => ControllerInitiated
577 */
578 acpigen_emit_byte(BIT(1));
579
580 /*
581 * Byte 7-8: Type Specific Flags
582 * [15:8]: 0 => Reserved
583 * [7]: 0 => Little Endian, 1 => Big Endian
584 * [6:4]: Data bits
585 * [3:2]: Stop bits
586 * [1:0]: Flow control
587 */
588 flags = uart->flow_control & 3;
589 flags |= (uart->stop_bits & 3) << 2;
590 flags |= (uart->data_bits & 7) << 4;
591 flags |= (uart->endian & 1) << 7;
592 acpigen_emit_word(flags);
593
594 /* Byte 9: Type Specific Revision ID */
595 acpigen_emit_byte(ACPI_UART_TYPE_SPECIFIC_REVISION_ID);
596
597 /* Byte 10-11: Type Data Length */
598 type_length = acpi_device_write_zero_len();
599
600 /* Byte 12-15: Initial Baud Rate */
601 acpigen_emit_dword(uart->initial_baud_rate);
602
603 /* Byte 16-17: RX FIFO size */
604 acpigen_emit_word(uart->rx_fifo_bytes);
605
606 /* Byte 18-19: TX FIFO size */
607 acpigen_emit_word(uart->tx_fifo_bytes);
608
609 /* Byte 20: Parity */
610 acpigen_emit_byte(uart->parity);
611
612 /* Byte 21: Lines Enabled */
613 acpigen_emit_byte(uart->lines_in_use);
614
615 /* Fill in Type Data Length */
616 acpi_device_fill_len(type_length);
617
618 /* Byte 22+: ResourceSource */
619 acpigen_emit_string(uart->resource);
620
621 /* Fill in Descriptor Length */
622 acpi_device_fill_len(desc_length);
623}
624
Raul E Rangel6d2bc002021-05-27 15:04:21 -0600625#define ACPI_POWER_RESOURCE_STATUS_ON_OP ONE_OP
626#define ACPI_POWER_RESOURCE_STATUS_OFF_OP ZERO_OP
627
628/**
629 * Writes an ACPI fragment that will check the GPIO and return 0 if the GPIO
630 * state does not match the active parameter.
631 */
632static void acpigen_write_gpio_STA(const struct acpi_gpio *gpio, bool active)
633{
634 if (!gpio || !gpio->pin_count)
635 return;
636
637 /* Read current GPIO status into Local0. */
638 acpigen_get_tx_gpio(gpio);
639
640 /*
641 * If (!Local0)
642 * {
643 * Return (Zero)
644 * }
645 */
646 acpigen_write_if();
647 if (active)
648 acpigen_emit_byte(LNOT_OP);
649 acpigen_emit_byte(LOCAL0_OP);
650 acpigen_write_return_op(ACPI_POWER_RESOURCE_STATUS_OFF_OP);
651 acpigen_write_if_end();
652}
653
654static void acpigen_write_power_res_STA(const struct acpi_power_res_params *params)
655{
656 acpigen_write_method_serialized("_STA", 0);
657
658 /* Verify all the GPIOs are in the ON state, otherwise return 0 */
659 acpigen_write_gpio_STA(params->enable_gpio, true);
660 acpigen_write_gpio_STA(params->reset_gpio, false);
661 acpigen_write_gpio_STA(params->stop_gpio, false);
662
663 /* All GPIOs are in the ON state */
664 acpigen_write_return_op(ACPI_POWER_RESOURCE_STATUS_ON_OP);
665
666 acpigen_pop_len(); /* Method */
667}
668
Duncan Lauriebd73dbb2017-02-17 17:05:03 -0800669/* PowerResource() with Enable and/or Reset control */
Shelley Chena0603392018-04-26 13:52:30 -0700670void acpi_device_add_power_res(const struct acpi_power_res_params *params)
Duncan Lauriebd73dbb2017-02-17 17:05:03 -0800671{
Furquan Shaikhd47946e2021-09-14 15:54:41 -0700672 static uint8_t id;
Furquan Shaikhdc782752020-04-30 22:49:39 -0700673 static const char * const power_res_dev_states[] = { "_PR0", "_PR3" };
Edward O'Callaghan7e262552020-01-23 10:32:33 +1100674 unsigned int reset_gpio = params->reset_gpio ? params->reset_gpio->pins[0] : 0;
675 unsigned int enable_gpio = params->enable_gpio ? params->enable_gpio->pins[0] : 0;
676 unsigned int stop_gpio = params->stop_gpio ? params->stop_gpio->pins[0] : 0;
Furquan Shaikhd47946e2021-09-14 15:54:41 -0700677 char pr_name[ACPI_NAME_BUFFER_SIZE];
Duncan Lauriebd73dbb2017-02-17 17:05:03 -0800678
Furquan Shaikhedf459f2017-08-28 17:20:49 -0700679 if (!reset_gpio && !enable_gpio && !stop_gpio)
Duncan Lauriebd73dbb2017-02-17 17:05:03 -0800680 return;
681
Furquan Shaikhd47946e2021-09-14 15:54:41 -0700682 snprintf(pr_name, sizeof(pr_name), "PR%02X", id++);
683
684 /* PowerResource (PR##, 0, 0) */
685 acpigen_write_power_res(pr_name, 0, 0, power_res_dev_states,
Duncan Lauriebd73dbb2017-02-17 17:05:03 -0800686 ARRAY_SIZE(power_res_dev_states));
687
Raul E Rangel6d2bc002021-05-27 15:04:21 -0600688 if (params->use_gpio_for_status) {
689 acpigen_write_power_res_STA(params);
690 } else {
691 /* Method (_STA, 0, NotSerialized) { Return (0x1) } */
692 acpigen_write_STA(ACPI_POWER_RESOURCE_STATUS_ON_OP);
693 }
Duncan Lauriebd73dbb2017-02-17 17:05:03 -0800694
695 /* Method (_ON, 0, Serialized) */
696 acpigen_write_method_serialized("_ON", 0);
Tim Van Patten3d4665c2022-04-13 11:53:20 -0600697 /* Call _STA and early return if the device is already enabled, since the Linux
698 kernel doesn't check the device status before calling _ON. This avoids
699 unnecessary delays while booting. */
700 if (params->use_gpio_for_status) {
701 /* Local0 = _STA () */
702 acpigen_write_store();
703 acpigen_emit_namestring("_STA");
704 acpigen_emit_byte(LOCAL0_OP);
705 /* If (( Local0 == ACPI_POWER_RESOURCE_STATUS_ON_OP)) */
706 acpigen_write_if_lequal_op_op(LOCAL0_OP, ACPI_POWER_RESOURCE_STATUS_ON_OP);
707 acpigen_write_return_op(ZERO_OP);
708 acpigen_write_if_end();
709 }
Duncan Lauriebd73dbb2017-02-17 17:05:03 -0800710 if (reset_gpio)
Shelley Chena0603392018-04-26 13:52:30 -0700711 acpigen_enable_tx_gpio(params->reset_gpio);
Duncan Lauriebd73dbb2017-02-17 17:05:03 -0800712 if (enable_gpio) {
Shelley Chena0603392018-04-26 13:52:30 -0700713 acpigen_enable_tx_gpio(params->enable_gpio);
714 if (params->enable_delay_ms)
715 acpigen_write_sleep(params->enable_delay_ms);
Duncan Lauriebd73dbb2017-02-17 17:05:03 -0800716 }
717 if (reset_gpio) {
Shelley Chena0603392018-04-26 13:52:30 -0700718 acpigen_disable_tx_gpio(params->reset_gpio);
719 if (params->reset_delay_ms)
720 acpigen_write_sleep(params->reset_delay_ms);
Duncan Lauriebd73dbb2017-02-17 17:05:03 -0800721 }
Furquan Shaikhedf459f2017-08-28 17:20:49 -0700722 if (stop_gpio) {
Shelley Chena0603392018-04-26 13:52:30 -0700723 acpigen_disable_tx_gpio(params->stop_gpio);
724 if (params->stop_delay_ms)
725 acpigen_write_sleep(params->stop_delay_ms);
Furquan Shaikhedf459f2017-08-28 17:20:49 -0700726 }
Duncan Lauriebd73dbb2017-02-17 17:05:03 -0800727 acpigen_pop_len(); /* _ON method */
728
729 /* Method (_OFF, 0, Serialized) */
730 acpigen_write_method_serialized("_OFF", 0);
Shelley Chena0603392018-04-26 13:52:30 -0700731 if (stop_gpio) {
732 acpigen_enable_tx_gpio(params->stop_gpio);
733 if (params->stop_off_delay_ms)
734 acpigen_write_sleep(params->stop_off_delay_ms);
735 }
736 if (reset_gpio) {
737 acpigen_enable_tx_gpio(params->reset_gpio);
738 if (params->reset_off_delay_ms)
739 acpigen_write_sleep(params->reset_off_delay_ms);
740 }
741 if (enable_gpio) {
742 acpigen_disable_tx_gpio(params->enable_gpio);
743 if (params->enable_off_delay_ms)
744 acpigen_write_sleep(params->enable_off_delay_ms);
745 }
Duncan Lauriebd73dbb2017-02-17 17:05:03 -0800746 acpigen_pop_len(); /* _OFF method */
747
Furquan Shaikhd47946e2021-09-14 15:54:41 -0700748 acpigen_pop_len(); /* PowerResource PR## */
Duncan Lauriebd73dbb2017-02-17 17:05:03 -0800749}
750
Duncan Laurieffc99902016-07-02 19:56:06 -0700751static void acpi_dp_write_array(const struct acpi_dp *array);
752static void acpi_dp_write_value(const struct acpi_dp *prop)
Duncan Laurie559e9472016-05-10 13:18:17 -0700753{
754 switch (prop->type) {
755 case ACPI_DP_TYPE_INTEGER:
756 acpigen_write_integer(prop->integer);
757 break;
758 case ACPI_DP_TYPE_STRING:
Harsha Priya3a96ac42016-07-15 17:31:43 -0700759 case ACPI_DP_TYPE_CHILD:
Duncan Laurie559e9472016-05-10 13:18:17 -0700760 acpigen_write_string(prop->string);
761 break;
762 case ACPI_DP_TYPE_REFERENCE:
763 acpigen_emit_namestring(prop->string);
764 break;
Duncan Laurieffc99902016-07-02 19:56:06 -0700765 case ACPI_DP_TYPE_ARRAY:
766 acpi_dp_write_array(prop->array);
767 break;
768 default:
769 break;
Duncan Laurie559e9472016-05-10 13:18:17 -0700770 }
771}
772
Duncan Laurieffc99902016-07-02 19:56:06 -0700773/* Package (2) { "prop->name", VALUE } */
774static void acpi_dp_write_property(const struct acpi_dp *prop)
Duncan Laurie559e9472016-05-10 13:18:17 -0700775{
776 acpigen_write_package(2);
Duncan Laurieffc99902016-07-02 19:56:06 -0700777 acpigen_write_string(prop->name);
Duncan Laurie559e9472016-05-10 13:18:17 -0700778 acpi_dp_write_value(prop);
779 acpigen_pop_len();
Duncan Laurie559e9472016-05-10 13:18:17 -0700780}
781
782/* Write array of Device Properties */
Duncan Laurieffc99902016-07-02 19:56:06 -0700783static void acpi_dp_write_array(const struct acpi_dp *array)
Duncan Laurie559e9472016-05-10 13:18:17 -0700784{
Duncan Laurieffc99902016-07-02 19:56:06 -0700785 const struct acpi_dp *dp;
786 char *pkg_count;
787
788 /* Package element count determined as it is populated */
789 pkg_count = acpigen_write_package(0);
790
Furquan Shaikh35c01bc2016-10-03 23:30:14 -0700791 /*
792 * Only acpi_dp of type DP_TYPE_TABLE is allowed to be an array.
793 * DP_TYPE_TABLE does not have a value to be written. Thus, start
794 * the loop from next type in the array.
795 */
796 for (dp = array->next; dp; dp = dp->next) {
Duncan Laurieffc99902016-07-02 19:56:06 -0700797 acpi_dp_write_value(dp);
798 (*pkg_count)++;
799 }
800
Duncan Laurie559e9472016-05-10 13:18:17 -0700801 acpigen_pop_len();
Duncan Laurie559e9472016-05-10 13:18:17 -0700802}
803
Duncan Laurieffc99902016-07-02 19:56:06 -0700804static void acpi_dp_free(struct acpi_dp *dp)
Duncan Laurie559e9472016-05-10 13:18:17 -0700805{
Duncan Laurieffc99902016-07-02 19:56:06 -0700806 while (dp) {
807 struct acpi_dp *p = dp->next;
808
809 switch (dp->type) {
810 case ACPI_DP_TYPE_CHILD:
811 acpi_dp_free(dp->child);
812 break;
813 case ACPI_DP_TYPE_ARRAY:
814 acpi_dp_free(dp->array);
815 break;
816 default:
817 break;
818 }
819
820 free(dp);
821 dp = p;
822 }
Duncan Laurie559e9472016-05-10 13:18:17 -0700823}
824
Duncan Laurie84fac412020-06-03 12:36:51 -0700825static bool acpi_dp_write_properties(struct acpi_dp *prop, const char *uuid)
826{
827 struct acpi_dp *dp;
828 char *prop_count = NULL;
829
830 /* Print base properties */
831 for (dp = prop; dp; dp = dp->next) {
832 if (dp->type == ACPI_DP_TYPE_TABLE ||
833 dp->type == ACPI_DP_TYPE_CHILD ||
834 dp->type == ACPI_DP_TYPE_PACKAGE)
835 continue;
836
837 /*
838 * The UUID and package is only added when
839 * we come across the first property. This
840 * is to avoid creating a zero-length package
841 * in situations where there are only children.
842 */
843 if (!prop_count) {
844 /* ToUUID (dp->uuid) */
845 acpigen_write_uuid(uuid);
846 /*
847 * Package (PROP), element count determined as
848 * it is populated
849 */
850 prop_count = acpigen_write_package(0);
851 }
852 (*prop_count)++;
853 acpi_dp_write_property(dp);
854 }
855 if (prop_count) {
856 /* Package (PROP) length, if a package was written */
857 acpigen_pop_len();
858 return true;
859 }
860 return false;
861}
862
Simon Glass8bfa51e2020-06-29 16:18:37 -0600863static void acpi_dp_write_(struct acpi_dp *table)
Duncan Laurie559e9472016-05-10 13:18:17 -0700864{
Duncan Laurieffc99902016-07-02 19:56:06 -0700865 struct acpi_dp *dp, *prop;
Duncan Laurie84fac412020-06-03 12:36:51 -0700866 char *dp_count;
Duncan Laurieffc99902016-07-02 19:56:06 -0700867 int child_count = 0;
868
Duncan Lauriec1adeb62020-04-29 00:04:14 -0700869 if (!table || table->type != ACPI_DP_TYPE_TABLE || !table->next)
Duncan Laurieffc99902016-07-02 19:56:06 -0700870 return;
871
872 /* Name (name) */
873 acpigen_write_name(table->name);
874
875 /* Device Property list starts with the next entry */
876 prop = table->next;
877
Matt Delco08258882019-01-30 11:16:08 -0800878 /* Package (DP), default to assuming no properties or children */
879 dp_count = acpigen_write_package(0);
Duncan Laurieffc99902016-07-02 19:56:06 -0700880
881 /* Print base properties */
Duncan Laurie84fac412020-06-03 12:36:51 -0700882 if (acpi_dp_write_properties(prop, table->uuid))
883 *dp_count += 2;
Duncan Laurieffc99902016-07-02 19:56:06 -0700884
Duncan Laurie84fac412020-06-03 12:36:51 -0700885 /* Count child properties */
886 for (dp = prop; dp; dp = dp->next)
887 if (dp->type == ACPI_DP_TYPE_CHILD)
888 child_count++;
889
890 /* Add child properties to the base table */
Duncan Laurieffc99902016-07-02 19:56:06 -0700891 if (child_count) {
Duncan Laurie84fac412020-06-03 12:36:51 -0700892 /* Update DP package count */
Matt Delco08258882019-01-30 11:16:08 -0800893 *dp_count += 2;
Duncan Laurieffc99902016-07-02 19:56:06 -0700894 /* ToUUID (ACPI_DP_CHILD_UUID) */
895 acpigen_write_uuid(ACPI_DP_CHILD_UUID);
896
897 /* Print child pointer properties */
898 acpigen_write_package(child_count);
899
900 for (dp = prop; dp; dp = dp->next)
901 if (dp->type == ACPI_DP_TYPE_CHILD)
902 acpi_dp_write_property(dp);
Matt Delco08258882019-01-30 11:16:08 -0800903 /* Package (CHILD) length */
Duncan Laurieffc99902016-07-02 19:56:06 -0700904 acpigen_pop_len();
905 }
906
Duncan Laurie84fac412020-06-03 12:36:51 -0700907 /* Write packages of properties with unique UUID */
908 for (dp = prop; dp; dp = dp->next)
909 if (dp->type == ACPI_DP_TYPE_PACKAGE)
910 if (acpi_dp_write_properties(dp->child, dp->uuid))
911 *dp_count += 2;
912
Duncan Laurieffc99902016-07-02 19:56:06 -0700913 /* Package (DP) length */
914 acpigen_pop_len();
915
916 /* Recursively parse children into separate tables */
917 for (dp = prop; dp; dp = dp->next)
918 if (dp->type == ACPI_DP_TYPE_CHILD)
Simon Glass8bfa51e2020-06-29 16:18:37 -0600919 acpi_dp_write_(dp->child);
920}
921
922void acpi_dp_write(struct acpi_dp *table)
923{
924 acpi_dp_write_(table);
Duncan Laurieffc99902016-07-02 19:56:06 -0700925
926 /* Clean up */
927 acpi_dp_free(table);
928}
929
930static struct acpi_dp *acpi_dp_new(struct acpi_dp *dp, enum acpi_dp_type type,
931 const char *name)
932{
933 struct acpi_dp *new;
934
935 new = malloc(sizeof(struct acpi_dp));
936 if (!new)
937 return NULL;
938
939 memset(new, 0, sizeof(*new));
940 new->type = type;
941 new->name = name;
Duncan Laurie84fac412020-06-03 12:36:51 -0700942 new->uuid = ACPI_DP_UUID;
Duncan Laurieffc99902016-07-02 19:56:06 -0700943
944 if (dp) {
945 /* Add to end of property list */
946 while (dp->next)
947 dp = dp->next;
948 dp->next = new;
949 }
950
951 return new;
952}
953
954struct acpi_dp *acpi_dp_new_table(const char *name)
955{
956 return acpi_dp_new(NULL, ACPI_DP_TYPE_TABLE, name);
957}
958
Duncan Laurieb3023b62017-08-29 08:26:50 -0700959size_t acpi_dp_add_property_list(struct acpi_dp *dp,
960 const struct acpi_dp *property_list,
961 size_t property_count)
962{
963 const struct acpi_dp *prop;
964 size_t i, properties_added = 0;
965
Jacob Garberc30e5902019-05-23 14:34:58 -0600966 if (!dp || !property_list)
967 return 0;
968
Duncan Laurieb3023b62017-08-29 08:26:50 -0700969 for (i = 0; i < property_count; i++) {
970 prop = &property_list[i];
971
972 if (prop->type == ACPI_DP_TYPE_UNKNOWN || !prop->name)
973 continue;
974
975 switch (prop->type) {
976 case ACPI_DP_TYPE_INTEGER:
977 acpi_dp_add_integer(dp, prop->name, prop->integer);
978 break;
979 case ACPI_DP_TYPE_STRING:
980 acpi_dp_add_string(dp, prop->name, prop->string);
981 break;
982 case ACPI_DP_TYPE_REFERENCE:
983 acpi_dp_add_reference(dp, prop->name, prop->string);
984 break;
985 case ACPI_DP_TYPE_ARRAY:
986 acpi_dp_add_array(dp, prop->array);
987 break;
988 case ACPI_DP_TYPE_CHILD:
989 acpi_dp_add_child(dp, prop->name, prop->child);
990 break;
991 default:
992 continue;
993 }
994
995 ++properties_added;
996 }
997
998 return properties_added;
999}
1000
Duncan Laurieffc99902016-07-02 19:56:06 -07001001struct acpi_dp *acpi_dp_add_integer(struct acpi_dp *dp, const char *name,
1002 uint64_t value)
1003{
Jacob Garberc30e5902019-05-23 14:34:58 -06001004 if (!dp)
1005 return NULL;
1006
Duncan Laurieffc99902016-07-02 19:56:06 -07001007 struct acpi_dp *new = acpi_dp_new(dp, ACPI_DP_TYPE_INTEGER, name);
1008
1009 if (new)
1010 new->integer = value;
1011
1012 return new;
1013}
1014
1015struct acpi_dp *acpi_dp_add_string(struct acpi_dp *dp, const char *name,
1016 const char *string)
1017{
Jacob Garberc30e5902019-05-23 14:34:58 -06001018 if (!dp)
1019 return NULL;
1020
Duncan Laurieffc99902016-07-02 19:56:06 -07001021 struct acpi_dp *new = acpi_dp_new(dp, ACPI_DP_TYPE_STRING, name);
1022
1023 if (new)
1024 new->string = string;
1025
1026 return new;
1027}
1028
1029struct acpi_dp *acpi_dp_add_reference(struct acpi_dp *dp, const char *name,
1030 const char *reference)
1031{
Jacob Garberc30e5902019-05-23 14:34:58 -06001032 if (!dp)
1033 return NULL;
1034
Duncan Laurieffc99902016-07-02 19:56:06 -07001035 struct acpi_dp *new = acpi_dp_new(dp, ACPI_DP_TYPE_REFERENCE, name);
1036
1037 if (new)
1038 new->string = reference;
1039
1040 return new;
1041}
1042
1043struct acpi_dp *acpi_dp_add_child(struct acpi_dp *dp, const char *name,
1044 struct acpi_dp *child)
1045{
1046 struct acpi_dp *new;
1047
Jacob Garberc30e5902019-05-23 14:34:58 -06001048 if (!dp || !child || child->type != ACPI_DP_TYPE_TABLE)
Duncan Laurieffc99902016-07-02 19:56:06 -07001049 return NULL;
1050
1051 new = acpi_dp_new(dp, ACPI_DP_TYPE_CHILD, name);
1052 if (new) {
1053 new->child = child;
1054 new->string = child->name;
1055 }
1056
1057 return new;
1058}
1059
Duncan Laurie84fac412020-06-03 12:36:51 -07001060struct acpi_dp *acpi_dp_add_package(struct acpi_dp *dp, struct acpi_dp *package)
1061{
1062 struct acpi_dp *new;
1063
1064 if (!dp || !package || package->type != ACPI_DP_TYPE_TABLE)
1065 return NULL;
1066
1067 new = acpi_dp_new(dp, ACPI_DP_TYPE_PACKAGE, NULL);
1068 if (new) {
1069 new->uuid = package->name;
1070 new->child = package;
1071 }
1072
1073 return new;
1074}
1075
Duncan Laurieffc99902016-07-02 19:56:06 -07001076struct acpi_dp *acpi_dp_add_array(struct acpi_dp *dp, struct acpi_dp *array)
1077{
1078 struct acpi_dp *new;
1079
Jacob Garberc30e5902019-05-23 14:34:58 -06001080 if (!dp || !array || array->type != ACPI_DP_TYPE_TABLE)
Duncan Laurieffc99902016-07-02 19:56:06 -07001081 return NULL;
1082
1083 new = acpi_dp_new(dp, ACPI_DP_TYPE_ARRAY, array->name);
1084 if (new)
1085 new->array = array;
1086
1087 return new;
1088}
1089
1090struct acpi_dp *acpi_dp_add_integer_array(struct acpi_dp *dp, const char *name,
Duncan Laurieed6eb272020-04-29 11:39:08 -07001091 const uint64_t *array, int len)
Duncan Laurieffc99902016-07-02 19:56:06 -07001092{
1093 struct acpi_dp *dp_array;
1094 int i;
1095
Jacob Garberc30e5902019-05-23 14:34:58 -06001096 if (!dp || len <= 0)
Duncan Laurieffc99902016-07-02 19:56:06 -07001097 return NULL;
1098
1099 dp_array = acpi_dp_new_table(name);
1100 if (!dp_array)
1101 return NULL;
1102
1103 for (i = 0; i < len; i++)
1104 if (!acpi_dp_add_integer(dp_array, NULL, array[i]))
1105 break;
1106
1107 acpi_dp_add_array(dp, dp_array);
1108
1109 return dp_array;
1110}
1111
Karthikeyan Ramasubramanian685dbe12020-10-05 10:44:50 -06001112struct acpi_dp *acpi_dp_add_gpio_array(struct acpi_dp *dp, const char *name,
1113 const struct acpi_gpio_res_params *params,
1114 size_t param_count)
1115{
1116 struct acpi_dp *gpio;
1117 uint32_t i;
1118
1119 if (!dp || !param_count)
1120 return NULL;
1121
1122 gpio = acpi_dp_new_table(name);
1123 if (!gpio)
1124 return NULL;
1125
1126 /*
1127 * Generate ACPI identifiers as follows:
1128 * Package () {
1129 * name, // e.g. cs-gpios
1130 * Package() {
1131 * ref, index, pin, active_low, // GPIO-0 (params[0])
1132 * ref, index, pin, active_low, // GPIO-1 (params[1])
1133 * ...
1134 * }
1135 * }
1136 */
1137 for (i = 0; i < param_count; i++, params++) {
1138 /*
1139 * If refs is NULL, leave a hole in the gpio array. This can be used in
1140 * conditions where some controllers use both GPIOs and native signals.
1141 */
1142 if (!params->ref) {
1143 acpi_dp_add_integer(gpio, NULL, 0);
1144 continue;
1145 }
1146
1147 /* The device that has _CRS containing GpioIO()/GpioInt() */
1148 acpi_dp_add_reference(gpio, NULL, params->ref);
1149
1150 /* Index of the GPIO resource in _CRS starting from zero */
1151 acpi_dp_add_integer(gpio, NULL, params->index);
1152
1153 /* Pin in the GPIO resource, typically zero */
1154 acpi_dp_add_integer(gpio, NULL, params->pin);
1155
1156 /* Set if pin is active low */
1157 acpi_dp_add_integer(gpio, NULL, params->active_low);
1158 }
1159 acpi_dp_add_array(dp, gpio);
1160
1161 return gpio;
1162
1163}
1164
1165
Duncan Laurieffc99902016-07-02 19:56:06 -07001166struct acpi_dp *acpi_dp_add_gpio(struct acpi_dp *dp, const char *name,
1167 const char *ref, int index, int pin,
1168 int active_low)
1169{
Karthikeyan Ramasubramanian685dbe12020-10-05 10:44:50 -06001170 struct acpi_gpio_res_params param = {
1171 .ref = ref,
1172 .index = index,
1173 .pin = pin,
1174 .active_low = active_low,
1175 };
Jacob Garberc30e5902019-05-23 14:34:58 -06001176
Karthikeyan Ramasubramanian685dbe12020-10-05 10:44:50 -06001177 return acpi_dp_add_gpio_array(dp, name, &param, 1);
Duncan Laurie559e9472016-05-10 13:18:17 -07001178}
Furquan Shaikhd1130af2020-04-23 12:51:42 -07001179
1180/*
1181 * This function writes a PCI device with _ADR object:
1182 * Example:
1183 * Scope (\_SB.PCI0)
1184 * {
1185 * Device (IGFX)
1186 * {
1187 * Name (_ADR, 0x0000000000000000)
1188 * Method (_STA, 0, NotSerialized) { Return (status) }
1189 * }
1190 * }
1191 */
Furquan Shaikh7536a392020-04-24 21:59:21 -07001192void acpi_device_write_pci_dev(const struct device *dev)
Furquan Shaikhd1130af2020-04-23 12:51:42 -07001193{
1194 const char *scope = acpi_device_scope(dev);
1195 const char *name = acpi_device_name(dev);
1196
1197 assert(dev->path.type == DEVICE_PATH_PCI);
1198 assert(name);
1199 assert(scope);
1200
1201 acpigen_write_scope(scope);
1202 acpigen_write_device(name);
1203
1204 acpigen_write_ADR_pci_device(dev);
1205 acpigen_write_STA(acpi_device_status(dev));
1206
1207 acpigen_pop_len(); /* Device */
1208 acpigen_pop_len(); /* Scope */
1209}
Kapil Porwalddc52a62022-11-26 19:10:57 +05301210
Kapil Porwal75436272022-11-28 17:25:48 +05301211/*
1212 * Helper function to add given integer property with an UUID to _DSD in the current scope.
1213 *
1214 * dsd - Pointer to a _DSD object.
1215 * Append to existing _DSD object if not NULL.
1216 * Create new _DSD object and flush it if NULL.
1217 * uuid - Pointer to the UUID string.
1218 * name - Pointer to the property name string.
1219 * value - Value of the integer property.
1220 */
1221static void acpi_device_add_integer_property_with_uuid(struct acpi_dp *dsd,
1222 const char *uuid,
1223 const char *name,
1224 uint64_t value)
Kapil Porwalddc52a62022-11-26 19:10:57 +05301225{
1226 struct acpi_dp *prev_dsd = dsd, *pkg;
1227 if (prev_dsd == NULL)
1228 dsd = acpi_dp_new_table("_DSD");
Kapil Porwal75436272022-11-28 17:25:48 +05301229 pkg = acpi_dp_new_table(uuid);
1230 acpi_dp_add_integer(pkg, name, value);
Kapil Porwalddc52a62022-11-26 19:10:57 +05301231 acpi_dp_add_package(dsd, pkg);
1232 if (prev_dsd == NULL)
1233 acpi_dp_write(dsd);
1234}
Kapil Porwal75436272022-11-28 17:25:48 +05301235
1236/* _DSD with ExternalFacingPort */
1237void acpi_device_add_external_facing_port(struct acpi_dp *dsd)
1238{
1239 acpi_device_add_integer_property_with_uuid(dsd,
1240 ACPI_DSD_EXTERNAL_FACING_PORT_UUID,
1241 ACPI_DSD_EXTERNAL_FACING_PORT_NAME,
1242 1);
1243}
1244
1245/* _DSD with HotPlugSupportInD3 */
1246void acpi_device_add_hotplug_support_in_d3(struct acpi_dp *dsd)
1247{
1248 acpi_device_add_integer_property_with_uuid(dsd,
1249 ACPI_DSD_HOTPLUG_IN_D3_UUID,
1250 ACPI_DSD_HOTPLUG_IN_D3_NAME,
1251 1);
1252}
1253
1254/* _DSD with DmaProperty */
1255void acpi_device_add_dma_property(struct acpi_dp *dsd)
1256{
1257 acpi_device_add_integer_property_with_uuid(dsd,
1258 ACPI_DSD_DMA_PROPERTY_UUID,
1259 ACPI_DSD_DMA_PROPERTY_NAME,
1260 1);
1261}
1262
1263/* _DSD with StorageD3Enable */
1264void acpi_device_add_storage_d3_enable(struct acpi_dp *dsd)
1265{
1266 acpi_device_add_integer_property_with_uuid(dsd,
1267 ACPI_DSD_STORAGE_D3_UUID,
1268 ACPI_DSD_STORAGE_D3_NAME,
1269 1);
1270}