2 * Copyright 2012-13 Advanced Micro Devices, Inc.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
26 #include <linux/types.h>
27 #include <linux/version.h>
30 #include <drm/drm_atomic_helper.h>
31 #include <drm/drm_fb_helper.h>
32 #include <drm/drm_atomic.h>
33 #include <drm/drm_edid.h>
37 #include "amdgpu_pm.h"
38 #include "dm_helpers.h"
39 #include "dm_services_types.h"
41 // We need to #undef FRAME_SIZE and DEPRECATED because they conflict
42 // with ptrace-abi.h's #define's of them.
48 #include "amdgpu_dm_types.h"
49 #include "amdgpu_dm_mst_types.h"
51 #include "modules/inc/mod_freesync.h"
53 #include "i2caux_interface.h"
55 struct dm_connector_state
{
56 struct drm_connector_state base
;
58 enum amdgpu_rmx_type scaling
;
59 uint8_t underscan_vborder
;
60 uint8_t underscan_hborder
;
61 bool underscan_enable
;
64 #define to_dm_connector_state(x)\
65 container_of((x), struct dm_connector_state, base)
68 void amdgpu_dm_encoder_destroy(struct drm_encoder
*encoder
)
70 drm_encoder_cleanup(encoder
);
74 static const struct drm_encoder_funcs amdgpu_dm_encoder_funcs
= {
75 .destroy
= amdgpu_dm_encoder_destroy
,
78 static void dm_set_cursor(
79 struct amdgpu_crtc
*amdgpu_crtc
,
84 struct dc_cursor_attributes attributes
;
85 struct dc_cursor_position position
;
86 struct drm_crtc
*crtc
= &amdgpu_crtc
->base
;
88 int xorigin
= 0, yorigin
= 0;
90 amdgpu_crtc
->cursor_width
= width
;
91 amdgpu_crtc
->cursor_height
= height
;
93 attributes
.address
.high_part
= upper_32_bits(gpu_addr
);
94 attributes
.address
.low_part
= lower_32_bits(gpu_addr
);
95 attributes
.width
= width
;
96 attributes
.height
= height
;
97 attributes
.color_format
= CURSOR_MODE_COLOR_PRE_MULTIPLIED_ALPHA
;
98 attributes
.rotation_angle
= 0;
99 attributes
.attribute_flags
.value
= 0;
101 attributes
.pitch
= attributes
.width
;
103 x
= amdgpu_crtc
->cursor_x
;
104 y
= amdgpu_crtc
->cursor_y
;
106 /* avivo cursor are offset into the total surface */
107 x
+= crtc
->primary
->state
->src_x
>> 16;
108 y
+= crtc
->primary
->state
->src_y
>> 16;
111 xorigin
= min(-x
, amdgpu_crtc
->max_cursor_width
- 1);
115 yorigin
= min(-y
, amdgpu_crtc
->max_cursor_height
- 1);
119 position
.enable
= true;
123 position
.x_hotspot
= xorigin
;
124 position
.y_hotspot
= yorigin
;
126 if (!dc_stream_set_cursor_attributes(
129 DRM_ERROR("DC failed to set cursor attributes\n");
132 if (!dc_stream_set_cursor_position(
135 DRM_ERROR("DC failed to set cursor position\n");
139 static int dm_crtc_cursor_set(
140 struct drm_crtc
*crtc
,
145 struct dc_cursor_position position
;
149 struct amdgpu_crtc
*amdgpu_crtc
= to_amdgpu_crtc(crtc
);
153 "%s: crtc_id=%d with size %d to %d \n",
155 amdgpu_crtc
->crtc_id
,
160 /* turn off cursor */
161 position
.enable
= false;
165 if (amdgpu_crtc
->stream
) {
166 /*set cursor visible false*/
167 dc_stream_set_cursor_position(
175 if ((width
> amdgpu_crtc
->max_cursor_width
) ||
176 (height
> amdgpu_crtc
->max_cursor_height
)) {
178 "%s: bad cursor width or height %d x %d\n",
185 /*program new cursor bo to hardware*/
186 dm_set_cursor(amdgpu_crtc
, address
, width
, height
);
193 static int dm_crtc_cursor_move(struct drm_crtc
*crtc
,
196 struct amdgpu_crtc
*amdgpu_crtc
= to_amdgpu_crtc(crtc
);
197 int xorigin
= 0, yorigin
= 0;
198 struct dc_cursor_position position
;
200 amdgpu_crtc
->cursor_x
= x
;
201 amdgpu_crtc
->cursor_y
= y
;
203 /* avivo cursor are offset into the total surface */
204 x
+= crtc
->primary
->state
->src_x
>> 16;
205 y
+= crtc
->primary
->state
->src_y
>> 16;
208 * TODO: for cursor debugging unguard the following
212 "%s: x %d y %d c->x %d c->y %d\n",
221 xorigin
= min(-x
, amdgpu_crtc
->max_cursor_width
- 1);
225 yorigin
= min(-y
, amdgpu_crtc
->max_cursor_height
- 1);
229 position
.enable
= true;
233 position
.x_hotspot
= xorigin
;
234 position
.y_hotspot
= yorigin
;
236 if (amdgpu_crtc
->stream
) {
237 if (!dc_stream_set_cursor_position(
240 DRM_ERROR("DC failed to set cursor position\n");
248 static bool fill_rects_from_plane_state(
249 const struct drm_plane_state
*state
,
250 struct dc_surface
*surface
)
252 surface
->src_rect
.x
= state
->src_x
>> 16;
253 surface
->src_rect
.y
= state
->src_y
>> 16;
254 /*we ignore for now mantissa and do not to deal with floating pixels :(*/
255 surface
->src_rect
.width
= state
->src_w
>> 16;
257 if (surface
->src_rect
.width
== 0)
260 surface
->src_rect
.height
= state
->src_h
>> 16;
261 if (surface
->src_rect
.height
== 0)
264 surface
->dst_rect
.x
= state
->crtc_x
;
265 surface
->dst_rect
.y
= state
->crtc_y
;
267 if (state
->crtc_w
== 0)
270 surface
->dst_rect
.width
= state
->crtc_w
;
272 if (state
->crtc_h
== 0)
275 surface
->dst_rect
.height
= state
->crtc_h
;
277 surface
->clip_rect
= surface
->dst_rect
;
279 switch (state
->rotation
& DRM_MODE_ROTATE_MASK
) {
280 case DRM_MODE_ROTATE_0
:
281 surface
->rotation
= ROTATION_ANGLE_0
;
283 case DRM_MODE_ROTATE_90
:
284 surface
->rotation
= ROTATION_ANGLE_90
;
286 case DRM_MODE_ROTATE_180
:
287 surface
->rotation
= ROTATION_ANGLE_180
;
289 case DRM_MODE_ROTATE_270
:
290 surface
->rotation
= ROTATION_ANGLE_270
;
293 surface
->rotation
= ROTATION_ANGLE_0
;
299 static bool get_fb_info(
300 const struct amdgpu_framebuffer
*amdgpu_fb
,
301 uint64_t *tiling_flags
,
302 uint64_t *fb_location
)
304 struct amdgpu_bo
*rbo
= gem_to_amdgpu_bo(amdgpu_fb
->obj
);
305 int r
= amdgpu_bo_reserve(rbo
, false);
306 if (unlikely(r
!= 0)){
307 DRM_ERROR("Unable to reserve buffer\n");
312 *fb_location
= amdgpu_bo_gpu_offset(rbo
);
315 amdgpu_bo_get_tiling_flags(rbo
, tiling_flags
);
317 amdgpu_bo_unreserve(rbo
);
321 static void fill_plane_attributes_from_fb(
322 struct amdgpu_device
*adev
,
323 struct dc_surface
*surface
,
324 const struct amdgpu_framebuffer
*amdgpu_fb
, bool addReq
)
326 uint64_t tiling_flags
;
327 uint64_t fb_location
= 0;
329 const struct drm_framebuffer
*fb
= &amdgpu_fb
->base
;
330 struct drm_format_name_buf format_name
;
335 addReq
== true ? &fb_location
:NULL
);
338 switch (fb
->format
->format
) {
340 surface
->format
= SURFACE_PIXEL_FORMAT_GRPH_PALETA_256_COLORS
;
342 case DRM_FORMAT_RGB565
:
343 surface
->format
= SURFACE_PIXEL_FORMAT_GRPH_RGB565
;
345 case DRM_FORMAT_XRGB8888
:
346 case DRM_FORMAT_ARGB8888
:
347 surface
->format
= SURFACE_PIXEL_FORMAT_GRPH_ARGB8888
;
349 case DRM_FORMAT_XRGB2101010
:
350 case DRM_FORMAT_ARGB2101010
:
351 surface
->format
= SURFACE_PIXEL_FORMAT_GRPH_ARGB2101010
;
353 case DRM_FORMAT_XBGR2101010
:
354 case DRM_FORMAT_ABGR2101010
:
355 surface
->format
= SURFACE_PIXEL_FORMAT_GRPH_ABGR2101010
;
357 case DRM_FORMAT_NV21
:
358 surface
->format
= SURFACE_PIXEL_FORMAT_VIDEO_420_YCbCr
;
360 case DRM_FORMAT_NV12
:
361 surface
->format
= SURFACE_PIXEL_FORMAT_VIDEO_420_YCrCb
;
364 DRM_ERROR("Unsupported screen format %s\n",
365 drm_get_format_name(fb
->format
->format
, &format_name
));
369 if (surface
->format
< SURFACE_PIXEL_FORMAT_VIDEO_BEGIN
) {
370 surface
->address
.type
= PLN_ADDR_TYPE_GRAPHICS
;
371 surface
->address
.grph
.addr
.low_part
= lower_32_bits(fb_location
);
372 surface
->address
.grph
.addr
.high_part
= upper_32_bits(fb_location
);
373 surface
->plane_size
.grph
.surface_size
.x
= 0;
374 surface
->plane_size
.grph
.surface_size
.y
= 0;
375 surface
->plane_size
.grph
.surface_size
.width
= fb
->width
;
376 surface
->plane_size
.grph
.surface_size
.height
= fb
->height
;
377 surface
->plane_size
.grph
.surface_pitch
=
378 fb
->pitches
[0] / fb
->format
->cpp
[0];
379 /* TODO: unhardcode */
380 surface
->color_space
= COLOR_SPACE_SRGB
;
383 awidth
= ALIGN(fb
->width
, 64);
384 surface
->address
.type
= PLN_ADDR_TYPE_VIDEO_PROGRESSIVE
;
385 surface
->address
.video_progressive
.luma_addr
.low_part
386 = lower_32_bits(fb_location
);
387 surface
->address
.video_progressive
.chroma_addr
.low_part
388 = lower_32_bits(fb_location
) +
389 (awidth
* fb
->height
);
390 surface
->plane_size
.video
.luma_size
.x
= 0;
391 surface
->plane_size
.video
.luma_size
.y
= 0;
392 surface
->plane_size
.video
.luma_size
.width
= awidth
;
393 surface
->plane_size
.video
.luma_size
.height
= fb
->height
;
394 /* TODO: unhardcode */
395 surface
->plane_size
.video
.luma_pitch
= awidth
;
397 surface
->plane_size
.video
.chroma_size
.x
= 0;
398 surface
->plane_size
.video
.chroma_size
.y
= 0;
399 surface
->plane_size
.video
.chroma_size
.width
= awidth
;
400 surface
->plane_size
.video
.chroma_size
.height
= fb
->height
;
401 surface
->plane_size
.video
.chroma_pitch
= awidth
/ 2;
403 /* TODO: unhardcode */
404 surface
->color_space
= COLOR_SPACE_YCBCR709
;
407 memset(&surface
->tiling_info
, 0, sizeof(surface
->tiling_info
));
409 /* Fill GFX params */
410 if (AMDGPU_TILING_GET(tiling_flags
, ARRAY_MODE
) == DC_ARRAY_2D_TILED_THIN1
)
412 unsigned bankw
, bankh
, mtaspect
, tile_split
, num_banks
;
414 bankw
= AMDGPU_TILING_GET(tiling_flags
, BANK_WIDTH
);
415 bankh
= AMDGPU_TILING_GET(tiling_flags
, BANK_HEIGHT
);
416 mtaspect
= AMDGPU_TILING_GET(tiling_flags
, MACRO_TILE_ASPECT
);
417 tile_split
= AMDGPU_TILING_GET(tiling_flags
, TILE_SPLIT
);
418 num_banks
= AMDGPU_TILING_GET(tiling_flags
, NUM_BANKS
);
420 /* XXX fix me for VI */
421 surface
->tiling_info
.gfx8
.num_banks
= num_banks
;
422 surface
->tiling_info
.gfx8
.array_mode
=
423 DC_ARRAY_2D_TILED_THIN1
;
424 surface
->tiling_info
.gfx8
.tile_split
= tile_split
;
425 surface
->tiling_info
.gfx8
.bank_width
= bankw
;
426 surface
->tiling_info
.gfx8
.bank_height
= bankh
;
427 surface
->tiling_info
.gfx8
.tile_aspect
= mtaspect
;
428 surface
->tiling_info
.gfx8
.tile_mode
=
429 DC_ADDR_SURF_MICRO_TILING_DISPLAY
;
430 } else if (AMDGPU_TILING_GET(tiling_flags
, ARRAY_MODE
)
431 == DC_ARRAY_1D_TILED_THIN1
) {
432 surface
->tiling_info
.gfx8
.array_mode
= DC_ARRAY_1D_TILED_THIN1
;
435 surface
->tiling_info
.gfx8
.pipe_config
=
436 AMDGPU_TILING_GET(tiling_flags
, PIPE_CONFIG
);
438 if (adev
->asic_type
== CHIP_VEGA10
||
439 adev
->asic_type
== CHIP_RAVEN
) {
440 /* Fill GFX9 params */
441 surface
->tiling_info
.gfx9
.num_pipes
=
442 adev
->gfx
.config
.gb_addr_config_fields
.num_pipes
;
443 surface
->tiling_info
.gfx9
.num_banks
=
444 adev
->gfx
.config
.gb_addr_config_fields
.num_banks
;
445 surface
->tiling_info
.gfx9
.pipe_interleave
=
446 adev
->gfx
.config
.gb_addr_config_fields
.pipe_interleave_size
;
447 surface
->tiling_info
.gfx9
.num_shader_engines
=
448 adev
->gfx
.config
.gb_addr_config_fields
.num_se
;
449 surface
->tiling_info
.gfx9
.max_compressed_frags
=
450 adev
->gfx
.config
.gb_addr_config_fields
.max_compress_frags
;
451 surface
->tiling_info
.gfx9
.num_rb_per_se
=
452 adev
->gfx
.config
.gb_addr_config_fields
.num_rb_per_se
;
453 surface
->tiling_info
.gfx9
.swizzle
=
454 AMDGPU_TILING_GET(tiling_flags
, SWIZZLE_MODE
);
455 surface
->tiling_info
.gfx9
.shaderEnable
= 1;
458 surface
->visible
= true;
459 surface
->scaling_quality
.h_taps_c
= 0;
460 surface
->scaling_quality
.v_taps_c
= 0;
462 /* is this needed? is surface zeroed at allocation? */
463 surface
->scaling_quality
.h_taps
= 0;
464 surface
->scaling_quality
.v_taps
= 0;
465 surface
->stereo_format
= PLANE_STEREO_FORMAT_NONE
;
469 #define NUM_OF_RAW_GAMMA_RAMP_RGB_256 256
471 static void fill_gamma_from_crtc(
472 const struct drm_crtc
*crtc
,
473 struct dc_surface
*dc_surface
)
476 struct dc_gamma
*gamma
;
477 struct drm_crtc_state
*state
= crtc
->state
;
478 struct drm_color_lut
*lut
= (struct drm_color_lut
*) state
->gamma_lut
->data
;
480 gamma
= dc_create_gamma();
485 for (i
= 0; i
< NUM_OF_RAW_GAMMA_RAMP_RGB_256
; i
++) {
486 gamma
->red
[i
] = lut
[i
].red
;
487 gamma
->green
[i
] = lut
[i
].green
;
488 gamma
->blue
[i
] = lut
[i
].blue
;
491 dc_surface
->gamma_correction
= gamma
;
494 static void fill_plane_attributes(
495 struct amdgpu_device
*adev
,
496 struct dc_surface
*surface
,
497 struct drm_plane_state
*state
, bool addrReq
)
499 const struct amdgpu_framebuffer
*amdgpu_fb
=
500 to_amdgpu_framebuffer(state
->fb
);
501 const struct drm_crtc
*crtc
= state
->crtc
;
502 struct dc_transfer_func
*input_tf
;
504 fill_rects_from_plane_state(state
, surface
);
505 fill_plane_attributes_from_fb(
506 crtc
->dev
->dev_private
,
511 input_tf
= dc_create_transfer_func();
513 if (input_tf
== NULL
)
516 input_tf
->type
= TF_TYPE_PREDEFINED
;
517 input_tf
->tf
= TRANSFER_FUNCTION_SRGB
;
519 surface
->in_transfer_func
= input_tf
;
521 /* In case of gamma set, update gamma value */
522 if (state
->crtc
->state
->gamma_lut
) {
523 fill_gamma_from_crtc(crtc
, surface
);
527 /*****************************************************************************/
529 struct amdgpu_connector
*aconnector_from_drm_crtc_id(
530 const struct drm_crtc
*crtc
)
532 struct drm_device
*dev
= crtc
->dev
;
533 struct drm_connector
*connector
;
534 struct amdgpu_crtc
*acrtc
= to_amdgpu_crtc(crtc
);
535 struct amdgpu_connector
*aconnector
;
537 list_for_each_entry(connector
,
538 &dev
->mode_config
.connector_list
, head
) {
540 aconnector
= to_amdgpu_connector(connector
);
542 if (aconnector
->base
.state
->crtc
!= &acrtc
->base
)
545 /* Found the connector */
549 /* If we get here, not found. */
553 static void update_stream_scaling_settings(
554 const struct drm_display_mode
*mode
,
555 const struct dm_connector_state
*dm_state
,
556 struct dc_stream
*stream
)
558 enum amdgpu_rmx_type rmx_type
;
560 struct rect src
= { 0 }; /* viewport in composition space*/
561 struct rect dst
= { 0 }; /* stream addressable area */
563 /* no mode. nothing to be done */
567 /* Full screen scaling by default */
568 src
.width
= mode
->hdisplay
;
569 src
.height
= mode
->vdisplay
;
570 dst
.width
= stream
->timing
.h_addressable
;
571 dst
.height
= stream
->timing
.v_addressable
;
573 rmx_type
= dm_state
->scaling
;
574 if (rmx_type
== RMX_ASPECT
|| rmx_type
== RMX_OFF
) {
575 if (src
.width
* dst
.height
<
576 src
.height
* dst
.width
) {
577 /* height needs less upscaling/more downscaling */
578 dst
.width
= src
.width
*
579 dst
.height
/ src
.height
;
581 /* width needs less upscaling/more downscaling */
582 dst
.height
= src
.height
*
583 dst
.width
/ src
.width
;
585 } else if (rmx_type
== RMX_CENTER
) {
589 dst
.x
= (stream
->timing
.h_addressable
- dst
.width
) / 2;
590 dst
.y
= (stream
->timing
.v_addressable
- dst
.height
) / 2;
592 if (dm_state
->underscan_enable
) {
593 dst
.x
+= dm_state
->underscan_hborder
/ 2;
594 dst
.y
+= dm_state
->underscan_vborder
/ 2;
595 dst
.width
-= dm_state
->underscan_hborder
;
596 dst
.height
-= dm_state
->underscan_vborder
;
602 DRM_DEBUG_KMS("Destination Rectangle x:%d y:%d width:%d height:%d\n",
603 dst
.x
, dst
.y
, dst
.width
, dst
.height
);
607 static void add_surface(struct dc
*dc
,
608 struct drm_crtc
*crtc
,
609 struct drm_plane
*plane
,
610 const struct dc_surface
**dc_surfaces
)
612 struct dc_surface
*dc_surface
;
613 struct amdgpu_crtc
*acrtc
= to_amdgpu_crtc(crtc
);
614 const struct dc_stream
*dc_stream
= acrtc
->stream
;
617 spin_lock_irqsave(&crtc
->dev
->event_lock
, flags
);
618 if (acrtc
->pflip_status
!= AMDGPU_FLIP_NONE
) {
619 DRM_ERROR("add_surface: acrtc %d, already busy\n",
621 spin_unlock_irqrestore(&crtc
->dev
->event_lock
, flags
);
622 /* In comit tail framework this cannot happen */
625 spin_unlock_irqrestore(&crtc
->dev
->event_lock
, flags
);
629 "%s: Failed to obtain stream on crtc (%d)!\n",
635 dc_surface
= dc_create_surface(dc
);
639 "%s: Failed to create a surface!\n",
644 /* Surface programming */
645 fill_plane_attributes(
646 crtc
->dev
->dev_private
,
651 *dc_surfaces
= dc_surface
;
657 static enum dc_color_depth
convert_color_depth_from_display_info(
658 const struct drm_connector
*connector
)
660 uint32_t bpc
= connector
->display_info
.bpc
;
662 /* Limited color depth to 8bit
663 * TODO: Still need to handle deep color*/
669 /* Temporary Work around, DRM don't parse color depth for
670 * EDID revision before 1.4
671 * TODO: Fix edid parsing
673 return COLOR_DEPTH_888
;
675 return COLOR_DEPTH_666
;
677 return COLOR_DEPTH_888
;
679 return COLOR_DEPTH_101010
;
681 return COLOR_DEPTH_121212
;
683 return COLOR_DEPTH_141414
;
685 return COLOR_DEPTH_161616
;
687 return COLOR_DEPTH_UNDEFINED
;
691 static enum dc_aspect_ratio
get_aspect_ratio(
692 const struct drm_display_mode
*mode_in
)
694 int32_t width
= mode_in
->crtc_hdisplay
* 9;
695 int32_t height
= mode_in
->crtc_vdisplay
* 16;
696 if ((width
- height
) < 10 && (width
- height
) > -10)
697 return ASPECT_RATIO_16_9
;
699 return ASPECT_RATIO_4_3
;
702 static enum dc_color_space
get_output_color_space(
703 const struct dc_crtc_timing
*dc_crtc_timing
)
705 enum dc_color_space color_space
= COLOR_SPACE_SRGB
;
707 switch (dc_crtc_timing
->pixel_encoding
) {
708 case PIXEL_ENCODING_YCBCR422
:
709 case PIXEL_ENCODING_YCBCR444
:
710 case PIXEL_ENCODING_YCBCR420
:
713 * 27030khz is the separation point between HDTV and SDTV
714 * according to HDMI spec, we use YCbCr709 and YCbCr601
717 if (dc_crtc_timing
->pix_clk_khz
> 27030) {
718 if (dc_crtc_timing
->flags
.Y_ONLY
)
720 COLOR_SPACE_YCBCR709_LIMITED
;
722 color_space
= COLOR_SPACE_YCBCR709
;
724 if (dc_crtc_timing
->flags
.Y_ONLY
)
726 COLOR_SPACE_YCBCR601_LIMITED
;
728 color_space
= COLOR_SPACE_YCBCR601
;
733 case PIXEL_ENCODING_RGB
:
734 color_space
= COLOR_SPACE_SRGB
;
745 /*****************************************************************************/
747 static void fill_stream_properties_from_drm_display_mode(
748 struct dc_stream
*stream
,
749 const struct drm_display_mode
*mode_in
,
750 const struct drm_connector
*connector
)
752 struct dc_crtc_timing
*timing_out
= &stream
->timing
;
753 memset(timing_out
, 0, sizeof(struct dc_crtc_timing
));
755 timing_out
->h_border_left
= 0;
756 timing_out
->h_border_right
= 0;
757 timing_out
->v_border_top
= 0;
758 timing_out
->v_border_bottom
= 0;
759 /* TODO: un-hardcode */
761 if ((connector
->display_info
.color_formats
& DRM_COLOR_FORMAT_YCRCB444
)
762 && stream
->sink
->sink_signal
== SIGNAL_TYPE_HDMI_TYPE_A
)
763 timing_out
->pixel_encoding
= PIXEL_ENCODING_YCBCR444
;
765 timing_out
->pixel_encoding
= PIXEL_ENCODING_RGB
;
767 timing_out
->timing_3d_format
= TIMING_3D_FORMAT_NONE
;
768 timing_out
->display_color_depth
= convert_color_depth_from_display_info(
770 timing_out
->scan_type
= SCANNING_TYPE_NODATA
;
771 timing_out
->hdmi_vic
= 0;
772 timing_out
->vic
= drm_match_cea_mode(mode_in
);
774 timing_out
->h_addressable
= mode_in
->crtc_hdisplay
;
775 timing_out
->h_total
= mode_in
->crtc_htotal
;
776 timing_out
->h_sync_width
=
777 mode_in
->crtc_hsync_end
- mode_in
->crtc_hsync_start
;
778 timing_out
->h_front_porch
=
779 mode_in
->crtc_hsync_start
- mode_in
->crtc_hdisplay
;
780 timing_out
->v_total
= mode_in
->crtc_vtotal
;
781 timing_out
->v_addressable
= mode_in
->crtc_vdisplay
;
782 timing_out
->v_front_porch
=
783 mode_in
->crtc_vsync_start
- mode_in
->crtc_vdisplay
;
784 timing_out
->v_sync_width
=
785 mode_in
->crtc_vsync_end
- mode_in
->crtc_vsync_start
;
786 timing_out
->pix_clk_khz
= mode_in
->crtc_clock
;
787 timing_out
->aspect_ratio
= get_aspect_ratio(mode_in
);
788 if (mode_in
->flags
& DRM_MODE_FLAG_PHSYNC
)
789 timing_out
->flags
.HSYNC_POSITIVE_POLARITY
= 1;
790 if (mode_in
->flags
& DRM_MODE_FLAG_PVSYNC
)
791 timing_out
->flags
.VSYNC_POSITIVE_POLARITY
= 1;
793 stream
->output_color_space
= get_output_color_space(timing_out
);
796 struct dc_transfer_func
*tf
= dc_create_transfer_func();
797 tf
->type
= TF_TYPE_PREDEFINED
;
798 tf
->tf
= TRANSFER_FUNCTION_SRGB
;
799 stream
->out_transfer_func
= tf
;
803 static void fill_audio_info(
804 struct audio_info
*audio_info
,
805 const struct drm_connector
*drm_connector
,
806 const struct dc_sink
*dc_sink
)
809 int cea_revision
= 0;
810 const struct dc_edid_caps
*edid_caps
= &dc_sink
->edid_caps
;
812 audio_info
->manufacture_id
= edid_caps
->manufacturer_id
;
813 audio_info
->product_id
= edid_caps
->product_id
;
815 cea_revision
= drm_connector
->display_info
.cea_rev
;
817 while (i
< AUDIO_INFO_DISPLAY_NAME_SIZE_IN_CHARS
&&
818 edid_caps
->display_name
[i
]) {
819 audio_info
->display_name
[i
] = edid_caps
->display_name
[i
];
823 if(cea_revision
>= 3) {
824 audio_info
->mode_count
= edid_caps
->audio_mode_count
;
826 for (i
= 0; i
< audio_info
->mode_count
; ++i
) {
827 audio_info
->modes
[i
].format_code
=
828 (enum audio_format_code
)
829 (edid_caps
->audio_modes
[i
].format_code
);
830 audio_info
->modes
[i
].channel_count
=
831 edid_caps
->audio_modes
[i
].channel_count
;
832 audio_info
->modes
[i
].sample_rates
.all
=
833 edid_caps
->audio_modes
[i
].sample_rate
;
834 audio_info
->modes
[i
].sample_size
=
835 edid_caps
->audio_modes
[i
].sample_size
;
839 audio_info
->flags
.all
= edid_caps
->speaker_flags
;
841 /* TODO: We only check for the progressive mode, check for interlace mode too */
842 if(drm_connector
->latency_present
[0]) {
843 audio_info
->video_latency
= drm_connector
->video_latency
[0];
844 audio_info
->audio_latency
= drm_connector
->audio_latency
[0];
847 /* TODO: For DP, video and audio latency should be calculated from DPCD caps */
851 static void copy_crtc_timing_for_drm_display_mode(
852 const struct drm_display_mode
*src_mode
,
853 struct drm_display_mode
*dst_mode
)
855 dst_mode
->crtc_hdisplay
= src_mode
->crtc_hdisplay
;
856 dst_mode
->crtc_vdisplay
= src_mode
->crtc_vdisplay
;
857 dst_mode
->crtc_clock
= src_mode
->crtc_clock
;
858 dst_mode
->crtc_hblank_start
= src_mode
->crtc_hblank_start
;
859 dst_mode
->crtc_hblank_end
= src_mode
->crtc_hblank_end
;
860 dst_mode
->crtc_hsync_start
= src_mode
->crtc_hsync_start
;
861 dst_mode
->crtc_hsync_end
= src_mode
->crtc_hsync_end
;
862 dst_mode
->crtc_htotal
= src_mode
->crtc_htotal
;
863 dst_mode
->crtc_hskew
= src_mode
->crtc_hskew
;
864 dst_mode
->crtc_vblank_start
= src_mode
->crtc_vblank_start
;
865 dst_mode
->crtc_vblank_end
= src_mode
->crtc_vblank_end
;
866 dst_mode
->crtc_vsync_start
= src_mode
->crtc_vsync_start
;
867 dst_mode
->crtc_vsync_end
= src_mode
->crtc_vsync_end
;
868 dst_mode
->crtc_vtotal
= src_mode
->crtc_vtotal
;
871 static void decide_crtc_timing_for_drm_display_mode(
872 struct drm_display_mode
*drm_mode
,
873 const struct drm_display_mode
*native_mode
,
877 copy_crtc_timing_for_drm_display_mode(native_mode
, drm_mode
);
878 } else if (native_mode
->clock
== drm_mode
->clock
&&
879 native_mode
->htotal
== drm_mode
->htotal
&&
880 native_mode
->vtotal
== drm_mode
->vtotal
) {
881 copy_crtc_timing_for_drm_display_mode(native_mode
, drm_mode
);
883 /* no scaling nor amdgpu inserted, no need to patch */
887 static struct dc_stream
*create_stream_for_sink(
888 struct amdgpu_connector
*aconnector
,
889 const struct drm_display_mode
*drm_mode
,
890 const struct dm_connector_state
*dm_state
)
892 struct drm_display_mode
*preferred_mode
= NULL
;
893 const struct drm_connector
*drm_connector
;
894 struct dc_stream
*stream
= NULL
;
895 struct drm_display_mode mode
= *drm_mode
;
896 bool native_mode_found
= false;
898 if (NULL
== aconnector
) {
899 DRM_ERROR("aconnector is NULL!\n");
900 goto drm_connector_null
;
903 if (NULL
== dm_state
) {
904 DRM_ERROR("dm_state is NULL!\n");
908 drm_connector
= &aconnector
->base
;
909 stream
= dc_create_stream_for_sink(aconnector
->dc_sink
);
911 if (NULL
== stream
) {
912 DRM_ERROR("Failed to create stream for sink!\n");
913 goto stream_create_fail
;
916 list_for_each_entry(preferred_mode
, &aconnector
->base
.modes
, head
) {
917 /* Search for preferred mode */
918 if (preferred_mode
->type
& DRM_MODE_TYPE_PREFERRED
) {
919 native_mode_found
= true;
923 if (!native_mode_found
)
924 preferred_mode
= list_first_entry_or_null(
925 &aconnector
->base
.modes
,
926 struct drm_display_mode
,
929 if (NULL
== preferred_mode
) {
930 /* This may not be an error, the use case is when we we have no
931 * usermode calls to reset and set mode upon hotplug. In this
932 * case, we call set mode ourselves to restore the previous mode
933 * and the modelist may not be filled in in time.
935 DRM_INFO("No preferred mode found\n");
937 decide_crtc_timing_for_drm_display_mode(
938 &mode
, preferred_mode
,
939 dm_state
->scaling
!= RMX_OFF
);
942 fill_stream_properties_from_drm_display_mode(stream
,
943 &mode
, &aconnector
->base
);
944 update_stream_scaling_settings(&mode
, dm_state
, stream
);
949 aconnector
->dc_sink
);
957 void amdgpu_dm_crtc_destroy(struct drm_crtc
*crtc
)
959 drm_crtc_cleanup(crtc
);
963 static void dm_crtc_destroy_state(struct drm_crtc
*crtc
,
964 struct drm_crtc_state
*state
)
966 struct dm_crtc_state
*cur
= to_dm_crtc_state(state
);
968 if (cur
->dc_stream
) {
969 /* TODO Destroy dc_stream objects are stream object is flattened */
970 dm_free(cur
->dc_stream
);
974 __drm_atomic_helper_crtc_destroy_state(state
);
980 static void dm_crtc_reset_state(struct drm_crtc
*crtc
)
982 struct dm_crtc_state
*state
;
985 dm_crtc_destroy_state(crtc
, crtc
->state
);
987 state
= kzalloc(sizeof(*state
), GFP_KERNEL
);
992 crtc
->state
= &state
->base
;
993 crtc
->state
->crtc
= crtc
;
995 state
->dc_stream
= dm_alloc(sizeof(*state
->dc_stream
));
996 WARN_ON(!state
->dc_stream
);
999 static struct drm_crtc_state
*
1000 dm_crtc_duplicate_state(struct drm_crtc
*crtc
)
1002 struct dm_crtc_state
*state
, *cur
;
1003 struct dc_stream
*dc_stream
;
1005 if (WARN_ON(!crtc
->state
))
1008 cur
= to_dm_crtc_state(crtc
->state
);
1009 if (WARN_ON(!cur
->dc_stream
))
1012 dc_stream
= dm_alloc(sizeof(*dc_stream
));
1013 if (WARN_ON(!dc_stream
))
1016 state
= dm_alloc(sizeof(*state
));
1017 if (WARN_ON(!state
)) {
1022 __drm_atomic_helper_crtc_duplicate_state(crtc
, &state
->base
);
1024 state
->dc_stream
= dc_stream
;
1026 /* TODO Duplicate dc_stream after objects are stream object is flattened */
1028 return &state
->base
;
1031 /* Implemented only the options currently availible for the driver */
1032 static const struct drm_crtc_funcs amdgpu_dm_crtc_funcs
= {
1033 .reset
= dm_crtc_reset_state
,
1034 .destroy
= amdgpu_dm_crtc_destroy
,
1035 .gamma_set
= drm_atomic_helper_legacy_gamma_set
,
1036 .set_config
= drm_atomic_helper_set_config
,
1037 .page_flip
= drm_atomic_helper_page_flip
,
1038 .atomic_duplicate_state
= dm_crtc_duplicate_state
,
1039 .atomic_destroy_state
= dm_crtc_destroy_state
,
1042 static enum drm_connector_status
1043 amdgpu_dm_connector_detect(struct drm_connector
*connector
, bool force
)
1046 struct amdgpu_connector
*aconnector
= to_amdgpu_connector(connector
);
1049 * 1. This interface is NOT called in context of HPD irq.
1050 * 2. This interface *is called* in context of user-mode ioctl. Which
1051 * makes it a bad place for *any* MST-related activit. */
1053 if (aconnector
->base
.force
== DRM_FORCE_UNSPECIFIED
)
1054 connected
= (aconnector
->dc_sink
!= NULL
);
1056 connected
= (aconnector
->base
.force
== DRM_FORCE_ON
);
1058 return (connected
? connector_status_connected
:
1059 connector_status_disconnected
);
1062 int amdgpu_dm_connector_atomic_set_property(
1063 struct drm_connector
*connector
,
1064 struct drm_connector_state
*connector_state
,
1065 struct drm_property
*property
,
1068 struct drm_device
*dev
= connector
->dev
;
1069 struct amdgpu_device
*adev
= dev
->dev_private
;
1070 struct dm_connector_state
*dm_old_state
=
1071 to_dm_connector_state(connector
->state
);
1072 struct dm_connector_state
*dm_new_state
=
1073 to_dm_connector_state(connector_state
);
1077 if (property
== dev
->mode_config
.scaling_mode_property
) {
1078 enum amdgpu_rmx_type rmx_type
;
1081 case DRM_MODE_SCALE_CENTER
:
1082 rmx_type
= RMX_CENTER
;
1084 case DRM_MODE_SCALE_ASPECT
:
1085 rmx_type
= RMX_ASPECT
;
1087 case DRM_MODE_SCALE_FULLSCREEN
:
1088 rmx_type
= RMX_FULL
;
1090 case DRM_MODE_SCALE_NONE
:
1096 if (dm_old_state
->scaling
== rmx_type
)
1099 dm_new_state
->scaling
= rmx_type
;
1101 } else if (property
== adev
->mode_info
.underscan_hborder_property
) {
1102 dm_new_state
->underscan_hborder
= val
;
1104 } else if (property
== adev
->mode_info
.underscan_vborder_property
) {
1105 dm_new_state
->underscan_vborder
= val
;
1107 } else if (property
== adev
->mode_info
.underscan_property
) {
1108 dm_new_state
->underscan_enable
= val
;
1115 int amdgpu_dm_connector_atomic_get_property(
1116 struct drm_connector
*connector
,
1117 const struct drm_connector_state
*state
,
1118 struct drm_property
*property
,
1121 struct drm_device
*dev
= connector
->dev
;
1122 struct amdgpu_device
*adev
= dev
->dev_private
;
1123 struct dm_connector_state
*dm_state
=
1124 to_dm_connector_state(state
);
1127 if (property
== dev
->mode_config
.scaling_mode_property
) {
1128 switch (dm_state
->scaling
) {
1130 *val
= DRM_MODE_SCALE_CENTER
;
1133 *val
= DRM_MODE_SCALE_ASPECT
;
1136 *val
= DRM_MODE_SCALE_FULLSCREEN
;
1140 *val
= DRM_MODE_SCALE_NONE
;
1144 } else if (property
== adev
->mode_info
.underscan_hborder_property
) {
1145 *val
= dm_state
->underscan_hborder
;
1147 } else if (property
== adev
->mode_info
.underscan_vborder_property
) {
1148 *val
= dm_state
->underscan_vborder
;
1150 } else if (property
== adev
->mode_info
.underscan_property
) {
1151 *val
= dm_state
->underscan_enable
;
1157 void amdgpu_dm_connector_destroy(struct drm_connector
*connector
)
1159 struct amdgpu_connector
*aconnector
= to_amdgpu_connector(connector
);
1160 const struct dc_link
*link
= aconnector
->dc_link
;
1161 struct amdgpu_device
*adev
= connector
->dev
->dev_private
;
1162 struct amdgpu_display_manager
*dm
= &adev
->dm
;
1163 #if defined(CONFIG_BACKLIGHT_CLASS_DEVICE) ||\
1164 defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE)
1166 if (link
->connector_signal
& (SIGNAL_TYPE_EDP
| SIGNAL_TYPE_LVDS
)) {
1167 amdgpu_dm_register_backlight_device(dm
);
1169 if (dm
->backlight_dev
) {
1170 backlight_device_unregister(dm
->backlight_dev
);
1171 dm
->backlight_dev
= NULL
;
1176 drm_connector_unregister(connector
);
1177 drm_connector_cleanup(connector
);
1181 void amdgpu_dm_connector_funcs_reset(struct drm_connector
*connector
)
1183 struct dm_connector_state
*state
=
1184 to_dm_connector_state(connector
->state
);
1188 state
= kzalloc(sizeof(*state
), GFP_KERNEL
);
1191 state
->scaling
= RMX_OFF
;
1192 state
->underscan_enable
= false;
1193 state
->underscan_hborder
= 0;
1194 state
->underscan_vborder
= 0;
1196 connector
->state
= &state
->base
;
1197 connector
->state
->connector
= connector
;
1201 struct drm_connector_state
*amdgpu_dm_connector_atomic_duplicate_state(
1202 struct drm_connector
*connector
)
1204 struct dm_connector_state
*state
=
1205 to_dm_connector_state(connector
->state
);
1207 struct dm_connector_state
*new_state
=
1208 kmemdup(state
, sizeof(*state
), GFP_KERNEL
);
1211 __drm_atomic_helper_connector_duplicate_state(connector
,
1213 return &new_state
->base
;
1219 static const struct drm_connector_funcs amdgpu_dm_connector_funcs
= {
1220 .reset
= amdgpu_dm_connector_funcs_reset
,
1221 .detect
= amdgpu_dm_connector_detect
,
1222 .fill_modes
= drm_helper_probe_single_connector_modes
,
1223 .destroy
= amdgpu_dm_connector_destroy
,
1224 .atomic_duplicate_state
= amdgpu_dm_connector_atomic_duplicate_state
,
1225 .atomic_destroy_state
= drm_atomic_helper_connector_destroy_state
,
1226 .atomic_set_property
= amdgpu_dm_connector_atomic_set_property
,
1227 .atomic_get_property
= amdgpu_dm_connector_atomic_get_property
1230 static struct drm_encoder
*best_encoder(struct drm_connector
*connector
)
1232 int enc_id
= connector
->encoder_ids
[0];
1233 struct drm_mode_object
*obj
;
1234 struct drm_encoder
*encoder
;
1236 DRM_DEBUG_KMS("Finding the best encoder\n");
1238 /* pick the encoder ids */
1240 obj
= drm_mode_object_find(connector
->dev
, enc_id
, DRM_MODE_OBJECT_ENCODER
);
1242 DRM_ERROR("Couldn't find a matching encoder for our connector\n");
1245 encoder
= obj_to_encoder(obj
);
1248 DRM_ERROR("No encoder id\n");
1252 static int get_modes(struct drm_connector
*connector
)
1254 return amdgpu_dm_connector_get_modes(connector
);
1257 static void create_eml_sink(struct amdgpu_connector
*aconnector
)
1259 struct dc_sink_init_data init_params
= {
1260 .link
= aconnector
->dc_link
,
1261 .sink_signal
= SIGNAL_TYPE_VIRTUAL
1263 struct edid
*edid
= (struct edid
*) aconnector
->base
.edid_blob_ptr
->data
;
1265 if (!aconnector
->base
.edid_blob_ptr
||
1266 !aconnector
->base
.edid_blob_ptr
->data
) {
1267 DRM_ERROR("No EDID firmware found on connector: %s ,forcing to OFF!\n",
1268 aconnector
->base
.name
);
1270 aconnector
->base
.force
= DRM_FORCE_OFF
;
1271 aconnector
->base
.override_edid
= false;
1275 aconnector
->edid
= edid
;
1277 aconnector
->dc_em_sink
= dc_link_add_remote_sink(
1278 aconnector
->dc_link
,
1280 (edid
->extensions
+ 1) * EDID_LENGTH
,
1283 if (aconnector
->base
.force
1285 aconnector
->dc_sink
= aconnector
->dc_link
->local_sink
?
1286 aconnector
->dc_link
->local_sink
:
1287 aconnector
->dc_em_sink
;
1290 static void handle_edid_mgmt(struct amdgpu_connector
*aconnector
)
1292 struct dc_link
*link
= (struct dc_link
*)aconnector
->dc_link
;
1294 /* In case of headless boot with force on for DP managed connector
1295 * Those settings have to be != 0 to get initial modeset
1297 if (link
->connector_signal
== SIGNAL_TYPE_DISPLAY_PORT
) {
1298 link
->verified_link_cap
.lane_count
= LANE_COUNT_FOUR
;
1299 link
->verified_link_cap
.link_rate
= LINK_RATE_HIGH2
;
1303 aconnector
->base
.override_edid
= true;
1304 create_eml_sink(aconnector
);
1307 int amdgpu_dm_connector_mode_valid(
1308 struct drm_connector
*connector
,
1309 struct drm_display_mode
*mode
)
1311 int result
= MODE_ERROR
;
1312 const struct dc_sink
*dc_sink
;
1313 struct amdgpu_device
*adev
= connector
->dev
->dev_private
;
1314 struct dc_validation_set val_set
= { 0 };
1315 /* TODO: Unhardcode stream count */
1316 struct dc_stream
*stream
;
1317 struct amdgpu_connector
*aconnector
= to_amdgpu_connector(connector
);
1318 struct validate_context
*context
;
1320 if ((mode
->flags
& DRM_MODE_FLAG_INTERLACE
) ||
1321 (mode
->flags
& DRM_MODE_FLAG_DBLSCAN
))
1324 /* Only run this the first time mode_valid is called to initilialize
1327 if (aconnector
->base
.force
!= DRM_FORCE_UNSPECIFIED
&&
1328 !aconnector
->dc_em_sink
)
1329 handle_edid_mgmt(aconnector
);
1331 dc_sink
= to_amdgpu_connector(connector
)->dc_sink
;
1333 if (NULL
== dc_sink
) {
1334 DRM_ERROR("dc_sink is NULL!\n");
1338 stream
= dc_create_stream_for_sink(dc_sink
);
1339 if (NULL
== stream
) {
1340 DRM_ERROR("Failed to create stream for sink!\n");
1341 goto stream_create_fail
;
1344 drm_mode_set_crtcinfo(mode
, 0);
1345 fill_stream_properties_from_drm_display_mode(stream
, mode
, connector
);
1347 val_set
.stream
= stream
;
1348 val_set
.surface_count
= 0;
1349 stream
->src
.width
= mode
->hdisplay
;
1350 stream
->src
.height
= mode
->vdisplay
;
1351 stream
->dst
= stream
->src
;
1353 context
= dc_get_validate_context(adev
->dm
.dc
, &val_set
, 1);
1357 dc_resource_validate_ctx_destruct(context
);
1361 dc_stream_release(stream
);
1365 /* TODO: error handling*/
1369 static const struct drm_connector_helper_funcs
1370 amdgpu_dm_connector_helper_funcs
= {
1372 * If hotplug a second bigger display in FB Con mode, bigger resolution
1373 * modes will be filtered by drm_mode_validate_size(), and those modes
1374 * is missing after user start lightdm. So we need to renew modes list.
1375 * in get_modes call back, not just return the modes count
1377 .get_modes
= get_modes
,
1378 .mode_valid
= amdgpu_dm_connector_mode_valid
,
1379 .best_encoder
= best_encoder
1382 static void dm_crtc_helper_disable(struct drm_crtc
*crtc
)
1386 static int dm_crtc_helper_atomic_check(
1387 struct drm_crtc
*crtc
,
1388 struct drm_crtc_state
*state
)
1393 static bool dm_crtc_helper_mode_fixup(
1394 struct drm_crtc
*crtc
,
1395 const struct drm_display_mode
*mode
,
1396 struct drm_display_mode
*adjusted_mode
)
1401 static const struct drm_crtc_helper_funcs amdgpu_dm_crtc_helper_funcs
= {
1402 .disable
= dm_crtc_helper_disable
,
1403 .atomic_check
= dm_crtc_helper_atomic_check
,
1404 .mode_fixup
= dm_crtc_helper_mode_fixup
1407 static void dm_encoder_helper_disable(struct drm_encoder
*encoder
)
1412 static int dm_encoder_helper_atomic_check(
1413 struct drm_encoder
*encoder
,
1414 struct drm_crtc_state
*crtc_state
,
1415 struct drm_connector_state
*conn_state
)
1420 const struct drm_encoder_helper_funcs amdgpu_dm_encoder_helper_funcs
= {
1421 .disable
= dm_encoder_helper_disable
,
1422 .atomic_check
= dm_encoder_helper_atomic_check
1425 static void dm_drm_plane_reset(struct drm_plane
*plane
)
1427 struct dm_plane_state
*amdgpu_state
= NULL
;
1428 struct amdgpu_device
*adev
= plane
->dev
->dev_private
;
1431 plane
->funcs
->atomic_destroy_state(plane
, plane
->state
);
1433 amdgpu_state
= kzalloc(sizeof(*amdgpu_state
), GFP_KERNEL
);
1436 plane
->state
= &amdgpu_state
->base
;
1437 plane
->state
->plane
= plane
;
1438 plane
->state
->rotation
= DRM_MODE_ROTATE_0
;
1440 amdgpu_state
->dc_surface
= dc_create_surface(adev
->dm
.dc
);
1441 WARN_ON(!amdgpu_state
->dc_surface
);
1447 static struct drm_plane_state
*
1448 dm_drm_plane_duplicate_state(struct drm_plane
*plane
)
1450 struct dm_plane_state
*dm_plane_state
, *old_dm_plane_state
;
1451 struct amdgpu_device
*adev
= plane
->dev
->dev_private
;
1453 old_dm_plane_state
= to_dm_plane_state(plane
->state
);
1454 dm_plane_state
= kzalloc(sizeof(*dm_plane_state
), GFP_KERNEL
);
1455 if (!dm_plane_state
)
1458 if (old_dm_plane_state
->dc_surface
) {
1459 struct dc_surface
*dc_surface
= dc_create_surface(adev
->dm
.dc
);
1460 if (WARN_ON(!dc_surface
))
1463 __drm_atomic_helper_plane_duplicate_state(plane
, &dm_plane_state
->base
);
1465 memcpy(dc_surface
, old_dm_plane_state
->dc_surface
, sizeof(*dc_surface
));
1467 if (old_dm_plane_state
->dc_surface
->gamma_correction
)
1468 dc_gamma_retain(dc_surface
->gamma_correction
);
1470 if (old_dm_plane_state
->dc_surface
->in_transfer_func
)
1471 dc_transfer_func_retain(dc_surface
->in_transfer_func
);
1473 dm_plane_state
->dc_surface
= dc_surface
;
1475 /*TODO Check for inferred values to be reset */
1482 return &dm_plane_state
->base
;
1485 void dm_drm_plane_destroy_state(struct drm_plane
*plane
,
1486 struct drm_plane_state
*state
)
1488 struct dm_plane_state
*dm_plane_state
= to_dm_plane_state(state
);
1490 if (dm_plane_state
->dc_surface
) {
1491 struct dc_surface
*dc_surface
= dm_plane_state
->dc_surface
;
1493 if (dc_surface
->gamma_correction
)
1494 dc_gamma_release(&dc_surface
->gamma_correction
);
1496 if (dc_surface
->in_transfer_func
)
1497 dc_transfer_func_release(dc_surface
->in_transfer_func
);
1499 dc_surface_release(dc_surface
);
1502 __drm_atomic_helper_plane_destroy_state(state
);
1503 kfree(dm_plane_state
);
1506 static const struct drm_plane_funcs dm_plane_funcs
= {
1507 .update_plane
= drm_atomic_helper_update_plane
,
1508 .disable_plane
= drm_atomic_helper_disable_plane
,
1509 .destroy
= drm_plane_cleanup
,
1510 .reset
= dm_drm_plane_reset
,
1511 .atomic_duplicate_state
= dm_drm_plane_duplicate_state
,
1512 .atomic_destroy_state
= dm_drm_plane_destroy_state
,
1515 static int dm_plane_helper_prepare_fb(
1516 struct drm_plane
*plane
,
1517 struct drm_plane_state
*new_state
)
1519 struct amdgpu_framebuffer
*afb
;
1520 struct drm_gem_object
*obj
;
1521 struct amdgpu_bo
*rbo
;
1524 if (!new_state
->fb
) {
1525 DRM_DEBUG_KMS("No FB bound\n");
1529 afb
= to_amdgpu_framebuffer(new_state
->fb
);
1532 rbo
= gem_to_amdgpu_bo(obj
);
1533 r
= amdgpu_bo_reserve(rbo
, false);
1534 if (unlikely(r
!= 0))
1537 r
= amdgpu_bo_pin(rbo
, AMDGPU_GEM_DOMAIN_VRAM
, &afb
->address
);
1539 amdgpu_bo_unreserve(rbo
);
1541 if (unlikely(r
!= 0)) {
1542 DRM_ERROR("Failed to pin framebuffer\n");
1548 /* It's a hack for s3 since in 4.9 kernel filter out cursor buffer
1549 * prepare and cleanup in drm_atomic_helper_prepare_planes
1550 * and drm_atomic_helper_cleanup_planes because fb doens't in s3.
1551 * IN 4.10 kernel this code should be removed and amdgpu_device_suspend
1552 * code touching fram buffers should be avoided for DC.
1554 if (plane
->type
== DRM_PLANE_TYPE_CURSOR
) {
1555 struct amdgpu_crtc
*acrtc
= to_amdgpu_crtc(new_state
->crtc
);
1557 acrtc
->cursor_bo
= obj
;
1562 static void dm_plane_helper_cleanup_fb(
1563 struct drm_plane
*plane
,
1564 struct drm_plane_state
*old_state
)
1566 struct amdgpu_bo
*rbo
;
1567 struct amdgpu_framebuffer
*afb
;
1573 afb
= to_amdgpu_framebuffer(old_state
->fb
);
1574 rbo
= gem_to_amdgpu_bo(afb
->obj
);
1575 r
= amdgpu_bo_reserve(rbo
, false);
1577 DRM_ERROR("failed to reserve rbo before unpin\n");
1580 amdgpu_bo_unpin(rbo
);
1581 amdgpu_bo_unreserve(rbo
);
1582 amdgpu_bo_unref(&rbo
);
1586 int dm_create_validation_set_for_connector(struct drm_connector
*connector
,
1587 struct drm_display_mode
*mode
, struct dc_validation_set
*val_set
)
1589 int result
= MODE_ERROR
;
1590 const struct dc_sink
*dc_sink
=
1591 to_amdgpu_connector(connector
)->dc_sink
;
1592 /* TODO: Unhardcode stream count */
1593 struct dc_stream
*stream
;
1595 if ((mode
->flags
& DRM_MODE_FLAG_INTERLACE
) ||
1596 (mode
->flags
& DRM_MODE_FLAG_DBLSCAN
))
1599 if (NULL
== dc_sink
) {
1600 DRM_ERROR("dc_sink is NULL!\n");
1604 stream
= dc_create_stream_for_sink(dc_sink
);
1606 if (NULL
== stream
) {
1607 DRM_ERROR("Failed to create stream for sink!\n");
1611 drm_mode_set_crtcinfo(mode
, 0);
1613 fill_stream_properties_from_drm_display_mode(stream
, mode
, connector
);
1615 val_set
->stream
= stream
;
1617 stream
->src
.width
= mode
->hdisplay
;
1618 stream
->src
.height
= mode
->vdisplay
;
1619 stream
->dst
= stream
->src
;
1624 static const struct drm_plane_helper_funcs dm_plane_helper_funcs
= {
1625 .prepare_fb
= dm_plane_helper_prepare_fb
,
1626 .cleanup_fb
= dm_plane_helper_cleanup_fb
,
1630 * TODO: these are currently initialized to rgb formats only.
1631 * For future use cases we should either initialize them dynamically based on
1632 * plane capabilities, or initialize this array to all formats, so internal drm
1633 * check will succeed, and let DC to implement proper check
1635 static uint32_t rgb_formats
[] = {
1637 DRM_FORMAT_XRGB8888
,
1638 DRM_FORMAT_ARGB8888
,
1639 DRM_FORMAT_RGBA8888
,
1640 DRM_FORMAT_XRGB2101010
,
1641 DRM_FORMAT_XBGR2101010
,
1642 DRM_FORMAT_ARGB2101010
,
1643 DRM_FORMAT_ABGR2101010
,
1646 static uint32_t yuv_formats
[] = {
1651 static const u32 cursor_formats
[] = {
1655 int amdgpu_dm_plane_init(struct amdgpu_display_manager
*dm
,
1656 struct amdgpu_plane
*aplane
,
1657 unsigned long possible_crtcs
)
1661 switch (aplane
->base
.type
) {
1662 case DRM_PLANE_TYPE_PRIMARY
:
1663 aplane
->base
.format_default
= true;
1665 res
= drm_universal_plane_init(
1671 ARRAY_SIZE(rgb_formats
),
1672 NULL
, aplane
->base
.type
, NULL
);
1674 case DRM_PLANE_TYPE_OVERLAY
:
1675 res
= drm_universal_plane_init(
1681 ARRAY_SIZE(yuv_formats
),
1682 NULL
, aplane
->base
.type
, NULL
);
1684 case DRM_PLANE_TYPE_CURSOR
:
1685 res
= drm_universal_plane_init(
1691 ARRAY_SIZE(cursor_formats
),
1692 NULL
, aplane
->base
.type
, NULL
);
1696 drm_plane_helper_add(&aplane
->base
, &dm_plane_helper_funcs
);
1701 int amdgpu_dm_crtc_init(struct amdgpu_display_manager
*dm
,
1702 struct drm_plane
*plane
,
1703 uint32_t crtc_index
)
1705 struct amdgpu_crtc
*acrtc
= NULL
;
1706 struct amdgpu_plane
*cursor_plane
;
1710 cursor_plane
= kzalloc(sizeof(*cursor_plane
), GFP_KERNEL
);
1714 cursor_plane
->base
.type
= DRM_PLANE_TYPE_CURSOR
;
1715 res
= amdgpu_dm_plane_init(dm
, cursor_plane
, 0);
1717 acrtc
= kzalloc(sizeof(struct amdgpu_crtc
), GFP_KERNEL
);
1721 res
= drm_crtc_init_with_planes(
1725 &cursor_plane
->base
,
1726 &amdgpu_dm_crtc_funcs
, NULL
);
1731 drm_crtc_helper_add(&acrtc
->base
, &amdgpu_dm_crtc_helper_funcs
);
1733 acrtc
->max_cursor_width
= dm
->adev
->dm
.dc
->caps
.max_cursor_size
;
1734 acrtc
->max_cursor_height
= dm
->adev
->dm
.dc
->caps
.max_cursor_size
;
1736 acrtc
->crtc_id
= crtc_index
;
1737 acrtc
->base
.enabled
= false;
1739 dm
->adev
->mode_info
.crtcs
[crtc_index
] = acrtc
;
1740 drm_mode_crtc_set_gamma_size(&acrtc
->base
, 256);
1748 kfree(cursor_plane
);
1749 acrtc
->crtc_id
= -1;
1754 static int to_drm_connector_type(enum signal_type st
)
1757 case SIGNAL_TYPE_HDMI_TYPE_A
:
1758 return DRM_MODE_CONNECTOR_HDMIA
;
1759 case SIGNAL_TYPE_EDP
:
1760 return DRM_MODE_CONNECTOR_eDP
;
1761 case SIGNAL_TYPE_RGB
:
1762 return DRM_MODE_CONNECTOR_VGA
;
1763 case SIGNAL_TYPE_DISPLAY_PORT
:
1764 case SIGNAL_TYPE_DISPLAY_PORT_MST
:
1765 return DRM_MODE_CONNECTOR_DisplayPort
;
1766 case SIGNAL_TYPE_DVI_DUAL_LINK
:
1767 case SIGNAL_TYPE_DVI_SINGLE_LINK
:
1768 return DRM_MODE_CONNECTOR_DVID
;
1769 case SIGNAL_TYPE_VIRTUAL
:
1770 return DRM_MODE_CONNECTOR_VIRTUAL
;
1773 return DRM_MODE_CONNECTOR_Unknown
;
1777 static void amdgpu_dm_get_native_mode(struct drm_connector
*connector
)
1779 const struct drm_connector_helper_funcs
*helper
=
1780 connector
->helper_private
;
1781 struct drm_encoder
*encoder
;
1782 struct amdgpu_encoder
*amdgpu_encoder
;
1784 encoder
= helper
->best_encoder(connector
);
1786 if (encoder
== NULL
)
1789 amdgpu_encoder
= to_amdgpu_encoder(encoder
);
1791 amdgpu_encoder
->native_mode
.clock
= 0;
1793 if (!list_empty(&connector
->probed_modes
)) {
1794 struct drm_display_mode
*preferred_mode
= NULL
;
1795 list_for_each_entry(preferred_mode
,
1796 &connector
->probed_modes
,
1798 if (preferred_mode
->type
& DRM_MODE_TYPE_PREFERRED
) {
1799 amdgpu_encoder
->native_mode
= *preferred_mode
;
1807 static struct drm_display_mode
*amdgpu_dm_create_common_mode(
1808 struct drm_encoder
*encoder
, char *name
,
1809 int hdisplay
, int vdisplay
)
1811 struct drm_device
*dev
= encoder
->dev
;
1812 struct amdgpu_encoder
*amdgpu_encoder
= to_amdgpu_encoder(encoder
);
1813 struct drm_display_mode
*mode
= NULL
;
1814 struct drm_display_mode
*native_mode
= &amdgpu_encoder
->native_mode
;
1816 mode
= drm_mode_duplicate(dev
, native_mode
);
1821 mode
->hdisplay
= hdisplay
;
1822 mode
->vdisplay
= vdisplay
;
1823 mode
->type
&= ~DRM_MODE_TYPE_PREFERRED
;
1824 strncpy(mode
->name
, name
, DRM_DISPLAY_MODE_LEN
);
1830 static void amdgpu_dm_connector_add_common_modes(struct drm_encoder
*encoder
,
1831 struct drm_connector
*connector
)
1833 struct amdgpu_encoder
*amdgpu_encoder
= to_amdgpu_encoder(encoder
);
1834 struct drm_display_mode
*mode
= NULL
;
1835 struct drm_display_mode
*native_mode
= &amdgpu_encoder
->native_mode
;
1836 struct amdgpu_connector
*amdgpu_connector
=
1837 to_amdgpu_connector(connector
);
1841 char name
[DRM_DISPLAY_MODE_LEN
];
1845 { "640x480", 640, 480},
1846 { "800x600", 800, 600},
1847 { "1024x768", 1024, 768},
1848 { "1280x720", 1280, 720},
1849 { "1280x800", 1280, 800},
1850 {"1280x1024", 1280, 1024},
1851 { "1440x900", 1440, 900},
1852 {"1680x1050", 1680, 1050},
1853 {"1600x1200", 1600, 1200},
1854 {"1920x1080", 1920, 1080},
1855 {"1920x1200", 1920, 1200}
1858 n
= sizeof(common_modes
) / sizeof(common_modes
[0]);
1860 for (i
= 0; i
< n
; i
++) {
1861 struct drm_display_mode
*curmode
= NULL
;
1862 bool mode_existed
= false;
1864 if (common_modes
[i
].w
> native_mode
->hdisplay
||
1865 common_modes
[i
].h
> native_mode
->vdisplay
||
1866 (common_modes
[i
].w
== native_mode
->hdisplay
&&
1867 common_modes
[i
].h
== native_mode
->vdisplay
))
1870 list_for_each_entry(curmode
, &connector
->probed_modes
, head
) {
1871 if (common_modes
[i
].w
== curmode
->hdisplay
&&
1872 common_modes
[i
].h
== curmode
->vdisplay
) {
1873 mode_existed
= true;
1881 mode
= amdgpu_dm_create_common_mode(encoder
,
1882 common_modes
[i
].name
, common_modes
[i
].w
,
1884 drm_mode_probed_add(connector
, mode
);
1885 amdgpu_connector
->num_modes
++;
1889 static void amdgpu_dm_connector_ddc_get_modes(
1890 struct drm_connector
*connector
,
1893 struct amdgpu_connector
*amdgpu_connector
=
1894 to_amdgpu_connector(connector
);
1897 /* empty probed_modes */
1898 INIT_LIST_HEAD(&connector
->probed_modes
);
1899 amdgpu_connector
->num_modes
=
1900 drm_add_edid_modes(connector
, edid
);
1902 drm_edid_to_eld(connector
, edid
);
1904 amdgpu_dm_get_native_mode(connector
);
1906 amdgpu_connector
->num_modes
= 0;
1909 int amdgpu_dm_connector_get_modes(struct drm_connector
*connector
)
1911 const struct drm_connector_helper_funcs
*helper
=
1912 connector
->helper_private
;
1913 struct amdgpu_connector
*amdgpu_connector
=
1914 to_amdgpu_connector(connector
);
1915 struct drm_encoder
*encoder
;
1916 struct edid
*edid
= amdgpu_connector
->edid
;
1918 encoder
= helper
->best_encoder(connector
);
1920 amdgpu_dm_connector_ddc_get_modes(connector
, edid
);
1921 amdgpu_dm_connector_add_common_modes(encoder
, connector
);
1922 return amdgpu_connector
->num_modes
;
1925 void amdgpu_dm_connector_init_helper(
1926 struct amdgpu_display_manager
*dm
,
1927 struct amdgpu_connector
*aconnector
,
1929 const struct dc_link
*link
,
1932 struct amdgpu_device
*adev
= dm
->ddev
->dev_private
;
1934 aconnector
->connector_id
= link_index
;
1935 aconnector
->dc_link
= link
;
1936 aconnector
->base
.interlace_allowed
= false;
1937 aconnector
->base
.doublescan_allowed
= false;
1938 aconnector
->base
.stereo_allowed
= false;
1939 aconnector
->base
.dpms
= DRM_MODE_DPMS_OFF
;
1940 aconnector
->hpd
.hpd
= AMDGPU_HPD_NONE
; /* not used */
1942 mutex_init(&aconnector
->hpd_lock
);
1944 /*configure suport HPD hot plug connector_>polled default value is 0
1945 * which means HPD hot plug not supported*/
1946 switch (connector_type
) {
1947 case DRM_MODE_CONNECTOR_HDMIA
:
1948 aconnector
->base
.polled
= DRM_CONNECTOR_POLL_HPD
;
1950 case DRM_MODE_CONNECTOR_DisplayPort
:
1951 aconnector
->base
.polled
= DRM_CONNECTOR_POLL_HPD
;
1953 case DRM_MODE_CONNECTOR_DVID
:
1954 aconnector
->base
.polled
= DRM_CONNECTOR_POLL_HPD
;
1960 drm_object_attach_property(&aconnector
->base
.base
,
1961 dm
->ddev
->mode_config
.scaling_mode_property
,
1962 DRM_MODE_SCALE_NONE
);
1964 drm_object_attach_property(&aconnector
->base
.base
,
1965 adev
->mode_info
.underscan_property
,
1967 drm_object_attach_property(&aconnector
->base
.base
,
1968 adev
->mode_info
.underscan_hborder_property
,
1970 drm_object_attach_property(&aconnector
->base
.base
,
1971 adev
->mode_info
.underscan_vborder_property
,
1976 int amdgpu_dm_i2c_xfer(struct i2c_adapter
*i2c_adap
,
1977 struct i2c_msg
*msgs
, int num
)
1979 struct amdgpu_i2c_adapter
*i2c
= i2c_get_adapdata(i2c_adap
);
1980 struct ddc_service
*ddc_service
= i2c
->ddc_service
;
1981 struct i2c_command cmd
;
1985 cmd
.payloads
= kzalloc(num
* sizeof(struct i2c_payload
), GFP_KERNEL
);
1990 cmd
.number_of_payloads
= num
;
1991 cmd
.engine
= I2C_COMMAND_ENGINE_DEFAULT
;
1994 for (i
= 0; i
< num
; i
++) {
1995 cmd
.payloads
[i
].write
= !(msgs
[i
].flags
& I2C_M_RD
);
1996 cmd
.payloads
[i
].address
= msgs
[i
].addr
;
1997 cmd
.payloads
[i
].length
= msgs
[i
].len
;
1998 cmd
.payloads
[i
].data
= msgs
[i
].buf
;
2001 if (dal_i2caux_submit_i2c_command(
2002 ddc_service
->ctx
->i2caux
,
2003 ddc_service
->ddc_pin
,
2007 kfree(cmd
.payloads
);
2011 u32
amdgpu_dm_i2c_func(struct i2c_adapter
*adap
)
2013 return I2C_FUNC_I2C
| I2C_FUNC_SMBUS_EMUL
;
2016 static const struct i2c_algorithm amdgpu_dm_i2c_algo
= {
2017 .master_xfer
= amdgpu_dm_i2c_xfer
,
2018 .functionality
= amdgpu_dm_i2c_func
,
2021 static struct amdgpu_i2c_adapter
*create_i2c(
2022 struct ddc_service
*ddc_service
,
2026 struct amdgpu_device
*adev
= ddc_service
->ctx
->driver_context
;
2027 struct amdgpu_i2c_adapter
*i2c
;
2029 i2c
= kzalloc(sizeof (struct amdgpu_i2c_adapter
), GFP_KERNEL
);
2030 i2c
->base
.owner
= THIS_MODULE
;
2031 i2c
->base
.class = I2C_CLASS_DDC
;
2032 i2c
->base
.dev
.parent
= &adev
->pdev
->dev
;
2033 i2c
->base
.algo
= &amdgpu_dm_i2c_algo
;
2034 snprintf(i2c
->base
.name
, sizeof (i2c
->base
.name
), "AMDGPU DM i2c hw bus %d", link_index
);
2035 i2c_set_adapdata(&i2c
->base
, i2c
);
2036 i2c
->ddc_service
= ddc_service
;
2041 /* Note: this function assumes that dc_link_detect() was called for the
2042 * dc_link which will be represented by this aconnector. */
2043 int amdgpu_dm_connector_init(
2044 struct amdgpu_display_manager
*dm
,
2045 struct amdgpu_connector
*aconnector
,
2046 uint32_t link_index
,
2047 struct amdgpu_encoder
*aencoder
)
2051 struct dc
*dc
= dm
->dc
;
2052 const struct dc_link
*link
= dc_get_link_at_index(dc
, link_index
);
2053 struct amdgpu_i2c_adapter
*i2c
;
2054 ((struct dc_link
*)link
)->priv
= aconnector
;
2056 DRM_DEBUG_KMS("%s()\n", __func__
);
2058 i2c
= create_i2c(link
->ddc
, link
->link_index
, &res
);
2059 aconnector
->i2c
= i2c
;
2060 res
= i2c_add_adapter(&i2c
->base
);
2063 DRM_ERROR("Failed to register hw i2c %d\n", link
->link_index
);
2067 connector_type
= to_drm_connector_type(link
->connector_signal
);
2069 res
= drm_connector_init(
2072 &amdgpu_dm_connector_funcs
,
2076 DRM_ERROR("connector_init failed\n");
2077 aconnector
->connector_id
= -1;
2081 drm_connector_helper_add(
2083 &amdgpu_dm_connector_helper_funcs
);
2085 amdgpu_dm_connector_init_helper(
2092 drm_mode_connector_attach_encoder(
2093 &aconnector
->base
, &aencoder
->base
);
2095 drm_connector_register(&aconnector
->base
);
2097 if (connector_type
== DRM_MODE_CONNECTOR_DisplayPort
2098 || connector_type
== DRM_MODE_CONNECTOR_eDP
)
2099 amdgpu_dm_initialize_dp_connector(dm
, aconnector
);
2101 #if defined(CONFIG_BACKLIGHT_CLASS_DEVICE) ||\
2102 defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE)
2104 /* NOTE: this currently will create backlight device even if a panel
2105 * is not connected to the eDP/LVDS connector.
2107 * This is less than ideal but we don't have sink information at this
2108 * stage since detection happens after. We can't do detection earlier
2109 * since MST detection needs connectors to be created first.
2111 if (link
->connector_signal
& (SIGNAL_TYPE_EDP
| SIGNAL_TYPE_LVDS
)) {
2112 /* Event if registration failed, we should continue with
2113 * DM initialization because not having a backlight control
2114 * is better then a black screen. */
2115 amdgpu_dm_register_backlight_device(dm
);
2117 if (dm
->backlight_dev
)
2118 dm
->backlight_link
= link
;
2125 aconnector
->i2c
= NULL
;
2130 int amdgpu_dm_get_encoder_crtc_mask(struct amdgpu_device
*adev
)
2132 switch (adev
->mode_info
.num_crtc
) {
2149 int amdgpu_dm_encoder_init(
2150 struct drm_device
*dev
,
2151 struct amdgpu_encoder
*aencoder
,
2152 uint32_t link_index
)
2154 struct amdgpu_device
*adev
= dev
->dev_private
;
2156 int res
= drm_encoder_init(dev
,
2158 &amdgpu_dm_encoder_funcs
,
2159 DRM_MODE_ENCODER_TMDS
,
2162 aencoder
->base
.possible_crtcs
= amdgpu_dm_get_encoder_crtc_mask(adev
);
2165 aencoder
->encoder_id
= link_index
;
2167 aencoder
->encoder_id
= -1;
2169 drm_encoder_helper_add(&aencoder
->base
, &amdgpu_dm_encoder_helper_funcs
);
2174 static bool modeset_required(struct drm_crtc_state
*crtc_state
)
2176 if (!drm_atomic_crtc_needs_modeset(crtc_state
))
2179 if (!crtc_state
->enable
)
2182 return crtc_state
->active
;
2185 static bool modereset_required(struct drm_crtc_state
*crtc_state
)
2187 if (!drm_atomic_crtc_needs_modeset(crtc_state
))
2190 return !crtc_state
->enable
|| !crtc_state
->active
;
2193 static void manage_dm_interrupts(
2194 struct amdgpu_device
*adev
,
2195 struct amdgpu_crtc
*acrtc
,
2199 * this is not correct translation but will work as soon as VBLANK
2200 * constant is the same as PFLIP
2203 amdgpu_crtc_idx_to_irq_type(
2208 drm_crtc_vblank_on(&acrtc
->base
);
2211 &adev
->pageflip_irq
,
2217 &adev
->pageflip_irq
,
2219 drm_crtc_vblank_off(&acrtc
->base
);
2223 static bool is_scaling_state_different(
2224 const struct dm_connector_state
*dm_state
,
2225 const struct dm_connector_state
*old_dm_state
)
2227 if (dm_state
->scaling
!= old_dm_state
->scaling
)
2229 if (!dm_state
->underscan_enable
&& old_dm_state
->underscan_enable
) {
2230 if (old_dm_state
->underscan_hborder
!= 0 && old_dm_state
->underscan_vborder
!= 0)
2232 } else if (dm_state
->underscan_enable
&& !old_dm_state
->underscan_enable
) {
2233 if (dm_state
->underscan_hborder
!= 0 && dm_state
->underscan_vborder
!= 0)
2235 } else if (dm_state
->underscan_hborder
!= old_dm_state
->underscan_hborder
2236 || dm_state
->underscan_vborder
!= old_dm_state
->underscan_vborder
)
2241 static void remove_stream(struct amdgpu_device
*adev
, struct amdgpu_crtc
*acrtc
)
2244 * we evade vblanks and pflips on crtc that
2247 manage_dm_interrupts(adev
, acrtc
, false);
2249 /* this is the update mode case */
2250 if (adev
->dm
.freesync_module
)
2251 mod_freesync_remove_stream(adev
->dm
.freesync_module
,
2254 dc_stream_release(acrtc
->stream
);
2255 acrtc
->stream
= NULL
;
2256 acrtc
->otg_inst
= -1;
2257 acrtc
->enabled
= false;
2260 static void handle_cursor_update(
2261 struct drm_plane
*plane
,
2262 struct drm_plane_state
*old_plane_state
)
2264 if (!plane
->state
->fb
&& !old_plane_state
->fb
)
2267 /* Check if it's a cursor on/off update or just cursor move*/
2268 if (plane
->state
->fb
== old_plane_state
->fb
)
2269 dm_crtc_cursor_move(
2271 plane
->state
->crtc_x
,
2272 plane
->state
->crtc_y
);
2274 struct amdgpu_framebuffer
*afb
=
2275 to_amdgpu_framebuffer(plane
->state
->fb
);
2277 (!!plane
->state
->fb
) ?
2278 plane
->state
->crtc
:
2279 old_plane_state
->crtc
,
2280 (!!plane
->state
->fb
) ?
2283 plane
->state
->crtc_w
,
2284 plane
->state
->crtc_h
);
2289 static void prepare_flip_isr(struct amdgpu_crtc
*acrtc
)
2292 assert_spin_locked(&acrtc
->base
.dev
->event_lock
);
2293 WARN_ON(acrtc
->event
);
2295 acrtc
->event
= acrtc
->base
.state
->event
;
2297 /* Set the flip status */
2298 acrtc
->pflip_status
= AMDGPU_FLIP_SUBMITTED
;
2300 /* Mark this event as consumed */
2301 acrtc
->base
.state
->event
= NULL
;
2303 DRM_DEBUG_DRIVER("crtc:%d, pflip_stat:AMDGPU_FLIP_SUBMITTED\n",
2310 * Waits on all BO's fences and for proper vblank count
2312 static void amdgpu_dm_do_flip(
2313 struct drm_crtc
*crtc
,
2314 struct drm_framebuffer
*fb
,
2317 unsigned long flags
;
2318 uint32_t target_vblank
;
2320 struct amdgpu_crtc
*acrtc
= to_amdgpu_crtc(crtc
);
2321 struct amdgpu_framebuffer
*afb
= to_amdgpu_framebuffer(fb
);
2322 struct amdgpu_bo
*abo
= gem_to_amdgpu_bo(afb
->obj
);
2323 struct amdgpu_device
*adev
= crtc
->dev
->dev_private
;
2324 bool async_flip
= (acrtc
->flip_flags
& DRM_MODE_PAGE_FLIP_ASYNC
) != 0;
2325 struct dc_flip_addrs addr
= { {0} };
2326 struct dc_surface_update surface_updates
[1] = { {0} };
2328 /* Prepare wait for target vblank early - before the fence-waits */
2329 target_vblank
= target
- drm_crtc_vblank_count(crtc
) +
2330 amdgpu_get_vblank_counter_kms(crtc
->dev
, acrtc
->crtc_id
);
2332 /*TODO This might fail and hence better not used, wait
2333 * explicitly on fences instead
2334 * and in general should be called for
2335 * blocking commit to as per framework helpers
2337 r
= amdgpu_bo_reserve(abo
, true);
2338 if (unlikely(r
!= 0)) {
2339 DRM_ERROR("failed to reserve buffer before flip\n");
2343 /* Wait for all fences on this FB */
2344 WARN_ON(reservation_object_wait_timeout_rcu(abo
->tbo
.resv
, true, false,
2345 MAX_SCHEDULE_TIMEOUT
) < 0);
2347 amdgpu_bo_unreserve(abo
);
2349 /* Wait until we're out of the vertical blank period before the one
2350 * targeted by the flip
2352 while ((acrtc
->enabled
&&
2353 (amdgpu_get_crtc_scanoutpos(adev
->ddev
, acrtc
->crtc_id
, 0,
2354 &vpos
, &hpos
, NULL
, NULL
,
2356 & (DRM_SCANOUTPOS_VALID
| DRM_SCANOUTPOS_IN_VBLANK
)) ==
2357 (DRM_SCANOUTPOS_VALID
| DRM_SCANOUTPOS_IN_VBLANK
) &&
2358 (int)(target_vblank
-
2359 amdgpu_get_vblank_counter_kms(adev
->ddev
, acrtc
->crtc_id
)) > 0)) {
2360 usleep_range(1000, 1100);
2364 spin_lock_irqsave(&crtc
->dev
->event_lock
, flags
);
2365 /* update crtc fb */
2366 crtc
->primary
->fb
= fb
;
2368 WARN_ON(acrtc
->pflip_status
!= AMDGPU_FLIP_NONE
);
2369 WARN_ON(!acrtc
->stream
);
2371 addr
.address
.grph
.addr
.low_part
= lower_32_bits(afb
->address
);
2372 addr
.address
.grph
.addr
.high_part
= upper_32_bits(afb
->address
);
2373 addr
.flip_immediate
= async_flip
;
2376 if (acrtc
->base
.state
->event
)
2377 prepare_flip_isr(acrtc
);
2379 surface_updates
->surface
= dc_stream_get_status(acrtc
->stream
)->surfaces
[0];
2380 surface_updates
->flip_addr
= &addr
;
2383 dc_update_surfaces_for_stream(adev
->dm
.dc
, surface_updates
, 1, acrtc
->stream
);
2385 DRM_DEBUG_DRIVER("%s Flipping to hi: 0x%x, low: 0x%x \n",
2387 addr
.address
.grph
.addr
.high_part
,
2388 addr
.address
.grph
.addr
.low_part
);
2391 spin_unlock_irqrestore(&crtc
->dev
->event_lock
, flags
);
2394 static void amdgpu_dm_commit_surfaces(struct drm_atomic_state
*state
,
2395 struct drm_device
*dev
,
2396 struct amdgpu_display_manager
*dm
,
2397 struct drm_crtc
*pcrtc
,
2398 bool *wait_for_vblank
)
2401 struct drm_plane
*plane
;
2402 struct drm_plane_state
*old_plane_state
;
2403 const struct dc_stream
*dc_stream_attach
;
2404 const struct dc_surface
*dc_surfaces_constructed
[MAX_SURFACES
];
2405 struct amdgpu_crtc
*acrtc_attach
= to_amdgpu_crtc(pcrtc
);
2406 int planes_count
= 0;
2408 /* update planes when needed */
2409 for_each_plane_in_state(state
, plane
, old_plane_state
, i
) {
2410 struct drm_plane_state
*plane_state
= plane
->state
;
2411 struct drm_crtc
*crtc
= plane_state
->crtc
;
2412 struct drm_framebuffer
*fb
= plane_state
->fb
;
2413 struct drm_connector
*connector
;
2414 struct dm_connector_state
*con_state
= NULL
;
2417 if (plane
->type
== DRM_PLANE_TYPE_CURSOR
) {
2418 handle_cursor_update(plane
, old_plane_state
);
2422 if (!fb
|| !crtc
|| pcrtc
!= crtc
|| !crtc
->state
->active
)
2425 pflip_needed
= !state
->allow_modeset
;
2426 if (!pflip_needed
) {
2427 list_for_each_entry(connector
,
2428 &dev
->mode_config
.connector_list
,
2430 if (connector
->state
->crtc
== crtc
) {
2431 con_state
= to_dm_connector_state(
2438 * This situation happens in the following case:
2439 * we are about to get set mode for connector who's only
2440 * possible crtc (in encoder crtc mask) is used by
2441 * another connector, that is why it will try to
2442 * re-assing crtcs in order to make configuration
2443 * supported. For our implementation we need to make all
2444 * encoders support all crtcs, then this issue will
2445 * never arise again. But to guard code from this issue
2448 * Also it should be needed when used with actual
2449 * drm_atomic_commit ioctl in future
2454 add_surface(dm
->dc
, crtc
, plane
,
2455 &dc_surfaces_constructed
[planes_count
]);
2456 if (dc_surfaces_constructed
[planes_count
] == NULL
) {
2457 dm_error("%s: Failed to add surface!\n", __func__
);
2460 dc_stream_attach
= acrtc_attach
->stream
;
2463 } else if (crtc
->state
->planes_changed
) {
2464 /* Assume even ONE crtc with immediate flip means
2465 * entire can't wait for VBLANK
2466 * TODO Check if it's correct
2469 acrtc_attach
->flip_flags
& DRM_MODE_PAGE_FLIP_ASYNC
?
2472 /* TODO: Needs rework for multiplane flip */
2473 if (plane
->type
== DRM_PLANE_TYPE_PRIMARY
)
2474 drm_crtc_vblank_get(crtc
);
2479 drm_crtc_vblank_count(crtc
) + *wait_for_vblank
);
2481 /*TODO BUG remove ASAP in 4.12 to avoid race between worker and flip IOCTL */
2483 /*clean up the flags for next usage*/
2484 acrtc_attach
->flip_flags
= 0;
2490 unsigned long flags
;
2492 if (pcrtc
->state
->event
) {
2494 drm_crtc_vblank_get(pcrtc
);
2496 spin_lock_irqsave(&pcrtc
->dev
->event_lock
, flags
);
2497 prepare_flip_isr(acrtc_attach
);
2498 spin_unlock_irqrestore(&pcrtc
->dev
->event_lock
, flags
);
2501 if (false == dc_commit_surfaces_to_stream(dm
->dc
,
2502 dc_surfaces_constructed
,
2505 dm_error("%s: Failed to attach surface!\n", __func__
);
2507 for (i
= 0; i
< planes_count
; i
++)
2508 dc_surface_release(dc_surfaces_constructed
[i
]);
2510 /*TODO BUG Here should go disable planes on CRTC. */
2514 void amdgpu_dm_atomic_commit_tail(
2515 struct drm_atomic_state
*state
)
2517 struct drm_device
*dev
= state
->dev
;
2518 struct amdgpu_device
*adev
= dev
->dev_private
;
2519 struct amdgpu_display_manager
*dm
= &adev
->dm
;
2521 uint32_t commit_streams_count
= 0;
2522 uint32_t new_crtcs_count
= 0;
2523 struct drm_crtc
*crtc
, *pcrtc
;
2524 struct drm_crtc_state
*old_crtc_state
;
2525 const struct dc_stream
*commit_streams
[MAX_STREAMS
];
2526 struct amdgpu_crtc
*new_crtcs
[MAX_STREAMS
];
2527 const struct dc_stream
*new_stream
;
2528 unsigned long flags
;
2529 bool wait_for_vblank
= true;
2530 struct drm_connector
*connector
;
2531 struct drm_connector_state
*old_conn_state
;
2533 drm_atomic_helper_update_legacy_modeset_state(dev
, state
);
2534 /* update changed items */
2535 for_each_crtc_in_state(state
, crtc
, old_crtc_state
, i
) {
2536 struct amdgpu_crtc
*acrtc
;
2537 struct amdgpu_connector
*aconnector
= NULL
;
2538 struct drm_crtc_state
*new_state
= crtc
->state
;
2540 acrtc
= to_amdgpu_crtc(crtc
);
2542 amdgpu_dm_find_first_crct_matching_connector(
2548 "amdgpu_crtc id:%d crtc_state_flags: enable:%d, active:%d, "
2549 "planes_changed:%d, mode_changed:%d,active_changed:%d,"
2550 "connectors_changed:%d\n",
2554 new_state
->planes_changed
,
2555 new_state
->mode_changed
,
2556 new_state
->active_changed
,
2557 new_state
->connectors_changed
);
2559 /* handles headless hotplug case, updating new_state and
2560 * aconnector as needed
2563 if (modeset_required(new_state
)) {
2564 struct dm_connector_state
*dm_state
= NULL
;
2568 dm_state
= to_dm_connector_state(aconnector
->base
.state
);
2570 new_stream
= create_stream_for_sink(
2575 DRM_INFO("Atomic commit: SET crtc id %d: [%p]\n", acrtc
->crtc_id
, acrtc
);
2579 * this could happen because of issues with
2580 * userspace notifications delivery.
2581 * In this case userspace tries to set mode on
2582 * display which is disconnect in fact.
2583 * dc_sink in NULL in this case on aconnector.
2584 * We expect reset mode will come soon.
2586 * This can also happen when unplug is done
2587 * during resume sequence ended
2589 * In this case, we want to pretend we still
2590 * have a sink to keep the pipe running so that
2591 * hw state is consistent with the sw state
2593 DRM_DEBUG_KMS("%s: Failed to create new stream for crtc %d\n",
2594 __func__
, acrtc
->base
.base
.id
);
2599 remove_stream(adev
, acrtc
);
2602 * this loop saves set mode crtcs
2603 * we needed to enable vblanks once all
2604 * resources acquired in dc after dc_commit_streams
2606 new_crtcs
[new_crtcs_count
] = acrtc
;
2609 acrtc
->stream
= new_stream
;
2610 acrtc
->enabled
= true;
2611 acrtc
->hw_mode
= crtc
->state
->mode
;
2612 crtc
->hwmode
= crtc
->state
->mode
;
2613 } else if (modereset_required(new_state
)) {
2615 DRM_INFO("Atomic commit: RESET. crtc id %d:[%p]\n", acrtc
->crtc_id
, acrtc
);
2616 /* i.e. reset mode */
2618 remove_stream(adev
, acrtc
);
2620 } /* for_each_crtc_in_state() */
2622 /* Handle scaling and undersacn changes*/
2623 for_each_connector_in_state(state
, connector
, old_conn_state
, i
) {
2624 struct amdgpu_connector
*aconnector
= to_amdgpu_connector(connector
);
2625 struct dm_connector_state
*con_new_state
=
2626 to_dm_connector_state(aconnector
->base
.state
);
2627 struct dm_connector_state
*con_old_state
=
2628 to_dm_connector_state(old_conn_state
);
2629 struct amdgpu_crtc
*acrtc
= to_amdgpu_crtc(con_new_state
->base
.crtc
);
2630 const struct dc_stream_status
*status
= NULL
;
2632 /* Skip any modesets/resets */
2633 if (!acrtc
|| drm_atomic_crtc_needs_modeset(acrtc
->base
.state
))
2636 /* Skip any thing not scale or underscan chnages */
2637 if (!is_scaling_state_different(con_new_state
, con_old_state
))
2640 update_stream_scaling_settings(&con_new_state
->base
.crtc
->mode
,
2641 con_new_state
, (struct dc_stream
*)acrtc
->stream
);
2643 status
= dc_stream_get_status(acrtc
->stream
);
2645 WARN_ON(!status
->surface_count
);
2650 /*TODO How it works with MPO ?*/
2651 if (!dc_commit_surfaces_to_stream(
2653 (const struct dc_surface
**)status
->surfaces
,
2654 status
->surface_count
,
2656 dm_error("%s: Failed to update stream scaling!\n", __func__
);
2659 list_for_each_entry(crtc
, &dev
->mode_config
.crtc_list
, head
) {
2661 struct amdgpu_crtc
*acrtc
= to_amdgpu_crtc(crtc
);
2663 if (acrtc
->stream
) {
2664 commit_streams
[commit_streams_count
] = acrtc
->stream
;
2665 ++commit_streams_count
;
2670 * Add streams after required streams from new and replaced streams
2671 * are removed from freesync module
2673 if (adev
->dm
.freesync_module
) {
2674 for (i
= 0; i
< new_crtcs_count
; i
++) {
2675 struct amdgpu_connector
*aconnector
= NULL
;
2676 new_stream
= new_crtcs
[i
]->stream
;
2678 amdgpu_dm_find_first_crct_matching_connector(
2680 &new_crtcs
[i
]->base
,
2684 "Atomic commit: Failed to find connector for acrtc id:%d "
2685 "skipping freesync init\n",
2686 new_crtcs
[i
]->crtc_id
);
2690 mod_freesync_add_stream(adev
->dm
.freesync_module
,
2691 new_stream
, &aconnector
->caps
);
2695 /* DC is optimized not to do anything if 'streams' didn't change. */
2696 WARN_ON(!dc_commit_streams(dm
->dc
, commit_streams
, commit_streams_count
));
2698 list_for_each_entry(crtc
, &dev
->mode_config
.crtc_list
, head
) {
2699 struct amdgpu_crtc
*acrtc
= to_amdgpu_crtc(crtc
);
2701 if (acrtc
->stream
!= NULL
)
2703 dc_stream_get_status(acrtc
->stream
)->primary_otg_inst
;
2706 for (i
= 0; i
< new_crtcs_count
; i
++) {
2708 * loop to enable interrupts on newly arrived crtc
2710 struct amdgpu_crtc
*acrtc
= new_crtcs
[i
];
2712 if (adev
->dm
.freesync_module
)
2713 mod_freesync_notify_mode_change(
2714 adev
->dm
.freesync_module
, &acrtc
->stream
, 1);
2716 manage_dm_interrupts(adev
, acrtc
, true);
2719 /* update planes when needed per crtc*/
2720 for_each_crtc_in_state(state
, pcrtc
, old_crtc_state
, j
) {
2721 struct amdgpu_crtc
*acrtc
= to_amdgpu_crtc(pcrtc
);
2724 amdgpu_dm_commit_surfaces(state
, dev
, dm
, pcrtc
, &wait_for_vblank
);
2729 * send vblank event on all events not handled in flip and
2730 * mark consumed event for drm_atomic_helper_commit_hw_done
2732 spin_lock_irqsave(&adev
->ddev
->event_lock
, flags
);
2733 for_each_crtc_in_state(state
, crtc
, old_crtc_state
, i
) {
2734 struct amdgpu_crtc
*acrtc
= to_amdgpu_crtc(crtc
);
2736 if (acrtc
->base
.state
->event
)
2737 drm_send_event_locked(dev
, &crtc
->state
->event
->base
);
2739 acrtc
->base
.state
->event
= NULL
;
2741 spin_unlock_irqrestore(&adev
->ddev
->event_lock
, flags
);
2743 /* Signal HW programming completion */
2744 drm_atomic_helper_commit_hw_done(state
);
2746 if (wait_for_vblank
)
2747 drm_atomic_helper_wait_for_vblanks(dev
, state
);
2749 drm_atomic_helper_cleanup_planes(dev
, state
);
2753 static int dm_force_atomic_commit(struct drm_connector
*connector
)
2756 struct drm_device
*ddev
= connector
->dev
;
2757 struct drm_atomic_state
*state
= drm_atomic_state_alloc(ddev
);
2758 struct amdgpu_crtc
*disconnected_acrtc
= to_amdgpu_crtc(connector
->encoder
->crtc
);
2759 struct drm_plane
*plane
= disconnected_acrtc
->base
.primary
;
2760 struct drm_connector_state
*conn_state
;
2761 struct drm_crtc_state
*crtc_state
;
2762 struct drm_plane_state
*plane_state
;
2767 state
->acquire_ctx
= ddev
->mode_config
.acquire_ctx
;
2769 /* Construct an atomic state to restore previous display setting */
2772 * Attach connectors to drm_atomic_state
2774 conn_state
= drm_atomic_get_connector_state(state
, connector
);
2776 ret
= PTR_ERR_OR_ZERO(conn_state
);
2780 /* Attach crtc to drm_atomic_state*/
2781 crtc_state
= drm_atomic_get_crtc_state(state
, &disconnected_acrtc
->base
);
2783 ret
= PTR_ERR_OR_ZERO(crtc_state
);
2787 /* force a restore */
2788 crtc_state
->mode_changed
= true;
2790 /* Attach plane to drm_atomic_state */
2791 plane_state
= drm_atomic_get_plane_state(state
, plane
);
2793 ret
= PTR_ERR_OR_ZERO(plane_state
);
2798 /* Call commit internally with the state we just constructed */
2799 ret
= drm_atomic_commit(state
);
2804 DRM_ERROR("Restoring old state failed with %i\n", ret
);
2805 drm_atomic_state_put(state
);
2811 * This functions handle all cases when set mode does not come upon hotplug.
2812 * This include when the same display is unplugged then plugged back into the
2813 * same port and when we are running without usermode desktop manager supprot
2815 void dm_restore_drm_connector_state(struct drm_device
*dev
, struct drm_connector
*connector
)
2817 struct amdgpu_connector
*aconnector
= to_amdgpu_connector(connector
);
2818 struct amdgpu_crtc
*disconnected_acrtc
;
2820 if (!aconnector
->dc_sink
|| !connector
->state
|| !connector
->encoder
)
2823 disconnected_acrtc
= to_amdgpu_crtc(connector
->encoder
->crtc
);
2825 if (!disconnected_acrtc
|| !disconnected_acrtc
->stream
)
2829 * If the previous sink is not released and different from the current,
2830 * we deduce we are in a state where we can not rely on usermode call
2831 * to turn on the display, so we do it here
2833 if (disconnected_acrtc
->stream
->sink
!= aconnector
->dc_sink
)
2834 dm_force_atomic_commit(&aconnector
->base
);
2837 static uint32_t add_val_sets_surface(
2838 struct dc_validation_set
*val_sets
,
2840 const struct dc_stream
*stream
,
2841 const struct dc_surface
*surface
)
2843 uint32_t i
= 0, j
= 0;
2845 while (i
< set_count
) {
2846 if (val_sets
[i
].stream
== stream
) {
2847 while (val_sets
[i
].surfaces
[j
])
2854 val_sets
[i
].surfaces
[j
] = surface
;
2855 val_sets
[i
].surface_count
++;
2857 return val_sets
[i
].surface_count
;
2860 static uint32_t update_in_val_sets_stream(
2861 struct dc_validation_set
*val_sets
,
2862 struct drm_crtc
**crtcs
,
2864 const struct dc_stream
*old_stream
,
2865 const struct dc_stream
*new_stream
,
2866 struct drm_crtc
*crtc
)
2870 while (i
< set_count
) {
2871 if (val_sets
[i
].stream
== old_stream
)
2876 val_sets
[i
].stream
= new_stream
;
2879 if (i
== set_count
) {
2880 /* nothing found. add new one to the end */
2881 return set_count
+ 1;
2887 static uint32_t remove_from_val_sets(
2888 struct dc_validation_set
*val_sets
,
2890 const struct dc_stream
*stream
)
2894 for (i
= 0; i
< set_count
; i
++)
2895 if (val_sets
[i
].stream
== stream
)
2898 if (i
== set_count
) {
2905 for (; i
< set_count
; i
++) {
2906 val_sets
[i
] = val_sets
[i
+ 1];
2913 * Grabs all modesetting locks to serialize against any blocking commits,
2914 * Waits for completion of all non blocking commits.
2916 static int do_aquire_global_lock(
2917 struct drm_device
*dev
,
2918 struct drm_atomic_state
*state
)
2920 struct drm_crtc
*crtc
;
2921 struct drm_crtc_commit
*commit
;
2924 /* Adding all modeset locks to aquire_ctx will
2925 * ensure that when the framework release it the
2926 * extra locks we are locking here will get released to
2928 ret
= drm_modeset_lock_all_ctx(dev
, state
->acquire_ctx
);
2932 list_for_each_entry(crtc
, &dev
->mode_config
.crtc_list
, head
) {
2933 spin_lock(&crtc
->commit_lock
);
2934 commit
= list_first_entry_or_null(&crtc
->commit_list
,
2935 struct drm_crtc_commit
, commit_entry
);
2937 drm_crtc_commit_get(commit
);
2938 spin_unlock(&crtc
->commit_lock
);
2943 /* Make sure all pending HW programming completed and
2946 ret
= wait_for_completion_interruptible_timeout(&commit
->hw_done
, 10*HZ
);
2949 ret
= wait_for_completion_interruptible_timeout(
2950 &commit
->flip_done
, 10*HZ
);
2953 DRM_ERROR("[CRTC:%d:%s] hw_done or flip_done "
2954 "timed out\n", crtc
->base
.id
, crtc
->name
);
2956 drm_crtc_commit_put(commit
);
2959 return ret
< 0 ? ret
: 0;
2962 int amdgpu_dm_atomic_check(struct drm_device
*dev
,
2963 struct drm_atomic_state
*state
)
2965 struct drm_crtc
*crtc
;
2966 struct drm_crtc_state
*crtc_state
;
2967 struct drm_plane
*plane
;
2968 struct drm_plane_state
*plane_state
;
2972 int new_stream_count
;
2973 struct dc_validation_set set
[MAX_STREAMS
] = {{ 0 }};
2974 struct dc_stream
*new_streams
[MAX_STREAMS
] = { 0 };
2975 struct drm_crtc
*crtc_set
[MAX_STREAMS
] = { 0 };
2976 struct amdgpu_device
*adev
= dev
->dev_private
;
2977 struct dc
*dc
= adev
->dm
.dc
;
2978 bool need_to_validate
= false;
2979 struct validate_context
*context
;
2980 struct drm_connector
*connector
;
2981 struct drm_connector_state
*conn_state
;
2983 * This bool will be set for true for any modeset/reset
2984 * or surface update which implies non fast surfae update.
2986 bool aquire_global_lock
= false;
2988 ret
= drm_atomic_helper_check(dev
, state
);
2991 DRM_ERROR("Atomic state validation failed with error :%d !\n",
2998 /* copy existing configuration */
2999 new_stream_count
= 0;
3001 list_for_each_entry(crtc
, &dev
->mode_config
.crtc_list
, head
) {
3003 struct amdgpu_crtc
*acrtc
= to_amdgpu_crtc(crtc
);
3005 if (acrtc
->stream
) {
3006 set
[set_count
].stream
= acrtc
->stream
;
3007 crtc_set
[set_count
] = crtc
;
3012 /* update changed items */
3013 for_each_crtc_in_state(state
, crtc
, crtc_state
, i
) {
3014 struct amdgpu_crtc
*acrtc
= NULL
;
3015 struct amdgpu_connector
*aconnector
= NULL
;
3017 acrtc
= to_amdgpu_crtc(crtc
);
3019 aconnector
= amdgpu_dm_find_first_crct_matching_connector(state
, crtc
, true);
3022 "amdgpu_crtc id:%d crtc_state_flags: enable:%d, active:%d, "
3023 "planes_changed:%d, mode_changed:%d,active_changed:%d,"
3024 "connectors_changed:%d\n",
3028 crtc_state
->planes_changed
,
3029 crtc_state
->mode_changed
,
3030 crtc_state
->active_changed
,
3031 crtc_state
->connectors_changed
);
3033 if (modeset_required(crtc_state
)) {
3035 struct dc_stream
*new_stream
= NULL
;
3036 struct drm_connector_state
*conn_state
= NULL
;
3037 struct dm_connector_state
*dm_state
= NULL
;
3040 conn_state
= drm_atomic_get_connector_state(state
, &aconnector
->base
);
3041 if (IS_ERR(conn_state
))
3043 dm_state
= to_dm_connector_state(conn_state
);
3046 new_stream
= create_stream_for_sink(aconnector
, &crtc_state
->mode
, dm_state
);
3049 * we can have no stream on ACTION_SET if a display
3050 * was disconnected during S3, in this case it not and
3051 * error, the OS will be updated after detection, and
3052 * do the right thing on next atomic commit
3055 DRM_DEBUG_KMS("%s: Failed to create new stream for crtc %d\n",
3056 __func__
, acrtc
->base
.base
.id
);
3060 new_streams
[new_stream_count
] = new_stream
;
3061 set_count
= update_in_val_sets_stream(
3070 need_to_validate
= true;
3071 aquire_global_lock
= true;
3073 } else if (modereset_required(crtc_state
)) {
3075 /* i.e. reset mode */
3076 if (acrtc
->stream
) {
3077 set_count
= remove_from_val_sets(
3081 aquire_global_lock
= true;
3087 * Hack: Commit needs planes right now, specifically for gamma
3088 * TODO rework commit to check CRTC for gamma change
3090 if (crtc_state
->color_mgmt_changed
) {
3092 ret
= drm_atomic_add_affected_planes(state
, crtc
);
3100 /* Check scaling and undersacn changes*/
3101 for_each_connector_in_state(state
, connector
, conn_state
, i
) {
3102 struct amdgpu_connector
*aconnector
= to_amdgpu_connector(connector
);
3103 struct dm_connector_state
*con_old_state
=
3104 to_dm_connector_state(aconnector
->base
.state
);
3105 struct dm_connector_state
*con_new_state
=
3106 to_dm_connector_state(conn_state
);
3107 struct amdgpu_crtc
*acrtc
= to_amdgpu_crtc(con_new_state
->base
.crtc
);
3108 struct dc_stream
*new_stream
;
3110 /* Skip any modesets/resets */
3111 if (!acrtc
|| drm_atomic_crtc_needs_modeset(acrtc
->base
.state
))
3114 /* Skip any thing not scale or underscan chnages */
3115 if (!is_scaling_state_different(con_new_state
, con_old_state
))
3118 new_stream
= create_stream_for_sink(
3120 &acrtc
->base
.state
->mode
,
3124 DRM_ERROR("%s: Failed to create new stream for crtc %d\n",
3125 __func__
, acrtc
->base
.base
.id
);
3129 new_streams
[new_stream_count
] = new_stream
;
3130 set_count
= update_in_val_sets_stream(
3139 need_to_validate
= true;
3140 aquire_global_lock
= true;
3143 for (i
= 0; i
< set_count
; i
++) {
3144 for_each_plane_in_state(state
, plane
, plane_state
, j
) {
3145 struct drm_crtc
*crtc
= plane_state
->crtc
;
3146 struct drm_framebuffer
*fb
= plane_state
->fb
;
3147 struct drm_connector
*connector
;
3148 struct dm_connector_state
*dm_state
= NULL
;
3149 struct drm_crtc_state
*crtc_state
;
3152 /*TODO Implement atomic check for cursor plane */
3153 if (plane
->type
== DRM_PLANE_TYPE_CURSOR
)
3156 if (!fb
|| !crtc
|| crtc_set
[i
] != crtc
||
3157 !crtc
->state
->planes_changed
|| !crtc
->state
->active
)
3161 crtc_state
= drm_atomic_get_crtc_state(state
, crtc
);
3162 pflip_needed
= !state
->allow_modeset
;
3163 if (!pflip_needed
) {
3164 struct dc_surface
*surface
;
3166 list_for_each_entry(connector
,
3167 &dev
->mode_config
.connector_list
, head
) {
3168 if (connector
->state
->crtc
== crtc
) {
3169 dm_state
= to_dm_connector_state(
3176 * This situation happens in the following case:
3177 * we are about to get set mode for connector who's only
3178 * possible crtc (in encoder crtc mask) is used by
3179 * another connector, that is why it will try to
3180 * re-assing crtcs in order to make configuration
3181 * supported. For our implementation we need to make all
3182 * encoders support all crtcs, then this issue will
3183 * never arise again. But to guard code from this issue
3186 * Also it should be needed when used with actual
3187 * drm_atomic_commit ioctl in future
3192 surface
= dc_create_surface(dc
);
3193 fill_plane_attributes(
3194 crtc
->dev
->dev_private
,
3199 add_val_sets_surface(
3205 need_to_validate
= true;
3206 aquire_global_lock
= true;
3211 context
= dc_get_validate_context(dc
, set
, set_count
);
3213 if (need_to_validate
== false || set_count
== 0 || context
) {
3217 * For full updates case when
3218 * removing/adding/updateding streams on once CRTC while flipping
3220 * acquiring global lock will guarantee that any such full
3222 * will wait for completion of any outstanding flip using DRMs
3223 * synchronization events.
3225 if (aquire_global_lock
)
3226 ret
= do_aquire_global_lock(dev
, state
);
3231 dc_resource_validate_ctx_destruct(context
);
3235 for (i
= 0; i
< set_count
; i
++)
3236 for (j
= 0; j
< set
[i
].surface_count
; j
++)
3237 dc_surface_release(set
[i
].surfaces
[j
]);
3239 for (i
= 0; i
< new_stream_count
; i
++)
3240 dc_stream_release(new_streams
[i
]);
3243 if (ret
== -EDEADLK
)
3244 DRM_DEBUG_KMS("Atomic check stopped due to to deadlock.\n");
3245 else if (ret
== -EINTR
|| ret
== -EAGAIN
|| ret
== -ERESTARTSYS
)
3246 DRM_DEBUG_KMS("Atomic check stopped due to to signal.\n");
3248 DRM_ERROR("Atomic check failed.\n");
3254 static bool is_dp_capable_without_timing_msa(
3256 struct amdgpu_connector
*amdgpu_connector
)
3259 bool capable
= false;
3261 if (amdgpu_connector
->dc_link
&&
3262 dm_helpers_dp_read_dpcd(
3264 amdgpu_connector
->dc_link
,
3265 DP_DOWN_STREAM_PORT_COUNT
,
3267 sizeof(dpcd_data
))) {
3268 capable
= (dpcd_data
& DP_MSA_TIMING_PAR_IGNORED
) ? true:false;
3273 void amdgpu_dm_add_sink_to_freesync_module(
3274 struct drm_connector
*connector
,
3278 uint64_t val_capable
;
3279 bool edid_check_required
;
3280 struct detailed_timing
*timing
;
3281 struct detailed_non_pixel
*data
;
3282 struct detailed_data_monitor_range
*range
;
3283 struct amdgpu_connector
*amdgpu_connector
=
3284 to_amdgpu_connector(connector
);
3286 struct drm_device
*dev
= connector
->dev
;
3287 struct amdgpu_device
*adev
= dev
->dev_private
;
3288 edid_check_required
= false;
3289 if (!amdgpu_connector
->dc_sink
) {
3290 DRM_ERROR("dc_sink NULL, could not add free_sync module.\n");
3293 if (!adev
->dm
.freesync_module
)
3296 * if edid non zero restrict freesync only for dp and edp
3299 if (amdgpu_connector
->dc_sink
->sink_signal
== SIGNAL_TYPE_DISPLAY_PORT
3300 || amdgpu_connector
->dc_sink
->sink_signal
== SIGNAL_TYPE_EDP
) {
3301 edid_check_required
= is_dp_capable_without_timing_msa(
3307 if (edid_check_required
== true && (edid
->version
> 1 ||
3308 (edid
->version
== 1 && edid
->revision
> 1))) {
3309 for (i
= 0; i
< 4; i
++) {
3311 timing
= &edid
->detailed_timings
[i
];
3312 data
= &timing
->data
.other_data
;
3313 range
= &data
->data
.range
;
3315 * Check if monitor has continuous frequency mode
3317 if (data
->type
!= EDID_DETAIL_MONITOR_RANGE
)
3320 * Check for flag range limits only. If flag == 1 then
3321 * no additional timing information provided.
3322 * Default GTF, GTF Secondary curve and CVT are not
3325 if (range
->flags
!= 1)
3328 amdgpu_connector
->min_vfreq
= range
->min_vfreq
;
3329 amdgpu_connector
->max_vfreq
= range
->max_vfreq
;
3330 amdgpu_connector
->pixel_clock_mhz
=
3331 range
->pixel_clock_mhz
* 10;
3335 if (amdgpu_connector
->max_vfreq
-
3336 amdgpu_connector
->min_vfreq
> 10) {
3337 amdgpu_connector
->caps
.supported
= true;
3338 amdgpu_connector
->caps
.min_refresh_in_micro_hz
=
3339 amdgpu_connector
->min_vfreq
* 1000000;
3340 amdgpu_connector
->caps
.max_refresh_in_micro_hz
=
3341 amdgpu_connector
->max_vfreq
* 1000000;
3347 * TODO figure out how to notify user-mode or DRM of freesync caps
3348 * once we figure out how to deal with freesync in an upstreamable
3354 void amdgpu_dm_remove_sink_from_freesync_module(
3355 struct drm_connector
*connector
)
3358 * TODO fill in once we figure out how to deal with freesync in
3359 * an upstreamable fashion