blob: 5001c385c5d2d0ae2e75ffdafc792213345bc46d [file] [log] [blame]
Aaron Durbin9420a522015-11-17 16:31:00 -06001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright 2015 Google, Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
15
16#include <arch/acpi.h>
17#include <cbfs.h>
18#include <commonlib/endian.h>
19#include <console/console.h>
20#include <nhlt.h>
21#include <stdlib.h>
22#include <string.h>
23
24#define NHLT_RID 1
25#define NHLT_SSID 1
26#define WAVEFORMAT_TAG 0xfffe
Aaron Durbined0f6d72016-06-28 14:59:21 -050027#define DEFAULT_VIRTUAL_BUS_ID 0
Aaron Durbin9420a522015-11-17 16:31:00 -060028
29static const struct sub_format pcm_subformat = {
30 .data1 = 0x00000001,
31 .data2 = 0x0000,
32 .data3 = 0x0010,
33 .data4 = { 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 },
34};
35
36struct nhlt *nhlt_init(void)
37{
38 struct nhlt *nhlt;
39
40 nhlt = malloc(sizeof(*nhlt));
41
42 if (nhlt == NULL)
43 return NULL;
44
45 memset(nhlt, 0, sizeof(*nhlt));
Harsha Priya5c315112018-05-03 18:07:40 -070046 nhlt->subsystem_id = NHLT_SSID;
Aaron Durbin9420a522015-11-17 16:31:00 -060047
48 return nhlt;
49}
50
51struct nhlt_endpoint *nhlt_add_endpoint(struct nhlt *nhlt, int link_type,
52 int device_type, int dir,
53 uint16_t vid, uint16_t did)
54{
55 struct nhlt_endpoint *endp;
56
57 if (link_type < NHLT_LINK_HDA || link_type >= NHLT_MAX_LINK_TYPES)
58 return NULL;
59
60 if (nhlt->num_endpoints >= MAX_ENDPOINTS)
61 return NULL;
62
63 endp = &nhlt->endpoints[nhlt->num_endpoints];
64
65 endp->link_type = link_type;
66 endp->instance_id = nhlt->current_instance_id[link_type];
67 endp->vendor_id = vid;
68 endp->device_id = did;
69 endp->revision_id = NHLT_RID;
Harsha Priya5c315112018-05-03 18:07:40 -070070 endp->subsystem_id = nhlt->subsystem_id;
Aaron Durbin9420a522015-11-17 16:31:00 -060071 endp->device_type = device_type;
72 endp->direction = dir;
Aaron Durbined0f6d72016-06-28 14:59:21 -050073 endp->virtual_bus_id = DEFAULT_VIRTUAL_BUS_ID;
Aaron Durbin9420a522015-11-17 16:31:00 -060074
75 nhlt->num_endpoints++;
76
77 return endp;
78}
79
80static int append_specific_config(struct nhlt_specific_config *spec_cfg,
81 const void *config, size_t config_sz)
82{
83 size_t new_sz;
84 void *new_cfg;
85
86 if (config == NULL || config_sz == 0)
87 return 0;
88
89 new_sz = spec_cfg->size + config_sz;
90
91 new_cfg = malloc(new_sz);
92
93 if (new_cfg == NULL)
94 return -1;
95
96 /* Append new config. */
97 memcpy(new_cfg, spec_cfg->capabilities, spec_cfg->size);
98 memcpy(new_cfg + spec_cfg->size, config, config_sz);
99
100 free(spec_cfg->capabilities);
101
102 /* Update with new config data. */
103 spec_cfg->size = new_sz;
104 spec_cfg->capabilities = new_cfg;
105
106 return 0;
107}
108
109int nhlt_endpoint_append_config(struct nhlt_endpoint *endp, const void *config,
110 size_t config_sz)
111{
112 return append_specific_config(&endp->config, config, config_sz);
113}
114
115struct nhlt_format *nhlt_add_format(struct nhlt_endpoint *endp,
116 int num_channels,
117 int sample_freq_khz,
118 int container_bits_per_sample,
119 int valid_bits_per_sample,
120 uint32_t speaker_mask)
121{
122 struct nhlt_format *fmt;
123 struct nhlt_waveform *wave;
124
125 if (endp->num_formats >= MAX_FORMATS)
126 return NULL;
127
128 fmt = &endp->formats[endp->num_formats];
129 wave = &fmt->waveform;
130
131 wave->tag = WAVEFORMAT_TAG;
132 wave->num_channels = num_channels;
133 wave->samples_per_second = sample_freq_khz * KHz;
134 wave->bits_per_sample = container_bits_per_sample;
135 wave->extra_size = sizeof(wave->valid_bits_per_sample);
136 wave->extra_size += sizeof(wave->channel_mask);
137 wave->extra_size += sizeof(wave->sub_format);
138 wave->valid_bits_per_sample = valid_bits_per_sample;
139 wave->channel_mask = speaker_mask;
140 memcpy(&wave->sub_format, &pcm_subformat, sizeof(wave->sub_format));
141
142 /* Calculate the dervied fields. */
143 wave->block_align = wave->num_channels * wave->bits_per_sample / 8;
144 wave->bytes_per_second = wave->block_align * wave->samples_per_second;
145
146 endp->num_formats++;
147
148 return fmt;
149}
150
151int nhlt_format_append_config(struct nhlt_format *fmt, const void *config,
152 size_t config_sz)
153{
154 return append_specific_config(&fmt->config, config, config_sz);
155}
156
157int nhlt_endpoint_add_formats(struct nhlt_endpoint *endp,
158 const struct nhlt_format_config *formats,
159 size_t num_formats)
160{
161 size_t i;
162
163 for (i = 0; i < num_formats; i++) {
164 struct nhlt_format *fmt;
165 struct cbfsf file;
166 struct region_device settings;
167 void *settings_data;
168 const struct nhlt_format_config *cfg = &formats[i];
169
170 fmt = nhlt_add_format(endp, cfg->num_channels,
171 cfg->sample_freq_khz,
172 cfg->container_bits_per_sample,
173 cfg->valid_bits_per_sample,
174 cfg->speaker_mask);
175
176 if (fmt == NULL)
177 return -1;
178
179 if (cfg->settings_file == NULL)
180 continue;
181
182 /* Find the settings file in CBFS and place it in format. */
183 if (cbfs_boot_locate(&file, cfg->settings_file, NULL))
184 return -1;
185
186 cbfs_file_data(&settings, &file);
187
188 settings_data = rdev_mmap_full(&settings);
189
190 if (settings_data == NULL)
191 return -1;
192
193 if (nhlt_format_append_config(fmt, settings_data,
194 region_device_sz(&settings))) {
195 rdev_munmap(&settings, settings_data);
196 return -1;
197 }
198
199 rdev_munmap(&settings, settings_data);
200 }
201
202 return 0;
203}
204
205void nhlt_next_instance(struct nhlt *nhlt, int link_type)
206{
207 if (link_type < NHLT_LINK_HDA || link_type >= NHLT_MAX_LINK_TYPES)
208 return;
209
210 nhlt->current_instance_id[link_type]++;
211}
212
213static size_t calc_specific_config_size(struct nhlt_specific_config *cfg)
214{
215 return sizeof(cfg->size) + cfg->size;
216}
217
218static size_t calc_format_size(struct nhlt_format *fmt)
219{
220 size_t sz = 0;
221
222 /* Wave format first. */
223 sz += sizeof(fmt->waveform.tag);
224 sz += sizeof(fmt->waveform.num_channels);
225 sz += sizeof(fmt->waveform.samples_per_second);
226 sz += sizeof(fmt->waveform.bytes_per_second);
227 sz += sizeof(fmt->waveform.block_align);
228 sz += sizeof(fmt->waveform.bits_per_sample);
229 sz += sizeof(fmt->waveform.extra_size);
230 sz += sizeof(fmt->waveform.valid_bits_per_sample);
231 sz += sizeof(fmt->waveform.channel_mask);
232 sz += sizeof(fmt->waveform.sub_format);
233
234 sz += calc_specific_config_size(&fmt->config);
235
236 return sz;
237}
238
239static size_t calc_endpoint_size(struct nhlt_endpoint *endp)
240{
241 int i;
242 size_t sz = 0;
243
244 sz += sizeof(endp->length) + sizeof(endp->link_type);
245 sz += sizeof(endp->instance_id) + sizeof(endp->vendor_id);
246 sz += sizeof(endp->device_id) + sizeof(endp->revision_id);
247 sz += sizeof(endp->subsystem_id) + sizeof(endp->device_type);
248 sz += sizeof(endp->direction) + sizeof(endp->virtual_bus_id);
249 sz += calc_specific_config_size(&endp->config);
250 sz += sizeof(endp->num_formats);
251
252 for (i = 0; i < endp->num_formats; i++)
253 sz += calc_format_size(&endp->formats[i]);
254
255 /* Adjust endpoint length to reflect current configuration. */
256 endp->length = sz;
257
258 return sz;
259}
260
261static size_t calc_endpoints_size(struct nhlt *nhlt)
262{
263 int i;
264 size_t sz = 0;
265
266 for (i = 0; i < nhlt->num_endpoints; i++)
267 sz += calc_endpoint_size(&nhlt->endpoints[i]);
268
269 return sz;
270}
271
272static size_t calc_size(struct nhlt *nhlt)
273{
274 return sizeof(nhlt->num_endpoints) + calc_endpoints_size(nhlt);
275}
276
277size_t nhlt_current_size(struct nhlt *nhlt)
278{
279 return calc_size(nhlt) + sizeof(acpi_header_t);
280}
281
282static void nhlt_free_resources(struct nhlt *nhlt)
283{
284 int i;
285 int j;
286
287 /* Free all specific configs. */
288 for (i = 0; i < nhlt->num_endpoints; i++) {
289 struct nhlt_endpoint *endp = &nhlt->endpoints[i];
290
291 free(endp->config.capabilities);
292 for (j = 0; j < endp->num_formats; j++) {
293 struct nhlt_format *fmt = &endp->formats[j];
294
295 free(fmt->config.capabilities);
296 }
297 }
298
299 /* Free nhlt object proper. */
300 free(nhlt);
301}
302
303struct cursor {
304 uint8_t *buf;
305};
306
307static void ser8(struct cursor *cur, uint8_t val)
308{
309 write_le8(cur->buf, val);
310 cur->buf += sizeof(val);
311}
312
313static void ser16(struct cursor *cur, uint16_t val)
314{
315 write_le16(cur->buf, val);
316 cur->buf += sizeof(val);
317}
318
319static void ser32(struct cursor *cur, uint32_t val)
320{
321 write_le32(cur->buf, val);
322 cur->buf += sizeof(val);
323}
324
325static void serblob(struct cursor *cur, void *from, size_t sz)
326{
327 memcpy(cur->buf, from, sz);
328 cur->buf += sz;
329}
330
331static void serialize_specific_config(struct nhlt_specific_config *cfg,
332 struct cursor *cur)
333{
334 ser32(cur, cfg->size);
335 serblob(cur, cfg->capabilities, cfg->size);
336}
337
338static void serialize_waveform(struct nhlt_waveform *wave, struct cursor *cur)
339{
340 ser16(cur, wave->tag);
341 ser16(cur, wave->num_channels);
342 ser32(cur, wave->samples_per_second);
343 ser32(cur, wave->bytes_per_second);
344 ser16(cur, wave->block_align);
345 ser16(cur, wave->bits_per_sample);
346 ser16(cur, wave->extra_size);
347 ser16(cur, wave->valid_bits_per_sample);
348 ser32(cur, wave->channel_mask);
349 ser32(cur, wave->sub_format.data1);
350 ser16(cur, wave->sub_format.data2);
351 ser16(cur, wave->sub_format.data3);
352 serblob(cur, wave->sub_format.data4, sizeof(wave->sub_format.data4));
353}
354
355static void serialize_format(struct nhlt_format *fmt, struct cursor *cur)
356{
357 serialize_waveform(&fmt->waveform, cur);
358 serialize_specific_config(&fmt->config, cur);
359}
360
361static void serialize_endpoint(struct nhlt_endpoint *endp, struct cursor *cur)
362{
363 int i;
364
365 ser32(cur, endp->length);
366 ser8(cur, endp->link_type);
367 ser8(cur, endp->instance_id);
368 ser16(cur, endp->vendor_id);
369 ser16(cur, endp->device_id);
370 ser16(cur, endp->revision_id);
371 ser32(cur, endp->subsystem_id);
372 ser8(cur, endp->device_type);
373 ser8(cur, endp->direction);
374 ser8(cur, endp->virtual_bus_id);
375 serialize_specific_config(&endp->config, cur);
376 ser8(cur, endp->num_formats);
377
378 for (i = 0; i < endp->num_formats; i++)
379 serialize_format(&endp->formats[i], cur);
380}
381
382static void nhlt_serialize_endpoints(struct nhlt *nhlt, struct cursor *cur)
383{
384 int i;
385
386 ser8(cur, nhlt->num_endpoints);
387
388 for (i = 0; i < nhlt->num_endpoints; i++)
389 serialize_endpoint(&nhlt->endpoints[i], cur);
390}
391
392uintptr_t nhlt_serialize(struct nhlt *nhlt, uintptr_t acpi_addr)
393{
Aaron Durbinb4afe3c2016-11-29 23:14:25 -0600394 return nhlt_serialize_oem_overrides(nhlt, acpi_addr, NULL, NULL, 0);
Fang, Yang A16ff8592016-01-28 16:52:33 -0800395}
396
397uintptr_t nhlt_serialize_oem_overrides(struct nhlt *nhlt,
Aaron Durbinb4afe3c2016-11-29 23:14:25 -0600398 uintptr_t acpi_addr, const char *oem_id, const char *oem_table_id,
399 uint32_t oem_revision)
Fang, Yang A16ff8592016-01-28 16:52:33 -0800400{
Aaron Durbin9420a522015-11-17 16:31:00 -0600401 struct cursor cur;
402 acpi_header_t *header;
403 size_t sz;
Fang, Yang A16ff8592016-01-28 16:52:33 -0800404 size_t oem_id_len;
405 size_t oem_table_id_len;
Aaron Durbin9420a522015-11-17 16:31:00 -0600406
407 printk(BIOS_DEBUG, "ACPI: * NHLT\n");
408
409 sz = nhlt_current_size(nhlt);
410
411 /* Create header */
412 header = (void *)acpi_addr;
413 memset(header, 0, sizeof(acpi_header_t));
414 memcpy(header->signature, "NHLT", 4);
415 write_le32(&header->length, sz);
Marc Jones7a5f0a92018-08-22 19:36:40 -0600416 write_le8(&header->revision, get_acpi_table_revision(NHLT));
Fang, Yang A16ff8592016-01-28 16:52:33 -0800417
418 if (oem_id == NULL)
419 oem_id = OEM_ID;
420
421 if (oem_table_id == NULL)
422 oem_table_id = ACPI_TABLE_CREATOR;
423
424 oem_id_len = MIN(strlen(oem_id), 6);
425 oem_table_id_len = MIN(strlen(oem_table_id), 8);
426
427 memcpy(header->oem_id, oem_id, oem_id_len);
428 memcpy(header->oem_table_id, oem_table_id, oem_table_id_len);
Aaron Durbinb4afe3c2016-11-29 23:14:25 -0600429 write_le32(&header->oem_revision, oem_revision);
Aaron Durbin9420a522015-11-17 16:31:00 -0600430 memcpy(header->asl_compiler_id, ASLC, 4);
431
432 cur.buf = (void *)(acpi_addr + sizeof(acpi_header_t));
433 nhlt_serialize_endpoints(nhlt, &cur);
434
435 write_le8(&header->checksum, acpi_checksum((void *)header, sz));
436
437 nhlt_free_resources(nhlt);
438
439 acpi_addr += sz;
440 acpi_addr = ALIGN_UP(acpi_addr, 16);
441
442 return acpi_addr;
443}
Aaron Durbined0f6d72016-06-28 14:59:21 -0500444
445static int _nhlt_add_single_endpoint(struct nhlt *nhlt, int virtual_bus_id,
446 const struct nhlt_endp_descriptor *epd)
447{
448 struct nhlt_endpoint *endp;
449
450 endp = nhlt_add_endpoint(nhlt, epd->link, epd->device, epd->direction,
451 epd->vid, epd->did);
452
453 if (endp == NULL)
454 return -1;
455
456 endp->virtual_bus_id = virtual_bus_id;
457
458 if (nhlt_endpoint_append_config(endp, epd->cfg, epd->cfg_size))
459 return -1;
460
461 if (nhlt_endpoint_add_formats(endp, epd->formats, epd->num_formats))
462 return -1;
463
464 return 0;
465}
466
467static int _nhlt_add_endpoints(struct nhlt *nhlt, int virtual_bus_id,
468 const struct nhlt_endp_descriptor *epds,
469 size_t num_epds)
470{
471 size_t i;
472
473 for (i = 0; i < num_epds; i++)
474 if (_nhlt_add_single_endpoint(nhlt, virtual_bus_id, &epds[i]))
475 return -1;
476
477 return 0;
478}
479
480int nhlt_add_endpoints(struct nhlt *nhlt,
481 const struct nhlt_endp_descriptor *epds,
482 size_t num_epds)
483{
484 int ret;
485 ret = _nhlt_add_endpoints(nhlt, DEFAULT_VIRTUAL_BUS_ID, epds, num_epds);
486 return ret;
487}
488
489int nhlt_add_ssp_endpoints(struct nhlt *nhlt, int virtual_bus_id,
490 const struct nhlt_endp_descriptor *epds, size_t num_epds)
491{
492 int ret;
493
494 ret = _nhlt_add_endpoints(nhlt, virtual_bus_id, epds, num_epds);
495
496 if (!ret)
497 nhlt_next_instance(nhlt, NHLT_LINK_SSP);
498
499 return ret;
500}