blob: 30bb72bf03676bdcfc4fb189e9d8ffa69140aa3a [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"
Narendra Muppalla77b32932017-05-10 13:53:11 -070039#include "sde_trace.h"
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -040040
Gopikrishnaiah Anandande2c81b2017-03-15 12:41:29 -070041struct sde_crtc_irq_info {
42 struct sde_irq_callback irq;
43 u32 event;
44 int (*func)(struct drm_crtc *crtc, bool en,
45 struct sde_irq_callback *irq);
46 struct list_head list;
47};
48
Gopikrishnaiah Anandanb6b401f2017-03-14 16:39:49 -070049struct sde_crtc_custom_events {
50 u32 event;
51 int (*func)(struct drm_crtc *crtc, bool en,
52 struct sde_irq_callback *irq);
53};
54
Gopikrishnaiah Anandan84b4f672017-04-26 10:28:51 -070055static int sde_crtc_power_interrupt_handler(struct drm_crtc *crtc_drm,
56 bool en, struct sde_irq_callback *ad_irq);
57
Gopikrishnaiah Anandanb6b401f2017-03-14 16:39:49 -070058static struct sde_crtc_custom_events custom_events[] = {
Gopikrishnaiah Anandan84b4f672017-04-26 10:28:51 -070059 {DRM_EVENT_AD_BACKLIGHT, sde_cp_ad_interrupt},
60 {DRM_EVENT_CRTC_POWER, sde_crtc_power_interrupt_handler}
Gopikrishnaiah Anandanb6b401f2017-03-14 16:39:49 -070061};
62
Clarence Ipcae1bb62016-07-07 12:07:13 -040063/* default input fence timeout, in ms */
64#define SDE_CRTC_INPUT_FENCE_TIMEOUT 2000
65
Dhaval Patel4e574842016-08-23 15:11:37 -070066/*
67 * The default input fence timeout is 2 seconds while max allowed
68 * range is 10 seconds. Any value above 10 seconds adds glitches beyond
69 * tolerance limit.
70 */
71#define SDE_CRTC_MAX_INPUT_FENCE_TIMEOUT 10000
72
Dhaval Patel48c76022016-09-01 17:51:23 -070073/* layer mixer index on sde_crtc */
74#define LEFT_MIXER 0
75#define RIGHT_MIXER 1
76
Dhaval Patelf9245d62017-03-28 16:24:00 -070077#define MISR_BUFF_SIZE 256
78
Lloyd Atkinson4f1c8692016-09-14 14:04:25 -040079static inline struct sde_kms *_sde_crtc_get_kms(struct drm_crtc *crtc)
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -040080{
Clarence Ip7f70ce42017-03-20 06:53:46 -070081 struct msm_drm_private *priv;
82
83 if (!crtc || !crtc->dev || !crtc->dev->dev_private) {
84 SDE_ERROR("invalid crtc\n");
85 return NULL;
86 }
87 priv = crtc->dev->dev_private;
88 if (!priv || !priv->kms) {
89 SDE_ERROR("invalid kms\n");
90 return NULL;
91 }
Abhijit Kulkarni40e38162016-06-26 22:12:09 -040092
Ben Chan78647cd2016-06-26 22:02:47 -040093 return to_sde_kms(priv->kms);
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -040094}
95
Dhaval Patelf9245d62017-03-28 16:24:00 -070096static inline int _sde_crtc_power_enable(struct sde_crtc *sde_crtc, bool enable)
97{
98 struct drm_crtc *crtc;
99 struct msm_drm_private *priv;
100 struct sde_kms *sde_kms;
101
102 if (!sde_crtc) {
103 SDE_ERROR("invalid sde crtc\n");
104 return -EINVAL;
105 }
106
107 crtc = &sde_crtc->base;
108 if (!crtc->dev || !crtc->dev->dev_private) {
109 SDE_ERROR("invalid drm device\n");
110 return -EINVAL;
111 }
112
113 priv = crtc->dev->dev_private;
114 if (!priv->kms) {
115 SDE_ERROR("invalid kms\n");
116 return -EINVAL;
117 }
118
119 sde_kms = to_sde_kms(priv->kms);
120
121 return sde_power_resource_enable(&priv->phandle, sde_kms->core_client,
122 enable);
123}
124
Alan Kwongcdb2f282017-03-18 13:42:06 -0700125/**
126 * _sde_crtc_rp_to_crtc - get crtc from resource pool object
127 * @rp: Pointer to resource pool
128 * return: Pointer to drm crtc if success; null otherwise
129 */
130static struct drm_crtc *_sde_crtc_rp_to_crtc(struct sde_crtc_respool *rp)
131{
132 if (!rp)
133 return NULL;
134
135 return container_of(rp, struct sde_crtc_state, rp)->base.crtc;
136}
137
138/**
139 * _sde_crtc_rp_reclaim - reclaim unused, or all if forced, resources in pool
140 * @rp: Pointer to resource pool
141 * @force: True to reclaim all resources; otherwise, reclaim only unused ones
142 * return: None
143 */
144static void _sde_crtc_rp_reclaim(struct sde_crtc_respool *rp, bool force)
145{
146 struct sde_crtc_res *res, *next;
147 struct drm_crtc *crtc;
148
149 crtc = _sde_crtc_rp_to_crtc(rp);
150 if (!crtc) {
151 SDE_ERROR("invalid crtc\n");
152 return;
153 }
154
155 SDE_DEBUG("crtc%d.%u %s\n", crtc->base.id, rp->sequence_id,
156 force ? "destroy" : "free_unused");
157
158 list_for_each_entry_safe(res, next, &rp->res_list, list) {
159 if (!force && !(res->flags & SDE_CRTC_RES_FLAG_FREE))
160 continue;
161 SDE_DEBUG("crtc%d.%u reclaim res:0x%x/0x%llx/%pK/%d\n",
162 crtc->base.id, rp->sequence_id,
163 res->type, res->tag, res->val,
164 atomic_read(&res->refcount));
165 list_del(&res->list);
166 if (res->ops.put)
167 res->ops.put(res->val);
168 kfree(res);
169 }
170}
171
172/**
173 * _sde_crtc_rp_free_unused - free unused resource in pool
174 * @rp: Pointer to resource pool
175 * return: none
176 */
177static void _sde_crtc_rp_free_unused(struct sde_crtc_respool *rp)
178{
179 _sde_crtc_rp_reclaim(rp, false);
180}
181
182/**
183 * _sde_crtc_rp_destroy - destroy resource pool
184 * @rp: Pointer to resource pool
185 * return: None
186 */
187static void _sde_crtc_rp_destroy(struct sde_crtc_respool *rp)
188{
189 _sde_crtc_rp_reclaim(rp, true);
190}
191
192/**
193 * _sde_crtc_hw_blk_get - get callback for hardware block
194 * @val: Resource handle
195 * @type: Resource type
196 * @tag: Search tag for given resource
197 * return: Resource handle
198 */
199static void *_sde_crtc_hw_blk_get(void *val, u32 type, u64 tag)
200{
201 SDE_DEBUG("res:%d/0x%llx/%pK\n", type, tag, val);
202 return sde_hw_blk_get(val, type, tag);
203}
204
205/**
206 * _sde_crtc_hw_blk_put - put callback for hardware block
207 * @val: Resource handle
208 * return: None
209 */
210static void _sde_crtc_hw_blk_put(void *val)
211{
212 SDE_DEBUG("res://%pK\n", val);
213 sde_hw_blk_put(val);
214}
215
216/**
217 * _sde_crtc_rp_duplicate - duplicate resource pool and reset reference count
218 * @rp: Pointer to original resource pool
219 * @dup_rp: Pointer to duplicated resource pool
220 * return: None
221 */
222static void _sde_crtc_rp_duplicate(struct sde_crtc_respool *rp,
223 struct sde_crtc_respool *dup_rp)
224{
225 struct sde_crtc_res *res, *dup_res;
226 struct drm_crtc *crtc;
227
228 if (!rp || !dup_rp) {
229 SDE_ERROR("invalid resource pool\n");
230 return;
231 }
232
233 crtc = _sde_crtc_rp_to_crtc(rp);
234 if (!crtc) {
235 SDE_ERROR("invalid crtc\n");
236 return;
237 }
238
239 SDE_DEBUG("crtc%d.%u duplicate\n", crtc->base.id, rp->sequence_id);
240
241 dup_rp->sequence_id = rp->sequence_id + 1;
242 INIT_LIST_HEAD(&dup_rp->res_list);
243 dup_rp->ops = rp->ops;
244 list_for_each_entry(res, &rp->res_list, list) {
245 dup_res = kzalloc(sizeof(struct sde_crtc_res), GFP_KERNEL);
246 if (!dup_res)
247 return;
248 INIT_LIST_HEAD(&dup_res->list);
249 atomic_set(&dup_res->refcount, 0);
250 dup_res->type = res->type;
251 dup_res->tag = res->tag;
252 dup_res->val = res->val;
253 dup_res->ops = res->ops;
254 dup_res->flags = SDE_CRTC_RES_FLAG_FREE;
255 SDE_DEBUG("crtc%d.%u dup res:0x%x/0x%llx/%pK/%d\n",
256 crtc->base.id, dup_rp->sequence_id,
257 dup_res->type, dup_res->tag, dup_res->val,
258 atomic_read(&dup_res->refcount));
259 list_add_tail(&dup_res->list, &dup_rp->res_list);
260 if (dup_res->ops.get)
261 dup_res->ops.get(dup_res->val, 0, -1);
262 }
263}
264
265/**
266 * _sde_crtc_rp_reset - reset resource pool after allocation
267 * @rp: Pointer to original resource pool
268 * return: None
269 */
270static void _sde_crtc_rp_reset(struct sde_crtc_respool *rp)
271{
272 if (!rp) {
273 SDE_ERROR("invalid resource pool\n");
274 return;
275 }
276
277 rp->sequence_id = 0;
278 INIT_LIST_HEAD(&rp->res_list);
279 rp->ops.get = _sde_crtc_hw_blk_get;
280 rp->ops.put = _sde_crtc_hw_blk_put;
281}
282
283/**
284 * _sde_crtc_rp_add - add given resource to resource pool
285 * @rp: Pointer to original resource pool
286 * @type: Resource type
287 * @tag: Search tag for given resource
288 * @val: Resource handle
289 * @ops: Resource callback operations
290 * return: 0 if success; error code otherwise
291 */
292static int _sde_crtc_rp_add(struct sde_crtc_respool *rp, u32 type, u64 tag,
293 void *val, struct sde_crtc_res_ops *ops)
294{
295 struct sde_crtc_res *res;
296 struct drm_crtc *crtc;
297
298 if (!rp || !ops) {
299 SDE_ERROR("invalid resource pool/ops\n");
300 return -EINVAL;
301 }
302
303 crtc = _sde_crtc_rp_to_crtc(rp);
304 if (!crtc) {
305 SDE_ERROR("invalid crtc\n");
306 return -EINVAL;
307 }
308
309 list_for_each_entry(res, &rp->res_list, list) {
310 if (res->type != type || res->tag != tag)
311 continue;
312 SDE_ERROR("crtc%d.%u already exist res:0x%x/0x%llx/%pK/%d\n",
313 crtc->base.id, rp->sequence_id,
314 res->type, res->tag, res->val,
315 atomic_read(&res->refcount));
316 return -EEXIST;
317 }
318 res = kzalloc(sizeof(struct sde_crtc_res), GFP_KERNEL);
319 if (!res)
320 return -ENOMEM;
321 INIT_LIST_HEAD(&res->list);
322 atomic_set(&res->refcount, 1);
323 res->type = type;
324 res->tag = tag;
325 res->val = val;
326 res->ops = *ops;
327 list_add_tail(&res->list, &rp->res_list);
328 SDE_DEBUG("crtc%d.%u added res:0x%x/0x%llx\n",
329 crtc->base.id, rp->sequence_id, type, tag);
330 return 0;
331}
332
333/**
334 * _sde_crtc_rp_get - lookup the resource from given resource pool and obtain
335 * if available; otherwise, obtain resource from global pool
336 * @rp: Pointer to original resource pool
337 * @type: Resource type
338 * @tag: Search tag for given resource
339 * return: Resource handle if success; pointer error or null otherwise
340 */
341static void *_sde_crtc_rp_get(struct sde_crtc_respool *rp, u32 type, u64 tag)
342{
343 struct sde_crtc_res *res;
344 void *val = NULL;
345 int rc;
346 struct drm_crtc *crtc;
347
348 if (!rp) {
349 SDE_ERROR("invalid resource pool\n");
350 return NULL;
351 }
352
353 crtc = _sde_crtc_rp_to_crtc(rp);
354 if (!crtc) {
355 SDE_ERROR("invalid crtc\n");
356 return NULL;
357 }
358
359 list_for_each_entry(res, &rp->res_list, list) {
360 if (res->type != type || res->tag != tag)
361 continue;
362 SDE_DEBUG("crtc%d.%u found res:0x%x/0x%llx/%pK/%d\n",
363 crtc->base.id, rp->sequence_id,
364 res->type, res->tag, res->val,
365 atomic_read(&res->refcount));
366 atomic_inc(&res->refcount);
367 res->flags &= ~SDE_CRTC_RES_FLAG_FREE;
368 return res->val;
369 }
370 list_for_each_entry(res, &rp->res_list, list) {
371 if (res->type != type || !(res->flags & SDE_CRTC_RES_FLAG_FREE))
372 continue;
373 SDE_DEBUG("crtc%d.%u retag res:0x%x/0x%llx/%pK/%d\n",
374 crtc->base.id, rp->sequence_id,
375 res->type, res->tag, res->val,
376 atomic_read(&res->refcount));
377 atomic_inc(&res->refcount);
378 res->tag = tag;
379 res->flags &= ~SDE_CRTC_RES_FLAG_FREE;
380 return res->val;
381 }
382 if (rp->ops.get)
383 val = rp->ops.get(NULL, type, -1);
384 if (IS_ERR_OR_NULL(val)) {
Alan Kwong42e35052017-05-05 06:52:51 -0700385 SDE_DEBUG("crtc%d.%u failed to get res:0x%x//\n",
Alan Kwongcdb2f282017-03-18 13:42:06 -0700386 crtc->base.id, rp->sequence_id, type);
387 return NULL;
388 }
389 rc = _sde_crtc_rp_add(rp, type, tag, val, &rp->ops);
390 if (rc) {
391 SDE_ERROR("crtc%d.%u failed to add res:0x%x/0x%llx\n",
392 crtc->base.id, rp->sequence_id, type, tag);
393 if (rp->ops.put)
394 rp->ops.put(val);
395 val = NULL;
396 }
397 return val;
398}
399
400/**
401 * _sde_crtc_rp_put - return given resource to resource pool
402 * @rp: Pointer to original resource pool
403 * @type: Resource type
404 * @tag: Search tag for given resource
405 * return: None
406 */
407static void _sde_crtc_rp_put(struct sde_crtc_respool *rp, u32 type, u64 tag)
408{
409 struct sde_crtc_res *res, *next;
410 struct drm_crtc *crtc;
411
412 if (!rp) {
413 SDE_ERROR("invalid resource pool\n");
414 return;
415 }
416
417 crtc = _sde_crtc_rp_to_crtc(rp);
418 if (!crtc) {
419 SDE_ERROR("invalid crtc\n");
420 return;
421 }
422
423 list_for_each_entry_safe(res, next, &rp->res_list, list) {
424 if (res->type != type || res->tag != tag)
425 continue;
426 SDE_DEBUG("crtc%d.%u found res:0x%x/0x%llx/%pK/%d\n",
427 crtc->base.id, rp->sequence_id,
428 res->type, res->tag, res->val,
429 atomic_read(&res->refcount));
430 if (res->flags & SDE_CRTC_RES_FLAG_FREE)
431 SDE_ERROR(
432 "crtc%d.%u already free res:0x%x/0x%llx/%pK/%d\n",
433 crtc->base.id, rp->sequence_id,
434 res->type, res->tag, res->val,
435 atomic_read(&res->refcount));
436 else if (atomic_dec_return(&res->refcount) == 0)
437 res->flags |= SDE_CRTC_RES_FLAG_FREE;
438
439 return;
440 }
441 SDE_ERROR("crtc%d.%u not found res:0x%x/0x%llx\n",
442 crtc->base.id, rp->sequence_id, type, tag);
443}
444
445int sde_crtc_res_add(struct drm_crtc_state *state, u32 type, u64 tag,
446 void *val, struct sde_crtc_res_ops *ops)
447{
448 struct sde_crtc_respool *rp;
449
450 if (!state) {
451 SDE_ERROR("invalid parameters\n");
452 return -EINVAL;
453 }
454
455 rp = &to_sde_crtc_state(state)->rp;
456 return _sde_crtc_rp_add(rp, type, tag, val, ops);
457}
458
459void *sde_crtc_res_get(struct drm_crtc_state *state, u32 type, u64 tag)
460{
461 struct sde_crtc_respool *rp;
462 void *val;
463
464 if (!state) {
465 SDE_ERROR("invalid parameters\n");
466 return NULL;
467 }
468
469 rp = &to_sde_crtc_state(state)->rp;
470 val = _sde_crtc_rp_get(rp, type, tag);
471 if (IS_ERR(val)) {
472 SDE_ERROR("failed to get res type:0x%x:0x%llx\n",
473 type, tag);
474 return NULL;
475 }
476
477 return val;
478}
479
480void sde_crtc_res_put(struct drm_crtc_state *state, u32 type, u64 tag)
481{
482 struct sde_crtc_respool *rp;
483
484 if (!state) {
485 SDE_ERROR("invalid parameters\n");
486 return;
487 }
488
489 rp = &to_sde_crtc_state(state)->rp;
490 _sde_crtc_rp_put(rp, type, tag);
491}
492
Clarence Ipa18d4832017-03-13 12:35:44 -0700493static void _sde_crtc_deinit_events(struct sde_crtc *sde_crtc)
494{
495 if (!sde_crtc)
496 return;
Clarence Ipa18d4832017-03-13 12:35:44 -0700497}
498
Narendra Muppalla1b0b3352015-09-29 10:16:51 -0700499static void sde_crtc_destroy(struct drm_crtc *crtc)
500{
501 struct sde_crtc *sde_crtc = to_sde_crtc(crtc);
502
Lloyd Atkinson4f1c8692016-09-14 14:04:25 -0400503 SDE_DEBUG("\n");
Clarence Ip7a753bb2016-07-07 11:47:44 -0400504
505 if (!crtc)
506 return;
507
Dhaval Patele4a5dda2016-10-13 19:29:30 -0700508 if (sde_crtc->blob_info)
509 drm_property_unreference_blob(sde_crtc->blob_info);
Clarence Ip7a753bb2016-07-07 11:47:44 -0400510 msm_property_destroy(&sde_crtc->property_info);
Gopikrishnaiah Anandane0e5e0c2016-05-25 11:05:33 -0700511 sde_cp_crtc_destroy_properties(crtc);
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -0700512
Clarence Ip24f80662016-06-13 19:05:32 -0400513 sde_fence_deinit(&sde_crtc->output_fence);
Clarence Ipa18d4832017-03-13 12:35:44 -0700514 _sde_crtc_deinit_events(sde_crtc);
Clarence Ip7a753bb2016-07-07 11:47:44 -0400515
Narendra Muppalla1b0b3352015-09-29 10:16:51 -0700516 drm_crtc_cleanup(crtc);
Clarence Ip7f70ce42017-03-20 06:53:46 -0700517 mutex_destroy(&sde_crtc->crtc_lock);
Narendra Muppalla1b0b3352015-09-29 10:16:51 -0700518 kfree(sde_crtc);
519}
520
Narendra Muppalla1b0b3352015-09-29 10:16:51 -0700521static bool sde_crtc_mode_fixup(struct drm_crtc *crtc,
522 const struct drm_display_mode *mode,
523 struct drm_display_mode *adjusted_mode)
524{
Lloyd Atkinson4f1c8692016-09-14 14:04:25 -0400525 SDE_DEBUG("\n");
Lloyd Atkinsonaf7952d2016-06-26 22:41:26 -0400526
Lloyd Atkinson4f1c8692016-09-14 14:04:25 -0400527 if (msm_is_mode_seamless(adjusted_mode) &&
528 (!crtc->enabled || crtc->state->active_changed)) {
529 SDE_ERROR("crtc state prevents seamless transition\n");
530 return false;
Lloyd Atkinsonaf7952d2016-06-26 22:41:26 -0400531 }
532
Narendra Muppalla1b0b3352015-09-29 10:16:51 -0700533 return true;
534}
535
Dhaval Patel48c76022016-09-01 17:51:23 -0700536static void _sde_crtc_setup_blend_cfg(struct sde_crtc_mixer *mixer,
537 struct sde_plane_state *pstate, struct sde_format *format)
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -0400538{
Dhaval Patel48c76022016-09-01 17:51:23 -0700539 uint32_t blend_op, fg_alpha, bg_alpha;
540 uint32_t blend_type;
Dhaval Patel44f12472016-08-29 12:19:47 -0700541 struct sde_hw_mixer *lm = mixer->hw_lm;
542
Dhaval Patel48c76022016-09-01 17:51:23 -0700543 /* default to opaque blending */
544 fg_alpha = sde_plane_get_property(pstate, PLANE_PROP_ALPHA);
545 bg_alpha = 0xFF - fg_alpha;
546 blend_op = SDE_BLEND_FG_ALPHA_FG_CONST | SDE_BLEND_BG_ALPHA_BG_CONST;
547 blend_type = sde_plane_get_property(pstate, PLANE_PROP_BLEND_OP);
Dhaval Patel44f12472016-08-29 12:19:47 -0700548
Dhaval Patel48c76022016-09-01 17:51:23 -0700549 SDE_DEBUG("blend type:0x%x blend alpha:0x%x\n", blend_type, fg_alpha);
550
551 switch (blend_type) {
552
553 case SDE_DRM_BLEND_OP_OPAQUE:
554 blend_op = SDE_BLEND_FG_ALPHA_FG_CONST |
555 SDE_BLEND_BG_ALPHA_BG_CONST;
556 break;
557
558 case SDE_DRM_BLEND_OP_PREMULTIPLIED:
559 if (format->alpha_enable) {
560 blend_op = SDE_BLEND_FG_ALPHA_FG_CONST |
561 SDE_BLEND_BG_ALPHA_FG_PIXEL;
562 if (fg_alpha != 0xff) {
563 bg_alpha = fg_alpha;
564 blend_op |= SDE_BLEND_BG_MOD_ALPHA |
565 SDE_BLEND_BG_INV_MOD_ALPHA;
566 } else {
567 blend_op |= SDE_BLEND_BG_INV_ALPHA;
568 }
569 }
570 break;
571
572 case SDE_DRM_BLEND_OP_COVERAGE:
573 if (format->alpha_enable) {
574 blend_op = SDE_BLEND_FG_ALPHA_FG_PIXEL |
575 SDE_BLEND_BG_ALPHA_FG_PIXEL;
576 if (fg_alpha != 0xff) {
577 bg_alpha = fg_alpha;
578 blend_op |= SDE_BLEND_FG_MOD_ALPHA |
579 SDE_BLEND_FG_INV_MOD_ALPHA |
580 SDE_BLEND_BG_MOD_ALPHA |
581 SDE_BLEND_BG_INV_MOD_ALPHA;
582 } else {
583 blend_op |= SDE_BLEND_BG_INV_ALPHA;
584 }
585 }
586 break;
587 default:
588 /* do nothing */
589 break;
Clarence Ipd9f9fa62016-09-09 13:42:32 -0400590 }
Dhaval Patel48c76022016-09-01 17:51:23 -0700591
592 lm->ops.setup_blend_config(lm, pstate->stage, fg_alpha,
593 bg_alpha, blend_op);
Dhaval Patel6c666622017-03-21 23:02:59 -0700594 SDE_DEBUG(
595 "format: %4.4s, alpha_enable %u fg alpha:0x%x bg alpha:0x%x blend_op:0x%x\n",
596 (char *) &format->base.pixel_format,
Dhaval Patel48c76022016-09-01 17:51:23 -0700597 format->alpha_enable, fg_alpha, bg_alpha, blend_op);
598}
599
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -0800600static void _sde_crtc_setup_dim_layer_cfg(struct drm_crtc *crtc,
601 struct sde_crtc *sde_crtc, struct sde_crtc_mixer *mixer,
602 struct sde_hw_dim_layer *dim_layer)
603{
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -0500604 struct sde_crtc_state *cstate;
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -0800605 struct sde_hw_mixer *lm;
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -0800606 struct sde_hw_dim_layer split_dim_layer;
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -0800607 int i;
608
609 if (!dim_layer->rect.w || !dim_layer->rect.h) {
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -0700610 SDE_DEBUG("empty dim_layer\n");
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -0800611 return;
612 }
613
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -0500614 cstate = to_sde_crtc_state(crtc->state);
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -0800615
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -0700616 SDE_DEBUG("dim_layer - flags:%d, stage:%d\n",
617 dim_layer->flags, dim_layer->stage);
618
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -0800619 split_dim_layer.stage = dim_layer->stage;
620 split_dim_layer.color_fill = dim_layer->color_fill;
621
622 /*
623 * traverse through the layer mixers attached to crtc and find the
624 * intersecting dim layer rect in each LM and program accordingly.
625 */
626 for (i = 0; i < sde_crtc->num_mixers; i++) {
627 split_dim_layer.flags = dim_layer->flags;
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -0800628
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -0500629 sde_kms_rect_intersect(&cstate->lm_bounds[i], &dim_layer->rect,
Lloyd Atkinsone0e11e22017-01-17 12:08:48 -0500630 &split_dim_layer.rect);
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -0500631 if (sde_kms_rect_is_null(&split_dim_layer.rect)) {
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -0800632 /*
633 * no extra programming required for non-intersecting
634 * layer mixers with INCLUSIVE dim layer
635 */
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -0500636 if (split_dim_layer.flags & SDE_DRM_DIM_LAYER_INCLUSIVE)
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -0800637 continue;
638
639 /*
640 * program the other non-intersecting layer mixers with
641 * INCLUSIVE dim layer of full size for uniformity
642 * with EXCLUSIVE dim layer config.
643 */
644 split_dim_layer.flags &= ~SDE_DRM_DIM_LAYER_EXCLUSIVE;
645 split_dim_layer.flags |= SDE_DRM_DIM_LAYER_INCLUSIVE;
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -0500646 memcpy(&split_dim_layer.rect, &cstate->lm_bounds[i],
647 sizeof(split_dim_layer.rect));
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -0800648
649 } else {
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -0500650 split_dim_layer.rect.x =
651 split_dim_layer.rect.x -
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -0700652 cstate->lm_bounds[i].x;
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -0800653 }
654
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -0700655 SDE_DEBUG("split_dim_layer - LM:%d, rect:{%d,%d,%d,%d}}\n",
656 i, split_dim_layer.rect.x, split_dim_layer.rect.y,
657 split_dim_layer.rect.w, split_dim_layer.rect.h);
658
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -0800659 lm = mixer[i].hw_lm;
660 mixer[i].mixer_op_mode |= 1 << split_dim_layer.stage;
661 lm->ops.setup_dim_layer(lm, &split_dim_layer);
662 }
663}
664
Lloyd Atkinson8ba47032017-03-22 17:13:32 -0400665void sde_crtc_get_crtc_roi(struct drm_crtc_state *state,
666 const struct sde_rect **crtc_roi)
667{
668 struct sde_crtc_state *crtc_state;
669
670 if (!state || !crtc_roi)
671 return;
672
673 crtc_state = to_sde_crtc_state(state);
674 *crtc_roi = &crtc_state->crtc_roi;
675}
676
677static int _sde_crtc_set_roi_v1(struct drm_crtc_state *state,
678 void *usr_ptr)
679{
680 struct drm_crtc *crtc;
681 struct sde_crtc_state *cstate;
682 struct sde_drm_roi_v1 roi_v1;
683 int i;
684
685 if (!state) {
686 SDE_ERROR("invalid args\n");
687 return -EINVAL;
688 }
689
690 cstate = to_sde_crtc_state(state);
691 crtc = cstate->base.crtc;
692
693 memset(&cstate->user_roi_list, 0, sizeof(cstate->user_roi_list));
694
695 if (!usr_ptr) {
696 SDE_DEBUG("crtc%d: rois cleared\n", DRMID(crtc));
697 return 0;
698 }
699
700 if (copy_from_user(&roi_v1, usr_ptr, sizeof(roi_v1))) {
701 SDE_ERROR("crtc%d: failed to copy roi_v1 data\n", DRMID(crtc));
702 return -EINVAL;
703 }
704
705 SDE_DEBUG("crtc%d: num_rects %d\n", DRMID(crtc), roi_v1.num_rects);
706
707 if (roi_v1.num_rects == 0) {
708 SDE_DEBUG("crtc%d: rois cleared\n", DRMID(crtc));
709 return 0;
710 }
711
712 if (roi_v1.num_rects > SDE_MAX_ROI_V1) {
713 SDE_ERROR("crtc%d: too many rects specified: %d\n", DRMID(crtc),
714 roi_v1.num_rects);
715 return -EINVAL;
716 }
717
718 cstate->user_roi_list.num_rects = roi_v1.num_rects;
719 for (i = 0; i < roi_v1.num_rects; ++i) {
720 cstate->user_roi_list.roi[i] = roi_v1.roi[i];
721 SDE_DEBUG("crtc%d: roi%d: roi (%d,%d) (%d,%d)\n",
722 DRMID(crtc), i,
723 cstate->user_roi_list.roi[i].x1,
724 cstate->user_roi_list.roi[i].y1,
725 cstate->user_roi_list.roi[i].x2,
726 cstate->user_roi_list.roi[i].y2);
727 }
728
729 return 0;
730}
731
Ingrid Gallardo83532222017-06-02 16:48:51 -0700732static bool _sde_crtc_setup_is_3dmux_dsc(struct drm_crtc_state *state)
733{
734 int i;
735 struct sde_crtc_state *cstate;
736 bool is_3dmux_dsc = false;
737
738 cstate = to_sde_crtc_state(state);
739
740 for (i = 0; i < cstate->num_connectors; i++) {
741 struct drm_connector *conn = cstate->connectors[i];
742
743 if (sde_connector_get_topology_name(conn) ==
744 SDE_RM_TOPOLOGY_DUALPIPE_3DMERGE_DSC)
745 is_3dmux_dsc = true;
746 }
747
748 return is_3dmux_dsc;
749}
750
Lloyd Atkinson8ba47032017-03-22 17:13:32 -0400751static int _sde_crtc_set_crtc_roi(struct drm_crtc *crtc,
752 struct drm_crtc_state *state)
753{
754 struct drm_connector *conn;
755 struct drm_connector_state *conn_state;
756 struct sde_crtc *sde_crtc;
757 struct sde_crtc_state *crtc_state;
758 struct sde_rect *crtc_roi;
Lloyd Atkinson8ba47032017-03-22 17:13:32 -0400759 int i, num_attached_conns = 0;
760
761 if (!crtc || !state)
762 return -EINVAL;
763
764 sde_crtc = to_sde_crtc(crtc);
765 crtc_state = to_sde_crtc_state(state);
766 crtc_roi = &crtc_state->crtc_roi;
767
Lloyd Atkinson8ba47032017-03-22 17:13:32 -0400768 for_each_connector_in_state(state->state, conn, conn_state, i) {
769 struct sde_connector_state *sde_conn_state;
770
771 if (!conn_state || conn_state->crtc != crtc)
772 continue;
773
774 if (num_attached_conns) {
775 SDE_ERROR(
776 "crtc%d: unsupported: roi on crtc w/ >1 connectors\n",
777 DRMID(crtc));
778 return -EINVAL;
779 }
780 ++num_attached_conns;
781
782 sde_conn_state = to_sde_connector_state(conn_state);
783
Ingrid Gallardo83532222017-06-02 16:48:51 -0700784 /*
785 * current driver only supports same connector and crtc size,
786 * but if support for different sizes is added, driver needs
787 * to check the connector roi here to make sure is full screen
788 * for dsc 3d-mux topology that doesn't support partial update.
789 */
Lloyd Atkinson8ba47032017-03-22 17:13:32 -0400790 if (memcmp(&sde_conn_state->rois, &crtc_state->user_roi_list,
791 sizeof(crtc_state->user_roi_list))) {
792 SDE_ERROR("%s: crtc -> conn roi scaling unsupported\n",
793 sde_crtc->name);
794 return -EINVAL;
795 }
796 }
797
Lloyd Atkinsonc2baf412017-04-19 17:53:09 -0400798 sde_kms_rect_merge_rectangles(&crtc_state->user_roi_list, crtc_roi);
Lloyd Atkinson8ba47032017-03-22 17:13:32 -0400799
Ingrid Gallardo83532222017-06-02 16:48:51 -0700800 /*
801 * for 3dmux dsc, make sure is full ROI, since current driver doesn't
802 * support partial update for this configuration.
803 */
804 if (!sde_kms_rect_is_null(crtc_roi) &&
805 _sde_crtc_setup_is_3dmux_dsc(state)) {
806 struct drm_display_mode *adj_mode = &state->adjusted_mode;
807
808 if (crtc_roi->w != adj_mode->hdisplay ||
809 crtc_roi->h != adj_mode->vdisplay) {
810 SDE_ERROR("%s: unsupported top roi[%d %d] wxh[%d %d]\n",
811 sde_crtc->name, crtc_roi->w, crtc_roi->h,
812 adj_mode->hdisplay, adj_mode->vdisplay);
813 return -EINVAL;
814 }
815 }
816
Lloyd Atkinson8ba47032017-03-22 17:13:32 -0400817 SDE_DEBUG("%s: crtc roi (%d,%d,%d,%d)\n", sde_crtc->name,
818 crtc_roi->x, crtc_roi->y, crtc_roi->w, crtc_roi->h);
819
820 return 0;
821}
822
Lloyd Atkinson77382202017-02-01 14:59:43 -0500823static int _sde_crtc_check_autorefresh(struct drm_crtc *crtc,
824 struct drm_crtc_state *state)
825{
826 struct sde_crtc *sde_crtc;
827 struct sde_crtc_state *crtc_state;
828 struct drm_connector *conn;
829 struct drm_connector_state *conn_state;
830 int i;
831
832 if (!crtc || !state)
833 return -EINVAL;
834
835 sde_crtc = to_sde_crtc(crtc);
836 crtc_state = to_sde_crtc_state(state);
837
838 if (sde_kms_rect_is_null(&crtc_state->crtc_roi))
839 return 0;
840
841 /* partial update active, check if autorefresh is also requested */
842 for_each_connector_in_state(state->state, conn, conn_state, i) {
843 uint64_t autorefresh;
844
845 if (!conn_state || conn_state->crtc != crtc)
846 continue;
847
848 autorefresh = sde_connector_get_property(conn_state,
849 CONNECTOR_PROP_AUTOREFRESH);
850 if (autorefresh) {
851 SDE_ERROR(
852 "%s: autorefresh & partial crtc roi incompatible %llu\n",
853 sde_crtc->name, autorefresh);
854 return -EINVAL;
855 }
856 }
857
858 return 0;
859}
860
Lloyd Atkinson8ba47032017-03-22 17:13:32 -0400861static int _sde_crtc_set_lm_roi(struct drm_crtc *crtc,
862 struct drm_crtc_state *state, int lm_idx)
863{
864 struct sde_crtc *sde_crtc;
865 struct sde_crtc_state *crtc_state;
866 const struct sde_rect *crtc_roi;
867 const struct sde_rect *lm_bounds;
868 struct sde_rect *lm_roi;
869
870 if (!crtc || !state || lm_idx >= ARRAY_SIZE(crtc_state->lm_bounds))
871 return -EINVAL;
872
873 sde_crtc = to_sde_crtc(crtc);
874 crtc_state = to_sde_crtc_state(state);
875 crtc_roi = &crtc_state->crtc_roi;
876 lm_bounds = &crtc_state->lm_bounds[lm_idx];
877 lm_roi = &crtc_state->lm_roi[lm_idx];
878
Lloyd Atkinson73fb8092017-02-08 16:02:55 -0500879 if (sde_kms_rect_is_null(crtc_roi))
Lloyd Atkinson8ba47032017-03-22 17:13:32 -0400880 memcpy(lm_roi, lm_bounds, sizeof(*lm_roi));
Lloyd Atkinson73fb8092017-02-08 16:02:55 -0500881 else
882 sde_kms_rect_intersect(crtc_roi, lm_bounds, lm_roi);
Lloyd Atkinson8ba47032017-03-22 17:13:32 -0400883
884 SDE_DEBUG("%s: lm%d roi (%d,%d,%d,%d)\n", sde_crtc->name, lm_idx,
885 lm_roi->x, lm_roi->y, lm_roi->w, lm_roi->h);
886
Lloyd Atkinson73fb8092017-02-08 16:02:55 -0500887 /* if any dimension is zero, clear all dimensions for clarity */
888 if (sde_kms_rect_is_null(lm_roi))
889 memset(lm_roi, 0, sizeof(*lm_roi));
890
Lloyd Atkinson8ba47032017-03-22 17:13:32 -0400891 return 0;
892}
893
Lloyd Atkinson73fb8092017-02-08 16:02:55 -0500894static u32 _sde_crtc_get_displays_affected(struct drm_crtc *crtc,
895 struct drm_crtc_state *state)
896{
897 struct sde_crtc *sde_crtc;
898 struct sde_crtc_state *crtc_state;
899 u32 disp_bitmask = 0;
900 int i;
901
902 sde_crtc = to_sde_crtc(crtc);
903 crtc_state = to_sde_crtc_state(state);
904
Lloyd Atkinson66e7dde2017-02-08 15:52:53 -0500905 /* pingpong split: one ROI, one LM, two physical displays */
906 if (crtc_state->is_ppsplit) {
907 u32 lm_split_width = crtc_state->lm_bounds[0].w / 2;
908 struct sde_rect *roi = &crtc_state->lm_roi[0];
909
910 if (sde_kms_rect_is_null(roi))
911 disp_bitmask = 0;
912 else if ((u32)roi->x + (u32)roi->w <= lm_split_width)
913 disp_bitmask = BIT(0); /* left only */
914 else if (roi->x >= lm_split_width)
915 disp_bitmask = BIT(1); /* right only */
916 else
917 disp_bitmask = BIT(0) | BIT(1); /* left and right */
918 } else {
919 for (i = 0; i < sde_crtc->num_mixers; i++) {
920 if (!sde_kms_rect_is_null(&crtc_state->lm_roi[i]))
921 disp_bitmask |= BIT(i);
922 }
Lloyd Atkinson73fb8092017-02-08 16:02:55 -0500923 }
924
925 SDE_DEBUG("affected displays 0x%x\n", disp_bitmask);
926
927 return disp_bitmask;
928}
929
Lloyd Atkinson8ba47032017-03-22 17:13:32 -0400930static int _sde_crtc_check_rois_centered_and_symmetric(struct drm_crtc *crtc,
931 struct drm_crtc_state *state)
932{
933 struct sde_crtc *sde_crtc;
934 struct sde_crtc_state *crtc_state;
Lloyd Atkinson73fb8092017-02-08 16:02:55 -0500935 const struct sde_rect *roi[CRTC_DUAL_MIXERS];
Lloyd Atkinson8ba47032017-03-22 17:13:32 -0400936
937 if (!crtc || !state)
938 return -EINVAL;
939
Lloyd Atkinson73fb8092017-02-08 16:02:55 -0500940 sde_crtc = to_sde_crtc(crtc);
941 crtc_state = to_sde_crtc_state(state);
942
Lloyd Atkinson73fb8092017-02-08 16:02:55 -0500943 if (sde_crtc->num_mixers > CRTC_DUAL_MIXERS) {
944 SDE_ERROR("%s: unsupported number of mixers: %d\n",
945 sde_crtc->name, sde_crtc->num_mixers);
946 return -EINVAL;
947 }
948
Lloyd Atkinson8ba47032017-03-22 17:13:32 -0400949 /*
Lloyd Atkinson66e7dde2017-02-08 15:52:53 -0500950 * If using pingpong split: one ROI, one LM, two physical displays
951 * then the ROI must be centered on the panel split boundary and
952 * be of equal width across the split.
Lloyd Atkinson8ba47032017-03-22 17:13:32 -0400953 */
Lloyd Atkinson66e7dde2017-02-08 15:52:53 -0500954 if (crtc_state->is_ppsplit) {
955 u16 panel_split_width;
956 u32 display_mask;
957
958 roi[0] = &crtc_state->lm_roi[0];
959
960 if (sde_kms_rect_is_null(roi[0]))
961 return 0;
962
963 display_mask = _sde_crtc_get_displays_affected(crtc, state);
964 if (display_mask != (BIT(0) | BIT(1)))
965 return 0;
966
967 panel_split_width = crtc_state->lm_bounds[0].w / 2;
968 if (roi[0]->x + roi[0]->w / 2 != panel_split_width) {
969 SDE_ERROR("%s: roi x %d w %d split %d\n",
970 sde_crtc->name, roi[0]->x, roi[0]->w,
971 panel_split_width);
972 return -EINVAL;
973 }
974
975 return 0;
976 }
977
978 /*
979 * On certain HW, if using 2 LM, ROIs must be split evenly between the
980 * LMs and be of equal width.
981 */
982 if (sde_crtc->num_mixers == 1)
983 return 0;
984
Lloyd Atkinson73fb8092017-02-08 16:02:55 -0500985 roi[0] = &crtc_state->lm_roi[0];
986 roi[1] = &crtc_state->lm_roi[1];
Lloyd Atkinson8ba47032017-03-22 17:13:32 -0400987
Lloyd Atkinson73fb8092017-02-08 16:02:55 -0500988 /* if one of the roi is null it's a left/right-only update */
989 if (sde_kms_rect_is_null(roi[0]) || sde_kms_rect_is_null(roi[1]))
990 return 0;
Lloyd Atkinson8ba47032017-03-22 17:13:32 -0400991
Lloyd Atkinson73fb8092017-02-08 16:02:55 -0500992 /* check lm rois are equal width & first roi ends at 2nd roi */
993 if (roi[0]->x + roi[0]->w != roi[1]->x || roi[0]->w != roi[1]->w) {
994 SDE_ERROR(
995 "%s: rois not centered and symmetric: roi0 x %d w %d roi1 x %d w %d\n",
996 sde_crtc->name, roi[0]->x, roi[0]->w,
997 roi[1]->x, roi[1]->w);
998 return -EINVAL;
Lloyd Atkinson8ba47032017-03-22 17:13:32 -0400999 }
1000
1001 return 0;
1002}
1003
1004static int _sde_crtc_check_planes_within_crtc_roi(struct drm_crtc *crtc,
1005 struct drm_crtc_state *state)
1006{
1007 struct sde_crtc *sde_crtc;
1008 struct sde_crtc_state *crtc_state;
1009 const struct sde_rect *crtc_roi;
1010 struct drm_plane_state *pstate;
1011 struct drm_plane *plane;
1012
1013 if (!crtc || !state)
1014 return -EINVAL;
1015
1016 /*
1017 * Reject commit if a Plane CRTC destination coordinates fall outside
1018 * the partial CRTC ROI. LM output is determined via connector ROIs,
1019 * if they are specified, not Plane CRTC ROIs.
1020 */
1021
1022 sde_crtc = to_sde_crtc(crtc);
1023 crtc_state = to_sde_crtc_state(state);
1024 crtc_roi = &crtc_state->crtc_roi;
1025
1026 if (sde_kms_rect_is_null(crtc_roi))
1027 return 0;
1028
1029 drm_atomic_crtc_state_for_each_plane(plane, state) {
1030 struct sde_rect plane_roi, intersection;
1031
1032 pstate = drm_atomic_get_plane_state(state->state, plane);
1033 if (IS_ERR_OR_NULL(pstate)) {
1034 int rc = PTR_ERR(pstate);
1035
1036 SDE_ERROR("%s: failed to get plane%d state, %d\n",
1037 sde_crtc->name, plane->base.id, rc);
1038 return rc;
1039 }
1040
1041 plane_roi.x = pstate->crtc_x;
1042 plane_roi.y = pstate->crtc_y;
1043 plane_roi.w = pstate->crtc_w;
1044 plane_roi.h = pstate->crtc_h;
1045 sde_kms_rect_intersect(crtc_roi, &plane_roi, &intersection);
1046 if (!sde_kms_rect_is_equal(&plane_roi, &intersection)) {
1047 SDE_ERROR(
1048 "%s: plane%d crtc roi (%d,%d,%d,%d) outside crtc roi (%d,%d,%d,%d)\n",
1049 sde_crtc->name, plane->base.id,
1050 plane_roi.x, plane_roi.y,
1051 plane_roi.w, plane_roi.h,
1052 crtc_roi->x, crtc_roi->y,
1053 crtc_roi->w, crtc_roi->h);
1054 return -E2BIG;
1055 }
1056 }
1057
1058 return 0;
1059}
1060
1061static int _sde_crtc_check_rois(struct drm_crtc *crtc,
1062 struct drm_crtc_state *state)
1063{
1064 struct sde_crtc *sde_crtc;
1065 int lm_idx;
1066 int rc;
1067
1068 if (!crtc || !state)
1069 return -EINVAL;
1070
1071 sde_crtc = to_sde_crtc(crtc);
1072
1073 rc = _sde_crtc_set_crtc_roi(crtc, state);
1074 if (rc)
1075 return rc;
1076
Lloyd Atkinson77382202017-02-01 14:59:43 -05001077 rc = _sde_crtc_check_autorefresh(crtc, state);
1078 if (rc)
1079 return rc;
1080
Lloyd Atkinson8ba47032017-03-22 17:13:32 -04001081 for (lm_idx = 0; lm_idx < sde_crtc->num_mixers; lm_idx++) {
1082 rc = _sde_crtc_set_lm_roi(crtc, state, lm_idx);
1083 if (rc)
1084 return rc;
1085 }
1086
1087 rc = _sde_crtc_check_rois_centered_and_symmetric(crtc, state);
1088 if (rc)
1089 return rc;
1090
1091 rc = _sde_crtc_check_planes_within_crtc_roi(crtc, state);
1092 if (rc)
1093 return rc;
1094
1095 return 0;
1096}
1097
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05001098static void _sde_crtc_program_lm_output_roi(struct drm_crtc *crtc)
1099{
1100 struct sde_crtc *sde_crtc;
1101 struct sde_crtc_state *crtc_state;
Lloyd Atkinson8ba47032017-03-22 17:13:32 -04001102 const struct sde_rect *lm_roi;
1103 struct sde_hw_mixer *hw_lm;
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05001104 int lm_idx, lm_horiz_position;
1105
Lloyd Atkinson8ba47032017-03-22 17:13:32 -04001106 if (!crtc)
1107 return;
1108
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05001109 sde_crtc = to_sde_crtc(crtc);
1110 crtc_state = to_sde_crtc_state(crtc->state);
1111
1112 lm_horiz_position = 0;
1113 for (lm_idx = 0; lm_idx < sde_crtc->num_mixers; lm_idx++) {
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05001114 struct sde_hw_mixer_cfg cfg;
1115
Lloyd Atkinson8ba47032017-03-22 17:13:32 -04001116 lm_roi = &crtc_state->lm_roi[lm_idx];
1117 hw_lm = sde_crtc->mixers[lm_idx].hw_lm;
1118
1119 SDE_EVT32(DRMID(crtc_state->base.crtc), lm_idx,
1120 lm_roi->x, lm_roi->y, lm_roi->w, lm_roi->h);
1121
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05001122 if (sde_kms_rect_is_null(lm_roi))
1123 continue;
1124
Ping Lif41c2ef2017-05-04 14:40:45 -07001125 hw_lm->cfg.out_width = lm_roi->w;
1126 hw_lm->cfg.out_height = lm_roi->h;
1127 hw_lm->cfg.right_mixer = lm_horiz_position;
1128
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05001129 cfg.out_width = lm_roi->w;
1130 cfg.out_height = lm_roi->h;
1131 cfg.right_mixer = lm_horiz_position++;
1132 cfg.flags = 0;
1133 hw_lm->ops.setup_mixer_out(hw_lm, &cfg);
1134 }
1135}
1136
Dhaval Patel48c76022016-09-01 17:51:23 -07001137static void _sde_crtc_blend_setup_mixer(struct drm_crtc *crtc,
1138 struct sde_crtc *sde_crtc, struct sde_crtc_mixer *mixer)
1139{
1140 struct drm_plane *plane;
Dhaval Patel6c666622017-03-21 23:02:59 -07001141 struct drm_framebuffer *fb;
1142 struct drm_plane_state *state;
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08001143 struct sde_crtc_state *cstate;
Dhaval Patel48c76022016-09-01 17:51:23 -07001144 struct sde_plane_state *pstate = NULL;
1145 struct sde_format *format;
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08001146 struct sde_hw_ctl *ctl;
1147 struct sde_hw_mixer *lm;
1148 struct sde_hw_stage_cfg *stage_cfg;
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05001149 struct sde_rect plane_crtc_roi;
Dhaval Patel48c76022016-09-01 17:51:23 -07001150
Clarence Ip7e5f0002017-05-29 18:46:56 -04001151 u32 flush_mask, flush_sbuf, flush_tmp;
Dhaval Patel572cfd22017-06-12 19:33:39 -07001152 uint32_t stage_idx, lm_idx;
1153 int zpos_cnt[SDE_STAGE_MAX + 1] = { 0 };
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08001154 int i;
Dhaval Patel572cfd22017-06-12 19:33:39 -07001155 bool bg_alpha_enable = false;
Alan Kwong4dd64c82017-02-04 18:41:51 -08001156 u32 prefill = 0;
Dhaval Patel48c76022016-09-01 17:51:23 -07001157
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08001158 if (!sde_crtc || !mixer) {
1159 SDE_ERROR("invalid sde_crtc or mixer\n");
1160 return;
1161 }
1162
1163 ctl = mixer->hw_ctl;
1164 lm = mixer->hw_lm;
1165 stage_cfg = &sde_crtc->stage_cfg;
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05001166 cstate = to_sde_crtc_state(crtc->state);
Clarence Ip7e5f0002017-05-29 18:46:56 -04001167 flush_sbuf = 0x0;
Dhaval Patel44f12472016-08-29 12:19:47 -07001168
Clarence Ip7eb90452017-05-23 11:41:19 -04001169 cstate->sbuf_cfg.rot_op_mode = SDE_CTL_ROT_OP_MODE_OFFLINE;
1170 cstate->sbuf_prefill_line = 0;
1171
Dhaval Patel44f12472016-08-29 12:19:47 -07001172 drm_atomic_crtc_for_each_plane(plane, crtc) {
Dhaval Patel6c666622017-03-21 23:02:59 -07001173 state = plane->state;
1174 if (!state)
1175 continue;
Dhaval Patel48c76022016-09-01 17:51:23 -07001176
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05001177 plane_crtc_roi.x = state->crtc_x;
1178 plane_crtc_roi.y = state->crtc_y;
1179 plane_crtc_roi.w = state->crtc_w;
1180 plane_crtc_roi.h = state->crtc_h;
1181
Dhaval Patel6c666622017-03-21 23:02:59 -07001182 pstate = to_sde_plane_state(state);
1183 fb = state->fb;
Dhaval Patel44f12472016-08-29 12:19:47 -07001184
Alan Kwong4dd64c82017-02-04 18:41:51 -08001185 if (sde_plane_is_sbuf_mode(plane, &prefill))
Clarence Ip7eb90452017-05-23 11:41:19 -04001186 cstate->sbuf_cfg.rot_op_mode =
1187 SDE_CTL_ROT_OP_MODE_INLINE_SYNC;
1188 if (prefill > cstate->sbuf_prefill_line)
1189 cstate->sbuf_prefill_line = prefill;
Alan Kwong4dd64c82017-02-04 18:41:51 -08001190
Clarence Ip7e5f0002017-05-29 18:46:56 -04001191 sde_plane_get_ctl_flush(plane, ctl, &flush_mask, &flush_tmp);
Dhaval Patel44f12472016-08-29 12:19:47 -07001192
Clarence Ip7e5f0002017-05-29 18:46:56 -04001193 /* persist rotator flush bit(s) for one more commit */
1194 flush_mask |= cstate->sbuf_flush_mask | flush_tmp;
1195 flush_sbuf |= flush_tmp;
Dhaval Patel48c76022016-09-01 17:51:23 -07001196
1197 SDE_DEBUG("crtc %d stage:%d - plane %d sspp %d fb %d\n",
Clarence Ipd9f9fa62016-09-09 13:42:32 -04001198 crtc->base.id,
Clarence Ipd9f9fa62016-09-09 13:42:32 -04001199 pstate->stage,
1200 plane->base.id,
1201 sde_plane_pipe(plane) - SSPP_VIG0,
Dhaval Patel6c666622017-03-21 23:02:59 -07001202 state->fb ? state->fb->base.id : -1);
Dhaval Patel44f12472016-08-29 12:19:47 -07001203
Dhaval Patel48c76022016-09-01 17:51:23 -07001204 format = to_sde_format(msm_framebuffer_format(pstate->base.fb));
Dhaval Patel572cfd22017-06-12 19:33:39 -07001205 if (pstate->stage == SDE_STAGE_BASE && format->alpha_enable)
1206 bg_alpha_enable = true;
Dhaval Patel44f12472016-08-29 12:19:47 -07001207
Lloyd Atkinson8ba47032017-03-22 17:13:32 -04001208 SDE_EVT32(DRMID(crtc), DRMID(plane),
1209 state->fb ? state->fb->base.id : -1,
1210 state->src_x >> 16, state->src_y >> 16,
1211 state->src_w >> 16, state->src_h >> 16,
1212 state->crtc_x, state->crtc_y,
Clarence Ip7eb90452017-05-23 11:41:19 -04001213 state->crtc_w, state->crtc_h,
1214 cstate->sbuf_cfg.rot_op_mode);
Dhaval Patel6c666622017-03-21 23:02:59 -07001215
Dhaval Patel572cfd22017-06-12 19:33:39 -07001216 stage_idx = zpos_cnt[pstate->stage]++;
1217 stage_cfg->stage[pstate->stage][stage_idx] =
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05001218 sde_plane_pipe(plane);
Dhaval Patel572cfd22017-06-12 19:33:39 -07001219 stage_cfg->multirect_index[pstate->stage][stage_idx] =
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05001220 pstate->multirect_index;
1221
Dhaval Patel572cfd22017-06-12 19:33:39 -07001222 SDE_EVT32(DRMID(crtc), DRMID(plane), stage_idx,
1223 sde_plane_pipe(plane) - SSPP_VIG0, pstate->stage,
1224 pstate->multirect_index, pstate->multirect_mode,
1225 format->base.pixel_format, fb ? fb->modifier[0] : 0);
1226
1227 /* blend config update */
1228 for (lm_idx = 0; lm_idx < sde_crtc->num_mixers; lm_idx++) {
1229 _sde_crtc_setup_blend_cfg(mixer + lm_idx, pstate,
1230 format);
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05001231 mixer[lm_idx].flush_mask |= flush_mask;
1232
Dhaval Patel572cfd22017-06-12 19:33:39 -07001233 if (bg_alpha_enable && !format->alpha_enable)
1234 mixer[lm_idx].mixer_op_mode = 0;
1235 else
1236 mixer[lm_idx].mixer_op_mode |=
Dhaval Patel48c76022016-09-01 17:51:23 -07001237 1 << pstate->stage;
Dhaval Patel48c76022016-09-01 17:51:23 -07001238 }
Dhaval Patel44f12472016-08-29 12:19:47 -07001239 }
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08001240
Clarence Ip7e5f0002017-05-29 18:46:56 -04001241 cstate->sbuf_flush_mask = flush_sbuf;
1242
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08001243 if (lm && lm->ops.setup_dim_layer) {
1244 cstate = to_sde_crtc_state(crtc->state);
1245 for (i = 0; i < cstate->num_dim_layers; i++)
1246 _sde_crtc_setup_dim_layer_cfg(crtc, sde_crtc,
1247 mixer, &cstate->dim_layer[i]);
1248 }
Alan Kwong4dd64c82017-02-04 18:41:51 -08001249
Clarence Ip7eb90452017-05-23 11:41:19 -04001250 if (ctl->ops.setup_sbuf_cfg)
Alan Kwong4dd64c82017-02-04 18:41:51 -08001251 ctl->ops.setup_sbuf_cfg(ctl, &cstate->sbuf_cfg);
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05001252
1253 _sde_crtc_program_lm_output_roi(crtc);
Dhaval Patel44f12472016-08-29 12:19:47 -07001254}
1255
Lloyd Atkinson094780d2017-04-24 17:25:08 -04001256static void _sde_crtc_swap_mixers_for_right_partial_update(
1257 struct drm_crtc *crtc)
1258{
1259 struct sde_crtc *sde_crtc;
1260 struct sde_crtc_state *cstate;
1261 struct drm_encoder *drm_enc;
1262 bool is_right_only;
1263 bool encoder_in_dsc_merge = false;
1264
1265 if (!crtc || !crtc->state)
1266 return;
1267
1268 sde_crtc = to_sde_crtc(crtc);
1269 cstate = to_sde_crtc_state(crtc->state);
1270
1271 if (sde_crtc->num_mixers != CRTC_DUAL_MIXERS)
1272 return;
1273
1274 drm_for_each_encoder(drm_enc, crtc->dev) {
1275 if (drm_enc->crtc == crtc &&
1276 sde_encoder_is_dsc_merge(drm_enc)) {
1277 encoder_in_dsc_merge = true;
1278 break;
1279 }
1280 }
1281
1282 /**
1283 * For right-only partial update with DSC merge, we swap LM0 & LM1.
1284 * This is due to two reasons:
1285 * - On 8996, there is a DSC HW requirement that in DSC Merge Mode,
1286 * the left DSC must be used, right DSC cannot be used alone.
1287 * For right-only partial update, this means swap layer mixers to map
1288 * Left LM to Right INTF. On later HW this was relaxed.
1289 * - In DSC Merge mode, the physical encoder has already registered
1290 * PP0 as the master, to switch to right-only we would have to
1291 * reprogram to be driven by PP1 instead.
1292 * To support both cases, we prefer to support the mixer swap solution.
1293 */
1294 if (!encoder_in_dsc_merge)
1295 return;
1296
1297 is_right_only = sde_kms_rect_is_null(&cstate->lm_roi[0]) &&
1298 !sde_kms_rect_is_null(&cstate->lm_roi[1]);
1299
1300 if (is_right_only && !sde_crtc->mixers_swapped) {
1301 /* right-only update swap mixers */
1302 swap(sde_crtc->mixers[0], sde_crtc->mixers[1]);
1303 sde_crtc->mixers_swapped = true;
1304 } else if (!is_right_only && sde_crtc->mixers_swapped) {
1305 /* left-only or full update, swap back */
1306 swap(sde_crtc->mixers[0], sde_crtc->mixers[1]);
1307 sde_crtc->mixers_swapped = false;
1308 }
1309
1310 SDE_DEBUG("%s: right_only %d swapped %d, mix0->lm%d, mix1->lm%d\n",
1311 sde_crtc->name, is_right_only, sde_crtc->mixers_swapped,
1312 sde_crtc->mixers[0].hw_lm->idx - LM_0,
1313 sde_crtc->mixers[1].hw_lm->idx - LM_0);
1314 SDE_EVT32(DRMID(crtc), is_right_only, sde_crtc->mixers_swapped,
1315 sde_crtc->mixers[0].hw_lm->idx - LM_0,
1316 sde_crtc->mixers[1].hw_lm->idx - LM_0);
1317}
1318
Clarence Ipd9f9fa62016-09-09 13:42:32 -04001319/**
1320 * _sde_crtc_blend_setup - configure crtc mixers
1321 * @crtc: Pointer to drm crtc structure
1322 */
1323static void _sde_crtc_blend_setup(struct drm_crtc *crtc)
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001324{
Lloyd Atkinson73fb8092017-02-08 16:02:55 -05001325 struct sde_crtc *sde_crtc;
1326 struct sde_crtc_state *sde_crtc_state;
1327 struct sde_crtc_mixer *mixer;
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001328 struct sde_hw_ctl *ctl;
1329 struct sde_hw_mixer *lm;
Dhaval Patel44f12472016-08-29 12:19:47 -07001330
1331 int i;
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001332
Lloyd Atkinson73fb8092017-02-08 16:02:55 -05001333 if (!crtc)
1334 return;
1335
1336 sde_crtc = to_sde_crtc(crtc);
1337 sde_crtc_state = to_sde_crtc_state(crtc->state);
1338 mixer = sde_crtc->mixers;
1339
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04001340 SDE_DEBUG("%s\n", sde_crtc->name);
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001341
Dhaval Patel48c76022016-09-01 17:51:23 -07001342 if (sde_crtc->num_mixers > CRTC_DUAL_MIXERS) {
1343 SDE_ERROR("invalid number mixers: %d\n", sde_crtc->num_mixers);
1344 return;
1345 }
1346
1347 for (i = 0; i < sde_crtc->num_mixers; i++) {
1348 if (!mixer[i].hw_lm || !mixer[i].hw_ctl) {
1349 SDE_ERROR("invalid lm or ctl assigned to mixer\n");
1350 return;
1351 }
1352 mixer[i].mixer_op_mode = 0;
1353 mixer[i].flush_mask = 0;
Lloyd Atkinsone5ec30d2016-08-23 14:32:32 -04001354 if (mixer[i].hw_ctl->ops.clear_all_blendstages)
1355 mixer[i].hw_ctl->ops.clear_all_blendstages(
1356 mixer[i].hw_ctl);
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08001357
1358 /* clear dim_layer settings */
1359 lm = mixer[i].hw_lm;
1360 if (lm->ops.clear_dim_layer)
1361 lm->ops.clear_dim_layer(lm);
Dhaval Patel48c76022016-09-01 17:51:23 -07001362 }
1363
Lloyd Atkinson094780d2017-04-24 17:25:08 -04001364 _sde_crtc_swap_mixers_for_right_partial_update(crtc);
1365
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001366 /* initialize stage cfg */
Clarence Ip8f7366c2016-07-05 12:15:26 -04001367 memset(&sde_crtc->stage_cfg, 0, sizeof(struct sde_hw_stage_cfg));
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001368
Dhaval Patel48c76022016-09-01 17:51:23 -07001369 _sde_crtc_blend_setup_mixer(crtc, sde_crtc, mixer);
1370
Abhijit Kulkarni71002ba2016-06-24 18:36:28 -04001371 for (i = 0; i < sde_crtc->num_mixers; i++) {
Lloyd Atkinson73fb8092017-02-08 16:02:55 -05001372 const struct sde_rect *lm_roi = &sde_crtc_state->lm_roi[i];
1373
Abhijit Kulkarni71002ba2016-06-24 18:36:28 -04001374 ctl = mixer[i].hw_ctl;
1375 lm = mixer[i].hw_lm;
Abhijit Kulkarni71002ba2016-06-24 18:36:28 -04001376
Lloyd Atkinson73fb8092017-02-08 16:02:55 -05001377 if (sde_kms_rect_is_null(lm_roi)) {
1378 SDE_DEBUG(
1379 "%s: lm%d leave ctl%d mask 0 since null roi\n",
1380 sde_crtc->name, lm->idx - LM_0,
1381 ctl->idx - CTL_0);
1382 continue;
1383 }
1384
Dhaval Patel48c76022016-09-01 17:51:23 -07001385 lm->ops.setup_alpha_out(lm, mixer[i].mixer_op_mode);
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001386
Dhaval Patel48c76022016-09-01 17:51:23 -07001387 mixer[i].flush_mask |= ctl->ops.get_bitmask_mixer(ctl,
Abhijit Kulkarni71002ba2016-06-24 18:36:28 -04001388 mixer[i].hw_lm->idx);
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04001389
1390 /* stage config flush mask */
Dhaval Patel48c76022016-09-01 17:51:23 -07001391 ctl->ops.update_pending_flush(ctl, mixer[i].flush_mask);
1392
Clarence Ip8e69ad02016-12-09 09:43:57 -05001393 SDE_DEBUG("lm %d, op_mode 0x%X, ctl %d, flush mask 0x%x\n",
1394 mixer[i].hw_lm->idx - LM_0,
1395 mixer[i].mixer_op_mode,
1396 ctl->idx - CTL_0,
1397 mixer[i].flush_mask);
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001398
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001399 ctl->ops.setup_blendstage(ctl, mixer[i].hw_lm->idx,
Dhaval Patel572cfd22017-06-12 19:33:39 -07001400 &sde_crtc->stage_cfg);
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001401 }
Lloyd Atkinson8ba47032017-03-22 17:13:32 -04001402
1403 _sde_crtc_program_lm_output_roi(crtc);
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001404}
1405
Clarence Ip0d0e96d2016-10-24 18:13:13 -04001406void sde_crtc_prepare_commit(struct drm_crtc *crtc,
1407 struct drm_crtc_state *old_state)
Clarence Ip24f80662016-06-13 19:05:32 -04001408{
1409 struct sde_crtc *sde_crtc;
Clarence Ip0d0e96d2016-10-24 18:13:13 -04001410 struct sde_crtc_state *cstate;
1411 struct drm_connector *conn;
Clarence Ip24f80662016-06-13 19:05:32 -04001412
Clarence Ip0d0e96d2016-10-24 18:13:13 -04001413 if (!crtc || !crtc->state) {
Clarence Ip24f80662016-06-13 19:05:32 -04001414 SDE_ERROR("invalid crtc\n");
1415 return;
1416 }
1417
1418 sde_crtc = to_sde_crtc(crtc);
Clarence Ip0d0e96d2016-10-24 18:13:13 -04001419 cstate = to_sde_crtc_state(crtc->state);
Dhaval Patel6c666622017-03-21 23:02:59 -07001420 SDE_EVT32_VERBOSE(DRMID(crtc));
Clarence Ip24f80662016-06-13 19:05:32 -04001421
Clarence Ip0d0e96d2016-10-24 18:13:13 -04001422 /* identify connectors attached to this crtc */
Clarence Ip0d0e96d2016-10-24 18:13:13 -04001423 cstate->num_connectors = 0;
1424
1425 drm_for_each_connector(conn, crtc->dev)
1426 if (conn->state && conn->state->crtc == crtc &&
1427 cstate->num_connectors < MAX_CONNECTORS) {
1428 cstate->connectors[cstate->num_connectors++] = conn;
1429 sde_connector_prepare_fence(conn);
Clarence Ip0d0e96d2016-10-24 18:13:13 -04001430 }
1431
1432 /* prepare main output fence */
Clarence Ip24f80662016-06-13 19:05:32 -04001433 sde_fence_prepare(&sde_crtc->output_fence);
1434}
1435
Abhinav Kumarf2e94b52017-02-09 20:27:24 -08001436/**
1437 * _sde_crtc_complete_flip - signal pending page_flip events
1438 * Any pending vblank events are added to the vblank_event_list
1439 * so that the next vblank interrupt shall signal them.
1440 * However PAGE_FLIP events are not handled through the vblank_event_list.
1441 * This API signals any pending PAGE_FLIP events requested through
1442 * DRM_IOCTL_MODE_PAGE_FLIP and are cached in the sde_crtc->event.
1443 * if file!=NULL, this is preclose potential cancel-flip path
1444 * @crtc: Pointer to drm crtc structure
1445 * @file: Pointer to drm file
1446 */
Lloyd Atkinson4f1c8692016-09-14 14:04:25 -04001447static void _sde_crtc_complete_flip(struct drm_crtc *crtc,
1448 struct drm_file *file)
Abhijit Kulkarni40e38162016-06-26 22:12:09 -04001449{
1450 struct sde_crtc *sde_crtc = to_sde_crtc(crtc);
1451 struct drm_device *dev = crtc->dev;
1452 struct drm_pending_vblank_event *event;
1453 unsigned long flags;
1454
1455 spin_lock_irqsave(&dev->event_lock, flags);
1456 event = sde_crtc->event;
1457 if (event) {
1458 /* if regular vblank case (!file) or if cancel-flip from
1459 * preclose on file that requested flip, then send the
1460 * event:
1461 */
1462 if (!file || (event->base.file_priv == file)) {
1463 sde_crtc->event = NULL;
Lloyd Atkinson4f1c8692016-09-14 14:04:25 -04001464 DRM_DEBUG_VBL("%s: send event: %pK\n",
Dhaval Patelec10fad2016-08-22 14:40:48 -07001465 sde_crtc->name, event);
Lloyd Atkinson5d40d312016-09-06 08:34:13 -04001466 SDE_EVT32(DRMID(crtc));
Lloyd Atkinsonac933642016-09-14 11:52:00 -04001467 drm_crtc_send_vblank_event(crtc, event);
Abhijit Kulkarni40e38162016-06-26 22:12:09 -04001468 }
1469 }
1470 spin_unlock_irqrestore(&dev->event_lock, flags);
1471}
1472
Alan Kwong3e985f02017-02-12 15:08:44 -08001473enum sde_intf_mode sde_crtc_get_intf_mode(struct drm_crtc *crtc)
1474{
1475 struct drm_encoder *encoder;
1476
1477 if (!crtc || !crtc->dev) {
1478 SDE_ERROR("invalid crtc\n");
1479 return INTF_MODE_NONE;
1480 }
1481
1482 drm_for_each_encoder(encoder, crtc->dev)
1483 if (encoder->crtc == crtc)
1484 return sde_encoder_get_intf_mode(encoder);
1485
1486 return INTF_MODE_NONE;
1487}
1488
Abhijit Kulkarni40e38162016-06-26 22:12:09 -04001489static void sde_crtc_vblank_cb(void *data)
1490{
1491 struct drm_crtc *crtc = (struct drm_crtc *)data;
Alan Kwong07da0982016-11-04 12:57:45 -04001492 struct sde_crtc *sde_crtc = to_sde_crtc(crtc);
1493
1494 /* keep statistics on vblank callback - with auto reset via debugfs */
1495 if (ktime_equal(sde_crtc->vblank_cb_time, ktime_set(0, 0)))
1496 sde_crtc->vblank_cb_time = ktime_get();
1497 else
1498 sde_crtc->vblank_cb_count++;
Abhinav Kumarf2e94b52017-02-09 20:27:24 -08001499 _sde_crtc_complete_flip(crtc, NULL);
Lloyd Atkinsonac933642016-09-14 11:52:00 -04001500 drm_crtc_handle_vblank(crtc);
Lloyd Atkinson9eabe7a2016-09-14 13:39:15 -04001501 DRM_DEBUG_VBL("crtc%d\n", crtc->base.id);
Dhaval Patel6c666622017-03-21 23:02:59 -07001502 SDE_EVT32_VERBOSE(DRMID(crtc));
Abhijit Kulkarni40e38162016-06-26 22:12:09 -04001503}
1504
Alan Kwong628d19e2016-10-31 13:50:13 -04001505static void sde_crtc_frame_event_work(struct kthread_work *work)
1506{
Alan Kwong67a3f792016-11-01 23:16:53 -04001507 struct msm_drm_private *priv;
Alan Kwong628d19e2016-10-31 13:50:13 -04001508 struct sde_crtc_frame_event *fevent;
1509 struct drm_crtc *crtc;
1510 struct sde_crtc *sde_crtc;
Alan Kwonga1939682017-05-05 11:30:08 -07001511 struct sde_crtc_state *cstate;
Alan Kwong628d19e2016-10-31 13:50:13 -04001512 struct sde_kms *sde_kms;
Veera Sundaram Sankaran2c748e62017-06-13 17:01:48 -07001513 struct drm_encoder *encoder;
Alan Kwong628d19e2016-10-31 13:50:13 -04001514 unsigned long flags;
1515
1516 if (!work) {
1517 SDE_ERROR("invalid work handle\n");
1518 return;
1519 }
1520
1521 fevent = container_of(work, struct sde_crtc_frame_event, work);
Alan Kwonga1939682017-05-05 11:30:08 -07001522 if (!fevent->crtc || !fevent->crtc->state) {
Alan Kwong628d19e2016-10-31 13:50:13 -04001523 SDE_ERROR("invalid crtc\n");
1524 return;
1525 }
1526
1527 crtc = fevent->crtc;
1528 sde_crtc = to_sde_crtc(crtc);
Alan Kwonga1939682017-05-05 11:30:08 -07001529 cstate = to_sde_crtc_state(crtc->state);
Alan Kwong628d19e2016-10-31 13:50:13 -04001530
1531 sde_kms = _sde_crtc_get_kms(crtc);
1532 if (!sde_kms) {
1533 SDE_ERROR("invalid kms handle\n");
1534 return;
1535 }
Alan Kwong67a3f792016-11-01 23:16:53 -04001536 priv = sde_kms->dev->dev_private;
Alan Kwong628d19e2016-10-31 13:50:13 -04001537
1538 SDE_DEBUG("crtc%d event:%u ts:%lld\n", crtc->base.id, fevent->event,
1539 ktime_to_ns(fevent->ts));
1540
1541 if (fevent->event == SDE_ENCODER_FRAME_EVENT_DONE ||
Lloyd Atkinson8c49c582016-11-18 14:23:54 -05001542 (fevent->event & SDE_ENCODER_FRAME_EVENT_ERROR) ||
1543 (fevent->event & SDE_ENCODER_FRAME_EVENT_PANEL_DEAD)) {
Alan Kwong628d19e2016-10-31 13:50:13 -04001544
1545 if (atomic_read(&sde_crtc->frame_pending) < 1) {
1546 /* this should not happen */
1547 SDE_ERROR("crtc%d ts:%lld invalid frame_pending:%d\n",
1548 crtc->base.id,
1549 ktime_to_ns(fevent->ts),
1550 atomic_read(&sde_crtc->frame_pending));
Dhaval Patel6c666622017-03-21 23:02:59 -07001551 SDE_EVT32(DRMID(crtc), fevent->event,
1552 SDE_EVTLOG_FUNC_CASE1);
Alan Kwong628d19e2016-10-31 13:50:13 -04001553 } else if (atomic_dec_return(&sde_crtc->frame_pending) == 0) {
1554 /* release bandwidth and other resources */
1555 SDE_DEBUG("crtc%d ts:%lld last pending\n",
1556 crtc->base.id,
1557 ktime_to_ns(fevent->ts));
Dhaval Patel6c666622017-03-21 23:02:59 -07001558 SDE_EVT32(DRMID(crtc), fevent->event,
1559 SDE_EVTLOG_FUNC_CASE2);
Veera Sundaram Sankaran7ee99092017-06-13 11:19:36 -07001560 sde_core_perf_crtc_release_bw(crtc);
Alan Kwong628d19e2016-10-31 13:50:13 -04001561 } else {
Dhaval Patel6c666622017-03-21 23:02:59 -07001562 SDE_EVT32_VERBOSE(DRMID(crtc), fevent->event,
1563 SDE_EVTLOG_FUNC_CASE3);
Alan Kwong628d19e2016-10-31 13:50:13 -04001564 }
Alan Kwonga1939682017-05-05 11:30:08 -07001565
Veera Sundaram Sankaran7ee99092017-06-13 11:19:36 -07001566 if (fevent->event == SDE_ENCODER_FRAME_EVENT_DONE ||
Veera Sundaram Sankaran2c748e62017-06-13 17:01:48 -07001567 (fevent->event & SDE_ENCODER_FRAME_EVENT_ERROR)) {
1568 bool signal_fence = true;
1569
1570 drm_for_each_encoder(encoder, crtc->dev) {
1571 if (encoder->crtc != crtc)
1572 continue;
1573
1574 signal_fence &=
1575 sde_encoder_is_cmd_mode(encoder);
1576 }
1577
1578 /* signal release fence only for cmd mode panels here */
1579 if (signal_fence) {
1580 sde_fence_signal(&sde_crtc->output_fence, 0);
1581 SDE_EVT32_VERBOSE(DRMID(crtc), fevent->event,
1582 SDE_EVTLOG_FUNC_CASE4);
1583 }
1584
Veera Sundaram Sankaran7ee99092017-06-13 11:19:36 -07001585 complete_all(&sde_crtc->frame_done_comp);
Veera Sundaram Sankaran2c748e62017-06-13 17:01:48 -07001586 }
Veera Sundaram Sankaran7ee99092017-06-13 11:19:36 -07001587
1588 if (fevent->event == SDE_ENCODER_FRAME_EVENT_DONE)
Alan Kwonga1939682017-05-05 11:30:08 -07001589 sde_core_perf_crtc_update(crtc, 0, false);
Alan Kwong628d19e2016-10-31 13:50:13 -04001590 } else {
1591 SDE_ERROR("crtc%d ts:%lld unknown event %u\n", crtc->base.id,
1592 ktime_to_ns(fevent->ts),
1593 fevent->event);
Dhaval Patel6c666622017-03-21 23:02:59 -07001594 SDE_EVT32(DRMID(crtc), fevent->event, SDE_EVTLOG_FUNC_CASE4);
Alan Kwong628d19e2016-10-31 13:50:13 -04001595 }
1596
Lloyd Atkinson8c49c582016-11-18 14:23:54 -05001597 if (fevent->event & SDE_ENCODER_FRAME_EVENT_PANEL_DEAD)
1598 SDE_ERROR("crtc%d ts:%lld received panel dead event\n",
1599 crtc->base.id, ktime_to_ns(fevent->ts));
1600
Alan Kwong628d19e2016-10-31 13:50:13 -04001601 spin_lock_irqsave(&sde_crtc->spin_lock, flags);
1602 list_add_tail(&fevent->list, &sde_crtc->frame_event_list);
1603 spin_unlock_irqrestore(&sde_crtc->spin_lock, flags);
1604}
1605
1606static void sde_crtc_frame_event_cb(void *data, u32 event)
1607{
1608 struct drm_crtc *crtc = (struct drm_crtc *)data;
1609 struct sde_crtc *sde_crtc;
1610 struct msm_drm_private *priv;
Alan Kwong628d19e2016-10-31 13:50:13 -04001611 struct sde_crtc_frame_event *fevent;
1612 unsigned long flags;
Veera Sundaram Sankaran10ea2bd2017-06-14 14:10:57 -07001613 u32 crtc_id;
Alan Kwong628d19e2016-10-31 13:50:13 -04001614
1615 if (!crtc || !crtc->dev || !crtc->dev->dev_private) {
1616 SDE_ERROR("invalid parameters\n");
1617 return;
1618 }
1619 sde_crtc = to_sde_crtc(crtc);
1620 priv = crtc->dev->dev_private;
Veera Sundaram Sankaran10ea2bd2017-06-14 14:10:57 -07001621 crtc_id = drm_crtc_index(crtc);
Alan Kwong628d19e2016-10-31 13:50:13 -04001622
1623 SDE_DEBUG("crtc%d\n", crtc->base.id);
Ingrid Gallardo79b44392017-05-30 16:30:52 -07001624 SDE_EVT32_VERBOSE(DRMID(crtc), event);
Alan Kwong628d19e2016-10-31 13:50:13 -04001625
1626 spin_lock_irqsave(&sde_crtc->spin_lock, flags);
Lloyd Atkinson78831f82016-12-09 11:24:56 -05001627 fevent = list_first_entry_or_null(&sde_crtc->frame_event_list,
1628 struct sde_crtc_frame_event, list);
1629 if (fevent)
1630 list_del_init(&fevent->list);
Alan Kwong628d19e2016-10-31 13:50:13 -04001631 spin_unlock_irqrestore(&sde_crtc->spin_lock, flags);
1632
Lloyd Atkinson78831f82016-12-09 11:24:56 -05001633 if (!fevent) {
Alan Kwong628d19e2016-10-31 13:50:13 -04001634 SDE_ERROR("crtc%d event %d overflow\n",
1635 crtc->base.id, event);
1636 SDE_EVT32(DRMID(crtc), event);
1637 return;
1638 }
1639
Alan Kwong628d19e2016-10-31 13:50:13 -04001640 fevent->event = event;
1641 fevent->crtc = crtc;
1642 fevent->ts = ktime_get();
Veera Sundaram Sankaran10ea2bd2017-06-14 14:10:57 -07001643 kthread_queue_work(&priv->event_thread[crtc_id].worker, &fevent->work);
Alan Kwong628d19e2016-10-31 13:50:13 -04001644}
1645
Clarence Ip0d0e96d2016-10-24 18:13:13 -04001646void sde_crtc_complete_commit(struct drm_crtc *crtc,
1647 struct drm_crtc_state *old_state)
Clarence Ip24f80662016-06-13 19:05:32 -04001648{
Clarence Ip0d0e96d2016-10-24 18:13:13 -04001649 struct sde_crtc *sde_crtc;
1650 struct sde_crtc_state *cstate;
Veera Sundaram Sankaran2c748e62017-06-13 17:01:48 -07001651 struct drm_encoder *encoder;
Clarence Ip0d0e96d2016-10-24 18:13:13 -04001652 int i;
Veera Sundaram Sankaran2c748e62017-06-13 17:01:48 -07001653 bool signal_fence = true;
Clarence Ip0d0e96d2016-10-24 18:13:13 -04001654
1655 if (!crtc || !crtc->state) {
Clarence Ip24f80662016-06-13 19:05:32 -04001656 SDE_ERROR("invalid crtc\n");
1657 return;
1658 }
1659
Clarence Ip0d0e96d2016-10-24 18:13:13 -04001660 sde_crtc = to_sde_crtc(crtc);
1661 cstate = to_sde_crtc_state(crtc->state);
Dhaval Patel6c666622017-03-21 23:02:59 -07001662 SDE_EVT32_VERBOSE(DRMID(crtc));
Clarence Ip0d0e96d2016-10-24 18:13:13 -04001663
Veera Sundaram Sankaran2c748e62017-06-13 17:01:48 -07001664 drm_for_each_encoder(encoder, crtc->dev) {
1665 if (encoder->crtc != crtc)
1666 continue;
Clarence Ip0d0e96d2016-10-24 18:13:13 -04001667
Veera Sundaram Sankaran2c748e62017-06-13 17:01:48 -07001668 signal_fence &= !sde_encoder_is_cmd_mode(encoder);
1669 }
1670
1671 /* signal release fence for non-cmd mode panels */
1672 if (signal_fence)
1673 sde_fence_signal(&sde_crtc->output_fence, 0);
1674
1675 /* signal retire fence */
Clarence Ip0d0e96d2016-10-24 18:13:13 -04001676 for (i = 0; i < cstate->num_connectors; ++i)
1677 sde_connector_complete_commit(cstate->connectors[i]);
Clarence Ip24f80662016-06-13 19:05:32 -04001678}
1679
Lloyd Atkinson5d722782016-05-30 14:09:41 -04001680/**
Clarence Ipcae1bb62016-07-07 12:07:13 -04001681 * _sde_crtc_set_input_fence_timeout - update ns version of in fence timeout
1682 * @cstate: Pointer to sde crtc state
1683 */
1684static void _sde_crtc_set_input_fence_timeout(struct sde_crtc_state *cstate)
1685{
1686 if (!cstate) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07001687 SDE_ERROR("invalid cstate\n");
Clarence Ipcae1bb62016-07-07 12:07:13 -04001688 return;
1689 }
1690 cstate->input_fence_timeout_ns =
1691 sde_crtc_get_property(cstate, CRTC_PROP_INPUT_FENCE_TIMEOUT);
1692 cstate->input_fence_timeout_ns *= NSEC_PER_MSEC;
1693}
1694
1695/**
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08001696 * _sde_crtc_set_dim_layer_v1 - copy dim layer settings from userspace
1697 * @cstate: Pointer to sde crtc state
1698 * @user_ptr: User ptr for sde_drm_dim_layer_v1 struct
1699 */
1700static void _sde_crtc_set_dim_layer_v1(struct sde_crtc_state *cstate,
1701 void *usr_ptr)
1702{
1703 struct sde_drm_dim_layer_v1 dim_layer_v1;
1704 struct sde_drm_dim_layer_cfg *user_cfg;
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -07001705 struct sde_hw_dim_layer *dim_layer;
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08001706 u32 count, i;
1707
1708 if (!cstate) {
1709 SDE_ERROR("invalid cstate\n");
1710 return;
1711 }
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -07001712 dim_layer = cstate->dim_layer;
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08001713
1714 if (!usr_ptr) {
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -07001715 SDE_DEBUG("dim_layer data removed\n");
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08001716 return;
1717 }
1718
1719 if (copy_from_user(&dim_layer_v1, usr_ptr, sizeof(dim_layer_v1))) {
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -07001720 SDE_ERROR("failed to copy dim_layer data\n");
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08001721 return;
1722 }
1723
1724 count = dim_layer_v1.num_layers;
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -07001725 if (count > SDE_MAX_DIM_LAYERS) {
1726 SDE_ERROR("invalid number of dim_layers:%d", count);
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08001727 return;
1728 }
1729
1730 /* populate from user space */
1731 cstate->num_dim_layers = count;
1732 for (i = 0; i < count; i++) {
1733 user_cfg = &dim_layer_v1.layer_cfg[i];
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08001734
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -07001735 dim_layer[i].flags = user_cfg->flags;
1736 dim_layer[i].stage = user_cfg->stage + SDE_STAGE_0;
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08001737
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -07001738 dim_layer[i].rect.x = user_cfg->rect.x1;
1739 dim_layer[i].rect.y = user_cfg->rect.y1;
1740 dim_layer[i].rect.w = user_cfg->rect.x2 - user_cfg->rect.x1;
1741 dim_layer[i].rect.h = user_cfg->rect.y2 - user_cfg->rect.y1;
1742
1743 dim_layer[i].color_fill = (struct sde_mdss_color) {
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08001744 user_cfg->color_fill.color_0,
1745 user_cfg->color_fill.color_1,
1746 user_cfg->color_fill.color_2,
1747 user_cfg->color_fill.color_3,
1748 };
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -07001749
1750 SDE_DEBUG("dim_layer[%d] - flags:%d, stage:%d\n",
1751 i, dim_layer[i].flags, dim_layer[i].stage);
1752 SDE_DEBUG(" rect:{%d,%d,%d,%d}, color:{%d,%d,%d,%d}\n",
1753 dim_layer[i].rect.x, dim_layer[i].rect.y,
1754 dim_layer[i].rect.w, dim_layer[i].rect.h,
1755 dim_layer[i].color_fill.color_0,
1756 dim_layer[i].color_fill.color_1,
1757 dim_layer[i].color_fill.color_2,
1758 dim_layer[i].color_fill.color_3);
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08001759 }
1760}
1761
1762/**
Clarence Ipcae1bb62016-07-07 12:07:13 -04001763 * _sde_crtc_wait_for_fences - wait for incoming framebuffer sync fences
1764 * @crtc: Pointer to CRTC object
1765 */
1766static void _sde_crtc_wait_for_fences(struct drm_crtc *crtc)
1767{
1768 struct drm_plane *plane = NULL;
1769 uint32_t wait_ms = 1;
Clarence Ip8dedc232016-09-09 16:41:00 -04001770 ktime_t kt_end, kt_wait;
Dhaval Patel39323d42017-03-01 23:48:24 -08001771 int rc = 0;
Clarence Ipcae1bb62016-07-07 12:07:13 -04001772
Lloyd Atkinson4f1c8692016-09-14 14:04:25 -04001773 SDE_DEBUG("\n");
Clarence Ipcae1bb62016-07-07 12:07:13 -04001774
1775 if (!crtc || !crtc->state) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07001776 SDE_ERROR("invalid crtc/state %pK\n", crtc);
Clarence Ipcae1bb62016-07-07 12:07:13 -04001777 return;
1778 }
1779
1780 /* use monotonic timer to limit total fence wait time */
Clarence Ip8dedc232016-09-09 16:41:00 -04001781 kt_end = ktime_add_ns(ktime_get(),
1782 to_sde_crtc_state(crtc->state)->input_fence_timeout_ns);
Clarence Ipcae1bb62016-07-07 12:07:13 -04001783
1784 /*
1785 * Wait for fences sequentially, as all of them need to be signalled
1786 * before we can proceed.
1787 *
1788 * Limit total wait time to INPUT_FENCE_TIMEOUT, but still call
1789 * sde_plane_wait_input_fence with wait_ms == 0 after the timeout so
1790 * that each plane can check its fence status and react appropriately
Dhaval Patel39323d42017-03-01 23:48:24 -08001791 * if its fence has timed out. Call input fence wait multiple times if
1792 * fence wait is interrupted due to interrupt call.
Clarence Ipcae1bb62016-07-07 12:07:13 -04001793 */
Narendra Muppalla77b32932017-05-10 13:53:11 -07001794 SDE_ATRACE_BEGIN("plane_wait_input_fence");
Clarence Ipcae1bb62016-07-07 12:07:13 -04001795 drm_atomic_crtc_for_each_plane(plane, crtc) {
Dhaval Patel39323d42017-03-01 23:48:24 -08001796 do {
Clarence Ip8dedc232016-09-09 16:41:00 -04001797 kt_wait = ktime_sub(kt_end, ktime_get());
1798 if (ktime_compare(kt_wait, ktime_set(0, 0)) >= 0)
1799 wait_ms = ktime_to_ms(kt_wait);
Clarence Ipcae1bb62016-07-07 12:07:13 -04001800 else
1801 wait_ms = 0;
Dhaval Patel39323d42017-03-01 23:48:24 -08001802
1803 rc = sde_plane_wait_input_fence(plane, wait_ms);
1804 } while (wait_ms && rc == -ERESTARTSYS);
Clarence Ipcae1bb62016-07-07 12:07:13 -04001805 }
Narendra Muppalla77b32932017-05-10 13:53:11 -07001806 SDE_ATRACE_END("plane_wait_input_fence");
Clarence Ipcae1bb62016-07-07 12:07:13 -04001807}
1808
Lloyd Atkinson11f34442016-08-11 11:19:52 -04001809static void _sde_crtc_setup_mixer_for_encoder(
1810 struct drm_crtc *crtc,
1811 struct drm_encoder *enc)
1812{
1813 struct sde_crtc *sde_crtc = to_sde_crtc(crtc);
Lloyd Atkinson4f1c8692016-09-14 14:04:25 -04001814 struct sde_kms *sde_kms = _sde_crtc_get_kms(crtc);
Lloyd Atkinson11f34442016-08-11 11:19:52 -04001815 struct sde_rm *rm = &sde_kms->rm;
1816 struct sde_crtc_mixer *mixer;
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04001817 struct sde_hw_ctl *last_valid_ctl = NULL;
Lloyd Atkinson11f34442016-08-11 11:19:52 -04001818 int i;
Gopikrishnaiah Anandane0e5e0c2016-05-25 11:05:33 -07001819 struct sde_rm_hw_iter lm_iter, ctl_iter, dspp_iter;
Lloyd Atkinson11f34442016-08-11 11:19:52 -04001820
1821 sde_rm_init_hw_iter(&lm_iter, enc->base.id, SDE_HW_BLK_LM);
1822 sde_rm_init_hw_iter(&ctl_iter, enc->base.id, SDE_HW_BLK_CTL);
Gopikrishnaiah Anandane0e5e0c2016-05-25 11:05:33 -07001823 sde_rm_init_hw_iter(&dspp_iter, enc->base.id, SDE_HW_BLK_DSPP);
Lloyd Atkinson11f34442016-08-11 11:19:52 -04001824
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04001825 /* Set up all the mixers and ctls reserved by this encoder */
Lloyd Atkinson11f34442016-08-11 11:19:52 -04001826 for (i = sde_crtc->num_mixers; i < ARRAY_SIZE(sde_crtc->mixers); i++) {
1827 mixer = &sde_crtc->mixers[i];
1828
Lloyd Atkinson11f34442016-08-11 11:19:52 -04001829 if (!sde_rm_get_hw(rm, &lm_iter))
1830 break;
1831 mixer->hw_lm = (struct sde_hw_mixer *)lm_iter.hw;
1832
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04001833 /* CTL may be <= LMs, if <, multiple LMs controlled by 1 CTL */
1834 if (!sde_rm_get_hw(rm, &ctl_iter)) {
1835 SDE_DEBUG("no ctl assigned to lm %d, using previous\n",
Clarence Ip8e69ad02016-12-09 09:43:57 -05001836 mixer->hw_lm->idx - LM_0);
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04001837 mixer->hw_ctl = last_valid_ctl;
1838 } else {
1839 mixer->hw_ctl = (struct sde_hw_ctl *)ctl_iter.hw;
1840 last_valid_ctl = mixer->hw_ctl;
1841 }
Lloyd Atkinson11f34442016-08-11 11:19:52 -04001842
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04001843 /* Shouldn't happen, mixers are always >= ctls */
1844 if (!mixer->hw_ctl) {
1845 SDE_ERROR("no valid ctls found for lm %d\n",
Clarence Ip8e69ad02016-12-09 09:43:57 -05001846 mixer->hw_lm->idx - LM_0);
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04001847 return;
1848 }
1849
Gopikrishnaiah Anandane0e5e0c2016-05-25 11:05:33 -07001850 /* Dspp may be null */
1851 (void) sde_rm_get_hw(rm, &dspp_iter);
1852 mixer->hw_dspp = (struct sde_hw_dspp *)dspp_iter.hw;
1853
Lloyd Atkinson11f34442016-08-11 11:19:52 -04001854 mixer->encoder = enc;
1855
1856 sde_crtc->num_mixers++;
Clarence Ipd9f9fa62016-09-09 13:42:32 -04001857 SDE_DEBUG("setup mixer %d: lm %d\n",
1858 i, mixer->hw_lm->idx - LM_0);
1859 SDE_DEBUG("setup mixer %d: ctl %d\n",
1860 i, mixer->hw_ctl->idx - CTL_0);
Lloyd Atkinson11f34442016-08-11 11:19:52 -04001861 }
1862}
1863
1864static void _sde_crtc_setup_mixers(struct drm_crtc *crtc)
1865{
1866 struct sde_crtc *sde_crtc = to_sde_crtc(crtc);
1867 struct drm_encoder *enc;
1868
Lloyd Atkinson11f34442016-08-11 11:19:52 -04001869 sde_crtc->num_mixers = 0;
1870 memset(sde_crtc->mixers, 0, sizeof(sde_crtc->mixers));
1871
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07001872 mutex_lock(&sde_crtc->crtc_lock);
Lloyd Atkinson11f34442016-08-11 11:19:52 -04001873 /* Check for mixers on all encoders attached to this crtc */
1874 list_for_each_entry(enc, &crtc->dev->mode_config.encoder_list, head) {
1875 if (enc->crtc != crtc)
1876 continue;
1877
1878 _sde_crtc_setup_mixer_for_encoder(crtc, enc);
1879 }
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05001880
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07001881 mutex_unlock(&sde_crtc->crtc_lock);
Lloyd Atkinson11f34442016-08-11 11:19:52 -04001882}
1883
Lloyd Atkinson66e7dde2017-02-08 15:52:53 -05001884static void _sde_crtc_setup_is_ppsplit(struct drm_crtc_state *state)
1885{
1886 int i;
1887 struct sde_crtc_state *cstate;
1888
1889 cstate = to_sde_crtc_state(state);
1890
1891 cstate->is_ppsplit = false;
1892 for (i = 0; i < cstate->num_connectors; i++) {
1893 struct drm_connector *conn = cstate->connectors[i];
1894
1895 if (sde_connector_get_topology_name(conn) ==
1896 SDE_RM_TOPOLOGY_PPSPLIT)
1897 cstate->is_ppsplit = true;
1898 }
1899}
1900
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05001901static void _sde_crtc_setup_lm_bounds(struct drm_crtc *crtc,
1902 struct drm_crtc_state *state)
1903{
1904 struct sde_crtc *sde_crtc;
1905 struct sde_crtc_state *cstate;
1906 struct drm_display_mode *adj_mode;
1907 u32 crtc_split_width;
1908 int i;
1909
1910 if (!crtc || !state) {
1911 SDE_ERROR("invalid args\n");
1912 return;
1913 }
1914
1915 sde_crtc = to_sde_crtc(crtc);
1916 cstate = to_sde_crtc_state(state);
1917
1918 adj_mode = &state->adjusted_mode;
1919 crtc_split_width = sde_crtc_mixer_width(sde_crtc, adj_mode);
1920
1921 for (i = 0; i < sde_crtc->num_mixers; i++) {
Lloyd Atkinson8ba47032017-03-22 17:13:32 -04001922 cstate->lm_bounds[i].x = crtc_split_width * i;
1923 cstate->lm_bounds[i].y = 0;
1924 cstate->lm_bounds[i].w = crtc_split_width;
1925 cstate->lm_bounds[i].h = adj_mode->vdisplay;
1926 memcpy(&cstate->lm_roi[i], &cstate->lm_bounds[i],
1927 sizeof(cstate->lm_roi[i]));
1928 SDE_EVT32(DRMID(crtc), i,
1929 cstate->lm_bounds[i].x, cstate->lm_bounds[i].y,
1930 cstate->lm_bounds[i].w, cstate->lm_bounds[i].h);
1931 SDE_DEBUG("%s: lm%d bnd&roi (%d,%d,%d,%d)\n", sde_crtc->name, i,
1932 cstate->lm_roi[i].x, cstate->lm_roi[i].y,
1933 cstate->lm_roi[i].w, cstate->lm_roi[i].h);
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05001934 }
1935
1936 drm_mode_debug_printmodeline(adj_mode);
1937}
1938
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001939static void sde_crtc_atomic_begin(struct drm_crtc *crtc,
Clarence Ip0d0e96d2016-10-24 18:13:13 -04001940 struct drm_crtc_state *old_state)
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07001941{
Clarence Ipcae1bb62016-07-07 12:07:13 -04001942 struct sde_crtc *sde_crtc;
Dhaval Patel0e558f42017-04-30 00:51:40 -07001943 struct drm_encoder *encoder;
Clarence Ipcae1bb62016-07-07 12:07:13 -04001944 struct drm_device *dev;
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001945 unsigned long flags;
1946
Clarence Ipcae1bb62016-07-07 12:07:13 -04001947 if (!crtc) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07001948 SDE_ERROR("invalid crtc\n");
Clarence Ipcae1bb62016-07-07 12:07:13 -04001949 return;
1950 }
1951
Alan Kwong163d2612016-11-03 00:56:56 -04001952 if (!crtc->state->enable) {
1953 SDE_DEBUG("crtc%d -> enable %d, skip atomic_begin\n",
1954 crtc->base.id, crtc->state->enable);
1955 return;
1956 }
1957
1958 SDE_DEBUG("crtc%d\n", crtc->base.id);
1959
Clarence Ipcae1bb62016-07-07 12:07:13 -04001960 sde_crtc = to_sde_crtc(crtc);
1961 dev = crtc->dev;
1962
Lloyd Atkinson8ba47032017-03-22 17:13:32 -04001963 if (!sde_crtc->num_mixers) {
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04001964 _sde_crtc_setup_mixers(crtc);
Lloyd Atkinson66e7dde2017-02-08 15:52:53 -05001965 _sde_crtc_setup_is_ppsplit(crtc->state);
Lloyd Atkinson8ba47032017-03-22 17:13:32 -04001966 _sde_crtc_setup_lm_bounds(crtc, crtc->state);
1967 }
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05001968
Lloyd Atkinson265d2212016-05-30 13:12:01 -04001969 if (sde_crtc->event) {
1970 WARN_ON(sde_crtc->event);
1971 } else {
1972 spin_lock_irqsave(&dev->event_lock, flags);
1973 sde_crtc->event = crtc->state->event;
1974 spin_unlock_irqrestore(&dev->event_lock, flags);
1975 }
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001976
Dhaval Patel0e558f42017-04-30 00:51:40 -07001977 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
1978 if (encoder->crtc != crtc)
1979 continue;
Lloyd Atkinson5d722782016-05-30 14:09:41 -04001980
Dhaval Patel0e558f42017-04-30 00:51:40 -07001981 /* encoder will trigger pending mask now */
1982 sde_encoder_trigger_kickoff_pending(encoder);
Lloyd Atkinson5d722782016-05-30 14:09:41 -04001983 }
1984
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001985 /*
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04001986 * If no mixers have been allocated in sde_crtc_atomic_check(),
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001987 * it means we are trying to flush a CRTC whose state is disabled:
1988 * nothing else needs to be done.
1989 */
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04001990 if (unlikely(!sde_crtc->num_mixers))
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001991 return;
1992
Clarence Ipd9f9fa62016-09-09 13:42:32 -04001993 _sde_crtc_blend_setup(crtc);
Gopikrishnaiah Anandane0e5e0c2016-05-25 11:05:33 -07001994 sde_cp_crtc_apply_properties(crtc);
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001995
1996 /*
1997 * PP_DONE irq is only used by command mode for now.
1998 * It is better to request pending before FLUSH and START trigger
1999 * to make sure no pp_done irq missed.
2000 * This is safe because no pp_done will happen before SW trigger
2001 * in command mode.
2002 */
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07002003}
2004
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002005static void sde_crtc_atomic_flush(struct drm_crtc *crtc,
2006 struct drm_crtc_state *old_crtc_state)
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07002007{
Dhaval Patel82c8dbc2017-02-18 23:15:10 -08002008 struct drm_encoder *encoder;
Clarence Ipcae1bb62016-07-07 12:07:13 -04002009 struct sde_crtc *sde_crtc;
2010 struct drm_device *dev;
Lloyd Atkinson265d2212016-05-30 13:12:01 -04002011 struct drm_plane *plane;
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002012 unsigned long flags;
Dhaval Patel82c8dbc2017-02-18 23:15:10 -08002013 struct sde_crtc_state *cstate;
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07002014
Clarence Ipcae1bb62016-07-07 12:07:13 -04002015 if (!crtc) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07002016 SDE_ERROR("invalid crtc\n");
Clarence Ipcae1bb62016-07-07 12:07:13 -04002017 return;
2018 }
2019
Alan Kwong163d2612016-11-03 00:56:56 -04002020 if (!crtc->state->enable) {
2021 SDE_DEBUG("crtc%d -> enable %d, skip atomic_flush\n",
2022 crtc->base.id, crtc->state->enable);
2023 return;
2024 }
2025
2026 SDE_DEBUG("crtc%d\n", crtc->base.id);
Clarence Ipcae1bb62016-07-07 12:07:13 -04002027
2028 sde_crtc = to_sde_crtc(crtc);
Dhaval Patel82c8dbc2017-02-18 23:15:10 -08002029 cstate = to_sde_crtc_state(crtc->state);
Clarence Ipcae1bb62016-07-07 12:07:13 -04002030 dev = crtc->dev;
2031
Lloyd Atkinson265d2212016-05-30 13:12:01 -04002032 if (sde_crtc->event) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07002033 SDE_DEBUG("already received sde_crtc->event\n");
Lloyd Atkinson265d2212016-05-30 13:12:01 -04002034 } else {
Lloyd Atkinson265d2212016-05-30 13:12:01 -04002035 spin_lock_irqsave(&dev->event_lock, flags);
2036 sde_crtc->event = crtc->state->event;
2037 spin_unlock_irqrestore(&dev->event_lock, flags);
2038 }
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002039
2040 /*
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04002041 * If no mixers has been allocated in sde_crtc_atomic_check(),
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002042 * it means we are trying to flush a CRTC whose state is disabled:
2043 * nothing else needs to be done.
2044 */
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04002045 if (unlikely(!sde_crtc->num_mixers))
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002046 return;
2047
Clarence Ipcae1bb62016-07-07 12:07:13 -04002048 /* wait for acquire fences before anything else is done */
2049 _sde_crtc_wait_for_fences(crtc);
2050
Dhaval Patel82c8dbc2017-02-18 23:15:10 -08002051 if (!cstate->rsc_update) {
2052 drm_for_each_encoder(encoder, dev) {
2053 if (encoder->crtc != crtc)
2054 continue;
2055
2056 cstate->rsc_client =
Dhaval Patel30fae8a2017-04-21 18:42:41 -07002057 sde_encoder_get_rsc_client(encoder);
Dhaval Patel82c8dbc2017-02-18 23:15:10 -08002058 }
2059 cstate->rsc_update = true;
2060 }
2061
Alan Kwong9aa061c2016-11-06 21:17:12 -05002062 /* update performance setting before crtc kickoff */
2063 sde_core_perf_crtc_update(crtc, 1, false);
2064
Clarence Ipcae1bb62016-07-07 12:07:13 -04002065 /*
2066 * Final plane updates: Give each plane a chance to complete all
2067 * required writes/flushing before crtc's "flush
2068 * everything" call below.
2069 */
2070 drm_atomic_crtc_for_each_plane(plane, crtc)
2071 sde_plane_flush(plane);
2072
Lloyd Atkinson5d722782016-05-30 14:09:41 -04002073 /* Kickoff will be scheduled by outer layer */
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07002074}
2075
Clarence Ip7a753bb2016-07-07 11:47:44 -04002076/**
2077 * sde_crtc_destroy_state - state destroy hook
2078 * @crtc: drm CRTC
2079 * @state: CRTC state object to release
2080 */
2081static void sde_crtc_destroy_state(struct drm_crtc *crtc,
2082 struct drm_crtc_state *state)
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07002083{
Clarence Ip7a753bb2016-07-07 11:47:44 -04002084 struct sde_crtc *sde_crtc;
2085 struct sde_crtc_state *cstate;
2086
2087 if (!crtc || !state) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07002088 SDE_ERROR("invalid argument(s)\n");
Clarence Ip7a753bb2016-07-07 11:47:44 -04002089 return;
2090 }
2091
2092 sde_crtc = to_sde_crtc(crtc);
2093 cstate = to_sde_crtc_state(state);
2094
Alan Kwong163d2612016-11-03 00:56:56 -04002095 SDE_DEBUG("crtc%d\n", crtc->base.id);
Clarence Ip7a753bb2016-07-07 11:47:44 -04002096
Alan Kwongcdb2f282017-03-18 13:42:06 -07002097 _sde_crtc_rp_destroy(&cstate->rp);
2098
Dhaval Patel04c7e8e2016-09-26 20:14:31 -07002099 __drm_atomic_helper_crtc_destroy_state(state);
Clarence Ip7a753bb2016-07-07 11:47:44 -04002100
2101 /* destroy value helper */
2102 msm_property_destroy_state(&sde_crtc->property_info, cstate,
2103 cstate->property_values, cstate->property_blobs);
2104}
2105
Veera Sundaram Sankaran7ee99092017-06-13 11:19:36 -07002106static int _sde_crtc_wait_for_frame_done(struct drm_crtc *crtc)
2107{
2108 struct sde_crtc *sde_crtc;
2109 int ret, rc = 0;
2110
2111 if (!crtc) {
2112 SDE_ERROR("invalid argument\n");
2113 return -EINVAL;
2114 }
2115 sde_crtc = to_sde_crtc(crtc);
2116
2117 if (!atomic_read(&sde_crtc->frame_pending)) {
2118 SDE_DEBUG("no frames pending\n");
2119 return 0;
2120 }
2121
2122 SDE_EVT32(DRMID(crtc), SDE_EVTLOG_FUNC_ENTRY);
2123 ret = wait_for_completion_timeout(&sde_crtc->frame_done_comp,
2124 msecs_to_jiffies(SDE_FRAME_DONE_TIMEOUT));
2125 if (!ret) {
2126 SDE_ERROR("frame done completion wait timed out, ret:%d\n",
2127 ret);
2128 SDE_EVT32(DRMID(crtc), SDE_EVTLOG_FATAL);
2129 rc = -ETIMEDOUT;
2130 }
2131 SDE_EVT32(DRMID(crtc), SDE_EVTLOG_FUNC_EXIT);
2132
2133 return rc;
2134}
2135
Lloyd Atkinson5d722782016-05-30 14:09:41 -04002136void sde_crtc_commit_kickoff(struct drm_crtc *crtc)
2137{
Lloyd Atkinsone7bcdd22016-08-11 10:53:37 -04002138 struct drm_encoder *encoder;
2139 struct drm_device *dev;
Alan Kwong628d19e2016-10-31 13:50:13 -04002140 struct sde_crtc *sde_crtc;
Alan Kwong67a3f792016-11-01 23:16:53 -04002141 struct msm_drm_private *priv;
2142 struct sde_kms *sde_kms;
Alan Kwong4aacd532017-02-04 18:51:33 -08002143 struct sde_crtc_state *cstate;
Lloyd Atkinson5d722782016-05-30 14:09:41 -04002144
2145 if (!crtc) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07002146 SDE_ERROR("invalid argument\n");
Lloyd Atkinson5d722782016-05-30 14:09:41 -04002147 return;
2148 }
Lloyd Atkinsone7bcdd22016-08-11 10:53:37 -04002149 dev = crtc->dev;
Alan Kwong628d19e2016-10-31 13:50:13 -04002150 sde_crtc = to_sde_crtc(crtc);
Alan Kwong67a3f792016-11-01 23:16:53 -04002151 sde_kms = _sde_crtc_get_kms(crtc);
2152 priv = sde_kms->dev->dev_private;
Alan Kwong4aacd532017-02-04 18:51:33 -08002153 cstate = to_sde_crtc_state(crtc->state);
Lloyd Atkinson5d722782016-05-30 14:09:41 -04002154
Clarence Ip90b282d2017-05-04 10:00:32 -07002155 /*
2156 * If no mixers has been allocated in sde_crtc_atomic_check(),
2157 * it means we are trying to start a CRTC whose state is disabled:
2158 * nothing else needs to be done.
2159 */
2160 if (unlikely(!sde_crtc->num_mixers))
2161 return;
2162
Narendra Muppalla77b32932017-05-10 13:53:11 -07002163 SDE_ATRACE_BEGIN("crtc_commit");
Lloyd Atkinsone7bcdd22016-08-11 10:53:37 -04002164 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
Alan Kwong4aacd532017-02-04 18:51:33 -08002165 struct sde_encoder_kickoff_params params = { 0 };
2166
Lloyd Atkinsone7bcdd22016-08-11 10:53:37 -04002167 if (encoder->crtc != crtc)
2168 continue;
2169
2170 /*
2171 * Encoder will flush/start now, unless it has a tx pending.
2172 * If so, it may delay and flush at an irq event (e.g. ppdone)
2173 */
Alan Kwong4aacd532017-02-04 18:51:33 -08002174 params.inline_rotate_prefill = cstate->sbuf_prefill_line;
Lloyd Atkinson73fb8092017-02-08 16:02:55 -05002175 params.affected_displays = _sde_crtc_get_displays_affected(crtc,
2176 crtc->state);
Alan Kwong4aacd532017-02-04 18:51:33 -08002177 sde_encoder_prepare_for_kickoff(encoder, &params);
Alan Kwong628d19e2016-10-31 13:50:13 -04002178 }
2179
Veera Sundaram Sankaran7ee99092017-06-13 11:19:36 -07002180 /* wait for frame_event_done completion */
2181 if (_sde_crtc_wait_for_frame_done(crtc)) {
2182 SDE_ERROR("crtc%d wait for frame done failed;frame_pending%d\n",
2183 crtc->base.id,
2184 atomic_read(&sde_crtc->frame_pending));
Narendra Muppalla77b32932017-05-10 13:53:11 -07002185 goto end;
Veera Sundaram Sankaran7ee99092017-06-13 11:19:36 -07002186 }
2187
2188 if (atomic_inc_return(&sde_crtc->frame_pending) == 1) {
Alan Kwong628d19e2016-10-31 13:50:13 -04002189 /* acquire bandwidth and other resources */
2190 SDE_DEBUG("crtc%d first commit\n", crtc->base.id);
Veera Sundaram Sankaran7ee99092017-06-13 11:19:36 -07002191 SDE_EVT32(DRMID(crtc), SDE_EVTLOG_FUNC_CASE1);
Alan Kwong628d19e2016-10-31 13:50:13 -04002192 } else {
2193 SDE_DEBUG("crtc%d commit\n", crtc->base.id);
Veera Sundaram Sankaran7ee99092017-06-13 11:19:36 -07002194 SDE_EVT32(DRMID(crtc), SDE_EVTLOG_FUNC_CASE2);
Alan Kwong628d19e2016-10-31 13:50:13 -04002195 }
Dhaval Pateld67cf4a2017-06-14 18:08:32 -07002196 sde_crtc->play_count++;
Alan Kwong628d19e2016-10-31 13:50:13 -04002197
2198 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
2199 if (encoder->crtc != crtc)
2200 continue;
2201
2202 sde_encoder_kickoff(encoder);
Lloyd Atkinsone7bcdd22016-08-11 10:53:37 -04002203 }
Veera Sundaram Sankaran7ee99092017-06-13 11:19:36 -07002204
2205 reinit_completion(&sde_crtc->frame_done_comp);
2206
Narendra Muppalla77b32932017-05-10 13:53:11 -07002207end:
2208 SDE_ATRACE_END("crtc_commit");
2209 return;
Lloyd Atkinson5d722782016-05-30 14:09:41 -04002210}
2211
Clarence Ip7a753bb2016-07-07 11:47:44 -04002212/**
Clarence Ip7f70ce42017-03-20 06:53:46 -07002213 * _sde_crtc_vblank_enable_nolock - update power resource and vblank request
2214 * @sde_crtc: Pointer to sde crtc structure
2215 * @enable: Whether to enable/disable vblanks
2216 */
2217static void _sde_crtc_vblank_enable_nolock(
2218 struct sde_crtc *sde_crtc, bool enable)
2219{
2220 struct drm_device *dev;
2221 struct drm_crtc *crtc;
2222 struct drm_encoder *enc;
Clarence Ip7f70ce42017-03-20 06:53:46 -07002223
2224 if (!sde_crtc) {
2225 SDE_ERROR("invalid crtc\n");
2226 return;
2227 }
2228
2229 crtc = &sde_crtc->base;
2230 dev = crtc->dev;
Clarence Ip7f70ce42017-03-20 06:53:46 -07002231
2232 if (enable) {
Lloyd Atkinson2c554eb2017-05-24 16:22:39 -04002233 int ret;
2234
2235 /* drop lock since power crtc cb may try to re-acquire lock */
2236 mutex_unlock(&sde_crtc->crtc_lock);
2237 ret = _sde_crtc_power_enable(sde_crtc, true);
2238 mutex_lock(&sde_crtc->crtc_lock);
2239 if (ret)
Dhaval Patelf9245d62017-03-28 16:24:00 -07002240 return;
2241
Clarence Ip7f70ce42017-03-20 06:53:46 -07002242 list_for_each_entry(enc, &dev->mode_config.encoder_list, head) {
2243 if (enc->crtc != crtc)
2244 continue;
2245
2246 SDE_EVT32(DRMID(crtc), DRMID(enc), enable);
2247
2248 sde_encoder_register_vblank_callback(enc,
2249 sde_crtc_vblank_cb, (void *)crtc);
2250 }
2251 } else {
2252 list_for_each_entry(enc, &dev->mode_config.encoder_list, head) {
2253 if (enc->crtc != crtc)
2254 continue;
2255
2256 SDE_EVT32(DRMID(crtc), DRMID(enc), enable);
2257
2258 sde_encoder_register_vblank_callback(enc, NULL, NULL);
2259 }
Lloyd Atkinson2c554eb2017-05-24 16:22:39 -04002260
2261 /* drop lock since power crtc cb may try to re-acquire lock */
2262 mutex_unlock(&sde_crtc->crtc_lock);
Dhaval Patelf9245d62017-03-28 16:24:00 -07002263 _sde_crtc_power_enable(sde_crtc, false);
Lloyd Atkinson2c554eb2017-05-24 16:22:39 -04002264 mutex_lock(&sde_crtc->crtc_lock);
Clarence Ip7f70ce42017-03-20 06:53:46 -07002265 }
2266}
2267
2268/**
2269 * _sde_crtc_set_suspend - notify crtc of suspend enable/disable
2270 * @crtc: Pointer to drm crtc object
2271 * @enable: true to enable suspend, false to indicate resume
2272 */
2273static void _sde_crtc_set_suspend(struct drm_crtc *crtc, bool enable)
2274{
2275 struct sde_crtc *sde_crtc;
2276 struct msm_drm_private *priv;
2277 struct sde_kms *sde_kms;
Gopikrishnaiah Anandan84b4f672017-04-26 10:28:51 -07002278 struct drm_event event;
2279 u32 power_on;
Clarence Ip7f70ce42017-03-20 06:53:46 -07002280
2281 if (!crtc || !crtc->dev || !crtc->dev->dev_private) {
2282 SDE_ERROR("invalid crtc\n");
2283 return;
2284 }
2285 sde_crtc = to_sde_crtc(crtc);
2286 priv = crtc->dev->dev_private;
2287
2288 if (!priv->kms) {
2289 SDE_ERROR("invalid crtc kms\n");
2290 return;
2291 }
2292 sde_kms = to_sde_kms(priv->kms);
2293
2294 SDE_DEBUG("crtc%d suspend = %d\n", crtc->base.id, enable);
2295
2296 mutex_lock(&sde_crtc->crtc_lock);
2297
Gopikrishnaiah Anandan84b4f672017-04-26 10:28:51 -07002298 event.type = DRM_EVENT_CRTC_POWER;
2299 event.length = sizeof(u32);
Clarence Ip7f70ce42017-03-20 06:53:46 -07002300 /*
Clarence Ip2f9beeb2017-03-16 11:04:53 -04002301 * Update CP on suspend/resume transitions
2302 */
Gopikrishnaiah Anandan84b4f672017-04-26 10:28:51 -07002303 if (enable && !sde_crtc->suspend) {
Clarence Ip2f9beeb2017-03-16 11:04:53 -04002304 sde_cp_crtc_suspend(crtc);
Gopikrishnaiah Anandan84b4f672017-04-26 10:28:51 -07002305 power_on = 0;
2306 } else if (!enable && sde_crtc->suspend) {
Clarence Ip2f9beeb2017-03-16 11:04:53 -04002307 sde_cp_crtc_resume(crtc);
Gopikrishnaiah Anandan84b4f672017-04-26 10:28:51 -07002308 power_on = 1;
2309 }
Clarence Ip2f9beeb2017-03-16 11:04:53 -04002310
2311 /*
Clarence Ip7f70ce42017-03-20 06:53:46 -07002312 * If the vblank refcount != 0, release a power reference on suspend
2313 * and take it back during resume (if it is still != 0).
2314 */
2315 if (sde_crtc->suspend == enable)
2316 SDE_DEBUG("crtc%d suspend already set to %d, ignoring update\n",
2317 crtc->base.id, enable);
2318 else if (atomic_read(&sde_crtc->vblank_refcount) != 0)
2319 _sde_crtc_vblank_enable_nolock(sde_crtc, !enable);
2320
2321 sde_crtc->suspend = enable;
Gopikrishnaiah Anandan84b4f672017-04-26 10:28:51 -07002322 msm_mode_object_event_nofity(&crtc->base, crtc->dev, &event,
2323 (u8 *)&power_on);
Clarence Ip7f70ce42017-03-20 06:53:46 -07002324 mutex_unlock(&sde_crtc->crtc_lock);
2325}
2326
2327/**
Clarence Ip7a753bb2016-07-07 11:47:44 -04002328 * sde_crtc_duplicate_state - state duplicate hook
2329 * @crtc: Pointer to drm crtc structure
2330 * @Returns: Pointer to new drm_crtc_state structure
2331 */
2332static struct drm_crtc_state *sde_crtc_duplicate_state(struct drm_crtc *crtc)
2333{
2334 struct sde_crtc *sde_crtc;
2335 struct sde_crtc_state *cstate, *old_cstate;
2336
2337 if (!crtc || !crtc->state) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07002338 SDE_ERROR("invalid argument(s)\n");
Clarence Ip7a753bb2016-07-07 11:47:44 -04002339 return NULL;
2340 }
2341
2342 sde_crtc = to_sde_crtc(crtc);
2343 old_cstate = to_sde_crtc_state(crtc->state);
2344 cstate = msm_property_alloc_state(&sde_crtc->property_info);
2345 if (!cstate) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07002346 SDE_ERROR("failed to allocate state\n");
Clarence Ip7a753bb2016-07-07 11:47:44 -04002347 return NULL;
2348 }
2349
2350 /* duplicate value helper */
2351 msm_property_duplicate_state(&sde_crtc->property_info,
2352 old_cstate, cstate,
2353 cstate->property_values, cstate->property_blobs);
2354
2355 /* duplicate base helper */
2356 __drm_atomic_helper_crtc_duplicate_state(crtc, &cstate->base);
2357
Alan Kwongcdb2f282017-03-18 13:42:06 -07002358 _sde_crtc_rp_duplicate(&old_cstate->rp, &cstate->rp);
2359
Clarence Ip7a753bb2016-07-07 11:47:44 -04002360 return &cstate->base;
2361}
2362
2363/**
2364 * sde_crtc_reset - reset hook for CRTCs
2365 * Resets the atomic state for @crtc by freeing the state pointer (which might
2366 * be NULL, e.g. at driver load time) and allocating a new empty state object.
2367 * @crtc: Pointer to drm crtc structure
2368 */
2369static void sde_crtc_reset(struct drm_crtc *crtc)
2370{
2371 struct sde_crtc *sde_crtc;
2372 struct sde_crtc_state *cstate;
2373
2374 if (!crtc) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07002375 SDE_ERROR("invalid crtc\n");
Clarence Ip7a753bb2016-07-07 11:47:44 -04002376 return;
2377 }
2378
Clarence Ip7f70ce42017-03-20 06:53:46 -07002379 /* revert suspend actions, if necessary */
2380 if (msm_is_suspend_state(crtc->dev))
2381 _sde_crtc_set_suspend(crtc, false);
2382
Clarence Ip7a753bb2016-07-07 11:47:44 -04002383 /* remove previous state, if present */
2384 if (crtc->state) {
2385 sde_crtc_destroy_state(crtc, crtc->state);
2386 crtc->state = 0;
2387 }
2388
2389 sde_crtc = to_sde_crtc(crtc);
2390 cstate = msm_property_alloc_state(&sde_crtc->property_info);
2391 if (!cstate) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07002392 SDE_ERROR("failed to allocate state\n");
Clarence Ip7a753bb2016-07-07 11:47:44 -04002393 return;
2394 }
2395
2396 /* reset value helper */
2397 msm_property_reset_state(&sde_crtc->property_info, cstate,
2398 cstate->property_values, cstate->property_blobs);
2399
Clarence Ipcae1bb62016-07-07 12:07:13 -04002400 _sde_crtc_set_input_fence_timeout(cstate);
2401
Alan Kwongcdb2f282017-03-18 13:42:06 -07002402 _sde_crtc_rp_reset(&cstate->rp);
2403
Clarence Ip7a753bb2016-07-07 11:47:44 -04002404 cstate->base.crtc = crtc;
2405 crtc->state = &cstate->base;
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07002406}
2407
Clarence Ip9728a1d2017-04-18 22:22:13 -04002408static int _sde_crtc_vblank_no_lock(struct sde_crtc *sde_crtc, bool en)
2409{
2410 if (!sde_crtc) {
2411 SDE_ERROR("invalid crtc\n");
2412 return -EINVAL;
2413 } else if (en && atomic_inc_return(&sde_crtc->vblank_refcount) == 1) {
2414 SDE_DEBUG("crtc%d vblank enable\n", sde_crtc->base.base.id);
2415 if (!sde_crtc->suspend)
2416 _sde_crtc_vblank_enable_nolock(sde_crtc, true);
2417 } else if (!en && atomic_read(&sde_crtc->vblank_refcount) < 1) {
2418 SDE_ERROR("crtc%d invalid vblank disable\n",
2419 sde_crtc->base.base.id);
2420 return -EINVAL;
2421 } else if (!en && atomic_dec_return(&sde_crtc->vblank_refcount) == 0) {
2422 SDE_DEBUG("crtc%d vblank disable\n", sde_crtc->base.base.id);
2423 if (!sde_crtc->suspend)
2424 _sde_crtc_vblank_enable_nolock(sde_crtc, false);
2425 } else {
2426 SDE_DEBUG("crtc%d vblank %s refcount:%d\n",
2427 sde_crtc->base.base.id,
2428 en ? "enable" : "disable",
2429 atomic_read(&sde_crtc->vblank_refcount));
2430 }
2431
2432 return 0;
2433}
2434
Veera Sundaram Sankaran82916e02017-03-29 18:44:22 -07002435static void sde_crtc_handle_power_event(u32 event_type, void *arg)
2436{
2437 struct drm_crtc *crtc = arg;
2438 struct sde_crtc *sde_crtc;
2439 struct drm_encoder *encoder;
2440
2441 if (!crtc) {
2442 SDE_ERROR("invalid crtc\n");
2443 return;
2444 }
2445 sde_crtc = to_sde_crtc(crtc);
2446
2447 mutex_lock(&sde_crtc->crtc_lock);
2448
2449 SDE_EVT32(DRMID(crtc), event_type);
2450
2451 if (event_type == SDE_POWER_EVENT_POST_ENABLE) {
2452 /* restore encoder; crtc will be programmed during commit */
2453 drm_for_each_encoder(encoder, crtc->dev) {
2454 if (encoder->crtc != crtc)
2455 continue;
2456
2457 sde_encoder_virt_restore(encoder);
2458 }
2459
2460 } else if (event_type == SDE_POWER_EVENT_POST_DISABLE) {
2461 struct drm_plane *plane;
2462
2463 /*
2464 * set revalidate flag in planes, so it will be re-programmed
2465 * in the next frame update
2466 */
2467 drm_atomic_crtc_for_each_plane(plane, crtc)
2468 sde_plane_set_revalidate(plane, true);
2469 }
2470
2471 mutex_unlock(&sde_crtc->crtc_lock);
2472}
2473
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002474static void sde_crtc_disable(struct drm_crtc *crtc)
2475{
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04002476 struct sde_crtc *sde_crtc;
Dhaval Patel82c8dbc2017-02-18 23:15:10 -08002477 struct sde_crtc_state *cstate;
Alan Kwong07da0982016-11-04 12:57:45 -04002478 struct drm_encoder *encoder;
Veera Sundaram Sankaran82916e02017-03-29 18:44:22 -07002479 struct msm_drm_private *priv;
Gopikrishnaiah Anandande2c81b2017-03-15 12:41:29 -07002480 unsigned long flags;
2481 struct sde_crtc_irq_info *node = NULL;
2482 int ret;
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04002483
Veera Sundaram Sankaran82916e02017-03-29 18:44:22 -07002484 if (!crtc || !crtc->dev || !crtc->dev->dev_private || !crtc->state) {
Lloyd Atkinson4f1c8692016-09-14 14:04:25 -04002485 SDE_ERROR("invalid crtc\n");
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04002486 return;
2487 }
2488 sde_crtc = to_sde_crtc(crtc);
Dhaval Patel82c8dbc2017-02-18 23:15:10 -08002489 cstate = to_sde_crtc_state(crtc->state);
Veera Sundaram Sankaran82916e02017-03-29 18:44:22 -07002490 priv = crtc->dev->dev_private;
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04002491
Alan Kwong163d2612016-11-03 00:56:56 -04002492 SDE_DEBUG("crtc%d\n", crtc->base.id);
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04002493
Clarence Ip7f70ce42017-03-20 06:53:46 -07002494 if (msm_is_suspend_state(crtc->dev))
2495 _sde_crtc_set_suspend(crtc, true);
2496
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07002497 mutex_lock(&sde_crtc->crtc_lock);
Alan Kwong628d19e2016-10-31 13:50:13 -04002498 SDE_EVT32(DRMID(crtc));
2499
Veera Sundaram Sankaran7ee99092017-06-13 11:19:36 -07002500 /* wait for frame_event_done completion */
2501 if (_sde_crtc_wait_for_frame_done(crtc))
2502 SDE_ERROR("crtc%d wait for frame done failed;frame_pending%d\n",
2503 crtc->base.id,
2504 atomic_read(&sde_crtc->frame_pending));
2505
Clarence Ip7f70ce42017-03-20 06:53:46 -07002506 if (atomic_read(&sde_crtc->vblank_refcount) && !sde_crtc->suspend) {
Alan Kwong628d19e2016-10-31 13:50:13 -04002507 SDE_ERROR("crtc%d invalid vblank refcount\n",
2508 crtc->base.id);
Dhaval Patel6c666622017-03-21 23:02:59 -07002509 SDE_EVT32(DRMID(crtc), atomic_read(&sde_crtc->vblank_refcount),
2510 SDE_EVTLOG_FUNC_CASE1);
Clarence Ip9728a1d2017-04-18 22:22:13 -04002511 while (atomic_read(&sde_crtc->vblank_refcount))
2512 if (_sde_crtc_vblank_no_lock(sde_crtc, false))
2513 break;
Alan Kwong07da0982016-11-04 12:57:45 -04002514 }
2515
Alan Kwong628d19e2016-10-31 13:50:13 -04002516 if (atomic_read(&sde_crtc->frame_pending)) {
Dhaval Patel6c666622017-03-21 23:02:59 -07002517 SDE_EVT32(DRMID(crtc), atomic_read(&sde_crtc->frame_pending),
2518 SDE_EVTLOG_FUNC_CASE2);
Alan Kwong9aa061c2016-11-06 21:17:12 -05002519 sde_core_perf_crtc_release_bw(crtc);
Alan Kwong628d19e2016-10-31 13:50:13 -04002520 atomic_set(&sde_crtc->frame_pending, 0);
2521 }
2522
Alan Kwong9aa061c2016-11-06 21:17:12 -05002523 sde_core_perf_crtc_update(crtc, 0, true);
2524
Alan Kwong628d19e2016-10-31 13:50:13 -04002525 drm_for_each_encoder(encoder, crtc->dev) {
2526 if (encoder->crtc != crtc)
2527 continue;
2528 sde_encoder_register_frame_event_callback(encoder, NULL, NULL);
Dhaval Patel82c8dbc2017-02-18 23:15:10 -08002529 cstate->rsc_client = NULL;
2530 cstate->rsc_update = false;
Alan Kwong628d19e2016-10-31 13:50:13 -04002531 }
2532
Veera Sundaram Sankaran82916e02017-03-29 18:44:22 -07002533 if (sde_crtc->power_event)
2534 sde_power_handle_unregister_event(&priv->phandle,
2535 sde_crtc->power_event);
2536
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04002537 memset(sde_crtc->mixers, 0, sizeof(sde_crtc->mixers));
2538 sde_crtc->num_mixers = 0;
Gopikrishnaiah Anandande2c81b2017-03-15 12:41:29 -07002539
Alan Kwong8411a9112017-06-06 19:29:01 -04002540 /* disable clk & bw control until clk & bw properties are set */
2541 cstate->bw_control = false;
Alan Kwong0230a102017-05-16 11:36:44 -07002542 cstate->bw_split_vote = false;
Alan Kwong8411a9112017-06-06 19:29:01 -04002543
Gopikrishnaiah Anandande2c81b2017-03-15 12:41:29 -07002544 spin_lock_irqsave(&sde_crtc->spin_lock, flags);
2545 list_for_each_entry(node, &sde_crtc->user_event_list, list) {
2546 ret = 0;
2547 if (node->func)
2548 ret = node->func(crtc, false, &node->irq);
2549 if (ret)
2550 SDE_ERROR("%s failed to disable event %x\n",
2551 sde_crtc->name, node->event);
2552 }
2553 spin_unlock_irqrestore(&sde_crtc->spin_lock, flags);
2554
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07002555 mutex_unlock(&sde_crtc->crtc_lock);
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002556}
2557
2558static void sde_crtc_enable(struct drm_crtc *crtc)
2559{
Clarence Ipcae1bb62016-07-07 12:07:13 -04002560 struct sde_crtc *sde_crtc;
Alan Kwong628d19e2016-10-31 13:50:13 -04002561 struct drm_encoder *encoder;
Veera Sundaram Sankaran82916e02017-03-29 18:44:22 -07002562 struct msm_drm_private *priv;
Gopikrishnaiah Anandande2c81b2017-03-15 12:41:29 -07002563 unsigned long flags;
2564 struct sde_crtc_irq_info *node = NULL;
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05002565 int ret;
Lloyd Atkinsonaf7952d2016-06-26 22:41:26 -04002566
Veera Sundaram Sankaran82916e02017-03-29 18:44:22 -07002567 if (!crtc || !crtc->dev || !crtc->dev->dev_private) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07002568 SDE_ERROR("invalid crtc\n");
Clarence Ipcae1bb62016-07-07 12:07:13 -04002569 return;
2570 }
Veera Sundaram Sankaran82916e02017-03-29 18:44:22 -07002571 priv = crtc->dev->dev_private;
Clarence Ipcae1bb62016-07-07 12:07:13 -04002572
Alan Kwong163d2612016-11-03 00:56:56 -04002573 SDE_DEBUG("crtc%d\n", crtc->base.id);
Alan Kwong628d19e2016-10-31 13:50:13 -04002574 SDE_EVT32(DRMID(crtc));
Clarence Ipcae1bb62016-07-07 12:07:13 -04002575 sde_crtc = to_sde_crtc(crtc);
Lloyd Atkinsonaf7952d2016-06-26 22:41:26 -04002576
Alan Kwong628d19e2016-10-31 13:50:13 -04002577 drm_for_each_encoder(encoder, crtc->dev) {
2578 if (encoder->crtc != crtc)
2579 continue;
2580 sde_encoder_register_frame_event_callback(encoder,
2581 sde_crtc_frame_event_cb, (void *)crtc);
2582 }
2583
Gopikrishnaiah Anandande2c81b2017-03-15 12:41:29 -07002584 spin_lock_irqsave(&sde_crtc->spin_lock, flags);
2585 list_for_each_entry(node, &sde_crtc->user_event_list, list) {
2586 ret = 0;
2587 if (node->func)
2588 ret = node->func(crtc, true, &node->irq);
2589 if (ret)
2590 SDE_ERROR("%s failed to enable event %x\n",
2591 sde_crtc->name, node->event);
2592 }
2593 spin_unlock_irqrestore(&sde_crtc->spin_lock, flags);
Veera Sundaram Sankaran82916e02017-03-29 18:44:22 -07002594
2595 sde_crtc->power_event = sde_power_handle_register_event(
2596 &priv->phandle,
2597 SDE_POWER_EVENT_POST_ENABLE | SDE_POWER_EVENT_POST_DISABLE,
2598 sde_crtc_handle_power_event, crtc, sde_crtc->name);
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002599}
2600
2601struct plane_state {
Dhaval Patelec10fad2016-08-22 14:40:48 -07002602 struct sde_plane_state *sde_pstate;
Dhaval Patel04c7e8e2016-09-26 20:14:31 -07002603 const struct drm_plane_state *drm_pstate;
Clarence Ipc47a0692016-10-11 10:54:17 -04002604 int stage;
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08002605 u32 pipe_id;
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002606};
2607
Clarence Ipc47a0692016-10-11 10:54:17 -04002608static int pstate_cmp(const void *a, const void *b)
2609{
2610 struct plane_state *pa = (struct plane_state *)a;
2611 struct plane_state *pb = (struct plane_state *)b;
2612 int rc = 0;
2613 int pa_zpos, pb_zpos;
2614
2615 pa_zpos = sde_plane_get_property(pa->sde_pstate, PLANE_PROP_ZPOS);
2616 pb_zpos = sde_plane_get_property(pb->sde_pstate, PLANE_PROP_ZPOS);
2617
2618 if (pa_zpos != pb_zpos)
2619 rc = pa_zpos - pb_zpos;
2620 else
2621 rc = pa->drm_pstate->crtc_x - pb->drm_pstate->crtc_x;
2622
2623 return rc;
2624}
2625
Dhaval Patela8d6bc62017-05-10 17:40:18 -07002626static int _sde_crtc_excl_rect_overlap_check(struct plane_state pstates[],
2627 int cnt, int curr_cnt, struct sde_rect *excl_rect, int z_pos)
2628{
2629 struct sde_rect dst_rect, intersect;
2630 int i, rc = -EINVAL;
2631 const struct drm_plane_state *pstate;
2632
2633 /* start checking from next plane */
2634 for (i = curr_cnt; i < cnt; i++) {
2635 pstate = pstates[i].drm_pstate;
2636 POPULATE_RECT(&dst_rect, pstate->crtc_x, pstate->crtc_y,
2637 pstate->crtc_w, pstate->crtc_h, true);
2638 sde_kms_rect_intersect(&dst_rect, excl_rect, &intersect);
2639
2640 if (intersect.w == excl_rect->w && intersect.h == excl_rect->h
2641 /* next plane may be on same z-order */
2642 && z_pos != pstates[i].stage) {
2643 rc = 0;
2644 goto end;
2645 }
2646 }
2647
2648 SDE_ERROR("excl rect does not find top overlapping rect\n");
2649end:
2650 return rc;
2651}
2652
2653/* no input validation - caller API has all the checks */
2654static int _sde_crtc_excl_dim_layer_check(struct drm_crtc_state *state,
2655 struct plane_state pstates[], int cnt)
2656{
2657 struct sde_crtc_state *cstate = to_sde_crtc_state(state);
2658 struct drm_display_mode *mode = &state->adjusted_mode;
2659 const struct drm_plane_state *pstate;
2660 struct sde_plane_state *sde_pstate;
2661 int rc = 0, i;
2662
2663 /* Check dim layer rect bounds and stage */
2664 for (i = 0; i < cstate->num_dim_layers; i++) {
2665 if ((CHECK_LAYER_BOUNDS(cstate->dim_layer[i].rect.y,
2666 cstate->dim_layer[i].rect.h, mode->vdisplay)) ||
2667 (CHECK_LAYER_BOUNDS(cstate->dim_layer[i].rect.x,
2668 cstate->dim_layer[i].rect.w, mode->hdisplay)) ||
2669 (cstate->dim_layer[i].stage >= SDE_STAGE_MAX) ||
2670 (!cstate->dim_layer[i].rect.w) ||
2671 (!cstate->dim_layer[i].rect.h)) {
2672 SDE_ERROR("invalid dim_layer:{%d,%d,%d,%d}, stage:%d\n",
2673 cstate->dim_layer[i].rect.x,
2674 cstate->dim_layer[i].rect.y,
2675 cstate->dim_layer[i].rect.w,
2676 cstate->dim_layer[i].rect.h,
2677 cstate->dim_layer[i].stage);
2678 SDE_ERROR("display: %dx%d\n", mode->hdisplay,
2679 mode->vdisplay);
2680 rc = -E2BIG;
2681 goto end;
2682 }
2683 }
2684
2685 /* this is traversing on sorted z-order pstates */
2686 for (i = 0; i < cnt; i++) {
2687 pstate = pstates[i].drm_pstate;
2688 sde_pstate = to_sde_plane_state(pstate);
2689 if (sde_pstate->excl_rect.w && sde_pstate->excl_rect.h) {
2690 /* check overlap on all top z-order */
2691 rc = _sde_crtc_excl_rect_overlap_check(pstates, cnt,
2692 i + 1, &sde_pstate->excl_rect, pstates[i].stage);
2693 if (rc)
2694 goto end;
2695 }
2696 }
2697
2698end:
2699 return rc;
2700}
2701
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002702static int sde_crtc_atomic_check(struct drm_crtc *crtc,
2703 struct drm_crtc_state *state)
2704{
Clarence Ipcae1bb62016-07-07 12:07:13 -04002705 struct sde_crtc *sde_crtc;
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08002706 struct plane_state pstates[SDE_STAGE_MAX * 4];
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08002707 struct sde_crtc_state *cstate;
Dhaval Patelec10fad2016-08-22 14:40:48 -07002708
Dhaval Patel04c7e8e2016-09-26 20:14:31 -07002709 const struct drm_plane_state *pstate;
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002710 struct drm_plane *plane;
Dhaval Patelec10fad2016-08-22 14:40:48 -07002711 struct drm_display_mode *mode;
2712
2713 int cnt = 0, rc = 0, mixer_width, i, z_pos;
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08002714
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08002715 struct sde_multirect_plane_states multirect_plane[SDE_STAGE_MAX * 2];
2716 int multirect_count = 0;
2717 const struct drm_plane_state *pipe_staged[SSPP_MAX];
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04002718 int left_zpos_cnt = 0, right_zpos_cnt = 0;
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002719
Clarence Ipcae1bb62016-07-07 12:07:13 -04002720 if (!crtc) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07002721 SDE_ERROR("invalid crtc\n");
Clarence Ipcae1bb62016-07-07 12:07:13 -04002722 return -EINVAL;
2723 }
2724
Alan Kwongcdb2f282017-03-18 13:42:06 -07002725 sde_crtc = to_sde_crtc(crtc);
2726 cstate = to_sde_crtc_state(state);
2727
Lloyd Atkinson5217336c2016-09-15 18:21:18 -04002728 if (!state->enable || !state->active) {
2729 SDE_DEBUG("crtc%d -> enable %d, active %d, skip atomic_check\n",
2730 crtc->base.id, state->enable, state->active);
Alan Kwongcdb2f282017-03-18 13:42:06 -07002731 goto end;
Lloyd Atkinson5217336c2016-09-15 18:21:18 -04002732 }
2733
Dhaval Patelec10fad2016-08-22 14:40:48 -07002734 mode = &state->adjusted_mode;
2735 SDE_DEBUG("%s: check", sde_crtc->name);
Clarence Ipcae1bb62016-07-07 12:07:13 -04002736
Clarence Ip90b282d2017-05-04 10:00:32 -07002737 /* force a full mode set if active state changed */
2738 if (state->active_changed)
2739 state->mode_changed = true;
2740
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08002741 memset(pipe_staged, 0, sizeof(pipe_staged));
2742
Dhaval Patelec10fad2016-08-22 14:40:48 -07002743 mixer_width = sde_crtc_mixer_width(sde_crtc, mode);
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002744
Lloyd Atkinson66e7dde2017-02-08 15:52:53 -05002745 _sde_crtc_setup_is_ppsplit(state);
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05002746 _sde_crtc_setup_lm_bounds(crtc, state);
2747
Dhaval Patelec10fad2016-08-22 14:40:48 -07002748 /* get plane state for all drm planes associated with crtc state */
Dhaval Patel04c7e8e2016-09-26 20:14:31 -07002749 drm_atomic_crtc_state_for_each_plane_state(plane, pstate, state) {
Clarence Ipc47a0692016-10-11 10:54:17 -04002750 if (IS_ERR_OR_NULL(pstate)) {
2751 rc = PTR_ERR(pstate);
2752 SDE_ERROR("%s: failed to get plane%d state, %d\n",
2753 sde_crtc->name, plane->base.id, rc);
Alan Kwong85767282016-10-03 18:03:37 -04002754 goto end;
2755 }
Clarence Ipc47a0692016-10-11 10:54:17 -04002756 if (cnt >= ARRAY_SIZE(pstates))
2757 continue;
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002758
Dhaval Patelec10fad2016-08-22 14:40:48 -07002759 pstates[cnt].sde_pstate = to_sde_plane_state(pstate);
2760 pstates[cnt].drm_pstate = pstate;
Clarence Ipc47a0692016-10-11 10:54:17 -04002761 pstates[cnt].stage = sde_plane_get_property(
2762 pstates[cnt].sde_pstate, PLANE_PROP_ZPOS);
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08002763 pstates[cnt].pipe_id = sde_plane_pipe(plane);
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08002764
2765 /* check dim layer stage with every plane */
2766 for (i = 0; i < cstate->num_dim_layers; i++) {
2767 if (pstates[cnt].stage == cstate->dim_layer[i].stage) {
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -07002768 SDE_ERROR(
2769 "plane:%d/dim_layer:%i-same stage:%d\n",
2770 plane->base.id, i,
2771 cstate->dim_layer[i].stage);
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08002772 rc = -EINVAL;
2773 goto end;
2774 }
2775 }
2776
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08002777 if (pipe_staged[pstates[cnt].pipe_id]) {
2778 multirect_plane[multirect_count].r0 =
2779 pipe_staged[pstates[cnt].pipe_id];
2780 multirect_plane[multirect_count].r1 = pstate;
2781 multirect_count++;
2782
2783 pipe_staged[pstates[cnt].pipe_id] = NULL;
2784 } else {
2785 pipe_staged[pstates[cnt].pipe_id] = pstate;
2786 }
2787
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002788 cnt++;
Dhaval Patelec10fad2016-08-22 14:40:48 -07002789
2790 if (CHECK_LAYER_BOUNDS(pstate->crtc_y, pstate->crtc_h,
2791 mode->vdisplay) ||
2792 CHECK_LAYER_BOUNDS(pstate->crtc_x, pstate->crtc_w,
2793 mode->hdisplay)) {
2794 SDE_ERROR("invalid vertical/horizontal destination\n");
2795 SDE_ERROR("y:%d h:%d vdisp:%d x:%d w:%d hdisp:%d\n",
2796 pstate->crtc_y, pstate->crtc_h, mode->vdisplay,
2797 pstate->crtc_x, pstate->crtc_w, mode->hdisplay);
2798 rc = -E2BIG;
2799 goto end;
2800 }
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002801 }
2802
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08002803 for (i = 1; i < SSPP_MAX; i++) {
Jeykumar Sankarane964dc72017-05-10 19:26:43 -07002804 if (pipe_staged[i]) {
2805 sde_plane_clear_multirect(pipe_staged[i]);
2806
2807 if (is_sde_plane_virtual(pipe_staged[i]->plane)) {
2808 SDE_ERROR("invalid use of virtual plane: %d\n",
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08002809 pipe_staged[i]->plane->base.id);
Jeykumar Sankarane964dc72017-05-10 19:26:43 -07002810 goto end;
2811 }
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08002812 }
2813 }
2814
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04002815 /* assign mixer stages based on sorted zpos property */
2816 sort(pstates, cnt, sizeof(pstates[0]), pstate_cmp, NULL);
2817
Dhaval Patela8d6bc62017-05-10 17:40:18 -07002818 rc = _sde_crtc_excl_dim_layer_check(state, pstates, cnt);
2819 if (rc)
2820 goto end;
2821
Clarence Ipc47a0692016-10-11 10:54:17 -04002822 if (!sde_is_custom_client()) {
2823 int stage_old = pstates[0].stage;
Dhaval Patelec10fad2016-08-22 14:40:48 -07002824
Clarence Ipc47a0692016-10-11 10:54:17 -04002825 z_pos = 0;
2826 for (i = 0; i < cnt; i++) {
2827 if (stage_old != pstates[i].stage)
2828 ++z_pos;
2829 stage_old = pstates[i].stage;
2830 pstates[i].stage = z_pos;
2831 }
2832 }
2833
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04002834 z_pos = -1;
Clarence Ipc47a0692016-10-11 10:54:17 -04002835 for (i = 0; i < cnt; i++) {
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04002836 /* reset counts at every new blend stage */
2837 if (pstates[i].stage != z_pos) {
2838 left_zpos_cnt = 0;
2839 right_zpos_cnt = 0;
2840 z_pos = pstates[i].stage;
2841 }
Clarence Ipc47a0692016-10-11 10:54:17 -04002842
2843 /* verify z_pos setting before using it */
Clarence Ip649989a2016-10-21 14:28:34 -04002844 if (z_pos >= SDE_STAGE_MAX - SDE_STAGE_0) {
Clarence Ipc47a0692016-10-11 10:54:17 -04002845 SDE_ERROR("> %d plane stages assigned\n",
2846 SDE_STAGE_MAX - SDE_STAGE_0);
2847 rc = -EINVAL;
2848 goto end;
2849 } else if (pstates[i].drm_pstate->crtc_x < mixer_width) {
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04002850 if (left_zpos_cnt == 2) {
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08002851 SDE_ERROR("> 2 planes @ stage %d on left\n",
Dhaval Patelec10fad2016-08-22 14:40:48 -07002852 z_pos);
2853 rc = -EINVAL;
2854 goto end;
2855 }
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04002856 left_zpos_cnt++;
2857
Dhaval Patelec10fad2016-08-22 14:40:48 -07002858 } else {
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04002859 if (right_zpos_cnt == 2) {
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08002860 SDE_ERROR("> 2 planes @ stage %d on right\n",
Dhaval Patelec10fad2016-08-22 14:40:48 -07002861 z_pos);
2862 rc = -EINVAL;
2863 goto end;
2864 }
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04002865 right_zpos_cnt++;
Dhaval Patelec10fad2016-08-22 14:40:48 -07002866 }
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04002867
Clarence Ipc47a0692016-10-11 10:54:17 -04002868 pstates[i].sde_pstate->stage = z_pos + SDE_STAGE_0;
Dhaval Patelec10fad2016-08-22 14:40:48 -07002869 SDE_DEBUG("%s: zpos %d", sde_crtc->name, z_pos);
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002870 }
2871
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08002872 for (i = 0; i < multirect_count; i++) {
2873 if (sde_plane_validate_multirect_v2(&multirect_plane[i])) {
2874 SDE_ERROR(
2875 "multirect validation failed for planes (%d - %d)\n",
2876 multirect_plane[i].r0->plane->base.id,
2877 multirect_plane[i].r1->plane->base.id);
2878 rc = -EINVAL;
Alan Kwong9aa061c2016-11-06 21:17:12 -05002879 goto end;
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08002880 }
2881 }
2882
Alan Kwong9aa061c2016-11-06 21:17:12 -05002883 rc = sde_core_perf_crtc_check(crtc, state);
2884 if (rc) {
2885 SDE_ERROR("crtc%d failed performance check %d\n",
2886 crtc->base.id, rc);
2887 goto end;
2888 }
2889
Jeykumar Sankaranaaaa0712017-06-12 17:59:16 -07002890 /* validate source split:
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04002891 * use pstates sorted by stage to check planes on same stage
2892 * we assume that all pipes are in source split so its valid to compare
2893 * without taking into account left/right mixer placement
2894 */
2895 for (i = 1; i < cnt; i++) {
2896 struct plane_state *prv_pstate, *cur_pstate;
Jeykumar Sankaranaaaa0712017-06-12 17:59:16 -07002897 struct sde_rect left_rect, right_rect;
2898 int32_t left_pid, right_pid;
2899 int32_t stage;
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04002900
2901 prv_pstate = &pstates[i - 1];
2902 cur_pstate = &pstates[i];
2903 if (prv_pstate->stage != cur_pstate->stage)
2904 continue;
2905
Jeykumar Sankaranaaaa0712017-06-12 17:59:16 -07002906 stage = cur_pstate->stage;
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04002907
Jeykumar Sankaranaaaa0712017-06-12 17:59:16 -07002908 left_pid = prv_pstate->sde_pstate->base.plane->base.id;
2909 POPULATE_RECT(&left_rect, prv_pstate->drm_pstate->crtc_x,
2910 prv_pstate->drm_pstate->crtc_y,
2911 prv_pstate->drm_pstate->crtc_w,
2912 prv_pstate->drm_pstate->crtc_h, false);
2913
2914 right_pid = cur_pstate->sde_pstate->base.plane->base.id;
2915 POPULATE_RECT(&right_rect, cur_pstate->drm_pstate->crtc_x,
2916 cur_pstate->drm_pstate->crtc_y,
2917 cur_pstate->drm_pstate->crtc_w,
2918 cur_pstate->drm_pstate->crtc_h, false);
2919
2920 if (right_rect.x < left_rect.x) {
2921 swap(left_pid, right_pid);
2922 swap(left_rect, right_rect);
2923 }
2924
2925 /**
2926 * - planes are enumerated in pipe-priority order such that
2927 * planes with lower drm_id must be left-most in a shared
2928 * blend-stage when using source split.
2929 * - planes in source split must be contiguous in width
2930 * - planes in source split must have same dest yoff and height
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04002931 */
Jeykumar Sankaranaaaa0712017-06-12 17:59:16 -07002932 if (right_pid < left_pid) {
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04002933 SDE_ERROR(
Jeykumar Sankaranaaaa0712017-06-12 17:59:16 -07002934 "invalid src split cfg. priority mismatch. stage: %d left: %d right: %d\n",
2935 stage, left_pid, right_pid);
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04002936 rc = -EINVAL;
2937 goto end;
Jeykumar Sankaranaaaa0712017-06-12 17:59:16 -07002938 } else if (right_rect.x != (left_rect.x + left_rect.w)) {
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04002939 SDE_ERROR(
Jeykumar Sankaranaaaa0712017-06-12 17:59:16 -07002940 "non-contiguous coordinates for src split. stage: %d left: %d - %d right: %d - %d\n",
2941 stage, left_rect.x, left_rect.w,
2942 right_rect.x, right_rect.w);
2943 rc = -EINVAL;
2944 goto end;
2945 } else if ((left_rect.y != right_rect.y) ||
2946 (left_rect.h != right_rect.h)) {
2947 SDE_ERROR(
2948 "source split at stage: %d. invalid yoff/height: l_y: %d r_y: %d l_h: %d r_h: %d\n",
2949 stage, left_rect.y, right_rect.y,
2950 left_rect.h, right_rect.h);
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04002951 rc = -EINVAL;
2952 goto end;
2953 }
2954 }
2955
Lloyd Atkinson8ba47032017-03-22 17:13:32 -04002956 rc = _sde_crtc_check_rois(crtc, state);
2957 if (rc) {
2958 SDE_ERROR("crtc%d failed roi check %d\n", crtc->base.id, rc);
2959 goto end;
2960 }
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04002961
Dhaval Patelec10fad2016-08-22 14:40:48 -07002962end:
Alan Kwongcdb2f282017-03-18 13:42:06 -07002963 _sde_crtc_rp_free_unused(&cstate->rp);
Dhaval Patelec10fad2016-08-22 14:40:48 -07002964 return rc;
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002965}
2966
Abhijit Kulkarni7acb3262016-07-05 15:27:25 -04002967int sde_crtc_vblank(struct drm_crtc *crtc, bool en)
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07002968{
Clarence Ip7f70ce42017-03-20 06:53:46 -07002969 struct sde_crtc *sde_crtc;
Clarence Ip9728a1d2017-04-18 22:22:13 -04002970 int rc;
Abhijit Kulkarni7acb3262016-07-05 15:27:25 -04002971
Clarence Ip7f70ce42017-03-20 06:53:46 -07002972 if (!crtc) {
2973 SDE_ERROR("invalid crtc\n");
2974 return -EINVAL;
2975 }
2976 sde_crtc = to_sde_crtc(crtc);
2977
2978 mutex_lock(&sde_crtc->crtc_lock);
Clarence Ip9728a1d2017-04-18 22:22:13 -04002979 rc = _sde_crtc_vblank_no_lock(sde_crtc, en);
Clarence Ip7f70ce42017-03-20 06:53:46 -07002980 mutex_unlock(&sde_crtc->crtc_lock);
Clarence Ip9728a1d2017-04-18 22:22:13 -04002981
Clarence Ip7f70ce42017-03-20 06:53:46 -07002982 return rc;
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07002983}
2984
Lloyd Atkinson5217336c2016-09-15 18:21:18 -04002985void sde_crtc_cancel_pending_flip(struct drm_crtc *crtc, struct drm_file *file)
2986{
2987 struct sde_crtc *sde_crtc = to_sde_crtc(crtc);
2988
Alan Kwong163d2612016-11-03 00:56:56 -04002989 SDE_DEBUG("%s: cancel: %p\n", sde_crtc->name, file);
Lloyd Atkinson4f1c8692016-09-14 14:04:25 -04002990 _sde_crtc_complete_flip(crtc, file);
Lloyd Atkinson5217336c2016-09-15 18:21:18 -04002991}
2992
Clarence Ip7a753bb2016-07-07 11:47:44 -04002993/**
2994 * sde_crtc_install_properties - install all drm properties for crtc
2995 * @crtc: Pointer to drm crtc structure
2996 */
Dhaval Patele4a5dda2016-10-13 19:29:30 -07002997static void sde_crtc_install_properties(struct drm_crtc *crtc,
2998 struct sde_mdss_cfg *catalog)
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07002999{
Clarence Ip7a753bb2016-07-07 11:47:44 -04003000 struct sde_crtc *sde_crtc;
3001 struct drm_device *dev;
Dhaval Patele4a5dda2016-10-13 19:29:30 -07003002 struct sde_kms_info *info;
Alan Kwong9aa061c2016-11-06 21:17:12 -05003003 struct sde_kms *sde_kms;
Clarence Ip7a753bb2016-07-07 11:47:44 -04003004
Lloyd Atkinson4f1c8692016-09-14 14:04:25 -04003005 SDE_DEBUG("\n");
Clarence Ip7a753bb2016-07-07 11:47:44 -04003006
Dhaval Patele4a5dda2016-10-13 19:29:30 -07003007 if (!crtc || !catalog) {
3008 SDE_ERROR("invalid crtc or catalog\n");
Clarence Ip7a753bb2016-07-07 11:47:44 -04003009 return;
3010 }
3011
3012 sde_crtc = to_sde_crtc(crtc);
3013 dev = crtc->dev;
Alan Kwong9aa061c2016-11-06 21:17:12 -05003014 sde_kms = _sde_crtc_get_kms(crtc);
Clarence Ip7a753bb2016-07-07 11:47:44 -04003015
Dhaval Patele4a5dda2016-10-13 19:29:30 -07003016 info = kzalloc(sizeof(struct sde_kms_info), GFP_KERNEL);
3017 if (!info) {
3018 SDE_ERROR("failed to allocate info memory\n");
3019 return;
3020 }
3021
Clarence Ip7a753bb2016-07-07 11:47:44 -04003022 /* range properties */
3023 msm_property_install_range(&sde_crtc->property_info,
Dhaval Patel4e574842016-08-23 15:11:37 -07003024 "input_fence_timeout", 0x0, 0, SDE_CRTC_MAX_INPUT_FENCE_TIMEOUT,
3025 SDE_CRTC_INPUT_FENCE_TIMEOUT, CRTC_PROP_INPUT_FENCE_TIMEOUT);
3026
3027 msm_property_install_range(&sde_crtc->property_info, "output_fence",
3028 0x0, 0, INR_OPEN_MAX, 0x0, CRTC_PROP_OUTPUT_FENCE);
Clarence Ip1d9728b2016-09-01 11:10:54 -04003029
3030 msm_property_install_range(&sde_crtc->property_info,
3031 "output_fence_offset", 0x0, 0, 1, 0,
3032 CRTC_PROP_OUTPUT_FENCE_OFFSET);
Dhaval Patele4a5dda2016-10-13 19:29:30 -07003033
Alan Kwong9aa061c2016-11-06 21:17:12 -05003034 msm_property_install_range(&sde_crtc->property_info,
3035 "core_clk", 0x0, 0, U64_MAX,
3036 sde_kms->perf.max_core_clk_rate,
3037 CRTC_PROP_CORE_CLK);
3038 msm_property_install_range(&sde_crtc->property_info,
3039 "core_ab", 0x0, 0, U64_MAX,
Alan Kwongff30f4a2017-05-23 12:02:00 -07003040 catalog->perf.max_bw_high * 1000ULL,
Alan Kwong9aa061c2016-11-06 21:17:12 -05003041 CRTC_PROP_CORE_AB);
3042 msm_property_install_range(&sde_crtc->property_info,
3043 "core_ib", 0x0, 0, U64_MAX,
Alan Kwongff30f4a2017-05-23 12:02:00 -07003044 catalog->perf.max_bw_high * 1000ULL,
Alan Kwong9aa061c2016-11-06 21:17:12 -05003045 CRTC_PROP_CORE_IB);
Alan Kwong4aacd532017-02-04 18:51:33 -08003046 msm_property_install_range(&sde_crtc->property_info,
Alan Kwong0230a102017-05-16 11:36:44 -07003047 "llcc_ab", 0x0, 0, U64_MAX,
Alan Kwongff30f4a2017-05-23 12:02:00 -07003048 catalog->perf.max_bw_high * 1000ULL,
Alan Kwong0230a102017-05-16 11:36:44 -07003049 CRTC_PROP_LLCC_AB);
Alan Kwong8c176bf2017-02-09 19:34:32 -08003050 msm_property_install_range(&sde_crtc->property_info,
Alan Kwong0230a102017-05-16 11:36:44 -07003051 "llcc_ib", 0x0, 0, U64_MAX,
Alan Kwongff30f4a2017-05-23 12:02:00 -07003052 catalog->perf.max_bw_high * 1000ULL,
Alan Kwong0230a102017-05-16 11:36:44 -07003053 CRTC_PROP_LLCC_IB);
3054 msm_property_install_range(&sde_crtc->property_info,
3055 "dram_ab", 0x0, 0, U64_MAX,
3056 catalog->perf.max_bw_high * 1000ULL,
3057 CRTC_PROP_DRAM_AB);
3058 msm_property_install_range(&sde_crtc->property_info,
3059 "dram_ib", 0x0, 0, U64_MAX,
3060 catalog->perf.max_bw_high * 1000ULL,
3061 CRTC_PROP_DRAM_IB);
Alan Kwong8c176bf2017-02-09 19:34:32 -08003062 msm_property_install_range(&sde_crtc->property_info,
Alan Kwong4aacd532017-02-04 18:51:33 -08003063 "rot_prefill_bw", 0, 0, U64_MAX,
3064 catalog->perf.max_bw_high * 1000ULL,
3065 CRTC_PROP_ROT_PREFILL_BW);
Alan Kwong8c176bf2017-02-09 19:34:32 -08003066 msm_property_install_range(&sde_crtc->property_info,
3067 "rot_clk", 0, 0, U64_MAX,
3068 sde_kms->perf.max_core_clk_rate,
3069 CRTC_PROP_ROT_CLK);
Alan Kwong9aa061c2016-11-06 21:17:12 -05003070
Dhaval Patele4a5dda2016-10-13 19:29:30 -07003071 msm_property_install_blob(&sde_crtc->property_info, "capabilities",
3072 DRM_MODE_PROP_IMMUTABLE, CRTC_PROP_INFO);
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08003073
Lloyd Atkinson8ba47032017-03-22 17:13:32 -04003074 msm_property_install_volatile_range(&sde_crtc->property_info,
3075 "sde_drm_roi_v1", 0x0, 0, ~0, 0, CRTC_PROP_ROI_V1);
3076
Dhaval Patele4a5dda2016-10-13 19:29:30 -07003077 sde_kms_info_reset(info);
3078
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -07003079 if (catalog->has_dim_layer) {
3080 msm_property_install_volatile_range(&sde_crtc->property_info,
3081 "dim_layer_v1", 0x0, 0, ~0, 0, CRTC_PROP_DIM_LAYER_V1);
3082 sde_kms_info_add_keyint(info, "dim_layer_v1_max_layers",
3083 SDE_MAX_DIM_LAYERS);
3084 }
3085
Dhaval Patele4a5dda2016-10-13 19:29:30 -07003086 sde_kms_info_add_keyint(info, "hw_version", catalog->hwversion);
3087 sde_kms_info_add_keyint(info, "max_linewidth",
3088 catalog->max_mixer_width);
3089 sde_kms_info_add_keyint(info, "max_blendstages",
3090 catalog->max_mixer_blendstages);
3091 if (catalog->qseed_type == SDE_SSPP_SCALER_QSEED2)
3092 sde_kms_info_add_keystr(info, "qseed_type", "qseed2");
3093 if (catalog->qseed_type == SDE_SSPP_SCALER_QSEED3)
3094 sde_kms_info_add_keystr(info, "qseed_type", "qseed3");
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08003095
3096 if (sde_is_custom_client()) {
3097 if (catalog->smart_dma_rev == SDE_SSPP_SMART_DMA_V1)
3098 sde_kms_info_add_keystr(info,
3099 "smart_dma_rev", "smart_dma_v1");
3100 if (catalog->smart_dma_rev == SDE_SSPP_SMART_DMA_V2)
3101 sde_kms_info_add_keystr(info,
3102 "smart_dma_rev", "smart_dma_v2");
3103 }
3104
Dhaval Patele4a5dda2016-10-13 19:29:30 -07003105 sde_kms_info_add_keyint(info, "has_src_split", catalog->has_src_split);
Alan Kwong2f84f8a2016-12-29 13:07:47 -05003106 if (catalog->perf.max_bw_low)
3107 sde_kms_info_add_keyint(info, "max_bandwidth_low",
Alan Kwong6259a382017-04-04 06:18:02 -07003108 catalog->perf.max_bw_low * 1000LL);
Alan Kwong2f84f8a2016-12-29 13:07:47 -05003109 if (catalog->perf.max_bw_high)
3110 sde_kms_info_add_keyint(info, "max_bandwidth_high",
Alan Kwong6259a382017-04-04 06:18:02 -07003111 catalog->perf.max_bw_high * 1000LL);
Alan Kwong2f84f8a2016-12-29 13:07:47 -05003112 if (sde_kms->perf.max_core_clk_rate)
3113 sde_kms_info_add_keyint(info, "max_mdp_clk",
3114 sde_kms->perf.max_core_clk_rate);
Alan Kwong6259a382017-04-04 06:18:02 -07003115 sde_kms_info_add_keystr(info, "core_ib_ff",
3116 catalog->perf.core_ib_ff);
3117 sde_kms_info_add_keystr(info, "core_clk_ff",
3118 catalog->perf.core_clk_ff);
3119 sde_kms_info_add_keystr(info, "comp_ratio_rt",
3120 catalog->perf.comp_ratio_rt);
3121 sde_kms_info_add_keystr(info, "comp_ratio_nrt",
3122 catalog->perf.comp_ratio_nrt);
3123 sde_kms_info_add_keyint(info, "dest_scale_prefill_lines",
3124 catalog->perf.dest_scale_prefill_lines);
3125 sde_kms_info_add_keyint(info, "undersized_prefill_lines",
3126 catalog->perf.undersized_prefill_lines);
3127 sde_kms_info_add_keyint(info, "macrotile_prefill_lines",
3128 catalog->perf.macrotile_prefill_lines);
3129 sde_kms_info_add_keyint(info, "yuv_nv12_prefill_lines",
3130 catalog->perf.yuv_nv12_prefill_lines);
3131 sde_kms_info_add_keyint(info, "linear_prefill_lines",
3132 catalog->perf.linear_prefill_lines);
3133 sde_kms_info_add_keyint(info, "downscaling_prefill_lines",
3134 catalog->perf.downscaling_prefill_lines);
3135 sde_kms_info_add_keyint(info, "xtra_prefill_lines",
3136 catalog->perf.xtra_prefill_lines);
3137 sde_kms_info_add_keyint(info, "amortizable_threshold",
3138 catalog->perf.amortizable_threshold);
3139 sde_kms_info_add_keyint(info, "min_prefill_lines",
3140 catalog->perf.min_prefill_lines);
3141
Dhaval Patele4a5dda2016-10-13 19:29:30 -07003142 msm_property_set_blob(&sde_crtc->property_info, &sde_crtc->blob_info,
Narendra Muppalla22d17252017-05-31 15:13:39 -07003143 info->data, SDE_KMS_INFO_DATALEN(info), CRTC_PROP_INFO);
Dhaval Patele4a5dda2016-10-13 19:29:30 -07003144
3145 kfree(info);
Clarence Ip7a753bb2016-07-07 11:47:44 -04003146}
3147
3148/**
3149 * sde_crtc_atomic_set_property - atomically set a crtc drm property
3150 * @crtc: Pointer to drm crtc structure
3151 * @state: Pointer to drm crtc state structure
3152 * @property: Pointer to targeted drm property
3153 * @val: Updated property value
3154 * @Returns: Zero on success
3155 */
3156static int sde_crtc_atomic_set_property(struct drm_crtc *crtc,
3157 struct drm_crtc_state *state,
3158 struct drm_property *property,
3159 uint64_t val)
3160{
3161 struct sde_crtc *sde_crtc;
3162 struct sde_crtc_state *cstate;
Clarence Ipcae1bb62016-07-07 12:07:13 -04003163 int idx, ret = -EINVAL;
Clarence Ip7a753bb2016-07-07 11:47:44 -04003164
3165 if (!crtc || !state || !property) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07003166 SDE_ERROR("invalid argument(s)\n");
Clarence Ip7a753bb2016-07-07 11:47:44 -04003167 } else {
3168 sde_crtc = to_sde_crtc(crtc);
3169 cstate = to_sde_crtc_state(state);
3170 ret = msm_property_atomic_set(&sde_crtc->property_info,
3171 cstate->property_values, cstate->property_blobs,
3172 property, val);
Clarence Ipcae1bb62016-07-07 12:07:13 -04003173 if (!ret) {
3174 idx = msm_property_index(&sde_crtc->property_info,
3175 property);
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08003176 switch (idx) {
3177 case CRTC_PROP_INPUT_FENCE_TIMEOUT:
Clarence Ipcae1bb62016-07-07 12:07:13 -04003178 _sde_crtc_set_input_fence_timeout(cstate);
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08003179 break;
3180 case CRTC_PROP_DIM_LAYER_V1:
3181 _sde_crtc_set_dim_layer_v1(cstate, (void *)val);
3182 break;
Lloyd Atkinson8ba47032017-03-22 17:13:32 -04003183 case CRTC_PROP_ROI_V1:
3184 ret = _sde_crtc_set_roi_v1(state, (void *)val);
3185 break;
Alan Kwong8411a9112017-06-06 19:29:01 -04003186 case CRTC_PROP_CORE_CLK:
Alan Kwongff30f4a2017-05-23 12:02:00 -07003187 case CRTC_PROP_CORE_AB:
3188 case CRTC_PROP_CORE_IB:
Alan Kwongff30f4a2017-05-23 12:02:00 -07003189 cstate->bw_control = true;
3190 break;
Alan Kwong0230a102017-05-16 11:36:44 -07003191 case CRTC_PROP_LLCC_AB:
3192 case CRTC_PROP_LLCC_IB:
3193 case CRTC_PROP_DRAM_AB:
3194 case CRTC_PROP_DRAM_IB:
3195 cstate->bw_control = true;
3196 cstate->bw_split_vote = true;
3197 break;
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08003198 default:
3199 /* nothing to do */
3200 break;
3201 }
Gopikrishnaiah Anandane0e5e0c2016-05-25 11:05:33 -07003202 } else {
3203 ret = sde_cp_crtc_set_property(crtc,
3204 property, val);
Clarence Ipcae1bb62016-07-07 12:07:13 -04003205 }
Gopikrishnaiah Anandane0e5e0c2016-05-25 11:05:33 -07003206 if (ret)
3207 DRM_ERROR("failed to set the property\n");
Alan Kwongcdb2f282017-03-18 13:42:06 -07003208
3209 SDE_DEBUG("crtc%d %s[%d] <= 0x%llx ret=%d\n", crtc->base.id,
3210 property->name, property->base.id, val, ret);
Clarence Ip7a753bb2016-07-07 11:47:44 -04003211 }
3212
3213 return ret;
3214}
3215
3216/**
3217 * sde_crtc_set_property - set a crtc drm property
3218 * @crtc: Pointer to drm crtc structure
3219 * @property: Pointer to targeted drm property
3220 * @val: Updated property value
3221 * @Returns: Zero on success
3222 */
3223static int sde_crtc_set_property(struct drm_crtc *crtc,
3224 struct drm_property *property, uint64_t val)
3225{
Lloyd Atkinson4f1c8692016-09-14 14:04:25 -04003226 SDE_DEBUG("\n");
Clarence Ip7a753bb2016-07-07 11:47:44 -04003227
3228 return sde_crtc_atomic_set_property(crtc, crtc->state, property, val);
3229}
3230
3231/**
3232 * sde_crtc_atomic_get_property - retrieve a crtc drm property
3233 * @crtc: Pointer to drm crtc structure
3234 * @state: Pointer to drm crtc state structure
3235 * @property: Pointer to targeted drm property
3236 * @val: Pointer to variable for receiving property value
3237 * @Returns: Zero on success
3238 */
3239static int sde_crtc_atomic_get_property(struct drm_crtc *crtc,
3240 const struct drm_crtc_state *state,
3241 struct drm_property *property,
3242 uint64_t *val)
3243{
3244 struct sde_crtc *sde_crtc;
3245 struct sde_crtc_state *cstate;
Clarence Ip24f80662016-06-13 19:05:32 -04003246 int i, ret = -EINVAL;
Dhaval Patel5cb59be2017-04-20 20:00:56 -07003247 bool conn_offset = 0;
Clarence Ip7a753bb2016-07-07 11:47:44 -04003248
3249 if (!crtc || !state) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07003250 SDE_ERROR("invalid argument(s)\n");
Clarence Ip7a753bb2016-07-07 11:47:44 -04003251 } else {
3252 sde_crtc = to_sde_crtc(crtc);
3253 cstate = to_sde_crtc_state(state);
Dhaval Patel5cb59be2017-04-20 20:00:56 -07003254
3255 for (i = 0; i < cstate->num_connectors; ++i) {
3256 conn_offset = sde_connector_needs_offset(
3257 cstate->connectors[i]);
3258 if (conn_offset)
3259 break;
3260 }
3261
Clarence Ip24f80662016-06-13 19:05:32 -04003262 i = msm_property_index(&sde_crtc->property_info, property);
3263 if (i == CRTC_PROP_OUTPUT_FENCE) {
Dhaval Patel39323d42017-03-01 23:48:24 -08003264 uint32_t offset = sde_crtc_get_property(cstate,
Clarence Ip1d9728b2016-09-01 11:10:54 -04003265 CRTC_PROP_OUTPUT_FENCE_OFFSET);
3266
Dhaval Patel5cb59be2017-04-20 20:00:56 -07003267 ret = sde_fence_create(&sde_crtc->output_fence, val,
3268 offset + conn_offset);
Clarence Ip1d9728b2016-09-01 11:10:54 -04003269 if (ret)
3270 SDE_ERROR("fence create failed\n");
Clarence Ip24f80662016-06-13 19:05:32 -04003271 } else {
3272 ret = msm_property_atomic_get(&sde_crtc->property_info,
3273 cstate->property_values,
3274 cstate->property_blobs, property, val);
Gopikrishnaiah Anandane0e5e0c2016-05-25 11:05:33 -07003275 if (ret)
3276 ret = sde_cp_crtc_get_property(crtc,
3277 property, val);
Clarence Ip24f80662016-06-13 19:05:32 -04003278 }
Gopikrishnaiah Anandane0e5e0c2016-05-25 11:05:33 -07003279 if (ret)
3280 DRM_ERROR("get property failed\n");
Clarence Ip7a753bb2016-07-07 11:47:44 -04003281 }
Clarence Ip7a753bb2016-07-07 11:47:44 -04003282 return ret;
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07003283}
3284
Alan Kwong67a3f792016-11-01 23:16:53 -04003285#ifdef CONFIG_DEBUG_FS
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07003286static int _sde_debugfs_status_show(struct seq_file *s, void *data)
Clarence Ip8f7366c2016-07-05 12:15:26 -04003287{
3288 struct sde_crtc *sde_crtc;
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07003289 struct sde_plane_state *pstate = NULL;
Clarence Ip8f7366c2016-07-05 12:15:26 -04003290 struct sde_crtc_mixer *m;
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07003291
3292 struct drm_crtc *crtc;
3293 struct drm_plane *plane;
3294 struct drm_display_mode *mode;
3295 struct drm_framebuffer *fb;
3296 struct drm_plane_state *state;
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -07003297 struct sde_crtc_state *cstate;
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07003298
3299 int i, out_width;
Clarence Ip8f7366c2016-07-05 12:15:26 -04003300
3301 if (!s || !s->private)
3302 return -EINVAL;
3303
3304 sde_crtc = s->private;
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07003305 crtc = &sde_crtc->base;
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -07003306 cstate = to_sde_crtc_state(crtc->state);
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07003307
3308 mutex_lock(&sde_crtc->crtc_lock);
3309 mode = &crtc->state->adjusted_mode;
3310 out_width = sde_crtc_mixer_width(sde_crtc, mode);
3311
3312 seq_printf(s, "crtc:%d width:%d height:%d\n", crtc->base.id,
3313 mode->hdisplay, mode->vdisplay);
3314
3315 seq_puts(s, "\n");
3316
Clarence Ip8f7366c2016-07-05 12:15:26 -04003317 for (i = 0; i < sde_crtc->num_mixers; ++i) {
Lloyd Atkinsone7bcdd22016-08-11 10:53:37 -04003318 m = &sde_crtc->mixers[i];
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07003319 if (!m->hw_lm)
3320 seq_printf(s, "\tmixer[%d] has no lm\n", i);
3321 else if (!m->hw_ctl)
3322 seq_printf(s, "\tmixer[%d] has no ctl\n", i);
3323 else
3324 seq_printf(s, "\tmixer:%d ctl:%d width:%d height:%d\n",
3325 m->hw_lm->idx - LM_0, m->hw_ctl->idx - CTL_0,
3326 out_width, mode->vdisplay);
Clarence Ip8f7366c2016-07-05 12:15:26 -04003327 }
Dhaval Patel44f12472016-08-29 12:19:47 -07003328
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07003329 seq_puts(s, "\n");
Dhaval Patel48c76022016-09-01 17:51:23 -07003330
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -07003331 for (i = 0; i < cstate->num_dim_layers; i++) {
3332 struct sde_hw_dim_layer *dim_layer = &cstate->dim_layer[i];
3333
3334 seq_printf(s, "\tdim_layer:%d] stage:%d flags:%d\n",
3335 i, dim_layer->stage, dim_layer->flags);
3336 seq_printf(s, "\tdst_x:%d dst_y:%d dst_w:%d dst_h:%d\n",
3337 dim_layer->rect.x, dim_layer->rect.y,
3338 dim_layer->rect.w, dim_layer->rect.h);
3339 seq_printf(s,
3340 "\tcolor_0:%d color_1:%d color_2:%d color_3:%d\n",
3341 dim_layer->color_fill.color_0,
3342 dim_layer->color_fill.color_1,
3343 dim_layer->color_fill.color_2,
3344 dim_layer->color_fill.color_3);
3345 seq_puts(s, "\n");
3346 }
3347
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07003348 drm_atomic_crtc_for_each_plane(plane, crtc) {
3349 pstate = to_sde_plane_state(plane->state);
3350 state = plane->state;
3351
3352 if (!pstate || !state)
3353 continue;
3354
3355 seq_printf(s, "\tplane:%u stage:%d\n", plane->base.id,
3356 pstate->stage);
3357
3358 if (plane->state->fb) {
3359 fb = plane->state->fb;
3360
3361 seq_printf(s, "\tfb:%d image format:%4.4s wxh:%ux%u bpp:%d\n",
3362 fb->base.id, (char *) &fb->pixel_format,
3363 fb->width, fb->height, fb->bits_per_pixel);
3364
3365 seq_puts(s, "\t");
3366 for (i = 0; i < ARRAY_SIZE(fb->modifier); i++)
3367 seq_printf(s, "modifier[%d]:%8llu ", i,
3368 fb->modifier[i]);
3369 seq_puts(s, "\n");
3370
3371 seq_puts(s, "\t");
3372 for (i = 0; i < ARRAY_SIZE(fb->pitches); i++)
3373 seq_printf(s, "pitches[%d]:%8u ", i,
3374 fb->pitches[i]);
3375 seq_puts(s, "\n");
3376
3377 seq_puts(s, "\t");
3378 for (i = 0; i < ARRAY_SIZE(fb->offsets); i++)
3379 seq_printf(s, "offsets[%d]:%8u ", i,
3380 fb->offsets[i]);
Dhaval Patel48c76022016-09-01 17:51:23 -07003381 seq_puts(s, "\n");
3382 }
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07003383
3384 seq_printf(s, "\tsrc_x:%4d src_y:%4d src_w:%4d src_h:%4d\n",
3385 state->src_x, state->src_y, state->src_w, state->src_h);
3386
3387 seq_printf(s, "\tdst x:%4d dst_y:%4d dst_w:%4d dst_h:%4d\n",
3388 state->crtc_x, state->crtc_y, state->crtc_w,
3389 state->crtc_h);
Jeykumar Sankarane964dc72017-05-10 19:26:43 -07003390 seq_printf(s, "\tmultirect: mode: %d index: %d\n",
3391 pstate->multirect_mode, pstate->multirect_index);
Veera Sundaram Sankaran58e12812017-05-05 11:51:09 -07003392
3393 seq_printf(s, "\texcl_rect: x:%4d y:%4d w:%4d h:%4d\n",
3394 pstate->excl_rect.x, pstate->excl_rect.y,
3395 pstate->excl_rect.w, pstate->excl_rect.h);
3396
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07003397 seq_puts(s, "\n");
Clarence Ip8f7366c2016-07-05 12:15:26 -04003398 }
Alan Kwong07da0982016-11-04 12:57:45 -04003399
3400 if (sde_crtc->vblank_cb_count) {
3401 ktime_t diff = ktime_sub(ktime_get(), sde_crtc->vblank_cb_time);
3402 s64 diff_ms = ktime_to_ms(diff);
3403 s64 fps = diff_ms ? DIV_ROUND_CLOSEST(
3404 sde_crtc->vblank_cb_count * 1000, diff_ms) : 0;
3405
3406 seq_printf(s,
Dhaval Pateld67cf4a2017-06-14 18:08:32 -07003407 "vblank fps:%lld count:%u total:%llums total_framecount:%llu\n",
3408 fps, sde_crtc->vblank_cb_count,
3409 ktime_to_ms(diff), sde_crtc->play_count);
Alan Kwong07da0982016-11-04 12:57:45 -04003410
3411 /* reset time & count for next measurement */
3412 sde_crtc->vblank_cb_count = 0;
3413 sde_crtc->vblank_cb_time = ktime_set(0, 0);
3414 }
3415
3416 seq_printf(s, "vblank_refcount:%d\n",
3417 atomic_read(&sde_crtc->vblank_refcount));
3418
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07003419 mutex_unlock(&sde_crtc->crtc_lock);
3420
Clarence Ip8f7366c2016-07-05 12:15:26 -04003421 return 0;
3422}
3423
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07003424static int _sde_debugfs_status_open(struct inode *inode, struct file *file)
Clarence Ip8f7366c2016-07-05 12:15:26 -04003425{
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07003426 return single_open(file, _sde_debugfs_status_show, inode->i_private);
Clarence Ip8f7366c2016-07-05 12:15:26 -04003427}
3428
Dhaval Patelf9245d62017-03-28 16:24:00 -07003429static ssize_t _sde_crtc_misr_setup(struct file *file,
3430 const char __user *user_buf, size_t count, loff_t *ppos)
3431{
3432 struct sde_crtc *sde_crtc;
3433 struct sde_crtc_mixer *m;
3434 int i = 0, rc;
3435 char buf[MISR_BUFF_SIZE + 1];
3436 u32 frame_count, enable;
3437 size_t buff_copy;
3438
3439 if (!file || !file->private_data)
3440 return -EINVAL;
3441
3442 sde_crtc = file->private_data;
3443 buff_copy = min_t(size_t, count, MISR_BUFF_SIZE);
3444 if (copy_from_user(buf, user_buf, buff_copy)) {
3445 SDE_ERROR("buffer copy failed\n");
3446 return -EINVAL;
3447 }
3448
3449 buf[buff_copy] = 0; /* end of string */
3450
3451 if (sscanf(buf, "%u %u", &enable, &frame_count) != 2)
3452 return -EINVAL;
3453
3454 rc = _sde_crtc_power_enable(sde_crtc, true);
3455 if (rc)
3456 return rc;
3457
3458 mutex_lock(&sde_crtc->crtc_lock);
3459 sde_crtc->misr_enable = enable;
3460 for (i = 0; i < sde_crtc->num_mixers; ++i) {
3461 m = &sde_crtc->mixers[i];
3462 if (!m->hw_lm)
3463 continue;
3464
3465 m->hw_lm->ops.setup_misr(m->hw_lm, enable, frame_count);
3466 }
3467 mutex_unlock(&sde_crtc->crtc_lock);
3468 _sde_crtc_power_enable(sde_crtc, false);
3469
3470 return count;
3471}
3472
3473static ssize_t _sde_crtc_misr_read(struct file *file,
3474 char __user *user_buff, size_t count, loff_t *ppos)
3475{
3476 struct sde_crtc *sde_crtc;
3477 struct sde_crtc_mixer *m;
3478 int i = 0, rc;
3479 ssize_t len = 0;
3480 char buf[MISR_BUFF_SIZE + 1] = {'\0'};
3481
3482 if (*ppos)
3483 return 0;
3484
3485 if (!file || !file->private_data)
3486 return -EINVAL;
3487
3488 sde_crtc = file->private_data;
3489 rc = _sde_crtc_power_enable(sde_crtc, true);
3490 if (rc)
3491 return rc;
3492
3493 mutex_lock(&sde_crtc->crtc_lock);
3494 if (!sde_crtc->misr_enable) {
3495 len += snprintf(buf + len, MISR_BUFF_SIZE - len,
3496 "disabled\n");
3497 goto buff_check;
3498 }
3499
3500 for (i = 0; i < sde_crtc->num_mixers; ++i) {
3501 m = &sde_crtc->mixers[i];
3502 if (!m->hw_lm)
3503 continue;
3504
3505 len += snprintf(buf + len, MISR_BUFF_SIZE - len, "lm idx:%d\n",
3506 m->hw_lm->idx - LM_0);
3507 len += snprintf(buf + len, MISR_BUFF_SIZE - len, "0x%x\n",
3508 m->hw_lm->ops.collect_misr(m->hw_lm));
3509 }
3510
3511buff_check:
3512 if (count <= len) {
3513 len = 0;
3514 goto end;
3515 }
3516
3517 if (copy_to_user(user_buff, buf, len)) {
3518 len = -EFAULT;
3519 goto end;
3520 }
3521
3522 *ppos += len; /* increase offset */
3523
3524end:
3525 mutex_unlock(&sde_crtc->crtc_lock);
3526 _sde_crtc_power_enable(sde_crtc, false);
3527 return len;
3528}
3529
3530#define DEFINE_SDE_DEBUGFS_SEQ_FOPS(__prefix) \
Alan Kwong67a3f792016-11-01 23:16:53 -04003531static int __prefix ## _open(struct inode *inode, struct file *file) \
3532{ \
3533 return single_open(file, __prefix ## _show, inode->i_private); \
3534} \
3535static const struct file_operations __prefix ## _fops = { \
3536 .owner = THIS_MODULE, \
3537 .open = __prefix ## _open, \
3538 .release = single_release, \
3539 .read = seq_read, \
3540 .llseek = seq_lseek, \
3541}
3542
3543static int sde_crtc_debugfs_state_show(struct seq_file *s, void *v)
3544{
3545 struct drm_crtc *crtc = (struct drm_crtc *) s->private;
Alan Kwong751cf462017-06-08 10:26:46 -04003546 struct sde_crtc *sde_crtc = to_sde_crtc(crtc);
Alan Kwong67a3f792016-11-01 23:16:53 -04003547 struct sde_crtc_state *cstate = to_sde_crtc_state(crtc->state);
Alan Kwongcdb2f282017-03-18 13:42:06 -07003548 struct sde_crtc_res *res;
Alan Kwong0230a102017-05-16 11:36:44 -07003549 int i;
Alan Kwong67a3f792016-11-01 23:16:53 -04003550
3551 seq_printf(s, "num_connectors: %d\n", cstate->num_connectors);
Dhaval Patel4d424602017-02-18 19:40:14 -08003552 seq_printf(s, "client type: %d\n", sde_crtc_get_client_type(crtc));
Alan Kwong3e985f02017-02-12 15:08:44 -08003553 seq_printf(s, "intf_mode: %d\n", sde_crtc_get_intf_mode(crtc));
Alan Kwong751cf462017-06-08 10:26:46 -04003554 seq_printf(s, "core_clk_rate: %llu\n",
3555 sde_crtc->cur_perf.core_clk_rate);
Alan Kwong0230a102017-05-16 11:36:44 -07003556 for (i = SDE_POWER_HANDLE_DBUS_ID_MNOC;
3557 i < SDE_POWER_HANDLE_DBUS_ID_MAX; i++) {
3558 seq_printf(s, "bw_ctl[%s]: %llu\n",
3559 sde_power_handle_get_dbus_name(i),
3560 sde_crtc->cur_perf.bw_ctl[i]);
3561 seq_printf(s, "max_per_pipe_ib[%s]: %llu\n",
3562 sde_power_handle_get_dbus_name(i),
3563 sde_crtc->cur_perf.max_per_pipe_ib[i]);
3564 }
Alan Kwong67a3f792016-11-01 23:16:53 -04003565
Alan Kwongcdb2f282017-03-18 13:42:06 -07003566 seq_printf(s, "rp.%d: ", cstate->rp.sequence_id);
3567 list_for_each_entry(res, &cstate->rp.res_list, list)
3568 seq_printf(s, "0x%x/0x%llx/%pK/%d ",
3569 res->type, res->tag, res->val,
3570 atomic_read(&res->refcount));
3571 seq_puts(s, "\n");
3572
Alan Kwong67a3f792016-11-01 23:16:53 -04003573 return 0;
3574}
3575DEFINE_SDE_DEBUGFS_SEQ_FOPS(sde_crtc_debugfs_state);
3576
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07003577static int _sde_crtc_init_debugfs(struct drm_crtc *crtc)
Clarence Ip8f7366c2016-07-05 12:15:26 -04003578{
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07003579 struct sde_crtc *sde_crtc;
3580 struct sde_kms *sde_kms;
3581
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07003582 static const struct file_operations debugfs_status_fops = {
3583 .open = _sde_debugfs_status_open,
Clarence Ip8f7366c2016-07-05 12:15:26 -04003584 .read = seq_read,
3585 .llseek = seq_lseek,
3586 .release = single_release,
3587 };
Dhaval Patelf9245d62017-03-28 16:24:00 -07003588 static const struct file_operations debugfs_misr_fops = {
3589 .open = simple_open,
3590 .read = _sde_crtc_misr_read,
3591 .write = _sde_crtc_misr_setup,
3592 };
Alan Kwong67a3f792016-11-01 23:16:53 -04003593
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07003594 if (!crtc)
3595 return -EINVAL;
3596 sde_crtc = to_sde_crtc(crtc);
3597
3598 sde_kms = _sde_crtc_get_kms(crtc);
3599 if (!sde_kms)
3600 return -EINVAL;
3601
3602 sde_crtc->debugfs_root = debugfs_create_dir(sde_crtc->name,
Lloyd Atkinson09e64bf2017-04-13 14:09:59 -07003603 crtc->dev->primary->debugfs_root);
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07003604 if (!sde_crtc->debugfs_root)
3605 return -ENOMEM;
3606
3607 /* don't error check these */
Lloyd Atkinson8de415a2017-05-23 11:31:16 -04003608 debugfs_create_file("status", 0400,
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07003609 sde_crtc->debugfs_root,
3610 sde_crtc, &debugfs_status_fops);
Lloyd Atkinson8de415a2017-05-23 11:31:16 -04003611 debugfs_create_file("state", 0600,
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07003612 sde_crtc->debugfs_root,
3613 &sde_crtc->base,
3614 &sde_crtc_debugfs_state_fops);
Lloyd Atkinson8de415a2017-05-23 11:31:16 -04003615 debugfs_create_file("misr_data", 0600, sde_crtc->debugfs_root,
Dhaval Patelf9245d62017-03-28 16:24:00 -07003616 sde_crtc, &debugfs_misr_fops);
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07003617
3618 return 0;
3619}
3620
3621static void _sde_crtc_destroy_debugfs(struct drm_crtc *crtc)
3622{
3623 struct sde_crtc *sde_crtc;
3624
3625 if (!crtc)
3626 return;
3627 sde_crtc = to_sde_crtc(crtc);
3628 debugfs_remove_recursive(sde_crtc->debugfs_root);
Clarence Ip8f7366c2016-07-05 12:15:26 -04003629}
Alan Kwong67a3f792016-11-01 23:16:53 -04003630#else
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07003631static int _sde_crtc_init_debugfs(struct drm_crtc *crtc)
Alan Kwong67a3f792016-11-01 23:16:53 -04003632{
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07003633 return 0;
Alan Kwong67a3f792016-11-01 23:16:53 -04003634}
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07003635
3636static void _sde_crtc_destroy_debugfs(struct drm_crtc *crtc)
3637{
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07003638}
3639#endif /* CONFIG_DEBUG_FS */
3640
3641static int sde_crtc_late_register(struct drm_crtc *crtc)
3642{
3643 return _sde_crtc_init_debugfs(crtc);
3644}
3645
3646static void sde_crtc_early_unregister(struct drm_crtc *crtc)
3647{
3648 _sde_crtc_destroy_debugfs(crtc);
3649}
3650
3651static const struct drm_crtc_funcs sde_crtc_funcs = {
3652 .set_config = drm_atomic_helper_set_config,
3653 .destroy = sde_crtc_destroy,
3654 .page_flip = drm_atomic_helper_page_flip,
3655 .set_property = sde_crtc_set_property,
3656 .atomic_set_property = sde_crtc_atomic_set_property,
3657 .atomic_get_property = sde_crtc_atomic_get_property,
3658 .reset = sde_crtc_reset,
3659 .atomic_duplicate_state = sde_crtc_duplicate_state,
3660 .atomic_destroy_state = sde_crtc_destroy_state,
3661 .late_register = sde_crtc_late_register,
3662 .early_unregister = sde_crtc_early_unregister,
3663};
3664
3665static const struct drm_crtc_helper_funcs sde_crtc_helper_funcs = {
3666 .mode_fixup = sde_crtc_mode_fixup,
3667 .disable = sde_crtc_disable,
3668 .enable = sde_crtc_enable,
3669 .atomic_check = sde_crtc_atomic_check,
3670 .atomic_begin = sde_crtc_atomic_begin,
3671 .atomic_flush = sde_crtc_atomic_flush,
3672};
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07003673
Clarence Ipa18d4832017-03-13 12:35:44 -07003674static void _sde_crtc_event_cb(struct kthread_work *work)
3675{
3676 struct sde_crtc_event *event;
3677 struct sde_crtc *sde_crtc;
3678 unsigned long irq_flags;
3679
3680 if (!work) {
3681 SDE_ERROR("invalid work item\n");
3682 return;
3683 }
3684
3685 event = container_of(work, struct sde_crtc_event, kt_work);
Clarence Ipa18d4832017-03-13 12:35:44 -07003686
3687 /* set sde_crtc to NULL for static work structures */
3688 sde_crtc = event->sde_crtc;
3689 if (!sde_crtc)
3690 return;
3691
Gopikrishnaiah Anandanb6b401f2017-03-14 16:39:49 -07003692 if (event->cb_func)
3693 event->cb_func(&sde_crtc->base, event->usr);
3694
Clarence Ipa18d4832017-03-13 12:35:44 -07003695 spin_lock_irqsave(&sde_crtc->event_lock, irq_flags);
3696 list_add_tail(&event->list, &sde_crtc->event_free_list);
3697 spin_unlock_irqrestore(&sde_crtc->event_lock, irq_flags);
3698}
3699
3700int sde_crtc_event_queue(struct drm_crtc *crtc,
Gopikrishnaiah Anandanb6b401f2017-03-14 16:39:49 -07003701 void (*func)(struct drm_crtc *crtc, void *usr), void *usr)
Clarence Ipa18d4832017-03-13 12:35:44 -07003702{
3703 unsigned long irq_flags;
3704 struct sde_crtc *sde_crtc;
Veera Sundaram Sankaran10ea2bd2017-06-14 14:10:57 -07003705 struct msm_drm_private *priv;
Clarence Ipa18d4832017-03-13 12:35:44 -07003706 struct sde_crtc_event *event = NULL;
Veera Sundaram Sankaran10ea2bd2017-06-14 14:10:57 -07003707 u32 crtc_id;
Clarence Ipa18d4832017-03-13 12:35:44 -07003708
Veera Sundaram Sankaran10ea2bd2017-06-14 14:10:57 -07003709 if (!crtc || !crtc->dev || !crtc->dev->dev_private || !func) {
3710 SDE_ERROR("invalid parameters\n");
Clarence Ipa18d4832017-03-13 12:35:44 -07003711 return -EINVAL;
Veera Sundaram Sankaran10ea2bd2017-06-14 14:10:57 -07003712 }
Clarence Ipa18d4832017-03-13 12:35:44 -07003713 sde_crtc = to_sde_crtc(crtc);
Veera Sundaram Sankaran10ea2bd2017-06-14 14:10:57 -07003714 priv = crtc->dev->dev_private;
3715 crtc_id = drm_crtc_index(crtc);
Clarence Ipa18d4832017-03-13 12:35:44 -07003716
3717 /*
3718 * Obtain an event struct from the private cache. This event
3719 * queue may be called from ISR contexts, so use a private
3720 * cache to avoid calling any memory allocation functions.
3721 */
3722 spin_lock_irqsave(&sde_crtc->event_lock, irq_flags);
3723 if (!list_empty(&sde_crtc->event_free_list)) {
3724 event = list_first_entry(&sde_crtc->event_free_list,
3725 struct sde_crtc_event, list);
3726 list_del_init(&event->list);
3727 }
3728 spin_unlock_irqrestore(&sde_crtc->event_lock, irq_flags);
3729
3730 if (!event)
3731 return -ENOMEM;
3732
3733 /* populate event node */
3734 event->sde_crtc = sde_crtc;
3735 event->cb_func = func;
3736 event->usr = usr;
3737
3738 /* queue new event request */
3739 kthread_init_work(&event->kt_work, _sde_crtc_event_cb);
Veera Sundaram Sankaran10ea2bd2017-06-14 14:10:57 -07003740 kthread_queue_work(&priv->event_thread[crtc_id].worker,
3741 &event->kt_work);
Clarence Ipa18d4832017-03-13 12:35:44 -07003742
3743 return 0;
3744}
3745
3746static int _sde_crtc_init_events(struct sde_crtc *sde_crtc)
3747{
3748 int i, rc = 0;
3749
3750 if (!sde_crtc) {
3751 SDE_ERROR("invalid crtc\n");
3752 return -EINVAL;
3753 }
3754
3755 spin_lock_init(&sde_crtc->event_lock);
3756
3757 INIT_LIST_HEAD(&sde_crtc->event_free_list);
3758 for (i = 0; i < SDE_CRTC_MAX_EVENT_COUNT; ++i)
3759 list_add_tail(&sde_crtc->event_cache[i].list,
3760 &sde_crtc->event_free_list);
3761
Clarence Ipa18d4832017-03-13 12:35:44 -07003762 return rc;
3763}
3764
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04003765/* initialize crtc */
Lloyd Atkinsonac933642016-09-14 11:52:00 -04003766struct drm_crtc *sde_crtc_init(struct drm_device *dev, struct drm_plane *plane)
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07003767{
3768 struct drm_crtc *crtc = NULL;
Clarence Ip8f7366c2016-07-05 12:15:26 -04003769 struct sde_crtc *sde_crtc = NULL;
3770 struct msm_drm_private *priv = NULL;
3771 struct sde_kms *kms = NULL;
Clarence Ipa18d4832017-03-13 12:35:44 -07003772 int i, rc;
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07003773
Clarence Ip8f7366c2016-07-05 12:15:26 -04003774 priv = dev->dev_private;
3775 kms = to_sde_kms(priv->kms);
3776
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07003777 sde_crtc = kzalloc(sizeof(*sde_crtc), GFP_KERNEL);
3778 if (!sde_crtc)
3779 return ERR_PTR(-ENOMEM);
3780
3781 crtc = &sde_crtc->base;
Gopikrishnaiah Anandane0e5e0c2016-05-25 11:05:33 -07003782 crtc->dev = dev;
Alan Kwong07da0982016-11-04 12:57:45 -04003783 atomic_set(&sde_crtc->vblank_refcount, 0);
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07003784
Clarence Ip7f70ce42017-03-20 06:53:46 -07003785 mutex_init(&sde_crtc->crtc_lock);
Alan Kwong628d19e2016-10-31 13:50:13 -04003786 spin_lock_init(&sde_crtc->spin_lock);
3787 atomic_set(&sde_crtc->frame_pending, 0);
3788
Veera Sundaram Sankaran7ee99092017-06-13 11:19:36 -07003789 init_completion(&sde_crtc->frame_done_comp);
3790
Alan Kwong628d19e2016-10-31 13:50:13 -04003791 INIT_LIST_HEAD(&sde_crtc->frame_event_list);
Gopikrishnaiah Anandande2c81b2017-03-15 12:41:29 -07003792 INIT_LIST_HEAD(&sde_crtc->user_event_list);
Alan Kwong628d19e2016-10-31 13:50:13 -04003793 for (i = 0; i < ARRAY_SIZE(sde_crtc->frame_events); i++) {
3794 INIT_LIST_HEAD(&sde_crtc->frame_events[i].list);
3795 list_add(&sde_crtc->frame_events[i].list,
3796 &sde_crtc->frame_event_list);
3797 kthread_init_work(&sde_crtc->frame_events[i].work,
3798 sde_crtc_frame_event_work);
3799 }
3800
Dhaval Patel04c7e8e2016-09-26 20:14:31 -07003801 drm_crtc_init_with_planes(dev, crtc, plane, NULL, &sde_crtc_funcs,
3802 NULL);
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07003803
3804 drm_crtc_helper_add(crtc, &sde_crtc_helper_funcs);
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04003805 plane->crtc = crtc;
3806
Clarence Ip8f7366c2016-07-05 12:15:26 -04003807 /* save user friendly CRTC name for later */
3808 snprintf(sde_crtc->name, SDE_CRTC_NAME_SIZE, "crtc%u", crtc->base.id);
3809
Clarence Ipa18d4832017-03-13 12:35:44 -07003810 /* initialize event handling */
3811 rc = _sde_crtc_init_events(sde_crtc);
3812 if (rc) {
3813 drm_crtc_cleanup(crtc);
3814 kfree(sde_crtc);
3815 return ERR_PTR(rc);
3816 }
3817
Clarence Ip9a74a442016-08-25 18:29:03 -04003818 /* initialize output fence support */
Lloyd Atkinson5d40d312016-09-06 08:34:13 -04003819 sde_fence_init(&sde_crtc->output_fence, sde_crtc->name, crtc->base.id);
Clarence Ip24f80662016-06-13 19:05:32 -04003820
Clarence Ip7a753bb2016-07-07 11:47:44 -04003821 /* create CRTC properties */
3822 msm_property_init(&sde_crtc->property_info, &crtc->base, dev,
3823 priv->crtc_property, sde_crtc->property_data,
3824 CRTC_PROP_COUNT, CRTC_PROP_BLOBCOUNT,
3825 sizeof(struct sde_crtc_state));
3826
Dhaval Patele4a5dda2016-10-13 19:29:30 -07003827 sde_crtc_install_properties(crtc, kms->catalog);
Gopikrishnaiah Anandan703eb902016-10-06 18:43:57 -07003828
3829 /* Install color processing properties */
Gopikrishnaiah Anandane0e5e0c2016-05-25 11:05:33 -07003830 sde_cp_crtc_init(crtc);
Gopikrishnaiah Anandan703eb902016-10-06 18:43:57 -07003831 sde_cp_crtc_install_properties(crtc);
Clarence Ip7a753bb2016-07-07 11:47:44 -04003832
Dhaval Patelec10fad2016-08-22 14:40:48 -07003833 SDE_DEBUG("%s: successfully initialized crtc\n", sde_crtc->name);
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07003834 return crtc;
3835}
Gopikrishnaiah Anandande2c81b2017-03-15 12:41:29 -07003836
Gopikrishnaiah Anandan5154c712017-02-27 17:48:24 -08003837static int _sde_crtc_event_enable(struct sde_kms *kms,
3838 struct drm_crtc *crtc_drm, u32 event)
Gopikrishnaiah Anandande2c81b2017-03-15 12:41:29 -07003839{
Gopikrishnaiah Anandan5154c712017-02-27 17:48:24 -08003840 struct sde_crtc *crtc = NULL;
3841 struct sde_crtc_irq_info *node;
3842 struct msm_drm_private *priv;
3843 unsigned long flags;
3844 bool found = false;
Gopikrishnaiah Anandanb6b401f2017-03-14 16:39:49 -07003845 int ret, i = 0;
Gopikrishnaiah Anandan5154c712017-02-27 17:48:24 -08003846
3847 crtc = to_sde_crtc(crtc_drm);
3848 spin_lock_irqsave(&crtc->spin_lock, flags);
3849 list_for_each_entry(node, &crtc->user_event_list, list) {
3850 if (node->event == event) {
3851 found = true;
3852 break;
3853 }
3854 }
3855 spin_unlock_irqrestore(&crtc->spin_lock, flags);
3856
3857 /* event already enabled */
3858 if (found)
3859 return 0;
3860
Gopikrishnaiah Anandanb6b401f2017-03-14 16:39:49 -07003861 node = NULL;
3862 for (i = 0; i < ARRAY_SIZE(custom_events); i++) {
3863 if (custom_events[i].event == event &&
3864 custom_events[i].func) {
3865 node = kzalloc(sizeof(*node), GFP_KERNEL);
3866 if (!node)
3867 return -ENOMEM;
3868 node->event = event;
3869 INIT_LIST_HEAD(&node->list);
3870 node->func = custom_events[i].func;
3871 node->event = event;
3872 break;
3873 }
3874 }
Gopikrishnaiah Anandan5154c712017-02-27 17:48:24 -08003875
Gopikrishnaiah Anandanb6b401f2017-03-14 16:39:49 -07003876 if (!node) {
Gopikrishnaiah Anandan5154c712017-02-27 17:48:24 -08003877 SDE_ERROR("unsupported event %x\n", event);
Gopikrishnaiah Anandan5154c712017-02-27 17:48:24 -08003878 return -EINVAL;
3879 }
3880
3881 priv = kms->dev->dev_private;
3882 ret = 0;
3883 if (crtc_drm->enabled) {
3884 sde_power_resource_enable(&priv->phandle, kms->core_client,
3885 true);
3886 ret = node->func(crtc_drm, true, &node->irq);
3887 sde_power_resource_enable(&priv->phandle, kms->core_client,
3888 false);
3889 }
3890
3891 if (!ret) {
3892 spin_lock_irqsave(&crtc->spin_lock, flags);
3893 list_add_tail(&node->list, &crtc->user_event_list);
3894 spin_unlock_irqrestore(&crtc->spin_lock, flags);
3895 } else {
3896 kfree(node);
3897 }
3898
3899 return ret;
3900}
3901
3902static int _sde_crtc_event_disable(struct sde_kms *kms,
3903 struct drm_crtc *crtc_drm, u32 event)
3904{
3905 struct sde_crtc *crtc = NULL;
3906 struct sde_crtc_irq_info *node = NULL;
3907 struct msm_drm_private *priv;
3908 unsigned long flags;
3909 bool found = false;
3910 int ret;
3911
3912 crtc = to_sde_crtc(crtc_drm);
3913 spin_lock_irqsave(&crtc->spin_lock, flags);
3914 list_for_each_entry(node, &crtc->user_event_list, list) {
3915 if (node->event == event) {
3916 list_del(&node->list);
3917 found = true;
3918 break;
3919 }
3920 }
3921 spin_unlock_irqrestore(&crtc->spin_lock, flags);
3922
3923 /* event already disabled */
3924 if (!found)
3925 return 0;
3926
3927 /**
3928 * crtc is disabled interrupts are cleared remove from the list,
3929 * no need to disable/de-register.
3930 */
3931 if (!crtc_drm->enabled) {
3932 kfree(node);
3933 return 0;
3934 }
3935 priv = kms->dev->dev_private;
3936 sde_power_resource_enable(&priv->phandle, kms->core_client, true);
3937 ret = node->func(crtc_drm, false, &node->irq);
3938 sde_power_resource_enable(&priv->phandle, kms->core_client, false);
3939 return ret;
3940}
3941
3942int sde_crtc_register_custom_event(struct sde_kms *kms,
3943 struct drm_crtc *crtc_drm, u32 event, bool en)
3944{
3945 struct sde_crtc *crtc = NULL;
3946 int ret;
3947
3948 crtc = to_sde_crtc(crtc_drm);
3949 if (!crtc || !kms || !kms->dev) {
3950 DRM_ERROR("invalid sde_crtc %pK kms %pK dev %pK\n", crtc,
3951 kms, ((kms) ? (kms->dev) : NULL));
3952 return -EINVAL;
3953 }
3954
3955 if (en)
3956 ret = _sde_crtc_event_enable(kms, crtc_drm, event);
3957 else
3958 ret = _sde_crtc_event_disable(kms, crtc_drm, event);
3959
3960 return ret;
Gopikrishnaiah Anandande2c81b2017-03-15 12:41:29 -07003961}
Gopikrishnaiah Anandan84b4f672017-04-26 10:28:51 -07003962
3963static int sde_crtc_power_interrupt_handler(struct drm_crtc *crtc_drm,
3964 bool en, struct sde_irq_callback *irq)
3965{
3966 return 0;
3967}