blob: 935dc129839e5b165c2e10ef190a103ea0d71b64 [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/**
Clarence Ip7f70ce42017-03-20 06:53:46 -07002207 * _sde_crtc_vblank_enable_nolock - update power resource and vblank request
2208 * @sde_crtc: Pointer to sde crtc structure
2209 * @enable: Whether to enable/disable vblanks
2210 */
2211static void _sde_crtc_vblank_enable_nolock(
2212 struct sde_crtc *sde_crtc, bool enable)
2213{
2214 struct drm_device *dev;
2215 struct drm_crtc *crtc;
2216 struct drm_encoder *enc;
Clarence Ip7f70ce42017-03-20 06:53:46 -07002217
2218 if (!sde_crtc) {
2219 SDE_ERROR("invalid crtc\n");
2220 return;
2221 }
2222
2223 crtc = &sde_crtc->base;
2224 dev = crtc->dev;
Clarence Ip7f70ce42017-03-20 06:53:46 -07002225
2226 if (enable) {
Lloyd Atkinson2c554eb2017-05-24 16:22:39 -04002227 int ret;
2228
2229 /* drop lock since power crtc cb may try to re-acquire lock */
2230 mutex_unlock(&sde_crtc->crtc_lock);
2231 ret = _sde_crtc_power_enable(sde_crtc, true);
2232 mutex_lock(&sde_crtc->crtc_lock);
2233 if (ret)
Dhaval Patelf9245d62017-03-28 16:24:00 -07002234 return;
2235
Clarence Ip7f70ce42017-03-20 06:53:46 -07002236 list_for_each_entry(enc, &dev->mode_config.encoder_list, head) {
2237 if (enc->crtc != crtc)
2238 continue;
2239
2240 SDE_EVT32(DRMID(crtc), DRMID(enc), enable);
2241
2242 sde_encoder_register_vblank_callback(enc,
2243 sde_crtc_vblank_cb, (void *)crtc);
2244 }
2245 } else {
2246 list_for_each_entry(enc, &dev->mode_config.encoder_list, head) {
2247 if (enc->crtc != crtc)
2248 continue;
2249
2250 SDE_EVT32(DRMID(crtc), DRMID(enc), enable);
2251
2252 sde_encoder_register_vblank_callback(enc, NULL, NULL);
2253 }
Lloyd Atkinson2c554eb2017-05-24 16:22:39 -04002254
2255 /* drop lock since power crtc cb may try to re-acquire lock */
2256 mutex_unlock(&sde_crtc->crtc_lock);
Dhaval Patelf9245d62017-03-28 16:24:00 -07002257 _sde_crtc_power_enable(sde_crtc, false);
Lloyd Atkinson2c554eb2017-05-24 16:22:39 -04002258 mutex_lock(&sde_crtc->crtc_lock);
Clarence Ip7f70ce42017-03-20 06:53:46 -07002259 }
2260}
2261
2262/**
2263 * _sde_crtc_set_suspend - notify crtc of suspend enable/disable
2264 * @crtc: Pointer to drm crtc object
2265 * @enable: true to enable suspend, false to indicate resume
2266 */
2267static void _sde_crtc_set_suspend(struct drm_crtc *crtc, bool enable)
2268{
2269 struct sde_crtc *sde_crtc;
2270 struct msm_drm_private *priv;
2271 struct sde_kms *sde_kms;
Gopikrishnaiah Anandan84b4f672017-04-26 10:28:51 -07002272 struct drm_event event;
2273 u32 power_on;
Clarence Ip7f70ce42017-03-20 06:53:46 -07002274
2275 if (!crtc || !crtc->dev || !crtc->dev->dev_private) {
2276 SDE_ERROR("invalid crtc\n");
2277 return;
2278 }
2279 sde_crtc = to_sde_crtc(crtc);
2280 priv = crtc->dev->dev_private;
2281
2282 if (!priv->kms) {
2283 SDE_ERROR("invalid crtc kms\n");
2284 return;
2285 }
2286 sde_kms = to_sde_kms(priv->kms);
2287
2288 SDE_DEBUG("crtc%d suspend = %d\n", crtc->base.id, enable);
2289
2290 mutex_lock(&sde_crtc->crtc_lock);
2291
Gopikrishnaiah Anandan84b4f672017-04-26 10:28:51 -07002292 event.type = DRM_EVENT_CRTC_POWER;
2293 event.length = sizeof(u32);
Clarence Ip7f70ce42017-03-20 06:53:46 -07002294 /*
Clarence Ip2f9beeb2017-03-16 11:04:53 -04002295 * Update CP on suspend/resume transitions
2296 */
Gopikrishnaiah Anandan84b4f672017-04-26 10:28:51 -07002297 if (enable && !sde_crtc->suspend) {
Clarence Ip2f9beeb2017-03-16 11:04:53 -04002298 sde_cp_crtc_suspend(crtc);
Gopikrishnaiah Anandan84b4f672017-04-26 10:28:51 -07002299 power_on = 0;
2300 } else if (!enable && sde_crtc->suspend) {
Clarence Ip2f9beeb2017-03-16 11:04:53 -04002301 sde_cp_crtc_resume(crtc);
Gopikrishnaiah Anandan84b4f672017-04-26 10:28:51 -07002302 power_on = 1;
2303 }
Clarence Ip2f9beeb2017-03-16 11:04:53 -04002304
2305 /*
Lloyd Atkinsonb2be0c42017-07-17 16:41:00 -04002306 * If the vblank is enabled, release a power reference on suspend
2307 * and take it back during resume (if it is still enabled).
Clarence Ip7f70ce42017-03-20 06:53:46 -07002308 */
2309 if (sde_crtc->suspend == enable)
2310 SDE_DEBUG("crtc%d suspend already set to %d, ignoring update\n",
2311 crtc->base.id, enable);
Lloyd Atkinsonb2be0c42017-07-17 16:41:00 -04002312 else if (sde_crtc->vblank_enable)
Clarence Ip7f70ce42017-03-20 06:53:46 -07002313 _sde_crtc_vblank_enable_nolock(sde_crtc, !enable);
2314
2315 sde_crtc->suspend = enable;
Benjamin Chan34a92c72017-06-28 11:01:18 -04002316 msm_mode_object_event_notify(&crtc->base, crtc->dev, &event,
Gopikrishnaiah Anandan84b4f672017-04-26 10:28:51 -07002317 (u8 *)&power_on);
Clarence Ip7f70ce42017-03-20 06:53:46 -07002318 mutex_unlock(&sde_crtc->crtc_lock);
2319}
2320
2321/**
Clarence Ip7a753bb2016-07-07 11:47:44 -04002322 * sde_crtc_duplicate_state - state duplicate hook
2323 * @crtc: Pointer to drm crtc structure
2324 * @Returns: Pointer to new drm_crtc_state structure
2325 */
2326static struct drm_crtc_state *sde_crtc_duplicate_state(struct drm_crtc *crtc)
2327{
2328 struct sde_crtc *sde_crtc;
2329 struct sde_crtc_state *cstate, *old_cstate;
2330
2331 if (!crtc || !crtc->state) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07002332 SDE_ERROR("invalid argument(s)\n");
Clarence Ip7a753bb2016-07-07 11:47:44 -04002333 return NULL;
2334 }
2335
2336 sde_crtc = to_sde_crtc(crtc);
2337 old_cstate = to_sde_crtc_state(crtc->state);
2338 cstate = msm_property_alloc_state(&sde_crtc->property_info);
2339 if (!cstate) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07002340 SDE_ERROR("failed to allocate state\n");
Clarence Ip7a753bb2016-07-07 11:47:44 -04002341 return NULL;
2342 }
2343
2344 /* duplicate value helper */
2345 msm_property_duplicate_state(&sde_crtc->property_info,
2346 old_cstate, cstate,
Clarence Ip4a2955d2017-07-04 18:04:33 -04002347 &cstate->property_state, cstate->property_values);
Clarence Ip7a753bb2016-07-07 11:47:44 -04002348
2349 /* duplicate base helper */
2350 __drm_atomic_helper_crtc_duplicate_state(crtc, &cstate->base);
2351
Alan Kwongcdb2f282017-03-18 13:42:06 -07002352 _sde_crtc_rp_duplicate(&old_cstate->rp, &cstate->rp);
2353
Clarence Ip7a753bb2016-07-07 11:47:44 -04002354 return &cstate->base;
2355}
2356
2357/**
2358 * sde_crtc_reset - reset hook for CRTCs
2359 * Resets the atomic state for @crtc by freeing the state pointer (which might
2360 * be NULL, e.g. at driver load time) and allocating a new empty state object.
2361 * @crtc: Pointer to drm crtc structure
2362 */
2363static void sde_crtc_reset(struct drm_crtc *crtc)
2364{
2365 struct sde_crtc *sde_crtc;
2366 struct sde_crtc_state *cstate;
2367
2368 if (!crtc) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07002369 SDE_ERROR("invalid crtc\n");
Clarence Ip7a753bb2016-07-07 11:47:44 -04002370 return;
2371 }
2372
Clarence Ip7f70ce42017-03-20 06:53:46 -07002373 /* revert suspend actions, if necessary */
2374 if (msm_is_suspend_state(crtc->dev))
2375 _sde_crtc_set_suspend(crtc, false);
2376
Clarence Ip7a753bb2016-07-07 11:47:44 -04002377 /* remove previous state, if present */
2378 if (crtc->state) {
2379 sde_crtc_destroy_state(crtc, crtc->state);
2380 crtc->state = 0;
2381 }
2382
2383 sde_crtc = to_sde_crtc(crtc);
2384 cstate = msm_property_alloc_state(&sde_crtc->property_info);
2385 if (!cstate) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07002386 SDE_ERROR("failed to allocate state\n");
Clarence Ip7a753bb2016-07-07 11:47:44 -04002387 return;
2388 }
2389
2390 /* reset value helper */
2391 msm_property_reset_state(&sde_crtc->property_info, cstate,
Clarence Ip4a2955d2017-07-04 18:04:33 -04002392 &cstate->property_state,
2393 cstate->property_values);
Clarence Ip7a753bb2016-07-07 11:47:44 -04002394
Clarence Ipcae1bb62016-07-07 12:07:13 -04002395 _sde_crtc_set_input_fence_timeout(cstate);
2396
Alan Kwongcdb2f282017-03-18 13:42:06 -07002397 _sde_crtc_rp_reset(&cstate->rp);
2398
Clarence Ip7a753bb2016-07-07 11:47:44 -04002399 cstate->base.crtc = crtc;
2400 crtc->state = &cstate->base;
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07002401}
2402
Clarence Ip9728a1d2017-04-18 22:22:13 -04002403static int _sde_crtc_vblank_no_lock(struct sde_crtc *sde_crtc, bool en)
2404{
2405 if (!sde_crtc) {
2406 SDE_ERROR("invalid crtc\n");
2407 return -EINVAL;
Clarence Ip9728a1d2017-04-18 22:22:13 -04002408 }
2409
Lloyd Atkinsonb2be0c42017-07-17 16:41:00 -04002410 if (!sde_crtc->base.enabled || sde_crtc->suspend)
2411 SDE_EVT32(DRMID(&sde_crtc->base), sde_crtc->base.enabled, en,
2412 sde_crtc->vblank_enable, sde_crtc->suspend);
2413 else if (sde_crtc->vblank_enable != en)
2414 _sde_crtc_vblank_enable_nolock(sde_crtc, en);
2415 sde_crtc->vblank_enable = en;
2416
Clarence Ip9728a1d2017-04-18 22:22:13 -04002417 return 0;
2418}
2419
Veera Sundaram Sankaran82916e02017-03-29 18:44:22 -07002420static void sde_crtc_handle_power_event(u32 event_type, void *arg)
2421{
2422 struct drm_crtc *crtc = arg;
2423 struct sde_crtc *sde_crtc;
2424 struct drm_encoder *encoder;
Benjamin Chan90139102017-06-21 16:00:39 -04002425 struct drm_event event;
2426 u32 power_on = 0;
Veera Sundaram Sankaran82916e02017-03-29 18:44:22 -07002427
2428 if (!crtc) {
2429 SDE_ERROR("invalid crtc\n");
2430 return;
2431 }
2432 sde_crtc = to_sde_crtc(crtc);
2433
2434 mutex_lock(&sde_crtc->crtc_lock);
2435
2436 SDE_EVT32(DRMID(crtc), event_type);
2437
2438 if (event_type == SDE_POWER_EVENT_POST_ENABLE) {
2439 /* restore encoder; crtc will be programmed during commit */
2440 drm_for_each_encoder(encoder, crtc->dev) {
2441 if (encoder->crtc != crtc)
2442 continue;
2443
2444 sde_encoder_virt_restore(encoder);
2445 }
Ping Lie505f3b2017-06-19 14:19:08 -07002446 sde_cp_crtc_post_ipc(crtc);
Veera Sundaram Sankaran82916e02017-03-29 18:44:22 -07002447
Benjamin Chan90139102017-06-21 16:00:39 -04002448 event.type = DRM_EVENT_SDE_POWER;
2449 event.length = sizeof(power_on);
2450 power_on = 1;
2451 msm_mode_object_event_notify(&crtc->base, crtc->dev, &event,
2452 (u8 *)&power_on);
Veera Sundaram Sankaran82916e02017-03-29 18:44:22 -07002453 } else if (event_type == SDE_POWER_EVENT_POST_DISABLE) {
2454 struct drm_plane *plane;
2455
2456 /*
2457 * set revalidate flag in planes, so it will be re-programmed
2458 * in the next frame update
2459 */
2460 drm_atomic_crtc_for_each_plane(plane, crtc)
2461 sde_plane_set_revalidate(plane, true);
Alan Kwong8a9b38a2017-06-22 11:30:52 -04002462
Gopikrishnaiah Anandandb90fa12017-05-09 17:56:08 -07002463 sde_cp_crtc_suspend(crtc);
Benjamin Chan90139102017-06-21 16:00:39 -04002464
2465 event.type = DRM_EVENT_SDE_POWER;
2466 event.length = sizeof(power_on);
2467 power_on = 0;
2468 msm_mode_object_event_notify(&crtc->base, crtc->dev, &event,
2469 (u8 *)&power_on);
Ping Lie505f3b2017-06-19 14:19:08 -07002470 } else if (event_type == SDE_POWER_EVENT_PRE_DISABLE) {
2471 sde_cp_crtc_pre_ipc(crtc);
Veera Sundaram Sankaran82916e02017-03-29 18:44:22 -07002472 }
2473
2474 mutex_unlock(&sde_crtc->crtc_lock);
2475}
2476
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002477static void sde_crtc_disable(struct drm_crtc *crtc)
2478{
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04002479 struct sde_crtc *sde_crtc;
Dhaval Patel82c8dbc2017-02-18 23:15:10 -08002480 struct sde_crtc_state *cstate;
Alan Kwong07da0982016-11-04 12:57:45 -04002481 struct drm_encoder *encoder;
Veera Sundaram Sankaran82916e02017-03-29 18:44:22 -07002482 struct msm_drm_private *priv;
Gopikrishnaiah Anandande2c81b2017-03-15 12:41:29 -07002483 unsigned long flags;
2484 struct sde_crtc_irq_info *node = NULL;
2485 int ret;
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04002486
Veera Sundaram Sankaran82916e02017-03-29 18:44:22 -07002487 if (!crtc || !crtc->dev || !crtc->dev->dev_private || !crtc->state) {
Lloyd Atkinson4f1c8692016-09-14 14:04:25 -04002488 SDE_ERROR("invalid crtc\n");
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04002489 return;
2490 }
2491 sde_crtc = to_sde_crtc(crtc);
Dhaval Patel82c8dbc2017-02-18 23:15:10 -08002492 cstate = to_sde_crtc_state(crtc->state);
Veera Sundaram Sankaran82916e02017-03-29 18:44:22 -07002493 priv = crtc->dev->dev_private;
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04002494
Alan Kwong163d2612016-11-03 00:56:56 -04002495 SDE_DEBUG("crtc%d\n", crtc->base.id);
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04002496
Clarence Ip7f70ce42017-03-20 06:53:46 -07002497 if (msm_is_suspend_state(crtc->dev))
2498 _sde_crtc_set_suspend(crtc, true);
2499
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07002500 mutex_lock(&sde_crtc->crtc_lock);
Alan Kwong628d19e2016-10-31 13:50:13 -04002501 SDE_EVT32(DRMID(crtc));
2502
Veera Sundaram Sankaran7ee99092017-06-13 11:19:36 -07002503 /* wait for frame_event_done completion */
2504 if (_sde_crtc_wait_for_frame_done(crtc))
2505 SDE_ERROR("crtc%d wait for frame done failed;frame_pending%d\n",
2506 crtc->base.id,
2507 atomic_read(&sde_crtc->frame_pending));
2508
Lloyd Atkinsonb2be0c42017-07-17 16:41:00 -04002509 if (sde_crtc->vblank_enable && !sde_crtc->suspend) {
2510 SDE_DEBUG("crtc%d vblank left enabled at disable time\n",
Alan Kwong628d19e2016-10-31 13:50:13 -04002511 crtc->base.id);
Lloyd Atkinsonb2be0c42017-07-17 16:41:00 -04002512 SDE_EVT32(DRMID(crtc), sde_crtc->vblank_enable,
2513 SDE_EVTLOG_FUNC_CASE1);
2514 _sde_crtc_vblank_enable_nolock(sde_crtc, false);
Alan Kwong07da0982016-11-04 12:57:45 -04002515 }
2516
Alan Kwong628d19e2016-10-31 13:50:13 -04002517 if (atomic_read(&sde_crtc->frame_pending)) {
Dhaval Patel6c666622017-03-21 23:02:59 -07002518 SDE_EVT32(DRMID(crtc), atomic_read(&sde_crtc->frame_pending),
2519 SDE_EVTLOG_FUNC_CASE2);
Alan Kwong9aa061c2016-11-06 21:17:12 -05002520 sde_core_perf_crtc_release_bw(crtc);
Alan Kwong628d19e2016-10-31 13:50:13 -04002521 atomic_set(&sde_crtc->frame_pending, 0);
2522 }
2523
Ping Li6d5bf542017-06-27 11:40:28 -07002524 spin_lock_irqsave(&sde_crtc->spin_lock, flags);
2525 list_for_each_entry(node, &sde_crtc->user_event_list, list) {
2526 ret = 0;
2527 if (node->func)
2528 ret = node->func(crtc, false, &node->irq);
2529 if (ret)
2530 SDE_ERROR("%s failed to disable event %x\n",
2531 sde_crtc->name, node->event);
2532 }
2533 spin_unlock_irqrestore(&sde_crtc->spin_lock, flags);
2534
Alan Kwong9aa061c2016-11-06 21:17:12 -05002535 sde_core_perf_crtc_update(crtc, 0, true);
2536
Alan Kwong628d19e2016-10-31 13:50:13 -04002537 drm_for_each_encoder(encoder, crtc->dev) {
2538 if (encoder->crtc != crtc)
2539 continue;
2540 sde_encoder_register_frame_event_callback(encoder, NULL, NULL);
Dhaval Patel82c8dbc2017-02-18 23:15:10 -08002541 cstate->rsc_client = NULL;
2542 cstate->rsc_update = false;
Alan Kwong628d19e2016-10-31 13:50:13 -04002543 }
2544
Veera Sundaram Sankaran82916e02017-03-29 18:44:22 -07002545 if (sde_crtc->power_event)
2546 sde_power_handle_unregister_event(&priv->phandle,
2547 sde_crtc->power_event);
2548
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04002549 memset(sde_crtc->mixers, 0, sizeof(sde_crtc->mixers));
2550 sde_crtc->num_mixers = 0;
Gopikrishnaiah Anandande2c81b2017-03-15 12:41:29 -07002551
Alan Kwong8411a9112017-06-06 19:29:01 -04002552 /* disable clk & bw control until clk & bw properties are set */
2553 cstate->bw_control = false;
Alan Kwong0230a102017-05-16 11:36:44 -07002554 cstate->bw_split_vote = false;
Alan Kwong8411a9112017-06-06 19:29:01 -04002555
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07002556 mutex_unlock(&sde_crtc->crtc_lock);
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002557}
2558
2559static void sde_crtc_enable(struct drm_crtc *crtc)
2560{
Clarence Ipcae1bb62016-07-07 12:07:13 -04002561 struct sde_crtc *sde_crtc;
Alan Kwong628d19e2016-10-31 13:50:13 -04002562 struct drm_encoder *encoder;
Veera Sundaram Sankaran82916e02017-03-29 18:44:22 -07002563 struct msm_drm_private *priv;
Gopikrishnaiah Anandande2c81b2017-03-15 12:41:29 -07002564 unsigned long flags;
2565 struct sde_crtc_irq_info *node = NULL;
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05002566 int ret;
Lloyd Atkinsonaf7952d2016-06-26 22:41:26 -04002567
Veera Sundaram Sankaran82916e02017-03-29 18:44:22 -07002568 if (!crtc || !crtc->dev || !crtc->dev->dev_private) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07002569 SDE_ERROR("invalid crtc\n");
Clarence Ipcae1bb62016-07-07 12:07:13 -04002570 return;
2571 }
Veera Sundaram Sankaran82916e02017-03-29 18:44:22 -07002572 priv = crtc->dev->dev_private;
Clarence Ipcae1bb62016-07-07 12:07:13 -04002573
Alan Kwong163d2612016-11-03 00:56:56 -04002574 SDE_DEBUG("crtc%d\n", crtc->base.id);
Alan Kwong628d19e2016-10-31 13:50:13 -04002575 SDE_EVT32(DRMID(crtc));
Clarence Ipcae1bb62016-07-07 12:07:13 -04002576 sde_crtc = to_sde_crtc(crtc);
Lloyd Atkinsonaf7952d2016-06-26 22:41:26 -04002577
Alan Kwong628d19e2016-10-31 13:50:13 -04002578 drm_for_each_encoder(encoder, crtc->dev) {
2579 if (encoder->crtc != crtc)
2580 continue;
2581 sde_encoder_register_frame_event_callback(encoder,
2582 sde_crtc_frame_event_cb, (void *)crtc);
2583 }
2584
Lloyd Atkinsonb2be0c42017-07-17 16:41:00 -04002585 mutex_lock(&sde_crtc->crtc_lock);
2586 if (sde_crtc->vblank_enable) {
2587 /* honor user vblank request on crtc while it was disabled */
2588 SDE_DEBUG("%s vblank found enabled at crtc enable time\n",
2589 sde_crtc->name);
2590 SDE_EVT32(DRMID(crtc), sde_crtc->vblank_enable);
2591 _sde_crtc_vblank_enable_nolock(sde_crtc, true);
2592 }
2593 mutex_unlock(&sde_crtc->crtc_lock);
2594
Gopikrishnaiah Anandande2c81b2017-03-15 12:41:29 -07002595 spin_lock_irqsave(&sde_crtc->spin_lock, flags);
2596 list_for_each_entry(node, &sde_crtc->user_event_list, list) {
2597 ret = 0;
2598 if (node->func)
2599 ret = node->func(crtc, true, &node->irq);
2600 if (ret)
2601 SDE_ERROR("%s failed to enable event %x\n",
2602 sde_crtc->name, node->event);
2603 }
2604 spin_unlock_irqrestore(&sde_crtc->spin_lock, flags);
Veera Sundaram Sankaran82916e02017-03-29 18:44:22 -07002605
2606 sde_crtc->power_event = sde_power_handle_register_event(
2607 &priv->phandle,
Ping Lie505f3b2017-06-19 14:19:08 -07002608 SDE_POWER_EVENT_POST_ENABLE | SDE_POWER_EVENT_POST_DISABLE |
2609 SDE_POWER_EVENT_PRE_DISABLE,
Veera Sundaram Sankaran82916e02017-03-29 18:44:22 -07002610 sde_crtc_handle_power_event, crtc, sde_crtc->name);
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002611}
2612
2613struct plane_state {
Dhaval Patelec10fad2016-08-22 14:40:48 -07002614 struct sde_plane_state *sde_pstate;
Dhaval Patel04c7e8e2016-09-26 20:14:31 -07002615 const struct drm_plane_state *drm_pstate;
Clarence Ipc47a0692016-10-11 10:54:17 -04002616 int stage;
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08002617 u32 pipe_id;
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002618};
2619
Clarence Ipc47a0692016-10-11 10:54:17 -04002620static int pstate_cmp(const void *a, const void *b)
2621{
2622 struct plane_state *pa = (struct plane_state *)a;
2623 struct plane_state *pb = (struct plane_state *)b;
2624 int rc = 0;
2625 int pa_zpos, pb_zpos;
2626
2627 pa_zpos = sde_plane_get_property(pa->sde_pstate, PLANE_PROP_ZPOS);
2628 pb_zpos = sde_plane_get_property(pb->sde_pstate, PLANE_PROP_ZPOS);
2629
2630 if (pa_zpos != pb_zpos)
2631 rc = pa_zpos - pb_zpos;
2632 else
2633 rc = pa->drm_pstate->crtc_x - pb->drm_pstate->crtc_x;
2634
2635 return rc;
2636}
2637
Dhaval Patela8d6bc62017-05-10 17:40:18 -07002638static int _sde_crtc_excl_rect_overlap_check(struct plane_state pstates[],
2639 int cnt, int curr_cnt, struct sde_rect *excl_rect, int z_pos)
2640{
2641 struct sde_rect dst_rect, intersect;
2642 int i, rc = -EINVAL;
2643 const struct drm_plane_state *pstate;
2644
2645 /* start checking from next plane */
2646 for (i = curr_cnt; i < cnt; i++) {
2647 pstate = pstates[i].drm_pstate;
2648 POPULATE_RECT(&dst_rect, pstate->crtc_x, pstate->crtc_y,
Veera Sundaram Sankaran9d9ff912017-06-20 10:41:21 -07002649 pstate->crtc_w, pstate->crtc_h, false);
Dhaval Patela8d6bc62017-05-10 17:40:18 -07002650 sde_kms_rect_intersect(&dst_rect, excl_rect, &intersect);
2651
2652 if (intersect.w == excl_rect->w && intersect.h == excl_rect->h
2653 /* next plane may be on same z-order */
2654 && z_pos != pstates[i].stage) {
2655 rc = 0;
2656 goto end;
2657 }
2658 }
2659
2660 SDE_ERROR("excl rect does not find top overlapping rect\n");
2661end:
2662 return rc;
2663}
2664
2665/* no input validation - caller API has all the checks */
2666static int _sde_crtc_excl_dim_layer_check(struct drm_crtc_state *state,
2667 struct plane_state pstates[], int cnt)
2668{
2669 struct sde_crtc_state *cstate = to_sde_crtc_state(state);
2670 struct drm_display_mode *mode = &state->adjusted_mode;
2671 const struct drm_plane_state *pstate;
2672 struct sde_plane_state *sde_pstate;
2673 int rc = 0, i;
2674
2675 /* Check dim layer rect bounds and stage */
2676 for (i = 0; i < cstate->num_dim_layers; i++) {
2677 if ((CHECK_LAYER_BOUNDS(cstate->dim_layer[i].rect.y,
2678 cstate->dim_layer[i].rect.h, mode->vdisplay)) ||
2679 (CHECK_LAYER_BOUNDS(cstate->dim_layer[i].rect.x,
2680 cstate->dim_layer[i].rect.w, mode->hdisplay)) ||
2681 (cstate->dim_layer[i].stage >= SDE_STAGE_MAX) ||
2682 (!cstate->dim_layer[i].rect.w) ||
2683 (!cstate->dim_layer[i].rect.h)) {
2684 SDE_ERROR("invalid dim_layer:{%d,%d,%d,%d}, stage:%d\n",
2685 cstate->dim_layer[i].rect.x,
2686 cstate->dim_layer[i].rect.y,
2687 cstate->dim_layer[i].rect.w,
2688 cstate->dim_layer[i].rect.h,
2689 cstate->dim_layer[i].stage);
2690 SDE_ERROR("display: %dx%d\n", mode->hdisplay,
2691 mode->vdisplay);
2692 rc = -E2BIG;
2693 goto end;
2694 }
2695 }
2696
2697 /* this is traversing on sorted z-order pstates */
2698 for (i = 0; i < cnt; i++) {
2699 pstate = pstates[i].drm_pstate;
2700 sde_pstate = to_sde_plane_state(pstate);
2701 if (sde_pstate->excl_rect.w && sde_pstate->excl_rect.h) {
2702 /* check overlap on all top z-order */
2703 rc = _sde_crtc_excl_rect_overlap_check(pstates, cnt,
2704 i + 1, &sde_pstate->excl_rect, pstates[i].stage);
2705 if (rc)
2706 goto end;
2707 }
2708 }
2709
2710end:
2711 return rc;
2712}
2713
Abhijit Kulkarni7444a7d2017-06-21 18:53:36 -07002714static int _sde_crtc_find_plane_fb_modes(struct drm_crtc_state *state,
2715 uint32_t *fb_ns,
2716 uint32_t *fb_sec,
2717 uint32_t *fb_ns_dir,
2718 uint32_t *fb_sec_dir)
2719{
2720 struct drm_plane *plane;
2721 const struct drm_plane_state *pstate;
2722 struct sde_plane_state *sde_pstate;
2723 uint32_t mode = 0;
2724 int rc;
2725
2726 if (!state) {
2727 SDE_ERROR("invalid state\n");
2728 return -EINVAL;
2729 }
2730
2731 *fb_ns = 0;
2732 *fb_sec = 0;
2733 *fb_ns_dir = 0;
2734 *fb_sec_dir = 0;
2735 drm_atomic_crtc_state_for_each_plane_state(plane, pstate, state) {
2736 if (IS_ERR_OR_NULL(pstate)) {
2737 rc = PTR_ERR(pstate);
2738 SDE_ERROR("crtc%d failed to get plane%d state%d\n",
2739 state->crtc->base.id,
2740 plane->base.id, rc);
2741 return rc;
2742 }
2743 sde_pstate = to_sde_plane_state(pstate);
2744 mode = sde_plane_get_property(sde_pstate,
2745 PLANE_PROP_FB_TRANSLATION_MODE);
2746 switch (mode) {
2747 case SDE_DRM_FB_NON_SEC:
2748 (*fb_ns)++;
2749 break;
2750 case SDE_DRM_FB_SEC:
2751 (*fb_sec)++;
2752 break;
2753 case SDE_DRM_FB_NON_SEC_DIR_TRANS:
2754 (*fb_ns_dir)++;
2755 break;
2756 case SDE_DRM_FB_SEC_DIR_TRANS:
2757 (*fb_sec_dir)++;
2758 break;
2759 default:
2760 SDE_ERROR("Error: Plane[%d], fb_trans_mode:%d",
2761 plane->base.id,
2762 mode);
2763 return -EINVAL;
2764 }
2765 }
2766 return 0;
2767}
2768
2769static int _sde_crtc_check_secure_state(struct drm_crtc *crtc,
2770 struct drm_crtc_state *state)
2771{
2772 struct drm_encoder *encoder;
2773 struct sde_crtc_state *cstate;
2774 uint32_t secure;
2775 uint32_t fb_ns = 0, fb_sec = 0, fb_ns_dir = 0, fb_sec_dir = 0;
2776 int encoder_cnt = 0;
2777 int rc;
2778
2779 if (!crtc || !state) {
2780 SDE_ERROR("invalid arguments\n");
2781 return -EINVAL;
2782 }
2783
2784 cstate = to_sde_crtc_state(state);
2785
2786 secure = sde_crtc_get_property(cstate,
2787 CRTC_PROP_SECURITY_LEVEL);
2788
2789 rc = _sde_crtc_find_plane_fb_modes(state,
2790 &fb_ns,
2791 &fb_sec,
2792 &fb_ns_dir,
2793 &fb_sec_dir);
2794 if (rc)
2795 return rc;
2796
2797 /**
2798 * validate planes
2799 * fb_ns_dir is for secure display use case,
2800 * fb_sec_dir is for secure camera preview use case,
2801 * fb_sec is for secure video playback,
2802 * fb_ns is for normal non secure use cases.
2803 */
2804 if (((secure == SDE_DRM_SEC_ONLY) &&
2805 (fb_ns || fb_sec || fb_sec_dir)) ||
2806 (fb_sec || fb_sec_dir)) {
2807 SDE_ERROR(
2808 "crtc%d: invalid planes fb_modes Sec:%d, NS:%d, Sec_Dir:%d, NS_Dir%d\n",
2809 crtc->base.id,
2810 fb_sec, fb_ns, fb_sec_dir,
2811 fb_ns_dir);
2812 return -EINVAL;
2813 }
2814
2815 /**
2816 * secure_crtc is not allowed in a shared toppolgy
2817 * across different encoders.
2818 */
2819 if (fb_ns_dir || fb_sec_dir) {
2820 drm_for_each_encoder(encoder, crtc->dev)
2821 if (encoder->crtc == crtc)
2822 encoder_cnt++;
2823
2824 if (encoder_cnt >
2825 MAX_ALLOWED_ENCODER_CNT_PER_SECURE_CRTC) {
2826 SDE_ERROR(
2827 "crtc%d, invalid virtual encoder crtc%d\n",
2828 crtc->base.id,
2829 encoder_cnt);
2830 return -EINVAL;
2831
2832 }
2833 }
2834 SDE_DEBUG("crtc:%d Secure validation successful\n", crtc->base.id);
2835 return 0;
2836}
2837
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002838static int sde_crtc_atomic_check(struct drm_crtc *crtc,
2839 struct drm_crtc_state *state)
2840{
Clarence Ipcae1bb62016-07-07 12:07:13 -04002841 struct sde_crtc *sde_crtc;
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08002842 struct plane_state pstates[SDE_STAGE_MAX * 4];
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08002843 struct sde_crtc_state *cstate;
Dhaval Patelec10fad2016-08-22 14:40:48 -07002844
Dhaval Patel04c7e8e2016-09-26 20:14:31 -07002845 const struct drm_plane_state *pstate;
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002846 struct drm_plane *plane;
Dhaval Patelec10fad2016-08-22 14:40:48 -07002847 struct drm_display_mode *mode;
2848
2849 int cnt = 0, rc = 0, mixer_width, i, z_pos;
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08002850
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08002851 struct sde_multirect_plane_states multirect_plane[SDE_STAGE_MAX * 2];
2852 int multirect_count = 0;
2853 const struct drm_plane_state *pipe_staged[SSPP_MAX];
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04002854 int left_zpos_cnt = 0, right_zpos_cnt = 0;
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002855
Clarence Ipcae1bb62016-07-07 12:07:13 -04002856 if (!crtc) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07002857 SDE_ERROR("invalid crtc\n");
Clarence Ipcae1bb62016-07-07 12:07:13 -04002858 return -EINVAL;
2859 }
2860
Alan Kwongcdb2f282017-03-18 13:42:06 -07002861 sde_crtc = to_sde_crtc(crtc);
2862 cstate = to_sde_crtc_state(state);
2863
Lloyd Atkinson5217336c2016-09-15 18:21:18 -04002864 if (!state->enable || !state->active) {
2865 SDE_DEBUG("crtc%d -> enable %d, active %d, skip atomic_check\n",
2866 crtc->base.id, state->enable, state->active);
Alan Kwongcdb2f282017-03-18 13:42:06 -07002867 goto end;
Lloyd Atkinson5217336c2016-09-15 18:21:18 -04002868 }
2869
Dhaval Patelec10fad2016-08-22 14:40:48 -07002870 mode = &state->adjusted_mode;
2871 SDE_DEBUG("%s: check", sde_crtc->name);
Clarence Ipcae1bb62016-07-07 12:07:13 -04002872
Clarence Ip90b282d2017-05-04 10:00:32 -07002873 /* force a full mode set if active state changed */
2874 if (state->active_changed)
2875 state->mode_changed = true;
2876
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08002877 memset(pipe_staged, 0, sizeof(pipe_staged));
2878
Dhaval Patelec10fad2016-08-22 14:40:48 -07002879 mixer_width = sde_crtc_mixer_width(sde_crtc, mode);
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002880
Lloyd Atkinson66e7dde2017-02-08 15:52:53 -05002881 _sde_crtc_setup_is_ppsplit(state);
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05002882 _sde_crtc_setup_lm_bounds(crtc, state);
2883
Abhijit Kulkarni7444a7d2017-06-21 18:53:36 -07002884 rc = _sde_crtc_check_secure_state(crtc, state);
2885 if (rc)
2886 return rc;
2887
Dhaval Patelec10fad2016-08-22 14:40:48 -07002888 /* get plane state for all drm planes associated with crtc state */
Dhaval Patel04c7e8e2016-09-26 20:14:31 -07002889 drm_atomic_crtc_state_for_each_plane_state(plane, pstate, state) {
Clarence Ipc47a0692016-10-11 10:54:17 -04002890 if (IS_ERR_OR_NULL(pstate)) {
2891 rc = PTR_ERR(pstate);
2892 SDE_ERROR("%s: failed to get plane%d state, %d\n",
2893 sde_crtc->name, plane->base.id, rc);
Alan Kwong85767282016-10-03 18:03:37 -04002894 goto end;
2895 }
Clarence Ipc47a0692016-10-11 10:54:17 -04002896 if (cnt >= ARRAY_SIZE(pstates))
2897 continue;
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002898
Dhaval Patelec10fad2016-08-22 14:40:48 -07002899 pstates[cnt].sde_pstate = to_sde_plane_state(pstate);
2900 pstates[cnt].drm_pstate = pstate;
Clarence Ipc47a0692016-10-11 10:54:17 -04002901 pstates[cnt].stage = sde_plane_get_property(
2902 pstates[cnt].sde_pstate, PLANE_PROP_ZPOS);
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08002903 pstates[cnt].pipe_id = sde_plane_pipe(plane);
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08002904
2905 /* check dim layer stage with every plane */
2906 for (i = 0; i < cstate->num_dim_layers; i++) {
Veera Sundaram Sankaranb9ed6bd2017-07-11 19:18:03 -07002907 if (cstate->dim_layer[i].stage
2908 == (pstates[cnt].stage + SDE_STAGE_0)) {
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -07002909 SDE_ERROR(
2910 "plane:%d/dim_layer:%i-same stage:%d\n",
2911 plane->base.id, i,
2912 cstate->dim_layer[i].stage);
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08002913 rc = -EINVAL;
2914 goto end;
2915 }
2916 }
2917
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08002918 if (pipe_staged[pstates[cnt].pipe_id]) {
2919 multirect_plane[multirect_count].r0 =
2920 pipe_staged[pstates[cnt].pipe_id];
2921 multirect_plane[multirect_count].r1 = pstate;
2922 multirect_count++;
2923
2924 pipe_staged[pstates[cnt].pipe_id] = NULL;
2925 } else {
2926 pipe_staged[pstates[cnt].pipe_id] = pstate;
2927 }
2928
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002929 cnt++;
Dhaval Patelec10fad2016-08-22 14:40:48 -07002930
2931 if (CHECK_LAYER_BOUNDS(pstate->crtc_y, pstate->crtc_h,
2932 mode->vdisplay) ||
2933 CHECK_LAYER_BOUNDS(pstate->crtc_x, pstate->crtc_w,
2934 mode->hdisplay)) {
2935 SDE_ERROR("invalid vertical/horizontal destination\n");
2936 SDE_ERROR("y:%d h:%d vdisp:%d x:%d w:%d hdisp:%d\n",
2937 pstate->crtc_y, pstate->crtc_h, mode->vdisplay,
2938 pstate->crtc_x, pstate->crtc_w, mode->hdisplay);
2939 rc = -E2BIG;
2940 goto end;
2941 }
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002942 }
2943
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08002944 for (i = 1; i < SSPP_MAX; i++) {
Jeykumar Sankarane964dc72017-05-10 19:26:43 -07002945 if (pipe_staged[i]) {
2946 sde_plane_clear_multirect(pipe_staged[i]);
2947
2948 if (is_sde_plane_virtual(pipe_staged[i]->plane)) {
Veera Sundaram Sankaran372596d2017-06-21 17:57:25 -07002949 SDE_ERROR(
2950 "r1 only virt plane:%d not supported\n",
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08002951 pipe_staged[i]->plane->base.id);
Veera Sundaram Sankaran372596d2017-06-21 17:57:25 -07002952 rc = -EINVAL;
Jeykumar Sankarane964dc72017-05-10 19:26:43 -07002953 goto end;
2954 }
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08002955 }
2956 }
2957
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04002958 /* assign mixer stages based on sorted zpos property */
2959 sort(pstates, cnt, sizeof(pstates[0]), pstate_cmp, NULL);
2960
Dhaval Patela8d6bc62017-05-10 17:40:18 -07002961 rc = _sde_crtc_excl_dim_layer_check(state, pstates, cnt);
2962 if (rc)
2963 goto end;
2964
Clarence Ipc47a0692016-10-11 10:54:17 -04002965 if (!sde_is_custom_client()) {
2966 int stage_old = pstates[0].stage;
Dhaval Patelec10fad2016-08-22 14:40:48 -07002967
Clarence Ipc47a0692016-10-11 10:54:17 -04002968 z_pos = 0;
2969 for (i = 0; i < cnt; i++) {
2970 if (stage_old != pstates[i].stage)
2971 ++z_pos;
2972 stage_old = pstates[i].stage;
2973 pstates[i].stage = z_pos;
2974 }
2975 }
2976
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04002977 z_pos = -1;
Clarence Ipc47a0692016-10-11 10:54:17 -04002978 for (i = 0; i < cnt; i++) {
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04002979 /* reset counts at every new blend stage */
2980 if (pstates[i].stage != z_pos) {
2981 left_zpos_cnt = 0;
2982 right_zpos_cnt = 0;
2983 z_pos = pstates[i].stage;
2984 }
Clarence Ipc47a0692016-10-11 10:54:17 -04002985
2986 /* verify z_pos setting before using it */
Clarence Ip649989a2016-10-21 14:28:34 -04002987 if (z_pos >= SDE_STAGE_MAX - SDE_STAGE_0) {
Clarence Ipc47a0692016-10-11 10:54:17 -04002988 SDE_ERROR("> %d plane stages assigned\n",
2989 SDE_STAGE_MAX - SDE_STAGE_0);
2990 rc = -EINVAL;
2991 goto end;
2992 } else if (pstates[i].drm_pstate->crtc_x < mixer_width) {
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04002993 if (left_zpos_cnt == 2) {
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08002994 SDE_ERROR("> 2 planes @ stage %d on left\n",
Dhaval Patelec10fad2016-08-22 14:40:48 -07002995 z_pos);
2996 rc = -EINVAL;
2997 goto end;
2998 }
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04002999 left_zpos_cnt++;
3000
Dhaval Patelec10fad2016-08-22 14:40:48 -07003001 } else {
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04003002 if (right_zpos_cnt == 2) {
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08003003 SDE_ERROR("> 2 planes @ stage %d on right\n",
Dhaval Patelec10fad2016-08-22 14:40:48 -07003004 z_pos);
3005 rc = -EINVAL;
3006 goto end;
3007 }
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04003008 right_zpos_cnt++;
Dhaval Patelec10fad2016-08-22 14:40:48 -07003009 }
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04003010
Clarence Ipc47a0692016-10-11 10:54:17 -04003011 pstates[i].sde_pstate->stage = z_pos + SDE_STAGE_0;
Dhaval Patelec10fad2016-08-22 14:40:48 -07003012 SDE_DEBUG("%s: zpos %d", sde_crtc->name, z_pos);
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04003013 }
3014
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08003015 for (i = 0; i < multirect_count; i++) {
3016 if (sde_plane_validate_multirect_v2(&multirect_plane[i])) {
3017 SDE_ERROR(
3018 "multirect validation failed for planes (%d - %d)\n",
3019 multirect_plane[i].r0->plane->base.id,
3020 multirect_plane[i].r1->plane->base.id);
3021 rc = -EINVAL;
Alan Kwong9aa061c2016-11-06 21:17:12 -05003022 goto end;
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08003023 }
3024 }
3025
Alan Kwong9aa061c2016-11-06 21:17:12 -05003026 rc = sde_core_perf_crtc_check(crtc, state);
3027 if (rc) {
3028 SDE_ERROR("crtc%d failed performance check %d\n",
3029 crtc->base.id, rc);
3030 goto end;
3031 }
3032
Jeykumar Sankaranaaaa0712017-06-12 17:59:16 -07003033 /* validate source split:
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04003034 * use pstates sorted by stage to check planes on same stage
3035 * we assume that all pipes are in source split so its valid to compare
3036 * without taking into account left/right mixer placement
3037 */
3038 for (i = 1; i < cnt; i++) {
3039 struct plane_state *prv_pstate, *cur_pstate;
Jeykumar Sankaranaaaa0712017-06-12 17:59:16 -07003040 struct sde_rect left_rect, right_rect;
3041 int32_t left_pid, right_pid;
3042 int32_t stage;
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04003043
3044 prv_pstate = &pstates[i - 1];
3045 cur_pstate = &pstates[i];
3046 if (prv_pstate->stage != cur_pstate->stage)
3047 continue;
3048
Jeykumar Sankaranaaaa0712017-06-12 17:59:16 -07003049 stage = cur_pstate->stage;
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04003050
Jeykumar Sankaranaaaa0712017-06-12 17:59:16 -07003051 left_pid = prv_pstate->sde_pstate->base.plane->base.id;
3052 POPULATE_RECT(&left_rect, prv_pstate->drm_pstate->crtc_x,
3053 prv_pstate->drm_pstate->crtc_y,
3054 prv_pstate->drm_pstate->crtc_w,
3055 prv_pstate->drm_pstate->crtc_h, false);
3056
3057 right_pid = cur_pstate->sde_pstate->base.plane->base.id;
3058 POPULATE_RECT(&right_rect, cur_pstate->drm_pstate->crtc_x,
3059 cur_pstate->drm_pstate->crtc_y,
3060 cur_pstate->drm_pstate->crtc_w,
3061 cur_pstate->drm_pstate->crtc_h, false);
3062
3063 if (right_rect.x < left_rect.x) {
3064 swap(left_pid, right_pid);
3065 swap(left_rect, right_rect);
3066 }
3067
3068 /**
3069 * - planes are enumerated in pipe-priority order such that
3070 * planes with lower drm_id must be left-most in a shared
3071 * blend-stage when using source split.
3072 * - planes in source split must be contiguous in width
3073 * - planes in source split must have same dest yoff and height
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04003074 */
Jeykumar Sankaranaaaa0712017-06-12 17:59:16 -07003075 if (right_pid < left_pid) {
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04003076 SDE_ERROR(
Jeykumar Sankaranaaaa0712017-06-12 17:59:16 -07003077 "invalid src split cfg. priority mismatch. stage: %d left: %d right: %d\n",
3078 stage, left_pid, right_pid);
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04003079 rc = -EINVAL;
3080 goto end;
Jeykumar Sankaranaaaa0712017-06-12 17:59:16 -07003081 } else if (right_rect.x != (left_rect.x + left_rect.w)) {
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04003082 SDE_ERROR(
Jeykumar Sankaranaaaa0712017-06-12 17:59:16 -07003083 "non-contiguous coordinates for src split. stage: %d left: %d - %d right: %d - %d\n",
3084 stage, left_rect.x, left_rect.w,
3085 right_rect.x, right_rect.w);
3086 rc = -EINVAL;
3087 goto end;
3088 } else if ((left_rect.y != right_rect.y) ||
3089 (left_rect.h != right_rect.h)) {
3090 SDE_ERROR(
3091 "source split at stage: %d. invalid yoff/height: l_y: %d r_y: %d l_h: %d r_h: %d\n",
3092 stage, left_rect.y, right_rect.y,
3093 left_rect.h, right_rect.h);
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04003094 rc = -EINVAL;
3095 goto end;
3096 }
3097 }
3098
Lloyd Atkinson8ba47032017-03-22 17:13:32 -04003099 rc = _sde_crtc_check_rois(crtc, state);
3100 if (rc) {
3101 SDE_ERROR("crtc%d failed roi check %d\n", crtc->base.id, rc);
3102 goto end;
3103 }
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04003104
Dhaval Patelec10fad2016-08-22 14:40:48 -07003105end:
Alan Kwongcdb2f282017-03-18 13:42:06 -07003106 _sde_crtc_rp_free_unused(&cstate->rp);
Dhaval Patelec10fad2016-08-22 14:40:48 -07003107 return rc;
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04003108}
3109
Abhijit Kulkarni7acb3262016-07-05 15:27:25 -04003110int sde_crtc_vblank(struct drm_crtc *crtc, bool en)
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07003111{
Clarence Ip7f70ce42017-03-20 06:53:46 -07003112 struct sde_crtc *sde_crtc;
Clarence Ip9728a1d2017-04-18 22:22:13 -04003113 int rc;
Abhijit Kulkarni7acb3262016-07-05 15:27:25 -04003114
Clarence Ip7f70ce42017-03-20 06:53:46 -07003115 if (!crtc) {
3116 SDE_ERROR("invalid crtc\n");
3117 return -EINVAL;
3118 }
3119 sde_crtc = to_sde_crtc(crtc);
3120
3121 mutex_lock(&sde_crtc->crtc_lock);
Clarence Ip9728a1d2017-04-18 22:22:13 -04003122 rc = _sde_crtc_vblank_no_lock(sde_crtc, en);
Clarence Ip7f70ce42017-03-20 06:53:46 -07003123 mutex_unlock(&sde_crtc->crtc_lock);
Clarence Ip9728a1d2017-04-18 22:22:13 -04003124
Clarence Ip7f70ce42017-03-20 06:53:46 -07003125 return rc;
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07003126}
3127
Lloyd Atkinson5217336c2016-09-15 18:21:18 -04003128void sde_crtc_cancel_pending_flip(struct drm_crtc *crtc, struct drm_file *file)
3129{
3130 struct sde_crtc *sde_crtc = to_sde_crtc(crtc);
3131
Alan Kwong163d2612016-11-03 00:56:56 -04003132 SDE_DEBUG("%s: cancel: %p\n", sde_crtc->name, file);
Lloyd Atkinson4f1c8692016-09-14 14:04:25 -04003133 _sde_crtc_complete_flip(crtc, file);
Lloyd Atkinson5217336c2016-09-15 18:21:18 -04003134}
3135
Clarence Ip7a753bb2016-07-07 11:47:44 -04003136/**
3137 * sde_crtc_install_properties - install all drm properties for crtc
3138 * @crtc: Pointer to drm crtc structure
3139 */
Dhaval Patele4a5dda2016-10-13 19:29:30 -07003140static void sde_crtc_install_properties(struct drm_crtc *crtc,
3141 struct sde_mdss_cfg *catalog)
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07003142{
Clarence Ip7a753bb2016-07-07 11:47:44 -04003143 struct sde_crtc *sde_crtc;
3144 struct drm_device *dev;
Dhaval Patele4a5dda2016-10-13 19:29:30 -07003145 struct sde_kms_info *info;
Alan Kwong9aa061c2016-11-06 21:17:12 -05003146 struct sde_kms *sde_kms;
Abhijit Kulkarni50d69442017-04-11 19:50:47 -07003147 static const struct drm_prop_enum_list e_secure_level[] = {
3148 {SDE_DRM_SEC_NON_SEC, "sec_and_non_sec"},
3149 {SDE_DRM_SEC_ONLY, "sec_only"},
3150 };
Clarence Ip7a753bb2016-07-07 11:47:44 -04003151
Lloyd Atkinson4f1c8692016-09-14 14:04:25 -04003152 SDE_DEBUG("\n");
Clarence Ip7a753bb2016-07-07 11:47:44 -04003153
Dhaval Patele4a5dda2016-10-13 19:29:30 -07003154 if (!crtc || !catalog) {
3155 SDE_ERROR("invalid crtc or catalog\n");
Clarence Ip7a753bb2016-07-07 11:47:44 -04003156 return;
3157 }
3158
3159 sde_crtc = to_sde_crtc(crtc);
3160 dev = crtc->dev;
Alan Kwong9aa061c2016-11-06 21:17:12 -05003161 sde_kms = _sde_crtc_get_kms(crtc);
Clarence Ip7a753bb2016-07-07 11:47:44 -04003162
Narendra Muppallaec11a0a2017-06-15 15:35:17 -07003163 if (!sde_kms) {
3164 SDE_ERROR("invalid argument\n");
3165 return;
3166 }
3167
Dhaval Patele4a5dda2016-10-13 19:29:30 -07003168 info = kzalloc(sizeof(struct sde_kms_info), GFP_KERNEL);
3169 if (!info) {
3170 SDE_ERROR("failed to allocate info memory\n");
3171 return;
3172 }
3173
Clarence Ip7a753bb2016-07-07 11:47:44 -04003174 /* range properties */
3175 msm_property_install_range(&sde_crtc->property_info,
Dhaval Patel4e574842016-08-23 15:11:37 -07003176 "input_fence_timeout", 0x0, 0, SDE_CRTC_MAX_INPUT_FENCE_TIMEOUT,
3177 SDE_CRTC_INPUT_FENCE_TIMEOUT, CRTC_PROP_INPUT_FENCE_TIMEOUT);
3178
3179 msm_property_install_range(&sde_crtc->property_info, "output_fence",
3180 0x0, 0, INR_OPEN_MAX, 0x0, CRTC_PROP_OUTPUT_FENCE);
Clarence Ip1d9728b2016-09-01 11:10:54 -04003181
3182 msm_property_install_range(&sde_crtc->property_info,
3183 "output_fence_offset", 0x0, 0, 1, 0,
3184 CRTC_PROP_OUTPUT_FENCE_OFFSET);
Dhaval Patele4a5dda2016-10-13 19:29:30 -07003185
Alan Kwong9aa061c2016-11-06 21:17:12 -05003186 msm_property_install_range(&sde_crtc->property_info,
3187 "core_clk", 0x0, 0, U64_MAX,
3188 sde_kms->perf.max_core_clk_rate,
3189 CRTC_PROP_CORE_CLK);
3190 msm_property_install_range(&sde_crtc->property_info,
3191 "core_ab", 0x0, 0, U64_MAX,
Alan Kwongff30f4a2017-05-23 12:02:00 -07003192 catalog->perf.max_bw_high * 1000ULL,
Alan Kwong9aa061c2016-11-06 21:17:12 -05003193 CRTC_PROP_CORE_AB);
3194 msm_property_install_range(&sde_crtc->property_info,
3195 "core_ib", 0x0, 0, U64_MAX,
Alan Kwongff30f4a2017-05-23 12:02:00 -07003196 catalog->perf.max_bw_high * 1000ULL,
Alan Kwong9aa061c2016-11-06 21:17:12 -05003197 CRTC_PROP_CORE_IB);
Alan Kwong4aacd532017-02-04 18:51:33 -08003198 msm_property_install_range(&sde_crtc->property_info,
Alan Kwong0230a102017-05-16 11:36:44 -07003199 "llcc_ab", 0x0, 0, U64_MAX,
Alan Kwongff30f4a2017-05-23 12:02:00 -07003200 catalog->perf.max_bw_high * 1000ULL,
Alan Kwong0230a102017-05-16 11:36:44 -07003201 CRTC_PROP_LLCC_AB);
Alan Kwong8c176bf2017-02-09 19:34:32 -08003202 msm_property_install_range(&sde_crtc->property_info,
Alan Kwong0230a102017-05-16 11:36:44 -07003203 "llcc_ib", 0x0, 0, U64_MAX,
Alan Kwongff30f4a2017-05-23 12:02:00 -07003204 catalog->perf.max_bw_high * 1000ULL,
Alan Kwong0230a102017-05-16 11:36:44 -07003205 CRTC_PROP_LLCC_IB);
3206 msm_property_install_range(&sde_crtc->property_info,
3207 "dram_ab", 0x0, 0, U64_MAX,
3208 catalog->perf.max_bw_high * 1000ULL,
3209 CRTC_PROP_DRAM_AB);
3210 msm_property_install_range(&sde_crtc->property_info,
3211 "dram_ib", 0x0, 0, U64_MAX,
3212 catalog->perf.max_bw_high * 1000ULL,
3213 CRTC_PROP_DRAM_IB);
Alan Kwong8c176bf2017-02-09 19:34:32 -08003214 msm_property_install_range(&sde_crtc->property_info,
Alan Kwong4aacd532017-02-04 18:51:33 -08003215 "rot_prefill_bw", 0, 0, U64_MAX,
3216 catalog->perf.max_bw_high * 1000ULL,
3217 CRTC_PROP_ROT_PREFILL_BW);
Alan Kwong8c176bf2017-02-09 19:34:32 -08003218 msm_property_install_range(&sde_crtc->property_info,
3219 "rot_clk", 0, 0, U64_MAX,
3220 sde_kms->perf.max_core_clk_rate,
3221 CRTC_PROP_ROT_CLK);
Alan Kwong9aa061c2016-11-06 21:17:12 -05003222
Dhaval Patele4a5dda2016-10-13 19:29:30 -07003223 msm_property_install_blob(&sde_crtc->property_info, "capabilities",
3224 DRM_MODE_PROP_IMMUTABLE, CRTC_PROP_INFO);
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08003225
Lloyd Atkinson8ba47032017-03-22 17:13:32 -04003226 msm_property_install_volatile_range(&sde_crtc->property_info,
3227 "sde_drm_roi_v1", 0x0, 0, ~0, 0, CRTC_PROP_ROI_V1);
3228
Abhijit Kulkarni50d69442017-04-11 19:50:47 -07003229 msm_property_install_enum(&sde_crtc->property_info, "security_level",
3230 0x0, 0, e_secure_level,
3231 ARRAY_SIZE(e_secure_level),
3232 CRTC_PROP_SECURITY_LEVEL);
3233
Dhaval Patele4a5dda2016-10-13 19:29:30 -07003234 sde_kms_info_reset(info);
3235
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -07003236 if (catalog->has_dim_layer) {
3237 msm_property_install_volatile_range(&sde_crtc->property_info,
3238 "dim_layer_v1", 0x0, 0, ~0, 0, CRTC_PROP_DIM_LAYER_V1);
3239 sde_kms_info_add_keyint(info, "dim_layer_v1_max_layers",
3240 SDE_MAX_DIM_LAYERS);
3241 }
3242
Dhaval Patele4a5dda2016-10-13 19:29:30 -07003243 sde_kms_info_add_keyint(info, "hw_version", catalog->hwversion);
3244 sde_kms_info_add_keyint(info, "max_linewidth",
3245 catalog->max_mixer_width);
3246 sde_kms_info_add_keyint(info, "max_blendstages",
3247 catalog->max_mixer_blendstages);
3248 if (catalog->qseed_type == SDE_SSPP_SCALER_QSEED2)
3249 sde_kms_info_add_keystr(info, "qseed_type", "qseed2");
3250 if (catalog->qseed_type == SDE_SSPP_SCALER_QSEED3)
3251 sde_kms_info_add_keystr(info, "qseed_type", "qseed3");
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08003252
3253 if (sde_is_custom_client()) {
3254 if (catalog->smart_dma_rev == SDE_SSPP_SMART_DMA_V1)
3255 sde_kms_info_add_keystr(info,
3256 "smart_dma_rev", "smart_dma_v1");
3257 if (catalog->smart_dma_rev == SDE_SSPP_SMART_DMA_V2)
3258 sde_kms_info_add_keystr(info,
3259 "smart_dma_rev", "smart_dma_v2");
3260 }
3261
Dhaval Patele4a5dda2016-10-13 19:29:30 -07003262 sde_kms_info_add_keyint(info, "has_src_split", catalog->has_src_split);
Alan Kwong2f84f8a2016-12-29 13:07:47 -05003263 if (catalog->perf.max_bw_low)
3264 sde_kms_info_add_keyint(info, "max_bandwidth_low",
Alan Kwong6259a382017-04-04 06:18:02 -07003265 catalog->perf.max_bw_low * 1000LL);
Alan Kwong2f84f8a2016-12-29 13:07:47 -05003266 if (catalog->perf.max_bw_high)
3267 sde_kms_info_add_keyint(info, "max_bandwidth_high",
Alan Kwong6259a382017-04-04 06:18:02 -07003268 catalog->perf.max_bw_high * 1000LL);
Alan Kwong2f84f8a2016-12-29 13:07:47 -05003269 if (sde_kms->perf.max_core_clk_rate)
3270 sde_kms_info_add_keyint(info, "max_mdp_clk",
3271 sde_kms->perf.max_core_clk_rate);
Alan Kwong6259a382017-04-04 06:18:02 -07003272 sde_kms_info_add_keystr(info, "core_ib_ff",
3273 catalog->perf.core_ib_ff);
3274 sde_kms_info_add_keystr(info, "core_clk_ff",
3275 catalog->perf.core_clk_ff);
3276 sde_kms_info_add_keystr(info, "comp_ratio_rt",
3277 catalog->perf.comp_ratio_rt);
3278 sde_kms_info_add_keystr(info, "comp_ratio_nrt",
3279 catalog->perf.comp_ratio_nrt);
3280 sde_kms_info_add_keyint(info, "dest_scale_prefill_lines",
3281 catalog->perf.dest_scale_prefill_lines);
3282 sde_kms_info_add_keyint(info, "undersized_prefill_lines",
3283 catalog->perf.undersized_prefill_lines);
3284 sde_kms_info_add_keyint(info, "macrotile_prefill_lines",
3285 catalog->perf.macrotile_prefill_lines);
3286 sde_kms_info_add_keyint(info, "yuv_nv12_prefill_lines",
3287 catalog->perf.yuv_nv12_prefill_lines);
3288 sde_kms_info_add_keyint(info, "linear_prefill_lines",
3289 catalog->perf.linear_prefill_lines);
3290 sde_kms_info_add_keyint(info, "downscaling_prefill_lines",
3291 catalog->perf.downscaling_prefill_lines);
3292 sde_kms_info_add_keyint(info, "xtra_prefill_lines",
3293 catalog->perf.xtra_prefill_lines);
3294 sde_kms_info_add_keyint(info, "amortizable_threshold",
3295 catalog->perf.amortizable_threshold);
3296 sde_kms_info_add_keyint(info, "min_prefill_lines",
3297 catalog->perf.min_prefill_lines);
3298
Dhaval Patele4a5dda2016-10-13 19:29:30 -07003299 msm_property_set_blob(&sde_crtc->property_info, &sde_crtc->blob_info,
Narendra Muppalla22d17252017-05-31 15:13:39 -07003300 info->data, SDE_KMS_INFO_DATALEN(info), CRTC_PROP_INFO);
Dhaval Patele4a5dda2016-10-13 19:29:30 -07003301
3302 kfree(info);
Clarence Ip7a753bb2016-07-07 11:47:44 -04003303}
3304
3305/**
3306 * sde_crtc_atomic_set_property - atomically set a crtc drm property
3307 * @crtc: Pointer to drm crtc structure
3308 * @state: Pointer to drm crtc state structure
3309 * @property: Pointer to targeted drm property
3310 * @val: Updated property value
3311 * @Returns: Zero on success
3312 */
3313static int sde_crtc_atomic_set_property(struct drm_crtc *crtc,
3314 struct drm_crtc_state *state,
3315 struct drm_property *property,
3316 uint64_t val)
3317{
3318 struct sde_crtc *sde_crtc;
3319 struct sde_crtc_state *cstate;
Clarence Ipcae1bb62016-07-07 12:07:13 -04003320 int idx, ret = -EINVAL;
Clarence Ip7a753bb2016-07-07 11:47:44 -04003321
3322 if (!crtc || !state || !property) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07003323 SDE_ERROR("invalid argument(s)\n");
Clarence Ip7a753bb2016-07-07 11:47:44 -04003324 } else {
3325 sde_crtc = to_sde_crtc(crtc);
3326 cstate = to_sde_crtc_state(state);
3327 ret = msm_property_atomic_set(&sde_crtc->property_info,
Clarence Ip4a2955d2017-07-04 18:04:33 -04003328 &cstate->property_state, property, val);
Clarence Ipcae1bb62016-07-07 12:07:13 -04003329 if (!ret) {
3330 idx = msm_property_index(&sde_crtc->property_info,
3331 property);
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08003332 switch (idx) {
3333 case CRTC_PROP_INPUT_FENCE_TIMEOUT:
Clarence Ipcae1bb62016-07-07 12:07:13 -04003334 _sde_crtc_set_input_fence_timeout(cstate);
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08003335 break;
3336 case CRTC_PROP_DIM_LAYER_V1:
3337 _sde_crtc_set_dim_layer_v1(cstate, (void *)val);
3338 break;
Lloyd Atkinson8ba47032017-03-22 17:13:32 -04003339 case CRTC_PROP_ROI_V1:
3340 ret = _sde_crtc_set_roi_v1(state, (void *)val);
3341 break;
Alan Kwong8411a9112017-06-06 19:29:01 -04003342 case CRTC_PROP_CORE_CLK:
Alan Kwongff30f4a2017-05-23 12:02:00 -07003343 case CRTC_PROP_CORE_AB:
3344 case CRTC_PROP_CORE_IB:
Alan Kwongff30f4a2017-05-23 12:02:00 -07003345 cstate->bw_control = true;
3346 break;
Alan Kwong0230a102017-05-16 11:36:44 -07003347 case CRTC_PROP_LLCC_AB:
3348 case CRTC_PROP_LLCC_IB:
3349 case CRTC_PROP_DRAM_AB:
3350 case CRTC_PROP_DRAM_IB:
3351 cstate->bw_control = true;
3352 cstate->bw_split_vote = true;
3353 break;
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08003354 default:
3355 /* nothing to do */
3356 break;
3357 }
Gopikrishnaiah Anandane0e5e0c2016-05-25 11:05:33 -07003358 } else {
3359 ret = sde_cp_crtc_set_property(crtc,
3360 property, val);
Clarence Ipcae1bb62016-07-07 12:07:13 -04003361 }
Gopikrishnaiah Anandane0e5e0c2016-05-25 11:05:33 -07003362 if (ret)
3363 DRM_ERROR("failed to set the property\n");
Alan Kwongcdb2f282017-03-18 13:42:06 -07003364
3365 SDE_DEBUG("crtc%d %s[%d] <= 0x%llx ret=%d\n", crtc->base.id,
3366 property->name, property->base.id, val, ret);
Clarence Ip7a753bb2016-07-07 11:47:44 -04003367 }
3368
3369 return ret;
3370}
3371
3372/**
3373 * sde_crtc_set_property - set a crtc drm property
3374 * @crtc: Pointer to drm crtc structure
3375 * @property: Pointer to targeted drm property
3376 * @val: Updated property value
3377 * @Returns: Zero on success
3378 */
3379static int sde_crtc_set_property(struct drm_crtc *crtc,
3380 struct drm_property *property, uint64_t val)
3381{
Lloyd Atkinson4f1c8692016-09-14 14:04:25 -04003382 SDE_DEBUG("\n");
Clarence Ip7a753bb2016-07-07 11:47:44 -04003383
3384 return sde_crtc_atomic_set_property(crtc, crtc->state, property, val);
3385}
3386
3387/**
3388 * sde_crtc_atomic_get_property - retrieve a crtc drm property
3389 * @crtc: Pointer to drm crtc structure
3390 * @state: Pointer to drm crtc state structure
3391 * @property: Pointer to targeted drm property
3392 * @val: Pointer to variable for receiving property value
3393 * @Returns: Zero on success
3394 */
3395static int sde_crtc_atomic_get_property(struct drm_crtc *crtc,
3396 const struct drm_crtc_state *state,
3397 struct drm_property *property,
3398 uint64_t *val)
3399{
3400 struct sde_crtc *sde_crtc;
3401 struct sde_crtc_state *cstate;
Veera Sundaram Sankaran675ff622017-06-21 21:44:46 -07003402 struct drm_encoder *encoder;
Clarence Ip24f80662016-06-13 19:05:32 -04003403 int i, ret = -EINVAL;
Dhaval Patel5cb59be2017-04-20 20:00:56 -07003404 bool conn_offset = 0;
Veera Sundaram Sankaran675ff622017-06-21 21:44:46 -07003405 bool is_cmd = true;
Clarence Ip7a753bb2016-07-07 11:47:44 -04003406
3407 if (!crtc || !state) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07003408 SDE_ERROR("invalid argument(s)\n");
Clarence Ip7a753bb2016-07-07 11:47:44 -04003409 } else {
3410 sde_crtc = to_sde_crtc(crtc);
3411 cstate = to_sde_crtc_state(state);
Dhaval Patel5cb59be2017-04-20 20:00:56 -07003412
3413 for (i = 0; i < cstate->num_connectors; ++i) {
3414 conn_offset = sde_connector_needs_offset(
3415 cstate->connectors[i]);
3416 if (conn_offset)
3417 break;
3418 }
3419
Veera Sundaram Sankaran675ff622017-06-21 21:44:46 -07003420 /**
3421 * set the cmd flag only when all the encoders attached
3422 * to the crtc are in cmd mode. Consider all other cases
3423 * as video mode.
3424 */
3425 drm_for_each_encoder(encoder, crtc->dev) {
3426 if (encoder->crtc == crtc)
3427 is_cmd &= sde_encoder_is_cmd_mode(encoder);
3428 }
3429
Clarence Ip24f80662016-06-13 19:05:32 -04003430 i = msm_property_index(&sde_crtc->property_info, property);
3431 if (i == CRTC_PROP_OUTPUT_FENCE) {
Dhaval Patel39323d42017-03-01 23:48:24 -08003432 uint32_t offset = sde_crtc_get_property(cstate,
Clarence Ip1d9728b2016-09-01 11:10:54 -04003433 CRTC_PROP_OUTPUT_FENCE_OFFSET);
3434
Veera Sundaram Sankaran675ff622017-06-21 21:44:46 -07003435 /**
3436 * set the offset to 0 only for cmd mode panels, so
3437 * the release fence for the current frame can be
3438 * triggered right after PP_DONE interrupt.
3439 */
3440 offset = is_cmd ? 0 : (offset + conn_offset);
3441
Dhaval Patel5cb59be2017-04-20 20:00:56 -07003442 ret = sde_fence_create(&sde_crtc->output_fence, val,
Veera Sundaram Sankaran675ff622017-06-21 21:44:46 -07003443 offset);
Clarence Ip1d9728b2016-09-01 11:10:54 -04003444 if (ret)
3445 SDE_ERROR("fence create failed\n");
Clarence Ip24f80662016-06-13 19:05:32 -04003446 } else {
3447 ret = msm_property_atomic_get(&sde_crtc->property_info,
Clarence Ip4a2955d2017-07-04 18:04:33 -04003448 &cstate->property_state,
3449 property, val);
Gopikrishnaiah Anandane0e5e0c2016-05-25 11:05:33 -07003450 if (ret)
3451 ret = sde_cp_crtc_get_property(crtc,
3452 property, val);
Clarence Ip24f80662016-06-13 19:05:32 -04003453 }
Gopikrishnaiah Anandane0e5e0c2016-05-25 11:05:33 -07003454 if (ret)
3455 DRM_ERROR("get property failed\n");
Clarence Ip7a753bb2016-07-07 11:47:44 -04003456 }
Clarence Ip7a753bb2016-07-07 11:47:44 -04003457 return ret;
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07003458}
3459
Alan Kwong67a3f792016-11-01 23:16:53 -04003460#ifdef CONFIG_DEBUG_FS
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07003461static int _sde_debugfs_status_show(struct seq_file *s, void *data)
Clarence Ip8f7366c2016-07-05 12:15:26 -04003462{
3463 struct sde_crtc *sde_crtc;
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07003464 struct sde_plane_state *pstate = NULL;
Clarence Ip8f7366c2016-07-05 12:15:26 -04003465 struct sde_crtc_mixer *m;
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07003466
3467 struct drm_crtc *crtc;
3468 struct drm_plane *plane;
3469 struct drm_display_mode *mode;
3470 struct drm_framebuffer *fb;
3471 struct drm_plane_state *state;
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -07003472 struct sde_crtc_state *cstate;
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07003473
3474 int i, out_width;
Clarence Ip8f7366c2016-07-05 12:15:26 -04003475
3476 if (!s || !s->private)
3477 return -EINVAL;
3478
3479 sde_crtc = s->private;
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07003480 crtc = &sde_crtc->base;
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -07003481 cstate = to_sde_crtc_state(crtc->state);
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07003482
3483 mutex_lock(&sde_crtc->crtc_lock);
3484 mode = &crtc->state->adjusted_mode;
3485 out_width = sde_crtc_mixer_width(sde_crtc, mode);
3486
3487 seq_printf(s, "crtc:%d width:%d height:%d\n", crtc->base.id,
3488 mode->hdisplay, mode->vdisplay);
3489
3490 seq_puts(s, "\n");
3491
Clarence Ip8f7366c2016-07-05 12:15:26 -04003492 for (i = 0; i < sde_crtc->num_mixers; ++i) {
Lloyd Atkinsone7bcdd22016-08-11 10:53:37 -04003493 m = &sde_crtc->mixers[i];
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07003494 if (!m->hw_lm)
3495 seq_printf(s, "\tmixer[%d] has no lm\n", i);
3496 else if (!m->hw_ctl)
3497 seq_printf(s, "\tmixer[%d] has no ctl\n", i);
3498 else
3499 seq_printf(s, "\tmixer:%d ctl:%d width:%d height:%d\n",
3500 m->hw_lm->idx - LM_0, m->hw_ctl->idx - CTL_0,
3501 out_width, mode->vdisplay);
Clarence Ip8f7366c2016-07-05 12:15:26 -04003502 }
Dhaval Patel44f12472016-08-29 12:19:47 -07003503
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07003504 seq_puts(s, "\n");
Dhaval Patel48c76022016-09-01 17:51:23 -07003505
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -07003506 for (i = 0; i < cstate->num_dim_layers; i++) {
3507 struct sde_hw_dim_layer *dim_layer = &cstate->dim_layer[i];
3508
3509 seq_printf(s, "\tdim_layer:%d] stage:%d flags:%d\n",
3510 i, dim_layer->stage, dim_layer->flags);
3511 seq_printf(s, "\tdst_x:%d dst_y:%d dst_w:%d dst_h:%d\n",
3512 dim_layer->rect.x, dim_layer->rect.y,
3513 dim_layer->rect.w, dim_layer->rect.h);
3514 seq_printf(s,
3515 "\tcolor_0:%d color_1:%d color_2:%d color_3:%d\n",
3516 dim_layer->color_fill.color_0,
3517 dim_layer->color_fill.color_1,
3518 dim_layer->color_fill.color_2,
3519 dim_layer->color_fill.color_3);
3520 seq_puts(s, "\n");
3521 }
3522
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07003523 drm_atomic_crtc_for_each_plane(plane, crtc) {
3524 pstate = to_sde_plane_state(plane->state);
3525 state = plane->state;
3526
3527 if (!pstate || !state)
3528 continue;
3529
3530 seq_printf(s, "\tplane:%u stage:%d\n", plane->base.id,
3531 pstate->stage);
3532
3533 if (plane->state->fb) {
3534 fb = plane->state->fb;
3535
3536 seq_printf(s, "\tfb:%d image format:%4.4s wxh:%ux%u bpp:%d\n",
3537 fb->base.id, (char *) &fb->pixel_format,
3538 fb->width, fb->height, fb->bits_per_pixel);
3539
3540 seq_puts(s, "\t");
3541 for (i = 0; i < ARRAY_SIZE(fb->modifier); i++)
3542 seq_printf(s, "modifier[%d]:%8llu ", i,
3543 fb->modifier[i]);
3544 seq_puts(s, "\n");
3545
3546 seq_puts(s, "\t");
3547 for (i = 0; i < ARRAY_SIZE(fb->pitches); i++)
3548 seq_printf(s, "pitches[%d]:%8u ", i,
3549 fb->pitches[i]);
3550 seq_puts(s, "\n");
3551
3552 seq_puts(s, "\t");
3553 for (i = 0; i < ARRAY_SIZE(fb->offsets); i++)
3554 seq_printf(s, "offsets[%d]:%8u ", i,
3555 fb->offsets[i]);
Dhaval Patel48c76022016-09-01 17:51:23 -07003556 seq_puts(s, "\n");
3557 }
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07003558
3559 seq_printf(s, "\tsrc_x:%4d src_y:%4d src_w:%4d src_h:%4d\n",
3560 state->src_x, state->src_y, state->src_w, state->src_h);
3561
3562 seq_printf(s, "\tdst x:%4d dst_y:%4d dst_w:%4d dst_h:%4d\n",
3563 state->crtc_x, state->crtc_y, state->crtc_w,
3564 state->crtc_h);
Jeykumar Sankarane964dc72017-05-10 19:26:43 -07003565 seq_printf(s, "\tmultirect: mode: %d index: %d\n",
3566 pstate->multirect_mode, pstate->multirect_index);
Veera Sundaram Sankaran58e12812017-05-05 11:51:09 -07003567
3568 seq_printf(s, "\texcl_rect: x:%4d y:%4d w:%4d h:%4d\n",
3569 pstate->excl_rect.x, pstate->excl_rect.y,
3570 pstate->excl_rect.w, pstate->excl_rect.h);
3571
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07003572 seq_puts(s, "\n");
Clarence Ip8f7366c2016-07-05 12:15:26 -04003573 }
Alan Kwong07da0982016-11-04 12:57:45 -04003574
3575 if (sde_crtc->vblank_cb_count) {
3576 ktime_t diff = ktime_sub(ktime_get(), sde_crtc->vblank_cb_time);
3577 s64 diff_ms = ktime_to_ms(diff);
3578 s64 fps = diff_ms ? DIV_ROUND_CLOSEST(
3579 sde_crtc->vblank_cb_count * 1000, diff_ms) : 0;
3580
3581 seq_printf(s,
Dhaval Pateld67cf4a2017-06-14 18:08:32 -07003582 "vblank fps:%lld count:%u total:%llums total_framecount:%llu\n",
3583 fps, sde_crtc->vblank_cb_count,
3584 ktime_to_ms(diff), sde_crtc->play_count);
Alan Kwong07da0982016-11-04 12:57:45 -04003585
3586 /* reset time & count for next measurement */
3587 sde_crtc->vblank_cb_count = 0;
3588 sde_crtc->vblank_cb_time = ktime_set(0, 0);
3589 }
3590
Lloyd Atkinsonb2be0c42017-07-17 16:41:00 -04003591 seq_printf(s, "vblank_enable:%d\n", sde_crtc->vblank_enable);
Alan Kwong07da0982016-11-04 12:57:45 -04003592
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07003593 mutex_unlock(&sde_crtc->crtc_lock);
3594
Clarence Ip8f7366c2016-07-05 12:15:26 -04003595 return 0;
3596}
3597
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07003598static int _sde_debugfs_status_open(struct inode *inode, struct file *file)
Clarence Ip8f7366c2016-07-05 12:15:26 -04003599{
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07003600 return single_open(file, _sde_debugfs_status_show, inode->i_private);
Clarence Ip8f7366c2016-07-05 12:15:26 -04003601}
3602
Dhaval Patelf9245d62017-03-28 16:24:00 -07003603static ssize_t _sde_crtc_misr_setup(struct file *file,
3604 const char __user *user_buf, size_t count, loff_t *ppos)
3605{
3606 struct sde_crtc *sde_crtc;
3607 struct sde_crtc_mixer *m;
3608 int i = 0, rc;
3609 char buf[MISR_BUFF_SIZE + 1];
3610 u32 frame_count, enable;
3611 size_t buff_copy;
3612
3613 if (!file || !file->private_data)
3614 return -EINVAL;
3615
3616 sde_crtc = file->private_data;
3617 buff_copy = min_t(size_t, count, MISR_BUFF_SIZE);
3618 if (copy_from_user(buf, user_buf, buff_copy)) {
3619 SDE_ERROR("buffer copy failed\n");
3620 return -EINVAL;
3621 }
3622
3623 buf[buff_copy] = 0; /* end of string */
3624
3625 if (sscanf(buf, "%u %u", &enable, &frame_count) != 2)
3626 return -EINVAL;
3627
3628 rc = _sde_crtc_power_enable(sde_crtc, true);
3629 if (rc)
3630 return rc;
3631
3632 mutex_lock(&sde_crtc->crtc_lock);
3633 sde_crtc->misr_enable = enable;
3634 for (i = 0; i < sde_crtc->num_mixers; ++i) {
3635 m = &sde_crtc->mixers[i];
3636 if (!m->hw_lm)
3637 continue;
3638
3639 m->hw_lm->ops.setup_misr(m->hw_lm, enable, frame_count);
3640 }
3641 mutex_unlock(&sde_crtc->crtc_lock);
3642 _sde_crtc_power_enable(sde_crtc, false);
3643
3644 return count;
3645}
3646
3647static ssize_t _sde_crtc_misr_read(struct file *file,
3648 char __user *user_buff, size_t count, loff_t *ppos)
3649{
3650 struct sde_crtc *sde_crtc;
3651 struct sde_crtc_mixer *m;
3652 int i = 0, rc;
3653 ssize_t len = 0;
3654 char buf[MISR_BUFF_SIZE + 1] = {'\0'};
3655
3656 if (*ppos)
3657 return 0;
3658
3659 if (!file || !file->private_data)
3660 return -EINVAL;
3661
3662 sde_crtc = file->private_data;
3663 rc = _sde_crtc_power_enable(sde_crtc, true);
3664 if (rc)
3665 return rc;
3666
3667 mutex_lock(&sde_crtc->crtc_lock);
3668 if (!sde_crtc->misr_enable) {
3669 len += snprintf(buf + len, MISR_BUFF_SIZE - len,
3670 "disabled\n");
3671 goto buff_check;
3672 }
3673
3674 for (i = 0; i < sde_crtc->num_mixers; ++i) {
3675 m = &sde_crtc->mixers[i];
3676 if (!m->hw_lm)
3677 continue;
3678
3679 len += snprintf(buf + len, MISR_BUFF_SIZE - len, "lm idx:%d\n",
3680 m->hw_lm->idx - LM_0);
3681 len += snprintf(buf + len, MISR_BUFF_SIZE - len, "0x%x\n",
3682 m->hw_lm->ops.collect_misr(m->hw_lm));
3683 }
3684
3685buff_check:
3686 if (count <= len) {
3687 len = 0;
3688 goto end;
3689 }
3690
3691 if (copy_to_user(user_buff, buf, len)) {
3692 len = -EFAULT;
3693 goto end;
3694 }
3695
3696 *ppos += len; /* increase offset */
3697
3698end:
3699 mutex_unlock(&sde_crtc->crtc_lock);
3700 _sde_crtc_power_enable(sde_crtc, false);
3701 return len;
3702}
3703
3704#define DEFINE_SDE_DEBUGFS_SEQ_FOPS(__prefix) \
Alan Kwong67a3f792016-11-01 23:16:53 -04003705static int __prefix ## _open(struct inode *inode, struct file *file) \
3706{ \
3707 return single_open(file, __prefix ## _show, inode->i_private); \
3708} \
3709static const struct file_operations __prefix ## _fops = { \
3710 .owner = THIS_MODULE, \
3711 .open = __prefix ## _open, \
3712 .release = single_release, \
3713 .read = seq_read, \
3714 .llseek = seq_lseek, \
3715}
3716
3717static int sde_crtc_debugfs_state_show(struct seq_file *s, void *v)
3718{
3719 struct drm_crtc *crtc = (struct drm_crtc *) s->private;
Alan Kwong751cf462017-06-08 10:26:46 -04003720 struct sde_crtc *sde_crtc = to_sde_crtc(crtc);
Alan Kwong67a3f792016-11-01 23:16:53 -04003721 struct sde_crtc_state *cstate = to_sde_crtc_state(crtc->state);
Alan Kwongcdb2f282017-03-18 13:42:06 -07003722 struct sde_crtc_res *res;
Alan Kwong0230a102017-05-16 11:36:44 -07003723 int i;
Alan Kwong67a3f792016-11-01 23:16:53 -04003724
3725 seq_printf(s, "num_connectors: %d\n", cstate->num_connectors);
Dhaval Patel4d424602017-02-18 19:40:14 -08003726 seq_printf(s, "client type: %d\n", sde_crtc_get_client_type(crtc));
Alan Kwong3e985f02017-02-12 15:08:44 -08003727 seq_printf(s, "intf_mode: %d\n", sde_crtc_get_intf_mode(crtc));
Alan Kwong751cf462017-06-08 10:26:46 -04003728 seq_printf(s, "core_clk_rate: %llu\n",
3729 sde_crtc->cur_perf.core_clk_rate);
Alan Kwong0230a102017-05-16 11:36:44 -07003730 for (i = SDE_POWER_HANDLE_DBUS_ID_MNOC;
3731 i < SDE_POWER_HANDLE_DBUS_ID_MAX; i++) {
3732 seq_printf(s, "bw_ctl[%s]: %llu\n",
3733 sde_power_handle_get_dbus_name(i),
3734 sde_crtc->cur_perf.bw_ctl[i]);
3735 seq_printf(s, "max_per_pipe_ib[%s]: %llu\n",
3736 sde_power_handle_get_dbus_name(i),
3737 sde_crtc->cur_perf.max_per_pipe_ib[i]);
3738 }
Alan Kwong67a3f792016-11-01 23:16:53 -04003739
Alan Kwongcdb2f282017-03-18 13:42:06 -07003740 seq_printf(s, "rp.%d: ", cstate->rp.sequence_id);
3741 list_for_each_entry(res, &cstate->rp.res_list, list)
3742 seq_printf(s, "0x%x/0x%llx/%pK/%d ",
3743 res->type, res->tag, res->val,
3744 atomic_read(&res->refcount));
3745 seq_puts(s, "\n");
3746
Alan Kwong67a3f792016-11-01 23:16:53 -04003747 return 0;
3748}
3749DEFINE_SDE_DEBUGFS_SEQ_FOPS(sde_crtc_debugfs_state);
3750
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07003751static int _sde_crtc_init_debugfs(struct drm_crtc *crtc)
Clarence Ip8f7366c2016-07-05 12:15:26 -04003752{
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07003753 struct sde_crtc *sde_crtc;
3754 struct sde_kms *sde_kms;
3755
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07003756 static const struct file_operations debugfs_status_fops = {
3757 .open = _sde_debugfs_status_open,
Clarence Ip8f7366c2016-07-05 12:15:26 -04003758 .read = seq_read,
3759 .llseek = seq_lseek,
3760 .release = single_release,
3761 };
Dhaval Patelf9245d62017-03-28 16:24:00 -07003762 static const struct file_operations debugfs_misr_fops = {
3763 .open = simple_open,
3764 .read = _sde_crtc_misr_read,
3765 .write = _sde_crtc_misr_setup,
3766 };
Alan Kwong67a3f792016-11-01 23:16:53 -04003767
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07003768 if (!crtc)
3769 return -EINVAL;
3770 sde_crtc = to_sde_crtc(crtc);
3771
3772 sde_kms = _sde_crtc_get_kms(crtc);
3773 if (!sde_kms)
3774 return -EINVAL;
3775
3776 sde_crtc->debugfs_root = debugfs_create_dir(sde_crtc->name,
Lloyd Atkinson09e64bf2017-04-13 14:09:59 -07003777 crtc->dev->primary->debugfs_root);
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07003778 if (!sde_crtc->debugfs_root)
3779 return -ENOMEM;
3780
3781 /* don't error check these */
Lloyd Atkinson8de415a2017-05-23 11:31:16 -04003782 debugfs_create_file("status", 0400,
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07003783 sde_crtc->debugfs_root,
3784 sde_crtc, &debugfs_status_fops);
Lloyd Atkinson8de415a2017-05-23 11:31:16 -04003785 debugfs_create_file("state", 0600,
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07003786 sde_crtc->debugfs_root,
3787 &sde_crtc->base,
3788 &sde_crtc_debugfs_state_fops);
Lloyd Atkinson8de415a2017-05-23 11:31:16 -04003789 debugfs_create_file("misr_data", 0600, sde_crtc->debugfs_root,
Dhaval Patelf9245d62017-03-28 16:24:00 -07003790 sde_crtc, &debugfs_misr_fops);
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07003791
3792 return 0;
3793}
3794
3795static void _sde_crtc_destroy_debugfs(struct drm_crtc *crtc)
3796{
3797 struct sde_crtc *sde_crtc;
3798
3799 if (!crtc)
3800 return;
3801 sde_crtc = to_sde_crtc(crtc);
3802 debugfs_remove_recursive(sde_crtc->debugfs_root);
Clarence Ip8f7366c2016-07-05 12:15:26 -04003803}
Alan Kwong67a3f792016-11-01 23:16:53 -04003804#else
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07003805static int _sde_crtc_init_debugfs(struct drm_crtc *crtc)
Alan Kwong67a3f792016-11-01 23:16:53 -04003806{
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07003807 return 0;
Alan Kwong67a3f792016-11-01 23:16:53 -04003808}
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07003809
3810static void _sde_crtc_destroy_debugfs(struct drm_crtc *crtc)
3811{
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07003812}
3813#endif /* CONFIG_DEBUG_FS */
3814
3815static int sde_crtc_late_register(struct drm_crtc *crtc)
3816{
3817 return _sde_crtc_init_debugfs(crtc);
3818}
3819
3820static void sde_crtc_early_unregister(struct drm_crtc *crtc)
3821{
3822 _sde_crtc_destroy_debugfs(crtc);
3823}
3824
3825static const struct drm_crtc_funcs sde_crtc_funcs = {
3826 .set_config = drm_atomic_helper_set_config,
3827 .destroy = sde_crtc_destroy,
3828 .page_flip = drm_atomic_helper_page_flip,
3829 .set_property = sde_crtc_set_property,
3830 .atomic_set_property = sde_crtc_atomic_set_property,
3831 .atomic_get_property = sde_crtc_atomic_get_property,
3832 .reset = sde_crtc_reset,
3833 .atomic_duplicate_state = sde_crtc_duplicate_state,
3834 .atomic_destroy_state = sde_crtc_destroy_state,
3835 .late_register = sde_crtc_late_register,
3836 .early_unregister = sde_crtc_early_unregister,
3837};
3838
3839static const struct drm_crtc_helper_funcs sde_crtc_helper_funcs = {
3840 .mode_fixup = sde_crtc_mode_fixup,
3841 .disable = sde_crtc_disable,
3842 .enable = sde_crtc_enable,
3843 .atomic_check = sde_crtc_atomic_check,
3844 .atomic_begin = sde_crtc_atomic_begin,
3845 .atomic_flush = sde_crtc_atomic_flush,
3846};
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07003847
Clarence Ipa18d4832017-03-13 12:35:44 -07003848static void _sde_crtc_event_cb(struct kthread_work *work)
3849{
3850 struct sde_crtc_event *event;
3851 struct sde_crtc *sde_crtc;
3852 unsigned long irq_flags;
3853
3854 if (!work) {
3855 SDE_ERROR("invalid work item\n");
3856 return;
3857 }
3858
3859 event = container_of(work, struct sde_crtc_event, kt_work);
Clarence Ipa18d4832017-03-13 12:35:44 -07003860
3861 /* set sde_crtc to NULL for static work structures */
3862 sde_crtc = event->sde_crtc;
3863 if (!sde_crtc)
3864 return;
3865
Gopikrishnaiah Anandanb6b401f2017-03-14 16:39:49 -07003866 if (event->cb_func)
3867 event->cb_func(&sde_crtc->base, event->usr);
3868
Clarence Ipa18d4832017-03-13 12:35:44 -07003869 spin_lock_irqsave(&sde_crtc->event_lock, irq_flags);
3870 list_add_tail(&event->list, &sde_crtc->event_free_list);
3871 spin_unlock_irqrestore(&sde_crtc->event_lock, irq_flags);
3872}
3873
3874int sde_crtc_event_queue(struct drm_crtc *crtc,
Gopikrishnaiah Anandanb6b401f2017-03-14 16:39:49 -07003875 void (*func)(struct drm_crtc *crtc, void *usr), void *usr)
Clarence Ipa18d4832017-03-13 12:35:44 -07003876{
3877 unsigned long irq_flags;
3878 struct sde_crtc *sde_crtc;
Veera Sundaram Sankaran10ea2bd2017-06-14 14:10:57 -07003879 struct msm_drm_private *priv;
Clarence Ipa18d4832017-03-13 12:35:44 -07003880 struct sde_crtc_event *event = NULL;
Veera Sundaram Sankaran10ea2bd2017-06-14 14:10:57 -07003881 u32 crtc_id;
Clarence Ipa18d4832017-03-13 12:35:44 -07003882
Veera Sundaram Sankaran10ea2bd2017-06-14 14:10:57 -07003883 if (!crtc || !crtc->dev || !crtc->dev->dev_private || !func) {
3884 SDE_ERROR("invalid parameters\n");
Clarence Ipa18d4832017-03-13 12:35:44 -07003885 return -EINVAL;
Veera Sundaram Sankaran10ea2bd2017-06-14 14:10:57 -07003886 }
Clarence Ipa18d4832017-03-13 12:35:44 -07003887 sde_crtc = to_sde_crtc(crtc);
Veera Sundaram Sankaran10ea2bd2017-06-14 14:10:57 -07003888 priv = crtc->dev->dev_private;
3889 crtc_id = drm_crtc_index(crtc);
Clarence Ipa18d4832017-03-13 12:35:44 -07003890
3891 /*
3892 * Obtain an event struct from the private cache. This event
3893 * queue may be called from ISR contexts, so use a private
3894 * cache to avoid calling any memory allocation functions.
3895 */
3896 spin_lock_irqsave(&sde_crtc->event_lock, irq_flags);
3897 if (!list_empty(&sde_crtc->event_free_list)) {
3898 event = list_first_entry(&sde_crtc->event_free_list,
3899 struct sde_crtc_event, list);
3900 list_del_init(&event->list);
3901 }
3902 spin_unlock_irqrestore(&sde_crtc->event_lock, irq_flags);
3903
3904 if (!event)
3905 return -ENOMEM;
3906
3907 /* populate event node */
3908 event->sde_crtc = sde_crtc;
3909 event->cb_func = func;
3910 event->usr = usr;
3911
3912 /* queue new event request */
3913 kthread_init_work(&event->kt_work, _sde_crtc_event_cb);
Veera Sundaram Sankaran10ea2bd2017-06-14 14:10:57 -07003914 kthread_queue_work(&priv->event_thread[crtc_id].worker,
3915 &event->kt_work);
Clarence Ipa18d4832017-03-13 12:35:44 -07003916
3917 return 0;
3918}
3919
3920static int _sde_crtc_init_events(struct sde_crtc *sde_crtc)
3921{
3922 int i, rc = 0;
3923
3924 if (!sde_crtc) {
3925 SDE_ERROR("invalid crtc\n");
3926 return -EINVAL;
3927 }
3928
3929 spin_lock_init(&sde_crtc->event_lock);
3930
3931 INIT_LIST_HEAD(&sde_crtc->event_free_list);
3932 for (i = 0; i < SDE_CRTC_MAX_EVENT_COUNT; ++i)
3933 list_add_tail(&sde_crtc->event_cache[i].list,
3934 &sde_crtc->event_free_list);
3935
Clarence Ipa18d4832017-03-13 12:35:44 -07003936 return rc;
3937}
3938
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04003939/* initialize crtc */
Lloyd Atkinsonac933642016-09-14 11:52:00 -04003940struct drm_crtc *sde_crtc_init(struct drm_device *dev, struct drm_plane *plane)
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07003941{
3942 struct drm_crtc *crtc = NULL;
Clarence Ip8f7366c2016-07-05 12:15:26 -04003943 struct sde_crtc *sde_crtc = NULL;
3944 struct msm_drm_private *priv = NULL;
3945 struct sde_kms *kms = NULL;
Clarence Ipa18d4832017-03-13 12:35:44 -07003946 int i, rc;
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07003947
Clarence Ip8f7366c2016-07-05 12:15:26 -04003948 priv = dev->dev_private;
3949 kms = to_sde_kms(priv->kms);
3950
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07003951 sde_crtc = kzalloc(sizeof(*sde_crtc), GFP_KERNEL);
3952 if (!sde_crtc)
3953 return ERR_PTR(-ENOMEM);
3954
3955 crtc = &sde_crtc->base;
Gopikrishnaiah Anandane0e5e0c2016-05-25 11:05:33 -07003956 crtc->dev = dev;
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07003957
Clarence Ip7f70ce42017-03-20 06:53:46 -07003958 mutex_init(&sde_crtc->crtc_lock);
Alan Kwong628d19e2016-10-31 13:50:13 -04003959 spin_lock_init(&sde_crtc->spin_lock);
3960 atomic_set(&sde_crtc->frame_pending, 0);
3961
Veera Sundaram Sankaran7ee99092017-06-13 11:19:36 -07003962 init_completion(&sde_crtc->frame_done_comp);
3963
Alan Kwong628d19e2016-10-31 13:50:13 -04003964 INIT_LIST_HEAD(&sde_crtc->frame_event_list);
Gopikrishnaiah Anandande2c81b2017-03-15 12:41:29 -07003965 INIT_LIST_HEAD(&sde_crtc->user_event_list);
Alan Kwong628d19e2016-10-31 13:50:13 -04003966 for (i = 0; i < ARRAY_SIZE(sde_crtc->frame_events); i++) {
3967 INIT_LIST_HEAD(&sde_crtc->frame_events[i].list);
3968 list_add(&sde_crtc->frame_events[i].list,
3969 &sde_crtc->frame_event_list);
3970 kthread_init_work(&sde_crtc->frame_events[i].work,
3971 sde_crtc_frame_event_work);
3972 }
3973
Dhaval Patel04c7e8e2016-09-26 20:14:31 -07003974 drm_crtc_init_with_planes(dev, crtc, plane, NULL, &sde_crtc_funcs,
3975 NULL);
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07003976
3977 drm_crtc_helper_add(crtc, &sde_crtc_helper_funcs);
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04003978 plane->crtc = crtc;
3979
Clarence Ip8f7366c2016-07-05 12:15:26 -04003980 /* save user friendly CRTC name for later */
3981 snprintf(sde_crtc->name, SDE_CRTC_NAME_SIZE, "crtc%u", crtc->base.id);
3982
Clarence Ipa18d4832017-03-13 12:35:44 -07003983 /* initialize event handling */
3984 rc = _sde_crtc_init_events(sde_crtc);
3985 if (rc) {
3986 drm_crtc_cleanup(crtc);
3987 kfree(sde_crtc);
3988 return ERR_PTR(rc);
3989 }
3990
Clarence Ip9a74a442016-08-25 18:29:03 -04003991 /* initialize output fence support */
Lloyd Atkinson5d40d312016-09-06 08:34:13 -04003992 sde_fence_init(&sde_crtc->output_fence, sde_crtc->name, crtc->base.id);
Clarence Ip24f80662016-06-13 19:05:32 -04003993
Clarence Ip7a753bb2016-07-07 11:47:44 -04003994 /* create CRTC properties */
3995 msm_property_init(&sde_crtc->property_info, &crtc->base, dev,
3996 priv->crtc_property, sde_crtc->property_data,
3997 CRTC_PROP_COUNT, CRTC_PROP_BLOBCOUNT,
3998 sizeof(struct sde_crtc_state));
3999
Dhaval Patele4a5dda2016-10-13 19:29:30 -07004000 sde_crtc_install_properties(crtc, kms->catalog);
Gopikrishnaiah Anandan703eb902016-10-06 18:43:57 -07004001
4002 /* Install color processing properties */
Gopikrishnaiah Anandane0e5e0c2016-05-25 11:05:33 -07004003 sde_cp_crtc_init(crtc);
Gopikrishnaiah Anandan703eb902016-10-06 18:43:57 -07004004 sde_cp_crtc_install_properties(crtc);
Clarence Ip7a753bb2016-07-07 11:47:44 -04004005
Dhaval Patelec10fad2016-08-22 14:40:48 -07004006 SDE_DEBUG("%s: successfully initialized crtc\n", sde_crtc->name);
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07004007 return crtc;
4008}
Gopikrishnaiah Anandande2c81b2017-03-15 12:41:29 -07004009
Gopikrishnaiah Anandan5154c712017-02-27 17:48:24 -08004010static int _sde_crtc_event_enable(struct sde_kms *kms,
4011 struct drm_crtc *crtc_drm, u32 event)
Gopikrishnaiah Anandande2c81b2017-03-15 12:41:29 -07004012{
Gopikrishnaiah Anandan5154c712017-02-27 17:48:24 -08004013 struct sde_crtc *crtc = NULL;
4014 struct sde_crtc_irq_info *node;
4015 struct msm_drm_private *priv;
4016 unsigned long flags;
4017 bool found = false;
Gopikrishnaiah Anandanb6b401f2017-03-14 16:39:49 -07004018 int ret, i = 0;
Gopikrishnaiah Anandan5154c712017-02-27 17:48:24 -08004019
4020 crtc = to_sde_crtc(crtc_drm);
4021 spin_lock_irqsave(&crtc->spin_lock, flags);
4022 list_for_each_entry(node, &crtc->user_event_list, list) {
4023 if (node->event == event) {
4024 found = true;
4025 break;
4026 }
4027 }
4028 spin_unlock_irqrestore(&crtc->spin_lock, flags);
4029
4030 /* event already enabled */
4031 if (found)
4032 return 0;
4033
Gopikrishnaiah Anandanb6b401f2017-03-14 16:39:49 -07004034 node = NULL;
4035 for (i = 0; i < ARRAY_SIZE(custom_events); i++) {
4036 if (custom_events[i].event == event &&
4037 custom_events[i].func) {
4038 node = kzalloc(sizeof(*node), GFP_KERNEL);
4039 if (!node)
4040 return -ENOMEM;
4041 node->event = event;
4042 INIT_LIST_HEAD(&node->list);
4043 node->func = custom_events[i].func;
4044 node->event = event;
4045 break;
4046 }
4047 }
Gopikrishnaiah Anandan5154c712017-02-27 17:48:24 -08004048
Gopikrishnaiah Anandanb6b401f2017-03-14 16:39:49 -07004049 if (!node) {
Gopikrishnaiah Anandan5154c712017-02-27 17:48:24 -08004050 SDE_ERROR("unsupported event %x\n", event);
Gopikrishnaiah Anandan5154c712017-02-27 17:48:24 -08004051 return -EINVAL;
4052 }
4053
4054 priv = kms->dev->dev_private;
4055 ret = 0;
4056 if (crtc_drm->enabled) {
4057 sde_power_resource_enable(&priv->phandle, kms->core_client,
4058 true);
4059 ret = node->func(crtc_drm, true, &node->irq);
4060 sde_power_resource_enable(&priv->phandle, kms->core_client,
4061 false);
4062 }
4063
4064 if (!ret) {
4065 spin_lock_irqsave(&crtc->spin_lock, flags);
4066 list_add_tail(&node->list, &crtc->user_event_list);
4067 spin_unlock_irqrestore(&crtc->spin_lock, flags);
4068 } else {
4069 kfree(node);
4070 }
4071
4072 return ret;
4073}
4074
4075static int _sde_crtc_event_disable(struct sde_kms *kms,
4076 struct drm_crtc *crtc_drm, u32 event)
4077{
4078 struct sde_crtc *crtc = NULL;
4079 struct sde_crtc_irq_info *node = NULL;
4080 struct msm_drm_private *priv;
4081 unsigned long flags;
4082 bool found = false;
4083 int ret;
4084
4085 crtc = to_sde_crtc(crtc_drm);
4086 spin_lock_irqsave(&crtc->spin_lock, flags);
4087 list_for_each_entry(node, &crtc->user_event_list, list) {
4088 if (node->event == event) {
4089 list_del(&node->list);
4090 found = true;
4091 break;
4092 }
4093 }
4094 spin_unlock_irqrestore(&crtc->spin_lock, flags);
4095
4096 /* event already disabled */
4097 if (!found)
4098 return 0;
4099
4100 /**
4101 * crtc is disabled interrupts are cleared remove from the list,
4102 * no need to disable/de-register.
4103 */
4104 if (!crtc_drm->enabled) {
4105 kfree(node);
4106 return 0;
4107 }
4108 priv = kms->dev->dev_private;
4109 sde_power_resource_enable(&priv->phandle, kms->core_client, true);
4110 ret = node->func(crtc_drm, false, &node->irq);
4111 sde_power_resource_enable(&priv->phandle, kms->core_client, false);
4112 return ret;
4113}
4114
4115int sde_crtc_register_custom_event(struct sde_kms *kms,
4116 struct drm_crtc *crtc_drm, u32 event, bool en)
4117{
4118 struct sde_crtc *crtc = NULL;
4119 int ret;
4120
4121 crtc = to_sde_crtc(crtc_drm);
4122 if (!crtc || !kms || !kms->dev) {
4123 DRM_ERROR("invalid sde_crtc %pK kms %pK dev %pK\n", crtc,
4124 kms, ((kms) ? (kms->dev) : NULL));
4125 return -EINVAL;
4126 }
4127
4128 if (en)
4129 ret = _sde_crtc_event_enable(kms, crtc_drm, event);
4130 else
4131 ret = _sde_crtc_event_disable(kms, crtc_drm, event);
4132
4133 return ret;
Gopikrishnaiah Anandande2c81b2017-03-15 12:41:29 -07004134}
Gopikrishnaiah Anandan84b4f672017-04-26 10:28:51 -07004135
4136static int sde_crtc_power_interrupt_handler(struct drm_crtc *crtc_drm,
4137 bool en, struct sde_irq_callback *irq)
4138{
4139 return 0;
4140}
Benjamin Chan90139102017-06-21 16:00:39 -04004141
4142static int sde_crtc_pm_event_handler(struct drm_crtc *crtc, bool en,
4143 struct sde_irq_callback *noirq)
4144{
4145 /*
4146 * IRQ object noirq is not being used here since there is
4147 * no crtc irq from pm event.
4148 */
4149 return 0;
4150}