1 /*
2 * Copyright 2012-15 Advanced Micro Devices, Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 *
22 * Authors: AMD
23 *
24 */
25
26 #include <linux/vmalloc.h>
27 #include <drm/display/drm_dp_helper.h>
28 #include <drm/display/drm_dp_mst_helper.h>
29 #include <drm/drm_atomic.h>
30 #include <drm/drm_atomic_helper.h>
31 #include <drm/drm_fixed.h>
32 #include <drm/drm_edid.h>
33 #include "dm_services.h"
34 #include "amdgpu.h"
35 #include "amdgpu_dm.h"
36 #include "amdgpu_dm_mst_types.h"
37 #include "amdgpu_dm_hdcp.h"
38
39 #include "dc.h"
40 #include "dm_helpers.h"
41
42 #include "ddc_service_types.h"
43 #include "dpcd_defs.h"
44
45 #include "dmub_cmd.h"
46 #if defined(CONFIG_DEBUG_FS)
47 #include "amdgpu_dm_debugfs.h"
48 #endif
49
50 #include "dc/resource/dcn20/dcn20_resource.h"
51
52 #define PEAK_FACTOR_X1000 1006
53
dm_dp_aux_transfer(struct drm_dp_aux * aux,struct drm_dp_aux_msg * msg)54 static ssize_t dm_dp_aux_transfer(struct drm_dp_aux *aux,
55 struct drm_dp_aux_msg *msg)
56 {
57 ssize_t result = 0;
58 struct aux_payload payload;
59 enum aux_return_code_type operation_result;
60 struct amdgpu_device *adev;
61 struct ddc_service *ddc;
62
63 if (WARN_ON(msg->size > 16))
64 return -E2BIG;
65
66 payload.address = msg->address;
67 payload.data = msg->buffer;
68 payload.length = msg->size;
69 payload.reply = &msg->reply;
70 payload.i2c_over_aux = (msg->request & DP_AUX_NATIVE_WRITE) == 0;
71 payload.write = (msg->request & DP_AUX_I2C_READ) == 0;
72 payload.mot = (msg->request & DP_AUX_I2C_MOT) != 0;
73 payload.write_status_update =
74 (msg->request & DP_AUX_I2C_WRITE_STATUS_UPDATE) != 0;
75 payload.defer_delay = 0;
76
77 result = dc_link_aux_transfer_raw(TO_DM_AUX(aux)->ddc_service, &payload,
78 &operation_result);
79
80 /*
81 * w/a on certain intel platform where hpd is unexpected to pull low during
82 * 1st sideband message transaction by return AUX_RET_ERROR_HPD_DISCON
83 * aux transaction is succuess in such case, therefore bypass the error
84 */
85 ddc = TO_DM_AUX(aux)->ddc_service;
86 adev = ddc->ctx->driver_context;
87 if (adev->dm.aux_hpd_discon_quirk) {
88 if (msg->address == DP_SIDEBAND_MSG_DOWN_REQ_BASE &&
89 operation_result == AUX_RET_ERROR_HPD_DISCON) {
90 result = 0;
91 operation_result = AUX_RET_SUCCESS;
92 }
93 }
94
95 if (payload.write && result >= 0)
96 result = msg->size;
97
98 if (result < 0)
99 switch (operation_result) {
100 case AUX_RET_SUCCESS:
101 break;
102 case AUX_RET_ERROR_HPD_DISCON:
103 case AUX_RET_ERROR_UNKNOWN:
104 case AUX_RET_ERROR_INVALID_OPERATION:
105 case AUX_RET_ERROR_PROTOCOL_ERROR:
106 result = -EIO;
107 break;
108 case AUX_RET_ERROR_INVALID_REPLY:
109 case AUX_RET_ERROR_ENGINE_ACQUIRE:
110 result = -EBUSY;
111 break;
112 case AUX_RET_ERROR_TIMEOUT:
113 result = -ETIMEDOUT;
114 break;
115 }
116
117 return result;
118 }
119
120 static void
dm_dp_mst_connector_destroy(struct drm_connector * connector)121 dm_dp_mst_connector_destroy(struct drm_connector *connector)
122 {
123 struct amdgpu_dm_connector *aconnector =
124 to_amdgpu_dm_connector(connector);
125
126 if (aconnector->dc_sink) {
127 dc_link_remove_remote_sink(aconnector->dc_link,
128 aconnector->dc_sink);
129 dc_sink_release(aconnector->dc_sink);
130 }
131
132 kfree(aconnector->edid);
133
134 drm_connector_cleanup(connector);
135 drm_dp_mst_put_port_malloc(aconnector->mst_output_port);
136 kfree(aconnector);
137 }
138
139 static int
amdgpu_dm_mst_connector_late_register(struct drm_connector * connector)140 amdgpu_dm_mst_connector_late_register(struct drm_connector *connector)
141 {
142 struct amdgpu_dm_connector *amdgpu_dm_connector =
143 to_amdgpu_dm_connector(connector);
144 int r;
145
146 r = drm_dp_mst_connector_late_register(connector,
147 amdgpu_dm_connector->mst_output_port);
148 if (r < 0)
149 return r;
150
151 #if defined(CONFIG_DEBUG_FS)
152 connector_debugfs_init(amdgpu_dm_connector);
153 #endif
154
155 return 0;
156 }
157
158 static void
amdgpu_dm_mst_connector_early_unregister(struct drm_connector * connector)159 amdgpu_dm_mst_connector_early_unregister(struct drm_connector *connector)
160 {
161 struct amdgpu_dm_connector *aconnector =
162 to_amdgpu_dm_connector(connector);
163 struct drm_dp_mst_port *port = aconnector->mst_output_port;
164 struct amdgpu_dm_connector *root = aconnector->mst_root;
165 struct dc_link *dc_link = aconnector->dc_link;
166 struct dc_sink *dc_sink = aconnector->dc_sink;
167
168 drm_dp_mst_connector_early_unregister(connector, port);
169
170 /*
171 * Release dc_sink for connector which its attached port is
172 * no longer in the mst topology
173 */
174 drm_modeset_lock(&root->mst_mgr.base.lock, NULL);
175 if (dc_sink) {
176 if (dc_link->sink_count)
177 dc_link_remove_remote_sink(dc_link, dc_sink);
178
179 drm_dbg_dp(connector->dev,
180 "DM_MST: remove remote sink 0x%p, %d remaining\n",
181 dc_sink, dc_link->sink_count);
182
183 dc_sink_release(dc_sink);
184 aconnector->dc_sink = NULL;
185 aconnector->edid = NULL;
186 aconnector->dsc_aux = NULL;
187 port->passthrough_aux = NULL;
188 }
189
190 aconnector->mst_status = MST_STATUS_DEFAULT;
191 drm_modeset_unlock(&root->mst_mgr.base.lock);
192 }
193
194 static const struct drm_connector_funcs dm_dp_mst_connector_funcs = {
195 .fill_modes = drm_helper_probe_single_connector_modes,
196 .destroy = dm_dp_mst_connector_destroy,
197 .reset = amdgpu_dm_connector_funcs_reset,
198 .atomic_duplicate_state = amdgpu_dm_connector_atomic_duplicate_state,
199 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
200 .atomic_set_property = amdgpu_dm_connector_atomic_set_property,
201 .atomic_get_property = amdgpu_dm_connector_atomic_get_property,
202 .late_register = amdgpu_dm_mst_connector_late_register,
203 .early_unregister = amdgpu_dm_mst_connector_early_unregister,
204 };
205
needs_dsc_aux_workaround(struct dc_link * link)206 bool needs_dsc_aux_workaround(struct dc_link *link)
207 {
208 if (link->dpcd_caps.branch_dev_id == DP_BRANCH_DEVICE_ID_90CC24 &&
209 (link->dpcd_caps.dpcd_rev.raw == DPCD_REV_14 || link->dpcd_caps.dpcd_rev.raw == DPCD_REV_12) &&
210 link->dpcd_caps.sink_count.bits.SINK_COUNT >= 2)
211 return true;
212
213 return false;
214 }
215
216 #if defined(CONFIG_DRM_AMD_DC_FP)
is_synaptics_cascaded_panamera(struct dc_link * link,struct drm_dp_mst_port * port)217 static bool is_synaptics_cascaded_panamera(struct dc_link *link, struct drm_dp_mst_port *port)
218 {
219 u8 branch_vendor_data[4] = { 0 }; // Vendor data 0x50C ~ 0x50F
220
221 if (drm_dp_dpcd_read(port->mgr->aux, DP_BRANCH_VENDOR_SPECIFIC_START, &branch_vendor_data, 4) == 4) {
222 if (link->dpcd_caps.branch_dev_id == DP_BRANCH_DEVICE_ID_90CC24 &&
223 IS_SYNAPTICS_CASCADED_PANAMERA(link->dpcd_caps.branch_dev_name, branch_vendor_data)) {
224 DRM_INFO("Synaptics Cascaded MST hub\n");
225 return true;
226 }
227 }
228
229 return false;
230 }
231
validate_dsc_caps_on_connector(struct amdgpu_dm_connector * aconnector)232 static bool validate_dsc_caps_on_connector(struct amdgpu_dm_connector *aconnector)
233 {
234 struct dc_sink *dc_sink = aconnector->dc_sink;
235 struct drm_dp_mst_port *port = aconnector->mst_output_port;
236 u8 dsc_caps[16] = { 0 };
237 u8 dsc_branch_dec_caps_raw[3] = { 0 }; // DSC branch decoder caps 0xA0 ~ 0xA2
238 u8 *dsc_branch_dec_caps = NULL;
239
240 aconnector->dsc_aux = drm_dp_mst_dsc_aux_for_port(port);
241
242 /*
243 * drm_dp_mst_dsc_aux_for_port() will return NULL for certain configs
244 * because it only check the dsc/fec caps of the "port variable" and not the dock
245 *
246 * This case will return NULL: DSC capabe MST dock connected to a non fec/dsc capable display
247 *
248 * Workaround: explicitly check the use case above and use the mst dock's aux as dsc_aux
249 *
250 */
251 if (!aconnector->dsc_aux && !port->parent->port_parent &&
252 needs_dsc_aux_workaround(aconnector->dc_link))
253 aconnector->dsc_aux = &aconnector->mst_root->dm_dp_aux.aux;
254
255 /* synaptics cascaded MST hub case */
256 if (is_synaptics_cascaded_panamera(aconnector->dc_link, port))
257 aconnector->dsc_aux = port->mgr->aux;
258
259 if (!aconnector->dsc_aux)
260 return false;
261
262 if (drm_dp_dpcd_read(aconnector->dsc_aux, DP_DSC_SUPPORT, dsc_caps, 16) < 0)
263 return false;
264
265 if (drm_dp_dpcd_read(aconnector->dsc_aux,
266 DP_DSC_BRANCH_OVERALL_THROUGHPUT_0, dsc_branch_dec_caps_raw, 3) == 3)
267 dsc_branch_dec_caps = dsc_branch_dec_caps_raw;
268
269 if (!dc_dsc_parse_dsc_dpcd(aconnector->dc_link->ctx->dc,
270 dsc_caps, dsc_branch_dec_caps,
271 &dc_sink->dsc_caps.dsc_dec_caps))
272 return false;
273
274 return true;
275 }
276 #endif
277
retrieve_downstream_port_device(struct amdgpu_dm_connector * aconnector)278 static bool retrieve_downstream_port_device(struct amdgpu_dm_connector *aconnector)
279 {
280 union dp_downstream_port_present ds_port_present;
281
282 if (!aconnector->dsc_aux)
283 return false;
284
285 if (drm_dp_dpcd_read(aconnector->dsc_aux, DP_DOWNSTREAMPORT_PRESENT, &ds_port_present, 1) < 0) {
286 DRM_INFO("Failed to read downstream_port_present 0x05 from DFP of branch device\n");
287 return false;
288 }
289
290 aconnector->mst_downstream_port_present = ds_port_present;
291 DRM_INFO("Downstream port present %d, type %d\n",
292 ds_port_present.fields.PORT_PRESENT, ds_port_present.fields.PORT_TYPE);
293
294 return true;
295 }
296
dm_dp_mst_get_modes(struct drm_connector * connector)297 static int dm_dp_mst_get_modes(struct drm_connector *connector)
298 {
299 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
300 int ret = 0;
301
302 if (!aconnector)
303 return drm_add_edid_modes(connector, NULL);
304
305 if (!aconnector->edid) {
306 struct edid *edid;
307
308 edid = drm_dp_mst_get_edid(connector, &aconnector->mst_root->mst_mgr, aconnector->mst_output_port);
309
310 if (!edid) {
311 amdgpu_dm_set_mst_status(&aconnector->mst_status,
312 MST_REMOTE_EDID, false);
313
314 drm_connector_update_edid_property(
315 &aconnector->base,
316 NULL);
317
318 DRM_DEBUG_KMS("Can't get EDID of %s. Add default remote sink.", connector->name);
319 if (!aconnector->dc_sink) {
320 struct dc_sink *dc_sink;
321 struct dc_sink_init_data init_params = {
322 .link = aconnector->dc_link,
323 .sink_signal = SIGNAL_TYPE_DISPLAY_PORT_MST };
324
325 dc_sink = dc_link_add_remote_sink(
326 aconnector->dc_link,
327 NULL,
328 0,
329 &init_params);
330
331 if (!dc_sink) {
332 DRM_ERROR("Unable to add a remote sink\n");
333 return 0;
334 }
335
336 drm_dbg_dp(connector->dev,
337 "DM_MST: add remote sink 0x%p, %d remaining\n",
338 dc_sink,
339 aconnector->dc_link->sink_count);
340
341 dc_sink->priv = aconnector;
342 aconnector->dc_sink = dc_sink;
343 }
344
345 return ret;
346 }
347
348 aconnector->edid = edid;
349 amdgpu_dm_set_mst_status(&aconnector->mst_status,
350 MST_REMOTE_EDID, true);
351 }
352
353 if (aconnector->dc_sink && aconnector->dc_sink->sink_signal == SIGNAL_TYPE_VIRTUAL) {
354 dc_sink_release(aconnector->dc_sink);
355 aconnector->dc_sink = NULL;
356 }
357
358 if (!aconnector->dc_sink) {
359 struct dc_sink *dc_sink;
360 struct dc_sink_init_data init_params = {
361 .link = aconnector->dc_link,
362 .sink_signal = SIGNAL_TYPE_DISPLAY_PORT_MST };
363 dc_sink = dc_link_add_remote_sink(
364 aconnector->dc_link,
365 (uint8_t *)aconnector->edid,
366 (aconnector->edid->extensions + 1) * EDID_LENGTH,
367 &init_params);
368
369 if (!dc_sink) {
370 DRM_ERROR("Unable to add a remote sink\n");
371 return 0;
372 }
373
374 drm_dbg_dp(connector->dev,
375 "DM_MST: add remote sink 0x%p, %d remaining\n",
376 dc_sink, aconnector->dc_link->sink_count);
377
378 dc_sink->priv = aconnector;
379 /* dc_link_add_remote_sink returns a new reference */
380 aconnector->dc_sink = dc_sink;
381
382 /* when display is unplugged from mst hub, connctor will be
383 * destroyed within dm_dp_mst_connector_destroy. connector
384 * hdcp perperties, like type, undesired, desired, enabled,
385 * will be lost. So, save hdcp properties into hdcp_work within
386 * amdgpu_dm_atomic_commit_tail. if the same display is
387 * plugged back with same display index, its hdcp properties
388 * will be retrieved from hdcp_work within dm_dp_mst_get_modes
389 */
390 if (aconnector->dc_sink && connector->state) {
391 struct drm_device *dev = connector->dev;
392 struct amdgpu_device *adev = drm_to_adev(dev);
393
394 if (adev->dm.hdcp_workqueue) {
395 struct hdcp_workqueue *hdcp_work = adev->dm.hdcp_workqueue;
396 struct hdcp_workqueue *hdcp_w =
397 &hdcp_work[aconnector->dc_link->link_index];
398
399 connector->state->hdcp_content_type =
400 hdcp_w->hdcp_content_type[connector->index];
401 connector->state->content_protection =
402 hdcp_w->content_protection[connector->index];
403 }
404 }
405
406 if (aconnector->dc_sink) {
407 amdgpu_dm_update_freesync_caps(
408 connector, aconnector->edid);
409
410 #if defined(CONFIG_DRM_AMD_DC_FP)
411 if (!validate_dsc_caps_on_connector(aconnector))
412 memset(&aconnector->dc_sink->dsc_caps,
413 0, sizeof(aconnector->dc_sink->dsc_caps));
414 #endif
415
416 if (!retrieve_downstream_port_device(aconnector))
417 memset(&aconnector->mst_downstream_port_present,
418 0, sizeof(aconnector->mst_downstream_port_present));
419 }
420 }
421
422 drm_connector_update_edid_property(
423 &aconnector->base, aconnector->edid);
424
425 ret = drm_add_edid_modes(connector, aconnector->edid);
426
427 return ret;
428 }
429
430 static struct drm_encoder *
dm_mst_atomic_best_encoder(struct drm_connector * connector,struct drm_atomic_state * state)431 dm_mst_atomic_best_encoder(struct drm_connector *connector,
432 struct drm_atomic_state *state)
433 {
434 struct drm_connector_state *connector_state = drm_atomic_get_new_connector_state(state,
435 connector);
436 struct amdgpu_device *adev = drm_to_adev(connector->dev);
437 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(connector_state->crtc);
438
439 return &adev->dm.mst_encoders[acrtc->crtc_id].base;
440 }
441
442 static int
dm_dp_mst_detect(struct drm_connector * connector,struct drm_modeset_acquire_ctx * ctx,bool force)443 dm_dp_mst_detect(struct drm_connector *connector,
444 struct drm_modeset_acquire_ctx *ctx, bool force)
445 {
446 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
447 struct amdgpu_dm_connector *master = aconnector->mst_root;
448 struct drm_dp_mst_port *port = aconnector->mst_output_port;
449 int connection_status;
450
451 if (drm_connector_is_unregistered(connector))
452 return connector_status_disconnected;
453
454 connection_status = drm_dp_mst_detect_port(connector, ctx, &master->mst_mgr,
455 aconnector->mst_output_port);
456
457 if (port->pdt != DP_PEER_DEVICE_NONE && !port->dpcd_rev) {
458 uint8_t dpcd_rev;
459 int ret;
460
461 ret = drm_dp_dpcd_readb(&port->aux, DP_DP13_DPCD_REV, &dpcd_rev);
462
463 if (ret == 1) {
464 port->dpcd_rev = dpcd_rev;
465
466 /* Could be DP1.2 DP Rx case*/
467 if (!dpcd_rev) {
468 ret = drm_dp_dpcd_readb(&port->aux, DP_DPCD_REV, &dpcd_rev);
469
470 if (ret == 1)
471 port->dpcd_rev = dpcd_rev;
472 }
473
474 if (!dpcd_rev)
475 DRM_DEBUG_KMS("Can't decide DPCD revision number!");
476 }
477
478 /*
479 * Could be legacy sink, logical port etc on DP1.2.
480 * Will get Nack under these cases when issue remote
481 * DPCD read.
482 */
483 if (ret != 1)
484 DRM_DEBUG_KMS("Can't access DPCD");
485 } else if (port->pdt == DP_PEER_DEVICE_NONE) {
486 port->dpcd_rev = 0;
487 }
488
489 /*
490 * Release dc_sink for connector which unplug event is notified by CSN msg
491 */
492 if (connection_status == connector_status_disconnected && aconnector->dc_sink) {
493 if (aconnector->dc_link->sink_count)
494 dc_link_remove_remote_sink(aconnector->dc_link, aconnector->dc_sink);
495
496 drm_dbg_dp(connector->dev,
497 "DM_MST: remove remote sink 0x%p, %d remaining\n",
498 aconnector->dc_link,
499 aconnector->dc_link->sink_count);
500
501 dc_sink_release(aconnector->dc_sink);
502 aconnector->dc_sink = NULL;
503 aconnector->edid = NULL;
504 aconnector->dsc_aux = NULL;
505 port->passthrough_aux = NULL;
506
507 amdgpu_dm_set_mst_status(&aconnector->mst_status,
508 MST_REMOTE_EDID | MST_ALLOCATE_NEW_PAYLOAD | MST_CLEAR_ALLOCATED_PAYLOAD,
509 false);
510 }
511
512 return connection_status;
513 }
514
dm_dp_mst_atomic_check(struct drm_connector * connector,struct drm_atomic_state * state)515 static int dm_dp_mst_atomic_check(struct drm_connector *connector,
516 struct drm_atomic_state *state)
517 {
518 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
519 struct drm_dp_mst_topology_mgr *mst_mgr = &aconnector->mst_root->mst_mgr;
520 struct drm_dp_mst_port *mst_port = aconnector->mst_output_port;
521
522 return drm_dp_atomic_release_time_slots(state, mst_mgr, mst_port);
523 }
524
525 static const struct drm_connector_helper_funcs dm_dp_mst_connector_helper_funcs = {
526 .get_modes = dm_dp_mst_get_modes,
527 .mode_valid = amdgpu_dm_connector_mode_valid,
528 .atomic_best_encoder = dm_mst_atomic_best_encoder,
529 .detect_ctx = dm_dp_mst_detect,
530 .atomic_check = dm_dp_mst_atomic_check,
531 };
532
amdgpu_dm_encoder_destroy(struct drm_encoder * encoder)533 static void amdgpu_dm_encoder_destroy(struct drm_encoder *encoder)
534 {
535 drm_encoder_cleanup(encoder);
536 }
537
538 static const struct drm_encoder_funcs amdgpu_dm_encoder_funcs = {
539 .destroy = amdgpu_dm_encoder_destroy,
540 };
541
542 void
dm_dp_create_fake_mst_encoders(struct amdgpu_device * adev)543 dm_dp_create_fake_mst_encoders(struct amdgpu_device *adev)
544 {
545 struct drm_device *dev = adev_to_drm(adev);
546 int i;
547
548 for (i = 0; i < adev->dm.display_indexes_num; i++) {
549 struct amdgpu_encoder *amdgpu_encoder = &adev->dm.mst_encoders[i];
550 struct drm_encoder *encoder = &amdgpu_encoder->base;
551
552 encoder->possible_crtcs = amdgpu_dm_get_encoder_crtc_mask(adev);
553
554 drm_encoder_init(
555 dev,
556 &amdgpu_encoder->base,
557 &amdgpu_dm_encoder_funcs,
558 DRM_MODE_ENCODER_DPMST,
559 NULL);
560
561 drm_encoder_helper_add(encoder, &amdgpu_dm_encoder_helper_funcs);
562 }
563 }
564
565 static struct drm_connector *
dm_dp_add_mst_connector(struct drm_dp_mst_topology_mgr * mgr,struct drm_dp_mst_port * port,const char * pathprop)566 dm_dp_add_mst_connector(struct drm_dp_mst_topology_mgr *mgr,
567 struct drm_dp_mst_port *port,
568 const char *pathprop)
569 {
570 struct amdgpu_dm_connector *master = container_of(mgr, struct amdgpu_dm_connector, mst_mgr);
571 struct drm_device *dev = master->base.dev;
572 struct amdgpu_device *adev = drm_to_adev(dev);
573 struct amdgpu_dm_connector *aconnector;
574 struct drm_connector *connector;
575 int i;
576
577 aconnector = kzalloc(sizeof(*aconnector), GFP_KERNEL);
578 if (!aconnector)
579 return NULL;
580
581 DRM_DEBUG_DRIVER("%s: Create aconnector 0x%p for port 0x%p\n", __func__, aconnector, port);
582
583 connector = &aconnector->base;
584 aconnector->mst_output_port = port;
585 aconnector->mst_root = master;
586 amdgpu_dm_set_mst_status(&aconnector->mst_status,
587 MST_PROBE, true);
588
589 if (drm_connector_init(
590 dev,
591 connector,
592 &dm_dp_mst_connector_funcs,
593 DRM_MODE_CONNECTOR_DisplayPort)) {
594 kfree(aconnector);
595 return NULL;
596 }
597 drm_connector_helper_add(connector, &dm_dp_mst_connector_helper_funcs);
598
599 amdgpu_dm_connector_init_helper(
600 &adev->dm,
601 aconnector,
602 DRM_MODE_CONNECTOR_DisplayPort,
603 master->dc_link,
604 master->connector_id);
605
606 for (i = 0; i < adev->dm.display_indexes_num; i++) {
607 drm_connector_attach_encoder(&aconnector->base,
608 &adev->dm.mst_encoders[i].base);
609 }
610
611 connector->max_bpc_property = master->base.max_bpc_property;
612 if (connector->max_bpc_property)
613 drm_connector_attach_max_bpc_property(connector, 8, 16);
614
615 connector->vrr_capable_property = master->base.vrr_capable_property;
616 if (connector->vrr_capable_property)
617 drm_connector_attach_vrr_capable_property(connector);
618
619 drm_object_attach_property(
620 &connector->base,
621 dev->mode_config.path_property,
622 0);
623 drm_object_attach_property(
624 &connector->base,
625 dev->mode_config.tile_property,
626 0);
627 connector->colorspace_property = master->base.colorspace_property;
628 if (connector->colorspace_property)
629 drm_connector_attach_colorspace_property(connector);
630
631 drm_connector_set_path_property(connector, pathprop);
632
633 /*
634 * Initialize connector state before adding the connectror to drm and
635 * framebuffer lists
636 */
637 amdgpu_dm_connector_funcs_reset(connector);
638
639 drm_dp_mst_get_port_malloc(port);
640
641 return connector;
642 }
643
dm_handle_mst_sideband_msg_ready_event(struct drm_dp_mst_topology_mgr * mgr,enum mst_msg_ready_type msg_rdy_type)644 void dm_handle_mst_sideband_msg_ready_event(
645 struct drm_dp_mst_topology_mgr *mgr,
646 enum mst_msg_ready_type msg_rdy_type)
647 {
648 uint8_t esi[DP_PSR_ERROR_STATUS - DP_SINK_COUNT_ESI] = { 0 };
649 uint8_t dret;
650 bool new_irq_handled = false;
651 int dpcd_addr;
652 uint8_t dpcd_bytes_to_read;
653 const uint8_t max_process_count = 30;
654 uint8_t process_count = 0;
655 u8 retry;
656 struct amdgpu_dm_connector *aconnector =
657 container_of(mgr, struct amdgpu_dm_connector, mst_mgr);
658
659
660 const struct dc_link_status *link_status = dc_link_get_status(aconnector->dc_link);
661
662 if (link_status->dpcd_caps->dpcd_rev.raw < 0x12) {
663 dpcd_bytes_to_read = DP_LANE0_1_STATUS - DP_SINK_COUNT;
664 /* DPCD 0x200 - 0x201 for downstream IRQ */
665 dpcd_addr = DP_SINK_COUNT;
666 } else {
667 dpcd_bytes_to_read = DP_PSR_ERROR_STATUS - DP_SINK_COUNT_ESI;
668 /* DPCD 0x2002 - 0x2005 for downstream IRQ */
669 dpcd_addr = DP_SINK_COUNT_ESI;
670 }
671
672 mutex_lock(&aconnector->handle_mst_msg_ready);
673
674 while (process_count < max_process_count) {
675 u8 ack[DP_PSR_ERROR_STATUS - DP_SINK_COUNT_ESI] = {};
676
677 process_count++;
678
679 dret = drm_dp_dpcd_read(
680 &aconnector->dm_dp_aux.aux,
681 dpcd_addr,
682 esi,
683 dpcd_bytes_to_read);
684
685 if (dret != dpcd_bytes_to_read) {
686 DRM_DEBUG_KMS("DPCD read and acked number is not as expected!");
687 break;
688 }
689
690 DRM_DEBUG_DRIVER("ESI %02x %02x %02x\n", esi[0], esi[1], esi[2]);
691
692 switch (msg_rdy_type) {
693 case DOWN_REP_MSG_RDY_EVENT:
694 /* Only handle DOWN_REP_MSG_RDY case*/
695 esi[1] &= DP_DOWN_REP_MSG_RDY;
696 break;
697 case UP_REQ_MSG_RDY_EVENT:
698 /* Only handle UP_REQ_MSG_RDY case*/
699 esi[1] &= DP_UP_REQ_MSG_RDY;
700 break;
701 default:
702 /* Handle both cases*/
703 esi[1] &= (DP_DOWN_REP_MSG_RDY | DP_UP_REQ_MSG_RDY);
704 break;
705 }
706
707 if (!esi[1])
708 break;
709
710 /* handle MST irq */
711 if (aconnector->mst_mgr.mst_state)
712 drm_dp_mst_hpd_irq_handle_event(&aconnector->mst_mgr,
713 esi,
714 ack,
715 &new_irq_handled);
716
717 if (new_irq_handled) {
718 /* ACK at DPCD to notify down stream */
719 for (retry = 0; retry < 3; retry++) {
720 ssize_t wret;
721
722 wret = drm_dp_dpcd_writeb(&aconnector->dm_dp_aux.aux,
723 dpcd_addr + 1,
724 ack[1]);
725 if (wret == 1)
726 break;
727 }
728
729 if (retry == 3) {
730 DRM_ERROR("Failed to ack MST event.\n");
731 break;
732 }
733
734 drm_dp_mst_hpd_irq_send_new_request(&aconnector->mst_mgr);
735
736 new_irq_handled = false;
737 } else {
738 break;
739 }
740 }
741
742 mutex_unlock(&aconnector->handle_mst_msg_ready);
743
744 if (process_count == max_process_count)
745 DRM_DEBUG_DRIVER("Loop exceeded max iterations\n");
746 }
747
dm_handle_mst_down_rep_msg_ready(struct drm_dp_mst_topology_mgr * mgr)748 static void dm_handle_mst_down_rep_msg_ready(struct drm_dp_mst_topology_mgr *mgr)
749 {
750 dm_handle_mst_sideband_msg_ready_event(mgr, DOWN_REP_MSG_RDY_EVENT);
751 }
752
753 static const struct drm_dp_mst_topology_cbs dm_mst_cbs = {
754 .add_connector = dm_dp_add_mst_connector,
755 .poll_hpd_irq = dm_handle_mst_down_rep_msg_ready,
756 };
757
amdgpu_dm_initialize_dp_connector(struct amdgpu_display_manager * dm,struct amdgpu_dm_connector * aconnector,int link_index)758 void amdgpu_dm_initialize_dp_connector(struct amdgpu_display_manager *dm,
759 struct amdgpu_dm_connector *aconnector,
760 int link_index)
761 {
762 struct dc_link_settings max_link_enc_cap = {0};
763
764 aconnector->dm_dp_aux.aux.name =
765 kasprintf(GFP_KERNEL, "AMDGPU DM aux hw bus %d",
766 link_index);
767 aconnector->dm_dp_aux.aux.transfer = dm_dp_aux_transfer;
768 aconnector->dm_dp_aux.aux.drm_dev = dm->ddev;
769 aconnector->dm_dp_aux.ddc_service = aconnector->dc_link->ddc;
770
771 drm_dp_aux_init(&aconnector->dm_dp_aux.aux);
772 drm_dp_cec_register_connector(&aconnector->dm_dp_aux.aux,
773 &aconnector->base);
774
775 if (aconnector->base.connector_type == DRM_MODE_CONNECTOR_eDP)
776 return;
777
778 dc_link_dp_get_max_link_enc_cap(aconnector->dc_link, &max_link_enc_cap);
779 aconnector->mst_mgr.cbs = &dm_mst_cbs;
780 drm_dp_mst_topology_mgr_init(&aconnector->mst_mgr, adev_to_drm(dm->adev),
781 &aconnector->dm_dp_aux.aux, 16, 4, aconnector->connector_id);
782
783 drm_connector_attach_dp_subconnector_property(&aconnector->base);
784 }
785
dm_mst_get_pbn_divider(struct dc_link * link)786 int dm_mst_get_pbn_divider(struct dc_link *link)
787 {
788 if (!link)
789 return 0;
790
791 return dc_link_bandwidth_kbps(link,
792 dc_link_get_link_cap(link)) / (8 * 1000 * 54);
793 }
794
795 struct dsc_mst_fairness_params {
796 struct dc_crtc_timing *timing;
797 struct dc_sink *sink;
798 struct dc_dsc_bw_range bw_range;
799 bool compression_possible;
800 struct drm_dp_mst_port *port;
801 enum dsc_clock_force_state clock_force_enable;
802 uint32_t num_slices_h;
803 uint32_t num_slices_v;
804 uint32_t bpp_overwrite;
805 struct amdgpu_dm_connector *aconnector;
806 };
807
808 #if defined(CONFIG_DRM_AMD_DC_FP)
get_fec_overhead_multiplier(struct dc_link * dc_link)809 static uint16_t get_fec_overhead_multiplier(struct dc_link *dc_link)
810 {
811 u8 link_coding_cap;
812 uint16_t fec_overhead_multiplier_x1000 = PBN_FEC_OVERHEAD_MULTIPLIER_8B_10B;
813
814 link_coding_cap = dc_link_dp_mst_decide_link_encoding_format(dc_link);
815 if (link_coding_cap == DP_128b_132b_ENCODING)
816 fec_overhead_multiplier_x1000 = PBN_FEC_OVERHEAD_MULTIPLIER_128B_132B;
817
818 return fec_overhead_multiplier_x1000;
819 }
820
kbps_to_peak_pbn(int kbps,uint16_t fec_overhead_multiplier_x1000)821 static int kbps_to_peak_pbn(int kbps, uint16_t fec_overhead_multiplier_x1000)
822 {
823 u64 peak_kbps = kbps;
824
825 peak_kbps *= 1006;
826 peak_kbps *= fec_overhead_multiplier_x1000;
827 peak_kbps = div_u64(peak_kbps, 1000 * 1000);
828 return (int) DIV64_U64_ROUND_UP(peak_kbps * 64, (54 * 8 * 1000));
829 }
830
set_dsc_configs_from_fairness_vars(struct dsc_mst_fairness_params * params,struct dsc_mst_fairness_vars * vars,int count,int k)831 static void set_dsc_configs_from_fairness_vars(struct dsc_mst_fairness_params *params,
832 struct dsc_mst_fairness_vars *vars,
833 int count,
834 int k)
835 {
836 struct drm_connector *drm_connector;
837 int i;
838 struct dc_dsc_config_options dsc_options = {0};
839
840 for (i = 0; i < count; i++) {
841 drm_connector = ¶ms[i].aconnector->base;
842
843 dc_dsc_get_default_config_option(params[i].sink->ctx->dc, &dsc_options);
844 dsc_options.max_target_bpp_limit_override_x16 = drm_connector->display_info.max_dsc_bpp * 16;
845
846 memset(¶ms[i].timing->dsc_cfg, 0, sizeof(params[i].timing->dsc_cfg));
847 if (vars[i + k].dsc_enabled && dc_dsc_compute_config(
848 params[i].sink->ctx->dc->res_pool->dscs[0],
849 ¶ms[i].sink->dsc_caps.dsc_dec_caps,
850 &dsc_options,
851 0,
852 params[i].timing,
853 dc_link_get_highest_encoding_format(params[i].aconnector->dc_link),
854 ¶ms[i].timing->dsc_cfg)) {
855 params[i].timing->flags.DSC = 1;
856
857 if (params[i].bpp_overwrite)
858 params[i].timing->dsc_cfg.bits_per_pixel = params[i].bpp_overwrite;
859 else
860 params[i].timing->dsc_cfg.bits_per_pixel = vars[i + k].bpp_x16;
861
862 if (params[i].num_slices_h)
863 params[i].timing->dsc_cfg.num_slices_h = params[i].num_slices_h;
864
865 if (params[i].num_slices_v)
866 params[i].timing->dsc_cfg.num_slices_v = params[i].num_slices_v;
867 } else {
868 params[i].timing->flags.DSC = 0;
869 }
870 params[i].timing->dsc_cfg.mst_pbn = vars[i + k].pbn;
871 }
872
873 for (i = 0; i < count; i++) {
874 if (params[i].sink) {
875 if (params[i].sink->sink_signal != SIGNAL_TYPE_VIRTUAL &&
876 params[i].sink->sink_signal != SIGNAL_TYPE_NONE)
877 DRM_DEBUG_DRIVER("MST_DSC %s i=%d dispname=%s\n", __func__, i,
878 params[i].sink->edid_caps.display_name);
879 }
880
881 DRM_DEBUG_DRIVER("MST_DSC dsc=%d bits_per_pixel=%d pbn=%d\n",
882 params[i].timing->flags.DSC,
883 params[i].timing->dsc_cfg.bits_per_pixel,
884 vars[i + k].pbn);
885 }
886 }
887
bpp_x16_from_pbn(struct dsc_mst_fairness_params param,int pbn)888 static int bpp_x16_from_pbn(struct dsc_mst_fairness_params param, int pbn)
889 {
890 struct dc_dsc_config dsc_config;
891 u64 kbps;
892
893 struct drm_connector *drm_connector = ¶m.aconnector->base;
894 struct dc_dsc_config_options dsc_options = {0};
895
896 dc_dsc_get_default_config_option(param.sink->ctx->dc, &dsc_options);
897 dsc_options.max_target_bpp_limit_override_x16 = drm_connector->display_info.max_dsc_bpp * 16;
898
899 kbps = div_u64((u64)pbn * 994 * 8 * 54, 64);
900 dc_dsc_compute_config(
901 param.sink->ctx->dc->res_pool->dscs[0],
902 ¶m.sink->dsc_caps.dsc_dec_caps,
903 &dsc_options,
904 (int) kbps, param.timing,
905 dc_link_get_highest_encoding_format(param.aconnector->dc_link),
906 &dsc_config);
907
908 return dsc_config.bits_per_pixel;
909 }
910
increase_dsc_bpp(struct drm_atomic_state * state,struct drm_dp_mst_topology_state * mst_state,struct dc_link * dc_link,struct dsc_mst_fairness_params * params,struct dsc_mst_fairness_vars * vars,int count,int k)911 static int increase_dsc_bpp(struct drm_atomic_state *state,
912 struct drm_dp_mst_topology_state *mst_state,
913 struct dc_link *dc_link,
914 struct dsc_mst_fairness_params *params,
915 struct dsc_mst_fairness_vars *vars,
916 int count,
917 int k)
918 {
919 int i;
920 bool bpp_increased[MAX_PIPES];
921 int initial_slack[MAX_PIPES];
922 int min_initial_slack;
923 int next_index;
924 int remaining_to_increase = 0;
925 int link_timeslots_used;
926 int fair_pbn_alloc;
927 int ret = 0;
928 uint16_t fec_overhead_multiplier_x1000 = get_fec_overhead_multiplier(dc_link);
929
930 for (i = 0; i < count; i++) {
931 if (vars[i + k].dsc_enabled) {
932 initial_slack[i] =
933 kbps_to_peak_pbn(params[i].bw_range.max_kbps, fec_overhead_multiplier_x1000) - vars[i + k].pbn;
934 bpp_increased[i] = false;
935 remaining_to_increase += 1;
936 } else {
937 initial_slack[i] = 0;
938 bpp_increased[i] = true;
939 }
940 }
941
942 while (remaining_to_increase) {
943 next_index = -1;
944 min_initial_slack = -1;
945 for (i = 0; i < count; i++) {
946 if (!bpp_increased[i]) {
947 if (min_initial_slack == -1 || min_initial_slack > initial_slack[i]) {
948 min_initial_slack = initial_slack[i];
949 next_index = i;
950 }
951 }
952 }
953
954 if (next_index == -1)
955 break;
956
957 link_timeslots_used = 0;
958
959 for (i = 0; i < count; i++)
960 link_timeslots_used += DIV_ROUND_UP(vars[i + k].pbn, dfixed_trunc(mst_state->pbn_div));
961
962 fair_pbn_alloc =
963 (63 - link_timeslots_used) / remaining_to_increase * dfixed_trunc(mst_state->pbn_div);
964
965 if (initial_slack[next_index] > fair_pbn_alloc) {
966 vars[next_index].pbn += fair_pbn_alloc;
967 ret = drm_dp_atomic_find_time_slots(state,
968 params[next_index].port->mgr,
969 params[next_index].port,
970 vars[next_index].pbn);
971 if (ret < 0)
972 return ret;
973
974 ret = drm_dp_mst_atomic_check(state);
975 if (ret == 0) {
976 vars[next_index].bpp_x16 = bpp_x16_from_pbn(params[next_index], vars[next_index].pbn);
977 } else {
978 vars[next_index].pbn -= fair_pbn_alloc;
979 ret = drm_dp_atomic_find_time_slots(state,
980 params[next_index].port->mgr,
981 params[next_index].port,
982 vars[next_index].pbn);
983 if (ret < 0)
984 return ret;
985 }
986 } else {
987 vars[next_index].pbn += initial_slack[next_index];
988 ret = drm_dp_atomic_find_time_slots(state,
989 params[next_index].port->mgr,
990 params[next_index].port,
991 vars[next_index].pbn);
992 if (ret < 0)
993 return ret;
994
995 ret = drm_dp_mst_atomic_check(state);
996 if (ret == 0) {
997 vars[next_index].bpp_x16 = params[next_index].bw_range.max_target_bpp_x16;
998 } else {
999 vars[next_index].pbn -= initial_slack[next_index];
1000 ret = drm_dp_atomic_find_time_slots(state,
1001 params[next_index].port->mgr,
1002 params[next_index].port,
1003 vars[next_index].pbn);
1004 if (ret < 0)
1005 return ret;
1006 }
1007 }
1008
1009 bpp_increased[next_index] = true;
1010 remaining_to_increase--;
1011 }
1012 return 0;
1013 }
1014
try_disable_dsc(struct drm_atomic_state * state,struct dc_link * dc_link,struct dsc_mst_fairness_params * params,struct dsc_mst_fairness_vars * vars,int count,int k)1015 static int try_disable_dsc(struct drm_atomic_state *state,
1016 struct dc_link *dc_link,
1017 struct dsc_mst_fairness_params *params,
1018 struct dsc_mst_fairness_vars *vars,
1019 int count,
1020 int k)
1021 {
1022 int i;
1023 bool tried[MAX_PIPES];
1024 int kbps_increase[MAX_PIPES];
1025 int max_kbps_increase;
1026 int next_index;
1027 int remaining_to_try = 0;
1028 int ret;
1029 uint16_t fec_overhead_multiplier_x1000 = get_fec_overhead_multiplier(dc_link);
1030 int var_pbn;
1031
1032 for (i = 0; i < count; i++) {
1033 if (vars[i + k].dsc_enabled
1034 && vars[i + k].bpp_x16 == params[i].bw_range.max_target_bpp_x16
1035 && params[i].clock_force_enable == DSC_CLK_FORCE_DEFAULT) {
1036 kbps_increase[i] = params[i].bw_range.stream_kbps - params[i].bw_range.max_kbps;
1037 tried[i] = false;
1038 remaining_to_try += 1;
1039 } else {
1040 kbps_increase[i] = 0;
1041 tried[i] = true;
1042 }
1043 }
1044
1045 while (remaining_to_try) {
1046 next_index = -1;
1047 max_kbps_increase = -1;
1048 for (i = 0; i < count; i++) {
1049 if (!tried[i]) {
1050 if (max_kbps_increase == -1 || max_kbps_increase < kbps_increase[i]) {
1051 max_kbps_increase = kbps_increase[i];
1052 next_index = i;
1053 }
1054 }
1055 }
1056
1057 if (next_index == -1)
1058 break;
1059
1060 DRM_DEBUG_DRIVER("MST_DSC index #%d, try no compression\n", next_index);
1061 var_pbn = vars[next_index].pbn;
1062 vars[next_index].pbn = kbps_to_peak_pbn(params[next_index].bw_range.stream_kbps, fec_overhead_multiplier_x1000);
1063 ret = drm_dp_atomic_find_time_slots(state,
1064 params[next_index].port->mgr,
1065 params[next_index].port,
1066 vars[next_index].pbn);
1067 if (ret < 0) {
1068 DRM_DEBUG_DRIVER("%s:%d MST_DSC index #%d, failed to set pbn to the state, %d\n",
1069 __func__, __LINE__, next_index, ret);
1070 vars[next_index].pbn = var_pbn;
1071 return ret;
1072 }
1073
1074 ret = drm_dp_mst_atomic_check(state);
1075 if (ret == 0) {
1076 DRM_DEBUG_DRIVER("MST_DSC index #%d, greedily disable dsc\n", next_index);
1077 vars[next_index].dsc_enabled = false;
1078 vars[next_index].bpp_x16 = 0;
1079 } else {
1080 DRM_DEBUG_DRIVER("MST_DSC index #%d, restore optimized pbn value\n", next_index);
1081 vars[next_index].pbn = var_pbn;
1082 ret = drm_dp_atomic_find_time_slots(state,
1083 params[next_index].port->mgr,
1084 params[next_index].port,
1085 vars[next_index].pbn);
1086 if (ret < 0) {
1087 DRM_DEBUG_DRIVER("%s:%d MST_DSC index #%d, failed to set pbn to the state, %d\n",
1088 __func__, __LINE__, next_index, ret);
1089 return ret;
1090 }
1091 }
1092
1093 tried[next_index] = true;
1094 remaining_to_try--;
1095 }
1096 return 0;
1097 }
1098
log_dsc_params(int count,struct dsc_mst_fairness_vars * vars,int k)1099 static void log_dsc_params(int count, struct dsc_mst_fairness_vars *vars, int k)
1100 {
1101 int i;
1102
1103 for (i = 0; i < count; i++)
1104 DRM_DEBUG_DRIVER("MST_DSC DSC params: stream #%d --- dsc_enabled = %d, bpp_x16 = %d, pbn = %d\n",
1105 i, vars[i + k].dsc_enabled, vars[i + k].bpp_x16, vars[i + k].pbn);
1106 }
1107
compute_mst_dsc_configs_for_link(struct drm_atomic_state * state,struct dc_state * dc_state,struct dc_link * dc_link,struct dsc_mst_fairness_vars * vars,struct drm_dp_mst_topology_mgr * mgr,int * link_vars_start_index)1108 static int compute_mst_dsc_configs_for_link(struct drm_atomic_state *state,
1109 struct dc_state *dc_state,
1110 struct dc_link *dc_link,
1111 struct dsc_mst_fairness_vars *vars,
1112 struct drm_dp_mst_topology_mgr *mgr,
1113 int *link_vars_start_index)
1114 {
1115 struct dc_stream_state *stream;
1116 struct dsc_mst_fairness_params params[MAX_PIPES];
1117 struct amdgpu_dm_connector *aconnector;
1118 struct drm_dp_mst_topology_state *mst_state = drm_atomic_get_mst_topology_state(state, mgr);
1119 int count = 0;
1120 int i, k, ret;
1121 bool debugfs_overwrite = false;
1122 uint16_t fec_overhead_multiplier_x1000 = get_fec_overhead_multiplier(dc_link);
1123
1124 memset(params, 0, sizeof(params));
1125
1126 if (IS_ERR(mst_state))
1127 return PTR_ERR(mst_state);
1128
1129 /* Set up params */
1130 DRM_DEBUG_DRIVER("%s: MST_DSC Set up params for %d streams\n", __func__, dc_state->stream_count);
1131 for (i = 0; i < dc_state->stream_count; i++) {
1132 struct dc_dsc_policy dsc_policy = {0};
1133
1134 stream = dc_state->streams[i];
1135
1136 if (stream->link != dc_link)
1137 continue;
1138
1139 aconnector = (struct amdgpu_dm_connector *)stream->dm_stream_context;
1140 if (!aconnector)
1141 continue;
1142
1143 if (!aconnector->mst_output_port)
1144 continue;
1145
1146 stream->timing.flags.DSC = 0;
1147
1148 params[count].timing = &stream->timing;
1149 params[count].sink = stream->sink;
1150 params[count].aconnector = aconnector;
1151 params[count].port = aconnector->mst_output_port;
1152 params[count].clock_force_enable = aconnector->dsc_settings.dsc_force_enable;
1153 if (params[count].clock_force_enable == DSC_CLK_FORCE_ENABLE)
1154 debugfs_overwrite = true;
1155 params[count].num_slices_h = aconnector->dsc_settings.dsc_num_slices_h;
1156 params[count].num_slices_v = aconnector->dsc_settings.dsc_num_slices_v;
1157 params[count].bpp_overwrite = aconnector->dsc_settings.dsc_bits_per_pixel;
1158 params[count].compression_possible = stream->sink->dsc_caps.dsc_dec_caps.is_dsc_supported;
1159 dc_dsc_get_policy_for_timing(params[count].timing, 0, &dsc_policy, dc_link_get_highest_encoding_format(stream->link));
1160 if (!dc_dsc_compute_bandwidth_range(
1161 stream->sink->ctx->dc->res_pool->dscs[0],
1162 stream->sink->ctx->dc->debug.dsc_min_slice_height_override,
1163 dsc_policy.min_target_bpp * 16,
1164 dsc_policy.max_target_bpp * 16,
1165 &stream->sink->dsc_caps.dsc_dec_caps,
1166 &stream->timing,
1167 dc_link_get_highest_encoding_format(dc_link),
1168 ¶ms[count].bw_range))
1169 params[count].bw_range.stream_kbps = dc_bandwidth_in_kbps_from_timing(&stream->timing,
1170 dc_link_get_highest_encoding_format(dc_link));
1171
1172 DRM_DEBUG_DRIVER("MST_DSC #%d stream 0x%p - max_kbps = %u, min_kbps = %u, uncompressed_kbps = %u\n",
1173 count, stream, params[count].bw_range.max_kbps, params[count].bw_range.min_kbps,
1174 params[count].bw_range.stream_kbps);
1175 count++;
1176 }
1177
1178 if (count == 0) {
1179 ASSERT(0);
1180 return 0;
1181 }
1182
1183 /* k is start index of vars for current phy link used by mst hub */
1184 k = *link_vars_start_index;
1185 /* set vars start index for next mst hub phy link */
1186 *link_vars_start_index += count;
1187
1188 /* Try no compression */
1189 DRM_DEBUG_DRIVER("MST_DSC Try no compression\n");
1190 for (i = 0; i < count; i++) {
1191 vars[i + k].aconnector = params[i].aconnector;
1192 vars[i + k].pbn = kbps_to_peak_pbn(params[i].bw_range.stream_kbps, fec_overhead_multiplier_x1000);
1193 vars[i + k].dsc_enabled = false;
1194 vars[i + k].bpp_x16 = 0;
1195 ret = drm_dp_atomic_find_time_slots(state, params[i].port->mgr, params[i].port,
1196 vars[i + k].pbn);
1197 if (ret < 0)
1198 return ret;
1199 }
1200 ret = drm_dp_mst_atomic_check(state);
1201 if (ret == 0 && !debugfs_overwrite) {
1202 set_dsc_configs_from_fairness_vars(params, vars, count, k);
1203 return 0;
1204 } else if (ret != -ENOSPC) {
1205 return ret;
1206 }
1207
1208 log_dsc_params(count, vars, k);
1209
1210 /* Try max compression */
1211 DRM_DEBUG_DRIVER("MST_DSC Try max compression\n");
1212 for (i = 0; i < count; i++) {
1213 if (params[i].compression_possible && params[i].clock_force_enable != DSC_CLK_FORCE_DISABLE) {
1214 vars[i + k].pbn = kbps_to_peak_pbn(params[i].bw_range.min_kbps, fec_overhead_multiplier_x1000);
1215 vars[i + k].dsc_enabled = true;
1216 vars[i + k].bpp_x16 = params[i].bw_range.min_target_bpp_x16;
1217 ret = drm_dp_atomic_find_time_slots(state, params[i].port->mgr,
1218 params[i].port, vars[i + k].pbn);
1219 if (ret < 0)
1220 return ret;
1221 } else {
1222 vars[i + k].pbn = kbps_to_peak_pbn(params[i].bw_range.stream_kbps, fec_overhead_multiplier_x1000);
1223 vars[i + k].dsc_enabled = false;
1224 vars[i + k].bpp_x16 = 0;
1225 ret = drm_dp_atomic_find_time_slots(state, params[i].port->mgr,
1226 params[i].port, vars[i + k].pbn);
1227 if (ret < 0)
1228 return ret;
1229 }
1230 }
1231 ret = drm_dp_mst_atomic_check(state);
1232 if (ret != 0)
1233 return ret;
1234
1235 log_dsc_params(count, vars, k);
1236
1237 /* Optimize degree of compression */
1238 DRM_DEBUG_DRIVER("MST_DSC Try optimize compression\n");
1239 ret = increase_dsc_bpp(state, mst_state, dc_link, params, vars, count, k);
1240 if (ret < 0) {
1241 DRM_DEBUG_DRIVER("MST_DSC Failed to optimize compression\n");
1242 return ret;
1243 }
1244
1245 log_dsc_params(count, vars, k);
1246
1247 DRM_DEBUG_DRIVER("MST_DSC Try disable compression\n");
1248 ret = try_disable_dsc(state, dc_link, params, vars, count, k);
1249 if (ret < 0) {
1250 DRM_DEBUG_DRIVER("MST_DSC Failed to disable compression\n");
1251 return ret;
1252 }
1253
1254 log_dsc_params(count, vars, k);
1255
1256 set_dsc_configs_from_fairness_vars(params, vars, count, k);
1257
1258 return 0;
1259 }
1260
is_dsc_need_re_compute(struct drm_atomic_state * state,struct dc_state * dc_state,struct dc_link * dc_link)1261 static bool is_dsc_need_re_compute(
1262 struct drm_atomic_state *state,
1263 struct dc_state *dc_state,
1264 struct dc_link *dc_link)
1265 {
1266 int i, j;
1267 bool is_dsc_need_re_compute = false;
1268 struct amdgpu_dm_connector *stream_on_link[MAX_PIPES];
1269 int new_stream_on_link_num = 0;
1270 struct amdgpu_dm_connector *aconnector;
1271 struct dc_stream_state *stream;
1272 const struct dc *dc = dc_link->dc;
1273
1274 /* only check phy used by dsc mst branch */
1275 if (dc_link->type != dc_connection_mst_branch)
1276 goto out;
1277
1278 /* add a check for older MST DSC with no virtual DPCDs */
1279 if (needs_dsc_aux_workaround(dc_link) &&
1280 (!(dc_link->dpcd_caps.dsc_caps.dsc_basic_caps.fields.dsc_support.DSC_SUPPORT ||
1281 dc_link->dpcd_caps.dsc_caps.dsc_basic_caps.fields.dsc_support.DSC_PASSTHROUGH_SUPPORT)))
1282 goto out;
1283
1284 for (i = 0; i < MAX_PIPES; i++)
1285 stream_on_link[i] = NULL;
1286
1287 DRM_DEBUG_DRIVER("%s: MST_DSC check on %d streams in new dc_state\n", __func__, dc_state->stream_count);
1288
1289 /* check if there is mode change in new request */
1290 for (i = 0; i < dc_state->stream_count; i++) {
1291 struct drm_crtc_state *new_crtc_state;
1292 struct drm_connector_state *new_conn_state;
1293
1294 stream = dc_state->streams[i];
1295 if (!stream)
1296 continue;
1297
1298 DRM_DEBUG_DRIVER("%s:%d MST_DSC checking #%d stream 0x%p\n", __func__, __LINE__, i, stream);
1299
1300 /* check if stream using the same link for mst */
1301 if (stream->link != dc_link)
1302 continue;
1303
1304 aconnector = (struct amdgpu_dm_connector *) stream->dm_stream_context;
1305 if (!aconnector || !aconnector->dsc_aux)
1306 continue;
1307
1308 stream_on_link[new_stream_on_link_num] = aconnector;
1309 new_stream_on_link_num++;
1310
1311 new_conn_state = drm_atomic_get_new_connector_state(state, &aconnector->base);
1312 if (!new_conn_state) {
1313 DRM_DEBUG_DRIVER("%s:%d MST_DSC no new_conn_state for stream 0x%p, aconnector 0x%p\n",
1314 __func__, __LINE__, stream, aconnector);
1315 continue;
1316 }
1317
1318 if (IS_ERR(new_conn_state))
1319 continue;
1320
1321 if (!new_conn_state->crtc)
1322 continue;
1323
1324 new_crtc_state = drm_atomic_get_new_crtc_state(state, new_conn_state->crtc);
1325 if (!new_crtc_state) {
1326 DRM_DEBUG_DRIVER("%s:%d MST_DSC no new_crtc_state for crtc of stream 0x%p, aconnector 0x%p\n",
1327 __func__, __LINE__, stream, aconnector);
1328 continue;
1329 }
1330
1331 if (IS_ERR(new_crtc_state))
1332 continue;
1333
1334 if (new_crtc_state->enable && new_crtc_state->active) {
1335 if (new_crtc_state->mode_changed || new_crtc_state->active_changed ||
1336 new_crtc_state->connectors_changed) {
1337 DRM_DEBUG_DRIVER("%s:%d MST_DSC dsc recompute required."
1338 "stream 0x%p in new dc_state\n",
1339 __func__, __LINE__, stream);
1340 is_dsc_need_re_compute = true;
1341 goto out;
1342 }
1343 }
1344 }
1345
1346 if (new_stream_on_link_num == 0) {
1347 DRM_DEBUG_DRIVER("%s:%d MST_DSC no mode change request for streams in new dc_state\n",
1348 __func__, __LINE__);
1349 is_dsc_need_re_compute = false;
1350 goto out;
1351 }
1352
1353 DRM_DEBUG_DRIVER("%s: MST_DSC check on %d streams in current dc_state\n",
1354 __func__, dc->current_state->stream_count);
1355
1356 /* check current_state if there stream on link but it is not in
1357 * new request state
1358 */
1359 for (i = 0; i < dc->current_state->stream_count; i++) {
1360 stream = dc->current_state->streams[i];
1361 /* only check stream on the mst hub */
1362 if (stream->link != dc_link)
1363 continue;
1364
1365 aconnector = (struct amdgpu_dm_connector *)stream->dm_stream_context;
1366 if (!aconnector)
1367 continue;
1368
1369 for (j = 0; j < new_stream_on_link_num; j++) {
1370 if (stream_on_link[j]) {
1371 if (aconnector == stream_on_link[j])
1372 break;
1373 }
1374 }
1375
1376 if (j == new_stream_on_link_num) {
1377 /* not in new state */
1378 DRM_DEBUG_DRIVER("%s:%d MST_DSC dsc recompute required."
1379 "stream 0x%p in current dc_state but not in new dc_state\n",
1380 __func__, __LINE__, stream);
1381 is_dsc_need_re_compute = true;
1382 break;
1383 }
1384 }
1385
1386 out:
1387 DRM_DEBUG_DRIVER("%s: MST_DSC dsc recompute %s\n",
1388 __func__, is_dsc_need_re_compute ? "required" : "not required");
1389
1390 return is_dsc_need_re_compute;
1391 }
1392
compute_mst_dsc_configs_for_state(struct drm_atomic_state * state,struct dc_state * dc_state,struct dsc_mst_fairness_vars * vars)1393 int compute_mst_dsc_configs_for_state(struct drm_atomic_state *state,
1394 struct dc_state *dc_state,
1395 struct dsc_mst_fairness_vars *vars)
1396 {
1397 int i, j;
1398 struct dc_stream_state *stream;
1399 bool computed_streams[MAX_PIPES];
1400 struct amdgpu_dm_connector *aconnector;
1401 struct drm_dp_mst_topology_mgr *mst_mgr;
1402 struct resource_pool *res_pool;
1403 int link_vars_start_index = 0;
1404 int ret = 0;
1405
1406 for (i = 0; i < dc_state->stream_count; i++)
1407 computed_streams[i] = false;
1408
1409 for (i = 0; i < dc_state->stream_count; i++) {
1410 stream = dc_state->streams[i];
1411 res_pool = stream->ctx->dc->res_pool;
1412
1413 if (stream->signal != SIGNAL_TYPE_DISPLAY_PORT_MST)
1414 continue;
1415
1416 aconnector = (struct amdgpu_dm_connector *)stream->dm_stream_context;
1417
1418 DRM_DEBUG_DRIVER("%s: MST_DSC compute mst dsc configs for stream 0x%p, aconnector 0x%p\n",
1419 __func__, stream, aconnector);
1420
1421 if (!aconnector || !aconnector->dc_sink || !aconnector->mst_output_port)
1422 continue;
1423
1424 if (!aconnector->dc_sink->dsc_caps.dsc_dec_caps.is_dsc_supported)
1425 continue;
1426
1427 if (computed_streams[i])
1428 continue;
1429
1430 if (res_pool->funcs->remove_stream_from_ctx &&
1431 res_pool->funcs->remove_stream_from_ctx(stream->ctx->dc, dc_state, stream) != DC_OK)
1432 return -EINVAL;
1433
1434 if (!is_dsc_need_re_compute(state, dc_state, stream->link))
1435 continue;
1436
1437 mst_mgr = aconnector->mst_output_port->mgr;
1438 ret = compute_mst_dsc_configs_for_link(state, dc_state, stream->link, vars, mst_mgr,
1439 &link_vars_start_index);
1440 if (ret != 0)
1441 return ret;
1442
1443 for (j = 0; j < dc_state->stream_count; j++) {
1444 if (dc_state->streams[j]->link == stream->link)
1445 computed_streams[j] = true;
1446 }
1447 }
1448
1449 for (i = 0; i < dc_state->stream_count; i++) {
1450 stream = dc_state->streams[i];
1451
1452 if (stream->timing.flags.DSC == 1)
1453 if (dc_stream_add_dsc_to_resource(stream->ctx->dc, dc_state, stream) != DC_OK) {
1454 DRM_DEBUG_DRIVER("%s:%d MST_DSC Failed to request dsc hw resource for stream 0x%p\n",
1455 __func__, __LINE__, stream);
1456 return -EINVAL;
1457 }
1458 }
1459
1460 return ret;
1461 }
1462
pre_compute_mst_dsc_configs_for_state(struct drm_atomic_state * state,struct dc_state * dc_state,struct dsc_mst_fairness_vars * vars)1463 static int pre_compute_mst_dsc_configs_for_state(struct drm_atomic_state *state,
1464 struct dc_state *dc_state,
1465 struct dsc_mst_fairness_vars *vars)
1466 {
1467 int i, j;
1468 struct dc_stream_state *stream;
1469 bool computed_streams[MAX_PIPES];
1470 struct amdgpu_dm_connector *aconnector;
1471 struct drm_dp_mst_topology_mgr *mst_mgr;
1472 int link_vars_start_index = 0;
1473 int ret = 0;
1474
1475 for (i = 0; i < dc_state->stream_count; i++)
1476 computed_streams[i] = false;
1477
1478 for (i = 0; i < dc_state->stream_count; i++) {
1479 stream = dc_state->streams[i];
1480
1481 if (stream->signal != SIGNAL_TYPE_DISPLAY_PORT_MST)
1482 continue;
1483
1484 aconnector = (struct amdgpu_dm_connector *)stream->dm_stream_context;
1485
1486 DRM_DEBUG_DRIVER("MST_DSC pre compute mst dsc configs for #%d stream 0x%p, aconnector 0x%p\n",
1487 i, stream, aconnector);
1488
1489 if (!aconnector || !aconnector->dc_sink || !aconnector->mst_output_port)
1490 continue;
1491
1492 if (!aconnector->dc_sink->dsc_caps.dsc_dec_caps.is_dsc_supported)
1493 continue;
1494
1495 if (computed_streams[i])
1496 continue;
1497
1498 if (!is_dsc_need_re_compute(state, dc_state, stream->link))
1499 continue;
1500
1501 mst_mgr = aconnector->mst_output_port->mgr;
1502 ret = compute_mst_dsc_configs_for_link(state, dc_state, stream->link, vars, mst_mgr,
1503 &link_vars_start_index);
1504 if (ret != 0)
1505 return ret;
1506
1507 for (j = 0; j < dc_state->stream_count; j++) {
1508 if (dc_state->streams[j]->link == stream->link)
1509 computed_streams[j] = true;
1510 }
1511 }
1512
1513 return ret;
1514 }
1515
find_crtc_index_in_state_by_stream(struct drm_atomic_state * state,struct dc_stream_state * stream)1516 static int find_crtc_index_in_state_by_stream(struct drm_atomic_state *state,
1517 struct dc_stream_state *stream)
1518 {
1519 int i;
1520 struct drm_crtc *crtc;
1521 struct drm_crtc_state *new_state, *old_state;
1522
1523 for_each_oldnew_crtc_in_state(state, crtc, old_state, new_state, i) {
1524 struct dm_crtc_state *dm_state = to_dm_crtc_state(new_state);
1525
1526 if (dm_state->stream == stream)
1527 return i;
1528 }
1529 return -1;
1530 }
1531
is_link_to_dschub(struct dc_link * dc_link)1532 static bool is_link_to_dschub(struct dc_link *dc_link)
1533 {
1534 union dpcd_dsc_basic_capabilities *dsc_caps =
1535 &dc_link->dpcd_caps.dsc_caps.dsc_basic_caps;
1536
1537 /* only check phy used by dsc mst branch */
1538 if (dc_link->type != dc_connection_mst_branch)
1539 return false;
1540
1541 if (!(dsc_caps->fields.dsc_support.DSC_SUPPORT ||
1542 dsc_caps->fields.dsc_support.DSC_PASSTHROUGH_SUPPORT))
1543 return false;
1544 return true;
1545 }
1546
is_dsc_precompute_needed(struct drm_atomic_state * state)1547 static bool is_dsc_precompute_needed(struct drm_atomic_state *state)
1548 {
1549 int i;
1550 struct drm_crtc *crtc;
1551 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
1552 bool ret = false;
1553
1554 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
1555 struct dm_crtc_state *dm_crtc_state = to_dm_crtc_state(new_crtc_state);
1556
1557 if (!amdgpu_dm_find_first_crtc_matching_connector(state, crtc)) {
1558 ret = false;
1559 break;
1560 }
1561 if (dm_crtc_state->stream && dm_crtc_state->stream->link)
1562 if (is_link_to_dschub(dm_crtc_state->stream->link))
1563 ret = true;
1564 }
1565 return ret;
1566 }
1567
pre_validate_dsc(struct drm_atomic_state * state,struct dm_atomic_state ** dm_state_ptr,struct dsc_mst_fairness_vars * vars)1568 int pre_validate_dsc(struct drm_atomic_state *state,
1569 struct dm_atomic_state **dm_state_ptr,
1570 struct dsc_mst_fairness_vars *vars)
1571 {
1572 int i;
1573 struct dm_atomic_state *dm_state;
1574 struct dc_state *local_dc_state = NULL;
1575 int ret = 0;
1576
1577 if (!is_dsc_precompute_needed(state)) {
1578 DRM_INFO_ONCE("%s:%d MST_DSC dsc precompute is not needed\n", __func__, __LINE__);
1579 return 0;
1580 }
1581 ret = dm_atomic_get_state(state, dm_state_ptr);
1582 if (ret != 0) {
1583 DRM_INFO_ONCE("%s:%d MST_DSC dm_atomic_get_state() failed\n", __func__, __LINE__);
1584 return ret;
1585 }
1586 dm_state = *dm_state_ptr;
1587
1588 /*
1589 * create local vailable for dc_state. copy content of streams of dm_state->context
1590 * to local variable. make sure stream pointer of local variable not the same as stream
1591 * from dm_state->context.
1592 */
1593
1594 local_dc_state = vmalloc(sizeof(struct dc_state));
1595 if (!local_dc_state)
1596 return -ENOMEM;
1597 memcpy(local_dc_state, dm_state->context, sizeof(struct dc_state));
1598
1599 for (i = 0; i < local_dc_state->stream_count; i++) {
1600 struct dc_stream_state *stream = dm_state->context->streams[i];
1601 int ind = find_crtc_index_in_state_by_stream(state, stream);
1602
1603 if (ind >= 0) {
1604 struct drm_connector *connector;
1605 struct amdgpu_dm_connector *aconnector;
1606 struct drm_connector_state *drm_new_conn_state;
1607 struct dm_connector_state *dm_new_conn_state;
1608 struct dm_crtc_state *dm_old_crtc_state;
1609
1610 connector =
1611 amdgpu_dm_find_first_crtc_matching_connector(state,
1612 state->crtcs[ind].ptr);
1613 aconnector = to_amdgpu_dm_connector(connector);
1614 drm_new_conn_state =
1615 drm_atomic_get_new_connector_state(state,
1616 &aconnector->base);
1617 dm_new_conn_state = to_dm_connector_state(drm_new_conn_state);
1618 dm_old_crtc_state = to_dm_crtc_state(state->crtcs[ind].old_state);
1619
1620 local_dc_state->streams[i] =
1621 create_validate_stream_for_sink(aconnector,
1622 &state->crtcs[ind].new_state->mode,
1623 dm_new_conn_state,
1624 dm_old_crtc_state->stream);
1625 if (local_dc_state->streams[i] == NULL) {
1626 ret = -EINVAL;
1627 break;
1628 }
1629 }
1630 }
1631
1632 if (ret != 0)
1633 goto clean_exit;
1634
1635 ret = pre_compute_mst_dsc_configs_for_state(state, local_dc_state, vars);
1636 if (ret != 0) {
1637 DRM_INFO_ONCE("%s:%d MST_DSC dsc pre_compute_mst_dsc_configs_for_state() failed\n",
1638 __func__, __LINE__);
1639 ret = -EINVAL;
1640 goto clean_exit;
1641 }
1642
1643 /*
1644 * compare local_streams -> timing with dm_state->context,
1645 * if the same set crtc_state->mode-change = 0;
1646 */
1647 for (i = 0; i < local_dc_state->stream_count; i++) {
1648 struct dc_stream_state *stream = dm_state->context->streams[i];
1649
1650 if (local_dc_state->streams[i] &&
1651 dc_is_timing_changed(stream, local_dc_state->streams[i])) {
1652 DRM_INFO_ONCE("%s:%d MST_DSC crtc[%d] needs mode_change\n", __func__, __LINE__, i);
1653 } else {
1654 int ind = find_crtc_index_in_state_by_stream(state, stream);
1655
1656 if (ind >= 0) {
1657 DRM_INFO_ONCE("%s:%d MST_DSC no mode changed for stream 0x%p\n",
1658 __func__, __LINE__, stream);
1659 state->crtcs[ind].new_state->mode_changed = 0;
1660 }
1661 }
1662 }
1663 clean_exit:
1664 for (i = 0; i < local_dc_state->stream_count; i++) {
1665 struct dc_stream_state *stream = dm_state->context->streams[i];
1666
1667 if (local_dc_state->streams[i] != stream)
1668 dc_stream_release(local_dc_state->streams[i]);
1669 }
1670
1671 vfree(local_dc_state);
1672
1673 return ret;
1674 }
1675
kbps_from_pbn(unsigned int pbn)1676 static unsigned int kbps_from_pbn(unsigned int pbn)
1677 {
1678 unsigned int kbps = pbn;
1679
1680 kbps *= (1000000 / PEAK_FACTOR_X1000);
1681 kbps *= 8;
1682 kbps *= 54;
1683 kbps /= 64;
1684
1685 return kbps;
1686 }
1687
is_dsc_common_config_possible(struct dc_stream_state * stream,struct dc_dsc_bw_range * bw_range)1688 static bool is_dsc_common_config_possible(struct dc_stream_state *stream,
1689 struct dc_dsc_bw_range *bw_range)
1690 {
1691 struct dc_dsc_policy dsc_policy = {0};
1692
1693 dc_dsc_get_policy_for_timing(&stream->timing, 0, &dsc_policy, dc_link_get_highest_encoding_format(stream->link));
1694 dc_dsc_compute_bandwidth_range(stream->sink->ctx->dc->res_pool->dscs[0],
1695 stream->sink->ctx->dc->debug.dsc_min_slice_height_override,
1696 dsc_policy.min_target_bpp * 16,
1697 dsc_policy.max_target_bpp * 16,
1698 &stream->sink->dsc_caps.dsc_dec_caps,
1699 &stream->timing, dc_link_get_highest_encoding_format(stream->link), bw_range);
1700
1701 return bw_range->max_target_bpp_x16 && bw_range->min_target_bpp_x16;
1702 }
1703 #endif
1704
1705 #if defined(CONFIG_DRM_AMD_DC_FP)
dp_get_link_current_set_bw(struct drm_dp_aux * aux,uint32_t * cur_link_bw)1706 static bool dp_get_link_current_set_bw(struct drm_dp_aux *aux, uint32_t *cur_link_bw)
1707 {
1708 uint32_t total_data_bw_efficiency_x10000 = 0;
1709 uint32_t link_rate_per_lane_kbps = 0;
1710 enum dc_link_rate link_rate;
1711 union lane_count_set lane_count;
1712 u8 dp_link_encoding;
1713 u8 link_bw_set = 0;
1714
1715 *cur_link_bw = 0;
1716
1717 if (drm_dp_dpcd_read(aux, DP_MAIN_LINK_CHANNEL_CODING_SET, &dp_link_encoding, 1) != 1 ||
1718 drm_dp_dpcd_read(aux, DP_LANE_COUNT_SET, &lane_count.raw, 1) != 1 ||
1719 drm_dp_dpcd_read(aux, DP_LINK_BW_SET, &link_bw_set, 1) != 1)
1720 return false;
1721
1722 switch (dp_link_encoding) {
1723 case DP_8b_10b_ENCODING:
1724 link_rate = link_bw_set;
1725 link_rate_per_lane_kbps = link_rate * LINK_RATE_REF_FREQ_IN_KHZ * BITS_PER_DP_BYTE;
1726 total_data_bw_efficiency_x10000 = DATA_EFFICIENCY_8b_10b_x10000;
1727 total_data_bw_efficiency_x10000 /= 100;
1728 total_data_bw_efficiency_x10000 *= DATA_EFFICIENCY_8b_10b_FEC_EFFICIENCY_x100;
1729 break;
1730 case DP_128b_132b_ENCODING:
1731 switch (link_bw_set) {
1732 case DP_LINK_BW_10:
1733 link_rate = LINK_RATE_UHBR10;
1734 break;
1735 case DP_LINK_BW_13_5:
1736 link_rate = LINK_RATE_UHBR13_5;
1737 break;
1738 case DP_LINK_BW_20:
1739 link_rate = LINK_RATE_UHBR20;
1740 break;
1741 default:
1742 return false;
1743 }
1744
1745 link_rate_per_lane_kbps = link_rate * 10000;
1746 total_data_bw_efficiency_x10000 = DATA_EFFICIENCY_128b_132b_x10000;
1747 break;
1748 default:
1749 return false;
1750 }
1751
1752 *cur_link_bw = link_rate_per_lane_kbps * lane_count.bits.LANE_COUNT_SET / 10000 * total_data_bw_efficiency_x10000;
1753 return true;
1754 }
1755 #endif
1756
dm_dp_mst_is_port_support_mode(struct amdgpu_dm_connector * aconnector,struct dc_stream_state * stream)1757 enum dc_status dm_dp_mst_is_port_support_mode(
1758 struct amdgpu_dm_connector *aconnector,
1759 struct dc_stream_state *stream)
1760 {
1761 #if defined(CONFIG_DRM_AMD_DC_FP)
1762 int branch_max_throughput_mps = 0;
1763 struct dc_link_settings cur_link_settings;
1764 uint32_t end_to_end_bw_in_kbps = 0;
1765 uint32_t root_link_bw_in_kbps = 0;
1766 uint32_t virtual_channel_bw_in_kbps = 0;
1767 struct dc_dsc_bw_range bw_range = {0};
1768 struct dc_dsc_config_options dsc_options = {0};
1769 uint32_t stream_kbps;
1770
1771 /* DSC unnecessary case
1772 * Check if timing could be supported within end-to-end BW
1773 */
1774 stream_kbps =
1775 dc_bandwidth_in_kbps_from_timing(&stream->timing,
1776 dc_link_get_highest_encoding_format(stream->link));
1777 cur_link_settings = stream->link->verified_link_cap;
1778 root_link_bw_in_kbps = dc_link_bandwidth_kbps(aconnector->dc_link, &cur_link_settings);
1779 virtual_channel_bw_in_kbps = kbps_from_pbn(aconnector->mst_output_port->full_pbn);
1780
1781 /* pick the end to end bw bottleneck */
1782 end_to_end_bw_in_kbps = min(root_link_bw_in_kbps, virtual_channel_bw_in_kbps);
1783
1784 if (stream_kbps <= end_to_end_bw_in_kbps) {
1785 DRM_DEBUG_DRIVER("MST_DSC no dsc required. End-to-end bw sufficient\n");
1786 return DC_OK;
1787 }
1788
1789 /*DSC necessary case*/
1790 if (!aconnector->dsc_aux)
1791 return DC_FAIL_BANDWIDTH_VALIDATE;
1792
1793 if (is_dsc_common_config_possible(stream, &bw_range)) {
1794
1795 /*capable of dsc passthough. dsc bitstream along the entire path*/
1796 if (aconnector->mst_output_port->passthrough_aux) {
1797 if (bw_range.min_kbps > end_to_end_bw_in_kbps) {
1798 DRM_DEBUG_DRIVER("MST_DSC dsc passthrough and decode at endpoint"
1799 "Max dsc compression bw can't fit into end-to-end bw\n");
1800 return DC_FAIL_BANDWIDTH_VALIDATE;
1801 }
1802 } else {
1803 /*dsc bitstream decoded at the dp last link*/
1804 struct drm_dp_mst_port *immediate_upstream_port = NULL;
1805 uint32_t end_link_bw = 0;
1806
1807 /*Get last DP link BW capability*/
1808 if (dp_get_link_current_set_bw(&aconnector->mst_output_port->aux, &end_link_bw)) {
1809 if (stream_kbps > end_link_bw) {
1810 DRM_DEBUG_DRIVER("MST_DSC dsc decode at last link."
1811 "Mode required bw can't fit into last link\n");
1812 return DC_FAIL_BANDWIDTH_VALIDATE;
1813 }
1814 }
1815
1816 /*Get virtual channel bandwidth between source and the link before the last link*/
1817 if (aconnector->mst_output_port->parent->port_parent)
1818 immediate_upstream_port = aconnector->mst_output_port->parent->port_parent;
1819
1820 if (immediate_upstream_port) {
1821 virtual_channel_bw_in_kbps = kbps_from_pbn(immediate_upstream_port->full_pbn);
1822 virtual_channel_bw_in_kbps = min(root_link_bw_in_kbps, virtual_channel_bw_in_kbps);
1823 if (bw_range.min_kbps > virtual_channel_bw_in_kbps) {
1824 DRM_DEBUG_DRIVER("MST_DSC dsc decode at last link."
1825 "Max dsc compression can't fit into MST available bw\n");
1826 return DC_FAIL_BANDWIDTH_VALIDATE;
1827 }
1828 }
1829 }
1830
1831 /*Confirm if we can obtain dsc config*/
1832 dc_dsc_get_default_config_option(stream->link->dc, &dsc_options);
1833 dsc_options.max_target_bpp_limit_override_x16 = aconnector->base.display_info.max_dsc_bpp * 16;
1834 if (dc_dsc_compute_config(stream->sink->ctx->dc->res_pool->dscs[0],
1835 &stream->sink->dsc_caps.dsc_dec_caps,
1836 &dsc_options,
1837 end_to_end_bw_in_kbps,
1838 &stream->timing,
1839 dc_link_get_highest_encoding_format(stream->link),
1840 &stream->timing.dsc_cfg)) {
1841 stream->timing.flags.DSC = 1;
1842 DRM_DEBUG_DRIVER("MST_DSC require dsc and dsc config found\n");
1843 } else {
1844 DRM_DEBUG_DRIVER("MST_DSC require dsc but can't find appropriate dsc config\n");
1845 return DC_FAIL_BANDWIDTH_VALIDATE;
1846 }
1847
1848 /* check is mst dsc output bandwidth branch_overall_throughput_0_mps */
1849 switch (stream->timing.pixel_encoding) {
1850 case PIXEL_ENCODING_RGB:
1851 case PIXEL_ENCODING_YCBCR444:
1852 branch_max_throughput_mps =
1853 aconnector->dc_sink->dsc_caps.dsc_dec_caps.branch_overall_throughput_0_mps;
1854 break;
1855 case PIXEL_ENCODING_YCBCR422:
1856 case PIXEL_ENCODING_YCBCR420:
1857 branch_max_throughput_mps =
1858 aconnector->dc_sink->dsc_caps.dsc_dec_caps.branch_overall_throughput_1_mps;
1859 break;
1860 default:
1861 break;
1862 }
1863
1864 if (branch_max_throughput_mps != 0 &&
1865 ((stream->timing.pix_clk_100hz / 10) > branch_max_throughput_mps * 1000)) {
1866 DRM_DEBUG_DRIVER("MST_DSC require dsc but max throughput mps fails\n");
1867 return DC_FAIL_BANDWIDTH_VALIDATE;
1868 }
1869 } else {
1870 DRM_DEBUG_DRIVER("MST_DSC require dsc but can't find common dsc config\n");
1871 return DC_FAIL_BANDWIDTH_VALIDATE;
1872 }
1873 #endif
1874 return DC_OK;
1875 }
1876