1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause) 2 // Copyright(c) 2015-2020 Intel Corporation. 3 4 /* 5 * Bandwidth management algorithm based on 2^n gears 6 * 7 */ 8 9 #include <linux/bitops.h> 10 #include <linux/device.h> 11 #include <linux/module.h> 12 #include <linux/mod_devicetable.h> 13 #include <linux/slab.h> 14 #include <linux/soundwire/sdw.h> 15 #include "bus.h" 16 17 #define SDW_STRM_RATE_GROUPING 1 18 19 struct sdw_group_params { 20 unsigned int rate; 21 unsigned int lane; 22 int full_bw; 23 int payload_bw; 24 int hwidth; 25 }; 26 27 struct sdw_group { 28 unsigned int count; 29 unsigned int max_size; 30 unsigned int *rates; 31 unsigned int *lanes; 32 }; 33 34 void sdw_compute_slave_ports(struct sdw_master_runtime *m_rt, 35 struct sdw_transport_data *t_data) 36 { 37 struct sdw_slave_runtime *s_rt = NULL; 38 struct sdw_port_runtime *p_rt; 39 int port_bo, sample_int; 40 unsigned int rate, bps, ch = 0; 41 unsigned int slave_total_ch; 42 struct sdw_bus_params *b_params = &m_rt->bus->params; 43 44 port_bo = t_data->block_offset; 45 46 list_for_each_entry(s_rt, &m_rt->slave_rt_list, m_rt_node) { 47 rate = m_rt->stream->params.rate; 48 bps = m_rt->stream->params.bps; 49 sample_int = (m_rt->bus->params.curr_dr_freq / rate); 50 slave_total_ch = 0; 51 52 list_for_each_entry(p_rt, &s_rt->port_list, port_node) { 53 if (p_rt->lane != t_data->lane) 54 continue; 55 56 ch = hweight32(p_rt->ch_mask); 57 58 sdw_fill_xport_params(&p_rt->transport_params, 59 p_rt->num, false, 60 SDW_BLK_GRP_CNT_1, 61 sample_int, port_bo, port_bo >> 8, 62 t_data->hstart, 63 t_data->hstop, 64 SDW_BLK_PKG_PER_PORT, p_rt->lane); 65 66 sdw_fill_port_params(&p_rt->port_params, 67 p_rt->num, bps, 68 SDW_PORT_FLOW_MODE_ISOCH, 69 b_params->s_data_mode); 70 71 port_bo += bps * ch; 72 slave_total_ch += ch; 73 } 74 75 if (m_rt->direction == SDW_DATA_DIR_TX && 76 m_rt->ch_count == slave_total_ch) { 77 /* 78 * Slave devices were configured to access all channels 79 * of the stream, which indicates that they operate in 80 * 'mirror mode'. Make sure we reset the port offset for 81 * the next device in the list 82 */ 83 port_bo = t_data->block_offset; 84 } 85 } 86 } 87 EXPORT_SYMBOL(sdw_compute_slave_ports); 88 89 static void sdw_compute_dp0_slave_ports(struct sdw_master_runtime *m_rt) 90 { 91 struct sdw_bus *bus = m_rt->bus; 92 struct sdw_slave_runtime *s_rt; 93 struct sdw_port_runtime *p_rt; 94 95 list_for_each_entry(s_rt, &m_rt->slave_rt_list, m_rt_node) { 96 list_for_each_entry(p_rt, &s_rt->port_list, port_node) { 97 sdw_fill_xport_params(&p_rt->transport_params, p_rt->num, false, 98 SDW_BLK_GRP_CNT_1, bus->params.col, 0, 0, 1, 99 bus->params.col - 1, SDW_BLK_PKG_PER_PORT, 0x0); 100 101 sdw_fill_port_params(&p_rt->port_params, p_rt->num, bus->params.col - 1, 102 SDW_PORT_FLOW_MODE_ISOCH, SDW_PORT_DATA_MODE_NORMAL); 103 } 104 } 105 } 106 107 static void sdw_compute_dp0_master_ports(struct sdw_master_runtime *m_rt) 108 { 109 struct sdw_port_runtime *p_rt; 110 struct sdw_bus *bus = m_rt->bus; 111 112 list_for_each_entry(p_rt, &m_rt->port_list, port_node) { 113 sdw_fill_xport_params(&p_rt->transport_params, p_rt->num, false, 114 SDW_BLK_GRP_CNT_1, bus->params.col, 0, 0, 1, 115 bus->params.col - 1, SDW_BLK_PKG_PER_PORT, 0x0); 116 117 sdw_fill_port_params(&p_rt->port_params, p_rt->num, bus->params.col - 1, 118 SDW_PORT_FLOW_MODE_ISOCH, SDW_PORT_DATA_MODE_NORMAL); 119 } 120 } 121 122 static void sdw_compute_dp0_port_params(struct sdw_bus *bus) 123 { 124 struct sdw_master_runtime *m_rt; 125 126 list_for_each_entry(m_rt, &bus->m_rt_list, bus_node) { 127 /* DP0 is for BPT only */ 128 if (m_rt->stream->type != SDW_STREAM_BPT) 129 continue; 130 sdw_compute_dp0_master_ports(m_rt); 131 sdw_compute_dp0_slave_ports(m_rt); 132 } 133 } 134 135 static void sdw_compute_master_ports(struct sdw_master_runtime *m_rt, 136 struct sdw_group_params *params, 137 int *port_bo, int hstop) 138 { 139 struct sdw_transport_data t_data = {0}; 140 struct sdw_port_runtime *p_rt; 141 struct sdw_bus *bus = m_rt->bus; 142 struct sdw_bus_params *b_params = &bus->params; 143 int sample_int, hstart = 0; 144 unsigned int rate, bps, ch; 145 146 rate = m_rt->stream->params.rate; 147 bps = m_rt->stream->params.bps; 148 ch = m_rt->ch_count; 149 sample_int = (bus->params.curr_dr_freq / rate); 150 151 if (rate != params->rate) 152 return; 153 154 t_data.hstop = hstop; 155 hstart = hstop - params->hwidth + 1; 156 t_data.hstart = hstart; 157 158 list_for_each_entry(p_rt, &m_rt->port_list, port_node) { 159 if (p_rt->lane != params->lane) 160 continue; 161 162 sdw_fill_xport_params(&p_rt->transport_params, p_rt->num, 163 false, SDW_BLK_GRP_CNT_1, sample_int, 164 *port_bo, (*port_bo) >> 8, hstart, hstop, 165 SDW_BLK_PKG_PER_PORT, p_rt->lane); 166 167 sdw_fill_port_params(&p_rt->port_params, 168 p_rt->num, bps, 169 SDW_PORT_FLOW_MODE_ISOCH, 170 b_params->m_data_mode); 171 172 /* Check for first entry */ 173 if (!(p_rt == list_first_entry(&m_rt->port_list, 174 struct sdw_port_runtime, 175 port_node))) { 176 (*port_bo) += bps * ch; 177 continue; 178 } 179 180 t_data.hstart = hstart; 181 t_data.hstop = hstop; 182 t_data.block_offset = *port_bo; 183 t_data.sub_block_offset = 0; 184 (*port_bo) += bps * ch; 185 } 186 187 t_data.lane = params->lane; 188 sdw_compute_slave_ports(m_rt, &t_data); 189 } 190 191 static void _sdw_compute_port_params(struct sdw_bus *bus, 192 struct sdw_group_params *params, int count) 193 { 194 struct sdw_master_runtime *m_rt; 195 int port_bo, i, l; 196 int hstop; 197 198 /* Run loop for all groups to compute transport parameters */ 199 for (l = 0; l < SDW_MAX_LANES; l++) { 200 if (l > 0 && !bus->lane_used_bandwidth[l]) 201 continue; 202 /* reset hstop for each lane */ 203 hstop = bus->params.col - 1; 204 for (i = 0; i < count; i++) { 205 if (params[i].lane != l) 206 continue; 207 port_bo = 1; 208 209 list_for_each_entry(m_rt, &bus->m_rt_list, bus_node) { 210 /* 211 * Only runtimes with CONFIGURED, PREPARED, ENABLED, and DISABLED 212 * states should be included in the bandwidth calculation. 213 */ 214 if (m_rt->stream->state > SDW_STREAM_DISABLED || 215 m_rt->stream->state < SDW_STREAM_CONFIGURED) 216 continue; 217 sdw_compute_master_ports(m_rt, ¶ms[i], &port_bo, hstop); 218 } 219 220 hstop = hstop - params[i].hwidth; 221 } 222 } 223 } 224 225 static int sdw_compute_group_params(struct sdw_bus *bus, 226 struct sdw_stream_runtime *stream, 227 struct sdw_group_params *params, 228 struct sdw_group *group) 229 { 230 struct sdw_master_runtime *m_rt; 231 struct sdw_port_runtime *p_rt; 232 int sel_col = bus->params.col; 233 unsigned int rate, bps, ch; 234 int i, l, column_needed; 235 236 /* Calculate bandwidth per group */ 237 for (i = 0; i < group->count; i++) { 238 params[i].rate = group->rates[i]; 239 params[i].lane = group->lanes[i]; 240 params[i].full_bw = bus->params.curr_dr_freq / params[i].rate; 241 } 242 243 list_for_each_entry(m_rt, &bus->m_rt_list, bus_node) { 244 if (m_rt->stream == stream) { 245 /* Only runtime during prepare should be added */ 246 if (stream->state != SDW_STREAM_CONFIGURED) 247 continue; 248 } else { 249 /* 250 * Include runtimes with running (ENABLED/PREPARED state) and 251 * paused (DISABLED state) streams 252 */ 253 if (m_rt->stream->state != SDW_STREAM_ENABLED && 254 m_rt->stream->state != SDW_STREAM_PREPARED && 255 m_rt->stream->state != SDW_STREAM_DISABLED) 256 continue; 257 } 258 list_for_each_entry(p_rt, &m_rt->port_list, port_node) { 259 rate = m_rt->stream->params.rate; 260 bps = m_rt->stream->params.bps; 261 ch = hweight32(p_rt->ch_mask); 262 263 for (i = 0; i < group->count; i++) { 264 if (rate == params[i].rate && p_rt->lane == params[i].lane) 265 params[i].payload_bw += bps * ch; 266 } 267 } 268 } 269 270 for (l = 0; l < SDW_MAX_LANES; l++) { 271 if (l > 0 && !bus->lane_used_bandwidth[l]) 272 continue; 273 /* reset column_needed for each lane */ 274 column_needed = 0; 275 for (i = 0; i < group->count; i++) { 276 if (params[i].lane != l) 277 continue; 278 279 params[i].hwidth = (sel_col * params[i].payload_bw + 280 params[i].full_bw - 1) / params[i].full_bw; 281 282 column_needed += params[i].hwidth; 283 /* There is no control column for lane 1 and above */ 284 if (column_needed > sel_col) 285 return -EINVAL; 286 /* Column 0 is control column on lane 0 */ 287 if (params[i].lane == 0 && column_needed > sel_col - 1) 288 return -EINVAL; 289 } 290 } 291 292 293 return 0; 294 } 295 296 static int sdw_add_element_group_count(struct sdw_group *group, 297 unsigned int rate, unsigned int lane) 298 { 299 int num = group->count; 300 int i; 301 302 for (i = 0; i < num; i++) { 303 if (rate == group->rates[i] && lane == group->lanes[i]) 304 return 0; 305 } 306 307 if (group->count >= group->max_size) { 308 unsigned int *rates; 309 unsigned int *lanes; 310 311 rates = krealloc_array(group->rates, group->max_size + 1, 312 sizeof(*group->rates), GFP_KERNEL); 313 if (!rates) 314 return -ENOMEM; 315 316 group->rates = rates; 317 318 lanes = krealloc_array(group->lanes, group->max_size + 1, 319 sizeof(*group->lanes), GFP_KERNEL); 320 if (!lanes) 321 return -ENOMEM; 322 323 group->lanes = lanes; 324 325 group->max_size += 1; 326 } 327 328 group->rates[group->count] = rate; 329 group->lanes[group->count++] = lane; 330 331 return 0; 332 } 333 334 static int sdw_get_group_count(struct sdw_bus *bus, 335 struct sdw_group *group) 336 { 337 struct sdw_master_runtime *m_rt; 338 struct sdw_port_runtime *p_rt; 339 unsigned int rate; 340 int ret = 0; 341 342 group->count = 0; 343 group->max_size = SDW_STRM_RATE_GROUPING; 344 group->rates = kcalloc(group->max_size, sizeof(int), GFP_KERNEL); 345 if (!group->rates) 346 return -ENOMEM; 347 348 group->lanes = kcalloc(group->max_size, sizeof(int), GFP_KERNEL); 349 if (!group->lanes) { 350 kfree(group->rates); 351 group->rates = NULL; 352 return -ENOMEM; 353 } 354 355 list_for_each_entry(m_rt, &bus->m_rt_list, bus_node) { 356 if (m_rt->stream->state == SDW_STREAM_DEPREPARED) 357 continue; 358 359 rate = m_rt->stream->params.rate; 360 if (m_rt == list_first_entry(&bus->m_rt_list, 361 struct sdw_master_runtime, 362 bus_node)) { 363 group->rates[group->count++] = rate; 364 } 365 /* 366 * Different ports could use different lane, add group element 367 * even if m_rt is the first entry 368 */ 369 list_for_each_entry(p_rt, &m_rt->port_list, port_node) { 370 ret = sdw_add_element_group_count(group, rate, p_rt->lane); 371 if (ret < 0) { 372 kfree(group->rates); 373 kfree(group->lanes); 374 return ret; 375 } 376 } 377 } 378 379 return ret; 380 } 381 382 /** 383 * sdw_compute_port_params: Compute transport and port parameters 384 * 385 * @bus: SDW Bus instance 386 * @stream: Soundwire stream 387 */ 388 static int sdw_compute_port_params(struct sdw_bus *bus, struct sdw_stream_runtime *stream) 389 { 390 struct sdw_group_params *params = NULL; 391 struct sdw_group group; 392 int ret; 393 394 ret = sdw_get_group_count(bus, &group); 395 if (ret < 0) 396 return ret; 397 398 if (group.count == 0) 399 goto out; 400 401 params = kzalloc_objs(*params, group.count); 402 if (!params) { 403 ret = -ENOMEM; 404 goto out; 405 } 406 407 /* Compute transport parameters for grouped streams */ 408 ret = sdw_compute_group_params(bus, stream, params, &group); 409 if (ret < 0) 410 goto free_params; 411 412 _sdw_compute_port_params(bus, params, group.count); 413 414 free_params: 415 kfree(params); 416 out: 417 kfree(group.rates); 418 kfree(group.lanes); 419 420 return ret; 421 } 422 423 static int sdw_select_row_col(struct sdw_bus *bus, int clk_freq) 424 { 425 struct sdw_master_prop *prop = &bus->prop; 426 int r, c; 427 428 for (c = 0; c < SDW_FRAME_COLS; c++) { 429 for (r = 0; r < SDW_FRAME_ROWS; r++) { 430 if (sdw_rows[r] != prop->default_row || 431 sdw_cols[c] != prop->default_col) 432 continue; 433 434 if (clk_freq * (sdw_cols[c] - 1) < 435 bus->params.bandwidth * sdw_cols[c]) 436 continue; 437 438 bus->params.row = sdw_rows[r]; 439 bus->params.col = sdw_cols[c]; 440 return 0; 441 } 442 } 443 444 return -EINVAL; 445 } 446 447 static bool is_clock_scaling_supported(struct sdw_bus *bus) 448 { 449 struct sdw_master_runtime *m_rt; 450 struct sdw_slave_runtime *s_rt; 451 452 list_for_each_entry(m_rt, &bus->m_rt_list, bus_node) 453 list_for_each_entry(s_rt, &m_rt->slave_rt_list, m_rt_node) 454 if (!is_clock_scaling_supported_by_slave(s_rt->slave)) 455 return false; 456 457 return true; 458 } 459 460 /** 461 * is_lane_connected_to_all_peripherals: Check if the given manager lane connects to all peripherals 462 * So that all peripherals can use the manager lane. 463 * 464 * @m_rt: Manager runtime 465 * @lane: Lane number 466 */ 467 static bool is_lane_connected_to_all_peripherals(struct sdw_master_runtime *m_rt, unsigned int lane) 468 { 469 struct sdw_slave_prop *slave_prop; 470 struct sdw_slave_runtime *s_rt; 471 int i; 472 473 list_for_each_entry(s_rt, &m_rt->slave_rt_list, m_rt_node) { 474 slave_prop = &s_rt->slave->prop; 475 for (i = 1; i < SDW_MAX_LANES; i++) { 476 if (slave_prop->lane_maps[i] == lane) { 477 dev_dbg(&s_rt->slave->dev, 478 "M lane %d is connected to P lane %d\n", 479 lane, i); 480 break; 481 } 482 } 483 if (i == SDW_MAX_LANES) { 484 dev_dbg(&s_rt->slave->dev, "M lane %d is not connected\n", lane); 485 return false; 486 } 487 } 488 return true; 489 } 490 491 static int get_manager_lane(struct sdw_bus *bus, struct sdw_master_runtime *m_rt, 492 struct sdw_slave_runtime *s_rt, unsigned int curr_dr_freq) 493 { 494 struct sdw_slave_prop *slave_prop = &s_rt->slave->prop; 495 struct sdw_port_runtime *m_p_rt; 496 unsigned int required_bandwidth; 497 int m_lane; 498 int l; 499 500 for (l = 1; l < SDW_MAX_LANES; l++) { 501 if (!slave_prop->lane_maps[l]) 502 continue; 503 504 required_bandwidth = 0; 505 list_for_each_entry(m_p_rt, &m_rt->port_list, port_node) { 506 required_bandwidth += m_rt->stream->params.rate * 507 hweight32(m_p_rt->ch_mask) * 508 m_rt->stream->params.bps; 509 } 510 if (required_bandwidth <= 511 curr_dr_freq - bus->lane_used_bandwidth[l]) { 512 /* Check if m_lane is connected to all Peripherals */ 513 if (!is_lane_connected_to_all_peripherals(m_rt, 514 slave_prop->lane_maps[l])) { 515 dev_dbg(bus->dev, 516 "Not all Peripherals are connected to M lane %d\n", 517 slave_prop->lane_maps[l]); 518 continue; 519 } 520 m_lane = slave_prop->lane_maps[l]; 521 dev_dbg(&s_rt->slave->dev, "M lane %d is used\n", m_lane); 522 bus->lane_used_bandwidth[l] += required_bandwidth; 523 /* 524 * Use non-zero manager lane, subtract the lane 0 525 * bandwidth that is already calculated 526 */ 527 bus->params.bandwidth -= required_bandwidth; 528 return m_lane; 529 } 530 } 531 532 /* No available multi lane found, only lane 0 can be used */ 533 return 0; 534 } 535 536 /** 537 * sdw_compute_bus_params: Compute bus parameters 538 * 539 * @bus: SDW Bus instance 540 */ 541 static int sdw_compute_bus_params(struct sdw_bus *bus) 542 { 543 struct sdw_master_prop *mstr_prop = &bus->prop; 544 struct sdw_slave_prop *slave_prop; 545 struct sdw_port_runtime *m_p_rt; 546 struct sdw_port_runtime *s_p_rt; 547 struct sdw_master_runtime *m_rt; 548 struct sdw_slave_runtime *s_rt; 549 unsigned int curr_dr_freq = 0; 550 int i, l, clk_values, ret; 551 bool is_gear = false; 552 int m_lane = 0; 553 u32 *clk_buf; 554 555 if (mstr_prop->num_clk_gears) { 556 clk_values = mstr_prop->num_clk_gears; 557 clk_buf = mstr_prop->clk_gears; 558 is_gear = true; 559 } else if (mstr_prop->num_clk_freq) { 560 clk_values = mstr_prop->num_clk_freq; 561 clk_buf = mstr_prop->clk_freq; 562 } else { 563 clk_values = 1; 564 clk_buf = NULL; 565 } 566 567 /* If dynamic scaling is not supported, don't try higher freq */ 568 if (!is_clock_scaling_supported(bus)) 569 clk_values = 1; 570 571 for (i = 0; i < clk_values; i++) { 572 if (!clk_buf) 573 curr_dr_freq = bus->params.max_dr_freq; 574 else 575 curr_dr_freq = (is_gear) ? 576 (bus->params.max_dr_freq >> clk_buf[i]) : 577 clk_buf[i] * SDW_DOUBLE_RATE_FACTOR; 578 579 if (curr_dr_freq * (mstr_prop->default_col - 1) >= 580 bus->params.bandwidth * mstr_prop->default_col) 581 break; 582 583 list_for_each_entry(m_rt, &bus->m_rt_list, bus_node) { 584 /* 585 * Get the first s_rt that will be used to find the available lane that 586 * can be used. No need to check all Peripherals because we can't use 587 * multi-lane if we can't find any available lane for the first Peripheral. 588 */ 589 s_rt = list_first_entry(&m_rt->slave_rt_list, 590 struct sdw_slave_runtime, m_rt_node); 591 592 /* 593 * Find the available Manager lane that connected to the first Peripheral. 594 */ 595 m_lane = get_manager_lane(bus, m_rt, s_rt, curr_dr_freq); 596 if (m_lane > 0) 597 goto out; 598 } 599 600 /* 601 * TODO: Check all the Slave(s) port(s) audio modes and find 602 * whether given clock rate is supported with glitchless 603 * transition. 604 */ 605 } 606 607 if (i == clk_values) { 608 dev_err(bus->dev, "%s: could not find clock value for bandwidth %d\n", 609 __func__, bus->params.bandwidth); 610 return -EINVAL; 611 } 612 out: 613 /* multilane can be used */ 614 if (m_lane > 0) { 615 /* Set Peripheral lanes */ 616 list_for_each_entry(s_rt, &m_rt->slave_rt_list, m_rt_node) { 617 slave_prop = &s_rt->slave->prop; 618 for (l = 1; l < SDW_MAX_LANES; l++) { 619 if (slave_prop->lane_maps[l] == m_lane) { 620 list_for_each_entry(s_p_rt, &s_rt->port_list, port_node) { 621 s_p_rt->lane = l; 622 dev_dbg(&s_rt->slave->dev, 623 "Set P lane %d for port %d\n", 624 l, s_p_rt->num); 625 } 626 break; 627 } 628 } 629 } 630 /* 631 * Set Manager lanes. Configure the last m_rt in bus->m_rt_list only since 632 * we don't want to touch other m_rts that are already working. 633 */ 634 list_for_each_entry(m_p_rt, &m_rt->port_list, port_node) { 635 m_p_rt->lane = m_lane; 636 } 637 } 638 639 if (!mstr_prop->default_frame_rate || !mstr_prop->default_row) 640 return -EINVAL; 641 642 mstr_prop->default_col = curr_dr_freq / mstr_prop->default_frame_rate / 643 mstr_prop->default_row; 644 645 ret = sdw_select_row_col(bus, curr_dr_freq); 646 if (ret < 0) { 647 dev_err(bus->dev, "%s: could not find frame configuration for bus dr_freq %d\n", 648 __func__, curr_dr_freq); 649 return -EINVAL; 650 } 651 652 bus->params.curr_dr_freq = curr_dr_freq; 653 return 0; 654 } 655 656 /** 657 * sdw_compute_params: Compute bus, transport and port parameters 658 * 659 * @bus: SDW Bus instance 660 * @stream: Soundwire stream 661 */ 662 int sdw_compute_params(struct sdw_bus *bus, struct sdw_stream_runtime *stream) 663 { 664 int ret; 665 666 /* Computes clock frequency, frame shape and frame frequency */ 667 ret = sdw_compute_bus_params(bus); 668 if (ret < 0) 669 return ret; 670 671 if (stream->type == SDW_STREAM_BPT) { 672 sdw_compute_dp0_port_params(bus); 673 return 0; 674 } 675 676 /* Compute transport and port params */ 677 ret = sdw_compute_port_params(bus, stream); 678 if (ret < 0) { 679 dev_err(bus->dev, "Compute transport params failed: %d\n", ret); 680 return ret; 681 } 682 683 return 0; 684 } 685 EXPORT_SYMBOL(sdw_compute_params); 686 687 MODULE_LICENSE("Dual BSD/GPL"); 688 MODULE_DESCRIPTION("SoundWire Generic Bandwidth Allocation"); 689