]> git.ipfire.org Git - thirdparty/u-boot.git/blame - arch/x86/lib/acpi_nhlt.c
x86: nhlt: Correct output of bytes and 16-bit data
[thirdparty/u-boot.git] / arch / x86 / lib / acpi_nhlt.c
CommitLineData
7f926c96
SG
1// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright 2020 Google LLC
4 *
5 * Modified from coreboot nhlt.c
6 */
7
8#define LOG_CATEGORY LOGC_ACPI
9
10#include <common.h>
11#include <binman.h>
12#include <dm.h>
13#include <log.h>
14#include <malloc.h>
15#include <tables_csum.h>
16#include <acpi/acpi_table.h>
17#include <asm/acpi_nhlt.h>
18#include <asm/unaligned.h>
19#include <dm/acpi.h>
20
21#define NHLT_RID 1
22#define NHLT_SSID 1
23#define WAVEFORMAT_TAG 0xfffe
24#define DEFAULT_VIRTUAL_BUS_ID 0
25
26static const struct sub_format pcm_subformat = {
27 .data1 = 0x00000001,
28 .data2 = 0x0000,
29 .data3 = 0x0010,
30 .data4 = { 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 },
31};
32
33struct nhlt *nhlt_init(void)
34{
35 struct nhlt *nhlt;
36
37 nhlt = malloc(sizeof(*nhlt));
38
39 if (!nhlt)
40 return NULL;
41
42 memset(nhlt, 0, sizeof(*nhlt));
43 nhlt->subsystem_id = NHLT_SSID;
44
45 return nhlt;
46}
47
48struct nhlt_endpoint *nhlt_add_endpoint(struct nhlt *nhlt, int link_type,
49 int device_type, int dir,
50 u16 vid, u16 did)
51{
52 struct nhlt_endpoint *endp;
53
54 if (link_type < NHLT_LINK_HDA || link_type >= NHLT_MAX_LINK_TYPES)
55 return NULL;
56
57 if (nhlt->num_endpoints >= MAX_ENDPOINTS)
58 return NULL;
59
60 endp = &nhlt->endpoints[nhlt->num_endpoints];
61
62 endp->link_type = link_type;
63 endp->instance_id = nhlt->current_instance_id[link_type];
64 endp->vendor_id = vid;
65 endp->device_id = did;
66 endp->revision_id = NHLT_RID;
67 endp->subsystem_id = nhlt->subsystem_id;
68 endp->device_type = device_type;
69 endp->direction = dir;
70 endp->virtual_bus_id = DEFAULT_VIRTUAL_BUS_ID;
71
72 nhlt->num_endpoints++;
73
74 return endp;
75}
76
77static int append_specific_config(struct nhlt_specific_config *spec_cfg,
78 const void *config, size_t config_sz)
79{
80 size_t new_sz;
81 void *new_cfg;
82
83 new_sz = spec_cfg->size + config_sz;
84 new_cfg = malloc(new_sz);
85 if (!new_cfg)
86 return -ENOMEM;
87
88 /* Append new config */
89 memcpy(new_cfg, spec_cfg->capabilities, spec_cfg->size);
90 memcpy(new_cfg + spec_cfg->size, config, config_sz);
91
92 free(spec_cfg->capabilities);
93
94 /* Update with new config data */
95 spec_cfg->size = new_sz;
96 spec_cfg->capabilities = new_cfg;
97
98 return 0;
99}
100
101int nhlt_endpoint_append_config(struct nhlt_endpoint *endp, const void *config,
102 size_t config_sz)
103{
104 return append_specific_config(&endp->config, config, config_sz);
105}
106
107struct nhlt_format *nhlt_add_format(struct nhlt_endpoint *endp,
108 int num_channels, int sample_freq_khz,
109 int container_bits_per_sample,
110 int valid_bits_per_sample,
111 uint32_t speaker_mask)
112{
113 struct nhlt_format *fmt;
114 struct nhlt_waveform *wave;
115
116 if (endp->num_formats >= MAX_FORMATS)
117 return NULL;
118
119 fmt = &endp->formats[endp->num_formats];
120 wave = &fmt->waveform;
121
122 wave->tag = WAVEFORMAT_TAG;
123 wave->num_channels = num_channels;
124 wave->samples_per_second = sample_freq_khz * 1000;
125 wave->bits_per_sample = container_bits_per_sample;
126 wave->extra_size = sizeof(wave->valid_bits_per_sample);
127 wave->extra_size += sizeof(wave->channel_mask);
128 wave->extra_size += sizeof(wave->sub_format);
129 wave->valid_bits_per_sample = valid_bits_per_sample;
130 wave->channel_mask = speaker_mask;
131 memcpy(&wave->sub_format, &pcm_subformat, sizeof(wave->sub_format));
132
133 /* Calculate the dervied fields */
134 wave->block_align = wave->num_channels * wave->bits_per_sample / 8;
135 wave->bytes_per_second = wave->block_align * wave->samples_per_second;
136
137 endp->num_formats++;
138
139 return fmt;
140}
141
142int nhlt_format_append_config(struct nhlt_format *fmt, const void *config,
143 size_t config_sz)
144{
145 return append_specific_config(&fmt->config, config, config_sz);
146}
147
148int nhlt_endpoint_add_formats(struct nhlt_endpoint *endp,
149 const struct nhlt_format_config *formats,
150 size_t num_formats)
151{
152 ofnode node;
153 size_t i;
154
155 node = binman_section_find_node("private-files");
156
157 for (i = 0; i < num_formats; i++) {
158 const struct nhlt_format_config *cfg = &formats[i];
159 struct nhlt_format *fmt;
160 void *data;
161 int size;
162 int ret;
163
164 fmt = nhlt_add_format(endp, cfg->num_channels,
165 cfg->sample_freq_khz,
166 cfg->container_bits_per_sample,
167 cfg->valid_bits_per_sample,
168 cfg->speaker_mask);
169 if (!fmt)
170 return -ENOSPC;
171
172 if (!cfg->settings_file)
173 continue;
174
175 ret = binman_entry_map(node, cfg->settings_file, &data, &size);
176 if (ret) {
177 log_warning("Failed to find settings file %s\n",
178 cfg->settings_file);
179 return log_msg_ret("settings", ret);
180 }
181
182 ret = nhlt_format_append_config(fmt, data, size);
183 if (ret)
184 return log_msg_ret("append", ret);
185 }
186
187 return 0;
188}
189
190void nhlt_next_instance(struct nhlt *nhlt, int link_type)
191{
192 if (link_type < NHLT_LINK_HDA || link_type >= NHLT_MAX_LINK_TYPES)
193 return;
194
195 nhlt->current_instance_id[link_type]++;
196}
197
198static size_t calc_specific_config_size(struct nhlt_specific_config *cfg)
199{
200 return sizeof(cfg->size) + cfg->size;
201}
202
203static size_t calc_format_size(struct nhlt_format *fmt)
204{
205 size_t sz = 0;
206
207 /* Wave format first */
208 sz += sizeof(fmt->waveform.tag);
209 sz += sizeof(fmt->waveform.num_channels);
210 sz += sizeof(fmt->waveform.samples_per_second);
211 sz += sizeof(fmt->waveform.bytes_per_second);
212 sz += sizeof(fmt->waveform.block_align);
213 sz += sizeof(fmt->waveform.bits_per_sample);
214 sz += sizeof(fmt->waveform.extra_size);
215 sz += sizeof(fmt->waveform.valid_bits_per_sample);
216 sz += sizeof(fmt->waveform.channel_mask);
217 sz += sizeof(fmt->waveform.sub_format);
218
219 sz += calc_specific_config_size(&fmt->config);
220
221 return sz;
222}
223
224static size_t calc_endpoint_size(struct nhlt_endpoint *endp)
225{
226 int i;
227 size_t sz = 0;
228
229 sz += sizeof(endp->length) + sizeof(endp->link_type);
230 sz += sizeof(endp->instance_id) + sizeof(endp->vendor_id);
231 sz += sizeof(endp->device_id) + sizeof(endp->revision_id);
232 sz += sizeof(endp->subsystem_id) + sizeof(endp->device_type);
233 sz += sizeof(endp->direction) + sizeof(endp->virtual_bus_id);
234 sz += calc_specific_config_size(&endp->config);
235 sz += sizeof(endp->num_formats);
236
237 for (i = 0; i < endp->num_formats; i++)
238 sz += calc_format_size(&endp->formats[i]);
239
240 /* Adjust endpoint length to reflect current configuration */
241 endp->length = sz;
242
243 return sz;
244}
245
246static size_t calc_endpoints_size(struct nhlt *nhlt)
247{
248 size_t sz = 0;
249 int i;
250
251 for (i = 0; i < nhlt->num_endpoints; i++)
252 sz += calc_endpoint_size(&nhlt->endpoints[i]);
253
254 return sz;
255}
256
257static size_t calc_size(struct nhlt *nhlt)
258{
259 return sizeof(nhlt->num_endpoints) + calc_endpoints_size(nhlt);
260}
261
262size_t nhlt_current_size(struct nhlt *nhlt)
263{
264 return calc_size(nhlt) + sizeof(struct acpi_table_header);
265}
266
267static void nhlt_free_resources(struct nhlt *nhlt)
268{
269 int i, j;
270
271 /* Free all specific configs */
272 for (i = 0; i < nhlt->num_endpoints; i++) {
273 struct nhlt_endpoint *endp = &nhlt->endpoints[i];
274
275 free(endp->config.capabilities);
276 for (j = 0; j < endp->num_formats; j++) {
277 struct nhlt_format *fmt = &endp->formats[j];
278
279 free(fmt->config.capabilities);
280 }
281 }
282
283 /* Free nhlt object proper */
284 free(nhlt);
285}
286
287struct cursor {
a87fff80 288 u8 *start;
7f926c96
SG
289 u8 *buf;
290};
291
292static void ser8(struct cursor *cur, uint val)
293{
294 *cur->buf = val;
a87fff80 295 cur->buf += sizeof(u8);
7f926c96
SG
296}
297
298static void ser16(struct cursor *cur, uint val)
299{
300 put_unaligned_le16(val, cur->buf);
a87fff80 301 cur->buf += sizeof(u16);
7f926c96
SG
302}
303
304static void ser32(struct cursor *cur, uint val)
305{
306 put_unaligned_le32(val, cur->buf);
a87fff80 307 cur->buf += sizeof(u32);
7f926c96
SG
308}
309
310static void serblob(struct cursor *cur, void *from, size_t sz)
311{
312 memcpy(cur->buf, from, sz);
313 cur->buf += sz;
314}
315
316static void serialise_specific_config(struct nhlt_specific_config *cfg,
317 struct cursor *cur)
318{
a87fff80 319 log_debug("%zx\n", cur->buf - cur->start);
7f926c96
SG
320 ser32(cur, cfg->size);
321 serblob(cur, cfg->capabilities, cfg->size);
322}
323
324static void serialise_waveform(struct nhlt_waveform *wave, struct cursor *cur)
325{
a87fff80 326 log_debug("%zx\n", cur->buf - cur->start);
7f926c96
SG
327 ser16(cur, wave->tag);
328 ser16(cur, wave->num_channels);
329 ser32(cur, wave->samples_per_second);
330 ser32(cur, wave->bytes_per_second);
331 ser16(cur, wave->block_align);
332 ser16(cur, wave->bits_per_sample);
333 ser16(cur, wave->extra_size);
334 ser16(cur, wave->valid_bits_per_sample);
335 ser32(cur, wave->channel_mask);
336 ser32(cur, wave->sub_format.data1);
337 ser16(cur, wave->sub_format.data2);
338 ser16(cur, wave->sub_format.data3);
339 serblob(cur, wave->sub_format.data4, sizeof(wave->sub_format.data4));
340}
341
342static void serialise_format(struct nhlt_format *fmt, struct cursor *cur)
343{
a87fff80 344 log_debug("%zx\n", cur->buf - cur->start);
7f926c96
SG
345 serialise_waveform(&fmt->waveform, cur);
346 serialise_specific_config(&fmt->config, cur);
347}
348
349static void serialise_endpoint(struct nhlt_endpoint *endp, struct cursor *cur)
350{
351 int i;
352
a87fff80 353 log_debug("%zx\n", cur->buf - cur->start);
7f926c96
SG
354 ser32(cur, endp->length);
355 ser8(cur, endp->link_type);
356 ser8(cur, endp->instance_id);
357 ser16(cur, endp->vendor_id);
358 ser16(cur, endp->device_id);
359 ser16(cur, endp->revision_id);
360 ser32(cur, endp->subsystem_id);
361 ser8(cur, endp->device_type);
362 ser8(cur, endp->direction);
363 ser8(cur, endp->virtual_bus_id);
364 serialise_specific_config(&endp->config, cur);
365 ser8(cur, endp->num_formats);
366
367 for (i = 0; i < endp->num_formats; i++)
368 serialise_format(&endp->formats[i], cur);
369}
370
371static void nhlt_serialise_endpoints(struct nhlt *nhlt, struct cursor *cur)
372{
373 int i;
374
375 ser8(cur, nhlt->num_endpoints);
376
377 for (i = 0; i < nhlt->num_endpoints; i++)
378 serialise_endpoint(&nhlt->endpoints[i], cur);
379}
380
381int nhlt_serialise_oem_overrides(struct acpi_ctx *ctx, struct nhlt *nhlt,
382 const char *oem_id, const char *oem_table_id,
383 uint32_t oem_revision)
384{
385 struct cursor cur;
386 struct acpi_table_header *header;
387 size_t sz;
388 size_t oem_id_len;
389 size_t oem_table_id_len;
390 int ret;
391
392 log_info("ACPI: * NHLT\n");
393 sz = nhlt_current_size(nhlt);
394
395 /* Create header */
396 header = (void *)ctx->current;
397 memset(header, '\0', sizeof(struct acpi_table_header));
398 acpi_fill_header(header, "NHLT");
399 header->length = sz;
400 header->revision = acpi_get_table_revision(ACPITAB_NHLT);
401
402 if (oem_id) {
403 oem_id_len = min((int)strlen(oem_id), 6);
404 memcpy(header->oem_id, oem_id, oem_id_len);
405 }
406 if (oem_table_id) {
407 oem_table_id_len = min((int)strlen(oem_table_id), 8);
408 memcpy(header->oem_table_id, oem_table_id, oem_table_id_len);
409 }
410 header->oem_revision = oem_revision;
411
412 cur.buf = (void *)(header + 1);
a87fff80 413 cur.start = (void *)header;
7f926c96
SG
414 nhlt_serialise_endpoints(nhlt, &cur);
415
416 header->checksum = table_compute_checksum(header, sz);
417 nhlt_free_resources(nhlt);
a87fff80 418 assert(cur.buf - cur.start == sz);
7f926c96
SG
419
420 ret = acpi_add_table(ctx, ctx->current);
421 if (ret)
422 return log_msg_ret("add", ret);
423 acpi_inc_align(ctx, sz);
424
425 return 0;
426}
427
428static int _nhlt_add_single_endpoint(struct nhlt *nhlt, int virtual_bus_id,
429 const struct nhlt_endp_descriptor *epd)
430{
431 struct nhlt_endpoint *endp;
432 int ret;
433
434 endp = nhlt_add_endpoint(nhlt, epd->link, epd->device, epd->direction,
435 epd->vid, epd->did);
436 if (!endp)
437 return -EINVAL;
438
439 endp->virtual_bus_id = virtual_bus_id;
440
441 ret = nhlt_endpoint_append_config(endp, epd->cfg, epd->cfg_size);
442 if (ret)
443 return ret;
444
445 ret = nhlt_endpoint_add_formats(endp, epd->formats, epd->num_formats);
446 if (ret)
447 return log_msg_ret("formats", ret);
448
449 return 0;
450}
451
452static int _nhlt_add_endpoints(struct nhlt *nhlt, int virtual_bus_id,
453 const struct nhlt_endp_descriptor *epds,
454 size_t num_epds)
455{
456 size_t i;
457 int ret;
458
459 for (i = 0; i < num_epds; i++) {
460 ret = _nhlt_add_single_endpoint(nhlt, virtual_bus_id, &epds[i]);
461 if (ret)
462 return log_ret(ret);
463 }
464
465 return 0;
466}
467
468int nhlt_add_endpoints(struct nhlt *nhlt,
469 const struct nhlt_endp_descriptor *epds, size_t num_epds)
470{
471 int ret;
472
473 ret = _nhlt_add_endpoints(nhlt, DEFAULT_VIRTUAL_BUS_ID, epds, num_epds);
474
475 return ret;
476}
477
478int nhlt_add_ssp_endpoints(struct nhlt *nhlt, int virtual_bus_id,
479 const struct nhlt_endp_descriptor *epds,
480 size_t num_epds)
481{
482 int ret;
483
484 ret = _nhlt_add_endpoints(nhlt, virtual_bus_id, epds, num_epds);
485 if (!ret)
486 nhlt_next_instance(nhlt, NHLT_LINK_SSP);
487
488 return ret;
489}