blob: d854a45cbc6d83eb0368bff139130fe57a1fad4b [file] [log] [blame]
Simon Glass1361a532020-07-07 13:11:39 -06001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Generation of tables for particular device types
4 *
5 * Copyright 2019 Google LLC
6 * Mostly taken from coreboot file of the same name
7 */
8
9#include <common.h>
10#include <dm.h>
Simon Glassff715c62020-07-07 13:11:43 -060011#include <irq.h>
Simon Glass1361a532020-07-07 13:11:39 -060012#include <log.h>
13#include <acpi/acpi_device.h>
Simon Glassff715c62020-07-07 13:11:43 -060014#include <acpi/acpigen.h>
Simon Glass1361a532020-07-07 13:11:39 -060015#include <dm/acpi.h>
16
17/**
18 * acpi_device_path_fill() - Find the root device and build a path from there
19 *
20 * This recursively reaches back to the root device and progressively adds path
21 * elements until the device is reached.
22 *
23 * @dev: Device to return path of
24 * @buf: Buffer to hold the path
25 * @buf_len: Length of buffer
26 * @cur: Current position in the buffer
27 * @return new position in buffer after adding @dev, or -ve on error
28 */
29static int acpi_device_path_fill(const struct udevice *dev, char *buf,
30 size_t buf_len, int cur)
31{
32 char name[ACPI_NAME_MAX];
33 int next = 0;
34 int ret;
35
36 ret = acpi_get_name(dev, name);
37 if (ret)
38 return ret;
39
40 /*
41 * Make sure this name segment will fit, including the path segment
42 * separator and possible NULL terminator, if this is the last segment.
43 */
44 if (cur + strlen(name) + 2 > buf_len)
45 return -ENOSPC;
46
47 /* Walk up the tree to the root device */
48 if (dev_get_parent(dev)) {
49 next = acpi_device_path_fill(dev_get_parent(dev), buf, buf_len,
50 cur);
51 if (next < 0)
52 return next;
53 }
54
55 /* Fill in the path from the root device */
56 next += snprintf(buf + next, buf_len - next, "%s%s",
57 dev_get_parent(dev) && *name ? "." : "", name);
58
59 return next;
60}
61
62int acpi_device_path(const struct udevice *dev, char *buf, int maxlen)
63{
64 int ret;
65
66 ret = acpi_device_path_fill(dev, buf, maxlen, 0);
67 if (ret < 0)
68 return ret;
69
70 return 0;
71}
72
73int acpi_device_scope(const struct udevice *dev, char *scope, int maxlen)
74{
75 int ret;
76
77 if (!dev_get_parent(dev))
78 return log_msg_ret("noparent", -EINVAL);
79
80 ret = acpi_device_path_fill(dev_get_parent(dev), scope, maxlen, 0);
81 if (ret < 0)
82 return log_msg_ret("fill", ret);
83
84 return 0;
85}
Simon Glass2715b362020-07-07 13:11:40 -060086
87enum acpi_dev_status acpi_device_status(const struct udevice *dev)
88{
89 return ACPI_DSTATUS_ALL_ON;
90}
Simon Glassff715c62020-07-07 13:11:43 -060091
92/**
93 * largeres_write_len_f() - Write a placeholder word value
94 *
95 * Write a forward length for a large resource (2 bytes)
96 *
97 * @return pointer to the zero word (for fixing up later)
98 */
99static void *largeres_write_len_f(struct acpi_ctx *ctx)
100{
101 u8 *p = acpigen_get_current(ctx);
102
103 acpigen_emit_word(ctx, 0);
104
105 return p;
106}
107
108/**
109 * largeres_fill_from_len() - Fill in a length value
110 *
111 * This calculated the number of bytes since the provided @start and writes it
112 * to @ptr, which was previous returned by largeres_write_len_f().
113 *
114 * @ptr: Word to update
115 * @start: Start address to count from to calculated the length
116 */
117static void largeres_fill_from_len(struct acpi_ctx *ctx, char *ptr, u8 *start)
118{
119 u16 len = acpigen_get_current(ctx) - start;
120
121 ptr[0] = len & 0xff;
122 ptr[1] = (len >> 8) & 0xff;
123}
124
125/**
126 * largeres_fill_len() - Fill in a length value, excluding the length itself
127 *
128 * Fill in the length field with the value calculated from after the 16bit
129 * field to acpigen current. This is useful since the length value does not
130 * include the length field itself.
131 *
132 * This calls acpi_device_largeres_fill_len() passing @ptr + 2 as @start
133 *
134 * @ptr: Word to update.
135 */
136static void largeres_fill_len(struct acpi_ctx *ctx, void *ptr)
137{
138 largeres_fill_from_len(ctx, ptr, ptr + sizeof(u16));
139}
140
141/* ACPI 6.3 section 6.4.3.6: Extended Interrupt Descriptor */
142static int acpi_device_write_interrupt(struct acpi_ctx *ctx,
143 const struct acpi_irq *irq)
144{
145 void *desc_length;
146 u8 flags;
147
148 if (!irq->pin)
149 return -ENOENT;
150
151 /* This is supported by GpioInt() but not Interrupt() */
152 if (irq->polarity == ACPI_IRQ_ACTIVE_BOTH)
153 return -EINVAL;
154
155 /* Byte 0: Descriptor Type */
156 acpigen_emit_byte(ctx, ACPI_DESCRIPTOR_INTERRUPT);
157
158 /* Byte 1-2: Length (filled in later) */
159 desc_length = largeres_write_len_f(ctx);
160
161 /*
162 * Byte 3: Flags
163 * [7:5]: Reserved
164 * [4]: Wake (0=NO_WAKE 1=WAKE)
165 * [3]: Sharing (0=EXCLUSIVE 1=SHARED)
166 * [2]: Polarity (0=HIGH 1=LOW)
167 * [1]: Mode (0=LEVEL 1=EDGE)
168 * [0]: Resource (0=PRODUCER 1=CONSUMER)
169 */
170 flags = BIT(0); /* ResourceConsumer */
171 if (irq->mode == ACPI_IRQ_EDGE_TRIGGERED)
172 flags |= BIT(1);
173 if (irq->polarity == ACPI_IRQ_ACTIVE_LOW)
174 flags |= BIT(2);
175 if (irq->shared == ACPI_IRQ_SHARED)
176 flags |= BIT(3);
177 if (irq->wake == ACPI_IRQ_WAKE)
178 flags |= BIT(4);
179 acpigen_emit_byte(ctx, flags);
180
181 /* Byte 4: Interrupt Table Entry Count */
182 acpigen_emit_byte(ctx, 1);
183
184 /* Byte 5-8: Interrupt Number */
185 acpigen_emit_dword(ctx, irq->pin);
186
187 /* Fill in Descriptor Length (account for len word) */
188 largeres_fill_len(ctx, desc_length);
189
190 return 0;
191}
192
193int acpi_device_write_interrupt_irq(struct acpi_ctx *ctx,
194 const struct irq *req_irq)
195{
196 struct acpi_irq irq;
197 int ret;
198
199 ret = irq_get_acpi(req_irq, &irq);
200 if (ret)
201 return log_msg_ret("get", ret);
202 ret = acpi_device_write_interrupt(ctx, &irq);
203 if (ret)
204 return log_msg_ret("write", ret);
205
206 return 0;
207}