blob: c64dd9c008195975c085012739c19821c862d620 [file] [log] [blame]
Simon Glassf6cdb912020-07-07 21:32:16 -06001// 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 {
288 u8 *buf;
289};
290
291static void ser8(struct cursor *cur, uint val)
292{
293 *cur->buf = val;
294 cur->buf += sizeof(val);
295}
296
297static void ser16(struct cursor *cur, uint val)
298{
299 put_unaligned_le16(val, cur->buf);
300 cur->buf += sizeof(val);
301}
302
303static void ser32(struct cursor *cur, uint val)
304{
305 put_unaligned_le32(val, cur->buf);
306 cur->buf += sizeof(val);
307}
308
309static void serblob(struct cursor *cur, void *from, size_t sz)
310{
311 memcpy(cur->buf, from, sz);
312 cur->buf += sz;
313}
314
315static void serialise_specific_config(struct nhlt_specific_config *cfg,
316 struct cursor *cur)
317{
318 ser32(cur, cfg->size);
319 serblob(cur, cfg->capabilities, cfg->size);
320}
321
322static void serialise_waveform(struct nhlt_waveform *wave, struct cursor *cur)
323{
324 ser16(cur, wave->tag);
325 ser16(cur, wave->num_channels);
326 ser32(cur, wave->samples_per_second);
327 ser32(cur, wave->bytes_per_second);
328 ser16(cur, wave->block_align);
329 ser16(cur, wave->bits_per_sample);
330 ser16(cur, wave->extra_size);
331 ser16(cur, wave->valid_bits_per_sample);
332 ser32(cur, wave->channel_mask);
333 ser32(cur, wave->sub_format.data1);
334 ser16(cur, wave->sub_format.data2);
335 ser16(cur, wave->sub_format.data3);
336 serblob(cur, wave->sub_format.data4, sizeof(wave->sub_format.data4));
337}
338
339static void serialise_format(struct nhlt_format *fmt, struct cursor *cur)
340{
341 serialise_waveform(&fmt->waveform, cur);
342 serialise_specific_config(&fmt->config, cur);
343}
344
345static void serialise_endpoint(struct nhlt_endpoint *endp, struct cursor *cur)
346{
347 int i;
348
349 ser32(cur, endp->length);
350 ser8(cur, endp->link_type);
351 ser8(cur, endp->instance_id);
352 ser16(cur, endp->vendor_id);
353 ser16(cur, endp->device_id);
354 ser16(cur, endp->revision_id);
355 ser32(cur, endp->subsystem_id);
356 ser8(cur, endp->device_type);
357 ser8(cur, endp->direction);
358 ser8(cur, endp->virtual_bus_id);
359 serialise_specific_config(&endp->config, cur);
360 ser8(cur, endp->num_formats);
361
362 for (i = 0; i < endp->num_formats; i++)
363 serialise_format(&endp->formats[i], cur);
364}
365
366static void nhlt_serialise_endpoints(struct nhlt *nhlt, struct cursor *cur)
367{
368 int i;
369
370 ser8(cur, nhlt->num_endpoints);
371
372 for (i = 0; i < nhlt->num_endpoints; i++)
373 serialise_endpoint(&nhlt->endpoints[i], cur);
374}
375
376int nhlt_serialise_oem_overrides(struct acpi_ctx *ctx, struct nhlt *nhlt,
377 const char *oem_id, const char *oem_table_id,
378 uint32_t oem_revision)
379{
380 struct cursor cur;
381 struct acpi_table_header *header;
382 size_t sz;
383 size_t oem_id_len;
384 size_t oem_table_id_len;
385 int ret;
386
387 log_info("ACPI: * NHLT\n");
388 sz = nhlt_current_size(nhlt);
389
390 /* Create header */
391 header = (void *)ctx->current;
392 memset(header, '\0', sizeof(struct acpi_table_header));
393 acpi_fill_header(header, "NHLT");
394 header->length = sz;
395 header->revision = acpi_get_table_revision(ACPITAB_NHLT);
396
397 if (oem_id) {
398 oem_id_len = min((int)strlen(oem_id), 6);
399 memcpy(header->oem_id, oem_id, oem_id_len);
400 }
401 if (oem_table_id) {
402 oem_table_id_len = min((int)strlen(oem_table_id), 8);
403 memcpy(header->oem_table_id, oem_table_id, oem_table_id_len);
404 }
405 header->oem_revision = oem_revision;
406
407 cur.buf = (void *)(header + 1);
408 nhlt_serialise_endpoints(nhlt, &cur);
409
410 header->checksum = table_compute_checksum(header, sz);
411 nhlt_free_resources(nhlt);
412
413 ret = acpi_add_table(ctx, ctx->current);
414 if (ret)
415 return log_msg_ret("add", ret);
416 acpi_inc_align(ctx, sz);
417
418 return 0;
419}
420
421static int _nhlt_add_single_endpoint(struct nhlt *nhlt, int virtual_bus_id,
422 const struct nhlt_endp_descriptor *epd)
423{
424 struct nhlt_endpoint *endp;
425 int ret;
426
427 endp = nhlt_add_endpoint(nhlt, epd->link, epd->device, epd->direction,
428 epd->vid, epd->did);
429 if (!endp)
430 return -EINVAL;
431
432 endp->virtual_bus_id = virtual_bus_id;
433
434 ret = nhlt_endpoint_append_config(endp, epd->cfg, epd->cfg_size);
435 if (ret)
436 return ret;
437
438 ret = nhlt_endpoint_add_formats(endp, epd->formats, epd->num_formats);
439 if (ret)
440 return log_msg_ret("formats", ret);
441
442 return 0;
443}
444
445static int _nhlt_add_endpoints(struct nhlt *nhlt, int virtual_bus_id,
446 const struct nhlt_endp_descriptor *epds,
447 size_t num_epds)
448{
449 size_t i;
450 int ret;
451
452 for (i = 0; i < num_epds; i++) {
453 ret = _nhlt_add_single_endpoint(nhlt, virtual_bus_id, &epds[i]);
454 if (ret)
455 return log_ret(ret);
456 }
457
458 return 0;
459}
460
461int nhlt_add_endpoints(struct nhlt *nhlt,
462 const struct nhlt_endp_descriptor *epds, size_t num_epds)
463{
464 int ret;
465
466 ret = _nhlt_add_endpoints(nhlt, DEFAULT_VIRTUAL_BUS_ID, epds, num_epds);
467
468 return ret;
469}
470
471int nhlt_add_ssp_endpoints(struct nhlt *nhlt, int virtual_bus_id,
472 const struct nhlt_endp_descriptor *epds,
473 size_t num_epds)
474{
475 int ret;
476
477 ret = _nhlt_add_endpoints(nhlt, virtual_bus_id, epds, num_epds);
478 if (!ret)
479 nhlt_next_instance(nhlt, NHLT_LINK_SSP);
480
481 return ret;
482}