blob: 821f93f87b2e2ad25f485e53714dee21581d45b4 [file] [log] [blame]
Dhaval Patel14d46ce2017-01-17 16:28:12 -08001/*
2 * Copyright (c) 2014-2017 The Linux Foundation. All rights reserved.
3 * Copyright (C) 2013 Red Hat
4 * Author: Rob Clark <robdclark@gmail.com>
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07005 *
Dhaval Patel14d46ce2017-01-17 16:28:12 -08006 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published by
8 * the Free Software Foundation.
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07009 *
Dhaval Patel14d46ce2017-01-17 16:28:12 -080010 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program. If not, see <http://www.gnu.org/licenses/>.
Narendra Muppalla1b0b3352015-09-29 10:16:51 -070017 */
18
Clarence Ipd9f9fa62016-09-09 13:42:32 -040019#define pr_fmt(fmt) "[drm:%s:%d] " fmt, __func__, __LINE__
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -040020#include <linux/sort.h>
Clarence Ip8f7366c2016-07-05 12:15:26 -040021#include <linux/debugfs.h>
Clarence Ipcae1bb62016-07-07 12:07:13 -040022#include <linux/ktime.h>
Clarence Ip4c1d9772016-06-26 09:35:38 -040023#include <uapi/drm/sde_drm.h>
Narendra Muppalla1b0b3352015-09-29 10:16:51 -070024#include <drm/drm_mode.h>
25#include <drm/drm_crtc.h>
26#include <drm/drm_crtc_helper.h>
27#include <drm/drm_flip_work.h>
28
29#include "sde_kms.h"
30#include "sde_hw_lm.h"
Clarence Ipc475b082016-06-26 09:27:23 -040031#include "sde_hw_ctl.h"
Abhijit Kulkarni40e38162016-06-26 22:12:09 -040032#include "sde_crtc.h"
Alan Kwong83285fb2016-10-21 20:51:17 -040033#include "sde_plane.h"
Gopikrishnaiah Anandane0e5e0c2016-05-25 11:05:33 -070034#include "sde_color_processing.h"
Alan Kwong83285fb2016-10-21 20:51:17 -040035#include "sde_encoder.h"
36#include "sde_connector.h"
Alan Kwong67a3f792016-11-01 23:16:53 -040037#include "sde_power_handle.h"
Alan Kwong9aa061c2016-11-06 21:17:12 -050038#include "sde_core_perf.h"
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -040039
Clarence Ipcae1bb62016-07-07 12:07:13 -040040/* default input fence timeout, in ms */
41#define SDE_CRTC_INPUT_FENCE_TIMEOUT 2000
42
Dhaval Patel4e574842016-08-23 15:11:37 -070043/*
44 * The default input fence timeout is 2 seconds while max allowed
45 * range is 10 seconds. Any value above 10 seconds adds glitches beyond
46 * tolerance limit.
47 */
48#define SDE_CRTC_MAX_INPUT_FENCE_TIMEOUT 10000
49
Dhaval Patel48c76022016-09-01 17:51:23 -070050/* layer mixer index on sde_crtc */
51#define LEFT_MIXER 0
52#define RIGHT_MIXER 1
53
Lloyd Atkinson4f1c8692016-09-14 14:04:25 -040054static inline struct sde_kms *_sde_crtc_get_kms(struct drm_crtc *crtc)
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -040055{
56 struct msm_drm_private *priv = crtc->dev->dev_private;
Abhijit Kulkarni40e38162016-06-26 22:12:09 -040057
Ben Chan78647cd2016-06-26 22:02:47 -040058 return to_sde_kms(priv->kms);
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -040059}
60
Narendra Muppalla1b0b3352015-09-29 10:16:51 -070061static void sde_crtc_destroy(struct drm_crtc *crtc)
62{
63 struct sde_crtc *sde_crtc = to_sde_crtc(crtc);
64
Lloyd Atkinson4f1c8692016-09-14 14:04:25 -040065 SDE_DEBUG("\n");
Clarence Ip7a753bb2016-07-07 11:47:44 -040066
67 if (!crtc)
68 return;
69
Dhaval Patele4a5dda2016-10-13 19:29:30 -070070 if (sde_crtc->blob_info)
71 drm_property_unreference_blob(sde_crtc->blob_info);
Clarence Ip7a753bb2016-07-07 11:47:44 -040072 msm_property_destroy(&sde_crtc->property_info);
Gopikrishnaiah Anandane0e5e0c2016-05-25 11:05:33 -070073 sde_cp_crtc_destroy_properties(crtc);
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -070074
Clarence Ip8f7366c2016-07-05 12:15:26 -040075 debugfs_remove_recursive(sde_crtc->debugfs_root);
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -070076 mutex_destroy(&sde_crtc->crtc_lock);
Clarence Ip24f80662016-06-13 19:05:32 -040077 sde_fence_deinit(&sde_crtc->output_fence);
Clarence Ip7a753bb2016-07-07 11:47:44 -040078
Narendra Muppalla1b0b3352015-09-29 10:16:51 -070079 drm_crtc_cleanup(crtc);
80 kfree(sde_crtc);
81}
82
Narendra Muppalla1b0b3352015-09-29 10:16:51 -070083static bool sde_crtc_mode_fixup(struct drm_crtc *crtc,
84 const struct drm_display_mode *mode,
85 struct drm_display_mode *adjusted_mode)
86{
Lloyd Atkinson4f1c8692016-09-14 14:04:25 -040087 SDE_DEBUG("\n");
Lloyd Atkinsonaf7952d2016-06-26 22:41:26 -040088
Lloyd Atkinson4f1c8692016-09-14 14:04:25 -040089 if (msm_is_mode_seamless(adjusted_mode) &&
90 (!crtc->enabled || crtc->state->active_changed)) {
91 SDE_ERROR("crtc state prevents seamless transition\n");
92 return false;
Lloyd Atkinsonaf7952d2016-06-26 22:41:26 -040093 }
94
Narendra Muppalla1b0b3352015-09-29 10:16:51 -070095 return true;
96}
97
Dhaval Patel48c76022016-09-01 17:51:23 -070098static void _sde_crtc_setup_blend_cfg(struct sde_crtc_mixer *mixer,
99 struct sde_plane_state *pstate, struct sde_format *format)
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -0400100{
Dhaval Patel48c76022016-09-01 17:51:23 -0700101 uint32_t blend_op, fg_alpha, bg_alpha;
102 uint32_t blend_type;
Dhaval Patel44f12472016-08-29 12:19:47 -0700103 struct sde_hw_mixer *lm = mixer->hw_lm;
104
Dhaval Patel48c76022016-09-01 17:51:23 -0700105 /* default to opaque blending */
106 fg_alpha = sde_plane_get_property(pstate, PLANE_PROP_ALPHA);
107 bg_alpha = 0xFF - fg_alpha;
108 blend_op = SDE_BLEND_FG_ALPHA_FG_CONST | SDE_BLEND_BG_ALPHA_BG_CONST;
109 blend_type = sde_plane_get_property(pstate, PLANE_PROP_BLEND_OP);
Dhaval Patel44f12472016-08-29 12:19:47 -0700110
Dhaval Patel48c76022016-09-01 17:51:23 -0700111 SDE_DEBUG("blend type:0x%x blend alpha:0x%x\n", blend_type, fg_alpha);
112
113 switch (blend_type) {
114
115 case SDE_DRM_BLEND_OP_OPAQUE:
116 blend_op = SDE_BLEND_FG_ALPHA_FG_CONST |
117 SDE_BLEND_BG_ALPHA_BG_CONST;
118 break;
119
120 case SDE_DRM_BLEND_OP_PREMULTIPLIED:
121 if (format->alpha_enable) {
122 blend_op = SDE_BLEND_FG_ALPHA_FG_CONST |
123 SDE_BLEND_BG_ALPHA_FG_PIXEL;
124 if (fg_alpha != 0xff) {
125 bg_alpha = fg_alpha;
126 blend_op |= SDE_BLEND_BG_MOD_ALPHA |
127 SDE_BLEND_BG_INV_MOD_ALPHA;
128 } else {
129 blend_op |= SDE_BLEND_BG_INV_ALPHA;
130 }
131 }
132 break;
133
134 case SDE_DRM_BLEND_OP_COVERAGE:
135 if (format->alpha_enable) {
136 blend_op = SDE_BLEND_FG_ALPHA_FG_PIXEL |
137 SDE_BLEND_BG_ALPHA_FG_PIXEL;
138 if (fg_alpha != 0xff) {
139 bg_alpha = fg_alpha;
140 blend_op |= SDE_BLEND_FG_MOD_ALPHA |
141 SDE_BLEND_FG_INV_MOD_ALPHA |
142 SDE_BLEND_BG_MOD_ALPHA |
143 SDE_BLEND_BG_INV_MOD_ALPHA;
144 } else {
145 blend_op |= SDE_BLEND_BG_INV_ALPHA;
146 }
147 }
148 break;
149 default:
150 /* do nothing */
151 break;
Clarence Ipd9f9fa62016-09-09 13:42:32 -0400152 }
Dhaval Patel48c76022016-09-01 17:51:23 -0700153
154 lm->ops.setup_blend_config(lm, pstate->stage, fg_alpha,
155 bg_alpha, blend_op);
156 SDE_DEBUG("format 0x%x, alpha_enable %u fg alpha:0x%x bg alpha:0x%x \"\
157 blend_op:0x%x\n", format->base.pixel_format,
158 format->alpha_enable, fg_alpha, bg_alpha, blend_op);
159}
160
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -0800161static void _sde_crtc_setup_dim_layer_cfg(struct drm_crtc *crtc,
162 struct sde_crtc *sde_crtc, struct sde_crtc_mixer *mixer,
163 struct sde_hw_dim_layer *dim_layer)
164{
165 struct sde_hw_mixer *lm;
166 struct sde_rect mixer_rect;
167 struct sde_hw_dim_layer split_dim_layer;
168 u32 mixer_width, mixer_height;
169 int i;
170
171 if (!dim_layer->rect.w || !dim_layer->rect.h) {
172 SDE_DEBUG("empty dim layer\n");
173 return;
174 }
175
176 mixer_width = get_crtc_split_width(crtc);
177 mixer_height = get_crtc_mixer_height(crtc);
178 mixer_rect = (struct sde_rect) {0, 0, mixer_width, mixer_height};
179
180 split_dim_layer.stage = dim_layer->stage;
181 split_dim_layer.color_fill = dim_layer->color_fill;
182
183 /*
184 * traverse through the layer mixers attached to crtc and find the
185 * intersecting dim layer rect in each LM and program accordingly.
186 */
187 for (i = 0; i < sde_crtc->num_mixers; i++) {
188 split_dim_layer.flags = dim_layer->flags;
189 mixer_rect.x = i * mixer_width;
190
191 sde_kms_rect_intersect(&split_dim_layer.rect, &mixer_rect,
192 &dim_layer->rect);
193 if (!split_dim_layer.rect.w && !split_dim_layer.rect.h) {
194 /*
195 * no extra programming required for non-intersecting
196 * layer mixers with INCLUSIVE dim layer
197 */
198 if (split_dim_layer.flags
199 & SDE_DRM_DIM_LAYER_INCLUSIVE)
200 continue;
201
202 /*
203 * program the other non-intersecting layer mixers with
204 * INCLUSIVE dim layer of full size for uniformity
205 * with EXCLUSIVE dim layer config.
206 */
207 split_dim_layer.flags &= ~SDE_DRM_DIM_LAYER_EXCLUSIVE;
208 split_dim_layer.flags |= SDE_DRM_DIM_LAYER_INCLUSIVE;
209 split_dim_layer.rect = (struct sde_rect) {0, 0,
210 mixer_width, mixer_height};
211
212 } else {
213 split_dim_layer.rect.x = split_dim_layer.rect.x
214 - (i * mixer_width);
215 }
216
217 lm = mixer[i].hw_lm;
218 mixer[i].mixer_op_mode |= 1 << split_dim_layer.stage;
219 lm->ops.setup_dim_layer(lm, &split_dim_layer);
220 }
221}
222
Dhaval Patel48c76022016-09-01 17:51:23 -0700223static void _sde_crtc_blend_setup_mixer(struct drm_crtc *crtc,
224 struct sde_crtc *sde_crtc, struct sde_crtc_mixer *mixer)
225{
226 struct drm_plane *plane;
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -0800227 struct sde_crtc_state *cstate;
Dhaval Patel48c76022016-09-01 17:51:23 -0700228 struct sde_plane_state *pstate = NULL;
229 struct sde_format *format;
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -0800230 struct sde_hw_ctl *ctl;
231 struct sde_hw_mixer *lm;
232 struct sde_hw_stage_cfg *stage_cfg;
Dhaval Patel48c76022016-09-01 17:51:23 -0700233
234 u32 flush_mask = 0, crtc_split_width;
235 uint32_t lm_idx = LEFT_MIXER, idx;
236 bool bg_alpha_enable[CRTC_DUAL_MIXERS] = {false};
237 bool lm_right = false;
238 int left_crtc_zpos_cnt[SDE_STAGE_MAX + 1] = {0};
239 int right_crtc_zpos_cnt[SDE_STAGE_MAX + 1] = {0};
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -0800240 int i;
Dhaval Patel48c76022016-09-01 17:51:23 -0700241
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -0800242 if (!sde_crtc || !mixer) {
243 SDE_ERROR("invalid sde_crtc or mixer\n");
244 return;
245 }
246
247 ctl = mixer->hw_ctl;
248 lm = mixer->hw_lm;
249 stage_cfg = &sde_crtc->stage_cfg;
Dhaval Patel48c76022016-09-01 17:51:23 -0700250 crtc_split_width = get_crtc_split_width(crtc);
Dhaval Patel44f12472016-08-29 12:19:47 -0700251
252 drm_atomic_crtc_for_each_plane(plane, crtc) {
Dhaval Patel48c76022016-09-01 17:51:23 -0700253
Dhaval Patel44f12472016-08-29 12:19:47 -0700254 pstate = to_sde_plane_state(plane->state);
Dhaval Patel44f12472016-08-29 12:19:47 -0700255
Dhaval Patel48c76022016-09-01 17:51:23 -0700256 flush_mask = ctl->ops.get_bitmask_sspp(ctl,
257 sde_plane_pipe(plane));
Dhaval Patel44f12472016-08-29 12:19:47 -0700258
Dhaval Patel48c76022016-09-01 17:51:23 -0700259 /* always stage plane on either left or right lm */
260 if (plane->state->crtc_x >= crtc_split_width) {
261 lm_idx = RIGHT_MIXER;
262 idx = right_crtc_zpos_cnt[pstate->stage]++;
263 } else {
264 lm_idx = LEFT_MIXER;
265 idx = left_crtc_zpos_cnt[pstate->stage]++;
266 }
267
268 /* stage plane on right LM if it crosses the boundary */
269 lm_right = (lm_idx == LEFT_MIXER) &&
270 (plane->state->crtc_x + plane->state->crtc_w >
271 crtc_split_width);
272
273 stage_cfg->stage[lm_idx][pstate->stage][idx] =
274 sde_plane_pipe(plane);
Jeykumar Sankaran2e655032017-02-04 14:05:45 -0800275 stage_cfg->multirect_index
276 [lm_idx][pstate->stage][idx] =
277 pstate->multirect_index;
Dhaval Patel48c76022016-09-01 17:51:23 -0700278 mixer[lm_idx].flush_mask |= flush_mask;
279
280 SDE_DEBUG("crtc %d stage:%d - plane %d sspp %d fb %d\n",
Clarence Ipd9f9fa62016-09-09 13:42:32 -0400281 crtc->base.id,
Clarence Ipd9f9fa62016-09-09 13:42:32 -0400282 pstate->stage,
283 plane->base.id,
284 sde_plane_pipe(plane) - SSPP_VIG0,
285 plane->state->fb ?
286 plane->state->fb->base.id : -1);
Dhaval Patel44f12472016-08-29 12:19:47 -0700287
Dhaval Patel48c76022016-09-01 17:51:23 -0700288 format = to_sde_format(msm_framebuffer_format(pstate->base.fb));
Dhaval Patel44f12472016-08-29 12:19:47 -0700289
Dhaval Patel48c76022016-09-01 17:51:23 -0700290 /* blend config update */
291 if (pstate->stage != SDE_STAGE_BASE) {
292 _sde_crtc_setup_blend_cfg(mixer + lm_idx, pstate,
293 format);
294
295 if (bg_alpha_enable[lm_idx] && !format->alpha_enable)
296 mixer[lm_idx].mixer_op_mode = 0;
297 else
298 mixer[lm_idx].mixer_op_mode |=
299 1 << pstate->stage;
300 } else if (format->alpha_enable) {
301 bg_alpha_enable[lm_idx] = true;
302 }
303
304 if (lm_right) {
305 idx = right_crtc_zpos_cnt[pstate->stage]++;
306 stage_cfg->stage[RIGHT_MIXER][pstate->stage][idx] =
307 sde_plane_pipe(plane);
Jeykumar Sankaran2e655032017-02-04 14:05:45 -0800308 stage_cfg->multirect_index
309 [RIGHT_MIXER][pstate->stage][idx] =
310 pstate->multirect_index;
Dhaval Patel48c76022016-09-01 17:51:23 -0700311 mixer[RIGHT_MIXER].flush_mask |= flush_mask;
312
313 /* blend config update */
314 if (pstate->stage != SDE_STAGE_BASE) {
315 _sde_crtc_setup_blend_cfg(mixer + RIGHT_MIXER,
316 pstate, format);
317
318 if (bg_alpha_enable[RIGHT_MIXER] &&
319 !format->alpha_enable)
320 mixer[RIGHT_MIXER].mixer_op_mode = 0;
321 else
322 mixer[RIGHT_MIXER].mixer_op_mode |=
323 1 << pstate->stage;
324 } else if (format->alpha_enable) {
325 bg_alpha_enable[RIGHT_MIXER] = true;
326 }
327 }
Dhaval Patel44f12472016-08-29 12:19:47 -0700328 }
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -0800329
330 if (lm && lm->ops.setup_dim_layer) {
331 cstate = to_sde_crtc_state(crtc->state);
332 for (i = 0; i < cstate->num_dim_layers; i++)
333 _sde_crtc_setup_dim_layer_cfg(crtc, sde_crtc,
334 mixer, &cstate->dim_layer[i]);
335 }
Dhaval Patel44f12472016-08-29 12:19:47 -0700336}
337
Clarence Ipd9f9fa62016-09-09 13:42:32 -0400338/**
339 * _sde_crtc_blend_setup - configure crtc mixers
340 * @crtc: Pointer to drm crtc structure
341 */
342static void _sde_crtc_blend_setup(struct drm_crtc *crtc)
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -0400343{
344 struct sde_crtc *sde_crtc = to_sde_crtc(crtc);
Lloyd Atkinsone7bcdd22016-08-11 10:53:37 -0400345 struct sde_crtc_mixer *mixer = sde_crtc->mixers;
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -0400346 struct sde_hw_ctl *ctl;
347 struct sde_hw_mixer *lm;
Dhaval Patel44f12472016-08-29 12:19:47 -0700348
349 int i;
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -0400350
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -0400351 SDE_DEBUG("%s\n", sde_crtc->name);
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -0400352
Dhaval Patel48c76022016-09-01 17:51:23 -0700353 if (sde_crtc->num_mixers > CRTC_DUAL_MIXERS) {
354 SDE_ERROR("invalid number mixers: %d\n", sde_crtc->num_mixers);
355 return;
356 }
357
358 for (i = 0; i < sde_crtc->num_mixers; i++) {
359 if (!mixer[i].hw_lm || !mixer[i].hw_ctl) {
360 SDE_ERROR("invalid lm or ctl assigned to mixer\n");
361 return;
362 }
363 mixer[i].mixer_op_mode = 0;
364 mixer[i].flush_mask = 0;
Lloyd Atkinsone5ec30d2016-08-23 14:32:32 -0400365 if (mixer[i].hw_ctl->ops.clear_all_blendstages)
366 mixer[i].hw_ctl->ops.clear_all_blendstages(
367 mixer[i].hw_ctl);
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -0800368
369 /* clear dim_layer settings */
370 lm = mixer[i].hw_lm;
371 if (lm->ops.clear_dim_layer)
372 lm->ops.clear_dim_layer(lm);
Dhaval Patel48c76022016-09-01 17:51:23 -0700373 }
374
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -0400375 /* initialize stage cfg */
Clarence Ip8f7366c2016-07-05 12:15:26 -0400376 memset(&sde_crtc->stage_cfg, 0, sizeof(struct sde_hw_stage_cfg));
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -0400377
Dhaval Patel48c76022016-09-01 17:51:23 -0700378 _sde_crtc_blend_setup_mixer(crtc, sde_crtc, mixer);
379
Abhijit Kulkarni71002ba2016-06-24 18:36:28 -0400380 for (i = 0; i < sde_crtc->num_mixers; i++) {
Abhijit Kulkarni71002ba2016-06-24 18:36:28 -0400381 ctl = mixer[i].hw_ctl;
382 lm = mixer[i].hw_lm;
Abhijit Kulkarni71002ba2016-06-24 18:36:28 -0400383
Dhaval Patel48c76022016-09-01 17:51:23 -0700384 lm->ops.setup_alpha_out(lm, mixer[i].mixer_op_mode);
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -0400385
Dhaval Patel48c76022016-09-01 17:51:23 -0700386 mixer[i].flush_mask |= ctl->ops.get_bitmask_mixer(ctl,
Abhijit Kulkarni71002ba2016-06-24 18:36:28 -0400387 mixer[i].hw_lm->idx);
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -0400388
389 /* stage config flush mask */
Dhaval Patel48c76022016-09-01 17:51:23 -0700390 ctl->ops.update_pending_flush(ctl, mixer[i].flush_mask);
391
Clarence Ip8e69ad02016-12-09 09:43:57 -0500392 SDE_DEBUG("lm %d, op_mode 0x%X, ctl %d, flush mask 0x%x\n",
393 mixer[i].hw_lm->idx - LM_0,
394 mixer[i].mixer_op_mode,
395 ctl->idx - CTL_0,
396 mixer[i].flush_mask);
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -0400397
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -0400398 ctl->ops.setup_blendstage(ctl, mixer[i].hw_lm->idx,
Dhaval Patel44f12472016-08-29 12:19:47 -0700399 &sde_crtc->stage_cfg, i);
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -0400400 }
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -0400401}
402
Clarence Ip0d0e96d2016-10-24 18:13:13 -0400403void sde_crtc_prepare_commit(struct drm_crtc *crtc,
404 struct drm_crtc_state *old_state)
Clarence Ip24f80662016-06-13 19:05:32 -0400405{
406 struct sde_crtc *sde_crtc;
Clarence Ip0d0e96d2016-10-24 18:13:13 -0400407 struct sde_crtc_state *cstate;
408 struct drm_connector *conn;
Clarence Ip24f80662016-06-13 19:05:32 -0400409
Clarence Ip0d0e96d2016-10-24 18:13:13 -0400410 if (!crtc || !crtc->state) {
Clarence Ip24f80662016-06-13 19:05:32 -0400411 SDE_ERROR("invalid crtc\n");
412 return;
413 }
414
415 sde_crtc = to_sde_crtc(crtc);
Clarence Ip0d0e96d2016-10-24 18:13:13 -0400416 cstate = to_sde_crtc_state(crtc->state);
Lloyd Atkinson5d40d312016-09-06 08:34:13 -0400417 SDE_EVT32(DRMID(crtc));
Clarence Ip24f80662016-06-13 19:05:32 -0400418
Clarence Ip0d0e96d2016-10-24 18:13:13 -0400419 /* identify connectors attached to this crtc */
Clarence Ip0d0e96d2016-10-24 18:13:13 -0400420 cstate->num_connectors = 0;
421
422 drm_for_each_connector(conn, crtc->dev)
423 if (conn->state && conn->state->crtc == crtc &&
424 cstate->num_connectors < MAX_CONNECTORS) {
425 cstate->connectors[cstate->num_connectors++] = conn;
426 sde_connector_prepare_fence(conn);
Clarence Ip0d0e96d2016-10-24 18:13:13 -0400427 }
428
Alan Kwong67a3f792016-11-01 23:16:53 -0400429 if (cstate->num_connectors > 0 && cstate->connectors[0]->encoder)
430 cstate->intf_mode = sde_encoder_get_intf_mode(
431 cstate->connectors[0]->encoder);
432 else
433 cstate->intf_mode = INTF_MODE_NONE;
434
Clarence Ip0d0e96d2016-10-24 18:13:13 -0400435 /* prepare main output fence */
Clarence Ip24f80662016-06-13 19:05:32 -0400436 sde_fence_prepare(&sde_crtc->output_fence);
437}
438
Abhijit Kulkarni40e38162016-06-26 22:12:09 -0400439/* if file!=NULL, this is preclose potential cancel-flip path */
Lloyd Atkinson4f1c8692016-09-14 14:04:25 -0400440static void _sde_crtc_complete_flip(struct drm_crtc *crtc,
441 struct drm_file *file)
Abhijit Kulkarni40e38162016-06-26 22:12:09 -0400442{
443 struct sde_crtc *sde_crtc = to_sde_crtc(crtc);
444 struct drm_device *dev = crtc->dev;
445 struct drm_pending_vblank_event *event;
446 unsigned long flags;
447
448 spin_lock_irqsave(&dev->event_lock, flags);
449 event = sde_crtc->event;
450 if (event) {
451 /* if regular vblank case (!file) or if cancel-flip from
452 * preclose on file that requested flip, then send the
453 * event:
454 */
455 if (!file || (event->base.file_priv == file)) {
456 sde_crtc->event = NULL;
Lloyd Atkinson4f1c8692016-09-14 14:04:25 -0400457 DRM_DEBUG_VBL("%s: send event: %pK\n",
Dhaval Patelec10fad2016-08-22 14:40:48 -0700458 sde_crtc->name, event);
Lloyd Atkinson5d40d312016-09-06 08:34:13 -0400459 SDE_EVT32(DRMID(crtc));
Lloyd Atkinsonac933642016-09-14 11:52:00 -0400460 drm_crtc_send_vblank_event(crtc, event);
Abhijit Kulkarni40e38162016-06-26 22:12:09 -0400461 }
462 }
463 spin_unlock_irqrestore(&dev->event_lock, flags);
464}
465
466static void sde_crtc_vblank_cb(void *data)
467{
468 struct drm_crtc *crtc = (struct drm_crtc *)data;
Alan Kwong07da0982016-11-04 12:57:45 -0400469 struct sde_crtc *sde_crtc = to_sde_crtc(crtc);
470
471 /* keep statistics on vblank callback - with auto reset via debugfs */
472 if (ktime_equal(sde_crtc->vblank_cb_time, ktime_set(0, 0)))
473 sde_crtc->vblank_cb_time = ktime_get();
474 else
475 sde_crtc->vblank_cb_count++;
Abhijit Kulkarni40e38162016-06-26 22:12:09 -0400476
Lloyd Atkinsonac933642016-09-14 11:52:00 -0400477 drm_crtc_handle_vblank(crtc);
Lloyd Atkinson9eabe7a2016-09-14 13:39:15 -0400478 DRM_DEBUG_VBL("crtc%d\n", crtc->base.id);
Lloyd Atkinson5d40d312016-09-06 08:34:13 -0400479 SDE_EVT32_IRQ(DRMID(crtc));
Abhijit Kulkarni40e38162016-06-26 22:12:09 -0400480}
481
Alan Kwong628d19e2016-10-31 13:50:13 -0400482static void sde_crtc_frame_event_work(struct kthread_work *work)
483{
Alan Kwong67a3f792016-11-01 23:16:53 -0400484 struct msm_drm_private *priv;
Alan Kwong628d19e2016-10-31 13:50:13 -0400485 struct sde_crtc_frame_event *fevent;
486 struct drm_crtc *crtc;
487 struct sde_crtc *sde_crtc;
488 struct sde_kms *sde_kms;
489 unsigned long flags;
490
491 if (!work) {
492 SDE_ERROR("invalid work handle\n");
493 return;
494 }
495
496 fevent = container_of(work, struct sde_crtc_frame_event, work);
497 if (!fevent->crtc) {
498 SDE_ERROR("invalid crtc\n");
499 return;
500 }
501
502 crtc = fevent->crtc;
503 sde_crtc = to_sde_crtc(crtc);
504
505 sde_kms = _sde_crtc_get_kms(crtc);
506 if (!sde_kms) {
507 SDE_ERROR("invalid kms handle\n");
508 return;
509 }
Alan Kwong67a3f792016-11-01 23:16:53 -0400510 priv = sde_kms->dev->dev_private;
Alan Kwong628d19e2016-10-31 13:50:13 -0400511
512 SDE_DEBUG("crtc%d event:%u ts:%lld\n", crtc->base.id, fevent->event,
513 ktime_to_ns(fevent->ts));
514
515 if (fevent->event == SDE_ENCODER_FRAME_EVENT_DONE ||
Lloyd Atkinson8c49c582016-11-18 14:23:54 -0500516 (fevent->event & SDE_ENCODER_FRAME_EVENT_ERROR) ||
517 (fevent->event & SDE_ENCODER_FRAME_EVENT_PANEL_DEAD)) {
Alan Kwong628d19e2016-10-31 13:50:13 -0400518
519 if (atomic_read(&sde_crtc->frame_pending) < 1) {
520 /* this should not happen */
521 SDE_ERROR("crtc%d ts:%lld invalid frame_pending:%d\n",
522 crtc->base.id,
523 ktime_to_ns(fevent->ts),
524 atomic_read(&sde_crtc->frame_pending));
525 SDE_EVT32(DRMID(crtc), fevent->event, 0);
526 } else if (atomic_dec_return(&sde_crtc->frame_pending) == 0) {
527 /* release bandwidth and other resources */
528 SDE_DEBUG("crtc%d ts:%lld last pending\n",
529 crtc->base.id,
530 ktime_to_ns(fevent->ts));
531 SDE_EVT32(DRMID(crtc), fevent->event, 1);
Alan Kwong9aa061c2016-11-06 21:17:12 -0500532 sde_core_perf_crtc_release_bw(crtc);
Alan Kwong628d19e2016-10-31 13:50:13 -0400533 } else {
534 SDE_EVT32(DRMID(crtc), fevent->event, 2);
535 }
536 } else {
537 SDE_ERROR("crtc%d ts:%lld unknown event %u\n", crtc->base.id,
538 ktime_to_ns(fevent->ts),
539 fevent->event);
540 SDE_EVT32(DRMID(crtc), fevent->event, 3);
541 }
542
Lloyd Atkinson8c49c582016-11-18 14:23:54 -0500543 if (fevent->event & SDE_ENCODER_FRAME_EVENT_PANEL_DEAD)
544 SDE_ERROR("crtc%d ts:%lld received panel dead event\n",
545 crtc->base.id, ktime_to_ns(fevent->ts));
546
Alan Kwong628d19e2016-10-31 13:50:13 -0400547 spin_lock_irqsave(&sde_crtc->spin_lock, flags);
548 list_add_tail(&fevent->list, &sde_crtc->frame_event_list);
549 spin_unlock_irqrestore(&sde_crtc->spin_lock, flags);
550}
551
552static void sde_crtc_frame_event_cb(void *data, u32 event)
553{
554 struct drm_crtc *crtc = (struct drm_crtc *)data;
555 struct sde_crtc *sde_crtc;
556 struct msm_drm_private *priv;
Alan Kwong628d19e2016-10-31 13:50:13 -0400557 struct sde_crtc_frame_event *fevent;
558 unsigned long flags;
559 int pipe_id;
560
561 if (!crtc || !crtc->dev || !crtc->dev->dev_private) {
562 SDE_ERROR("invalid parameters\n");
563 return;
564 }
565 sde_crtc = to_sde_crtc(crtc);
566 priv = crtc->dev->dev_private;
567 pipe_id = drm_crtc_index(crtc);
568
569 SDE_DEBUG("crtc%d\n", crtc->base.id);
570
571 SDE_EVT32(DRMID(crtc), event);
572
573 spin_lock_irqsave(&sde_crtc->spin_lock, flags);
Lloyd Atkinson78831f82016-12-09 11:24:56 -0500574 fevent = list_first_entry_or_null(&sde_crtc->frame_event_list,
575 struct sde_crtc_frame_event, list);
576 if (fevent)
577 list_del_init(&fevent->list);
Alan Kwong628d19e2016-10-31 13:50:13 -0400578 spin_unlock_irqrestore(&sde_crtc->spin_lock, flags);
579
Lloyd Atkinson78831f82016-12-09 11:24:56 -0500580 if (!fevent) {
Alan Kwong628d19e2016-10-31 13:50:13 -0400581 SDE_ERROR("crtc%d event %d overflow\n",
582 crtc->base.id, event);
583 SDE_EVT32(DRMID(crtc), event);
584 return;
585 }
586
Alan Kwong628d19e2016-10-31 13:50:13 -0400587 fevent->event = event;
588 fevent->crtc = crtc;
589 fevent->ts = ktime_get();
590 kthread_queue_work(&priv->disp_thread[pipe_id].worker, &fevent->work);
591}
592
Clarence Ip0d0e96d2016-10-24 18:13:13 -0400593void sde_crtc_complete_commit(struct drm_crtc *crtc,
594 struct drm_crtc_state *old_state)
Clarence Ip24f80662016-06-13 19:05:32 -0400595{
Clarence Ip0d0e96d2016-10-24 18:13:13 -0400596 struct sde_crtc *sde_crtc;
597 struct sde_crtc_state *cstate;
598 int i;
599
600 if (!crtc || !crtc->state) {
Clarence Ip24f80662016-06-13 19:05:32 -0400601 SDE_ERROR("invalid crtc\n");
602 return;
603 }
604
Clarence Ip0d0e96d2016-10-24 18:13:13 -0400605 sde_crtc = to_sde_crtc(crtc);
606 cstate = to_sde_crtc_state(crtc->state);
Lloyd Atkinson5d40d312016-09-06 08:34:13 -0400607 SDE_EVT32(DRMID(crtc));
Clarence Ip0d0e96d2016-10-24 18:13:13 -0400608
609 /* signal output fence(s) at end of commit */
610 sde_fence_signal(&sde_crtc->output_fence, 0);
611
612 for (i = 0; i < cstate->num_connectors; ++i)
613 sde_connector_complete_commit(cstate->connectors[i]);
Clarence Ip24f80662016-06-13 19:05:32 -0400614}
615
Lloyd Atkinson5d722782016-05-30 14:09:41 -0400616/**
Clarence Ipcae1bb62016-07-07 12:07:13 -0400617 * _sde_crtc_set_input_fence_timeout - update ns version of in fence timeout
618 * @cstate: Pointer to sde crtc state
619 */
620static void _sde_crtc_set_input_fence_timeout(struct sde_crtc_state *cstate)
621{
622 if (!cstate) {
Dhaval Patelec10fad2016-08-22 14:40:48 -0700623 SDE_ERROR("invalid cstate\n");
Clarence Ipcae1bb62016-07-07 12:07:13 -0400624 return;
625 }
626 cstate->input_fence_timeout_ns =
627 sde_crtc_get_property(cstate, CRTC_PROP_INPUT_FENCE_TIMEOUT);
628 cstate->input_fence_timeout_ns *= NSEC_PER_MSEC;
629}
630
631/**
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -0800632 * _sde_crtc_set_dim_layer_v1 - copy dim layer settings from userspace
633 * @cstate: Pointer to sde crtc state
634 * @user_ptr: User ptr for sde_drm_dim_layer_v1 struct
635 */
636static void _sde_crtc_set_dim_layer_v1(struct sde_crtc_state *cstate,
637 void *usr_ptr)
638{
639 struct sde_drm_dim_layer_v1 dim_layer_v1;
640 struct sde_drm_dim_layer_cfg *user_cfg;
641 u32 count, i;
642
643 if (!cstate) {
644 SDE_ERROR("invalid cstate\n");
645 return;
646 }
647
648 if (!usr_ptr) {
649 SDE_DEBUG("dim layer data removed\n");
650 return;
651 }
652
653 if (copy_from_user(&dim_layer_v1, usr_ptr, sizeof(dim_layer_v1))) {
654 SDE_ERROR("failed to copy dim layer data\n");
655 return;
656 }
657
658 count = dim_layer_v1.num_layers;
659 if (!count || (count > SDE_MAX_DIM_LAYERS)) {
660 SDE_ERROR("invalid number of Dim Layers:%d", count);
661 return;
662 }
663
664 /* populate from user space */
665 cstate->num_dim_layers = count;
666 for (i = 0; i < count; i++) {
667 user_cfg = &dim_layer_v1.layer_cfg[i];
668 cstate->dim_layer[i].flags = user_cfg->flags;
669 cstate->dim_layer[i].stage = user_cfg->stage + SDE_STAGE_0;
670
671 cstate->dim_layer[i].rect.x = user_cfg->rect.x1;
672 cstate->dim_layer[i].rect.y = user_cfg->rect.y1;
673 cstate->dim_layer[i].rect.w = user_cfg->rect.x2 -
674 user_cfg->rect.x1 + 1;
675 cstate->dim_layer[i].rect.h = user_cfg->rect.y2 -
676 user_cfg->rect.y1 + 1;
677
678 cstate->dim_layer[i].color_fill = (struct sde_mdss_color) {
679 user_cfg->color_fill.color_0,
680 user_cfg->color_fill.color_1,
681 user_cfg->color_fill.color_2,
682 user_cfg->color_fill.color_3,
683 };
684 }
685}
686
687/**
Clarence Ipcae1bb62016-07-07 12:07:13 -0400688 * _sde_crtc_wait_for_fences - wait for incoming framebuffer sync fences
689 * @crtc: Pointer to CRTC object
690 */
691static void _sde_crtc_wait_for_fences(struct drm_crtc *crtc)
692{
693 struct drm_plane *plane = NULL;
694 uint32_t wait_ms = 1;
Clarence Ip8dedc232016-09-09 16:41:00 -0400695 ktime_t kt_end, kt_wait;
Clarence Ipcae1bb62016-07-07 12:07:13 -0400696
Lloyd Atkinson4f1c8692016-09-14 14:04:25 -0400697 SDE_DEBUG("\n");
Clarence Ipcae1bb62016-07-07 12:07:13 -0400698
699 if (!crtc || !crtc->state) {
Dhaval Patelec10fad2016-08-22 14:40:48 -0700700 SDE_ERROR("invalid crtc/state %pK\n", crtc);
Clarence Ipcae1bb62016-07-07 12:07:13 -0400701 return;
702 }
703
704 /* use monotonic timer to limit total fence wait time */
Clarence Ip8dedc232016-09-09 16:41:00 -0400705 kt_end = ktime_add_ns(ktime_get(),
706 to_sde_crtc_state(crtc->state)->input_fence_timeout_ns);
Clarence Ipcae1bb62016-07-07 12:07:13 -0400707
708 /*
709 * Wait for fences sequentially, as all of them need to be signalled
710 * before we can proceed.
711 *
712 * Limit total wait time to INPUT_FENCE_TIMEOUT, but still call
713 * sde_plane_wait_input_fence with wait_ms == 0 after the timeout so
714 * that each plane can check its fence status and react appropriately
715 * if its fence has timed out.
716 */
717 drm_atomic_crtc_for_each_plane(plane, crtc) {
718 if (wait_ms) {
719 /* determine updated wait time */
Clarence Ip8dedc232016-09-09 16:41:00 -0400720 kt_wait = ktime_sub(kt_end, ktime_get());
721 if (ktime_compare(kt_wait, ktime_set(0, 0)) >= 0)
722 wait_ms = ktime_to_ms(kt_wait);
Clarence Ipcae1bb62016-07-07 12:07:13 -0400723 else
724 wait_ms = 0;
725 }
726 sde_plane_wait_input_fence(plane, wait_ms);
727 }
728}
729
Lloyd Atkinson11f34442016-08-11 11:19:52 -0400730static void _sde_crtc_setup_mixer_for_encoder(
731 struct drm_crtc *crtc,
732 struct drm_encoder *enc)
733{
734 struct sde_crtc *sde_crtc = to_sde_crtc(crtc);
Lloyd Atkinson4f1c8692016-09-14 14:04:25 -0400735 struct sde_kms *sde_kms = _sde_crtc_get_kms(crtc);
Lloyd Atkinson11f34442016-08-11 11:19:52 -0400736 struct sde_rm *rm = &sde_kms->rm;
737 struct sde_crtc_mixer *mixer;
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -0400738 struct sde_hw_ctl *last_valid_ctl = NULL;
Lloyd Atkinson11f34442016-08-11 11:19:52 -0400739 int i;
Gopikrishnaiah Anandane0e5e0c2016-05-25 11:05:33 -0700740 struct sde_rm_hw_iter lm_iter, ctl_iter, dspp_iter;
Lloyd Atkinson11f34442016-08-11 11:19:52 -0400741
742 sde_rm_init_hw_iter(&lm_iter, enc->base.id, SDE_HW_BLK_LM);
743 sde_rm_init_hw_iter(&ctl_iter, enc->base.id, SDE_HW_BLK_CTL);
Gopikrishnaiah Anandane0e5e0c2016-05-25 11:05:33 -0700744 sde_rm_init_hw_iter(&dspp_iter, enc->base.id, SDE_HW_BLK_DSPP);
Lloyd Atkinson11f34442016-08-11 11:19:52 -0400745
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -0400746 /* Set up all the mixers and ctls reserved by this encoder */
Lloyd Atkinson11f34442016-08-11 11:19:52 -0400747 for (i = sde_crtc->num_mixers; i < ARRAY_SIZE(sde_crtc->mixers); i++) {
748 mixer = &sde_crtc->mixers[i];
749
Lloyd Atkinson11f34442016-08-11 11:19:52 -0400750 if (!sde_rm_get_hw(rm, &lm_iter))
751 break;
752 mixer->hw_lm = (struct sde_hw_mixer *)lm_iter.hw;
753
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -0400754 /* CTL may be <= LMs, if <, multiple LMs controlled by 1 CTL */
755 if (!sde_rm_get_hw(rm, &ctl_iter)) {
756 SDE_DEBUG("no ctl assigned to lm %d, using previous\n",
Clarence Ip8e69ad02016-12-09 09:43:57 -0500757 mixer->hw_lm->idx - LM_0);
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -0400758 mixer->hw_ctl = last_valid_ctl;
759 } else {
760 mixer->hw_ctl = (struct sde_hw_ctl *)ctl_iter.hw;
761 last_valid_ctl = mixer->hw_ctl;
762 }
Lloyd Atkinson11f34442016-08-11 11:19:52 -0400763
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -0400764 /* Shouldn't happen, mixers are always >= ctls */
765 if (!mixer->hw_ctl) {
766 SDE_ERROR("no valid ctls found for lm %d\n",
Clarence Ip8e69ad02016-12-09 09:43:57 -0500767 mixer->hw_lm->idx - LM_0);
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -0400768 return;
769 }
770
Gopikrishnaiah Anandane0e5e0c2016-05-25 11:05:33 -0700771 /* Dspp may be null */
772 (void) sde_rm_get_hw(rm, &dspp_iter);
773 mixer->hw_dspp = (struct sde_hw_dspp *)dspp_iter.hw;
774
Lloyd Atkinson11f34442016-08-11 11:19:52 -0400775 mixer->encoder = enc;
776
777 sde_crtc->num_mixers++;
Clarence Ipd9f9fa62016-09-09 13:42:32 -0400778 SDE_DEBUG("setup mixer %d: lm %d\n",
779 i, mixer->hw_lm->idx - LM_0);
780 SDE_DEBUG("setup mixer %d: ctl %d\n",
781 i, mixer->hw_ctl->idx - CTL_0);
Lloyd Atkinson11f34442016-08-11 11:19:52 -0400782 }
783}
784
785static void _sde_crtc_setup_mixers(struct drm_crtc *crtc)
786{
787 struct sde_crtc *sde_crtc = to_sde_crtc(crtc);
788 struct drm_encoder *enc;
789
Lloyd Atkinson11f34442016-08-11 11:19:52 -0400790 sde_crtc->num_mixers = 0;
791 memset(sde_crtc->mixers, 0, sizeof(sde_crtc->mixers));
792
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -0700793 mutex_lock(&sde_crtc->crtc_lock);
Lloyd Atkinson11f34442016-08-11 11:19:52 -0400794 /* Check for mixers on all encoders attached to this crtc */
795 list_for_each_entry(enc, &crtc->dev->mode_config.encoder_list, head) {
796 if (enc->crtc != crtc)
797 continue;
798
799 _sde_crtc_setup_mixer_for_encoder(crtc, enc);
800 }
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -0700801 mutex_unlock(&sde_crtc->crtc_lock);
Lloyd Atkinson11f34442016-08-11 11:19:52 -0400802}
803
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -0400804static void sde_crtc_atomic_begin(struct drm_crtc *crtc,
Clarence Ip0d0e96d2016-10-24 18:13:13 -0400805 struct drm_crtc_state *old_state)
Narendra Muppalla1b0b3352015-09-29 10:16:51 -0700806{
Clarence Ipcae1bb62016-07-07 12:07:13 -0400807 struct sde_crtc *sde_crtc;
808 struct drm_device *dev;
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -0400809 unsigned long flags;
Lloyd Atkinson5d722782016-05-30 14:09:41 -0400810 u32 i;
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -0400811
Clarence Ipcae1bb62016-07-07 12:07:13 -0400812 if (!crtc) {
Dhaval Patelec10fad2016-08-22 14:40:48 -0700813 SDE_ERROR("invalid crtc\n");
Clarence Ipcae1bb62016-07-07 12:07:13 -0400814 return;
815 }
816
Alan Kwong163d2612016-11-03 00:56:56 -0400817 if (!crtc->state->enable) {
818 SDE_DEBUG("crtc%d -> enable %d, skip atomic_begin\n",
819 crtc->base.id, crtc->state->enable);
820 return;
821 }
822
823 SDE_DEBUG("crtc%d\n", crtc->base.id);
824
Clarence Ipcae1bb62016-07-07 12:07:13 -0400825 sde_crtc = to_sde_crtc(crtc);
826 dev = crtc->dev;
827
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -0400828 if (!sde_crtc->num_mixers)
829 _sde_crtc_setup_mixers(crtc);
Lloyd Atkinson11f34442016-08-11 11:19:52 -0400830
Lloyd Atkinson265d2212016-05-30 13:12:01 -0400831 if (sde_crtc->event) {
832 WARN_ON(sde_crtc->event);
833 } else {
834 spin_lock_irqsave(&dev->event_lock, flags);
835 sde_crtc->event = crtc->state->event;
836 spin_unlock_irqrestore(&dev->event_lock, flags);
837 }
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -0400838
Lloyd Atkinson5d722782016-05-30 14:09:41 -0400839 /* Reset flush mask from previous commit */
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -0400840 for (i = 0; i < ARRAY_SIZE(sde_crtc->mixers); i++) {
Lloyd Atkinsone7bcdd22016-08-11 10:53:37 -0400841 struct sde_hw_ctl *ctl = sde_crtc->mixers[i].hw_ctl;
Lloyd Atkinson5d722782016-05-30 14:09:41 -0400842
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -0400843 if (ctl)
844 ctl->ops.clear_pending_flush(ctl);
Lloyd Atkinson5d722782016-05-30 14:09:41 -0400845 }
846
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -0400847 /*
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -0400848 * If no mixers have been allocated in sde_crtc_atomic_check(),
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -0400849 * it means we are trying to flush a CRTC whose state is disabled:
850 * nothing else needs to be done.
851 */
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -0400852 if (unlikely(!sde_crtc->num_mixers))
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -0400853 return;
854
Clarence Ipd9f9fa62016-09-09 13:42:32 -0400855 _sde_crtc_blend_setup(crtc);
Gopikrishnaiah Anandane0e5e0c2016-05-25 11:05:33 -0700856 sde_cp_crtc_apply_properties(crtc);
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -0400857
858 /*
859 * PP_DONE irq is only used by command mode for now.
860 * It is better to request pending before FLUSH and START trigger
861 * to make sure no pp_done irq missed.
862 * This is safe because no pp_done will happen before SW trigger
863 * in command mode.
864 */
Narendra Muppalla1b0b3352015-09-29 10:16:51 -0700865}
866
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -0400867static void sde_crtc_atomic_flush(struct drm_crtc *crtc,
868 struct drm_crtc_state *old_crtc_state)
Narendra Muppalla1b0b3352015-09-29 10:16:51 -0700869{
Dhaval Patel82c8dbc2017-02-18 23:15:10 -0800870 struct drm_encoder *encoder;
Clarence Ipcae1bb62016-07-07 12:07:13 -0400871 struct sde_crtc *sde_crtc;
872 struct drm_device *dev;
Lloyd Atkinson265d2212016-05-30 13:12:01 -0400873 struct drm_plane *plane;
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -0400874 unsigned long flags;
Dhaval Patel82c8dbc2017-02-18 23:15:10 -0800875 struct sde_crtc_state *cstate;
Narendra Muppalla1b0b3352015-09-29 10:16:51 -0700876
Clarence Ipcae1bb62016-07-07 12:07:13 -0400877 if (!crtc) {
Dhaval Patelec10fad2016-08-22 14:40:48 -0700878 SDE_ERROR("invalid crtc\n");
Clarence Ipcae1bb62016-07-07 12:07:13 -0400879 return;
880 }
881
Alan Kwong163d2612016-11-03 00:56:56 -0400882 if (!crtc->state->enable) {
883 SDE_DEBUG("crtc%d -> enable %d, skip atomic_flush\n",
884 crtc->base.id, crtc->state->enable);
885 return;
886 }
887
888 SDE_DEBUG("crtc%d\n", crtc->base.id);
Clarence Ipcae1bb62016-07-07 12:07:13 -0400889
890 sde_crtc = to_sde_crtc(crtc);
Dhaval Patel82c8dbc2017-02-18 23:15:10 -0800891 cstate = to_sde_crtc_state(crtc->state);
Clarence Ipcae1bb62016-07-07 12:07:13 -0400892 dev = crtc->dev;
893
Lloyd Atkinson265d2212016-05-30 13:12:01 -0400894 if (sde_crtc->event) {
Dhaval Patelec10fad2016-08-22 14:40:48 -0700895 SDE_DEBUG("already received sde_crtc->event\n");
Lloyd Atkinson265d2212016-05-30 13:12:01 -0400896 } else {
Lloyd Atkinson265d2212016-05-30 13:12:01 -0400897 spin_lock_irqsave(&dev->event_lock, flags);
898 sde_crtc->event = crtc->state->event;
899 spin_unlock_irqrestore(&dev->event_lock, flags);
900 }
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -0400901
902 /*
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -0400903 * If no mixers has been allocated in sde_crtc_atomic_check(),
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -0400904 * it means we are trying to flush a CRTC whose state is disabled:
905 * nothing else needs to be done.
906 */
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -0400907 if (unlikely(!sde_crtc->num_mixers))
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -0400908 return;
909
Clarence Ipcae1bb62016-07-07 12:07:13 -0400910 /* wait for acquire fences before anything else is done */
911 _sde_crtc_wait_for_fences(crtc);
912
Dhaval Patel82c8dbc2017-02-18 23:15:10 -0800913 if (!cstate->rsc_update) {
914 drm_for_each_encoder(encoder, dev) {
915 if (encoder->crtc != crtc)
916 continue;
917
918 cstate->rsc_client =
919 sde_encoder_update_rsc_client(encoder, true);
920 }
921 cstate->rsc_update = true;
922 }
923
Alan Kwong9aa061c2016-11-06 21:17:12 -0500924 /* update performance setting before crtc kickoff */
925 sde_core_perf_crtc_update(crtc, 1, false);
926
Clarence Ipcae1bb62016-07-07 12:07:13 -0400927 /*
928 * Final plane updates: Give each plane a chance to complete all
929 * required writes/flushing before crtc's "flush
930 * everything" call below.
931 */
932 drm_atomic_crtc_for_each_plane(plane, crtc)
933 sde_plane_flush(plane);
934
Lloyd Atkinson5d722782016-05-30 14:09:41 -0400935 /* Kickoff will be scheduled by outer layer */
Narendra Muppalla1b0b3352015-09-29 10:16:51 -0700936}
937
Clarence Ip7a753bb2016-07-07 11:47:44 -0400938/**
939 * sde_crtc_destroy_state - state destroy hook
940 * @crtc: drm CRTC
941 * @state: CRTC state object to release
942 */
943static void sde_crtc_destroy_state(struct drm_crtc *crtc,
944 struct drm_crtc_state *state)
Narendra Muppalla1b0b3352015-09-29 10:16:51 -0700945{
Clarence Ip7a753bb2016-07-07 11:47:44 -0400946 struct sde_crtc *sde_crtc;
947 struct sde_crtc_state *cstate;
948
949 if (!crtc || !state) {
Dhaval Patelec10fad2016-08-22 14:40:48 -0700950 SDE_ERROR("invalid argument(s)\n");
Clarence Ip7a753bb2016-07-07 11:47:44 -0400951 return;
952 }
953
954 sde_crtc = to_sde_crtc(crtc);
955 cstate = to_sde_crtc_state(state);
956
Alan Kwong163d2612016-11-03 00:56:56 -0400957 SDE_DEBUG("crtc%d\n", crtc->base.id);
Clarence Ip7a753bb2016-07-07 11:47:44 -0400958
Dhaval Patel04c7e8e2016-09-26 20:14:31 -0700959 __drm_atomic_helper_crtc_destroy_state(state);
Clarence Ip7a753bb2016-07-07 11:47:44 -0400960
961 /* destroy value helper */
962 msm_property_destroy_state(&sde_crtc->property_info, cstate,
963 cstate->property_values, cstate->property_blobs);
964}
965
Lloyd Atkinson5d722782016-05-30 14:09:41 -0400966void sde_crtc_commit_kickoff(struct drm_crtc *crtc)
967{
Lloyd Atkinsone7bcdd22016-08-11 10:53:37 -0400968 struct drm_encoder *encoder;
969 struct drm_device *dev;
Alan Kwong628d19e2016-10-31 13:50:13 -0400970 struct sde_crtc *sde_crtc;
Alan Kwong67a3f792016-11-01 23:16:53 -0400971 struct msm_drm_private *priv;
972 struct sde_kms *sde_kms;
Lloyd Atkinson5d722782016-05-30 14:09:41 -0400973
974 if (!crtc) {
Dhaval Patelec10fad2016-08-22 14:40:48 -0700975 SDE_ERROR("invalid argument\n");
Lloyd Atkinson5d722782016-05-30 14:09:41 -0400976 return;
977 }
Lloyd Atkinsone7bcdd22016-08-11 10:53:37 -0400978 dev = crtc->dev;
Alan Kwong628d19e2016-10-31 13:50:13 -0400979 sde_crtc = to_sde_crtc(crtc);
Alan Kwong67a3f792016-11-01 23:16:53 -0400980 sde_kms = _sde_crtc_get_kms(crtc);
981 priv = sde_kms->dev->dev_private;
Lloyd Atkinson5d722782016-05-30 14:09:41 -0400982
Lloyd Atkinsone7bcdd22016-08-11 10:53:37 -0400983 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
984 if (encoder->crtc != crtc)
985 continue;
986
987 /*
988 * Encoder will flush/start now, unless it has a tx pending.
989 * If so, it may delay and flush at an irq event (e.g. ppdone)
990 */
Alan Kwong628d19e2016-10-31 13:50:13 -0400991 sde_encoder_prepare_for_kickoff(encoder);
992 }
993
994 if (atomic_read(&sde_crtc->frame_pending) > 2) {
995 /* framework allows only 1 outstanding + current */
996 SDE_ERROR("crtc%d invalid frame pending\n",
997 crtc->base.id);
998 SDE_EVT32(DRMID(crtc), 0);
999 return;
1000 } else if (atomic_inc_return(&sde_crtc->frame_pending) == 1) {
1001 /* acquire bandwidth and other resources */
1002 SDE_DEBUG("crtc%d first commit\n", crtc->base.id);
1003 SDE_EVT32(DRMID(crtc), 1);
1004 } else {
1005 SDE_DEBUG("crtc%d commit\n", crtc->base.id);
1006 SDE_EVT32(DRMID(crtc), 2);
1007 }
1008
1009 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
1010 if (encoder->crtc != crtc)
1011 continue;
1012
1013 sde_encoder_kickoff(encoder);
Lloyd Atkinsone7bcdd22016-08-11 10:53:37 -04001014 }
Lloyd Atkinson5d722782016-05-30 14:09:41 -04001015}
1016
Clarence Ip7a753bb2016-07-07 11:47:44 -04001017/**
1018 * sde_crtc_duplicate_state - state duplicate hook
1019 * @crtc: Pointer to drm crtc structure
1020 * @Returns: Pointer to new drm_crtc_state structure
1021 */
1022static struct drm_crtc_state *sde_crtc_duplicate_state(struct drm_crtc *crtc)
1023{
1024 struct sde_crtc *sde_crtc;
1025 struct sde_crtc_state *cstate, *old_cstate;
1026
1027 if (!crtc || !crtc->state) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07001028 SDE_ERROR("invalid argument(s)\n");
Clarence Ip7a753bb2016-07-07 11:47:44 -04001029 return NULL;
1030 }
1031
1032 sde_crtc = to_sde_crtc(crtc);
1033 old_cstate = to_sde_crtc_state(crtc->state);
1034 cstate = msm_property_alloc_state(&sde_crtc->property_info);
1035 if (!cstate) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07001036 SDE_ERROR("failed to allocate state\n");
Clarence Ip7a753bb2016-07-07 11:47:44 -04001037 return NULL;
1038 }
1039
1040 /* duplicate value helper */
1041 msm_property_duplicate_state(&sde_crtc->property_info,
1042 old_cstate, cstate,
1043 cstate->property_values, cstate->property_blobs);
1044
1045 /* duplicate base helper */
1046 __drm_atomic_helper_crtc_duplicate_state(crtc, &cstate->base);
1047
1048 return &cstate->base;
1049}
1050
1051/**
1052 * sde_crtc_reset - reset hook for CRTCs
1053 * Resets the atomic state for @crtc by freeing the state pointer (which might
1054 * be NULL, e.g. at driver load time) and allocating a new empty state object.
1055 * @crtc: Pointer to drm crtc structure
1056 */
1057static void sde_crtc_reset(struct drm_crtc *crtc)
1058{
1059 struct sde_crtc *sde_crtc;
1060 struct sde_crtc_state *cstate;
1061
1062 if (!crtc) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07001063 SDE_ERROR("invalid crtc\n");
Clarence Ip7a753bb2016-07-07 11:47:44 -04001064 return;
1065 }
1066
1067 /* remove previous state, if present */
1068 if (crtc->state) {
1069 sde_crtc_destroy_state(crtc, crtc->state);
1070 crtc->state = 0;
1071 }
1072
1073 sde_crtc = to_sde_crtc(crtc);
1074 cstate = msm_property_alloc_state(&sde_crtc->property_info);
1075 if (!cstate) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07001076 SDE_ERROR("failed to allocate state\n");
Clarence Ip7a753bb2016-07-07 11:47:44 -04001077 return;
1078 }
1079
1080 /* reset value helper */
1081 msm_property_reset_state(&sde_crtc->property_info, cstate,
1082 cstate->property_values, cstate->property_blobs);
1083
Clarence Ipcae1bb62016-07-07 12:07:13 -04001084 _sde_crtc_set_input_fence_timeout(cstate);
1085
Clarence Ip7a753bb2016-07-07 11:47:44 -04001086 cstate->base.crtc = crtc;
1087 crtc->state = &cstate->base;
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07001088}
1089
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001090static void sde_crtc_disable(struct drm_crtc *crtc)
1091{
Alan Kwong67a3f792016-11-01 23:16:53 -04001092 struct msm_drm_private *priv;
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04001093 struct sde_crtc *sde_crtc;
Dhaval Patel82c8dbc2017-02-18 23:15:10 -08001094 struct sde_crtc_state *cstate;
Alan Kwong07da0982016-11-04 12:57:45 -04001095 struct drm_encoder *encoder;
Alan Kwong67a3f792016-11-01 23:16:53 -04001096 struct sde_kms *sde_kms;
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04001097
1098 if (!crtc) {
Lloyd Atkinson4f1c8692016-09-14 14:04:25 -04001099 SDE_ERROR("invalid crtc\n");
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04001100 return;
1101 }
1102 sde_crtc = to_sde_crtc(crtc);
Dhaval Patel82c8dbc2017-02-18 23:15:10 -08001103 cstate = to_sde_crtc_state(crtc->state);
Alan Kwong67a3f792016-11-01 23:16:53 -04001104 sde_kms = _sde_crtc_get_kms(crtc);
1105 priv = sde_kms->dev->dev_private;
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04001106
Alan Kwong163d2612016-11-03 00:56:56 -04001107 SDE_DEBUG("crtc%d\n", crtc->base.id);
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04001108
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07001109 mutex_lock(&sde_crtc->crtc_lock);
Alan Kwong628d19e2016-10-31 13:50:13 -04001110 SDE_EVT32(DRMID(crtc));
1111
Alan Kwong07da0982016-11-04 12:57:45 -04001112 if (atomic_read(&sde_crtc->vblank_refcount)) {
Alan Kwong628d19e2016-10-31 13:50:13 -04001113 SDE_ERROR("crtc%d invalid vblank refcount\n",
1114 crtc->base.id);
1115 SDE_EVT32(DRMID(crtc));
Alan Kwong07da0982016-11-04 12:57:45 -04001116 drm_for_each_encoder(encoder, crtc->dev) {
1117 if (encoder->crtc != crtc)
1118 continue;
1119 sde_encoder_register_vblank_callback(encoder, NULL,
1120 NULL);
1121 }
1122 atomic_set(&sde_crtc->vblank_refcount, 0);
1123 }
1124
Alan Kwong628d19e2016-10-31 13:50:13 -04001125 if (atomic_read(&sde_crtc->frame_pending)) {
1126 /* release bandwidth and other resources */
1127 SDE_ERROR("crtc%d invalid frame pending\n",
1128 crtc->base.id);
1129 SDE_EVT32(DRMID(crtc));
Alan Kwong9aa061c2016-11-06 21:17:12 -05001130 sde_core_perf_crtc_release_bw(crtc);
Alan Kwong628d19e2016-10-31 13:50:13 -04001131 atomic_set(&sde_crtc->frame_pending, 0);
1132 }
1133
Alan Kwong9aa061c2016-11-06 21:17:12 -05001134 sde_core_perf_crtc_update(crtc, 0, true);
1135
Alan Kwong628d19e2016-10-31 13:50:13 -04001136 drm_for_each_encoder(encoder, crtc->dev) {
1137 if (encoder->crtc != crtc)
1138 continue;
1139 sde_encoder_register_frame_event_callback(encoder, NULL, NULL);
Dhaval Patel82c8dbc2017-02-18 23:15:10 -08001140 sde_encoder_update_rsc_client(encoder, false);
1141 cstate->rsc_client = NULL;
1142 cstate->rsc_update = false;
Alan Kwong628d19e2016-10-31 13:50:13 -04001143 }
1144
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04001145 memset(sde_crtc->mixers, 0, sizeof(sde_crtc->mixers));
1146 sde_crtc->num_mixers = 0;
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07001147 mutex_unlock(&sde_crtc->crtc_lock);
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001148}
1149
1150static void sde_crtc_enable(struct drm_crtc *crtc)
1151{
Clarence Ipcae1bb62016-07-07 12:07:13 -04001152 struct sde_crtc *sde_crtc;
1153 struct sde_crtc_mixer *mixer;
Lloyd Atkinsonaf7952d2016-06-26 22:41:26 -04001154 struct sde_hw_mixer *lm;
Lloyd Atkinsonaf7952d2016-06-26 22:41:26 -04001155 struct drm_display_mode *mode;
1156 struct sde_hw_mixer_cfg cfg;
Alan Kwong628d19e2016-10-31 13:50:13 -04001157 struct drm_encoder *encoder;
Lloyd Atkinsonaf7952d2016-06-26 22:41:26 -04001158 int i;
Lloyd Atkinsonaf7952d2016-06-26 22:41:26 -04001159
Clarence Ipcae1bb62016-07-07 12:07:13 -04001160 if (!crtc) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07001161 SDE_ERROR("invalid crtc\n");
Clarence Ipcae1bb62016-07-07 12:07:13 -04001162 return;
1163 }
1164
Alan Kwong163d2612016-11-03 00:56:56 -04001165 SDE_DEBUG("crtc%d\n", crtc->base.id);
Alan Kwong628d19e2016-10-31 13:50:13 -04001166 SDE_EVT32(DRMID(crtc));
Lloyd Atkinsonaf7952d2016-06-26 22:41:26 -04001167
Clarence Ipcae1bb62016-07-07 12:07:13 -04001168 sde_crtc = to_sde_crtc(crtc);
Lloyd Atkinsone7bcdd22016-08-11 10:53:37 -04001169 mixer = sde_crtc->mixers;
Clarence Ipcae1bb62016-07-07 12:07:13 -04001170
Lloyd Atkinsonaf7952d2016-06-26 22:41:26 -04001171 if (WARN_ON(!crtc->state))
1172 return;
1173
1174 mode = &crtc->state->adjusted_mode;
1175
1176 drm_mode_debug_printmodeline(mode);
1177
Alan Kwong628d19e2016-10-31 13:50:13 -04001178 drm_for_each_encoder(encoder, crtc->dev) {
1179 if (encoder->crtc != crtc)
1180 continue;
1181 sde_encoder_register_frame_event_callback(encoder,
1182 sde_crtc_frame_event_cb, (void *)crtc);
1183 }
1184
Lloyd Atkinsonaf7952d2016-06-26 22:41:26 -04001185 for (i = 0; i < sde_crtc->num_mixers; i++) {
1186 lm = mixer[i].hw_lm;
Dhaval Patelec10fad2016-08-22 14:40:48 -07001187 cfg.out_width = sde_crtc_mixer_width(sde_crtc, mode);
Lloyd Atkinsonaf7952d2016-06-26 22:41:26 -04001188 cfg.out_height = mode->vdisplay;
1189 cfg.right_mixer = (i == 0) ? false : true;
1190 cfg.flags = 0;
1191 lm->ops.setup_mixer_out(lm, &cfg);
1192 }
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001193}
1194
1195struct plane_state {
Dhaval Patelec10fad2016-08-22 14:40:48 -07001196 struct sde_plane_state *sde_pstate;
Dhaval Patel04c7e8e2016-09-26 20:14:31 -07001197 const struct drm_plane_state *drm_pstate;
Clarence Ipc47a0692016-10-11 10:54:17 -04001198 int stage;
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08001199 u32 pipe_id;
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001200};
1201
Clarence Ipc47a0692016-10-11 10:54:17 -04001202static int pstate_cmp(const void *a, const void *b)
1203{
1204 struct plane_state *pa = (struct plane_state *)a;
1205 struct plane_state *pb = (struct plane_state *)b;
1206 int rc = 0;
1207 int pa_zpos, pb_zpos;
1208
1209 pa_zpos = sde_plane_get_property(pa->sde_pstate, PLANE_PROP_ZPOS);
1210 pb_zpos = sde_plane_get_property(pb->sde_pstate, PLANE_PROP_ZPOS);
1211
1212 if (pa_zpos != pb_zpos)
1213 rc = pa_zpos - pb_zpos;
1214 else
1215 rc = pa->drm_pstate->crtc_x - pb->drm_pstate->crtc_x;
1216
1217 return rc;
1218}
1219
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001220static int sde_crtc_atomic_check(struct drm_crtc *crtc,
1221 struct drm_crtc_state *state)
1222{
Clarence Ipcae1bb62016-07-07 12:07:13 -04001223 struct sde_crtc *sde_crtc;
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08001224 struct plane_state pstates[SDE_STAGE_MAX * 4];
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08001225 struct sde_crtc_state *cstate;
Dhaval Patelec10fad2016-08-22 14:40:48 -07001226
Dhaval Patel04c7e8e2016-09-26 20:14:31 -07001227 const struct drm_plane_state *pstate;
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001228 struct drm_plane *plane;
Dhaval Patelec10fad2016-08-22 14:40:48 -07001229 struct drm_display_mode *mode;
1230
1231 int cnt = 0, rc = 0, mixer_width, i, z_pos;
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08001232
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08001233 struct sde_multirect_plane_states multirect_plane[SDE_STAGE_MAX * 2];
1234 int multirect_count = 0;
1235 const struct drm_plane_state *pipe_staged[SSPP_MAX];
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04001236 int left_zpos_cnt = 0, right_zpos_cnt = 0;
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001237
Clarence Ipcae1bb62016-07-07 12:07:13 -04001238 if (!crtc) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07001239 SDE_ERROR("invalid crtc\n");
Clarence Ipcae1bb62016-07-07 12:07:13 -04001240 return -EINVAL;
1241 }
1242
Lloyd Atkinson5217336c2016-09-15 18:21:18 -04001243 if (!state->enable || !state->active) {
1244 SDE_DEBUG("crtc%d -> enable %d, active %d, skip atomic_check\n",
1245 crtc->base.id, state->enable, state->active);
1246 return 0;
1247 }
1248
Clarence Ipcae1bb62016-07-07 12:07:13 -04001249 sde_crtc = to_sde_crtc(crtc);
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08001250 cstate = to_sde_crtc_state(state);
Dhaval Patelec10fad2016-08-22 14:40:48 -07001251 mode = &state->adjusted_mode;
1252 SDE_DEBUG("%s: check", sde_crtc->name);
Clarence Ipcae1bb62016-07-07 12:07:13 -04001253
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08001254 memset(pipe_staged, 0, sizeof(pipe_staged));
1255
Dhaval Patelec10fad2016-08-22 14:40:48 -07001256 mixer_width = sde_crtc_mixer_width(sde_crtc, mode);
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001257
Dhaval Patelec10fad2016-08-22 14:40:48 -07001258 /* get plane state for all drm planes associated with crtc state */
Dhaval Patel04c7e8e2016-09-26 20:14:31 -07001259 drm_atomic_crtc_state_for_each_plane_state(plane, pstate, state) {
Clarence Ipc47a0692016-10-11 10:54:17 -04001260 if (IS_ERR_OR_NULL(pstate)) {
1261 rc = PTR_ERR(pstate);
1262 SDE_ERROR("%s: failed to get plane%d state, %d\n",
1263 sde_crtc->name, plane->base.id, rc);
Alan Kwong85767282016-10-03 18:03:37 -04001264 goto end;
1265 }
Clarence Ipc47a0692016-10-11 10:54:17 -04001266 if (cnt >= ARRAY_SIZE(pstates))
1267 continue;
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001268
Dhaval Patelec10fad2016-08-22 14:40:48 -07001269 pstates[cnt].sde_pstate = to_sde_plane_state(pstate);
1270 pstates[cnt].drm_pstate = pstate;
Clarence Ipc47a0692016-10-11 10:54:17 -04001271 pstates[cnt].stage = sde_plane_get_property(
1272 pstates[cnt].sde_pstate, PLANE_PROP_ZPOS);
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08001273 pstates[cnt].pipe_id = sde_plane_pipe(plane);
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08001274
1275 /* check dim layer stage with every plane */
1276 for (i = 0; i < cstate->num_dim_layers; i++) {
1277 if (pstates[cnt].stage == cstate->dim_layer[i].stage) {
1278 SDE_ERROR("plane%d/dimlayer in same stage:%d\n",
1279 plane->base.id,
1280 cstate->dim_layer[i].stage);
1281 rc = -EINVAL;
1282 goto end;
1283 }
1284 }
1285
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08001286 if (pipe_staged[pstates[cnt].pipe_id]) {
1287 multirect_plane[multirect_count].r0 =
1288 pipe_staged[pstates[cnt].pipe_id];
1289 multirect_plane[multirect_count].r1 = pstate;
1290 multirect_count++;
1291
1292 pipe_staged[pstates[cnt].pipe_id] = NULL;
1293 } else {
1294 pipe_staged[pstates[cnt].pipe_id] = pstate;
1295 }
1296
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001297 cnt++;
Dhaval Patelec10fad2016-08-22 14:40:48 -07001298
1299 if (CHECK_LAYER_BOUNDS(pstate->crtc_y, pstate->crtc_h,
1300 mode->vdisplay) ||
1301 CHECK_LAYER_BOUNDS(pstate->crtc_x, pstate->crtc_w,
1302 mode->hdisplay)) {
1303 SDE_ERROR("invalid vertical/horizontal destination\n");
1304 SDE_ERROR("y:%d h:%d vdisp:%d x:%d w:%d hdisp:%d\n",
1305 pstate->crtc_y, pstate->crtc_h, mode->vdisplay,
1306 pstate->crtc_x, pstate->crtc_w, mode->hdisplay);
1307 rc = -E2BIG;
1308 goto end;
1309 }
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001310 }
1311
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08001312 for (i = 1; i < SSPP_MAX; i++) {
1313 if (pipe_staged[i] &&
1314 is_sde_plane_virtual(pipe_staged[i]->plane)) {
1315 SDE_ERROR("invalid use of virtual plane: %d\n",
1316 pipe_staged[i]->plane->base.id);
1317 goto end;
1318 }
1319 }
1320
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08001321 /* Check dim layer rect bounds and stage */
1322 for (i = 0; i < cstate->num_dim_layers; i++) {
1323 if ((CHECK_LAYER_BOUNDS(cstate->dim_layer[i].rect.y,
1324 cstate->dim_layer[i].rect.h, mode->vdisplay)) ||
1325 (CHECK_LAYER_BOUNDS(cstate->dim_layer[i].rect.x,
1326 cstate->dim_layer[i].rect.w, mode->hdisplay)) ||
1327 (cstate->dim_layer[i].stage >= SDE_STAGE_MAX) ||
1328 (!cstate->dim_layer[i].rect.w) ||
1329 (!cstate->dim_layer[i].rect.h)) {
1330 SDE_ERROR("invalid dim_layer:{%d,%d,%d,%d}, stage:%d\n",
1331 cstate->dim_layer[i].rect.x,
1332 cstate->dim_layer[i].rect.y,
1333 cstate->dim_layer[i].rect.w,
1334 cstate->dim_layer[i].rect.h,
1335 cstate->dim_layer[i].stage);
1336 SDE_ERROR("display: %dx%d\n", mode->hdisplay,
1337 mode->vdisplay);
1338 rc = -E2BIG;
1339 goto end;
1340 }
1341 }
1342
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04001343 /* assign mixer stages based on sorted zpos property */
1344 sort(pstates, cnt, sizeof(pstates[0]), pstate_cmp, NULL);
1345
Clarence Ipc47a0692016-10-11 10:54:17 -04001346 if (!sde_is_custom_client()) {
1347 int stage_old = pstates[0].stage;
Dhaval Patelec10fad2016-08-22 14:40:48 -07001348
Clarence Ipc47a0692016-10-11 10:54:17 -04001349 z_pos = 0;
1350 for (i = 0; i < cnt; i++) {
1351 if (stage_old != pstates[i].stage)
1352 ++z_pos;
1353 stage_old = pstates[i].stage;
1354 pstates[i].stage = z_pos;
1355 }
1356 }
1357
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04001358 z_pos = -1;
Clarence Ipc47a0692016-10-11 10:54:17 -04001359 for (i = 0; i < cnt; i++) {
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04001360 /* reset counts at every new blend stage */
1361 if (pstates[i].stage != z_pos) {
1362 left_zpos_cnt = 0;
1363 right_zpos_cnt = 0;
1364 z_pos = pstates[i].stage;
1365 }
Clarence Ipc47a0692016-10-11 10:54:17 -04001366
1367 /* verify z_pos setting before using it */
Clarence Ip649989a2016-10-21 14:28:34 -04001368 if (z_pos >= SDE_STAGE_MAX - SDE_STAGE_0) {
Clarence Ipc47a0692016-10-11 10:54:17 -04001369 SDE_ERROR("> %d plane stages assigned\n",
1370 SDE_STAGE_MAX - SDE_STAGE_0);
1371 rc = -EINVAL;
1372 goto end;
1373 } else if (pstates[i].drm_pstate->crtc_x < mixer_width) {
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04001374 if (left_zpos_cnt == 2) {
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08001375 SDE_ERROR("> 2 planes @ stage %d on left\n",
Dhaval Patelec10fad2016-08-22 14:40:48 -07001376 z_pos);
1377 rc = -EINVAL;
1378 goto end;
1379 }
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04001380 left_zpos_cnt++;
1381
Dhaval Patelec10fad2016-08-22 14:40:48 -07001382 } else {
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04001383 if (right_zpos_cnt == 2) {
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08001384 SDE_ERROR("> 2 planes @ stage %d on right\n",
Dhaval Patelec10fad2016-08-22 14:40:48 -07001385 z_pos);
1386 rc = -EINVAL;
1387 goto end;
1388 }
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04001389 right_zpos_cnt++;
Dhaval Patelec10fad2016-08-22 14:40:48 -07001390 }
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04001391
Clarence Ipc47a0692016-10-11 10:54:17 -04001392 pstates[i].sde_pstate->stage = z_pos + SDE_STAGE_0;
Dhaval Patelec10fad2016-08-22 14:40:48 -07001393 SDE_DEBUG("%s: zpos %d", sde_crtc->name, z_pos);
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001394 }
1395
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08001396 for (i = 0; i < multirect_count; i++) {
1397 if (sde_plane_validate_multirect_v2(&multirect_plane[i])) {
1398 SDE_ERROR(
1399 "multirect validation failed for planes (%d - %d)\n",
1400 multirect_plane[i].r0->plane->base.id,
1401 multirect_plane[i].r1->plane->base.id);
1402 rc = -EINVAL;
Alan Kwong9aa061c2016-11-06 21:17:12 -05001403 goto end;
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08001404 }
1405 }
1406
Alan Kwong9aa061c2016-11-06 21:17:12 -05001407 rc = sde_core_perf_crtc_check(crtc, state);
1408 if (rc) {
1409 SDE_ERROR("crtc%d failed performance check %d\n",
1410 crtc->base.id, rc);
1411 goto end;
1412 }
1413
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04001414 /*
1415 * enforce pipe priority restrictions
1416 * use pstates sorted by stage to check planes on same stage
1417 * we assume that all pipes are in source split so its valid to compare
1418 * without taking into account left/right mixer placement
1419 */
1420 for (i = 1; i < cnt; i++) {
1421 struct plane_state *prv_pstate, *cur_pstate;
1422 int32_t prv_x, cur_x, prv_id, cur_id;
1423
1424 prv_pstate = &pstates[i - 1];
1425 cur_pstate = &pstates[i];
1426 if (prv_pstate->stage != cur_pstate->stage)
1427 continue;
1428
1429 prv_x = prv_pstate->drm_pstate->crtc_x;
1430 cur_x = cur_pstate->drm_pstate->crtc_x;
1431 prv_id = prv_pstate->sde_pstate->base.plane->base.id;
1432 cur_id = cur_pstate->sde_pstate->base.plane->base.id;
1433
1434 /*
1435 * Planes are enumerated in pipe-priority order such that planes
1436 * with lower drm_id must be left-most in a shared blend-stage
1437 * when using source split.
1438 */
1439 if (cur_x > prv_x && cur_id < prv_id) {
1440 SDE_ERROR(
1441 "shared z_pos %d lower id plane%d @ x%d should be left of plane%d @ x %d\n",
1442 cur_pstate->stage, cur_id, cur_x,
1443 prv_id, prv_x);
1444 rc = -EINVAL;
1445 goto end;
1446 } else if (cur_x < prv_x && cur_id > prv_id) {
1447 SDE_ERROR(
1448 "shared z_pos %d lower id plane%d @ x%d should be left of plane%d @ x %d\n",
1449 cur_pstate->stage, prv_id, prv_x,
1450 cur_id, cur_x);
1451 rc = -EINVAL;
1452 goto end;
1453 }
1454 }
1455
1456
Dhaval Patelec10fad2016-08-22 14:40:48 -07001457end:
1458 return rc;
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001459}
1460
Abhijit Kulkarni7acb3262016-07-05 15:27:25 -04001461int sde_crtc_vblank(struct drm_crtc *crtc, bool en)
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07001462{
Alan Kwong07da0982016-11-04 12:57:45 -04001463 struct sde_crtc *sde_crtc = to_sde_crtc(crtc);
Lloyd Atkinsone7bcdd22016-08-11 10:53:37 -04001464 struct drm_encoder *encoder;
1465 struct drm_device *dev = crtc->dev;
Abhijit Kulkarni7acb3262016-07-05 15:27:25 -04001466
Alan Kwong07da0982016-11-04 12:57:45 -04001467 if (en && atomic_inc_return(&sde_crtc->vblank_refcount) == 1) {
1468 SDE_DEBUG("crtc%d vblank enable\n", crtc->base.id);
1469 } else if (!en && atomic_read(&sde_crtc->vblank_refcount) < 1) {
1470 SDE_ERROR("crtc%d invalid vblank disable\n", crtc->base.id);
1471 return -EINVAL;
1472 } else if (!en && atomic_dec_return(&sde_crtc->vblank_refcount) == 0) {
1473 SDE_DEBUG("crtc%d vblank disable\n", crtc->base.id);
1474 } else {
1475 SDE_DEBUG("crtc%d vblank %s refcount:%d\n",
1476 crtc->base.id,
1477 en ? "enable" : "disable",
1478 atomic_read(&sde_crtc->vblank_refcount));
1479 return 0;
1480 }
Lloyd Atkinsone5c2c0b2016-07-05 12:23:29 -04001481
Lloyd Atkinsone7bcdd22016-08-11 10:53:37 -04001482 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
1483 if (encoder->crtc != crtc)
1484 continue;
Alan Kwongcf42ee02016-10-04 09:19:17 -04001485
Lloyd Atkinson5d40d312016-09-06 08:34:13 -04001486 SDE_EVT32(DRMID(crtc), en);
Lloyd Atkinson5d722782016-05-30 14:09:41 -04001487
Lloyd Atkinsone7bcdd22016-08-11 10:53:37 -04001488 if (en)
1489 sde_encoder_register_vblank_callback(encoder,
1490 sde_crtc_vblank_cb, (void *)crtc);
1491 else
1492 sde_encoder_register_vblank_callback(encoder, NULL,
1493 NULL);
1494 }
Abhijit Kulkarni7acb3262016-07-05 15:27:25 -04001495
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07001496 return 0;
1497}
1498
Lloyd Atkinson5217336c2016-09-15 18:21:18 -04001499void sde_crtc_cancel_pending_flip(struct drm_crtc *crtc, struct drm_file *file)
1500{
1501 struct sde_crtc *sde_crtc = to_sde_crtc(crtc);
1502
Alan Kwong163d2612016-11-03 00:56:56 -04001503 SDE_DEBUG("%s: cancel: %p\n", sde_crtc->name, file);
Lloyd Atkinson4f1c8692016-09-14 14:04:25 -04001504 _sde_crtc_complete_flip(crtc, file);
Lloyd Atkinson5217336c2016-09-15 18:21:18 -04001505}
1506
Clarence Ip7a753bb2016-07-07 11:47:44 -04001507/**
1508 * sde_crtc_install_properties - install all drm properties for crtc
1509 * @crtc: Pointer to drm crtc structure
1510 */
Dhaval Patele4a5dda2016-10-13 19:29:30 -07001511static void sde_crtc_install_properties(struct drm_crtc *crtc,
1512 struct sde_mdss_cfg *catalog)
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07001513{
Clarence Ip7a753bb2016-07-07 11:47:44 -04001514 struct sde_crtc *sde_crtc;
1515 struct drm_device *dev;
Dhaval Patele4a5dda2016-10-13 19:29:30 -07001516 struct sde_kms_info *info;
Alan Kwong9aa061c2016-11-06 21:17:12 -05001517 struct sde_kms *sde_kms;
Clarence Ip7a753bb2016-07-07 11:47:44 -04001518
Lloyd Atkinson4f1c8692016-09-14 14:04:25 -04001519 SDE_DEBUG("\n");
Clarence Ip7a753bb2016-07-07 11:47:44 -04001520
Dhaval Patele4a5dda2016-10-13 19:29:30 -07001521 if (!crtc || !catalog) {
1522 SDE_ERROR("invalid crtc or catalog\n");
Clarence Ip7a753bb2016-07-07 11:47:44 -04001523 return;
1524 }
1525
1526 sde_crtc = to_sde_crtc(crtc);
1527 dev = crtc->dev;
Alan Kwong9aa061c2016-11-06 21:17:12 -05001528 sde_kms = _sde_crtc_get_kms(crtc);
Clarence Ip7a753bb2016-07-07 11:47:44 -04001529
Dhaval Patele4a5dda2016-10-13 19:29:30 -07001530 info = kzalloc(sizeof(struct sde_kms_info), GFP_KERNEL);
1531 if (!info) {
1532 SDE_ERROR("failed to allocate info memory\n");
1533 return;
1534 }
1535
Clarence Ip7a753bb2016-07-07 11:47:44 -04001536 /* range properties */
1537 msm_property_install_range(&sde_crtc->property_info,
Dhaval Patel4e574842016-08-23 15:11:37 -07001538 "input_fence_timeout", 0x0, 0, SDE_CRTC_MAX_INPUT_FENCE_TIMEOUT,
1539 SDE_CRTC_INPUT_FENCE_TIMEOUT, CRTC_PROP_INPUT_FENCE_TIMEOUT);
1540
1541 msm_property_install_range(&sde_crtc->property_info, "output_fence",
1542 0x0, 0, INR_OPEN_MAX, 0x0, CRTC_PROP_OUTPUT_FENCE);
Clarence Ip1d9728b2016-09-01 11:10:54 -04001543
1544 msm_property_install_range(&sde_crtc->property_info,
1545 "output_fence_offset", 0x0, 0, 1, 0,
1546 CRTC_PROP_OUTPUT_FENCE_OFFSET);
Dhaval Patele4a5dda2016-10-13 19:29:30 -07001547
Alan Kwong9aa061c2016-11-06 21:17:12 -05001548 msm_property_install_range(&sde_crtc->property_info,
1549 "core_clk", 0x0, 0, U64_MAX,
1550 sde_kms->perf.max_core_clk_rate,
1551 CRTC_PROP_CORE_CLK);
1552 msm_property_install_range(&sde_crtc->property_info,
1553 "core_ab", 0x0, 0, U64_MAX,
1554 SDE_POWER_HANDLE_DATA_BUS_AB_QUOTA,
1555 CRTC_PROP_CORE_AB);
1556 msm_property_install_range(&sde_crtc->property_info,
1557 "core_ib", 0x0, 0, U64_MAX,
1558 SDE_POWER_HANDLE_DATA_BUS_IB_QUOTA,
1559 CRTC_PROP_CORE_IB);
1560
Dhaval Patele4a5dda2016-10-13 19:29:30 -07001561 msm_property_install_blob(&sde_crtc->property_info, "capabilities",
1562 DRM_MODE_PROP_IMMUTABLE, CRTC_PROP_INFO);
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08001563
1564 if (catalog->has_dim_layer) {
1565 msm_property_install_volatile_range(&sde_crtc->property_info,
1566 "dim_layer_v1", 0x0, 0, ~0, 0, CRTC_PROP_DIM_LAYER_V1);
1567 }
1568
Dhaval Patele4a5dda2016-10-13 19:29:30 -07001569 sde_kms_info_reset(info);
1570
1571 sde_kms_info_add_keyint(info, "hw_version", catalog->hwversion);
1572 sde_kms_info_add_keyint(info, "max_linewidth",
1573 catalog->max_mixer_width);
1574 sde_kms_info_add_keyint(info, "max_blendstages",
1575 catalog->max_mixer_blendstages);
1576 if (catalog->qseed_type == SDE_SSPP_SCALER_QSEED2)
1577 sde_kms_info_add_keystr(info, "qseed_type", "qseed2");
1578 if (catalog->qseed_type == SDE_SSPP_SCALER_QSEED3)
1579 sde_kms_info_add_keystr(info, "qseed_type", "qseed3");
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08001580
1581 if (sde_is_custom_client()) {
1582 if (catalog->smart_dma_rev == SDE_SSPP_SMART_DMA_V1)
1583 sde_kms_info_add_keystr(info,
1584 "smart_dma_rev", "smart_dma_v1");
1585 if (catalog->smart_dma_rev == SDE_SSPP_SMART_DMA_V2)
1586 sde_kms_info_add_keystr(info,
1587 "smart_dma_rev", "smart_dma_v2");
1588 }
1589
Dhaval Patele4a5dda2016-10-13 19:29:30 -07001590 sde_kms_info_add_keyint(info, "has_src_split", catalog->has_src_split);
Alan Kwong2f84f8a2016-12-29 13:07:47 -05001591 if (catalog->perf.max_bw_low)
1592 sde_kms_info_add_keyint(info, "max_bandwidth_low",
1593 catalog->perf.max_bw_low);
1594 if (catalog->perf.max_bw_high)
1595 sde_kms_info_add_keyint(info, "max_bandwidth_high",
1596 catalog->perf.max_bw_high);
1597 if (sde_kms->perf.max_core_clk_rate)
1598 sde_kms_info_add_keyint(info, "max_mdp_clk",
1599 sde_kms->perf.max_core_clk_rate);
Dhaval Patele4a5dda2016-10-13 19:29:30 -07001600 msm_property_set_blob(&sde_crtc->property_info, &sde_crtc->blob_info,
1601 info->data, info->len, CRTC_PROP_INFO);
1602
1603 kfree(info);
Clarence Ip7a753bb2016-07-07 11:47:44 -04001604}
1605
1606/**
1607 * sde_crtc_atomic_set_property - atomically set a crtc drm property
1608 * @crtc: Pointer to drm crtc structure
1609 * @state: Pointer to drm crtc state structure
1610 * @property: Pointer to targeted drm property
1611 * @val: Updated property value
1612 * @Returns: Zero on success
1613 */
1614static int sde_crtc_atomic_set_property(struct drm_crtc *crtc,
1615 struct drm_crtc_state *state,
1616 struct drm_property *property,
1617 uint64_t val)
1618{
1619 struct sde_crtc *sde_crtc;
1620 struct sde_crtc_state *cstate;
Clarence Ipcae1bb62016-07-07 12:07:13 -04001621 int idx, ret = -EINVAL;
Clarence Ip7a753bb2016-07-07 11:47:44 -04001622
1623 if (!crtc || !state || !property) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07001624 SDE_ERROR("invalid argument(s)\n");
Clarence Ip7a753bb2016-07-07 11:47:44 -04001625 } else {
1626 sde_crtc = to_sde_crtc(crtc);
1627 cstate = to_sde_crtc_state(state);
1628 ret = msm_property_atomic_set(&sde_crtc->property_info,
1629 cstate->property_values, cstate->property_blobs,
1630 property, val);
Clarence Ipcae1bb62016-07-07 12:07:13 -04001631 if (!ret) {
1632 idx = msm_property_index(&sde_crtc->property_info,
1633 property);
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08001634 switch (idx) {
1635 case CRTC_PROP_INPUT_FENCE_TIMEOUT:
Clarence Ipcae1bb62016-07-07 12:07:13 -04001636 _sde_crtc_set_input_fence_timeout(cstate);
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08001637 break;
1638 case CRTC_PROP_DIM_LAYER_V1:
1639 _sde_crtc_set_dim_layer_v1(cstate, (void *)val);
1640 break;
1641 default:
1642 /* nothing to do */
1643 break;
1644 }
Gopikrishnaiah Anandane0e5e0c2016-05-25 11:05:33 -07001645 } else {
1646 ret = sde_cp_crtc_set_property(crtc,
1647 property, val);
Clarence Ipcae1bb62016-07-07 12:07:13 -04001648 }
Gopikrishnaiah Anandane0e5e0c2016-05-25 11:05:33 -07001649 if (ret)
1650 DRM_ERROR("failed to set the property\n");
Clarence Ip7a753bb2016-07-07 11:47:44 -04001651 }
1652
1653 return ret;
1654}
1655
1656/**
1657 * sde_crtc_set_property - set a crtc drm property
1658 * @crtc: Pointer to drm crtc structure
1659 * @property: Pointer to targeted drm property
1660 * @val: Updated property value
1661 * @Returns: Zero on success
1662 */
1663static int sde_crtc_set_property(struct drm_crtc *crtc,
1664 struct drm_property *property, uint64_t val)
1665{
Lloyd Atkinson4f1c8692016-09-14 14:04:25 -04001666 SDE_DEBUG("\n");
Clarence Ip7a753bb2016-07-07 11:47:44 -04001667
1668 return sde_crtc_atomic_set_property(crtc, crtc->state, property, val);
1669}
1670
1671/**
1672 * sde_crtc_atomic_get_property - retrieve a crtc drm property
1673 * @crtc: Pointer to drm crtc structure
1674 * @state: Pointer to drm crtc state structure
1675 * @property: Pointer to targeted drm property
1676 * @val: Pointer to variable for receiving property value
1677 * @Returns: Zero on success
1678 */
1679static int sde_crtc_atomic_get_property(struct drm_crtc *crtc,
1680 const struct drm_crtc_state *state,
1681 struct drm_property *property,
1682 uint64_t *val)
1683{
1684 struct sde_crtc *sde_crtc;
1685 struct sde_crtc_state *cstate;
Clarence Ip24f80662016-06-13 19:05:32 -04001686 int i, ret = -EINVAL;
Clarence Ip7a753bb2016-07-07 11:47:44 -04001687
1688 if (!crtc || !state) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07001689 SDE_ERROR("invalid argument(s)\n");
Clarence Ip7a753bb2016-07-07 11:47:44 -04001690 } else {
1691 sde_crtc = to_sde_crtc(crtc);
1692 cstate = to_sde_crtc_state(state);
Clarence Ip24f80662016-06-13 19:05:32 -04001693 i = msm_property_index(&sde_crtc->property_info, property);
1694 if (i == CRTC_PROP_OUTPUT_FENCE) {
Clarence Ip1d9728b2016-09-01 11:10:54 -04001695 int offset = sde_crtc_get_property(cstate,
1696 CRTC_PROP_OUTPUT_FENCE_OFFSET);
1697
1698 ret = sde_fence_create(
1699 &sde_crtc->output_fence, val, offset);
1700 if (ret)
1701 SDE_ERROR("fence create failed\n");
Clarence Ip24f80662016-06-13 19:05:32 -04001702 } else {
1703 ret = msm_property_atomic_get(&sde_crtc->property_info,
1704 cstate->property_values,
1705 cstate->property_blobs, property, val);
Gopikrishnaiah Anandane0e5e0c2016-05-25 11:05:33 -07001706 if (ret)
1707 ret = sde_cp_crtc_get_property(crtc,
1708 property, val);
Clarence Ip24f80662016-06-13 19:05:32 -04001709 }
Gopikrishnaiah Anandane0e5e0c2016-05-25 11:05:33 -07001710 if (ret)
1711 DRM_ERROR("get property failed\n");
Clarence Ip7a753bb2016-07-07 11:47:44 -04001712 }
Clarence Ip7a753bb2016-07-07 11:47:44 -04001713 return ret;
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07001714}
1715
Alan Kwong67a3f792016-11-01 23:16:53 -04001716#ifdef CONFIG_DEBUG_FS
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07001717static int _sde_debugfs_status_show(struct seq_file *s, void *data)
Clarence Ip8f7366c2016-07-05 12:15:26 -04001718{
1719 struct sde_crtc *sde_crtc;
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07001720 struct sde_plane_state *pstate = NULL;
Clarence Ip8f7366c2016-07-05 12:15:26 -04001721 struct sde_crtc_mixer *m;
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07001722
1723 struct drm_crtc *crtc;
1724 struct drm_plane *plane;
1725 struct drm_display_mode *mode;
1726 struct drm_framebuffer *fb;
1727 struct drm_plane_state *state;
1728
1729 int i, out_width;
Clarence Ip8f7366c2016-07-05 12:15:26 -04001730
1731 if (!s || !s->private)
1732 return -EINVAL;
1733
1734 sde_crtc = s->private;
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07001735 crtc = &sde_crtc->base;
1736
1737 mutex_lock(&sde_crtc->crtc_lock);
1738 mode = &crtc->state->adjusted_mode;
1739 out_width = sde_crtc_mixer_width(sde_crtc, mode);
1740
1741 seq_printf(s, "crtc:%d width:%d height:%d\n", crtc->base.id,
1742 mode->hdisplay, mode->vdisplay);
1743
1744 seq_puts(s, "\n");
1745
Clarence Ip8f7366c2016-07-05 12:15:26 -04001746 for (i = 0; i < sde_crtc->num_mixers; ++i) {
Lloyd Atkinsone7bcdd22016-08-11 10:53:37 -04001747 m = &sde_crtc->mixers[i];
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07001748 if (!m->hw_lm)
1749 seq_printf(s, "\tmixer[%d] has no lm\n", i);
1750 else if (!m->hw_ctl)
1751 seq_printf(s, "\tmixer[%d] has no ctl\n", i);
1752 else
1753 seq_printf(s, "\tmixer:%d ctl:%d width:%d height:%d\n",
1754 m->hw_lm->idx - LM_0, m->hw_ctl->idx - CTL_0,
1755 out_width, mode->vdisplay);
Clarence Ip8f7366c2016-07-05 12:15:26 -04001756 }
Dhaval Patel44f12472016-08-29 12:19:47 -07001757
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07001758 seq_puts(s, "\n");
Dhaval Patel48c76022016-09-01 17:51:23 -07001759
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07001760 drm_atomic_crtc_for_each_plane(plane, crtc) {
1761 pstate = to_sde_plane_state(plane->state);
1762 state = plane->state;
1763
1764 if (!pstate || !state)
1765 continue;
1766
1767 seq_printf(s, "\tplane:%u stage:%d\n", plane->base.id,
1768 pstate->stage);
1769
1770 if (plane->state->fb) {
1771 fb = plane->state->fb;
1772
1773 seq_printf(s, "\tfb:%d image format:%4.4s wxh:%ux%u bpp:%d\n",
1774 fb->base.id, (char *) &fb->pixel_format,
1775 fb->width, fb->height, fb->bits_per_pixel);
1776
1777 seq_puts(s, "\t");
1778 for (i = 0; i < ARRAY_SIZE(fb->modifier); i++)
1779 seq_printf(s, "modifier[%d]:%8llu ", i,
1780 fb->modifier[i]);
1781 seq_puts(s, "\n");
1782
1783 seq_puts(s, "\t");
1784 for (i = 0; i < ARRAY_SIZE(fb->pitches); i++)
1785 seq_printf(s, "pitches[%d]:%8u ", i,
1786 fb->pitches[i]);
1787 seq_puts(s, "\n");
1788
1789 seq_puts(s, "\t");
1790 for (i = 0; i < ARRAY_SIZE(fb->offsets); i++)
1791 seq_printf(s, "offsets[%d]:%8u ", i,
1792 fb->offsets[i]);
Dhaval Patel48c76022016-09-01 17:51:23 -07001793 seq_puts(s, "\n");
1794 }
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07001795
1796 seq_printf(s, "\tsrc_x:%4d src_y:%4d src_w:%4d src_h:%4d\n",
1797 state->src_x, state->src_y, state->src_w, state->src_h);
1798
1799 seq_printf(s, "\tdst x:%4d dst_y:%4d dst_w:%4d dst_h:%4d\n",
1800 state->crtc_x, state->crtc_y, state->crtc_w,
1801 state->crtc_h);
1802 seq_puts(s, "\n");
Clarence Ip8f7366c2016-07-05 12:15:26 -04001803 }
Alan Kwong07da0982016-11-04 12:57:45 -04001804
1805 if (sde_crtc->vblank_cb_count) {
1806 ktime_t diff = ktime_sub(ktime_get(), sde_crtc->vblank_cb_time);
1807 s64 diff_ms = ktime_to_ms(diff);
1808 s64 fps = diff_ms ? DIV_ROUND_CLOSEST(
1809 sde_crtc->vblank_cb_count * 1000, diff_ms) : 0;
1810
1811 seq_printf(s,
1812 "vblank fps:%lld count:%u total:%llums\n",
1813 fps,
1814 sde_crtc->vblank_cb_count,
1815 ktime_to_ms(diff));
1816
1817 /* reset time & count for next measurement */
1818 sde_crtc->vblank_cb_count = 0;
1819 sde_crtc->vblank_cb_time = ktime_set(0, 0);
1820 }
1821
1822 seq_printf(s, "vblank_refcount:%d\n",
1823 atomic_read(&sde_crtc->vblank_refcount));
1824
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07001825 mutex_unlock(&sde_crtc->crtc_lock);
1826
Clarence Ip8f7366c2016-07-05 12:15:26 -04001827 return 0;
1828}
1829
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07001830static int _sde_debugfs_status_open(struct inode *inode, struct file *file)
Clarence Ip8f7366c2016-07-05 12:15:26 -04001831{
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07001832 return single_open(file, _sde_debugfs_status_show, inode->i_private);
Clarence Ip8f7366c2016-07-05 12:15:26 -04001833}
Alan Kwong67a3f792016-11-01 23:16:53 -04001834#endif
Clarence Ip8f7366c2016-07-05 12:15:26 -04001835
Clarence Ip7a753bb2016-07-07 11:47:44 -04001836static const struct drm_crtc_funcs sde_crtc_funcs = {
1837 .set_config = drm_atomic_helper_set_config,
1838 .destroy = sde_crtc_destroy,
1839 .page_flip = drm_atomic_helper_page_flip,
1840 .set_property = sde_crtc_set_property,
1841 .atomic_set_property = sde_crtc_atomic_set_property,
1842 .atomic_get_property = sde_crtc_atomic_get_property,
1843 .reset = sde_crtc_reset,
1844 .atomic_duplicate_state = sde_crtc_duplicate_state,
1845 .atomic_destroy_state = sde_crtc_destroy_state,
Clarence Ip7a753bb2016-07-07 11:47:44 -04001846};
1847
1848static const struct drm_crtc_helper_funcs sde_crtc_helper_funcs = {
1849 .mode_fixup = sde_crtc_mode_fixup,
Clarence Ip7a753bb2016-07-07 11:47:44 -04001850 .disable = sde_crtc_disable,
1851 .enable = sde_crtc_enable,
1852 .atomic_check = sde_crtc_atomic_check,
1853 .atomic_begin = sde_crtc_atomic_begin,
1854 .atomic_flush = sde_crtc_atomic_flush,
1855};
1856
Alan Kwong67a3f792016-11-01 23:16:53 -04001857#ifdef CONFIG_DEBUG_FS
1858#define DEFINE_SDE_DEBUGFS_SEQ_FOPS(__prefix) \
1859static int __prefix ## _open(struct inode *inode, struct file *file) \
1860{ \
1861 return single_open(file, __prefix ## _show, inode->i_private); \
1862} \
1863static const struct file_operations __prefix ## _fops = { \
1864 .owner = THIS_MODULE, \
1865 .open = __prefix ## _open, \
1866 .release = single_release, \
1867 .read = seq_read, \
1868 .llseek = seq_lseek, \
1869}
1870
1871static int sde_crtc_debugfs_state_show(struct seq_file *s, void *v)
1872{
1873 struct drm_crtc *crtc = (struct drm_crtc *) s->private;
1874 struct sde_crtc_state *cstate = to_sde_crtc_state(crtc->state);
1875
1876 seq_printf(s, "num_connectors: %d\n", cstate->num_connectors);
Dhaval Patel4d424602017-02-18 19:40:14 -08001877 seq_printf(s, "client type: %d\n", sde_crtc_get_client_type(crtc));
Alan Kwong67a3f792016-11-01 23:16:53 -04001878 seq_printf(s, "intf_mode: %d\n", cstate->intf_mode);
Alan Kwong9aa061c2016-11-06 21:17:12 -05001879 seq_printf(s, "bw_ctl: %llu\n", cstate->cur_perf.bw_ctl);
1880 seq_printf(s, "core_clk_rate: %u\n", cstate->cur_perf.core_clk_rate);
1881 seq_printf(s, "max_per_pipe_ib: %llu\n",
1882 cstate->cur_perf.max_per_pipe_ib);
Alan Kwong67a3f792016-11-01 23:16:53 -04001883
1884 return 0;
1885}
1886DEFINE_SDE_DEBUGFS_SEQ_FOPS(sde_crtc_debugfs_state);
1887
Clarence Ip8f7366c2016-07-05 12:15:26 -04001888static void _sde_crtc_init_debugfs(struct sde_crtc *sde_crtc,
1889 struct sde_kms *sde_kms)
1890{
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07001891 static const struct file_operations debugfs_status_fops = {
1892 .open = _sde_debugfs_status_open,
Clarence Ip8f7366c2016-07-05 12:15:26 -04001893 .read = seq_read,
1894 .llseek = seq_lseek,
1895 .release = single_release,
1896 };
Alan Kwong67a3f792016-11-01 23:16:53 -04001897
Clarence Ip8f7366c2016-07-05 12:15:26 -04001898 if (sde_crtc && sde_kms) {
1899 sde_crtc->debugfs_root = debugfs_create_dir(sde_crtc->name,
1900 sde_debugfs_get_root(sde_kms));
1901 if (sde_crtc->debugfs_root) {
1902 /* don't error check these */
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07001903 debugfs_create_file("status", 0444,
Clarence Ip8f7366c2016-07-05 12:15:26 -04001904 sde_crtc->debugfs_root,
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07001905 sde_crtc, &debugfs_status_fops);
Alan Kwong9aa061c2016-11-06 21:17:12 -05001906 debugfs_create_file("state", 0644,
1907 sde_crtc->debugfs_root,
1908 &sde_crtc->base,
1909 &sde_crtc_debugfs_state_fops);
Clarence Ip8f7366c2016-07-05 12:15:26 -04001910 }
1911 }
1912}
Alan Kwong67a3f792016-11-01 23:16:53 -04001913#else
1914static void _sde_crtc_init_debugfs(struct sde_crtc *sde_crtc,
1915 struct sde_kms *sde_kms)
1916{
1917}
1918#endif
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07001919
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001920/* initialize crtc */
Lloyd Atkinsonac933642016-09-14 11:52:00 -04001921struct drm_crtc *sde_crtc_init(struct drm_device *dev, struct drm_plane *plane)
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07001922{
1923 struct drm_crtc *crtc = NULL;
Clarence Ip8f7366c2016-07-05 12:15:26 -04001924 struct sde_crtc *sde_crtc = NULL;
1925 struct msm_drm_private *priv = NULL;
1926 struct sde_kms *kms = NULL;
Alan Kwong628d19e2016-10-31 13:50:13 -04001927 int i;
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07001928
Clarence Ip8f7366c2016-07-05 12:15:26 -04001929 priv = dev->dev_private;
1930 kms = to_sde_kms(priv->kms);
1931
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07001932 sde_crtc = kzalloc(sizeof(*sde_crtc), GFP_KERNEL);
1933 if (!sde_crtc)
1934 return ERR_PTR(-ENOMEM);
1935
1936 crtc = &sde_crtc->base;
Gopikrishnaiah Anandane0e5e0c2016-05-25 11:05:33 -07001937 crtc->dev = dev;
Alan Kwong07da0982016-11-04 12:57:45 -04001938 atomic_set(&sde_crtc->vblank_refcount, 0);
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07001939
Alan Kwong628d19e2016-10-31 13:50:13 -04001940 spin_lock_init(&sde_crtc->spin_lock);
1941 atomic_set(&sde_crtc->frame_pending, 0);
1942
1943 INIT_LIST_HEAD(&sde_crtc->frame_event_list);
1944 for (i = 0; i < ARRAY_SIZE(sde_crtc->frame_events); i++) {
1945 INIT_LIST_HEAD(&sde_crtc->frame_events[i].list);
1946 list_add(&sde_crtc->frame_events[i].list,
1947 &sde_crtc->frame_event_list);
1948 kthread_init_work(&sde_crtc->frame_events[i].work,
1949 sde_crtc_frame_event_work);
1950 }
1951
Dhaval Patel04c7e8e2016-09-26 20:14:31 -07001952 drm_crtc_init_with_planes(dev, crtc, plane, NULL, &sde_crtc_funcs,
1953 NULL);
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07001954
1955 drm_crtc_helper_add(crtc, &sde_crtc_helper_funcs);
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001956 plane->crtc = crtc;
1957
Clarence Ip8f7366c2016-07-05 12:15:26 -04001958 /* save user friendly CRTC name for later */
1959 snprintf(sde_crtc->name, SDE_CRTC_NAME_SIZE, "crtc%u", crtc->base.id);
1960
Clarence Ip9a74a442016-08-25 18:29:03 -04001961 /* initialize output fence support */
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07001962 mutex_init(&sde_crtc->crtc_lock);
Lloyd Atkinson5d40d312016-09-06 08:34:13 -04001963 sde_fence_init(&sde_crtc->output_fence, sde_crtc->name, crtc->base.id);
Clarence Ip24f80662016-06-13 19:05:32 -04001964
1965 /* initialize debugfs support */
Clarence Ip8f7366c2016-07-05 12:15:26 -04001966 _sde_crtc_init_debugfs(sde_crtc, kms);
1967
Clarence Ip7a753bb2016-07-07 11:47:44 -04001968 /* create CRTC properties */
1969 msm_property_init(&sde_crtc->property_info, &crtc->base, dev,
1970 priv->crtc_property, sde_crtc->property_data,
1971 CRTC_PROP_COUNT, CRTC_PROP_BLOBCOUNT,
1972 sizeof(struct sde_crtc_state));
1973
Dhaval Patele4a5dda2016-10-13 19:29:30 -07001974 sde_crtc_install_properties(crtc, kms->catalog);
Gopikrishnaiah Anandan703eb902016-10-06 18:43:57 -07001975
1976 /* Install color processing properties */
Gopikrishnaiah Anandane0e5e0c2016-05-25 11:05:33 -07001977 sde_cp_crtc_init(crtc);
Gopikrishnaiah Anandan703eb902016-10-06 18:43:57 -07001978 sde_cp_crtc_install_properties(crtc);
Clarence Ip7a753bb2016-07-07 11:47:44 -04001979
Dhaval Patelec10fad2016-08-22 14:40:48 -07001980 SDE_DEBUG("%s: successfully initialized crtc\n", sde_crtc->name);
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07001981 return crtc;
1982}