@@ -257,6 +257,23 @@ static inline struct vc4_crtc *to_vc4_crtc(struct drm_crtc *crtc)
257257 return container_of (crtc , struct vc4_crtc , base );
258258}
259259
260+ struct vc4_crtc_state {
261+ struct drm_crtc_state base ;
262+
263+ struct {
264+ unsigned int left ;
265+ unsigned int right ;
266+ unsigned int top ;
267+ unsigned int bottom ;
268+ } margins ;
269+ };
270+
271+ static inline struct vc4_crtc_state *
272+ to_vc4_crtc_state (struct drm_crtc_state * crtc_state )
273+ {
274+ return (struct vc4_crtc_state * )crtc_state ;
275+ }
276+
260277struct vc4_fkms_encoder {
261278 struct drm_encoder base ;
262279 bool hdmi_monitor ;
@@ -365,17 +382,127 @@ static int vc4_plane_set_blank(struct drm_plane *plane, bool blank)
365382 return ret ;
366383}
367384
385+ static void vc4_fkms_crtc_get_margins (struct drm_crtc_state * state ,
386+ unsigned int * left , unsigned int * right ,
387+ unsigned int * top , unsigned int * bottom )
388+ {
389+ struct vc4_crtc_state * vc4_state = to_vc4_crtc_state (state );
390+ struct drm_connector_state * conn_state ;
391+ struct drm_connector * conn ;
392+ int i ;
393+
394+ * left = vc4_state -> margins .left ;
395+ * right = vc4_state -> margins .right ;
396+ * top = vc4_state -> margins .top ;
397+ * bottom = vc4_state -> margins .bottom ;
398+
399+ /* We have to interate over all new connector states because
400+ * vc4_fkms_crtc_get_margins() might be called before
401+ * vc4_fkms_crtc_atomic_check() which means margins info in
402+ * vc4_crtc_state might be outdated.
403+ */
404+ for_each_new_connector_in_state (state -> state , conn , conn_state , i ) {
405+ if (conn_state -> crtc != state -> crtc )
406+ continue ;
407+
408+ * left = conn_state -> tv .margins .left ;
409+ * right = conn_state -> tv .margins .right ;
410+ * top = conn_state -> tv .margins .top ;
411+ * bottom = conn_state -> tv .margins .bottom ;
412+ break ;
413+ }
414+ }
415+
416+ static int vc4_fkms_margins_adj (struct drm_plane_state * pstate ,
417+ struct set_plane * plane )
418+ {
419+ unsigned int left , right , top , bottom ;
420+ int adjhdisplay , adjvdisplay ;
421+ struct drm_crtc_state * crtc_state ;
422+
423+ crtc_state = drm_atomic_get_new_crtc_state (pstate -> state ,
424+ pstate -> crtc );
425+
426+ vc4_fkms_crtc_get_margins (crtc_state , & left , & right , & top , & bottom );
427+
428+ if (!left && !right && !top && !bottom )
429+ return 0 ;
430+
431+ if (left + right >= crtc_state -> mode .hdisplay ||
432+ top + bottom >= crtc_state -> mode .vdisplay )
433+ return - EINVAL ;
434+
435+ adjhdisplay = crtc_state -> mode .hdisplay - (left + right );
436+ plane -> dst_x = DIV_ROUND_CLOSEST (plane -> dst_x * adjhdisplay ,
437+ (int )crtc_state -> mode .hdisplay );
438+ plane -> dst_x += left ;
439+ if (plane -> dst_x > (int )(crtc_state -> mode .hdisplay - left ))
440+ plane -> dst_x = crtc_state -> mode .hdisplay - left ;
441+
442+ adjvdisplay = crtc_state -> mode .vdisplay - (top + bottom );
443+ plane -> dst_y = DIV_ROUND_CLOSEST (plane -> dst_y * adjvdisplay ,
444+ (int )crtc_state -> mode .vdisplay );
445+ plane -> dst_y += top ;
446+ if (plane -> dst_y > (int )(crtc_state -> mode .vdisplay - top ))
447+ plane -> dst_y = crtc_state -> mode .vdisplay - top ;
448+
449+ plane -> dst_w = DIV_ROUND_CLOSEST (plane -> dst_w * adjhdisplay ,
450+ crtc_state -> mode .hdisplay );
451+ plane -> dst_h = DIV_ROUND_CLOSEST (plane -> dst_h * adjvdisplay ,
452+ crtc_state -> mode .vdisplay );
453+
454+ if (!plane -> dst_w || !plane -> dst_h )
455+ return - EINVAL ;
456+
457+ return 0 ;
458+ }
459+
368460static void vc4_plane_atomic_update (struct drm_plane * plane ,
369461 struct drm_plane_state * old_state )
370462{
371463 struct drm_plane_state * state = plane -> state ;
464+
465+ /*
466+ * Do NOT set now, as we haven't checked if the crtc is active or not.
467+ * Set from vc4_plane_set_blank instead.
468+ *
469+ * If the CRTC is on (or going to be on) and we're enabled,
470+ * then unblank. Otherwise, stay blank until CRTC enable.
471+ */
472+ if (state -> crtc -> state -> active )
473+ vc4_plane_set_blank (plane , false);
474+ }
475+
476+ static void vc4_plane_atomic_disable (struct drm_plane * plane ,
477+ struct drm_plane_state * old_state )
478+ {
479+ struct drm_plane_state * state = plane -> state ;
480+ struct vc4_fkms_plane * vc4_plane = to_vc4_fkms_plane (plane );
481+
482+ DRM_DEBUG_ATOMIC ("[PLANE:%d:%s] plane disable %dx%d@%d +%d,%d\n" ,
483+ plane -> base .id , plane -> name ,
484+ state -> crtc_w ,
485+ state -> crtc_h ,
486+ vc4_plane -> mb .plane .vc_image_type ,
487+ state -> crtc_x ,
488+ state -> crtc_y );
489+ vc4_plane_set_blank (plane , true);
490+ }
491+
492+ static bool plane_enabled (struct drm_plane_state * state )
493+ {
494+ return state -> fb && state -> crtc ;
495+ }
496+
497+ static int vc4_plane_to_mb (struct drm_plane * plane ,
498+ struct mailbox_set_plane * mb ,
499+ struct drm_plane_state * state )
500+ {
372501 struct drm_framebuffer * fb = state -> fb ;
373502 struct drm_gem_cma_object * bo = drm_fb_cma_get_gem_obj (fb , 0 );
374503 const struct drm_format_info * drm_fmt = fb -> format ;
375504 const struct vc_image_format * vc_fmt =
376505 vc4_get_vc_image_fmt (drm_fmt -> format );
377- struct vc4_fkms_plane * vc4_plane = to_vc4_fkms_plane (plane );
378- struct mailbox_set_plane * mb = & vc4_plane -> mb ;
379506 int num_planes = fb -> format -> num_planes ;
380507 struct drm_display_mode * mode = & state -> crtc -> mode ;
381508 unsigned int rotation = SUPPORTED_ROTATIONS ;
@@ -417,25 +544,7 @@ static void vc4_plane_atomic_update(struct drm_plane *plane,
417544 break ;
418545 }
419546
420- /* FIXME: If the dest rect goes off screen then clip the src rect so we
421- * don't have off-screen pixels.
422- */
423- if (plane -> type == DRM_PLANE_TYPE_CURSOR ) {
424- /* There is no scaling on the cursor plane, therefore the calcs
425- * to alter the source crop as the cursor goes off the screen
426- * are simple.
427- */
428- if (mb -> plane .dst_x + mb -> plane .dst_w > mode -> hdisplay ) {
429- mb -> plane .dst_w = mode -> hdisplay - mb -> plane .dst_x ;
430- mb -> plane .src_w = (mode -> hdisplay - mb -> plane .dst_x )
431- << 16 ;
432- }
433- if (mb -> plane .dst_y + mb -> plane .dst_h > mode -> vdisplay ) {
434- mb -> plane .dst_h = mode -> vdisplay - mb -> plane .dst_y ;
435- mb -> plane .src_h = (mode -> vdisplay - mb -> plane .dst_y )
436- << 16 ;
437- }
438- }
547+ vc4_fkms_margins_adj (state , & mb -> plane );
439548
440549 if (num_planes > 1 ) {
441550 /* Assume this must be YUV */
@@ -525,38 +634,19 @@ static void vc4_plane_atomic_update(struct drm_plane *plane,
525634 state -> alpha ,
526635 state -> normalized_zpos );
527636
528- /*
529- * Do NOT set now, as we haven't checked if the crtc is active or not.
530- * Set from vc4_plane_set_blank instead.
531- *
532- * If the CRTC is on (or going to be on) and we're enabled,
533- * then unblank. Otherwise, stay blank until CRTC enable.
534- */
535- if (state -> crtc -> state -> active )
536- vc4_plane_set_blank (plane , false);
637+ return 0 ;
537638}
538639
539- static void vc4_plane_atomic_disable (struct drm_plane * plane ,
540- struct drm_plane_state * old_state )
640+ static int vc4_plane_atomic_check (struct drm_plane * plane ,
641+ struct drm_plane_state * state )
541642{
542- //struct vc4_dev *vc4 = to_vc4_dev(plane->dev);
543- struct drm_plane_state * state = plane -> state ;
544643 struct vc4_fkms_plane * vc4_plane = to_vc4_fkms_plane (plane );
545644
546- DRM_DEBUG_ATOMIC ("[PLANE:%d:%s] plane disable %dx%d@%d +%d,%d\n" ,
547- plane -> base .id , plane -> name ,
548- state -> crtc_w ,
549- state -> crtc_h ,
550- vc4_plane -> mb .plane .vc_image_type ,
551- state -> crtc_x ,
552- state -> crtc_y );
553- vc4_plane_set_blank (plane , true);
554- }
645+ if (!plane_enabled (state ))
646+ return 0 ;
647+
648+ return vc4_plane_to_mb (plane , & vc4_plane -> mb , state );
555649
556- static int vc4_plane_atomic_check (struct drm_plane * plane ,
557- struct drm_plane_state * state )
558- {
559- return 0 ;
560650}
561651
562652/* Called during init to allocate the plane's atomic state. */
@@ -907,8 +997,23 @@ vc4_crtc_mode_valid(struct drm_crtc *crtc, const struct drm_display_mode *mode)
907997static int vc4_crtc_atomic_check (struct drm_crtc * crtc ,
908998 struct drm_crtc_state * state )
909999{
910- DRM_DEBUG_KMS ("[CRTC:%d] crtc_atomic_check.\n" ,
911- crtc -> base .id );
1000+ struct vc4_crtc_state * vc4_state = to_vc4_crtc_state (state );
1001+ struct drm_connector * conn ;
1002+ struct drm_connector_state * conn_state ;
1003+ int i ;
1004+
1005+ DRM_DEBUG_KMS ("[CRTC:%d] crtc_atomic_check.\n" , crtc -> base .id );
1006+
1007+ for_each_new_connector_in_state (state -> state , conn , conn_state , i ) {
1008+ if (conn_state -> crtc != crtc )
1009+ continue ;
1010+
1011+ vc4_state -> margins .left = conn_state -> tv .margins .left ;
1012+ vc4_state -> margins .right = conn_state -> tv .margins .right ;
1013+ vc4_state -> margins .top = conn_state -> tv .margins .top ;
1014+ vc4_state -> margins .bottom = conn_state -> tv .margins .bottom ;
1015+ break ;
1016+ }
9121017 return 0 ;
9131018}
9141019
@@ -1009,6 +1114,33 @@ static int vc4_page_flip(struct drm_crtc *crtc,
10091114 return drm_atomic_helper_page_flip (crtc , fb , event , flags , ctx );
10101115}
10111116
1117+ static struct drm_crtc_state *
1118+ vc4_crtc_duplicate_state (struct drm_crtc * crtc )
1119+ {
1120+ struct vc4_crtc_state * vc4_state , * old_vc4_state ;
1121+
1122+ vc4_state = kzalloc (sizeof (* vc4_state ), GFP_KERNEL );
1123+ if (!vc4_state )
1124+ return NULL ;
1125+
1126+ old_vc4_state = to_vc4_crtc_state (crtc -> state );
1127+ vc4_state -> margins = old_vc4_state -> margins ;
1128+
1129+ __drm_atomic_helper_crtc_duplicate_state (crtc , & vc4_state -> base );
1130+ return & vc4_state -> base ;
1131+ }
1132+
1133+ static void
1134+ vc4_crtc_reset (struct drm_crtc * crtc )
1135+ {
1136+ if (crtc -> state )
1137+ __drm_atomic_helper_crtc_destroy_state (crtc -> state );
1138+
1139+ crtc -> state = kzalloc (sizeof (* crtc -> state ), GFP_KERNEL );
1140+ if (crtc -> state )
1141+ crtc -> state -> crtc = crtc ;
1142+ }
1143+
10121144static int vc4_fkms_enable_vblank (struct drm_crtc * crtc )
10131145{
10141146 struct vc4_crtc * vc4_crtc = to_vc4_crtc (crtc );
@@ -1036,8 +1168,8 @@ static const struct drm_crtc_funcs vc4_crtc_funcs = {
10361168 .set_property = NULL ,
10371169 .cursor_set = NULL , /* handled by drm_mode_cursor_universal */
10381170 .cursor_move = NULL , /* handled by drm_mode_cursor_universal */
1039- .reset = drm_atomic_helper_crtc_reset ,
1040- .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state ,
1171+ .reset = vc4_crtc_reset ,
1172+ .atomic_duplicate_state = vc4_crtc_duplicate_state ,
10411173 .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state ,
10421174 .enable_vblank = vc4_fkms_enable_vblank ,
10431175 .disable_vblank = vc4_fkms_disable_vblank ,
@@ -1289,6 +1421,13 @@ vc4_fkms_connector_init(struct drm_device *dev, struct drm_encoder *encoder,
12891421 connector -> interlace_allowed = 0 ;
12901422 }
12911423
1424+ /* Create and attach TV margin props to this connector. */
1425+ ret = drm_mode_create_tv_margin_properties (dev );
1426+ if (ret )
1427+ return ERR_PTR (ret );
1428+
1429+ drm_connector_attach_tv_margin_properties (connector );
1430+
12921431 connector -> polled = (DRM_CONNECTOR_POLL_CONNECT |
12931432 DRM_CONNECTOR_POLL_DISCONNECT );
12941433
0 commit comments