1 /*
2 * Copyright 2015 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 "dm_services_types.h"
27 #include "dc.h"
28 #include "dc/inc/core_types.h"
29
30 #include "vid.h"
31 #include "amdgpu.h"
32 #include "amdgpu_display.h"
33 #include "atom.h"
34 #include "amdgpu_dm.h"
35 #include "amdgpu_pm.h"
36
37 #include "amd_shared.h"
38 #include "amdgpu_dm_irq.h"
39 #include "dm_helpers.h"
40 #include "dm_services_types.h"
41 #include "amdgpu_dm_mst_types.h"
42 #if defined(CONFIG_DEBUG_FS)
43 #include "amdgpu_dm_debugfs.h"
44 #endif
45
46 #include "ivsrcid/ivsrcid_vislands30.h"
47
48 #include <linux/module.h>
49 #include <linux/moduleparam.h>
50 #include <linux/version.h>
51 #include <linux/types.h>
52 #include <linux/pm_runtime.h>
53
54 #include <drm/drmP.h>
55 #include <drm/drm_atomic.h>
56 #include <drm/drm_atomic_helper.h>
57 #include <drm/drm_dp_mst_helper.h>
58 #include <drm/drm_fb_helper.h>
59 #include <drm/drm_edid.h>
60
61 #if defined(CONFIG_DRM_AMD_DC_DCN1_0)
62 #include "ivsrcid/irqsrcs_dcn_1_0.h"
63
64 #include "dcn/dcn_1_0_offset.h"
65 #include "dcn/dcn_1_0_sh_mask.h"
66 #include "soc15_hw_ip.h"
67 #include "vega10_ip_offset.h"
68
69 #include "soc15_common.h"
70 #endif
71
72 #include "modules/inc/mod_freesync.h"
73
74 #include "i2caux_interface.h"
75
76 /* basic init/fini API */
77 static int amdgpu_dm_init(struct amdgpu_device *adev);
78 static void amdgpu_dm_fini(struct amdgpu_device *adev);
79
80 /* initializes drm_device display related structures, based on the information
81 * provided by DAL. The drm strcutures are: drm_crtc, drm_connector,
82 * drm_encoder, drm_mode_config
83 *
84 * Returns 0 on success
85 */
86 static int amdgpu_dm_initialize_drm_device(struct amdgpu_device *adev);
87 /* removes and deallocates the drm structures, created by the above function */
88 static void amdgpu_dm_destroy_drm_device(struct amdgpu_display_manager *dm);
89
90 static void
91 amdgpu_dm_update_connector_after_detect(struct amdgpu_dm_connector *aconnector);
92
93 static int amdgpu_dm_plane_init(struct amdgpu_display_manager *dm,
94 struct amdgpu_plane *aplane,
95 unsigned long possible_crtcs);
96 static int amdgpu_dm_crtc_init(struct amdgpu_display_manager *dm,
97 struct drm_plane *plane,
98 uint32_t link_index);
99 static int amdgpu_dm_connector_init(struct amdgpu_display_manager *dm,
100 struct amdgpu_dm_connector *amdgpu_dm_connector,
101 uint32_t link_index,
102 struct amdgpu_encoder *amdgpu_encoder);
103 static int amdgpu_dm_encoder_init(struct drm_device *dev,
104 struct amdgpu_encoder *aencoder,
105 uint32_t link_index);
106
107 static int amdgpu_dm_connector_get_modes(struct drm_connector *connector);
108
109 static int amdgpu_dm_atomic_commit(struct drm_device *dev,
110 struct drm_atomic_state *state,
111 bool nonblock);
112
113 static void amdgpu_dm_atomic_commit_tail(struct drm_atomic_state *state);
114
115 static int amdgpu_dm_atomic_check(struct drm_device *dev,
116 struct drm_atomic_state *state);
117
118
119
120
121 static const enum drm_plane_type dm_plane_type_default[AMDGPU_MAX_PLANES] = {
122 DRM_PLANE_TYPE_PRIMARY,
123 DRM_PLANE_TYPE_PRIMARY,
124 DRM_PLANE_TYPE_PRIMARY,
125 DRM_PLANE_TYPE_PRIMARY,
126 DRM_PLANE_TYPE_PRIMARY,
127 DRM_PLANE_TYPE_PRIMARY,
128 };
129
130 static const enum drm_plane_type dm_plane_type_carizzo[AMDGPU_MAX_PLANES] = {
131 DRM_PLANE_TYPE_PRIMARY,
132 DRM_PLANE_TYPE_PRIMARY,
133 DRM_PLANE_TYPE_PRIMARY,
134 DRM_PLANE_TYPE_OVERLAY,/* YUV Capable Underlay */
135 };
136
137 static const enum drm_plane_type dm_plane_type_stoney[AMDGPU_MAX_PLANES] = {
138 DRM_PLANE_TYPE_PRIMARY,
139 DRM_PLANE_TYPE_PRIMARY,
140 DRM_PLANE_TYPE_OVERLAY, /* YUV Capable Underlay */
141 };
142
143 /*
144 * dm_vblank_get_counter
145 *
146 * @brief
147 * Get counter for number of vertical blanks
148 *
149 * @param
150 * struct amdgpu_device *adev - [in] desired amdgpu device
151 * int disp_idx - [in] which CRTC to get the counter from
152 *
153 * @return
154 * Counter for vertical blanks
155 */
dm_vblank_get_counter(struct amdgpu_device * adev,int crtc)156 static u32 dm_vblank_get_counter(struct amdgpu_device *adev, int crtc)
157 {
158 if (crtc >= adev->mode_info.num_crtc)
159 return 0;
160 else {
161 struct amdgpu_crtc *acrtc = adev->mode_info.crtcs[crtc];
162 struct dm_crtc_state *acrtc_state = to_dm_crtc_state(
163 acrtc->base.state);
164
165
166 if (acrtc_state->stream == NULL) {
167 DRM_ERROR("dc_stream_state is NULL for crtc '%d'!\n",
168 crtc);
169 return 0;
170 }
171
172 return dc_stream_get_vblank_counter(acrtc_state->stream);
173 }
174 }
175
dm_crtc_get_scanoutpos(struct amdgpu_device * adev,int crtc,u32 * vbl,u32 * position)176 static int dm_crtc_get_scanoutpos(struct amdgpu_device *adev, int crtc,
177 u32 *vbl, u32 *position)
178 {
179 uint32_t v_blank_start, v_blank_end, h_position, v_position;
180
181 if ((crtc < 0) || (crtc >= adev->mode_info.num_crtc))
182 return -EINVAL;
183 else {
184 struct amdgpu_crtc *acrtc = adev->mode_info.crtcs[crtc];
185 struct dm_crtc_state *acrtc_state = to_dm_crtc_state(
186 acrtc->base.state);
187
188 if (acrtc_state->stream == NULL) {
189 DRM_ERROR("dc_stream_state is NULL for crtc '%d'!\n",
190 crtc);
191 return 0;
192 }
193
194 /*
195 * TODO rework base driver to use values directly.
196 * for now parse it back into reg-format
197 */
198 dc_stream_get_scanoutpos(acrtc_state->stream,
199 &v_blank_start,
200 &v_blank_end,
201 &h_position,
202 &v_position);
203
204 *position = v_position | (h_position << 16);
205 *vbl = v_blank_start | (v_blank_end << 16);
206 }
207
208 return 0;
209 }
210
dm_is_idle(void * handle)211 static bool dm_is_idle(void *handle)
212 {
213 /* XXX todo */
214 return true;
215 }
216
dm_wait_for_idle(void * handle)217 static int dm_wait_for_idle(void *handle)
218 {
219 /* XXX todo */
220 return 0;
221 }
222
dm_check_soft_reset(void * handle)223 static bool dm_check_soft_reset(void *handle)
224 {
225 return false;
226 }
227
dm_soft_reset(void * handle)228 static int dm_soft_reset(void *handle)
229 {
230 /* XXX todo */
231 return 0;
232 }
233
234 static struct amdgpu_crtc *
get_crtc_by_otg_inst(struct amdgpu_device * adev,int otg_inst)235 get_crtc_by_otg_inst(struct amdgpu_device *adev,
236 int otg_inst)
237 {
238 struct drm_device *dev = adev->ddev;
239 struct drm_crtc *crtc;
240 struct amdgpu_crtc *amdgpu_crtc;
241
242 /*
243 * following if is check inherited from both functions where this one is
244 * used now. Need to be checked why it could happen.
245 */
246 if (otg_inst == -1) {
247 WARN_ON(1);
248 return adev->mode_info.crtcs[0];
249 }
250
251 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
252 amdgpu_crtc = to_amdgpu_crtc(crtc);
253
254 if (amdgpu_crtc->otg_inst == otg_inst)
255 return amdgpu_crtc;
256 }
257
258 return NULL;
259 }
260
dm_pflip_high_irq(void * interrupt_params)261 static void dm_pflip_high_irq(void *interrupt_params)
262 {
263 struct amdgpu_crtc *amdgpu_crtc;
264 struct common_irq_params *irq_params = interrupt_params;
265 struct amdgpu_device *adev = irq_params->adev;
266 unsigned long flags;
267
268 amdgpu_crtc = get_crtc_by_otg_inst(adev, irq_params->irq_src - IRQ_TYPE_PFLIP);
269
270 /* IRQ could occur when in initial stage */
271 /*TODO work and BO cleanup */
272 if (amdgpu_crtc == NULL) {
273 DRM_DEBUG_DRIVER("CRTC is null, returning.\n");
274 return;
275 }
276
277 spin_lock_irqsave(&adev->ddev->event_lock, flags);
278
279 if (amdgpu_crtc->pflip_status != AMDGPU_FLIP_SUBMITTED){
280 DRM_DEBUG_DRIVER("amdgpu_crtc->pflip_status = %d !=AMDGPU_FLIP_SUBMITTED(%d) on crtc:%d[%p] \n",
281 amdgpu_crtc->pflip_status,
282 AMDGPU_FLIP_SUBMITTED,
283 amdgpu_crtc->crtc_id,
284 amdgpu_crtc);
285 spin_unlock_irqrestore(&adev->ddev->event_lock, flags);
286 return;
287 }
288
289
290 /* wakeup usersapce */
291 if (amdgpu_crtc->event) {
292 /* Update to correct count/ts if racing with vblank irq */
293 drm_crtc_accurate_vblank_count(&amdgpu_crtc->base);
294
295 drm_crtc_send_vblank_event(&amdgpu_crtc->base, amdgpu_crtc->event);
296
297 /* page flip completed. clean up */
298 amdgpu_crtc->event = NULL;
299
300 } else
301 WARN_ON(1);
302
303 amdgpu_crtc->pflip_status = AMDGPU_FLIP_NONE;
304 spin_unlock_irqrestore(&adev->ddev->event_lock, flags);
305
306 DRM_DEBUG_DRIVER("%s - crtc :%d[%p], pflip_stat:AMDGPU_FLIP_NONE\n",
307 __func__, amdgpu_crtc->crtc_id, amdgpu_crtc);
308
309 drm_crtc_vblank_put(&amdgpu_crtc->base);
310 }
311
dm_crtc_high_irq(void * interrupt_params)312 static void dm_crtc_high_irq(void *interrupt_params)
313 {
314 struct common_irq_params *irq_params = interrupt_params;
315 struct amdgpu_device *adev = irq_params->adev;
316 uint8_t crtc_index = 0;
317 struct amdgpu_crtc *acrtc;
318
319 acrtc = get_crtc_by_otg_inst(adev, irq_params->irq_src - IRQ_TYPE_VBLANK);
320
321 if (acrtc)
322 crtc_index = acrtc->crtc_id;
323
324 drm_handle_vblank(adev->ddev, crtc_index);
325 amdgpu_dm_crtc_handle_crc_irq(&acrtc->base);
326 }
327
dm_set_clockgating_state(void * handle,enum amd_clockgating_state state)328 static int dm_set_clockgating_state(void *handle,
329 enum amd_clockgating_state state)
330 {
331 return 0;
332 }
333
dm_set_powergating_state(void * handle,enum amd_powergating_state state)334 static int dm_set_powergating_state(void *handle,
335 enum amd_powergating_state state)
336 {
337 return 0;
338 }
339
340 /* Prototypes of private functions */
341 static int dm_early_init(void* handle);
342
hotplug_notify_work_func(struct work_struct * work)343 static void hotplug_notify_work_func(struct work_struct *work)
344 {
345 struct amdgpu_display_manager *dm = container_of(work, struct amdgpu_display_manager, mst_hotplug_work);
346 struct drm_device *dev = dm->ddev;
347
348 drm_kms_helper_hotplug_event(dev);
349 }
350
351 /* Allocate memory for FBC compressed data */
amdgpu_dm_fbc_init(struct drm_connector * connector)352 static void amdgpu_dm_fbc_init(struct drm_connector *connector)
353 {
354 struct drm_device *dev = connector->dev;
355 struct amdgpu_device *adev = dev->dev_private;
356 struct dm_comressor_info *compressor = &adev->dm.compressor;
357 struct amdgpu_dm_connector *aconn = to_amdgpu_dm_connector(connector);
358 struct drm_display_mode *mode;
359 unsigned long max_size = 0;
360
361 if (adev->dm.dc->fbc_compressor == NULL)
362 return;
363
364 if (aconn->dc_link->connector_signal != SIGNAL_TYPE_EDP)
365 return;
366
367 if (compressor->bo_ptr)
368 return;
369
370
371 list_for_each_entry(mode, &connector->modes, head) {
372 if (max_size < mode->htotal * mode->vtotal)
373 max_size = mode->htotal * mode->vtotal;
374 }
375
376 if (max_size) {
377 int r = amdgpu_bo_create_kernel(adev, max_size * 4, PAGE_SIZE,
378 AMDGPU_GEM_DOMAIN_GTT, &compressor->bo_ptr,
379 &compressor->gpu_addr, &compressor->cpu_addr);
380
381 if (r)
382 DRM_ERROR("DM: Failed to initialize FBC\n");
383 else {
384 adev->dm.dc->ctx->fbc_gpu_addr = compressor->gpu_addr;
385 DRM_INFO("DM: FBC alloc %lu\n", max_size*4);
386 }
387
388 }
389
390 }
391
392
393 /* Init display KMS
394 *
395 * Returns 0 on success
396 */
amdgpu_dm_init(struct amdgpu_device * adev)397 static int amdgpu_dm_init(struct amdgpu_device *adev)
398 {
399 struct dc_init_data init_data;
400 adev->dm.ddev = adev->ddev;
401 adev->dm.adev = adev;
402
403 /* Zero all the fields */
404 memset(&init_data, 0, sizeof(init_data));
405
406 if(amdgpu_dm_irq_init(adev)) {
407 DRM_ERROR("amdgpu: failed to initialize DM IRQ support.\n");
408 goto error;
409 }
410
411 init_data.asic_id.chip_family = adev->family;
412
413 init_data.asic_id.pci_revision_id = adev->rev_id;
414 init_data.asic_id.hw_internal_rev = adev->external_rev_id;
415
416 init_data.asic_id.vram_width = adev->gmc.vram_width;
417 /* TODO: initialize init_data.asic_id.vram_type here!!!! */
418 init_data.asic_id.atombios_base_address =
419 adev->mode_info.atom_context->bios;
420
421 init_data.driver = adev;
422
423 adev->dm.cgs_device = amdgpu_cgs_create_device(adev);
424
425 if (!adev->dm.cgs_device) {
426 DRM_ERROR("amdgpu: failed to create cgs device.\n");
427 goto error;
428 }
429
430 init_data.cgs_device = adev->dm.cgs_device;
431
432 adev->dm.dal = NULL;
433
434 init_data.dce_environment = DCE_ENV_PRODUCTION_DRV;
435
436 /*
437 * TODO debug why this doesn't work on Raven
438 */
439 if (adev->flags & AMD_IS_APU &&
440 adev->asic_type >= CHIP_CARRIZO &&
441 adev->asic_type < CHIP_RAVEN)
442 init_data.flags.gpu_vm_support = true;
443
444 /* Display Core create. */
445 adev->dm.dc = dc_create(&init_data);
446
447 if (adev->dm.dc) {
448 DRM_INFO("Display Core initialized with v%s!\n", DC_VER);
449 } else {
450 DRM_INFO("Display Core failed to initialize with v%s!\n", DC_VER);
451 goto error;
452 }
453
454 INIT_WORK(&adev->dm.mst_hotplug_work, hotplug_notify_work_func);
455
456 adev->dm.freesync_module = mod_freesync_create(adev->dm.dc);
457 if (!adev->dm.freesync_module) {
458 DRM_ERROR(
459 "amdgpu: failed to initialize freesync_module.\n");
460 } else
461 DRM_DEBUG_DRIVER("amdgpu: freesync_module init done %p.\n",
462 adev->dm.freesync_module);
463
464 amdgpu_dm_init_color_mod();
465
466 if (amdgpu_dm_initialize_drm_device(adev)) {
467 DRM_ERROR(
468 "amdgpu: failed to initialize sw for display support.\n");
469 goto error;
470 }
471
472 /* Update the actual used number of crtc */
473 adev->mode_info.num_crtc = adev->dm.display_indexes_num;
474
475 /* TODO: Add_display_info? */
476
477 /* TODO use dynamic cursor width */
478 adev->ddev->mode_config.cursor_width = adev->dm.dc->caps.max_cursor_size;
479 adev->ddev->mode_config.cursor_height = adev->dm.dc->caps.max_cursor_size;
480
481 if (drm_vblank_init(adev->ddev, adev->dm.display_indexes_num)) {
482 DRM_ERROR(
483 "amdgpu: failed to initialize sw for display support.\n");
484 goto error;
485 }
486
487 DRM_DEBUG_DRIVER("KMS initialized.\n");
488
489 return 0;
490 error:
491 amdgpu_dm_fini(adev);
492
493 return -1;
494 }
495
amdgpu_dm_fini(struct amdgpu_device * adev)496 static void amdgpu_dm_fini(struct amdgpu_device *adev)
497 {
498 amdgpu_dm_destroy_drm_device(&adev->dm);
499 /*
500 * TODO: pageflip, vlank interrupt
501 *
502 * amdgpu_dm_irq_fini(adev);
503 */
504
505 if (adev->dm.cgs_device) {
506 amdgpu_cgs_destroy_device(adev->dm.cgs_device);
507 adev->dm.cgs_device = NULL;
508 }
509 if (adev->dm.freesync_module) {
510 mod_freesync_destroy(adev->dm.freesync_module);
511 adev->dm.freesync_module = NULL;
512 }
513 /* DC Destroy TODO: Replace destroy DAL */
514 if (adev->dm.dc)
515 dc_destroy(&adev->dm.dc);
516 return;
517 }
518
dm_sw_init(void * handle)519 static int dm_sw_init(void *handle)
520 {
521 return 0;
522 }
523
dm_sw_fini(void * handle)524 static int dm_sw_fini(void *handle)
525 {
526 return 0;
527 }
528
detect_mst_link_for_all_connectors(struct drm_device * dev)529 static int detect_mst_link_for_all_connectors(struct drm_device *dev)
530 {
531 struct amdgpu_dm_connector *aconnector;
532 struct drm_connector *connector;
533 int ret = 0;
534
535 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
536
537 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
538 aconnector = to_amdgpu_dm_connector(connector);
539 if (aconnector->dc_link->type == dc_connection_mst_branch &&
540 aconnector->mst_mgr.aux) {
541 DRM_DEBUG_DRIVER("DM_MST: starting TM on aconnector: %p [id: %d]\n",
542 aconnector, aconnector->base.base.id);
543
544 ret = drm_dp_mst_topology_mgr_set_mst(&aconnector->mst_mgr, true);
545 if (ret < 0) {
546 DRM_ERROR("DM_MST: Failed to start MST\n");
547 ((struct dc_link *)aconnector->dc_link)->type = dc_connection_single;
548 return ret;
549 }
550 }
551 }
552
553 drm_modeset_unlock(&dev->mode_config.connection_mutex);
554 return ret;
555 }
556
dm_late_init(void * handle)557 static int dm_late_init(void *handle)
558 {
559 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
560
561 return detect_mst_link_for_all_connectors(adev->ddev);
562 }
563
s3_handle_mst(struct drm_device * dev,bool suspend)564 static void s3_handle_mst(struct drm_device *dev, bool suspend)
565 {
566 struct amdgpu_dm_connector *aconnector;
567 struct drm_connector *connector;
568 struct drm_dp_mst_topology_mgr *mgr;
569 int ret;
570 bool need_hotplug = false;
571
572 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
573
574 list_for_each_entry(connector, &dev->mode_config.connector_list,
575 head) {
576 aconnector = to_amdgpu_dm_connector(connector);
577 if (aconnector->dc_link->type != dc_connection_mst_branch ||
578 aconnector->mst_port)
579 continue;
580
581 mgr = &aconnector->mst_mgr;
582
583 if (suspend) {
584 drm_dp_mst_topology_mgr_suspend(mgr);
585 } else {
586 ret = drm_dp_mst_topology_mgr_resume(mgr);
587 if (ret < 0) {
588 drm_dp_mst_topology_mgr_set_mst(mgr, false);
589 need_hotplug = true;
590 }
591 }
592 }
593
594 drm_modeset_unlock(&dev->mode_config.connection_mutex);
595
596 if (need_hotplug)
597 drm_kms_helper_hotplug_event(dev);
598 }
599
dm_hw_init(void * handle)600 static int dm_hw_init(void *handle)
601 {
602 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
603 /* Create DAL display manager */
604 amdgpu_dm_init(adev);
605 amdgpu_dm_hpd_init(adev);
606
607 return 0;
608 }
609
dm_hw_fini(void * handle)610 static int dm_hw_fini(void *handle)
611 {
612 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
613
614 amdgpu_dm_hpd_fini(adev);
615
616 amdgpu_dm_irq_fini(adev);
617 amdgpu_dm_fini(adev);
618 return 0;
619 }
620
dm_suspend(void * handle)621 static int dm_suspend(void *handle)
622 {
623 struct amdgpu_device *adev = handle;
624 struct amdgpu_display_manager *dm = &adev->dm;
625 int ret = 0;
626
627 WARN_ON(adev->dm.cached_state);
628 adev->dm.cached_state = drm_atomic_helper_suspend(adev->ddev);
629
630 s3_handle_mst(adev->ddev, true);
631
632 amdgpu_dm_irq_suspend(adev);
633
634
635 dc_set_power_state(dm->dc, DC_ACPI_CM_POWER_STATE_D3);
636
637 return ret;
638 }
639
640 static struct amdgpu_dm_connector *
amdgpu_dm_find_first_crtc_matching_connector(struct drm_atomic_state * state,struct drm_crtc * crtc)641 amdgpu_dm_find_first_crtc_matching_connector(struct drm_atomic_state *state,
642 struct drm_crtc *crtc)
643 {
644 uint32_t i;
645 struct drm_connector_state *new_con_state;
646 struct drm_connector *connector;
647 struct drm_crtc *crtc_from_state;
648
649 for_each_new_connector_in_state(state, connector, new_con_state, i) {
650 crtc_from_state = new_con_state->crtc;
651
652 if (crtc_from_state == crtc)
653 return to_amdgpu_dm_connector(connector);
654 }
655
656 return NULL;
657 }
658
emulated_link_detect(struct dc_link * link)659 static void emulated_link_detect(struct dc_link *link)
660 {
661 struct dc_sink_init_data sink_init_data = { 0 };
662 struct display_sink_capability sink_caps = { 0 };
663 enum dc_edid_status edid_status;
664 struct dc_context *dc_ctx = link->ctx;
665 struct dc_sink *sink = NULL;
666 struct dc_sink *prev_sink = NULL;
667
668 link->type = dc_connection_none;
669 prev_sink = link->local_sink;
670
671 if (prev_sink != NULL)
672 dc_sink_retain(prev_sink);
673
674 switch (link->connector_signal) {
675 case SIGNAL_TYPE_HDMI_TYPE_A: {
676 sink_caps.transaction_type = DDC_TRANSACTION_TYPE_I2C;
677 sink_caps.signal = SIGNAL_TYPE_HDMI_TYPE_A;
678 break;
679 }
680
681 case SIGNAL_TYPE_DVI_SINGLE_LINK: {
682 sink_caps.transaction_type = DDC_TRANSACTION_TYPE_I2C;
683 sink_caps.signal = SIGNAL_TYPE_DVI_SINGLE_LINK;
684 break;
685 }
686
687 case SIGNAL_TYPE_DVI_DUAL_LINK: {
688 sink_caps.transaction_type = DDC_TRANSACTION_TYPE_I2C;
689 sink_caps.signal = SIGNAL_TYPE_DVI_DUAL_LINK;
690 break;
691 }
692
693 case SIGNAL_TYPE_LVDS: {
694 sink_caps.transaction_type = DDC_TRANSACTION_TYPE_I2C;
695 sink_caps.signal = SIGNAL_TYPE_LVDS;
696 break;
697 }
698
699 case SIGNAL_TYPE_EDP: {
700 sink_caps.transaction_type =
701 DDC_TRANSACTION_TYPE_I2C_OVER_AUX;
702 sink_caps.signal = SIGNAL_TYPE_EDP;
703 break;
704 }
705
706 case SIGNAL_TYPE_DISPLAY_PORT: {
707 sink_caps.transaction_type =
708 DDC_TRANSACTION_TYPE_I2C_OVER_AUX;
709 sink_caps.signal = SIGNAL_TYPE_VIRTUAL;
710 break;
711 }
712
713 default:
714 DC_ERROR("Invalid connector type! signal:%d\n",
715 link->connector_signal);
716 return;
717 }
718
719 sink_init_data.link = link;
720 sink_init_data.sink_signal = sink_caps.signal;
721
722 sink = dc_sink_create(&sink_init_data);
723 if (!sink) {
724 DC_ERROR("Failed to create sink!\n");
725 return;
726 }
727
728 link->local_sink = sink;
729
730 edid_status = dm_helpers_read_local_edid(
731 link->ctx,
732 link,
733 sink);
734
735 if (edid_status != EDID_OK)
736 DC_ERROR("Failed to read EDID");
737
738 }
739
dm_resume(void * handle)740 static int dm_resume(void *handle)
741 {
742 struct amdgpu_device *adev = handle;
743 struct drm_device *ddev = adev->ddev;
744 struct amdgpu_display_manager *dm = &adev->dm;
745 struct amdgpu_dm_connector *aconnector;
746 struct drm_connector *connector;
747 struct drm_crtc *crtc;
748 struct drm_crtc_state *new_crtc_state;
749 struct dm_crtc_state *dm_new_crtc_state;
750 struct drm_plane *plane;
751 struct drm_plane_state *new_plane_state;
752 struct dm_plane_state *dm_new_plane_state;
753 enum dc_connection_type new_connection_type = dc_connection_none;
754 int i;
755
756 /* power on hardware */
757 dc_set_power_state(dm->dc, DC_ACPI_CM_POWER_STATE_D0);
758
759 /* program HPD filter */
760 dc_resume(dm->dc);
761
762 /* On resume we need to rewrite the MSTM control bits to enamble MST*/
763 s3_handle_mst(ddev, false);
764
765 /*
766 * early enable HPD Rx IRQ, should be done before set mode as short
767 * pulse interrupts are used for MST
768 */
769 amdgpu_dm_irq_resume_early(adev);
770
771 /* Do detection*/
772 list_for_each_entry(connector, &ddev->mode_config.connector_list, head) {
773 aconnector = to_amdgpu_dm_connector(connector);
774
775 /*
776 * this is the case when traversing through already created
777 * MST connectors, should be skipped
778 */
779 if (aconnector->mst_port)
780 continue;
781
782 mutex_lock(&aconnector->hpd_lock);
783 if (!dc_link_detect_sink(aconnector->dc_link, &new_connection_type))
784 DRM_ERROR("KMS: Failed to detect connector\n");
785
786 if (aconnector->base.force && new_connection_type == dc_connection_none)
787 emulated_link_detect(aconnector->dc_link);
788 else
789 dc_link_detect(aconnector->dc_link, DETECT_REASON_HPD);
790
791 if (aconnector->fake_enable && aconnector->dc_link->local_sink)
792 aconnector->fake_enable = false;
793
794 aconnector->dc_sink = NULL;
795 amdgpu_dm_update_connector_after_detect(aconnector);
796 mutex_unlock(&aconnector->hpd_lock);
797 }
798
799 /* Force mode set in atomic comit */
800 for_each_new_crtc_in_state(dm->cached_state, crtc, new_crtc_state, i)
801 new_crtc_state->active_changed = true;
802
803 /*
804 * atomic_check is expected to create the dc states. We need to release
805 * them here, since they were duplicated as part of the suspend
806 * procedure.
807 */
808 for_each_new_crtc_in_state(dm->cached_state, crtc, new_crtc_state, i) {
809 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
810 if (dm_new_crtc_state->stream) {
811 WARN_ON(kref_read(&dm_new_crtc_state->stream->refcount) > 1);
812 dc_stream_release(dm_new_crtc_state->stream);
813 dm_new_crtc_state->stream = NULL;
814 }
815 }
816
817 for_each_new_plane_in_state(dm->cached_state, plane, new_plane_state, i) {
818 dm_new_plane_state = to_dm_plane_state(new_plane_state);
819 if (dm_new_plane_state->dc_state) {
820 WARN_ON(kref_read(&dm_new_plane_state->dc_state->refcount) > 1);
821 dc_plane_state_release(dm_new_plane_state->dc_state);
822 dm_new_plane_state->dc_state = NULL;
823 }
824 }
825
826 drm_atomic_helper_resume(ddev, dm->cached_state);
827
828 dm->cached_state = NULL;
829
830 amdgpu_dm_irq_resume_late(adev);
831
832 return 0;
833 }
834
835 static const struct amd_ip_funcs amdgpu_dm_funcs = {
836 .name = "dm",
837 .early_init = dm_early_init,
838 .late_init = dm_late_init,
839 .sw_init = dm_sw_init,
840 .sw_fini = dm_sw_fini,
841 .hw_init = dm_hw_init,
842 .hw_fini = dm_hw_fini,
843 .suspend = dm_suspend,
844 .resume = dm_resume,
845 .is_idle = dm_is_idle,
846 .wait_for_idle = dm_wait_for_idle,
847 .check_soft_reset = dm_check_soft_reset,
848 .soft_reset = dm_soft_reset,
849 .set_clockgating_state = dm_set_clockgating_state,
850 .set_powergating_state = dm_set_powergating_state,
851 };
852
853 const struct amdgpu_ip_block_version dm_ip_block =
854 {
855 .type = AMD_IP_BLOCK_TYPE_DCE,
856 .major = 1,
857 .minor = 0,
858 .rev = 0,
859 .funcs = &amdgpu_dm_funcs,
860 };
861
862
863 static struct drm_atomic_state *
dm_atomic_state_alloc(struct drm_device * dev)864 dm_atomic_state_alloc(struct drm_device *dev)
865 {
866 struct dm_atomic_state *state = kzalloc(sizeof(*state), GFP_KERNEL);
867
868 if (!state)
869 return NULL;
870
871 if (drm_atomic_state_init(dev, &state->base) < 0)
872 goto fail;
873
874 return &state->base;
875
876 fail:
877 kfree(state);
878 return NULL;
879 }
880
881 static void
dm_atomic_state_clear(struct drm_atomic_state * state)882 dm_atomic_state_clear(struct drm_atomic_state *state)
883 {
884 struct dm_atomic_state *dm_state = to_dm_atomic_state(state);
885
886 if (dm_state->context) {
887 dc_release_state(dm_state->context);
888 dm_state->context = NULL;
889 }
890
891 drm_atomic_state_default_clear(state);
892 }
893
894 static void
dm_atomic_state_alloc_free(struct drm_atomic_state * state)895 dm_atomic_state_alloc_free(struct drm_atomic_state *state)
896 {
897 struct dm_atomic_state *dm_state = to_dm_atomic_state(state);
898 drm_atomic_state_default_release(state);
899 kfree(dm_state);
900 }
901
902 static const struct drm_mode_config_funcs amdgpu_dm_mode_funcs = {
903 .fb_create = amdgpu_display_user_framebuffer_create,
904 .output_poll_changed = drm_fb_helper_output_poll_changed,
905 .atomic_check = amdgpu_dm_atomic_check,
906 .atomic_commit = amdgpu_dm_atomic_commit,
907 .atomic_state_alloc = dm_atomic_state_alloc,
908 .atomic_state_clear = dm_atomic_state_clear,
909 .atomic_state_free = dm_atomic_state_alloc_free
910 };
911
912 static struct drm_mode_config_helper_funcs amdgpu_dm_mode_config_helperfuncs = {
913 .atomic_commit_tail = amdgpu_dm_atomic_commit_tail
914 };
915
916 static void
amdgpu_dm_update_connector_after_detect(struct amdgpu_dm_connector * aconnector)917 amdgpu_dm_update_connector_after_detect(struct amdgpu_dm_connector *aconnector)
918 {
919 struct drm_connector *connector = &aconnector->base;
920 struct drm_device *dev = connector->dev;
921 struct dc_sink *sink;
922
923 /* MST handled by drm_mst framework */
924 if (aconnector->mst_mgr.mst_state == true)
925 return;
926
927
928 sink = aconnector->dc_link->local_sink;
929
930 /* Edid mgmt connector gets first update only in mode_valid hook and then
931 * the connector sink is set to either fake or physical sink depends on link status.
932 * don't do it here if u are during boot
933 */
934 if (aconnector->base.force != DRM_FORCE_UNSPECIFIED
935 && aconnector->dc_em_sink) {
936
937 /* For S3 resume with headless use eml_sink to fake stream
938 * because on resume connecotr->sink is set ti NULL
939 */
940 mutex_lock(&dev->mode_config.mutex);
941
942 if (sink) {
943 if (aconnector->dc_sink) {
944 amdgpu_dm_remove_sink_from_freesync_module(
945 connector);
946 /* retain and release bellow are used for
947 * bump up refcount for sink because the link don't point
948 * to it anymore after disconnect so on next crtc to connector
949 * reshuffle by UMD we will get into unwanted dc_sink release
950 */
951 if (aconnector->dc_sink != aconnector->dc_em_sink)
952 dc_sink_release(aconnector->dc_sink);
953 }
954 aconnector->dc_sink = sink;
955 amdgpu_dm_add_sink_to_freesync_module(
956 connector, aconnector->edid);
957 } else {
958 amdgpu_dm_remove_sink_from_freesync_module(connector);
959 if (!aconnector->dc_sink)
960 aconnector->dc_sink = aconnector->dc_em_sink;
961 else if (aconnector->dc_sink != aconnector->dc_em_sink)
962 dc_sink_retain(aconnector->dc_sink);
963 }
964
965 mutex_unlock(&dev->mode_config.mutex);
966 return;
967 }
968
969 /*
970 * TODO: temporary guard to look for proper fix
971 * if this sink is MST sink, we should not do anything
972 */
973 if (sink && sink->sink_signal == SIGNAL_TYPE_DISPLAY_PORT_MST)
974 return;
975
976 if (aconnector->dc_sink == sink) {
977 /* We got a DP short pulse (Link Loss, DP CTS, etc...).
978 * Do nothing!! */
979 DRM_DEBUG_DRIVER("DCHPD: connector_id=%d: dc_sink didn't change.\n",
980 aconnector->connector_id);
981 return;
982 }
983
984 DRM_DEBUG_DRIVER("DCHPD: connector_id=%d: Old sink=%p New sink=%p\n",
985 aconnector->connector_id, aconnector->dc_sink, sink);
986
987 mutex_lock(&dev->mode_config.mutex);
988
989 /* 1. Update status of the drm connector
990 * 2. Send an event and let userspace tell us what to do */
991 if (sink) {
992 /* TODO: check if we still need the S3 mode update workaround.
993 * If yes, put it here. */
994 if (aconnector->dc_sink)
995 amdgpu_dm_remove_sink_from_freesync_module(
996 connector);
997
998 aconnector->dc_sink = sink;
999 if (sink->dc_edid.length == 0) {
1000 aconnector->edid = NULL;
1001 } else {
1002 aconnector->edid =
1003 (struct edid *) sink->dc_edid.raw_edid;
1004
1005
1006 drm_connector_update_edid_property(connector,
1007 aconnector->edid);
1008 }
1009 amdgpu_dm_add_sink_to_freesync_module(connector, aconnector->edid);
1010
1011 } else {
1012 amdgpu_dm_remove_sink_from_freesync_module(connector);
1013 drm_connector_update_edid_property(connector, NULL);
1014 aconnector->num_modes = 0;
1015 aconnector->dc_sink = NULL;
1016 aconnector->edid = NULL;
1017 }
1018
1019 mutex_unlock(&dev->mode_config.mutex);
1020 }
1021
handle_hpd_irq(void * param)1022 static void handle_hpd_irq(void *param)
1023 {
1024 struct amdgpu_dm_connector *aconnector = (struct amdgpu_dm_connector *)param;
1025 struct drm_connector *connector = &aconnector->base;
1026 struct drm_device *dev = connector->dev;
1027 enum dc_connection_type new_connection_type = dc_connection_none;
1028
1029 /* In case of failure or MST no need to update connector status or notify the OS
1030 * since (for MST case) MST does this in it's own context.
1031 */
1032 mutex_lock(&aconnector->hpd_lock);
1033
1034 if (aconnector->fake_enable)
1035 aconnector->fake_enable = false;
1036
1037 if (!dc_link_detect_sink(aconnector->dc_link, &new_connection_type))
1038 DRM_ERROR("KMS: Failed to detect connector\n");
1039
1040 if (aconnector->base.force && new_connection_type == dc_connection_none) {
1041 emulated_link_detect(aconnector->dc_link);
1042
1043
1044 drm_modeset_lock_all(dev);
1045 dm_restore_drm_connector_state(dev, connector);
1046 drm_modeset_unlock_all(dev);
1047
1048 if (aconnector->base.force == DRM_FORCE_UNSPECIFIED)
1049 drm_kms_helper_hotplug_event(dev);
1050
1051 } else if (dc_link_detect(aconnector->dc_link, DETECT_REASON_HPD)) {
1052 amdgpu_dm_update_connector_after_detect(aconnector);
1053
1054
1055 drm_modeset_lock_all(dev);
1056 dm_restore_drm_connector_state(dev, connector);
1057 drm_modeset_unlock_all(dev);
1058
1059 if (aconnector->base.force == DRM_FORCE_UNSPECIFIED)
1060 drm_kms_helper_hotplug_event(dev);
1061 }
1062 mutex_unlock(&aconnector->hpd_lock);
1063
1064 }
1065
dm_handle_hpd_rx_irq(struct amdgpu_dm_connector * aconnector)1066 static void dm_handle_hpd_rx_irq(struct amdgpu_dm_connector *aconnector)
1067 {
1068 uint8_t esi[DP_PSR_ERROR_STATUS - DP_SINK_COUNT_ESI] = { 0 };
1069 uint8_t dret;
1070 bool new_irq_handled = false;
1071 int dpcd_addr;
1072 int dpcd_bytes_to_read;
1073
1074 const int max_process_count = 30;
1075 int process_count = 0;
1076
1077 const struct dc_link_status *link_status = dc_link_get_status(aconnector->dc_link);
1078
1079 if (link_status->dpcd_caps->dpcd_rev.raw < 0x12) {
1080 dpcd_bytes_to_read = DP_LANE0_1_STATUS - DP_SINK_COUNT;
1081 /* DPCD 0x200 - 0x201 for downstream IRQ */
1082 dpcd_addr = DP_SINK_COUNT;
1083 } else {
1084 dpcd_bytes_to_read = DP_PSR_ERROR_STATUS - DP_SINK_COUNT_ESI;
1085 /* DPCD 0x2002 - 0x2005 for downstream IRQ */
1086 dpcd_addr = DP_SINK_COUNT_ESI;
1087 }
1088
1089 dret = drm_dp_dpcd_read(
1090 &aconnector->dm_dp_aux.aux,
1091 dpcd_addr,
1092 esi,
1093 dpcd_bytes_to_read);
1094
1095 while (dret == dpcd_bytes_to_read &&
1096 process_count < max_process_count) {
1097 uint8_t retry;
1098 dret = 0;
1099
1100 process_count++;
1101
1102 DRM_DEBUG_DRIVER("ESI %02x %02x %02x\n", esi[0], esi[1], esi[2]);
1103 /* handle HPD short pulse irq */
1104 if (aconnector->mst_mgr.mst_state)
1105 drm_dp_mst_hpd_irq(
1106 &aconnector->mst_mgr,
1107 esi,
1108 &new_irq_handled);
1109
1110 if (new_irq_handled) {
1111 /* ACK at DPCD to notify down stream */
1112 const int ack_dpcd_bytes_to_write =
1113 dpcd_bytes_to_read - 1;
1114
1115 for (retry = 0; retry < 3; retry++) {
1116 uint8_t wret;
1117
1118 wret = drm_dp_dpcd_write(
1119 &aconnector->dm_dp_aux.aux,
1120 dpcd_addr + 1,
1121 &esi[1],
1122 ack_dpcd_bytes_to_write);
1123 if (wret == ack_dpcd_bytes_to_write)
1124 break;
1125 }
1126
1127 /* check if there is new irq to be handle */
1128 dret = drm_dp_dpcd_read(
1129 &aconnector->dm_dp_aux.aux,
1130 dpcd_addr,
1131 esi,
1132 dpcd_bytes_to_read);
1133
1134 new_irq_handled = false;
1135 } else {
1136 break;
1137 }
1138 }
1139
1140 if (process_count == max_process_count)
1141 DRM_DEBUG_DRIVER("Loop exceeded max iterations\n");
1142 }
1143
handle_hpd_rx_irq(void * param)1144 static void handle_hpd_rx_irq(void *param)
1145 {
1146 struct amdgpu_dm_connector *aconnector = (struct amdgpu_dm_connector *)param;
1147 struct drm_connector *connector = &aconnector->base;
1148 struct drm_device *dev = connector->dev;
1149 struct dc_link *dc_link = aconnector->dc_link;
1150 bool is_mst_root_connector = aconnector->mst_mgr.mst_state;
1151 enum dc_connection_type new_connection_type = dc_connection_none;
1152
1153 /* TODO:Temporary add mutex to protect hpd interrupt not have a gpio
1154 * conflict, after implement i2c helper, this mutex should be
1155 * retired.
1156 */
1157 if (dc_link->type != dc_connection_mst_branch)
1158 mutex_lock(&aconnector->hpd_lock);
1159
1160 if (dc_link_handle_hpd_rx_irq(dc_link, NULL, NULL) &&
1161 !is_mst_root_connector) {
1162 /* Downstream Port status changed. */
1163 if (!dc_link_detect_sink(dc_link, &new_connection_type))
1164 DRM_ERROR("KMS: Failed to detect connector\n");
1165
1166 if (aconnector->base.force && new_connection_type == dc_connection_none) {
1167 emulated_link_detect(dc_link);
1168
1169 if (aconnector->fake_enable)
1170 aconnector->fake_enable = false;
1171
1172 amdgpu_dm_update_connector_after_detect(aconnector);
1173
1174
1175 drm_modeset_lock_all(dev);
1176 dm_restore_drm_connector_state(dev, connector);
1177 drm_modeset_unlock_all(dev);
1178
1179 drm_kms_helper_hotplug_event(dev);
1180 } else if (dc_link_detect(dc_link, DETECT_REASON_HPDRX)) {
1181
1182 if (aconnector->fake_enable)
1183 aconnector->fake_enable = false;
1184
1185 amdgpu_dm_update_connector_after_detect(aconnector);
1186
1187
1188 drm_modeset_lock_all(dev);
1189 dm_restore_drm_connector_state(dev, connector);
1190 drm_modeset_unlock_all(dev);
1191
1192 drm_kms_helper_hotplug_event(dev);
1193 }
1194 }
1195 if ((dc_link->cur_link_settings.lane_count != LANE_COUNT_UNKNOWN) ||
1196 (dc_link->type == dc_connection_mst_branch))
1197 dm_handle_hpd_rx_irq(aconnector);
1198
1199 if (dc_link->type != dc_connection_mst_branch)
1200 mutex_unlock(&aconnector->hpd_lock);
1201 }
1202
register_hpd_handlers(struct amdgpu_device * adev)1203 static void register_hpd_handlers(struct amdgpu_device *adev)
1204 {
1205 struct drm_device *dev = adev->ddev;
1206 struct drm_connector *connector;
1207 struct amdgpu_dm_connector *aconnector;
1208 const struct dc_link *dc_link;
1209 struct dc_interrupt_params int_params = {0};
1210
1211 int_params.requested_polarity = INTERRUPT_POLARITY_DEFAULT;
1212 int_params.current_polarity = INTERRUPT_POLARITY_DEFAULT;
1213
1214 list_for_each_entry(connector,
1215 &dev->mode_config.connector_list, head) {
1216
1217 aconnector = to_amdgpu_dm_connector(connector);
1218 dc_link = aconnector->dc_link;
1219
1220 if (DC_IRQ_SOURCE_INVALID != dc_link->irq_source_hpd) {
1221 int_params.int_context = INTERRUPT_LOW_IRQ_CONTEXT;
1222 int_params.irq_source = dc_link->irq_source_hpd;
1223
1224 amdgpu_dm_irq_register_interrupt(adev, &int_params,
1225 handle_hpd_irq,
1226 (void *) aconnector);
1227 }
1228
1229 if (DC_IRQ_SOURCE_INVALID != dc_link->irq_source_hpd_rx) {
1230
1231 /* Also register for DP short pulse (hpd_rx). */
1232 int_params.int_context = INTERRUPT_LOW_IRQ_CONTEXT;
1233 int_params.irq_source = dc_link->irq_source_hpd_rx;
1234
1235 amdgpu_dm_irq_register_interrupt(adev, &int_params,
1236 handle_hpd_rx_irq,
1237 (void *) aconnector);
1238 }
1239 }
1240 }
1241
1242 /* Register IRQ sources and initialize IRQ callbacks */
dce110_register_irq_handlers(struct amdgpu_device * adev)1243 static int dce110_register_irq_handlers(struct amdgpu_device *adev)
1244 {
1245 struct dc *dc = adev->dm.dc;
1246 struct common_irq_params *c_irq_params;
1247 struct dc_interrupt_params int_params = {0};
1248 int r;
1249 int i;
1250 unsigned client_id = AMDGPU_IH_CLIENTID_LEGACY;
1251
1252 if (adev->asic_type == CHIP_VEGA10 ||
1253 adev->asic_type == CHIP_VEGA12 ||
1254 adev->asic_type == CHIP_VEGA20 ||
1255 adev->asic_type == CHIP_RAVEN)
1256 client_id = SOC15_IH_CLIENTID_DCE;
1257
1258 int_params.requested_polarity = INTERRUPT_POLARITY_DEFAULT;
1259 int_params.current_polarity = INTERRUPT_POLARITY_DEFAULT;
1260
1261 /* Actions of amdgpu_irq_add_id():
1262 * 1. Register a set() function with base driver.
1263 * Base driver will call set() function to enable/disable an
1264 * interrupt in DC hardware.
1265 * 2. Register amdgpu_dm_irq_handler().
1266 * Base driver will call amdgpu_dm_irq_handler() for ALL interrupts
1267 * coming from DC hardware.
1268 * amdgpu_dm_irq_handler() will re-direct the interrupt to DC
1269 * for acknowledging and handling. */
1270
1271 /* Use VBLANK interrupt */
1272 for (i = VISLANDS30_IV_SRCID_D1_VERTICAL_INTERRUPT0; i <= VISLANDS30_IV_SRCID_D6_VERTICAL_INTERRUPT0; i++) {
1273 r = amdgpu_irq_add_id(adev, client_id, i, &adev->crtc_irq);
1274 if (r) {
1275 DRM_ERROR("Failed to add crtc irq id!\n");
1276 return r;
1277 }
1278
1279 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
1280 int_params.irq_source =
1281 dc_interrupt_to_irq_source(dc, i, 0);
1282
1283 c_irq_params = &adev->dm.vblank_params[int_params.irq_source - DC_IRQ_SOURCE_VBLANK1];
1284
1285 c_irq_params->adev = adev;
1286 c_irq_params->irq_src = int_params.irq_source;
1287
1288 amdgpu_dm_irq_register_interrupt(adev, &int_params,
1289 dm_crtc_high_irq, c_irq_params);
1290 }
1291
1292 /* Use GRPH_PFLIP interrupt */
1293 for (i = VISLANDS30_IV_SRCID_D1_GRPH_PFLIP;
1294 i <= VISLANDS30_IV_SRCID_D6_GRPH_PFLIP; i += 2) {
1295 r = amdgpu_irq_add_id(adev, client_id, i, &adev->pageflip_irq);
1296 if (r) {
1297 DRM_ERROR("Failed to add page flip irq id!\n");
1298 return r;
1299 }
1300
1301 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
1302 int_params.irq_source =
1303 dc_interrupt_to_irq_source(dc, i, 0);
1304
1305 c_irq_params = &adev->dm.pflip_params[int_params.irq_source - DC_IRQ_SOURCE_PFLIP_FIRST];
1306
1307 c_irq_params->adev = adev;
1308 c_irq_params->irq_src = int_params.irq_source;
1309
1310 amdgpu_dm_irq_register_interrupt(adev, &int_params,
1311 dm_pflip_high_irq, c_irq_params);
1312
1313 }
1314
1315 /* HPD */
1316 r = amdgpu_irq_add_id(adev, client_id,
1317 VISLANDS30_IV_SRCID_HOTPLUG_DETECT_A, &adev->hpd_irq);
1318 if (r) {
1319 DRM_ERROR("Failed to add hpd irq id!\n");
1320 return r;
1321 }
1322
1323 register_hpd_handlers(adev);
1324
1325 return 0;
1326 }
1327
1328 #if defined(CONFIG_DRM_AMD_DC_DCN1_0)
1329 /* Register IRQ sources and initialize IRQ callbacks */
dcn10_register_irq_handlers(struct amdgpu_device * adev)1330 static int dcn10_register_irq_handlers(struct amdgpu_device *adev)
1331 {
1332 struct dc *dc = adev->dm.dc;
1333 struct common_irq_params *c_irq_params;
1334 struct dc_interrupt_params int_params = {0};
1335 int r;
1336 int i;
1337
1338 int_params.requested_polarity = INTERRUPT_POLARITY_DEFAULT;
1339 int_params.current_polarity = INTERRUPT_POLARITY_DEFAULT;
1340
1341 /* Actions of amdgpu_irq_add_id():
1342 * 1. Register a set() function with base driver.
1343 * Base driver will call set() function to enable/disable an
1344 * interrupt in DC hardware.
1345 * 2. Register amdgpu_dm_irq_handler().
1346 * Base driver will call amdgpu_dm_irq_handler() for ALL interrupts
1347 * coming from DC hardware.
1348 * amdgpu_dm_irq_handler() will re-direct the interrupt to DC
1349 * for acknowledging and handling.
1350 * */
1351
1352 /* Use VSTARTUP interrupt */
1353 for (i = DCN_1_0__SRCID__DC_D1_OTG_VSTARTUP;
1354 i <= DCN_1_0__SRCID__DC_D1_OTG_VSTARTUP + adev->mode_info.num_crtc - 1;
1355 i++) {
1356 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DCE, i, &adev->crtc_irq);
1357
1358 if (r) {
1359 DRM_ERROR("Failed to add crtc irq id!\n");
1360 return r;
1361 }
1362
1363 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
1364 int_params.irq_source =
1365 dc_interrupt_to_irq_source(dc, i, 0);
1366
1367 c_irq_params = &adev->dm.vblank_params[int_params.irq_source - DC_IRQ_SOURCE_VBLANK1];
1368
1369 c_irq_params->adev = adev;
1370 c_irq_params->irq_src = int_params.irq_source;
1371
1372 amdgpu_dm_irq_register_interrupt(adev, &int_params,
1373 dm_crtc_high_irq, c_irq_params);
1374 }
1375
1376 /* Use GRPH_PFLIP interrupt */
1377 for (i = DCN_1_0__SRCID__HUBP0_FLIP_INTERRUPT;
1378 i <= DCN_1_0__SRCID__HUBP0_FLIP_INTERRUPT + adev->mode_info.num_crtc - 1;
1379 i++) {
1380 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DCE, i, &adev->pageflip_irq);
1381 if (r) {
1382 DRM_ERROR("Failed to add page flip irq id!\n");
1383 return r;
1384 }
1385
1386 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
1387 int_params.irq_source =
1388 dc_interrupt_to_irq_source(dc, i, 0);
1389
1390 c_irq_params = &adev->dm.pflip_params[int_params.irq_source - DC_IRQ_SOURCE_PFLIP_FIRST];
1391
1392 c_irq_params->adev = adev;
1393 c_irq_params->irq_src = int_params.irq_source;
1394
1395 amdgpu_dm_irq_register_interrupt(adev, &int_params,
1396 dm_pflip_high_irq, c_irq_params);
1397
1398 }
1399
1400 /* HPD */
1401 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DCE, DCN_1_0__SRCID__DC_HPD1_INT,
1402 &adev->hpd_irq);
1403 if (r) {
1404 DRM_ERROR("Failed to add hpd irq id!\n");
1405 return r;
1406 }
1407
1408 register_hpd_handlers(adev);
1409
1410 return 0;
1411 }
1412 #endif
1413
amdgpu_dm_mode_config_init(struct amdgpu_device * adev)1414 static int amdgpu_dm_mode_config_init(struct amdgpu_device *adev)
1415 {
1416 int r;
1417
1418 adev->mode_info.mode_config_initialized = true;
1419
1420 adev->ddev->mode_config.funcs = (void *)&amdgpu_dm_mode_funcs;
1421 adev->ddev->mode_config.helper_private = &amdgpu_dm_mode_config_helperfuncs;
1422
1423 adev->ddev->mode_config.max_width = 16384;
1424 adev->ddev->mode_config.max_height = 16384;
1425
1426 adev->ddev->mode_config.preferred_depth = 24;
1427 adev->ddev->mode_config.prefer_shadow = 1;
1428 /* indicate support of immediate flip */
1429 adev->ddev->mode_config.async_page_flip = true;
1430
1431 adev->ddev->mode_config.fb_base = adev->gmc.aper_base;
1432
1433 r = amdgpu_display_modeset_create_props(adev);
1434 if (r)
1435 return r;
1436
1437 return 0;
1438 }
1439
1440 #if defined(CONFIG_BACKLIGHT_CLASS_DEVICE) ||\
1441 defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE)
1442
amdgpu_dm_backlight_update_status(struct backlight_device * bd)1443 static int amdgpu_dm_backlight_update_status(struct backlight_device *bd)
1444 {
1445 struct amdgpu_display_manager *dm = bl_get_data(bd);
1446
1447 if (dc_link_set_backlight_level(dm->backlight_link,
1448 bd->props.brightness, 0, 0))
1449 return 0;
1450 else
1451 return 1;
1452 }
1453
amdgpu_dm_backlight_get_brightness(struct backlight_device * bd)1454 static int amdgpu_dm_backlight_get_brightness(struct backlight_device *bd)
1455 {
1456 struct amdgpu_display_manager *dm = bl_get_data(bd);
1457 int ret = dc_link_get_backlight_level(dm->backlight_link);
1458
1459 if (ret == DC_ERROR_UNEXPECTED)
1460 return bd->props.brightness;
1461 return ret;
1462 }
1463
1464 static const struct backlight_ops amdgpu_dm_backlight_ops = {
1465 .options = BL_CORE_SUSPENDRESUME,
1466 .get_brightness = amdgpu_dm_backlight_get_brightness,
1467 .update_status = amdgpu_dm_backlight_update_status,
1468 };
1469
1470 static void
amdgpu_dm_register_backlight_device(struct amdgpu_display_manager * dm)1471 amdgpu_dm_register_backlight_device(struct amdgpu_display_manager *dm)
1472 {
1473 char bl_name[16];
1474 struct backlight_properties props = { 0 };
1475
1476 props.max_brightness = AMDGPU_MAX_BL_LEVEL;
1477 props.brightness = AMDGPU_MAX_BL_LEVEL;
1478 props.type = BACKLIGHT_RAW;
1479
1480 snprintf(bl_name, sizeof(bl_name), "amdgpu_bl%d",
1481 dm->adev->ddev->primary->index);
1482
1483 dm->backlight_dev = backlight_device_register(bl_name,
1484 dm->adev->ddev->dev,
1485 dm,
1486 &amdgpu_dm_backlight_ops,
1487 &props);
1488
1489 if (IS_ERR(dm->backlight_dev))
1490 DRM_ERROR("DM: Backlight registration failed!\n");
1491 else
1492 DRM_DEBUG_DRIVER("DM: Registered Backlight device: %s\n", bl_name);
1493 }
1494
1495 #endif
1496
initialize_plane(struct amdgpu_display_manager * dm,struct amdgpu_mode_info * mode_info,int plane_id)1497 static int initialize_plane(struct amdgpu_display_manager *dm,
1498 struct amdgpu_mode_info *mode_info,
1499 int plane_id)
1500 {
1501 struct amdgpu_plane *plane;
1502 unsigned long possible_crtcs;
1503 int ret = 0;
1504
1505 plane = kzalloc(sizeof(struct amdgpu_plane), GFP_KERNEL);
1506 mode_info->planes[plane_id] = plane;
1507
1508 if (!plane) {
1509 DRM_ERROR("KMS: Failed to allocate plane\n");
1510 return -ENOMEM;
1511 }
1512 plane->base.type = mode_info->plane_type[plane_id];
1513
1514 /*
1515 * HACK: IGT tests expect that each plane can only have one
1516 * one possible CRTC. For now, set one CRTC for each
1517 * plane that is not an underlay, but still allow multiple
1518 * CRTCs for underlay planes.
1519 */
1520 possible_crtcs = 1 << plane_id;
1521 if (plane_id >= dm->dc->caps.max_streams)
1522 possible_crtcs = 0xff;
1523
1524 ret = amdgpu_dm_plane_init(dm, mode_info->planes[plane_id], possible_crtcs);
1525
1526 if (ret) {
1527 DRM_ERROR("KMS: Failed to initialize plane\n");
1528 return ret;
1529 }
1530
1531 return ret;
1532 }
1533
1534
register_backlight_device(struct amdgpu_display_manager * dm,struct dc_link * link)1535 static void register_backlight_device(struct amdgpu_display_manager *dm,
1536 struct dc_link *link)
1537 {
1538 #if defined(CONFIG_BACKLIGHT_CLASS_DEVICE) ||\
1539 defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE)
1540
1541 if ((link->connector_signal & (SIGNAL_TYPE_EDP | SIGNAL_TYPE_LVDS)) &&
1542 link->type != dc_connection_none) {
1543 /* Event if registration failed, we should continue with
1544 * DM initialization because not having a backlight control
1545 * is better then a black screen.
1546 */
1547 amdgpu_dm_register_backlight_device(dm);
1548
1549 if (dm->backlight_dev)
1550 dm->backlight_link = link;
1551 }
1552 #endif
1553 }
1554
1555
1556 /* In this architecture, the association
1557 * connector -> encoder -> crtc
1558 * id not really requried. The crtc and connector will hold the
1559 * display_index as an abstraction to use with DAL component
1560 *
1561 * Returns 0 on success
1562 */
amdgpu_dm_initialize_drm_device(struct amdgpu_device * adev)1563 static int amdgpu_dm_initialize_drm_device(struct amdgpu_device *adev)
1564 {
1565 struct amdgpu_display_manager *dm = &adev->dm;
1566 int32_t i;
1567 struct amdgpu_dm_connector *aconnector = NULL;
1568 struct amdgpu_encoder *aencoder = NULL;
1569 struct amdgpu_mode_info *mode_info = &adev->mode_info;
1570 uint32_t link_cnt;
1571 int32_t total_overlay_planes, total_primary_planes;
1572 enum dc_connection_type new_connection_type = dc_connection_none;
1573
1574 link_cnt = dm->dc->caps.max_links;
1575 if (amdgpu_dm_mode_config_init(dm->adev)) {
1576 DRM_ERROR("DM: Failed to initialize mode config\n");
1577 return -1;
1578 }
1579
1580 /* Identify the number of planes to be initialized */
1581 total_overlay_planes = dm->dc->caps.max_slave_planes;
1582 total_primary_planes = dm->dc->caps.max_planes - dm->dc->caps.max_slave_planes;
1583
1584 /* First initialize overlay planes, index starting after primary planes */
1585 for (i = (total_overlay_planes - 1); i >= 0; i--) {
1586 if (initialize_plane(dm, mode_info, (total_primary_planes + i))) {
1587 DRM_ERROR("KMS: Failed to initialize overlay plane\n");
1588 goto fail;
1589 }
1590 }
1591
1592 /* Initialize primary planes */
1593 for (i = (total_primary_planes - 1); i >= 0; i--) {
1594 if (initialize_plane(dm, mode_info, i)) {
1595 DRM_ERROR("KMS: Failed to initialize primary plane\n");
1596 goto fail;
1597 }
1598 }
1599
1600 for (i = 0; i < dm->dc->caps.max_streams; i++)
1601 if (amdgpu_dm_crtc_init(dm, &mode_info->planes[i]->base, i)) {
1602 DRM_ERROR("KMS: Failed to initialize crtc\n");
1603 goto fail;
1604 }
1605
1606 dm->display_indexes_num = dm->dc->caps.max_streams;
1607
1608 /* loops over all connectors on the board */
1609 for (i = 0; i < link_cnt; i++) {
1610 struct dc_link *link = NULL;
1611
1612 if (i > AMDGPU_DM_MAX_DISPLAY_INDEX) {
1613 DRM_ERROR(
1614 "KMS: Cannot support more than %d display indexes\n",
1615 AMDGPU_DM_MAX_DISPLAY_INDEX);
1616 continue;
1617 }
1618
1619 aconnector = kzalloc(sizeof(*aconnector), GFP_KERNEL);
1620 if (!aconnector)
1621 goto fail;
1622
1623 aencoder = kzalloc(sizeof(*aencoder), GFP_KERNEL);
1624 if (!aencoder)
1625 goto fail;
1626
1627 if (amdgpu_dm_encoder_init(dm->ddev, aencoder, i)) {
1628 DRM_ERROR("KMS: Failed to initialize encoder\n");
1629 goto fail;
1630 }
1631
1632 if (amdgpu_dm_connector_init(dm, aconnector, i, aencoder)) {
1633 DRM_ERROR("KMS: Failed to initialize connector\n");
1634 goto fail;
1635 }
1636
1637 link = dc_get_link_at_index(dm->dc, i);
1638
1639 if (!dc_link_detect_sink(link, &new_connection_type))
1640 DRM_ERROR("KMS: Failed to detect connector\n");
1641
1642 if (aconnector->base.force && new_connection_type == dc_connection_none) {
1643 emulated_link_detect(link);
1644 amdgpu_dm_update_connector_after_detect(aconnector);
1645
1646 } else if (dc_link_detect(link, DETECT_REASON_BOOT)) {
1647 amdgpu_dm_update_connector_after_detect(aconnector);
1648 register_backlight_device(dm, link);
1649 }
1650
1651
1652 }
1653
1654 /* Software is initialized. Now we can register interrupt handlers. */
1655 switch (adev->asic_type) {
1656 case CHIP_BONAIRE:
1657 case CHIP_HAWAII:
1658 case CHIP_KAVERI:
1659 case CHIP_KABINI:
1660 case CHIP_MULLINS:
1661 case CHIP_TONGA:
1662 case CHIP_FIJI:
1663 case CHIP_CARRIZO:
1664 case CHIP_STONEY:
1665 case CHIP_POLARIS11:
1666 case CHIP_POLARIS10:
1667 case CHIP_POLARIS12:
1668 case CHIP_VEGAM:
1669 case CHIP_VEGA10:
1670 case CHIP_VEGA12:
1671 case CHIP_VEGA20:
1672 if (dce110_register_irq_handlers(dm->adev)) {
1673 DRM_ERROR("DM: Failed to initialize IRQ\n");
1674 goto fail;
1675 }
1676 break;
1677 #if defined(CONFIG_DRM_AMD_DC_DCN1_0)
1678 case CHIP_RAVEN:
1679 if (dcn10_register_irq_handlers(dm->adev)) {
1680 DRM_ERROR("DM: Failed to initialize IRQ\n");
1681 goto fail;
1682 }
1683 break;
1684 #endif
1685 default:
1686 DRM_ERROR("Unsupported ASIC type: 0x%X\n", adev->asic_type);
1687 goto fail;
1688 }
1689
1690 if (adev->asic_type != CHIP_CARRIZO && adev->asic_type != CHIP_STONEY)
1691 dm->dc->debug.disable_stutter = amdgpu_pp_feature_mask & PP_STUTTER_MODE ? false : true;
1692
1693 return 0;
1694 fail:
1695 kfree(aencoder);
1696 kfree(aconnector);
1697 for (i = 0; i < dm->dc->caps.max_planes; i++)
1698 kfree(mode_info->planes[i]);
1699 return -1;
1700 }
1701
amdgpu_dm_destroy_drm_device(struct amdgpu_display_manager * dm)1702 static void amdgpu_dm_destroy_drm_device(struct amdgpu_display_manager *dm)
1703 {
1704 drm_mode_config_cleanup(dm->ddev);
1705 return;
1706 }
1707
1708 /******************************************************************************
1709 * amdgpu_display_funcs functions
1710 *****************************************************************************/
1711
1712 /**
1713 * dm_bandwidth_update - program display watermarks
1714 *
1715 * @adev: amdgpu_device pointer
1716 *
1717 * Calculate and program the display watermarks and line buffer allocation.
1718 */
dm_bandwidth_update(struct amdgpu_device * adev)1719 static void dm_bandwidth_update(struct amdgpu_device *adev)
1720 {
1721 /* TODO: implement later */
1722 }
1723
amdgpu_notify_freesync(struct drm_device * dev,void * data,struct drm_file * filp)1724 static int amdgpu_notify_freesync(struct drm_device *dev, void *data,
1725 struct drm_file *filp)
1726 {
1727 struct mod_freesync_params freesync_params;
1728 uint8_t num_streams;
1729 uint8_t i;
1730
1731 struct amdgpu_device *adev = dev->dev_private;
1732 int r = 0;
1733
1734 /* Get freesync enable flag from DRM */
1735
1736 num_streams = dc_get_current_stream_count(adev->dm.dc);
1737
1738 for (i = 0; i < num_streams; i++) {
1739 struct dc_stream_state *stream;
1740 stream = dc_get_stream_at_index(adev->dm.dc, i);
1741
1742 mod_freesync_update_state(adev->dm.freesync_module,
1743 &stream, 1, &freesync_params);
1744 }
1745
1746 return r;
1747 }
1748
1749 static const struct amdgpu_display_funcs dm_display_funcs = {
1750 .bandwidth_update = dm_bandwidth_update, /* called unconditionally */
1751 .vblank_get_counter = dm_vblank_get_counter,/* called unconditionally */
1752 .backlight_set_level = NULL, /* never called for DC */
1753 .backlight_get_level = NULL, /* never called for DC */
1754 .hpd_sense = NULL,/* called unconditionally */
1755 .hpd_set_polarity = NULL, /* called unconditionally */
1756 .hpd_get_gpio_reg = NULL, /* VBIOS parsing. DAL does it. */
1757 .page_flip_get_scanoutpos =
1758 dm_crtc_get_scanoutpos,/* called unconditionally */
1759 .add_encoder = NULL, /* VBIOS parsing. DAL does it. */
1760 .add_connector = NULL, /* VBIOS parsing. DAL does it. */
1761 .notify_freesync = amdgpu_notify_freesync,
1762
1763 };
1764
1765 #if defined(CONFIG_DEBUG_KERNEL_DC)
1766
s3_debug_store(struct device * device,struct device_attribute * attr,const char * buf,size_t count)1767 static ssize_t s3_debug_store(struct device *device,
1768 struct device_attribute *attr,
1769 const char *buf,
1770 size_t count)
1771 {
1772 int ret;
1773 int s3_state;
1774 struct pci_dev *pdev = to_pci_dev(device);
1775 struct drm_device *drm_dev = pci_get_drvdata(pdev);
1776 struct amdgpu_device *adev = drm_dev->dev_private;
1777
1778 ret = kstrtoint(buf, 0, &s3_state);
1779
1780 if (ret == 0) {
1781 if (s3_state) {
1782 dm_resume(adev);
1783 drm_kms_helper_hotplug_event(adev->ddev);
1784 } else
1785 dm_suspend(adev);
1786 }
1787
1788 return ret == 0 ? count : 0;
1789 }
1790
1791 DEVICE_ATTR_WO(s3_debug);
1792
1793 #endif
1794
dm_early_init(void * handle)1795 static int dm_early_init(void *handle)
1796 {
1797 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1798
1799 switch (adev->asic_type) {
1800 case CHIP_BONAIRE:
1801 case CHIP_HAWAII:
1802 adev->mode_info.num_crtc = 6;
1803 adev->mode_info.num_hpd = 6;
1804 adev->mode_info.num_dig = 6;
1805 adev->mode_info.plane_type = dm_plane_type_default;
1806 break;
1807 case CHIP_KAVERI:
1808 adev->mode_info.num_crtc = 4;
1809 adev->mode_info.num_hpd = 6;
1810 adev->mode_info.num_dig = 7;
1811 adev->mode_info.plane_type = dm_plane_type_default;
1812 break;
1813 case CHIP_KABINI:
1814 case CHIP_MULLINS:
1815 adev->mode_info.num_crtc = 2;
1816 adev->mode_info.num_hpd = 6;
1817 adev->mode_info.num_dig = 6;
1818 adev->mode_info.plane_type = dm_plane_type_default;
1819 break;
1820 case CHIP_FIJI:
1821 case CHIP_TONGA:
1822 adev->mode_info.num_crtc = 6;
1823 adev->mode_info.num_hpd = 6;
1824 adev->mode_info.num_dig = 7;
1825 adev->mode_info.plane_type = dm_plane_type_default;
1826 break;
1827 case CHIP_CARRIZO:
1828 adev->mode_info.num_crtc = 3;
1829 adev->mode_info.num_hpd = 6;
1830 adev->mode_info.num_dig = 9;
1831 adev->mode_info.plane_type = dm_plane_type_carizzo;
1832 break;
1833 case CHIP_STONEY:
1834 adev->mode_info.num_crtc = 2;
1835 adev->mode_info.num_hpd = 6;
1836 adev->mode_info.num_dig = 9;
1837 adev->mode_info.plane_type = dm_plane_type_stoney;
1838 break;
1839 case CHIP_POLARIS11:
1840 case CHIP_POLARIS12:
1841 adev->mode_info.num_crtc = 5;
1842 adev->mode_info.num_hpd = 5;
1843 adev->mode_info.num_dig = 5;
1844 adev->mode_info.plane_type = dm_plane_type_default;
1845 break;
1846 case CHIP_POLARIS10:
1847 case CHIP_VEGAM:
1848 adev->mode_info.num_crtc = 6;
1849 adev->mode_info.num_hpd = 6;
1850 adev->mode_info.num_dig = 6;
1851 adev->mode_info.plane_type = dm_plane_type_default;
1852 break;
1853 case CHIP_VEGA10:
1854 case CHIP_VEGA12:
1855 case CHIP_VEGA20:
1856 adev->mode_info.num_crtc = 6;
1857 adev->mode_info.num_hpd = 6;
1858 adev->mode_info.num_dig = 6;
1859 adev->mode_info.plane_type = dm_plane_type_default;
1860 break;
1861 #if defined(CONFIG_DRM_AMD_DC_DCN1_0)
1862 case CHIP_RAVEN:
1863 adev->mode_info.num_crtc = 4;
1864 adev->mode_info.num_hpd = 4;
1865 adev->mode_info.num_dig = 4;
1866 adev->mode_info.plane_type = dm_plane_type_default;
1867 break;
1868 #endif
1869 default:
1870 DRM_ERROR("Unsupported ASIC type: 0x%X\n", adev->asic_type);
1871 return -EINVAL;
1872 }
1873
1874 amdgpu_dm_set_irq_funcs(adev);
1875
1876 if (adev->mode_info.funcs == NULL)
1877 adev->mode_info.funcs = &dm_display_funcs;
1878
1879 /* Note: Do NOT change adev->audio_endpt_rreg and
1880 * adev->audio_endpt_wreg because they are initialised in
1881 * amdgpu_device_init() */
1882 #if defined(CONFIG_DEBUG_KERNEL_DC)
1883 device_create_file(
1884 adev->ddev->dev,
1885 &dev_attr_s3_debug);
1886 #endif
1887
1888 return 0;
1889 }
1890
modeset_required(struct drm_crtc_state * crtc_state,struct dc_stream_state * new_stream,struct dc_stream_state * old_stream)1891 static bool modeset_required(struct drm_crtc_state *crtc_state,
1892 struct dc_stream_state *new_stream,
1893 struct dc_stream_state *old_stream)
1894 {
1895 if (!drm_atomic_crtc_needs_modeset(crtc_state))
1896 return false;
1897
1898 if (!crtc_state->enable)
1899 return false;
1900
1901 return crtc_state->active;
1902 }
1903
modereset_required(struct drm_crtc_state * crtc_state)1904 static bool modereset_required(struct drm_crtc_state *crtc_state)
1905 {
1906 if (!drm_atomic_crtc_needs_modeset(crtc_state))
1907 return false;
1908
1909 return !crtc_state->enable || !crtc_state->active;
1910 }
1911
amdgpu_dm_encoder_destroy(struct drm_encoder * encoder)1912 static void amdgpu_dm_encoder_destroy(struct drm_encoder *encoder)
1913 {
1914 drm_encoder_cleanup(encoder);
1915 kfree(encoder);
1916 }
1917
1918 static const struct drm_encoder_funcs amdgpu_dm_encoder_funcs = {
1919 .destroy = amdgpu_dm_encoder_destroy,
1920 };
1921
fill_rects_from_plane_state(const struct drm_plane_state * state,struct dc_plane_state * plane_state)1922 static bool fill_rects_from_plane_state(const struct drm_plane_state *state,
1923 struct dc_plane_state *plane_state)
1924 {
1925 plane_state->src_rect.x = state->src_x >> 16;
1926 plane_state->src_rect.y = state->src_y >> 16;
1927 /*we ignore for now mantissa and do not to deal with floating pixels :(*/
1928 plane_state->src_rect.width = state->src_w >> 16;
1929
1930 if (plane_state->src_rect.width == 0)
1931 return false;
1932
1933 plane_state->src_rect.height = state->src_h >> 16;
1934 if (plane_state->src_rect.height == 0)
1935 return false;
1936
1937 plane_state->dst_rect.x = state->crtc_x;
1938 plane_state->dst_rect.y = state->crtc_y;
1939
1940 if (state->crtc_w == 0)
1941 return false;
1942
1943 plane_state->dst_rect.width = state->crtc_w;
1944
1945 if (state->crtc_h == 0)
1946 return false;
1947
1948 plane_state->dst_rect.height = state->crtc_h;
1949
1950 plane_state->clip_rect = plane_state->dst_rect;
1951
1952 switch (state->rotation & DRM_MODE_ROTATE_MASK) {
1953 case DRM_MODE_ROTATE_0:
1954 plane_state->rotation = ROTATION_ANGLE_0;
1955 break;
1956 case DRM_MODE_ROTATE_90:
1957 plane_state->rotation = ROTATION_ANGLE_90;
1958 break;
1959 case DRM_MODE_ROTATE_180:
1960 plane_state->rotation = ROTATION_ANGLE_180;
1961 break;
1962 case DRM_MODE_ROTATE_270:
1963 plane_state->rotation = ROTATION_ANGLE_270;
1964 break;
1965 default:
1966 plane_state->rotation = ROTATION_ANGLE_0;
1967 break;
1968 }
1969
1970 return true;
1971 }
get_fb_info(const struct amdgpu_framebuffer * amdgpu_fb,uint64_t * tiling_flags)1972 static int get_fb_info(const struct amdgpu_framebuffer *amdgpu_fb,
1973 uint64_t *tiling_flags)
1974 {
1975 struct amdgpu_bo *rbo = gem_to_amdgpu_bo(amdgpu_fb->base.obj[0]);
1976 int r = amdgpu_bo_reserve(rbo, false);
1977
1978 if (unlikely(r)) {
1979 // Don't show error msg. when return -ERESTARTSYS
1980 if (r != -ERESTARTSYS)
1981 DRM_ERROR("Unable to reserve buffer: %d\n", r);
1982 return r;
1983 }
1984
1985 if (tiling_flags)
1986 amdgpu_bo_get_tiling_flags(rbo, tiling_flags);
1987
1988 amdgpu_bo_unreserve(rbo);
1989
1990 return r;
1991 }
1992
fill_plane_attributes_from_fb(struct amdgpu_device * adev,struct dc_plane_state * plane_state,const struct amdgpu_framebuffer * amdgpu_fb)1993 static int fill_plane_attributes_from_fb(struct amdgpu_device *adev,
1994 struct dc_plane_state *plane_state,
1995 const struct amdgpu_framebuffer *amdgpu_fb)
1996 {
1997 uint64_t tiling_flags;
1998 unsigned int awidth;
1999 const struct drm_framebuffer *fb = &amdgpu_fb->base;
2000 int ret = 0;
2001 struct drm_format_name_buf format_name;
2002
2003 ret = get_fb_info(
2004 amdgpu_fb,
2005 &tiling_flags);
2006
2007 if (ret)
2008 return ret;
2009
2010 switch (fb->format->format) {
2011 case DRM_FORMAT_C8:
2012 plane_state->format = SURFACE_PIXEL_FORMAT_GRPH_PALETA_256_COLORS;
2013 break;
2014 case DRM_FORMAT_RGB565:
2015 plane_state->format = SURFACE_PIXEL_FORMAT_GRPH_RGB565;
2016 break;
2017 case DRM_FORMAT_XRGB8888:
2018 case DRM_FORMAT_ARGB8888:
2019 plane_state->format = SURFACE_PIXEL_FORMAT_GRPH_ARGB8888;
2020 break;
2021 case DRM_FORMAT_XRGB2101010:
2022 case DRM_FORMAT_ARGB2101010:
2023 plane_state->format = SURFACE_PIXEL_FORMAT_GRPH_ARGB2101010;
2024 break;
2025 case DRM_FORMAT_XBGR2101010:
2026 case DRM_FORMAT_ABGR2101010:
2027 plane_state->format = SURFACE_PIXEL_FORMAT_GRPH_ABGR2101010;
2028 break;
2029 case DRM_FORMAT_NV21:
2030 plane_state->format = SURFACE_PIXEL_FORMAT_VIDEO_420_YCbCr;
2031 break;
2032 case DRM_FORMAT_NV12:
2033 plane_state->format = SURFACE_PIXEL_FORMAT_VIDEO_420_YCrCb;
2034 break;
2035 default:
2036 DRM_ERROR("Unsupported screen format %s\n",
2037 drm_get_format_name(fb->format->format, &format_name));
2038 return -EINVAL;
2039 }
2040
2041 if (plane_state->format < SURFACE_PIXEL_FORMAT_VIDEO_BEGIN) {
2042 plane_state->address.type = PLN_ADDR_TYPE_GRAPHICS;
2043 plane_state->plane_size.grph.surface_size.x = 0;
2044 plane_state->plane_size.grph.surface_size.y = 0;
2045 plane_state->plane_size.grph.surface_size.width = fb->width;
2046 plane_state->plane_size.grph.surface_size.height = fb->height;
2047 plane_state->plane_size.grph.surface_pitch =
2048 fb->pitches[0] / fb->format->cpp[0];
2049 /* TODO: unhardcode */
2050 plane_state->color_space = COLOR_SPACE_SRGB;
2051
2052 } else {
2053 awidth = ALIGN(fb->width, 64);
2054 plane_state->address.type = PLN_ADDR_TYPE_VIDEO_PROGRESSIVE;
2055 plane_state->plane_size.video.luma_size.x = 0;
2056 plane_state->plane_size.video.luma_size.y = 0;
2057 plane_state->plane_size.video.luma_size.width = awidth;
2058 plane_state->plane_size.video.luma_size.height = fb->height;
2059 /* TODO: unhardcode */
2060 plane_state->plane_size.video.luma_pitch = awidth;
2061
2062 plane_state->plane_size.video.chroma_size.x = 0;
2063 plane_state->plane_size.video.chroma_size.y = 0;
2064 plane_state->plane_size.video.chroma_size.width = awidth;
2065 plane_state->plane_size.video.chroma_size.height = fb->height;
2066 plane_state->plane_size.video.chroma_pitch = awidth / 2;
2067
2068 /* TODO: unhardcode */
2069 plane_state->color_space = COLOR_SPACE_YCBCR709;
2070 }
2071
2072 memset(&plane_state->tiling_info, 0, sizeof(plane_state->tiling_info));
2073
2074 /* Fill GFX8 params */
2075 if (AMDGPU_TILING_GET(tiling_flags, ARRAY_MODE) == DC_ARRAY_2D_TILED_THIN1) {
2076 unsigned int bankw, bankh, mtaspect, tile_split, num_banks;
2077
2078 bankw = AMDGPU_TILING_GET(tiling_flags, BANK_WIDTH);
2079 bankh = AMDGPU_TILING_GET(tiling_flags, BANK_HEIGHT);
2080 mtaspect = AMDGPU_TILING_GET(tiling_flags, MACRO_TILE_ASPECT);
2081 tile_split = AMDGPU_TILING_GET(tiling_flags, TILE_SPLIT);
2082 num_banks = AMDGPU_TILING_GET(tiling_flags, NUM_BANKS);
2083
2084 /* XXX fix me for VI */
2085 plane_state->tiling_info.gfx8.num_banks = num_banks;
2086 plane_state->tiling_info.gfx8.array_mode =
2087 DC_ARRAY_2D_TILED_THIN1;
2088 plane_state->tiling_info.gfx8.tile_split = tile_split;
2089 plane_state->tiling_info.gfx8.bank_width = bankw;
2090 plane_state->tiling_info.gfx8.bank_height = bankh;
2091 plane_state->tiling_info.gfx8.tile_aspect = mtaspect;
2092 plane_state->tiling_info.gfx8.tile_mode =
2093 DC_ADDR_SURF_MICRO_TILING_DISPLAY;
2094 } else if (AMDGPU_TILING_GET(tiling_flags, ARRAY_MODE)
2095 == DC_ARRAY_1D_TILED_THIN1) {
2096 plane_state->tiling_info.gfx8.array_mode = DC_ARRAY_1D_TILED_THIN1;
2097 }
2098
2099 plane_state->tiling_info.gfx8.pipe_config =
2100 AMDGPU_TILING_GET(tiling_flags, PIPE_CONFIG);
2101
2102 if (adev->asic_type == CHIP_VEGA10 ||
2103 adev->asic_type == CHIP_VEGA12 ||
2104 adev->asic_type == CHIP_VEGA20 ||
2105 adev->asic_type == CHIP_RAVEN) {
2106 /* Fill GFX9 params */
2107 plane_state->tiling_info.gfx9.num_pipes =
2108 adev->gfx.config.gb_addr_config_fields.num_pipes;
2109 plane_state->tiling_info.gfx9.num_banks =
2110 adev->gfx.config.gb_addr_config_fields.num_banks;
2111 plane_state->tiling_info.gfx9.pipe_interleave =
2112 adev->gfx.config.gb_addr_config_fields.pipe_interleave_size;
2113 plane_state->tiling_info.gfx9.num_shader_engines =
2114 adev->gfx.config.gb_addr_config_fields.num_se;
2115 plane_state->tiling_info.gfx9.max_compressed_frags =
2116 adev->gfx.config.gb_addr_config_fields.max_compress_frags;
2117 plane_state->tiling_info.gfx9.num_rb_per_se =
2118 adev->gfx.config.gb_addr_config_fields.num_rb_per_se;
2119 plane_state->tiling_info.gfx9.swizzle =
2120 AMDGPU_TILING_GET(tiling_flags, SWIZZLE_MODE);
2121 plane_state->tiling_info.gfx9.shaderEnable = 1;
2122 }
2123
2124 plane_state->visible = true;
2125 plane_state->scaling_quality.h_taps_c = 0;
2126 plane_state->scaling_quality.v_taps_c = 0;
2127
2128 /* is this needed? is plane_state zeroed at allocation? */
2129 plane_state->scaling_quality.h_taps = 0;
2130 plane_state->scaling_quality.v_taps = 0;
2131 plane_state->stereo_format = PLANE_STEREO_FORMAT_NONE;
2132
2133 return ret;
2134
2135 }
2136
fill_plane_attributes(struct amdgpu_device * adev,struct dc_plane_state * dc_plane_state,struct drm_plane_state * plane_state,struct drm_crtc_state * crtc_state)2137 static int fill_plane_attributes(struct amdgpu_device *adev,
2138 struct dc_plane_state *dc_plane_state,
2139 struct drm_plane_state *plane_state,
2140 struct drm_crtc_state *crtc_state)
2141 {
2142 const struct amdgpu_framebuffer *amdgpu_fb =
2143 to_amdgpu_framebuffer(plane_state->fb);
2144 const struct drm_crtc *crtc = plane_state->crtc;
2145 int ret = 0;
2146
2147 if (!fill_rects_from_plane_state(plane_state, dc_plane_state))
2148 return -EINVAL;
2149
2150 ret = fill_plane_attributes_from_fb(
2151 crtc->dev->dev_private,
2152 dc_plane_state,
2153 amdgpu_fb);
2154
2155 if (ret)
2156 return ret;
2157
2158 /*
2159 * Always set input transfer function, since plane state is refreshed
2160 * every time.
2161 */
2162 ret = amdgpu_dm_set_degamma_lut(crtc_state, dc_plane_state);
2163 if (ret) {
2164 dc_transfer_func_release(dc_plane_state->in_transfer_func);
2165 dc_plane_state->in_transfer_func = NULL;
2166 }
2167
2168 return ret;
2169 }
2170
2171 /*****************************************************************************/
2172
update_stream_scaling_settings(const struct drm_display_mode * mode,const struct dm_connector_state * dm_state,struct dc_stream_state * stream)2173 static void update_stream_scaling_settings(const struct drm_display_mode *mode,
2174 const struct dm_connector_state *dm_state,
2175 struct dc_stream_state *stream)
2176 {
2177 enum amdgpu_rmx_type rmx_type;
2178
2179 struct rect src = { 0 }; /* viewport in composition space*/
2180 struct rect dst = { 0 }; /* stream addressable area */
2181
2182 /* no mode. nothing to be done */
2183 if (!mode)
2184 return;
2185
2186 /* Full screen scaling by default */
2187 src.width = mode->hdisplay;
2188 src.height = mode->vdisplay;
2189 dst.width = stream->timing.h_addressable;
2190 dst.height = stream->timing.v_addressable;
2191
2192 if (dm_state) {
2193 rmx_type = dm_state->scaling;
2194 if (rmx_type == RMX_ASPECT || rmx_type == RMX_OFF) {
2195 if (src.width * dst.height <
2196 src.height * dst.width) {
2197 /* height needs less upscaling/more downscaling */
2198 dst.width = src.width *
2199 dst.height / src.height;
2200 } else {
2201 /* width needs less upscaling/more downscaling */
2202 dst.height = src.height *
2203 dst.width / src.width;
2204 }
2205 } else if (rmx_type == RMX_CENTER) {
2206 dst = src;
2207 }
2208
2209 dst.x = (stream->timing.h_addressable - dst.width) / 2;
2210 dst.y = (stream->timing.v_addressable - dst.height) / 2;
2211
2212 if (dm_state->underscan_enable) {
2213 dst.x += dm_state->underscan_hborder / 2;
2214 dst.y += dm_state->underscan_vborder / 2;
2215 dst.width -= dm_state->underscan_hborder;
2216 dst.height -= dm_state->underscan_vborder;
2217 }
2218 }
2219
2220 stream->src = src;
2221 stream->dst = dst;
2222
2223 DRM_DEBUG_DRIVER("Destination Rectangle x:%d y:%d width:%d height:%d\n",
2224 dst.x, dst.y, dst.width, dst.height);
2225
2226 }
2227
2228 static enum dc_color_depth
convert_color_depth_from_display_info(const struct drm_connector * connector)2229 convert_color_depth_from_display_info(const struct drm_connector *connector)
2230 {
2231 struct dm_connector_state *dm_conn_state =
2232 to_dm_connector_state(connector->state);
2233 uint32_t bpc = connector->display_info.bpc;
2234
2235 /* TODO: Remove this when there's support for max_bpc in drm */
2236 if (dm_conn_state && bpc > dm_conn_state->max_bpc)
2237 /* Round down to nearest even number. */
2238 bpc = dm_conn_state->max_bpc - (dm_conn_state->max_bpc & 1);
2239
2240 switch (bpc) {
2241 case 0:
2242 /* Temporary Work around, DRM don't parse color depth for
2243 * EDID revision before 1.4
2244 * TODO: Fix edid parsing
2245 */
2246 return COLOR_DEPTH_888;
2247 case 6:
2248 return COLOR_DEPTH_666;
2249 case 8:
2250 return COLOR_DEPTH_888;
2251 case 10:
2252 return COLOR_DEPTH_101010;
2253 case 12:
2254 return COLOR_DEPTH_121212;
2255 case 14:
2256 return COLOR_DEPTH_141414;
2257 case 16:
2258 return COLOR_DEPTH_161616;
2259 default:
2260 return COLOR_DEPTH_UNDEFINED;
2261 }
2262 }
2263
2264 static enum dc_aspect_ratio
get_aspect_ratio(const struct drm_display_mode * mode_in)2265 get_aspect_ratio(const struct drm_display_mode *mode_in)
2266 {
2267 /* 1-1 mapping, since both enums follow the HDMI spec. */
2268 return (enum dc_aspect_ratio) mode_in->picture_aspect_ratio;
2269 }
2270
2271 static enum dc_color_space
get_output_color_space(const struct dc_crtc_timing * dc_crtc_timing)2272 get_output_color_space(const struct dc_crtc_timing *dc_crtc_timing)
2273 {
2274 enum dc_color_space color_space = COLOR_SPACE_SRGB;
2275
2276 switch (dc_crtc_timing->pixel_encoding) {
2277 case PIXEL_ENCODING_YCBCR422:
2278 case PIXEL_ENCODING_YCBCR444:
2279 case PIXEL_ENCODING_YCBCR420:
2280 {
2281 /*
2282 * 27030khz is the separation point between HDTV and SDTV
2283 * according to HDMI spec, we use YCbCr709 and YCbCr601
2284 * respectively
2285 */
2286 if (dc_crtc_timing->pix_clk_khz > 27030) {
2287 if (dc_crtc_timing->flags.Y_ONLY)
2288 color_space =
2289 COLOR_SPACE_YCBCR709_LIMITED;
2290 else
2291 color_space = COLOR_SPACE_YCBCR709;
2292 } else {
2293 if (dc_crtc_timing->flags.Y_ONLY)
2294 color_space =
2295 COLOR_SPACE_YCBCR601_LIMITED;
2296 else
2297 color_space = COLOR_SPACE_YCBCR601;
2298 }
2299
2300 }
2301 break;
2302 case PIXEL_ENCODING_RGB:
2303 color_space = COLOR_SPACE_SRGB;
2304 break;
2305
2306 default:
2307 WARN_ON(1);
2308 break;
2309 }
2310
2311 return color_space;
2312 }
2313
reduce_mode_colour_depth(struct dc_crtc_timing * timing_out)2314 static void reduce_mode_colour_depth(struct dc_crtc_timing *timing_out)
2315 {
2316 if (timing_out->display_color_depth <= COLOR_DEPTH_888)
2317 return;
2318
2319 timing_out->display_color_depth--;
2320 }
2321
adjust_colour_depth_from_display_info(struct dc_crtc_timing * timing_out,const struct drm_display_info * info)2322 static void adjust_colour_depth_from_display_info(struct dc_crtc_timing *timing_out,
2323 const struct drm_display_info *info)
2324 {
2325 int normalized_clk;
2326 if (timing_out->display_color_depth <= COLOR_DEPTH_888)
2327 return;
2328 do {
2329 normalized_clk = timing_out->pix_clk_khz;
2330 /* YCbCr 4:2:0 requires additional adjustment of 1/2 */
2331 if (timing_out->pixel_encoding == PIXEL_ENCODING_YCBCR420)
2332 normalized_clk /= 2;
2333 /* Adjusting pix clock following on HDMI spec based on colour depth */
2334 switch (timing_out->display_color_depth) {
2335 case COLOR_DEPTH_101010:
2336 normalized_clk = (normalized_clk * 30) / 24;
2337 break;
2338 case COLOR_DEPTH_121212:
2339 normalized_clk = (normalized_clk * 36) / 24;
2340 break;
2341 case COLOR_DEPTH_161616:
2342 normalized_clk = (normalized_clk * 48) / 24;
2343 break;
2344 default:
2345 return;
2346 }
2347 if (normalized_clk <= info->max_tmds_clock)
2348 return;
2349 reduce_mode_colour_depth(timing_out);
2350
2351 } while (timing_out->display_color_depth > COLOR_DEPTH_888);
2352
2353 }
2354 /*****************************************************************************/
2355
2356 static void
fill_stream_properties_from_drm_display_mode(struct dc_stream_state * stream,const struct drm_display_mode * mode_in,const struct drm_connector * connector)2357 fill_stream_properties_from_drm_display_mode(struct dc_stream_state *stream,
2358 const struct drm_display_mode *mode_in,
2359 const struct drm_connector *connector)
2360 {
2361 struct dc_crtc_timing *timing_out = &stream->timing;
2362 const struct drm_display_info *info = &connector->display_info;
2363
2364 memset(timing_out, 0, sizeof(struct dc_crtc_timing));
2365
2366 timing_out->h_border_left = 0;
2367 timing_out->h_border_right = 0;
2368 timing_out->v_border_top = 0;
2369 timing_out->v_border_bottom = 0;
2370 /* TODO: un-hardcode */
2371 if (drm_mode_is_420_only(info, mode_in)
2372 && stream->sink->sink_signal == SIGNAL_TYPE_HDMI_TYPE_A)
2373 timing_out->pixel_encoding = PIXEL_ENCODING_YCBCR420;
2374 else if ((connector->display_info.color_formats & DRM_COLOR_FORMAT_YCRCB444)
2375 && stream->sink->sink_signal == SIGNAL_TYPE_HDMI_TYPE_A)
2376 timing_out->pixel_encoding = PIXEL_ENCODING_YCBCR444;
2377 else
2378 timing_out->pixel_encoding = PIXEL_ENCODING_RGB;
2379
2380 timing_out->timing_3d_format = TIMING_3D_FORMAT_NONE;
2381 timing_out->display_color_depth = convert_color_depth_from_display_info(
2382 connector);
2383 timing_out->scan_type = SCANNING_TYPE_NODATA;
2384 timing_out->hdmi_vic = 0;
2385 timing_out->vic = drm_match_cea_mode(mode_in);
2386
2387 timing_out->h_addressable = mode_in->crtc_hdisplay;
2388 timing_out->h_total = mode_in->crtc_htotal;
2389 timing_out->h_sync_width =
2390 mode_in->crtc_hsync_end - mode_in->crtc_hsync_start;
2391 timing_out->h_front_porch =
2392 mode_in->crtc_hsync_start - mode_in->crtc_hdisplay;
2393 timing_out->v_total = mode_in->crtc_vtotal;
2394 timing_out->v_addressable = mode_in->crtc_vdisplay;
2395 timing_out->v_front_porch =
2396 mode_in->crtc_vsync_start - mode_in->crtc_vdisplay;
2397 timing_out->v_sync_width =
2398 mode_in->crtc_vsync_end - mode_in->crtc_vsync_start;
2399 timing_out->pix_clk_khz = mode_in->crtc_clock;
2400 timing_out->aspect_ratio = get_aspect_ratio(mode_in);
2401 if (mode_in->flags & DRM_MODE_FLAG_PHSYNC)
2402 timing_out->flags.HSYNC_POSITIVE_POLARITY = 1;
2403 if (mode_in->flags & DRM_MODE_FLAG_PVSYNC)
2404 timing_out->flags.VSYNC_POSITIVE_POLARITY = 1;
2405
2406 stream->output_color_space = get_output_color_space(timing_out);
2407
2408 stream->out_transfer_func->type = TF_TYPE_PREDEFINED;
2409 stream->out_transfer_func->tf = TRANSFER_FUNCTION_SRGB;
2410 if (stream->sink->sink_signal == SIGNAL_TYPE_HDMI_TYPE_A)
2411 adjust_colour_depth_from_display_info(timing_out, info);
2412 }
2413
fill_audio_info(struct audio_info * audio_info,const struct drm_connector * drm_connector,const struct dc_sink * dc_sink)2414 static void fill_audio_info(struct audio_info *audio_info,
2415 const struct drm_connector *drm_connector,
2416 const struct dc_sink *dc_sink)
2417 {
2418 int i = 0;
2419 int cea_revision = 0;
2420 const struct dc_edid_caps *edid_caps = &dc_sink->edid_caps;
2421
2422 audio_info->manufacture_id = edid_caps->manufacturer_id;
2423 audio_info->product_id = edid_caps->product_id;
2424
2425 cea_revision = drm_connector->display_info.cea_rev;
2426
2427 strncpy(audio_info->display_name,
2428 edid_caps->display_name,
2429 AUDIO_INFO_DISPLAY_NAME_SIZE_IN_CHARS - 1);
2430
2431 if (cea_revision >= 3) {
2432 audio_info->mode_count = edid_caps->audio_mode_count;
2433
2434 for (i = 0; i < audio_info->mode_count; ++i) {
2435 audio_info->modes[i].format_code =
2436 (enum audio_format_code)
2437 (edid_caps->audio_modes[i].format_code);
2438 audio_info->modes[i].channel_count =
2439 edid_caps->audio_modes[i].channel_count;
2440 audio_info->modes[i].sample_rates.all =
2441 edid_caps->audio_modes[i].sample_rate;
2442 audio_info->modes[i].sample_size =
2443 edid_caps->audio_modes[i].sample_size;
2444 }
2445 }
2446
2447 audio_info->flags.all = edid_caps->speaker_flags;
2448
2449 /* TODO: We only check for the progressive mode, check for interlace mode too */
2450 if (drm_connector->latency_present[0]) {
2451 audio_info->video_latency = drm_connector->video_latency[0];
2452 audio_info->audio_latency = drm_connector->audio_latency[0];
2453 }
2454
2455 /* TODO: For DP, video and audio latency should be calculated from DPCD caps */
2456
2457 }
2458
2459 static void
copy_crtc_timing_for_drm_display_mode(const struct drm_display_mode * src_mode,struct drm_display_mode * dst_mode)2460 copy_crtc_timing_for_drm_display_mode(const struct drm_display_mode *src_mode,
2461 struct drm_display_mode *dst_mode)
2462 {
2463 dst_mode->crtc_hdisplay = src_mode->crtc_hdisplay;
2464 dst_mode->crtc_vdisplay = src_mode->crtc_vdisplay;
2465 dst_mode->crtc_clock = src_mode->crtc_clock;
2466 dst_mode->crtc_hblank_start = src_mode->crtc_hblank_start;
2467 dst_mode->crtc_hblank_end = src_mode->crtc_hblank_end;
2468 dst_mode->crtc_hsync_start = src_mode->crtc_hsync_start;
2469 dst_mode->crtc_hsync_end = src_mode->crtc_hsync_end;
2470 dst_mode->crtc_htotal = src_mode->crtc_htotal;
2471 dst_mode->crtc_hskew = src_mode->crtc_hskew;
2472 dst_mode->crtc_vblank_start = src_mode->crtc_vblank_start;
2473 dst_mode->crtc_vblank_end = src_mode->crtc_vblank_end;
2474 dst_mode->crtc_vsync_start = src_mode->crtc_vsync_start;
2475 dst_mode->crtc_vsync_end = src_mode->crtc_vsync_end;
2476 dst_mode->crtc_vtotal = src_mode->crtc_vtotal;
2477 }
2478
2479 static void
decide_crtc_timing_for_drm_display_mode(struct drm_display_mode * drm_mode,const struct drm_display_mode * native_mode,bool scale_enabled)2480 decide_crtc_timing_for_drm_display_mode(struct drm_display_mode *drm_mode,
2481 const struct drm_display_mode *native_mode,
2482 bool scale_enabled)
2483 {
2484 if (scale_enabled) {
2485 copy_crtc_timing_for_drm_display_mode(native_mode, drm_mode);
2486 } else if (native_mode->clock == drm_mode->clock &&
2487 native_mode->htotal == drm_mode->htotal &&
2488 native_mode->vtotal == drm_mode->vtotal) {
2489 copy_crtc_timing_for_drm_display_mode(native_mode, drm_mode);
2490 } else {
2491 /* no scaling nor amdgpu inserted, no need to patch */
2492 }
2493 }
2494
2495 static struct dc_sink *
create_fake_sink(struct amdgpu_dm_connector * aconnector)2496 create_fake_sink(struct amdgpu_dm_connector *aconnector)
2497 {
2498 struct dc_sink_init_data sink_init_data = { 0 };
2499 struct dc_sink *sink = NULL;
2500 sink_init_data.link = aconnector->dc_link;
2501 sink_init_data.sink_signal = aconnector->dc_link->connector_signal;
2502
2503 sink = dc_sink_create(&sink_init_data);
2504 if (!sink) {
2505 DRM_ERROR("Failed to create sink!\n");
2506 return NULL;
2507 }
2508 sink->sink_signal = SIGNAL_TYPE_VIRTUAL;
2509
2510 return sink;
2511 }
2512
set_multisync_trigger_params(struct dc_stream_state * stream)2513 static void set_multisync_trigger_params(
2514 struct dc_stream_state *stream)
2515 {
2516 if (stream->triggered_crtc_reset.enabled) {
2517 stream->triggered_crtc_reset.event = CRTC_EVENT_VSYNC_RISING;
2518 stream->triggered_crtc_reset.delay = TRIGGER_DELAY_NEXT_LINE;
2519 }
2520 }
2521
set_master_stream(struct dc_stream_state * stream_set[],int stream_count)2522 static void set_master_stream(struct dc_stream_state *stream_set[],
2523 int stream_count)
2524 {
2525 int j, highest_rfr = 0, master_stream = 0;
2526
2527 for (j = 0; j < stream_count; j++) {
2528 if (stream_set[j] && stream_set[j]->triggered_crtc_reset.enabled) {
2529 int refresh_rate = 0;
2530
2531 refresh_rate = (stream_set[j]->timing.pix_clk_khz*1000)/
2532 (stream_set[j]->timing.h_total*stream_set[j]->timing.v_total);
2533 if (refresh_rate > highest_rfr) {
2534 highest_rfr = refresh_rate;
2535 master_stream = j;
2536 }
2537 }
2538 }
2539 for (j = 0; j < stream_count; j++) {
2540 if (stream_set[j])
2541 stream_set[j]->triggered_crtc_reset.event_source = stream_set[master_stream];
2542 }
2543 }
2544
dm_enable_per_frame_crtc_master_sync(struct dc_state * context)2545 static void dm_enable_per_frame_crtc_master_sync(struct dc_state *context)
2546 {
2547 int i = 0;
2548
2549 if (context->stream_count < 2)
2550 return;
2551 for (i = 0; i < context->stream_count ; i++) {
2552 if (!context->streams[i])
2553 continue;
2554 /* TODO: add a function to read AMD VSDB bits and will set
2555 * crtc_sync_master.multi_sync_enabled flag
2556 * For now its set to false
2557 */
2558 set_multisync_trigger_params(context->streams[i]);
2559 }
2560 set_master_stream(context->streams, context->stream_count);
2561 }
2562
2563 static struct dc_stream_state *
create_stream_for_sink(struct amdgpu_dm_connector * aconnector,const struct drm_display_mode * drm_mode,const struct dm_connector_state * dm_state)2564 create_stream_for_sink(struct amdgpu_dm_connector *aconnector,
2565 const struct drm_display_mode *drm_mode,
2566 const struct dm_connector_state *dm_state)
2567 {
2568 struct drm_display_mode *preferred_mode = NULL;
2569 struct drm_connector *drm_connector;
2570 struct dc_stream_state *stream = NULL;
2571 struct drm_display_mode mode = *drm_mode;
2572 bool native_mode_found = false;
2573 struct dc_sink *sink = NULL;
2574 if (aconnector == NULL) {
2575 DRM_ERROR("aconnector is NULL!\n");
2576 return stream;
2577 }
2578
2579 drm_connector = &aconnector->base;
2580
2581 if (!aconnector->dc_sink) {
2582 /*
2583 * Create dc_sink when necessary to MST
2584 * Don't apply fake_sink to MST
2585 */
2586 if (aconnector->mst_port) {
2587 dm_dp_mst_dc_sink_create(drm_connector);
2588 return stream;
2589 }
2590
2591 sink = create_fake_sink(aconnector);
2592 if (!sink)
2593 return stream;
2594 } else {
2595 sink = aconnector->dc_sink;
2596 }
2597
2598 stream = dc_create_stream_for_sink(sink);
2599
2600 if (stream == NULL) {
2601 DRM_ERROR("Failed to create stream for sink!\n");
2602 goto finish;
2603 }
2604
2605 list_for_each_entry(preferred_mode, &aconnector->base.modes, head) {
2606 /* Search for preferred mode */
2607 if (preferred_mode->type & DRM_MODE_TYPE_PREFERRED) {
2608 native_mode_found = true;
2609 break;
2610 }
2611 }
2612 if (!native_mode_found)
2613 preferred_mode = list_first_entry_or_null(
2614 &aconnector->base.modes,
2615 struct drm_display_mode,
2616 head);
2617
2618 if (preferred_mode == NULL) {
2619 /* This may not be an error, the use case is when we we have no
2620 * usermode calls to reset and set mode upon hotplug. In this
2621 * case, we call set mode ourselves to restore the previous mode
2622 * and the modelist may not be filled in in time.
2623 */
2624 DRM_DEBUG_DRIVER("No preferred mode found\n");
2625 } else {
2626 decide_crtc_timing_for_drm_display_mode(
2627 &mode, preferred_mode,
2628 dm_state ? (dm_state->scaling != RMX_OFF) : false);
2629 }
2630
2631 if (!dm_state)
2632 drm_mode_set_crtcinfo(&mode, 0);
2633
2634 fill_stream_properties_from_drm_display_mode(stream,
2635 &mode, &aconnector->base);
2636 update_stream_scaling_settings(&mode, dm_state, stream);
2637
2638 fill_audio_info(
2639 &stream->audio_info,
2640 drm_connector,
2641 sink);
2642
2643 update_stream_signal(stream);
2644
2645 if (dm_state && dm_state->freesync_capable)
2646 stream->ignore_msa_timing_param = true;
2647 finish:
2648 if (sink && sink->sink_signal == SIGNAL_TYPE_VIRTUAL && aconnector->base.force != DRM_FORCE_ON)
2649 dc_sink_release(sink);
2650
2651 return stream;
2652 }
2653
amdgpu_dm_crtc_destroy(struct drm_crtc * crtc)2654 static void amdgpu_dm_crtc_destroy(struct drm_crtc *crtc)
2655 {
2656 drm_crtc_cleanup(crtc);
2657 kfree(crtc);
2658 }
2659
dm_crtc_destroy_state(struct drm_crtc * crtc,struct drm_crtc_state * state)2660 static void dm_crtc_destroy_state(struct drm_crtc *crtc,
2661 struct drm_crtc_state *state)
2662 {
2663 struct dm_crtc_state *cur = to_dm_crtc_state(state);
2664
2665 /* TODO Destroy dc_stream objects are stream object is flattened */
2666 if (cur->stream)
2667 dc_stream_release(cur->stream);
2668
2669
2670 __drm_atomic_helper_crtc_destroy_state(state);
2671
2672
2673 kfree(state);
2674 }
2675
dm_crtc_reset_state(struct drm_crtc * crtc)2676 static void dm_crtc_reset_state(struct drm_crtc *crtc)
2677 {
2678 struct dm_crtc_state *state;
2679
2680 if (crtc->state)
2681 dm_crtc_destroy_state(crtc, crtc->state);
2682
2683 state = kzalloc(sizeof(*state), GFP_KERNEL);
2684 if (WARN_ON(!state))
2685 return;
2686
2687 crtc->state = &state->base;
2688 crtc->state->crtc = crtc;
2689
2690 }
2691
2692 static struct drm_crtc_state *
dm_crtc_duplicate_state(struct drm_crtc * crtc)2693 dm_crtc_duplicate_state(struct drm_crtc *crtc)
2694 {
2695 struct dm_crtc_state *state, *cur;
2696
2697 cur = to_dm_crtc_state(crtc->state);
2698
2699 if (WARN_ON(!crtc->state))
2700 return NULL;
2701
2702 state = kzalloc(sizeof(*state), GFP_KERNEL);
2703 if (!state)
2704 return NULL;
2705
2706 __drm_atomic_helper_crtc_duplicate_state(crtc, &state->base);
2707
2708 if (cur->stream) {
2709 state->stream = cur->stream;
2710 dc_stream_retain(state->stream);
2711 }
2712
2713 /* TODO Duplicate dc_stream after objects are stream object is flattened */
2714
2715 return &state->base;
2716 }
2717
2718
dm_set_vblank(struct drm_crtc * crtc,bool enable)2719 static inline int dm_set_vblank(struct drm_crtc *crtc, bool enable)
2720 {
2721 enum dc_irq_source irq_source;
2722 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
2723 struct amdgpu_device *adev = crtc->dev->dev_private;
2724
2725 irq_source = IRQ_TYPE_VBLANK + acrtc->otg_inst;
2726 return dc_interrupt_set(adev->dm.dc, irq_source, enable) ? 0 : -EBUSY;
2727 }
2728
dm_enable_vblank(struct drm_crtc * crtc)2729 static int dm_enable_vblank(struct drm_crtc *crtc)
2730 {
2731 return dm_set_vblank(crtc, true);
2732 }
2733
dm_disable_vblank(struct drm_crtc * crtc)2734 static void dm_disable_vblank(struct drm_crtc *crtc)
2735 {
2736 dm_set_vblank(crtc, false);
2737 }
2738
2739 /* Implemented only the options currently availible for the driver */
2740 static const struct drm_crtc_funcs amdgpu_dm_crtc_funcs = {
2741 .reset = dm_crtc_reset_state,
2742 .destroy = amdgpu_dm_crtc_destroy,
2743 .gamma_set = drm_atomic_helper_legacy_gamma_set,
2744 .set_config = drm_atomic_helper_set_config,
2745 .page_flip = drm_atomic_helper_page_flip,
2746 .atomic_duplicate_state = dm_crtc_duplicate_state,
2747 .atomic_destroy_state = dm_crtc_destroy_state,
2748 .set_crc_source = amdgpu_dm_crtc_set_crc_source,
2749 .enable_vblank = dm_enable_vblank,
2750 .disable_vblank = dm_disable_vblank,
2751 };
2752
2753 static enum drm_connector_status
amdgpu_dm_connector_detect(struct drm_connector * connector,bool force)2754 amdgpu_dm_connector_detect(struct drm_connector *connector, bool force)
2755 {
2756 bool connected;
2757 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
2758
2759 /* Notes:
2760 * 1. This interface is NOT called in context of HPD irq.
2761 * 2. This interface *is called* in context of user-mode ioctl. Which
2762 * makes it a bad place for *any* MST-related activit. */
2763
2764 if (aconnector->base.force == DRM_FORCE_UNSPECIFIED &&
2765 !aconnector->fake_enable)
2766 connected = (aconnector->dc_sink != NULL);
2767 else
2768 connected = (aconnector->base.force == DRM_FORCE_ON);
2769
2770 return (connected ? connector_status_connected :
2771 connector_status_disconnected);
2772 }
2773
amdgpu_dm_connector_atomic_set_property(struct drm_connector * connector,struct drm_connector_state * connector_state,struct drm_property * property,uint64_t val)2774 int amdgpu_dm_connector_atomic_set_property(struct drm_connector *connector,
2775 struct drm_connector_state *connector_state,
2776 struct drm_property *property,
2777 uint64_t val)
2778 {
2779 struct drm_device *dev = connector->dev;
2780 struct amdgpu_device *adev = dev->dev_private;
2781 struct dm_connector_state *dm_old_state =
2782 to_dm_connector_state(connector->state);
2783 struct dm_connector_state *dm_new_state =
2784 to_dm_connector_state(connector_state);
2785
2786 int ret = -EINVAL;
2787
2788 if (property == dev->mode_config.scaling_mode_property) {
2789 enum amdgpu_rmx_type rmx_type;
2790
2791 switch (val) {
2792 case DRM_MODE_SCALE_CENTER:
2793 rmx_type = RMX_CENTER;
2794 break;
2795 case DRM_MODE_SCALE_ASPECT:
2796 rmx_type = RMX_ASPECT;
2797 break;
2798 case DRM_MODE_SCALE_FULLSCREEN:
2799 rmx_type = RMX_FULL;
2800 break;
2801 case DRM_MODE_SCALE_NONE:
2802 default:
2803 rmx_type = RMX_OFF;
2804 break;
2805 }
2806
2807 if (dm_old_state->scaling == rmx_type)
2808 return 0;
2809
2810 dm_new_state->scaling = rmx_type;
2811 ret = 0;
2812 } else if (property == adev->mode_info.underscan_hborder_property) {
2813 dm_new_state->underscan_hborder = val;
2814 ret = 0;
2815 } else if (property == adev->mode_info.underscan_vborder_property) {
2816 dm_new_state->underscan_vborder = val;
2817 ret = 0;
2818 } else if (property == adev->mode_info.underscan_property) {
2819 dm_new_state->underscan_enable = val;
2820 ret = 0;
2821 } else if (property == adev->mode_info.max_bpc_property) {
2822 dm_new_state->max_bpc = val;
2823 ret = 0;
2824 }
2825
2826 return ret;
2827 }
2828
amdgpu_dm_connector_atomic_get_property(struct drm_connector * connector,const struct drm_connector_state * state,struct drm_property * property,uint64_t * val)2829 int amdgpu_dm_connector_atomic_get_property(struct drm_connector *connector,
2830 const struct drm_connector_state *state,
2831 struct drm_property *property,
2832 uint64_t *val)
2833 {
2834 struct drm_device *dev = connector->dev;
2835 struct amdgpu_device *adev = dev->dev_private;
2836 struct dm_connector_state *dm_state =
2837 to_dm_connector_state(state);
2838 int ret = -EINVAL;
2839
2840 if (property == dev->mode_config.scaling_mode_property) {
2841 switch (dm_state->scaling) {
2842 case RMX_CENTER:
2843 *val = DRM_MODE_SCALE_CENTER;
2844 break;
2845 case RMX_ASPECT:
2846 *val = DRM_MODE_SCALE_ASPECT;
2847 break;
2848 case RMX_FULL:
2849 *val = DRM_MODE_SCALE_FULLSCREEN;
2850 break;
2851 case RMX_OFF:
2852 default:
2853 *val = DRM_MODE_SCALE_NONE;
2854 break;
2855 }
2856 ret = 0;
2857 } else if (property == adev->mode_info.underscan_hborder_property) {
2858 *val = dm_state->underscan_hborder;
2859 ret = 0;
2860 } else if (property == adev->mode_info.underscan_vborder_property) {
2861 *val = dm_state->underscan_vborder;
2862 ret = 0;
2863 } else if (property == adev->mode_info.underscan_property) {
2864 *val = dm_state->underscan_enable;
2865 ret = 0;
2866 } else if (property == adev->mode_info.max_bpc_property) {
2867 *val = dm_state->max_bpc;
2868 ret = 0;
2869 }
2870 return ret;
2871 }
2872
amdgpu_dm_connector_destroy(struct drm_connector * connector)2873 static void amdgpu_dm_connector_destroy(struct drm_connector *connector)
2874 {
2875 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
2876 const struct dc_link *link = aconnector->dc_link;
2877 struct amdgpu_device *adev = connector->dev->dev_private;
2878 struct amdgpu_display_manager *dm = &adev->dm;
2879
2880 #if defined(CONFIG_BACKLIGHT_CLASS_DEVICE) ||\
2881 defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE)
2882
2883 if ((link->connector_signal & (SIGNAL_TYPE_EDP | SIGNAL_TYPE_LVDS)) &&
2884 link->type != dc_connection_none &&
2885 dm->backlight_dev) {
2886 backlight_device_unregister(dm->backlight_dev);
2887 dm->backlight_dev = NULL;
2888 }
2889 #endif
2890 drm_connector_unregister(connector);
2891 drm_connector_cleanup(connector);
2892 kfree(connector);
2893 }
2894
amdgpu_dm_connector_funcs_reset(struct drm_connector * connector)2895 void amdgpu_dm_connector_funcs_reset(struct drm_connector *connector)
2896 {
2897 struct dm_connector_state *state =
2898 to_dm_connector_state(connector->state);
2899
2900 if (connector->state)
2901 __drm_atomic_helper_connector_destroy_state(connector->state);
2902
2903 kfree(state);
2904
2905 state = kzalloc(sizeof(*state), GFP_KERNEL);
2906
2907 if (state) {
2908 state->scaling = RMX_OFF;
2909 state->underscan_enable = false;
2910 state->underscan_hborder = 0;
2911 state->underscan_vborder = 0;
2912 state->max_bpc = 8;
2913
2914 __drm_atomic_helper_connector_reset(connector, &state->base);
2915 }
2916 }
2917
2918 struct drm_connector_state *
amdgpu_dm_connector_atomic_duplicate_state(struct drm_connector * connector)2919 amdgpu_dm_connector_atomic_duplicate_state(struct drm_connector *connector)
2920 {
2921 struct dm_connector_state *state =
2922 to_dm_connector_state(connector->state);
2923
2924 struct dm_connector_state *new_state =
2925 kmemdup(state, sizeof(*state), GFP_KERNEL);
2926
2927 if (new_state) {
2928 __drm_atomic_helper_connector_duplicate_state(connector,
2929 &new_state->base);
2930 new_state->max_bpc = state->max_bpc;
2931 return &new_state->base;
2932 }
2933
2934 return NULL;
2935 }
2936
2937 static const struct drm_connector_funcs amdgpu_dm_connector_funcs = {
2938 .reset = amdgpu_dm_connector_funcs_reset,
2939 .detect = amdgpu_dm_connector_detect,
2940 .fill_modes = drm_helper_probe_single_connector_modes,
2941 .destroy = amdgpu_dm_connector_destroy,
2942 .atomic_duplicate_state = amdgpu_dm_connector_atomic_duplicate_state,
2943 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
2944 .atomic_set_property = amdgpu_dm_connector_atomic_set_property,
2945 .atomic_get_property = amdgpu_dm_connector_atomic_get_property
2946 };
2947
best_encoder(struct drm_connector * connector)2948 static struct drm_encoder *best_encoder(struct drm_connector *connector)
2949 {
2950 int enc_id = connector->encoder_ids[0];
2951 struct drm_mode_object *obj;
2952 struct drm_encoder *encoder;
2953
2954 DRM_DEBUG_DRIVER("Finding the best encoder\n");
2955
2956 /* pick the encoder ids */
2957 if (enc_id) {
2958 obj = drm_mode_object_find(connector->dev, NULL, enc_id, DRM_MODE_OBJECT_ENCODER);
2959 if (!obj) {
2960 DRM_ERROR("Couldn't find a matching encoder for our connector\n");
2961 return NULL;
2962 }
2963 encoder = obj_to_encoder(obj);
2964 return encoder;
2965 }
2966 DRM_ERROR("No encoder id\n");
2967 return NULL;
2968 }
2969
get_modes(struct drm_connector * connector)2970 static int get_modes(struct drm_connector *connector)
2971 {
2972 return amdgpu_dm_connector_get_modes(connector);
2973 }
2974
create_eml_sink(struct amdgpu_dm_connector * aconnector)2975 static void create_eml_sink(struct amdgpu_dm_connector *aconnector)
2976 {
2977 struct dc_sink_init_data init_params = {
2978 .link = aconnector->dc_link,
2979 .sink_signal = SIGNAL_TYPE_VIRTUAL
2980 };
2981 struct edid *edid;
2982
2983 if (!aconnector->base.edid_blob_ptr) {
2984 DRM_ERROR("No EDID firmware found on connector: %s ,forcing to OFF!\n",
2985 aconnector->base.name);
2986
2987 aconnector->base.force = DRM_FORCE_OFF;
2988 aconnector->base.override_edid = false;
2989 return;
2990 }
2991
2992 edid = (struct edid *) aconnector->base.edid_blob_ptr->data;
2993
2994 aconnector->edid = edid;
2995
2996 aconnector->dc_em_sink = dc_link_add_remote_sink(
2997 aconnector->dc_link,
2998 (uint8_t *)edid,
2999 (edid->extensions + 1) * EDID_LENGTH,
3000 &init_params);
3001
3002 if (aconnector->base.force == DRM_FORCE_ON)
3003 aconnector->dc_sink = aconnector->dc_link->local_sink ?
3004 aconnector->dc_link->local_sink :
3005 aconnector->dc_em_sink;
3006 }
3007
handle_edid_mgmt(struct amdgpu_dm_connector * aconnector)3008 static void handle_edid_mgmt(struct amdgpu_dm_connector *aconnector)
3009 {
3010 struct dc_link *link = (struct dc_link *)aconnector->dc_link;
3011
3012 /* In case of headless boot with force on for DP managed connector
3013 * Those settings have to be != 0 to get initial modeset
3014 */
3015 if (link->connector_signal == SIGNAL_TYPE_DISPLAY_PORT) {
3016 link->verified_link_cap.lane_count = LANE_COUNT_FOUR;
3017 link->verified_link_cap.link_rate = LINK_RATE_HIGH2;
3018 }
3019
3020
3021 aconnector->base.override_edid = true;
3022 create_eml_sink(aconnector);
3023 }
3024
amdgpu_dm_connector_mode_valid(struct drm_connector * connector,struct drm_display_mode * mode)3025 enum drm_mode_status amdgpu_dm_connector_mode_valid(struct drm_connector *connector,
3026 struct drm_display_mode *mode)
3027 {
3028 int result = MODE_ERROR;
3029 struct dc_sink *dc_sink;
3030 struct amdgpu_device *adev = connector->dev->dev_private;
3031 /* TODO: Unhardcode stream count */
3032 struct dc_stream_state *stream;
3033 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
3034 enum dc_status dc_result = DC_OK;
3035
3036 if ((mode->flags & DRM_MODE_FLAG_INTERLACE) ||
3037 (mode->flags & DRM_MODE_FLAG_DBLSCAN))
3038 return result;
3039
3040 /* Only run this the first time mode_valid is called to initilialize
3041 * EDID mgmt
3042 */
3043 if (aconnector->base.force != DRM_FORCE_UNSPECIFIED &&
3044 !aconnector->dc_em_sink)
3045 handle_edid_mgmt(aconnector);
3046
3047 dc_sink = to_amdgpu_dm_connector(connector)->dc_sink;
3048
3049 if (dc_sink == NULL) {
3050 DRM_ERROR("dc_sink is NULL!\n");
3051 goto fail;
3052 }
3053
3054 stream = create_stream_for_sink(aconnector, mode, NULL);
3055 if (stream == NULL) {
3056 DRM_ERROR("Failed to create stream for sink!\n");
3057 goto fail;
3058 }
3059
3060 dc_result = dc_validate_stream(adev->dm.dc, stream);
3061
3062 if (dc_result == DC_OK)
3063 result = MODE_OK;
3064 else
3065 DRM_DEBUG_KMS("Mode %dx%d (clk %d) failed DC validation with error %d\n",
3066 mode->vdisplay,
3067 mode->hdisplay,
3068 mode->clock,
3069 dc_result);
3070
3071 dc_stream_release(stream);
3072
3073 fail:
3074 /* TODO: error handling*/
3075 return result;
3076 }
3077
3078 static const struct drm_connector_helper_funcs
3079 amdgpu_dm_connector_helper_funcs = {
3080 /*
3081 * If hotplug a second bigger display in FB Con mode, bigger resolution
3082 * modes will be filtered by drm_mode_validate_size(), and those modes
3083 * is missing after user start lightdm. So we need to renew modes list.
3084 * in get_modes call back, not just return the modes count
3085 */
3086 .get_modes = get_modes,
3087 .mode_valid = amdgpu_dm_connector_mode_valid,
3088 .best_encoder = best_encoder
3089 };
3090
dm_crtc_helper_disable(struct drm_crtc * crtc)3091 static void dm_crtc_helper_disable(struct drm_crtc *crtc)
3092 {
3093 }
3094
dm_crtc_helper_atomic_check(struct drm_crtc * crtc,struct drm_crtc_state * state)3095 static int dm_crtc_helper_atomic_check(struct drm_crtc *crtc,
3096 struct drm_crtc_state *state)
3097 {
3098 struct amdgpu_device *adev = crtc->dev->dev_private;
3099 struct dc *dc = adev->dm.dc;
3100 struct dm_crtc_state *dm_crtc_state = to_dm_crtc_state(state);
3101 int ret = -EINVAL;
3102
3103 if (unlikely(!dm_crtc_state->stream &&
3104 modeset_required(state, NULL, dm_crtc_state->stream))) {
3105 WARN_ON(1);
3106 return ret;
3107 }
3108
3109 /* In some use cases, like reset, no stream is attached */
3110 if (!dm_crtc_state->stream)
3111 return 0;
3112
3113 if (dc_validate_stream(dc, dm_crtc_state->stream) == DC_OK)
3114 return 0;
3115
3116 return ret;
3117 }
3118
dm_crtc_helper_mode_fixup(struct drm_crtc * crtc,const struct drm_display_mode * mode,struct drm_display_mode * adjusted_mode)3119 static bool dm_crtc_helper_mode_fixup(struct drm_crtc *crtc,
3120 const struct drm_display_mode *mode,
3121 struct drm_display_mode *adjusted_mode)
3122 {
3123 return true;
3124 }
3125
3126 static const struct drm_crtc_helper_funcs amdgpu_dm_crtc_helper_funcs = {
3127 .disable = dm_crtc_helper_disable,
3128 .atomic_check = dm_crtc_helper_atomic_check,
3129 .mode_fixup = dm_crtc_helper_mode_fixup
3130 };
3131
dm_encoder_helper_disable(struct drm_encoder * encoder)3132 static void dm_encoder_helper_disable(struct drm_encoder *encoder)
3133 {
3134
3135 }
3136
dm_encoder_helper_atomic_check(struct drm_encoder * encoder,struct drm_crtc_state * crtc_state,struct drm_connector_state * conn_state)3137 static int dm_encoder_helper_atomic_check(struct drm_encoder *encoder,
3138 struct drm_crtc_state *crtc_state,
3139 struct drm_connector_state *conn_state)
3140 {
3141 return 0;
3142 }
3143
3144 const struct drm_encoder_helper_funcs amdgpu_dm_encoder_helper_funcs = {
3145 .disable = dm_encoder_helper_disable,
3146 .atomic_check = dm_encoder_helper_atomic_check
3147 };
3148
dm_drm_plane_reset(struct drm_plane * plane)3149 static void dm_drm_plane_reset(struct drm_plane *plane)
3150 {
3151 struct dm_plane_state *amdgpu_state = NULL;
3152
3153 if (plane->state)
3154 plane->funcs->atomic_destroy_state(plane, plane->state);
3155
3156 amdgpu_state = kzalloc(sizeof(*amdgpu_state), GFP_KERNEL);
3157 WARN_ON(amdgpu_state == NULL);
3158
3159 if (amdgpu_state) {
3160 plane->state = &amdgpu_state->base;
3161 plane->state->plane = plane;
3162 plane->state->rotation = DRM_MODE_ROTATE_0;
3163 }
3164 }
3165
3166 static struct drm_plane_state *
dm_drm_plane_duplicate_state(struct drm_plane * plane)3167 dm_drm_plane_duplicate_state(struct drm_plane *plane)
3168 {
3169 struct dm_plane_state *dm_plane_state, *old_dm_plane_state;
3170
3171 old_dm_plane_state = to_dm_plane_state(plane->state);
3172 dm_plane_state = kzalloc(sizeof(*dm_plane_state), GFP_KERNEL);
3173 if (!dm_plane_state)
3174 return NULL;
3175
3176 __drm_atomic_helper_plane_duplicate_state(plane, &dm_plane_state->base);
3177
3178 if (old_dm_plane_state->dc_state) {
3179 dm_plane_state->dc_state = old_dm_plane_state->dc_state;
3180 dc_plane_state_retain(dm_plane_state->dc_state);
3181 }
3182
3183 return &dm_plane_state->base;
3184 }
3185
dm_drm_plane_destroy_state(struct drm_plane * plane,struct drm_plane_state * state)3186 void dm_drm_plane_destroy_state(struct drm_plane *plane,
3187 struct drm_plane_state *state)
3188 {
3189 struct dm_plane_state *dm_plane_state = to_dm_plane_state(state);
3190
3191 if (dm_plane_state->dc_state)
3192 dc_plane_state_release(dm_plane_state->dc_state);
3193
3194 drm_atomic_helper_plane_destroy_state(plane, state);
3195 }
3196
3197 static const struct drm_plane_funcs dm_plane_funcs = {
3198 .update_plane = drm_atomic_helper_update_plane,
3199 .disable_plane = drm_atomic_helper_disable_plane,
3200 .destroy = drm_primary_helper_destroy,
3201 .reset = dm_drm_plane_reset,
3202 .atomic_duplicate_state = dm_drm_plane_duplicate_state,
3203 .atomic_destroy_state = dm_drm_plane_destroy_state,
3204 };
3205
dm_plane_helper_prepare_fb(struct drm_plane * plane,struct drm_plane_state * new_state)3206 static int dm_plane_helper_prepare_fb(struct drm_plane *plane,
3207 struct drm_plane_state *new_state)
3208 {
3209 struct amdgpu_framebuffer *afb;
3210 struct drm_gem_object *obj;
3211 struct amdgpu_device *adev;
3212 struct amdgpu_bo *rbo;
3213 uint64_t chroma_addr = 0;
3214 struct dm_plane_state *dm_plane_state_new, *dm_plane_state_old;
3215 unsigned int awidth;
3216 uint32_t domain;
3217 int r;
3218
3219 dm_plane_state_old = to_dm_plane_state(plane->state);
3220 dm_plane_state_new = to_dm_plane_state(new_state);
3221
3222 if (!new_state->fb) {
3223 DRM_DEBUG_DRIVER("No FB bound\n");
3224 return 0;
3225 }
3226
3227 afb = to_amdgpu_framebuffer(new_state->fb);
3228 obj = new_state->fb->obj[0];
3229 rbo = gem_to_amdgpu_bo(obj);
3230 adev = amdgpu_ttm_adev(rbo->tbo.bdev);
3231 r = amdgpu_bo_reserve(rbo, false);
3232 if (unlikely(r != 0))
3233 return r;
3234
3235 if (plane->type != DRM_PLANE_TYPE_CURSOR)
3236 domain = amdgpu_display_supported_domains(adev);
3237 else
3238 domain = AMDGPU_GEM_DOMAIN_VRAM;
3239
3240 r = amdgpu_bo_pin(rbo, domain);
3241 if (unlikely(r != 0)) {
3242 if (r != -ERESTARTSYS)
3243 DRM_ERROR("Failed to pin framebuffer with error %d\n", r);
3244 amdgpu_bo_unreserve(rbo);
3245 return r;
3246 }
3247
3248 r = amdgpu_ttm_alloc_gart(&rbo->tbo);
3249 if (unlikely(r != 0)) {
3250 amdgpu_bo_unpin(rbo);
3251 amdgpu_bo_unreserve(rbo);
3252 DRM_ERROR("%p bind failed\n", rbo);
3253 return r;
3254 }
3255 amdgpu_bo_unreserve(rbo);
3256
3257 afb->address = amdgpu_bo_gpu_offset(rbo);
3258
3259 amdgpu_bo_ref(rbo);
3260
3261 if (dm_plane_state_new->dc_state &&
3262 dm_plane_state_old->dc_state != dm_plane_state_new->dc_state) {
3263 struct dc_plane_state *plane_state = dm_plane_state_new->dc_state;
3264
3265 if (plane_state->format < SURFACE_PIXEL_FORMAT_VIDEO_BEGIN) {
3266 plane_state->address.grph.addr.low_part = lower_32_bits(afb->address);
3267 plane_state->address.grph.addr.high_part = upper_32_bits(afb->address);
3268 } else {
3269 awidth = ALIGN(new_state->fb->width, 64);
3270 plane_state->address.type = PLN_ADDR_TYPE_VIDEO_PROGRESSIVE;
3271 plane_state->address.video_progressive.luma_addr.low_part
3272 = lower_32_bits(afb->address);
3273 plane_state->address.video_progressive.luma_addr.high_part
3274 = upper_32_bits(afb->address);
3275 chroma_addr = afb->address + (u64)awidth * new_state->fb->height;
3276 plane_state->address.video_progressive.chroma_addr.low_part
3277 = lower_32_bits(chroma_addr);
3278 plane_state->address.video_progressive.chroma_addr.high_part
3279 = upper_32_bits(chroma_addr);
3280 }
3281 }
3282
3283 return 0;
3284 }
3285
dm_plane_helper_cleanup_fb(struct drm_plane * plane,struct drm_plane_state * old_state)3286 static void dm_plane_helper_cleanup_fb(struct drm_plane *plane,
3287 struct drm_plane_state *old_state)
3288 {
3289 struct amdgpu_bo *rbo;
3290 int r;
3291
3292 if (!old_state->fb)
3293 return;
3294
3295 rbo = gem_to_amdgpu_bo(old_state->fb->obj[0]);
3296 r = amdgpu_bo_reserve(rbo, false);
3297 if (unlikely(r)) {
3298 DRM_ERROR("failed to reserve rbo before unpin\n");
3299 return;
3300 }
3301
3302 amdgpu_bo_unpin(rbo);
3303 amdgpu_bo_unreserve(rbo);
3304 amdgpu_bo_unref(&rbo);
3305 }
3306
dm_plane_atomic_check(struct drm_plane * plane,struct drm_plane_state * state)3307 static int dm_plane_atomic_check(struct drm_plane *plane,
3308 struct drm_plane_state *state)
3309 {
3310 struct amdgpu_device *adev = plane->dev->dev_private;
3311 struct dc *dc = adev->dm.dc;
3312 struct dm_plane_state *dm_plane_state = to_dm_plane_state(state);
3313
3314 if (!dm_plane_state->dc_state)
3315 return 0;
3316
3317 if (!fill_rects_from_plane_state(state, dm_plane_state->dc_state))
3318 return -EINVAL;
3319
3320 if (dc_validate_plane(dc, dm_plane_state->dc_state) == DC_OK)
3321 return 0;
3322
3323 return -EINVAL;
3324 }
3325
3326 static const struct drm_plane_helper_funcs dm_plane_helper_funcs = {
3327 .prepare_fb = dm_plane_helper_prepare_fb,
3328 .cleanup_fb = dm_plane_helper_cleanup_fb,
3329 .atomic_check = dm_plane_atomic_check,
3330 };
3331
3332 /*
3333 * TODO: these are currently initialized to rgb formats only.
3334 * For future use cases we should either initialize them dynamically based on
3335 * plane capabilities, or initialize this array to all formats, so internal drm
3336 * check will succeed, and let DC to implement proper check
3337 */
3338 static const uint32_t rgb_formats[] = {
3339 DRM_FORMAT_RGB888,
3340 DRM_FORMAT_XRGB8888,
3341 DRM_FORMAT_ARGB8888,
3342 DRM_FORMAT_RGBA8888,
3343 DRM_FORMAT_XRGB2101010,
3344 DRM_FORMAT_XBGR2101010,
3345 DRM_FORMAT_ARGB2101010,
3346 DRM_FORMAT_ABGR2101010,
3347 };
3348
3349 static const uint32_t yuv_formats[] = {
3350 DRM_FORMAT_NV12,
3351 DRM_FORMAT_NV21,
3352 };
3353
3354 static const u32 cursor_formats[] = {
3355 DRM_FORMAT_ARGB8888
3356 };
3357
amdgpu_dm_plane_init(struct amdgpu_display_manager * dm,struct amdgpu_plane * aplane,unsigned long possible_crtcs)3358 static int amdgpu_dm_plane_init(struct amdgpu_display_manager *dm,
3359 struct amdgpu_plane *aplane,
3360 unsigned long possible_crtcs)
3361 {
3362 int res = -EPERM;
3363
3364 switch (aplane->base.type) {
3365 case DRM_PLANE_TYPE_PRIMARY:
3366 res = drm_universal_plane_init(
3367 dm->adev->ddev,
3368 &aplane->base,
3369 possible_crtcs,
3370 &dm_plane_funcs,
3371 rgb_formats,
3372 ARRAY_SIZE(rgb_formats),
3373 NULL, aplane->base.type, NULL);
3374 break;
3375 case DRM_PLANE_TYPE_OVERLAY:
3376 res = drm_universal_plane_init(
3377 dm->adev->ddev,
3378 &aplane->base,
3379 possible_crtcs,
3380 &dm_plane_funcs,
3381 yuv_formats,
3382 ARRAY_SIZE(yuv_formats),
3383 NULL, aplane->base.type, NULL);
3384 break;
3385 case DRM_PLANE_TYPE_CURSOR:
3386 res = drm_universal_plane_init(
3387 dm->adev->ddev,
3388 &aplane->base,
3389 possible_crtcs,
3390 &dm_plane_funcs,
3391 cursor_formats,
3392 ARRAY_SIZE(cursor_formats),
3393 NULL, aplane->base.type, NULL);
3394 break;
3395 }
3396
3397 drm_plane_helper_add(&aplane->base, &dm_plane_helper_funcs);
3398
3399 /* Create (reset) the plane state */
3400 if (aplane->base.funcs->reset)
3401 aplane->base.funcs->reset(&aplane->base);
3402
3403
3404 return res;
3405 }
3406
amdgpu_dm_crtc_init(struct amdgpu_display_manager * dm,struct drm_plane * plane,uint32_t crtc_index)3407 static int amdgpu_dm_crtc_init(struct amdgpu_display_manager *dm,
3408 struct drm_plane *plane,
3409 uint32_t crtc_index)
3410 {
3411 struct amdgpu_crtc *acrtc = NULL;
3412 struct amdgpu_plane *cursor_plane;
3413
3414 int res = -ENOMEM;
3415
3416 cursor_plane = kzalloc(sizeof(*cursor_plane), GFP_KERNEL);
3417 if (!cursor_plane)
3418 goto fail;
3419
3420 cursor_plane->base.type = DRM_PLANE_TYPE_CURSOR;
3421 res = amdgpu_dm_plane_init(dm, cursor_plane, 0);
3422
3423 acrtc = kzalloc(sizeof(struct amdgpu_crtc), GFP_KERNEL);
3424 if (!acrtc)
3425 goto fail;
3426
3427 res = drm_crtc_init_with_planes(
3428 dm->ddev,
3429 &acrtc->base,
3430 plane,
3431 &cursor_plane->base,
3432 &amdgpu_dm_crtc_funcs, NULL);
3433
3434 if (res)
3435 goto fail;
3436
3437 drm_crtc_helper_add(&acrtc->base, &amdgpu_dm_crtc_helper_funcs);
3438
3439 /* Create (reset) the plane state */
3440 if (acrtc->base.funcs->reset)
3441 acrtc->base.funcs->reset(&acrtc->base);
3442
3443 acrtc->max_cursor_width = dm->adev->dm.dc->caps.max_cursor_size;
3444 acrtc->max_cursor_height = dm->adev->dm.dc->caps.max_cursor_size;
3445
3446 acrtc->crtc_id = crtc_index;
3447 acrtc->base.enabled = false;
3448
3449 dm->adev->mode_info.crtcs[crtc_index] = acrtc;
3450 drm_crtc_enable_color_mgmt(&acrtc->base, MAX_COLOR_LUT_ENTRIES,
3451 true, MAX_COLOR_LUT_ENTRIES);
3452 drm_mode_crtc_set_gamma_size(&acrtc->base, MAX_COLOR_LEGACY_LUT_ENTRIES);
3453
3454 return 0;
3455
3456 fail:
3457 kfree(acrtc);
3458 kfree(cursor_plane);
3459 return res;
3460 }
3461
3462
to_drm_connector_type(enum signal_type st)3463 static int to_drm_connector_type(enum signal_type st)
3464 {
3465 switch (st) {
3466 case SIGNAL_TYPE_HDMI_TYPE_A:
3467 return DRM_MODE_CONNECTOR_HDMIA;
3468 case SIGNAL_TYPE_EDP:
3469 return DRM_MODE_CONNECTOR_eDP;
3470 case SIGNAL_TYPE_RGB:
3471 return DRM_MODE_CONNECTOR_VGA;
3472 case SIGNAL_TYPE_DISPLAY_PORT:
3473 case SIGNAL_TYPE_DISPLAY_PORT_MST:
3474 return DRM_MODE_CONNECTOR_DisplayPort;
3475 case SIGNAL_TYPE_DVI_DUAL_LINK:
3476 case SIGNAL_TYPE_DVI_SINGLE_LINK:
3477 return DRM_MODE_CONNECTOR_DVID;
3478 case SIGNAL_TYPE_VIRTUAL:
3479 return DRM_MODE_CONNECTOR_VIRTUAL;
3480
3481 default:
3482 return DRM_MODE_CONNECTOR_Unknown;
3483 }
3484 }
3485
amdgpu_dm_get_native_mode(struct drm_connector * connector)3486 static void amdgpu_dm_get_native_mode(struct drm_connector *connector)
3487 {
3488 const struct drm_connector_helper_funcs *helper =
3489 connector->helper_private;
3490 struct drm_encoder *encoder;
3491 struct amdgpu_encoder *amdgpu_encoder;
3492
3493 encoder = helper->best_encoder(connector);
3494
3495 if (encoder == NULL)
3496 return;
3497
3498 amdgpu_encoder = to_amdgpu_encoder(encoder);
3499
3500 amdgpu_encoder->native_mode.clock = 0;
3501
3502 if (!list_empty(&connector->probed_modes)) {
3503 struct drm_display_mode *preferred_mode = NULL;
3504
3505 list_for_each_entry(preferred_mode,
3506 &connector->probed_modes,
3507 head) {
3508 if (preferred_mode->type & DRM_MODE_TYPE_PREFERRED)
3509 amdgpu_encoder->native_mode = *preferred_mode;
3510
3511 break;
3512 }
3513
3514 }
3515 }
3516
3517 static struct drm_display_mode *
amdgpu_dm_create_common_mode(struct drm_encoder * encoder,char * name,int hdisplay,int vdisplay)3518 amdgpu_dm_create_common_mode(struct drm_encoder *encoder,
3519 char *name,
3520 int hdisplay, int vdisplay)
3521 {
3522 struct drm_device *dev = encoder->dev;
3523 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
3524 struct drm_display_mode *mode = NULL;
3525 struct drm_display_mode *native_mode = &amdgpu_encoder->native_mode;
3526
3527 mode = drm_mode_duplicate(dev, native_mode);
3528
3529 if (mode == NULL)
3530 return NULL;
3531
3532 mode->hdisplay = hdisplay;
3533 mode->vdisplay = vdisplay;
3534 mode->type &= ~DRM_MODE_TYPE_PREFERRED;
3535 strncpy(mode->name, name, DRM_DISPLAY_MODE_LEN);
3536
3537 return mode;
3538
3539 }
3540
amdgpu_dm_connector_add_common_modes(struct drm_encoder * encoder,struct drm_connector * connector)3541 static void amdgpu_dm_connector_add_common_modes(struct drm_encoder *encoder,
3542 struct drm_connector *connector)
3543 {
3544 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
3545 struct drm_display_mode *mode = NULL;
3546 struct drm_display_mode *native_mode = &amdgpu_encoder->native_mode;
3547 struct amdgpu_dm_connector *amdgpu_dm_connector =
3548 to_amdgpu_dm_connector(connector);
3549 int i;
3550 int n;
3551 struct mode_size {
3552 char name[DRM_DISPLAY_MODE_LEN];
3553 int w;
3554 int h;
3555 } common_modes[] = {
3556 { "640x480", 640, 480},
3557 { "800x600", 800, 600},
3558 { "1024x768", 1024, 768},
3559 { "1280x720", 1280, 720},
3560 { "1280x800", 1280, 800},
3561 {"1280x1024", 1280, 1024},
3562 { "1440x900", 1440, 900},
3563 {"1680x1050", 1680, 1050},
3564 {"1600x1200", 1600, 1200},
3565 {"1920x1080", 1920, 1080},
3566 {"1920x1200", 1920, 1200}
3567 };
3568
3569 n = ARRAY_SIZE(common_modes);
3570
3571 for (i = 0; i < n; i++) {
3572 struct drm_display_mode *curmode = NULL;
3573 bool mode_existed = false;
3574
3575 if (common_modes[i].w > native_mode->hdisplay ||
3576 common_modes[i].h > native_mode->vdisplay ||
3577 (common_modes[i].w == native_mode->hdisplay &&
3578 common_modes[i].h == native_mode->vdisplay))
3579 continue;
3580
3581 list_for_each_entry(curmode, &connector->probed_modes, head) {
3582 if (common_modes[i].w == curmode->hdisplay &&
3583 common_modes[i].h == curmode->vdisplay) {
3584 mode_existed = true;
3585 break;
3586 }
3587 }
3588
3589 if (mode_existed)
3590 continue;
3591
3592 mode = amdgpu_dm_create_common_mode(encoder,
3593 common_modes[i].name, common_modes[i].w,
3594 common_modes[i].h);
3595 drm_mode_probed_add(connector, mode);
3596 amdgpu_dm_connector->num_modes++;
3597 }
3598 }
3599
amdgpu_dm_connector_ddc_get_modes(struct drm_connector * connector,struct edid * edid)3600 static void amdgpu_dm_connector_ddc_get_modes(struct drm_connector *connector,
3601 struct edid *edid)
3602 {
3603 struct amdgpu_dm_connector *amdgpu_dm_connector =
3604 to_amdgpu_dm_connector(connector);
3605
3606 if (edid) {
3607 /* empty probed_modes */
3608 INIT_LIST_HEAD(&connector->probed_modes);
3609 amdgpu_dm_connector->num_modes =
3610 drm_add_edid_modes(connector, edid);
3611
3612 amdgpu_dm_get_native_mode(connector);
3613 } else {
3614 amdgpu_dm_connector->num_modes = 0;
3615 }
3616 }
3617
amdgpu_dm_connector_get_modes(struct drm_connector * connector)3618 static int amdgpu_dm_connector_get_modes(struct drm_connector *connector)
3619 {
3620 const struct drm_connector_helper_funcs *helper =
3621 connector->helper_private;
3622 struct amdgpu_dm_connector *amdgpu_dm_connector =
3623 to_amdgpu_dm_connector(connector);
3624 struct drm_encoder *encoder;
3625 struct edid *edid = amdgpu_dm_connector->edid;
3626
3627 encoder = helper->best_encoder(connector);
3628
3629 if (!edid || !drm_edid_is_valid(edid)) {
3630 drm_add_modes_noedid(connector, 640, 480);
3631 } else {
3632 amdgpu_dm_connector_ddc_get_modes(connector, edid);
3633 amdgpu_dm_connector_add_common_modes(encoder, connector);
3634 }
3635 amdgpu_dm_fbc_init(connector);
3636
3637 return amdgpu_dm_connector->num_modes;
3638 }
3639
amdgpu_dm_connector_init_helper(struct amdgpu_display_manager * dm,struct amdgpu_dm_connector * aconnector,int connector_type,struct dc_link * link,int link_index)3640 void amdgpu_dm_connector_init_helper(struct amdgpu_display_manager *dm,
3641 struct amdgpu_dm_connector *aconnector,
3642 int connector_type,
3643 struct dc_link *link,
3644 int link_index)
3645 {
3646 struct amdgpu_device *adev = dm->ddev->dev_private;
3647
3648 /*
3649 * Some of the properties below require access to state, like bpc.
3650 * Allocate some default initial connector state with our reset helper.
3651 */
3652 if (aconnector->base.funcs->reset)
3653 aconnector->base.funcs->reset(&aconnector->base);
3654
3655 aconnector->connector_id = link_index;
3656 aconnector->dc_link = link;
3657 aconnector->base.interlace_allowed = false;
3658 aconnector->base.doublescan_allowed = false;
3659 aconnector->base.stereo_allowed = false;
3660 aconnector->base.dpms = DRM_MODE_DPMS_OFF;
3661 aconnector->hpd.hpd = AMDGPU_HPD_NONE; /* not used */
3662 mutex_init(&aconnector->hpd_lock);
3663
3664 /* configure support HPD hot plug connector_>polled default value is 0
3665 * which means HPD hot plug not supported
3666 */
3667 switch (connector_type) {
3668 case DRM_MODE_CONNECTOR_HDMIA:
3669 aconnector->base.polled = DRM_CONNECTOR_POLL_HPD;
3670 aconnector->base.ycbcr_420_allowed =
3671 link->link_enc->features.ycbcr420_supported ? true : false;
3672 break;
3673 case DRM_MODE_CONNECTOR_DisplayPort:
3674 aconnector->base.polled = DRM_CONNECTOR_POLL_HPD;
3675 aconnector->base.ycbcr_420_allowed =
3676 link->link_enc->features.ycbcr420_supported ? true : false;
3677 break;
3678 case DRM_MODE_CONNECTOR_DVID:
3679 aconnector->base.polled = DRM_CONNECTOR_POLL_HPD;
3680 break;
3681 default:
3682 break;
3683 }
3684
3685 drm_object_attach_property(&aconnector->base.base,
3686 dm->ddev->mode_config.scaling_mode_property,
3687 DRM_MODE_SCALE_NONE);
3688
3689 drm_object_attach_property(&aconnector->base.base,
3690 adev->mode_info.underscan_property,
3691 UNDERSCAN_OFF);
3692 drm_object_attach_property(&aconnector->base.base,
3693 adev->mode_info.underscan_hborder_property,
3694 0);
3695 drm_object_attach_property(&aconnector->base.base,
3696 adev->mode_info.underscan_vborder_property,
3697 0);
3698 drm_object_attach_property(&aconnector->base.base,
3699 adev->mode_info.max_bpc_property,
3700 0);
3701
3702 }
3703
amdgpu_dm_i2c_xfer(struct i2c_adapter * i2c_adap,struct i2c_msg * msgs,int num)3704 static int amdgpu_dm_i2c_xfer(struct i2c_adapter *i2c_adap,
3705 struct i2c_msg *msgs, int num)
3706 {
3707 struct amdgpu_i2c_adapter *i2c = i2c_get_adapdata(i2c_adap);
3708 struct ddc_service *ddc_service = i2c->ddc_service;
3709 struct i2c_command cmd;
3710 int i;
3711 int result = -EIO;
3712
3713 cmd.payloads = kcalloc(num, sizeof(struct i2c_payload), GFP_KERNEL);
3714
3715 if (!cmd.payloads)
3716 return result;
3717
3718 cmd.number_of_payloads = num;
3719 cmd.engine = I2C_COMMAND_ENGINE_DEFAULT;
3720 cmd.speed = 100;
3721
3722 for (i = 0; i < num; i++) {
3723 cmd.payloads[i].write = !(msgs[i].flags & I2C_M_RD);
3724 cmd.payloads[i].address = msgs[i].addr;
3725 cmd.payloads[i].length = msgs[i].len;
3726 cmd.payloads[i].data = msgs[i].buf;
3727 }
3728
3729 if (dal_i2caux_submit_i2c_command(
3730 ddc_service->ctx->i2caux,
3731 ddc_service->ddc_pin,
3732 &cmd))
3733 result = num;
3734
3735 kfree(cmd.payloads);
3736 return result;
3737 }
3738
amdgpu_dm_i2c_func(struct i2c_adapter * adap)3739 static u32 amdgpu_dm_i2c_func(struct i2c_adapter *adap)
3740 {
3741 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
3742 }
3743
3744 static const struct i2c_algorithm amdgpu_dm_i2c_algo = {
3745 .master_xfer = amdgpu_dm_i2c_xfer,
3746 .functionality = amdgpu_dm_i2c_func,
3747 };
3748
3749 static struct amdgpu_i2c_adapter *
create_i2c(struct ddc_service * ddc_service,int link_index,int * res)3750 create_i2c(struct ddc_service *ddc_service,
3751 int link_index,
3752 int *res)
3753 {
3754 struct amdgpu_device *adev = ddc_service->ctx->driver_context;
3755 struct amdgpu_i2c_adapter *i2c;
3756
3757 i2c = kzalloc(sizeof(struct amdgpu_i2c_adapter), GFP_KERNEL);
3758 if (!i2c)
3759 return NULL;
3760 i2c->base.owner = THIS_MODULE;
3761 i2c->base.class = I2C_CLASS_DDC;
3762 i2c->base.dev.parent = &adev->pdev->dev;
3763 i2c->base.algo = &amdgpu_dm_i2c_algo;
3764 snprintf(i2c->base.name, sizeof(i2c->base.name), "AMDGPU DM i2c hw bus %d", link_index);
3765 i2c_set_adapdata(&i2c->base, i2c);
3766 i2c->ddc_service = ddc_service;
3767
3768 return i2c;
3769 }
3770
3771
3772 /* Note: this function assumes that dc_link_detect() was called for the
3773 * dc_link which will be represented by this aconnector.
3774 */
amdgpu_dm_connector_init(struct amdgpu_display_manager * dm,struct amdgpu_dm_connector * aconnector,uint32_t link_index,struct amdgpu_encoder * aencoder)3775 static int amdgpu_dm_connector_init(struct amdgpu_display_manager *dm,
3776 struct amdgpu_dm_connector *aconnector,
3777 uint32_t link_index,
3778 struct amdgpu_encoder *aencoder)
3779 {
3780 int res = 0;
3781 int connector_type;
3782 struct dc *dc = dm->dc;
3783 struct dc_link *link = dc_get_link_at_index(dc, link_index);
3784 struct amdgpu_i2c_adapter *i2c;
3785
3786 link->priv = aconnector;
3787
3788 DRM_DEBUG_DRIVER("%s()\n", __func__);
3789
3790 i2c = create_i2c(link->ddc, link->link_index, &res);
3791 if (!i2c) {
3792 DRM_ERROR("Failed to create i2c adapter data\n");
3793 return -ENOMEM;
3794 }
3795
3796 aconnector->i2c = i2c;
3797 res = i2c_add_adapter(&i2c->base);
3798
3799 if (res) {
3800 DRM_ERROR("Failed to register hw i2c %d\n", link->link_index);
3801 goto out_free;
3802 }
3803
3804 connector_type = to_drm_connector_type(link->connector_signal);
3805
3806 res = drm_connector_init(
3807 dm->ddev,
3808 &aconnector->base,
3809 &amdgpu_dm_connector_funcs,
3810 connector_type);
3811
3812 if (res) {
3813 DRM_ERROR("connector_init failed\n");
3814 aconnector->connector_id = -1;
3815 goto out_free;
3816 }
3817
3818 drm_connector_helper_add(
3819 &aconnector->base,
3820 &amdgpu_dm_connector_helper_funcs);
3821
3822 amdgpu_dm_connector_init_helper(
3823 dm,
3824 aconnector,
3825 connector_type,
3826 link,
3827 link_index);
3828
3829 drm_connector_attach_encoder(
3830 &aconnector->base, &aencoder->base);
3831
3832 drm_connector_register(&aconnector->base);
3833 #if defined(CONFIG_DEBUG_FS)
3834 res = connector_debugfs_init(aconnector);
3835 if (res) {
3836 DRM_ERROR("Failed to create debugfs for connector");
3837 goto out_free;
3838 }
3839 #endif
3840
3841 if (connector_type == DRM_MODE_CONNECTOR_DisplayPort
3842 || connector_type == DRM_MODE_CONNECTOR_eDP)
3843 amdgpu_dm_initialize_dp_connector(dm, aconnector);
3844
3845 out_free:
3846 if (res) {
3847 kfree(i2c);
3848 aconnector->i2c = NULL;
3849 }
3850 return res;
3851 }
3852
amdgpu_dm_get_encoder_crtc_mask(struct amdgpu_device * adev)3853 int amdgpu_dm_get_encoder_crtc_mask(struct amdgpu_device *adev)
3854 {
3855 switch (adev->mode_info.num_crtc) {
3856 case 1:
3857 return 0x1;
3858 case 2:
3859 return 0x3;
3860 case 3:
3861 return 0x7;
3862 case 4:
3863 return 0xf;
3864 case 5:
3865 return 0x1f;
3866 case 6:
3867 default:
3868 return 0x3f;
3869 }
3870 }
3871
amdgpu_dm_encoder_init(struct drm_device * dev,struct amdgpu_encoder * aencoder,uint32_t link_index)3872 static int amdgpu_dm_encoder_init(struct drm_device *dev,
3873 struct amdgpu_encoder *aencoder,
3874 uint32_t link_index)
3875 {
3876 struct amdgpu_device *adev = dev->dev_private;
3877
3878 int res = drm_encoder_init(dev,
3879 &aencoder->base,
3880 &amdgpu_dm_encoder_funcs,
3881 DRM_MODE_ENCODER_TMDS,
3882 NULL);
3883
3884 aencoder->base.possible_crtcs = amdgpu_dm_get_encoder_crtc_mask(adev);
3885
3886 if (!res)
3887 aencoder->encoder_id = link_index;
3888 else
3889 aencoder->encoder_id = -1;
3890
3891 drm_encoder_helper_add(&aencoder->base, &amdgpu_dm_encoder_helper_funcs);
3892
3893 return res;
3894 }
3895
manage_dm_interrupts(struct amdgpu_device * adev,struct amdgpu_crtc * acrtc,bool enable)3896 static void manage_dm_interrupts(struct amdgpu_device *adev,
3897 struct amdgpu_crtc *acrtc,
3898 bool enable)
3899 {
3900 /*
3901 * this is not correct translation but will work as soon as VBLANK
3902 * constant is the same as PFLIP
3903 */
3904 int irq_type =
3905 amdgpu_display_crtc_idx_to_irq_type(
3906 adev,
3907 acrtc->crtc_id);
3908
3909 if (enable) {
3910 drm_crtc_vblank_on(&acrtc->base);
3911 amdgpu_irq_get(
3912 adev,
3913 &adev->pageflip_irq,
3914 irq_type);
3915 } else {
3916
3917 amdgpu_irq_put(
3918 adev,
3919 &adev->pageflip_irq,
3920 irq_type);
3921 drm_crtc_vblank_off(&acrtc->base);
3922 }
3923 }
3924
3925 static bool
is_scaling_state_different(const struct dm_connector_state * dm_state,const struct dm_connector_state * old_dm_state)3926 is_scaling_state_different(const struct dm_connector_state *dm_state,
3927 const struct dm_connector_state *old_dm_state)
3928 {
3929 if (dm_state->scaling != old_dm_state->scaling)
3930 return true;
3931 if (!dm_state->underscan_enable && old_dm_state->underscan_enable) {
3932 if (old_dm_state->underscan_hborder != 0 && old_dm_state->underscan_vborder != 0)
3933 return true;
3934 } else if (dm_state->underscan_enable && !old_dm_state->underscan_enable) {
3935 if (dm_state->underscan_hborder != 0 && dm_state->underscan_vborder != 0)
3936 return true;
3937 } else if (dm_state->underscan_hborder != old_dm_state->underscan_hborder ||
3938 dm_state->underscan_vborder != old_dm_state->underscan_vborder)
3939 return true;
3940 return false;
3941 }
3942
remove_stream(struct amdgpu_device * adev,struct amdgpu_crtc * acrtc,struct dc_stream_state * stream)3943 static void remove_stream(struct amdgpu_device *adev,
3944 struct amdgpu_crtc *acrtc,
3945 struct dc_stream_state *stream)
3946 {
3947 /* this is the update mode case */
3948 if (adev->dm.freesync_module)
3949 mod_freesync_remove_stream(adev->dm.freesync_module, stream);
3950
3951 acrtc->otg_inst = -1;
3952 acrtc->enabled = false;
3953 }
3954
get_cursor_position(struct drm_plane * plane,struct drm_crtc * crtc,struct dc_cursor_position * position)3955 static int get_cursor_position(struct drm_plane *plane, struct drm_crtc *crtc,
3956 struct dc_cursor_position *position)
3957 {
3958 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
3959 int x, y;
3960 int xorigin = 0, yorigin = 0;
3961
3962 if (!crtc || !plane->state->fb) {
3963 position->enable = false;
3964 position->x = 0;
3965 position->y = 0;
3966 return 0;
3967 }
3968
3969 if ((plane->state->crtc_w > amdgpu_crtc->max_cursor_width) ||
3970 (plane->state->crtc_h > amdgpu_crtc->max_cursor_height)) {
3971 DRM_ERROR("%s: bad cursor width or height %d x %d\n",
3972 __func__,
3973 plane->state->crtc_w,
3974 plane->state->crtc_h);
3975 return -EINVAL;
3976 }
3977
3978 x = plane->state->crtc_x;
3979 y = plane->state->crtc_y;
3980 /* avivo cursor are offset into the total surface */
3981 x += crtc->primary->state->src_x >> 16;
3982 y += crtc->primary->state->src_y >> 16;
3983 if (x < 0) {
3984 xorigin = min(-x, amdgpu_crtc->max_cursor_width - 1);
3985 x = 0;
3986 }
3987 if (y < 0) {
3988 yorigin = min(-y, amdgpu_crtc->max_cursor_height - 1);
3989 y = 0;
3990 }
3991 position->enable = true;
3992 position->x = x;
3993 position->y = y;
3994 position->x_hotspot = xorigin;
3995 position->y_hotspot = yorigin;
3996
3997 return 0;
3998 }
3999
handle_cursor_update(struct drm_plane * plane,struct drm_plane_state * old_plane_state)4000 static void handle_cursor_update(struct drm_plane *plane,
4001 struct drm_plane_state *old_plane_state)
4002 {
4003 struct amdgpu_framebuffer *afb = to_amdgpu_framebuffer(plane->state->fb);
4004 struct drm_crtc *crtc = afb ? plane->state->crtc : old_plane_state->crtc;
4005 struct dm_crtc_state *crtc_state = crtc ? to_dm_crtc_state(crtc->state) : NULL;
4006 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
4007 uint64_t address = afb ? afb->address : 0;
4008 struct dc_cursor_position position;
4009 struct dc_cursor_attributes attributes;
4010 int ret;
4011
4012 if (!plane->state->fb && !old_plane_state->fb)
4013 return;
4014
4015 DRM_DEBUG_DRIVER("%s: crtc_id=%d with size %d to %d\n",
4016 __func__,
4017 amdgpu_crtc->crtc_id,
4018 plane->state->crtc_w,
4019 plane->state->crtc_h);
4020
4021 ret = get_cursor_position(plane, crtc, &position);
4022 if (ret)
4023 return;
4024
4025 if (!position.enable) {
4026 /* turn off cursor */
4027 if (crtc_state && crtc_state->stream)
4028 dc_stream_set_cursor_position(crtc_state->stream,
4029 &position);
4030 return;
4031 }
4032
4033 amdgpu_crtc->cursor_width = plane->state->crtc_w;
4034 amdgpu_crtc->cursor_height = plane->state->crtc_h;
4035
4036 memset(&attributes, 0, sizeof(attributes));
4037 attributes.address.high_part = upper_32_bits(address);
4038 attributes.address.low_part = lower_32_bits(address);
4039 attributes.width = plane->state->crtc_w;
4040 attributes.height = plane->state->crtc_h;
4041 attributes.color_format = CURSOR_MODE_COLOR_PRE_MULTIPLIED_ALPHA;
4042 attributes.rotation_angle = 0;
4043 attributes.attribute_flags.value = 0;
4044
4045 attributes.pitch = attributes.width;
4046
4047 if (crtc_state->stream) {
4048 if (!dc_stream_set_cursor_attributes(crtc_state->stream,
4049 &attributes))
4050 DRM_ERROR("DC failed to set cursor attributes\n");
4051
4052 if (!dc_stream_set_cursor_position(crtc_state->stream,
4053 &position))
4054 DRM_ERROR("DC failed to set cursor position\n");
4055 }
4056 }
4057
prepare_flip_isr(struct amdgpu_crtc * acrtc)4058 static void prepare_flip_isr(struct amdgpu_crtc *acrtc)
4059 {
4060
4061 assert_spin_locked(&acrtc->base.dev->event_lock);
4062 WARN_ON(acrtc->event);
4063
4064 acrtc->event = acrtc->base.state->event;
4065
4066 /* Set the flip status */
4067 acrtc->pflip_status = AMDGPU_FLIP_SUBMITTED;
4068
4069 /* Mark this event as consumed */
4070 acrtc->base.state->event = NULL;
4071
4072 DRM_DEBUG_DRIVER("crtc:%d, pflip_stat:AMDGPU_FLIP_SUBMITTED\n",
4073 acrtc->crtc_id);
4074 }
4075
4076 /*
4077 * Executes flip
4078 *
4079 * Waits on all BO's fences and for proper vblank count
4080 */
amdgpu_dm_do_flip(struct drm_crtc * crtc,struct drm_framebuffer * fb,uint32_t target,struct dc_state * state)4081 static void amdgpu_dm_do_flip(struct drm_crtc *crtc,
4082 struct drm_framebuffer *fb,
4083 uint32_t target,
4084 struct dc_state *state)
4085 {
4086 unsigned long flags;
4087 uint32_t target_vblank;
4088 int r, vpos, hpos;
4089 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
4090 struct amdgpu_framebuffer *afb = to_amdgpu_framebuffer(fb);
4091 struct amdgpu_bo *abo = gem_to_amdgpu_bo(fb->obj[0]);
4092 struct amdgpu_device *adev = crtc->dev->dev_private;
4093 bool async_flip = (crtc->state->pageflip_flags & DRM_MODE_PAGE_FLIP_ASYNC) != 0;
4094 struct dc_flip_addrs addr = { {0} };
4095 /* TODO eliminate or rename surface_update */
4096 struct dc_surface_update surface_updates[1] = { {0} };
4097 struct dm_crtc_state *acrtc_state = to_dm_crtc_state(crtc->state);
4098
4099
4100 /* Prepare wait for target vblank early - before the fence-waits */
4101 target_vblank = target - (uint32_t)drm_crtc_vblank_count(crtc) +
4102 amdgpu_get_vblank_counter_kms(crtc->dev, acrtc->crtc_id);
4103
4104 /* TODO This might fail and hence better not used, wait
4105 * explicitly on fences instead
4106 * and in general should be called for
4107 * blocking commit to as per framework helpers
4108 */
4109 r = amdgpu_bo_reserve(abo, true);
4110 if (unlikely(r != 0)) {
4111 DRM_ERROR("failed to reserve buffer before flip\n");
4112 WARN_ON(1);
4113 }
4114
4115 /* Wait for all fences on this FB */
4116 WARN_ON(reservation_object_wait_timeout_rcu(abo->tbo.resv, true, false,
4117 MAX_SCHEDULE_TIMEOUT) < 0);
4118
4119 amdgpu_bo_unreserve(abo);
4120
4121 /* Wait until we're out of the vertical blank period before the one
4122 * targeted by the flip
4123 */
4124 while ((acrtc->enabled &&
4125 (amdgpu_display_get_crtc_scanoutpos(adev->ddev, acrtc->crtc_id,
4126 0, &vpos, &hpos, NULL,
4127 NULL, &crtc->hwmode)
4128 & (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_IN_VBLANK)) ==
4129 (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_IN_VBLANK) &&
4130 (int)(target_vblank -
4131 amdgpu_get_vblank_counter_kms(adev->ddev, acrtc->crtc_id)) > 0)) {
4132 usleep_range(1000, 1100);
4133 }
4134
4135 /* Flip */
4136 spin_lock_irqsave(&crtc->dev->event_lock, flags);
4137
4138 WARN_ON(acrtc->pflip_status != AMDGPU_FLIP_NONE);
4139 WARN_ON(!acrtc_state->stream);
4140
4141 addr.address.grph.addr.low_part = lower_32_bits(afb->address);
4142 addr.address.grph.addr.high_part = upper_32_bits(afb->address);
4143 addr.flip_immediate = async_flip;
4144
4145
4146 if (acrtc->base.state->event)
4147 prepare_flip_isr(acrtc);
4148
4149 spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
4150
4151 surface_updates->surface = dc_stream_get_status(acrtc_state->stream)->plane_states[0];
4152 surface_updates->flip_addr = &addr;
4153
4154 dc_commit_updates_for_stream(adev->dm.dc,
4155 surface_updates,
4156 1,
4157 acrtc_state->stream,
4158 NULL,
4159 &surface_updates->surface,
4160 state);
4161
4162 DRM_DEBUG_DRIVER("%s Flipping to hi: 0x%x, low: 0x%x \n",
4163 __func__,
4164 addr.address.grph.addr.high_part,
4165 addr.address.grph.addr.low_part);
4166 }
4167
4168 /*
4169 * TODO this whole function needs to go
4170 *
4171 * dc_surface_update is needlessly complex. See if we can just replace this
4172 * with a dc_plane_state and follow the atomic model a bit more closely here.
4173 */
commit_planes_to_stream(struct dc * dc,struct dc_plane_state ** plane_states,uint8_t new_plane_count,struct dm_crtc_state * dm_new_crtc_state,struct dm_crtc_state * dm_old_crtc_state,struct dc_state * state)4174 static bool commit_planes_to_stream(
4175 struct dc *dc,
4176 struct dc_plane_state **plane_states,
4177 uint8_t new_plane_count,
4178 struct dm_crtc_state *dm_new_crtc_state,
4179 struct dm_crtc_state *dm_old_crtc_state,
4180 struct dc_state *state)
4181 {
4182 /* no need to dynamically allocate this. it's pretty small */
4183 struct dc_surface_update updates[MAX_SURFACES];
4184 struct dc_flip_addrs *flip_addr;
4185 struct dc_plane_info *plane_info;
4186 struct dc_scaling_info *scaling_info;
4187 int i;
4188 struct dc_stream_state *dc_stream = dm_new_crtc_state->stream;
4189 struct dc_stream_update *stream_update =
4190 kzalloc(sizeof(struct dc_stream_update), GFP_KERNEL);
4191
4192 if (!stream_update) {
4193 BREAK_TO_DEBUGGER();
4194 return false;
4195 }
4196
4197 flip_addr = kcalloc(MAX_SURFACES, sizeof(struct dc_flip_addrs),
4198 GFP_KERNEL);
4199 plane_info = kcalloc(MAX_SURFACES, sizeof(struct dc_plane_info),
4200 GFP_KERNEL);
4201 scaling_info = kcalloc(MAX_SURFACES, sizeof(struct dc_scaling_info),
4202 GFP_KERNEL);
4203
4204 if (!flip_addr || !plane_info || !scaling_info) {
4205 kfree(flip_addr);
4206 kfree(plane_info);
4207 kfree(scaling_info);
4208 kfree(stream_update);
4209 return false;
4210 }
4211
4212 memset(updates, 0, sizeof(updates));
4213
4214 stream_update->src = dc_stream->src;
4215 stream_update->dst = dc_stream->dst;
4216 stream_update->out_transfer_func = dc_stream->out_transfer_func;
4217
4218 for (i = 0; i < new_plane_count; i++) {
4219 updates[i].surface = plane_states[i];
4220 updates[i].gamma =
4221 (struct dc_gamma *)plane_states[i]->gamma_correction;
4222 updates[i].in_transfer_func = plane_states[i]->in_transfer_func;
4223 flip_addr[i].address = plane_states[i]->address;
4224 flip_addr[i].flip_immediate = plane_states[i]->flip_immediate;
4225 plane_info[i].color_space = plane_states[i]->color_space;
4226 plane_info[i].format = plane_states[i]->format;
4227 plane_info[i].plane_size = plane_states[i]->plane_size;
4228 plane_info[i].rotation = plane_states[i]->rotation;
4229 plane_info[i].horizontal_mirror = plane_states[i]->horizontal_mirror;
4230 plane_info[i].stereo_format = plane_states[i]->stereo_format;
4231 plane_info[i].tiling_info = plane_states[i]->tiling_info;
4232 plane_info[i].visible = plane_states[i]->visible;
4233 plane_info[i].per_pixel_alpha = plane_states[i]->per_pixel_alpha;
4234 plane_info[i].dcc = plane_states[i]->dcc;
4235 scaling_info[i].scaling_quality = plane_states[i]->scaling_quality;
4236 scaling_info[i].src_rect = plane_states[i]->src_rect;
4237 scaling_info[i].dst_rect = plane_states[i]->dst_rect;
4238 scaling_info[i].clip_rect = plane_states[i]->clip_rect;
4239
4240 updates[i].flip_addr = &flip_addr[i];
4241 updates[i].plane_info = &plane_info[i];
4242 updates[i].scaling_info = &scaling_info[i];
4243 }
4244
4245 dc_commit_updates_for_stream(
4246 dc,
4247 updates,
4248 new_plane_count,
4249 dc_stream, stream_update, plane_states, state);
4250
4251 kfree(flip_addr);
4252 kfree(plane_info);
4253 kfree(scaling_info);
4254 kfree(stream_update);
4255 return true;
4256 }
4257
amdgpu_dm_commit_planes(struct drm_atomic_state * state,struct drm_device * dev,struct amdgpu_display_manager * dm,struct drm_crtc * pcrtc,bool * wait_for_vblank)4258 static void amdgpu_dm_commit_planes(struct drm_atomic_state *state,
4259 struct drm_device *dev,
4260 struct amdgpu_display_manager *dm,
4261 struct drm_crtc *pcrtc,
4262 bool *wait_for_vblank)
4263 {
4264 uint32_t i;
4265 struct drm_plane *plane;
4266 struct drm_plane_state *old_plane_state, *new_plane_state;
4267 struct dc_stream_state *dc_stream_attach;
4268 struct dc_plane_state *plane_states_constructed[MAX_SURFACES];
4269 struct amdgpu_crtc *acrtc_attach = to_amdgpu_crtc(pcrtc);
4270 struct drm_crtc_state *new_pcrtc_state =
4271 drm_atomic_get_new_crtc_state(state, pcrtc);
4272 struct dm_crtc_state *acrtc_state = to_dm_crtc_state(new_pcrtc_state);
4273 struct dm_crtc_state *dm_old_crtc_state =
4274 to_dm_crtc_state(drm_atomic_get_old_crtc_state(state, pcrtc));
4275 struct dm_atomic_state *dm_state = to_dm_atomic_state(state);
4276 int planes_count = 0;
4277 unsigned long flags;
4278
4279 /* update planes when needed */
4280 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
4281 struct drm_crtc *crtc = new_plane_state->crtc;
4282 struct drm_crtc_state *new_crtc_state;
4283 struct drm_framebuffer *fb = new_plane_state->fb;
4284 bool pflip_needed;
4285 struct dm_plane_state *dm_new_plane_state = to_dm_plane_state(new_plane_state);
4286
4287 if (plane->type == DRM_PLANE_TYPE_CURSOR) {
4288 handle_cursor_update(plane, old_plane_state);
4289 continue;
4290 }
4291
4292 if (!fb || !crtc || pcrtc != crtc)
4293 continue;
4294
4295 new_crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
4296 if (!new_crtc_state->active)
4297 continue;
4298
4299 pflip_needed = !state->allow_modeset;
4300
4301 spin_lock_irqsave(&crtc->dev->event_lock, flags);
4302 if (acrtc_attach->pflip_status != AMDGPU_FLIP_NONE) {
4303 DRM_ERROR("%s: acrtc %d, already busy\n",
4304 __func__,
4305 acrtc_attach->crtc_id);
4306 /* In commit tail framework this cannot happen */
4307 WARN_ON(1);
4308 }
4309 spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
4310
4311 if (!pflip_needed || plane->type == DRM_PLANE_TYPE_OVERLAY) {
4312 WARN_ON(!dm_new_plane_state->dc_state);
4313
4314 plane_states_constructed[planes_count] = dm_new_plane_state->dc_state;
4315
4316 dc_stream_attach = acrtc_state->stream;
4317 planes_count++;
4318
4319 } else if (new_crtc_state->planes_changed) {
4320 /* Assume even ONE crtc with immediate flip means
4321 * entire can't wait for VBLANK
4322 * TODO Check if it's correct
4323 */
4324 *wait_for_vblank =
4325 new_pcrtc_state->pageflip_flags & DRM_MODE_PAGE_FLIP_ASYNC ?
4326 false : true;
4327
4328 /* TODO: Needs rework for multiplane flip */
4329 if (plane->type == DRM_PLANE_TYPE_PRIMARY)
4330 drm_crtc_vblank_get(crtc);
4331
4332 amdgpu_dm_do_flip(
4333 crtc,
4334 fb,
4335 (uint32_t)drm_crtc_vblank_count(crtc) + *wait_for_vblank,
4336 dm_state->context);
4337 }
4338
4339 }
4340
4341 if (planes_count) {
4342 unsigned long flags;
4343
4344 if (new_pcrtc_state->event) {
4345
4346 drm_crtc_vblank_get(pcrtc);
4347
4348 spin_lock_irqsave(&pcrtc->dev->event_lock, flags);
4349 prepare_flip_isr(acrtc_attach);
4350 spin_unlock_irqrestore(&pcrtc->dev->event_lock, flags);
4351 }
4352
4353
4354 if (false == commit_planes_to_stream(dm->dc,
4355 plane_states_constructed,
4356 planes_count,
4357 acrtc_state,
4358 dm_old_crtc_state,
4359 dm_state->context))
4360 dm_error("%s: Failed to attach plane!\n", __func__);
4361 } else {
4362 /*TODO BUG Here should go disable planes on CRTC. */
4363 }
4364 }
4365
4366 /**
4367 * amdgpu_dm_crtc_copy_transient_flags - copy mirrored flags from DRM to DC
4368 * @crtc_state: the DRM CRTC state
4369 * @stream_state: the DC stream state.
4370 *
4371 * Copy the mirrored transient state flags from DRM, to DC. It is used to bring
4372 * a dc_stream_state's flags in sync with a drm_crtc_state's flags.
4373 */
amdgpu_dm_crtc_copy_transient_flags(struct drm_crtc_state * crtc_state,struct dc_stream_state * stream_state)4374 static void amdgpu_dm_crtc_copy_transient_flags(struct drm_crtc_state *crtc_state,
4375 struct dc_stream_state *stream_state)
4376 {
4377 stream_state->mode_changed = drm_atomic_crtc_needs_modeset(crtc_state);
4378 }
4379
amdgpu_dm_atomic_commit(struct drm_device * dev,struct drm_atomic_state * state,bool nonblock)4380 static int amdgpu_dm_atomic_commit(struct drm_device *dev,
4381 struct drm_atomic_state *state,
4382 bool nonblock)
4383 {
4384 struct drm_crtc *crtc;
4385 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
4386 struct amdgpu_device *adev = dev->dev_private;
4387 int i;
4388
4389 /*
4390 * We evade vblanks and pflips on crtc that
4391 * should be changed. We do it here to flush & disable
4392 * interrupts before drm_swap_state is called in drm_atomic_helper_commit
4393 * it will update crtc->dm_crtc_state->stream pointer which is used in
4394 * the ISRs.
4395 */
4396 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
4397 struct dm_crtc_state *dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
4398 struct dm_crtc_state *dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
4399 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
4400
4401 if (drm_atomic_crtc_needs_modeset(new_crtc_state)
4402 && dm_old_crtc_state->stream) {
4403 /*
4404 * CRC capture was enabled but not disabled.
4405 * Release the vblank reference.
4406 */
4407 if (dm_new_crtc_state->crc_enabled) {
4408 drm_crtc_vblank_put(crtc);
4409 dm_new_crtc_state->crc_enabled = false;
4410 }
4411
4412 manage_dm_interrupts(adev, acrtc, false);
4413 }
4414 }
4415 /* Add check here for SoC's that support hardware cursor plane, to
4416 * unset legacy_cursor_update */
4417
4418 return drm_atomic_helper_commit(dev, state, nonblock);
4419
4420 /*TODO Handle EINTR, reenable IRQ*/
4421 }
4422
amdgpu_dm_atomic_commit_tail(struct drm_atomic_state * state)4423 static void amdgpu_dm_atomic_commit_tail(struct drm_atomic_state *state)
4424 {
4425 struct drm_device *dev = state->dev;
4426 struct amdgpu_device *adev = dev->dev_private;
4427 struct amdgpu_display_manager *dm = &adev->dm;
4428 struct dm_atomic_state *dm_state;
4429 uint32_t i, j;
4430 struct drm_crtc *crtc;
4431 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
4432 unsigned long flags;
4433 bool wait_for_vblank = true;
4434 struct drm_connector *connector;
4435 struct drm_connector_state *old_con_state, *new_con_state;
4436 struct dm_crtc_state *dm_old_crtc_state, *dm_new_crtc_state;
4437 int crtc_disable_count = 0;
4438
4439 drm_atomic_helper_update_legacy_modeset_state(dev, state);
4440
4441 dm_state = to_dm_atomic_state(state);
4442
4443 /* update changed items */
4444 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
4445 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
4446
4447 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
4448 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
4449
4450 DRM_DEBUG_DRIVER(
4451 "amdgpu_crtc id:%d crtc_state_flags: enable:%d, active:%d, "
4452 "planes_changed:%d, mode_changed:%d,active_changed:%d,"
4453 "connectors_changed:%d\n",
4454 acrtc->crtc_id,
4455 new_crtc_state->enable,
4456 new_crtc_state->active,
4457 new_crtc_state->planes_changed,
4458 new_crtc_state->mode_changed,
4459 new_crtc_state->active_changed,
4460 new_crtc_state->connectors_changed);
4461
4462 /* Copy all transient state flags into dc state */
4463 if (dm_new_crtc_state->stream) {
4464 amdgpu_dm_crtc_copy_transient_flags(&dm_new_crtc_state->base,
4465 dm_new_crtc_state->stream);
4466 }
4467
4468 /* handles headless hotplug case, updating new_state and
4469 * aconnector as needed
4470 */
4471
4472 if (modeset_required(new_crtc_state, dm_new_crtc_state->stream, dm_old_crtc_state->stream)) {
4473
4474 DRM_DEBUG_DRIVER("Atomic commit: SET crtc id %d: [%p]\n", acrtc->crtc_id, acrtc);
4475
4476 if (!dm_new_crtc_state->stream) {
4477 /*
4478 * this could happen because of issues with
4479 * userspace notifications delivery.
4480 * In this case userspace tries to set mode on
4481 * display which is disconnect in fact.
4482 * dc_sink in NULL in this case on aconnector.
4483 * We expect reset mode will come soon.
4484 *
4485 * This can also happen when unplug is done
4486 * during resume sequence ended
4487 *
4488 * In this case, we want to pretend we still
4489 * have a sink to keep the pipe running so that
4490 * hw state is consistent with the sw state
4491 */
4492 DRM_DEBUG_DRIVER("%s: Failed to create new stream for crtc %d\n",
4493 __func__, acrtc->base.base.id);
4494 continue;
4495 }
4496
4497 if (dm_old_crtc_state->stream)
4498 remove_stream(adev, acrtc, dm_old_crtc_state->stream);
4499
4500 pm_runtime_get_noresume(dev->dev);
4501
4502 acrtc->enabled = true;
4503 acrtc->hw_mode = new_crtc_state->mode;
4504 crtc->hwmode = new_crtc_state->mode;
4505 } else if (modereset_required(new_crtc_state)) {
4506 DRM_DEBUG_DRIVER("Atomic commit: RESET. crtc id %d:[%p]\n", acrtc->crtc_id, acrtc);
4507
4508 /* i.e. reset mode */
4509 if (dm_old_crtc_state->stream)
4510 remove_stream(adev, acrtc, dm_old_crtc_state->stream);
4511 }
4512 } /* for_each_crtc_in_state() */
4513
4514 /*
4515 * Add streams after required streams from new and replaced streams
4516 * are removed from freesync module
4517 */
4518 if (adev->dm.freesync_module) {
4519 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state,
4520 new_crtc_state, i) {
4521 struct amdgpu_dm_connector *aconnector = NULL;
4522 struct dm_connector_state *dm_new_con_state = NULL;
4523 struct amdgpu_crtc *acrtc = NULL;
4524 bool modeset_needed;
4525
4526 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
4527 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
4528 modeset_needed = modeset_required(
4529 new_crtc_state,
4530 dm_new_crtc_state->stream,
4531 dm_old_crtc_state->stream);
4532 /* We add stream to freesync if:
4533 * 1. Said stream is not null, and
4534 * 2. A modeset is requested. This means that the
4535 * stream was removed previously, and needs to be
4536 * replaced.
4537 */
4538 if (dm_new_crtc_state->stream == NULL ||
4539 !modeset_needed)
4540 continue;
4541
4542 acrtc = to_amdgpu_crtc(crtc);
4543
4544 aconnector =
4545 amdgpu_dm_find_first_crtc_matching_connector(
4546 state, crtc);
4547 if (!aconnector) {
4548 DRM_DEBUG_DRIVER("Atomic commit: Failed to "
4549 "find connector for acrtc "
4550 "id:%d skipping freesync "
4551 "init\n",
4552 acrtc->crtc_id);
4553 continue;
4554 }
4555
4556 mod_freesync_add_stream(adev->dm.freesync_module,
4557 dm_new_crtc_state->stream,
4558 &aconnector->caps);
4559 new_con_state = drm_atomic_get_new_connector_state(
4560 state, &aconnector->base);
4561 dm_new_con_state = to_dm_connector_state(new_con_state);
4562
4563 mod_freesync_set_user_enable(adev->dm.freesync_module,
4564 &dm_new_crtc_state->stream,
4565 1,
4566 &dm_new_con_state->user_enable);
4567 }
4568 }
4569
4570 if (dm_state->context) {
4571 dm_enable_per_frame_crtc_master_sync(dm_state->context);
4572 WARN_ON(!dc_commit_state(dm->dc, dm_state->context));
4573 }
4574
4575 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
4576 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
4577
4578 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
4579
4580 if (dm_new_crtc_state->stream != NULL) {
4581 const struct dc_stream_status *status =
4582 dc_stream_get_status(dm_new_crtc_state->stream);
4583
4584 if (!status)
4585 DC_ERR("got no status for stream %p on acrtc%p\n", dm_new_crtc_state->stream, acrtc);
4586 else
4587 acrtc->otg_inst = status->primary_otg_inst;
4588 }
4589 }
4590
4591 /* Handle scaling and underscan changes*/
4592 for_each_oldnew_connector_in_state(state, connector, old_con_state, new_con_state, i) {
4593 struct dm_connector_state *dm_new_con_state = to_dm_connector_state(new_con_state);
4594 struct dm_connector_state *dm_old_con_state = to_dm_connector_state(old_con_state);
4595 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(dm_new_con_state->base.crtc);
4596 struct dc_stream_status *status = NULL;
4597
4598 if (acrtc) {
4599 new_crtc_state = drm_atomic_get_new_crtc_state(state, &acrtc->base);
4600 old_crtc_state = drm_atomic_get_old_crtc_state(state, &acrtc->base);
4601 }
4602
4603 /* Skip any modesets/resets */
4604 if (!acrtc || drm_atomic_crtc_needs_modeset(new_crtc_state))
4605 continue;
4606
4607 /* Skip any thing not scale or underscan changes */
4608 if (!is_scaling_state_different(dm_new_con_state, dm_old_con_state))
4609 continue;
4610
4611 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
4612
4613 update_stream_scaling_settings(&dm_new_con_state->base.crtc->mode,
4614 dm_new_con_state, (struct dc_stream_state *)dm_new_crtc_state->stream);
4615
4616 if (!dm_new_crtc_state->stream)
4617 continue;
4618
4619 status = dc_stream_get_status(dm_new_crtc_state->stream);
4620 WARN_ON(!status);
4621 WARN_ON(!status->plane_count);
4622
4623 /*TODO How it works with MPO ?*/
4624 if (!commit_planes_to_stream(
4625 dm->dc,
4626 status->plane_states,
4627 status->plane_count,
4628 dm_new_crtc_state,
4629 to_dm_crtc_state(old_crtc_state),
4630 dm_state->context))
4631 dm_error("%s: Failed to update stream scaling!\n", __func__);
4632 }
4633
4634 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state,
4635 new_crtc_state, i) {
4636 /*
4637 * loop to enable interrupts on newly arrived crtc
4638 */
4639 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
4640 bool modeset_needed;
4641
4642 if (old_crtc_state->active && !new_crtc_state->active)
4643 crtc_disable_count++;
4644
4645 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
4646 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
4647 modeset_needed = modeset_required(
4648 new_crtc_state,
4649 dm_new_crtc_state->stream,
4650 dm_old_crtc_state->stream);
4651
4652 if (dm_new_crtc_state->stream == NULL || !modeset_needed)
4653 continue;
4654
4655 if (adev->dm.freesync_module)
4656 mod_freesync_notify_mode_change(
4657 adev->dm.freesync_module,
4658 &dm_new_crtc_state->stream, 1);
4659
4660 manage_dm_interrupts(adev, acrtc, true);
4661 }
4662
4663 /* update planes when needed per crtc*/
4664 for_each_new_crtc_in_state(state, crtc, new_crtc_state, j) {
4665 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
4666
4667 if (dm_new_crtc_state->stream)
4668 amdgpu_dm_commit_planes(state, dev, dm, crtc, &wait_for_vblank);
4669 }
4670
4671
4672 /*
4673 * send vblank event on all events not handled in flip and
4674 * mark consumed event for drm_atomic_helper_commit_hw_done
4675 */
4676 spin_lock_irqsave(&adev->ddev->event_lock, flags);
4677 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
4678
4679 if (new_crtc_state->event)
4680 drm_send_event_locked(dev, &new_crtc_state->event->base);
4681
4682 new_crtc_state->event = NULL;
4683 }
4684 spin_unlock_irqrestore(&adev->ddev->event_lock, flags);
4685
4686
4687 if (wait_for_vblank)
4688 drm_atomic_helper_wait_for_flip_done(dev, state);
4689
4690 /*
4691 * FIXME:
4692 * Delay hw_done() until flip_done() is signaled. This is to block
4693 * another commit from freeing the CRTC state while we're still
4694 * waiting on flip_done.
4695 */
4696 drm_atomic_helper_commit_hw_done(state);
4697
4698 drm_atomic_helper_cleanup_planes(dev, state);
4699
4700 /* Finally, drop a runtime PM reference for each newly disabled CRTC,
4701 * so we can put the GPU into runtime suspend if we're not driving any
4702 * displays anymore
4703 */
4704 for (i = 0; i < crtc_disable_count; i++)
4705 pm_runtime_put_autosuspend(dev->dev);
4706 pm_runtime_mark_last_busy(dev->dev);
4707 }
4708
4709
dm_force_atomic_commit(struct drm_connector * connector)4710 static int dm_force_atomic_commit(struct drm_connector *connector)
4711 {
4712 int ret = 0;
4713 struct drm_device *ddev = connector->dev;
4714 struct drm_atomic_state *state = drm_atomic_state_alloc(ddev);
4715 struct amdgpu_crtc *disconnected_acrtc = to_amdgpu_crtc(connector->encoder->crtc);
4716 struct drm_plane *plane = disconnected_acrtc->base.primary;
4717 struct drm_connector_state *conn_state;
4718 struct drm_crtc_state *crtc_state;
4719 struct drm_plane_state *plane_state;
4720
4721 if (!state)
4722 return -ENOMEM;
4723
4724 state->acquire_ctx = ddev->mode_config.acquire_ctx;
4725
4726 /* Construct an atomic state to restore previous display setting */
4727
4728 /*
4729 * Attach connectors to drm_atomic_state
4730 */
4731 conn_state = drm_atomic_get_connector_state(state, connector);
4732
4733 ret = PTR_ERR_OR_ZERO(conn_state);
4734 if (ret)
4735 goto err;
4736
4737 /* Attach crtc to drm_atomic_state*/
4738 crtc_state = drm_atomic_get_crtc_state(state, &disconnected_acrtc->base);
4739
4740 ret = PTR_ERR_OR_ZERO(crtc_state);
4741 if (ret)
4742 goto err;
4743
4744 /* force a restore */
4745 crtc_state->mode_changed = true;
4746
4747 /* Attach plane to drm_atomic_state */
4748 plane_state = drm_atomic_get_plane_state(state, plane);
4749
4750 ret = PTR_ERR_OR_ZERO(plane_state);
4751 if (ret)
4752 goto err;
4753
4754
4755 /* Call commit internally with the state we just constructed */
4756 ret = drm_atomic_commit(state);
4757 if (!ret)
4758 return 0;
4759
4760 err:
4761 DRM_ERROR("Restoring old state failed with %i\n", ret);
4762 drm_atomic_state_put(state);
4763
4764 return ret;
4765 }
4766
4767 /*
4768 * This functions handle all cases when set mode does not come upon hotplug.
4769 * This include when the same display is unplugged then plugged back into the
4770 * same port and when we are running without usermode desktop manager supprot
4771 */
dm_restore_drm_connector_state(struct drm_device * dev,struct drm_connector * connector)4772 void dm_restore_drm_connector_state(struct drm_device *dev,
4773 struct drm_connector *connector)
4774 {
4775 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
4776 struct amdgpu_crtc *disconnected_acrtc;
4777 struct dm_crtc_state *acrtc_state;
4778
4779 if (!aconnector->dc_sink || !connector->state || !connector->encoder)
4780 return;
4781
4782 disconnected_acrtc = to_amdgpu_crtc(connector->encoder->crtc);
4783 if (!disconnected_acrtc)
4784 return;
4785
4786 acrtc_state = to_dm_crtc_state(disconnected_acrtc->base.state);
4787 if (!acrtc_state->stream)
4788 return;
4789
4790 /*
4791 * If the previous sink is not released and different from the current,
4792 * we deduce we are in a state where we can not rely on usermode call
4793 * to turn on the display, so we do it here
4794 */
4795 if (acrtc_state->stream->sink != aconnector->dc_sink)
4796 dm_force_atomic_commit(&aconnector->base);
4797 }
4798
4799 /*`
4800 * Grabs all modesetting locks to serialize against any blocking commits,
4801 * Waits for completion of all non blocking commits.
4802 */
do_aquire_global_lock(struct drm_device * dev,struct drm_atomic_state * state)4803 static int do_aquire_global_lock(struct drm_device *dev,
4804 struct drm_atomic_state *state)
4805 {
4806 struct drm_crtc *crtc;
4807 struct drm_crtc_commit *commit;
4808 long ret;
4809
4810 /* Adding all modeset locks to aquire_ctx will
4811 * ensure that when the framework release it the
4812 * extra locks we are locking here will get released to
4813 */
4814 ret = drm_modeset_lock_all_ctx(dev, state->acquire_ctx);
4815 if (ret)
4816 return ret;
4817
4818 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
4819 spin_lock(&crtc->commit_lock);
4820 commit = list_first_entry_or_null(&crtc->commit_list,
4821 struct drm_crtc_commit, commit_entry);
4822 if (commit)
4823 drm_crtc_commit_get(commit);
4824 spin_unlock(&crtc->commit_lock);
4825
4826 if (!commit)
4827 continue;
4828
4829 /* Make sure all pending HW programming completed and
4830 * page flips done
4831 */
4832 ret = wait_for_completion_interruptible_timeout(&commit->hw_done, 10*HZ);
4833
4834 if (ret > 0)
4835 ret = wait_for_completion_interruptible_timeout(
4836 &commit->flip_done, 10*HZ);
4837
4838 if (ret == 0)
4839 DRM_ERROR("[CRTC:%d:%s] hw_done or flip_done "
4840 "timed out\n", crtc->base.id, crtc->name);
4841
4842 drm_crtc_commit_put(commit);
4843 }
4844
4845 return ret < 0 ? ret : 0;
4846 }
4847
dm_update_crtcs_state(struct dc * dc,struct drm_atomic_state * state,bool enable,bool * lock_and_validation_needed)4848 static int dm_update_crtcs_state(struct dc *dc,
4849 struct drm_atomic_state *state,
4850 bool enable,
4851 bool *lock_and_validation_needed)
4852 {
4853 struct drm_crtc *crtc;
4854 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
4855 int i;
4856 struct dm_crtc_state *dm_old_crtc_state, *dm_new_crtc_state;
4857 struct dm_atomic_state *dm_state = to_dm_atomic_state(state);
4858 struct dc_stream_state *new_stream;
4859 int ret = 0;
4860
4861 /*TODO Move this code into dm_crtc_atomic_check once we get rid of dc_validation_set */
4862 /* update changed items */
4863 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
4864 struct amdgpu_crtc *acrtc = NULL;
4865 struct amdgpu_dm_connector *aconnector = NULL;
4866 struct drm_connector_state *drm_new_conn_state = NULL, *drm_old_conn_state = NULL;
4867 struct dm_connector_state *dm_new_conn_state = NULL, *dm_old_conn_state = NULL;
4868 struct drm_plane_state *new_plane_state = NULL;
4869
4870 new_stream = NULL;
4871
4872 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
4873 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
4874 acrtc = to_amdgpu_crtc(crtc);
4875
4876 new_plane_state = drm_atomic_get_new_plane_state(state, new_crtc_state->crtc->primary);
4877
4878 if (new_crtc_state->enable && new_plane_state && !new_plane_state->fb) {
4879 ret = -EINVAL;
4880 goto fail;
4881 }
4882
4883 aconnector = amdgpu_dm_find_first_crtc_matching_connector(state, crtc);
4884
4885 /* TODO This hack should go away */
4886 if (aconnector && enable) {
4887 // Make sure fake sink is created in plug-in scenario
4888 drm_new_conn_state = drm_atomic_get_new_connector_state(state,
4889 &aconnector->base);
4890 drm_old_conn_state = drm_atomic_get_old_connector_state(state,
4891 &aconnector->base);
4892
4893 if (IS_ERR(drm_new_conn_state)) {
4894 ret = PTR_ERR_OR_ZERO(drm_new_conn_state);
4895 break;
4896 }
4897
4898 dm_new_conn_state = to_dm_connector_state(drm_new_conn_state);
4899 dm_old_conn_state = to_dm_connector_state(drm_old_conn_state);
4900
4901 new_stream = create_stream_for_sink(aconnector,
4902 &new_crtc_state->mode,
4903 dm_new_conn_state);
4904
4905 /*
4906 * we can have no stream on ACTION_SET if a display
4907 * was disconnected during S3, in this case it not and
4908 * error, the OS will be updated after detection, and
4909 * do the right thing on next atomic commit
4910 */
4911
4912 if (!new_stream) {
4913 DRM_DEBUG_DRIVER("%s: Failed to create new stream for crtc %d\n",
4914 __func__, acrtc->base.base.id);
4915 break;
4916 }
4917
4918 if (dc_is_stream_unchanged(new_stream, dm_old_crtc_state->stream) &&
4919 dc_is_stream_scaling_unchanged(new_stream, dm_old_crtc_state->stream)) {
4920 new_crtc_state->mode_changed = false;
4921 DRM_DEBUG_DRIVER("Mode change not required, setting mode_changed to %d",
4922 new_crtc_state->mode_changed);
4923 }
4924 }
4925
4926 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
4927 goto next_crtc;
4928
4929 DRM_DEBUG_DRIVER(
4930 "amdgpu_crtc id:%d crtc_state_flags: enable:%d, active:%d, "
4931 "planes_changed:%d, mode_changed:%d,active_changed:%d,"
4932 "connectors_changed:%d\n",
4933 acrtc->crtc_id,
4934 new_crtc_state->enable,
4935 new_crtc_state->active,
4936 new_crtc_state->planes_changed,
4937 new_crtc_state->mode_changed,
4938 new_crtc_state->active_changed,
4939 new_crtc_state->connectors_changed);
4940
4941 /* Remove stream for any changed/disabled CRTC */
4942 if (!enable) {
4943
4944 if (!dm_old_crtc_state->stream)
4945 goto next_crtc;
4946
4947 DRM_DEBUG_DRIVER("Disabling DRM crtc: %d\n",
4948 crtc->base.id);
4949
4950 /* i.e. reset mode */
4951 if (dc_remove_stream_from_ctx(
4952 dc,
4953 dm_state->context,
4954 dm_old_crtc_state->stream) != DC_OK) {
4955 ret = -EINVAL;
4956 goto fail;
4957 }
4958
4959 dc_stream_release(dm_old_crtc_state->stream);
4960 dm_new_crtc_state->stream = NULL;
4961
4962 *lock_and_validation_needed = true;
4963
4964 } else {/* Add stream for any updated/enabled CRTC */
4965 /*
4966 * Quick fix to prevent NULL pointer on new_stream when
4967 * added MST connectors not found in existing crtc_state in the chained mode
4968 * TODO: need to dig out the root cause of that
4969 */
4970 if (!aconnector || (!aconnector->dc_sink && aconnector->mst_port))
4971 goto next_crtc;
4972
4973 if (modereset_required(new_crtc_state))
4974 goto next_crtc;
4975
4976 if (modeset_required(new_crtc_state, new_stream,
4977 dm_old_crtc_state->stream)) {
4978
4979 WARN_ON(dm_new_crtc_state->stream);
4980
4981 dm_new_crtc_state->stream = new_stream;
4982
4983 dc_stream_retain(new_stream);
4984
4985 DRM_DEBUG_DRIVER("Enabling DRM crtc: %d\n",
4986 crtc->base.id);
4987
4988 if (dc_add_stream_to_ctx(
4989 dc,
4990 dm_state->context,
4991 dm_new_crtc_state->stream) != DC_OK) {
4992 ret = -EINVAL;
4993 goto fail;
4994 }
4995
4996 *lock_and_validation_needed = true;
4997 }
4998 }
4999
5000 next_crtc:
5001 /* Release extra reference */
5002 if (new_stream)
5003 dc_stream_release(new_stream);
5004
5005 /*
5006 * We want to do dc stream updates that do not require a
5007 * full modeset below.
5008 */
5009 if (!(enable && aconnector && new_crtc_state->enable &&
5010 new_crtc_state->active))
5011 continue;
5012 /*
5013 * Given above conditions, the dc state cannot be NULL because:
5014 * 1. We're in the process of enabling CRTCs (just been added
5015 * to the dc context, or already is on the context)
5016 * 2. Has a valid connector attached, and
5017 * 3. Is currently active and enabled.
5018 * => The dc stream state currently exists.
5019 */
5020 BUG_ON(dm_new_crtc_state->stream == NULL);
5021
5022 /* Scaling or underscan settings */
5023 if (is_scaling_state_different(dm_old_conn_state, dm_new_conn_state))
5024 update_stream_scaling_settings(
5025 &new_crtc_state->mode, dm_new_conn_state, dm_new_crtc_state->stream);
5026
5027 /*
5028 * Color management settings. We also update color properties
5029 * when a modeset is needed, to ensure it gets reprogrammed.
5030 */
5031 if (dm_new_crtc_state->base.color_mgmt_changed ||
5032 drm_atomic_crtc_needs_modeset(new_crtc_state)) {
5033 ret = amdgpu_dm_set_regamma_lut(dm_new_crtc_state);
5034 if (ret)
5035 goto fail;
5036 amdgpu_dm_set_ctm(dm_new_crtc_state);
5037 }
5038 }
5039
5040 return ret;
5041
5042 fail:
5043 if (new_stream)
5044 dc_stream_release(new_stream);
5045 return ret;
5046 }
5047
dm_update_planes_state(struct dc * dc,struct drm_atomic_state * state,bool enable,bool * lock_and_validation_needed)5048 static int dm_update_planes_state(struct dc *dc,
5049 struct drm_atomic_state *state,
5050 bool enable,
5051 bool *lock_and_validation_needed)
5052 {
5053 struct drm_crtc *new_plane_crtc, *old_plane_crtc;
5054 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
5055 struct drm_plane *plane;
5056 struct drm_plane_state *old_plane_state, *new_plane_state;
5057 struct dm_crtc_state *dm_new_crtc_state, *dm_old_crtc_state;
5058 struct dm_atomic_state *dm_state = to_dm_atomic_state(state);
5059 struct dm_plane_state *dm_new_plane_state, *dm_old_plane_state;
5060 int i ;
5061 /* TODO return page_flip_needed() function */
5062 bool pflip_needed = !state->allow_modeset;
5063 int ret = 0;
5064
5065
5066 /* Add new planes, in reverse order as DC expectation */
5067 for_each_oldnew_plane_in_state_reverse(state, plane, old_plane_state, new_plane_state, i) {
5068 new_plane_crtc = new_plane_state->crtc;
5069 old_plane_crtc = old_plane_state->crtc;
5070 dm_new_plane_state = to_dm_plane_state(new_plane_state);
5071 dm_old_plane_state = to_dm_plane_state(old_plane_state);
5072
5073 /*TODO Implement atomic check for cursor plane */
5074 if (plane->type == DRM_PLANE_TYPE_CURSOR)
5075 continue;
5076
5077 /* Remove any changed/removed planes */
5078 if (!enable) {
5079 if (pflip_needed &&
5080 plane->type != DRM_PLANE_TYPE_OVERLAY)
5081 continue;
5082
5083 if (!old_plane_crtc)
5084 continue;
5085
5086 old_crtc_state = drm_atomic_get_old_crtc_state(
5087 state, old_plane_crtc);
5088 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
5089
5090 if (!dm_old_crtc_state->stream)
5091 continue;
5092
5093 DRM_DEBUG_ATOMIC("Disabling DRM plane: %d on DRM crtc %d\n",
5094 plane->base.id, old_plane_crtc->base.id);
5095
5096 if (!dc_remove_plane_from_context(
5097 dc,
5098 dm_old_crtc_state->stream,
5099 dm_old_plane_state->dc_state,
5100 dm_state->context)) {
5101
5102 ret = EINVAL;
5103 return ret;
5104 }
5105
5106
5107 dc_plane_state_release(dm_old_plane_state->dc_state);
5108 dm_new_plane_state->dc_state = NULL;
5109
5110 *lock_and_validation_needed = true;
5111
5112 } else { /* Add new planes */
5113 struct dc_plane_state *dc_new_plane_state;
5114
5115 if (drm_atomic_plane_disabling(plane->state, new_plane_state))
5116 continue;
5117
5118 if (!new_plane_crtc)
5119 continue;
5120
5121 new_crtc_state = drm_atomic_get_new_crtc_state(state, new_plane_crtc);
5122 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
5123
5124 if (!dm_new_crtc_state->stream)
5125 continue;
5126
5127 if (pflip_needed &&
5128 plane->type != DRM_PLANE_TYPE_OVERLAY)
5129 continue;
5130
5131 WARN_ON(dm_new_plane_state->dc_state);
5132
5133 dc_new_plane_state = dc_create_plane_state(dc);
5134 if (!dc_new_plane_state)
5135 return -ENOMEM;
5136
5137 DRM_DEBUG_DRIVER("Enabling DRM plane: %d on DRM crtc %d\n",
5138 plane->base.id, new_plane_crtc->base.id);
5139
5140 ret = fill_plane_attributes(
5141 new_plane_crtc->dev->dev_private,
5142 dc_new_plane_state,
5143 new_plane_state,
5144 new_crtc_state);
5145 if (ret) {
5146 dc_plane_state_release(dc_new_plane_state);
5147 return ret;
5148 }
5149
5150 /*
5151 * Any atomic check errors that occur after this will
5152 * not need a release. The plane state will be attached
5153 * to the stream, and therefore part of the atomic
5154 * state. It'll be released when the atomic state is
5155 * cleaned.
5156 */
5157 if (!dc_add_plane_to_context(
5158 dc,
5159 dm_new_crtc_state->stream,
5160 dc_new_plane_state,
5161 dm_state->context)) {
5162
5163 dc_plane_state_release(dc_new_plane_state);
5164 return -EINVAL;
5165 }
5166
5167 dm_new_plane_state->dc_state = dc_new_plane_state;
5168
5169 /* Tell DC to do a full surface update every time there
5170 * is a plane change. Inefficient, but works for now.
5171 */
5172 dm_new_plane_state->dc_state->update_flags.bits.full_update = 1;
5173
5174 *lock_and_validation_needed = true;
5175 }
5176 }
5177
5178
5179 return ret;
5180 }
5181
amdgpu_dm_atomic_check(struct drm_device * dev,struct drm_atomic_state * state)5182 static int amdgpu_dm_atomic_check(struct drm_device *dev,
5183 struct drm_atomic_state *state)
5184 {
5185 struct amdgpu_device *adev = dev->dev_private;
5186 struct dc *dc = adev->dm.dc;
5187 struct dm_atomic_state *dm_state = to_dm_atomic_state(state);
5188 struct drm_connector *connector;
5189 struct drm_connector_state *old_con_state, *new_con_state;
5190 struct drm_crtc *crtc;
5191 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
5192 int ret, i;
5193
5194 /*
5195 * This bool will be set for true for any modeset/reset
5196 * or plane update which implies non fast surface update.
5197 */
5198 bool lock_and_validation_needed = false;
5199
5200 ret = drm_atomic_helper_check_modeset(dev, state);
5201 if (ret)
5202 goto fail;
5203
5204 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
5205 if (!drm_atomic_crtc_needs_modeset(new_crtc_state) &&
5206 !new_crtc_state->color_mgmt_changed)
5207 continue;
5208
5209 if (!new_crtc_state->enable)
5210 continue;
5211
5212 ret = drm_atomic_add_affected_connectors(state, crtc);
5213 if (ret)
5214 return ret;
5215
5216 ret = drm_atomic_add_affected_planes(state, crtc);
5217 if (ret)
5218 goto fail;
5219 }
5220
5221 dm_state->context = dc_create_state();
5222 ASSERT(dm_state->context);
5223 dc_resource_state_copy_construct_current(dc, dm_state->context);
5224
5225 /* Remove exiting planes if they are modified */
5226 ret = dm_update_planes_state(dc, state, false, &lock_and_validation_needed);
5227 if (ret) {
5228 goto fail;
5229 }
5230
5231 /* Disable all crtcs which require disable */
5232 ret = dm_update_crtcs_state(dc, state, false, &lock_and_validation_needed);
5233 if (ret) {
5234 goto fail;
5235 }
5236
5237 /* Enable all crtcs which require enable */
5238 ret = dm_update_crtcs_state(dc, state, true, &lock_and_validation_needed);
5239 if (ret) {
5240 goto fail;
5241 }
5242
5243 /* Add new/modified planes */
5244 ret = dm_update_planes_state(dc, state, true, &lock_and_validation_needed);
5245 if (ret) {
5246 goto fail;
5247 }
5248
5249 /* Run this here since we want to validate the streams we created */
5250 ret = drm_atomic_helper_check_planes(dev, state);
5251 if (ret)
5252 goto fail;
5253
5254 /* Check scaling and underscan changes*/
5255 /*TODO Removed scaling changes validation due to inability to commit
5256 * new stream into context w\o causing full reset. Need to
5257 * decide how to handle.
5258 */
5259 for_each_oldnew_connector_in_state(state, connector, old_con_state, new_con_state, i) {
5260 struct dm_connector_state *dm_old_con_state = to_dm_connector_state(old_con_state);
5261 struct dm_connector_state *dm_new_con_state = to_dm_connector_state(new_con_state);
5262 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(dm_new_con_state->base.crtc);
5263
5264 /* Skip any modesets/resets */
5265 if (!acrtc || drm_atomic_crtc_needs_modeset(
5266 drm_atomic_get_new_crtc_state(state, &acrtc->base)))
5267 continue;
5268
5269 /* Skip any thing not scale or underscan changes */
5270 if (!is_scaling_state_different(dm_new_con_state, dm_old_con_state))
5271 continue;
5272
5273 lock_and_validation_needed = true;
5274 }
5275
5276 /*
5277 * For full updates case when
5278 * removing/adding/updating streams on once CRTC while flipping
5279 * on another CRTC,
5280 * acquiring global lock will guarantee that any such full
5281 * update commit
5282 * will wait for completion of any outstanding flip using DRMs
5283 * synchronization events.
5284 */
5285
5286 if (lock_and_validation_needed) {
5287
5288 ret = do_aquire_global_lock(dev, state);
5289 if (ret)
5290 goto fail;
5291
5292 if (dc_validate_global_state(dc, dm_state->context) != DC_OK) {
5293 ret = -EINVAL;
5294 goto fail;
5295 }
5296 }
5297
5298 /* Must be success */
5299 WARN_ON(ret);
5300 return ret;
5301
5302 fail:
5303 if (ret == -EDEADLK)
5304 DRM_DEBUG_DRIVER("Atomic check stopped to avoid deadlock.\n");
5305 else if (ret == -EINTR || ret == -EAGAIN || ret == -ERESTARTSYS)
5306 DRM_DEBUG_DRIVER("Atomic check stopped due to signal.\n");
5307 else
5308 DRM_DEBUG_DRIVER("Atomic check failed with err: %d \n", ret);
5309
5310 return ret;
5311 }
5312
is_dp_capable_without_timing_msa(struct dc * dc,struct amdgpu_dm_connector * amdgpu_dm_connector)5313 static bool is_dp_capable_without_timing_msa(struct dc *dc,
5314 struct amdgpu_dm_connector *amdgpu_dm_connector)
5315 {
5316 uint8_t dpcd_data;
5317 bool capable = false;
5318
5319 if (amdgpu_dm_connector->dc_link &&
5320 dm_helpers_dp_read_dpcd(
5321 NULL,
5322 amdgpu_dm_connector->dc_link,
5323 DP_DOWN_STREAM_PORT_COUNT,
5324 &dpcd_data,
5325 sizeof(dpcd_data))) {
5326 capable = (dpcd_data & DP_MSA_TIMING_PAR_IGNORED) ? true:false;
5327 }
5328
5329 return capable;
5330 }
amdgpu_dm_add_sink_to_freesync_module(struct drm_connector * connector,struct edid * edid)5331 void amdgpu_dm_add_sink_to_freesync_module(struct drm_connector *connector,
5332 struct edid *edid)
5333 {
5334 int i;
5335 bool edid_check_required;
5336 struct detailed_timing *timing;
5337 struct detailed_non_pixel *data;
5338 struct detailed_data_monitor_range *range;
5339 struct amdgpu_dm_connector *amdgpu_dm_connector =
5340 to_amdgpu_dm_connector(connector);
5341 struct dm_connector_state *dm_con_state;
5342
5343 struct drm_device *dev = connector->dev;
5344 struct amdgpu_device *adev = dev->dev_private;
5345
5346 if (!connector->state) {
5347 DRM_ERROR("%s - Connector has no state", __func__);
5348 return;
5349 }
5350
5351 dm_con_state = to_dm_connector_state(connector->state);
5352
5353 edid_check_required = false;
5354 if (!amdgpu_dm_connector->dc_sink) {
5355 DRM_ERROR("dc_sink NULL, could not add free_sync module.\n");
5356 return;
5357 }
5358 if (!adev->dm.freesync_module)
5359 return;
5360 /*
5361 * if edid non zero restrict freesync only for dp and edp
5362 */
5363 if (edid) {
5364 if (amdgpu_dm_connector->dc_sink->sink_signal == SIGNAL_TYPE_DISPLAY_PORT
5365 || amdgpu_dm_connector->dc_sink->sink_signal == SIGNAL_TYPE_EDP) {
5366 edid_check_required = is_dp_capable_without_timing_msa(
5367 adev->dm.dc,
5368 amdgpu_dm_connector);
5369 }
5370 }
5371 dm_con_state->freesync_capable = false;
5372 if (edid_check_required == true && (edid->version > 1 ||
5373 (edid->version == 1 && edid->revision > 1))) {
5374 for (i = 0; i < 4; i++) {
5375
5376 timing = &edid->detailed_timings[i];
5377 data = &timing->data.other_data;
5378 range = &data->data.range;
5379 /*
5380 * Check if monitor has continuous frequency mode
5381 */
5382 if (data->type != EDID_DETAIL_MONITOR_RANGE)
5383 continue;
5384 /*
5385 * Check for flag range limits only. If flag == 1 then
5386 * no additional timing information provided.
5387 * Default GTF, GTF Secondary curve and CVT are not
5388 * supported
5389 */
5390 if (range->flags != 1)
5391 continue;
5392
5393 amdgpu_dm_connector->min_vfreq = range->min_vfreq;
5394 amdgpu_dm_connector->max_vfreq = range->max_vfreq;
5395 amdgpu_dm_connector->pixel_clock_mhz =
5396 range->pixel_clock_mhz * 10;
5397 break;
5398 }
5399
5400 if (amdgpu_dm_connector->max_vfreq -
5401 amdgpu_dm_connector->min_vfreq > 10) {
5402 amdgpu_dm_connector->caps.supported = true;
5403 amdgpu_dm_connector->caps.min_refresh_in_micro_hz =
5404 amdgpu_dm_connector->min_vfreq * 1000000;
5405 amdgpu_dm_connector->caps.max_refresh_in_micro_hz =
5406 amdgpu_dm_connector->max_vfreq * 1000000;
5407 dm_con_state->freesync_capable = true;
5408 }
5409 }
5410
5411 /*
5412 * TODO figure out how to notify user-mode or DRM of freesync caps
5413 * once we figure out how to deal with freesync in an upstreamable
5414 * fashion
5415 */
5416
5417 }
5418
amdgpu_dm_remove_sink_from_freesync_module(struct drm_connector * connector)5419 void amdgpu_dm_remove_sink_from_freesync_module(struct drm_connector *connector)
5420 {
5421 /*
5422 * TODO fill in once we figure out how to deal with freesync in
5423 * an upstreamable fashion
5424 */
5425 }
5426