util/sconfig: Remove unused ioapic and irq keywords
[coreboot.git] / src / acpi / device.c
blobb76357e6757b5eea0dc1fb0e0b34cfafb5798a38
1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #include <assert.h>
4 #include <string.h>
5 #include <acpi/acpi.h>
6 #include <acpi/acpi_device.h>
7 #include <acpi/acpigen.h>
8 #include <acpi/acpigen_pci.h>
9 #include <device/device.h>
10 #include <device/path.h>
11 #include <stdlib.h>
12 #include <types.h>
13 #include <crc_byte.h>
15 #if CONFIG(GENERIC_GPIO_LIB)
16 #include <gpio.h>
17 #endif
19 #define ACPI_DP_UUID "daffd814-6eba-4d8c-8a91-bc9bbf4aa301"
20 #define ACPI_DP_CHILD_UUID "dbb8e3e6-5886-4ba6-8795-1319f52a966b"
23 * Below properties are defined at
24 * https://docs.microsoft.com/en-us/windows-hardware/drivers/pci/dsd-for-pcie-root-ports
26 #define ACPI_DSD_EXTERNAL_FACING_PORT_UUID "EFCC06CC-73AC-4BC3-BFF0-76143807C389"
27 #define ACPI_DSD_EXTERNAL_FACING_PORT_NAME "ExternalFacingPort"
29 #define ACPI_DSD_HOTPLUG_IN_D3_UUID "6211E2C0-58A3-4AF3-90E1-927A4E0C55A4"
30 #define ACPI_DSD_HOTPLUG_IN_D3_NAME "HotPlugSupportInD3"
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"
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
40 #define ACPI_DSD_STORAGE_D3_UUID "5025030F-842F-4AB4-A561-99A5189762D0"
41 #define ACPI_DSD_STORAGE_D3_NAME "StorageD3Enable"
43 /* Write empty word value and return pointer to it */
44 static void *acpi_device_write_zero_len(void)
46 char *p = acpigen_get_current();
47 acpigen_emit_word(0);
48 return p;
51 /* Fill in length value from start to current at specified location */
52 static void acpi_device_fill_from_len(char *ptr, char *start)
54 uint16_t len = acpigen_get_current() - start;
55 ptr[0] = len & 0xff;
56 ptr[1] = (len >> 8) & 0xff;
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.
64 static void acpi_device_fill_len(void *ptr)
66 acpi_device_fill_from_len(ptr, ptr + sizeof(uint16_t));
69 /* Locate and return the ACPI name for this device */
70 const char *acpi_device_name(const struct device *dev)
72 const struct device *pdev = dev;
73 const char *name = NULL;
75 if (!dev)
76 return NULL;
78 /* Check for device specific handler */
79 if (dev->ops && dev->ops->acpi_name)
80 return dev->ops->acpi_name(dev);
82 /* 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;
95 return NULL;
98 /* Locate and return the ACPI _HID (Hardware ID) for this device */
99 const char *acpi_device_hid(const struct device *dev)
101 if (!dev)
102 return NULL;
104 /* Check for device specific handler */
105 if (dev->ops->acpi_hid)
106 return dev->ops->acpi_hid(dev);
109 * Don't walk up the tree to find any parent that can identify this device, as
110 * PNP devices are hard to identify.
113 return NULL;
117 * Generate unique ID based on the ACPI path.
118 * Collisions on the same _HID are possible but very unlikely.
120 uint32_t acpi_device_uid(const struct device *dev)
122 const char *path = acpi_device_path(dev);
123 if (!path)
124 return 0;
126 return CRC(path, strlen(path), crc32_byte);
129 /* Recursive function to find the root device and print a path from there */
130 static ssize_t acpi_device_path_fill(const struct device *dev, char *buf,
131 size_t buf_len, size_t cur)
133 const char *name = acpi_device_name(dev);
134 ssize_t next = 0;
136 if (!name)
137 return -1;
140 * Make sure this name segment will fit, including the path segment
141 * separator and possible NUL terminator if this is the last segment.
143 if (!dev || (cur + strlen(name) + 2) > buf_len)
144 return cur;
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);
149 if (next < 0)
150 return next;
152 /* Fill in the path from the root device */
153 next += snprintf(buf + next, buf_len - next, "%s%s",
154 (dev->path.type == DEVICE_PATH_ROOT
155 || (strlen(name) == 0)) ?
156 "" : ".", name);
158 return next;
162 * Warning: just as with dev_path() this uses a static buffer
163 * so should not be called multiple times in one statement
165 const char *acpi_device_path(const struct device *dev)
167 static char buf[DEVICE_PATH_MAX] = {};
169 if (!dev)
170 return NULL;
172 if (acpi_device_path_fill(dev, buf, sizeof(buf), 0) <= 0)
173 return NULL;
175 return buf;
178 /* Return the path of the parent device as the ACPI Scope for this device */
179 const char *acpi_device_scope(const struct device *dev)
181 static char buf[DEVICE_PATH_MAX] = {};
183 if (!dev || !dev->bus || !dev->bus->dev)
184 return NULL;
186 if (acpi_device_path_fill(dev->bus->dev, buf, sizeof(buf), 0) <= 0)
187 return NULL;
189 return buf;
192 /* Concatenate the device path and provided name suffix */
193 const char *acpi_device_path_join(const struct device *dev, const char *name)
195 static char buf[DEVICE_PATH_MAX] = {};
196 ssize_t len;
198 if (!dev)
199 return NULL;
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;
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);
211 return buf;
214 int acpi_device_status(const struct device *dev)
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;
223 /* Write the unique _UID based on ACPI device path. */
224 void acpi_device_write_uid(const struct device *dev)
226 acpigen_write_name_integer("_UID", acpi_device_uid(dev));
229 /* ACPI 6.1 section 6.4.3.6: Extended Interrupt Descriptor */
230 void acpi_device_write_interrupt(const struct acpi_irq *irq)
232 void *desc_length;
233 uint8_t flags;
235 if (!irq || !irq->pin)
236 return;
238 /* This is supported by GpioInt() but not Interrupt() */
239 if (irq->polarity == ACPI_IRQ_ACTIVE_BOTH)
240 return;
242 /* Byte 0: Descriptor Type */
243 acpigen_emit_byte(ACPI_DESCRIPTOR_INTERRUPT);
245 /* Byte 1-2: Length (filled in later) */
246 desc_length = acpi_device_write_zero_len();
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)
257 flags = 1 << 0; /* ResourceConsumer */
258 if (irq->mode == ACPI_IRQ_EDGE_TRIGGERED)
259 flags |= 1 << 1;
260 if (irq->polarity == ACPI_IRQ_ACTIVE_LOW)
261 flags |= 1 << 2;
262 if (irq->shared == ACPI_IRQ_SHARED)
263 flags |= 1 << 3;
264 if (irq->wake == ACPI_IRQ_WAKE)
265 flags |= 1 << 4;
266 acpigen_emit_byte(flags);
268 /* Byte 4: Interrupt Table Entry Count */
269 acpigen_emit_byte(1);
271 /* Byte 5-8: Interrupt Number */
272 acpigen_emit_dword(irq->pin);
274 /* Fill in Descriptor Length (account for len word) */
275 acpi_device_fill_len(desc_length);
278 /* ACPI 6.1 section 6.4.3.8.1 - GPIO Interrupt or I/O */
279 void acpi_device_write_gpio(const struct acpi_gpio *gpio)
281 void *start, *desc_length;
282 void *pin_table_offset, *vendor_data_offset, *resource_offset;
283 uint16_t flags = 0;
284 int pin;
286 if (!gpio || gpio->type > ACPI_GPIO_TYPE_IO)
287 return;
289 start = acpigen_get_current();
291 /* Byte 0: Descriptor Type */
292 acpigen_emit_byte(ACPI_DESCRIPTOR_GPIO);
294 /* Byte 1-2: Length (fill in later) */
295 desc_length = acpi_device_write_zero_len();
297 /* Byte 3: Revision ID */
298 acpigen_emit_byte(ACPI_GPIO_REVISION_ID);
300 /* Byte 4: GpioIo or GpioInt */
301 acpigen_emit_byte(gpio->type);
304 * Byte 5-6: General Flags
305 * [15:1]: 0 => Reserved
306 * [0]: 1 => ResourceConsumer
308 acpigen_emit_word(1 << 0);
310 switch (gpio->type) {
311 case ACPI_GPIO_TYPE_INTERRUPT:
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)
320 if (gpio->irq.mode == ACPI_IRQ_EDGE_TRIGGERED)
321 flags |= 1 << 0;
322 if (gpio->irq.shared == ACPI_IRQ_SHARED)
323 flags |= 1 << 3;
324 if (gpio->irq.wake == ACPI_IRQ_WAKE)
325 flags |= 1 << 4;
327 switch (gpio->irq.polarity) {
328 case ACPI_IRQ_ACTIVE_HIGH:
329 flags |= 0 << 1;
330 break;
331 case ACPI_IRQ_ACTIVE_LOW:
332 flags |= 1 << 1;
333 break;
334 case ACPI_IRQ_ACTIVE_BOTH:
335 flags |= 2 << 1;
336 break;
338 break;
340 case ACPI_GPIO_TYPE_IO:
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
352 flags |= gpio->io_restrict & 3;
353 if (gpio->io_shared)
354 flags |= 1 << 3;
355 break;
357 acpigen_emit_word(flags);
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
367 acpigen_emit_byte(gpio->pull);
369 /* Byte 10-11: Output Drive Strength in 1/100 mA */
370 acpigen_emit_word(gpio->output_drive_strength);
372 /* Byte 12-13: Debounce Timeout in 1/100 ms */
373 acpigen_emit_word(gpio->interrupt_debounce_timeout);
375 /* Byte 14-15: Pin Table Offset, relative to start */
376 pin_table_offset = acpi_device_write_zero_len();
378 /* Byte 16: Reserved */
379 acpigen_emit_byte(0);
381 /* Byte 17-18: Resource Source Name Offset, relative to start */
382 resource_offset = acpi_device_write_zero_len();
384 /* Byte 19-20: Vendor Data Offset, relative to start */
385 vendor_data_offset = acpi_device_write_zero_len();
387 /* Byte 21-22: Vendor Data Length */
388 acpigen_emit_word(0);
390 /* Fill in Pin Table Offset */
391 acpi_device_fill_from_len(pin_table_offset, start);
393 /* Pin Table, one word for each pin */
394 for (pin = 0; pin < gpio->pin_count; pin++) {
395 uint16_t acpi_pin = gpio->pins[pin];
396 #if CONFIG(GENERIC_GPIO_LIB)
397 acpi_pin = gpio_acpi_pin(acpi_pin);
398 #endif
399 acpigen_emit_word(acpi_pin);
402 /* Fill in Resource Source Name Offset */
403 acpi_device_fill_from_len(resource_offset, start);
405 /* Resource Source Name String */
406 #if CONFIG(GENERIC_GPIO_LIB)
407 acpigen_emit_string(gpio->resource ? : gpio_acpi_path(gpio->pins[0]));
408 #else
409 acpigen_emit_string(gpio->resource);
410 #endif
412 /* Fill in Vendor Data Offset */
413 acpi_device_fill_from_len(vendor_data_offset, start);
415 /* Fill in GPIO Descriptor Length (account for len word) */
416 acpi_device_fill_len(desc_length);
419 /* ACPI 6.1 section 6.4.3.8.2.1 - I2cSerialBus() */
420 void acpi_device_write_i2c(const struct acpi_i2c *i2c)
422 void *desc_length, *type_length;
424 /* Byte 0: Descriptor Type */
425 acpigen_emit_byte(ACPI_DESCRIPTOR_SERIAL_BUS);
427 /* Byte 1+2: Length (filled in later) */
428 desc_length = acpi_device_write_zero_len();
430 /* Byte 3: Revision ID */
431 acpigen_emit_byte(ACPI_I2C_SERIAL_BUS_REVISION_ID);
433 /* Byte 4: Resource Source Index is Reserved */
434 acpigen_emit_byte(0);
436 /* Byte 5: Serial Bus Type is I2C */
437 acpigen_emit_byte(ACPI_SERIAL_BUS_TYPE_I2C);
440 * Byte 6: Flags
441 * [7:2]: 0 => Reserved
442 * [1]: 1 => ResourceConsumer
443 * [0]: 0 => ControllerInitiated
445 acpigen_emit_byte(1 << 1);
448 * Byte 7-8: Type Specific Flags
449 * [15:1]: 0 => Reserved
450 * [0]: 0 => 7bit, 1 => 10bit
452 acpigen_emit_word(i2c->mode_10bit);
454 /* Byte 9: Type Specific Revision ID */
455 acpigen_emit_byte(ACPI_I2C_TYPE_SPECIFIC_REVISION_ID);
457 /* Byte 10-11: I2C Type Data Length */
458 type_length = acpi_device_write_zero_len();
460 /* Byte 12-15: I2C Bus Speed */
461 acpigen_emit_dword(i2c->speed);
463 /* Byte 16-17: I2C Slave Address */
464 acpigen_emit_word(i2c->address);
466 /* Fill in Type Data Length */
467 acpi_device_fill_len(type_length);
469 /* Byte 18+: ResourceSource */
470 acpigen_emit_string(i2c->resource);
472 /* Fill in I2C Descriptor Length */
473 acpi_device_fill_len(desc_length);
476 /* ACPI 6.1 section 6.4.3.8.2.2 - SpiSerialBus() */
477 void acpi_device_write_spi(const struct acpi_spi *spi)
479 void *desc_length, *type_length;
480 uint16_t flags = 0;
482 /* Byte 0: Descriptor Type */
483 acpigen_emit_byte(ACPI_DESCRIPTOR_SERIAL_BUS);
485 /* Byte 1+2: Length (filled in later) */
486 desc_length = acpi_device_write_zero_len();
488 /* Byte 3: Revision ID */
489 acpigen_emit_byte(ACPI_SPI_SERIAL_BUS_REVISION_ID);
491 /* Byte 4: Resource Source Index is Reserved */
492 acpigen_emit_byte(0);
494 /* Byte 5: Serial Bus Type is SPI */
495 acpigen_emit_byte(ACPI_SERIAL_BUS_TYPE_SPI);
498 * Byte 6: Flags
499 * [7:2]: 0 => Reserved
500 * [1]: 1 => ResourceConsumer
501 * [0]: 0 => ControllerInitiated
503 acpigen_emit_byte(1 << 1);
506 * Byte 7-8: Type Specific Flags
507 * [15:2]: 0 => Reserved
508 * [1]: 0 => ActiveLow, 1 => ActiveHigh
509 * [0]: 0 => FourWire, 1 => ThreeWire
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);
517 /* Byte 9: Type Specific Revision ID */
518 acpigen_emit_byte(ACPI_SPI_TYPE_SPECIFIC_REVISION_ID);
520 /* Byte 10-11: SPI Type Data Length */
521 type_length = acpi_device_write_zero_len();
523 /* Byte 12-15: Connection Speed */
524 acpigen_emit_dword(spi->speed);
526 /* Byte 16: Data Bit Length */
527 acpigen_emit_byte(spi->data_bit_length);
529 /* Byte 17: Clock Phase */
530 acpigen_emit_byte(spi->clock_phase);
532 /* Byte 18: Clock Polarity */
533 acpigen_emit_byte(spi->clock_polarity);
535 /* Byte 19-20: Device Selection */
536 acpigen_emit_word(spi->device_select);
538 /* Fill in Type Data Length */
539 acpi_device_fill_len(type_length);
541 /* Byte 21+: ResourceSource String */
542 acpigen_emit_string(spi->resource);
544 /* Fill in SPI Descriptor Length */
545 acpi_device_fill_len(desc_length);
548 /* UART Serial Bus - UARTSerialBusV2() */
549 void acpi_device_write_uart(const struct acpi_uart *uart)
551 void *desc_length, *type_length;
552 uint16_t flags;
554 /* Byte 0: Descriptor Type */
555 acpigen_emit_byte(ACPI_DESCRIPTOR_SERIAL_BUS);
557 /* Byte 1+2: Length (filled in later) */
558 desc_length = acpi_device_write_zero_len();
560 /* Byte 3: Revision ID */
561 acpigen_emit_byte(ACPI_UART_SERIAL_BUS_REVISION_ID);
563 /* Byte 4: Resource Source Index is Reserved */
564 acpigen_emit_byte(0);
566 /* Byte 5: Serial Bus Type is UART */
567 acpigen_emit_byte(ACPI_SERIAL_BUS_TYPE_UART);
570 * Byte 6: Flags
571 * [7:2]: 0 => Reserved
572 * [1]: 1 => ResourceConsumer
573 * [0]: 0 => ControllerInitiated
575 acpigen_emit_byte(BIT(1));
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
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);
591 /* Byte 9: Type Specific Revision ID */
592 acpigen_emit_byte(ACPI_UART_TYPE_SPECIFIC_REVISION_ID);
594 /* Byte 10-11: Type Data Length */
595 type_length = acpi_device_write_zero_len();
597 /* Byte 12-15: Initial Baud Rate */
598 acpigen_emit_dword(uart->initial_baud_rate);
600 /* Byte 16-17: RX FIFO size */
601 acpigen_emit_word(uart->rx_fifo_bytes);
603 /* Byte 18-19: TX FIFO size */
604 acpigen_emit_word(uart->tx_fifo_bytes);
606 /* Byte 20: Parity */
607 acpigen_emit_byte(uart->parity);
609 /* Byte 21: Lines Enabled */
610 acpigen_emit_byte(uart->lines_in_use);
612 /* Fill in Type Data Length */
613 acpi_device_fill_len(type_length);
615 /* Byte 22+: ResourceSource */
616 acpigen_emit_string(uart->resource);
618 /* Fill in Descriptor Length */
619 acpi_device_fill_len(desc_length);
622 #define ACPI_POWER_RESOURCE_STATUS_ON_OP ONE_OP
623 #define ACPI_POWER_RESOURCE_STATUS_OFF_OP ZERO_OP
626 * Writes an ACPI fragment that will check the GPIO and return 0 if the GPIO
627 * state does not match the active parameter.
629 static void acpigen_write_gpio_STA(const struct acpi_gpio *gpio, bool active)
631 if (!gpio || !gpio->pin_count)
632 return;
634 /* Read current GPIO status into Local0. */
635 acpigen_get_tx_gpio(gpio);
638 * If (!Local0)
640 * Return (Zero)
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();
651 static void acpigen_write_power_res_STA(const struct acpi_power_res_params *params)
653 acpigen_write_method_serialized("_STA", 0);
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);
660 /* All GPIOs are in the ON state */
661 acpigen_write_return_op(ACPI_POWER_RESOURCE_STATUS_ON_OP);
663 acpigen_pop_len(); /* Method */
666 /* PowerResource() with Enable and/or Reset control */
667 void acpi_device_add_power_res(const struct acpi_power_res_params *params)
669 static uint8_t id;
670 static const char * const power_res_dev_states[] = { "_PR0", "_PR3" };
671 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;
674 char pr_name[ACPI_NAME_BUFFER_SIZE];
676 if (!reset_gpio && !enable_gpio && !stop_gpio)
677 return;
679 snprintf(pr_name, sizeof(pr_name), "PR%02X", id++);
681 /* PowerResource (PR##, 0, 0) */
682 acpigen_write_power_res(pr_name, 0, 0, power_res_dev_states,
683 ARRAY_SIZE(power_res_dev_states));
685 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);
692 /* Method (_ON, 0, Serialized) */
693 acpigen_write_method_serialized("_ON", 0);
694 /* 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();
707 if (reset_gpio)
708 acpigen_enable_tx_gpio(params->reset_gpio);
709 if (enable_gpio) {
710 acpigen_enable_tx_gpio(params->enable_gpio);
711 if (params->enable_delay_ms)
712 acpigen_write_sleep(params->enable_delay_ms);
714 if (reset_gpio) {
715 acpigen_disable_tx_gpio(params->reset_gpio);
716 if (params->reset_delay_ms)
717 acpigen_write_sleep(params->reset_delay_ms);
719 if (stop_gpio) {
720 acpigen_disable_tx_gpio(params->stop_gpio);
721 if (params->stop_delay_ms)
722 acpigen_write_sleep(params->stop_delay_ms);
724 acpigen_pop_len(); /* _ON method */
726 /* Method (_OFF, 0, Serialized) */
727 acpigen_write_method_serialized("_OFF", 0);
728 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);
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);
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);
743 acpigen_pop_len(); /* _OFF method */
745 acpigen_pop_len(); /* PowerResource PR## */
748 static void acpi_dp_write_array(const struct acpi_dp *array);
749 static void acpi_dp_write_value(const struct acpi_dp *prop)
751 switch (prop->type) {
752 case ACPI_DP_TYPE_INTEGER:
753 acpigen_write_integer(prop->integer);
754 break;
755 case ACPI_DP_TYPE_STRING:
756 case ACPI_DP_TYPE_CHILD:
757 acpigen_write_string(prop->string);
758 break;
759 case ACPI_DP_TYPE_REFERENCE:
760 acpigen_emit_namestring(prop->string);
761 break;
762 case ACPI_DP_TYPE_ARRAY:
763 acpi_dp_write_array(prop->array);
764 break;
765 default:
766 break;
770 /* Package (2) { "prop->name", VALUE } */
771 static void acpi_dp_write_property(const struct acpi_dp *prop)
773 acpigen_write_package(2);
774 acpigen_write_string(prop->name);
775 acpi_dp_write_value(prop);
776 acpigen_pop_len();
779 /* Write array of Device Properties */
780 static void acpi_dp_write_array(const struct acpi_dp *array)
782 const struct acpi_dp *dp;
783 char *pkg_count;
785 /* Package element count determined as it is populated */
786 pkg_count = acpigen_write_package(0);
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.
793 for (dp = array->next; dp; dp = dp->next) {
794 acpi_dp_write_value(dp);
795 (*pkg_count)++;
798 acpigen_pop_len();
801 static void acpi_dp_free(struct acpi_dp *dp)
803 while (dp) {
804 struct acpi_dp *p = dp->next;
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;
817 free(dp);
818 dp = p;
822 static bool acpi_dp_write_properties(struct acpi_dp *prop, const char *uuid)
824 struct acpi_dp *dp;
825 char *prop_count = NULL;
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;
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.
840 if (!prop_count) {
841 /* ToUUID (dp->uuid) */
842 acpigen_write_uuid(uuid);
844 * Package (PROP), element count determined as
845 * it is populated
847 prop_count = acpigen_write_package(0);
849 (*prop_count)++;
850 acpi_dp_write_property(dp);
852 if (prop_count) {
853 /* Package (PROP) length, if a package was written */
854 acpigen_pop_len();
855 return true;
857 return false;
860 static void acpi_dp_write_(struct acpi_dp *table)
862 struct acpi_dp *dp, *prop;
863 char *dp_count;
864 int child_count = 0;
866 if (!table || table->type != ACPI_DP_TYPE_TABLE || !table->next)
867 return;
869 /* Name (name) */
870 acpigen_write_name(table->name);
872 /* Device Property list starts with the next entry */
873 prop = table->next;
875 /* Package (DP), default to assuming no properties or children */
876 dp_count = acpigen_write_package(0);
878 /* Print base properties */
879 if (acpi_dp_write_properties(prop, table->uuid))
880 *dp_count += 2;
882 /* Count child properties */
883 for (dp = prop; dp; dp = dp->next)
884 if (dp->type == ACPI_DP_TYPE_CHILD)
885 child_count++;
887 /* Add child properties to the base table */
888 if (child_count) {
889 /* Update DP package count */
890 *dp_count += 2;
891 /* ToUUID (ACPI_DP_CHILD_UUID) */
892 acpigen_write_uuid(ACPI_DP_CHILD_UUID);
894 /* Print child pointer properties */
895 acpigen_write_package(child_count);
897 for (dp = prop; dp; dp = dp->next)
898 if (dp->type == ACPI_DP_TYPE_CHILD)
899 acpi_dp_write_property(dp);
900 /* Package (CHILD) length */
901 acpigen_pop_len();
904 /* 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;
910 /* Package (DP) length */
911 acpigen_pop_len();
913 /* Recursively parse children into separate tables */
914 for (dp = prop; dp; dp = dp->next)
915 if (dp->type == ACPI_DP_TYPE_CHILD)
916 acpi_dp_write_(dp->child);
919 void acpi_dp_write(struct acpi_dp *table)
921 acpi_dp_write_(table);
923 /* Clean up */
924 acpi_dp_free(table);
927 static struct acpi_dp *acpi_dp_new(struct acpi_dp *dp, enum acpi_dp_type type,
928 const char *name)
930 struct acpi_dp *new;
932 new = malloc(sizeof(struct acpi_dp));
933 if (!new)
934 return NULL;
936 memset(new, 0, sizeof(*new));
937 new->type = type;
938 new->name = name;
939 new->uuid = ACPI_DP_UUID;
941 if (dp) {
942 /* Add to end of property list */
943 while (dp->next)
944 dp = dp->next;
945 dp->next = new;
948 return new;
951 struct acpi_dp *acpi_dp_new_table(const char *name)
953 return acpi_dp_new(NULL, ACPI_DP_TYPE_TABLE, name);
956 size_t acpi_dp_add_property_list(struct acpi_dp *dp,
957 const struct acpi_dp *property_list,
958 size_t property_count)
960 const struct acpi_dp *prop;
961 size_t i, properties_added = 0;
963 if (!dp || !property_list)
964 return 0;
966 for (i = 0; i < property_count; i++) {
967 prop = &property_list[i];
969 if (prop->type == ACPI_DP_TYPE_UNKNOWN || !prop->name)
970 continue;
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;
992 ++properties_added;
995 return properties_added;
998 struct acpi_dp *acpi_dp_add_integer(struct acpi_dp *dp, const char *name,
999 uint64_t value)
1001 if (!dp)
1002 return NULL;
1004 struct acpi_dp *new = acpi_dp_new(dp, ACPI_DP_TYPE_INTEGER, name);
1006 if (new)
1007 new->integer = value;
1009 return new;
1012 struct acpi_dp *acpi_dp_add_string(struct acpi_dp *dp, const char *name,
1013 const char *string)
1015 if (!dp)
1016 return NULL;
1018 struct acpi_dp *new = acpi_dp_new(dp, ACPI_DP_TYPE_STRING, name);
1020 if (new)
1021 new->string = string;
1023 return new;
1026 struct acpi_dp *acpi_dp_add_reference(struct acpi_dp *dp, const char *name,
1027 const char *reference)
1029 if (!dp)
1030 return NULL;
1032 struct acpi_dp *new = acpi_dp_new(dp, ACPI_DP_TYPE_REFERENCE, name);
1034 if (new)
1035 new->string = reference;
1037 return new;
1040 struct acpi_dp *acpi_dp_add_child(struct acpi_dp *dp, const char *name,
1041 struct acpi_dp *child)
1043 struct acpi_dp *new;
1045 if (!dp || !child || child->type != ACPI_DP_TYPE_TABLE)
1046 return NULL;
1048 new = acpi_dp_new(dp, ACPI_DP_TYPE_CHILD, name);
1049 if (new) {
1050 new->child = child;
1051 new->string = child->name;
1054 return new;
1057 struct acpi_dp *acpi_dp_add_package(struct acpi_dp *dp, struct acpi_dp *package)
1059 struct acpi_dp *new;
1061 if (!dp || !package || package->type != ACPI_DP_TYPE_TABLE)
1062 return NULL;
1064 new = acpi_dp_new(dp, ACPI_DP_TYPE_PACKAGE, NULL);
1065 if (new) {
1066 new->uuid = package->name;
1067 new->child = package;
1070 return new;
1073 struct acpi_dp *acpi_dp_add_array(struct acpi_dp *dp, struct acpi_dp *array)
1075 struct acpi_dp *new;
1077 if (!dp || !array || array->type != ACPI_DP_TYPE_TABLE)
1078 return NULL;
1080 new = acpi_dp_new(dp, ACPI_DP_TYPE_ARRAY, array->name);
1081 if (new)
1082 new->array = array;
1084 return new;
1087 struct acpi_dp *acpi_dp_add_integer_array(struct acpi_dp *dp, const char *name,
1088 const uint64_t *array, int len)
1090 struct acpi_dp *dp_array;
1091 int i;
1093 if (!dp || len <= 0)
1094 return NULL;
1096 dp_array = acpi_dp_new_table(name);
1097 if (!dp_array)
1098 return NULL;
1100 for (i = 0; i < len; i++)
1101 if (!acpi_dp_add_integer(dp_array, NULL, array[i]))
1102 break;
1104 acpi_dp_add_array(dp, dp_array);
1106 return dp_array;
1109 struct 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)
1113 struct acpi_dp *gpio;
1114 uint32_t i;
1116 if (!dp || !param_count)
1117 return NULL;
1119 gpio = acpi_dp_new_table(name);
1120 if (!gpio)
1121 return NULL;
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 * ...
1134 for (i = 0; i < param_count; i++, params++) {
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.
1139 if (!params->ref) {
1140 acpi_dp_add_integer(gpio, NULL, 0);
1141 continue;
1144 /* The device that has _CRS containing GpioIO()/GpioInt() */
1145 acpi_dp_add_reference(gpio, NULL, params->ref);
1147 /* Index of the GPIO resource in _CRS starting from zero */
1148 acpi_dp_add_integer(gpio, NULL, params->index);
1150 /* Pin in the GPIO resource, typically zero */
1151 acpi_dp_add_integer(gpio, NULL, params->pin);
1153 /* Set if pin is active low */
1154 acpi_dp_add_integer(gpio, NULL, params->active_low);
1156 acpi_dp_add_array(dp, gpio);
1158 return gpio;
1163 struct 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)
1167 struct acpi_gpio_res_params param = {
1168 .ref = ref,
1169 .index = index,
1170 .pin = pin,
1171 .active_low = active_low,
1174 return acpi_dp_add_gpio_array(dp, name, &param, 1);
1178 * This function writes a PCI device with _ADR object:
1179 * Example:
1180 * Scope (\_SB.PCI0)
1182 * Device (IGFX)
1184 * Name (_ADR, 0x0000000000000000)
1185 * Method (_STA, 0, NotSerialized) { Return (status) }
1189 void acpi_device_write_pci_dev(const struct device *dev)
1191 const char *scope = acpi_device_scope(dev);
1192 const char *name = acpi_device_name(dev);
1194 assert(dev->path.type == DEVICE_PATH_PCI);
1195 assert(name);
1196 assert(scope);
1198 acpigen_write_scope(scope);
1199 acpigen_write_device(name);
1201 acpigen_write_ADR_pci_device(dev);
1202 acpigen_write_STA(acpi_device_status(dev));
1204 acpigen_pop_len(); /* Device */
1205 acpigen_pop_len(); /* Scope */
1209 * Helper function to add given integer property with an UUID to _DSD in the current scope.
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.
1218 static void acpi_device_add_integer_property_with_uuid(struct acpi_dp *dsd,
1219 const char *uuid,
1220 const char *name,
1221 uint64_t value)
1223 struct acpi_dp *prev_dsd = dsd, *pkg;
1224 if (prev_dsd == NULL)
1225 dsd = acpi_dp_new_table("_DSD");
1226 pkg = acpi_dp_new_table(uuid);
1227 acpi_dp_add_integer(pkg, name, value);
1228 acpi_dp_add_package(dsd, pkg);
1229 if (prev_dsd == NULL)
1230 acpi_dp_write(dsd);
1233 /* _DSD with ExternalFacingPort */
1234 void acpi_device_add_external_facing_port(struct acpi_dp *dsd)
1236 acpi_device_add_integer_property_with_uuid(dsd,
1237 ACPI_DSD_EXTERNAL_FACING_PORT_UUID,
1238 ACPI_DSD_EXTERNAL_FACING_PORT_NAME,
1242 /* _DSD with HotPlugSupportInD3 */
1243 void acpi_device_add_hotplug_support_in_d3(struct acpi_dp *dsd)
1245 acpi_device_add_integer_property_with_uuid(dsd,
1246 ACPI_DSD_HOTPLUG_IN_D3_UUID,
1247 ACPI_DSD_HOTPLUG_IN_D3_NAME,
1251 /* _DSD with DmaProperty */
1252 void acpi_device_add_dma_property(struct acpi_dp *dsd)
1254 acpi_device_add_integer_property_with_uuid(dsd,
1255 ACPI_DSD_DMA_PROPERTY_UUID,
1256 ACPI_DSD_DMA_PROPERTY_NAME,
1260 /* _DSD with StorageD3Enable */
1261 void acpi_device_add_storage_d3_enable(struct acpi_dp *dsd)
1263 acpi_device_add_integer_property_with_uuid(dsd,
1264 ACPI_DSD_STORAGE_D3_UUID,
1265 ACPI_DSD_STORAGE_D3_NAME,