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