blob: 23f23b025ec2cd202edd6cf60c47211ff3e98076 [file] [log] [blame]
Dhaval Patel14d46ce2017-01-17 16:28:12 -08001/*
2 * Copyright (c) 2014-2017 The Linux Foundation. All rights reserved.
3 * Copyright (C) 2013 Red Hat
4 * Author: Rob Clark <robdclark@gmail.com>
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07005 *
Dhaval Patel14d46ce2017-01-17 16:28:12 -08006 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published by
8 * the Free Software Foundation.
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07009 *
Dhaval Patel14d46ce2017-01-17 16:28:12 -080010 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program. If not, see <http://www.gnu.org/licenses/>.
Narendra Muppalla1b0b3352015-09-29 10:16:51 -070017 */
18
Clarence Ipd9f9fa62016-09-09 13:42:32 -040019#define pr_fmt(fmt) "[drm:%s:%d] " fmt, __func__, __LINE__
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -040020#include <linux/sort.h>
Clarence Ip8f7366c2016-07-05 12:15:26 -040021#include <linux/debugfs.h>
Clarence Ipcae1bb62016-07-07 12:07:13 -040022#include <linux/ktime.h>
Clarence Ip4c1d9772016-06-26 09:35:38 -040023#include <uapi/drm/sde_drm.h>
Narendra Muppalla1b0b3352015-09-29 10:16:51 -070024#include <drm/drm_mode.h>
25#include <drm/drm_crtc.h>
26#include <drm/drm_crtc_helper.h>
27#include <drm/drm_flip_work.h>
28
29#include "sde_kms.h"
30#include "sde_hw_lm.h"
Clarence Ipc475b082016-06-26 09:27:23 -040031#include "sde_hw_ctl.h"
Abhijit Kulkarni40e38162016-06-26 22:12:09 -040032#include "sde_crtc.h"
Alan Kwong83285fb2016-10-21 20:51:17 -040033#include "sde_plane.h"
Gopikrishnaiah Anandane0e5e0c2016-05-25 11:05:33 -070034#include "sde_color_processing.h"
Alan Kwong83285fb2016-10-21 20:51:17 -040035#include "sde_encoder.h"
36#include "sde_connector.h"
Alan Kwong67a3f792016-11-01 23:16:53 -040037#include "sde_power_handle.h"
Alan Kwong9aa061c2016-11-06 21:17:12 -050038#include "sde_core_perf.h"
Narendra Muppalla77b32932017-05-10 13:53:11 -070039#include "sde_trace.h"
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -040040
Gopikrishnaiah Anandande2c81b2017-03-15 12:41:29 -070041struct sde_crtc_irq_info {
42 struct sde_irq_callback irq;
43 u32 event;
44 int (*func)(struct drm_crtc *crtc, bool en,
45 struct sde_irq_callback *irq);
46 struct list_head list;
47};
48
Gopikrishnaiah Anandanb6b401f2017-03-14 16:39:49 -070049struct sde_crtc_custom_events {
50 u32 event;
51 int (*func)(struct drm_crtc *crtc, bool en,
52 struct sde_irq_callback *irq);
53};
54
Gopikrishnaiah Anandan84b4f672017-04-26 10:28:51 -070055static int sde_crtc_power_interrupt_handler(struct drm_crtc *crtc_drm,
56 bool en, struct sde_irq_callback *ad_irq);
57
Gopikrishnaiah Anandanb6b401f2017-03-14 16:39:49 -070058static struct sde_crtc_custom_events custom_events[] = {
Gopikrishnaiah Anandan84b4f672017-04-26 10:28:51 -070059 {DRM_EVENT_AD_BACKLIGHT, sde_cp_ad_interrupt},
60 {DRM_EVENT_CRTC_POWER, sde_crtc_power_interrupt_handler}
Gopikrishnaiah Anandanb6b401f2017-03-14 16:39:49 -070061};
62
Clarence Ipcae1bb62016-07-07 12:07:13 -040063/* default input fence timeout, in ms */
64#define SDE_CRTC_INPUT_FENCE_TIMEOUT 2000
65
Dhaval Patel4e574842016-08-23 15:11:37 -070066/*
67 * The default input fence timeout is 2 seconds while max allowed
68 * range is 10 seconds. Any value above 10 seconds adds glitches beyond
69 * tolerance limit.
70 */
71#define SDE_CRTC_MAX_INPUT_FENCE_TIMEOUT 10000
72
Dhaval Patel48c76022016-09-01 17:51:23 -070073/* layer mixer index on sde_crtc */
74#define LEFT_MIXER 0
75#define RIGHT_MIXER 1
76
Dhaval Patelf9245d62017-03-28 16:24:00 -070077#define MISR_BUFF_SIZE 256
78
Lloyd Atkinson4f1c8692016-09-14 14:04:25 -040079static inline struct sde_kms *_sde_crtc_get_kms(struct drm_crtc *crtc)
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -040080{
Clarence Ip7f70ce42017-03-20 06:53:46 -070081 struct msm_drm_private *priv;
82
83 if (!crtc || !crtc->dev || !crtc->dev->dev_private) {
84 SDE_ERROR("invalid crtc\n");
85 return NULL;
86 }
87 priv = crtc->dev->dev_private;
88 if (!priv || !priv->kms) {
89 SDE_ERROR("invalid kms\n");
90 return NULL;
91 }
Abhijit Kulkarni40e38162016-06-26 22:12:09 -040092
Ben Chan78647cd2016-06-26 22:02:47 -040093 return to_sde_kms(priv->kms);
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -040094}
95
Dhaval Patelf9245d62017-03-28 16:24:00 -070096static inline int _sde_crtc_power_enable(struct sde_crtc *sde_crtc, bool enable)
97{
98 struct drm_crtc *crtc;
99 struct msm_drm_private *priv;
100 struct sde_kms *sde_kms;
101
102 if (!sde_crtc) {
103 SDE_ERROR("invalid sde crtc\n");
104 return -EINVAL;
105 }
106
107 crtc = &sde_crtc->base;
108 if (!crtc->dev || !crtc->dev->dev_private) {
109 SDE_ERROR("invalid drm device\n");
110 return -EINVAL;
111 }
112
113 priv = crtc->dev->dev_private;
114 if (!priv->kms) {
115 SDE_ERROR("invalid kms\n");
116 return -EINVAL;
117 }
118
119 sde_kms = to_sde_kms(priv->kms);
120
121 return sde_power_resource_enable(&priv->phandle, sde_kms->core_client,
122 enable);
123}
124
Alan Kwongcdb2f282017-03-18 13:42:06 -0700125/**
126 * _sde_crtc_rp_to_crtc - get crtc from resource pool object
127 * @rp: Pointer to resource pool
128 * return: Pointer to drm crtc if success; null otherwise
129 */
130static struct drm_crtc *_sde_crtc_rp_to_crtc(struct sde_crtc_respool *rp)
131{
132 if (!rp)
133 return NULL;
134
135 return container_of(rp, struct sde_crtc_state, rp)->base.crtc;
136}
137
138/**
139 * _sde_crtc_rp_reclaim - reclaim unused, or all if forced, resources in pool
140 * @rp: Pointer to resource pool
141 * @force: True to reclaim all resources; otherwise, reclaim only unused ones
142 * return: None
143 */
144static void _sde_crtc_rp_reclaim(struct sde_crtc_respool *rp, bool force)
145{
146 struct sde_crtc_res *res, *next;
147 struct drm_crtc *crtc;
148
149 crtc = _sde_crtc_rp_to_crtc(rp);
150 if (!crtc) {
151 SDE_ERROR("invalid crtc\n");
152 return;
153 }
154
155 SDE_DEBUG("crtc%d.%u %s\n", crtc->base.id, rp->sequence_id,
156 force ? "destroy" : "free_unused");
157
158 list_for_each_entry_safe(res, next, &rp->res_list, list) {
159 if (!force && !(res->flags & SDE_CRTC_RES_FLAG_FREE))
160 continue;
161 SDE_DEBUG("crtc%d.%u reclaim res:0x%x/0x%llx/%pK/%d\n",
162 crtc->base.id, rp->sequence_id,
163 res->type, res->tag, res->val,
164 atomic_read(&res->refcount));
165 list_del(&res->list);
166 if (res->ops.put)
167 res->ops.put(res->val);
168 kfree(res);
169 }
170}
171
172/**
173 * _sde_crtc_rp_free_unused - free unused resource in pool
174 * @rp: Pointer to resource pool
175 * return: none
176 */
177static void _sde_crtc_rp_free_unused(struct sde_crtc_respool *rp)
178{
179 _sde_crtc_rp_reclaim(rp, false);
180}
181
182/**
183 * _sde_crtc_rp_destroy - destroy resource pool
184 * @rp: Pointer to resource pool
185 * return: None
186 */
187static void _sde_crtc_rp_destroy(struct sde_crtc_respool *rp)
188{
189 _sde_crtc_rp_reclaim(rp, true);
190}
191
192/**
193 * _sde_crtc_hw_blk_get - get callback for hardware block
194 * @val: Resource handle
195 * @type: Resource type
196 * @tag: Search tag for given resource
197 * return: Resource handle
198 */
199static void *_sde_crtc_hw_blk_get(void *val, u32 type, u64 tag)
200{
201 SDE_DEBUG("res:%d/0x%llx/%pK\n", type, tag, val);
202 return sde_hw_blk_get(val, type, tag);
203}
204
205/**
206 * _sde_crtc_hw_blk_put - put callback for hardware block
207 * @val: Resource handle
208 * return: None
209 */
210static void _sde_crtc_hw_blk_put(void *val)
211{
212 SDE_DEBUG("res://%pK\n", val);
213 sde_hw_blk_put(val);
214}
215
216/**
217 * _sde_crtc_rp_duplicate - duplicate resource pool and reset reference count
218 * @rp: Pointer to original resource pool
219 * @dup_rp: Pointer to duplicated resource pool
220 * return: None
221 */
222static void _sde_crtc_rp_duplicate(struct sde_crtc_respool *rp,
223 struct sde_crtc_respool *dup_rp)
224{
225 struct sde_crtc_res *res, *dup_res;
226 struct drm_crtc *crtc;
227
228 if (!rp || !dup_rp) {
229 SDE_ERROR("invalid resource pool\n");
230 return;
231 }
232
233 crtc = _sde_crtc_rp_to_crtc(rp);
234 if (!crtc) {
235 SDE_ERROR("invalid crtc\n");
236 return;
237 }
238
239 SDE_DEBUG("crtc%d.%u duplicate\n", crtc->base.id, rp->sequence_id);
240
241 dup_rp->sequence_id = rp->sequence_id + 1;
242 INIT_LIST_HEAD(&dup_rp->res_list);
243 dup_rp->ops = rp->ops;
244 list_for_each_entry(res, &rp->res_list, list) {
245 dup_res = kzalloc(sizeof(struct sde_crtc_res), GFP_KERNEL);
246 if (!dup_res)
247 return;
248 INIT_LIST_HEAD(&dup_res->list);
249 atomic_set(&dup_res->refcount, 0);
250 dup_res->type = res->type;
251 dup_res->tag = res->tag;
252 dup_res->val = res->val;
253 dup_res->ops = res->ops;
254 dup_res->flags = SDE_CRTC_RES_FLAG_FREE;
255 SDE_DEBUG("crtc%d.%u dup res:0x%x/0x%llx/%pK/%d\n",
256 crtc->base.id, dup_rp->sequence_id,
257 dup_res->type, dup_res->tag, dup_res->val,
258 atomic_read(&dup_res->refcount));
259 list_add_tail(&dup_res->list, &dup_rp->res_list);
260 if (dup_res->ops.get)
261 dup_res->ops.get(dup_res->val, 0, -1);
262 }
263}
264
265/**
266 * _sde_crtc_rp_reset - reset resource pool after allocation
267 * @rp: Pointer to original resource pool
268 * return: None
269 */
270static void _sde_crtc_rp_reset(struct sde_crtc_respool *rp)
271{
272 if (!rp) {
273 SDE_ERROR("invalid resource pool\n");
274 return;
275 }
276
277 rp->sequence_id = 0;
278 INIT_LIST_HEAD(&rp->res_list);
279 rp->ops.get = _sde_crtc_hw_blk_get;
280 rp->ops.put = _sde_crtc_hw_blk_put;
281}
282
283/**
284 * _sde_crtc_rp_add - add given resource to resource pool
285 * @rp: Pointer to original resource pool
286 * @type: Resource type
287 * @tag: Search tag for given resource
288 * @val: Resource handle
289 * @ops: Resource callback operations
290 * return: 0 if success; error code otherwise
291 */
292static int _sde_crtc_rp_add(struct sde_crtc_respool *rp, u32 type, u64 tag,
293 void *val, struct sde_crtc_res_ops *ops)
294{
295 struct sde_crtc_res *res;
296 struct drm_crtc *crtc;
297
298 if (!rp || !ops) {
299 SDE_ERROR("invalid resource pool/ops\n");
300 return -EINVAL;
301 }
302
303 crtc = _sde_crtc_rp_to_crtc(rp);
304 if (!crtc) {
305 SDE_ERROR("invalid crtc\n");
306 return -EINVAL;
307 }
308
309 list_for_each_entry(res, &rp->res_list, list) {
310 if (res->type != type || res->tag != tag)
311 continue;
312 SDE_ERROR("crtc%d.%u already exist res:0x%x/0x%llx/%pK/%d\n",
313 crtc->base.id, rp->sequence_id,
314 res->type, res->tag, res->val,
315 atomic_read(&res->refcount));
316 return -EEXIST;
317 }
318 res = kzalloc(sizeof(struct sde_crtc_res), GFP_KERNEL);
319 if (!res)
320 return -ENOMEM;
321 INIT_LIST_HEAD(&res->list);
322 atomic_set(&res->refcount, 1);
323 res->type = type;
324 res->tag = tag;
325 res->val = val;
326 res->ops = *ops;
327 list_add_tail(&res->list, &rp->res_list);
328 SDE_DEBUG("crtc%d.%u added res:0x%x/0x%llx\n",
329 crtc->base.id, rp->sequence_id, type, tag);
330 return 0;
331}
332
333/**
334 * _sde_crtc_rp_get - lookup the resource from given resource pool and obtain
335 * if available; otherwise, obtain resource from global pool
336 * @rp: Pointer to original resource pool
337 * @type: Resource type
338 * @tag: Search tag for given resource
339 * return: Resource handle if success; pointer error or null otherwise
340 */
341static void *_sde_crtc_rp_get(struct sde_crtc_respool *rp, u32 type, u64 tag)
342{
343 struct sde_crtc_res *res;
344 void *val = NULL;
345 int rc;
346 struct drm_crtc *crtc;
347
348 if (!rp) {
349 SDE_ERROR("invalid resource pool\n");
350 return NULL;
351 }
352
353 crtc = _sde_crtc_rp_to_crtc(rp);
354 if (!crtc) {
355 SDE_ERROR("invalid crtc\n");
356 return NULL;
357 }
358
359 list_for_each_entry(res, &rp->res_list, list) {
360 if (res->type != type || res->tag != tag)
361 continue;
362 SDE_DEBUG("crtc%d.%u found res:0x%x/0x%llx/%pK/%d\n",
363 crtc->base.id, rp->sequence_id,
364 res->type, res->tag, res->val,
365 atomic_read(&res->refcount));
366 atomic_inc(&res->refcount);
367 res->flags &= ~SDE_CRTC_RES_FLAG_FREE;
368 return res->val;
369 }
370 list_for_each_entry(res, &rp->res_list, list) {
371 if (res->type != type || !(res->flags & SDE_CRTC_RES_FLAG_FREE))
372 continue;
373 SDE_DEBUG("crtc%d.%u retag res:0x%x/0x%llx/%pK/%d\n",
374 crtc->base.id, rp->sequence_id,
375 res->type, res->tag, res->val,
376 atomic_read(&res->refcount));
377 atomic_inc(&res->refcount);
378 res->tag = tag;
379 res->flags &= ~SDE_CRTC_RES_FLAG_FREE;
380 return res->val;
381 }
382 if (rp->ops.get)
383 val = rp->ops.get(NULL, type, -1);
384 if (IS_ERR_OR_NULL(val)) {
Alan Kwong42e35052017-05-05 06:52:51 -0700385 SDE_DEBUG("crtc%d.%u failed to get res:0x%x//\n",
Alan Kwongcdb2f282017-03-18 13:42:06 -0700386 crtc->base.id, rp->sequence_id, type);
387 return NULL;
388 }
389 rc = _sde_crtc_rp_add(rp, type, tag, val, &rp->ops);
390 if (rc) {
391 SDE_ERROR("crtc%d.%u failed to add res:0x%x/0x%llx\n",
392 crtc->base.id, rp->sequence_id, type, tag);
393 if (rp->ops.put)
394 rp->ops.put(val);
395 val = NULL;
396 }
397 return val;
398}
399
400/**
401 * _sde_crtc_rp_put - return given resource to resource pool
402 * @rp: Pointer to original resource pool
403 * @type: Resource type
404 * @tag: Search tag for given resource
405 * return: None
406 */
407static void _sde_crtc_rp_put(struct sde_crtc_respool *rp, u32 type, u64 tag)
408{
409 struct sde_crtc_res *res, *next;
410 struct drm_crtc *crtc;
411
412 if (!rp) {
413 SDE_ERROR("invalid resource pool\n");
414 return;
415 }
416
417 crtc = _sde_crtc_rp_to_crtc(rp);
418 if (!crtc) {
419 SDE_ERROR("invalid crtc\n");
420 return;
421 }
422
423 list_for_each_entry_safe(res, next, &rp->res_list, list) {
424 if (res->type != type || res->tag != tag)
425 continue;
426 SDE_DEBUG("crtc%d.%u found res:0x%x/0x%llx/%pK/%d\n",
427 crtc->base.id, rp->sequence_id,
428 res->type, res->tag, res->val,
429 atomic_read(&res->refcount));
430 if (res->flags & SDE_CRTC_RES_FLAG_FREE)
431 SDE_ERROR(
432 "crtc%d.%u already free res:0x%x/0x%llx/%pK/%d\n",
433 crtc->base.id, rp->sequence_id,
434 res->type, res->tag, res->val,
435 atomic_read(&res->refcount));
436 else if (atomic_dec_return(&res->refcount) == 0)
437 res->flags |= SDE_CRTC_RES_FLAG_FREE;
438
439 return;
440 }
441 SDE_ERROR("crtc%d.%u not found res:0x%x/0x%llx\n",
442 crtc->base.id, rp->sequence_id, type, tag);
443}
444
445int sde_crtc_res_add(struct drm_crtc_state *state, u32 type, u64 tag,
446 void *val, struct sde_crtc_res_ops *ops)
447{
448 struct sde_crtc_respool *rp;
449
450 if (!state) {
451 SDE_ERROR("invalid parameters\n");
452 return -EINVAL;
453 }
454
455 rp = &to_sde_crtc_state(state)->rp;
456 return _sde_crtc_rp_add(rp, type, tag, val, ops);
457}
458
459void *sde_crtc_res_get(struct drm_crtc_state *state, u32 type, u64 tag)
460{
461 struct sde_crtc_respool *rp;
462 void *val;
463
464 if (!state) {
465 SDE_ERROR("invalid parameters\n");
466 return NULL;
467 }
468
469 rp = &to_sde_crtc_state(state)->rp;
470 val = _sde_crtc_rp_get(rp, type, tag);
471 if (IS_ERR(val)) {
472 SDE_ERROR("failed to get res type:0x%x:0x%llx\n",
473 type, tag);
474 return NULL;
475 }
476
477 return val;
478}
479
480void sde_crtc_res_put(struct drm_crtc_state *state, u32 type, u64 tag)
481{
482 struct sde_crtc_respool *rp;
483
484 if (!state) {
485 SDE_ERROR("invalid parameters\n");
486 return;
487 }
488
489 rp = &to_sde_crtc_state(state)->rp;
490 _sde_crtc_rp_put(rp, type, tag);
491}
492
Clarence Ipa18d4832017-03-13 12:35:44 -0700493static void _sde_crtc_deinit_events(struct sde_crtc *sde_crtc)
494{
495 if (!sde_crtc)
496 return;
497
498 if (sde_crtc->event_thread) {
499 kthread_flush_worker(&sde_crtc->event_worker);
500 kthread_stop(sde_crtc->event_thread);
501 sde_crtc->event_thread = NULL;
502 }
503}
504
Narendra Muppalla1b0b3352015-09-29 10:16:51 -0700505static void sde_crtc_destroy(struct drm_crtc *crtc)
506{
507 struct sde_crtc *sde_crtc = to_sde_crtc(crtc);
508
Lloyd Atkinson4f1c8692016-09-14 14:04:25 -0400509 SDE_DEBUG("\n");
Clarence Ip7a753bb2016-07-07 11:47:44 -0400510
511 if (!crtc)
512 return;
513
Dhaval Patele4a5dda2016-10-13 19:29:30 -0700514 if (sde_crtc->blob_info)
515 drm_property_unreference_blob(sde_crtc->blob_info);
Clarence Ip7a753bb2016-07-07 11:47:44 -0400516 msm_property_destroy(&sde_crtc->property_info);
Gopikrishnaiah Anandane0e5e0c2016-05-25 11:05:33 -0700517 sde_cp_crtc_destroy_properties(crtc);
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -0700518
Clarence Ip24f80662016-06-13 19:05:32 -0400519 sde_fence_deinit(&sde_crtc->output_fence);
Clarence Ipa18d4832017-03-13 12:35:44 -0700520 _sde_crtc_deinit_events(sde_crtc);
Clarence Ip7a753bb2016-07-07 11:47:44 -0400521
Narendra Muppalla1b0b3352015-09-29 10:16:51 -0700522 drm_crtc_cleanup(crtc);
Clarence Ip7f70ce42017-03-20 06:53:46 -0700523 mutex_destroy(&sde_crtc->crtc_lock);
Narendra Muppalla1b0b3352015-09-29 10:16:51 -0700524 kfree(sde_crtc);
525}
526
Narendra Muppalla1b0b3352015-09-29 10:16:51 -0700527static bool sde_crtc_mode_fixup(struct drm_crtc *crtc,
528 const struct drm_display_mode *mode,
529 struct drm_display_mode *adjusted_mode)
530{
Lloyd Atkinson4f1c8692016-09-14 14:04:25 -0400531 SDE_DEBUG("\n");
Lloyd Atkinsonaf7952d2016-06-26 22:41:26 -0400532
Lloyd Atkinson4f1c8692016-09-14 14:04:25 -0400533 if (msm_is_mode_seamless(adjusted_mode) &&
534 (!crtc->enabled || crtc->state->active_changed)) {
535 SDE_ERROR("crtc state prevents seamless transition\n");
536 return false;
Lloyd Atkinsonaf7952d2016-06-26 22:41:26 -0400537 }
538
Narendra Muppalla1b0b3352015-09-29 10:16:51 -0700539 return true;
540}
541
Dhaval Patel48c76022016-09-01 17:51:23 -0700542static void _sde_crtc_setup_blend_cfg(struct sde_crtc_mixer *mixer,
543 struct sde_plane_state *pstate, struct sde_format *format)
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -0400544{
Dhaval Patel48c76022016-09-01 17:51:23 -0700545 uint32_t blend_op, fg_alpha, bg_alpha;
546 uint32_t blend_type;
Dhaval Patel44f12472016-08-29 12:19:47 -0700547 struct sde_hw_mixer *lm = mixer->hw_lm;
548
Dhaval Patel48c76022016-09-01 17:51:23 -0700549 /* default to opaque blending */
550 fg_alpha = sde_plane_get_property(pstate, PLANE_PROP_ALPHA);
551 bg_alpha = 0xFF - fg_alpha;
552 blend_op = SDE_BLEND_FG_ALPHA_FG_CONST | SDE_BLEND_BG_ALPHA_BG_CONST;
553 blend_type = sde_plane_get_property(pstate, PLANE_PROP_BLEND_OP);
Dhaval Patel44f12472016-08-29 12:19:47 -0700554
Dhaval Patel48c76022016-09-01 17:51:23 -0700555 SDE_DEBUG("blend type:0x%x blend alpha:0x%x\n", blend_type, fg_alpha);
556
557 switch (blend_type) {
558
559 case SDE_DRM_BLEND_OP_OPAQUE:
560 blend_op = SDE_BLEND_FG_ALPHA_FG_CONST |
561 SDE_BLEND_BG_ALPHA_BG_CONST;
562 break;
563
564 case SDE_DRM_BLEND_OP_PREMULTIPLIED:
565 if (format->alpha_enable) {
566 blend_op = SDE_BLEND_FG_ALPHA_FG_CONST |
567 SDE_BLEND_BG_ALPHA_FG_PIXEL;
568 if (fg_alpha != 0xff) {
569 bg_alpha = fg_alpha;
570 blend_op |= SDE_BLEND_BG_MOD_ALPHA |
571 SDE_BLEND_BG_INV_MOD_ALPHA;
572 } else {
573 blend_op |= SDE_BLEND_BG_INV_ALPHA;
574 }
575 }
576 break;
577
578 case SDE_DRM_BLEND_OP_COVERAGE:
579 if (format->alpha_enable) {
580 blend_op = SDE_BLEND_FG_ALPHA_FG_PIXEL |
581 SDE_BLEND_BG_ALPHA_FG_PIXEL;
582 if (fg_alpha != 0xff) {
583 bg_alpha = fg_alpha;
584 blend_op |= SDE_BLEND_FG_MOD_ALPHA |
585 SDE_BLEND_FG_INV_MOD_ALPHA |
586 SDE_BLEND_BG_MOD_ALPHA |
587 SDE_BLEND_BG_INV_MOD_ALPHA;
588 } else {
589 blend_op |= SDE_BLEND_BG_INV_ALPHA;
590 }
591 }
592 break;
593 default:
594 /* do nothing */
595 break;
Clarence Ipd9f9fa62016-09-09 13:42:32 -0400596 }
Dhaval Patel48c76022016-09-01 17:51:23 -0700597
598 lm->ops.setup_blend_config(lm, pstate->stage, fg_alpha,
599 bg_alpha, blend_op);
Dhaval Patel6c666622017-03-21 23:02:59 -0700600 SDE_DEBUG(
601 "format: %4.4s, alpha_enable %u fg alpha:0x%x bg alpha:0x%x blend_op:0x%x\n",
602 (char *) &format->base.pixel_format,
Dhaval Patel48c76022016-09-01 17:51:23 -0700603 format->alpha_enable, fg_alpha, bg_alpha, blend_op);
604}
605
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -0800606static void _sde_crtc_setup_dim_layer_cfg(struct drm_crtc *crtc,
607 struct sde_crtc *sde_crtc, struct sde_crtc_mixer *mixer,
608 struct sde_hw_dim_layer *dim_layer)
609{
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -0500610 struct sde_crtc_state *cstate;
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -0800611 struct sde_hw_mixer *lm;
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -0800612 struct sde_hw_dim_layer split_dim_layer;
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -0800613 int i;
614
615 if (!dim_layer->rect.w || !dim_layer->rect.h) {
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -0700616 SDE_DEBUG("empty dim_layer\n");
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -0800617 return;
618 }
619
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -0500620 cstate = to_sde_crtc_state(crtc->state);
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -0800621
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -0700622 SDE_DEBUG("dim_layer - flags:%d, stage:%d\n",
623 dim_layer->flags, dim_layer->stage);
624
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -0800625 split_dim_layer.stage = dim_layer->stage;
626 split_dim_layer.color_fill = dim_layer->color_fill;
627
628 /*
629 * traverse through the layer mixers attached to crtc and find the
630 * intersecting dim layer rect in each LM and program accordingly.
631 */
632 for (i = 0; i < sde_crtc->num_mixers; i++) {
633 split_dim_layer.flags = dim_layer->flags;
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -0800634
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -0500635 sde_kms_rect_intersect(&cstate->lm_bounds[i], &dim_layer->rect,
Lloyd Atkinsone0e11e22017-01-17 12:08:48 -0500636 &split_dim_layer.rect);
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -0500637 if (sde_kms_rect_is_null(&split_dim_layer.rect)) {
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -0800638 /*
639 * no extra programming required for non-intersecting
640 * layer mixers with INCLUSIVE dim layer
641 */
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -0500642 if (split_dim_layer.flags & SDE_DRM_DIM_LAYER_INCLUSIVE)
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -0800643 continue;
644
645 /*
646 * program the other non-intersecting layer mixers with
647 * INCLUSIVE dim layer of full size for uniformity
648 * with EXCLUSIVE dim layer config.
649 */
650 split_dim_layer.flags &= ~SDE_DRM_DIM_LAYER_EXCLUSIVE;
651 split_dim_layer.flags |= SDE_DRM_DIM_LAYER_INCLUSIVE;
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -0500652 memcpy(&split_dim_layer.rect, &cstate->lm_bounds[i],
653 sizeof(split_dim_layer.rect));
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -0800654
655 } else {
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -0500656 split_dim_layer.rect.x =
657 split_dim_layer.rect.x -
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -0700658 cstate->lm_bounds[i].x;
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -0800659 }
660
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -0700661 SDE_DEBUG("split_dim_layer - LM:%d, rect:{%d,%d,%d,%d}}\n",
662 i, split_dim_layer.rect.x, split_dim_layer.rect.y,
663 split_dim_layer.rect.w, split_dim_layer.rect.h);
664
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -0800665 lm = mixer[i].hw_lm;
666 mixer[i].mixer_op_mode |= 1 << split_dim_layer.stage;
667 lm->ops.setup_dim_layer(lm, &split_dim_layer);
668 }
669}
670
Lloyd Atkinson8ba47032017-03-22 17:13:32 -0400671void sde_crtc_get_crtc_roi(struct drm_crtc_state *state,
672 const struct sde_rect **crtc_roi)
673{
674 struct sde_crtc_state *crtc_state;
675
676 if (!state || !crtc_roi)
677 return;
678
679 crtc_state = to_sde_crtc_state(state);
680 *crtc_roi = &crtc_state->crtc_roi;
681}
682
683static int _sde_crtc_set_roi_v1(struct drm_crtc_state *state,
684 void *usr_ptr)
685{
686 struct drm_crtc *crtc;
687 struct sde_crtc_state *cstate;
688 struct sde_drm_roi_v1 roi_v1;
689 int i;
690
691 if (!state) {
692 SDE_ERROR("invalid args\n");
693 return -EINVAL;
694 }
695
696 cstate = to_sde_crtc_state(state);
697 crtc = cstate->base.crtc;
698
699 memset(&cstate->user_roi_list, 0, sizeof(cstate->user_roi_list));
700
701 if (!usr_ptr) {
702 SDE_DEBUG("crtc%d: rois cleared\n", DRMID(crtc));
703 return 0;
704 }
705
706 if (copy_from_user(&roi_v1, usr_ptr, sizeof(roi_v1))) {
707 SDE_ERROR("crtc%d: failed to copy roi_v1 data\n", DRMID(crtc));
708 return -EINVAL;
709 }
710
711 SDE_DEBUG("crtc%d: num_rects %d\n", DRMID(crtc), roi_v1.num_rects);
712
713 if (roi_v1.num_rects == 0) {
714 SDE_DEBUG("crtc%d: rois cleared\n", DRMID(crtc));
715 return 0;
716 }
717
718 if (roi_v1.num_rects > SDE_MAX_ROI_V1) {
719 SDE_ERROR("crtc%d: too many rects specified: %d\n", DRMID(crtc),
720 roi_v1.num_rects);
721 return -EINVAL;
722 }
723
724 cstate->user_roi_list.num_rects = roi_v1.num_rects;
725 for (i = 0; i < roi_v1.num_rects; ++i) {
726 cstate->user_roi_list.roi[i] = roi_v1.roi[i];
727 SDE_DEBUG("crtc%d: roi%d: roi (%d,%d) (%d,%d)\n",
728 DRMID(crtc), i,
729 cstate->user_roi_list.roi[i].x1,
730 cstate->user_roi_list.roi[i].y1,
731 cstate->user_roi_list.roi[i].x2,
732 cstate->user_roi_list.roi[i].y2);
733 }
734
735 return 0;
736}
737
738static int _sde_crtc_set_crtc_roi(struct drm_crtc *crtc,
739 struct drm_crtc_state *state)
740{
741 struct drm_connector *conn;
742 struct drm_connector_state *conn_state;
743 struct sde_crtc *sde_crtc;
744 struct sde_crtc_state *crtc_state;
745 struct sde_rect *crtc_roi;
Lloyd Atkinson8ba47032017-03-22 17:13:32 -0400746 int i, num_attached_conns = 0;
747
748 if (!crtc || !state)
749 return -EINVAL;
750
751 sde_crtc = to_sde_crtc(crtc);
752 crtc_state = to_sde_crtc_state(state);
753 crtc_roi = &crtc_state->crtc_roi;
754
Lloyd Atkinson8ba47032017-03-22 17:13:32 -0400755 for_each_connector_in_state(state->state, conn, conn_state, i) {
756 struct sde_connector_state *sde_conn_state;
757
758 if (!conn_state || conn_state->crtc != crtc)
759 continue;
760
761 if (num_attached_conns) {
762 SDE_ERROR(
763 "crtc%d: unsupported: roi on crtc w/ >1 connectors\n",
764 DRMID(crtc));
765 return -EINVAL;
766 }
767 ++num_attached_conns;
768
769 sde_conn_state = to_sde_connector_state(conn_state);
770
771 if (memcmp(&sde_conn_state->rois, &crtc_state->user_roi_list,
772 sizeof(crtc_state->user_roi_list))) {
773 SDE_ERROR("%s: crtc -> conn roi scaling unsupported\n",
774 sde_crtc->name);
775 return -EINVAL;
776 }
777 }
778
Lloyd Atkinsonc2baf412017-04-19 17:53:09 -0400779 sde_kms_rect_merge_rectangles(&crtc_state->user_roi_list, crtc_roi);
Lloyd Atkinson8ba47032017-03-22 17:13:32 -0400780
781 SDE_DEBUG("%s: crtc roi (%d,%d,%d,%d)\n", sde_crtc->name,
782 crtc_roi->x, crtc_roi->y, crtc_roi->w, crtc_roi->h);
783
784 return 0;
785}
786
Lloyd Atkinson77382202017-02-01 14:59:43 -0500787static int _sde_crtc_check_autorefresh(struct drm_crtc *crtc,
788 struct drm_crtc_state *state)
789{
790 struct sde_crtc *sde_crtc;
791 struct sde_crtc_state *crtc_state;
792 struct drm_connector *conn;
793 struct drm_connector_state *conn_state;
794 int i;
795
796 if (!crtc || !state)
797 return -EINVAL;
798
799 sde_crtc = to_sde_crtc(crtc);
800 crtc_state = to_sde_crtc_state(state);
801
802 if (sde_kms_rect_is_null(&crtc_state->crtc_roi))
803 return 0;
804
805 /* partial update active, check if autorefresh is also requested */
806 for_each_connector_in_state(state->state, conn, conn_state, i) {
807 uint64_t autorefresh;
808
809 if (!conn_state || conn_state->crtc != crtc)
810 continue;
811
812 autorefresh = sde_connector_get_property(conn_state,
813 CONNECTOR_PROP_AUTOREFRESH);
814 if (autorefresh) {
815 SDE_ERROR(
816 "%s: autorefresh & partial crtc roi incompatible %llu\n",
817 sde_crtc->name, autorefresh);
818 return -EINVAL;
819 }
820 }
821
822 return 0;
823}
824
Lloyd Atkinson8ba47032017-03-22 17:13:32 -0400825static int _sde_crtc_set_lm_roi(struct drm_crtc *crtc,
826 struct drm_crtc_state *state, int lm_idx)
827{
828 struct sde_crtc *sde_crtc;
829 struct sde_crtc_state *crtc_state;
830 const struct sde_rect *crtc_roi;
831 const struct sde_rect *lm_bounds;
832 struct sde_rect *lm_roi;
833
834 if (!crtc || !state || lm_idx >= ARRAY_SIZE(crtc_state->lm_bounds))
835 return -EINVAL;
836
837 sde_crtc = to_sde_crtc(crtc);
838 crtc_state = to_sde_crtc_state(state);
839 crtc_roi = &crtc_state->crtc_roi;
840 lm_bounds = &crtc_state->lm_bounds[lm_idx];
841 lm_roi = &crtc_state->lm_roi[lm_idx];
842
Lloyd Atkinson73fb8092017-02-08 16:02:55 -0500843 if (sde_kms_rect_is_null(crtc_roi))
Lloyd Atkinson8ba47032017-03-22 17:13:32 -0400844 memcpy(lm_roi, lm_bounds, sizeof(*lm_roi));
Lloyd Atkinson73fb8092017-02-08 16:02:55 -0500845 else
846 sde_kms_rect_intersect(crtc_roi, lm_bounds, lm_roi);
Lloyd Atkinson8ba47032017-03-22 17:13:32 -0400847
848 SDE_DEBUG("%s: lm%d roi (%d,%d,%d,%d)\n", sde_crtc->name, lm_idx,
849 lm_roi->x, lm_roi->y, lm_roi->w, lm_roi->h);
850
Lloyd Atkinson73fb8092017-02-08 16:02:55 -0500851 /* if any dimension is zero, clear all dimensions for clarity */
852 if (sde_kms_rect_is_null(lm_roi))
853 memset(lm_roi, 0, sizeof(*lm_roi));
854
Lloyd Atkinson8ba47032017-03-22 17:13:32 -0400855 return 0;
856}
857
Lloyd Atkinson73fb8092017-02-08 16:02:55 -0500858static u32 _sde_crtc_get_displays_affected(struct drm_crtc *crtc,
859 struct drm_crtc_state *state)
860{
861 struct sde_crtc *sde_crtc;
862 struct sde_crtc_state *crtc_state;
863 u32 disp_bitmask = 0;
864 int i;
865
866 sde_crtc = to_sde_crtc(crtc);
867 crtc_state = to_sde_crtc_state(state);
868
Lloyd Atkinson66e7dde2017-02-08 15:52:53 -0500869 /* pingpong split: one ROI, one LM, two physical displays */
870 if (crtc_state->is_ppsplit) {
871 u32 lm_split_width = crtc_state->lm_bounds[0].w / 2;
872 struct sde_rect *roi = &crtc_state->lm_roi[0];
873
874 if (sde_kms_rect_is_null(roi))
875 disp_bitmask = 0;
876 else if ((u32)roi->x + (u32)roi->w <= lm_split_width)
877 disp_bitmask = BIT(0); /* left only */
878 else if (roi->x >= lm_split_width)
879 disp_bitmask = BIT(1); /* right only */
880 else
881 disp_bitmask = BIT(0) | BIT(1); /* left and right */
882 } else {
883 for (i = 0; i < sde_crtc->num_mixers; i++) {
884 if (!sde_kms_rect_is_null(&crtc_state->lm_roi[i]))
885 disp_bitmask |= BIT(i);
886 }
Lloyd Atkinson73fb8092017-02-08 16:02:55 -0500887 }
888
889 SDE_DEBUG("affected displays 0x%x\n", disp_bitmask);
890
891 return disp_bitmask;
892}
893
Lloyd Atkinson8ba47032017-03-22 17:13:32 -0400894static int _sde_crtc_check_rois_centered_and_symmetric(struct drm_crtc *crtc,
895 struct drm_crtc_state *state)
896{
897 struct sde_crtc *sde_crtc;
898 struct sde_crtc_state *crtc_state;
Lloyd Atkinson73fb8092017-02-08 16:02:55 -0500899 const struct sde_rect *roi[CRTC_DUAL_MIXERS];
Lloyd Atkinson8ba47032017-03-22 17:13:32 -0400900
901 if (!crtc || !state)
902 return -EINVAL;
903
Lloyd Atkinson73fb8092017-02-08 16:02:55 -0500904 sde_crtc = to_sde_crtc(crtc);
905 crtc_state = to_sde_crtc_state(state);
906
Lloyd Atkinson73fb8092017-02-08 16:02:55 -0500907 if (sde_crtc->num_mixers > CRTC_DUAL_MIXERS) {
908 SDE_ERROR("%s: unsupported number of mixers: %d\n",
909 sde_crtc->name, sde_crtc->num_mixers);
910 return -EINVAL;
911 }
912
Lloyd Atkinson8ba47032017-03-22 17:13:32 -0400913 /*
Lloyd Atkinson66e7dde2017-02-08 15:52:53 -0500914 * If using pingpong split: one ROI, one LM, two physical displays
915 * then the ROI must be centered on the panel split boundary and
916 * be of equal width across the split.
Lloyd Atkinson8ba47032017-03-22 17:13:32 -0400917 */
Lloyd Atkinson66e7dde2017-02-08 15:52:53 -0500918 if (crtc_state->is_ppsplit) {
919 u16 panel_split_width;
920 u32 display_mask;
921
922 roi[0] = &crtc_state->lm_roi[0];
923
924 if (sde_kms_rect_is_null(roi[0]))
925 return 0;
926
927 display_mask = _sde_crtc_get_displays_affected(crtc, state);
928 if (display_mask != (BIT(0) | BIT(1)))
929 return 0;
930
931 panel_split_width = crtc_state->lm_bounds[0].w / 2;
932 if (roi[0]->x + roi[0]->w / 2 != panel_split_width) {
933 SDE_ERROR("%s: roi x %d w %d split %d\n",
934 sde_crtc->name, roi[0]->x, roi[0]->w,
935 panel_split_width);
936 return -EINVAL;
937 }
938
939 return 0;
940 }
941
942 /*
943 * On certain HW, if using 2 LM, ROIs must be split evenly between the
944 * LMs and be of equal width.
945 */
946 if (sde_crtc->num_mixers == 1)
947 return 0;
948
Lloyd Atkinson73fb8092017-02-08 16:02:55 -0500949 roi[0] = &crtc_state->lm_roi[0];
950 roi[1] = &crtc_state->lm_roi[1];
Lloyd Atkinson8ba47032017-03-22 17:13:32 -0400951
Lloyd Atkinson73fb8092017-02-08 16:02:55 -0500952 /* if one of the roi is null it's a left/right-only update */
953 if (sde_kms_rect_is_null(roi[0]) || sde_kms_rect_is_null(roi[1]))
954 return 0;
Lloyd Atkinson8ba47032017-03-22 17:13:32 -0400955
Lloyd Atkinson73fb8092017-02-08 16:02:55 -0500956 /* check lm rois are equal width & first roi ends at 2nd roi */
957 if (roi[0]->x + roi[0]->w != roi[1]->x || roi[0]->w != roi[1]->w) {
958 SDE_ERROR(
959 "%s: rois not centered and symmetric: roi0 x %d w %d roi1 x %d w %d\n",
960 sde_crtc->name, roi[0]->x, roi[0]->w,
961 roi[1]->x, roi[1]->w);
962 return -EINVAL;
Lloyd Atkinson8ba47032017-03-22 17:13:32 -0400963 }
964
965 return 0;
966}
967
968static int _sde_crtc_check_planes_within_crtc_roi(struct drm_crtc *crtc,
969 struct drm_crtc_state *state)
970{
971 struct sde_crtc *sde_crtc;
972 struct sde_crtc_state *crtc_state;
973 const struct sde_rect *crtc_roi;
974 struct drm_plane_state *pstate;
975 struct drm_plane *plane;
976
977 if (!crtc || !state)
978 return -EINVAL;
979
980 /*
981 * Reject commit if a Plane CRTC destination coordinates fall outside
982 * the partial CRTC ROI. LM output is determined via connector ROIs,
983 * if they are specified, not Plane CRTC ROIs.
984 */
985
986 sde_crtc = to_sde_crtc(crtc);
987 crtc_state = to_sde_crtc_state(state);
988 crtc_roi = &crtc_state->crtc_roi;
989
990 if (sde_kms_rect_is_null(crtc_roi))
991 return 0;
992
993 drm_atomic_crtc_state_for_each_plane(plane, state) {
994 struct sde_rect plane_roi, intersection;
995
996 pstate = drm_atomic_get_plane_state(state->state, plane);
997 if (IS_ERR_OR_NULL(pstate)) {
998 int rc = PTR_ERR(pstate);
999
1000 SDE_ERROR("%s: failed to get plane%d state, %d\n",
1001 sde_crtc->name, plane->base.id, rc);
1002 return rc;
1003 }
1004
1005 plane_roi.x = pstate->crtc_x;
1006 plane_roi.y = pstate->crtc_y;
1007 plane_roi.w = pstate->crtc_w;
1008 plane_roi.h = pstate->crtc_h;
1009 sde_kms_rect_intersect(crtc_roi, &plane_roi, &intersection);
1010 if (!sde_kms_rect_is_equal(&plane_roi, &intersection)) {
1011 SDE_ERROR(
1012 "%s: plane%d crtc roi (%d,%d,%d,%d) outside crtc roi (%d,%d,%d,%d)\n",
1013 sde_crtc->name, plane->base.id,
1014 plane_roi.x, plane_roi.y,
1015 plane_roi.w, plane_roi.h,
1016 crtc_roi->x, crtc_roi->y,
1017 crtc_roi->w, crtc_roi->h);
1018 return -E2BIG;
1019 }
1020 }
1021
1022 return 0;
1023}
1024
1025static int _sde_crtc_check_rois(struct drm_crtc *crtc,
1026 struct drm_crtc_state *state)
1027{
1028 struct sde_crtc *sde_crtc;
1029 int lm_idx;
1030 int rc;
1031
1032 if (!crtc || !state)
1033 return -EINVAL;
1034
1035 sde_crtc = to_sde_crtc(crtc);
1036
1037 rc = _sde_crtc_set_crtc_roi(crtc, state);
1038 if (rc)
1039 return rc;
1040
Lloyd Atkinson77382202017-02-01 14:59:43 -05001041 rc = _sde_crtc_check_autorefresh(crtc, state);
1042 if (rc)
1043 return rc;
1044
Lloyd Atkinson8ba47032017-03-22 17:13:32 -04001045 for (lm_idx = 0; lm_idx < sde_crtc->num_mixers; lm_idx++) {
1046 rc = _sde_crtc_set_lm_roi(crtc, state, lm_idx);
1047 if (rc)
1048 return rc;
1049 }
1050
1051 rc = _sde_crtc_check_rois_centered_and_symmetric(crtc, state);
1052 if (rc)
1053 return rc;
1054
1055 rc = _sde_crtc_check_planes_within_crtc_roi(crtc, state);
1056 if (rc)
1057 return rc;
1058
1059 return 0;
1060}
1061
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05001062static void _sde_crtc_program_lm_output_roi(struct drm_crtc *crtc)
1063{
1064 struct sde_crtc *sde_crtc;
1065 struct sde_crtc_state *crtc_state;
Lloyd Atkinson8ba47032017-03-22 17:13:32 -04001066 const struct sde_rect *lm_roi;
1067 struct sde_hw_mixer *hw_lm;
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05001068 int lm_idx, lm_horiz_position;
1069
Lloyd Atkinson8ba47032017-03-22 17:13:32 -04001070 if (!crtc)
1071 return;
1072
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05001073 sde_crtc = to_sde_crtc(crtc);
1074 crtc_state = to_sde_crtc_state(crtc->state);
1075
1076 lm_horiz_position = 0;
1077 for (lm_idx = 0; lm_idx < sde_crtc->num_mixers; lm_idx++) {
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05001078 struct sde_hw_mixer_cfg cfg;
1079
Lloyd Atkinson8ba47032017-03-22 17:13:32 -04001080 lm_roi = &crtc_state->lm_roi[lm_idx];
1081 hw_lm = sde_crtc->mixers[lm_idx].hw_lm;
1082
1083 SDE_EVT32(DRMID(crtc_state->base.crtc), lm_idx,
1084 lm_roi->x, lm_roi->y, lm_roi->w, lm_roi->h);
1085
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05001086 if (sde_kms_rect_is_null(lm_roi))
1087 continue;
1088
Ping Lif41c2ef2017-05-04 14:40:45 -07001089 hw_lm->cfg.out_width = lm_roi->w;
1090 hw_lm->cfg.out_height = lm_roi->h;
1091 hw_lm->cfg.right_mixer = lm_horiz_position;
1092
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05001093 cfg.out_width = lm_roi->w;
1094 cfg.out_height = lm_roi->h;
1095 cfg.right_mixer = lm_horiz_position++;
1096 cfg.flags = 0;
1097 hw_lm->ops.setup_mixer_out(hw_lm, &cfg);
1098 }
1099}
1100
Dhaval Patel48c76022016-09-01 17:51:23 -07001101static void _sde_crtc_blend_setup_mixer(struct drm_crtc *crtc,
1102 struct sde_crtc *sde_crtc, struct sde_crtc_mixer *mixer)
1103{
1104 struct drm_plane *plane;
Dhaval Patel6c666622017-03-21 23:02:59 -07001105 struct drm_framebuffer *fb;
1106 struct drm_plane_state *state;
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08001107 struct sde_crtc_state *cstate;
Dhaval Patel48c76022016-09-01 17:51:23 -07001108 struct sde_plane_state *pstate = NULL;
1109 struct sde_format *format;
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08001110 struct sde_hw_ctl *ctl;
1111 struct sde_hw_mixer *lm;
1112 struct sde_hw_stage_cfg *stage_cfg;
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05001113 struct sde_rect plane_crtc_roi;
Dhaval Patel48c76022016-09-01 17:51:23 -07001114
Clarence Ip7e5f0002017-05-29 18:46:56 -04001115 u32 flush_mask, flush_sbuf, flush_tmp;
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05001116 uint32_t lm_idx = LEFT_MIXER, stage_idx;
Dhaval Patel48c76022016-09-01 17:51:23 -07001117 bool bg_alpha_enable[CRTC_DUAL_MIXERS] = {false};
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05001118 int zpos_cnt[CRTC_DUAL_MIXERS][SDE_STAGE_MAX + 1] = { {0} };
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08001119 int i;
Alan Kwong4dd64c82017-02-04 18:41:51 -08001120 u32 prefill = 0;
Dhaval Patel48c76022016-09-01 17:51:23 -07001121
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08001122 if (!sde_crtc || !mixer) {
1123 SDE_ERROR("invalid sde_crtc or mixer\n");
1124 return;
1125 }
1126
1127 ctl = mixer->hw_ctl;
1128 lm = mixer->hw_lm;
1129 stage_cfg = &sde_crtc->stage_cfg;
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05001130 cstate = to_sde_crtc_state(crtc->state);
Clarence Ip7e5f0002017-05-29 18:46:56 -04001131 flush_sbuf = 0x0;
Dhaval Patel44f12472016-08-29 12:19:47 -07001132
Clarence Ip7eb90452017-05-23 11:41:19 -04001133 cstate->sbuf_cfg.rot_op_mode = SDE_CTL_ROT_OP_MODE_OFFLINE;
1134 cstate->sbuf_prefill_line = 0;
1135
Dhaval Patel44f12472016-08-29 12:19:47 -07001136 drm_atomic_crtc_for_each_plane(plane, crtc) {
Dhaval Patel6c666622017-03-21 23:02:59 -07001137 state = plane->state;
1138 if (!state)
1139 continue;
Dhaval Patel48c76022016-09-01 17:51:23 -07001140
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05001141 plane_crtc_roi.x = state->crtc_x;
1142 plane_crtc_roi.y = state->crtc_y;
1143 plane_crtc_roi.w = state->crtc_w;
1144 plane_crtc_roi.h = state->crtc_h;
1145
Dhaval Patel6c666622017-03-21 23:02:59 -07001146 pstate = to_sde_plane_state(state);
1147 fb = state->fb;
Dhaval Patel44f12472016-08-29 12:19:47 -07001148
Alan Kwong4dd64c82017-02-04 18:41:51 -08001149 if (sde_plane_is_sbuf_mode(plane, &prefill))
Clarence Ip7eb90452017-05-23 11:41:19 -04001150 cstate->sbuf_cfg.rot_op_mode =
1151 SDE_CTL_ROT_OP_MODE_INLINE_SYNC;
1152 if (prefill > cstate->sbuf_prefill_line)
1153 cstate->sbuf_prefill_line = prefill;
Alan Kwong4dd64c82017-02-04 18:41:51 -08001154
Clarence Ip7e5f0002017-05-29 18:46:56 -04001155 sde_plane_get_ctl_flush(plane, ctl, &flush_mask, &flush_tmp);
Dhaval Patel44f12472016-08-29 12:19:47 -07001156
Clarence Ip7e5f0002017-05-29 18:46:56 -04001157 /* persist rotator flush bit(s) for one more commit */
1158 flush_mask |= cstate->sbuf_flush_mask | flush_tmp;
1159 flush_sbuf |= flush_tmp;
Dhaval Patel48c76022016-09-01 17:51:23 -07001160
1161 SDE_DEBUG("crtc %d stage:%d - plane %d sspp %d fb %d\n",
Clarence Ipd9f9fa62016-09-09 13:42:32 -04001162 crtc->base.id,
Clarence Ipd9f9fa62016-09-09 13:42:32 -04001163 pstate->stage,
1164 plane->base.id,
1165 sde_plane_pipe(plane) - SSPP_VIG0,
Dhaval Patel6c666622017-03-21 23:02:59 -07001166 state->fb ? state->fb->base.id : -1);
Dhaval Patel44f12472016-08-29 12:19:47 -07001167
Dhaval Patel48c76022016-09-01 17:51:23 -07001168 format = to_sde_format(msm_framebuffer_format(pstate->base.fb));
Dhaval Patel44f12472016-08-29 12:19:47 -07001169
Lloyd Atkinson8ba47032017-03-22 17:13:32 -04001170 SDE_EVT32(DRMID(crtc), DRMID(plane),
1171 state->fb ? state->fb->base.id : -1,
1172 state->src_x >> 16, state->src_y >> 16,
1173 state->src_w >> 16, state->src_h >> 16,
1174 state->crtc_x, state->crtc_y,
Clarence Ip7eb90452017-05-23 11:41:19 -04001175 state->crtc_w, state->crtc_h,
1176 cstate->sbuf_cfg.rot_op_mode);
Dhaval Patel6c666622017-03-21 23:02:59 -07001177
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05001178 for (lm_idx = 0; lm_idx < sde_crtc->num_mixers; lm_idx++) {
1179 struct sde_rect intersect;
Dhaval Patel48c76022016-09-01 17:51:23 -07001180
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05001181 /* skip if the roi doesn't fall within LM's bounds */
1182 sde_kms_rect_intersect(&plane_crtc_roi,
1183 &cstate->lm_bounds[lm_idx],
1184 &intersect);
1185 if (sde_kms_rect_is_null(&intersect))
1186 continue;
Dhaval Patel48c76022016-09-01 17:51:23 -07001187
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05001188 stage_idx = zpos_cnt[lm_idx][pstate->stage]++;
1189 stage_cfg->stage[lm_idx][pstate->stage][stage_idx] =
1190 sde_plane_pipe(plane);
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08001191 stage_cfg->multirect_index
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05001192 [lm_idx][pstate->stage][stage_idx] =
1193 pstate->multirect_index;
1194
1195 mixer[lm_idx].flush_mask |= flush_mask;
1196
1197
1198 SDE_EVT32(DRMID(plane), DRMID(crtc), lm_idx, stage_idx,
1199 pstate->stage, pstate->multirect_index,
1200 pstate->multirect_mode,
1201 format->base.pixel_format,
1202 fb ? fb->modifier[0] : 0);
Dhaval Patel48c76022016-09-01 17:51:23 -07001203
1204 /* blend config update */
1205 if (pstate->stage != SDE_STAGE_BASE) {
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05001206 _sde_crtc_setup_blend_cfg(mixer + lm_idx,
1207 pstate, format);
Dhaval Patel48c76022016-09-01 17:51:23 -07001208
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05001209 if (bg_alpha_enable[lm_idx] &&
Dhaval Patel48c76022016-09-01 17:51:23 -07001210 !format->alpha_enable)
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05001211 mixer[lm_idx].mixer_op_mode = 0;
Dhaval Patel48c76022016-09-01 17:51:23 -07001212 else
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05001213 mixer[lm_idx].mixer_op_mode |=
Dhaval Patel48c76022016-09-01 17:51:23 -07001214 1 << pstate->stage;
1215 } else if (format->alpha_enable) {
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05001216 bg_alpha_enable[lm_idx] = true;
Dhaval Patel48c76022016-09-01 17:51:23 -07001217 }
1218 }
Dhaval Patel44f12472016-08-29 12:19:47 -07001219 }
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08001220
Clarence Ip7e5f0002017-05-29 18:46:56 -04001221 cstate->sbuf_flush_mask = flush_sbuf;
1222
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08001223 if (lm && lm->ops.setup_dim_layer) {
1224 cstate = to_sde_crtc_state(crtc->state);
1225 for (i = 0; i < cstate->num_dim_layers; i++)
1226 _sde_crtc_setup_dim_layer_cfg(crtc, sde_crtc,
1227 mixer, &cstate->dim_layer[i]);
1228 }
Alan Kwong4dd64c82017-02-04 18:41:51 -08001229
Clarence Ip7eb90452017-05-23 11:41:19 -04001230 if (ctl->ops.setup_sbuf_cfg)
Alan Kwong4dd64c82017-02-04 18:41:51 -08001231 ctl->ops.setup_sbuf_cfg(ctl, &cstate->sbuf_cfg);
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05001232
1233 _sde_crtc_program_lm_output_roi(crtc);
Dhaval Patel44f12472016-08-29 12:19:47 -07001234}
1235
Lloyd Atkinson094780d2017-04-24 17:25:08 -04001236static void _sde_crtc_swap_mixers_for_right_partial_update(
1237 struct drm_crtc *crtc)
1238{
1239 struct sde_crtc *sde_crtc;
1240 struct sde_crtc_state *cstate;
1241 struct drm_encoder *drm_enc;
1242 bool is_right_only;
1243 bool encoder_in_dsc_merge = false;
1244
1245 if (!crtc || !crtc->state)
1246 return;
1247
1248 sde_crtc = to_sde_crtc(crtc);
1249 cstate = to_sde_crtc_state(crtc->state);
1250
1251 if (sde_crtc->num_mixers != CRTC_DUAL_MIXERS)
1252 return;
1253
1254 drm_for_each_encoder(drm_enc, crtc->dev) {
1255 if (drm_enc->crtc == crtc &&
1256 sde_encoder_is_dsc_merge(drm_enc)) {
1257 encoder_in_dsc_merge = true;
1258 break;
1259 }
1260 }
1261
1262 /**
1263 * For right-only partial update with DSC merge, we swap LM0 & LM1.
1264 * This is due to two reasons:
1265 * - On 8996, there is a DSC HW requirement that in DSC Merge Mode,
1266 * the left DSC must be used, right DSC cannot be used alone.
1267 * For right-only partial update, this means swap layer mixers to map
1268 * Left LM to Right INTF. On later HW this was relaxed.
1269 * - In DSC Merge mode, the physical encoder has already registered
1270 * PP0 as the master, to switch to right-only we would have to
1271 * reprogram to be driven by PP1 instead.
1272 * To support both cases, we prefer to support the mixer swap solution.
1273 */
1274 if (!encoder_in_dsc_merge)
1275 return;
1276
1277 is_right_only = sde_kms_rect_is_null(&cstate->lm_roi[0]) &&
1278 !sde_kms_rect_is_null(&cstate->lm_roi[1]);
1279
1280 if (is_right_only && !sde_crtc->mixers_swapped) {
1281 /* right-only update swap mixers */
1282 swap(sde_crtc->mixers[0], sde_crtc->mixers[1]);
1283 sde_crtc->mixers_swapped = true;
1284 } else if (!is_right_only && sde_crtc->mixers_swapped) {
1285 /* left-only or full update, swap back */
1286 swap(sde_crtc->mixers[0], sde_crtc->mixers[1]);
1287 sde_crtc->mixers_swapped = false;
1288 }
1289
1290 SDE_DEBUG("%s: right_only %d swapped %d, mix0->lm%d, mix1->lm%d\n",
1291 sde_crtc->name, is_right_only, sde_crtc->mixers_swapped,
1292 sde_crtc->mixers[0].hw_lm->idx - LM_0,
1293 sde_crtc->mixers[1].hw_lm->idx - LM_0);
1294 SDE_EVT32(DRMID(crtc), is_right_only, sde_crtc->mixers_swapped,
1295 sde_crtc->mixers[0].hw_lm->idx - LM_0,
1296 sde_crtc->mixers[1].hw_lm->idx - LM_0);
1297}
1298
Clarence Ipd9f9fa62016-09-09 13:42:32 -04001299/**
1300 * _sde_crtc_blend_setup - configure crtc mixers
1301 * @crtc: Pointer to drm crtc structure
1302 */
1303static void _sde_crtc_blend_setup(struct drm_crtc *crtc)
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001304{
Lloyd Atkinson73fb8092017-02-08 16:02:55 -05001305 struct sde_crtc *sde_crtc;
1306 struct sde_crtc_state *sde_crtc_state;
1307 struct sde_crtc_mixer *mixer;
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001308 struct sde_hw_ctl *ctl;
1309 struct sde_hw_mixer *lm;
Dhaval Patel44f12472016-08-29 12:19:47 -07001310
1311 int i;
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001312
Lloyd Atkinson73fb8092017-02-08 16:02:55 -05001313 if (!crtc)
1314 return;
1315
1316 sde_crtc = to_sde_crtc(crtc);
1317 sde_crtc_state = to_sde_crtc_state(crtc->state);
1318 mixer = sde_crtc->mixers;
1319
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04001320 SDE_DEBUG("%s\n", sde_crtc->name);
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001321
Dhaval Patel48c76022016-09-01 17:51:23 -07001322 if (sde_crtc->num_mixers > CRTC_DUAL_MIXERS) {
1323 SDE_ERROR("invalid number mixers: %d\n", sde_crtc->num_mixers);
1324 return;
1325 }
1326
1327 for (i = 0; i < sde_crtc->num_mixers; i++) {
1328 if (!mixer[i].hw_lm || !mixer[i].hw_ctl) {
1329 SDE_ERROR("invalid lm or ctl assigned to mixer\n");
1330 return;
1331 }
1332 mixer[i].mixer_op_mode = 0;
1333 mixer[i].flush_mask = 0;
Lloyd Atkinsone5ec30d2016-08-23 14:32:32 -04001334 if (mixer[i].hw_ctl->ops.clear_all_blendstages)
1335 mixer[i].hw_ctl->ops.clear_all_blendstages(
1336 mixer[i].hw_ctl);
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08001337
1338 /* clear dim_layer settings */
1339 lm = mixer[i].hw_lm;
1340 if (lm->ops.clear_dim_layer)
1341 lm->ops.clear_dim_layer(lm);
Dhaval Patel48c76022016-09-01 17:51:23 -07001342 }
1343
Lloyd Atkinson094780d2017-04-24 17:25:08 -04001344 _sde_crtc_swap_mixers_for_right_partial_update(crtc);
1345
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001346 /* initialize stage cfg */
Clarence Ip8f7366c2016-07-05 12:15:26 -04001347 memset(&sde_crtc->stage_cfg, 0, sizeof(struct sde_hw_stage_cfg));
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001348
Dhaval Patel48c76022016-09-01 17:51:23 -07001349 _sde_crtc_blend_setup_mixer(crtc, sde_crtc, mixer);
1350
Abhijit Kulkarni71002ba2016-06-24 18:36:28 -04001351 for (i = 0; i < sde_crtc->num_mixers; i++) {
Lloyd Atkinson73fb8092017-02-08 16:02:55 -05001352 const struct sde_rect *lm_roi = &sde_crtc_state->lm_roi[i];
1353
Abhijit Kulkarni71002ba2016-06-24 18:36:28 -04001354 ctl = mixer[i].hw_ctl;
1355 lm = mixer[i].hw_lm;
Abhijit Kulkarni71002ba2016-06-24 18:36:28 -04001356
Lloyd Atkinson73fb8092017-02-08 16:02:55 -05001357 if (sde_kms_rect_is_null(lm_roi)) {
1358 SDE_DEBUG(
1359 "%s: lm%d leave ctl%d mask 0 since null roi\n",
1360 sde_crtc->name, lm->idx - LM_0,
1361 ctl->idx - CTL_0);
1362 continue;
1363 }
1364
Dhaval Patel48c76022016-09-01 17:51:23 -07001365 lm->ops.setup_alpha_out(lm, mixer[i].mixer_op_mode);
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001366
Dhaval Patel48c76022016-09-01 17:51:23 -07001367 mixer[i].flush_mask |= ctl->ops.get_bitmask_mixer(ctl,
Abhijit Kulkarni71002ba2016-06-24 18:36:28 -04001368 mixer[i].hw_lm->idx);
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04001369
1370 /* stage config flush mask */
Dhaval Patel48c76022016-09-01 17:51:23 -07001371 ctl->ops.update_pending_flush(ctl, mixer[i].flush_mask);
1372
Clarence Ip8e69ad02016-12-09 09:43:57 -05001373 SDE_DEBUG("lm %d, op_mode 0x%X, ctl %d, flush mask 0x%x\n",
1374 mixer[i].hw_lm->idx - LM_0,
1375 mixer[i].mixer_op_mode,
1376 ctl->idx - CTL_0,
1377 mixer[i].flush_mask);
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001378
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001379 ctl->ops.setup_blendstage(ctl, mixer[i].hw_lm->idx,
Dhaval Patel44f12472016-08-29 12:19:47 -07001380 &sde_crtc->stage_cfg, i);
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001381 }
Lloyd Atkinson8ba47032017-03-22 17:13:32 -04001382
1383 _sde_crtc_program_lm_output_roi(crtc);
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001384}
1385
Clarence Ip0d0e96d2016-10-24 18:13:13 -04001386void sde_crtc_prepare_commit(struct drm_crtc *crtc,
1387 struct drm_crtc_state *old_state)
Clarence Ip24f80662016-06-13 19:05:32 -04001388{
1389 struct sde_crtc *sde_crtc;
Clarence Ip0d0e96d2016-10-24 18:13:13 -04001390 struct sde_crtc_state *cstate;
1391 struct drm_connector *conn;
Clarence Ip24f80662016-06-13 19:05:32 -04001392
Clarence Ip0d0e96d2016-10-24 18:13:13 -04001393 if (!crtc || !crtc->state) {
Clarence Ip24f80662016-06-13 19:05:32 -04001394 SDE_ERROR("invalid crtc\n");
1395 return;
1396 }
1397
1398 sde_crtc = to_sde_crtc(crtc);
Clarence Ip0d0e96d2016-10-24 18:13:13 -04001399 cstate = to_sde_crtc_state(crtc->state);
Dhaval Patel6c666622017-03-21 23:02:59 -07001400 SDE_EVT32_VERBOSE(DRMID(crtc));
Clarence Ip24f80662016-06-13 19:05:32 -04001401
Clarence Ip0d0e96d2016-10-24 18:13:13 -04001402 /* identify connectors attached to this crtc */
Clarence Ip0d0e96d2016-10-24 18:13:13 -04001403 cstate->num_connectors = 0;
1404
1405 drm_for_each_connector(conn, crtc->dev)
1406 if (conn->state && conn->state->crtc == crtc &&
1407 cstate->num_connectors < MAX_CONNECTORS) {
1408 cstate->connectors[cstate->num_connectors++] = conn;
1409 sde_connector_prepare_fence(conn);
Clarence Ip0d0e96d2016-10-24 18:13:13 -04001410 }
1411
1412 /* prepare main output fence */
Clarence Ip24f80662016-06-13 19:05:32 -04001413 sde_fence_prepare(&sde_crtc->output_fence);
1414}
1415
Abhinav Kumarf2e94b52017-02-09 20:27:24 -08001416/**
1417 * _sde_crtc_complete_flip - signal pending page_flip events
1418 * Any pending vblank events are added to the vblank_event_list
1419 * so that the next vblank interrupt shall signal them.
1420 * However PAGE_FLIP events are not handled through the vblank_event_list.
1421 * This API signals any pending PAGE_FLIP events requested through
1422 * DRM_IOCTL_MODE_PAGE_FLIP and are cached in the sde_crtc->event.
1423 * if file!=NULL, this is preclose potential cancel-flip path
1424 * @crtc: Pointer to drm crtc structure
1425 * @file: Pointer to drm file
1426 */
Lloyd Atkinson4f1c8692016-09-14 14:04:25 -04001427static void _sde_crtc_complete_flip(struct drm_crtc *crtc,
1428 struct drm_file *file)
Abhijit Kulkarni40e38162016-06-26 22:12:09 -04001429{
1430 struct sde_crtc *sde_crtc = to_sde_crtc(crtc);
1431 struct drm_device *dev = crtc->dev;
1432 struct drm_pending_vblank_event *event;
1433 unsigned long flags;
1434
1435 spin_lock_irqsave(&dev->event_lock, flags);
1436 event = sde_crtc->event;
1437 if (event) {
1438 /* if regular vblank case (!file) or if cancel-flip from
1439 * preclose on file that requested flip, then send the
1440 * event:
1441 */
1442 if (!file || (event->base.file_priv == file)) {
1443 sde_crtc->event = NULL;
Lloyd Atkinson4f1c8692016-09-14 14:04:25 -04001444 DRM_DEBUG_VBL("%s: send event: %pK\n",
Dhaval Patelec10fad2016-08-22 14:40:48 -07001445 sde_crtc->name, event);
Lloyd Atkinson5d40d312016-09-06 08:34:13 -04001446 SDE_EVT32(DRMID(crtc));
Lloyd Atkinsonac933642016-09-14 11:52:00 -04001447 drm_crtc_send_vblank_event(crtc, event);
Abhijit Kulkarni40e38162016-06-26 22:12:09 -04001448 }
1449 }
1450 spin_unlock_irqrestore(&dev->event_lock, flags);
1451}
1452
Alan Kwong3e985f02017-02-12 15:08:44 -08001453enum sde_intf_mode sde_crtc_get_intf_mode(struct drm_crtc *crtc)
1454{
1455 struct drm_encoder *encoder;
1456
1457 if (!crtc || !crtc->dev) {
1458 SDE_ERROR("invalid crtc\n");
1459 return INTF_MODE_NONE;
1460 }
1461
1462 drm_for_each_encoder(encoder, crtc->dev)
1463 if (encoder->crtc == crtc)
1464 return sde_encoder_get_intf_mode(encoder);
1465
1466 return INTF_MODE_NONE;
1467}
1468
Abhijit Kulkarni40e38162016-06-26 22:12:09 -04001469static void sde_crtc_vblank_cb(void *data)
1470{
1471 struct drm_crtc *crtc = (struct drm_crtc *)data;
Alan Kwong07da0982016-11-04 12:57:45 -04001472 struct sde_crtc *sde_crtc = to_sde_crtc(crtc);
1473
1474 /* keep statistics on vblank callback - with auto reset via debugfs */
1475 if (ktime_equal(sde_crtc->vblank_cb_time, ktime_set(0, 0)))
1476 sde_crtc->vblank_cb_time = ktime_get();
1477 else
1478 sde_crtc->vblank_cb_count++;
Abhinav Kumarf2e94b52017-02-09 20:27:24 -08001479 _sde_crtc_complete_flip(crtc, NULL);
Lloyd Atkinsonac933642016-09-14 11:52:00 -04001480 drm_crtc_handle_vblank(crtc);
Lloyd Atkinson9eabe7a2016-09-14 13:39:15 -04001481 DRM_DEBUG_VBL("crtc%d\n", crtc->base.id);
Dhaval Patel6c666622017-03-21 23:02:59 -07001482 SDE_EVT32_VERBOSE(DRMID(crtc));
Abhijit Kulkarni40e38162016-06-26 22:12:09 -04001483}
1484
Alan Kwong628d19e2016-10-31 13:50:13 -04001485static void sde_crtc_frame_event_work(struct kthread_work *work)
1486{
Alan Kwong67a3f792016-11-01 23:16:53 -04001487 struct msm_drm_private *priv;
Alan Kwong628d19e2016-10-31 13:50:13 -04001488 struct sde_crtc_frame_event *fevent;
1489 struct drm_crtc *crtc;
1490 struct sde_crtc *sde_crtc;
Alan Kwonga1939682017-05-05 11:30:08 -07001491 struct sde_crtc_state *cstate;
Alan Kwong628d19e2016-10-31 13:50:13 -04001492 struct sde_kms *sde_kms;
1493 unsigned long flags;
Ingrid Gallardo79b44392017-05-30 16:30:52 -07001494 bool disable_inprogress = false;
Alan Kwong628d19e2016-10-31 13:50:13 -04001495
1496 if (!work) {
1497 SDE_ERROR("invalid work handle\n");
1498 return;
1499 }
1500
1501 fevent = container_of(work, struct sde_crtc_frame_event, work);
Alan Kwonga1939682017-05-05 11:30:08 -07001502 if (!fevent->crtc || !fevent->crtc->state) {
Alan Kwong628d19e2016-10-31 13:50:13 -04001503 SDE_ERROR("invalid crtc\n");
1504 return;
1505 }
1506
1507 crtc = fevent->crtc;
1508 sde_crtc = to_sde_crtc(crtc);
Alan Kwonga1939682017-05-05 11:30:08 -07001509 cstate = to_sde_crtc_state(crtc->state);
Alan Kwong628d19e2016-10-31 13:50:13 -04001510
1511 sde_kms = _sde_crtc_get_kms(crtc);
1512 if (!sde_kms) {
1513 SDE_ERROR("invalid kms handle\n");
1514 return;
1515 }
Alan Kwong67a3f792016-11-01 23:16:53 -04001516 priv = sde_kms->dev->dev_private;
Alan Kwong628d19e2016-10-31 13:50:13 -04001517
1518 SDE_DEBUG("crtc%d event:%u ts:%lld\n", crtc->base.id, fevent->event,
1519 ktime_to_ns(fevent->ts));
Ingrid Gallardo79b44392017-05-30 16:30:52 -07001520 disable_inprogress = fevent->event &
1521 SDE_ENCODER_FRAME_EVENT_DURING_DISABLE;
1522 fevent->event &= ~SDE_ENCODER_FRAME_EVENT_DURING_DISABLE;
Alan Kwong628d19e2016-10-31 13:50:13 -04001523
1524 if (fevent->event == SDE_ENCODER_FRAME_EVENT_DONE ||
Lloyd Atkinson8c49c582016-11-18 14:23:54 -05001525 (fevent->event & SDE_ENCODER_FRAME_EVENT_ERROR) ||
1526 (fevent->event & SDE_ENCODER_FRAME_EVENT_PANEL_DEAD)) {
Alan Kwong628d19e2016-10-31 13:50:13 -04001527
1528 if (atomic_read(&sde_crtc->frame_pending) < 1) {
1529 /* this should not happen */
1530 SDE_ERROR("crtc%d ts:%lld invalid frame_pending:%d\n",
1531 crtc->base.id,
1532 ktime_to_ns(fevent->ts),
1533 atomic_read(&sde_crtc->frame_pending));
Dhaval Patel6c666622017-03-21 23:02:59 -07001534 SDE_EVT32(DRMID(crtc), fevent->event,
1535 SDE_EVTLOG_FUNC_CASE1);
Alan Kwong628d19e2016-10-31 13:50:13 -04001536 } else if (atomic_dec_return(&sde_crtc->frame_pending) == 0) {
1537 /* release bandwidth and other resources */
1538 SDE_DEBUG("crtc%d ts:%lld last pending\n",
1539 crtc->base.id,
1540 ktime_to_ns(fevent->ts));
Dhaval Patel6c666622017-03-21 23:02:59 -07001541 SDE_EVT32(DRMID(crtc), fevent->event,
1542 SDE_EVTLOG_FUNC_CASE2);
Ingrid Gallardo79b44392017-05-30 16:30:52 -07001543 if (!disable_inprogress)
1544 sde_core_perf_crtc_release_bw(crtc);
Alan Kwong628d19e2016-10-31 13:50:13 -04001545 } else {
Dhaval Patel6c666622017-03-21 23:02:59 -07001546 SDE_EVT32_VERBOSE(DRMID(crtc), fevent->event,
1547 SDE_EVTLOG_FUNC_CASE3);
Alan Kwong628d19e2016-10-31 13:50:13 -04001548 }
Alan Kwonga1939682017-05-05 11:30:08 -07001549
Ingrid Gallardo79b44392017-05-30 16:30:52 -07001550 if (fevent->event == SDE_ENCODER_FRAME_EVENT_DONE &&
1551 !disable_inprogress)
Alan Kwonga1939682017-05-05 11:30:08 -07001552 sde_core_perf_crtc_update(crtc, 0, false);
Alan Kwong628d19e2016-10-31 13:50:13 -04001553 } else {
1554 SDE_ERROR("crtc%d ts:%lld unknown event %u\n", crtc->base.id,
1555 ktime_to_ns(fevent->ts),
1556 fevent->event);
Dhaval Patel6c666622017-03-21 23:02:59 -07001557 SDE_EVT32(DRMID(crtc), fevent->event, SDE_EVTLOG_FUNC_CASE4);
Alan Kwong628d19e2016-10-31 13:50:13 -04001558 }
1559
Lloyd Atkinson8c49c582016-11-18 14:23:54 -05001560 if (fevent->event & SDE_ENCODER_FRAME_EVENT_PANEL_DEAD)
1561 SDE_ERROR("crtc%d ts:%lld received panel dead event\n",
1562 crtc->base.id, ktime_to_ns(fevent->ts));
1563
Alan Kwong628d19e2016-10-31 13:50:13 -04001564 spin_lock_irqsave(&sde_crtc->spin_lock, flags);
1565 list_add_tail(&fevent->list, &sde_crtc->frame_event_list);
1566 spin_unlock_irqrestore(&sde_crtc->spin_lock, flags);
1567}
1568
1569static void sde_crtc_frame_event_cb(void *data, u32 event)
1570{
1571 struct drm_crtc *crtc = (struct drm_crtc *)data;
1572 struct sde_crtc *sde_crtc;
1573 struct msm_drm_private *priv;
Alan Kwong628d19e2016-10-31 13:50:13 -04001574 struct sde_crtc_frame_event *fevent;
1575 unsigned long flags;
1576 int pipe_id;
1577
1578 if (!crtc || !crtc->dev || !crtc->dev->dev_private) {
1579 SDE_ERROR("invalid parameters\n");
1580 return;
1581 }
1582 sde_crtc = to_sde_crtc(crtc);
1583 priv = crtc->dev->dev_private;
1584 pipe_id = drm_crtc_index(crtc);
1585
1586 SDE_DEBUG("crtc%d\n", crtc->base.id);
Ingrid Gallardo79b44392017-05-30 16:30:52 -07001587 SDE_EVT32_VERBOSE(DRMID(crtc), event);
Alan Kwong628d19e2016-10-31 13:50:13 -04001588
1589 spin_lock_irqsave(&sde_crtc->spin_lock, flags);
Lloyd Atkinson78831f82016-12-09 11:24:56 -05001590 fevent = list_first_entry_or_null(&sde_crtc->frame_event_list,
1591 struct sde_crtc_frame_event, list);
1592 if (fevent)
1593 list_del_init(&fevent->list);
Alan Kwong628d19e2016-10-31 13:50:13 -04001594 spin_unlock_irqrestore(&sde_crtc->spin_lock, flags);
1595
Lloyd Atkinson78831f82016-12-09 11:24:56 -05001596 if (!fevent) {
Alan Kwong628d19e2016-10-31 13:50:13 -04001597 SDE_ERROR("crtc%d event %d overflow\n",
1598 crtc->base.id, event);
1599 SDE_EVT32(DRMID(crtc), event);
1600 return;
1601 }
1602
Alan Kwong628d19e2016-10-31 13:50:13 -04001603 fevent->event = event;
1604 fevent->crtc = crtc;
1605 fevent->ts = ktime_get();
Ingrid Gallardo79b44392017-05-30 16:30:52 -07001606 if (event & SDE_ENCODER_FRAME_EVENT_DURING_DISABLE)
1607 sde_crtc_frame_event_work(&fevent->work);
1608 else
1609 kthread_queue_work(&priv->disp_thread[pipe_id].worker,
1610 &fevent->work);
Alan Kwong628d19e2016-10-31 13:50:13 -04001611}
1612
Clarence Ip0d0e96d2016-10-24 18:13:13 -04001613void sde_crtc_complete_commit(struct drm_crtc *crtc,
1614 struct drm_crtc_state *old_state)
Clarence Ip24f80662016-06-13 19:05:32 -04001615{
Clarence Ip0d0e96d2016-10-24 18:13:13 -04001616 struct sde_crtc *sde_crtc;
1617 struct sde_crtc_state *cstate;
1618 int i;
1619
1620 if (!crtc || !crtc->state) {
Clarence Ip24f80662016-06-13 19:05:32 -04001621 SDE_ERROR("invalid crtc\n");
1622 return;
1623 }
1624
Clarence Ip0d0e96d2016-10-24 18:13:13 -04001625 sde_crtc = to_sde_crtc(crtc);
1626 cstate = to_sde_crtc_state(crtc->state);
Dhaval Patel6c666622017-03-21 23:02:59 -07001627 SDE_EVT32_VERBOSE(DRMID(crtc));
Clarence Ip0d0e96d2016-10-24 18:13:13 -04001628
1629 /* signal output fence(s) at end of commit */
1630 sde_fence_signal(&sde_crtc->output_fence, 0);
1631
1632 for (i = 0; i < cstate->num_connectors; ++i)
1633 sde_connector_complete_commit(cstate->connectors[i]);
Clarence Ip24f80662016-06-13 19:05:32 -04001634}
1635
Lloyd Atkinson5d722782016-05-30 14:09:41 -04001636/**
Clarence Ipcae1bb62016-07-07 12:07:13 -04001637 * _sde_crtc_set_input_fence_timeout - update ns version of in fence timeout
1638 * @cstate: Pointer to sde crtc state
1639 */
1640static void _sde_crtc_set_input_fence_timeout(struct sde_crtc_state *cstate)
1641{
1642 if (!cstate) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07001643 SDE_ERROR("invalid cstate\n");
Clarence Ipcae1bb62016-07-07 12:07:13 -04001644 return;
1645 }
1646 cstate->input_fence_timeout_ns =
1647 sde_crtc_get_property(cstate, CRTC_PROP_INPUT_FENCE_TIMEOUT);
1648 cstate->input_fence_timeout_ns *= NSEC_PER_MSEC;
1649}
1650
1651/**
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08001652 * _sde_crtc_set_dim_layer_v1 - copy dim layer settings from userspace
1653 * @cstate: Pointer to sde crtc state
1654 * @user_ptr: User ptr for sde_drm_dim_layer_v1 struct
1655 */
1656static void _sde_crtc_set_dim_layer_v1(struct sde_crtc_state *cstate,
1657 void *usr_ptr)
1658{
1659 struct sde_drm_dim_layer_v1 dim_layer_v1;
1660 struct sde_drm_dim_layer_cfg *user_cfg;
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -07001661 struct sde_hw_dim_layer *dim_layer;
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08001662 u32 count, i;
1663
1664 if (!cstate) {
1665 SDE_ERROR("invalid cstate\n");
1666 return;
1667 }
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -07001668 dim_layer = cstate->dim_layer;
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08001669
1670 if (!usr_ptr) {
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -07001671 SDE_DEBUG("dim_layer data removed\n");
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08001672 return;
1673 }
1674
1675 if (copy_from_user(&dim_layer_v1, usr_ptr, sizeof(dim_layer_v1))) {
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -07001676 SDE_ERROR("failed to copy dim_layer data\n");
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08001677 return;
1678 }
1679
1680 count = dim_layer_v1.num_layers;
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -07001681 if (count > SDE_MAX_DIM_LAYERS) {
1682 SDE_ERROR("invalid number of dim_layers:%d", count);
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08001683 return;
1684 }
1685
1686 /* populate from user space */
1687 cstate->num_dim_layers = count;
1688 for (i = 0; i < count; i++) {
1689 user_cfg = &dim_layer_v1.layer_cfg[i];
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08001690
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -07001691 dim_layer[i].flags = user_cfg->flags;
1692 dim_layer[i].stage = user_cfg->stage + SDE_STAGE_0;
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08001693
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -07001694 dim_layer[i].rect.x = user_cfg->rect.x1;
1695 dim_layer[i].rect.y = user_cfg->rect.y1;
1696 dim_layer[i].rect.w = user_cfg->rect.x2 - user_cfg->rect.x1;
1697 dim_layer[i].rect.h = user_cfg->rect.y2 - user_cfg->rect.y1;
1698
1699 dim_layer[i].color_fill = (struct sde_mdss_color) {
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08001700 user_cfg->color_fill.color_0,
1701 user_cfg->color_fill.color_1,
1702 user_cfg->color_fill.color_2,
1703 user_cfg->color_fill.color_3,
1704 };
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -07001705
1706 SDE_DEBUG("dim_layer[%d] - flags:%d, stage:%d\n",
1707 i, dim_layer[i].flags, dim_layer[i].stage);
1708 SDE_DEBUG(" rect:{%d,%d,%d,%d}, color:{%d,%d,%d,%d}\n",
1709 dim_layer[i].rect.x, dim_layer[i].rect.y,
1710 dim_layer[i].rect.w, dim_layer[i].rect.h,
1711 dim_layer[i].color_fill.color_0,
1712 dim_layer[i].color_fill.color_1,
1713 dim_layer[i].color_fill.color_2,
1714 dim_layer[i].color_fill.color_3);
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08001715 }
1716}
1717
1718/**
Clarence Ipcae1bb62016-07-07 12:07:13 -04001719 * _sde_crtc_wait_for_fences - wait for incoming framebuffer sync fences
1720 * @crtc: Pointer to CRTC object
1721 */
1722static void _sde_crtc_wait_for_fences(struct drm_crtc *crtc)
1723{
1724 struct drm_plane *plane = NULL;
1725 uint32_t wait_ms = 1;
Clarence Ip8dedc232016-09-09 16:41:00 -04001726 ktime_t kt_end, kt_wait;
Dhaval Patel39323d42017-03-01 23:48:24 -08001727 int rc = 0;
Clarence Ipcae1bb62016-07-07 12:07:13 -04001728
Lloyd Atkinson4f1c8692016-09-14 14:04:25 -04001729 SDE_DEBUG("\n");
Clarence Ipcae1bb62016-07-07 12:07:13 -04001730
1731 if (!crtc || !crtc->state) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07001732 SDE_ERROR("invalid crtc/state %pK\n", crtc);
Clarence Ipcae1bb62016-07-07 12:07:13 -04001733 return;
1734 }
1735
1736 /* use monotonic timer to limit total fence wait time */
Clarence Ip8dedc232016-09-09 16:41:00 -04001737 kt_end = ktime_add_ns(ktime_get(),
1738 to_sde_crtc_state(crtc->state)->input_fence_timeout_ns);
Clarence Ipcae1bb62016-07-07 12:07:13 -04001739
1740 /*
1741 * Wait for fences sequentially, as all of them need to be signalled
1742 * before we can proceed.
1743 *
1744 * Limit total wait time to INPUT_FENCE_TIMEOUT, but still call
1745 * sde_plane_wait_input_fence with wait_ms == 0 after the timeout so
1746 * that each plane can check its fence status and react appropriately
Dhaval Patel39323d42017-03-01 23:48:24 -08001747 * if its fence has timed out. Call input fence wait multiple times if
1748 * fence wait is interrupted due to interrupt call.
Clarence Ipcae1bb62016-07-07 12:07:13 -04001749 */
Narendra Muppalla77b32932017-05-10 13:53:11 -07001750 SDE_ATRACE_BEGIN("plane_wait_input_fence");
Clarence Ipcae1bb62016-07-07 12:07:13 -04001751 drm_atomic_crtc_for_each_plane(plane, crtc) {
Dhaval Patel39323d42017-03-01 23:48:24 -08001752 do {
Clarence Ip8dedc232016-09-09 16:41:00 -04001753 kt_wait = ktime_sub(kt_end, ktime_get());
1754 if (ktime_compare(kt_wait, ktime_set(0, 0)) >= 0)
1755 wait_ms = ktime_to_ms(kt_wait);
Clarence Ipcae1bb62016-07-07 12:07:13 -04001756 else
1757 wait_ms = 0;
Dhaval Patel39323d42017-03-01 23:48:24 -08001758
1759 rc = sde_plane_wait_input_fence(plane, wait_ms);
1760 } while (wait_ms && rc == -ERESTARTSYS);
Clarence Ipcae1bb62016-07-07 12:07:13 -04001761 }
Narendra Muppalla77b32932017-05-10 13:53:11 -07001762 SDE_ATRACE_END("plane_wait_input_fence");
Clarence Ipcae1bb62016-07-07 12:07:13 -04001763}
1764
Lloyd Atkinson11f34442016-08-11 11:19:52 -04001765static void _sde_crtc_setup_mixer_for_encoder(
1766 struct drm_crtc *crtc,
1767 struct drm_encoder *enc)
1768{
1769 struct sde_crtc *sde_crtc = to_sde_crtc(crtc);
Lloyd Atkinson4f1c8692016-09-14 14:04:25 -04001770 struct sde_kms *sde_kms = _sde_crtc_get_kms(crtc);
Lloyd Atkinson11f34442016-08-11 11:19:52 -04001771 struct sde_rm *rm = &sde_kms->rm;
1772 struct sde_crtc_mixer *mixer;
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04001773 struct sde_hw_ctl *last_valid_ctl = NULL;
Lloyd Atkinson11f34442016-08-11 11:19:52 -04001774 int i;
Gopikrishnaiah Anandane0e5e0c2016-05-25 11:05:33 -07001775 struct sde_rm_hw_iter lm_iter, ctl_iter, dspp_iter;
Lloyd Atkinson11f34442016-08-11 11:19:52 -04001776
1777 sde_rm_init_hw_iter(&lm_iter, enc->base.id, SDE_HW_BLK_LM);
1778 sde_rm_init_hw_iter(&ctl_iter, enc->base.id, SDE_HW_BLK_CTL);
Gopikrishnaiah Anandane0e5e0c2016-05-25 11:05:33 -07001779 sde_rm_init_hw_iter(&dspp_iter, enc->base.id, SDE_HW_BLK_DSPP);
Lloyd Atkinson11f34442016-08-11 11:19:52 -04001780
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04001781 /* Set up all the mixers and ctls reserved by this encoder */
Lloyd Atkinson11f34442016-08-11 11:19:52 -04001782 for (i = sde_crtc->num_mixers; i < ARRAY_SIZE(sde_crtc->mixers); i++) {
1783 mixer = &sde_crtc->mixers[i];
1784
Lloyd Atkinson11f34442016-08-11 11:19:52 -04001785 if (!sde_rm_get_hw(rm, &lm_iter))
1786 break;
1787 mixer->hw_lm = (struct sde_hw_mixer *)lm_iter.hw;
1788
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04001789 /* CTL may be <= LMs, if <, multiple LMs controlled by 1 CTL */
1790 if (!sde_rm_get_hw(rm, &ctl_iter)) {
1791 SDE_DEBUG("no ctl assigned to lm %d, using previous\n",
Clarence Ip8e69ad02016-12-09 09:43:57 -05001792 mixer->hw_lm->idx - LM_0);
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04001793 mixer->hw_ctl = last_valid_ctl;
1794 } else {
1795 mixer->hw_ctl = (struct sde_hw_ctl *)ctl_iter.hw;
1796 last_valid_ctl = mixer->hw_ctl;
1797 }
Lloyd Atkinson11f34442016-08-11 11:19:52 -04001798
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04001799 /* Shouldn't happen, mixers are always >= ctls */
1800 if (!mixer->hw_ctl) {
1801 SDE_ERROR("no valid ctls found for lm %d\n",
Clarence Ip8e69ad02016-12-09 09:43:57 -05001802 mixer->hw_lm->idx - LM_0);
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04001803 return;
1804 }
1805
Gopikrishnaiah Anandane0e5e0c2016-05-25 11:05:33 -07001806 /* Dspp may be null */
1807 (void) sde_rm_get_hw(rm, &dspp_iter);
1808 mixer->hw_dspp = (struct sde_hw_dspp *)dspp_iter.hw;
1809
Lloyd Atkinson11f34442016-08-11 11:19:52 -04001810 mixer->encoder = enc;
1811
1812 sde_crtc->num_mixers++;
Clarence Ipd9f9fa62016-09-09 13:42:32 -04001813 SDE_DEBUG("setup mixer %d: lm %d\n",
1814 i, mixer->hw_lm->idx - LM_0);
1815 SDE_DEBUG("setup mixer %d: ctl %d\n",
1816 i, mixer->hw_ctl->idx - CTL_0);
Lloyd Atkinson11f34442016-08-11 11:19:52 -04001817 }
1818}
1819
1820static void _sde_crtc_setup_mixers(struct drm_crtc *crtc)
1821{
1822 struct sde_crtc *sde_crtc = to_sde_crtc(crtc);
1823 struct drm_encoder *enc;
1824
Lloyd Atkinson11f34442016-08-11 11:19:52 -04001825 sde_crtc->num_mixers = 0;
1826 memset(sde_crtc->mixers, 0, sizeof(sde_crtc->mixers));
1827
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07001828 mutex_lock(&sde_crtc->crtc_lock);
Lloyd Atkinson11f34442016-08-11 11:19:52 -04001829 /* Check for mixers on all encoders attached to this crtc */
1830 list_for_each_entry(enc, &crtc->dev->mode_config.encoder_list, head) {
1831 if (enc->crtc != crtc)
1832 continue;
1833
1834 _sde_crtc_setup_mixer_for_encoder(crtc, enc);
1835 }
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05001836
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07001837 mutex_unlock(&sde_crtc->crtc_lock);
Lloyd Atkinson11f34442016-08-11 11:19:52 -04001838}
1839
Lloyd Atkinson66e7dde2017-02-08 15:52:53 -05001840static void _sde_crtc_setup_is_ppsplit(struct drm_crtc_state *state)
1841{
1842 int i;
1843 struct sde_crtc_state *cstate;
1844
1845 cstate = to_sde_crtc_state(state);
1846
1847 cstate->is_ppsplit = false;
1848 for (i = 0; i < cstate->num_connectors; i++) {
1849 struct drm_connector *conn = cstate->connectors[i];
1850
1851 if (sde_connector_get_topology_name(conn) ==
1852 SDE_RM_TOPOLOGY_PPSPLIT)
1853 cstate->is_ppsplit = true;
1854 }
1855}
1856
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05001857static void _sde_crtc_setup_lm_bounds(struct drm_crtc *crtc,
1858 struct drm_crtc_state *state)
1859{
1860 struct sde_crtc *sde_crtc;
1861 struct sde_crtc_state *cstate;
1862 struct drm_display_mode *adj_mode;
1863 u32 crtc_split_width;
1864 int i;
1865
1866 if (!crtc || !state) {
1867 SDE_ERROR("invalid args\n");
1868 return;
1869 }
1870
1871 sde_crtc = to_sde_crtc(crtc);
1872 cstate = to_sde_crtc_state(state);
1873
1874 adj_mode = &state->adjusted_mode;
1875 crtc_split_width = sde_crtc_mixer_width(sde_crtc, adj_mode);
1876
1877 for (i = 0; i < sde_crtc->num_mixers; i++) {
Lloyd Atkinson8ba47032017-03-22 17:13:32 -04001878 cstate->lm_bounds[i].x = crtc_split_width * i;
1879 cstate->lm_bounds[i].y = 0;
1880 cstate->lm_bounds[i].w = crtc_split_width;
1881 cstate->lm_bounds[i].h = adj_mode->vdisplay;
1882 memcpy(&cstate->lm_roi[i], &cstate->lm_bounds[i],
1883 sizeof(cstate->lm_roi[i]));
1884 SDE_EVT32(DRMID(crtc), i,
1885 cstate->lm_bounds[i].x, cstate->lm_bounds[i].y,
1886 cstate->lm_bounds[i].w, cstate->lm_bounds[i].h);
1887 SDE_DEBUG("%s: lm%d bnd&roi (%d,%d,%d,%d)\n", sde_crtc->name, i,
1888 cstate->lm_roi[i].x, cstate->lm_roi[i].y,
1889 cstate->lm_roi[i].w, cstate->lm_roi[i].h);
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05001890 }
1891
1892 drm_mode_debug_printmodeline(adj_mode);
1893}
1894
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001895static void sde_crtc_atomic_begin(struct drm_crtc *crtc,
Clarence Ip0d0e96d2016-10-24 18:13:13 -04001896 struct drm_crtc_state *old_state)
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07001897{
Clarence Ipcae1bb62016-07-07 12:07:13 -04001898 struct sde_crtc *sde_crtc;
Dhaval Patel0e558f42017-04-30 00:51:40 -07001899 struct drm_encoder *encoder;
Clarence Ipcae1bb62016-07-07 12:07:13 -04001900 struct drm_device *dev;
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001901 unsigned long flags;
1902
Clarence Ipcae1bb62016-07-07 12:07:13 -04001903 if (!crtc) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07001904 SDE_ERROR("invalid crtc\n");
Clarence Ipcae1bb62016-07-07 12:07:13 -04001905 return;
1906 }
1907
Alan Kwong163d2612016-11-03 00:56:56 -04001908 if (!crtc->state->enable) {
1909 SDE_DEBUG("crtc%d -> enable %d, skip atomic_begin\n",
1910 crtc->base.id, crtc->state->enable);
1911 return;
1912 }
1913
1914 SDE_DEBUG("crtc%d\n", crtc->base.id);
1915
Clarence Ipcae1bb62016-07-07 12:07:13 -04001916 sde_crtc = to_sde_crtc(crtc);
1917 dev = crtc->dev;
1918
Lloyd Atkinson8ba47032017-03-22 17:13:32 -04001919 if (!sde_crtc->num_mixers) {
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04001920 _sde_crtc_setup_mixers(crtc);
Lloyd Atkinson66e7dde2017-02-08 15:52:53 -05001921 _sde_crtc_setup_is_ppsplit(crtc->state);
Lloyd Atkinson8ba47032017-03-22 17:13:32 -04001922 _sde_crtc_setup_lm_bounds(crtc, crtc->state);
1923 }
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05001924
Lloyd Atkinson265d2212016-05-30 13:12:01 -04001925 if (sde_crtc->event) {
1926 WARN_ON(sde_crtc->event);
1927 } else {
1928 spin_lock_irqsave(&dev->event_lock, flags);
1929 sde_crtc->event = crtc->state->event;
1930 spin_unlock_irqrestore(&dev->event_lock, flags);
1931 }
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001932
Dhaval Patel0e558f42017-04-30 00:51:40 -07001933 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
1934 if (encoder->crtc != crtc)
1935 continue;
Lloyd Atkinson5d722782016-05-30 14:09:41 -04001936
Dhaval Patel0e558f42017-04-30 00:51:40 -07001937 /* encoder will trigger pending mask now */
1938 sde_encoder_trigger_kickoff_pending(encoder);
Lloyd Atkinson5d722782016-05-30 14:09:41 -04001939 }
1940
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001941 /*
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04001942 * If no mixers have been allocated in sde_crtc_atomic_check(),
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001943 * it means we are trying to flush a CRTC whose state is disabled:
1944 * nothing else needs to be done.
1945 */
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04001946 if (unlikely(!sde_crtc->num_mixers))
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001947 return;
1948
Clarence Ipd9f9fa62016-09-09 13:42:32 -04001949 _sde_crtc_blend_setup(crtc);
Gopikrishnaiah Anandane0e5e0c2016-05-25 11:05:33 -07001950 sde_cp_crtc_apply_properties(crtc);
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001951
1952 /*
1953 * PP_DONE irq is only used by command mode for now.
1954 * It is better to request pending before FLUSH and START trigger
1955 * to make sure no pp_done irq missed.
1956 * This is safe because no pp_done will happen before SW trigger
1957 * in command mode.
1958 */
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07001959}
1960
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001961static void sde_crtc_atomic_flush(struct drm_crtc *crtc,
1962 struct drm_crtc_state *old_crtc_state)
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07001963{
Dhaval Patel82c8dbc2017-02-18 23:15:10 -08001964 struct drm_encoder *encoder;
Clarence Ipcae1bb62016-07-07 12:07:13 -04001965 struct sde_crtc *sde_crtc;
1966 struct drm_device *dev;
Lloyd Atkinson265d2212016-05-30 13:12:01 -04001967 struct drm_plane *plane;
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001968 unsigned long flags;
Dhaval Patel82c8dbc2017-02-18 23:15:10 -08001969 struct sde_crtc_state *cstate;
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07001970
Clarence Ipcae1bb62016-07-07 12:07:13 -04001971 if (!crtc) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07001972 SDE_ERROR("invalid crtc\n");
Clarence Ipcae1bb62016-07-07 12:07:13 -04001973 return;
1974 }
1975
Alan Kwong163d2612016-11-03 00:56:56 -04001976 if (!crtc->state->enable) {
1977 SDE_DEBUG("crtc%d -> enable %d, skip atomic_flush\n",
1978 crtc->base.id, crtc->state->enable);
1979 return;
1980 }
1981
1982 SDE_DEBUG("crtc%d\n", crtc->base.id);
Clarence Ipcae1bb62016-07-07 12:07:13 -04001983
1984 sde_crtc = to_sde_crtc(crtc);
Dhaval Patel82c8dbc2017-02-18 23:15:10 -08001985 cstate = to_sde_crtc_state(crtc->state);
Clarence Ipcae1bb62016-07-07 12:07:13 -04001986 dev = crtc->dev;
1987
Lloyd Atkinson265d2212016-05-30 13:12:01 -04001988 if (sde_crtc->event) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07001989 SDE_DEBUG("already received sde_crtc->event\n");
Lloyd Atkinson265d2212016-05-30 13:12:01 -04001990 } else {
Lloyd Atkinson265d2212016-05-30 13:12:01 -04001991 spin_lock_irqsave(&dev->event_lock, flags);
1992 sde_crtc->event = crtc->state->event;
1993 spin_unlock_irqrestore(&dev->event_lock, flags);
1994 }
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001995
1996 /*
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04001997 * If no mixers has been allocated in sde_crtc_atomic_check(),
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001998 * it means we are trying to flush a CRTC whose state is disabled:
1999 * nothing else needs to be done.
2000 */
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04002001 if (unlikely(!sde_crtc->num_mixers))
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002002 return;
2003
Clarence Ipcae1bb62016-07-07 12:07:13 -04002004 /* wait for acquire fences before anything else is done */
2005 _sde_crtc_wait_for_fences(crtc);
2006
Dhaval Patel82c8dbc2017-02-18 23:15:10 -08002007 if (!cstate->rsc_update) {
2008 drm_for_each_encoder(encoder, dev) {
2009 if (encoder->crtc != crtc)
2010 continue;
2011
2012 cstate->rsc_client =
Dhaval Patel30fae8a2017-04-21 18:42:41 -07002013 sde_encoder_get_rsc_client(encoder);
Dhaval Patel82c8dbc2017-02-18 23:15:10 -08002014 }
2015 cstate->rsc_update = true;
2016 }
2017
Alan Kwong9aa061c2016-11-06 21:17:12 -05002018 /* update performance setting before crtc kickoff */
2019 sde_core_perf_crtc_update(crtc, 1, false);
2020
Clarence Ipcae1bb62016-07-07 12:07:13 -04002021 /*
2022 * Final plane updates: Give each plane a chance to complete all
2023 * required writes/flushing before crtc's "flush
2024 * everything" call below.
2025 */
2026 drm_atomic_crtc_for_each_plane(plane, crtc)
2027 sde_plane_flush(plane);
2028
Lloyd Atkinson5d722782016-05-30 14:09:41 -04002029 /* Kickoff will be scheduled by outer layer */
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07002030}
2031
Clarence Ip7a753bb2016-07-07 11:47:44 -04002032/**
2033 * sde_crtc_destroy_state - state destroy hook
2034 * @crtc: drm CRTC
2035 * @state: CRTC state object to release
2036 */
2037static void sde_crtc_destroy_state(struct drm_crtc *crtc,
2038 struct drm_crtc_state *state)
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07002039{
Clarence Ip7a753bb2016-07-07 11:47:44 -04002040 struct sde_crtc *sde_crtc;
2041 struct sde_crtc_state *cstate;
2042
2043 if (!crtc || !state) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07002044 SDE_ERROR("invalid argument(s)\n");
Clarence Ip7a753bb2016-07-07 11:47:44 -04002045 return;
2046 }
2047
2048 sde_crtc = to_sde_crtc(crtc);
2049 cstate = to_sde_crtc_state(state);
2050
Alan Kwong163d2612016-11-03 00:56:56 -04002051 SDE_DEBUG("crtc%d\n", crtc->base.id);
Clarence Ip7a753bb2016-07-07 11:47:44 -04002052
Alan Kwongcdb2f282017-03-18 13:42:06 -07002053 _sde_crtc_rp_destroy(&cstate->rp);
2054
Dhaval Patel04c7e8e2016-09-26 20:14:31 -07002055 __drm_atomic_helper_crtc_destroy_state(state);
Clarence Ip7a753bb2016-07-07 11:47:44 -04002056
2057 /* destroy value helper */
2058 msm_property_destroy_state(&sde_crtc->property_info, cstate,
2059 cstate->property_values, cstate->property_blobs);
2060}
2061
Lloyd Atkinson5d722782016-05-30 14:09:41 -04002062void sde_crtc_commit_kickoff(struct drm_crtc *crtc)
2063{
Lloyd Atkinsone7bcdd22016-08-11 10:53:37 -04002064 struct drm_encoder *encoder;
2065 struct drm_device *dev;
Alan Kwong628d19e2016-10-31 13:50:13 -04002066 struct sde_crtc *sde_crtc;
Alan Kwong67a3f792016-11-01 23:16:53 -04002067 struct msm_drm_private *priv;
2068 struct sde_kms *sde_kms;
Alan Kwong4aacd532017-02-04 18:51:33 -08002069 struct sde_crtc_state *cstate;
Lloyd Atkinson5d722782016-05-30 14:09:41 -04002070
2071 if (!crtc) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07002072 SDE_ERROR("invalid argument\n");
Lloyd Atkinson5d722782016-05-30 14:09:41 -04002073 return;
2074 }
Lloyd Atkinsone7bcdd22016-08-11 10:53:37 -04002075 dev = crtc->dev;
Alan Kwong628d19e2016-10-31 13:50:13 -04002076 sde_crtc = to_sde_crtc(crtc);
Alan Kwong67a3f792016-11-01 23:16:53 -04002077 sde_kms = _sde_crtc_get_kms(crtc);
2078 priv = sde_kms->dev->dev_private;
Alan Kwong4aacd532017-02-04 18:51:33 -08002079 cstate = to_sde_crtc_state(crtc->state);
Lloyd Atkinson5d722782016-05-30 14:09:41 -04002080
Clarence Ip90b282d2017-05-04 10:00:32 -07002081 /*
2082 * If no mixers has been allocated in sde_crtc_atomic_check(),
2083 * it means we are trying to start a CRTC whose state is disabled:
2084 * nothing else needs to be done.
2085 */
2086 if (unlikely(!sde_crtc->num_mixers))
2087 return;
2088
Narendra Muppalla77b32932017-05-10 13:53:11 -07002089 SDE_ATRACE_BEGIN("crtc_commit");
Lloyd Atkinsone7bcdd22016-08-11 10:53:37 -04002090 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
Alan Kwong4aacd532017-02-04 18:51:33 -08002091 struct sde_encoder_kickoff_params params = { 0 };
2092
Lloyd Atkinsone7bcdd22016-08-11 10:53:37 -04002093 if (encoder->crtc != crtc)
2094 continue;
2095
2096 /*
2097 * Encoder will flush/start now, unless it has a tx pending.
2098 * If so, it may delay and flush at an irq event (e.g. ppdone)
2099 */
Alan Kwong4aacd532017-02-04 18:51:33 -08002100 params.inline_rotate_prefill = cstate->sbuf_prefill_line;
Lloyd Atkinson73fb8092017-02-08 16:02:55 -05002101 params.affected_displays = _sde_crtc_get_displays_affected(crtc,
2102 crtc->state);
Alan Kwong4aacd532017-02-04 18:51:33 -08002103 sde_encoder_prepare_for_kickoff(encoder, &params);
Alan Kwong628d19e2016-10-31 13:50:13 -04002104 }
2105
2106 if (atomic_read(&sde_crtc->frame_pending) > 2) {
2107 /* framework allows only 1 outstanding + current */
2108 SDE_ERROR("crtc%d invalid frame pending\n",
2109 crtc->base.id);
2110 SDE_EVT32(DRMID(crtc), 0);
Narendra Muppalla77b32932017-05-10 13:53:11 -07002111 goto end;
Alan Kwong628d19e2016-10-31 13:50:13 -04002112 } else if (atomic_inc_return(&sde_crtc->frame_pending) == 1) {
2113 /* acquire bandwidth and other resources */
2114 SDE_DEBUG("crtc%d first commit\n", crtc->base.id);
2115 SDE_EVT32(DRMID(crtc), 1);
2116 } else {
2117 SDE_DEBUG("crtc%d commit\n", crtc->base.id);
2118 SDE_EVT32(DRMID(crtc), 2);
2119 }
2120
2121 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
2122 if (encoder->crtc != crtc)
2123 continue;
2124
2125 sde_encoder_kickoff(encoder);
Lloyd Atkinsone7bcdd22016-08-11 10:53:37 -04002126 }
Narendra Muppalla77b32932017-05-10 13:53:11 -07002127end:
2128 SDE_ATRACE_END("crtc_commit");
2129 return;
Lloyd Atkinson5d722782016-05-30 14:09:41 -04002130}
2131
Clarence Ip7a753bb2016-07-07 11:47:44 -04002132/**
Clarence Ip7f70ce42017-03-20 06:53:46 -07002133 * _sde_crtc_vblank_enable_nolock - update power resource and vblank request
2134 * @sde_crtc: Pointer to sde crtc structure
2135 * @enable: Whether to enable/disable vblanks
2136 */
2137static void _sde_crtc_vblank_enable_nolock(
2138 struct sde_crtc *sde_crtc, bool enable)
2139{
2140 struct drm_device *dev;
2141 struct drm_crtc *crtc;
2142 struct drm_encoder *enc;
Clarence Ip7f70ce42017-03-20 06:53:46 -07002143
2144 if (!sde_crtc) {
2145 SDE_ERROR("invalid crtc\n");
2146 return;
2147 }
2148
2149 crtc = &sde_crtc->base;
2150 dev = crtc->dev;
Clarence Ip7f70ce42017-03-20 06:53:46 -07002151
2152 if (enable) {
Lloyd Atkinson2c554eb2017-05-24 16:22:39 -04002153 int ret;
2154
2155 /* drop lock since power crtc cb may try to re-acquire lock */
2156 mutex_unlock(&sde_crtc->crtc_lock);
2157 ret = _sde_crtc_power_enable(sde_crtc, true);
2158 mutex_lock(&sde_crtc->crtc_lock);
2159 if (ret)
Dhaval Patelf9245d62017-03-28 16:24:00 -07002160 return;
2161
Clarence Ip7f70ce42017-03-20 06:53:46 -07002162 list_for_each_entry(enc, &dev->mode_config.encoder_list, head) {
2163 if (enc->crtc != crtc)
2164 continue;
2165
2166 SDE_EVT32(DRMID(crtc), DRMID(enc), enable);
2167
2168 sde_encoder_register_vblank_callback(enc,
2169 sde_crtc_vblank_cb, (void *)crtc);
2170 }
2171 } else {
2172 list_for_each_entry(enc, &dev->mode_config.encoder_list, head) {
2173 if (enc->crtc != crtc)
2174 continue;
2175
2176 SDE_EVT32(DRMID(crtc), DRMID(enc), enable);
2177
2178 sde_encoder_register_vblank_callback(enc, NULL, NULL);
2179 }
Lloyd Atkinson2c554eb2017-05-24 16:22:39 -04002180
2181 /* drop lock since power crtc cb may try to re-acquire lock */
2182 mutex_unlock(&sde_crtc->crtc_lock);
Dhaval Patelf9245d62017-03-28 16:24:00 -07002183 _sde_crtc_power_enable(sde_crtc, false);
Lloyd Atkinson2c554eb2017-05-24 16:22:39 -04002184 mutex_lock(&sde_crtc->crtc_lock);
Clarence Ip7f70ce42017-03-20 06:53:46 -07002185 }
2186}
2187
2188/**
2189 * _sde_crtc_set_suspend - notify crtc of suspend enable/disable
2190 * @crtc: Pointer to drm crtc object
2191 * @enable: true to enable suspend, false to indicate resume
2192 */
2193static void _sde_crtc_set_suspend(struct drm_crtc *crtc, bool enable)
2194{
2195 struct sde_crtc *sde_crtc;
2196 struct msm_drm_private *priv;
2197 struct sde_kms *sde_kms;
Gopikrishnaiah Anandan84b4f672017-04-26 10:28:51 -07002198 struct drm_event event;
2199 u32 power_on;
Clarence Ip7f70ce42017-03-20 06:53:46 -07002200
2201 if (!crtc || !crtc->dev || !crtc->dev->dev_private) {
2202 SDE_ERROR("invalid crtc\n");
2203 return;
2204 }
2205 sde_crtc = to_sde_crtc(crtc);
2206 priv = crtc->dev->dev_private;
2207
2208 if (!priv->kms) {
2209 SDE_ERROR("invalid crtc kms\n");
2210 return;
2211 }
2212 sde_kms = to_sde_kms(priv->kms);
2213
2214 SDE_DEBUG("crtc%d suspend = %d\n", crtc->base.id, enable);
2215
2216 mutex_lock(&sde_crtc->crtc_lock);
2217
Gopikrishnaiah Anandan84b4f672017-04-26 10:28:51 -07002218 event.type = DRM_EVENT_CRTC_POWER;
2219 event.length = sizeof(u32);
Clarence Ip7f70ce42017-03-20 06:53:46 -07002220 /*
Clarence Ip2f9beeb2017-03-16 11:04:53 -04002221 * Update CP on suspend/resume transitions
2222 */
Gopikrishnaiah Anandan84b4f672017-04-26 10:28:51 -07002223 if (enable && !sde_crtc->suspend) {
Clarence Ip2f9beeb2017-03-16 11:04:53 -04002224 sde_cp_crtc_suspend(crtc);
Gopikrishnaiah Anandan84b4f672017-04-26 10:28:51 -07002225 power_on = 0;
2226 } else if (!enable && sde_crtc->suspend) {
Clarence Ip2f9beeb2017-03-16 11:04:53 -04002227 sde_cp_crtc_resume(crtc);
Gopikrishnaiah Anandan84b4f672017-04-26 10:28:51 -07002228 power_on = 1;
2229 }
Clarence Ip2f9beeb2017-03-16 11:04:53 -04002230
2231 /*
Clarence Ip7f70ce42017-03-20 06:53:46 -07002232 * If the vblank refcount != 0, release a power reference on suspend
2233 * and take it back during resume (if it is still != 0).
2234 */
2235 if (sde_crtc->suspend == enable)
2236 SDE_DEBUG("crtc%d suspend already set to %d, ignoring update\n",
2237 crtc->base.id, enable);
2238 else if (atomic_read(&sde_crtc->vblank_refcount) != 0)
2239 _sde_crtc_vblank_enable_nolock(sde_crtc, !enable);
2240
2241 sde_crtc->suspend = enable;
Gopikrishnaiah Anandan84b4f672017-04-26 10:28:51 -07002242 msm_mode_object_event_nofity(&crtc->base, crtc->dev, &event,
2243 (u8 *)&power_on);
Clarence Ip7f70ce42017-03-20 06:53:46 -07002244 mutex_unlock(&sde_crtc->crtc_lock);
2245}
2246
2247/**
Clarence Ip7a753bb2016-07-07 11:47:44 -04002248 * sde_crtc_duplicate_state - state duplicate hook
2249 * @crtc: Pointer to drm crtc structure
2250 * @Returns: Pointer to new drm_crtc_state structure
2251 */
2252static struct drm_crtc_state *sde_crtc_duplicate_state(struct drm_crtc *crtc)
2253{
2254 struct sde_crtc *sde_crtc;
2255 struct sde_crtc_state *cstate, *old_cstate;
2256
2257 if (!crtc || !crtc->state) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07002258 SDE_ERROR("invalid argument(s)\n");
Clarence Ip7a753bb2016-07-07 11:47:44 -04002259 return NULL;
2260 }
2261
2262 sde_crtc = to_sde_crtc(crtc);
2263 old_cstate = to_sde_crtc_state(crtc->state);
2264 cstate = msm_property_alloc_state(&sde_crtc->property_info);
2265 if (!cstate) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07002266 SDE_ERROR("failed to allocate state\n");
Clarence Ip7a753bb2016-07-07 11:47:44 -04002267 return NULL;
2268 }
2269
2270 /* duplicate value helper */
2271 msm_property_duplicate_state(&sde_crtc->property_info,
2272 old_cstate, cstate,
2273 cstate->property_values, cstate->property_blobs);
2274
2275 /* duplicate base helper */
2276 __drm_atomic_helper_crtc_duplicate_state(crtc, &cstate->base);
2277
Alan Kwongcdb2f282017-03-18 13:42:06 -07002278 _sde_crtc_rp_duplicate(&old_cstate->rp, &cstate->rp);
2279
Clarence Ip7a753bb2016-07-07 11:47:44 -04002280 return &cstate->base;
2281}
2282
2283/**
2284 * sde_crtc_reset - reset hook for CRTCs
2285 * Resets the atomic state for @crtc by freeing the state pointer (which might
2286 * be NULL, e.g. at driver load time) and allocating a new empty state object.
2287 * @crtc: Pointer to drm crtc structure
2288 */
2289static void sde_crtc_reset(struct drm_crtc *crtc)
2290{
2291 struct sde_crtc *sde_crtc;
2292 struct sde_crtc_state *cstate;
2293
2294 if (!crtc) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07002295 SDE_ERROR("invalid crtc\n");
Clarence Ip7a753bb2016-07-07 11:47:44 -04002296 return;
2297 }
2298
Clarence Ip7f70ce42017-03-20 06:53:46 -07002299 /* revert suspend actions, if necessary */
2300 if (msm_is_suspend_state(crtc->dev))
2301 _sde_crtc_set_suspend(crtc, false);
2302
Clarence Ip7a753bb2016-07-07 11:47:44 -04002303 /* remove previous state, if present */
2304 if (crtc->state) {
2305 sde_crtc_destroy_state(crtc, crtc->state);
2306 crtc->state = 0;
2307 }
2308
2309 sde_crtc = to_sde_crtc(crtc);
2310 cstate = msm_property_alloc_state(&sde_crtc->property_info);
2311 if (!cstate) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07002312 SDE_ERROR("failed to allocate state\n");
Clarence Ip7a753bb2016-07-07 11:47:44 -04002313 return;
2314 }
2315
2316 /* reset value helper */
2317 msm_property_reset_state(&sde_crtc->property_info, cstate,
2318 cstate->property_values, cstate->property_blobs);
2319
Clarence Ipcae1bb62016-07-07 12:07:13 -04002320 _sde_crtc_set_input_fence_timeout(cstate);
2321
Alan Kwongcdb2f282017-03-18 13:42:06 -07002322 _sde_crtc_rp_reset(&cstate->rp);
2323
Clarence Ip7a753bb2016-07-07 11:47:44 -04002324 cstate->base.crtc = crtc;
2325 crtc->state = &cstate->base;
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07002326}
2327
Clarence Ip9728a1d2017-04-18 22:22:13 -04002328static int _sde_crtc_vblank_no_lock(struct sde_crtc *sde_crtc, bool en)
2329{
2330 if (!sde_crtc) {
2331 SDE_ERROR("invalid crtc\n");
2332 return -EINVAL;
2333 } else if (en && atomic_inc_return(&sde_crtc->vblank_refcount) == 1) {
2334 SDE_DEBUG("crtc%d vblank enable\n", sde_crtc->base.base.id);
2335 if (!sde_crtc->suspend)
2336 _sde_crtc_vblank_enable_nolock(sde_crtc, true);
2337 } else if (!en && atomic_read(&sde_crtc->vblank_refcount) < 1) {
2338 SDE_ERROR("crtc%d invalid vblank disable\n",
2339 sde_crtc->base.base.id);
2340 return -EINVAL;
2341 } else if (!en && atomic_dec_return(&sde_crtc->vblank_refcount) == 0) {
2342 SDE_DEBUG("crtc%d vblank disable\n", sde_crtc->base.base.id);
2343 if (!sde_crtc->suspend)
2344 _sde_crtc_vblank_enable_nolock(sde_crtc, false);
2345 } else {
2346 SDE_DEBUG("crtc%d vblank %s refcount:%d\n",
2347 sde_crtc->base.base.id,
2348 en ? "enable" : "disable",
2349 atomic_read(&sde_crtc->vblank_refcount));
2350 }
2351
2352 return 0;
2353}
2354
Veera Sundaram Sankaran82916e02017-03-29 18:44:22 -07002355static void sde_crtc_handle_power_event(u32 event_type, void *arg)
2356{
2357 struct drm_crtc *crtc = arg;
2358 struct sde_crtc *sde_crtc;
2359 struct drm_encoder *encoder;
2360
2361 if (!crtc) {
2362 SDE_ERROR("invalid crtc\n");
2363 return;
2364 }
2365 sde_crtc = to_sde_crtc(crtc);
2366
2367 mutex_lock(&sde_crtc->crtc_lock);
2368
2369 SDE_EVT32(DRMID(crtc), event_type);
2370
2371 if (event_type == SDE_POWER_EVENT_POST_ENABLE) {
2372 /* restore encoder; crtc will be programmed during commit */
2373 drm_for_each_encoder(encoder, crtc->dev) {
2374 if (encoder->crtc != crtc)
2375 continue;
2376
2377 sde_encoder_virt_restore(encoder);
2378 }
2379
2380 } else if (event_type == SDE_POWER_EVENT_POST_DISABLE) {
2381 struct drm_plane *plane;
2382
2383 /*
2384 * set revalidate flag in planes, so it will be re-programmed
2385 * in the next frame update
2386 */
2387 drm_atomic_crtc_for_each_plane(plane, crtc)
2388 sde_plane_set_revalidate(plane, true);
2389 }
2390
2391 mutex_unlock(&sde_crtc->crtc_lock);
2392}
2393
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002394static void sde_crtc_disable(struct drm_crtc *crtc)
2395{
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04002396 struct sde_crtc *sde_crtc;
Dhaval Patel82c8dbc2017-02-18 23:15:10 -08002397 struct sde_crtc_state *cstate;
Alan Kwong07da0982016-11-04 12:57:45 -04002398 struct drm_encoder *encoder;
Veera Sundaram Sankaran82916e02017-03-29 18:44:22 -07002399 struct msm_drm_private *priv;
Gopikrishnaiah Anandande2c81b2017-03-15 12:41:29 -07002400 unsigned long flags;
2401 struct sde_crtc_irq_info *node = NULL;
2402 int ret;
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04002403
Veera Sundaram Sankaran82916e02017-03-29 18:44:22 -07002404 if (!crtc || !crtc->dev || !crtc->dev->dev_private || !crtc->state) {
Lloyd Atkinson4f1c8692016-09-14 14:04:25 -04002405 SDE_ERROR("invalid crtc\n");
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04002406 return;
2407 }
2408 sde_crtc = to_sde_crtc(crtc);
Dhaval Patel82c8dbc2017-02-18 23:15:10 -08002409 cstate = to_sde_crtc_state(crtc->state);
Veera Sundaram Sankaran82916e02017-03-29 18:44:22 -07002410 priv = crtc->dev->dev_private;
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04002411
Alan Kwong163d2612016-11-03 00:56:56 -04002412 SDE_DEBUG("crtc%d\n", crtc->base.id);
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04002413
Clarence Ip7f70ce42017-03-20 06:53:46 -07002414 if (msm_is_suspend_state(crtc->dev))
2415 _sde_crtc_set_suspend(crtc, true);
2416
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07002417 mutex_lock(&sde_crtc->crtc_lock);
Alan Kwong628d19e2016-10-31 13:50:13 -04002418 SDE_EVT32(DRMID(crtc));
2419
Clarence Ip7f70ce42017-03-20 06:53:46 -07002420 if (atomic_read(&sde_crtc->vblank_refcount) && !sde_crtc->suspend) {
Alan Kwong628d19e2016-10-31 13:50:13 -04002421 SDE_ERROR("crtc%d invalid vblank refcount\n",
2422 crtc->base.id);
Dhaval Patel6c666622017-03-21 23:02:59 -07002423 SDE_EVT32(DRMID(crtc), atomic_read(&sde_crtc->vblank_refcount),
2424 SDE_EVTLOG_FUNC_CASE1);
Clarence Ip9728a1d2017-04-18 22:22:13 -04002425 while (atomic_read(&sde_crtc->vblank_refcount))
2426 if (_sde_crtc_vblank_no_lock(sde_crtc, false))
2427 break;
Alan Kwong07da0982016-11-04 12:57:45 -04002428 }
2429
Alan Kwong628d19e2016-10-31 13:50:13 -04002430 if (atomic_read(&sde_crtc->frame_pending)) {
2431 /* release bandwidth and other resources */
Ingrid Gallardo79b44392017-05-30 16:30:52 -07002432 SDE_ERROR("crtc%d invalid frame pending\n", crtc->base.id);
Dhaval Patel6c666622017-03-21 23:02:59 -07002433 SDE_EVT32(DRMID(crtc), atomic_read(&sde_crtc->frame_pending),
2434 SDE_EVTLOG_FUNC_CASE2);
Alan Kwong9aa061c2016-11-06 21:17:12 -05002435 sde_core_perf_crtc_release_bw(crtc);
Alan Kwong628d19e2016-10-31 13:50:13 -04002436 atomic_set(&sde_crtc->frame_pending, 0);
2437 }
2438
Alan Kwong9aa061c2016-11-06 21:17:12 -05002439 sde_core_perf_crtc_update(crtc, 0, true);
2440
Alan Kwong628d19e2016-10-31 13:50:13 -04002441 drm_for_each_encoder(encoder, crtc->dev) {
2442 if (encoder->crtc != crtc)
2443 continue;
2444 sde_encoder_register_frame_event_callback(encoder, NULL, NULL);
Dhaval Patel82c8dbc2017-02-18 23:15:10 -08002445 cstate->rsc_client = NULL;
2446 cstate->rsc_update = false;
Alan Kwong628d19e2016-10-31 13:50:13 -04002447 }
2448
Veera Sundaram Sankaran82916e02017-03-29 18:44:22 -07002449 if (sde_crtc->power_event)
2450 sde_power_handle_unregister_event(&priv->phandle,
2451 sde_crtc->power_event);
2452
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04002453 memset(sde_crtc->mixers, 0, sizeof(sde_crtc->mixers));
2454 sde_crtc->num_mixers = 0;
Gopikrishnaiah Anandande2c81b2017-03-15 12:41:29 -07002455
2456 spin_lock_irqsave(&sde_crtc->spin_lock, flags);
2457 list_for_each_entry(node, &sde_crtc->user_event_list, list) {
2458 ret = 0;
2459 if (node->func)
2460 ret = node->func(crtc, false, &node->irq);
2461 if (ret)
2462 SDE_ERROR("%s failed to disable event %x\n",
2463 sde_crtc->name, node->event);
2464 }
2465 spin_unlock_irqrestore(&sde_crtc->spin_lock, flags);
2466
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07002467 mutex_unlock(&sde_crtc->crtc_lock);
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002468}
2469
2470static void sde_crtc_enable(struct drm_crtc *crtc)
2471{
Clarence Ipcae1bb62016-07-07 12:07:13 -04002472 struct sde_crtc *sde_crtc;
Alan Kwong628d19e2016-10-31 13:50:13 -04002473 struct drm_encoder *encoder;
Veera Sundaram Sankaran82916e02017-03-29 18:44:22 -07002474 struct msm_drm_private *priv;
Gopikrishnaiah Anandande2c81b2017-03-15 12:41:29 -07002475 unsigned long flags;
2476 struct sde_crtc_irq_info *node = NULL;
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05002477 int ret;
Lloyd Atkinsonaf7952d2016-06-26 22:41:26 -04002478
Veera Sundaram Sankaran82916e02017-03-29 18:44:22 -07002479 if (!crtc || !crtc->dev || !crtc->dev->dev_private) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07002480 SDE_ERROR("invalid crtc\n");
Clarence Ipcae1bb62016-07-07 12:07:13 -04002481 return;
2482 }
Veera Sundaram Sankaran82916e02017-03-29 18:44:22 -07002483 priv = crtc->dev->dev_private;
Clarence Ipcae1bb62016-07-07 12:07:13 -04002484
Alan Kwong163d2612016-11-03 00:56:56 -04002485 SDE_DEBUG("crtc%d\n", crtc->base.id);
Alan Kwong628d19e2016-10-31 13:50:13 -04002486 SDE_EVT32(DRMID(crtc));
Clarence Ipcae1bb62016-07-07 12:07:13 -04002487 sde_crtc = to_sde_crtc(crtc);
Lloyd Atkinsonaf7952d2016-06-26 22:41:26 -04002488
Alan Kwong628d19e2016-10-31 13:50:13 -04002489 drm_for_each_encoder(encoder, crtc->dev) {
2490 if (encoder->crtc != crtc)
2491 continue;
2492 sde_encoder_register_frame_event_callback(encoder,
2493 sde_crtc_frame_event_cb, (void *)crtc);
2494 }
2495
Gopikrishnaiah Anandande2c81b2017-03-15 12:41:29 -07002496 spin_lock_irqsave(&sde_crtc->spin_lock, flags);
2497 list_for_each_entry(node, &sde_crtc->user_event_list, list) {
2498 ret = 0;
2499 if (node->func)
2500 ret = node->func(crtc, true, &node->irq);
2501 if (ret)
2502 SDE_ERROR("%s failed to enable event %x\n",
2503 sde_crtc->name, node->event);
2504 }
2505 spin_unlock_irqrestore(&sde_crtc->spin_lock, flags);
Veera Sundaram Sankaran82916e02017-03-29 18:44:22 -07002506
2507 sde_crtc->power_event = sde_power_handle_register_event(
2508 &priv->phandle,
2509 SDE_POWER_EVENT_POST_ENABLE | SDE_POWER_EVENT_POST_DISABLE,
2510 sde_crtc_handle_power_event, crtc, sde_crtc->name);
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002511}
2512
2513struct plane_state {
Dhaval Patelec10fad2016-08-22 14:40:48 -07002514 struct sde_plane_state *sde_pstate;
Dhaval Patel04c7e8e2016-09-26 20:14:31 -07002515 const struct drm_plane_state *drm_pstate;
Clarence Ipc47a0692016-10-11 10:54:17 -04002516 int stage;
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08002517 u32 pipe_id;
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002518};
2519
Clarence Ipc47a0692016-10-11 10:54:17 -04002520static int pstate_cmp(const void *a, const void *b)
2521{
2522 struct plane_state *pa = (struct plane_state *)a;
2523 struct plane_state *pb = (struct plane_state *)b;
2524 int rc = 0;
2525 int pa_zpos, pb_zpos;
2526
2527 pa_zpos = sde_plane_get_property(pa->sde_pstate, PLANE_PROP_ZPOS);
2528 pb_zpos = sde_plane_get_property(pb->sde_pstate, PLANE_PROP_ZPOS);
2529
2530 if (pa_zpos != pb_zpos)
2531 rc = pa_zpos - pb_zpos;
2532 else
2533 rc = pa->drm_pstate->crtc_x - pb->drm_pstate->crtc_x;
2534
2535 return rc;
2536}
2537
Dhaval Patela8d6bc62017-05-10 17:40:18 -07002538static int _sde_crtc_excl_rect_overlap_check(struct plane_state pstates[],
2539 int cnt, int curr_cnt, struct sde_rect *excl_rect, int z_pos)
2540{
2541 struct sde_rect dst_rect, intersect;
2542 int i, rc = -EINVAL;
2543 const struct drm_plane_state *pstate;
2544
2545 /* start checking from next plane */
2546 for (i = curr_cnt; i < cnt; i++) {
2547 pstate = pstates[i].drm_pstate;
2548 POPULATE_RECT(&dst_rect, pstate->crtc_x, pstate->crtc_y,
2549 pstate->crtc_w, pstate->crtc_h, true);
2550 sde_kms_rect_intersect(&dst_rect, excl_rect, &intersect);
2551
2552 if (intersect.w == excl_rect->w && intersect.h == excl_rect->h
2553 /* next plane may be on same z-order */
2554 && z_pos != pstates[i].stage) {
2555 rc = 0;
2556 goto end;
2557 }
2558 }
2559
2560 SDE_ERROR("excl rect does not find top overlapping rect\n");
2561end:
2562 return rc;
2563}
2564
2565/* no input validation - caller API has all the checks */
2566static int _sde_crtc_excl_dim_layer_check(struct drm_crtc_state *state,
2567 struct plane_state pstates[], int cnt)
2568{
2569 struct sde_crtc_state *cstate = to_sde_crtc_state(state);
2570 struct drm_display_mode *mode = &state->adjusted_mode;
2571 const struct drm_plane_state *pstate;
2572 struct sde_plane_state *sde_pstate;
2573 int rc = 0, i;
2574
2575 /* Check dim layer rect bounds and stage */
2576 for (i = 0; i < cstate->num_dim_layers; i++) {
2577 if ((CHECK_LAYER_BOUNDS(cstate->dim_layer[i].rect.y,
2578 cstate->dim_layer[i].rect.h, mode->vdisplay)) ||
2579 (CHECK_LAYER_BOUNDS(cstate->dim_layer[i].rect.x,
2580 cstate->dim_layer[i].rect.w, mode->hdisplay)) ||
2581 (cstate->dim_layer[i].stage >= SDE_STAGE_MAX) ||
2582 (!cstate->dim_layer[i].rect.w) ||
2583 (!cstate->dim_layer[i].rect.h)) {
2584 SDE_ERROR("invalid dim_layer:{%d,%d,%d,%d}, stage:%d\n",
2585 cstate->dim_layer[i].rect.x,
2586 cstate->dim_layer[i].rect.y,
2587 cstate->dim_layer[i].rect.w,
2588 cstate->dim_layer[i].rect.h,
2589 cstate->dim_layer[i].stage);
2590 SDE_ERROR("display: %dx%d\n", mode->hdisplay,
2591 mode->vdisplay);
2592 rc = -E2BIG;
2593 goto end;
2594 }
2595 }
2596
2597 /* this is traversing on sorted z-order pstates */
2598 for (i = 0; i < cnt; i++) {
2599 pstate = pstates[i].drm_pstate;
2600 sde_pstate = to_sde_plane_state(pstate);
2601 if (sde_pstate->excl_rect.w && sde_pstate->excl_rect.h) {
2602 /* check overlap on all top z-order */
2603 rc = _sde_crtc_excl_rect_overlap_check(pstates, cnt,
2604 i + 1, &sde_pstate->excl_rect, pstates[i].stage);
2605 if (rc)
2606 goto end;
2607 }
2608 }
2609
2610end:
2611 return rc;
2612}
2613
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002614static int sde_crtc_atomic_check(struct drm_crtc *crtc,
2615 struct drm_crtc_state *state)
2616{
Clarence Ipcae1bb62016-07-07 12:07:13 -04002617 struct sde_crtc *sde_crtc;
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08002618 struct plane_state pstates[SDE_STAGE_MAX * 4];
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08002619 struct sde_crtc_state *cstate;
Dhaval Patelec10fad2016-08-22 14:40:48 -07002620
Dhaval Patel04c7e8e2016-09-26 20:14:31 -07002621 const struct drm_plane_state *pstate;
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002622 struct drm_plane *plane;
Dhaval Patelec10fad2016-08-22 14:40:48 -07002623 struct drm_display_mode *mode;
2624
2625 int cnt = 0, rc = 0, mixer_width, i, z_pos;
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08002626
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08002627 struct sde_multirect_plane_states multirect_plane[SDE_STAGE_MAX * 2];
2628 int multirect_count = 0;
2629 const struct drm_plane_state *pipe_staged[SSPP_MAX];
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04002630 int left_zpos_cnt = 0, right_zpos_cnt = 0;
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002631
Clarence Ipcae1bb62016-07-07 12:07:13 -04002632 if (!crtc) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07002633 SDE_ERROR("invalid crtc\n");
Clarence Ipcae1bb62016-07-07 12:07:13 -04002634 return -EINVAL;
2635 }
2636
Alan Kwongcdb2f282017-03-18 13:42:06 -07002637 sde_crtc = to_sde_crtc(crtc);
2638 cstate = to_sde_crtc_state(state);
2639
Lloyd Atkinson5217336c2016-09-15 18:21:18 -04002640 if (!state->enable || !state->active) {
2641 SDE_DEBUG("crtc%d -> enable %d, active %d, skip atomic_check\n",
2642 crtc->base.id, state->enable, state->active);
Alan Kwongcdb2f282017-03-18 13:42:06 -07002643 goto end;
Lloyd Atkinson5217336c2016-09-15 18:21:18 -04002644 }
2645
Dhaval Patelec10fad2016-08-22 14:40:48 -07002646 mode = &state->adjusted_mode;
2647 SDE_DEBUG("%s: check", sde_crtc->name);
Clarence Ipcae1bb62016-07-07 12:07:13 -04002648
Clarence Ip90b282d2017-05-04 10:00:32 -07002649 /* force a full mode set if active state changed */
2650 if (state->active_changed)
2651 state->mode_changed = true;
2652
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08002653 memset(pipe_staged, 0, sizeof(pipe_staged));
2654
Dhaval Patelec10fad2016-08-22 14:40:48 -07002655 mixer_width = sde_crtc_mixer_width(sde_crtc, mode);
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002656
Lloyd Atkinson66e7dde2017-02-08 15:52:53 -05002657 _sde_crtc_setup_is_ppsplit(state);
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05002658 _sde_crtc_setup_lm_bounds(crtc, state);
2659
Dhaval Patelec10fad2016-08-22 14:40:48 -07002660 /* get plane state for all drm planes associated with crtc state */
Dhaval Patel04c7e8e2016-09-26 20:14:31 -07002661 drm_atomic_crtc_state_for_each_plane_state(plane, pstate, state) {
Clarence Ipc47a0692016-10-11 10:54:17 -04002662 if (IS_ERR_OR_NULL(pstate)) {
2663 rc = PTR_ERR(pstate);
2664 SDE_ERROR("%s: failed to get plane%d state, %d\n",
2665 sde_crtc->name, plane->base.id, rc);
Alan Kwong85767282016-10-03 18:03:37 -04002666 goto end;
2667 }
Clarence Ipc47a0692016-10-11 10:54:17 -04002668 if (cnt >= ARRAY_SIZE(pstates))
2669 continue;
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002670
Dhaval Patelec10fad2016-08-22 14:40:48 -07002671 pstates[cnt].sde_pstate = to_sde_plane_state(pstate);
2672 pstates[cnt].drm_pstate = pstate;
Clarence Ipc47a0692016-10-11 10:54:17 -04002673 pstates[cnt].stage = sde_plane_get_property(
2674 pstates[cnt].sde_pstate, PLANE_PROP_ZPOS);
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08002675 pstates[cnt].pipe_id = sde_plane_pipe(plane);
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08002676
2677 /* check dim layer stage with every plane */
2678 for (i = 0; i < cstate->num_dim_layers; i++) {
2679 if (pstates[cnt].stage == cstate->dim_layer[i].stage) {
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -07002680 SDE_ERROR(
2681 "plane:%d/dim_layer:%i-same stage:%d\n",
2682 plane->base.id, i,
2683 cstate->dim_layer[i].stage);
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08002684 rc = -EINVAL;
2685 goto end;
2686 }
2687 }
2688
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08002689 if (pipe_staged[pstates[cnt].pipe_id]) {
2690 multirect_plane[multirect_count].r0 =
2691 pipe_staged[pstates[cnt].pipe_id];
2692 multirect_plane[multirect_count].r1 = pstate;
2693 multirect_count++;
2694
2695 pipe_staged[pstates[cnt].pipe_id] = NULL;
2696 } else {
2697 pipe_staged[pstates[cnt].pipe_id] = pstate;
2698 }
2699
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002700 cnt++;
Dhaval Patelec10fad2016-08-22 14:40:48 -07002701
2702 if (CHECK_LAYER_BOUNDS(pstate->crtc_y, pstate->crtc_h,
2703 mode->vdisplay) ||
2704 CHECK_LAYER_BOUNDS(pstate->crtc_x, pstate->crtc_w,
2705 mode->hdisplay)) {
2706 SDE_ERROR("invalid vertical/horizontal destination\n");
2707 SDE_ERROR("y:%d h:%d vdisp:%d x:%d w:%d hdisp:%d\n",
2708 pstate->crtc_y, pstate->crtc_h, mode->vdisplay,
2709 pstate->crtc_x, pstate->crtc_w, mode->hdisplay);
2710 rc = -E2BIG;
2711 goto end;
2712 }
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002713 }
2714
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08002715 for (i = 1; i < SSPP_MAX; i++) {
Jeykumar Sankarane964dc72017-05-10 19:26:43 -07002716 if (pipe_staged[i]) {
2717 sde_plane_clear_multirect(pipe_staged[i]);
2718
2719 if (is_sde_plane_virtual(pipe_staged[i]->plane)) {
2720 SDE_ERROR("invalid use of virtual plane: %d\n",
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08002721 pipe_staged[i]->plane->base.id);
Jeykumar Sankarane964dc72017-05-10 19:26:43 -07002722 goto end;
2723 }
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08002724 }
2725 }
2726
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04002727 /* assign mixer stages based on sorted zpos property */
2728 sort(pstates, cnt, sizeof(pstates[0]), pstate_cmp, NULL);
2729
Dhaval Patela8d6bc62017-05-10 17:40:18 -07002730 rc = _sde_crtc_excl_dim_layer_check(state, pstates, cnt);
2731 if (rc)
2732 goto end;
2733
Clarence Ipc47a0692016-10-11 10:54:17 -04002734 if (!sde_is_custom_client()) {
2735 int stage_old = pstates[0].stage;
Dhaval Patelec10fad2016-08-22 14:40:48 -07002736
Clarence Ipc47a0692016-10-11 10:54:17 -04002737 z_pos = 0;
2738 for (i = 0; i < cnt; i++) {
2739 if (stage_old != pstates[i].stage)
2740 ++z_pos;
2741 stage_old = pstates[i].stage;
2742 pstates[i].stage = z_pos;
2743 }
2744 }
2745
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04002746 z_pos = -1;
Clarence Ipc47a0692016-10-11 10:54:17 -04002747 for (i = 0; i < cnt; i++) {
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04002748 /* reset counts at every new blend stage */
2749 if (pstates[i].stage != z_pos) {
2750 left_zpos_cnt = 0;
2751 right_zpos_cnt = 0;
2752 z_pos = pstates[i].stage;
2753 }
Clarence Ipc47a0692016-10-11 10:54:17 -04002754
2755 /* verify z_pos setting before using it */
Clarence Ip649989a2016-10-21 14:28:34 -04002756 if (z_pos >= SDE_STAGE_MAX - SDE_STAGE_0) {
Clarence Ipc47a0692016-10-11 10:54:17 -04002757 SDE_ERROR("> %d plane stages assigned\n",
2758 SDE_STAGE_MAX - SDE_STAGE_0);
2759 rc = -EINVAL;
2760 goto end;
2761 } else if (pstates[i].drm_pstate->crtc_x < mixer_width) {
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04002762 if (left_zpos_cnt == 2) {
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08002763 SDE_ERROR("> 2 planes @ stage %d on left\n",
Dhaval Patelec10fad2016-08-22 14:40:48 -07002764 z_pos);
2765 rc = -EINVAL;
2766 goto end;
2767 }
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04002768 left_zpos_cnt++;
2769
Dhaval Patelec10fad2016-08-22 14:40:48 -07002770 } else {
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04002771 if (right_zpos_cnt == 2) {
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08002772 SDE_ERROR("> 2 planes @ stage %d on right\n",
Dhaval Patelec10fad2016-08-22 14:40:48 -07002773 z_pos);
2774 rc = -EINVAL;
2775 goto end;
2776 }
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04002777 right_zpos_cnt++;
Dhaval Patelec10fad2016-08-22 14:40:48 -07002778 }
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04002779
Clarence Ipc47a0692016-10-11 10:54:17 -04002780 pstates[i].sde_pstate->stage = z_pos + SDE_STAGE_0;
Dhaval Patelec10fad2016-08-22 14:40:48 -07002781 SDE_DEBUG("%s: zpos %d", sde_crtc->name, z_pos);
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002782 }
2783
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08002784 for (i = 0; i < multirect_count; i++) {
2785 if (sde_plane_validate_multirect_v2(&multirect_plane[i])) {
2786 SDE_ERROR(
2787 "multirect validation failed for planes (%d - %d)\n",
2788 multirect_plane[i].r0->plane->base.id,
2789 multirect_plane[i].r1->plane->base.id);
2790 rc = -EINVAL;
Alan Kwong9aa061c2016-11-06 21:17:12 -05002791 goto end;
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08002792 }
2793 }
2794
Alan Kwong9aa061c2016-11-06 21:17:12 -05002795 rc = sde_core_perf_crtc_check(crtc, state);
2796 if (rc) {
2797 SDE_ERROR("crtc%d failed performance check %d\n",
2798 crtc->base.id, rc);
2799 goto end;
2800 }
2801
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04002802 /*
2803 * enforce pipe priority restrictions
2804 * use pstates sorted by stage to check planes on same stage
2805 * we assume that all pipes are in source split so its valid to compare
2806 * without taking into account left/right mixer placement
2807 */
2808 for (i = 1; i < cnt; i++) {
2809 struct plane_state *prv_pstate, *cur_pstate;
2810 int32_t prv_x, cur_x, prv_id, cur_id;
2811
2812 prv_pstate = &pstates[i - 1];
2813 cur_pstate = &pstates[i];
2814 if (prv_pstate->stage != cur_pstate->stage)
2815 continue;
2816
2817 prv_x = prv_pstate->drm_pstate->crtc_x;
2818 cur_x = cur_pstate->drm_pstate->crtc_x;
2819 prv_id = prv_pstate->sde_pstate->base.plane->base.id;
2820 cur_id = cur_pstate->sde_pstate->base.plane->base.id;
2821
2822 /*
2823 * Planes are enumerated in pipe-priority order such that planes
2824 * with lower drm_id must be left-most in a shared blend-stage
2825 * when using source split.
2826 */
2827 if (cur_x > prv_x && cur_id < prv_id) {
2828 SDE_ERROR(
2829 "shared z_pos %d lower id plane%d @ x%d should be left of plane%d @ x %d\n",
2830 cur_pstate->stage, cur_id, cur_x,
2831 prv_id, prv_x);
2832 rc = -EINVAL;
2833 goto end;
2834 } else if (cur_x < prv_x && cur_id > prv_id) {
2835 SDE_ERROR(
2836 "shared z_pos %d lower id plane%d @ x%d should be left of plane%d @ x %d\n",
2837 cur_pstate->stage, prv_id, prv_x,
2838 cur_id, cur_x);
2839 rc = -EINVAL;
2840 goto end;
2841 }
2842 }
2843
Lloyd Atkinson8ba47032017-03-22 17:13:32 -04002844 rc = _sde_crtc_check_rois(crtc, state);
2845 if (rc) {
2846 SDE_ERROR("crtc%d failed roi check %d\n", crtc->base.id, rc);
2847 goto end;
2848 }
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04002849
Dhaval Patelec10fad2016-08-22 14:40:48 -07002850end:
Alan Kwongcdb2f282017-03-18 13:42:06 -07002851 _sde_crtc_rp_free_unused(&cstate->rp);
Dhaval Patelec10fad2016-08-22 14:40:48 -07002852 return rc;
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002853}
2854
Abhijit Kulkarni7acb3262016-07-05 15:27:25 -04002855int sde_crtc_vblank(struct drm_crtc *crtc, bool en)
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07002856{
Clarence Ip7f70ce42017-03-20 06:53:46 -07002857 struct sde_crtc *sde_crtc;
Clarence Ip9728a1d2017-04-18 22:22:13 -04002858 int rc;
Abhijit Kulkarni7acb3262016-07-05 15:27:25 -04002859
Clarence Ip7f70ce42017-03-20 06:53:46 -07002860 if (!crtc) {
2861 SDE_ERROR("invalid crtc\n");
2862 return -EINVAL;
2863 }
2864 sde_crtc = to_sde_crtc(crtc);
2865
2866 mutex_lock(&sde_crtc->crtc_lock);
Clarence Ip9728a1d2017-04-18 22:22:13 -04002867 rc = _sde_crtc_vblank_no_lock(sde_crtc, en);
Clarence Ip7f70ce42017-03-20 06:53:46 -07002868 mutex_unlock(&sde_crtc->crtc_lock);
Clarence Ip9728a1d2017-04-18 22:22:13 -04002869
Clarence Ip7f70ce42017-03-20 06:53:46 -07002870 return rc;
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07002871}
2872
Lloyd Atkinson5217336c2016-09-15 18:21:18 -04002873void sde_crtc_cancel_pending_flip(struct drm_crtc *crtc, struct drm_file *file)
2874{
2875 struct sde_crtc *sde_crtc = to_sde_crtc(crtc);
2876
Alan Kwong163d2612016-11-03 00:56:56 -04002877 SDE_DEBUG("%s: cancel: %p\n", sde_crtc->name, file);
Lloyd Atkinson4f1c8692016-09-14 14:04:25 -04002878 _sde_crtc_complete_flip(crtc, file);
Lloyd Atkinson5217336c2016-09-15 18:21:18 -04002879}
2880
Clarence Ip7a753bb2016-07-07 11:47:44 -04002881/**
2882 * sde_crtc_install_properties - install all drm properties for crtc
2883 * @crtc: Pointer to drm crtc structure
2884 */
Dhaval Patele4a5dda2016-10-13 19:29:30 -07002885static void sde_crtc_install_properties(struct drm_crtc *crtc,
2886 struct sde_mdss_cfg *catalog)
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07002887{
Clarence Ip7a753bb2016-07-07 11:47:44 -04002888 struct sde_crtc *sde_crtc;
2889 struct drm_device *dev;
Dhaval Patele4a5dda2016-10-13 19:29:30 -07002890 struct sde_kms_info *info;
Alan Kwong9aa061c2016-11-06 21:17:12 -05002891 struct sde_kms *sde_kms;
Clarence Ip7a753bb2016-07-07 11:47:44 -04002892
Lloyd Atkinson4f1c8692016-09-14 14:04:25 -04002893 SDE_DEBUG("\n");
Clarence Ip7a753bb2016-07-07 11:47:44 -04002894
Dhaval Patele4a5dda2016-10-13 19:29:30 -07002895 if (!crtc || !catalog) {
2896 SDE_ERROR("invalid crtc or catalog\n");
Clarence Ip7a753bb2016-07-07 11:47:44 -04002897 return;
2898 }
2899
2900 sde_crtc = to_sde_crtc(crtc);
2901 dev = crtc->dev;
Alan Kwong9aa061c2016-11-06 21:17:12 -05002902 sde_kms = _sde_crtc_get_kms(crtc);
Clarence Ip7a753bb2016-07-07 11:47:44 -04002903
Dhaval Patele4a5dda2016-10-13 19:29:30 -07002904 info = kzalloc(sizeof(struct sde_kms_info), GFP_KERNEL);
2905 if (!info) {
2906 SDE_ERROR("failed to allocate info memory\n");
2907 return;
2908 }
2909
Clarence Ip7a753bb2016-07-07 11:47:44 -04002910 /* range properties */
2911 msm_property_install_range(&sde_crtc->property_info,
Dhaval Patel4e574842016-08-23 15:11:37 -07002912 "input_fence_timeout", 0x0, 0, SDE_CRTC_MAX_INPUT_FENCE_TIMEOUT,
2913 SDE_CRTC_INPUT_FENCE_TIMEOUT, CRTC_PROP_INPUT_FENCE_TIMEOUT);
2914
2915 msm_property_install_range(&sde_crtc->property_info, "output_fence",
2916 0x0, 0, INR_OPEN_MAX, 0x0, CRTC_PROP_OUTPUT_FENCE);
Clarence Ip1d9728b2016-09-01 11:10:54 -04002917
2918 msm_property_install_range(&sde_crtc->property_info,
2919 "output_fence_offset", 0x0, 0, 1, 0,
2920 CRTC_PROP_OUTPUT_FENCE_OFFSET);
Dhaval Patele4a5dda2016-10-13 19:29:30 -07002921
Alan Kwong9aa061c2016-11-06 21:17:12 -05002922 msm_property_install_range(&sde_crtc->property_info,
2923 "core_clk", 0x0, 0, U64_MAX,
2924 sde_kms->perf.max_core_clk_rate,
2925 CRTC_PROP_CORE_CLK);
2926 msm_property_install_range(&sde_crtc->property_info,
2927 "core_ab", 0x0, 0, U64_MAX,
Alan Kwongff30f4a2017-05-23 12:02:00 -07002928 catalog->perf.max_bw_high * 1000ULL,
Alan Kwong9aa061c2016-11-06 21:17:12 -05002929 CRTC_PROP_CORE_AB);
2930 msm_property_install_range(&sde_crtc->property_info,
2931 "core_ib", 0x0, 0, U64_MAX,
Alan Kwongff30f4a2017-05-23 12:02:00 -07002932 catalog->perf.max_bw_high * 1000ULL,
Alan Kwong9aa061c2016-11-06 21:17:12 -05002933 CRTC_PROP_CORE_IB);
Alan Kwong4aacd532017-02-04 18:51:33 -08002934 msm_property_install_range(&sde_crtc->property_info,
Alan Kwong8c176bf2017-02-09 19:34:32 -08002935 "mem_ab", 0x0, 0, U64_MAX,
Alan Kwongff30f4a2017-05-23 12:02:00 -07002936 catalog->perf.max_bw_high * 1000ULL,
Alan Kwong8c176bf2017-02-09 19:34:32 -08002937 CRTC_PROP_MEM_AB);
2938 msm_property_install_range(&sde_crtc->property_info,
2939 "mem_ib", 0x0, 0, U64_MAX,
Alan Kwongff30f4a2017-05-23 12:02:00 -07002940 catalog->perf.max_bw_high * 1000ULL,
Alan Kwong8c176bf2017-02-09 19:34:32 -08002941 CRTC_PROP_MEM_IB);
2942 msm_property_install_range(&sde_crtc->property_info,
Alan Kwong4aacd532017-02-04 18:51:33 -08002943 "rot_prefill_bw", 0, 0, U64_MAX,
2944 catalog->perf.max_bw_high * 1000ULL,
2945 CRTC_PROP_ROT_PREFILL_BW);
Alan Kwong8c176bf2017-02-09 19:34:32 -08002946 msm_property_install_range(&sde_crtc->property_info,
2947 "rot_clk", 0, 0, U64_MAX,
2948 sde_kms->perf.max_core_clk_rate,
2949 CRTC_PROP_ROT_CLK);
Alan Kwong9aa061c2016-11-06 21:17:12 -05002950
Dhaval Patele4a5dda2016-10-13 19:29:30 -07002951 msm_property_install_blob(&sde_crtc->property_info, "capabilities",
2952 DRM_MODE_PROP_IMMUTABLE, CRTC_PROP_INFO);
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08002953
Lloyd Atkinson8ba47032017-03-22 17:13:32 -04002954 msm_property_install_volatile_range(&sde_crtc->property_info,
2955 "sde_drm_roi_v1", 0x0, 0, ~0, 0, CRTC_PROP_ROI_V1);
2956
Dhaval Patele4a5dda2016-10-13 19:29:30 -07002957 sde_kms_info_reset(info);
2958
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -07002959 if (catalog->has_dim_layer) {
2960 msm_property_install_volatile_range(&sde_crtc->property_info,
2961 "dim_layer_v1", 0x0, 0, ~0, 0, CRTC_PROP_DIM_LAYER_V1);
2962 sde_kms_info_add_keyint(info, "dim_layer_v1_max_layers",
2963 SDE_MAX_DIM_LAYERS);
2964 }
2965
Dhaval Patele4a5dda2016-10-13 19:29:30 -07002966 sde_kms_info_add_keyint(info, "hw_version", catalog->hwversion);
2967 sde_kms_info_add_keyint(info, "max_linewidth",
2968 catalog->max_mixer_width);
2969 sde_kms_info_add_keyint(info, "max_blendstages",
2970 catalog->max_mixer_blendstages);
2971 if (catalog->qseed_type == SDE_SSPP_SCALER_QSEED2)
2972 sde_kms_info_add_keystr(info, "qseed_type", "qseed2");
2973 if (catalog->qseed_type == SDE_SSPP_SCALER_QSEED3)
2974 sde_kms_info_add_keystr(info, "qseed_type", "qseed3");
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08002975
2976 if (sde_is_custom_client()) {
2977 if (catalog->smart_dma_rev == SDE_SSPP_SMART_DMA_V1)
2978 sde_kms_info_add_keystr(info,
2979 "smart_dma_rev", "smart_dma_v1");
2980 if (catalog->smart_dma_rev == SDE_SSPP_SMART_DMA_V2)
2981 sde_kms_info_add_keystr(info,
2982 "smart_dma_rev", "smart_dma_v2");
2983 }
2984
Dhaval Patele4a5dda2016-10-13 19:29:30 -07002985 sde_kms_info_add_keyint(info, "has_src_split", catalog->has_src_split);
Alan Kwong2f84f8a2016-12-29 13:07:47 -05002986 if (catalog->perf.max_bw_low)
2987 sde_kms_info_add_keyint(info, "max_bandwidth_low",
Alan Kwong6259a382017-04-04 06:18:02 -07002988 catalog->perf.max_bw_low * 1000LL);
Alan Kwong2f84f8a2016-12-29 13:07:47 -05002989 if (catalog->perf.max_bw_high)
2990 sde_kms_info_add_keyint(info, "max_bandwidth_high",
Alan Kwong6259a382017-04-04 06:18:02 -07002991 catalog->perf.max_bw_high * 1000LL);
Alan Kwong2f84f8a2016-12-29 13:07:47 -05002992 if (sde_kms->perf.max_core_clk_rate)
2993 sde_kms_info_add_keyint(info, "max_mdp_clk",
2994 sde_kms->perf.max_core_clk_rate);
Alan Kwong6259a382017-04-04 06:18:02 -07002995 sde_kms_info_add_keystr(info, "core_ib_ff",
2996 catalog->perf.core_ib_ff);
2997 sde_kms_info_add_keystr(info, "core_clk_ff",
2998 catalog->perf.core_clk_ff);
2999 sde_kms_info_add_keystr(info, "comp_ratio_rt",
3000 catalog->perf.comp_ratio_rt);
3001 sde_kms_info_add_keystr(info, "comp_ratio_nrt",
3002 catalog->perf.comp_ratio_nrt);
3003 sde_kms_info_add_keyint(info, "dest_scale_prefill_lines",
3004 catalog->perf.dest_scale_prefill_lines);
3005 sde_kms_info_add_keyint(info, "undersized_prefill_lines",
3006 catalog->perf.undersized_prefill_lines);
3007 sde_kms_info_add_keyint(info, "macrotile_prefill_lines",
3008 catalog->perf.macrotile_prefill_lines);
3009 sde_kms_info_add_keyint(info, "yuv_nv12_prefill_lines",
3010 catalog->perf.yuv_nv12_prefill_lines);
3011 sde_kms_info_add_keyint(info, "linear_prefill_lines",
3012 catalog->perf.linear_prefill_lines);
3013 sde_kms_info_add_keyint(info, "downscaling_prefill_lines",
3014 catalog->perf.downscaling_prefill_lines);
3015 sde_kms_info_add_keyint(info, "xtra_prefill_lines",
3016 catalog->perf.xtra_prefill_lines);
3017 sde_kms_info_add_keyint(info, "amortizable_threshold",
3018 catalog->perf.amortizable_threshold);
3019 sde_kms_info_add_keyint(info, "min_prefill_lines",
3020 catalog->perf.min_prefill_lines);
3021
Dhaval Patele4a5dda2016-10-13 19:29:30 -07003022 msm_property_set_blob(&sde_crtc->property_info, &sde_crtc->blob_info,
3023 info->data, info->len, CRTC_PROP_INFO);
3024
3025 kfree(info);
Clarence Ip7a753bb2016-07-07 11:47:44 -04003026}
3027
3028/**
3029 * sde_crtc_atomic_set_property - atomically set a crtc drm property
3030 * @crtc: Pointer to drm crtc structure
3031 * @state: Pointer to drm crtc state structure
3032 * @property: Pointer to targeted drm property
3033 * @val: Updated property value
3034 * @Returns: Zero on success
3035 */
3036static int sde_crtc_atomic_set_property(struct drm_crtc *crtc,
3037 struct drm_crtc_state *state,
3038 struct drm_property *property,
3039 uint64_t val)
3040{
3041 struct sde_crtc *sde_crtc;
3042 struct sde_crtc_state *cstate;
Clarence Ipcae1bb62016-07-07 12:07:13 -04003043 int idx, ret = -EINVAL;
Clarence Ip7a753bb2016-07-07 11:47:44 -04003044
3045 if (!crtc || !state || !property) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07003046 SDE_ERROR("invalid argument(s)\n");
Clarence Ip7a753bb2016-07-07 11:47:44 -04003047 } else {
3048 sde_crtc = to_sde_crtc(crtc);
3049 cstate = to_sde_crtc_state(state);
3050 ret = msm_property_atomic_set(&sde_crtc->property_info,
3051 cstate->property_values, cstate->property_blobs,
3052 property, val);
Clarence Ipcae1bb62016-07-07 12:07:13 -04003053 if (!ret) {
3054 idx = msm_property_index(&sde_crtc->property_info,
3055 property);
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08003056 switch (idx) {
3057 case CRTC_PROP_INPUT_FENCE_TIMEOUT:
Clarence Ipcae1bb62016-07-07 12:07:13 -04003058 _sde_crtc_set_input_fence_timeout(cstate);
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08003059 break;
3060 case CRTC_PROP_DIM_LAYER_V1:
3061 _sde_crtc_set_dim_layer_v1(cstate, (void *)val);
3062 break;
Lloyd Atkinson8ba47032017-03-22 17:13:32 -04003063 case CRTC_PROP_ROI_V1:
3064 ret = _sde_crtc_set_roi_v1(state, (void *)val);
3065 break;
Alan Kwongff30f4a2017-05-23 12:02:00 -07003066 case CRTC_PROP_CORE_AB:
3067 case CRTC_PROP_CORE_IB:
3068 case CRTC_PROP_MEM_AB:
3069 case CRTC_PROP_MEM_IB:
3070 cstate->bw_control = true;
3071 break;
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08003072 default:
3073 /* nothing to do */
3074 break;
3075 }
Gopikrishnaiah Anandane0e5e0c2016-05-25 11:05:33 -07003076 } else {
3077 ret = sde_cp_crtc_set_property(crtc,
3078 property, val);
Clarence Ipcae1bb62016-07-07 12:07:13 -04003079 }
Gopikrishnaiah Anandane0e5e0c2016-05-25 11:05:33 -07003080 if (ret)
3081 DRM_ERROR("failed to set the property\n");
Alan Kwongcdb2f282017-03-18 13:42:06 -07003082
3083 SDE_DEBUG("crtc%d %s[%d] <= 0x%llx ret=%d\n", crtc->base.id,
3084 property->name, property->base.id, val, ret);
Clarence Ip7a753bb2016-07-07 11:47:44 -04003085 }
3086
3087 return ret;
3088}
3089
3090/**
3091 * sde_crtc_set_property - set a crtc drm property
3092 * @crtc: Pointer to drm crtc structure
3093 * @property: Pointer to targeted drm property
3094 * @val: Updated property value
3095 * @Returns: Zero on success
3096 */
3097static int sde_crtc_set_property(struct drm_crtc *crtc,
3098 struct drm_property *property, uint64_t val)
3099{
Lloyd Atkinson4f1c8692016-09-14 14:04:25 -04003100 SDE_DEBUG("\n");
Clarence Ip7a753bb2016-07-07 11:47:44 -04003101
3102 return sde_crtc_atomic_set_property(crtc, crtc->state, property, val);
3103}
3104
3105/**
3106 * sde_crtc_atomic_get_property - retrieve a crtc drm property
3107 * @crtc: Pointer to drm crtc structure
3108 * @state: Pointer to drm crtc state structure
3109 * @property: Pointer to targeted drm property
3110 * @val: Pointer to variable for receiving property value
3111 * @Returns: Zero on success
3112 */
3113static int sde_crtc_atomic_get_property(struct drm_crtc *crtc,
3114 const struct drm_crtc_state *state,
3115 struct drm_property *property,
3116 uint64_t *val)
3117{
3118 struct sde_crtc *sde_crtc;
3119 struct sde_crtc_state *cstate;
Clarence Ip24f80662016-06-13 19:05:32 -04003120 int i, ret = -EINVAL;
Dhaval Patel5cb59be2017-04-20 20:00:56 -07003121 bool conn_offset = 0;
Clarence Ip7a753bb2016-07-07 11:47:44 -04003122
3123 if (!crtc || !state) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07003124 SDE_ERROR("invalid argument(s)\n");
Clarence Ip7a753bb2016-07-07 11:47:44 -04003125 } else {
3126 sde_crtc = to_sde_crtc(crtc);
3127 cstate = to_sde_crtc_state(state);
Dhaval Patel5cb59be2017-04-20 20:00:56 -07003128
3129 for (i = 0; i < cstate->num_connectors; ++i) {
3130 conn_offset = sde_connector_needs_offset(
3131 cstate->connectors[i]);
3132 if (conn_offset)
3133 break;
3134 }
3135
Clarence Ip24f80662016-06-13 19:05:32 -04003136 i = msm_property_index(&sde_crtc->property_info, property);
3137 if (i == CRTC_PROP_OUTPUT_FENCE) {
Dhaval Patel39323d42017-03-01 23:48:24 -08003138 uint32_t offset = sde_crtc_get_property(cstate,
Clarence Ip1d9728b2016-09-01 11:10:54 -04003139 CRTC_PROP_OUTPUT_FENCE_OFFSET);
3140
Dhaval Patel5cb59be2017-04-20 20:00:56 -07003141 ret = sde_fence_create(&sde_crtc->output_fence, val,
3142 offset + conn_offset);
Clarence Ip1d9728b2016-09-01 11:10:54 -04003143 if (ret)
3144 SDE_ERROR("fence create failed\n");
Clarence Ip24f80662016-06-13 19:05:32 -04003145 } else {
3146 ret = msm_property_atomic_get(&sde_crtc->property_info,
3147 cstate->property_values,
3148 cstate->property_blobs, property, val);
Gopikrishnaiah Anandane0e5e0c2016-05-25 11:05:33 -07003149 if (ret)
3150 ret = sde_cp_crtc_get_property(crtc,
3151 property, val);
Clarence Ip24f80662016-06-13 19:05:32 -04003152 }
Gopikrishnaiah Anandane0e5e0c2016-05-25 11:05:33 -07003153 if (ret)
3154 DRM_ERROR("get property failed\n");
Clarence Ip7a753bb2016-07-07 11:47:44 -04003155 }
Clarence Ip7a753bb2016-07-07 11:47:44 -04003156 return ret;
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07003157}
3158
Alan Kwong67a3f792016-11-01 23:16:53 -04003159#ifdef CONFIG_DEBUG_FS
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07003160static int _sde_debugfs_status_show(struct seq_file *s, void *data)
Clarence Ip8f7366c2016-07-05 12:15:26 -04003161{
3162 struct sde_crtc *sde_crtc;
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07003163 struct sde_plane_state *pstate = NULL;
Clarence Ip8f7366c2016-07-05 12:15:26 -04003164 struct sde_crtc_mixer *m;
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07003165
3166 struct drm_crtc *crtc;
3167 struct drm_plane *plane;
3168 struct drm_display_mode *mode;
3169 struct drm_framebuffer *fb;
3170 struct drm_plane_state *state;
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -07003171 struct sde_crtc_state *cstate;
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07003172
3173 int i, out_width;
Clarence Ip8f7366c2016-07-05 12:15:26 -04003174
3175 if (!s || !s->private)
3176 return -EINVAL;
3177
3178 sde_crtc = s->private;
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07003179 crtc = &sde_crtc->base;
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -07003180 cstate = to_sde_crtc_state(crtc->state);
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07003181
3182 mutex_lock(&sde_crtc->crtc_lock);
3183 mode = &crtc->state->adjusted_mode;
3184 out_width = sde_crtc_mixer_width(sde_crtc, mode);
3185
3186 seq_printf(s, "crtc:%d width:%d height:%d\n", crtc->base.id,
3187 mode->hdisplay, mode->vdisplay);
3188
3189 seq_puts(s, "\n");
3190
Clarence Ip8f7366c2016-07-05 12:15:26 -04003191 for (i = 0; i < sde_crtc->num_mixers; ++i) {
Lloyd Atkinsone7bcdd22016-08-11 10:53:37 -04003192 m = &sde_crtc->mixers[i];
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07003193 if (!m->hw_lm)
3194 seq_printf(s, "\tmixer[%d] has no lm\n", i);
3195 else if (!m->hw_ctl)
3196 seq_printf(s, "\tmixer[%d] has no ctl\n", i);
3197 else
3198 seq_printf(s, "\tmixer:%d ctl:%d width:%d height:%d\n",
3199 m->hw_lm->idx - LM_0, m->hw_ctl->idx - CTL_0,
3200 out_width, mode->vdisplay);
Clarence Ip8f7366c2016-07-05 12:15:26 -04003201 }
Dhaval Patel44f12472016-08-29 12:19:47 -07003202
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07003203 seq_puts(s, "\n");
Dhaval Patel48c76022016-09-01 17:51:23 -07003204
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -07003205 for (i = 0; i < cstate->num_dim_layers; i++) {
3206 struct sde_hw_dim_layer *dim_layer = &cstate->dim_layer[i];
3207
3208 seq_printf(s, "\tdim_layer:%d] stage:%d flags:%d\n",
3209 i, dim_layer->stage, dim_layer->flags);
3210 seq_printf(s, "\tdst_x:%d dst_y:%d dst_w:%d dst_h:%d\n",
3211 dim_layer->rect.x, dim_layer->rect.y,
3212 dim_layer->rect.w, dim_layer->rect.h);
3213 seq_printf(s,
3214 "\tcolor_0:%d color_1:%d color_2:%d color_3:%d\n",
3215 dim_layer->color_fill.color_0,
3216 dim_layer->color_fill.color_1,
3217 dim_layer->color_fill.color_2,
3218 dim_layer->color_fill.color_3);
3219 seq_puts(s, "\n");
3220 }
3221
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07003222 drm_atomic_crtc_for_each_plane(plane, crtc) {
3223 pstate = to_sde_plane_state(plane->state);
3224 state = plane->state;
3225
3226 if (!pstate || !state)
3227 continue;
3228
3229 seq_printf(s, "\tplane:%u stage:%d\n", plane->base.id,
3230 pstate->stage);
3231
3232 if (plane->state->fb) {
3233 fb = plane->state->fb;
3234
3235 seq_printf(s, "\tfb:%d image format:%4.4s wxh:%ux%u bpp:%d\n",
3236 fb->base.id, (char *) &fb->pixel_format,
3237 fb->width, fb->height, fb->bits_per_pixel);
3238
3239 seq_puts(s, "\t");
3240 for (i = 0; i < ARRAY_SIZE(fb->modifier); i++)
3241 seq_printf(s, "modifier[%d]:%8llu ", i,
3242 fb->modifier[i]);
3243 seq_puts(s, "\n");
3244
3245 seq_puts(s, "\t");
3246 for (i = 0; i < ARRAY_SIZE(fb->pitches); i++)
3247 seq_printf(s, "pitches[%d]:%8u ", i,
3248 fb->pitches[i]);
3249 seq_puts(s, "\n");
3250
3251 seq_puts(s, "\t");
3252 for (i = 0; i < ARRAY_SIZE(fb->offsets); i++)
3253 seq_printf(s, "offsets[%d]:%8u ", i,
3254 fb->offsets[i]);
Dhaval Patel48c76022016-09-01 17:51:23 -07003255 seq_puts(s, "\n");
3256 }
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07003257
3258 seq_printf(s, "\tsrc_x:%4d src_y:%4d src_w:%4d src_h:%4d\n",
3259 state->src_x, state->src_y, state->src_w, state->src_h);
3260
3261 seq_printf(s, "\tdst x:%4d dst_y:%4d dst_w:%4d dst_h:%4d\n",
3262 state->crtc_x, state->crtc_y, state->crtc_w,
3263 state->crtc_h);
Jeykumar Sankarane964dc72017-05-10 19:26:43 -07003264 seq_printf(s, "\tmultirect: mode: %d index: %d\n",
3265 pstate->multirect_mode, pstate->multirect_index);
Veera Sundaram Sankaran58e12812017-05-05 11:51:09 -07003266
3267 seq_printf(s, "\texcl_rect: x:%4d y:%4d w:%4d h:%4d\n",
3268 pstate->excl_rect.x, pstate->excl_rect.y,
3269 pstate->excl_rect.w, pstate->excl_rect.h);
3270
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07003271 seq_puts(s, "\n");
Clarence Ip8f7366c2016-07-05 12:15:26 -04003272 }
Alan Kwong07da0982016-11-04 12:57:45 -04003273
3274 if (sde_crtc->vblank_cb_count) {
3275 ktime_t diff = ktime_sub(ktime_get(), sde_crtc->vblank_cb_time);
3276 s64 diff_ms = ktime_to_ms(diff);
3277 s64 fps = diff_ms ? DIV_ROUND_CLOSEST(
3278 sde_crtc->vblank_cb_count * 1000, diff_ms) : 0;
3279
3280 seq_printf(s,
3281 "vblank fps:%lld count:%u total:%llums\n",
3282 fps,
3283 sde_crtc->vblank_cb_count,
3284 ktime_to_ms(diff));
3285
3286 /* reset time & count for next measurement */
3287 sde_crtc->vblank_cb_count = 0;
3288 sde_crtc->vblank_cb_time = ktime_set(0, 0);
3289 }
3290
3291 seq_printf(s, "vblank_refcount:%d\n",
3292 atomic_read(&sde_crtc->vblank_refcount));
3293
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07003294 mutex_unlock(&sde_crtc->crtc_lock);
3295
Clarence Ip8f7366c2016-07-05 12:15:26 -04003296 return 0;
3297}
3298
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07003299static int _sde_debugfs_status_open(struct inode *inode, struct file *file)
Clarence Ip8f7366c2016-07-05 12:15:26 -04003300{
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07003301 return single_open(file, _sde_debugfs_status_show, inode->i_private);
Clarence Ip8f7366c2016-07-05 12:15:26 -04003302}
3303
Dhaval Patelf9245d62017-03-28 16:24:00 -07003304static ssize_t _sde_crtc_misr_setup(struct file *file,
3305 const char __user *user_buf, size_t count, loff_t *ppos)
3306{
3307 struct sde_crtc *sde_crtc;
3308 struct sde_crtc_mixer *m;
3309 int i = 0, rc;
3310 char buf[MISR_BUFF_SIZE + 1];
3311 u32 frame_count, enable;
3312 size_t buff_copy;
3313
3314 if (!file || !file->private_data)
3315 return -EINVAL;
3316
3317 sde_crtc = file->private_data;
3318 buff_copy = min_t(size_t, count, MISR_BUFF_SIZE);
3319 if (copy_from_user(buf, user_buf, buff_copy)) {
3320 SDE_ERROR("buffer copy failed\n");
3321 return -EINVAL;
3322 }
3323
3324 buf[buff_copy] = 0; /* end of string */
3325
3326 if (sscanf(buf, "%u %u", &enable, &frame_count) != 2)
3327 return -EINVAL;
3328
3329 rc = _sde_crtc_power_enable(sde_crtc, true);
3330 if (rc)
3331 return rc;
3332
3333 mutex_lock(&sde_crtc->crtc_lock);
3334 sde_crtc->misr_enable = enable;
3335 for (i = 0; i < sde_crtc->num_mixers; ++i) {
3336 m = &sde_crtc->mixers[i];
3337 if (!m->hw_lm)
3338 continue;
3339
3340 m->hw_lm->ops.setup_misr(m->hw_lm, enable, frame_count);
3341 }
3342 mutex_unlock(&sde_crtc->crtc_lock);
3343 _sde_crtc_power_enable(sde_crtc, false);
3344
3345 return count;
3346}
3347
3348static ssize_t _sde_crtc_misr_read(struct file *file,
3349 char __user *user_buff, size_t count, loff_t *ppos)
3350{
3351 struct sde_crtc *sde_crtc;
3352 struct sde_crtc_mixer *m;
3353 int i = 0, rc;
3354 ssize_t len = 0;
3355 char buf[MISR_BUFF_SIZE + 1] = {'\0'};
3356
3357 if (*ppos)
3358 return 0;
3359
3360 if (!file || !file->private_data)
3361 return -EINVAL;
3362
3363 sde_crtc = file->private_data;
3364 rc = _sde_crtc_power_enable(sde_crtc, true);
3365 if (rc)
3366 return rc;
3367
3368 mutex_lock(&sde_crtc->crtc_lock);
3369 if (!sde_crtc->misr_enable) {
3370 len += snprintf(buf + len, MISR_BUFF_SIZE - len,
3371 "disabled\n");
3372 goto buff_check;
3373 }
3374
3375 for (i = 0; i < sde_crtc->num_mixers; ++i) {
3376 m = &sde_crtc->mixers[i];
3377 if (!m->hw_lm)
3378 continue;
3379
3380 len += snprintf(buf + len, MISR_BUFF_SIZE - len, "lm idx:%d\n",
3381 m->hw_lm->idx - LM_0);
3382 len += snprintf(buf + len, MISR_BUFF_SIZE - len, "0x%x\n",
3383 m->hw_lm->ops.collect_misr(m->hw_lm));
3384 }
3385
3386buff_check:
3387 if (count <= len) {
3388 len = 0;
3389 goto end;
3390 }
3391
3392 if (copy_to_user(user_buff, buf, len)) {
3393 len = -EFAULT;
3394 goto end;
3395 }
3396
3397 *ppos += len; /* increase offset */
3398
3399end:
3400 mutex_unlock(&sde_crtc->crtc_lock);
3401 _sde_crtc_power_enable(sde_crtc, false);
3402 return len;
3403}
3404
3405#define DEFINE_SDE_DEBUGFS_SEQ_FOPS(__prefix) \
Alan Kwong67a3f792016-11-01 23:16:53 -04003406static int __prefix ## _open(struct inode *inode, struct file *file) \
3407{ \
3408 return single_open(file, __prefix ## _show, inode->i_private); \
3409} \
3410static const struct file_operations __prefix ## _fops = { \
3411 .owner = THIS_MODULE, \
3412 .open = __prefix ## _open, \
3413 .release = single_release, \
3414 .read = seq_read, \
3415 .llseek = seq_lseek, \
3416}
3417
3418static int sde_crtc_debugfs_state_show(struct seq_file *s, void *v)
3419{
3420 struct drm_crtc *crtc = (struct drm_crtc *) s->private;
3421 struct sde_crtc_state *cstate = to_sde_crtc_state(crtc->state);
Alan Kwongcdb2f282017-03-18 13:42:06 -07003422 struct sde_crtc_res *res;
Alan Kwong67a3f792016-11-01 23:16:53 -04003423
3424 seq_printf(s, "num_connectors: %d\n", cstate->num_connectors);
Dhaval Patel4d424602017-02-18 19:40:14 -08003425 seq_printf(s, "client type: %d\n", sde_crtc_get_client_type(crtc));
Alan Kwong3e985f02017-02-12 15:08:44 -08003426 seq_printf(s, "intf_mode: %d\n", sde_crtc_get_intf_mode(crtc));
Alan Kwong9aa061c2016-11-06 21:17:12 -05003427 seq_printf(s, "bw_ctl: %llu\n", cstate->cur_perf.bw_ctl);
Alan Kwong97abd432017-05-11 14:52:23 -07003428 seq_printf(s, "core_clk_rate: %llu\n", cstate->cur_perf.core_clk_rate);
Alan Kwong9aa061c2016-11-06 21:17:12 -05003429 seq_printf(s, "max_per_pipe_ib: %llu\n",
3430 cstate->cur_perf.max_per_pipe_ib);
Alan Kwong67a3f792016-11-01 23:16:53 -04003431
Alan Kwongcdb2f282017-03-18 13:42:06 -07003432 seq_printf(s, "rp.%d: ", cstate->rp.sequence_id);
3433 list_for_each_entry(res, &cstate->rp.res_list, list)
3434 seq_printf(s, "0x%x/0x%llx/%pK/%d ",
3435 res->type, res->tag, res->val,
3436 atomic_read(&res->refcount));
3437 seq_puts(s, "\n");
3438
Alan Kwong67a3f792016-11-01 23:16:53 -04003439 return 0;
3440}
3441DEFINE_SDE_DEBUGFS_SEQ_FOPS(sde_crtc_debugfs_state);
3442
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07003443static int _sde_crtc_init_debugfs(struct drm_crtc *crtc)
Clarence Ip8f7366c2016-07-05 12:15:26 -04003444{
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07003445 struct sde_crtc *sde_crtc;
3446 struct sde_kms *sde_kms;
3447
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07003448 static const struct file_operations debugfs_status_fops = {
3449 .open = _sde_debugfs_status_open,
Clarence Ip8f7366c2016-07-05 12:15:26 -04003450 .read = seq_read,
3451 .llseek = seq_lseek,
3452 .release = single_release,
3453 };
Dhaval Patelf9245d62017-03-28 16:24:00 -07003454 static const struct file_operations debugfs_misr_fops = {
3455 .open = simple_open,
3456 .read = _sde_crtc_misr_read,
3457 .write = _sde_crtc_misr_setup,
3458 };
Alan Kwong67a3f792016-11-01 23:16:53 -04003459
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07003460 if (!crtc)
3461 return -EINVAL;
3462 sde_crtc = to_sde_crtc(crtc);
3463
3464 sde_kms = _sde_crtc_get_kms(crtc);
3465 if (!sde_kms)
3466 return -EINVAL;
3467
3468 sde_crtc->debugfs_root = debugfs_create_dir(sde_crtc->name,
Lloyd Atkinson09e64bf2017-04-13 14:09:59 -07003469 crtc->dev->primary->debugfs_root);
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07003470 if (!sde_crtc->debugfs_root)
3471 return -ENOMEM;
3472
3473 /* don't error check these */
3474 debugfs_create_file("status", 0444,
3475 sde_crtc->debugfs_root,
3476 sde_crtc, &debugfs_status_fops);
3477 debugfs_create_file("state", 0644,
3478 sde_crtc->debugfs_root,
3479 &sde_crtc->base,
3480 &sde_crtc_debugfs_state_fops);
Dhaval Patelf9245d62017-03-28 16:24:00 -07003481 debugfs_create_file("misr_data", 0644, sde_crtc->debugfs_root,
3482 sde_crtc, &debugfs_misr_fops);
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07003483
3484 return 0;
3485}
3486
3487static void _sde_crtc_destroy_debugfs(struct drm_crtc *crtc)
3488{
3489 struct sde_crtc *sde_crtc;
3490
3491 if (!crtc)
3492 return;
3493 sde_crtc = to_sde_crtc(crtc);
3494 debugfs_remove_recursive(sde_crtc->debugfs_root);
Clarence Ip8f7366c2016-07-05 12:15:26 -04003495}
Alan Kwong67a3f792016-11-01 23:16:53 -04003496#else
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07003497static int _sde_crtc_init_debugfs(struct drm_crtc *crtc)
Alan Kwong67a3f792016-11-01 23:16:53 -04003498{
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07003499 return 0;
Alan Kwong67a3f792016-11-01 23:16:53 -04003500}
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07003501
3502static void _sde_crtc_destroy_debugfs(struct drm_crtc *crtc)
3503{
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07003504}
3505#endif /* CONFIG_DEBUG_FS */
3506
3507static int sde_crtc_late_register(struct drm_crtc *crtc)
3508{
3509 return _sde_crtc_init_debugfs(crtc);
3510}
3511
3512static void sde_crtc_early_unregister(struct drm_crtc *crtc)
3513{
3514 _sde_crtc_destroy_debugfs(crtc);
3515}
3516
3517static const struct drm_crtc_funcs sde_crtc_funcs = {
3518 .set_config = drm_atomic_helper_set_config,
3519 .destroy = sde_crtc_destroy,
3520 .page_flip = drm_atomic_helper_page_flip,
3521 .set_property = sde_crtc_set_property,
3522 .atomic_set_property = sde_crtc_atomic_set_property,
3523 .atomic_get_property = sde_crtc_atomic_get_property,
3524 .reset = sde_crtc_reset,
3525 .atomic_duplicate_state = sde_crtc_duplicate_state,
3526 .atomic_destroy_state = sde_crtc_destroy_state,
3527 .late_register = sde_crtc_late_register,
3528 .early_unregister = sde_crtc_early_unregister,
3529};
3530
3531static const struct drm_crtc_helper_funcs sde_crtc_helper_funcs = {
3532 .mode_fixup = sde_crtc_mode_fixup,
3533 .disable = sde_crtc_disable,
3534 .enable = sde_crtc_enable,
3535 .atomic_check = sde_crtc_atomic_check,
3536 .atomic_begin = sde_crtc_atomic_begin,
3537 .atomic_flush = sde_crtc_atomic_flush,
3538};
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07003539
Clarence Ipa18d4832017-03-13 12:35:44 -07003540static void _sde_crtc_event_cb(struct kthread_work *work)
3541{
3542 struct sde_crtc_event *event;
3543 struct sde_crtc *sde_crtc;
3544 unsigned long irq_flags;
3545
3546 if (!work) {
3547 SDE_ERROR("invalid work item\n");
3548 return;
3549 }
3550
3551 event = container_of(work, struct sde_crtc_event, kt_work);
Clarence Ipa18d4832017-03-13 12:35:44 -07003552
3553 /* set sde_crtc to NULL for static work structures */
3554 sde_crtc = event->sde_crtc;
3555 if (!sde_crtc)
3556 return;
3557
Gopikrishnaiah Anandanb6b401f2017-03-14 16:39:49 -07003558 if (event->cb_func)
3559 event->cb_func(&sde_crtc->base, event->usr);
3560
Clarence Ipa18d4832017-03-13 12:35:44 -07003561 spin_lock_irqsave(&sde_crtc->event_lock, irq_flags);
3562 list_add_tail(&event->list, &sde_crtc->event_free_list);
3563 spin_unlock_irqrestore(&sde_crtc->event_lock, irq_flags);
3564}
3565
3566int sde_crtc_event_queue(struct drm_crtc *crtc,
Gopikrishnaiah Anandanb6b401f2017-03-14 16:39:49 -07003567 void (*func)(struct drm_crtc *crtc, void *usr), void *usr)
Clarence Ipa18d4832017-03-13 12:35:44 -07003568{
3569 unsigned long irq_flags;
3570 struct sde_crtc *sde_crtc;
3571 struct sde_crtc_event *event = NULL;
3572
3573 if (!crtc || !func)
3574 return -EINVAL;
3575 sde_crtc = to_sde_crtc(crtc);
3576
Gopikrishnaiah Anandanb6b401f2017-03-14 16:39:49 -07003577 if (!sde_crtc->event_thread)
3578 return -EINVAL;
Clarence Ipa18d4832017-03-13 12:35:44 -07003579 /*
3580 * Obtain an event struct from the private cache. This event
3581 * queue may be called from ISR contexts, so use a private
3582 * cache to avoid calling any memory allocation functions.
3583 */
3584 spin_lock_irqsave(&sde_crtc->event_lock, irq_flags);
3585 if (!list_empty(&sde_crtc->event_free_list)) {
3586 event = list_first_entry(&sde_crtc->event_free_list,
3587 struct sde_crtc_event, list);
3588 list_del_init(&event->list);
3589 }
3590 spin_unlock_irqrestore(&sde_crtc->event_lock, irq_flags);
3591
3592 if (!event)
3593 return -ENOMEM;
3594
3595 /* populate event node */
3596 event->sde_crtc = sde_crtc;
3597 event->cb_func = func;
3598 event->usr = usr;
3599
3600 /* queue new event request */
3601 kthread_init_work(&event->kt_work, _sde_crtc_event_cb);
3602 kthread_queue_work(&sde_crtc->event_worker, &event->kt_work);
3603
3604 return 0;
3605}
3606
3607static int _sde_crtc_init_events(struct sde_crtc *sde_crtc)
3608{
3609 int i, rc = 0;
3610
3611 if (!sde_crtc) {
3612 SDE_ERROR("invalid crtc\n");
3613 return -EINVAL;
3614 }
3615
3616 spin_lock_init(&sde_crtc->event_lock);
3617
3618 INIT_LIST_HEAD(&sde_crtc->event_free_list);
3619 for (i = 0; i < SDE_CRTC_MAX_EVENT_COUNT; ++i)
3620 list_add_tail(&sde_crtc->event_cache[i].list,
3621 &sde_crtc->event_free_list);
3622
3623 kthread_init_worker(&sde_crtc->event_worker);
3624 sde_crtc->event_thread = kthread_run(kthread_worker_fn,
3625 &sde_crtc->event_worker, "crtc_event:%d",
3626 sde_crtc->base.base.id);
3627
3628 if (IS_ERR_OR_NULL(sde_crtc->event_thread)) {
3629 SDE_ERROR("failed to create event thread\n");
3630 rc = PTR_ERR(sde_crtc->event_thread);
3631 sde_crtc->event_thread = NULL;
3632 }
3633
3634 return rc;
3635}
3636
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04003637/* initialize crtc */
Lloyd Atkinsonac933642016-09-14 11:52:00 -04003638struct drm_crtc *sde_crtc_init(struct drm_device *dev, struct drm_plane *plane)
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07003639{
3640 struct drm_crtc *crtc = NULL;
Clarence Ip8f7366c2016-07-05 12:15:26 -04003641 struct sde_crtc *sde_crtc = NULL;
3642 struct msm_drm_private *priv = NULL;
3643 struct sde_kms *kms = NULL;
Clarence Ipa18d4832017-03-13 12:35:44 -07003644 int i, rc;
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07003645
Clarence Ip8f7366c2016-07-05 12:15:26 -04003646 priv = dev->dev_private;
3647 kms = to_sde_kms(priv->kms);
3648
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07003649 sde_crtc = kzalloc(sizeof(*sde_crtc), GFP_KERNEL);
3650 if (!sde_crtc)
3651 return ERR_PTR(-ENOMEM);
3652
3653 crtc = &sde_crtc->base;
Gopikrishnaiah Anandane0e5e0c2016-05-25 11:05:33 -07003654 crtc->dev = dev;
Alan Kwong07da0982016-11-04 12:57:45 -04003655 atomic_set(&sde_crtc->vblank_refcount, 0);
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07003656
Clarence Ip7f70ce42017-03-20 06:53:46 -07003657 mutex_init(&sde_crtc->crtc_lock);
Alan Kwong628d19e2016-10-31 13:50:13 -04003658 spin_lock_init(&sde_crtc->spin_lock);
3659 atomic_set(&sde_crtc->frame_pending, 0);
3660
3661 INIT_LIST_HEAD(&sde_crtc->frame_event_list);
Gopikrishnaiah Anandande2c81b2017-03-15 12:41:29 -07003662 INIT_LIST_HEAD(&sde_crtc->user_event_list);
Alan Kwong628d19e2016-10-31 13:50:13 -04003663 for (i = 0; i < ARRAY_SIZE(sde_crtc->frame_events); i++) {
3664 INIT_LIST_HEAD(&sde_crtc->frame_events[i].list);
3665 list_add(&sde_crtc->frame_events[i].list,
3666 &sde_crtc->frame_event_list);
3667 kthread_init_work(&sde_crtc->frame_events[i].work,
3668 sde_crtc_frame_event_work);
3669 }
3670
Dhaval Patel04c7e8e2016-09-26 20:14:31 -07003671 drm_crtc_init_with_planes(dev, crtc, plane, NULL, &sde_crtc_funcs,
3672 NULL);
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07003673
3674 drm_crtc_helper_add(crtc, &sde_crtc_helper_funcs);
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04003675 plane->crtc = crtc;
3676
Clarence Ip8f7366c2016-07-05 12:15:26 -04003677 /* save user friendly CRTC name for later */
3678 snprintf(sde_crtc->name, SDE_CRTC_NAME_SIZE, "crtc%u", crtc->base.id);
3679
Clarence Ipa18d4832017-03-13 12:35:44 -07003680 /* initialize event handling */
3681 rc = _sde_crtc_init_events(sde_crtc);
3682 if (rc) {
3683 drm_crtc_cleanup(crtc);
3684 kfree(sde_crtc);
3685 return ERR_PTR(rc);
3686 }
3687
Clarence Ip9a74a442016-08-25 18:29:03 -04003688 /* initialize output fence support */
Lloyd Atkinson5d40d312016-09-06 08:34:13 -04003689 sde_fence_init(&sde_crtc->output_fence, sde_crtc->name, crtc->base.id);
Clarence Ip24f80662016-06-13 19:05:32 -04003690
Clarence Ip7a753bb2016-07-07 11:47:44 -04003691 /* create CRTC properties */
3692 msm_property_init(&sde_crtc->property_info, &crtc->base, dev,
3693 priv->crtc_property, sde_crtc->property_data,
3694 CRTC_PROP_COUNT, CRTC_PROP_BLOBCOUNT,
3695 sizeof(struct sde_crtc_state));
3696
Dhaval Patele4a5dda2016-10-13 19:29:30 -07003697 sde_crtc_install_properties(crtc, kms->catalog);
Gopikrishnaiah Anandan703eb902016-10-06 18:43:57 -07003698
3699 /* Install color processing properties */
Gopikrishnaiah Anandane0e5e0c2016-05-25 11:05:33 -07003700 sde_cp_crtc_init(crtc);
Gopikrishnaiah Anandan703eb902016-10-06 18:43:57 -07003701 sde_cp_crtc_install_properties(crtc);
Clarence Ip7a753bb2016-07-07 11:47:44 -04003702
Dhaval Patelec10fad2016-08-22 14:40:48 -07003703 SDE_DEBUG("%s: successfully initialized crtc\n", sde_crtc->name);
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07003704 return crtc;
3705}
Gopikrishnaiah Anandande2c81b2017-03-15 12:41:29 -07003706
Gopikrishnaiah Anandan5154c712017-02-27 17:48:24 -08003707static int _sde_crtc_event_enable(struct sde_kms *kms,
3708 struct drm_crtc *crtc_drm, u32 event)
Gopikrishnaiah Anandande2c81b2017-03-15 12:41:29 -07003709{
Gopikrishnaiah Anandan5154c712017-02-27 17:48:24 -08003710 struct sde_crtc *crtc = NULL;
3711 struct sde_crtc_irq_info *node;
3712 struct msm_drm_private *priv;
3713 unsigned long flags;
3714 bool found = false;
Gopikrishnaiah Anandanb6b401f2017-03-14 16:39:49 -07003715 int ret, i = 0;
Gopikrishnaiah Anandan5154c712017-02-27 17:48:24 -08003716
3717 crtc = to_sde_crtc(crtc_drm);
3718 spin_lock_irqsave(&crtc->spin_lock, flags);
3719 list_for_each_entry(node, &crtc->user_event_list, list) {
3720 if (node->event == event) {
3721 found = true;
3722 break;
3723 }
3724 }
3725 spin_unlock_irqrestore(&crtc->spin_lock, flags);
3726
3727 /* event already enabled */
3728 if (found)
3729 return 0;
3730
Gopikrishnaiah Anandanb6b401f2017-03-14 16:39:49 -07003731 node = NULL;
3732 for (i = 0; i < ARRAY_SIZE(custom_events); i++) {
3733 if (custom_events[i].event == event &&
3734 custom_events[i].func) {
3735 node = kzalloc(sizeof(*node), GFP_KERNEL);
3736 if (!node)
3737 return -ENOMEM;
3738 node->event = event;
3739 INIT_LIST_HEAD(&node->list);
3740 node->func = custom_events[i].func;
3741 node->event = event;
3742 break;
3743 }
3744 }
Gopikrishnaiah Anandan5154c712017-02-27 17:48:24 -08003745
Gopikrishnaiah Anandanb6b401f2017-03-14 16:39:49 -07003746 if (!node) {
Gopikrishnaiah Anandan5154c712017-02-27 17:48:24 -08003747 SDE_ERROR("unsupported event %x\n", event);
Gopikrishnaiah Anandan5154c712017-02-27 17:48:24 -08003748 return -EINVAL;
3749 }
3750
3751 priv = kms->dev->dev_private;
3752 ret = 0;
3753 if (crtc_drm->enabled) {
3754 sde_power_resource_enable(&priv->phandle, kms->core_client,
3755 true);
3756 ret = node->func(crtc_drm, true, &node->irq);
3757 sde_power_resource_enable(&priv->phandle, kms->core_client,
3758 false);
3759 }
3760
3761 if (!ret) {
3762 spin_lock_irqsave(&crtc->spin_lock, flags);
3763 list_add_tail(&node->list, &crtc->user_event_list);
3764 spin_unlock_irqrestore(&crtc->spin_lock, flags);
3765 } else {
3766 kfree(node);
3767 }
3768
3769 return ret;
3770}
3771
3772static int _sde_crtc_event_disable(struct sde_kms *kms,
3773 struct drm_crtc *crtc_drm, u32 event)
3774{
3775 struct sde_crtc *crtc = NULL;
3776 struct sde_crtc_irq_info *node = NULL;
3777 struct msm_drm_private *priv;
3778 unsigned long flags;
3779 bool found = false;
3780 int ret;
3781
3782 crtc = to_sde_crtc(crtc_drm);
3783 spin_lock_irqsave(&crtc->spin_lock, flags);
3784 list_for_each_entry(node, &crtc->user_event_list, list) {
3785 if (node->event == event) {
3786 list_del(&node->list);
3787 found = true;
3788 break;
3789 }
3790 }
3791 spin_unlock_irqrestore(&crtc->spin_lock, flags);
3792
3793 /* event already disabled */
3794 if (!found)
3795 return 0;
3796
3797 /**
3798 * crtc is disabled interrupts are cleared remove from the list,
3799 * no need to disable/de-register.
3800 */
3801 if (!crtc_drm->enabled) {
3802 kfree(node);
3803 return 0;
3804 }
3805 priv = kms->dev->dev_private;
3806 sde_power_resource_enable(&priv->phandle, kms->core_client, true);
3807 ret = node->func(crtc_drm, false, &node->irq);
3808 sde_power_resource_enable(&priv->phandle, kms->core_client, false);
3809 return ret;
3810}
3811
3812int sde_crtc_register_custom_event(struct sde_kms *kms,
3813 struct drm_crtc *crtc_drm, u32 event, bool en)
3814{
3815 struct sde_crtc *crtc = NULL;
3816 int ret;
3817
3818 crtc = to_sde_crtc(crtc_drm);
3819 if (!crtc || !kms || !kms->dev) {
3820 DRM_ERROR("invalid sde_crtc %pK kms %pK dev %pK\n", crtc,
3821 kms, ((kms) ? (kms->dev) : NULL));
3822 return -EINVAL;
3823 }
3824
3825 if (en)
3826 ret = _sde_crtc_event_enable(kms, crtc_drm, event);
3827 else
3828 ret = _sde_crtc_event_disable(kms, crtc_drm, event);
3829
3830 return ret;
Gopikrishnaiah Anandande2c81b2017-03-15 12:41:29 -07003831}
Gopikrishnaiah Anandan84b4f672017-04-26 10:28:51 -07003832
3833static int sde_crtc_power_interrupt_handler(struct drm_crtc *crtc_drm,
3834 bool en, struct sde_irq_callback *irq)
3835{
3836 return 0;
3837}