1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) STMicroelectronics SA 2014
4  * Authors: Benjamin Gaignard <benjamin.gaignard@st.com>
5  *          Fabien Dessenne <fabien.dessenne@st.com>
6  *          for STMicroelectronics.
7  */
8 
9 #include <linux/types.h>
10 
11 #include <drm/drm_fb_cma_helper.h>
12 #include <drm/drm_fourcc.h>
13 #include <drm/drm_gem_cma_helper.h>
14 
15 #include "sti_compositor.h"
16 #include "sti_drv.h"
17 #include "sti_plane.h"
18 
sti_plane_to_str(struct sti_plane * plane)19 const char *sti_plane_to_str(struct sti_plane *plane)
20 {
21 	switch (plane->desc) {
22 	case STI_GDP_0:
23 		return "GDP0";
24 	case STI_GDP_1:
25 		return "GDP1";
26 	case STI_GDP_2:
27 		return "GDP2";
28 	case STI_GDP_3:
29 		return "GDP3";
30 	case STI_HQVDP_0:
31 		return "HQVDP0";
32 	case STI_CURSOR:
33 		return "CURSOR";
34 	default:
35 		return "<UNKNOWN PLANE>";
36 	}
37 }
38 
39 #define STI_FPS_INTERVAL_MS     3000
40 
sti_plane_update_fps(struct sti_plane * plane,bool new_frame,bool new_field)41 void sti_plane_update_fps(struct sti_plane *plane,
42 			  bool new_frame,
43 			  bool new_field)
44 {
45 	struct drm_plane_state *state = plane->drm_plane.state;
46 	ktime_t now;
47 	struct sti_fps_info *fps;
48 	int fpks, fipks, ms_since_last, num_frames, num_fields;
49 
50 	now = ktime_get();
51 
52 	/* Compute number of frame updates */
53 	fps = &plane->fps_info;
54 
55 	if (new_field)
56 		fps->curr_field_counter++;
57 
58 	/* do not perform fps calcul if new_frame is false */
59 	if (!new_frame)
60 		return;
61 
62 	fps->curr_frame_counter++;
63 	ms_since_last = ktime_to_ms(ktime_sub(now, fps->last_timestamp));
64 	num_frames = fps->curr_frame_counter - fps->last_frame_counter;
65 
66 	if (num_frames <= 0  || ms_since_last < STI_FPS_INTERVAL_MS)
67 		return;
68 
69 	fps->last_timestamp = now;
70 	fps->last_frame_counter = fps->curr_frame_counter;
71 
72 	if (state->fb) {
73 		fpks = (num_frames * 1000000) / ms_since_last;
74 		snprintf(plane->fps_info.fps_str, FPS_LENGTH,
75 			 "%-8s %4dx%-4d %.4s @ %3d.%-3.3d fps (%s)",
76 			 plane->drm_plane.name,
77 			 state->fb->width,
78 			 state->fb->height,
79 			 (char *)&state->fb->format->format,
80 			 fpks / 1000, fpks % 1000,
81 			 sti_plane_to_str(plane));
82 	}
83 
84 	if (fps->curr_field_counter) {
85 		/* Compute number of field updates */
86 		num_fields = fps->curr_field_counter - fps->last_field_counter;
87 		fps->last_field_counter = fps->curr_field_counter;
88 		fipks = (num_fields * 1000000) / ms_since_last;
89 		snprintf(plane->fps_info.fips_str,
90 			 FPS_LENGTH, " - %3d.%-3.3d field/sec",
91 			 fipks / 1000, fipks % 1000);
92 	} else {
93 		plane->fps_info.fips_str[0] = '\0';
94 	}
95 
96 	if (fps->output)
97 		DRM_INFO("%s%s\n",
98 			 plane->fps_info.fps_str,
99 			 plane->fps_info.fips_str);
100 }
101 
sti_plane_get_default_zpos(enum drm_plane_type type)102 static int sti_plane_get_default_zpos(enum drm_plane_type type)
103 {
104 	switch (type) {
105 	case DRM_PLANE_TYPE_PRIMARY:
106 		return 0;
107 	case DRM_PLANE_TYPE_OVERLAY:
108 		return 1;
109 	case DRM_PLANE_TYPE_CURSOR:
110 		return 7;
111 	}
112 	return 0;
113 }
114 
sti_plane_attach_zorder_property(struct drm_plane * drm_plane,enum drm_plane_type type)115 static void sti_plane_attach_zorder_property(struct drm_plane *drm_plane,
116 					     enum drm_plane_type type)
117 {
118 	int zpos = sti_plane_get_default_zpos(type);
119 
120 	switch (type) {
121 	case DRM_PLANE_TYPE_PRIMARY:
122 	case DRM_PLANE_TYPE_OVERLAY:
123 		drm_plane_create_zpos_property(drm_plane, zpos, 0, 6);
124 		break;
125 	case DRM_PLANE_TYPE_CURSOR:
126 		drm_plane_create_zpos_immutable_property(drm_plane, zpos);
127 		break;
128 	}
129 }
130 
sti_plane_init_property(struct sti_plane * plane,enum drm_plane_type type)131 void sti_plane_init_property(struct sti_plane *plane,
132 			     enum drm_plane_type type)
133 {
134 	sti_plane_attach_zorder_property(&plane->drm_plane, type);
135 
136 	DRM_DEBUG_DRIVER("drm plane:%d mapped to %s\n",
137 			 plane->drm_plane.base.id, sti_plane_to_str(plane));
138 }
139