blob: af3d262bc7e111ec4514f333a7383d9653d9f229 [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>
Elyes HAOUAS70a03dd2019-12-02 20:47:50 +010010#include <stdlib.h>
Elyes Haouasbdd03c22024-05-27 11:20:07 +020011#include <stdio.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
Jianeng Ceng01344bc2024-04-09 21:20:20 +080043/* Write GPIO descriptor of DSD property */
44int acpi_device_write_dsd_gpio(struct acpi_gpio *gpio, int *curr_index)
45{
46 int ret = -1;
47
48 if (!gpio || !curr_index)
49 return ret;
50
51 if (gpio->pin_count == 0)
52 return ret;
53
54 acpi_device_write_gpio(gpio);
Jianeng Ceng05ee5c22024-04-25 17:24:03 +080055 ret = (*curr_index)++;
Jianeng Ceng01344bc2024-04-09 21:20:20 +080056
57 return ret;
58}
59
Duncan Laurie6b7c1f62016-05-09 15:38:36 -070060/* Write empty word value and return pointer to it */
61static void *acpi_device_write_zero_len(void)
62{
63 char *p = acpigen_get_current();
64 acpigen_emit_word(0);
65 return p;
66}
67
68/* Fill in length value from start to current at specified location */
69static void acpi_device_fill_from_len(char *ptr, char *start)
70{
71 uint16_t len = acpigen_get_current() - start;
72 ptr[0] = len & 0xff;
73 ptr[1] = (len >> 8) & 0xff;
74}
75
76/*
77 * Fill in the length field with the value calculated from after
78 * the 16bit field to acpigen current as this length value does
79 * not include the length field itself.
80 */
81static void acpi_device_fill_len(void *ptr)
82{
83 acpi_device_fill_from_len(ptr, ptr + sizeof(uint16_t));
84}
85
Duncan Lauried9af3ce2016-05-08 18:15:25 -070086/* Locate and return the ACPI name for this device */
Aamir Bohrae825d3f2019-07-30 10:11:41 +053087const char *acpi_device_name(const struct device *dev)
Duncan Lauried9af3ce2016-05-08 18:15:25 -070088{
Aamir Bohrae825d3f2019-07-30 10:11:41 +053089 const struct device *pdev = dev;
Duncan Laurie47029142018-05-07 14:28:53 -070090 const char *name = NULL;
91
Duncan Lauried9af3ce2016-05-08 18:15:25 -070092 if (!dev)
93 return NULL;
94
95 /* Check for device specific handler */
CoolStar42be8992023-08-24 02:00:50 -070096 if (dev->ops && dev->ops->acpi_name) {
97 name = dev->ops->acpi_name(dev);
98 if (name)
99 return name;
100 }
Duncan Lauried9af3ce2016-05-08 18:15:25 -0700101
Duncan Laurie47029142018-05-07 14:28:53 -0700102 /* Walk up the tree to find if any parent can identify this device */
Arthur Heymans7fcd4d52023-08-24 15:12:19 +0200103 while (pdev->upstream) {
104 pdev = pdev->upstream->dev;
Duncan Laurie47029142018-05-07 14:28:53 -0700105 if (!pdev)
106 break;
Patrick Rudolphf95dbce2024-01-22 15:39:46 +0100107 if (is_root_device(pdev))
Duncan Laurie47029142018-05-07 14:28:53 -0700108 break;
109 if (pdev->ops && pdev->ops->acpi_name)
110 name = pdev->ops->acpi_name(dev);
111 if (name)
112 return name;
113 }
Duncan Lauried9af3ce2016-05-08 18:15:25 -0700114
115 return NULL;
116}
117
Patrick Rudolpheeb8e742019-08-20 08:20:01 +0200118/* Locate and return the ACPI _HID (Hardware ID) for this device */
119const char *acpi_device_hid(const struct device *dev)
120{
121 if (!dev)
122 return NULL;
123
124 /* Check for device specific handler */
125 if (dev->ops->acpi_hid)
126 return dev->ops->acpi_hid(dev);
127
128 /*
129 * Don't walk up the tree to find any parent that can identify this device, as
130 * PNP devices are hard to identify.
131 */
132
133 return NULL;
134}
135
Patrick Rudolphc83bab62019-12-13 12:16:06 +0100136/*
137 * Generate unique ID based on the ACPI path.
138 * Collisions on the same _HID are possible but very unlikely.
139 */
Furquan Shaikhd14d03a2020-04-24 21:27:29 -0700140uint32_t acpi_device_uid(const struct device *dev)
Patrick Rudolphc83bab62019-12-13 12:16:06 +0100141{
142 const char *path = acpi_device_path(dev);
143 if (!path)
144 return 0;
145
146 return CRC(path, strlen(path), crc32_byte);
147}
148
Duncan Lauried9af3ce2016-05-08 18:15:25 -0700149/* Recursive function to find the root device and print a path from there */
Aamir Bohrae825d3f2019-07-30 10:11:41 +0530150static ssize_t acpi_device_path_fill(const struct device *dev, char *buf,
Duncan Laurie47029142018-05-07 14:28:53 -0700151 size_t buf_len, size_t cur)
Duncan Lauried9af3ce2016-05-08 18:15:25 -0700152{
153 const char *name = acpi_device_name(dev);
Duncan Laurie47029142018-05-07 14:28:53 -0700154 ssize_t next = 0;
155
156 if (!name)
157 return -1;
Duncan Lauried9af3ce2016-05-08 18:15:25 -0700158
159 /*
160 * Make sure this name segment will fit, including the path segment
161 * separator and possible NUL terminator if this is the last segment.
162 */
Duncan Laurie47029142018-05-07 14:28:53 -0700163 if (!dev || (cur + strlen(name) + 2) > buf_len)
Duncan Lauried9af3ce2016-05-08 18:15:25 -0700164 return cur;
165
166 /* Walk up the tree to the root device */
Arthur Heymans7fcd4d52023-08-24 15:12:19 +0200167 if (!is_root_device(dev) && dev->upstream && dev->upstream->dev)
168 next = acpi_device_path_fill(dev->upstream->dev, buf, buf_len, cur);
Duncan Laurie47029142018-05-07 14:28:53 -0700169 if (next < 0)
170 return next;
Duncan Lauried9af3ce2016-05-08 18:15:25 -0700171
172 /* Fill in the path from the root device */
173 next += snprintf(buf + next, buf_len - next, "%s%s",
Patrick Rudolphf95dbce2024-01-22 15:39:46 +0100174 (is_root_device(dev) || (strlen(name) == 0)) ?
175 "" : ".", name);
Duncan Lauried9af3ce2016-05-08 18:15:25 -0700176
177 return next;
178}
179
180/*
181 * Warning: just as with dev_path() this uses a static buffer
Martin Roth0949e732021-10-01 14:28:22 -0600182 * so should not be called multiple times in one statement
Duncan Lauried9af3ce2016-05-08 18:15:25 -0700183 */
Aamir Bohrae825d3f2019-07-30 10:11:41 +0530184const char *acpi_device_path(const struct device *dev)
Duncan Lauried9af3ce2016-05-08 18:15:25 -0700185{
186 static char buf[DEVICE_PATH_MAX] = {};
187
188 if (!dev)
189 return NULL;
190
191 if (acpi_device_path_fill(dev, buf, sizeof(buf), 0) <= 0)
192 return NULL;
193
194 return buf;
195}
196
197/* Return the path of the parent device as the ACPI Scope for this device */
Aamir Bohrae825d3f2019-07-30 10:11:41 +0530198const char *acpi_device_scope(const struct device *dev)
Duncan Lauried9af3ce2016-05-08 18:15:25 -0700199{
Duncan Lauried02685b2016-06-30 14:37:37 -0700200 static char buf[DEVICE_PATH_MAX] = {};
201
Arthur Heymans7fcd4d52023-08-24 15:12:19 +0200202 if (!dev || !dev->upstream || !dev->upstream->dev)
Duncan Lauried9af3ce2016-05-08 18:15:25 -0700203 return NULL;
204
Arthur Heymans7fcd4d52023-08-24 15:12:19 +0200205 if (acpi_device_path_fill(dev->upstream->dev, buf, sizeof(buf), 0) <= 0)
Duncan Lauried02685b2016-06-30 14:37:37 -0700206 return NULL;
207
208 return buf;
Duncan Lauried9af3ce2016-05-08 18:15:25 -0700209}
210
Elyes HAOUAS6716bab2020-01-09 21:29:25 +0100211/* Concatenate the device path and provided name suffix */
Aamir Bohrae825d3f2019-07-30 10:11:41 +0530212const char *acpi_device_path_join(const struct device *dev, const char *name)
Duncan Lauried9af3ce2016-05-08 18:15:25 -0700213{
214 static char buf[DEVICE_PATH_MAX] = {};
Jacob Garberf2ba2d92019-07-02 10:50:10 -0600215 ssize_t len;
Duncan Lauried9af3ce2016-05-08 18:15:25 -0700216
217 if (!dev)
218 return NULL;
219
220 /* Build the path of this device */
221 len = acpi_device_path_fill(dev, buf, sizeof(buf), 0);
222 if (len <= 0)
223 return NULL;
224
225 /* Ensure there is room for the added name, separator, and NUL */
226 if ((len + strlen(name) + 2) > sizeof(buf))
227 return NULL;
228 snprintf(buf + len, sizeof(buf) - len, ".%s", name);
229
230 return buf;
231}
Duncan Laurie6b7c1f62016-05-09 15:38:36 -0700232
Hung-Te Linb4be50c2018-09-10 10:55:49 +0800233int acpi_device_status(const struct device *dev)
234{
235 if (!dev->enabled)
236 return ACPI_STATUS_DEVICE_ALL_OFF;
237 if (dev->hidden)
238 return ACPI_STATUS_DEVICE_HIDDEN_ON;
239 return ACPI_STATUS_DEVICE_ALL_ON;
240}
241
Patrick Rudolphc83bab62019-12-13 12:16:06 +0100242/* Write the unique _UID based on ACPI device path. */
Furquan Shaikhd14d03a2020-04-24 21:27:29 -0700243void acpi_device_write_uid(const struct device *dev)
Patrick Rudolphc83bab62019-12-13 12:16:06 +0100244{
245 acpigen_write_name_integer("_UID", acpi_device_uid(dev));
246}
247
Duncan Laurie6b7c1f62016-05-09 15:38:36 -0700248/* ACPI 6.1 section 6.4.3.6: Extended Interrupt Descriptor */
249void acpi_device_write_interrupt(const struct acpi_irq *irq)
250{
251 void *desc_length;
252 uint8_t flags;
253
254 if (!irq || !irq->pin)
255 return;
256
257 /* This is supported by GpioInt() but not Interrupt() */
Furquan Shaikh5b9b5932017-02-21 13:16:30 -0800258 if (irq->polarity == ACPI_IRQ_ACTIVE_BOTH)
Duncan Laurie6b7c1f62016-05-09 15:38:36 -0700259 return;
260
261 /* Byte 0: Descriptor Type */
262 acpigen_emit_byte(ACPI_DESCRIPTOR_INTERRUPT);
263
264 /* Byte 1-2: Length (filled in later) */
265 desc_length = acpi_device_write_zero_len();
266
267 /*
268 * Byte 3: Flags
269 * [7:5]: Reserved
270 * [4]: Wake (0=NO_WAKE 1=WAKE)
271 * [3]: Sharing (0=EXCLUSIVE 1=SHARED)
272 * [2]: Polarity (0=HIGH 1=LOW)
273 * [1]: Mode (0=LEVEL 1=EDGE)
274 * [0]: Resource (0=PRODUCER 1=CONSUMER)
275 */
276 flags = 1 << 0; /* ResourceConsumer */
Furquan Shaikh5b9b5932017-02-21 13:16:30 -0800277 if (irq->mode == ACPI_IRQ_EDGE_TRIGGERED)
Duncan Laurie6b7c1f62016-05-09 15:38:36 -0700278 flags |= 1 << 1;
Furquan Shaikh5b9b5932017-02-21 13:16:30 -0800279 if (irq->polarity == ACPI_IRQ_ACTIVE_LOW)
Duncan Laurie6b7c1f62016-05-09 15:38:36 -0700280 flags |= 1 << 2;
Furquan Shaikh5b9b5932017-02-21 13:16:30 -0800281 if (irq->shared == ACPI_IRQ_SHARED)
Duncan Laurie6b7c1f62016-05-09 15:38:36 -0700282 flags |= 1 << 3;
Furquan Shaikh5b9b5932017-02-21 13:16:30 -0800283 if (irq->wake == ACPI_IRQ_WAKE)
Duncan Laurie6b7c1f62016-05-09 15:38:36 -0700284 flags |= 1 << 4;
285 acpigen_emit_byte(flags);
286
287 /* Byte 4: Interrupt Table Entry Count */
288 acpigen_emit_byte(1);
289
290 /* Byte 5-8: Interrupt Number */
291 acpigen_emit_dword(irq->pin);
292
293 /* Fill in Descriptor Length (account for len word) */
294 acpi_device_fill_len(desc_length);
295}
Duncan Lauriecfb6ea72016-05-09 17:08:38 -0700296
297/* ACPI 6.1 section 6.4.3.8.1 - GPIO Interrupt or I/O */
298void acpi_device_write_gpio(const struct acpi_gpio *gpio)
299{
300 void *start, *desc_length;
301 void *pin_table_offset, *vendor_data_offset, *resource_offset;
302 uint16_t flags = 0;
303 int pin;
304
305 if (!gpio || gpio->type > ACPI_GPIO_TYPE_IO)
306 return;
307
308 start = acpigen_get_current();
309
310 /* Byte 0: Descriptor Type */
311 acpigen_emit_byte(ACPI_DESCRIPTOR_GPIO);
312
313 /* Byte 1-2: Length (fill in later) */
314 desc_length = acpi_device_write_zero_len();
315
316 /* Byte 3: Revision ID */
317 acpigen_emit_byte(ACPI_GPIO_REVISION_ID);
318
319 /* Byte 4: GpioIo or GpioInt */
320 acpigen_emit_byte(gpio->type);
321
322 /*
323 * Byte 5-6: General Flags
324 * [15:1]: 0 => Reserved
325 * [0]: 1 => ResourceConsumer
326 */
327 acpigen_emit_word(1 << 0);
328
329 switch (gpio->type) {
330 case ACPI_GPIO_TYPE_INTERRUPT:
331 /*
332 * Byte 7-8: GPIO Interrupt Flags
333 * [15:5]: 0 => Reserved
334 * [4]: Wake (0=NO_WAKE 1=WAKE)
335 * [3]: Sharing (0=EXCLUSIVE 1=SHARED)
336 * [2:1]: Polarity (0=HIGH 1=LOW 2=BOTH)
337 * [0]: Mode (0=LEVEL 1=EDGE)
338 */
Furquan Shaikh5b9b5932017-02-21 13:16:30 -0800339 if (gpio->irq.mode == ACPI_IRQ_EDGE_TRIGGERED)
Duncan Lauriecfb6ea72016-05-09 17:08:38 -0700340 flags |= 1 << 0;
Furquan Shaikh5b9b5932017-02-21 13:16:30 -0800341 if (gpio->irq.shared == ACPI_IRQ_SHARED)
Duncan Lauriecfb6ea72016-05-09 17:08:38 -0700342 flags |= 1 << 3;
Furquan Shaikh5b9b5932017-02-21 13:16:30 -0800343 if (gpio->irq.wake == ACPI_IRQ_WAKE)
Duncan Lauriecfb6ea72016-05-09 17:08:38 -0700344 flags |= 1 << 4;
345
346 switch (gpio->irq.polarity) {
Furquan Shaikh5b9b5932017-02-21 13:16:30 -0800347 case ACPI_IRQ_ACTIVE_HIGH:
Duncan Lauriecfb6ea72016-05-09 17:08:38 -0700348 flags |= 0 << 1;
349 break;
Furquan Shaikh5b9b5932017-02-21 13:16:30 -0800350 case ACPI_IRQ_ACTIVE_LOW:
Duncan Lauriecfb6ea72016-05-09 17:08:38 -0700351 flags |= 1 << 1;
352 break;
Furquan Shaikh5b9b5932017-02-21 13:16:30 -0800353 case ACPI_IRQ_ACTIVE_BOTH:
Duncan Lauriecfb6ea72016-05-09 17:08:38 -0700354 flags |= 2 << 1;
355 break;
356 }
357 break;
358
359 case ACPI_GPIO_TYPE_IO:
360 /*
361 * Byte 7-8: GPIO IO Flags
362 * [15:4]: 0 => Reserved
363 * [3]: Sharing (0=EXCLUSIVE 1=SHARED)
364 * [2]: 0 => Reserved
365 * [1:0]: IO Restriction
366 * 0 => IoRestrictionNone
367 * 1 => IoRestrictionInputOnly
368 * 2 => IoRestrictionOutputOnly
369 * 3 => IoRestrictionNoneAndPreserve
370 */
371 flags |= gpio->io_restrict & 3;
372 if (gpio->io_shared)
373 flags |= 1 << 3;
374 break;
375 }
376 acpigen_emit_word(flags);
377
378 /*
379 * Byte 9: Pin Configuration
380 * 0x01 => Default (no configuration applied)
381 * 0x02 => Pull-up
382 * 0x03 => Pull-down
383 * 0x04-0x7F => Reserved
384 * 0x80-0xff => Vendor defined
385 */
386 acpigen_emit_byte(gpio->pull);
387
388 /* Byte 10-11: Output Drive Strength in 1/100 mA */
389 acpigen_emit_word(gpio->output_drive_strength);
390
391 /* Byte 12-13: Debounce Timeout in 1/100 ms */
392 acpigen_emit_word(gpio->interrupt_debounce_timeout);
393
394 /* Byte 14-15: Pin Table Offset, relative to start */
395 pin_table_offset = acpi_device_write_zero_len();
396
397 /* Byte 16: Reserved */
398 acpigen_emit_byte(0);
399
400 /* Byte 17-18: Resource Source Name Offset, relative to start */
401 resource_offset = acpi_device_write_zero_len();
402
403 /* Byte 19-20: Vendor Data Offset, relative to start */
404 vendor_data_offset = acpi_device_write_zero_len();
405
406 /* Byte 21-22: Vendor Data Length */
407 acpigen_emit_word(0);
408
409 /* Fill in Pin Table Offset */
410 acpi_device_fill_from_len(pin_table_offset, start);
411
412 /* Pin Table, one word for each pin */
Duncan Laurie5b6c28c2016-06-29 10:47:22 -0700413 for (pin = 0; pin < gpio->pin_count; pin++) {
414 uint16_t acpi_pin = gpio->pins[pin];
Julius Wernercd49cce2019-03-05 16:53:33 -0800415#if CONFIG(GENERIC_GPIO_LIB)
Duncan Laurie5b6c28c2016-06-29 10:47:22 -0700416 acpi_pin = gpio_acpi_pin(acpi_pin);
417#endif
418 acpigen_emit_word(acpi_pin);
419 }
Duncan Lauriecfb6ea72016-05-09 17:08:38 -0700420
421 /* Fill in Resource Source Name Offset */
422 acpi_device_fill_from_len(resource_offset, start);
423
424 /* Resource Source Name String */
Julius Wernercd49cce2019-03-05 16:53:33 -0800425#if CONFIG(GENERIC_GPIO_LIB)
Duncan Lauriecfb6ea72016-05-09 17:08:38 -0700426 acpigen_emit_string(gpio->resource ? : gpio_acpi_path(gpio->pins[0]));
427#else
428 acpigen_emit_string(gpio->resource);
429#endif
430
431 /* Fill in Vendor Data Offset */
432 acpi_device_fill_from_len(vendor_data_offset, start);
433
434 /* Fill in GPIO Descriptor Length (account for len word) */
435 acpi_device_fill_len(desc_length);
436}
Duncan Laurie1010b4a2016-05-09 20:10:47 -0700437
438/* ACPI 6.1 section 6.4.3.8.2.1 - I2cSerialBus() */
439void acpi_device_write_i2c(const struct acpi_i2c *i2c)
440{
441 void *desc_length, *type_length;
442
443 /* Byte 0: Descriptor Type */
444 acpigen_emit_byte(ACPI_DESCRIPTOR_SERIAL_BUS);
445
446 /* Byte 1+2: Length (filled in later) */
447 desc_length = acpi_device_write_zero_len();
448
449 /* Byte 3: Revision ID */
Elyes HAOUAS34564ed2019-04-16 08:12:10 +0200450 acpigen_emit_byte(ACPI_I2C_SERIAL_BUS_REVISION_ID);
Duncan Laurie1010b4a2016-05-09 20:10:47 -0700451
452 /* Byte 4: Resource Source Index is Reserved */
453 acpigen_emit_byte(0);
454
455 /* Byte 5: Serial Bus Type is I2C */
456 acpigen_emit_byte(ACPI_SERIAL_BUS_TYPE_I2C);
457
458 /*
459 * Byte 6: Flags
460 * [7:2]: 0 => Reserved
461 * [1]: 1 => ResourceConsumer
462 * [0]: 0 => ControllerInitiated
463 */
464 acpigen_emit_byte(1 << 1);
465
466 /*
467 * Byte 7-8: Type Specific Flags
468 * [15:1]: 0 => Reserved
469 * [0]: 0 => 7bit, 1 => 10bit
470 */
471 acpigen_emit_word(i2c->mode_10bit);
472
473 /* Byte 9: Type Specific Revision ID */
Elyes HAOUAS34564ed2019-04-16 08:12:10 +0200474 acpigen_emit_byte(ACPI_I2C_TYPE_SPECIFIC_REVISION_ID);
Duncan Laurie1010b4a2016-05-09 20:10:47 -0700475
476 /* Byte 10-11: I2C Type Data Length */
477 type_length = acpi_device_write_zero_len();
478
479 /* Byte 12-15: I2C Bus Speed */
480 acpigen_emit_dword(i2c->speed);
481
482 /* Byte 16-17: I2C Slave Address */
483 acpigen_emit_word(i2c->address);
484
485 /* Fill in Type Data Length */
486 acpi_device_fill_len(type_length);
487
488 /* Byte 18+: ResourceSource */
489 acpigen_emit_string(i2c->resource);
490
491 /* Fill in I2C Descriptor Length */
492 acpi_device_fill_len(desc_length);
493}
Duncan Laurie70c86d92016-05-10 07:26:34 -0700494
495/* ACPI 6.1 section 6.4.3.8.2.2 - SpiSerialBus() */
496void acpi_device_write_spi(const struct acpi_spi *spi)
497{
498 void *desc_length, *type_length;
499 uint16_t flags = 0;
500
501 /* Byte 0: Descriptor Type */
502 acpigen_emit_byte(ACPI_DESCRIPTOR_SERIAL_BUS);
503
504 /* Byte 1+2: Length (filled in later) */
505 desc_length = acpi_device_write_zero_len();
506
507 /* Byte 3: Revision ID */
Elyes HAOUAS34564ed2019-04-16 08:12:10 +0200508 acpigen_emit_byte(ACPI_SPI_SERIAL_BUS_REVISION_ID);
Duncan Laurie70c86d92016-05-10 07:26:34 -0700509
510 /* Byte 4: Resource Source Index is Reserved */
511 acpigen_emit_byte(0);
512
513 /* Byte 5: Serial Bus Type is SPI */
514 acpigen_emit_byte(ACPI_SERIAL_BUS_TYPE_SPI);
515
516 /*
517 * Byte 6: Flags
518 * [7:2]: 0 => Reserved
519 * [1]: 1 => ResourceConsumer
520 * [0]: 0 => ControllerInitiated
521 */
522 acpigen_emit_byte(1 << 1);
523
524 /*
525 * Byte 7-8: Type Specific Flags
526 * [15:2]: 0 => Reserved
527 * [1]: 0 => ActiveLow, 1 => ActiveHigh
528 * [0]: 0 => FourWire, 1 => ThreeWire
529 */
530 if (spi->wire_mode == SPI_3_WIRE_MODE)
531 flags |= 1 << 0;
532 if (spi->device_select_polarity == SPI_POLARITY_HIGH)
533 flags |= 1 << 1;
534 acpigen_emit_word(flags);
535
536 /* Byte 9: Type Specific Revision ID */
Elyes HAOUAS34564ed2019-04-16 08:12:10 +0200537 acpigen_emit_byte(ACPI_SPI_TYPE_SPECIFIC_REVISION_ID);
Duncan Laurie70c86d92016-05-10 07:26:34 -0700538
539 /* Byte 10-11: SPI Type Data Length */
540 type_length = acpi_device_write_zero_len();
541
542 /* Byte 12-15: Connection Speed */
543 acpigen_emit_dword(spi->speed);
544
545 /* Byte 16: Data Bit Length */
546 acpigen_emit_byte(spi->data_bit_length);
547
548 /* Byte 17: Clock Phase */
549 acpigen_emit_byte(spi->clock_phase);
550
551 /* Byte 18: Clock Polarity */
552 acpigen_emit_byte(spi->clock_polarity);
553
554 /* Byte 19-20: Device Selection */
555 acpigen_emit_word(spi->device_select);
556
557 /* Fill in Type Data Length */
558 acpi_device_fill_len(type_length);
559
560 /* Byte 21+: ResourceSource String */
561 acpigen_emit_string(spi->resource);
562
563 /* Fill in SPI Descriptor Length */
564 acpi_device_fill_len(desc_length);
565}
Duncan Laurie559e9472016-05-10 13:18:17 -0700566
Duncan Lauriedccef0d2020-05-27 12:29:51 -0700567/* UART Serial Bus - UARTSerialBusV2() */
568void acpi_device_write_uart(const struct acpi_uart *uart)
569{
570 void *desc_length, *type_length;
571 uint16_t flags;
572
573 /* Byte 0: Descriptor Type */
574 acpigen_emit_byte(ACPI_DESCRIPTOR_SERIAL_BUS);
575
576 /* Byte 1+2: Length (filled in later) */
577 desc_length = acpi_device_write_zero_len();
578
579 /* Byte 3: Revision ID */
580 acpigen_emit_byte(ACPI_UART_SERIAL_BUS_REVISION_ID);
581
582 /* Byte 4: Resource Source Index is Reserved */
583 acpigen_emit_byte(0);
584
585 /* Byte 5: Serial Bus Type is UART */
586 acpigen_emit_byte(ACPI_SERIAL_BUS_TYPE_UART);
587
588 /*
589 * Byte 6: Flags
590 * [7:2]: 0 => Reserved
591 * [1]: 1 => ResourceConsumer
592 * [0]: 0 => ControllerInitiated
593 */
594 acpigen_emit_byte(BIT(1));
595
596 /*
597 * Byte 7-8: Type Specific Flags
598 * [15:8]: 0 => Reserved
599 * [7]: 0 => Little Endian, 1 => Big Endian
600 * [6:4]: Data bits
601 * [3:2]: Stop bits
602 * [1:0]: Flow control
603 */
604 flags = uart->flow_control & 3;
605 flags |= (uart->stop_bits & 3) << 2;
606 flags |= (uart->data_bits & 7) << 4;
607 flags |= (uart->endian & 1) << 7;
608 acpigen_emit_word(flags);
609
610 /* Byte 9: Type Specific Revision ID */
611 acpigen_emit_byte(ACPI_UART_TYPE_SPECIFIC_REVISION_ID);
612
613 /* Byte 10-11: Type Data Length */
614 type_length = acpi_device_write_zero_len();
615
616 /* Byte 12-15: Initial Baud Rate */
617 acpigen_emit_dword(uart->initial_baud_rate);
618
619 /* Byte 16-17: RX FIFO size */
620 acpigen_emit_word(uart->rx_fifo_bytes);
621
622 /* Byte 18-19: TX FIFO size */
623 acpigen_emit_word(uart->tx_fifo_bytes);
624
625 /* Byte 20: Parity */
626 acpigen_emit_byte(uart->parity);
627
628 /* Byte 21: Lines Enabled */
629 acpigen_emit_byte(uart->lines_in_use);
630
631 /* Fill in Type Data Length */
632 acpi_device_fill_len(type_length);
633
634 /* Byte 22+: ResourceSource */
635 acpigen_emit_string(uart->resource);
636
637 /* Fill in Descriptor Length */
638 acpi_device_fill_len(desc_length);
639}
640
Raul E Rangel6d2bc002021-05-27 15:04:21 -0600641#define ACPI_POWER_RESOURCE_STATUS_ON_OP ONE_OP
642#define ACPI_POWER_RESOURCE_STATUS_OFF_OP ZERO_OP
643
644/**
645 * Writes an ACPI fragment that will check the GPIO and return 0 if the GPIO
646 * state does not match the active parameter.
647 */
648static void acpigen_write_gpio_STA(const struct acpi_gpio *gpio, bool active)
649{
650 if (!gpio || !gpio->pin_count)
651 return;
652
653 /* Read current GPIO status into Local0. */
654 acpigen_get_tx_gpio(gpio);
655
656 /*
657 * If (!Local0)
658 * {
659 * Return (Zero)
660 * }
661 */
662 acpigen_write_if();
663 if (active)
664 acpigen_emit_byte(LNOT_OP);
665 acpigen_emit_byte(LOCAL0_OP);
666 acpigen_write_return_op(ACPI_POWER_RESOURCE_STATUS_OFF_OP);
667 acpigen_write_if_end();
668}
669
670static void acpigen_write_power_res_STA(const struct acpi_power_res_params *params)
671{
672 acpigen_write_method_serialized("_STA", 0);
673
674 /* Verify all the GPIOs are in the ON state, otherwise return 0 */
675 acpigen_write_gpio_STA(params->enable_gpio, true);
676 acpigen_write_gpio_STA(params->reset_gpio, false);
677 acpigen_write_gpio_STA(params->stop_gpio, false);
678
679 /* All GPIOs are in the ON state */
680 acpigen_write_return_op(ACPI_POWER_RESOURCE_STATUS_ON_OP);
681
682 acpigen_pop_len(); /* Method */
683}
684
Duncan Lauriebd73dbb2017-02-17 17:05:03 -0800685/* PowerResource() with Enable and/or Reset control */
Shelley Chena0603392018-04-26 13:52:30 -0700686void acpi_device_add_power_res(const struct acpi_power_res_params *params)
Duncan Lauriebd73dbb2017-02-17 17:05:03 -0800687{
Furquan Shaikhd47946e2021-09-14 15:54:41 -0700688 static uint8_t id;
Furquan Shaikhdc782752020-04-30 22:49:39 -0700689 static const char * const power_res_dev_states[] = { "_PR0", "_PR3" };
Edward O'Callaghan7e262552020-01-23 10:32:33 +1100690 unsigned int reset_gpio = params->reset_gpio ? params->reset_gpio->pins[0] : 0;
691 unsigned int enable_gpio = params->enable_gpio ? params->enable_gpio->pins[0] : 0;
692 unsigned int stop_gpio = params->stop_gpio ? params->stop_gpio->pins[0] : 0;
Furquan Shaikhd47946e2021-09-14 15:54:41 -0700693 char pr_name[ACPI_NAME_BUFFER_SIZE];
Duncan Lauriebd73dbb2017-02-17 17:05:03 -0800694
Furquan Shaikhedf459f2017-08-28 17:20:49 -0700695 if (!reset_gpio && !enable_gpio && !stop_gpio)
Duncan Lauriebd73dbb2017-02-17 17:05:03 -0800696 return;
697
Furquan Shaikhd47946e2021-09-14 15:54:41 -0700698 snprintf(pr_name, sizeof(pr_name), "PR%02X", id++);
699
700 /* PowerResource (PR##, 0, 0) */
701 acpigen_write_power_res(pr_name, 0, 0, power_res_dev_states,
Duncan Lauriebd73dbb2017-02-17 17:05:03 -0800702 ARRAY_SIZE(power_res_dev_states));
703
Raul E Rangel6d2bc002021-05-27 15:04:21 -0600704 if (params->use_gpio_for_status) {
705 acpigen_write_power_res_STA(params);
706 } else {
707 /* Method (_STA, 0, NotSerialized) { Return (0x1) } */
708 acpigen_write_STA(ACPI_POWER_RESOURCE_STATUS_ON_OP);
709 }
Duncan Lauriebd73dbb2017-02-17 17:05:03 -0800710
711 /* Method (_ON, 0, Serialized) */
712 acpigen_write_method_serialized("_ON", 0);
Tim Van Patten3d4665c2022-04-13 11:53:20 -0600713 /* Call _STA and early return if the device is already enabled, since the Linux
714 kernel doesn't check the device status before calling _ON. This avoids
715 unnecessary delays while booting. */
716 if (params->use_gpio_for_status) {
717 /* Local0 = _STA () */
718 acpigen_write_store();
719 acpigen_emit_namestring("_STA");
720 acpigen_emit_byte(LOCAL0_OP);
721 /* If (( Local0 == ACPI_POWER_RESOURCE_STATUS_ON_OP)) */
722 acpigen_write_if_lequal_op_op(LOCAL0_OP, ACPI_POWER_RESOURCE_STATUS_ON_OP);
723 acpigen_write_return_op(ZERO_OP);
724 acpigen_write_if_end();
725 }
Duncan Lauriebd73dbb2017-02-17 17:05:03 -0800726 if (reset_gpio)
Shelley Chena0603392018-04-26 13:52:30 -0700727 acpigen_enable_tx_gpio(params->reset_gpio);
Duncan Lauriebd73dbb2017-02-17 17:05:03 -0800728 if (enable_gpio) {
Shelley Chena0603392018-04-26 13:52:30 -0700729 acpigen_enable_tx_gpio(params->enable_gpio);
730 if (params->enable_delay_ms)
731 acpigen_write_sleep(params->enable_delay_ms);
Duncan Lauriebd73dbb2017-02-17 17:05:03 -0800732 }
733 if (reset_gpio) {
Shelley Chena0603392018-04-26 13:52:30 -0700734 acpigen_disable_tx_gpio(params->reset_gpio);
735 if (params->reset_delay_ms)
736 acpigen_write_sleep(params->reset_delay_ms);
Duncan Lauriebd73dbb2017-02-17 17:05:03 -0800737 }
Furquan Shaikhedf459f2017-08-28 17:20:49 -0700738 if (stop_gpio) {
Shelley Chena0603392018-04-26 13:52:30 -0700739 acpigen_disable_tx_gpio(params->stop_gpio);
740 if (params->stop_delay_ms)
741 acpigen_write_sleep(params->stop_delay_ms);
Furquan Shaikhedf459f2017-08-28 17:20:49 -0700742 }
Duncan Lauriebd73dbb2017-02-17 17:05:03 -0800743 acpigen_pop_len(); /* _ON method */
744
745 /* Method (_OFF, 0, Serialized) */
746 acpigen_write_method_serialized("_OFF", 0);
Shelley Chena0603392018-04-26 13:52:30 -0700747 if (stop_gpio) {
748 acpigen_enable_tx_gpio(params->stop_gpio);
749 if (params->stop_off_delay_ms)
750 acpigen_write_sleep(params->stop_off_delay_ms);
751 }
752 if (reset_gpio) {
753 acpigen_enable_tx_gpio(params->reset_gpio);
754 if (params->reset_off_delay_ms)
755 acpigen_write_sleep(params->reset_off_delay_ms);
756 }
757 if (enable_gpio) {
758 acpigen_disable_tx_gpio(params->enable_gpio);
759 if (params->enable_off_delay_ms)
760 acpigen_write_sleep(params->enable_off_delay_ms);
761 }
Duncan Lauriebd73dbb2017-02-17 17:05:03 -0800762 acpigen_pop_len(); /* _OFF method */
763
Furquan Shaikhd47946e2021-09-14 15:54:41 -0700764 acpigen_pop_len(); /* PowerResource PR## */
Duncan Lauriebd73dbb2017-02-17 17:05:03 -0800765}
766
Duncan Laurieffc99902016-07-02 19:56:06 -0700767static void acpi_dp_write_array(const struct acpi_dp *array);
768static void acpi_dp_write_value(const struct acpi_dp *prop)
Duncan Laurie559e9472016-05-10 13:18:17 -0700769{
770 switch (prop->type) {
771 case ACPI_DP_TYPE_INTEGER:
772 acpigen_write_integer(prop->integer);
773 break;
774 case ACPI_DP_TYPE_STRING:
Harsha Priya3a96ac42016-07-15 17:31:43 -0700775 case ACPI_DP_TYPE_CHILD:
Duncan Laurie559e9472016-05-10 13:18:17 -0700776 acpigen_write_string(prop->string);
777 break;
778 case ACPI_DP_TYPE_REFERENCE:
779 acpigen_emit_namestring(prop->string);
780 break;
Duncan Laurieffc99902016-07-02 19:56:06 -0700781 case ACPI_DP_TYPE_ARRAY:
782 acpi_dp_write_array(prop->array);
783 break;
784 default:
785 break;
Duncan Laurie559e9472016-05-10 13:18:17 -0700786 }
787}
788
Duncan Laurieffc99902016-07-02 19:56:06 -0700789/* Package (2) { "prop->name", VALUE } */
790static void acpi_dp_write_property(const struct acpi_dp *prop)
Duncan Laurie559e9472016-05-10 13:18:17 -0700791{
792 acpigen_write_package(2);
Duncan Laurieffc99902016-07-02 19:56:06 -0700793 acpigen_write_string(prop->name);
Duncan Laurie559e9472016-05-10 13:18:17 -0700794 acpi_dp_write_value(prop);
795 acpigen_pop_len();
Duncan Laurie559e9472016-05-10 13:18:17 -0700796}
797
798/* Write array of Device Properties */
Duncan Laurieffc99902016-07-02 19:56:06 -0700799static void acpi_dp_write_array(const struct acpi_dp *array)
Duncan Laurie559e9472016-05-10 13:18:17 -0700800{
Duncan Laurieffc99902016-07-02 19:56:06 -0700801 const struct acpi_dp *dp;
802 char *pkg_count;
803
804 /* Package element count determined as it is populated */
805 pkg_count = acpigen_write_package(0);
806
Furquan Shaikh35c01bc2016-10-03 23:30:14 -0700807 /*
808 * Only acpi_dp of type DP_TYPE_TABLE is allowed to be an array.
809 * DP_TYPE_TABLE does not have a value to be written. Thus, start
810 * the loop from next type in the array.
811 */
812 for (dp = array->next; dp; dp = dp->next) {
Duncan Laurieffc99902016-07-02 19:56:06 -0700813 acpi_dp_write_value(dp);
814 (*pkg_count)++;
815 }
816
Duncan Laurie559e9472016-05-10 13:18:17 -0700817 acpigen_pop_len();
Duncan Laurie559e9472016-05-10 13:18:17 -0700818}
819
Duncan Laurieffc99902016-07-02 19:56:06 -0700820static void acpi_dp_free(struct acpi_dp *dp)
Duncan Laurie559e9472016-05-10 13:18:17 -0700821{
Duncan Laurieffc99902016-07-02 19:56:06 -0700822 while (dp) {
823 struct acpi_dp *p = dp->next;
824
825 switch (dp->type) {
826 case ACPI_DP_TYPE_CHILD:
827 acpi_dp_free(dp->child);
828 break;
829 case ACPI_DP_TYPE_ARRAY:
830 acpi_dp_free(dp->array);
831 break;
832 default:
833 break;
834 }
835
836 free(dp);
837 dp = p;
838 }
Duncan Laurie559e9472016-05-10 13:18:17 -0700839}
840
Duncan Laurie84fac412020-06-03 12:36:51 -0700841static bool acpi_dp_write_properties(struct acpi_dp *prop, const char *uuid)
842{
843 struct acpi_dp *dp;
844 char *prop_count = NULL;
845
846 /* Print base properties */
847 for (dp = prop; dp; dp = dp->next) {
848 if (dp->type == ACPI_DP_TYPE_TABLE ||
849 dp->type == ACPI_DP_TYPE_CHILD ||
850 dp->type == ACPI_DP_TYPE_PACKAGE)
851 continue;
852
853 /*
854 * The UUID and package is only added when
855 * we come across the first property. This
856 * is to avoid creating a zero-length package
857 * in situations where there are only children.
858 */
859 if (!prop_count) {
860 /* ToUUID (dp->uuid) */
861 acpigen_write_uuid(uuid);
862 /*
863 * Package (PROP), element count determined as
864 * it is populated
865 */
866 prop_count = acpigen_write_package(0);
867 }
868 (*prop_count)++;
869 acpi_dp_write_property(dp);
870 }
871 if (prop_count) {
872 /* Package (PROP) length, if a package was written */
873 acpigen_pop_len();
874 return true;
875 }
876 return false;
877}
878
Simon Glass8bfa51e2020-06-29 16:18:37 -0600879static void acpi_dp_write_(struct acpi_dp *table)
Duncan Laurie559e9472016-05-10 13:18:17 -0700880{
Duncan Laurieffc99902016-07-02 19:56:06 -0700881 struct acpi_dp *dp, *prop;
Duncan Laurie84fac412020-06-03 12:36:51 -0700882 char *dp_count;
Duncan Laurieffc99902016-07-02 19:56:06 -0700883 int child_count = 0;
884
Duncan Lauriec1adeb62020-04-29 00:04:14 -0700885 if (!table || table->type != ACPI_DP_TYPE_TABLE || !table->next)
Duncan Laurieffc99902016-07-02 19:56:06 -0700886 return;
887
888 /* Name (name) */
889 acpigen_write_name(table->name);
890
891 /* Device Property list starts with the next entry */
892 prop = table->next;
893
Matt Delco08258882019-01-30 11:16:08 -0800894 /* Package (DP), default to assuming no properties or children */
895 dp_count = acpigen_write_package(0);
Duncan Laurieffc99902016-07-02 19:56:06 -0700896
897 /* Print base properties */
Duncan Laurie84fac412020-06-03 12:36:51 -0700898 if (acpi_dp_write_properties(prop, table->uuid))
899 *dp_count += 2;
Duncan Laurieffc99902016-07-02 19:56:06 -0700900
Duncan Laurie84fac412020-06-03 12:36:51 -0700901 /* Count child properties */
902 for (dp = prop; dp; dp = dp->next)
903 if (dp->type == ACPI_DP_TYPE_CHILD)
904 child_count++;
905
906 /* Add child properties to the base table */
Duncan Laurieffc99902016-07-02 19:56:06 -0700907 if (child_count) {
Duncan Laurie84fac412020-06-03 12:36:51 -0700908 /* Update DP package count */
Matt Delco08258882019-01-30 11:16:08 -0800909 *dp_count += 2;
Duncan Laurieffc99902016-07-02 19:56:06 -0700910 /* ToUUID (ACPI_DP_CHILD_UUID) */
911 acpigen_write_uuid(ACPI_DP_CHILD_UUID);
912
913 /* Print child pointer properties */
914 acpigen_write_package(child_count);
915
916 for (dp = prop; dp; dp = dp->next)
917 if (dp->type == ACPI_DP_TYPE_CHILD)
918 acpi_dp_write_property(dp);
Matt Delco08258882019-01-30 11:16:08 -0800919 /* Package (CHILD) length */
Duncan Laurieffc99902016-07-02 19:56:06 -0700920 acpigen_pop_len();
921 }
922
Duncan Laurie84fac412020-06-03 12:36:51 -0700923 /* Write packages of properties with unique UUID */
924 for (dp = prop; dp; dp = dp->next)
925 if (dp->type == ACPI_DP_TYPE_PACKAGE)
926 if (acpi_dp_write_properties(dp->child, dp->uuid))
927 *dp_count += 2;
928
Duncan Laurieffc99902016-07-02 19:56:06 -0700929 /* Package (DP) length */
930 acpigen_pop_len();
931
932 /* Recursively parse children into separate tables */
933 for (dp = prop; dp; dp = dp->next)
934 if (dp->type == ACPI_DP_TYPE_CHILD)
Simon Glass8bfa51e2020-06-29 16:18:37 -0600935 acpi_dp_write_(dp->child);
936}
937
938void acpi_dp_write(struct acpi_dp *table)
939{
940 acpi_dp_write_(table);
Duncan Laurieffc99902016-07-02 19:56:06 -0700941
942 /* Clean up */
943 acpi_dp_free(table);
944}
945
946static struct acpi_dp *acpi_dp_new(struct acpi_dp *dp, enum acpi_dp_type type,
947 const char *name)
948{
949 struct acpi_dp *new;
950
951 new = malloc(sizeof(struct acpi_dp));
952 if (!new)
953 return NULL;
954
955 memset(new, 0, sizeof(*new));
956 new->type = type;
957 new->name = name;
Duncan Laurie84fac412020-06-03 12:36:51 -0700958 new->uuid = ACPI_DP_UUID;
Duncan Laurieffc99902016-07-02 19:56:06 -0700959
960 if (dp) {
961 /* Add to end of property list */
962 while (dp->next)
963 dp = dp->next;
964 dp->next = new;
965 }
966
967 return new;
968}
969
970struct acpi_dp *acpi_dp_new_table(const char *name)
971{
972 return acpi_dp_new(NULL, ACPI_DP_TYPE_TABLE, name);
973}
974
Duncan Laurieb3023b62017-08-29 08:26:50 -0700975size_t acpi_dp_add_property_list(struct acpi_dp *dp,
976 const struct acpi_dp *property_list,
977 size_t property_count)
978{
979 const struct acpi_dp *prop;
980 size_t i, properties_added = 0;
981
Jacob Garberc30e5902019-05-23 14:34:58 -0600982 if (!dp || !property_list)
983 return 0;
984
Duncan Laurieb3023b62017-08-29 08:26:50 -0700985 for (i = 0; i < property_count; i++) {
986 prop = &property_list[i];
987
988 if (prop->type == ACPI_DP_TYPE_UNKNOWN || !prop->name)
989 continue;
990
991 switch (prop->type) {
992 case ACPI_DP_TYPE_INTEGER:
993 acpi_dp_add_integer(dp, prop->name, prop->integer);
994 break;
995 case ACPI_DP_TYPE_STRING:
996 acpi_dp_add_string(dp, prop->name, prop->string);
997 break;
998 case ACPI_DP_TYPE_REFERENCE:
999 acpi_dp_add_reference(dp, prop->name, prop->string);
1000 break;
1001 case ACPI_DP_TYPE_ARRAY:
1002 acpi_dp_add_array(dp, prop->array);
1003 break;
1004 case ACPI_DP_TYPE_CHILD:
1005 acpi_dp_add_child(dp, prop->name, prop->child);
1006 break;
1007 default:
1008 continue;
1009 }
1010
1011 ++properties_added;
1012 }
1013
1014 return properties_added;
1015}
1016
Duncan Laurieffc99902016-07-02 19:56:06 -07001017struct acpi_dp *acpi_dp_add_integer(struct acpi_dp *dp, const char *name,
1018 uint64_t value)
1019{
Jacob Garberc30e5902019-05-23 14:34:58 -06001020 if (!dp)
1021 return NULL;
1022
Duncan Laurieffc99902016-07-02 19:56:06 -07001023 struct acpi_dp *new = acpi_dp_new(dp, ACPI_DP_TYPE_INTEGER, name);
1024
1025 if (new)
1026 new->integer = value;
1027
1028 return new;
1029}
1030
1031struct acpi_dp *acpi_dp_add_string(struct acpi_dp *dp, const char *name,
1032 const char *string)
1033{
Jacob Garberc30e5902019-05-23 14:34:58 -06001034 if (!dp)
1035 return NULL;
1036
Duncan Laurieffc99902016-07-02 19:56:06 -07001037 struct acpi_dp *new = acpi_dp_new(dp, ACPI_DP_TYPE_STRING, name);
1038
1039 if (new)
1040 new->string = string;
1041
1042 return new;
1043}
1044
1045struct acpi_dp *acpi_dp_add_reference(struct acpi_dp *dp, const char *name,
1046 const char *reference)
1047{
Jacob Garberc30e5902019-05-23 14:34:58 -06001048 if (!dp)
1049 return NULL;
1050
Duncan Laurieffc99902016-07-02 19:56:06 -07001051 struct acpi_dp *new = acpi_dp_new(dp, ACPI_DP_TYPE_REFERENCE, name);
1052
1053 if (new)
1054 new->string = reference;
1055
1056 return new;
1057}
1058
1059struct acpi_dp *acpi_dp_add_child(struct acpi_dp *dp, const char *name,
1060 struct acpi_dp *child)
1061{
1062 struct acpi_dp *new;
1063
Jacob Garberc30e5902019-05-23 14:34:58 -06001064 if (!dp || !child || child->type != ACPI_DP_TYPE_TABLE)
Duncan Laurieffc99902016-07-02 19:56:06 -07001065 return NULL;
1066
1067 new = acpi_dp_new(dp, ACPI_DP_TYPE_CHILD, name);
1068 if (new) {
1069 new->child = child;
1070 new->string = child->name;
1071 }
1072
1073 return new;
1074}
1075
Duncan Laurie84fac412020-06-03 12:36:51 -07001076struct acpi_dp *acpi_dp_add_package(struct acpi_dp *dp, struct acpi_dp *package)
1077{
1078 struct acpi_dp *new;
1079
1080 if (!dp || !package || package->type != ACPI_DP_TYPE_TABLE)
1081 return NULL;
1082
1083 new = acpi_dp_new(dp, ACPI_DP_TYPE_PACKAGE, NULL);
1084 if (new) {
1085 new->uuid = package->name;
1086 new->child = package;
1087 }
1088
1089 return new;
1090}
1091
Duncan Laurieffc99902016-07-02 19:56:06 -07001092struct acpi_dp *acpi_dp_add_array(struct acpi_dp *dp, struct acpi_dp *array)
1093{
1094 struct acpi_dp *new;
1095
Jacob Garberc30e5902019-05-23 14:34:58 -06001096 if (!dp || !array || array->type != ACPI_DP_TYPE_TABLE)
Duncan Laurieffc99902016-07-02 19:56:06 -07001097 return NULL;
1098
1099 new = acpi_dp_new(dp, ACPI_DP_TYPE_ARRAY, array->name);
1100 if (new)
1101 new->array = array;
1102
1103 return new;
1104}
1105
1106struct acpi_dp *acpi_dp_add_integer_array(struct acpi_dp *dp, const char *name,
Duncan Laurieed6eb272020-04-29 11:39:08 -07001107 const uint64_t *array, int len)
Duncan Laurieffc99902016-07-02 19:56:06 -07001108{
1109 struct acpi_dp *dp_array;
1110 int i;
1111
Jacob Garberc30e5902019-05-23 14:34:58 -06001112 if (!dp || len <= 0)
Duncan Laurieffc99902016-07-02 19:56:06 -07001113 return NULL;
1114
1115 dp_array = acpi_dp_new_table(name);
1116 if (!dp_array)
1117 return NULL;
1118
1119 for (i = 0; i < len; i++)
1120 if (!acpi_dp_add_integer(dp_array, NULL, array[i]))
1121 break;
1122
1123 acpi_dp_add_array(dp, dp_array);
1124
1125 return dp_array;
1126}
1127
Karthikeyan Ramasubramanian685dbe12020-10-05 10:44:50 -06001128struct acpi_dp *acpi_dp_add_gpio_array(struct acpi_dp *dp, const char *name,
1129 const struct acpi_gpio_res_params *params,
1130 size_t param_count)
1131{
1132 struct acpi_dp *gpio;
1133 uint32_t i;
1134
1135 if (!dp || !param_count)
1136 return NULL;
1137
1138 gpio = acpi_dp_new_table(name);
1139 if (!gpio)
1140 return NULL;
1141
1142 /*
1143 * Generate ACPI identifiers as follows:
1144 * Package () {
1145 * name, // e.g. cs-gpios
1146 * Package() {
1147 * ref, index, pin, active_low, // GPIO-0 (params[0])
1148 * ref, index, pin, active_low, // GPIO-1 (params[1])
1149 * ...
1150 * }
1151 * }
1152 */
1153 for (i = 0; i < param_count; i++, params++) {
1154 /*
1155 * If refs is NULL, leave a hole in the gpio array. This can be used in
1156 * conditions where some controllers use both GPIOs and native signals.
1157 */
1158 if (!params->ref) {
1159 acpi_dp_add_integer(gpio, NULL, 0);
1160 continue;
1161 }
1162
1163 /* The device that has _CRS containing GpioIO()/GpioInt() */
1164 acpi_dp_add_reference(gpio, NULL, params->ref);
1165
1166 /* Index of the GPIO resource in _CRS starting from zero */
1167 acpi_dp_add_integer(gpio, NULL, params->index);
1168
1169 /* Pin in the GPIO resource, typically zero */
1170 acpi_dp_add_integer(gpio, NULL, params->pin);
1171
1172 /* Set if pin is active low */
1173 acpi_dp_add_integer(gpio, NULL, params->active_low);
1174 }
1175 acpi_dp_add_array(dp, gpio);
1176
1177 return gpio;
Karthikeyan Ramasubramanian685dbe12020-10-05 10:44:50 -06001178}
1179
1180
Duncan Laurieffc99902016-07-02 19:56:06 -07001181struct acpi_dp *acpi_dp_add_gpio(struct acpi_dp *dp, const char *name,
1182 const char *ref, int index, int pin,
1183 int active_low)
1184{
Karthikeyan Ramasubramanian685dbe12020-10-05 10:44:50 -06001185 struct acpi_gpio_res_params param = {
1186 .ref = ref,
1187 .index = index,
1188 .pin = pin,
1189 .active_low = active_low,
1190 };
Jacob Garberc30e5902019-05-23 14:34:58 -06001191
Karthikeyan Ramasubramanian685dbe12020-10-05 10:44:50 -06001192 return acpi_dp_add_gpio_array(dp, name, &param, 1);
Duncan Laurie559e9472016-05-10 13:18:17 -07001193}
Furquan Shaikhd1130af2020-04-23 12:51:42 -07001194
1195/*
1196 * This function writes a PCI device with _ADR object:
1197 * Example:
1198 * Scope (\_SB.PCI0)
1199 * {
1200 * Device (IGFX)
1201 * {
1202 * Name (_ADR, 0x0000000000000000)
1203 * Method (_STA, 0, NotSerialized) { Return (status) }
1204 * }
1205 * }
1206 */
Furquan Shaikh7536a392020-04-24 21:59:21 -07001207void acpi_device_write_pci_dev(const struct device *dev)
Furquan Shaikhd1130af2020-04-23 12:51:42 -07001208{
1209 const char *scope = acpi_device_scope(dev);
1210 const char *name = acpi_device_name(dev);
1211
1212 assert(dev->path.type == DEVICE_PATH_PCI);
1213 assert(name);
1214 assert(scope);
1215
1216 acpigen_write_scope(scope);
1217 acpigen_write_device(name);
1218
1219 acpigen_write_ADR_pci_device(dev);
1220 acpigen_write_STA(acpi_device_status(dev));
1221
1222 acpigen_pop_len(); /* Device */
1223 acpigen_pop_len(); /* Scope */
1224}
Kapil Porwalddc52a62022-11-26 19:10:57 +05301225
Kapil Porwal75436272022-11-28 17:25:48 +05301226/*
1227 * Helper function to add given integer property with an UUID to _DSD in the current scope.
1228 *
1229 * dsd - Pointer to a _DSD object.
1230 * Append to existing _DSD object if not NULL.
1231 * Create new _DSD object and flush it if NULL.
1232 * uuid - Pointer to the UUID string.
1233 * name - Pointer to the property name string.
1234 * value - Value of the integer property.
1235 */
1236static void acpi_device_add_integer_property_with_uuid(struct acpi_dp *dsd,
1237 const char *uuid,
1238 const char *name,
1239 uint64_t value)
Kapil Porwalddc52a62022-11-26 19:10:57 +05301240{
1241 struct acpi_dp *prev_dsd = dsd, *pkg;
1242 if (prev_dsd == NULL)
1243 dsd = acpi_dp_new_table("_DSD");
Kapil Porwal75436272022-11-28 17:25:48 +05301244 pkg = acpi_dp_new_table(uuid);
1245 acpi_dp_add_integer(pkg, name, value);
Kapil Porwalddc52a62022-11-26 19:10:57 +05301246 acpi_dp_add_package(dsd, pkg);
1247 if (prev_dsd == NULL)
1248 acpi_dp_write(dsd);
1249}
Kapil Porwal75436272022-11-28 17:25:48 +05301250
1251/* _DSD with ExternalFacingPort */
1252void acpi_device_add_external_facing_port(struct acpi_dp *dsd)
1253{
1254 acpi_device_add_integer_property_with_uuid(dsd,
1255 ACPI_DSD_EXTERNAL_FACING_PORT_UUID,
1256 ACPI_DSD_EXTERNAL_FACING_PORT_NAME,
1257 1);
1258}
1259
1260/* _DSD with HotPlugSupportInD3 */
1261void acpi_device_add_hotplug_support_in_d3(struct acpi_dp *dsd)
1262{
1263 acpi_device_add_integer_property_with_uuid(dsd,
1264 ACPI_DSD_HOTPLUG_IN_D3_UUID,
1265 ACPI_DSD_HOTPLUG_IN_D3_NAME,
1266 1);
1267}
1268
1269/* _DSD with DmaProperty */
1270void acpi_device_add_dma_property(struct acpi_dp *dsd)
1271{
1272 acpi_device_add_integer_property_with_uuid(dsd,
1273 ACPI_DSD_DMA_PROPERTY_UUID,
1274 ACPI_DSD_DMA_PROPERTY_NAME,
1275 1);
1276}
1277
1278/* _DSD with StorageD3Enable */
1279void acpi_device_add_storage_d3_enable(struct acpi_dp *dsd)
1280{
1281 acpi_device_add_integer_property_with_uuid(dsd,
1282 ACPI_DSD_STORAGE_D3_UUID,
1283 ACPI_DSD_STORAGE_D3_NAME,
1284 1);
1285}