summaryrefslogtreecommitdiffstats
path: root/lib/acpi/acpi_dp.c
blob: 7e3e3259d8d477585af29e91ac338445254b4e3b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
// SPDX-License-Identifier: GPL-2.0
/*
 * Generation of tables for particular device types
 *
 * Copyright 2019 Google LLC
 * Mostly taken from coreboot file acpi_device.c
 */

#include <common.h>
#include <dm.h>
#include <log.h>
#include <malloc.h>
#include <uuid.h>
#include <acpi/acpigen.h>
#include <acpi/acpi_dp.h>
#include <dm/acpi.h>

static void acpi_dp_write_array(struct acpi_ctx *ctx,
				const struct acpi_dp *array);

static void acpi_dp_write_value(struct acpi_ctx *ctx,
				const struct acpi_dp *prop)
{
	switch (prop->type) {
	case ACPI_DP_TYPE_INTEGER:
		acpigen_write_integer(ctx, prop->integer);
		break;
	case ACPI_DP_TYPE_STRING:
	case ACPI_DP_TYPE_CHILD:
		acpigen_write_string(ctx, prop->string);
		break;
	case ACPI_DP_TYPE_REFERENCE:
		acpigen_emit_namestring(ctx, prop->string);
		break;
	case ACPI_DP_TYPE_ARRAY:
		acpi_dp_write_array(ctx, prop->array);
		break;
	default:
		break;
	}
}

/* Package (2) { "prop->name", VALUE } */
static void acpi_dp_write_property(struct acpi_ctx *ctx,
				   const struct acpi_dp *prop)
{
	acpigen_write_package(ctx, 2);
	acpigen_write_string(ctx, prop->name);
	acpi_dp_write_value(ctx, prop);
	acpigen_pop_len(ctx);
}

/* Write array of Device Properties */
static void acpi_dp_write_array(struct acpi_ctx *ctx,
				const struct acpi_dp *array)
{
	const struct acpi_dp *dp;
	char *pkg_count;

	/* Package element count determined as it is populated */
	pkg_count = acpigen_write_package(ctx, 0);

	/*
	 * Only acpi_dp of type DP_TYPE_TABLE is allowed to be an array.
	 * DP_TYPE_TABLE does not have a value to be written. Thus, start
	 * the loop from next type in the array.
	 */
	for (dp = array->next; dp; dp = dp->next) {
		acpi_dp_write_value(ctx, dp);
		(*pkg_count)++;
	}

	acpigen_pop_len(ctx);
}

static void acpi_dp_free(struct acpi_dp *dp)
{
	assert(dp);
	while (dp) {
		struct acpi_dp *p = dp->next;

		switch (dp->type) {
		case ACPI_DP_TYPE_CHILD:
			acpi_dp_free(dp->child);
			break;
		case ACPI_DP_TYPE_ARRAY:
			acpi_dp_free(dp->array);
			break;
		default:
			break;
		}

		free(dp);
		dp = p;
	}
}

static int acpi_dp_write_internal(struct acpi_ctx *ctx, struct acpi_dp *table)
{
	struct acpi_dp *dp, *prop;
	char *dp_count, *prop_count = NULL;
	int child_count = 0;
	int ret;

	assert(table);
	if (table->type != ACPI_DP_TYPE_TABLE)
		return 0;

	/* Name (name) */
	acpigen_write_name(ctx, table->name);

	/* Device Property list starts with the next entry */
	prop = table->next;

	/* Package (DP), default to assuming no properties or children */
	dp_count = acpigen_write_package(ctx, 0);

	/* Print base properties */
	for (dp = prop; dp; dp = dp->next) {
		if (dp->type == ACPI_DP_TYPE_CHILD) {
			child_count++;
		} else {
			/*
			 * The UUID and package is only added when
			 * we come across the first property.  This
			 * is to avoid creating a zero-length package
			 * in situations where there are only children.
			 */
			if (!prop_count) {
				*dp_count += 2;
				/* ToUUID (ACPI_DP_UUID) */
				ret = acpigen_write_uuid(ctx, ACPI_DP_UUID);
				if (ret)
					return log_msg_ret("touuid", ret);
				/*
				 * Package (PROP), element count determined as
				 * it is populated
				 */
				prop_count = acpigen_write_package(ctx, 0);
			}
			(*prop_count)++;
			acpi_dp_write_property(ctx, dp);
		}
	}

	if (prop_count) {
		/* Package (PROP) length, if a package was written */
		acpigen_pop_len(ctx);
	}

	if (child_count) {
		/* Update DP package count to 2 or 4 */
		*dp_count += 2;
		/* ToUUID (ACPI_DP_CHILD_UUID) */
		ret = acpigen_write_uuid(ctx, ACPI_DP_CHILD_UUID);
		if (ret)
			return log_msg_ret("child uuid", ret);

		/* Print child pointer properties */
		acpigen_write_package(ctx, child_count);

		for (dp = prop; dp; dp = dp->next)
			if (dp->type == ACPI_DP_TYPE_CHILD)
				acpi_dp_write_property(ctx, dp);
		/* Package (CHILD) length */
		acpigen_pop_len(ctx);
	}

	/* Package (DP) length */
	acpigen_pop_len(ctx);

	/* Recursively parse children into separate tables */
	for (dp = prop; dp; dp = dp->next) {
		if (dp->type == ACPI_DP_TYPE_CHILD) {
			ret = acpi_dp_write_internal(ctx, dp->child);
			if (ret)
				return log_msg_ret("dp child", ret);
		}
	}

	return 0;
}

int acpi_dp_write(struct acpi_ctx *ctx, struct acpi_dp *table)
{
	int ret;

	ret = acpi_dp_write_internal(ctx, table);

	/* Clean up */
	acpi_dp_free(table);

	if (ret)
		return log_msg_ret("write", ret);

	return 0;
}

static struct acpi_dp *acpi_dp_new(struct acpi_dp *dp, enum acpi_dp_type type,
				   const char *name)
{
	struct acpi_dp *new;

	new = malloc(sizeof(struct acpi_dp));
	if (!new)
		return NULL;

	memset(new, '\0', sizeof(*new));
	new->type = type;
	new->name = name;

	if (dp) {
		/* Add to end of property list */
		while (dp->next)
			dp = dp->next;
		dp->next = new;
	}

	return new;
}

struct acpi_dp *acpi_dp_new_table(const char *name)
{
	return acpi_dp_new(NULL, ACPI_DP_TYPE_TABLE, name);
}

struct acpi_dp *acpi_dp_add_integer(struct acpi_dp *dp, const char *name,
				    u64 value)
{
	struct acpi_dp *new;

	assert(dp);
	new = acpi_dp_new(dp, ACPI_DP_TYPE_INTEGER, name);

	if (new)
		new->integer = value;

	return new;
}

struct acpi_dp *acpi_dp_add_string(struct acpi_dp *dp, const char *name,
				   const char *string)
{
	struct acpi_dp *new;

	assert(dp);
	new = acpi_dp_new(dp, ACPI_DP_TYPE_STRING, name);
	if (new)
		new->string = string;

	return new;
}

struct acpi_dp *acpi_dp_add_reference(struct acpi_dp *dp, const char *name,
				      const char *reference)
{
	struct acpi_dp *new;

	assert(dp);
	new = acpi_dp_new(dp, ACPI_DP_TYPE_REFERENCE, name);
	if (new)
		new->string = reference;

	return new;
}

struct acpi_dp *acpi_dp_add_child(struct acpi_dp *dp, const char *name,
				  struct acpi_dp *child)
{
	struct acpi_dp *new;

	assert(dp);
	if (child->type != ACPI_DP_TYPE_TABLE)
		return NULL;

	new = acpi_dp_new(dp, ACPI_DP_TYPE_CHILD, name);
	if (new) {
		new->child = child;
		new->string = child->name;
	}

	return new;
}

struct acpi_dp *acpi_dp_add_array(struct acpi_dp *dp, struct acpi_dp *array)
{
	struct acpi_dp *new;

	assert(dp);
	assert(array);
	if (array->type != ACPI_DP_TYPE_TABLE)
		return NULL;

	new = acpi_dp_new(dp, ACPI_DP_TYPE_ARRAY, array->name);
	if (new)
		new->array = array;

	return new;
}

struct acpi_dp *acpi_dp_add_integer_array(struct acpi_dp *dp, const char *name,
					  u64 *array, int len)
{
	struct acpi_dp *dp_array;
	int i;

	assert(dp);
	if (len <= 0)
		return NULL;

	dp_array = acpi_dp_new_table(name);
	if (!dp_array)
		return NULL;

	for (i = 0; i < len; i++)
		if (!acpi_dp_add_integer(dp_array, NULL, array[i]))
			break;

	if (!acpi_dp_add_array(dp, dp_array))
		return NULL;

	return dp_array;
}

struct acpi_dp *acpi_dp_add_gpio(struct acpi_dp *dp, const char *name,
				 const char *ref, int index, int pin,
				 enum acpi_gpio_polarity polarity)
{
	struct acpi_dp *gpio;

	assert(dp);
	gpio = acpi_dp_new_table(name);
	if (!gpio)
		return NULL;

	if (!acpi_dp_add_reference(gpio, NULL, ref) ||
	    !acpi_dp_add_integer(gpio, NULL, index) ||
	    !acpi_dp_add_integer(gpio, NULL, pin) ||
	    !acpi_dp_add_integer(gpio, NULL, polarity == ACPI_GPIO_ACTIVE_LOW))
		return NULL;

	if (!acpi_dp_add_array(dp, gpio))
		return NULL;

	return gpio;
}

int acpi_dp_ofnode_copy_int(ofnode node, struct acpi_dp *dp, const char *prop)
{
	int ret;
	u32 val = 0;

	ret = ofnode_read_u32(node, prop, &val);
	if (ret)
		return ret;
	if (!acpi_dp_add_integer(dp, prop, val))
		return log_ret(-ENOMEM);

	return 0;
}

int acpi_dp_ofnode_copy_str(ofnode node, struct acpi_dp *dp, const char *prop)
{
	const char *val;

	val = ofnode_read_string(node, prop);
	if (!val)
		return -EINVAL;
	if (!acpi_dp_add_string(dp, prop, val))
		return log_ret(-ENOMEM);

	return 0;
}

int acpi_dp_dev_copy_int(const struct udevice *dev, struct acpi_dp *dp,
			 const char *prop)
{
	int ret;
	u32 val = 0;

	ret = dev_read_u32(dev, prop, &val);
	if (ret)
		return ret;
	if (!acpi_dp_add_integer(dp, prop, val))
		return log_ret(-ENOMEM);

	return ret;
}

int acpi_dp_dev_copy_str(const struct udevice *dev, struct acpi_dp *dp,
			 const char *prop)
{
	const char *val;

	val = dev_read_string(dev, prop);
	if (!val)
		return -EINVAL;
	if (!acpi_dp_add_string(dp, prop, val))
		return log_ret(-ENOMEM);

	return 0;
}