blob: b4c413f95b8bdf6ee974036420286d73459ae15c [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
Benjamin Chan90139102017-06-21 16:00:39 -040058static int sde_crtc_pm_event_handler(struct drm_crtc *crtc_drm,
59 bool en, struct sde_irq_callback *noirq);
60
Gopikrishnaiah Anandanb6b401f2017-03-14 16:39:49 -070061static struct sde_crtc_custom_events custom_events[] = {
Gopikrishnaiah Anandan84b4f672017-04-26 10:28:51 -070062 {DRM_EVENT_AD_BACKLIGHT, sde_cp_ad_interrupt},
Benjamin Chan90139102017-06-21 16:00:39 -040063 {DRM_EVENT_CRTC_POWER, sde_crtc_power_interrupt_handler},
64 {DRM_EVENT_SDE_POWER, sde_crtc_pm_event_handler},
Gopikrishnaiah Anandanb6b401f2017-03-14 16:39:49 -070065};
66
Clarence Ipcae1bb62016-07-07 12:07:13 -040067/* default input fence timeout, in ms */
68#define SDE_CRTC_INPUT_FENCE_TIMEOUT 2000
69
Dhaval Patel4e574842016-08-23 15:11:37 -070070/*
71 * The default input fence timeout is 2 seconds while max allowed
72 * range is 10 seconds. Any value above 10 seconds adds glitches beyond
73 * tolerance limit.
74 */
75#define SDE_CRTC_MAX_INPUT_FENCE_TIMEOUT 10000
76
Dhaval Patel48c76022016-09-01 17:51:23 -070077/* layer mixer index on sde_crtc */
78#define LEFT_MIXER 0
79#define RIGHT_MIXER 1
80
Dhaval Patelf9245d62017-03-28 16:24:00 -070081#define MISR_BUFF_SIZE 256
82
Lloyd Atkinson4f1c8692016-09-14 14:04:25 -040083static inline struct sde_kms *_sde_crtc_get_kms(struct drm_crtc *crtc)
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -040084{
Clarence Ip7f70ce42017-03-20 06:53:46 -070085 struct msm_drm_private *priv;
86
87 if (!crtc || !crtc->dev || !crtc->dev->dev_private) {
88 SDE_ERROR("invalid crtc\n");
89 return NULL;
90 }
91 priv = crtc->dev->dev_private;
92 if (!priv || !priv->kms) {
93 SDE_ERROR("invalid kms\n");
94 return NULL;
95 }
Abhijit Kulkarni40e38162016-06-26 22:12:09 -040096
Ben Chan78647cd2016-06-26 22:02:47 -040097 return to_sde_kms(priv->kms);
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -040098}
99
Dhaval Patelf9245d62017-03-28 16:24:00 -0700100static inline int _sde_crtc_power_enable(struct sde_crtc *sde_crtc, bool enable)
101{
102 struct drm_crtc *crtc;
103 struct msm_drm_private *priv;
104 struct sde_kms *sde_kms;
105
106 if (!sde_crtc) {
107 SDE_ERROR("invalid sde crtc\n");
108 return -EINVAL;
109 }
110
111 crtc = &sde_crtc->base;
112 if (!crtc->dev || !crtc->dev->dev_private) {
113 SDE_ERROR("invalid drm device\n");
114 return -EINVAL;
115 }
116
117 priv = crtc->dev->dev_private;
118 if (!priv->kms) {
119 SDE_ERROR("invalid kms\n");
120 return -EINVAL;
121 }
122
123 sde_kms = to_sde_kms(priv->kms);
124
125 return sde_power_resource_enable(&priv->phandle, sde_kms->core_client,
126 enable);
127}
128
Alan Kwongcdb2f282017-03-18 13:42:06 -0700129/**
130 * _sde_crtc_rp_to_crtc - get crtc from resource pool object
131 * @rp: Pointer to resource pool
132 * return: Pointer to drm crtc if success; null otherwise
133 */
134static struct drm_crtc *_sde_crtc_rp_to_crtc(struct sde_crtc_respool *rp)
135{
136 if (!rp)
137 return NULL;
138
139 return container_of(rp, struct sde_crtc_state, rp)->base.crtc;
140}
141
142/**
143 * _sde_crtc_rp_reclaim - reclaim unused, or all if forced, resources in pool
144 * @rp: Pointer to resource pool
145 * @force: True to reclaim all resources; otherwise, reclaim only unused ones
146 * return: None
147 */
148static void _sde_crtc_rp_reclaim(struct sde_crtc_respool *rp, bool force)
149{
150 struct sde_crtc_res *res, *next;
151 struct drm_crtc *crtc;
152
153 crtc = _sde_crtc_rp_to_crtc(rp);
154 if (!crtc) {
155 SDE_ERROR("invalid crtc\n");
156 return;
157 }
158
159 SDE_DEBUG("crtc%d.%u %s\n", crtc->base.id, rp->sequence_id,
160 force ? "destroy" : "free_unused");
161
162 list_for_each_entry_safe(res, next, &rp->res_list, list) {
163 if (!force && !(res->flags & SDE_CRTC_RES_FLAG_FREE))
164 continue;
165 SDE_DEBUG("crtc%d.%u reclaim res:0x%x/0x%llx/%pK/%d\n",
166 crtc->base.id, rp->sequence_id,
167 res->type, res->tag, res->val,
168 atomic_read(&res->refcount));
169 list_del(&res->list);
170 if (res->ops.put)
171 res->ops.put(res->val);
172 kfree(res);
173 }
174}
175
176/**
177 * _sde_crtc_rp_free_unused - free unused resource in pool
178 * @rp: Pointer to resource pool
179 * return: none
180 */
181static void _sde_crtc_rp_free_unused(struct sde_crtc_respool *rp)
182{
183 _sde_crtc_rp_reclaim(rp, false);
184}
185
186/**
187 * _sde_crtc_rp_destroy - destroy resource pool
188 * @rp: Pointer to resource pool
189 * return: None
190 */
191static void _sde_crtc_rp_destroy(struct sde_crtc_respool *rp)
192{
193 _sde_crtc_rp_reclaim(rp, true);
194}
195
196/**
197 * _sde_crtc_hw_blk_get - get callback for hardware block
198 * @val: Resource handle
199 * @type: Resource type
200 * @tag: Search tag for given resource
201 * return: Resource handle
202 */
203static void *_sde_crtc_hw_blk_get(void *val, u32 type, u64 tag)
204{
205 SDE_DEBUG("res:%d/0x%llx/%pK\n", type, tag, val);
206 return sde_hw_blk_get(val, type, tag);
207}
208
209/**
210 * _sde_crtc_hw_blk_put - put callback for hardware block
211 * @val: Resource handle
212 * return: None
213 */
214static void _sde_crtc_hw_blk_put(void *val)
215{
216 SDE_DEBUG("res://%pK\n", val);
217 sde_hw_blk_put(val);
218}
219
220/**
221 * _sde_crtc_rp_duplicate - duplicate resource pool and reset reference count
222 * @rp: Pointer to original resource pool
223 * @dup_rp: Pointer to duplicated resource pool
224 * return: None
225 */
226static void _sde_crtc_rp_duplicate(struct sde_crtc_respool *rp,
227 struct sde_crtc_respool *dup_rp)
228{
229 struct sde_crtc_res *res, *dup_res;
230 struct drm_crtc *crtc;
231
232 if (!rp || !dup_rp) {
233 SDE_ERROR("invalid resource pool\n");
234 return;
235 }
236
237 crtc = _sde_crtc_rp_to_crtc(rp);
238 if (!crtc) {
239 SDE_ERROR("invalid crtc\n");
240 return;
241 }
242
243 SDE_DEBUG("crtc%d.%u duplicate\n", crtc->base.id, rp->sequence_id);
244
245 dup_rp->sequence_id = rp->sequence_id + 1;
246 INIT_LIST_HEAD(&dup_rp->res_list);
247 dup_rp->ops = rp->ops;
248 list_for_each_entry(res, &rp->res_list, list) {
249 dup_res = kzalloc(sizeof(struct sde_crtc_res), GFP_KERNEL);
250 if (!dup_res)
251 return;
252 INIT_LIST_HEAD(&dup_res->list);
253 atomic_set(&dup_res->refcount, 0);
254 dup_res->type = res->type;
255 dup_res->tag = res->tag;
256 dup_res->val = res->val;
257 dup_res->ops = res->ops;
258 dup_res->flags = SDE_CRTC_RES_FLAG_FREE;
259 SDE_DEBUG("crtc%d.%u dup res:0x%x/0x%llx/%pK/%d\n",
260 crtc->base.id, dup_rp->sequence_id,
261 dup_res->type, dup_res->tag, dup_res->val,
262 atomic_read(&dup_res->refcount));
263 list_add_tail(&dup_res->list, &dup_rp->res_list);
264 if (dup_res->ops.get)
265 dup_res->ops.get(dup_res->val, 0, -1);
266 }
267}
268
269/**
270 * _sde_crtc_rp_reset - reset resource pool after allocation
271 * @rp: Pointer to original resource pool
272 * return: None
273 */
274static void _sde_crtc_rp_reset(struct sde_crtc_respool *rp)
275{
276 if (!rp) {
277 SDE_ERROR("invalid resource pool\n");
278 return;
279 }
280
281 rp->sequence_id = 0;
282 INIT_LIST_HEAD(&rp->res_list);
283 rp->ops.get = _sde_crtc_hw_blk_get;
284 rp->ops.put = _sde_crtc_hw_blk_put;
285}
286
287/**
288 * _sde_crtc_rp_add - add given resource to resource pool
289 * @rp: Pointer to original resource pool
290 * @type: Resource type
291 * @tag: Search tag for given resource
292 * @val: Resource handle
293 * @ops: Resource callback operations
294 * return: 0 if success; error code otherwise
295 */
296static int _sde_crtc_rp_add(struct sde_crtc_respool *rp, u32 type, u64 tag,
297 void *val, struct sde_crtc_res_ops *ops)
298{
299 struct sde_crtc_res *res;
300 struct drm_crtc *crtc;
301
302 if (!rp || !ops) {
303 SDE_ERROR("invalid resource pool/ops\n");
304 return -EINVAL;
305 }
306
307 crtc = _sde_crtc_rp_to_crtc(rp);
308 if (!crtc) {
309 SDE_ERROR("invalid crtc\n");
310 return -EINVAL;
311 }
312
313 list_for_each_entry(res, &rp->res_list, list) {
314 if (res->type != type || res->tag != tag)
315 continue;
316 SDE_ERROR("crtc%d.%u already exist res:0x%x/0x%llx/%pK/%d\n",
317 crtc->base.id, rp->sequence_id,
318 res->type, res->tag, res->val,
319 atomic_read(&res->refcount));
320 return -EEXIST;
321 }
322 res = kzalloc(sizeof(struct sde_crtc_res), GFP_KERNEL);
323 if (!res)
324 return -ENOMEM;
325 INIT_LIST_HEAD(&res->list);
326 atomic_set(&res->refcount, 1);
327 res->type = type;
328 res->tag = tag;
329 res->val = val;
330 res->ops = *ops;
331 list_add_tail(&res->list, &rp->res_list);
332 SDE_DEBUG("crtc%d.%u added res:0x%x/0x%llx\n",
333 crtc->base.id, rp->sequence_id, type, tag);
334 return 0;
335}
336
337/**
338 * _sde_crtc_rp_get - lookup the resource from given resource pool and obtain
339 * if available; otherwise, obtain resource from global pool
340 * @rp: Pointer to original resource pool
341 * @type: Resource type
342 * @tag: Search tag for given resource
343 * return: Resource handle if success; pointer error or null otherwise
344 */
345static void *_sde_crtc_rp_get(struct sde_crtc_respool *rp, u32 type, u64 tag)
346{
347 struct sde_crtc_res *res;
348 void *val = NULL;
349 int rc;
350 struct drm_crtc *crtc;
351
352 if (!rp) {
353 SDE_ERROR("invalid resource pool\n");
354 return NULL;
355 }
356
357 crtc = _sde_crtc_rp_to_crtc(rp);
358 if (!crtc) {
359 SDE_ERROR("invalid crtc\n");
360 return NULL;
361 }
362
363 list_for_each_entry(res, &rp->res_list, list) {
364 if (res->type != type || res->tag != tag)
365 continue;
366 SDE_DEBUG("crtc%d.%u found res:0x%x/0x%llx/%pK/%d\n",
367 crtc->base.id, rp->sequence_id,
368 res->type, res->tag, res->val,
369 atomic_read(&res->refcount));
370 atomic_inc(&res->refcount);
371 res->flags &= ~SDE_CRTC_RES_FLAG_FREE;
372 return res->val;
373 }
374 list_for_each_entry(res, &rp->res_list, list) {
375 if (res->type != type || !(res->flags & SDE_CRTC_RES_FLAG_FREE))
376 continue;
377 SDE_DEBUG("crtc%d.%u retag res:0x%x/0x%llx/%pK/%d\n",
378 crtc->base.id, rp->sequence_id,
379 res->type, res->tag, res->val,
380 atomic_read(&res->refcount));
381 atomic_inc(&res->refcount);
382 res->tag = tag;
383 res->flags &= ~SDE_CRTC_RES_FLAG_FREE;
384 return res->val;
385 }
386 if (rp->ops.get)
387 val = rp->ops.get(NULL, type, -1);
388 if (IS_ERR_OR_NULL(val)) {
Alan Kwong42e35052017-05-05 06:52:51 -0700389 SDE_DEBUG("crtc%d.%u failed to get res:0x%x//\n",
Alan Kwongcdb2f282017-03-18 13:42:06 -0700390 crtc->base.id, rp->sequence_id, type);
391 return NULL;
392 }
393 rc = _sde_crtc_rp_add(rp, type, tag, val, &rp->ops);
394 if (rc) {
395 SDE_ERROR("crtc%d.%u failed to add res:0x%x/0x%llx\n",
396 crtc->base.id, rp->sequence_id, type, tag);
397 if (rp->ops.put)
398 rp->ops.put(val);
399 val = NULL;
400 }
401 return val;
402}
403
404/**
405 * _sde_crtc_rp_put - return given resource to resource pool
406 * @rp: Pointer to original resource pool
407 * @type: Resource type
408 * @tag: Search tag for given resource
409 * return: None
410 */
411static void _sde_crtc_rp_put(struct sde_crtc_respool *rp, u32 type, u64 tag)
412{
413 struct sde_crtc_res *res, *next;
414 struct drm_crtc *crtc;
415
416 if (!rp) {
417 SDE_ERROR("invalid resource pool\n");
418 return;
419 }
420
421 crtc = _sde_crtc_rp_to_crtc(rp);
422 if (!crtc) {
423 SDE_ERROR("invalid crtc\n");
424 return;
425 }
426
427 list_for_each_entry_safe(res, next, &rp->res_list, list) {
428 if (res->type != type || res->tag != tag)
429 continue;
430 SDE_DEBUG("crtc%d.%u found res:0x%x/0x%llx/%pK/%d\n",
431 crtc->base.id, rp->sequence_id,
432 res->type, res->tag, res->val,
433 atomic_read(&res->refcount));
434 if (res->flags & SDE_CRTC_RES_FLAG_FREE)
435 SDE_ERROR(
436 "crtc%d.%u already free res:0x%x/0x%llx/%pK/%d\n",
437 crtc->base.id, rp->sequence_id,
438 res->type, res->tag, res->val,
439 atomic_read(&res->refcount));
440 else if (atomic_dec_return(&res->refcount) == 0)
441 res->flags |= SDE_CRTC_RES_FLAG_FREE;
442
443 return;
444 }
445 SDE_ERROR("crtc%d.%u not found res:0x%x/0x%llx\n",
446 crtc->base.id, rp->sequence_id, type, tag);
447}
448
449int sde_crtc_res_add(struct drm_crtc_state *state, u32 type, u64 tag,
450 void *val, struct sde_crtc_res_ops *ops)
451{
452 struct sde_crtc_respool *rp;
453
454 if (!state) {
455 SDE_ERROR("invalid parameters\n");
456 return -EINVAL;
457 }
458
459 rp = &to_sde_crtc_state(state)->rp;
460 return _sde_crtc_rp_add(rp, type, tag, val, ops);
461}
462
463void *sde_crtc_res_get(struct drm_crtc_state *state, u32 type, u64 tag)
464{
465 struct sde_crtc_respool *rp;
466 void *val;
467
468 if (!state) {
469 SDE_ERROR("invalid parameters\n");
470 return NULL;
471 }
472
473 rp = &to_sde_crtc_state(state)->rp;
474 val = _sde_crtc_rp_get(rp, type, tag);
475 if (IS_ERR(val)) {
476 SDE_ERROR("failed to get res type:0x%x:0x%llx\n",
477 type, tag);
478 return NULL;
479 }
480
481 return val;
482}
483
484void sde_crtc_res_put(struct drm_crtc_state *state, u32 type, u64 tag)
485{
486 struct sde_crtc_respool *rp;
487
488 if (!state) {
489 SDE_ERROR("invalid parameters\n");
490 return;
491 }
492
493 rp = &to_sde_crtc_state(state)->rp;
494 _sde_crtc_rp_put(rp, type, tag);
495}
496
Clarence Ipa18d4832017-03-13 12:35:44 -0700497static void _sde_crtc_deinit_events(struct sde_crtc *sde_crtc)
498{
499 if (!sde_crtc)
500 return;
Clarence Ipa18d4832017-03-13 12:35:44 -0700501}
502
Narendra Muppalla1b0b3352015-09-29 10:16:51 -0700503static void sde_crtc_destroy(struct drm_crtc *crtc)
504{
505 struct sde_crtc *sde_crtc = to_sde_crtc(crtc);
506
Lloyd Atkinson4f1c8692016-09-14 14:04:25 -0400507 SDE_DEBUG("\n");
Clarence Ip7a753bb2016-07-07 11:47:44 -0400508
509 if (!crtc)
510 return;
511
Dhaval Patele4a5dda2016-10-13 19:29:30 -0700512 if (sde_crtc->blob_info)
513 drm_property_unreference_blob(sde_crtc->blob_info);
Clarence Ip7a753bb2016-07-07 11:47:44 -0400514 msm_property_destroy(&sde_crtc->property_info);
Gopikrishnaiah Anandane0e5e0c2016-05-25 11:05:33 -0700515 sde_cp_crtc_destroy_properties(crtc);
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -0700516
Clarence Ip24f80662016-06-13 19:05:32 -0400517 sde_fence_deinit(&sde_crtc->output_fence);
Clarence Ipa18d4832017-03-13 12:35:44 -0700518 _sde_crtc_deinit_events(sde_crtc);
Clarence Ip7a753bb2016-07-07 11:47:44 -0400519
Narendra Muppalla1b0b3352015-09-29 10:16:51 -0700520 drm_crtc_cleanup(crtc);
Clarence Ip7f70ce42017-03-20 06:53:46 -0700521 mutex_destroy(&sde_crtc->crtc_lock);
Narendra Muppalla1b0b3352015-09-29 10:16:51 -0700522 kfree(sde_crtc);
523}
524
Narendra Muppalla1b0b3352015-09-29 10:16:51 -0700525static bool sde_crtc_mode_fixup(struct drm_crtc *crtc,
526 const struct drm_display_mode *mode,
527 struct drm_display_mode *adjusted_mode)
528{
Lloyd Atkinson4f1c8692016-09-14 14:04:25 -0400529 SDE_DEBUG("\n");
Lloyd Atkinsonaf7952d2016-06-26 22:41:26 -0400530
Lloyd Atkinson4f1c8692016-09-14 14:04:25 -0400531 if (msm_is_mode_seamless(adjusted_mode) &&
532 (!crtc->enabled || crtc->state->active_changed)) {
533 SDE_ERROR("crtc state prevents seamless transition\n");
534 return false;
Lloyd Atkinsonaf7952d2016-06-26 22:41:26 -0400535 }
536
Narendra Muppalla1b0b3352015-09-29 10:16:51 -0700537 return true;
538}
539
Dhaval Patel48c76022016-09-01 17:51:23 -0700540static void _sde_crtc_setup_blend_cfg(struct sde_crtc_mixer *mixer,
541 struct sde_plane_state *pstate, struct sde_format *format)
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -0400542{
Dhaval Patel48c76022016-09-01 17:51:23 -0700543 uint32_t blend_op, fg_alpha, bg_alpha;
544 uint32_t blend_type;
Dhaval Patel44f12472016-08-29 12:19:47 -0700545 struct sde_hw_mixer *lm = mixer->hw_lm;
546
Dhaval Patel48c76022016-09-01 17:51:23 -0700547 /* default to opaque blending */
548 fg_alpha = sde_plane_get_property(pstate, PLANE_PROP_ALPHA);
549 bg_alpha = 0xFF - fg_alpha;
550 blend_op = SDE_BLEND_FG_ALPHA_FG_CONST | SDE_BLEND_BG_ALPHA_BG_CONST;
551 blend_type = sde_plane_get_property(pstate, PLANE_PROP_BLEND_OP);
Dhaval Patel44f12472016-08-29 12:19:47 -0700552
Dhaval Patel48c76022016-09-01 17:51:23 -0700553 SDE_DEBUG("blend type:0x%x blend alpha:0x%x\n", blend_type, fg_alpha);
554
555 switch (blend_type) {
556
557 case SDE_DRM_BLEND_OP_OPAQUE:
558 blend_op = SDE_BLEND_FG_ALPHA_FG_CONST |
559 SDE_BLEND_BG_ALPHA_BG_CONST;
560 break;
561
562 case SDE_DRM_BLEND_OP_PREMULTIPLIED:
563 if (format->alpha_enable) {
564 blend_op = SDE_BLEND_FG_ALPHA_FG_CONST |
565 SDE_BLEND_BG_ALPHA_FG_PIXEL;
566 if (fg_alpha != 0xff) {
567 bg_alpha = fg_alpha;
568 blend_op |= SDE_BLEND_BG_MOD_ALPHA |
569 SDE_BLEND_BG_INV_MOD_ALPHA;
570 } else {
571 blend_op |= SDE_BLEND_BG_INV_ALPHA;
572 }
573 }
574 break;
575
576 case SDE_DRM_BLEND_OP_COVERAGE:
577 if (format->alpha_enable) {
578 blend_op = SDE_BLEND_FG_ALPHA_FG_PIXEL |
579 SDE_BLEND_BG_ALPHA_FG_PIXEL;
580 if (fg_alpha != 0xff) {
581 bg_alpha = fg_alpha;
582 blend_op |= SDE_BLEND_FG_MOD_ALPHA |
583 SDE_BLEND_FG_INV_MOD_ALPHA |
584 SDE_BLEND_BG_MOD_ALPHA |
585 SDE_BLEND_BG_INV_MOD_ALPHA;
586 } else {
587 blend_op |= SDE_BLEND_BG_INV_ALPHA;
588 }
589 }
590 break;
591 default:
592 /* do nothing */
593 break;
Clarence Ipd9f9fa62016-09-09 13:42:32 -0400594 }
Dhaval Patel48c76022016-09-01 17:51:23 -0700595
596 lm->ops.setup_blend_config(lm, pstate->stage, fg_alpha,
597 bg_alpha, blend_op);
Dhaval Patel6c666622017-03-21 23:02:59 -0700598 SDE_DEBUG(
599 "format: %4.4s, alpha_enable %u fg alpha:0x%x bg alpha:0x%x blend_op:0x%x\n",
600 (char *) &format->base.pixel_format,
Dhaval Patel48c76022016-09-01 17:51:23 -0700601 format->alpha_enable, fg_alpha, bg_alpha, blend_op);
602}
603
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -0800604static void _sde_crtc_setup_dim_layer_cfg(struct drm_crtc *crtc,
605 struct sde_crtc *sde_crtc, struct sde_crtc_mixer *mixer,
606 struct sde_hw_dim_layer *dim_layer)
607{
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -0500608 struct sde_crtc_state *cstate;
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -0800609 struct sde_hw_mixer *lm;
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -0800610 struct sde_hw_dim_layer split_dim_layer;
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -0800611 int i;
612
613 if (!dim_layer->rect.w || !dim_layer->rect.h) {
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -0700614 SDE_DEBUG("empty dim_layer\n");
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -0800615 return;
616 }
617
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -0500618 cstate = to_sde_crtc_state(crtc->state);
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -0800619
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -0700620 SDE_DEBUG("dim_layer - flags:%d, stage:%d\n",
621 dim_layer->flags, dim_layer->stage);
622
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -0800623 split_dim_layer.stage = dim_layer->stage;
624 split_dim_layer.color_fill = dim_layer->color_fill;
625
626 /*
627 * traverse through the layer mixers attached to crtc and find the
628 * intersecting dim layer rect in each LM and program accordingly.
629 */
630 for (i = 0; i < sde_crtc->num_mixers; i++) {
631 split_dim_layer.flags = dim_layer->flags;
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -0800632
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -0500633 sde_kms_rect_intersect(&cstate->lm_bounds[i], &dim_layer->rect,
Lloyd Atkinsone0e11e22017-01-17 12:08:48 -0500634 &split_dim_layer.rect);
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -0500635 if (sde_kms_rect_is_null(&split_dim_layer.rect)) {
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -0800636 /*
637 * no extra programming required for non-intersecting
638 * layer mixers with INCLUSIVE dim layer
639 */
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -0500640 if (split_dim_layer.flags & SDE_DRM_DIM_LAYER_INCLUSIVE)
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -0800641 continue;
642
643 /*
644 * program the other non-intersecting layer mixers with
645 * INCLUSIVE dim layer of full size for uniformity
646 * with EXCLUSIVE dim layer config.
647 */
648 split_dim_layer.flags &= ~SDE_DRM_DIM_LAYER_EXCLUSIVE;
649 split_dim_layer.flags |= SDE_DRM_DIM_LAYER_INCLUSIVE;
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -0500650 memcpy(&split_dim_layer.rect, &cstate->lm_bounds[i],
651 sizeof(split_dim_layer.rect));
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -0800652
653 } else {
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -0500654 split_dim_layer.rect.x =
655 split_dim_layer.rect.x -
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -0700656 cstate->lm_bounds[i].x;
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -0800657 }
658
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -0700659 SDE_DEBUG("split_dim_layer - LM:%d, rect:{%d,%d,%d,%d}}\n",
660 i, split_dim_layer.rect.x, split_dim_layer.rect.y,
661 split_dim_layer.rect.w, split_dim_layer.rect.h);
662
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -0800663 lm = mixer[i].hw_lm;
664 mixer[i].mixer_op_mode |= 1 << split_dim_layer.stage;
665 lm->ops.setup_dim_layer(lm, &split_dim_layer);
666 }
667}
668
Lloyd Atkinson8ba47032017-03-22 17:13:32 -0400669void sde_crtc_get_crtc_roi(struct drm_crtc_state *state,
670 const struct sde_rect **crtc_roi)
671{
672 struct sde_crtc_state *crtc_state;
673
674 if (!state || !crtc_roi)
675 return;
676
677 crtc_state = to_sde_crtc_state(state);
678 *crtc_roi = &crtc_state->crtc_roi;
679}
680
681static int _sde_crtc_set_roi_v1(struct drm_crtc_state *state,
682 void *usr_ptr)
683{
684 struct drm_crtc *crtc;
685 struct sde_crtc_state *cstate;
686 struct sde_drm_roi_v1 roi_v1;
687 int i;
688
689 if (!state) {
690 SDE_ERROR("invalid args\n");
691 return -EINVAL;
692 }
693
694 cstate = to_sde_crtc_state(state);
695 crtc = cstate->base.crtc;
696
697 memset(&cstate->user_roi_list, 0, sizeof(cstate->user_roi_list));
698
699 if (!usr_ptr) {
700 SDE_DEBUG("crtc%d: rois cleared\n", DRMID(crtc));
701 return 0;
702 }
703
704 if (copy_from_user(&roi_v1, usr_ptr, sizeof(roi_v1))) {
705 SDE_ERROR("crtc%d: failed to copy roi_v1 data\n", DRMID(crtc));
706 return -EINVAL;
707 }
708
709 SDE_DEBUG("crtc%d: num_rects %d\n", DRMID(crtc), roi_v1.num_rects);
710
711 if (roi_v1.num_rects == 0) {
712 SDE_DEBUG("crtc%d: rois cleared\n", DRMID(crtc));
713 return 0;
714 }
715
716 if (roi_v1.num_rects > SDE_MAX_ROI_V1) {
717 SDE_ERROR("crtc%d: too many rects specified: %d\n", DRMID(crtc),
718 roi_v1.num_rects);
719 return -EINVAL;
720 }
721
722 cstate->user_roi_list.num_rects = roi_v1.num_rects;
723 for (i = 0; i < roi_v1.num_rects; ++i) {
724 cstate->user_roi_list.roi[i] = roi_v1.roi[i];
725 SDE_DEBUG("crtc%d: roi%d: roi (%d,%d) (%d,%d)\n",
726 DRMID(crtc), i,
727 cstate->user_roi_list.roi[i].x1,
728 cstate->user_roi_list.roi[i].y1,
729 cstate->user_roi_list.roi[i].x2,
730 cstate->user_roi_list.roi[i].y2);
731 }
732
733 return 0;
734}
735
Ingrid Gallardo83532222017-06-02 16:48:51 -0700736static bool _sde_crtc_setup_is_3dmux_dsc(struct drm_crtc_state *state)
737{
738 int i;
739 struct sde_crtc_state *cstate;
740 bool is_3dmux_dsc = false;
741
742 cstate = to_sde_crtc_state(state);
743
744 for (i = 0; i < cstate->num_connectors; i++) {
745 struct drm_connector *conn = cstate->connectors[i];
746
747 if (sde_connector_get_topology_name(conn) ==
748 SDE_RM_TOPOLOGY_DUALPIPE_3DMERGE_DSC)
749 is_3dmux_dsc = true;
750 }
751
752 return is_3dmux_dsc;
753}
754
Lloyd Atkinson8ba47032017-03-22 17:13:32 -0400755static int _sde_crtc_set_crtc_roi(struct drm_crtc *crtc,
756 struct drm_crtc_state *state)
757{
758 struct drm_connector *conn;
759 struct drm_connector_state *conn_state;
760 struct sde_crtc *sde_crtc;
761 struct sde_crtc_state *crtc_state;
762 struct sde_rect *crtc_roi;
Lloyd Atkinson8ba47032017-03-22 17:13:32 -0400763 int i, num_attached_conns = 0;
764
765 if (!crtc || !state)
766 return -EINVAL;
767
768 sde_crtc = to_sde_crtc(crtc);
769 crtc_state = to_sde_crtc_state(state);
770 crtc_roi = &crtc_state->crtc_roi;
771
Lloyd Atkinson8ba47032017-03-22 17:13:32 -0400772 for_each_connector_in_state(state->state, conn, conn_state, i) {
773 struct sde_connector_state *sde_conn_state;
774
775 if (!conn_state || conn_state->crtc != crtc)
776 continue;
777
778 if (num_attached_conns) {
779 SDE_ERROR(
780 "crtc%d: unsupported: roi on crtc w/ >1 connectors\n",
781 DRMID(crtc));
782 return -EINVAL;
783 }
784 ++num_attached_conns;
785
786 sde_conn_state = to_sde_connector_state(conn_state);
787
Ingrid Gallardo83532222017-06-02 16:48:51 -0700788 /*
789 * current driver only supports same connector and crtc size,
790 * but if support for different sizes is added, driver needs
791 * to check the connector roi here to make sure is full screen
792 * for dsc 3d-mux topology that doesn't support partial update.
793 */
Lloyd Atkinson8ba47032017-03-22 17:13:32 -0400794 if (memcmp(&sde_conn_state->rois, &crtc_state->user_roi_list,
795 sizeof(crtc_state->user_roi_list))) {
796 SDE_ERROR("%s: crtc -> conn roi scaling unsupported\n",
797 sde_crtc->name);
798 return -EINVAL;
799 }
800 }
801
Lloyd Atkinsonc2baf412017-04-19 17:53:09 -0400802 sde_kms_rect_merge_rectangles(&crtc_state->user_roi_list, crtc_roi);
Lloyd Atkinson8ba47032017-03-22 17:13:32 -0400803
Ingrid Gallardo83532222017-06-02 16:48:51 -0700804 /*
805 * for 3dmux dsc, make sure is full ROI, since current driver doesn't
806 * support partial update for this configuration.
807 */
808 if (!sde_kms_rect_is_null(crtc_roi) &&
809 _sde_crtc_setup_is_3dmux_dsc(state)) {
810 struct drm_display_mode *adj_mode = &state->adjusted_mode;
811
812 if (crtc_roi->w != adj_mode->hdisplay ||
813 crtc_roi->h != adj_mode->vdisplay) {
814 SDE_ERROR("%s: unsupported top roi[%d %d] wxh[%d %d]\n",
815 sde_crtc->name, crtc_roi->w, crtc_roi->h,
816 adj_mode->hdisplay, adj_mode->vdisplay);
817 return -EINVAL;
818 }
819 }
820
Lloyd Atkinson8ba47032017-03-22 17:13:32 -0400821 SDE_DEBUG("%s: crtc roi (%d,%d,%d,%d)\n", sde_crtc->name,
822 crtc_roi->x, crtc_roi->y, crtc_roi->w, crtc_roi->h);
823
824 return 0;
825}
826
Lloyd Atkinson77382202017-02-01 14:59:43 -0500827static int _sde_crtc_check_autorefresh(struct drm_crtc *crtc,
828 struct drm_crtc_state *state)
829{
830 struct sde_crtc *sde_crtc;
831 struct sde_crtc_state *crtc_state;
832 struct drm_connector *conn;
833 struct drm_connector_state *conn_state;
834 int i;
835
836 if (!crtc || !state)
837 return -EINVAL;
838
839 sde_crtc = to_sde_crtc(crtc);
840 crtc_state = to_sde_crtc_state(state);
841
842 if (sde_kms_rect_is_null(&crtc_state->crtc_roi))
843 return 0;
844
845 /* partial update active, check if autorefresh is also requested */
846 for_each_connector_in_state(state->state, conn, conn_state, i) {
847 uint64_t autorefresh;
848
849 if (!conn_state || conn_state->crtc != crtc)
850 continue;
851
852 autorefresh = sde_connector_get_property(conn_state,
853 CONNECTOR_PROP_AUTOREFRESH);
854 if (autorefresh) {
855 SDE_ERROR(
856 "%s: autorefresh & partial crtc roi incompatible %llu\n",
857 sde_crtc->name, autorefresh);
858 return -EINVAL;
859 }
860 }
861
862 return 0;
863}
864
Lloyd Atkinson8ba47032017-03-22 17:13:32 -0400865static int _sde_crtc_set_lm_roi(struct drm_crtc *crtc,
866 struct drm_crtc_state *state, int lm_idx)
867{
868 struct sde_crtc *sde_crtc;
869 struct sde_crtc_state *crtc_state;
870 const struct sde_rect *crtc_roi;
871 const struct sde_rect *lm_bounds;
872 struct sde_rect *lm_roi;
873
874 if (!crtc || !state || lm_idx >= ARRAY_SIZE(crtc_state->lm_bounds))
875 return -EINVAL;
876
877 sde_crtc = to_sde_crtc(crtc);
878 crtc_state = to_sde_crtc_state(state);
879 crtc_roi = &crtc_state->crtc_roi;
880 lm_bounds = &crtc_state->lm_bounds[lm_idx];
881 lm_roi = &crtc_state->lm_roi[lm_idx];
882
Lloyd Atkinson73fb8092017-02-08 16:02:55 -0500883 if (sde_kms_rect_is_null(crtc_roi))
Lloyd Atkinson8ba47032017-03-22 17:13:32 -0400884 memcpy(lm_roi, lm_bounds, sizeof(*lm_roi));
Lloyd Atkinson73fb8092017-02-08 16:02:55 -0500885 else
886 sde_kms_rect_intersect(crtc_roi, lm_bounds, lm_roi);
Lloyd Atkinson8ba47032017-03-22 17:13:32 -0400887
888 SDE_DEBUG("%s: lm%d roi (%d,%d,%d,%d)\n", sde_crtc->name, lm_idx,
889 lm_roi->x, lm_roi->y, lm_roi->w, lm_roi->h);
890
Lloyd Atkinson73fb8092017-02-08 16:02:55 -0500891 /* if any dimension is zero, clear all dimensions for clarity */
892 if (sde_kms_rect_is_null(lm_roi))
893 memset(lm_roi, 0, sizeof(*lm_roi));
894
Lloyd Atkinson8ba47032017-03-22 17:13:32 -0400895 return 0;
896}
897
Lloyd Atkinson73fb8092017-02-08 16:02:55 -0500898static u32 _sde_crtc_get_displays_affected(struct drm_crtc *crtc,
899 struct drm_crtc_state *state)
900{
901 struct sde_crtc *sde_crtc;
902 struct sde_crtc_state *crtc_state;
903 u32 disp_bitmask = 0;
904 int i;
905
906 sde_crtc = to_sde_crtc(crtc);
907 crtc_state = to_sde_crtc_state(state);
908
Lloyd Atkinson66e7dde2017-02-08 15:52:53 -0500909 /* pingpong split: one ROI, one LM, two physical displays */
910 if (crtc_state->is_ppsplit) {
911 u32 lm_split_width = crtc_state->lm_bounds[0].w / 2;
912 struct sde_rect *roi = &crtc_state->lm_roi[0];
913
914 if (sde_kms_rect_is_null(roi))
915 disp_bitmask = 0;
916 else if ((u32)roi->x + (u32)roi->w <= lm_split_width)
917 disp_bitmask = BIT(0); /* left only */
918 else if (roi->x >= lm_split_width)
919 disp_bitmask = BIT(1); /* right only */
920 else
921 disp_bitmask = BIT(0) | BIT(1); /* left and right */
922 } else {
923 for (i = 0; i < sde_crtc->num_mixers; i++) {
924 if (!sde_kms_rect_is_null(&crtc_state->lm_roi[i]))
925 disp_bitmask |= BIT(i);
926 }
Lloyd Atkinson73fb8092017-02-08 16:02:55 -0500927 }
928
929 SDE_DEBUG("affected displays 0x%x\n", disp_bitmask);
930
931 return disp_bitmask;
932}
933
Lloyd Atkinson8ba47032017-03-22 17:13:32 -0400934static int _sde_crtc_check_rois_centered_and_symmetric(struct drm_crtc *crtc,
935 struct drm_crtc_state *state)
936{
937 struct sde_crtc *sde_crtc;
938 struct sde_crtc_state *crtc_state;
Lloyd Atkinson73fb8092017-02-08 16:02:55 -0500939 const struct sde_rect *roi[CRTC_DUAL_MIXERS];
Lloyd Atkinson8ba47032017-03-22 17:13:32 -0400940
941 if (!crtc || !state)
942 return -EINVAL;
943
Lloyd Atkinson73fb8092017-02-08 16:02:55 -0500944 sde_crtc = to_sde_crtc(crtc);
945 crtc_state = to_sde_crtc_state(state);
946
Lloyd Atkinson73fb8092017-02-08 16:02:55 -0500947 if (sde_crtc->num_mixers > CRTC_DUAL_MIXERS) {
948 SDE_ERROR("%s: unsupported number of mixers: %d\n",
949 sde_crtc->name, sde_crtc->num_mixers);
950 return -EINVAL;
951 }
952
Lloyd Atkinson8ba47032017-03-22 17:13:32 -0400953 /*
Lloyd Atkinson66e7dde2017-02-08 15:52:53 -0500954 * If using pingpong split: one ROI, one LM, two physical displays
955 * then the ROI must be centered on the panel split boundary and
956 * be of equal width across the split.
Lloyd Atkinson8ba47032017-03-22 17:13:32 -0400957 */
Lloyd Atkinson66e7dde2017-02-08 15:52:53 -0500958 if (crtc_state->is_ppsplit) {
959 u16 panel_split_width;
960 u32 display_mask;
961
962 roi[0] = &crtc_state->lm_roi[0];
963
964 if (sde_kms_rect_is_null(roi[0]))
965 return 0;
966
967 display_mask = _sde_crtc_get_displays_affected(crtc, state);
968 if (display_mask != (BIT(0) | BIT(1)))
969 return 0;
970
971 panel_split_width = crtc_state->lm_bounds[0].w / 2;
972 if (roi[0]->x + roi[0]->w / 2 != panel_split_width) {
973 SDE_ERROR("%s: roi x %d w %d split %d\n",
974 sde_crtc->name, roi[0]->x, roi[0]->w,
975 panel_split_width);
976 return -EINVAL;
977 }
978
979 return 0;
980 }
981
982 /*
983 * On certain HW, if using 2 LM, ROIs must be split evenly between the
984 * LMs and be of equal width.
985 */
Clarence Ipffb87422017-06-30 13:37:48 -0400986 if (sde_crtc->num_mixers < 2)
Lloyd Atkinson66e7dde2017-02-08 15:52:53 -0500987 return 0;
988
Lloyd Atkinson73fb8092017-02-08 16:02:55 -0500989 roi[0] = &crtc_state->lm_roi[0];
990 roi[1] = &crtc_state->lm_roi[1];
Lloyd Atkinson8ba47032017-03-22 17:13:32 -0400991
Lloyd Atkinson73fb8092017-02-08 16:02:55 -0500992 /* if one of the roi is null it's a left/right-only update */
993 if (sde_kms_rect_is_null(roi[0]) || sde_kms_rect_is_null(roi[1]))
994 return 0;
Lloyd Atkinson8ba47032017-03-22 17:13:32 -0400995
Lloyd Atkinson73fb8092017-02-08 16:02:55 -0500996 /* check lm rois are equal width & first roi ends at 2nd roi */
997 if (roi[0]->x + roi[0]->w != roi[1]->x || roi[0]->w != roi[1]->w) {
998 SDE_ERROR(
999 "%s: rois not centered and symmetric: roi0 x %d w %d roi1 x %d w %d\n",
1000 sde_crtc->name, roi[0]->x, roi[0]->w,
1001 roi[1]->x, roi[1]->w);
1002 return -EINVAL;
Lloyd Atkinson8ba47032017-03-22 17:13:32 -04001003 }
1004
1005 return 0;
1006}
1007
1008static int _sde_crtc_check_planes_within_crtc_roi(struct drm_crtc *crtc,
1009 struct drm_crtc_state *state)
1010{
1011 struct sde_crtc *sde_crtc;
1012 struct sde_crtc_state *crtc_state;
1013 const struct sde_rect *crtc_roi;
1014 struct drm_plane_state *pstate;
1015 struct drm_plane *plane;
1016
1017 if (!crtc || !state)
1018 return -EINVAL;
1019
1020 /*
1021 * Reject commit if a Plane CRTC destination coordinates fall outside
1022 * the partial CRTC ROI. LM output is determined via connector ROIs,
1023 * if they are specified, not Plane CRTC ROIs.
1024 */
1025
1026 sde_crtc = to_sde_crtc(crtc);
1027 crtc_state = to_sde_crtc_state(state);
1028 crtc_roi = &crtc_state->crtc_roi;
1029
1030 if (sde_kms_rect_is_null(crtc_roi))
1031 return 0;
1032
1033 drm_atomic_crtc_state_for_each_plane(plane, state) {
1034 struct sde_rect plane_roi, intersection;
1035
1036 pstate = drm_atomic_get_plane_state(state->state, plane);
1037 if (IS_ERR_OR_NULL(pstate)) {
1038 int rc = PTR_ERR(pstate);
1039
1040 SDE_ERROR("%s: failed to get plane%d state, %d\n",
1041 sde_crtc->name, plane->base.id, rc);
1042 return rc;
1043 }
1044
1045 plane_roi.x = pstate->crtc_x;
1046 plane_roi.y = pstate->crtc_y;
1047 plane_roi.w = pstate->crtc_w;
1048 plane_roi.h = pstate->crtc_h;
1049 sde_kms_rect_intersect(crtc_roi, &plane_roi, &intersection);
1050 if (!sde_kms_rect_is_equal(&plane_roi, &intersection)) {
1051 SDE_ERROR(
1052 "%s: plane%d crtc roi (%d,%d,%d,%d) outside crtc roi (%d,%d,%d,%d)\n",
1053 sde_crtc->name, plane->base.id,
1054 plane_roi.x, plane_roi.y,
1055 plane_roi.w, plane_roi.h,
1056 crtc_roi->x, crtc_roi->y,
1057 crtc_roi->w, crtc_roi->h);
1058 return -E2BIG;
1059 }
1060 }
1061
1062 return 0;
1063}
1064
1065static int _sde_crtc_check_rois(struct drm_crtc *crtc,
1066 struct drm_crtc_state *state)
1067{
1068 struct sde_crtc *sde_crtc;
1069 int lm_idx;
1070 int rc;
1071
1072 if (!crtc || !state)
1073 return -EINVAL;
1074
1075 sde_crtc = to_sde_crtc(crtc);
1076
1077 rc = _sde_crtc_set_crtc_roi(crtc, state);
1078 if (rc)
1079 return rc;
1080
Lloyd Atkinson77382202017-02-01 14:59:43 -05001081 rc = _sde_crtc_check_autorefresh(crtc, state);
1082 if (rc)
1083 return rc;
1084
Lloyd Atkinson8ba47032017-03-22 17:13:32 -04001085 for (lm_idx = 0; lm_idx < sde_crtc->num_mixers; lm_idx++) {
1086 rc = _sde_crtc_set_lm_roi(crtc, state, lm_idx);
1087 if (rc)
1088 return rc;
1089 }
1090
1091 rc = _sde_crtc_check_rois_centered_and_symmetric(crtc, state);
1092 if (rc)
1093 return rc;
1094
1095 rc = _sde_crtc_check_planes_within_crtc_roi(crtc, state);
1096 if (rc)
1097 return rc;
1098
1099 return 0;
1100}
1101
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05001102static void _sde_crtc_program_lm_output_roi(struct drm_crtc *crtc)
1103{
1104 struct sde_crtc *sde_crtc;
1105 struct sde_crtc_state *crtc_state;
Lloyd Atkinson8ba47032017-03-22 17:13:32 -04001106 const struct sde_rect *lm_roi;
1107 struct sde_hw_mixer *hw_lm;
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05001108 int lm_idx, lm_horiz_position;
1109
Lloyd Atkinson8ba47032017-03-22 17:13:32 -04001110 if (!crtc)
1111 return;
1112
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05001113 sde_crtc = to_sde_crtc(crtc);
1114 crtc_state = to_sde_crtc_state(crtc->state);
1115
1116 lm_horiz_position = 0;
1117 for (lm_idx = 0; lm_idx < sde_crtc->num_mixers; lm_idx++) {
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05001118 struct sde_hw_mixer_cfg cfg;
1119
Lloyd Atkinson8ba47032017-03-22 17:13:32 -04001120 lm_roi = &crtc_state->lm_roi[lm_idx];
1121 hw_lm = sde_crtc->mixers[lm_idx].hw_lm;
1122
1123 SDE_EVT32(DRMID(crtc_state->base.crtc), lm_idx,
1124 lm_roi->x, lm_roi->y, lm_roi->w, lm_roi->h);
1125
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05001126 if (sde_kms_rect_is_null(lm_roi))
1127 continue;
1128
Ping Lif41c2ef2017-05-04 14:40:45 -07001129 hw_lm->cfg.out_width = lm_roi->w;
1130 hw_lm->cfg.out_height = lm_roi->h;
1131 hw_lm->cfg.right_mixer = lm_horiz_position;
1132
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05001133 cfg.out_width = lm_roi->w;
1134 cfg.out_height = lm_roi->h;
1135 cfg.right_mixer = lm_horiz_position++;
1136 cfg.flags = 0;
1137 hw_lm->ops.setup_mixer_out(hw_lm, &cfg);
1138 }
1139}
1140
Dhaval Patel48c76022016-09-01 17:51:23 -07001141static void _sde_crtc_blend_setup_mixer(struct drm_crtc *crtc,
1142 struct sde_crtc *sde_crtc, struct sde_crtc_mixer *mixer)
1143{
1144 struct drm_plane *plane;
Dhaval Patel6c666622017-03-21 23:02:59 -07001145 struct drm_framebuffer *fb;
1146 struct drm_plane_state *state;
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08001147 struct sde_crtc_state *cstate;
Dhaval Patel48c76022016-09-01 17:51:23 -07001148 struct sde_plane_state *pstate = NULL;
1149 struct sde_format *format;
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08001150 struct sde_hw_ctl *ctl;
1151 struct sde_hw_mixer *lm;
1152 struct sde_hw_stage_cfg *stage_cfg;
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05001153 struct sde_rect plane_crtc_roi;
Dhaval Patel48c76022016-09-01 17:51:23 -07001154
Clarence Ip7e5f0002017-05-29 18:46:56 -04001155 u32 flush_mask, flush_sbuf, flush_tmp;
Dhaval Patel572cfd22017-06-12 19:33:39 -07001156 uint32_t stage_idx, lm_idx;
1157 int zpos_cnt[SDE_STAGE_MAX + 1] = { 0 };
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08001158 int i;
Dhaval Patel572cfd22017-06-12 19:33:39 -07001159 bool bg_alpha_enable = false;
Alan Kwong4dd64c82017-02-04 18:41:51 -08001160 u32 prefill = 0;
Dhaval Patel48c76022016-09-01 17:51:23 -07001161
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08001162 if (!sde_crtc || !mixer) {
1163 SDE_ERROR("invalid sde_crtc or mixer\n");
1164 return;
1165 }
1166
1167 ctl = mixer->hw_ctl;
1168 lm = mixer->hw_lm;
1169 stage_cfg = &sde_crtc->stage_cfg;
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05001170 cstate = to_sde_crtc_state(crtc->state);
Clarence Ip7e5f0002017-05-29 18:46:56 -04001171 flush_sbuf = 0x0;
Dhaval Patel44f12472016-08-29 12:19:47 -07001172
Clarence Ip7eb90452017-05-23 11:41:19 -04001173 cstate->sbuf_cfg.rot_op_mode = SDE_CTL_ROT_OP_MODE_OFFLINE;
1174 cstate->sbuf_prefill_line = 0;
1175
Dhaval Patel44f12472016-08-29 12:19:47 -07001176 drm_atomic_crtc_for_each_plane(plane, crtc) {
Dhaval Patel6c666622017-03-21 23:02:59 -07001177 state = plane->state;
1178 if (!state)
1179 continue;
Dhaval Patel48c76022016-09-01 17:51:23 -07001180
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05001181 plane_crtc_roi.x = state->crtc_x;
1182 plane_crtc_roi.y = state->crtc_y;
1183 plane_crtc_roi.w = state->crtc_w;
1184 plane_crtc_roi.h = state->crtc_h;
1185
Dhaval Patel6c666622017-03-21 23:02:59 -07001186 pstate = to_sde_plane_state(state);
1187 fb = state->fb;
Dhaval Patel44f12472016-08-29 12:19:47 -07001188
Alan Kwong4dd64c82017-02-04 18:41:51 -08001189 if (sde_plane_is_sbuf_mode(plane, &prefill))
Clarence Ip7eb90452017-05-23 11:41:19 -04001190 cstate->sbuf_cfg.rot_op_mode =
1191 SDE_CTL_ROT_OP_MODE_INLINE_SYNC;
1192 if (prefill > cstate->sbuf_prefill_line)
1193 cstate->sbuf_prefill_line = prefill;
Alan Kwong4dd64c82017-02-04 18:41:51 -08001194
Clarence Ip7e5f0002017-05-29 18:46:56 -04001195 sde_plane_get_ctl_flush(plane, ctl, &flush_mask, &flush_tmp);
Dhaval Patel44f12472016-08-29 12:19:47 -07001196
Clarence Ip7e5f0002017-05-29 18:46:56 -04001197 /* persist rotator flush bit(s) for one more commit */
1198 flush_mask |= cstate->sbuf_flush_mask | flush_tmp;
1199 flush_sbuf |= flush_tmp;
Dhaval Patel48c76022016-09-01 17:51:23 -07001200
1201 SDE_DEBUG("crtc %d stage:%d - plane %d sspp %d fb %d\n",
Clarence Ipd9f9fa62016-09-09 13:42:32 -04001202 crtc->base.id,
Clarence Ipd9f9fa62016-09-09 13:42:32 -04001203 pstate->stage,
1204 plane->base.id,
1205 sde_plane_pipe(plane) - SSPP_VIG0,
Dhaval Patel6c666622017-03-21 23:02:59 -07001206 state->fb ? state->fb->base.id : -1);
Dhaval Patel44f12472016-08-29 12:19:47 -07001207
Dhaval Patel48c76022016-09-01 17:51:23 -07001208 format = to_sde_format(msm_framebuffer_format(pstate->base.fb));
Narendra Muppallaec11a0a2017-06-15 15:35:17 -07001209 if (!format) {
1210 SDE_ERROR("invalid format\n");
1211 return;
1212 }
1213
Dhaval Patel572cfd22017-06-12 19:33:39 -07001214 if (pstate->stage == SDE_STAGE_BASE && format->alpha_enable)
1215 bg_alpha_enable = true;
Dhaval Patel44f12472016-08-29 12:19:47 -07001216
Lloyd Atkinson8ba47032017-03-22 17:13:32 -04001217 SDE_EVT32(DRMID(crtc), DRMID(plane),
1218 state->fb ? state->fb->base.id : -1,
1219 state->src_x >> 16, state->src_y >> 16,
1220 state->src_w >> 16, state->src_h >> 16,
1221 state->crtc_x, state->crtc_y,
Clarence Ip7eb90452017-05-23 11:41:19 -04001222 state->crtc_w, state->crtc_h,
1223 cstate->sbuf_cfg.rot_op_mode);
Dhaval Patel6c666622017-03-21 23:02:59 -07001224
Dhaval Patel572cfd22017-06-12 19:33:39 -07001225 stage_idx = zpos_cnt[pstate->stage]++;
1226 stage_cfg->stage[pstate->stage][stage_idx] =
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05001227 sde_plane_pipe(plane);
Dhaval Patel572cfd22017-06-12 19:33:39 -07001228 stage_cfg->multirect_index[pstate->stage][stage_idx] =
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05001229 pstate->multirect_index;
1230
Dhaval Patel572cfd22017-06-12 19:33:39 -07001231 SDE_EVT32(DRMID(crtc), DRMID(plane), stage_idx,
1232 sde_plane_pipe(plane) - SSPP_VIG0, pstate->stage,
1233 pstate->multirect_index, pstate->multirect_mode,
1234 format->base.pixel_format, fb ? fb->modifier[0] : 0);
1235
1236 /* blend config update */
1237 for (lm_idx = 0; lm_idx < sde_crtc->num_mixers; lm_idx++) {
1238 _sde_crtc_setup_blend_cfg(mixer + lm_idx, pstate,
1239 format);
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05001240 mixer[lm_idx].flush_mask |= flush_mask;
1241
Dhaval Patel572cfd22017-06-12 19:33:39 -07001242 if (bg_alpha_enable && !format->alpha_enable)
1243 mixer[lm_idx].mixer_op_mode = 0;
1244 else
1245 mixer[lm_idx].mixer_op_mode |=
Dhaval Patel48c76022016-09-01 17:51:23 -07001246 1 << pstate->stage;
Dhaval Patel48c76022016-09-01 17:51:23 -07001247 }
Dhaval Patel44f12472016-08-29 12:19:47 -07001248 }
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08001249
Clarence Ip7e5f0002017-05-29 18:46:56 -04001250 cstate->sbuf_flush_mask = flush_sbuf;
1251
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08001252 if (lm && lm->ops.setup_dim_layer) {
1253 cstate = to_sde_crtc_state(crtc->state);
1254 for (i = 0; i < cstate->num_dim_layers; i++)
1255 _sde_crtc_setup_dim_layer_cfg(crtc, sde_crtc,
1256 mixer, &cstate->dim_layer[i]);
1257 }
Alan Kwong4dd64c82017-02-04 18:41:51 -08001258
Clarence Ip7eb90452017-05-23 11:41:19 -04001259 if (ctl->ops.setup_sbuf_cfg)
Alan Kwong4dd64c82017-02-04 18:41:51 -08001260 ctl->ops.setup_sbuf_cfg(ctl, &cstate->sbuf_cfg);
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05001261
1262 _sde_crtc_program_lm_output_roi(crtc);
Dhaval Patel44f12472016-08-29 12:19:47 -07001263}
1264
Lloyd Atkinson094780d2017-04-24 17:25:08 -04001265static void _sde_crtc_swap_mixers_for_right_partial_update(
1266 struct drm_crtc *crtc)
1267{
1268 struct sde_crtc *sde_crtc;
1269 struct sde_crtc_state *cstate;
1270 struct drm_encoder *drm_enc;
1271 bool is_right_only;
1272 bool encoder_in_dsc_merge = false;
1273
1274 if (!crtc || !crtc->state)
1275 return;
1276
1277 sde_crtc = to_sde_crtc(crtc);
1278 cstate = to_sde_crtc_state(crtc->state);
1279
1280 if (sde_crtc->num_mixers != CRTC_DUAL_MIXERS)
1281 return;
1282
1283 drm_for_each_encoder(drm_enc, crtc->dev) {
1284 if (drm_enc->crtc == crtc &&
1285 sde_encoder_is_dsc_merge(drm_enc)) {
1286 encoder_in_dsc_merge = true;
1287 break;
1288 }
1289 }
1290
1291 /**
1292 * For right-only partial update with DSC merge, we swap LM0 & LM1.
1293 * This is due to two reasons:
1294 * - On 8996, there is a DSC HW requirement that in DSC Merge Mode,
1295 * the left DSC must be used, right DSC cannot be used alone.
1296 * For right-only partial update, this means swap layer mixers to map
1297 * Left LM to Right INTF. On later HW this was relaxed.
1298 * - In DSC Merge mode, the physical encoder has already registered
1299 * PP0 as the master, to switch to right-only we would have to
1300 * reprogram to be driven by PP1 instead.
1301 * To support both cases, we prefer to support the mixer swap solution.
1302 */
1303 if (!encoder_in_dsc_merge)
1304 return;
1305
1306 is_right_only = sde_kms_rect_is_null(&cstate->lm_roi[0]) &&
1307 !sde_kms_rect_is_null(&cstate->lm_roi[1]);
1308
1309 if (is_right_only && !sde_crtc->mixers_swapped) {
1310 /* right-only update swap mixers */
1311 swap(sde_crtc->mixers[0], sde_crtc->mixers[1]);
1312 sde_crtc->mixers_swapped = true;
1313 } else if (!is_right_only && sde_crtc->mixers_swapped) {
1314 /* left-only or full update, swap back */
1315 swap(sde_crtc->mixers[0], sde_crtc->mixers[1]);
1316 sde_crtc->mixers_swapped = false;
1317 }
1318
1319 SDE_DEBUG("%s: right_only %d swapped %d, mix0->lm%d, mix1->lm%d\n",
1320 sde_crtc->name, is_right_only, sde_crtc->mixers_swapped,
1321 sde_crtc->mixers[0].hw_lm->idx - LM_0,
1322 sde_crtc->mixers[1].hw_lm->idx - LM_0);
1323 SDE_EVT32(DRMID(crtc), is_right_only, sde_crtc->mixers_swapped,
1324 sde_crtc->mixers[0].hw_lm->idx - LM_0,
1325 sde_crtc->mixers[1].hw_lm->idx - LM_0);
1326}
1327
Clarence Ipd9f9fa62016-09-09 13:42:32 -04001328/**
1329 * _sde_crtc_blend_setup - configure crtc mixers
1330 * @crtc: Pointer to drm crtc structure
1331 */
1332static void _sde_crtc_blend_setup(struct drm_crtc *crtc)
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001333{
Lloyd Atkinson73fb8092017-02-08 16:02:55 -05001334 struct sde_crtc *sde_crtc;
1335 struct sde_crtc_state *sde_crtc_state;
1336 struct sde_crtc_mixer *mixer;
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001337 struct sde_hw_ctl *ctl;
1338 struct sde_hw_mixer *lm;
Dhaval Patel44f12472016-08-29 12:19:47 -07001339
1340 int i;
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001341
Lloyd Atkinson73fb8092017-02-08 16:02:55 -05001342 if (!crtc)
1343 return;
1344
1345 sde_crtc = to_sde_crtc(crtc);
1346 sde_crtc_state = to_sde_crtc_state(crtc->state);
1347 mixer = sde_crtc->mixers;
1348
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04001349 SDE_DEBUG("%s\n", sde_crtc->name);
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001350
Dhaval Patel48c76022016-09-01 17:51:23 -07001351 if (sde_crtc->num_mixers > CRTC_DUAL_MIXERS) {
1352 SDE_ERROR("invalid number mixers: %d\n", sde_crtc->num_mixers);
1353 return;
1354 }
1355
1356 for (i = 0; i < sde_crtc->num_mixers; i++) {
1357 if (!mixer[i].hw_lm || !mixer[i].hw_ctl) {
1358 SDE_ERROR("invalid lm or ctl assigned to mixer\n");
1359 return;
1360 }
1361 mixer[i].mixer_op_mode = 0;
1362 mixer[i].flush_mask = 0;
Lloyd Atkinsone5ec30d2016-08-23 14:32:32 -04001363 if (mixer[i].hw_ctl->ops.clear_all_blendstages)
1364 mixer[i].hw_ctl->ops.clear_all_blendstages(
1365 mixer[i].hw_ctl);
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08001366
1367 /* clear dim_layer settings */
1368 lm = mixer[i].hw_lm;
1369 if (lm->ops.clear_dim_layer)
1370 lm->ops.clear_dim_layer(lm);
Dhaval Patel48c76022016-09-01 17:51:23 -07001371 }
1372
Lloyd Atkinson094780d2017-04-24 17:25:08 -04001373 _sde_crtc_swap_mixers_for_right_partial_update(crtc);
1374
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001375 /* initialize stage cfg */
Clarence Ip8f7366c2016-07-05 12:15:26 -04001376 memset(&sde_crtc->stage_cfg, 0, sizeof(struct sde_hw_stage_cfg));
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001377
Dhaval Patel48c76022016-09-01 17:51:23 -07001378 _sde_crtc_blend_setup_mixer(crtc, sde_crtc, mixer);
1379
Abhijit Kulkarni71002ba2016-06-24 18:36:28 -04001380 for (i = 0; i < sde_crtc->num_mixers; i++) {
Lloyd Atkinson73fb8092017-02-08 16:02:55 -05001381 const struct sde_rect *lm_roi = &sde_crtc_state->lm_roi[i];
1382
Abhijit Kulkarni71002ba2016-06-24 18:36:28 -04001383 ctl = mixer[i].hw_ctl;
1384 lm = mixer[i].hw_lm;
Abhijit Kulkarni71002ba2016-06-24 18:36:28 -04001385
Lloyd Atkinson73fb8092017-02-08 16:02:55 -05001386 if (sde_kms_rect_is_null(lm_roi)) {
1387 SDE_DEBUG(
1388 "%s: lm%d leave ctl%d mask 0 since null roi\n",
1389 sde_crtc->name, lm->idx - LM_0,
1390 ctl->idx - CTL_0);
1391 continue;
1392 }
1393
Dhaval Patel48c76022016-09-01 17:51:23 -07001394 lm->ops.setup_alpha_out(lm, mixer[i].mixer_op_mode);
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001395
Dhaval Patel48c76022016-09-01 17:51:23 -07001396 mixer[i].flush_mask |= ctl->ops.get_bitmask_mixer(ctl,
Abhijit Kulkarni71002ba2016-06-24 18:36:28 -04001397 mixer[i].hw_lm->idx);
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04001398
1399 /* stage config flush mask */
Dhaval Patel48c76022016-09-01 17:51:23 -07001400 ctl->ops.update_pending_flush(ctl, mixer[i].flush_mask);
1401
Clarence Ip8e69ad02016-12-09 09:43:57 -05001402 SDE_DEBUG("lm %d, op_mode 0x%X, ctl %d, flush mask 0x%x\n",
1403 mixer[i].hw_lm->idx - LM_0,
1404 mixer[i].mixer_op_mode,
1405 ctl->idx - CTL_0,
1406 mixer[i].flush_mask);
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001407
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001408 ctl->ops.setup_blendstage(ctl, mixer[i].hw_lm->idx,
Dhaval Patel572cfd22017-06-12 19:33:39 -07001409 &sde_crtc->stage_cfg);
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001410 }
Lloyd Atkinson8ba47032017-03-22 17:13:32 -04001411
1412 _sde_crtc_program_lm_output_roi(crtc);
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001413}
1414
Clarence Ip0d0e96d2016-10-24 18:13:13 -04001415void sde_crtc_prepare_commit(struct drm_crtc *crtc,
1416 struct drm_crtc_state *old_state)
Clarence Ip24f80662016-06-13 19:05:32 -04001417{
1418 struct sde_crtc *sde_crtc;
Clarence Ip0d0e96d2016-10-24 18:13:13 -04001419 struct sde_crtc_state *cstate;
1420 struct drm_connector *conn;
Clarence Ip24f80662016-06-13 19:05:32 -04001421
Clarence Ip0d0e96d2016-10-24 18:13:13 -04001422 if (!crtc || !crtc->state) {
Clarence Ip24f80662016-06-13 19:05:32 -04001423 SDE_ERROR("invalid crtc\n");
1424 return;
1425 }
1426
1427 sde_crtc = to_sde_crtc(crtc);
Clarence Ip0d0e96d2016-10-24 18:13:13 -04001428 cstate = to_sde_crtc_state(crtc->state);
Dhaval Patel6c666622017-03-21 23:02:59 -07001429 SDE_EVT32_VERBOSE(DRMID(crtc));
Clarence Ip24f80662016-06-13 19:05:32 -04001430
Clarence Ip0d0e96d2016-10-24 18:13:13 -04001431 /* identify connectors attached to this crtc */
Clarence Ip0d0e96d2016-10-24 18:13:13 -04001432 cstate->num_connectors = 0;
1433
1434 drm_for_each_connector(conn, crtc->dev)
1435 if (conn->state && conn->state->crtc == crtc &&
1436 cstate->num_connectors < MAX_CONNECTORS) {
1437 cstate->connectors[cstate->num_connectors++] = conn;
1438 sde_connector_prepare_fence(conn);
Clarence Ip0d0e96d2016-10-24 18:13:13 -04001439 }
1440
1441 /* prepare main output fence */
Clarence Ip24f80662016-06-13 19:05:32 -04001442 sde_fence_prepare(&sde_crtc->output_fence);
1443}
1444
Abhinav Kumarf2e94b52017-02-09 20:27:24 -08001445/**
1446 * _sde_crtc_complete_flip - signal pending page_flip events
1447 * Any pending vblank events are added to the vblank_event_list
1448 * so that the next vblank interrupt shall signal them.
1449 * However PAGE_FLIP events are not handled through the vblank_event_list.
1450 * This API signals any pending PAGE_FLIP events requested through
1451 * DRM_IOCTL_MODE_PAGE_FLIP and are cached in the sde_crtc->event.
1452 * if file!=NULL, this is preclose potential cancel-flip path
1453 * @crtc: Pointer to drm crtc structure
1454 * @file: Pointer to drm file
1455 */
Lloyd Atkinson4f1c8692016-09-14 14:04:25 -04001456static void _sde_crtc_complete_flip(struct drm_crtc *crtc,
1457 struct drm_file *file)
Abhijit Kulkarni40e38162016-06-26 22:12:09 -04001458{
1459 struct sde_crtc *sde_crtc = to_sde_crtc(crtc);
1460 struct drm_device *dev = crtc->dev;
1461 struct drm_pending_vblank_event *event;
1462 unsigned long flags;
1463
1464 spin_lock_irqsave(&dev->event_lock, flags);
1465 event = sde_crtc->event;
1466 if (event) {
1467 /* if regular vblank case (!file) or if cancel-flip from
1468 * preclose on file that requested flip, then send the
1469 * event:
1470 */
1471 if (!file || (event->base.file_priv == file)) {
1472 sde_crtc->event = NULL;
Lloyd Atkinson4f1c8692016-09-14 14:04:25 -04001473 DRM_DEBUG_VBL("%s: send event: %pK\n",
Dhaval Patelec10fad2016-08-22 14:40:48 -07001474 sde_crtc->name, event);
Dhaval Patela5f75952017-07-25 11:17:41 -07001475 SDE_EVT32_VERBOSE(DRMID(crtc));
Lloyd Atkinsonac933642016-09-14 11:52:00 -04001476 drm_crtc_send_vblank_event(crtc, event);
Abhijit Kulkarni40e38162016-06-26 22:12:09 -04001477 }
1478 }
1479 spin_unlock_irqrestore(&dev->event_lock, flags);
1480}
1481
Alan Kwong3e985f02017-02-12 15:08:44 -08001482enum sde_intf_mode sde_crtc_get_intf_mode(struct drm_crtc *crtc)
1483{
1484 struct drm_encoder *encoder;
1485
1486 if (!crtc || !crtc->dev) {
1487 SDE_ERROR("invalid crtc\n");
1488 return INTF_MODE_NONE;
1489 }
1490
1491 drm_for_each_encoder(encoder, crtc->dev)
1492 if (encoder->crtc == crtc)
1493 return sde_encoder_get_intf_mode(encoder);
1494
1495 return INTF_MODE_NONE;
1496}
1497
Abhijit Kulkarni40e38162016-06-26 22:12:09 -04001498static void sde_crtc_vblank_cb(void *data)
1499{
1500 struct drm_crtc *crtc = (struct drm_crtc *)data;
Alan Kwong07da0982016-11-04 12:57:45 -04001501 struct sde_crtc *sde_crtc = to_sde_crtc(crtc);
1502
1503 /* keep statistics on vblank callback - with auto reset via debugfs */
1504 if (ktime_equal(sde_crtc->vblank_cb_time, ktime_set(0, 0)))
1505 sde_crtc->vblank_cb_time = ktime_get();
1506 else
1507 sde_crtc->vblank_cb_count++;
Abhinav Kumarf2e94b52017-02-09 20:27:24 -08001508 _sde_crtc_complete_flip(crtc, NULL);
Lloyd Atkinsonac933642016-09-14 11:52:00 -04001509 drm_crtc_handle_vblank(crtc);
Lloyd Atkinson9eabe7a2016-09-14 13:39:15 -04001510 DRM_DEBUG_VBL("crtc%d\n", crtc->base.id);
Dhaval Patel6c666622017-03-21 23:02:59 -07001511 SDE_EVT32_VERBOSE(DRMID(crtc));
Abhijit Kulkarni40e38162016-06-26 22:12:09 -04001512}
1513
Alan Kwong628d19e2016-10-31 13:50:13 -04001514static void sde_crtc_frame_event_work(struct kthread_work *work)
1515{
Alan Kwong67a3f792016-11-01 23:16:53 -04001516 struct msm_drm_private *priv;
Alan Kwong628d19e2016-10-31 13:50:13 -04001517 struct sde_crtc_frame_event *fevent;
1518 struct drm_crtc *crtc;
1519 struct sde_crtc *sde_crtc;
Alan Kwonga1939682017-05-05 11:30:08 -07001520 struct sde_crtc_state *cstate;
Alan Kwong628d19e2016-10-31 13:50:13 -04001521 struct sde_kms *sde_kms;
1522 unsigned long flags;
Veera Sundaram Sankaran675ff622017-06-21 21:44:46 -07001523 bool frame_done = false;
1524 int i;
Alan Kwong628d19e2016-10-31 13:50:13 -04001525
1526 if (!work) {
1527 SDE_ERROR("invalid work handle\n");
1528 return;
1529 }
1530
1531 fevent = container_of(work, struct sde_crtc_frame_event, work);
Alan Kwonga1939682017-05-05 11:30:08 -07001532 if (!fevent->crtc || !fevent->crtc->state) {
Alan Kwong628d19e2016-10-31 13:50:13 -04001533 SDE_ERROR("invalid crtc\n");
1534 return;
1535 }
1536
1537 crtc = fevent->crtc;
1538 sde_crtc = to_sde_crtc(crtc);
Alan Kwonga1939682017-05-05 11:30:08 -07001539 cstate = to_sde_crtc_state(crtc->state);
Alan Kwong628d19e2016-10-31 13:50:13 -04001540
1541 sde_kms = _sde_crtc_get_kms(crtc);
1542 if (!sde_kms) {
1543 SDE_ERROR("invalid kms handle\n");
1544 return;
1545 }
Alan Kwong67a3f792016-11-01 23:16:53 -04001546 priv = sde_kms->dev->dev_private;
Veera Sundaram Sankarana90e1392017-07-06 15:00:09 -07001547 SDE_ATRACE_BEGIN("crtc_frame_event");
Alan Kwong628d19e2016-10-31 13:50:13 -04001548
1549 SDE_DEBUG("crtc%d event:%u ts:%lld\n", crtc->base.id, fevent->event,
1550 ktime_to_ns(fevent->ts));
1551
Veera Sundaram Sankaran675ff622017-06-21 21:44:46 -07001552 SDE_EVT32_VERBOSE(DRMID(crtc), fevent->event, SDE_EVTLOG_FUNC_ENTRY);
1553
1554 if (fevent->event & (SDE_ENCODER_FRAME_EVENT_DONE
1555 | SDE_ENCODER_FRAME_EVENT_ERROR
1556 | SDE_ENCODER_FRAME_EVENT_PANEL_DEAD)) {
Alan Kwong628d19e2016-10-31 13:50:13 -04001557
1558 if (atomic_read(&sde_crtc->frame_pending) < 1) {
1559 /* this should not happen */
1560 SDE_ERROR("crtc%d ts:%lld invalid frame_pending:%d\n",
1561 crtc->base.id,
1562 ktime_to_ns(fevent->ts),
1563 atomic_read(&sde_crtc->frame_pending));
Dhaval Patel6c666622017-03-21 23:02:59 -07001564 SDE_EVT32(DRMID(crtc), fevent->event,
1565 SDE_EVTLOG_FUNC_CASE1);
Alan Kwong628d19e2016-10-31 13:50:13 -04001566 } else if (atomic_dec_return(&sde_crtc->frame_pending) == 0) {
1567 /* release bandwidth and other resources */
1568 SDE_DEBUG("crtc%d ts:%lld last pending\n",
1569 crtc->base.id,
1570 ktime_to_ns(fevent->ts));
Dhaval Patel6c666622017-03-21 23:02:59 -07001571 SDE_EVT32(DRMID(crtc), fevent->event,
1572 SDE_EVTLOG_FUNC_CASE2);
Veera Sundaram Sankaran7ee99092017-06-13 11:19:36 -07001573 sde_core_perf_crtc_release_bw(crtc);
Alan Kwong628d19e2016-10-31 13:50:13 -04001574 } else {
Dhaval Patel6c666622017-03-21 23:02:59 -07001575 SDE_EVT32_VERBOSE(DRMID(crtc), fevent->event,
1576 SDE_EVTLOG_FUNC_CASE3);
Alan Kwong628d19e2016-10-31 13:50:13 -04001577 }
Alan Kwonga1939682017-05-05 11:30:08 -07001578
Veera Sundaram Sankaran675ff622017-06-21 21:44:46 -07001579 if (fevent->event & SDE_ENCODER_FRAME_EVENT_DONE)
Alan Kwonga1939682017-05-05 11:30:08 -07001580 sde_core_perf_crtc_update(crtc, 0, false);
Veera Sundaram Sankaran675ff622017-06-21 21:44:46 -07001581
1582 if (fevent->event & (SDE_ENCODER_FRAME_EVENT_DONE
1583 | SDE_ENCODER_FRAME_EVENT_ERROR))
1584 frame_done = true;
1585 }
1586
Veera Sundaram Sankarana90e1392017-07-06 15:00:09 -07001587 if (fevent->event & SDE_ENCODER_FRAME_EVENT_SIGNAL_RELEASE_FENCE) {
1588 SDE_ATRACE_BEGIN("signal_release_fence");
Veera Sundaram Sankaran675ff622017-06-21 21:44:46 -07001589 sde_fence_signal(&sde_crtc->output_fence, fevent->ts, 0);
Veera Sundaram Sankarana90e1392017-07-06 15:00:09 -07001590 SDE_ATRACE_END("signal_release_fence");
1591 }
Veera Sundaram Sankaran675ff622017-06-21 21:44:46 -07001592
1593 if (fevent->event & SDE_ENCODER_FRAME_EVENT_SIGNAL_RETIRE_FENCE) {
Veera Sundaram Sankarana90e1392017-07-06 15:00:09 -07001594 SDE_ATRACE_BEGIN("signal_retire_fence");
Veera Sundaram Sankaran675ff622017-06-21 21:44:46 -07001595 for (i = 0; i < cstate->num_connectors; ++i)
1596 sde_connector_complete_commit(cstate->connectors[i],
1597 fevent->ts);
Veera Sundaram Sankarana90e1392017-07-06 15:00:09 -07001598 SDE_ATRACE_END("signal_retire_fence");
Alan Kwong628d19e2016-10-31 13:50:13 -04001599 }
1600
Lloyd Atkinson8c49c582016-11-18 14:23:54 -05001601 if (fevent->event & SDE_ENCODER_FRAME_EVENT_PANEL_DEAD)
1602 SDE_ERROR("crtc%d ts:%lld received panel dead event\n",
1603 crtc->base.id, ktime_to_ns(fevent->ts));
1604
Veera Sundaram Sankaran675ff622017-06-21 21:44:46 -07001605 if (frame_done)
1606 complete_all(&sde_crtc->frame_done_comp);
1607
Alan Kwong628d19e2016-10-31 13:50:13 -04001608 spin_lock_irqsave(&sde_crtc->spin_lock, flags);
1609 list_add_tail(&fevent->list, &sde_crtc->frame_event_list);
1610 spin_unlock_irqrestore(&sde_crtc->spin_lock, flags);
Veera Sundaram Sankarana90e1392017-07-06 15:00:09 -07001611 SDE_ATRACE_END("crtc_frame_event");
Alan Kwong628d19e2016-10-31 13:50:13 -04001612}
1613
1614static void sde_crtc_frame_event_cb(void *data, u32 event)
1615{
1616 struct drm_crtc *crtc = (struct drm_crtc *)data;
1617 struct sde_crtc *sde_crtc;
1618 struct msm_drm_private *priv;
Alan Kwong628d19e2016-10-31 13:50:13 -04001619 struct sde_crtc_frame_event *fevent;
1620 unsigned long flags;
Veera Sundaram Sankaran10ea2bd2017-06-14 14:10:57 -07001621 u32 crtc_id;
Alan Kwong628d19e2016-10-31 13:50:13 -04001622
1623 if (!crtc || !crtc->dev || !crtc->dev->dev_private) {
1624 SDE_ERROR("invalid parameters\n");
1625 return;
1626 }
1627 sde_crtc = to_sde_crtc(crtc);
1628 priv = crtc->dev->dev_private;
Veera Sundaram Sankaran10ea2bd2017-06-14 14:10:57 -07001629 crtc_id = drm_crtc_index(crtc);
Alan Kwong628d19e2016-10-31 13:50:13 -04001630
1631 SDE_DEBUG("crtc%d\n", crtc->base.id);
Ingrid Gallardo79b44392017-05-30 16:30:52 -07001632 SDE_EVT32_VERBOSE(DRMID(crtc), event);
Alan Kwong628d19e2016-10-31 13:50:13 -04001633
1634 spin_lock_irqsave(&sde_crtc->spin_lock, flags);
Lloyd Atkinson78831f82016-12-09 11:24:56 -05001635 fevent = list_first_entry_or_null(&sde_crtc->frame_event_list,
1636 struct sde_crtc_frame_event, list);
1637 if (fevent)
1638 list_del_init(&fevent->list);
Alan Kwong628d19e2016-10-31 13:50:13 -04001639 spin_unlock_irqrestore(&sde_crtc->spin_lock, flags);
1640
Lloyd Atkinson78831f82016-12-09 11:24:56 -05001641 if (!fevent) {
Alan Kwong628d19e2016-10-31 13:50:13 -04001642 SDE_ERROR("crtc%d event %d overflow\n",
1643 crtc->base.id, event);
1644 SDE_EVT32(DRMID(crtc), event);
1645 return;
1646 }
1647
Alan Kwong628d19e2016-10-31 13:50:13 -04001648 fevent->event = event;
1649 fevent->crtc = crtc;
1650 fevent->ts = ktime_get();
Veera Sundaram Sankaran10ea2bd2017-06-14 14:10:57 -07001651 kthread_queue_work(&priv->event_thread[crtc_id].worker, &fevent->work);
Alan Kwong628d19e2016-10-31 13:50:13 -04001652}
1653
Lloyd Atkinson5d722782016-05-30 14:09:41 -04001654/**
Clarence Ipcae1bb62016-07-07 12:07:13 -04001655 * _sde_crtc_set_input_fence_timeout - update ns version of in fence timeout
1656 * @cstate: Pointer to sde crtc state
1657 */
1658static void _sde_crtc_set_input_fence_timeout(struct sde_crtc_state *cstate)
1659{
1660 if (!cstate) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07001661 SDE_ERROR("invalid cstate\n");
Clarence Ipcae1bb62016-07-07 12:07:13 -04001662 return;
1663 }
1664 cstate->input_fence_timeout_ns =
1665 sde_crtc_get_property(cstate, CRTC_PROP_INPUT_FENCE_TIMEOUT);
1666 cstate->input_fence_timeout_ns *= NSEC_PER_MSEC;
1667}
1668
1669/**
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08001670 * _sde_crtc_set_dim_layer_v1 - copy dim layer settings from userspace
1671 * @cstate: Pointer to sde crtc state
1672 * @user_ptr: User ptr for sde_drm_dim_layer_v1 struct
1673 */
1674static void _sde_crtc_set_dim_layer_v1(struct sde_crtc_state *cstate,
1675 void *usr_ptr)
1676{
1677 struct sde_drm_dim_layer_v1 dim_layer_v1;
1678 struct sde_drm_dim_layer_cfg *user_cfg;
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -07001679 struct sde_hw_dim_layer *dim_layer;
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08001680 u32 count, i;
1681
1682 if (!cstate) {
1683 SDE_ERROR("invalid cstate\n");
1684 return;
1685 }
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -07001686 dim_layer = cstate->dim_layer;
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08001687
1688 if (!usr_ptr) {
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -07001689 SDE_DEBUG("dim_layer data removed\n");
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08001690 return;
1691 }
1692
1693 if (copy_from_user(&dim_layer_v1, usr_ptr, sizeof(dim_layer_v1))) {
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -07001694 SDE_ERROR("failed to copy dim_layer data\n");
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08001695 return;
1696 }
1697
1698 count = dim_layer_v1.num_layers;
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -07001699 if (count > SDE_MAX_DIM_LAYERS) {
1700 SDE_ERROR("invalid number of dim_layers:%d", count);
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08001701 return;
1702 }
1703
1704 /* populate from user space */
1705 cstate->num_dim_layers = count;
1706 for (i = 0; i < count; i++) {
1707 user_cfg = &dim_layer_v1.layer_cfg[i];
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08001708
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -07001709 dim_layer[i].flags = user_cfg->flags;
1710 dim_layer[i].stage = user_cfg->stage + SDE_STAGE_0;
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08001711
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -07001712 dim_layer[i].rect.x = user_cfg->rect.x1;
1713 dim_layer[i].rect.y = user_cfg->rect.y1;
1714 dim_layer[i].rect.w = user_cfg->rect.x2 - user_cfg->rect.x1;
1715 dim_layer[i].rect.h = user_cfg->rect.y2 - user_cfg->rect.y1;
1716
1717 dim_layer[i].color_fill = (struct sde_mdss_color) {
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08001718 user_cfg->color_fill.color_0,
1719 user_cfg->color_fill.color_1,
1720 user_cfg->color_fill.color_2,
1721 user_cfg->color_fill.color_3,
1722 };
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -07001723
1724 SDE_DEBUG("dim_layer[%d] - flags:%d, stage:%d\n",
1725 i, dim_layer[i].flags, dim_layer[i].stage);
1726 SDE_DEBUG(" rect:{%d,%d,%d,%d}, color:{%d,%d,%d,%d}\n",
1727 dim_layer[i].rect.x, dim_layer[i].rect.y,
1728 dim_layer[i].rect.w, dim_layer[i].rect.h,
1729 dim_layer[i].color_fill.color_0,
1730 dim_layer[i].color_fill.color_1,
1731 dim_layer[i].color_fill.color_2,
1732 dim_layer[i].color_fill.color_3);
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08001733 }
1734}
1735
1736/**
Clarence Ipcae1bb62016-07-07 12:07:13 -04001737 * _sde_crtc_wait_for_fences - wait for incoming framebuffer sync fences
1738 * @crtc: Pointer to CRTC object
1739 */
1740static void _sde_crtc_wait_for_fences(struct drm_crtc *crtc)
1741{
1742 struct drm_plane *plane = NULL;
1743 uint32_t wait_ms = 1;
Clarence Ip8dedc232016-09-09 16:41:00 -04001744 ktime_t kt_end, kt_wait;
Dhaval Patel39323d42017-03-01 23:48:24 -08001745 int rc = 0;
Clarence Ipcae1bb62016-07-07 12:07:13 -04001746
Lloyd Atkinson4f1c8692016-09-14 14:04:25 -04001747 SDE_DEBUG("\n");
Clarence Ipcae1bb62016-07-07 12:07:13 -04001748
1749 if (!crtc || !crtc->state) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07001750 SDE_ERROR("invalid crtc/state %pK\n", crtc);
Clarence Ipcae1bb62016-07-07 12:07:13 -04001751 return;
1752 }
1753
1754 /* use monotonic timer to limit total fence wait time */
Clarence Ip8dedc232016-09-09 16:41:00 -04001755 kt_end = ktime_add_ns(ktime_get(),
1756 to_sde_crtc_state(crtc->state)->input_fence_timeout_ns);
Clarence Ipcae1bb62016-07-07 12:07:13 -04001757
1758 /*
1759 * Wait for fences sequentially, as all of them need to be signalled
1760 * before we can proceed.
1761 *
1762 * Limit total wait time to INPUT_FENCE_TIMEOUT, but still call
1763 * sde_plane_wait_input_fence with wait_ms == 0 after the timeout so
1764 * that each plane can check its fence status and react appropriately
Dhaval Patel39323d42017-03-01 23:48:24 -08001765 * if its fence has timed out. Call input fence wait multiple times if
1766 * fence wait is interrupted due to interrupt call.
Clarence Ipcae1bb62016-07-07 12:07:13 -04001767 */
Narendra Muppalla77b32932017-05-10 13:53:11 -07001768 SDE_ATRACE_BEGIN("plane_wait_input_fence");
Clarence Ipcae1bb62016-07-07 12:07:13 -04001769 drm_atomic_crtc_for_each_plane(plane, crtc) {
Dhaval Patel39323d42017-03-01 23:48:24 -08001770 do {
Clarence Ip8dedc232016-09-09 16:41:00 -04001771 kt_wait = ktime_sub(kt_end, ktime_get());
1772 if (ktime_compare(kt_wait, ktime_set(0, 0)) >= 0)
1773 wait_ms = ktime_to_ms(kt_wait);
Clarence Ipcae1bb62016-07-07 12:07:13 -04001774 else
1775 wait_ms = 0;
Dhaval Patel39323d42017-03-01 23:48:24 -08001776
1777 rc = sde_plane_wait_input_fence(plane, wait_ms);
1778 } while (wait_ms && rc == -ERESTARTSYS);
Clarence Ipcae1bb62016-07-07 12:07:13 -04001779 }
Narendra Muppalla77b32932017-05-10 13:53:11 -07001780 SDE_ATRACE_END("plane_wait_input_fence");
Clarence Ipcae1bb62016-07-07 12:07:13 -04001781}
1782
Lloyd Atkinson11f34442016-08-11 11:19:52 -04001783static void _sde_crtc_setup_mixer_for_encoder(
1784 struct drm_crtc *crtc,
1785 struct drm_encoder *enc)
1786{
1787 struct sde_crtc *sde_crtc = to_sde_crtc(crtc);
Lloyd Atkinson4f1c8692016-09-14 14:04:25 -04001788 struct sde_kms *sde_kms = _sde_crtc_get_kms(crtc);
Lloyd Atkinson11f34442016-08-11 11:19:52 -04001789 struct sde_rm *rm = &sde_kms->rm;
1790 struct sde_crtc_mixer *mixer;
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04001791 struct sde_hw_ctl *last_valid_ctl = NULL;
Lloyd Atkinson11f34442016-08-11 11:19:52 -04001792 int i;
Gopikrishnaiah Anandane0e5e0c2016-05-25 11:05:33 -07001793 struct sde_rm_hw_iter lm_iter, ctl_iter, dspp_iter;
Lloyd Atkinson11f34442016-08-11 11:19:52 -04001794
1795 sde_rm_init_hw_iter(&lm_iter, enc->base.id, SDE_HW_BLK_LM);
1796 sde_rm_init_hw_iter(&ctl_iter, enc->base.id, SDE_HW_BLK_CTL);
Gopikrishnaiah Anandane0e5e0c2016-05-25 11:05:33 -07001797 sde_rm_init_hw_iter(&dspp_iter, enc->base.id, SDE_HW_BLK_DSPP);
Lloyd Atkinson11f34442016-08-11 11:19:52 -04001798
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04001799 /* Set up all the mixers and ctls reserved by this encoder */
Lloyd Atkinson11f34442016-08-11 11:19:52 -04001800 for (i = sde_crtc->num_mixers; i < ARRAY_SIZE(sde_crtc->mixers); i++) {
1801 mixer = &sde_crtc->mixers[i];
1802
Lloyd Atkinson11f34442016-08-11 11:19:52 -04001803 if (!sde_rm_get_hw(rm, &lm_iter))
1804 break;
1805 mixer->hw_lm = (struct sde_hw_mixer *)lm_iter.hw;
1806
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04001807 /* CTL may be <= LMs, if <, multiple LMs controlled by 1 CTL */
1808 if (!sde_rm_get_hw(rm, &ctl_iter)) {
1809 SDE_DEBUG("no ctl assigned to lm %d, using previous\n",
Clarence Ip8e69ad02016-12-09 09:43:57 -05001810 mixer->hw_lm->idx - LM_0);
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04001811 mixer->hw_ctl = last_valid_ctl;
1812 } else {
1813 mixer->hw_ctl = (struct sde_hw_ctl *)ctl_iter.hw;
1814 last_valid_ctl = mixer->hw_ctl;
1815 }
Lloyd Atkinson11f34442016-08-11 11:19:52 -04001816
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04001817 /* Shouldn't happen, mixers are always >= ctls */
1818 if (!mixer->hw_ctl) {
1819 SDE_ERROR("no valid ctls found for lm %d\n",
Clarence Ip8e69ad02016-12-09 09:43:57 -05001820 mixer->hw_lm->idx - LM_0);
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04001821 return;
1822 }
1823
Gopikrishnaiah Anandane0e5e0c2016-05-25 11:05:33 -07001824 /* Dspp may be null */
1825 (void) sde_rm_get_hw(rm, &dspp_iter);
1826 mixer->hw_dspp = (struct sde_hw_dspp *)dspp_iter.hw;
1827
Lloyd Atkinson11f34442016-08-11 11:19:52 -04001828 mixer->encoder = enc;
1829
1830 sde_crtc->num_mixers++;
Clarence Ipd9f9fa62016-09-09 13:42:32 -04001831 SDE_DEBUG("setup mixer %d: lm %d\n",
1832 i, mixer->hw_lm->idx - LM_0);
1833 SDE_DEBUG("setup mixer %d: ctl %d\n",
1834 i, mixer->hw_ctl->idx - CTL_0);
Lloyd Atkinson11f34442016-08-11 11:19:52 -04001835 }
1836}
1837
1838static void _sde_crtc_setup_mixers(struct drm_crtc *crtc)
1839{
1840 struct sde_crtc *sde_crtc = to_sde_crtc(crtc);
1841 struct drm_encoder *enc;
1842
Lloyd Atkinson11f34442016-08-11 11:19:52 -04001843 sde_crtc->num_mixers = 0;
1844 memset(sde_crtc->mixers, 0, sizeof(sde_crtc->mixers));
1845
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07001846 mutex_lock(&sde_crtc->crtc_lock);
Lloyd Atkinson11f34442016-08-11 11:19:52 -04001847 /* Check for mixers on all encoders attached to this crtc */
1848 list_for_each_entry(enc, &crtc->dev->mode_config.encoder_list, head) {
1849 if (enc->crtc != crtc)
1850 continue;
1851
1852 _sde_crtc_setup_mixer_for_encoder(crtc, enc);
1853 }
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05001854
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07001855 mutex_unlock(&sde_crtc->crtc_lock);
Lloyd Atkinson11f34442016-08-11 11:19:52 -04001856}
1857
Lloyd Atkinson66e7dde2017-02-08 15:52:53 -05001858static void _sde_crtc_setup_is_ppsplit(struct drm_crtc_state *state)
1859{
1860 int i;
1861 struct sde_crtc_state *cstate;
1862
1863 cstate = to_sde_crtc_state(state);
1864
1865 cstate->is_ppsplit = false;
1866 for (i = 0; i < cstate->num_connectors; i++) {
1867 struct drm_connector *conn = cstate->connectors[i];
1868
1869 if (sde_connector_get_topology_name(conn) ==
1870 SDE_RM_TOPOLOGY_PPSPLIT)
1871 cstate->is_ppsplit = true;
1872 }
1873}
1874
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05001875static void _sde_crtc_setup_lm_bounds(struct drm_crtc *crtc,
1876 struct drm_crtc_state *state)
1877{
1878 struct sde_crtc *sde_crtc;
1879 struct sde_crtc_state *cstate;
1880 struct drm_display_mode *adj_mode;
1881 u32 crtc_split_width;
1882 int i;
1883
1884 if (!crtc || !state) {
1885 SDE_ERROR("invalid args\n");
1886 return;
1887 }
1888
1889 sde_crtc = to_sde_crtc(crtc);
1890 cstate = to_sde_crtc_state(state);
1891
1892 adj_mode = &state->adjusted_mode;
1893 crtc_split_width = sde_crtc_mixer_width(sde_crtc, adj_mode);
1894
1895 for (i = 0; i < sde_crtc->num_mixers; i++) {
Lloyd Atkinson8ba47032017-03-22 17:13:32 -04001896 cstate->lm_bounds[i].x = crtc_split_width * i;
1897 cstate->lm_bounds[i].y = 0;
1898 cstate->lm_bounds[i].w = crtc_split_width;
1899 cstate->lm_bounds[i].h = adj_mode->vdisplay;
1900 memcpy(&cstate->lm_roi[i], &cstate->lm_bounds[i],
1901 sizeof(cstate->lm_roi[i]));
Dhaval Patela5f75952017-07-25 11:17:41 -07001902 SDE_EVT32_VERBOSE(DRMID(crtc), i,
Lloyd Atkinson8ba47032017-03-22 17:13:32 -04001903 cstate->lm_bounds[i].x, cstate->lm_bounds[i].y,
1904 cstate->lm_bounds[i].w, cstate->lm_bounds[i].h);
1905 SDE_DEBUG("%s: lm%d bnd&roi (%d,%d,%d,%d)\n", sde_crtc->name, i,
1906 cstate->lm_roi[i].x, cstate->lm_roi[i].y,
1907 cstate->lm_roi[i].w, cstate->lm_roi[i].h);
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05001908 }
1909
1910 drm_mode_debug_printmodeline(adj_mode);
1911}
1912
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001913static void sde_crtc_atomic_begin(struct drm_crtc *crtc,
Clarence Ip0d0e96d2016-10-24 18:13:13 -04001914 struct drm_crtc_state *old_state)
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07001915{
Clarence Ipcae1bb62016-07-07 12:07:13 -04001916 struct sde_crtc *sde_crtc;
Dhaval Patel0e558f42017-04-30 00:51:40 -07001917 struct drm_encoder *encoder;
Clarence Ipcae1bb62016-07-07 12:07:13 -04001918 struct drm_device *dev;
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001919 unsigned long flags;
1920
Clarence Ipcae1bb62016-07-07 12:07:13 -04001921 if (!crtc) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07001922 SDE_ERROR("invalid crtc\n");
Clarence Ipcae1bb62016-07-07 12:07:13 -04001923 return;
1924 }
1925
Alan Kwong163d2612016-11-03 00:56:56 -04001926 if (!crtc->state->enable) {
1927 SDE_DEBUG("crtc%d -> enable %d, skip atomic_begin\n",
1928 crtc->base.id, crtc->state->enable);
1929 return;
1930 }
1931
1932 SDE_DEBUG("crtc%d\n", crtc->base.id);
1933
Clarence Ipcae1bb62016-07-07 12:07:13 -04001934 sde_crtc = to_sde_crtc(crtc);
1935 dev = crtc->dev;
1936
Lloyd Atkinson8ba47032017-03-22 17:13:32 -04001937 if (!sde_crtc->num_mixers) {
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04001938 _sde_crtc_setup_mixers(crtc);
Lloyd Atkinson66e7dde2017-02-08 15:52:53 -05001939 _sde_crtc_setup_is_ppsplit(crtc->state);
Lloyd Atkinson8ba47032017-03-22 17:13:32 -04001940 _sde_crtc_setup_lm_bounds(crtc, crtc->state);
1941 }
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05001942
Lloyd Atkinson265d2212016-05-30 13:12:01 -04001943 if (sde_crtc->event) {
1944 WARN_ON(sde_crtc->event);
1945 } else {
1946 spin_lock_irqsave(&dev->event_lock, flags);
1947 sde_crtc->event = crtc->state->event;
1948 spin_unlock_irqrestore(&dev->event_lock, flags);
1949 }
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001950
Dhaval Patel0e558f42017-04-30 00:51:40 -07001951 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
1952 if (encoder->crtc != crtc)
1953 continue;
Lloyd Atkinson5d722782016-05-30 14:09:41 -04001954
Dhaval Patel0e558f42017-04-30 00:51:40 -07001955 /* encoder will trigger pending mask now */
1956 sde_encoder_trigger_kickoff_pending(encoder);
Lloyd Atkinson5d722782016-05-30 14:09:41 -04001957 }
1958
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001959 /*
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04001960 * If no mixers have been allocated in sde_crtc_atomic_check(),
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001961 * it means we are trying to flush a CRTC whose state is disabled:
1962 * nothing else needs to be done.
1963 */
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04001964 if (unlikely(!sde_crtc->num_mixers))
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001965 return;
1966
Clarence Ipd9f9fa62016-09-09 13:42:32 -04001967 _sde_crtc_blend_setup(crtc);
Gopikrishnaiah Anandane0e5e0c2016-05-25 11:05:33 -07001968 sde_cp_crtc_apply_properties(crtc);
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001969
1970 /*
1971 * PP_DONE irq is only used by command mode for now.
1972 * It is better to request pending before FLUSH and START trigger
1973 * to make sure no pp_done irq missed.
1974 * This is safe because no pp_done will happen before SW trigger
1975 * in command mode.
1976 */
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07001977}
1978
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001979static void sde_crtc_atomic_flush(struct drm_crtc *crtc,
1980 struct drm_crtc_state *old_crtc_state)
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07001981{
Dhaval Patel82c8dbc2017-02-18 23:15:10 -08001982 struct drm_encoder *encoder;
Clarence Ipcae1bb62016-07-07 12:07:13 -04001983 struct sde_crtc *sde_crtc;
1984 struct drm_device *dev;
Lloyd Atkinson265d2212016-05-30 13:12:01 -04001985 struct drm_plane *plane;
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001986 unsigned long flags;
Dhaval Patel82c8dbc2017-02-18 23:15:10 -08001987 struct sde_crtc_state *cstate;
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07001988
Clarence Ipcae1bb62016-07-07 12:07:13 -04001989 if (!crtc) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07001990 SDE_ERROR("invalid crtc\n");
Clarence Ipcae1bb62016-07-07 12:07:13 -04001991 return;
1992 }
1993
Alan Kwong163d2612016-11-03 00:56:56 -04001994 if (!crtc->state->enable) {
1995 SDE_DEBUG("crtc%d -> enable %d, skip atomic_flush\n",
1996 crtc->base.id, crtc->state->enable);
1997 return;
1998 }
1999
2000 SDE_DEBUG("crtc%d\n", crtc->base.id);
Clarence Ipcae1bb62016-07-07 12:07:13 -04002001
2002 sde_crtc = to_sde_crtc(crtc);
Dhaval Patel82c8dbc2017-02-18 23:15:10 -08002003 cstate = to_sde_crtc_state(crtc->state);
Clarence Ipcae1bb62016-07-07 12:07:13 -04002004 dev = crtc->dev;
2005
Lloyd Atkinson265d2212016-05-30 13:12:01 -04002006 if (sde_crtc->event) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07002007 SDE_DEBUG("already received sde_crtc->event\n");
Lloyd Atkinson265d2212016-05-30 13:12:01 -04002008 } else {
Lloyd Atkinson265d2212016-05-30 13:12:01 -04002009 spin_lock_irqsave(&dev->event_lock, flags);
2010 sde_crtc->event = crtc->state->event;
2011 spin_unlock_irqrestore(&dev->event_lock, flags);
2012 }
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002013
2014 /*
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04002015 * If no mixers has been allocated in sde_crtc_atomic_check(),
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002016 * it means we are trying to flush a CRTC whose state is disabled:
2017 * nothing else needs to be done.
2018 */
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04002019 if (unlikely(!sde_crtc->num_mixers))
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002020 return;
2021
Alan Kwong346223e2017-06-30 15:29:22 -04002022 /*
2023 * For planes without commit update, drm framework will not add
2024 * those planes to current state since hardware update is not
2025 * required. However, if those planes were power collapsed since
2026 * last commit cycle, driver has to restore the hardware state
2027 * of those planes explicitly here prior to plane flush.
2028 */
2029 drm_atomic_crtc_for_each_plane(plane, crtc)
2030 sde_plane_restore(plane);
2031
Clarence Ipcae1bb62016-07-07 12:07:13 -04002032 /* wait for acquire fences before anything else is done */
2033 _sde_crtc_wait_for_fences(crtc);
2034
Dhaval Patel82c8dbc2017-02-18 23:15:10 -08002035 if (!cstate->rsc_update) {
2036 drm_for_each_encoder(encoder, dev) {
2037 if (encoder->crtc != crtc)
2038 continue;
2039
2040 cstate->rsc_client =
Dhaval Patel30fae8a2017-04-21 18:42:41 -07002041 sde_encoder_get_rsc_client(encoder);
Dhaval Patel82c8dbc2017-02-18 23:15:10 -08002042 }
2043 cstate->rsc_update = true;
2044 }
2045
Alan Kwong9aa061c2016-11-06 21:17:12 -05002046 /* update performance setting before crtc kickoff */
2047 sde_core_perf_crtc_update(crtc, 1, false);
2048
Clarence Ipcae1bb62016-07-07 12:07:13 -04002049 /*
2050 * Final plane updates: Give each plane a chance to complete all
2051 * required writes/flushing before crtc's "flush
2052 * everything" call below.
2053 */
2054 drm_atomic_crtc_for_each_plane(plane, crtc)
2055 sde_plane_flush(plane);
2056
Lloyd Atkinson5d722782016-05-30 14:09:41 -04002057 /* Kickoff will be scheduled by outer layer */
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07002058}
2059
Clarence Ip7a753bb2016-07-07 11:47:44 -04002060/**
2061 * sde_crtc_destroy_state - state destroy hook
2062 * @crtc: drm CRTC
2063 * @state: CRTC state object to release
2064 */
2065static void sde_crtc_destroy_state(struct drm_crtc *crtc,
2066 struct drm_crtc_state *state)
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07002067{
Clarence Ip7a753bb2016-07-07 11:47:44 -04002068 struct sde_crtc *sde_crtc;
2069 struct sde_crtc_state *cstate;
2070
2071 if (!crtc || !state) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07002072 SDE_ERROR("invalid argument(s)\n");
Clarence Ip7a753bb2016-07-07 11:47:44 -04002073 return;
2074 }
2075
2076 sde_crtc = to_sde_crtc(crtc);
2077 cstate = to_sde_crtc_state(state);
2078
Alan Kwong163d2612016-11-03 00:56:56 -04002079 SDE_DEBUG("crtc%d\n", crtc->base.id);
Clarence Ip7a753bb2016-07-07 11:47:44 -04002080
Alan Kwongcdb2f282017-03-18 13:42:06 -07002081 _sde_crtc_rp_destroy(&cstate->rp);
2082
Dhaval Patel04c7e8e2016-09-26 20:14:31 -07002083 __drm_atomic_helper_crtc_destroy_state(state);
Clarence Ip7a753bb2016-07-07 11:47:44 -04002084
2085 /* destroy value helper */
2086 msm_property_destroy_state(&sde_crtc->property_info, cstate,
Clarence Ip4a2955d2017-07-04 18:04:33 -04002087 &cstate->property_state);
Clarence Ip7a753bb2016-07-07 11:47:44 -04002088}
2089
Veera Sundaram Sankaran7ee99092017-06-13 11:19:36 -07002090static int _sde_crtc_wait_for_frame_done(struct drm_crtc *crtc)
2091{
2092 struct sde_crtc *sde_crtc;
2093 int ret, rc = 0;
2094
2095 if (!crtc) {
2096 SDE_ERROR("invalid argument\n");
2097 return -EINVAL;
2098 }
2099 sde_crtc = to_sde_crtc(crtc);
2100
2101 if (!atomic_read(&sde_crtc->frame_pending)) {
2102 SDE_DEBUG("no frames pending\n");
2103 return 0;
2104 }
2105
Dhaval Patela5f75952017-07-25 11:17:41 -07002106 SDE_EVT32_VERBOSE(DRMID(crtc), SDE_EVTLOG_FUNC_ENTRY);
Veera Sundaram Sankaran7ee99092017-06-13 11:19:36 -07002107 ret = wait_for_completion_timeout(&sde_crtc->frame_done_comp,
2108 msecs_to_jiffies(SDE_FRAME_DONE_TIMEOUT));
2109 if (!ret) {
2110 SDE_ERROR("frame done completion wait timed out, ret:%d\n",
2111 ret);
2112 SDE_EVT32(DRMID(crtc), SDE_EVTLOG_FATAL);
2113 rc = -ETIMEDOUT;
2114 }
Dhaval Patela5f75952017-07-25 11:17:41 -07002115 SDE_EVT32_VERBOSE(DRMID(crtc), SDE_EVTLOG_FUNC_EXIT);
Veera Sundaram Sankaran7ee99092017-06-13 11:19:36 -07002116
2117 return rc;
2118}
2119
Lloyd Atkinson5d722782016-05-30 14:09:41 -04002120void sde_crtc_commit_kickoff(struct drm_crtc *crtc)
2121{
Lloyd Atkinsone7bcdd22016-08-11 10:53:37 -04002122 struct drm_encoder *encoder;
2123 struct drm_device *dev;
Alan Kwong628d19e2016-10-31 13:50:13 -04002124 struct sde_crtc *sde_crtc;
Alan Kwong67a3f792016-11-01 23:16:53 -04002125 struct msm_drm_private *priv;
2126 struct sde_kms *sde_kms;
Alan Kwong4aacd532017-02-04 18:51:33 -08002127 struct sde_crtc_state *cstate;
Veera Sundaram Sankarana90e1392017-07-06 15:00:09 -07002128 int ret;
Lloyd Atkinson5d722782016-05-30 14:09:41 -04002129
2130 if (!crtc) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07002131 SDE_ERROR("invalid argument\n");
Lloyd Atkinson5d722782016-05-30 14:09:41 -04002132 return;
2133 }
Lloyd Atkinsone7bcdd22016-08-11 10:53:37 -04002134 dev = crtc->dev;
Alan Kwong628d19e2016-10-31 13:50:13 -04002135 sde_crtc = to_sde_crtc(crtc);
Alan Kwong67a3f792016-11-01 23:16:53 -04002136 sde_kms = _sde_crtc_get_kms(crtc);
Narendra Muppallaec11a0a2017-06-15 15:35:17 -07002137
2138 if (!sde_kms || !sde_kms->dev || !sde_kms->dev->dev_private) {
2139 SDE_ERROR("invalid argument\n");
2140 return;
2141 }
2142
Alan Kwong67a3f792016-11-01 23:16:53 -04002143 priv = sde_kms->dev->dev_private;
Alan Kwong4aacd532017-02-04 18:51:33 -08002144 cstate = to_sde_crtc_state(crtc->state);
Lloyd Atkinson5d722782016-05-30 14:09:41 -04002145
Clarence Ip90b282d2017-05-04 10:00:32 -07002146 /*
2147 * If no mixers has been allocated in sde_crtc_atomic_check(),
2148 * it means we are trying to start a CRTC whose state is disabled:
2149 * nothing else needs to be done.
2150 */
2151 if (unlikely(!sde_crtc->num_mixers))
2152 return;
2153
Narendra Muppalla77b32932017-05-10 13:53:11 -07002154 SDE_ATRACE_BEGIN("crtc_commit");
Lloyd Atkinsone7bcdd22016-08-11 10:53:37 -04002155 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
Alan Kwong4aacd532017-02-04 18:51:33 -08002156 struct sde_encoder_kickoff_params params = { 0 };
2157
Lloyd Atkinsone7bcdd22016-08-11 10:53:37 -04002158 if (encoder->crtc != crtc)
2159 continue;
2160
2161 /*
2162 * Encoder will flush/start now, unless it has a tx pending.
2163 * If so, it may delay and flush at an irq event (e.g. ppdone)
2164 */
Alan Kwong4aacd532017-02-04 18:51:33 -08002165 params.inline_rotate_prefill = cstate->sbuf_prefill_line;
Lloyd Atkinson73fb8092017-02-08 16:02:55 -05002166 params.affected_displays = _sde_crtc_get_displays_affected(crtc,
2167 crtc->state);
Alan Kwong4aacd532017-02-04 18:51:33 -08002168 sde_encoder_prepare_for_kickoff(encoder, &params);
Alan Kwong628d19e2016-10-31 13:50:13 -04002169 }
2170
Veera Sundaram Sankaran7ee99092017-06-13 11:19:36 -07002171 /* wait for frame_event_done completion */
Veera Sundaram Sankarana90e1392017-07-06 15:00:09 -07002172 SDE_ATRACE_BEGIN("wait_for_frame_done_event");
2173 ret = _sde_crtc_wait_for_frame_done(crtc);
2174 SDE_ATRACE_END("wait_for_frame_done_event");
2175 if (ret) {
Veera Sundaram Sankaran7ee99092017-06-13 11:19:36 -07002176 SDE_ERROR("crtc%d wait for frame done failed;frame_pending%d\n",
2177 crtc->base.id,
2178 atomic_read(&sde_crtc->frame_pending));
Narendra Muppalla77b32932017-05-10 13:53:11 -07002179 goto end;
Veera Sundaram Sankaran7ee99092017-06-13 11:19:36 -07002180 }
2181
2182 if (atomic_inc_return(&sde_crtc->frame_pending) == 1) {
Alan Kwong628d19e2016-10-31 13:50:13 -04002183 /* acquire bandwidth and other resources */
2184 SDE_DEBUG("crtc%d first commit\n", crtc->base.id);
Veera Sundaram Sankaran7ee99092017-06-13 11:19:36 -07002185 SDE_EVT32(DRMID(crtc), SDE_EVTLOG_FUNC_CASE1);
Alan Kwong628d19e2016-10-31 13:50:13 -04002186 } else {
2187 SDE_DEBUG("crtc%d commit\n", crtc->base.id);
Veera Sundaram Sankaran7ee99092017-06-13 11:19:36 -07002188 SDE_EVT32(DRMID(crtc), SDE_EVTLOG_FUNC_CASE2);
Alan Kwong628d19e2016-10-31 13:50:13 -04002189 }
Dhaval Pateld67cf4a2017-06-14 18:08:32 -07002190 sde_crtc->play_count++;
Alan Kwong628d19e2016-10-31 13:50:13 -04002191
2192 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
2193 if (encoder->crtc != crtc)
2194 continue;
2195
2196 sde_encoder_kickoff(encoder);
Lloyd Atkinsone7bcdd22016-08-11 10:53:37 -04002197 }
Veera Sundaram Sankaran7ee99092017-06-13 11:19:36 -07002198
2199 reinit_completion(&sde_crtc->frame_done_comp);
2200
Narendra Muppalla77b32932017-05-10 13:53:11 -07002201end:
2202 SDE_ATRACE_END("crtc_commit");
2203 return;
Lloyd Atkinson5d722782016-05-30 14:09:41 -04002204}
2205
Clarence Ip7a753bb2016-07-07 11:47:44 -04002206/**
Lloyd Atkinsondcb1c4a2017-07-27 10:52:09 -04002207 * _sde_crtc_vblank_enable_no_lock - update power resource and vblank request
Clarence Ip7f70ce42017-03-20 06:53:46 -07002208 * @sde_crtc: Pointer to sde crtc structure
2209 * @enable: Whether to enable/disable vblanks
Lloyd Atkinsondcb1c4a2017-07-27 10:52:09 -04002210 *
2211 * @Return: error code
Clarence Ip7f70ce42017-03-20 06:53:46 -07002212 */
Lloyd Atkinsondcb1c4a2017-07-27 10:52:09 -04002213static int _sde_crtc_vblank_enable_no_lock(
Clarence Ip7f70ce42017-03-20 06:53:46 -07002214 struct sde_crtc *sde_crtc, bool enable)
2215{
2216 struct drm_device *dev;
2217 struct drm_crtc *crtc;
2218 struct drm_encoder *enc;
Clarence Ip7f70ce42017-03-20 06:53:46 -07002219
2220 if (!sde_crtc) {
2221 SDE_ERROR("invalid crtc\n");
Lloyd Atkinsondcb1c4a2017-07-27 10:52:09 -04002222 return -EINVAL;
Clarence Ip7f70ce42017-03-20 06:53:46 -07002223 }
2224
2225 crtc = &sde_crtc->base;
2226 dev = crtc->dev;
Clarence Ip7f70ce42017-03-20 06:53:46 -07002227
2228 if (enable) {
Lloyd Atkinson2c554eb2017-05-24 16:22:39 -04002229 int ret;
2230
2231 /* drop lock since power crtc cb may try to re-acquire lock */
2232 mutex_unlock(&sde_crtc->crtc_lock);
2233 ret = _sde_crtc_power_enable(sde_crtc, true);
2234 mutex_lock(&sde_crtc->crtc_lock);
2235 if (ret)
Lloyd Atkinsondcb1c4a2017-07-27 10:52:09 -04002236 return ret;
Dhaval Patelf9245d62017-03-28 16:24:00 -07002237
Clarence Ip7f70ce42017-03-20 06:53:46 -07002238 list_for_each_entry(enc, &dev->mode_config.encoder_list, head) {
2239 if (enc->crtc != crtc)
2240 continue;
2241
Lloyd Atkinsondcb1c4a2017-07-27 10:52:09 -04002242 SDE_EVT32(DRMID(&sde_crtc->base), DRMID(enc), enable,
2243 sde_crtc->enabled,
2244 sde_crtc->suspend,
2245 sde_crtc->vblank_requested);
Clarence Ip7f70ce42017-03-20 06:53:46 -07002246
2247 sde_encoder_register_vblank_callback(enc,
2248 sde_crtc_vblank_cb, (void *)crtc);
2249 }
2250 } else {
2251 list_for_each_entry(enc, &dev->mode_config.encoder_list, head) {
2252 if (enc->crtc != crtc)
2253 continue;
2254
Lloyd Atkinsondcb1c4a2017-07-27 10:52:09 -04002255 SDE_EVT32(DRMID(&sde_crtc->base), DRMID(enc), enable,
2256 sde_crtc->enabled,
2257 sde_crtc->suspend,
2258 sde_crtc->vblank_requested);
Clarence Ip7f70ce42017-03-20 06:53:46 -07002259
2260 sde_encoder_register_vblank_callback(enc, NULL, NULL);
2261 }
Lloyd Atkinson2c554eb2017-05-24 16:22:39 -04002262
2263 /* drop lock since power crtc cb may try to re-acquire lock */
2264 mutex_unlock(&sde_crtc->crtc_lock);
Dhaval Patelf9245d62017-03-28 16:24:00 -07002265 _sde_crtc_power_enable(sde_crtc, false);
Lloyd Atkinson2c554eb2017-05-24 16:22:39 -04002266 mutex_lock(&sde_crtc->crtc_lock);
Clarence Ip7f70ce42017-03-20 06:53:46 -07002267 }
Lloyd Atkinsondcb1c4a2017-07-27 10:52:09 -04002268
2269 return 0;
Clarence Ip7f70ce42017-03-20 06:53:46 -07002270}
2271
2272/**
2273 * _sde_crtc_set_suspend - notify crtc of suspend enable/disable
2274 * @crtc: Pointer to drm crtc object
2275 * @enable: true to enable suspend, false to indicate resume
2276 */
2277static void _sde_crtc_set_suspend(struct drm_crtc *crtc, bool enable)
2278{
2279 struct sde_crtc *sde_crtc;
2280 struct msm_drm_private *priv;
2281 struct sde_kms *sde_kms;
Gopikrishnaiah Anandan84b4f672017-04-26 10:28:51 -07002282 struct drm_event event;
2283 u32 power_on;
Lloyd Atkinsondcb1c4a2017-07-27 10:52:09 -04002284 int ret = 0;
Clarence Ip7f70ce42017-03-20 06:53:46 -07002285
2286 if (!crtc || !crtc->dev || !crtc->dev->dev_private) {
2287 SDE_ERROR("invalid crtc\n");
2288 return;
2289 }
2290 sde_crtc = to_sde_crtc(crtc);
2291 priv = crtc->dev->dev_private;
2292
2293 if (!priv->kms) {
2294 SDE_ERROR("invalid crtc kms\n");
2295 return;
2296 }
2297 sde_kms = to_sde_kms(priv->kms);
2298
2299 SDE_DEBUG("crtc%d suspend = %d\n", crtc->base.id, enable);
Lloyd Atkinsondcb1c4a2017-07-27 10:52:09 -04002300 SDE_EVT32_VERBOSE(DRMID(crtc), enable);
Clarence Ip7f70ce42017-03-20 06:53:46 -07002301
2302 mutex_lock(&sde_crtc->crtc_lock);
2303
Gopikrishnaiah Anandan84b4f672017-04-26 10:28:51 -07002304 event.type = DRM_EVENT_CRTC_POWER;
2305 event.length = sizeof(u32);
Clarence Ip7f70ce42017-03-20 06:53:46 -07002306 /*
Clarence Ip2f9beeb2017-03-16 11:04:53 -04002307 * Update CP on suspend/resume transitions
2308 */
Gopikrishnaiah Anandan84b4f672017-04-26 10:28:51 -07002309 if (enable && !sde_crtc->suspend) {
Clarence Ip2f9beeb2017-03-16 11:04:53 -04002310 sde_cp_crtc_suspend(crtc);
Gopikrishnaiah Anandan84b4f672017-04-26 10:28:51 -07002311 power_on = 0;
2312 } else if (!enable && sde_crtc->suspend) {
Clarence Ip2f9beeb2017-03-16 11:04:53 -04002313 sde_cp_crtc_resume(crtc);
Gopikrishnaiah Anandan84b4f672017-04-26 10:28:51 -07002314 power_on = 1;
2315 }
Clarence Ip2f9beeb2017-03-16 11:04:53 -04002316
2317 /*
Lloyd Atkinsonb2be0c42017-07-17 16:41:00 -04002318 * If the vblank is enabled, release a power reference on suspend
2319 * and take it back during resume (if it is still enabled).
Clarence Ip7f70ce42017-03-20 06:53:46 -07002320 */
Lloyd Atkinsondcb1c4a2017-07-27 10:52:09 -04002321 SDE_EVT32(DRMID(&sde_crtc->base), enable, sde_crtc->enabled,
2322 sde_crtc->suspend, sde_crtc->vblank_requested);
Clarence Ip7f70ce42017-03-20 06:53:46 -07002323 if (sde_crtc->suspend == enable)
2324 SDE_DEBUG("crtc%d suspend already set to %d, ignoring update\n",
2325 crtc->base.id, enable);
Lloyd Atkinsondcb1c4a2017-07-27 10:52:09 -04002326 else if (sde_crtc->enabled && sde_crtc->vblank_requested) {
2327 ret = _sde_crtc_vblank_enable_no_lock(sde_crtc, !enable);
2328 if (ret)
2329 SDE_ERROR("%s vblank enable failed: %d\n",
2330 sde_crtc->name, ret);
2331 }
Clarence Ip7f70ce42017-03-20 06:53:46 -07002332
2333 sde_crtc->suspend = enable;
Benjamin Chan34a92c72017-06-28 11:01:18 -04002334 msm_mode_object_event_notify(&crtc->base, crtc->dev, &event,
Gopikrishnaiah Anandan84b4f672017-04-26 10:28:51 -07002335 (u8 *)&power_on);
Clarence Ip7f70ce42017-03-20 06:53:46 -07002336 mutex_unlock(&sde_crtc->crtc_lock);
2337}
2338
2339/**
Clarence Ip7a753bb2016-07-07 11:47:44 -04002340 * sde_crtc_duplicate_state - state duplicate hook
2341 * @crtc: Pointer to drm crtc structure
2342 * @Returns: Pointer to new drm_crtc_state structure
2343 */
2344static struct drm_crtc_state *sde_crtc_duplicate_state(struct drm_crtc *crtc)
2345{
2346 struct sde_crtc *sde_crtc;
2347 struct sde_crtc_state *cstate, *old_cstate;
2348
2349 if (!crtc || !crtc->state) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07002350 SDE_ERROR("invalid argument(s)\n");
Clarence Ip7a753bb2016-07-07 11:47:44 -04002351 return NULL;
2352 }
2353
2354 sde_crtc = to_sde_crtc(crtc);
2355 old_cstate = to_sde_crtc_state(crtc->state);
2356 cstate = msm_property_alloc_state(&sde_crtc->property_info);
2357 if (!cstate) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07002358 SDE_ERROR("failed to allocate state\n");
Clarence Ip7a753bb2016-07-07 11:47:44 -04002359 return NULL;
2360 }
2361
2362 /* duplicate value helper */
2363 msm_property_duplicate_state(&sde_crtc->property_info,
2364 old_cstate, cstate,
Clarence Ip4a2955d2017-07-04 18:04:33 -04002365 &cstate->property_state, cstate->property_values);
Clarence Ip7a753bb2016-07-07 11:47:44 -04002366
2367 /* duplicate base helper */
2368 __drm_atomic_helper_crtc_duplicate_state(crtc, &cstate->base);
2369
Alan Kwongcdb2f282017-03-18 13:42:06 -07002370 _sde_crtc_rp_duplicate(&old_cstate->rp, &cstate->rp);
2371
Clarence Ip7a753bb2016-07-07 11:47:44 -04002372 return &cstate->base;
2373}
2374
2375/**
2376 * sde_crtc_reset - reset hook for CRTCs
2377 * Resets the atomic state for @crtc by freeing the state pointer (which might
2378 * be NULL, e.g. at driver load time) and allocating a new empty state object.
2379 * @crtc: Pointer to drm crtc structure
2380 */
2381static void sde_crtc_reset(struct drm_crtc *crtc)
2382{
2383 struct sde_crtc *sde_crtc;
2384 struct sde_crtc_state *cstate;
2385
2386 if (!crtc) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07002387 SDE_ERROR("invalid crtc\n");
Clarence Ip7a753bb2016-07-07 11:47:44 -04002388 return;
2389 }
2390
Clarence Ip7f70ce42017-03-20 06:53:46 -07002391 /* revert suspend actions, if necessary */
2392 if (msm_is_suspend_state(crtc->dev))
2393 _sde_crtc_set_suspend(crtc, false);
2394
Clarence Ip7a753bb2016-07-07 11:47:44 -04002395 /* remove previous state, if present */
2396 if (crtc->state) {
2397 sde_crtc_destroy_state(crtc, crtc->state);
2398 crtc->state = 0;
2399 }
2400
2401 sde_crtc = to_sde_crtc(crtc);
2402 cstate = msm_property_alloc_state(&sde_crtc->property_info);
2403 if (!cstate) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07002404 SDE_ERROR("failed to allocate state\n");
Clarence Ip7a753bb2016-07-07 11:47:44 -04002405 return;
2406 }
2407
2408 /* reset value helper */
2409 msm_property_reset_state(&sde_crtc->property_info, cstate,
Clarence Ip4a2955d2017-07-04 18:04:33 -04002410 &cstate->property_state,
2411 cstate->property_values);
Clarence Ip7a753bb2016-07-07 11:47:44 -04002412
Clarence Ipcae1bb62016-07-07 12:07:13 -04002413 _sde_crtc_set_input_fence_timeout(cstate);
2414
Alan Kwongcdb2f282017-03-18 13:42:06 -07002415 _sde_crtc_rp_reset(&cstate->rp);
2416
Clarence Ip7a753bb2016-07-07 11:47:44 -04002417 cstate->base.crtc = crtc;
2418 crtc->state = &cstate->base;
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07002419}
2420
Veera Sundaram Sankaran82916e02017-03-29 18:44:22 -07002421static void sde_crtc_handle_power_event(u32 event_type, void *arg)
2422{
2423 struct drm_crtc *crtc = arg;
2424 struct sde_crtc *sde_crtc;
2425 struct drm_encoder *encoder;
Benjamin Chan90139102017-06-21 16:00:39 -04002426 struct drm_event event;
2427 u32 power_on = 0;
Veera Sundaram Sankaran82916e02017-03-29 18:44:22 -07002428
2429 if (!crtc) {
2430 SDE_ERROR("invalid crtc\n");
2431 return;
2432 }
2433 sde_crtc = to_sde_crtc(crtc);
2434
2435 mutex_lock(&sde_crtc->crtc_lock);
2436
2437 SDE_EVT32(DRMID(crtc), event_type);
2438
2439 if (event_type == SDE_POWER_EVENT_POST_ENABLE) {
2440 /* restore encoder; crtc will be programmed during commit */
2441 drm_for_each_encoder(encoder, crtc->dev) {
2442 if (encoder->crtc != crtc)
2443 continue;
2444
2445 sde_encoder_virt_restore(encoder);
2446 }
Ping Lie505f3b2017-06-19 14:19:08 -07002447 sde_cp_crtc_post_ipc(crtc);
Veera Sundaram Sankaran82916e02017-03-29 18:44:22 -07002448
Benjamin Chan90139102017-06-21 16:00:39 -04002449 event.type = DRM_EVENT_SDE_POWER;
2450 event.length = sizeof(power_on);
2451 power_on = 1;
2452 msm_mode_object_event_notify(&crtc->base, crtc->dev, &event,
2453 (u8 *)&power_on);
Veera Sundaram Sankaran82916e02017-03-29 18:44:22 -07002454 } else if (event_type == SDE_POWER_EVENT_POST_DISABLE) {
2455 struct drm_plane *plane;
2456
2457 /*
2458 * set revalidate flag in planes, so it will be re-programmed
2459 * in the next frame update
2460 */
2461 drm_atomic_crtc_for_each_plane(plane, crtc)
2462 sde_plane_set_revalidate(plane, true);
Alan Kwong8a9b38a2017-06-22 11:30:52 -04002463
Gopikrishnaiah Anandandb90fa12017-05-09 17:56:08 -07002464 sde_cp_crtc_suspend(crtc);
Benjamin Chan90139102017-06-21 16:00:39 -04002465
2466 event.type = DRM_EVENT_SDE_POWER;
2467 event.length = sizeof(power_on);
2468 power_on = 0;
2469 msm_mode_object_event_notify(&crtc->base, crtc->dev, &event,
2470 (u8 *)&power_on);
Ping Lie505f3b2017-06-19 14:19:08 -07002471 } else if (event_type == SDE_POWER_EVENT_PRE_DISABLE) {
2472 sde_cp_crtc_pre_ipc(crtc);
Veera Sundaram Sankaran82916e02017-03-29 18:44:22 -07002473 }
2474
2475 mutex_unlock(&sde_crtc->crtc_lock);
2476}
2477
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002478static void sde_crtc_disable(struct drm_crtc *crtc)
2479{
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04002480 struct sde_crtc *sde_crtc;
Dhaval Patel82c8dbc2017-02-18 23:15:10 -08002481 struct sde_crtc_state *cstate;
Alan Kwong07da0982016-11-04 12:57:45 -04002482 struct drm_encoder *encoder;
Veera Sundaram Sankaran82916e02017-03-29 18:44:22 -07002483 struct msm_drm_private *priv;
Gopikrishnaiah Anandande2c81b2017-03-15 12:41:29 -07002484 unsigned long flags;
2485 struct sde_crtc_irq_info *node = NULL;
2486 int ret;
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04002487
Veera Sundaram Sankaran82916e02017-03-29 18:44:22 -07002488 if (!crtc || !crtc->dev || !crtc->dev->dev_private || !crtc->state) {
Lloyd Atkinson4f1c8692016-09-14 14:04:25 -04002489 SDE_ERROR("invalid crtc\n");
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04002490 return;
2491 }
2492 sde_crtc = to_sde_crtc(crtc);
Dhaval Patel82c8dbc2017-02-18 23:15:10 -08002493 cstate = to_sde_crtc_state(crtc->state);
Veera Sundaram Sankaran82916e02017-03-29 18:44:22 -07002494 priv = crtc->dev->dev_private;
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04002495
Alan Kwong163d2612016-11-03 00:56:56 -04002496 SDE_DEBUG("crtc%d\n", crtc->base.id);
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04002497
Clarence Ip7f70ce42017-03-20 06:53:46 -07002498 if (msm_is_suspend_state(crtc->dev))
2499 _sde_crtc_set_suspend(crtc, true);
2500
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07002501 mutex_lock(&sde_crtc->crtc_lock);
Lloyd Atkinsondcb1c4a2017-07-27 10:52:09 -04002502 SDE_EVT32_VERBOSE(DRMID(crtc));
Alan Kwong628d19e2016-10-31 13:50:13 -04002503
Veera Sundaram Sankaran7ee99092017-06-13 11:19:36 -07002504 /* wait for frame_event_done completion */
2505 if (_sde_crtc_wait_for_frame_done(crtc))
2506 SDE_ERROR("crtc%d wait for frame done failed;frame_pending%d\n",
2507 crtc->base.id,
2508 atomic_read(&sde_crtc->frame_pending));
2509
Lloyd Atkinsondcb1c4a2017-07-27 10:52:09 -04002510 SDE_EVT32(DRMID(crtc), sde_crtc->enabled, sde_crtc->suspend,
2511 sde_crtc->vblank_requested);
2512 if (sde_crtc->enabled && !sde_crtc->suspend &&
2513 sde_crtc->vblank_requested) {
2514 ret = _sde_crtc_vblank_enable_no_lock(sde_crtc, false);
2515 if (ret)
2516 SDE_ERROR("%s vblank enable failed: %d\n",
2517 sde_crtc->name, ret);
Alan Kwong07da0982016-11-04 12:57:45 -04002518 }
Lloyd Atkinsondcb1c4a2017-07-27 10:52:09 -04002519 sde_crtc->enabled = false;
Alan Kwong07da0982016-11-04 12:57:45 -04002520
Alan Kwong628d19e2016-10-31 13:50:13 -04002521 if (atomic_read(&sde_crtc->frame_pending)) {
Dhaval Patel6c666622017-03-21 23:02:59 -07002522 SDE_EVT32(DRMID(crtc), atomic_read(&sde_crtc->frame_pending),
2523 SDE_EVTLOG_FUNC_CASE2);
Alan Kwong9aa061c2016-11-06 21:17:12 -05002524 sde_core_perf_crtc_release_bw(crtc);
Alan Kwong628d19e2016-10-31 13:50:13 -04002525 atomic_set(&sde_crtc->frame_pending, 0);
2526 }
2527
Ping Li6d5bf542017-06-27 11:40:28 -07002528 spin_lock_irqsave(&sde_crtc->spin_lock, flags);
2529 list_for_each_entry(node, &sde_crtc->user_event_list, list) {
2530 ret = 0;
2531 if (node->func)
2532 ret = node->func(crtc, false, &node->irq);
2533 if (ret)
2534 SDE_ERROR("%s failed to disable event %x\n",
2535 sde_crtc->name, node->event);
2536 }
2537 spin_unlock_irqrestore(&sde_crtc->spin_lock, flags);
2538
Alan Kwong9aa061c2016-11-06 21:17:12 -05002539 sde_core_perf_crtc_update(crtc, 0, true);
2540
Alan Kwong628d19e2016-10-31 13:50:13 -04002541 drm_for_each_encoder(encoder, crtc->dev) {
2542 if (encoder->crtc != crtc)
2543 continue;
2544 sde_encoder_register_frame_event_callback(encoder, NULL, NULL);
Dhaval Patel82c8dbc2017-02-18 23:15:10 -08002545 cstate->rsc_client = NULL;
2546 cstate->rsc_update = false;
Alan Kwong628d19e2016-10-31 13:50:13 -04002547 }
2548
Veera Sundaram Sankaran82916e02017-03-29 18:44:22 -07002549 if (sde_crtc->power_event)
2550 sde_power_handle_unregister_event(&priv->phandle,
2551 sde_crtc->power_event);
2552
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04002553 memset(sde_crtc->mixers, 0, sizeof(sde_crtc->mixers));
2554 sde_crtc->num_mixers = 0;
Gopikrishnaiah Anandande2c81b2017-03-15 12:41:29 -07002555
Alan Kwong8411a9112017-06-06 19:29:01 -04002556 /* disable clk & bw control until clk & bw properties are set */
2557 cstate->bw_control = false;
Alan Kwong0230a102017-05-16 11:36:44 -07002558 cstate->bw_split_vote = false;
Alan Kwong8411a9112017-06-06 19:29:01 -04002559
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07002560 mutex_unlock(&sde_crtc->crtc_lock);
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002561}
2562
2563static void sde_crtc_enable(struct drm_crtc *crtc)
2564{
Clarence Ipcae1bb62016-07-07 12:07:13 -04002565 struct sde_crtc *sde_crtc;
Alan Kwong628d19e2016-10-31 13:50:13 -04002566 struct drm_encoder *encoder;
Veera Sundaram Sankaran82916e02017-03-29 18:44:22 -07002567 struct msm_drm_private *priv;
Gopikrishnaiah Anandande2c81b2017-03-15 12:41:29 -07002568 unsigned long flags;
2569 struct sde_crtc_irq_info *node = NULL;
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05002570 int ret;
Lloyd Atkinsonaf7952d2016-06-26 22:41:26 -04002571
Veera Sundaram Sankaran82916e02017-03-29 18:44:22 -07002572 if (!crtc || !crtc->dev || !crtc->dev->dev_private) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07002573 SDE_ERROR("invalid crtc\n");
Clarence Ipcae1bb62016-07-07 12:07:13 -04002574 return;
2575 }
Veera Sundaram Sankaran82916e02017-03-29 18:44:22 -07002576 priv = crtc->dev->dev_private;
Clarence Ipcae1bb62016-07-07 12:07:13 -04002577
Alan Kwong163d2612016-11-03 00:56:56 -04002578 SDE_DEBUG("crtc%d\n", crtc->base.id);
Lloyd Atkinsondcb1c4a2017-07-27 10:52:09 -04002579 SDE_EVT32_VERBOSE(DRMID(crtc));
Clarence Ipcae1bb62016-07-07 12:07:13 -04002580 sde_crtc = to_sde_crtc(crtc);
Lloyd Atkinsonaf7952d2016-06-26 22:41:26 -04002581
Alan Kwong628d19e2016-10-31 13:50:13 -04002582 drm_for_each_encoder(encoder, crtc->dev) {
2583 if (encoder->crtc != crtc)
2584 continue;
2585 sde_encoder_register_frame_event_callback(encoder,
2586 sde_crtc_frame_event_cb, (void *)crtc);
2587 }
2588
Lloyd Atkinsonb2be0c42017-07-17 16:41:00 -04002589 mutex_lock(&sde_crtc->crtc_lock);
Lloyd Atkinsondcb1c4a2017-07-27 10:52:09 -04002590 SDE_EVT32(DRMID(crtc), sde_crtc->enabled, sde_crtc->suspend,
2591 sde_crtc->vblank_requested);
2592 if (!sde_crtc->enabled && !sde_crtc->suspend &&
2593 sde_crtc->vblank_requested) {
2594 ret = _sde_crtc_vblank_enable_no_lock(sde_crtc, true);
2595 if (ret)
2596 SDE_ERROR("%s vblank enable failed: %d\n",
2597 sde_crtc->name, ret);
Lloyd Atkinsonb2be0c42017-07-17 16:41:00 -04002598 }
Lloyd Atkinsondcb1c4a2017-07-27 10:52:09 -04002599 sde_crtc->enabled = true;
Lloyd Atkinsonb2be0c42017-07-17 16:41:00 -04002600 mutex_unlock(&sde_crtc->crtc_lock);
2601
Gopikrishnaiah Anandande2c81b2017-03-15 12:41:29 -07002602 spin_lock_irqsave(&sde_crtc->spin_lock, flags);
2603 list_for_each_entry(node, &sde_crtc->user_event_list, list) {
2604 ret = 0;
2605 if (node->func)
2606 ret = node->func(crtc, true, &node->irq);
2607 if (ret)
2608 SDE_ERROR("%s failed to enable event %x\n",
2609 sde_crtc->name, node->event);
2610 }
2611 spin_unlock_irqrestore(&sde_crtc->spin_lock, flags);
Veera Sundaram Sankaran82916e02017-03-29 18:44:22 -07002612
2613 sde_crtc->power_event = sde_power_handle_register_event(
2614 &priv->phandle,
Ping Lie505f3b2017-06-19 14:19:08 -07002615 SDE_POWER_EVENT_POST_ENABLE | SDE_POWER_EVENT_POST_DISABLE |
2616 SDE_POWER_EVENT_PRE_DISABLE,
Veera Sundaram Sankaran82916e02017-03-29 18:44:22 -07002617 sde_crtc_handle_power_event, crtc, sde_crtc->name);
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002618}
2619
2620struct plane_state {
Dhaval Patelec10fad2016-08-22 14:40:48 -07002621 struct sde_plane_state *sde_pstate;
Dhaval Patel04c7e8e2016-09-26 20:14:31 -07002622 const struct drm_plane_state *drm_pstate;
Clarence Ipc47a0692016-10-11 10:54:17 -04002623 int stage;
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08002624 u32 pipe_id;
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002625};
2626
Clarence Ipc47a0692016-10-11 10:54:17 -04002627static int pstate_cmp(const void *a, const void *b)
2628{
2629 struct plane_state *pa = (struct plane_state *)a;
2630 struct plane_state *pb = (struct plane_state *)b;
2631 int rc = 0;
2632 int pa_zpos, pb_zpos;
2633
2634 pa_zpos = sde_plane_get_property(pa->sde_pstate, PLANE_PROP_ZPOS);
2635 pb_zpos = sde_plane_get_property(pb->sde_pstate, PLANE_PROP_ZPOS);
2636
2637 if (pa_zpos != pb_zpos)
2638 rc = pa_zpos - pb_zpos;
2639 else
2640 rc = pa->drm_pstate->crtc_x - pb->drm_pstate->crtc_x;
2641
2642 return rc;
2643}
2644
Dhaval Patela8d6bc62017-05-10 17:40:18 -07002645static int _sde_crtc_excl_rect_overlap_check(struct plane_state pstates[],
2646 int cnt, int curr_cnt, struct sde_rect *excl_rect, int z_pos)
2647{
2648 struct sde_rect dst_rect, intersect;
2649 int i, rc = -EINVAL;
2650 const struct drm_plane_state *pstate;
2651
2652 /* start checking from next plane */
2653 for (i = curr_cnt; i < cnt; i++) {
2654 pstate = pstates[i].drm_pstate;
2655 POPULATE_RECT(&dst_rect, pstate->crtc_x, pstate->crtc_y,
Veera Sundaram Sankaran9d9ff912017-06-20 10:41:21 -07002656 pstate->crtc_w, pstate->crtc_h, false);
Dhaval Patela8d6bc62017-05-10 17:40:18 -07002657 sde_kms_rect_intersect(&dst_rect, excl_rect, &intersect);
2658
2659 if (intersect.w == excl_rect->w && intersect.h == excl_rect->h
2660 /* next plane may be on same z-order */
2661 && z_pos != pstates[i].stage) {
2662 rc = 0;
2663 goto end;
2664 }
2665 }
2666
2667 SDE_ERROR("excl rect does not find top overlapping rect\n");
2668end:
2669 return rc;
2670}
2671
2672/* no input validation - caller API has all the checks */
2673static int _sde_crtc_excl_dim_layer_check(struct drm_crtc_state *state,
2674 struct plane_state pstates[], int cnt)
2675{
2676 struct sde_crtc_state *cstate = to_sde_crtc_state(state);
2677 struct drm_display_mode *mode = &state->adjusted_mode;
2678 const struct drm_plane_state *pstate;
2679 struct sde_plane_state *sde_pstate;
2680 int rc = 0, i;
2681
2682 /* Check dim layer rect bounds and stage */
2683 for (i = 0; i < cstate->num_dim_layers; i++) {
2684 if ((CHECK_LAYER_BOUNDS(cstate->dim_layer[i].rect.y,
2685 cstate->dim_layer[i].rect.h, mode->vdisplay)) ||
2686 (CHECK_LAYER_BOUNDS(cstate->dim_layer[i].rect.x,
2687 cstate->dim_layer[i].rect.w, mode->hdisplay)) ||
2688 (cstate->dim_layer[i].stage >= SDE_STAGE_MAX) ||
2689 (!cstate->dim_layer[i].rect.w) ||
2690 (!cstate->dim_layer[i].rect.h)) {
2691 SDE_ERROR("invalid dim_layer:{%d,%d,%d,%d}, stage:%d\n",
2692 cstate->dim_layer[i].rect.x,
2693 cstate->dim_layer[i].rect.y,
2694 cstate->dim_layer[i].rect.w,
2695 cstate->dim_layer[i].rect.h,
2696 cstate->dim_layer[i].stage);
2697 SDE_ERROR("display: %dx%d\n", mode->hdisplay,
2698 mode->vdisplay);
2699 rc = -E2BIG;
2700 goto end;
2701 }
2702 }
2703
2704 /* this is traversing on sorted z-order pstates */
2705 for (i = 0; i < cnt; i++) {
2706 pstate = pstates[i].drm_pstate;
2707 sde_pstate = to_sde_plane_state(pstate);
2708 if (sde_pstate->excl_rect.w && sde_pstate->excl_rect.h) {
2709 /* check overlap on all top z-order */
2710 rc = _sde_crtc_excl_rect_overlap_check(pstates, cnt,
2711 i + 1, &sde_pstate->excl_rect, pstates[i].stage);
2712 if (rc)
2713 goto end;
2714 }
2715 }
2716
2717end:
2718 return rc;
2719}
2720
Abhijit Kulkarni7444a7d2017-06-21 18:53:36 -07002721static int _sde_crtc_find_plane_fb_modes(struct drm_crtc_state *state,
2722 uint32_t *fb_ns,
2723 uint32_t *fb_sec,
2724 uint32_t *fb_ns_dir,
2725 uint32_t *fb_sec_dir)
2726{
2727 struct drm_plane *plane;
2728 const struct drm_plane_state *pstate;
2729 struct sde_plane_state *sde_pstate;
2730 uint32_t mode = 0;
2731 int rc;
2732
2733 if (!state) {
2734 SDE_ERROR("invalid state\n");
2735 return -EINVAL;
2736 }
2737
2738 *fb_ns = 0;
2739 *fb_sec = 0;
2740 *fb_ns_dir = 0;
2741 *fb_sec_dir = 0;
2742 drm_atomic_crtc_state_for_each_plane_state(plane, pstate, state) {
2743 if (IS_ERR_OR_NULL(pstate)) {
2744 rc = PTR_ERR(pstate);
2745 SDE_ERROR("crtc%d failed to get plane%d state%d\n",
2746 state->crtc->base.id,
2747 plane->base.id, rc);
2748 return rc;
2749 }
2750 sde_pstate = to_sde_plane_state(pstate);
2751 mode = sde_plane_get_property(sde_pstate,
2752 PLANE_PROP_FB_TRANSLATION_MODE);
2753 switch (mode) {
2754 case SDE_DRM_FB_NON_SEC:
2755 (*fb_ns)++;
2756 break;
2757 case SDE_DRM_FB_SEC:
2758 (*fb_sec)++;
2759 break;
2760 case SDE_DRM_FB_NON_SEC_DIR_TRANS:
2761 (*fb_ns_dir)++;
2762 break;
2763 case SDE_DRM_FB_SEC_DIR_TRANS:
2764 (*fb_sec_dir)++;
2765 break;
2766 default:
2767 SDE_ERROR("Error: Plane[%d], fb_trans_mode:%d",
2768 plane->base.id,
2769 mode);
2770 return -EINVAL;
2771 }
2772 }
2773 return 0;
2774}
2775
2776static int _sde_crtc_check_secure_state(struct drm_crtc *crtc,
2777 struct drm_crtc_state *state)
2778{
2779 struct drm_encoder *encoder;
2780 struct sde_crtc_state *cstate;
2781 uint32_t secure;
2782 uint32_t fb_ns = 0, fb_sec = 0, fb_ns_dir = 0, fb_sec_dir = 0;
2783 int encoder_cnt = 0;
2784 int rc;
2785
2786 if (!crtc || !state) {
2787 SDE_ERROR("invalid arguments\n");
2788 return -EINVAL;
2789 }
2790
2791 cstate = to_sde_crtc_state(state);
2792
2793 secure = sde_crtc_get_property(cstate,
2794 CRTC_PROP_SECURITY_LEVEL);
2795
2796 rc = _sde_crtc_find_plane_fb_modes(state,
2797 &fb_ns,
2798 &fb_sec,
2799 &fb_ns_dir,
2800 &fb_sec_dir);
2801 if (rc)
2802 return rc;
2803
2804 /**
2805 * validate planes
2806 * fb_ns_dir is for secure display use case,
2807 * fb_sec_dir is for secure camera preview use case,
2808 * fb_sec is for secure video playback,
2809 * fb_ns is for normal non secure use cases.
2810 */
2811 if (((secure == SDE_DRM_SEC_ONLY) &&
2812 (fb_ns || fb_sec || fb_sec_dir)) ||
2813 (fb_sec || fb_sec_dir)) {
2814 SDE_ERROR(
2815 "crtc%d: invalid planes fb_modes Sec:%d, NS:%d, Sec_Dir:%d, NS_Dir%d\n",
2816 crtc->base.id,
2817 fb_sec, fb_ns, fb_sec_dir,
2818 fb_ns_dir);
2819 return -EINVAL;
2820 }
2821
2822 /**
2823 * secure_crtc is not allowed in a shared toppolgy
2824 * across different encoders.
2825 */
2826 if (fb_ns_dir || fb_sec_dir) {
2827 drm_for_each_encoder(encoder, crtc->dev)
2828 if (encoder->crtc == crtc)
2829 encoder_cnt++;
2830
2831 if (encoder_cnt >
2832 MAX_ALLOWED_ENCODER_CNT_PER_SECURE_CRTC) {
2833 SDE_ERROR(
2834 "crtc%d, invalid virtual encoder crtc%d\n",
2835 crtc->base.id,
2836 encoder_cnt);
2837 return -EINVAL;
2838
2839 }
2840 }
2841 SDE_DEBUG("crtc:%d Secure validation successful\n", crtc->base.id);
2842 return 0;
2843}
2844
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002845static int sde_crtc_atomic_check(struct drm_crtc *crtc,
2846 struct drm_crtc_state *state)
2847{
Clarence Ipcae1bb62016-07-07 12:07:13 -04002848 struct sde_crtc *sde_crtc;
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08002849 struct plane_state pstates[SDE_STAGE_MAX * 4];
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08002850 struct sde_crtc_state *cstate;
Dhaval Patelec10fad2016-08-22 14:40:48 -07002851
Dhaval Patel04c7e8e2016-09-26 20:14:31 -07002852 const struct drm_plane_state *pstate;
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002853 struct drm_plane *plane;
Dhaval Patelec10fad2016-08-22 14:40:48 -07002854 struct drm_display_mode *mode;
2855
2856 int cnt = 0, rc = 0, mixer_width, i, z_pos;
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08002857
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08002858 struct sde_multirect_plane_states multirect_plane[SDE_STAGE_MAX * 2];
2859 int multirect_count = 0;
2860 const struct drm_plane_state *pipe_staged[SSPP_MAX];
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04002861 int left_zpos_cnt = 0, right_zpos_cnt = 0;
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002862
Clarence Ipcae1bb62016-07-07 12:07:13 -04002863 if (!crtc) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07002864 SDE_ERROR("invalid crtc\n");
Clarence Ipcae1bb62016-07-07 12:07:13 -04002865 return -EINVAL;
2866 }
2867
Alan Kwongcdb2f282017-03-18 13:42:06 -07002868 sde_crtc = to_sde_crtc(crtc);
2869 cstate = to_sde_crtc_state(state);
2870
Lloyd Atkinson5217336c2016-09-15 18:21:18 -04002871 if (!state->enable || !state->active) {
2872 SDE_DEBUG("crtc%d -> enable %d, active %d, skip atomic_check\n",
2873 crtc->base.id, state->enable, state->active);
Alan Kwongcdb2f282017-03-18 13:42:06 -07002874 goto end;
Lloyd Atkinson5217336c2016-09-15 18:21:18 -04002875 }
2876
Dhaval Patelec10fad2016-08-22 14:40:48 -07002877 mode = &state->adjusted_mode;
2878 SDE_DEBUG("%s: check", sde_crtc->name);
Clarence Ipcae1bb62016-07-07 12:07:13 -04002879
Clarence Ip90b282d2017-05-04 10:00:32 -07002880 /* force a full mode set if active state changed */
2881 if (state->active_changed)
2882 state->mode_changed = true;
2883
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08002884 memset(pipe_staged, 0, sizeof(pipe_staged));
2885
Dhaval Patelec10fad2016-08-22 14:40:48 -07002886 mixer_width = sde_crtc_mixer_width(sde_crtc, mode);
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002887
Lloyd Atkinson66e7dde2017-02-08 15:52:53 -05002888 _sde_crtc_setup_is_ppsplit(state);
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05002889 _sde_crtc_setup_lm_bounds(crtc, state);
2890
Abhijit Kulkarni7444a7d2017-06-21 18:53:36 -07002891 rc = _sde_crtc_check_secure_state(crtc, state);
2892 if (rc)
2893 return rc;
2894
Dhaval Patelec10fad2016-08-22 14:40:48 -07002895 /* get plane state for all drm planes associated with crtc state */
Dhaval Patel04c7e8e2016-09-26 20:14:31 -07002896 drm_atomic_crtc_state_for_each_plane_state(plane, pstate, state) {
Clarence Ipc47a0692016-10-11 10:54:17 -04002897 if (IS_ERR_OR_NULL(pstate)) {
2898 rc = PTR_ERR(pstate);
2899 SDE_ERROR("%s: failed to get plane%d state, %d\n",
2900 sde_crtc->name, plane->base.id, rc);
Alan Kwong85767282016-10-03 18:03:37 -04002901 goto end;
2902 }
Clarence Ipc47a0692016-10-11 10:54:17 -04002903 if (cnt >= ARRAY_SIZE(pstates))
2904 continue;
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002905
Dhaval Patelec10fad2016-08-22 14:40:48 -07002906 pstates[cnt].sde_pstate = to_sde_plane_state(pstate);
2907 pstates[cnt].drm_pstate = pstate;
Clarence Ipc47a0692016-10-11 10:54:17 -04002908 pstates[cnt].stage = sde_plane_get_property(
2909 pstates[cnt].sde_pstate, PLANE_PROP_ZPOS);
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08002910 pstates[cnt].pipe_id = sde_plane_pipe(plane);
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08002911
2912 /* check dim layer stage with every plane */
2913 for (i = 0; i < cstate->num_dim_layers; i++) {
Veera Sundaram Sankaranb9ed6bd2017-07-11 19:18:03 -07002914 if (cstate->dim_layer[i].stage
2915 == (pstates[cnt].stage + SDE_STAGE_0)) {
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -07002916 SDE_ERROR(
2917 "plane:%d/dim_layer:%i-same stage:%d\n",
2918 plane->base.id, i,
2919 cstate->dim_layer[i].stage);
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08002920 rc = -EINVAL;
2921 goto end;
2922 }
2923 }
2924
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08002925 if (pipe_staged[pstates[cnt].pipe_id]) {
2926 multirect_plane[multirect_count].r0 =
2927 pipe_staged[pstates[cnt].pipe_id];
2928 multirect_plane[multirect_count].r1 = pstate;
2929 multirect_count++;
2930
2931 pipe_staged[pstates[cnt].pipe_id] = NULL;
2932 } else {
2933 pipe_staged[pstates[cnt].pipe_id] = pstate;
2934 }
2935
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002936 cnt++;
Dhaval Patelec10fad2016-08-22 14:40:48 -07002937
2938 if (CHECK_LAYER_BOUNDS(pstate->crtc_y, pstate->crtc_h,
2939 mode->vdisplay) ||
2940 CHECK_LAYER_BOUNDS(pstate->crtc_x, pstate->crtc_w,
2941 mode->hdisplay)) {
2942 SDE_ERROR("invalid vertical/horizontal destination\n");
2943 SDE_ERROR("y:%d h:%d vdisp:%d x:%d w:%d hdisp:%d\n",
2944 pstate->crtc_y, pstate->crtc_h, mode->vdisplay,
2945 pstate->crtc_x, pstate->crtc_w, mode->hdisplay);
2946 rc = -E2BIG;
2947 goto end;
2948 }
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002949 }
2950
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08002951 for (i = 1; i < SSPP_MAX; i++) {
Jeykumar Sankarane964dc72017-05-10 19:26:43 -07002952 if (pipe_staged[i]) {
2953 sde_plane_clear_multirect(pipe_staged[i]);
2954
2955 if (is_sde_plane_virtual(pipe_staged[i]->plane)) {
Veera Sundaram Sankaran372596d2017-06-21 17:57:25 -07002956 SDE_ERROR(
2957 "r1 only virt plane:%d not supported\n",
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08002958 pipe_staged[i]->plane->base.id);
Veera Sundaram Sankaran372596d2017-06-21 17:57:25 -07002959 rc = -EINVAL;
Jeykumar Sankarane964dc72017-05-10 19:26:43 -07002960 goto end;
2961 }
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08002962 }
2963 }
2964
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04002965 /* assign mixer stages based on sorted zpos property */
2966 sort(pstates, cnt, sizeof(pstates[0]), pstate_cmp, NULL);
2967
Dhaval Patela8d6bc62017-05-10 17:40:18 -07002968 rc = _sde_crtc_excl_dim_layer_check(state, pstates, cnt);
2969 if (rc)
2970 goto end;
2971
Clarence Ipc47a0692016-10-11 10:54:17 -04002972 if (!sde_is_custom_client()) {
2973 int stage_old = pstates[0].stage;
Dhaval Patelec10fad2016-08-22 14:40:48 -07002974
Clarence Ipc47a0692016-10-11 10:54:17 -04002975 z_pos = 0;
2976 for (i = 0; i < cnt; i++) {
2977 if (stage_old != pstates[i].stage)
2978 ++z_pos;
2979 stage_old = pstates[i].stage;
2980 pstates[i].stage = z_pos;
2981 }
2982 }
2983
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04002984 z_pos = -1;
Clarence Ipc47a0692016-10-11 10:54:17 -04002985 for (i = 0; i < cnt; i++) {
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04002986 /* reset counts at every new blend stage */
2987 if (pstates[i].stage != z_pos) {
2988 left_zpos_cnt = 0;
2989 right_zpos_cnt = 0;
2990 z_pos = pstates[i].stage;
2991 }
Clarence Ipc47a0692016-10-11 10:54:17 -04002992
2993 /* verify z_pos setting before using it */
Clarence Ip649989a2016-10-21 14:28:34 -04002994 if (z_pos >= SDE_STAGE_MAX - SDE_STAGE_0) {
Clarence Ipc47a0692016-10-11 10:54:17 -04002995 SDE_ERROR("> %d plane stages assigned\n",
2996 SDE_STAGE_MAX - SDE_STAGE_0);
2997 rc = -EINVAL;
2998 goto end;
2999 } else if (pstates[i].drm_pstate->crtc_x < mixer_width) {
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04003000 if (left_zpos_cnt == 2) {
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08003001 SDE_ERROR("> 2 planes @ stage %d on left\n",
Dhaval Patelec10fad2016-08-22 14:40:48 -07003002 z_pos);
3003 rc = -EINVAL;
3004 goto end;
3005 }
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04003006 left_zpos_cnt++;
3007
Dhaval Patelec10fad2016-08-22 14:40:48 -07003008 } else {
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04003009 if (right_zpos_cnt == 2) {
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08003010 SDE_ERROR("> 2 planes @ stage %d on right\n",
Dhaval Patelec10fad2016-08-22 14:40:48 -07003011 z_pos);
3012 rc = -EINVAL;
3013 goto end;
3014 }
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04003015 right_zpos_cnt++;
Dhaval Patelec10fad2016-08-22 14:40:48 -07003016 }
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04003017
Clarence Ipc47a0692016-10-11 10:54:17 -04003018 pstates[i].sde_pstate->stage = z_pos + SDE_STAGE_0;
Dhaval Patelec10fad2016-08-22 14:40:48 -07003019 SDE_DEBUG("%s: zpos %d", sde_crtc->name, z_pos);
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04003020 }
3021
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08003022 for (i = 0; i < multirect_count; i++) {
3023 if (sde_plane_validate_multirect_v2(&multirect_plane[i])) {
3024 SDE_ERROR(
3025 "multirect validation failed for planes (%d - %d)\n",
3026 multirect_plane[i].r0->plane->base.id,
3027 multirect_plane[i].r1->plane->base.id);
3028 rc = -EINVAL;
Alan Kwong9aa061c2016-11-06 21:17:12 -05003029 goto end;
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08003030 }
3031 }
3032
Alan Kwong9aa061c2016-11-06 21:17:12 -05003033 rc = sde_core_perf_crtc_check(crtc, state);
3034 if (rc) {
3035 SDE_ERROR("crtc%d failed performance check %d\n",
3036 crtc->base.id, rc);
3037 goto end;
3038 }
3039
Jeykumar Sankaranaaaa0712017-06-12 17:59:16 -07003040 /* validate source split:
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04003041 * use pstates sorted by stage to check planes on same stage
3042 * we assume that all pipes are in source split so its valid to compare
3043 * without taking into account left/right mixer placement
3044 */
3045 for (i = 1; i < cnt; i++) {
3046 struct plane_state *prv_pstate, *cur_pstate;
Jeykumar Sankaranaaaa0712017-06-12 17:59:16 -07003047 struct sde_rect left_rect, right_rect;
3048 int32_t left_pid, right_pid;
3049 int32_t stage;
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04003050
3051 prv_pstate = &pstates[i - 1];
3052 cur_pstate = &pstates[i];
3053 if (prv_pstate->stage != cur_pstate->stage)
3054 continue;
3055
Jeykumar Sankaranaaaa0712017-06-12 17:59:16 -07003056 stage = cur_pstate->stage;
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04003057
Jeykumar Sankaranaaaa0712017-06-12 17:59:16 -07003058 left_pid = prv_pstate->sde_pstate->base.plane->base.id;
3059 POPULATE_RECT(&left_rect, prv_pstate->drm_pstate->crtc_x,
3060 prv_pstate->drm_pstate->crtc_y,
3061 prv_pstate->drm_pstate->crtc_w,
3062 prv_pstate->drm_pstate->crtc_h, false);
3063
3064 right_pid = cur_pstate->sde_pstate->base.plane->base.id;
3065 POPULATE_RECT(&right_rect, cur_pstate->drm_pstate->crtc_x,
3066 cur_pstate->drm_pstate->crtc_y,
3067 cur_pstate->drm_pstate->crtc_w,
3068 cur_pstate->drm_pstate->crtc_h, false);
3069
3070 if (right_rect.x < left_rect.x) {
3071 swap(left_pid, right_pid);
3072 swap(left_rect, right_rect);
3073 }
3074
3075 /**
3076 * - planes are enumerated in pipe-priority order such that
3077 * planes with lower drm_id must be left-most in a shared
3078 * blend-stage when using source split.
3079 * - planes in source split must be contiguous in width
3080 * - planes in source split must have same dest yoff and height
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04003081 */
Jeykumar Sankaranaaaa0712017-06-12 17:59:16 -07003082 if (right_pid < left_pid) {
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04003083 SDE_ERROR(
Jeykumar Sankaranaaaa0712017-06-12 17:59:16 -07003084 "invalid src split cfg. priority mismatch. stage: %d left: %d right: %d\n",
3085 stage, left_pid, right_pid);
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04003086 rc = -EINVAL;
3087 goto end;
Jeykumar Sankaranaaaa0712017-06-12 17:59:16 -07003088 } else if (right_rect.x != (left_rect.x + left_rect.w)) {
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04003089 SDE_ERROR(
Jeykumar Sankaranaaaa0712017-06-12 17:59:16 -07003090 "non-contiguous coordinates for src split. stage: %d left: %d - %d right: %d - %d\n",
3091 stage, left_rect.x, left_rect.w,
3092 right_rect.x, right_rect.w);
3093 rc = -EINVAL;
3094 goto end;
3095 } else if ((left_rect.y != right_rect.y) ||
3096 (left_rect.h != right_rect.h)) {
3097 SDE_ERROR(
3098 "source split at stage: %d. invalid yoff/height: l_y: %d r_y: %d l_h: %d r_h: %d\n",
3099 stage, left_rect.y, right_rect.y,
3100 left_rect.h, right_rect.h);
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04003101 rc = -EINVAL;
3102 goto end;
3103 }
3104 }
3105
Lloyd Atkinson8ba47032017-03-22 17:13:32 -04003106 rc = _sde_crtc_check_rois(crtc, state);
3107 if (rc) {
3108 SDE_ERROR("crtc%d failed roi check %d\n", crtc->base.id, rc);
3109 goto end;
3110 }
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04003111
Dhaval Patelec10fad2016-08-22 14:40:48 -07003112end:
Alan Kwongcdb2f282017-03-18 13:42:06 -07003113 _sde_crtc_rp_free_unused(&cstate->rp);
Dhaval Patelec10fad2016-08-22 14:40:48 -07003114 return rc;
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04003115}
3116
Abhijit Kulkarni7acb3262016-07-05 15:27:25 -04003117int sde_crtc_vblank(struct drm_crtc *crtc, bool en)
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07003118{
Clarence Ip7f70ce42017-03-20 06:53:46 -07003119 struct sde_crtc *sde_crtc;
Lloyd Atkinsondcb1c4a2017-07-27 10:52:09 -04003120 int ret;
Abhijit Kulkarni7acb3262016-07-05 15:27:25 -04003121
Clarence Ip7f70ce42017-03-20 06:53:46 -07003122 if (!crtc) {
3123 SDE_ERROR("invalid crtc\n");
3124 return -EINVAL;
3125 }
3126 sde_crtc = to_sde_crtc(crtc);
3127
3128 mutex_lock(&sde_crtc->crtc_lock);
Lloyd Atkinsondcb1c4a2017-07-27 10:52:09 -04003129 SDE_EVT32(DRMID(&sde_crtc->base), en, sde_crtc->enabled,
3130 sde_crtc->suspend, sde_crtc->vblank_requested);
3131 if (sde_crtc->enabled && !sde_crtc->suspend) {
3132 ret = _sde_crtc_vblank_enable_no_lock(sde_crtc, en);
3133 if (ret)
3134 SDE_ERROR("%s vblank enable failed: %d\n",
3135 sde_crtc->name, ret);
3136 }
3137 sde_crtc->vblank_requested = en;
Clarence Ip7f70ce42017-03-20 06:53:46 -07003138 mutex_unlock(&sde_crtc->crtc_lock);
Clarence Ip9728a1d2017-04-18 22:22:13 -04003139
Lloyd Atkinsondcb1c4a2017-07-27 10:52:09 -04003140 return 0;
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07003141}
3142
Lloyd Atkinson5217336c2016-09-15 18:21:18 -04003143void sde_crtc_cancel_pending_flip(struct drm_crtc *crtc, struct drm_file *file)
3144{
3145 struct sde_crtc *sde_crtc = to_sde_crtc(crtc);
3146
Alan Kwong163d2612016-11-03 00:56:56 -04003147 SDE_DEBUG("%s: cancel: %p\n", sde_crtc->name, file);
Lloyd Atkinson4f1c8692016-09-14 14:04:25 -04003148 _sde_crtc_complete_flip(crtc, file);
Lloyd Atkinson5217336c2016-09-15 18:21:18 -04003149}
3150
Clarence Ip7a753bb2016-07-07 11:47:44 -04003151/**
3152 * sde_crtc_install_properties - install all drm properties for crtc
3153 * @crtc: Pointer to drm crtc structure
3154 */
Dhaval Patele4a5dda2016-10-13 19:29:30 -07003155static void sde_crtc_install_properties(struct drm_crtc *crtc,
3156 struct sde_mdss_cfg *catalog)
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07003157{
Clarence Ip7a753bb2016-07-07 11:47:44 -04003158 struct sde_crtc *sde_crtc;
3159 struct drm_device *dev;
Dhaval Patele4a5dda2016-10-13 19:29:30 -07003160 struct sde_kms_info *info;
Alan Kwong9aa061c2016-11-06 21:17:12 -05003161 struct sde_kms *sde_kms;
Abhijit Kulkarni50d69442017-04-11 19:50:47 -07003162 static const struct drm_prop_enum_list e_secure_level[] = {
3163 {SDE_DRM_SEC_NON_SEC, "sec_and_non_sec"},
3164 {SDE_DRM_SEC_ONLY, "sec_only"},
3165 };
Clarence Ip7a753bb2016-07-07 11:47:44 -04003166
Lloyd Atkinson4f1c8692016-09-14 14:04:25 -04003167 SDE_DEBUG("\n");
Clarence Ip7a753bb2016-07-07 11:47:44 -04003168
Dhaval Patele4a5dda2016-10-13 19:29:30 -07003169 if (!crtc || !catalog) {
3170 SDE_ERROR("invalid crtc or catalog\n");
Clarence Ip7a753bb2016-07-07 11:47:44 -04003171 return;
3172 }
3173
3174 sde_crtc = to_sde_crtc(crtc);
3175 dev = crtc->dev;
Alan Kwong9aa061c2016-11-06 21:17:12 -05003176 sde_kms = _sde_crtc_get_kms(crtc);
Clarence Ip7a753bb2016-07-07 11:47:44 -04003177
Narendra Muppallaec11a0a2017-06-15 15:35:17 -07003178 if (!sde_kms) {
3179 SDE_ERROR("invalid argument\n");
3180 return;
3181 }
3182
Dhaval Patele4a5dda2016-10-13 19:29:30 -07003183 info = kzalloc(sizeof(struct sde_kms_info), GFP_KERNEL);
3184 if (!info) {
3185 SDE_ERROR("failed to allocate info memory\n");
3186 return;
3187 }
3188
Clarence Ip7a753bb2016-07-07 11:47:44 -04003189 /* range properties */
3190 msm_property_install_range(&sde_crtc->property_info,
Dhaval Patel4e574842016-08-23 15:11:37 -07003191 "input_fence_timeout", 0x0, 0, SDE_CRTC_MAX_INPUT_FENCE_TIMEOUT,
3192 SDE_CRTC_INPUT_FENCE_TIMEOUT, CRTC_PROP_INPUT_FENCE_TIMEOUT);
3193
3194 msm_property_install_range(&sde_crtc->property_info, "output_fence",
3195 0x0, 0, INR_OPEN_MAX, 0x0, CRTC_PROP_OUTPUT_FENCE);
Clarence Ip1d9728b2016-09-01 11:10:54 -04003196
3197 msm_property_install_range(&sde_crtc->property_info,
3198 "output_fence_offset", 0x0, 0, 1, 0,
3199 CRTC_PROP_OUTPUT_FENCE_OFFSET);
Dhaval Patele4a5dda2016-10-13 19:29:30 -07003200
Alan Kwong9aa061c2016-11-06 21:17:12 -05003201 msm_property_install_range(&sde_crtc->property_info,
3202 "core_clk", 0x0, 0, U64_MAX,
3203 sde_kms->perf.max_core_clk_rate,
3204 CRTC_PROP_CORE_CLK);
3205 msm_property_install_range(&sde_crtc->property_info,
3206 "core_ab", 0x0, 0, U64_MAX,
Alan Kwongff30f4a2017-05-23 12:02:00 -07003207 catalog->perf.max_bw_high * 1000ULL,
Alan Kwong9aa061c2016-11-06 21:17:12 -05003208 CRTC_PROP_CORE_AB);
3209 msm_property_install_range(&sde_crtc->property_info,
3210 "core_ib", 0x0, 0, U64_MAX,
Alan Kwongff30f4a2017-05-23 12:02:00 -07003211 catalog->perf.max_bw_high * 1000ULL,
Alan Kwong9aa061c2016-11-06 21:17:12 -05003212 CRTC_PROP_CORE_IB);
Alan Kwong4aacd532017-02-04 18:51:33 -08003213 msm_property_install_range(&sde_crtc->property_info,
Alan Kwong0230a102017-05-16 11:36:44 -07003214 "llcc_ab", 0x0, 0, U64_MAX,
Alan Kwongff30f4a2017-05-23 12:02:00 -07003215 catalog->perf.max_bw_high * 1000ULL,
Alan Kwong0230a102017-05-16 11:36:44 -07003216 CRTC_PROP_LLCC_AB);
Alan Kwong8c176bf2017-02-09 19:34:32 -08003217 msm_property_install_range(&sde_crtc->property_info,
Alan Kwong0230a102017-05-16 11:36:44 -07003218 "llcc_ib", 0x0, 0, U64_MAX,
Alan Kwongff30f4a2017-05-23 12:02:00 -07003219 catalog->perf.max_bw_high * 1000ULL,
Alan Kwong0230a102017-05-16 11:36:44 -07003220 CRTC_PROP_LLCC_IB);
3221 msm_property_install_range(&sde_crtc->property_info,
3222 "dram_ab", 0x0, 0, U64_MAX,
3223 catalog->perf.max_bw_high * 1000ULL,
3224 CRTC_PROP_DRAM_AB);
3225 msm_property_install_range(&sde_crtc->property_info,
3226 "dram_ib", 0x0, 0, U64_MAX,
3227 catalog->perf.max_bw_high * 1000ULL,
3228 CRTC_PROP_DRAM_IB);
Alan Kwong8c176bf2017-02-09 19:34:32 -08003229 msm_property_install_range(&sde_crtc->property_info,
Alan Kwong4aacd532017-02-04 18:51:33 -08003230 "rot_prefill_bw", 0, 0, U64_MAX,
3231 catalog->perf.max_bw_high * 1000ULL,
3232 CRTC_PROP_ROT_PREFILL_BW);
Alan Kwong8c176bf2017-02-09 19:34:32 -08003233 msm_property_install_range(&sde_crtc->property_info,
3234 "rot_clk", 0, 0, U64_MAX,
3235 sde_kms->perf.max_core_clk_rate,
3236 CRTC_PROP_ROT_CLK);
Alan Kwong9aa061c2016-11-06 21:17:12 -05003237
Dhaval Patele4a5dda2016-10-13 19:29:30 -07003238 msm_property_install_blob(&sde_crtc->property_info, "capabilities",
3239 DRM_MODE_PROP_IMMUTABLE, CRTC_PROP_INFO);
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08003240
Lloyd Atkinson8ba47032017-03-22 17:13:32 -04003241 msm_property_install_volatile_range(&sde_crtc->property_info,
3242 "sde_drm_roi_v1", 0x0, 0, ~0, 0, CRTC_PROP_ROI_V1);
3243
Abhijit Kulkarni50d69442017-04-11 19:50:47 -07003244 msm_property_install_enum(&sde_crtc->property_info, "security_level",
3245 0x0, 0, e_secure_level,
3246 ARRAY_SIZE(e_secure_level),
3247 CRTC_PROP_SECURITY_LEVEL);
3248
Dhaval Patele4a5dda2016-10-13 19:29:30 -07003249 sde_kms_info_reset(info);
3250
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -07003251 if (catalog->has_dim_layer) {
3252 msm_property_install_volatile_range(&sde_crtc->property_info,
3253 "dim_layer_v1", 0x0, 0, ~0, 0, CRTC_PROP_DIM_LAYER_V1);
3254 sde_kms_info_add_keyint(info, "dim_layer_v1_max_layers",
3255 SDE_MAX_DIM_LAYERS);
3256 }
3257
Dhaval Patele4a5dda2016-10-13 19:29:30 -07003258 sde_kms_info_add_keyint(info, "hw_version", catalog->hwversion);
3259 sde_kms_info_add_keyint(info, "max_linewidth",
3260 catalog->max_mixer_width);
3261 sde_kms_info_add_keyint(info, "max_blendstages",
3262 catalog->max_mixer_blendstages);
3263 if (catalog->qseed_type == SDE_SSPP_SCALER_QSEED2)
3264 sde_kms_info_add_keystr(info, "qseed_type", "qseed2");
3265 if (catalog->qseed_type == SDE_SSPP_SCALER_QSEED3)
3266 sde_kms_info_add_keystr(info, "qseed_type", "qseed3");
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08003267
3268 if (sde_is_custom_client()) {
3269 if (catalog->smart_dma_rev == SDE_SSPP_SMART_DMA_V1)
3270 sde_kms_info_add_keystr(info,
3271 "smart_dma_rev", "smart_dma_v1");
3272 if (catalog->smart_dma_rev == SDE_SSPP_SMART_DMA_V2)
3273 sde_kms_info_add_keystr(info,
3274 "smart_dma_rev", "smart_dma_v2");
3275 }
3276
Dhaval Patele4a5dda2016-10-13 19:29:30 -07003277 sde_kms_info_add_keyint(info, "has_src_split", catalog->has_src_split);
Alan Kwong2f84f8a2016-12-29 13:07:47 -05003278 if (catalog->perf.max_bw_low)
3279 sde_kms_info_add_keyint(info, "max_bandwidth_low",
Alan Kwong6259a382017-04-04 06:18:02 -07003280 catalog->perf.max_bw_low * 1000LL);
Alan Kwong2f84f8a2016-12-29 13:07:47 -05003281 if (catalog->perf.max_bw_high)
3282 sde_kms_info_add_keyint(info, "max_bandwidth_high",
Alan Kwong6259a382017-04-04 06:18:02 -07003283 catalog->perf.max_bw_high * 1000LL);
Alan Kwong2f84f8a2016-12-29 13:07:47 -05003284 if (sde_kms->perf.max_core_clk_rate)
3285 sde_kms_info_add_keyint(info, "max_mdp_clk",
3286 sde_kms->perf.max_core_clk_rate);
Alan Kwong6259a382017-04-04 06:18:02 -07003287 sde_kms_info_add_keystr(info, "core_ib_ff",
3288 catalog->perf.core_ib_ff);
3289 sde_kms_info_add_keystr(info, "core_clk_ff",
3290 catalog->perf.core_clk_ff);
3291 sde_kms_info_add_keystr(info, "comp_ratio_rt",
3292 catalog->perf.comp_ratio_rt);
3293 sde_kms_info_add_keystr(info, "comp_ratio_nrt",
3294 catalog->perf.comp_ratio_nrt);
3295 sde_kms_info_add_keyint(info, "dest_scale_prefill_lines",
3296 catalog->perf.dest_scale_prefill_lines);
3297 sde_kms_info_add_keyint(info, "undersized_prefill_lines",
3298 catalog->perf.undersized_prefill_lines);
3299 sde_kms_info_add_keyint(info, "macrotile_prefill_lines",
3300 catalog->perf.macrotile_prefill_lines);
3301 sde_kms_info_add_keyint(info, "yuv_nv12_prefill_lines",
3302 catalog->perf.yuv_nv12_prefill_lines);
3303 sde_kms_info_add_keyint(info, "linear_prefill_lines",
3304 catalog->perf.linear_prefill_lines);
3305 sde_kms_info_add_keyint(info, "downscaling_prefill_lines",
3306 catalog->perf.downscaling_prefill_lines);
3307 sde_kms_info_add_keyint(info, "xtra_prefill_lines",
3308 catalog->perf.xtra_prefill_lines);
3309 sde_kms_info_add_keyint(info, "amortizable_threshold",
3310 catalog->perf.amortizable_threshold);
3311 sde_kms_info_add_keyint(info, "min_prefill_lines",
3312 catalog->perf.min_prefill_lines);
3313
Dhaval Patele4a5dda2016-10-13 19:29:30 -07003314 msm_property_set_blob(&sde_crtc->property_info, &sde_crtc->blob_info,
Narendra Muppalla22d17252017-05-31 15:13:39 -07003315 info->data, SDE_KMS_INFO_DATALEN(info), CRTC_PROP_INFO);
Dhaval Patele4a5dda2016-10-13 19:29:30 -07003316
3317 kfree(info);
Clarence Ip7a753bb2016-07-07 11:47:44 -04003318}
3319
3320/**
3321 * sde_crtc_atomic_set_property - atomically set a crtc drm property
3322 * @crtc: Pointer to drm crtc structure
3323 * @state: Pointer to drm crtc state structure
3324 * @property: Pointer to targeted drm property
3325 * @val: Updated property value
3326 * @Returns: Zero on success
3327 */
3328static int sde_crtc_atomic_set_property(struct drm_crtc *crtc,
3329 struct drm_crtc_state *state,
3330 struct drm_property *property,
3331 uint64_t val)
3332{
3333 struct sde_crtc *sde_crtc;
3334 struct sde_crtc_state *cstate;
Clarence Ipcae1bb62016-07-07 12:07:13 -04003335 int idx, ret = -EINVAL;
Clarence Ip7a753bb2016-07-07 11:47:44 -04003336
3337 if (!crtc || !state || !property) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07003338 SDE_ERROR("invalid argument(s)\n");
Clarence Ip7a753bb2016-07-07 11:47:44 -04003339 } else {
3340 sde_crtc = to_sde_crtc(crtc);
3341 cstate = to_sde_crtc_state(state);
3342 ret = msm_property_atomic_set(&sde_crtc->property_info,
Clarence Ip4a2955d2017-07-04 18:04:33 -04003343 &cstate->property_state, property, val);
Clarence Ipcae1bb62016-07-07 12:07:13 -04003344 if (!ret) {
3345 idx = msm_property_index(&sde_crtc->property_info,
3346 property);
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08003347 switch (idx) {
3348 case CRTC_PROP_INPUT_FENCE_TIMEOUT:
Clarence Ipcae1bb62016-07-07 12:07:13 -04003349 _sde_crtc_set_input_fence_timeout(cstate);
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08003350 break;
3351 case CRTC_PROP_DIM_LAYER_V1:
3352 _sde_crtc_set_dim_layer_v1(cstate, (void *)val);
3353 break;
Lloyd Atkinson8ba47032017-03-22 17:13:32 -04003354 case CRTC_PROP_ROI_V1:
3355 ret = _sde_crtc_set_roi_v1(state, (void *)val);
3356 break;
Alan Kwong8411a9112017-06-06 19:29:01 -04003357 case CRTC_PROP_CORE_CLK:
Alan Kwongff30f4a2017-05-23 12:02:00 -07003358 case CRTC_PROP_CORE_AB:
3359 case CRTC_PROP_CORE_IB:
Alan Kwongff30f4a2017-05-23 12:02:00 -07003360 cstate->bw_control = true;
3361 break;
Alan Kwong0230a102017-05-16 11:36:44 -07003362 case CRTC_PROP_LLCC_AB:
3363 case CRTC_PROP_LLCC_IB:
3364 case CRTC_PROP_DRAM_AB:
3365 case CRTC_PROP_DRAM_IB:
3366 cstate->bw_control = true;
3367 cstate->bw_split_vote = true;
3368 break;
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08003369 default:
3370 /* nothing to do */
3371 break;
3372 }
Gopikrishnaiah Anandane0e5e0c2016-05-25 11:05:33 -07003373 } else {
3374 ret = sde_cp_crtc_set_property(crtc,
3375 property, val);
Clarence Ipcae1bb62016-07-07 12:07:13 -04003376 }
Gopikrishnaiah Anandane0e5e0c2016-05-25 11:05:33 -07003377 if (ret)
3378 DRM_ERROR("failed to set the property\n");
Alan Kwongcdb2f282017-03-18 13:42:06 -07003379
3380 SDE_DEBUG("crtc%d %s[%d] <= 0x%llx ret=%d\n", crtc->base.id,
3381 property->name, property->base.id, val, ret);
Clarence Ip7a753bb2016-07-07 11:47:44 -04003382 }
3383
3384 return ret;
3385}
3386
3387/**
3388 * sde_crtc_set_property - set a crtc drm property
3389 * @crtc: Pointer to drm crtc structure
3390 * @property: Pointer to targeted drm property
3391 * @val: Updated property value
3392 * @Returns: Zero on success
3393 */
3394static int sde_crtc_set_property(struct drm_crtc *crtc,
3395 struct drm_property *property, uint64_t val)
3396{
Lloyd Atkinson4f1c8692016-09-14 14:04:25 -04003397 SDE_DEBUG("\n");
Clarence Ip7a753bb2016-07-07 11:47:44 -04003398
3399 return sde_crtc_atomic_set_property(crtc, crtc->state, property, val);
3400}
3401
3402/**
3403 * sde_crtc_atomic_get_property - retrieve a crtc drm property
3404 * @crtc: Pointer to drm crtc structure
3405 * @state: Pointer to drm crtc state structure
3406 * @property: Pointer to targeted drm property
3407 * @val: Pointer to variable for receiving property value
3408 * @Returns: Zero on success
3409 */
3410static int sde_crtc_atomic_get_property(struct drm_crtc *crtc,
3411 const struct drm_crtc_state *state,
3412 struct drm_property *property,
3413 uint64_t *val)
3414{
3415 struct sde_crtc *sde_crtc;
3416 struct sde_crtc_state *cstate;
Veera Sundaram Sankaran675ff622017-06-21 21:44:46 -07003417 struct drm_encoder *encoder;
Clarence Ip24f80662016-06-13 19:05:32 -04003418 int i, ret = -EINVAL;
Dhaval Patel5cb59be2017-04-20 20:00:56 -07003419 bool conn_offset = 0;
Veera Sundaram Sankaran675ff622017-06-21 21:44:46 -07003420 bool is_cmd = true;
Clarence Ip7a753bb2016-07-07 11:47:44 -04003421
3422 if (!crtc || !state) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07003423 SDE_ERROR("invalid argument(s)\n");
Clarence Ip7a753bb2016-07-07 11:47:44 -04003424 } else {
3425 sde_crtc = to_sde_crtc(crtc);
3426 cstate = to_sde_crtc_state(state);
Dhaval Patel5cb59be2017-04-20 20:00:56 -07003427
3428 for (i = 0; i < cstate->num_connectors; ++i) {
3429 conn_offset = sde_connector_needs_offset(
3430 cstate->connectors[i]);
3431 if (conn_offset)
3432 break;
3433 }
3434
Veera Sundaram Sankaran675ff622017-06-21 21:44:46 -07003435 /**
3436 * set the cmd flag only when all the encoders attached
3437 * to the crtc are in cmd mode. Consider all other cases
3438 * as video mode.
3439 */
3440 drm_for_each_encoder(encoder, crtc->dev) {
3441 if (encoder->crtc == crtc)
3442 is_cmd &= sde_encoder_is_cmd_mode(encoder);
3443 }
3444
Clarence Ip24f80662016-06-13 19:05:32 -04003445 i = msm_property_index(&sde_crtc->property_info, property);
3446 if (i == CRTC_PROP_OUTPUT_FENCE) {
Dhaval Patel39323d42017-03-01 23:48:24 -08003447 uint32_t offset = sde_crtc_get_property(cstate,
Clarence Ip1d9728b2016-09-01 11:10:54 -04003448 CRTC_PROP_OUTPUT_FENCE_OFFSET);
3449
Veera Sundaram Sankaran675ff622017-06-21 21:44:46 -07003450 /**
3451 * set the offset to 0 only for cmd mode panels, so
3452 * the release fence for the current frame can be
3453 * triggered right after PP_DONE interrupt.
3454 */
3455 offset = is_cmd ? 0 : (offset + conn_offset);
3456
Dhaval Patel5cb59be2017-04-20 20:00:56 -07003457 ret = sde_fence_create(&sde_crtc->output_fence, val,
Veera Sundaram Sankaran675ff622017-06-21 21:44:46 -07003458 offset);
Clarence Ip1d9728b2016-09-01 11:10:54 -04003459 if (ret)
3460 SDE_ERROR("fence create failed\n");
Clarence Ip24f80662016-06-13 19:05:32 -04003461 } else {
3462 ret = msm_property_atomic_get(&sde_crtc->property_info,
Clarence Ip4a2955d2017-07-04 18:04:33 -04003463 &cstate->property_state,
3464 property, val);
Gopikrishnaiah Anandane0e5e0c2016-05-25 11:05:33 -07003465 if (ret)
3466 ret = sde_cp_crtc_get_property(crtc,
3467 property, val);
Clarence Ip24f80662016-06-13 19:05:32 -04003468 }
Gopikrishnaiah Anandane0e5e0c2016-05-25 11:05:33 -07003469 if (ret)
3470 DRM_ERROR("get property failed\n");
Clarence Ip7a753bb2016-07-07 11:47:44 -04003471 }
Clarence Ip7a753bb2016-07-07 11:47:44 -04003472 return ret;
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07003473}
3474
Alan Kwong67a3f792016-11-01 23:16:53 -04003475#ifdef CONFIG_DEBUG_FS
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07003476static int _sde_debugfs_status_show(struct seq_file *s, void *data)
Clarence Ip8f7366c2016-07-05 12:15:26 -04003477{
3478 struct sde_crtc *sde_crtc;
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07003479 struct sde_plane_state *pstate = NULL;
Clarence Ip8f7366c2016-07-05 12:15:26 -04003480 struct sde_crtc_mixer *m;
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07003481
3482 struct drm_crtc *crtc;
3483 struct drm_plane *plane;
3484 struct drm_display_mode *mode;
3485 struct drm_framebuffer *fb;
3486 struct drm_plane_state *state;
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -07003487 struct sde_crtc_state *cstate;
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07003488
3489 int i, out_width;
Clarence Ip8f7366c2016-07-05 12:15:26 -04003490
3491 if (!s || !s->private)
3492 return -EINVAL;
3493
3494 sde_crtc = s->private;
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07003495 crtc = &sde_crtc->base;
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -07003496 cstate = to_sde_crtc_state(crtc->state);
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07003497
3498 mutex_lock(&sde_crtc->crtc_lock);
3499 mode = &crtc->state->adjusted_mode;
3500 out_width = sde_crtc_mixer_width(sde_crtc, mode);
3501
3502 seq_printf(s, "crtc:%d width:%d height:%d\n", crtc->base.id,
3503 mode->hdisplay, mode->vdisplay);
3504
3505 seq_puts(s, "\n");
3506
Clarence Ip8f7366c2016-07-05 12:15:26 -04003507 for (i = 0; i < sde_crtc->num_mixers; ++i) {
Lloyd Atkinsone7bcdd22016-08-11 10:53:37 -04003508 m = &sde_crtc->mixers[i];
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07003509 if (!m->hw_lm)
3510 seq_printf(s, "\tmixer[%d] has no lm\n", i);
3511 else if (!m->hw_ctl)
3512 seq_printf(s, "\tmixer[%d] has no ctl\n", i);
3513 else
3514 seq_printf(s, "\tmixer:%d ctl:%d width:%d height:%d\n",
3515 m->hw_lm->idx - LM_0, m->hw_ctl->idx - CTL_0,
3516 out_width, mode->vdisplay);
Clarence Ip8f7366c2016-07-05 12:15:26 -04003517 }
Dhaval Patel44f12472016-08-29 12:19:47 -07003518
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07003519 seq_puts(s, "\n");
Dhaval Patel48c76022016-09-01 17:51:23 -07003520
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -07003521 for (i = 0; i < cstate->num_dim_layers; i++) {
3522 struct sde_hw_dim_layer *dim_layer = &cstate->dim_layer[i];
3523
3524 seq_printf(s, "\tdim_layer:%d] stage:%d flags:%d\n",
3525 i, dim_layer->stage, dim_layer->flags);
3526 seq_printf(s, "\tdst_x:%d dst_y:%d dst_w:%d dst_h:%d\n",
3527 dim_layer->rect.x, dim_layer->rect.y,
3528 dim_layer->rect.w, dim_layer->rect.h);
3529 seq_printf(s,
3530 "\tcolor_0:%d color_1:%d color_2:%d color_3:%d\n",
3531 dim_layer->color_fill.color_0,
3532 dim_layer->color_fill.color_1,
3533 dim_layer->color_fill.color_2,
3534 dim_layer->color_fill.color_3);
3535 seq_puts(s, "\n");
3536 }
3537
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07003538 drm_atomic_crtc_for_each_plane(plane, crtc) {
3539 pstate = to_sde_plane_state(plane->state);
3540 state = plane->state;
3541
3542 if (!pstate || !state)
3543 continue;
3544
3545 seq_printf(s, "\tplane:%u stage:%d\n", plane->base.id,
3546 pstate->stage);
3547
3548 if (plane->state->fb) {
3549 fb = plane->state->fb;
3550
3551 seq_printf(s, "\tfb:%d image format:%4.4s wxh:%ux%u bpp:%d\n",
3552 fb->base.id, (char *) &fb->pixel_format,
3553 fb->width, fb->height, fb->bits_per_pixel);
3554
3555 seq_puts(s, "\t");
3556 for (i = 0; i < ARRAY_SIZE(fb->modifier); i++)
3557 seq_printf(s, "modifier[%d]:%8llu ", i,
3558 fb->modifier[i]);
3559 seq_puts(s, "\n");
3560
3561 seq_puts(s, "\t");
3562 for (i = 0; i < ARRAY_SIZE(fb->pitches); i++)
3563 seq_printf(s, "pitches[%d]:%8u ", i,
3564 fb->pitches[i]);
3565 seq_puts(s, "\n");
3566
3567 seq_puts(s, "\t");
3568 for (i = 0; i < ARRAY_SIZE(fb->offsets); i++)
3569 seq_printf(s, "offsets[%d]:%8u ", i,
3570 fb->offsets[i]);
Dhaval Patel48c76022016-09-01 17:51:23 -07003571 seq_puts(s, "\n");
3572 }
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07003573
3574 seq_printf(s, "\tsrc_x:%4d src_y:%4d src_w:%4d src_h:%4d\n",
3575 state->src_x, state->src_y, state->src_w, state->src_h);
3576
3577 seq_printf(s, "\tdst x:%4d dst_y:%4d dst_w:%4d dst_h:%4d\n",
3578 state->crtc_x, state->crtc_y, state->crtc_w,
3579 state->crtc_h);
Jeykumar Sankarane964dc72017-05-10 19:26:43 -07003580 seq_printf(s, "\tmultirect: mode: %d index: %d\n",
3581 pstate->multirect_mode, pstate->multirect_index);
Veera Sundaram Sankaran58e12812017-05-05 11:51:09 -07003582
3583 seq_printf(s, "\texcl_rect: x:%4d y:%4d w:%4d h:%4d\n",
3584 pstate->excl_rect.x, pstate->excl_rect.y,
3585 pstate->excl_rect.w, pstate->excl_rect.h);
3586
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07003587 seq_puts(s, "\n");
Clarence Ip8f7366c2016-07-05 12:15:26 -04003588 }
Alan Kwong07da0982016-11-04 12:57:45 -04003589
3590 if (sde_crtc->vblank_cb_count) {
3591 ktime_t diff = ktime_sub(ktime_get(), sde_crtc->vblank_cb_time);
3592 s64 diff_ms = ktime_to_ms(diff);
3593 s64 fps = diff_ms ? DIV_ROUND_CLOSEST(
3594 sde_crtc->vblank_cb_count * 1000, diff_ms) : 0;
3595
3596 seq_printf(s,
Dhaval Pateld67cf4a2017-06-14 18:08:32 -07003597 "vblank fps:%lld count:%u total:%llums total_framecount:%llu\n",
3598 fps, sde_crtc->vblank_cb_count,
3599 ktime_to_ms(diff), sde_crtc->play_count);
Alan Kwong07da0982016-11-04 12:57:45 -04003600
3601 /* reset time & count for next measurement */
3602 sde_crtc->vblank_cb_count = 0;
3603 sde_crtc->vblank_cb_time = ktime_set(0, 0);
3604 }
3605
Lloyd Atkinsondcb1c4a2017-07-27 10:52:09 -04003606 seq_printf(s, "vblank_enable:%d\n", sde_crtc->vblank_requested);
Alan Kwong07da0982016-11-04 12:57:45 -04003607
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07003608 mutex_unlock(&sde_crtc->crtc_lock);
3609
Clarence Ip8f7366c2016-07-05 12:15:26 -04003610 return 0;
3611}
3612
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07003613static int _sde_debugfs_status_open(struct inode *inode, struct file *file)
Clarence Ip8f7366c2016-07-05 12:15:26 -04003614{
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07003615 return single_open(file, _sde_debugfs_status_show, inode->i_private);
Clarence Ip8f7366c2016-07-05 12:15:26 -04003616}
3617
Dhaval Patelf9245d62017-03-28 16:24:00 -07003618static ssize_t _sde_crtc_misr_setup(struct file *file,
3619 const char __user *user_buf, size_t count, loff_t *ppos)
3620{
3621 struct sde_crtc *sde_crtc;
3622 struct sde_crtc_mixer *m;
3623 int i = 0, rc;
3624 char buf[MISR_BUFF_SIZE + 1];
3625 u32 frame_count, enable;
3626 size_t buff_copy;
3627
3628 if (!file || !file->private_data)
3629 return -EINVAL;
3630
3631 sde_crtc = file->private_data;
3632 buff_copy = min_t(size_t, count, MISR_BUFF_SIZE);
3633 if (copy_from_user(buf, user_buf, buff_copy)) {
3634 SDE_ERROR("buffer copy failed\n");
3635 return -EINVAL;
3636 }
3637
3638 buf[buff_copy] = 0; /* end of string */
3639
3640 if (sscanf(buf, "%u %u", &enable, &frame_count) != 2)
3641 return -EINVAL;
3642
3643 rc = _sde_crtc_power_enable(sde_crtc, true);
3644 if (rc)
3645 return rc;
3646
3647 mutex_lock(&sde_crtc->crtc_lock);
3648 sde_crtc->misr_enable = enable;
3649 for (i = 0; i < sde_crtc->num_mixers; ++i) {
3650 m = &sde_crtc->mixers[i];
3651 if (!m->hw_lm)
3652 continue;
3653
3654 m->hw_lm->ops.setup_misr(m->hw_lm, enable, frame_count);
3655 }
3656 mutex_unlock(&sde_crtc->crtc_lock);
3657 _sde_crtc_power_enable(sde_crtc, false);
3658
3659 return count;
3660}
3661
3662static ssize_t _sde_crtc_misr_read(struct file *file,
3663 char __user *user_buff, size_t count, loff_t *ppos)
3664{
3665 struct sde_crtc *sde_crtc;
3666 struct sde_crtc_mixer *m;
3667 int i = 0, rc;
3668 ssize_t len = 0;
3669 char buf[MISR_BUFF_SIZE + 1] = {'\0'};
3670
3671 if (*ppos)
3672 return 0;
3673
3674 if (!file || !file->private_data)
3675 return -EINVAL;
3676
3677 sde_crtc = file->private_data;
3678 rc = _sde_crtc_power_enable(sde_crtc, true);
3679 if (rc)
3680 return rc;
3681
3682 mutex_lock(&sde_crtc->crtc_lock);
3683 if (!sde_crtc->misr_enable) {
3684 len += snprintf(buf + len, MISR_BUFF_SIZE - len,
3685 "disabled\n");
3686 goto buff_check;
3687 }
3688
3689 for (i = 0; i < sde_crtc->num_mixers; ++i) {
3690 m = &sde_crtc->mixers[i];
3691 if (!m->hw_lm)
3692 continue;
3693
3694 len += snprintf(buf + len, MISR_BUFF_SIZE - len, "lm idx:%d\n",
3695 m->hw_lm->idx - LM_0);
3696 len += snprintf(buf + len, MISR_BUFF_SIZE - len, "0x%x\n",
3697 m->hw_lm->ops.collect_misr(m->hw_lm));
3698 }
3699
3700buff_check:
3701 if (count <= len) {
3702 len = 0;
3703 goto end;
3704 }
3705
3706 if (copy_to_user(user_buff, buf, len)) {
3707 len = -EFAULT;
3708 goto end;
3709 }
3710
3711 *ppos += len; /* increase offset */
3712
3713end:
3714 mutex_unlock(&sde_crtc->crtc_lock);
3715 _sde_crtc_power_enable(sde_crtc, false);
3716 return len;
3717}
3718
3719#define DEFINE_SDE_DEBUGFS_SEQ_FOPS(__prefix) \
Alan Kwong67a3f792016-11-01 23:16:53 -04003720static int __prefix ## _open(struct inode *inode, struct file *file) \
3721{ \
3722 return single_open(file, __prefix ## _show, inode->i_private); \
3723} \
3724static const struct file_operations __prefix ## _fops = { \
3725 .owner = THIS_MODULE, \
3726 .open = __prefix ## _open, \
3727 .release = single_release, \
3728 .read = seq_read, \
3729 .llseek = seq_lseek, \
3730}
3731
3732static int sde_crtc_debugfs_state_show(struct seq_file *s, void *v)
3733{
3734 struct drm_crtc *crtc = (struct drm_crtc *) s->private;
Alan Kwong751cf462017-06-08 10:26:46 -04003735 struct sde_crtc *sde_crtc = to_sde_crtc(crtc);
Alan Kwong67a3f792016-11-01 23:16:53 -04003736 struct sde_crtc_state *cstate = to_sde_crtc_state(crtc->state);
Alan Kwongcdb2f282017-03-18 13:42:06 -07003737 struct sde_crtc_res *res;
Alan Kwong0230a102017-05-16 11:36:44 -07003738 int i;
Alan Kwong67a3f792016-11-01 23:16:53 -04003739
3740 seq_printf(s, "num_connectors: %d\n", cstate->num_connectors);
Dhaval Patel4d424602017-02-18 19:40:14 -08003741 seq_printf(s, "client type: %d\n", sde_crtc_get_client_type(crtc));
Alan Kwong3e985f02017-02-12 15:08:44 -08003742 seq_printf(s, "intf_mode: %d\n", sde_crtc_get_intf_mode(crtc));
Alan Kwong751cf462017-06-08 10:26:46 -04003743 seq_printf(s, "core_clk_rate: %llu\n",
3744 sde_crtc->cur_perf.core_clk_rate);
Alan Kwong0230a102017-05-16 11:36:44 -07003745 for (i = SDE_POWER_HANDLE_DBUS_ID_MNOC;
3746 i < SDE_POWER_HANDLE_DBUS_ID_MAX; i++) {
3747 seq_printf(s, "bw_ctl[%s]: %llu\n",
3748 sde_power_handle_get_dbus_name(i),
3749 sde_crtc->cur_perf.bw_ctl[i]);
3750 seq_printf(s, "max_per_pipe_ib[%s]: %llu\n",
3751 sde_power_handle_get_dbus_name(i),
3752 sde_crtc->cur_perf.max_per_pipe_ib[i]);
3753 }
Alan Kwong67a3f792016-11-01 23:16:53 -04003754
Alan Kwongcdb2f282017-03-18 13:42:06 -07003755 seq_printf(s, "rp.%d: ", cstate->rp.sequence_id);
3756 list_for_each_entry(res, &cstate->rp.res_list, list)
3757 seq_printf(s, "0x%x/0x%llx/%pK/%d ",
3758 res->type, res->tag, res->val,
3759 atomic_read(&res->refcount));
3760 seq_puts(s, "\n");
3761
Alan Kwong67a3f792016-11-01 23:16:53 -04003762 return 0;
3763}
3764DEFINE_SDE_DEBUGFS_SEQ_FOPS(sde_crtc_debugfs_state);
3765
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07003766static int _sde_crtc_init_debugfs(struct drm_crtc *crtc)
Clarence Ip8f7366c2016-07-05 12:15:26 -04003767{
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07003768 struct sde_crtc *sde_crtc;
3769 struct sde_kms *sde_kms;
3770
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07003771 static const struct file_operations debugfs_status_fops = {
3772 .open = _sde_debugfs_status_open,
Clarence Ip8f7366c2016-07-05 12:15:26 -04003773 .read = seq_read,
3774 .llseek = seq_lseek,
3775 .release = single_release,
3776 };
Dhaval Patelf9245d62017-03-28 16:24:00 -07003777 static const struct file_operations debugfs_misr_fops = {
3778 .open = simple_open,
3779 .read = _sde_crtc_misr_read,
3780 .write = _sde_crtc_misr_setup,
3781 };
Alan Kwong67a3f792016-11-01 23:16:53 -04003782
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07003783 if (!crtc)
3784 return -EINVAL;
3785 sde_crtc = to_sde_crtc(crtc);
3786
3787 sde_kms = _sde_crtc_get_kms(crtc);
3788 if (!sde_kms)
3789 return -EINVAL;
3790
3791 sde_crtc->debugfs_root = debugfs_create_dir(sde_crtc->name,
Lloyd Atkinson09e64bf2017-04-13 14:09:59 -07003792 crtc->dev->primary->debugfs_root);
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07003793 if (!sde_crtc->debugfs_root)
3794 return -ENOMEM;
3795
3796 /* don't error check these */
Lloyd Atkinson8de415a2017-05-23 11:31:16 -04003797 debugfs_create_file("status", 0400,
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07003798 sde_crtc->debugfs_root,
3799 sde_crtc, &debugfs_status_fops);
Lloyd Atkinson8de415a2017-05-23 11:31:16 -04003800 debugfs_create_file("state", 0600,
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07003801 sde_crtc->debugfs_root,
3802 &sde_crtc->base,
3803 &sde_crtc_debugfs_state_fops);
Lloyd Atkinson8de415a2017-05-23 11:31:16 -04003804 debugfs_create_file("misr_data", 0600, sde_crtc->debugfs_root,
Dhaval Patelf9245d62017-03-28 16:24:00 -07003805 sde_crtc, &debugfs_misr_fops);
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07003806
3807 return 0;
3808}
3809
3810static void _sde_crtc_destroy_debugfs(struct drm_crtc *crtc)
3811{
3812 struct sde_crtc *sde_crtc;
3813
3814 if (!crtc)
3815 return;
3816 sde_crtc = to_sde_crtc(crtc);
3817 debugfs_remove_recursive(sde_crtc->debugfs_root);
Clarence Ip8f7366c2016-07-05 12:15:26 -04003818}
Alan Kwong67a3f792016-11-01 23:16:53 -04003819#else
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07003820static int _sde_crtc_init_debugfs(struct drm_crtc *crtc)
Alan Kwong67a3f792016-11-01 23:16:53 -04003821{
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07003822 return 0;
Alan Kwong67a3f792016-11-01 23:16:53 -04003823}
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07003824
3825static void _sde_crtc_destroy_debugfs(struct drm_crtc *crtc)
3826{
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07003827}
3828#endif /* CONFIG_DEBUG_FS */
3829
3830static int sde_crtc_late_register(struct drm_crtc *crtc)
3831{
3832 return _sde_crtc_init_debugfs(crtc);
3833}
3834
3835static void sde_crtc_early_unregister(struct drm_crtc *crtc)
3836{
3837 _sde_crtc_destroy_debugfs(crtc);
3838}
3839
3840static const struct drm_crtc_funcs sde_crtc_funcs = {
3841 .set_config = drm_atomic_helper_set_config,
3842 .destroy = sde_crtc_destroy,
3843 .page_flip = drm_atomic_helper_page_flip,
3844 .set_property = sde_crtc_set_property,
3845 .atomic_set_property = sde_crtc_atomic_set_property,
3846 .atomic_get_property = sde_crtc_atomic_get_property,
3847 .reset = sde_crtc_reset,
3848 .atomic_duplicate_state = sde_crtc_duplicate_state,
3849 .atomic_destroy_state = sde_crtc_destroy_state,
3850 .late_register = sde_crtc_late_register,
3851 .early_unregister = sde_crtc_early_unregister,
3852};
3853
3854static const struct drm_crtc_helper_funcs sde_crtc_helper_funcs = {
3855 .mode_fixup = sde_crtc_mode_fixup,
3856 .disable = sde_crtc_disable,
3857 .enable = sde_crtc_enable,
3858 .atomic_check = sde_crtc_atomic_check,
3859 .atomic_begin = sde_crtc_atomic_begin,
3860 .atomic_flush = sde_crtc_atomic_flush,
3861};
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07003862
Clarence Ipa18d4832017-03-13 12:35:44 -07003863static void _sde_crtc_event_cb(struct kthread_work *work)
3864{
3865 struct sde_crtc_event *event;
3866 struct sde_crtc *sde_crtc;
3867 unsigned long irq_flags;
3868
3869 if (!work) {
3870 SDE_ERROR("invalid work item\n");
3871 return;
3872 }
3873
3874 event = container_of(work, struct sde_crtc_event, kt_work);
Clarence Ipa18d4832017-03-13 12:35:44 -07003875
3876 /* set sde_crtc to NULL for static work structures */
3877 sde_crtc = event->sde_crtc;
3878 if (!sde_crtc)
3879 return;
3880
Gopikrishnaiah Anandanb6b401f2017-03-14 16:39:49 -07003881 if (event->cb_func)
3882 event->cb_func(&sde_crtc->base, event->usr);
3883
Clarence Ipa18d4832017-03-13 12:35:44 -07003884 spin_lock_irqsave(&sde_crtc->event_lock, irq_flags);
3885 list_add_tail(&event->list, &sde_crtc->event_free_list);
3886 spin_unlock_irqrestore(&sde_crtc->event_lock, irq_flags);
3887}
3888
3889int sde_crtc_event_queue(struct drm_crtc *crtc,
Gopikrishnaiah Anandanb6b401f2017-03-14 16:39:49 -07003890 void (*func)(struct drm_crtc *crtc, void *usr), void *usr)
Clarence Ipa18d4832017-03-13 12:35:44 -07003891{
3892 unsigned long irq_flags;
3893 struct sde_crtc *sde_crtc;
Veera Sundaram Sankaran10ea2bd2017-06-14 14:10:57 -07003894 struct msm_drm_private *priv;
Clarence Ipa18d4832017-03-13 12:35:44 -07003895 struct sde_crtc_event *event = NULL;
Veera Sundaram Sankaran10ea2bd2017-06-14 14:10:57 -07003896 u32 crtc_id;
Clarence Ipa18d4832017-03-13 12:35:44 -07003897
Veera Sundaram Sankaran10ea2bd2017-06-14 14:10:57 -07003898 if (!crtc || !crtc->dev || !crtc->dev->dev_private || !func) {
3899 SDE_ERROR("invalid parameters\n");
Clarence Ipa18d4832017-03-13 12:35:44 -07003900 return -EINVAL;
Veera Sundaram Sankaran10ea2bd2017-06-14 14:10:57 -07003901 }
Clarence Ipa18d4832017-03-13 12:35:44 -07003902 sde_crtc = to_sde_crtc(crtc);
Veera Sundaram Sankaran10ea2bd2017-06-14 14:10:57 -07003903 priv = crtc->dev->dev_private;
3904 crtc_id = drm_crtc_index(crtc);
Clarence Ipa18d4832017-03-13 12:35:44 -07003905
3906 /*
3907 * Obtain an event struct from the private cache. This event
3908 * queue may be called from ISR contexts, so use a private
3909 * cache to avoid calling any memory allocation functions.
3910 */
3911 spin_lock_irqsave(&sde_crtc->event_lock, irq_flags);
3912 if (!list_empty(&sde_crtc->event_free_list)) {
3913 event = list_first_entry(&sde_crtc->event_free_list,
3914 struct sde_crtc_event, list);
3915 list_del_init(&event->list);
3916 }
3917 spin_unlock_irqrestore(&sde_crtc->event_lock, irq_flags);
3918
3919 if (!event)
3920 return -ENOMEM;
3921
3922 /* populate event node */
3923 event->sde_crtc = sde_crtc;
3924 event->cb_func = func;
3925 event->usr = usr;
3926
3927 /* queue new event request */
3928 kthread_init_work(&event->kt_work, _sde_crtc_event_cb);
Veera Sundaram Sankaran10ea2bd2017-06-14 14:10:57 -07003929 kthread_queue_work(&priv->event_thread[crtc_id].worker,
3930 &event->kt_work);
Clarence Ipa18d4832017-03-13 12:35:44 -07003931
3932 return 0;
3933}
3934
3935static int _sde_crtc_init_events(struct sde_crtc *sde_crtc)
3936{
3937 int i, rc = 0;
3938
3939 if (!sde_crtc) {
3940 SDE_ERROR("invalid crtc\n");
3941 return -EINVAL;
3942 }
3943
3944 spin_lock_init(&sde_crtc->event_lock);
3945
3946 INIT_LIST_HEAD(&sde_crtc->event_free_list);
3947 for (i = 0; i < SDE_CRTC_MAX_EVENT_COUNT; ++i)
3948 list_add_tail(&sde_crtc->event_cache[i].list,
3949 &sde_crtc->event_free_list);
3950
Clarence Ipa18d4832017-03-13 12:35:44 -07003951 return rc;
3952}
3953
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04003954/* initialize crtc */
Lloyd Atkinsonac933642016-09-14 11:52:00 -04003955struct drm_crtc *sde_crtc_init(struct drm_device *dev, struct drm_plane *plane)
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07003956{
3957 struct drm_crtc *crtc = NULL;
Clarence Ip8f7366c2016-07-05 12:15:26 -04003958 struct sde_crtc *sde_crtc = NULL;
3959 struct msm_drm_private *priv = NULL;
3960 struct sde_kms *kms = NULL;
Clarence Ipa18d4832017-03-13 12:35:44 -07003961 int i, rc;
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07003962
Clarence Ip8f7366c2016-07-05 12:15:26 -04003963 priv = dev->dev_private;
3964 kms = to_sde_kms(priv->kms);
3965
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07003966 sde_crtc = kzalloc(sizeof(*sde_crtc), GFP_KERNEL);
3967 if (!sde_crtc)
3968 return ERR_PTR(-ENOMEM);
3969
3970 crtc = &sde_crtc->base;
Gopikrishnaiah Anandane0e5e0c2016-05-25 11:05:33 -07003971 crtc->dev = dev;
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07003972
Clarence Ip7f70ce42017-03-20 06:53:46 -07003973 mutex_init(&sde_crtc->crtc_lock);
Alan Kwong628d19e2016-10-31 13:50:13 -04003974 spin_lock_init(&sde_crtc->spin_lock);
3975 atomic_set(&sde_crtc->frame_pending, 0);
3976
Veera Sundaram Sankaran7ee99092017-06-13 11:19:36 -07003977 init_completion(&sde_crtc->frame_done_comp);
3978
Alan Kwong628d19e2016-10-31 13:50:13 -04003979 INIT_LIST_HEAD(&sde_crtc->frame_event_list);
Gopikrishnaiah Anandande2c81b2017-03-15 12:41:29 -07003980 INIT_LIST_HEAD(&sde_crtc->user_event_list);
Alan Kwong628d19e2016-10-31 13:50:13 -04003981 for (i = 0; i < ARRAY_SIZE(sde_crtc->frame_events); i++) {
3982 INIT_LIST_HEAD(&sde_crtc->frame_events[i].list);
3983 list_add(&sde_crtc->frame_events[i].list,
3984 &sde_crtc->frame_event_list);
3985 kthread_init_work(&sde_crtc->frame_events[i].work,
3986 sde_crtc_frame_event_work);
3987 }
3988
Dhaval Patel04c7e8e2016-09-26 20:14:31 -07003989 drm_crtc_init_with_planes(dev, crtc, plane, NULL, &sde_crtc_funcs,
3990 NULL);
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07003991
3992 drm_crtc_helper_add(crtc, &sde_crtc_helper_funcs);
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04003993 plane->crtc = crtc;
3994
Clarence Ip8f7366c2016-07-05 12:15:26 -04003995 /* save user friendly CRTC name for later */
3996 snprintf(sde_crtc->name, SDE_CRTC_NAME_SIZE, "crtc%u", crtc->base.id);
3997
Clarence Ipa18d4832017-03-13 12:35:44 -07003998 /* initialize event handling */
3999 rc = _sde_crtc_init_events(sde_crtc);
4000 if (rc) {
4001 drm_crtc_cleanup(crtc);
4002 kfree(sde_crtc);
4003 return ERR_PTR(rc);
4004 }
4005
Clarence Ip9a74a442016-08-25 18:29:03 -04004006 /* initialize output fence support */
Lloyd Atkinson5d40d312016-09-06 08:34:13 -04004007 sde_fence_init(&sde_crtc->output_fence, sde_crtc->name, crtc->base.id);
Clarence Ip24f80662016-06-13 19:05:32 -04004008
Clarence Ip7a753bb2016-07-07 11:47:44 -04004009 /* create CRTC properties */
4010 msm_property_init(&sde_crtc->property_info, &crtc->base, dev,
4011 priv->crtc_property, sde_crtc->property_data,
4012 CRTC_PROP_COUNT, CRTC_PROP_BLOBCOUNT,
4013 sizeof(struct sde_crtc_state));
4014
Dhaval Patele4a5dda2016-10-13 19:29:30 -07004015 sde_crtc_install_properties(crtc, kms->catalog);
Gopikrishnaiah Anandan703eb902016-10-06 18:43:57 -07004016
4017 /* Install color processing properties */
Gopikrishnaiah Anandane0e5e0c2016-05-25 11:05:33 -07004018 sde_cp_crtc_init(crtc);
Gopikrishnaiah Anandan703eb902016-10-06 18:43:57 -07004019 sde_cp_crtc_install_properties(crtc);
Clarence Ip7a753bb2016-07-07 11:47:44 -04004020
Dhaval Patelec10fad2016-08-22 14:40:48 -07004021 SDE_DEBUG("%s: successfully initialized crtc\n", sde_crtc->name);
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07004022 return crtc;
4023}
Gopikrishnaiah Anandande2c81b2017-03-15 12:41:29 -07004024
Gopikrishnaiah Anandan5154c712017-02-27 17:48:24 -08004025static int _sde_crtc_event_enable(struct sde_kms *kms,
4026 struct drm_crtc *crtc_drm, u32 event)
Gopikrishnaiah Anandande2c81b2017-03-15 12:41:29 -07004027{
Gopikrishnaiah Anandan5154c712017-02-27 17:48:24 -08004028 struct sde_crtc *crtc = NULL;
4029 struct sde_crtc_irq_info *node;
4030 struct msm_drm_private *priv;
4031 unsigned long flags;
4032 bool found = false;
Gopikrishnaiah Anandanb6b401f2017-03-14 16:39:49 -07004033 int ret, i = 0;
Gopikrishnaiah Anandan5154c712017-02-27 17:48:24 -08004034
4035 crtc = to_sde_crtc(crtc_drm);
4036 spin_lock_irqsave(&crtc->spin_lock, flags);
4037 list_for_each_entry(node, &crtc->user_event_list, list) {
4038 if (node->event == event) {
4039 found = true;
4040 break;
4041 }
4042 }
4043 spin_unlock_irqrestore(&crtc->spin_lock, flags);
4044
4045 /* event already enabled */
4046 if (found)
4047 return 0;
4048
Gopikrishnaiah Anandanb6b401f2017-03-14 16:39:49 -07004049 node = NULL;
4050 for (i = 0; i < ARRAY_SIZE(custom_events); i++) {
4051 if (custom_events[i].event == event &&
4052 custom_events[i].func) {
4053 node = kzalloc(sizeof(*node), GFP_KERNEL);
4054 if (!node)
4055 return -ENOMEM;
4056 node->event = event;
4057 INIT_LIST_HEAD(&node->list);
4058 node->func = custom_events[i].func;
4059 node->event = event;
4060 break;
4061 }
4062 }
Gopikrishnaiah Anandan5154c712017-02-27 17:48:24 -08004063
Gopikrishnaiah Anandanb6b401f2017-03-14 16:39:49 -07004064 if (!node) {
Gopikrishnaiah Anandan5154c712017-02-27 17:48:24 -08004065 SDE_ERROR("unsupported event %x\n", event);
Gopikrishnaiah Anandan5154c712017-02-27 17:48:24 -08004066 return -EINVAL;
4067 }
4068
4069 priv = kms->dev->dev_private;
4070 ret = 0;
4071 if (crtc_drm->enabled) {
4072 sde_power_resource_enable(&priv->phandle, kms->core_client,
4073 true);
4074 ret = node->func(crtc_drm, true, &node->irq);
4075 sde_power_resource_enable(&priv->phandle, kms->core_client,
4076 false);
4077 }
4078
4079 if (!ret) {
4080 spin_lock_irqsave(&crtc->spin_lock, flags);
4081 list_add_tail(&node->list, &crtc->user_event_list);
4082 spin_unlock_irqrestore(&crtc->spin_lock, flags);
4083 } else {
4084 kfree(node);
4085 }
4086
4087 return ret;
4088}
4089
4090static int _sde_crtc_event_disable(struct sde_kms *kms,
4091 struct drm_crtc *crtc_drm, u32 event)
4092{
4093 struct sde_crtc *crtc = NULL;
4094 struct sde_crtc_irq_info *node = NULL;
4095 struct msm_drm_private *priv;
4096 unsigned long flags;
4097 bool found = false;
4098 int ret;
4099
4100 crtc = to_sde_crtc(crtc_drm);
4101 spin_lock_irqsave(&crtc->spin_lock, flags);
4102 list_for_each_entry(node, &crtc->user_event_list, list) {
4103 if (node->event == event) {
4104 list_del(&node->list);
4105 found = true;
4106 break;
4107 }
4108 }
4109 spin_unlock_irqrestore(&crtc->spin_lock, flags);
4110
4111 /* event already disabled */
4112 if (!found)
4113 return 0;
4114
4115 /**
4116 * crtc is disabled interrupts are cleared remove from the list,
4117 * no need to disable/de-register.
4118 */
4119 if (!crtc_drm->enabled) {
4120 kfree(node);
4121 return 0;
4122 }
4123 priv = kms->dev->dev_private;
4124 sde_power_resource_enable(&priv->phandle, kms->core_client, true);
4125 ret = node->func(crtc_drm, false, &node->irq);
4126 sde_power_resource_enable(&priv->phandle, kms->core_client, false);
4127 return ret;
4128}
4129
4130int sde_crtc_register_custom_event(struct sde_kms *kms,
4131 struct drm_crtc *crtc_drm, u32 event, bool en)
4132{
4133 struct sde_crtc *crtc = NULL;
4134 int ret;
4135
4136 crtc = to_sde_crtc(crtc_drm);
4137 if (!crtc || !kms || !kms->dev) {
4138 DRM_ERROR("invalid sde_crtc %pK kms %pK dev %pK\n", crtc,
4139 kms, ((kms) ? (kms->dev) : NULL));
4140 return -EINVAL;
4141 }
4142
4143 if (en)
4144 ret = _sde_crtc_event_enable(kms, crtc_drm, event);
4145 else
4146 ret = _sde_crtc_event_disable(kms, crtc_drm, event);
4147
4148 return ret;
Gopikrishnaiah Anandande2c81b2017-03-15 12:41:29 -07004149}
Gopikrishnaiah Anandan84b4f672017-04-26 10:28:51 -07004150
4151static int sde_crtc_power_interrupt_handler(struct drm_crtc *crtc_drm,
4152 bool en, struct sde_irq_callback *irq)
4153{
4154 return 0;
4155}
Benjamin Chan90139102017-06-21 16:00:39 -04004156
4157static int sde_crtc_pm_event_handler(struct drm_crtc *crtc, bool en,
4158 struct sde_irq_callback *noirq)
4159{
4160 /*
4161 * IRQ object noirq is not being used here since there is
4162 * no crtc irq from pm event.
4163 */
4164 return 0;
4165}