blob: da94415f4057108a2bdc705fa7b932f303c1bc9b [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));
46
47 return nhlt;
48}
49
50struct nhlt_endpoint *nhlt_add_endpoint(struct nhlt *nhlt, int link_type,
51 int device_type, int dir,
52 uint16_t vid, uint16_t did)
53{
54 struct nhlt_endpoint *endp;
55
56 if (link_type < NHLT_LINK_HDA || link_type >= NHLT_MAX_LINK_TYPES)
57 return NULL;
58
59 if (nhlt->num_endpoints >= MAX_ENDPOINTS)
60 return NULL;
61
62 endp = &nhlt->endpoints[nhlt->num_endpoints];
63
64 endp->link_type = link_type;
65 endp->instance_id = nhlt->current_instance_id[link_type];
66 endp->vendor_id = vid;
67 endp->device_id = did;
68 endp->revision_id = NHLT_RID;
69 endp->subsystem_id = NHLT_SSID;
70 endp->device_type = device_type;
71 endp->direction = dir;
Aaron Durbined0f6d72016-06-28 14:59:21 -050072 endp->virtual_bus_id = DEFAULT_VIRTUAL_BUS_ID;
Aaron Durbin9420a522015-11-17 16:31:00 -060073
74 nhlt->num_endpoints++;
75
76 return endp;
77}
78
79static int append_specific_config(struct nhlt_specific_config *spec_cfg,
80 const void *config, size_t config_sz)
81{
82 size_t new_sz;
83 void *new_cfg;
84
85 if (config == NULL || config_sz == 0)
86 return 0;
87
88 new_sz = spec_cfg->size + config_sz;
89
90 new_cfg = malloc(new_sz);
91
92 if (new_cfg == NULL)
93 return -1;
94
95 /* Append new config. */
96 memcpy(new_cfg, spec_cfg->capabilities, spec_cfg->size);
97 memcpy(new_cfg + spec_cfg->size, config, config_sz);
98
99 free(spec_cfg->capabilities);
100
101 /* Update with new config data. */
102 spec_cfg->size = new_sz;
103 spec_cfg->capabilities = new_cfg;
104
105 return 0;
106}
107
108int nhlt_endpoint_append_config(struct nhlt_endpoint *endp, const void *config,
109 size_t config_sz)
110{
111 return append_specific_config(&endp->config, config, config_sz);
112}
113
114struct nhlt_format *nhlt_add_format(struct nhlt_endpoint *endp,
115 int num_channels,
116 int sample_freq_khz,
117 int container_bits_per_sample,
118 int valid_bits_per_sample,
119 uint32_t speaker_mask)
120{
121 struct nhlt_format *fmt;
122 struct nhlt_waveform *wave;
123
124 if (endp->num_formats >= MAX_FORMATS)
125 return NULL;
126
127 fmt = &endp->formats[endp->num_formats];
128 wave = &fmt->waveform;
129
130 wave->tag = WAVEFORMAT_TAG;
131 wave->num_channels = num_channels;
132 wave->samples_per_second = sample_freq_khz * KHz;
133 wave->bits_per_sample = container_bits_per_sample;
134 wave->extra_size = sizeof(wave->valid_bits_per_sample);
135 wave->extra_size += sizeof(wave->channel_mask);
136 wave->extra_size += sizeof(wave->sub_format);
137 wave->valid_bits_per_sample = valid_bits_per_sample;
138 wave->channel_mask = speaker_mask;
139 memcpy(&wave->sub_format, &pcm_subformat, sizeof(wave->sub_format));
140
141 /* Calculate the dervied fields. */
142 wave->block_align = wave->num_channels * wave->bits_per_sample / 8;
143 wave->bytes_per_second = wave->block_align * wave->samples_per_second;
144
145 endp->num_formats++;
146
147 return fmt;
148}
149
150int nhlt_format_append_config(struct nhlt_format *fmt, const void *config,
151 size_t config_sz)
152{
153 return append_specific_config(&fmt->config, config, config_sz);
154}
155
156int nhlt_endpoint_add_formats(struct nhlt_endpoint *endp,
157 const struct nhlt_format_config *formats,
158 size_t num_formats)
159{
160 size_t i;
161
162 for (i = 0; i < num_formats; i++) {
163 struct nhlt_format *fmt;
164 struct cbfsf file;
165 struct region_device settings;
166 void *settings_data;
167 const struct nhlt_format_config *cfg = &formats[i];
168
169 fmt = nhlt_add_format(endp, cfg->num_channels,
170 cfg->sample_freq_khz,
171 cfg->container_bits_per_sample,
172 cfg->valid_bits_per_sample,
173 cfg->speaker_mask);
174
175 if (fmt == NULL)
176 return -1;
177
178 if (cfg->settings_file == NULL)
179 continue;
180
181 /* Find the settings file in CBFS and place it in format. */
182 if (cbfs_boot_locate(&file, cfg->settings_file, NULL))
183 return -1;
184
185 cbfs_file_data(&settings, &file);
186
187 settings_data = rdev_mmap_full(&settings);
188
189 if (settings_data == NULL)
190 return -1;
191
192 if (nhlt_format_append_config(fmt, settings_data,
193 region_device_sz(&settings))) {
194 rdev_munmap(&settings, settings_data);
195 return -1;
196 }
197
198 rdev_munmap(&settings, settings_data);
199 }
200
201 return 0;
202}
203
204void nhlt_next_instance(struct nhlt *nhlt, int link_type)
205{
206 if (link_type < NHLT_LINK_HDA || link_type >= NHLT_MAX_LINK_TYPES)
207 return;
208
209 nhlt->current_instance_id[link_type]++;
210}
211
212static size_t calc_specific_config_size(struct nhlt_specific_config *cfg)
213{
214 return sizeof(cfg->size) + cfg->size;
215}
216
217static size_t calc_format_size(struct nhlt_format *fmt)
218{
219 size_t sz = 0;
220
221 /* Wave format first. */
222 sz += sizeof(fmt->waveform.tag);
223 sz += sizeof(fmt->waveform.num_channels);
224 sz += sizeof(fmt->waveform.samples_per_second);
225 sz += sizeof(fmt->waveform.bytes_per_second);
226 sz += sizeof(fmt->waveform.block_align);
227 sz += sizeof(fmt->waveform.bits_per_sample);
228 sz += sizeof(fmt->waveform.extra_size);
229 sz += sizeof(fmt->waveform.valid_bits_per_sample);
230 sz += sizeof(fmt->waveform.channel_mask);
231 sz += sizeof(fmt->waveform.sub_format);
232
233 sz += calc_specific_config_size(&fmt->config);
234
235 return sz;
236}
237
238static size_t calc_endpoint_size(struct nhlt_endpoint *endp)
239{
240 int i;
241 size_t sz = 0;
242
243 sz += sizeof(endp->length) + sizeof(endp->link_type);
244 sz += sizeof(endp->instance_id) + sizeof(endp->vendor_id);
245 sz += sizeof(endp->device_id) + sizeof(endp->revision_id);
246 sz += sizeof(endp->subsystem_id) + sizeof(endp->device_type);
247 sz += sizeof(endp->direction) + sizeof(endp->virtual_bus_id);
248 sz += calc_specific_config_size(&endp->config);
249 sz += sizeof(endp->num_formats);
250
251 for (i = 0; i < endp->num_formats; i++)
252 sz += calc_format_size(&endp->formats[i]);
253
254 /* Adjust endpoint length to reflect current configuration. */
255 endp->length = sz;
256
257 return sz;
258}
259
260static size_t calc_endpoints_size(struct nhlt *nhlt)
261{
262 int i;
263 size_t sz = 0;
264
265 for (i = 0; i < nhlt->num_endpoints; i++)
266 sz += calc_endpoint_size(&nhlt->endpoints[i]);
267
268 return sz;
269}
270
271static size_t calc_size(struct nhlt *nhlt)
272{
273 return sizeof(nhlt->num_endpoints) + calc_endpoints_size(nhlt);
274}
275
276size_t nhlt_current_size(struct nhlt *nhlt)
277{
278 return calc_size(nhlt) + sizeof(acpi_header_t);
279}
280
281static void nhlt_free_resources(struct nhlt *nhlt)
282{
283 int i;
284 int j;
285
286 /* Free all specific configs. */
287 for (i = 0; i < nhlt->num_endpoints; i++) {
288 struct nhlt_endpoint *endp = &nhlt->endpoints[i];
289
290 free(endp->config.capabilities);
291 for (j = 0; j < endp->num_formats; j++) {
292 struct nhlt_format *fmt = &endp->formats[j];
293
294 free(fmt->config.capabilities);
295 }
296 }
297
298 /* Free nhlt object proper. */
299 free(nhlt);
300}
301
302struct cursor {
303 uint8_t *buf;
304};
305
306static void ser8(struct cursor *cur, uint8_t val)
307{
308 write_le8(cur->buf, val);
309 cur->buf += sizeof(val);
310}
311
312static void ser16(struct cursor *cur, uint16_t val)
313{
314 write_le16(cur->buf, val);
315 cur->buf += sizeof(val);
316}
317
318static void ser32(struct cursor *cur, uint32_t val)
319{
320 write_le32(cur->buf, val);
321 cur->buf += sizeof(val);
322}
323
324static void serblob(struct cursor *cur, void *from, size_t sz)
325{
326 memcpy(cur->buf, from, sz);
327 cur->buf += sz;
328}
329
330static void serialize_specific_config(struct nhlt_specific_config *cfg,
331 struct cursor *cur)
332{
333 ser32(cur, cfg->size);
334 serblob(cur, cfg->capabilities, cfg->size);
335}
336
337static void serialize_waveform(struct nhlt_waveform *wave, struct cursor *cur)
338{
339 ser16(cur, wave->tag);
340 ser16(cur, wave->num_channels);
341 ser32(cur, wave->samples_per_second);
342 ser32(cur, wave->bytes_per_second);
343 ser16(cur, wave->block_align);
344 ser16(cur, wave->bits_per_sample);
345 ser16(cur, wave->extra_size);
346 ser16(cur, wave->valid_bits_per_sample);
347 ser32(cur, wave->channel_mask);
348 ser32(cur, wave->sub_format.data1);
349 ser16(cur, wave->sub_format.data2);
350 ser16(cur, wave->sub_format.data3);
351 serblob(cur, wave->sub_format.data4, sizeof(wave->sub_format.data4));
352}
353
354static void serialize_format(struct nhlt_format *fmt, struct cursor *cur)
355{
356 serialize_waveform(&fmt->waveform, cur);
357 serialize_specific_config(&fmt->config, cur);
358}
359
360static void serialize_endpoint(struct nhlt_endpoint *endp, struct cursor *cur)
361{
362 int i;
363
364 ser32(cur, endp->length);
365 ser8(cur, endp->link_type);
366 ser8(cur, endp->instance_id);
367 ser16(cur, endp->vendor_id);
368 ser16(cur, endp->device_id);
369 ser16(cur, endp->revision_id);
370 ser32(cur, endp->subsystem_id);
371 ser8(cur, endp->device_type);
372 ser8(cur, endp->direction);
373 ser8(cur, endp->virtual_bus_id);
374 serialize_specific_config(&endp->config, cur);
375 ser8(cur, endp->num_formats);
376
377 for (i = 0; i < endp->num_formats; i++)
378 serialize_format(&endp->formats[i], cur);
379}
380
381static void nhlt_serialize_endpoints(struct nhlt *nhlt, struct cursor *cur)
382{
383 int i;
384
385 ser8(cur, nhlt->num_endpoints);
386
387 for (i = 0; i < nhlt->num_endpoints; i++)
388 serialize_endpoint(&nhlt->endpoints[i], cur);
389}
390
391uintptr_t nhlt_serialize(struct nhlt *nhlt, uintptr_t acpi_addr)
392{
Aaron Durbinb4afe3c2016-11-29 23:14:25 -0600393 return nhlt_serialize_oem_overrides(nhlt, acpi_addr, NULL, NULL, 0);
Fang, Yang A16ff8592016-01-28 16:52:33 -0800394}
395
396uintptr_t nhlt_serialize_oem_overrides(struct nhlt *nhlt,
Aaron Durbinb4afe3c2016-11-29 23:14:25 -0600397 uintptr_t acpi_addr, const char *oem_id, const char *oem_table_id,
398 uint32_t oem_revision)
Fang, Yang A16ff8592016-01-28 16:52:33 -0800399{
Aaron Durbin9420a522015-11-17 16:31:00 -0600400 struct cursor cur;
401 acpi_header_t *header;
402 size_t sz;
Fang, Yang A16ff8592016-01-28 16:52:33 -0800403 size_t oem_id_len;
404 size_t oem_table_id_len;
Aaron Durbin9420a522015-11-17 16:31:00 -0600405
406 printk(BIOS_DEBUG, "ACPI: * NHLT\n");
407
408 sz = nhlt_current_size(nhlt);
409
410 /* Create header */
411 header = (void *)acpi_addr;
412 memset(header, 0, sizeof(acpi_header_t));
413 memcpy(header->signature, "NHLT", 4);
414 write_le32(&header->length, sz);
415 write_le8(&header->revision, 5);
Fang, Yang A16ff8592016-01-28 16:52:33 -0800416
417 if (oem_id == NULL)
418 oem_id = OEM_ID;
419
420 if (oem_table_id == NULL)
421 oem_table_id = ACPI_TABLE_CREATOR;
422
423 oem_id_len = MIN(strlen(oem_id), 6);
424 oem_table_id_len = MIN(strlen(oem_table_id), 8);
425
426 memcpy(header->oem_id, oem_id, oem_id_len);
427 memcpy(header->oem_table_id, oem_table_id, oem_table_id_len);
Aaron Durbinb4afe3c2016-11-29 23:14:25 -0600428 write_le32(&header->oem_revision, oem_revision);
Aaron Durbin9420a522015-11-17 16:31:00 -0600429 memcpy(header->asl_compiler_id, ASLC, 4);
430
431 cur.buf = (void *)(acpi_addr + sizeof(acpi_header_t));
432 nhlt_serialize_endpoints(nhlt, &cur);
433
434 write_le8(&header->checksum, acpi_checksum((void *)header, sz));
435
436 nhlt_free_resources(nhlt);
437
438 acpi_addr += sz;
439 acpi_addr = ALIGN_UP(acpi_addr, 16);
440
441 return acpi_addr;
442}
Aaron Durbined0f6d72016-06-28 14:59:21 -0500443
444static int _nhlt_add_single_endpoint(struct nhlt *nhlt, int virtual_bus_id,
445 const struct nhlt_endp_descriptor *epd)
446{
447 struct nhlt_endpoint *endp;
448
449 endp = nhlt_add_endpoint(nhlt, epd->link, epd->device, epd->direction,
450 epd->vid, epd->did);
451
452 if (endp == NULL)
453 return -1;
454
455 endp->virtual_bus_id = virtual_bus_id;
456
457 if (nhlt_endpoint_append_config(endp, epd->cfg, epd->cfg_size))
458 return -1;
459
460 if (nhlt_endpoint_add_formats(endp, epd->formats, epd->num_formats))
461 return -1;
462
463 return 0;
464}
465
466static int _nhlt_add_endpoints(struct nhlt *nhlt, int virtual_bus_id,
467 const struct nhlt_endp_descriptor *epds,
468 size_t num_epds)
469{
470 size_t i;
471
472 for (i = 0; i < num_epds; i++)
473 if (_nhlt_add_single_endpoint(nhlt, virtual_bus_id, &epds[i]))
474 return -1;
475
476 return 0;
477}
478
479int nhlt_add_endpoints(struct nhlt *nhlt,
480 const struct nhlt_endp_descriptor *epds,
481 size_t num_epds)
482{
483 int ret;
484 ret = _nhlt_add_endpoints(nhlt, DEFAULT_VIRTUAL_BUS_ID, epds, num_epds);
485 return ret;
486}
487
488int nhlt_add_ssp_endpoints(struct nhlt *nhlt, int virtual_bus_id,
489 const struct nhlt_endp_descriptor *epds, size_t num_epds)
490{
491 int ret;
492
493 ret = _nhlt_add_endpoints(nhlt, virtual_bus_id, epds, num_epds);
494
495 if (!ret)
496 nhlt_next_instance(nhlt, NHLT_LINK_SSP);
497
498 return ret;
499}